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"
17 typedef MojoCdmPromise
<> SimpleMojoCdmPromise
;
18 typedef MojoCdmPromise
<std::string
> NewSessionMojoCdmPromise
;
20 MojoCdmService::MojoCdmService(const mojo::String
& key_system
)
21 : weak_factory_(this) {
22 base::WeakPtr
<MojoCdmService
> weak_this
= weak_factory_
.GetWeakPtr();
24 // TODO(xhwang): Client syntax has been removed, so a new mechanism for client
25 // discovery must be added to this interface. See http://crbug.com/451321.
28 if (CanUseAesDecryptor(key_system
)) {
29 cdm_
.reset(new AesDecryptor(
30 base::Bind(&MojoCdmService::OnSessionMessage
, weak_this
),
31 base::Bind(&MojoCdmService::OnSessionClosed
, weak_this
),
32 base::Bind(&MojoCdmService::OnSessionKeysChange
, weak_this
)));
35 // TODO(xhwang): Check key system support in the app.
39 MojoCdmService::~MojoCdmService() {
42 // mojo::MediaRenderer implementation.
43 void MojoCdmService::SetServerCertificate(
44 mojo::Array
<uint8_t> certificate_data
,
45 const mojo::Callback
<void(mojo::CdmPromiseResultPtr
)>& callback
) {
46 const std::vector
<uint8_t>& certificate_data_vector
=
47 certificate_data
.storage();
48 cdm_
->SetServerCertificate(
49 certificate_data_vector
.empty() ? nullptr : &certificate_data_vector
[0],
50 certificate_data_vector
.size(),
51 scoped_ptr
<SimpleCdmPromise
>(new SimpleMojoCdmPromise(callback
)));
54 void MojoCdmService::CreateSessionAndGenerateRequest(
55 mojo::ContentDecryptionModule::SessionType session_type
,
56 const mojo::String
& init_data_type
,
57 mojo::Array
<uint8_t> init_data
,
58 const mojo::Callback
<void(mojo::CdmPromiseResultPtr
, mojo::String
)>&
60 const std::vector
<uint8_t>& init_data_vector
= init_data
.storage();
61 cdm_
->CreateSessionAndGenerateRequest(
62 static_cast<MediaKeys::SessionType
>(session_type
),
63 init_data_type
.To
<std::string
>(),
64 init_data_vector
.empty() ? nullptr : &init_data_vector
[0],
65 init_data_vector
.size(),
66 scoped_ptr
<NewSessionCdmPromise
>(new NewSessionMojoCdmPromise(callback
)));
69 void MojoCdmService::LoadSession(
70 mojo::ContentDecryptionModule::SessionType session_type
,
71 const mojo::String
& session_id
,
72 const mojo::Callback
<void(mojo::CdmPromiseResultPtr
, mojo::String
)>&
75 static_cast<MediaKeys::SessionType
>(session_type
),
76 session_id
.To
<std::string
>(),
77 scoped_ptr
<NewSessionCdmPromise
>(new NewSessionMojoCdmPromise(callback
)));
80 void MojoCdmService::UpdateSession(
81 const mojo::String
& session_id
,
82 mojo::Array
<uint8_t> response
,
83 const mojo::Callback
<void(mojo::CdmPromiseResultPtr
)>& callback
) {
84 const std::vector
<uint8_t>& response_vector
= response
.storage();
86 session_id
.To
<std::string
>(),
87 response_vector
.empty() ? nullptr : &response_vector
[0],
88 response_vector
.size(),
89 scoped_ptr
<SimpleCdmPromise
>(new SimpleMojoCdmPromise(callback
)));
92 void MojoCdmService::CloseSession(
93 const mojo::String
& session_id
,
94 const mojo::Callback
<void(mojo::CdmPromiseResultPtr
)>& callback
) {
96 session_id
.To
<std::string
>(),
97 scoped_ptr
<SimpleCdmPromise
>(new SimpleMojoCdmPromise(callback
)));
100 void MojoCdmService::RemoveSession(
101 const mojo::String
& session_id
,
102 const mojo::Callback
<void(mojo::CdmPromiseResultPtr
)>& callback
) {
104 session_id
.To
<std::string
>(),
105 scoped_ptr
<SimpleCdmPromise
>(new SimpleMojoCdmPromise(callback
)));
108 void MojoCdmService::GetCdmContext(
110 mojo::InterfaceRequest
<mojo::Decryptor
> decryptor
) {
114 void MojoCdmService::OnSessionMessage(const std::string
& session_id
,
115 MediaKeys::MessageType message_type
,
116 const std::vector
<uint8_t>& message
,
117 const GURL
& legacy_destination_url
) {
118 client_
->OnSessionMessage(session_id
,
119 static_cast<mojo::CdmMessageType
>(message_type
),
120 mojo::Array
<uint8_t>::From(message
),
121 mojo::String::From(legacy_destination_url
));
124 void MojoCdmService::OnSessionKeysChange(const std::string
& session_id
,
125 bool has_additional_usable_key
,
126 CdmKeysInfo keys_info
) {
127 mojo::Array
<mojo::CdmKeyInformationPtr
> keys_data
;
128 for (const auto& key
: keys_info
)
129 keys_data
.push_back(mojo::CdmKeyInformation::From(*key
));
130 client_
->OnSessionKeysChange(session_id
, has_additional_usable_key
,
134 void MojoCdmService::OnSessionExpirationUpdate(
135 const std::string
& session_id
,
136 const base::Time
& new_expiry_time
) {
137 client_
->OnSessionExpirationUpdate(session_id
,
138 new_expiry_time
.ToInternalValue());
141 void MojoCdmService::OnSessionClosed(const std::string
& session_id
) {
142 client_
->OnSessionClosed(session_id
);
145 void MojoCdmService::OnSessionError(const std::string
& session_id
,
146 MediaKeys::Exception exception
,
147 uint32_t system_code
,
148 const std::string
& error_message
) {
149 client_
->OnSessionError(session_id
,
150 static_cast<mojo::CdmException
>(exception
),
151 system_code
, error_message
);