* @param {!Window} win
(win)
| 58 | * @param {!Window} win |
| 59 | */ |
| 60 | constructor(win) { |
| 61 | /** @const @private {!Window} */ |
| 62 | this.win_ = win; |
| 63 | |
| 64 | /** @const @private {!../service/xhr-impl.Xhr} */ |
| 65 | this.xhr_ = Services.xhrFor(win); |
| 66 | |
| 67 | // Use `testLocation` for testing with iframes. @see testing/iframe.js. |
| 68 | let loc = win.location; |
| 69 | if (getMode().test && win.testLocation) { |
| 70 | loc = win.testLocation; |
| 71 | } |
| 72 | // Use RTV to make sure we fetch prod/canary/experiment correctly. |
| 73 | const useLocal = getMode().localDev || getMode().test; |
| 74 | const useRtvVersion = !useLocal; |
| 75 | |
| 76 | let url = ''; |
| 77 | |
| 78 | let policy = { |
| 79 | createScriptURL: function (url) { |
| 80 | // Only allow the correct webworker url to pass through |
| 81 | const regexURL = |
| 82 | /^https:\/\/([a-zA-Z0-9_-]+\.)?cdn\.ampproject\.org(\/.*)?$/; |
| 83 | |
| 84 | if ( |
| 85 | (regexURL.test(url) || getMode().test || getMode().localDev) && |
| 86 | (url.endsWith('ww.js') || |
| 87 | url.endsWith('ww.min.js') || |
| 88 | url.endsWith('ww.mjs') || |
| 89 | url.endsWith('ww.min.mjs')) |
| 90 | ) { |
| 91 | return url; |
| 92 | } else { |
| 93 | return ''; |
| 94 | } |
| 95 | }, |
| 96 | }; |
| 97 | |
| 98 | if (self.trustedTypes && self.trustedTypes.createPolicy) { |
| 99 | policy = self.trustedTypes.createPolicy('amp-worker#fetchUrl', policy); |
| 100 | } |
| 101 | |
| 102 | url = policy |
| 103 | .createScriptURL( |
| 104 | calculateEntryPointScriptUrl(loc, 'ww', useLocal, useRtvVersion) |
| 105 | ) |
| 106 | .toString(); |
| 107 | |
| 108 | dev().fine(TAG, 'Fetching web worker from', url); |
| 109 | |
| 110 | /** @private {Worker} */ |
| 111 | this.worker_ = null; |
| 112 | |
| 113 | /** @const @private {!Promise} */ |
| 114 | this.fetchPromise_ = this.xhr_ |
| 115 | .fetchText(url, { |
| 116 | ampCors: false, |
| 117 | bypassInterceptorForDev: getMode().localDev, |