browser.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /**
  2. * @license
  3. * Copyright Google Inc. All Rights Reserved.
  4. *
  5. * Use of this source code is governed by an MIT-style license that can be
  6. * found in the LICENSE file at https://angular.io/license
  7. */
  8. /**
  9. * @fileoverview
  10. * @suppress {missingRequire}
  11. */
  12. import {findEventTasks} from '../common/events';
  13. import {patchTimer} from '../common/timers';
  14. import {patchClass, patchMethod, patchPrototype, scheduleMacroTaskWithCurrentZone, ZONE_SYMBOL_ADD_EVENT_LISTENER, ZONE_SYMBOL_REMOVE_EVENT_LISTENER, zoneSymbol} from '../common/utils';
  15. import {patchCustomElements} from './custom-elements';
  16. import {propertyPatch} from './define-property';
  17. import {eventTargetPatch, patchEvent} from './event-target';
  18. import {propertyDescriptorPatch} from './property-descriptor';
  19. Zone.__load_patch('legacy', (global: any) => {
  20. const legacyPatch = global[Zone.__symbol__('legacyPatch')];
  21. if (legacyPatch) {
  22. legacyPatch();
  23. }
  24. });
  25. Zone.__load_patch('timers', (global: any) => {
  26. const set = 'set';
  27. const clear = 'clear';
  28. patchTimer(global, set, clear, 'Timeout');
  29. patchTimer(global, set, clear, 'Interval');
  30. patchTimer(global, set, clear, 'Immediate');
  31. });
  32. Zone.__load_patch('requestAnimationFrame', (global: any) => {
  33. patchTimer(global, 'request', 'cancel', 'AnimationFrame');
  34. patchTimer(global, 'mozRequest', 'mozCancel', 'AnimationFrame');
  35. patchTimer(global, 'webkitRequest', 'webkitCancel', 'AnimationFrame');
  36. });
  37. Zone.__load_patch('blocking', (global: any, Zone: ZoneType) => {
  38. const blockingMethods = ['alert', 'prompt', 'confirm'];
  39. for (let i = 0; i < blockingMethods.length; i++) {
  40. const name = blockingMethods[i];
  41. patchMethod(global, name, (delegate, symbol, name) => {
  42. return function(s: any, args: any[]) {
  43. return Zone.current.run(delegate, global, args, name);
  44. };
  45. });
  46. }
  47. });
  48. Zone.__load_patch('EventTarget', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
  49. patchEvent(global, api);
  50. eventTargetPatch(global, api);
  51. // patch XMLHttpRequestEventTarget's addEventListener/removeEventListener
  52. const XMLHttpRequestEventTarget = (global as any)['XMLHttpRequestEventTarget'];
  53. if (XMLHttpRequestEventTarget && XMLHttpRequestEventTarget.prototype) {
  54. api.patchEventTarget(global, [XMLHttpRequestEventTarget.prototype]);
  55. }
  56. patchClass('MutationObserver');
  57. patchClass('WebKitMutationObserver');
  58. patchClass('IntersectionObserver');
  59. patchClass('FileReader');
  60. });
  61. Zone.__load_patch('on_property', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
  62. propertyDescriptorPatch(api, global);
  63. propertyPatch();
  64. });
  65. Zone.__load_patch('customElements', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
  66. patchCustomElements(global, api);
  67. });
  68. Zone.__load_patch('XHR', (global: any, Zone: ZoneType) => {
  69. // Treat XMLHttpRequest as a macrotask.
  70. patchXHR(global);
  71. const XHR_TASK = zoneSymbol('xhrTask');
  72. const XHR_SYNC = zoneSymbol('xhrSync');
  73. const XHR_LISTENER = zoneSymbol('xhrListener');
  74. const XHR_SCHEDULED = zoneSymbol('xhrScheduled');
  75. const XHR_URL = zoneSymbol('xhrURL');
  76. const XHR_ERROR_BEFORE_SCHEDULED = zoneSymbol('xhrErrorBeforeScheduled');
  77. interface XHROptions extends TaskData {
  78. target: any;
  79. url: string;
  80. args: any[];
  81. aborted: boolean;
  82. }
  83. function patchXHR(window: any) {
  84. const XMLHttpRequest = window['XMLHttpRequest'];
  85. if (!XMLHttpRequest) {
  86. // XMLHttpRequest is not available in service worker
  87. return;
  88. }
  89. const XMLHttpRequestPrototype: any = XMLHttpRequest.prototype;
  90. function findPendingTask(target: any) {
  91. return target[XHR_TASK];
  92. }
  93. let oriAddListener = XMLHttpRequestPrototype[ZONE_SYMBOL_ADD_EVENT_LISTENER];
  94. let oriRemoveListener = XMLHttpRequestPrototype[ZONE_SYMBOL_REMOVE_EVENT_LISTENER];
  95. if (!oriAddListener) {
  96. const XMLHttpRequestEventTarget = window['XMLHttpRequestEventTarget'];
  97. if (XMLHttpRequestEventTarget) {
  98. const XMLHttpRequestEventTargetPrototype = XMLHttpRequestEventTarget.prototype;
  99. oriAddListener = XMLHttpRequestEventTargetPrototype[ZONE_SYMBOL_ADD_EVENT_LISTENER];
  100. oriRemoveListener = XMLHttpRequestEventTargetPrototype[ZONE_SYMBOL_REMOVE_EVENT_LISTENER];
  101. }
  102. }
  103. const READY_STATE_CHANGE = 'readystatechange';
  104. const SCHEDULED = 'scheduled';
  105. function scheduleTask(task: Task) {
  106. const data = <XHROptions>task.data;
  107. const target = data.target;
  108. target[XHR_SCHEDULED] = false;
  109. target[XHR_ERROR_BEFORE_SCHEDULED] = false;
  110. // remove existing event listener
  111. const listener = target[XHR_LISTENER];
  112. if (!oriAddListener) {
  113. oriAddListener = target[ZONE_SYMBOL_ADD_EVENT_LISTENER];
  114. oriRemoveListener = target[ZONE_SYMBOL_REMOVE_EVENT_LISTENER];
  115. }
  116. if (listener) {
  117. oriRemoveListener.call(target, READY_STATE_CHANGE, listener);
  118. }
  119. const newListener = target[XHR_LISTENER] = () => {
  120. if (target.readyState === target.DONE) {
  121. // sometimes on some browsers XMLHttpRequest will fire onreadystatechange with
  122. // readyState=4 multiple times, so we need to check task state here
  123. if (!data.aborted && target[XHR_SCHEDULED] && task.state === SCHEDULED) {
  124. // check whether the xhr has registered onload listener
  125. // if that is the case, the task should invoke after all
  126. // onload listeners finish.
  127. const loadTasks = target['__zone_symbol__loadfalse'];
  128. if (loadTasks && loadTasks.length > 0) {
  129. const oriInvoke = task.invoke;
  130. task.invoke = function() {
  131. // need to load the tasks again, because in other
  132. // load listener, they may remove themselves
  133. const loadTasks = target['__zone_symbol__loadfalse'];
  134. for (let i = 0; i < loadTasks.length; i++) {
  135. if (loadTasks[i] === task) {
  136. loadTasks.splice(i, 1);
  137. }
  138. }
  139. if (!data.aborted && task.state === SCHEDULED) {
  140. oriInvoke.call(task);
  141. }
  142. };
  143. loadTasks.push(task);
  144. } else {
  145. task.invoke();
  146. }
  147. } else if (!data.aborted && target[XHR_SCHEDULED] === false) {
  148. // error occurs when xhr.send()
  149. target[XHR_ERROR_BEFORE_SCHEDULED] = true;
  150. }
  151. }
  152. };
  153. oriAddListener.call(target, READY_STATE_CHANGE, newListener);
  154. const storedTask: Task = target[XHR_TASK];
  155. if (!storedTask) {
  156. target[XHR_TASK] = task;
  157. }
  158. sendNative!.apply(target, data.args);
  159. target[XHR_SCHEDULED] = true;
  160. return task;
  161. }
  162. function placeholderCallback() {}
  163. function clearTask(task: Task) {
  164. const data = <XHROptions>task.data;
  165. // Note - ideally, we would call data.target.removeEventListener here, but it's too late
  166. // to prevent it from firing. So instead, we store info for the event listener.
  167. data.aborted = true;
  168. return abortNative!.apply(data.target, data.args);
  169. }
  170. const openNative =
  171. patchMethod(XMLHttpRequestPrototype, 'open', () => function(self: any, args: any[]) {
  172. self[XHR_SYNC] = args[2] == false;
  173. self[XHR_URL] = args[1];
  174. return openNative!.apply(self, args);
  175. });
  176. const XMLHTTPREQUEST_SOURCE = 'XMLHttpRequest.send';
  177. const fetchTaskAborting = zoneSymbol('fetchTaskAborting');
  178. const fetchTaskScheduling = zoneSymbol('fetchTaskScheduling');
  179. const sendNative: Function|null =
  180. patchMethod(XMLHttpRequestPrototype, 'send', () => function(self: any, args: any[]) {
  181. if ((Zone.current as any)[fetchTaskScheduling] === true) {
  182. // a fetch is scheduling, so we are using xhr to polyfill fetch
  183. // and because we already schedule macroTask for fetch, we should
  184. // not schedule a macroTask for xhr again
  185. return sendNative!.apply(self, args);
  186. }
  187. if (self[XHR_SYNC]) {
  188. // if the XHR is sync there is no task to schedule, just execute the code.
  189. return sendNative!.apply(self, args);
  190. } else {
  191. const options: XHROptions =
  192. {target: self, url: self[XHR_URL], isPeriodic: false, args: args, aborted: false};
  193. const task = scheduleMacroTaskWithCurrentZone(
  194. XMLHTTPREQUEST_SOURCE, placeholderCallback, options, scheduleTask, clearTask);
  195. if (self && self[XHR_ERROR_BEFORE_SCHEDULED] === true && !options.aborted &&
  196. task.state === SCHEDULED) {
  197. // xhr request throw error when send
  198. // we should invoke task instead of leaving a scheduled
  199. // pending macroTask
  200. task.invoke();
  201. }
  202. }
  203. });
  204. const abortNative =
  205. patchMethod(XMLHttpRequestPrototype, 'abort', () => function(self: any, args: any[]) {
  206. const task: Task = findPendingTask(self);
  207. if (task && typeof task.type == 'string') {
  208. // If the XHR has already completed, do nothing.
  209. // If the XHR has already been aborted, do nothing.
  210. // Fix #569, call abort multiple times before done will cause
  211. // macroTask task count be negative number
  212. if (task.cancelFn == null || (task.data && (<XHROptions>task.data).aborted)) {
  213. return;
  214. }
  215. task.zone.cancelTask(task);
  216. } else if ((Zone.current as any)[fetchTaskAborting] === true) {
  217. // the abort is called from fetch polyfill, we need to call native abort of XHR.
  218. return abortNative!.apply(self, args);
  219. }
  220. // Otherwise, we are trying to abort an XHR which has not yet been sent, so there is no
  221. // task
  222. // to cancel. Do nothing.
  223. });
  224. }
  225. });
  226. Zone.__load_patch('geolocation', (global: any) => {
  227. /// GEO_LOCATION
  228. if (global['navigator'] && global['navigator'].geolocation) {
  229. patchPrototype(global['navigator'].geolocation, ['getCurrentPosition', 'watchPosition']);
  230. }
  231. });
  232. Zone.__load_patch('PromiseRejectionEvent', (global: any, Zone: ZoneType) => {
  233. // handle unhandled promise rejection
  234. function findPromiseRejectionHandler(evtName: string) {
  235. return function(e: any) {
  236. const eventTasks = findEventTasks(global, evtName);
  237. eventTasks.forEach(eventTask => {
  238. // windows has added unhandledrejection event listener
  239. // trigger the event listener
  240. const PromiseRejectionEvent = global['PromiseRejectionEvent'];
  241. if (PromiseRejectionEvent) {
  242. const evt = new PromiseRejectionEvent(evtName, {promise: e.promise, reason: e.rejection});
  243. eventTask.invoke(evt);
  244. }
  245. });
  246. };
  247. }
  248. if (global['PromiseRejectionEvent']) {
  249. (Zone as any)[zoneSymbol('unhandledPromiseRejectionHandler')] =
  250. findPromiseRejectionHandler('unhandledrejection');
  251. (Zone as any)[zoneSymbol('rejectionHandledHandler')] =
  252. findPromiseRejectionHandler('rejectionhandled');
  253. }
  254. });