1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chromeos/ash/components/network/managed_state.h"
#include <stdint.h>
#include <memory>
#include "base/logging.h"
#include "base/values.h"
#include "chromeos/ash/components/network/device_state.h"
#include "chromeos/ash/components/network/network_event_log.h"
#include "chromeos/ash/components/network/network_state.h"
#include "chromeos/ash/components/network/network_type_pattern.h"
#include "third_party/cros_system_api/dbus/service_constants.h"
namespace ash {
bool ManagedState::Matches(const NetworkTypePattern& pattern) const {
return pattern.MatchesType(type());
}
// static
std::string ManagedState::TypeToString(ManagedType type) {
switch (type) {
case MANAGED_TYPE_NETWORK:
return "Network";
case MANAGED_TYPE_DEVICE:
return "Device";
}
return "Unknown";
}
ManagedState::ManagedState(ManagedType type, const std::string& path)
: managed_type_(type), path_(path) {}
ManagedState::~ManagedState() = default;
std::unique_ptr<ManagedState> ManagedState::Create(ManagedType type,
const std::string& path) {
switch (type) {
case MANAGED_TYPE_NETWORK:
return std::make_unique<NetworkState>(path);
case MANAGED_TYPE_DEVICE:
return std::make_unique<DeviceState>(path);
}
return nullptr;
}
NetworkState* ManagedState::AsNetworkState() {
if (managed_type() == MANAGED_TYPE_NETWORK)
return static_cast<NetworkState*>(this);
return nullptr;
}
const NetworkState* ManagedState::AsNetworkState() const {
if (managed_type() == MANAGED_TYPE_NETWORK)
return static_cast<const NetworkState*>(this);
return nullptr;
}
DeviceState* ManagedState::AsDeviceState() {
if (managed_type() == MANAGED_TYPE_DEVICE)
return static_cast<DeviceState*>(this);
return nullptr;
}
const DeviceState* ManagedState::AsDeviceState() const {
if (managed_type() == MANAGED_TYPE_DEVICE)
return static_cast<const DeviceState*>(this);
return nullptr;
}
bool ManagedState::InitialPropertiesReceived(
const base::Value::Dict& properties) {
return false;
}
void ManagedState::GetStateProperties(base::Value::Dict* dictionary) const {
dictionary->Set(shill::kNameProperty, name());
dictionary->Set(shill::kTypeProperty, type());
}
bool ManagedState::ManagedStatePropertyChanged(const std::string& key,
const base::Value& value) {
if (key == shill::kNameProperty) {
return GetStringValue(key, value, &name_);
} else if (key == shill::kTypeProperty) {
return GetStringValue(key, value, &type_);
}
return false;
}
bool ManagedState::GetBooleanValue(const std::string& key,
const base::Value& value,
bool* out_value) {
if (!value.is_bool()) {
NET_LOG(ERROR) << "Error parsing state value: " << NetworkPathId(path_)
<< "." << key;
return false;
}
bool new_value = value.GetBool();
if (*out_value == new_value)
return false;
*out_value = new_value;
return true;
}
bool ManagedState::GetIntegerValue(const std::string& key,
const base::Value& value,
int* out_value) {
if (!value.is_int()) {
NET_LOG(ERROR) << "Error parsing state value: " << NetworkPathId(path_)
<< "." << key;
return false;
}
if (*out_value == value.GetInt())
return false;
*out_value = value.GetInt();
return true;
}
bool ManagedState::GetStringValue(const std::string& key,
const base::Value& value,
std::string* out_value) {
if (!value.is_string()) {
NET_LOG(ERROR) << "Error parsing state value: " << NetworkPathId(path_)
<< "." << key;
return false;
}
if (*out_value == value.GetString())
return false;
*out_value = value.GetString();
return true;
}
bool ManagedState::GetUInt32Value(const std::string& key,
const base::Value& value,
uint32_t* out_value) {
// base::Value restricts the number types to BOOL, INTEGER, and DOUBLE only.
// uint32_t will automatically get converted to a double, which is why we try
// to obtain the value as a double (see dbus/values_util.h).
uint32_t new_value;
double double_value = value.GetIfDouble().value_or(-1);
if (double_value < 0) {
NET_LOG(ERROR) << "Error parsing state value: " << NetworkPathId(path_)
<< "." << key;
return false;
}
new_value = static_cast<uint32_t>(double_value);
if (*out_value == new_value)
return false;
*out_value = new_value;
return true;
}
} // namespace ash
|