managed_state.cc revision 2385ea399aae016c0806a4f9ef3c9cfe3d2a39df
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.
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"
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"
14
15namespace chromeos {
16
17ManagedState::ManagedState(ManagedType type, const std::string& path)
18    : managed_type_(type),
19      path_(path),
20      update_received_(false),
21      update_requested_(false) {
22}
23
24ManagedState::~ManagedState() {
25}
26
27ManagedState* ManagedState::Create(ManagedType type, const std::string& path) {
28  switch (type) {
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);
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
51FavoriteState* ManagedState::AsFavoriteState() {
52  if (managed_type() == MANAGED_TYPE_FAVORITE)
53    return static_cast<FavoriteState*>(this);
54  return NULL;
55}
56
57bool ManagedState::InitialPropertiesReceived(
58    const base::DictionaryValue& properties) {
59  return false;
60}
61
62bool 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) {
75  bool new_value;
76  if (!value.GetAsBoolean(&new_value)) {
77    NET_LOG_ERROR("Error parsing state value", path() + "." + key);
78    return false;
79  }
80  if (*out_value == new_value)
81    return false;
82  *out_value = new_value;
83  return true;
84}
85
86bool ManagedState::GetIntegerValue(const std::string& key,
87                                   const base::Value& value,
88                                   int* out_value) {
89  int new_value;
90  if (!value.GetAsInteger(&new_value)) {
91    NET_LOG_ERROR("Error parsing state value", path() + "." + key);
92    return false;
93  }
94  if (*out_value == new_value)
95    return false;
96  *out_value = new_value;
97  return true;
98}
99
100bool 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);
106    return false;
107  }
108  if (*out_value == new_value)
109    return false;
110  *out_value = new_value;
111  return true;
112}
113
114bool 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
133}  // namespace chromeos
134