Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactors private browsing logic #2481

Merged
merged 1 commit into from Dec 21, 2012
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
55 changes: 29 additions & 26 deletions extensions/firefox/components/PdfStreamConverter.js
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