Unregister from GCM when the only GCM app is removed
[chromium-blink-merge.git] / media / mojo / services / mojo_cdm.cc
blob43dbf2d7a14daba3ba7f34877fb8908d5667183e
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 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 // TODO(xhwang): Client syntax has been removed, so a new mechanism for client
56 // discovery must be added to this interface. See http://crbug.com/451321.
57 NOTREACHED();
60 MojoCdm::~MojoCdm() {
61 DVLOG(1) << __FUNCTION__;
64 void MojoCdm::SetServerCertificate(const uint8_t* certificate_data,
65 int certificate_data_length,
66 scoped_ptr<SimpleCdmPromise> promise) {
67 remote_cdm_->SetServerCertificate(
68 CreateMojoArray(certificate_data, certificate_data_length),
69 base::Bind(&MojoCdm::OnPromiseResult<>, weak_factory_.GetWeakPtr(),
70 base::Passed(&promise)));
73 void MojoCdm::CreateSessionAndGenerateRequest(
74 SessionType session_type,
75 const std::string& init_data_type,
76 const uint8_t* init_data,
77 int init_data_length,
78 scoped_ptr<NewSessionCdmPromise> promise) {
79 remote_cdm_->CreateSessionAndGenerateRequest(
80 static_cast<mojo::ContentDecryptionModule::SessionType>(session_type),
81 init_data_type, CreateMojoArray(init_data, init_data_length),
82 base::Bind(&MojoCdm::OnPromiseResult<std::string>,
83 weak_factory_.GetWeakPtr(), base::Passed(&promise)));
86 void MojoCdm::LoadSession(SessionType session_type,
87 const std::string& session_id,
88 scoped_ptr<NewSessionCdmPromise> promise) {
89 remote_cdm_->LoadSession(
90 static_cast<mojo::ContentDecryptionModule::SessionType>(session_type),
91 session_id,
92 base::Bind(&MojoCdm::OnPromiseResult<std::string>,
93 weak_factory_.GetWeakPtr(), base::Passed(&promise)));
96 void MojoCdm::UpdateSession(const std::string& session_id,
97 const uint8_t* response,
98 int response_length,
99 scoped_ptr<SimpleCdmPromise> promise) {
100 remote_cdm_->UpdateSession(
101 session_id, CreateMojoArray(response, response_length),
102 base::Bind(&MojoCdm::OnPromiseResult<>, weak_factory_.GetWeakPtr(),
103 base::Passed(&promise)));
106 void MojoCdm::CloseSession(const std::string& session_id,
107 scoped_ptr<SimpleCdmPromise> promise) {
108 remote_cdm_->CloseSession(session_id, base::Bind(&MojoCdm::OnPromiseResult<>,
109 weak_factory_.GetWeakPtr(),
110 base::Passed(&promise)));
113 void MojoCdm::RemoveSession(const std::string& session_id,
114 scoped_ptr<SimpleCdmPromise> promise) {
115 remote_cdm_->RemoveSession(session_id, base::Bind(&MojoCdm::OnPromiseResult<>,
116 weak_factory_.GetWeakPtr(),
117 base::Passed(&promise)));
120 CdmContext* MojoCdm::GetCdmContext() {
121 NOTIMPLEMENTED();
122 return nullptr;
125 void MojoCdm::OnSessionMessage(const mojo::String& session_id,
126 mojo::CdmMessageType message_type,
127 mojo::Array<uint8_t> message,
128 const mojo::String& legacy_destination_url) {
129 GURL verified_gurl = GURL(legacy_destination_url);
130 if (!verified_gurl.is_valid() && !verified_gurl.is_empty()) {
131 DLOG(WARNING) << "SessionMessage destination_url is invalid : "
132 << verified_gurl.possibly_invalid_spec();
133 verified_gurl = GURL::EmptyGURL(); // Replace invalid destination_url.
136 session_message_cb_.Run(session_id,
137 static_cast<MediaKeys::MessageType>(message_type),
138 message.storage(), verified_gurl);
141 void MojoCdm::OnSessionClosed(const mojo::String& session_id) {
142 session_closed_cb_.Run(session_id);
145 void MojoCdm::OnSessionError(const mojo::String& session_id,
146 mojo::CdmException exception,
147 uint32_t system_code,
148 const mojo::String& error_message) {
149 session_error_cb_.Run(session_id,
150 static_cast<MediaKeys::Exception>(exception),
151 system_code, error_message);
154 void MojoCdm::OnSessionKeysChange(
155 const mojo::String& session_id,
156 bool has_additional_usable_key,
157 mojo::Array<mojo::CdmKeyInformationPtr> keys_info) {
158 media::CdmKeysInfo key_data;
159 key_data.reserve(keys_info.size());
160 for (size_t i = 0; i < keys_info.size(); ++i) {
161 key_data.push_back(
162 keys_info[i].To<scoped_ptr<media::CdmKeyInformation>>().release());
164 session_keys_change_cb_.Run(session_id, has_additional_usable_key,
165 key_data.Pass());
168 void MojoCdm::OnSessionExpirationUpdate(const mojo::String& session_id,
169 int64_t new_expiry_time_usec) {
170 session_expiration_update_cb_.Run(
171 session_id, base::Time::FromInternalValue(new_expiry_time_usec));
174 } // namespace media