Bug 995820 - Wait for JavaBridge to load before sending messages to it; r=mcomella

This commit is contained in:
Jim Chen 2014-04-15 08:42:00 -07:00
parent b146a467b0
commit 9e3fc9d88d
2 changed files with 16 additions and 0 deletions

View File

@ -67,6 +67,8 @@ public final class JavascriptBridge {
private JSONObject mSavedAsyncMessage;
// Number of levels in the synchronous call stack
private int mCallStackDepth;
// If JavaBridge has been loaded
private boolean mJavaBridgeLoaded;
/* package */ static void init(final UITestContext context) {
sActions = context.getActions();
@ -181,7 +183,15 @@ public final class JavascriptBridge {
} while (result != MessageStatus.QUEUE_EMPTY);
}
private void ensureJavaBridgeLoaded() {
while (!mJavaBridgeLoaded) {
processPendingMessage();
}
}
private void sendMessage(final String innerType, final String method, final Object[] args) {
ensureJavaBridgeLoaded();
// Call from Java to Javascript
final JSONObject message = new JSONObject();
final JSONArray jsonArgs = new JSONArray();
@ -217,6 +227,10 @@ public final class JavascriptBridge {
mLogParser.logMessage(message.getString("message"));
return MessageStatus.PROCESSED;
} else if ("notify-loaded".equals(type)) {
mJavaBridgeLoaded = true;
return MessageStatus.PROCESSED;
} else if ("sync-reply".equals(type)) {
// Reply to Java-to-Javascript sync call
return MessageStatus.REPLIED;

View File

@ -1157,6 +1157,8 @@ function JavaBridge(obj) {
// The number of replies needed to answer all outstanding sync calls.
this._repliesNeeded = 0;
this._Services.obs.addObserver(this, this._EVENT_TYPE, false);
this._sendMessage("notify-loaded", []);
};
JavaBridge.prototype = {