Skip to content

Instantly share code, notes, and snippets.

@littlebtc
Created September 13, 2009 16:33
Show Gist options
  • Select an option

  • Save littlebtc/186233 to your computer and use it in GitHub Desktop.

Select an option

Save littlebtc/186233 to your computer and use it in GitHub Desktop.
Index: script.js
===================================================================
--- script.js (revision 13)
+++ script.js (working copy)
@@ -59,7 +59,7 @@
// Display begin clean message
var strGetRes = document.getElementById("strRes");
var text = strGetRes.getString("BeginClean");
- var alertsService = Components.classes["@mozilla.org/alerts-service;1"].getService(Components.interfaces.nsIAlertsService);
+ var alertsService = Components.classes["@mozilla.org/alerts-service;1"].getService(Components.interfaces.nsIAlertsService);
alertsService.showAlertNotification("chrome://PlacesCleaner/content/edit-clear-32.png", "PlacesCleaner", text, false);
var booOnlyVacuum = PlacesCleaner.checkOnlyVacuum();
@@ -70,20 +70,74 @@
// Begin clean
if (booOnlyVacuum == false){
- Components.classes["@mozilla.org/browser/nav-history-service;1"].getService(Components.interfaces.nsPIPlacesDatabase).DBConnection.executeSimpleSQL("DELETE FROM moz_historyvisits WHERE place_id IN (SELECT id FROM moz_places WHERE visit_count <=" + intViewTime + ");");
- Components.classes["@mozilla.org/browser/nav-history-service;1"].getService(Components.interfaces.nsPIPlacesDatabase).DBConnection.executeSimpleSQL("DELETE FROM moz_places WHERE (visit_count <= " + intViewTime + " AND hidden <> 1 AND id NOT IN (SELECT place_id FROM moz_annos UNION SELECT fk FROM moz_bookmarks));");
- Components.classes["@mozilla.org/browser/nav-history-service;1"].getService(Components.interfaces.nsPIPlacesDatabase).DBConnection.executeSimpleSQL("DELETE FROM moz_inputhistory WHERE place_id NOT IN (SELECT id FROM moz_places);");
- Components.classes["@mozilla.org/browser/nav-history-service;1"].getService(Components.interfaces.nsPIPlacesDatabase).DBConnection.executeSimpleSQL("DELETE FROM moz_favicons WHERE id NOT IN (SELECT favicon_id FROM moz_places);");
- Components.classes["@mozilla.org/browser/nav-history-service;1"].getService(Components.interfaces.nsPIPlacesDatabase).DBConnection.executeSimpleSQL("DELETE FROM moz_annos WHERE anno_attribute_id IN (SELECT id FROM moz_anno_attributes WHERE name = 'google-toolbar/thumbnail-score' OR name = 'google-toolbar/thumbnail');");
+ // Get mozIStorageConnection for places
+ var dbConn = Components.classes["@mozilla.org/browser/nav-history-service;1"].getService(Components.interfaces.nsPIPlacesDatabase).DBConnection;
+ // Clean the places DB
+ // It is a little COMPLEX to do it right on Firefox 3.5+, due to the existence of temp table
+ // See also https://wiki.mozilla.org/Places/Places_SQL_queries_best_practices
+ var statements = [];
+
+ // Delete visit count from moz_historyvisits
+ statements.push(dbConn.createStatement(
+ "DELETE FROM moz_historyvisits_view WHERE place_id IN (" +
+ "SELECT id FROM moz_places_temp WHERE visit_count <=" + intViewTime + " " +
+ "UNION ALL "+
+ "SELECT id FROM moz_places WHERE id NOT IN (SELECT id FROM moz_places_temp) AND visit_count <=" + intViewTime + " " +
+ ")"
+ ));
+ // Clean the original Places entry
+ statements.push(dbConn.createStatement(
+ "DELETE FROM moz_places_view WHERE (visit_count <= " + intViewTime + " " +
+ "AND hidden <> 1 AND id NOT IN (SELECT place_id FROM moz_annos UNION SELECT fk FROM moz_bookmarks))"
+ ));
+ // Clean the input history / favicon for the deleted Places entry
+ statements.push(dbConn.createStatement(
+ "DELETE FROM moz_inputhistory WHERE place_id NOT IN " +
+ "(SELECT id FROM moz_places_temp UNION ALL SELECT id FROM moz_places)"
+ ));
+ statements.push(dbConn.createStatement(
+ "DELETE FROM moz_favicons WHERE id NOT IN ("+
+ "SELECT favicon_id FROM moz_places_temp "+
+ "UNION ALL "+
+ "SELECT favicon_id FROM moz_places WHERE id NOT IN (SELECT id FROM moz_places_temp)"+
+ ")"
+ ));
+ // Clean Annotations from the old version of Google Toolbar
+ statements.push(dbConn.createStatement(
+ "DELETE FROM moz_annos WHERE anno_attribute_id IN (SELECT id FROM moz_anno_attributes WHERE name = 'google-toolbar/thumbnail-score' OR name = 'google-toolbar/thumbnail')"
+ ));
+ // Assign a callback, after the queries are completed, vacuum the database
+ var callback = {
+ handleCompletion: function(aReason) {
+ if (aReason != Components.interfaces.mozIStorageStatementCallback.REASON_FINISHED) {
+ Components.utils.reportError('Aborted / Canceled Queries!');
+ return;
+ }
+ this._that.doVacuum.call(this._that);
+ },
+ handleError: function(aError) {
+ Components.utils.reportError("Error: " + aError.message);
+ },
+ _that: null
+ }
+ callback._that = this;
+ dbConn.executeAsync(statements, statements.length, callback);
} else {
var console = Components.classes["@mozilla.org/consoleservice;1"].getService(Components.interfaces.nsIConsoleService);
- console.logStringMessage("Only Vacuum!");
+ console.logStringMessage("Only Vacuum!");
+ this.doVacuum();
}
-
- Components.classes["@mozilla.org/browser/nav-history-service;1"].getService(Components.interfaces.nsPIPlacesDatabase).DBConnection.executeSimpleSQL("VACUUM");
-
+
+ },
+ /* Run Vacuum and post-processing */
+ doVacuum: function() {
+ var dbConn = Components.classes["@mozilla.org/browser/nav-history-service;1"].getService(Components.interfaces.nsPIPlacesDatabase).DBConnection;
+ // In my view, VACUUM should freeze the main thread to prevend unnecessary read / write to SQLite
+ dbConn.createStatement('VACUUM').execute();
// Display end clean message
+ var strGetRes = document.getElementById("strRes");
var text = strGetRes.getString("EndClean");
+ var alertsService = Components.classes["@mozilla.org/alerts-service;1"].getService(Components.interfaces.nsIAlertsService);
alertsService.showAlertNotification("chrome://PlacesCleaner/content/edit-clear-32.png", "PlacesCleaner", text, false);
// Save last clean time
@@ -93,10 +147,10 @@
pref.setCharPref('extensions.PlacesCleaner.lastvacuumtime', LastVacuumDay);
// If set manual and hide statusbar icon, hide it
+ var booHideStatus = PlacesCleaner.checkHideStatus();
if (booHideStatus == true)
document.getElementById("PlacesCleaner-panel").hidden = true;
},
-
getintViewTime: function() {
return Components.classes["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefService)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment