OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/net/prediction_options.h" | 5 #include "chrome/browser/net/prediction_options.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/prefs/pref_service.h" | 8 #include "base/prefs/pref_service.h" |
9 #include "chrome/browser/profiles/profile_io_data.h" | 9 #include "chrome/browser/profiles/profile_io_data.h" |
10 #include "chrome/common/pref_names.h" | 10 #include "chrome/common/pref_names.h" |
11 #include "components/pref_registry/pref_registry_syncable.h" | 11 #include "components/pref_registry/pref_registry_syncable.h" |
12 #include "content/public/browser/browser_thread.h" | 12 #include "content/public/browser/browser_thread.h" |
13 #include "net/base/network_change_notifier.h" | 13 #include "net/base/network_change_notifier.h" |
14 | 14 |
15 namespace { | 15 namespace { |
16 | 16 |
17 // Since looking up preferences and current network connection are presumably | 17 // Since looking up preferences and current network connection are presumably |
18 // both cheap, we do not cache them here. | 18 // both cheap, we do not cache them here. |
19 bool CanPredictNetworkActions(int NetworkPredictionOptions, | 19 bool CanPrefetchAndPrerender(int network_prediction_options, |
20 bool NetworkPredictionEnabled) { | 20 bool network_prediction_enabled) { |
21 switch (NetworkPredictionOptions) { | 21 switch (network_prediction_options) { |
22 case chrome_browser_net::NETWORK_PREDICTION_ALWAYS: | 22 case chrome_browser_net::NETWORK_PREDICTION_ALWAYS: |
23 return true; | 23 return true; |
24 case chrome_browser_net::NETWORK_PREDICTION_WIFI_ONLY: | 24 case chrome_browser_net::NETWORK_PREDICTION_WIFI_ONLY: |
25 return !net::NetworkChangeNotifier::IsConnectionCellular( | 25 return !net::NetworkChangeNotifier::IsConnectionCellular( |
26 net::NetworkChangeNotifier::GetConnectionType()); | 26 net::NetworkChangeNotifier::GetConnectionType()); |
27 case chrome_browser_net::NETWORK_PREDICTION_NEVER: | 27 case chrome_browser_net::NETWORK_PREDICTION_NEVER: |
28 return false; | 28 return false; |
29 case chrome_browser_net::NETWORK_PREDICTION_UNSET: | 29 case chrome_browser_net::NETWORK_PREDICTION_UNSET: |
30 return NetworkPredictionEnabled; | 30 return network_prediction_enabled; |
31 default: | 31 default: |
32 NOTREACHED() << "Unknown kNetworkPredictionOptions value."; | 32 NOTREACHED() << "Unknown kNetworkPredictionOptions value."; |
33 return false; | 33 return false; |
| 34 } |
| 35 } |
| 36 |
| 37 bool CanPreresolveAndPreconnect(int network_prediction_options, |
| 38 bool network_prediction_enabled) { |
| 39 switch (network_prediction_options) { |
| 40 case chrome_browser_net::NETWORK_PREDICTION_ALWAYS: |
| 41 return true; |
| 42 // DNS preresolution and TCP preconnect are performed even on cellular |
| 43 // networks if the user setting is WIFI_ONLY. |
| 44 case chrome_browser_net::NETWORK_PREDICTION_WIFI_ONLY: |
| 45 return true; |
| 46 case chrome_browser_net::NETWORK_PREDICTION_NEVER: |
| 47 return false; |
| 48 case chrome_browser_net::NETWORK_PREDICTION_UNSET: |
| 49 return network_prediction_enabled; |
| 50 default: |
| 51 NOTREACHED() << "Unknown kNetworkPredictionOptions value."; |
| 52 return false; |
34 } | 53 } |
35 } | 54 } |
36 | 55 |
37 } // namespace | 56 } // namespace |
38 | 57 |
39 namespace chrome_browser_net { | 58 namespace chrome_browser_net { |
40 | 59 |
41 void RegisterPredictionOptionsProfilePrefs( | 60 void RegisterPredictionOptionsProfilePrefs( |
42 user_prefs::PrefRegistrySyncable* registry) { | 61 user_prefs::PrefRegistrySyncable* registry) { |
43 registry->RegisterIntegerPref( | 62 registry->RegisterIntegerPref( |
(...skipping 16 matching lines...) Expand all Loading... |
60 | 79 |
61 bool value = false; | 80 bool value = false; |
62 if (network_prediction_enabled->GetAsBoolean(&value)) { | 81 if (network_prediction_enabled->GetAsBoolean(&value)) { |
63 pref_service->SetInteger( | 82 pref_service->SetInteger( |
64 prefs::kNetworkPredictionOptions, | 83 prefs::kNetworkPredictionOptions, |
65 value ? chrome_browser_net::NETWORK_PREDICTION_WIFI_ONLY | 84 value ? chrome_browser_net::NETWORK_PREDICTION_WIFI_ONLY |
66 : chrome_browser_net::NETWORK_PREDICTION_NEVER); | 85 : chrome_browser_net::NETWORK_PREDICTION_NEVER); |
67 } | 86 } |
68 } | 87 } |
69 | 88 |
70 bool CanPredictNetworkActionsIO(ProfileIOData* profile_io_data) { | 89 bool CanPrefetchAndPrerenderIO(ProfileIOData* profile_io_data) { |
71 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 90 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
72 DCHECK(profile_io_data); | 91 DCHECK(profile_io_data); |
73 | 92 return CanPrefetchAndPrerender( |
74 return CanPredictNetworkActions( | |
75 profile_io_data->network_prediction_options()->GetValue(), | 93 profile_io_data->network_prediction_options()->GetValue(), |
76 profile_io_data->network_prediction_enabled()->GetValue()); | 94 profile_io_data->network_prediction_enabled()->GetValue()); |
77 } | 95 } |
78 | 96 |
79 bool CanPredictNetworkActionsUI(PrefService* prefs) { | 97 bool CanPrefetchAndPrerenderUI(PrefService* prefs) { |
80 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 98 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
81 DCHECK(prefs); | 99 DCHECK(prefs); |
82 return CanPredictNetworkActions( | 100 return CanPrefetchAndPrerender( |
83 prefs->GetInteger(prefs::kNetworkPredictionOptions), | 101 prefs->GetInteger(prefs::kNetworkPredictionOptions), |
84 prefs->GetBoolean(prefs::kNetworkPredictionEnabled)); | 102 prefs->GetBoolean(prefs::kNetworkPredictionEnabled)); |
85 } | 103 } |
| 104 |
| 105 bool CanPredictNetworkActionsUI(PrefService* prefs) { |
| 106 return CanPrefetchAndPrerenderUI(prefs); |
| 107 } |
| 108 |
| 109 bool CanPreresolveAndPreconnectIO(ProfileIOData* profile_io_data) { |
| 110 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 111 DCHECK(profile_io_data); |
| 112 return CanPreresolveAndPreconnect( |
| 113 profile_io_data->network_prediction_options()->GetValue(), |
| 114 profile_io_data->network_prediction_enabled()->GetValue()); |
| 115 } |
| 116 |
| 117 bool CanPreresolveAndPreconnectUI(PrefService* prefs) { |
| 118 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 119 DCHECK(prefs); |
| 120 return CanPreresolveAndPreconnect( |
| 121 prefs->GetInteger(prefs::kNetworkPredictionOptions), |
| 122 prefs->GetBoolean(prefs::kNetworkPredictionEnabled)); |
| 123 } |
86 | 124 |
87 } // namespace chrome_browser_net | 125 } // namespace chrome_browser_net |
OLD | NEW |