OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "components/filesystem/public/cpp/prefs/filesystem_json_pref_store.h" | 5 #include "components/filesystem/public/cpp/prefs/filesystem_json_pref_store.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <utility> | 10 #include <utility> |
(...skipping 15 matching lines...) Expand all Loading... |
26 #include "mojo/common/common_type_converters.h" | 26 #include "mojo/common/common_type_converters.h" |
27 | 27 |
28 namespace filesystem { | 28 namespace filesystem { |
29 | 29 |
30 // Result returned from internal read tasks. | 30 // Result returned from internal read tasks. |
31 struct FilesystemJsonPrefStore::ReadResult { | 31 struct FilesystemJsonPrefStore::ReadResult { |
32 public: | 32 public: |
33 ReadResult(); | 33 ReadResult(); |
34 ~ReadResult(); | 34 ~ReadResult(); |
35 | 35 |
36 scoped_ptr<base::Value> value; | 36 std::unique_ptr<base::Value> value; |
37 PrefReadError error; | 37 PrefReadError error; |
38 | 38 |
39 private: | 39 private: |
40 DISALLOW_COPY_AND_ASSIGN(ReadResult); | 40 DISALLOW_COPY_AND_ASSIGN(ReadResult); |
41 }; | 41 }; |
42 | 42 |
43 FilesystemJsonPrefStore::ReadResult::ReadResult() | 43 FilesystemJsonPrefStore::ReadResult::ReadResult() |
44 : error(PersistentPrefStore::PREF_READ_ERROR_NONE) {} | 44 : error(PersistentPrefStore::PREF_READ_ERROR_NONE) {} |
45 | 45 |
46 FilesystemJsonPrefStore::ReadResult::~ReadResult() {} | 46 FilesystemJsonPrefStore::ReadResult::~ReadResult() {} |
47 | 47 |
48 namespace { | 48 namespace { |
49 | 49 |
50 PersistentPrefStore::PrefReadError HandleReadErrors(const base::Value* value) { | 50 PersistentPrefStore::PrefReadError HandleReadErrors(const base::Value* value) { |
51 if (!value->IsType(base::Value::TYPE_DICTIONARY)) | 51 if (!value->IsType(base::Value::TYPE_DICTIONARY)) |
52 return PersistentPrefStore::PREF_READ_ERROR_JSON_TYPE; | 52 return PersistentPrefStore::PREF_READ_ERROR_JSON_TYPE; |
53 return PersistentPrefStore::PREF_READ_ERROR_NONE; | 53 return PersistentPrefStore::PREF_READ_ERROR_NONE; |
54 } | 54 } |
55 | 55 |
56 } // namespace | 56 } // namespace |
57 | 57 |
58 FilesystemJsonPrefStore::FilesystemJsonPrefStore( | 58 FilesystemJsonPrefStore::FilesystemJsonPrefStore( |
59 const std::string& pref_filename, | 59 const std::string& pref_filename, |
60 filesystem::FileSystemPtr filesystem, | 60 filesystem::FileSystemPtr filesystem, |
61 scoped_ptr<PrefFilter> pref_filter) | 61 std::unique_ptr<PrefFilter> pref_filter) |
62 : path_(pref_filename), | 62 : path_(pref_filename), |
63 binding_(this), | 63 binding_(this), |
64 filesystem_(std::move(filesystem)), | 64 filesystem_(std::move(filesystem)), |
65 prefs_(new base::DictionaryValue()), | 65 prefs_(new base::DictionaryValue()), |
66 read_only_(false), | 66 read_only_(false), |
67 pref_filter_(std::move(pref_filter)), | 67 pref_filter_(std::move(pref_filter)), |
68 initialized_(false), | 68 initialized_(false), |
69 filtering_in_progress_(false), | 69 filtering_in_progress_(false), |
70 pending_lossy_write_(false), | 70 pending_lossy_write_(false), |
71 read_error_(PREF_READ_ERROR_NONE) { | 71 read_error_(PREF_READ_ERROR_NONE) { |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 } | 110 } |
111 | 111 |
112 bool FilesystemJsonPrefStore::GetMutableValue(const std::string& key, | 112 bool FilesystemJsonPrefStore::GetMutableValue(const std::string& key, |
113 base::Value** result) { | 113 base::Value** result) { |
114 DCHECK(CalledOnValidThread()); | 114 DCHECK(CalledOnValidThread()); |
115 | 115 |
116 return prefs_->Get(key, result); | 116 return prefs_->Get(key, result); |
117 } | 117 } |
118 | 118 |
119 void FilesystemJsonPrefStore::SetValue(const std::string& key, | 119 void FilesystemJsonPrefStore::SetValue(const std::string& key, |
120 scoped_ptr<base::Value> value, | 120 std::unique_ptr<base::Value> value, |
121 uint32_t flags) { | 121 uint32_t flags) { |
122 DCHECK(CalledOnValidThread()); | 122 DCHECK(CalledOnValidThread()); |
123 | 123 |
124 DCHECK(value); | 124 DCHECK(value); |
125 base::Value* old_value = nullptr; | 125 base::Value* old_value = nullptr; |
126 prefs_->Get(key, &old_value); | 126 prefs_->Get(key, &old_value); |
127 if (!old_value || !value->Equals(old_value)) { | 127 if (!old_value || !value->Equals(old_value)) { |
128 prefs_->Set(key, std::move(value)); | 128 prefs_->Set(key, std::move(value)); |
129 ReportValueChanged(key, flags); | 129 ReportValueChanged(key, flags); |
130 } | 130 } |
131 } | 131 } |
132 | 132 |
133 void FilesystemJsonPrefStore::SetValueSilently(const std::string& key, | 133 void FilesystemJsonPrefStore::SetValueSilently( |
134 scoped_ptr<base::Value> value, | 134 const std::string& key, |
135 uint32_t flags) { | 135 std::unique_ptr<base::Value> value, |
| 136 uint32_t flags) { |
136 DCHECK(CalledOnValidThread()); | 137 DCHECK(CalledOnValidThread()); |
137 | 138 |
138 DCHECK(value); | 139 DCHECK(value); |
139 base::Value* old_value = nullptr; | 140 base::Value* old_value = nullptr; |
140 prefs_->Get(key, &old_value); | 141 prefs_->Get(key, &old_value); |
141 if (!old_value || !value->Equals(old_value)) { | 142 if (!old_value || !value->Equals(old_value)) { |
142 prefs_->Set(key, std::move(value)); | 143 prefs_->Set(key, std::move(value)); |
143 ScheduleWrite(flags); | 144 ScheduleWrite(flags); |
144 } | 145 } |
145 } | 146 } |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
228 if (pref_filter_) | 229 if (pref_filter_) |
229 pref_filter_->FilterUpdate(key); | 230 pref_filter_->FilterUpdate(key); |
230 | 231 |
231 FOR_EACH_OBSERVER(PrefStore::Observer, observers_, OnPrefValueChanged(key)); | 232 FOR_EACH_OBSERVER(PrefStore::Observer, observers_, OnPrefValueChanged(key)); |
232 | 233 |
233 ScheduleWrite(flags); | 234 ScheduleWrite(flags); |
234 } | 235 } |
235 | 236 |
236 void FilesystemJsonPrefStore::OnFileSystemShutdown() {} | 237 void FilesystemJsonPrefStore::OnFileSystemShutdown() {} |
237 | 238 |
238 void FilesystemJsonPrefStore::OnFileRead(scoped_ptr<ReadResult> read_result) { | 239 void FilesystemJsonPrefStore::OnFileRead( |
| 240 std::unique_ptr<ReadResult> read_result) { |
239 DCHECK(CalledOnValidThread()); | 241 DCHECK(CalledOnValidThread()); |
240 | 242 |
241 DCHECK(read_result); | 243 DCHECK(read_result); |
242 | 244 |
243 scoped_ptr<base::DictionaryValue> unfiltered_prefs(new base::DictionaryValue); | 245 std::unique_ptr<base::DictionaryValue> unfiltered_prefs( |
| 246 new base::DictionaryValue); |
244 | 247 |
245 read_error_ = read_result->error; | 248 read_error_ = read_result->error; |
246 | 249 |
247 switch (read_error_) { | 250 switch (read_error_) { |
248 case PREF_READ_ERROR_ACCESS_DENIED: | 251 case PREF_READ_ERROR_ACCESS_DENIED: |
249 case PREF_READ_ERROR_FILE_OTHER: | 252 case PREF_READ_ERROR_FILE_OTHER: |
250 case PREF_READ_ERROR_FILE_LOCKED: | 253 case PREF_READ_ERROR_FILE_LOCKED: |
251 case PREF_READ_ERROR_JSON_TYPE: | 254 case PREF_READ_ERROR_JSON_TYPE: |
252 case PREF_READ_ERROR_FILE_NOT_SPECIFIED: | 255 case PREF_READ_ERROR_FILE_NOT_SPECIFIED: |
253 read_only_ = true; | 256 read_only_ = true; |
(...skipping 28 matching lines...) Expand all Loading... |
282 FinalizeFileRead(std::move(unfiltered_prefs), false); | 285 FinalizeFileRead(std::move(unfiltered_prefs), false); |
283 } | 286 } |
284 } | 287 } |
285 | 288 |
286 FilesystemJsonPrefStore::~FilesystemJsonPrefStore() { | 289 FilesystemJsonPrefStore::~FilesystemJsonPrefStore() { |
287 // TODO(erg): Now that writing is asynchronous, we can't really async write | 290 // TODO(erg): Now that writing is asynchronous, we can't really async write |
288 // prefs at shutdown. See comment in CommitPendingWrite(). | 291 // prefs at shutdown. See comment in CommitPendingWrite(). |
289 } | 292 } |
290 | 293 |
291 void FilesystemJsonPrefStore::FinalizeFileRead( | 294 void FilesystemJsonPrefStore::FinalizeFileRead( |
292 scoped_ptr<base::DictionaryValue> prefs, | 295 std::unique_ptr<base::DictionaryValue> prefs, |
293 bool schedule_write) { | 296 bool schedule_write) { |
294 DCHECK(CalledOnValidThread()); | 297 DCHECK(CalledOnValidThread()); |
295 | 298 |
296 filtering_in_progress_ = false; | 299 filtering_in_progress_ = false; |
297 | 300 |
298 prefs_ = std::move(prefs); | 301 prefs_ = std::move(prefs); |
299 | 302 |
300 initialized_ = true; | 303 initialized_ = true; |
301 | 304 |
302 if (schedule_write) | 305 if (schedule_write) |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
384 | 387 |
385 void FilesystemJsonPrefStore::OnPreferencesReadStart() { | 388 void FilesystemJsonPrefStore::OnPreferencesReadStart() { |
386 directory_->ReadEntireFile( | 389 directory_->ReadEntireFile( |
387 path_, | 390 path_, |
388 Bind(&FilesystemJsonPrefStore::OnPreferencesFileRead, AsWeakPtr())); | 391 Bind(&FilesystemJsonPrefStore::OnPreferencesFileRead, AsWeakPtr())); |
389 } | 392 } |
390 | 393 |
391 void FilesystemJsonPrefStore::OnPreferencesFileRead( | 394 void FilesystemJsonPrefStore::OnPreferencesFileRead( |
392 FileError err, | 395 FileError err, |
393 mojo::Array<uint8_t> contents) { | 396 mojo::Array<uint8_t> contents) { |
394 scoped_ptr<FilesystemJsonPrefStore::ReadResult> read_result( | 397 std::unique_ptr<FilesystemJsonPrefStore::ReadResult> read_result( |
395 new FilesystemJsonPrefStore::ReadResult); | 398 new FilesystemJsonPrefStore::ReadResult); |
396 // TODO(erg): Needs even better error handling. | 399 // TODO(erg): Needs even better error handling. |
397 switch (err) { | 400 switch (err) { |
398 case FileError::IN_USE: | 401 case FileError::IN_USE: |
399 case FileError::ACCESS_DENIED: | 402 case FileError::ACCESS_DENIED: |
400 case FileError::NOT_A_FILE: { | 403 case FileError::NOT_A_FILE: { |
401 read_only_ = true; | 404 read_only_ = true; |
402 break; | 405 break; |
403 } | 406 } |
404 case FileError::FAILED: | 407 case FileError::FAILED: |
(...skipping 10 matching lines...) Expand all Loading... |
415 reinterpret_cast<char*>(&contents.front()), contents.size())); | 418 reinterpret_cast<char*>(&contents.front()), contents.size())); |
416 read_result->value = deserializer.Deserialize(&error_code, &error_msg); | 419 read_result->value = deserializer.Deserialize(&error_code, &error_msg); |
417 read_result->error = HandleReadErrors(read_result->value.get()); | 420 read_result->error = HandleReadErrors(read_result->value.get()); |
418 } | 421 } |
419 } | 422 } |
420 | 423 |
421 OnFileRead(std::move(read_result)); | 424 OnFileRead(std::move(read_result)); |
422 } | 425 } |
423 | 426 |
424 } // namespace filesystem | 427 } // namespace filesystem |
OLD | NEW |