Commit 85b96278 authored by Mugdha Lakhani's avatar Mugdha Lakhani Committed by Commit Bot

[Prerender] Refactor prediction code into Chrome specific delegate impl.

WebLayer doesn't need prediction logic so abstract it out into a
delegate interfact and move Chrome-specific logic into
ChromePrerenderManagerDelegate.

Bug: 1096088
Change-Id: I0daf2a64e5f195a4e5826ec7f4c9e89a402b04b9
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2326673Reviewed-by: default avatarJohn Abd-El-Malek <jam@chromium.org>
Reviewed-by: default avatarRyan Sturm <ryansturm@chromium.org>
Commit-Queue: Mugdha Lakhani <nator@chromium.org>
Cr-Commit-Position: refs/heads/master@{#794451}
parent 45fed288
......@@ -52,4 +52,65 @@ std::unique_ptr<PrerenderContentsDelegate>
ChromePrerenderManagerDelegate::GetPrerenderContentsDelegate() {
return std::make_unique<ChromePrerenderContentsDelegate>();
}
bool ChromePrerenderManagerDelegate::IsPredictionEnabled(Origin origin) {
return GetPredictionStatusForOrigin(origin) ==
chrome_browser_net::NetworkPredictionStatus::ENABLED;
}
bool ChromePrerenderManagerDelegate::IsPredictionDisabledDueToNetwork(
Origin origin) {
return GetPredictionStatusForOrigin(origin) ==
chrome_browser_net::NetworkPredictionStatus::DISABLED_DUE_TO_NETWORK;
}
bool ChromePrerenderManagerDelegate::IsPredictionEnabled() {
return GetPredictionStatus() ==
chrome_browser_net::NetworkPredictionStatus::ENABLED;
}
std::string ChromePrerenderManagerDelegate::GetReasonForDisablingPrediction() {
std::string disabled_note;
if (GetPredictionStatus() ==
chrome_browser_net::NetworkPredictionStatus::DISABLED_ALWAYS)
disabled_note = "Disabled by user setting";
if (GetPredictionStatus() ==
chrome_browser_net::NetworkPredictionStatus::DISABLED_DUE_TO_NETWORK)
disabled_note = "Disabled on cellular connection by default";
return disabled_note;
}
chrome_browser_net::NetworkPredictionStatus
ChromePrerenderManagerDelegate::GetPredictionStatus() const {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
return chrome_browser_net::CanPrefetchAndPrerenderUI(profile_->GetPrefs());
}
chrome_browser_net::NetworkPredictionStatus
ChromePrerenderManagerDelegate::GetPredictionStatusForOrigin(
Origin origin) const {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
// <link rel=prerender> origins ignore the network state and the privacy
// settings. Web developers should be able prefetch with all possible privacy
// settings and with all possible network types. This would avoid web devs
// coming up with creative ways to prefetch in cases they are not allowed to
// do so.
if (origin == ORIGIN_LINK_REL_PRERENDER_SAMEDOMAIN ||
origin == ORIGIN_LINK_REL_PRERENDER_CROSSDOMAIN) {
return chrome_browser_net::NetworkPredictionStatus::ENABLED;
}
// Prerendering forced for cellular networks still prevents navigation with
// the DISABLED_ALWAYS selected via privacy settings.
chrome_browser_net::NetworkPredictionStatus prediction_status =
chrome_browser_net::CanPrefetchAndPrerenderUI(profile_->GetPrefs());
if (origin == ORIGIN_EXTERNAL_REQUEST_FORCED_PRERENDER &&
prediction_status == chrome_browser_net::NetworkPredictionStatus::
DISABLED_DUE_TO_NETWORK) {
return chrome_browser_net::NetworkPredictionStatus::ENABLED;
}
return prediction_status;
}
} // namespace prerender
......@@ -5,6 +5,7 @@
#ifndef CHROME_BROWSER_PRERENDER_CHROME_PRERENDER_MANAGER_DELEGATE_H_
#define CHROME_BROWSER_PRERENDER_CHROME_PRERENDER_MANAGER_DELEGATE_H_
#include "chrome/browser/net/prediction_options.h"
#include "chrome/browser/prerender/prerender_manager_delegate.h"
class Profile;
......@@ -25,8 +26,15 @@ class ChromePrerenderManagerDelegate : public PrerenderManagerDelegate {
void MaybePreconnect(const GURL& url) override;
std::unique_ptr<PrerenderContentsDelegate> GetPrerenderContentsDelegate()
override;
bool IsPredictionEnabled(Origin origin) override;
bool IsPredictionEnabled() override;
bool IsPredictionDisabledDueToNetwork(Origin origin) override;
std::string GetReasonForDisablingPrediction() override;
private:
chrome_browser_net::NetworkPredictionStatus GetPredictionStatus() const;
chrome_browser_net::NetworkPredictionStatus GetPredictionStatusForOrigin(
Origin origin) const;
Profile* profile_;
};
......
......@@ -31,9 +31,6 @@
#include "base/time/time.h"
#include "base/timer/elapsed_timer.h"
#include "base/values.h"
#include "chrome/browser/net/prediction_options.h"
#include "chrome/browser/predictors/loading_predictor.h"
#include "chrome/browser/predictors/loading_predictor_factory.h"
#include "chrome/browser/prerender/prerender_contents.h"
#include "chrome/browser/prerender/prerender_field_trial.h"
#include "chrome/browser/prerender/prerender_handle.h"
......@@ -62,7 +59,6 @@
#include "third_party/blink/public/common/prerender/prerender_rel_type.h"
#include "ui/gfx/geometry/rect.h"
using chrome_browser_net::NetworkPredictionStatus;
using content::BrowserThread;
using content::RenderViewHost;
using content::SessionStorageNamespace;
......@@ -410,14 +406,9 @@ std::unique_ptr<base::DictionaryValue> PrerenderManager::CopyAsValue() const {
auto dict_value = std::make_unique<base::DictionaryValue>();
dict_value->Set("history", prerender_history_->CopyEntriesAsValue());
dict_value->Set("active", GetActivePrerendersAsValue());
dict_value->SetBoolean(
"enabled", GetPredictionStatus() == NetworkPredictionStatus::ENABLED);
std::string disabled_note;
if (GetPredictionStatus() == NetworkPredictionStatus::DISABLED_ALWAYS)
disabled_note = "Disabled by user setting";
if (GetPredictionStatus() == NetworkPredictionStatus::DISABLED_DUE_TO_NETWORK)
disabled_note = "Disabled on cellular connection by default";
dict_value->SetString("disabled_note", disabled_note);
dict_value->SetBoolean("enabled", delegate_->IsPredictionEnabled());
dict_value->SetString("disabled_note",
delegate_->GetReasonForDisablingPrediction());
// If prerender is disabled via a flag this method is not even called.
std::string enabled_note;
dict_value->SetString("enabled_note", enabled_note);
......@@ -554,11 +545,9 @@ PrerenderManager::AddPrerenderWithPreconnectFallback(
return nullptr;
}
NetworkPredictionStatus prerendering_status =
GetPredictionStatusForOrigin(origin);
if (prerendering_status != NetworkPredictionStatus::ENABLED) {
if (!delegate_->IsPredictionEnabled(origin)) {
FinalStatus final_status =
prerendering_status == NetworkPredictionStatus::DISABLED_DUE_TO_NETWORK
delegate_->IsPredictionDisabledDueToNetwork(origin)
? FINAL_STATUS_CELLULAR_NETWORK
: FINAL_STATUS_PRERENDERING_DISABLED;
SkipPrerenderContentsAndMaybePreconnect(url, origin, final_status);
......@@ -995,43 +984,6 @@ void PrerenderManager::RecordNetworkBytesConsumed(Origin origin,
recent_profile_bytes);
}
NetworkPredictionStatus PrerenderManager::GetPredictionStatus() const {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
return chrome_browser_net::CanPrefetchAndPrerenderUI(profile_->GetPrefs());
}
NetworkPredictionStatus PrerenderManager::GetPredictionStatusForOrigin(
Origin origin) const {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
// <link rel=prerender> origins ignore the network state and the privacy
// settings. Web developers should be able prefetch with all possible privacy
// settings and with all possible network types. This would avoid web devs
// coming up with creative ways to prefetch in cases they are not allowed to
// do so.
if (origin == ORIGIN_LINK_REL_PRERENDER_SAMEDOMAIN ||
origin == ORIGIN_LINK_REL_PRERENDER_CROSSDOMAIN) {
return NetworkPredictionStatus::ENABLED;
}
// Prerendering forced for cellular networks still prevents navigation with
// the DISABLED_ALWAYS selected via privacy settings.
NetworkPredictionStatus prediction_status =
chrome_browser_net::CanPrefetchAndPrerenderUI(profile_->GetPrefs());
if (origin == ORIGIN_EXTERNAL_REQUEST_FORCED_PRERENDER &&
prediction_status == NetworkPredictionStatus::DISABLED_DUE_TO_NETWORK) {
return NetworkPredictionStatus::ENABLED;
}
return prediction_status;
}
void PrerenderManager::AddProfileNetworkBytesIfEnabled(int64_t bytes) {
DCHECK_GE(bytes, 0);
if (GetPredictionStatus() == NetworkPredictionStatus::ENABLED &&
IsNoStatePrefetchEnabled())
profile_network_bytes_ += bytes;
}
void PrerenderManager::AddPrerenderProcessHost(
content::RenderProcessHost* process_host) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
......
......@@ -266,10 +266,6 @@ class PrerenderManager : public content::RenderProcessHostObserver,
// recorded.
void RecordNetworkBytesConsumed(Origin origin, int64_t prerender_bytes);
// Add to the running tally of bytes transferred over the network for this
// profile if prerendering is currently enabled.
void AddProfileNetworkBytesIfEnabled(int64_t bytes);
// Registers a new ProcessHost performing a prerender. Called by
// PrerenderContents.
void AddPrerenderProcessHost(content::RenderProcessHost* process_host);
......@@ -388,15 +384,6 @@ class PrerenderManager : public content::RenderProcessHostObserver,
// Time window for which we record old navigations, in milliseconds.
static const int kNavigationRecordWindowMs = 5000;
// Returns whether prerendering is currently enabled or the reason why it is
// disabled.
chrome_browser_net::NetworkPredictionStatus GetPredictionStatus() const;
// Returns whether prerendering is currently enabled or the reason why it is
// disabled after taking into account the origin of the request.
chrome_browser_net::NetworkPredictionStatus GetPredictionStatusForOrigin(
Origin origin) const;
// Adds a prerender for |url| from |referrer|. The |origin| specifies how the
// prerender was added. If |bounds| is empty, then
// PrerenderContents::StartPrerendering will instead use a default from
......
......@@ -7,6 +7,7 @@
#include "base/memory/scoped_refptr.h"
#include "chrome/browser/prerender/prerender_contents_delegate.h"
#include "components/prerender/common/prerender_origin.h"
#include "url/gurl.h"
namespace content_settings {
......@@ -31,6 +32,18 @@ class PrerenderManagerDelegate {
// Get the prerender contents delegate.
virtual std::unique_ptr<PrerenderContentsDelegate>
GetPrerenderContentsDelegate() = 0;
// Check whether predictive loading of web pages is enabled for |origin|.
virtual bool IsPredictionEnabled(Origin origin) = 0;
// Check whether predictive loading of web pages is enabled.
virtual bool IsPredictionEnabled() = 0;
// Check whether predictive loading of web pages is disabled due to network.
virtual bool IsPredictionDisabledDueToNetwork(Origin origin) = 0;
// Gets the reason why predictive loading of web pages was disabld.
virtual std::string GetReasonForDisablingPrediction() = 0;
};
} // namespace prerender
......
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