Add default implementations for AppWindowRegistry::Observer notifications.
[chromium-blink-merge.git] / chromeos / network / managed_state.cc
blob4bbece8e75d93552a01525fa490b787eeb3be4be
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.
5 #include "chromeos/network/managed_state.h"
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"
16 namespace chromeos {
18 bool ManagedState::Matches(const NetworkTypePattern& pattern) const {
19 return pattern.MatchesType(type());
22 // static
23 std::string ManagedState::TypeToString(ManagedType type) {
24 switch (type) {
25 case MANAGED_TYPE_NETWORK:
26 return "Network";
27 case MANAGED_TYPE_FAVORITE:
28 return "Favorite";
29 case MANAGED_TYPE_DEVICE:
30 return "Device";
32 return "Unknown";
35 ManagedState::ManagedState(ManagedType type, const std::string& path)
36 : managed_type_(type),
37 path_(path),
38 update_received_(false),
39 update_requested_(false) {
42 ManagedState::~ManagedState() {
45 ManagedState* ManagedState::Create(ManagedType type, const std::string& path) {
46 switch (type) {
47 case MANAGED_TYPE_NETWORK:
48 return new NetworkState(path);
49 case MANAGED_TYPE_FAVORITE:
50 return new FavoriteState(path);
51 case MANAGED_TYPE_DEVICE:
52 return new DeviceState(path);
54 return NULL;
57 NetworkState* ManagedState::AsNetworkState() {
58 if (managed_type() == MANAGED_TYPE_NETWORK)
59 return static_cast<NetworkState*>(this);
60 return NULL;
63 DeviceState* ManagedState::AsDeviceState() {
64 if (managed_type() == MANAGED_TYPE_DEVICE)
65 return static_cast<DeviceState*>(this);
66 return NULL;
69 FavoriteState* ManagedState::AsFavoriteState() {
70 if (managed_type() == MANAGED_TYPE_FAVORITE)
71 return static_cast<FavoriteState*>(this);
72 return NULL;
75 bool ManagedState::InitialPropertiesReceived(
76 const base::DictionaryValue& properties) {
77 return false;
80 void ManagedState::GetStateProperties(base::DictionaryValue* dictionary) const {
81 dictionary->SetStringWithoutPathExpansion(shill::kNameProperty, name());
82 dictionary->SetStringWithoutPathExpansion(shill::kTypeProperty, type());
85 bool ManagedState::ManagedStatePropertyChanged(const std::string& key,
86 const base::Value& value) {
87 if (key == shill::kNameProperty) {
88 return GetStringValue(key, value, &name_);
89 } else if (key == shill::kTypeProperty) {
90 return GetStringValue(key, value, &type_);
92 return false;
95 bool ManagedState::GetBooleanValue(const std::string& key,
96 const base::Value& value,
97 bool* out_value) {
98 bool new_value;
99 if (!value.GetAsBoolean(&new_value)) {
100 NET_LOG_ERROR("Error parsing state value", path() + "." + key);
101 return false;
103 if (*out_value == new_value)
104 return false;
105 *out_value = new_value;
106 return true;
109 bool ManagedState::GetIntegerValue(const std::string& key,
110 const base::Value& value,
111 int* out_value) {
112 int new_value;
113 if (!value.GetAsInteger(&new_value)) {
114 NET_LOG_ERROR("Error parsing state value", path() + "." + key);
115 return false;
117 if (*out_value == new_value)
118 return false;
119 *out_value = new_value;
120 return true;
123 bool ManagedState::GetStringValue(const std::string& key,
124 const base::Value& value,
125 std::string* out_value) {
126 std::string new_value;
127 if (!value.GetAsString(&new_value)) {
128 NET_LOG_ERROR("Error parsing state: " + key, path());
129 return false;
131 if (*out_value == new_value)
132 return false;
133 *out_value = new_value;
134 return true;
137 bool ManagedState::GetUInt32Value(const std::string& key,
138 const base::Value& value,
139 uint32* out_value) {
140 // base::Value restricts the number types to BOOL, INTEGER, and DOUBLE only.
141 // uint32 will automatically get converted to a double, which is why we try
142 // to obtain the value as a double (see dbus/values_util.h).
143 uint32 new_value;
144 double double_value;
145 if (!value.GetAsDouble(&double_value) || double_value < 0) {
146 NET_LOG_ERROR("Error parsing state value", path() + "." + key);
147 return false;
149 new_value = static_cast<uint32>(double_value);
150 if (*out_value == new_value)
151 return false;
152 *out_value = new_value;
153 return true;
156 } // namespace chromeos