Call InstanceID::DeleteID to delete InstanceID data when the app or
[chromium-blink-merge.git] / components / gcm_driver / gcm_driver_android.cc
blob6f8dd596b4489316b8ede47878aac226d7d4c1ed
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 InstanceIDHandler* GCMDriverAndroid::GetInstanceIDHandler() {
166 // Not supported for Android.
167 return NULL;
170 void GCMDriverAndroid::AddHeartbeatInterval(const std::string& scope,
171 int interval_ms) {
174 void GCMDriverAndroid::RemoveHeartbeatInterval(const std::string& scope) {
177 GCMClient::Result GCMDriverAndroid::EnsureStarted(
178 GCMClient::StartMode start_mode) {
179 // TODO(johnme): Maybe we should check if GMS is available?
180 return GCMClient::SUCCESS;
183 void GCMDriverAndroid::RegisterImpl(
184 const std::string& app_id, const std::vector<std::string>& sender_ids) {
185 DCHECK_EQ(1u, sender_ids.size());
186 JNIEnv* env = AttachCurrentThread();
187 Java_GCMDriver_register(
188 env, java_ref_.obj(),
189 ConvertUTF8ToJavaString(env, app_id).Release(),
190 ConvertUTF8ToJavaString(env, sender_ids[0]).Release());
193 void GCMDriverAndroid::UnregisterImpl(const std::string& app_id) {
194 NOTREACHED();
197 void GCMDriverAndroid::UnregisterWithSenderIdImpl(
198 const std::string& app_id, const std::string& sender_id) {
199 JNIEnv* env = AttachCurrentThread();
200 Java_GCMDriver_unregister(
201 env, java_ref_.obj(),
202 ConvertUTF8ToJavaString(env, app_id).Release(),
203 ConvertUTF8ToJavaString(env, sender_id).Release());
206 void GCMDriverAndroid::SendImpl(const std::string& app_id,
207 const std::string& receiver_id,
208 const GCMClient::OutgoingMessage& message) {
209 NOTIMPLEMENTED();
212 } // namespace gcm