[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 | |
| 7 | #include "base/logging.h" |
| 8 | #include "base/prefs/pref_service.h" |
[email protected] | ba85a60 | 2014-06-28 20:37:12 | [diff] [blame] | 9 | #include "chrome/browser/profiles/profile_io_data.h" |
| 10 | #include "chrome/common/pref_names.h" |
| 11 | #include "components/pref_registry/pref_registry_syncable.h" |
| 12 | #include "content/public/browser/browser_thread.h" |
[email protected] | ba85a60 | 2014-06-28 20:37:12 | [diff] [blame] | 13 | |
| 14 | namespace { |
| 15 | |
| 16 | // Since looking up preferences and current network connection are presumably |
| 17 | // both cheap, we do not cache them here. |
| 18 | bool CanPredictNetworkActions(int NetworkPredictionOptions, |
| 19 | bool NetworkPredictionEnabled) { |
| 20 | switch (NetworkPredictionOptions) { |
| 21 | case chrome_browser_net::NETWORK_PREDICTION_ALWAYS: |
| 22 | return true; |
| 23 | case chrome_browser_net::NETWORK_PREDICTION_WIFI_ONLY: |
| 24 | return !net::NetworkChangeNotifier::IsConnectionCellular( |
| 25 | net::NetworkChangeNotifier::GetConnectionType()); |
| 26 | case chrome_browser_net::NETWORK_PREDICTION_NEVER: |
| 27 | return false; |
| 28 | case chrome_browser_net::NETWORK_PREDICTION_UNSET: |
| 29 | return NetworkPredictionEnabled; |
| 30 | default: |
| 31 | NOTREACHED() << "Unknown kNetworkPredictionOptions value."; |
| 32 | return false; |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | } // namespace |
| 37 | |
| 38 | namespace chrome_browser_net { |
| 39 | |
| 40 | void RegisterPredictionOptionsProfilePrefs( |
| 41 | user_prefs::PrefRegistrySyncable* registry) { |
| 42 | registry->RegisterIntegerPref( |
| 43 | prefs::kNetworkPredictionOptions, |
| 44 | chrome_browser_net::NETWORK_PREDICTION_UNSET, |
| 45 | user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); |
| 46 | } |
| 47 | |
[email protected] | a21eaff | 2014-06-30 16:58:02 | [diff] [blame^] | 48 | bool CanPredictNetworkActionsIO(ProfileIOData* profile_io_data) { |
[email protected] | ba85a60 | 2014-06-28 20:37:12 | [diff] [blame] | 49 | DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
[email protected] | a21eaff | 2014-06-30 16:58:02 | [diff] [blame^] | 50 | DCHECK(profile_io_data); |
[email protected] | ba85a60 | 2014-06-28 20:37:12 | [diff] [blame] | 51 | |
[email protected] | ba85a60 | 2014-06-28 20:37:12 | [diff] [blame] | 52 | return CanPredictNetworkActions( |
[email protected] | a21eaff | 2014-06-30 16:58:02 | [diff] [blame^] | 53 | profile_io_data->network_prediction_options()->GetValue(), |
| 54 | profile_io_data->network_prediction_enabled()->GetValue()); |
[email protected] | ba85a60 | 2014-06-28 20:37:12 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | bool CanPredictNetworkActionsUI(PrefService* prefs) { |
| 58 | DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 59 | DCHECK(prefs); |
| 60 | return CanPredictNetworkActions( |
| 61 | prefs->GetInteger(prefs::kNetworkPredictionOptions), |
| 62 | prefs->GetBoolean(prefs::kNetworkPredictionEnabled)); |
| 63 | } |
| 64 | |
| 65 | } // namespace chrome_browser_net |