| 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/payment_manifest_parser_host.h" | 5 #include "components/payments/content/payment_manifest_parser_host.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
| 14 #include "components/strings/grit/components_strings.h" | 14 #include "components/strings/grit/components_strings.h" |
| 15 #include "content/public/browser/utility_process_mojo_client.h" | 15 #include "content/public/browser/utility_process_mojo_client.h" |
| 16 #include "ui/base/l10n/l10n_util.h" | 16 #include "ui/base/l10n/l10n_util.h" |
| 17 #include "url/url_constants.h" |
| 17 | 18 |
| 18 namespace payments { | 19 namespace payments { |
| 19 | 20 |
| 20 PaymentManifestParserHost::PaymentManifestParserHost() {} | 21 PaymentManifestParserHost::PaymentManifestParserHost() : callback_counter_(0) {} |
| 21 | 22 |
| 22 PaymentManifestParserHost::~PaymentManifestParserHost() {} | 23 PaymentManifestParserHost::~PaymentManifestParserHost() {} |
| 23 | 24 |
| 24 void PaymentManifestParserHost::StartUtilityProcess() { | 25 void PaymentManifestParserHost::StartUtilityProcess() { |
| 25 mojo_client_ = base::MakeUnique< | 26 mojo_client_ = base::MakeUnique< |
| 26 content::UtilityProcessMojoClient<mojom::PaymentManifestParser>>( | 27 content::UtilityProcessMojoClient<mojom::PaymentManifestParser>>( |
| 27 l10n_util::GetStringUTF16( | 28 l10n_util::GetStringUTF16( |
| 28 IDS_UTILITY_PROCESS_PAYMENT_MANIFEST_PARSER_NAME)); | 29 IDS_UTILITY_PROCESS_PAYMENT_MANIFEST_PARSER_NAME)); |
| 29 mojo_client_->set_error_callback( | 30 mojo_client_->set_error_callback( |
| 30 base::Bind(&PaymentManifestParserHost::OnUtilityProcessStopped, | 31 base::Bind(&PaymentManifestParserHost::OnUtilityProcessStopped, |
| 31 base::Unretained(this))); | 32 base::Unretained(this))); |
| 32 mojo_client_->Start(); | 33 mojo_client_->Start(); |
| 33 } | 34 } |
| 34 | 35 |
| 35 void PaymentManifestParserHost::ParsePaymentManifest(const std::string& content, | 36 void PaymentManifestParserHost::ParsePaymentMethodManifest( |
| 36 Callback callback) { | 37 const std::string& content, |
| 38 PaymentMethodCallback callback) { |
| 37 if (!mojo_client_) { | 39 if (!mojo_client_) { |
| 38 std::move(callback).Run(std::vector<mojom::PaymentManifestSectionPtr>()); | 40 std::move(callback).Run(std::vector<GURL>()); |
| 39 return; | 41 return; |
| 40 } | 42 } |
| 41 | 43 |
| 42 pending_callbacks_.push_back(std::move(callback)); | 44 int64_t callback_identifier = callback_counter_++; |
| 43 DCHECK_GE(10U, pending_callbacks_.size()); | 45 const auto& result = pending_payment_method_callbacks_.insert( |
| 44 Callback* callback_identifier = &pending_callbacks_.back(); | 46 std::make_pair(callback_identifier, std::move(callback))); |
| 47 DCHECK(result.second); |
| 48 DCHECK_GE(10U, pending_payment_method_callbacks_.size()); |
| 45 | 49 |
| 46 mojo_client_->service()->Parse( | 50 mojo_client_->service()->ParsePaymentMethodManifest( |
| 47 content, base::Bind(&PaymentManifestParserHost::OnParse, | 51 content, base::Bind(&PaymentManifestParserHost::OnPaymentMethodParse, |
| 48 base::Unretained(this), callback_identifier)); | 52 base::Unretained(this), callback_identifier)); |
| 49 } | 53 } |
| 50 | 54 |
| 51 void PaymentManifestParserHost::OnParse( | 55 void PaymentManifestParserHost::ParseWebAppManifest(const std::string& content, |
| 52 const Callback* callback_identifier, | 56 WebAppCallback callback) { |
| 53 std::vector<mojom::PaymentManifestSectionPtr> manifest) { | 57 if (!mojo_client_) { |
| 54 // At most 10 manifests to parse, so iterating a vector is not too slow. | 58 std::move(callback).Run(std::vector<mojom::WebAppManifestSectionPtr>()); |
| 59 return; |
| 60 } |
| 61 |
| 62 int64_t callback_identifier = callback_counter_++; |
| 63 const auto& result = pending_web_app_callbacks_.insert( |
| 64 std::make_pair(callback_identifier, std::move(callback))); |
| 65 DCHECK(result.second); |
| 66 DCHECK_GE(10U, pending_web_app_callbacks_.size()); |
| 67 |
| 68 mojo_client_->service()->ParseWebAppManifest( |
| 69 content, base::Bind(&PaymentManifestParserHost::OnWebAppParse, |
| 70 base::Unretained(this), callback_identifier)); |
| 71 } |
| 72 |
| 73 void PaymentManifestParserHost::OnPaymentMethodParse( |
| 74 int64_t callback_identifier, |
| 75 const std::vector<GURL>& web_app_manifest_urls) { |
| 55 const auto& pending_callback_it = | 76 const auto& pending_callback_it = |
| 56 std::find_if(pending_callbacks_.begin(), pending_callbacks_.end(), | 77 pending_payment_method_callbacks_.find(callback_identifier); |
| 57 [callback_identifier](const Callback& pending_callback) { | 78 if (pending_callback_it == pending_payment_method_callbacks_.end()) { |
| 58 return &pending_callback == callback_identifier; | |
| 59 }); | |
| 60 if (pending_callback_it == pending_callbacks_.end()) { | |
| 61 // If unable to find the pending callback, then something went wrong in the | 79 // If unable to find the pending callback, then something went wrong in the |
| 62 // utility process. Stop the utility process and notify all callbacks. | 80 // utility process. Stop the utility process and notify all callbacks. |
| 63 OnUtilityProcessStopped(); | 81 OnUtilityProcessStopped(); |
| 64 return; | 82 return; |
| 65 } | 83 } |
| 66 | 84 |
| 67 Callback callback = std::move(*pending_callback_it); | 85 PaymentMethodCallback callback = std::move(pending_callback_it->second); |
| 68 pending_callbacks_.erase(pending_callback_it); | 86 pending_payment_method_callbacks_.erase(pending_callback_it); |
| 87 |
| 88 const size_t kMaximumNumberOfWebAppUrls = 100U; |
| 89 if (web_app_manifest_urls.size() > kMaximumNumberOfWebAppUrls) { |
| 90 // If more than 100 items, then something went wrong in the utility |
| 91 // process. Stop the utility process and notify all callbacks. |
| 92 OnUtilityProcessStopped(); |
| 93 return; |
| 94 } |
| 95 |
| 96 for (const auto& url : web_app_manifest_urls) { |
| 97 if (!url.is_valid() || !url.SchemeIs(url::kHttpsScheme)) { |
| 98 // If not a valid URL with HTTPS scheme, then something went wrong in the |
| 99 // utility process. Stop the utility process and notify all callbacks. |
| 100 OnUtilityProcessStopped(); |
| 101 return; |
| 102 } |
| 103 } |
| 69 | 104 |
| 70 // Can trigger synchronous deletion of this object, so can't access any of | 105 // Can trigger synchronous deletion of this object, so can't access any of |
| 71 // the member variables after this block. | 106 // the member variables after this block. |
| 107 std::move(callback).Run(web_app_manifest_urls); |
| 108 } |
| 109 |
| 110 void PaymentManifestParserHost::OnWebAppParse( |
| 111 int64_t callback_identifier, |
| 112 std::vector<mojom::WebAppManifestSectionPtr> manifest) { |
| 113 const auto& pending_callback_it = |
| 114 pending_web_app_callbacks_.find(callback_identifier); |
| 115 if (pending_callback_it == pending_web_app_callbacks_.end()) { |
| 116 // If unable to find the pending callback, then something went wrong in the |
| 117 // utility process. Stop the utility process and notify all callbacks. |
| 118 OnUtilityProcessStopped(); |
| 119 return; |
| 120 } |
| 121 |
| 122 const size_t kMaximumNumberOfSections = 100U; |
| 123 if (manifest.size() > kMaximumNumberOfSections) { |
| 124 // If more than 100 items, then something went wrong in the utility |
| 125 // process. Stop the utility process and notify all callbacks. |
| 126 OnUtilityProcessStopped(); |
| 127 return; |
| 128 } |
| 129 |
| 130 for (size_t i = 0; i < manifest.size(); ++i) { |
| 131 const size_t kMaximumNumberOfFingerprints = 100U; |
| 132 if (manifest[i]->fingerprints.size() > kMaximumNumberOfFingerprints) { |
| 133 // If more than 100 items, then something went wrong in the utility |
| 134 // process. Stop the utility process and notify all callbacks. |
| 135 OnUtilityProcessStopped(); |
| 136 return; |
| 137 } |
| 138 } |
| 139 |
| 140 WebAppCallback callback = std::move(pending_callback_it->second); |
| 141 pending_web_app_callbacks_.erase(pending_callback_it); |
| 142 |
| 143 // Can trigger synchronous deletion of this object, so can't access any of |
| 144 // the member variables after this block. |
| 72 std::move(callback).Run(std::move(manifest)); | 145 std::move(callback).Run(std::move(manifest)); |
| 73 } | 146 } |
| 74 | 147 |
| 75 void PaymentManifestParserHost::OnUtilityProcessStopped() { | 148 void PaymentManifestParserHost::OnUtilityProcessStopped() { |
| 76 mojo_client_.reset(); | 149 mojo_client_.reset(); |
| 77 std::vector<Callback> callbacks = std::move(pending_callbacks_); | 150 |
| 78 for (Callback& callback : callbacks) { | 151 std::unordered_map<int64_t, PaymentMethodCallback> payment_method_callbacks = |
| 152 std::move(pending_payment_method_callbacks_); |
| 153 std::unordered_map<int64_t, WebAppCallback> web_app_callbacks = |
| 154 std::move(pending_web_app_callbacks_); |
| 155 |
| 156 for (auto& callback : payment_method_callbacks) { |
| 79 // Can trigger synchronous deletion of this object, so can't access any of | 157 // Can trigger synchronous deletion of this object, so can't access any of |
| 80 // the member variables after this line. | 158 // the member variables after this line. |
| 81 std::move(callback).Run(std::vector<mojom::PaymentManifestSectionPtr>()); | 159 std::move(callback.second).Run(std::vector<GURL>()); |
| 160 } |
| 161 |
| 162 for (auto& callback : web_app_callbacks) { |
| 163 // Can trigger synchronous deletion of this object, so can't access any of |
| 164 // the member variables after this line. |
| 165 std::move(callback.second) |
| 166 .Run(std::vector<mojom::WebAppManifestSectionPtr>()); |
| 82 } | 167 } |
| 83 } | 168 } |
| 84 | 169 |
| 85 } // namespace payments | 170 } // namespace payments |
| OLD | NEW |