[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" |
| 13 | #include "third_party/cros_system_api/dbus/service_constants.h" |
| 14 | |
| 15 | namespace chromeos { |
| 16 | |
| 17 | ManagedState::ManagedState(ManagedType type, const std::string& path) |
| 18 | : managed_type_(type), |
| 19 | path_(path), |
[email protected] | 6f821df | 2013-05-22 21:22:02 | [diff] [blame] | 20 | update_requested_(false) { |
[email protected] | 3c8bd11 | 2012-11-07 10:14:59 | [diff] [blame] | 21 | } |
| 22 | |
| 23 | ManagedState::~ManagedState() { |
| 24 | } |
| 25 | |
| 26 | ManagedState* ManagedState::Create(ManagedType type, const std::string& path) { |
[email protected] | 5092ea87 | 2013-05-16 21:56:08 | [diff] [blame] | 27 | switch (type) { |
[email protected] | 3c8bd11 | 2012-11-07 10:14:59 | [diff] [blame] | 28 | case MANAGED_TYPE_NETWORK: |
| 29 | return new NetworkState(path); |
[email protected] | ce21fc6 | 2013-07-03 10:45:58 | [diff] [blame] | 30 | case MANAGED_TYPE_FAVORITE: |
| 31 | return new FavoriteState(path); |
[email protected] | 3c8bd11 | 2012-11-07 10:14:59 | [diff] [blame] | 32 | case MANAGED_TYPE_DEVICE: |
| 33 | return new DeviceState(path); |
| 34 | } |
| 35 | return NULL; |
| 36 | } |
| 37 | |
| 38 | NetworkState* ManagedState::AsNetworkState() { |
| 39 | if (managed_type() == MANAGED_TYPE_NETWORK) |
| 40 | return static_cast<NetworkState*>(this); |
| 41 | return NULL; |
| 42 | } |
| 43 | |
| 44 | DeviceState* ManagedState::AsDeviceState() { |
| 45 | if (managed_type() == MANAGED_TYPE_DEVICE) |
| 46 | return static_cast<DeviceState*>(this); |
| 47 | return NULL; |
| 48 | } |
| 49 | |
[email protected] | ce21fc6 | 2013-07-03 10:45:58 | [diff] [blame] | 50 | FavoriteState* ManagedState::AsFavoriteState() { |
| 51 | if (managed_type() == MANAGED_TYPE_FAVORITE) |
| 52 | return static_cast<FavoriteState*>(this); |
| 53 | return NULL; |
| 54 | } |
| 55 | |
[email protected] | 577b22d0 | 2013-07-31 01:33:02 | [diff] [blame^] | 56 | bool ManagedState::InitialPropertiesReceived( |
| 57 | const base::DictionaryValue& properties) { |
| 58 | return false; |
[email protected] | a7f1d1b | 2013-04-23 00:50:15 | [diff] [blame] | 59 | } |
| 60 | |
[email protected] | 3c8bd11 | 2012-11-07 10:14:59 | [diff] [blame] | 61 | bool ManagedState::ManagedStatePropertyChanged(const std::string& key, |
| 62 | const base::Value& value) { |
| 63 | if (key == flimflam::kNameProperty) { |
| 64 | return GetStringValue(key, value, &name_); |
| 65 | } else if (key == flimflam::kTypeProperty) { |
| 66 | return GetStringValue(key, value, &type_); |
| 67 | } |
| 68 | return false; |
| 69 | } |
| 70 | |
| 71 | bool ManagedState::GetBooleanValue(const std::string& key, |
| 72 | const base::Value& value, |
| 73 | bool* out_value) { |
[email protected] | 1c8edf1 | 2013-05-15 10:01:26 | [diff] [blame] | 74 | bool new_value; |
| 75 | if (!value.GetAsBoolean(&new_value)) { |
[email protected] | 5092ea87 | 2013-05-16 21:56:08 | [diff] [blame] | 76 | NET_LOG_ERROR("Error parsing state value", path() + "." + key); |
[email protected] | 1c8edf1 | 2013-05-15 10:01:26 | [diff] [blame] | 77 | return false; |
| 78 | } |
| 79 | if (*out_value == new_value) |
| 80 | return false; |
| 81 | *out_value = new_value; |
| 82 | return true; |
[email protected] | 3c8bd11 | 2012-11-07 10:14:59 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | bool ManagedState::GetIntegerValue(const std::string& key, |
| 86 | const base::Value& value, |
| 87 | int* out_value) { |
[email protected] | 1c8edf1 | 2013-05-15 10:01:26 | [diff] [blame] | 88 | int new_value; |
| 89 | if (!value.GetAsInteger(&new_value)) { |
[email protected] | 5092ea87 | 2013-05-16 21:56:08 | [diff] [blame] | 90 | NET_LOG_ERROR("Error parsing state value", path() + "." + key); |
[email protected] | 1c8edf1 | 2013-05-15 10:01:26 | [diff] [blame] | 91 | return false; |
| 92 | } |
| 93 | if (*out_value == new_value) |
| 94 | return false; |
| 95 | *out_value = new_value; |
| 96 | return true; |
[email protected] | 3c8bd11 | 2012-11-07 10:14:59 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | bool ManagedState::GetStringValue(const std::string& key, |
| 100 | const base::Value& value, |
| 101 | std::string* out_value) { |
[email protected] | 1c8edf1 | 2013-05-15 10:01:26 | [diff] [blame] | 102 | std::string new_value; |
| 103 | if (!value.GetAsString(&new_value)) { |
[email protected] | 5092ea87 | 2013-05-16 21:56:08 | [diff] [blame] | 104 | NET_LOG_ERROR("Error parsing state value", path() + "." + key); |
[email protected] | 1c8edf1 | 2013-05-15 10:01:26 | [diff] [blame] | 105 | return false; |
| 106 | } |
| 107 | if (*out_value == new_value) |
| 108 | return false; |
| 109 | *out_value = new_value; |
| 110 | return true; |
[email protected] | 3c8bd11 | 2012-11-07 10:14:59 | [diff] [blame] | 111 | } |
| 112 | |
[email protected] | 63014ca4 | 2013-07-10 04:55:26 | [diff] [blame] | 113 | bool ManagedState::GetUInt32Value(const std::string& key, |
| 114 | const base::Value& value, |
| 115 | uint32* out_value) { |
| 116 | // base::Value restricts the number types to BOOL, INTEGER, and DOUBLE only. |
| 117 | // uint32 will automatically get converted to a double, which is why we try |
| 118 | // to obtain the value as a double (see dbus/values_util.h). |
| 119 | uint32 new_value; |
| 120 | double double_value; |
| 121 | if (!value.GetAsDouble(&double_value) || double_value < 0) { |
| 122 | NET_LOG_ERROR("Error parsing state value", path() + "." + key); |
| 123 | return false; |
| 124 | } |
| 125 | new_value = static_cast<uint32>(double_value); |
| 126 | if (*out_value == new_value) |
| 127 | return false; |
| 128 | *out_value = new_value; |
| 129 | return true; |
| 130 | } |
| 131 | |
[email protected] | 3c8bd11 | 2012-11-07 10:14:59 | [diff] [blame] | 132 | } // namespace chromeos |