1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00
metamask-extension/shared/modules/browser-runtime.utils.js

56 lines
1.5 KiB
JavaScript
Raw Normal View History

MV3: Update service worker restart logic and keep-alive logic for dapp support (#16075) * dapp: add debug statements * dapp: add retry logic [debug] * dapp: keep SW alive on rpc request * Revert "dapp: add debug statements" This reverts commit ea21786f7f66c712eea02405cd68fe925d227ffa. * dapp: try to set up ext streams asap on reset * dapp: apply keep alive logic to phishingPageStream * dapp:put keep-alive logic behind isManifestV3 flag * Re-activate streams after a period of service worker in-activity * dapp: rm extra function * dapp: update phishing onDisconnect * dapp: fix eslint missing global chrome * add EXTENSION_MESSAGES const * use EXTENSION_MESSAGES more generic comment * update comment * dapp: clean timeout and interval * Fix DAPP action replay * execute DAPP action replay for only MV3 * fix * fix * fix * comment out DAPP action replay code * fix * fix * fix * scripts/background: use browser polyfill * Revert "scripts/background: use browser polyfill" This reverts commit 2ab6234d11b3b11e10dd993d454eeaad63bfc886. * scripts/background: use browser polyfill * script/background: check lastError * dapp: use EXTENSION_MESSAGES * scripts/background: send ready msg to all tabs * dapp: update onMessage handler comment and name * dapp: return values onMessage listener see: https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/runtime/onMessage#addlistener_syntax * dapp: mv onMessage listener * dapp: add onMessage setupPhishingExtStreams * dapp: rn reset -> destroy streams * dapp: rn reset -> destroy for phishing streams * dapp: clean comment * dapp: rm unused comments planning to be readded in follow-up PR: #16250 * dapp: onMessage return Promise|undefined * dapp:clean: add missing undefined return type * dapp: use new checkForErrorAndLog for Chrome API handy stackoverflow: https://stackoverflow.com/a/28432087/4053142 * dapp:fix: return tabs.query result * dapp:eslint: return undefined fix Expected to return a value at the end of arrow function.eslintconsistent-return * background: do not query tabs w/out url * background: rm Could not establish... catch - no longer needed after improved tabs query * dapp:clean: rm unused checkForError... for now... * dapp: prevent setupExtensionStreams called twice - calling connect will trigger disconnect and may cause issues - only setup streams if they are not connected * dapp: handle onDisconnect lastError - throwing errors from contentscript will break the dapp, so only warn - not handling lastError when it's found will also break the dapp * background: update tabs.query url comment * background: update tabs.query url comment 2 * dapp: fix SW restart for multi dapp support - ref: https://stackoverflow.com/a/54686484/4053142 * dapp:clean: rm extra "." from console.warn * clean: comments for dapp and background * Adding catch block (#16454) * fix: FireFox provider injection * lib/util: fix invalid checkForErrorAndWarn export * bg: add explanation for tabs.sendMessage catch * dapp: add browser-runtime.utils * runtime.utils: add checkForLastErrorAndLog Co-authored-by: Jyoti Puri <jyotipuri@gmail.com> Co-authored-by: legobeat <109787230+legobeat@users.noreply.github.com>
2022-11-14 18:18:08 +01:00
/**
* Utility Functions to support browser.runtime JavaScript API
*/
import browser from 'webextension-polyfill';
import log from 'loglevel';
/**
* Returns an Error if extension.runtime.lastError is present
* this is a workaround for the non-standard error object that's used
*
* According to the docs, we are expected to check lastError in runtime API callbacks:
* "
* If you call an asynchronous function that may set lastError, you are expected to
* check for the error when you handle the result of the function. If lastError has been
* set and you don't check it within the callback function, then an error will be raised.
* "
*
* @see {@link https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/runtime/lastError}
* @returns {Error|undefined}
*/
export function checkForLastError() {
const { lastError } = browser.runtime;
if (!lastError) {
return undefined;
}
// if it quacks like an Error, its an Error
if (lastError.stack && lastError.message) {
return lastError;
}
// repair incomplete error object (eg chromium v77)
return new Error(lastError.message);
}
/** @returns {Error|undefined} */
export function checkForLastErrorAndLog() {
const error = checkForLastError();
if (error) {
log.error(error);
}
return error;
}
/** @returns {Error|undefined} */
export function checkForLastErrorAndWarn() {
const error = checkForLastError();
if (error) {
console.warn(error);
}
return error;
}