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 const GURL
& security_origin
) {
32 key_system_
= key_system
;
33 key_system_uma_prefix_
=
34 kMediaEME
+ GetKeySystemNameForUMA(key_system
) + kDot
;
36 base::WeakPtr
<CdmSessionAdapter
> weak_this
= weak_ptr_factory_
.GetWeakPtr();
37 media_keys_
= cdm_factory
->Create(
38 key_system
, security_origin
,
39 base::Bind(&CdmSessionAdapter::OnSessionMessage
, weak_this
),
40 base::Bind(&CdmSessionAdapter::OnSessionClosed
, weak_this
),
41 base::Bind(&CdmSessionAdapter::OnSessionError
, weak_this
),
42 base::Bind(&CdmSessionAdapter::OnSessionKeysChange
, weak_this
),
43 base::Bind(&CdmSessionAdapter::OnSessionExpirationUpdate
, weak_this
));
44 return media_keys_
.get() != nullptr;
47 void CdmSessionAdapter::SetServerCertificate(
48 const uint8
* server_certificate
,
49 int server_certificate_length
,
50 scoped_ptr
<SimpleCdmPromise
> promise
) {
51 media_keys_
->SetServerCertificate(
52 server_certificate
, server_certificate_length
, promise
.Pass());
55 WebContentDecryptionModuleSessionImpl
* CdmSessionAdapter::CreateSession() {
56 return new WebContentDecryptionModuleSessionImpl(this);
59 bool CdmSessionAdapter::RegisterSession(
60 const std::string
& session_id
,
61 base::WeakPtr
<WebContentDecryptionModuleSessionImpl
> session
) {
62 // If this session ID is already registered, don't register it again.
63 if (ContainsKey(sessions_
, session_id
))
66 sessions_
[session_id
] = session
;
70 void CdmSessionAdapter::UnregisterSession(const std::string
& session_id
) {
71 DCHECK(ContainsKey(sessions_
, session_id
));
72 sessions_
.erase(session_id
);
75 void CdmSessionAdapter::InitializeNewSession(
76 const std::string
& init_data_type
,
77 const uint8
* init_data
,
79 MediaKeys::SessionType session_type
,
80 scoped_ptr
<NewSessionCdmPromise
> promise
) {
81 media_keys_
->CreateSessionAndGenerateRequest(session_type
, init_data_type
,
82 init_data
, init_data_length
,
86 void CdmSessionAdapter::LoadSession(MediaKeys::SessionType session_type
,
87 const std::string
& session_id
,
88 scoped_ptr
<NewSessionCdmPromise
> promise
) {
89 media_keys_
->LoadSession(session_type
, session_id
, promise
.Pass());
92 void CdmSessionAdapter::UpdateSession(const std::string
& session_id
,
93 const uint8
* response
,
95 scoped_ptr
<SimpleCdmPromise
> promise
) {
96 media_keys_
->UpdateSession(session_id
, response
, response_length
,
100 void CdmSessionAdapter::CloseSession(const std::string
& session_id
,
101 scoped_ptr
<SimpleCdmPromise
> promise
) {
102 media_keys_
->CloseSession(session_id
, promise
.Pass());
105 void CdmSessionAdapter::RemoveSession(const std::string
& session_id
,
106 scoped_ptr
<SimpleCdmPromise
> promise
) {
107 media_keys_
->RemoveSession(session_id
, promise
.Pass());
110 CdmContext
* CdmSessionAdapter::GetCdmContext() {
111 return media_keys_
->GetCdmContext();
114 const std::string
& CdmSessionAdapter::GetKeySystem() const {
118 const std::string
& CdmSessionAdapter::GetKeySystemUMAPrefix() const {
119 return key_system_uma_prefix_
;
122 void CdmSessionAdapter::OnSessionMessage(
123 const std::string
& session_id
,
124 MediaKeys::MessageType message_type
,
125 const std::vector
<uint8
>& message
,
126 const GURL
& /* legacy_destination_url */) {
127 WebContentDecryptionModuleSessionImpl
* session
= GetSession(session_id
);
128 DLOG_IF(WARNING
, !session
) << __FUNCTION__
<< " for unknown session "
131 session
->OnSessionMessage(message_type
, message
);
134 void CdmSessionAdapter::OnSessionKeysChange(const std::string
& session_id
,
135 bool has_additional_usable_key
,
136 CdmKeysInfo keys_info
) {
137 // TODO(jrummell): Pass |keys_info| on.
138 WebContentDecryptionModuleSessionImpl
* session
= GetSession(session_id
);
139 DLOG_IF(WARNING
, !session
) << __FUNCTION__
<< " for unknown session "
142 session
->OnSessionKeysChange(has_additional_usable_key
, keys_info
.Pass());
145 void CdmSessionAdapter::OnSessionExpirationUpdate(
146 const std::string
& session_id
,
147 const base::Time
& new_expiry_time
) {
148 WebContentDecryptionModuleSessionImpl
* session
= GetSession(session_id
);
149 DLOG_IF(WARNING
, !session
) << __FUNCTION__
<< " for unknown session "
152 session
->OnSessionExpirationUpdate(new_expiry_time
);
155 void CdmSessionAdapter::OnSessionClosed(const std::string
& session_id
) {
156 WebContentDecryptionModuleSessionImpl
* session
= GetSession(session_id
);
157 DLOG_IF(WARNING
, !session
) << __FUNCTION__
<< " for unknown session "
160 session
->OnSessionClosed();
163 void CdmSessionAdapter::OnSessionError(const std::string
& session_id
,
164 MediaKeys::Exception exception_code
,
166 const std::string
& error_message
) {
167 // Error events not used by unprefixed EME.
168 // TODO(jrummell): Remove when prefixed EME removed.
171 WebContentDecryptionModuleSessionImpl
* CdmSessionAdapter::GetSession(
172 const std::string
& session_id
) {
173 // Since session objects may get garbage collected, it is possible that there
174 // are events coming back from the CDM and the session has been unregistered.
175 // We can not tell if the CDM is firing events at sessions that never existed.
176 SessionMap::iterator session
= sessions_
.find(session_id
);
177 return (session
!= sessions_
.end()) ? session
->second
.get() : NULL
;