Home | History | Annotate | Download | only in net
      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"
      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"
     14 
     15 namespace chrome_browser_net {
     16 
     17 namespace {
     18 
     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:
     24       return true;
     25     case NETWORK_PREDICTION_NEVER:
     26       return false;
     27     default:
     28       DCHECK_EQ(NETWORK_PREDICTION_WIFI_ONLY, network_prediction_options);
     29       return !net::NetworkChangeNotifier::IsConnectionCellular(
     30                  net::NetworkChangeNotifier::GetConnectionType());
     31   }
     32 }
     33 
     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;
     38 }
     39 
     40 }  // namespace
     41 
     42 void RegisterPredictionOptionsProfilePrefs(
     43     user_prefs::PrefRegistrySyncable* registry) {
     44   registry->RegisterIntegerPref(
     45       prefs::kNetworkPredictionOptions,
     46       NETWORK_PREDICTION_DEFAULT,
     47       user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
     48 }
     49 
     50 void MigrateNetworkPredictionUserPrefs(PrefService* pref_service) {
     51   // Nothing to do if the user or this migration code has already set the new
     52   // preference.
     53   if (pref_service->GetUserPrefValue(prefs::kNetworkPredictionOptions))
     54     return;
     55 
     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)
     60     return;
     61 
     62   bool value = false;
     63   if (network_prediction_enabled->GetAsBoolean(&value)) {
     64     pref_service->SetInteger(
     65         prefs::kNetworkPredictionOptions,
     66         value ? NETWORK_PREDICTION_WIFI_ONLY : NETWORK_PREDICTION_NEVER);
     67   }
     68 }
     69 
     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());
     75 }
     76 
     77 bool CanPrefetchAndPrerenderUI(PrefService* prefs) {
     78   DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
     79   DCHECK(prefs);
     80   return CanPrefetchAndPrerender(
     81       prefs->GetInteger(prefs::kNetworkPredictionOptions));
     82 }
     83 
     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());
     89 }
     90 
     91 bool CanPreresolveAndPreconnectUI(PrefService* prefs) {
     92   DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
     93   DCHECK(prefs);
     94   return CanPreresolveAndPreconnect(
     95       prefs->GetInteger(prefs::kNetworkPredictionOptions));
     96 }
     97 
     98 }  // namespace chrome_browser_net
     99