Permalink
Browse files

Move writeToFile into an util module.

And create the constants module to suppor it (and their usage elsewhere).
  • Loading branch information...
1 parent 1f010e5 commit 3438a6d154ad1c1058bc07de589f22c13dc3794d @arantius arantius committed Aug 22, 2011
View
11 content/config.js
@@ -1,6 +1,7 @@
-Components.utils.import("resource://greasemonkey/third-party/MatchPattern.js");
+Components.utils.import('resource://greasemonkey/constants.js');
Components.utils.import('resource://greasemonkey/prefmanager.js');
-Components.utils.import("resource://greasemonkey/util.js");
+Components.utils.import('resource://greasemonkey/third-party/MatchPattern.js');
+Components.utils.import('resource://greasemonkey/util.js');
function Config() {
this._saveTimer = null;
@@ -120,7 +121,7 @@ Config.prototype._save = function(saveNow) {
var domSerializer = Components
.classes["@mozilla.org/xmlextras/xmlserializer;1"]
.createInstance(Components.interfaces.nsIDOMSerializer);
- GM_writeToFile(domSerializer.serializeToString(doc), this._configFile);
+ GM_util.writeToFile(domSerializer.serializeToString(doc), this._configFile);
};
Config.prototype.parse = function(source, uri, updateScript) {
@@ -368,8 +369,8 @@ Config.prototype.move = function(script, destination) {
Config.prototype._initScriptDir = function() {
var dir = GM_scriptDir();
if (!dir.exists()) {
- dir.create(Components.interfaces.nsIFile.DIRECTORY_TYPE, GM_directoryMask);
- GM_writeToFile("<UserScriptConfig/>", this._configFile);
+ dir.create(Components.interfaces.nsIFile.DIRECTORY_TYPE, GM_constants.directoryMask);
+ GM_util.writeToFile("<UserScriptConfig/>", this._configFile);
}
};
View
2 content/newscript.js
@@ -38,7 +38,7 @@ function doInstall() {
// finish making the script object ready to install
// (put this created script into a file -- only way to install it)
var tempFile = GM_getTempFile();
- GM_writeToFile(scriptSrc, tempFile, function() {
+ GM_util.writeToFile(scriptSrc, tempFile, function() {
script.setDownloadedFile(tempFile);
// install this script
View
9 content/script.js
@@ -1,6 +1,7 @@
+Components.utils.import('resource://greasemonkey/constants.js');
Components.utils.import('resource://greasemonkey/prefmanager.js');
-Components.utils.import("resource://greasemonkey/third-party/convert2RegExp.js");
-Components.utils.import("resource://greasemonkey/third-party/MatchPattern.js");
+Components.utils.import('resource://greasemonkey/third-party/MatchPattern.js');
+Components.utils.import('resource://greasemonkey/third-party/convert2RegExp.js');
Components.utils.import('resource://greasemonkey/util.js');
function Script(configNode) {
@@ -361,11 +362,11 @@ Script.prototype._initFile = function(tempFile) {
var nsIFile = Components.interfaces.nsIFile;
var file = GM_scriptDir();
file.append(name);
- file.createUnique(nsIFile.DIRECTORY_TYPE, GM_directoryMask);
+ file.createUnique(nsIFile.DIRECTORY_TYPE, GM_constants.directoryMask);
this._basedir = file.leafName;
file.append(name + ".user.js");
- file.createUnique(nsIFile.NORMAL_FILE_TYPE, GM_fileMask);
+ file.createUnique(nsIFile.NORMAL_FILE_TYPE, GM_constants.fileMask);
this._filename = file.leafName;
GM_log("Moving script file from " + tempFile.path + " to " + file.path);
View
7 content/scriptdownloader.js
@@ -1,4 +1,5 @@
-Components.utils.import("resource://greasemonkey/util.js");
+Components.utils.import('resource://greasemonkey/constants.js');
+Components.utils.import('resource://greasemonkey/util.js');
var GM_ScriptDownloader;
(function private_scope() {
@@ -107,13 +108,13 @@ GM_ScriptDownloader.prototype.handleScriptDownloadComplete = function() {
var base = this.script.name.replace(/[^A-Z0-9_]/gi, "").toLowerCase();
file.append(base + ".user.js");
file.createUnique(
- Components.interfaces.nsILocalFile.NORMAL_FILE_TYPE, GM_fileMask);
+ Components.interfaces.nsILocalFile.NORMAL_FILE_TYPE, GM_constants.fileMask);
this.tempFiles_.push(file);
function handleWriteComplete() {
}
- GM_writeToFile(source, file,
+ GM_util.writeToFile(source, file,
GM_util.hitch(this, 'handleScriptDownloadWriteComplete', file));
} catch (e) {
// NOTE: unlocalized string
View
3 content/scriptrequire.js
@@ -1,3 +1,4 @@
+Components.utils.import('resource://greasemonkey/constants.js');
Components.utils.import('resource://greasemonkey/util.js');
function ScriptRequire(script) {
@@ -38,7 +39,7 @@ ScriptRequire.prototype._initFile = function() {
var file = this._script._basedirFile;
file.append(name);
file.createUnique(
- Components.interfaces.nsIFile.NORMAL_FILE_TYPE, GM_fileMask);
+ Components.interfaces.nsIFile.NORMAL_FILE_TYPE, GM_constants.fileMask);
this._filename = file.leafName;
GM_log("Moving dependency file from " + this._tempFile.path + " to " + file.path);
View
40 content/utils.js
@@ -1,6 +1,4 @@
-Components.utils.import("resource://gre/modules/NetUtil.jsm");
-Components.utils.import("resource://gre/modules/FileUtils.jsm");
-
+Components.utils.import('resource://greasemonkey/constants.js');
Components.utils.import('resource://greasemonkey/prefmanager.js');
Components.utils.import('resource://greasemonkey/util.js');
// Load module-ized methods here for legacy access.
@@ -15,8 +13,6 @@ var GM_stringBundle = Components
.getService(Components.interfaces.nsIStringBundleService)
.createBundle("chrome://greasemonkey/locale/gm-browser.properties");
-var GM_directoryMask = parseInt('750', 8);
-var GM_fileMask = parseInt('640', 8);
function GM_getService() {
return Components
@@ -71,7 +67,7 @@ function GM_getTempFile() {
file.append("gm-temp");
file.createUnique(
- Components.interfaces.nsILocalFile.NORMAL_FILE_TYPE, GM_fileMask);
+ Components.interfaces.nsILocalFile.NORMAL_FILE_TYPE, GM_constants.fileMask);
return file;
}
@@ -146,35 +142,3 @@ function GM_getBrowserWindow() {
.getService(Components.interfaces.nsIWindowMediator)
.getMostRecentWindow("navigator:browser");
}
-
-
-/** Given string data and an nsIFile, write it safely to that file. */
-function GM_writeToFile(aData, aFile, aCallback) {
- // PR_WRONLY PR_CREATE_FILE PR_TRUNCATE
- var _streamFlags = 0x02 | 0x08 | 0x20;
-
- // Assume aData is a string; convert it to a UTF-8 stream.
- var converter = Components
- .classes["@mozilla.org/intl/scriptableunicodeconverter"]
- .createInstance(Components.interfaces.nsIScriptableUnicodeConverter);
- converter.charset = "UTF-8";
- var istream = converter.convertToInputStream(aData);
-
- // Create a temporary file (stream) to hold the data.
- var tmpFile = aFile.clone();
- tmpFile.createUnique(
- Components.interfaces.nsILocalFile.NORMAL_FILE_TYPE, GM_fileMask);
- var ostream = Components
- .classes["@mozilla.org/network/safe-file-output-stream;1"]
- .createInstance(Components.interfaces.nsIFileOutputStream);
- ostream.init(tmpFile, _streamFlags, GM_fileMask, 0);
-
- NetUtil.asyncCopy(istream, ostream, function(status) {
- if (Components.isSuccessCode(status)) {
- // On successful write, move it to the real location.
- tmpFile.moveTo(aFile.parent, aFile.leafName);
-
- if (aCallback) aCallback();
- }
- });
-}
View
6 modules/constants.js
@@ -0,0 +1,6 @@
+const EXPORTED_SYMBOLS = ['GM_constants'];
+
+const GM_constants = {
+ 'directoryMask': parseInt('750', 8),
+ 'fileMask': parseInt('640', 8),
+};
View
37 modules/util/writeToFile.js
@@ -0,0 +1,37 @@
+Components.utils.import('resource://gre/modules/NetUtil.jsm');
+Components.utils.import('resource://greasemonkey/constants.js');
+
+const EXPORTED_SYMBOLS = ['writeToFile'];
+
+const NORMAL_FILE_TYPE = Components.interfaces.nsILocalFile.NORMAL_FILE_TYPE;
+// PR_WRONLY PR_CREATE_FILE PR_TRUNCATE
+const STREAM_FLAGS = 0x02 | 0x08 | 0x20;
+
+const converter = Components
+ .classes["@mozilla.org/intl/scriptableunicodeconverter"]
+ .createInstance(Components.interfaces.nsIScriptableUnicodeConverter);
+converter.charset = "UTF-8";
+
+const ostream = Components
+ .classes["@mozilla.org/network/safe-file-output-stream;1"]
+ .createInstance(Components.interfaces.nsIFileOutputStream);
+
+/** Given string data and an nsIFile, write it safely to that file. */
+function writeToFile(aData, aFile, aCallback) {
+ // Assume aData is a string; convert it to a UTF-8 stream.
+ var istream = converter.convertToInputStream(aData);
+
+ // Create a temporary file (stream) to hold the data.
+ var tmpFile = aFile.clone();
+ tmpFile.createUnique(NORMAL_FILE_TYPE, GM_constants.fileMask);
+ ostream.init(tmpFile, STREAM_FLAGS, GM_constants.fileMask, 0);
+
+ NetUtil.asyncCopy(istream, ostream, function(status) {
+ if (Components.isSuccessCode(status)) {
+ // On successful write, move it to the real location.
+ tmpFile.moveTo(aFile.parent, aFile.leafName);
+
+ if (aCallback) aCallback();
+ }
+ });
+}

0 comments on commit 3438a6d

Please sign in to comment.