Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/net/predictor_tab_helper.h" | 5 #include "chrome/browser/net/predictor_tab_helper.h" |
| 6 | 6 |
| 7 #include "base/feature_list.h" | 7 #include "base/feature_list.h" |
| 8 #include "base/trace_event/trace_event.h" | |
| 9 #include "chrome/browser/net/prediction_options.h" | |
| 8 #include "chrome/browser/net/predictor.h" | 10 #include "chrome/browser/net/predictor.h" |
| 9 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
| 10 #include "chrome/common/url_constants.h" | 12 #include "chrome/common/url_constants.h" |
| 13 #include "content/public/browser/browser_thread.h" | |
| 11 #include "content/public/browser/navigation_handle.h" | 14 #include "content/public/browser/navigation_handle.h" |
| 15 #include "content/public/browser/render_process_host.h" | |
| 16 #include "content/public/browser/service_worker_context.h" | |
| 17 #include "content/public/browser/storage_partition.h" | |
| 12 #include "content/public/common/browser_side_navigation_policy.h" | 18 #include "content/public/common/browser_side_navigation_policy.h" |
| 19 #include "content/public/common/content_features.h" | |
| 13 | 20 |
| 14 #if defined(OS_CHROMEOS) | 21 #if defined(OS_CHROMEOS) |
| 15 #include "chrome/browser/chromeos/profiles/profile_helper.h" | 22 #include "chrome/browser/chromeos/profiles/profile_helper.h" |
| 16 #endif // defined(OS_CHROMEOS) | 23 #endif // defined(OS_CHROMEOS) |
| 17 | 24 |
| 18 DEFINE_WEB_CONTENTS_USER_DATA_KEY(chrome_browser_net::PredictorTabHelper); | 25 DEFINE_WEB_CONTENTS_USER_DATA_KEY(chrome_browser_net::PredictorTabHelper); |
| 19 | 26 |
| 20 namespace chrome_browser_net { | 27 namespace chrome_browser_net { |
| 21 | 28 |
| 22 namespace { | 29 namespace { |
| 23 | 30 |
| 24 // Triggers the preconnector on renderer-initiated navigations. This captures | 31 // Triggers the preconnector on renderer-initiated navigations. This captures |
| 25 // more navigations. | 32 // more navigations. |
| 26 const base::Feature kPreconnectMore{"PreconnectMore", | 33 const base::Feature kPreconnectMore{"PreconnectMore", |
| 27 base::FEATURE_DISABLED_BY_DEFAULT}; | 34 base::FEATURE_DISABLED_BY_DEFAULT}; |
| 28 | 35 |
| 29 } // namespace | 36 } // namespace |
| 30 | 37 |
| 31 PredictorTabHelper::PredictorTabHelper(content::WebContents* web_contents) | 38 PredictorTabHelper::PredictorTabHelper(content::WebContents* web_contents) |
| 32 : content::WebContentsObserver(web_contents), | 39 : content::WebContentsObserver(web_contents), |
| 33 predicted_from_pending_entry_(false) { | 40 predicted_from_pending_entry_(false), |
| 34 } | 41 weak_factory_(this) {} |
| 35 | 42 |
| 36 PredictorTabHelper::~PredictorTabHelper() { | 43 PredictorTabHelper::~PredictorTabHelper() { |
| 37 } | 44 } |
| 38 | 45 |
| 39 void PredictorTabHelper::DidStartNavigation( | 46 void PredictorTabHelper::DidStartNavigation( |
| 40 content::NavigationHandle* navigation_handle) { | 47 content::NavigationHandle* navigation_handle) { |
| 48 TRACE_EVENT1("net", "PredictorTabHelper::DidStartNavigation", "url", | |
| 49 navigation_handle->GetURL().spec()); | |
| 41 if (!base::FeatureList::IsEnabled(kPreconnectMore) && | 50 if (!base::FeatureList::IsEnabled(kPreconnectMore) && |
| 42 (!content::IsBrowserSideNavigationEnabled() || | 51 (!content::IsBrowserSideNavigationEnabled() || |
| 43 navigation_handle->IsRendererInitiated())) | 52 navigation_handle->IsRendererInitiated())) |
| 44 return; | 53 return; |
| 45 // Subframe navigations are handled in WitnessURLRequest. | 54 // Subframe navigations are handled in WitnessURLRequest. |
| 46 if (!navigation_handle->IsInMainFrame()) | 55 if (!navigation_handle->IsInMainFrame()) |
| 47 return; | 56 return; |
| 48 if (predicted_from_pending_entry_) { | 57 if (predicted_from_pending_entry_) { |
| 49 predicted_from_pending_entry_ = false; | 58 predicted_from_pending_entry_ = false; |
| 50 return; | 59 return; |
| 51 } | 60 } |
| 52 PreconnectUrl(navigation_handle->GetURL()); | 61 HandleNavigation(navigation_handle->GetURL()); |
| 53 } | 62 } |
| 54 | 63 |
| 55 void PredictorTabHelper::DidStartNavigationToPendingEntry( | 64 void PredictorTabHelper::DidStartNavigationToPendingEntry( |
| 56 const GURL& url, | 65 const GURL& url, |
| 57 content::ReloadType reload_type) { | 66 content::ReloadType reload_type) { |
| 67 TRACE_EVENT1("net", "PredictorTabHelper::DidStartNavigationToPendingEntry", | |
| 68 "url", url.spec()); | |
| 58 // This method isn't needed with PlzNavigate (see comment in header for | 69 // This method isn't needed with PlzNavigate (see comment in header for |
| 59 // predicted_from_pending_entry_) | 70 // predicted_from_pending_entry_) |
| 60 if (content::IsBrowserSideNavigationEnabled()) | 71 if (content::IsBrowserSideNavigationEnabled()) |
| 61 return; | 72 return; |
| 62 | 73 |
| 63 // The standard way to preconnect based on navigation. | 74 // The standard way to preconnect based on navigation. |
| 64 PreconnectUrl(url); | 75 HandleNavigation(url); |
| 65 predicted_from_pending_entry_ = true; | 76 predicted_from_pending_entry_ = true; |
| 66 } | 77 } |
| 67 | 78 |
| 68 void PredictorTabHelper::DocumentOnLoadCompletedInMainFrame() { | 79 void PredictorTabHelper::DocumentOnLoadCompletedInMainFrame() { |
| 69 Profile* profile = | 80 Profile* profile = |
| 70 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); | 81 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); |
| 71 Predictor* predictor = profile->GetNetworkPredictor(); | 82 Predictor* predictor = profile->GetNetworkPredictor(); |
| 72 #if defined(OS_CHROMEOS) | 83 #if defined(OS_CHROMEOS) |
| 73 if (chromeos::ProfileHelper::IsSigninProfile(profile)) | 84 if (chromeos::ProfileHelper::IsSigninProfile(profile)) |
| 74 return; | 85 return; |
| 75 #endif | 86 #endif |
| 76 if (predictor) | 87 if (predictor) |
| 77 predictor->SaveStateForNextStartup(); | 88 predictor->SaveStateForNextStartup(); |
| 78 } | 89 } |
| 79 | 90 |
| 80 void PredictorTabHelper::PreconnectUrl(const GURL& url) { | 91 void PredictorTabHelper::HandleNavigation(const GURL& url) { |
| 92 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 93 TRACE_EVENT1("net", "PredictorTabHelper::HandleNavigation", "url", | |
| 94 url.spec()); | |
| 95 if (!url.SchemeIsHTTPOrHTTPS()) | |
| 96 return; | |
| 97 content::RenderProcessHost* host = web_contents()->GetRenderProcessHost(); | |
| 98 NetworkPredictionOptionsForServiceWorker options = | |
| 99 GetNetworkPredictionOptionsForServiceWorker(); | |
| 100 if (!host || | |
| 101 options == NetworkPredictionOptionsForServiceWorker::PRECONNECT_ONLY) { | |
| 102 PreconnectUrlWithPredictor(url); | |
| 103 return; | |
| 104 } | |
| 105 | |
| 106 host->GetStoragePartition() | |
| 107 ->GetServiceWorkerContext() | |
| 108 ->StartServiceWorkerForNavigationPreconnect( | |
| 109 url, host, | |
| 110 base::Bind( | |
| 111 &PredictorTabHelper::DidStartServiceWorkerForNavigaionPreconnect, | |
| 112 weak_factory_.GetWeakPtr(), url)); | |
| 113 if (options == NetworkPredictionOptionsForServiceWorker:: | |
| 114 START_SERVICE_WORKER_AND_PRECONNECT) { | |
| 115 PreconnectUrlWithPredictor(url); | |
| 116 } | |
| 117 } | |
| 118 | |
| 119 void PredictorTabHelper::DidStartServiceWorkerForNavigaionPreconnect( | |
|
falken
2017/06/01 06:54:17
Navigation
horo
2017/06/01 11:15:06
Done.
| |
| 120 const GURL& url, | |
| 121 bool success) { | |
| 122 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 123 TRACE_EVENT1("net", "PredictorTabHelper::DidStartServiceWorkerForPreconnect", | |
| 124 "success", success); | |
| 125 NetworkPredictionOptionsForServiceWorker options = | |
| 126 GetNetworkPredictionOptionsForServiceWorker(); | |
| 127 if ((!success && options != NetworkPredictionOptionsForServiceWorker:: | |
| 128 START_SERVICE_WORKER_AND_PRECONNECT) || | |
| 129 options == NetworkPredictionOptionsForServiceWorker:: | |
| 130 START_SERVICE_WORKER_AND_DEFER_PRECONNECT) { | |
|
falken
2017/06/01 06:54:17
I feel a switch would be more readable here, since
horo
2017/06/01 11:15:06
Done.
| |
| 131 PreconnectUrlWithPredictor(url); | |
| 132 } | |
| 133 } | |
| 134 | |
| 135 void PredictorTabHelper::PreconnectUrlWithPredictor(const GURL& url) { | |
|
falken
2017/06/01 06:54:17
I'm wondering why did the name change to WithPredi
horo
2017/06/01 11:15:06
Done.
| |
| 81 Profile* profile = | 136 Profile* profile = |
| 82 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); | 137 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); |
| 83 Predictor* predictor(profile->GetNetworkPredictor()); | 138 Predictor* predictor(profile->GetNetworkPredictor()); |
| 84 if (predictor && url.SchemeIsHTTPOrHTTPS()) | 139 if (!predictor) |
| 85 predictor->PreconnectUrlAndSubresources(url, GURL()); | 140 return; |
| 141 predictor->PreconnectUrlAndSubresources(url, GURL()); | |
| 86 } | 142 } |
| 87 | 143 |
| 88 } // namespace chrome_browser_net | 144 } // namespace chrome_browser_net |
| OLD | NEW |