Browse Source

firefox: cookie and browser cache management

pull/2/head
gorhill 10 years ago
parent
commit
a49340b57a
  1. 39
      platform/chromium/vapi-background.js
  2. 104
      platform/firefox/vapi-background.js
  3. 2
      src/background.html
  4. 7
      src/js/browsercache.js
  5. 10
      src/js/cookies.js

39
platform/chromium/vapi-background.js

@ -473,6 +473,7 @@ vAPI.setIcon = function(tabId, iconId, badge) {
chrome.browserAction.setIcon({ tabId: tabId, path: iconPaths }, onIconReady); chrome.browserAction.setIcon({ tabId: tabId, path: iconPaths }, onIconReady);
}; };
/******************************************************************************/
/******************************************************************************/ /******************************************************************************/
vAPI.messaging = { vAPI.messaging = {
@ -625,6 +626,7 @@ CallbackWrapper.prototype.proxy = function(response) {
CallbackWrapper.junkyard.push(this); CallbackWrapper.junkyard.push(this);
}; };
/******************************************************************************/
/******************************************************************************/ /******************************************************************************/
vAPI.net = {}; vAPI.net = {};
@ -812,6 +814,7 @@ vAPI.net.registerListeners = function() {
// End of: Network event handlers // End of: Network event handlers
}; };
/******************************************************************************/
/******************************************************************************/ /******************************************************************************/
vAPI.contextMenu = { vAPI.contextMenu = {
@ -833,6 +836,7 @@ vAPI.lastError = function() {
return chrome.runtime.lastError; return chrome.runtime.lastError;
}; };
/******************************************************************************/
/******************************************************************************/ /******************************************************************************/
// This is called only once, when everything has been loaded in memory after // This is called only once, when everything has been loaded in memory after
@ -843,6 +847,7 @@ vAPI.lastError = function() {
vAPI.onLoadAllCompleted = function() { vAPI.onLoadAllCompleted = function() {
}; };
/******************************************************************************/
/******************************************************************************/ /******************************************************************************/
vAPI.punycodeHostname = function(hostname) { vAPI.punycodeHostname = function(hostname) {
@ -853,30 +858,48 @@ vAPI.punycodeURL = function(url) {
return url; return url;
}; };
/******************************************************************************/
/******************************************************************************/ /******************************************************************************/
vAPI.browserCache = {};
vAPI.browserData = {};
/******************************************************************************/ /******************************************************************************/
vAPI.browserCache.clearByTime = function(since) {
chrome.browsingData.removeCache({ since: 0 });
// https://developer.chrome.com/extensions/browsingData
vAPI.browserData.clearCache = function(callback) {
chrome.browsingData.removeCache({ since: 0 }, callback);
}; };
vAPI.browserCache.clearByOrigin = function(/* domain */) {
/******************************************************************************/
// Not supported on Chromium
vAPI.browserData.clearOrigin = function(domain, callback) {
// unsupported on Chromium // unsupported on Chromium
if ( typeof callback === 'function' ) {
callback(undefined);
}
}; };
/******************************************************************************/ /******************************************************************************/
/******************************************************************************/
// https://developer.chrome.com/extensions/cookies
vAPI.cookies = {}; vAPI.cookies = {};
/******************************************************************************/ /******************************************************************************/
vAPI.cookies.registerListeners = function() {
if ( typeof this.onChanged === 'function' ) {
chrome.cookies.onChanged.addListener(this.onChanged);
vAPI.cookies.start = function() {
var onChanged = function(changeInfo) {
var handler = changeInfo.removed ? this.onRemoved : this.onChanged;
if ( typeof handler !== 'function' ) {
return;
} }
handler(changeInfo.cookie);
};
chrome.cookies.onChanged.addListener(onChanged.bind(this));
}; };
/******************************************************************************/ /******************************************************************************/
@ -890,6 +913,8 @@ vAPI.cookies.getAll = function(callback) {
vAPI.cookies.remove = function(details, callback) { vAPI.cookies.remove = function(details, callback) {
chrome.cookies.remove(details, callback || noopFunc); chrome.cookies.remove(details, callback || noopFunc);
}; };
/******************************************************************************/
/******************************************************************************/ /******************************************************************************/
})(); })();

104
platform/firefox/vapi-background.js

