| 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" | 
| (...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 257     for (std::vector<PendingRequest>::iterator it = requests_.begin(); | 257     for (std::vector<PendingRequest>::iterator it = requests_.begin(); | 
| 258          it != requests_.end(); ++it) { | 258          it != requests_.end(); ++it) { | 
| 259       if (it->url_ == dictionary_url) | 259       if (it->url_ == dictionary_url) | 
| 260         return true; | 260         return true; | 
| 261     } | 261     } | 
| 262     return false; | 262     return false; | 
| 263   } | 263   } | 
| 264 | 264 | 
| 265   bool CompletePendingRequest(const GURL& dictionary_url, | 265   bool CompletePendingRequest(const GURL& dictionary_url, | 
| 266                               const std::string& dictionary_text, | 266                               const std::string& dictionary_text, | 
| 267                               const BoundNetLog& net_log, | 267                               const NetLogWithSource& net_log, | 
| 268                               bool was_from_cache) { | 268                               bool was_from_cache) { | 
| 269     for (std::vector<PendingRequest>::iterator it = requests_.begin(); | 269     for (std::vector<PendingRequest>::iterator it = requests_.begin(); | 
| 270          it != requests_.end(); ++it) { | 270          it != requests_.end(); ++it) { | 
| 271       if (it->url_ == dictionary_url) { | 271       if (it->url_ == dictionary_url) { | 
| 272         it->callback_.Run(dictionary_text, dictionary_url, net_log, | 272         it->callback_.Run(dictionary_text, dictionary_url, net_log, | 
| 273                           was_from_cache); | 273                           was_from_cache); | 
| 274         requests_.erase(it); | 274         requests_.erase(it); | 
| 275         return true; | 275         return true; | 
| 276       } | 276       } | 
| 277     } | 277     } | 
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 317     // which leaves the test in control of signals to SdchOwner. | 317     // which leaves the test in control of signals to SdchOwner. | 
| 318     url_request_context_.set_job_factory(&job_factory_); | 318     url_request_context_.set_job_factory(&job_factory_); | 
| 319 | 319 | 
| 320     // Reduce sizes to reduce time for string operations. | 320     // Reduce sizes to reduce time for string operations. | 
| 321     sdch_owner_->SetMaxTotalDictionarySize(kMaxSizeForTesting); | 321     sdch_owner_->SetMaxTotalDictionarySize(kMaxSizeForTesting); | 
| 322     sdch_owner_->SetMinSpaceForDictionaryFetch(kMinFetchSpaceForTesting); | 322     sdch_owner_->SetMinSpaceForDictionaryFetch(kMinFetchSpaceForTesting); | 
| 323   } | 323   } | 
| 324 | 324 | 
| 325   SdchManager& sdch_manager() { return sdch_manager_; } | 325   SdchManager& sdch_manager() { return sdch_manager_; } | 
| 326   SdchOwner& sdch_owner() { return *(sdch_owner_.get()); } | 326   SdchOwner& sdch_owner() { return *(sdch_owner_.get()); } | 
| 327   BoundNetLog& bound_net_log() { return net_log_; } | 327   NetLogWithSource& net_log() { return net_log_; } | 
| 328 | 328 | 
| 329   int JobsRecentlyCreated() { | 329   int JobsRecentlyCreated() { | 
| 330     int result = error_jobs_created - last_jobs_created_; | 330     int result = error_jobs_created - last_jobs_created_; | 
| 331     last_jobs_created_ = error_jobs_created; | 331     last_jobs_created_ = error_jobs_created; | 
| 332     return result; | 332     return result; | 
| 333   } | 333   } | 
| 334 | 334 | 
| 335   bool DictionaryPresentInManager(const std::string& server_hash) { | 335   bool DictionaryPresentInManager(const std::string& server_hash) { | 
| 336     // Presumes all tests use generic url. | 336     // Presumes all tests use generic url. | 
| 337     SdchProblemCode tmp; | 337     SdchProblemCode tmp; | 
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 390     return CreateAndAddDictionary(size, last_used_time, base::Time(), | 390     return CreateAndAddDictionary(size, last_used_time, base::Time(), | 
| 391                                   server_hash_p); | 391                                   server_hash_p); | 
| 392   } | 392   } | 
| 393 | 393 | 
| 394   void ResetOwner() { | 394   void ResetOwner() { | 
| 395     sdch_owner_.reset(new SdchOwner(&sdch_manager_, &url_request_context_)); | 395     sdch_owner_.reset(new SdchOwner(&sdch_manager_, &url_request_context_)); | 
| 396   } | 396   } | 
| 397 | 397 | 
| 398  private: | 398  private: | 
| 399   int last_jobs_created_; | 399   int last_jobs_created_; | 
| 400   BoundNetLog net_log_; | 400   NetLogWithSource net_log_; | 
| 401   int dictionary_creation_index_; | 401   int dictionary_creation_index_; | 
| 402 | 402 | 
| 403   // The dependencies of these objects (sdch_owner_ -> {sdch_manager_, | 403   // The dependencies of these objects (sdch_owner_ -> {sdch_manager_, | 
| 404   // url_request_context_}, url_request_context_->job_factory_) require | 404   // url_request_context_}, url_request_context_->job_factory_) require | 
| 405   // this order for correct destruction semantics. | 405   // this order for correct destruction semantics. | 
| 406   MockURLRequestJobFactory job_factory_; | 406   MockURLRequestJobFactory job_factory_; | 
| 407   URLRequestContext url_request_context_; | 407   URLRequestContext url_request_context_; | 
| 408   SdchManager sdch_manager_; | 408   SdchManager sdch_manager_; | 
| 409   std::unique_ptr<SdchOwner> sdch_owner_; | 409   std::unique_ptr<SdchOwner> sdch_owner_; | 
| 410 | 410 | 
| 411   DISALLOW_COPY_AND_ASSIGN(SdchOwnerTest); | 411   DISALLOW_COPY_AND_ASSIGN(SdchOwnerTest); | 
| 412 }; | 412 }; | 
| 413 | 413 | 
| 414 // 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 | 
| 415 // when there's not? | 415 // when there's not? | 
| 416 TEST_F(SdchOwnerTest, OnGetDictionary_Fetching) { | 416 TEST_F(SdchOwnerTest, OnGetDictionary_Fetching) { | 
| 417   GURL request_url(std::string(generic_url) + "/r1"); | 417   GURL request_url(std::string(generic_url) + "/r1"); | 
| 418 | 418 | 
| 419   // Fetch generated when empty. | 419   // Fetch generated when empty. | 
| 420   GURL dict_url1(std::string(generic_url) + "/d1"); | 420   GURL dict_url1(std::string(generic_url) + "/d1"); | 
| 421   EXPECT_EQ(0, JobsRecentlyCreated()); | 421   EXPECT_EQ(0, JobsRecentlyCreated()); | 
| 422   SignalGetDictionaryAndClearJobs(request_url, dict_url1); | 422   SignalGetDictionaryAndClearJobs(request_url, dict_url1); | 
| 423   EXPECT_EQ(1, JobsRecentlyCreated()); | 423   EXPECT_EQ(1, JobsRecentlyCreated()); | 
| 424 | 424 | 
| 425   // Fetch generated when half full. | 425   // Fetch generated when half full. | 
| 426   GURL dict_url2(std::string(generic_url) + "/d2"); | 426   GURL dict_url2(std::string(generic_url) + "/d2"); | 
| 427   std::string dictionary1(NewSdchDictionary(kMaxSizeForTesting / 2)); | 427   std::string dictionary1(NewSdchDictionary(kMaxSizeForTesting / 2)); | 
| 428   sdch_owner().OnDictionaryFetched(base::Time::Now(), base::Time::Now(), 1, | 428   sdch_owner().OnDictionaryFetched(base::Time::Now(), base::Time::Now(), 1, | 
| 429                                    dictionary1, dict_url1, bound_net_log(), | 429                                    dictionary1, dict_url1, net_log(), false); | 
| 430                                    false); |  | 
| 431   EXPECT_EQ(0, JobsRecentlyCreated()); | 430   EXPECT_EQ(0, JobsRecentlyCreated()); | 
| 432   SignalGetDictionaryAndClearJobs(request_url, dict_url2); | 431   SignalGetDictionaryAndClearJobs(request_url, dict_url2); | 
| 433   EXPECT_EQ(1, JobsRecentlyCreated()); | 432   EXPECT_EQ(1, JobsRecentlyCreated()); | 
| 434 | 433 | 
| 435   // Fetch not generated when close to completely full. | 434   // Fetch not generated when close to completely full. | 
| 436   GURL dict_url3(std::string(generic_url) + "/d3"); | 435   GURL dict_url3(std::string(generic_url) + "/d3"); | 
| 437   std::string dictionary2(NewSdchDictionary( | 436   std::string dictionary2(NewSdchDictionary( | 
| 438       (kMaxSizeForTesting / 2 - kMinFetchSpaceForTesting / 2))); | 437       (kMaxSizeForTesting / 2 - kMinFetchSpaceForTesting / 2))); | 
| 439   sdch_owner().OnDictionaryFetched(base::Time::Now(), base::Time::Now(), 1, | 438   sdch_owner().OnDictionaryFetched(base::Time::Now(), base::Time::Now(), 1, | 
| 440                                    dictionary2, dict_url2, bound_net_log(), | 439                                    dictionary2, dict_url2, net_log(), false); | 
| 441                                    false); |  | 
| 442   EXPECT_EQ(0, JobsRecentlyCreated()); | 440   EXPECT_EQ(0, JobsRecentlyCreated()); | 
| 443   SignalGetDictionaryAndClearJobs(request_url, dict_url3); | 441   SignalGetDictionaryAndClearJobs(request_url, dict_url3); | 
| 444   EXPECT_EQ(0, JobsRecentlyCreated()); | 442   EXPECT_EQ(0, JobsRecentlyCreated()); | 
| 445 } | 443 } | 
| 446 | 444 | 
| 447 // Make sure attempts to add dictionaries do what they should. | 445 // Make sure attempts to add dictionaries do what they should. | 
| 448 TEST_F(SdchOwnerTest, OnDictionaryFetched_Fetching) { | 446 TEST_F(SdchOwnerTest, OnDictionaryFetched_Fetching) { | 
| 449   GURL request_url(std::string(generic_url) + "/r1"); | 447   GURL request_url(std::string(generic_url) + "/r1"); | 
| 450   std::string client_hash; | 448   std::string client_hash; | 
| 451   std::string server_hash; | 449   std::string server_hash; | 
| (...skipping 383 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 835     std::string dict; | 833     std::string dict; | 
| 836     dict.append("Domain: "); | 834     dict.append("Domain: "); | 
| 837     dict.append(url.host()); | 835     dict.append(url.host()); | 
| 838     dict.append("\n\n"); | 836     dict.append("\n\n"); | 
| 839     dict.append(url.spec()); | 837     dict.append(url.spec()); | 
| 840     dict.append(nonce); | 838     dict.append(nonce); | 
| 841     return dict; | 839     return dict; | 
| 842   } | 840   } | 
| 843 | 841 | 
| 844  protected: | 842  protected: | 
| 845   BoundNetLog net_log_; | 843   NetLogWithSource net_log_; | 
| 846   std::unique_ptr<SdchManager> manager_; | 844   std::unique_ptr<SdchManager> manager_; | 
| 847   MockSdchDictionaryFetcher* fetcher_; | 845   MockSdchDictionaryFetcher* fetcher_; | 
| 848   std::unique_ptr<SdchOwner> owner_; | 846   std::unique_ptr<SdchOwner> owner_; | 
| 849   TestURLRequestContext url_request_context_; | 847   TestURLRequestContext url_request_context_; | 
| 850 }; | 848 }; | 
| 851 | 849 | 
| 852 // Test an empty persistence store. | 850 // Test an empty persistence store. | 
| 853 TEST_F(SdchOwnerPersistenceTest, Empty) { | 851 TEST_F(SdchOwnerPersistenceTest, Empty) { | 
| 854   ResetOwner(base::WrapUnique(new TestPrefStorage(true))); | 852   ResetOwner(base::WrapUnique(new TestPrefStorage(true))); | 
| 855   EXPECT_EQ(0, owner_->GetDictionaryCountForTesting()); | 853   EXPECT_EQ(0, owner_->GetDictionaryCountForTesting()); | 
| (...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 1013 | 1011 | 
| 1014   EXPECT_TRUE(CompleteLoadFromURL(url0, "0", true)); | 1012   EXPECT_TRUE(CompleteLoadFromURL(url0, "0", true)); | 
| 1015   EXPECT_TRUE(CompleteLoadFromURL(url1, "1", false)); | 1013   EXPECT_TRUE(CompleteLoadFromURL(url1, "1", false)); | 
| 1016 | 1014 | 
| 1017   tester.ExpectTotalCount("Sdch3.NetworkBytesSpent", 1); | 1015   tester.ExpectTotalCount("Sdch3.NetworkBytesSpent", 1); | 
| 1018   tester.ExpectUniqueSample("Sdch3.NetworkBytesSpent", | 1016   tester.ExpectUniqueSample("Sdch3.NetworkBytesSpent", | 
| 1019                             CreateDictionary(url1, "1").size(), 1); | 1017                             CreateDictionary(url1, "1").size(), 1); | 
| 1020 } | 1018 } | 
| 1021 | 1019 | 
| 1022 }  // namespace net | 1020 }  // namespace net | 
| OLD | NEW | 
|---|