937fce57a1eafc9ff2d58a53beedf451d4695d42
[platform/framework/web/crosswalk.git] / src / components / domain_reliability / bake_in_configs.py
1 #!/usr/bin/env python
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.
5
6
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."""
10
11
12 import json
13 import os
14 import sys
15
16
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')
24
25
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.
29
30 // AUTOGENERATED FILE. DO NOT EDIT.
31 //
32 // (Update configs in components/domain_reliability/baked_in_configs and list
33 // configs in components/domain_reliability.gypi instead.)
34
35 #include "components/domain_reliability/baked_in_configs.h"
36
37 #include <stdlib.h>
38
39 namespace domain_reliability {
40
41 const char* const kBakedInJsonConfigs[] = {
42 """
43
44
45 CC_FOOTER = """  NULL
46 };
47
48 }  // namespace domain_reliability
49 """
50
51
52 def domain_is_whitelisted(domain):
53   return any(domain == e or domain.endswith('.' + e)  for e in DOMAIN_WHITELIST)
54
55
56 def quote_and_wrap_text(text, width=79, prefix='  "', suffix='"'):
57   max_length = width - len(prefix) - len(suffix)
58   output = prefix
59   line_length = 0
60   for c in text:
61     if c == "\"":
62       c = "\\\""
63     elif c == "\n":
64       c = "\\n"
65     elif c == "\\":
66       c = "\\\\"
67     if line_length + len(c) > max_length:
68       output += suffix + "\n" + prefix
69       line_length = 0
70     output += c
71     line_length += len(c)
72   output += suffix
73   return output
74
75
76 def main():
77   if len(sys.argv) < 3:
78     print >> sys.stderr, ('Usage: %s <JSON files...> <output C++ file>' %
79                           sys.argv[0])
80     print >> sys.stderr, sys.modules[__name__].__doc__
81     return 1
82
83   cpp_code = CC_HEADER
84   found_invalid_config = False
85   for json_file in sys.argv[1:-1]:
86     with open(json_file, 'r') as f:
87       json_text = f.read()
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
92       continue
93     domain = config['monitored_domain']
94     if not domain_is_whitelisted(domain):
95       print >> sys.stderr, ('%s: monitored_domain "%s" not in whitelist' %
96                             (json_file, domain))
97       found_invalid_config = True
98       continue
99     cpp_code += "  // " + json_file + ":\n"
100     cpp_code += quote_and_wrap_text(json_text) + ",\n"
101     cpp_code += "\n"
102   cpp_code += CC_FOOTER
103
104   if found_invalid_config:
105     return 1
106
107   with open(sys.argv[-1], 'wb') as f:
108     f.write(cpp_code)
109
110   return 0
111
112
113 if __name__ == '__main__':
114   sys.exit(main())