Some additional network settings cleanup
[chromium-blink-merge.git] / chromeos / network / managed_state.cc
blob75fce5bed3864bb0e955df26fabc7aaf100bc027
1 // Copyright (c) 2012 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.
5 #include "chromeos/network/managed_state.h"
7 #include "base/logging.h"
8 #include "base/values.h"
9 #include "chromeos/network/device_state.h"
10 #include "chromeos/network/network_event_log.h"
11 #include "chromeos/network/network_state.h"
12 #include "chromeos/network/network_type_pattern.h"
13 #include "third_party/cros_system_api/dbus/service_constants.h"
15 namespace chromeos {
17 bool ManagedState::Matches(const NetworkTypePattern& pattern) const {
18 return pattern.MatchesType(type());
21 // static
22 std::string ManagedState::TypeToString(ManagedType type) {
23 switch (type) {
24 case MANAGED_TYPE_NETWORK:
25 return "Network";
26 case MANAGED_TYPE_DEVICE:
27 return "Device";
29 return "Unknown";
32 ManagedState::ManagedState(ManagedType type, const std::string& path)
33 : managed_type_(type),
34 path_(path),
35 update_received_(false),
36 update_requested_(false) {
39 ManagedState::~ManagedState() {
42 ManagedState* ManagedState::Create(ManagedType type, const std::string& path) {
43 switch (type) {
44 case MANAGED_TYPE_NETWORK:
45 return new NetworkState(path);
46 case MANAGED_TYPE_DEVICE:
47 return new DeviceState(path);
49 return NULL;
52 NetworkState* ManagedState::AsNetworkState() {
53 if (managed_type() == MANAGED_TYPE_NETWORK)
54 return static_cast<NetworkState*>(this);
55 return NULL;
58 DeviceState* ManagedState::AsDeviceState() {
59 if (managed_type() == MANAGED_TYPE_DEVICE)
60 return static_cast<DeviceState*>(this);
61 return NULL;
64 bool ManagedState::InitialPropertiesReceived(
65 const base::DictionaryValue& properties) {
66 return false;
69 void ManagedState::GetStateProperties(base::DictionaryValue* dictionary) const {
70 dictionary->SetStringWithoutPathExpansion(shill::kNameProperty, name());
71 dictionary->SetStringWithoutPathExpansion(shill::kTypeProperty, type());
74 bool ManagedState::ManagedStatePropertyChanged(const std::string& key,
75 const base::Value& value) {
76 if (key == shill::kNameProperty) {
77 return GetStringValue(key, value, &name_);
78 } else if (key == shill::kTypeProperty) {
79 return GetStringValue(key, value, &type_);
81 return false;
84 bool ManagedState::GetBooleanValue(const std::string& key,
85 const base::Value& value,
86 bool* out_value) {
87 bool new_value;
88 if (!value.GetAsBoolean(&new_value)) {
89 NET_LOG_ERROR("Error parsing state value", path() + "." + key);
90 return false;
92 if (*out_value == new_value)
93 return false;
94 *out_value = new_value;
95 return true;
98 bool ManagedState::GetIntegerValue(const std::string& key,
99 const base::Value& value,
100 int* out_value) {
101 int new_value;
102 if (!value.GetAsInteger(&new_value)) {
103 NET_LOG_ERROR("Error parsing state value", path() + "." + key);
104 return false;
106 if (*out_value == new_value)
107 return false;
108 *out_value = new_value;
109 return true;
112 bool ManagedState::GetStringValue(const std::string& key,
113 const base::Value& value,
114 std::string* out_value) {
115 std::string new_value;
116 if (!value.GetAsString(&new_value)) {
117 NET_LOG_ERROR("Error parsing state: " + key, path());
118 return false;
120 if (*out_value == new_value)
121 return false;
122 *out_value = new_value;
123 return true;
126 bool ManagedState::GetUInt32Value(const std::string& key,
127 const base::Value& value,
128 uint32* out_value) {
129 // base::Value restricts the number types to BOOL, INTEGER, and DOUBLE only.
130 // uint32 will automatically get converted to a double, which is why we try
131 // to obtain the value as a double (see dbus/values_util.h).
132 uint32 new_value;
133 double double_value;
134 if (!value.GetAsDouble(&double_value) || double_value < 0) {
135 NET_LOG_ERROR("Error parsing state value", path() + "." + key);
136 return false;
138 new_value = static_cast<uint32>(double_value);
139 if (*out_value == new_value)
140 return false;
141 *out_value = new_value;
142 return true;
145 } // namespace chromeos