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"
17 // Since looking up preferences and current network connection are presumably
18 // both cheap, we do not cache them here.
19 bool CanPrefetchAndPrerender(int network_prediction_options,
20 bool network_prediction_enabled) {
21 switch (network_prediction_options) {
22 case chrome_browser_net::NETWORK_PREDICTION_ALWAYS:
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:
29 case chrome_browser_net::NETWORK_PREDICTION_UNSET:
30 return network_prediction_enabled;
32 NOTREACHED() << "Unknown kNetworkPredictionOptions value.";
37 bool CanPreresolveAndPreconnect(int network_prediction_options,
38 bool network_prediction_enabled) {
39 switch (network_prediction_options) {
40 case chrome_browser_net::NETWORK_PREDICTION_ALWAYS:
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:
46 case chrome_browser_net::NETWORK_PREDICTION_NEVER:
48 case chrome_browser_net::NETWORK_PREDICTION_UNSET:
49 return network_prediction_enabled;
51 NOTREACHED() << "Unknown kNetworkPredictionOptions value.";
58 namespace chrome_browser_net {
60 void RegisterPredictionOptionsProfilePrefs(
61 user_prefs::PrefRegistrySyncable* registry) {
62 registry->RegisterIntegerPref(
63 prefs::kNetworkPredictionOptions,
64 chrome_browser_net::NETWORK_PREDICTION_UNSET,
65 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
68 void MigrateNetworkPredictionUserPrefs(PrefService* pref_service) {
69 // Nothing to do if the user or this migration code has already set the new
71 if (pref_service->GetUserPrefValue(prefs::kNetworkPredictionOptions))
74 // Nothing to do if the user has not set the old preference.
75 const base::Value* network_prediction_enabled =
76 pref_service->GetUserPrefValue(prefs::kNetworkPredictionEnabled);
77 if (!network_prediction_enabled)
81 if (network_prediction_enabled->GetAsBoolean(&value)) {
82 pref_service->SetInteger(
83 prefs::kNetworkPredictionOptions,
84 value ? chrome_browser_net::NETWORK_PREDICTION_WIFI_ONLY
85 : chrome_browser_net::NETWORK_PREDICTION_NEVER);
89 bool CanPrefetchAndPrerenderIO(ProfileIOData* profile_io_data) {
90 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
91 DCHECK(profile_io_data);
92 return CanPrefetchAndPrerender(
93 profile_io_data->network_prediction_options()->GetValue(),
94 profile_io_data->network_prediction_enabled()->GetValue());
97 bool CanPrefetchAndPrerenderUI(PrefService* prefs) {
98 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
100 return CanPrefetchAndPrerender(
101 prefs->GetInteger(prefs::kNetworkPredictionOptions),
102 prefs->GetBoolean(prefs::kNetworkPredictionEnabled));
105 bool CanPredictNetworkActionsUI(PrefService* prefs) {
106 return CanPrefetchAndPrerenderUI(prefs);
109 bool CanPreresolveAndPreconnectIO(ProfileIOData* profile_io_data) {
110 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
111 DCHECK(profile_io_data);
112 return CanPreresolveAndPreconnect(
113 profile_io_data->network_prediction_options()->GetValue(),
114 profile_io_data->network_prediction_enabled()->GetValue());
117 bool CanPreresolveAndPreconnectUI(PrefService* prefs) {
118 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
120 return CanPreresolveAndPreconnect(
121 prefs->GetInteger(prefs::kNetworkPredictionOptions),
122 prefs->GetBoolean(prefs::kNetworkPredictionEnabled));
125 } // namespace chrome_browser_net