Bug 627239 - Don't store thumbnails for https pages r=ehsan

This commit is contained in:
Raymond Lee 2011-05-31 04:02:04 +08:00
parent b1643fe520
commit 8fe5312dc3
7 changed files with 567 additions and 209 deletions

View File

@ -47,8 +47,6 @@ let Storage = {
GROUPS_DATA_IDENTIFIER: "tabview-groups",
TAB_DATA_IDENTIFIER: "tabview-tab",
UI_DATA_IDENTIFIER: "tabview-ui",
CACHE_CLIENT_IDENTIFIER: "tabview-cache",
CACHE_PREFIX: "moz-panorama:",
// ----------
// Function: toString
@ -64,28 +62,12 @@ let Storage = {
this._sessionStore =
Cc["@mozilla.org/browser/sessionstore;1"].
getService(Ci.nsISessionStore);
// Create stream-based cache session for tabview
let cacheService =
Cc["@mozilla.org/network/cache-service;1"].
getService(Ci.nsICacheService);
this._cacheSession = cacheService.createSession(
this.CACHE_CLIENT_IDENTIFIER, Ci.nsICache.STORE_ON_DISK, true);
this.StringInputStream = Components.Constructor(
"@mozilla.org/io/string-input-stream;1", "nsIStringInputStream",
"setData");
this.StorageStream = Components.Constructor(
"@mozilla.org/storagestream;1", "nsIStorageStream",
"init");
},
// ----------
// Function: uninit
uninit: function Storage_uninit () {
this._sessionStore = null;
this._cacheSession = null;
this.StringInputStream = null;
this.StorageStream = null;
},
// ----------
@ -114,137 +96,6 @@ let Storage = {
}
},
// ----------
// Function: _openCacheEntry
// Opens a cache entry for the given <url> and requests access <access>.
// Calls <successCallback>(entry) when the entry was successfully opened with
// requested access rights. Otherwise calls <errorCallback>().
_openCacheEntry: function Storage__openCacheEntry(url, access, successCallback, errorCallback) {
let onCacheEntryAvailable = function (entry, accessGranted, status) {
if (entry && access == accessGranted && Components.isSuccessCode(status)) {
successCallback(entry);
} else {
entry && entry.close();
errorCallback();
}
}
let key = this.CACHE_PREFIX + url;
// switch to synchronous mode if parent window is about to close
if (UI.isDOMWindowClosing) {
let entry = this._cacheSession.openCacheEntry(key, access, true);
let status = Components.results.NS_OK;
onCacheEntryAvailable(entry, entry.accessGranted, status);
} else {
let listener = new CacheListener(onCacheEntryAvailable);
this._cacheSession.asyncOpenCacheEntry(key, access, listener);
}
},
// ----------
// Function: saveThumbnail
// Saves the <imageData> to the cache using the given <url> as key.
// Calls <callback>(status) when finished (passing true or false indicating
// whether the operation succeeded).
saveThumbnail: function Storage_saveThumbnail(url, imageData, callback) {
Utils.assert(url, "url");
Utils.assert(imageData, "imageData");
Utils.assert(typeof callback == "function", "callback arg must be a function");
let self = this;
let StringInputStream = this.StringInputStream;
let onCacheEntryAvailable = function (entry) {
let outputStream = entry.openOutputStream(0);
let cleanup = function () {
outputStream.close();
entry.close();
}
// switch to synchronous mode if parent window is about to close
if (UI.isDOMWindowClosing) {
outputStream.write(imageData, imageData.length);
cleanup();
callback(true);
return;
}
// asynchronous mode
let inputStream = new StringInputStream(imageData, imageData.length);
gNetUtil.asyncCopy(inputStream, outputStream, function (result) {
cleanup();
inputStream.close();
callback(Components.isSuccessCode(result));
});
}
let onCacheEntryUnavailable = function () {
callback(false);
}
this._openCacheEntry(url, Ci.nsICache.ACCESS_WRITE,
onCacheEntryAvailable, onCacheEntryUnavailable);
},
// ----------
// Function: loadThumbnail
// Asynchrously loads image data from the cache using the given <url> as key.
// Calls <callback>(status, data) when finished, passing true or false
// (indicating whether the operation succeeded) and the retrieved image data.
loadThumbnail: function Storage_loadThumbnail(url, callback) {
Utils.assert(url, "url");
Utils.assert(typeof callback == "function", "callback arg must be a function");
let self = this;
let onCacheEntryAvailable = function (entry) {
let imageChunks = [];
let nativeInputStream = entry.openInputStream(0);
const CHUNK_SIZE = 0x10000; // 65k
const PR_UINT32_MAX = 0xFFFFFFFF;
let storageStream = new self.StorageStream(CHUNK_SIZE, PR_UINT32_MAX, null);
let storageOutStream = storageStream.getOutputStream(0);
let cleanup = function () {
nativeInputStream.close();
storageStream.close();
storageOutStream.close();
entry.close();
}
gNetUtil.asyncCopy(nativeInputStream, storageOutStream, function (result) {
// cancel if parent window has already been closed
if (typeof UI == "undefined") {
cleanup();
return;
}
let imageData = null;
let isSuccess = Components.isSuccessCode(result);
if (isSuccess) {
let storageInStream = storageStream.newInputStream(0);
imageData = gNetUtil.readInputStreamToString(storageInStream,
storageInStream.available());
storageInStream.close();
}
cleanup();
callback(isSuccess, imageData);
});
}
let onCacheEntryUnavailable = function () {
callback(false);
}
this._openCacheEntry(url, Ci.nsICache.ACCESS_READ,
onCacheEntryAvailable, onCacheEntryUnavailable);
},
// ----------
// Function: saveTab
// Saves the data for a single tab.
@ -255,16 +106,9 @@ let Storage = {
let imageData = data.imageData;
// Remove imageData from payload
delete data.imageData;
if (imageData != null) {
this.saveThumbnail(data.url, imageData, function (status) {
if (status) {
// Notify subscribers
tab._tabViewTabItem._sendToSubscribers("savedCachedImageData");
} else {
Utils.log("Error while saving thumbnail: " + e);
}
});
}
if (imageData != null)
ThumbnailStorage.saveThumbnail(tab, imageData);
}
this._sessionStore.setTabValue(tab, this.TAB_DATA_IDENTIFIER,
@ -292,18 +136,13 @@ let Storage = {
}
if (existingData) {
this.loadThumbnail(existingData.url, function (status, imageData) {
if (status) {
ThumbnailStorage.loadThumbnail(
tab, existingData.url,
function(status, imageData) {
callback(imageData);
// Notify subscribers
tab._tabViewTabItem._sendToSubscribers("loadedCachedImageData");
} else {
Utils.log("Error while loading thumbnail");
}
});
);
}
return existingData;
},
@ -409,26 +248,3 @@ let Storage = {
}
};
// ##########
// Class: CacheListener
// Generic CacheListener for feeding to asynchronous cache calls.
// Calls <callback>(entry, access, status) when the requested cache entry
// is available.
function CacheListener(callback) {
Utils.assert(typeof callback == "function", "callback arg must be a function");
this.callback = callback;
};
CacheListener.prototype = {
// ----------
// Function: toString
// Prints [CacheListener] for debug use
toString: function CacheListener_toString() {
return "[CacheListener]";
},
QueryInterface: XPCOMUtils.generateQI([Ci.nsICacheListener]),
onCacheEntryAvailable: function (entry, access, status) {
this.callback(entry, access, status);
}
};

