Chromium Code Reviews| 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/BudgetChunk.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)); | |
| 22 } | |
| 16 | 23 |
| 17 ScriptPromise BudgetService::getCost(ScriptState* scriptState, const AtomicStrin g& actionType) | 24 BudgetService::~BudgetService() |
| 18 { | 25 { |
| 19 // TODO(harkness): Trigger the cost calculation. | 26 } |
| 20 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::crea te(NotSupportedError, "Not yet implemented")); | 27 |
| 28 ScriptPromise BudgetService::getCost(ScriptState* scriptState, const AtomicStrin g& /* actionType */) | |
|
johnme
2016/08/17 13:54:02
operationType?
harkness
2016/08/18 10:23:26
Actually, when it comes in to this function, it's
johnme
2016/08/18 13:04:31
Oh, I was looking at https://beverloo.github.io/bu
harkness
2016/08/22 08:57:47
Yup, that's definitely on the roadmap.
| |
| 29 { | |
| 30 DCHECK(m_service); | |
|
johnme
2016/08/17 13:54:01
Nit: Perhaps just DCHECK once in the constructor?
harkness
2016/08/18 10:23:26
Done.
| |
| 31 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; | |
| 32 ScriptPromise promise = resolver->promise(); | |
| 33 | |
| 34 // TODO(harkness): Map the actionType to OperationType. | |
| 35 // Get the cost for the action from the browser BudgetService. | |
| 36 mojom::blink::OperationType type = mojom::blink::OperationType::SILENT_PUSH; | |
| 37 m_service->GetCost(type, convertToBaseCallback(WTF::bind(&BudgetService::asy ncGetCost, wrapPersistent(this), wrapPersistent(resolver)))); | |
| 38 return promise; | |
| 39 } | |
| 40 | |
| 41 void BudgetService::asyncGetCost(ScriptPromiseResolver* resolver, double cost) c onst | |
| 42 { | |
| 43 resolver->resolve(cost); | |
| 21 } | 44 } |
| 22 | 45 |
| 23 ScriptPromise BudgetService::getBudget(ScriptState* scriptState) | 46 ScriptPromise BudgetService::getBudget(ScriptState* scriptState) |
| 24 { | 47 { |
| 25 // TODO(harkness): Trigger the budget calculation. | 48 DCHECK(m_service); |
| 26 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::crea te(NotSupportedError, "Not yet implemented")); | 49 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; |
| 50 ScriptPromise promise = resolver->promise(); | |
| 51 | |
| 52 // Get the budget from the browser BudgetService. | |
| 53 RefPtr<SecurityOrigin> origin(scriptState->getExecutionContext()->getSecurit yOrigin()); | |
| 54 m_service->GetBudget(origin, convertToBaseCallback(WTF::bind(&BudgetService: :asyncGetBudget, wrapPersistent(this), PassRefPtr<ScriptState>(scriptState), wra pPersistent(resolver)))); | |
|
johnme
2016/08/17 13:54:01
1. Is it ok to hold a reference to the ScriptState
harkness
2016/08/18 10:23:26
I didn't know about adoptRef, and if I'd known I p
| |
| 55 return promise; | |
| 56 } | |
| 57 | |
| 58 void BudgetService::asyncGetBudget(PassRefPtr<ScriptState> scriptState, ScriptPr omiseResolver* resolver, const mojo::WTFArray<mojom::blink::BudgetStatePtr> expe ctations) const | |
| 59 { | |
| 60 | |
| 61 Vector<v8::Local<v8::Value>> budget; | |
| 62 if (!scriptState) { | |
|
johnme
2016/08/17 13:54:02
How can this occur?
harkness
2016/08/18 10:23:26
That was left over from when I was passing ScriptS
| |
| 63 resolver->resolve(budget); | |
| 64 return; | |
| 65 } | |
| 66 if (!scriptState->contextIsValid()) { | |
| 67 resolver->resolve(budget); | |
| 68 return; | |
| 69 } | |
| 70 | |
| 71 // Create a new scope for the object that is passed back to script. | |
| 72 ScriptState::Scope scope(scriptState.get()); | |
| 73 | |
| 74 // Copy the chunks into the budget array. | |
| 75 budget.grow(expectations.size()); | |
| 76 for (uint i = 0; i < expectations.size(); i++) { | |
| 77 BudgetChunk chunk; | |
| 78 chunk.setAmount(expectations[i]->budget_at); | |
| 79 chunk.setExpiration(expectations[i]->time); | |
| 80 budget[i] = freezeV8Object(toV8(chunk, scriptState.get()), scriptState-> isolate()); | |
|
johnme
2016/08/17 13:54:01
BudgetState objects shouldn't need to be individua
harkness
2016/08/18 10:23:26
I think I can get away without the extra freezeV8O
johnme
2016/08/18 13:04:31
It looks like ExtendableMessageEvent::ports() retu
harkness
2016/08/22 08:57:47
I hadn't realized that BudgetState (formerly Budge
| |
| 81 } | |
| 82 | |
| 83 resolver->resolve(budget); | |
| 27 } | 84 } |
| 28 | 85 |
| 29 } // namespace blink | 86 } // namespace blink |
| OLD | NEW |