Bug 989098 - Use wrapper object to pass generic objects in sendResponse. r=wesj

This commit is contained in:
Brian Nicholson 2014-03-28 15:03:20 -07:00
parent 0633a33d8b
commit 3e747f49cf
8 changed files with 25 additions and 28 deletions

View File

@ -19,6 +19,8 @@ import java.util.concurrent.CopyOnWriteArrayList;
public final class EventDispatcher {
private static final String LOGTAG = "GeckoEventDispatcher";
private static final String GUID = "__guid__";
private static final String SUFFIX_RETURN = "Return";
private static final String SUFFIX_ERROR = "Error";
private final Map<String, CopyOnWriteArrayList<GeckoEventListener>> mEventListeners
= new HashMap<String, CopyOnWriteArrayList<GeckoEventListener>>();
@ -102,21 +104,22 @@ public final class EventDispatcher {
}
public static void sendResponse(JSONObject message, JSONObject response) {
try {
response.put(GUID, message.getString(GUID));
GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent(message.getString("type") + ":Return", response.toString()));
} catch (Exception ex) {
Log.e(LOGTAG, "Unable to send response", ex);
}
public static void sendResponse(JSONObject message, Object response) {
sendResponseHelper(SUFFIX_RETURN, message, response);
}
public static void sendError(JSONObject message, JSONObject error) {
public static void sendError(JSONObject message, Object response) {
sendResponseHelper(SUFFIX_ERROR, message, response);
}
private static void sendResponseHelper(String suffix, JSONObject message, Object response) {
try {
error.put(GUID, message.getString(GUID));
GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent(message.getString("type") + ":Error", error.toString()));
} catch (Exception ex) {
Log.e(LOGTAG, "Unable to send error", ex);
final JSONObject wrapper = new JSONObject();
wrapper.put(GUID, message.getString(GUID));
wrapper.put("response", response);
GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent(message.getString("type") + ":" + suffix, wrapper.toString()));
} catch (Exception e) {
Log.e(LOGTAG, "Unable to send " + suffix, e);
}
}
}

View File

@ -7254,7 +7254,7 @@ var WebappsUI = {
manifestURL: aData.app.manifestURL,
origin: origin
}, (data) => {
let profilePath = JSON.parse(data).profile;
let profilePath = data.profile;
if (!profilePath)
return;

View File

@ -42,7 +42,7 @@ let Accounts = Object.freeze({
if (error) {
deferred.reject(error);
} else {
deferred.resolve(JSON.parse(data).exists);
deferred.resolve(data.exists);
}
});

View File

@ -123,10 +123,10 @@ var HelperApps = {
let data = this._sendMessageSync(msg);
if (!data)
return [];
return parseData(JSON.parse(data));
return parseData(data);
} else {
sendMessageToJava(msg, function(data) {
callback(parseData(JSON.parse(data)));
callback(parseData(data));
});
}
},
@ -175,7 +175,7 @@ var HelperApps = {
});
sendMessageToJava(msg, function(data) {
callback(JSON.parse(data));
callback(data);
});
} else {
let msg = this._getMessage("Intent:Open", uri, {

View File

@ -27,8 +27,8 @@ function sendMessageToJava(aMessage, aCallback) {
Services.obs.removeObserver(obs, aMessage.type + ":Return", false);
Services.obs.removeObserver(obs, aMessage.type + ":Error", false);
aCallback(aTopic == aMessage.type + ":Return" ? aData : null,
aTopic == aMessage.type + ":Error" ? aData : null)
aCallback(aTopic == aMessage.type + ":Return" ? data.response : null,
aTopic == aMessage.type + ":Error" ? data.response : null);
}
}

View File

@ -159,17 +159,11 @@ Prompt.prototype = {
show: function(callback) {
this.callback = callback;
log("Sending message");
Services.obs.addObserver(this, "Prompt:Return", false);
this._innerShow();
},
_innerShow: function() {
sendMessageToJava(this.msg, (aData) => {
log("observe " + aData);
let data = JSON.parse(aData);
Services.obs.removeObserver(this, "Prompt:Return", false);
sendMessageToJava(this.msg, (data) => {
if (this.callback)
this.callback(data);
});

View File

@ -66,7 +66,7 @@ SharedPreferences.prototype = Object.freeze({
preferences: prefs,
branch: this._branch,
}, (data) => {
result = JSON.parse(data).values;
result = data.values;
});
let thread = Services.tm.currentThread;

View File

@ -338,7 +338,7 @@ this.WebappManager = {
sendMessageToJava({
type: "Webapps:GetApkVersions",
packageNames: packageNames
}, data => deferred.resolve(JSON.parse(data).versions));
}, data => deferred.resolve(data.versions));
return deferred.promise;
},