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

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

Issue 543173002: Implement MediaKeySession.generateRequest() (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: create early Created 6 years, 3 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 /* 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 20 matching lines...) Expand all
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/ExceptionCode.h" 33 #include "core/dom/ExceptionCode.h"
34 #include "core/events/Event.h" 34 #include "core/events/Event.h"
35 #include "core/events/GenericEventQueue.h" 35 #include "core/events/GenericEventQueue.h"
36 #include "core/html/MediaKeyError.h" 36 #include "core/html/MediaKeyError.h"
37 #include "modules/encryptedmedia/MediaKeyMessageEvent.h" 37 #include "modules/encryptedmedia/MediaKeyMessageEvent.h"
38 #include "modules/encryptedmedia/MediaKeys.h" 38 #include "modules/encryptedmedia/MediaKeys.h"
39 #include "modules/encryptedmedia/SimpleContentDecryptionModuleResult.h" 39 #include "modules/encryptedmedia/SimpleContentDecryptionModuleResult.h"
40 #include "platform/ContentDecryptionModuleResult.h" 40 #include "platform/ContentDecryptionModuleResult.h"
41 #include "platform/ContentType.h"
41 #include "platform/Logging.h" 42 #include "platform/Logging.h"
43 #include "platform/MIMETypeRegistry.h"
42 #include "platform/Timer.h" 44 #include "platform/Timer.h"
43 #include "public/platform/WebContentDecryptionModule.h" 45 #include "public/platform/WebContentDecryptionModule.h"
44 #include "public/platform/WebContentDecryptionModuleException.h" 46 #include "public/platform/WebContentDecryptionModuleException.h"
45 #include "public/platform/WebContentDecryptionModuleSession.h" 47 #include "public/platform/WebContentDecryptionModuleSession.h"
46 #include "public/platform/WebString.h" 48 #include "public/platform/WebString.h"
47 #include "public/platform/WebURL.h" 49 #include "public/platform/WebURL.h"
48 #include "wtf/ArrayBuffer.h" 50 #include "wtf/ArrayBuffer.h"
49 #include "wtf/ArrayBufferView.h" 51 #include "wtf/ArrayBufferView.h"
50 52
51 namespace blink { 53 namespace blink {
52 54
55 static bool isKeySystemSupportedWithInitDataType(const String& keySystem, const String& initDataType)
56 {
57 ASSERT(!keySystem.isEmpty());
58
59 // FIXME: initDataType != contentType. Implement this properly.
60 // http://crbug.com/385874.
61 String contentType = initDataType;
62 if (initDataType == "webm") {
63 contentType = "video/webm";
64 } else if (initDataType == "cenc") {
65 contentType = "video/mp4";
66 }
67
68 ContentType type(contentType);
69 return MIMETypeRegistry::isSupportedEncryptedMediaMIMEType(keySystem, type.t ype(), type.parameter("codecs"));
70 }
71
53 // A class holding a pending action. 72 // A class holding a pending action.
54 class MediaKeySession::PendingAction : public GarbageCollectedFinalized<MediaKey Session::PendingAction> { 73 class MediaKeySession::PendingAction : public GarbageCollectedFinalized<MediaKey Session::PendingAction> {
55 public: 74 public:
56 enum Type { 75 enum Type {
76 GenerateRequest,
57 Update, 77 Update,
58 Release, 78 Release
59 Message
60 }; 79 };
61 80
62 Type type() const { return m_type; } 81 Type type() const { return m_type; }
63 82
64 const Persistent<ContentDecryptionModuleResult> result() const 83 const Persistent<ContentDecryptionModuleResult> result() const
65 { 84 {
66 ASSERT(m_type == Update || m_type == Release);
67 return m_result; 85 return m_result;
68 } 86 }
69 87
70 const RefPtr<ArrayBuffer> data() const 88 const RefPtr<ArrayBuffer> data() const
71 { 89 {
72 ASSERT(m_type == Update); 90 ASSERT(m_type == GenerateRequest || m_type == Update);
73 return m_data; 91 return m_data;
74 } 92 }
75 93
76 RefPtrWillBeRawPtr<Event> event() 94 const String& initDataType() const
77 { 95 {
78 ASSERT(m_type == Message); 96 ASSERT(m_type == GenerateRequest);
79 return m_event; 97 return m_initDataType;
98 }
99
100 static PendingAction* CreatePendingGenerateRequest(ContentDecryptionModuleRe sult* result, const String& initDataType, PassRefPtr<ArrayBuffer> initData)
101 {
102 ASSERT(result);
103 ASSERT(initData);
104 return new PendingAction(GenerateRequest, result, initDataType, initData );
80 } 105 }
81 106
82 static PendingAction* CreatePendingUpdate(ContentDecryptionModuleResult* res ult, PassRefPtr<ArrayBuffer> data) 107 static PendingAction* CreatePendingUpdate(ContentDecryptionModuleResult* res ult, PassRefPtr<ArrayBuffer> data)
83 { 108 {
84 ASSERT(result); 109 ASSERT(result);
85 ASSERT(data); 110 ASSERT(data);
86 return new PendingAction(Update, result, data); 111 return new PendingAction(Update, result, String(), data);
87 } 112 }
88 113
89 static PendingAction* CreatePendingRelease(ContentDecryptionModuleResult* re sult) 114 static PendingAction* CreatePendingRelease(ContentDecryptionModuleResult* re sult)
90 { 115 {
91 ASSERT(result); 116 ASSERT(result);
92 return new PendingAction(Release, result, PassRefPtr<ArrayBuffer>()); 117 return new PendingAction(Release, result, String(), PassRefPtr<ArrayBuff er>());
93 }
94
95 static PendingAction* CreatePendingMessage(PassRefPtrWillBeRawPtr<Event> eve nt)
96 {
97 ASSERT(event);
98 return new PendingAction(Message, event);
99 } 118 }
100 119
101 ~PendingAction() 120 ~PendingAction()
102 { 121 {
103 } 122 }
104 123
105 void trace(Visitor* visitor) 124 void trace(Visitor* visitor)
106 { 125 {
107 visitor->trace(m_result); 126 visitor->trace(m_result);
108 visitor->trace(m_event);
109 } 127 }
110 128
111 private: 129 private:
112 PendingAction(Type type, ContentDecryptionModuleResult* result, PassRefPtr<A rrayBuffer> data) 130 PendingAction(Type type, ContentDecryptionModuleResult* result, const String & initDataType, PassRefPtr<ArrayBuffer> data)
113 : m_type(type) 131 : m_type(type)
114 , m_result(result) 132 , m_result(result)
133 , m_initDataType(initDataType)
115 , m_data(data) 134 , m_data(data)
116 { 135 {
117 } 136 }
118 137
119 PendingAction(Type type, PassRefPtrWillBeRawPtr<Event> event)
120 : m_type(type)
121 , m_event(event)
122 {
123 }
124
125 const Type m_type; 138 const Type m_type;
126 const Member<ContentDecryptionModuleResult> m_result; 139 const Member<ContentDecryptionModuleResult> m_result;
140 const String m_initDataType;
127 const RefPtr<ArrayBuffer> m_data; 141 const RefPtr<ArrayBuffer> m_data;
128 const RefPtrWillBeMember<Event> m_event;
129 }; 142 };
130 143
131 // This class allows a MediaKeySession object to be created asynchronously. 144 // This class wraps the promise resolver used when initializing a new session
132 class MediaKeySessionInitializer : public ScriptPromiseResolver { 145 // and is passed to Chromium to fullfill the promise. This implementation of
133 WTF_MAKE_NONCOPYABLE(MediaKeySessionInitializer); 146 // completeWithSession() will resolve the promise with void, while
147 // completeWithError() will reject the promise with an exception. complete()
148 // is not expected to be called, and will reject the promise.
149 class NewSessionResult : public ContentDecryptionModuleResult {
150 public:
151 NewSessionResult(ScriptState* scriptState, MediaKeySession* session)
152 : m_resolver(ScriptPromiseResolver::create(scriptState))
153 , m_session(session)
154 {
155 WTF_LOG(Media, "NewSessionResult(%p)", this);
156 }
134 157
135 public: 158 virtual ~NewSessionResult()
136 static ScriptPromise create(ScriptState*, MediaKeys*, const String& initData Type, PassRefPtr<ArrayBuffer> initData, const String& sessionType);
137 virtual ~MediaKeySessionInitializer();
138
139 void completeWithSession(WebContentDecryptionModuleResult::SessionStatus);
140 void completeWithDOMException(ExceptionCode, const String& errorMessage);
141
142 private:
143 MediaKeySessionInitializer(ScriptState*, MediaKeys*, const String& initDataT ype, PassRefPtr<ArrayBuffer> initData, const String& sessionType);
144 void timerFired(Timer<MediaKeySessionInitializer>*);
145
146 Persistent<MediaKeys> m_mediaKeys;
147 OwnPtr<WebContentDecryptionModuleSession> m_cdmSession;
148
149 // The next 3 values are simply the initialization data saved so that the
150 // asynchronous creation has the data needed.
151 String m_initDataType;
152 RefPtr<ArrayBuffer> m_initData;
153 String m_sessionType;
154
155 Timer<MediaKeySessionInitializer> m_timer;
156 };
157
158 // Represents the result used when a new WebContentDecryptionModuleSession
159 // object has been created. Needed as MediaKeySessionInitializer can't be both
160 // a ScriptPromiseResolver and ContentDecryptionModuleResult at the same time.
161 class NewMediaKeySessionResult FINAL : public ContentDecryptionModuleResult {
162 public:
163 NewMediaKeySessionResult(MediaKeySessionInitializer* initializer)
164 : m_initializer(initializer)
165 { 159 {
160 WTF_LOG(Media, "~NewSessionResult(%p)", this);
166 } 161 }
167 162
168 // ContentDecryptionModuleResult implementation. 163 // ContentDecryptionModuleResult implementation.
169 virtual void complete() OVERRIDE 164 virtual void complete() OVERRIDE
170 { 165 {
171 ASSERT_NOT_REACHED(); 166 ASSERT_NOT_REACHED();
172 m_initializer->completeWithDOMException(InvalidStateError, "Unexpected c ompletion."); 167 completeWithDOMException(InvalidStateError, "Unexpected completion.");
173 } 168 }
174 169
175 virtual void completeWithSession(WebContentDecryptionModuleResult::SessionSt atus status) OVERRIDE 170 virtual void completeWithSession(WebContentDecryptionModuleResult::SessionSt atus status) OVERRIDE
176 { 171 {
177 m_initializer->completeWithSession(status); 172 if (status != WebContentDecryptionModuleResult::NewSession) {
173 ASSERT_NOT_REACHED();
174 completeWithDOMException(InvalidStateError, "Unexpected completion." );
175 }
176
177 m_session->finishGenerateRequest();
178
179 // This should resolve(void). The current V8 implementation doesn't
180 // distinguish between no parameter vs. one undefined parameter, so
181 // this has the correct effect.
ddorwin 2014/09/09 21:35:25 We should probably have a Blink bug to support Pro
jrummell 2014/09/10 01:18:25 There is resolve(), so switched to that and remove
182 m_resolver->resolve(V8UndefinedType());
183 m_resolver.clear();
178 } 184 }
179 185
180 virtual void completeWithError(WebContentDecryptionModuleException code, uns igned long systemCode, const WebString& message) OVERRIDE 186 virtual void completeWithError(WebContentDecryptionModuleException exception Code, unsigned long systemCode, const WebString& errorMessage) OVERRIDE
181 { 187 {
182 m_initializer->completeWithDOMException(WebCdmExceptionToExceptionCode(c ode), message); 188 completeWithDOMException(WebCdmExceptionToExceptionCode(exceptionCode), errorMessage);
189 }
190
191 // It is only valid to call this before completion.
192 ScriptPromise promise() { return m_resolver->promise(); }
193
194 void trace(Visitor* visitor)
195 {
196 visitor->trace(m_session);
197 ContentDecryptionModuleResult::trace(visitor);
183 } 198 }
184 199
185 private: 200 private:
186 MediaKeySessionInitializer* m_initializer; 201 // Reject the promise with a DOMException.
202 void completeWithDOMException(ExceptionCode code, const String& errorMessage )
203 {
204 m_resolver->reject(DOMException::create(code, errorMessage));
205 m_resolver.clear();
206 }
207
208 RefPtr<ScriptPromiseResolver> m_resolver;
209 Member<MediaKeySession> m_session;
187 }; 210 };
188 211
189 ScriptPromise MediaKeySessionInitializer::create(ScriptState* scriptState, Media Keys* mediaKeys, const String& initDataType, PassRefPtr<ArrayBuffer> initData, c onst String& sessionType) 212 MediaKeySession* MediaKeySession::create(ScriptState* scriptState, MediaKeys* me diaKeys, const String& sessionType)
190 { 213 {
191 RefPtr<MediaKeySessionInitializer> initializer = adoptRef(new MediaKeySessio nInitializer(scriptState, mediaKeys, initDataType, initData, sessionType)); 214 RefPtrWillBeRawPtr<MediaKeySession> session = adoptRefCountedGarbageCollecte dWillBeNoop(new MediaKeySession(scriptState, mediaKeys, sessionType));
192 initializer->suspendIfNeeded(); 215 session->suspendIfNeeded();
193 initializer->keepAliveWhilePending(); 216 return session.get();
194 return initializer->promise();
195 } 217 }
196 218
197 MediaKeySessionInitializer::MediaKeySessionInitializer(ScriptState* scriptState, MediaKeys* mediaKeys, const String& initDataType, PassRefPtr<ArrayBuffer> initD ata, const String& sessionType) 219 MediaKeySession::MediaKeySession(ScriptState* scriptState, MediaKeys* mediaKeys, const String& sessionType)
198 : ScriptPromiseResolver(scriptState) 220 : ActiveDOMObject(scriptState->executionContext())
221 , m_keySystem(mediaKeys->keySystem())
222 , m_asyncEventQueue(GenericEventQueue::create(this))
199 , m_mediaKeys(mediaKeys) 223 , m_mediaKeys(mediaKeys)
200 , m_initDataType(initDataType)
201 , m_initData(initData)
202 , m_sessionType(sessionType) 224 , m_sessionType(sessionType)
203 , m_timer(this, &MediaKeySessionInitializer::timerFired) 225 , m_isUninitialized(true)
204 { 226 , m_isCallable(false)
205 WTF_LOG(Media, "MediaKeySessionInitializer::MediaKeySessionInitializer");
206
207 // Start the timer so that MediaKeySession can be created asynchronously.
208 m_timer.startOneShot(0, FROM_HERE);
209 }
210
211 MediaKeySessionInitializer::~MediaKeySessionInitializer()
212 {
213 WTF_LOG(Media, "MediaKeySessionInitializer::~MediaKeySessionInitializer");
214 }
215
216 void MediaKeySessionInitializer::timerFired(Timer<MediaKeySessionInitializer>*)
217 {
218 WTF_LOG(Media, "MediaKeySessionInitializer::timerFired");
219
220 // Continue MediaKeys::createSession() at step 7.
221 // 7.1 Let request be null. (Request provided by cdm in message event).
222 // 7.2 Let default URL be null. (Also provided by cdm in message event).
223
224 // 7.3 Let cdm be the cdm loaded in create().
225 WebContentDecryptionModule* cdm = m_mediaKeys->contentDecryptionModule();
226
227 // 7.4 Use the cdm to execute the following steps:
228 // 7.4.1 If the init data is not valid for initDataType, reject promise
229 // with a new DOMException whose name is "InvalidAccessError".
230 // 7.4.2 If the init data is not supported by the cdm, reject promise with
231 // a new DOMException whose name is "NotSupportedError".
232 // 7.4.3 Let request be a request (e.g. a license request) generated based
233 // on the init data, which is interpreteted per initDataType, and
234 // sessionType. If sessionType is "temporary", the request is for a
235 // temporary non-persisted license. If sessionType is "persistent",
236 // the request is for a persistable license.
237 // 7.4.4 If the init data indicates a default URL, let default URL be
238 // that URL. The URL may be validated and/or normalized.
239 m_cdmSession = adoptPtr(cdm->createSession());
240 NewMediaKeySessionResult* result = new NewMediaKeySessionResult(this);
241 m_cdmSession->initializeNewSession(m_initDataType, static_cast<unsigned char *>(m_initData->data()), m_initData->byteLength(), m_sessionType, result->result( ));
242
243 WTF_LOG(Media, "MediaKeySessionInitializer::timerFired done");
244 // Note: As soon as the promise is resolved (or rejected), the
245 // ScriptPromiseResolver object (|this|) is freed. So if
246 // initializeNewSession() is synchronous, access to any members will crash.
247 }
248
249 void MediaKeySessionInitializer::completeWithSession(WebContentDecryptionModuleR esult::SessionStatus status)
250 {
251 WTF_LOG(Media, "MediaKeySessionInitializer::completeWithSession");
252
253 switch (status) {
254 case WebContentDecryptionModuleResult::NewSession: {
255 // Resume MediaKeys::createSession().
256 // 7.5 Let the session ID be a unique Session ID string. It may be
257 // obtained from cdm (it is).
258 // 7.6 Let session be a new MediaKeySession object, and initialize it.
259 // (Object was created previously, complete the steps for 7.6).
260 RefPtrWillBeRawPtr<MediaKeySession> session = adoptRefCountedGarbageColl ectedWillBeNoop(new MediaKeySession(executionContext(), m_mediaKeys, m_cdmSessio n.release()));
261 session->suspendIfNeeded();
262
263 // 7.7 If any of the preceding steps failed, reject promise with a
264 // new DOMException whose name is the appropriate error name
265 // and that has an appropriate message.
266 // (Implemented by CDM/Chromium calling completeWithError()).
267
268 // 7.8 Add an entry for the value of the sessionId attribute to the
269 // list of active session IDs for this object.
270 // (Implemented in SessionIdAdapter).
271
272 // 7.9 Run the Queue a "message" Event algorithm on the session,
273 // providing request and default URL.
274 // (Done by the CDM).
275
276 // 7.10 Resolve promise with session.
277 resolve(session.release());
278 WTF_LOG(Media, "MediaKeySessionInitializer::completeWithSession done w/s ession");
279 return;
280 }
281
282 case WebContentDecryptionModuleResult::SessionNotFound:
283 // Step 4.7.1 of MediaKeys::loadSession(): If there is no data
284 // stored for the sessionId in the origin, resolve promise with
285 // undefined.
286 resolve(V8UndefinedType());
287 WTF_LOG(Media, "MediaKeySessionInitializer::completeWithSession done w/u ndefined");
288 return;
289
290 case WebContentDecryptionModuleResult::SessionAlreadyExists:
291 // If a session already exists, resolve the promise with null.
292 resolve(V8NullType());
293 WTF_LOG(Media, "MediaKeySessionInitializer::completeWithSession done w/n ull");
294 return;
295 }
296 ASSERT_NOT_REACHED();
297 }
298
299 void MediaKeySessionInitializer::completeWithDOMException(ExceptionCode code, co nst String& errorMessage)
300 {
301 WTF_LOG(Media, "MediaKeySessionInitializer::completeWithDOMException");
302 reject(DOMException::create(code, errorMessage));
303 }
304
305 ScriptPromise MediaKeySession::create(ScriptState* scriptState, MediaKeys* media Keys, const String& initDataType, PassRefPtr<ArrayBuffer> initData, const String & sessionType)
306 {
307 // Since creation is done asynchronously, use MediaKeySessionInitializer
308 // to do it.
309 return MediaKeySessionInitializer::create(scriptState, mediaKeys, initDataTy pe, initData, sessionType);
310 }
311
312 MediaKeySession::MediaKeySession(ExecutionContext* context, MediaKeys* keys, Pas sOwnPtr<WebContentDecryptionModuleSession> cdmSession)
313 : ActiveDOMObject(context)
314 , m_keySystem(keys->keySystem())
315 , m_asyncEventQueue(GenericEventQueue::create(this))
316 , m_session(cdmSession)
317 , m_keys(keys)
318 , m_isClosed(false) 227 , m_isClosed(false)
319 , m_closedPromise(new ClosedPromise(context, this, ClosedPromise::Closed)) 228 , m_closedPromise(new ClosedPromise(scriptState->executionContext(), this, C losedPromise::Closed))
320 , m_actionTimer(this, &MediaKeySession::actionTimerFired) 229 , m_actionTimer(this, &MediaKeySession::actionTimerFired)
321 { 230 {
322 WTF_LOG(Media, "MediaKeySession(%p)::MediaKeySession", this); 231 WTF_LOG(Media, "MediaKeySession(%p)::MediaKeySession", this);
323 ScriptWrappable::init(this); 232 ScriptWrappable::init(this);
233
234 // Create the matching Chromium object. It will not be usable until
235 // initializeNewSession() is called in response to the user calling
236 // generateRequest().
237 WebContentDecryptionModule* cdm = mediaKeys->contentDecryptionModule();
238 m_session = adoptPtr(cdm->createSession());
324 m_session->setClientInterface(this); 239 m_session->setClientInterface(this);
ddorwin 2014/09/09 21:35:25 cleanup: Can we now pass the client to createSessi
jrummell 2014/09/10 01:18:25 The code is still there on the Chromium side, but
325 240
326 // Resume MediaKeys::createSession() at step 7.6. 241 // MediaKeys::createSession(), step 2.
327 // 7.6.1 Set the error attribute to null. 242 // 2.1 Let the sessionId attribute be the empty string.
328 ASSERT(!m_error); 243 ASSERT(sessionId().isEmpty());
329 244
330 // 7.6.2 Set the sessionId attribute to session ID. 245 // 2.2 Let the expiration attribute be NaN.
331 ASSERT(!sessionId().isEmpty()); 246 // FIXME: Add expiration property.
332 247
333 // 7.6.3 Let expiration be NaN. 248 // 2.3 Let the closed attribute be a new promise.
334 // 7.6.4 Let closed be a new promise. 249 ASSERT(!closed(scriptState).isUndefinedOrNull());
335 // 7.6.5 Let the session type be sessionType. 250
336 // FIXME: Implement the previous 3 values. 251 // 2.4 Let the session type be sessionType.
252 ASSERT(sessionType == m_sessionType);
253
254 // 2.5 Let uninitialized be true.
255 ASSERT(m_isUninitialized);
256
257 // 2.6 Let callable be false.
258 ASSERT(!m_isCallable);
337 } 259 }
338 260
339 MediaKeySession::~MediaKeySession() 261 MediaKeySession::~MediaKeySession()
340 { 262 {
341 WTF_LOG(Media, "MediaKeySession(%p)::~MediaKeySession", this); 263 WTF_LOG(Media, "MediaKeySession(%p)::~MediaKeySession", this);
342 m_session.clear(); 264 m_session.clear();
343 #if !ENABLE(OILPAN) 265 #if !ENABLE(OILPAN)
344 // MediaKeySession and m_asyncEventQueue always become unreachable 266 // MediaKeySession and m_asyncEventQueue always become unreachable
345 // together. So MediaKeySession and m_asyncEventQueue are destructed in the 267 // together. So MediaKeySession and m_asyncEventQueue are destructed in the
346 // same GC. We don't need to call cancelAllEvents explicitly in Oilpan. 268 // same GC. We don't need to call cancelAllEvents explicitly in Oilpan.
347 m_asyncEventQueue->cancelAllEvents(); 269 m_asyncEventQueue->cancelAllEvents();
348 #endif 270 #endif
349 } 271 }
350 272
351 void MediaKeySession::setError(MediaKeyError* error) 273 void MediaKeySession::setError(MediaKeyError* error)
352 { 274 {
353 m_error = error; 275 m_error = error;
354 } 276 }
355 277
356 String MediaKeySession::sessionId() const 278 String MediaKeySession::sessionId() const
357 { 279 {
358 return m_session->sessionId(); 280 return m_session->sessionId();
359 } 281 }
360 282
361 ScriptPromise MediaKeySession::closed(ScriptState* scriptState) 283 ScriptPromise MediaKeySession::closed(ScriptState* scriptState)
362 { 284 {
363 return m_closedPromise->promise(scriptState->world()); 285 return m_closedPromise->promise(scriptState->world());
364 } 286 }
365 287
288 ScriptPromise MediaKeySession::generateRequest(ScriptState* scriptState, const S tring& initDataType, ArrayBuffer* initData)
289 {
290 RefPtr<ArrayBuffer> initDataCopy = ArrayBuffer::create(initData->data(), ini tData->byteLength());
291 return generateRequestInternal(scriptState, initDataType, initDataCopy.relea se());
292 }
293
294 ScriptPromise MediaKeySession::generateRequest(ScriptState* scriptState, const S tring& initDataType, ArrayBufferView* initData)
295 {
296 RefPtr<ArrayBuffer> initDataCopy = ArrayBuffer::create(initData->baseAddress (), initData->byteLength());
297 return generateRequestInternal(scriptState, initDataType, initDataCopy.relea se());
298 }
299
300 ScriptPromise MediaKeySession::generateRequestInternal(ScriptState* scriptState, const String& initDataType, PassRefPtr<ArrayBuffer> initData)
301 {
302 WTF_LOG(Media, "MediaKeySession(%p)::generateRequest %s", this, initDataType .ascii().data());
303
304 // From https://dvcs.w3.org/hg/html-media/raw-file/default/encrypted-media/e ncrypted-media.html#dom-generaterequest:
305 // The generateRequest(initDataType, initData) method creates a new session
306 // for the specified initData. It must run the following steps:
307
308 // 1. If this object's uninitialized value is false, return a promise
309 // rejected with a new DOMException whose name is "InvalidStateError".
310 if (!m_isUninitialized) {
311 return ScriptPromise::rejectWithDOMException(
312 scriptState, DOMException::create(InvalidStateError, "The session is already initialized."));
313 }
314
315 // 2. Let this object's uninitialized be false.
316 m_isUninitialized = false;
317
318 // 3. If initDataType is an empty string, return a promise rejected with a
319 // new DOMException whose name is "InvalidAccessError".
320 if (initDataType.isEmpty()) {
321 return ScriptPromise::rejectWithDOMException(
322 scriptState, DOMException::create(InvalidAccessError, "The initDataT ype parameter is empty."));
323 }
324
325 // 4. If initData is an empty array, return a promise rejected with a new
326 // DOMException whose name is"InvalidAccessError".
327 if (!initData->byteLength()) {
328 return ScriptPromise::rejectWithDOMException(
329 scriptState, DOMException::create(InvalidAccessError, "The initData parameter is empty."));
330 }
331
332 // 5. Let media keys be the MediaKeys object that created this object.
333 // (Use m_mediaKey, which was set in the constructor.)
334
335 // 6. If the content decryption module corresponding to media keys's
336 // keySystem attribute does not support initDataType as an initialization
337 // data type, return a promise rejected with a new DOMException whose
338 // name is "NotSupportedError". String comparison is case-sensitive.
339 if (!isKeySystemSupportedWithInitDataType(m_keySystem, initDataType)) {
340 return ScriptPromise::rejectWithDOMException(
341 scriptState, DOMException::create(NotSupportedError, "The initializa tion data type '" + initDataType + "' is not supported by the key system."));
342 }
343
344 // 7. Let init data be a copy of the contents of the initData parameter.
345 // (Done before calling this method.)
346
347 // 8. Let session type be this object's session type.
348 // (Done in constructor.)
349
350 // 9. Let promise be a new promise.
351 NewSessionResult* result = new NewSessionResult(scriptState, this);
352 ScriptPromise promise = result->promise();
353
354 // 10. Run the following steps asynchronously (documented in
355 // actionTimerFired())
356 m_pendingActions.append(PendingAction::CreatePendingGenerateRequest(result, initDataType, initData));
357 ASSERT(!m_actionTimer.isActive());
358 m_actionTimer.startOneShot(0, FROM_HERE);
359
360 // 11. Return promise.
361 return promise;
362 }
363
366 ScriptPromise MediaKeySession::update(ScriptState* scriptState, ArrayBuffer* res ponse) 364 ScriptPromise MediaKeySession::update(ScriptState* scriptState, ArrayBuffer* res ponse)
367 { 365 {
368 RefPtr<ArrayBuffer> responseCopy = ArrayBuffer::create(response->data(), res ponse->byteLength()); 366 RefPtr<ArrayBuffer> responseCopy = ArrayBuffer::create(response->data(), res ponse->byteLength());
369 return updateInternal(scriptState, responseCopy.release()); 367 return updateInternal(scriptState, responseCopy.release());
370 } 368 }
371 369
372 ScriptPromise MediaKeySession::update(ScriptState* scriptState, ArrayBufferView* response) 370 ScriptPromise MediaKeySession::update(ScriptState* scriptState, ArrayBufferView* response)
373 { 371 {
374 RefPtr<ArrayBuffer> responseCopy = ArrayBuffer::create(response->baseAddress (), response->byteLength()); 372 RefPtr<ArrayBuffer> responseCopy = ArrayBuffer::create(response->baseAddress (), response->byteLength());
375 return updateInternal(scriptState, responseCopy.release()); 373 return updateInternal(scriptState, responseCopy.release());
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
449 // Resolving promises now run synchronously and may result in additional 447 // Resolving promises now run synchronously and may result in additional
450 // actions getting added to the queue. As a result, swap the queue to 448 // actions getting added to the queue. As a result, swap the queue to
451 // a local copy to avoid problems if this happens. 449 // a local copy to avoid problems if this happens.
452 HeapDeque<Member<PendingAction> > pendingActions; 450 HeapDeque<Member<PendingAction> > pendingActions;
453 pendingActions.swap(m_pendingActions); 451 pendingActions.swap(m_pendingActions);
454 452
455 while (!pendingActions.isEmpty()) { 453 while (!pendingActions.isEmpty()) {
456 PendingAction* action = pendingActions.takeFirst(); 454 PendingAction* action = pendingActions.takeFirst();
457 455
458 switch (action->type()) { 456 switch (action->type()) {
457 case PendingAction::GenerateRequest:
458 WTF_LOG(Media, "MediaKeySession(%p)::actionTimerFired: GenerateReque st", this);
459
460 // 10.1 Let request be null.
461 // 10.2 Let cdm be the CDM loaded during the initialization of
462 // media keys.
463 // 10.3 Use the cdm to execute the following steps:
464 // 10.3.1 If the init data is not valid for initDataType, reject
465 // promise with a new DOMException whose name is
466 // "InvalidAccessError".
467 // 10.3.2 If the init data is not supported by the cdm, reject
468 // promise with a new DOMException whose name is
469 // "NotSupportedError".
470 // 10.3.3 Let request be a request (e.g. a license request)
471 // generated based on the init data, which is interpreted
472 // per initDataType, and session type.
473 m_session->initializeNewSession(action->initDataType(), static_cast< unsigned char*>(action->data()->data()), action->data()->byteLength(), m_session Type, action->result()->result());
474
475 // Remainder of steps executed in finishGenerateRequest(), called
476 // when |result| is resolved.
477 break;
478
459 case PendingAction::Update: 479 case PendingAction::Update:
460 WTF_LOG(Media, "MediaKeySession(%p)::actionTimerFired: Update", this ); 480 WTF_LOG(Media, "MediaKeySession(%p)::actionTimerFired: Update", this );
461 // NOTE: Continued from step 4 of MediaKeySession::update(). 481 // NOTE: Continued from step 4 of MediaKeySession::update().
462 // Continue the update call by passing message to the cdm. Once 482 // Continue the update call by passing message to the cdm. Once
463 // completed, it will resolve/reject the promise. 483 // completed, it will resolve/reject the promise.
464 m_session->update(static_cast<unsigned char*>(action->data()->data() ), action->data()->byteLength(), action->result()->result()); 484 m_session->update(static_cast<unsigned char*>(action->data()->data() ), action->data()->byteLength(), action->result()->result());
465 break; 485 break;
486
466 case PendingAction::Release: 487 case PendingAction::Release:
467 WTF_LOG(Media, "MediaKeySession(%p)::actionTimerFired: Release", thi s); 488 WTF_LOG(Media, "MediaKeySession(%p)::actionTimerFired: Release", thi s);
468 // NOTE: Continued from step 3 of MediaKeySession::release(). 489 // NOTE: Continued from step 3 of MediaKeySession::release().
469 // 3.1 Let cdm be the cdm loaded in create(). 490 // 3.1 Let cdm be the cdm loaded in create().
470 // 3.2 Use the cdm to execute the following steps: 491 // 3.2 Use the cdm to execute the following steps:
471 // 3.2.1 Process the close request. Do not remove stored session dat a. 492 // 3.2.1 Process the close request. Do not remove stored session dat a.
472 // 3.2.2 If the previous step caused the session to be closed, run t he 493 // 3.2.2 If the previous step caused the session to be closed, run t he
473 // Session Close algorithm on this object. 494 // Session Close algorithm on this object.
474 // 3.3 Resolve promise with undefined. 495 // 3.3 Resolve promise with undefined.
475 m_session->release(action->result()->result()); 496 m_session->release(action->result()->result());
476 break; 497 break;
477 case PendingAction::Message:
478 WTF_LOG(Media, "MediaKeySession(%p)::actionTimerFired: Message", thi s);
479 m_asyncEventQueue->enqueueEvent(action->event().release());
480 break;
481 } 498 }
482 } 499 }
483 } 500 }
484 501
485 // Queue a task to fire a simple event named keymessage at the new object 502 void MediaKeySession::finishGenerateRequest()
503 {
504 // 10.4 Set the sessionId attribute to a unique Session ID string.
505 // It may be obtained from cdm.
506 // (Done by call in sessionId()).
jrummell 2014/09/09 19:56:00 Oops. Forgot to add the ASSERT. Done locally, will
507
508 // 10.5 If any of the preceding steps failed, reject promise with a new
509 // DOMException whose name is the appropriate error name.
510 // (Done by call to completeWithError()).
511
512 // 10.6 Add an entry for the value of the sessionId attribute to
513 // media keys's list of active session IDs.
514 // FIXME: Is this required?
515 // https://www.w3.org/Bugs/Public/show_bug.cgi?id=26758
516
517 // 10.7 Run the Queue a "message" Event algorithm on the session,
518 // providing request and null.
519 // (Done by the CDM).
520
521 // 10.8 Let this object's callable be true.
522 m_isCallable = true;
523 }
524
525 // Queue a task to fire a simple event named keymessage at the new object.
486 void MediaKeySession::message(const unsigned char* message, size_t messageLength , const WebURL& destinationURL) 526 void MediaKeySession::message(const unsigned char* message, size_t messageLength , const WebURL& destinationURL)
487 { 527 {
488 WTF_LOG(Media, "MediaKeySession(%p)::message", this); 528 WTF_LOG(Media, "MediaKeySession(%p)::message", this);
529 ASSERT(m_isCallable);
ddorwin 2014/09/09 21:35:24 It may not be obvious why this is necessary. We sh
jrummell 2014/09/10 01:18:25 ASSERT is a macro, but added comment.
489 530
490 MediaKeyMessageEventInit init; 531 MediaKeyMessageEventInit init;
491 init.bubbles = false; 532 init.bubbles = false;
492 init.cancelable = false; 533 init.cancelable = false;
493 init.message = ArrayBuffer::create(static_cast<const void*>(message), messag eLength); 534 init.message = ArrayBuffer::create(static_cast<const void*>(message), messag eLength);
494 init.destinationURL = destinationURL.string(); 535 init.destinationURL = destinationURL.string();
495 536
496 RefPtrWillBeRawPtr<MediaKeyMessageEvent> event = MediaKeyMessageEvent::creat e(EventTypeNames::message, init); 537 RefPtrWillBeRawPtr<MediaKeyMessageEvent> event = MediaKeyMessageEvent::creat e(EventTypeNames::message, init);
497 event->setTarget(this); 538 event->setTarget(this);
498
499 if (!hasEventListeners()) {
500 // Since this event may be generated immediately after resolving the
501 // CreateSession() promise, it is possible that the JavaScript hasn't
502 // had time to run the .then() action and bind any necessary event
503 // handlers. If there are no event handlers connected, delay enqueuing
504 // this message to provide time for the JavaScript to run. This will
505 // also affect the (rare) case where there is no message handler
506 // attched during normal operation.
507 m_pendingActions.append(PendingAction::CreatePendingMessage(event.releas e()));
508 if (!m_actionTimer.isActive())
509 m_actionTimer.startOneShot(0, FROM_HERE);
510 return;
511 }
512
513 m_asyncEventQueue->enqueueEvent(event.release()); 539 m_asyncEventQueue->enqueueEvent(event.release());
514 } 540 }
515 541
516 void MediaKeySession::ready() 542 void MediaKeySession::ready()
517 { 543 {
518 WTF_LOG(Media, "MediaKeySession(%p)::ready", this); 544 WTF_LOG(Media, "MediaKeySession(%p)::ready", this);
519 545
520 RefPtrWillBeRawPtr<Event> event = Event::create(EventTypeNames::ready); 546 RefPtrWillBeRawPtr<Event> event = Event::create(EventTypeNames::ready);
521 event->setTarget(this); 547 event->setTarget(this);
522 m_asyncEventQueue->enqueueEvent(event.release()); 548 m_asyncEventQueue->enqueueEvent(event.release());
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
593 } 619 }
594 620
595 bool MediaKeySession::hasPendingActivity() const 621 bool MediaKeySession::hasPendingActivity() const
596 { 622 {
597 // Remain around if there are pending events or MediaKeys is still around 623 // Remain around if there are pending events or MediaKeys is still around
598 // and we're not closed. 624 // and we're not closed.
599 WTF_LOG(Media, "MediaKeySession(%p)::hasPendingActivity %s%s%s%s", this, 625 WTF_LOG(Media, "MediaKeySession(%p)::hasPendingActivity %s%s%s%s", this,
600 ActiveDOMObject::hasPendingActivity() ? " ActiveDOMObject::hasPendingAct ivity()" : "", 626 ActiveDOMObject::hasPendingActivity() ? " ActiveDOMObject::hasPendingAct ivity()" : "",
601 !m_pendingActions.isEmpty() ? " !m_pendingActions.isEmpty()" : "", 627 !m_pendingActions.isEmpty() ? " !m_pendingActions.isEmpty()" : "",
602 m_asyncEventQueue->hasPendingEvents() ? " m_asyncEventQueue->hasPendingE vents()" : "", 628 m_asyncEventQueue->hasPendingEvents() ? " m_asyncEventQueue->hasPendingE vents()" : "",
603 (m_keys && !m_isClosed) ? " m_keys && !m_isClosed" : ""); 629 (m_mediaKeys && !m_isClosed) ? " m_mediaKeys && !m_isClosed" : "");
604 630
605 return ActiveDOMObject::hasPendingActivity() 631 return ActiveDOMObject::hasPendingActivity()
606 || !m_pendingActions.isEmpty() 632 || !m_pendingActions.isEmpty()
607 || m_asyncEventQueue->hasPendingEvents() 633 || m_asyncEventQueue->hasPendingEvents()
608 || (m_keys && !m_isClosed); 634 || (m_mediaKeys && !m_isClosed);
609 } 635 }
610 636
611 void MediaKeySession::stop() 637 void MediaKeySession::stop()
612 { 638 {
613 // Stop the CDM from firing any more events for this session. 639 // Stop the CDM from firing any more events for this session.
614 m_session.clear(); 640 m_session.clear();
615 m_isClosed = true; 641 m_isClosed = true;
616 642
617 if (m_actionTimer.isActive()) 643 if (m_actionTimer.isActive())
618 m_actionTimer.stop(); 644 m_actionTimer.stop();
619 m_pendingActions.clear(); 645 m_pendingActions.clear();
620 m_asyncEventQueue->close(); 646 m_asyncEventQueue->close();
621 } 647 }
622 648
623 void MediaKeySession::trace(Visitor* visitor) 649 void MediaKeySession::trace(Visitor* visitor)
624 { 650 {
625 visitor->trace(m_error); 651 visitor->trace(m_error);
626 visitor->trace(m_asyncEventQueue); 652 visitor->trace(m_asyncEventQueue);
627 visitor->trace(m_pendingActions); 653 visitor->trace(m_pendingActions);
628 visitor->trace(m_keys); 654 visitor->trace(m_mediaKeys);
629 visitor->trace(m_closedPromise); 655 visitor->trace(m_closedPromise);
630 EventTargetWithInlineData::trace(visitor); 656 EventTargetWithInlineData::trace(visitor);
631 } 657 }
632 658
633 } // namespace blink 659 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698