Bug 1419102 - Use Cu.readUTF8File which remembers the file access and reads ahead the file in a background thread on subsequent startups. r=Mossop draft
authorFelipe Gomes <felipc@gmail.com>
Fri, 19 Jan 2018 13:04:08 -0200
changeset 722690 f2bcb7df58e3f2caec2cf310a122c392e83b4878
parent 722689 8c65f8bd586dfb3b7ee98e6fbecdc14248eec68d
child 722691 f6ec31581d381cb1500fc82b984640a66d03cefb
push id96199
push userfelipc@gmail.com
push dateFri, 19 Jan 2018 15:05:44 +0000
reviewersMossop
bugs1419102
milestone59.0a1
Bug 1419102 - Use Cu.readUTF8File which remembers the file access and reads ahead the file in a background thread on subsequent startups. r=Mossop MozReview-Commit-ID: 1JLO9n3vUgx
browser/components/enterprisepolicies/EnterprisePolicies.js
--- a/browser/components/enterprisepolicies/EnterprisePolicies.js
+++ b/browser/components/enterprisepolicies/EnterprisePolicies.js
@@ -5,17 +5,16 @@
 const Ci = Components.interfaces;
 const Cc = Components.classes;
 const Cr = Components.results;
 const Cu = Components.utils;
 
 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
 Cu.import("resource://gre/modules/Services.jsm");
 Cu.import("resource://gre/modules/AppConstants.jsm");
-Cu.import("resource://gre/modules/osfile.jsm");
 
 XPCOMUtils.defineLazyModuleGetters(this, {
   NetUtil: "resource://gre/modules/NetUtil.jsm",
   Policies: "resource:///modules/policies/Policies.jsm",
   PoliciesValidator: "resource:///modules/policies/PoliciesValidator.jsm",
 });
 
 // This is the file that will be searched for in the
@@ -39,20 +38,16 @@ XPCOMUtils.defineLazyGetter(this, "log",
     prefix: "Enterprise Policies",
     // tip: set maxLogLevel to "debug" and use log.debug() to create detailed
     // messages during development. See LOG_LEVELS in Console.jsm for details.
     maxLogLevel: "error",
     maxLogLevelPref: PREF_LOGLEVEL,
   });
 });
 
-// File mode constants
-const MODE_RDONLY   = 0x01;
-const PERMS_FILE    = 0o644;
-
 // ==== Start XPCOM Boilerplate ==== \\
 
 // Factory object
 const EnterprisePoliciesFactory = {
   _instance: null,
   createInstance: function BGSF_createInstance(outer, iid) {
     if (outer != null)
       throw Components.results.NS_ERROR_NO_AGGREGATION;
@@ -298,39 +293,35 @@ JSONFileReader.prototype = {
     if (this._data.json === null) {
       this.readData();
     }
 
     return this._data.json;
   },
 
   readData() {
-    let inputStream;
     try {
-      inputStream = Cc["@mozilla.org/network/file-input-stream;1"].
-                          createInstance(Ci.nsIFileInputStream);
-
-      inputStream.init(this._file, MODE_RDONLY, PERMS_FILE, 0);
-      this._data.exists = true;
-
-      let bytes = NetUtil.readInputStream(inputStream, inputStream.available());
-      this._data.json = JSON.parse(new TextDecoder().decode(bytes));
+      let data = Cu.readUTF8File(this._file);
+      if (data) {
+        this._data.exists = true;
+        this._data.json = JSON.parse(data);
+      } else {
+        this._data.exists = false;
+      }
     } catch (ex) {
       if (ex instanceof Components.Exception &&
           ex.result == Cr.NS_ERROR_FILE_NOT_FOUND) {
         this._data.exists = false;
       } else if (ex instanceof SyntaxError) {
         log.error("Error parsing JSON file");
         this._data.failed = true;
       } else {
         log.error("Error reading file");
         this._data.failed = true;
       }
-    } finally {
-      inputStream.close();
     }
   }
 };
 
 function getConfigurationFile() {
   let configFile = Services.dirsvc.get("XREAppDist", Ci.nsIFile);
   configFile.append(POLICIES_FILENAME);