Bug 1416468 - Share container data parsing instead of duplicating it. r? draft
authorIan Moody <moz-ian@perix.co.uk>
Mon, 13 Nov 2017 18:23:37 +0000
changeset 697295 0028c5ed43b9fd773370e357cfe0765627f85ddb
parent 697096 fc194660762d1b92e1679d860a8bf41116d0f54f
child 740087 2e2dc79217b19afd64885c0cbcd77617822a7333
push id88963
push usermoz-ian@perix.co.uk
push dateMon, 13 Nov 2017 21:19:24 +0000
bugs1416468
milestone59.0a1
Bug 1416468 - Share container data parsing instead of duplicating it. r? load() and ensureDataReady() both parsethe containers data, so put the parsing into a function they can share, and use NetUtil.readInputStream() instead of NetUtil.readInputStreamToString() to get bytes. MozReview-Commit-ID: I4RYaFEhID6
toolkit/components/contextualidentity/ContextualIdentityService.jsm
--- a/toolkit/components/contextualidentity/ContextualIdentityService.jsm
+++ b/toolkit/components/contextualidentity/ContextualIdentityService.jsm
@@ -146,30 +146,17 @@ function _ContextualIdentityService(path
   load() {
     OS.File.read(this._path).then(bytes => {
       // If synchronous loading happened in the meantime, exit now.
       if (this._dataReady) {
         return;
       }
 
       try {
-        let data = JSON.parse(gTextDecoder.decode(bytes));
-        if (data.version == 1) {
-          this.resetDefault();
-        }
-        if (data.version != 2) {
-          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;
+        this.parseData(bytes);
       } catch (error) {
         this.loadError(error);
       }
     }, (error) => {
       this.loadError(error);
     });
   },
 
@@ -300,34 +287,47 @@ function _ContextualIdentityService(path
       icon: identity.icon,
       color: identity.color,
       userContextId: identity.userContextId,
     };
 
     return {wrappedJSObject};
   },
 
+  parseData(bytes) {
+    let data = JSON.parse(gTextDecoder.decode(bytes));
+    if (data.version == 1) {
+      this.resetDefault();
+    }
+    if (data.version != 2) {
+      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;
+  },
+
   ensureDataReady() {
     if (this._dataReady) {
       return;
     }
 
     try {
       // This reads the file and automatically detects the UTF-8 encoding.
       let inputStream = Cc["@mozilla.org/network/file-input-stream;1"]
                           .createInstance(Ci.nsIFileInputStream);
       inputStream.init(new FileUtils.File(this._path),
                        FileUtils.MODE_RDONLY, FileUtils.PERMS_FILE, 0);
       try {
-        let bytes = NetUtil.readInputStreamToString(inputStream, inputStream.available());
-        let data = JSON.parse(gTextDecoder.decode(bytes));
-        this._identities = data.identities;
-        this._lastUserContextId = data.lastUserContextId;
-
-        this._dataReady = true;
+        let bytes = NetUtil.readInputStream(inputStream, inputStream.available());
+        this.parseData(bytes);
       } finally {
         inputStream.close();
       }
     } catch (error) {
       this.loadError(error);
     }
   },