| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "components/precache/core/precache_fetcher.h" | 5 #include "components/precache/core/precache_fetcher.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <cstring> | 9 #include <cstring> |
| 10 #include <memory> |
| 10 #include <set> | 11 #include <set> |
| 11 #include <string> | 12 #include <string> |
| 12 #include <vector> | 13 #include <vector> |
| 13 | 14 |
| 14 #include "base/bind.h" | 15 #include "base/bind.h" |
| 15 #include "base/callback.h" | 16 #include "base/callback.h" |
| 16 #include "base/command_line.h" | 17 #include "base/command_line.h" |
| 17 #include "base/compiler_specific.h" | 18 #include "base/compiler_specific.h" |
| 19 #include "base/memory/ptr_util.h" |
| 18 #include "base/memory/ref_counted.h" | 20 #include "base/memory/ref_counted.h" |
| 19 #include "base/memory/scoped_ptr.h" | |
| 20 #include "base/memory/weak_ptr.h" | 21 #include "base/memory/weak_ptr.h" |
| 21 #include "base/run_loop.h" | 22 #include "base/run_loop.h" |
| 22 #include "base/test/histogram_tester.h" | 23 #include "base/test/histogram_tester.h" |
| 23 #include "base/thread_task_runner_handle.h" | 24 #include "base/thread_task_runner_handle.h" |
| 24 #include "components/precache/core/precache_switches.h" | 25 #include "components/precache/core/precache_switches.h" |
| 25 #include "components/precache/core/proto/precache.pb.h" | 26 #include "components/precache/core/proto/precache.pb.h" |
| 26 #include "net/base/load_flags.h" | 27 #include "net/base/load_flags.h" |
| 27 #include "net/http/http_response_headers.h" | 28 #include "net/http/http_response_headers.h" |
| 28 #include "net/http/http_status_code.h" | 29 #include "net/http/http_status_code.h" |
| 29 #include "net/url_request/test_url_fetcher_factory.h" | 30 #include "net/url_request/test_url_fetcher_factory.h" |
| (...skipping 27 matching lines...) Expand all Loading... |
| 57 "http://custom-manifest-url-prefix.com/good-manifest.com"; | 58 "http://custom-manifest-url-prefix.com/good-manifest.com"; |
| 58 const char kResourceFetchFailureURL[] = "http://resource-fetch-failure.com"; | 59 const char kResourceFetchFailureURL[] = "http://resource-fetch-failure.com"; |
| 59 const char kGoodResourceURL[] = "http://good-resource.com"; | 60 const char kGoodResourceURL[] = "http://good-resource.com"; |
| 60 const char kForcedStartingURLManifestURL[] = | 61 const char kForcedStartingURLManifestURL[] = |
| 61 "http://manifest-url-prefix.com/forced-starting-url.com"; | 62 "http://manifest-url-prefix.com/forced-starting-url.com"; |
| 62 | 63 |
| 63 class TestURLFetcherCallback { | 64 class TestURLFetcherCallback { |
| 64 public: | 65 public: |
| 65 TestURLFetcherCallback() : total_response_bytes_(0) {} | 66 TestURLFetcherCallback() : total_response_bytes_(0) {} |
| 66 | 67 |
| 67 scoped_ptr<net::FakeURLFetcher> CreateURLFetcher( | 68 std::unique_ptr<net::FakeURLFetcher> CreateURLFetcher( |
| 68 const GURL& url, net::URLFetcherDelegate* delegate, | 69 const GURL& url, |
| 69 const std::string& response_data, net::HttpStatusCode response_code, | 70 net::URLFetcherDelegate* delegate, |
| 71 const std::string& response_data, |
| 72 net::HttpStatusCode response_code, |
| 70 net::URLRequestStatus::Status status) { | 73 net::URLRequestStatus::Status status) { |
| 71 scoped_ptr<net::FakeURLFetcher> fetcher(new net::FakeURLFetcher( | 74 std::unique_ptr<net::FakeURLFetcher> fetcher(new net::FakeURLFetcher( |
| 72 url, delegate, response_data, response_code, status)); | 75 url, delegate, response_data, response_code, status)); |
| 73 | 76 |
| 74 total_response_bytes_ += response_data.size(); | 77 total_response_bytes_ += response_data.size(); |
| 75 requested_urls_.insert(url); | 78 requested_urls_.insert(url); |
| 76 | 79 |
| 77 return fetcher; | 80 return fetcher; |
| 78 } | 81 } |
| 79 | 82 |
| 80 const std::multiset<GURL>& requested_urls() const { | 83 const std::multiset<GURL>& requested_urls() const { |
| 81 return requested_urls_; | 84 return requested_urls_; |
| (...skipping 22 matching lines...) Expand all Loading... |
| 104 }; | 107 }; |
| 105 | 108 |
| 106 class MockURLFetcherFactory : public net::URLFetcherFactory { | 109 class MockURLFetcherFactory : public net::URLFetcherFactory { |
| 107 public: | 110 public: |
| 108 typedef net::URLFetcher* DoURLFetcher( | 111 typedef net::URLFetcher* DoURLFetcher( |
| 109 int id, | 112 int id, |
| 110 const GURL& url, | 113 const GURL& url, |
| 111 net::URLFetcher::RequestType request_type, | 114 net::URLFetcher::RequestType request_type, |
| 112 net::URLFetcherDelegate* delegate); | 115 net::URLFetcherDelegate* delegate); |
| 113 | 116 |
| 114 scoped_ptr<net::URLFetcher> CreateURLFetcher( | 117 std::unique_ptr<net::URLFetcher> CreateURLFetcher( |
| 115 int id, | 118 int id, |
| 116 const GURL& url, | 119 const GURL& url, |
| 117 net::URLFetcher::RequestType request_type, | 120 net::URLFetcher::RequestType request_type, |
| 118 net::URLFetcherDelegate* delegate) override { | 121 net::URLFetcherDelegate* delegate) override { |
| 119 return make_scoped_ptr(DoCreateURLFetcher(id, url, request_type, delegate)); | 122 return base::WrapUnique( |
| 123 DoCreateURLFetcher(id, url, request_type, delegate)); |
| 120 } | 124 } |
| 121 | 125 |
| 122 // The method to mock out, instead of CreateURLFetcher. This is necessary | 126 // The method to mock out, instead of CreateURLFetcher. This is necessary |
| 123 // because gmock can't handle move-only types such as scoped_ptr. | 127 // because gmock can't handle move-only types such as scoped_ptr. |
| 124 MOCK_METHOD4(DoCreateURLFetcher, DoURLFetcher); | 128 MOCK_METHOD4(DoCreateURLFetcher, DoURLFetcher); |
| 125 | 129 |
| 126 // A fake successful response. When the action runs, it saves off a pointer to | 130 // A fake successful response. When the action runs, it saves off a pointer to |
| 127 // the FakeURLFetcher in its output parameter for later inspection. | 131 // the FakeURLFetcher in its output parameter for later inspection. |
| 128 testing::Action<DoURLFetcher> RespondWith(const std::string& body, | 132 testing::Action<DoURLFetcher> RespondWith(const std::string& body, |
| 129 net::FakeURLFetcher** fetcher) { | 133 net::FakeURLFetcher** fetcher) { |
| (...skipping 585 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 715 | 719 |
| 716 // good-manifest.com will not have been completed. | 720 // good-manifest.com will not have been completed. |
| 717 EXPECT_THAT(histogram.GetAllSamples("Precache.Fetch.PercentCompleted"), | 721 EXPECT_THAT(histogram.GetAllSamples("Precache.Fetch.PercentCompleted"), |
| 718 ElementsAre(base::Bucket(0, 1))); | 722 ElementsAre(base::Bucket(0, 1))); |
| 719 histogram.ExpectTotalCount("Precache.Fetch.TimeToComplete", 1); | 723 histogram.ExpectTotalCount("Precache.Fetch.TimeToComplete", 1); |
| 720 } | 724 } |
| 721 | 725 |
| 722 } // namespace | 726 } // namespace |
| 723 | 727 |
| 724 } // namespace precache | 728 } // namespace precache |
| OLD | NEW |