browser/devtools/scratchpad/scratchpad-manager.jsm

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* vim:set ts=2 sw=2 sts=2 et tw=80:
michael@0 2 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 "use strict";
michael@0 7
michael@0 8 this.EXPORTED_SYMBOLS = ["ScratchpadManager"];
michael@0 9
michael@0 10 const Cc = Components.classes;
michael@0 11 const Ci = Components.interfaces;
michael@0 12 const Cu = Components.utils;
michael@0 13
michael@0 14 const SCRATCHPAD_WINDOW_URL = "chrome://browser/content/devtools/scratchpad.xul";
michael@0 15 const SCRATCHPAD_WINDOW_FEATURES = "chrome,titlebar,toolbar,centerscreen,resizable,dialog=no";
michael@0 16
michael@0 17 Cu.import("resource://gre/modules/Services.jsm");
michael@0 18
michael@0 19 /**
michael@0 20 * The ScratchpadManager object opens new Scratchpad windows and manages the state
michael@0 21 * of open scratchpads for session restore. There's only one ScratchpadManager in
michael@0 22 * the life of the browser.
michael@0 23 */
michael@0 24 this.ScratchpadManager = {
michael@0 25
michael@0 26 _nextUid: 1,
michael@0 27 _scratchpads: [],
michael@0 28
michael@0 29 /**
michael@0 30 * Get the saved states of open scratchpad windows. Called by
michael@0 31 * session restore.
michael@0 32 *
michael@0 33 * @return array
michael@0 34 * The array of scratchpad states.
michael@0 35 */
michael@0 36 getSessionState: function SPM_getSessionState()
michael@0 37 {
michael@0 38 return this._scratchpads;
michael@0 39 },
michael@0 40
michael@0 41 /**
michael@0 42 * Restore scratchpad windows from the scratchpad session store file.
michael@0 43 * Called by session restore.
michael@0 44 *
michael@0 45 * @param function aSession
michael@0 46 * The session object with scratchpad states.
michael@0 47 *
michael@0 48 * @return array
michael@0 49 * The restored scratchpad windows.
michael@0 50 */
michael@0 51 restoreSession: function SPM_restoreSession(aSession)
michael@0 52 {
michael@0 53 if (!Array.isArray(aSession)) {
michael@0 54 return [];
michael@0 55 }
michael@0 56
michael@0 57 let wins = [];
michael@0 58 aSession.forEach(function(state) {
michael@0 59 let win = this.openScratchpad(state);
michael@0 60 wins.push(win);
michael@0 61 }, this);
michael@0 62
michael@0 63 return wins;
michael@0 64 },
michael@0 65
michael@0 66 /**
michael@0 67 * Iterate through open scratchpad windows and save their states.
michael@0 68 */
michael@0 69 saveOpenWindows: function SPM_saveOpenWindows() {
michael@0 70 this._scratchpads = [];
michael@0 71
michael@0 72 function clone(src) {
michael@0 73 let dest = {};
michael@0 74
michael@0 75 for (let key in src) {
michael@0 76 if (src.hasOwnProperty(key)) {
michael@0 77 dest[key] = src[key];
michael@0 78 }
michael@0 79 }
michael@0 80
michael@0 81 return dest;
michael@0 82 }
michael@0 83
michael@0 84 // We need to clone objects we get from Scratchpad instances
michael@0 85 // because such (cross-window) objects have a property 'parent'
michael@0 86 // that holds on to a ChromeWindow instance. This means that
michael@0 87 // such objects are not primitive-values-only anymore so they
michael@0 88 // can leak.
michael@0 89
michael@0 90 let enumerator = Services.wm.getEnumerator("devtools:scratchpad");
michael@0 91 while (enumerator.hasMoreElements()) {
michael@0 92 let win = enumerator.getNext();
michael@0 93 if (!win.closed && win.Scratchpad.initialized) {
michael@0 94 this._scratchpads.push(clone(win.Scratchpad.getState()));
michael@0 95 }
michael@0 96 }
michael@0 97 },
michael@0 98
michael@0 99 /**
michael@0 100 * Open a new scratchpad window with an optional initial state.
michael@0 101 *
michael@0 102 * @param object aState
michael@0 103 * Optional. The initial state of the scratchpad, an object
michael@0 104 * with properties filename, text, and executionContext.
michael@0 105 *
michael@0 106 * @return nsIDomWindow
michael@0 107 * The opened scratchpad window.
michael@0 108 */
michael@0 109 openScratchpad: function SPM_openScratchpad(aState)
michael@0 110 {
michael@0 111 let params = Cc["@mozilla.org/embedcomp/dialogparam;1"]
michael@0 112 .createInstance(Ci.nsIDialogParamBlock);
michael@0 113
michael@0 114 params.SetNumberStrings(2);
michael@0 115 params.SetString(0, this.createUid());
michael@0 116
michael@0 117 if (aState) {
michael@0 118 if (typeof aState != 'object') {
michael@0 119 return;
michael@0 120 }
michael@0 121
michael@0 122 params.SetString(1, JSON.stringify(aState));
michael@0 123 }
michael@0 124
michael@0 125 let win = Services.ww.openWindow(null, SCRATCHPAD_WINDOW_URL, "_blank",
michael@0 126 SCRATCHPAD_WINDOW_FEATURES, params);
michael@0 127
michael@0 128 // Only add the shutdown observer if we've opened a scratchpad window.
michael@0 129 ShutdownObserver.init();
michael@0 130
michael@0 131 return win;
michael@0 132 },
michael@0 133
michael@0 134 /**
michael@0 135 * Create a unique ID for a new Scratchpad.
michael@0 136 */
michael@0 137 createUid: function SPM_createUid()
michael@0 138 {
michael@0 139 return JSON.stringify(this._nextUid++);
michael@0 140 }
michael@0 141 };
michael@0 142
michael@0 143
michael@0 144 /**
michael@0 145 * The ShutdownObserver listens for app shutdown and saves the current state
michael@0 146 * of the scratchpads for session restore.
michael@0 147 */
michael@0 148 var ShutdownObserver = {
michael@0 149 _initialized: false,
michael@0 150
michael@0 151 init: function SDO_init()
michael@0 152 {
michael@0 153 if (this._initialized) {
michael@0 154 return;
michael@0 155 }
michael@0 156
michael@0 157 Services.obs.addObserver(this, "quit-application-granted", false);
michael@0 158
michael@0 159 this._initialized = true;
michael@0 160 },
michael@0 161
michael@0 162 observe: function SDO_observe(aMessage, aTopic, aData)
michael@0 163 {
michael@0 164 if (aTopic == "quit-application-granted") {
michael@0 165 ScratchpadManager.saveOpenWindows();
michael@0 166 this.uninit();
michael@0 167 }
michael@0 168 },
michael@0 169
michael@0 170 uninit: function SDO_uninit()
michael@0 171 {
michael@0 172 Services.obs.removeObserver(this, "quit-application-granted");
michael@0 173 }
michael@0 174 };

mercurial