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" |
| 13 #include "public/platform/InterfaceProvider.h" |
| 14 #include "public/platform/Platform.h" |
| 15 #include "public/platform/modules/budget_service/budget_service.mojom-blink.h" |
12 | 16 |
13 namespace blink { | 17 namespace blink { |
14 | 18 |
15 BudgetService::BudgetService() {} | 19 BudgetService::BudgetService() |
| 20 { |
| 21 Platform::current()->interfaceProvider()->getInterface(mojo::GetProxy(&m_ser
vice)); |
16 | 22 |
17 ScriptPromise BudgetService::getCost(ScriptState* scriptState, const AtomicStrin
g& actionType) | 23 // Set a connection error handler, so that if an embedder doesn't |
| 24 // implement a BudgetSerice mojo service, the developer will get a |
| 25 // actionable information. |
| 26 m_service.set_connection_error_handler(convertToBaseCallback(WTF::bind(&Budg
etService::onConnectionError, wrapWeakPersistent(this)))); |
| 27 } |
| 28 |
| 29 BudgetService::~BudgetService() |
18 { | 30 { |
19 // TODO(harkness): Trigger the cost calculation. | 31 } |
20 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::crea
te(NotSupportedError, "Not yet implemented")); | 32 |
| 33 ScriptPromise BudgetService::getCost(ScriptState* scriptState, const AtomicStrin
g& /* actionType */) |
| 34 { |
| 35 DCHECK(m_service); |
| 36 |
| 37 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; |
| 38 ScriptPromise promise = resolver->promise(); |
| 39 |
| 40 // TODO(harkness): Map the actionType to BudgetOperationType. |
| 41 // 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)))); |
| 44 return promise; |
| 45 } |
| 46 |
| 47 void BudgetService::gotCost(ScriptPromiseResolver* resolver, double cost) const |
| 48 { |
| 49 resolver->resolve(cost); |
21 } | 50 } |
22 | 51 |
23 ScriptPromise BudgetService::getBudget(ScriptState* scriptState) | 52 ScriptPromise BudgetService::getBudget(ScriptState* scriptState) |
24 { | 53 { |
25 // TODO(harkness): Trigger the budget calculation. | 54 DCHECK(m_service); |
26 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::crea
te(NotSupportedError, "Not yet implemented")); | 55 |
| 56 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; |
| 57 ScriptPromise promise = resolver->promise(); |
| 58 |
| 59 ExecutionContext* context = scriptState->getExecutionContext(); |
| 60 if (!context) |
| 61 return promise; |
| 62 |
| 63 // Get the budget from the browser BudgetService. |
| 64 RefPtr<SecurityOrigin> origin(context->getSecurityOrigin()); |
| 65 m_service->GetBudget(origin, convertToBaseCallback(WTF::bind(&BudgetService:
:gotBudget, wrapPersistent(this), wrapPersistent(resolver)))); |
| 66 return promise; |
| 67 } |
| 68 |
| 69 void BudgetService::gotBudget(ScriptPromiseResolver* resolver, const mojo::WTFAr
ray<mojom::blink::BudgetStatePtr> expectations) const |
| 70 { |
| 71 // Copy the chunks into the budget array. |
| 72 HeapVector<Member<BudgetState>> budget(expectations.size()); |
| 73 for (size_t i = 0; i < expectations.size(); i++) |
| 74 budget[i] = new BudgetState(expectations[i]->budget_at, expectations[i]-
>time); |
| 75 |
| 76 resolver->resolve(budget); |
| 77 } |
| 78 |
| 79 void BudgetService::onConnectionError() |
| 80 { |
| 81 LOG(ERROR) << "Unable to connect to the Mojo BudgetService."; |
| 82 // TODO(harkness): Reject in flight promises. |
27 } | 83 } |
28 | 84 |
29 } // namespace blink | 85 } // namespace blink |
OLD | NEW |