Chromium Code Reviews| 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 CanPrefetchAndPrerender(int network_prediction_options, | 19 bool CanPrefetchAndPrerender(int network_prediction_options) { |
|
gavinp
2014/08/28 13:34:11
Hmm, should the type be chrome_browser_net::Networ
| |
| 20 bool network_prediction_enabled) { | |
| 21 switch (network_prediction_options) { | 20 switch (network_prediction_options) { |
| 22 case chrome_browser_net::NETWORK_PREDICTION_ALWAYS: | 21 case chrome_browser_net::NETWORK_PREDICTION_ALWAYS: |
| 23 return true; | 22 return true; |
| 24 case chrome_browser_net::NETWORK_PREDICTION_WIFI_ONLY: | |
| 25 return !net::NetworkChangeNotifier::IsConnectionCellular( | |
| 26 net::NetworkChangeNotifier::GetConnectionType()); | |
| 27 case chrome_browser_net::NETWORK_PREDICTION_NEVER: | 23 case chrome_browser_net::NETWORK_PREDICTION_NEVER: |
| 28 return false; | 24 return false; |
| 29 case chrome_browser_net::NETWORK_PREDICTION_UNSET: | |
| 30 return network_prediction_enabled; | |
| 31 default: | 25 default: |
| 32 NOTREACHED() << "Unknown kNetworkPredictionOptions value."; | 26 return !net::NetworkChangeNotifier::IsConnectionCellular( |
| 33 return false; | 27 net::NetworkChangeNotifier::GetConnectionType()); |
| 34 } | 28 } |
| 35 } | 29 } |
| 36 | 30 |
| 37 bool CanPreresolveAndPreconnect(int network_prediction_options, | 31 bool CanPreresolveAndPreconnect(int network_prediction_options) { |
| 38 bool network_prediction_enabled) { | 32 // DNS preresolution and TCP preconnect are performed even on cellular |
| 39 switch (network_prediction_options) { | 33 // networks if the user setting is WIFI_ONLY. |
| 40 case chrome_browser_net::NETWORK_PREDICTION_ALWAYS: | 34 return network_prediction_options != |
| 41 return true; | 35 chrome_browser_net::NETWORK_PREDICTION_NEVER; |
| 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 } | 36 } |
| 55 | 37 |
| 56 } // namespace | 38 } // namespace |
| 57 | 39 |
| 58 namespace chrome_browser_net { | 40 namespace chrome_browser_net { |
| 59 | 41 |
| 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 chrome_browser_net::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 ? chrome_browser_net::NETWORK_PREDICTION_WIFI_ONLY |
| 85 : chrome_browser_net::NETWORK_PREDICTION_NEVER); | 67 : chrome_browser_net::NETWORK_PREDICTION_NEVER); |
| 86 } | 68 } |
| 87 } | 69 } |
| 88 | 70 |
| 89 bool CanPrefetchAndPrerenderIO(ProfileIOData* profile_io_data) { | 71 bool CanPrefetchAndPrerenderIO(ProfileIOData* profile_io_data) { |
| 90 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 72 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 91 DCHECK(profile_io_data); | 73 DCHECK(profile_io_data); |
| 92 return CanPrefetchAndPrerender( | 74 return CanPrefetchAndPrerender( |
| 93 profile_io_data->network_prediction_options()->GetValue(), | 75 profile_io_data->network_prediction_options()->GetValue()); |
| 94 profile_io_data->network_prediction_enabled()->GetValue()); | |
| 95 } | 76 } |
| 96 | 77 |
| 97 bool CanPrefetchAndPrerenderUI(PrefService* prefs) { | 78 bool CanPrefetchAndPrerenderUI(PrefService* prefs) { |
| 98 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 79 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 99 DCHECK(prefs); | 80 DCHECK(prefs); |
| 100 return CanPrefetchAndPrerender( | 81 return CanPrefetchAndPrerender( |
| 101 prefs->GetInteger(prefs::kNetworkPredictionOptions), | 82 prefs->GetInteger(prefs::kNetworkPredictionOptions)); |
| 102 prefs->GetBoolean(prefs::kNetworkPredictionEnabled)); | |
| 103 } | 83 } |
| 104 | 84 |
| 105 bool CanPredictNetworkActionsUI(PrefService* prefs) { | 85 bool CanPredictNetworkActionsUI(PrefService* prefs) { |
| 106 return CanPrefetchAndPrerenderUI(prefs); | 86 return CanPrefetchAndPrerenderUI(prefs); |
| 107 } | 87 } |
| 108 | 88 |
| 109 bool CanPreresolveAndPreconnectIO(ProfileIOData* profile_io_data) { | 89 bool CanPreresolveAndPreconnectIO(ProfileIOData* profile_io_data) { |
| 110 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 90 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 111 DCHECK(profile_io_data); | 91 DCHECK(profile_io_data); |
| 112 return CanPreresolveAndPreconnect( | 92 return CanPreresolveAndPreconnect( |
| 113 profile_io_data->network_prediction_options()->GetValue(), | 93 profile_io_data->network_prediction_options()->GetValue()); |
| 114 profile_io_data->network_prediction_enabled()->GetValue()); | |
| 115 } | 94 } |
| 116 | 95 |
| 117 bool CanPreresolveAndPreconnectUI(PrefService* prefs) { | 96 bool CanPreresolveAndPreconnectUI(PrefService* prefs) { |
| 118 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 97 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 119 DCHECK(prefs); | 98 DCHECK(prefs); |
| 120 return CanPreresolveAndPreconnect( | 99 return CanPreresolveAndPreconnect( |
| 121 prefs->GetInteger(prefs::kNetworkPredictionOptions), | 100 prefs->GetInteger(prefs::kNetworkPredictionOptions)); |
| 122 prefs->GetBoolean(prefs::kNetworkPredictionEnabled)); | |
| 123 } | 101 } |
| 124 | 102 |
| 125 } // namespace chrome_browser_net | 103 } // namespace chrome_browser_net |
| OLD | NEW |