Commit 8dbd81a8 authored by Mohamad Ahmadi's avatar Mohamad Ahmadi Committed by Commit Bot

[Payment Request] PaymentRequestCache, a map of WebState to PaymentRequests

- Creates a PaymentRequestCache class as KeyedService along with its
  factory to ensure only one instance of it exists per BrowserState.
- PaymentRequestCache maintains a map of web::WebState to a list of
  payments::PaymentRequest instances maintained for that WebState.
- The PaymentRequestCache instance can be accessed in browser tests via
  the instance of BrowserState in order to verify the correct creation
  and state of payments::PaymentRequest objects.

BUG=602666

Change-Id: I4aef5be49ab93261bcbebd6f742378bcca59938e
Reviewed-on: https://chromium-review.googlesource.com/577379Reviewed-by: default avatarRouslan Solomakhin <rouslan@chromium.org>
Commit-Queue: mahmadi <mahmadi@chromium.org>
Cr-Commit-Position: refs/heads/master@{#487909}
parent 0e184eb4
......@@ -9,10 +9,14 @@ source_set("payments") {
sources = [
"ios_can_make_payment_query_factory.cc",
"ios_can_make_payment_query_factory.h",
"ios_payment_request_cache_factory.h",
"ios_payment_request_cache_factory.mm",
"itunes_json_request.cc",
"itunes_json_request.h",
"payment_request.h",
"payment_request.mm",
"payment_request_cache.h",
"payment_request_cache.mm",
"payment_request_util.h",
"payment_request_util.mm",
]
......
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef IOS_CHROME_BROWSER_PAYMENTS_IOS_PAYMENT_REQUEST_CACHE_FACTORY_H_
#define IOS_CHROME_BROWSER_PAYMENTS_IOS_PAYMENT_REQUEST_CACHE_FACTORY_H_
#include <memory>
#include "base/macros.h"
#include "components/keyed_service/ios/browser_state_keyed_service_factory.h"
namespace base {
template <typename T>
struct DefaultSingletonTraits;
} // namespace base
namespace ios {
class ChromeBrowserState;
} // namespace ios
namespace payments {
class PaymentRequestCache;
// Ensures that there's only one instance of payments::PaymentRequestCache per
// browser state. Allows the PaymentRequestCache instance to be used in browser
// tests.
class IOSPaymentRequestCacheFactory : public BrowserStateKeyedServiceFactory {
public:
static PaymentRequestCache* GetForBrowserState(
ios::ChromeBrowserState* browser_state);
static IOSPaymentRequestCacheFactory* GetInstance();
private:
friend struct base::DefaultSingletonTraits<IOSPaymentRequestCacheFactory>;
IOSPaymentRequestCacheFactory();
~IOSPaymentRequestCacheFactory() override;
// BrowserStateKeyedServiceFactory implementation.
std::unique_ptr<KeyedService> BuildServiceInstanceFor(
web::BrowserState* context) const override;
DISALLOW_COPY_AND_ASSIGN(IOSPaymentRequestCacheFactory);
};
} // namespace payments
#endif // IOS_CHROME_BROWSER_PAYMENTS_IOS_PAYMENT_REQUEST_CACHE_FACTORY_H_
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ios/chrome/browser/payments/ios_payment_request_cache_factory.h"
#include "base/memory/ptr_util.h"
#include "base/memory/singleton.h"
#include "components/keyed_service/ios/browser_state_dependency_manager.h"
#include "ios/chrome/browser/browser_state/chrome_browser_state.h"
#include "ios/chrome/browser/payments/payment_request_cache.h"
#include "ios/web/public/browser_state.h"
namespace payments {
// static
payments::PaymentRequestCache*
IOSPaymentRequestCacheFactory::GetForBrowserState(
ios::ChromeBrowserState* browser_state) {
return static_cast<payments::PaymentRequestCache*>(
GetInstance()->GetServiceForBrowserState(browser_state, true));
}
// static
IOSPaymentRequestCacheFactory* IOSPaymentRequestCacheFactory::GetInstance() {
return base::Singleton<IOSPaymentRequestCacheFactory>::get();
}
IOSPaymentRequestCacheFactory::IOSPaymentRequestCacheFactory()
: BrowserStateKeyedServiceFactory(
"PaymentRequestCache",
BrowserStateDependencyManager::GetInstance()) {}
IOSPaymentRequestCacheFactory::~IOSPaymentRequestCacheFactory() {}
std::unique_ptr<KeyedService>
IOSPaymentRequestCacheFactory::BuildServiceInstanceFor(
web::BrowserState* context) const {
return base::WrapUnique(new payments::PaymentRequestCache);
}
} // namespace payments
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef IOS_CHROME_BROWSER_PAYMENTS_PAYMENT_REQUEST_CACHE_H_
#define IOS_CHROME_BROWSER_PAYMENTS_PAYMENT_REQUEST_CACHE_H_
#include <memory>
#include <set>
#include <unordered_map>
#include "base/macros.h"
#include "components/keyed_service/core/keyed_service.h"
#include "ios/chrome/browser/payments/payment_request.h"
#include "ios/web/public/web_state/web_state.h"
namespace payments {
// Maintains a map of web::WebState to a list of payments::PaymentRequest
// instances maintained for that web state.
class PaymentRequestCache : public KeyedService {
public:
typedef std::set<std::unique_ptr<payments::PaymentRequest>,
payments::PaymentRequest::Compare>
PaymentRequestSet;
PaymentRequestCache();
~PaymentRequestCache() override;
// Adds |payment_request| to the cache for |web_state| and returns a pointer
// to the payments::PaymentRequest instance wrapped by |payment_request|.
payments::PaymentRequest* AddPaymentRequest(
web::WebState* web_state,
std::unique_ptr<payments::PaymentRequest> payment_request);
// Returns the payments::PaymentRequest instances maintained for |web_state|.
PaymentRequestCache::PaymentRequestSet& GetPaymentRequests(
web::WebState* web_state);
// Clears the payments::PaymentRequest instances maintained for |web_state|.
void ClearPaymentRequests(web::WebState* web_state);
private:
std::unordered_map<web::WebState*, PaymentRequestSet> payment_requests_;
};
} // namespace payments
#endif // IOS_CHROME_BROWSER_PAYMENTS_PAYMENT_REQUEST_CACHE_H_
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import "ios/chrome/browser/payments/payment_request_cache.h"
#include <utility>
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
namespace payments {
PaymentRequestCache::PaymentRequestCache() {}
PaymentRequestCache::~PaymentRequestCache() {}
payments::PaymentRequest* PaymentRequestCache::AddPaymentRequest(
web::WebState* web_state,
std::unique_ptr<payments::PaymentRequest> payment_request) {
PaymentRequestCache::PaymentRequestSet& payment_requests =
GetPaymentRequests(web_state);
const auto result = payment_requests.insert(std::move(payment_request));
return result.first->get();
}
PaymentRequestCache::PaymentRequestSet& PaymentRequestCache::GetPaymentRequests(
web::WebState* web_state) {
const auto result = payment_requests_.insert(
std::make_pair(web_state, PaymentRequestCache::PaymentRequestSet()));
return result.first->second;
}
void PaymentRequestCache::ClearPaymentRequests(web::WebState* web_state) {
payment_requests_.erase(web_state);
}
} // namespace payments
......@@ -35,7 +35,9 @@
#include "ios/chrome/browser/autofill/validation_rules_storage_factory.h"
#include "ios/chrome/browser/browser_state/chrome_browser_state.h"
#include "ios/chrome/browser/payments/ios_can_make_payment_query_factory.h"
#include "ios/chrome/browser/payments/ios_payment_request_cache_factory.h"
#include "ios/chrome/browser/payments/payment_request.h"
#import "ios/chrome/browser/payments/payment_request_cache.h"
#include "ios/chrome/browser/procedural_block_types.h"
#import "ios/chrome/browser/ui/commands/UIKit+ChromeExecuteCommand.h"
#import "ios/chrome/browser/ui/commands/generic_chrome_command.h"
......@@ -96,12 +98,9 @@ struct PendingPaymentResponse {
// PersonalDataManager used to manage user credit cards and addresses.
autofill::PersonalDataManager* _personalDataManager;
// The of map WebState to the list of payments::PaymentRequest instances
// maintained for that WebState.
std::unordered_map<web::WebState*,
std::set<std::unique_ptr<payments::PaymentRequest>,
payments::PaymentRequest::Compare>>
_paymentRequests;
// Maintains a map of web::WebState to a list of payments::PaymentRequest
// instances maintained for that WebState.
payments::PaymentRequestCache* _paymentRequestCache;
// The observer for |_activeWebState|.
std::unique_ptr<web::WebStateObserverBridge> _activeWebStateObserver;
......@@ -242,6 +241,10 @@ struct PendingPaymentResponse {
_personalDataManager =
autofill::PersonalDataManagerFactory::GetForBrowserState(
browserState->GetOriginalChromeBrowserState());
_paymentRequestCache =
payments::IOSPaymentRequestCacheFactory::GetForBrowserState(
browserState->GetOriginalChromeBrowserState());
}
return self;
}
......@@ -261,11 +264,6 @@ struct PendingPaymentResponse {
[webState->GetJSInjectionReceiver()
instanceOfClass:[JSPaymentRequestManager class]]);
_activeWebState = webState;
if (_paymentRequests.find(webState) == _paymentRequests.end()) {
_paymentRequests[webState] =
std::set<std::unique_ptr<payments::PaymentRequest>,
payments::PaymentRequest::Compare>();
}
_activeWebStateObserver =
base::MakeUnique<web::WebStateObserverBridge>(webState, self);
[self enableActiveWebState];
......@@ -278,9 +276,7 @@ struct PendingPaymentResponse {
// The lifetime of a PaymentRequest is tied to the WebState it is associated
// with and the current URL. Therefore, PaymentRequest instances should get
// destroyed when the WebState goes away.
const auto iterator = _paymentRequests.find(webState);
DCHECK(iterator != _paymentRequests.end());
_paymentRequests.erase(iterator);
_paymentRequestCache->ClearPaymentRequests(webState);
}
- (void)enablePaymentRequest:(BOOL)enabled {
......@@ -419,15 +415,10 @@ struct PendingPaymentResponse {
DLOG(ERROR) << "JS message parameter 'payment_request' is invalid";
return nullptr;
}
const auto iterator = _paymentRequests.find(_activeWebState);
DCHECK(iterator != _paymentRequests.end());
const auto result =
iterator->second.insert(base::MakeUnique<payments::PaymentRequest>(
webPaymentRequest, _browserState, _activeWebState,
_personalDataManager, self));
DCHECK(result.first != iterator->second.end());
return result.first->get();
return _paymentRequestCache->AddPaymentRequest(
_activeWebState, base::MakeUnique<payments::PaymentRequest>(
webPaymentRequest, _browserState, _activeWebState,
_personalDataManager, self));
}
// Extracts a web::PaymentRequest from |message|. Returns the cached instance of
......@@ -925,25 +916,23 @@ requestFullCreditCard:(const autofill::CreditCard&)creditCard
// The lifetime of a PaymentRequest is tied to the WebState it is associated
// with and the current URL. Therefore, PaymentRequest instances should get
// destroyed when the WebState goes away or the user navigates to a URL.
const auto iterator = _paymentRequests.find(_activeWebState);
DCHECK(iterator != _paymentRequests.end());
iterator->second.clear();
_paymentRequestCache->ClearPaymentRequests(_activeWebState);
}
#pragma mark - Helper methods
- (payments::PaymentRequest*)paymentRequestWithId:
(std::string)paymentRequestId {
const auto iterator = _paymentRequests.find(_activeWebState);
DCHECK(iterator != _paymentRequests.end());
const payments::PaymentRequestCache::PaymentRequestSet& paymentRequests =
_paymentRequestCache->GetPaymentRequests(_activeWebState);
const auto found = std::find_if(
iterator->second.begin(), iterator->second.end(),
paymentRequests.begin(), paymentRequests.end(),
[&paymentRequestId](
const std::unique_ptr<payments::PaymentRequest>& request) {
return request.get()->web_payment_request().payment_request_id ==
paymentRequestId;
});
return found != iterator->second.end() ? found->get() : nullptr;
return found != paymentRequests.end() ? found->get() : nullptr;
}
@end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment