Skip to content

Commit

Permalink
cleanup of extension driver attachment and event registration
Browse files Browse the repository at this point in the history
  • Loading branch information
brendankenny committed Jul 30, 2016
1 parent 07b3ccb commit 69622b8
Showing 1 changed file with 45 additions and 53 deletions.
98 changes: 45 additions & 53 deletions lighthouse-core/gather/drivers/extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,39 +27,69 @@ class ExtensionDriver extends Driver {
constructor() {
super();
this._tabId = null;
this._debuggerConnected = false;
this.handleUnexpectedDetach();

this._eventEmitter = new EventEmitter();
chrome.debugger.onEvent.addListener((_, method, params) =>
this._eventEmitter.emit(method, params));
this._onEvent = this._onEvent.bind(this);
this._onUnexpectedDetach = this._onUnexpectedDetach.bind(this);
}

_onEvent(source, method, params) {
// log events received
log.log('<=', method, params);

this._eventEmitter.emit(method, params);
}

_onUnexpectedDetach(debuggee, detachReason) {
this._detachCleanup();
throw new Error('Lighthouse detached from browser: ' + detachReason);
}

_detachCleanup() {
this._tabId = null;
this.url = null;
chrome.debugger.onEvent.removeListener(this._onEvent);
chrome.debugger.onDetach.removeListener(this._onUnexpectedDetach);
this._eventEmitter.removeAllListeners();
this._eventEmitter = null;
}

connect() {
if (this._debuggerConnected) {
if (this._tabId !== null) {
return Promise.resolve();
}

return this.queryCurrentTab_()
.then(tabId => {
this._tabId = tabId;
this.beginLogging();
return this.attachDebugger_(tabId)
.then(_ => this.enableRuntimeEvents());
});
chrome.debugger.onEvent.addListener(this._onEvent);
chrome.debugger.onDetach.addListener(this._onUnexpectedDetach);

return new Promise((resolve, reject) => {
chrome.debugger.attach({tabId}, '1.1', _ => {
if (chrome.runtime.lastError) {
return reject(chrome.runtime.lastError);
}
resolve(tabId);
});
});
}).then(_ => this.enableRuntimeEvents());
}

disconnect() {
if (this._tabId === null) {
return Promise.resolve();
}

return this.detachDebugger_(this._tabId)
.then(_ => {
this._tabId = null;
this.url = null;
this._debuggerConnected = false;
});
const tabId = this._tabId;
return new Promise((resolve, reject) => {
chrome.debugger.detach({tabId}, _ => {
if (chrome.runtime.lastError) {
return reject(chrome.runtime.lastError);
}
resolve();
});
}).then(_ => this._detachCleanup());
}

reloadForCleanStateIfNeeded(options) {
Expand All @@ -71,13 +101,6 @@ class ExtensionDriver extends Driver {
});
}

beginLogging() {
// log events received
chrome.debugger.onEvent.addListener((source, method, params) =>
log.log('<=', method, params)
);
}

/**
* Call protocol methods
* @param {!string} command
Expand Down Expand Up @@ -132,37 +155,6 @@ class ExtensionDriver extends Driver {

return Promise.resolve(this.url);
}

attachDebugger_(tabId) {
return new Promise((resolve, reject) => {
chrome.debugger.attach({tabId}, '1.1', _ => {
if (chrome.runtime.lastError) {
return reject(chrome.runtime.lastError);
}
this._debuggerConnected = true;
resolve(tabId);
});
});
}

detachDebugger_(tabId) {
return new Promise((resolve, reject) => {
chrome.debugger.detach({tabId}, _ => {
if (chrome.runtime.lastError) {
return reject(chrome.runtime.lastError);
}

resolve(tabId);
});
});
}

handleUnexpectedDetach() {
chrome.debugger.onDetach.addListener((debuggee, detachReason) => {
this._debuggerConnected = false;
throw new Error('Lighthouse detached from browser: ' + detachReason);
});
}
}

module.exports = ExtensionDriver;

0 comments on commit 69622b8

Please sign in to comment.