Home | History | Annotate | Download | only in browser
      1 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
      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 <utility>
      8 
      9 #include "base/hash_tables.h"
     10 #include "base/lazy_instance.h"
     11 #include "grit/theme_resources_map.h"
     12 
     13 namespace {
     14 
     15 // A wrapper class that holds a hash_map between resource strings and resource
     16 // ids.  This is done so we can use base::LazyInstance which takes care of
     17 // thread safety in initializing the hash_map for us.
     18 class ThemeMap {
     19  public:
     20   typedef base::hash_map<std::string, int> StringIntMap;
     21 
     22   ThemeMap() {
     23     for (size_t i = 0; i < kThemeResourcesSize; ++i) {
     24       id_map_[kThemeResources[i].name] = kThemeResources[i].value;
     25     }
     26   }
     27 
     28   int GetId(const std::string& resource_name) {
     29     StringIntMap::const_iterator it = id_map_.find(resource_name);
     30     if (it == id_map_.end())
     31       return -1;
     32     return it->second;
     33   }
     34 
     35  private:
     36   StringIntMap id_map_;
     37 };
     38 
     39 static base::LazyInstance<ThemeMap> g_theme_ids(base::LINKER_INITIALIZED);
     40 
     41 }  // namespace
     42 
     43 int ResourcesUtil::GetThemeResourceId(const std::string& resource_name) {
     44   return g_theme_ids.Get().GetId(resource_name);
     45 }
     46