| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.components.gcm_driver; | 5 package org.chromium.components.gcm_driver; |
| 6 | 6 |
| 7 import android.content.Context; | 7 import android.content.Context; |
| 8 import android.os.AsyncTask; | 8 import android.os.AsyncTask; |
| 9 import android.os.Bundle; | |
| 10 | 9 |
| 11 import org.chromium.base.Log; | 10 import org.chromium.base.Log; |
| 12 import org.chromium.base.ThreadUtils; | 11 import org.chromium.base.ThreadUtils; |
| 13 import org.chromium.base.VisibleForTesting; | 12 import org.chromium.base.VisibleForTesting; |
| 14 import org.chromium.base.annotations.CalledByNative; | 13 import org.chromium.base.annotations.CalledByNative; |
| 15 import org.chromium.base.annotations.JNINamespace; | 14 import org.chromium.base.annotations.JNINamespace; |
| 16 | 15 |
| 17 import java.io.IOException; | 16 import java.io.IOException; |
| 18 import java.util.ArrayList; | |
| 19 import java.util.List; | |
| 20 | 17 |
| 21 /** | 18 /** |
| 22 * This class is the Java counterpart to the C++ GCMDriverAndroid class. | 19 * This class is the Java counterpart to the C++ GCMDriverAndroid class. |
| 23 * It uses Android's Java GCM APIs to implement GCM registration etc, and | 20 * It uses Android's Java GCM APIs to implement GCM registration etc, and |
| 24 * sends back GCM messages over JNI. | 21 * sends back GCM messages over JNI. |
| 25 * | 22 * |
| 26 * Threading model: all calls to/from C++ happen on the UI thread. | 23 * Threading model: all calls to/from C++ happen on the UI thread. |
| 27 */ | 24 */ |
| 28 @JNINamespace("gcm") | 25 @JNINamespace("gcm") |
| 29 public class GCMDriver { | 26 public class GCMDriver { |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 108 } | 105 } |
| 109 | 106 |
| 110 @Override | 107 @Override |
| 111 protected void onPostExecute(Boolean success) { | 108 protected void onPostExecute(Boolean success) { |
| 112 nativeOnUnregisterFinished(mNativeGCMDriverAndroid, appId, succe
ss); | 109 nativeOnUnregisterFinished(mNativeGCMDriverAndroid, appId, succe
ss); |
| 113 } | 110 } |
| 114 }.execute(); | 111 }.execute(); |
| 115 } | 112 } |
| 116 | 113 |
| 117 // The caller of this function is responsible for ensuring the browser proce
ss is initialized. | 114 // The caller of this function is responsible for ensuring the browser proce
ss is initialized. |
| 118 public static void onMessageReceived(String appId, String senderId, Bundle e
xtras) { | 115 public static void dispatchMessage(GCMMessage message) { |
| 119 // TODO(johnme): Store message and redeliver later if Chrome is killed b
efore delivery. | |
| 120 ThreadUtils.assertOnUiThread(); | 116 ThreadUtils.assertOnUiThread(); |
| 117 |
| 121 if (sInstance == null) { | 118 if (sInstance == null) { |
| 122 // Change of behaviour, throw exception instead of failing silently
with Log.e. | |
| 123 throw new RuntimeException("Failed to instantiate GCMDriver."); | 119 throw new RuntimeException("Failed to instantiate GCMDriver."); |
| 124 } | 120 } |
| 125 final String bundleSubtype = "subtype"; | |
| 126 final String bundleSenderId = "from"; | |
| 127 final String bundleCollapseKey = "collapse_key"; | |
| 128 final String bundleRawData = "rawData"; | |
| 129 final String bundleGcmplex = "com.google.ipc.invalidation.gcmmplex."; | |
| 130 | 121 |
| 131 String collapseKey = extras.getString(bundleCollapseKey); // May be nul
l. | 122 sInstance.nativeOnMessageReceived(sInstance.mNativeGCMDriverAndroid, mes
sage.getAppId(), |
| 132 byte[] rawData = extras.getByteArray(bundleRawData); // May be null. | 123 message.getSenderId(), message.getCollapseKey(), message.getRawD
ata(), |
| 133 | 124 message.getDataKeysAndValuesArray()); |
| 134 List<String> dataKeysAndValues = new ArrayList<String>(); | |
| 135 for (String key : extras.keySet()) { | |
| 136 // TODO(johnme): Check there aren't other keys that we need to exclu
de. | |
| 137 if (key.equals(bundleSubtype) || key.equals(bundleSenderId) | |
| 138 || key.equals(bundleCollapseKey) || key.equals(bundleRawData
) | |
| 139 || key.startsWith(bundleGcmplex)) { | |
| 140 continue; | |
| 141 } | |
| 142 Object value = extras.get(key); | |
| 143 if (!(value instanceof String)) { | |
| 144 continue; | |
| 145 } | |
| 146 dataKeysAndValues.add(key); | |
| 147 dataKeysAndValues.add((String) value); | |
| 148 } | |
| 149 | |
| 150 sInstance.nativeOnMessageReceived(sInstance.mNativeGCMDriverAndroid, | |
| 151 appId, senderId, collapseKey, rawData, | |
| 152 dataKeysAndValues.toArray(new String[dataKeysAndValues.size()]))
; | |
| 153 } | 125 } |
| 154 | 126 |
| 155 @VisibleForTesting | 127 @VisibleForTesting |
| 156 public static void overrideSubscriberForTesting(GoogleCloudMessagingSubscrib
er subscriber) { | 128 public static void overrideSubscriberForTesting(GoogleCloudMessagingSubscrib
er subscriber) { |
| 157 assert sInstance != null; | 129 assert sInstance != null; |
| 158 assert subscriber != null; | 130 assert subscriber != null; |
| 159 sInstance.mSubscriber = subscriber; | 131 sInstance.mSubscriber = subscriber; |
| 160 } | 132 } |
| 161 | 133 |
| 162 private native void nativeOnRegisterFinished(long nativeGCMDriverAndroid, St
ring appId, | 134 private native void nativeOnRegisterFinished(long nativeGCMDriverAndroid, St
ring appId, |
| 163 String registrationId, boolean success); | 135 String registrationId, boolean success); |
| 164 private native void nativeOnUnregisterFinished(long nativeGCMDriverAndroid,
String appId, | 136 private native void nativeOnUnregisterFinished(long nativeGCMDriverAndroid,
String appId, |
| 165 boolean success); | 137 boolean success); |
| 166 private native void nativeOnMessageReceived(long nativeGCMDriverAndroid, Str
ing appId, | 138 private native void nativeOnMessageReceived(long nativeGCMDriverAndroid, Str
ing appId, |
| 167 String senderId, String collapseKey, byte[] rawData, String[] dataKe
ysAndValues); | 139 String senderId, String collapseKey, byte[] rawData, String[] dataKe
ysAndValues); |
| 168 } | 140 } |
| OLD | NEW |