Bug 1287091 - part 3 - ContextualIdentityService JSON file should have versions, r=Gijs
☠☠ backed out by 37e221f3b0df ☠ ☠
authorAndrea Marchesini <amarchesini@mozilla.com>
Sat, 23 Jul 2016 08:58:09 +0200
changeset 331505 c5efa902cbe52cda80ef1db75322f8d0a9b31d18
parent 331504 93fa501cfd3a0d8b8ed5960789a2197a0db59ba9
child 331506 c01aeb081e2d201c27eba23327f125566c1cc29a
push id9858
push userjlund@mozilla.com
push dateMon, 01 Aug 2016 14:37:10 +0000
treeherdermozilla-aurora@203106ef6cb6 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersGijs
bugs1287091
milestone50.0a1
Bug 1287091 - part 3 - ContextualIdentityService JSON file should have versions, r=Gijs
toolkit/components/contextualidentity/ContextualIdentityService.jsm
--- a/toolkit/components/contextualidentity/ContextualIdentityService.jsm
+++ b/toolkit/components/contextualidentity/ContextualIdentityService.jsm
@@ -62,26 +62,27 @@ this.ContextualIdentityService = {
     { userContextId: 4,
       public: true,
       icon: "chrome://browser/skin/usercontext/shopping.svg",
       color: "#ee5195",
       label: "userContextShopping.label",
       accessKey: "userContextShopping.accesskey",
       telemetryId: 4,
     },
-    { userContextId: Math.pow(2, 31) - 1,
+    { userContextId: 5,
       public: false,
       icon: "",
       color: "",
       label: "userContextIdInternal.thumbnail",
       accessKey: "" },
   ],
 
   _identities: null,
   _openedIdentities: new Set(),
+  _lastUserContextId: 0,
 
   _path: null,
   _dataReady: false,
 
   _saver: null,
 
   init() {
     this._path = OS.Path.join(OS.Constants.Path.profileDir, "containers.json");
@@ -96,51 +97,69 @@ this.ContextualIdentityService = {
   load() {
     OS.File.read(this._path).then(bytes => {
       // If synchronous loading happened in the meantime, exit now.
       if (this._dataReady) {
         return;
       }
 
       try {
-        this._identities = JSON.parse(gTextDecoder.decode(bytes));
+        let data = JSON.parse(gTextDecoder.decode(bytes));
+        if (data.version != 1) {
+          dump("ERROR - ContextualIdentityService - Unknown version found in " + this._path + "\n");
+          this.loadError(null);
+          return;
+        }
+
+        this._identities = data.identities;
+        this._lastUserContextId = data.lastUserContextId;
+
         this._dataReady = true;
       } catch(error) {
         this.loadError(error);
       }
     }, (error) => {
       this.loadError(error);
     });
   },
 
   loadError(error) {
-    if (!(error instanceof OS.File.Error && error.becauseNoSuchFile) &&
+    if (error != null &&
+        !(error instanceof OS.File.Error && error.becauseNoSuchFile) &&
         !(error instanceof Components.Exception &&
           error.result == Cr.NS_ERROR_FILE_NOT_FOUND)) {
       // Let's report the error.
       Cu.reportError(error);
     }
 
     // If synchronous loading happened in the meantime, exit now.
     if (this._dataReady) {
       return;
     }
 
     this._identities = this._defaultIdentities;
+    this._lastUserContextId = this._defaultIdentities.length;
+
     this._dataReady = true;
 
     this.saveSoon();
   },
 
   saveSoon() {
     this._saver.arm();
   },
 
   save() {
-   let bytes = gTextEncoder.encode(JSON.stringify(this._identities));
+   let object = {
+     version: 1,
+     lastUserContextId: this._lastUserContextId,
+     identities: this._identities
+   };
+
+   let bytes = gTextEncoder.encode(JSON.stringify(object));
    return OS.File.writeAtomic(this._path, bytes,
                               { tmpPath: this._path + ".tmp" });
   },
 
   ensureDataReady() {
     if (this._dataReady) {
       return;
     }