View File

@ -1,6 +1,7 @@
const Cc = Components.classes;
const Ci = Components.interfaces;
const Cu = Components.utils;
const Cr = Components.results;
Cu.import("resource:///modules/tabview/AllTabs.jsm");
Cu.import("resource:///modules/tabview/utils.jsm");
@ -50,5 +51,6 @@ var gTabViewFrame = gWindow.document.getElementById("tab-view");
#include tabitems.js
#include drag.js
#include trench.js
#include thumbnailStorage.js
#include ui.js
#include search.js

View File

@ -0,0 +1,373 @@
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is thumbnailStorage.js.
*
* The Initial Developer of the Original Code is
* the Mozilla Foundation.
* Portions created by the Initial Developer are Copyright (C) 2011
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Raymond Lee <raymond@appcoast.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
// **********
// Title: thumbnailStorage.js
// ##########
// Class: ThumbnailStorage
// Singleton for persistent storage of thumbnail data.
let ThumbnailStorage = {
CACHE_CLIENT_IDENTIFIER: "tabview-cache",
CACHE_PREFIX: "moz-panorama:",
PREF_DISK_CACHE_SSL: "browser.cache.disk_cache_ssl",
// Holds the cache session reference
_cacheSession: null,
// Holds the string input stream reference
_stringInputStream: null,
// Holds the storage stream reference
_storageStream: null,
// Holds the progress listener reference
_progressListener: null,
// Used to keep track of disk_cache_ssl preference
enablePersistentHttpsCaching: null,
// Used to keep track of browsers whose thumbs we shouldn't save
excludedBrowsers: [],
// ----------
// Function: toString
// Prints [ThumbnailStorage] for debug use.
toString: function ThumbnailStorage_toString() {
return "[ThumbnailStorage]";
},
// ----------
// Function: init
// Should be called when UI is initialized.
init: function ThumbnailStorage_init() {
// Create stream-based cache session for tabview
let cacheService =
Cc["@mozilla.org/network/cache-service;1"].
getService(Ci.nsICacheService);
this._cacheSession = cacheService.createSession(
this.CACHE_CLIENT_IDENTIFIER, Ci.nsICache.STORE_ON_DISK, true);
this._stringInputStream = Components.Constructor(
"@mozilla.org/io/string-input-stream;1", "nsIStringInputStream",
"setData");
this._storageStream = Components.Constructor(
"@mozilla.org/storagestream;1", "nsIStorageStream",
"init");
// store the preference value
this.enablePersistentHttpsCaching =
Services.prefs.getBoolPref(this.PREF_DISK_CACHE_SSL);
Services.prefs.addObserver(this.PREF_DISK_CACHE_SSL, this, false);
let self = this;
// tabs are already loaded before UI is initialized so cache-control
// values are unknown. We add browsers with https to the list for now.
gBrowser.browsers.forEach(function(browser) {
let checkAndAddToList = function(browserObj) {
if (!self.enablePersistentHttpsCaching &&
browserObj.currentURI.schemeIs("https"))
self.excludedBrowsers.push(browserObj);
};
if (browser.contentDocument.readyState != "complete" ||
browser.webProgress.isLoadingDocument) {
browser.addEventListener("load", function() {
browser.removeEventListener("load", arguments.callee, true);
checkAndAddToList(browser);
}, true);
} else {
checkAndAddToList(browser);
}
});
gBrowser.addTabsProgressListener(this);
},
// Function: uninit
// Should be called when window is unloaded.
uninit: function ThumbnailStorage_uninit() {
gBrowser.removeTabsProgressListener(this);
Services.prefs.removeObserver(this.PREF_DISK_CACHE_SSL, this);
},
// ----------
// Function: _openCacheEntry
// Opens a cache entry for the given <url> and requests access <access>.
// Calls <successCallback>(entry) when the entry was successfully opened with
// requested access rights. Otherwise calls <errorCallback>().
_openCacheEntry: function ThumbnailStorage__openCacheEntry(url, access, successCallback, errorCallback) {
let onCacheEntryAvailable = function(entry, accessGranted, status) {
if (entry && access == accessGranted && Components.isSuccessCode(status)) {
successCallback(entry);
} else {
entry && entry.close();
errorCallback();
}
}
let key = this.CACHE_PREFIX + url;
// switch to synchronous mode if parent window is about to close
if (UI.isDOMWindowClosing) {
let entry = this._cacheSession.openCacheEntry(key, access, true);
let status = Cr.NS_OK;
onCacheEntryAvailable(entry, entry.accessGranted, status);
} else {
let listener = new CacheListener(onCacheEntryAvailable);
this._cacheSession.asyncOpenCacheEntry(key, access, listener);
}
},
// Function: _shouldSaveThumbnail
// Checks whether to save tab's thumbnail or not.
_shouldSaveThumbnail : function ThumbnailStorage__shouldSaveThumbnail(tab) {
return (this.excludedBrowsers.indexOf(tab.linkedBrowser) == -1);
},
// ----------
// Function: saveThumbnail
// Saves the <imageData> to the cache using the given <url> as key.
// Calls <callback>(status, data) when finished, passing true or false
// (indicating whether the operation succeeded).
saveThumbnail: function ThumbnailStorage_saveThumbnail(tab, imageData, callback) {
Utils.assert(tab, "tab");
Utils.assert(imageData, "imageData");
if (!this._shouldSaveThumbnail(tab)) {
tab._tabViewTabItem._sendToSubscribers("deniedToCacheImageData");
if (callback)
callback(false);
return;
}
let self = this;
let completed = function(status) {
if (callback)
callback(status);
if (status) {
// Notify subscribers
tab._tabViewTabItem._sendToSubscribers("savedCachedImageData");
} else {
Utils.log("Error while saving thumbnail: " + e);
}
};
let onCacheEntryAvailable = function(entry) {
let outputStream = entry.openOutputStream(0);
let cleanup = function() {
outputStream.close();
entry.close();
}
// switch to synchronous mode if parent window is about to close
if (UI.isDOMWindowClosing) {
outputStream.write(imageData, imageData.length);
cleanup();
completed(true);
return;
}
// asynchronous mode
let inputStream = new self._stringInputStream(imageData, imageData.length);
gNetUtil.asyncCopy(inputStream, outputStream, function (result) {
cleanup();
inputStream.close();
completed(Components.isSuccessCode(result));
});
}
let onCacheEntryUnavailable = function() {
completed(false);
}
this._openCacheEntry(tab.linkedBrowser.currentURI.spec,
Ci.nsICache.ACCESS_WRITE, onCacheEntryAvailable,
onCacheEntryUnavailable);
},
// ----------
// Function: loadThumbnail
// Asynchrously loads image data from the cache using the given <url> as key.
// Calls <callback>(status, data) when finished, passing true or false
// (indicating whether the operation succeeded) and the retrieved image data.
loadThumbnail: function ThumbnailStorage_loadThumbnail(tab, url, callback) {
Utils.assert(tab, "tab");
Utils.assert(url, "url");
Utils.assert(typeof callback == "function", "callback arg must be a function");
let self = this;
let completed = function(status, imageData) {
callback(status, imageData);
if (status) {
// Notify subscribers
tab._tabViewTabItem._sendToSubscribers("loadedCachedImageData");
} else {
Utils.log("Error while loading thumbnail");
}
}
let onCacheEntryAvailable = function(entry) {
let imageChunks = [];
let nativeInputStream = entry.openInputStream(0);
const CHUNK_SIZE = 0x10000; // 65k
const PR_UINT32_MAX = 0xFFFFFFFF;
let storageStream = new self._storageStream(CHUNK_SIZE, PR_UINT32_MAX, null);
let storageOutStream = storageStream.getOutputStream(0);
let cleanup = function () {
nativeInputStream.close();
storageStream.close();
storageOutStream.close();
entry.close();
}
gNetUtil.asyncCopy(nativeInputStream, storageOutStream, function (result) {
// cancel if parent window has already been closed
if (typeof UI == "undefined") {
cleanup();
return;
}
let imageData = null;
let isSuccess = Components.isSuccessCode(result);
if (isSuccess) {
let storageInStream = storageStream.newInputStream(0);
imageData = gNetUtil.readInputStreamToString(storageInStream,
storageInStream.available());
storageInStream.close();
}
cleanup();
completed(isSuccess, imageData);
});
}
let onCacheEntryUnavailable = function() {
completed(false);
}
this._openCacheEntry(url, Ci.nsICache.ACCESS_READ,
onCacheEntryAvailable, onCacheEntryUnavailable);
},
// ----------
// Function: observe
// Implements the observer interface.
observe: function ThumbnailStorage_observe(subject, topic, data) {
this.enablePersistentHttpsCaching =
Services.prefs.getBoolPref(this.PREF_DISK_CACHE_SSL);
},
// ----------
// Implements progress listener interface.
QueryInterface: XPCOMUtils.generateQI([Ci.nsIWebProgressListener,
Ci.nsISupportsWeakReference,
Ci.nsISupports]),
onStateChange: function ThumbnailStorage_onStateChange(
browser, webProgress, request, flag, status) {
if (flag & Ci.nsIWebProgressListener.STATE_START &&
flag & Ci.nsIWebProgressListener.STATE_IS_WINDOW) {
// ensure the dom window is the top one
if (webProgress.DOMWindow.parent == webProgress.DOMWindow) {
let index = this.excludedBrowsers.indexOf(browser);
if (index != -1)
this.excludedBrowsers.splice(index, 1);
}
}
if (flag & Ci.nsIWebProgressListener.STATE_STOP &&
flag & Ci.nsIWebProgressListener.STATE_IS_WINDOW) {
// ensure the dom window is the top one
if (webProgress.DOMWindow.parent == webProgress.DOMWindow &&
request && request instanceof Ci.nsIHttpChannel) {
request.QueryInterface(Ci.nsIHttpChannel);
let inhibitPersistentThumb = false;
if (request.isNoStoreResponse()) {
inhibitPersistentThumb = true;
} else if (!this.enablePersistentHttpsCaching &&
request.URI.schemeIs("https")) {
let cacheControlHeader;
try {
cacheControlHeader = request.getResponseHeader("Cache-Control");
} catch(e) {
// this error would occur when "Cache-Control" doesn't exist in
// the eaders
}
if (cacheControlHeader && !(/public/i).test(cacheControlHeader))
inhibitPersistentThumb = true;
}
if (inhibitPersistentThumb &&
this.excludedBrowsers.indexOf(browser) == -1)
this.excludedBrowsers.push(browser);
}
}
}
}
// ##########
// Class: CacheListener
// Generic CacheListener for feeding to asynchronous cache calls.
// Calls <callback>(entry, access, status) when the requested cache entry
// is available.
function CacheListener(callback) {
Utils.assert(typeof callback == "function", "callback arg must be a function");
this.callback = callback;
};
CacheListener.prototype = {
// ----------
// Function: toString
// Prints [CacheListener] for debug use
toString: function CacheListener_toString() {
return "[CacheListener]";
},
QueryInterface: XPCOMUtils.generateQI([Ci.nsICacheListener]),
onCacheEntryAvailable: function CacheListener_onCacheEntryAvailable(
entry, access, status) {
this.callback(entry, access, status);
}
};

