| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 "components/cdm/browser/media_drm_storage_impl.h" | 5 #include "components/cdm/browser/media_drm_storage_impl.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
| 9 #include "components/prefs/pref_registry_simple.h" | 9 #include "components/prefs/pref_registry_simple.h" |
| 10 #include "components/prefs/pref_service.h" | 10 #include "components/prefs/pref_service.h" |
| 11 #include "components/prefs/scoped_user_pref_update.h" | 11 #include "components/prefs/scoped_user_pref_update.h" |
| 12 #include "content/public/browser/browser_thread.h" |
| 12 #include "content/public/browser/navigation_handle.h" | 13 #include "content/public/browser/navigation_handle.h" |
| 13 | 14 |
| 14 // The storage will be managed by PrefService. All data will be stored in a | 15 // The storage will be managed by PrefService. All data will be stored in a |
| 15 // dictionary under the key "media.media_drm_storage". The dictionary is | 16 // dictionary under the key "media.media_drm_storage". The dictionary is |
| 16 // structured as follows: | 17 // structured as follows: |
| 17 // | 18 // |
| 18 // { | 19 // { |
| 19 // $origin: { | 20 // $origin: { |
| 20 // "origin_id": $origin_id | 21 // "origin_id": $origin_id |
| 21 // "creation_time": $creation_time | 22 // "creation_time": $creation_time |
| 22 // "sessions" : { | 23 // "sessions" : { |
| 23 // $session_id: { | 24 // $session_id: { |
| 24 // "key_set_id": $key_set_id, | 25 // "key_set_id": $key_set_id, |
| 25 // "mime_type": $mime_type, | 26 // "mime_type": $mime_type, |
| 26 // "creation_time": $creation_time | 27 // "creation_time": $creation_time |
| 27 // }, | 28 // }, |
| 28 // # more session_id map... | 29 // # more session_id map... |
| 29 // } | 30 // } |
| 30 // }, | 31 // }, |
| 31 // # more origin map... | 32 // # more origin map... |
| 32 // } | 33 // } |
| 33 | 34 |
| 34 namespace cdm { | 35 namespace cdm { |
| 35 | 36 |
| 36 namespace { | 37 namespace { |
| 37 | 38 |
| 38 const char kMediaDrmStorage[] = "media.media_drm_storage"; | 39 const char kMediaDrmStorage[] = "media.media_drm_storage"; |
| 39 const char kCreationTime[] = "creation_time"; | |
| 40 const char kSessions[] = "sessions"; | |
| 41 const char kKeySetId[] = "key_set_id"; | |
| 42 const char kMimeType[] = "mime_type"; | |
| 43 | |
| 44 std::unique_ptr<base::DictionaryValue> CreateOriginDictionary() { | |
| 45 auto dict = base::MakeUnique<base::DictionaryValue>(); | |
| 46 // TODO(xhwang): Create |origin_id|. | |
| 47 dict->SetDouble(kCreationTime, base::Time::Now().ToDoubleT()); | |
| 48 return dict; | |
| 49 } | |
| 50 | |
| 51 std::unique_ptr<base::DictionaryValue> CreateSessionDictionary( | |
| 52 const std::vector<uint8_t>& key_set_id, | |
| 53 const std::string& mime_type) { | |
| 54 auto dict = base::MakeUnique<base::DictionaryValue>(); | |
| 55 dict->SetString(kKeySetId, | |
| 56 std::string(reinterpret_cast<const char*>(key_set_id.data()), | |
| 57 key_set_id.size())); | |
| 58 dict->SetString(kMimeType, mime_type); | |
| 59 dict->SetDouble(kCreationTime, base::Time::Now().ToDoubleT()); | |
| 60 return dict; | |
| 61 } | |
| 62 | |
| 63 bool GetSessionData(const base::DictionaryValue* sesssion_dict, | |
| 64 std::vector<uint8_t>* key_set_id, | |
| 65 std::string* mime_type) { | |
| 66 std::string key_set_id_string; | |
| 67 if (!sesssion_dict->GetString(kKeySetId, &key_set_id_string)) | |
| 68 return false; | |
| 69 | |
| 70 if (!sesssion_dict->GetString(kMimeType, mime_type)) | |
| 71 return false; | |
| 72 | |
| 73 key_set_id->assign(key_set_id_string.begin(), key_set_id_string.end()); | |
| 74 return true; | |
| 75 } | |
| 76 | |
| 77 #if DCHECK_IS_ON() | |
| 78 // Returns whether |dict| has a value assocaited with the |key|. | |
| 79 bool HasEntry(const base::DictionaryValue& dict, const std::string& key) { | |
| 80 return dict.GetDictionaryWithoutPathExpansion(key, nullptr); | |
| 81 } | |
| 82 #endif | |
| 83 | 40 |
| 84 } // namespace | 41 } // namespace |
| 85 | 42 |
| 86 // static | 43 // static |
| 87 void MediaDrmStorageImpl::RegisterProfilePrefs(PrefRegistrySimple* registry) { | 44 void MediaDrmStorageImpl::RegisterProfilePrefs(PrefRegistrySimple* registry) { |
| 88 registry->RegisterDictionaryPref(kMediaDrmStorage); | 45 registry->RegisterDictionaryPref(kMediaDrmStorage); |
| 89 } | 46 } |
| 90 | 47 |
| 91 MediaDrmStorageImpl::MediaDrmStorageImpl( | 48 MediaDrmStorageImpl::MediaDrmStorageImpl( |
| 92 content::RenderFrameHost* render_frame_host, | 49 content::RenderFrameHost* render_frame_host, |
| 93 PrefService* pref_service, | 50 PrefService* pref_service, |
| 94 const url::Origin& origin, | 51 const url::Origin& origin, |
| 95 media::mojom::MediaDrmStorageRequest request) | 52 media::mojom::MediaDrmStorageRequest request) |
| 96 : render_frame_host_(render_frame_host), | 53 : render_frame_host_(render_frame_host), |
| 97 pref_service_(pref_service), | 54 pref_service_(pref_service), |
| 98 origin_(origin), | 55 origin_(origin), |
| 99 origin_string_(origin.Serialize()), | |
| 100 binding_(this, std::move(request)) { | 56 binding_(this, std::move(request)) { |
| 101 DVLOG(1) << __func__ << ": origin = " << origin; | 57 DVLOG(1) << __func__ << ": origin = " << origin; |
| 102 DCHECK(thread_checker_.CalledOnValidThread()); | 58 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 103 DCHECK(pref_service_); | 59 DCHECK(pref_service_); |
| 104 DCHECK(!origin_string_.empty()); | |
| 105 | 60 |
| 106 // |this| owns |binding_|, so unretained is safe. | 61 // |this| owns |binding_|, so unretained is safe. |
| 107 binding_.set_connection_error_handler( | 62 binding_.set_connection_error_handler( |
| 108 base::Bind(&MediaDrmStorageImpl::Close, base::Unretained(this))); | 63 base::Bind(&MediaDrmStorageImpl::Close, base::Unretained(this))); |
| 109 } | 64 } |
| 110 | 65 |
| 111 MediaDrmStorageImpl::~MediaDrmStorageImpl() { | 66 MediaDrmStorageImpl::~MediaDrmStorageImpl() { |
| 112 DVLOG(1) << __func__; | 67 DVLOG(1) << __func__; |
| 113 DCHECK(thread_checker_.CalledOnValidThread()); | 68 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 114 } | 69 } |
| 115 | 70 |
| 116 // TODO(xhwang): Update this function to return an origin ID. If the origin is | 71 // TODO(xhwang): Update this function to return an origin ID. If the origin is |
| 117 // not the same as |origin_|, return an empty origin ID. | 72 // not the same as |origin_|, return an empty origin ID. |
| 118 void MediaDrmStorageImpl::Initialize(const url::Origin& origin) { | 73 void MediaDrmStorageImpl::Initialize(const url::Origin& origin) { |
| 119 DVLOG(1) << __func__ << ": origin = " << origin; | 74 DVLOG(1) << __func__ << ": origin = " << origin; |
| 120 DCHECK(thread_checker_.CalledOnValidThread()); | 75 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 121 DCHECK(!initialized_); | 76 DCHECK(!initialized_); |
| 122 | 77 |
| 123 initialized_ = true; | 78 initialized_ = true; |
| 124 } | 79 } |
| 125 | 80 |
| 126 void MediaDrmStorageImpl::OnProvisioned(const OnProvisionedCallback& callback) { | 81 void MediaDrmStorageImpl::OnProvisioned(const OnProvisionedCallback& callback) { |
| 127 DVLOG(1) << __func__; | 82 DVLOG(1) << __func__; |
| 128 DCHECK(thread_checker_.CalledOnValidThread()); | 83 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 129 | 84 |
| 130 if (!initialized_) { | 85 if (!initialized_) { |
| 131 DVLOG(1) << __func__ << ": Not initialized."; | 86 DVLOG(1) << __func__ << ": Not initialized."; |
| 132 callback.Run(false); | 87 callback.Run(false); |
| 133 return; | 88 return; |
| 134 } | 89 } |
| 135 | 90 |
| 136 DictionaryPrefUpdate update(pref_service_, kMediaDrmStorage); | 91 NOTIMPLEMENTED(); |
| 137 base::DictionaryValue* storage_dict = update.Get(); | 92 callback.Run(false); |
| 138 DCHECK(storage_dict); | |
| 139 | |
| 140 // The origin string may contain dots. Do not use path expansion. | |
| 141 DVLOG_IF(1, HasEntry(*storage_dict, origin_string_)) | |
| 142 << __func__ << ": Entry for origin " << origin_string_ | |
| 143 << " already exists and will be cleared"; | |
| 144 | |
| 145 storage_dict->SetWithoutPathExpansion(origin_string_, | |
| 146 CreateOriginDictionary()); | |
| 147 callback.Run(true); | |
| 148 } | 93 } |
| 149 | 94 |
| 150 void MediaDrmStorageImpl::SavePersistentSession( | 95 void MediaDrmStorageImpl::SavePersistentSession( |
| 151 const std::string& session_id, | 96 const std::string& session_id, |
| 152 media::mojom::SessionDataPtr session_data, | 97 media::mojom::SessionDataPtr session_data, |
| 153 const SavePersistentSessionCallback& callback) { | 98 const SavePersistentSessionCallback& callback) { |
| 154 DVLOG(2) << __func__; | 99 DVLOG(2) << __func__; |
| 155 DCHECK(thread_checker_.CalledOnValidThread()); | 100 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 156 | 101 |
| 157 if (!initialized_) { | 102 if (!initialized_) { |
| 158 DVLOG(1) << __func__ << ": Not initialized."; | 103 DVLOG(1) << __func__ << ": Not initialized."; |
| 159 callback.Run(false); | 104 callback.Run(false); |
| 160 return; | 105 return; |
| 161 } | 106 } |
| 162 | 107 |
| 163 DictionaryPrefUpdate update(pref_service_, kMediaDrmStorage); | 108 NOTIMPLEMENTED(); |
| 164 base::DictionaryValue* storage_dict = update.Get(); | 109 callback.Run(false); |
| 165 DCHECK(storage_dict); | |
| 166 | |
| 167 base::DictionaryValue* origin_dict = nullptr; | |
| 168 // The origin string may contain dots. Do not use path expansion. | |
| 169 storage_dict->GetDictionaryWithoutPathExpansion(origin_string_, &origin_dict); | |
| 170 if (!origin_dict) { | |
| 171 DVLOG(1) << __func__ | |
| 172 << ": Failed to save persistent session data; entry for origin " | |
| 173 << origin_string_ << " does not exist."; | |
| 174 callback.Run(false); | |
| 175 return; | |
| 176 } | |
| 177 | |
| 178 base::DictionaryValue* sessions_dict = nullptr; | |
| 179 if (!origin_dict->GetDictionary(kSessions, &sessions_dict)) { | |
| 180 DVLOG(2) << __func__ << ": No session exists; creating a new dict."; | |
| 181 origin_dict->Set(kSessions, base::MakeUnique<base::DictionaryValue>()); | |
| 182 DCHECK(origin_dict->GetDictionary(kSessions, &sessions_dict)); | |
| 183 } | |
| 184 | |
| 185 DVLOG_IF(1, HasEntry(*sessions_dict, session_id)) | |
| 186 << __func__ << ": Session ID already exists and will be replaced."; | |
| 187 | |
| 188 sessions_dict->SetWithoutPathExpansion( | |
| 189 session_id, CreateSessionDictionary(session_data->key_set_id, | |
| 190 session_data->mime_type)); | |
| 191 | |
| 192 callback.Run(true); | |
| 193 } | 110 } |
| 194 | 111 |
| 195 void MediaDrmStorageImpl::LoadPersistentSession( | 112 void MediaDrmStorageImpl::LoadPersistentSession( |
| 196 const std::string& session_id, | 113 const std::string& session_id, |
| 197 const LoadPersistentSessionCallback& callback) { | 114 const LoadPersistentSessionCallback& callback) { |
| 198 DVLOG(2) << __func__; | 115 DVLOG(2) << __func__; |
| 199 DCHECK(thread_checker_.CalledOnValidThread()); | 116 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 200 | 117 |
| 201 if (!initialized_) { | 118 if (!initialized_) { |
| 202 DVLOG(1) << __func__ << ": Not initialized."; | 119 DVLOG(1) << __func__ << ": Not initialized."; |
| 203 callback.Run(nullptr); | 120 callback.Run(nullptr); |
| 204 return; | 121 return; |
| 205 } | 122 } |
| 206 | 123 |
| 207 const base::DictionaryValue* storage_dict = | 124 NOTIMPLEMENTED(); |
| 208 pref_service_->GetDictionary(kMediaDrmStorage); | 125 callback.Run(nullptr); |
| 209 | |
| 210 const base::DictionaryValue* origin_dict = nullptr; | |
| 211 // The origin string may contain dots. Do not use path expansion. | |
| 212 storage_dict->GetDictionaryWithoutPathExpansion(origin_string_, &origin_dict); | |
| 213 if (!origin_dict) { | |
| 214 DVLOG(1) << __func__ | |
| 215 << ": Failed to save persistent session data; entry for origin " | |
| 216 << origin_ << " does not exist."; | |
| 217 callback.Run(nullptr); | |
| 218 return; | |
| 219 } | |
| 220 | |
| 221 const base::DictionaryValue* sessions_dict = nullptr; | |
| 222 if (!origin_dict->GetDictionary(kSessions, &sessions_dict)) { | |
| 223 DVLOG(2) << __func__ << ": Sessions dictionary does not exist."; | |
| 224 callback.Run(nullptr); | |
| 225 return; | |
| 226 } | |
| 227 | |
| 228 const base::DictionaryValue* session_dict = nullptr; | |
| 229 if (!sessions_dict->GetDictionaryWithoutPathExpansion(session_id, | |
| 230 &session_dict)) { | |
| 231 DVLOG(2) << __func__ << ": Session dictionary does not exist."; | |
| 232 callback.Run(nullptr); | |
| 233 return; | |
| 234 } | |
| 235 | |
| 236 std::vector<uint8_t> key_set_id; | |
| 237 std::string mime_type; | |
| 238 if (!GetSessionData(session_dict, &key_set_id, &mime_type)) { | |
| 239 DVLOG(2) << __func__ << ": Failed to read session data."; | |
| 240 callback.Run(nullptr); | |
| 241 return; | |
| 242 } | |
| 243 | |
| 244 callback.Run(media::mojom::SessionData::New(key_set_id, mime_type)); | |
| 245 } | 126 } |
| 246 | 127 |
| 247 void MediaDrmStorageImpl::RemovePersistentSession( | 128 void MediaDrmStorageImpl::RemovePersistentSession( |
| 248 const std::string& session_id, | 129 const std::string& session_id, |
| 249 const RemovePersistentSessionCallback& callback) { | 130 const RemovePersistentSessionCallback& callback) { |
| 250 DVLOG(2) << __func__; | 131 DVLOG(2) << __func__; |
| 251 DCHECK(thread_checker_.CalledOnValidThread()); | 132 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 252 | 133 |
| 253 if (!initialized_) { | 134 if (!initialized_) { |
| 254 DVLOG(1) << __func__ << ": Not initialized."; | 135 DVLOG(1) << __func__ << ": Not initialized."; |
| 255 callback.Run(false); | 136 callback.Run(false); |
| 256 return; | 137 return; |
| 257 } | 138 } |
| 258 | 139 |
| 259 DictionaryPrefUpdate update(pref_service_, kMediaDrmStorage); | 140 NOTIMPLEMENTED(); |
| 260 base::DictionaryValue* storage_dict = update.Get(); | 141 callback.Run(false); |
| 261 DCHECK(storage_dict); | |
| 262 | |
| 263 base::DictionaryValue* origin_dict = nullptr; | |
| 264 // The origin string may contain dots. Do not use path expansion. | |
| 265 storage_dict->GetDictionaryWithoutPathExpansion(origin_string_, &origin_dict); | |
| 266 if (!origin_dict) { | |
| 267 DVLOG(1) << __func__ << ": Entry for rigin " << origin_string_ | |
| 268 << " does not exist."; | |
| 269 callback.Run(true); | |
| 270 return; | |
| 271 } | |
| 272 | |
| 273 base::DictionaryValue* sessions_dict = nullptr; | |
| 274 if (!origin_dict->GetDictionary(kSessions, &sessions_dict)) { | |
| 275 DVLOG(2) << __func__ << ": Sessions dictionary does not exist."; | |
| 276 callback.Run(true); | |
| 277 return; | |
| 278 } | |
| 279 | |
| 280 sessions_dict->RemoveWithoutPathExpansion(session_id, nullptr); | |
| 281 callback.Run(true); | |
| 282 } | 142 } |
| 283 | 143 |
| 284 void MediaDrmStorageImpl::RenderFrameDeleted( | 144 void MediaDrmStorageImpl::RenderFrameDeleted( |
| 285 content::RenderFrameHost* render_frame_host) { | 145 content::RenderFrameHost* render_frame_host) { |
| 286 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 287 | |
| 288 if (render_frame_host == render_frame_host_) { | 146 if (render_frame_host == render_frame_host_) { |
| 289 DVLOG(1) << __func__ << ": RenderFrame destroyed."; | 147 DVLOG(1) << __func__ << ": RenderFrame destroyed."; |
| 290 Close(); | 148 Close(); |
| 291 } | 149 } |
| 292 } | 150 } |
| 293 | 151 |
| 294 void MediaDrmStorageImpl::DidFinishNavigation( | 152 void MediaDrmStorageImpl::DidFinishNavigation( |
| 295 content::NavigationHandle* navigation_handle) { | 153 content::NavigationHandle* navigation_handle) { |
| 296 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 297 | |
| 298 if (navigation_handle->GetRenderFrameHost() == render_frame_host_) { | 154 if (navigation_handle->GetRenderFrameHost() == render_frame_host_) { |
| 299 DVLOG(1) << __func__ << ": Close connection on navigation."; | 155 DVLOG(1) << __func__ << ": Close connection on navigation."; |
| 300 Close(); | 156 Close(); |
| 301 } | 157 } |
| 302 } | 158 } |
| 303 | 159 |
| 304 void MediaDrmStorageImpl::Close() { | 160 void MediaDrmStorageImpl::Close() { |
| 305 DVLOG(1) << __func__; | 161 DVLOG(1) << __func__; |
| 306 DCHECK(thread_checker_.CalledOnValidThread()); | 162 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 307 | 163 |
| 308 delete this; | 164 delete this; |
| 309 } | 165 } |
| 310 | 166 |
| 311 } // namespace cdm | 167 } // namespace cdm |
| OLD | NEW |