| 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 "bindings/modules/v8/V8StorageEstimate.h" |
| 9 #include "core/dom/DOMException.h" | 9 #include "core/dom/DOMException.h" |
| 10 #include "core/dom/Document.h" | 10 #include "core/dom/Document.h" |
| 11 #include "core/dom/ExceptionCode.h" | 11 #include "core/dom/ExceptionCode.h" |
| 12 #include "modules/permissions/PermissionUtils.h" | 12 #include "modules/permissions/PermissionUtils.h" |
| 13 #include "modules/quota/StorageEstimate.h" | 13 #include "modules/quota/StorageEstimate.h" |
| 14 #include "platform/StorageQuotaCallbacks.h" | 14 #include "platform/StorageQuotaCallbacks.h" |
| 15 #include "platform/UserGestureIndicator.h" | 15 #include "platform/UserGestureIndicator.h" |
| 16 #include "public/platform/Platform.h" | 16 #include "public/platform/Platform.h" |
| 17 #include "wtf/Functional.h" | 17 #include "wtf/Functional.h" |
| 18 | 18 |
| 19 namespace blink { | 19 namespace blink { |
| 20 | 20 |
| 21 using mojom::blink::PermissionName; | 21 using mojom::blink::PermissionName; |
| 22 using mojom::blink::PermissionService; | 22 using mojom::blink::PermissionService; |
| 23 using mojom::blink::PermissionStatus; | 23 using mojom::blink::PermissionStatus; |
| 24 | 24 |
| 25 namespace { | 25 namespace { |
| 26 | 26 |
| 27 const char uniqueOriginErrorMessage[] = |
| 28 "The operation is not supported in this context."; |
| 29 |
| 27 class EstimateCallbacks final : public StorageQuotaCallbacks { | 30 class EstimateCallbacks final : public StorageQuotaCallbacks { |
| 28 WTF_MAKE_NONCOPYABLE(EstimateCallbacks); | 31 WTF_MAKE_NONCOPYABLE(EstimateCallbacks); |
| 29 | 32 |
| 30 public: | 33 public: |
| 31 explicit EstimateCallbacks(ScriptPromiseResolver* resolver) | 34 explicit EstimateCallbacks(ScriptPromiseResolver* resolver) |
| 32 : m_resolver(resolver) {} | 35 : m_resolver(resolver) {} |
| 33 | 36 |
| 34 ~EstimateCallbacks() override {} | 37 ~EstimateCallbacks() override {} |
| 35 | 38 |
| 36 void didQueryStorageUsageAndQuota(unsigned long long usageInBytes, | 39 void didQueryStorageUsageAndQuota(unsigned long long usageInBytes, |
| (...skipping 16 matching lines...) Expand all Loading... |
| 53 private: | 56 private: |
| 54 Member<ScriptPromiseResolver> m_resolver; | 57 Member<ScriptPromiseResolver> m_resolver; |
| 55 }; | 58 }; |
| 56 | 59 |
| 57 } // namespace | 60 } // namespace |
| 58 | 61 |
| 59 ScriptPromise StorageManager::persist(ScriptState* scriptState) { | 62 ScriptPromise StorageManager::persist(ScriptState* scriptState) { |
| 60 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); | 63 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
| 61 ScriptPromise promise = resolver->promise(); | 64 ScriptPromise promise = resolver->promise(); |
| 62 ExecutionContext* executionContext = scriptState->getExecutionContext(); | 65 ExecutionContext* executionContext = scriptState->getExecutionContext(); |
| 66 DCHECK(executionContext->isSecureContext()); // [SecureContext] in IDL |
| 63 SecurityOrigin* securityOrigin = executionContext->getSecurityOrigin(); | 67 SecurityOrigin* securityOrigin = executionContext->getSecurityOrigin(); |
| 64 // TODO(dgrogan): Is the isUnique() check covered by the later | |
| 65 // isSecureContext() check? If so, maybe remove it. Write a test if it | |
| 66 // stays. | |
| 67 if (securityOrigin->isUnique()) { | 68 if (securityOrigin->isUnique()) { |
| 68 resolver->reject(DOMException::create(NotSupportedError)); | 69 resolver->reject(V8ThrowException::createTypeError( |
| 70 scriptState->isolate(), uniqueOriginErrorMessage)); |
| 69 return promise; | 71 return promise; |
| 70 } | 72 } |
| 71 String errorMessage; | 73 |
| 72 if (!executionContext->isSecureContext(errorMessage)) { | |
| 73 resolver->reject(DOMException::create(SecurityError, errorMessage)); | |
| 74 return promise; | |
| 75 } | |
| 76 ASSERT(executionContext->isDocument()); | 74 ASSERT(executionContext->isDocument()); |
| 77 PermissionService* permissionService = | 75 PermissionService* permissionService = |
| 78 getPermissionService(scriptState->getExecutionContext()); | 76 getPermissionService(scriptState->getExecutionContext()); |
| 79 if (!permissionService) { | 77 if (!permissionService) { |
| 80 resolver->reject(DOMException::create( | 78 resolver->reject(DOMException::create( |
| 81 InvalidStateError, | 79 InvalidStateError, |
| 82 "In its current state, the global scope can't request permissions.")); | 80 "In its current state, the global scope can't request permissions.")); |
| 83 return promise; | 81 return promise; |
| 84 } | 82 } |
| 85 permissionService->RequestPermission( | 83 permissionService->RequestPermission( |
| 86 createPermissionDescriptor(PermissionName::DURABLE_STORAGE), | 84 createPermissionDescriptor(PermissionName::DURABLE_STORAGE), |
| 87 scriptState->getExecutionContext()->getSecurityOrigin(), | 85 scriptState->getExecutionContext()->getSecurityOrigin(), |
| 88 UserGestureIndicator::processingUserGesture(), | 86 UserGestureIndicator::processingUserGesture(), |
| 89 convertToBaseCallback( | 87 convertToBaseCallback( |
| 90 WTF::bind(&StorageManager::permissionRequestComplete, | 88 WTF::bind(&StorageManager::permissionRequestComplete, |
| 91 wrapPersistent(this), wrapPersistent(resolver)))); | 89 wrapPersistent(this), wrapPersistent(resolver)))); |
| 92 | 90 |
| 93 return promise; | 91 return promise; |
| 94 } | 92 } |
| 95 | 93 |
| 96 ScriptPromise StorageManager::persisted(ScriptState* scriptState) { | 94 ScriptPromise StorageManager::persisted(ScriptState* scriptState) { |
| 97 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); | 95 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
| 98 ScriptPromise promise = resolver->promise(); | 96 ScriptPromise promise = resolver->promise(); |
| 97 ExecutionContext* executionContext = scriptState->getExecutionContext(); |
| 98 DCHECK(executionContext->isSecureContext()); // [SecureContext] in IDL |
| 99 SecurityOrigin* securityOrigin = executionContext->getSecurityOrigin(); |
| 100 if (securityOrigin->isUnique()) { |
| 101 resolver->reject(V8ThrowException::createTypeError( |
| 102 scriptState->isolate(), uniqueOriginErrorMessage)); |
| 103 return promise; |
| 104 } |
| 105 |
| 99 PermissionService* permissionService = | 106 PermissionService* permissionService = |
| 100 getPermissionService(scriptState->getExecutionContext()); | 107 getPermissionService(scriptState->getExecutionContext()); |
| 101 if (!permissionService) { | 108 if (!permissionService) { |
| 102 resolver->reject(DOMException::create( | 109 resolver->reject(DOMException::create( |
| 103 InvalidStateError, | 110 InvalidStateError, |
| 104 "In its current state, the global scope can't query permissions.")); | 111 "In its current state, the global scope can't query permissions.")); |
| 105 return promise; | 112 return promise; |
| 106 } | 113 } |
| 107 permissionService->HasPermission( | 114 permissionService->HasPermission( |
| 108 createPermissionDescriptor(PermissionName::DURABLE_STORAGE), | 115 createPermissionDescriptor(PermissionName::DURABLE_STORAGE), |
| 109 scriptState->getExecutionContext()->getSecurityOrigin(), | 116 scriptState->getExecutionContext()->getSecurityOrigin(), |
| 110 convertToBaseCallback( | 117 convertToBaseCallback( |
| 111 WTF::bind(&StorageManager::permissionRequestComplete, | 118 WTF::bind(&StorageManager::permissionRequestComplete, |
| 112 wrapPersistent(this), wrapPersistent(resolver)))); | 119 wrapPersistent(this), wrapPersistent(resolver)))); |
| 113 return promise; | 120 return promise; |
| 114 } | 121 } |
| 115 | 122 |
| 116 ScriptPromise StorageManager::estimate(ScriptState* scriptState) { | 123 ScriptPromise StorageManager::estimate(ScriptState* scriptState) { |
| 117 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); | 124 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
| 118 ScriptPromise promise = resolver->promise(); | 125 ScriptPromise promise = resolver->promise(); |
| 119 ExecutionContext* executionContext = scriptState->getExecutionContext(); | 126 ExecutionContext* executionContext = scriptState->getExecutionContext(); |
| 127 DCHECK(executionContext->isSecureContext()); // [SecureContext] in IDL |
| 120 SecurityOrigin* securityOrigin = executionContext->getSecurityOrigin(); | 128 SecurityOrigin* securityOrigin = executionContext->getSecurityOrigin(); |
| 121 if (securityOrigin->isUnique()) { | 129 if (securityOrigin->isUnique()) { |
| 122 resolver->reject(DOMException::create(NotSupportedError)); | 130 resolver->reject(V8ThrowException::createTypeError( |
| 123 return promise; | 131 scriptState->isolate(), uniqueOriginErrorMessage)); |
| 124 } | |
| 125 // IDL has: [SecureContext] | |
| 126 String errorMessage; | |
| 127 if (!executionContext->isSecureContext(errorMessage)) { | |
| 128 resolver->reject(DOMException::create(SecurityError, errorMessage)); | |
| 129 return promise; | 132 return promise; |
| 130 } | 133 } |
| 131 | 134 |
| 132 KURL storagePartition = KURL(KURL(), securityOrigin->toString()); | 135 KURL storagePartition = KURL(KURL(), securityOrigin->toString()); |
| 133 Platform::current()->queryStorageUsageAndQuota( | 136 Platform::current()->queryStorageUsageAndQuota( |
| 134 storagePartition, WebStorageQuotaTypeTemporary, | 137 storagePartition, WebStorageQuotaTypeTemporary, |
| 135 new EstimateCallbacks(resolver)); | 138 new EstimateCallbacks(resolver)); |
| 136 return promise; | 139 return promise; |
| 137 } | 140 } |
| 138 | 141 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 155 | 158 |
| 156 void StorageManager::permissionRequestComplete(ScriptPromiseResolver* resolver, | 159 void StorageManager::permissionRequestComplete(ScriptPromiseResolver* resolver, |
| 157 PermissionStatus status) { | 160 PermissionStatus status) { |
| 158 if (!resolver->getExecutionContext() || | 161 if (!resolver->getExecutionContext() || |
| 159 resolver->getExecutionContext()->isContextDestroyed()) | 162 resolver->getExecutionContext()->isContextDestroyed()) |
| 160 return; | 163 return; |
| 161 resolver->resolve(status == PermissionStatus::GRANTED); | 164 resolver->resolve(status == PermissionStatus::GRANTED); |
| 162 } | 165 } |
| 163 | 166 |
| 164 } // namespace blink | 167 } // namespace blink |
| OLD | NEW |