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

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: Use existing switches and fix runloop 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"
17 #include "content/public/common/content_switches.h"
13 #include "content/public/test/test_utils.h" 18 #include "content/public/test/test_utils.h"
19 #include "net/base/host_port_pair.h"
14 #include "net/base/net_errors.h" 20 #include "net/base/net_errors.h"
21 #include "net/base/net_log.h"
15 #include "net/dns/host_resolver_proc.h" 22 #include "net/dns/host_resolver_proc.h"
16 #include "net/dns/mock_host_resolver.h" 23 #include "net/dns/mock_host_resolver.h"
17 #include "testing/gmock/include/gmock/gmock.h" 24 #include "testing/gmock/include/gmock/gmock.h"
18 25
19 using content::BrowserThread; 26 using content::BrowserThread;
20 using testing::HasSubstr; 27 using testing::HasSubstr;
21 28
22 namespace { 29 namespace {
23 30
31 const char kBlinkPreconnectFeature[] = "LinkPreconnect";
24 const char kChromiumHostname[] = "chromium.org"; 32 const char kChromiumHostname[] = "chromium.org";
25 33
26 // Records a history of all hostnames for which resolving has been requested, 34 // Records a history of all hostnames for which resolving has been requested,
27 // and immediately fails the resolution requests themselves. 35 // and immediately fails the resolution requests themselves.
28 class HostResolutionRequestRecorder : public net::HostResolverProc { 36 class HostResolutionRequestRecorder : public net::HostResolverProc {
29 public: 37 public:
30 HostResolutionRequestRecorder() 38 HostResolutionRequestRecorder()
31 : HostResolverProc(NULL), 39 : HostResolverProc(NULL),
32 is_waiting_for_hostname_(false) { 40 is_waiting_for_hostname_(false) {
33 } 41 }
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
84 // requested and thus is running a nested message loop. 92 // requested and thus is running a nested message loop.
85 bool is_waiting_for_hostname_; 93 bool is_waiting_for_hostname_;
86 94
87 // A list of hostnames for which resolution has already been requested. Only 95 // A list of hostnames for which resolution has already been requested. Only
88 // to be accessed from the UI thread. 96 // to be accessed from the UI thread.
89 std::vector<std::string> requested_hostnames_; 97 std::vector<std::string> requested_hostnames_;
90 98
91 DISALLOW_COPY_AND_ASSIGN(HostResolutionRequestRecorder); 99 DISALLOW_COPY_AND_ASSIGN(HostResolutionRequestRecorder);
92 }; 100 };
93 101
102 // Watches the NetLog event stream for a connect event to the provided
103 // host:port pair.
104 class ConnectNetLogObserver : public net::NetLog::ThreadSafeObserver {
105 public:
106 explicit ConnectNetLogObserver(const std::string& host_port_pair)
107 : host_port_pair_(host_port_pair) {
108 }
109
110 ~ConnectNetLogObserver() override {
111 }
112
113 void Attach() {
114 g_browser_process->net_log()->AddThreadSafeObserver(
115 this, net::NetLog::LOG_ALL_BUT_BYTES);
116 }
117
118 void Detach() {
119 if (net_log())
120 net_log()->RemoveThreadSafeObserver(this);
121 }
122
123 void WaitForConnect() {
124 run_loop_.Run();
125 }
126
127 private:
128 void OnAddEntry(const net::NetLog::Entry& entry) override {
129 scoped_ptr<base::Value> param_value(entry.ParametersToValue());
130 base::DictionaryValue* param_dict = NULL;
131 std::string group_name;
132
133 if (entry.source().type == net::NetLog::SOURCE_CONNECT_JOB &&
134 param_value.get() != NULL &&
135 param_value->GetAsDictionary(&param_dict) &&
136 param_dict != NULL &&
137 param_dict->GetString("group_name", &group_name) &&
138 host_port_pair_ == group_name) {
139 run_loop_.Quit();
140 }
141 }
142
143 base::RunLoop run_loop_;
144 const std::string host_port_pair_;
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(
169 switches::kEnableExperimentalWebPlatformFeatures);
170 command_line->AppendSwitchASCII(
171 switches::kEnableBlinkFeatures, kBlinkPreconnectFeature);
172 }
173
114 void TearDownInProcessBrowserTestFixture() override { 174 void TearDownInProcessBrowserTestFixture() override {
115 InProcessBrowserTest::TearDownInProcessBrowserTestFixture(); 175 InProcessBrowserTest::TearDownInProcessBrowserTestFixture();
116 scoped_host_resolver_proc_.reset(); 176 scoped_host_resolver_proc_.reset();
117 } 177 }
118 178
119 void LearnAboutInitialNavigation(const GURL& url) { 179 void LearnAboutInitialNavigation(const GURL& url) {
120 Predictor* predictor = browser()->profile()->GetNetworkPredictor(); 180 Predictor* predictor = browser()->profile()->GetNetworkPredictor();
121 BrowserThread::PostTask(BrowserThread::IO, 181 BrowserThread::PostTask(BrowserThread::IO,
122 FROM_HERE, 182 FROM_HERE,
123 base::Bind(&Predictor::LearnAboutInitialNavigation, 183 base::Bind(&Predictor::LearnAboutInitialNavigation,
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
193 } 253 }
194 254
195 IN_PROC_BROWSER_TEST_F(PredictorBrowserTest, DnsPrefetch) { 255 IN_PROC_BROWSER_TEST_F(PredictorBrowserTest, DnsPrefetch) {
196 ASSERT_TRUE(test_server()->Start()); 256 ASSERT_TRUE(test_server()->Start());
197 ui_test_utils::NavigateToURL( 257 ui_test_utils::NavigateToURL(
198 browser(), 258 browser(),
199 GURL(test_server()->GetURL("files/predictor/dns_prefetch.html"))); 259 GURL(test_server()->GetURL("files/predictor/dns_prefetch.html")));
200 WaitUntilHostHasBeenRequested(kChromiumHostname); 260 WaitUntilHostHasBeenRequested(kChromiumHostname);
201 } 261 }
202 262
263 IN_PROC_BROWSER_TEST_F(PredictorBrowserTest, Preconnect) {
264 ASSERT_TRUE(test_server()->Start());
265
266 // Create a HTML preconnect reference to the local server in the form
267 // <link rel="preconnect" href="http://test-server/">
268 // and navigate to it as a data URI.
269 GURL preconnect_url = test_server()->GetURL("");
270 std::string preconnect_content =
271 "<link rel=\"preconnect\" href=\"" + preconnect_url.spec() + "\">";
272 std::string encoded;
273 base::Base64Encode(preconnect_content, &encoded);
274 std::string data_uri = "data:text/html;base64," + encoded;
275
276 net::HostPortPair host_port_pair = net::HostPortPair::FromURL(preconnect_url);
277 ConnectNetLogObserver net_log_observer(host_port_pair.ToString());
278 net_log_observer.Attach();
279
280 ui_test_utils::NavigateToURL(browser(), GURL(data_uri));
281
282 net_log_observer.WaitForConnect();
283 net_log_observer.Detach();
284 }
285
203 } // namespace chrome_browser_net 286 } // namespace chrome_browser_net
204 287
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