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"
17 bool ManagedState::Matches(const NetworkTypePattern
& pattern
) const {
18 return pattern
.MatchesType(type());
22 std::string
ManagedState::TypeToString(ManagedType type
) {
24 case MANAGED_TYPE_NETWORK
:
26 case MANAGED_TYPE_DEVICE
:
32 ManagedState::ManagedState(ManagedType type
, const std::string
& path
)
33 : managed_type_(type
),
35 update_received_(false),
36 update_requested_(false) {
39 ManagedState::~ManagedState() {
42 ManagedState
* ManagedState::Create(ManagedType type
, const std::string
& path
) {
44 case MANAGED_TYPE_NETWORK
:
45 return new NetworkState(path
);
46 case MANAGED_TYPE_DEVICE
:
47 return new DeviceState(path
);
52 NetworkState
* ManagedState::AsNetworkState() {
53 if (managed_type() == MANAGED_TYPE_NETWORK
)
54 return static_cast<NetworkState
*>(this);
58 DeviceState
* ManagedState::AsDeviceState() {
59 if (managed_type() == MANAGED_TYPE_DEVICE
)
60 return static_cast<DeviceState
*>(this);
64 bool ManagedState::InitialPropertiesReceived(
65 const base::DictionaryValue
& properties
) {
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_
);
84 bool ManagedState::GetBooleanValue(const std::string
& key
,
85 const base::Value
& value
,
88 if (!value
.GetAsBoolean(&new_value
)) {
89 NET_LOG_ERROR("Error parsing state value", path() + "." + key
);
92 if (*out_value
== new_value
)
94 *out_value
= new_value
;
98 bool ManagedState::GetIntegerValue(const std::string
& key
,
99 const base::Value
& value
,
102 if (!value
.GetAsInteger(&new_value
)) {
103 NET_LOG_ERROR("Error parsing state value", path() + "." + key
);
106 if (*out_value
== new_value
)
108 *out_value
= new_value
;
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());
120 if (*out_value
== new_value
)
122 *out_value
= new_value
;
126 bool ManagedState::GetUInt32Value(const std::string
& key
,
127 const base::Value
& 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).
134 if (!value
.GetAsDouble(&double_value
) || double_value
< 0) {
135 NET_LOG_ERROR("Error parsing state value", path() + "." + key
);
138 new_value
= static_cast<uint32
>(double_value
);
139 if (*out_value
== new_value
)
141 *out_value
= new_value
;
145 } // namespace chromeos