Skip to content

Commit

Permalink
Add support for frames to TabManager & TabTracker
Browse files Browse the repository at this point in the history
  • Loading branch information
Joris-van-der-Wel committed Aug 20, 2018
1 parent e13258a commit b170728
Show file tree
Hide file tree
Showing 14 changed files with 469 additions and 166 deletions.
20 changes: 18 additions & 2 deletions lib/WaitForEvent.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
'use strict';
const DeepStore = require('deep-store');

const NOOP = () => {};

class WaitForEvent {
constructor() {
this._pendingWaits = new Map();
this._pendingWaits = new DeepStore();
Object.freeze(this);
}

/**
* @param {Array} key
* @param {Function} func
*/
async wait(key, func = NOOP) {
{
const waitData = this._pendingWaits.get(key);
Expand Down Expand Up @@ -43,10 +49,20 @@ class WaitForEvent {
return true;
}

resolve(key, value) {
/**
* @param {Array} key
* @param {*} value
* @return {Boolean}
*/
resolve(key, value = undefined) {
return this._fulfill(false, key, value);
}

/**
* @param {Array} key
* @param {Error} error
* @return {Boolean}
*/
reject(key, error) {
return this._fulfill(true, key, error);
}
Expand Down
8 changes: 6 additions & 2 deletions lib/contentRpc/TabContentRPC.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const log = require('../logger')({hostname: 'background', MODULE: 'TabContentRPC

const DEFAULT_CALL_TIMEOUT = 15003;
const NOOP = () => {};
const promiseTry = async fn => fn();

class TabContentRPCTab {
constructor(browserTabId, browserFrameId, tabContentRPC) {
Expand All @@ -15,6 +16,7 @@ class TabContentRPCTab {
this._methods = new MethodRegistrations();
this._tabContentRPC = tabContentRPC;
this._destroyed = false;
this._initializePromise = null;
Object.seal(this);
}

Expand Down Expand Up @@ -172,7 +174,7 @@ class TabContentRPC {

const {id: browserTabId} = tab;
const rpc = this.get(browserTabId, browserFrameId);
return Promise.resolve().then(() => rpc._handle(message));
return rpc._initializePromise.then(() => rpc._handle(message));
}

handleTabsRemoved(browserTabId, removeInfo) {
Expand Down Expand Up @@ -202,7 +204,9 @@ class TabContentRPC {

const rpc = new TabContentRPCTab(browserTabId, browserFrameId, this);
this.rpcMap.get(browserTabId).set(browserFrameId, rpc);
this.onRpcInitialize({browserTabId, browserFrameId, rpc});
rpc._initializePromise = promiseTry(() => this.onRpcInitialize({browserTabId, browserFrameId, rpc}))
.catch(err => log.error({err}, 'Error while calling onRpcInitialize'))
.then(() => undefined);
return rpc;
}

Expand Down
4 changes: 2 additions & 2 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@
"matches": [
"*://*/*"
],
"all_frames": false,
"all_frames": true,
"js": [
"build/tabs-content.js"
],
"run_at": "document_start"
}
]
}
}
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
"chai-dom": "^1.5.0",
"chai-subset": "^1.6.0",
"cjson": "^0.5.0",
"deep-store": "1.1.0",
"error-stack-parser": "^2.0.1",
"fs-extra": "^7.0.0",
"istanbul-lib-instrument": "^1.9.1",
Expand All @@ -83,6 +84,7 @@
"react-dom": "^16.0.0",
"shortid": "^2.2.8",
"split": "^1.0.1",
"symbol-tree": "^3.2.2",
"ws": "^6.0.0",
"yargs": "^12.0.1"
},
Expand Down
18 changes: 18 additions & 0 deletions runner-modules/runResult/lib/Event.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ class Event {
timing: new TimePeriod(),
type: String(type),
tabId: null,
frameId: null,
tabContentId: null,
});
Object.freeze(this);
Expand Down Expand Up @@ -286,6 +287,22 @@ class Event {
this[PRIVATE].tabId = value ? String(value) : null;
}

/**
*
* @return {?number}
*/
get frameId() {
return this[PRIVATE].frameId;
}

/**
*
* @param {?number} value
*/
set frameId(value) {
this[PRIVATE].frameId = value ? Number(value) : null;
}

/**
*
* @return {?string}
Expand Down Expand Up @@ -333,6 +350,7 @@ class Event {
metaData: metaData,
children: [...this.children].map(event => event.toJSONObject()),
tabId: this.tabId,
frameId: this.frameId,
tabContentId: this.tabContentId,
};
/* eslint-enable sort-keys */
Expand Down
3 changes: 2 additions & 1 deletion runner-modules/runResult/lib/background/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ TimePoint.setCounterFunc(() => ({
}));

module.exports = script => {
const handleTabsInitializedTabRpc = ({tab, rpc}) => {
const handleTabsInitializedTabRpc = ({tab, frame, rpc}) => {
rpc.method('runResult.scriptResult', result => {
try {
log.debug('Received script result object from content');
for (const event of result.events) {
event.tabId = tab.id;
event.frameId = frame.browserFrameId;
event.tabContentId = tab.currentContentId;
}
scriptResult.mergeJSONObject(result);
Expand Down
6 changes: 3 additions & 3 deletions runner-modules/tabs/lib/background/ScriptWindow.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class ScriptWindow extends EventEmitter {
this.openPromise = null;
this.firstTabCreation = true;
this.closed = false;
this._navigationCompletedWait = new WaitForEvent(); // key is the browserTabId
this._navigationCompletedWait = new WaitForEvent(); // key is [browserTabId]
this._sizeMinusViewport = Object.freeze({width: 0, height: 0});
this.handleWebNavigationCompleted = this.handleWebNavigationCompleted.bind(this);
Object.seal(this);
Expand All @@ -39,7 +39,7 @@ class ScriptWindow extends EventEmitter {
return;
}

this._navigationCompletedWait.resolve(browserTabId);
this._navigationCompletedWait.resolve([browserTabId]);
}
catch (err) {
log.error({err}, 'Error in browser.webNavigation.onCompleted');
Expand Down Expand Up @@ -82,7 +82,7 @@ class ScriptWindow extends EventEmitter {
*/
async _gatherBrowserWindowDetails(browserWindowId, firstTabId) {
// navigate to blank.html and wait for the load event
await this._navigationCompletedWait.wait(firstTabId, async () => {
await this._navigationCompletedWait.wait([firstTabId], async () => {
await this.browserTabs.update(firstTabId, {url: BLANK_HTML});
});

Expand Down
Loading

0 comments on commit b170728

Please sign in to comment.