Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: polyfill.js

Issue 29582716: Issue 4579 - Ignore runtime.lastError caused by wrapper (Closed) Base URL: https://hg.adblockplus.org/adblockpluschrome/
Patch Set: Created Oct. 19, 2017, 1:56 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-present eyeo GmbH 3 * Copyright (C) 2006-present eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
(...skipping 25 matching lines...) Expand all
36 "tabs.query", 36 "tabs.query",
37 "tabs.reload", 37 "tabs.reload",
38 "tabs.sendMessage", 38 "tabs.sendMessage",
39 "tabs.update", 39 "tabs.update",
40 "webNavigation.getAllFrames", 40 "webNavigation.getAllFrames",
41 "webRequest.handlerBehaviorChanged", 41 "webRequest.handlerBehaviorChanged",
42 "windows.create", 42 "windows.create",
43 "windows.update" 43 "windows.update"
44 ]; 44 ];
45 45
46 // Errors that occur only when we show an interest in the response from an
47 // API call.
48 const noFulfillmentErrors = [
49 "The message port closed before a response was received."
50 ];
51
52 const fulfillmentInterestFlag = Symbol();
Manish Jethani 2017/10/19 01:59:46 We use a symbol here so it becomes like a private
53
54 function SpecialPromise(...args)
55 {
56 return Reflect.construct(Promise, args, SpecialPromise);
Manish Jethani 2017/10/19 01:59:46 Essentially an extended promise.
57 }
58
59 SpecialPromise.prototype = Object.create(
60 Object.assign({}, Promise.prototype, {
61 then(...args)
62 {
63 // Set this flag to indicate that there is interest in the fulfillment
64 // of this promise.
65 this[fulfillmentInterestFlag] = true;
66
67 return Promise.prototype.then.apply(this, args);
68 }
69 })
70 );
71
46 function wrapAPI(api) 72 function wrapAPI(api)
47 { 73 {
48 let object = browser; 74 let object = browser;
49 let path = api.split("."); 75 let path = api.split(".");
50 let name = path.pop(); 76 let name = path.pop();
51 77
52 for (let node of path) 78 for (let node of path)
53 { 79 {
54 object = object[node]; 80 object = object[node];
55 81
56 if (!object) 82 if (!object)
57 return; 83 return;
58 } 84 }
59 85
60 let func = object[name]; 86 let func = object[name];
61 object[name] = function(...args) 87 object[name] = function(...args)
62 { 88 {
63 if (typeof args[args.length - 1] == "function") 89 if (typeof args[args.length - 1] == "function")
64 return func.apply(object, args); 90 return func.apply(object, args);
65 91
66 // If the last argument is undefined, we drop it from the list assuming 92 // If the last argument is undefined, we drop it from the list assuming
67 // it stands for the optional callback. We must do this, because we have 93 // it stands for the optional callback. We must do this, because we have
68 // to replace it with our own callback. If we simply append our own 94 // to replace it with our own callback. If we simply append our own
69 // callback to the list, it won't match the signature of the function and 95 // callback to the list, it won't match the signature of the function and
70 // will cause an exception. 96 // will cause an exception.
71 if (typeof args[args.length - 1] == "undefined") 97 if (typeof args[args.length - 1] == "undefined")
72 args.pop(); 98 args.pop();
73 99
74 return new Promise((resolve, reject) => 100 let promise = new SpecialPromise((resolve, reject) =>
75 { 101 {
76 func.call(object, ...args, result => 102 func.call(object, ...args, result =>
77 { 103 {
78 let error = browser.runtime.lastError; 104 let error = browser.runtime.lastError;
79 if (error) 105 if (error)
106 {
107 // If is this an error we got only because we passed in a callback
108 // and there's no actual interest in the response, ignore the
109 // error.
110 if (noFulfillmentErrors.includes(error.message) &&
111 !promise[fulfillmentInterestFlag])
112 {
113 return;
114 }
115
80 reject(error); 116 reject(error);
117 }
81 else 118 else
119 {
82 resolve(result); 120 resolve(result);
121 }
83 }); 122 });
84 }); 123 });
124
125 return promise;
85 }; 126 };
86 } 127 }
87 128
88 function shouldWrapAPIs() 129 function shouldWrapAPIs()
89 { 130 {
90 try 131 try
91 { 132 {
92 return !(browser.storage.local.get([]) instanceof Promise); 133 return !(browser.storage.local.get([]) instanceof Promise);
93 } 134 }
94 catch (error) 135 catch (error)
(...skipping 17 matching lines...) Expand all
112 153
113 // Workaround since HTMLCollection, NodeList, StyleSheetList, and CSSRuleList 154 // Workaround since HTMLCollection, NodeList, StyleSheetList, and CSSRuleList
114 // didn't have iterator support before Chrome 51. 155 // didn't have iterator support before Chrome 51.
115 // https://bugs.chromium.org/p/chromium/issues/detail?id=401699 156 // https://bugs.chromium.org/p/chromium/issues/detail?id=401699
116 for (let object of [HTMLCollection, NodeList, StyleSheetList, CSSRuleList]) 157 for (let object of [HTMLCollection, NodeList, StyleSheetList, CSSRuleList])
117 { 158 {
118 if (!(Symbol.iterator in object.prototype)) 159 if (!(Symbol.iterator in object.prototype))
119 object.prototype[Symbol.iterator] = Array.prototype[Symbol.iterator]; 160 object.prototype[Symbol.iterator] = Array.prototype[Symbol.iterator];
120 } 161 }
121 } 162 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld