OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 | 6 |
7 """Takes the JSON files in components/domain_reliability/baked_in_configs and | 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 | 8 encodes their contents as an array of C strings that gets compiled in to Chrome |
9 and loaded at runtime.""" | 9 and loaded at runtime.""" |
10 | 10 |
(...skipping 465 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
476 json_files = ast.literal_eval(gypi_text)['variables']['baked_in_configs'] | 476 json_files = ast.literal_eval(gypi_text)['variables']['baked_in_configs'] |
477 return json_files | 477 return json_files |
478 | 478 |
479 | 479 |
480 def read_json_files_from_file(list_file): | 480 def read_json_files_from_file(list_file): |
481 with open(list_file, 'r') as f: | 481 with open(list_file, 'r') as f: |
482 list_text = f.read() | 482 list_text = f.read() |
483 return shlex.split(list_text) | 483 return shlex.split(list_text) |
484 | 484 |
485 | 485 |
486 def domain_is_whitelisted(domain): | 486 def origin_is_whitelisted(origin): |
| 487 if origin.startswith('https://') and origin.endswith('/'): |
| 488 domain = origin[8:-1] |
| 489 else: |
| 490 return False |
487 return any(domain == e or domain.endswith('.' + e) for e in DOMAIN_WHITELIST) | 491 return any(domain == e or domain.endswith('.' + e) for e in DOMAIN_WHITELIST) |
488 | 492 |
489 | 493 |
490 def quote_and_wrap_text(text, width=79, prefix=' "', suffix='"'): | 494 def quote_and_wrap_text(text, width=79, prefix=' "', suffix='"'): |
491 max_length = width - len(prefix) - len(suffix) | 495 max_length = width - len(prefix) - len(suffix) |
492 output = prefix | 496 output = prefix |
493 line_length = 0 | 497 line_length = 0 |
494 for c in text: | 498 for c in text: |
495 if c == "\"": | 499 if c == "\"": |
496 c = "\\\"" | 500 c = "\\\"" |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
550 | 554 |
551 for json_file in json_files: | 555 for json_file in json_files: |
552 with open(json_file, 'r') as f: | 556 with open(json_file, 'r') as f: |
553 json_text = f.read() | 557 json_text = f.read() |
554 try: | 558 try: |
555 config = json.loads(json_text) | 559 config = json.loads(json_text) |
556 except ValueError, e: | 560 except ValueError, e: |
557 print >> sys.stderr, "%s: error parsing JSON: %s" % (json_file, e) | 561 print >> sys.stderr, "%s: error parsing JSON: %s" % (json_file, e) |
558 found_invalid_config = True | 562 found_invalid_config = True |
559 continue | 563 continue |
560 if 'monitored_domain' not in config: | 564 if 'origin' not in config: |
561 print >> sys.stderr, '%s: no monitored_domain found' % json_file | 565 print >> sys.stderr, '%s: no origin found' % json_file |
562 found_invalid_config = True | 566 found_invalid_config = True |
563 continue | 567 continue |
564 domain = config['monitored_domain'] | 568 origin = config['origin'] |
565 if not domain_is_whitelisted(domain): | 569 if not origin_is_whitelisted(origin): |
566 print >> sys.stderr, ('%s: monitored_domain "%s" not in whitelist' % | 570 print >> sys.stderr, ('%s: origin "%s" not in whitelist' % |
567 (json_file, domain)) | 571 (json_file, origin)) |
568 found_invalid_config = True | 572 found_invalid_config = True |
569 continue | 573 continue |
570 | 574 |
571 # Re-dump JSON to get a more compact representation. | 575 # Re-dump JSON to get a more compact representation. |
572 dumped_json_text = json.dumps(config, separators=(',', ':')) | 576 dumped_json_text = json.dumps(config, separators=(',', ':')) |
573 | 577 |
574 cpp_code += " // " + json_file + ":\n" | 578 cpp_code += " // " + json_file + ":\n" |
575 cpp_code += quote_and_wrap_text(dumped_json_text) + ",\n" | 579 cpp_code += quote_and_wrap_text(dumped_json_text) + ",\n" |
576 cpp_code += "\n" | 580 cpp_code += "\n" |
577 | 581 |
578 cpp_code += CC_FOOTER | 582 cpp_code += CC_FOOTER |
579 | 583 |
580 if found_invalid_config: | 584 if found_invalid_config: |
581 return 1 | 585 return 1 |
582 | 586 |
583 with open(opts.output, 'wb') as f: | 587 with open(opts.output, 'wb') as f: |
584 f.write(cpp_code) | 588 f.write(cpp_code) |
585 | 589 |
586 return 0 | 590 return 0 |
587 | 591 |
588 | 592 |
589 if __name__ == '__main__': | 593 if __name__ == '__main__': |
590 sys.exit(main()) | 594 sys.exit(main()) |
OLD | NEW |