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::Enable() {
104 void GCMDriverAndroid::Disable() {
107 GCMClient
* GCMDriverAndroid::GetGCMClientForTesting() const {
112 bool GCMDriverAndroid::IsStarted() const {
116 bool GCMDriverAndroid::IsConnected() const {
117 // TODO(gcm): hook up to GCM connected status
121 void GCMDriverAndroid::GetGCMStatistics(
122 const GetGCMStatisticsCallback
& callback
,
127 void GCMDriverAndroid::SetGCMRecording(const GetGCMStatisticsCallback
& callback
,
132 GCMClient::Result
GCMDriverAndroid::EnsureStarted() {
133 // TODO(johnme): Maybe we should check if GMS is available?
134 return GCMClient::SUCCESS
;
137 void GCMDriverAndroid::RegisterImpl(
138 const std::string
& app_id
,
139 const std::vector
<std::string
>& sender_ids
) {
140 JNIEnv
* env
= AttachCurrentThread();
141 Java_GCMDriver_register(
142 env
, java_ref_
.obj(),
143 ConvertUTF8ToJavaString(env
, app_id
).Release(),
144 ToJavaArrayOfStrings(env
, sender_ids
).obj());
147 void GCMDriverAndroid::UnregisterImpl(const std::string
& app_id
) {
148 JNIEnv
* env
= AttachCurrentThread();
149 Java_GCMDriver_unregister(
150 env
, java_ref_
.obj(),
151 ConvertUTF8ToJavaString(env
, app_id
).Release());
154 void GCMDriverAndroid::SendImpl(const std::string
& app_id
,
155 const std::string
& receiver_id
,
156 const GCMClient::OutgoingMessage
& message
) {