Roll src/third_party/WebKit 26e7263:bda1fe3e (svn 201960:201962)
[chromium-blink-merge.git] / media / blink / cdm_session_adapter.cc
blob4dcfe96e51d9b9cb87c1f870bb3409ce9df6acf4
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/metrics/histogram.h"
10 #include "base/stl_util.h"
11 #include "base/trace_event/trace_event.h"
12 #include "media/base/cdm_factory.h"
13 #include "media/base/cdm_key_information.h"
14 #include "media/base/cdm_promise.h"
15 #include "media/base/key_systems.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[] = ".";
24 const char kTimeToCreateCdmUMAName[] = "CreateCdmTime";
26 CdmSessionAdapter::CdmSessionAdapter()
27 : trace_id_(0), weak_ptr_factory_(this) {}
29 CdmSessionAdapter::~CdmSessionAdapter() {}
31 void CdmSessionAdapter::CreateCdm(
32 CdmFactory* cdm_factory,
33 const std::string& key_system,
34 const GURL& security_origin,
35 const CdmConfig& cdm_config,
36 scoped_ptr<blink::WebContentDecryptionModuleResult> result) {
37 TRACE_EVENT_ASYNC_BEGIN0("media", "CdmSessionAdapter::CreateCdm",
38 ++trace_id_);
40 base::TimeTicks start_time = base::TimeTicks::Now();
42 // Note: WebContentDecryptionModuleImpl::Create() calls this method without
43 // holding a reference to the CdmSessionAdapter. Bind OnCdmCreated() with
44 // |this| instead of |weak_this| to prevent |this| from being destructed.
45 base::WeakPtr<CdmSessionAdapter> weak_this = weak_ptr_factory_.GetWeakPtr();
47 DCHECK(!cdm_created_result_);
48 cdm_created_result_ = result.Pass();
50 cdm_factory->Create(
51 key_system, security_origin, cdm_config,
52 base::Bind(&CdmSessionAdapter::OnSessionMessage, weak_this),
53 base::Bind(&CdmSessionAdapter::OnSessionClosed, weak_this),
54 base::Bind(&CdmSessionAdapter::OnLegacySessionError, weak_this),
55 base::Bind(&CdmSessionAdapter::OnSessionKeysChange, weak_this),
56 base::Bind(&CdmSessionAdapter::OnSessionExpirationUpdate, weak_this),
57 base::Bind(&CdmSessionAdapter::OnCdmCreated, this, key_system,
58 start_time));
61 void CdmSessionAdapter::SetServerCertificate(
62 const std::vector<uint8_t>& certificate,
63 scoped_ptr<SimpleCdmPromise> promise) {
64 cdm_->SetServerCertificate(certificate, promise.Pass());
67 WebContentDecryptionModuleSessionImpl* CdmSessionAdapter::CreateSession() {
68 return new WebContentDecryptionModuleSessionImpl(this);
71 bool CdmSessionAdapter::RegisterSession(
72 const std::string& session_id,
73 base::WeakPtr<WebContentDecryptionModuleSessionImpl> session) {
74 // If this session ID is already registered, don't register it again.
75 if (ContainsKey(sessions_, session_id))
76 return false;
78 sessions_[session_id] = session;
79 return true;
82 void CdmSessionAdapter::UnregisterSession(const std::string& session_id) {
83 DCHECK(ContainsKey(sessions_, session_id));
84 sessions_.erase(session_id);
87 void CdmSessionAdapter::InitializeNewSession(
88 EmeInitDataType init_data_type,
89 const std::vector<uint8_t>& init_data,
90 MediaKeys::SessionType session_type,
91 scoped_ptr<NewSessionCdmPromise> promise) {
92 cdm_->CreateSessionAndGenerateRequest(session_type, init_data_type, init_data,
93 promise.Pass());
96 void CdmSessionAdapter::LoadSession(MediaKeys::SessionType session_type,
97 const std::string& session_id,
98 scoped_ptr<NewSessionCdmPromise> promise) {
99 cdm_->LoadSession(session_type, session_id, promise.Pass());
102 void CdmSessionAdapter::UpdateSession(const std::string& session_id,
103 const std::vector<uint8_t>& response,
104 scoped_ptr<SimpleCdmPromise> promise) {
105 cdm_->UpdateSession(session_id, response, promise.Pass());
108 void CdmSessionAdapter::CloseSession(const std::string& session_id,
109 scoped_ptr<SimpleCdmPromise> promise) {
110 cdm_->CloseSession(session_id, promise.Pass());
113 void CdmSessionAdapter::RemoveSession(const std::string& session_id,
114 scoped_ptr<SimpleCdmPromise> promise) {
115 cdm_->RemoveSession(session_id, promise.Pass());
118 CdmContext* CdmSessionAdapter::GetCdmContext() {
119 return cdm_->GetCdmContext();
122 const std::string& CdmSessionAdapter::GetKeySystem() const {
123 return key_system_;
126 const std::string& CdmSessionAdapter::GetKeySystemUMAPrefix() const {
127 DCHECK(!key_system_uma_prefix_.empty());
128 return key_system_uma_prefix_;
131 void CdmSessionAdapter::OnCdmCreated(
132 const std::string& key_system,
133 base::TimeTicks start_time,
134 scoped_ptr<MediaKeys> cdm,
135 const std::string& error_message) {
136 DVLOG(2) << __FUNCTION__;
137 DCHECK(!cdm_);
139 TRACE_EVENT_ASYNC_END2("media", "CdmSessionAdapter::CreateCdm", trace_id_,
140 "success", (cdm ? "true" : "false"), "error_message",
141 error_message);
143 if (!cdm) {
144 cdm_created_result_->completeWithError(
145 blink::WebContentDecryptionModuleExceptionNotSupportedError, 0,
146 blink::WebString::fromUTF8(error_message));
147 cdm_created_result_.reset();
148 return;
151 key_system_ = key_system;
152 key_system_uma_prefix_ =
153 kMediaEME + GetKeySystemNameForUMA(key_system) + kDot;
155 // Only report time for successful CDM creation.
156 ReportTimeToCreateCdmUMA(base::TimeTicks::Now() - start_time);
158 cdm_ = cdm.Pass();
160 cdm_created_result_->completeWithContentDecryptionModule(
161 new WebContentDecryptionModuleImpl(this));
162 cdm_created_result_.reset();
165 void CdmSessionAdapter::OnSessionMessage(
166 const std::string& session_id,
167 MediaKeys::MessageType message_type,
168 const std::vector<uint8_t>& message,
169 const GURL& /* legacy_destination_url */) {
170 WebContentDecryptionModuleSessionImpl* session = GetSession(session_id);
171 DLOG_IF(WARNING, !session) << __FUNCTION__ << " for unknown session "
172 << session_id;
173 if (session)
174 session->OnSessionMessage(message_type, message);
177 void CdmSessionAdapter::OnSessionKeysChange(const std::string& session_id,
178 bool has_additional_usable_key,
179 CdmKeysInfo keys_info) {
180 // TODO(jrummell): Pass |keys_info| on.
181 WebContentDecryptionModuleSessionImpl* session = GetSession(session_id);
182 DLOG_IF(WARNING, !session) << __FUNCTION__ << " for unknown session "
183 << session_id;
184 if (session)
185 session->OnSessionKeysChange(has_additional_usable_key, keys_info.Pass());
188 void CdmSessionAdapter::OnSessionExpirationUpdate(
189 const std::string& session_id,
190 const base::Time& new_expiry_time) {
191 WebContentDecryptionModuleSessionImpl* session = GetSession(session_id);
192 DLOG_IF(WARNING, !session) << __FUNCTION__ << " for unknown session "
193 << session_id;
194 if (session)
195 session->OnSessionExpirationUpdate(new_expiry_time);
198 void CdmSessionAdapter::OnSessionClosed(const std::string& session_id) {
199 WebContentDecryptionModuleSessionImpl* session = GetSession(session_id);
200 DLOG_IF(WARNING, !session) << __FUNCTION__ << " for unknown session "
201 << session_id;
202 if (session)
203 session->OnSessionClosed();
206 void CdmSessionAdapter::OnLegacySessionError(
207 const std::string& session_id,
208 MediaKeys::Exception exception_code,
209 uint32_t system_code,
210 const std::string& error_message) {
211 // Error events not used by unprefixed EME.
212 // TODO(jrummell): Remove when prefixed EME removed.
215 WebContentDecryptionModuleSessionImpl* CdmSessionAdapter::GetSession(
216 const std::string& session_id) {
217 // Since session objects may get garbage collected, it is possible that there
218 // are events coming back from the CDM and the session has been unregistered.
219 // We can not tell if the CDM is firing events at sessions that never existed.
220 SessionMap::iterator session = sessions_.find(session_id);
221 return (session != sessions_.end()) ? session->second.get() : NULL;
224 void CdmSessionAdapter::ReportTimeToCreateCdmUMA(base::TimeDelta time) const {
225 // Note: This leaks memory, which is expected behavior.
226 base::HistogramBase* histogram = base::Histogram::FactoryTimeGet(
227 GetKeySystemUMAPrefix() + kTimeToCreateCdmUMAName,
228 base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromSeconds(10),
229 50, base::HistogramBase::kUmaTargetedHistogramFlag);
231 histogram->AddTime(time);
234 } // namespace media