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 chrome_browser_net { |
| 16 |
15 namespace { | 17 namespace { |
16 | 18 |
17 // Since looking up preferences and current network connection are presumably | 19 // Since looking up preferences and current network connection are presumably |
18 // both cheap, we do not cache them here. | 20 // both cheap, we do not cache them here. |
19 bool CanPrefetchAndPrerender(int network_prediction_options, | 21 bool CanPrefetchAndPrerender(int network_prediction_options) { |
20 bool network_prediction_enabled) { | |
21 switch (network_prediction_options) { | 22 switch (network_prediction_options) { |
22 case chrome_browser_net::NETWORK_PREDICTION_ALWAYS: | 23 case NETWORK_PREDICTION_ALWAYS: |
23 return true; | 24 return true; |
24 case chrome_browser_net::NETWORK_PREDICTION_WIFI_ONLY: | 25 case NETWORK_PREDICTION_NEVER: |
| 26 return false; |
| 27 default: |
| 28 DCHECK_EQ(NETWORK_PREDICTION_WIFI_ONLY, network_prediction_options); |
25 return !net::NetworkChangeNotifier::IsConnectionCellular( | 29 return !net::NetworkChangeNotifier::IsConnectionCellular( |
26 net::NetworkChangeNotifier::GetConnectionType()); | 30 net::NetworkChangeNotifier::GetConnectionType()); |
27 case chrome_browser_net::NETWORK_PREDICTION_NEVER: | |
28 return false; | |
29 case chrome_browser_net::NETWORK_PREDICTION_UNSET: | |
30 return network_prediction_enabled; | |
31 default: | |
32 NOTREACHED() << "Unknown kNetworkPredictionOptions value."; | |
33 return false; | |
34 } | 31 } |
35 } | 32 } |
36 | 33 |
37 bool CanPreresolveAndPreconnect(int network_prediction_options, | 34 bool CanPreresolveAndPreconnect(int network_prediction_options) { |
38 bool network_prediction_enabled) { | 35 // DNS preresolution and TCP preconnect are performed even on cellular |
39 switch (network_prediction_options) { | 36 // networks if the user setting is WIFI_ONLY. |
40 case chrome_browser_net::NETWORK_PREDICTION_ALWAYS: | 37 return network_prediction_options != NETWORK_PREDICTION_NEVER; |
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; | |
53 } | |
54 } | 38 } |
55 | 39 |
56 } // namespace | 40 } // namespace |
57 | 41 |
58 namespace chrome_browser_net { | |
59 | |
60 void RegisterPredictionOptionsProfilePrefs( | 42 void RegisterPredictionOptionsProfilePrefs( |
61 user_prefs::PrefRegistrySyncable* registry) { | 43 user_prefs::PrefRegistrySyncable* registry) { |
62 registry->RegisterIntegerPref( | 44 registry->RegisterIntegerPref( |
63 prefs::kNetworkPredictionOptions, | 45 prefs::kNetworkPredictionOptions, |
64 chrome_browser_net::NETWORK_PREDICTION_UNSET, | 46 NETWORK_PREDICTION_DEFAULT, |
65 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); | 47 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); |
66 } | 48 } |
67 | 49 |
68 void MigrateNetworkPredictionUserPrefs(PrefService* pref_service) { | 50 void MigrateNetworkPredictionUserPrefs(PrefService* pref_service) { |
69 // Nothing to do if the user or this migration code has already set the new | 51 // Nothing to do if the user or this migration code has already set the new |
70 // preference. | 52 // preference. |
71 if (pref_service->GetUserPrefValue(prefs::kNetworkPredictionOptions)) | 53 if (pref_service->GetUserPrefValue(prefs::kNetworkPredictionOptions)) |
72 return; | 54 return; |
73 | 55 |
74 // Nothing to do if the user has not set the old preference. | 56 // Nothing to do if the user has not set the old preference. |
75 const base::Value* network_prediction_enabled = | 57 const base::Value* network_prediction_enabled = |
76 pref_service->GetUserPrefValue(prefs::kNetworkPredictionEnabled); | 58 pref_service->GetUserPrefValue(prefs::kNetworkPredictionEnabled); |
77 if (!network_prediction_enabled) | 59 if (!network_prediction_enabled) |
78 return; | 60 return; |
79 | 61 |
80 bool value = false; | 62 bool value = false; |
81 if (network_prediction_enabled->GetAsBoolean(&value)) { | 63 if (network_prediction_enabled->GetAsBoolean(&value)) { |
82 pref_service->SetInteger( | 64 pref_service->SetInteger( |
83 prefs::kNetworkPredictionOptions, | 65 prefs::kNetworkPredictionOptions, |
84 value ? chrome_browser_net::NETWORK_PREDICTION_WIFI_ONLY | 66 value ? NETWORK_PREDICTION_WIFI_ONLY : NETWORK_PREDICTION_NEVER); |
85 : chrome_browser_net::NETWORK_PREDICTION_NEVER); | |
86 } | 67 } |
87 } | 68 } |
88 | 69 |
89 bool CanPrefetchAndPrerenderIO(ProfileIOData* profile_io_data) { | 70 bool CanPrefetchAndPrerenderIO(ProfileIOData* profile_io_data) { |
90 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 71 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
91 DCHECK(profile_io_data); | 72 DCHECK(profile_io_data); |
92 return CanPrefetchAndPrerender( | 73 return CanPrefetchAndPrerender( |
93 profile_io_data->network_prediction_options()->GetValue(), | 74 profile_io_data->network_prediction_options()->GetValue()); |
94 profile_io_data->network_prediction_enabled()->GetValue()); | |
95 } | 75 } |
96 | 76 |
97 bool CanPrefetchAndPrerenderUI(PrefService* prefs) { | 77 bool CanPrefetchAndPrerenderUI(PrefService* prefs) { |
98 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 78 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
99 DCHECK(prefs); | 79 DCHECK(prefs); |
100 return CanPrefetchAndPrerender( | 80 return CanPrefetchAndPrerender( |
101 prefs->GetInteger(prefs::kNetworkPredictionOptions), | 81 prefs->GetInteger(prefs::kNetworkPredictionOptions)); |
102 prefs->GetBoolean(prefs::kNetworkPredictionEnabled)); | |
103 } | 82 } |
104 | 83 |
105 bool CanPredictNetworkActionsUI(PrefService* prefs) { | 84 bool CanPredictNetworkActionsUI(PrefService* prefs) { |
106 return CanPrefetchAndPrerenderUI(prefs); | 85 return CanPrefetchAndPrerenderUI(prefs); |
107 } | 86 } |
108 | 87 |
109 bool CanPreresolveAndPreconnectIO(ProfileIOData* profile_io_data) { | 88 bool CanPreresolveAndPreconnectIO(ProfileIOData* profile_io_data) { |
110 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 89 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
111 DCHECK(profile_io_data); | 90 DCHECK(profile_io_data); |
112 return CanPreresolveAndPreconnect( | 91 return CanPreresolveAndPreconnect( |
113 profile_io_data->network_prediction_options()->GetValue(), | 92 profile_io_data->network_prediction_options()->GetValue()); |
114 profile_io_data->network_prediction_enabled()->GetValue()); | |
115 } | 93 } |
116 | 94 |
117 bool CanPreresolveAndPreconnectUI(PrefService* prefs) { | 95 bool CanPreresolveAndPreconnectUI(PrefService* prefs) { |
118 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 96 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
119 DCHECK(prefs); | 97 DCHECK(prefs); |
120 return CanPreresolveAndPreconnect( | 98 return CanPreresolveAndPreconnect( |
121 prefs->GetInteger(prefs::kNetworkPredictionOptions), | 99 prefs->GetInteger(prefs::kNetworkPredictionOptions)); |
122 prefs->GetBoolean(prefs::kNetworkPredictionEnabled)); | |
123 } | 100 } |
124 | 101 |
125 } // namespace chrome_browser_net | 102 } // namespace chrome_browser_net |
OLD | NEW |