toolkit/devtools/server/actors/string.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.

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 "use strict";
     7 let {Cu} = require("chrome");
     8 let {DebuggerServer} = require("devtools/server/main");
    10 let {Promise: promise} = Cu.import("resource://gre/modules/Promise.jsm", {});
    11 let {Class} = require("sdk/core/heritage");
    13 let protocol = require("devtools/server/protocol");
    14 let {method, Arg, Option, RetVal} = protocol;
    16 exports.LongStringActor = protocol.ActorClass({
    17   typeName: "longstractor",
    19   initialize: function(conn, str) {
    20     protocol.Actor.prototype.initialize.call(this, conn);
    21     this.str = str;
    22     this.short = (this.str.length < DebuggerServer.LONG_STRING_LENGTH);
    23   },
    25   destroy: function() {
    26     this.str = null;
    27     protocol.Actor.prototype.destroy.call(this);
    28   },
    30   form: function() {
    31     if (this.short) {
    32       return this.str;
    33     }
    34     return {
    35       type: "longString",
    36       actor: this.actorID,
    37       length: this.str.length,
    38       initial: this.str.substring(0, DebuggerServer.LONG_STRING_INITIAL_LENGTH)
    39     }
    40   },
    42   substring: method(function(start, end) {
    43     return promise.resolve(this.str.substring(start, end));
    44   }, {
    45     request: {
    46       start: Arg(0),
    47       end: Arg(1)
    48     },
    49     response: { substring: RetVal() },
    50   }),
    52   release: method(function() { }, { release: true })
    53 });
    55 /**
    56  * When a LongString on the server is short enough to be passed
    57  * as a full string, the client will get a ShortLongString instead of
    58  * a LongStringFront.  Its API should match.
    59  *
    60  * I'm very proud of this name.
    61  */
    62 exports.ShortLongString = Class({
    63   initialize: function(str) {
    64     this.str = str;
    65   },
    67   get length() { return this.str.length; },
    68   get initial() { return this.str; },
    69   string: function() { return promise.resolve(this.str) },
    71   substring: function(start, end) {
    72     return promise.resolve(this.str.substring(start, end));
    73   },
    75   release: function() {
    76     this.str = null;
    77     return promise.resolve(undefined);
    78   }
    79 })
    81 exports.LongStringFront = protocol.FrontClass(exports.LongStringActor, {
    82   initialize: function(client, form) {
    83     // Don't give the form by default, because we're being tricky and it might just
    84     // be a string.
    85     protocol.Front.prototype.initialize.call(this, client, null);
    86     this.form(form);
    87   },
    89   destroy: function() {
    90     this.initial = null;
    91     this.length = null;
    92     this.strPromise = null;
    93     protocol.Front.prototype.destroy.call(this);
    94   },
    96   form: function(form) {
    97     this.actorID = form.actorID;
    98     this.initial = form.initial;
    99     this.length = form.length;
   100   },
   102   string: function() {
   103     if (!this.strPromise) {
   104       let promiseRest = (thusFar) => {
   105         if (thusFar.length === this.length)
   106           return promise.resolve(thusFar);
   107         else {
   108           return this.substring(thusFar.length,
   109                                 thusFar.length + DebuggerServer.LONG_STRING_READ_LENGTH)
   110             .then((next) => promiseRest(thusFar + next));
   111         }
   112       }
   114       this.strPromise = promiseRest(this.initial);
   115     }
   116     return this.strPromise;
   117   }
   118 });
   120 // The long string actor needs some custom marshalling, because it is sometimes
   121 // returned as a primitive rather than a complete form.
   123 let stringActorType = protocol.types.getType("longstractor");
   124 protocol.types.addType("longstring", {
   125   _actor: true,
   126   write: (value, context, detail) => {
   127     if (!(context instanceof protocol.Actor)) {
   128       throw Error("Passing a longstring as an argument isn't supported.");
   129     }
   130     if (value.short) {
   131       return value.str;
   132     } else {
   133       return stringActorType.write(value, context, detail);
   134     }
   135   },
   136   read: (value, context, detail) => {
   137     if (context instanceof protocol.Actor) {
   138       throw Error("Passing a longstring as an argument isn't supported.");
   139     }
   140     if (typeof(value) === "string") {
   141       return exports.ShortLongString(value);
   142     }
   143     return stringActorType.read(value, context, detail);
   144   }
   145 });

mercurial