summaryrefslogtreecommitdiffstats
path: root/chrome/browser
diff options
context:
space:
mode:
authortc@google.com <tc@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2009-04-24 18:06:05 +0000
committertc@google.com <tc@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2009-04-24 18:06:05 +0000
commit80cc3f70e67f5a2efeb0f434c58d9f9b5641f21a (patch)
tree9d7b3bf4cc3caf1690e96164f0f0297ca5c635e6 /chrome/browser
parent4d9cf078fa29f9e4b530c909af386ef201e3a935 (diff)
downloadchromium_src-80cc3f70e67f5a2efeb0f434c58d9f9b5641f21a.zip
chromium_src-80cc3f70e67f5a2efeb0f434c58d9f9b5641f21a.tar.gz
chromium_src-80cc3f70e67f5a2efeb0f434c58d9f9b5641f21a.tar.bz2
First cut at adding a map between strings names and resource ids.
This creates a mapping for all the entries in the theme_resources.grd file and adds a static method for querying the mapping. BUG=10639 Review URL: http://codereview.chromium.org/92085 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@14443 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser')
-rw-r--r--chrome/browser/browser.vcproj12
-rw-r--r--chrome/browser/theme_resources_util.cc46
-rw-r--r--chrome/browser/theme_resources_util.h22
-rw-r--r--chrome/browser/theme_resources_util_unittest.cc33
4 files changed, 113 insertions, 0 deletions
diff --git a/chrome/browser/browser.vcproj b/chrome/browser/browser.vcproj
index 854355d..b41bf15 100644
--- a/chrome/browser/browser.vcproj
+++ b/chrome/browser/browser.vcproj
@@ -2603,6 +2603,18 @@
>
</File>
<File
+ RelativePath="$(OutDir)\grit_derived_sources\grit\theme_resources_map.cc"
+ >
+ </File>
+ <File
+ RelativePath=".\theme_resources_util.cc"
+ >
+ </File>
+ <File
+ RelativePath=".\theme_resources_util.h"
+ >
+ </File>
+ <File
RelativePath=".\toolbar_model.cc"
>
</File>
diff --git a/chrome/browser/theme_resources_util.cc b/chrome/browser/theme_resources_util.cc
new file mode 100644
index 0000000..2358f6f
--- /dev/null
+++ b/chrome/browser/theme_resources_util.cc
@@ -0,0 +1,46 @@
+// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/theme_resources_util.h"
+
+#include "base/hash_tables.h"
+#include "base/lazy_instance.h"
+#include "base/logging.h"
+#include "grit/theme_resources_map.h"
+
+#include <utility>
+
+namespace {
+
+// A wrapper class that holds a hash_map between resource strings and resource
+// ids. This is done so we can use base::LazyInstance which takes care of
+// thread safety in initializing the hash_map for us.
+class ThemeMap {
+ public:
+ typedef base::hash_map<std::string, int> StringIntMap;
+
+ ThemeMap() {
+ for (size_t i = 0; i < kThemeResourcesSize; ++i) {
+ id_map_[kThemeResources[i].name] = kThemeResources[i].value;
+ }
+ }
+
+ int GetId(const std::string& resource_name) {
+ StringIntMap::const_iterator it = id_map_.find(resource_name);
+ if (it == id_map_.end())
+ return -1;
+ return it->second;
+ }
+
+ private:
+ StringIntMap id_map_;
+};
+
+static base::LazyInstance<ThemeMap> g_theme_ids(base::LINKER_INITIALIZED);
+
+} // namespace
+
+int ThemeResourcesUtil::GetId(const std::string& resource_name) {
+ return g_theme_ids.Get().GetId(resource_name);
+}
diff --git a/chrome/browser/theme_resources_util.h b/chrome/browser/theme_resources_util.h
new file mode 100644
index 0000000..6509a50
--- /dev/null
+++ b/chrome/browser/theme_resources_util.h
@@ -0,0 +1,22 @@
+// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CHROME_BROWSER_THEME_RESOURCES_UTIL_H_
+#define CHROME_BROWSER_THEME_RESOURCES_UTIL_H_
+
+#include <string>
+
+#include "base/basictypes.h"
+
+class ThemeResourcesUtil {
+ public:
+ // Returns the theme resource id or -1 if no resource with the name exists.
+ static int GetId(const std::string& resource_name);
+
+ private:
+ ThemeResourcesUtil() {}
+ DISALLOW_COPY_AND_ASSIGN(ThemeResourcesUtil);
+};
+
+#endif // CHROME_BROWSER_THEME_RESOURCES_UTIL_H_
diff --git a/chrome/browser/theme_resources_util_unittest.cc b/chrome/browser/theme_resources_util_unittest.cc
new file mode 100644
index 0000000..4bd6bd0
--- /dev/null
+++ b/chrome/browser/theme_resources_util_unittest.cc
@@ -0,0 +1,33 @@
+// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/theme_resources_util.h"
+
+#include "grit/theme_resources.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace {
+
+struct TestCase {
+ const char* name;
+ int id;
+};
+
+} // namespace
+
+TEST(ThemeResourcesUtil, SpotCheckIds) {
+ const TestCase kTestCases[] = {
+ {"back", IDR_BACK},
+ {"go", IDR_GO},
+ {"star", IDR_STAR},
+ {"sad_tab", IDR_SAD_TAB},
+ };
+ for (size_t i = 0; i < arraysize(kTestCases); ++i) {
+ EXPECT_EQ(kTestCases[i].id, ThemeResourcesUtil::GetId(kTestCases[i].name));
+ }
+
+ // Should return -1 of unknown names.
+ EXPECT_EQ(-1, ThemeResourcesUtil::GetId("foobar"));
+ EXPECT_EQ(-1, ThemeResourcesUtil::GetId("backstar"));
+}