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
3 // found in the LICENSE file.
5 #include "components/gcm_driver/gcm_driver_android.h"
9 #include "base/android/jni_android.h"
10 #include "base/android/jni_array.h"
11 #include "base/android/jni_string.h"
12 #include "base/compiler_specific.h"
13 #include "base/logging.h"
14 #include "jni/GCMDriver_jni.h"
16 using base::android::AppendJavaStringArrayToStringVector
;
17 using base::android::AttachCurrentThread
;
18 using base::android::ConvertJavaStringToUTF8
;
19 using base::android::ConvertUTF8ToJavaString
;
20 using base::android::JavaByteArrayToByteVector
;
24 GCMDriverAndroid::GCMDriverAndroid(
25 const base::FilePath
& store_path
,
26 const scoped_refptr
<base::SequencedTaskRunner
>& blocking_task_runner
)
27 : GCMDriver(store_path
, blocking_task_runner
) {
28 JNIEnv
* env
= AttachCurrentThread();
30 Java_GCMDriver_create(env
,
31 reinterpret_cast<intptr_t>(this),
32 base::android::GetApplicationContext()));
35 GCMDriverAndroid::~GCMDriverAndroid() {
36 JNIEnv
* env
= AttachCurrentThread();
37 Java_GCMDriver_destroy(env
, java_ref_
.obj());
40 void GCMDriverAndroid::OnRegisterFinished(JNIEnv
* env
,
43 jstring j_registration_id
,
45 std::string app_id
= ConvertJavaStringToUTF8(env
, j_app_id
);
46 std::string registration_id
= ConvertJavaStringToUTF8(env
, j_registration_id
);
47 GCMClient::Result result
= success
? GCMClient::SUCCESS
48 : GCMClient::UNKNOWN_ERROR
;
50 RegisterFinished(app_id
, registration_id
, result
);
53 void GCMDriverAndroid::OnUnregisterFinished(JNIEnv
* env
,
57 std::string app_id
= ConvertJavaStringToUTF8(env
, j_app_id
);
58 GCMClient::Result result
= success
? GCMClient::SUCCESS
59 : GCMClient::UNKNOWN_ERROR
;
61 UnregisterFinished(app_id
, result
);
64 void GCMDriverAndroid::OnMessageReceived(JNIEnv
* env
,
68 jstring j_collapse_key
,
69 jbyteArray j_raw_data
,
70 jobjectArray j_data_keys_and_values
) {
71 std::string app_id
= ConvertJavaStringToUTF8(env
, j_app_id
);
73 IncomingMessage message
;
74 message
.sender_id
= ConvertJavaStringToUTF8(env
, j_sender_id
);
75 message
.collapse_key
= ConvertJavaStringToUTF8(env
, j_collapse_key
);
76 // Expand j_data_keys_and_values from array to map.
77 std::vector
<std::string
> data_keys_and_values
;
78 AppendJavaStringArrayToStringVector(env
,
79 j_data_keys_and_values
,
80 &data_keys_and_values
);
81 for (size_t i
= 0; i
+ 1 < data_keys_and_values
.size(); i
+= 2) {
82 message
.data
[data_keys_and_values
[i
]] = data_keys_and_values
[i
+1];
84 // Convert j_raw_data from byte[] to binary std::string.
86 std::vector
<uint8_t> raw_data
;
87 JavaByteArrayToByteVector(env
, j_raw_data
, &raw_data
);
88 message
.raw_data
.assign(raw_data
.begin(), raw_data
.end());
91 GetAppHandler(app_id
)->OnMessage(app_id
, message
);
94 void GCMDriverAndroid::OnMessagesDeleted(JNIEnv
* env
,
97 std::string app_id
= ConvertJavaStringToUTF8(env
, j_app_id
);
99 GetAppHandler(app_id
)->OnMessagesDeleted(app_id
);
103 bool GCMDriverAndroid::RegisterBindings(JNIEnv
* env
) {
104 return RegisterNativesImpl(env
);
107 void GCMDriverAndroid::OnSignedIn() {
110 void GCMDriverAndroid::OnSignedOut() {
113 void GCMDriverAndroid::AddConnectionObserver(GCMConnectionObserver
* observer
) {
116 void GCMDriverAndroid::RemoveConnectionObserver(
117 GCMConnectionObserver
* observer
) {
120 void GCMDriverAndroid::Enable() {
123 void GCMDriverAndroid::Disable() {
126 GCMClient
* GCMDriverAndroid::GetGCMClientForTesting() const {
131 bool GCMDriverAndroid::IsStarted() const {
135 bool GCMDriverAndroid::IsConnected() const {
136 // TODO(gcm): hook up to GCM connected status
140 void GCMDriverAndroid::GetGCMStatistics(
141 const GetGCMStatisticsCallback
& callback
,
146 void GCMDriverAndroid::SetGCMRecording(const GetGCMStatisticsCallback
& callback
,
151 void GCMDriverAndroid::SetAccountTokens(
152 const std::vector
<GCMClient::AccountTokenInfo
>& account_tokens
) {
156 void GCMDriverAndroid::UpdateAccountMapping(
157 const AccountMapping
& account_mapping
) {
161 void GCMDriverAndroid::RemoveAccountMapping(const std::string
& account_id
) {
165 base::Time
GCMDriverAndroid::GetLastTokenFetchTime() {
170 void GCMDriverAndroid::SetLastTokenFetchTime(const base::Time
& time
) {
174 void GCMDriverAndroid::WakeFromSuspendForHeartbeat(bool wake
) {
177 InstanceIDHandler
* GCMDriverAndroid::GetInstanceIDHandler() {
178 // Not supported for Android.
182 void GCMDriverAndroid::AddHeartbeatInterval(const std::string
& scope
,
186 void GCMDriverAndroid::RemoveHeartbeatInterval(const std::string
& scope
) {
189 GCMClient::Result
GCMDriverAndroid::EnsureStarted(
190 GCMClient::StartMode start_mode
) {
191 // TODO(johnme): Maybe we should check if GMS is available?
192 return GCMClient::SUCCESS
;
195 void GCMDriverAndroid::RegisterImpl(
196 const std::string
& app_id
, const std::vector
<std::string
>& sender_ids
) {
197 DCHECK_EQ(1u, sender_ids
.size());
198 JNIEnv
* env
= AttachCurrentThread();
199 Java_GCMDriver_register(
200 env
, java_ref_
.obj(),
201 ConvertUTF8ToJavaString(env
, app_id
).Release(),
202 ConvertUTF8ToJavaString(env
, sender_ids
[0]).Release());
205 void GCMDriverAndroid::UnregisterImpl(const std::string
& app_id
) {
209 void GCMDriverAndroid::UnregisterWithSenderIdImpl(
210 const std::string
& app_id
, const std::string
& sender_id
) {
211 JNIEnv
* env
= AttachCurrentThread();
212 Java_GCMDriver_unregister(
213 env
, java_ref_
.obj(),
214 ConvertUTF8ToJavaString(env
, app_id
).Release(),
215 ConvertUTF8ToJavaString(env
, sender_id
).Release());
218 void GCMDriverAndroid::SendImpl(const std::string
& app_id
,
219 const std::string
& receiver_id
,
220 const OutgoingMessage
& message
) {