| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 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/payments/content/android/payment_manifest_parser_android.h" | 5 #include "components/payments/content/android/payment_manifest_parser_android.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <climits> | 9 #include <climits> |
| 10 | 10 |
| 11 #include "base/android/jni_array.h" | 11 #include "base/android/jni_array.h" |
| 12 #include "base/android/jni_string.h" | 12 #include "base/android/jni_string.h" |
| 13 #include "base/bind.h" | 13 #include "base/bind.h" |
| 14 #include "base/bind_helpers.h" | |
| 15 #include "base/logging.h" | |
| 16 #include "base/memory/ptr_util.h" | 14 #include "base/memory/ptr_util.h" |
| 17 #include "base/numerics/safe_conversions.h" | 15 #include "base/numerics/safe_conversions.h" |
| 18 #include "components/strings/grit/components_strings.h" | |
| 19 #include "content/public/browser/utility_process_mojo_client.h" | |
| 20 #include "jni/PaymentManifestParser_jni.h" | 16 #include "jni/PaymentManifestParser_jni.h" |
| 21 #include "ui/base/l10n/l10n_util.h" | |
| 22 | 17 |
| 23 namespace payments { | 18 namespace payments { |
| 19 namespace { |
| 24 | 20 |
| 25 class PaymentManifestParserAndroid::ParseCallback { | 21 class ParseCallback { |
| 26 public: | 22 public: |
| 27 explicit ParseCallback(const base::android::JavaParamRef<jobject>& jcallback) | 23 explicit ParseCallback(const base::android::JavaParamRef<jobject>& jcallback) |
| 28 : jcallback_(jcallback) {} | 24 : jcallback_(jcallback) {} |
| 29 | 25 |
| 30 ~ParseCallback() {} | 26 ~ParseCallback() {} |
| 31 | 27 |
| 32 void OnManifestParseSuccess( | 28 void OnManifestParsed( |
| 33 std::vector<mojom::PaymentManifestSectionPtr> manifest) { | 29 std::vector<mojom::PaymentManifestSectionPtr> manifest) { |
| 34 DCHECK(!manifest.empty()); | 30 JNIEnv* env = base::android::AttachCurrentThread(); |
| 35 | 31 |
| 36 JNIEnv* env = base::android::AttachCurrentThread(); | 32 if (manifest.empty()) { |
| 33 // Can trigger synchronous deletion of PaymentManifestParserAndroid. |
| 34 Java_ManifestParseCallback_onManifestParseFailure(env, jcallback_); |
| 35 return; |
| 36 } |
| 37 |
| 37 base::android::ScopedJavaLocalRef<jobjectArray> jmanifest = | 38 base::android::ScopedJavaLocalRef<jobjectArray> jmanifest = |
| 38 Java_PaymentManifestParser_createManifest(env, manifest.size()); | 39 Java_PaymentManifestParser_createManifest(env, manifest.size()); |
| 39 | 40 |
| 40 // Java array indices must be integers. | 41 // Java array indices must be integers. |
| 41 for (size_t i = 0; i < manifest.size() && i <= static_cast<size_t>(INT_MAX); | 42 for (size_t i = 0; i < manifest.size() && i <= static_cast<size_t>(INT_MAX); |
| 42 ++i) { | 43 ++i) { |
| 43 const mojom::PaymentManifestSectionPtr& section = manifest[i]; | 44 const mojom::PaymentManifestSectionPtr& section = manifest[i]; |
| 44 if (section->sha256_cert_fingerprints.size() > | 45 if (section->sha256_cert_fingerprints.size() > |
| 45 static_cast<size_t>(INT_MAX)) { | 46 static_cast<size_t>(INT_MAX)) { |
| 46 continue; | 47 continue; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 62 base::checked_cast<int>(j), | 63 base::checked_cast<int>(j), |
| 63 base::android::ToJavaByteArray(env, fingerprint)); | 64 base::android::ToJavaByteArray(env, fingerprint)); |
| 64 } | 65 } |
| 65 } | 66 } |
| 66 | 67 |
| 67 // Can trigger synchronous deletion of PaymentManifestParserAndroid. | 68 // Can trigger synchronous deletion of PaymentManifestParserAndroid. |
| 68 Java_ManifestParseCallback_onManifestParseSuccess(env, jcallback_, | 69 Java_ManifestParseCallback_onManifestParseSuccess(env, jcallback_, |
| 69 jmanifest.obj()); | 70 jmanifest.obj()); |
| 70 } | 71 } |
| 71 | 72 |
| 72 void OnManifestParseFailure() { | |
| 73 // Can trigger synchronous deletion of PaymentManifestParserAndroid. | |
| 74 Java_ManifestParseCallback_onManifestParseFailure( | |
| 75 base::android::AttachCurrentThread(), jcallback_); | |
| 76 } | |
| 77 | |
| 78 private: | 73 private: |
| 79 base::android::ScopedJavaGlobalRef<jobject> jcallback_; | 74 base::android::ScopedJavaGlobalRef<jobject> jcallback_; |
| 80 | 75 |
| 81 DISALLOW_COPY_AND_ASSIGN(ParseCallback); | 76 DISALLOW_COPY_AND_ASSIGN(ParseCallback); |
| 82 }; | 77 }; |
| 83 | 78 |
| 79 } // namespace |
| 80 |
| 84 PaymentManifestParserAndroid::PaymentManifestParserAndroid() {} | 81 PaymentManifestParserAndroid::PaymentManifestParserAndroid() {} |
| 85 | 82 |
| 86 PaymentManifestParserAndroid::~PaymentManifestParserAndroid() {} | 83 PaymentManifestParserAndroid::~PaymentManifestParserAndroid() {} |
| 87 | 84 |
| 88 void PaymentManifestParserAndroid::StartUtilityProcess( | 85 void PaymentManifestParserAndroid::StartUtilityProcess( |
| 89 JNIEnv* env, | 86 JNIEnv* env, |
| 90 const base::android::JavaParamRef<jobject>& jcaller) { | 87 const base::android::JavaParamRef<jobject>& jcaller) { |
| 91 mojo_client_ = base::MakeUnique< | 88 host_.StartUtilityProcess(); |
| 92 content::UtilityProcessMojoClient<mojom::PaymentManifestParser>>( | |
| 93 l10n_util::GetStringUTF16(IDS_UTILITY_PROCESS_MANIFEST_PARSER_NAME)); | |
| 94 mojo_client_->set_error_callback( | |
| 95 base::Bind(&PaymentManifestParserAndroid::OnUtilityProcessStopped, | |
| 96 base::Unretained(this))); | |
| 97 mojo_client_->Start(); | |
| 98 } | 89 } |
| 99 | 90 |
| 100 void PaymentManifestParserAndroid::ParsePaymentManifest( | 91 void PaymentManifestParserAndroid::ParsePaymentManifest( |
| 101 JNIEnv* env, | 92 JNIEnv* env, |
| 102 const base::android::JavaParamRef<jobject>& jcaller, | 93 const base::android::JavaParamRef<jobject>& jcaller, |
| 103 const base::android::JavaParamRef<jstring>& jcontent, | 94 const base::android::JavaParamRef<jstring>& jcontent, |
| 104 const base::android::JavaParamRef<jobject>& jcallback) { | 95 const base::android::JavaParamRef<jobject>& jcallback) { |
| 105 std::unique_ptr<ParseCallback> pending_callback = | 96 host_.ParsePaymentManifest( |
| 106 base::MakeUnique<ParseCallback>(jcallback); | |
| 107 | |
| 108 if (!mojo_client_) { | |
| 109 pending_callback->OnManifestParseFailure(); | |
| 110 return; | |
| 111 } | |
| 112 | |
| 113 ParseCallback* callback_identifier = pending_callback.get(); | |
| 114 pending_callbacks_.push_back(std::move(pending_callback)); | |
| 115 DCHECK_GE(10U, pending_callbacks_.size()); | |
| 116 | |
| 117 mojo_client_->service()->Parse( | |
| 118 base::android::ConvertJavaStringToUTF8(env, jcontent), | 97 base::android::ConvertJavaStringToUTF8(env, jcontent), |
| 119 base::Bind(&PaymentManifestParserAndroid::OnParse, base::Unretained(this), | 98 base::BindOnce(&ParseCallback::OnManifestParsed, |
| 120 callback_identifier)); | 99 base::MakeUnique<ParseCallback>(jcallback))); |
| 121 } | 100 } |
| 122 | 101 |
| 123 void PaymentManifestParserAndroid::StopUtilityProcess( | 102 void PaymentManifestParserAndroid::StopUtilityProcess( |
| 124 JNIEnv* env, | 103 JNIEnv* env, |
| 125 const base::android::JavaParamRef<jobject>& jcaller) { | 104 const base::android::JavaParamRef<jobject>& jcaller) { |
| 126 delete this; | 105 delete this; |
| 127 } | 106 } |
| 128 | 107 |
| 129 void PaymentManifestParserAndroid::OnParse( | |
| 130 ParseCallback* callback_identifier, | |
| 131 std::vector<mojom::PaymentManifestSectionPtr> manifest) { | |
| 132 // At most 10 manifests to parse, so iterating a vector is not too slow. | |
| 133 for (auto it = pending_callbacks_.begin(); it != pending_callbacks_.end(); | |
| 134 ++it) { | |
| 135 if (it->get() == callback_identifier) { | |
| 136 std::unique_ptr<ParseCallback> pending_callback = std::move(*it); | |
| 137 pending_callbacks_.erase(it); | |
| 138 | |
| 139 // Can trigger synchronous deletion of this object, so can't access any of | |
| 140 // the member variables after this block. | |
| 141 if (manifest.empty()) | |
| 142 pending_callback->OnManifestParseFailure(); | |
| 143 else | |
| 144 pending_callback->OnManifestParseSuccess(std::move(manifest)); | |
| 145 return; | |
| 146 } | |
| 147 } | |
| 148 | |
| 149 // If unable to find the pending callback, then something went wrong in the | |
| 150 // utility process. Stop the utility process and notify all callbacks. | |
| 151 OnUtilityProcessStopped(); | |
| 152 } | |
| 153 | |
| 154 void PaymentManifestParserAndroid::OnUtilityProcessStopped() { | |
| 155 mojo_client_.reset(); | |
| 156 std::vector<std::unique_ptr<ParseCallback>> callbacks = | |
| 157 std::move(pending_callbacks_); | |
| 158 for (const auto& callback : callbacks) { | |
| 159 // Can trigger synchronous deletion of this object, so can't access any of | |
| 160 // the member variables after this line. | |
| 161 callback->OnManifestParseFailure(); | |
| 162 } | |
| 163 } | |
| 164 | |
| 165 bool RegisterPaymentManifestParser(JNIEnv* env) { | 108 bool RegisterPaymentManifestParser(JNIEnv* env) { |
| 166 return RegisterNativesImpl(env); | 109 return RegisterNativesImpl(env); |
| 167 } | 110 } |
| 168 | 111 |
| 169 // Caller owns the result. | 112 // Caller owns the result. |
| 170 jlong CreatePaymentManifestParserAndroid( | 113 jlong CreatePaymentManifestParserAndroid( |
| 171 JNIEnv* env, | 114 JNIEnv* env, |
| 172 const base::android::JavaParamRef<jclass>& jcaller) { | 115 const base::android::JavaParamRef<jclass>& jcaller) { |
| 173 return reinterpret_cast<jlong>(new PaymentManifestParserAndroid); | 116 return reinterpret_cast<jlong>(new PaymentManifestParserAndroid); |
| 174 } | 117 } |
| 175 | 118 |
| 176 } // namespace payments | 119 } // namespace payments |
| OLD | NEW |