| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/encryptedmedia/ContentDecryptionModuleResultPromise.h" | 5 #include "modules/encryptedmedia/ContentDecryptionModuleResultPromise.h" |
| 6 | 6 |
| 7 #include "bindings/core/v8/ScriptPromise.h" | 7 #include "bindings/core/v8/ScriptPromise.h" |
| 8 #include "bindings/core/v8/ScriptState.h" | 8 #include "bindings/core/v8/ScriptState.h" |
| 9 #include "core/dom/DOMException.h" | 9 #include "core/dom/DOMException.h" |
| 10 #include "core/dom/ExecutionContext.h" | 10 #include "core/dom/ExecutionContext.h" |
| (...skipping 29 matching lines...) Expand all Loading... |
| 40 } | 40 } |
| 41 | 41 |
| 42 ContentDecryptionModuleResultPromise::ContentDecryptionModuleResultPromise( | 42 ContentDecryptionModuleResultPromise::ContentDecryptionModuleResultPromise( |
| 43 ScriptState* scriptState) | 43 ScriptState* scriptState) |
| 44 : m_resolver(ScriptPromiseResolver::create(scriptState)) {} | 44 : m_resolver(ScriptPromiseResolver::create(scriptState)) {} |
| 45 | 45 |
| 46 ContentDecryptionModuleResultPromise::~ContentDecryptionModuleResultPromise() {} | 46 ContentDecryptionModuleResultPromise::~ContentDecryptionModuleResultPromise() {} |
| 47 | 47 |
| 48 void ContentDecryptionModuleResultPromise::complete() { | 48 void ContentDecryptionModuleResultPromise::complete() { |
| 49 NOTREACHED(); | 49 NOTREACHED(); |
| 50 if (!isValidToFulfillPromise()) |
| 51 return; |
| 50 reject(InvalidStateError, "Unexpected completion."); | 52 reject(InvalidStateError, "Unexpected completion."); |
| 51 } | 53 } |
| 52 | 54 |
| 53 void ContentDecryptionModuleResultPromise::completeWithContentDecryptionModule( | 55 void ContentDecryptionModuleResultPromise::completeWithContentDecryptionModule( |
| 54 WebContentDecryptionModule* cdm) { | 56 WebContentDecryptionModule* cdm) { |
| 55 NOTREACHED(); | 57 NOTREACHED(); |
| 58 if (!isValidToFulfillPromise()) |
| 59 return; |
| 56 reject(InvalidStateError, "Unexpected completion."); | 60 reject(InvalidStateError, "Unexpected completion."); |
| 57 } | 61 } |
| 58 | 62 |
| 59 void ContentDecryptionModuleResultPromise::completeWithSession( | 63 void ContentDecryptionModuleResultPromise::completeWithSession( |
| 60 WebContentDecryptionModuleResult::SessionStatus status) { | 64 WebContentDecryptionModuleResult::SessionStatus status) { |
| 61 NOTREACHED(); | 65 NOTREACHED(); |
| 66 if (!isValidToFulfillPromise()) |
| 67 return; |
| 62 reject(InvalidStateError, "Unexpected completion."); | 68 reject(InvalidStateError, "Unexpected completion."); |
| 63 } | 69 } |
| 64 | 70 |
| 65 void ContentDecryptionModuleResultPromise::completeWithError( | 71 void ContentDecryptionModuleResultPromise::completeWithError( |
| 66 WebContentDecryptionModuleException exceptionCode, | 72 WebContentDecryptionModuleException exceptionCode, |
| 67 unsigned long systemCode, | 73 unsigned long systemCode, |
| 68 const WebString& errorMessage) { | 74 const WebString& errorMessage) { |
| 75 if (!isValidToFulfillPromise()) |
| 76 return; |
| 77 |
| 69 // Non-zero |systemCode| is appended to the |errorMessage|. If the | 78 // Non-zero |systemCode| is appended to the |errorMessage|. If the |
| 70 // |errorMessage| is empty, we'll report "Rejected with system code | 79 // |errorMessage| is empty, we'll report "Rejected with system code |
| 71 // (systemCode)". | 80 // (systemCode)". |
| 72 StringBuilder result; | 81 StringBuilder result; |
| 73 result.append(errorMessage); | 82 result.append(errorMessage); |
| 74 if (systemCode != 0) { | 83 if (systemCode != 0) { |
| 75 if (result.isEmpty()) | 84 if (result.isEmpty()) |
| 76 result.append("Rejected with system code"); | 85 result.append("Rejected with system code"); |
| 77 result.append(" ("); | 86 result.append(" ("); |
| 78 result.appendNumber(systemCode); | 87 result.appendNumber(systemCode); |
| 79 result.append(')'); | 88 result.append(')'); |
| 80 } | 89 } |
| 81 reject(WebCdmExceptionToExceptionCode(exceptionCode), result.toString()); | 90 reject(WebCdmExceptionToExceptionCode(exceptionCode), result.toString()); |
| 82 } | 91 } |
| 83 | 92 |
| 84 ScriptPromise ContentDecryptionModuleResultPromise::promise() { | 93 ScriptPromise ContentDecryptionModuleResultPromise::promise() { |
| 85 return m_resolver->promise(); | 94 return m_resolver->promise(); |
| 86 } | 95 } |
| 87 | 96 |
| 88 void ContentDecryptionModuleResultPromise::reject(ExceptionCode code, | 97 void ContentDecryptionModuleResultPromise::reject(ExceptionCode code, |
| 89 const String& errorMessage) { | 98 const String& errorMessage) { |
| 90 // Reject the promise asynchronously. This avoids problems when gc is | 99 DCHECK(isValidToFulfillPromise()); |
| 91 // destroying objects that result in unfulfilled promises being rejected. | |
| 92 // (Resolving promises is still done synchronously as there may be events | |
| 93 // already posted that need to happen only after the promise is resolved.) | |
| 94 // TODO(jrummell): Make resolving a promise asynchronous as well (including | |
| 95 // making sure events still happen after the promise is resolved). | |
| 96 if (getExecutionContext()) { | |
| 97 getExecutionContext()->postTask( | |
| 98 BLINK_FROM_HERE, | |
| 99 createSameThreadTask( | |
| 100 &ContentDecryptionModuleResultPromise::rejectInternal, | |
| 101 wrapPersistent(this), code, errorMessage)); | |
| 102 } | |
| 103 } | |
| 104 | 100 |
| 105 void ContentDecryptionModuleResultPromise::rejectInternal( | |
| 106 ExceptionCode code, | |
| 107 const String& errorMessage) { | |
| 108 m_resolver->reject(DOMException::create(code, errorMessage)); | 101 m_resolver->reject(DOMException::create(code, errorMessage)); |
| 109 m_resolver.clear(); | 102 m_resolver.clear(); |
| 110 } | 103 } |
| 111 | 104 |
| 112 ExecutionContext* ContentDecryptionModuleResultPromise::getExecutionContext() | 105 ExecutionContext* ContentDecryptionModuleResultPromise::getExecutionContext() |
| 113 const { | 106 const { |
| 114 return m_resolver->getExecutionContext(); | 107 return m_resolver->getExecutionContext(); |
| 115 } | 108 } |
| 116 | 109 |
| 110 bool ContentDecryptionModuleResultPromise::isValidToFulfillPromise() { |
| 111 // getExecutionContext() is no longer valid once the context is destroyed. |
| 112 // activeDOMObjectsAreStopped() is called to see if the context is in the |
| 113 // process of being destroyed. If it is, there is no need to fulfill this |
| 114 // promise which is about to go away anyway. |
| 115 return getExecutionContext() && |
| 116 !getExecutionContext()->activeDOMObjectsAreStopped(); |
| 117 } |
| 118 |
| 117 DEFINE_TRACE(ContentDecryptionModuleResultPromise) { | 119 DEFINE_TRACE(ContentDecryptionModuleResultPromise) { |
| 118 visitor->trace(m_resolver); | 120 visitor->trace(m_resolver); |
| 119 ContentDecryptionModuleResult::trace(visitor); | 121 ContentDecryptionModuleResult::trace(visitor); |
| 120 } | 122 } |
| 121 | 123 |
| 122 } // namespace blink | 124 } // namespace blink |
| OLD | NEW |