OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/gcm_driver/instance_id/instance_id_android.h" | 5 #include "components/gcm_driver/instance_id/instance_id_android.h" |
6 | 6 |
| 7 #include <stdint.h> |
| 8 |
| 9 #include "base/android/context_utils.h" |
| 10 #include "base/android/jni_android.h" |
| 11 #include "base/android/jni_array.h" |
| 12 #include "base/android/jni_string.h" |
| 13 #include "base/bind.h" |
| 14 #include "base/location.h" |
7 #include "base/logging.h" | 15 #include "base/logging.h" |
8 #include "base/memory/scoped_ptr.h" | 16 #include "base/memory/scoped_ptr.h" |
| 17 #include "base/thread_task_runner_handle.h" |
| 18 #include "base/time/time.h" |
| 19 #include "jni/InstanceIDBridge_jni.h" |
| 20 |
| 21 using base::android::AttachCurrentThread; |
| 22 using base::android::ConvertJavaStringToUTF8; |
| 23 using base::android::ConvertUTF8ToJavaString; |
9 | 24 |
10 namespace instance_id { | 25 namespace instance_id { |
11 | 26 |
12 // static | 27 // static |
13 scoped_ptr<InstanceID> InstanceID::Create(const std::string& app_id, | 28 bool InstanceIDAndroid::RegisterJni(JNIEnv* env) { |
14 gcm::InstanceIDHandler* handler) { | 29 return RegisterNativesImpl(env); |
15 return make_scoped_ptr(new InstanceIDAndroid(app_id, handler)); | |
16 } | 30 } |
17 | 31 |
18 InstanceIDAndroid::InstanceIDAndroid(const std::string& app_id, | 32 // static |
19 gcm::InstanceIDHandler* handler) | 33 scoped_ptr<InstanceID> InstanceID::Create(const std::string& app_id, |
20 : InstanceID(app_id, handler) {} | 34 gcm::InstanceIDHandler* unused) { |
| 35 return make_scoped_ptr(new InstanceIDAndroid(app_id)); |
| 36 } |
| 37 |
| 38 InstanceIDAndroid::InstanceIDAndroid(const std::string& app_id) |
| 39 : InstanceID(app_id) { |
| 40 DCHECK(thread_checker_.CalledOnValidThread()); |
| 41 |
| 42 DCHECK(!app_id.empty()) << "Empty app_id is not supported"; |
| 43 // The |app_id| is stored in GCM's category field by the desktop InstanceID |
| 44 // implementation, but because the category is reserved for the app's package |
| 45 // name on Android the subtype field is used instead. |
| 46 std::string subtype = app_id; |
| 47 |
| 48 JNIEnv* env = AttachCurrentThread(); |
| 49 java_ref_.Reset(Java_InstanceIDBridge_create( |
| 50 env, reinterpret_cast<intptr_t>(this), |
| 51 base::android::GetApplicationContext(), |
| 52 ConvertUTF8ToJavaString(env, subtype).obj())); |
| 53 } |
21 | 54 |
22 InstanceIDAndroid::~InstanceIDAndroid() { | 55 InstanceIDAndroid::~InstanceIDAndroid() { |
| 56 DCHECK(thread_checker_.CalledOnValidThread()); |
| 57 |
| 58 JNIEnv* env = AttachCurrentThread(); |
| 59 Java_InstanceIDBridge_destroy(env, java_ref_.obj()); |
23 } | 60 } |
24 | 61 |
25 void InstanceIDAndroid::GetID(const GetIDCallback& callback) { | 62 void InstanceIDAndroid::GetID(const GetIDCallback& callback) { |
26 NOTIMPLEMENTED(); | 63 DCHECK(thread_checker_.CalledOnValidThread()); |
| 64 |
| 65 JNIEnv* env = AttachCurrentThread(); |
| 66 std::string id = ConvertJavaStringToUTF8( |
| 67 Java_InstanceIDBridge_getId(env, java_ref_.obj())); |
| 68 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 69 base::Bind(callback, id)); |
27 } | 70 } |
28 | 71 |
29 void InstanceIDAndroid::GetCreationTime( | 72 void InstanceIDAndroid::GetCreationTime( |
30 const GetCreationTimeCallback& callback) { | 73 const GetCreationTimeCallback& callback) { |
31 NOTIMPLEMENTED(); | 74 DCHECK(thread_checker_.CalledOnValidThread()); |
| 75 |
| 76 JNIEnv* env = AttachCurrentThread(); |
| 77 int64_t creation_time_unix_ms = |
| 78 Java_InstanceIDBridge_getCreationTime(env, java_ref_.obj()); |
| 79 base::Time creation_time; |
| 80 // If the InstanceID's getId, getToken and deleteToken methods have never been |
| 81 // called, or deleteInstanceID has cleared it since, creation time will be 0. |
| 82 if (creation_time_unix_ms) { |
| 83 creation_time = base::Time::UnixEpoch() + |
| 84 base::TimeDelta::FromMilliseconds(creation_time_unix_ms); |
| 85 } |
| 86 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 87 FROM_HERE, base::Bind(callback, creation_time)); |
32 } | 88 } |
33 | 89 |
34 void InstanceIDAndroid::GetToken( | 90 void InstanceIDAndroid::GetToken( |
35 const std::string& audience, | 91 const std::string& authorized_entity, |
36 const std::string& scope, | 92 const std::string& scope, |
37 const std::map<std::string, std::string>& options, | 93 const std::map<std::string, std::string>& options, |
38 const GetTokenCallback& callback) { | 94 const GetTokenCallback& callback) { |
39 NOTIMPLEMENTED(); | 95 DCHECK(thread_checker_.CalledOnValidThread()); |
| 96 |
| 97 int32_t request_id = get_token_callbacks_.Add(new GetTokenCallback(callback)); |
| 98 |
| 99 std::vector<std::string> options_strings; |
| 100 for (const auto& entry : options) { |
| 101 options_strings.push_back(entry.first); |
| 102 options_strings.push_back(entry.second); |
| 103 } |
| 104 |
| 105 JNIEnv* env = AttachCurrentThread(); |
| 106 Java_InstanceIDBridge_getToken( |
| 107 env, java_ref_.obj(), request_id, |
| 108 ConvertUTF8ToJavaString(env, authorized_entity).obj(), |
| 109 ConvertUTF8ToJavaString(env, scope).obj(), |
| 110 base::android::ToJavaArrayOfStrings(env, options_strings).obj()); |
40 } | 111 } |
41 | 112 |
42 void InstanceIDAndroid::DeleteToken(const std::string& audience, | 113 void InstanceIDAndroid::DeleteToken(const std::string& authorized_entity, |
43 const std::string& scope, | 114 const std::string& scope, |
44 const DeleteTokenCallback& callback) { | 115 const DeleteTokenCallback& callback) { |
45 NOTIMPLEMENTED(); | 116 DCHECK(thread_checker_.CalledOnValidThread()); |
| 117 |
| 118 int32_t request_id = |
| 119 delete_token_callbacks_.Add(new DeleteTokenCallback(callback)); |
| 120 |
| 121 JNIEnv* env = AttachCurrentThread(); |
| 122 Java_InstanceIDBridge_deleteToken( |
| 123 env, java_ref_.obj(), request_id, |
| 124 ConvertUTF8ToJavaString(env, authorized_entity).obj(), |
| 125 ConvertUTF8ToJavaString(env, scope).obj()); |
46 } | 126 } |
47 | 127 |
48 void InstanceIDAndroid::DeleteID(const DeleteIDCallback& callback) { | 128 void InstanceIDAndroid::DeleteID(const DeleteIDCallback& callback) { |
49 NOTIMPLEMENTED(); | 129 DCHECK(thread_checker_.CalledOnValidThread()); |
| 130 |
| 131 int32_t request_id = delete_id_callbacks_.Add(new DeleteIDCallback(callback)); |
| 132 |
| 133 JNIEnv* env = AttachCurrentThread(); |
| 134 Java_InstanceIDBridge_deleteInstanceID(env, java_ref_.obj(), request_id); |
| 135 } |
| 136 |
| 137 void InstanceIDAndroid::DidGetToken( |
| 138 JNIEnv* env, |
| 139 const base::android::JavaParamRef<jobject>& obj, |
| 140 jint request_id, |
| 141 const base::android::JavaParamRef<jstring>& jtoken) { |
| 142 DCHECK(thread_checker_.CalledOnValidThread()); |
| 143 |
| 144 GetTokenCallback* callback = get_token_callbacks_.Lookup(request_id); |
| 145 DCHECK(callback); |
| 146 std::string token = ConvertJavaStringToUTF8(jtoken); |
| 147 callback->Run( |
| 148 token, token.empty() ? InstanceID::UNKNOWN_ERROR : InstanceID::SUCCESS); |
| 149 get_token_callbacks_.Remove(request_id); |
| 150 } |
| 151 |
| 152 void InstanceIDAndroid::DidDeleteToken( |
| 153 JNIEnv* env, |
| 154 const base::android::JavaParamRef<jobject>& obj, |
| 155 jint request_id, |
| 156 jboolean success) { |
| 157 DCHECK(thread_checker_.CalledOnValidThread()); |
| 158 |
| 159 DeleteTokenCallback* callback = delete_token_callbacks_.Lookup(request_id); |
| 160 DCHECK(callback); |
| 161 callback->Run(success ? InstanceID::SUCCESS : InstanceID::UNKNOWN_ERROR); |
| 162 delete_token_callbacks_.Remove(request_id); |
| 163 } |
| 164 |
| 165 void InstanceIDAndroid::DidDeleteID( |
| 166 JNIEnv* env, |
| 167 const base::android::JavaParamRef<jobject>& obj, |
| 168 jint request_id, |
| 169 jboolean success) { |
| 170 DCHECK(thread_checker_.CalledOnValidThread()); |
| 171 |
| 172 DeleteIDCallback* callback = delete_id_callbacks_.Lookup(request_id); |
| 173 DCHECK(callback); |
| 174 callback->Run(success ? InstanceID::SUCCESS : InstanceID::UNKNOWN_ERROR); |
| 175 delete_id_callbacks_.Remove(request_id); |
50 } | 176 } |
51 | 177 |
52 } // namespace instance_id | 178 } // namespace instance_id |
OLD | NEW |