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