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