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 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
|
// Copyright 2019 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/sync_wifi/synced_network_updater_impl.h"
#include "base/functional/bind.h"
#include "base/values.h"
#include "chromeos/ash/components/network/network_configuration_handler.h"
#include "chromeos/ash/components/network/network_event_log.h"
#include "chromeos/ash/components/network/network_handler.h"
#include "chromeos/ash/components/network/network_metadata_store.h"
#include "chromeos/ash/components/network/network_profile_handler.h"
#include "chromeos/ash/components/network/network_state.h"
#include "chromeos/ash/components/sync_wifi/network_type_conversions.h"
#include "chromeos/ash/components/timer_factory/timer_factory.h"
#include "components/device_event_log/device_event_log.h"
#include "components/proxy_config/proxy_config_dictionary.h"
#include "third_party/cros_system_api/dbus/shill/dbus-constants.h"
namespace ash::network_config {
namespace mojom = ::chromeos::network_config::mojom;
}
namespace ash::sync_wifi {
namespace {
const int kMaxRetries = 3;
constexpr base::TimeDelta kTimeout = base::Minutes(1);
} // namespace
SyncedNetworkUpdaterImpl::SyncedNetworkUpdaterImpl(
std::unique_ptr<PendingNetworkConfigurationTracker> tracker,
network_config::mojom::CrosNetworkConfig* cros_network_config,
ash::timer_factory::TimerFactory* timer_factory,
SyncedNetworkMetricsLogger* metrics_logger)
: tracker_(std::move(tracker)),
cros_network_config_(cros_network_config),
timer_factory_(timer_factory),
metrics_logger_(metrics_logger) {
cros_network_config_->AddObserver(
cros_network_config_observer_receiver_.BindNewPipeAndPassRemote());
// Load the current list of networks.
OnNetworkStateListChanged();
std::vector<PendingNetworkConfigurationUpdate> updates =
tracker_->GetPendingUpdates();
for (const PendingNetworkConfigurationUpdate& update : updates)
Retry(update);
}
SyncedNetworkUpdaterImpl::~SyncedNetworkUpdaterImpl() = default;
void SyncedNetworkUpdaterImpl::AddOrUpdateNetwork(
const sync_pb::WifiConfigurationSpecifics& specifics) {
auto id = NetworkIdentifier::FromProto(specifics);
std::string change_guid = tracker_->TrackPendingUpdate(id, specifics);
StartAddOrUpdateOperation(change_guid, id, specifics);
}
void SyncedNetworkUpdaterImpl::StartAddOrUpdateOperation(
const std::string& change_guid,
const NetworkIdentifier& id,
const sync_pb::WifiConfigurationSpecifics& specifics) {
network_config::mojom::NetworkStatePropertiesPtr existing_network =
FindMojoNetwork(id);
network_config::mojom::ConfigPropertiesPtr config =
MojoNetworkConfigFromProto(specifics);
// We can get into this state when the SSID's are improperly encoded hex
// strings, which causes the proto conversion above to return a nullptr.
//
// TODO(b/270151177) : Dig into to understand why we are getting improperly
// encoded SSIDs.
if (!config) {
NET_LOG(ERROR) << "Failed to generate local network config";
metrics_logger_->RecordApplyGenerateLocalNetworkConfig(/*success=*/false);
return;
}
metrics_logger_->RecordApplyGenerateLocalNetworkConfig(/*success=*/true);
StartTimer(change_guid, id);
if (existing_network) {
NET_LOG(EVENT) << "Updating existing network "
<< NetworkGuidId(existing_network->guid);
if (network_guid_to_updates_counter_.contains(existing_network->guid))
network_guid_to_updates_counter_[existing_network->guid]++;
else
network_guid_to_updates_counter_[existing_network->guid] = 1;
cros_network_config_->SetProperties(
existing_network->guid, std::move(config),
base::BindOnce(&SyncedNetworkUpdaterImpl::OnSetPropertiesResult,
weak_ptr_factory_.GetWeakPtr(), change_guid,
existing_network->guid, specifics));
return;
}
NET_LOG(EVENT) << "Adding new network configuration.";
cros_network_config_->ConfigureNetwork(
std::move(config), /*shared=*/false,
base::BindOnce(&SyncedNetworkUpdaterImpl::OnConfigureNetworkResult,
weak_ptr_factory_.GetWeakPtr(), change_guid, specifics));
}
void SyncedNetworkUpdaterImpl::RemoveNetwork(const NetworkIdentifier& id) {
network_config::mojom::NetworkStatePropertiesPtr network =
FindMojoNetwork(id);
if (!network) {
NET_LOG(EVENT) << "Network not found, nothing to remove.";
return;
}
NET_LOG(EVENT) << "Removing network " << NetworkGuidId(network->guid);
std::string change_guid =
tracker_->TrackPendingUpdate(id, /*specifics=*/std::nullopt);
StartDeleteOperation(change_guid, id, network->guid);
}
void SyncedNetworkUpdaterImpl::StartDeleteOperation(
const std::string& change_guid,
const NetworkIdentifier& id,
std::string guid) {
StartTimer(change_guid, id);
cros_network_config_->ForgetNetwork(
guid, base::BindOnce(&SyncedNetworkUpdaterImpl::OnForgetNetworkResult,
weak_ptr_factory_.GetWeakPtr(), change_guid, id));
}
bool SyncedNetworkUpdaterImpl::IsUpdateInProgress(
const std::string& network_guid) {
return network_guid_to_updates_counter_.contains(network_guid) &&
network_guid_to_updates_counter_[network_guid] > 0;
}
network_config::mojom::NetworkStatePropertiesPtr
SyncedNetworkUpdaterImpl::FindMojoNetwork(const NetworkIdentifier& id) {
for (const network_config::mojom::NetworkStatePropertiesPtr& network :
networks_) {
if (id == NetworkIdentifier::FromMojoNetwork(network))
return network.Clone();
}
return nullptr;
}
void SyncedNetworkUpdaterImpl::OnNetworkStateListChanged() {
cros_network_config_->GetNetworkStateList(
network_config::mojom::NetworkFilter::New(
network_config::mojom::FilterType::kConfigured,
network_config::mojom::NetworkType::kWiFi,
/*limit=*/0),
base::BindOnce(&SyncedNetworkUpdaterImpl::OnGetNetworkList,
base::Unretained(this)));
}
void SyncedNetworkUpdaterImpl::OnGetNetworkList(
std::vector<network_config::mojom::NetworkStatePropertiesPtr> networks) {
networks_ = std::move(networks);
}
void SyncedNetworkUpdaterImpl::OnConfigureNetworkResult(
const std::string& change_guid,
const sync_pb::WifiConfigurationSpecifics& proto,
const std::optional<std::string>& network_guid,
const std::string& error_message) {
auto id = NetworkIdentifier::FromProto(proto);
if (network_guid) {
NET_LOG(EVENT) << "Successfully configured network "
<< NetworkGuidId(*network_guid);
NetworkMetadataStore* metadata_store =
NetworkHandler::Get()->network_metadata_store();
metadata_store->SetIsConfiguredBySync(*network_guid);
metadata_store->SetLastConnectedTimestamp(
*network_guid, base::Milliseconds(proto.last_connected_timestamp()));
} else {
NET_LOG(ERROR) << "Failed to configure network "
<< NetworkId(NetworkStateFromNetworkIdentifier(id))
<< " because: " << error_message;
metrics_logger_->RecordApplyNetworkFailureReason(
ApplyNetworkFailureReason::kFailedToAdd, error_message);
}
HandleShillResult(change_guid, id, network_guid.has_value());
}
void SyncedNetworkUpdaterImpl::OnSetPropertiesResult(
const std::string& change_guid,
const std::string& network_guid,
const sync_pb::WifiConfigurationSpecifics& proto,
bool is_success,
const std::string& error_message) {
if (is_success) {
NET_LOG(EVENT) << "Successfully updated network "
<< NetworkGuidId(network_guid);
NetworkMetadataStore* metadata_store =
NetworkHandler::Get()->network_metadata_store();
metadata_store->SetIsConfiguredBySync(network_guid);
metadata_store->SetLastConnectedTimestamp(
network_guid, base::Milliseconds(proto.last_connected_timestamp()));
} else {
NET_LOG(ERROR) << "Failed to update network "
<< NetworkGuidId(network_guid);
metrics_logger_->RecordApplyNetworkFailureReason(
ApplyNetworkFailureReason::kFailedToUpdate, error_message);
}
if (network_guid_to_updates_counter_.contains(network_guid))
network_guid_to_updates_counter_[network_guid]--;
HandleShillResult(change_guid, NetworkIdentifier::FromProto(proto),
is_success);
}
void SyncedNetworkUpdaterImpl::OnForgetNetworkResult(
const std::string& change_guid,
const NetworkIdentifier& id,
bool is_success) {
if (is_success) {
NET_LOG(EVENT) << "Successfully deleted network for change " << change_guid;
} else {
NET_LOG(ERROR) << "Failed to remove network for change " << change_guid;
metrics_logger_->RecordApplyNetworkFailureReason(
ApplyNetworkFailureReason::kFailedToRemove, "");
}
HandleShillResult(change_guid, id, is_success);
}
void SyncedNetworkUpdaterImpl::HandleShillResult(const std::string& change_guid,
const NetworkIdentifier& id,
bool is_success) {
change_guid_to_timer_map_.erase(change_guid);
if (!tracker_->GetPendingUpdate(change_guid, id)) {
NET_LOG(EVENT)
<< "Update to network with change_guid " << change_guid
<< " is no longer pending. This is likely because the change was"
" preempted by another update to the same network.";
return;
}
if (is_success) {
tracker_->MarkComplete(change_guid, id);
metrics_logger_->RecordApplyNetworkSuccess();
return;
}
tracker_->IncrementCompletedAttempts(change_guid, id);
std::optional<PendingNetworkConfigurationUpdate> update =
tracker_->GetPendingUpdate(change_guid, id);
if (update->completed_attempts() >= kMaxRetries) {
NET_LOG(ERROR) << "Ran out of retries for change " << change_guid
<< " to network "
<< NetworkId(NetworkStateFromNetworkIdentifier(id));
tracker_->MarkComplete(change_guid, id);
metrics_logger_->RecordApplyNetworkFailed();
return;
}
Retry(*update);
}
void SyncedNetworkUpdaterImpl::CleanupUpdate(const std::string& change_guid,
const NetworkIdentifier& id) {
tracker_->MarkComplete(change_guid, id);
}
void SyncedNetworkUpdaterImpl::Retry(
const PendingNetworkConfigurationUpdate& update) {
if (update.IsDeleteOperation()) {
network_config::mojom::NetworkStatePropertiesPtr network =
FindMojoNetwork(update.id());
if (!network) {
tracker_->MarkComplete(update.change_guid(), update.id());
return;
}
StartDeleteOperation(update.change_guid(), update.id(), network->guid);
return;
}
StartAddOrUpdateOperation(update.change_guid(), update.id(),
update.specifics().value());
}
void SyncedNetworkUpdaterImpl::StartTimer(const std::string& change_guid,
const NetworkIdentifier& id) {
change_guid_to_timer_map_[change_guid] = timer_factory_->CreateOneShotTimer();
change_guid_to_timer_map_[change_guid]->Start(
FROM_HERE, kTimeout,
base::BindOnce(&SyncedNetworkUpdaterImpl::OnTimeout,
base::Unretained(this), change_guid, id));
}
void SyncedNetworkUpdaterImpl::OnTimeout(const std::string& change_guid,
const NetworkIdentifier& id) {
NET_LOG(ERROR) << "Failed to update network, operation timed out.";
metrics_logger_->RecordApplyNetworkFailureReason(
ApplyNetworkFailureReason::kTimedout, "");
HandleShillResult(change_guid, id, /*is_success=*/false);
}
} // namespace ash::sync_wifi
|