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 "content/renderer/media/cdm_session_adapter.h"
8 #include "base/logging.h"
9 #include "base/memory/weak_ptr.h"
10 #include "base/stl_util.h"
11 #include "content/renderer/media/crypto/content_decryption_module_factory.h"
12 #include "content/renderer/media/crypto/key_systems.h"
13 #include "content/renderer/media/webcontentdecryptionmodulesession_impl.h"
14 #include "media/base/cdm_promise.h"
15 #include "media/base/media_keys.h"
20 const char kMediaEME[] = "Media.EME.";
21 const char kDot[] = ".";
23 CdmSessionAdapter::CdmSessionAdapter() :
24 #if defined(ENABLE_BROWSER_CDMS)
27 weak_ptr_factory_(this) {}
29 CdmSessionAdapter::~CdmSessionAdapter() {}
31 bool CdmSessionAdapter::Initialize(
32 #if defined(ENABLE_PEPPER_CDMS)
33 const CreatePepperCdmCB& create_pepper_cdm_cb,
34 #elif defined(ENABLE_BROWSER_CDMS)
35 RendererCdmManager* manager,
36 #endif // defined(ENABLE_PEPPER_CDMS)
37 const std::string& key_system,
38 const GURL& security_origin) {
39 key_system_uma_prefix_ = kMediaEME + KeySystemNameForUMA(key_system) + kDot;
40 base::WeakPtr<CdmSessionAdapter> weak_this = weak_ptr_factory_.GetWeakPtr();
41 media_keys_ = ContentDecryptionModuleFactory::Create(
44 #if defined(ENABLE_PEPPER_CDMS)
46 #elif defined(ENABLE_BROWSER_CDMS)
49 #endif // defined(ENABLE_PEPPER_CDMS)
50 base::Bind(&CdmSessionAdapter::OnSessionMessage, weak_this),
51 base::Bind(&CdmSessionAdapter::OnSessionReady, weak_this),
52 base::Bind(&CdmSessionAdapter::OnSessionClosed, weak_this),
53 base::Bind(&CdmSessionAdapter::OnSessionError, weak_this),
54 base::Bind(&CdmSessionAdapter::OnSessionKeysChange, weak_this),
55 base::Bind(&CdmSessionAdapter::OnSessionExpirationUpdate, weak_this));
57 // Success if |media_keys_| created.
61 void CdmSessionAdapter::SetServerCertificate(
62 const uint8* server_certificate,
63 int server_certificate_length,
64 scoped_ptr<media::SimpleCdmPromise> promise) {
65 media_keys_->SetServerCertificate(
66 server_certificate, server_certificate_length, promise.Pass());
69 WebContentDecryptionModuleSessionImpl* CdmSessionAdapter::CreateSession() {
70 return new WebContentDecryptionModuleSessionImpl(this);
73 bool CdmSessionAdapter::RegisterSession(
74 const std::string& web_session_id,
75 base::WeakPtr<WebContentDecryptionModuleSessionImpl> session) {
76 // If this session ID is already registered, don't register it again.
77 if (ContainsKey(sessions_, web_session_id))
80 sessions_[web_session_id] = session;
84 void CdmSessionAdapter::UnregisterSession(const std::string& web_session_id) {
85 DCHECK(ContainsKey(sessions_, web_session_id));
86 sessions_.erase(web_session_id);
89 void CdmSessionAdapter::InitializeNewSession(
90 const std::string& init_data_type,
91 const uint8* init_data,
93 media::MediaKeys::SessionType session_type,
94 scoped_ptr<media::NewSessionCdmPromise> promise) {
95 media_keys_->CreateSession(init_data_type,
102 void CdmSessionAdapter::UpdateSession(
103 const std::string& web_session_id,
104 const uint8* response,
106 scoped_ptr<media::SimpleCdmPromise> promise) {
107 media_keys_->UpdateSession(
108 web_session_id, response, response_length, promise.Pass());
111 void CdmSessionAdapter::CloseSession(
112 const std::string& web_session_id,
113 scoped_ptr<media::SimpleCdmPromise> promise) {
114 media_keys_->CloseSession(web_session_id, promise.Pass());
117 void CdmSessionAdapter::RemoveSession(
118 const std::string& web_session_id,
119 scoped_ptr<media::SimpleCdmPromise> promise) {
120 media_keys_->RemoveSession(web_session_id, promise.Pass());
123 void CdmSessionAdapter::GetUsableKeyIds(
124 const std::string& web_session_id,
125 scoped_ptr<media::KeyIdsPromise> promise) {
126 media_keys_->GetUsableKeyIds(web_session_id, promise.Pass());
129 media::Decryptor* CdmSessionAdapter::GetDecryptor() {
130 return media_keys_->GetDecryptor();
133 const std::string& CdmSessionAdapter::GetKeySystemUMAPrefix() const {
134 return key_system_uma_prefix_;
137 #if defined(ENABLE_BROWSER_CDMS)
138 int CdmSessionAdapter::GetCdmId() const {
141 #endif // defined(ENABLE_BROWSER_CDMS)
143 void CdmSessionAdapter::OnSessionMessage(const std::string& web_session_id,
144 const std::vector<uint8>& message,
145 const GURL& destination_url) {
146 WebContentDecryptionModuleSessionImpl* session = GetSession(web_session_id);
147 DLOG_IF(WARNING, !session) << __FUNCTION__ << " for unknown session "
150 session->OnSessionMessage(message, destination_url);
153 void CdmSessionAdapter::OnSessionKeysChange(const std::string& web_session_id,
154 bool has_additional_usable_key) {
155 WebContentDecryptionModuleSessionImpl* session = GetSession(web_session_id);
156 DLOG_IF(WARNING, !session) << __FUNCTION__ << " for unknown session "
159 session->OnSessionKeysChange(has_additional_usable_key);
162 void CdmSessionAdapter::OnSessionExpirationUpdate(
163 const std::string& web_session_id,
164 const base::Time& new_expiry_time) {
165 WebContentDecryptionModuleSessionImpl* session = GetSession(web_session_id);
166 DLOG_IF(WARNING, !session) << __FUNCTION__ << " for unknown session "
169 session->OnSessionExpirationUpdate(new_expiry_time);
172 void CdmSessionAdapter::OnSessionReady(const std::string& web_session_id) {
173 WebContentDecryptionModuleSessionImpl* session = GetSession(web_session_id);
174 DLOG_IF(WARNING, !session) << __FUNCTION__ << " for unknown session "
177 session->OnSessionReady();
180 void CdmSessionAdapter::OnSessionClosed(const std::string& web_session_id) {
181 WebContentDecryptionModuleSessionImpl* session = GetSession(web_session_id);
182 DLOG_IF(WARNING, !session) << __FUNCTION__ << " for unknown session "
185 session->OnSessionClosed();
188 void CdmSessionAdapter::OnSessionError(
189 const std::string& web_session_id,
190 media::MediaKeys::Exception exception_code,
192 const std::string& error_message) {
193 WebContentDecryptionModuleSessionImpl* session = GetSession(web_session_id);
194 DLOG_IF(WARNING, !session) << __FUNCTION__ << " for unknown session "
197 session->OnSessionError(exception_code, system_code, error_message);
200 WebContentDecryptionModuleSessionImpl* CdmSessionAdapter::GetSession(
201 const std::string& web_session_id) {
202 // Since session objects may get garbage collected, it is possible that there
203 // are events coming back from the CDM and the session has been unregistered.
204 // We can not tell if the CDM is firing events at sessions that never existed.
205 SessionMap::iterator session = sessions_.find(web_session_id);
206 return (session != sessions_.end()) ? session->second.get() : NULL;
209 } // namespace content