From ce9bcf3e3b788f8ded1cde7702bdc9cfd1f8859e Mon Sep 17 00:00:00 2001 From: "vivek.vg" Date: Tue, 13 Jan 2015 22:42:36 -0800 Subject: [PATCH] [mojo] Make HTML viewer load the blink resources from the generated pak file. Blink is about to make use of blink_resources.grd for the inline resources of user agent stylesheets. This removes the dependency upon using make-file-arrays.py which embeds these resources as strings. See part 1, 2 and 3. Also the .rodata section of libblink_web (in component build mode) is reduced by ~33kb. In this CL, the html viewer would implement the loadResource() method and use the DataPack to load the required resources. The resources are generated from blink_resources.grd which has been added as the deps. Part 1: https://codereview.chromium.org/436843004/ Part 2: https://codereview.chromium.org/422023008 Part 3: https://codereview.chromium.org/573553002 BUG=312586 Review URL: https://codereview.chromium.org/827223002 Cr-Commit-Position: refs/heads/master@{#311420} --- mojo/services/html_viewer/BUILD.gn | 23 ++++ mojo/services/html_viewer/DEPS | 1 + mojo/services/html_viewer/blink_platform_impl.cc | 15 ++ mojo/services/html_viewer/blink_platform_impl.h | 3 + .../html_viewer/blink_resource_constants.h | 71 ++++++++++ .../html_viewer/generate_blink_resource_map.py | 151 +++++++++++++++++++++ 6 files changed, 264 insertions(+) create mode 100644 mojo/services/html_viewer/blink_resource_constants.h create mode 100644 mojo/services/html_viewer/generate_blink_resource_map.py diff --git a/mojo/services/html_viewer/BUILD.gn b/mojo/services/html_viewer/BUILD.gn index 444ee076f2b8..7537cafc310f 100644 --- a/mojo/services/html_viewer/BUILD.gn +++ b/mojo/services/html_viewer/BUILD.gn @@ -5,8 +5,29 @@ import("//mojo/public/mojo.gni") import("//mojo/public/mojo_application.gni") +action("generate_blink_resource_map") { + script = "//mojo/services/html_viewer/generate_blink_resource_map.py" + args = [ + "--pak-file", + rebase_path("$root_out_dir/gen/blink/public/resources/blink_resources.pak"), + "--header", + rebase_path("$target_gen_dir/blink_resource_map.h"), + "--cpp", + rebase_path("$target_gen_dir/blink_resource_map.cc"), + ] + outputs = [ + "$target_gen_dir/blink_resource_map.cc", + "$target_gen_dir/blink_resource_map.h", + ] + public_deps = [ + "//third_party/WebKit/public:resources", + ] +} + source_set("lib") { sources = [ + "$target_gen_dir/blink_resource_map.cc", + "$target_gen_dir/blink_resource_map.h", "ax_provider_impl.cc", "ax_provider_impl.h", "blink_basic_type_converters.cc", @@ -15,6 +36,7 @@ source_set("lib") { "blink_input_events_type_converters.h", "blink_platform_impl.cc", "blink_platform_impl.h", + "blink_resource_constants.h", "blink_url_request_type_converters.cc", "blink_url_request_type_converters.h", "html_document.cc", @@ -81,6 +103,7 @@ source_set("lib") { public_deps = [ "//mojo/public/cpp/bindings", "//third_party/WebKit/public:blink", + ":generate_blink_resource_map", ] } diff --git a/mojo/services/html_viewer/DEPS b/mojo/services/html_viewer/DEPS index 3d4a855a3818..37deb332b9df 100644 --- a/mojo/services/html_viewer/DEPS +++ b/mojo/services/html_viewer/DEPS @@ -1,4 +1,5 @@ include_rules = [ + "+blink/public/resources", "+cc", "+gin", "+media", diff --git a/mojo/services/html_viewer/blink_platform_impl.cc b/mojo/services/html_viewer/blink_platform_impl.cc index d97a5098e1b8..441ca0c182e6 100644 --- a/mojo/services/html_viewer/blink_platform_impl.cc +++ b/mojo/services/html_viewer/blink_platform_impl.cc @@ -11,6 +11,7 @@ #include "base/synchronization/waitable_event.h" #include "base/threading/platform_thread.h" #include "base/time/time.h" +#include "mojo/services/html_viewer/blink_resource_constants.h" #include "mojo/services/html_viewer/webthread_impl.h" #include "net/base/data_url.h" #include "net/base/mime_util.h" @@ -143,6 +144,20 @@ const unsigned char* BlinkPlatformImpl::getTraceCategoryEnabledFlag( return buf; } +blink::WebData BlinkPlatformImpl::loadResource(const char* resource) { + for (size_t i = 0; i < arraysize(kDataResources); ++i) { + if (!strcmp(resource, kDataResources[i].name)) { + int length; + const char* data = + blink_resource_map_.GetResource(kDataResources[i].id, &length); + CHECK(data != nullptr && length > 0); + return blink::WebData(data, length); + } + } + NOTREACHED() << "Requested resource is unavailable!"; + return blink::WebData(); +} + blink::WebURLLoader* BlinkPlatformImpl::createURLLoader() { return NULL; } diff --git a/mojo/services/html_viewer/blink_platform_impl.h b/mojo/services/html_viewer/blink_platform_impl.h index 10f2c49c6a4c..7ef8236c11b1 100644 --- a/mojo/services/html_viewer/blink_platform_impl.h +++ b/mojo/services/html_viewer/blink_platform_impl.h @@ -10,6 +10,7 @@ #include "base/threading/thread_local_storage.h" #include "base/timer/timer.h" #include "cc/blink/web_compositor_support_impl.h" +#include "mojo/services/html_viewer/blink_resource_map.h" #include "mojo/services/html_viewer/webmimeregistry_impl.h" #include "mojo/services/html_viewer/webthemeengine_impl.h" #include "third_party/WebKit/public/platform/Platform.h" @@ -52,6 +53,7 @@ class BlinkPlatformImpl : public blink::Platform { virtual blink::WebScrollbarBehavior* scrollbarBehavior(); virtual const unsigned char* getTraceCategoryEnabledFlag( const char* category_name); + virtual blink::WebData loadResource(const char* name); private: void SuspendSharedTimer(); @@ -75,6 +77,7 @@ class BlinkPlatformImpl : public blink::Platform { WebThemeEngineImpl theme_engine_; WebMimeRegistryImpl mime_registry_; blink::WebScrollbarBehavior scrollbar_behavior_; + BlinkResourceMap blink_resource_map_; DISALLOW_COPY_AND_ASSIGN(BlinkPlatformImpl); }; diff --git a/mojo/services/html_viewer/blink_resource_constants.h b/mojo/services/html_viewer/blink_resource_constants.h new file mode 100644 index 000000000000..c4ebb9027cc6 --- /dev/null +++ b/mojo/services/html_viewer/blink_resource_constants.h @@ -0,0 +1,71 @@ +// Copyright 2015 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 MOJO_SERVICES_HTML_VIEWER_BLINK_RESOURCE_CONSTANTS_H_ +#define MOJO_SERVICES_HTML_VIEWER_BLINK_RESOURCE_CONSTANTS_H_ + +#include "blink/public/resources/grit/blink_resources.h" + +namespace html_viewer { + +struct DataResource { + const char* name; + int id; +}; + +const DataResource kDataResources[] = { + {"html.css", IDR_UASTYLE_HTML_CSS}, + {"quirks.css", IDR_UASTYLE_QUIRKS_CSS}, + {"view-source.css", IDR_UASTYLE_VIEW_SOURCE_CSS}, + {"themeChromium.css", IDR_UASTYLE_THEME_CHROMIUM_CSS}, +#if defined(OS_ANDROID) + {"themeChromiumAndroid.css", IDR_UASTYLE_THEME_CHROMIUM_ANDROID_CSS}, + {"mediaControlsAndroid.css", IDR_UASTYLE_MEDIA_CONTROLS_ANDROID_CSS}, +#endif +#if defined(OS_LINUX) + {"themeChromiumLinux.css", IDR_UASTYLE_THEME_CHROMIUM_LINUX_CSS}, +#endif + {"themeChromiumSkia.css", IDR_UASTYLE_THEME_CHROMIUM_SKIA_CSS}, + {"themeInputMultipleFields.css", + IDR_UASTYLE_THEME_INPUT_MULTIPLE_FIELDS_CSS}, +#if defined(OS_MACOSX) + {"themeMac.css", IDR_UASTYLE_THEME_MAC_CSS}, +#endif + {"themeWin.css", IDR_UASTYLE_THEME_WIN_CSS}, + {"themeWinQuirks.css", IDR_UASTYLE_THEME_WIN_QUIRKS_CSS}, + {"svg.css", IDR_UASTYLE_SVG_CSS}, + {"navigationTransitions.css", IDR_UASTYLE_NAVIGATION_TRANSITIONS_CSS}, + {"mathml.css", IDR_UASTYLE_MATHML_CSS}, + {"mediaControls.css", IDR_UASTYLE_MEDIA_CONTROLS_CSS}, + {"fullscreen.css", IDR_UASTYLE_FULLSCREEN_CSS}, + {"xhtmlmp.css", IDR_UASTYLE_XHTMLMP_CSS}, + {"viewportAndroid.css", IDR_UASTYLE_VIEWPORT_ANDROID_CSS}, + {"InspectorOverlayPage.html", IDR_INSPECTOR_OVERLAY_PAGE_HTML}, + {"InjectedScriptCanvasModuleSource.js", + IDR_INSPECTOR_INJECTED_SCRIPT_CANVAS_MODULE_SOURCE_JS}, + {"InjectedScriptSource.js", IDR_INSPECTOR_INJECTED_SCRIPT_SOURCE_JS}, + {"DebuggerScriptSource.js", IDR_INSPECTOR_DEBUGGER_SCRIPT_SOURCE_JS}, + {"DocumentExecCommand.js", IDR_PRIVATE_SCRIPT_DOCUMENTEXECCOMMAND_JS}, + {"DocumentXMLTreeViewer.css", IDR_PRIVATE_SCRIPT_DOCUMENTXMLTREEVIEWER_CSS}, + {"DocumentXMLTreeViewer.js", IDR_PRIVATE_SCRIPT_DOCUMENTXMLTREEVIEWER_JS}, + {"HTMLMarqueeElement.js", IDR_PRIVATE_SCRIPT_HTMLMARQUEEELEMENT_JS}, + {"PluginPlaceholderElement.js", + IDR_PRIVATE_SCRIPT_PLUGINPLACEHOLDERELEMENT_JS}, + {"PrivateScriptRunner.js", IDR_PRIVATE_SCRIPT_PRIVATESCRIPTRUNNER_JS}, +#ifdef IDR_PICKER_COMMON_JS + {"pickerCommon.js", IDR_PICKER_COMMON_JS}, + {"pickerCommon.css", IDR_PICKER_COMMON_CSS}, + {"calendarPicker.js", IDR_CALENDAR_PICKER_JS}, + {"calendarPicker.css", IDR_CALENDAR_PICKER_CSS}, + {"pickerButton.css", IDR_PICKER_BUTTON_CSS}, + {"suggestionPicker.js", IDR_SUGGESTION_PICKER_JS}, + {"suggestionPicker.css", IDR_SUGGESTION_PICKER_CSS}, + {"colorSuggestionPicker.js", IDR_COLOR_SUGGESTION_PICKER_JS}, + {"colorSuggestionPicker.css", IDR_COLOR_SUGGESTION_PICKER_CSS} +#endif +}; + +} // namespace html_viewer + +#endif // MOJO_SERVICES_HTML_VIEWER_BLINK_RESOURCE_CONSTANTS_H_ diff --git a/mojo/services/html_viewer/generate_blink_resource_map.py b/mojo/services/html_viewer/generate_blink_resource_map.py new file mode 100644 index 000000000000..4044154a3048 --- /dev/null +++ b/mojo/services/html_viewer/generate_blink_resource_map.py @@ -0,0 +1,151 @@ +# Copyright 2015 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. + +from string import Template + +import optparse +import os +import sys + +try: + grit_module_path = os.path.join( + os.path.dirname(__file__), '..', '..', '..', 'tools', 'grit') + sys.path.insert(0, grit_module_path) + from grit.format import data_pack as DataPack +except ImportError, e: + print 'ImportError: ', e + sys.exit(-1) + +def is_ascii(s): + return all(ord(c) < 128 for c in s) + +header_template = \ +"""// Copyright 2015 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 MOJO_SERVICES_HTML_VIEWER_BLINK_RESOURCE_MAP_H_ +#define MOJO_SERVICES_HTML_VIEWER_BLINK_RESOURCE_MAP_H_ + +#include + +namespace html_viewer { + +class BlinkResourceMap { + public: + BlinkResourceMap(); + const char* GetResource(int id, int* length); + + private: + struct ResourceEntry { + const char* data; + int length; + + ResourceEntry() + : data(nullptr) + , length(0) { + } + + ResourceEntry(const char* data, int length) + : data(data) + , length(length) { + } + }; + typedef std::map ResourceMap; + ResourceMap resources_; +}; + +} // namespace html_viewer +#endif // MOJO_SERVICES_HTML_VIEWER_BLINK_RESOURCE_MAP_H_""" + +cpp_template = \ +"""// Copyright 2015 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 "$header_file_name" + +#include "base/macros.h" + +namespace html_viewer { + +$definitions + +BlinkResourceMap::BlinkResourceMap() +{ + $map_initializer +} + +const char* BlinkResourceMap::GetResource(int id, int* length) +{ + ResourceMap::iterator it = resources_.find(id); + if (it == resources_.end()) { + *length = 0; + return nullptr; + } + *length = it->second.length; + return it->second.data; +} + +} // namespace html_viewer""" + +def main(): + parser = optparse.OptionParser( + usage='Usage: %prog --pak-file PAK_FILE --header HEADER --cpp CPP\n') + parser.add_option('-i', '--pak-file', action='store', dest='pak_file', + help='The .pak file to be extracted.') + parser.add_option('', '--header', action='store', dest='header_file', + help='Header file to be generated.') + parser.add_option('', '--cpp', action='store', dest='cpp_file', + help='C++ file to be generated.') + + (options, _) = parser.parse_args() + if (not options.pak_file or not options.header_file or not options.cpp_file): + parser.print_help() + sys.exit(-1) + + header_file = open(options.header_file, 'w+') + cpp_file = open(options.cpp_file, 'w+') + + pak_contents = DataPack.ReadDataPack(options.pak_file) + resourceIds = [] + + header_contents = dict() + cpp_contents = dict() + + definitions = [] + + for (resId, data) in pak_contents.resources.iteritems(): + if not is_ascii(data): + continue + resourceIds.append(resId) + hex_values = ['0x{0:02x}'.format(ord(char)) for char in data] + f = lambda A, n=12: [A[i:i+n] for i in range(0, len(A), n)] + hex_values_string = ',\n '.join(', '.join(x) for x in f(hex_values)) + cpp_definition = \ + 'const char kResource%s[%d] = {\n %s \n};' % \ + (str(resId), len(hex_values), hex_values_string) + definitions.append(cpp_definition) + + header_file_contents = Template(header_template).substitute(header_contents) + header_file.write(header_file_contents) + header_file.close() + + map_initializer = [] + for resId in resourceIds: + insert_statement = \ + 'resources_.insert(std::pair(\n' \ + ' %s, ResourceEntry(kResource%s, arraysize(kResource%s))));' + map_initializer.append( \ + insert_statement % (str(resId), str(resId), str(resId))) + + cpp_contents['definitions']= '\n'.join(definitions) + cpp_contents['header_file_name'] = os.path.basename(options.header_file) + cpp_contents['map_initializer'] = '\n '.join(map_initializer) + cpp_file_contents = Template(cpp_template).substitute(cpp_contents) + cpp_file.write(cpp_file_contents) + cpp_file.close() + +if __name__ == '__main__': + main() -- 2.11.4.GIT