Bug 784558. [Page Thumbnails] Re-add removeFile function to allow single thumbnail removal, used when removing entries from the history. r=felipe
authorTim Taubert <ttaubert@mozilla.com>
Fri, 24 Aug 2012 18:11:06 -0700
changeset 105398 89e186d2a1716b5609fb939394853d23558b4b01
parent 105397 010b43686a5fb14887b56289d8635582eb49c9fb
child 105399 16089a551f630b9c46d073d7c23559621d8b4723
push id55
push usershu@rfrn.org
push dateThu, 30 Aug 2012 01:33:09 +0000
reviewersfelipe
bugs784558
milestone17.0a1
Bug 784558. [Page Thumbnails] Re-add removeFile function to allow single thumbnail removal, used when removing entries from the history. r=felipe
browser/components/thumbnails/PageThumbsWorker.js
browser/components/thumbnails/test/browser_thumbnails_storage.js
browser/components/thumbnails/test/head.js
--- a/browser/components/thumbnails/PageThumbsWorker.js
+++ b/browser/components/thumbnails/PageThumbsWorker.js
@@ -14,16 +14,19 @@
 importScripts("resource://gre/modules/osfile.jsm");
 
 let PageThumbsWorker = {
   handleMessage: function Worker_handleMessage(aEvent) {
     let msg = aEvent.data;
     let data = {result: null, data: null};
 
     switch (msg.type) {
+      case "removeFile":
+        data.result = this.removeFile(msg);
+        break;
       case "removeFiles":
         data.result = this.removeFiles(msg);
         break;
       case "getFilesInDirectory":
         data.result = this.getFilesInDirectory(msg);
         break;
       default:
         data.result = false;
@@ -43,16 +46,25 @@ let PageThumbsWorker = {
         entries.push(entry.name);
       }
     }
 
     iter.close();
     return entries;
   },
 
+  removeFile: function Worker_removeFile(msg) {
+    try {
+      OS.File.remove(msg.path);
+      return true;
+    } catch (e) {
+      return false;
+    }
+  },
+
   removeFiles: function Worker_removeFiles(msg) {
     for (let file of msg.paths) {
       try {
         OS.File.remove(file);
       } catch (e) {
         // We couldn't remove the file for some reason.
         // Let's just continue with the next one.
       }
--- a/browser/components/thumbnails/test/browser_thumbnails_storage.js
+++ b/browser/components/thumbnails/test/browser_thumbnails_storage.js
@@ -14,42 +14,52 @@ XPCOMUtils.defineLazyGetter(this, "Sanit
 
 /**
  * These tests ensure that the thumbnail storage is working as intended.
  * Newly captured thumbnails should be saved as files and they should as well
  * be removed when the user sanitizes their history.
  */
 function runTests() {
   yield clearHistory();
-
-  // create a thumbnail
-  yield addTab(URL);
-  yield whenFileExists();
-  gBrowser.removeTab(gBrowser.selectedTab);
+  yield createThumbnail();
 
-  // clear all browser history
-  yield clearHistory();
-
-  // create a thumbnail
-  yield addTab(URL);
-  yield whenFileExists();
-  gBrowser.removeTab(gBrowser.selectedTab);
-
-  // make sure copy() updates an existing file
+  // Make sure Storage.copy() updates an existing file.
   PageThumbsStorage.copy(URL, URL_COPY);
   let copy = PageThumbsStorage.getFileForURL(URL_COPY);
   let mtime = copy.lastModifiedTime -= 60;
 
   PageThumbsStorage.copy(URL, URL_COPY);
   isnot(PageThumbsStorage.getFileForURL(URL_COPY).lastModifiedTime, mtime,
         "thumbnail file was updated");
 
-  // clear last 10 mins of history
+  let file = PageThumbsStorage.getFileForURL(URL);
+  let fileCopy = PageThumbsStorage.getFileForURL(URL_COPY);
+
+  // Clear the browser history. Retry until the files are gone because Windows
+  // locks them sometimes.
+  while (file.exists() || fileCopy.exists()) {
+    yield clearHistory();
+  }
+
+  yield createThumbnail();
+
+  // Clear the last 10 minutes of browsing history.
   yield clearHistory(true);
-  ok(!copy.exists(), "copy of thumbnail has been removed");
+
+  // Retry until the file is gone because Windows locks it sometimes.
+  while (file.exists()) {
+    // Re-add our URL to the history so that history observer's onDeleteURI()
+    // is called again.
+    let time = Date.now() * 1000;
+    let trans = Ci.nsINavHistoryService.TRANSITION_LINK;
+    PlacesUtils.history.addVisit(makeURI(URL), time, null, trans, false, 0);
+
+    // Try again...
+    yield clearHistory(true);
+  }
 }
 
 function clearHistory(aUseRange) {
   let s = new Sanitizer();
   s.prefDomain = "privacy.cpd.";
 
   let prefs = gPrefService.getBranch(s.prefDomain);
   prefs.setBoolPref("history", true);
@@ -60,30 +70,38 @@ function clearHistory(aUseRange) {
   prefs.setBoolPref("offlineApps", false);
   prefs.setBoolPref("passwords", false);
   prefs.setBoolPref("sessions", false);
   prefs.setBoolPref("siteSettings", false);
 
   if (aUseRange) {
     let usec = Date.now() * 1000;
     s.range = [usec - 10 * 60 * 1000 * 1000, usec];
+    s.ignoreTimespan = false;
   }
 
   s.sanitize();
   s.range = null;
+  s.ignoreTimespan = true;
 
-  executeSoon(function () {
-    if (PageThumbsStorage.getFileForURL(URL).exists())
-      clearHistory(aUseRange);
-    else
+  executeSoon(next);
+}
+
+function createThumbnail() {
+  addTab(URL, function () {
+    whenFileExists(function () {
+      gBrowser.removeTab(gBrowser.selectedTab);
       next();
+    });
   });
 }
 
-function whenFileExists() {
-  let callback = whenFileExists;
-
+function whenFileExists(aCallback) {
+  let callback;
   let file = PageThumbsStorage.getFileForURL(URL);
-  if (file.exists() && file.fileSize)
-    callback = next;
+  if (file.exists() && file.fileSize) {
+    callback = aCallback;
+  } else {
+    callback = function () whenFileExists(aCallback);
+  }
 
   executeSoon(callback);
 }
--- a/browser/components/thumbnails/test/head.js
+++ b/browser/components/thumbnails/test/head.js
@@ -52,20 +52,21 @@ let TestRunner = {
  */
 function next() {
   TestRunner.next();
 }
 
 /**
  * Creates a new tab with the given URI.
  * @param aURI The URI that's loaded in the tab.
+ * @param aCallback The function to call when the tab has loaded.
  */
-function addTab(aURI) {
+function addTab(aURI, aCallback) {
   let tab = gBrowser.selectedTab = gBrowser.addTab(aURI);
-  whenLoaded(tab.linkedBrowser);
+  whenLoaded(tab.linkedBrowser, aCallback);
 }
 
 /**
  * Loads a new URI into the currently selected tab.
  * @param aURI The URI to load.
  */
 function navigateTo(aURI) {
   let browser = gBrowser.selectedTab.linkedBrowser;