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

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

Issue 2407013002: EME: Improve promise lifetime (Closed)
Patch Set: remove ContextLifecycleObserver 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 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
59 void ContentDecryptionModuleResultPromise::completeWithSession( 59 void ContentDecryptionModuleResultPromise::completeWithSession(
60 WebContentDecryptionModuleResult::SessionStatus status) { 60 WebContentDecryptionModuleResult::SessionStatus status) {
61 NOTREACHED(); 61 NOTREACHED();
62 reject(InvalidStateError, "Unexpected completion."); 62 reject(InvalidStateError, "Unexpected completion.");
63 } 63 }
64 64
65 void ContentDecryptionModuleResultPromise::completeWithError( 65 void ContentDecryptionModuleResultPromise::completeWithError(
66 WebContentDecryptionModuleException exceptionCode, 66 WebContentDecryptionModuleException exceptionCode,
67 unsigned long systemCode, 67 unsigned long systemCode,
68 const WebString& errorMessage) { 68 const WebString& errorMessage) {
69 if (!isValidToFulfillPromise())
70 return;
71
69 // Non-zero |systemCode| is appended to the |errorMessage|. If the 72 // Non-zero |systemCode| is appended to the |errorMessage|. If the
70 // |errorMessage| is empty, we'll report "Rejected with system code 73 // |errorMessage| is empty, we'll report "Rejected with system code
71 // (systemCode)". 74 // (systemCode)".
72 StringBuilder result; 75 StringBuilder result;
73 result.append(errorMessage); 76 result.append(errorMessage);
74 if (systemCode != 0) { 77 if (systemCode != 0) {
75 if (result.isEmpty()) 78 if (result.isEmpty())
76 result.append("Rejected with system code"); 79 result.append("Rejected with system code");
77 result.append(" ("); 80 result.append(" (");
78 result.appendNumber(systemCode); 81 result.appendNumber(systemCode);
79 result.append(')'); 82 result.append(')');
80 } 83 }
81 reject(WebCdmExceptionToExceptionCode(exceptionCode), result.toString()); 84 reject(WebCdmExceptionToExceptionCode(exceptionCode), result.toString());
82 } 85 }
83 86
84 ScriptPromise ContentDecryptionModuleResultPromise::promise() { 87 ScriptPromise ContentDecryptionModuleResultPromise::promise() {
85 return m_resolver->promise(); 88 return m_resolver->promise();
86 } 89 }
87 90
88 void ContentDecryptionModuleResultPromise::reject(ExceptionCode code, 91 void ContentDecryptionModuleResultPromise::reject(ExceptionCode code,
89 const String& errorMessage) { 92 const String& errorMessage) {
90 // Reject the promise asynchronously. This avoids problems when gc is 93 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 94
105 void ContentDecryptionModuleResultPromise::rejectInternal(
106 ExceptionCode code,
107 const String& errorMessage) {
108 m_resolver->reject(DOMException::create(code, errorMessage)); 95 m_resolver->reject(DOMException::create(code, errorMessage));
109 m_resolver.clear(); 96 m_resolver.clear();
110 } 97 }
111 98
112 ExecutionContext* ContentDecryptionModuleResultPromise::getExecutionContext() 99 ExecutionContext* ContentDecryptionModuleResultPromise::getExecutionContext()
113 const { 100 const {
114 return m_resolver->getExecutionContext(); 101 return m_resolver->getExecutionContext();
115 } 102 }
116 103
104 bool ContentDecryptionModuleResultPromise::isValidToFulfillPromise() {
105 // getExecutionContext() is no longer valid once the context is destroyed.
106 return getExecutionContext() &&
107 !getExecutionContext()->activeDOMObjectsAreStopped();
haraken 2016/10/14 00:48:41 Add a comment about why we need to check getExecut
jrummell 2016/10/14 02:13:30 Done.
108 }
109
117 DEFINE_TRACE(ContentDecryptionModuleResultPromise) { 110 DEFINE_TRACE(ContentDecryptionModuleResultPromise) {
118 visitor->trace(m_resolver); 111 visitor->trace(m_resolver);
119 ContentDecryptionModuleResult::trace(visitor); 112 ContentDecryptionModuleResult::trace(visitor);
120 } 113 }
121 114
122 } // namespace blink 115 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698