[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] | a7f1d1b | 2013-04-23 00:50:15 | [diff] [blame] | 56 | void ManagedState::InitialPropertiesReceived() { |
| 57 | } |
| 58 | |
[email protected] | 3c8bd11 | 2012-11-07 10:14:59 | [diff] [blame] | 59 | bool ManagedState::ManagedStatePropertyChanged(const std::string& key, |
| 60 | const base::Value& value) { |
| 61 | if (key == flimflam::kNameProperty) { |
| 62 | return GetStringValue(key, value, &name_); |
| 63 | } else if (key == flimflam::kTypeProperty) { |
| 64 | return GetStringValue(key, value, &type_); |
| 65 | } |
| 66 | return false; |
| 67 | } |
| 68 | |
| 69 | bool ManagedState::GetBooleanValue(const std::string& key, |
| 70 | const base::Value& value, |
| 71 | bool* out_value) { |
[email protected] | 1c8edf1 | 2013-05-15 10:01:26 | [diff] [blame] | 72 | bool new_value; |
| 73 | if (!value.GetAsBoolean(&new_value)) { |
[email protected] | 5092ea87 | 2013-05-16 21:56:08 | [diff] [blame] | 74 | NET_LOG_ERROR("Error parsing state value", path() + "." + key); |
[email protected] | 1c8edf1 | 2013-05-15 10:01:26 | [diff] [blame] | 75 | return false; |
| 76 | } |
| 77 | if (*out_value == new_value) |
| 78 | return false; |
| 79 | *out_value = new_value; |
| 80 | return true; |
[email protected] | 3c8bd11 | 2012-11-07 10:14:59 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | bool ManagedState::GetIntegerValue(const std::string& key, |
| 84 | const base::Value& value, |
| 85 | int* out_value) { |
[email protected] | 1c8edf1 | 2013-05-15 10:01:26 | [diff] [blame] | 86 | int new_value; |
| 87 | if (!value.GetAsInteger(&new_value)) { |
[email protected] | 5092ea87 | 2013-05-16 21:56:08 | [diff] [blame] | 88 | NET_LOG_ERROR("Error parsing state value", path() + "." + key); |
[email protected] | 1c8edf1 | 2013-05-15 10:01:26 | [diff] [blame] | 89 | return false; |
| 90 | } |
| 91 | if (*out_value == new_value) |
| 92 | return false; |
| 93 | *out_value = new_value; |
| 94 | return true; |
[email protected] | 3c8bd11 | 2012-11-07 10:14:59 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | bool ManagedState::GetStringValue(const std::string& key, |
| 98 | const base::Value& value, |
| 99 | std::string* out_value) { |
[email protected] | 1c8edf1 | 2013-05-15 10:01:26 | [diff] [blame] | 100 | std::string new_value; |
| 101 | if (!value.GetAsString(&new_value)) { |
[email protected] | 5092ea87 | 2013-05-16 21:56:08 | [diff] [blame] | 102 | NET_LOG_ERROR("Error parsing state value", path() + "." + key); |
[email protected] | 1c8edf1 | 2013-05-15 10:01:26 | [diff] [blame] | 103 | return false; |
| 104 | } |
| 105 | if (*out_value == new_value) |
| 106 | return false; |
| 107 | *out_value = new_value; |
| 108 | return true; |
[email protected] | 3c8bd11 | 2012-11-07 10:14:59 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | } // namespace chromeos |