Roll ANGLE cc54ab3..c5b2ba5
[chromium-blink-merge.git] / components / gcm_driver / gcm_driver_android.cc
blob84a89982c687c2cffbcab40d701f8439727dbd36
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 <stdint.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;
22 namespace gcm {
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();
29 java_ref_.Reset(
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,
41 jobject obj,
42 jstring j_app_id,
43 jstring j_registration_id,
44 jboolean success) {
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,
54 jobject obj,
55 jstring j_app_id,
56 jboolean success) {
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,
65 jobject obj,
66 jstring j_app_id,
67 jstring j_sender_id,
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.
85 if (j_raw_data) {
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,
95 jobject obj,
96 jstring j_app_id) {
97 std::string app_id = ConvertJavaStringToUTF8(env, j_app_id);
99 GetAppHandler(app_id)->OnMessagesDeleted(app_id);
102 // static
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 {
127 NOTIMPLEMENTED();
128 return NULL;
131 bool GCMDriverAndroid::IsStarted() const {
132 return true;
135 bool GCMDriverAndroid::IsConnected() const {
136 // TODO(gcm): hook up to GCM connected status
137 return true;
140 void GCMDriverAndroid::GetGCMStatistics(
141 const GetGCMStatisticsCallback& callback,
142 bool clear_logs) {
143 NOTIMPLEMENTED();
146 void GCMDriverAndroid::SetGCMRecording(const GetGCMStatisticsCallback& callback,
147 bool recording) {
148 NOTIMPLEMENTED();
151 void GCMDriverAndroid::SetAccountTokens(
152 const std::vector<GCMClient::AccountTokenInfo>& account_tokens) {
153 NOTIMPLEMENTED();
156 void GCMDriverAndroid::UpdateAccountMapping(
157 const AccountMapping& account_mapping) {
158 NOTIMPLEMENTED();
161 void GCMDriverAndroid::RemoveAccountMapping(const std::string& account_id) {
162 NOTIMPLEMENTED();
165 base::Time GCMDriverAndroid::GetLastTokenFetchTime() {
166 NOTIMPLEMENTED();
167 return base::Time();
170 void GCMDriverAndroid::SetLastTokenFetchTime(const base::Time& time) {
171 NOTIMPLEMENTED();
174 void GCMDriverAndroid::WakeFromSuspendForHeartbeat(bool wake) {
177 InstanceIDHandler* GCMDriverAndroid::GetInstanceIDHandler() {
178 // Not supported for Android.
179 return NULL;
182 void GCMDriverAndroid::AddHeartbeatInterval(const std::string& scope,
183 int interval_ms) {
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(env, java_ref_.obj(),
200 ConvertUTF8ToJavaString(env, app_id).obj(),
201 ConvertUTF8ToJavaString(env, sender_ids[0]).obj());
204 void GCMDriverAndroid::UnregisterImpl(const std::string& app_id) {
205 NOTREACHED();
208 void GCMDriverAndroid::UnregisterWithSenderIdImpl(
209 const std::string& app_id, const std::string& sender_id) {
210 JNIEnv* env = AttachCurrentThread();
211 Java_GCMDriver_unregister(env, java_ref_.obj(),
212 ConvertUTF8ToJavaString(env, app_id).obj(),
213 ConvertUTF8ToJavaString(env, sender_id).obj());
216 void GCMDriverAndroid::SendImpl(const std::string& app_id,
217 const std::string& receiver_id,
218 const OutgoingMessage& message) {
219 NOTIMPLEMENTED();
222 } // namespace gcm