Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(352)

Side by Side Diff: mojo/services/html_viewer/generate_blink_resource_map.py

Issue 827223002: [mojo] Make HTML viewer load the blink resources from the generated pak file. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Adding new blink_resource_constants.h file Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « mojo/services/html_viewer/blink_resource_constants.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 # Copyright 2015 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 from string import Template
6
7 import optparse
8 import os
9 import sys
10
11 try:
12 grit_module_path = os.path.join(
13 os.path.dirname(__file__), '..', '..', '..', 'tools', 'grit')
14 sys.path.insert(0, grit_module_path)
15 from grit.format import data_pack as DataPack
16 except ImportError, e:
17 print 'ImportError: ', e
18 sys.exit(-1)
19
20 def is_ascii(s):
21 return all(ord(c) < 128 for c in s)
22
23 header_template = \
24 """// Copyright 2015 The Chromium Authors. All rights reserved.
25 // Use of this source code is governed by a BSD-style license that can be
26 // found in the LICENSE file.
27
28 #ifndef MOJO_SERVICES_HTML_VIEWER_BLINK_RESOURCE_MAP_H_
29 #define MOJO_SERVICES_HTML_VIEWER_BLINK_RESOURCE_MAP_H_
30
31 #include <map>
32
33 namespace html_viewer {
34
35 class BlinkResourceMap {
36 public:
37 BlinkResourceMap();
38 const char* GetResource(int id, int* length);
39
40 private:
41 struct ResourceEntry {
42 const char* data;
43 int length;
44
45 ResourceEntry()
46 : data(nullptr)
47 , length(0) {
48 }
49
50 ResourceEntry(const char* data, int length)
51 : data(data)
52 , length(length) {
53 }
54 };
55 typedef std::map<int, ResourceEntry> ResourceMap;
56 ResourceMap resources_;
57 };
58
59 } // namespace html_viewer
60 #endif // MOJO_SERVICES_HTML_VIEWER_BLINK_RESOURCE_MAP_H_"""
61
62 cpp_template = \
63 """// Copyright 2015 The Chromium Authors. All rights reserved.
64 // Use of this source code is governed by a BSD-style license that can be
65 // found in the LICENSE file.
66
67 #include "$header_file_name"
68
69 #include "base/macros.h"
70
71 namespace html_viewer {
72
73 $definitions
74
75 BlinkResourceMap::BlinkResourceMap()
76 {
77 $map_initializer
78 }
79
80 const char* BlinkResourceMap::GetResource(int id, int* length)
81 {
82 ResourceMap::iterator it = resources_.find(id);
83 if (it == resources_.end()) {
84 *length = 0;
85 return nullptr;
86 }
87 *length = it->second.length;
88 return it->second.data;
89 }
90
91 } // namespace html_viewer"""
92
93 def main():
94 parser = optparse.OptionParser(
95 usage='Usage: %prog --pak-file PAK_FILE --header HEADER --cpp CPP\n')
96 parser.add_option('-i', '--pak-file', action='store', dest='pak_file',
97 help='The .pak file to be extracted.')
98 parser.add_option('', '--header', action='store', dest='header_file',
99 help='Header file to be generated.')
100 parser.add_option('', '--cpp', action='store', dest='cpp_file',
101 help='C++ file to be generated.')
102
103 (options, _) = parser.parse_args()
104 if (not options.pak_file or not options.header_file or not options.cpp_file):
105 parser.print_help()
106 sys.exit(-1)
107
108 header_file = open(options.header_file, 'w+')
109 cpp_file = open(options.cpp_file, 'w+')
110
111 pak_contents = DataPack.ReadDataPack(options.pak_file)
112 resourceIds = []
113
114 header_contents = dict()
115 cpp_contents = dict()
116
117 definitions = []
118
119 for (resId, data) in pak_contents.resources.iteritems():
120 if not is_ascii(data):
121 continue
122 resourceIds.append(resId)
123 hex_values = ['0x{0:02x}'.format(ord(char)) for char in data]
124 f = lambda A, n=12: [A[i:i+n] for i in range(0, len(A), n)]
125 hex_values_string = ',\n '.join(', '.join(x) for x in f(hex_values))
126 cpp_definition = \
127 'const char kResource%s[%d] = {\n %s \n};' % \
128 (str(resId), len(hex_values), hex_values_string)
129 definitions.append(cpp_definition)
130
131 header_file_contents = Template(header_template).substitute(header_contents)
132 header_file.write(header_file_contents)
133 header_file.close()
134
135 map_initializer = []
136 for resId in resourceIds:
137 insert_statement = \
138 'resources_.insert(std::pair<int, ResourceEntry>(\n' \
139 ' %s, ResourceEntry(kResource%s, arraysize(kResource%s))));'
140 map_initializer.append( \
141 insert_statement % (str(resId), str(resId), str(resId)))
142
143 cpp_contents['definitions']= '\n'.join(definitions)
144 cpp_contents['header_file_name'] = os.path.basename(options.header_file)
145 cpp_contents['map_initializer'] = '\n '.join(map_initializer)
146 cpp_file_contents = Template(cpp_template).substitute(cpp_contents)
147 cpp_file.write(cpp_file_contents)
148 cpp_file.close()
149
150 if __name__ == '__main__':
151 main()
OLDNEW
« no previous file with comments | « mojo/services/html_viewer/blink_resource_constants.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698