OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "media/cdm/proxy_decryptor.h" | 5 #include "media/cdm/proxy_decryptor.h" |
6 | 6 |
7 #include <cstring> | 7 #include <cstring> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/callback_helpers.h" | 10 #include "base/callback_helpers.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
13 #include "media/base/cdm_callback_promise.h" | 13 #include "media/base/cdm_callback_promise.h" |
14 #include "media/base/cdm_factory.h" | 14 #include "media/base/cdm_factory.h" |
15 #include "media/base/cdm_key_information.h" | 15 #include "media/base/cdm_key_information.h" |
16 #include "media/base/key_systems.h" | 16 #include "media/base/key_systems.h" |
17 #include "media/base/media_permission.h" | 17 #include "media/base/media_permission.h" |
18 #include "media/cdm/json_web_key.h" | 18 #include "media/cdm/json_web_key.h" |
19 #include "media/cdm/key_system_names.h" | 19 #include "media/cdm/key_system_names.h" |
20 | 20 |
21 namespace media { | 21 namespace media { |
22 | 22 |
23 // Special system code to signal a closed persistent session in a SessionError() | 23 // Special system code to signal a closed persistent session in a SessionError() |
24 // call. This is needed because there is no SessionClosed() call in the prefixed | 24 // call. This is needed because there is no SessionClosed() call in the prefixed |
25 // EME API. | 25 // EME API. |
26 const int kSessionClosedSystemCode = 29127; | 26 const int kSessionClosedSystemCode = 29127; |
27 | 27 |
| 28 ProxyDecryptor::PendingGenerateKeyRequestData::PendingGenerateKeyRequestData( |
| 29 EmeInitDataType init_data_type, |
| 30 const std::vector<uint8>& init_data) |
| 31 : init_data_type(init_data_type), init_data(init_data) { |
| 32 } |
| 33 |
| 34 ProxyDecryptor::PendingGenerateKeyRequestData:: |
| 35 ~PendingGenerateKeyRequestData() { |
| 36 } |
| 37 |
28 ProxyDecryptor::ProxyDecryptor(MediaPermission* media_permission, | 38 ProxyDecryptor::ProxyDecryptor(MediaPermission* media_permission, |
29 const KeyAddedCB& key_added_cb, | 39 const KeyAddedCB& key_added_cb, |
30 const KeyErrorCB& key_error_cb, | 40 const KeyErrorCB& key_error_cb, |
31 const KeyMessageCB& key_message_cb) | 41 const KeyMessageCB& key_message_cb) |
32 : media_permission_(media_permission), | 42 : is_creating_cdm_(false), |
| 43 media_permission_(media_permission), |
33 key_added_cb_(key_added_cb), | 44 key_added_cb_(key_added_cb), |
34 key_error_cb_(key_error_cb), | 45 key_error_cb_(key_error_cb), |
35 key_message_cb_(key_message_cb), | 46 key_message_cb_(key_message_cb), |
36 is_clear_key_(false), | 47 is_clear_key_(false), |
37 weak_ptr_factory_(this) { | 48 weak_ptr_factory_(this) { |
38 DCHECK(media_permission); | 49 DCHECK(media_permission); |
39 DCHECK(!key_added_cb_.is_null()); | 50 DCHECK(!key_added_cb_.is_null()); |
40 DCHECK(!key_error_cb_.is_null()); | 51 DCHECK(!key_error_cb_.is_null()); |
41 DCHECK(!key_message_cb_.is_null()); | 52 DCHECK(!key_message_cb_.is_null()); |
42 } | 53 } |
43 | 54 |
44 ProxyDecryptor::~ProxyDecryptor() { | 55 ProxyDecryptor::~ProxyDecryptor() { |
45 // Destroy the decryptor explicitly before destroying the plugin. | 56 // Destroy the decryptor explicitly before destroying the plugin. |
46 media_keys_.reset(); | 57 media_keys_.reset(); |
47 } | 58 } |
48 | 59 |
49 CdmContext* ProxyDecryptor::GetCdmContext() { | 60 void ProxyDecryptor::CreateCdm(CdmFactory* cdm_factory, |
50 return media_keys_ ? media_keys_->GetCdmContext() : nullptr; | 61 const std::string& key_system, |
| 62 const GURL& security_origin, |
| 63 const CdmContextReadyCB& cdm_context_ready_cb) { |
| 64 DVLOG(1) << __FUNCTION__ << ": key_system = " << key_system; |
| 65 DCHECK(!is_creating_cdm_); |
| 66 DCHECK(!media_keys_); |
| 67 |
| 68 // TODO(sandersd): Trigger permissions check here and use it to determine |
| 69 // distinctive identifier support, instead of always requiring the |
| 70 // permission. http://crbug.com/455271 |
| 71 bool allow_distinctive_identifier = true; |
| 72 bool allow_persistent_state = true; |
| 73 |
| 74 is_creating_cdm_ = true; |
| 75 |
| 76 base::WeakPtr<ProxyDecryptor> weak_this = weak_ptr_factory_.GetWeakPtr(); |
| 77 cdm_factory->Create( |
| 78 key_system, allow_distinctive_identifier, allow_persistent_state, |
| 79 security_origin, base::Bind(&ProxyDecryptor::OnSessionMessage, weak_this), |
| 80 base::Bind(&ProxyDecryptor::OnSessionClosed, weak_this), |
| 81 base::Bind(&ProxyDecryptor::OnLegacySessionError, weak_this), |
| 82 base::Bind(&ProxyDecryptor::OnSessionKeysChange, weak_this), |
| 83 base::Bind(&ProxyDecryptor::OnSessionExpirationUpdate, weak_this), |
| 84 base::Bind(&ProxyDecryptor::OnCdmCreated, weak_this, key_system, |
| 85 security_origin, cdm_context_ready_cb)); |
51 } | 86 } |
52 | 87 |
53 bool ProxyDecryptor::InitializeCDM(CdmFactory* cdm_factory, | 88 void ProxyDecryptor::OnCdmCreated(const std::string& key_system, |
54 const std::string& key_system, | 89 const GURL& security_origin, |
55 const GURL& security_origin) { | 90 const CdmContextReadyCB& cdm_context_ready_cb, |
56 DVLOG(1) << "InitializeCDM: key_system = " << key_system; | 91 scoped_ptr<MediaKeys> cdm) { |
| 92 is_creating_cdm_ = false; |
57 | 93 |
58 DCHECK(!media_keys_); | 94 if (!cdm) { |
59 media_keys_ = CreateMediaKeys(cdm_factory, key_system, security_origin); | 95 cdm_context_ready_cb.Run(nullptr); |
60 if (!media_keys_) | 96 return; |
61 return false; | 97 } |
62 | 98 |
63 key_system_ = key_system; | 99 key_system_ = key_system; |
64 security_origin_ = security_origin; | 100 security_origin_ = security_origin; |
| 101 is_clear_key_ = IsClearKey(key_system) || IsExternalClearKey(key_system); |
| 102 media_keys_ = cdm.Pass(); |
65 | 103 |
66 is_clear_key_ = | 104 cdm_context_ready_cb.Run(media_keys_->GetCdmContext()); |
67 IsClearKey(key_system) || IsExternalClearKey(key_system); | 105 |
68 return true; | 106 for (const auto& request : pending_requests_) |
| 107 GenerateKeyRequestInternal(request->init_data_type, request->init_data); |
| 108 |
| 109 pending_requests_.clear(); |
| 110 } |
| 111 |
| 112 void ProxyDecryptor::GenerateKeyRequest(EmeInitDataType init_data_type, |
| 113 const uint8* init_data, |
| 114 int init_data_length) { |
| 115 std::vector<uint8> init_data_vector(init_data, init_data + init_data_length); |
| 116 |
| 117 if (is_creating_cdm_) { |
| 118 pending_requests_.push_back( |
| 119 new PendingGenerateKeyRequestData(init_data_type, init_data_vector)); |
| 120 return; |
| 121 } |
| 122 |
| 123 GenerateKeyRequestInternal(init_data_type, init_data_vector); |
69 } | 124 } |
70 | 125 |
71 // Returns true if |data| is prefixed with |header| and has data after the | 126 // Returns true if |data| is prefixed with |header| and has data after the |
72 // |header|. | 127 // |header|. |
73 bool HasHeader(const uint8* data, int data_length, const std::string& header) { | 128 bool HasHeader(const std::vector<uint8>& data, const std::string& header) { |
74 return static_cast<size_t>(data_length) > header.size() && | 129 return data.size() > header.size() && |
75 std::equal(data, data + header.size(), header.begin()); | 130 std::equal(header.begin(), header.end(), data.begin()); |
76 } | 131 } |
77 | 132 |
78 // Removes the first |length| items from |data|. | 133 // Removes the first |length| items from |data|. |
79 void StripHeader(std::vector<uint8>& data, size_t length) { | 134 void StripHeader(std::vector<uint8>& data, size_t length) { |
80 data.erase(data.begin(), data.begin() + length); | 135 data.erase(data.begin(), data.begin() + length); |
81 } | 136 } |
82 | 137 |
83 bool ProxyDecryptor::GenerateKeyRequest(EmeInitDataType init_data_type, | 138 void ProxyDecryptor::GenerateKeyRequestInternal( |
84 const uint8* init_data, | 139 EmeInitDataType init_data_type, |
85 int init_data_length) { | 140 const std::vector<uint8>& init_data) { |
86 DVLOG(1) << "GenerateKeyRequest()"; | 141 DVLOG(1) << __FUNCTION__; |
| 142 DCHECK(!is_creating_cdm_); |
| 143 |
| 144 if (!media_keys_) { |
| 145 OnLegacySessionError(std::string(), MediaKeys::NOT_SUPPORTED_ERROR, 0, |
| 146 "CDM creation failed."); |
| 147 return; |
| 148 } |
| 149 |
87 const char kPrefixedApiPersistentSessionHeader[] = "PERSISTENT|"; | 150 const char kPrefixedApiPersistentSessionHeader[] = "PERSISTENT|"; |
88 const char kPrefixedApiLoadSessionHeader[] = "LOAD_SESSION|"; | 151 const char kPrefixedApiLoadSessionHeader[] = "LOAD_SESSION|"; |
89 | 152 |
90 SessionCreationType session_creation_type = TemporarySession; | 153 SessionCreationType session_creation_type = TemporarySession; |
91 std::vector<uint8> init_data_vector(init_data, init_data + init_data_length); | 154 std::vector<uint8> stripped_init_data = init_data; |
92 if (HasHeader(init_data, init_data_length, kPrefixedApiLoadSessionHeader)) { | 155 if (HasHeader(init_data, kPrefixedApiLoadSessionHeader)) { |
93 session_creation_type = LoadSession; | 156 session_creation_type = LoadSession; |
94 StripHeader(init_data_vector, strlen(kPrefixedApiLoadSessionHeader)); | 157 StripHeader(stripped_init_data, strlen(kPrefixedApiLoadSessionHeader)); |
95 } else if (HasHeader(init_data, | 158 } else if (HasHeader(init_data, kPrefixedApiPersistentSessionHeader)) { |
96 init_data_length, | |
97 kPrefixedApiPersistentSessionHeader)) { | |
98 session_creation_type = PersistentSession; | 159 session_creation_type = PersistentSession; |
99 StripHeader(init_data_vector, strlen(kPrefixedApiPersistentSessionHeader)); | 160 StripHeader(stripped_init_data, |
| 161 strlen(kPrefixedApiPersistentSessionHeader)); |
100 } | 162 } |
101 | 163 |
102 scoped_ptr<NewSessionCdmPromise> promise(new CdmCallbackPromise<std::string>( | 164 scoped_ptr<NewSessionCdmPromise> promise(new CdmCallbackPromise<std::string>( |
103 base::Bind(&ProxyDecryptor::SetSessionId, weak_ptr_factory_.GetWeakPtr(), | 165 base::Bind(&ProxyDecryptor::SetSessionId, weak_ptr_factory_.GetWeakPtr(), |
104 session_creation_type), | 166 session_creation_type), |
105 base::Bind(&ProxyDecryptor::OnLegacySessionError, | 167 base::Bind(&ProxyDecryptor::OnLegacySessionError, |
106 weak_ptr_factory_.GetWeakPtr(), | 168 weak_ptr_factory_.GetWeakPtr(), |
107 std::string()))); // No session id until created. | 169 std::string()))); // No session id until created. |
108 | 170 |
109 if (session_creation_type == LoadSession) { | 171 if (session_creation_type == LoadSession) { |
110 media_keys_->LoadSession( | 172 media_keys_->LoadSession( |
111 MediaKeys::PERSISTENT_LICENSE_SESSION, | 173 MediaKeys::PERSISTENT_LICENSE_SESSION, |
112 std::string( | 174 std::string( |
113 reinterpret_cast<const char*>(vector_as_array(&init_data_vector)), | 175 reinterpret_cast<const char*>(vector_as_array(&stripped_init_data)), |
114 init_data_vector.size()), | 176 stripped_init_data.size()), |
115 promise.Pass()); | 177 promise.Pass()); |
116 return true; | 178 return; |
117 } | 179 } |
118 | 180 |
119 MediaKeys::SessionType session_type = | 181 MediaKeys::SessionType session_type = |
120 session_creation_type == PersistentSession | 182 session_creation_type == PersistentSession |
121 ? MediaKeys::PERSISTENT_LICENSE_SESSION | 183 ? MediaKeys::PERSISTENT_LICENSE_SESSION |
122 : MediaKeys::TEMPORARY_SESSION; | 184 : MediaKeys::TEMPORARY_SESSION; |
123 | 185 |
124 // No permission required when AesDecryptor is used or when the key system is | 186 // No permission required when AesDecryptor is used or when the key system is |
125 // external clear key. | 187 // external clear key. |
126 DCHECK(!key_system_.empty()); | 188 DCHECK(!key_system_.empty()); |
127 if (CanUseAesDecryptor(key_system_) || IsExternalClearKey(key_system_)) { | 189 if (CanUseAesDecryptor(key_system_) || IsExternalClearKey(key_system_)) { |
128 OnPermissionStatus(session_type, init_data_type, init_data_vector, | 190 OnPermissionStatus(session_type, init_data_type, stripped_init_data, |
129 promise.Pass(), true /* granted */); | 191 promise.Pass(), true /* granted */); |
130 return true; | 192 return; |
131 } | 193 } |
132 | 194 |
133 #if defined(OS_CHROMEOS) || defined(OS_ANDROID) | 195 #if defined(OS_CHROMEOS) || defined(OS_ANDROID) |
134 media_permission_->RequestPermission( | 196 media_permission_->RequestPermission( |
135 MediaPermission::PROTECTED_MEDIA_IDENTIFIER, security_origin_, | 197 MediaPermission::PROTECTED_MEDIA_IDENTIFIER, security_origin_, |
136 base::Bind(&ProxyDecryptor::OnPermissionStatus, | 198 base::Bind(&ProxyDecryptor::OnPermissionStatus, |
137 weak_ptr_factory_.GetWeakPtr(), session_type, init_data_type, | 199 weak_ptr_factory_.GetWeakPtr(), session_type, init_data_type, |
138 init_data_vector, base::Passed(&promise))); | 200 stripped_init_data, base::Passed(&promise))); |
139 #else | 201 #else |
140 OnPermissionStatus(session_type, init_data_type, init_data_vector, | 202 OnPermissionStatus(session_type, init_data_type, stripped_init_data, |
141 promise.Pass(), true /* granted */); | 203 promise.Pass(), true /* granted */); |
142 #endif | 204 #endif |
143 | |
144 return true; | |
145 } | 205 } |
146 | 206 |
147 void ProxyDecryptor::OnPermissionStatus( | 207 void ProxyDecryptor::OnPermissionStatus( |
148 MediaKeys::SessionType session_type, | 208 MediaKeys::SessionType session_type, |
149 EmeInitDataType init_data_type, | 209 EmeInitDataType init_data_type, |
150 const std::vector<uint8>& init_data, | 210 const std::vector<uint8>& init_data, |
151 scoped_ptr<NewSessionCdmPromise> promise, | 211 scoped_ptr<NewSessionCdmPromise> promise, |
152 bool granted) { | 212 bool granted) { |
153 // ProxyDecryptor is only used by Prefixed EME, where RequestPermission() is | 213 // ProxyDecryptor is only used by Prefixed EME, where RequestPermission() is |
154 // only for triggering the permission UI. Later CheckPermission() will be | 214 // only for triggering the permission UI. Later CheckPermission() will be |
155 // called (e.g. in PlatformVerificationFlow on ChromeOS; in BrowserCdmManager | 215 // called (e.g. in PlatformVerificationFlow on ChromeOS; in BrowserCdmManager |
156 // on Android) and the permission status will be evaluated then. | 216 // on Android) and the permission status will be evaluated then. |
157 DVLOG_IF(1, !granted) << "Permission request rejected."; | 217 DVLOG_IF(1, !granted) << "Permission request rejected."; |
158 | 218 |
159 media_keys_->CreateSessionAndGenerateRequest( | 219 media_keys_->CreateSessionAndGenerateRequest( |
160 session_type, init_data_type, vector_as_array(&init_data), | 220 session_type, init_data_type, vector_as_array(&init_data), |
161 init_data.size(), promise.Pass()); | 221 init_data.size(), promise.Pass()); |
162 } | 222 } |
163 | 223 |
164 void ProxyDecryptor::AddKey(const uint8* key, | 224 void ProxyDecryptor::AddKey(const uint8* key, |
165 int key_length, | 225 int key_length, |
166 const uint8* init_data, | 226 const uint8* init_data, |
167 int init_data_length, | 227 int init_data_length, |
168 const std::string& session_id) { | 228 const std::string& session_id) { |
169 DVLOG(1) << "AddKey()"; | 229 DVLOG(1) << "AddKey()"; |
170 | 230 |
| 231 if (!media_keys_) { |
| 232 OnLegacySessionError(std::string(), MediaKeys::INVALID_STATE_ERROR, 0, |
| 233 "CDM is not available."); |
| 234 return; |
| 235 } |
| 236 |
171 // In the prefixed API, the session parameter provided to addKey() is | 237 // In the prefixed API, the session parameter provided to addKey() is |
172 // optional, so use the single existing session if it exists. | 238 // optional, so use the single existing session if it exists. |
173 std::string new_session_id(session_id); | 239 std::string new_session_id(session_id); |
174 if (new_session_id.empty()) { | 240 if (new_session_id.empty()) { |
175 if (active_sessions_.size() == 1) { | 241 if (active_sessions_.size() == 1) { |
176 base::hash_map<std::string, bool>::iterator it = active_sessions_.begin(); | 242 base::hash_map<std::string, bool>::iterator it = active_sessions_.begin(); |
177 new_session_id = it->first; | 243 new_session_id = it->first; |
178 } else { | 244 } else { |
179 OnLegacySessionError(std::string(), MediaKeys::NOT_SUPPORTED_ERROR, 0, | 245 OnLegacySessionError(std::string(), MediaKeys::NOT_SUPPORTED_ERROR, 0, |
180 "SessionId not specified."); | 246 "SessionId not specified."); |
(...skipping 28 matching lines...) Expand all Loading... |
209 jwk.size(), promise.Pass()); | 275 jwk.size(), promise.Pass()); |
210 return; | 276 return; |
211 } | 277 } |
212 | 278 |
213 media_keys_->UpdateSession(new_session_id, key, key_length, promise.Pass()); | 279 media_keys_->UpdateSession(new_session_id, key, key_length, promise.Pass()); |
214 } | 280 } |
215 | 281 |
216 void ProxyDecryptor::CancelKeyRequest(const std::string& session_id) { | 282 void ProxyDecryptor::CancelKeyRequest(const std::string& session_id) { |
217 DVLOG(1) << "CancelKeyRequest()"; | 283 DVLOG(1) << "CancelKeyRequest()"; |
218 | 284 |
| 285 if (!media_keys_) { |
| 286 OnLegacySessionError(std::string(), MediaKeys::INVALID_STATE_ERROR, 0, |
| 287 "CDM is not available."); |
| 288 return; |
| 289 } |
| 290 |
219 scoped_ptr<SimpleCdmPromise> promise(new CdmCallbackPromise<>( | 291 scoped_ptr<SimpleCdmPromise> promise(new CdmCallbackPromise<>( |
220 base::Bind(&ProxyDecryptor::OnSessionClosed, | 292 base::Bind(&ProxyDecryptor::OnSessionClosed, |
221 weak_ptr_factory_.GetWeakPtr(), session_id), | 293 weak_ptr_factory_.GetWeakPtr(), session_id), |
222 base::Bind(&ProxyDecryptor::OnLegacySessionError, | 294 base::Bind(&ProxyDecryptor::OnLegacySessionError, |
223 weak_ptr_factory_.GetWeakPtr(), session_id))); | 295 weak_ptr_factory_.GetWeakPtr(), session_id))); |
224 media_keys_->RemoveSession(session_id, promise.Pass()); | 296 media_keys_->RemoveSession(session_id, promise.Pass()); |
225 } | 297 } |
226 | 298 |
227 scoped_ptr<MediaKeys> ProxyDecryptor::CreateMediaKeys( | |
228 CdmFactory* cdm_factory, | |
229 const std::string& key_system, | |
230 const GURL& security_origin) { | |
231 // TODO(sandersd): Trigger permissions check here and use it to determine | |
232 // distinctive identifier support, instead of always requiring the | |
233 // permission. http://crbug.com/455271 | |
234 bool allow_distinctive_identifier = true; | |
235 bool allow_persistent_state = true; | |
236 base::WeakPtr<ProxyDecryptor> weak_this = weak_ptr_factory_.GetWeakPtr(); | |
237 return cdm_factory->Create( | |
238 key_system, allow_distinctive_identifier, allow_persistent_state, | |
239 security_origin, base::Bind(&ProxyDecryptor::OnSessionMessage, weak_this), | |
240 base::Bind(&ProxyDecryptor::OnSessionClosed, weak_this), | |
241 base::Bind(&ProxyDecryptor::OnLegacySessionError, weak_this), | |
242 base::Bind(&ProxyDecryptor::OnSessionKeysChange, weak_this), | |
243 base::Bind(&ProxyDecryptor::OnSessionExpirationUpdate, weak_this)); | |
244 } | |
245 | |
246 void ProxyDecryptor::OnSessionMessage(const std::string& session_id, | 299 void ProxyDecryptor::OnSessionMessage(const std::string& session_id, |
247 MediaKeys::MessageType message_type, | 300 MediaKeys::MessageType message_type, |
248 const std::vector<uint8>& message, | 301 const std::vector<uint8>& message, |
249 const GURL& legacy_destination_url) { | 302 const GURL& legacy_destination_url) { |
250 // Assumes that OnSessionCreated() has been called before this. | 303 // Assumes that OnSessionCreated() has been called before this. |
251 | 304 |
252 // For ClearKey, convert the message from JSON into just passing the key | 305 // For ClearKey, convert the message from JSON into just passing the key |
253 // as the message. If unable to extract the key, return the message unchanged. | 306 // as the message. If unable to extract the key, return the message unchanged. |
254 if (is_clear_key_) { | 307 if (is_clear_key_) { |
255 std::vector<uint8> key; | 308 std::vector<uint8> key; |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
333 bool is_persistent = | 386 bool is_persistent = |
334 session_type == PersistentSession || session_type == LoadSession; | 387 session_type == PersistentSession || session_type == LoadSession; |
335 active_sessions_.insert(std::make_pair(session_id, is_persistent)); | 388 active_sessions_.insert(std::make_pair(session_id, is_persistent)); |
336 | 389 |
337 // For LoadSession(), generate the KeyAdded event. | 390 // For LoadSession(), generate the KeyAdded event. |
338 if (session_type == LoadSession) | 391 if (session_type == LoadSession) |
339 GenerateKeyAdded(session_id); | 392 GenerateKeyAdded(session_id); |
340 } | 393 } |
341 | 394 |
342 } // namespace media | 395 } // namespace media |
OLD | NEW |