addon-sdk/source/lib/sdk/private-browsing/utils.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/addon-sdk/source/lib/sdk/private-browsing/utils.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,104 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +'use strict';
     1.8 +
     1.9 +module.metadata = {
    1.10 +  "stability": "unstable"
    1.11 +};
    1.12 +
    1.13 +const { Cc, Ci, Cu } = require('chrome');
    1.14 +const { defer } = require('../lang/functional');
    1.15 +const { emit, on, once, off } = require('../event/core');
    1.16 +const { when: unload } = require('../system/unload');
    1.17 +const events = require('../system/events');
    1.18 +const { deprecateFunction } = require('../util/deprecate');
    1.19 +const { isOneOf, is, satisfiesVersion, version } = require('../system/xul-app');
    1.20 +const { isWindowPrivate } = require('../window/utils');
    1.21 +const { isPrivateBrowsingSupported } = require('../self');
    1.22 +
    1.23 +let deferredEmit = defer(emit);
    1.24 +let pbService;
    1.25 +let PrivateBrowsingUtils;
    1.26 +
    1.27 +// Private browsing is only supported in Fx
    1.28 +if (isOneOf(['Firefox', 'Fennec'])) {
    1.29 +  // get the nsIPrivateBrowsingService if it exists
    1.30 +  try {
    1.31 +    pbService = Cc["@mozilla.org/privatebrowsing;1"].
    1.32 +                getService(Ci.nsIPrivateBrowsingService);
    1.33 +
    1.34 +    // a dummy service exists for the moment (Fx20 atleast), but will be removed eventually
    1.35 +    // ie: the service will exist, but it won't do anything and the global private browing
    1.36 +    //     feature is not really active.  See Bug 818800 and Bug 826037
    1.37 +    if (!('privateBrowsingEnabled' in pbService))
    1.38 +      pbService = undefined;
    1.39 +  }
    1.40 +  catch(e) { /* Private Browsing Service has been removed (Bug 818800) */ }
    1.41 +
    1.42 +  try {
    1.43 +    PrivateBrowsingUtils = Cu.import('resource://gre/modules/PrivateBrowsingUtils.jsm', {}).PrivateBrowsingUtils;
    1.44 +  }
    1.45 +  catch(e) { /* if this file DNE then an error will be thrown */ }
    1.46 +}
    1.47 +
    1.48 +// checks that global private browsing is implemented
    1.49 +let isGlobalPBSupported = exports.isGlobalPBSupported = !!pbService && is('Firefox');
    1.50 +
    1.51 +// checks that per-window private browsing is implemented
    1.52 +let isWindowPBSupported = exports.isWindowPBSupported =
    1.53 +                          !pbService && !!PrivateBrowsingUtils && is('Firefox');
    1.54 +
    1.55 +// checks that per-tab private browsing is implemented
    1.56 +let isTabPBSupported = exports.isTabPBSupported =
    1.57 +                       !pbService && !!PrivateBrowsingUtils && is('Fennec') && satisfiesVersion(version, '>=20.0*');
    1.58 +
    1.59 +exports.isPermanentPrivateBrowsing = function() {
    1.60 + return !!(PrivateBrowsingUtils && PrivateBrowsingUtils.permanentPrivateBrowsing);
    1.61 +}
    1.62 +                       
    1.63 +function ignoreWindow(window) {
    1.64 +  return !isPrivateBrowsingSupported && isWindowPrivate(window) && !isGlobalPBSupported;
    1.65 +}
    1.66 +exports.ignoreWindow = ignoreWindow;
    1.67 +
    1.68 +function onChange() {
    1.69 +  // Emit event with in next turn of event loop.
    1.70 +  deferredEmit(exports, pbService.privateBrowsingEnabled ? 'start' : 'stop');
    1.71 +}
    1.72 +
    1.73 +// Currently, only Firefox implements the private browsing service.
    1.74 +if (isGlobalPBSupported) {
    1.75 +  // set up an observer for private browsing switches.
    1.76 +  events.on('private-browsing-transition-complete', onChange);
    1.77 +}
    1.78 +
    1.79 +// We toggle private browsing mode asynchronously in order to work around
    1.80 +// bug 659629.  Since private browsing transitions are asynchronous
    1.81 +// anyway, this doesn't significantly change the behavior of the API.
    1.82 +let setMode = defer(function setMode(value) {
    1.83 +  value = !!value;  // Cast to boolean.
    1.84 +
    1.85 +  // default
    1.86 +  return pbService && (pbService.privateBrowsingEnabled = value);
    1.87 +});
    1.88 +exports.setMode = deprecateFunction(
    1.89 +  setMode,
    1.90 +  'require("sdk/private-browsing").activate and ' +
    1.91 +  'require("sdk/private-browsing").deactivate ' +
    1.92 +  'are deprecated.'
    1.93 +);
    1.94 +
    1.95 +let getMode = function getMode(chromeWin) {
    1.96 +  if (isWindowPrivate(chromeWin))
    1.97 +    return true;
    1.98 +
    1.99 +  // default
   1.100 +  return pbService ? pbService.privateBrowsingEnabled : false;
   1.101 +};
   1.102 +exports.getMode = getMode;
   1.103 +
   1.104 +exports.on = on.bind(null, exports);
   1.105 +
   1.106 +// Make sure listeners are cleaned up.
   1.107 +unload(function() off(exports));

mercurial