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/budget/BudgetService.h" | 5 #include "modules/budget/BudgetService.h" |
6 | 6 |
7 #include "bindings/core/v8/ScriptPromise.h" | 7 #include "bindings/core/v8/ScriptPromise.h" |
8 #include "bindings/core/v8/ScriptPromiseResolver.h" | 8 #include "bindings/core/v8/ScriptPromiseResolver.h" |
9 #include "bindings/core/v8/ScriptState.h" | 9 #include "bindings/core/v8/ScriptState.h" |
10 #include "core/dom/DOMException.h" | 10 #include "core/dom/DOMException.h" |
11 #include "core/dom/ExceptionCode.h" | 11 #include "core/dom/ExceptionCode.h" |
| 12 #include "core/dom/ExecutionContext.h" |
12 #include "modules/budget/BudgetState.h" | 13 #include "modules/budget/BudgetState.h" |
13 #include "public/platform/InterfaceProvider.h" | 14 #include "public/platform/InterfaceProvider.h" |
14 #include "public/platform/Platform.h" | 15 #include "public/platform/Platform.h" |
15 #include "public/platform/modules/budget_service/budget_service.mojom-blink.h" | 16 #include "public/platform/modules/budget_service/budget_service.mojom-blink.h" |
16 | 17 |
17 namespace blink { | 18 namespace blink { |
18 namespace { | 19 namespace { |
19 | 20 |
20 mojom::blink::BudgetOperationType StringToOperationType( | 21 mojom::blink::BudgetOperationType StringToOperationType( |
21 const AtomicString& operation) { | 22 const AtomicString& operation) { |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
53 WTF::Bind(&BudgetService::OnConnectionError, WrapWeakPersistent(this)))); | 54 WTF::Bind(&BudgetService::OnConnectionError, WrapWeakPersistent(this)))); |
54 } | 55 } |
55 | 56 |
56 BudgetService::~BudgetService() {} | 57 BudgetService::~BudgetService() {} |
57 | 58 |
58 ScriptPromise BudgetService::getCost(ScriptState* script_state, | 59 ScriptPromise BudgetService::getCost(ScriptState* script_state, |
59 const AtomicString& operation) { | 60 const AtomicString& operation) { |
60 DCHECK(service_); | 61 DCHECK(service_); |
61 | 62 |
62 String error_message; | 63 String error_message; |
63 if (!script_state->GetExecutionContext()->IsSecureContext(error_message)) | 64 if (!ExecutionContext::From(script_state)->IsSecureContext(error_message)) |
64 return ScriptPromise::RejectWithDOMException( | 65 return ScriptPromise::RejectWithDOMException( |
65 script_state, DOMException::Create(kSecurityError, error_message)); | 66 script_state, DOMException::Create(kSecurityError, error_message)); |
66 | 67 |
67 mojom::blink::BudgetOperationType type = StringToOperationType(operation); | 68 mojom::blink::BudgetOperationType type = StringToOperationType(operation); |
68 DCHECK_NE(type, mojom::blink::BudgetOperationType::INVALID_OPERATION); | 69 DCHECK_NE(type, mojom::blink::BudgetOperationType::INVALID_OPERATION); |
69 | 70 |
70 ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state); | 71 ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state); |
71 ScriptPromise promise = resolver->Promise(); | 72 ScriptPromise promise = resolver->Promise(); |
72 | 73 |
73 // Get the cost for the action from the browser BudgetService. | 74 // Get the cost for the action from the browser BudgetService. |
74 service_->GetCost(type, ConvertToBaseCallback(WTF::Bind( | 75 service_->GetCost(type, ConvertToBaseCallback(WTF::Bind( |
75 &BudgetService::GotCost, WrapPersistent(this), | 76 &BudgetService::GotCost, WrapPersistent(this), |
76 WrapPersistent(resolver)))); | 77 WrapPersistent(resolver)))); |
77 return promise; | 78 return promise; |
78 } | 79 } |
79 | 80 |
80 void BudgetService::GotCost(ScriptPromiseResolver* resolver, | 81 void BudgetService::GotCost(ScriptPromiseResolver* resolver, |
81 double cost) const { | 82 double cost) const { |
82 resolver->Resolve(cost); | 83 resolver->Resolve(cost); |
83 } | 84 } |
84 | 85 |
85 ScriptPromise BudgetService::getBudget(ScriptState* script_state) { | 86 ScriptPromise BudgetService::getBudget(ScriptState* script_state) { |
86 DCHECK(service_); | 87 DCHECK(service_); |
87 | 88 |
88 String error_message; | 89 String error_message; |
89 if (!script_state->GetExecutionContext()->IsSecureContext(error_message)) | 90 if (!ExecutionContext::From(script_state)->IsSecureContext(error_message)) |
90 return ScriptPromise::RejectWithDOMException( | 91 return ScriptPromise::RejectWithDOMException( |
91 script_state, DOMException::Create(kSecurityError, error_message)); | 92 script_state, DOMException::Create(kSecurityError, error_message)); |
92 | 93 |
93 ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state); | 94 ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state); |
94 ScriptPromise promise = resolver->Promise(); | 95 ScriptPromise promise = resolver->Promise(); |
95 | 96 |
96 // Get the budget from the browser BudgetService. | 97 // Get the budget from the browser BudgetService. |
97 RefPtr<SecurityOrigin> origin( | 98 RefPtr<SecurityOrigin> origin( |
98 script_state->GetExecutionContext()->GetSecurityOrigin()); | 99 ExecutionContext::From(script_state)->GetSecurityOrigin()); |
99 service_->GetBudget( | 100 service_->GetBudget( |
100 origin, ConvertToBaseCallback(WTF::Bind(&BudgetService::GotBudget, | 101 origin, ConvertToBaseCallback(WTF::Bind(&BudgetService::GotBudget, |
101 WrapPersistent(this), | 102 WrapPersistent(this), |
102 WrapPersistent(resolver)))); | 103 WrapPersistent(resolver)))); |
103 return promise; | 104 return promise; |
104 } | 105 } |
105 | 106 |
106 void BudgetService::GotBudget( | 107 void BudgetService::GotBudget( |
107 ScriptPromiseResolver* resolver, | 108 ScriptPromiseResolver* resolver, |
108 mojom::blink::BudgetServiceErrorType error, | 109 mojom::blink::BudgetServiceErrorType error, |
(...skipping 16 matching lines...) Expand all Loading... |
125 } | 126 } |
126 | 127 |
127 ScriptPromise BudgetService::reserve(ScriptState* script_state, | 128 ScriptPromise BudgetService::reserve(ScriptState* script_state, |
128 const AtomicString& operation) { | 129 const AtomicString& operation) { |
129 DCHECK(service_); | 130 DCHECK(service_); |
130 | 131 |
131 mojom::blink::BudgetOperationType type = StringToOperationType(operation); | 132 mojom::blink::BudgetOperationType type = StringToOperationType(operation); |
132 DCHECK_NE(type, mojom::blink::BudgetOperationType::INVALID_OPERATION); | 133 DCHECK_NE(type, mojom::blink::BudgetOperationType::INVALID_OPERATION); |
133 | 134 |
134 String error_message; | 135 String error_message; |
135 if (!script_state->GetExecutionContext()->IsSecureContext(error_message)) | 136 if (!ExecutionContext::From(script_state)->IsSecureContext(error_message)) |
136 return ScriptPromise::RejectWithDOMException( | 137 return ScriptPromise::RejectWithDOMException( |
137 script_state, DOMException::Create(kSecurityError, error_message)); | 138 script_state, DOMException::Create(kSecurityError, error_message)); |
138 | 139 |
139 ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state); | 140 ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state); |
140 ScriptPromise promise = resolver->Promise(); | 141 ScriptPromise promise = resolver->Promise(); |
141 | 142 |
142 // Call to the BudgetService to place the reservation. | 143 // Call to the BudgetService to place the reservation. |
143 RefPtr<SecurityOrigin> origin( | 144 RefPtr<SecurityOrigin> origin( |
144 script_state->GetExecutionContext()->GetSecurityOrigin()); | 145 ExecutionContext::From(script_state)->GetSecurityOrigin()); |
145 service_->Reserve(origin, type, | 146 service_->Reserve(origin, type, |
146 ConvertToBaseCallback(WTF::Bind( | 147 ConvertToBaseCallback(WTF::Bind( |
147 &BudgetService::GotReservation, WrapPersistent(this), | 148 &BudgetService::GotReservation, WrapPersistent(this), |
148 WrapPersistent(resolver)))); | 149 WrapPersistent(resolver)))); |
149 return promise; | 150 return promise; |
150 } | 151 } |
151 | 152 |
152 void BudgetService::GotReservation(ScriptPromiseResolver* resolver, | 153 void BudgetService::GotReservation(ScriptPromiseResolver* resolver, |
153 mojom::blink::BudgetServiceErrorType error, | 154 mojom::blink::BudgetServiceErrorType error, |
154 bool success) const { | 155 bool success) const { |
155 if (error != mojom::blink::BudgetServiceErrorType::NONE) { | 156 if (error != mojom::blink::BudgetServiceErrorType::NONE) { |
156 resolver->Reject(ErrorTypeToException(error)); | 157 resolver->Reject(ErrorTypeToException(error)); |
157 return; | 158 return; |
158 } | 159 } |
159 | 160 |
160 resolver->Resolve(success); | 161 resolver->Resolve(success); |
161 } | 162 } |
162 | 163 |
163 void BudgetService::OnConnectionError() { | 164 void BudgetService::OnConnectionError() { |
164 LOG(ERROR) << "Unable to connect to the Mojo BudgetService."; | 165 LOG(ERROR) << "Unable to connect to the Mojo BudgetService."; |
165 // TODO(harkness): Reject in flight promises. | 166 // TODO(harkness): Reject in flight promises. |
166 } | 167 } |
167 | 168 |
168 } // namespace blink | 169 } // namespace blink |
OLD | NEW |