blob: 466dcaf21ece1ba429578b883a7f22d0ede397ce [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]7b6f21d2013-08-05 17:36:3720 update_received_(false),
[email protected]6f821df2013-05-22 21:22:0221 update_requested_(false) {
[email protected]3c8bd112012-11-07 10:14:5922}
23
24ManagedState::~ManagedState() {
25}
26
27ManagedState* ManagedState::Create(ManagedType type, const std::string& path) {
[email protected]5092ea872013-05-16 21:56:0828 switch (type) {
[email protected]3c8bd112012-11-07 10:14:5929 case MANAGED_TYPE_NETWORK:
30 return new NetworkState(path);
[email protected]ce21fc62013-07-03 10:45:5831 case MANAGED_TYPE_FAVORITE:
32 return new FavoriteState(path);
[email protected]3c8bd112012-11-07 10:14:5933 case MANAGED_TYPE_DEVICE:
34 return new DeviceState(path);
35 }
36 return NULL;
37}
38
39NetworkState* ManagedState::AsNetworkState() {
40 if (managed_type() == MANAGED_TYPE_NETWORK)
41 return static_cast<NetworkState*>(this);
42 return NULL;
43}
44
45DeviceState* ManagedState::AsDeviceState() {
46 if (managed_type() == MANAGED_TYPE_DEVICE)
47 return static_cast<DeviceState*>(this);
48 return NULL;
49}
50
[email protected]ce21fc62013-07-03 10:45:5851FavoriteState* ManagedState::AsFavoriteState() {
52 if (managed_type() == MANAGED_TYPE_FAVORITE)
53 return static_cast<FavoriteState*>(this);
54 return NULL;
55}
56
[email protected]577b22d02013-07-31 01:33:0257bool ManagedState::InitialPropertiesReceived(
58 const base::DictionaryValue& properties) {
59 return false;
[email protected]a7f1d1b2013-04-23 00:50:1560}
61
[email protected]3c8bd112012-11-07 10:14:5962bool 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_);
68 }
69 return false;
70}
71
72bool ManagedState::GetBooleanValue(const std::string& key,
73 const base::Value& value,
74 bool* out_value) {
[email protected]1c8edf12013-05-15 10:01:2675 bool new_value;
76 if (!value.GetAsBoolean(&new_value)) {
[email protected]5092ea872013-05-16 21:56:0877 NET_LOG_ERROR("Error parsing state value", path() + "." + key);
[email protected]1c8edf12013-05-15 10:01:2678 return false;
79 }
80 if (*out_value == new_value)
81 return false;
82 *out_value = new_value;
83 return true;
[email protected]3c8bd112012-11-07 10:14:5984}
85
86bool ManagedState::GetIntegerValue(const std::string& key,
87 const base::Value& value,
88 int* out_value) {
[email protected]1c8edf12013-05-15 10:01:2689 int new_value;
90 if (!value.GetAsInteger(&new_value)) {
[email protected]5092ea872013-05-16 21:56:0891 NET_LOG_ERROR("Error parsing state value", path() + "." + key);
[email protected]1c8edf12013-05-15 10:01:2692 return false;
93 }
94 if (*out_value == new_value)
95 return false;
96 *out_value = new_value;
97 return true;
[email protected]3c8bd112012-11-07 10:14:5998}
99
100bool ManagedState::GetStringValue(const std::string& key,
101 const base::Value& value,
102 std::string* out_value) {
[email protected]1c8edf12013-05-15 10:01:26103 std::string new_value;
104 if (!value.GetAsString(&new_value)) {
[email protected]5092ea872013-05-16 21:56:08105 NET_LOG_ERROR("Error parsing state value", path() + "." + key);
[email protected]1c8edf12013-05-15 10:01:26106 return false;
107 }
108 if (*out_value == new_value)
109 return false;
110 *out_value = new_value;
111 return true;
[email protected]3c8bd112012-11-07 10:14:59112}
113
[email protected]63014ca42013-07-10 04:55:26114bool ManagedState::GetUInt32Value(const std::string& key,
115 const base::Value& value,
116 uint32* out_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).
120 uint32 new_value;
121 double double_value;
122 if (!value.GetAsDouble(&double_value) || double_value < 0) {
123 NET_LOG_ERROR("Error parsing state value", path() + "." + key);
124 return false;
125 }
126 new_value = static_cast<uint32>(double_value);
127 if (*out_value == new_value)
128 return false;
129 *out_value = new_value;
130 return true;
131}
132
[email protected]3c8bd112012-11-07 10:14:59133} // namespace chromeos