• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_event_log.h"
11 #include "chromeos/network/network_state.h"
12 #include "chromeos/network/network_type_pattern.h"
13 #include "third_party/cros_system_api/dbus/service_constants.h"
14 
15 namespace chromeos {
16 
Matches(const NetworkTypePattern & pattern) const17 bool ManagedState::Matches(const NetworkTypePattern& pattern) const {
18   return pattern.MatchesType(type());
19 }
20 
21 // static
TypeToString(ManagedType type)22 std::string ManagedState::TypeToString(ManagedType type) {
23   switch (type) {
24     case MANAGED_TYPE_NETWORK:
25       return "Network";
26     case MANAGED_TYPE_DEVICE:
27       return "Device";
28   }
29   return "Unknown";
30 }
31 
ManagedState(ManagedType type,const std::string & path)32 ManagedState::ManagedState(ManagedType type, const std::string& path)
33     : managed_type_(type),
34       path_(path),
35       update_received_(false),
36       update_requested_(false) {
37 }
38 
~ManagedState()39 ManagedState::~ManagedState() {
40 }
41 
Create(ManagedType type,const std::string & path)42 ManagedState* ManagedState::Create(ManagedType type, const std::string& path) {
43   switch (type) {
44     case MANAGED_TYPE_NETWORK:
45       return new NetworkState(path);
46     case MANAGED_TYPE_DEVICE:
47       return new DeviceState(path);
48   }
49   return NULL;
50 }
51 
AsNetworkState()52 NetworkState* ManagedState::AsNetworkState() {
53   if (managed_type() == MANAGED_TYPE_NETWORK)
54     return static_cast<NetworkState*>(this);
55   return NULL;
56 }
57 
AsDeviceState()58 DeviceState* ManagedState::AsDeviceState() {
59   if (managed_type() == MANAGED_TYPE_DEVICE)
60     return static_cast<DeviceState*>(this);
61   return NULL;
62 }
63 
InitialPropertiesReceived(const base::DictionaryValue & properties)64 bool ManagedState::InitialPropertiesReceived(
65     const base::DictionaryValue& properties) {
66   return false;
67 }
68 
GetStateProperties(base::DictionaryValue * dictionary) const69 void ManagedState::GetStateProperties(base::DictionaryValue* dictionary) const {
70   dictionary->SetStringWithoutPathExpansion(shill::kNameProperty, name());
71   dictionary->SetStringWithoutPathExpansion(shill::kTypeProperty, type());
72 }
73 
ManagedStatePropertyChanged(const std::string & key,const base::Value & value)74 bool ManagedState::ManagedStatePropertyChanged(const std::string& key,
75                                                const base::Value& value) {
76   if (key == shill::kNameProperty) {
77     return GetStringValue(key, value, &name_);
78   } else if (key == shill::kTypeProperty) {
79     return GetStringValue(key, value, &type_);
80   }
81   return false;
82 }
83 
GetBooleanValue(const std::string & key,const base::Value & value,bool * out_value)84 bool ManagedState::GetBooleanValue(const std::string& key,
85                                    const base::Value& value,
86                                    bool* out_value) {
87   bool new_value;
88   if (!value.GetAsBoolean(&new_value)) {
89     NET_LOG_ERROR("Error parsing state value", path() + "." + key);
90     return false;
91   }
92   if (*out_value == new_value)
93     return false;
94   *out_value = new_value;
95   return true;
96 }
97 
GetIntegerValue(const std::string & key,const base::Value & value,int * out_value)98 bool ManagedState::GetIntegerValue(const std::string& key,
99                                    const base::Value& value,
100                                    int* out_value) {
101   int new_value;
102   if (!value.GetAsInteger(&new_value)) {
103     NET_LOG_ERROR("Error parsing state value", path() + "." + key);
104     return false;
105   }
106   if (*out_value == new_value)
107     return false;
108   *out_value = new_value;
109   return true;
110 }
111 
GetStringValue(const std::string & key,const base::Value & value,std::string * out_value)112 bool ManagedState::GetStringValue(const std::string& key,
113                                   const base::Value& value,
114                                   std::string* out_value) {
115   std::string new_value;
116   if (!value.GetAsString(&new_value)) {
117     NET_LOG_ERROR("Error parsing state: " + key, path());
118     return false;
119   }
120   if (*out_value == new_value)
121     return false;
122   *out_value = new_value;
123   return true;
124 }
125 
GetUInt32Value(const std::string & key,const base::Value & value,uint32 * out_value)126 bool ManagedState::GetUInt32Value(const std::string& key,
127                                   const base::Value& value,
128                                   uint32* out_value) {
129   // base::Value restricts the number types to BOOL, INTEGER, and DOUBLE only.
130   // uint32 will automatically get converted to a double, which is why we try
131   // to obtain the value as a double (see dbus/values_util.h).
132   uint32 new_value;
133   double double_value;
134   if (!value.GetAsDouble(&double_value) || double_value < 0) {
135     NET_LOG_ERROR("Error parsing state value", path() + "." + key);
136     return false;
137   }
138   new_value = static_cast<uint32>(double_value);
139   if (*out_value == new_value)
140     return false;
141   *out_value = new_value;
142   return true;
143 }
144 
145 }  // namespace chromeos
146