| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 "net/sdch/sdch_owner.h" | 5 #include "net/sdch/sdch_owner.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/location.h" | 9 #include "base/location.h" |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "base/memory/memory_pressure_listener.h" | 11 #include "base/memory/memory_pressure_listener.h" |
| 12 #include "base/memory/ptr_util.h" |
| 12 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
| 13 #include "base/single_thread_task_runner.h" | 14 #include "base/single_thread_task_runner.h" |
| 14 #include "base/strings/stringprintf.h" | 15 #include "base/strings/stringprintf.h" |
| 15 #include "base/test/histogram_tester.h" | 16 #include "base/test/histogram_tester.h" |
| 16 #include "base/test/simple_test_clock.h" | 17 #include "base/test/simple_test_clock.h" |
| 17 #include "base/thread_task_runner_handle.h" | 18 #include "base/thread_task_runner_handle.h" |
| 18 #include "base/values.h" | 19 #include "base/values.h" |
| 19 #include "net/base/sdch_manager.h" | 20 #include "net/base/sdch_manager.h" |
| 20 #include "net/log/net_log.h" | 21 #include "net/log/net_log.h" |
| 21 #include "net/url_request/url_request.h" | 22 #include "net/url_request/url_request.h" |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 ReadError GetReadError() const override { return PERSISTENCE_FAILURE_NONE; } | 85 ReadError GetReadError() const override { return PERSISTENCE_FAILURE_NONE; } |
| 85 | 86 |
| 86 bool GetValue(const base::DictionaryValue** result) const override { | 87 bool GetValue(const base::DictionaryValue** result) const override { |
| 87 *result = &storage_; | 88 *result = &storage_; |
| 88 return true; | 89 return true; |
| 89 } | 90 } |
| 90 bool GetMutableValue(base::DictionaryValue** result) override { | 91 bool GetMutableValue(base::DictionaryValue** result) override { |
| 91 *result = &storage_; | 92 *result = &storage_; |
| 92 return true; | 93 return true; |
| 93 } | 94 } |
| 94 void SetValue(scoped_ptr<base::DictionaryValue> value) override { | 95 void SetValue(std::unique_ptr<base::DictionaryValue> value) override { |
| 95 storage_.Clear(); | 96 storage_.Clear(); |
| 96 storage_.MergeDictionary(value.get()); | 97 storage_.MergeDictionary(value.get()); |
| 97 } | 98 } |
| 98 | 99 |
| 99 void ReportValueChanged() override {} | 100 void ReportValueChanged() override {} |
| 100 | 101 |
| 101 // This storage class requires no special initialization. | 102 // This storage class requires no special initialization. |
| 102 bool IsInitializationComplete() override { return initialized_; } | 103 bool IsInitializationComplete() override { return initialized_; } |
| 103 void StartObservingInit(SdchOwner* observer) override { | 104 void StartObservingInit(SdchOwner* observer) override { |
| 104 DCHECK(!initialization_observer_); | 105 DCHECK(!initialization_observer_); |
| (...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 327 | 328 |
| 328 int JobsRecentlyCreated() { | 329 int JobsRecentlyCreated() { |
| 329 int result = error_jobs_created - last_jobs_created_; | 330 int result = error_jobs_created - last_jobs_created_; |
| 330 last_jobs_created_ = error_jobs_created; | 331 last_jobs_created_ = error_jobs_created; |
| 331 return result; | 332 return result; |
| 332 } | 333 } |
| 333 | 334 |
| 334 bool DictionaryPresentInManager(const std::string& server_hash) { | 335 bool DictionaryPresentInManager(const std::string& server_hash) { |
| 335 // Presumes all tests use generic url. | 336 // Presumes all tests use generic url. |
| 336 SdchProblemCode tmp; | 337 SdchProblemCode tmp; |
| 337 scoped_ptr<SdchManager::DictionarySet> set( | 338 std::unique_ptr<SdchManager::DictionarySet> set( |
| 338 sdch_manager_.GetDictionarySetByHash(GURL(generic_url), server_hash, | 339 sdch_manager_.GetDictionarySetByHash(GURL(generic_url), server_hash, |
| 339 &tmp)); | 340 &tmp)); |
| 340 return !!set.get(); | 341 return !!set.get(); |
| 341 } | 342 } |
| 342 | 343 |
| 343 void WaitForNoJobs() { | 344 void WaitForNoJobs() { |
| 344 if (outstanding_url_request_error_counting_jobs == 0) | 345 if (outstanding_url_request_error_counting_jobs == 0) |
| 345 return; | 346 return; |
| 346 | 347 |
| 347 base::RunLoop run_loop; | 348 base::RunLoop run_loop; |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 398 int last_jobs_created_; | 399 int last_jobs_created_; |
| 399 BoundNetLog net_log_; | 400 BoundNetLog net_log_; |
| 400 int dictionary_creation_index_; | 401 int dictionary_creation_index_; |
| 401 | 402 |
| 402 // The dependencies of these objects (sdch_owner_ -> {sdch_manager_, | 403 // The dependencies of these objects (sdch_owner_ -> {sdch_manager_, |
| 403 // url_request_context_}, url_request_context_->job_factory_) require | 404 // url_request_context_}, url_request_context_->job_factory_) require |
| 404 // this order for correct destruction semantics. | 405 // this order for correct destruction semantics. |
| 405 MockURLRequestJobFactory job_factory_; | 406 MockURLRequestJobFactory job_factory_; |
| 406 URLRequestContext url_request_context_; | 407 URLRequestContext url_request_context_; |
| 407 SdchManager sdch_manager_; | 408 SdchManager sdch_manager_; |
| 408 scoped_ptr<SdchOwner> sdch_owner_; | 409 std::unique_ptr<SdchOwner> sdch_owner_; |
| 409 | 410 |
| 410 DISALLOW_COPY_AND_ASSIGN(SdchOwnerTest); | 411 DISALLOW_COPY_AND_ASSIGN(SdchOwnerTest); |
| 411 }; | 412 }; |
| 412 | 413 |
| 413 // Does OnGetDictionary result in a fetch when there's enough space, and not | 414 // Does OnGetDictionary result in a fetch when there's enough space, and not |
| 414 // when there's not? | 415 // when there's not? |
| 415 TEST_F(SdchOwnerTest, OnGetDictionary_Fetching) { | 416 TEST_F(SdchOwnerTest, OnGetDictionary_Fetching) { |
| 416 GURL request_url(std::string(generic_url) + "/r1"); | 417 GURL request_url(std::string(generic_url) + "/r1"); |
| 417 | 418 |
| 418 // Fetch generated when empty. | 419 // Fetch generated when empty. |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 470 } | 471 } |
| 471 | 472 |
| 472 // Confirm auto-eviction happens if space is needed. | 473 // Confirm auto-eviction happens if space is needed. |
| 473 TEST_F(SdchOwnerTest, ConfirmAutoEviction) { | 474 TEST_F(SdchOwnerTest, ConfirmAutoEviction) { |
| 474 base::Time start_time = base::Time::Now(); | 475 base::Time start_time = base::Time::Now(); |
| 475 std::string server_hash_d1; | 476 std::string server_hash_d1; |
| 476 std::string server_hash_d2; | 477 std::string server_hash_d2; |
| 477 std::string server_hash_d3; | 478 std::string server_hash_d3; |
| 478 | 479 |
| 479 base::SimpleTestClock* test_clock = new base::SimpleTestClock(); | 480 base::SimpleTestClock* test_clock = new base::SimpleTestClock(); |
| 480 sdch_owner().SetClockForTesting(make_scoped_ptr(test_clock)); | 481 sdch_owner().SetClockForTesting(base::WrapUnique(test_clock)); |
| 481 test_clock->SetNow(base::Time::Now()); | 482 test_clock->SetNow(base::Time::Now()); |
| 482 | 483 |
| 483 // Add two dictionaries, one recent, one more than a day in the past. | 484 // Add two dictionaries, one recent, one more than a day in the past. |
| 484 base::Time fresh(base::Time::Now() - base::TimeDelta::FromHours(23)); | 485 base::Time fresh(base::Time::Now() - base::TimeDelta::FromHours(23)); |
| 485 base::Time stale(base::Time::Now() - base::TimeDelta::FromHours(25)); | 486 base::Time stale(base::Time::Now() - base::TimeDelta::FromHours(25)); |
| 486 | 487 |
| 487 EXPECT_TRUE( | 488 EXPECT_TRUE( |
| 488 CreateAndAddDictionary(kMaxSizeForTesting / 2, fresh, &server_hash_d1)); | 489 CreateAndAddDictionary(kMaxSizeForTesting / 2, fresh, &server_hash_d1)); |
| 489 EXPECT_TRUE( | 490 EXPECT_TRUE( |
| 490 CreateAndAddDictionary(kMaxSizeForTesting / 2, stale, &server_hash_d2)); | 491 CreateAndAddDictionary(kMaxSizeForTesting / 2, stale, &server_hash_d2)); |
| (...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 727 // Addition should now succeed. | 728 // Addition should now succeed. |
| 728 EXPECT_TRUE( | 729 EXPECT_TRUE( |
| 729 CreateAndAddDictionary(kMaxSizeForTesting, base::Time::Now(), nullptr)); | 730 CreateAndAddDictionary(kMaxSizeForTesting, base::Time::Now(), nullptr)); |
| 730 } | 731 } |
| 731 | 732 |
| 732 // Confirm that use of a pinned dictionary after its removal works properly. | 733 // Confirm that use of a pinned dictionary after its removal works properly. |
| 733 TEST_F(SdchOwnerTest, PinRemoveUse) { | 734 TEST_F(SdchOwnerTest, PinRemoveUse) { |
| 734 // Pass ownership of the storage to the SdchOwner, but keep a pointer. | 735 // Pass ownership of the storage to the SdchOwner, but keep a pointer. |
| 735 TestPrefStorage* pref_store = new TestPrefStorage(true); | 736 TestPrefStorage* pref_store = new TestPrefStorage(true); |
| 736 sdch_owner().EnablePersistentStorage( | 737 sdch_owner().EnablePersistentStorage( |
| 737 scoped_ptr<SdchOwner::PrefStorage>(pref_store)); | 738 std::unique_ptr<SdchOwner::PrefStorage>(pref_store)); |
| 738 | 739 |
| 739 std::string server_hash_d1; | 740 std::string server_hash_d1; |
| 740 EXPECT_TRUE(CreateAndAddDictionary(kMaxSizeForTesting / 2, base::Time::Now(), | 741 EXPECT_TRUE(CreateAndAddDictionary(kMaxSizeForTesting / 2, base::Time::Now(), |
| 741 &server_hash_d1)); | 742 &server_hash_d1)); |
| 742 | 743 |
| 743 scoped_ptr<SdchManager::DictionarySet> return_set( | 744 std::unique_ptr<SdchManager::DictionarySet> return_set( |
| 744 sdch_manager().GetDictionarySet( | 745 sdch_manager().GetDictionarySet( |
| 745 GURL(std::string(generic_url) + "/x.html"))); | 746 GURL(std::string(generic_url) + "/x.html"))); |
| 746 ASSERT_TRUE(return_set.get()); | 747 ASSERT_TRUE(return_set.get()); |
| 747 EXPECT_TRUE(return_set->GetDictionaryText(server_hash_d1)); | 748 EXPECT_TRUE(return_set->GetDictionaryText(server_hash_d1)); |
| 748 | 749 |
| 749 const base::Value* result = nullptr; | 750 const base::Value* result = nullptr; |
| 750 const base::DictionaryValue* dict_result = nullptr; | 751 const base::DictionaryValue* dict_result = nullptr; |
| 751 ASSERT_TRUE(pref_store->GetValue(&dict_result)); | 752 ASSERT_TRUE(pref_store->GetValue(&dict_result)); |
| 752 EXPECT_TRUE(dict_result->Get("dictionaries", &result)); | 753 EXPECT_TRUE(dict_result->Get("dictionaries", &result)); |
| 753 EXPECT_TRUE(dict_result->Get("dictionaries." + server_hash_d1, &result)); | 754 EXPECT_TRUE(dict_result->Get("dictionaries." + server_hash_d1, &result)); |
| 754 | 755 |
| 755 sdch_manager().ClearData(); | 756 sdch_manager().ClearData(); |
| 756 | 757 |
| 757 ASSERT_TRUE(pref_store->GetValue(&dict_result)); | 758 ASSERT_TRUE(pref_store->GetValue(&dict_result)); |
| 758 EXPECT_TRUE(dict_result->Get("dictionaries", &result)); | 759 EXPECT_TRUE(dict_result->Get("dictionaries", &result)); |
| 759 EXPECT_FALSE(dict_result->Get("dictionaries." + server_hash_d1, &result)); | 760 EXPECT_FALSE(dict_result->Get("dictionaries." + server_hash_d1, &result)); |
| 760 | 761 |
| 761 scoped_ptr<SdchManager::DictionarySet> return_set2( | 762 std::unique_ptr<SdchManager::DictionarySet> return_set2( |
| 762 sdch_manager().GetDictionarySet( | 763 sdch_manager().GetDictionarySet( |
| 763 GURL(std::string(generic_url) + "/x.html"))); | 764 GURL(std::string(generic_url) + "/x.html"))); |
| 764 EXPECT_FALSE(return_set2.get()); | 765 EXPECT_FALSE(return_set2.get()); |
| 765 | 766 |
| 766 sdch_manager().OnDictionaryUsed(server_hash_d1); | 767 sdch_manager().OnDictionaryUsed(server_hash_d1); |
| 767 | 768 |
| 768 ASSERT_TRUE(pref_store->GetValue(&dict_result)); | 769 ASSERT_TRUE(pref_store->GetValue(&dict_result)); |
| 769 EXPECT_TRUE(dict_result->Get("dictionaries", &result)); | 770 EXPECT_TRUE(dict_result->Get("dictionaries", &result)); |
| 770 EXPECT_FALSE(dict_result->Get("dictionaries." + server_hash_d1, &result)); | 771 EXPECT_FALSE(dict_result->Get("dictionaries." + server_hash_d1, &result)); |
| 771 } | 772 } |
| (...skipping 23 matching lines...) Expand all Loading... |
| 795 public: | 796 public: |
| 796 SdchOwnerPersistenceTest() {} | 797 SdchOwnerPersistenceTest() {} |
| 797 virtual ~SdchOwnerPersistenceTest() {} | 798 virtual ~SdchOwnerPersistenceTest() {} |
| 798 | 799 |
| 799 void ClearOwner() { | 800 void ClearOwner() { |
| 800 owner_.reset(NULL); | 801 owner_.reset(NULL); |
| 801 } | 802 } |
| 802 | 803 |
| 803 // If the storage points is non-null it will be saved as the persistent | 804 // If the storage points is non-null it will be saved as the persistent |
| 804 // storage for the SdchOwner. | 805 // storage for the SdchOwner. |
| 805 void ResetOwner(scoped_ptr<SdchOwner::PrefStorage> storage) { | 806 void ResetOwner(std::unique_ptr<SdchOwner::PrefStorage> storage) { |
| 806 // This has to be done first, since SdchOwner may be observing SdchManager, | 807 // This has to be done first, since SdchOwner may be observing SdchManager, |
| 807 // and SdchManager can't be destroyed with a live observer. | 808 // and SdchManager can't be destroyed with a live observer. |
| 808 owner_.reset(NULL); | 809 owner_.reset(NULL); |
| 809 manager_.reset(new SdchManager()); | 810 manager_.reset(new SdchManager()); |
| 810 fetcher_ = new MockSdchDictionaryFetcher(); | 811 fetcher_ = new MockSdchDictionaryFetcher(); |
| 811 owner_.reset(new SdchOwner(manager_.get(), | 812 owner_.reset(new SdchOwner(manager_.get(), |
| 812 &url_request_context_)); | 813 &url_request_context_)); |
| 813 owner_->SetMaxTotalDictionarySize(SdchOwnerTest::kMaxSizeForTesting); | 814 owner_->SetMaxTotalDictionarySize(SdchOwnerTest::kMaxSizeForTesting); |
| 814 owner_->SetMinSpaceForDictionaryFetch( | 815 owner_->SetMinSpaceForDictionaryFetch( |
| 815 SdchOwnerTest::kMinFetchSpaceForTesting); | 816 SdchOwnerTest::kMinFetchSpaceForTesting); |
| 816 owner_->SetFetcherForTesting(make_scoped_ptr(fetcher_)); | 817 owner_->SetFetcherForTesting(base::WrapUnique(fetcher_)); |
| 817 if (storage) | 818 if (storage) |
| 818 owner_->EnablePersistentStorage(std::move(storage)); | 819 owner_->EnablePersistentStorage(std::move(storage)); |
| 819 } | 820 } |
| 820 | 821 |
| 821 void InsertDictionaryForURL(const GURL& url, const std::string& nonce) { | 822 void InsertDictionaryForURL(const GURL& url, const std::string& nonce) { |
| 822 owner_->OnDictionaryFetched(base::Time::Now(), base::Time::Now(), 1, | 823 owner_->OnDictionaryFetched(base::Time::Now(), base::Time::Now(), 1, |
| 823 CreateDictionary(url, nonce), url, net_log_, | 824 CreateDictionary(url, nonce), url, net_log_, |
| 824 false); | 825 false); |
| 825 } | 826 } |
| 826 | 827 |
| 827 bool CompleteLoadFromURL(const GURL& url, const std::string& nonce, | 828 bool CompleteLoadFromURL(const GURL& url, const std::string& nonce, |
| 828 bool was_from_cache) { | 829 bool was_from_cache) { |
| 829 return fetcher_->CompletePendingRequest(url, CreateDictionary(url, nonce), | 830 return fetcher_->CompletePendingRequest(url, CreateDictionary(url, nonce), |
| 830 net_log_, was_from_cache); | 831 net_log_, was_from_cache); |
| 831 } | 832 } |
| 832 | 833 |
| 833 std::string CreateDictionary(const GURL& url, const std::string& nonce) { | 834 std::string CreateDictionary(const GURL& url, const std::string& nonce) { |
| 834 std::string dict; | 835 std::string dict; |
| 835 dict.append("Domain: "); | 836 dict.append("Domain: "); |
| 836 dict.append(url.host()); | 837 dict.append(url.host()); |
| 837 dict.append("\n\n"); | 838 dict.append("\n\n"); |
| 838 dict.append(url.spec()); | 839 dict.append(url.spec()); |
| 839 dict.append(nonce); | 840 dict.append(nonce); |
| 840 return dict; | 841 return dict; |
| 841 } | 842 } |
| 842 | 843 |
| 843 protected: | 844 protected: |
| 844 BoundNetLog net_log_; | 845 BoundNetLog net_log_; |
| 845 scoped_ptr<SdchManager> manager_; | 846 std::unique_ptr<SdchManager> manager_; |
| 846 MockSdchDictionaryFetcher* fetcher_; | 847 MockSdchDictionaryFetcher* fetcher_; |
| 847 scoped_ptr<SdchOwner> owner_; | 848 std::unique_ptr<SdchOwner> owner_; |
| 848 TestURLRequestContext url_request_context_; | 849 TestURLRequestContext url_request_context_; |
| 849 }; | 850 }; |
| 850 | 851 |
| 851 // Test an empty persistence store. | 852 // Test an empty persistence store. |
| 852 TEST_F(SdchOwnerPersistenceTest, Empty) { | 853 TEST_F(SdchOwnerPersistenceTest, Empty) { |
| 853 ResetOwner(make_scoped_ptr(new TestPrefStorage(true))); | 854 ResetOwner(base::WrapUnique(new TestPrefStorage(true))); |
| 854 EXPECT_EQ(0, owner_->GetDictionaryCountForTesting()); | 855 EXPECT_EQ(0, owner_->GetDictionaryCountForTesting()); |
| 855 } | 856 } |
| 856 | 857 |
| 857 // Test a persistence store with a bad version number. | 858 // Test a persistence store with a bad version number. |
| 858 TEST_F(SdchOwnerPersistenceTest, Persistent_BadVersion) { | 859 TEST_F(SdchOwnerPersistenceTest, Persistent_BadVersion) { |
| 859 scoped_ptr<base::DictionaryValue> sdch_dict(new base::DictionaryValue()); | 860 std::unique_ptr<base::DictionaryValue> sdch_dict(new base::DictionaryValue()); |
| 860 sdch_dict->SetInteger("version", 2); | 861 sdch_dict->SetInteger("version", 2); |
| 861 | 862 |
| 862 scoped_ptr<TestPrefStorage> storage(new TestPrefStorage(true)); | 863 std::unique_ptr<TestPrefStorage> storage(new TestPrefStorage(true)); |
| 863 storage->SetValue(std::move(sdch_dict)); | 864 storage->SetValue(std::move(sdch_dict)); |
| 864 | 865 |
| 865 TestPrefStorage* old_storage = storage.get(); // Save storage pointer. | 866 TestPrefStorage* old_storage = storage.get(); // Save storage pointer. |
| 866 ResetOwner(std::move(storage)); // Takes ownership of storage pointer. | 867 ResetOwner(std::move(storage)); // Takes ownership of storage pointer. |
| 867 | 868 |
| 868 storage.reset(new TestPrefStorage(*old_storage)); | 869 storage.reset(new TestPrefStorage(*old_storage)); |
| 869 ResetOwner(std::move(storage)); | 870 ResetOwner(std::move(storage)); |
| 870 EXPECT_EQ(0, owner_->GetDictionaryCountForTesting()); | 871 EXPECT_EQ(0, owner_->GetDictionaryCountForTesting()); |
| 871 } | 872 } |
| 872 | 873 |
| 873 // Test a persistence store with an empty dictionaries map. | 874 // Test a persistence store with an empty dictionaries map. |
| 874 TEST_F(SdchOwnerPersistenceTest, Persistent_EmptyDictList) { | 875 TEST_F(SdchOwnerPersistenceTest, Persistent_EmptyDictList) { |
| 875 scoped_ptr<base::DictionaryValue> sdch_dict(new base::DictionaryValue()); | 876 std::unique_ptr<base::DictionaryValue> sdch_dict(new base::DictionaryValue()); |
| 876 scoped_ptr<base::DictionaryValue> dicts(new base::DictionaryValue()); | 877 std::unique_ptr<base::DictionaryValue> dicts(new base::DictionaryValue()); |
| 877 sdch_dict->SetInteger("version", 1); | 878 sdch_dict->SetInteger("version", 1); |
| 878 sdch_dict->Set("dictionaries", std::move(dicts)); | 879 sdch_dict->Set("dictionaries", std::move(dicts)); |
| 879 | 880 |
| 880 scoped_ptr<TestPrefStorage> storage(new TestPrefStorage(true)); | 881 std::unique_ptr<TestPrefStorage> storage(new TestPrefStorage(true)); |
| 881 storage->SetValue(std::move(sdch_dict)); | 882 storage->SetValue(std::move(sdch_dict)); |
| 882 ResetOwner(std::move(storage)); | 883 ResetOwner(std::move(storage)); |
| 883 EXPECT_EQ(0, owner_->GetDictionaryCountForTesting()); | 884 EXPECT_EQ(0, owner_->GetDictionaryCountForTesting()); |
| 884 } | 885 } |
| 885 | 886 |
| 886 TEST_F(SdchOwnerPersistenceTest, OneDict) { | 887 TEST_F(SdchOwnerPersistenceTest, OneDict) { |
| 887 const GURL url("http://www.example.com/dict"); | 888 const GURL url("http://www.example.com/dict"); |
| 888 | 889 |
| 889 scoped_ptr<TestPrefStorage> storage(new TestPrefStorage(true)); | 890 std::unique_ptr<TestPrefStorage> storage(new TestPrefStorage(true)); |
| 890 TestPrefStorage* old_storage = storage.get(); // Save storage pointer. | 891 TestPrefStorage* old_storage = storage.get(); // Save storage pointer. |
| 891 ResetOwner(std::move(storage)); // Takes ownership of storage pointer. | 892 ResetOwner(std::move(storage)); // Takes ownership of storage pointer. |
| 892 EXPECT_EQ(0, owner_->GetDictionaryCountForTesting()); | 893 EXPECT_EQ(0, owner_->GetDictionaryCountForTesting()); |
| 893 InsertDictionaryForURL(url, "0"); | 894 InsertDictionaryForURL(url, "0"); |
| 894 EXPECT_EQ(1, owner_->GetDictionaryCountForTesting()); | 895 EXPECT_EQ(1, owner_->GetDictionaryCountForTesting()); |
| 895 | 896 |
| 896 storage.reset(new TestPrefStorage(*old_storage)); | 897 storage.reset(new TestPrefStorage(*old_storage)); |
| 897 ResetOwner(std::move(storage)); | 898 ResetOwner(std::move(storage)); |
| 898 EXPECT_EQ(0, owner_->GetDictionaryCountForTesting()); | 899 EXPECT_EQ(0, owner_->GetDictionaryCountForTesting()); |
| 899 EXPECT_TRUE(CompleteLoadFromURL(url, "0", true)); | 900 EXPECT_TRUE(CompleteLoadFromURL(url, "0", true)); |
| 900 EXPECT_EQ(1, owner_->GetDictionaryCountForTesting()); | 901 EXPECT_EQ(1, owner_->GetDictionaryCountForTesting()); |
| 901 } | 902 } |
| 902 | 903 |
| 903 TEST_F(SdchOwnerPersistenceTest, TwoDicts) { | 904 TEST_F(SdchOwnerPersistenceTest, TwoDicts) { |
| 904 const GURL url0("http://www.example.com/dict0"); | 905 const GURL url0("http://www.example.com/dict0"); |
| 905 const GURL url1("http://www.example.com/dict1"); | 906 const GURL url1("http://www.example.com/dict1"); |
| 906 | 907 |
| 907 scoped_ptr<TestPrefStorage> storage(new TestPrefStorage(true)); | 908 std::unique_ptr<TestPrefStorage> storage(new TestPrefStorage(true)); |
| 908 TestPrefStorage* old_storage = storage.get(); // Save storage pointer. | 909 TestPrefStorage* old_storage = storage.get(); // Save storage pointer. |
| 909 ResetOwner(std::move(storage)); // Takes ownership of storage pointer. | 910 ResetOwner(std::move(storage)); // Takes ownership of storage pointer. |
| 910 InsertDictionaryForURL(url0, "0"); | 911 InsertDictionaryForURL(url0, "0"); |
| 911 InsertDictionaryForURL(url1, "1"); | 912 InsertDictionaryForURL(url1, "1"); |
| 912 | 913 |
| 913 storage.reset(new TestPrefStorage(*old_storage)); | 914 storage.reset(new TestPrefStorage(*old_storage)); |
| 914 ResetOwner(std::move(storage)); | 915 ResetOwner(std::move(storage)); |
| 915 EXPECT_TRUE(CompleteLoadFromURL(url0, "0", true)); | 916 EXPECT_TRUE(CompleteLoadFromURL(url0, "0", true)); |
| 916 EXPECT_TRUE(CompleteLoadFromURL(url1, "1", true)); | 917 EXPECT_TRUE(CompleteLoadFromURL(url1, "1", true)); |
| 917 EXPECT_EQ(2, owner_->GetDictionaryCountForTesting()); | 918 EXPECT_EQ(2, owner_->GetDictionaryCountForTesting()); |
| 918 EXPECT_TRUE(owner_->HasDictionaryFromURLForTesting(url0)); | 919 EXPECT_TRUE(owner_->HasDictionaryFromURLForTesting(url0)); |
| 919 EXPECT_TRUE(owner_->HasDictionaryFromURLForTesting(url1)); | 920 EXPECT_TRUE(owner_->HasDictionaryFromURLForTesting(url1)); |
| 920 } | 921 } |
| 921 | 922 |
| 922 TEST_F(SdchOwnerPersistenceTest, OneGoodDictOneBadDict) { | 923 TEST_F(SdchOwnerPersistenceTest, OneGoodDictOneBadDict) { |
| 923 const GURL url0("http://www.example.com/dict0"); | 924 const GURL url0("http://www.example.com/dict0"); |
| 924 const GURL url1("http://www.example.com/dict1"); | 925 const GURL url1("http://www.example.com/dict1"); |
| 925 | 926 |
| 926 scoped_ptr<TestPrefStorage> storage(new TestPrefStorage(true)); | 927 std::unique_ptr<TestPrefStorage> storage(new TestPrefStorage(true)); |
| 927 TestPrefStorage* old_storage = storage.get(); // Save storage pointer. | 928 TestPrefStorage* old_storage = storage.get(); // Save storage pointer. |
| 928 ResetOwner(std::move(storage)); // Takes ownership of storage. | 929 ResetOwner(std::move(storage)); // Takes ownership of storage. |
| 929 InsertDictionaryForURL(url0, "0"); | 930 InsertDictionaryForURL(url0, "0"); |
| 930 InsertDictionaryForURL(url1, "1"); | 931 InsertDictionaryForURL(url1, "1"); |
| 931 | 932 |
| 932 // Make a new storage based on the current contents of the old one. | 933 // Make a new storage based on the current contents of the old one. |
| 933 storage.reset(new TestPrefStorage(*old_storage)); | 934 storage.reset(new TestPrefStorage(*old_storage)); |
| 934 base::DictionaryValue* dict = nullptr; | 935 base::DictionaryValue* dict = nullptr; |
| 935 ASSERT_TRUE(GetDictionaryForURL(storage.get(), url1, nullptr, &dict)); | 936 ASSERT_TRUE(GetDictionaryForURL(storage.get(), url1, nullptr, &dict)); |
| 936 dict->Remove("use_count", nullptr); | 937 dict->Remove("use_count", nullptr); |
| 937 | 938 |
| 938 ResetOwner(std::move(storage)); | 939 ResetOwner(std::move(storage)); |
| 939 EXPECT_TRUE(CompleteLoadFromURL(url0, "0", true)); | 940 EXPECT_TRUE(CompleteLoadFromURL(url0, "0", true)); |
| 940 EXPECT_FALSE(CompleteLoadFromURL(url1, "1", true)); | 941 EXPECT_FALSE(CompleteLoadFromURL(url1, "1", true)); |
| 941 EXPECT_EQ(1, owner_->GetDictionaryCountForTesting()); | 942 EXPECT_EQ(1, owner_->GetDictionaryCountForTesting()); |
| 942 EXPECT_TRUE(owner_->HasDictionaryFromURLForTesting(url0)); | 943 EXPECT_TRUE(owner_->HasDictionaryFromURLForTesting(url0)); |
| 943 EXPECT_FALSE(owner_->HasDictionaryFromURLForTesting(url1)); | 944 EXPECT_FALSE(owner_->HasDictionaryFromURLForTesting(url1)); |
| 944 } | 945 } |
| 945 | 946 |
| 946 TEST_F(SdchOwnerPersistenceTest, UsingDictionaryUpdatesUseCount) { | 947 TEST_F(SdchOwnerPersistenceTest, UsingDictionaryUpdatesUseCount) { |
| 947 const GURL url("http://www.example.com/dict"); | 948 const GURL url("http://www.example.com/dict"); |
| 948 | 949 |
| 949 scoped_ptr<TestPrefStorage> storage(new TestPrefStorage(true)); | 950 std::unique_ptr<TestPrefStorage> storage(new TestPrefStorage(true)); |
| 950 TestPrefStorage* old_storage = storage.get(); // Save storage pointer. | 951 TestPrefStorage* old_storage = storage.get(); // Save storage pointer. |
| 951 ResetOwner(std::move(storage)); // Takes ownership of storage pointer. | 952 ResetOwner(std::move(storage)); // Takes ownership of storage pointer. |
| 952 InsertDictionaryForURL(url, "0"); | 953 InsertDictionaryForURL(url, "0"); |
| 953 | 954 |
| 954 std::string hash; | 955 std::string hash; |
| 955 int old_count; | 956 int old_count; |
| 956 storage.reset(new TestPrefStorage(*old_storage)); | 957 storage.reset(new TestPrefStorage(*old_storage)); |
| 957 { | 958 { |
| 958 ClearOwner(); | 959 ClearOwner(); |
| 959 base::DictionaryValue* dict = nullptr; | 960 base::DictionaryValue* dict = nullptr; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 973 ASSERT_TRUE(dict->GetInteger("use_count", &new_count)); | 974 ASSERT_TRUE(dict->GetInteger("use_count", &new_count)); |
| 974 } | 975 } |
| 975 | 976 |
| 976 EXPECT_EQ(old_count + 1, new_count); | 977 EXPECT_EQ(old_count + 1, new_count); |
| 977 } | 978 } |
| 978 | 979 |
| 979 TEST_F(SdchOwnerPersistenceTest, LoadingDictionaryMerges) { | 980 TEST_F(SdchOwnerPersistenceTest, LoadingDictionaryMerges) { |
| 980 const GURL url0("http://www.example.com/dict0"); | 981 const GURL url0("http://www.example.com/dict0"); |
| 981 const GURL url1("http://www.example.com/dict1"); | 982 const GURL url1("http://www.example.com/dict1"); |
| 982 | 983 |
| 983 scoped_ptr<TestPrefStorage> storage(new TestPrefStorage(true)); | 984 std::unique_ptr<TestPrefStorage> storage(new TestPrefStorage(true)); |
| 984 TestPrefStorage* old_storage = storage.get(); // Save storage pointer. | 985 TestPrefStorage* old_storage = storage.get(); // Save storage pointer. |
| 985 ResetOwner(std::move(storage)); // Takes ownership of storage pointer. | 986 ResetOwner(std::move(storage)); // Takes ownership of storage pointer. |
| 986 InsertDictionaryForURL(url1, "1"); | 987 InsertDictionaryForURL(url1, "1"); |
| 987 | 988 |
| 988 storage.reset(new TestPrefStorage(*old_storage)); | 989 storage.reset(new TestPrefStorage(*old_storage)); |
| 989 ResetOwner(scoped_ptr<SdchOwner::PrefStorage>()); | 990 ResetOwner(std::unique_ptr<SdchOwner::PrefStorage>()); |
| 990 InsertDictionaryForURL(url0, "0"); | 991 InsertDictionaryForURL(url0, "0"); |
| 991 EXPECT_EQ(1, owner_->GetDictionaryCountForTesting()); | 992 EXPECT_EQ(1, owner_->GetDictionaryCountForTesting()); |
| 992 owner_->EnablePersistentStorage(std::move(storage)); | 993 owner_->EnablePersistentStorage(std::move(storage)); |
| 993 ASSERT_TRUE(CompleteLoadFromURL(url1, "1", true)); | 994 ASSERT_TRUE(CompleteLoadFromURL(url1, "1", true)); |
| 994 EXPECT_EQ(2, owner_->GetDictionaryCountForTesting()); | 995 EXPECT_EQ(2, owner_->GetDictionaryCountForTesting()); |
| 995 } | 996 } |
| 996 | 997 |
| 997 TEST_F(SdchOwnerPersistenceTest, PersistenceMetrics) { | 998 TEST_F(SdchOwnerPersistenceTest, PersistenceMetrics) { |
| 998 const GURL url0("http://www.example.com/dict0"); | 999 const GURL url0("http://www.example.com/dict0"); |
| 999 const GURL url1("http://www.example.com/dict1"); | 1000 const GURL url1("http://www.example.com/dict1"); |
| 1000 | 1001 |
| 1001 scoped_ptr<TestPrefStorage> storage(new TestPrefStorage(true)); | 1002 std::unique_ptr<TestPrefStorage> storage(new TestPrefStorage(true)); |
| 1002 TestPrefStorage* old_storage = storage.get(); // Save storage pointer. | 1003 TestPrefStorage* old_storage = storage.get(); // Save storage pointer. |
| 1003 ResetOwner(std::move(storage)); // Takes ownership of storage pointer. | 1004 ResetOwner(std::move(storage)); // Takes ownership of storage pointer. |
| 1004 | 1005 |
| 1005 InsertDictionaryForURL(url0, "0"); | 1006 InsertDictionaryForURL(url0, "0"); |
| 1006 InsertDictionaryForURL(url1, "1"); | 1007 InsertDictionaryForURL(url1, "1"); |
| 1007 | 1008 |
| 1008 storage.reset(new TestPrefStorage(*old_storage)); | 1009 storage.reset(new TestPrefStorage(*old_storage)); |
| 1009 ResetOwner(std::move(storage)); | 1010 ResetOwner(std::move(storage)); |
| 1010 | 1011 |
| 1011 base::HistogramTester tester; | 1012 base::HistogramTester tester; |
| 1012 | 1013 |
| 1013 EXPECT_TRUE(CompleteLoadFromURL(url0, "0", true)); | 1014 EXPECT_TRUE(CompleteLoadFromURL(url0, "0", true)); |
| 1014 EXPECT_TRUE(CompleteLoadFromURL(url1, "1", false)); | 1015 EXPECT_TRUE(CompleteLoadFromURL(url1, "1", false)); |
| 1015 | 1016 |
| 1016 tester.ExpectTotalCount("Sdch3.NetworkBytesSpent", 1); | 1017 tester.ExpectTotalCount("Sdch3.NetworkBytesSpent", 1); |
| 1017 tester.ExpectUniqueSample("Sdch3.NetworkBytesSpent", | 1018 tester.ExpectUniqueSample("Sdch3.NetworkBytesSpent", |
| 1018 CreateDictionary(url1, "1").size(), 1); | 1019 CreateDictionary(url1, "1").size(), 1); |
| 1019 } | 1020 } |
| 1020 | 1021 |
| 1021 } // namespace net | 1022 } // namespace net |
| OLD | NEW |