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"
7 #include "base/android/jni_android.h"
8 #include "base/android/jni_array.h"
9 #include "base/android/jni_string.h"
10 #include "base/compiler_specific.h"
11 #include "base/logging.h"
12 #include "jni/GCMDriver_jni.h"
14 using base::android::AppendJavaStringArrayToStringVector
;
15 using base::android::AttachCurrentThread
;
16 using base::android::ConvertJavaStringToUTF8
;
17 using base::android::ConvertUTF8ToJavaString
;
18 using base::android::ToJavaArrayOfStrings
;
22 GCMDriverAndroid::GCMDriverAndroid() {
23 JNIEnv
* env
= AttachCurrentThread();
25 Java_GCMDriver_create(env
,
26 reinterpret_cast<intptr_t>(this),
27 base::android::GetApplicationContext()));
30 GCMDriverAndroid::~GCMDriverAndroid() {
31 JNIEnv
* env
= AttachCurrentThread();
32 Java_GCMDriver_destroy(env
, java_ref_
.obj());
35 void GCMDriverAndroid::OnRegisterFinished(JNIEnv
* env
,
38 jstring j_registration_id
,
40 std::string app_id
= ConvertJavaStringToUTF8(env
, j_app_id
);
41 std::string registration_id
= ConvertJavaStringToUTF8(env
, j_registration_id
);
42 GCMClient::Result result
= success
? GCMClient::SUCCESS
43 : GCMClient::UNKNOWN_ERROR
;
45 RegisterFinished(app_id
, registration_id
, result
);
48 void GCMDriverAndroid::OnUnregisterFinished(JNIEnv
* env
,
52 std::string app_id
= ConvertJavaStringToUTF8(env
, j_app_id
);
53 GCMClient::Result result
= success
? GCMClient::SUCCESS
54 : GCMClient::UNKNOWN_ERROR
;
56 UnregisterFinished(app_id
, result
);
59 void GCMDriverAndroid::OnMessageReceived(JNIEnv
* env
,
63 jstring j_collapse_key
,
64 jobjectArray j_data_keys_and_values
) {
65 std::string app_id
= ConvertJavaStringToUTF8(env
, j_app_id
);
67 GCMClient::IncomingMessage message
;
68 message
.sender_id
= ConvertJavaStringToUTF8(env
, j_sender_id
);
69 message
.collapse_key
= ConvertJavaStringToUTF8(env
, j_collapse_key
);
70 // Expand j_data_keys_and_values from array to map.
71 std::vector
<std::string
> data_keys_and_values
;
72 AppendJavaStringArrayToStringVector(env
,
73 j_data_keys_and_values
,
74 &data_keys_and_values
);
75 for (size_t i
= 0; i
+ 1 < data_keys_and_values
.size(); i
+= 2) {
76 message
.data
[data_keys_and_values
[i
]] = data_keys_and_values
[i
+1];
79 GetAppHandler(app_id
)->OnMessage(app_id
, message
);
82 void GCMDriverAndroid::OnMessagesDeleted(JNIEnv
* env
,
85 std::string app_id
= ConvertJavaStringToUTF8(env
, j_app_id
);
87 GetAppHandler(app_id
)->OnMessagesDeleted(app_id
);
91 bool GCMDriverAndroid::RegisterBindings(JNIEnv
* env
) {
92 return RegisterNativesImpl(env
);
95 void GCMDriverAndroid::OnSignedIn() {
98 void GCMDriverAndroid::OnSignedOut() {
101 void GCMDriverAndroid::AddConnectionObserver(GCMConnectionObserver
* observer
) {
104 void GCMDriverAndroid::RemoveConnectionObserver(
105 GCMConnectionObserver
* observer
) {
108 void GCMDriverAndroid::Enable() {
111 void GCMDriverAndroid::Disable() {
114 GCMClient
* GCMDriverAndroid::GetGCMClientForTesting() const {
119 bool GCMDriverAndroid::IsStarted() const {
123 bool GCMDriverAndroid::IsConnected() const {
124 // TODO(gcm): hook up to GCM connected status
128 void GCMDriverAndroid::GetGCMStatistics(
129 const GetGCMStatisticsCallback
& callback
,
134 void GCMDriverAndroid::SetGCMRecording(const GetGCMStatisticsCallback
& callback
,
139 void GCMDriverAndroid::SetAccountTokens(
140 const std::vector
<GCMClient::AccountTokenInfo
>& account_tokens
) {
144 void GCMDriverAndroid::UpdateAccountMapping(
145 const AccountMapping
& account_mapping
) {
149 void GCMDriverAndroid::RemoveAccountMapping(const std::string
& account_id
) {
153 base::Time
GCMDriverAndroid::GetLastTokenFetchTime() {
158 void GCMDriverAndroid::SetLastTokenFetchTime(const base::Time
& time
) {
162 void GCMDriverAndroid::WakeFromSuspendForHeartbeat(bool wake
) {
165 GCMClient::Result
GCMDriverAndroid::EnsureStarted(
166 GCMClient::StartMode start_mode
) {
167 // TODO(johnme): Maybe we should check if GMS is available?
168 return GCMClient::SUCCESS
;
171 void GCMDriverAndroid::RegisterImpl(
172 const std::string
& app_id
, const std::vector
<std::string
>& sender_ids
) {
173 DCHECK_EQ(1u, sender_ids
.size());
174 JNIEnv
* env
= AttachCurrentThread();
175 Java_GCMDriver_register(
176 env
, java_ref_
.obj(),
177 ConvertUTF8ToJavaString(env
, app_id
).Release(),
178 ConvertUTF8ToJavaString(env
, sender_ids
[0]).Release());
181 void GCMDriverAndroid::UnregisterImpl(const std::string
& app_id
) {
185 void GCMDriverAndroid::UnregisterWithSenderIdImpl(
186 const std::string
& app_id
, const std::string
& sender_id
) {
187 JNIEnv
* env
= AttachCurrentThread();
188 Java_GCMDriver_unregister(
189 env
, java_ref_
.obj(),
190 ConvertUTF8ToJavaString(env
, app_id
).Release(),
191 ConvertUTF8ToJavaString(env
, sender_id
).Release());
194 void GCMDriverAndroid::SendImpl(const std::string
& app_id
,
195 const std::string
& receiver_id
,
196 const GCMClient::OutgoingMessage
& message
) {