Bug 1127004 - Use promises and session in b2g allowConnection. r=past

This commit is contained in:
J. Ryan Stinnett 2015-02-05 15:26:13 -06:00
parent ac7acbf236
commit 49385deef4
4 changed files with 75 additions and 22 deletions

View File

@ -24,27 +24,55 @@ XPCOMUtils.defineLazyGetter(this, "B2GTabList", function() {
});
let RemoteDebugger = {
_promptDone: false,
_promptAnswer: false,
_listening: false,
prompt: function() {
/**
* Prompt the user to accept or decline the incoming connection.
*
* @param session object
* The session object will contain at least the following fields:
* {
* authentication,
* client: {
* host,
* port
* },
* server: {
* host,
* port
* }
* }
* Specific authentication modes may include additional fields. Check
* the different |allowConnection| methods in
* toolkit/devtools/security/auth.js.
* @return An AuthenticationResult value.
* A promise that will be resolved to the above is also allowed.
*/
allowConnection(session) {
if (this._promptingForAllow) {
// Don't stack connection prompts if one is already open
return DebuggerServer.AuthenticationResult.DENY;
}
this._listen();
this._promptDone = false;
this._promptingForAllow = new Promise(resolve => {
this._handleAllowResult = allowed => {
this._handleAllowResult = null;
this._promptingForAllow = null;
if (allowed) {
resolve(DebuggerServer.AuthenticationResult.ALLOW);
} else {
resolve(DebuggerServer.AuthenticationResult.DENY);
}
};
shell.sendChromeEvent({
"type": "remote-debugger-prompt"
shell.sendChromeEvent({
type: "remote-debugger-prompt",
session
});
});
while(!this._promptDone) {
Services.tm.currentThread.processNextEvent(true);
}
if (this._promptAnswer) {
return DebuggerServer.AuthenticationResult.ALLOW;
}
return DebuggerServer.AuthenticationResult.DENY;
return this._promptingForAllow;
},
_listen: function() {
@ -63,8 +91,9 @@ let RemoteDebugger = {
if (detail.type !== "remote-debugger-prompt") {
return;
}
this._promptAnswer = detail.value;
this._promptDone = true;
if (this._handleAllowResult) {
this._handleAllowResult(detail.value);
}
},
initServer: function() {
@ -94,7 +123,6 @@ let RemoteDebugger = {
*/
DebuggerServer.createRootActor = function createRootActor(connection)
{
let { Promise: promise } = Cu.import("resource://gre/modules/Promise.jsm", {});
let parameters = {
tabList: new B2GTabList(connection),
// Use an explicit global actor list to prevent exposing
@ -118,7 +146,8 @@ let RemoteDebugger = {
}
};
RemoteDebugger.prompt = RemoteDebugger.prompt.bind(RemoteDebugger);
RemoteDebugger.allowConnection =
RemoteDebugger.allowConnection.bind(RemoteDebugger);
let USBRemoteDebugger = {
@ -146,7 +175,7 @@ let USBRemoteDebugger = {
debug("Starting USB debugger on " + portOrPath);
let AuthenticatorType = DebuggerServer.Authenticators.get("PROMPT");
let authenticator = new AuthenticatorType.Server();
authenticator.allowConnection = RemoteDebugger.prompt;
authenticator.allowConnection = RemoteDebugger.allowConnection;
this._listener = DebuggerServer.createListener();
this._listener.portOrPath = portOrPath;
this._listener.authenticator = authenticator;
@ -187,7 +216,7 @@ let WiFiRemoteDebugger = {
debug("Starting WiFi debugger");
let AuthenticatorType = DebuggerServer.Authenticators.get("OOB_CERT");
let authenticator = new AuthenticatorType.Server();
authenticator.allowConnection = RemoteDebugger.prompt;
authenticator.allowConnection = RemoteDebugger.allowConnection;
this._listener = DebuggerServer.createListener();
this._listener.portOrPath = -1 /* any available port */;
this._listener.authenticator = authenticator;

View File

@ -205,7 +205,7 @@ Prompt.Server.prototype = {
},
/**
* Prompt the user to accept or decline the incoming connection. The default
* Prompt the user to accept or decline the incoming connection. The default
* implementation is used unless this is overridden on a particular
* authenticator instance.
*
@ -215,6 +215,7 @@ Prompt.Server.prototype = {
* @param session object
* In PROMPT mode, the |session| includes:
* {
* authentication: "PROMPT",
* client: {
* host,
* port
@ -560,6 +561,7 @@ OOBCert.Server.prototype = {
* @param session object
* In OOB_CERT mode, the |session| includes:
* {
* authentication: "OOB_CERT",
* client: {
* host,
* port,

View File

@ -101,11 +101,26 @@ Client.defaultSendOOB = ({ authResult, oob }) => {
};
/**
* Prompt the user to accept or decline the incoming connection. This is the
* Prompt the user to accept or decline the incoming connection. This is the
* default implementation that products embedding the debugger server may
* choose to override. This can be overridden via |allowConnection| on the
* socket's authenticator instance.
*
* @param session object
* The session object will contain at least the following fields:
* {
* authentication,
* client: {
* host,
* port
* },
* server: {
* host,
* port
* }
* }
* Specific authentication modes may include additional fields. Check
* the different |allowConnection| methods in ./auth.js.
* @return An AuthenticationResult value.
* A promise that will be resolved to the above is also allowed.
*/

View File

@ -707,6 +707,9 @@ ServerSocketConnection.prototype = {
}),
deny(result) {
if (this._destroyed) {
return;
}
let errorName = result;
for (let name in Cr) {
if (Cr[name] === result) {
@ -723,12 +726,16 @@ ServerSocketConnection.prototype = {
},
allow() {
if (this._destroyed) {
return;
}
dumpn("Debugging connection allowed on " + this.address);
DebuggerServer._onConnection(this._transport);
this.destroy();
},
destroy() {
this._destroyed = true;
clearTimeout(this._handshakeTimeout);
this._setSecurityObserver(null);
this._listener = null;