[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] | 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. | ||||
bnc | c1a560b6 | 2014-08-29 12:54:23 | [diff] [blame] | 21 | bool CanPrefetchAndPrerender(int network_prediction_options) { |
[email protected] | 16b061fe | 2014-08-13 15:54:05 | [diff] [blame] | 22 | switch (network_prediction_options) { |
bnc | c1a560b6 | 2014-08-29 12:54:23 | [diff] [blame] | 23 | case NETWORK_PREDICTION_ALWAYS: |
[email protected] | ba85a60 | 2014-06-28 20:37:12 | [diff] [blame] | 24 | return true; |
bnc | c1a560b6 | 2014-08-29 12:54:23 | [diff] [blame] | 25 | case NETWORK_PREDICTION_NEVER: |
[email protected] | ba85a60 | 2014-06-28 20:37:12 | [diff] [blame] | 26 | return false; |
[email protected] | 16b061fe | 2014-08-13 15:54:05 | [diff] [blame] | 27 | default: |
bnc | c1a560b6 | 2014-08-29 12:54:23 | [diff] [blame] | 28 | DCHECK_EQ(NETWORK_PREDICTION_WIFI_ONLY, network_prediction_options); |
29 | return !net::NetworkChangeNotifier::IsConnectionCellular( | ||||
30 | net::NetworkChangeNotifier::GetConnectionType()); | ||||
[email protected] | 16b061fe | 2014-08-13 15:54:05 | [diff] [blame] | 31 | } |
32 | } | ||||
33 | |||||
bnc | c1a560b6 | 2014-08-29 12:54:23 | [diff] [blame] | 34 | bool CanPreresolveAndPreconnect(int network_prediction_options) { |
35 | // DNS preresolution and TCP preconnect are performed even on cellular | ||||
36 | // networks if the user setting is WIFI_ONLY. | ||||
37 | return network_prediction_options != NETWORK_PREDICTION_NEVER; | ||||
[email protected] | ba85a60 | 2014-06-28 20:37:12 | [diff] [blame] | 38 | } |
39 | |||||
40 | } // namespace | ||||
41 | |||||
[email protected] | ba85a60 | 2014-06-28 20:37:12 | [diff] [blame] | 42 | void RegisterPredictionOptionsProfilePrefs( |
43 | user_prefs::PrefRegistrySyncable* registry) { | ||||
44 | registry->RegisterIntegerPref( | ||||
45 | prefs::kNetworkPredictionOptions, | ||||
bnc | c1a560b6 | 2014-08-29 12:54:23 | [diff] [blame] | 46 | NETWORK_PREDICTION_DEFAULT, |
[email protected] | ba85a60 | 2014-06-28 20:37:12 | [diff] [blame] | 47 | user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); |
48 | } | ||||
49 | |||||
[email protected] | 16b061fe | 2014-08-13 15:54:05 | [diff] [blame] | 50 | bool CanPrefetchAndPrerenderIO(ProfileIOData* profile_io_data) { |
anujk.sharma | 2e02ce16 | 2015-04-29 23:10:02 | [diff] [blame] | 51 | DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
[email protected] | a21eaff | 2014-06-30 16:58:02 | [diff] [blame] | 52 | DCHECK(profile_io_data); |
[email protected] | 16b061fe | 2014-08-13 15:54:05 | [diff] [blame] | 53 | return CanPrefetchAndPrerender( |
bnc | c1a560b6 | 2014-08-29 12:54:23 | [diff] [blame] | 54 | profile_io_data->network_prediction_options()->GetValue()); |
[email protected] | ba85a60 | 2014-06-28 20:37:12 | [diff] [blame] | 55 | } |
56 | |||||
[email protected] | 16b061fe | 2014-08-13 15:54:05 | [diff] [blame] | 57 | bool CanPrefetchAndPrerenderUI(PrefService* prefs) { |
anujk.sharma | 2e02ce16 | 2015-04-29 23:10:02 | [diff] [blame] | 58 | DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
[email protected] | ba85a60 | 2014-06-28 20:37:12 | [diff] [blame] | 59 | DCHECK(prefs); |
[email protected] | 16b061fe | 2014-08-13 15:54:05 | [diff] [blame] | 60 | return CanPrefetchAndPrerender( |
bnc | c1a560b6 | 2014-08-29 12:54:23 | [diff] [blame] | 61 | prefs->GetInteger(prefs::kNetworkPredictionOptions)); |
[email protected] | 16b061fe | 2014-08-13 15:54:05 | [diff] [blame] | 62 | } |
63 | |||||
[email protected] | 16b061fe | 2014-08-13 15:54:05 | [diff] [blame] | 64 | bool CanPreresolveAndPreconnectIO(ProfileIOData* profile_io_data) { |
anujk.sharma | 2e02ce16 | 2015-04-29 23:10:02 | [diff] [blame] | 65 | DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
[email protected] | 16b061fe | 2014-08-13 15:54:05 | [diff] [blame] | 66 | DCHECK(profile_io_data); |
67 | return CanPreresolveAndPreconnect( | ||||
bnc | c1a560b6 | 2014-08-29 12:54:23 | [diff] [blame] | 68 | profile_io_data->network_prediction_options()->GetValue()); |
[email protected] | 16b061fe | 2014-08-13 15:54:05 | [diff] [blame] | 69 | } |
70 | |||||
71 | bool CanPreresolveAndPreconnectUI(PrefService* prefs) { | ||||
anujk.sharma | 2e02ce16 | 2015-04-29 23:10:02 | [diff] [blame] | 72 | DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
[email protected] | 16b061fe | 2014-08-13 15:54:05 | [diff] [blame] | 73 | DCHECK(prefs); |
74 | return CanPreresolveAndPreconnect( | ||||
bnc | c1a560b6 | 2014-08-29 12:54:23 | [diff] [blame] | 75 | prefs->GetInteger(prefs::kNetworkPredictionOptions)); |
[email protected] | ba85a60 | 2014-06-28 20:37:12 | [diff] [blame] | 76 | } |
77 | |||||
78 | } // namespace chrome_browser_net |