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/policy/network_prediction_policy_handler.h"
6
7#include "base/prefs/pref_value_map.h"
8#include "base/values.h"
9#include "chrome/browser/net/prediction_options.h"
10#include "chrome/common/pref_names.h"
11#include "components/policy/core/browser/policy_error_map.h"
12#include "components/policy/core/common/policy_map.h"
13#include "grit/components_strings.h"
14#include "policy/policy_constants.h"
15
16namespace policy {
17
18NetworkPredictionPolicyHandler::NetworkPredictionPolicyHandler() {
19}
20
21NetworkPredictionPolicyHandler::~NetworkPredictionPolicyHandler() {
22}
23
24bool NetworkPredictionPolicyHandler::CheckPolicySettings(
25    const PolicyMap& policies,
26    PolicyErrorMap* errors) {
27  // Deprecated boolean preference.
28  const base::Value* network_prediction_enabled =
29      policies.GetValue(key::kDnsPrefetchingEnabled);
30  // New enumerated preference.
31  const base::Value* network_prediction_options =
32      policies.GetValue(key::kNetworkPredictionOptions);
33
34  if (network_prediction_enabled &&
35      !network_prediction_enabled->IsType(base::Value::TYPE_BOOLEAN)) {
36    errors->AddError(key::kDnsPrefetchingEnabled,
37                     IDS_POLICY_TYPE_ERROR,
38                     ValueTypeToString(base::Value::TYPE_BOOLEAN));
39  }
40
41  if (network_prediction_options &&
42      !network_prediction_options->IsType(base::Value::TYPE_INTEGER)) {
43    errors->AddError(key::kNetworkPredictionOptions,
44                     IDS_POLICY_TYPE_ERROR,
45                     ValueTypeToString(base::Value::TYPE_INTEGER));
46  }
47
48  if (network_prediction_enabled && network_prediction_options) {
49    errors->AddError(key::kDnsPrefetchingEnabled,
50                     IDS_POLICY_OVERRIDDEN,
51                     key::kNetworkPredictionOptions);
52  }
53
54  return true;
55}
56
57void NetworkPredictionPolicyHandler::ApplyPolicySettings(
58    const PolicyMap& policies,
59    PrefValueMap* prefs) {
60  // If new preference is managed by policy, apply it to both preferences.
61  const base::Value* network_prediction_options =
62      policies.GetValue(key::kNetworkPredictionOptions);
63  int int_setting;
64  if (network_prediction_options &&
65      network_prediction_options->GetAsInteger(&int_setting)) {
66    prefs->SetInteger(prefs::kNetworkPredictionOptions, int_setting);
67    // Be conservative here and only set Enabled if policy says ALWAYS.
68    prefs->SetBoolean(
69        prefs::kNetworkPredictionEnabled,
70        int_setting == chrome_browser_net::NETWORK_PREDICTION_ALWAYS);
71    return;
72  }
73
74  // If deprecated preference is managed by policy, apply it to both
75  // preferences.
76  const base::Value* network_prediction_enabled =
77      policies.GetValue(key::kDnsPrefetchingEnabled);
78  bool bool_setting;
79  if (network_prediction_enabled &&
80      network_prediction_enabled->GetAsBoolean(&bool_setting)) {
81    prefs->SetBoolean(prefs::kNetworkPredictionEnabled, bool_setting);
82    // Some predictive network actions, most notably prefetch, used to be
83    // hardwired never to run on cellular network.  In order to retain this
84    // behavior (unless explicitly overriden by kNetworkPredictionOptions),
85    // kNetworkPredictionEnabled = true is translated to
86    // kNetworkPredictionOptions = WIFI_ONLY.
87    prefs->SetInteger(prefs::kNetworkPredictionOptions,
88                      bool_setting
89                          ? chrome_browser_net::NETWORK_PREDICTION_WIFI_ONLY
90                          : chrome_browser_net::NETWORK_PREDICTION_NEVER);
91  }
92}
93
94}  // namespace policy
95