From a460ed85d2abf26971739a4de86c8fbb6ae2593f Mon Sep 17 00:00:00 2001 From: "Ilya Ig. Petrov" Date: Sun, 5 Feb 2017 17:06:30 +0000 Subject: [PATCH] Less logs, no betterStack, get rid of API fixes, setTimeout each API CB --- .../extension/00-init-apis.js | 16 +++----- .../extension/11-error-handlers-api.js | 37 ++++++++++--------- .../extension/12-errors-lib.js | 2 +- .../extension/14-ip-to-host-api.js | 3 -- .../extension/15-pac-kitchen-api.js | 17 +++++---- ...7-sync-pac-script-with-pac-provider-api.js | 24 +++++------- .../extension/20-api-fixes.js | 27 -------------- .../extension/manifest.json | 1 - .../pages/choose-pac-provider/index.js | 9 ++--- .../extension/pages/troubleshoot/index.js | 4 +- 10 files changed, 53 insertions(+), 87 deletions(-) delete mode 100644 extensions/chromium/runet-censorship-bypass/extension/20-api-fixes.js diff --git a/extensions/chromium/runet-censorship-bypass/extension/00-init-apis.js b/extensions/chromium/runet-censorship-bypass/extension/00-init-apis.js index 26ed1fb..d1679d5 100644 --- a/extensions/chromium/runet-censorship-bypass/extension/00-init-apis.js +++ b/extensions/chromium/runet-censorship-bypass/extension/00-init-apis.js @@ -35,17 +35,14 @@ }, - checkChromeError(betterStack) { + checkChromeError() { // Chrome API calls your cb in a context different from the point of API // method invokation. - const err = chrome.runtime.lastError || chrome.extension.lastError; + let err = chrome.runtime.lastError || chrome.extension.lastError; if (err) { - const args = ['API returned error:', err]; - if (betterStack) { - args.push('\n' + betterStack); - } - console.warn(...args); + err = new Error(err.message); // Add stack. + console.warn('API returned error:', err); } return err; @@ -53,14 +50,13 @@ chromified(cb = self.mandatory(), ...replaceArgs) { - const stack = (new Error()).stack; - // Take error first callback and convert it to chrome api callback. + // Take error first callback and convert it to chrome API callback. return function(...args) { if (replaceArgs.length) { args = replaceArgs; } - const err = self.checkChromeError(stack); + const err = self.checkChromeError(); // setTimeout fixes error context. setTimeout( cb.bind(null, err, ...args), 0 ); diff --git a/extensions/chromium/runet-censorship-bypass/extension/11-error-handlers-api.js b/extensions/chromium/runet-censorship-bypass/extension/11-error-handlers-api.js index c8075c8..a0913d2 100644 --- a/extensions/chromium/runet-censorship-bypass/extension/11-error-handlers-api.js +++ b/extensions/chromium/runet-censorship-bypass/extension/11-error-handlers-api.js @@ -2,6 +2,8 @@ { // Private namespace + const chromified = window.utils.chromified; + function errorJsonReplacer(key, value) { // fooWindow.ErrorEvent !== barWindow.ErrorEvent @@ -63,7 +65,7 @@ state: window.utils.createStorage('handlers-'), - viewErrorVoid(type = window.utils.mandatory(), err) { + viewError(type = window.utils.mandatory(), err) { let errors = {}; if (err) { @@ -90,7 +92,7 @@ }, - switchVoid(onOffStr, eventName) { + switch(onOffStr, eventName) { if (!['on', 'off'].includes(onOffStr)) { throw new TypeError('First argument bust be "on" or "off".'); @@ -136,7 +138,7 @@ idToError: {}, - mayNotifyVoid( + mayNotify( id, title, errOrMessage, { icon = 'default-128.png', @@ -166,12 +168,12 @@ }, - installListenersOnAsync(win, name, cb) { + installListenersOn(win, name, cb) { win.addEventListener('error', (errEvent) => { console.warn(name + ':GLOBAL ERROR', errEvent); - this.mayNotifyVoid('ext-error', 'Ошибка расширения', errEvent, + this.mayNotify('ext-error', 'Ошибка расширения', errEvent, {icon: 'ext-error-128.png'}); }); @@ -186,7 +188,8 @@ }); if (cb) { - // setTimeout changes error context. + // In most cases getBackgroundPage( (bg) => installListenersOn + // Without setTimeout errors are swallowed, bug #357568 setTimeout(cb, 0); } @@ -201,10 +204,10 @@ chrome.proxy.settings.get( {}, - (details) => handlers.isControllable(details) + chromified((err, details) => handlers.isControllable(details)) ); - chrome.notifications.onClicked.addListener( function(notId) { + chrome.notifications.onClicked.addListener( chromified( (_, notId) => { chrome.notifications.clear(notId); if(notId === 'no-control') { @@ -212,13 +215,13 @@ window.utils.messages.searchSettingsForUrl('proxy') ); } - handlers.viewErrorVoid(notId); + handlers.viewError(notId); - }); + })); - handlers.installListenersOnAsync(window, 'BG'); + handlers.installListenersOn(window, 'BG'); - chrome.proxy.onProxyError.addListener((details) => { + chrome.proxy.onProxyError.addListener( chromified( (_, details) => { if (!handlers.ifControlled) { return; @@ -231,19 +234,19 @@ */ console.warn('PAC ERROR', details); // TOOD: add "view pac script at this line" button. - handlers.mayNotifyVoid('pac-error', 'Ошибка PAC!', + handlers.mayNotify('pac-error', 'Ошибка PAC!', details.error + '\n' + details.details, {icon: 'pac-error-128.png'} ); - }); + })); - chrome.proxy.settings.onChange.addListener((details) => { + chrome.proxy.settings.onChange.addListener( chromified((_, details) => { console.log('Proxy settings changed.', details); const noCon = 'no-control'; if ( !handlers.isControllable(details) ) { - handlers.mayNotifyVoid( + handlers.mayNotify( noCon, chrome.i18n.getMessage('noControl'), chrome.i18n.getMessage('which'), @@ -253,6 +256,6 @@ chrome.notifications.clear( noCon ); } - }); + })); } diff --git a/extensions/chromium/runet-censorship-bypass/extension/12-errors-lib.js b/extensions/chromium/runet-censorship-bypass/extension/12-errors-lib.js index 08338a3..063cfbd 100644 --- a/extensions/chromium/runet-censorship-bypass/extension/12-errors-lib.js +++ b/extensions/chromium/runet-censorship-bypass/extension/12-errors-lib.js @@ -48,7 +48,7 @@ clarifyThen: function(message, cb = mandatory()) { - return (err, ...args) => cb( clarify(err, message), ...args ); + return (err, ...args) => cb( self.clarify(err, message), ...args ); }, diff --git a/extensions/chromium/runet-censorship-bypass/extension/14-ip-to-host-api.js b/extensions/chromium/runet-censorship-bypass/extension/14-ip-to-host-api.js index 70559eb..ccd8c72 100644 --- a/extensions/chromium/runet-censorship-bypass/extension/14-ip-to-host-api.js +++ b/extensions/chromium/runet-censorship-bypass/extension/14-ip-to-host-api.js @@ -213,7 +213,6 @@ console.log('Purging old IPs for', hostStr); for(const ip of Object.keys(privates._ipToHostObj)) { if (hostStr === privates._ipToHostObj[ip].host) { - console.log('del', ip); delete privates._ipToHostObj[ip]; } } @@ -230,9 +229,7 @@ // Object may be shared, string can't. const hostObj = _getHostObj(hostStr); for(const ip of ips) { - console.log('IP', ip); privates._ipToHostObj[ip] = hostObj; - console.log(privates._ipToHostObj[ip], privates._ipToHostObj); } } return cb(err, null, ...warns); diff --git a/extensions/chromium/runet-censorship-bypass/extension/15-pac-kitchen-api.js b/extensions/chromium/runet-censorship-bypass/extension/15-pac-kitchen-api.js index 7f186e2..857c924 100644 --- a/extensions/chromium/runet-censorship-bypass/extension/15-pac-kitchen-api.js +++ b/extensions/chromium/runet-censorship-bypass/extension/15-pac-kitchen-api.js @@ -102,8 +102,7 @@ constructor(mods = {}) { const defaults = getDefaults(); - const ifAllDefaults = - Object.keys(defaults) + const ifAllDefaults = Object.keys(defaults) .every( (prop) => !(prop in mods) || Boolean(defaults[prop]) === Boolean(mods[prop]) @@ -257,7 +256,7 @@ details ? resolve(details) - : chrome.proxy.settings.get({}, resolve) + : chrome.proxy.settings.get({}, chromified( (_, res) => resolve(res))) ).then( (details) => { @@ -284,7 +283,7 @@ }, - checkIncontinenceVoid(details) { + checkIncontinence(details) { if ( kitchenState(ifIncontinence) ) { this._tryNowAsync(details, () => {/* Swallow. */}); @@ -295,7 +294,7 @@ keepCookedNowAsync(pacMods = mandatory(), cb = throwIfError) { - console.log('Keep cooked now...', cb); + console.log('Keep cooked now...', pacMods); if (typeof(pacMods) === 'function') { cb = pacMods; pacMods = this.getCurrentConfigs(); @@ -364,7 +363,11 @@ }; - pacKitchen.checkIncontinenceVoid(); - chrome.proxy.settings.onChange.addListener( pacKitchen.checkIncontinenceVoid.bind(pacKitchen) ); + pacKitchen.checkIncontinence(); + chrome.proxy.settings.onChange.addListener( + chromified( + (_, details) => pacKitchen.checkIncontinence(details) + ) + ); } // Private namespace ends. diff --git a/extensions/chromium/runet-censorship-bypass/extension/17-sync-pac-script-with-pac-provider-api.js b/extensions/chromium/runet-censorship-bypass/extension/17-sync-pac-script-with-pac-provider-api.js index e8f969b..dd7d91f 100644 --- a/extensions/chromium/runet-censorship-bypass/extension/17-sync-pac-script-with-pac-provider-api.js +++ b/extensions/chromium/runet-censorship-bypass/extension/17-sync-pac-script-with-pac-provider-api.js @@ -24,7 +24,6 @@ const mandatory = window.utils.mandatory; const throwIfError = window.utils.throwIfError; const chromified = window.utils.chromified; - const checkChromeError = window.utils.checkChromeError; const clarifyThen = window.apis.errorsLib.clarifyThen; const Warning = window.apis.errorsLib.Warning; @@ -60,13 +59,12 @@ }, }; console.log('Setting chrome proxy settings...'); - chrome.proxy.settings.set( {value: config}, () => { + chrome.proxy.settings.set( {value: config}, chromified((err) => { - const err = checkChromeError(); if (err) { return cb(err); } - chrome.proxy.settings.get({}, (details) => { + chrome.proxy.settings.get({}, chromified((_, details) => { if ( !window.utils.areSettingsControlledFor( details ) ) { @@ -78,9 +76,9 @@ } console.log('Successfuly set PAC in proxy settings..'); cb(); - }); + })); - }); + })); }; @@ -397,16 +395,15 @@ chrome.alarms.clearAll( () => chrome.proxy.settings.clear( {}, - () => { + chromified((err) => { - const err = checkChromeError(); if (err) { return cb(err); } this.setCurrentPacProviderKey(null); this.pushToStorageAsync(cb); - } + }) ) ); @@ -415,9 +412,8 @@ }; // ON EACH LAUNCH, STARTUP, RELOAD, UPDATE, ENABLE - chrome.storage.local.get(null, (oldStorage) => { + chrome.storage.local.get(null, chromified( (err, oldStorage) => { - const err = checkChromeError(); if (err) { throw err; } @@ -431,7 +427,7 @@ const antiCensorRu = window.apis.antiCensorRu; chrome.alarms.onAlarm.addListener( - (alarm) => { + chromified((_, alarm) => { if (alarm.name === antiCensorRu._periodicUpdateAlarmReason) { console.log( @@ -441,7 +437,7 @@ antiCensorRu.syncWithPacProviderAsync(() => {/* swallow */}); } - } + }) ); console.log('Alarm listener installed. We won\'t miss any PAC update.'); @@ -533,6 +529,6 @@ * Add storage.lastPacUpdateStamp. **/ - }); + })); } diff --git a/extensions/chromium/runet-censorship-bypass/extension/20-api-fixes.js b/extensions/chromium/runet-censorship-bypass/extension/20-api-fixes.js deleted file mode 100644 index cc24199..0000000 --- a/extensions/chromium/runet-censorship-bypass/extension/20-api-fixes.js +++ /dev/null @@ -1,27 +0,0 @@ -'use strict'; - -/* `setTimeout` changes context of execution from other window - (e.g. popup) to background window, so we may catch errors - in bg error handlers. - More: https://bugs.chromium.org/p/chromium/issues/detail?id=357568 - setTimeout is applied to Async/Void methods - only (name ends with Async/Void) -*/ -// Fix error context of methods of all APIs. - -for(const apiName of Object.keys(window.apis)) { - const api = window.apis[apiName]; - for(const prop of Object.keys(api)) { - const method = api[prop]; - if ( !( - typeof(api[prop]) === 'function' - && ['Async', 'Void'].some( (suff) => method.name.endsWith(suff) ) - ) - ) { - continue; - } - api[prop] = function(...args) { - setTimeout(method.bind(this, ...args), 0); - }; - } -} diff --git a/extensions/chromium/runet-censorship-bypass/extension/manifest.json b/extensions/chromium/runet-censorship-bypass/extension/manifest.json index 0e4ba90..1648f2a 100755 --- a/extensions/chromium/runet-censorship-bypass/extension/manifest.json +++ b/extensions/chromium/runet-censorship-bypass/extension/manifest.json @@ -31,7 +31,6 @@ "14-ip-to-host-api.js", "15-pac-kitchen-api.js", "17-sync-pac-script-with-pac-provider-api.js", - "20-api-fixes.js", "30-block-informer.js", "40-context-menus.js" ] diff --git a/extensions/chromium/runet-censorship-bypass/extension/pages/choose-pac-provider/index.js b/extensions/chromium/runet-censorship-bypass/extension/pages/choose-pac-provider/index.js index 7719d06..f21553f 100755 --- a/extensions/chromium/runet-censorship-bypass/extension/pages/choose-pac-provider/index.js +++ b/extensions/chromium/runet-censorship-bypass/extension/pages/choose-pac-provider/index.js @@ -10,7 +10,7 @@ document.getElementById('pac-mods').onchange = function() { }; chrome.runtime.getBackgroundPage( (backgroundPage) => - backgroundPage.apis.errorHandlers.installListenersOnAsync( + backgroundPage.apis.errorHandlers.installListenersOn( window, 'PUP', async() => { const getStatus = () => document.querySelector('#status'); @@ -119,7 +119,7 @@ chrome.runtime.getBackgroundPage( (backgroundPage) => if (err) { getStatus().querySelector('.link-button').onclick = function() { - errorHandlers.viewErrorVoid('pup-ext-err', err); + errorHandlers.viewError('pup-ext-err', err); return false; }; @@ -144,7 +144,6 @@ chrome.runtime.getBackgroundPage( (backgroundPage) => warns = warns.filter( (w) => w ); if (err || warns.length) { - backgroundPage.console.log('ERR', err, 'W', warns.length, 'w', warns); showErrors(err, ...warns); } else { setStatusTo(afterStatus); @@ -479,7 +478,7 @@ chrome.runtime.getBackgroundPage( (backgroundPage) => conduct( 'Применяем исключения...', (cb) => pacKitchen.keepCookedNowAsync(pacMods, cb), - 'Исключения применены.', + 'Исключения применены. Не забывайте о кэше!', () => { excList.childNodes.forEach( @@ -616,7 +615,7 @@ HTTPS 11.22.33.44:8080;">${conf.value || localStorage.getItem(uiRaw) || ''} - backgroundPage.apis.errorHandlers.installListenersOnAsync( + backgroundPage.apis.errorHandlers.installListenersOn( window, 'TRBL', () => { document.getElementById('reset-settings').onclick = () => { @@ -12,7 +12,7 @@ chrome.runtime.getBackgroundPage( (backgroundPage) => }; document.getElementById('view-errors').onclick = () => - backgroundPage.apis.errorHandlers.viewErrorVoid('all'); + backgroundPage.apis.errorHandlers.viewError('all'); }) );