Skip to content

Commit

Permalink
Cleanly display a message when downloading a non UTF-8 script.
Browse files Browse the repository at this point in the history
  • Loading branch information
arantius committed Jul 16, 2012
1 parent 311e9d9 commit f934d77
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 9 deletions.
1 change: 1 addition & 0 deletions locale/en-US/greasemonkey.properties
Expand Up @@ -5,6 +5,7 @@ error.matchPattern.parse=@match: Could not parse the pattern.
error.matchPattern.path=@match: Invalid path specified.
error.matchPattern.scheme=@match: Invalid scheme specified.
error.parsingScript=Could not parse script:
error.scriptCharset=Error reading script: All Greasemonkey scripts MUST be encoded with UTF-8.
error.serverReturned=Server returned
error.unknown=Unknown error.
warning.scripts-should-grant=Warning: All scripts should specify @grant metadata.
Expand Down
33 changes: 29 additions & 4 deletions modules/remoteScript.js
Expand Up @@ -101,7 +101,10 @@ DownloadListener.prototype = {
var converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"]
.createInstance(Ci.nsIScriptableUnicodeConverter);
converter.charset = 'UTF-8';
var source = converter.convertFromByteArray(this._data, this._data.length)
var source = '';
try {
source = converter.convertFromByteArray(this._data, this._data.length);
} catch (e) { }
return this._remoteScript.parseScript(source, false);
},

Expand Down Expand Up @@ -524,7 +527,23 @@ RemoteScript.prototype._downloadScriptCb = function(
aCompletionCallback, aChannel, aSuccess) {
if (aSuccess) {
// At this point downloading the script itself is definitely done.
this._parseScriptFile();

// Parse the script.
try {
this._parseScriptFile();
} catch (e) {
// If that failed, set the error message, and ...
if (-1 === new String(e).indexOf('Unicode')) {
this.cleanup(stringBundle.GetStringFromName('error.unknown'));
} else {
this.cleanup(stringBundle.GetStringFromName('error.scriptCharset'));
}
// ... fake a successful download, so the install window will show, with
// that error message.
this._dispatchCallbacks('scriptMeta', new Script());
return aCompletionCallback(true, 'script');
}

if (!this.script) {
dump('RemoteScript: finishing with error because no script was found.\n');
// If we STILL don't have a script, this is a fatal error.
Expand All @@ -538,9 +557,15 @@ RemoteScript.prototype._downloadScriptCb = function(

RemoteScript.prototype._parseScriptFile = function() {
if (this.done) return;
var source = GM_util.getContents(this._scriptFile);
var source = GM_util.getContents(this._scriptFile, null, true);
if (!source) return null;
return this.parseScript(source, true);
var script = null;
try {
this.parseScript(source, true);
} catch (e) {
dump('RemoteScript._parseScriptFile: ' + e + '\n');
}
return script;
};

RemoteScript.prototype._postParseScript = function() {
Expand Down
10 changes: 5 additions & 5 deletions modules/util/getContents.js
Expand Up @@ -11,15 +11,14 @@ const unicodeConverter = Components
.classes["@mozilla.org/intl/scriptableunicodeconverter"]
.createInstance(Components.interfaces.nsIScriptableUnicodeConverter);

function getContents(file, charset) {
if (!charset) charset = "UTF-8";
unicodeConverter.charset = charset;
function getContents(aFile, aCharset, aFatal) {
unicodeConverter.charset = aCharset || 'UTF-8';

var channel = ioService.newChannelFromURI(GM_util.getUriFromFile(file));
var channel = ioService.newChannelFromURI(GM_util.getUriFromFile(aFile));
try {
var input = channel.open();
} catch (e) {
GM_util.logError(new Error("Could not open file: " + file.path));
GM_util.logError(new Error("Could not open file: " + aFile.path));
return "";
}

Expand All @@ -31,6 +30,7 @@ function getContents(file, charset) {
try {
return unicodeConverter.ConvertToUnicode(str);
} catch (e) {
if (aFatal) throw e;
return str;
}
}

0 comments on commit f934d77

Please sign in to comment.