blob: d74bc7f4accc959329dc57be8232263736d4473c [file] [log] [blame]
[email protected]2a281332012-07-11 22:20:231// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]80cc3f72009-04-24 18:06:052// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]1faee3f02010-06-21 07:01:345#include "chrome/browser/resources_util.h"
[email protected]80cc3f72009-04-24 18:06:056
avib896c712015-12-26 02:10:437#include <stddef.h>
8
[email protected]c5212892010-09-08 06:30:339#include <utility>
10
brettw84cff3f2017-06-29 18:26:5011#include "base/containers/flat_map.h"
[email protected]80cc3f72009-04-24 18:06:0512#include "base/lazy_instance.h"
avib896c712015-12-26 02:10:4313#include "build/build_config.h"
thestig4a9b0ef2016-08-29 08:22:1214#include "chrome/grit/theme_resources_map.h"
15#include "components/grit/components_scaled_resources_map.h"
16#include "ui/resources/grit/ui_resources_map.h"
[email protected]80cc3f72009-04-24 18:06:0517
[email protected]cdd7bb82014-07-25 09:19:2618#if defined(OS_CHROMEOS)
thestig4a9b0ef2016-08-29 08:22:1219#include "ui/chromeos/resources/grit/ui_chromeos_resources_map.h"
[email protected]cdd7bb82014-07-25 09:19:2620#endif
21
[email protected]80cc3f72009-04-24 18:06:0522namespace {
23
brettw84cff3f2017-06-29 18:26:5024// A wrapper class that holds a map between resource strings and resource
[email protected]80cc3f72009-04-24 18:06:0525// ids. This is done so we can use base::LazyInstance which takes care of
brettw84cff3f2017-06-29 18:26:5026// thread safety in initializing the map for us.
[email protected]80cc3f72009-04-24 18:06:0527class ThemeMap {
28 public:
brettw84cff3f2017-06-29 18:26:5029 typedef base::flat_map<std::string, int> StringIntMap;
[email protected]80cc3f72009-04-24 18:06:0530
31 ThemeMap() {
brettw84cff3f2017-06-29 18:26:5032 // Construct in one-shot from a moved vector.
33 std::vector<StringIntMap::value_type> storage;
34
droger2a6ab542015-10-09 16:10:2835 for (size_t i = 0; i < kComponentsScaledResourcesSize; ++i) {
brettw84cff3f2017-06-29 18:26:5036 storage.emplace_back(kComponentsScaledResources[i].name,
37 kComponentsScaledResources[i].value);
droger2a6ab542015-10-09 16:10:2838 }
brettw84cff3f2017-06-29 18:26:5039 for (size_t i = 0; i < kThemeResourcesSize; ++i) {
40 storage.emplace_back(kThemeResources[i].name, kThemeResources[i].value);
41 }
42 for (size_t i = 0; i < kUiResourcesSize; ++i) {
43 storage.emplace_back(kUiResources[i].name, kUiResources[i].value);
44 }
[email protected]cdd7bb82014-07-25 09:19:2645#if defined(OS_CHROMEOS)
brettw84cff3f2017-06-29 18:26:5046 for (size_t i = 0; i < kUiChromeosResourcesSize; ++i) {
47 storage.emplace_back(kUiChromeosResources[i].name,
48 kUiChromeosResources[i].value);
49 }
[email protected]cdd7bb82014-07-25 09:19:2650#endif
brettw84cff3f2017-06-29 18:26:5051
52 id_map_ = StringIntMap(std::move(storage), base::KEEP_FIRST_OF_DUPES);
[email protected]80cc3f72009-04-24 18:06:0553 }
54
55 int GetId(const std::string& resource_name) {
brettw84cff3f2017-06-29 18:26:5056 auto it = id_map_.find(resource_name);
[email protected]80cc3f72009-04-24 18:06:0557 if (it == id_map_.end())
58 return -1;
59 return it->second;
60 }
61
62 private:
63 StringIntMap id_map_;
64};
65
scottmg5e65e3a2017-03-08 08:48:4666static base::LazyInstance<ThemeMap>::DestructorAtExit g_theme_ids =
67 LAZY_INSTANCE_INITIALIZER;
[email protected]80cc3f72009-04-24 18:06:0568
69} // namespace
70
[email protected]1faee3f02010-06-21 07:01:3471int ResourcesUtil::GetThemeResourceId(const std::string& resource_name) {
[email protected]80cc3f72009-04-24 18:06:0572 return g_theme_ids.Get().GetId(resource_name);
73}