Chromium Code Reviews| Index: components/payments/content/payment_manifest_parser_host.cc |
| diff --git a/components/payments/content/payment_manifest_parser_host.cc b/components/payments/content/payment_manifest_parser_host.cc |
| index 04c13d254207458b080aa936764bce399bd25aeb..e9dbab70be366368a96b333114299ffb6ac520e9 100644 |
| --- a/components/payments/content/payment_manifest_parser_host.cc |
| +++ b/components/payments/content/payment_manifest_parser_host.cc |
| @@ -14,10 +14,11 @@ |
| #include "components/strings/grit/components_strings.h" |
| #include "content/public/browser/utility_process_mojo_client.h" |
| #include "ui/base/l10n/l10n_util.h" |
| +#include "url/url_constants.h" |
| namespace payments { |
| -PaymentManifestParserHost::PaymentManifestParserHost() {} |
| +PaymentManifestParserHost::PaymentManifestParserHost() : callback_counter_(0) {} |
| PaymentManifestParserHost::~PaymentManifestParserHost() {} |
| @@ -32,40 +33,112 @@ void PaymentManifestParserHost::StartUtilityProcess() { |
| mojo_client_->Start(); |
| } |
| -void PaymentManifestParserHost::ParsePaymentManifest(const std::string& content, |
| - Callback callback) { |
| +void PaymentManifestParserHost::ParsePaymentMethodManifest( |
| + const std::string& content, |
| + PaymentMethodCallback callback) { |
| if (!mojo_client_) { |
| - std::move(callback).Run(std::vector<mojom::PaymentManifestSectionPtr>()); |
| + std::move(callback).Run(std::vector<GURL>()); |
| return; |
| } |
| - pending_callbacks_.push_back(std::move(callback)); |
| - DCHECK_GE(10U, pending_callbacks_.size()); |
| - Callback* callback_identifier = &pending_callbacks_.back(); |
| + int64_t callback_identifier = callback_counter_++; |
| + const auto& result = pending_payment_method_callbacks_.insert( |
| + std::make_pair(callback_identifier, std::move(callback))); |
| + DCHECK(result.second); |
| + DCHECK_GE(10U, pending_payment_method_callbacks_.size()); |
| - mojo_client_->service()->Parse( |
| - content, base::Bind(&PaymentManifestParserHost::OnParse, |
| + mojo_client_->service()->ParsePaymentMethodManifest( |
| + content, base::Bind(&PaymentManifestParserHost::OnPaymentMethodParse, |
| base::Unretained(this), callback_identifier)); |
| } |
| -void PaymentManifestParserHost::OnParse( |
| - const Callback* callback_identifier, |
| - std::vector<mojom::PaymentManifestSectionPtr> manifest) { |
| - // At most 10 manifests to parse, so iterating a vector is not too slow. |
| +void PaymentManifestParserHost::ParseWebAppManifest(const std::string& content, |
| + WebAppCallback callback) { |
| + if (!mojo_client_) { |
| + std::move(callback).Run(std::vector<mojom::WebAppManifestSectionPtr>()); |
| + return; |
| + } |
| + |
| + int64_t callback_identifier = callback_counter_++; |
| + const auto& result = pending_web_app_callbacks_.insert( |
| + std::make_pair(callback_identifier, std::move(callback))); |
| + DCHECK(result.second); |
| + DCHECK_GE(10U, pending_web_app_callbacks_.size()); |
| + |
| + mojo_client_->service()->ParseWebAppManifest( |
| + content, base::Bind(&PaymentManifestParserHost::OnWebAppParse, |
| + base::Unretained(this), callback_identifier)); |
| +} |
| + |
| +void PaymentManifestParserHost::OnPaymentMethodParse( |
| + int64_t callback_identifier, |
| + const std::vector<GURL>& webAppManifestUrls) { |
|
Mathieu
2017/04/07 13:29:06
c++ case
please use gerrit instead
2017/04/07 16:56:30
Done.
|
| + const auto& pending_callback_it = |
| + pending_payment_method_callbacks_.find(callback_identifier); |
| + if (pending_callback_it == pending_payment_method_callbacks_.end()) { |
| + // If unable to find the pending callback, then something went wrong in the |
| + // utility process. Stop the utility process and notify all callbacks. |
| + OnUtilityProcessStopped(); |
| + return; |
| + } |
| + |
| + PaymentMethodCallback callback = std::move(pending_callback_it->second); |
| + pending_payment_method_callbacks_.erase(pending_callback_it); |
| + |
| + const size_t kMaximumNumberOfWebAppUrls = 100U; |
| + if (webAppManifestUrls.size() > kMaximumNumberOfWebAppUrls) { |
| + // If more than 100 items, then something went wrong in the utility |
| + // process. Stop the utility process and notify all callbacks. |
| + OnUtilityProcessStopped(); |
| + return; |
| + } |
| + |
| + for (const auto& gurl : webAppManifestUrls) { |
| + if (!gurl.is_valid() || !gurl.SchemeIs(url::kHttpsScheme)) { |
| + // If not a valid URL with HTTPS scheme, then something went wrong in the |
| + // utility process. Stop the utility process and notify all callbacks. |
| + OnUtilityProcessStopped(); |
| + return; |
| + } |
| + } |
| + |
| + // Can trigger synchronous deletion of this object, so can't access any of |
| + // the member variables after this block. |
| + std::move(callback).Run(webAppManifestUrls); |
| +} |
| + |
| +void PaymentManifestParserHost::OnWebAppParse( |
| + int64_t callback_identifier, |
| + std::vector<mojom::WebAppManifestSectionPtr> manifest) { |
| const auto& pending_callback_it = |
| - std::find_if(pending_callbacks_.begin(), pending_callbacks_.end(), |
| - [callback_identifier](const Callback& pending_callback) { |
| - return &pending_callback == callback_identifier; |
| - }); |
| - if (pending_callback_it == pending_callbacks_.end()) { |
| + pending_web_app_callbacks_.find(callback_identifier); |
| + if (pending_callback_it == pending_web_app_callbacks_.end()) { |
| // If unable to find the pending callback, then something went wrong in the |
| // utility process. Stop the utility process and notify all callbacks. |
| OnUtilityProcessStopped(); |
| return; |
| } |
| - Callback callback = std::move(*pending_callback_it); |
| - pending_callbacks_.erase(pending_callback_it); |
| + const size_t kMaximumNumberOfSections = 100U; |
| + if (manifest.size() > kMaximumNumberOfSections) { |
| + // If more than 100 items, then something went wrong in the utility |
| + // process. Stop the utility process and notify all callbacks. |
| + OnUtilityProcessStopped(); |
| + return; |
| + } |
| + |
| + for (size_t i = 0; i < manifest.size(); ++i) { |
| + const size_t kMaximumNumberOfFingerprints = 100U; |
| + if (manifest[i]->fingerprints.size() > kMaximumNumberOfFingerprints) { |
| + // If more than 100 items, then something went wrong in the utility |
| + // process. Stop the utility process and notify all callbacks. |
| + OnUtilityProcessStopped(); |
| + return; |
| + } |
| + } |
| + |
| + WebAppCallback callback = std::move(pending_callback_it->second); |
| + pending_web_app_callbacks_.erase(pending_callback_it); |
| // Can trigger synchronous deletion of this object, so can't access any of |
| // the member variables after this block. |
| @@ -74,11 +147,23 @@ void PaymentManifestParserHost::OnParse( |
| void PaymentManifestParserHost::OnUtilityProcessStopped() { |
| mojo_client_.reset(); |
| - std::vector<Callback> callbacks = std::move(pending_callbacks_); |
| - for (Callback& callback : callbacks) { |
| + |
| + std::unordered_map<int64_t, PaymentMethodCallback> payment_method_callbacks = |
| + std::move(pending_payment_method_callbacks_); |
| + std::unordered_map<int64_t, WebAppCallback> web_app_callbacks = |
| + std::move(pending_web_app_callbacks_); |
| + |
| + for (auto& callback : payment_method_callbacks) { |
| + // Can trigger synchronous deletion of this object, so can't access any of |
| + // the member variables after this line. |
| + std::move(callback.second).Run(std::vector<GURL>()); |
| + } |
| + |
| + for (auto& callback : web_app_callbacks) { |
| // Can trigger synchronous deletion of this object, so can't access any of |
| // the member variables after this line. |
| - std::move(callback).Run(std::vector<mojom::PaymentManifestSectionPtr>()); |
| + std::move(callback.second) |
| + .Run(std::vector<mojom::WebAppManifestSectionPtr>()); |
| } |
| } |