Roll src/third_party/WebKit f36d5e0:68b67cd (svn 193299:193303)
[chromium-blink-merge.git] / components / gcm_driver / gcm_driver_android.cc
blob2f82fbec91c434a9fddaf104e4f460ae921a4c30
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;
20 namespace gcm {
22 GCMDriverAndroid::GCMDriverAndroid() {
23 JNIEnv* env = AttachCurrentThread();
24 java_ref_.Reset(
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,
36 jobject obj,
37 jstring j_app_id,
38 jstring j_registration_id,
39 jboolean success) {
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,
49 jobject obj,
50 jstring j_app_id,
51 jboolean success) {
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,
60 jobject obj,
61 jstring j_app_id,
62 jstring j_sender_id,
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,
83 jobject obj,
84 jstring j_app_id) {
85 std::string app_id = ConvertJavaStringToUTF8(env, j_app_id);
87 GetAppHandler(app_id)->OnMessagesDeleted(app_id);
90 // static
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 {
115 NOTIMPLEMENTED();
116 return NULL;
119 bool GCMDriverAndroid::IsStarted() const {
120 return true;
123 bool GCMDriverAndroid::IsConnected() const {
124 // TODO(gcm): hook up to GCM connected status
125 return true;
128 void GCMDriverAndroid::GetGCMStatistics(
129 const GetGCMStatisticsCallback& callback,
130 bool clear_logs) {
131 NOTIMPLEMENTED();
134 void GCMDriverAndroid::SetGCMRecording(const GetGCMStatisticsCallback& callback,
135 bool recording) {
136 NOTIMPLEMENTED();
139 void GCMDriverAndroid::SetAccountTokens(
140 const std::vector<GCMClient::AccountTokenInfo>& account_tokens) {
141 NOTIMPLEMENTED();
144 void GCMDriverAndroid::UpdateAccountMapping(
145 const AccountMapping& account_mapping) {
146 NOTIMPLEMENTED();
149 void GCMDriverAndroid::RemoveAccountMapping(const std::string& account_id) {
150 NOTIMPLEMENTED();
153 base::Time GCMDriverAndroid::GetLastTokenFetchTime() {
154 NOTIMPLEMENTED();
155 return base::Time();
158 void GCMDriverAndroid::SetLastTokenFetchTime(const base::Time& time) {
159 NOTIMPLEMENTED();
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) {
182 NOTREACHED();
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) {
197 NOTIMPLEMENTED();
200 } // namespace gcm