Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(313)

Side by Side Diff: components/payments/content/android/payment_manifest_parser_android.cc

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

Powered by Google App Engine
This is Rietveld 408576698