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',
296 'googlecommerce.com',
298 'googleenterprise.com',
301 'googlepayments.com',
303 'googlesyndication.com',
304 'googletagmanager.com',
305 'googletagservices.com',
306 'googleusercontent.com',
313 'youtube-3rd-party.com',
314 'youtube-nocookie.com',
442 'youtubeeducation.com',
443 'youtubemobilesupport.com',
448 CC_HEADER
= """// AUTOGENERATED FILE. DO NOT EDIT.
450 // (Update configs in components/domain_reliability/baked_in_configs and list
451 // configs in components/domain_reliability/baked_in_configs.gypi instead.)
453 #include "components/domain_reliability/baked_in_configs.h"
457 namespace domain_reliability {
459 const char* const kBakedInJsonConfigs[] = {
466 } // namespace domain_reliability
470 def read_json_files_from_gypi(gypi_file
):
471 with
open(gypi_file
, 'r') as f
:
473 json_files
= ast
.literal_eval(gypi_text
)['variables']['baked_in_configs']
477 def domain_is_whitelisted(domain
):
478 return any(domain
== e
or domain
.endswith('.' + e
) for e
in DOMAIN_WHITELIST
)
481 def quote_and_wrap_text(text
, width
=79, prefix
=' "', suffix
='"'):
482 max_length
= width
- len(prefix
) - len(suffix
)
492 if line_length
+ len(c
) > max_length
:
493 output
+= suffix
+ "\n" + prefix
496 line_length
+= len(c
)
502 if len(sys
.argv
) != 4:
503 print >> sys
.stderr
, (('Usage: %s <JSON pathname base directory> ' +
504 '<input .gypi file> <output .cpp file>') %
506 print >> sys
.stderr
, sys
.modules
[__name__
].__doc
__
508 json_path
= sys
.argv
[1]
509 gypi_file
= sys
.argv
[2]
510 cpp_file
= sys
.argv
[3]
512 json_files
= read_json_files_from_gypi(gypi_file
)
513 json_files
= [ os
.path
.join(json_path
, f
) for f
in json_files
]
514 json_files
= [ os
.path
.normpath(f
) for f
in json_files
]
517 found_invalid_config
= False
519 for json_file
in json_files
:
520 with
open(json_file
, 'r') as f
:
523 config
= json
.loads(json_text
)
524 except ValueError, e
:
525 print >> sys
.stderr
, "%s: error parsing JSON: %s" % (json_file
, e
)
526 found_invalid_config
= True
528 if 'monitored_domain' not in config
:
529 print >> sys
.stderr
, '%s: no monitored_domain found' % json_file
530 found_invalid_config
= True
532 domain
= config
['monitored_domain']
533 if not domain_is_whitelisted(domain
):
534 print >> sys
.stderr
, ('%s: monitored_domain "%s" not in whitelist' %
536 found_invalid_config
= True
539 # Re-dump JSON to get a more compact representation.
540 dumped_json_text
= json
.dumps(config
, separators
=(',', ':'))
542 cpp_code
+= " // " + json_file
+ ":\n"
543 cpp_code
+= quote_and_wrap_text(dumped_json_text
) + ",\n"
546 cpp_code
+= CC_FOOTER
548 if found_invalid_config
:
551 with
open(cpp_file
, 'wb') as f
:
557 if __name__
== '__main__':