1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
14 * The Original Code is MozMill Test code.
16 * The Initial Developer of the Original Code is Mozilla Foundation.
17 * Portions created by the Initial Developer are Copyright (C) 2010
18 * the Initial Developer. All Rights Reserved.
21 * Henrik Skupin <hskupin@mozilla.com>
22 * Aaron Train <aaron.train@gmail.com>
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
40 * The SessionStoreAPI adds support for accessing session related elements and features
45 // Include required modules
46 var prefs = require("prefs");
47 var utils = require("utils");
48 var widgets = require("widgets");
50 // Session Store service
51 var sessionStoreService = Cc["@mozilla.org/browser/sessionstore;1"]
52 .getService(Ci.nsISessionStore);
54 // Preference for indicating the amount of restorable tabs
55 const SESSIONSTORE_MAXTABS_PREF = 'browser.sessionstore.max_tabs_undo';
57 const gTimeout = 5000;
62 * @param {MozMillController} controller
63 * MozMill controller of the browser window to operate on.
65 function aboutSessionRestore(controller)
67 this._controller = controller;
71 * This class handles the about:sessionrestore page.
73 aboutSessionRestore.prototype = {
75 * Returns the MozMill controller
77 * @returns Mozmill controller
78 * @type {MozMillController}
81 return this._controller;
85 * Returns the tree which contains the windows and tabs
87 * @returns Tree with windows and tabs to restore
91 return this.getElement({type: "tabList"});
95 * Gets all the needed external DTD urls as an array
97 * @returns Array of external DTD urls
100 getDtds : function aboutSessionRestore_getDtds() {
101 var dtds = ["chrome://browser/locale/browser.dtd",
102 "chrome://browser/locale/aboutSessionRestore.dtd"];
107 * Retrieve an UI element based on the given spec
109 * @param {object} spec
110 * Information of the UI element which should be retrieved
111 * type: General type information
112 * subtype: Specific element or property
113 * value: Value of the element or property
114 * @returns Element which has been created
117 getElement : function aboutSessionRestore_getElement(spec) {
121 case "button_restoreSession":
122 elem = new elementslib.ID(this._controller.tabs.activeTab, "errorTryAgain");
124 case "error_longDesc":
125 elem = new elementslib.ID(this._controller.tabs.activeTab, "errorLongDesc");
127 case "error_pageContainer":
128 elem = new elementslib.ID(this._controller.tabs.activeTab, "errorPageContainer");
130 case "error_shortDesc":
131 elem = new elementslib.ID(this._controller.tabs.activeTab, "errorShortDescText");
134 elem = new elementslib.ID(this._controller.tabs.activeTab, "errorTitleText");
137 elem = new elementslib.ID(this._controller.window.document, "tabList");
140 throw new Error(arguments.callee.name + ": Unknown element type - " + spec.type);
147 * Returns the current restore state of the given element
149 * @param {object} element
150 * Element which restore state should be retrieved
151 * @returns True if the element should be restored
155 getRestoreState : function aboutSessionRestore_getRestoreState(element) {
156 var tree = this.tabList.getNode();
158 return tree.view.getCellValue(element.listIndex, tree.columns.getColumnAt(0));
162 * Get restorable tabs under the given window
164 * @param {object} window
165 * Window inside the tree
166 * @returns List of tabs
167 * @type {array of object}
169 getTabs : function aboutSessionRestore_getTabs(window) {
171 var tree = this.tabList.getNode();
173 // Add entries when they are tabs (no container)
174 var ii = window.listIndex + 1;
175 while (ii < tree.view.rowCount && !tree.view.isContainer(ii)) {
179 restore: tree.view.getCellValue(ii, tree.columns.getColumnAt(0)),
180 title: tree.view.getCellText(ii, tree.columns.getColumnAt(2))
189 * Get restorable windows
191 * @returns List of windows
192 * @type {array of object}
194 getWindows : function aboutSessionRestore_getWindows() {
196 var tree = this.tabList.getNode();
198 for (var ii = 0; ii < tree.view.rowCount; ii++) {
199 if (tree.view.isContainer(ii)) {
201 index: windows.length,
203 open: tree.view.isContainerOpen(ii),
204 restore: tree.view.getCellValue(ii, tree.columns.getColumnAt(0)),
205 title: tree.view.getCellText(ii, tree.columns.getColumnAt(2))
214 * Toggles the restore state for the element
216 * @param {object} element
217 * Specifies the element which restore state should be toggled
219 toggleRestoreState : function aboutSessionRestore_toggleRestoreState(element) {
220 var state = this.getRestoreState(element);
222 widgets.clickTreeCell(this._controller, this.tabList, element.listIndex, 0, {});
223 this._controller.sleep(0);
225 this._controller.assertJS("subject.newState != subject.oldState",
226 {newState : this.getRestoreState(element), oldState : state});
231 * Resets the list of recently closed tabs by setting and clearing the user preference
233 function resetRecentlyClosedTabs()
235 prefs.preferences.setPref(SESSIONSTORE_MAXTABS_PREF, 0);
236 prefs.preferences.clearUserPref(SESSIONSTORE_MAXTABS_PREF);
240 * Returns the number of restorable tabs for a given window
242 * @param {MozMillController} controller
243 * MozMillController of the window to operate on
244 * @returns The number of restorable tabs in the window
246 function getClosedTabCount(controller)
248 return sessionStoreService.getClosedTabCount(controller.window);
252 * Restores the tab which has been recently closed
254 * @param {MozMillController} controller
255 * MozMillController of the window to operate on
256 * @param {object} event
257 * Specifies the event to use to execute the command
259 function undoClosedTab(controller, event)
261 var count = sessionStoreService.getClosedTabCount(controller.window);
263 switch (event.type) {
265 throw new Error("Menu gets build dynamically and cannot be accessed.");
268 var cmdKey = utils.getEntity(this.getDtds(), "tabCmd.commandkey");
269 controller.keypress(null, cmdKey, {accelKey: true, shiftKey: true});
274 controller.assertJS("subject.newTabCount < subject.oldTabCount",
276 newTabCount : sessionStoreService.getClosedTabCount(controller.window),
282 * Restores the window which has been recently closed
284 * @param {MozMillController} controller
285 * MozMillController of the window to operate on
286 * @param {object} event
287 * Specifies the event to use to execute the command
289 function undoClosedWindow(controller, event)
291 var count = sessionStoreService.getClosedWindowCount(controller.window);
293 switch (event.type) {
295 throw new Error("Menu gets build dynamically and cannot be accessed.");
298 var cmdKey = utils.getEntity(this.getDtds(), "newNavigatorCmd.key");
299 controller.keypress(null, cmdKey, {accelKey: true, shiftKey: true});
304 controller.assertJS("subject.newWindowCount < subject.oldWindowCount",
306 newWindowCount : sessionStoreService.getClosedWindowCount(controller.window),
307 oldWindowCount : count
311 // Export of functions
312 exports.getClosedTabCount = getClosedTabCount;
313 exports.resetRecentlyClosedTabs = resetRecentlyClosedTabs;
314 exports.undoClosedTab = undoClosedTab;
315 exports.undoClosedWindow = undoClosedWindow;
318 exports.aboutSessionRestore = aboutSessionRestore;