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