Commit 900ceec6 authored by Lily Houghton's avatar Lily Houghton Committed by Commit Bot

Change the Predictor class use the Preconnect API from the NetworkService

Bug: 821027
Cq-Include-Trybots: luci.chromium.try:linux_mojo
Change-Id: I597a7ee5d5cd423474232dc3d996055b0962dc60
Reviewed-on: https://chromium-review.googlesource.com/1104800
Commit-Queue: Lily Houghton <lilyhoughton@chromium.org>
Reviewed-by: default avatarMatt Menke <mmenke@chromium.org>
Reviewed-by: default avatarHelen Li <xunjieli@chromium.org>
Cr-Commit-Position: refs/heads/master@{#574096}
parent 16c1139f
...@@ -33,6 +33,7 @@ ...@@ -33,6 +33,7 @@
#include "chrome/browser/io_thread.h" #include "chrome/browser/io_thread.h"
#include "chrome/browser/predictors/loading_predictor_config.h" #include "chrome/browser/predictors/loading_predictor_config.h"
#include "chrome/browser/prefs/session_startup_pref.h" #include "chrome/browser/prefs/session_startup_pref.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_io_data.h" #include "chrome/browser/profiles/profile_io_data.h"
#include "chrome/common/chrome_features.h" #include "chrome/common/chrome_features.h"
#include "chrome/common/chrome_switches.h" #include "chrome/common/chrome_switches.h"
...@@ -42,6 +43,7 @@ ...@@ -42,6 +43,7 @@
#include "components/prefs/scoped_user_pref_update.h" #include "components/prefs/scoped_user_pref_update.h"
#include "content/public/browser/browser_thread.h" #include "content/public/browser/browser_thread.h"
#include "content/public/browser/resource_hints.h" #include "content/public/browser/resource_hints.h"
#include "content/public/browser/storage_partition.h"
#include "net/base/address_list.h" #include "net/base/address_list.h"
#include "net/base/completion_callback.h" #include "net/base/completion_callback.h"
#include "net/base/host_port_pair.h" #include "net/base/host_port_pair.h"
...@@ -188,11 +190,13 @@ void Predictor::RegisterProfilePrefs( ...@@ -188,11 +190,13 @@ void Predictor::RegisterProfilePrefs(
void Predictor::InitNetworkPredictor(PrefService* user_prefs, void Predictor::InitNetworkPredictor(PrefService* user_prefs,
IOThread* io_thread, IOThread* io_thread,
net::URLRequestContextGetter* getter, net::URLRequestContextGetter* getter,
ProfileIOData* profile_io_data) { ProfileIOData* profile_io_data,
Profile* profile) {
DCHECK_CURRENTLY_ON(BrowserThread::UI); DCHECK_CURRENTLY_ON(BrowserThread::UI);
user_prefs_ = user_prefs; user_prefs_ = user_prefs;
url_request_context_getter_ = getter; url_request_context_getter_ = getter;
profile_ = profile;
// Gather the list of hostnames to prefetch on startup. // Gather the list of hostnames to prefetch on startup.
std::vector<GURL> urls = GetPredictedUrlListAtStartup(user_prefs); std::vector<GURL> urls = GetPredictedUrlListAtStartup(user_prefs);
...@@ -687,30 +691,6 @@ void Predictor::PreconnectUrl(const GURL& url, ...@@ -687,30 +691,6 @@ void Predictor::PreconnectUrl(const GURL& url,
} }
} }
void Predictor::PreconnectUrlOnIOThread(
const GURL& original_url,
const GURL& site_for_cookies,
UrlInfo::ResolutionMotivation motivation,
bool allow_credentials,
int count) {
// Skip the HSTS redirect.
GURL url = GetHSTSRedirectOnIOThread(original_url);
// TODO(csharrison): The observer should only be notified after the null check
// for the URLRequestContextGetter. The predictor tests should be fixed to
// allow for this, as they currently expect a callback with no getter.
if (observer_) {
observer_->OnPreconnectUrl(url, site_for_cookies, motivation, count);
}
net::URLRequestContextGetter* getter = url_request_context_getter_.get();
if (!getter)
return;
content::PreconnectUrl(getter, url, site_for_cookies, count,
allow_credentials);
}
void Predictor::PredictFrameSubresources(const GURL& url, void Predictor::PredictFrameSubresources(const GURL& url,
const GURL& site_for_cookies) { const GURL& site_for_cookies) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) || DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) ||
...@@ -759,6 +739,29 @@ enum SubresourceValue { ...@@ -759,6 +739,29 @@ enum SubresourceValue {
SUBRESOURCE_VALUE_MAX SUBRESOURCE_VALUE_MAX
}; };
void Predictor::PreconnectUrlOnIOThread(
const GURL& original_url,
const GURL& site_for_cookies,
UrlInfo::ResolutionMotivation motivation,
bool allow_credentials,
int count) {
// Skip the HSTS redirect.
GURL url = GetHSTSRedirectOnIOThread(original_url);
// TODO(csharrison): The observer should only be notified after the null check
// for the URLRequestContextGetter. The predictor tests should be fixed to
// allow for this, as they currently expect a callback with no getter.
if (observer_) {
observer_->OnPreconnectUrl(url, site_for_cookies, motivation, count);
}
BrowserThread::PostTask(
BrowserThread::UI, FROM_HERE,
base::BindOnce(&Predictor::PreconnectUrlOnUIThread,
ui_weak_factory_->GetWeakPtr(), url, site_for_cookies,
motivation, allow_credentials, count));
}
void Predictor::PrepareFrameSubresources(const GURL& original_url, void Predictor::PrepareFrameSubresources(const GURL& original_url,
const GURL& site_for_cookies) { const GURL& site_for_cookies) {
// Apply HSTS redirect early so it is taken into account when looking up // Apply HSTS redirect early so it is taken into account when looking up
...@@ -961,6 +964,26 @@ GURL Predictor::GetHSTSRedirectOnIOThread(const GURL& url) { ...@@ -961,6 +964,26 @@ GURL Predictor::GetHSTSRedirectOnIOThread(const GURL& url) {
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void Predictor::PreconnectUrlOnUIThread(
const GURL& url,
const GURL& site_for_cookies,
UrlInfo::ResolutionMotivation motivation,
bool allow_credentials,
int count) {
bool privacy_mode = false;
int load_flags = net::LOAD_NORMAL;
if (!allow_credentials) {
privacy_mode = true;
load_flags = net::LOAD_DO_NOT_SEND_COOKIES | net::LOAD_DO_NOT_SAVE_COOKIES |
net::LOAD_DO_NOT_SEND_AUTH_DATA;
}
content::BrowserContext::GetDefaultStoragePartition(profile_)
->GetNetworkContext()
->PreconnectSockets(count, url, load_flags, privacy_mode);
}
bool Predictor::PredictorEnabled() const { bool Predictor::PredictorEnabled() const {
base::AutoLock lock(predictor_enabled_lock_); base::AutoLock lock(predictor_enabled_lock_);
return predictor_enabled_; return predictor_enabled_;
...@@ -1065,11 +1088,11 @@ GURL Predictor::CanonicalizeUrl(const GURL& url) { ...@@ -1065,11 +1088,11 @@ GURL Predictor::CanonicalizeUrl(const GURL& url) {
return GURL(scheme + "://" + url.host() + colon_plus_port); return GURL(scheme + "://" + url.host() + colon_plus_port);
} }
void SimplePredictor::InitNetworkPredictor( void SimplePredictor::InitNetworkPredictor(PrefService* user_prefs,
PrefService* user_prefs, IOThread* io_thread,
IOThread* io_thread, net::URLRequestContextGetter* getter,
net::URLRequestContextGetter* getter, ProfileIOData* profile_io_data,
ProfileIOData* profile_io_data) { Profile* profile) {
// Empty function for unittests. // Empty function for unittests.
} }
......
...@@ -83,10 +83,11 @@ class PredictorObserver { ...@@ -83,10 +83,11 @@ class PredictorObserver {
// Predictor is constructed during Profile construction (on the UI thread), // Predictor is constructed during Profile construction (on the UI thread),
// but it is destroyed on the IO thread when ProfileIOData goes away. All of // but it is destroyed on the IO thread when ProfileIOData goes away. All of
// its core state and functionality happens on the IO thread. The only UI // its core state and functionality happens on the IO thread.
// methods are initialization / shutdown related (including preconnect // The only UI methods are initialization / shutdown related (including
// initialization), or convenience methods that internally forward calls to // preconnect initialization), convenience methods that internally forward
// the IO thread. // calls to the IO thread, or internal functions that interface with the Network
// Service.
class Predictor { class Predictor {
public: public:
// A version number for prefs that are saved. This should be incremented when // A version number for prefs that are saved. This should be incremented when
...@@ -141,7 +142,8 @@ class Predictor { ...@@ -141,7 +142,8 @@ class Predictor {
virtual void InitNetworkPredictor(PrefService* user_prefs, virtual void InitNetworkPredictor(PrefService* user_prefs,
IOThread* io_thread, IOThread* io_thread,
net::URLRequestContextGetter* getter, net::URLRequestContextGetter* getter,
ProfileIOData* profile_io_data); ProfileIOData* profile_io_data,
Profile* profile);
// The Omnibox has proposed a given url to the user, and if it is a search // The Omnibox has proposed a given url to the user, and if it is a search
// URL, then it also indicates that this is preconnectable (i.e., we could // URL, then it also indicates that this is preconnectable (i.e., we could
...@@ -234,24 +236,18 @@ class Predictor { ...@@ -234,24 +236,18 @@ class Predictor {
// Called from the UI thread in response to the load event. // Called from the UI thread in response to the load event.
void SaveStateForNextStartup(); void SaveStateForNextStartup();
// May be called from either the IO or UI thread and will PostTask // ------------- End IO thread methods.
// to the IO thread if necessary.
// The following methods may be called on either the IO or UI threads.
// Calls |PreconnectUrlOnIOThread()|, posting it to the IO thread if
// necessary.
void PreconnectUrl(const GURL& url, void PreconnectUrl(const GURL& url,
const GURL& site_for_cookies, const GURL& site_for_cookies,
UrlInfo::ResolutionMotivation motivation, UrlInfo::ResolutionMotivation motivation,
bool allow_credentials, bool allow_credentials,
int count); int count);
void PreconnectUrlOnIOThread(const GURL& url,
const GURL& site_for_cookies,
UrlInfo::ResolutionMotivation motivation,
bool allow_credentials,
int count);
// ------------- End IO thread methods.
// The following methods may be called on either the IO or UI threads.
// Instigate pre-connection to any URLs, or pre-resolution of related host, // Instigate pre-connection to any URLs, or pre-resolution of related host,
// that we predict will be needed after this navigation (typically // that we predict will be needed after this navigation (typically
// more-embedded resources on a page). This method will actually post a task // more-embedded resources on a page). This method will actually post a task
...@@ -358,6 +354,12 @@ class Predictor { ...@@ -358,6 +354,12 @@ class Predictor {
// ------------- Start IO thread methods. // ------------- Start IO thread methods.
void PreconnectUrlOnIOThread(const GURL& original_url,
const GURL& site_for_cookies,
UrlInfo::ResolutionMotivation motivation,
bool allow_credentials,
int count);
// Perform actual resolution or preconnection to subresources now. This is // Perform actual resolution or preconnection to subresources now. This is
// an internal worker method that is reached via a post task from // an internal worker method that is reached via a post task from
// PredictFrameSubresources(). // PredictFrameSubresources().
...@@ -404,6 +406,12 @@ class Predictor { ...@@ -404,6 +406,12 @@ class Predictor {
// ------------- End IO thread methods. // ------------- End IO thread methods.
void PreconnectUrlOnUIThread(const GURL& url,
const GURL& site_for_cookies,
UrlInfo::ResolutionMotivation motivation,
bool allow_credentials,
int count);
std::unique_ptr<InitialObserver> initial_observer_; std::unique_ptr<InitialObserver> initial_observer_;
// Reference to URLRequestContextGetter from the Profile which owns the // Reference to URLRequestContextGetter from the Profile which owns the
...@@ -425,6 +433,10 @@ class Predictor { ...@@ -425,6 +433,10 @@ class Predictor {
// CanPrefetchAndPrerenderIO and CanPreresolveAndPreconnectIO. // CanPrefetchAndPrerenderIO and CanPreresolveAndPreconnectIO.
ProfileIOData* profile_io_data_; ProfileIOData* profile_io_data_;
// This is set by InitNetworkPredictor and used for calling into the Network
// Context.
Profile* profile_;
// work_queue_ holds a list of names we need to look up. // work_queue_ holds a list of names we need to look up.
HostNameQueue work_queue_; HostNameQueue work_queue_;
...@@ -510,7 +522,8 @@ class SimplePredictor : public Predictor { ...@@ -510,7 +522,8 @@ class SimplePredictor : public Predictor {
void InitNetworkPredictor(PrefService* user_prefs, void InitNetworkPredictor(PrefService* user_prefs,
IOThread* io_thread, IOThread* io_thread,
net::URLRequestContextGetter* getter, net::URLRequestContextGetter* getter,
ProfileIOData* profile_io_data) override; ProfileIOData* profile_io_data,
Profile* profile) override;
void ShutdownOnUIThread() override; void ShutdownOnUIThread() override;
private: private:
......
...@@ -250,11 +250,9 @@ ProfileImplIOData::Handle::CreateMainRequestContextGetter( ...@@ -250,11 +250,9 @@ ProfileImplIOData::Handle::CreateMainRequestContextGetter(
BrowserThread::GetTaskRunnerForThread(BrowserThread::UI), BrowserThread::GetTaskRunnerForThread(BrowserThread::UI),
db_task_runner); db_task_runner);
io_data_->predictor_ io_data_->predictor_->InitNetworkPredictor(profile_->GetPrefs(), io_thread,
->InitNetworkPredictor(profile_->GetPrefs(), main_request_context_getter_.get(),
io_thread, io_data_, profile_);
main_request_context_getter_.get(),
io_data_);
content::NotificationService::current()->Notify( content::NotificationService::current()->Notify(
chrome::NOTIFICATION_PROFILE_URL_REQUEST_CONTEXT_GETTER_INITIALIZED, chrome::NOTIFICATION_PROFILE_URL_REQUEST_CONTEXT_GETTER_INITIALIZED,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment