2011-11-02 12:32:55 -07:00
|
|
|
/* vim:set ts=2 sw=2 sts=2 et tw=80:
|
2012-05-21 04:12:37 -07:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2011-11-02 12:32:55 -07:00
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2012-10-31 09:13:28 -07:00
|
|
|
this.EXPORTED_SYMBOLS = ["ScratchpadManager"];
|
2011-11-02 12:32:55 -07:00
|
|
|
|
|
|
|
const Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
const Cu = Components.utils;
|
|
|
|
|
2013-04-30 08:56:00 -07:00
|
|
|
const SCRATCHPAD_WINDOW_URL = "chrome://browser/content/devtools/scratchpad.xul";
|
2011-11-02 12:32:55 -07:00
|
|
|
const SCRATCHPAD_WINDOW_FEATURES = "chrome,titlebar,toolbar,centerscreen,resizable,dialog=no";
|
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The ScratchpadManager object opens new Scratchpad windows and manages the state
|
|
|
|
* of open scratchpads for session restore. There's only one ScratchpadManager in
|
|
|
|
* the life of the browser.
|
|
|
|
*/
|
2012-10-31 09:13:28 -07:00
|
|
|
this.ScratchpadManager = {
|
2011-11-02 12:32:55 -07:00
|
|
|
|
2012-09-14 13:12:57 -07:00
|
|
|
_nextUid: 1,
|
2011-11-02 12:32:55 -07:00
|
|
|
_scratchpads: [],
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the saved states of open scratchpad windows. Called by
|
|
|
|
* session restore.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
* The array of scratchpad states.
|
|
|
|
*/
|
|
|
|
getSessionState: function SPM_getSessionState()
|
|
|
|
{
|
|
|
|
return this._scratchpads;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Restore scratchpad windows from the scratchpad session store file.
|
|
|
|
* Called by session restore.
|
|
|
|
*
|
|
|
|
* @param function aSession
|
|
|
|
* The session object with scratchpad states.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
* The restored scratchpad windows.
|
|
|
|
*/
|
|
|
|
restoreSession: function SPM_restoreSession(aSession)
|
|
|
|
{
|
|
|
|
if (!Array.isArray(aSession)) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
let wins = [];
|
|
|
|
aSession.forEach(function(state) {
|
|
|
|
let win = this.openScratchpad(state);
|
|
|
|
wins.push(win);
|
|
|
|
}, this);
|
|
|
|
|
|
|
|
return wins;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Iterate through open scratchpad windows and save their states.
|
|
|
|
*/
|
|
|
|
saveOpenWindows: function SPM_saveOpenWindows() {
|
|
|
|
this._scratchpads = [];
|
|
|
|
|
2012-12-19 04:39:00 -08:00
|
|
|
function clone(src) {
|
|
|
|
let dest = {};
|
|
|
|
|
|
|
|
for (let key in src) {
|
|
|
|
if (src.hasOwnProperty(key)) {
|
|
|
|
dest[key] = src[key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return dest;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We need to clone objects we get from Scratchpad instances
|
|
|
|
// because such (cross-window) objects have a property 'parent'
|
|
|
|
// that holds on to a ChromeWindow instance. This means that
|
|
|
|
// such objects are not primitive-values-only anymore so they
|
|
|
|
// can leak.
|
|
|
|
|
2011-11-02 12:32:55 -07:00
|
|
|
let enumerator = Services.wm.getEnumerator("devtools:scratchpad");
|
|
|
|
while (enumerator.hasMoreElements()) {
|
|
|
|
let win = enumerator.getNext();
|
2011-12-10 09:03:57 -08:00
|
|
|
if (!win.closed && win.Scratchpad.initialized) {
|
2012-12-19 04:39:00 -08:00
|
|
|
this._scratchpads.push(clone(win.Scratchpad.getState()));
|
2011-11-02 12:32:55 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Open a new scratchpad window with an optional initial state.
|
|
|
|
*
|
|
|
|
* @param object aState
|
|
|
|
* Optional. The initial state of the scratchpad, an object
|
|
|
|
* with properties filename, text, and executionContext.
|
|
|
|
*
|
|
|
|
* @return nsIDomWindow
|
|
|
|
* The opened scratchpad window.
|
|
|
|
*/
|
|
|
|
openScratchpad: function SPM_openScratchpad(aState)
|
|
|
|
{
|
2012-09-14 13:12:57 -07:00
|
|
|
let params = Cc["@mozilla.org/embedcomp/dialogparam;1"]
|
|
|
|
.createInstance(Ci.nsIDialogParamBlock);
|
|
|
|
|
|
|
|
params.SetNumberStrings(2);
|
2013-10-04 12:44:09 -07:00
|
|
|
params.SetString(0, this.createUid());
|
2012-09-14 13:12:57 -07:00
|
|
|
|
2011-11-02 12:32:55 -07:00
|
|
|
if (aState) {
|
|
|
|
if (typeof aState != 'object') {
|
|
|
|
return;
|
|
|
|
}
|
2012-09-14 13:12:57 -07:00
|
|
|
|
|
|
|
params.SetString(1, JSON.stringify(aState));
|
2011-11-02 12:32:55 -07:00
|
|
|
}
|
2012-09-14 13:12:57 -07:00
|
|
|
|
2011-11-02 12:32:55 -07:00
|
|
|
let win = Services.ww.openWindow(null, SCRATCHPAD_WINDOW_URL, "_blank",
|
|
|
|
SCRATCHPAD_WINDOW_FEATURES, params);
|
2012-12-19 04:39:00 -08:00
|
|
|
|
2012-07-03 06:40:12 -07:00
|
|
|
// Only add the shutdown observer if we've opened a scratchpad window.
|
2011-11-02 12:32:55 -07:00
|
|
|
ShutdownObserver.init();
|
|
|
|
|
|
|
|
return win;
|
2013-10-04 12:44:09 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a unique ID for a new Scratchpad.
|
|
|
|
*/
|
|
|
|
createUid: function SPM_createUid()
|
|
|
|
{
|
|
|
|
return JSON.stringify(this._nextUid++);
|
2011-11-02 12:32:55 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The ShutdownObserver listens for app shutdown and saves the current state
|
|
|
|
* of the scratchpads for session restore.
|
|
|
|
*/
|
|
|
|
var ShutdownObserver = {
|
|
|
|
_initialized: false,
|
|
|
|
|
|
|
|
init: function SDO_init()
|
|
|
|
{
|
|
|
|
if (this._initialized) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Services.obs.addObserver(this, "quit-application-granted", false);
|
2012-12-19 04:39:00 -08:00
|
|
|
|
2011-11-02 12:32:55 -07:00
|
|
|
this._initialized = true;
|
|
|
|
},
|
|
|
|
|
|
|
|
observe: function SDO_observe(aMessage, aTopic, aData)
|
|
|
|
{
|
|
|
|
if (aTopic == "quit-application-granted") {
|
|
|
|
ScratchpadManager.saveOpenWindows();
|
|
|
|
this.uninit();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
uninit: function SDO_uninit()
|
|
|
|
{
|
|
|
|
Services.obs.removeObserver(this, "quit-application-granted");
|
|
|
|
}
|
2012-10-26 12:05:00 -07:00
|
|
|
};
|