|
| 1 | +// offscreen-tab-opener.js |
| 2 | + |
1 | 3 | import{ |
2 | | -KnownOrigins, |
3 | | -OffscreenCommunicationTarget, |
| 4 | +KnownOrigins, |
| 5 | +OffscreenCommunicationTarget, |
4 | 6 | }from'../../shared/constants/offscreen-communication'; |
5 | 7 |
|
6 | | -asyncfunctionopenConnectorTab(url:string){ |
7 | | -constbrowserTab=window.open(url); |
8 | | -if(!browserTab){ |
9 | | -thrownewError('Failed to open Lattice connector.'); |
10 | | -} |
11 | | - |
12 | | -returnbrowserTab; |
13 | | -} |
14 | | - |
15 | | -exportdefaultfunctioninit(){ |
16 | | -/** |
17 | | - * The main element of the eth-lattice-keyring library that is impacted by MV3 |
18 | | - * is the _openConnectorTab method which is responsible for opening a tab to |
19 | | - * the lattice connector. This method is called by the _getCreds method of the |
20 | | - * keyring. In the default keyring this would be attempted inside the service |
21 | | - * worker which has no DOM and therefore would fail. The solution is to split |
22 | | - * the functionality so that the openConnectorTab portion operates inside the |
23 | | - * offscreen document with its DOM. That is what this file is responsible for. |
24 | | - * |
25 | | - * When receiving a message from the service worker script targeting the |
26 | | - * lattice iframe, this listener will execute and open the new tab for |
27 | | - * connecting to the lattice device. The response from the lattice connector |
28 | | - * is then sent back to the offscreen bridge for lattice, which extends from |
29 | | - * the eth-lattice-keyring Keyring class. |
30 | | - */ |
31 | | -chrome.runtime.onMessage.addListener((msg,_sender,sendResponse)=>{ |
32 | | -if(msg.target!==OffscreenCommunicationTarget.latticeOffscreen){ |
33 | | -return; |
| 8 | +/** |
| 9 | + *@private |
| 10 | + * Attempts to open a new browser tab for the Lattice connector URL. |
| 11 | + * Throws an error if the tab opening is blocked (e.g., by a pop-up blocker). |
| 12 | + *@param {string} url - The URL of the Lattice connector page. |
| 13 | + *@returns {Promise<Window>} The newly opened browser tab window object. |
| 14 | + */ |
| 15 | +asyncfunctionopenConnectorTab(url:string):Promise<Window>{ |
| 16 | +constbrowserTab=window.open(url); |
| 17 | +if(!browserTab){ |
| 18 | +thrownewError('Failed to open Lattice connector. Check for pop-up blockers.'); |
34 | 19 | } |
| 20 | +returnbrowserTab; |
| 21 | +} |
35 | 22 |
|
36 | | -// Open the tab |
37 | | -openConnectorTab(msg.params.url).then((browserTab)=>{ |
38 | | -// Watch for the open window closing before creds are sent back |
39 | | -constlistenInterval=setInterval(()=>{ |
40 | | -if(browserTab.closed){ |
41 | | -clearInterval(listenInterval); |
42 | | -sendResponse({ |
43 | | -error:newError('Lattice connector closed.'), |
44 | | -}); |
| 23 | +/** |
| 24 | + *@public |
| 25 | + * Initializes the message listener for the Offscreen Document. |
| 26 | + * This listener handles requests from the Service Worker to open the Lattice connection tab, |
| 27 | + * listens for the response (creds) from that tab, and forwards the result back. |
| 28 | + */ |
| 29 | +exportdefaultfunctioninit():void{ |
| 30 | +chrome.runtime.onMessage.addListener((msg,_sender,sendResponse)=>{ |
| 31 | +// 1. Filter: Only process messages targeting this offscreen document. |
| 32 | +if(msg.target!==OffscreenCommunicationTarget.latticeOffscreen){ |
| 33 | +returnfalse;// Return false if we haven't processed the message. |
45 | 34 | } |
46 | | -},500); |
47 | 35 |
|
48 | | -// On a Chromium browser we can just listen for a window message from the |
49 | | -// lattice tab (using the event.origin property). When we get that it'll be |
50 | | -// the response from the lattice connector with the deviceID and password. |
51 | | -// We can then forward that response to the lattice-offscreen-keyring's |
52 | | -// _getCreds method using sendResponse API from the chrome runtime. |
53 | | -window.addEventListener( |
54 | | -'message', |
55 | | -(event)=>{ |
56 | | -// Ensure origin, source is the browser tab, and data is a string (JSON to be parsed) |
57 | | -if( |
58 | | -event.origin!==KnownOrigins.lattice|| |
59 | | -event.source!==browserTab|| |
60 | | -typeofevent.data!=='string' |
61 | | -){ |
62 | | -return; |
63 | | -} |
| 36 | +// Open the tab and start the monitoring process asynchronously. |
| 37 | +openConnectorTab(msg.params.url) |
| 38 | +.then((browserTab)=>{ |
| 39 | +letlistenInterval:number; |
| 40 | +letonMessageListener:(event:MessageEvent)=>void; |
64 | 41 |
|
65 | | -try{ |
66 | | -// Stop the listener |
67 | | -clearInterval(listenInterval); |
| 42 | +/** |
| 43 | + * Common cleanup function to stop the interval and remove the event listener. |
| 44 | + */ |
| 45 | +constcleanupAndRespond=(response:{error?:Error|string,result?:any})=>{ |
| 46 | +clearInterval(listenInterval); |
| 47 | +if(onMessageListener){ |
| 48 | +window.removeEventListener('message',onMessageListener,false); |
| 49 | +} |
| 50 | +// Serialize Error object to string message for reliable transmission |
| 51 | +if(response.error&&typeofresponse.error!=='string'){ |
| 52 | +response.error=response.error.message; |
| 53 | +} |
| 54 | +sendResponse(response); |
| 55 | +}; |
68 | 56 |
|
69 | | -// Parse and return creds |
70 | | -constcreds=JSON.parse(event.data); |
71 | | -if(!creds.deviceID||!creds.password){ |
72 | | -sendResponse({ |
73 | | -error:newError('Invalid credentials returned from Lattice.'), |
74 | | -}); |
75 | | -} |
76 | | -sendResponse({ |
77 | | -result:creds, |
78 | | -}); |
79 | | -}catch(err){ |
80 | | -console.error('Error parsing Lattice credentials',err); |
81 | | -sendResponse({ |
82 | | -error:err, |
83 | | -}); |
84 | | -} |
85 | | -}, |
86 | | -false, |
87 | | -); |
88 | | -}); |
| 57 | +// 2. Start checking if the window is closed by the user. |
| 58 | +listenInterval=window.setInterval(()=>{ |
| 59 | +if(browserTab.closed){ |
| 60 | +cleanupAndRespond({error:'Lattice connector closed by user.'}); |
| 61 | +} |
| 62 | +},500); |
89 | 63 |
|
90 | | -// eslint-disable-next-line consistent-return |
91 | | -returntrue; |
92 | | -}); |
93 | | -} |
| 64 | +// 3. Define and register the message listener for the credentials. |
| 65 | +onMessageListener=(event:MessageEvent)=>{ |
| 66 | +// Security check: Verify origin, source, and data type. |
| 67 | +if( |
| 68 | +event.origin!==KnownOrigins.lattice|| |
| 69 | +event.source!==browserTab|| |
| 70 | +typeofevent.data!=='string' |
| 71 | +){ |
| 72 | +return;// Ignore non-Lattice or malformed messages. |
| 73 | +} |
| 74 | + |
| 75 | +try{ |
| 76 | +constcreds=JSON.parse(event.data); |
| 77 | + |
| 78 | +if(!creds||!creds.deviceID||!creds.password){ |
| 79 | +// Invalid data structure received. |
| 80 | +thrownewError('Invalid credentials structure returned from Lattice.'); |
| 81 | +} |
| 82 | + |
| 83 | +// Success: Cleanup and send result back to the Service Worker. |
| 84 | +cleanupAndRespond({result:creds}); |
| 85 | + |
| 86 | +}catch(error){ |
| 87 | +console.error('Error processing Lattice credentials:',error); |
| 88 | +// Failure: Cleanup and send error back. |
| 89 | +cleanupAndRespond({error:(errorasError).message||'Unknown error parsing credentials.'}); |
| 90 | +} |
| 91 | +}; |
| 92 | + |
| 93 | +window.addEventListener('message',onMessageListener,false); |
| 94 | +}) |
| 95 | +.catch((error)=>{ |
| 96 | +// Handle tab opening failure (e.g., pop-up |