Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-1644: Fix Unchecked runtime.lastError in message handler #362

Merged
merged 7 commits into from Apr 2, 2019
handle runtime lasterror on sendMessage calls
  • Loading branch information
christophertino committed Mar 29, 2019
commit 67c74d7332604980e6d5a816b9ec1cc7d91fa67b
@@ -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',
@@ -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) => {
@@ -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);
}
});
}
}

@@ -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);
}
});
}

/**
ProTip! Use n and p to navigate between commits in a pull request.