OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/quota/StorageManager.h" | 5 #include "modules/quota/StorageManager.h" |
6 | 6 |
7 #include "bindings/core/v8/ScriptPromiseResolver.h" | 7 #include "bindings/core/v8/ScriptPromiseResolver.h" |
| 8 #include "bindings/modules/v8/V8StorageEstimate.h" |
8 #include "core/dom/DOMException.h" | 9 #include "core/dom/DOMException.h" |
9 #include "core/dom/Document.h" | 10 #include "core/dom/Document.h" |
10 #include "core/dom/ExceptionCode.h" | 11 #include "core/dom/ExceptionCode.h" |
11 #include "modules/permissions/Permissions.h" | 12 #include "modules/permissions/Permissions.h" |
| 13 #include "modules/quota/StorageEstimate.h" |
| 14 #include "platform/StorageQuotaCallbacks.h" |
12 #include "public/platform/Platform.h" | 15 #include "public/platform/Platform.h" |
13 #include "public/platform/WebCallbacks.h" | 16 #include "public/platform/WebCallbacks.h" |
14 #include "public/platform/modules/permissions/WebPermissionClient.h" | 17 #include "public/platform/modules/permissions/WebPermissionClient.h" |
15 #include "public/platform/modules/permissions/WebPermissionStatus.h" | 18 #include "public/platform/modules/permissions/WebPermissionStatus.h" |
16 | 19 |
17 namespace blink { | 20 namespace blink { |
18 | 21 |
19 namespace { | 22 namespace { |
20 | 23 |
21 class DurableStorageQueryCallbacks final : public WebPermissionCallback { | 24 class DurableStorageQueryCallbacks final : public WebPermissionCallback { |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
63 } | 66 } |
64 void onError() override | 67 void onError() override |
65 { | 68 { |
66 ASSERT_NOT_REACHED(); | 69 ASSERT_NOT_REACHED(); |
67 } | 70 } |
68 | 71 |
69 private: | 72 private: |
70 Persistent<ScriptPromiseResolver> m_resolver; | 73 Persistent<ScriptPromiseResolver> m_resolver; |
71 }; | 74 }; |
72 | 75 |
| 76 class EstimateCallbacks final : public StorageQuotaCallbacks { |
| 77 WTF_MAKE_NONCOPYABLE(EstimateCallbacks); |
| 78 public: |
| 79 explicit EstimateCallbacks(ScriptPromiseResolver* resolver) |
| 80 : m_resolver(resolver) {} |
| 81 |
| 82 ~EstimateCallbacks() override {} |
| 83 |
| 84 void didQueryStorageUsageAndQuota(unsigned long long usageInBytes, unsigned
long long quotaInBytes) override |
| 85 { |
| 86 StorageEstimate estimate; |
| 87 estimate.setUsage(usageInBytes); |
| 88 estimate.setQuota(quotaInBytes); |
| 89 m_resolver->resolve(estimate); |
| 90 } |
| 91 |
| 92 void didFail(WebStorageQuotaError error) override |
| 93 { |
| 94 m_resolver->reject(DOMException::create(static_cast<ExceptionCode>(error
))); |
| 95 } |
| 96 |
| 97 DEFINE_INLINE_VIRTUAL_TRACE() |
| 98 { |
| 99 visitor->trace(m_resolver); |
| 100 StorageQuotaCallbacks::trace(visitor); |
| 101 } |
| 102 |
| 103 private: |
| 104 Member<ScriptPromiseResolver> m_resolver; |
| 105 }; |
| 106 |
73 } // namespace | 107 } // namespace |
74 | 108 |
75 ScriptPromise StorageManager::requestPersistent(ScriptState* scriptState) | 109 ScriptPromise StorageManager::requestPersistent(ScriptState* scriptState) |
76 { | 110 { |
77 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; | 111 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; |
78 ScriptPromise promise = resolver->promise(); | 112 ScriptPromise promise = resolver->promise(); |
79 ExecutionContext* executionContext = scriptState->getExecutionContext(); | 113 ExecutionContext* executionContext = scriptState->getExecutionContext(); |
80 SecurityOrigin* securityOrigin = executionContext->getSecurityOrigin(); | 114 SecurityOrigin* securityOrigin = executionContext->getSecurityOrigin(); |
81 // TODO(dgrogan): Is the isUnique() check covered by the later | 115 // TODO(dgrogan): Is the isUnique() check covered by the later |
82 // isSecureContext() check? If so, maybe remove it. Write a test if it | 116 // isSecureContext() check? If so, maybe remove it. Write a test if it |
(...skipping 24 matching lines...) Expand all Loading... |
107 ScriptPromise promise = resolver->promise(); | 141 ScriptPromise promise = resolver->promise(); |
108 WebPermissionClient* permissionClient = Permissions::getClient(scriptState->
getExecutionContext()); | 142 WebPermissionClient* permissionClient = Permissions::getClient(scriptState->
getExecutionContext()); |
109 if (!permissionClient) { | 143 if (!permissionClient) { |
110 resolver->reject(DOMException::create(InvalidStateError, "In its current
state, the global scope can't query permissions.")); | 144 resolver->reject(DOMException::create(InvalidStateError, "In its current
state, the global scope can't query permissions.")); |
111 return promise; | 145 return promise; |
112 } | 146 } |
113 permissionClient->queryPermission(WebPermissionTypeDurableStorage, KURL(KURL
(), scriptState->getExecutionContext()->getSecurityOrigin()->toString()), new Du
rableStorageQueryCallbacks(resolver)); | 147 permissionClient->queryPermission(WebPermissionTypeDurableStorage, KURL(KURL
(), scriptState->getExecutionContext()->getSecurityOrigin()->toString()), new Du
rableStorageQueryCallbacks(resolver)); |
114 return promise; | 148 return promise; |
115 } | 149 } |
116 | 150 |
| 151 ScriptPromise StorageManager::estimate(ScriptState* scriptState) |
| 152 { |
| 153 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; |
| 154 ScriptPromise promise = resolver->promise(); |
| 155 ExecutionContext* executionContext = scriptState->getExecutionContext(); |
| 156 SecurityOrigin* securityOrigin = executionContext->getSecurityOrigin(); |
| 157 if (securityOrigin->isUnique()) { |
| 158 resolver->reject(DOMException::create(NotSupportedError)); |
| 159 return promise; |
| 160 } |
| 161 // IDL has: [SecureContext] |
| 162 String errorMessage; |
| 163 if (!executionContext->isSecureContext(errorMessage)) { |
| 164 resolver->reject(DOMException::create(SecurityError, errorMessage)); |
| 165 return promise; |
| 166 } |
| 167 |
| 168 KURL storagePartition = KURL(KURL(), securityOrigin->toString()); |
| 169 Platform::current()->queryStorageUsageAndQuota(storagePartition, WebStorageQ
uotaTypeTemporary, new EstimateCallbacks(resolver)); |
| 170 return promise; |
| 171 } |
| 172 |
117 DEFINE_TRACE(StorageManager) | 173 DEFINE_TRACE(StorageManager) |
118 { | 174 { |
119 } | 175 } |
120 | 176 |
121 } // namespace blink | 177 } // namespace blink |
OLD | NEW |