blob: 655e5769a4bc26ec54262c88e6160b006e1c12a5 [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]6f821df2013-05-22 21:22:0220 update_requested_(false) {
[email protected]3c8bd112012-11-07 10:14:5921}
22
23ManagedState::~ManagedState() {
24}
25
26ManagedState* ManagedState::Create(ManagedType type, const std::string& path) {
[email protected]5092ea872013-05-16 21:56:0827 switch (type) {
[email protected]3c8bd112012-11-07 10:14:5928 case MANAGED_TYPE_NETWORK:
29 return new NetworkState(path);
[email protected]ce21fc62013-07-03 10:45:5830 case MANAGED_TYPE_FAVORITE:
31 return new FavoriteState(path);
[email protected]3c8bd112012-11-07 10:14:5932 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
[email protected]ce21fc62013-07-03 10:45:5850FavoriteState* ManagedState::AsFavoriteState() {
51 if (managed_type() == MANAGED_TYPE_FAVORITE)
52 return static_cast<FavoriteState*>(this);
53 return NULL;
54}
55
[email protected]a7f1d1b2013-04-23 00:50:1556void ManagedState::InitialPropertiesReceived() {
57}
58
[email protected]3c8bd112012-11-07 10:14:5959bool 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) {
[email protected]1c8edf12013-05-15 10:01:2672 bool new_value;
73 if (!value.GetAsBoolean(&new_value)) {
[email protected]5092ea872013-05-16 21:56:0874 NET_LOG_ERROR("Error parsing state value", path() + "." + key);
[email protected]1c8edf12013-05-15 10:01:2675 return false;
76 }
77 if (*out_value == new_value)
78 return false;
79 *out_value = new_value;
80 return true;
[email protected]3c8bd112012-11-07 10:14:5981}
82
83bool ManagedState::GetIntegerValue(const std::string& key,
84 const base::Value& value,
85 int* out_value) {
[email protected]1c8edf12013-05-15 10:01:2686 int new_value;
87 if (!value.GetAsInteger(&new_value)) {
[email protected]5092ea872013-05-16 21:56:0888 NET_LOG_ERROR("Error parsing state value", path() + "." + key);
[email protected]1c8edf12013-05-15 10:01:2689 return false;
90 }
91 if (*out_value == new_value)
92 return false;
93 *out_value = new_value;
94 return true;
[email protected]3c8bd112012-11-07 10:14:5995}
96
97bool ManagedState::GetStringValue(const std::string& key,
98 const base::Value& value,
99 std::string* out_value) {
[email protected]1c8edf12013-05-15 10:01:26100 std::string new_value;
101 if (!value.GetAsString(&new_value)) {
[email protected]5092ea872013-05-16 21:56:08102 NET_LOG_ERROR("Error parsing state value", path() + "." + key);
[email protected]1c8edf12013-05-15 10:01:26103 return false;
104 }
105 if (*out_value == new_value)
106 return false;
107 *out_value = new_value;
108 return true;
[email protected]3c8bd112012-11-07 10:14:59109}
110
111} // namespace chromeos