Chromium Code Reviews| 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" | |
| 7 #include "base/logging.h" | 13 #include "base/logging.h" |
| 8 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/time/time.h" | |
| 16 #include "jni/InstanceIDWithSubtype_jni.h" | |
| 17 | |
| 18 using base::android::AttachCurrentThread; | |
| 19 using base::android::ConvertJavaStringToUTF8; | |
| 20 using base::android::ConvertUTF8ToJavaString; | |
| 9 | 21 |
| 10 namespace instance_id { | 22 namespace instance_id { |
| 11 | 23 |
| 12 // static | 24 // static |
| 13 scoped_ptr<InstanceID> InstanceID::Create(const std::string& app_id, | 25 bool InstanceIDAndroid::RegisterBindings(JNIEnv* env) { |
| 14 gcm::InstanceIDHandler* handler) { | 26 return RegisterNativesImpl(env); |
| 15 return make_scoped_ptr(new InstanceIDAndroid(app_id, handler)); | |
| 16 } | 27 } |
| 17 | 28 |
| 18 InstanceIDAndroid::InstanceIDAndroid(const std::string& app_id, | 29 // static |
| 19 gcm::InstanceIDHandler* handler) | 30 scoped_ptr<InstanceID> InstanceID::Create(const std::string& app_id, |
| 20 : InstanceID(app_id, handler) {} | 31 gcm::InstanceIDHandler* unused) { |
| 32 return make_scoped_ptr(new InstanceIDAndroid(app_id)); | |
| 33 } | |
| 34 | |
| 35 InstanceIDAndroid::InstanceIDAndroid(const std::string& app_id) | |
| 36 : InstanceID(app_id) { | |
| 37 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 38 | |
| 39 DCHECK(!app_id.empty()) << "Empty app_id is not supported"; | |
| 40 // TODO(johnme): Passing app_id as subtype isn't always correct. | |
|
Peter Beverloo
2016/04/11 14:05:31
nit: Either remove or clarify the TODO, this is ve
johnme
2016/04/13 11:42:12
Done.
| |
| 41 std::string subtype = app_id; | |
| 42 | |
| 43 JNIEnv* env = AttachCurrentThread(); | |
| 44 java_ref_.Reset(Java_InstanceIDWithSubtype_getInstance( | |
| 45 env, reinterpret_cast<intptr_t>(this), | |
| 46 base::android::GetApplicationContext(), | |
| 47 ConvertUTF8ToJavaString(env, subtype).obj())); | |
| 48 } | |
| 21 | 49 |
| 22 InstanceIDAndroid::~InstanceIDAndroid() { | 50 InstanceIDAndroid::~InstanceIDAndroid() { |
| 23 } | 51 } |
| 24 | 52 |
| 25 void InstanceIDAndroid::GetID(const GetIDCallback& callback) { | 53 void InstanceIDAndroid::GetID(const GetIDCallback& callback) { |
| 26 NOTIMPLEMENTED(); | 54 DCHECK(thread_checker_.CalledOnValidThread()); |
| 55 | |
| 56 JNIEnv* env = AttachCurrentThread(); | |
| 57 std::string id = ConvertJavaStringToUTF8( | |
| 58 Java_InstanceIDWithSubtype_getId(env, java_ref_.obj())); | |
| 59 callback.Run(id); | |
|
Peter Beverloo
2016/04/11 14:05:31
nit: for this one and the callbacks on lines :70 a
johnme
2016/04/13 11:42:12
Done.
| |
| 27 } | 60 } |
| 28 | 61 |
| 29 void InstanceIDAndroid::GetCreationTime( | 62 void InstanceIDAndroid::GetCreationTime( |
| 30 const GetCreationTimeCallback& callback) { | 63 const GetCreationTimeCallback& callback) { |
| 31 NOTIMPLEMENTED(); | 64 DCHECK(thread_checker_.CalledOnValidThread()); |
| 65 | |
| 66 JNIEnv* env = AttachCurrentThread(); | |
| 67 int64_t creation_time_unix_ms = | |
| 68 Java_InstanceIDWithSubtype_getCreationTime(env, java_ref_.obj()); | |
| 69 if (!creation_time_unix_ms) { | |
|
Peter Beverloo
2016/04/11 14:05:31
Please document when this would happen.
johnme
2016/04/13 11:42:12
Added comment.
| |
| 70 callback.Run(base::Time()); | |
| 71 } else { | |
| 72 callback.Run(base::Time::UnixEpoch() + | |
| 73 base::TimeDelta::FromMilliseconds(creation_time_unix_ms)); | |
| 74 } | |
| 32 } | 75 } |
| 33 | 76 |
| 34 void InstanceIDAndroid::GetToken( | 77 void InstanceIDAndroid::GetToken( |
| 35 const std::string& audience, | 78 const std::string& authorized_entity, |
| 36 const std::string& scope, | 79 const std::string& scope, |
| 37 const std::map<std::string, std::string>& options, | 80 const std::map<std::string, std::string>& options, |
| 38 const GetTokenCallback& callback) { | 81 const GetTokenCallback& callback) { |
| 39 NOTIMPLEMENTED(); | 82 DCHECK(thread_checker_.CalledOnValidThread()); |
| 83 | |
| 84 int32_t request_id = get_token_callbacks_.Add(new GetTokenCallback(callback)); | |
| 85 | |
| 86 std::vector<std::string> options_strings; | |
| 87 for (const auto& entry : options) { | |
| 88 options_strings.push_back(entry.first); | |
| 89 options_strings.push_back(entry.second); | |
| 90 } | |
| 91 | |
| 92 JNIEnv* env = AttachCurrentThread(); | |
| 93 Java_InstanceIDWithSubtype_getToken( | |
| 94 env, java_ref_.obj(), request_id, | |
| 95 ConvertUTF8ToJavaString(env, authorized_entity).obj(), | |
| 96 ConvertUTF8ToJavaString(env, scope).obj(), | |
| 97 base::android::ToJavaArrayOfStrings(env, options_strings).obj()); | |
| 40 } | 98 } |
| 41 | 99 |
| 42 void InstanceIDAndroid::DeleteToken(const std::string& audience, | 100 void InstanceIDAndroid::DeleteToken(const std::string& authorized_entity, |
| 43 const std::string& scope, | 101 const std::string& scope, |
| 44 const DeleteTokenCallback& callback) { | 102 const DeleteTokenCallback& callback) { |
| 45 NOTIMPLEMENTED(); | 103 DCHECK(thread_checker_.CalledOnValidThread()); |
| 104 | |
| 105 int32_t request_id = | |
| 106 delete_token_callbacks_.Add(new DeleteTokenCallback(callback)); | |
| 107 | |
| 108 JNIEnv* env = AttachCurrentThread(); | |
| 109 Java_InstanceIDWithSubtype_deleteToken( | |
| 110 env, java_ref_.obj(), request_id, | |
| 111 ConvertUTF8ToJavaString(env, authorized_entity).obj(), | |
| 112 ConvertUTF8ToJavaString(env, scope).obj()); | |
| 46 } | 113 } |
| 47 | 114 |
| 48 void InstanceIDAndroid::DeleteID(const DeleteIDCallback& callback) { | 115 void InstanceIDAndroid::DeleteID(const DeleteIDCallback& callback) { |
| 49 NOTIMPLEMENTED(); | 116 DCHECK(thread_checker_.CalledOnValidThread()); |
| 117 | |
| 118 int32_t request_id = delete_id_callbacks_.Add(new DeleteIDCallback(callback)); | |
| 119 | |
| 120 JNIEnv* env = AttachCurrentThread(); | |
| 121 Java_InstanceIDWithSubtype_deleteInstanceID(env, java_ref_.obj(), request_id); | |
| 122 } | |
| 123 | |
| 124 void InstanceIDAndroid::DidGetToken( | |
| 125 JNIEnv* env, | |
| 126 const base::android::JavaParamRef<jobject>& obj, | |
| 127 jint request_id, | |
| 128 const base::android::JavaParamRef<jstring>& jtoken) { | |
| 129 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 130 | |
| 131 GetTokenCallback* callback = get_token_callbacks_.Lookup(request_id); | |
| 132 DCHECK(callback); | |
| 133 std::string token = ConvertJavaStringToUTF8(jtoken); | |
| 134 callback->Run( | |
| 135 token, token.empty() ? InstanceID::UNKNOWN_ERROR : InstanceID::SUCCESS); | |
|
Peter Beverloo
2016/04/11 14:05:31
What happens when we get a token whilst the device
johnme
2016/04/13 11:42:12
The token is the empty string, and the status is U
| |
| 136 get_token_callbacks_.Remove(request_id); | |
| 137 } | |
| 138 | |
| 139 void InstanceIDAndroid::DidDeleteToken( | |
| 140 JNIEnv* env, | |
| 141 const base::android::JavaParamRef<jobject>& obj, | |
| 142 jint request_id, | |
| 143 jboolean success) { | |
| 144 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 145 | |
| 146 DeleteTokenCallback* callback = delete_token_callbacks_.Lookup(request_id); | |
| 147 DCHECK(callback); | |
| 148 callback->Run(success ? InstanceID::SUCCESS : InstanceID::UNKNOWN_ERROR); | |
| 149 delete_token_callbacks_.Remove(request_id); | |
| 150 } | |
| 151 | |
| 152 void InstanceIDAndroid::DidDeleteID( | |
| 153 JNIEnv* env, | |
| 154 const base::android::JavaParamRef<jobject>& obj, | |
| 155 jint request_id, | |
| 156 jboolean success) { | |
| 157 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 158 | |
| 159 DeleteIDCallback* callback = delete_id_callbacks_.Lookup(request_id); | |
| 160 DCHECK(callback); | |
| 161 callback->Run(success ? InstanceID::SUCCESS : InstanceID::UNKNOWN_ERROR); | |
| 162 delete_id_callbacks_.Remove(request_id); | |
| 50 } | 163 } |
| 51 | 164 |
| 52 } // namespace instance_id | 165 } // namespace instance_id |
| OLD | NEW |