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.
5 #include "media/blink/cdm_session_adapter.h"
8 #include "base/logging.h"
9 #include "base/stl_util.h"
10 #include "media/base/cdm_factory.h"
11 #include "media/base/cdm_key_information.h"
12 #include "media/base/cdm_promise.h"
13 #include "media/base/key_systems.h"
14 #include "media/blink/webcontentdecryptionmodule_impl.h"
15 #include "media/blink/webcontentdecryptionmodulesession_impl.h"
20 const char kMediaEME
[] = "Media.EME.";
21 const char kDot
[] = ".";
23 CdmSessionAdapter::CdmSessionAdapter() : weak_ptr_factory_(this) {
26 CdmSessionAdapter::~CdmSessionAdapter() {}
28 void CdmSessionAdapter::CreateCdm(
29 CdmFactory
* cdm_factory
,
30 const std::string
& key_system
,
31 const GURL
& security_origin
,
32 const CdmConfig
& cdm_config
,
33 blink::WebContentDecryptionModuleResult result
) {
34 // Note: WebContentDecryptionModuleImpl::Create() calls this method without
35 // holding a reference to the CdmSessionAdapter. Bind OnCdmCreated() with
36 // |this| instead of |weak_this| to prevent |this| from being destructed.
37 base::WeakPtr
<CdmSessionAdapter
> weak_this
= weak_ptr_factory_
.GetWeakPtr();
39 key_system
, security_origin
, cdm_config
,
40 base::Bind(&CdmSessionAdapter::OnSessionMessage
, weak_this
),
41 base::Bind(&CdmSessionAdapter::OnSessionClosed
, weak_this
),
42 base::Bind(&CdmSessionAdapter::OnLegacySessionError
, weak_this
),
43 base::Bind(&CdmSessionAdapter::OnSessionKeysChange
, weak_this
),
44 base::Bind(&CdmSessionAdapter::OnSessionExpirationUpdate
, weak_this
),
45 base::Bind(&CdmSessionAdapter::OnCdmCreated
, this, key_system
, result
));
48 void CdmSessionAdapter::SetServerCertificate(
49 const std::vector
<uint8_t>& certificate
,
50 scoped_ptr
<SimpleCdmPromise
> promise
) {
51 cdm_
->SetServerCertificate(certificate
, promise
.Pass());
54 WebContentDecryptionModuleSessionImpl
* CdmSessionAdapter::CreateSession() {
55 return new WebContentDecryptionModuleSessionImpl(this);
58 bool CdmSessionAdapter::RegisterSession(
59 const std::string
& session_id
,
60 base::WeakPtr
<WebContentDecryptionModuleSessionImpl
> session
) {
61 // If this session ID is already registered, don't register it again.
62 if (ContainsKey(sessions_
, session_id
))
65 sessions_
[session_id
] = session
;
69 void CdmSessionAdapter::UnregisterSession(const std::string
& session_id
) {
70 DCHECK(ContainsKey(sessions_
, session_id
));
71 sessions_
.erase(session_id
);
74 void CdmSessionAdapter::InitializeNewSession(
75 EmeInitDataType init_data_type
,
76 const std::vector
<uint8_t>& init_data
,
77 MediaKeys::SessionType session_type
,
78 scoped_ptr
<NewSessionCdmPromise
> promise
) {
79 cdm_
->CreateSessionAndGenerateRequest(session_type
, init_data_type
, init_data
,
83 void CdmSessionAdapter::LoadSession(MediaKeys::SessionType session_type
,
84 const std::string
& session_id
,
85 scoped_ptr
<NewSessionCdmPromise
> promise
) {
86 cdm_
->LoadSession(session_type
, session_id
, promise
.Pass());
89 void CdmSessionAdapter::UpdateSession(const std::string
& session_id
,
90 const std::vector
<uint8_t>& response
,
91 scoped_ptr
<SimpleCdmPromise
> promise
) {
92 cdm_
->UpdateSession(session_id
, response
, promise
.Pass());
95 void CdmSessionAdapter::CloseSession(const std::string
& session_id
,
96 scoped_ptr
<SimpleCdmPromise
> promise
) {
97 cdm_
->CloseSession(session_id
, promise
.Pass());
100 void CdmSessionAdapter::RemoveSession(const std::string
& session_id
,
101 scoped_ptr
<SimpleCdmPromise
> promise
) {
102 cdm_
->RemoveSession(session_id
, promise
.Pass());
105 CdmContext
* CdmSessionAdapter::GetCdmContext() {
106 return cdm_
->GetCdmContext();
109 const std::string
& CdmSessionAdapter::GetKeySystem() const {
113 const std::string
& CdmSessionAdapter::GetKeySystemUMAPrefix() const {
114 return key_system_uma_prefix_
;
117 void CdmSessionAdapter::OnCdmCreated(
118 const std::string
& key_system
,
119 blink::WebContentDecryptionModuleResult result
,
120 scoped_ptr
<MediaKeys
> cdm
,
121 const std::string
& error_message
) {
122 DVLOG(2) << __FUNCTION__
;
124 result
.completeWithError(
125 blink::WebContentDecryptionModuleExceptionNotSupportedError
, 0,
126 blink::WebString::fromUTF8(error_message
));
130 key_system_
= key_system
;
131 key_system_uma_prefix_
=
132 kMediaEME
+ GetKeySystemNameForUMA(key_system
) + kDot
;
135 result
.completeWithContentDecryptionModule(
136 new WebContentDecryptionModuleImpl(this));
139 void CdmSessionAdapter::OnSessionMessage(
140 const std::string
& session_id
,
141 MediaKeys::MessageType message_type
,
142 const std::vector
<uint8_t>& message
,
143 const GURL
& /* legacy_destination_url */) {
144 WebContentDecryptionModuleSessionImpl
* session
= GetSession(session_id
);
145 DLOG_IF(WARNING
, !session
) << __FUNCTION__
<< " for unknown session "
148 session
->OnSessionMessage(message_type
, message
);
151 void CdmSessionAdapter::OnSessionKeysChange(const std::string
& session_id
,
152 bool has_additional_usable_key
,
153 CdmKeysInfo keys_info
) {
154 // TODO(jrummell): Pass |keys_info| on.
155 WebContentDecryptionModuleSessionImpl
* session
= GetSession(session_id
);
156 DLOG_IF(WARNING
, !session
) << __FUNCTION__
<< " for unknown session "
159 session
->OnSessionKeysChange(has_additional_usable_key
, keys_info
.Pass());
162 void CdmSessionAdapter::OnSessionExpirationUpdate(
163 const std::string
& session_id
,
164 const base::Time
& new_expiry_time
) {
165 WebContentDecryptionModuleSessionImpl
* session
= GetSession(session_id
);
166 DLOG_IF(WARNING
, !session
) << __FUNCTION__
<< " for unknown session "
169 session
->OnSessionExpirationUpdate(new_expiry_time
);
172 void CdmSessionAdapter::OnSessionClosed(const std::string
& session_id
) {
173 WebContentDecryptionModuleSessionImpl
* session
= GetSession(session_id
);
174 DLOG_IF(WARNING
, !session
) << __FUNCTION__
<< " for unknown session "
177 session
->OnSessionClosed();
180 void CdmSessionAdapter::OnLegacySessionError(
181 const std::string
& session_id
,
182 MediaKeys::Exception exception_code
,
183 uint32_t system_code
,
184 const std::string
& error_message
) {
185 // Error events not used by unprefixed EME.
186 // TODO(jrummell): Remove when prefixed EME removed.
189 WebContentDecryptionModuleSessionImpl
* CdmSessionAdapter::GetSession(
190 const std::string
& session_id
) {
191 // Since session objects may get garbage collected, it is possible that there
192 // are events coming back from the CDM and the session has been unregistered.
193 // We can not tell if the CDM is firing events at sessions that never existed.
194 SessionMap::iterator session
= sessions_
.find(session_id
);
195 return (session
!= sessions_
.end()) ? session
->second
.get() : NULL
;