[email protected] | ba85a60 | 2014-06-28 20:37:12 | [diff] [blame] | 1 | // 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 Wennborg | 1790e6b | 2020-04-24 19:10:33 | [diff] [blame] | 7 | #include "base/check_op.h" |
Tarun Bansal | 837b9f99 | 2018-12-13 19:19:21 | [diff] [blame] | 8 | #include "chrome/common/chrome_features.h" |
[email protected] | ba85a60 | 2014-06-28 20:37:12 | [diff] [blame] | 9 | #include "chrome/common/pref_names.h" |
10 | #include "components/pref_registry/pref_registry_syncable.h" | ||||
brettw | b1fc1b8 | 2016-02-02 00:19:08 | [diff] [blame] | 11 | #include "components/prefs/pref_service.h" |
[email protected] | ba85a60 | 2014-06-28 20:37:12 | [diff] [blame] | 12 | #include "content/public/browser/browser_thread.h" |
[email protected] | 619611c8 | 2014-07-29 21:58:13 | [diff] [blame] | 13 | #include "net/base/network_change_notifier.h" |
[email protected] | ba85a60 | 2014-06-28 20:37:12 | [diff] [blame] | 14 | |
bnc | c1a560b6 | 2014-08-29 12:54:23 | [diff] [blame] | 15 | namespace chrome_browser_net { |
16 | |||||
[email protected] | ba85a60 | 2014-06-28 20:37:12 | [diff] [blame] | 17 | namespace { |
18 | |||||
19 | // Since looking up preferences and current network connection are presumably | ||||
20 | // both cheap, we do not cache them here. | ||||
gabadie | 85b991d | 2016-01-07 10:39:39 | [diff] [blame] | 21 | NetworkPredictionStatus CanPrefetchAndPrerender( |
22 | int network_prediction_options) { | ||||
[email protected] | 16b061fe | 2014-08-13 15:54:05 | [diff] [blame] | 23 | switch (network_prediction_options) { |
bnc | c1a560b6 | 2014-08-29 12:54:23 | [diff] [blame] | 24 | case NETWORK_PREDICTION_ALWAYS: |
newt | 1f354ca | 2016-02-25 23:53:22 | [diff] [blame] | 25 | case NETWORK_PREDICTION_WIFI_ONLY: |
Tarun Bansal | 837b9f99 | 2018-12-13 19:19:21 | [diff] [blame] | 26 | if (base::FeatureList::IsEnabled( |
27 | features::kPredictivePrefetchingAllowedOnAllConnectionTypes) || | ||||
28 | !net::NetworkChangeNotifier::IsConnectionCellular( | ||||
29 | net::NetworkChangeNotifier::GetConnectionType())) { | ||||
newt | 1f354ca | 2016-02-25 23:53:22 | [diff] [blame] | 30 | return NetworkPredictionStatus::ENABLED; |
gabadie | 85b991d | 2016-01-07 10:39:39 | [diff] [blame] | 31 | } |
Tarun Bansal | 837b9f99 | 2018-12-13 19:19:21 | [diff] [blame] | 32 | return NetworkPredictionStatus::DISABLED_DUE_TO_NETWORK; |
newt | 1f354ca | 2016-02-25 23:53:22 | [diff] [blame] | 33 | default: |
34 | DCHECK_EQ(NETWORK_PREDICTION_NEVER, network_prediction_options); | ||||
35 | return NetworkPredictionStatus::DISABLED_ALWAYS; | ||||
[email protected] | 16b061fe | 2014-08-13 15:54:05 | [diff] [blame] | 36 | } |
37 | } | ||||
38 | |||||
bnc | c1a560b6 | 2014-08-29 12:54:23 | [diff] [blame] | 39 | bool 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] | ba85a60 | 2014-06-28 20:37:12 | [diff] [blame] | 43 | } |
44 | |||||
45 | } // namespace | ||||
46 | |||||
[email protected] | ba85a60 | 2014-06-28 20:37:12 | [diff] [blame] | 47 | void RegisterPredictionOptionsProfilePrefs( |
48 | user_prefs::PrefRegistrySyncable* registry) { | ||||
49 | registry->RegisterIntegerPref( | ||||
50 | prefs::kNetworkPredictionOptions, | ||||
bnc | c1a560b6 | 2014-08-29 12:54:23 | [diff] [blame] | 51 | NETWORK_PREDICTION_DEFAULT, |
[email protected] | ba85a60 | 2014-06-28 20:37:12 | [diff] [blame] | 52 | user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); |
53 | } | ||||
54 | |||||
gabadie | 85b991d | 2016-01-07 10:39:39 | [diff] [blame] | 55 | NetworkPredictionStatus CanPrefetchAndPrerenderUI(PrefService* prefs) { |
anujk.sharma | 2e02ce16 | 2015-04-29 23:10:02 | [diff] [blame] | 56 | DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
[email protected] | ba85a60 | 2014-06-28 20:37:12 | [diff] [blame] | 57 | DCHECK(prefs); |
[email protected] | 16b061fe | 2014-08-13 15:54:05 | [diff] [blame] | 58 | return CanPrefetchAndPrerender( |
bnc | c1a560b6 | 2014-08-29 12:54:23 | [diff] [blame] | 59 | prefs->GetInteger(prefs::kNetworkPredictionOptions)); |
[email protected] | 16b061fe | 2014-08-13 15:54:05 | [diff] [blame] | 60 | } |
61 | |||||
[email protected] | 16b061fe | 2014-08-13 15:54:05 | [diff] [blame] | 62 | bool CanPreresolveAndPreconnectUI(PrefService* prefs) { |
anujk.sharma | 2e02ce16 | 2015-04-29 23:10:02 | [diff] [blame] | 63 | DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
[email protected] | 16b061fe | 2014-08-13 15:54:05 | [diff] [blame] | 64 | DCHECK(prefs); |
65 | return CanPreresolveAndPreconnect( | ||||
bnc | c1a560b6 | 2014-08-29 12:54:23 | [diff] [blame] | 66 | prefs->GetInteger(prefs::kNetworkPredictionOptions)); |
[email protected] | ba85a60 | 2014-06-28 20:37:12 | [diff] [blame] | 67 | } |
68 | |||||
69 | } // namespace chrome_browser_net |