62e9db41bdafcc42577bc0d1f3f8afdff999cb0d
[platform/framework/web/crosswalk.git] / src / chromeos / network / managed_state.cc
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
16 namespace chromeos {
17
18 bool ManagedState::Matches(const NetworkTypePattern& pattern) const {
19   return pattern.MatchesType(type());
20 }
21
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";
31   }
32   return "Unknown";
33 }
34
35 ManagedState::ManagedState(ManagedType type, const std::string& path)
36     : managed_type_(type),
37       path_(path),
38       update_received_(false),
39       update_requested_(false) {
40 }
41
42 ManagedState::~ManagedState() {
43 }
44
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);
53   }
54   return NULL;
55 }
56
57 NetworkState* ManagedState::AsNetworkState() {
58   if (managed_type() == MANAGED_TYPE_NETWORK)
59     return static_cast<NetworkState*>(this);
60   return NULL;
61 }
62
63 DeviceState* ManagedState::AsDeviceState() {
64   if (managed_type() == MANAGED_TYPE_DEVICE)
65     return static_cast<DeviceState*>(this);
66   return NULL;
67 }
68
69 FavoriteState* ManagedState::AsFavoriteState() {
70   if (managed_type() == MANAGED_TYPE_FAVORITE)
71     return static_cast<FavoriteState*>(this);
72   return NULL;
73 }
74
75 bool ManagedState::InitialPropertiesReceived(
76     const base::DictionaryValue& properties) {
77   return false;
78 }
79
80 bool ManagedState::ManagedStatePropertyChanged(const std::string& key,
81                                                const base::Value& value) {
82   if (key == shill::kNameProperty) {
83     return GetStringValue(key, value, &name_);
84   } else if (key == shill::kTypeProperty) {
85     return GetStringValue(key, value, &type_);
86   }
87   return false;
88 }
89
90 bool ManagedState::GetBooleanValue(const std::string& key,
91                                    const base::Value& value,
92                                    bool* out_value) {
93   bool new_value;
94   if (!value.GetAsBoolean(&new_value)) {
95     NET_LOG_ERROR("Error parsing state value", path() + "." + key);
96     return false;
97   }
98   if (*out_value == new_value)
99     return false;
100   *out_value = new_value;
101   return true;
102 }
103
104 bool ManagedState::GetIntegerValue(const std::string& key,
105                                    const base::Value& value,
106                                    int* out_value) {
107   int new_value;
108   if (!value.GetAsInteger(&new_value)) {
109     NET_LOG_ERROR("Error parsing state value", path() + "." + key);
110     return false;
111   }
112   if (*out_value == new_value)
113     return false;
114   *out_value = new_value;
115   return true;
116 }
117
118 bool ManagedState::GetStringValue(const std::string& key,
119                                   const base::Value& value,
120                                   std::string* out_value) {
121   std::string new_value;
122   if (!value.GetAsString(&new_value)) {
123     NET_LOG_ERROR("Error parsing state: " + key, path());
124     return false;
125   }
126   if (*out_value == new_value)
127     return false;
128   *out_value = new_value;
129   return true;
130 }
131
132 bool ManagedState::GetUInt32Value(const std::string& key,
133                                   const base::Value& value,
134                                   uint32* out_value) {
135   // base::Value restricts the number types to BOOL, INTEGER, and DOUBLE only.
136   // uint32 will automatically get converted to a double, which is why we try
137   // to obtain the value as a double (see dbus/values_util.h).
138   uint32 new_value;
139   double double_value;
140   if (!value.GetAsDouble(&double_value) || double_value < 0) {
141     NET_LOG_ERROR("Error parsing state value", path() + "." + key);
142     return false;
143   }
144   new_value = static_cast<uint32>(double_value);
145   if (*out_value == new_value)
146     return false;
147   *out_value = new_value;
148   return true;
149 }
150
151 }  // namespace chromeos