blob: cedddf325840917f8a9915f004d76072f769bd15 [file] [log] [blame]
[email protected]3c8bd112012-11-07 10:14:591// 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.
4
5#include "chromeos/network/managed_state.h"
6
7#include "base/logging.h"
8#include "base/values.h"
9#include "chromeos/network/device_state.h"
[email protected]ce21fc62013-07-03 10:45:5810#include "chromeos/network/favorite_state.h"
[email protected]5092ea872013-05-16 21:56:0811#include "chromeos/network/network_event_log.h"
[email protected]3c8bd112012-11-07 10:14:5912#include "chromeos/network/network_state.h"
[email protected]184bd2d2013-09-11 20:38:0413#include "chromeos/network/shill_property_util.h"
[email protected]3c8bd112012-11-07 10:14:5914#include "third_party/cros_system_api/dbus/service_constants.h"
15
16namespace chromeos {
17
[email protected]184bd2d2013-09-11 20:38:0418bool ManagedState::Matches(const NetworkTypePattern& pattern) const {
19 return pattern.MatchesType(type());
20}
21
[email protected]3c8bd112012-11-07 10:14:5922ManagedState::ManagedState(ManagedType type, const std::string& path)
23 : managed_type_(type),
24 path_(path),
[email protected]7b6f21d2013-08-05 17:36:3725 update_received_(false),
[email protected]6f821df2013-05-22 21:22:0226 update_requested_(false) {
[email protected]3c8bd112012-11-07 10:14:5927}
28
29ManagedState::~ManagedState() {
30}
31
32ManagedState* ManagedState::Create(ManagedType type, const std::string& path) {
[email protected]5092ea872013-05-16 21:56:0833 switch (type) {
[email protected]3c8bd112012-11-07 10:14:5934 case MANAGED_TYPE_NETWORK:
35 return new NetworkState(path);
[email protected]ce21fc62013-07-03 10:45:5836 case MANAGED_TYPE_FAVORITE:
37 return new FavoriteState(path);
[email protected]3c8bd112012-11-07 10:14:5938 case MANAGED_TYPE_DEVICE:
39 return new DeviceState(path);
40 }
41 return NULL;
42}
43
44NetworkState* ManagedState::AsNetworkState() {
45 if (managed_type() == MANAGED_TYPE_NETWORK)
46 return static_cast<NetworkState*>(this);
47 return NULL;
48}
49
50DeviceState* ManagedState::AsDeviceState() {
51 if (managed_type() == MANAGED_TYPE_DEVICE)
52 return static_cast<DeviceState*>(this);
53 return NULL;
54}
55
[email protected]ce21fc62013-07-03 10:45:5856FavoriteState* ManagedState::AsFavoriteState() {
57 if (managed_type() == MANAGED_TYPE_FAVORITE)
58 return static_cast<FavoriteState*>(this);
59 return NULL;
60}
61
[email protected]577b22d02013-07-31 01:33:0262bool ManagedState::InitialPropertiesReceived(
63 const base::DictionaryValue& properties) {
64 return false;
[email protected]a7f1d1b2013-04-23 00:50:1565}
66
[email protected]3c8bd112012-11-07 10:14:5967bool ManagedState::ManagedStatePropertyChanged(const std::string& key,
68 const base::Value& value) {
69 if (key == flimflam::kNameProperty) {
70 return GetStringValue(key, value, &name_);
71 } else if (key == flimflam::kTypeProperty) {
72 return GetStringValue(key, value, &type_);
73 }
74 return false;
75}
76
77bool ManagedState::GetBooleanValue(const std::string& key,
78 const base::Value& value,
79 bool* out_value) {
[email protected]1c8edf12013-05-15 10:01:2680 bool new_value;
81 if (!value.GetAsBoolean(&new_value)) {
[email protected]5092ea872013-05-16 21:56:0882 NET_LOG_ERROR("Error parsing state value", path() + "." + key);
[email protected]1c8edf12013-05-15 10:01:2683 return false;
84 }
85 if (*out_value == new_value)
86 return false;
87 *out_value = new_value;
88 return true;
[email protected]3c8bd112012-11-07 10:14:5989}
90
91bool ManagedState::GetIntegerValue(const std::string& key,
92 const base::Value& value,
93 int* out_value) {
[email protected]1c8edf12013-05-15 10:01:2694 int new_value;
95 if (!value.GetAsInteger(&new_value)) {
[email protected]5092ea872013-05-16 21:56:0896 NET_LOG_ERROR("Error parsing state value", path() + "." + key);
[email protected]1c8edf12013-05-15 10:01:2697 return false;
98 }
99 if (*out_value == new_value)
100 return false;
101 *out_value = new_value;
102 return true;
[email protected]3c8bd112012-11-07 10:14:59103}
104
105bool ManagedState::GetStringValue(const std::string& key,
106 const base::Value& value,
107 std::string* out_value) {
[email protected]1c8edf12013-05-15 10:01:26108 std::string new_value;
109 if (!value.GetAsString(&new_value)) {
[email protected]5092ea872013-05-16 21:56:08110 NET_LOG_ERROR("Error parsing state value", path() + "." + key);
[email protected]1c8edf12013-05-15 10:01:26111 return false;
112 }
113 if (*out_value == new_value)
114 return false;
115 *out_value = new_value;
116 return true;
[email protected]3c8bd112012-11-07 10:14:59117}
118
[email protected]63014ca42013-07-10 04:55:26119bool ManagedState::GetUInt32Value(const std::string& key,
120 const base::Value& value,
121 uint32* out_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).
125 uint32 new_value;
126 double double_value;
127 if (!value.GetAsDouble(&double_value) || double_value < 0) {
128 NET_LOG_ERROR("Error parsing state value", path() + "." + key);
129 return false;
130 }
131 new_value = static_cast<uint32>(double_value);
132 if (*out_value == new_value)
133 return false;
134 *out_value = new_value;
135 return true;
136}
137
[email protected]3c8bd112012-11-07 10:14:59138} // namespace chromeos