Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(836)

Side by Side Diff: third_party/WebKit/Source/modules/budget/BudgetService.cpp

Issue 2231873002: Added budget_service.mojom (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@budget_api
Patch Set: code review comments Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 */)
29 {
30 DCHECK(m_service);
31 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ;
32 ScriptPromise promise = resolver->promise();
33
34 // TODO(harkness): Map the actionType to OperationType.
35 mojom::blink::OperationType type = mojom::blink::OperationType::SILENT_PUSH;
36 m_service->GetCost(type, convertToBaseCallback(WTF::bind(&BudgetService::asy ncGetCost, wrapPersistent(this), wrapPersistent(resolver))));
37 return promise;
38 }
39
40 void BudgetService::asyncGetCost(ScriptPromiseResolver* resolver, double cost) c onst
41 {
42 resolver->resolve(cost);
21 } 43 }
22 44
23 ScriptPromise BudgetService::getBudget(ScriptState* scriptState) 45 ScriptPromise BudgetService::getBudget(ScriptState* scriptState)
24 { 46 {
25 // TODO(harkness): Trigger the budget calculation. 47 DCHECK(m_service);
26 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::crea te(NotSupportedError, "Not yet implemented")); 48 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ;
49 ScriptPromise promise = resolver->promise();
50
51 // TODO(harkness): Get the origin from the ScriptState.
52 RefPtr<SecurityOrigin> origin(scriptState->getExecutionContext()->getSecurit yOrigin());
53 m_service->GetBudget(origin, convertToBaseCallback(WTF::bind(&BudgetService: :asyncGetBudget, wrapPersistent(this), unretained(scriptState), wrapPersistent(r esolver))));
harkness 2016/08/15 01:06:39 I couldn't find documentation about when to use un
54 return promise;
55 }
56
57 void BudgetService::asyncGetBudget(ScriptState* scriptState, ScriptPromiseResolv er* resolver, const mojo::WTFArray<mojom::blink::BudgetStatePtr> expectations) c onst
58 {
59
60 Vector<v8::Local<v8::Value>> budget;
61 if (!scriptState) {
62 resolver->resolve(budget);
63 return;
64 }
65
66 // Copy the chunks into the budget array.
67 budget.grow(expectations.size());
68 for (uint i = 0; i < expectations.size(); i++) {
69 BudgetChunk chunk;
70 chunk.setAmount(expectations[i]->budget_at);
71 chunk.setExpiration(expectations[i]->time);
72
73 budget[i] = freezeV8Object(toV8(chunk, scriptState), scriptState->isolat e());
harkness 2016/08/15 01:06:39 This line crashes, uploading so you can take a loo
74 }
75
76 resolver->resolve(budget);
27 } 77 }
28 78
29 } // namespace blink 79 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/modules/budget/BudgetService.h ('k') | third_party/WebKit/Source/modules/modules.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698