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

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: Switched to custom NetLog observer 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";
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)
mmenke 2015/02/13 22:49:09 nit: const std::String&
Pat Meenan 2015/02/14 00:42:56 Done.
108 : host_port_pair_(host_port_pair),
mmenke 2015/02/13 22:49:08 nit: +2 indent
Pat Meenan 2015/02/14 00:42:56 Done.
109 saw_connect_event_(false) {
mmenke 2015/02/13 22:49:08 nit: +4 indent
Pat Meenan 2015/02/14 00:42:56 Done.
110 g_browser_process->net_log()->AddThreadSafeObserver(
111 this, net::NetLog::LOG_ALL_BUT_BYTES);
mmenke 2015/02/13 22:49:08 Adding during the constructor and removing during
Pat Meenan 2015/02/14 00:42:56 Done.
112 }
113
114 ~ConnectNetLogObserver() override {
115 if (net_log())
116 net_log()->RemoveThreadSafeObserver(this);
117 }
118
119 void WaitForConnect() {
120 if (!saw_connect_event_)
mmenke 2015/02/13 22:49:08 This isn't needed. Run loop can be quit before it
Pat Meenan 2015/02/14 00:42:56 Done.
121 run_loop_.Run();
122 }
123
124 private:
125 void OnAddEntry(const net::NetLog::Entry& entry) override {
126 scoped_ptr<base::Value> param_value(entry.ParametersToValue());
127 base::DictionaryValue* param_dict = NULL;
128 std::string group_name;
129
130 if (entry.source().type == net::NetLog::SOURCE_CONNECT_JOB &&
131 param_value.get() != NULL &&
132 param_value->GetAsDictionary(&param_dict) &&
133 param_dict != NULL &&
134 param_dict->GetString("group_name", &group_name) &&
135 host_port_pair_ == group_name) {
136 saw_connect_event_ = true;
137 if (run_loop_.running())
138 run_loop_.Quit();
139 }
140 }
141
142 base::RunLoop run_loop_;
143 const std::string host_port_pair_;
144 bool saw_connect_event_;
145 };
146
94 } // namespace 147 } // namespace
95 148
96 namespace chrome_browser_net { 149 namespace chrome_browser_net {
97 150
98 class PredictorBrowserTest : public InProcessBrowserTest { 151 class PredictorBrowserTest : public InProcessBrowserTest {
99 public: 152 public:
100 PredictorBrowserTest() 153 PredictorBrowserTest()
101 : startup_url_("http://host1:1"), 154 : startup_url_("http://host1:1"),
102 referring_url_("http://host2:1"), 155 referring_url_("http://host2:1"),
103 target_url_("http://host3:1"), 156 target_url_("http://host3:1"),
104 host_resolution_request_recorder_(new HostResolutionRequestRecorder) { 157 host_resolution_request_recorder_(new HostResolutionRequestRecorder) {
105 } 158 }
106 159
107 protected: 160 protected:
108 void SetUpInProcessBrowserTestFixture() override { 161 void SetUpInProcessBrowserTestFixture() override {
109 scoped_host_resolver_proc_.reset(new net::ScopedDefaultHostResolverProc( 162 scoped_host_resolver_proc_.reset(new net::ScopedDefaultHostResolverProc(
110 host_resolution_request_recorder_.get())); 163 host_resolution_request_recorder_.get()));
111 InProcessBrowserTest::SetUpInProcessBrowserTestFixture(); 164 InProcessBrowserTest::SetUpInProcessBrowserTestFixture();
112 } 165 }
113 166
167 void SetUpCommandLine(base::CommandLine* command_line) override {
168 command_line->AppendSwitch(kEnablePreconnect);
169 command_line->AppendSwitch(kEnableExperimentalWebPlatformFeatures);
170 }
171
114 void TearDownInProcessBrowserTestFixture() override { 172 void TearDownInProcessBrowserTestFixture() override {
115 InProcessBrowserTest::TearDownInProcessBrowserTestFixture(); 173 InProcessBrowserTest::TearDownInProcessBrowserTestFixture();
116 scoped_host_resolver_proc_.reset(); 174 scoped_host_resolver_proc_.reset();
117 } 175 }
118 176
119 void LearnAboutInitialNavigation(const GURL& url) { 177 void LearnAboutInitialNavigation(const GURL& url) {
120 Predictor* predictor = browser()->profile()->GetNetworkPredictor(); 178 Predictor* predictor = browser()->profile()->GetNetworkPredictor();
121 BrowserThread::PostTask(BrowserThread::IO, 179 BrowserThread::PostTask(BrowserThread::IO,
122 FROM_HERE, 180 FROM_HERE,
123 base::Bind(&Predictor::LearnAboutInitialNavigation, 181 base::Bind(&Predictor::LearnAboutInitialNavigation,
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
193 } 251 }
194 252
195 IN_PROC_BROWSER_TEST_F(PredictorBrowserTest, DnsPrefetch) { 253 IN_PROC_BROWSER_TEST_F(PredictorBrowserTest, DnsPrefetch) {
196 ASSERT_TRUE(test_server()->Start()); 254 ASSERT_TRUE(test_server()->Start());
197 ui_test_utils::NavigateToURL( 255 ui_test_utils::NavigateToURL(
198 browser(), 256 browser(),
199 GURL(test_server()->GetURL("files/predictor/dns_prefetch.html"))); 257 GURL(test_server()->GetURL("files/predictor/dns_prefetch.html")));
200 WaitUntilHostHasBeenRequested(kChromiumHostname); 258 WaitUntilHostHasBeenRequested(kChromiumHostname);
201 } 259 }
202 260
261 IN_PROC_BROWSER_TEST_F(PredictorBrowserTest, Preconnect) {
262 ASSERT_TRUE(test_server()->Start());
263
264 // Create a HTML preconnect reference to the local server in the form
265 // <link rel="preconnect" href="http://test-server/">
266 // and navigate to it as a data URI.
267 GURL preconnect_url = test_server()->GetURL("");
268 std::string preconnect_content =
269 "<link rel=\"preconnect\" href=\"" + preconnect_url.spec() + "\">";
270 std::string encoded;
271 base::Base64Encode(preconnect_content, &encoded);
272 std::string data_uri = "data:text/html;base64," + encoded;
273
274 net::HostPortPair host_port_pair = net::HostPortPair::FromURL(preconnect_url);
275 ConnectNetLogObserver net_log_observer(host_port_pair.ToString());
276
277 ui_test_utils::NavigateToURL(browser(), GURL(data_uri));
278
279 net_log_observer.WaitForConnect();
280 }
281
203 } // namespace chrome_browser_net 282 } // namespace chrome_browser_net
204 283
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