Chromium Code Reviews| Index: third_party/WebKit/Source/modules/budget/BudgetService.cpp |
| diff --git a/third_party/WebKit/Source/modules/budget/BudgetService.cpp b/third_party/WebKit/Source/modules/budget/BudgetService.cpp |
| index 52ae315ddafb453f58e4b13fade28daa1d8c6a25..099d7aad09060e531cefd7e47e43e2a5083eef88 100644 |
| --- a/third_party/WebKit/Source/modules/budget/BudgetService.cpp |
| +++ b/third_party/WebKit/Source/modules/budget/BudgetService.cpp |
| @@ -9,21 +9,59 @@ |
| #include "bindings/core/v8/ScriptState.h" |
| #include "core/dom/DOMException.h" |
| #include "core/dom/ExceptionCode.h" |
| +#include "modules/budget/BudgetState.h" |
| +#include "public/platform/InterfaceProvider.h" |
| +#include "public/platform/Platform.h" |
| +#include "public/platform/modules/budget_service/budget_service.mojom-blink.h" |
| namespace blink { |
| -BudgetService::BudgetService() {} |
| +BudgetService::BudgetService() |
| +{ |
| + Platform::current()->interfaceProvider()->getInterface(mojo::GetProxy(&m_service)); |
| + DCHECK(m_service); |
|
Peter Beverloo
2016/08/22 17:57:55
I don't think this DCHECK is valid— when getting t
harkness
2016/08/23 09:38:18
Done. Added the check to getCost and getBudget ins
|
| +} |
| + |
| +BudgetService::~BudgetService() |
| +{ |
| +} |
| + |
| +ScriptPromise BudgetService::getCost(ScriptState* scriptState, const AtomicString& /* actionType */) |
| +{ |
| + ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
| + ScriptPromise promise = resolver->promise(); |
| -ScriptPromise BudgetService::getCost(ScriptState* scriptState, const AtomicString& actionType) |
| + // TODO(harkness): Map the actionType to BudgetOperationType. |
| + // Get the cost for the action from the browser BudgetService. |
| + mojom::blink::BudgetOperationType type = mojom::blink::BudgetOperationType::SILENT_PUSH; |
| + m_service->GetCost(type, convertToBaseCallback(WTF::bind(&BudgetService::gotCost, wrapPersistent(this), wrapPersistent(resolver)))); |
| + return promise; |
| +} |
| + |
| +void BudgetService::gotCost(ScriptPromiseResolver* resolver, double cost) const |
| { |
| - // TODO(harkness): Trigger the cost calculation. |
| - return ScriptPromise::rejectWithDOMException(scriptState, DOMException::create(NotSupportedError, "Not yet implemented")); |
| + resolver->resolve(cost); |
| } |
| ScriptPromise BudgetService::getBudget(ScriptState* scriptState) |
| { |
| - // TODO(harkness): Trigger the budget calculation. |
| - return ScriptPromise::rejectWithDOMException(scriptState, DOMException::create(NotSupportedError, "Not yet implemented")); |
| + ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
| + ScriptPromise promise = resolver->promise(); |
| + |
| + // Get the budget from the browser BudgetService. |
| + RefPtr<SecurityOrigin> origin(scriptState->getExecutionContext()->getSecurityOrigin()); |
| + m_service->GetBudget(origin, convertToBaseCallback(WTF::bind(&BudgetService::gotBudget, wrapPersistent(this), wrapPersistent(resolver)))); |
| + return promise; |
| +} |
| + |
| +void BudgetService::gotBudget(ScriptPromiseResolver* resolver, const mojo::WTFArray<mojom::blink::BudgetStatePtr> expectations) const |
| +{ |
| + // Copy the chunks into the budget array. |
| + HeapVector<Member<BudgetState>> budget(expectations.size()); |
| + for (uint i = 0; i < expectations.size(); i++) |
|
Peter Beverloo
2016/08/22 17:57:55
nit: we usually use size_t for this, which is also
harkness
2016/08/23 09:38:18
Done.
|
| + budget[i] = new BudgetState(expectations[i]->budget_at, expectations[i]->time); |
| + |
| + resolver->resolve(budget); |
| } |
| } // namespace blink |