Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(207)

Side by Side Diff: chrome/browser/prerender/prerender_test_utils.cc

Issue 2540203002: NoStatePrefetch: Add a test to verify request priority (Closed)
Patch Set: removed include url_request_filter.h Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "chrome/browser/prerender/prerender_test_utils.h" 5 #include "chrome/browser/prerender/prerender_test_utils.h"
6 6
7 #include <memory> 7 #include <memory>
8 #include <set> 8 #include <set>
9 #include <string> 9 #include <string>
10 #include <utility> 10 #include <utility>
11 #include <vector> 11 #include <vector>
12 12
13 #include "base/callback.h"
13 #include "base/command_line.h" 14 #include "base/command_line.h"
14 #include "base/strings/string_split.h" 15 #include "base/strings/string_split.h"
15 #include "base/strings/utf_string_conversions.h" 16 #include "base/strings/utf_string_conversions.h"
16 #include "chrome/browser/loader/chrome_resource_dispatcher_host_delegate.h" 17 #include "chrome/browser/loader/chrome_resource_dispatcher_host_delegate.h"
17 #include "chrome/browser/prerender/prerender_manager.h" 18 #include "chrome/browser/prerender/prerender_manager.h"
18 #include "chrome/browser/prerender/prerender_manager_factory.h" 19 #include "chrome/browser/prerender/prerender_manager_factory.h"
19 #include "chrome/browser/profiles/profile.h" 20 #include "chrome/browser/profiles/profile.h"
20 #include "chrome/browser/safe_browsing/local_database_manager.h" 21 #include "chrome/browser/safe_browsing/local_database_manager.h"
21 #include "chrome/browser/ui/browser.h" 22 #include "chrome/browser/ui/browser.h"
22 #include "chrome/browser/ui/tabs/tab_strip_model.h" 23 #include "chrome/browser/ui/tabs/tab_strip_model.h"
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
98 content::BrowserThread::UI, FROM_HERE, 99 content::BrowserThread::UI, FROM_HERE,
99 base::Bind(&RequestCounter::RequestStarted, counter_)); 100 base::Bind(&RequestCounter::RequestStarted, counter_));
100 } 101 }
101 102
102 private: 103 private:
103 base::FilePath file_; 104 base::FilePath file_;
104 base::WeakPtr<RequestCounter> counter_; 105 base::WeakPtr<RequestCounter> counter_;
105 mutable base::WeakPtrFactory<CountingInterceptor> weak_factory_; 106 mutable base::WeakPtrFactory<CountingInterceptor> weak_factory_;
106 }; 107 };
107 108
108 // URLRequestInterceptor which asserts that the request is prefetch only. Pings 109 class CountingInterceptorWithCallback : public net::URLRequestInterceptor {
109 // |counter| after the flag is checked.
110 class PrefetchOnlyInterceptor : public net::URLRequestInterceptor {
111 public: 110 public:
112 explicit PrefetchOnlyInterceptor(const base::WeakPtr<RequestCounter>& counter) 111 // Inserts the interceptor object to intercept requests to |url|. Can be
113 : counter_(counter) {} 112 // called on any thread. Assumes that |counter| lives on the UI thread. The
114 ~PrefetchOnlyInterceptor() override {} 113 // |callback_io| will be called on IO thread with the net::URLrequest
114 // provided.
115 static void Initialize(const GURL& url,
116 RequestCounter* counter,
117 base::Callback<void(net::URLRequest*)> callback_io) {
118 content::BrowserThread::PostTask(
119 content::BrowserThread::IO, FROM_HERE,
120 base::Bind(&CountingInterceptorWithCallback::CreateAndAddOnIO, url,
121 counter->AsWeakPtr(), callback_io));
122 }
115 123
124 // net::URLRequestInterceptor:
116 net::URLRequestJob* MaybeInterceptRequest( 125 net::URLRequestJob* MaybeInterceptRequest(
117 net::URLRequest* request, 126 net::URLRequest* request,
118 net::NetworkDelegate* network_delegate) const override { 127 net::NetworkDelegate* network_delegate) const override {
119 EXPECT_TRUE(request->load_flags() & net::LOAD_PREFETCH); 128 // Run the callback.
129 callback_.Run(request);
130
131 // Ping the request counter.
120 content::BrowserThread::PostTask( 132 content::BrowserThread::PostTask(
121 content::BrowserThread::UI, FROM_HERE, 133 content::BrowserThread::UI, FROM_HERE,
122 base::Bind(&RequestCounter::RequestStarted, counter_)); 134 base::Bind(&RequestCounter::RequestStarted, counter_));
123 return nullptr; 135 return nullptr;
124 } 136 }
125 137
126 private: 138 private:
139 CountingInterceptorWithCallback(
140 const base::WeakPtr<RequestCounter>& counter,
141 base::Callback<void(net::URLRequest*)> callback)
142 : callback_(callback), counter_(counter) {}
143
144 static void CreateAndAddOnIO(
145 const GURL& url,
146 const base::WeakPtr<RequestCounter>& counter,
147 base::Callback<void(net::URLRequest*)> callback_io) {
148 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
149 // Create the object with base::WrapUnique to restrict access to the
150 // constructor.
151 net::URLRequestFilter::GetInstance()->AddUrlInterceptor(
152 url, base::WrapUnique(
153 new CountingInterceptorWithCallback(counter, callback_io)));
154 }
155
156 base::Callback<void(net::URLRequest*)> callback_;
127 base::WeakPtr<RequestCounter> counter_; 157 base::WeakPtr<RequestCounter> counter_;
158
159 DISALLOW_COPY_AND_ASSIGN(CountingInterceptorWithCallback);
128 }; 160 };
129 161
130 // URLRequestJob (and associated handler) which hangs. 162 // URLRequestJob (and associated handler) which hangs.
131 class HangingURLRequestJob : public net::URLRequestJob { 163 class HangingURLRequestJob : public net::URLRequestJob {
132 public: 164 public:
133 HangingURLRequestJob(net::URLRequest* request, 165 HangingURLRequestJob(net::URLRequest* request,
134 net::NetworkDelegate* network_delegate) 166 net::NetworkDelegate* network_delegate)
135 : net::URLRequestJob(request, network_delegate) { 167 : net::URLRequestJob(request, network_delegate) {
136 } 168 }
137 169
(...skipping 576 matching lines...) Expand 10 before | Expand all | Expand 10 after
714 746
715 void CreateCountingInterceptorOnIO( 747 void CreateCountingInterceptorOnIO(
716 const GURL& url, 748 const GURL& url,
717 const base::FilePath& file, 749 const base::FilePath& file,
718 const base::WeakPtr<RequestCounter>& counter) { 750 const base::WeakPtr<RequestCounter>& counter) {
719 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); 751 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
720 net::URLRequestFilter::GetInstance()->AddUrlInterceptor( 752 net::URLRequestFilter::GetInstance()->AddUrlInterceptor(
721 url, base::MakeUnique<CountingInterceptor>(file, counter)); 753 url, base::MakeUnique<CountingInterceptor>(file, counter));
722 } 754 }
723 755
724 void CreatePrefetchOnlyInterceptorOnIO( 756 void InterceptRequestAndCount(
725 const GURL& url, 757 const GURL& url,
726 const base::WeakPtr<RequestCounter>& counter) { 758 RequestCounter* counter,
727 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); 759 base::Callback<void(net::URLRequest*)> callback_io) {
728 net::URLRequestFilter::GetInstance()->AddUrlInterceptor( 760 CountingInterceptorWithCallback::Initialize(url, counter, callback_io);
729 url, base::MakeUnique<PrefetchOnlyInterceptor>(counter));
730 } 761 }
731 762
732 void CreateMockInterceptorOnIO(const GURL& url, const base::FilePath& file) { 763 void CreateMockInterceptorOnIO(const GURL& url, const base::FilePath& file) {
733 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); 764 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
734 net::URLRequestFilter::GetInstance()->AddUrlInterceptor( 765 net::URLRequestFilter::GetInstance()->AddUrlInterceptor(
735 url, net::URLRequestMockHTTPJob::CreateInterceptorForSingleFile( 766 url, net::URLRequestMockHTTPJob::CreateInterceptorForSingleFile(
736 file, content::BrowserThread::GetBlockingPool())); 767 file, content::BrowserThread::GetBlockingPool()));
737 } 768 }
738 769
739 void CreateHangingFirstRequestInterceptorOnIO( 770 void CreateHangingFirstRequestInterceptorOnIO(
740 const GURL& url, const base::FilePath& file, base::Closure callback) { 771 const GURL& url, const base::FilePath& file, base::Closure callback) {
741 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 772 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
742 std::unique_ptr<net::URLRequestInterceptor> interceptor( 773 std::unique_ptr<net::URLRequestInterceptor> interceptor(
743 new HangingFirstRequestInterceptor(file, callback)); 774 new HangingFirstRequestInterceptor(file, callback));
744 net::URLRequestFilter::GetInstance()->AddUrlInterceptor( 775 net::URLRequestFilter::GetInstance()->AddUrlInterceptor(
745 url, std::move(interceptor)); 776 url, std::move(interceptor));
746 } 777 }
747 778
748 } // namespace test_utils 779 } // namespace test_utils
749 780
750 } // namespace prerender 781 } // namespace prerender
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698