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.
5 #include "chrome/browser/net/predictor_tab_helper.h"
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"
15 DEFINE_WEB_CONTENTS_USER_DATA_KEY(chrome_browser_net::PredictorTabHelper
);
17 using content::BrowserThread
;
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
;
35 namespace chrome_browser_net
{
37 PredictorTabHelper::PredictorTabHelper(content::WebContents
* web_contents
)
38 : content::WebContentsObserver(web_contents
) {
41 PredictorTabHelper::~PredictorTabHelper() {
44 void PredictorTabHelper::DidStartNavigationToPendingEntry(
46 content::NavigationController::ReloadType reload_type
) {
48 Profile::FromBrowserContext(web_contents()->GetBrowserContext());
49 chrome_browser_net::Predictor
* predictor
= profile
->GetNetworkPredictor();
52 if (url
.SchemeIs(content::kHttpScheme
) || url
.SchemeIs(content::kHttpsScheme
))
53 predictor
->PreconnectUrlAndSubresources(url
, GURL());
56 void PredictorTabHelper::DidNavigateMainFrame(
57 const content::LoadCommittedDetails
& details
,
58 const content::FrameNavigateParams
& params
) {
59 if (!IsUserLinkNavigationRequest(params
.transition
) ||
60 !(params
.url
.SchemeIsHTTPOrHTTPS()))
63 Profile
* profile
= Profile::FromBrowserContext(
64 web_contents()->GetBrowserContext());
65 Predictor
* predictor
= profile
->GetNetworkPredictor();
67 BrowserThread::PostTask(
70 base::Bind(&Predictor::RecordLinkNavigation
,
71 base::Unretained(predictor
),
76 } // namespace chrome_browser_net