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 "third_party/cros_system_api/dbus/service_constants.h"
17 ManagedState::ManagedState(ManagedType type
, const std::string
& path
)
18 : managed_type_(type
),
20 update_received_(false),
21 update_requested_(false) {
24 ManagedState::~ManagedState() {
27 ManagedState
* ManagedState::Create(ManagedType type
, const std::string
& path
) {
29 case MANAGED_TYPE_NETWORK
:
30 return new NetworkState(path
);
31 case MANAGED_TYPE_FAVORITE
:
32 return new FavoriteState(path
);
33 case MANAGED_TYPE_DEVICE
:
34 return new DeviceState(path
);
39 NetworkState
* ManagedState::AsNetworkState() {
40 if (managed_type() == MANAGED_TYPE_NETWORK
)
41 return static_cast<NetworkState
*>(this);
45 DeviceState
* ManagedState::AsDeviceState() {
46 if (managed_type() == MANAGED_TYPE_DEVICE
)
47 return static_cast<DeviceState
*>(this);
51 FavoriteState
* ManagedState::AsFavoriteState() {
52 if (managed_type() == MANAGED_TYPE_FAVORITE
)
53 return static_cast<FavoriteState
*>(this);
57 bool ManagedState::InitialPropertiesReceived(
58 const base::DictionaryValue
& properties
) {
62 bool ManagedState::ManagedStatePropertyChanged(const std::string
& key
,
63 const base::Value
& value
) {
64 if (key
== flimflam::kNameProperty
) {
65 return GetStringValue(key
, value
, &name_
);
66 } else if (key
== flimflam::kTypeProperty
) {
67 return GetStringValue(key
, value
, &type_
);
72 bool ManagedState::GetBooleanValue(const std::string
& key
,
73 const base::Value
& value
,
76 if (!value
.GetAsBoolean(&new_value
)) {
77 NET_LOG_ERROR("Error parsing state value", path() + "." + key
);
80 if (*out_value
== new_value
)
82 *out_value
= new_value
;
86 bool ManagedState::GetIntegerValue(const std::string
& key
,
87 const base::Value
& value
,
90 if (!value
.GetAsInteger(&new_value
)) {
91 NET_LOG_ERROR("Error parsing state value", path() + "." + key
);
94 if (*out_value
== new_value
)
96 *out_value
= new_value
;
100 bool ManagedState::GetStringValue(const std::string
& key
,
101 const base::Value
& value
,
102 std::string
* out_value
) {
103 std::string new_value
;
104 if (!value
.GetAsString(&new_value
)) {
105 NET_LOG_ERROR("Error parsing state value", path() + "." + key
);
108 if (*out_value
== new_value
)
110 *out_value
= new_value
;
114 bool ManagedState::GetUInt32Value(const std::string
& key
,
115 const base::Value
& value
,
117 // base::Value restricts the number types to BOOL, INTEGER, and DOUBLE only.
118 // uint32 will automatically get converted to a double, which is why we try
119 // to obtain the value as a double (see dbus/values_util.h).
122 if (!value
.GetAsDouble(&double_value
) || double_value
< 0) {
123 NET_LOG_ERROR("Error parsing state value", path() + "." + key
);
126 new_value
= static_cast<uint32
>(double_value
);
127 if (*out_value
== new_value
)
129 *out_value
= new_value
;
133 } // namespace chromeos