1 // Copyright 2013 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 "content/renderer/media/webcontentdecryptionmodulesession_impl.h"
6
7 #include "base/callback_helpers.h"
8 #include "base/logging.h"
9 #include "base/strings/string_util.h"
10 #include "third_party/WebKit/public/platform/WebURL.h"
11
12 namespace content {
13
WebContentDecryptionModuleSessionImpl(uint32 session_id,media::MediaKeys * media_keys,Client * client,const SessionClosedCB & session_closed_cb)14 WebContentDecryptionModuleSessionImpl::WebContentDecryptionModuleSessionImpl(
15 uint32 session_id,
16 media::MediaKeys* media_keys,
17 Client* client,
18 const SessionClosedCB& session_closed_cb)
19 : media_keys_(media_keys),
20 client_(client),
21 session_closed_cb_(session_closed_cb),
22 session_id_(session_id) {
23 DCHECK(media_keys_);
24 }
25
26 WebContentDecryptionModuleSessionImpl::
~WebContentDecryptionModuleSessionImpl()27 ~WebContentDecryptionModuleSessionImpl() {
28 }
29
sessionId() const30 blink::WebString WebContentDecryptionModuleSessionImpl::sessionId() const {
31 return web_session_id_;
32 }
33
generateKeyRequest(const blink::WebString & mime_type,const uint8 * init_data,size_t init_data_length)34 void WebContentDecryptionModuleSessionImpl::generateKeyRequest(
35 const blink::WebString& mime_type,
36 const uint8* init_data, size_t init_data_length) {
37 // TODO(ddorwin): Guard against this in supported types check and remove this.
38 // Chromium only supports ASCII MIME types.
39 if (!IsStringASCII(mime_type)) {
40 NOTREACHED();
41 OnSessionError(media::MediaKeys::kUnknownError, 0);
42 return;
43 }
44
45 media_keys_->CreateSession(
46 session_id_, UTF16ToASCII(mime_type), init_data, init_data_length);
47 }
48
update(const uint8 * response,size_t response_length)49 void WebContentDecryptionModuleSessionImpl::update(const uint8* response,
50 size_t response_length) {
51 DCHECK(response);
52 media_keys_->UpdateSession(session_id_, response, response_length);
53 }
54
close()55 void WebContentDecryptionModuleSessionImpl::close() {
56 media_keys_->ReleaseSession(session_id_);
57 }
58
OnSessionCreated(const std::string & web_session_id)59 void WebContentDecryptionModuleSessionImpl::OnSessionCreated(
60 const std::string& web_session_id) {
61 // Due to heartbeat messages, OnSessionCreated() can get called multiple
62 // times.
63 // TODO(jrummell): Once all CDMs are updated to support reference ids,
64 // OnSessionCreated() should only be called once, and the second check can be
65 // removed.
66 blink::WebString id = blink::WebString::fromUTF8(web_session_id);
67 DCHECK(web_session_id_.isEmpty() || web_session_id_ == id)
68 << "Session ID may not be changed once set.";
69 web_session_id_ = id;
70 }
71
OnSessionMessage(const std::vector<uint8> & message,const std::string & destination_url)72 void WebContentDecryptionModuleSessionImpl::OnSessionMessage(
73 const std::vector<uint8>& message,
74 const std::string& destination_url) {
75 client_->keyMessage(message.empty() ? NULL : &message[0],
76 message.size(),
77 GURL(destination_url));
78 }
79
OnSessionReady()80 void WebContentDecryptionModuleSessionImpl::OnSessionReady() {
81 // TODO(jrummell): Blink APIs need to be updated to the new EME API. For now,
82 // convert the response to the old v0.1b API.
83 client_->keyAdded();
84 }
85
OnSessionClosed()86 void WebContentDecryptionModuleSessionImpl::OnSessionClosed() {
87 if (!session_closed_cb_.is_null())
88 base::ResetAndReturn(&session_closed_cb_).Run(session_id_);
89 }
90
OnSessionError(media::MediaKeys::KeyError error_code,int system_code)91 void WebContentDecryptionModuleSessionImpl::OnSessionError(
92 media::MediaKeys::KeyError error_code,
93 int system_code) {
94 client_->keyError(static_cast<Client::MediaKeyErrorCode>(error_code),
95 system_code);
96 }
97
98 } // namespace content
99