Bug 519479 - places default title behavior shouldn't use part of the URL, r=dietrich
--- 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