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