@ -35,6 +35,7 @@
// Useful links // Useful links
// //
// https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interface // https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interface
// https://developer.mozilla.org/en-US/docs/Mozilla/JavaScript_code_modules/Services.jsm
/******************************************************************************/ /******************************************************************************/
@ -84,7 +85,7 @@ var cleanupTasks = [];
// Fixed by github.com/AlexVallat: // Fixed by github.com/AlexVallat:
// https://github.com/AlexVallat/uBlock/commit/7b781248f00cbe3d61b1cc367c440db80fa06049 // https://github.com/AlexVallat/uBlock/commit/7b781248f00cbe3d61b1cc367c440db80fa06049
// 7 instances of cleanupTasks.push, but one is unique to fennec, and one to desktop. // 7 instances of cleanupTasks.push, but one is unique to fennec, and one to desktop.
var expectedNumberOfCleanups = 6;
var expectedNumberOfCleanups = 7;
window.addEventListener('unload', function() { window.addEventListener('unload', function() {
for ( var cleanup of cleanupTasks ) { for ( var cleanup of cleanupTasks ) {
@ -1684,6 +1685,7 @@ vAPI.toolbarButton.updateState = function(win, tabId) {
vAPI.toolbarButton.init(); vAPI.toolbarButton.init();
/******************************************************************************/
/******************************************************************************/ /******************************************************************************/
vAPI.contextMenu = { vAPI.contextMenu = {
@ -1843,6 +1845,7 @@ vAPI.contextMenu.remove = function() {
this.onCommand = null; this.onCommand = null;
}; };
/******************************************************************************/
/******************************************************************************/ /******************************************************************************/
var optionsObserver = { var optionsObserver = {
@ -1885,12 +1888,14 @@ var optionsObserver = {
optionsObserver.register(); optionsObserver.register();
/******************************************************************************/
/******************************************************************************/ /******************************************************************************/
vAPI.lastError = function() { vAPI.lastError = function() {
return null; return null;
}; };
/******************************************************************************/
/******************************************************************************/ /******************************************************************************/
// This is called only once, when everything has been loaded in memory after // This is called only once, when everything has been loaded in memory after
@ -1899,19 +1904,15 @@ vAPI.lastError = function() {
// the web pages before uBlock was ready. // the web pages before uBlock was ready.
vAPI.onLoadAllCompleted = function() { vAPI.onLoadAllCompleted = function() {
var µb = µBlock;
for ( var tab of this.tabs.getAllSync() ) { for ( var tab of this.tabs.getAllSync() ) {
// We're insterested in only the tabs that were already loaded // We're insterested in only the tabs that were already loaded
var tabId = this.tabs.getTabId(tab);
var browser = getBrowserForTab(tab);
µb.tabContextManager.commit(tabId, browser.currentURI.asciiSpec);
µb.bindTabToPageStats(tabId, browser.currentURI.asciiSpec);
browser.messageManager.sendAsyncMessage(
getBrowserForTab(tab).messageManager.sendAsyncMessage(
location.host + '-load-completed' location.host + '-load-completed'
); );
} }
}; };
/******************************************************************************/
/******************************************************************************/ /******************************************************************************/
// Likelihood is that we do not have to punycode: given punycode overhead, // Likelihood is that we do not have to punycode: given punycode overhead,
@ -1932,48 +1933,113 @@ vAPI.punycodeURL = function(url) {
}; };
/******************************************************************************/ /******************************************************************************/
/******************************************************************************/ /******************************************************************************/
vAPI.browserCache = {};
vAPI.browserData = {};
/******************************************************************************/ /******************************************************************************/
vAPI.browserCache.clearByTime = function(since) {
// TODO
// https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsICacheService
// https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsICache
vAPI.browserData.clearCache = function(callback) {
// PURGE_DISK_DATA_ONLY:1
// PURGE_DISK_ALL:2
// PURGE_EVERYTHING:3
// However I verified that not argument does clear the cache data.
Services.cache2.clear();
if ( typeof callback === 'function' ) {
callback();
}
}; };
vAPI.browserCache.clearByOrigin = function(/* domain */) {
/******************************************************************************/
vAPI.browserData.clearOrigin = function(/* domain */) {
// TODO // TODO
}; };
/******************************************************************************/ /******************************************************************************/
/******************************************************************************/
// https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsICookie2
// https://developer.mozilla.org/en-US/docs/Observer_Notifications#Cookies
vAPI.cookies = {}; vAPI.cookies = {};
/******************************************************************************/ /******************************************************************************/
vAPI.cookies.registerListeners = function() {
// TODO
vAPI.cookies.CookieEntry = function(ffCookie) {
this.domain = ffCookie.host;
this.name = ffCookie.name;
this.path = ffCookie.path;
this.secure = ffCookie.isSecure === true;
this.session = ffCookie.expires === 0;
this.value = ffCookie.value;
};
/******************************************************************************/
vAPI.cookies.start = function() {
Services.obs.addObserver(this, 'cookie-changed', false);
cleanupTasks.push(this.stop.bind(this));
};
/******************************************************************************/
vAPI.cookies.stop = function() {
Services.obs.removeObserver(this, 'cookie-changed');
};
/******************************************************************************/
vAPI.cookies.observe = function(subject, topic, reason) {
if ( topic !== 'cookie-changed' ) {
return;
}
var handler = reason === 'deleted' ? this.onRemoved : this.onChanged;
if ( typeof handler !== 'function' ) {
return;
}
handler(new this.CookieEntry(subject.QueryInterface(Ci.nsICookie)));
}; };
/******************************************************************************/ /******************************************************************************/
// Meant and expected to be asynchronous.
vAPI.cookies.getAll = function(callback) { vAPI.cookies.getAll = function(callback) {
// TODO
if ( typeof callback === 'function' ) {
callback([]);
if ( typeof callback !== 'function' ) {
return;
}
var onAsync = function() {
var out = [];
var enumerator = Services.cookies.enumerator;
while ( enumerator.hasMoreElements() ) {
out.push(new this.CookieEntry(enumerator.getNext().QueryInterface(Ci.nsICookie)));
} }
callback(out);
};
setTimeout(onAsync.bind(this), 0);
}; };
/******************************************************************************/ /******************************************************************************/
vAPI.cookies.remove = function(details, callback) { vAPI.cookies.remove = function(details, callback) {
// TODO
var uri = Services.io.newURI(details.url, null, null);
var cookies = Services.cookies;
cookies.remove(uri.asciiHost, details.name, uri.path, false);
cookies.remove( '.' + uri.asciiHost, details.name, uri.path, false);
if ( typeof callback === 'function' ) { if ( typeof callback === 'function' ) {
callback(null);
callback({
domain: uri.asciiHost,
name: details.name,
path: uri.path
});
} }
}; };
/******************************************************************************/
/******************************************************************************/ /******************************************************************************/
})(); })();

2
src/background.html

@ -21,13 +21,13 @@
<script src="js/assets.js"></script> <script src="js/assets.js"></script>
<script src="js/httpsb.js"></script> <script src="js/httpsb.js"></script>
<script src="js/reqstats.js"></script> <script src="js/reqstats.js"></script>
<script src="js/uritools.js"></script>
<script src="js/cookies.js"></script> <script src="js/cookies.js"></script>
<script src="js/logger.js"></script> <script src="js/logger.js"></script>
<script src="js/messaging.js"></script> <script src="js/messaging.js"></script>
<script src="js/profiler.js"></script> <script src="js/profiler.js"></script>
<script src="js/storage.js"></script> <script src="js/storage.js"></script>
<script src="js/pagestats.js"></script> <script src="js/pagestats.js"></script>
<script src="js/uritools.js"></script>
<script src="js/tab.js"></script> <script src="js/tab.js"></script>
<script src="js/traffic.js"></script> <script src="js/traffic.js"></script>
<script src="js/useragent.js"></script> <script src="js/useragent.js"></script>

7
src/js/browsercache.js

@ -44,14 +44,15 @@ var clearCache = function() {
return; return;
} }
vAPI.browserData.clearCache();
µm.clearBrowserCacheCycle = µm.userSettings.clearBrowserCacheAfter; µm.clearBrowserCacheCycle = µm.userSettings.clearBrowserCacheAfter;
µm.browserCacheClearedCounter++; µm.browserCacheClearedCounter++;
vAPI.browserCache.clearByTime(0);
// TODO: i18n
µm.logger.writeOne('', 'info', 'browser cache cleared'); µm.logger.writeOne('', 'info', 'browser cache cleared');
//console.debug('clearBrowserCacheCallback()> vAPI.browserCache.clearByTime() called');
//console.debug('clearBrowserCacheCallback()> vAPI.browserData.clearCache() called');
}; };
setTimeout(clearCache, 15 * 60 * 1000); setTimeout(clearCache, 15 * 60 * 1000);

10
src/js/cookies.js

@ -496,13 +496,7 @@ var canRemoveCookie = function(cookieKey, srcHostnames) {
// Listen to any change in cookieland, we will update page stats accordingly. // Listen to any change in cookieland, we will update page stats accordingly.
vAPI.cookies.onChanged = function(changeInfo) {
if ( changeInfo.removed ) {
return;
}
var cookie = changeInfo.cookie;
vAPI.cookies.onChanged = function(cookie) {
// rhill 2013-12-11: If cookie value didn't change, no need to record. // rhill 2013-12-11: If cookie value didn't change, no need to record.
// https://github.com/gorhill/httpswitchboard/issues/79 // https://github.com/gorhill/httpswitchboard/issues/79
var cookieKey = cookieKeyFromCookie(cookie); var cookieKey = cookieKeyFromCookie(cookie);
@ -536,7 +530,7 @@ vAPI.cookies.onChanged = function(changeInfo) {
/******************************************************************************/ /******************************************************************************/
vAPI.cookies.getAll(addCookiesToDict); vAPI.cookies.getAll(addCookiesToDict);
vAPI.cookies.registerListeners();
vAPI.cookies.start();
µm.asyncJobs.add('cookieHunterRemove', null, processRemoveQueue, 2 * 60 * 1000, true); µm.asyncJobs.add('cookieHunterRemove', null, processRemoveQueue, 2 * 60 * 1000, true);
µm.asyncJobs.add('cookieHunterClean', null, processClean, 10 * 60 * 1000, true); µm.asyncJobs.add('cookieHunterClean', null, processClean, 10 * 60 * 1000, true);

Loading…
Cancel
Save