managed_state.cc revision eb525c5499e34cc9c4b825d6d9e75bb07cc06ace
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_requested_(false) {
21}
22
23ManagedState::~ManagedState() {
24}
25
26ManagedState* ManagedState::Create(ManagedType type, const std::string& path) {
27  switch (type) {
28    case MANAGED_TYPE_NETWORK:
29      return new NetworkState(path);
30    case MANAGED_TYPE_FAVORITE:
31      return new FavoriteState(path);
32    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
50FavoriteState* ManagedState::AsFavoriteState() {
51  if (managed_type() == MANAGED_TYPE_FAVORITE)
52    return static_cast<FavoriteState*>(this);
53  return NULL;
54}
55
56void ManagedState::InitialPropertiesReceived() {
57}
58
59bool ManagedState::ManagedStatePropertyChanged(const std::string& key,
60                                               const base::Value& value) {
61  if (key == flimflam::kNameProperty) {
62    return GetStringValue(key, value, &name_);
63  } else if (key == flimflam::kTypeProperty) {
64    return GetStringValue(key, value, &type_);
65  }
66  return false;
67}
68
69bool ManagedState::GetBooleanValue(const std::string& key,
70                                   const base::Value& value,
71                                   bool* out_value) {
72  bool new_value;
73  if (!value.GetAsBoolean(&new_value)) {
74    NET_LOG_ERROR("Error parsing state value", path() + "." + key);
75    return false;
76  }
77  if (*out_value == new_value)
78    return false;
79  *out_value = new_value;
80  return true;
81}
82
83bool ManagedState::GetIntegerValue(const std::string& key,
84                                   const base::Value& value,
85                                   int* out_value) {
86  int new_value;
87  if (!value.GetAsInteger(&new_value)) {
88    NET_LOG_ERROR("Error parsing state value", path() + "." + key);
89    return false;
90  }
91  if (*out_value == new_value)
92    return false;
93  *out_value = new_value;
94  return true;
95}
96
97bool ManagedState::GetStringValue(const std::string& key,
98                                  const base::Value& value,
99                                  std::string* out_value) {
100  std::string new_value;
101  if (!value.GetAsString(&new_value)) {
102    NET_LOG_ERROR("Error parsing state value", path() + "." + key);
103    return false;
104  }
105  if (*out_value == new_value)
106    return false;
107  *out_value = new_value;
108  return true;
109}
110
111}  // namespace chromeos
112