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