From c19dc8952ce9b8741e63ac114b544e1eb5a08588 Mon Sep 17 00:00:00 2001 From: Christopher Tino Date: Fri, 29 Mar 2019 16:18:41 -0400 Subject: [PATCH 1/6] consolidate and refactor sendMessage methods --- app/Account/AccountReducer.js | 2 +- app/content-scripts/utils/msg.js | 75 ++----------------------------- app/panel/components/Rewards.jsx | 2 +- app/panel/components/Settings.jsx | 2 +- app/panel/utils/msg.js | 40 ++++++++++------- 5 files changed, 32 insertions(+), 89 deletions(-) diff --git a/app/Account/AccountReducer.js b/app/Account/AccountReducer.js index 5f1b81483..5468cf70e 100644 --- a/app/Account/AccountReducer.js +++ b/app/Account/AccountReducer.js @@ -34,7 +34,7 @@ export default (state = initialState, action) => { switch (action.type) { case UPDATE_PANEL_DATA: { const { account } = action.data; - if (account === null) { + if (!account) { return Object.assign({}, initialState); } const { diff --git a/app/content-scripts/utils/msg.js b/app/content-scripts/utils/msg.js index c4518502b..547202066 100644 --- a/app/content-scripts/utils/msg.js +++ b/app/content-scripts/utils/msg.js @@ -15,8 +15,8 @@ */ /* eslint no-use-before-define: 0 */ -import globals from '../../../src/classes/Globals'; import { log } from '../../../src/utils/common'; +import { sendMessageInPromise as panelSendPromiseMessage, sendMessage as panelSendMessage } from '../../panel/utils/msg'; /** * Message wrapper function @@ -24,9 +24,6 @@ import { log } from '../../../src/utils/common'; * @return {Object} set of APIs for handling messages */ export default function (origin) { - const IS_EDGE = (globals.BROWSER_INFO.name === 'edge'); - const { onMessage } = chrome.runtime; - /** * Send a message wrapped in a promise. * @memberOf ContentScriptUtils @@ -35,49 +32,8 @@ export default function (origin) { * @param {Object} message message data * @return {Promise} response or null */ - let MESSAGE_ID = 0; - let LISTENER_ADDED = false; function sendMessageInPromise(name, message) { - // On Edge 39.14965.1001.0 callback is not called when multiple - // Edge instances running. So instead we shoot message back - // from background. See onMessageHandler, HANDLE UNIVERSAL EVENTS HERE - // in src/background.js.To be removed, once Edge fixed. - if (IS_EDGE) { - return new Promise((resolve) => { - const messageId = MESSAGE_ID.toString(); - MESSAGE_ID++; - if (!LISTENER_ADDED) { - LISTENER_ADDED = true; - onMessage.addListener((request, sender, sendResponse) => { - if (messageId === request.name) { - resolve(request.message); - } - if (sendResponse) { - sendResponse(); - } - }); - } - chrome.runtime.sendMessage({ - name, - message, - origin, - messageId, - }, () => {}); - }); - } - return new Promise(((resolve) => { - chrome.runtime.sendMessage({ - name, - message, - origin, - }, (response) => { - if (chrome.runtime.lastError) { - log(chrome.runtime.lastError, origin, name, message); - resolve(null); - } - resolve(response); - }); - })); + panelSendPromiseMessage(name, message, origin); } /** @@ -91,7 +47,7 @@ export default function (origin) { */ function sendMessage(name, message, callback) { log(`origin ${origin} sending to handler`, name); - _sendMessageToHandler(name, origin, message, callback); + panelSendMessage(name, message, origin, callback); } /** @@ -105,30 +61,7 @@ export default function (origin) { */ function sendMessageToBackground(name, message, callback) { log(`origin ${origin} sending to background onMessageHandler`, name); - _sendMessageToHandler(name, '', message, callback); - } - - /** - * Send a message without an `origin` parameter. This will - * be picked up by the general onMessageHandler in src/background. - * - * @private - * - * @param {string} name message name - * @param {string} source message origin - * @param {Object} message message data - * @param {function} [callback] callback called by the message recipient - */ - function _sendMessageToHandler(name, source, message, callback = function () {}) { - log(`_sendMessageToHandler:${source} sending to background`, name); - // @EDGE chrome.runtime.sendMessage(message) works, but - // const callback; chrome.runtime.sendMessage(message, callback) fails to - // execute and chrome.runtime.lastError is undefined. - chrome.runtime.sendMessage({ - name, - message, - origin: source, // prevents eslint no-shadow - }, callback); + panelSendMessage(name, message, '', callback); } return { diff --git a/app/panel/components/Rewards.jsx b/app/panel/components/Rewards.jsx index 47cddfc46..d5df450fc 100644 --- a/app/panel/components/Rewards.jsx +++ b/app/panel/components/Rewards.jsx @@ -141,7 +141,7 @@ class Rewards extends React.Component { origin: 'rewards-hub', type: 'action-signal', }; - sendMessage('setPanelData', { enable_offers: !enable_offers, signal }, undefined, 'rewardsPanel'); + sendMessage('setPanelData', { enable_offers: !enable_offers, signal }, 'rewardsPanel'); sendMessage('ping', enable_offers ? 'rewards_on' : 'rewards_off'); // TODO catch } diff --git a/app/panel/components/Settings.jsx b/app/panel/components/Settings.jsx index d29b74477..bb6007b31 100644 --- a/app/panel/components/Settings.jsx +++ b/app/panel/components/Settings.jsx @@ -96,7 +96,7 @@ class Settings extends React.Component { origin: 'rewards-hub', type: 'action-signal', }; - sendMessage('setPanelData', { enable_offers: event.currentTarget.checked, signal }, undefined, 'rewardsPanel'); + sendMessage('setPanelData', { enable_offers: event.currentTarget.checked, signal }, 'rewardsPanel'); sendMessage('ping', event.currentTarget.checked ? 'rewards_on' : 'rewards_off'); } this.props.actions.toggleCheckbox({ diff --git a/app/panel/utils/msg.js b/app/panel/utils/msg.js index 4b121b862..bae29cba5 100644 --- a/app/panel/utils/msg.js +++ b/app/panel/utils/msg.js @@ -27,17 +27,18 @@ const IS_EDGE = (globals.BROWSER_INFO.name === 'edge'); * @return {Promise} */ let MESSAGE_ID = 0; -const LISTENER_ADDED = false; +let LISTENER_ADDED = false; export function sendMessageInPromise(name, message, origin = '') { // On Edge 39.14965.1001.0 callback is not called when multiple - // Edge instances running. So instead we shoot message back + // Edge instances are running. So instead we pass the message back // from background. See onMessageHandler, HANDLE UNIVERSAL EVENTS HERE - // in src/background.js.To be removed, once Edge fixed. + // in src/background.js. To be removed, once Edge is fixed. if (IS_EDGE) { return new Promise((resolve) => { const messageId = MESSAGE_ID.toString(); MESSAGE_ID++; if (!LISTENER_ADDED) { + LISTENER_ADDED = true; onMessage.addListener((request, sender, sendResponse) => { if (messageId === request.name) { resolve(request.message); @@ -63,7 +64,7 @@ export function sendMessageInPromise(name, message, origin = '') { }, (response) => { if (chrome.runtime.lastError) { log(chrome.runtime.lastError, name, message); - resolve(null); + resolve(false); } resolve(response); }); @@ -77,16 +78,16 @@ export function sendMessageInPromise(name, message, origin = '') { * * @param {string} name message name * @param {Object} message message data + * @param {string} origin message origin * @param {function} callback callback message * @return {Object} response * @todo runtime.sendMessage does not return any value. */ -export function sendMessage(name, message, callback = function () {}, origin = null) { +export function sendMessage(name, message, origin = '', callback = _defaultCallback()) { log('Panel sendMessage: sending to background', name); - // @EDGE chrome.runtime.sendMessage(message) works, but - // const callback; chrome.runtime.sendMessage(message, callback) fails to execute and chrome.runtime.lastError is undefined. - // const fallback = function () {}; // Workaround for Edge. callback cannot be undefined. - // callback = callback || fallback; + // @EDGE chrome.runtime.sendMessage(message) works, but the `callback` of + // chrome.runtime.sendMessage(message, callback) fails to + // execute and chrome.runtime.lastError is undefined. return chrome.runtime.sendMessage({ name, message, @@ -105,12 +106,8 @@ export function sendMessage(name, message, callback = function () {}, origin = n * @return {Object} response * @todo runtime.sendMessage does not return any value. */ -export function sendRewardMessage(name, message, callback = function () {}) { - log('Panel sendMessage: sending to background', name); - // @EDGE chrome.runtime.sendMessage(message) works, but - // const callback; chrome.runtime.sendMessage(message, callback) fails to execute and chrome.runtime.lastError is undefined. - // const fallback = function () {}; // Workaround for Edge. callback cannot be undefined. - // callback = callback || fallback; +export function sendRewardMessage(name, message, callback = _defaultCallback()) { + log('Panel sendRewardMessage: sending to background', name); return chrome.runtime.sendMessage({ name, message, @@ -139,3 +136,16 @@ export function openSupportPage() { sendMessage('account.openSupportPage'); window.close(); } + + +/** + * Default callback handler for sendMessage. Allows us to handle + * 'Unchecked runtime.lastError: The message port closed before a response was received' errors. + * This occurs when the `chrome.runtime.onmessage` handler returns `false` with no `callback()` + * but `chrome.runtime.sendMessage` has been passed a default callback. + */ +function _defaultCallback() { + if (chrome.runtime.lastError) { + log('defaultCallback error:', chrome.runtime.lastError); + } +} From a021659c65aa14fb6f25b816e06428a7a53f3a67 Mon Sep 17 00:00:00 2001 From: Christopher Tino Date: Fri, 29 Mar 2019 16:46:45 -0400 Subject: [PATCH 2/6] fix lint errors --- app/panel/utils/msg.js | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/app/panel/utils/msg.js b/app/panel/utils/msg.js index bae29cba5..b015c84cb 100644 --- a/app/panel/utils/msg.js +++ b/app/panel/utils/msg.js @@ -17,6 +17,18 @@ import { log } from '../../../src/utils/common'; const { onMessage } = chrome.runtime; const IS_EDGE = (globals.BROWSER_INFO.name === 'edge'); +/** + * Default callback handler for sendMessage. Allows us to handle + * 'Unchecked runtime.lastError: The message port closed before a response was received' errors. + * This occurs when the `chrome.runtime.onmessage` handler returns `false` with no `callback()` + * but `chrome.runtime.sendMessage` has been passed a default callback. + */ +const defaultCallback = function () { + if (chrome.runtime.lastError) { + log('defaultCallback error:', chrome.runtime.lastError); + } +}; + /** * Send a message to the handlers in src/background wrapped in a * promise. This should be used for messages that require a callback. @@ -83,7 +95,7 @@ export function sendMessageInPromise(name, message, origin = '') { * @return {Object} response * @todo runtime.sendMessage does not return any value. */ -export function sendMessage(name, message, origin = '', callback = _defaultCallback()) { +export function sendMessage(name, message, origin = '', callback = defaultCallback()) { log('Panel sendMessage: sending to background', name); // @EDGE chrome.runtime.sendMessage(message) works, but the `callback` of // chrome.runtime.sendMessage(message, callback) fails to @@ -106,7 +118,7 @@ export function sendMessage(name, message, origin = '', callback = _defaultCallb * @return {Object} response * @todo runtime.sendMessage does not return any value. */ -export function sendRewardMessage(name, message, callback = _defaultCallback()) { +export function sendRewardMessage(name, message, callback = defaultCallback()) { log('Panel sendRewardMessage: sending to background', name); return chrome.runtime.sendMessage({ name, @@ -136,16 +148,3 @@ export function openSupportPage() { sendMessage('account.openSupportPage'); window.close(); } - - -/** - * Default callback handler for sendMessage. Allows us to handle - * 'Unchecked runtime.lastError: The message port closed before a response was received' errors. - * This occurs when the `chrome.runtime.onmessage` handler returns `false` with no `callback()` - * but `chrome.runtime.sendMessage` has been passed a default callback. - */ -function _defaultCallback() { - if (chrome.runtime.lastError) { - log('defaultCallback error:', chrome.runtime.lastError); - } -} From 67c74d7332604980e6d5a816b9ec1cc7d91fa67b Mon Sep 17 00:00:00 2001 From: Christopher Tino Date: Fri, 29 Mar 2019 19:52:05 -0400 Subject: [PATCH 3/6] handle runtime lasterror on sendMessage calls --- app/content-scripts/notifications.js | 7 ++++++- app/panel/utils/msg.js | 8 ++++++-- src/classes/ExtMessenger.js | 6 +++++- src/utils/utils.js | 19 ++++++++++++++++--- 4 files changed, 33 insertions(+), 7 deletions(-) diff --git a/app/content-scripts/notifications.js b/app/content-scripts/notifications.js index 3fcd398c7..fd3d6e1a5 100644 --- a/app/content-scripts/notifications.js +++ b/app/content-scripts/notifications.js @@ -539,7 +539,12 @@ const NotificationsContentScript = (function (win, doc) { const fileReader = new FileReader(); fileReader.onload = (fileLoadedEvent) => { - const fallback = function () {}; // Workaround for Edge. callback cannot be undefined. + // Workaround for Edge. Callback cannot be undefined. + const fallback = () => { + if (chrome.runtime.lastError) { + log('showBrowseWindow error:', chrome.runtime.lastError); + } + }; chrome.runtime.sendMessage({ origin: 'notifications', name: 'importFile', diff --git a/app/panel/utils/msg.js b/app/panel/utils/msg.js index b015c84cb..c27ba5a76 100644 --- a/app/panel/utils/msg.js +++ b/app/panel/utils/msg.js @@ -23,7 +23,7 @@ const IS_EDGE = (globals.BROWSER_INFO.name === 'edge'); * This occurs when the `chrome.runtime.onmessage` handler returns `false` with no `callback()` * but `chrome.runtime.sendMessage` has been passed a default callback. */ -const defaultCallback = function () { +const defaultCallback = () => { if (chrome.runtime.lastError) { log('defaultCallback error:', chrome.runtime.lastError); } @@ -65,7 +65,11 @@ export function sendMessageInPromise(name, message, origin = '') { message, messageId, origin, - }, () => {}); + }, () => { + if (chrome.runtime.lastError) { + log('sendMessageInPromise error:', chrome.runtime.lastError); + } + }); }); } return new Promise(((resolve) => { diff --git a/src/classes/ExtMessenger.js b/src/classes/ExtMessenger.js index 188ea8308..42b554c68 100644 --- a/src/classes/ExtMessenger.js +++ b/src/classes/ExtMessenger.js @@ -28,7 +28,11 @@ export class ExtMessenger { } sendMessage(extensionId, message) { - chrome.runtime.sendMessage(extensionId, message, () => {}); + chrome.runtime.sendMessage(extensionId, message, () => { + if (chrome.runtime.lastError) { + log('ExtMessenger sendMessage error:', chrome.runtime.lastError); + } + }); } } diff --git a/src/utils/utils.js b/src/utils/utils.js index 7b3a1626f..124aebea7 100644 --- a/src/utils/utils.js +++ b/src/utils/utils.js @@ -27,6 +27,15 @@ import { log, objectEntries } from './common'; const { BROWSER_INFO } = globals; const IS_FIREFOX = (BROWSER_INFO.name === 'firefox'); +/** + * Handle chrome.runtime.lastError messages + */ +const defaultCallback = () => { + if (chrome.runtime.lastError) { + log('defaultCallback error:', chrome.runtime.lastError); + } +}; + /** * Send message to a specific tab ID. * @memberOf BackgroundUtils @@ -36,7 +45,7 @@ const IS_FIREFOX = (BROWSER_INFO.name === 'firefox'); * @param {Object} message message data * @param {function} callback function to call (at most once) when you have a response */ -export function sendMessage(tab_id, name, message, callback = function () {}) { +export function sendMessage(tab_id, name, message, callback = defaultCallback()) { log(`BACKGROUND SENT ${name} TO TAB`); chrome.tabs.sendMessage(tab_id, { name, @@ -54,7 +63,7 @@ export function sendMessage(tab_id, name, message, callback = function () {}) { * @param {Object} message message data * @param {function} callback function to call (at most once) when you have a response */ -export function sendMessageToFrame(tab_id, frame_id, name, message, callback = function () {}) { +export function sendMessageToFrame(tab_id, frame_id, name, message, callback = defaultCallback()) { log(`BACKGROUND SENT ${name} TO TAB ${tab_id} - FRAME ${frame_id}`); chrome.tabs.sendMessage(tab_id, { name, @@ -72,7 +81,11 @@ export function sendMessageToFrame(tab_id, frame_id, name, message, callback = f */ export function sendMessageToPanel(name, message) { log('BACKGROUND SENDS MESSAGE TO PANEL', name); - chrome.runtime.sendMessage({ name, message }); + chrome.runtime.sendMessage({ name, message }, () => { + if (chrome.runtime.lastError) { + log('sendMessageToPanel error:', chrome.runtime.lastError); + } + }); } /** From 61c2b8b4e0e330f7e0d90ded513557198843db84 Mon Sep 17 00:00:00 2001 From: Christopher Tino Date: Mon, 1 Apr 2019 10:54:27 -0400 Subject: [PATCH 4/6] update browser core --- package.json | 2 +- yarn.lock | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 1bf0ca3dc..be572fd66 100644 --- a/package.json +++ b/package.json @@ -47,7 +47,7 @@ "dependencies": { "@cliqz/adblocker": "0.3.1", "base64-js": "^1.2.1", - "browser-core": "https://s3.amazonaws.com/cdncliqz/update/edge/ghostery/v7.35/7.35.1.8ec615c.tgz", + "browser-core": "https://s3.amazonaws.com/cdncliqz/update/edge/ghostery/v7.35/7.35.1.acd5469.tgz", "classnames": "^2.2.5", "d3": "^5.7.0", "foundation-sites": "^6.4.4-rc1", diff --git a/yarn.lock b/yarn.lock index b2687861d..5f986acb8 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1259,9 +1259,9 @@ brorand@^1.0.1: resolved "https://registry.yarnpkg.com/brorand/-/brorand-1.1.0.tgz#12c25efe40a45e3c323eb8675a0a0ce57b22371f" integrity sha1-EsJe/kCkXjwyPrhnWgoM5XsiNx8= -"browser-core@https://s3.amazonaws.com/cdncliqz/update/edge/ghostery/v7.35/7.35.1.8ec615c.tgz": +"browser-core@https://s3.amazonaws.com/cdncliqz/update/edge/ghostery/v7.35/7.35.1.acd5469.tgz": version "7.35.1" - resolved "https://s3.amazonaws.com/cdncliqz/update/edge/ghostery/v7.35/7.35.1.8ec615c.tgz#f3af23b470490c5eac352a74b468bca3817f3416" + resolved "https://s3.amazonaws.com/cdncliqz/update/edge/ghostery/v7.35/7.35.1.acd5469.tgz#758f63214056b5e133bfc1c8a007a0d22c1f28c5" dependencies: "@cliqz-oss/dexie" "^2.0.4" "@cliqz/adblocker" "^0.6.8" From 15a4a1c7a8ac1f2866b7273c95596cabfaae92b5 Mon Sep 17 00:00:00 2001 From: Christopher Tino Date: Mon, 1 Apr 2019 12:48:00 -0400 Subject: [PATCH 5/6] update readme #341 --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2c4225726..9adf50fbf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,9 +1,13 @@ ### GHOSTERY 8.3.2 (UNRELEASED) ++ Ghostery tracker panel now updates dynamically in real time! + Remove unsupported file types for Opera automated-review + Removed uneccesary files for slimmer production build + Sync account creation UI betweem intro hub, panel and signon-web + Performance improvments when browsing certian Google sites (gmail, maps) ++ Feature parity for Edge browser (Human Web, Rewards) ++ Clean up various errors thrown by content scripts and message handlers ++ Updated Readme (team members and open-source projects) + Minor UI tweaks ### GHOSTERY 8.3.1 (January 31, 2019) From 7f602af333135695d99b5b60e61f44dbc0d16f39 Mon Sep 17 00:00:00 2001 From: Christopher Tino Date: Tue, 2 Apr 2019 11:07:14 -0400 Subject: [PATCH 6/6] revert falsey check on account reducer --- app/Account/AccountReducer.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Account/AccountReducer.js b/app/Account/AccountReducer.js index 5468cf70e..5f1b81483 100644 --- a/app/Account/AccountReducer.js +++ b/app/Account/AccountReducer.js @@ -34,7 +34,7 @@ export default (state = initialState, action) => { switch (action.type) { case UPDATE_PANEL_DATA: { const { account } = action.data; - if (!account) { + if (account === null) { return Object.assign({}, initialState); } const {