Bug 707094 - DOMApplicationRegistry (Webapps.jsm): Use FileUtils.jsm for simpler and more robust nsIFile handling [r=philikon]
authorFabrice Desré <fabrice@mozilla.com>
Mon, 05 Dec 2011 20:22:01 -0800
changeset 83128 c9eee0711ca6c1d8b9a152af2f9372fef4cd5f31
parent 83127 1f093067e982dff68a1b6ef8d2617e965f84d493
child 83129 85358854403fbd5c6b1731e2eda2722deea51d98
push id519
push userakeybl@mozilla.com
push dateWed, 01 Feb 2012 00:38:35 +0000
treeherdermozilla-beta@788ea1ef610b [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersphilikon
bugs707094
milestone11.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 707094 - DOMApplicationRegistry (Webapps.jsm): Use FileUtils.jsm for simpler and more robust nsIFile handling [r=philikon]
dom/base/Webapps.jsm
--- a/dom/base/Webapps.jsm
+++ b/dom/base/Webapps.jsm
@@ -38,47 +38,42 @@
 const Cu = Components.utils; 
 const Cc = Components.classes;
 const Ci = Components.interfaces;
 
 let EXPORTED_SYMBOLS = ["DOMApplicationRegistry", "DOMApplicationManifest"];
 
 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
 Cu.import("resource://gre/modules/Services.jsm");
+Cu.import("resource://gre/modules/FileUtils.jsm");
 
 XPCOMUtils.defineLazyGetter(this, "NetUtil", function() {
   Cu.import("resource://gre/modules/NetUtil.jsm");
   return NetUtil;
 });
 
 let DOMApplicationRegistry = {
-  appsDir: null,
   appsFile: null,
   webapps: { },
 
   init: function() {
     this.mm = Cc["@mozilla.org/parentprocessmessagemanager;1"].getService(Ci.nsIFrameMessageManager);
     let messages = ["Webapps:Install", "Webapps:Uninstall",
                     "Webapps:Enumerate", "Webapps:Launch"];
 
     messages.forEach((function(msgName) {
       this.mm.addMessageListener(msgName, this);
     }).bind(this));
 
-    let file =  Cc["@mozilla.org/file/directory_service;1"].getService(Ci.nsIProperties).get("ProfD", Ci.nsIFile);
-    file.append("webapps");
-    if (!file.exists() || !file.isDirectory()) {
-      file.create(Ci.nsIFile.DIRECTORY_TYPE, 0700);
-    }
-    this.appsDir = file;
-    this.appsFile = file.clone();
-    this.appsFile.append("webapps.json");
+    let appsDir = FileUtils.getDir("ProfD", ["webapps"], true, true);
+    this.appsFile = FileUtils.getFile("ProfD", ["webapps", "webapps.json"], true);
+
     if (!this.appsFile.exists())
       return;
-    
+
     this._loadJSONAsync(this.appsFile, (function(aData) { this.webapps = aData; }).bind(this));
   },
 
   _loadJSONAsync: function(aFile, aCallback) {
     try {
       let channel = NetUtil.newChannel(aFile);
       channel.contentType = "application/json";
       NetUtil.asyncFetch(channel, function(aStream, aResult) {
@@ -135,18 +130,17 @@ let DOMApplicationRegistry = {
         else
           this.enumerate(msg);
         break;
     }
   },
 
   _writeFile: function ss_writeFile(aFile, aData, aCallbak) {
     // Initialize the file output stream.
-    let ostream = Cc["@mozilla.org/network/safe-file-output-stream;1"].createInstance(Ci.nsIFileOutputStream);
-    ostream.init(aFile, 0x02 | 0x08 | 0x20, 0600, ostream.DEFER_OPEN);
+    let ostream = FileUtils.openSafeFileOutputStream(aFile);
 
     // Obtain a converter to convert our data to a UTF-8 encoded input stream.
     let converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"].createInstance(Ci.nsIScriptableUnicodeConverter);
     converter.charset = "UTF-8";
 
     // Asynchronously copy the data to the file.
     let istream = converter.convertToInputStream(aData);
     NetUtil.asyncCopy(istream, ostream, function(rc) {
@@ -171,32 +165,29 @@ let DOMApplicationRegistry = {
   },
   
   confirmInstall: function(aData) {
     let app = aData.app;
     let id = this._appId(app.origin);
 
     // install an application again is considered as an update
     if (id) {
-      let dir = this.appsDir.clone();
-      dir.append(id);
+      let dir = FileUtils.getDir("ProfD", ["webapps", id], true, true);
       try {
         dir.remove(true);
       } catch(e) {
       }
     }
     else {
       let uuidGenerator = Cc["@mozilla.org/uuid-generator;1"].getService(Ci.nsIUUIDGenerator);
       id = uuidGenerator.generateUUID().toString();
     }
 
-    let dir = this.appsDir.clone();
-    dir.append(id);
-    dir.create(Ci.nsIFile.DIRECTORY_TYPE, 0700);
-    
+    let dir = FileUtils.getDir("ProfD", ["webapps", id], true, true);
+
     let manFile = dir.clone();
     manFile.append("manifest.json");
     this._writeFile(manFile, JSON.stringify(app.manifest));
 
     this.webapps[id] = this._cloneAppObject(app);
     delete this.webapps[id].manifest;
     this.webapps[id].installTime = (new Date()).getTime()
 
@@ -214,36 +205,33 @@ let DOMApplicationRegistry = {
   },
 
   /**
     * Asynchronously reads a list of manifests
     */
   _readManifests: function(aData, aFinalCallback, aIndex) {
     let index = aIndex || 0;
     let id = aData[index].id;
-    let file = this.appsDir.clone();
-    file.append(id);
-    file.append("manifest.json");
+    let file = FileUtils.getFile("ProfD", ["webapps", id, "manifest.json"], true);
     this._loadJSONAsync(file, (function(aJSON) {
       aData[index].manifest = aJSON;
       if (index == aData.length - 1)
         aFinalCallback(aData);
       else
         this._readManifests(aData, aFinalCallback, index + 1);
     }).bind(this)); 
   },
  
   uninstall: function(aData) {
     for (let id in this.webapps) {
       let app = this.webapps[id];
       if (app.origin == aData.origin) {
         delete this.webapps[id];
         this._writeFile(this.appsFile, JSON.stringify(this.webapps));
-        let dir = this.appsDir.clone();
-        dir.append(id);
+        let dir = FileUtils.getDir("ProfD", ["webapps", id], true, true);
         try {
           dir.remove(true);
         } catch (e) {
         }
         this.mm.sendAsyncMessage("Webapps:Uninstall:Return:OK", aData);
       }
     }
   },