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

Unified Diff: Source/modules/quota/StorageManager.cpp

Issue 1154573005: Expose Durable Storage API to script (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: update comment Created 5 years, 5 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: Source/modules/quota/StorageManager.cpp
diff --git a/Source/modules/quota/StorageManager.cpp b/Source/modules/quota/StorageManager.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..340d14ed34880f3dc923e98ca6aa8178728edaed
--- /dev/null
+++ b/Source/modules/quota/StorageManager.cpp
@@ -0,0 +1,86 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "config.h"
+#include "modules/quota/StorageManager.h"
+
+#include "bindings/core/v8/ScriptPromiseResolver.h"
+#include "core/dom/DOMError.h"
jsbell 2015/08/03 19:57:41 DOMError is not used?
dgrogan 2015/08/06 23:52:02 Only as a vehicle for ExceptionCode.h. Fixed.
+#include "core/dom/Document.h"
+#include "modules/permissions/Permissions.h"
+#include "public/platform/Platform.h"
+#include "public/platform/WebCallbacks.h"
+#include "public/platform/modules/permissions/WebPermissionClient.h"
+#include "public/platform/modules/permissions/WebPermissionStatus.h"
+
+namespace blink {
+
+class DurableStoragePermissionCallbacks : public WebCallbacks<WebPermissionStatus*, void> {
jsbell 2015/08/03 19:57:41 Put this class in anonymous namespace?
jsbell 2015/08/03 19:57:41 final?
dgrogan 2015/08/06 23:52:02 Done.
dgrogan 2015/08/06 23:52:02 Done.
+public:
+ DurableStoragePermissionCallbacks(PassRefPtr<ScriptPromiseResolver> resolver)
+ : m_resolver(resolver)
+ {
+ }
+
+ void onSuccess(WebPermissionStatus* rawStatus) override
+ {
+ OwnPtr<WebPermissionStatus> status = adoptPtr(rawStatus);
+ String toReturn;
+ switch (*status) {
+ case WebPermissionStatusGranted:
+ toReturn = "granted";
+ break;
+ case WebPermissionStatusDenied:
+ toReturn = "denied";
+ break;
+ case WebPermissionStatusPrompt:
+ toReturn = "default";
+ break;
+ }
+ m_resolver->resolve(toReturn);
Lalit Maganti 2015/08/04 13:27:41 Not really any work to be done but very similar co
dgrogan 2015/08/06 23:52:02 It'd be awkward to consolidate while the strings a
+ }
+ void onError() override
+ {
+ ASSERT_NOT_REACHED();
Lalit Maganti 2015/08/04 13:27:41 I think propogating the error to the resolver may
dgrogan 2015/08/06 23:52:02 I was trying to document that this method can neve
Lalit Maganti 2015/08/07 08:47:28 You're right - currently it cannot be reached. How
dgrogan 2015/08/10 22:26:40 I thought about this and still prefer to leave it
+ }
+
+private:
+ RefPtr<ScriptPromiseResolver> m_resolver;
+};
+
+ScriptPromise StorageManager::requestPersistent(ScriptState* scriptState)
+{
+ RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scriptState);
+ ScriptPromise promise = resolver->promise();
+ ExecutionContext* executionContext = scriptState->executionContext();
+ SecurityOrigin* securityOrigin = executionContext->securityOrigin();
+ // TODO(dgrogan): Durable is restricted to secure contexts in the browser
+ // process. Can we do it here too, before jumping the process boundary?
jsbell 2015/08/03 19:57:41 Yes, use something like: String errorMessage; if
dgrogan 2015/08/06 23:52:02 Done, thanks a lot for the pointer.
+ if (securityOrigin->isUnique()) {
+ resolver->reject(DOMException::create(NotSupportedError));
+ return promise;
+ }
+ if (executionContext->isDocument()) {
mlamouri (slow - plz ping) 2015/08/04 13:15:19 Why do you want to check if it's in a document? Th
dgrogan 2015/08/06 23:52:02 I should probably rather DCHECK that it's a Docume
+ WebPermissionClient* permissionClient = Permissions::getClient(executionContext);
+ ASSERT(permissionClient);
Lalit Maganti 2015/08/04 13:27:41 Should this be DASSERT or ASSERT? I think you can
dgrogan 2015/08/06 23:52:02 I don't think DASSERT exists: https://code.google.
Lalit Maganti 2015/08/07 08:47:28 Apologies - I meant DCHECK
+ permissionClient->requestPermission(WebPermissionTypeDurableStorage, KURL(KURL(), scriptState->executionContext()->securityOrigin()->toString()), new DurableStoragePermissionCallbacks(resolver));
+ } else {
+ resolver->reject(DOMException::create(NotSupportedError));
+ return promise;
+ }
+
+ return promise;
+}
+
+ScriptPromise StorageManager::persistentPermission(ScriptState* scriptState)
+{
+ // TODO(dgrogan): Add a test for calling this method on a file:/// origin.
+ RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scriptState);
+ ScriptPromise promise = resolver->promise();
+ WebPermissionClient* permissionClient = Permissions::getClient(scriptState->executionContext());
+ permissionClient->queryPermission(WebPermissionTypeDurableStorage, KURL(KURL(), scriptState->executionContext()->securityOrigin()->toString()), new DurableStoragePermissionCallbacks(resolver));
+ return promise;
+}
+
+} // namespace blink

Powered by Google App Engine
This is Rietveld 408576698