blob: a57015672e9fbedfb1a25e880a4adb21ad114011 [file] [log] [blame]
[email protected]2a281332012-07-11 22:20:231// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[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>
10
brettw84cff3f2017-06-29 18:26:5011#include "base/containers/flat_map.h"
Lei Zhang2cfceac2018-11-14 19:36:3312#include "base/no_destructor.h"
avib896c712015-12-26 02:10:4313#include "build/build_config.h"
thestig4a9b0ef2016-08-29 08:22:1214#include "chrome/grit/theme_resources_map.h"
15#include "components/grit/components_scaled_resources_map.h"
16#include "ui/resources/grit/ui_resources_map.h"
[email protected]80cc3f72009-04-24 18:06:0517
[email protected]cdd7bb82014-07-25 09:19:2618#if defined(OS_CHROMEOS)
thestig4a9b0ef2016-08-29 08:22:1219#include "ui/chromeos/resources/grit/ui_chromeos_resources_map.h"
[email protected]cdd7bb82014-07-25 09:19:2620#endif
21
[email protected]80cc3f72009-04-24 18:06:0522namespace {
23
brettw84cff3f2017-06-29 18:26:5024// A wrapper class that holds a map between resource strings and resource
Lei Zhang2cfceac2018-11-14 19:36:3325// ids. This is done so we can use base::NoDestructor which takes care of
brettw84cff3f2017-06-29 18:26:5026// thread safety in initializing the map for us.
[email protected]80cc3f72009-04-24 18:06:0527class ThemeMap {
28 public:
Lei Zhang2cfceac2018-11-14 19:36:3329 using StringIntMap = base::flat_map<std::string, int>;
[email protected]80cc3f72009-04-24 18:06:0530
31 ThemeMap() {
Lei Zhang2cfceac2018-11-14 19:36:3332 size_t storage_size =
33 kComponentsScaledResourcesSize + kThemeResourcesSize + kUiResourcesSize;
34#if defined(OS_CHROMEOS)
35 storage_size += kUiChromeosResourcesSize;
36#endif
37
brettw84cff3f2017-06-29 18:26:5038 // Construct in one-shot from a moved vector.
39 std::vector<StringIntMap::value_type> storage;
Lei Zhang2cfceac2018-11-14 19:36:3340 storage.reserve(storage_size);
brettw84cff3f2017-06-29 18:26:5041
droger2a6ab542015-10-09 16:10:2842 for (size_t i = 0; i < kComponentsScaledResourcesSize; ++i) {
brettw84cff3f2017-06-29 18:26:5043 storage.emplace_back(kComponentsScaledResources[i].name,
44 kComponentsScaledResources[i].value);
droger2a6ab542015-10-09 16:10:2845 }
brettw84cff3f2017-06-29 18:26:5046 for (size_t i = 0; i < kThemeResourcesSize; ++i) {
47 storage.emplace_back(kThemeResources[i].name, kThemeResources[i].value);
48 }
49 for (size_t i = 0; i < kUiResourcesSize; ++i) {
50 storage.emplace_back(kUiResources[i].name, kUiResources[i].value);
51 }
[email protected]cdd7bb82014-07-25 09:19:2652#if defined(OS_CHROMEOS)
brettw84cff3f2017-06-29 18:26:5053 for (size_t i = 0; i < kUiChromeosResourcesSize; ++i) {
54 storage.emplace_back(kUiChromeosResources[i].name,
55 kUiChromeosResources[i].value);
56 }
[email protected]cdd7bb82014-07-25 09:19:2657#endif
brettw84cff3f2017-06-29 18:26:5058
Jan Wilken Dörrie5e5c02f2019-09-23 17:30:0359 id_map_ = StringIntMap(std::move(storage));
[email protected]80cc3f72009-04-24 18:06:0560 }
61
Lei Zhang2cfceac2018-11-14 19:36:3362 int GetId(const std::string& resource_name) const {
brettw84cff3f2017-06-29 18:26:5063 auto it = id_map_.find(resource_name);
Lei Zhang2cfceac2018-11-14 19:36:3364 return it != id_map_.end() ? it->second : -1;
[email protected]80cc3f72009-04-24 18:06:0565 }
66
67 private:
68 StringIntMap id_map_;
69};
70
Lei Zhang2cfceac2018-11-14 19:36:3371ThemeMap& GetThemeIdsMap() {
72 static base::NoDestructor<ThemeMap> s;
73 return *s;
74}
[email protected]80cc3f72009-04-24 18:06:0575
76} // namespace
77
[email protected]1faee3f02010-06-21 07:01:3478int ResourcesUtil::GetThemeResourceId(const std::string& resource_name) {
Lei Zhang2cfceac2018-11-14 19:36:3379 return GetThemeIdsMap().GetId(resource_name);
[email protected]80cc3f72009-04-24 18:06:0580}