Delete chrome.mediaGalleriesPrivate because the functionality unique to it has since...
[chromium-blink-merge.git] / media / mojo / services / mojo_cdm.cc
blob24d590543417d106f750eb1b8232e7ed2fec38e2
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 "mojo/public/cpp/application/connect.h"
13 #include "mojo/public/cpp/bindings/interface_impl.h"
14 #include "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 SessionErrorCB& session_error_cb,
39 const SessionKeysChangeCB& session_keys_change_cb,
40 const SessionExpirationUpdateCB& session_expiration_update_cb)
41 : remote_cdm_(remote_cdm.Pass()),
42 session_message_cb_(session_message_cb),
43 session_closed_cb_(session_closed_cb),
44 session_error_cb_(session_error_cb),
45 session_keys_change_cb_(session_keys_change_cb),
46 session_expiration_update_cb_(session_expiration_update_cb),
47 weak_factory_(this) {
48 DVLOG(1) << __FUNCTION__;
49 DCHECK(!session_message_cb_.is_null());
50 DCHECK(!session_closed_cb_.is_null());
51 DCHECK(!session_error_cb_.is_null());
52 DCHECK(!session_keys_change_cb_.is_null());
53 DCHECK(!session_expiration_update_cb_.is_null());
55 remote_cdm_.set_client(this);
58 MojoCdm::~MojoCdm() {
59 DVLOG(1) << __FUNCTION__;
62 void MojoCdm::SetServerCertificate(const uint8_t* certificate_data,
63 int certificate_data_length,
64 scoped_ptr<SimpleCdmPromise> promise) {
65 remote_cdm_->SetServerCertificate(
66 CreateMojoArray(certificate_data, certificate_data_length),
67 base::Bind(&MojoCdm::OnPromiseResult<>, weak_factory_.GetWeakPtr(),
68 base::Passed(&promise)));
71 void MojoCdm::CreateSessionAndGenerateRequest(
72 SessionType session_type,
73 const std::string& init_data_type,
74 const uint8_t* init_data,
75 int init_data_length,
76 scoped_ptr<NewSessionCdmPromise> promise) {
77 remote_cdm_->CreateSessionAndGenerateRequest(
78 static_cast<mojo::ContentDecryptionModule::SessionType>(session_type),
79 init_data_type, CreateMojoArray(init_data, init_data_length),
80 base::Bind(&MojoCdm::OnPromiseResult<std::string>,
81 weak_factory_.GetWeakPtr(), base::Passed(&promise)));
84 void MojoCdm::LoadSession(SessionType session_type,
85 const std::string& session_id,
86 scoped_ptr<NewSessionCdmPromise> promise) {
87 remote_cdm_->LoadSession(
88 static_cast<mojo::ContentDecryptionModule::SessionType>(session_type),
89 session_id,
90 base::Bind(&MojoCdm::OnPromiseResult<std::string>,
91 weak_factory_.GetWeakPtr(), base::Passed(&promise)));
94 void MojoCdm::UpdateSession(const std::string& session_id,
95 const uint8_t* response,
96 int response_length,
97 scoped_ptr<SimpleCdmPromise> promise) {
98 remote_cdm_->UpdateSession(
99 session_id, CreateMojoArray(response, response_length),
100 base::Bind(&MojoCdm::OnPromiseResult<>, weak_factory_.GetWeakPtr(),
101 base::Passed(&promise)));
104 void MojoCdm::CloseSession(const std::string& session_id,
105 scoped_ptr<SimpleCdmPromise> promise) {
106 remote_cdm_->CloseSession(session_id, base::Bind(&MojoCdm::OnPromiseResult<>,
107 weak_factory_.GetWeakPtr(),
108 base::Passed(&promise)));
111 void MojoCdm::RemoveSession(const std::string& session_id,
112 scoped_ptr<SimpleCdmPromise> promise) {
113 remote_cdm_->RemoveSession(session_id, base::Bind(&MojoCdm::OnPromiseResult<>,
114 weak_factory_.GetWeakPtr(),
115 base::Passed(&promise)));
118 CdmContext* MojoCdm::GetCdmContext() {
119 NOTIMPLEMENTED();
120 return nullptr;
123 void MojoCdm::OnSessionMessage(const mojo::String& session_id,
124 mojo::CdmMessageType message_type,
125 mojo::Array<uint8_t> message,
126 const mojo::String& legacy_destination_url) {
127 GURL verified_gurl = GURL(legacy_destination_url);
128 if (!verified_gurl.is_valid() && !verified_gurl.is_empty()) {
129 DLOG(WARNING) << "SessionMessage destination_url is invalid : "
130 << verified_gurl.possibly_invalid_spec();
131 verified_gurl = GURL::EmptyGURL(); // Replace invalid destination_url.
134 session_message_cb_.Run(session_id,
135 static_cast<MediaKeys::MessageType>(message_type),
136 message.storage(), verified_gurl);
139 void MojoCdm::OnSessionClosed(const mojo::String& session_id) {
140 session_closed_cb_.Run(session_id);
143 void MojoCdm::OnSessionError(const mojo::String& session_id,
144 mojo::CdmException exception,
145 uint32_t system_code,
146 const mojo::String& error_message) {
147 session_error_cb_.Run(session_id,
148 static_cast<MediaKeys::Exception>(exception),
149 system_code, error_message);
152 void MojoCdm::OnSessionKeysChange(
153 const mojo::String& session_id,
154 bool has_additional_usable_key,
155 mojo::Array<mojo::CdmKeyInformationPtr> keys_info) {
156 media::CdmKeysInfo key_data;
157 key_data.reserve(keys_info.size());
158 for (size_t i = 0; i < keys_info.size(); ++i) {
159 key_data.push_back(
160 keys_info[i].To<scoped_ptr<media::CdmKeyInformation>>().release());
162 session_keys_change_cb_.Run(session_id, has_additional_usable_key,
163 key_data.Pass());
166 void MojoCdm::OnSessionExpirationUpdate(const mojo::String& session_id,
167 int64_t new_expiry_time_usec) {
168 session_expiration_update_cb_.Run(
169 session_id, base::Time::FromInternalValue(new_expiry_time_usec));
172 } // namespace media