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::Purge() {
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::UpdateAccountMapping(
140 const AccountMapping
& account_mapping
) {
143 void GCMDriverAndroid::RemoveAccountMapping(const std::string
& account_id
) {
146 GCMClient::Result
GCMDriverAndroid::EnsureStarted() {
147 // TODO(johnme): Maybe we should check if GMS is available?
148 return GCMClient::SUCCESS
;
151 void GCMDriverAndroid::RegisterImpl(
152 const std::string
& app_id
,
153 const std::vector
<std::string
>& sender_ids
) {
154 JNIEnv
* env
= AttachCurrentThread();
155 Java_GCMDriver_register(
156 env
, java_ref_
.obj(),
157 ConvertUTF8ToJavaString(env
, app_id
).Release(),
158 ToJavaArrayOfStrings(env
, sender_ids
).obj());
161 void GCMDriverAndroid::UnregisterImpl(const std::string
& app_id
) {
162 JNIEnv
* env
= AttachCurrentThread();
163 Java_GCMDriver_unregister(
164 env
, java_ref_
.obj(),
165 ConvertUTF8ToJavaString(env
, app_id
).Release());
168 void GCMDriverAndroid::SendImpl(const std::string
& app_id
,
169 const std::string
& receiver_id
,
170 const GCMClient::OutgoingMessage
& message
) {