| 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 <utility> |
| 8 |
| 7 #include "base/bind.h" | 9 #include "base/bind.h" |
| 8 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
| 9 #include "base/files/scoped_temp_dir.h" | 11 #include "base/files/scoped_temp_dir.h" |
| 10 #include "base/location.h" | 12 #include "base/location.h" |
| 11 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
| 12 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/message_loop/message_loop.h" | 15 #include "base/message_loop/message_loop.h" |
| 14 #include "base/metrics/histogram_samples.h" | 16 #include "base/metrics/histogram_samples.h" |
| 15 #include "base/metrics/statistics_recorder.h" | 17 #include "base/metrics/statistics_recorder.h" |
| 16 #include "base/path_service.h" | 18 #include "base/path_service.h" |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 66 DISALLOW_COPY_AND_ASSIGN(InterceptingPrefFilter); | 68 DISALLOW_COPY_AND_ASSIGN(InterceptingPrefFilter); |
| 67 }; | 69 }; |
| 68 | 70 |
| 69 InterceptingPrefFilter::InterceptingPrefFilter() {} | 71 InterceptingPrefFilter::InterceptingPrefFilter() {} |
| 70 InterceptingPrefFilter::~InterceptingPrefFilter() {} | 72 InterceptingPrefFilter::~InterceptingPrefFilter() {} |
| 71 | 73 |
| 72 void InterceptingPrefFilter::FilterOnLoad( | 74 void InterceptingPrefFilter::FilterOnLoad( |
| 73 const PostFilterOnLoadCallback& post_filter_on_load_callback, | 75 const PostFilterOnLoadCallback& post_filter_on_load_callback, |
| 74 scoped_ptr<base::DictionaryValue> pref_store_contents) { | 76 scoped_ptr<base::DictionaryValue> pref_store_contents) { |
| 75 post_filter_on_load_callback_ = post_filter_on_load_callback; | 77 post_filter_on_load_callback_ = post_filter_on_load_callback; |
| 76 intercepted_prefs_ = pref_store_contents.Pass(); | 78 intercepted_prefs_ = std::move(pref_store_contents); |
| 77 } | 79 } |
| 78 | 80 |
| 79 void InterceptingPrefFilter::ReleasePrefs() { | 81 void InterceptingPrefFilter::ReleasePrefs() { |
| 80 EXPECT_FALSE(post_filter_on_load_callback_.is_null()); | 82 EXPECT_FALSE(post_filter_on_load_callback_.is_null()); |
| 81 post_filter_on_load_callback_.Run(intercepted_prefs_.Pass(), false); | 83 post_filter_on_load_callback_.Run(std::move(intercepted_prefs_), false); |
| 82 post_filter_on_load_callback_.Reset(); | 84 post_filter_on_load_callback_.Reset(); |
| 83 } | 85 } |
| 84 | 86 |
| 85 class MockPrefStoreObserver : public PrefStore::Observer { | 87 class MockPrefStoreObserver : public PrefStore::Observer { |
| 86 public: | 88 public: |
| 87 MOCK_METHOD1(OnPrefValueChanged, void (const std::string&)); | 89 MOCK_METHOD1(OnPrefValueChanged, void (const std::string&)); |
| 88 MOCK_METHOD1(OnInitializationCompleted, void (bool)); | 90 MOCK_METHOD1(OnInitializationCompleted, void (bool)); |
| 89 }; | 91 }; |
| 90 | 92 |
| 91 class MockReadErrorDelegate : public PersistentPrefStore::ReadErrorDelegate { | 93 class MockReadErrorDelegate : public PersistentPrefStore::ReadErrorDelegate { |
| (...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 341 // This test is just documenting some potentially non-obvious behavior. It | 343 // This test is just documenting some potentially non-obvious behavior. It |
| 342 // shouldn't be taken as normative. | 344 // shouldn't be taken as normative. |
| 343 TEST_F(JsonPrefStoreTest, RemoveClearsEmptyParent) { | 345 TEST_F(JsonPrefStoreTest, RemoveClearsEmptyParent) { |
| 344 FilePath pref_file = temp_dir_.path().AppendASCII("empty_values.json"); | 346 FilePath pref_file = temp_dir_.path().AppendASCII("empty_values.json"); |
| 345 | 347 |
| 346 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( | 348 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
| 347 pref_file, message_loop_.task_runner(), scoped_ptr<PrefFilter>()); | 349 pref_file, message_loop_.task_runner(), scoped_ptr<PrefFilter>()); |
| 348 | 350 |
| 349 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue); | 351 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue); |
| 350 dict->SetString("key", "value"); | 352 dict->SetString("key", "value"); |
| 351 pref_store->SetValue("dict", dict.Pass(), | 353 pref_store->SetValue("dict", std::move(dict), |
| 352 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); | 354 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
| 353 | 355 |
| 354 pref_store->RemoveValue("dict.key", | 356 pref_store->RemoveValue("dict.key", |
| 355 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); | 357 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
| 356 | 358 |
| 357 const base::Value* retrieved_dict = NULL; | 359 const base::Value* retrieved_dict = NULL; |
| 358 bool has_dict = pref_store->GetValue("dict", &retrieved_dict); | 360 bool has_dict = pref_store->GetValue("dict", &retrieved_dict); |
| 359 EXPECT_FALSE(has_dict); | 361 EXPECT_FALSE(has_dict); |
| 360 } | 362 } |
| 361 | 363 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 385 temp_dir_.path().AppendASCII("write.json"))); | 387 temp_dir_.path().AppendASCII("write.json"))); |
| 386 | 388 |
| 387 // Test that the persistent value can be loaded. | 389 // Test that the persistent value can be loaded. |
| 388 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); | 390 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); |
| 389 ASSERT_TRUE(PathExists(input_file)); | 391 ASSERT_TRUE(PathExists(input_file)); |
| 390 | 392 |
| 391 scoped_ptr<InterceptingPrefFilter> intercepting_pref_filter( | 393 scoped_ptr<InterceptingPrefFilter> intercepting_pref_filter( |
| 392 new InterceptingPrefFilter()); | 394 new InterceptingPrefFilter()); |
| 393 InterceptingPrefFilter* raw_intercepting_pref_filter_ = | 395 InterceptingPrefFilter* raw_intercepting_pref_filter_ = |
| 394 intercepting_pref_filter.get(); | 396 intercepting_pref_filter.get(); |
| 395 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( | 397 scoped_refptr<JsonPrefStore> pref_store = |
| 396 input_file, message_loop_.task_runner(), intercepting_pref_filter.Pass()); | 398 new JsonPrefStore(input_file, message_loop_.task_runner(), |
| 399 std::move(intercepting_pref_filter)); |
| 397 | 400 |
| 398 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_ASYNCHRONOUS_TASK_INCOMPLETE, | 401 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_ASYNCHRONOUS_TASK_INCOMPLETE, |
| 399 pref_store->ReadPrefs()); | 402 pref_store->ReadPrefs()); |
| 400 EXPECT_FALSE(pref_store->ReadOnly()); | 403 EXPECT_FALSE(pref_store->ReadOnly()); |
| 401 | 404 |
| 402 // The store shouldn't be considered initialized until the interceptor | 405 // The store shouldn't be considered initialized until the interceptor |
| 403 // returns. | 406 // returns. |
| 404 EXPECT_TRUE(raw_intercepting_pref_filter_->has_intercepted_prefs()); | 407 EXPECT_TRUE(raw_intercepting_pref_filter_->has_intercepted_prefs()); |
| 405 EXPECT_FALSE(pref_store->IsInitializationComplete()); | 408 EXPECT_FALSE(pref_store->IsInitializationComplete()); |
| 406 EXPECT_FALSE(pref_store->GetValue(kHomePage, NULL)); | 409 EXPECT_FALSE(pref_store->GetValue(kHomePage, NULL)); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 430 temp_dir_.path().AppendASCII("write.json"))); | 433 temp_dir_.path().AppendASCII("write.json"))); |
| 431 | 434 |
| 432 // Test that the persistent value can be loaded. | 435 // Test that the persistent value can be loaded. |
| 433 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); | 436 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); |
| 434 ASSERT_TRUE(PathExists(input_file)); | 437 ASSERT_TRUE(PathExists(input_file)); |
| 435 | 438 |
| 436 scoped_ptr<InterceptingPrefFilter> intercepting_pref_filter( | 439 scoped_ptr<InterceptingPrefFilter> intercepting_pref_filter( |
| 437 new InterceptingPrefFilter()); | 440 new InterceptingPrefFilter()); |
| 438 InterceptingPrefFilter* raw_intercepting_pref_filter_ = | 441 InterceptingPrefFilter* raw_intercepting_pref_filter_ = |
| 439 intercepting_pref_filter.get(); | 442 intercepting_pref_filter.get(); |
| 440 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( | 443 scoped_refptr<JsonPrefStore> pref_store = |
| 441 input_file, message_loop_.task_runner(), intercepting_pref_filter.Pass()); | 444 new JsonPrefStore(input_file, message_loop_.task_runner(), |
| 445 std::move(intercepting_pref_filter)); |
| 442 | 446 |
| 443 MockPrefStoreObserver mock_observer; | 447 MockPrefStoreObserver mock_observer; |
| 444 pref_store->AddObserver(&mock_observer); | 448 pref_store->AddObserver(&mock_observer); |
| 445 | 449 |
| 446 // Ownership of the |mock_error_delegate| is handed to the |pref_store| below. | 450 // Ownership of the |mock_error_delegate| is handed to the |pref_store| below. |
| 447 MockReadErrorDelegate* mock_error_delegate = new MockReadErrorDelegate; | 451 MockReadErrorDelegate* mock_error_delegate = new MockReadErrorDelegate; |
| 448 | 452 |
| 449 { | 453 { |
| 450 pref_store->ReadPrefsAsync(mock_error_delegate); | 454 pref_store->ReadPrefsAsync(mock_error_delegate); |
| 451 | 455 |
| (...skipping 497 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 949 ASSERT_TRUE(file_writer->HasPendingWrite()); | 953 ASSERT_TRUE(file_writer->HasPendingWrite()); |
| 950 | 954 |
| 951 // Call CommitPendingWrite and check that the lossy pref is there with the | 955 // Call CommitPendingWrite and check that the lossy pref is there with the |
| 952 // last value set above. | 956 // last value set above. |
| 953 pref_store->CommitPendingWrite(); | 957 pref_store->CommitPendingWrite(); |
| 954 ASSERT_FALSE(file_writer->HasPendingWrite()); | 958 ASSERT_FALSE(file_writer->HasPendingWrite()); |
| 955 ASSERT_EQ("{\"lossy\":\"lossy\"}", GetTestFileContents()); | 959 ASSERT_EQ("{\"lossy\":\"lossy\"}", GetTestFileContents()); |
| 956 } | 960 } |
| 957 | 961 |
| 958 } // namespace base | 962 } // namespace base |
| OLD | NEW |