Commit b58c0181 authored by Michael Crouse's avatar Michael Crouse Committed by Commit Bot

[Previews] Add offline check before HintsFetch.

Only attempt to fetch hints if the network is available.

Note: The Optimization Guide does not support iOS but is in components
due to supporting Previews. A future refactor of Previews may allow for
 moving Optimization Guide to the browser layer. An assertion was added
to the BUILD.gn file to make the iOS decision more clear.

Bug: 986817
Change-Id: Iafa4101b42147530e0b38f2d6a4eb36a7ddac37c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1715505Reviewed-by: default avatarKinuko Yasuda <kinuko@chromium.org>
Reviewed-by: default avatarTarun Bansal <tbansal@chromium.org>
Reviewed-by: default avatarRobert Ogden <robertogden@chromium.org>
Commit-Queue: Michael Crouse <mcrouse@chromium.org>
Cr-Commit-Position: refs/heads/master@{#681180}
parent f699225c
......@@ -40,6 +40,7 @@
#include "components/previews/core/previews_switches.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/network_connection_change_simulator.h"
#include "net/test/embedded_test_server/http_request.h"
#include "net/test/embedded_test_server/http_response.h"
#include "services/network/public/cpp/network_quality_tracker.h"
......@@ -168,6 +169,11 @@ class HintsFetcherDisabledBrowserTest : public InProcessBrowserTest {
run_loop.Run();
}
void SetNetworkConnectionOffline() {
content::NetworkConnectionChangeSimulator().SetConnectionType(
network::mojom::ConnectionType::CONNECTION_NONE);
}
// Seeds the Site Engagement Service with two HTTP and two HTTPS sites for the
// current profile.
void SeedSiteEngagementService() {
......@@ -652,3 +658,30 @@ IN_PROC_BROWSER_TEST_F(
optimization_guide::HintCacheStore::StoreEntryType::kComponentHint),
0);
}
IN_PROC_BROWSER_TEST_F(
HintsFetcherBrowserTest,
DISABLE_ON_WIN_MAC_CHROMESOS(HintsFetcherNetworkOffline)) {
const base::HistogramTester* histogram_tester = GetHistogramTester();
GURL url = https_url();
base::CommandLine::ForCurrentProcess()->RemoveSwitch(
optimization_guide::switches::kFetchHintsOverride);
base::CommandLine::ForCurrentProcess()->AppendSwitch(
optimization_guide::switches::kFetchHintsOverrideTimer);
// Set the network to be offline.
SetNetworkConnectionOffline();
// Set the blacklist state to initialized so the sites in the engagement
// service will be used and not blacklisted on the first GetTopHosts
// request.
SetTopHostBlacklistState(optimization_guide::prefs::
HintsFetcherTopHostBlacklistState::kInitialized);
// Whitelist NoScript for https_url()'s' host.
SetUpComponentUpdateHints(https_url());
// No HintsFetch should occur because the connection is offline.
histogram_tester->ExpectTotalCount(
"OptimizationGuide.HintsFetcher.GetHintsRequest.HostCount", 0);
}
......@@ -2,6 +2,8 @@
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
assert(!is_ios, "Optimization Guide is not available on iOS.")
static_library("optimization_guide") {
sources = [
"bloom_filter.cc",
......@@ -42,6 +44,7 @@ static_library("optimization_guide") {
"//components/leveldb_proto",
"//components/optimization_guide/proto:optimization_guide_proto",
"//components/prefs",
"//content/public/browser",
"//google_apis",
"//net:net",
"//services/network/public/cpp",
......
include_rules = [
"+components/leveldb_proto",
"+components/prefs",
"+content/public/browser",
"+google_apis",
"+net",
"+services/network",
......
......@@ -12,12 +12,12 @@
#include "base/metrics/histogram_macros.h"
#include "components/optimization_guide/optimization_guide_features.h"
#include "components/optimization_guide/proto/hints.pb.h"
#include "content/public/browser/network_service_instance.h"
#include "net/base/load_flags.h"
#include "net/base/url_util.h"
#include "net/http/http_request_headers.h"
#include "net/http/http_response_headers.h"
#include "net/http/http_status_code.h"
#include "net/traffic_annotation/network_traffic_annotation.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "services/network/public/cpp/simple_url_loader.h"
......@@ -42,6 +42,11 @@ bool HintsFetcher::FetchOptimizationGuideServiceHints(
HintsFetchedCallback hints_fetched_callback) {
SEQUENCE_CHECKER(sequence_checker_);
if (content::GetNetworkConnectionTracker()->IsOffline()) {
std::move(hints_fetched_callback_).Run(base::nullopt);
return false;
}
if (url_loader_)
return false;
......
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