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/mojo/services/mojo_cdm_service.h"
8 #include "media/base/cdm_key_information.h"
9 #include "media/base/key_systems.h"
10 #include "media/cdm/aes_decryptor.h"
11 #include "media/mojo/services/media_type_converters.h"
12 #include "media/mojo/services/mojo_cdm_promise.h"
13 #include "mojo/common/common_type_converters.h"
14 #include "mojo/common/url_type_converters.h"
18 typedef MojoCdmPromise
<> SimpleMojoCdmPromise
;
19 typedef MojoCdmPromise
<std::string
> NewSessionMojoCdmPromise
;
21 MojoCdmService::MojoCdmService(const mojo::String
& key_system
)
22 : weak_factory_(this) {
23 base::WeakPtr
<MojoCdmService
> weak_this
= weak_factory_
.GetWeakPtr();
25 if (CanUseAesDecryptor(key_system
)) {
26 cdm_
.reset(new AesDecryptor(
27 base::Bind(&MojoCdmService::OnSessionMessage
, weak_this
),
28 base::Bind(&MojoCdmService::OnSessionClosed
, weak_this
),
29 base::Bind(&MojoCdmService::OnSessionKeysChange
, weak_this
)));
32 // TODO(xhwang): Check key system support in the app.
36 MojoCdmService::~MojoCdmService() {
39 void MojoCdmService::SetClient(mojo::ContentDecryptionModuleClientPtr client
) {
40 client_
= client
.Pass();
43 // mojo::MediaRenderer implementation.
44 void MojoCdmService::SetServerCertificate(
45 mojo::Array
<uint8_t> certificate_data
,
46 const mojo::Callback
<void(mojo::CdmPromiseResultPtr
)>& callback
) {
47 const std::vector
<uint8_t>& certificate_data_vector
=
48 certificate_data
.storage();
49 cdm_
->SetServerCertificate(
50 certificate_data_vector
.empty() ? nullptr : &certificate_data_vector
[0],
51 certificate_data_vector
.size(),
52 scoped_ptr
<SimpleCdmPromise
>(new SimpleMojoCdmPromise(callback
)));
55 void MojoCdmService::CreateSessionAndGenerateRequest(
56 mojo::ContentDecryptionModule::SessionType session_type
,
57 mojo::ContentDecryptionModule::InitDataType init_data_type
,
58 mojo::Array
<uint8_t> init_data
,
59 const mojo::Callback
<void(mojo::CdmPromiseResultPtr
, mojo::String
)>&
61 const std::vector
<uint8_t>& init_data_vector
= init_data
.storage();
62 cdm_
->CreateSessionAndGenerateRequest(
63 static_cast<MediaKeys::SessionType
>(session_type
),
64 static_cast<EmeInitDataType
>(init_data_type
),
65 init_data_vector
.empty() ? nullptr : &init_data_vector
[0],
66 init_data_vector
.size(),
67 scoped_ptr
<NewSessionCdmPromise
>(new NewSessionMojoCdmPromise(callback
)));
70 void MojoCdmService::LoadSession(
71 mojo::ContentDecryptionModule::SessionType session_type
,
72 const mojo::String
& session_id
,
73 const mojo::Callback
<void(mojo::CdmPromiseResultPtr
, mojo::String
)>&
76 static_cast<MediaKeys::SessionType
>(session_type
),
77 session_id
.To
<std::string
>(),
78 scoped_ptr
<NewSessionCdmPromise
>(new NewSessionMojoCdmPromise(callback
)));
81 void MojoCdmService::UpdateSession(
82 const mojo::String
& session_id
,
83 mojo::Array
<uint8_t> response
,
84 const mojo::Callback
<void(mojo::CdmPromiseResultPtr
)>& callback
) {
85 const std::vector
<uint8_t>& response_vector
= response
.storage();
87 session_id
.To
<std::string
>(),
88 response_vector
.empty() ? nullptr : &response_vector
[0],
89 response_vector
.size(),
90 scoped_ptr
<SimpleCdmPromise
>(new SimpleMojoCdmPromise(callback
)));
93 void MojoCdmService::CloseSession(
94 const mojo::String
& session_id
,
95 const mojo::Callback
<void(mojo::CdmPromiseResultPtr
)>& callback
) {
97 session_id
.To
<std::string
>(),
98 scoped_ptr
<SimpleCdmPromise
>(new SimpleMojoCdmPromise(callback
)));
101 void MojoCdmService::RemoveSession(
102 const mojo::String
& session_id
,
103 const mojo::Callback
<void(mojo::CdmPromiseResultPtr
)>& callback
) {
105 session_id
.To
<std::string
>(),
106 scoped_ptr
<SimpleCdmPromise
>(new SimpleMojoCdmPromise(callback
)));
109 void MojoCdmService::GetCdmContext(
111 mojo::InterfaceRequest
<mojo::Decryptor
> decryptor
) {
115 void MojoCdmService::OnSessionMessage(const std::string
& session_id
,
116 MediaKeys::MessageType message_type
,
117 const std::vector
<uint8_t>& message
,
118 const GURL
& legacy_destination_url
) {
119 client_
->OnSessionMessage(session_id
,
120 static_cast<mojo::CdmMessageType
>(message_type
),
121 mojo::Array
<uint8_t>::From(message
),
122 mojo::String::From(legacy_destination_url
));
125 void MojoCdmService::OnSessionKeysChange(const std::string
& session_id
,
126 bool has_additional_usable_key
,
127 CdmKeysInfo keys_info
) {
128 mojo::Array
<mojo::CdmKeyInformationPtr
> keys_data
;
129 for (const auto& key
: keys_info
)
130 keys_data
.push_back(mojo::CdmKeyInformation::From(*key
));
131 client_
->OnSessionKeysChange(session_id
, has_additional_usable_key
,
135 void MojoCdmService::OnSessionExpirationUpdate(
136 const std::string
& session_id
,
137 const base::Time
& new_expiry_time_sec
) {
138 client_
->OnSessionExpirationUpdate(session_id
,
139 new_expiry_time_sec
.ToDoubleT());
142 void MojoCdmService::OnSessionClosed(const std::string
& session_id
) {
143 client_
->OnSessionClosed(session_id
);
146 void MojoCdmService::OnLegacySessionError(const std::string
& session_id
,
147 MediaKeys::Exception exception
,
148 uint32_t system_code
,
149 const std::string
& error_message
) {
150 client_
->OnLegacySessionError(session_id
,
151 static_cast<mojo::CdmException
>(exception
),
152 system_code
, error_message
);