Permalink
Browse files

Download modified scripts' dependencies.

  • Loading branch information...
1 parent f8363f5 commit c7ce9b74f81b425ea8657bad6c714c91f389e190 @arantius arantius committed Dec 1, 2011
Showing with 58 additions and 27 deletions.
  1. +8 −4 content/script.js
  2. +50 −23 modules/remoteScript.js
View
@@ -480,10 +480,14 @@ Script.prototype.updateFromNewScript = function(newScript, safeWin, chromeWin) {
}
// Re-download dependencies. The timeout guarantees that it will
- // reliably complete after the normal document-end time. (See #1402; going
- // from some -> no requires means this is a short-circuit call.)
- var scriptDownloader = new GM_ScriptDownloader(null, null, null);
- GM_util.timeout(0, GM_util.hitch(scriptDownloader, 'startUpdateScript', this));
+ // reliably complete after the normal document-end time.
+ var scope = {};
+ Components.utils.import('resource://greasemonkey/remoteScript.js', scope);
+ var rs = new scope.RemoteScript(this._downloadURL);
+ rs.setScript(newScript);
+ rs.download(GM_util.hitch(this, function(aSuccess) {
+ rs.install(this);
+ }));
}
};
@@ -215,23 +215,35 @@ RemoteScript.prototype.install = function(aOldScript) {
if (!this.script) {
throw new Error('RemoteScript.install(): Script is not downloaded.');
}
- if (!this._baseName) {
- throw new Error('RemoteScript.install(): Script base name unknown.');
- }
- var suffix = 0;
- var file = GM_util.scriptDir();
- file.append(this._baseName);
- while (file.exists()) {
- suffix++;
- file = GM_util.scriptDir();
- file.append(this._baseName + '-' + suffix);
- }
- this._baseName = file.leafName;
+ if (aOldScript) {
+ // Just move the dependencies in.
+ var enumerator = this._tempDir.directoryEntries;
+ while (enumerator.hasMoreElements()) {
+ var file = enumerator.getNext().QueryInterface(Ci.nsILocalFile);
+ // TODO: Fix invalid private access.
+ file.moveTo(this.script._basedirFile, null);
+ }
+ } else {
+ // Completely install the new script.
+ if (!this._baseName) {
+ throw new Error('RemoteScript.install(): Script base name unknown.');
+ }
+
+ var suffix = 0;
+ var file = GM_util.scriptDir();
+ file.append(this._baseName);
+ while (file.exists()) {
+ suffix++;
+ file = GM_util.scriptDir();
+ file.append(this._baseName + '-' + suffix);
+ }
+ this._baseName = file.leafName;
- this.script.setFilename(this._baseName, this._scriptFile.leafName);
- this._tempDir.moveTo(GM_util.scriptDir(), this._baseName);
- this._tempDir = null;
+ this.script.setFilename(this._baseName, this._scriptFile.leafName);
+ this._tempDir.moveTo(GM_util.scriptDir(), this._baseName);
+ this._tempDir = null;
+ }
GM_config.install(this.script, aOldScript);
};
@@ -248,6 +260,17 @@ RemoteScript.prototype.onScriptMeta = function(aCallback) {
this._scriptMetaCallbacks.push(aCallback);
};
+/** Set the (installed) script, in order to download modified dependencies.
+ *
+ * After calling this, calling .download() will only get dependencies. This
+ * RemoteScript can then safely be .install(oldScript)'ed.
+ */
+RemoteScript.prototype.setScript = function(aScript) {
+ this._scriptFile = aScript.file;
+ this.script = aScript;
+ this._postParseScriptFile();
+}
+
RemoteScript.prototype.showSource = function(aTabBrowser) {
// Turn standard browser into tab browser, if necessary.
if (aTabBrowser.getTabBrowser) aTabBrowser = aTabBrowser.getTabBrowser()
@@ -384,14 +407,7 @@ RemoteScript.prototype._downloadFileProgress = function(
this.script = this._parseScriptFile();
if (this.script) {
// And if successful, prepare to download dependencies.
- this._dependencies = this.script.requires.concat(this.script.resources);
- if (this.script.icon.hasDownloadURL()) {
- this._dependencies.push(this.script.icon);
- }
- this._progress = [];
- for (var i = 0; i < this._dependencies.length; i++) {
- this._progress[i] = 0;
- }
+ this._postParseScriptFile();
}
}
@@ -447,3 +463,14 @@ RemoteScript.prototype._parseScriptFile = function(aForce) {
return null;
};
+
+RemoteScript.prototype._postParseScriptFile = function() {
+ this._dependencies = this.script.requires.concat(this.script.resources);
+ if (this.script.icon.hasDownloadURL()) {
+ this._dependencies.push(this.script.icon);
+ }
+ this._progress = [];
+ for (var i = 0; i < this._dependencies.length; i++) {
+ this._progress[i] = 0;
+ }
+};

0 comments on commit c7ce9b7

Please sign in to comment.