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) 2009
18 * the Initial Developer. All Rights Reserved.
21 * Henrik Skupin <hskupin@mozilla.com>
23 * Alternatively, the contents of this file may be used under the terms of
24 * either the GNU General Public License Version 2 or later (the "GPL"), or
25 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26 * in which case the provisions of the GPL or the LGPL are applicable instead
27 * of those above. If you wish to allow use of your version of this file only
28 * under the terms of either the GPL or the LGPL, and not to allow others to
29 * use your version of this file under the terms of the MPL, indicate your
30 * decision by deleting the provisions above and replace them with the notice
31 * and other provisions required by the GPL or the LGPL. If you do not delete
32 * the provisions above, a recipient may use your version of this file under
33 * the terms of any one of the MPL, the GPL or the LGPL.
35 * ***** END LICENSE BLOCK ***** */
39 * The PrivateBrowsingAPI adds support for handling the private browsing mode.
44 // Include required modules
45 var modalDialog = require("modal-dialog");
46 var prefs = require("prefs");
47 var utils = require("utils");
49 // Preference for confirmation dialog when entering Private Browsing mode
50 const PB_NO_PROMPT_PREF = 'browser.privatebrowsing.dont_prompt_on_enter';
52 const gTimeout = 5000;
55 * Create a new privateBrowsing instance.
57 * @class This class adds support for the Private Browsing mode
58 * @param {MozMillController} controller
59 * MozMillController to use for the modal entry dialog
61 function privateBrowsing(controller) {
62 this._controller = controller;
66 * Menu item in the main menu to enter/leave Private Browsing mode
69 this._pbMenuItem = new elementslib.Elem(this._controller.menus['tools-menu'].privateBrowsingItem);
70 this._pbTransitionItem = new elementslib.ID(this._controller.window.document, "Tools:PrivateBrowsing");
72 this.__defineGetter__('_pbs', function() {
74 return this._pbs = Cc["@mozilla.org/privatebrowsing;1"].
75 getService(Ci.nsIPrivateBrowsingService);
80 * Prototype definition of the privateBrowsing class
82 privateBrowsing.prototype = {
84 * Returns the controller of the current window
86 * @returns Mozmill Controller
87 * @type {MozMillController}
90 return this._controller;
94 * Checks the state of the Private Browsing mode
96 * @returns Enabled state
100 return this._pbs.privateBrowsingEnabled;
104 * Sets the state of the Private Browsing mode
106 * @param {boolean} value
107 * New state of the Private Browsing mode
110 this._pbs.privateBrowsingEnabled = value;
114 * Sets the callback handler for the confirmation dialog
116 * @param {function} callback
117 * Callback handler for the confirmation dialog
119 set handler(callback) {
120 this._handler = callback;
124 * Gets the enabled state of the confirmation dialog
126 * @returns Enabled state
130 return !prefs.preferences.getPref(PB_NO_PROMPT_PREF, true);
134 * Sets the enabled state of the confirmation dialog
136 * @param {boolean} value
137 * New enabled state of the confirmation dialog
139 set showPrompt(value){
140 prefs.preferences.setPref(PB_NO_PROMPT_PREF, !value);
144 * Gets all the needed external DTD urls as an array
146 * @returns Array of external DTD urls
149 getDtds : function downloadManager_getDtds() {
150 var dtds = ["chrome://branding/locale/brand.dtd",
151 "chrome://browser/locale/browser.dtd",
152 "chrome://browser/locale/aboutPrivateBrowsing.dtd"];
157 * Turn off Private Browsing mode and reset all changes
159 reset : function privateBrowsing_reset() {
164 this.enabled = false;
167 this.showPrompt = true;
171 * Start the Private Browsing mode
173 * @param {boolean} useShortcut
174 * Use the keyboard shortcut if true otherwise the menu entry is used
176 start: function privateBrowsing_start(useShortcut) {
182 if (this.showPrompt) {
183 dialog = new modalDialog.modalDialog(this._controller.window);
184 dialog.start(this._handler);
188 var cmdKey = utils.getEntity(this.getDtds(), "privateBrowsingCmd.commandkey");
189 this._controller.keypress(null, cmdKey, {accelKey: true, shiftKey: true});
191 this._controller.click(this._pbMenuItem);
195 dialog.waitForDialog();
197 this.waitForTransistionComplete(true);
201 * Stop the Private Browsing mode
203 * @param {boolean} useShortcut
204 * Use the keyboard shortcut if true otherwise the menu entry is used
206 stop: function privateBrowsing_stop(useShortcut)
212 var privateBrowsingCmdKey = utils.getEntity(this.getDtds(), "privateBrowsingCmd.commandkey");
213 this._controller.keypress(null, privateBrowsingCmdKey, {accelKey: true, shiftKey: true});
215 this._controller.click(this._pbMenuItem);
218 this.waitForTransistionComplete(false);
222 * Waits until the transistion into or out of the Private Browsing mode happened
224 * @param {boolean} state
225 * Expected target state of the Private Browsing mode
227 waitForTransistionComplete : function privateBrowsing_waitForTransitionComplete(state) {
228 // We have to wait until the transition has been finished
229 this._controller.waitForEval("subject.hasAttribute('disabled') == false", gTimeout, 100,
230 this._pbTransitionItem.getNode());
231 this._controller.waitForEval("subject.privateBrowsing.enabled == subject.state", gTimeout, 100,
232 {privateBrowsing: this, state: state});
237 exports.privateBrowsing = privateBrowsing;