Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / net / predictor_tab_helper.cc
1 // Copyright 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/predictor_tab_helper.h"
6
7 #include "base/command_line.h"
8 #include "chrome/browser/net/predictor.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/common/chrome_switches.h"
11 #include "chrome/common/url_constants.h"
12 #include "content/public/browser/browser_thread.h"
13 #include "content/public/common/frame_navigate_params.h"
14
15 DEFINE_WEB_CONTENTS_USER_DATA_KEY(chrome_browser_net::PredictorTabHelper);
16
17 using content::BrowserThread;
18
19 namespace {
20
21 bool IsUserLinkNavigationRequest(content::PageTransition page_transition) {
22   bool is_link = (content::PageTransitionStripQualifier(page_transition) ==
23                   content::PAGE_TRANSITION_LINK);
24   bool is_forward_back = (page_transition &
25                           content::PAGE_TRANSITION_FORWARD_BACK) != 0;
26   // Tracking navigation session including client-side redirect
27   // is currently not supported.
28   bool is_client_redirect = (page_transition &
29                              content::PAGE_TRANSITION_CLIENT_REDIRECT) != 0;
30   return is_link && !is_forward_back && !is_client_redirect;
31 }
32
33 }  // namespace
34
35 namespace chrome_browser_net {
36
37 PredictorTabHelper::PredictorTabHelper(content::WebContents* web_contents)
38     : content::WebContentsObserver(web_contents) {
39 }
40
41 PredictorTabHelper::~PredictorTabHelper() {
42 }
43
44 void PredictorTabHelper::DidStartNavigationToPendingEntry(
45     const GURL& url,
46     content::NavigationController::ReloadType reload_type) {
47   Profile* profile =
48       Profile::FromBrowserContext(web_contents()->GetBrowserContext());
49   chrome_browser_net::Predictor* predictor = profile->GetNetworkPredictor();
50   if (!predictor)
51     return;
52   if (url.SchemeIs(url::kHttpScheme) || url.SchemeIs(url::kHttpsScheme))
53     predictor->PreconnectUrlAndSubresources(url, GURL());
54 }
55
56 void PredictorTabHelper::DidNavigateMainFrame(
57     const content::LoadCommittedDetails& details,
58     const content::FrameNavigateParams& params) {
59   if (!IsUserLinkNavigationRequest(params.transition) ||
60       !(params.url.SchemeIsHTTPOrHTTPS()))
61     return;
62
63   Profile* profile = Profile::FromBrowserContext(
64       web_contents()->GetBrowserContext());
65   Predictor* predictor = profile->GetNetworkPredictor();
66   if (predictor) {
67     BrowserThread::PostTask(
68         BrowserThread::IO,
69         FROM_HERE,
70         base::Bind(&Predictor::RecordLinkNavigation,
71                    base::Unretained(predictor),
72                    params.url));
73   }
74 }
75
76 }  // namespace chrome_browser_net