| 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" |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 return ScriptPromise::rejectWithDOMException( | 64 return ScriptPromise::rejectWithDOMException( |
| 65 scriptState, DOMException::create(SecurityError, errorMessage)); | 65 scriptState, DOMException::create(SecurityError, errorMessage)); |
| 66 | 66 |
| 67 mojom::blink::BudgetOperationType type = stringToOperationType(operation); | 67 mojom::blink::BudgetOperationType type = stringToOperationType(operation); |
| 68 DCHECK_NE(type, mojom::blink::BudgetOperationType::INVALID_OPERATION); | 68 DCHECK_NE(type, mojom::blink::BudgetOperationType::INVALID_OPERATION); |
| 69 | 69 |
| 70 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); | 70 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
| 71 ScriptPromise promise = resolver->promise(); | 71 ScriptPromise promise = resolver->promise(); |
| 72 | 72 |
| 73 // Get the cost for the action from the browser BudgetService. | 73 // Get the cost for the action from the browser BudgetService. |
| 74 m_service->GetCost(type, convertToBaseCallback(WTF::bind( | 74 m_service->GetCost(type, |
| 75 &BudgetService::gotCost, wrapPersistent(this), | 75 convertToBaseCallback(WTF::bind( |
| 76 wrapPersistent(resolver)))); | 76 &BudgetService::gotCost, wrapPersistent(this), |
| 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* scriptState) { | 86 ScriptPromise BudgetService::getBudget(ScriptState* scriptState) { |
| 86 DCHECK(m_service); | 87 DCHECK(m_service); |
| 87 | 88 |
| 88 String errorMessage; | 89 String errorMessage; |
| 89 if (!scriptState->getExecutionContext()->isSecureContext(errorMessage)) | 90 if (!scriptState->getExecutionContext()->isSecureContext(errorMessage)) |
| 90 return ScriptPromise::rejectWithDOMException( | 91 return ScriptPromise::rejectWithDOMException( |
| 91 scriptState, DOMException::create(SecurityError, errorMessage)); | 92 scriptState, DOMException::create(SecurityError, errorMessage)); |
| 92 | 93 |
| 93 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); | 94 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
| 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 scriptState->getExecutionContext()->getSecurityOrigin()); | 99 scriptState->getExecutionContext()->getSecurityOrigin()); |
| 99 m_service->GetBudget( | 100 m_service->GetBudget(origin, |
| 100 origin, convertToBaseCallback(WTF::bind(&BudgetService::gotBudget, | 101 convertToBaseCallback(WTF::bind( |
| 101 wrapPersistent(this), | 102 &BudgetService::gotBudget, 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, |
| 109 const WTF::Vector<mojom::blink::BudgetStatePtr> expectations) const { | 110 const WTF::Vector<mojom::blink::BudgetStatePtr> expectations) const { |
| 110 if (error != mojom::blink::BudgetServiceErrorType::NONE) { | 111 if (error != mojom::blink::BudgetServiceErrorType::NONE) { |
| 111 resolver->reject(errorTypeToException(error)); | 112 resolver->reject(errorTypeToException(error)); |
| 112 return; | 113 return; |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 |