Supervised user import: Listen for profile creation/deletion
[chromium-blink-merge.git] / media / blink / cdm_session_adapter.cc
blob692eecca1eb188e7674bbeb8cf5ac83dddb2e069
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"
7 #include "base/bind.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"
18 #include "url/gurl.h"
20 namespace media {
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();
41 cdm_factory->Create(
42 key_system, allow_distinctive_identifier, allow_persistent_state,
43 security_origin,
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 std::vector<uint8_t>& certificate,
54 scoped_ptr<SimpleCdmPromise> promise) {
55 cdm_->SetServerCertificate(certificate, 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))
67 return false;
69 sessions_[session_id] = session;
70 return true;
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 std::vector<uint8_t>& init_data,
81 MediaKeys::SessionType session_type,
82 scoped_ptr<NewSessionCdmPromise> promise) {
83 cdm_->CreateSessionAndGenerateRequest(session_type, init_data_type, init_data,
84 promise.Pass());
87 void CdmSessionAdapter::LoadSession(MediaKeys::SessionType session_type,
88 const std::string& session_id,
89 scoped_ptr<NewSessionCdmPromise> promise) {
90 cdm_->LoadSession(session_type, session_id, promise.Pass());
93 void CdmSessionAdapter::UpdateSession(const std::string& session_id,
94 const std::vector<uint8_t>& response,
95 scoped_ptr<SimpleCdmPromise> promise) {
96 cdm_->UpdateSession(session_id, response, promise.Pass());
99 void CdmSessionAdapter::CloseSession(const std::string& session_id,
100 scoped_ptr<SimpleCdmPromise> promise) {
101 cdm_->CloseSession(session_id, promise.Pass());
104 void CdmSessionAdapter::RemoveSession(const std::string& session_id,
105 scoped_ptr<SimpleCdmPromise> promise) {
106 cdm_->RemoveSession(session_id, promise.Pass());
109 CdmContext* CdmSessionAdapter::GetCdmContext() {
110 return cdm_->GetCdmContext();
113 const std::string& CdmSessionAdapter::GetKeySystem() const {
114 return key_system_;
117 const std::string& CdmSessionAdapter::GetKeySystemUMAPrefix() const {
118 return key_system_uma_prefix_;
121 void CdmSessionAdapter::OnCdmCreated(
122 const std::string& key_system,
123 blink::WebContentDecryptionModuleResult result,
124 scoped_ptr<MediaKeys> cdm) {
125 DVLOG(2) << __FUNCTION__;
126 if (!cdm) {
127 result.completeWithError(
128 blink::WebContentDecryptionModuleExceptionNotSupportedError, 0,
129 "Failed to create the CDM instance.");
130 return;
133 key_system_ = key_system;
134 key_system_uma_prefix_ =
135 kMediaEME + GetKeySystemNameForUMA(key_system) + kDot;
136 cdm_ = cdm.Pass();
138 result.completeWithContentDecryptionModule(
139 new WebContentDecryptionModuleImpl(this));
142 void CdmSessionAdapter::OnSessionMessage(
143 const std::string& session_id,
144 MediaKeys::MessageType message_type,
145 const std::vector<uint8_t>& message,
146 const GURL& /* legacy_destination_url */) {
147 WebContentDecryptionModuleSessionImpl* session = GetSession(session_id);
148 DLOG_IF(WARNING, !session) << __FUNCTION__ << " for unknown session "
149 << session_id;
150 if (session)
151 session->OnSessionMessage(message_type, message);
154 void CdmSessionAdapter::OnSessionKeysChange(const std::string& session_id,
155 bool has_additional_usable_key,
156 CdmKeysInfo keys_info) {
157 // TODO(jrummell): Pass |keys_info| on.
158 WebContentDecryptionModuleSessionImpl* session = GetSession(session_id);
159 DLOG_IF(WARNING, !session) << __FUNCTION__ << " for unknown session "
160 << session_id;
161 if (session)
162 session->OnSessionKeysChange(has_additional_usable_key, keys_info.Pass());
165 void CdmSessionAdapter::OnSessionExpirationUpdate(
166 const std::string& session_id,
167 const base::Time& new_expiry_time) {
168 WebContentDecryptionModuleSessionImpl* session = GetSession(session_id);
169 DLOG_IF(WARNING, !session) << __FUNCTION__ << " for unknown session "
170 << session_id;
171 if (session)
172 session->OnSessionExpirationUpdate(new_expiry_time);
175 void CdmSessionAdapter::OnSessionClosed(const std::string& session_id) {
176 WebContentDecryptionModuleSessionImpl* session = GetSession(session_id);
177 DLOG_IF(WARNING, !session) << __FUNCTION__ << " for unknown session "
178 << session_id;
179 if (session)
180 session->OnSessionClosed();
183 void CdmSessionAdapter::OnLegacySessionError(
184 const std::string& session_id,
185 MediaKeys::Exception exception_code,
186 uint32_t system_code,
187 const std::string& error_message) {
188 // Error events not used by unprefixed EME.
189 // TODO(jrummell): Remove when prefixed EME removed.
192 WebContentDecryptionModuleSessionImpl* CdmSessionAdapter::GetSession(
193 const std::string& session_id) {
194 // Since session objects may get garbage collected, it is possible that there
195 // are events coming back from the CDM and the session has been unregistered.
196 // We can not tell if the CDM is firing events at sessions that never existed.
197 SessionMap::iterator session = sessions_.find(session_id);
198 return (session != sessions_.end()) ? session->second.get() : NULL;
201 } // namespace media