Chromium Code Reviews| Index: media/mojo/services/mojo_cdm.cc |
| diff --git a/media/mojo/services/mojo_cdm.cc b/media/mojo/services/mojo_cdm.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..b7abf260b39273f9d738e92ca3eaa963b79b459d |
| --- /dev/null |
| +++ b/media/mojo/services/mojo_cdm.cc |
| @@ -0,0 +1,174 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "media/mojo/services/mojo_cdm.h" |
| + |
| +#include "base/bind.h" |
| +#include "base/bind_helpers.h" |
| +#include "media/base/cdm_promise.h" |
| +#include "mojo/public/cpp/application/connect.h" |
| +#include "mojo/public/cpp/bindings/interface_impl.h" |
| +#include "mojo/public/interfaces/application/service_provider.mojom.h" |
| +#include "url/gurl.h" |
| + |
| +namespace media { |
| + |
| +static mojo::Array<uint8_t> CreateMojoArray(const uint8* data, int length) { |
| + DCHECK(data); |
| + DCHECK_GT(length, 0); |
| + std::vector<uint8> vector(data, data + length); |
| + mojo::Array<uint8_t> array; |
|
jrummell
2014/12/18 00:02:12
Why the different types in these two definitions?
xhwang
2014/12/18 17:04:59
This is how we construct an mojo::Array now:
https
jrummell
2014/12/18 20:01:14
As discussed offline, my question was around using
jamesr
2014/12/18 20:14:57
In general in chromium you should always use the _
|
| + array.Swap(&vector); |
| + return array.Pass(); |
| +} |
| + |
| +template <typename PromiseType> |
| +static void RejectPromise(scoped_ptr<PromiseType> promise, |
| + mojo::CdmPromiseResultPtr result) { |
| + promise->reject(static_cast<MediaKeys::Exception>(result->exception), |
|
jrummell
2014/12/18 00:02:12
Is there a need for compile-time checks that the 2
xhwang
2014/12/18 17:04:59
Done.
|
| + result->system_code, result->error_message); |
| +} |
| + |
| +MojoCdm::MojoCdm(mojo::ContentDecryptionModulePtr remote_cdm, |
| + const SessionMessageCB& session_message_cb, |
| + const SessionReadyCB& session_ready_cb, |
| + const SessionClosedCB& session_closed_cb, |
| + const SessionErrorCB& session_error_cb, |
| + const SessionKeysChangeCB& session_keys_change_cb, |
| + const SessionExpirationUpdateCB& session_expiration_update_cb) |
| + : remote_cdm_(remote_cdm.Pass()), |
| + session_message_cb_(session_message_cb), |
|
jrummell
2014/12/18 00:02:12
Should these cb's be checked for !null (since they
xhwang
2014/12/18 17:04:59
Done.
|
| + session_ready_cb_(session_ready_cb), |
| + session_closed_cb_(session_closed_cb), |
| + session_error_cb_(session_error_cb), |
| + session_keys_change_cb_(session_keys_change_cb), |
| + session_expiration_update_cb_(session_expiration_update_cb), |
| + weak_factory_(this) { |
| + DVLOG(1) << __FUNCTION__; |
| + DCHECK(remote_cdm_); |
|
jrummell
2014/12/18 00:02:12
Is this necessary given the following line will cr
xhwang
2014/12/18 17:04:59
Done.
|
| + remote_cdm_.set_client(this); |
| +} |
| + |
| +MojoCdm::~MojoCdm() { |
| + DVLOG(1) << __FUNCTION__; |
| +} |
| + |
| +void MojoCdm::SetServerCertificate(const uint8* certificate_data, |
| + int certificate_data_length, |
| + scoped_ptr<SimpleCdmPromise> promise) { |
| + remote_cdm_->SetServerCertificate( |
| + CreateMojoArray(certificate_data, certificate_data_length), |
| + base::Bind(&MojoCdm::OnPromiseResult<>, weak_factory_.GetWeakPtr(), |
| + base::Passed(&promise))); |
| +} |
| + |
| +void MojoCdm::CreateSession(const std::string& init_data_type, |
| + const uint8* init_data, |
| + int init_data_length, |
| + SessionType session_type, |
| + scoped_ptr<NewSessionCdmPromise> promise) { |
| + remote_cdm_->CreateSession( |
| + init_data_type, CreateMojoArray(init_data, init_data_length), |
| + static_cast<mojo::ContentDecryptionModule::SessionType>(session_type), |
| + base::Bind(&MojoCdm::OnPromiseResult<std::string>, |
| + weak_factory_.GetWeakPtr(), base::Passed(&promise))); |
| +} |
| + |
| +void MojoCdm::LoadSession(const std::string& session_id, |
| + scoped_ptr<NewSessionCdmPromise> promise) { |
| + remote_cdm_->LoadSession( |
| + session_id, |
| + base::Bind(&MojoCdm::OnPromiseResult<std::string>, |
| + weak_factory_.GetWeakPtr(), base::Passed(&promise))); |
| +} |
| + |
| +void MojoCdm::UpdateSession(const std::string& session_id, |
| + const uint8* response, |
| + int response_length, |
| + scoped_ptr<SimpleCdmPromise> promise) { |
| + remote_cdm_->UpdateSession( |
| + session_id, CreateMojoArray(response, response_length), |
| + base::Bind(&MojoCdm::OnPromiseResult<>, weak_factory_.GetWeakPtr(), |
| + base::Passed(&promise))); |
| +} |
| + |
| +void MojoCdm::CloseSession(const std::string& session_id, |
| + scoped_ptr<SimpleCdmPromise> promise) { |
| + remote_cdm_->CloseSession(session_id, base::Bind(&MojoCdm::OnPromiseResult<>, |
| + weak_factory_.GetWeakPtr(), |
| + base::Passed(&promise))); |
| +} |
| + |
| +void MojoCdm::RemoveSession(const std::string& session_id, |
| + scoped_ptr<SimpleCdmPromise> promise) { |
| + remote_cdm_->RemoveSession(session_id, base::Bind(&MojoCdm::OnPromiseResult<>, |
| + weak_factory_.GetWeakPtr(), |
| + base::Passed(&promise))); |
| +} |
| + |
| +void MojoCdm::GetUsableKeyIds(const std::string& session_id, |
| + scoped_ptr<KeyIdsPromise> promise) { |
| + remote_cdm_->GetUsableKeyIds( |
| + session_id, |
| + base::Bind(&MojoCdm::OnPromiseResult<KeyIdsVector>, |
| + weak_factory_.GetWeakPtr(), base::Passed(&promise))); |
| +} |
| + |
| +CdmContext* MojoCdm::GetCdmContext() { |
| + NOTIMPLEMENTED(); |
| + return nullptr; |
| +} |
| + |
| +void MojoCdm::OnSessionMessage(const mojo::String& session_id, |
| + mojo::Array<uint8_t> message, |
| + const mojo::String& destination_url) { |
| + GURL verified_gurl = GURL(destination_url); |
| + if (!verified_gurl.is_valid() && !verified_gurl.is_empty()) { |
| + DLOG(WARNING) << "SessionMessage destination_url is invalid : " |
| + << verified_gurl.possibly_invalid_spec(); |
| + verified_gurl = GURL::EmptyGURL(); // Replace invalid destination_url. |
| + } |
| + |
| + session_message_cb_.Run(session_id, message.storage(), verified_gurl); |
| +} |
| + |
| +void MojoCdm::OnSessionReady(const mojo::String& session_id) { |
| + session_ready_cb_.Run(session_id); |
| +} |
| + |
| +void MojoCdm::OnSessionClosed(const mojo::String& session_id) { |
| + session_closed_cb_.Run(session_id); |
| +} |
| + |
| +void MojoCdm::OnSessionError(const mojo::String& session_id, |
| + mojo::CdmException exception, |
| + uint32_t system_code, |
| + const mojo::String& error_message) { |
| + session_error_cb_.Run(session_id, |
| + static_cast<MediaKeys::Exception>(exception), |
| + system_code, error_message); |
| +} |
| + |
| +void MojoCdm::OnSessionKeysChange(const mojo::String& session_id, |
| + bool has_additional_usable_key) { |
| + session_keys_change_cb_.Run(session_id, has_additional_usable_key); |
| +} |
| + |
| +void MojoCdm::OnSessionExpirationUpdate(const mojo::String& session_id, |
| + int64_t new_expiry_time_usec) { |
| + session_expiration_update_cb_.Run( |
| + session_id, base::Time::FromInternalValue(new_expiry_time_usec)); |
| +} |
| + |
| +template <typename... T> |
| +void MojoCdm::OnPromiseResult(scoped_ptr<CdmPromiseTemplate<T...>> promise, |
| + mojo::CdmPromiseResultPtr result, |
| + typename MojoTypeTrait<T>::MojoType... args) { |
| + if (result->success) |
| + promise->resolve(args.template To<T>()...); // See ISO C++03 14.2/4. |
| + else |
| + RejectPromise(promise.Pass(), result.Pass()); |
| +} |
| + |
| +} // namespace media |