mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-10-11 20:35:50 +00:00
Bug 762094 - [Page Thumbnails] remove file cache fallback; r=felipe
This commit is contained in:
parent
ceab232006
commit
0f60bef165
@ -4,7 +4,7 @@
|
||||
|
||||
"use strict";
|
||||
|
||||
let EXPORTED_SYMBOLS = ["PageThumbs", "PageThumbsStorage", "PageThumbsCache"];
|
||||
let EXPORTED_SYMBOLS = ["PageThumbs", "PageThumbsStorage"];
|
||||
|
||||
const Cu = Components.utils;
|
||||
const Cc = Components.classes;
|
||||
@ -533,63 +533,3 @@ let PageThumbsHistoryObserver = {
|
||||
|
||||
QueryInterface: XPCOMUtils.generateQI([Ci.nsINavHistoryObserver])
|
||||
};
|
||||
|
||||
/**
|
||||
* A singleton handling the storage of page thumbnails.
|
||||
*/
|
||||
let PageThumbsCache = {
|
||||
/**
|
||||
* Calls the given callback with a cache entry opened for reading.
|
||||
* @param aKey The key identifying the desired cache entry.
|
||||
* @param aCallback The callback that is called when the cache entry is ready.
|
||||
*/
|
||||
getReadEntry: function Cache_getReadEntry(aKey, aCallback) {
|
||||
// Try to open the desired cache entry.
|
||||
this._openCacheEntry(aKey, Ci.nsICache.ACCESS_READ, aCallback);
|
||||
},
|
||||
|
||||
/**
|
||||
* Opens the cache entry identified by the given key.
|
||||
* @param aKey The key identifying the desired cache entry.
|
||||
* @param aAccess The desired access mode (see nsICache.ACCESS_* constants).
|
||||
* @param aCallback The function to be called when the cache entry was opened.
|
||||
*/
|
||||
_openCacheEntry: function Cache_openCacheEntry(aKey, aAccess, aCallback) {
|
||||
function onCacheEntryAvailable(aEntry, aAccessGranted, aStatus) {
|
||||
let validAccess = aAccess == aAccessGranted;
|
||||
let validStatus = Components.isSuccessCode(aStatus);
|
||||
|
||||
// Check if a valid entry was passed and if the
|
||||
// access we requested was actually granted.
|
||||
if (aEntry && !(validAccess && validStatus)) {
|
||||
aEntry.close();
|
||||
aEntry = null;
|
||||
}
|
||||
|
||||
aCallback(aEntry);
|
||||
}
|
||||
|
||||
let listener = this._createCacheListener(onCacheEntryAvailable);
|
||||
this._cacheSession.asyncOpenCacheEntry(aKey, aAccess, listener);
|
||||
},
|
||||
|
||||
/**
|
||||
* Returns a cache listener implementing the nsICacheListener interface.
|
||||
* @param aCallback The callback to be called when the cache entry is available.
|
||||
* @return The new cache listener.
|
||||
*/
|
||||
_createCacheListener: function Cache_createCacheListener(aCallback) {
|
||||
return {
|
||||
onCacheEntryAvailable: aCallback,
|
||||
QueryInterface: XPCOMUtils.generateQI([Ci.nsICacheListener])
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Define a lazy getter for the cache session.
|
||||
*/
|
||||
XPCOMUtils.defineLazyGetter(PageThumbsCache, "_cacheSession", function () {
|
||||
return Services.cache.createSession(PageThumbs.scheme,
|
||||
Ci.nsICache.STORE_ON_DISK, true);
|
||||
});
|
||||
|
@ -74,13 +74,8 @@ Protocol.prototype = {
|
||||
newChannel: function Proto_newChannel(aURI) {
|
||||
let {url} = parseURI(aURI);
|
||||
let file = PageThumbsStorage.getFileForURL(url);
|
||||
|
||||
if (file.exists()) {
|
||||
let fileuri = Services.io.newFileURI(file);
|
||||
return Services.io.newChannelFromURI(fileuri);
|
||||
}
|
||||
|
||||
return new Channel(aURI);
|
||||
let fileuri = Services.io.newFileURI(file);
|
||||
return Services.io.newChannelFromURI(fileuri);
|
||||
},
|
||||
|
||||
/**
|
||||
@ -95,299 +90,6 @@ Protocol.prototype = {
|
||||
|
||||
let NSGetFactory = XPCOMUtils.generateNSGetFactory([Protocol]);
|
||||
|
||||
/**
|
||||
* A channel implementation responsible for delivering cached thumbnails.
|
||||
*/
|
||||
function Channel(aURI) {
|
||||
this._uri = aURI;
|
||||
|
||||
// nsIChannel
|
||||
this.originalURI = aURI;
|
||||
|
||||
// nsIHttpChannel
|
||||
this._responseHeaders = {"content-type": PageThumbs.contentType};
|
||||
}
|
||||
|
||||
Channel.prototype = {
|
||||
_uri: null,
|
||||
_referrer: null,
|
||||
_canceled: false,
|
||||
_status: Cr.NS_OK,
|
||||
_isPending: false,
|
||||
_wasOpened: false,
|
||||
_responseText: "OK",
|
||||
_responseStatus: 200,
|
||||
_responseHeaders: null,
|
||||
_requestMethod: "GET",
|
||||
_requestStarted: false,
|
||||
_allowPipelining: true,
|
||||
_requestSucceeded: true,
|
||||
|
||||
/* :::::::: nsIChannel ::::::::::::::: */
|
||||
|
||||
get URI() this._uri,
|
||||
owner: null,
|
||||
notificationCallbacks: null,
|
||||
get securityInfo() null,
|
||||
|
||||
contentType: PageThumbs.contentType,
|
||||
contentCharset: null,
|
||||
contentLength: -1,
|
||||
|
||||
get contentDisposition() {
|
||||
throw (Components.returnCode = Cr.NS_ERROR_NOT_AVAILABLE);
|
||||
},
|
||||
|
||||
get contentDispositionFilename() {
|
||||
throw (Components.returnCode = Cr.NS_ERROR_NOT_AVAILABLE);
|
||||
},
|
||||
|
||||
get contentDispositionHeader() {
|
||||
throw (Components.returnCode = Cr.NS_ERROR_NOT_AVAILABLE);
|
||||
},
|
||||
|
||||
open: function Channel_open() {
|
||||
throw (Components.returnCode = Cr.NS_ERROR_NOT_IMPLEMENTED);
|
||||
},
|
||||
|
||||
asyncOpen: function Channel_asyncOpen(aListener, aContext) {
|
||||
if (this._isPending)
|
||||
throw (Components.returnCode = Cr.NS_ERROR_IN_PROGRESS);
|
||||
|
||||
if (this._wasOpened)
|
||||
throw (Components.returnCode = Cr.NS_ERROR_ALREADY_OPENED);
|
||||
|
||||
if (this._canceled)
|
||||
return (Components.returnCode = this._status);
|
||||
|
||||
this._isPending = true;
|
||||
this._wasOpened = true;
|
||||
|
||||
this._listener = aListener;
|
||||
this._context = aContext;
|
||||
|
||||
if (this.loadGroup)
|
||||
this.loadGroup.addRequest(this, null);
|
||||
|
||||
if (this._canceled)
|
||||
return;
|
||||
|
||||
let {url} = parseURI(this._uri);
|
||||
if (!url) {
|
||||
this._serveThumbnailNotFound();
|
||||
return;
|
||||
}
|
||||
|
||||
PageThumbsCache.getReadEntry(url, function (aEntry) {
|
||||
let inputStream = aEntry && aEntry.openInputStream(0);
|
||||
if (!inputStream || !inputStream.available()) {
|
||||
if (aEntry)
|
||||
aEntry.close();
|
||||
this._serveThumbnailNotFound();
|
||||
return;
|
||||
}
|
||||
|
||||
this._entry = aEntry;
|
||||
this._pump = Cc["@mozilla.org/network/input-stream-pump;1"].
|
||||
createInstance(Ci.nsIInputStreamPump);
|
||||
|
||||
this._pump.init(inputStream, -1, -1, 0, 0, true);
|
||||
this._pump.asyncRead(this, null);
|
||||
|
||||
this._trackThumbnailHitOrMiss(true);
|
||||
}.bind(this));
|
||||
},
|
||||
|
||||
/**
|
||||
* Serves a "404 Not Found" if we didn't find the requested thumbnail.
|
||||
*/
|
||||
_serveThumbnailNotFound: function Channel_serveThumbnailNotFound() {
|
||||
this._responseStatus = 404;
|
||||
this._responseText = "Not Found";
|
||||
this._requestSucceeded = false;
|
||||
|
||||
this.onStartRequest(this, null);
|
||||
this.onStopRequest(this, null, Cr.NS_OK);
|
||||
|
||||
this._trackThumbnailHitOrMiss(false);
|
||||
},
|
||||
|
||||
/**
|
||||
* Implements telemetry tracking for thumbnail cache hits and misses.
|
||||
* @param aFound Whether the thumbnail was found.
|
||||
*/
|
||||
_trackThumbnailHitOrMiss: function Channel_trackThumbnailHitOrMiss(aFound) {
|
||||
Services.telemetry.getHistogramById("FX_THUMBNAILS_HIT_OR_MISS")
|
||||
.add(aFound);
|
||||
},
|
||||
|
||||
/* :::::::: nsIStreamListener ::::::::::::::: */
|
||||
|
||||
onStartRequest: function Channel_onStartRequest(aRequest, aContext) {
|
||||
if (!this.canceled && Components.isSuccessCode(this._status))
|
||||
this._status = aRequest.status;
|
||||
|
||||
this._requestStarted = true;
|
||||
this._listener.onStartRequest(this, this._context);
|
||||
},
|
||||
|
||||
onDataAvailable: function Channel_onDataAvailable(aRequest, aContext,
|
||||
aInStream, aOffset, aCount) {
|
||||
this._listener.onDataAvailable(this, this._context, aInStream, aOffset, aCount);
|
||||
},
|
||||
|
||||
onStopRequest: function Channel_onStopRequest(aRequest, aContext, aStatus) {
|
||||
this._isPending = false;
|
||||
this._status = aStatus;
|
||||
|
||||
this._listener.onStopRequest(this, this._context, aStatus);
|
||||
this._listener = null;
|
||||
this._context = null;
|
||||
|
||||
if (this._entry)
|
||||
this._entry.close();
|
||||
|
||||
if (this.loadGroup)
|
||||
this.loadGroup.removeRequest(this, null, aStatus);
|
||||
},
|
||||
|
||||
/* :::::::: nsIRequest ::::::::::::::: */
|
||||
|
||||
get status() this._status,
|
||||
get name() this._uri.spec,
|
||||
isPending: function Channel_isPending() this._isPending,
|
||||
|
||||
loadFlags: Ci.nsIRequest.LOAD_NORMAL,
|
||||
loadGroup: null,
|
||||
|
||||
cancel: function Channel_cancel(aStatus) {
|
||||
if (this._canceled)
|
||||
return;
|
||||
|
||||
this._canceled = true;
|
||||
this._status = aStatus;
|
||||
|
||||
if (this._pump)
|
||||
this._pump.cancel(aStatus);
|
||||
},
|
||||
|
||||
suspend: function Channel_suspend() {
|
||||
if (this._pump)
|
||||
this._pump.suspend();
|
||||
},
|
||||
|
||||
resume: function Channel_resume() {
|
||||
if (this._pump)
|
||||
this._pump.resume();
|
||||
},
|
||||
|
||||
/* :::::::: nsIHttpChannel ::::::::::::::: */
|
||||
|
||||
get referrer() this._referrer,
|
||||
|
||||
set referrer(aReferrer) {
|
||||
if (this._wasOpened)
|
||||
throw (Components.returnCode = Cr.NS_ERROR_IN_PROGRESS);
|
||||
|
||||
this._referrer = aReferrer;
|
||||
},
|
||||
|
||||
get requestMethod() this._requestMethod,
|
||||
|
||||
set requestMethod(aMethod) {
|
||||
if (this._wasOpened)
|
||||
throw (Components.returnCode = Cr.NS_ERROR_IN_PROGRESS);
|
||||
|
||||
this._requestMethod = aMethod.toUpperCase();
|
||||
},
|
||||
|
||||
get allowPipelining() this._allowPipelining,
|
||||
|
||||
set allowPipelining(aAllow) {
|
||||
if (this._wasOpened)
|
||||
throw (Components.returnCode = Cr.NS_ERROR_FAILURE);
|
||||
|
||||
this._allowPipelining = aAllow;
|
||||
},
|
||||
|
||||
redirectionLimit: 10,
|
||||
|
||||
get responseStatus() {
|
||||
if (this._requestStarted)
|
||||
throw (Components.returnCode = Cr.NS_ERROR_NOT_AVAILABLE);
|
||||
|
||||
return this._responseStatus;
|
||||
},
|
||||
|
||||
get responseStatusText() {
|
||||
if (this._requestStarted)
|
||||
throw (Components.returnCode = Cr.NS_ERROR_NOT_AVAILABLE);
|
||||
|
||||
return this._responseText;
|
||||
},
|
||||
|
||||
get requestSucceeded() {
|
||||
if (this._requestStarted)
|
||||
throw (Components.returnCode = Cr.NS_ERROR_NOT_AVAILABLE);
|
||||
|
||||
return this._requestSucceeded;
|
||||
},
|
||||
|
||||
isNoCacheResponse: function Channel_isNoCacheResponse() false,
|
||||
isNoStoreResponse: function Channel_isNoStoreResponse() false,
|
||||
|
||||
getRequestHeader: function Channel_getRequestHeader() {
|
||||
throw (Components.returnCode = Cr.NS_ERROR_NOT_AVAILABLE);
|
||||
},
|
||||
|
||||
setRequestHeader: function Channel_setRequestHeader() {
|
||||
if (this._wasOpened)
|
||||
throw (Components.returnCode = Cr.NS_ERROR_IN_PROGRESS);
|
||||
},
|
||||
|
||||
visitRequestHeaders: function Channel_visitRequestHeaders() {},
|
||||
|
||||
getResponseHeader: function Channel_getResponseHeader(aHeader) {
|
||||
let name = aHeader.toLowerCase();
|
||||
if (name in this._responseHeaders)
|
||||
return this._responseHeaders[name];
|
||||
|
||||
throw (Components.returnCode = Cr.NS_ERROR_NOT_AVAILABLE);
|
||||
},
|
||||
|
||||
setResponseHeader: function Channel_setResponseHeader(aHeader, aValue, aMerge) {
|
||||
let name = aHeader.toLowerCase();
|
||||
if (!aValue && !aMerge)
|
||||
delete this._responseHeaders[name];
|
||||
else
|
||||
this._responseHeaders[name] = aValue;
|
||||
},
|
||||
|
||||
visitResponseHeaders: function Channel_visitResponseHeaders(aVisitor) {
|
||||
for (let name in this._responseHeaders) {
|
||||
let value = this._responseHeaders[name];
|
||||
|
||||
try {
|
||||
aVisitor.visitHeader(name, value);
|
||||
} catch (e) {
|
||||
// The visitor can throw to stop the iteration.
|
||||
return;
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
/* :::::::: nsIHttpChannelInternal ::::::::::::::: */
|
||||
|
||||
documentURI: null,
|
||||
get canceled() this._canceled,
|
||||
allowSpdy: false,
|
||||
|
||||
QueryInterface: XPCOMUtils.generateQI([Ci.nsIChannel,
|
||||
Ci.nsIHttpChannel,
|
||||
Ci.nsIHttpChannelInternal,
|
||||
Ci.nsIRequest])
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a given URI and extracts all parameters relevant to this protocol.
|
||||
* @param aURI The URI to parse.
|
||||
|
Loading…
Reference in New Issue
Block a user