blob: 277df66772826d41bfc96e4963500f898441608d [file] [log] [blame]
Avi Drissman4a8573c2022-09-09 19:35:541// Copyright 2012 The Chromium Authors
[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>
Lei Zhangbabf37042025-05-22 17:10:4710#include <vector>
[email protected]c5212892010-09-08 06:30:3311
brettw84cff3f2017-06-29 18:26:5012#include "base/containers/flat_map.h"
Lei Zhang2cfceac2018-11-14 19:36:3313#include "base/no_destructor.h"
Nicolas Dossou-Gbetededa98f42025-05-28 20:12:4814#include "build/branding_buildflags.h"
avib896c712015-12-26 02:10:4315#include "build/build_config.h"
thestig4a9b0ef2016-08-29 08:22:1216#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]80cc3f72009-04-24 18:06:0519
Nicolas Dossou-Gbetededa98f42025-05-28 20:12:4820#if BUILDFLAG(ENABLE_BUILTIN_SEARCH_PROVIDER_ASSETS)
Tomasz Wiszkowski8e66beb2025-05-06 18:48:4621#include "third_party/search_engines_data/search_engines_scaled_resources_map.h"
22#endif
Yuta Hijikata761ca452025-03-06 08:53:1723#if BUILDFLAG(IS_CHROMEOS)
thestig4a9b0ef2016-08-29 08:22:1224#include "ui/chromeos/resources/grit/ui_chromeos_resources_map.h"
[email protected]cdd7bb82014-07-25 09:19:2625#endif
26
[email protected]80cc3f72009-04-24 18:06:0527namespace {
28
brettw84cff3f2017-06-29 18:26:5029// A wrapper class that holds a map between resource strings and resource
Lei Zhang2cfceac2018-11-14 19:36:3330// ids. This is done so we can use base::NoDestructor which takes care of
brettw84cff3f2017-06-29 18:26:5031// thread safety in initializing the map for us.
[email protected]80cc3f72009-04-24 18:06:0532class ThemeMap {
33 public:
Lei Zhang2cfceac2018-11-14 19:36:3334 using StringIntMap = base::flat_map<std::string, int>;
[email protected]80cc3f72009-04-24 18:06:0535
36 ThemeMap() {
Lei Zhangbabf37042025-05-22 17:10:4737 size_t storage_size = std::size(kComponentsScaledResources) +
38 std::size(kThemeResources) + std::size(kUiResources);
Nicolas Dossou-Gbetededa98f42025-05-28 20:12:4839#if BUILDFLAG(ENABLE_BUILTIN_SEARCH_PROVIDER_ASSETS)
Lei Zhangbabf37042025-05-22 17:10:4740 storage_size += std::size(kSearchEnginesScaledResources);
Tomasz Wiszkowski8e66beb2025-05-06 18:48:4641#endif
Yuta Hijikata761ca452025-03-06 08:53:1742#if BUILDFLAG(IS_CHROMEOS)
Lei Zhangbabf37042025-05-22 17:10:4743 storage_size += std::size(kUiChromeosResources);
Lei Zhang2cfceac2018-11-14 19:36:3344#endif
45
brettw84cff3f2017-06-29 18:26:5046 // Construct in one-shot from a moved vector.
47 std::vector<StringIntMap::value_type> storage;
Lei Zhang2cfceac2018-11-14 19:36:3348 storage.reserve(storage_size);
brettw84cff3f2017-06-29 18:26:5049
Lei Zhangbabf37042025-05-22 17:10:4750 for (const auto& resource : kComponentsScaledResources) {
51 storage.emplace_back(resource.path, resource.id);
droger2a6ab542015-10-09 16:10:2852 }
Lei Zhangbabf37042025-05-22 17:10:4753 for (const auto& resource : kThemeResources) {
54 storage.emplace_back(resource.path, resource.id);
brettw84cff3f2017-06-29 18:26:5055 }
Lei Zhangbabf37042025-05-22 17:10:4756 for (const auto& resource : kUiResources) {
57 storage.emplace_back(resource.path, resource.id);
brettw84cff3f2017-06-29 18:26:5058 }
Nicolas Dossou-Gbetededa98f42025-05-28 20:12:4859#if BUILDFLAG(ENABLE_BUILTIN_SEARCH_PROVIDER_ASSETS)
Lei Zhangbabf37042025-05-22 17:10:4760 for (const auto& resource : kSearchEnginesScaledResources) {
61 storage.emplace_back(resource.path, resource.id);
Tomasz Wiszkowski8e66beb2025-05-06 18:48:4662 }
63#endif
Yuta Hijikata761ca452025-03-06 08:53:1764#if BUILDFLAG(IS_CHROMEOS)
Lei Zhangbabf37042025-05-22 17:10:4765 for (const auto& resource : kUiChromeosResources) {
66 storage.emplace_back(resource.path, resource.id);
brettw84cff3f2017-06-29 18:26:5067 }
[email protected]cdd7bb82014-07-25 09:19:2668#endif
brettw84cff3f2017-06-29 18:26:5069
Jan Wilken Dörrie5e5c02f2019-09-23 17:30:0370 id_map_ = StringIntMap(std::move(storage));
[email protected]80cc3f72009-04-24 18:06:0571 }
72
Lei Zhang2cfceac2018-11-14 19:36:3373 int GetId(const std::string& resource_name) const {
brettw84cff3f2017-06-29 18:26:5074 auto it = id_map_.find(resource_name);
Lei Zhang2cfceac2018-11-14 19:36:3375 return it != id_map_.end() ? it->second : -1;
[email protected]80cc3f72009-04-24 18:06:0576 }
77
78 private:
79 StringIntMap id_map_;
80};
81
Lei Zhang2cfceac2018-11-14 19:36:3382ThemeMap& GetThemeIdsMap() {
83 static base::NoDestructor<ThemeMap> s;
84 return *s;
85}
[email protected]80cc3f72009-04-24 18:06:0586
87} // namespace
88
[email protected]1faee3f02010-06-21 07:01:3489int ResourcesUtil::GetThemeResourceId(const std::string& resource_name) {
Lei Zhang2cfceac2018-11-14 19:36:3390 return GetThemeIdsMap().GetId(resource_name);
[email protected]80cc3f72009-04-24 18:06:0591}