blob: 5268257b841fcb6034aae6306b3fbddac598d26e [file] [log] [blame]
[email protected]ba85a602014-06-28 20:37:121// Copyright 2014 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "chrome/browser/net/prediction_options.h"
6
Hans Wennborg1790e6b2020-04-24 19:10:337#include "base/check_op.h"
Tarun Bansal837b9f992018-12-13 19:19:218#include "chrome/common/chrome_features.h"
[email protected]ba85a602014-06-28 20:37:129#include "chrome/common/pref_names.h"
10#include "components/pref_registry/pref_registry_syncable.h"
brettwb1fc1b82016-02-02 00:19:0811#include "components/prefs/pref_service.h"
[email protected]ba85a602014-06-28 20:37:1212#include "content/public/browser/browser_thread.h"
[email protected]619611c82014-07-29 21:58:1313#include "net/base/network_change_notifier.h"
[email protected]ba85a602014-06-28 20:37:1214
bncc1a560b62014-08-29 12:54:2315namespace chrome_browser_net {
16
[email protected]ba85a602014-06-28 20:37:1217namespace {
18
19// Since looking up preferences and current network connection are presumably
20// both cheap, we do not cache them here.
gabadie85b991d2016-01-07 10:39:3921NetworkPredictionStatus CanPrefetchAndPrerender(
22 int network_prediction_options) {
[email protected]16b061fe2014-08-13 15:54:0523 switch (network_prediction_options) {
bncc1a560b62014-08-29 12:54:2324 case NETWORK_PREDICTION_ALWAYS:
newt1f354ca2016-02-25 23:53:2225 case NETWORK_PREDICTION_WIFI_ONLY:
Tarun Bansal837b9f992018-12-13 19:19:2126 if (base::FeatureList::IsEnabled(
27 features::kPredictivePrefetchingAllowedOnAllConnectionTypes) ||
28 !net::NetworkChangeNotifier::IsConnectionCellular(
29 net::NetworkChangeNotifier::GetConnectionType())) {
newt1f354ca2016-02-25 23:53:2230 return NetworkPredictionStatus::ENABLED;
gabadie85b991d2016-01-07 10:39:3931 }
Tarun Bansal837b9f992018-12-13 19:19:2132 return NetworkPredictionStatus::DISABLED_DUE_TO_NETWORK;
newt1f354ca2016-02-25 23:53:2233 default:
34 DCHECK_EQ(NETWORK_PREDICTION_NEVER, network_prediction_options);
35 return NetworkPredictionStatus::DISABLED_ALWAYS;
[email protected]16b061fe2014-08-13 15:54:0536 }
37}
38
bncc1a560b62014-08-29 12:54:2339bool CanPreresolveAndPreconnect(int network_prediction_options) {
40 // DNS preresolution and TCP preconnect are performed even on cellular
41 // networks if the user setting is WIFI_ONLY.
42 return network_prediction_options != NETWORK_PREDICTION_NEVER;
[email protected]ba85a602014-06-28 20:37:1243}
44
45} // namespace
46
[email protected]ba85a602014-06-28 20:37:1247void RegisterPredictionOptionsProfilePrefs(
48 user_prefs::PrefRegistrySyncable* registry) {
49 registry->RegisterIntegerPref(
50 prefs::kNetworkPredictionOptions,
bncc1a560b62014-08-29 12:54:2351 NETWORK_PREDICTION_DEFAULT,
[email protected]ba85a602014-06-28 20:37:1252 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
53}
54
gabadie85b991d2016-01-07 10:39:3955NetworkPredictionStatus CanPrefetchAndPrerenderUI(PrefService* prefs) {
anujk.sharma2e02ce162015-04-29 23:10:0256 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
[email protected]ba85a602014-06-28 20:37:1257 DCHECK(prefs);
[email protected]16b061fe2014-08-13 15:54:0558 return CanPrefetchAndPrerender(
bncc1a560b62014-08-29 12:54:2359 prefs->GetInteger(prefs::kNetworkPredictionOptions));
[email protected]16b061fe2014-08-13 15:54:0560}
61
[email protected]16b061fe2014-08-13 15:54:0562bool CanPreresolveAndPreconnectUI(PrefService* prefs) {
anujk.sharma2e02ce162015-04-29 23:10:0263 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
[email protected]16b061fe2014-08-13 15:54:0564 DCHECK(prefs);
65 return CanPreresolveAndPreconnect(
bncc1a560b62014-08-29 12:54:2366 prefs->GetInteger(prefs::kNetworkPredictionOptions));
[email protected]ba85a602014-06-28 20:37:1267}
68
69} // namespace chrome_browser_net