diff options
-rw-r--r-- | chrome/app/chrome_resources.vcproj | 4 | ||||
-rw-r--r-- | chrome/app/theme/theme_resources.grd | 2 | ||||
-rw-r--r-- | chrome/browser/browser.vcproj | 12 | ||||
-rw-r--r-- | chrome/browser/theme_resources_util.cc | 46 | ||||
-rw-r--r-- | chrome/browser/theme_resources_util.h | 22 | ||||
-rw-r--r-- | chrome/browser/theme_resources_util_unittest.cc | 33 | ||||
-rw-r--r-- | chrome/chrome.gyp | 6 | ||||
-rw-r--r-- | chrome/test/unit/unittests.vcproj | 4 | ||||
-rw-r--r-- | tools/grit/grit/format/resource_map.py | 116 | ||||
-rw-r--r-- | tools/grit/grit/node/include.py | 3 | ||||
-rw-r--r-- | tools/grit/grit/node/misc.py | 6 | ||||
-rw-r--r-- | tools/grit/grit/tool/build.py | 3 |
12 files changed, 256 insertions, 1 deletions
diff --git a/chrome/app/chrome_resources.vcproj b/chrome/app/chrome_resources.vcproj index 96efc65..1bf8a6f 100644 --- a/chrome/app/chrome_resources.vcproj +++ b/chrome/app/chrome_resources.vcproj @@ -86,6 +86,10 @@ > </File> <File + RelativePath="$(OutDir)\grit_derived_sources\grit\theme_resources_map.h" + > + </File> + <File RelativePath="$(OutDir)\grit_derived_sources\grit\theme_resources.h" > </File> diff --git a/chrome/app/theme/theme_resources.grd b/chrome/app/theme/theme_resources.grd index 4967888..deff5fc 100644 --- a/chrome/app/theme/theme_resources.grd +++ b/chrome/app/theme/theme_resources.grd @@ -6,6 +6,8 @@ </output> <output filename="theme_resources.rc" type="rc_all" /> <output filename="theme_resources.pak" type="data_package" /> + <output filename="grit/theme_resources_map.h" type="resource_map_header" /> + <output filename="grit/theme_resources_map.cc" type="resource_map_source" /> </outputs> <release seq="1"> <includes> 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")); +} diff --git a/chrome/chrome.gyp b/chrome/chrome.gyp index cd3bc0d..eebd53c 100644 --- a/chrome/chrome.gyp +++ b/chrome/chrome.gyp @@ -1122,6 +1122,8 @@ 'browser/task_manager.h', 'browser/task_manager_resource_providers.cc', 'browser/task_manager_resource_providers.h', + 'browser/theme_resources_util.cc', + 'browser/theme_resources_util.h', 'browser/toolbar_model.cc', 'browser/toolbar_model.h', 'browser/user_data_manager.cc', @@ -1306,6 +1308,9 @@ 'browser/worker_host/worker_process_host.h', 'browser/worker_host/worker_service.cc', 'browser/worker_host/worker_service.h', + + # This file is generated by GRIT. + '<(SHARED_INTERMEDIATE_DIR)/chrome/grit/theme_resources_map.cc', ], 'conditions': [ ['javascript_engine=="v8"', { @@ -2250,6 +2255,7 @@ 'browser/tab_contents/render_view_host_manager_unittest.cc', 'browser/tab_contents/web_contents_unittest.cc', 'browser/tabs/tab_strip_model_unittest.cc', + 'browser/theme_resources_util_unittest.cc', 'browser/views/bookmark_editor_view_unittest.cc', 'browser/views/find_bar_win_unittest.cc', 'browser/views/keyword_editor_view_unittest.cc', diff --git a/chrome/test/unit/unittests.vcproj b/chrome/test/unit/unittests.vcproj index 8900642..85ca286 100644 --- a/chrome/test/unit/unittests.vcproj +++ b/chrome/test/unit/unittests.vcproj @@ -760,6 +760,10 @@ > </File> <File + RelativePath="..\..\browser\theme_resources_util_unittest.cc" + > + </File> + <File RelativePath="..\..\browser\history\thumbnail_database_unittest.cc" > </File> diff --git a/tools/grit/grit/format/resource_map.py b/tools/grit/grit/format/resource_map.py new file mode 100644 index 0000000..79ce50c --- /dev/null +++ b/tools/grit/grit/format/resource_map.py @@ -0,0 +1,116 @@ +#!/usr/bin/python2.4 +# 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. + +'''This file contains item formatters for resource_map_header and +resource_map_source files. A resource map is a mapping between resource names +(string) and the internal resource ID.''' + +import os + +from grit import util +from grit.format import interface + +def GetMapName(root): + '''Get the name of the resource map based on the header file name. E.g., + if our header filename is theme_resources.h, we name our resource map + kThemeResourcesMap. + + |root| is the grd file root.''' + outputs = root.GetOutputFiles() + rc_header_file = None + for output in outputs: + if 'rc_header' == output.GetType(): + rc_header_file = output.GetFilename() + if not rc_header_file: + raise Exception('unable to find resource header filename') + filename = os.path.splitext(os.path.split(rc_header_file)[1])[0] + filename = filename[0].upper() + filename[1:] + while filename.find('_') != -1: + pos = filename.find('_') + if pos >= len(filename): + break + filename = filename[:pos] + filename[pos + 1].upper() + filename[pos + 2:] + return 'k' + filename + + +class HeaderTopLevel(interface.ItemFormatter): + '''Create the header file for the resource mapping. This file just declares + an array of name/value pairs.''' + def Format(self, item, lang='en', begin_item=True, output_dir='.'): + if not begin_item: + return '' + return '''\ +// Copyright (c) %(year)d 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. +// This file is automatically generated by GRIT. Do not edit. + +#include <stddef.h> + +struct GritResourceMap { + const char* const name; + int value; +}; +extern const GritResourceMap %(map_name)s[]; +extern const size_t %(map_name)sSize; +''' % { 'year': util.GetCurrentYear(), + 'map_name': GetMapName(item.GetRoot()), + } + + +class SourceTopLevel(interface.ItemFormatter): + '''Create the C++ source file for the resource mapping. This class handles + the header/footer of the file.''' + def Format(self, item, lang='en', begin_item=True, output_dir='.'): + if begin_item: + grit_root = item.GetRoot() + outputs = grit_root.GetOutputFiles() + rc_header_file = None + map_header_file = None + for output in outputs: + if 'rc_header' == output.GetType(): + rc_header_file = output.GetFilename() + elif 'resource_map_header' == output.GetType(): + map_header_file = output.GetFilename() + if not rc_header_file or not map_header_file: + raise Exception('resource_map_source output type requires ' + 'resource_map_header and rc_header outputs') + + return '''\ +// Copyright (c) %(year)d 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. +// This file is automatically generated by GRIT. Do not edit. + +#include "%(map_header_file)s" + +#include "base/basictypes.h" +#include "%(rc_header_file)s" + +const GritResourceMap %(map_name)s[] = { +''' % { 'year': util.GetCurrentYear(), + 'map_header_file': map_header_file, + 'rc_header_file': rc_header_file, + 'map_name': GetMapName(item.GetRoot()), + } + else: + # Return the footer text. + return '''\ +}; + +const size_t %(map_name)sSize = arraysize(%(map_name)s); +''' % { 'map_name': GetMapName(item.GetRoot()) } + + +class SourceInclude(interface.ItemFormatter): + '''Populate the resource mapping. For each include, we map a string to + the ID.''' + def Format(self, item, lang='en', begin_item=True, output_dir='.'): + if not begin_item: + return '' + short_name = item.attrs['name'].lower() + if short_name.startswith('idr_'): + short_name = short_name[4:] + return ' {"%s", %s},\n' % (short_name, item.attrs['name']) diff --git a/tools/grit/grit/node/include.py b/tools/grit/grit/node/include.py index 2175240..b304549 100644 --- a/tools/grit/grit/node/include.py +++ b/tools/grit/grit/node/include.py @@ -40,6 +40,9 @@ class IncludeNode(base.Node): self.attrs['filenameonly'] == 'true', self.attrs['relativepath'] == 'true', self.attrs['flattenhtml'] == 'true') + elif t == 'resource_map_source': + from grit.format import resource_map + return resource_map.SourceInclude() else: return super(type(self), self).ItemFormatter(t) diff --git a/tools/grit/grit/node/misc.py b/tools/grit/grit/node/misc.py index d3441f2..1495615 100644 --- a/tools/grit/grit/node/misc.py +++ b/tools/grit/grit/node/misc.py @@ -218,6 +218,12 @@ class GritNode(base.Node): elif t in ['rc_all', 'rc_translateable', 'rc_nontranslateable']: from grit.format import rc # avoid circular dep return rc.TopLevel() + elif t == 'resource_map_header': + from grit.format import resource_map + return resource_map.HeaderTopLevel() + elif t == 'resource_map_source': + from grit.format import resource_map + return resource_map.SourceTopLevel() else: return super(type(self), self).ItemFormatter(t) diff --git a/tools/grit/grit/tool/build.py b/tools/grit/grit/tool/build.py index 7e274ee..c2111ab 100644 --- a/tools/grit/grit/tool/build.py +++ b/tools/grit/grit/tool/build.py @@ -162,7 +162,8 @@ are exported to translation interchange files (e.g. XMB files), etc. # Microsoft's RC compiler can only deal with single-byte or double-byte # files (no UTF-8), so we make all RC files UTF-16 to support all # character sets. - if output.GetType() in ['rc_header']: + if output.GetType() in ('rc_header', 'resource_map_header', + 'resource_map_source'): encoding = 'cp1252' else: encoding = 'utf_16' |