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 | "use strict"; |
michael@0 | 5 | |
michael@0 | 6 | Cu.import("resource://gre/modules/osfile.jsm"); |
michael@0 | 7 | const {VariablesView} = |
michael@0 | 8 | Cu.import("resource:///modules/devtools/VariablesView.jsm", {}); |
michael@0 | 9 | |
michael@0 | 10 | const VARIABLES_VIEW_URL = |
michael@0 | 11 | "chrome://browser/content/devtools/widgets/VariablesView.xul"; |
michael@0 | 12 | |
michael@0 | 13 | function ManifestEditor(project) { |
michael@0 | 14 | this.project = project; |
michael@0 | 15 | this._onContainerReady = this._onContainerReady.bind(this); |
michael@0 | 16 | this._onEval = this._onEval.bind(this); |
michael@0 | 17 | this._onSwitch = this._onSwitch.bind(this); |
michael@0 | 18 | this._onDelete = this._onDelete.bind(this); |
michael@0 | 19 | this._onNew = this._onNew.bind(this); |
michael@0 | 20 | } |
michael@0 | 21 | |
michael@0 | 22 | ManifestEditor.prototype = { |
michael@0 | 23 | get manifest() { return this.project.manifest; }, |
michael@0 | 24 | |
michael@0 | 25 | get editable() { return this.project.type == "packaged"; }, |
michael@0 | 26 | |
michael@0 | 27 | show: function(containerElement) { |
michael@0 | 28 | let deferred = promise.defer(); |
michael@0 | 29 | let iframe = this._iframe = document.createElement("iframe"); |
michael@0 | 30 | |
michael@0 | 31 | iframe.addEventListener("load", function onIframeLoad() { |
michael@0 | 32 | iframe.removeEventListener("load", onIframeLoad, true); |
michael@0 | 33 | deferred.resolve(iframe.contentWindow); |
michael@0 | 34 | }, true); |
michael@0 | 35 | |
michael@0 | 36 | iframe.setAttribute("src", VARIABLES_VIEW_URL); |
michael@0 | 37 | iframe.classList.add("variables-view"); |
michael@0 | 38 | containerElement.appendChild(iframe); |
michael@0 | 39 | |
michael@0 | 40 | return deferred.promise.then(this._onContainerReady); |
michael@0 | 41 | }, |
michael@0 | 42 | |
michael@0 | 43 | _onContainerReady: function(varWindow) { |
michael@0 | 44 | let variablesContainer = varWindow.document.querySelector("#variables"); |
michael@0 | 45 | |
michael@0 | 46 | variablesContainer.classList.add("manifest-editor"); |
michael@0 | 47 | |
michael@0 | 48 | let editor = this.editor = new VariablesView(variablesContainer); |
michael@0 | 49 | |
michael@0 | 50 | editor.onlyEnumVisible = true; |
michael@0 | 51 | editor.alignedValues = true; |
michael@0 | 52 | editor.actionsFirst = true; |
michael@0 | 53 | |
michael@0 | 54 | if (this.editable) { |
michael@0 | 55 | editor.eval = this._onEval; |
michael@0 | 56 | editor.switch = this._onSwitch; |
michael@0 | 57 | editor.delete = this._onDelete; |
michael@0 | 58 | editor.new = this._onNew; |
michael@0 | 59 | } |
michael@0 | 60 | |
michael@0 | 61 | return this.update(); |
michael@0 | 62 | }, |
michael@0 | 63 | |
michael@0 | 64 | _onEval: function(variable, value) { |
michael@0 | 65 | let parent = this._descend(variable.ownerView.symbolicPath); |
michael@0 | 66 | try { |
michael@0 | 67 | parent[variable.name] = JSON.parse(value); |
michael@0 | 68 | } catch(e) { |
michael@0 | 69 | Cu.reportError(e); |
michael@0 | 70 | } |
michael@0 | 71 | |
michael@0 | 72 | this.update(); |
michael@0 | 73 | }, |
michael@0 | 74 | |
michael@0 | 75 | _onSwitch: function(variable, newName) { |
michael@0 | 76 | if (variable.name == newName) { |
michael@0 | 77 | return; |
michael@0 | 78 | } |
michael@0 | 79 | |
michael@0 | 80 | let parent = this._descend(variable.ownerView.symbolicPath); |
michael@0 | 81 | parent[newName] = parent[variable.name]; |
michael@0 | 82 | delete parent[variable.name]; |
michael@0 | 83 | |
michael@0 | 84 | this.update(); |
michael@0 | 85 | }, |
michael@0 | 86 | |
michael@0 | 87 | _onDelete: function(variable) { |
michael@0 | 88 | let parent = this._descend(variable.ownerView.symbolicPath); |
michael@0 | 89 | delete parent[variable.name]; |
michael@0 | 90 | }, |
michael@0 | 91 | |
michael@0 | 92 | _onNew: function(variable, newName, newValue) { |
michael@0 | 93 | let parent = this._descend(variable.symbolicPath); |
michael@0 | 94 | try { |
michael@0 | 95 | parent[newName] = JSON.parse(newValue); |
michael@0 | 96 | } catch(e) { |
michael@0 | 97 | Cu.reportError(e); |
michael@0 | 98 | } |
michael@0 | 99 | |
michael@0 | 100 | this.update(); |
michael@0 | 101 | }, |
michael@0 | 102 | |
michael@0 | 103 | /** |
michael@0 | 104 | * Returns the value located at a given path in the manifest. |
michael@0 | 105 | * @param path array |
michael@0 | 106 | * A string for each path component: ["developer", "name"] |
michael@0 | 107 | */ |
michael@0 | 108 | _descend: function(path) { |
michael@0 | 109 | let parent = this.manifest; |
michael@0 | 110 | while (path.length) { |
michael@0 | 111 | parent = parent[path.shift()]; |
michael@0 | 112 | } |
michael@0 | 113 | return parent; |
michael@0 | 114 | }, |
michael@0 | 115 | |
michael@0 | 116 | update: function() { |
michael@0 | 117 | this.editor.rawObject = this.manifest; |
michael@0 | 118 | this.editor.commitHierarchy(); |
michael@0 | 119 | |
michael@0 | 120 | // Wait until the animation from commitHierarchy has completed |
michael@0 | 121 | let deferred = promise.defer(); |
michael@0 | 122 | setTimeout(deferred.resolve, this.editor.lazyEmptyDelay + 1); |
michael@0 | 123 | return deferred.promise; |
michael@0 | 124 | }, |
michael@0 | 125 | |
michael@0 | 126 | save: function() { |
michael@0 | 127 | if (this.editable) { |
michael@0 | 128 | let validator = new AppValidator(this.project); |
michael@0 | 129 | let manifestFile = validator._getPackagedManifestFile(); |
michael@0 | 130 | let path = manifestFile.path; |
michael@0 | 131 | |
michael@0 | 132 | let encoder = new TextEncoder(); |
michael@0 | 133 | let data = encoder.encode(JSON.stringify(this.manifest, null, 2)); |
michael@0 | 134 | |
michael@0 | 135 | return OS.File.writeAtomic(path, data, { tmpPath: path + ".tmp" }); |
michael@0 | 136 | } |
michael@0 | 137 | |
michael@0 | 138 | return promise.resolve(); |
michael@0 | 139 | }, |
michael@0 | 140 | |
michael@0 | 141 | destroy: function() { |
michael@0 | 142 | if (this._iframe) { |
michael@0 | 143 | this._iframe.remove(); |
michael@0 | 144 | } |
michael@0 | 145 | } |
michael@0 | 146 | }; |