Skip to content

Commit

Permalink
Merge pull request #2481 from yurydelendik/rm-global-pb
Browse files Browse the repository at this point in the history
Refactors private browsing logic
  • Loading branch information
brendandahl committed Dec 21, 2012
2 parents d58fad8 + 1e7e586 commit 4b4601d
Showing 1 changed file with 29 additions and 26 deletions.
55 changes: 29 additions & 26 deletions extensions/firefox/components/PdfStreamConverter.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,31 +30,19 @@ const PDF_CONTENT_TYPE = 'application/pdf';
const PREF_PREFIX = 'PDFJSSCRIPT_PREF_PREFIX';
const PDF_VIEWER_WEB_PAGE = 'resource://pdf.js/web/viewer.html';
const MAX_DATABASE_LENGTH = 4096;
const FIREFOX_ID = '{ec8030f7-c20a-464f-9b0e-13a3a9e97384}';

Cu.import('resource://gre/modules/XPCOMUtils.jsm');
Cu.import('resource://gre/modules/Services.jsm');
Cu.import('resource://gre/modules/NetUtil.jsm');

XPCOMUtils.defineLazyModuleGetter(this, 'PrivateBrowsingUtils',
'resource://gre/modules/PrivateBrowsingUtils.jsm');

let appInfo = Cc['@mozilla.org/xre/app-info;1']
.getService(Ci.nsIXULAppInfo);
let Svc = {};
XPCOMUtils.defineLazyServiceGetter(Svc, 'mime',
'@mozilla.org/mime;1',
'nsIMIMEService');

let isInPrivateBrowsing;
if (appInfo.ID === FIREFOX_ID) {
let privateBrowsing = Cc['@mozilla.org/privatebrowsing;1']
.getService(Ci.nsIPrivateBrowsingService);
isInPrivateBrowsing = function getInPrivateBrowsing() {
return privateBrowsing.privateBrowsingEnabled;
};
} else {
isInPrivateBrowsing = function() { return false; };
}

function getChromeWindow(domWindow) {
var containingBrowser = domWindow.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIWebNavigation)
Expand Down Expand Up @@ -219,6 +207,30 @@ function ChromeActions(domWindow, dataListener) {
}

ChromeActions.prototype = {
isInPrivateBrowsing: function() {
let docIsPrivate;
try {
docIsPrivate = PrivateBrowsingUtils.isWindowPrivate(this.domWindow);
} catch (x) {
// unable to use PrivateBrowsingUtils, e.g. FF15
}
if (typeof docIsPrivate === 'undefined') {
// per-window Private Browsing is not supported, trying global service
try {
let privateBrowsing = Cc['@mozilla.org/privatebrowsing;1']
.getService(Ci.nsIPrivateBrowsingService);
docIsPrivate = privateBrowsing.privateBrowsingEnabled;
} catch (x) {
// unable to get nsIPrivateBrowsingService (e.g. not Firefox)
docIsPrivate = false;
}
}
// caching the result
this.isInPrivateBrowsing = function isInPrivateBrowsingCached() {
return docIsPrivate;
};
return docIsPrivate;
},
download: function(data, sendResponse) {
var originalUrl = data.originalUrl;
// The data may not be downloaded so we need just retry getting the pdf with
Expand All @@ -231,16 +243,7 @@ ChromeActions.prototype = {
var frontWindow = Cc['@mozilla.org/embedcomp/window-watcher;1'].
getService(Ci.nsIWindowWatcher).activeWindow;

let docIsPrivate = false;
try {
docIsPrivate = this.domWindow
.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIWebNavigation)
.QueryInterface(Ci.nsILoadContext)
.usePrivateBrowsing;
} catch (x) {
}

let docIsPrivate = this.isInPrivateBrowsing();
let netChannel = NetUtil.newChannel(blobUri);
if ('nsIPrivateBrowsingChannel' in Ci &&
netChannel instanceof Ci.nsIPrivateBrowsingChannel) {
Expand Down Expand Up @@ -289,15 +292,15 @@ ChromeActions.prototype = {
});
},
setDatabase: function(data) {
if (isInPrivateBrowsing())
if (this.isInPrivateBrowsing())
return;
// Protect against something sending tons of data to setDatabase.
if (data.length > MAX_DATABASE_LENGTH)
return;
setStringPref(PREF_PREFIX + '.database', data);
},
getDatabase: function() {
if (isInPrivateBrowsing())
if (this.isInPrivateBrowsing())
return '{}';
return getStringPref(PREF_PREFIX + '.database', '{}');
},
Expand Down

0 comments on commit 4b4601d

Please sign in to comment.