Bug 838210 - Don't use file.exists() when not necessary (mobile). r=bnicholson, r=mfinkle

This commit is contained in:
Marco Castelluccio 2013-08-27 08:50:22 -04:00
Родитель 4c67d81f07
Коммит cd8ba5454f
6 изменённых файлов: 62 добавлений и 123 удалений

Просмотреть файл

@ -10,6 +10,8 @@ Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/PluralForm.jsm");
Cu.import("resource://gre/modules/PrivateBrowsingUtils.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "OS", "resource://gre/modules/osfile.jsm");
let gStrings = Services.strings.createBundle("chrome://browser/locale/aboutDownloads.properties");
let downloadTemplate =
@ -460,20 +462,15 @@ let Downloads = {
removeDownload: function dl_removeDownload(aItem) {
this._getDownloadForElement(aItem, function(aDownload) {
let f = null;
try {
f = aDownload.targetFile;
} catch (ex) {
// even if there is no file, pretend that there is so that we can remove
// it from the list
f = { leafName: "" };
if (aDownload.targetFile) {
OS.File.remove(aDownload.targetFile.path).then(null, function onError(reason) {
if (!(reason instanceof OS.File.Error && reason.becauseNoSuchFile)) {
this.logError("removeDownload() " + reason, aDownload);
}
}.bind(this));
}
aDownload.remove();
try {
if (f) f.remove(false);
} catch (ex) {
this.logError("removeDownload() " + ex, aDownload);
}
}.bind(this));
},
@ -531,17 +528,15 @@ let Downloads = {
cancelDownload: function dl_cancelDownload(aItem) {
this._getDownloadForElement(aItem, function(aDownload) {
try {
aDownload.cancel();
let f = aDownload.targetFile;
OS.File.remove(aDownload.targetFile.path).then(null, function onError(reason) {
if (!(reason instanceof OS.File.Error && reason.becauseNoSuchFile)) {
this.logError("cancelDownload() " + reason, aDownload);
}
}.bind(this));
if (f.exists())
f.remove(false);
aDownload.cancel();
this._updateDownloadRow(aItem, aDownload);
} catch (ex) {
this.logError("cancelDownload() " + ex, aDownload);
}
this._updateDownloadRow(aItem, aDownload);
}.bind(this));
},

Просмотреть файл

@ -35,8 +35,8 @@ XPCOMUtils.defineLazyModuleGetter(this, "UserAgentOverrides",
XPCOMUtils.defineLazyModuleGetter(this, "LoginManagerContent",
"resource://gre/modules/LoginManagerContent.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "NetUtil",
"resource://gre/modules/NetUtil.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "Task", "resource://gre/modules/Task.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "OS", "resource://gre/modules/osfile.jsm");
#ifdef MOZ_SAFE_BROWSING
XPCOMUtils.defineLazyModuleGetter(this, "SafeBrowsing",
@ -6907,18 +6907,9 @@ var WebappsUI = {
_writeData: function(aFile, aPrefs) {
if (aPrefs.length > 0) {
let data = JSON.stringify(aPrefs);
let ostream = Cc["@mozilla.org/network/file-output-stream;1"].createInstance(Ci.nsIFileOutputStream);
ostream.init(aFile, -1, -1, 0);
let istream = Cc["@mozilla.org/io/string-input-stream;1"].createInstance(Ci.nsIStringInputStream);
istream.setData(data, data.length);
NetUtil.asyncCopy(istream, ostream, function(aResult) {
if (!Components.isSuccessCode(aResult)) {
console.log("Error writing default prefs: " + aResult);
}
let array = new TextEncoder().encode(JSON.stringify(aPrefs));
OS.File.writeAtomic(aFile.path, array, { tmpPath: aFile.path + ".tmp" }).then(null, function onError(reason) {
console.log("Error writing default prefs: " + reason);
});
}
},
@ -7738,16 +7729,9 @@ var Distribution = {
return;
}
// Save the data for the later sessions
let ostream = Cc["@mozilla.org/network/safe-file-output-stream;1"].createInstance(Ci.nsIFileOutputStream);
ostream.init(this._file, 0x02 | 0x08 | 0x20, parseInt("600", 8), ostream.DEFER_OPEN);
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) { });
let array = new TextEncoder().encode(aData);
OS.File.writeAtomic(this._file.path, array, { tmpPath: this._file.path + ".tmp" });
break;
}
}
@ -7837,25 +7821,19 @@ var Distribution = {
// aFile is an nsIFile
// aCallback takes the parsed JSON object as a parameter
readJSON: function dc_readJSON(aFile, aCallback) {
if (!aFile.exists())
return;
let channel = NetUtil.newChannel(aFile);
channel.contentType = "application/json";
NetUtil.asyncFetch(channel, function(aStream, aResult) {
if (!Components.isSuccessCode(aResult)) {
Cu.reportError("Distribution: Could not read from " + aFile.leafName + " file");
return;
}
let raw = NetUtil.readInputStreamToString(aStream, aStream.available(), { charset : "UTF-8" }) || "";
aStream.close();
Task.spawn(function() {
let bytes = yield OS.File.read(aFile.path);
let raw = new TextDecoder().decode(bytes) || "";
try {
aCallback(JSON.parse(raw));
} catch (e) {
Cu.reportError("Distribution: Could not parse JSON: " + e);
}
}).then(null, function onError(reason) {
if (!(reason instanceof OS.File.Error && reason.becauseNoSuchFile)) {
Cu.reportError("Distribution: Could not read from " + aFile.leafName + " file");
}
});
}
};

Просмотреть файл

@ -11,6 +11,8 @@ function dump(a) {
const URI_GENERIC_ICON_DOWNLOAD = "drawable://alert_download";
XPCOMUtils.defineLazyModuleGetter(this, "OS", "resource://gre/modules/osfile.jsm");
var Downloads = {
_initialized: false,
_dlmgr: null,
@ -54,8 +56,8 @@ var Downloads = {
let fileURI = aDownload.target.spec;
let f = this._getLocalFile(fileURI);
if (f.exists())
f.remove(false);
OS.File.remove(f.path);
},
showAlert: function dl_showAlert(aDownload, aMessage, aTitle, aIcon) {

Просмотреть файл

@ -15,8 +15,7 @@ XPCOMUtils.defineLazyModuleGetter(this, "AddonManager",
XPCOMUtils.defineLazyModuleGetter(this, "AddonRepository",
"resource://gre/modules/AddonRepository.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "NetUtil",
"resource://gre/modules/NetUtil.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "OS", "resource://gre/modules/osfile.jsm");
function getPref(func, preference, defaultValue) {
try {
@ -130,19 +129,10 @@ var RecommendedSearchResults = {
if (!aData)
return;
// 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);
// 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) {
if (Components.isSuccessCode(rc))
Services.obs.notifyObservers(null, "recommended-addons-cache-updated", "");
let array = new TextEncoder().encode(aData);
OS.File.writeAtomic(aFile.path, array, { tmpPath: aFile.path + ".tmp" }).then(function onSuccess() {
Services.obs.notifyObservers(null, "recommended-addons-cache-updated", "");
});
},

Просмотреть файл

@ -133,10 +133,9 @@ HelperAppLauncherDialog.prototype = {
// Remove the file so that it's not there when we ensure non-existence later;
// this is safe because for the file to exist, the user would have had to
// confirm that he wanted the file overwritten.
if (file.exists())
file.remove(false);
file.remove(false);
}
catch (e) { }
catch (e) {}
var newDir = file.parent.QueryInterface(Ci.nsILocalFile);
prefs.setComplexValue("browser.download.lastDir", Ci.nsILocalFile, newDir);
file = this.validateLeafName(newDir, file.leafName, null);

Просмотреть файл

@ -15,8 +15,8 @@ XPCOMUtils.defineLazyServiceGetter(this, "CrashReporter",
"@mozilla.org/xre/app-info;1", "nsICrashReporter");
#endif
XPCOMUtils.defineLazyModuleGetter(this, "NetUtil",
"resource://gre/modules/NetUtil.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "Task", "resource://gre/modules/Task.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "OS", "resource://gre/modules/osfile.jsm");
function dump(a) {
Cc["@mozilla.org/consoleservice;1"].getService(Ci.nsIConsoleService).logStringMessage(a);
@ -66,17 +66,8 @@ SessionStore.prototype = {
},
_clearDisk: function ss_clearDisk() {
if (this._sessionFile.exists()) {
try {
this._sessionFile.remove(false);
} catch (ex) { dump(ex + '\n'); } // couldn't remove the file - what now?
}
if (this._sessionFileBackup.exists()) {
try {
this._sessionFileBackup.remove(false);
} catch (ex) { dump(ex + '\n'); } // couldn't remove the file - what now?
}
OS.File.remove(this._sessionFile.path);
OS.File.remove(this._sessionFileBackup.path);
},
_sendMessageToJava: function (aMsg) {
@ -155,8 +146,11 @@ SessionStore.prototype = {
// Move this session to sessionstore.bak so that:
// 1) we can get "tabs from last time" from sessionstore.bak
// 2) if sessionstore.js exists on next start, we know we crashed
if (this._sessionFile.exists())
this._sessionFile.moveTo(null, this._sessionFileBackup.leafName);
OS.File.move(this._sessionFile.path, this._sessionFileBackup.path).then(null, function onError(reason) {
if (!(reason instanceof OS.File.Error && reason.becauseNoSuchFile)) {
Cu.reportError("Error moving sessionstore files: " + reason);
}
});
observerService.removeObserver(this, "domwindowopened");
observerService.removeObserver(this, "domwindowclosed");
@ -564,20 +558,10 @@ SessionStore.prototype = {
if (!stateString.data)
return;
// 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);
// 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) {
if (Components.isSuccessCode(rc)) {
Services.obs.notifyObservers(null, "sessionstore-state-write-complete", "");
}
let array = new TextEncoder().encode(aData);
OS.File.writeAtomic(aFile.path, array, { tmpPath: aFile.path + ".tmp" }).then(function onSuccess() {
Services.obs.notifyObservers(null, "sessionstore-state-write-complete", "");
});
},
@ -1006,26 +990,17 @@ SessionStore.prototype = {
// session will be read from sessionstore.bak (which is also used for
// "tabs from last time").
if (aSessionString == null) {
if (!this._sessionFileBackup.exists()) {
throw "Session file doesn't exist";
}
let channel = NetUtil.newChannel(this._sessionFileBackup);
channel.contentType = "application/json";
NetUtil.asyncFetch(channel, function(aStream, aResult) {
try {
if (!Components.isSuccessCode(aResult)) {
throw "Could not fetch session file";
}
let data = NetUtil.readInputStreamToString(aStream, aStream.available(), { charset : "UTF-8" }) || "";
aStream.close();
restoreWindow(data);
} catch (e) {
Cu.reportError("SessionStore: " + e.message);
notifyObservers("fail");
Task.spawn(function() {
let bytes = yield OS.File.read(this._sessionFileBackup.path);
let data = JSON.parse(new TextDecoder().decode(bytes) || "");
restoreWindow(data);
}.bind(this)).then(null, function onError(reason) {
if (reason instanceof OS.File.Error && reason.becauseNoSuchFile) {
Cu.reportError("Session file doesn't exist");
} else {
Cu.reportError("SessionStore: " + reason.message);
}
notifyObservers("fail");
});
} else {
restoreWindow(aSessionString);