blob: 60bee165240df2ed1bdb0dae6918311825af095e [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"
13#include "third_party/cros_system_api/dbus/service_constants.h"
14
15namespace chromeos {
16
17ManagedState::ManagedState(ManagedType type, const std::string& path)
18 : managed_type_(type),
19 path_(path),
[email protected]6f821df2013-05-22 21:22:0220 update_requested_(false) {
[email protected]3c8bd112012-11-07 10:14:5921}
22
23ManagedState::~ManagedState() {
24}
25
26ManagedState* ManagedState::Create(ManagedType type, const std::string& path) {
[email protected]5092ea872013-05-16 21:56:0827 switch (type) {
[email protected]3c8bd112012-11-07 10:14:5928 case MANAGED_TYPE_NETWORK:
29 return new NetworkState(path);
[email protected]ce21fc62013-07-03 10:45:5830 case MANAGED_TYPE_FAVORITE:
31 return new FavoriteState(path);
[email protected]3c8bd112012-11-07 10:14:5932 case MANAGED_TYPE_DEVICE:
33 return new DeviceState(path);
34 }
35 return NULL;
36}
37
38NetworkState* ManagedState::AsNetworkState() {
39 if (managed_type() == MANAGED_TYPE_NETWORK)
40 return static_cast<NetworkState*>(this);
41 return NULL;
42}
43
44DeviceState* ManagedState::AsDeviceState() {
45 if (managed_type() == MANAGED_TYPE_DEVICE)
46 return static_cast<DeviceState*>(this);
47 return NULL;
48}
49
[email protected]ce21fc62013-07-03 10:45:5850FavoriteState* ManagedState::AsFavoriteState() {
51 if (managed_type() == MANAGED_TYPE_FAVORITE)
52 return static_cast<FavoriteState*>(this);
53 return NULL;
54}
55
[email protected]577b22d02013-07-31 01:33:0256bool ManagedState::InitialPropertiesReceived(
57 const base::DictionaryValue& properties) {
58 return false;
[email protected]a7f1d1b2013-04-23 00:50:1559}
60
[email protected]3c8bd112012-11-07 10:14:5961bool ManagedState::ManagedStatePropertyChanged(const std::string& key,
62 const base::Value& value) {
63 if (key == flimflam::kNameProperty) {
64 return GetStringValue(key, value, &name_);
65 } else if (key == flimflam::kTypeProperty) {
66 return GetStringValue(key, value, &type_);
67 }
68 return false;
69}
70
71bool ManagedState::GetBooleanValue(const std::string& key,
72 const base::Value& value,
73 bool* out_value) {
[email protected]1c8edf12013-05-15 10:01:2674 bool new_value;
75 if (!value.GetAsBoolean(&new_value)) {
[email protected]5092ea872013-05-16 21:56:0876 NET_LOG_ERROR("Error parsing state value", path() + "." + key);
[email protected]1c8edf12013-05-15 10:01:2677 return false;
78 }
79 if (*out_value == new_value)
80 return false;
81 *out_value = new_value;
82 return true;
[email protected]3c8bd112012-11-07 10:14:5983}
84
85bool ManagedState::GetIntegerValue(const std::string& key,
86 const base::Value& value,
87 int* out_value) {
[email protected]1c8edf12013-05-15 10:01:2688 int new_value;
89 if (!value.GetAsInteger(&new_value)) {
[email protected]5092ea872013-05-16 21:56:0890 NET_LOG_ERROR("Error parsing state value", path() + "." + key);
[email protected]1c8edf12013-05-15 10:01:2691 return false;
92 }
93 if (*out_value == new_value)
94 return false;
95 *out_value = new_value;
96 return true;
[email protected]3c8bd112012-11-07 10:14:5997}
98
99bool ManagedState::GetStringValue(const std::string& key,
100 const base::Value& value,
101 std::string* out_value) {
[email protected]1c8edf12013-05-15 10:01:26102 std::string new_value;
103 if (!value.GetAsString(&new_value)) {
[email protected]5092ea872013-05-16 21:56:08104 NET_LOG_ERROR("Error parsing state value", path() + "." + key);
[email protected]1c8edf12013-05-15 10:01:26105 return false;
106 }
107 if (*out_value == new_value)
108 return false;
109 *out_value = new_value;
110 return true;
[email protected]3c8bd112012-11-07 10:14:59111}
112
[email protected]63014ca42013-07-10 04:55:26113bool ManagedState::GetUInt32Value(const std::string& key,
114 const base::Value& value,
115 uint32* out_value) {
116 // base::Value restricts the number types to BOOL, INTEGER, and DOUBLE only.
117 // uint32 will automatically get converted to a double, which is why we try
118 // to obtain the value as a double (see dbus/values_util.h).
119 uint32 new_value;
120 double double_value;
121 if (!value.GetAsDouble(&double_value) || double_value < 0) {
122 NET_LOG_ERROR("Error parsing state value", path() + "." + key);
123 return false;
124 }
125 new_value = static_cast<uint32>(double_value);
126 if (*out_value == new_value)
127 return false;
128 *out_value = new_value;
129 return true;
130}
131
[email protected]3c8bd112012-11-07 10:14:59132} // namespace chromeos