| 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 #ifndef ContentDecryptionModuleResultPromise_h | 5 #ifndef ContentDecryptionModuleResultPromise_h |
| 6 #define ContentDecryptionModuleResultPromise_h | 6 #define ContentDecryptionModuleResultPromise_h |
| 7 | 7 |
| 8 #include "bindings/core/v8/ScriptPromiseResolver.h" | 8 #include "bindings/core/v8/ScriptPromiseResolver.h" |
| 9 #include "core/dom/ExceptionCode.h" | 9 #include "core/dom/ExceptionCode.h" |
| 10 #include "platform/ContentDecryptionModuleResult.h" | 10 #include "platform/ContentDecryptionModuleResult.h" |
| 11 | 11 |
| 12 namespace blink { | 12 namespace blink { |
| 13 | 13 |
| 14 ExceptionCode WebCdmExceptionToExceptionCode( | 14 ExceptionCode WebCdmExceptionToExceptionCode( |
| 15 WebContentDecryptionModuleException); | 15 WebContentDecryptionModuleException); |
| 16 | 16 |
| 17 // This class wraps the promise resolver to simplify creation of | 17 // This class wraps the promise resolver to simplify creation of |
| 18 // ContentDecryptionModuleResult objects. The default implementations of the | 18 // ContentDecryptionModuleResult objects. The default implementations of the |
| 19 // complete(), completeWithSession(), etc. methods will reject the promise | 19 // complete(), completeWithSession(), etc. methods will reject the promise |
| 20 // with an error. It needs to be subclassed and the appropriate complete() | 20 // with an error. It needs to be subclassed and the appropriate complete() |
| 21 // method overridden to resolve the promise as needed. | 21 // method overridden to resolve the promise as needed. |
| 22 // |
| 23 // Subclasses need to keep a Member<> to the object that created them so |
| 24 // that the creator remains around as long as this promise is pending. This |
| 25 // promise is not referenced by the object that created it (e.g MediaKeys, |
| 26 // MediaKeySession, Navigator.requestMediaKeySystemAccess), so this promise |
| 27 // may be cleaned up before or after it's creator once both become unreachable. |
| 28 // If it is after, the destruction of the creator may trigger this promise, |
| 29 // so use isValidToFulfillPromise() to verify that it is safe to fulfill |
| 30 // the promise. |
| 22 class ContentDecryptionModuleResultPromise | 31 class ContentDecryptionModuleResultPromise |
| 23 : public ContentDecryptionModuleResult { | 32 : public ContentDecryptionModuleResult { |
| 24 public: | 33 public: |
| 25 ~ContentDecryptionModuleResultPromise() override; | 34 ~ContentDecryptionModuleResultPromise() override; |
| 26 | 35 |
| 27 // ContentDecryptionModuleResult implementation. | 36 // ContentDecryptionModuleResult implementation. |
| 28 void complete() override; | 37 void complete() override; |
| 29 void completeWithContentDecryptionModule( | 38 void completeWithContentDecryptionModule( |
| 30 WebContentDecryptionModule*) override; | 39 WebContentDecryptionModule*) override; |
| 31 void completeWithSession( | 40 void completeWithSession( |
| 32 WebContentDecryptionModuleResult::SessionStatus) override; | 41 WebContentDecryptionModuleResult::SessionStatus) override; |
| 33 void completeWithError(WebContentDecryptionModuleException, | 42 void completeWithError(WebContentDecryptionModuleException, |
| 34 unsigned long systemCode, | 43 unsigned long systemCode, |
| 35 const WebString&) override; | 44 const WebString&) override; |
| 36 | 45 |
| 37 // It is only valid to call this before completion. | 46 // It is only valid to call this before completion. |
| 38 ScriptPromise promise(); | 47 ScriptPromise promise(); |
| 39 | 48 |
| 40 DECLARE_VIRTUAL_TRACE(); | 49 DECLARE_VIRTUAL_TRACE(); |
| 41 | 50 |
| 42 protected: | 51 protected: |
| 43 explicit ContentDecryptionModuleResultPromise(ScriptState*); | 52 explicit ContentDecryptionModuleResultPromise(ScriptState*); |
| 44 | 53 |
| 45 // Resolves the promise with |value|. Used by subclasses to resolve the | 54 // Resolves the promise with |value|. Used by subclasses to resolve the |
| 46 // promise. | 55 // promise. |
| 47 template <typename... T> | 56 template <typename... T> |
| 48 void resolve(T... value) { | 57 void resolve(T... value) { |
| 58 DCHECK(isValidToFulfillPromise()); |
| 59 |
| 49 m_resolver->resolve(value...); | 60 m_resolver->resolve(value...); |
| 50 m_resolver.clear(); | 61 m_resolver.clear(); |
| 51 } | 62 } |
| 52 | 63 |
| 53 // Rejects the promise with a DOMException. This will post a task to | 64 // Rejects the promise with a DOMException. |
| 54 // actually reject the promise later on. | |
| 55 void reject(ExceptionCode, const String& errorMessage); | 65 void reject(ExceptionCode, const String& errorMessage); |
| 56 | 66 |
| 57 ExecutionContext* getExecutionContext() const; | 67 ExecutionContext* getExecutionContext() const; |
| 58 | 68 |
| 69 // Determine if it's OK to resolve/reject this promise. |
| 70 bool isValidToFulfillPromise(); |
| 71 |
| 59 private: | 72 private: |
| 60 // Rejects the promise with a DOMException. | |
| 61 void rejectInternal(ExceptionCode, const String& errorMessage); | |
| 62 | |
| 63 Member<ScriptPromiseResolver> m_resolver; | 73 Member<ScriptPromiseResolver> m_resolver; |
| 64 }; | 74 }; |
| 65 | 75 |
| 66 } // namespace blink | 76 } // namespace blink |
| 67 | 77 |
| 68 #endif // ContentDecryptionModuleResultPromise_h | 78 #endif // ContentDecryptionModuleResultPromise_h |
| OLD | NEW |