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" | 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 mojom::blink::BudgetOperationType stringToOperationType(const AtomicString& operation) | |
Peter Beverloo
2016/09/05 17:06:16
nit: please don't indent this code by an additiona
harkness
2016/09/06 15:45:01
Done.
Peter Beverloo
2016/09/06 15:54:10
This isn't done either.
harkness
2016/09/06 16:17:25
git cl format strikes again, but if I override it,
| |
20 { | |
21 if (operation == "silent-push") | |
22 return mojom::blink::BudgetOperationType::SILENT_PUSH; | |
23 | |
24 return mojom::blink::BudgetOperationType::INVALID_OPERATION; | |
25 } | |
26 | |
27 DOMException* errorTypeToException(mojom::blink::BudgetServiceErrorType erro r) | |
28 { | |
29 switch (error) { | |
30 case mojom::blink::BudgetServiceErrorType::NO_ERROR: | |
31 return nullptr; | |
32 case mojom::blink::BudgetServiceErrorType::DATABASE_ERROR: | |
33 return DOMException::create(DataError, "Error reading the budget dat abase."); | |
34 case mojom::blink::BudgetServiceErrorType::NOT_SUPPORTED: | |
35 return DOMException::create(NotSupportedError, "Requested opration w as not supported"); | |
36 } | |
Peter Beverloo
2016/09/05 17:06:16
Please add a NOTREACHED() at the end of this metho
harkness
2016/09/06 15:45:01
Done.
| |
37 } | |
38 | |
39 } // namespace | |
18 | 40 |
19 BudgetService::BudgetService() | 41 BudgetService::BudgetService() |
20 { | 42 { |
21 Platform::current()->interfaceProvider()->getInterface(mojo::GetProxy(&m_ser vice)); | 43 Platform::current()->interfaceProvider()->getInterface(mojo::GetProxy(&m_ser vice)); |
22 | 44 |
23 // Set a connection error handler, so that if an embedder doesn't | 45 // Set a connection error handler, so that if an embedder doesn't |
24 // implement a BudgetSerice mojo service, the developer will get a | 46 // implement a BudgetSerice mojo service, the developer will get a |
25 // actionable information. | 47 // actionable information. |
26 m_service.set_connection_error_handler(convertToBaseCallback(WTF::bind(&Budg etService::onConnectionError, wrapWeakPersistent(this)))); | 48 m_service.set_connection_error_handler(convertToBaseCallback(WTF::bind(&Budg etService::onConnectionError, wrapWeakPersistent(this)))); |
27 } | 49 } |
28 | 50 |
29 BudgetService::~BudgetService() | 51 BudgetService::~BudgetService() |
30 { | 52 { |
31 } | 53 } |
32 | 54 |
33 ScriptPromise BudgetService::getCost(ScriptState* scriptState, const AtomicStrin g& /* actionType */) | 55 ScriptPromise BudgetService::getCost(ScriptState* scriptState, const AtomicStrin g& operation) |
34 { | 56 { |
35 DCHECK(m_service); | 57 DCHECK(m_service); |
36 | 58 |
37 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; | 59 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; |
38 ScriptPromise promise = resolver->promise(); | 60 ScriptPromise promise = resolver->promise(); |
39 | 61 |
40 // TODO(harkness): Map the actionType to BudgetOperationType. | 62 mojom::blink::BudgetOperationType type = stringToOperationType(operation); |
63 if (type == mojom::blink::BudgetOperationType::INVALID_OPERATION) { | |
64 resolver->reject(DOMException::create(NotSupportedError, "Invalid operat ion type specified")); | |
Peter Beverloo
2016/09/05 17:06:16
nit: the clean way of doing this (here and on line
harkness
2016/09/06 15:45:01
I updated here and 123, but 98 is a different situ
Peter Beverloo
2016/09/06 15:54:10
98's fine, thanks :)
harkness
2016/09/06 16:17:25
Acknowledged.
| |
65 return promise; | |
66 } | |
67 | |
41 // Get the cost for the action from the browser BudgetService. | 68 // 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)))); | 69 m_service->GetCost(type, convertToBaseCallback(WTF::bind(&BudgetService::got Cost, wrapPersistent(this), wrapPersistent(resolver)))); |
44 return promise; | 70 return promise; |
45 } | 71 } |
46 | 72 |
47 void BudgetService::gotCost(ScriptPromiseResolver* resolver, double cost) const | 73 void BudgetService::gotCost(ScriptPromiseResolver* resolver, double cost) const |
48 { | 74 { |
49 resolver->resolve(cost); | 75 resolver->resolve(cost); |
50 } | 76 } |
51 | 77 |
52 ScriptPromise BudgetService::getBudget(ScriptState* scriptState) | 78 ScriptPromise BudgetService::getBudget(ScriptState* scriptState) |
53 { | 79 { |
54 DCHECK(m_service); | 80 DCHECK(m_service); |
55 | 81 |
56 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; | 82 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; |
57 ScriptPromise promise = resolver->promise(); | 83 ScriptPromise promise = resolver->promise(); |
58 | 84 |
59 ExecutionContext* context = scriptState->getExecutionContext(); | 85 ExecutionContext* context = scriptState->getExecutionContext(); |
60 if (!context) | 86 if (!context) |
61 return promise; | 87 return promise; |
62 | 88 |
63 // Get the budget from the browser BudgetService. | 89 // Get the budget from the browser BudgetService. |
64 RefPtr<SecurityOrigin> origin(context->getSecurityOrigin()); | 90 RefPtr<SecurityOrigin> origin(context->getSecurityOrigin()); |
65 m_service->GetBudget(origin, convertToBaseCallback(WTF::bind(&BudgetService: :gotBudget, wrapPersistent(this), wrapPersistent(resolver)))); | 91 m_service->GetBudget(origin, convertToBaseCallback(WTF::bind(&BudgetService: :gotBudget, wrapPersistent(this), wrapPersistent(resolver)))); |
66 return promise; | 92 return promise; |
67 } | 93 } |
68 | 94 |
69 void BudgetService::gotBudget(ScriptPromiseResolver* resolver, const mojo::WTFAr ray<mojom::blink::BudgetStatePtr> expectations) const | 95 void BudgetService::gotBudget(ScriptPromiseResolver* resolver, mojom::blink::Bud getServiceErrorType error, const mojo::WTFArray<mojom::blink::BudgetStatePtr> ex pectations) const |
70 { | 96 { |
97 if (error != mojom::blink::BudgetServiceErrorType::NO_ERROR) { | |
98 resolver->reject(errorTypeToException(error)); | |
99 return; | |
100 } | |
101 | |
71 // Copy the chunks into the budget array. | 102 // Copy the chunks into the budget array. |
72 HeapVector<Member<BudgetState>> budget(expectations.size()); | 103 HeapVector<Member<BudgetState>> budget(expectations.size()); |
73 for (size_t i = 0; i < expectations.size(); i++) | 104 for (size_t i = 0; i < expectations.size(); i++) |
74 budget[i] = new BudgetState(expectations[i]->budget_at, expectations[i]- >time); | 105 budget[i] = new BudgetState(expectations[i]->budget_at, expectations[i]- >time); |
75 | 106 |
76 resolver->resolve(budget); | 107 resolver->resolve(budget); |
77 } | 108 } |
78 | 109 |
110 ScriptPromise BudgetService::reserve(ScriptState* scriptState, const AtomicStrin g& operation) | |
111 { | |
112 DCHECK(m_service); | |
113 | |
114 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; | |
115 ScriptPromise promise = resolver->promise(); | |
116 | |
117 ExecutionContext* context = scriptState->getExecutionContext(); | |
118 if (!context) | |
haraken
2016/09/06 01:46:44
Nit: I don't think this should happen.
harkness
2016/09/06 15:45:01
In a previous review, you mentioned that it could
haraken
2016/09/07 01:55:47
Maybe are you talking about other getExecutionCont
harkness
2016/09/07 09:24:52
Thanks very much for the explanation, I've removed
| |
119 return promise; | |
120 | |
Peter Beverloo
2016/09/05 17:06:16
nit: mind adding a TODO that we need to do a secur
harkness
2016/09/06 15:45:00
Done.
| |
121 mojom::blink::BudgetOperationType type = stringToOperationType(operation); | |
122 if (type == mojom::blink::BudgetOperationType::INVALID_OPERATION) { | |
123 resolver->reject(DOMException::create(NotSupportedError, "Invalid operat ion type specified")); | |
124 return promise; | |
125 } | |
126 | |
127 // Call to the BudgetService to place the reservation. | |
128 RefPtr<SecurityOrigin> origin(context->getSecurityOrigin()); | |
129 m_service->Reserve(origin, type, convertToBaseCallback(WTF::bind(&BudgetServ ice::gotReservation, wrapPersistent(this), wrapPersistent(resolver)))); | |
130 return promise; | |
131 } | |
132 | |
133 void BudgetService::gotReservation(ScriptPromiseResolver* resolver, mojom::blink ::BudgetServiceErrorType error, bool success) const | |
134 { | |
135 if (error != mojom::blink::BudgetServiceErrorType::NO_ERROR) { | |
136 resolver->reject(errorTypeToException(error)); | |
137 return; | |
138 } | |
139 | |
140 resolver->resolve(success); | |
141 } | |
142 | |
79 void BudgetService::onConnectionError() | 143 void BudgetService::onConnectionError() |
80 { | 144 { |
81 LOG(ERROR) << "Unable to connect to the Mojo BudgetService."; | 145 LOG(ERROR) << "Unable to connect to the Mojo BudgetService."; |
82 // TODO(harkness): Reject in flight promises. | 146 // TODO(harkness): Reject in flight promises. |
83 } | 147 } |
84 | 148 |
85 } // namespace blink | 149 } // namespace blink |
OLD | NEW |