From a2330104b6a4514b65cd035a2eaf7bd7d0fd2ad1 Mon Sep 17 00:00:00 2001 From: Juan Tejada Date: Tue, 19 Oct 2021 16:41:37 -0400 Subject: [PATCH 1/2] Dev Tools: Relax constraint on passing extensionId for backend init --- packages/react-devtools-extensions/src/injectGlobalHook.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/react-devtools-extensions/src/injectGlobalHook.js b/packages/react-devtools-extensions/src/injectGlobalHook.js index 02d5109e291eb..a5d96966c7e10 100644 --- a/packages/react-devtools-extensions/src/injectGlobalHook.js +++ b/packages/react-devtools-extensions/src/injectGlobalHook.js @@ -31,12 +31,14 @@ window.addEventListener('message', function onMessage({data, source}) { if (source !== window || !data) { return; } - if (data.extensionId !== CURRENT_EXTENSION_ID) { + if (data.extensionId != null && data.extensionId !== CURRENT_EXTENSION_ID) { if (__DEBUG__) { console.log( `[injectGlobalHook] Received message '${data.source}' from different extension instance. Skipping message.`, { currentExtension: EXTENSION_INSTALLATION_TYPE, + currentExtensionId: CURRENT_EXTENSION_ID, + providedExtensionId: data.extensionId, }, ); } From 5760cefdba71eeab4f0dcec4f6d7f0282af4798d Mon Sep 17 00:00:00 2001 From: Juan Tejada Date: Wed, 20 Oct 2021 09:24:02 -0400 Subject: [PATCH 2/2] Add extensionId to other postMessage calls This is technically not necessary because injectGlobalHook is not processing these messages, however the listener in injectGlobalHook does get called with these messages regardless. For consistency it seems like we should add them --- packages/react-devtools-extensions/src/backend.js | 8 +++++--- packages/react-devtools-extensions/src/contentScript.js | 5 +++++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/packages/react-devtools-extensions/src/backend.js b/packages/react-devtools-extensions/src/backend.js index aa8578a108646..5b6821bbba038 100644 --- a/packages/react-devtools-extensions/src/backend.js +++ b/packages/react-devtools-extensions/src/backend.js @@ -2,7 +2,7 @@ // Running module factories is intentionally delayed until we know the hook exists. // This is to avoid issues like: https://github.com/facebook/react-devtools/issues/1039 -/** @flow */ +// @flow strict-local 'use strict'; @@ -13,15 +13,16 @@ function welcome(event) { ) { return; } + const extensionId = event.data.extensionId; window.removeEventListener('message', welcome); - setup(window.__REACT_DEVTOOLS_GLOBAL_HOOK__); + setup(window.__REACT_DEVTOOLS_GLOBAL_HOOK__, extensionId); } window.addEventListener('message', welcome); -function setup(hook) { +function setup(hook, extensionId) { if (hook == null) { // DevTools didn't get injected into this page (maybe b'c of the contentType). return; @@ -55,6 +56,7 @@ function setup(hook) { { source: 'react-devtools-bridge', payload: {event, payload}, + extensionId, }, '*', transferable, diff --git a/packages/react-devtools-extensions/src/contentScript.js b/packages/react-devtools-extensions/src/contentScript.js index c914c6e7b3dfc..179959f7e01ec 100644 --- a/packages/react-devtools-extensions/src/contentScript.js +++ b/packages/react-devtools-extensions/src/contentScript.js @@ -2,6 +2,8 @@ 'use strict'; +import {CURRENT_EXTENSION_ID} from './constants'; + let backendDisconnected: boolean = false; let backendInitialized: boolean = false; @@ -10,6 +12,7 @@ function sayHelloToBackend() { { source: 'react-devtools-content-script', hello: true, + extensionId: CURRENT_EXTENSION_ID, }, '*', ); @@ -20,6 +23,7 @@ function handleMessageFromDevtools(message) { { source: 'react-devtools-content-script', payload: message, + extensionId: CURRENT_EXTENSION_ID, }, '*', ); @@ -49,6 +53,7 @@ function handleDisconnect() { type: 'event', event: 'shutdown', }, + extensionId: CURRENT_EXTENSION_ID, }, '*', );