[M120 Migration]Fix for crash during chrome exit
[platform/framework/web/chromium-efl.git] / chrome / browser / resources_util.cc
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.
4
5 #include "chrome/browser/resources_util.h"
6
7 #include <stddef.h>
8
9 #include <utility>
10
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"
18
19 #if BUILDFLAG(IS_CHROMEOS_ASH)
20 #include "ui/chromeos/resources/grit/ui_chromeos_resources_map.h"
21 #endif
22
23 namespace {
24
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.
28 class ThemeMap {
29  public:
30   using StringIntMap = base::flat_map<std::string, int>;
31
32   ThemeMap() {
33     size_t storage_size =
34         kComponentsScaledResourcesSize + kThemeResourcesSize + kUiResourcesSize;
35 #if BUILDFLAG(IS_CHROMEOS_ASH)
36     storage_size += kUiChromeosResourcesSize;
37 #endif
38
39     // Construct in one-shot from a moved vector.
40     std::vector<StringIntMap::value_type> storage;
41     storage.reserve(storage_size);
42
43     for (size_t i = 0; i < kComponentsScaledResourcesSize; ++i) {
44       storage.emplace_back(kComponentsScaledResources[i].path,
45                            kComponentsScaledResources[i].id);
46     }
47     for (size_t i = 0; i < kThemeResourcesSize; ++i) {
48       storage.emplace_back(kThemeResources[i].path, kThemeResources[i].id);
49     }
50     for (size_t i = 0; i < kUiResourcesSize; ++i) {
51       storage.emplace_back(kUiResources[i].path, kUiResources[i].id);
52     }
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);
57     }
58 #endif
59
60     id_map_ = StringIntMap(std::move(storage));
61   }
62
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;
66   }
67
68  private:
69   StringIntMap id_map_;
70 };
71
72 ThemeMap& GetThemeIdsMap() {
73   static base::NoDestructor<ThemeMap> s;
74   return *s;
75 }
76
77 }  // namespace
78
79 int ResourcesUtil::GetThemeResourceId(const std::string& resource_name) {
80   return GetThemeIdsMap().GetId(resource_name);
81 }