summaryrefslogtreecommitdiffstats
path: root/chrome/browser/net/prediction_options.cc
blob: a01f5ee1144643b4286932f7b0a086fb73a7719e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/net/prediction_options.h"

#include "base/logging.h"
#include "base/prefs/pref_service.h"
#include "chrome/browser/profiles/profile_io_data.h"
#include "chrome/common/pref_names.h"
#include "components/pref_registry/pref_registry_syncable.h"
#include "content/public/browser/browser_thread.h"
#include "net/base/network_change_notifier.h"

namespace {

// Since looking up preferences and current network connection are presumably
// both cheap, we do not cache them here.
bool CanPrefetchAndPrerender(int network_prediction_options,
                             bool network_prediction_enabled) {
  switch (network_prediction_options) {
    case chrome_browser_net::NETWORK_PREDICTION_ALWAYS:
      return true;
    case chrome_browser_net::NETWORK_PREDICTION_WIFI_ONLY:
      return !net::NetworkChangeNotifier::IsConnectionCellular(
          net::NetworkChangeNotifier::GetConnectionType());
    case chrome_browser_net::NETWORK_PREDICTION_NEVER:
      return false;
    case chrome_browser_net::NETWORK_PREDICTION_UNSET:
      return network_prediction_enabled;
    default:
      NOTREACHED() << "Unknown kNetworkPredictionOptions value.";
      return false;
  }
}

bool CanPreresolveAndPreconnect(int network_prediction_options,
                                bool network_prediction_enabled) {
  switch (network_prediction_options) {
    case chrome_browser_net::NETWORK_PREDICTION_ALWAYS:
      return true;
    // DNS preresolution and TCP preconnect are performed even on cellular
    // networks if the user setting is WIFI_ONLY.
    case chrome_browser_net::NETWORK_PREDICTION_WIFI_ONLY:
      return true;
    case chrome_browser_net::NETWORK_PREDICTION_NEVER:
      return false;
    case chrome_browser_net::NETWORK_PREDICTION_UNSET:
      return network_prediction_enabled;
    default:
      NOTREACHED() << "Unknown kNetworkPredictionOptions value.";
      return false;
  }
}

}  // namespace

namespace chrome_browser_net {

void RegisterPredictionOptionsProfilePrefs(
    user_prefs::PrefRegistrySyncable* registry) {
  registry->RegisterIntegerPref(
      prefs::kNetworkPredictionOptions,
      chrome_browser_net::NETWORK_PREDICTION_UNSET,
      user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
}

void MigrateNetworkPredictionUserPrefs(PrefService* pref_service) {
  // Nothing to do if the user or this migration code has already set the new
  // preference.
  if (pref_service->GetUserPrefValue(prefs::kNetworkPredictionOptions))
    return;

  // Nothing to do if the user has not set the old preference.
  const base::Value* network_prediction_enabled =
      pref_service->GetUserPrefValue(prefs::kNetworkPredictionEnabled);
  if (!network_prediction_enabled)
    return;

  bool value = false;
  if (network_prediction_enabled->GetAsBoolean(&value)) {
    pref_service->SetInteger(
        prefs::kNetworkPredictionOptions,
        value ? chrome_browser_net::NETWORK_PREDICTION_WIFI_ONLY
              : chrome_browser_net::NETWORK_PREDICTION_NEVER);
  }
}

bool CanPrefetchAndPrerenderIO(ProfileIOData* profile_io_data) {
  DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
  DCHECK(profile_io_data);
  return CanPrefetchAndPrerender(
      profile_io_data->network_prediction_options()->GetValue(),
      profile_io_data->network_prediction_enabled()->GetValue());
}

bool CanPrefetchAndPrerenderUI(PrefService* prefs) {
  DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
  DCHECK(prefs);
  return CanPrefetchAndPrerender(
      prefs->GetInteger(prefs::kNetworkPredictionOptions),
      prefs->GetBoolean(prefs::kNetworkPredictionEnabled));
}

bool CanPredictNetworkActionsUI(PrefService* prefs) {
  return CanPrefetchAndPrerenderUI(prefs);
}

bool CanPreresolveAndPreconnectIO(ProfileIOData* profile_io_data) {
  DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
  DCHECK(profile_io_data);
  return CanPreresolveAndPreconnect(
      profile_io_data->network_prediction_options()->GetValue(),
      profile_io_data->network_prediction_enabled()->GetValue());
}

bool CanPreresolveAndPreconnectUI(PrefService* prefs) {
  DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
  DCHECK(prefs);
  return CanPreresolveAndPreconnect(
      prefs->GetInteger(prefs::kNetworkPredictionOptions),
      prefs->GetBoolean(prefs::kNetworkPredictionEnabled));
}

}  // namespace chrome_browser_net