From 84bd208e23d340bc21e7b397f2690f10cd7a9c9a Mon Sep 17 00:00:00 2001 From: Nick Fitzgerald Date: Fri, 3 Jun 2016 10:45:10 -0700 Subject: [PATCH] Bug 1277673 - Decouple the ActorActorFront and ActorRegistryFront from the ActorActor and ActorRegistryActor respectively; r=ejpbruel --- devtools/client/inspector/markup/test/head.js | 2 +- .../client/shared/test/test-actor-registry.js | 2 +- devtools/server/actors/actor-registry.js | 84 ++----------------- .../tests/browser/browser_register_actor.js | 2 +- .../tests/unit/test_actor-registry-actor.js | 2 +- devtools/shared/fronts/actor-registry.js | 67 +++++++++++++++ devtools/shared/fronts/moz.build | 1 + devtools/shared/specs/actor-registry.js | 43 ++++++++++ devtools/shared/specs/moz.build | 1 + 9 files changed, 122 insertions(+), 82 deletions(-) create mode 100644 devtools/shared/fronts/actor-registry.js create mode 100644 devtools/shared/specs/actor-registry.js diff --git a/devtools/client/inspector/markup/test/head.js b/devtools/client/inspector/markup/test/head.js index c29c21dda254..367e6688b9a4 100644 --- a/devtools/client/inspector/markup/test/head.js +++ b/devtools/client/inspector/markup/test/head.js @@ -12,7 +12,7 @@ Services.scriptloader.loadSubScript( var {getInplaceEditorForSpan: inplaceEditor} = require("devtools/client/shared/inplace-editor"); var clipboard = require("sdk/clipboard"); -var {ActorRegistryFront} = require("devtools/server/actors/actor-registry"); +var {ActorRegistryFront} = require("devtools/shared/fronts/actor-registry"); // If a test times out we want to see the complete log and not just the last few // lines. diff --git a/devtools/client/shared/test/test-actor-registry.js b/devtools/client/shared/test/test-actor-registry.js index 512f4be84b92..c32b68c830ed 100644 --- a/devtools/client/shared/test/test-actor-registry.js +++ b/devtools/client/shared/test/test-actor-registry.js @@ -25,7 +25,7 @@ let deferred = promise.defer(); client.listTabs(deferred.resolve); let response = yield deferred.promise; - let { ActorRegistryFront } = require("devtools/server/actors/actor-registry"); + let { ActorRegistryFront } = require("devtools/shared/fronts/actor-registry"); let registryFront = ActorRegistryFront(client, response); // Then ask to register our test-actor to retrieve its front diff --git a/devtools/server/actors/actor-registry.js b/devtools/server/actors/actor-registry.js index 2e82bf0ab560..6a083ba6f9b6 100644 --- a/devtools/server/actors/actor-registry.js +++ b/devtools/server/actors/actor-registry.js @@ -11,50 +11,34 @@ const { Cu, CC, components } = require("chrome"); const Services = require("Services"); const { DebuggerServer } = require("devtools/server/main"); const { registerActor, unregisterActor } = require("devtools/server/actors/utils/actor-registry-utils"); - -loader.lazyImporter(this, "NetUtil", "resource://gre/modules/NetUtil.jsm"); +const { actorActorSpec, actorRegistrySpec } = require("devtools/shared/specs/actor-registry"); /** * The ActorActor gives you a handle to an actor you've dynamically * registered and allows you to unregister it. */ -const ActorActor = protocol.ActorClass({ - typeName: "actorActor", - +const ActorActor = protocol.ActorClassWithSpec(actorActorSpec, { initialize: function (conn, options) { protocol.Actor.prototype.initialize.call(this, conn); this.options = options; }, - unregister: method(function () { + unregister: function () { unregisterActor(this.options); - }, { - request: {}, - response: {} - }) -}); - -const ActorActorFront = protocol.FrontClass(ActorActor, { - initialize: function (client, form) { - protocol.Front.prototype.initialize.call(this, client, form); } }); -exports.ActorActorFront = ActorActorFront; - /* * The ActorRegistryActor allows clients to define new actors on the * server. This is particularly useful for addons. */ -const ActorRegistryActor = protocol.ActorClass({ - typeName: "actorRegistry", - +const ActorRegistryActor = protocol.ActorClassWithSpec(actorRegistrySpec, { initialize: function (conn) { protocol.Actor.prototype.initialize.call(this, conn); }, - registerActor: method(function (sourceText, fileName, options) { + registerActor: function (sourceText, fileName, options) { return registerActor(sourceText, fileName, options).then(() => { let { constructor, type } = options; @@ -64,63 +48,7 @@ const ActorRegistryActor = protocol.ActorClass({ global: type.global }); }); - }, { - request: { - sourceText: Arg(0, "string"), - filename: Arg(1, "string"), - options: Arg(2, "json") - }, - - response: { - actorActor: RetVal("actorActor") - } - }) + } }); exports.ActorRegistryActor = ActorRegistryActor; - -function request(uri) { - return new Promise((resolve, reject) => { - try { - uri = Services.io.newURI(uri, null, null); - } catch (e) { - reject(e); - } - - NetUtil.asyncFetch({ - uri, - loadUsingSystemPrincipal: true, - }, (stream, status, req) => { - if (!components.isSuccessCode(status)) { - reject(new Error("Request failed with status code = " - + status - + " after NetUtil.asyncFetch for url = " - + uri)); - return; - } - - let source = NetUtil.readInputStreamToString(stream, stream.available()); - stream.close(); - resolve(source); - }); - }); -} - -const ActorRegistryFront = protocol.FrontClass(ActorRegistryActor, { - initialize: function (client, form) { - protocol.Front.prototype.initialize.call(this, client, - { actor: form.actorRegistryActor }); - - this.manage(this); - }, - - registerActor: custom(function (uri, options) { - return request(uri, options) - .then(sourceText => { - return this._registerActor(sourceText, uri, options); - }); - }, { - impl: "_registerActor" - }) -}); -exports.ActorRegistryFront = ActorRegistryFront; diff --git a/devtools/server/tests/browser/browser_register_actor.js b/devtools/server/tests/browser/browser_register_actor.js index 38c33161ae3a..6276c2b5f6d9 100644 --- a/devtools/server/tests/browser/browser_register_actor.js +++ b/devtools/server/tests/browser/browser_register_actor.js @@ -2,7 +2,7 @@ var gClient; function test() { waitForExplicitFinish(); - var {ActorRegistryFront} = require("devtools/server/actors/actor-registry"); + var {ActorRegistryFront} = require("devtools/shared/fronts/actor-registry"); var actorURL = "chrome://mochitests/content/chrome/devtools/server/tests/mochitest/hello-actor.js"; if (!DebuggerServer.initialized) { diff --git a/devtools/server/tests/unit/test_actor-registry-actor.js b/devtools/server/tests/unit/test_actor-registry-actor.js index 161e04f39a4e..8b0abfbbb886 100644 --- a/devtools/server/tests/unit/test_actor-registry-actor.js +++ b/devtools/server/tests/unit/test_actor-registry-actor.js @@ -10,7 +10,7 @@ var gRegistryFront; var gActorFront; var gOldPref; -const { ActorRegistryFront } = require("devtools/server/actors/actor-registry"); +const { ActorRegistryFront } = require("devtools/shared/fronts/actor-registry"); function run_test() { diff --git a/devtools/shared/fronts/actor-registry.js b/devtools/shared/fronts/actor-registry.js new file mode 100644 index 000000000000..40f87b6093cb --- /dev/null +++ b/devtools/shared/fronts/actor-registry.js @@ -0,0 +1,67 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +"use strict"; + +const { components } = require("chrome"); +const Services = require("Services"); +const { actorActorSpec, actorRegistrySpec } = require("devtools/shared/specs/actor-registry"); +const protocol = require("devtools/shared/protocol"); +const { custom } = protocol; + +loader.lazyImporter(this, "NetUtil", "resource://gre/modules/NetUtil.jsm"); + +const ActorActorFront = protocol.FrontClassWithSpec(actorActorSpec, { + initialize: function (client, form) { + protocol.Front.prototype.initialize.call(this, client, form); + } +}); + +exports.ActorActorFront = ActorActorFront; + +function request(uri) { + return new Promise((resolve, reject) => { + try { + uri = Services.io.newURI(uri, null, null); + } catch (e) { + reject(e); + } + + NetUtil.asyncFetch({ + uri, + loadUsingSystemPrincipal: true, + }, (stream, status, req) => { + if (!components.isSuccessCode(status)) { + reject(new Error("Request failed with status code = " + + status + + " after NetUtil.asyncFetch for url = " + + uri)); + return; + } + + let source = NetUtil.readInputStreamToString(stream, stream.available()); + stream.close(); + resolve(source); + }); + }); +} + +const ActorRegistryFront = protocol.FrontClassWithSpec(actorRegistrySpec, { + initialize: function (client, form) { + protocol.Front.prototype.initialize.call(this, client, + { actor: form.actorRegistryActor }); + + this.manage(this); + }, + + registerActor: custom(function (uri, options) { + return request(uri, options) + .then(sourceText => { + return this._registerActor(sourceText, uri, options); + }); + }, { + impl: "_registerActor" + }) +}); + +exports.ActorRegistryFront = ActorRegistryFront; diff --git a/devtools/shared/fronts/moz.build b/devtools/shared/fronts/moz.build index 09c99c9fc033..0b1306da3a6d 100644 --- a/devtools/shared/fronts/moz.build +++ b/devtools/shared/fronts/moz.build @@ -5,6 +5,7 @@ # file, You can obtain one at http://mozilla.org/MPL/2.0/. DevToolsModules( + 'actor-registry.js', 'addons.js', 'animation.js', 'call-watcher.js', diff --git a/devtools/shared/specs/actor-registry.js b/devtools/shared/specs/actor-registry.js new file mode 100644 index 000000000000..0f57dc8d216c --- /dev/null +++ b/devtools/shared/specs/actor-registry.js @@ -0,0 +1,43 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +"use strict"; + +const { + Arg, + RetVal, + generateActorSpec, +} = require("devtools/shared/protocol"); + +const actorActorSpec = generateActorSpec({ + typeName: "actorActor", + + methods: { + unregister: { + request: {}, + response: {} + } + }, +}); + +exports.actorActorSpec = actorActorSpec; + +const actorRegistrySpec = generateActorSpec({ + typeName: "actorRegistry", + + methods: { + registerActor: { + request: { + sourceText: Arg(0, "string"), + filename: Arg(1, "string"), + options: Arg(2, "json") + }, + + response: { + actorActor: RetVal("actorActor") + } + } + } +}); + +exports.actorRegistrySpec = actorRegistrySpec; diff --git a/devtools/shared/specs/moz.build b/devtools/shared/specs/moz.build index 5531656947d0..b343baf2b12f 100644 --- a/devtools/shared/specs/moz.build +++ b/devtools/shared/specs/moz.build @@ -5,6 +5,7 @@ # file, You can obtain one at http://mozilla.org/MPL/2.0/. DevToolsModules( + 'actor-registry.js', 'addons.js', 'animation.js', 'call-watcher.js',