managed_state.cc revision 58537e28ecd584eab876aee8be7156509866d23a
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 "chromeos/network/shill_property_util.h"
14#include "third_party/cros_system_api/dbus/service_constants.h"
15
16namespace chromeos {
17
18bool ManagedState::Matches(const NetworkTypePattern& pattern) const {
19  return pattern.MatchesType(type());
20}
21
22ManagedState::ManagedState(ManagedType type, const std::string& path)
23    : managed_type_(type),
24      path_(path),
25      update_received_(false),
26      update_requested_(false) {
27}
28
29ManagedState::~ManagedState() {
30}
31
32ManagedState* ManagedState::Create(ManagedType type, const std::string& path) {
33  switch (type) {
34    case MANAGED_TYPE_NETWORK:
35      return new NetworkState(path);
36    case MANAGED_TYPE_FAVORITE:
37      return new FavoriteState(path);
38    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
56FavoriteState* ManagedState::AsFavoriteState() {
57  if (managed_type() == MANAGED_TYPE_FAVORITE)
58    return static_cast<FavoriteState*>(this);
59  return NULL;
60}
61
62bool ManagedState::InitialPropertiesReceived(
63    const base::DictionaryValue& properties) {
64  return false;
65}
66
67bool 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) {
80  bool new_value;
81  if (!value.GetAsBoolean(&new_value)) {
82    NET_LOG_ERROR("Error parsing state value", path() + "." + key);
83    return false;
84  }
85  if (*out_value == new_value)
86    return false;
87  *out_value = new_value;
88  return true;
89}
90
91bool ManagedState::GetIntegerValue(const std::string& key,
92                                   const base::Value& value,
93                                   int* out_value) {
94  int new_value;
95  if (!value.GetAsInteger(&new_value)) {
96    NET_LOG_ERROR("Error parsing state value", path() + "." + key);
97    return false;
98  }
99  if (*out_value == new_value)
100    return false;
101  *out_value = new_value;
102  return true;
103}
104
105bool ManagedState::GetStringValue(const std::string& key,
106                                  const base::Value& value,
107                                  std::string* out_value) {
108  std::string new_value;
109  if (!value.GetAsString(&new_value)) {
110    NET_LOG_ERROR("Error parsing state value", path() + "." + key);
111    return false;
112  }
113  if (*out_value == new_value)
114    return false;
115  *out_value = new_value;
116  return true;
117}
118
119bool 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
138}  // namespace chromeos
139