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

Unified Diff: third_party/WebKit/Source/modules/budget/BudgetService.cpp

Issue 2309863002: Plumb reserve method of the BudgetAPI (Closed)
Patch Set: Updated with code review comments 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 side-by-side diff with in-line comments
Download patch
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 6cbd0baa01c27c20cef728afeabbf835aee84bbe..d3c187cea351f1d2408385f929b1df61c3005a82 100644
--- a/third_party/WebKit/Source/modules/budget/BudgetService.cpp
+++ b/third_party/WebKit/Source/modules/budget/BudgetService.cpp
@@ -15,6 +15,30 @@
#include "public/platform/modules/budget_service/budget_service.mojom-blink.h"
namespace blink {
+namespace {
+ mojom::blink::BudgetOperationType stringToOperationType(const AtomicString& operation)
+ {
+ if (operation == "silent-push")
+ return mojom::blink::BudgetOperationType::SILENT_PUSH;
+
+ return mojom::blink::BudgetOperationType::INVALID_OPERATION;
+ }
+
+ DOMException* errorTypeToException(mojom::blink::BudgetServiceErrorType error)
+ {
+ switch (error) {
+ case mojom::blink::BudgetServiceErrorType::NO_ERROR:
+ return nullptr;
+ case mojom::blink::BudgetServiceErrorType::DATABASE_ERROR:
+ return DOMException::create(DataError, "Error reading the budget database.");
+ case mojom::blink::BudgetServiceErrorType::NOT_SUPPORTED:
+ return DOMException::create(NotSupportedError, "Requested opration was not supported");
+ }
+ NOTREACHED();
+ return nullptr;
+ }
+
+} // namespace
BudgetService::BudgetService()
{
@@ -30,16 +54,18 @@ BudgetService::~BudgetService()
{
}
-ScriptPromise BudgetService::getCost(ScriptState* scriptState, const AtomicString& /* actionType */)
+ScriptPromise BudgetService::getCost(ScriptState* scriptState, const AtomicString& operation)
{
DCHECK(m_service);
+ mojom::blink::BudgetOperationType type = stringToOperationType(operation);
+ if (type == mojom::blink::BudgetOperationType::INVALID_OPERATION)
+ return ScriptPromise::rejectWithDOMException(scriptState, DOMException::create(NotSupportedError, "Invalid operation type specified"));
+
ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState);
ScriptPromise promise = resolver->promise();
- // 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;
}
@@ -62,12 +88,18 @@ ScriptPromise BudgetService::getBudget(ScriptState* scriptState)
// Get the budget from the browser BudgetService.
RefPtr<SecurityOrigin> origin(context->getSecurityOrigin());
+ // TODO(harkness): Check that this is a valid secure origin.
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
+void BudgetService::gotBudget(ScriptPromiseResolver* resolver, mojom::blink::BudgetServiceErrorType error, const mojo::WTFArray<mojom::blink::BudgetStatePtr> expectations) const
{
+ if (error != mojom::blink::BudgetServiceErrorType::NO_ERROR) {
+ resolver->reject(errorTypeToException(error));
+ return;
+ }
+
// Copy the chunks into the budget array.
HeapVector<Member<BudgetState>> budget(expectations.size());
for (size_t i = 0; i < expectations.size(); i++)
@@ -76,6 +108,38 @@ void BudgetService::gotBudget(ScriptPromiseResolver* resolver, const mojo::WTFAr
resolver->resolve(budget);
}
+ScriptPromise BudgetService::reserve(ScriptState* scriptState, const AtomicString& operation)
+{
+ DCHECK(m_service);
+
+ mojom::blink::BudgetOperationType type = stringToOperationType(operation);
+ if (type == mojom::blink::BudgetOperationType::INVALID_OPERATION)
+ return ScriptPromise::rejectWithDOMException(scriptState, DOMException::create(NotSupportedError, "Invalid operation type specified"));
+
+ ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState);
+ ScriptPromise promise = resolver->promise();
+
+ ExecutionContext* context = scriptState->getExecutionContext();
+ if (!context)
+ return promise;
+
+ // Call to the BudgetService to place the reservation.
+ RefPtr<SecurityOrigin> origin(context->getSecurityOrigin());
+ // TODO(harkness): Check that this is a valid secure origin.
+ m_service->Reserve(origin, type, convertToBaseCallback(WTF::bind(&BudgetService::gotReservation, wrapPersistent(this), wrapPersistent(resolver))));
+ return promise;
+}
+
+void BudgetService::gotReservation(ScriptPromiseResolver* resolver, mojom::blink::BudgetServiceErrorType error, bool success) const
+{
+ if (error != mojom::blink::BudgetServiceErrorType::NO_ERROR) {
+ resolver->reject(errorTypeToException(error));
+ return;
+ }
+
+ resolver->resolve(success);
+}
+
void BudgetService::onConnectionError()
{
LOG(ERROR) << "Unable to connect to the Mojo BudgetService.";

Powered by Google App Engine
This is Rietveld 408576698