Commit cd94cfda authored by Liquan (Max) Gu's avatar Liquan (Max) Gu Committed by Commit Bot

Access PaymentAppProvider with paymentRequestWebContents

Change:
* Fixed the issue of not being able to launch payment handler twice.
The cause is that when PaymentHandler UI is opened and closed, the
payment handler's PaymentAppProvider was used, instead of the
merchant's.
* Changed PaymentAppProvider to make it one per merchant's WebContents,
not one per payment handler's WebContents.
* Rename WebContents PaymentHandler's codebase to make it better
distinguish between the merchant's and the paymentHandler's.
* Move PaymentAppProvider::SetOpenedWindow() from
service_worker_client_utils into PaymentRequestImpl -->
ServiceWorkerPaymentAppBridge.

Bug: 1131874

Change-Id: I831fec0f3cce0d19abf49d23ccf1e61689afe33a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2434101Reviewed-by: default avatarKinuko Yasuda <kinuko@chromium.org>
Reviewed-by: default avatarLiquan (Max) Gu <maxlg@chromium.org>
Reviewed-by: default avatarRouslan Solomakhin <rouslan@chromium.org>
Commit-Queue: Liquan (Max) Gu <maxlg@chromium.org>
Cr-Commit-Position: refs/heads/master@{#811554}
parent 4b732658
...@@ -708,6 +708,10 @@ public class PaymentRequestImpl ...@@ -708,6 +708,10 @@ public class PaymentRequestImpl
WebContents paymentHandlerWebContents = mPaymentUIsManager.showPaymentHandlerUI( WebContents paymentHandlerWebContents = mPaymentUIsManager.showPaymentHandlerUI(
url, mComponentPaymentRequestImpl.isOffTheRecord()); url, mComponentPaymentRequestImpl.isOffTheRecord());
if (paymentHandlerWebContents != null) { if (paymentHandlerWebContents != null) {
ServiceWorkerPaymentAppBridge.onOpeningPaymentAppWindow(
/*paymentRequestWebContents=*/mWebContents,
/*paymentHandlerWebContents=*/paymentHandlerWebContents);
// UKM for payment app origin should get recorded only when the origin of the invoked // UKM for payment app origin should get recorded only when the origin of the invoked
// payment app is shown to the user. // payment app is shown to the user.
mJourneyLogger.setPaymentAppUkmSourceId(mInvokedPaymentApp.getUkmSourceId()); mJourneyLogger.setPaymentAppUkmSourceId(mInvokedPaymentApp.getUkmSourceId());
......
...@@ -110,18 +110,20 @@ public class ServiceWorkerPaymentAppBridge { ...@@ -110,18 +110,20 @@ public class ServiceWorkerPaymentAppBridge {
@Override @Override
public void onDidFinishNavigation(Tab tab, NavigationHandle navigationHandle) { public void onDidFinishNavigation(Tab tab, NavigationHandle navigationHandle) {
// Notify closing payment app window so as to abort payment if unsecure. // Notify closing payment app window so as to abort payment if unsecure.
WebContents webContents = tab.getWebContents(); WebContents paymentRequestWebContents = tab.getWebContents();
if (!SslValidityChecker.isValidPageInPaymentHandlerWindow(webContents)) { if (!SslValidityChecker.isValidPageInPaymentHandlerWindow(
onClosingPaymentAppWindowForInsecureNavigation(webContents); paymentRequestWebContents)) {
onClosingPaymentAppWindowForInsecureNavigation(paymentRequestWebContents);
} }
} }
@Override @Override
public void onSSLStateUpdated(Tab tab) { public void onSSLStateUpdated(Tab tab) {
// Notify closing payment app window so as to abort payment if unsecure. // Notify closing payment app window so as to abort payment if unsecure.
WebContents webContents = tab.getWebContents(); WebContents paymentRequestWebContents = tab.getWebContents();
if (!SslValidityChecker.isValidPageInPaymentHandlerWindow(webContents)) { if (!SslValidityChecker.isValidPageInPaymentHandlerWindow(
onClosingPaymentAppWindowForInsecureNavigation(webContents); paymentRequestWebContents)) {
onClosingPaymentAppWindowForInsecureNavigation(paymentRequestWebContents);
} }
} }
}); });
...@@ -130,23 +132,39 @@ public class ServiceWorkerPaymentAppBridge { ...@@ -130,23 +132,39 @@ public class ServiceWorkerPaymentAppBridge {
/** /**
* Notify closing the opened payment app window for insecure navigation. * Notify closing the opened payment app window for insecure navigation.
* *
* @param webContents The web contents in the opened window. * @param paymentRequestWebContents The web contents in the opened window.
*/ */
public static void onClosingPaymentAppWindowForInsecureNavigation(WebContents webContents) { public static void onClosingPaymentAppWindowForInsecureNavigation(
if (webContents.isDestroyed()) return; WebContents paymentRequestWebContents) {
ServiceWorkerPaymentAppBridgeJni.get().onClosingPaymentAppWindow( if (paymentRequestWebContents.isDestroyed()) return;
webContents, PaymentEventResponseType.PAYMENT_HANDLER_INSECURE_NAVIGATION); ServiceWorkerPaymentAppBridgeJni.get().onClosingPaymentAppWindow(paymentRequestWebContents,
PaymentEventResponseType.PAYMENT_HANDLER_INSECURE_NAVIGATION);
} }
/** /**
* Notify closing the opened payment app window. * Notify closing the opened payment app window.
* *
* @param webContents The web contents in the opened window. Can be null. * @param paymentRequestWebContents The web contents in the opened window. Can be null.
*/ */
public static void onClosingPaymentAppWindow(@Nullable WebContents webContents) { public static void onClosingPaymentAppWindow(@Nullable WebContents paymentRequestWebContents) {
if (webContents == null || webContents.isDestroyed()) return; if (paymentRequestWebContents == null || paymentRequestWebContents.isDestroyed()) return;
ServiceWorkerPaymentAppBridgeJni.get().onClosingPaymentAppWindow( ServiceWorkerPaymentAppBridgeJni.get().onClosingPaymentAppWindow(
webContents, PaymentEventResponseType.PAYMENT_HANDLER_WINDOW_CLOSING); paymentRequestWebContents, PaymentEventResponseType.PAYMENT_HANDLER_WINDOW_CLOSING);
}
/**
* Called when payment handler's window is being opened.
*
* @param paymentRequestWebContents The web contents of the merchant's frame, cannot be null.
* @param paymentHandlerWebContents The web contents of the payment handler, cannot be null.
*/
public static void onOpeningPaymentAppWindow(
WebContents paymentRequestWebContents, WebContents paymentHandlerWebContents) {
if (paymentHandlerWebContents == null || paymentHandlerWebContents.isDestroyed()) return;
if (paymentRequestWebContents == null || paymentRequestWebContents.isDestroyed()) return;
ServiceWorkerPaymentAppBridgeJni.get().onOpeningPaymentAppWindow(
/*paymentRequestWebContents=*/paymentRequestWebContents,
/*paymentHandlerWebContents=*/paymentHandlerWebContents);
} }
/** /**
...@@ -189,7 +207,9 @@ public class ServiceWorkerPaymentAppBridge { ...@@ -189,7 +207,9 @@ public class ServiceWorkerPaymentAppBridge {
interface Natives { interface Natives {
void hasServiceWorkerPaymentApps(HasServiceWorkerPaymentAppsCallback callback); void hasServiceWorkerPaymentApps(HasServiceWorkerPaymentAppsCallback callback);
void getServiceWorkerPaymentAppsInfo(GetServiceWorkerPaymentAppsInfoCallback callback); void getServiceWorkerPaymentAppsInfo(GetServiceWorkerPaymentAppsInfoCallback callback);
void onClosingPaymentAppWindow(WebContents webContents, int reason); void onClosingPaymentAppWindow(WebContents paymentRequestWebContents, int reason);
void onOpeningPaymentAppWindow(
WebContents paymentRequestWebContents, WebContents paymentHandlerWebContents);
long getSourceIdForPaymentAppFromScope(GURL swScope); long getSourceIdForPaymentAppFromScope(GURL swScope);
} }
} }
...@@ -79,8 +79,10 @@ public class PaymentHandlerCoordinator { ...@@ -79,8 +79,10 @@ public class PaymentHandlerCoordinator {
PropertyModel model = new PropertyModel.Builder(PaymentHandlerProperties.ALL_KEYS).build(); PropertyModel model = new PropertyModel.Builder(PaymentHandlerProperties.ALL_KEYS).build();
PaymentHandlerMediator mediator = new PaymentHandlerMediator(model, this::hide, PaymentHandlerMediator mediator = new PaymentHandlerMediator(model, this::hide,
mPaymentHandlerWebContents, uiObserver, activity.getActivityTab().getView(), /*paymentRequestWebContents=*/paymentRequestWebContents,
mToolbarCoordinator.getToolbarHeightPx(), activity.getLifecycleDispatcher(), /*paymentHandlerWebContents*/ mPaymentHandlerWebContents, uiObserver,
activity.getActivityTab().getView(), mToolbarCoordinator.getToolbarHeightPx(),
activity.getLifecycleDispatcher(),
BottomSheetControllerProvider.from(activity.getWindowAndroid())); BottomSheetControllerProvider.from(activity.getWindowAndroid()));
activity.getWindow().getDecorView().addOnLayoutChangeListener(mediator); activity.getWindow().getDecorView().addOnLayoutChangeListener(mediator);
BottomSheetController bottomSheetController = BottomSheetController bottomSheetController =
......
...@@ -46,11 +46,8 @@ import java.lang.annotation.RetentionPolicy; ...@@ -46,11 +46,8 @@ import java.lang.annotation.RetentionPolicy;
private final PropertyModel mModel; private final PropertyModel mModel;
// Whenever invoked, invoked outside of the WebContentsObserver callbacks. // Whenever invoked, invoked outside of the WebContentsObserver callbacks.
private final Runnable mHider; private final Runnable mHider;
// Postfixes with "Ref" to distinguish from mWebContent in WebContentsObserver. Although private final WebContents mPaymentRequestWebContents;
// referencing the same object, mWebContentsRef is preferable to WebContents here because private final WebContents mPaymentHandlerWebContents;
// mWebContents (a weak ref) requires null checks, while mWebContentsRef is guaranteed to be not
// null.
private final WebContents mWebContentsRef;
private final PaymentHandlerUiObserver mPaymentHandlerUiObserver; private final PaymentHandlerUiObserver mPaymentHandlerUiObserver;
// Used to postpone execution of a callback to avoid destroy objects (e.g., WebContents) in // Used to postpone execution of a callback to avoid destroy objects (e.g., WebContents) in
// their own methods. // their own methods.
...@@ -82,7 +79,8 @@ import java.lang.annotation.RetentionPolicy; ...@@ -82,7 +79,8 @@ import java.lang.annotation.RetentionPolicy;
* payment handler component. * payment handler component.
* @param hider The callback to clean up {@link PaymentHandlerCoordinator} when the sheet is * @param hider The callback to clean up {@link PaymentHandlerCoordinator} when the sheet is
* hidden. * hidden.
* @param webContents The web-contents that loads the payment app. * @param paymentRequestWebContents The WebContents of the merchant's frame.
* @param paymentHandlerWebContents The WebContents of the payment handler.
* @param observer The {@link PaymentHandlerUiObserver} that observes this Payment Handler UI. * @param observer The {@link PaymentHandlerUiObserver} that observes this Payment Handler UI.
* @param tabView The view of the main tab. * @param tabView The view of the main tab.
* @param toolbarViewHeightPx The height of the toolbar view in px. * @param toolbarViewHeightPx The height of the toolbar view in px.
...@@ -91,14 +89,16 @@ import java.lang.annotation.RetentionPolicy; ...@@ -91,14 +89,16 @@ import java.lang.annotation.RetentionPolicy;
* @param sheetController A {@link BottomSheetController} to show UI in. * @param sheetController A {@link BottomSheetController} to show UI in.
*/ */
/* package */ PaymentHandlerMediator(PropertyModel model, Runnable hider, /* package */ PaymentHandlerMediator(PropertyModel model, Runnable hider,
WebContents webContents, PaymentHandlerUiObserver observer, View tabView, WebContents paymentRequestWebContents, WebContents paymentHandlerWebContents,
int toolbarViewHeightPx, ActivityLifecycleDispatcher activityLifeCycleDispatcher, PaymentHandlerUiObserver observer, View tabView, int toolbarViewHeightPx,
ActivityLifecycleDispatcher activityLifeCycleDispatcher,
BottomSheetController sheetController) { BottomSheetController sheetController) {
super(webContents); super(paymentHandlerWebContents);
assert webContents != null; assert paymentHandlerWebContents != null;
mTabView = tabView; mTabView = tabView;
mBottomSheetController = sheetController; mBottomSheetController = sheetController;
mWebContentsRef = webContents; mPaymentRequestWebContents = paymentRequestWebContents;
mPaymentHandlerWebContents = paymentHandlerWebContents;
mToolbarViewHeightPx = toolbarViewHeightPx; mToolbarViewHeightPx = toolbarViewHeightPx;
mModel = model; mModel = model;
mModel.set(PaymentHandlerProperties.BACK_PRESS_CALLBACK, this::onSystemBackButtonClicked); mModel.set(PaymentHandlerProperties.BACK_PRESS_CALLBACK, this::onSystemBackButtonClicked);
...@@ -166,7 +166,7 @@ import java.lang.annotation.RetentionPolicy; ...@@ -166,7 +166,7 @@ import java.lang.annotation.RetentionPolicy;
private void showScrim() { private void showScrim() {
// Using an empty scrim observer is to avoid the dismissal of the bottom-sheet on tapping. // Using an empty scrim observer is to avoid the dismissal of the bottom-sheet on tapping.
ChromeActivity activity = ChromeActivity.fromWebContents(mWebContentsRef); ChromeActivity activity = ChromeActivity.fromWebContents(mPaymentHandlerWebContents);
assert activity != null; assert activity != null;
PropertyModel params = mBottomSheetController.createScrimParams(); PropertyModel params = mBottomSheetController.createScrimParams();
...@@ -206,7 +206,7 @@ import java.lang.annotation.RetentionPolicy; ...@@ -206,7 +206,7 @@ import java.lang.annotation.RetentionPolicy;
switch (mCloseReason) { switch (mCloseReason) {
case CloseReason.INSECURE_NAVIGATION: case CloseReason.INSECURE_NAVIGATION:
ServiceWorkerPaymentAppBridge.onClosingPaymentAppWindowForInsecureNavigation( ServiceWorkerPaymentAppBridge.onClosingPaymentAppWindowForInsecureNavigation(
mWebContentsRef); mPaymentRequestWebContents);
break; break;
case CloseReason.USER: case CloseReason.USER:
// Intentional fallthrough. // Intentional fallthrough.
...@@ -214,7 +214,7 @@ import java.lang.annotation.RetentionPolicy; ...@@ -214,7 +214,7 @@ import java.lang.annotation.RetentionPolicy;
// Intentional fallthrough. // Intentional fallthrough.
// TODO(crbug.com/1017926): Respond to service worker with the net error. // TODO(crbug.com/1017926): Respond to service worker with the net error.
case CloseReason.ACTIVITY_DIED: case CloseReason.ACTIVITY_DIED:
ServiceWorkerPaymentAppBridge.onClosingPaymentAppWindow(mWebContentsRef); ServiceWorkerPaymentAppBridge.onClosingPaymentAppWindow(mPaymentRequestWebContents);
break; break;
case CloseReason.OTHERS: case CloseReason.OTHERS:
// No need to notify ServiceWorkerPaymentAppBridge when merchant aborts the // No need to notify ServiceWorkerPaymentAppBridge when merchant aborts the
...@@ -230,7 +230,7 @@ import java.lang.annotation.RetentionPolicy; ...@@ -230,7 +230,7 @@ import java.lang.annotation.RetentionPolicy;
} }
private void hideScrim() { private void hideScrim() {
ChromeActivity activity = ChromeActivity.fromWebContents(mWebContentsRef); ChromeActivity activity = ChromeActivity.fromWebContents(mPaymentHandlerWebContents);
// activity would be null when this method is triggered by activity being destroyed. // activity would be null when this method is triggered by activity being destroyed.
if (activity == null) return; if (activity == null) return;
...@@ -255,7 +255,7 @@ import java.lang.annotation.RetentionPolicy; ...@@ -255,7 +255,7 @@ import java.lang.annotation.RetentionPolicy;
} }
private void closeIfInsecure() { private void closeIfInsecure() {
if (!SslValidityChecker.isValidPageInPaymentHandlerWindow(mWebContentsRef)) { if (!SslValidityChecker.isValidPageInPaymentHandlerWindow(mPaymentHandlerWebContents)) {
closeUIForInsecureNavigation(); closeUIForInsecureNavigation();
} }
} }
...@@ -284,7 +284,7 @@ import java.lang.annotation.RetentionPolicy; ...@@ -284,7 +284,7 @@ import java.lang.annotation.RetentionPolicy;
} }
private void onSystemBackButtonClicked() { private void onSystemBackButtonClicked() {
NavigationController navigation = mWebContentsRef.getNavigationController(); NavigationController navigation = mPaymentHandlerWebContents.getNavigationController();
if (navigation.canGoBack()) navigation.goBack(); if (navigation.canGoBack()) navigation.goBack();
} }
} }
...@@ -121,16 +121,35 @@ static void JNI_ServiceWorkerPaymentAppBridge_GetServiceWorkerPaymentAppsInfo( ...@@ -121,16 +121,35 @@ static void JNI_ServiceWorkerPaymentAppBridge_GetServiceWorkerPaymentAppsInfo(
static void JNI_ServiceWorkerPaymentAppBridge_OnClosingPaymentAppWindow( static void JNI_ServiceWorkerPaymentAppBridge_OnClosingPaymentAppWindow(
JNIEnv* env, JNIEnv* env,
const JavaParamRef<jobject>& jweb_contents, const JavaParamRef<jobject>& payment_request_jweb_contents,
jint reason) { jint reason) {
content::WebContents* web_contents = content::WebContents* payment_request_web_contents =
content::WebContents::FromJavaWebContents(jweb_contents); content::WebContents::FromJavaWebContents(payment_request_jweb_contents);
DCHECK(web_contents); // Verified in Java before invoking this function. DCHECK(payment_request_web_contents); // Verified in Java before invoking
content::PaymentAppProvider::GetOrCreateForWebContents(web_contents) // this function.
content::PaymentAppProvider::GetOrCreateForWebContents(
payment_request_web_contents)
->OnClosingOpenedWindow( ->OnClosingOpenedWindow(
static_cast<payments::mojom::PaymentEventResponseType>(reason)); static_cast<payments::mojom::PaymentEventResponseType>(reason));
} }
static void JNI_ServiceWorkerPaymentAppBridge_OnOpeningPaymentAppWindow(
JNIEnv* env,
const JavaParamRef<jobject>& payment_request_jweb_contents,
const JavaParamRef<jobject>& payment_handler_jweb_contents) {
content::WebContents* payment_request_web_contents =
content::WebContents::FromJavaWebContents(payment_request_jweb_contents);
content::WebContents* payment_handler_web_contents =
content::WebContents::FromJavaWebContents(payment_handler_jweb_contents);
DCHECK(payment_request_web_contents); // Verified in Java before invoking
// this function.
DCHECK(payment_handler_web_contents); // Verified in Java before invoking
// this function.
content::PaymentAppProvider::GetOrCreateForWebContents(
payment_request_web_contents)
->SetOpenedWindow(payment_handler_web_contents);
}
static jlong static jlong
JNI_ServiceWorkerPaymentAppBridge_GetSourceIdForPaymentAppFromScope( JNI_ServiceWorkerPaymentAppBridge_GetSourceIdForPaymentAppFromScope(
JNIEnv* env, JNIEnv* env,
......
...@@ -104,19 +104,21 @@ void AddModifiersToMap(const std::vector<PaymentDetailsModifierPtr>& modifiers, ...@@ -104,19 +104,21 @@ void AddModifiersToMap(const std::vector<PaymentDetailsModifierPtr>& modifiers,
// static // static
PaymentAppProvider* PaymentAppProvider::GetOrCreateForWebContents( PaymentAppProvider* PaymentAppProvider::GetOrCreateForWebContents(
WebContents* web_contents) { WebContents* payment_request_web_contents) {
return PaymentAppProviderImpl::GetOrCreateForWebContents(web_contents); return PaymentAppProviderImpl::GetOrCreateForWebContents(
payment_request_web_contents);
} }
// static // static
PaymentAppProviderImpl* PaymentAppProviderImpl::GetOrCreateForWebContents( PaymentAppProviderImpl* PaymentAppProviderImpl::GetOrCreateForWebContents(
WebContents* web_contents) { WebContents* payment_request_web_contents) {
DCHECK_CURRENTLY_ON(BrowserThread::UI); DCHECK_CURRENTLY_ON(BrowserThread::UI);
auto* data = PaymentAppProviderImpl::FromWebContents(web_contents); auto* data =
PaymentAppProviderImpl::FromWebContents(payment_request_web_contents);
if (!data) if (!data)
PaymentAppProviderImpl::CreateForWebContents(web_contents); PaymentAppProviderImpl::CreateForWebContents(payment_request_web_contents);
return PaymentAppProviderImpl::FromWebContents(web_contents); return PaymentAppProviderImpl::FromWebContents(payment_request_web_contents);
} }
void PaymentAppProviderImpl::InvokePaymentApp( void PaymentAppProviderImpl::InvokePaymentApp(
...@@ -125,7 +127,7 @@ void PaymentAppProviderImpl::InvokePaymentApp( ...@@ -125,7 +127,7 @@ void PaymentAppProviderImpl::InvokePaymentApp(
PaymentRequestEventDataPtr event_data, PaymentRequestEventDataPtr event_data,
InvokePaymentAppCallback callback) { InvokePaymentAppCallback callback) {
DCHECK_CURRENTLY_ON(BrowserThread::UI); DCHECK_CURRENTLY_ON(BrowserThread::UI);
DCHECK(web_contents_); DCHECK(payment_request_web_contents_);
scoped_refptr<DevToolsBackgroundServicesContextImpl> dev_tools = scoped_refptr<DevToolsBackgroundServicesContextImpl> dev_tools =
GetDevTools(sw_origin); GetDevTools(sw_origin);
...@@ -148,7 +150,7 @@ void PaymentAppProviderImpl::InvokePaymentApp( ...@@ -148,7 +150,7 @@ void PaymentAppProviderImpl::InvokePaymentApp(
StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>( StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>(
BrowserContext::GetDefaultStoragePartition( BrowserContext::GetDefaultStoragePartition(
web_contents_->GetBrowserContext())); payment_request_web_contents_->GetBrowserContext()));
scoped_refptr<ServiceWorkerContextWrapper> service_worker_context = scoped_refptr<ServiceWorkerContextWrapper> service_worker_context =
partition->GetServiceWorkerContext(); partition->GetServiceWorkerContext();
...@@ -173,7 +175,7 @@ void PaymentAppProviderImpl::InstallAndInvokePaymentApp( ...@@ -173,7 +175,7 @@ void PaymentAppProviderImpl::InstallAndInvokePaymentApp(
RegistrationIdCallback registration_id_callback, RegistrationIdCallback registration_id_callback,
InvokePaymentAppCallback callback) { InvokePaymentAppCallback callback) {
DCHECK_CURRENTLY_ON(BrowserThread::UI); DCHECK_CURRENTLY_ON(BrowserThread::UI);
DCHECK(web_contents_); DCHECK(payment_request_web_contents_);
if (!sw_js_url.is_valid() || !sw_scope.is_valid() || method.empty()) { if (!sw_js_url.is_valid() || !sw_scope.is_valid() || method.empty()) {
GetUIThreadTaskRunner({})->PostTask( GetUIThreadTaskRunner({})->PostTask(
...@@ -196,8 +198,8 @@ void PaymentAppProviderImpl::InstallAndInvokePaymentApp( ...@@ -196,8 +198,8 @@ void PaymentAppProviderImpl::InstallAndInvokePaymentApp(
} }
PaymentAppInstaller::Install( PaymentAppInstaller::Install(
web_contents_, app_name, string_encoded_icon, sw_js_url, sw_scope, payment_request_web_contents_, app_name, string_encoded_icon, sw_js_url,
sw_use_cache, method, supported_delegations, sw_scope, sw_use_cache, method, supported_delegations,
base::BindOnce(&PaymentAppProviderImpl::OnInstallPaymentApp, base::BindOnce(&PaymentAppProviderImpl::OnInstallPaymentApp,
weak_ptr_factory_.GetWeakPtr(), weak_ptr_factory_.GetWeakPtr(),
url::Origin::Create(sw_scope), std::move(event_data), url::Origin::Create(sw_scope), std::move(event_data),
...@@ -214,7 +216,7 @@ void PaymentAppProviderImpl::UpdatePaymentAppIcon( ...@@ -214,7 +216,7 @@ void PaymentAppProviderImpl::UpdatePaymentAppIcon(
PaymentAppProvider::UpdatePaymentAppIconCallback callback) { PaymentAppProvider::UpdatePaymentAppIconCallback callback) {
StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>( StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>(
BrowserContext::GetDefaultStoragePartition( BrowserContext::GetDefaultStoragePartition(
web_contents_->GetBrowserContext())); payment_request_web_contents_->GetBrowserContext()));
scoped_refptr<PaymentAppContextImpl> payment_app_context = scoped_refptr<PaymentAppContextImpl> payment_app_context =
partition->GetPaymentAppContext(); partition->GetPaymentAppContext();
...@@ -232,7 +234,7 @@ void PaymentAppProviderImpl::CanMakePayment( ...@@ -232,7 +234,7 @@ void PaymentAppProviderImpl::CanMakePayment(
CanMakePaymentEventDataPtr event_data, CanMakePaymentEventDataPtr event_data,
CanMakePaymentCallback callback) { CanMakePaymentCallback callback) {
DCHECK_CURRENTLY_ON(BrowserThread::UI); DCHECK_CURRENTLY_ON(BrowserThread::UI);
DCHECK(web_contents_); DCHECK(payment_request_web_contents_);
scoped_refptr<DevToolsBackgroundServicesContextImpl> dev_tools = scoped_refptr<DevToolsBackgroundServicesContextImpl> dev_tools =
GetDevTools(sw_origin); GetDevTools(sw_origin);
...@@ -255,7 +257,7 @@ void PaymentAppProviderImpl::CanMakePayment( ...@@ -255,7 +257,7 @@ void PaymentAppProviderImpl::CanMakePayment(
StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>( StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>(
BrowserContext::GetDefaultStoragePartition( BrowserContext::GetDefaultStoragePartition(
web_contents_->GetBrowserContext())); payment_request_web_contents_->GetBrowserContext()));
scoped_refptr<ServiceWorkerContextWrapper> service_worker_context = scoped_refptr<ServiceWorkerContextWrapper> service_worker_context =
partition->GetServiceWorkerContext(); partition->GetServiceWorkerContext();
...@@ -274,7 +276,7 @@ void PaymentAppProviderImpl::AbortPayment(int64_t registration_id, ...@@ -274,7 +276,7 @@ void PaymentAppProviderImpl::AbortPayment(int64_t registration_id,
const std::string& payment_request_id, const std::string& payment_request_id,
AbortCallback callback) { AbortCallback callback) {
DCHECK_CURRENTLY_ON(BrowserThread::UI); DCHECK_CURRENTLY_ON(BrowserThread::UI);
DCHECK(web_contents_); DCHECK(payment_request_web_contents_);
scoped_refptr<DevToolsBackgroundServicesContextImpl> dev_tools = scoped_refptr<DevToolsBackgroundServicesContextImpl> dev_tools =
GetDevTools(sw_origin); GetDevTools(sw_origin);
...@@ -287,7 +289,7 @@ void PaymentAppProviderImpl::AbortPayment(int64_t registration_id, ...@@ -287,7 +289,7 @@ void PaymentAppProviderImpl::AbortPayment(int64_t registration_id,
StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>( StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>(
BrowserContext::GetDefaultStoragePartition( BrowserContext::GetDefaultStoragePartition(
web_contents_->GetBrowserContext())); payment_request_web_contents_->GetBrowserContext()));
scoped_refptr<ServiceWorkerContextWrapper> service_worker_context = scoped_refptr<ServiceWorkerContextWrapper> service_worker_context =
partition->GetServiceWorkerContext(); partition->GetServiceWorkerContext();
...@@ -300,15 +302,16 @@ void PaymentAppProviderImpl::AbortPayment(int64_t registration_id, ...@@ -300,15 +302,16 @@ void PaymentAppProviderImpl::AbortPayment(int64_t registration_id,
std::move(service_worker_context), std::move(callback))); std::move(service_worker_context), std::move(callback)));
} }
void PaymentAppProviderImpl::SetOpenedWindow() { void PaymentAppProviderImpl::SetOpenedWindow(
WebContents* payment_handler_web_contents) {
DCHECK_CURRENTLY_ON(BrowserThread::UI); DCHECK_CURRENTLY_ON(BrowserThread::UI);
DCHECK(web_contents_); DCHECK(payment_handler_web_contents);
CloseOpenedWindow(); CloseOpenedWindow();
DCHECK(!payment_handler_window_); DCHECK(!payment_handler_window_);
payment_handler_window_ = payment_handler_window_ = std::make_unique<PaymentHandlerWindowObserver>(
std::make_unique<PaymentHandlerWindowObserver>(web_contents_); payment_handler_web_contents);
} }
void PaymentAppProviderImpl::CloseOpenedWindow() { void PaymentAppProviderImpl::CloseOpenedWindow() {
...@@ -338,9 +341,9 @@ void PaymentAppProviderImpl::OnClosingOpenedWindow( ...@@ -338,9 +341,9 @@ void PaymentAppProviderImpl::OnClosingOpenedWindow(
scoped_refptr<DevToolsBackgroundServicesContextImpl> scoped_refptr<DevToolsBackgroundServicesContextImpl>
PaymentAppProviderImpl::GetDevTools(const url::Origin& sw_origin) { PaymentAppProviderImpl::GetDevTools(const url::Origin& sw_origin) {
DCHECK_CURRENTLY_ON(BrowserThread::UI); DCHECK_CURRENTLY_ON(BrowserThread::UI);
DCHECK(web_contents_); DCHECK(payment_request_web_contents_);
auto* storage_partition = BrowserContext::GetStoragePartitionForSite( auto* storage_partition = BrowserContext::GetStoragePartitionForSite(
web_contents_->GetBrowserContext(), sw_origin.GetURL(), payment_request_web_contents_->GetBrowserContext(), sw_origin.GetURL(),
/*can_create=*/true); /*can_create=*/true);
if (!storage_partition) if (!storage_partition)
return nullptr; return nullptr;
...@@ -362,7 +365,7 @@ void PaymentAppProviderImpl::StartServiceWorkerForDispatch( ...@@ -362,7 +365,7 @@ void PaymentAppProviderImpl::StartServiceWorkerForDispatch(
StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>( StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>(
BrowserContext::GetDefaultStoragePartition( BrowserContext::GetDefaultStoragePartition(
web_contents_->GetBrowserContext())); payment_request_web_contents_->GetBrowserContext()));
scoped_refptr<ServiceWorkerContextWrapper> service_worker_context = scoped_refptr<ServiceWorkerContextWrapper> service_worker_context =
partition->GetServiceWorkerContext(); partition->GetServiceWorkerContext();
...@@ -381,7 +384,7 @@ void PaymentAppProviderImpl::OnInstallPaymentApp( ...@@ -381,7 +384,7 @@ void PaymentAppProviderImpl::OnInstallPaymentApp(
InvokePaymentAppCallback callback, InvokePaymentAppCallback callback,
int64_t registration_id) { int64_t registration_id) {
DCHECK_CURRENTLY_ON(BrowserThread::UI); DCHECK_CURRENTLY_ON(BrowserThread::UI);
DCHECK(web_contents_); DCHECK(payment_request_web_contents_);
if (registration_id >= 0) { if (registration_id >= 0) {
std::move(registration_id_callback).Run(registration_id); std::move(registration_id_callback).Run(registration_id);
...@@ -394,11 +397,12 @@ void PaymentAppProviderImpl::OnInstallPaymentApp( ...@@ -394,11 +397,12 @@ void PaymentAppProviderImpl::OnInstallPaymentApp(
} }
} }
PaymentAppProviderImpl::PaymentAppProviderImpl(WebContents* web_contents) PaymentAppProviderImpl::PaymentAppProviderImpl(
: web_contents_(web_contents), WebContents* payment_request_web_contents)
: payment_request_web_contents_(payment_request_web_contents),
event_dispatcher_( event_dispatcher_(
std::make_unique<ServiceWorkerCoreThreadEventDispatcher>( std::make_unique<ServiceWorkerCoreThreadEventDispatcher>(
web_contents_)) { payment_request_web_contents_)) {
event_dispatcher_->set_payment_app_provider(weak_ptr_factory_.GetWeakPtr()); event_dispatcher_->set_payment_app_provider(weak_ptr_factory_.GetWeakPtr());
} }
...@@ -408,8 +412,8 @@ PaymentAppProviderImpl::~PaymentAppProviderImpl() { ...@@ -408,8 +412,8 @@ PaymentAppProviderImpl::~PaymentAppProviderImpl() {
} }
PaymentAppProviderImpl::PaymentHandlerWindowObserver:: PaymentAppProviderImpl::PaymentHandlerWindowObserver::
PaymentHandlerWindowObserver(WebContents* web_contents) PaymentHandlerWindowObserver(WebContents* payment_handler_web_contents)
: WebContentsObserver(web_contents) {} : WebContentsObserver(payment_handler_web_contents) {}
PaymentAppProviderImpl::PaymentHandlerWindowObserver:: PaymentAppProviderImpl::PaymentHandlerWindowObserver::
~PaymentHandlerWindowObserver() = default; ~PaymentHandlerWindowObserver() = default;
......
...@@ -20,7 +20,7 @@ class CONTENT_EXPORT PaymentAppProviderImpl ...@@ -20,7 +20,7 @@ class CONTENT_EXPORT PaymentAppProviderImpl
public: public:
~PaymentAppProviderImpl() override; ~PaymentAppProviderImpl() override;
static PaymentAppProviderImpl* GetOrCreateForWebContents( static PaymentAppProviderImpl* GetOrCreateForWebContents(
WebContents* web_contents); WebContents* payment_request_web_contents);
// Disallow copy and assign. // Disallow copy and assign.
PaymentAppProviderImpl(const PaymentAppProviderImpl& other) = delete; PaymentAppProviderImpl(const PaymentAppProviderImpl& other) = delete;
...@@ -60,13 +60,13 @@ class CONTENT_EXPORT PaymentAppProviderImpl ...@@ -60,13 +60,13 @@ class CONTENT_EXPORT PaymentAppProviderImpl
const url::Origin& sw_origin, const url::Origin& sw_origin,
const std::string& payment_request_id, const std::string& payment_request_id,
AbortCallback callback) override; AbortCallback callback) override;
void SetOpenedWindow() override; void SetOpenedWindow(WebContents* payment_handler_web_contents) override;
void CloseOpenedWindow() override; void CloseOpenedWindow() override;
void OnClosingOpenedWindow( void OnClosingOpenedWindow(
payments::mojom::PaymentEventResponseType reason) override; payments::mojom::PaymentEventResponseType reason) override;
private: private:
explicit PaymentAppProviderImpl(WebContents* web_contents); explicit PaymentAppProviderImpl(WebContents* payment_request_web_contents);
friend class WebContentsUserData<PaymentAppProviderImpl>; friend class WebContentsUserData<PaymentAppProviderImpl>;
WEB_CONTENTS_USER_DATA_KEY_DECL(); WEB_CONTENTS_USER_DATA_KEY_DECL();
...@@ -86,14 +86,15 @@ class CONTENT_EXPORT PaymentAppProviderImpl ...@@ -86,14 +86,15 @@ class CONTENT_EXPORT PaymentAppProviderImpl
// Note that constructor of WebContentsObserver is protected. // Note that constructor of WebContentsObserver is protected.
class PaymentHandlerWindowObserver : public WebContentsObserver { class PaymentHandlerWindowObserver : public WebContentsObserver {
public: public:
explicit PaymentHandlerWindowObserver(WebContents* web_contents); explicit PaymentHandlerWindowObserver(
WebContents* payment_handler_web_contents);
~PaymentHandlerWindowObserver() override; ~PaymentHandlerWindowObserver() override;
}; };
std::unique_ptr<PaymentHandlerWindowObserver> payment_handler_window_; std::unique_ptr<PaymentHandlerWindowObserver> payment_handler_window_;
// Owns this object. // Owns this object.
WebContents* web_contents_; WebContents* payment_request_web_contents_;
// It should be accessed only on the service worker core thread. // It should be accessed only on the service worker core thread.
std::unique_ptr<ServiceWorkerCoreThreadEventDispatcher> event_dispatcher_; std::unique_ptr<ServiceWorkerCoreThreadEventDispatcher> event_dispatcher_;
......
...@@ -209,13 +209,6 @@ void DidOpenURLOnUI(WindowType type, ...@@ -209,13 +209,6 @@ void DidOpenURLOnUI(WindowType type,
new OpenURLObserver(web_contents, new OpenURLObserver(web_contents,
rfhi->frame_tree_node()->frame_tree_node_id(), rfhi->frame_tree_node()->frame_tree_node_id(),
std::move(callback)); std::move(callback));
if (type == WindowType::PAYMENT_HANDLER_WINDOW) {
// Set the opened web_contents to payment app provider to manage its life
// cycle.
PaymentAppProvider::GetOrCreateForWebContents(web_contents)
->SetOpenedWindow();
}
} }
void OpenWindowOnUI( void OpenWindowOnUI(
......
...@@ -32,7 +32,7 @@ class CONTENT_EXPORT PaymentAppProvider { ...@@ -32,7 +32,7 @@ class CONTENT_EXPORT PaymentAppProvider {
// This static function is actually implemented in PaymentAppProviderImpl.cc. // This static function is actually implemented in PaymentAppProviderImpl.cc.
// Please see: content/browser/payments/payment_app_provider_impl.cc // Please see: content/browser/payments/payment_app_provider_impl.cc
static PaymentAppProvider* GetOrCreateForWebContents( static PaymentAppProvider* GetOrCreateForWebContents(
WebContents* web_contents); WebContents* payment_request_web_contents);
using RegistrationIdCallback = using RegistrationIdCallback =
base::OnceCallback<void(int64_t registration_id)>; base::OnceCallback<void(int64_t registration_id)>;
...@@ -84,7 +84,7 @@ class CONTENT_EXPORT PaymentAppProvider { ...@@ -84,7 +84,7 @@ class CONTENT_EXPORT PaymentAppProvider {
// opened window for payment handler at any moment in a browser context. The // opened window for payment handler at any moment in a browser context. The
// previously opened window in the same browser context will be closed after // previously opened window in the same browser context will be closed after
// calling this interface. // calling this interface.
virtual void SetOpenedWindow() = 0; virtual void SetOpenedWindow(WebContents* payment_handler_web_contents) = 0;
virtual void CloseOpenedWindow() = 0; virtual void CloseOpenedWindow() = 0;
// Notify the opened payment handler window is closing or closed by user so as // Notify the opened payment handler window is closing or closed by user so as
......
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