OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "modules/payments/PaymentRequestEvent.h" | 5 #include "modules/payments/PaymentRequestEvent.h" |
6 | 6 |
| 7 #include "bindings/core/v8/ScriptPromiseResolver.h" |
| 8 #include "core/dom/DOMException.h" |
| 9 #include "core/workers/WorkerGlobalScope.h" |
| 10 #include "core/workers/WorkerLocation.h" |
7 #include "modules/serviceworkers/RespondWithObserver.h" | 11 #include "modules/serviceworkers/RespondWithObserver.h" |
| 12 #include "modules/serviceworkers/ServiceWorkerGlobalScopeClient.h" |
| 13 #include "modules/serviceworkers/ServiceWorkerWindowClientCallback.h" |
| 14 #include "platform/wtf/PtrUtil.h" |
8 #include "platform/wtf/text/AtomicString.h" | 15 #include "platform/wtf/text/AtomicString.h" |
9 | 16 |
10 namespace blink { | 17 namespace blink { |
11 | 18 |
12 PaymentRequestEvent* PaymentRequestEvent::Create( | 19 PaymentRequestEvent* PaymentRequestEvent::Create( |
13 const AtomicString& type, | 20 const AtomicString& type, |
14 const PaymentAppRequest& app_request, | 21 const PaymentAppRequest& app_request, |
15 RespondWithObserver* respond_with_observer, | 22 RespondWithObserver* respond_with_observer, |
16 WaitUntilObserver* wait_until_observer) { | 23 WaitUntilObserver* wait_until_observer) { |
17 return new PaymentRequestEvent(type, app_request, respond_with_observer, | 24 return new PaymentRequestEvent(type, app_request, respond_with_observer, |
(...skipping 28 matching lines...) Expand all Loading... |
46 | 53 |
47 const HeapVector<PaymentDetailsModifier>& PaymentRequestEvent::modifiers() | 54 const HeapVector<PaymentDetailsModifier>& PaymentRequestEvent::modifiers() |
48 const { | 55 const { |
49 return modifiers_; | 56 return modifiers_; |
50 } | 57 } |
51 | 58 |
52 const String& PaymentRequestEvent::instrumentKey() const { | 59 const String& PaymentRequestEvent::instrumentKey() const { |
53 return instrument_key_; | 60 return instrument_key_; |
54 } | 61 } |
55 | 62 |
| 63 ScriptPromise PaymentRequestEvent::openWindow(ScriptState* script_state, |
| 64 const String& url) { |
| 65 ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state); |
| 66 ScriptPromise promise = resolver->Promise(); |
| 67 ExecutionContext* context = ExecutionContext::From(script_state); |
| 68 |
| 69 // TODO(gogerald): Check payment request state so as to reject promise with |
| 70 // "InvalidStateError" appropriately (refer |
| 71 // https://w3c.github.io/payment-handler/#dfn-open-window-algorithm). |
| 72 |
| 73 KURL parsed_url_to_open = context->CompleteURL(url); |
| 74 if (!parsed_url_to_open.IsValid()) { |
| 75 resolver->Reject(V8ThrowException::CreateTypeError( |
| 76 script_state->GetIsolate(), "'" + url + "' is not a valid URL.")); |
| 77 return promise; |
| 78 } |
| 79 |
| 80 // TODO(gogerald): Once the issue of the spec is resolved, we should apply the |
| 81 // changes. Refer https://github.com/w3c/payment-handler/issues/168. |
| 82 if (!context->GetSecurityOrigin()->IsSameSchemeHostPortAndSuborigin( |
| 83 SecurityOrigin::Create(parsed_url_to_open).Get())) { |
| 84 resolver->Reject(DOMException::Create( |
| 85 kSecurityError, |
| 86 "'" + parsed_url_to_open.ElidedString() + "' is not allowed.")); |
| 87 return promise; |
| 88 } |
| 89 |
| 90 // TODO(gogerald): Once the issue of the spec is resolved, we should apply the |
| 91 // changes. Refer https://github.com/w3c/payment-handler/issues/169. |
| 92 if (!context->IsWindowInteractionAllowed()) { |
| 93 resolver->Reject(DOMException::Create(kInvalidAccessError, |
| 94 "Not allowed to open a window.")); |
| 95 return promise; |
| 96 } |
| 97 context->ConsumeWindowInteraction(); |
| 98 |
| 99 ServiceWorkerGlobalScopeClient::From(context)->OpenWindowForPaymentHandler( |
| 100 parsed_url_to_open, WTF::MakeUnique<NavigateClientCallback>(resolver)); |
| 101 return promise; |
| 102 } |
| 103 |
56 void PaymentRequestEvent::respondWith(ScriptState* script_state, | 104 void PaymentRequestEvent::respondWith(ScriptState* script_state, |
57 ScriptPromise script_promise, | 105 ScriptPromise script_promise, |
58 ExceptionState& exception_state) { | 106 ExceptionState& exception_state) { |
59 stopImmediatePropagation(); | 107 stopImmediatePropagation(); |
60 if (observer_) { | 108 if (observer_) { |
61 observer_->RespondWith(script_state, script_promise, exception_state); | 109 observer_->RespondWith(script_state, script_promise, exception_state); |
62 } | 110 } |
63 } | 111 } |
64 | 112 |
65 DEFINE_TRACE(PaymentRequestEvent) { | 113 DEFINE_TRACE(PaymentRequestEvent) { |
(...skipping 12 matching lines...) Expand all Loading... |
78 top_level_origin_(app_request.topLevelOrigin()), | 126 top_level_origin_(app_request.topLevelOrigin()), |
79 payment_request_origin_(app_request.paymentRequestOrigin()), | 127 payment_request_origin_(app_request.paymentRequestOrigin()), |
80 payment_request_id_(app_request.paymentRequestId()), | 128 payment_request_id_(app_request.paymentRequestId()), |
81 method_data_(std::move(app_request.methodData())), | 129 method_data_(std::move(app_request.methodData())), |
82 total_(app_request.total()), | 130 total_(app_request.total()), |
83 modifiers_(app_request.modifiers()), | 131 modifiers_(app_request.modifiers()), |
84 instrument_key_(app_request.instrumentKey()), | 132 instrument_key_(app_request.instrumentKey()), |
85 observer_(respond_with_observer) {} | 133 observer_(respond_with_observer) {} |
86 | 134 |
87 } // namespace blink | 135 } // namespace blink |
OLD | NEW |