Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1796)

Unified Diff: chrome/browser/android/offline_pages/offline_page_request_job.cc

Issue 2388253002: Use the previews black list for offline previews (Closed)
Patch Set: mmenke comments Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/android/offline_pages/offline_page_request_job.cc
diff --git a/chrome/browser/android/offline_pages/offline_page_request_job.cc b/chrome/browser/android/offline_pages/offline_page_request_job.cc
index bc8ad7460fb11f40c8896f8a48d1557f85c5c02c..5f161416f40d0223d35eaf4c058085e8962459ee 100644
--- a/chrome/browser/android/offline_pages/offline_page_request_job.cc
+++ b/chrome/browser/android/offline_pages/offline_page_request_job.cc
@@ -12,40 +12,41 @@
#include "base/metrics/histogram_macros.h"
#include "base/strings/string_number_conversions.h"
#include "base/threading/thread_task_runner_handle.h"
#include "chrome/browser/android/offline_pages/offline_page_model_factory.h"
#include "chrome/browser/android/offline_pages/offline_page_tab_helper.h"
#include "chrome/browser/android/offline_pages/offline_page_utils.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "components/offline_pages/offline_page_model.h"
#include "components/offline_pages/request_header/offline_page_header.h"
-#include "components/previews/core/previews_experiments.h"
+#include "components/previews/core/previews_decider.h"
+#include "components/previews/core/previews_opt_out_store.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/resource_request_info.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/resource_type.h"
#include "net/base/network_change_notifier.h"
#include "net/http/http_request_headers.h"
-#include "net/nqe/network_quality_estimator.h"
#include "net/url_request/url_request.h"
-#include "net/url_request/url_request_context.h"
namespace offline_pages {
namespace {
enum class NetworkState {
// No network connection.
DISCONNECTED_NETWORK,
// Prohibitively slow means that the NetworkQualityEstimator reported a
// connection slow enough to warrant showing an offline page if available.
+ // This requires offline previews to be enabled and the URL of the request to
+ // be allowed by previews.
PROHIBITIVELY_SLOW_NETWORK,
// Network error received due to bad network, i.e. connected to a hotspot or
// proxy that does not have a working network.
FLAKY_NETWORK,
// Network is in working condition.
CONNECTED_NETWORK,
// Force to load the offline page if it is available, though network is in
// working condition.
FORCE_OFFLINE_ON_CONNECTED_NETWORK
};
@@ -101,51 +102,37 @@ class DefaultDelegate : public OfflinePageRequestJob::Delegate {
}
private:
static bool GetTabId(content::WebContents* web_contents, int* tab_id) {
return OfflinePageUtils::GetTabId(web_contents, tab_id);
}
DISALLOW_COPY_AND_ASSIGN(DefaultDelegate);
};
-bool IsNetworkProhibitivelySlow(net::URLRequest* request) {
- // NetworkQualityEstimator only works when it is enabled.
- if (!previews::IsOfflinePreviewsEnabled())
- return false;
-
- if (!request->context())
- return false;
-
- net::NetworkQualityEstimator* network_quality_estimator =
- request->context()->network_quality_estimator();
- if (!network_quality_estimator)
- return false;
-
- net::EffectiveConnectionType effective_connection_type =
- network_quality_estimator->GetEffectiveConnectionType();
- return effective_connection_type >= net::EFFECTIVE_CONNECTION_TYPE_OFFLINE &&
- effective_connection_type <= net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G;
-}
-
NetworkState GetNetworkState(net::URLRequest* request,
- const OfflinePageHeader& offline_header) {
+ const OfflinePageHeader& offline_header,
+ previews::PreviewsDecider* previews_decider) {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
if (offline_header.reason == OfflinePageHeader::Reason::NET_ERROR)
return NetworkState::FLAKY_NETWORK;
if (net::NetworkChangeNotifier::IsOffline())
return NetworkState::DISCONNECTED_NETWORK;
-
- if (IsNetworkProhibitivelySlow(request))
+ // Checks if previews are allowed, the network is slow, and the request is
+ // allowed to be shown for previews.
+ if (previews_decider &&
+ previews_decider->ShouldAllowPreview(*request,
+ previews::PreviewsType::OFFLINE)) {
return NetworkState::PROHIBITIVELY_SLOW_NETWORK;
+ }
// If offline header contains a reason other than RELOAD, the offline page
// should be forced to load even when the network is connected.
return (offline_header.reason != OfflinePageHeader::Reason::NONE &&
offline_header.reason != OfflinePageHeader::Reason::RELOAD)
? NetworkState::FORCE_OFFLINE_ON_CONNECTED_NETWORK
: NetworkState::CONNECTED_NETWORK;
}
OfflinePageRequestJob::AggregatedRequestResult
@@ -448,21 +435,22 @@ void SelectPage(
void OfflinePageRequestJob::ReportAggregatedRequestResult(
AggregatedRequestResult result) {
UMA_HISTOGRAM_ENUMERATION("OfflinePages.AggregatedRequestResult",
static_cast<int>(result),
static_cast<int>(AggregatedRequestResult::AGGREGATED_REQUEST_RESULT_MAX));
}
// static
OfflinePageRequestJob* OfflinePageRequestJob::Create(
net::URLRequest* request,
- net::NetworkDelegate* network_delegate) {
+ net::NetworkDelegate* network_delegate,
+ previews::PreviewsDecider* previews_decider) {
const content::ResourceRequestInfo* resource_request_info =
content::ResourceRequestInfo::ForRequest(request);
if (!resource_request_info)
return nullptr;
// Ignore the requests not for the main resource.
if (resource_request_info->GetResourceType() !=
content::RESOURCE_TYPE_MAIN_FRAME) {
return nullptr;
}
@@ -479,55 +467,57 @@ OfflinePageRequestJob* OfflinePageRequestJob::Create(
OfflinePageRequestInfo::GetFromRequest(request);
if (info) {
// Fall back to default which is set when an offline page cannot be served,
// either page not found or online version desired.
if (info->use_default())
return nullptr;
} else {
request->SetUserData(&kUserDataKey, new OfflinePageRequestInfo());
}
- return new OfflinePageRequestJob(request, network_delegate);
+ return new OfflinePageRequestJob(request, network_delegate, previews_decider);
}
OfflinePageRequestJob::OfflinePageRequestJob(
net::URLRequest* request,
- net::NetworkDelegate* network_delegate)
+ net::NetworkDelegate* network_delegate,
+ previews::PreviewsDecider* previews_decider)
: net::URLRequestFileJob(
request,
network_delegate,
base::FilePath(),
- content::BrowserThread::GetBlockingPool()->
- GetTaskRunnerWithShutdownBehavior(
+ content::BrowserThread::GetBlockingPool()
+ ->GetTaskRunnerWithShutdownBehavior(
base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)),
delegate_(new DefaultDelegate()),
- weak_ptr_factory_(this) {
-}
+ previews_decider_(previews_decider),
+ weak_ptr_factory_(this) {}
OfflinePageRequestJob::~OfflinePageRequestJob() {
}
void OfflinePageRequestJob::Start() {
base::ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE, base::Bind(&OfflinePageRequestJob::StartAsync,
weak_ptr_factory_.GetWeakPtr()));
}
void OfflinePageRequestJob::StartAsync() {
std::string offline_header_value;
request()->extra_request_headers().GetHeader(
kOfflinePageHeader, &offline_header_value);
// Note that |offline_header| will be empty if parsing from the header value
// fails.
OfflinePageHeader offline_header(offline_header_value);
- NetworkState network_state = GetNetworkState(request(), offline_header);
+ NetworkState network_state =
+ GetNetworkState(request(), offline_header, previews_decider_);
if (network_state == NetworkState::CONNECTED_NETWORK) {
FallbackToDefault();
return;
}
content::BrowserThread::PostTask(
content::BrowserThread::UI,
FROM_HERE,
base::Bind(&SelectPage,
request()->url(),

Powered by Google App Engine
This is Rietveld 408576698