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

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

Issue 2407013002: EME: Improve promise lifetime (Closed)
Patch Set: refactor check 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)),
46 m_contextDestroyed(false) {}
45 47
46 ContentDecryptionModuleResultPromise::~ContentDecryptionModuleResultPromise() {} 48 ContentDecryptionModuleResultPromise::~ContentDecryptionModuleResultPromise() {}
47 49
48 void ContentDecryptionModuleResultPromise::complete() { 50 void ContentDecryptionModuleResultPromise::complete() {
49 NOTREACHED(); 51 NOTREACHED();
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) {
(...skipping 19 matching lines...) Expand all
74 if (systemCode != 0) { 76 if (systemCode != 0) {
75 if (result.isEmpty()) 77 if (result.isEmpty())
76 result.append("Rejected with system code"); 78 result.append("Rejected with system code");
77 result.append(" ("); 79 result.append(" (");
78 result.appendNumber(systemCode); 80 result.appendNumber(systemCode);
79 result.append(')'); 81 result.append(')');
80 } 82 }
81 reject(WebCdmExceptionToExceptionCode(exceptionCode), result.toString()); 83 reject(WebCdmExceptionToExceptionCode(exceptionCode), result.toString());
82 } 84 }
83 85
86 void ContentDecryptionModuleResultPromise::contextDestroyed() {
87 m_contextDestroyed = true;
88 }
89
84 ScriptPromise ContentDecryptionModuleResultPromise::promise() { 90 ScriptPromise ContentDecryptionModuleResultPromise::promise() {
85 return m_resolver->promise(); 91 return m_resolver->promise();
86 } 92 }
87 93
88 void ContentDecryptionModuleResultPromise::reject(ExceptionCode code, 94 void ContentDecryptionModuleResultPromise::reject(ExceptionCode code,
89 const String& errorMessage) { 95 const String& errorMessage) {
90 // Reject the promise asynchronously. This avoids problems when gc is 96 if (!isValidToFulfillPromise())
91 // destroying objects that result in unfulfilled promises being rejected. 97 return;
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 98
105 void ContentDecryptionModuleResultPromise::rejectInternal(
106 ExceptionCode code,
107 const String& errorMessage) {
108 m_resolver->reject(DOMException::create(code, errorMessage)); 99 m_resolver->reject(DOMException::create(code, errorMessage));
109 m_resolver.clear(); 100 m_resolver.clear();
110 } 101 }
111 102
112 ExecutionContext* ContentDecryptionModuleResultPromise::getExecutionContext() 103 bool ContentDecryptionModuleResultPromise::isValidToFulfillPromise() {
113 const { 104 // getExecutionContext() is no longer valid once contextDestroyed() is called.
114 return m_resolver->getExecutionContext(); 105 if (m_contextDestroyed ||
haraken 2016/10/11 03:51:24 You can replace m_contextDestroyed with getExecuti
jrummell 2016/10/11 21:38:27 Done.
106 getExecutionContext()->activeDOMObjectsAreStopped()) {
107 m_resolver.clear();
haraken 2016/10/11 03:51:24 Why do you need to clear the promise when isValidT
jrummell 2016/10/11 21:38:27 Done. m_resolver was cleared to release the Member
108 return false;
109 }
110
111 return true;
115 } 112 }
116 113
117 DEFINE_TRACE(ContentDecryptionModuleResultPromise) { 114 DEFINE_TRACE(ContentDecryptionModuleResultPromise) {
118 visitor->trace(m_resolver); 115 visitor->trace(m_resolver);
119 ContentDecryptionModuleResult::trace(visitor); 116 ContentDecryptionModuleResult::trace(visitor);
117 ContextLifecycleObserver::trace(visitor);
120 } 118 }
121 119
122 } // namespace blink 120 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698