managed_state.cc revision c2e0dbddbe15c98d52c4786dac06cb8952a8ae6d
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/network_state.h"
11#include "third_party/cros_system_api/dbus/service_constants.h"
12
13namespace chromeos {
14
15ManagedState::ManagedState(ManagedType type, const std::string& path)
16    : managed_type_(type),
17      path_(path),
18      is_observed_(false) {
19}
20
21ManagedState::~ManagedState() {
22}
23
24ManagedState* ManagedState::Create(ManagedType type, const std::string& path) {
25  switch(type) {
26    case MANAGED_TYPE_NETWORK:
27      return new NetworkState(path);
28    case MANAGED_TYPE_DEVICE:
29      return new DeviceState(path);
30  }
31  return NULL;
32}
33
34NetworkState* ManagedState::AsNetworkState() {
35  if (managed_type() == MANAGED_TYPE_NETWORK)
36    return static_cast<NetworkState*>(this);
37  return NULL;
38}
39
40DeviceState* ManagedState::AsDeviceState() {
41  if (managed_type() == MANAGED_TYPE_DEVICE)
42    return static_cast<DeviceState*>(this);
43  return NULL;
44}
45
46void ManagedState::InitialPropertiesReceived() {
47}
48
49bool ManagedState::ManagedStatePropertyChanged(const std::string& key,
50                                               const base::Value& value) {
51  if (key == flimflam::kNameProperty) {
52    return GetStringValue(key, value, &name_);
53  } else if (key == flimflam::kTypeProperty) {
54    return GetStringValue(key, value, &type_);
55  }
56  return false;
57}
58
59bool ManagedState::GetBooleanValue(const std::string& key,
60                                   const base::Value& value,
61                                   bool* out_value) {
62  bool res = value.GetAsBoolean(out_value);
63  if (!res)
64    LOG(WARNING) << "Failed to parse boolean value for:" << key;
65  return res;
66}
67
68bool ManagedState::GetIntegerValue(const std::string& key,
69                                   const base::Value& value,
70                                   int* out_value) {
71  bool res = value.GetAsInteger(out_value);
72  if (!res)
73    LOG(WARNING) << "Failed to parse integer value for:" << key;
74  return res;
75}
76
77bool ManagedState::GetStringValue(const std::string& key,
78                                  const base::Value& value,
79                                  std::string* out_value) {
80  bool res = value.GetAsString(out_value);
81  if (!res)
82    LOG(WARNING) << "Failed to parse string value for:" << key;
83  return res;
84}
85
86}  // namespace chromeos
87