Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 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. | |
| 4 | |
| 5 #include "chrome/browser/payments/android/payment_manifest_web_data_service_andr oid.h" | |
| 6 | |
| 7 #include "base/android/jni_android.h" | |
| 8 #include "base/android/jni_array.h" | |
| 9 #include "base/android/jni_string.h" | |
| 10 #include "base/logging.h" | |
| 11 #include "base/memory/ptr_util.h" | |
| 12 #include "chrome/browser/profiles/profile_manager.h" | |
| 13 #include "chrome/browser/web_data_service_factory.h" | |
| 14 #include "components/keyed_service/core/service_access_type.h" | |
| 15 #include "jni/PaymentManifestWebDataService_jni.h" | |
| 16 | |
| 17 namespace payments { | |
|
please use gerrit instead
2017/04/24 18:22:31
Add an empty line after the namespace start.
gogerald1
2017/04/26 13:46:32
Done.
| |
| 18 // static | |
| 19 bool PaymentManifestWebDataServiceAndroid::Register(JNIEnv* env) { | |
| 20 return RegisterNativesImpl(env); | |
| 21 } | |
| 22 | |
| 23 PaymentManifestWebDataServiceAndroid::PaymentManifestWebDataServiceAndroid( | |
| 24 JNIEnv* env, | |
| 25 jobject obj) | |
| 26 : weak_java_obj_(env, obj) {} | |
| 27 | |
| 28 void PaymentManifestWebDataServiceAndroid::OnWebDataServiceRequestDone( | |
| 29 WebDataServiceBase::Handle h, | |
| 30 std::unique_ptr<WDTypedResult> result) { | |
| 31 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 32 if (weak_java_obj_.get(env).is_null()) | |
| 33 return; | |
| 34 | |
| 35 if (web_data_service_requests_.find(h) == web_data_service_requests_.end()) | |
| 36 return; | |
| 37 | |
| 38 switch (result->GetType()) { | |
| 39 case PAYMENT_WEB_APP_MANIFEST: | |
|
please use gerrit instead
2017/04/24 18:22:32
Question: Where is this defined?
gogerald1
2017/04/26 13:46:32
web_data_results.h
please use gerrit instead
2017/04/26 15:00:43
Include it?
gogerald1
2017/04/26 17:30:30
Done.
| |
| 40 OnWebAppManifestRequestDone(env, h, result.get()); | |
| 41 break; | |
| 42 case PAYMENT_METHOD_MANIFEST: | |
| 43 OnPaymentMethodManifestRequestDone(env, h, result.get()); | |
| 44 break; | |
| 45 default: | |
| 46 NOTREACHED() << "unsupported data type"; | |
| 47 } | |
| 48 } | |
| 49 | |
| 50 void PaymentManifestWebDataServiceAndroid::OnWebAppManifestRequestDone( | |
| 51 JNIEnv* env, | |
| 52 WebDataServiceBase::Handle h, | |
| 53 WDTypedResult* result) { | |
| 54 const std::vector<mojom::WebAppManifestSectionPtr>* manifest = &( | |
| 55 static_cast< | |
| 56 const WDResult<std::vector<mojom::WebAppManifestSectionPtr>>*>(result) | |
| 57 ->GetValue()); | |
|
please use gerrit instead
2017/04/24 18:22:31
Ouch, this is confusing. Please separate this into
gogerald1
2017/04/26 13:46:32
Done.
| |
| 58 | |
| 59 base::android::ScopedJavaLocalRef<jobjectArray> jmanifest = | |
| 60 Java_PaymentManifestWebDataService_createManifest(env, manifest->size()); | |
| 61 | |
| 62 for (size_t i = 0; i < manifest->size(); ++i) { | |
| 63 const mojom::WebAppManifestSectionPtr& section = (*manifest)[i]; | |
|
please use gerrit instead
2017/04/24 18:22:32
manifest->get(i) is less ugly, IMHO.
gogerald1
2017/04/26 13:46:32
Done.
| |
| 64 DCHECK_GE(100U, section->fingerprints.size()); | |
| 65 | |
| 66 Java_PaymentManifestWebDataService_addSectionToManifest( | |
| 67 env, jmanifest.obj(), base::checked_cast<int>(i), | |
|
please use gerrit instead
2017/04/24 18:22:31
#include "base/numerics/safe_conversions.h"
gogerald1
2017/04/26 13:46:32
Done.
| |
| 68 base::android::ConvertUTF8ToJavaString(env, section->id), | |
| 69 section->min_version, | |
| 70 base::checked_cast<int>(section->fingerprints.size())); | |
| 71 | |
| 72 for (size_t j = 0; j < section->fingerprints.size(); ++j) { | |
| 73 const std::vector<uint8_t>& fingerprint = section->fingerprints[j]; | |
| 74 Java_PaymentManifestWebDataService_addFingerprintToSection( | |
| 75 env, jmanifest.obj(), base::checked_cast<int>(i), | |
| 76 base::checked_cast<int>(j), | |
| 77 base::android::ToJavaByteArray(env, fingerprint)); | |
| 78 } | |
| 79 } | |
| 80 | |
| 81 Java_PaymentManifestWebDataServiceCallback_onPaymentWebAppManifestFetched( | |
| 82 env, web_data_service_requests_[h]->obj(), jmanifest.obj()); | |
| 83 web_data_service_requests_.erase(h); | |
| 84 } | |
| 85 | |
| 86 void PaymentManifestWebDataServiceAndroid::OnPaymentMethodManifestRequestDone( | |
| 87 JNIEnv* env, | |
| 88 WebDataServiceBase::Handle h, | |
| 89 WDTypedResult* result) { | |
| 90 const std::vector<std::string>* web_apps_ids = | |
|
please use gerrit instead
2017/04/24 18:22:32
#include <string>
gogerald1
2017/04/26 13:46:32
Done.
| |
| 91 &(static_cast<const WDResult<std::vector<std::string>>*>(result) | |
| 92 ->GetValue()); | |
|
please use gerrit instead
2017/04/24 18:22:32
Confusing. Please separate into several statements
gogerald1
2017/04/26 13:46:32
Done.
| |
| 93 | |
| 94 Java_PaymentManifestWebDataServiceCallback_onPaymentMethodManifestFetched( | |
| 95 env, web_data_service_requests_[h]->obj(), | |
| 96 base::android::ToJavaArrayOfStrings(env, *web_apps_ids)); | |
| 97 web_data_service_requests_.erase(h); | |
| 98 } | |
| 99 | |
| 100 bool PaymentManifestWebDataServiceAndroid::GetPaymentMethodManifest( | |
| 101 JNIEnv* env, | |
| 102 const base::android::JavaParamRef<jobject>& unused_obj, | |
| 103 const base::android::JavaParamRef<jstring>& jmethod_name, | |
| 104 const base::android::JavaParamRef<jobject>& jcallback) { | |
| 105 scoped_refptr<payments::PaymentManifestWebDataService> web_data_service = | |
| 106 WebDataServiceFactory::GetPaymentManifestWebDataForProfile( | |
| 107 ProfileManager::GetActiveUserProfile(), | |
| 108 ServiceAccessType::EXPLICIT_ACCESS); | |
| 109 if (web_data_service == nullptr) | |
| 110 return false; | |
| 111 | |
| 112 WebDataServiceBase::Handle handle = | |
| 113 web_data_service->GetPaymentMethodManifest( | |
| 114 base::android::ConvertJavaStringToUTF8(env, jmethod_name), this); | |
| 115 web_data_service_requests_[handle] = | |
| 116 base::MakeUnique<base::android::ScopedJavaGlobalRef<jobject>>(jcallback); | |
| 117 | |
| 118 return true; | |
| 119 } | |
| 120 | |
| 121 void PaymentManifestWebDataServiceAndroid::AddPaymentMethodManifest( | |
| 122 JNIEnv* env, | |
| 123 const base::android::JavaParamRef<jobject>& unused_obj, | |
| 124 const base::android::JavaParamRef<jstring>& jmethod_name, | |
| 125 const base::android::JavaParamRef<jobjectArray>& jweb_apps_ids) { | |
| 126 std::vector<std::string> web_apps_ids; | |
| 127 base::android::AppendJavaStringArrayToStringVector(env, jweb_apps_ids, | |
| 128 &web_apps_ids); | |
| 129 | |
| 130 scoped_refptr<payments::PaymentManifestWebDataService> web_data_service = | |
| 131 WebDataServiceFactory::GetPaymentManifestWebDataForProfile( | |
| 132 ProfileManager::GetActiveUserProfile(), | |
| 133 ServiceAccessType::EXPLICIT_ACCESS); | |
| 134 if (web_data_service == nullptr) | |
| 135 return; | |
| 136 | |
| 137 web_data_service->AddPaymentMethodManifest( | |
| 138 base::android::ConvertJavaStringToUTF8(jmethod_name), web_apps_ids); | |
| 139 } | |
| 140 | |
| 141 bool PaymentManifestWebDataServiceAndroid::GetPaymentWebAppManifest( | |
| 142 JNIEnv* env, | |
| 143 const base::android::JavaParamRef<jobject>& unused_obj, | |
| 144 const base::android::JavaParamRef<jstring>& jweb_app_id, | |
| 145 const base::android::JavaParamRef<jobject>& jcallback) { | |
| 146 scoped_refptr<payments::PaymentManifestWebDataService> web_data_service = | |
| 147 WebDataServiceFactory::GetPaymentManifestWebDataForProfile( | |
| 148 ProfileManager::GetActiveUserProfile(), | |
| 149 ServiceAccessType::EXPLICIT_ACCESS); | |
| 150 if (web_data_service == nullptr) | |
| 151 return false; | |
| 152 | |
| 153 WebDataServiceBase::Handle handle = | |
| 154 web_data_service->GetPaymentWebAppManifest( | |
| 155 base::android::ConvertJavaStringToUTF8(env, jweb_app_id), this); | |
| 156 web_data_service_requests_[handle] = | |
| 157 base::MakeUnique<base::android::ScopedJavaGlobalRef<jobject>>(jcallback); | |
| 158 | |
| 159 return true; | |
| 160 } | |
| 161 | |
| 162 void PaymentManifestWebDataServiceAndroid::AddPaymentWebAppManifest( | |
| 163 const std::vector<mojom::WebAppManifestSectionPtr>& manifest) { | |
| 164 scoped_refptr<payments::PaymentManifestWebDataService> web_data_service = | |
| 165 WebDataServiceFactory::GetPaymentManifestWebDataForProfile( | |
| 166 ProfileManager::GetActiveUserProfile(), | |
| 167 ServiceAccessType::EXPLICIT_ACCESS); | |
| 168 if (web_data_service != nullptr) | |
| 169 web_data_service->AddPaymentWebAppManifest(manifest); | |
| 170 } | |
| 171 | |
| 172 static jlong Init(JNIEnv* env, | |
| 173 const base::android::JavaParamRef<jobject>& obj) { | |
| 174 PaymentManifestWebDataServiceAndroid* manifest_web_data_service_android = | |
| 175 new PaymentManifestWebDataServiceAndroid(env, obj); | |
| 176 return reinterpret_cast<intptr_t>(manifest_web_data_service_android); | |
| 177 } | |
| 178 | |
| 179 } // namespace payments | |
| OLD | NEW |