bake_in_configs.py 12 KB

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