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