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

Side by Side Diff: chrome/browser/net/predictor_browsertest.cc

Issue 922533003: Eliminated the logic that accumulated multiple preconnect requests (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed review feedback Created 5 years, 10 months 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
« no previous file with comments | « no previous file | components/network_hints/renderer/renderer_preconnect.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "base/base64.h"
6 #include "base/command_line.h"
5 #include "base/json/json_string_value_serializer.h" 7 #include "base/json/json_string_value_serializer.h"
6 #include "base/prefs/pref_service.h" 8 #include "base/prefs/pref_service.h"
9 #include "chrome/browser/browser_process.h"
10 #include "chrome/browser/net/chrome_net_log.h"
7 #include "chrome/browser/net/predictor.h" 11 #include "chrome/browser/net/predictor.h"
8 #include "chrome/browser/profiles/profile.h" 12 #include "chrome/browser/profiles/profile.h"
9 #include "chrome/browser/ui/browser.h" 13 #include "chrome/browser/ui/browser.h"
10 #include "chrome/common/pref_names.h" 14 #include "chrome/common/pref_names.h"
11 #include "chrome/test/base/in_process_browser_test.h" 15 #include "chrome/test/base/in_process_browser_test.h"
12 #include "chrome/test/base/ui_test_utils.h" 16 #include "chrome/test/base/ui_test_utils.h"
13 #include "content/public/test/test_utils.h" 17 #include "content/public/test/test_utils.h"
18 #include "net/base/host_port_pair.h"
14 #include "net/base/net_errors.h" 19 #include "net/base/net_errors.h"
20 #include "net/base/net_log.h"
15 #include "net/dns/host_resolver_proc.h" 21 #include "net/dns/host_resolver_proc.h"
16 #include "net/dns/mock_host_resolver.h" 22 #include "net/dns/mock_host_resolver.h"
17 #include "testing/gmock/include/gmock/gmock.h" 23 #include "testing/gmock/include/gmock/gmock.h"
18 24
19 using content::BrowserThread; 25 using content::BrowserThread;
20 using testing::HasSubstr; 26 using testing::HasSubstr;
21 27
22 namespace { 28 namespace {
23 29
30 const char kEnablePreconnect[] = "--enable-blink-features=LinkPreconnect";
31 const char kEnableExperimentalWebPlatformFeatures[] =
32 "--enable-experimental-web-platform-features";
mmenke 2015/02/17 16:53:33 Should get these strings from the header (public/c
Pat Meenan 2015/02/17 17:38:08 Done.
24 const char kChromiumHostname[] = "chromium.org"; 33 const char kChromiumHostname[] = "chromium.org";
25 34
26 // Records a history of all hostnames for which resolving has been requested, 35 // Records a history of all hostnames for which resolving has been requested,
27 // and immediately fails the resolution requests themselves. 36 // and immediately fails the resolution requests themselves.
28 class HostResolutionRequestRecorder : public net::HostResolverProc { 37 class HostResolutionRequestRecorder : public net::HostResolverProc {
29 public: 38 public:
30 HostResolutionRequestRecorder() 39 HostResolutionRequestRecorder()
31 : HostResolverProc(NULL), 40 : HostResolverProc(NULL),
32 is_waiting_for_hostname_(false) { 41 is_waiting_for_hostname_(false) {
33 } 42 }
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
84 // requested and thus is running a nested message loop. 93 // requested and thus is running a nested message loop.
85 bool is_waiting_for_hostname_; 94 bool is_waiting_for_hostname_;
86 95
87 // A list of hostnames for which resolution has already been requested. Only 96 // A list of hostnames for which resolution has already been requested. Only
88 // to be accessed from the UI thread. 97 // to be accessed from the UI thread.
89 std::vector<std::string> requested_hostnames_; 98 std::vector<std::string> requested_hostnames_;
90 99
91 DISALLOW_COPY_AND_ASSIGN(HostResolutionRequestRecorder); 100 DISALLOW_COPY_AND_ASSIGN(HostResolutionRequestRecorder);
92 }; 101 };
93 102
103 // Watches the NetLog event stream for a connect event to the provided
104 // host:port pair.
105 class ConnectNetLogObserver : public net::NetLog::ThreadSafeObserver {
106 public:
107 explicit ConnectNetLogObserver(const std::string& host_port_pair)
108 : host_port_pair_(host_port_pair),
109 saw_connect_event_(false) {
110 }
111
112 ~ConnectNetLogObserver() override {
113 }
114
115 void Attach() {
116 g_browser_process->net_log()->AddThreadSafeObserver(
117 this, net::NetLog::LOG_ALL_BUT_BYTES);
118 }
119
120 void Detach() {
121 if (net_log())
122 net_log()->RemoveThreadSafeObserver(this);
123 }
124
125 void WaitForConnect() {
126 if (!saw_connect_event_)
mmenke 2015/02/17 16:53:33 Per earlier comment, need to get rid of this. Run
Pat Meenan 2015/02/17 17:38:07 Done.
127 run_loop_.Run();
128 }
129
130 private:
131 void OnAddEntry(const net::NetLog::Entry& entry) override {
132 scoped_ptr<base::Value> param_value(entry.ParametersToValue());
133 base::DictionaryValue* param_dict = NULL;
134 std::string group_name;
135
136 if (entry.source().type == net::NetLog::SOURCE_CONNECT_JOB &&
137 param_value.get() != NULL &&
138 param_value->GetAsDictionary(&param_dict) &&
139 param_dict != NULL &&
140 param_dict->GetString("group_name", &group_name) &&
141 host_port_pair_ == group_name) {
142 saw_connect_event_ = true;
143 run_loop_.Quit();
144 }
145 }
146
147 base::RunLoop run_loop_;
148 const std::string host_port_pair_;
149 bool saw_connect_event_;
150 };
151
94 } // namespace 152 } // namespace
95 153
96 namespace chrome_browser_net { 154 namespace chrome_browser_net {
97 155
98 class PredictorBrowserTest : public InProcessBrowserTest { 156 class PredictorBrowserTest : public InProcessBrowserTest {
99 public: 157 public:
100 PredictorBrowserTest() 158 PredictorBrowserTest()
101 : startup_url_("http://host1:1"), 159 : startup_url_("http://host1:1"),
102 referring_url_("http://host2:1"), 160 referring_url_("http://host2:1"),
103 target_url_("http://host3:1"), 161 target_url_("http://host3:1"),
104 host_resolution_request_recorder_(new HostResolutionRequestRecorder) { 162 host_resolution_request_recorder_(new HostResolutionRequestRecorder) {
105 } 163 }
106 164
107 protected: 165 protected:
108 void SetUpInProcessBrowserTestFixture() override { 166 void SetUpInProcessBrowserTestFixture() override {
109 scoped_host_resolver_proc_.reset(new net::ScopedDefaultHostResolverProc( 167 scoped_host_resolver_proc_.reset(new net::ScopedDefaultHostResolverProc(
110 host_resolution_request_recorder_.get())); 168 host_resolution_request_recorder_.get()));
111 InProcessBrowserTest::SetUpInProcessBrowserTestFixture(); 169 InProcessBrowserTest::SetUpInProcessBrowserTestFixture();
112 } 170 }
113 171
172 void SetUpCommandLine(base::CommandLine* command_line) override {
173 command_line->AppendSwitch(kEnablePreconnect);
174 command_line->AppendSwitch(kEnableExperimentalWebPlatformFeatures);
175 }
176
114 void TearDownInProcessBrowserTestFixture() override { 177 void TearDownInProcessBrowserTestFixture() override {
115 InProcessBrowserTest::TearDownInProcessBrowserTestFixture(); 178 InProcessBrowserTest::TearDownInProcessBrowserTestFixture();
116 scoped_host_resolver_proc_.reset(); 179 scoped_host_resolver_proc_.reset();
117 } 180 }
118 181
119 void LearnAboutInitialNavigation(const GURL& url) { 182 void LearnAboutInitialNavigation(const GURL& url) {
120 Predictor* predictor = browser()->profile()->GetNetworkPredictor(); 183 Predictor* predictor = browser()->profile()->GetNetworkPredictor();
121 BrowserThread::PostTask(BrowserThread::IO, 184 BrowserThread::PostTask(BrowserThread::IO,
122 FROM_HERE, 185 FROM_HERE,
123 base::Bind(&Predictor::LearnAboutInitialNavigation, 186 base::Bind(&Predictor::LearnAboutInitialNavigation,
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
193 } 256 }
194 257
195 IN_PROC_BROWSER_TEST_F(PredictorBrowserTest, DnsPrefetch) { 258 IN_PROC_BROWSER_TEST_F(PredictorBrowserTest, DnsPrefetch) {
196 ASSERT_TRUE(test_server()->Start()); 259 ASSERT_TRUE(test_server()->Start());
197 ui_test_utils::NavigateToURL( 260 ui_test_utils::NavigateToURL(
198 browser(), 261 browser(),
199 GURL(test_server()->GetURL("files/predictor/dns_prefetch.html"))); 262 GURL(test_server()->GetURL("files/predictor/dns_prefetch.html")));
200 WaitUntilHostHasBeenRequested(kChromiumHostname); 263 WaitUntilHostHasBeenRequested(kChromiumHostname);
201 } 264 }
202 265
266 IN_PROC_BROWSER_TEST_F(PredictorBrowserTest, Preconnect) {
267 ASSERT_TRUE(test_server()->Start());
268
269 // Create a HTML preconnect reference to the local server in the form
270 // <link rel="preconnect" href="http://test-server/">
271 // and navigate to it as a data URI.
272 GURL preconnect_url = test_server()->GetURL("");
273 std::string preconnect_content =
274 "<link rel=\"preconnect\" href=\"" + preconnect_url.spec() + "\">";
275 std::string encoded;
276 base::Base64Encode(preconnect_content, &encoded);
277 std::string data_uri = "data:text/html;base64," + encoded;
278
279 net::HostPortPair host_port_pair = net::HostPortPair::FromURL(preconnect_url);
280 ConnectNetLogObserver net_log_observer(host_port_pair.ToString());
281 net_log_observer.Attach();
282
283 ui_test_utils::NavigateToURL(browser(), GURL(data_uri));
284
285 net_log_observer.WaitForConnect();
286 net_log_observer.Detach();
287 }
288
203 } // namespace chrome_browser_net 289 } // namespace chrome_browser_net
204 290
OLDNEW
« no previous file with comments | « no previous file | components/network_hints/renderer/renderer_preconnect.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698