| OLD | NEW |
| (Empty) | |
| 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. |
| 4 |
| 5 #include "media/mojo/services/mojo_cdm.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" |
| 9 #include "media/base/cdm_promise.h" |
| 10 #include "mojo/public/cpp/application/connect.h" |
| 11 #include "mojo/public/cpp/bindings/interface_impl.h" |
| 12 #include "mojo/public/interfaces/application/service_provider.mojom.h" |
| 13 #include "url/gurl.h" |
| 14 |
| 15 namespace media { |
| 16 |
| 17 static mojo::Array<uint8_t> CreateMojoArray(const uint8_t* data, int length) { |
| 18 DCHECK(data); |
| 19 DCHECK_GT(length, 0); |
| 20 std::vector<uint8_t> vector(data, data + length); |
| 21 mojo::Array<uint8_t> array; |
| 22 array.Swap(&vector); |
| 23 return array.Pass(); |
| 24 } |
| 25 |
| 26 template <typename PromiseType> |
| 27 static void RejectPromise(scoped_ptr<PromiseType> promise, |
| 28 mojo::CdmPromiseResultPtr result) { |
| 29 promise->reject(static_cast<MediaKeys::Exception>(result->exception), |
| 30 result->system_code, result->error_message); |
| 31 } |
| 32 |
| 33 MojoCdm::MojoCdm(mojo::ContentDecryptionModulePtr remote_cdm, |
| 34 const SessionMessageCB& session_message_cb, |
| 35 const SessionReadyCB& session_ready_cb, |
| 36 const SessionClosedCB& session_closed_cb, |
| 37 const SessionErrorCB& session_error_cb, |
| 38 const SessionKeysChangeCB& session_keys_change_cb, |
| 39 const SessionExpirationUpdateCB& session_expiration_update_cb) |
| 40 : remote_cdm_(remote_cdm.Pass()), |
| 41 session_message_cb_(session_message_cb), |
| 42 session_ready_cb_(session_ready_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_ready_cb_.is_null()); |
| 51 DCHECK(!session_closed_cb_.is_null()); |
| 52 DCHECK(!session_error_cb_.is_null()); |
| 53 DCHECK(!session_keys_change_cb_.is_null()); |
| 54 DCHECK(!session_expiration_update_cb_.is_null()); |
| 55 |
| 56 remote_cdm_.set_client(this); |
| 57 } |
| 58 |
| 59 MojoCdm::~MojoCdm() { |
| 60 DVLOG(1) << __FUNCTION__; |
| 61 } |
| 62 |
| 63 void MojoCdm::SetServerCertificate(const uint8_t* certificate_data, |
| 64 int certificate_data_length, |
| 65 scoped_ptr<SimpleCdmPromise> promise) { |
| 66 remote_cdm_->SetServerCertificate( |
| 67 CreateMojoArray(certificate_data, certificate_data_length), |
| 68 base::Bind(&MojoCdm::OnPromiseResult<>, weak_factory_.GetWeakPtr(), |
| 69 base::Passed(&promise))); |
| 70 } |
| 71 |
| 72 void MojoCdm::CreateSession(const std::string& init_data_type, |
| 73 const uint8_t* init_data, |
| 74 int init_data_length, |
| 75 SessionType session_type, |
| 76 scoped_ptr<NewSessionCdmPromise> promise) { |
| 77 remote_cdm_->CreateSession( |
| 78 init_data_type, CreateMojoArray(init_data, init_data_length), |
| 79 static_cast<mojo::ContentDecryptionModule::SessionType>(session_type), |
| 80 base::Bind(&MojoCdm::OnPromiseResult<std::string>, |
| 81 weak_factory_.GetWeakPtr(), base::Passed(&promise))); |
| 82 } |
| 83 |
| 84 void MojoCdm::LoadSession(const std::string& session_id, |
| 85 scoped_ptr<NewSessionCdmPromise> promise) { |
| 86 remote_cdm_->LoadSession( |
| 87 session_id, |
| 88 base::Bind(&MojoCdm::OnPromiseResult<std::string>, |
| 89 weak_factory_.GetWeakPtr(), base::Passed(&promise))); |
| 90 } |
| 91 |
| 92 void MojoCdm::UpdateSession(const std::string& session_id, |
| 93 const uint8_t* response, |
| 94 int response_length, |
| 95 scoped_ptr<SimpleCdmPromise> promise) { |
| 96 remote_cdm_->UpdateSession( |
| 97 session_id, CreateMojoArray(response, response_length), |
| 98 base::Bind(&MojoCdm::OnPromiseResult<>, weak_factory_.GetWeakPtr(), |
| 99 base::Passed(&promise))); |
| 100 } |
| 101 |
| 102 void MojoCdm::CloseSession(const std::string& session_id, |
| 103 scoped_ptr<SimpleCdmPromise> promise) { |
| 104 remote_cdm_->CloseSession(session_id, base::Bind(&MojoCdm::OnPromiseResult<>, |
| 105 weak_factory_.GetWeakPtr(), |
| 106 base::Passed(&promise))); |
| 107 } |
| 108 |
| 109 void MojoCdm::RemoveSession(const std::string& session_id, |
| 110 scoped_ptr<SimpleCdmPromise> promise) { |
| 111 remote_cdm_->RemoveSession(session_id, base::Bind(&MojoCdm::OnPromiseResult<>, |
| 112 weak_factory_.GetWeakPtr(), |
| 113 base::Passed(&promise))); |
| 114 } |
| 115 |
| 116 void MojoCdm::GetUsableKeyIds(const std::string& session_id, |
| 117 scoped_ptr<KeyIdsPromise> promise) { |
| 118 remote_cdm_->GetUsableKeyIds( |
| 119 session_id, |
| 120 base::Bind(&MojoCdm::OnPromiseResult<KeyIdsVector>, |
| 121 weak_factory_.GetWeakPtr(), base::Passed(&promise))); |
| 122 } |
| 123 |
| 124 CdmContext* MojoCdm::GetCdmContext() { |
| 125 NOTIMPLEMENTED(); |
| 126 return nullptr; |
| 127 } |
| 128 |
| 129 void MojoCdm::OnSessionMessage(const mojo::String& session_id, |
| 130 mojo::Array<uint8_t> message, |
| 131 const mojo::String& destination_url) { |
| 132 GURL verified_gurl = GURL(destination_url); |
| 133 if (!verified_gurl.is_valid() && !verified_gurl.is_empty()) { |
| 134 DLOG(WARNING) << "SessionMessage destination_url is invalid : " |
| 135 << verified_gurl.possibly_invalid_spec(); |
| 136 verified_gurl = GURL::EmptyGURL(); // Replace invalid destination_url. |
| 137 } |
| 138 |
| 139 session_message_cb_.Run(session_id, message.storage(), verified_gurl); |
| 140 } |
| 141 |
| 142 void MojoCdm::OnSessionReady(const mojo::String& session_id) { |
| 143 session_ready_cb_.Run(session_id); |
| 144 } |
| 145 |
| 146 void MojoCdm::OnSessionClosed(const mojo::String& session_id) { |
| 147 session_closed_cb_.Run(session_id); |
| 148 } |
| 149 |
| 150 void MojoCdm::OnSessionError(const mojo::String& session_id, |
| 151 mojo::CdmException exception, |
| 152 uint32_t system_code, |
| 153 const mojo::String& error_message) { |
| 154 session_error_cb_.Run(session_id, |
| 155 static_cast<MediaKeys::Exception>(exception), |
| 156 system_code, error_message); |
| 157 } |
| 158 |
| 159 void MojoCdm::OnSessionKeysChange(const mojo::String& session_id, |
| 160 bool has_additional_usable_key) { |
| 161 session_keys_change_cb_.Run(session_id, has_additional_usable_key); |
| 162 } |
| 163 |
| 164 void MojoCdm::OnSessionExpirationUpdate(const mojo::String& session_id, |
| 165 int64_t new_expiry_time_usec) { |
| 166 session_expiration_update_cb_.Run( |
| 167 session_id, base::Time::FromInternalValue(new_expiry_time_usec)); |
| 168 } |
| 169 |
| 170 template <typename... T> |
| 171 void MojoCdm::OnPromiseResult(scoped_ptr<CdmPromiseTemplate<T...>> promise, |
| 172 mojo::CdmPromiseResultPtr result, |
| 173 typename MojoTypeTrait<T>::MojoType... args) { |
| 174 if (result->success) |
| 175 promise->resolve(args.template To<T>()...); // See ISO C++03 14.2/4. |
| 176 else |
| 177 RejectPromise(promise.Pass(), result.Pass()); |
| 178 } |
| 179 |
| 180 } // namespace media |
| OLD | NEW |