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

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

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

Powered by Google App Engine
This is Rietveld 408576698