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."""
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).
59 'doubleclickusercontent.com',
64 'google-analytics.com',
65 'google-syndication.com',
291 'googleadservices.com',
292 'googleadsserving.cn',
297 'googlecommerce.com',
299 'googleenterprise.com',
302 'googlepayments.com',
304 'googlesyndication.com',
305 'googletagmanager.com',
306 'googletagservices.com',
307 'googleusercontent.com',
314 'youtube-3rd-party.com',
315 'youtube-nocookie.com',
443 'youtubeeducation.com',
444 'youtubemobilesupport.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"
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
:
474 json_files
= ast
.literal_eval(gypi_text
)['variables']['baked_in_configs']
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
)
493 if line_length
+ len(c
) > max_length
:
494 output
+= suffix
+ "\n" + prefix
497 line_length
+= len(c
)
503 if len(sys
.argv
) != 4:
504 print >> sys
.stderr
, (('Usage: %s <JSON pathname base directory> ' +
505 '<input .gypi file> <output .cpp file>') %
507 print >> sys
.stderr
, sys
.modules
[__name__
].__doc
__
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
]
518 found_invalid_config
= False
520 for json_file
in json_files
:
521 with
open(json_file
, 'r') as f
:
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
529 if 'monitored_domain' not in config
:
530 print >> sys
.stderr
, '%s: no monitored_domain found' % json_file
531 found_invalid_config
= True
533 domain
= config
['monitored_domain']
534 if not domain_is_whitelisted(domain
):
535 print >> sys
.stderr
, ('%s: monitored_domain "%s" not in whitelist' %
537 found_invalid_config
= True
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"
547 cpp_code
+= CC_FOOTER
549 if found_invalid_config
:
552 with
open(cpp_file
, 'wb') as f
:
558 if __name__
== '__main__':