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.
1 /* -*- Mode: Javascript; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ft=javascript ts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 const {Cc, Ci, Cu, Cr} = require("chrome");
9 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
10 Cu.import("resource://gre/modules/Services.jsm");
12 let {Promise: promise} = Cu.import("resource://gre/modules/Promise.jsm", {});
13 let EventEmitter = require("devtools/toolkit/event-emitter");
15 Cu.import("resource:///modules/devtools/StyleEditorUI.jsm");
16 Cu.import("resource:///modules/devtools/StyleEditorUtil.jsm");
18 loader.lazyGetter(this, "StyleSheetsFront",
19 () => require("devtools/server/actors/stylesheets").StyleSheetsFront);
21 loader.lazyGetter(this, "StyleEditorFront",
22 () => require("devtools/server/actors/styleeditor").StyleEditorFront);
24 this.StyleEditorPanel = function StyleEditorPanel(panelWin, toolbox) {
25 EventEmitter.decorate(this);
27 this._toolbox = toolbox;
28 this._target = toolbox.target;
29 this._panelWin = panelWin;
30 this._panelDoc = panelWin.document;
32 this.destroy = this.destroy.bind(this);
33 this._showError = this._showError.bind(this);
34 }
36 exports.StyleEditorPanel = StyleEditorPanel;
38 StyleEditorPanel.prototype = {
39 get target() this._toolbox.target,
41 get panelWindow() this._panelWin,
43 /**
44 * open is effectively an asynchronous constructor
45 */
46 open: function() {
47 let deferred = promise.defer();
49 let targetPromise;
50 // We always interact with the target as if it were remote
51 if (!this.target.isRemote) {
52 targetPromise = this.target.makeRemote();
53 } else {
54 targetPromise = promise.resolve(this.target);
55 }
57 targetPromise.then(() => {
58 this.target.on("close", this.destroy);
60 if (this.target.form.styleSheetsActor) {
61 this._debuggee = StyleSheetsFront(this.target.client, this.target.form);
62 }
63 else {
64 /* We're talking to a pre-Firefox 29 server-side */
65 this._debuggee = StyleEditorFront(this.target.client, this.target.form);
66 }
67 this.UI = new StyleEditorUI(this._debuggee, this.target, this._panelDoc);
68 this.UI.initialize().then(() => {
69 this.UI.on("error", this._showError);
71 this.isReady = true;
73 deferred.resolve(this);
74 });
75 }, console.error);
77 return deferred.promise;
78 },
80 /**
81 * Show an error message from the style editor in the toolbox
82 * notification box.
83 *
84 * @param {string} event
85 * Type of event
86 * @param {string} code
87 * Error code of error to report
88 * @param {string} message
89 * Extra message to append to error message
90 */
91 _showError: function(event, code, message) {
92 if (!this._toolbox) {
93 // could get an async error after we've been destroyed
94 return;
95 }
97 let errorMessage = _(code);
98 if (message) {
99 errorMessage += " " + message;
100 }
102 let notificationBox = this._toolbox.getNotificationBox();
103 let notification = notificationBox.getNotificationWithValue("styleeditor-error");
104 if (!notification) {
105 notificationBox.appendNotification(errorMessage,
106 "styleeditor-error", "", notificationBox.PRIORITY_CRITICAL_LOW);
107 }
108 },
110 /**
111 * Select a stylesheet.
112 *
113 * @param {string} href
114 * Url of stylesheet to find and select in editor
115 * @param {number} line
116 * Line number to jump to after selecting. One-indexed
117 * @param {number} col
118 * Column number to jump to after selecting. One-indexed
119 */
120 selectStyleSheet: function(href, line, col) {
121 if (!this._debuggee || !this.UI) {
122 return;
123 }
124 this.UI.selectStyleSheet(href, line - 1, col ? col - 1 : 0);
125 },
127 /**
128 * Destroy the style editor.
129 */
130 destroy: function() {
131 if (!this._destroyed) {
132 this._destroyed = true;
134 this._target.off("close", this.destroy);
135 this._target = null;
136 this._toolbox = null;
137 this._panelDoc = null;
138 this._debuggee.destroy();
139 this._debuggee = null;
141 this.UI.destroy();
142 }
144 return promise.resolve(null);
145 },
146 }
148 XPCOMUtils.defineLazyGetter(StyleEditorPanel.prototype, "strings",
149 function () {
150 return Services.strings.createBundle(
151 "chrome://browser/locale/devtools/styleeditor.properties");
152 });