[email protected] | 3c8bd11 | 2012-11-07 10:14:59 | [diff] [blame] | 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" |
[email protected] | ce21fc6 | 2013-07-03 10:45:58 | [diff] [blame] | 10 | #include "chromeos/network/favorite_state.h" |
[email protected] | 5092ea87 | 2013-05-16 21:56:08 | [diff] [blame] | 11 | #include "chromeos/network/network_event_log.h" |
[email protected] | 3c8bd11 | 2012-11-07 10:14:59 | [diff] [blame] | 12 | #include "chromeos/network/network_state.h" |
[email protected] | 184bd2d | 2013-09-11 20:38:04 | [diff] [blame] | 13 | #include "chromeos/network/shill_property_util.h" |
[email protected] | 3c8bd11 | 2012-11-07 10:14:59 | [diff] [blame] | 14 | #include "third_party/cros_system_api/dbus/service_constants.h" |
| 15 | |
| 16 | namespace chromeos { |
| 17 | |
[email protected] | 184bd2d | 2013-09-11 20:38:04 | [diff] [blame] | 18 | bool ManagedState::Matches(const NetworkTypePattern& pattern) const { |
| 19 | return pattern.MatchesType(type()); |
| 20 | } |
| 21 | |
[email protected] | 3c8bd11 | 2012-11-07 10:14:59 | [diff] [blame] | 22 | ManagedState::ManagedState(ManagedType type, const std::string& path) |
| 23 | : managed_type_(type), |
| 24 | path_(path), |
[email protected] | 7b6f21d | 2013-08-05 17:36:37 | [diff] [blame] | 25 | update_received_(false), |
[email protected] | 6f821df | 2013-05-22 21:22:02 | [diff] [blame] | 26 | update_requested_(false) { |
[email protected] | 3c8bd11 | 2012-11-07 10:14:59 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | ManagedState::~ManagedState() { |
| 30 | } |
| 31 | |
| 32 | ManagedState* ManagedState::Create(ManagedType type, const std::string& path) { |
[email protected] | 5092ea87 | 2013-05-16 21:56:08 | [diff] [blame] | 33 | switch (type) { |
[email protected] | 3c8bd11 | 2012-11-07 10:14:59 | [diff] [blame] | 34 | case MANAGED_TYPE_NETWORK: |
| 35 | return new NetworkState(path); |
[email protected] | ce21fc6 | 2013-07-03 10:45:58 | [diff] [blame] | 36 | case MANAGED_TYPE_FAVORITE: |
| 37 | return new FavoriteState(path); |
[email protected] | 3c8bd11 | 2012-11-07 10:14:59 | [diff] [blame] | 38 | case MANAGED_TYPE_DEVICE: |
| 39 | return new DeviceState(path); |
| 40 | } |
| 41 | return NULL; |
| 42 | } |
| 43 | |
| 44 | NetworkState* ManagedState::AsNetworkState() { |
| 45 | if (managed_type() == MANAGED_TYPE_NETWORK) |
| 46 | return static_cast<NetworkState*>(this); |
| 47 | return NULL; |
| 48 | } |
| 49 | |
| 50 | DeviceState* ManagedState::AsDeviceState() { |
| 51 | if (managed_type() == MANAGED_TYPE_DEVICE) |
| 52 | return static_cast<DeviceState*>(this); |
| 53 | return NULL; |
| 54 | } |
| 55 | |
[email protected] | ce21fc6 | 2013-07-03 10:45:58 | [diff] [blame] | 56 | FavoriteState* ManagedState::AsFavoriteState() { |
| 57 | if (managed_type() == MANAGED_TYPE_FAVORITE) |
| 58 | return static_cast<FavoriteState*>(this); |
| 59 | return NULL; |
| 60 | } |
| 61 | |
[email protected] | 577b22d0 | 2013-07-31 01:33:02 | [diff] [blame] | 62 | bool ManagedState::InitialPropertiesReceived( |
| 63 | const base::DictionaryValue& properties) { |
| 64 | return false; |
[email protected] | a7f1d1b | 2013-04-23 00:50:15 | [diff] [blame] | 65 | } |
| 66 | |
[email protected] | 3c8bd11 | 2012-11-07 10:14:59 | [diff] [blame] | 67 | bool ManagedState::ManagedStatePropertyChanged(const std::string& key, |
| 68 | const base::Value& value) { |
[email protected] | 547396c5 | 2013-09-24 07:24:56 | [diff] [blame] | 69 | if (key == shill::kNameProperty) { |
[email protected] | 3c8bd11 | 2012-11-07 10:14:59 | [diff] [blame] | 70 | return GetStringValue(key, value, &name_); |
[email protected] | 547396c5 | 2013-09-24 07:24:56 | [diff] [blame] | 71 | } else if (key == shill::kTypeProperty) { |
[email protected] | 3c8bd11 | 2012-11-07 10:14:59 | [diff] [blame] | 72 | return GetStringValue(key, value, &type_); |
| 73 | } |
| 74 | return false; |
| 75 | } |
| 76 | |
| 77 | bool ManagedState::GetBooleanValue(const std::string& key, |
| 78 | const base::Value& value, |
| 79 | bool* out_value) { |
[email protected] | 1c8edf1 | 2013-05-15 10:01:26 | [diff] [blame] | 80 | bool new_value; |
| 81 | if (!value.GetAsBoolean(&new_value)) { |
[email protected] | 5092ea87 | 2013-05-16 21:56:08 | [diff] [blame] | 82 | NET_LOG_ERROR("Error parsing state value", path() + "." + key); |
[email protected] | 1c8edf1 | 2013-05-15 10:01:26 | [diff] [blame] | 83 | return false; |
| 84 | } |
| 85 | if (*out_value == new_value) |
| 86 | return false; |
| 87 | *out_value = new_value; |
| 88 | return true; |
[email protected] | 3c8bd11 | 2012-11-07 10:14:59 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | bool ManagedState::GetIntegerValue(const std::string& key, |
| 92 | const base::Value& value, |
| 93 | int* out_value) { |
[email protected] | 1c8edf1 | 2013-05-15 10:01:26 | [diff] [blame] | 94 | int new_value; |
| 95 | if (!value.GetAsInteger(&new_value)) { |
[email protected] | 5092ea87 | 2013-05-16 21:56:08 | [diff] [blame] | 96 | NET_LOG_ERROR("Error parsing state value", path() + "." + key); |
[email protected] | 1c8edf1 | 2013-05-15 10:01:26 | [diff] [blame] | 97 | return false; |
| 98 | } |
| 99 | if (*out_value == new_value) |
| 100 | return false; |
| 101 | *out_value = new_value; |
| 102 | return true; |
[email protected] | 3c8bd11 | 2012-11-07 10:14:59 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | bool ManagedState::GetStringValue(const std::string& key, |
| 106 | const base::Value& value, |
| 107 | std::string* out_value) { |
[email protected] | 1c8edf1 | 2013-05-15 10:01:26 | [diff] [blame] | 108 | std::string new_value; |
| 109 | if (!value.GetAsString(&new_value)) { |
[email protected] | 5092ea87 | 2013-05-16 21:56:08 | [diff] [blame] | 110 | NET_LOG_ERROR("Error parsing state value", path() + "." + key); |
[email protected] | 1c8edf1 | 2013-05-15 10:01:26 | [diff] [blame] | 111 | return false; |
| 112 | } |
| 113 | if (*out_value == new_value) |
| 114 | return false; |
| 115 | *out_value = new_value; |
| 116 | return true; |
[email protected] | 3c8bd11 | 2012-11-07 10:14:59 | [diff] [blame] | 117 | } |
| 118 | |
[email protected] | 63014ca4 | 2013-07-10 04:55:26 | [diff] [blame] | 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 | |
[email protected] | 3c8bd11 | 2012-11-07 10:14:59 | [diff] [blame] | 138 | } // namespace chromeos |