bug #40867 (r=bzarsky@mit.edu, sr=jst@netscape.com, a=asa@mozilla.org) Add new caching API to allow view-source to load from the cache and not the network.

This commit is contained in:
rpotts%netscape.com 2006-09-14 06:03:45 +00:00
Родитель c6e58d56ae
Коммит 93a7dd4bcb
3 изменённых файлов: 104 добавлений и 23 удалений

Просмотреть файл

@ -1001,7 +1001,7 @@ function BrowserLoadURL(aTriggeringEvent)
{ {
var url = gURLBar.value; var url = gURLBar.value;
if (url.match(/^view-source:/)) { if (url.match(/^view-source:/)) {
BrowserViewSourceOfURL(url.replace(/^view-source:/, ""), null); BrowserViewSourceOfURL(url.replace(/^view-source:/, ""), null, null);
} else { } else {
if (pref && pref.getBoolPref("browser.tabs.opentabfor.urlbar") && if (pref && pref.getBoolPref("browser.tabs.opentabfor.urlbar") &&
getBrowser().localName == "tabbrowser" && getBrowser().localName == "tabbrowser" &&
@ -1093,25 +1093,59 @@ function OpenAddressbook()
"chrome,extrachrome,menubar,resizable,status,toolbar"); "chrome,extrachrome,menubar,resizable,status,toolbar");
} }
function BrowserViewSource() function BrowserViewSourceOfDocument(aDocument)
{ {
var focusedWindow = document.commandDispatcher.focusedWindow; var docCharset;
if (focusedWindow == window) var pageCookie;
focusedWindow = _content; var webNav;
if (focusedWindow) // Get the document charset
var docCharset = "charset=" + focusedWindow.document.characterSet; docCharset = "charset=" + aDocument.characterSet;
BrowserViewSourceOfURL(getWebNavigation().currentURI.spec, docCharset); // Get the nsIWebNavigation associated with the document
try {
var win;
var ifRequestor;
// Get the DOMWindow for the requested document. If the DOMWindow
// cannot be found, then just use the _content window...
//
// XXX: This is a bit of a hack...
win = aDocument.defaultView;
if (win == window) {
win = _content;
}
ifRequestor = win.QueryInterface(Components.interfaces.nsIInterfaceRequestor);
webNav = ifRequestor.getInterface(Components.interfaces.nsIWebNavigation);
} catch(err) {
// If nsIWebNavigation cannot be found, just get the one for the whole
// window...
webNav = getWebNavigation();
}
//
// Get the 'PageDescriptor' for the current document. This allows the
// view-source to access the cached copy of the content rather than
// refetching it from the network...
//
try{
var PageLoader = webNav.QueryInterface(Components.interfaces.nsIWebPageDescriptor);
pageCookie = PageLoader.currentDescriptor;
} catch(err) {
// If no page descriptor is available, just use the view-source URL...
}
BrowserViewSourceOfURL(webNav.currentURI.spec, docCharset, pageCookie);
} }
function BrowserViewSourceOfURL(url, charset) function BrowserViewSourceOfURL(url, charset, pageCookie)
{ {
// try to open a view-source window while inheriting the charset (if any) // try to open a view-source window while inheriting the charset (if any)
openDialog("chrome://navigator/content/viewSource.xul", openDialog("chrome://navigator/content/viewSource.xul",
"_blank", "_blank",
"scrollbars,resizable,chrome,dialog=no", "scrollbars,resizable,chrome,dialog=no",
url, charset); url, charset, pageCookie);
} }
// doc=null for regular page info, doc=owner document for frame info. // doc=null for regular page info, doc=owner document for frame info.

Просмотреть файл

@ -133,7 +133,7 @@
<command id="cmd_copyImageContents"/> <command id="cmd_copyImageContents"/>
<!-- View Menu --> <!-- View Menu -->
<command id="View:PageSource" oncommand="BrowserViewSource();"/> <command id="View:PageSource" oncommand="BrowserViewSourceOfDocument(_content.document);"/>
<command id="View:PageInfo" oncommand="BrowserPageInfo();"/> <command id="View:PageInfo" oncommand="BrowserPageInfo();"/>
<command id="View:FullScreen" oncommand="BrowserFullScreen();"/> <command id="View:FullScreen" oncommand="BrowserFullScreen();"/>

Просмотреть файл

@ -61,22 +61,69 @@ function viewSource(url)
return false; return false;
} }
try { var loadFromURL = true;
if ("arguments" in window && window.arguments.length >= 2) { //
if (window.arguments[1].indexOf('charset=') != -1) { // Parse the 'arguments' supplied with the dialog.
var arrayArgComponents = window.arguments[1].split('='); // arg[0] - URL string.
if (arrayArgComponents) { // arg[1] - Charset value in the form 'charset=xxx'.
//we should "inherit" the charset menu setting in a new window // arg[2] - Page descriptor used to load content from the cache.
getMarkupDocumentViewer().defaultCharacterSet = arrayArgComponents[1]; //
} if ("arguments" in window) {
var arg;
//
// Set the charset of the viewsource window...
//
if (window.arguments.length >= 2) {
arg = window.arguments[1];
try {
if (typeof(arg) == "string" && arg.indexOf('charset=') != -1) {
var arrayArgComponents = arg.split('=');
if (arrayArgComponents) {
//we should "inherit" the charset menu setting in a new window
getMarkupDocumentViewer().defaultCharacterSet = arrayArgComponents[1];
}
}
} catch (ex) {
// Ignore the failure and keep processing arguments...
}
}
//
// Use the page descriptor to load the content from the cache (if
// available).
//
if (window.arguments.length >= 3) {
arg = window.arguments[2];
try {
if (typeof(arg) == "object" && arg != null) {
var pageLoaderIface = Components.interfaces.nsIWebPageDescriptor;
var PageLoader = getBrowser().webNavigation.QueryInterface(pageLoaderIface);
//
// Load the page using the page descriptor rather than the URL.
// This allows the content to be fetched from the cache (if
// possible) rather than the network...
//
PageLoader.LoadPage(arg, pageLoaderIface.DISPLAY_AS_SOURCE);
// The content was successfully loaded from the page cookie.
loadFromURL = false;
}
} catch(ex) {
// Ignore the failure. The content will be loaded via the URL
// that was supplied in arg[0].
} }
} }
} catch(ex) {
} }
var loadFlags = Components.interfaces.nsIWebNavigation.LOAD_FLAGS_NONE; if (loadFromURL) {
var viewSrcUrl = "view-source:" + url; //
getBrowser().webNavigation.loadURI(viewSrcUrl, loadFlags, null, null, null); // Currently, an exception is thrown if the URL load fails...
//
var loadFlags = Components.interfaces.nsIWebNavigation.LOAD_FLAGS_NONE;
var viewSrcUrl = "view-source:" + url;
getBrowser().webNavigation.loadURI(viewSrcUrl, loadFlags, null, null, null);
}
//check the view_source.wrap_long_lines pref and set the menuitem's checked attribute accordingly //check the view_source.wrap_long_lines pref and set the menuitem's checked attribute accordingly
if (gPrefs) { if (gPrefs) {