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

Side by Side Diff: Source/modules/encryptedmedia/MediaKeySession.cpp

Issue 687533004: Simplify use of ContentDecryptionModuleResult with promises (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Created 6 years, 1 month 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 /* 1 /*
2 * Copyright (C) 2013 Apple Inc. All rights reserved. 2 * Copyright (C) 2013 Apple Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions 5 * modification, are permitted provided that the following conditions
6 * are met: 6 * are met:
7 * 1. Redistributions of source code must retain the above copyright 7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer. 8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright 9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the 10 * notice, this list of conditions and the following disclaimer in the
(...skipping 18 matching lines...) Expand all
29 #include "bindings/core/v8/DOMWrapperWorld.h" 29 #include "bindings/core/v8/DOMWrapperWorld.h"
30 #include "bindings/core/v8/ScriptPromise.h" 30 #include "bindings/core/v8/ScriptPromise.h"
31 #include "bindings/core/v8/ScriptPromiseResolver.h" 31 #include "bindings/core/v8/ScriptPromiseResolver.h"
32 #include "bindings/core/v8/ScriptState.h" 32 #include "bindings/core/v8/ScriptState.h"
33 #include "core/dom/DOMArrayBuffer.h" 33 #include "core/dom/DOMArrayBuffer.h"
34 #include "core/dom/DOMArrayBufferView.h" 34 #include "core/dom/DOMArrayBufferView.h"
35 #include "core/dom/ExceptionCode.h" 35 #include "core/dom/ExceptionCode.h"
36 #include "core/events/Event.h" 36 #include "core/events/Event.h"
37 #include "core/events/GenericEventQueue.h" 37 #include "core/events/GenericEventQueue.h"
38 #include "core/html/MediaKeyError.h" 38 #include "core/html/MediaKeyError.h"
39 #include "modules/encryptedmedia/ContentDecryptionModuleResultPromise.h"
39 #include "modules/encryptedmedia/MediaKeyMessageEvent.h" 40 #include "modules/encryptedmedia/MediaKeyMessageEvent.h"
40 #include "modules/encryptedmedia/MediaKeys.h" 41 #include "modules/encryptedmedia/MediaKeys.h"
41 #include "modules/encryptedmedia/SimpleContentDecryptionModuleResult.h" 42 #include "modules/encryptedmedia/SimpleContentDecryptionModuleResultPromise.h"
42 #include "platform/ContentDecryptionModuleResult.h" 43 #include "platform/ContentDecryptionModuleResult.h"
43 #include "platform/ContentType.h" 44 #include "platform/ContentType.h"
44 #include "platform/Logging.h" 45 #include "platform/Logging.h"
45 #include "platform/MIMETypeRegistry.h" 46 #include "platform/MIMETypeRegistry.h"
46 #include "platform/Timer.h" 47 #include "platform/Timer.h"
47 #include "public/platform/WebContentDecryptionModule.h" 48 #include "public/platform/WebContentDecryptionModule.h"
48 #include "public/platform/WebContentDecryptionModuleException.h" 49 #include "public/platform/WebContentDecryptionModuleException.h"
49 #include "public/platform/WebContentDecryptionModuleSession.h" 50 #include "public/platform/WebContentDecryptionModuleSession.h"
50 #include "public/platform/WebString.h" 51 #include "public/platform/WebString.h"
51 #include "public/platform/WebURL.h" 52 #include "public/platform/WebURL.h"
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after
194 const Member<ContentDecryptionModuleResult> m_result; 195 const Member<ContentDecryptionModuleResult> m_result;
195 const String m_stringData; 196 const String m_stringData;
196 const RefPtr<DOMArrayBuffer> m_data; 197 const RefPtr<DOMArrayBuffer> m_data;
197 }; 198 };
198 199
199 // This class wraps the promise resolver used when initializing a new session 200 // This class wraps the promise resolver used when initializing a new session
200 // and is passed to Chromium to fullfill the promise. This implementation of 201 // and is passed to Chromium to fullfill the promise. This implementation of
201 // completeWithSession() will resolve the promise with void, while 202 // completeWithSession() will resolve the promise with void, while
202 // completeWithError() will reject the promise with an exception. complete() 203 // completeWithError() will reject the promise with an exception. complete()
203 // is not expected to be called, and will reject the promise. 204 // is not expected to be called, and will reject the promise.
204 class NewSessionResult : public ContentDecryptionModuleResult { 205 class NewSessionResult : public ContentDecryptionModuleResultPromise {
ddorwin 2014/11/11 22:44:09 nit: If the base and Simple* are "...ResultPromise
jrummell 2014/11/11 23:30:11 Done.
205 public: 206 public:
206 NewSessionResult(ScriptState* scriptState, MediaKeySession* session) 207 NewSessionResult(ScriptState* scriptState, MediaKeySession* session)
207 : m_resolver(ScriptPromiseResolver::create(scriptState)) 208 : ContentDecryptionModuleResultPromise(scriptState)
208 , m_session(session) 209 , m_session(session)
209 { 210 {
210 WTF_LOG(Media, "NewSessionResult(%p)", this);
211 } 211 }
212 212
213 virtual ~NewSessionResult() 213 virtual ~NewSessionResult()
214 { 214 {
215 WTF_LOG(Media, "~NewSessionResult(%p)", this);
216 } 215 }
217 216
218 // ContentDecryptionModuleResult implementation. 217 // ContentDecryptionModuleResult implementation.
219 virtual void complete() override
220 {
221 ASSERT_NOT_REACHED();
222 completeWithDOMException(InvalidStateError, "Unexpected completion.");
223 }
224
225 virtual void completeWithSession(WebContentDecryptionModuleResult::SessionSt atus status) override 218 virtual void completeWithSession(WebContentDecryptionModuleResult::SessionSt atus status) override
226 { 219 {
227 if (status != WebContentDecryptionModuleResult::NewSession) { 220 if (status != WebContentDecryptionModuleResult::NewSession) {
228 ASSERT_NOT_REACHED(); 221 ASSERT_NOT_REACHED();
229 completeWithDOMException(InvalidStateError, "Unexpected completion." ); 222 reject(InvalidStateError, "Unexpected completion.");
230 } 223 }
231 224
232 m_session->finishGenerateRequest(); 225 m_session->finishGenerateRequest();
233 m_resolver->resolve(); 226 resolve();
234 m_resolver.clear();
235 } 227 }
236 228
237 virtual void completeWithError(WebContentDecryptionModuleException exception Code, unsigned long systemCode, const WebString& errorMessage) override
238 {
239 completeWithDOMException(WebCdmExceptionToExceptionCode(exceptionCode), errorMessage);
240 }
241
242 // It is only valid to call this before completion.
243 ScriptPromise promise() { return m_resolver->promise(); }
244
245 void trace(Visitor* visitor) 229 void trace(Visitor* visitor)
246 { 230 {
247 visitor->trace(m_session); 231 visitor->trace(m_session);
248 ContentDecryptionModuleResult::trace(visitor); 232 ContentDecryptionModuleResultPromise::trace(visitor);
249 } 233 }
250 234
251 private: 235 private:
252 // Reject the promise with a DOMException.
253 void completeWithDOMException(ExceptionCode code, const String& errorMessage )
254 {
255 m_resolver->reject(DOMException::create(code, errorMessage));
256 m_resolver.clear();
257 }
258
259 RefPtr<ScriptPromiseResolver> m_resolver;
260 Member<MediaKeySession> m_session; 236 Member<MediaKeySession> m_session;
261 }; 237 };
262 238
263 // This class wraps the promise resolver used when loading a session 239 // This class wraps the promise resolver used when loading a session
264 // and is passed to Chromium to fullfill the promise. This implementation of 240 // and is passed to Chromium to fullfill the promise. This implementation of
265 // completeWithSession() will resolve the promise with true/false, while 241 // completeWithSession() will resolve the promise with true/false, while
266 // completeWithError() will reject the promise with an exception. complete() 242 // completeWithError() will reject the promise with an exception. complete()
267 // is not expected to be called, and will reject the promise. 243 // is not expected to be called, and will reject the promise.
268 class LoadSessionResult : public ContentDecryptionModuleResult { 244 class LoadSessionResult : public ContentDecryptionModuleResultPromise {
269 public: 245 public:
270 LoadSessionResult(ScriptState* scriptState, MediaKeySession* session) 246 LoadSessionResult(ScriptState* scriptState, MediaKeySession* session)
271 : m_resolver(ScriptPromiseResolver::create(scriptState)) 247 : ContentDecryptionModuleResultPromise(scriptState)
272 , m_session(session) 248 , m_session(session)
273 { 249 {
274 WTF_LOG(Media, "LoadSessionResult(%p)", this);
275 } 250 }
276 251
277 virtual ~LoadSessionResult() 252 virtual ~LoadSessionResult()
278 { 253 {
279 WTF_LOG(Media, "~LoadSessionResult(%p)", this);
280 } 254 }
281 255
282 // ContentDecryptionModuleResult implementation. 256 // ContentDecryptionModuleResult implementation.
283 virtual void complete() override
284 {
285 ASSERT_NOT_REACHED();
286 completeWithDOMException(InvalidStateError, "Unexpected completion.");
287 }
288
289 virtual void completeWithSession(WebContentDecryptionModuleResult::SessionSt atus status) override 257 virtual void completeWithSession(WebContentDecryptionModuleResult::SessionSt atus status) override
290 { 258 {
291 bool result = false; 259 bool result = false;
292 switch (status) { 260 switch (status) {
293 case WebContentDecryptionModuleResult::NewSession: 261 case WebContentDecryptionModuleResult::NewSession:
294 result = true; 262 result = true;
295 break; 263 break;
296 264
297 case WebContentDecryptionModuleResult::SessionNotFound: 265 case WebContentDecryptionModuleResult::SessionNotFound:
298 result = false; 266 result = false;
299 break; 267 break;
300 268
301 case WebContentDecryptionModuleResult::SessionAlreadyExists: 269 case WebContentDecryptionModuleResult::SessionAlreadyExists:
302 ASSERT_NOT_REACHED(); 270 ASSERT_NOT_REACHED();
303 completeWithDOMException(InvalidStateError, "Unexpected completion." ); 271 reject(InvalidStateError, "Unexpected completion.");
304 return; 272 return;
305 } 273 }
306 274
307 m_session->finishLoad(); 275 m_session->finishLoad();
308 m_resolver->resolve(result); 276 resolve(result);
309 m_resolver.clear();
310 } 277 }
311 278
312 virtual void completeWithError(WebContentDecryptionModuleException exception Code, unsigned long systemCode, const WebString& errorMessage) override
313 {
314 completeWithDOMException(WebCdmExceptionToExceptionCode(exceptionCode), errorMessage);
315 }
316
317 // It is only valid to call this before completion.
318 ScriptPromise promise() { return m_resolver->promise(); }
319
320 void trace(Visitor* visitor) 279 void trace(Visitor* visitor)
321 { 280 {
322 visitor->trace(m_session); 281 visitor->trace(m_session);
323 ContentDecryptionModuleResult::trace(visitor); 282 ContentDecryptionModuleResultPromise::trace(visitor);
324 } 283 }
325 284
326 private: 285 private:
327 // Reject the promise with a DOMException.
328 void completeWithDOMException(ExceptionCode code, const String& errorMessage )
329 {
330 m_resolver->reject(DOMException::create(code, errorMessage));
331 m_resolver.clear();
332 }
333
334 RefPtr<ScriptPromiseResolver> m_resolver;
335 Member<MediaKeySession> m_session; 286 Member<MediaKeySession> m_session;
336 }; 287 };
337 288
338 MediaKeySession* MediaKeySession::create(ScriptState* scriptState, MediaKeys* me diaKeys, const String& sessionType) 289 MediaKeySession* MediaKeySession::create(ScriptState* scriptState, MediaKeys* me diaKeys, const String& sessionType)
339 { 290 {
340 ASSERT(sessionType == kTemporary || sessionType == kPersistent); 291 ASSERT(sessionType == kTemporary || sessionType == kPersistent);
341 RefPtrWillBeRawPtr<MediaKeySession> session = new MediaKeySession(scriptStat e, mediaKeys, sessionType); 292 RefPtrWillBeRawPtr<MediaKeySession> session = new MediaKeySession(scriptStat e, mediaKeys, sessionType);
342 session->suspendIfNeeded(); 293 session->suspendIfNeeded();
343 return session.get(); 294 return session.get();
344 } 295 }
(...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after
575 // message "The response parameter is empty." 526 // message "The response parameter is empty."
576 if (!response->byteLength()) { 527 if (!response->byteLength()) {
577 return ScriptPromise::rejectWithDOMException( 528 return ScriptPromise::rejectWithDOMException(
578 scriptState, DOMException::create(InvalidAccessError, "The response parameter is empty.")); 529 scriptState, DOMException::create(InvalidAccessError, "The response parameter is empty."));
579 } 530 }
580 531
581 // 2. Let message be a copy of the contents of the response parameter. 532 // 2. Let message be a copy of the contents of the response parameter.
582 // (Copied in the caller.) 533 // (Copied in the caller.)
583 534
584 // 3. Let promise be a new promise. 535 // 3. Let promise be a new promise.
585 SimpleContentDecryptionModuleResult* result = new SimpleContentDecryptionMod uleResult(scriptState); 536 SimpleContentDecryptionModuleResultPromise* result = new SimpleContentDecryp tionModuleResultPromise(scriptState);
586 ScriptPromise promise = result->promise(); 537 ScriptPromise promise = result->promise();
587 538
588 // 4. Run the following steps asynchronously (documented in 539 // 4. Run the following steps asynchronously (documented in
589 // actionTimerFired()) 540 // actionTimerFired())
590 m_pendingActions.append(PendingAction::CreatePendingUpdate(result, response) ); 541 m_pendingActions.append(PendingAction::CreatePendingUpdate(result, response) );
591 if (!m_actionTimer.isActive()) 542 if (!m_actionTimer.isActive())
592 m_actionTimer.startOneShot(0, FROM_HERE); 543 m_actionTimer.startOneShot(0, FROM_HERE);
593 544
594 // 5. Return promise. 545 // 5. Return promise.
595 return promise; 546 return promise;
(...skipping 16 matching lines...) Expand all
612 return ScriptPromise::rejectWithDOMException( 563 return ScriptPromise::rejectWithDOMException(
613 scriptState, DOMException::create(InvalidStateError, "The session is not callable.")); 564 scriptState, DOMException::create(InvalidStateError, "The session is not callable."));
614 } 565 }
615 566
616 // 2. If the Session Close algorithm has been run on this object, 567 // 2. If the Session Close algorithm has been run on this object,
617 // return a resolved promise. 568 // return a resolved promise.
618 if (m_isClosed) 569 if (m_isClosed)
619 return ScriptPromise::cast(scriptState, ScriptValue()); 570 return ScriptPromise::cast(scriptState, ScriptValue());
620 571
621 // 3. Let promise be a new promise. 572 // 3. Let promise be a new promise.
622 SimpleContentDecryptionModuleResult* result = new SimpleContentDecryptionMod uleResult(scriptState); 573 SimpleContentDecryptionModuleResultPromise* result = new SimpleContentDecryp tionModuleResultPromise(scriptState);
623 ScriptPromise promise = result->promise(); 574 ScriptPromise promise = result->promise();
624 575
625 // 4. Run the following steps asynchronously (documented in 576 // 4. Run the following steps asynchronously (documented in
626 // actionTimerFired()). 577 // actionTimerFired()).
627 m_pendingActions.append(PendingAction::CreatePendingClose(result)); 578 m_pendingActions.append(PendingAction::CreatePendingClose(result));
628 if (!m_actionTimer.isActive()) 579 if (!m_actionTimer.isActive())
629 m_actionTimer.startOneShot(0, FROM_HERE); 580 m_actionTimer.startOneShot(0, FROM_HERE);
630 581
631 // 5. Return promise. 582 // 5. Return promise.
632 return promise; 583 return promise;
(...skipping 23 matching lines...) Expand all
656 607
657 // 3. If the Session Close algorithm has been run on this object, return a 608 // 3. If the Session Close algorithm has been run on this object, return a
658 // promise rejected with a new DOMException whose name is 609 // promise rejected with a new DOMException whose name is
659 // "InvalidStateError". 610 // "InvalidStateError".
660 if (m_isClosed) { 611 if (m_isClosed) {
661 return ScriptPromise::rejectWithDOMException( 612 return ScriptPromise::rejectWithDOMException(
662 scriptState, DOMException::create(InvalidStateError, "The session is already closed.")); 613 scriptState, DOMException::create(InvalidStateError, "The session is already closed."));
663 } 614 }
664 615
665 // 4. Let promise be a new promise. 616 // 4. Let promise be a new promise.
666 SimpleContentDecryptionModuleResult* result = new SimpleContentDecryptionMod uleResult(scriptState); 617 SimpleContentDecryptionModuleResultPromise* result = new SimpleContentDecryp tionModuleResultPromise(scriptState);
667 ScriptPromise promise = result->promise(); 618 ScriptPromise promise = result->promise();
668 619
669 // 5. Run the following steps asynchronously (documented in 620 // 5. Run the following steps asynchronously (documented in
670 // actionTimerFired()). 621 // actionTimerFired()).
671 m_pendingActions.append(PendingAction::CreatePendingRemove(result)); 622 m_pendingActions.append(PendingAction::CreatePendingRemove(result));
672 if (!m_actionTimer.isActive()) 623 if (!m_actionTimer.isActive())
673 m_actionTimer.startOneShot(0, FROM_HERE); 624 m_actionTimer.startOneShot(0, FROM_HERE);
674 625
675 // 6. Return promise. 626 // 6. Return promise.
676 return promise; 627 return promise;
(...skipping 325 matching lines...) Expand 10 before | Expand all | Expand 10 after
1002 { 953 {
1003 visitor->trace(m_error); 954 visitor->trace(m_error);
1004 visitor->trace(m_asyncEventQueue); 955 visitor->trace(m_asyncEventQueue);
1005 visitor->trace(m_pendingActions); 956 visitor->trace(m_pendingActions);
1006 visitor->trace(m_mediaKeys); 957 visitor->trace(m_mediaKeys);
1007 visitor->trace(m_closedPromise); 958 visitor->trace(m_closedPromise);
1008 EventTargetWithInlineData::trace(visitor); 959 EventTargetWithInlineData::trace(visitor);
1009 } 960 }
1010 961
1011 } // namespace blink 962 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698