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

Side by Side Diff: components/cdm/browser/media_drm_storage_impl.cc

Issue 2765343003: media: Add MediaDrmStorage (Closed)
Patch Set: Move MediaDrmStorageImpl to components/ 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 unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "components/cdm/browser/media_drm_storage_impl.h"
6
7 #include "base/logging.h"
8 #include "base/memory/ptr_util.h"
9 #include "base/optional.h"
10 #include "components/pref_registry/pref_registry_syncable.h"
11 #include "components/prefs/pref_service.h"
12 #include "components/prefs/scoped_user_pref_update.h"
13 #include "content/public/browser/browser_thread.h"
14
15 // The storage will be managed by PrefService. All data will be store as a
16 // dictionary under the key "media.media_drm_storage". The dictionary is
17 // structured as follows:
18 //
19 // {
20 // $origin: {
21 // "origin_id": $origin_id
22 // "creation_time": $provision_time
23 // $session_id: {
24 // "key_set_id": $key_set_id,
25 // "mime_type": $mime_type,
26 // "creation_time": $creation_time
27 // },
28 // # more session_id map...
29 // },
30 // # more origin map...
31 // }
32
33 namespace cdm {
34
35 namespace {
36
37 const char kMediaDrmStorage[] = "media.media_drm_storage";
38 const char kKeySetId[] = "key_set_id";
39 const char kMimeType[] = "mime_type";
40 const char kCreationTime[] = "creation_time";
41
42 std::unique_ptr<base::DictionaryValue> CreateOriginDictionary() {
43 auto dict = base::MakeUnique<base::DictionaryValue>();
44 // TODO(xhwang): Base64 encode the |key_set_id|?
45 // TODO(xhwang): Add creation time.
46 dict->SetDouble(kCreationTime, base::Time::Now().ToDoubleT());
47 return dict;
48 }
49
50 std::unique_ptr<base::DictionaryValue> CreateSessionDictionary(
51 const std::vector<uint8_t>& key_set_id,
52 const std::string& mime_type) {
53 auto dict = base::MakeUnique<base::DictionaryValue>();
54 // TODO(xhwang): Base64 encode the |key_set_id|?
55 // TODO(xhwang): Add creation time.
56 dict->SetString(kKeySetId,
57 std::string(reinterpret_cast<const char*>(key_set_id.data()),
58 key_set_id.size()));
59 dict->SetString(kMimeType, mime_type);
60 return dict;
61 }
62
63 } // namespace
64
65 // static
66 void MediaDrmStorageImpl::RegisterProfilePrefs(
67 user_prefs::PrefRegistrySyncable* registry) {
68 registry->RegisterDictionaryPref(kMediaDrmStorage,
69 new base::DictionaryValue());
70 }
71
72 MediaDrmStorageImpl::MediaDrmStorageImpl(PrefService* pref_service)
73 : pref_service_(pref_service), weak_factory_(this) {
74 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
75 DCHECK(pref_service);
76 }
77
78 MediaDrmStorageImpl::~MediaDrmStorageImpl() {
79 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
80 }
81
82 void MediaDrmStorageImpl::Initialize(const url::Origin& origin) {
83 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
84 DCHECK(!origin.unique());
85 origin_ = origin.Serialize();
86 }
87
88 void MediaDrmStorageImpl::OnProvisioned(const OnProvisionedCallback& callback) {
89 DVLOG(2) << __func__;
90 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
91 DCHECK(!origin_.empty());
92
93 DictionaryPrefUpdate update(pref_service_, kMediaDrmStorage);
94 base::DictionaryValue* storage_dict = update.Get();
95 DCHECK(storage_dict);
96
97 // The origin string may contain dots. Do not use path expansion.
98 if (storage_dict->GetDictionaryWithoutPathExpansion(origin_, nullptr)) {
99 DVLOG(1) << __func__
100 << ": Failed to save persistent session data; entry for origin "
101 << origin_ << " already exists.";
102 callback.Run(false);
103 return;
104 }
105
106 storage_dict->SetWithoutPathExpansion(origin_, CreateOriginDictionary());
107 callback.Run(true);
108 }
109
110 void MediaDrmStorageImpl::SavePersistentSession(
111 const std::string& session_id,
112 const std::vector<uint8_t>& key_set_id,
113 const std::string& mime_type,
114 const SavePersistentSessionCallback& callback) {
115 DVLOG(2) << __func__;
116 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
117 DCHECK(!origin_.empty());
118
119 DictionaryPrefUpdate update(pref_service_, kMediaDrmStorage);
120 base::DictionaryValue* storage_dict = update.Get();
121 DCHECK(storage_dict);
122
123 base::DictionaryValue* origin_dict = nullptr;
124 // The origin string may contain dots. Do not use path expansion.
125 storage_dict->GetDictionaryWithoutPathExpansion(origin_, &origin_dict);
126 if (!origin_dict) {
127 DVLOG(1) << __func__
128 << ": Failed to save persistent session data; entry for origin "
129 << origin_ << " does not exist.";
130 callback.Run(false);
131 return;
132 }
133
134 if (origin_dict->GetDictionaryWithoutPathExpansion(session_id, nullptr)) {
135 DVLOG(1) << __func__ << ": Session ID already exists";
136 callback.Run(false);
137 return;
138 }
139
140 origin_dict->SetWithoutPathExpansion(
141 session_id, CreateSessionDictionary(key_set_id, mime_type));
142
143 callback.Run(true);
144 }
145
146 void MediaDrmStorageImpl::LoadPersistentSession(
147 const std::string& session_id,
148 const LoadPersistentSessionCallback& callback) {
149 DVLOG(2) << __func__;
150 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
151 DCHECK(!origin_.empty());
152
153 NOTIMPLEMENTED();
154 callback.Run(false, base::nullopt, base::nullopt);
155 }
156
157 void MediaDrmStorageImpl::RemovePersistentSession(
158 const std::string& session_id,
159 const RemovePersistentSessionCallback& callback) {
160 DVLOG(2) << __func__;
161 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
162 DCHECK(!origin_.empty());
163
164 NOTIMPLEMENTED();
165 callback.Run(false);
166 }
167
168 } // namespace cdm
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698