[email protected] | 2a28133 | 2012-07-11 22:20:23 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 80cc3f7 | 2009-04-24 18:06:05 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. | ||||
4 | |||||
[email protected] | 1faee3f0 | 2010-06-21 07:01:34 | [diff] [blame] | 5 | #include "chrome/browser/resources_util.h" |
[email protected] | 80cc3f7 | 2009-04-24 18:06:05 | [diff] [blame] | 6 | |
[email protected] | c521289 | 2010-09-08 06:30:33 | [diff] [blame] | 7 | #include <utility> |
8 | |||||
[email protected] | 14c1c23 | 2013-06-11 17:52:44 | [diff] [blame^] | 9 | #include "base/containers/hash_tables.h" |
[email protected] | 80cc3f7 | 2009-04-24 18:06:05 | [diff] [blame] | 10 | #include "base/lazy_instance.h" |
[email protected] | 80cc3f7 | 2009-04-24 18:06:05 | [diff] [blame] | 11 | #include "grit/theme_resources_map.h" |
[email protected] | 1a4cb9d | 2013-01-09 05:35:22 | [diff] [blame] | 12 | #include "grit/ui_resources_map.h" |
[email protected] | 80cc3f7 | 2009-04-24 18:06:05 | [diff] [blame] | 13 | |
[email protected] | 80cc3f7 | 2009-04-24 18:06:05 | [diff] [blame] | 14 | namespace { |
15 | |||||
16 | // A wrapper class that holds a hash_map between resource strings and resource | ||||
17 | // ids. This is done so we can use base::LazyInstance which takes care of | ||||
18 | // thread safety in initializing the hash_map for us. | ||||
19 | class ThemeMap { | ||||
20 | public: | ||||
21 | typedef base::hash_map<std::string, int> StringIntMap; | ||||
22 | |||||
23 | ThemeMap() { | ||||
[email protected] | 2a28133 | 2012-07-11 22:20:23 | [diff] [blame] | 24 | for (size_t i = 0; i < kThemeResourcesSize; ++i) |
[email protected] | 80cc3f7 | 2009-04-24 18:06:05 | [diff] [blame] | 25 | id_map_[kThemeResources[i].name] = kThemeResources[i].value; |
[email protected] | 1a4cb9d | 2013-01-09 05:35:22 | [diff] [blame] | 26 | for (size_t i = 0; i < kUiResourcesSize; ++i) |
27 | id_map_[kUiResources[i].name] = kUiResources[i].value; | ||||
[email protected] | 80cc3f7 | 2009-04-24 18:06:05 | [diff] [blame] | 28 | } |
29 | |||||
30 | int GetId(const std::string& resource_name) { | ||||
31 | StringIntMap::const_iterator it = id_map_.find(resource_name); | ||||
32 | if (it == id_map_.end()) | ||||
33 | return -1; | ||||
34 | return it->second; | ||||
35 | } | ||||
36 | |||||
37 | private: | ||||
38 | StringIntMap id_map_; | ||||
39 | }; | ||||
40 | |||||
[email protected] | 6de0fd1d | 2011-11-15 13:31:49 | [diff] [blame] | 41 | static base::LazyInstance<ThemeMap> g_theme_ids = LAZY_INSTANCE_INITIALIZER; |
[email protected] | 80cc3f7 | 2009-04-24 18:06:05 | [diff] [blame] | 42 | |
43 | } // namespace | ||||
44 | |||||
[email protected] | 1faee3f0 | 2010-06-21 07:01:34 | [diff] [blame] | 45 | int ResourcesUtil::GetThemeResourceId(const std::string& resource_name) { |
[email protected] | 80cc3f7 | 2009-04-24 18:06:05 | [diff] [blame] | 46 | return g_theme_ids.Get().GetId(resource_name); |
47 | } |