Bug 519479 - places default title behavior shouldn't use part of the URL, r=dietrich
authorMarco Bonardo <mbonardo@mozilla.com>
Wed, 11 Nov 2009 12:04:18 +0100
changeset 26294 66d8e92512b9ffaae65dce40002957cee3ba922d
parent 26293 e6af6fa42b0d3501e6568bc6b6c761029ac40345
child 26295 614ae484f4a1f58a1a225910e5b5b7214c59c8ae
push id1834
push userclokep@gmail.com
push dateMon, 23 Jan 2017 21:48:40 +0000
treeherdercomm-beta@293cffe83e59 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersdietrich
bugs519479
Bug 519479 - places default title behavior shouldn't use part of the URL, r=dietrich
suite/common/places/tests/autocomplete/head_autocomplete.js
--- a/suite/common/places/tests/autocomplete/head_autocomplete.js
+++ b/suite/common/places/tests/autocomplete/head_autocomplete.js
@@ -180,37 +180,80 @@ var lmsvc = Cc["@mozilla.org/browser/liv
             getService(Ci.nsILivemarkService);
 
 // Some date not too long ago
 let gDate = new Date(Date.now() - 1000 * 60 * 60) * 1000;
 // Store the page info for each uri
 let gPages = [];
 
 /**
- * Sets the page title synchronously.  The page must already be in the database.
+ * Function gets current database connection, if the connection has been closed
+ * it will try to reconnect to the places.sqlite database.
+ */
+function DBConn()
+{
+  let db = Cc["@mozilla.org/browser/nav-history-service;1"].
+           getService(Ci.nsPIPlacesDatabase).
+           DBConnection;
+  if (db.connectionReady)
+    return db;
+
+  // open a new connection if needed
+  let file = dirSvc.get('ProfD', Ci.nsIFile);
+  file.append("places.sqlite");
+  let storageService = Cc["@mozilla.org/storage/service;1"].
+                       getService(Ci.mozIStorageService);
+  try {
+    var dbConn = storageService.openDatabase(file);
+  } catch (ex) {
+    return null;
+  }
+  return dbConn;
+}
+
+/**
+ * Sets title synchronously for a page in moz_places synchronously.
+ * History.SetPageTitle uses LAZY_ADD so we can't rely on it.
  *
  * @param aURI
  *        An nsIURI to set the title for.
  * @param aTitle
  *        The title to set the page to.
+ * @throws if the page is not found in the database.
+ *
+ * @note this function only exists because we have no API to do this. It should
+ *       be added in bug 421897.
  */
-function setPageTitle(aURI, aTitle)
-{
-  // XXX this function only exists because we have no API to do this. It should
-  //     be added in bug 421897.
-  let db = histsvc.QueryInterface(Ci.nsPIPlacesDatabase).DBConnection;
-  let stmt = db.createStatement(
-    "UPDATE moz_places_view " +
-    "SET title = :title " +
-    "WHERE url = :uri"
-  );
+function setPageTitle(aURI, aTitle) {
+  let dbConn = DBConn();
+  // Check that the page exists.
+  let stmt = dbConn.createStatement(
+    "SELECT id FROM moz_places_view WHERE url = :url");
+  stmt.params.url = aURI.spec;
+  try {
+    if (!stmt.executeStep()) {
+      do_throw("Unable to find page " + aURIString);
+      return;
+    }
+  }
+  finally {
+    stmt.finalize();
+  }
+
+  // Update the title
+  stmt = dbConn.createStatement(
+    "UPDATE moz_places_view SET title = :title WHERE url = :url");
   stmt.params.title = aTitle;
-  stmt.params.uri = aURI.spec;
-  stmt.execute();
-  stmt.finalize();
+  stmt.params.url = aURI.spec;
+  try {
+    stmt.execute();
+  }
+  finally {
+    stmt.finalize();
+  }
 }
 
 /**
  * Adds a livemark container with a single child, and creates various properties
  * for it depending on the parameters passed in.
  *
  * @param aContainerSiteURI
  *        An index into kURIs that holds the string for the URI of site of the