Make USB permissions work in the new permission message system
[chromium-blink-merge.git] / content / renderer / media / crypto / proxy_media_keys.cc
blobf28be12493e646bfd870a0ab427054acd95df515
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/crypto/proxy_media_keys.h"
7 #include <vector>
9 #include "base/basictypes.h"
10 #include "base/logging.h"
11 #include "base/stl_util.h"
12 #include "content/renderer/media/crypto/renderer_cdm_manager.h"
13 #include "media/base/cdm_initialized_promise.h"
14 #include "media/base/cdm_key_information.h"
15 #include "media/base/cdm_promise.h"
16 #include "media/base/key_systems.h"
18 namespace content {
20 void ProxyMediaKeys::Create(
21 const std::string& key_system,
22 const GURL& security_origin,
23 bool use_hw_secure_codecs,
24 RendererCdmManager* manager,
25 const media::SessionMessageCB& session_message_cb,
26 const media::SessionClosedCB& session_closed_cb,
27 const media::LegacySessionErrorCB& legacy_session_error_cb,
28 const media::SessionKeysChangeCB& session_keys_change_cb,
29 const media::SessionExpirationUpdateCB& session_expiration_update_cb,
30 const media::CdmCreatedCB& cdm_created_cb) {
31 DCHECK(manager);
32 scoped_ptr<ProxyMediaKeys> proxy_media_keys(new ProxyMediaKeys(
33 manager, session_message_cb, session_closed_cb, legacy_session_error_cb,
34 session_keys_change_cb, session_expiration_update_cb));
36 // ProxyMediaKeys ownership passed to the promise, but keep a copy in order
37 // to call InitializeCdm().
38 ProxyMediaKeys* proxy_media_keys_copy = proxy_media_keys.get();
39 scoped_ptr<media::CdmInitializedPromise> promise(
40 new media::CdmInitializedPromise(cdm_created_cb,
41 proxy_media_keys.Pass()));
42 proxy_media_keys_copy->InitializeCdm(key_system, security_origin,
43 use_hw_secure_codecs, promise.Pass());
46 ProxyMediaKeys::~ProxyMediaKeys() {
47 manager_->DestroyCdm(cdm_id_);
48 manager_->UnregisterMediaKeys(cdm_id_);
49 cdm_promise_adapter_.Clear();
52 void ProxyMediaKeys::SetServerCertificate(
53 const std::vector<uint8_t>& certificate,
54 scoped_ptr<media::SimpleCdmPromise> promise) {
55 uint32_t promise_id = cdm_promise_adapter_.SavePromise(promise.Pass());
56 manager_->SetServerCertificate(cdm_id_, promise_id, certificate);
59 void ProxyMediaKeys::CreateSessionAndGenerateRequest(
60 SessionType session_type,
61 media::EmeInitDataType init_data_type,
62 const std::vector<uint8_t>& init_data,
63 scoped_ptr<media::NewSessionCdmPromise> promise) {
64 CdmHostMsg_CreateSession_InitDataType create_session_init_data_type =
65 INIT_DATA_TYPE_WEBM;
66 switch (init_data_type) {
67 case media::EmeInitDataType::CENC:
68 create_session_init_data_type = INIT_DATA_TYPE_CENC;
69 break;
70 case media::EmeInitDataType::WEBM:
71 create_session_init_data_type = INIT_DATA_TYPE_WEBM;
72 break;
73 case media::EmeInitDataType::KEYIDS:
74 case media::EmeInitDataType::UNKNOWN:
75 DLOG(ERROR) << "Unsupported EME CreateSession init data type";
76 promise->reject(NOT_SUPPORTED_ERROR, 0,
77 "Unsupported EME CreateSession init data type");
78 return;
81 uint32_t promise_id = cdm_promise_adapter_.SavePromise(promise.Pass());
82 manager_->CreateSessionAndGenerateRequest(
83 cdm_id_, promise_id, session_type, create_session_init_data_type,
84 init_data);
87 void ProxyMediaKeys::LoadSession(
88 SessionType session_type,
89 const std::string& session_id,
90 scoped_ptr<media::NewSessionCdmPromise> promise) {
91 uint32_t promise_id = cdm_promise_adapter_.SavePromise(promise.Pass());
92 manager_->LoadSession(cdm_id_, promise_id, session_type, session_id);
95 void ProxyMediaKeys::UpdateSession(
96 const std::string& session_id,
97 const std::vector<uint8_t>& response,
98 scoped_ptr<media::SimpleCdmPromise> promise) {
99 uint32_t promise_id = cdm_promise_adapter_.SavePromise(promise.Pass());
100 manager_->UpdateSession(cdm_id_, promise_id, session_id, response);
103 void ProxyMediaKeys::CloseSession(const std::string& session_id,
104 scoped_ptr<media::SimpleCdmPromise> promise) {
105 uint32_t promise_id = cdm_promise_adapter_.SavePromise(promise.Pass());
106 manager_->CloseSession(cdm_id_, promise_id, session_id);
109 void ProxyMediaKeys::RemoveSession(
110 const std::string& session_id,
111 scoped_ptr<media::SimpleCdmPromise> promise) {
112 uint32_t promise_id = cdm_promise_adapter_.SavePromise(promise.Pass());
113 manager_->RemoveSession(cdm_id_, promise_id, session_id);
116 media::CdmContext* ProxyMediaKeys::GetCdmContext() {
117 return this;
120 media::Decryptor* ProxyMediaKeys::GetDecryptor() {
121 return nullptr;
124 int ProxyMediaKeys::GetCdmId() const {
125 return cdm_id_;
128 void ProxyMediaKeys::OnSessionMessage(
129 const std::string& session_id,
130 media::MediaKeys::MessageType message_type,
131 const std::vector<uint8_t>& message,
132 const GURL& legacy_destination_url) {
133 session_message_cb_.Run(session_id, message_type, message,
134 legacy_destination_url);
137 void ProxyMediaKeys::OnSessionClosed(const std::string& session_id) {
138 session_closed_cb_.Run(session_id);
141 void ProxyMediaKeys::OnLegacySessionError(const std::string& session_id,
142 media::MediaKeys::Exception exception,
143 uint32_t system_code,
144 const std::string& error_message) {
145 legacy_session_error_cb_.Run(session_id, exception, system_code,
146 error_message);
149 void ProxyMediaKeys::OnSessionKeysChange(const std::string& session_id,
150 bool has_additional_usable_key,
151 media::CdmKeysInfo keys_info) {
152 session_keys_change_cb_.Run(session_id, has_additional_usable_key,
153 keys_info.Pass());
156 void ProxyMediaKeys::OnSessionExpirationUpdate(
157 const std::string& session_id,
158 const base::Time& new_expiry_time) {
159 session_expiration_update_cb_.Run(session_id, new_expiry_time);
162 void ProxyMediaKeys::OnPromiseResolved(uint32_t promise_id) {
163 cdm_promise_adapter_.ResolvePromise(promise_id);
166 void ProxyMediaKeys::OnPromiseResolvedWithSession(
167 uint32_t promise_id,
168 const std::string& session_id) {
169 cdm_promise_adapter_.ResolvePromise(promise_id, session_id);
172 void ProxyMediaKeys::OnPromiseRejected(uint32_t promise_id,
173 media::MediaKeys::Exception exception,
174 uint32_t system_code,
175 const std::string& error_message) {
176 cdm_promise_adapter_.RejectPromise(promise_id, exception, system_code,
177 error_message);
180 ProxyMediaKeys::ProxyMediaKeys(
181 RendererCdmManager* manager,
182 const media::SessionMessageCB& session_message_cb,
183 const media::SessionClosedCB& session_closed_cb,
184 const media::LegacySessionErrorCB& legacy_session_error_cb,
185 const media::SessionKeysChangeCB& session_keys_change_cb,
186 const media::SessionExpirationUpdateCB& session_expiration_update_cb)
187 : manager_(manager),
188 session_message_cb_(session_message_cb),
189 session_closed_cb_(session_closed_cb),
190 legacy_session_error_cb_(legacy_session_error_cb),
191 session_keys_change_cb_(session_keys_change_cb),
192 session_expiration_update_cb_(session_expiration_update_cb) {
193 cdm_id_ = manager->RegisterMediaKeys(this);
196 void ProxyMediaKeys::InitializeCdm(
197 const std::string& key_system,
198 const GURL& security_origin,
199 bool use_hw_secure_codecs,
200 scoped_ptr<media::SimpleCdmPromise> promise) {
201 uint32_t promise_id = cdm_promise_adapter_.SavePromise(promise.Pass());
202 manager_->InitializeCdm(cdm_id_, promise_id, this, key_system,
203 security_origin, use_hw_secure_codecs);
206 } // namespace content