1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/resources_util.h"
11 #include "base/containers/flat_map.h"
12 #include "base/no_destructor.h"
13 #include "build/build_config.h"
14 #include "build/chromeos_buildflags.h"
15 #include "chrome/grit/theme_resources_map.h"
16 #include "components/grit/components_scaled_resources_map.h"
17 #include "ui/resources/grit/ui_resources_map.h"
19 #if BUILDFLAG(IS_CHROMEOS_ASH)
20 #include "ui/chromeos/resources/grit/ui_chromeos_resources_map.h"
25 // A wrapper class that holds a map between resource strings and resource
26 // ids. This is done so we can use base::NoDestructor which takes care of
27 // thread safety in initializing the map for us.
30 using StringIntMap = base::flat_map<std::string, int>;
34 kComponentsScaledResourcesSize + kThemeResourcesSize + kUiResourcesSize;
35 #if BUILDFLAG(IS_CHROMEOS_ASH)
36 storage_size += kUiChromeosResourcesSize;
39 // Construct in one-shot from a moved vector.
40 std::vector<StringIntMap::value_type> storage;
41 storage.reserve(storage_size);
43 for (size_t i = 0; i < kComponentsScaledResourcesSize; ++i) {
44 storage.emplace_back(kComponentsScaledResources[i].path,
45 kComponentsScaledResources[i].id);
47 for (size_t i = 0; i < kThemeResourcesSize; ++i) {
48 storage.emplace_back(kThemeResources[i].path, kThemeResources[i].id);
50 for (size_t i = 0; i < kUiResourcesSize; ++i) {
51 storage.emplace_back(kUiResources[i].path, kUiResources[i].id);
53 #if BUILDFLAG(IS_CHROMEOS_ASH)
54 for (size_t i = 0; i < kUiChromeosResourcesSize; ++i) {
55 storage.emplace_back(kUiChromeosResources[i].path,
56 kUiChromeosResources[i].id);
60 id_map_ = StringIntMap(std::move(storage));
63 int GetId(const std::string& resource_name) const {
64 auto it = id_map_.find(resource_name);
65 return it != id_map_.end() ? it->second : -1;
72 ThemeMap& GetThemeIdsMap() {
73 static base::NoDestructor<ThemeMap> s;
79 int ResourcesUtil::GetThemeResourceId(const std::string& resource_name) {
80 return GetThemeIdsMap().GetId(resource_name);