--- a/mobile/android/base/java/org/mozilla/gecko/push/PushService.java
+++ b/mobile/android/base/java/org/mozilla/gecko/push/PushService.java
@@ -83,27 +83,27 @@ public class PushService implements Bund
sInstance.registerGeckoEventListener();
sInstance.onStartup();
}
protected final PushManager pushManager;
private boolean canSendPushMessagesToGecko;
- private final List<String> pendingPushMessages;
+ private final List<JSONObject> pendingPushMessages;
public PushService(Context context) {
pushManager = new PushManager(new PushState(context, "GeckoPushState.json"), new GcmTokenClient(context), new PushManager.PushClientFactory() {
@Override
public PushClient getPushClient(String autopushEndpoint, boolean debug) {
return new PushClient(autopushEndpoint);
}
});
- pendingPushMessages = new LinkedList<String>();
+ pendingPushMessages = new LinkedList<>();
}
public void onStartup() {
Log.i(LOG_TAG, "Starting up.");
ThreadUtils.assertOnBackgroundThread();
try {
pushManager.startup(System.currentTimeMillis());
@@ -162,62 +162,63 @@ public class PushService implements Bund
final String profileName = subscription.serviceData.optString("profileName", null);
final String profilePath = subscription.serviceData.optString("profilePath", null);
if (profileName == null || profilePath == null) {
Log.e(LOG_TAG, "Corrupt serviceData found for chid: " + chid + "; ignoring dom/push message.");
return;
}
- final File profileDir = new File(profilePath);
- if (!GeckoThread.canUseProfile(profileName, profileDir)) {
- Log.e(LOG_TAG, "Mismatched profile for chid: " + chid + "; ignoring dom/push message.");
- return;
- }
-
- if (!canSendPushMessagesToGecko) {
+ if (canSendPushMessagesToGecko) {
+ if (!GeckoThread.canUseProfile(profileName, new File(profilePath))) {
+ Log.e(LOG_TAG, "Mismatched profile for chid: " + chid + "; ignoring dom/push message.");
+ return;
+ }
+ } else {
final Intent intent = GeckoService.getIntentToCreateServices(context, "android-push-service");
GeckoService.setIntentProfile(intent, profileName, profilePath);
context.startService(intent);
}
// DELIVERANCE!
final JSONObject data = new JSONObject();
try {
data.put("channelID", chid);
data.put("con", bundle.getString("con"));
data.put("enc", bundle.getString("enc"));
// Only one of cryptokey (newer) and enckey (deprecated) should be set, but the
// Gecko handler will verify this.
data.put("cryptokey", bundle.getString("cryptokey"));
data.put("enckey", bundle.getString("enckey"));
data.put("message", bundle.getString("body"));
+
+ if (!canSendPushMessagesToGecko) {
+ data.put("profileName", profileName);
+ data.put("profilePath", profilePath);
+ }
} catch (JSONException e) {
Log.e(LOG_TAG, "Got exception delivering dom/push message to Gecko!", e);
return;
}
- enqueueOrSendMessage(data.toString());
+ if (canSendPushMessagesToGecko) {
+ sendMessageToGeckoService(data);
+ } else {
+ Log.i(LOG_TAG, "Service not initialized, adding message to queue.");
+ pendingPushMessages.add(data);
+ }
} else {
Log.e(LOG_TAG, "Message directed to unknown service; dropping: " + subscription.service);
}
}
- protected void enqueueOrSendMessage(final @NonNull String message) {
- if (canSendPushMessagesToGecko) {
- sendMessageToGeckoService(message);
- } else {
- Log.i(LOG_TAG, "Service not initialized, adding message to queue.");
- pendingPushMessages.add(message);
- }
- }
-
- protected void sendMessageToGeckoService(final @NonNull String message) {
+ protected void sendMessageToGeckoService(final @NonNull JSONObject message) {
Log.i(LOG_TAG, "Delivering dom/push message to Gecko!");
- GeckoAppShell.notifyObservers("PushServiceAndroidGCM:ReceivedPushMessage", message,
+ GeckoAppShell.notifyObservers("PushServiceAndroidGCM:ReceivedPushMessage",
+ message.toString(),
GeckoThread.State.PROFILE_READY);
}
protected void registerGeckoEventListener() {
Log.d(LOG_TAG, "Registered Gecko event listener.");
EventDispatcher.getInstance().registerBackgroundThreadListener(this, GECKO_EVENTS);
}
@@ -274,17 +275,26 @@ public class PushService implements Bund
return;
}
if ("PushServiceAndroidGCM:Initialized".equals(event)) {
// Send all pending messages to Gecko and set the
// canSendPushMessageToGecko flag to true so that
// all new push messages are sent directly to Gecko
// instead of being queued.
canSendPushMessagesToGecko = true;
- for (String pushMessage : pendingPushMessages) {
+ for (JSONObject pushMessage : pendingPushMessages) {
+ final String profileName = pushMessage.optString("profileName", null);
+ final String profilePath = pushMessage.optString("profilePath", null);
+ if (profileName == null || profilePath == null ||
+ !GeckoThread.canUseProfile(profileName, new File(profilePath))) {
+ Log.e(LOG_TAG, "Mismatched profile for chid: " +
+ pushMessage.optString("channelID") +
+ "; ignoring dom/push message.");
+ continue;
+ }
sendMessageToGeckoService(pushMessage);
}
pendingPushMessages.clear();
callback.sendSuccess(null);
return;
}
if ("PushServiceAndroidGCM:Uninitialized".equals(event)) {
canSendPushMessagesToGecko = false;