Use setProperties for IP Config.
[chromium-blink-merge.git] / media / blink / cdm_session_adapter.cc
blobbba40264671e6d5afcad3904cd458e48347e252e
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/webcontentdecryptionmodulesession_impl.h"
17 #include "url/gurl.h"
19 namespace media {
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_uma_prefix_ =
33 kMediaEME + GetKeySystemNameForUMA(key_system) + kDot;
35 base::WeakPtr<CdmSessionAdapter> weak_this = weak_ptr_factory_.GetWeakPtr();
36 media_keys_ = cdm_factory->Create(
37 key_system, security_origin,
38 base::Bind(&CdmSessionAdapter::OnSessionMessage, weak_this),
39 base::Bind(&CdmSessionAdapter::OnSessionClosed, weak_this),
40 base::Bind(&CdmSessionAdapter::OnSessionError, weak_this),
41 base::Bind(&CdmSessionAdapter::OnSessionKeysChange, weak_this),
42 base::Bind(&CdmSessionAdapter::OnSessionExpirationUpdate, weak_this));
43 return media_keys_.get() != nullptr;
46 void CdmSessionAdapter::SetServerCertificate(
47 const uint8* server_certificate,
48 int server_certificate_length,
49 scoped_ptr<SimpleCdmPromise> promise) {
50 media_keys_->SetServerCertificate(
51 server_certificate, server_certificate_length, promise.Pass());
54 WebContentDecryptionModuleSessionImpl* CdmSessionAdapter::CreateSession() {
55 return new WebContentDecryptionModuleSessionImpl(this);
58 bool CdmSessionAdapter::RegisterSession(
59 const std::string& web_session_id,
60 base::WeakPtr<WebContentDecryptionModuleSessionImpl> session) {
61 // If this session ID is already registered, don't register it again.
62 if (ContainsKey(sessions_, web_session_id))
63 return false;
65 sessions_[web_session_id] = session;
66 return true;
69 void CdmSessionAdapter::UnregisterSession(const std::string& web_session_id) {
70 DCHECK(ContainsKey(sessions_, web_session_id));
71 sessions_.erase(web_session_id);
74 void CdmSessionAdapter::InitializeNewSession(
75 const std::string& init_data_type,
76 const uint8* init_data,
77 int init_data_length,
78 MediaKeys::SessionType session_type,
79 scoped_ptr<NewSessionCdmPromise> promise) {
80 media_keys_->CreateSessionAndGenerateRequest(session_type, init_data_type,
81 init_data, init_data_length,
82 promise.Pass());
85 void CdmSessionAdapter::LoadSession(
86 const std::string& web_session_id,
87 scoped_ptr<NewSessionCdmPromise> promise) {
88 media_keys_->LoadSession(web_session_id, promise.Pass());
91 void CdmSessionAdapter::UpdateSession(
92 const std::string& web_session_id,
93 const uint8* response,
94 int response_length,
95 scoped_ptr<SimpleCdmPromise> promise) {
96 media_keys_->UpdateSession(
97 web_session_id, response, response_length, promise.Pass());
100 void CdmSessionAdapter::CloseSession(
101 const std::string& web_session_id,
102 scoped_ptr<SimpleCdmPromise> promise) {
103 media_keys_->CloseSession(web_session_id, promise.Pass());
106 void CdmSessionAdapter::RemoveSession(
107 const std::string& web_session_id,
108 scoped_ptr<SimpleCdmPromise> promise) {
109 media_keys_->RemoveSession(web_session_id, promise.Pass());
112 CdmContext* CdmSessionAdapter::GetCdmContext() {
113 return media_keys_->GetCdmContext();
116 const std::string& CdmSessionAdapter::GetKeySystemUMAPrefix() const {
117 return key_system_uma_prefix_;
120 void CdmSessionAdapter::OnSessionMessage(const std::string& web_session_id,
121 const std::vector<uint8>& message,
122 const GURL& destination_url) {
123 WebContentDecryptionModuleSessionImpl* session = GetSession(web_session_id);
124 DLOG_IF(WARNING, !session) << __FUNCTION__ << " for unknown session "
125 << web_session_id;
126 if (session)
127 session->OnSessionMessage(message, destination_url);
130 void CdmSessionAdapter::OnSessionKeysChange(const std::string& web_session_id,
131 bool has_additional_usable_key,
132 CdmKeysInfo keys_info) {
133 // TODO(jrummell): Pass |keys_info| on.
134 WebContentDecryptionModuleSessionImpl* session = GetSession(web_session_id);
135 DLOG_IF(WARNING, !session) << __FUNCTION__ << " for unknown session "
136 << web_session_id;
137 if (session)
138 session->OnSessionKeysChange(has_additional_usable_key);
141 void CdmSessionAdapter::OnSessionExpirationUpdate(
142 const std::string& web_session_id,
143 const base::Time& new_expiry_time) {
144 WebContentDecryptionModuleSessionImpl* session = GetSession(web_session_id);
145 DLOG_IF(WARNING, !session) << __FUNCTION__ << " for unknown session "
146 << web_session_id;
147 if (session)
148 session->OnSessionExpirationUpdate(new_expiry_time);
151 void CdmSessionAdapter::OnSessionClosed(const std::string& web_session_id) {
152 WebContentDecryptionModuleSessionImpl* session = GetSession(web_session_id);
153 DLOG_IF(WARNING, !session) << __FUNCTION__ << " for unknown session "
154 << web_session_id;
155 if (session)
156 session->OnSessionClosed();
159 void CdmSessionAdapter::OnSessionError(
160 const std::string& web_session_id,
161 MediaKeys::Exception exception_code,
162 uint32 system_code,
163 const std::string& error_message) {
164 // Error events not used by unprefixed EME.
165 // TODO(jrummell): Remove when prefixed EME removed.
168 WebContentDecryptionModuleSessionImpl* CdmSessionAdapter::GetSession(
169 const std::string& web_session_id) {
170 // Since session objects may get garbage collected, it is possible that there
171 // are events coming back from the CDM and the session has been unregistered.
172 // We can not tell if the CDM is firing events at sessions that never existed.
173 SessionMap::iterator session = sessions_.find(web_session_id);
174 return (session != sessions_.end()) ? session->second.get() : NULL;
177 } // namespace media