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

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

Powered by Google App Engine
This is Rietveld 408576698