1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/net/connect_interceptor.h"
7 #include "chrome/browser/net/predictor.h"
8 #include "net/base/load_flags.h"
9 #include "net/url_request/url_request.h"
11 namespace chrome_browser_net
{
13 ConnectInterceptor::ConnectInterceptor(Predictor
* predictor
)
14 : timed_cache_(base::TimeDelta::FromSeconds(
15 Predictor::kMaxUnusedSocketLifetimeSecondsWithoutAGet
)),
16 predictor_(predictor
) {
20 ConnectInterceptor::~ConnectInterceptor() {
23 void ConnectInterceptor::WitnessURLRequest(net::URLRequest
* request
) {
24 GURL
request_scheme_host(Predictor::CanonicalizeUrl(request
->url()));
25 if (request_scheme_host
== GURL::EmptyGURL())
28 // Learn what URLs are likely to be needed during next startup.
29 predictor_
->LearnAboutInitialNavigation(request_scheme_host
);
31 bool redirected_host
= false;
32 bool is_subresource
= !(request
->load_flags() & net::LOAD_MAIN_FRAME
);
33 if (request
->referrer().empty()) {
34 if (request
->url() != request
->original_url()) {
35 // This request was completed with a redirect.
36 GURL
original_scheme_host(request
->original_url().GetWithEmptyPath());
37 if (request_scheme_host
!= original_scheme_host
) {
38 redirected_host
= true;
39 // Don't learn from redirects that take path as an argument, but do
40 // learn from short-hand typing entries, such as "cnn.com" redirects to
41 // "www.cnn.com". We can't just check for has_path(), as a mere "/"
42 // will count as a path, so we check that the path is at most a "/"
43 // (1 character long) to decide the redirect is "definitive" and has no
45 // TODO(jar): It may be ok to learn from all redirects, as the adaptive
46 // system will not respond until several identical redirects have taken
47 // place. Hence a use of a path (that changes) wouldn't really be
48 // learned from anyway.
49 if (request
->original_url().path().length() <= 1 &&
50 timed_cache_
.WasRecentlySeen(original_scheme_host
)) {
51 // TODO(jar): These definite redirects could be learned much faster.
52 predictor_
->LearnFromNavigation(original_scheme_host
,
58 GURL referring_scheme_host
= GURL(request
->referrer()).GetWithEmptyPath();
59 // Learn about our referring URL, for use in the future.
60 if (is_subresource
&& timed_cache_
.WasRecentlySeen(referring_scheme_host
))
61 predictor_
->LearnFromNavigation(referring_scheme_host
,
63 if (referring_scheme_host
== request_scheme_host
) {
64 // We've already made any/all predictions when we navigated to the
65 // referring host, so we can bail out here.
66 // We don't update the RecentlySeen() time because any preconnections
67 // need to be made at the first navigation (i.e., when referer was loaded)
68 // and wouldn't have waited for this current request navigation.
72 timed_cache_
.SetRecentlySeen(request_scheme_host
);
74 // Subresources for main frames usually get predicted when we detected the
75 // main frame request - way back in RenderViewHost::Navigate. So only handle
76 // predictions now for subresources or for redirected hosts.
77 if ((request
->load_flags() & net::LOAD_SUB_FRAME
) || redirected_host
)
78 predictor_
->PredictFrameSubresources(request_scheme_host
,
79 request
->first_party_for_cookies());
83 } // namespace chrome_browser_net