Bug 752407 - Thumbnail cache should be created in the Local profile folder, not the Roaming ones; r=mak
authorTim Taubert <tim.taubert@gmx.de>
Mon, 18 Jun 2012 16:05:23 +0200
changeset 101798 960b6d4ea73b52450320ae7c00702f3c233f523d
parent 101708 b1a0fb2bdbf7605a4f8fcd1cdcc76cda871b591f
child 101799 1dd1770cc77eaeed188db07da8a8254475767296
push id1316
push userakeybl@mozilla.com
push dateMon, 27 Aug 2012 22:37:00 +0000
treeherdermozilla-beta@db4b09302ee2 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmak
bugs752407
milestone16.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 752407 - Thumbnail cache should be created in the Local profile folder, not the Roaming ones; r=mak
browser/components/thumbnails/PageThumbs.jsm
--- a/browser/components/thumbnails/PageThumbs.jsm
+++ b/browser/components/thumbnails/PageThumbs.jsm
@@ -6,16 +6,18 @@
 
 let EXPORTED_SYMBOLS = ["PageThumbs", "PageThumbsStorage", "PageThumbsCache"];
 
 const Cu = Components.utils;
 const Cc = Components.classes;
 const Ci = Components.interfaces;
 
 const HTML_NAMESPACE = "http://www.w3.org/1999/xhtml";
+const PREF_STORAGE_VERSION = "browser.pagethumbnails.storage_version";
+const LATEST_STORAGE_VERSION = 1;
 
 /**
  * Name of the directory in the profile that contains the thumbnails.
  */
 const THUMBNAIL_DIRECTORY = "thumbnails";
 
 /**
  * The default background color for page thumbnails.
@@ -74,16 +76,19 @@ let PageThumbs = {
    * The thumbnails' image type.
    */
   get contentType() "image/png",
 
   init: function PageThumbs_init() {
     if (!this._initialized) {
       this._initialized = true;
       PlacesUtils.history.addObserver(PageThumbsHistoryObserver, false);
+
+      // Migrate the underlying storage, if needed.
+      PageThumbsStorageMigrator.migrate();
     }
   },
 
   uninit: function PageThumbs_uninit() {
     if (this._initialized) {
       this._initialized = false;
       PlacesUtils.history.removeObserver(PageThumbsHistoryObserver);
     }
@@ -243,17 +248,17 @@ let PageThumbs = {
     }
   },
 };
 
 let PageThumbsStorage = {
   getFileForURL: function Storage_getFileForURL(aURL, aOptions) {
     let hash = this._calculateMD5Hash(aURL);
     let parts = [THUMBNAIL_DIRECTORY, hash[0], hash[1]];
-    let file = FileUtils.getDir("ProfD", parts, aOptions && aOptions.createPath);
+    let file = FileUtils.getDir("ProfLD", parts, aOptions && aOptions.createPath);
     file.append(hash.slice(2) + ".png");
     return file;
   },
 
   write: function Storage_write(aURL, aDataStream, aCallback) {
     let file = this.getFileForURL(aURL, {createPath: true});
     let fos = FileUtils.openSafeFileOutputStream(file);
 
@@ -279,17 +284,17 @@ let PageThumbsStorage = {
       this.getFileForURL(aURL).remove(false);
     } catch (e) {
       /* The file might not exist or we're not permitted to remove it. */
     }
   },
 
   wipe: function Storage_wipe() {
     try {
-      FileUtils.getDir("ProfD", [THUMBNAIL_DIRECTORY]).remove(true);
+      FileUtils.getDir("ProfLD", [THUMBNAIL_DIRECTORY]).remove(true);
     } catch (e) {
       /* The file might not exist or we're not permitted to remove it. */
     }
   },
 
   _calculateMD5Hash: function Storage_calculateMD5Hash(aValue) {
     let hash = gCryptoHash;
     let value = gUnicodeConverter.convertToByteArray(aValue);
@@ -303,16 +308,54 @@ let PageThumbsStorage = {
     let hex = "";
     for (let i = 0; i < aData.length; i++)
       hex += ("0" + aData.charCodeAt(i).toString(16)).slice(-2);
     return hex;
   },
 
 };
 
+let PageThumbsStorageMigrator = {
+  get currentVersion() {
+    try {
+      return Services.prefs.getIntPref(PREF_STORAGE_VERSION);
+    } catch (e) {
+      // The pref doesn't exist, yet. Return version 0.
+      return 0;
+    }
+  },
+
+  set currentVersion(aVersion) {
+    Services.prefs.setIntPref(PREF_STORAGE_VERSION, aVersion);
+  },
+
+  migrate: function Migrator_migrate() {
+    let version = this.currentVersion;
+
+    if (version < 1)
+      this.removeThumbnailsFromRoamingProfile();
+
+    this.currentVersion = LATEST_STORAGE_VERSION;
+  },
+
+  removeThumbnailsFromRoamingProfile:
+  function Migrator_removeThumbnailsFromRoamingProfile() {
+    let local = FileUtils.getDir("ProfLD", [THUMBNAIL_DIRECTORY]);
+    let roaming = FileUtils.getDir("ProfD", [THUMBNAIL_DIRECTORY]);
+
+    if (!roaming.equals(local) && roaming.exists()) {
+      try {
+        roaming.remove(true);
+      } catch (e) {
+        // The directory might not exist or we're not permitted to remove it.
+      }
+    }
+  }
+};
+
 let PageThumbsHistoryObserver = {
   onDeleteURI: function Thumbnails_onDeleteURI(aURI, aGUID) {
     PageThumbsStorage.remove(aURI.spec);
   },
 
   onClearHistory: function Thumbnails_onClearHistory() {
     PageThumbsStorage.wipe();
   },