Chromium Code Reviews| 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 "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/files/scoped_temp_dir.h" | 9 #include "base/files/scoped_temp_dir.h" |
| 10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 30 // A PrefFilter that will intercept all calls to FilterOnLoad() and hold on | 30 // A PrefFilter that will intercept all calls to FilterOnLoad() and hold on |
| 31 // to the |prefs| until explicitly asked to release them. | 31 // to the |prefs| until explicitly asked to release them. |
| 32 class InterceptingPrefFilter : public PrefFilter { | 32 class InterceptingPrefFilter : public PrefFilter { |
| 33 public: | 33 public: |
| 34 InterceptingPrefFilter(); | 34 InterceptingPrefFilter(); |
| 35 virtual ~InterceptingPrefFilter(); | 35 virtual ~InterceptingPrefFilter(); |
| 36 | 36 |
| 37 // PrefFilter implementation: | 37 // PrefFilter implementation: |
| 38 virtual void FilterOnLoad( | 38 virtual void FilterOnLoad( |
| 39 const PostFilterOnLoadCallback& post_filter_on_load_callback, | 39 const PostFilterOnLoadCallback& post_filter_on_load_callback, |
| 40 scoped_ptr<base::DictionaryValue> pref_store_contents) OVERRIDE; | 40 base::DictionaryValue* pref_store_contents) OVERRIDE; |
| 41 virtual void FilterUpdate(const std::string& path) OVERRIDE {} | 41 virtual void FilterUpdate(const std::string& path) OVERRIDE {} |
| 42 virtual void FilterSerializeData( | 42 virtual void FilterSerializeData( |
| 43 const base::DictionaryValue* pref_store_contents) OVERRIDE {} | 43 const base::DictionaryValue* pref_store_contents) OVERRIDE {} |
| 44 | 44 |
| 45 bool has_intercepted_prefs() const { return intercepted_prefs_ != NULL; } | 45 bool has_intercepted_prefs() const { return intercepted_prefs_ != NULL; } |
| 46 | 46 |
| 47 // Finalize an intercepted read, handing |intercept_prefs_| back to its | 47 // Finalize an intercepted read, handing |intercept_prefs_| back to its |
| 48 // JsonPrefStore. | 48 // JsonPrefStore. |
| 49 void ReleasePrefs(); | 49 void ReleasePrefs(); |
| 50 | 50 |
| 51 private: | 51 private: |
| 52 PostFilterOnLoadCallback post_filter_on_load_callback_; | 52 PostFilterOnLoadCallback post_filter_on_load_callback_; |
| 53 scoped_ptr<base::DictionaryValue> intercepted_prefs_; | 53 base::DictionaryValue* intercepted_prefs_; |
| 54 | 54 |
| 55 DISALLOW_COPY_AND_ASSIGN(InterceptingPrefFilter); | 55 DISALLOW_COPY_AND_ASSIGN(InterceptingPrefFilter); |
| 56 }; | 56 }; |
| 57 | 57 |
| 58 InterceptingPrefFilter::InterceptingPrefFilter() {} | 58 InterceptingPrefFilter::InterceptingPrefFilter() : intercepted_prefs_(NULL) {} |
| 59 InterceptingPrefFilter::~InterceptingPrefFilter() {} | 59 InterceptingPrefFilter::~InterceptingPrefFilter() {} |
| 60 | 60 |
| 61 void InterceptingPrefFilter::FilterOnLoad( | 61 void InterceptingPrefFilter::FilterOnLoad( |
| 62 const PostFilterOnLoadCallback& post_filter_on_load_callback, | 62 const PostFilterOnLoadCallback& post_filter_on_load_callback, |
| 63 scoped_ptr<base::DictionaryValue> pref_store_contents) { | 63 base::DictionaryValue* pref_store_contents) { |
| 64 post_filter_on_load_callback_ = post_filter_on_load_callback; | 64 post_filter_on_load_callback_ = post_filter_on_load_callback; |
| 65 intercepted_prefs_ = pref_store_contents.Pass(); | 65 intercepted_prefs_ = pref_store_contents; |
| 66 } | 66 } |
| 67 | 67 |
| 68 void InterceptingPrefFilter::ReleasePrefs() { | 68 void InterceptingPrefFilter::ReleasePrefs() { |
|
gab
2014/06/11 21:12:55
"ReleasePrefs" is technically wrong now that this
| |
| 69 EXPECT_FALSE(post_filter_on_load_callback_.is_null()); | 69 EXPECT_FALSE(post_filter_on_load_callback_.is_null()); |
| 70 post_filter_on_load_callback_.Run(intercepted_prefs_.Pass(), false); | 70 intercepted_prefs_ = NULL; |
| 71 post_filter_on_load_callback_.Run(false); | |
| 71 post_filter_on_load_callback_.Reset(); | 72 post_filter_on_load_callback_.Reset(); |
| 72 } | 73 } |
| 73 | 74 |
| 74 class MockPrefStoreObserver : public PrefStore::Observer { | 75 class MockPrefStoreObserver : public PrefStore::Observer { |
| 75 public: | 76 public: |
| 76 MOCK_METHOD1(OnPrefValueChanged, void (const std::string&)); | 77 MOCK_METHOD1(OnPrefValueChanged, void (const std::string&)); |
| 77 MOCK_METHOD1(OnInitializationCompleted, void (bool)); | 78 MOCK_METHOD1(OnInitializationCompleted, void (bool)); |
| 78 }; | 79 }; |
| 79 | 80 |
| 80 class MockReadErrorDelegate : public PersistentPrefStore::ReadErrorDelegate { | 81 class MockReadErrorDelegate : public PersistentPrefStore::ReadErrorDelegate { |
| (...skipping 293 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 374 intercepting_pref_filter.PassAs<PrefFilter>()); | 375 intercepting_pref_filter.PassAs<PrefFilter>()); |
| 375 | 376 |
| 376 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_ASYNCHRONOUS_TASK_INCOMPLETE, | 377 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_ASYNCHRONOUS_TASK_INCOMPLETE, |
| 377 pref_store->ReadPrefs()); | 378 pref_store->ReadPrefs()); |
| 378 EXPECT_FALSE(pref_store->ReadOnly()); | 379 EXPECT_FALSE(pref_store->ReadOnly()); |
| 379 | 380 |
| 380 // The store shouldn't be considered initialized until the interceptor | 381 // The store shouldn't be considered initialized until the interceptor |
| 381 // returns. | 382 // returns. |
| 382 EXPECT_TRUE(raw_intercepting_pref_filter_->has_intercepted_prefs()); | 383 EXPECT_TRUE(raw_intercepting_pref_filter_->has_intercepted_prefs()); |
| 383 EXPECT_FALSE(pref_store->IsInitializationComplete()); | 384 EXPECT_FALSE(pref_store->IsInitializationComplete()); |
| 384 EXPECT_FALSE(pref_store->GetValue(kHomePage, NULL)); | 385 |
| 386 // The store should be readable while mid-interception. | |
| 387 EXPECT_TRUE(pref_store->GetValue(kHomePage, NULL)); | |
| 385 | 388 |
| 386 raw_intercepting_pref_filter_->ReleasePrefs(); | 389 raw_intercepting_pref_filter_->ReleasePrefs(); |
| 387 | 390 |
| 388 EXPECT_FALSE(raw_intercepting_pref_filter_->has_intercepted_prefs()); | 391 EXPECT_FALSE(raw_intercepting_pref_filter_->has_intercepted_prefs()); |
| 389 EXPECT_TRUE(pref_store->IsInitializationComplete()); | 392 EXPECT_TRUE(pref_store->IsInitializationComplete()); |
| 390 EXPECT_TRUE(pref_store->GetValue(kHomePage, NULL)); | 393 EXPECT_TRUE(pref_store->GetValue(kHomePage, NULL)); |
| 391 | 394 |
| 392 // The JSON file looks like this: | 395 // The JSON file looks like this: |
| 393 // { | 396 // { |
| 394 // "homepage": "http://www.cnn.com", | 397 // "homepage": "http://www.cnn.com", |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 430 pref_store->ReadPrefsAsync(mock_error_delegate); | 433 pref_store->ReadPrefsAsync(mock_error_delegate); |
| 431 | 434 |
| 432 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(0); | 435 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(0); |
| 433 // EXPECT_CALL(*mock_error_delegate, | 436 // EXPECT_CALL(*mock_error_delegate, |
| 434 // OnError(PersistentPrefStore::PREF_READ_ERROR_NONE)).Times(0); | 437 // OnError(PersistentPrefStore::PREF_READ_ERROR_NONE)).Times(0); |
| 435 RunLoop().RunUntilIdle(); | 438 RunLoop().RunUntilIdle(); |
| 436 | 439 |
| 437 EXPECT_FALSE(pref_store->ReadOnly()); | 440 EXPECT_FALSE(pref_store->ReadOnly()); |
| 438 EXPECT_TRUE(raw_intercepting_pref_filter_->has_intercepted_prefs()); | 441 EXPECT_TRUE(raw_intercepting_pref_filter_->has_intercepted_prefs()); |
| 439 EXPECT_FALSE(pref_store->IsInitializationComplete()); | 442 EXPECT_FALSE(pref_store->IsInitializationComplete()); |
| 440 EXPECT_FALSE(pref_store->GetValue(kHomePage, NULL)); | 443 EXPECT_TRUE(pref_store->GetValue(kHomePage, NULL)); |
| 441 } | 444 } |
| 442 | 445 |
| 443 { | 446 { |
| 444 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1); | 447 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1); |
| 445 // EXPECT_CALL(*mock_error_delegate, | 448 // EXPECT_CALL(*mock_error_delegate, |
| 446 // OnError(PersistentPrefStore::PREF_READ_ERROR_NONE)).Times(0); | 449 // OnError(PersistentPrefStore::PREF_READ_ERROR_NONE)).Times(0); |
| 447 | 450 |
| 448 raw_intercepting_pref_filter_->ReleasePrefs(); | 451 raw_intercepting_pref_filter_->ReleasePrefs(); |
| 449 | 452 |
| 450 EXPECT_FALSE(pref_store->ReadOnly()); | 453 EXPECT_FALSE(pref_store->ReadOnly()); |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 463 // "new_windows_in_tabs": true, | 466 // "new_windows_in_tabs": true, |
| 464 // "max_tabs": 20 | 467 // "max_tabs": 20 |
| 465 // } | 468 // } |
| 466 // } | 469 // } |
| 467 | 470 |
| 468 RunBasicJsonPrefStoreTest( | 471 RunBasicJsonPrefStoreTest( |
| 469 pref_store.get(), input_file, data_dir_.AppendASCII("write.golden.json")); | 472 pref_store.get(), input_file, data_dir_.AppendASCII("write.golden.json")); |
| 470 } | 473 } |
| 471 | 474 |
| 472 } // namespace base | 475 } // namespace base |
| OLD | NEW |