Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(222)

Unified Diff: components/cdm/browser/media_drm_storage_impl.cc

Issue 2791903004: media: Implement MediaDrmStorageImpl with tests (Closed)
Patch Set: fix rebase errors Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: components/cdm/browser/media_drm_storage_impl.cc
diff --git a/components/cdm/browser/media_drm_storage_impl.cc b/components/cdm/browser/media_drm_storage_impl.cc
index 458a48f03001ac6913a220e3a162a6b1bb8c17db..a7ecc138cf8cdbab085839b74667ec3153db595a 100644
--- a/components/cdm/browser/media_drm_storage_impl.cc
+++ b/components/cdm/browser/media_drm_storage_impl.cc
@@ -9,7 +9,6 @@
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
#include "components/prefs/scoped_user_pref_update.h"
-#include "content/public/browser/browser_thread.h"
#include "content/public/browser/navigation_handle.h"
// The storage will be managed by PrefService. All data will be stored in a
@@ -37,6 +36,43 @@ namespace cdm {
namespace {
const char kMediaDrmStorage[] = "media.media_drm_storage";
+const char kCreationTime[] = "creation_time";
+const char kSessions[] = "sessions";
+const char kKeySetId[] = "key_set_id";
+const char kMimeType[] = "mime_type";
+
+std::unique_ptr<base::DictionaryValue> CreateOriginDictionary() {
+ auto dict = base::MakeUnique<base::DictionaryValue>();
+ // TODO(xhwang): Create |origin_id|.
+ dict->SetDouble(kCreationTime, base::Time::Now().ToDoubleT());
+ return dict;
+}
+
+std::unique_ptr<base::DictionaryValue> CreateSessionDictionary(
+ const std::vector<uint8_t>& key_set_id,
+ const std::string& mime_type) {
+ auto dict = base::MakeUnique<base::DictionaryValue>();
+ dict->SetString(kKeySetId,
+ std::string(reinterpret_cast<const char*>(key_set_id.data()),
+ key_set_id.size()));
+ dict->SetString(kMimeType, mime_type);
+ dict->SetDouble(kCreationTime, base::Time::Now().ToDoubleT());
+ return dict;
+}
+
+bool GetSessionData(const base::DictionaryValue* sesssion_dict,
+ std::vector<uint8_t>* key_set_id,
+ std::string* mime_type) {
+ std::string key_set_id_string;
+ if (!sesssion_dict->GetString(kKeySetId, &key_set_id_string))
+ return false;
+
+ if (!sesssion_dict->GetString(kMimeType, mime_type))
+ return false;
+
+ key_set_id->assign(key_set_id_string.begin(), key_set_id_string.end());
+ return true;
+}
} // namespace
@@ -53,10 +89,12 @@ MediaDrmStorageImpl::MediaDrmStorageImpl(
: render_frame_host_(render_frame_host),
pref_service_(pref_service),
origin_(origin),
+ origin_string_(origin.Serialize()),
binding_(this, std::move(request)) {
DVLOG(1) << __func__ << ": origin = " << origin;
- DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
+ DCHECK(thread_checker_.CalledOnValidThread());
DCHECK(pref_service_);
+ DCHECK(!origin_string_.empty());
// |this| owns |binding_|, so unretained is safe.
binding_.set_connection_error_handler(
@@ -65,14 +103,14 @@ MediaDrmStorageImpl::MediaDrmStorageImpl(
MediaDrmStorageImpl::~MediaDrmStorageImpl() {
DVLOG(1) << __func__;
- DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
+ DCHECK(thread_checker_.CalledOnValidThread());
}
// TODO(xhwang): Update this function to return an origin ID. If the origin is
// not the same as |origin_|, return an empty origin ID.
void MediaDrmStorageImpl::Initialize(const url::Origin& origin) {
DVLOG(1) << __func__ << ": origin = " << origin;
- DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
+ DCHECK(thread_checker_.CalledOnValidThread());
DCHECK(!initialized_);
initialized_ = true;
@@ -80,7 +118,7 @@ void MediaDrmStorageImpl::Initialize(const url::Origin& origin) {
void MediaDrmStorageImpl::OnProvisioned(const OnProvisionedCallback& callback) {
DVLOG(1) << __func__;
- DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
+ DCHECK(thread_checker_.CalledOnValidThread());
if (!initialized_) {
DVLOG(1) << __func__ << ": Not initialized.";
@@ -88,8 +126,23 @@ void MediaDrmStorageImpl::OnProvisioned(const OnProvisionedCallback& callback) {
return;
}
- NOTIMPLEMENTED();
- callback.Run(false);
+ DictionaryPrefUpdate update(pref_service_, kMediaDrmStorage);
+ base::DictionaryValue* storage_dict = update.Get();
+ DCHECK(storage_dict);
+
+ // The origin string may contain dots. Do not use path expansion.
+ if (storage_dict->GetDictionaryWithoutPathExpansion(origin_string_,
+ nullptr)) {
+ DVLOG(1) << __func__
+ << ": Failed to save persistent session data; entry for origin "
+ << origin_string_ << " already exists.";
+ callback.Run(false);
yucliu1 2017/04/04 06:50:00 If this happens, all information associated with t
xhwang 2017/04/06 22:00:51 Updated the implementation to clear the old dictio
+ return;
+ }
+
+ storage_dict->SetWithoutPathExpansion(origin_string_,
+ CreateOriginDictionary());
+ callback.Run(true);
}
void MediaDrmStorageImpl::SavePersistentSession(
@@ -97,7 +150,7 @@ void MediaDrmStorageImpl::SavePersistentSession(
media::mojom::SessionDataPtr session_data,
const SavePersistentSessionCallback& callback) {
DVLOG(2) << __func__;
- DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
+ DCHECK(thread_checker_.CalledOnValidThread());
if (!initialized_) {
DVLOG(1) << __func__ << ": Not initialized.";
@@ -105,15 +158,46 @@ void MediaDrmStorageImpl::SavePersistentSession(
return;
}
- NOTIMPLEMENTED();
- callback.Run(false);
+ DictionaryPrefUpdate update(pref_service_, kMediaDrmStorage);
+ base::DictionaryValue* storage_dict = update.Get();
+ DCHECK(storage_dict);
+
+ base::DictionaryValue* origin_dict = nullptr;
+ // The origin string may contain dots. Do not use path expansion.
+ storage_dict->GetDictionaryWithoutPathExpansion(origin_string_, &origin_dict);
+ if (!origin_dict) {
+ DVLOG(1) << __func__
+ << ": Failed to save persistent session data; entry for origin "
+ << origin_string_ << " does not exist.";
+ callback.Run(false);
+ return;
+ }
+
+ base::DictionaryValue* sessions_dict = nullptr;
+ if (!origin_dict->GetDictionary(kSessions, &sessions_dict)) {
+ DVLOG(2) << __func__ << ": No session exists; creating a new dict.";
+ origin_dict->Set(kSessions, base::MakeUnique<base::DictionaryValue>());
+ DCHECK(origin_dict->GetDictionary(kSessions, &sessions_dict));
+ }
+
+ if (sessions_dict->GetDictionaryWithoutPathExpansion(session_id, nullptr)) {
+ DVLOG(1) << __func__ << ": Session ID already exists";
+ callback.Run(false);
yucliu1 2017/04/04 06:50:00 I think offline licenses can be updated, in which
xhwang 2017/04/06 22:00:51 Good point. Done.
+ return;
+ }
+
+ sessions_dict->SetWithoutPathExpansion(
+ session_id, CreateSessionDictionary(session_data->key_set_id,
+ session_data->mime_type));
+
+ callback.Run(true);
}
void MediaDrmStorageImpl::LoadPersistentSession(
const std::string& session_id,
const LoadPersistentSessionCallback& callback) {
DVLOG(2) << __func__;
- DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
+ DCHECK(thread_checker_.CalledOnValidThread());
if (!initialized_) {
DVLOG(1) << __func__ << ": Not initialized.";
@@ -121,15 +205,51 @@ void MediaDrmStorageImpl::LoadPersistentSession(
return;
}
- NOTIMPLEMENTED();
- callback.Run(nullptr);
+ const base::DictionaryValue* storage_dict =
+ pref_service_->GetDictionary(kMediaDrmStorage);
+
+ const base::DictionaryValue* origin_dict = nullptr;
+ // The origin string may contain dots. Do not use path expansion.
+ storage_dict->GetDictionaryWithoutPathExpansion(origin_string_, &origin_dict);
+ if (!origin_dict) {
+ DVLOG(1) << __func__
+ << ": Failed to save persistent session data; entry for origin "
+ << origin_ << " does not exist.";
+ callback.Run(nullptr);
+ return;
+ }
+
+ const base::DictionaryValue* sessions_dict = nullptr;
+ if (!origin_dict->GetDictionary(kSessions, &sessions_dict)) {
+ DVLOG(2) << __func__ << ": Sessions dictionary does not exist.";
+ callback.Run(nullptr);
+ return;
+ }
+
+ const base::DictionaryValue* session_dict = nullptr;
+ if (!sessions_dict->GetDictionaryWithoutPathExpansion(session_id,
+ &session_dict)) {
+ DVLOG(2) << __func__ << ": Session dictionary does not exist.";
+ callback.Run(nullptr);
+ return;
+ }
+
+ std::vector<uint8_t> key_set_id;
+ std::string mime_type;
+ if (!GetSessionData(session_dict, &key_set_id, &mime_type)) {
+ DVLOG(2) << __func__ << ": Failed to read session data.";
+ callback.Run(nullptr);
yucliu1 2017/04/04 06:50:00 MediaDrmBridge level has logic to clear storage wh
xhwang 2017/04/06 22:00:51 That should never happen unless user manually modi
+ return;
+ }
+
+ callback.Run(media::mojom::SessionData::New(key_set_id, mime_type));
}
void MediaDrmStorageImpl::RemovePersistentSession(
const std::string& session_id,
const RemovePersistentSessionCallback& callback) {
DVLOG(2) << __func__;
- DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
+ DCHECK(thread_checker_.CalledOnValidThread());
if (!initialized_) {
DVLOG(1) << __func__ << ": Not initialized.";
@@ -137,12 +257,34 @@ void MediaDrmStorageImpl::RemovePersistentSession(
return;
}
- NOTIMPLEMENTED();
- callback.Run(false);
+ DictionaryPrefUpdate update(pref_service_, kMediaDrmStorage);
+ base::DictionaryValue* storage_dict = update.Get();
+ DCHECK(storage_dict);
+
+ base::DictionaryValue* origin_dict = nullptr;
+ // The origin string may contain dots. Do not use path expansion.
+ storage_dict->GetDictionaryWithoutPathExpansion(origin_string_, &origin_dict);
+ if (!origin_dict) {
+ DVLOG(1) << __func__ << ": Entry for rigin " << origin_string_
+ << " does not exist.";
+ callback.Run(false);
+ return;
+ }
+
+ base::DictionaryValue* sessions_dict = nullptr;
+ if (!origin_dict->GetDictionary(kSessions, &sessions_dict)) {
+ DVLOG(2) << __func__ << ": Sessions dictionary does not exist.";
+ callback.Run(false);
+ return;
+ }
+
+ callback.Run(sessions_dict->RemoveWithoutPathExpansion(session_id, nullptr));
yucliu1 2017/04/04 06:50:00 If session_id doesn't exist, this should be treate
xhwang 2017/04/06 22:00:51 Done.
}
void MediaDrmStorageImpl::RenderFrameDeleted(
content::RenderFrameHost* render_frame_host) {
+ DCHECK(thread_checker_.CalledOnValidThread());
+
if (render_frame_host == render_frame_host_) {
DVLOG(1) << __func__ << ": RenderFrame destroyed.";
Close();
@@ -151,6 +293,8 @@ void MediaDrmStorageImpl::RenderFrameDeleted(
void MediaDrmStorageImpl::DidFinishNavigation(
content::NavigationHandle* navigation_handle) {
+ DCHECK(thread_checker_.CalledOnValidThread());
+
if (navigation_handle->GetRenderFrameHost() == render_frame_host_) {
DVLOG(1) << __func__ << ": Close connection on navigation.";
Close();
@@ -159,7 +303,7 @@ void MediaDrmStorageImpl::DidFinishNavigation(
void MediaDrmStorageImpl::Close() {
DVLOG(1) << __func__;
- DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
+ DCHECK(thread_checker_.CalledOnValidThread());
delete this;
}

Powered by Google App Engine
This is Rietveld 408576698