1// Copyright 2013 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/chromeos/policy/device_network_configuration_updater.h"
6
7#include "base/bind.h"
8#include "base/bind_helpers.h"
9#include "chrome/browser/chromeos/settings/cros_settings.h"
10#include "chromeos/network/managed_network_configuration_handler.h"
11#include "chromeos/network/network_device_handler.h"
12#include "chromeos/settings/cros_settings_names.h"
13#include "chromeos/settings/cros_settings_provider.h"
14#include "policy/policy_constants.h"
15
16namespace policy {
17
18DeviceNetworkConfigurationUpdater::~DeviceNetworkConfigurationUpdater() {}
19
20// static
21scoped_ptr<DeviceNetworkConfigurationUpdater>
22DeviceNetworkConfigurationUpdater::CreateForDevicePolicy(
23    PolicyService* policy_service,
24    chromeos::ManagedNetworkConfigurationHandler* network_config_handler,
25    chromeos::NetworkDeviceHandler* network_device_handler,
26    chromeos::CrosSettings* cros_settings) {
27  scoped_ptr<DeviceNetworkConfigurationUpdater> updater(
28      new DeviceNetworkConfigurationUpdater(policy_service,
29                                            network_config_handler,
30                                            network_device_handler,
31                                            cros_settings));
32  updater->Init();
33  return updater.Pass();
34}
35
36DeviceNetworkConfigurationUpdater::DeviceNetworkConfigurationUpdater(
37    PolicyService* policy_service,
38    chromeos::ManagedNetworkConfigurationHandler* network_config_handler,
39    chromeos::NetworkDeviceHandler* network_device_handler,
40    chromeos::CrosSettings* cros_settings)
41    : NetworkConfigurationUpdater(onc::ONC_SOURCE_DEVICE_POLICY,
42                                  key::kDeviceOpenNetworkConfiguration,
43                                  policy_service,
44                                  network_config_handler),
45      network_device_handler_(network_device_handler),
46      cros_settings_(cros_settings),
47      weak_factory_(this) {
48  DCHECK(network_device_handler_);
49  data_roaming_setting_subscription_ = cros_settings->AddSettingsObserver(
50      chromeos::kSignedDataRoamingEnabled,
51      base::Bind(
52          &DeviceNetworkConfigurationUpdater::OnDataRoamingSettingChanged,
53          base::Unretained(this)));
54}
55
56void DeviceNetworkConfigurationUpdater::Init() {
57  NetworkConfigurationUpdater::Init();
58
59  // Apply the roaming setting initially.
60  OnDataRoamingSettingChanged();
61}
62
63void DeviceNetworkConfigurationUpdater::ImportCertificates(
64    const base::ListValue& certificates_onc) {
65  // Importing CA and server certs from device policy is not  allowed, while
66  // importing client is not yet supported (as a system-wide PKCS#11 token to
67  // which they should be imported does not exists at the time).
68}
69
70void DeviceNetworkConfigurationUpdater::ApplyNetworkPolicy(
71    base::ListValue* network_configs_onc,
72    base::DictionaryValue* global_network_config) {
73  network_config_handler_->SetPolicy(onc_source_,
74                                     std::string() /* no username hash */,
75                                     *network_configs_onc,
76                                     *global_network_config);
77}
78
79void DeviceNetworkConfigurationUpdater::OnDataRoamingSettingChanged() {
80  chromeos::CrosSettingsProvider::TrustedStatus trusted_status =
81      cros_settings_->PrepareTrustedValues(base::Bind(
82          &DeviceNetworkConfigurationUpdater::OnDataRoamingSettingChanged,
83          weak_factory_.GetWeakPtr()));
84
85  if (trusted_status == chromeos::CrosSettingsProvider::TEMPORARILY_UNTRUSTED) {
86    // Return, this function will be called again later by
87    // PrepareTrustedValues.
88    return;
89  }
90
91  bool data_roaming_setting = false;
92  if (trusted_status == chromeos::CrosSettingsProvider::TRUSTED) {
93    if (!cros_settings_->GetBoolean(chromeos::kSignedDataRoamingEnabled,
94                                    &data_roaming_setting)) {
95      LOG(ERROR) << "Couldn't get device setting "
96                 << chromeos::kSignedDataRoamingEnabled;
97      data_roaming_setting = false;
98    }
99  } else {
100    DCHECK_EQ(trusted_status,
101              chromeos::CrosSettingsProvider::PERMANENTLY_UNTRUSTED);
102    // Roaming is disabled as we can't determine the correct setting.
103  }
104
105  network_device_handler_->SetCellularAllowRoaming(data_roaming_setting);
106}
107
108}  // namespace policy
109