Boris Zbarsky wrote:
>> Also, any idea why the LOAD_NO_NETWORK_IO and LOAD_ONLY_FROM_CACHE
>> flags would not prevent network activity as the documentation purports?
>
> Nope. Can I see the exact code you're using with them?
Source code follows. The main work is done in loadSourceFromCache,
which is called by getURLSource after checking on the mime type to
filter out binary stuff. You may notice several attempts (commented
out) of trying to use various load flags to filter out non-cached
sources. I also tried adding the loadFlags onto my cacheChannel
variable, same result - I keep seeing new network requests.
getURLSource : function( aBrowser )
{
try {
var lines;
var contentDoc = aBrowser.contentDocument;
if (contentDoc) {
var mimeCategory = gFOO.getMimeCategory( contentDoc );
if (!(mimeCategory in binaryCategoryMap)) {
lines = gFOO.loadSourceFromCache( aBrowser, contentDoc.URL,
contentDoc.characterSet );
} else {
lines = [ "Binary source format - cannot display." ];
}
} else {
return lines ? lines.join("\n") : null;
} catch ( e ) {
gFOO.logFailure( gFOO._myname+".getURLSource: " + e );
return null;
}
},
loadSourceFromCache : function( aBrowser, aURL, aCharset )
{
try {
var ioService =
CC["@mozilla.org/network/io-service;1"].getService( nsIIOService );
var channel, stream;
try {
channel = ioService.newChannel(aURL, null, null);
// channel.loadFlags |= nsIRequest.LOAD_FROM_CACHE |
nsIRequest.LOAD_BYPASS_LOCAL_CACHE_IF_BUSY;
// channel.loadFlags |= nsIRequest.LOAD_ONLY_FROM_CACHE |
nsIRequest.VALIDATE_NEVER | nsIRequest.LOAD_INITIAL_DOCUMENT_URI;
channel.loadFlags |= nsICachingChannel.LOAD_NO_NETWORK_IO |
nsICachingChannel.LOAD_ONLY_FROM_CACHE;
}
catch (err) {
return null;
}
if (aURL == aBrowser.contentWindow.location.href) {
if (channel instanceof nsICachingChannel) {
var cacheChannel = channel.QueryInterface(nsICachingChannel);
cacheChannel.cacheKey = gFOO.getCacheKey( aBrowser );
}
if (channel instanceof nsIUploadChannel) {
var postData = gFOO.getPostStream( aBrowser );
if (postData) {
var uploadChannel =
channel.QueryInterface(nsIUploadChannel);
uploadChannel.setUploadStream(postData, "", -1);
}
}
}
try { stream = channel.open(); } catch (err) { return null; }
var lines;
if (stream) {
try {
var data = gFOO.readFromStream( stream, aCharset );
lines = data.split(/\r\n|\r|\n/);
} catch (err) {
stream.close();
}
}
return lines;
} catch ( e ) {
gFOO.logFailure( gFOO._myname+".loadSourceFromCache: " + e );
return null;
}
},
getPostStream : function( aBrowser )
{
try {
var webNav = aBrowser.webNavigation;
var descriptor =
webNav.QueryInterface(nsIWebPageDescriptor).currentDescriptor;
var entry = descriptor.QueryInterface(nsISHEntry);
var postStream;
if (entry.postData) {
postStream = entry.postData.QueryInterface(nsISeekableStream);
postStream.seek(0, 0);
}
return postStream;
} catch ( e ) {
gFOO.logFailure( gFOO._myname+".getPostStream: " + e );
return null;
}
},
getCacheKey : function( aBrowser )
{
try {
var webNav = aBrowser.webNavigation;
var descriptor =
webNav.QueryInterface(nsIWebPageDescriptor).currentDescriptor;
var entry = descriptor.QueryInterface(nsISHEntry);
return entry.cacheKey;
} catch ( e ) {
gFOO.logFailure( gFOO._myname+".getCacheKey: " + e );
return null;
}
},
readFromStream : function( aStream, aCharset )
{
try {
var sisStream =
CC["@mozilla.org/scriptableinputstream;1"].createInstance(
nsIScriptableInputStream );
sisStream.init(aStream);
var segments = [];
for (var count = aStream.available(); count;
count=aStream.available()) {
var segment = sisStream.read(count);
segments.push(segment);
}
sisStream.close();
var sourceText = segments.join("");
return gFOO.convertToUnicode(sourceText, aCharset);
} catch ( e ) {
gFOO.logFailure( gFOO._myname+".readFromStream: " + e );
return null;
}
},
getMimeCategory : function( aContentDoc )
{
try {
// get the mime type from document's content type
var mimeType = aContentDoc.contentType;
if (!mimeType || !(mimeType in mimeCategoryMap)) {
// if no mime type available, find it based on url file extension
var lastDot = contentDoc.URL.lastIndexOf(".");
var ext = contentDoc.URL.substr(lastDot+1);
if (ext) {
var extMimeType = mimeExtensionMap[ext.toLowerCase()];
mimeType = extMimeType ? extMimeType : mimeType;
}
}
return mimeCategoryMap[mimeType];
} catch ( e ) {
gFOO.logFailure( gFOO._myname+".getMimeCategory: " + e );
return null;
}
},
_______________________________________________
dev-tech-network mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-tech-network