| 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 "modules/budget/BudgetState.h" | 12 #include "modules/budget/BudgetState.h" |
| 13 #include "public/platform/InterfaceProvider.h" | 13 #include "public/platform/InterfaceProvider.h" |
| 14 #include "public/platform/Platform.h" | 14 #include "public/platform/Platform.h" |
| 15 #include "public/platform/modules/budget_service/budget_service.mojom-blink.h" | 15 #include "public/platform/modules/budget_service/budget_service.mojom-blink.h" |
| 16 | 16 |
| 17 namespace blink { | 17 namespace blink { |
| 18 namespace { |
| 19 mojom::blink::BudgetOperationType stringToOperationType(const AtomicString&
operation) |
| 20 { |
| 21 if (operation == "silent-push") |
| 22 return mojom::blink::BudgetOperationType::SILENT_PUSH; |
| 23 |
| 24 return mojom::blink::BudgetOperationType::INVALID_OPERATION; |
| 25 } |
| 26 |
| 27 DOMException* errorTypeToException(mojom::blink::BudgetServiceErrorType erro
r) |
| 28 { |
| 29 switch (error) { |
| 30 case mojom::blink::BudgetServiceErrorType::NO_ERROR: |
| 31 return nullptr; |
| 32 case mojom::blink::BudgetServiceErrorType::DATABASE_ERROR: |
| 33 return DOMException::create(DataError, "Error reading the budget dat
abase."); |
| 34 case mojom::blink::BudgetServiceErrorType::NOT_SUPPORTED: |
| 35 return DOMException::create(NotSupportedError, "Requested opration w
as not supported"); |
| 36 } |
| 37 NOTREACHED(); |
| 38 return nullptr; |
| 39 } |
| 40 |
| 41 } // namespace |
| 18 | 42 |
| 19 BudgetService::BudgetService() | 43 BudgetService::BudgetService() |
| 20 { | 44 { |
| 21 Platform::current()->interfaceProvider()->getInterface(mojo::GetProxy(&m_ser
vice)); | 45 Platform::current()->interfaceProvider()->getInterface(mojo::GetProxy(&m_ser
vice)); |
| 22 | 46 |
| 23 // Set a connection error handler, so that if an embedder doesn't | 47 // Set a connection error handler, so that if an embedder doesn't |
| 24 // implement a BudgetSerice mojo service, the developer will get a | 48 // implement a BudgetSerice mojo service, the developer will get a |
| 25 // actionable information. | 49 // actionable information. |
| 26 m_service.set_connection_error_handler(convertToBaseCallback(WTF::bind(&Budg
etService::onConnectionError, wrapWeakPersistent(this)))); | 50 m_service.set_connection_error_handler(convertToBaseCallback(WTF::bind(&Budg
etService::onConnectionError, wrapWeakPersistent(this)))); |
| 27 } | 51 } |
| 28 | 52 |
| 29 BudgetService::~BudgetService() | 53 BudgetService::~BudgetService() |
| 30 { | 54 { |
| 31 } | 55 } |
| 32 | 56 |
| 33 ScriptPromise BudgetService::getCost(ScriptState* scriptState, const AtomicStrin
g& /* actionType */) | 57 ScriptPromise BudgetService::getCost(ScriptState* scriptState, const AtomicStrin
g& operation) |
| 34 { | 58 { |
| 35 DCHECK(m_service); | 59 DCHECK(m_service); |
| 36 | 60 |
| 61 mojom::blink::BudgetOperationType type = stringToOperationType(operation); |
| 62 if (type == mojom::blink::BudgetOperationType::INVALID_OPERATION) |
| 63 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(NotSupportedError, "Invalid operation type specified")); |
| 64 |
| 37 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; | 65 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; |
| 38 ScriptPromise promise = resolver->promise(); | 66 ScriptPromise promise = resolver->promise(); |
| 39 | 67 |
| 40 // TODO(harkness): Map the actionType to BudgetOperationType. | |
| 41 // Get the cost for the action from the browser BudgetService. | 68 // Get the cost for the action from the browser BudgetService. |
| 42 mojom::blink::BudgetOperationType type = mojom::blink::BudgetOperationType::
SILENT_PUSH; | |
| 43 m_service->GetCost(type, convertToBaseCallback(WTF::bind(&BudgetService::got
Cost, wrapPersistent(this), wrapPersistent(resolver)))); | 69 m_service->GetCost(type, convertToBaseCallback(WTF::bind(&BudgetService::got
Cost, wrapPersistent(this), wrapPersistent(resolver)))); |
| 44 return promise; | 70 return promise; |
| 45 } | 71 } |
| 46 | 72 |
| 47 void BudgetService::gotCost(ScriptPromiseResolver* resolver, double cost) const | 73 void BudgetService::gotCost(ScriptPromiseResolver* resolver, double cost) const |
| 48 { | 74 { |
| 49 resolver->resolve(cost); | 75 resolver->resolve(cost); |
| 50 } | 76 } |
| 51 | 77 |
| 52 ScriptPromise BudgetService::getBudget(ScriptState* scriptState) | 78 ScriptPromise BudgetService::getBudget(ScriptState* scriptState) |
| 53 { | 79 { |
| 54 DCHECK(m_service); | 80 DCHECK(m_service); |
| 55 | 81 |
| 56 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; | 82 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; |
| 57 ScriptPromise promise = resolver->promise(); | 83 ScriptPromise promise = resolver->promise(); |
| 58 | 84 |
| 59 ExecutionContext* context = scriptState->getExecutionContext(); | 85 ExecutionContext* context = scriptState->getExecutionContext(); |
| 60 if (!context) | 86 if (!context) |
| 61 return promise; | 87 return promise; |
| 62 | 88 |
| 63 // Get the budget from the browser BudgetService. | 89 // Get the budget from the browser BudgetService. |
| 64 RefPtr<SecurityOrigin> origin(context->getSecurityOrigin()); | 90 RefPtr<SecurityOrigin> origin(context->getSecurityOrigin()); |
| 91 // TODO(harkness): Check that this is a valid secure origin. |
| 65 m_service->GetBudget(origin, convertToBaseCallback(WTF::bind(&BudgetService:
:gotBudget, wrapPersistent(this), wrapPersistent(resolver)))); | 92 m_service->GetBudget(origin, convertToBaseCallback(WTF::bind(&BudgetService:
:gotBudget, wrapPersistent(this), wrapPersistent(resolver)))); |
| 66 return promise; | 93 return promise; |
| 67 } | 94 } |
| 68 | 95 |
| 69 void BudgetService::gotBudget(ScriptPromiseResolver* resolver, const mojo::WTFAr
ray<mojom::blink::BudgetStatePtr> expectations) const | 96 void BudgetService::gotBudget(ScriptPromiseResolver* resolver, mojom::blink::Bud
getServiceErrorType error, const mojo::WTFArray<mojom::blink::BudgetStatePtr> ex
pectations) const |
| 70 { | 97 { |
| 98 if (error != mojom::blink::BudgetServiceErrorType::NO_ERROR) { |
| 99 resolver->reject(errorTypeToException(error)); |
| 100 return; |
| 101 } |
| 102 |
| 71 // Copy the chunks into the budget array. | 103 // Copy the chunks into the budget array. |
| 72 HeapVector<Member<BudgetState>> budget(expectations.size()); | 104 HeapVector<Member<BudgetState>> budget(expectations.size()); |
| 73 for (size_t i = 0; i < expectations.size(); i++) | 105 for (size_t i = 0; i < expectations.size(); i++) |
| 74 budget[i] = new BudgetState(expectations[i]->budget_at, expectations[i]-
>time); | 106 budget[i] = new BudgetState(expectations[i]->budget_at, expectations[i]-
>time); |
| 75 | 107 |
| 76 resolver->resolve(budget); | 108 resolver->resolve(budget); |
| 77 } | 109 } |
| 78 | 110 |
| 111 ScriptPromise BudgetService::reserve(ScriptState* scriptState, const AtomicStrin
g& operation) |
| 112 { |
| 113 DCHECK(m_service); |
| 114 |
| 115 mojom::blink::BudgetOperationType type = stringToOperationType(operation); |
| 116 if (type == mojom::blink::BudgetOperationType::INVALID_OPERATION) |
| 117 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(NotSupportedError, "Invalid operation type specified")); |
| 118 |
| 119 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; |
| 120 ScriptPromise promise = resolver->promise(); |
| 121 |
| 122 ExecutionContext* context = scriptState->getExecutionContext(); |
| 123 if (!context) |
| 124 return promise; |
| 125 |
| 126 // Call to the BudgetService to place the reservation. |
| 127 RefPtr<SecurityOrigin> origin(context->getSecurityOrigin()); |
| 128 // TODO(harkness): Check that this is a valid secure origin. |
| 129 m_service->Reserve(origin, type, convertToBaseCallback(WTF::bind(&BudgetServ
ice::gotReservation, wrapPersistent(this), wrapPersistent(resolver)))); |
| 130 return promise; |
| 131 } |
| 132 |
| 133 void BudgetService::gotReservation(ScriptPromiseResolver* resolver, mojom::blink
::BudgetServiceErrorType error, bool success) const |
| 134 { |
| 135 if (error != mojom::blink::BudgetServiceErrorType::NO_ERROR) { |
| 136 resolver->reject(errorTypeToException(error)); |
| 137 return; |
| 138 } |
| 139 |
| 140 resolver->resolve(success); |
| 141 } |
| 142 |
| 79 void BudgetService::onConnectionError() | 143 void BudgetService::onConnectionError() |
| 80 { | 144 { |
| 81 LOG(ERROR) << "Unable to connect to the Mojo BudgetService."; | 145 LOG(ERROR) << "Unable to connect to the Mojo BudgetService."; |
| 82 // TODO(harkness): Reject in flight promises. | 146 // TODO(harkness): Reject in flight promises. |
| 83 } | 147 } |
| 84 | 148 |
| 85 } // namespace blink | 149 } // namespace blink |
| OLD | NEW |