2 # Copyright 2014 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
7 """Takes the JSON files in components/domain_reliability/baked_in_configs and
8 encodes their contents as an array of C strings that gets compiled in to Chrome
9 and loaded at runtime."""
17 # A whitelist of domains that the script will accept when baking configs in to
18 # Chrome, to ensure incorrect ones are not added accidentally. Subdomains of
19 # whitelist entries are also allowed (e.g. maps.google.com, ssl.gstatic.com).
20 DOMAIN_WHITELIST = ('2mdn.net', 'admob.com', 'doubleclick.net', 'ggpht.com',
21 'google.com', 'googleadservices.com', 'googleapis.com',
22 'googlesyndication.com', 'googleusercontent.com',
23 'googlevideo.com', 'gstatic.com', 'gvt1.com', 'youtube.com')
26 CC_HEADER = """// Copyright (C) 2014 The Chromium Authors. All rights reserved.
27 // Use of this source code is governed by a BSD-style license that can be
28 // found in the LICENSE file.
30 // AUTOGENERATED FILE. DO NOT EDIT.
32 // (Update configs in components/domain_reliability/baked_in_configs and list
33 // configs in components/domain_reliability.gypi instead.)
35 #include "components/domain_reliability/baked_in_configs.h"
39 namespace domain_reliability {
41 const char* const kBakedInJsonConfigs[] = {
48 } // namespace domain_reliability
52 def domain_is_whitelisted(domain):
53 return any(domain == e or domain.endswith('.' + e) for e in DOMAIN_WHITELIST)
56 def quote_and_wrap_text(text, width=79, prefix=' "', suffix='"'):
57 max_length = width - len(prefix) - len(suffix)
67 if line_length + len(c) > max_length:
68 output += suffix + "\n" + prefix
78 print >> sys.stderr, ('Usage: %s <JSON files...> <output C++ file>' %
80 print >> sys.stderr, sys.modules[__name__].__doc__
84 found_invalid_config = False
85 for json_file in sys.argv[1:-1]:
86 with open(json_file, 'r') as f:
88 config = json.loads(json_text)
89 if 'monitored_domain' not in config:
90 print >> sys.stderr, ('%s: no monitored_domain found' % json_file)
91 found_invalid_config = True
93 domain = config['monitored_domain']
94 if not domain_is_whitelisted(domain):
95 print >> sys.stderr, ('%s: monitored_domain "%s" not in whitelist' %
97 found_invalid_config = True
99 cpp_code += " // " + json_file + ":\n"
100 cpp_code += quote_and_wrap_text(json_text) + ",\n"
102 cpp_code += CC_FOOTER
104 if found_invalid_config:
107 with open(sys.argv[-1], 'wb') as f:
113 if __name__ == '__main__':