| 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 "content/renderer/media/cdm/renderer_cdm_manager.h" | |
| 6 | |
| 7 #include <stddef.h> | |
| 8 #include <utility> | |
| 9 | |
| 10 #include "base/stl_util.h" | |
| 11 #include "content/common/media/cdm_messages.h" | |
| 12 #include "content/renderer/media/cdm/proxy_media_keys.h" | |
| 13 #include "media/base/cdm_context.h" | |
| 14 #include "media/base/limits.h" | |
| 15 | |
| 16 namespace content { | |
| 17 | |
| 18 using media::MediaKeys; | |
| 19 | |
| 20 // Maximum sizes for various EME API parameters. These are checks to prevent | |
| 21 // unnecessarily large messages from being passed around, and the sizes | |
| 22 // are somewhat arbitrary as the EME spec doesn't specify any limits. | |
| 23 const size_t kMaxSessionMessageLength = 10240; // 10 KB | |
| 24 | |
| 25 RendererCdmManager::RendererCdmManager(RenderFrame* render_frame) | |
| 26 : RenderFrameObserver(render_frame), | |
| 27 next_cdm_id_(media::CdmContext::kInvalidCdmId + 1) {} | |
| 28 | |
| 29 RendererCdmManager::~RendererCdmManager() { | |
| 30 DCHECK(proxy_media_keys_map_.empty()) | |
| 31 << "RendererCdmManager is owned by RenderFrameImpl and is destroyed only " | |
| 32 "after all ProxyMediaKeys are destroyed and unregistered."; | |
| 33 } | |
| 34 | |
| 35 bool RendererCdmManager::OnMessageReceived(const IPC::Message& msg) { | |
| 36 bool handled = true; | |
| 37 IPC_BEGIN_MESSAGE_MAP(RendererCdmManager, msg) | |
| 38 IPC_MESSAGE_HANDLER(CdmMsg_SessionMessage, OnSessionMessage) | |
| 39 IPC_MESSAGE_HANDLER(CdmMsg_SessionClosed, OnSessionClosed) | |
| 40 IPC_MESSAGE_HANDLER(CdmMsg_SessionKeysChange, OnSessionKeysChange) | |
| 41 IPC_MESSAGE_HANDLER(CdmMsg_SessionExpirationUpdate, | |
| 42 OnSessionExpirationUpdate) | |
| 43 IPC_MESSAGE_HANDLER(CdmMsg_ResolvePromise, OnPromiseResolved) | |
| 44 IPC_MESSAGE_HANDLER(CdmMsg_ResolvePromiseWithSession, | |
| 45 OnPromiseResolvedWithSession) | |
| 46 IPC_MESSAGE_HANDLER(CdmMsg_RejectPromise, OnPromiseRejected) | |
| 47 IPC_MESSAGE_UNHANDLED(handled = false) | |
| 48 IPC_END_MESSAGE_MAP() | |
| 49 return handled; | |
| 50 } | |
| 51 | |
| 52 void RendererCdmManager::OnDestruct() { | |
| 53 delete this; | |
| 54 } | |
| 55 | |
| 56 void RendererCdmManager::InitializeCdm(int cdm_id, | |
| 57 uint32_t promise_id, | |
| 58 ProxyMediaKeys* media_keys, | |
| 59 const std::string& key_system, | |
| 60 const GURL& security_origin, | |
| 61 bool use_hw_secure_codecs) { | |
| 62 DCHECK(GetMediaKeys(cdm_id)) << "|cdm_id| not registered."; | |
| 63 CdmHostMsg_InitializeCdm_Params params; | |
| 64 params.key_system = key_system; | |
| 65 params.security_origin = security_origin; | |
| 66 params.use_hw_secure_codecs = use_hw_secure_codecs; | |
| 67 Send(new CdmHostMsg_InitializeCdm(routing_id(), cdm_id, promise_id, params)); | |
| 68 } | |
| 69 | |
| 70 void RendererCdmManager::SetServerCertificate( | |
| 71 int cdm_id, | |
| 72 uint32_t promise_id, | |
| 73 const std::vector<uint8_t>& certificate) { | |
| 74 DCHECK(GetMediaKeys(cdm_id)) << "|cdm_id| not registered."; | |
| 75 Send(new CdmHostMsg_SetServerCertificate(routing_id(), cdm_id, promise_id, | |
| 76 certificate)); | |
| 77 } | |
| 78 | |
| 79 void RendererCdmManager::CreateSessionAndGenerateRequest( | |
| 80 int cdm_id, | |
| 81 uint32_t promise_id, | |
| 82 media::MediaKeys::SessionType session_type, | |
| 83 CdmHostMsg_CreateSession_InitDataType init_data_type, | |
| 84 const std::vector<uint8_t>& init_data) { | |
| 85 DCHECK(GetMediaKeys(cdm_id)) << "|cdm_id| not registered."; | |
| 86 CdmHostMsg_CreateSessionAndGenerateRequest_Params params; | |
| 87 params.render_frame_id = routing_id(); | |
| 88 params.cdm_id = cdm_id; | |
| 89 params.promise_id = promise_id; | |
| 90 params.session_type = session_type; | |
| 91 params.init_data_type = init_data_type; | |
| 92 params.init_data = init_data; | |
| 93 Send(new CdmHostMsg_CreateSessionAndGenerateRequest(params)); | |
| 94 } | |
| 95 | |
| 96 void RendererCdmManager::LoadSession(int cdm_id, | |
| 97 uint32_t promise_id, | |
| 98 media::MediaKeys::SessionType session_type, | |
| 99 const std::string& session_id) { | |
| 100 DCHECK(GetMediaKeys(cdm_id)) << "|cdm_id| not registered."; | |
| 101 Send(new CdmHostMsg_LoadSession(routing_id(), cdm_id, promise_id, | |
| 102 session_type, session_id)); | |
| 103 } | |
| 104 | |
| 105 void RendererCdmManager::UpdateSession(int cdm_id, | |
| 106 uint32_t promise_id, | |
| 107 const std::string& session_id, | |
| 108 const std::vector<uint8_t>& response) { | |
| 109 DCHECK(GetMediaKeys(cdm_id)) << "|cdm_id| not registered."; | |
| 110 Send(new CdmHostMsg_UpdateSession(routing_id(), cdm_id, promise_id, | |
| 111 session_id, response)); | |
| 112 } | |
| 113 | |
| 114 void RendererCdmManager::CloseSession(int cdm_id, | |
| 115 uint32_t promise_id, | |
| 116 const std::string& session_id) { | |
| 117 DCHECK(GetMediaKeys(cdm_id)) << "|cdm_id| not registered."; | |
| 118 Send(new CdmHostMsg_CloseSession(routing_id(), cdm_id, promise_id, | |
| 119 session_id)); | |
| 120 } | |
| 121 | |
| 122 void RendererCdmManager::RemoveSession(int cdm_id, | |
| 123 uint32_t promise_id, | |
| 124 const std::string& session_id) { | |
| 125 DCHECK(GetMediaKeys(cdm_id)) << "|cdm_id| not registered."; | |
| 126 Send(new CdmHostMsg_RemoveSession(routing_id(), cdm_id, promise_id, | |
| 127 session_id)); | |
| 128 } | |
| 129 | |
| 130 void RendererCdmManager::DestroyCdm(int cdm_id) { | |
| 131 DCHECK(GetMediaKeys(cdm_id)) << "|cdm_id| not registered."; | |
| 132 Send(new CdmHostMsg_DestroyCdm(routing_id(), cdm_id)); | |
| 133 } | |
| 134 | |
| 135 void RendererCdmManager::OnSessionMessage( | |
| 136 int cdm_id, | |
| 137 const std::string& session_id, | |
| 138 media::MediaKeys::MessageType message_type, | |
| 139 const std::vector<uint8_t>& message) { | |
| 140 if (message.size() > kMaxSessionMessageLength) { | |
| 141 NOTREACHED(); | |
| 142 LOG(ERROR) << "Message is too long and dropped."; | |
| 143 return; | |
| 144 } | |
| 145 | |
| 146 ProxyMediaKeys* media_keys = GetMediaKeys(cdm_id); | |
| 147 if (media_keys) | |
| 148 media_keys->OnSessionMessage(session_id, message_type, message); | |
| 149 } | |
| 150 | |
| 151 void RendererCdmManager::OnSessionClosed(int cdm_id, | |
| 152 const std::string& session_id) { | |
| 153 ProxyMediaKeys* media_keys = GetMediaKeys(cdm_id); | |
| 154 if (media_keys) | |
| 155 media_keys->OnSessionClosed(session_id); | |
| 156 } | |
| 157 | |
| 158 void RendererCdmManager::OnSessionKeysChange( | |
| 159 int cdm_id, | |
| 160 const std::string& session_id, | |
| 161 bool has_additional_usable_key, | |
| 162 const std::vector<media::CdmKeyInformation>& key_info_vector) { | |
| 163 ProxyMediaKeys* media_keys = GetMediaKeys(cdm_id); | |
| 164 if (!media_keys) | |
| 165 return; | |
| 166 | |
| 167 media::CdmKeysInfo keys_info; | |
| 168 keys_info.reserve(key_info_vector.size()); | |
| 169 for (const auto& key_info : key_info_vector) | |
| 170 keys_info.push_back(new media::CdmKeyInformation(key_info)); | |
| 171 | |
| 172 media_keys->OnSessionKeysChange(session_id, has_additional_usable_key, | |
| 173 std::move(keys_info)); | |
| 174 } | |
| 175 | |
| 176 void RendererCdmManager::OnSessionExpirationUpdate( | |
| 177 int cdm_id, | |
| 178 const std::string& session_id, | |
| 179 const base::Time& new_expiry_time) { | |
| 180 ProxyMediaKeys* media_keys = GetMediaKeys(cdm_id); | |
| 181 if (media_keys) | |
| 182 media_keys->OnSessionExpirationUpdate(session_id, new_expiry_time); | |
| 183 } | |
| 184 | |
| 185 void RendererCdmManager::OnPromiseResolved(int cdm_id, uint32_t promise_id) { | |
| 186 ProxyMediaKeys* media_keys = GetMediaKeys(cdm_id); | |
| 187 if (media_keys) | |
| 188 media_keys->OnPromiseResolved(promise_id); | |
| 189 } | |
| 190 | |
| 191 void RendererCdmManager::OnPromiseResolvedWithSession( | |
| 192 int cdm_id, | |
| 193 uint32_t promise_id, | |
| 194 const std::string& session_id) { | |
| 195 if (session_id.length() > media::limits::kMaxSessionIdLength) { | |
| 196 NOTREACHED(); | |
| 197 OnPromiseRejected(cdm_id, promise_id, MediaKeys::INVALID_ACCESS_ERROR, 0, | |
| 198 "Session ID is too long"); | |
| 199 return; | |
| 200 } | |
| 201 | |
| 202 ProxyMediaKeys* media_keys = GetMediaKeys(cdm_id); | |
| 203 if (media_keys) | |
| 204 media_keys->OnPromiseResolvedWithSession(promise_id, session_id); | |
| 205 } | |
| 206 | |
| 207 void RendererCdmManager::OnPromiseRejected(int cdm_id, | |
| 208 uint32_t promise_id, | |
| 209 MediaKeys::Exception exception, | |
| 210 uint32_t system_code, | |
| 211 const std::string& error_message) { | |
| 212 ProxyMediaKeys* media_keys = GetMediaKeys(cdm_id); | |
| 213 if (media_keys) | |
| 214 media_keys->OnPromiseRejected(promise_id, exception, system_code, | |
| 215 error_message); | |
| 216 } | |
| 217 | |
| 218 int RendererCdmManager::RegisterMediaKeys(ProxyMediaKeys* media_keys) { | |
| 219 int cdm_id = next_cdm_id_++; | |
| 220 DCHECK_NE(cdm_id, media::CdmContext::kInvalidCdmId); | |
| 221 DCHECK(!base::ContainsKey(proxy_media_keys_map_, cdm_id)); | |
| 222 proxy_media_keys_map_[cdm_id] = media_keys; | |
| 223 return cdm_id; | |
| 224 } | |
| 225 | |
| 226 void RendererCdmManager::UnregisterMediaKeys(int cdm_id) { | |
| 227 DCHECK(base::ContainsKey(proxy_media_keys_map_, cdm_id)); | |
| 228 proxy_media_keys_map_.erase(cdm_id); | |
| 229 } | |
| 230 | |
| 231 ProxyMediaKeys* RendererCdmManager::GetMediaKeys(int cdm_id) { | |
| 232 std::map<int, ProxyMediaKeys*>::iterator iter = | |
| 233 proxy_media_keys_map_.find(cdm_id); | |
| 234 return (iter != proxy_media_keys_map_.end()) ? iter->second : NULL; | |
| 235 } | |
| 236 | |
| 237 } // namespace content | |
| OLD | NEW |