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

Side by Side Diff: third_party/WebKit/Source/modules/encryptedmedia/ContentDecryptionModuleResultPromise.cpp

Issue 2407013002: EME: Improve promise lifetime (Closed)
Patch Set: changes Created 4 years, 2 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 unified diff | Download patch
OLDNEW
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 23 matching lines...) Expand all
34 // the EME spec. 34 // the EME spec.
35 return UnknownError; 35 return UnknownError;
36 } 36 }
37 37
38 NOTREACHED(); 38 NOTREACHED();
39 return UnknownError; 39 return UnknownError;
40 } 40 }
41 41
42 ContentDecryptionModuleResultPromise::ContentDecryptionModuleResultPromise( 42 ContentDecryptionModuleResultPromise::ContentDecryptionModuleResultPromise(
43 ScriptState* scriptState) 43 ScriptState* scriptState)
44 : m_resolver(ScriptPromiseResolver::create(scriptState)) {} 44 : ContextLifecycleObserver(scriptState->getExecutionContext()),
45 m_resolver(ScriptPromiseResolver::create(scriptState)) {}
45 46
46 ContentDecryptionModuleResultPromise::~ContentDecryptionModuleResultPromise() {} 47 ContentDecryptionModuleResultPromise::~ContentDecryptionModuleResultPromise() {}
47 48
48 void ContentDecryptionModuleResultPromise::complete() { 49 void ContentDecryptionModuleResultPromise::complete() {
49 NOTREACHED(); 50 NOTREACHED();
50 reject(InvalidStateError, "Unexpected completion."); 51 reject(InvalidStateError, "Unexpected completion.");
51 } 52 }
52 53
53 void ContentDecryptionModuleResultPromise::completeWithContentDecryptionModule( 54 void ContentDecryptionModuleResultPromise::completeWithContentDecryptionModule(
54 WebContentDecryptionModule* cdm) { 55 WebContentDecryptionModule* cdm) {
55 NOTREACHED(); 56 NOTREACHED();
56 reject(InvalidStateError, "Unexpected completion."); 57 reject(InvalidStateError, "Unexpected completion.");
57 } 58 }
58 59
59 void ContentDecryptionModuleResultPromise::completeWithSession( 60 void ContentDecryptionModuleResultPromise::completeWithSession(
60 WebContentDecryptionModuleResult::SessionStatus status) { 61 WebContentDecryptionModuleResult::SessionStatus status) {
61 NOTREACHED(); 62 NOTREACHED();
62 reject(InvalidStateError, "Unexpected completion."); 63 reject(InvalidStateError, "Unexpected completion.");
63 } 64 }
64 65
65 void ContentDecryptionModuleResultPromise::completeWithError( 66 void ContentDecryptionModuleResultPromise::completeWithError(
66 WebContentDecryptionModuleException exceptionCode, 67 WebContentDecryptionModuleException exceptionCode,
67 unsigned long systemCode, 68 unsigned long systemCode,
68 const WebString& errorMessage) { 69 const WebString& errorMessage) {
70 if (!isValidToFulfillPromise())
71 return;
72
69 // Non-zero |systemCode| is appended to the |errorMessage|. If the 73 // Non-zero |systemCode| is appended to the |errorMessage|. If the
70 // |errorMessage| is empty, we'll report "Rejected with system code 74 // |errorMessage| is empty, we'll report "Rejected with system code
71 // (systemCode)". 75 // (systemCode)".
72 StringBuilder result; 76 StringBuilder result;
73 result.append(errorMessage); 77 result.append(errorMessage);
74 if (systemCode != 0) { 78 if (systemCode != 0) {
75 if (result.isEmpty()) 79 if (result.isEmpty())
76 result.append("Rejected with system code"); 80 result.append("Rejected with system code");
77 result.append(" ("); 81 result.append(" (");
78 result.appendNumber(systemCode); 82 result.appendNumber(systemCode);
79 result.append(')'); 83 result.append(')');
80 } 84 }
81 reject(WebCdmExceptionToExceptionCode(exceptionCode), result.toString()); 85 reject(WebCdmExceptionToExceptionCode(exceptionCode), result.toString());
82 } 86 }
83 87
84 ScriptPromise ContentDecryptionModuleResultPromise::promise() { 88 ScriptPromise ContentDecryptionModuleResultPromise::promise() {
85 return m_resolver->promise(); 89 return m_resolver->promise();
86 } 90 }
87 91
88 void ContentDecryptionModuleResultPromise::reject(ExceptionCode code, 92 void ContentDecryptionModuleResultPromise::reject(ExceptionCode code,
89 const String& errorMessage) { 93 const String& errorMessage) {
90 // Reject the promise asynchronously. This avoids problems when gc is 94 DCHECK(isValidToFulfillPromise());
yhirano 2016/10/14 01:05:25 Is this change (from PS3) correct? You once said
jrummell 2016/10/14 02:13:30 All the complete...() methods have the check for i
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 95
105 void ContentDecryptionModuleResultPromise::rejectInternal(
106 ExceptionCode code,
107 const String& errorMessage) {
108 m_resolver->reject(DOMException::create(code, errorMessage)); 96 m_resolver->reject(DOMException::create(code, errorMessage));
109 m_resolver.clear(); 97 m_resolver.clear();
110 } 98 }
111 99
112 ExecutionContext* ContentDecryptionModuleResultPromise::getExecutionContext() 100 bool ContentDecryptionModuleResultPromise::isValidToFulfillPromise() {
113 const { 101 // getExecutionContext() is no longer valid once the context is destroyed.
114 return m_resolver->getExecutionContext(); 102 return getExecutionContext() &&
103 !getExecutionContext()->activeDOMObjectsAreStopped();
115 } 104 }
116 105
117 DEFINE_TRACE(ContentDecryptionModuleResultPromise) { 106 DEFINE_TRACE(ContentDecryptionModuleResultPromise) {
118 visitor->trace(m_resolver); 107 visitor->trace(m_resolver);
119 ContentDecryptionModuleResult::trace(visitor); 108 ContentDecryptionModuleResult::trace(visitor);
109 ContextLifecycleObserver::trace(visitor);
120 } 110 }
121 111
122 } // namespace blink 112 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698