OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "chrome/browser/chromeos/file_system_provider/registry.h" | 5 #include "chrome/browser/chromeos/file_system_provider/registry.h" |
6 | 6 |
7 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
8 #include "base/prefs/pref_service.h" | 8 #include "base/prefs/pref_service.h" |
9 #include "base/prefs/scoped_user_pref_update.h" | 9 #include "base/prefs/scoped_user_pref_update.h" |
10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
11 #include "chrome/browser/chromeos/file_system_provider/mount_path_util.h" | 11 #include "chrome/browser/chromeos/file_system_provider/mount_path_util.h" |
12 #include "chrome/browser/chromeos/file_system_provider/observer.h" | 12 #include "chrome/browser/chromeos/file_system_provider/observer.h" |
13 #include "chrome/browser/chromeos/file_system_provider/provided_file_system.h" | 13 #include "chrome/browser/chromeos/file_system_provider/provided_file_system.h" |
14 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_info
.h" | 14 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_info
.h" |
15 #include "chrome/browser/chromeos/file_system_provider/service_factory.h" | 15 #include "chrome/browser/chromeos/file_system_provider/service_factory.h" |
16 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
17 #include "chrome/common/pref_names.h" | 17 #include "chrome/common/pref_names.h" |
18 #include "components/pref_registry/pref_registry_syncable.h" | 18 #include "components/pref_registry/pref_registry_syncable.h" |
19 #include "extensions/browser/extension_registry.h" | 19 #include "extensions/browser/extension_registry.h" |
20 #include "extensions/browser/extension_system.h" | 20 #include "extensions/browser/extension_system.h" |
21 #include "storage/browser/fileapi/external_mount_points.h" | 21 #include "storage/browser/fileapi/external_mount_points.h" |
22 | 22 |
23 namespace chromeos { | 23 namespace chromeos { |
24 namespace file_system_provider { | 24 namespace file_system_provider { |
25 | 25 |
26 const char kPrefKeyFileSystemId[] = "file-system-id"; | 26 const char kPrefKeyFileSystemId[] = "file-system-id"; |
27 const char kPrefKeyDisplayName[] = "display-name"; | 27 const char kPrefKeyDisplayName[] = "display-name"; |
28 const char kPrefKeyWritable[] = "writable"; | 28 const char kPrefKeyWritable[] = "writable"; |
29 const char kPrefKeySupportsNotifyTag[] = "supports-notify-tag"; | 29 const char kPrefKeySupportsNotifyTag[] = "supports-notify-tag"; |
30 const char kPrefKeyObservedEntries[] = "observed-entries"; | 30 const char kPrefKeyWatchers[] = "watchers"; |
31 const char kPrefKeyObservedEntryEntryPath[] = "entry-path"; | 31 const char kPrefKeyWatcherEntryPath[] = "entry-path"; |
32 const char kPrefKeyObservedEntryRecursive[] = "recursive"; | 32 const char kPrefKeyWatcherRecursive[] = "recursive"; |
33 const char kPrefKeyObservedEntryLastTag[] = "last-tag"; | 33 const char kPrefKeyWatcherLastTag[] = "last-tag"; |
34 const char kPrefKeyObservedEntryPersistentOrigins[] = "persistent-origins"; | 34 const char kPrefKeyWatcherPersistentOrigins[] = "persistent-origins"; |
35 | 35 |
36 void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) { | 36 void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) { |
37 registry->RegisterDictionaryPref( | 37 registry->RegisterDictionaryPref( |
38 prefs::kFileSystemProviderMounted, | 38 prefs::kFileSystemProviderMounted, |
39 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | 39 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
40 } | 40 } |
41 | 41 |
42 Registry::Registry(Profile* profile) : profile_(profile) { | 42 Registry::Registry(Profile* profile) : profile_(profile) { |
43 } | 43 } |
44 | 44 |
45 Registry::~Registry() { | 45 Registry::~Registry() { |
46 } | 46 } |
47 | 47 |
48 void Registry::RememberFileSystem( | 48 void Registry::RememberFileSystem( |
49 const ProvidedFileSystemInfo& file_system_info, | 49 const ProvidedFileSystemInfo& file_system_info, |
50 const ObservedEntries& observed_entries) { | 50 const Watchers& watchers) { |
51 base::DictionaryValue* const file_system = new base::DictionaryValue(); | 51 base::DictionaryValue* const file_system = new base::DictionaryValue(); |
52 file_system->SetStringWithoutPathExpansion(kPrefKeyFileSystemId, | 52 file_system->SetStringWithoutPathExpansion(kPrefKeyFileSystemId, |
53 file_system_info.file_system_id()); | 53 file_system_info.file_system_id()); |
54 file_system->SetStringWithoutPathExpansion(kPrefKeyDisplayName, | 54 file_system->SetStringWithoutPathExpansion(kPrefKeyDisplayName, |
55 file_system_info.display_name()); | 55 file_system_info.display_name()); |
56 file_system->SetBooleanWithoutPathExpansion(kPrefKeyWritable, | 56 file_system->SetBooleanWithoutPathExpansion(kPrefKeyWritable, |
57 file_system_info.writable()); | 57 file_system_info.writable()); |
58 file_system->SetBooleanWithoutPathExpansion( | 58 file_system->SetBooleanWithoutPathExpansion( |
59 kPrefKeySupportsNotifyTag, file_system_info.supports_notify_tag()); | 59 kPrefKeySupportsNotifyTag, file_system_info.supports_notify_tag()); |
60 | 60 |
61 base::DictionaryValue* const observed_entries_value = | 61 base::DictionaryValue* const watchers_value = new base::DictionaryValue(); |
62 new base::DictionaryValue(); | 62 file_system->SetWithoutPathExpansion(kPrefKeyWatchers, watchers_value); |
63 file_system->SetWithoutPathExpansion(kPrefKeyObservedEntries, | |
64 observed_entries_value); | |
65 | 63 |
66 for (const auto& it : observed_entries) { | 64 for (const auto& it : watchers) { |
67 base::DictionaryValue* const observed_entry = new base::DictionaryValue(); | 65 base::DictionaryValue* const watcher = new base::DictionaryValue(); |
68 observed_entries_value->SetWithoutPathExpansion( | 66 watchers_value->SetWithoutPathExpansion(it.second.entry_path.value(), |
69 it.second.entry_path.value(), observed_entry); | 67 watcher); |
70 observed_entry->SetStringWithoutPathExpansion( | 68 watcher->SetStringWithoutPathExpansion(kPrefKeyWatcherEntryPath, |
71 kPrefKeyObservedEntryEntryPath, it.second.entry_path.value()); | 69 it.second.entry_path.value()); |
72 observed_entry->SetBooleanWithoutPathExpansion( | 70 watcher->SetBooleanWithoutPathExpansion(kPrefKeyWatcherRecursive, |
73 kPrefKeyObservedEntryRecursive, it.second.recursive); | 71 it.second.recursive); |
74 observed_entry->SetStringWithoutPathExpansion(kPrefKeyObservedEntryLastTag, | 72 watcher->SetStringWithoutPathExpansion(kPrefKeyWatcherLastTag, |
75 it.second.last_tag); | 73 it.second.last_tag); |
76 base::ListValue* const persistent_origins_value = new base::ListValue(); | 74 base::ListValue* const persistent_origins_value = new base::ListValue(); |
77 observed_entry->SetWithoutPathExpansion( | 75 watcher->SetWithoutPathExpansion(kPrefKeyWatcherPersistentOrigins, |
78 kPrefKeyObservedEntryPersistentOrigins, persistent_origins_value); | 76 persistent_origins_value); |
79 for (const auto& subscriber_it : it.second.subscribers) { | 77 for (const auto& subscriber_it : it.second.subscribers) { |
80 // Only persistent subscribers should be stored in persistent storage. | 78 // Only persistent subscribers should be stored in persistent storage. |
81 // Other ones should not be restired after a restart. | 79 // Other ones should not be restired after a restart. |
82 if (subscriber_it.second.persistent) | 80 if (subscriber_it.second.persistent) |
83 persistent_origins_value->AppendString(subscriber_it.first.spec()); | 81 persistent_origins_value->AppendString(subscriber_it.first.spec()); |
84 } | 82 } |
85 } | 83 } |
86 | 84 |
87 PrefService* const pref_service = profile_->GetPrefs(); | 85 PrefService* const pref_service = profile_->GetPrefs(); |
88 DCHECK(pref_service); | 86 DCHECK(pref_service); |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
170 MountOptions options; | 168 MountOptions options; |
171 options.file_system_id = file_system_id; | 169 options.file_system_id = file_system_id; |
172 options.display_name = display_name; | 170 options.display_name = display_name; |
173 options.writable = writable; | 171 options.writable = writable; |
174 options.supports_notify_tag = supports_notify_tag; | 172 options.supports_notify_tag = supports_notify_tag; |
175 | 173 |
176 RestoredFileSystem restored_file_system; | 174 RestoredFileSystem restored_file_system; |
177 restored_file_system.extension_id = extension_id; | 175 restored_file_system.extension_id = extension_id; |
178 restored_file_system.options = options; | 176 restored_file_system.options = options; |
179 | 177 |
180 // Restore observed entries. It's optional, since this field is new. | 178 // Restore watchers. It's optional, since this field is new. |
181 const base::DictionaryValue* observed_entries = NULL; | 179 const base::DictionaryValue* watchers = NULL; |
182 if (file_system->GetDictionaryWithoutPathExpansion(kPrefKeyObservedEntries, | 180 if (file_system->GetDictionaryWithoutPathExpansion(kPrefKeyWatchers, |
183 &observed_entries)) { | 181 &watchers)) { |
184 for (base::DictionaryValue::Iterator it(*observed_entries); !it.IsAtEnd(); | 182 for (base::DictionaryValue::Iterator it(*watchers); !it.IsAtEnd(); |
185 it.Advance()) { | 183 it.Advance()) { |
186 const base::Value* observed_entry_value = NULL; | 184 const base::Value* watcher_value = NULL; |
187 const base::DictionaryValue* observed_entry = NULL; | 185 const base::DictionaryValue* watcher = NULL; |
188 observed_entries->GetWithoutPathExpansion(it.key(), | 186 watchers->GetWithoutPathExpansion(it.key(), &watcher_value); |
189 &observed_entry_value); | |
190 DCHECK(file_system_value); | 187 DCHECK(file_system_value); |
191 | 188 |
192 std::string entry_path; | 189 std::string entry_path; |
193 bool recursive = false; | 190 bool recursive = false; |
194 std::string last_tag; | 191 std::string last_tag; |
195 const base::ListValue* persistent_origins = NULL; | 192 const base::ListValue* persistent_origins = NULL; |
196 | 193 |
197 if (!observed_entry_value->GetAsDictionary(&observed_entry) || | 194 if (!watcher_value->GetAsDictionary(&watcher) || |
198 !observed_entry->GetStringWithoutPathExpansion( | 195 !watcher->GetStringWithoutPathExpansion(kPrefKeyWatcherEntryPath, |
199 kPrefKeyObservedEntryEntryPath, &entry_path) || | 196 &entry_path) || |
200 !observed_entry->GetBooleanWithoutPathExpansion( | 197 !watcher->GetBooleanWithoutPathExpansion(kPrefKeyWatcherRecursive, |
201 kPrefKeyObservedEntryRecursive, &recursive) || | 198 &recursive) || |
202 !observed_entry->GetStringWithoutPathExpansion( | 199 !watcher->GetStringWithoutPathExpansion(kPrefKeyWatcherLastTag, |
203 kPrefKeyObservedEntryLastTag, &last_tag) || | 200 &last_tag) || |
204 !observed_entry->GetListWithoutPathExpansion( | 201 !watcher->GetListWithoutPathExpansion( |
205 kPrefKeyObservedEntryPersistentOrigins, &persistent_origins) || | 202 kPrefKeyWatcherPersistentOrigins, &persistent_origins) || |
206 it.key() != entry_path || entry_path.empty() || | 203 it.key() != entry_path || entry_path.empty() || |
207 (!options.supports_notify_tag && | 204 (!options.supports_notify_tag && |
208 (!last_tag.empty() || persistent_origins->GetSize()))) { | 205 (!last_tag.empty() || persistent_origins->GetSize()))) { |
209 LOG(ERROR) << "Malformed observed entry information in preferences."; | 206 LOG(ERROR) << "Malformed watcher information in preferences."; |
210 continue; | 207 continue; |
211 } | 208 } |
212 | 209 |
213 ObservedEntry restored_observed_entry; | 210 Watcher restored_watcher; |
214 restored_observed_entry.entry_path = | 211 restored_watcher.entry_path = |
215 base::FilePath::FromUTF8Unsafe(entry_path); | 212 base::FilePath::FromUTF8Unsafe(entry_path); |
216 restored_observed_entry.recursive = recursive; | 213 restored_watcher.recursive = recursive; |
217 restored_observed_entry.last_tag = last_tag; | 214 restored_watcher.last_tag = last_tag; |
218 for (const auto& persistent_origin : *persistent_origins) { | 215 for (const auto& persistent_origin : *persistent_origins) { |
219 std::string origin; | 216 std::string origin; |
220 if (persistent_origin->GetAsString(&origin)) { | 217 if (persistent_origin->GetAsString(&origin)) { |
221 LOG(ERROR) << "Malformed subscriber information in preferences."; | 218 LOG(ERROR) << "Malformed subscriber information in preferences."; |
222 continue; | 219 continue; |
223 } | 220 } |
224 const GURL origin_as_gurl(origin); | 221 const GURL origin_as_gurl(origin); |
225 restored_observed_entry.subscribers[origin_as_gurl].origin = | 222 restored_watcher.subscribers[origin_as_gurl].origin = origin_as_gurl; |
226 origin_as_gurl; | 223 restored_watcher.subscribers[origin_as_gurl].persistent = true; |
227 restored_observed_entry.subscribers[origin_as_gurl].persistent = true; | |
228 } | 224 } |
229 restored_file_system.observed_entries[ObservedEntryKey( | 225 restored_file_system.watchers[WatcherKey( |
230 base::FilePath::FromUTF8Unsafe(entry_path), recursive)] = | 226 base::FilePath::FromUTF8Unsafe(entry_path), recursive)] = |
231 restored_observed_entry; | 227 restored_watcher; |
232 } | 228 } |
233 } | 229 } |
234 restored_file_systems->push_back(restored_file_system); | 230 restored_file_systems->push_back(restored_file_system); |
235 } | 231 } |
236 | 232 |
237 return restored_file_systems.Pass(); | 233 return restored_file_systems.Pass(); |
238 } | 234 } |
239 | 235 |
240 void Registry::UpdateObservedEntryTag( | 236 void Registry::UpdateWatcherTag(const ProvidedFileSystemInfo& file_system_info, |
241 const ProvidedFileSystemInfo& file_system_info, | 237 const Watcher& watcher) { |
242 const ObservedEntry& observed_entry) { | |
243 PrefService* const pref_service = profile_->GetPrefs(); | 238 PrefService* const pref_service = profile_->GetPrefs(); |
244 DCHECK(pref_service); | 239 DCHECK(pref_service); |
245 | 240 |
246 // TODO(mtomasz): Consider optimizing it by moving information about observed | 241 // TODO(mtomasz): Consider optimizing it by moving information about watchers |
247 // entries, or even file systems to leveldb. | 242 // or even file systems to leveldb. |
248 DictionaryPrefUpdate dict_update(pref_service, | 243 DictionaryPrefUpdate dict_update(pref_service, |
249 prefs::kFileSystemProviderMounted); | 244 prefs::kFileSystemProviderMounted); |
250 | 245 |
251 // All of the following checks should not happen in healthy environment. | 246 // All of the following checks should not happen in healthy environment. |
252 // However, since they rely on storage, DCHECKs can't be used. | 247 // However, since they rely on storage, DCHECKs can't be used. |
253 base::DictionaryValue* file_systems_per_extension = NULL; | 248 base::DictionaryValue* file_systems_per_extension = NULL; |
254 base::DictionaryValue* file_system = NULL; | 249 base::DictionaryValue* file_system = NULL; |
255 base::DictionaryValue* observed_entries = NULL; | 250 base::DictionaryValue* watchers = NULL; |
256 base::DictionaryValue* observed_entry_value = NULL; | 251 base::DictionaryValue* watcher_value = NULL; |
257 if (!dict_update->GetDictionaryWithoutPathExpansion( | 252 if (!dict_update->GetDictionaryWithoutPathExpansion( |
258 file_system_info.extension_id(), &file_systems_per_extension) || | 253 file_system_info.extension_id(), &file_systems_per_extension) || |
259 !file_systems_per_extension->GetDictionaryWithoutPathExpansion( | 254 !file_systems_per_extension->GetDictionaryWithoutPathExpansion( |
260 file_system_info.file_system_id(), &file_system) || | 255 file_system_info.file_system_id(), &file_system) || |
261 !file_system->GetDictionaryWithoutPathExpansion(kPrefKeyObservedEntries, | 256 !file_system->GetDictionaryWithoutPathExpansion(kPrefKeyWatchers, |
262 &observed_entries) || | 257 &watchers) || |
263 !observed_entries->GetDictionaryWithoutPathExpansion( | 258 !watchers->GetDictionaryWithoutPathExpansion(watcher.entry_path.value(), |
264 observed_entry.entry_path.value(), &observed_entry_value)) { | 259 &watcher_value)) { |
265 // Broken preferences. | 260 // Broken preferences. |
266 LOG(ERROR) << "Broken preferences detected while updating a tag."; | 261 LOG(ERROR) << "Broken preferences detected while updating a tag."; |
267 return; | 262 return; |
268 } | 263 } |
269 | 264 |
270 observed_entry_value->SetStringWithoutPathExpansion( | 265 watcher_value->SetStringWithoutPathExpansion(kPrefKeyWatcherLastTag, |
271 kPrefKeyObservedEntryLastTag, observed_entry.last_tag); | 266 watcher.last_tag); |
272 } | 267 } |
273 | 268 |
274 } // namespace file_system_provider | 269 } // namespace file_system_provider |
275 } // namespace chromeos | 270 } // namespace chromeos |
OLD | NEW |