OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 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 "chrome/browser/media/android/cdm/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 "chrome/browser/profiles/profile.h" | |
xhwang
2017/03/23 05:25:26
yucliu: This is the only place where this file dep
yucliu1
2017/03/23 16:36:49
This would be helpful!
https://cs.chromium.org/ch
| |
11 #include "components/pref_registry/pref_registry_syncable.h" | |
12 #include "components/prefs/pref_service.h" | |
13 #include "components/prefs/scoped_user_pref_update.h" | |
14 #include "content/public/browser/browser_context.h" | |
15 #include "content/public/browser/browser_thread.h" | |
16 #include "content/public/browser/web_contents.h" | |
17 #include "mojo/public/cpp/bindings/strong_binding.h" | |
18 | |
19 // The storage will be managed by PrefService. All data will be store as a | |
20 // dictionary under the key "media.media_drm_storage". The dictionary is | |
21 // structured as follows: | |
22 // | |
23 // { | |
24 // $origin: { | |
25 // "origin_id": $origin_id | |
yucliu1
2017/03/23 01:07:16
So origin_id is a random string, right? Who's resp
xhwang
2017/03/23 05:22:06
Good question. It's just a random string. In theor
| |
26 // "creation_time": $provision_time | |
27 // $session_id: { | |
28 // "key_set_id": $key_set_id, | |
29 // "mime_type": $mime_type, | |
30 // "creation_time": $creation_time | |
31 // }, | |
32 // # more session_id map... | |
33 // }, | |
34 // # more origin map... | |
35 // } | |
36 | |
37 namespace chrome { | |
38 | |
39 namespace { | |
40 | |
41 const char kMediaDrmStorage[] = "media.media_drm_storage"; | |
42 const char kKeySetId[] = "key_set_id"; | |
43 const char kMimeType[] = "mime_type"; | |
44 const char kCreationTime[] = "creation_time"; | |
45 | |
46 std::unique_ptr<base::DictionaryValue> CreateOriginDictionary() { | |
47 auto dict = base::MakeUnique<base::DictionaryValue>(); | |
48 // TODO(xhwang): Base64 encode the |key_set_id|? | |
49 // TODO(xhwang): Add creation time. | |
50 dict->SetDouble(kCreationTime, base::Time::Now().ToDoubleT()); | |
51 return dict; | |
52 } | |
53 | |
54 std::unique_ptr<base::DictionaryValue> CreateSessionDictionary( | |
55 const std::vector<uint8_t>& key_set_id, | |
56 const std::string& mime_type) { | |
57 auto dict = base::MakeUnique<base::DictionaryValue>(); | |
58 // TODO(xhwang): Base64 encode the |key_set_id|? | |
59 // TODO(xhwang): Add creation time. | |
60 dict->SetString(kKeySetId, | |
61 std::string(reinterpret_cast<const char*>(key_set_id.data()), | |
62 key_set_id.size())); | |
63 dict->SetString(kMimeType, mime_type); | |
64 return dict; | |
65 } | |
66 | |
67 } // namespace | |
68 | |
69 // static | |
70 void MediaDrmStorageImpl::RegisterProfilePrefs( | |
71 user_prefs::PrefRegistrySyncable* registry) { | |
72 registry->RegisterDictionaryPref(kMediaDrmStorage, | |
73 new base::DictionaryValue()); | |
74 } | |
75 | |
76 // static | |
77 void MediaDrmStorageImpl::Create(content::RenderFrameHost* render_frame_host, | |
78 media::mojom::MediaDrmStorageRequest request) { | |
79 DVLOG(2) << __func__; | |
80 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
81 DCHECK(render_frame_host); | |
82 | |
83 content::WebContents* web_contents = | |
84 content::WebContents::FromRenderFrameHost(render_frame_host); | |
85 if (!web_contents) | |
86 return; | |
87 | |
88 content::BrowserContext* browser_context = web_contents->GetBrowserContext(); | |
89 if (!browser_context) | |
90 return; | |
91 | |
92 Profile* profile = Profile::FromBrowserContext(browser_context); | |
93 if (!profile) | |
94 return; | |
95 | |
96 PrefService* pref_service = profile->GetPrefs(); | |
97 if (!pref_service) | |
98 return; | |
99 | |
100 mojo::MakeStrongBinding(base::MakeUnique<MediaDrmStorageImpl>(pref_service), | |
101 std::move(request)); | |
102 } | |
103 | |
104 MediaDrmStorageImpl::MediaDrmStorageImpl(PrefService* pref_service) | |
105 : pref_service_(pref_service), weak_factory_(this) { | |
106 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
107 DCHECK(pref_service); | |
108 } | |
109 | |
110 MediaDrmStorageImpl::~MediaDrmStorageImpl() { | |
111 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
112 } | |
113 | |
114 void MediaDrmStorageImpl::Initialize(const url::Origin& origin) { | |
115 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
116 DCHECK(!origin.unique()); | |
117 origin_ = origin.Serialize(); | |
118 } | |
119 | |
120 void MediaDrmStorageImpl::OnProvisioned(const OnProvisionedCallback& callback) { | |
121 DVLOG(2) << __func__; | |
122 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
123 DCHECK(!origin_.empty()); | |
124 | |
125 DictionaryPrefUpdate update(pref_service_, kMediaDrmStorage); | |
126 base::DictionaryValue* storage_dict = update.Get(); | |
127 DCHECK(storage_dict); | |
128 | |
129 // The origin string may contain dots. Do not use path expansion. | |
130 if (storage_dict->GetDictionaryWithoutPathExpansion(origin_, nullptr)) { | |
131 DVLOG(1) << __func__ | |
132 << ": Failed to save persistent session data; entry for origin " | |
133 << origin_ << " already exists."; | |
134 callback.Run(false); | |
135 return; | |
136 } | |
137 | |
138 storage_dict->SetWithoutPathExpansion(origin_, CreateOriginDictionary()); | |
139 callback.Run(true); | |
140 } | |
141 | |
142 void MediaDrmStorageImpl::SavePersistentSession( | |
143 const std::string& session_id, | |
144 const std::vector<uint8_t>& key_set_id, | |
145 const std::string& mime_type, | |
146 const SavePersistentSessionCallback& callback) { | |
147 DVLOG(2) << __func__; | |
148 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
149 DCHECK(!origin_.empty()); | |
150 | |
151 DictionaryPrefUpdate update(pref_service_, kMediaDrmStorage); | |
152 base::DictionaryValue* storage_dict = update.Get(); | |
153 DCHECK(storage_dict); | |
154 | |
155 base::DictionaryValue* origin_dict = nullptr; | |
156 // The origin string may contain dots. Do not use path expansion. | |
157 storage_dict->GetDictionaryWithoutPathExpansion(origin_, &origin_dict); | |
158 if (!origin_dict) { | |
159 DVLOG(1) << __func__ | |
160 << ": Failed to save persistent session data; entry for origin " | |
161 << origin_ << " does not exist."; | |
162 callback.Run(false); | |
yucliu1
2017/03/23 01:07:16
A bigger questions here, what should we do if erro
xhwang
2017/03/23 05:22:06
In most cases, we should never fail. The only legi
| |
163 return; | |
164 } | |
165 | |
166 if (origin_dict->GetDictionaryWithoutPathExpansion(session_id, nullptr)) { | |
167 DVLOG(1) << __func__ << ": Session ID already exists"; | |
168 callback.Run(false); | |
169 return; | |
170 } | |
171 | |
172 origin_dict->SetWithoutPathExpansion( | |
173 session_id, CreateSessionDictionary(key_set_id, mime_type)); | |
174 | |
175 callback.Run(true); | |
176 } | |
177 | |
178 void MediaDrmStorageImpl::LoadPersistentSession( | |
179 const std::string& session_id, | |
180 const LoadPersistentSessionCallback& callback) { | |
181 DVLOG(2) << __func__; | |
182 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
183 DCHECK(!origin_.empty()); | |
184 | |
185 NOTIMPLEMENTED(); | |
186 callback.Run(false, base::nullopt, base::nullopt); | |
187 } | |
188 | |
189 void MediaDrmStorageImpl::RemovePersistentSession( | |
190 const std::string& session_id, | |
191 const RemovePersistentSessionCallback& callback) { | |
192 DVLOG(2) << __func__; | |
193 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
194 DCHECK(!origin_.empty()); | |
195 | |
196 NOTIMPLEMENTED(); | |
197 callback.Run(false); | |
198 } | |
199 | |
200 } // namespace chrome | |
OLD | NEW |