Home | History | Annotate | Download | only in net
      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.
      4 
      5 #include "chrome/browser/net/connect_interceptor.h"
      6 
      7 #include "chrome/browser/net/predictor.h"
      8 #include "net/base/load_flags.h"
      9 #include "net/url_request/url_request.h"
     10 
     11 namespace chrome_browser_net {
     12 
     13 ConnectInterceptor::ConnectInterceptor(Predictor* predictor)
     14     : timed_cache_(base::TimeDelta::FromSeconds(
     15           Predictor::kMaxUnusedSocketLifetimeSecondsWithoutAGet)),
     16       predictor_(predictor) {
     17   DCHECK(predictor);
     18 }
     19 
     20 ConnectInterceptor::~ConnectInterceptor() {
     21 }
     22 
     23 void ConnectInterceptor::WitnessURLRequest(net::URLRequest* request) {
     24   GURL request_scheme_host(Predictor::CanonicalizeUrl(request->url()));
     25   if (request_scheme_host == GURL::EmptyGURL())
     26     return;
     27 
     28   // Learn what URLs are likely to be needed during next startup.
     29   predictor_->LearnAboutInitialNavigation(request_scheme_host);
     30 
     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
     44         // significant path.
     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,
     53                                           request_scheme_host);
     54         }
     55       }
     56     }
     57   } else {
     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,
     62                                       request_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.
     69       return;
     70     }
     71   }
     72   timed_cache_.SetRecentlySeen(request_scheme_host);
     73 
     74   predictor_->RecordPreconnectNavigationStat(request->url_chain(),
     75                                              is_subresource);
     76 
     77   // Subresources for main frames usually get predicted when we detected the
     78   // main frame request - way back in RenderViewHost::Navigate.  So only handle
     79   // predictions now for subresources or for redirected hosts.
     80   if ((request->load_flags() & net::LOAD_SUB_FRAME) || redirected_host)
     81     predictor_->PredictFrameSubresources(request_scheme_host,
     82                                          request->first_party_for_cookies());
     83   return;
     84 }
     85 
     86 }  // namespace chrome_browser_net
     87