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

Side by Side Diff: chrome/browser/chromeos/file_system_provider/registry.cc

Issue 661393002: [fsp] Separate logic for saving/restoring state to a separate class. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: C++11 features. Created 6 years, 2 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 2014 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/chromeos/file_system_provider/registry.h"
6
7 #include "base/files/file_path.h"
8 #include "base/prefs/pref_service.h"
9 #include "base/prefs/scoped_user_pref_update.h"
10 #include "base/stl_util.h"
11 #include "chrome/browser/chromeos/file_system_provider/mount_path_util.h"
12 #include "chrome/browser/chromeos/file_system_provider/observer.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"
15 #include "chrome/browser/chromeos/file_system_provider/service_factory.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/common/pref_names.h"
18 #include "components/pref_registry/pref_registry_syncable.h"
19 #include "extensions/browser/extension_registry.h"
20 #include "extensions/browser/extension_system.h"
21 #include "storage/browser/fileapi/external_mount_points.h"
22
23 namespace chromeos {
24 namespace file_system_provider {
25
26 const char kPrefKeyFileSystemId[] = "file-system-id";
27 const char kPrefKeyDisplayName[] = "display-name";
28 const char kPrefKeyWritable[] = "writable";
29 const char kPrefKeySupportsNotifyTag[] = "supports-notify-tag";
30 const char kPrefKeyObservedEntries[] = "observed-entries";
31 const char kPrefKeyObservedEntryEntryPath[] = "entry-path";
32 const char kPrefKeyObservedEntryRecursive[] = "recursive";
33 const char kPrefKeyObservedEntryLastTag[] = "last-tag";
34
35 void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) {
36 registry->RegisterDictionaryPref(
37 prefs::kFileSystemProviderMounted,
38 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
39 }
40
41 Registry::Registry(Profile* profile) : profile_(profile) {
42 }
43
44 Registry::~Registry() {
45 }
46
47 void Registry::RememberFileSystem(
48 const ProvidedFileSystemInfo& file_system_info,
49 const ObservedEntries& observed_entries) {
50 base::DictionaryValue* const file_system = new base::DictionaryValue();
51 file_system->SetStringWithoutPathExpansion(kPrefKeyFileSystemId,
52 file_system_info.file_system_id());
53 file_system->SetStringWithoutPathExpansion(kPrefKeyDisplayName,
54 file_system_info.display_name());
55 file_system->SetBooleanWithoutPathExpansion(kPrefKeyWritable,
56 file_system_info.writable());
57 file_system->SetBooleanWithoutPathExpansion(
58 kPrefKeySupportsNotifyTag, file_system_info.supports_notify_tag());
59
60 base::DictionaryValue* const observed_entries_value =
61 new base::DictionaryValue();
62 file_system->SetWithoutPathExpansion(kPrefKeyObservedEntries,
63 observed_entries_value);
64
65 for (const auto& it : observed_entries) {
66 base::DictionaryValue* const observed_entry = new base::DictionaryValue();
67 observed_entries_value->SetWithoutPathExpansion(it.first.value(),
68 observed_entry);
69 observed_entry->SetStringWithoutPathExpansion(
70 kPrefKeyObservedEntryEntryPath, it.second.entry_path.value());
71 observed_entry->SetBooleanWithoutPathExpansion(
72 kPrefKeyObservedEntryRecursive, it.second.recursive);
73 observed_entry->SetStringWithoutPathExpansion(kPrefKeyObservedEntryLastTag,
74 it.second.last_tag);
75 }
76
77 PrefService* const pref_service = profile_->GetPrefs();
78 DCHECK(pref_service);
79
80 DictionaryPrefUpdate dict_update(pref_service,
81 prefs::kFileSystemProviderMounted);
82
83 base::DictionaryValue* file_systems_per_extension = NULL;
84 if (!dict_update->GetDictionaryWithoutPathExpansion(
85 file_system_info.extension_id(), &file_systems_per_extension)) {
86 file_systems_per_extension = new base::DictionaryValue();
87 dict_update->SetWithoutPathExpansion(file_system_info.extension_id(),
88 file_systems_per_extension);
89 }
90
91 file_systems_per_extension->SetWithoutPathExpansion(
92 file_system_info.file_system_id(), file_system);
93 }
94
95 void Registry::ForgetFileSystem(const std::string& extension_id,
96 const std::string& file_system_id) {
97 PrefService* const pref_service = profile_->GetPrefs();
98 DCHECK(pref_service);
99
100 DictionaryPrefUpdate dict_update(pref_service,
101 prefs::kFileSystemProviderMounted);
102
103 base::DictionaryValue* file_systems_per_extension = NULL;
104 if (!dict_update->GetDictionaryWithoutPathExpansion(
105 extension_id, &file_systems_per_extension))
106 return; // Nothing to forget.
107
108 file_systems_per_extension->RemoveWithoutPathExpansion(file_system_id, NULL);
109 if (!file_systems_per_extension->size())
110 dict_update->Remove(extension_id, NULL);
111 }
112
113 scoped_ptr<Registry::RestoredFileSystems> Registry::RestoreFileSystems(
114 const std::string& extension_id) {
115 PrefService* const pref_service = profile_->GetPrefs();
116 DCHECK(pref_service);
117
118 const base::DictionaryValue* const file_systems =
119 pref_service->GetDictionary(prefs::kFileSystemProviderMounted);
120 DCHECK(file_systems);
121
122 const base::DictionaryValue* file_systems_per_extension = NULL;
123 if (!file_systems->GetDictionaryWithoutPathExpansion(
124 extension_id, &file_systems_per_extension)) {
125 return make_scoped_ptr(new RestoredFileSystems); // Nothing to restore.
126 }
127
128 scoped_ptr<RestoredFileSystems> restored_file_systems(
129 new RestoredFileSystems);
130
131 for (base::DictionaryValue::Iterator it(*file_systems_per_extension);
132 !it.IsAtEnd();
133 it.Advance()) {
134 const base::Value* file_system_value = NULL;
135 const base::DictionaryValue* file_system = NULL;
136 file_systems_per_extension->GetWithoutPathExpansion(it.key(),
137 &file_system_value);
138 DCHECK(file_system_value);
139
140 std::string file_system_id;
141 std::string display_name;
142 bool writable = false;
143 bool supports_notify_tag = false;
144
145 if (!file_system_value->GetAsDictionary(&file_system) ||
146 !file_system->GetStringWithoutPathExpansion(kPrefKeyFileSystemId,
147 &file_system_id) ||
148 !file_system->GetStringWithoutPathExpansion(kPrefKeyDisplayName,
149 &display_name) ||
150 !file_system->GetBooleanWithoutPathExpansion(kPrefKeyWritable,
151 &writable) ||
152 !file_system->GetBooleanWithoutPathExpansion(kPrefKeySupportsNotifyTag,
153 &supports_notify_tag) ||
154 file_system_id.empty() || display_name.empty()) {
155 LOG(ERROR)
156 << "Malformed provided file system information in preferences.";
157 continue;
158 }
159
160 MountOptions options;
161 options.file_system_id = file_system_id;
162 options.display_name = display_name;
163 options.writable = writable;
164 options.supports_notify_tag = supports_notify_tag;
165
166 RestoredFileSystem restored_file_system;
167 restored_file_system.extension_id = extension_id;
168 restored_file_system.options = options;
169
170 // Restore observed entries. It's optional, since this field is new.
171 const base::DictionaryValue* observed_entries = NULL;
172 if (file_system->GetDictionaryWithoutPathExpansion(kPrefKeyObservedEntries,
173 &observed_entries)) {
174 for (base::DictionaryValue::Iterator it(*observed_entries); !it.IsAtEnd();
175 it.Advance()) {
176 const base::Value* observed_entry_value = NULL;
177 const base::DictionaryValue* observed_entry = NULL;
178 observed_entries->GetWithoutPathExpansion(it.key(),
179 &observed_entry_value);
180 DCHECK(file_system_value);
181
182 std::string entry_path;
183 bool recursive = false;
184 std::string last_tag;
185
186 if (!observed_entry_value->GetAsDictionary(&observed_entry) ||
187 !observed_entry->GetStringWithoutPathExpansion(
188 kPrefKeyObservedEntryEntryPath, &entry_path) ||
189 !observed_entry->GetBooleanWithoutPathExpansion(
190 kPrefKeyObservedEntryRecursive, &recursive) ||
191 !observed_entry->GetStringWithoutPathExpansion(
192 kPrefKeyObservedEntryLastTag, &last_tag) ||
193 it.key() != entry_path || entry_path.empty() ||
194 (!options.supports_notify_tag && !last_tag.empty())) {
195 LOG(ERROR) << "Malformed observed entry information in preferences.";
196 continue;
197 }
198
199 ObservedEntry restored_observed_entry;
200 restored_observed_entry.entry_path =
201 base::FilePath::FromUTF8Unsafe(entry_path);
202 restored_observed_entry.recursive = recursive;
203 restored_observed_entry.last_tag = last_tag;
204 restored_file_system
205 .observed_entries[base::FilePath::FromUTF8Unsafe(entry_path)] =
206 restored_observed_entry;
207 }
208 }
209 restored_file_systems->push_back(restored_file_system);
210 }
211
212 return restored_file_systems.Pass();
213 }
214
215 void Registry::UpdateObservedEntryTag(
216 const ProvidedFileSystemInfo& file_system_info,
217 const ObservedEntry& observed_entry) {
218 PrefService* const pref_service = profile_->GetPrefs();
219 DCHECK(pref_service);
220
221 // TODO(mtomasz): Consider optimizing it by moving information about observed
222 // entries, or even file systems to leveldb.
223 DictionaryPrefUpdate dict_update(pref_service,
224 prefs::kFileSystemProviderMounted);
225
226 // All of the following checks should not happen in healthy environment.
227 // However, since they rely on storage, DCHECKs can't be used.
228 base::DictionaryValue* file_systems_per_extension = NULL;
229 base::DictionaryValue* file_system = NULL;
230 base::DictionaryValue* observed_entries = NULL;
231 base::DictionaryValue* observed_entry_value = NULL;
232 if (!dict_update->GetDictionaryWithoutPathExpansion(
233 file_system_info.extension_id(), &file_systems_per_extension) ||
234 !file_systems_per_extension->GetDictionaryWithoutPathExpansion(
235 file_system_info.file_system_id(), &file_system) ||
236 !file_system->GetDictionaryWithoutPathExpansion(kPrefKeyObservedEntries,
237 &observed_entries) ||
238 !observed_entries->GetDictionaryWithoutPathExpansion(
239 observed_entry.entry_path.value(), &observed_entry_value)) {
240 // Broken preferences.
241 LOG(ERROR) << "Broken preferences detected while updating a tag.";
242 return;
243 }
244
245 observed_entry_value->SetStringWithoutPathExpansion(
246 kPrefKeyObservedEntryLastTag, observed_entry.last_tag);
247 }
248
249 } // namespace file_system_provider
250 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698