toolkit/devtools/server/actors/gcli.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 "use strict";
michael@0 6
michael@0 7 var Cu = require('chrome').Cu;
michael@0 8 var XPCOMUtils = Cu.import("resource://gre/modules/XPCOMUtils.jsm", {}).XPCOMUtils;
michael@0 9
michael@0 10 XPCOMUtils.defineLazyModuleGetter(this, "console",
michael@0 11 "resource://gre/modules/devtools/Console.jsm");
michael@0 12 XPCOMUtils.defineLazyModuleGetter(this, "CommandUtils",
michael@0 13 "resource:///modules/devtools/DeveloperToolbar.jsm");
michael@0 14
michael@0 15 XPCOMUtils.defineLazyGetter(this, "Requisition", function() {
michael@0 16 return require("gcli/cli").Requisition;
michael@0 17 });
michael@0 18
michael@0 19 XPCOMUtils.defineLazyGetter(this, "centralCanon", function() {
michael@0 20 return require("gcli/commands/commands").centralCanon;
michael@0 21 });
michael@0 22
michael@0 23 var util = require('gcli/util/util');
michael@0 24
michael@0 25 var protocol = require("devtools/server/protocol");
michael@0 26 var method = protocol.method;
michael@0 27 var Arg = protocol.Arg;
michael@0 28 var Option = protocol.Option;
michael@0 29 var RetVal = protocol.RetVal;
michael@0 30
michael@0 31 /**
michael@0 32 * Manage remote connections that want to talk to GCLI
michael@0 33 */
michael@0 34 var GcliActor = protocol.ActorClass({
michael@0 35 typeName: "gcli",
michael@0 36
michael@0 37 initialize: function(conn, tabActor) {
michael@0 38 protocol.Actor.prototype.initialize.call(this, conn);
michael@0 39 this.tabActor = tabActor;
michael@0 40 let browser = tabActor.browser;
michael@0 41
michael@0 42 let environment = {
michael@0 43 chromeWindow: browser.ownerGlobal,
michael@0 44 chromeDocument: browser.ownerDocument,
michael@0 45 window: browser.contentWindow,
michael@0 46 document: browser.contentDocument
michael@0 47 };
michael@0 48
michael@0 49 this.requisition = new Requisition({ environment: env });
michael@0 50 },
michael@0 51
michael@0 52 /**
michael@0 53 * Retrieve a list of the remotely executable commands
michael@0 54 */
michael@0 55 specs: method(function() {
michael@0 56 return this.requisition.canon.getCommandSpecs();
michael@0 57 }, {
michael@0 58 request: {},
michael@0 59 response: RetVal("json")
michael@0 60 }),
michael@0 61
michael@0 62 /**
michael@0 63 * Execute a GCLI command
michael@0 64 * @return a promise of an object with the following properties:
michael@0 65 * - data: The output of the command
michael@0 66 * - type: The type of the data to allow selection of a converter
michael@0 67 * - error: True if the output was considered an error
michael@0 68 */
michael@0 69 execute: method(function(typed) {
michael@0 70 return this.requisition.updateExec(typed).then(function(output) {
michael@0 71 return output.toJson();
michael@0 72 });
michael@0 73 }, {
michael@0 74 request: {
michael@0 75 typed: Arg(0, "string") // The command string
michael@0 76 },
michael@0 77 response: RetVal("json")
michael@0 78 }),
michael@0 79
michael@0 80 /**
michael@0 81 * Get the state of an input string. i.e. requisition.getStateData()
michael@0 82 */
michael@0 83 state: method(function(typed, start, rank) {
michael@0 84 return this.requisition.update(typed).then(function() {
michael@0 85 return this.requisition.getStateData(start, rank);
michael@0 86 }.bind(this));
michael@0 87 }, {
michael@0 88 request: {
michael@0 89 typed: Arg(0, "string"), // The command string
michael@0 90 start: Arg(1, "number"), // Cursor start position
michael@0 91 rank: Arg(2, "number") // The prediction offset (# times UP/DOWN pressed)
michael@0 92 },
michael@0 93 response: RetVal("json")
michael@0 94 }),
michael@0 95
michael@0 96 /**
michael@0 97 * Call type.parse to check validity. Used by the remote type
michael@0 98 * @return a promise of an object with the following properties:
michael@0 99 * - status: Of of the following strings: VALID|INCOMPLETE|ERROR
michael@0 100 * - message: The message to display to the user
michael@0 101 * - predictions: An array of suggested values for the given parameter
michael@0 102 */
michael@0 103 typeparse: method(function(typed, param) {
michael@0 104 return this.requisition.update(typed).then(function() {
michael@0 105 var assignment = this.requisition.getAssignment(param);
michael@0 106
michael@0 107 return promise.resolve(assignment.predictions).then(function(predictions) {
michael@0 108 return {
michael@0 109 status: assignment.getStatus().toString(),
michael@0 110 message: assignment.message,
michael@0 111 predictions: predictions
michael@0 112 };
michael@0 113 });
michael@0 114 });
michael@0 115 }, {
michael@0 116 request: {
michael@0 117 typed: Arg(0, "string"), // The command string
michael@0 118 param: Arg(1, "string") // The name of the parameter to parse
michael@0 119 },
michael@0 120 response: RetVal("json")
michael@0 121 }),
michael@0 122
michael@0 123 /**
michael@0 124 * Get the incremented value of some type
michael@0 125 * @return a promise of a string containing the new argument text
michael@0 126 */
michael@0 127 typeincrement: method(function(typed, param) {
michael@0 128 return this.requisition.update(typed).then(function() {
michael@0 129 var assignment = this.requisition.getAssignment(param);
michael@0 130 return this.requisition.increment(assignment).then(function() {
michael@0 131 return assignment.arg == null ? undefined : assignment.arg.text;
michael@0 132 });
michael@0 133 });
michael@0 134 }, {
michael@0 135 request: {
michael@0 136 typed: Arg(0, "string"), // The command string
michael@0 137 param: Arg(1, "string") // The name of the parameter to parse
michael@0 138 },
michael@0 139 response: RetVal("string")
michael@0 140 }),
michael@0 141
michael@0 142 /**
michael@0 143 * See typeincrement
michael@0 144 */
michael@0 145 typedecrement: method(function(typed, param) {
michael@0 146 return this.requisition.update(typed).then(function() {
michael@0 147 var assignment = this.requisition.getAssignment(param);
michael@0 148 return this.requisition.decrement(assignment).then(function() {
michael@0 149 return assignment.arg == null ? undefined : assignment.arg.text;
michael@0 150 });
michael@0 151 });
michael@0 152 }, {
michael@0 153 request: {
michael@0 154 typed: Arg(0, "string"), // The command string
michael@0 155 param: Arg(1, "string") // The name of the parameter to parse
michael@0 156 },
michael@0 157 response: RetVal("string")
michael@0 158 }),
michael@0 159
michael@0 160 /**
michael@0 161 * Perform a lookup on a selection type to get the allowed values
michael@0 162 */
michael@0 163 selectioninfo: method(function(commandName, paramName, action) {
michael@0 164 var command = this.requisition.canon.getCommand(commandName);
michael@0 165 if (command == null) {
michael@0 166 throw new Error('No command called \'' + commandName + '\'');
michael@0 167 }
michael@0 168
michael@0 169 var type;
michael@0 170 command.params.forEach(function(param) {
michael@0 171 if (param.name === paramName) {
michael@0 172 type = param.type;
michael@0 173 }
michael@0 174 });
michael@0 175 if (type == null) {
michael@0 176 throw new Error('No parameter called \'' + paramName + '\' in \'' +
michael@0 177 commandName + '\'');
michael@0 178 }
michael@0 179
michael@0 180 switch (action) {
michael@0 181 case 'lookup':
michael@0 182 return type.lookup(context);
michael@0 183 case 'data':
michael@0 184 return type.data(context);
michael@0 185 default:
michael@0 186 throw new Error('Action must be either \'lookup\' or \'data\'');
michael@0 187 }
michael@0 188 }, {
michael@0 189 request: {
michael@0 190 typed: Arg(0, "string"), // The command containing the parameter in question
michael@0 191 param: Arg(1, "string"), // The name of the parameter
michael@0 192 action: Arg(1, "string") // 'lookup' or 'data' depending on the function to call
michael@0 193 },
michael@0 194 response: RetVal("json")
michael@0 195 })
michael@0 196 });
michael@0 197
michael@0 198 exports.GcliFront = protocol.FrontClass(GcliActor, {
michael@0 199 initialize: function(client, tabForm) {
michael@0 200 protocol.Front.prototype.initialize.call(this, client);
michael@0 201 this.actorID = tabForm.gcliActor;
michael@0 202
michael@0 203 // XXX: This is the first actor type in its hierarchy to use the protocol
michael@0 204 // library, so we're going to self-own on the client side for now.
michael@0 205 client.addActorPool(this);
michael@0 206 this.manage(this);
michael@0 207 },
michael@0 208 });
michael@0 209
michael@0 210 /**
michael@0 211 * Called the framework on DebuggerServer.registerModule()
michael@0 212 */
michael@0 213 exports.register = function(handle) {
michael@0 214 handle.addTabActor(GcliActor, "gcliActor");
michael@0 215 };
michael@0 216
michael@0 217 exports.unregister = function(handle) {
michael@0 218 handle.removeTabActor(GcliActor);
michael@0 219 };

mercurial