Bug 777926 - Use URL or title from history for bookmarking error pages. r=Mano
authorPeter Simonyi <ptsimony@csclub.uwaterloo.ca>
Wed, 19 Dec 2012 16:11:19 -0500
changeset 126073 59c4ddd0b7a410c8c7ebb5c21251dce31463bf35
parent 126072 a3e933f00833f3dc5757470ad6cf564ab472d620
child 126074 a3f4fd5bd56383f97f3ee0cf8369fc6cd5df12ab
push id2151
push userlsblakk@mozilla.com
push dateTue, 19 Feb 2013 18:06:57 +0000
treeherdermozilla-beta@4952e88741ec [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersMano
bugs777926
milestone20.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 777926 - Use URL or title from history for bookmarking error pages. r=Mano
browser/base/content/browser-places.js
browser/base/content/test/Makefile.in
browser/base/content/test/browser_bookmark_titles.js
--- a/browser/base/content/browser-places.js
+++ b/browser/base/content/browser-places.js
@@ -264,17 +264,21 @@ var PlacesCommandHook = {
       // no DOMWindow (?) but information about the loaded document
       // may still be obtained from the webNavigation.
       var webNav = aBrowser.webNavigation;
       var url = webNav.currentURI;
       var title;
       var description;
       var charset;
       try {
-        title = webNav.document.title || url.spec;
+        let isErrorPage = /^about:(neterror|certerror|blocked)/
+                          .test(webNav.document.documentURI);
+        title = isErrorPage ? PlacesUtils.history.getPageTitle(url)
+                            : webNav.document.title;
+        title = title || url.spec;
         description = PlacesUIUtils.getDescriptionFromDocument(webNav.document);
         charset = webNav.document.characterSet;
       }
       catch (e) { }
 
       if (aShowEditUI) {
         // If we bookmark the page here (i.e. page was not "starred" already)
         // but open right into the "edit" state, start batching here, so
--- a/browser/base/content/test/Makefile.in
+++ b/browser/base/content/test/Makefile.in
@@ -287,16 +287,17 @@ endif
                  browser_bug820497.js \
                  blockPluginVulnerableUpdatable.xml \
                  blockPluginVulnerableNoUpdate.xml \
                  blockNoPlugins.xml \
                  browser_utilityOverlay.js \
                  browser_bug676619.js \
                  download_page.html \
                  browser_URLBarSetURI.js \
+                 browser_bookmark_titles.js \
                  $(NULL)
 
 ifneq (cocoa,$(MOZ_WIDGET_TOOLKIT))
 _BROWSER_FILES += \
 		browser_bug462289.js \
 		$(NULL)
 else
 _BROWSER_FILES += \
new file mode 100644
--- /dev/null
+++ b/browser/base/content/test/browser_bookmark_titles.js
@@ -0,0 +1,75 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+// This file is tests for the default titles that new bookmarks get.
+
+let tests = [
+    ['http://example.com/browser/browser/base/content/test/dummy_page.html',
+     'Dummy test page'],
+    ['data:text/html;charset=utf-8,<title>test data: url</title>',
+     'test data: url'],
+    ['http://unregistered-domain.example',
+     'http://unregistered-domain.example/'],
+    ['https://untrusted.example.com/somepage.html',
+     'https://untrusted.example.com/somepage.html']
+];
+
+function generatorTest() {
+    gBrowser.selectedTab = gBrowser.addTab();
+    let browser = gBrowser.selectedBrowser;
+
+    browser.addEventListener("DOMContentLoaded", nextStep, true);
+    registerCleanupFunction(function () {
+        browser.removeEventListener("DOMContentLoaded", nextStep, true);
+        gBrowser.removeCurrentTab();
+    });
+
+    yield; // Wait for the new tab to load.
+
+    // Test that a bookmark of each URI gets the corresponding default title.
+    for (let i = 0; i < tests.length; ++i) {
+        let [uri, title] = tests[i];
+        content.location = uri;
+        yield;
+        checkBookmark(uri, title);
+    }
+
+    // Network failure test: now that dummy_page.html is in history, bookmarking
+    // it should give the last known page title as the default bookmark title.
+
+    // Simulate a network outage with offline mode. (Localhost is still
+    // accessible in offline mode, so disable the test proxy as well.)
+    BrowserOffline.toggleOfflineStatus();
+    let proxy = Services.prefs.getIntPref('network.proxy.type');
+    Services.prefs.setIntPref('network.proxy.type', 0);
+    registerCleanupFunction(function () {
+        BrowserOffline.toggleOfflineStatus();
+        Services.prefs.setIntPref('network.proxy.type', proxy);
+    });
+
+    // LOAD_FLAGS_BYPASS_CACHE isn't good enough. So clear the cache.
+    Services.cache.evictEntries(Services.cache.STORE_ANYWHERE);
+
+    let [uri, title] = tests[0];
+    content.location = uri;
+    yield;
+    // The offline mode test is only good if the page failed to load.
+    is(content.document.documentURI.substring(0, 14), 'about:neterror',
+        "Offline mode successfully simulated network outage.");
+    checkBookmark(uri, title);
+}
+
+// Bookmark the current page and confirm that the new bookmark has the expected
+// title. (Then delete the bookmark.)
+function checkBookmark(uri, expected_title) {
+    PlacesCommandHook.bookmarkCurrentPage(false);
+    
+    let id = PlacesUtils.getMostRecentBookmarkForURI(PlacesUtils._uri(uri));
+    let title = PlacesUtils.bookmarks.getItemTitle(id);
+
+    is(title, expected_title, "Bookmark got a good default title.");
+
+    PlacesUtils.bookmarks.removeItem(id);
+}
+