OLD | NEW |
(Empty) | |
| 1 // This file provides an OriginTrialsHelper object which can be used by |
| 2 // LayoutTests that are checking members exposed to script by origin trials. |
| 3 // |
| 4 // The current available methods are: |
| 5 // get_interface_names: |
| 6 // Report on the existence of the given interface names on the global object, |
| 7 // listing all the properties found for each interface. The properties can be |
| 8 // filtered by providing a list of desired property names. As well, it can |
| 9 // report on properties of the global object itself, by giving 'global' as one |
| 10 // of the interface names. |
| 11 // Example: |
| 12 // OriginTrialsHelper.get_interface_names( |
| 13 // this, |
| 14 // ['ForeignFetchEvent', 'InstallEvent', 'global'], |
| 15 // {'InstallEvent':['registerForeignFetch'], 'global':['onforeignfetch']})); |
| 16 // |
| 17 // check_interfaces_in_sw: |
| 18 // Collects the results of calling get_interface_names() in a service worker. |
| 19 // Use in a promise test to output the results. |
| 20 // Example: |
| 21 // promise_test(t => { |
| 22 // var script = 'path/to/script/calling/get_interface_names()' |
| 23 // var scope = 'matching scope' |
| 24 // return OriginTrialsHelper.check_interfaces_in_sw(t, script, scope) |
| 25 // .then(message => { |
| 26 // console.log('Interfaces in Service Worker - origin trial enabled.\n' |
| 27 // + message); |
| 28 // }); |
| 29 // |
| 30 // add_token: |
| 31 // Adds a trial token to the document, to enable a trial via script |
| 32 // Example: |
| 33 // OriginTrialsHelper.add_token('token produced by generate_token.py'); |
| 34 'use strict'; |
| 35 |
| 36 var OriginTrialsHelper = (function() { |
| 37 return { |
| 38 get_interface_names: |
| 39 (global_object, interface_names, |
| 40 opt_property_filters) => { |
| 41 var property_filters = opt_property_filters || {}; |
| 42 var result = []; |
| 43 function collect_property_info(object, property_name, output) { |
| 44 var descriptor = |
| 45 Object.getOwnPropertyDescriptor(object, property_name); |
| 46 if ('value' in descriptor) { |
| 47 if (typeof descriptor.value === 'function') { |
| 48 output.push(' method ' + property_name); |
| 49 } else { |
| 50 output.push(' attribute ' + property_name); |
| 51 } |
| 52 } else { |
| 53 if (descriptor.get) { |
| 54 output.push(' getter ' + property_name); |
| 55 } |
| 56 if (descriptor.set) { |
| 57 output.push(' setter ' + property_name); |
| 58 } |
| 59 } |
| 60 } |
| 61 interface_names.sort(); |
| 62 interface_names.forEach(function(interface_name) { |
| 63 var use_global = false; |
| 64 var interface_object; |
| 65 if (interface_name === 'global') { |
| 66 use_global = true; |
| 67 interface_object = global_object; |
| 68 } else { |
| 69 interface_object = global_object[interface_name]; |
| 70 } |
| 71 if (interface_object === undefined) { |
| 72 return; |
| 73 } |
| 74 var interface_prototype; |
| 75 var display_name; |
| 76 if (use_global) { |
| 77 interface_prototype = interface_object; |
| 78 display_name = 'global object'; |
| 79 } else { |
| 80 interface_prototype = interface_object.prototype; |
| 81 display_name = 'interface ' + interface_name; |
| 82 } |
| 83 result.push(display_name); |
| 84 var property_names = |
| 85 Object.getOwnPropertyNames(interface_prototype); |
| 86 var match_property_names = property_filters[interface_name]; |
| 87 if (match_property_names) { |
| 88 property_names = property_names.filter( |
| 89 name => { return match_property_names.indexOf(name) >= 0; }); |
| 90 } |
| 91 var property_strings = []; |
| 92 property_names.forEach(function(property_name) { |
| 93 collect_property_info( |
| 94 interface_prototype, property_name, property_strings); |
| 95 }); |
| 96 result.push.apply(result, property_strings.sort()); |
| 97 }); |
| 98 return result.join('\n'); |
| 99 }, |
| 100 |
| 101 check_interfaces_in_sw: |
| 102 (t, script, scope) => { |
| 103 var worker; |
| 104 var message; |
| 105 var registration; |
| 106 return service_worker_unregister_and_register(t, script, scope) |
| 107 .then(reg => { |
| 108 registration = reg; |
| 109 worker = registration.installing; |
| 110 return wait_for_state(t, worker, 'activated'); |
| 111 }) |
| 112 .then(_ => { |
| 113 var saw_message = new Promise(resolve => { |
| 114 navigator.serviceWorker.onmessage = |
| 115 e => { resolve(e.data); }; |
| 116 }); |
| 117 worker.postMessage(''); |
| 118 return saw_message; |
| 119 }) |
| 120 .then(msg => { |
| 121 message = msg; |
| 122 return registration.unregister(); |
| 123 }) |
| 124 .then(_ => { return message; }); |
| 125 }, |
| 126 |
| 127 add_token: (token_string) => { |
| 128 var tokenElement = document.createElement('meta'); |
| 129 tokenElement.httpEquiv = 'origin-trial'; |
| 130 tokenElement.content = token_string; |
| 131 document.head.appendChild(tokenElement); |
| 132 } |
| 133 } |
| 134 })(); |
OLD | NEW |