Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / media / mojo / services / mojo_cdm.cc
blobdf82ec95ff093dcd61ecdf66fc2004b6b3618fa1
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.h"
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "media/base/cdm_key_information.h"
10 #include "media/base/cdm_promise.h"
11 #include "media/mojo/services/media_type_converters.h"
12 #include "third_party/mojo/src/mojo/public/cpp/application/connect.h"
13 #include "third_party/mojo/src/mojo/public/cpp/bindings/interface_impl.h"
14 #include "third_party/mojo/src/mojo/public/interfaces/application/service_provider.mojom.h"
15 #include "url/gurl.h"
17 namespace media {
19 static mojo::Array<uint8_t> CreateMojoArray(const uint8_t* data, int length) {
20 DCHECK(data);
21 DCHECK_GT(length, 0);
22 std::vector<uint8_t> vector(data, data + length);
23 mojo::Array<uint8_t> array;
24 array.Swap(&vector);
25 return array.Pass();
28 template <typename PromiseType>
29 static void RejectPromise(scoped_ptr<PromiseType> promise,
30 mojo::CdmPromiseResultPtr result) {
31 promise->reject(static_cast<MediaKeys::Exception>(result->exception),
32 result->system_code, result->error_message);
35 MojoCdm::MojoCdm(mojo::ContentDecryptionModulePtr remote_cdm,
36 const SessionMessageCB& session_message_cb,
37 const SessionClosedCB& session_closed_cb,
38 const LegacySessionErrorCB& legacy_session_error_cb,
39 const SessionKeysChangeCB& session_keys_change_cb,
40 const SessionExpirationUpdateCB& session_expiration_update_cb)
41 : remote_cdm_(remote_cdm.Pass()),
42 binding_(this),
43 session_message_cb_(session_message_cb),
44 session_closed_cb_(session_closed_cb),
45 legacy_session_error_cb_(legacy_session_error_cb),
46 session_keys_change_cb_(session_keys_change_cb),
47 session_expiration_update_cb_(session_expiration_update_cb),
48 weak_factory_(this) {
49 DVLOG(1) << __FUNCTION__;
50 DCHECK(!session_message_cb_.is_null());
51 DCHECK(!session_closed_cb_.is_null());
52 DCHECK(!legacy_session_error_cb_.is_null());
53 DCHECK(!session_keys_change_cb_.is_null());
54 DCHECK(!session_expiration_update_cb_.is_null());
56 mojo::ContentDecryptionModuleClientPtr client_ptr;
57 binding_.Bind(GetProxy(&client_ptr));
58 remote_cdm_->SetClient(client_ptr.Pass());
61 MojoCdm::~MojoCdm() {
62 DVLOG(1) << __FUNCTION__;
65 void MojoCdm::SetServerCertificate(const uint8_t* certificate_data,
66 int certificate_data_length,
67 scoped_ptr<SimpleCdmPromise> promise) {
68 remote_cdm_->SetServerCertificate(
69 CreateMojoArray(certificate_data, certificate_data_length),
70 base::Bind(&MojoCdm::OnPromiseResult<>, weak_factory_.GetWeakPtr(),
71 base::Passed(&promise)));
74 void MojoCdm::CreateSessionAndGenerateRequest(
75 SessionType session_type,
76 EmeInitDataType init_data_type,
77 const uint8_t* init_data,
78 int init_data_length,
79 scoped_ptr<NewSessionCdmPromise> promise) {
80 remote_cdm_->CreateSessionAndGenerateRequest(
81 static_cast<mojo::ContentDecryptionModule::SessionType>(session_type),
82 static_cast<mojo::ContentDecryptionModule::InitDataType>(init_data_type),
83 CreateMojoArray(init_data, init_data_length),
84 base::Bind(&MojoCdm::OnPromiseResult<std::string>,
85 weak_factory_.GetWeakPtr(), base::Passed(&promise)));
88 void MojoCdm::LoadSession(SessionType session_type,
89 const std::string& session_id,
90 scoped_ptr<NewSessionCdmPromise> promise) {
91 remote_cdm_->LoadSession(
92 static_cast<mojo::ContentDecryptionModule::SessionType>(session_type),
93 session_id,
94 base::Bind(&MojoCdm::OnPromiseResult<std::string>,
95 weak_factory_.GetWeakPtr(), base::Passed(&promise)));
98 void MojoCdm::UpdateSession(const std::string& session_id,
99 const uint8_t* response,
100 int response_length,
101 scoped_ptr<SimpleCdmPromise> promise) {
102 remote_cdm_->UpdateSession(
103 session_id, CreateMojoArray(response, response_length),
104 base::Bind(&MojoCdm::OnPromiseResult<>, weak_factory_.GetWeakPtr(),
105 base::Passed(&promise)));
108 void MojoCdm::CloseSession(const std::string& session_id,
109 scoped_ptr<SimpleCdmPromise> promise) {
110 remote_cdm_->CloseSession(session_id, base::Bind(&MojoCdm::OnPromiseResult<>,
111 weak_factory_.GetWeakPtr(),
112 base::Passed(&promise)));
115 void MojoCdm::RemoveSession(const std::string& session_id,
116 scoped_ptr<SimpleCdmPromise> promise) {
117 remote_cdm_->RemoveSession(session_id, base::Bind(&MojoCdm::OnPromiseResult<>,
118 weak_factory_.GetWeakPtr(),
119 base::Passed(&promise)));
122 CdmContext* MojoCdm::GetCdmContext() {
123 NOTIMPLEMENTED();
124 return nullptr;
127 void MojoCdm::OnSessionMessage(const mojo::String& session_id,
128 mojo::CdmMessageType message_type,
129 mojo::Array<uint8_t> message,
130 const mojo::String& legacy_destination_url) {
131 GURL verified_gurl = GURL(legacy_destination_url);
132 if (!verified_gurl.is_valid() && !verified_gurl.is_empty()) {
133 DLOG(WARNING) << "SessionMessage destination_url is invalid : "
134 << verified_gurl.possibly_invalid_spec();
135 verified_gurl = GURL::EmptyGURL(); // Replace invalid destination_url.
138 session_message_cb_.Run(session_id,
139 static_cast<MediaKeys::MessageType>(message_type),
140 message.storage(), verified_gurl);
143 void MojoCdm::OnSessionClosed(const mojo::String& session_id) {
144 session_closed_cb_.Run(session_id);
147 void MojoCdm::OnLegacySessionError(const mojo::String& session_id,
148 mojo::CdmException exception,
149 uint32_t system_code,
150 const mojo::String& error_message) {
151 legacy_session_error_cb_.Run(session_id,
152 static_cast<MediaKeys::Exception>(exception),
153 system_code, error_message);
156 void MojoCdm::OnSessionKeysChange(
157 const mojo::String& session_id,
158 bool has_additional_usable_key,
159 mojo::Array<mojo::CdmKeyInformationPtr> keys_info) {
160 media::CdmKeysInfo key_data;
161 key_data.reserve(keys_info.size());
162 for (size_t i = 0; i < keys_info.size(); ++i) {
163 key_data.push_back(
164 keys_info[i].To<scoped_ptr<media::CdmKeyInformation>>().release());
166 session_keys_change_cb_.Run(session_id, has_additional_usable_key,
167 key_data.Pass());
170 void MojoCdm::OnSessionExpirationUpdate(const mojo::String& session_id,
171 double new_expiry_time_sec) {
172 session_expiration_update_cb_.Run(
173 session_id, base::Time::FromDoubleT(new_expiry_time_sec));
176 } // namespace media