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/favorite_state.h"
11 #include "chromeos/network/network_event_log.h"
12 #include "chromeos/network/network_state.h"
13 #include "chromeos/network/shill_property_util.h"
14 #include "third_party/cros_system_api/dbus/service_constants.h"
18 bool ManagedState::Matches(const NetworkTypePattern
& pattern
) const {
19 return pattern
.MatchesType(type());
22 ManagedState::ManagedState(ManagedType type
, const std::string
& path
)
23 : managed_type_(type
),
25 update_received_(false),
26 update_requested_(false) {
29 ManagedState::~ManagedState() {
32 ManagedState
* ManagedState::Create(ManagedType type
, const std::string
& path
) {
34 case MANAGED_TYPE_NETWORK
:
35 return new NetworkState(path
);
36 case MANAGED_TYPE_FAVORITE
:
37 return new FavoriteState(path
);
38 case MANAGED_TYPE_DEVICE
:
39 return new DeviceState(path
);
44 NetworkState
* ManagedState::AsNetworkState() {
45 if (managed_type() == MANAGED_TYPE_NETWORK
)
46 return static_cast<NetworkState
*>(this);
50 DeviceState
* ManagedState::AsDeviceState() {
51 if (managed_type() == MANAGED_TYPE_DEVICE
)
52 return static_cast<DeviceState
*>(this);
56 FavoriteState
* ManagedState::AsFavoriteState() {
57 if (managed_type() == MANAGED_TYPE_FAVORITE
)
58 return static_cast<FavoriteState
*>(this);
62 bool ManagedState::InitialPropertiesReceived(
63 const base::DictionaryValue
& properties
) {
67 bool ManagedState::ManagedStatePropertyChanged(const std::string
& key
,
68 const base::Value
& value
) {
69 if (key
== shill::kNameProperty
) {
70 return GetStringValue(key
, value
, &name_
);
71 } else if (key
== shill::kTypeProperty
) {
72 return GetStringValue(key
, value
, &type_
);
77 bool ManagedState::GetBooleanValue(const std::string
& key
,
78 const base::Value
& value
,
81 if (!value
.GetAsBoolean(&new_value
)) {
82 NET_LOG_ERROR("Error parsing state value", path() + "." + key
);
85 if (*out_value
== new_value
)
87 *out_value
= new_value
;
91 bool ManagedState::GetIntegerValue(const std::string
& key
,
92 const base::Value
& value
,
95 if (!value
.GetAsInteger(&new_value
)) {
96 NET_LOG_ERROR("Error parsing state value", path() + "." + key
);
99 if (*out_value
== new_value
)
101 *out_value
= new_value
;
105 bool ManagedState::GetStringValue(const std::string
& key
,
106 const base::Value
& value
,
107 std::string
* out_value
) {
108 std::string new_value
;
109 if (!value
.GetAsString(&new_value
)) {
110 NET_LOG_ERROR("Error parsing state value", path() + "." + key
);
113 if (*out_value
== new_value
)
115 *out_value
= new_value
;
119 bool ManagedState::GetUInt32Value(const std::string
& key
,
120 const base::Value
& value
,
122 // base::Value restricts the number types to BOOL, INTEGER, and DOUBLE only.
123 // uint32 will automatically get converted to a double, which is why we try
124 // to obtain the value as a double (see dbus/values_util.h).
127 if (!value
.GetAsDouble(&double_value
) || double_value
< 0) {
128 NET_LOG_ERROR("Error parsing state value", path() + "." + key
);
131 new_value
= static_cast<uint32
>(double_value
);
132 if (*out_value
== new_value
)
134 *out_value
= new_value
;
138 } // namespace chromeos