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/PaymentAppServiceWorkerRegistration.h" | 5 #include "modules/payments/PaymentAppServiceWorkerRegistration.h" |
6 | 6 |
| 7 #include "bindings/core/v8/ExceptionState.h" |
7 #include "core/dom/Document.h" | 8 #include "core/dom/Document.h" |
| 9 #include "core/dom/ExecutionContext.h" |
8 #include "modules/payments/PaymentManager.h" | 10 #include "modules/payments/PaymentManager.h" |
9 #include "modules/serviceworkers/ServiceWorkerRegistration.h" | 11 #include "modules/serviceworkers/ServiceWorkerRegistration.h" |
10 #include "platform/bindings/ScriptState.h" | 12 #include "platform/bindings/ScriptState.h" |
| 13 #include "platform/weborigin/KURL.h" |
| 14 #include "platform/weborigin/SecurityOrigin.h" |
11 | 15 |
12 namespace blink { | 16 namespace blink { |
13 | 17 |
14 PaymentAppServiceWorkerRegistration::~PaymentAppServiceWorkerRegistration() {} | 18 PaymentAppServiceWorkerRegistration::~PaymentAppServiceWorkerRegistration() {} |
15 | 19 |
16 // static | 20 // static |
17 PaymentAppServiceWorkerRegistration& PaymentAppServiceWorkerRegistration::From( | 21 PaymentAppServiceWorkerRegistration& PaymentAppServiceWorkerRegistration::From( |
18 ServiceWorkerRegistration& registration) { | 22 ServiceWorkerRegistration& registration) { |
19 PaymentAppServiceWorkerRegistration* supplement = | 23 PaymentAppServiceWorkerRegistration* supplement = |
20 static_cast<PaymentAppServiceWorkerRegistration*>( | 24 static_cast<PaymentAppServiceWorkerRegistration*>( |
21 Supplement<ServiceWorkerRegistration>::From(registration, | 25 Supplement<ServiceWorkerRegistration>::From(registration, |
22 SupplementName())); | 26 SupplementName())); |
23 | 27 |
24 if (!supplement) { | 28 if (!supplement) { |
25 supplement = new PaymentAppServiceWorkerRegistration(®istration); | 29 supplement = new PaymentAppServiceWorkerRegistration(®istration); |
26 ProvideTo(registration, SupplementName(), supplement); | 30 ProvideTo(registration, SupplementName(), supplement); |
27 } | 31 } |
28 | 32 |
29 return *supplement; | 33 return *supplement; |
30 } | 34 } |
31 | 35 |
32 // static | 36 // static |
33 PaymentManager* PaymentAppServiceWorkerRegistration::paymentManager( | 37 PaymentManager* PaymentAppServiceWorkerRegistration::paymentManager( |
34 ScriptState* script_state, | 38 ScriptState* script_state, |
35 ServiceWorkerRegistration& registration) { | 39 ServiceWorkerRegistration& registration, |
| 40 ExceptionState& exception_state) { |
36 return PaymentAppServiceWorkerRegistration::From(registration) | 41 return PaymentAppServiceWorkerRegistration::From(registration) |
37 .paymentManager(script_state); | 42 .paymentManager(script_state, exception_state); |
38 } | 43 } |
39 | 44 |
40 PaymentManager* PaymentAppServiceWorkerRegistration::paymentManager( | 45 PaymentManager* PaymentAppServiceWorkerRegistration::paymentManager( |
41 ScriptState* script_state) { | 46 ScriptState* script_state, |
| 47 ExceptionState& exception_state) { |
| 48 ExecutionContext* context = ExecutionContext::From(script_state); |
| 49 KURL origin = KURL(KURL(), context->GetSecurityOrigin()->ToString()); |
| 50 |
| 51 // The PaymentManager can be used only in SecureContext but we don't want to |
| 52 // enable the feature in Extension side. So, we additionally need the follwing |
| 53 // check whether the origin is HTTPS or not. |
| 54 if (!origin.ProtocolIsInHTTPFamily()) { |
| 55 exception_state.ThrowSecurityError( |
| 56 "PaymentManger can be used only in HTTPS scheme."); |
| 57 return nullptr; |
| 58 } |
| 59 |
42 if (!payment_manager_) { | 60 if (!payment_manager_) { |
43 payment_manager_ = PaymentManager::Create(registration_); | 61 payment_manager_ = PaymentManager::Create(registration_); |
44 } | 62 } |
45 return payment_manager_.Get(); | 63 return payment_manager_.Get(); |
46 } | 64 } |
47 | 65 |
48 DEFINE_TRACE(PaymentAppServiceWorkerRegistration) { | 66 DEFINE_TRACE(PaymentAppServiceWorkerRegistration) { |
49 visitor->Trace(registration_); | 67 visitor->Trace(registration_); |
50 visitor->Trace(payment_manager_); | 68 visitor->Trace(payment_manager_); |
51 Supplement<ServiceWorkerRegistration>::Trace(visitor); | 69 Supplement<ServiceWorkerRegistration>::Trace(visitor); |
52 } | 70 } |
53 | 71 |
54 PaymentAppServiceWorkerRegistration::PaymentAppServiceWorkerRegistration( | 72 PaymentAppServiceWorkerRegistration::PaymentAppServiceWorkerRegistration( |
55 ServiceWorkerRegistration* registration) | 73 ServiceWorkerRegistration* registration) |
56 : registration_(registration) {} | 74 : registration_(registration) {} |
57 | 75 |
58 // static | 76 // static |
59 const char* PaymentAppServiceWorkerRegistration::SupplementName() { | 77 const char* PaymentAppServiceWorkerRegistration::SupplementName() { |
60 return "PaymentAppServiceWorkerRegistration"; | 78 return "PaymentAppServiceWorkerRegistration"; |
61 } | 79 } |
62 | 80 |
63 } // namespace blink | 81 } // namespace blink |
OLD | NEW |