| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "config.h" |
| 6 #include "modules/encryptedmedia/MediaKeySystemAccess.h" |
| 7 |
| 8 #include "bindings/core/v8/ScriptPromiseResolver.h" |
| 9 #include "bindings/core/v8/ScriptState.h" |
| 10 #include "core/dom/DOMException.h" |
| 11 #include "core/dom/Document.h" |
| 12 #include "core/dom/ExceptionCode.h" |
| 13 #include "modules/encryptedmedia/MediaKeys.h" |
| 14 #include "modules/encryptedmedia/MediaKeysController.h" |
| 15 #include "platform/Logging.h" |
| 16 #include "platform/Timer.h" |
| 17 #include "public/platform/WebContentDecryptionModule.h" |
| 18 |
| 19 namespace blink { |
| 20 |
| 21 // This class allows a MediaKeys object to be created asynchronously. |
| 22 class MediaKeysInitializer : public ScriptPromiseResolver { |
| 23 WTF_MAKE_NONCOPYABLE(MediaKeysInitializer); |
| 24 |
| 25 public: |
| 26 static ScriptPromise create(ScriptState*, const String& keySystem); |
| 27 virtual ~MediaKeysInitializer(); |
| 28 |
| 29 private: |
| 30 MediaKeysInitializer(ScriptState*, const String& keySystem); |
| 31 void timerFired(Timer<MediaKeysInitializer>*); |
| 32 |
| 33 const String m_keySystem; |
| 34 Timer<MediaKeysInitializer> m_timer; |
| 35 }; |
| 36 |
| 37 ScriptPromise MediaKeysInitializer::create(ScriptState* scriptState, const Strin
g& keySystem) |
| 38 { |
| 39 RefPtr<MediaKeysInitializer> initializer = adoptRef(new MediaKeysInitializer
(scriptState, keySystem)); |
| 40 initializer->suspendIfNeeded(); |
| 41 initializer->keepAliveWhilePending(); |
| 42 return initializer->promise(); |
| 43 } |
| 44 |
| 45 MediaKeysInitializer::MediaKeysInitializer(ScriptState* scriptState, const Strin
g& keySystem) |
| 46 : ScriptPromiseResolver(scriptState) |
| 47 , m_keySystem(keySystem) |
| 48 , m_timer(this, &MediaKeysInitializer::timerFired) |
| 49 { |
| 50 WTF_LOG(Media, "MediaKeysInitializer::MediaKeysInitializer"); |
| 51 // Start the timer so that MediaKeys can be created asynchronously. |
| 52 m_timer.startOneShot(0, FROM_HERE); |
| 53 } |
| 54 |
| 55 MediaKeysInitializer::~MediaKeysInitializer() |
| 56 { |
| 57 WTF_LOG(Media, "MediaKeysInitializer::~MediaKeysInitializer"); |
| 58 } |
| 59 |
| 60 void MediaKeysInitializer::timerFired(Timer<MediaKeysInitializer>*) |
| 61 { |
| 62 WTF_LOG(Media, "MediaKeysInitializer::timerFired"); |
| 63 |
| 64 // NOTE: Continued from step 2. of MediaKeySystemAccess::createMediaKeys(). |
| 65 // 2.1 Let cdm be the CDM corresponding to this object. |
| 66 // 2.2 Load and initialize the cdm if necessary. |
| 67 Document* document = toDocument(executionContext()); |
| 68 MediaKeysController* controller = MediaKeysController::from(document->page()
); |
| 69 // FIXME: Should this return an error code so there can be a better error |
| 70 // message than UnknownError? Should it be asynchronous (maybe use |
| 71 // webContentDecryptionModuleResult)? |
| 72 OwnPtr<WebContentDecryptionModule> cdm = controller->createContentDecryption
Module(executionContext(), m_keySystem); |
| 73 |
| 74 // 2.3 If cdm fails to load or initialize, reject promise with a new |
| 75 // DOMException whose name is the appropriate error name. |
| 76 if (!cdm) { |
| 77 String message("A CDM instance could not be created for the '" + m_keySy
stem + "' key system."); |
| 78 reject(DOMException::create(UnknownError, message)); |
| 79 return; |
| 80 } |
| 81 |
| 82 // 2.4 Let media keys be a new MediaKeys object. |
| 83 MediaKeys* mediaKeys = new MediaKeys(executionContext(), m_keySystem, cdm.re
lease()); |
| 84 |
| 85 // 2.5 Resolve promise with media keys. |
| 86 resolve(mediaKeys); |
| 87 |
| 88 // Note: As soon as the promise is resolved (or rejected), the |
| 89 // ScriptPromiseResolver object (|this|) is freed. So access to |
| 90 // any members will crash once the promise is fulfilled. |
| 91 } |
| 92 |
| 93 MediaKeySystemAccess::MediaKeySystemAccess(const String& keySystem) |
| 94 : m_keySystem(keySystem) |
| 95 { |
| 96 // FIXME: There should be a Chromium-side equivalent object that contains |
| 97 // any information, including the key system, from the results of the |
| 98 // request call. It would also give us a place to put the createCDM() call. |
| 99 } |
| 100 |
| 101 MediaKeySystemAccess::~MediaKeySystemAccess() |
| 102 { |
| 103 } |
| 104 |
| 105 ScriptPromise MediaKeySystemAccess::createMediaKeys(ScriptState* scriptState) |
| 106 { |
| 107 // From https://dvcs.w3.org/hg/html-media/raw-file/default/encrypted-media/e
ncrypted-media.html#widl-MediaKeySystemAccess-createMediaKeys-Promise-MediaKeys |
| 108 // When this method is invoked, the user agent must run the following steps: |
| 109 // 1. Let promise be a new promise. |
| 110 // 2. Asynchronously create and initialize the MediaKeys object. |
| 111 // 3. Return promise. |
| 112 return MediaKeysInitializer::create(scriptState, m_keySystem); |
| 113 } |
| 114 |
| 115 void MediaKeySystemAccess::trace(Visitor*) |
| 116 { |
| 117 } |
| 118 |
| 119 } // namespace blink |
| OLD | NEW |