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 "base/prefs/json_pref_store.h" | 5 #include "base/prefs/json_pref_store.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/callback.h" | 10 #include "base/callback.h" |
(...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
290 } | 290 } |
291 | 291 |
292 void JsonPrefStore::RegisterOnNextSuccessfulWriteCallback( | 292 void JsonPrefStore::RegisterOnNextSuccessfulWriteCallback( |
293 const base::Closure& on_next_successful_write) { | 293 const base::Closure& on_next_successful_write) { |
294 writer_.RegisterOnNextSuccessfulWriteCallback(on_next_successful_write); | 294 writer_.RegisterOnNextSuccessfulWriteCallback(on_next_successful_write); |
295 } | 295 } |
296 | 296 |
297 void JsonPrefStore::OnFileRead(scoped_ptr<base::Value> value, | 297 void JsonPrefStore::OnFileRead(scoped_ptr<base::Value> value, |
298 PersistentPrefStore::PrefReadError error, | 298 PersistentPrefStore::PrefReadError error, |
299 bool no_dir) { | 299 bool no_dir) { |
300 scoped_ptr<base::DictionaryValue> unfiltered_prefs(new base::DictionaryValue); | |
301 | |
302 read_error_ = error; | 300 read_error_ = error; |
303 | 301 |
304 bool initialization_successful = !no_dir; | 302 bool initialization_successful = !no_dir; |
305 | 303 |
306 if (initialization_successful) { | 304 if (initialization_successful) { |
307 switch (read_error_) { | 305 switch (read_error_) { |
308 case PREF_READ_ERROR_ACCESS_DENIED: | 306 case PREF_READ_ERROR_ACCESS_DENIED: |
309 case PREF_READ_ERROR_FILE_OTHER: | 307 case PREF_READ_ERROR_FILE_OTHER: |
310 case PREF_READ_ERROR_FILE_LOCKED: | 308 case PREF_READ_ERROR_FILE_LOCKED: |
311 case PREF_READ_ERROR_JSON_TYPE: | 309 case PREF_READ_ERROR_JSON_TYPE: |
312 case PREF_READ_ERROR_FILE_NOT_SPECIFIED: | 310 case PREF_READ_ERROR_FILE_NOT_SPECIFIED: |
313 read_only_ = true; | 311 read_only_ = true; |
314 break; | 312 break; |
315 case PREF_READ_ERROR_NONE: | 313 case PREF_READ_ERROR_NONE: |
316 DCHECK(value.get()); | 314 DCHECK(value.get()); |
317 unfiltered_prefs.reset( | 315 prefs_.reset(static_cast<base::DictionaryValue*>(value.release())); |
318 static_cast<base::DictionaryValue*>(value.release())); | |
319 break; | 316 break; |
320 case PREF_READ_ERROR_NO_FILE: | 317 case PREF_READ_ERROR_NO_FILE: |
321 // If the file just doesn't exist, maybe this is first run. In any case | 318 // If the file just doesn't exist, maybe this is first run. In any case |
322 // there's no harm in writing out default prefs in this case. | 319 // there's no harm in writing out default prefs in this case. |
323 break; | 320 break; |
324 case PREF_READ_ERROR_JSON_PARSE: | 321 case PREF_READ_ERROR_JSON_PARSE: |
325 case PREF_READ_ERROR_JSON_REPEAT: | 322 case PREF_READ_ERROR_JSON_REPEAT: |
326 break; | 323 break; |
327 case PREF_READ_ERROR_ASYNCHRONOUS_TASK_INCOMPLETE: | 324 case PREF_READ_ERROR_ASYNCHRONOUS_TASK_INCOMPLETE: |
328 // This is a special error code to be returned by ReadPrefs when it | 325 // This is a special error code to be returned by ReadPrefs when it |
329 // can't complete synchronously, it should never be returned by the read | 326 // can't complete synchronously, it should never be returned by the read |
330 // operation itself. | 327 // operation itself. |
331 NOTREACHED(); | 328 NOTREACHED(); |
332 break; | 329 break; |
333 case PREF_READ_ERROR_LEVELDB_IO: | 330 case PREF_READ_ERROR_LEVELDB_IO: |
334 case PREF_READ_ERROR_LEVELDB_CORRUPTION_READ_ONLY: | 331 case PREF_READ_ERROR_LEVELDB_CORRUPTION_READ_ONLY: |
335 case PREF_READ_ERROR_LEVELDB_CORRUPTION: | 332 case PREF_READ_ERROR_LEVELDB_CORRUPTION: |
336 // These are specific to LevelDBPrefStore. | 333 // These are specific to LevelDBPrefStore. |
337 NOTREACHED(); | 334 NOTREACHED(); |
338 case PREF_READ_ERROR_MAX_ENUM: | 335 case PREF_READ_ERROR_MAX_ENUM: |
339 NOTREACHED(); | 336 NOTREACHED(); |
340 break; | 337 break; |
341 } | 338 } |
342 } | 339 } |
343 | 340 |
344 if (pref_filter_) { | 341 if (pref_filter_) { |
345 filtering_in_progress_ = true; | 342 filtering_in_progress_ = true; |
346 const PrefFilter::PostFilterOnLoadCallback post_filter_on_load_callback( | 343 const PrefFilter::PostFilterOnLoadCallback post_filter_on_load_callback( |
347 base::Bind( | 344 base::Bind( |
348 &JsonPrefStore::FinalizeFileRead, this, initialization_successful)); | 345 &JsonPrefStore::FinalizeFileRead, this, initialization_successful)); |
gab
2014/06/06 21:54:58
This callback was using ref-counting simply to avo
erikwright (departed)
2014/06/10 14:25:36
This can be mitigated in other ways. For example,
gab
2014/06/10 16:54:33
For the comment to be added above, how about:
//
erikwright (departed)
2014/06/10 20:27:47
I agree that we don't need a Local State backed Pr
| |
349 pref_filter_->FilterOnLoad(post_filter_on_load_callback, | 346 pref_filter_->FilterOnLoad(post_filter_on_load_callback, prefs_.get()); |
350 unfiltered_prefs.Pass()); | |
351 } else { | 347 } else { |
352 FinalizeFileRead(initialization_successful, unfiltered_prefs.Pass(), false); | 348 FinalizeFileRead(initialization_successful, false); |
353 } | 349 } |
354 } | 350 } |
355 | 351 |
356 JsonPrefStore::~JsonPrefStore() { | 352 JsonPrefStore::~JsonPrefStore() { |
357 CommitPendingWrite(); | 353 CommitPendingWrite(); |
358 } | 354 } |
359 | 355 |
360 bool JsonPrefStore::SerializeData(std::string* output) { | 356 bool JsonPrefStore::SerializeData(std::string* output) { |
361 if (pref_filter_) | 357 if (pref_filter_) |
362 pref_filter_->FilterSerializeData(prefs_.get()); | 358 pref_filter_->FilterSerializeData(prefs_.get()); |
363 | 359 |
364 JSONStringValueSerializer serializer(output); | 360 JSONStringValueSerializer serializer(output); |
365 serializer.set_pretty_print(true); | 361 serializer.set_pretty_print(true); |
366 return serializer.Serialize(*prefs_); | 362 return serializer.Serialize(*prefs_); |
367 } | 363 } |
368 | 364 |
369 void JsonPrefStore::FinalizeFileRead(bool initialization_successful, | 365 void JsonPrefStore::FinalizeFileRead(bool initialization_successful, |
370 scoped_ptr<base::DictionaryValue> prefs, | |
371 bool schedule_write) { | 366 bool schedule_write) { |
372 filtering_in_progress_ = false; | 367 filtering_in_progress_ = false; |
373 | 368 |
374 if (!initialization_successful) { | 369 if (!initialization_successful) { |
375 FOR_EACH_OBSERVER(PrefStore::Observer, | 370 FOR_EACH_OBSERVER(PrefStore::Observer, |
376 observers_, | 371 observers_, |
377 OnInitializationCompleted(false)); | 372 OnInitializationCompleted(false)); |
378 return; | 373 return; |
379 } | 374 } |
380 | 375 |
381 prefs_ = prefs.Pass(); | |
382 | |
383 initialized_ = true; | 376 initialized_ = true; |
384 | 377 |
385 if (schedule_write && !read_only_) | 378 if (schedule_write && !read_only_) |
386 writer_.ScheduleWrite(this); | 379 writer_.ScheduleWrite(this); |
387 | 380 |
388 if (error_delegate_ && read_error_ != PREF_READ_ERROR_NONE) | 381 if (error_delegate_ && read_error_ != PREF_READ_ERROR_NONE) |
389 error_delegate_->OnError(read_error_); | 382 error_delegate_->OnError(read_error_); |
390 | 383 |
391 FOR_EACH_OBSERVER(PrefStore::Observer, | 384 FOR_EACH_OBSERVER(PrefStore::Observer, |
392 observers_, | 385 observers_, |
393 OnInitializationCompleted(true)); | 386 OnInitializationCompleted(true)); |
394 | 387 |
395 return; | 388 return; |
396 } | 389 } |
OLD | NEW |