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/webcontentdecryptionmodulesession_impl.h"
21 const char kMediaEME
[] = "Media.EME.";
22 const char kDot
[] = ".";
24 CdmSessionAdapter::CdmSessionAdapter() : weak_ptr_factory_(this) {
27 CdmSessionAdapter::~CdmSessionAdapter() {}
29 bool CdmSessionAdapter::Initialize(CdmFactory
* cdm_factory
,
30 const std::string
& key_system
,
31 bool allow_distinctive_identifier
,
32 bool allow_persistent_state
,
33 const GURL
& security_origin
) {
34 key_system_
= key_system
;
35 key_system_uma_prefix_
=
36 kMediaEME
+ GetKeySystemNameForUMA(key_system
) + kDot
;
38 base::WeakPtr
<CdmSessionAdapter
> weak_this
= weak_ptr_factory_
.GetWeakPtr();
39 media_keys_
= cdm_factory
->Create(
40 key_system
, allow_distinctive_identifier
, allow_persistent_state
,
42 base::Bind(&CdmSessionAdapter::OnSessionMessage
, weak_this
),
43 base::Bind(&CdmSessionAdapter::OnSessionClosed
, weak_this
),
44 base::Bind(&CdmSessionAdapter::OnLegacySessionError
, weak_this
),
45 base::Bind(&CdmSessionAdapter::OnSessionKeysChange
, weak_this
),
46 base::Bind(&CdmSessionAdapter::OnSessionExpirationUpdate
, weak_this
));
47 return media_keys_
.get() != nullptr;
50 void CdmSessionAdapter::SetServerCertificate(
51 const uint8
* server_certificate
,
52 int server_certificate_length
,
53 scoped_ptr
<SimpleCdmPromise
> promise
) {
54 media_keys_
->SetServerCertificate(
55 server_certificate
, server_certificate_length
, promise
.Pass());
58 WebContentDecryptionModuleSessionImpl
* CdmSessionAdapter::CreateSession() {
59 return new WebContentDecryptionModuleSessionImpl(this);
62 bool CdmSessionAdapter::RegisterSession(
63 const std::string
& session_id
,
64 base::WeakPtr
<WebContentDecryptionModuleSessionImpl
> session
) {
65 // If this session ID is already registered, don't register it again.
66 if (ContainsKey(sessions_
, session_id
))
69 sessions_
[session_id
] = session
;
73 void CdmSessionAdapter::UnregisterSession(const std::string
& session_id
) {
74 DCHECK(ContainsKey(sessions_
, session_id
));
75 sessions_
.erase(session_id
);
78 void CdmSessionAdapter::InitializeNewSession(
79 EmeInitDataType init_data_type
,
80 const uint8
* init_data
,
82 MediaKeys::SessionType session_type
,
83 scoped_ptr
<NewSessionCdmPromise
> promise
) {
84 media_keys_
->CreateSessionAndGenerateRequest(session_type
, init_data_type
,
85 init_data
, init_data_length
,
89 void CdmSessionAdapter::LoadSession(MediaKeys::SessionType session_type
,
90 const std::string
& session_id
,
91 scoped_ptr
<NewSessionCdmPromise
> promise
) {
92 media_keys_
->LoadSession(session_type
, session_id
, promise
.Pass());
95 void CdmSessionAdapter::UpdateSession(const std::string
& session_id
,
96 const uint8
* response
,
98 scoped_ptr
<SimpleCdmPromise
> promise
) {
99 media_keys_
->UpdateSession(session_id
, response
, response_length
,
103 void CdmSessionAdapter::CloseSession(const std::string
& session_id
,
104 scoped_ptr
<SimpleCdmPromise
> promise
) {
105 media_keys_
->CloseSession(session_id
, promise
.Pass());
108 void CdmSessionAdapter::RemoveSession(const std::string
& session_id
,
109 scoped_ptr
<SimpleCdmPromise
> promise
) {
110 media_keys_
->RemoveSession(session_id
, promise
.Pass());
113 CdmContext
* CdmSessionAdapter::GetCdmContext() {
114 return media_keys_
->GetCdmContext();
117 const std::string
& CdmSessionAdapter::GetKeySystem() const {
121 const std::string
& CdmSessionAdapter::GetKeySystemUMAPrefix() const {
122 return key_system_uma_prefix_
;
125 void CdmSessionAdapter::OnSessionMessage(
126 const std::string
& session_id
,
127 MediaKeys::MessageType message_type
,
128 const std::vector
<uint8
>& message
,
129 const GURL
& /* legacy_destination_url */) {
130 WebContentDecryptionModuleSessionImpl
* session
= GetSession(session_id
);
131 DLOG_IF(WARNING
, !session
) << __FUNCTION__
<< " for unknown session "
134 session
->OnSessionMessage(message_type
, message
);
137 void CdmSessionAdapter::OnSessionKeysChange(const std::string
& session_id
,
138 bool has_additional_usable_key
,
139 CdmKeysInfo keys_info
) {
140 // TODO(jrummell): Pass |keys_info| on.
141 WebContentDecryptionModuleSessionImpl
* session
= GetSession(session_id
);
142 DLOG_IF(WARNING
, !session
) << __FUNCTION__
<< " for unknown session "
145 session
->OnSessionKeysChange(has_additional_usable_key
, keys_info
.Pass());
148 void CdmSessionAdapter::OnSessionExpirationUpdate(
149 const std::string
& session_id
,
150 const base::Time
& new_expiry_time
) {
151 WebContentDecryptionModuleSessionImpl
* session
= GetSession(session_id
);
152 DLOG_IF(WARNING
, !session
) << __FUNCTION__
<< " for unknown session "
155 session
->OnSessionExpirationUpdate(new_expiry_time
);
158 void CdmSessionAdapter::OnSessionClosed(const std::string
& session_id
) {
159 WebContentDecryptionModuleSessionImpl
* session
= GetSession(session_id
);
160 DLOG_IF(WARNING
, !session
) << __FUNCTION__
<< " for unknown session "
163 session
->OnSessionClosed();
166 void CdmSessionAdapter::OnLegacySessionError(
167 const std::string
& session_id
,
168 MediaKeys::Exception exception_code
,
170 const std::string
& error_message
) {
171 // Error events not used by unprefixed EME.
172 // TODO(jrummell): Remove when prefixed EME removed.
175 WebContentDecryptionModuleSessionImpl
* CdmSessionAdapter::GetSession(
176 const std::string
& session_id
) {
177 // Since session objects may get garbage collected, it is possible that there
178 // are events coming back from the CDM and the session has been unregistered.
179 // We can not tell if the CDM is firing events at sessions that never existed.
180 SessionMap::iterator session
= sessions_
.find(session_id
);
181 return (session
!= sessions_
.end()) ? session
->second
.get() : NULL
;