View File

@ -157,6 +157,9 @@ let UI = {
// initialize the direction of the page
this._initPageDirection();
// ___ thumbnail storage
ThumbnailStorage.init();
// ___ storage
Storage.init();
let data = Storage.readUIData(gWindow);
@ -286,6 +289,8 @@ let UI = {
}
},
// Function: uninit
// Should be called when window is unloaded.
uninit: function UI_uninit() {
// call our cleanup functions
this._cleanupFunctions.forEach(function(func) {
@ -297,6 +302,7 @@ let UI = {
TabItems.uninit();
GroupItems.uninit();
Storage.uninit();
ThumbnailStorage.uninit();
this._removeTabActionHandlers();
this._currentTab = null;
@ -680,22 +686,22 @@ let UI = {
// don't reenter Panorama due to all of the session restore tab
// manipulation (which otherwise we might). When transitioning away from
// PB, we reenter Panorama if we had been there directly before PB.
function pbObserver(aSubject, aTopic, aData) {
if (aTopic == "private-browsing") {
function pbObserver(subject, topic, data) {
if (topic == "private-browsing") {
// We could probably do this in private-browsing-change-granted, but
// this seems like a nicer spot, right in the middle of the process.
if (aData == "enter") {
if (data == "enter") {
// If we are in Tab View, exit.
self._privateBrowsing.wasInTabView = self.isTabViewVisible();
if (self.isTabViewVisible())
self.goToTab(gBrowser.selectedTab);
}
} else if (aTopic == "private-browsing-change-granted") {
if (aData == "enter" || aData == "exit") {
self._privateBrowsing.transitionMode = aData;
} else if (topic == "private-browsing-change-granted") {
if (data == "enter" || data == "exit") {
self._privateBrowsing.transitionMode = data;
self.storageBusy();
}
} else if (aTopic == "private-browsing-transition-complete") {
} else if (topic == "private-browsing-transition-complete") {
// We use .transitionMode here, as aData is empty.
if (self._privateBrowsing.transitionMode == "exit" &&
self._privateBrowsing.wasInTabView)

View File

@ -113,6 +113,7 @@ _BROWSER_FILES = \
browser_tabview_bug626368.js \
browser_tabview_bug626525.js \
browser_tabview_bug626791.js \
browser_tabview_bug627239.js \
browser_tabview_bug627288.js \
browser_tabview_bug627736.js \
browser_tabview_bug628061.js \

View File

@ -2,8 +2,9 @@
http://creativecommons.org/publicdomain/zero/1.0/ */
function test() {
let url = "http://non.existant/url";
let url = "http://www.example.com/";
let cw;
let tab = gBrowser.tabs[0];
let finishTest = function () {
is(1, gBrowser.tabs.length, "there is one tab, only");
@ -14,7 +15,7 @@ function test() {
waitForExplicitFinish();
let testErroneousLoading = function () {
cw.Storage.loadThumbnail(url, function (status, data) {
cw.ThumbnailStorage.loadThumbnail(tab, url, function (status, data) {
ok(!status, "thumbnail entry failed to load");
is(null, data, "no thumbnail data received");
next();
@ -25,11 +26,11 @@ function test() {
let saved = false;
let data = "thumbnail-data-asynchronous";
cw.Storage.saveThumbnail(url, data, function (status) {
cw.ThumbnailStorage.saveThumbnail(tab, data, function (status) {
ok(status, "thumbnail entry was saved");
ok(saved, "thumbnail was saved asynchronously");
cw.Storage.loadThumbnail(url, function (status, imageData) {
cw.ThumbnailStorage.loadThumbnail(tab, url, function (status, imageData) {
ok(status, "thumbnail entry was loaded");
is(imageData, data, "valid thumbnail data received");
next();
@ -46,11 +47,11 @@ function test() {
cw.UI.isDOMWindowClosing = true;
registerCleanupFunction(function () cw.UI.isDOMWindowClosing = false);
cw.Storage.saveThumbnail(url, data, function (status) {
cw.ThumbnailStorage.saveThumbnail(tab, data, function (status) {
ok(status, "thumbnail entry was saved");
ok(!saved, "thumbnail was saved synchronously");
cw.Storage.loadThumbnail(url, function (status, imageData) {
cw.ThumbnailStorage.loadThumbnail(tab, url, function (status, imageData) {
ok(status, "thumbnail entry was loaded");
is(imageData, data, "valid thumbnail data received");
@ -72,10 +73,13 @@ function test() {
hideTabView(finishTest);
}
tab.linkedBrowser.loadURI(url);
afterAllTabsLoaded(function() {
showTabView(function () {
registerCleanupFunction(function () TabView.hide());
cw = TabView.getContentWindow();
next();
});
});
}

View File

@ -0,0 +1,156 @@
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
let contentWindow;
let enablePersistentHttpsCaching;
let newTab;
function test() {
waitForExplicitFinish();
newTab = gBrowser.addTab();
HttpRequestObserver.register();
registerCleanupFunction(function () {
HttpRequestObserver.unregister();
if (gBrowser.tabs[1])
gBrowser.removeTab(gBrowser.tabs[1]);
hideTabView(function () {});
contentWindow.ThumbnailStorage.enablePersistentHttpsCaching =
enablePersistentHttpsCaching;
});
showTabView(function() {
contentWindow = TabView.getContentWindow();
test1();
});
}
function test1() {
// page with cache-control: no-store, should not save thumbnail
HttpRequestObserver.cacheControlValue = "no-store";
newTab.linkedBrowser.loadURI("http://www.example.com/browser/browser/base/content/test/tabview/dummy_page.html");
afterAllTabsLoaded(function() {
let tabItem = newTab._tabViewTabItem;
ok(!contentWindow.ThumbnailStorage._shouldSaveThumbnail(newTab),
"Should not save the thumbnail for tab");
tabItem.addSubscriber(tabItem, "deniedToCacheImageData", function() {
tabItem.removeSubscriber(tabItem, "deniedToCacheImageData");
test2();
});
tabItem.save(true);
HttpRequestObserver.cacheControlValue = null;
});
}
function test2() {
// page with cache-control: private, should save thumbnail
HttpRequestObserver.cacheControlValue = "private";
newTab.linkedBrowser.loadURI("http://www.example.com/");
afterAllTabsLoaded(function() {
let tabItem = newTab._tabViewTabItem;
ok(contentWindow.ThumbnailStorage._shouldSaveThumbnail(newTab),
"Should save the thumbnail for tab");
tabItem.addSubscriber(tabItem, "savedCachedImageData", function() {
tabItem.removeSubscriber(tabItem, "savedCachedImageData");
test3();
});
tabItem.save(true);
});
}
function test3() {
// page with cache-control: private with https caching enabled, should save thumbnail
HttpRequestObserver.cacheControlValue = "private";
enablePersistentHttpsCaching =
contentWindow.ThumbnailStorage.enablePersistentHttpsCaching;
contentWindow.ThumbnailStorage.enablePersistentHttpsCaching = true;
newTab.linkedBrowser.loadURI("https://example.com/browser/browser/base/content/test/tabview/dummy_page.html");
afterAllTabsLoaded(function() {
let tabItem = newTab._tabViewTabItem;
ok(contentWindow.ThumbnailStorage._shouldSaveThumbnail(newTab),
"Should save the thumbnail for tab");
tabItem.addSubscriber(tabItem, "savedCachedImageData", function() {
tabItem.removeSubscriber(tabItem, "savedCachedImageData");
test4();
});
tabItem.save(true);
});
}
function test4() {
// page with cache-control: public with https caching disabled, should save thumbnail
HttpRequestObserver.cacheControlValue = "public";
contentWindow.ThumbnailStorage.enablePersistentHttpsCaching = false;
newTab.linkedBrowser.loadURI("https://example.com/browser/browser/base/content/test/tabview/");
afterAllTabsLoaded(function() {
let tabItem = newTab._tabViewTabItem;
ok(contentWindow.ThumbnailStorage._shouldSaveThumbnail(newTab),
"Should save the thumbnail for tab");
tabItem.addSubscriber(tabItem, "savedCachedImageData", function() {
tabItem.removeSubscriber(tabItem, "savedCachedImageData");
test5();
});
tabItem.save(true);
});
}
function test5() {
// page with cache-control: private with https caching disabled, should not save thumbnail
HttpRequestObserver.cacheControlValue = "private";
newTab.linkedBrowser.loadURI("https://example.com/");
afterAllTabsLoaded(function() {
let tabItem = newTab._tabViewTabItem;
ok(!contentWindow.ThumbnailStorage._shouldSaveThumbnail(newTab),
"Should not the thumbnail for tab");
tabItem.addSubscriber(tabItem, "deniedToCacheImageData", function() {
tabItem.removeSubscriber(tabItem, "deniedToCacheImageData");
hideTabView(function () {
gBrowser.removeTab(gBrowser.tabs[1]);
finish();
});
});
tabItem.save(true);
});
}
let HttpRequestObserver = {
cacheControlValue: null,
observe: function(subject, topic, data) {
if (topic == "http-on-examine-response" && this.cacheControlValue) {
let httpChannel = subject.QueryInterface(Ci.nsIHttpChannel);
httpChannel.setResponseHeader("Cache-Control", this.cacheControlValue, false);
}
},
register: function() {
Services.obs.addObserver(this, "http-on-examine-response", false);
},
unregister: function() {
Services.obs.removeObserver(this, "http-on-examine-response");
}
};