Popular sites on the NTP: re-download popular suggestions once per Chrome run
[chromium-blink-merge.git] / chrome / browser / net / connect_interceptor.cc
blob40bf61ce876094ae785fc5df95dd1ce7ec75706c
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 "content/public/browser/resource_request_info.h"
9 #include "content/public/common/resource_type.h"
10 #include "net/base/load_flags.h"
11 #include "net/url_request/url_request.h"
13 namespace chrome_browser_net {
15 ConnectInterceptor::ConnectInterceptor(Predictor* predictor)
16 : timed_cache_(base::TimeDelta::FromSeconds(
17 Predictor::kMaxUnusedSocketLifetimeSecondsWithoutAGet)),
18 predictor_(predictor) {
19 DCHECK(predictor);
22 ConnectInterceptor::~ConnectInterceptor() {
25 void ConnectInterceptor::WitnessURLRequest(net::URLRequest* request) {
26 GURL request_scheme_host(Predictor::CanonicalizeUrl(request->url()));
27 if (request_scheme_host == GURL::EmptyGURL())
28 return;
30 const content::ResourceRequestInfo* info =
31 content::ResourceRequestInfo::ForRequest(request);
32 bool is_main_frame = false;
33 bool is_sub_frame = false;
34 // TODO(mmenke): Should the predictor really be fed requests without a
35 // ResourceRequestInfo?
36 if (info) {
37 content::ResourceType resource_type = info->GetResourceType();
38 is_main_frame = (resource_type == content::RESOURCE_TYPE_MAIN_FRAME);
39 is_sub_frame = (resource_type == content::RESOURCE_TYPE_SUB_FRAME);
42 // Learn what URLs are likely to be needed during next startup.
43 predictor_->LearnAboutInitialNavigation(request_scheme_host);
45 bool redirected_host = false;
46 if (request->referrer().empty()) {
47 if (request->url() != request->original_url()) {
48 // This request was completed with a redirect.
49 GURL original_scheme_host(request->original_url().GetWithEmptyPath());
50 if (request_scheme_host != original_scheme_host) {
51 redirected_host = true;
52 // Don't learn from redirects that take path as an argument, but do
53 // learn from short-hand typing entries, such as "cnn.com" redirects to
54 // "www.cnn.com". We can't just check for has_path(), as a mere "/"
55 // will count as a path, so we check that the path is at most a "/"
56 // (1 character long) to decide the redirect is "definitive" and has no
57 // significant path.
58 // TODO(jar): It may be ok to learn from all redirects, as the adaptive
59 // system will not respond until several identical redirects have taken
60 // place. Hence a use of a path (that changes) wouldn't really be
61 // learned from anyway.
62 if (request->original_url().path().length() <= 1 &&
63 timed_cache_.WasRecentlySeen(original_scheme_host)) {
64 // TODO(jar): These definite redirects could be learned much faster.
65 predictor_->LearnFromNavigation(original_scheme_host,
66 request_scheme_host);
70 } else {
71 GURL referring_scheme_host = GURL(request->referrer()).GetWithEmptyPath();
72 // Learn about our referring URL, for use in the future.
73 if (!is_main_frame && timed_cache_.WasRecentlySeen(referring_scheme_host)) {
74 predictor_->LearnFromNavigation(referring_scheme_host,
75 request_scheme_host);
77 if (referring_scheme_host == request_scheme_host) {
78 // We've already made any/all predictions when we navigated to the
79 // referring host, so we can bail out here.
80 // We don't update the RecentlySeen() time because any preconnections
81 // need to be made at the first navigation (i.e., when referer was loaded)
82 // and wouldn't have waited for this current request navigation.
83 return;
86 timed_cache_.SetRecentlySeen(request_scheme_host);
88 // Subresources for main frames usually get predicted when we detected the
89 // main frame request - way back in RenderViewHost::Navigate. So only handle
90 // predictions now for subresources or for redirected hosts.
91 if (is_sub_frame || redirected_host) {
92 predictor_->PredictFrameSubresources(request_scheme_host,
93 request->first_party_for_cookies());
95 return;
98 } // namespace chrome_browser_net