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/memory/weak_ptr.h"
10 #include "base/stl_util.h"
11 #include "media/base/cdm_factory.h"
12 #include "media/base/cdm_key_information.h"
13 #include "media/base/cdm_promise.h"
14 #include "media/base/key_systems.h"
15 #include "media/base/media_keys.h"
16 #include "media/blink/webcontentdecryptionmodule_impl.h"
17 #include "media/blink/webcontentdecryptionmodulesession_impl.h"
22 const char kMediaEME
[] = "Media.EME.";
23 const char kDot
[] = ".";
25 CdmSessionAdapter::CdmSessionAdapter() : weak_ptr_factory_(this) {
28 CdmSessionAdapter::~CdmSessionAdapter() {}
30 void CdmSessionAdapter::CreateCdm(
31 CdmFactory
* cdm_factory
,
32 const std::string
& key_system
,
33 bool allow_distinctive_identifier
,
34 bool allow_persistent_state
,
35 const GURL
& security_origin
,
36 blink::WebContentDecryptionModuleResult result
) {
37 // Note: WebContentDecryptionModuleImpl::Create() calls this method without
38 // holding a reference to the CdmSessionAdapter. Bind OnCdmCreated() with
39 // |this| instead of |weak_this| to prevent |this| from being desctructed.
40 base::WeakPtr
<CdmSessionAdapter
> weak_this
= weak_ptr_factory_
.GetWeakPtr();
42 key_system
, allow_distinctive_identifier
, allow_persistent_state
,
44 base::Bind(&CdmSessionAdapter::OnSessionMessage
, weak_this
),
45 base::Bind(&CdmSessionAdapter::OnSessionClosed
, weak_this
),
46 base::Bind(&CdmSessionAdapter::OnLegacySessionError
, weak_this
),
47 base::Bind(&CdmSessionAdapter::OnSessionKeysChange
, weak_this
),
48 base::Bind(&CdmSessionAdapter::OnSessionExpirationUpdate
, weak_this
),
49 base::Bind(&CdmSessionAdapter::OnCdmCreated
, this, key_system
, result
));
52 void CdmSessionAdapter::SetServerCertificate(
53 const uint8
* server_certificate
,
54 int server_certificate_length
,
55 scoped_ptr
<SimpleCdmPromise
> promise
) {
56 cdm_
->SetServerCertificate(server_certificate
, server_certificate_length
,
60 WebContentDecryptionModuleSessionImpl
* CdmSessionAdapter::CreateSession() {
61 return new WebContentDecryptionModuleSessionImpl(this);
64 bool CdmSessionAdapter::RegisterSession(
65 const std::string
& session_id
,
66 base::WeakPtr
<WebContentDecryptionModuleSessionImpl
> session
) {
67 // If this session ID is already registered, don't register it again.
68 if (ContainsKey(sessions_
, session_id
))
71 sessions_
[session_id
] = session
;
75 void CdmSessionAdapter::UnregisterSession(const std::string
& session_id
) {
76 DCHECK(ContainsKey(sessions_
, session_id
));
77 sessions_
.erase(session_id
);
80 void CdmSessionAdapter::InitializeNewSession(
81 EmeInitDataType init_data_type
,
82 const uint8
* init_data
,
84 MediaKeys::SessionType session_type
,
85 scoped_ptr
<NewSessionCdmPromise
> promise
) {
86 cdm_
->CreateSessionAndGenerateRequest(session_type
, init_data_type
, init_data
,
87 init_data_length
, promise
.Pass());
90 void CdmSessionAdapter::LoadSession(MediaKeys::SessionType session_type
,
91 const std::string
& session_id
,
92 scoped_ptr
<NewSessionCdmPromise
> promise
) {
93 cdm_
->LoadSession(session_type
, session_id
, promise
.Pass());
96 void CdmSessionAdapter::UpdateSession(const std::string
& session_id
,
97 const uint8
* response
,
99 scoped_ptr
<SimpleCdmPromise
> promise
) {
100 cdm_
->UpdateSession(session_id
, response
, response_length
, promise
.Pass());
103 void CdmSessionAdapter::CloseSession(const std::string
& session_id
,
104 scoped_ptr
<SimpleCdmPromise
> promise
) {
105 cdm_
->CloseSession(session_id
, promise
.Pass());
108 void CdmSessionAdapter::RemoveSession(const std::string
& session_id
,
109 scoped_ptr
<SimpleCdmPromise
> promise
) {
110 cdm_
->RemoveSession(session_id
, promise
.Pass());
113 CdmContext
* CdmSessionAdapter::GetCdmContext() {
114 return cdm_
->GetCdmContext();
117 const std::string
& CdmSessionAdapter::GetKeySystem() const {
121 const std::string
& CdmSessionAdapter::GetKeySystemUMAPrefix() const {
122 return key_system_uma_prefix_
;
125 void CdmSessionAdapter::OnCdmCreated(
126 const std::string
& key_system
,
127 blink::WebContentDecryptionModuleResult result
,
128 scoped_ptr
<MediaKeys
> cdm
) {
129 DVLOG(2) << __FUNCTION__
;
131 result
.completeWithError(
132 blink::WebContentDecryptionModuleExceptionNotSupportedError
, 0,
133 "Failed to create the CDM instance.");
137 key_system_
= key_system
;
138 key_system_uma_prefix_
=
139 kMediaEME
+ GetKeySystemNameForUMA(key_system
) + kDot
;
142 result
.completeWithContentDecryptionModule(
143 new WebContentDecryptionModuleImpl(this));
146 void CdmSessionAdapter::OnSessionMessage(
147 const std::string
& session_id
,
148 MediaKeys::MessageType message_type
,
149 const std::vector
<uint8
>& message
,
150 const GURL
& /* legacy_destination_url */) {
151 WebContentDecryptionModuleSessionImpl
* session
= GetSession(session_id
);
152 DLOG_IF(WARNING
, !session
) << __FUNCTION__
<< " for unknown session "
155 session
->OnSessionMessage(message_type
, message
);
158 void CdmSessionAdapter::OnSessionKeysChange(const std::string
& session_id
,
159 bool has_additional_usable_key
,
160 CdmKeysInfo keys_info
) {
161 // TODO(jrummell): Pass |keys_info| on.
162 WebContentDecryptionModuleSessionImpl
* session
= GetSession(session_id
);
163 DLOG_IF(WARNING
, !session
) << __FUNCTION__
<< " for unknown session "
166 session
->OnSessionKeysChange(has_additional_usable_key
, keys_info
.Pass());
169 void CdmSessionAdapter::OnSessionExpirationUpdate(
170 const std::string
& session_id
,
171 const base::Time
& new_expiry_time
) {
172 WebContentDecryptionModuleSessionImpl
* session
= GetSession(session_id
);
173 DLOG_IF(WARNING
, !session
) << __FUNCTION__
<< " for unknown session "
176 session
->OnSessionExpirationUpdate(new_expiry_time
);
179 void CdmSessionAdapter::OnSessionClosed(const std::string
& session_id
) {
180 WebContentDecryptionModuleSessionImpl
* session
= GetSession(session_id
);
181 DLOG_IF(WARNING
, !session
) << __FUNCTION__
<< " for unknown session "
184 session
->OnSessionClosed();
187 void CdmSessionAdapter::OnLegacySessionError(
188 const std::string
& session_id
,
189 MediaKeys::Exception exception_code
,
191 const std::string
& error_message
) {
192 // Error events not used by unprefixed EME.
193 // TODO(jrummell): Remove when prefixed EME removed.
196 WebContentDecryptionModuleSessionImpl
* CdmSessionAdapter::GetSession(
197 const std::string
& session_id
) {
198 // Since session objects may get garbage collected, it is possible that there
199 // are events coming back from the CDM and the session has been unregistered.
200 // We can not tell if the CDM is firing events at sessions that never existed.
201 SessionMap::iterator session
= sessions_
.find(session_id
);
202 return (session
!= sessions_
.end()) ? session
->second
.get() : NULL
;