| Avi Drissman | 4a8573c | 2022-09-09 19:35:54 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
| [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 | |
| avi | b896c71 | 2015-12-26 02:10:43 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | |||||
| [email protected] | c521289 | 2010-09-08 06:30:33 | [diff] [blame] | 9 | #include <utility> |
| Lei Zhang | babf3704 | 2025-05-22 17:10:47 | [diff] [blame] | 10 | #include <vector> |
| [email protected] | c521289 | 2010-09-08 06:30:33 | [diff] [blame] | 11 | |
| brettw | 84cff3f | 2017-06-29 18:26:50 | [diff] [blame] | 12 | #include "base/containers/flat_map.h" |
| Lei Zhang | 2cfceac | 2018-11-14 19:36:33 | [diff] [blame] | 13 | #include "base/no_destructor.h" |
| Nicolas Dossou-Gbete | deda98f4 | 2025-05-28 20:12:48 | [diff] [blame] | 14 | #include "build/branding_buildflags.h" |
| avi | b896c71 | 2015-12-26 02:10:43 | [diff] [blame] | 15 | #include "build/build_config.h" |
| thestig | 4a9b0ef | 2016-08-29 08:22:12 | [diff] [blame] | 16 | #include "chrome/grit/theme_resources_map.h" |
| 17 | #include "components/grit/components_scaled_resources_map.h" | ||||
| 18 | #include "ui/resources/grit/ui_resources_map.h" | ||||
| [email protected] | 80cc3f7 | 2009-04-24 18:06:05 | [diff] [blame] | 19 | |
| Nicolas Dossou-Gbete | deda98f4 | 2025-05-28 20:12:48 | [diff] [blame] | 20 | #if BUILDFLAG(ENABLE_BUILTIN_SEARCH_PROVIDER_ASSETS) |
| Tomasz Wiszkowski | 8e66beb | 2025-05-06 18:48:46 | [diff] [blame] | 21 | #include "third_party/search_engines_data/search_engines_scaled_resources_map.h" |
| 22 | #endif | ||||
| Yuta Hijikata | 761ca45 | 2025-03-06 08:53:17 | [diff] [blame] | 23 | #if BUILDFLAG(IS_CHROMEOS) |
| thestig | 4a9b0ef | 2016-08-29 08:22:12 | [diff] [blame] | 24 | #include "ui/chromeos/resources/grit/ui_chromeos_resources_map.h" |
| [email protected] | cdd7bb8 | 2014-07-25 09:19:26 | [diff] [blame] | 25 | #endif |
| 26 | |||||
| [email protected] | 80cc3f7 | 2009-04-24 18:06:05 | [diff] [blame] | 27 | namespace { |
| 28 | |||||
| brettw | 84cff3f | 2017-06-29 18:26:50 | [diff] [blame] | 29 | // A wrapper class that holds a map between resource strings and resource |
| Lei Zhang | 2cfceac | 2018-11-14 19:36:33 | [diff] [blame] | 30 | // ids. This is done so we can use base::NoDestructor which takes care of |
| brettw | 84cff3f | 2017-06-29 18:26:50 | [diff] [blame] | 31 | // thread safety in initializing the map for us. |
| [email protected] | 80cc3f7 | 2009-04-24 18:06:05 | [diff] [blame] | 32 | class ThemeMap { |
| 33 | public: | ||||
| Lei Zhang | 2cfceac | 2018-11-14 19:36:33 | [diff] [blame] | 34 | using StringIntMap = base::flat_map<std::string, int>; |
| [email protected] | 80cc3f7 | 2009-04-24 18:06:05 | [diff] [blame] | 35 | |
| 36 | ThemeMap() { | ||||
| Lei Zhang | babf3704 | 2025-05-22 17:10:47 | [diff] [blame] | 37 | size_t storage_size = std::size(kComponentsScaledResources) + |
| 38 | std::size(kThemeResources) + std::size(kUiResources); | ||||
| Nicolas Dossou-Gbete | deda98f4 | 2025-05-28 20:12:48 | [diff] [blame] | 39 | #if BUILDFLAG(ENABLE_BUILTIN_SEARCH_PROVIDER_ASSETS) |
| Lei Zhang | babf3704 | 2025-05-22 17:10:47 | [diff] [blame] | 40 | storage_size += std::size(kSearchEnginesScaledResources); |
| Tomasz Wiszkowski | 8e66beb | 2025-05-06 18:48:46 | [diff] [blame] | 41 | #endif |
| Yuta Hijikata | 761ca45 | 2025-03-06 08:53:17 | [diff] [blame] | 42 | #if BUILDFLAG(IS_CHROMEOS) |
| Lei Zhang | babf3704 | 2025-05-22 17:10:47 | [diff] [blame] | 43 | storage_size += std::size(kUiChromeosResources); |
| Lei Zhang | 2cfceac | 2018-11-14 19:36:33 | [diff] [blame] | 44 | #endif |
| 45 | |||||
| brettw | 84cff3f | 2017-06-29 18:26:50 | [diff] [blame] | 46 | // Construct in one-shot from a moved vector. |
| 47 | std::vector<StringIntMap::value_type> storage; | ||||
| Lei Zhang | 2cfceac | 2018-11-14 19:36:33 | [diff] [blame] | 48 | storage.reserve(storage_size); |
| brettw | 84cff3f | 2017-06-29 18:26:50 | [diff] [blame] | 49 | |
| Lei Zhang | babf3704 | 2025-05-22 17:10:47 | [diff] [blame] | 50 | for (const auto& resource : kComponentsScaledResources) { |
| 51 | storage.emplace_back(resource.path, resource.id); | ||||
| droger | 2a6ab54 | 2015-10-09 16:10:28 | [diff] [blame] | 52 | } |
| Lei Zhang | babf3704 | 2025-05-22 17:10:47 | [diff] [blame] | 53 | for (const auto& resource : kThemeResources) { |
| 54 | storage.emplace_back(resource.path, resource.id); | ||||
| brettw | 84cff3f | 2017-06-29 18:26:50 | [diff] [blame] | 55 | } |
| Lei Zhang | babf3704 | 2025-05-22 17:10:47 | [diff] [blame] | 56 | for (const auto& resource : kUiResources) { |
| 57 | storage.emplace_back(resource.path, resource.id); | ||||
| brettw | 84cff3f | 2017-06-29 18:26:50 | [diff] [blame] | 58 | } |
| Nicolas Dossou-Gbete | deda98f4 | 2025-05-28 20:12:48 | [diff] [blame] | 59 | #if BUILDFLAG(ENABLE_BUILTIN_SEARCH_PROVIDER_ASSETS) |
| Lei Zhang | babf3704 | 2025-05-22 17:10:47 | [diff] [blame] | 60 | for (const auto& resource : kSearchEnginesScaledResources) { |
| 61 | storage.emplace_back(resource.path, resource.id); | ||||
| Tomasz Wiszkowski | 8e66beb | 2025-05-06 18:48:46 | [diff] [blame] | 62 | } |
| 63 | #endif | ||||
| Yuta Hijikata | 761ca45 | 2025-03-06 08:53:17 | [diff] [blame] | 64 | #if BUILDFLAG(IS_CHROMEOS) |
| Lei Zhang | babf3704 | 2025-05-22 17:10:47 | [diff] [blame] | 65 | for (const auto& resource : kUiChromeosResources) { |
| 66 | storage.emplace_back(resource.path, resource.id); | ||||
| brettw | 84cff3f | 2017-06-29 18:26:50 | [diff] [blame] | 67 | } |
| [email protected] | cdd7bb8 | 2014-07-25 09:19:26 | [diff] [blame] | 68 | #endif |
| brettw | 84cff3f | 2017-06-29 18:26:50 | [diff] [blame] | 69 | |
| Jan Wilken Dörrie | 5e5c02f | 2019-09-23 17:30:03 | [diff] [blame] | 70 | id_map_ = StringIntMap(std::move(storage)); |
| [email protected] | 80cc3f7 | 2009-04-24 18:06:05 | [diff] [blame] | 71 | } |
| 72 | |||||
| Lei Zhang | 2cfceac | 2018-11-14 19:36:33 | [diff] [blame] | 73 | int GetId(const std::string& resource_name) const { |
| brettw | 84cff3f | 2017-06-29 18:26:50 | [diff] [blame] | 74 | auto it = id_map_.find(resource_name); |
| Lei Zhang | 2cfceac | 2018-11-14 19:36:33 | [diff] [blame] | 75 | return it != id_map_.end() ? it->second : -1; |
| [email protected] | 80cc3f7 | 2009-04-24 18:06:05 | [diff] [blame] | 76 | } |
| 77 | |||||
| 78 | private: | ||||
| 79 | StringIntMap id_map_; | ||||
| 80 | }; | ||||
| 81 | |||||
| Lei Zhang | 2cfceac | 2018-11-14 19:36:33 | [diff] [blame] | 82 | ThemeMap& GetThemeIdsMap() { |
| 83 | static base::NoDestructor<ThemeMap> s; | ||||
| 84 | return *s; | ||||
| 85 | } | ||||
| [email protected] | 80cc3f7 | 2009-04-24 18:06:05 | [diff] [blame] | 86 | |
| 87 | } // namespace | ||||
| 88 | |||||
| [email protected] | 1faee3f0 | 2010-06-21 07:01:34 | [diff] [blame] | 89 | int ResourcesUtil::GetThemeResourceId(const std::string& resource_name) { |
| Lei Zhang | 2cfceac | 2018-11-14 19:36:33 | [diff] [blame] | 90 | return GetThemeIdsMap().GetId(resource_name); |
| [email protected] | 80cc3f7 | 2009-04-24 18:06:05 | [diff] [blame] | 91 | } |