QUIC - cleanup changes to sync chromium tree with internal source.
[chromium-blink-merge.git] / components / domain_reliability / bake_in_configs.py
blob8cee936d7585d4020bde4f773149942531e2ec06
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.
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."""
12 import ast
13 import json
14 import os
15 import sys
18 # A whitelist of domains that the script will accept when baking configs in to
19 # Chrome, to ensure incorrect ones are not added accidentally. Subdomains of
20 # whitelist entries are also allowed (e.g. maps.google.com, ssl.gstatic.com).
21 DOMAIN_WHITELIST = (
22 '2mdn.net',
23 'admob.biz',
24 'admob.co.in',
25 'admob.co.kr',
26 'admob.co.nz',
27 'admob.co.uk',
28 'admob.co.za',
29 'admob.com',
30 'admob.com.br',
31 'admob.com.es',
32 'admob.com.fr',
33 'admob.com.mx',
34 'admob.com.pt',
35 'admob.de',
36 'admob.dk',
37 'admob.es',
38 'admob.fi',
39 'admob.fr',
40 'admob.gr',
41 'admob.hk',
42 'admob.ie',
43 'admob.in',
44 'admob.it',
45 'admob.jp',
46 'admob.kr',
47 'admob.mobi',
48 'admob.no',
49 'admob.ph',
50 'admob.pt',
51 'admob.sg',
52 'admob.tw',
53 'admob.us',
54 'admob.vn',
55 'dartmotif.com',
56 'doubleclick.com',
57 'doubleclick.ne.jp',
58 'doubleclick.net',
59 'doubleclickusercontent.com',
60 'g.co',
61 'ggpht.com',
62 'gmodules.com',
63 'goo.gl',
64 'google-analytics.com',
65 'google-syndication.com',
66 'google.ac',
67 'google.ad',
68 'google.ae',
69 'google.af',
70 'google.ag',
71 'google.al',
72 'google.am',
73 'google.as',
74 'google.at',
75 'google.az',
76 'google.ba',
77 'google.be',
78 'google.bf',
79 'google.bg',
80 'google.bi',
81 'google.bj',
82 'google.bs',
83 'google.bt',
84 'google.by',
85 'google.ca',
86 'google.cat',
87 'google.cc',
88 'google.cd',
89 'google.cf',
90 'google.cg',
91 'google.ch',
92 'google.ci',
93 'google.cl',
94 'google.cm',
95 'google.cn',
96 'google.co.ao',
97 'google.co.bw',
98 'google.co.ck',
99 'google.co.cr',
100 'google.co.hu',
101 'google.co.id',
102 'google.co.il',
103 'google.co.im',
104 'google.co.in',
105 'google.co.je',
106 'google.co.jp',
107 'google.co.ke',
108 'google.co.kr',
109 'google.co.ls',
110 'google.co.ma',
111 'google.co.mz',
112 'google.co.nz',
113 'google.co.th',
114 'google.co.tz',
115 'google.co.ug',
116 'google.co.uk',
117 'google.co.uz',
118 'google.co.ve',
119 'google.co.vi',
120 'google.co.za',
121 'google.co.zm',
122 'google.co.zw',
123 'google.com',
124 'google.com.af',
125 'google.com.ag',
126 'google.com.ai',
127 'google.com.ar',
128 'google.com.au',
129 'google.com.bd',
130 'google.com.bh',
131 'google.com.bn',
132 'google.com.bo',
133 'google.com.br',
134 'google.com.by',
135 'google.com.bz',
136 'google.com.cn',
137 'google.com.co',
138 'google.com.cu',
139 'google.com.cy',
140 'google.com.do',
141 'google.com.ec',
142 'google.com.eg',
143 'google.com.et',
144 'google.com.fj',
145 'google.com.ge',
146 'google.com.gh',
147 'google.com.gi',
148 'google.com.gr',
149 'google.com.gt',
150 'google.com.hk',
151 'google.com.iq',
152 'google.com.jm',
153 'google.com.jo',
154 'google.com.kh',
155 'google.com.kw',
156 'google.com.lb',
157 'google.com.ly',
158 'google.com.mm',
159 'google.com.mt',
160 'google.com.mx',
161 'google.com.my',
162 'google.com.na',
163 'google.com.nf',
164 'google.com.ng',
165 'google.com.ni',
166 'google.com.np',
167 'google.com.nr',
168 'google.com.om',
169 'google.com.pa',
170 'google.com.pe',
171 'google.com.pg',
172 'google.com.ph',
173 'google.com.pk',
174 'google.com.pl',
175 'google.com.pr',
176 'google.com.py',
177 'google.com.qa',
178 'google.com.ru',
179 'google.com.sa',
180 'google.com.sb',
181 'google.com.sg',
182 'google.com.sl',
183 'google.com.sv',
184 'google.com.tj',
185 'google.com.tn',
186 'google.com.tr',
187 'google.com.tw',
188 'google.com.ua',
189 'google.com.uy',
190 'google.com.vc',
191 'google.com.ve',
192 'google.com.vn',
193 'google.cv',
194 'google.cz',
195 'google.de',
196 'google.dj',
197 'google.dk',
198 'google.dm',
199 'google.dz',
200 'google.ee',
201 'google.es',
202 'google.fi',
203 'google.fm',
204 'google.fr',
205 'google.ga',
206 'google.ge',
207 'google.gg',
208 'google.gl',
209 'google.gm',
210 'google.gp',
211 'google.gr',
212 'google.gy',
213 'google.hk',
214 'google.hn',
215 'google.hr',
216 'google.ht',
217 'google.hu',
218 'google.ie',
219 'google.im',
220 'google.info',
221 'google.iq',
222 'google.ir',
223 'google.is',
224 'google.it',
225 'google.it.ao',
226 'google.je',
227 'google.jo',
228 'google.jobs',
229 'google.jp',
230 'google.kg',
231 'google.ki',
232 'google.kz',
233 'google.la',
234 'google.li',
235 'google.lk',
236 'google.lt',
237 'google.lu',
238 'google.lv',
239 'google.md',
240 'google.me',
241 'google.mg',
242 'google.mk',
243 'google.ml',
244 'google.mn',
245 'google.ms',
246 'google.mu',
247 'google.mv',
248 'google.mw',
249 'google.ne',
250 'google.ne.jp',
251 'google.net',
252 'google.ng',
253 'google.nl',
254 'google.no',
255 'google.nr',
256 'google.nu',
257 'google.off.ai',
258 'google.org',
259 'google.pk',
260 'google.pl',
261 'google.pn',
262 'google.ps',
263 'google.pt',
264 'google.ro',
265 'google.rs',
266 'google.ru',
267 'google.rw',
268 'google.sc',
269 'google.se',
270 'google.sh',
271 'google.si',
272 'google.sk',
273 'google.sm',
274 'google.sn',
275 'google.so',
276 'google.sr',
277 'google.st',
278 'google.td',
279 'google.tg',
280 'google.tk',
281 'google.tl',
282 'google.tm',
283 'google.tn',
284 'google.to',
285 'google.tt',
286 'google.us',
287 'google.uz',
288 'google.vg',
289 'google.vu',
290 'google.ws',
291 'googleadservices.com',
292 'googleadsserving.cn',
293 'googlealumni.com',
294 'googleapis.com',
295 'googleapps.com',
296 'googlecbs.com',
297 'googlecommerce.com',
298 'googledrive.com',
299 'googleenterprise.com',
300 'googlegoro.com',
301 'googlehosted.com',
302 'googlepayments.com',
303 'googlesource.com',
304 'googlesyndication.com',
305 'googletagmanager.com',
306 'googletagservices.com',
307 'googleusercontent.com',
308 'googlevideo.com',
309 'gstatic.com',
310 'gvt1.com',
311 'gvt2.com',
312 'withgoogle.com',
313 'youtu.be',
314 'youtube-3rd-party.com',
315 'youtube-nocookie.com',
316 'youtube.ae',
317 'youtube.al',
318 'youtube.am',
319 'youtube.at',
320 'youtube.az',
321 'youtube.ba',
322 'youtube.be',
323 'youtube.bg',
324 'youtube.bh',
325 'youtube.bo',
326 'youtube.ca',
327 'youtube.cat',
328 'youtube.ch',
329 'youtube.cl',
330 'youtube.co',
331 'youtube.co.ae',
332 'youtube.co.at',
333 'youtube.co.hu',
334 'youtube.co.id',
335 'youtube.co.il',
336 'youtube.co.in',
337 'youtube.co.jp',
338 'youtube.co.ke',
339 'youtube.co.kr',
340 'youtube.co.ma',
341 'youtube.co.nz',
342 'youtube.co.th',
343 'youtube.co.ug',
344 'youtube.co.uk',
345 'youtube.co.ve',
346 'youtube.co.za',
347 'youtube.com',
348 'youtube.com.ar',
349 'youtube.com.au',
350 'youtube.com.az',
351 'youtube.com.bh',
352 'youtube.com.bo',
353 'youtube.com.br',
354 'youtube.com.by',
355 'youtube.com.co',
356 'youtube.com.do',
357 'youtube.com.ee',
358 'youtube.com.eg',
359 'youtube.com.es',
360 'youtube.com.gh',
361 'youtube.com.gr',
362 'youtube.com.gt',
363 'youtube.com.hk',
364 'youtube.com.hr',
365 'youtube.com.jm',
366 'youtube.com.jo',
367 'youtube.com.kw',
368 'youtube.com.lb',
369 'youtube.com.lv',
370 'youtube.com.mk',
371 'youtube.com.mt',
372 'youtube.com.mx',
373 'youtube.com.my',
374 'youtube.com.ng',
375 'youtube.com.om',
376 'youtube.com.pe',
377 'youtube.com.ph',
378 'youtube.com.pk',
379 'youtube.com.pt',
380 'youtube.com.qa',
381 'youtube.com.ro',
382 'youtube.com.sa',
383 'youtube.com.sg',
384 'youtube.com.tn',
385 'youtube.com.tr',
386 'youtube.com.tw',
387 'youtube.com.ua',
388 'youtube.com.uy',
389 'youtube.com.ve',
390 'youtube.cz',
391 'youtube.de',
392 'youtube.dk',
393 'youtube.ee',
394 'youtube.es',
395 'youtube.fi',
396 'youtube.fr',
397 'youtube.ge',
398 'youtube.gr',
399 'youtube.gt',
400 'youtube.hk',
401 'youtube.hr',
402 'youtube.hu',
403 'youtube.ie',
404 'youtube.in',
405 'youtube.is',
406 'youtube.it',
407 'youtube.jo',
408 'youtube.jp',
409 'youtube.kr',
410 'youtube.lk',
411 'youtube.lt',
412 'youtube.lv',
413 'youtube.ma',
414 'youtube.md',
415 'youtube.me',
416 'youtube.mk',
417 'youtube.mx',
418 'youtube.my',
419 'youtube.ng',
420 'youtube.nl',
421 'youtube.no',
422 'youtube.pe',
423 'youtube.ph',
424 'youtube.pk',
425 'youtube.pl',
426 'youtube.pr',
427 'youtube.pt',
428 'youtube.qa',
429 'youtube.ro',
430 'youtube.rs',
431 'youtube.ru',
432 'youtube.sa',
433 'youtube.se',
434 'youtube.sg',
435 'youtube.si',
436 'youtube.sk',
437 'youtube.sn',
438 'youtube.tn',
439 'youtube.ua',
440 'youtube.ug',
441 'youtube.uy',
442 'youtube.vn',
443 'youtubeeducation.com',
444 'youtubemobilesupport.com',
445 'ytimg.com'
449 CC_HEADER = """// AUTOGENERATED FILE. DO NOT EDIT.
451 // (Update configs in components/domain_reliability/baked_in_configs and list
452 // configs in components/domain_reliability/baked_in_configs.gypi instead.)
454 #include "components/domain_reliability/baked_in_configs.h"
456 #include <stdlib.h>
458 namespace domain_reliability {
460 const char* const kBakedInJsonConfigs[] = {
464 CC_FOOTER = """ nullptr
467 } // namespace domain_reliability
471 def read_json_files_from_gypi(gypi_file):
472 with open(gypi_file, 'r') as f:
473 gypi_text = f.read()
474 json_files = ast.literal_eval(gypi_text)['variables']['baked_in_configs']
475 return json_files
478 def domain_is_whitelisted(domain):
479 return any(domain == e or domain.endswith('.' + e) for e in DOMAIN_WHITELIST)
482 def quote_and_wrap_text(text, width=79, prefix=' "', suffix='"'):
483 max_length = width - len(prefix) - len(suffix)
484 output = prefix
485 line_length = 0
486 for c in text:
487 if c == "\"":
488 c = "\\\""
489 elif c == "\n":
490 c = "\\n"
491 elif c == "\\":
492 c = "\\\\"
493 if line_length + len(c) > max_length:
494 output += suffix + "\n" + prefix
495 line_length = 0
496 output += c
497 line_length += len(c)
498 output += suffix
499 return output
502 def main():
503 if len(sys.argv) != 4:
504 print >> sys.stderr, (('Usage: %s <JSON pathname base directory> ' +
505 '<input .gypi file> <output .cpp file>') %
506 sys.argv[0])
507 print >> sys.stderr, sys.modules[__name__].__doc__
508 return 1
509 json_path = sys.argv[1]
510 gypi_file = sys.argv[2]
511 cpp_file = sys.argv[3]
513 json_files = read_json_files_from_gypi(gypi_file)
514 json_files = [ os.path.join(json_path, f) for f in json_files ]
515 json_files = [ os.path.normpath(f) for f in json_files ]
517 cpp_code = CC_HEADER
518 found_invalid_config = False
520 for json_file in json_files:
521 with open(json_file, 'r') as f:
522 json_text = f.read()
523 try:
524 config = json.loads(json_text)
525 except ValueError, e:
526 print >> sys.stderr, "%s: error parsing JSON: %s" % (json_file, e)
527 found_invalid_config = True
528 continue
529 if 'monitored_domain' not in config:
530 print >> sys.stderr, '%s: no monitored_domain found' % json_file
531 found_invalid_config = True
532 continue
533 domain = config['monitored_domain']
534 if not domain_is_whitelisted(domain):
535 print >> sys.stderr, ('%s: monitored_domain "%s" not in whitelist' %
536 (json_file, domain))
537 found_invalid_config = True
538 continue
540 # Re-dump JSON to get a more compact representation.
541 dumped_json_text = json.dumps(config, separators=(',', ':'))
543 cpp_code += " // " + json_file + ":\n"
544 cpp_code += quote_and_wrap_text(dumped_json_text) + ",\n"
545 cpp_code += "\n"
547 cpp_code += CC_FOOTER
549 if found_invalid_config:
550 return 1
552 with open(cpp_file, 'wb') as f:
553 f.write(cpp_code)
555 return 0
558 if __name__ == '__main__':
559 sys.exit(main())