]> git.donarmstrong.com Git - dactyl.git/blob - common/tests/functional/shared-modules/private-browsing.js
Initial import of 1.0~b6
[dactyl.git] / common / tests / functional / shared-modules / private-browsing.js
1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
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/
8  *
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
12  * License.
13  *
14  * The Original Code is MozMill Test code.
15  *
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.
19  *
20  * Contributor(s):
21  *   Henrik Skupin <hskupin@mozilla.com>
22  *
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.
34  *
35  * ***** END LICENSE BLOCK ***** */
36
37 /**
38  * @fileoverview
39  * The PrivateBrowsingAPI adds support for handling the private browsing mode.
40  *
41  * @version 1.0.0
42  */
43
44 // Include required modules
45 var modalDialog = require("modal-dialog");
46 var prefs = require("prefs");
47 var utils = require("utils");
48
49 // Preference for confirmation dialog when entering Private Browsing mode
50 const PB_NO_PROMPT_PREF = 'browser.privatebrowsing.dont_prompt_on_enter';
51
52 const gTimeout = 5000;
53
54 /**
55  * Create a new privateBrowsing instance.
56  *
57  * @class This class adds support for the Private Browsing mode
58  * @param {MozMillController} controller
59  *        MozMillController to use for the modal entry dialog
60  */
61 function privateBrowsing(controller) {
62   this._controller = controller;
63   this._handler = null;
64
65   /**
66    * Menu item in the main menu to enter/leave Private Browsing mode
67    * @private
68    */
69   this._pbMenuItem = new elementslib.Elem(this._controller.menus['tools-menu'].privateBrowsingItem);
70   this._pbTransitionItem = new elementslib.ID(this._controller.window.document, "Tools:PrivateBrowsing");
71
72   this.__defineGetter__('_pbs', function() {
73     delete this._pbs;
74     return this._pbs = Cc["@mozilla.org/privatebrowsing;1"].
75                        getService(Ci.nsIPrivateBrowsingService);
76   });
77 }
78
79 /**
80  * Prototype definition of the privateBrowsing class
81  */
82 privateBrowsing.prototype = {
83   /**
84    * Returns the controller of the current window
85    *
86    * @returns Mozmill Controller
87    * @type {MozMillController}
88    */
89   get controller() {
90     return this._controller;
91   },
92
93   /**
94    * Checks the state of the Private Browsing mode
95    *
96    * @returns Enabled state
97    * @type {boolean}
98    */
99   get enabled() {
100     return this._pbs.privateBrowsingEnabled;
101   },
102
103   /**
104    * Sets the state of the Private Browsing mode
105    *
106    * @param {boolean} value
107    *        New state of the Private Browsing mode
108    */
109   set enabled(value) {
110     this._pbs.privateBrowsingEnabled = value;
111   },
112
113   /**
114    * Sets the callback handler for the confirmation dialog
115    *
116    * @param {function} callback
117    *        Callback handler for the confirmation dialog
118    */
119   set handler(callback) {
120     this._handler = callback;
121   },
122
123   /**
124    * Gets the enabled state of the confirmation dialog
125    *
126    * @returns Enabled state
127    * @type {boolean}
128    */
129   get showPrompt() {
130     return !prefs.preferences.getPref(PB_NO_PROMPT_PREF, true);
131   },
132
133   /**
134    * Sets the enabled state of the confirmation dialog
135    *
136    * @param {boolean} value
137    *        New enabled state of the confirmation dialog
138    */
139   set showPrompt(value){
140     prefs.preferences.setPref(PB_NO_PROMPT_PREF, !value);
141   },
142
143   /**
144    * Gets all the needed external DTD urls as an array
145    *
146    * @returns Array of external DTD urls
147    * @type [string]
148    */
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"];
153     return dtds;
154   },
155
156   /**
157    * Turn off Private Browsing mode and reset all changes
158    */
159   reset : function privateBrowsing_reset() {
160     try {
161       this.stop(true);
162     } catch (ex) {
163       // Do a hard reset
164       this.enabled = false;
165     }
166
167     this.showPrompt = true;
168   },
169
170   /**
171    * Start the Private Browsing mode
172    *
173    * @param {boolean} useShortcut
174    *        Use the keyboard shortcut if true otherwise the menu entry is used
175    */
176   start: function privateBrowsing_start(useShortcut) {
177     var dialog = null;
178
179     if (this.enabled)
180       return;
181
182     if (this.showPrompt) {
183       dialog = new modalDialog.modalDialog(this._controller.window);
184       dialog.start(this._handler);
185     }
186
187     if (useShortcut) {
188       var cmdKey = utils.getEntity(this.getDtds(), "privateBrowsingCmd.commandkey");
189       this._controller.keypress(null, cmdKey, {accelKey: true, shiftKey: true});
190     } else {
191       this._controller.click(this._pbMenuItem);
192     }
193
194     if (dialog) {
195       dialog.waitForDialog();
196     }
197     this.waitForTransistionComplete(true);
198   },
199
200   /**
201    * Stop the Private Browsing mode
202    *
203    * @param {boolean} useShortcut
204    *        Use the keyboard shortcut if true otherwise the menu entry is used
205    */
206   stop: function privateBrowsing_stop(useShortcut)
207   {
208     if (!this.enabled)
209       return;
210
211     if (useShortcut) {
212       var privateBrowsingCmdKey = utils.getEntity(this.getDtds(), "privateBrowsingCmd.commandkey");
213       this._controller.keypress(null, privateBrowsingCmdKey, {accelKey: true, shiftKey: true});
214     } else {
215       this._controller.click(this._pbMenuItem);
216     }
217
218     this.waitForTransistionComplete(false);
219   },
220
221   /**
222    * Waits until the transistion into or out of the Private Browsing mode happened
223    *
224    * @param {boolean} state
225    *        Expected target state of the Private Browsing mode
226    */
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});
233   }
234 }
235
236 // Export of classes
237 exports.privateBrowsing = privateBrowsing;