OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/resources_util.h" | 5 #include "chrome/browser/resources_util.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
11 #include "base/containers/hash_tables.h" | 11 #include "base/containers/flat_map.h" |
12 #include "base/lazy_instance.h" | 12 #include "base/lazy_instance.h" |
13 #include "build/build_config.h" | 13 #include "build/build_config.h" |
14 #include "chrome/grit/theme_resources_map.h" | 14 #include "chrome/grit/theme_resources_map.h" |
15 #include "components/grit/components_scaled_resources_map.h" | 15 #include "components/grit/components_scaled_resources_map.h" |
16 #include "ui/resources/grit/ui_resources_map.h" | 16 #include "ui/resources/grit/ui_resources_map.h" |
17 | 17 |
18 #if defined(OS_CHROMEOS) | 18 #if defined(OS_CHROMEOS) |
19 #include "ui/chromeos/resources/grit/ui_chromeos_resources_map.h" | 19 #include "ui/chromeos/resources/grit/ui_chromeos_resources_map.h" |
20 #endif | 20 #endif |
21 | 21 |
22 namespace { | 22 namespace { |
23 | 23 |
24 // A wrapper class that holds a hash_map between resource strings and resource | 24 // A wrapper class that holds a map between resource strings and resource |
25 // ids. This is done so we can use base::LazyInstance which takes care of | 25 // ids. This is done so we can use base::LazyInstance which takes care of |
26 // thread safety in initializing the hash_map for us. | 26 // thread safety in initializing the map for us. |
27 class ThemeMap { | 27 class ThemeMap { |
28 public: | 28 public: |
29 typedef base::hash_map<std::string, int> StringIntMap; | 29 typedef base::flat_map<std::string, int> StringIntMap; |
30 | 30 |
31 ThemeMap() { | 31 ThemeMap() { |
| 32 // Construct in one-shot from a moved vector. |
| 33 std::vector<StringIntMap::value_type> storage; |
| 34 |
32 for (size_t i = 0; i < kComponentsScaledResourcesSize; ++i) { | 35 for (size_t i = 0; i < kComponentsScaledResourcesSize; ++i) { |
33 id_map_[kComponentsScaledResources[i].name] = | 36 storage.emplace_back(kComponentsScaledResources[i].name, |
34 kComponentsScaledResources[i].value; | 37 kComponentsScaledResources[i].value); |
35 } | 38 } |
36 for (size_t i = 0; i < kThemeResourcesSize; ++i) | 39 for (size_t i = 0; i < kThemeResourcesSize; ++i) { |
37 id_map_[kThemeResources[i].name] = kThemeResources[i].value; | 40 storage.emplace_back(kThemeResources[i].name, kThemeResources[i].value); |
38 for (size_t i = 0; i < kUiResourcesSize; ++i) | 41 } |
39 id_map_[kUiResources[i].name] = kUiResources[i].value; | 42 for (size_t i = 0; i < kUiResourcesSize; ++i) { |
| 43 storage.emplace_back(kUiResources[i].name, kUiResources[i].value); |
| 44 } |
40 #if defined(OS_CHROMEOS) | 45 #if defined(OS_CHROMEOS) |
41 for (size_t i = 0; i < kUiChromeosResourcesSize; ++i) | 46 for (size_t i = 0; i < kUiChromeosResourcesSize; ++i) { |
42 id_map_[kUiChromeosResources[i].name] = kUiChromeosResources[i].value; | 47 storage.emplace_back(kUiChromeosResources[i].name, |
| 48 kUiChromeosResources[i].value); |
| 49 } |
43 #endif | 50 #endif |
| 51 |
| 52 id_map_ = StringIntMap(std::move(storage), base::KEEP_FIRST_OF_DUPES); |
44 } | 53 } |
45 | 54 |
46 int GetId(const std::string& resource_name) { | 55 int GetId(const std::string& resource_name) { |
47 StringIntMap::const_iterator it = id_map_.find(resource_name); | 56 auto it = id_map_.find(resource_name); |
48 if (it == id_map_.end()) | 57 if (it == id_map_.end()) |
49 return -1; | 58 return -1; |
50 return it->second; | 59 return it->second; |
51 } | 60 } |
52 | 61 |
53 private: | 62 private: |
54 StringIntMap id_map_; | 63 StringIntMap id_map_; |
55 }; | 64 }; |
56 | 65 |
57 static base::LazyInstance<ThemeMap>::DestructorAtExit g_theme_ids = | 66 static base::LazyInstance<ThemeMap>::DestructorAtExit g_theme_ids = |
58 LAZY_INSTANCE_INITIALIZER; | 67 LAZY_INSTANCE_INITIALIZER; |
59 | 68 |
60 } // namespace | 69 } // namespace |
61 | 70 |
62 int ResourcesUtil::GetThemeResourceId(const std::string& resource_name) { | 71 int ResourcesUtil::GetThemeResourceId(const std::string& resource_name) { |
63 return g_theme_ids.Get().GetId(resource_name); | 72 return g_theme_ids.Get().GetId(resource_name); |
64 } | 73 } |
OLD | NEW |