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.
5 #include "chrome/browser/net/prediction_options.h"
7 #include "base/logging.h"
8 #include "base/prefs/pref_service.h"
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"
13 #include "net/base/network_change_notifier.h"
15 namespace chrome_browser_net
{
19 // Since looking up preferences and current network connection are presumably
20 // both cheap, we do not cache them here.
21 bool CanPrefetchAndPrerender(int network_prediction_options
) {
22 switch (network_prediction_options
) {
23 case NETWORK_PREDICTION_ALWAYS
:
25 case NETWORK_PREDICTION_NEVER
:
28 DCHECK_EQ(NETWORK_PREDICTION_WIFI_ONLY
, network_prediction_options
);
29 return !net::NetworkChangeNotifier::IsConnectionCellular(
30 net::NetworkChangeNotifier::GetConnectionType());
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
;
42 void RegisterPredictionOptionsProfilePrefs(
43 user_prefs::PrefRegistrySyncable
* registry
) {
44 registry
->RegisterIntegerPref(
45 prefs::kNetworkPredictionOptions
,
46 NETWORK_PREDICTION_DEFAULT
,
47 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF
);
50 void MigrateNetworkPredictionUserPrefs(PrefService
* pref_service
) {
51 // Nothing to do if the user or this migration code has already set the new
53 if (pref_service
->GetUserPrefValue(prefs::kNetworkPredictionOptions
))
56 // Nothing to do if the user has not set the old preference.
57 const base::Value
* network_prediction_enabled
=
58 pref_service
->GetUserPrefValue(prefs::kNetworkPredictionEnabled
);
59 if (!network_prediction_enabled
)
63 if (network_prediction_enabled
->GetAsBoolean(&value
)) {
64 pref_service
->SetInteger(
65 prefs::kNetworkPredictionOptions
,
66 value
? NETWORK_PREDICTION_WIFI_ONLY
: NETWORK_PREDICTION_NEVER
);
70 bool CanPrefetchAndPrerenderIO(ProfileIOData
* profile_io_data
) {
71 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO
));
72 DCHECK(profile_io_data
);
73 return CanPrefetchAndPrerender(
74 profile_io_data
->network_prediction_options()->GetValue());
77 bool CanPrefetchAndPrerenderUI(PrefService
* prefs
) {
78 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI
));
80 return CanPrefetchAndPrerender(
81 prefs
->GetInteger(prefs::kNetworkPredictionOptions
));
84 bool CanPreresolveAndPreconnectIO(ProfileIOData
* profile_io_data
) {
85 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO
));
86 DCHECK(profile_io_data
);
87 return CanPreresolveAndPreconnect(
88 profile_io_data
->network_prediction_options()->GetValue());
91 bool CanPreresolveAndPreconnectUI(PrefService
* prefs
) {
92 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI
));
94 return CanPreresolveAndPreconnect(
95 prefs
->GetInteger(prefs::kNetworkPredictionOptions
));
98 } // namespace chrome_browser_net