blob: 40abb5abbadf0948e1add54b4f81a72648a2f895 [file] [log] [blame]
[email protected]ba85a602014-06-28 20:37:121// 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]ba85a602014-06-28 20:37:129#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]619611c82014-07-29 21:58:1313#include "net/base/network_change_notifier.h"
[email protected]ba85a602014-06-28 20:37:1214
bncc1a560b62014-08-29 12:54:2315namespace chrome_browser_net {
16
[email protected]ba85a602014-06-28 20:37:1217namespace {
18
19// Since looking up preferences and current network connection are presumably
20// both cheap, we do not cache them here.
bncc1a560b62014-08-29 12:54:2321bool CanPrefetchAndPrerender(int network_prediction_options) {
[email protected]16b061fe2014-08-13 15:54:0522 switch (network_prediction_options) {
bncc1a560b62014-08-29 12:54:2323 case NETWORK_PREDICTION_ALWAYS:
[email protected]ba85a602014-06-28 20:37:1224 return true;
bncc1a560b62014-08-29 12:54:2325 case NETWORK_PREDICTION_NEVER:
[email protected]ba85a602014-06-28 20:37:1226 return false;
[email protected]16b061fe2014-08-13 15:54:0527 default:
bncc1a560b62014-08-29 12:54:2328 DCHECK_EQ(NETWORK_PREDICTION_WIFI_ONLY, network_prediction_options);
29 return !net::NetworkChangeNotifier::IsConnectionCellular(
30 net::NetworkChangeNotifier::GetConnectionType());
[email protected]16b061fe2014-08-13 15:54:0531 }
32}
33
bncc1a560b62014-08-29 12:54:2334bool 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]ba85a602014-06-28 20:37:1238}
39
40} // namespace
41
[email protected]ba85a602014-06-28 20:37:1242void RegisterPredictionOptionsProfilePrefs(
43 user_prefs::PrefRegistrySyncable* registry) {
44 registry->RegisterIntegerPref(
45 prefs::kNetworkPredictionOptions,
bncc1a560b62014-08-29 12:54:2346 NETWORK_PREDICTION_DEFAULT,
[email protected]ba85a602014-06-28 20:37:1247 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
48}
49
[email protected]16b061fe2014-08-13 15:54:0550bool CanPrefetchAndPrerenderIO(ProfileIOData* profile_io_data) {
anujk.sharma2e02ce162015-04-29 23:10:0251 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
[email protected]a21eaff2014-06-30 16:58:0252 DCHECK(profile_io_data);
[email protected]16b061fe2014-08-13 15:54:0553 return CanPrefetchAndPrerender(
bncc1a560b62014-08-29 12:54:2354 profile_io_data->network_prediction_options()->GetValue());
[email protected]ba85a602014-06-28 20:37:1255}
56
[email protected]16b061fe2014-08-13 15:54:0557bool CanPrefetchAndPrerenderUI(PrefService* prefs) {
anujk.sharma2e02ce162015-04-29 23:10:0258 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
[email protected]ba85a602014-06-28 20:37:1259 DCHECK(prefs);
[email protected]16b061fe2014-08-13 15:54:0560 return CanPrefetchAndPrerender(
bncc1a560b62014-08-29 12:54:2361 prefs->GetInteger(prefs::kNetworkPredictionOptions));
[email protected]16b061fe2014-08-13 15:54:0562}
63
[email protected]16b061fe2014-08-13 15:54:0564bool CanPreresolveAndPreconnectIO(ProfileIOData* profile_io_data) {
anujk.sharma2e02ce162015-04-29 23:10:0265 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
[email protected]16b061fe2014-08-13 15:54:0566 DCHECK(profile_io_data);
67 return CanPreresolveAndPreconnect(
bncc1a560b62014-08-29 12:54:2368 profile_io_data->network_prediction_options()->GetValue());
[email protected]16b061fe2014-08-13 15:54:0569}
70
71bool CanPreresolveAndPreconnectUI(PrefService* prefs) {
anujk.sharma2e02ce162015-04-29 23:10:0272 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
[email protected]16b061fe2014-08-13 15:54:0573 DCHECK(prefs);
74 return CanPreresolveAndPreconnect(
bncc1a560b62014-08-29 12:54:2375 prefs->GetInteger(prefs::kNetworkPredictionOptions));
[email protected]ba85a602014-06-28 20:37:1276}
77
78} // namespace chrome_browser_net