Commit 44f15333 authored by Meredith Lane's avatar Meredith Lane Committed by Commit Bot

Revert "[Wifi Sync] Implement the SyncedNetworkUpdater class."

This reverts commit a6f0d9ae.

Reason for revert: Causing failures in chromeos_components_unittests.
Bug: https://crbug.com/1018633

Original change's description:
> [Wifi Sync] Implement the SyncedNetworkUpdater class.
> 
> This is responsible for updating the local network stack to match
> incoming WifiConfigurationSpecifics protos from the sync backend.
> 
> Bug: 966270
> Change-Id: Ib4bdf1c22604b2528bc2abad5bc2ac1672e58290
> Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1768877
> Commit-Queue: Jon Mann <jonmann@chromium.org>
> Reviewed-by: Xiyuan Xia <xiyuan@chromium.org>
> Reviewed-by: Steven Bennetts <stevenjb@chromium.org>
> Reviewed-by: Kyle Horimoto <khorimoto@chromium.org>
> Cr-Commit-Position: refs/heads/master@{#709627}

TBR=xiyuan@chromium.org,stevenjb@chromium.org,khorimoto@chromium.org,jonmann@chromium.org

# Not skipping CQ checks because original CL landed > 1 day ago.

Bug: 966270
Change-Id: Idce7f0208c06412b9c950e3f833f50b5f15a816b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1882201Reviewed-by: default avatarMeredith Lane <meredithl@chromium.org>
Commit-Queue: Meredith Lane <meredithl@chromium.org>
Cr-Commit-Position: refs/heads/master@{#709792}
parent 4f491087
......@@ -39,10 +39,10 @@ WifiConfigurationSyncServiceFactory::~WifiConfigurationSyncServiceFactory() =
KeyedService* WifiConfigurationSyncServiceFactory::BuildServiceInstanceFor(
content::BrowserContext* context) const {
Profile* profile = Profile::FromBrowserContext(context);
return new chromeos::sync_wifi::WifiConfigurationSyncService(
chrome::GetChannel(), profile->GetPrefs(),
ModelTypeStoreServiceFactory::GetForProfile(profile)->GetStoreFactory());
chrome::GetChannel(), ModelTypeStoreServiceFactory::GetForProfile(
Profile::FromBrowserContext(context))
->GetStoreFactory());
}
void WifiConfigurationSyncServiceFactory::RegisterProfilePrefs(
......
......@@ -6,30 +6,22 @@ static_library("sync_wifi") {
sources = [
"network_identifier.cc",
"network_identifier.h",
"network_type_conversions.cc",
"network_type_conversions.h",
"pending_network_configuration_tracker.h",
"pending_network_configuration_tracker_impl.cc",
"pending_network_configuration_tracker_impl.h",
"pending_network_configuration_update.cc",
"pending_network_configuration_update.h",
"synced_network_updater.h",
"synced_network_updater_impl.cc",
"synced_network_updater_impl.h",
"wifi_configuration_bridge.cc",
"wifi_configuration_bridge.h",
"wifi_configuration_sync_service.cc",
"wifi_configuration_sync_service.h",
]
deps = [
"//ash/public/cpp",
"//base",
"//chromeos/network",
"//chromeos/services/network_config/public/mojom",
"//components/device_event_log",
"//chromeos/network:network",
"//components/keyed_service/core",
"//components/prefs",
"//components/proxy_config",
"//components/prefs:prefs",
"//components/sync",
"//components/version_info",
]
......@@ -38,8 +30,6 @@ static_library("sync_wifi") {
source_set("test_support") {
testonly = true
sources = [
"fake_pending_network_configuration_tracker.cc",
"fake_pending_network_configuration_tracker.h",
"test_data_generator.cc",
"test_data_generator.h",
]
......@@ -55,24 +45,16 @@ source_set("unit_tests") {
sources = [
"network_identifier_unittest.cc",
"pending_network_configuration_tracker_impl_unittest.cc",
"synced_network_updater_impl_unittest.cc",
"wifi_configuration_bridge_unittest.cc",
]
deps = [
":sync_wifi",
":test_support",
"//ash/public/cpp",
"//base/test:test_support",
"//chromeos/dbus/shill:test_support",
"//chromeos/login/login_state",
"//chromeos/login/login_state:test_support",
"//chromeos/network:test_support",
"//chromeos/services/network_config",
"//chromeos/services/network_config:in_process_instance",
"//chromeos/services/network_config/public/cpp:test_support",
"//components/sync:test_support",
"//components/sync_preferences:test_support",
"//components/user_manager:test_support",
"//testing/gtest",
]
}
include_rules = [
"+ash/public/cpp",
"+chromeos/network",
"+components/keyed_service/core",
"+components/prefs",
"+components/proxy_config",
"+components/user_manager",
"+components/version_info",
"+components/sync",
"+components/sync_preferences",
"+components/sync_preferences"
]
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chromeos/components/sync_wifi/fake_pending_network_configuration_tracker.h"
#include "base/guid.h"
#include "base/stl_util.h"
#include "chromeos/components/sync_wifi/pending_network_configuration_update.h"
namespace chromeos {
namespace sync_wifi {
FakePendingNetworkConfigurationTracker::
FakePendingNetworkConfigurationTracker() = default;
FakePendingNetworkConfigurationTracker::
~FakePendingNetworkConfigurationTracker() = default;
std::string FakePendingNetworkConfigurationTracker::TrackPendingUpdate(
const NetworkIdentifier& id,
const base::Optional<sync_pb::WifiConfigurationSpecificsData>& specifics) {
std::string change_id = base::GenerateGUID();
id_to_pending_update_map_.emplace(
id, PendingNetworkConfigurationUpdate(id, change_id, specifics,
/*completed_attempts=*/0));
id_to_completed_attempts_map_[id] = 0;
return change_id;
}
void FakePendingNetworkConfigurationTracker::MarkComplete(
const std::string& change_guid,
const NetworkIdentifier& id) {
if (base::Contains(id_to_pending_update_map_, id) &&
id_to_pending_update_map_.at(id).change_guid() == change_guid) {
id_to_pending_update_map_.erase(id);
}
}
void FakePendingNetworkConfigurationTracker::IncrementCompletedAttempts(
const std::string& change_id,
const NetworkIdentifier& id) {
base::Optional<PendingNetworkConfigurationUpdate> existing_update =
GetPendingUpdate(change_id, id);
id_to_pending_update_map_.emplace(
std::piecewise_construct, std::forward_as_tuple(id),
std::forward_as_tuple(id, change_id, existing_update->specifics(),
existing_update->completed_attempts() + 1));
id_to_completed_attempts_map_[id]++;
}
std::vector<PendingNetworkConfigurationUpdate>
FakePendingNetworkConfigurationTracker::GetPendingUpdates() {
std::vector<PendingNetworkConfigurationUpdate> list;
for (const auto& it : id_to_pending_update_map_)
list.emplace_back(it.second);
return list;
}
base::Optional<PendingNetworkConfigurationUpdate>
FakePendingNetworkConfigurationTracker::GetPendingUpdate(
const std::string& change_guid,
const NetworkIdentifier& id) {
if (!base::Contains(id_to_pending_update_map_, id) ||
id_to_pending_update_map_.at(id).change_guid() != change_guid) {
return base::nullopt;
}
return id_to_pending_update_map_.at(id);
}
PendingNetworkConfigurationUpdate*
FakePendingNetworkConfigurationTracker::GetPendingUpdateById(
const NetworkIdentifier& id) {
if (!base::Contains(id_to_pending_update_map_, id))
return nullptr;
return &id_to_pending_update_map_.at(id);
}
int FakePendingNetworkConfigurationTracker::GetCompletedAttempts(
const NetworkIdentifier& id) {
DCHECK(id_to_completed_attempts_map_.count(id));
return id_to_completed_attempts_map_[id];
}
} // namespace sync_wifi
} // namespace chromeos
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROMEOS_COMPONENTS_SYNC_WIFI_FAKE_PENDING_NETWORK_CONFIGURATION_TRACKER_H_
#define CHROMEOS_COMPONENTS_SYNC_WIFI_FAKE_PENDING_NETWORK_CONFIGURATION_TRACKER_H_
#include <map>
#include "base/containers/flat_map.h"
#include "base/optional.h"
#include "chromeos/components/sync_wifi/network_identifier.h"
#include "chromeos/components/sync_wifi/pending_network_configuration_tracker.h"
namespace chromeos {
namespace sync_wifi {
class FakePendingNetworkConfigurationTracker
: public PendingNetworkConfigurationTracker {
public:
FakePendingNetworkConfigurationTracker();
~FakePendingNetworkConfigurationTracker() override;
// sync_wifi::PendingNetworkConfigurationtracker::
std::string TrackPendingUpdate(
const NetworkIdentifier& id,
const base::Optional<sync_pb::WifiConfigurationSpecificsData>& specifics)
override;
void MarkComplete(const std::string& change_guid,
const NetworkIdentifier& id) override;
void IncrementCompletedAttempts(const std::string& change_id,
const NetworkIdentifier& id) override;
std::vector<PendingNetworkConfigurationUpdate> GetPendingUpdates() override;
base::Optional<PendingNetworkConfigurationUpdate> GetPendingUpdate(
const std::string& change_guid,
const NetworkIdentifier& id) override;
// Get the matching PendingNetworkConfigurationUpdate by for a given |id|.
// This is needed because some tests don't have insight into what
// change_guids are used.
PendingNetworkConfigurationUpdate* GetPendingUpdateById(
const NetworkIdentifier& id);
// This includes ssids which have already been removed from the tracker.
int GetCompletedAttempts(const NetworkIdentifier& id);
private:
std::map<NetworkIdentifier, PendingNetworkConfigurationUpdate>
id_to_pending_update_map_;
// This map is not cleared when MarkComplete is called to allow tests to
// verify that the expected number of retries were performed before removal.
std::map<NetworkIdentifier, int> id_to_completed_attempts_map_;
DISALLOW_COPY_AND_ASSIGN(FakePendingNetworkConfigurationTracker);
};
} // namespace sync_wifi
} // namespace chromeos
#endif // CHROMEOS_COMPONENTS_SYNC_WIFI_FAKE_PENDING_NETWORK_CONFIGURATION_TRACKER_H_
......@@ -7,9 +7,8 @@
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "chromeos/components/sync_wifi/network_type_conversions.h"
#include "chromeos/services/network_config/public/mojom/cros_network_config.mojom.h"
#include "components/sync/protocol/wifi_configuration_specifics.pb.h"
#include "chromeos/network/network_state.h"
#include "components/sync/protocol/model_type_state.pb.h"
#include "third_party/cros_system_api/dbus/shill/dbus-constants.h"
namespace chromeos {
......@@ -20,22 +19,32 @@ namespace {
const char kDelimeter[] = "_";
std::string GetSecurityType(
sync_pb::WifiConfigurationSpecificsData_SecurityType security_type) {
switch (security_type) {
case sync_pb::WifiConfigurationSpecificsData::SECURITY_TYPE_PSK:
return shill::kSecurityPsk;
case sync_pb::WifiConfigurationSpecificsData::SECURITY_TYPE_WEP:
return shill::kSecurityWep;
default:
NOTREACHED();
return "";
}
}
} // namespace
// static
NetworkIdentifier NetworkIdentifier::FromProto(
const sync_pb::WifiConfigurationSpecificsData& specifics) {
return NetworkIdentifier(
specifics.hex_ssid(),
SecurityTypeStringFromProto(specifics.security_type()));
return NetworkIdentifier(specifics.hex_ssid(),
GetSecurityType(specifics.security_type()));
}
// static
NetworkIdentifier NetworkIdentifier::FromMojoNetwork(
const network_config::mojom::NetworkStatePropertiesPtr& network) {
return NetworkIdentifier(
network->type_state->get_wifi()->hex_ssid,
SecurityTypeStringFromMojo(network->type_state->get_wifi()->security));
NetworkIdentifier NetworkIdentifier::FromNetwork(
const chromeos::NetworkState& network) {
return NetworkIdentifier(network.GetHexSsid(), network.security_class());
}
// static
......@@ -68,18 +77,6 @@ bool NetworkIdentifier::operator==(const NetworkIdentifier& o) const {
return hex_ssid_ == o.hex_ssid_ && security_type_ == o.security_type_;
}
bool NetworkIdentifier::operator!=(const NetworkIdentifier& o) const {
return hex_ssid_ != o.hex_ssid_ || security_type_ != o.security_type_;
}
bool NetworkIdentifier::operator>(const NetworkIdentifier& o) const {
return *this != o && !(*this < o);
}
bool NetworkIdentifier::operator<(const NetworkIdentifier& o) const {
return SerializeToString() < o.SerializeToString();
}
} // namespace sync_wifi
} // namespace chromeos
......@@ -7,14 +7,14 @@
#include <string>
#include "chromeos/services/network_config/public/mojom/cros_network_config.mojom-forward.h"
namespace sync_pb {
class WifiConfigurationSpecificsData;
}
namespace chromeos {
class NetworkState;
namespace sync_wifi {
// A unique identifier for synced networks which contains the properties
......@@ -23,8 +23,7 @@ class NetworkIdentifier {
public:
static NetworkIdentifier FromProto(
const sync_pb::WifiConfigurationSpecificsData& specifics);
static NetworkIdentifier FromMojoNetwork(
const network_config::mojom::NetworkStatePropertiesPtr& network);
static NetworkIdentifier FromNetwork(const chromeos::NetworkState& network);
// |serialized_string| is in the format of hex_ssid and security_type
// concatenated with an underscore. security_type is the shill constant
// returned from NetworkState::security_class(). For example, it would be
......@@ -41,9 +40,6 @@ class NetworkIdentifier {
virtual ~NetworkIdentifier();
bool operator==(const NetworkIdentifier& o) const;
bool operator!=(const NetworkIdentifier& o) const;
bool operator<(const NetworkIdentifier& o) const;
bool operator>(const NetworkIdentifier& o) const;
std::string SerializeToString() const;
......
......@@ -38,6 +38,16 @@ TEST_F(NetworkIdentifierTest, FromProto) {
EXPECT_EQ(expected_id, id);
}
TEST_F(NetworkIdentifierTest, FromNetwork) {
chromeos::NetworkState network("dummy_path");
network.PropertyChanged(shill::kWifiHexSsid, base::Value(kHexSsid));
network.PropertyChanged(shill::kSecurityClassProperty,
base::Value(shill::kSecurityPsk));
NetworkIdentifier id = NetworkIdentifier::FromNetwork(network);
EXPECT_EQ(kHexSsid, id.hex_ssid());
EXPECT_EQ(shill::kSecurityPsk, id.security_type());
}
TEST_F(NetworkIdentifierTest, FromString) {
std::string string_id("0123456789ABCDEF_psk");
NetworkIdentifier id = NetworkIdentifier::DeserializeFromString(string_id);
......
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chromeos/components/sync_wifi/network_type_conversions.h"
#include "base/strings/string_number_conversions.h"
#include "chromeos/components/sync_wifi/network_identifier.h"
#include "chromeos/network/network_handler.h"
#include "chromeos/network/network_state.h"
#include "chromeos/network/network_state_handler.h"
#include "chromeos/services/network_config/public/mojom/cros_network_config.mojom.h"
#include "third_party/cros_system_api/dbus/shill/dbus-constants.h"
namespace chromeos {
namespace sync_wifi {
namespace {
std::string DecodeHexString(const std::string& base_16) {
std::string decoded;
DCHECK_EQ(base_16.size() % 2, 0u) << "Must be a multiple of 2";
decoded.reserve(base_16.size() / 2);
std::vector<uint8_t> v;
if (!base::HexStringToBytes(base_16, &v)) {
NOTREACHED();
}
decoded.assign(reinterpret_cast<const char*>(&v[0]), v.size());
return decoded;
}
} // namespace
std::string SecurityTypeStringFromMojo(
const network_config::mojom::SecurityType& security_type) {
switch (security_type) {
case network_config::mojom::SecurityType::kWpaPsk:
return shill::kSecurityPsk;
case network_config::mojom::SecurityType::kWepPsk:
return shill::kSecurityWep;
default:
// Only PSK and WEP secured networks are supported by sync.
NOTREACHED();
return "";
}
}
std::string SecurityTypeStringFromProto(
const sync_pb::WifiConfigurationSpecificsData_SecurityType& security_type) {
switch (security_type) {
case sync_pb::WifiConfigurationSpecificsData::SECURITY_TYPE_PSK:
return shill::kSecurityPsk;
case sync_pb::WifiConfigurationSpecificsData::SECURITY_TYPE_WEP:
return shill::kSecurityWep;
default:
// Only PSK and WEP secured networks are supported by sync.
NOTREACHED();
return "";
}
}
network_config::mojom::SecurityType MojoSecurityTypeFromProto(
const sync_pb::WifiConfigurationSpecificsData_SecurityType& security_type) {
switch (security_type) {
case sync_pb::WifiConfigurationSpecificsData::SECURITY_TYPE_PSK:
return network_config::mojom::SecurityType::kWpaPsk;
case sync_pb::WifiConfigurationSpecificsData::SECURITY_TYPE_WEP:
return network_config::mojom::SecurityType::kWepPsk;
default:
// Only PSK and WEP secured networks are supported by sync.
NOTREACHED();
return network_config::mojom::SecurityType::kNone;
}
}
network_config::mojom::ConfigPropertiesPtr MojoNetworkConfigFromProto(
const sync_pb::WifiConfigurationSpecificsData& specifics) {
auto config = network_config::mojom::ConfigProperties::New();
auto wifi = network_config::mojom::WiFiConfigProperties::New();
wifi->ssid = DecodeHexString(specifics.hex_ssid());
wifi->security = MojoSecurityTypeFromProto(specifics.security_type());
wifi->passphrase = specifics.passphrase();
config->type_config =
network_config::mojom::NetworkTypeConfigProperties::NewWifi(
std::move(wifi));
config->auto_connect = network_config::mojom::AutoConnectConfig::New(
specifics.automatically_connect() ==
sync_pb::WifiConfigurationSpecificsData::AUTOMATICALLY_CONNECT_ENABLED);
config->priority = network_config::mojom::PriorityConfig::New(
specifics.is_preferred() ==
sync_pb::WifiConfigurationSpecificsData::IS_PREFERRED_ENABLED
? 1
: 0);
return config;
}
} // namespace sync_wifi
} // namespace chromeos
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROMEOS_COMPONENTS_SYNC_WIFI_NETWORK_TYPE_CONVERSIONS_H_
#define CHROMEOS_COMPONENTS_SYNC_WIFI_NETWORK_TYPE_CONVERSIONS_H_
#include "chromeos/services/network_config/public/mojom/cros_network_config.mojom.h"
#include "components/sync/protocol/wifi_configuration_specifics.pb.h"
namespace chromeos {
namespace sync_wifi {
std::string SecurityTypeStringFromMojo(
const network_config::mojom::SecurityType& security_type);
std::string SecurityTypeStringFromProto(
const sync_pb::WifiConfigurationSpecificsData_SecurityType& security_type);
network_config::mojom::SecurityType MojoSecurityTypeFromString(
const std::string& security_type);
network_config::mojom::SecurityType MojoSecurityTypeFromProto(
const sync_pb::WifiConfigurationSpecificsData_SecurityType& security_type);
network_config::mojom::ConfigPropertiesPtr MojoNetworkConfigFromProto(
const sync_pb::WifiConfigurationSpecificsData& specifics);
} // namespace sync_wifi
} // namespace chromeos
#endif // CHROMEOS_COMPONENTS_SYNC_WIFI_NETWORK_TYPE_CONVERSIONS_H_
......@@ -28,8 +28,9 @@ class PendingNetworkConfigurationTracker {
// Adds an update to the list of in flight changes. |change_uuid| is a
// unique identifier for each update, |id| is the identifier for the network
// which is getting updated, and |specifics| should be nullopt if the network
// is being deleted. Returns the change_guid.
virtual std::string TrackPendingUpdate(
// is being deleted.
virtual void TrackPendingUpdate(
const std::string& change_guid,
const NetworkIdentifier& id,
const base::Optional<sync_pb::WifiConfigurationSpecificsData>&
specifics) = 0;
......
......@@ -4,7 +4,6 @@
#include "chromeos/components/sync_wifi/pending_network_configuration_tracker_impl.h"
#include "base/guid.h"
#include "base/optional.h"
#include "base/strings/stringprintf.h"
#include "chromeos/components/sync_wifi/network_identifier.h"
......@@ -67,7 +66,8 @@ PendingNetworkConfigurationTrackerImpl::PendingNetworkConfigurationTrackerImpl(
PendingNetworkConfigurationTrackerImpl::
~PendingNetworkConfigurationTrackerImpl() = default;
std::string PendingNetworkConfigurationTrackerImpl::TrackPendingUpdate(
void PendingNetworkConfigurationTrackerImpl::TrackPendingUpdate(
const std::string& change_guid,
const NetworkIdentifier& id,
const base::Optional<sync_pb::WifiConfigurationSpecificsData>& specifics) {
std::string serialized_specifics;
......@@ -76,15 +76,11 @@ std::string PendingNetworkConfigurationTrackerImpl::TrackPendingUpdate(
else
CHECK(specifics->SerializeToString(&serialized_specifics));
std::string change_guid = base::GenerateGUID();
dict_.SetPath(GeneratePath(id, kChangeGuidKey), base::Value(change_guid));
dict_.SetPath(GeneratePath(id, kSpecificsKey),
base::Value(serialized_specifics));
dict_.SetPath(GeneratePath(id, kCompletedAttemptsKey), base::Value(0));
pref_service_->Set(kPendingNetworkConfigurationsPref, dict_);
return change_guid;
}
void PendingNetworkConfigurationTrackerImpl::MarkComplete(
......
......@@ -28,7 +28,8 @@ class PendingNetworkConfigurationTrackerImpl
static void RegisterProfilePrefs(PrefRegistrySimple* registry);
// sync_wifi::PendingNetworkConfigurationTracker::
std::string TrackPendingUpdate(
void TrackPendingUpdate(
const std::string& change_guid,
const NetworkIdentifier& id,
const base::Optional<sync_pb::WifiConfigurationSpecificsData>& specifics)
override;
......
......@@ -22,6 +22,8 @@ namespace {
const char kFredSsid[] = "Fred";
const char kMangoSsid[] = "Mango";
const char kChangeGuid1[] = "change-1";
const char kChangeGuid2[] = "change-2";
const char kPendingNetworkConfigurationsPref[] =
"sync_wifi.pending_network_configuration_updates";
......@@ -98,109 +100,102 @@ class PendingNetworkConfigurationTrackerImplTest : public testing::Test {
};
TEST_F(PendingNetworkConfigurationTrackerImplTest, TestMarkComplete) {
std::string change_guid = tracker()->TrackPendingUpdate(
fred_network_id(), /*specifics=*/base::nullopt);
AssertTrackerHasMatchingUpdate(change_guid, fred_network_id());
tracker()->TrackPendingUpdate(kChangeGuid1, fred_network_id(),
/*specifics=*/base::nullopt);
AssertTrackerHasMatchingUpdate(kChangeGuid1, fred_network_id());
EXPECT_EQ(1u, GetPref()->DictSize());
EXPECT_TRUE(DoesPrefContainPendingUpdate(fred_network_id(), change_guid));
tracker()->MarkComplete(change_guid, fred_network_id());
EXPECT_FALSE(tracker()->GetPendingUpdate(change_guid, fred_network_id()));
EXPECT_TRUE(DoesPrefContainPendingUpdate(fred_network_id(), kChangeGuid1));
tracker()->MarkComplete(kChangeGuid1, fred_network_id());
EXPECT_FALSE(tracker()->GetPendingUpdate(kChangeGuid1, fred_network_id()));
EXPECT_EQ(0u, GetPref()->DictSize());
}
TEST_F(PendingNetworkConfigurationTrackerImplTest, TestTwoChangesSameNetwork) {
std::string change_guid =
tracker()->TrackPendingUpdate(fred_network_id(),
tracker()->TrackPendingUpdate(kChangeGuid1, fred_network_id(),
/*specifics=*/base::nullopt);
tracker()->IncrementCompletedAttempts(change_guid, fred_network_id());
AssertTrackerHasMatchingUpdate(change_guid, fred_network_id(),
tracker()->IncrementCompletedAttempts(kChangeGuid1, fred_network_id());
AssertTrackerHasMatchingUpdate(kChangeGuid1, fred_network_id(),
/*completed_attempts=*/1);
EXPECT_EQ(1u, GetPref()->DictSize());
EXPECT_EQ(1, tracker()
->GetPendingUpdate(change_guid, fred_network_id())
->GetPendingUpdate(kChangeGuid1, fred_network_id())
->completed_attempts());
std::string second_change_guid =
tracker()->TrackPendingUpdate(fred_network_id(),
tracker()->TrackPendingUpdate(kChangeGuid2, fred_network_id(),
/*specifics=*/base::nullopt);
EXPECT_FALSE(tracker()->GetPendingUpdate(change_guid, fred_network_id()));
AssertTrackerHasMatchingUpdate(second_change_guid, fred_network_id());
EXPECT_FALSE(tracker()->GetPendingUpdate(kChangeGuid1, fred_network_id()));
AssertTrackerHasMatchingUpdate(kChangeGuid2, fred_network_id());
EXPECT_EQ(0, tracker()
->GetPendingUpdate(second_change_guid, fred_network_id())
->GetPendingUpdate(kChangeGuid2, fred_network_id())
->completed_attempts());
EXPECT_EQ(1u, GetPref()->DictSize());
}
TEST_F(PendingNetworkConfigurationTrackerImplTest,
TestTwoChangesDifferentNetworks) {
std::string change_guid =
tracker()->TrackPendingUpdate(fred_network_id(),
tracker()->TrackPendingUpdate(kChangeGuid1, fred_network_id(),
/*specifics=*/base::nullopt);
AssertTrackerHasMatchingUpdate(change_guid, fred_network_id());
EXPECT_TRUE(DoesPrefContainPendingUpdate(fred_network_id(), change_guid));
AssertTrackerHasMatchingUpdate(kChangeGuid1, fred_network_id());
EXPECT_TRUE(DoesPrefContainPendingUpdate(fred_network_id(), kChangeGuid1));
EXPECT_EQ(1u, GetPref()->DictSize());
std::string second_change_guid =
tracker()->TrackPendingUpdate(mango_network_id(),
tracker()->TrackPendingUpdate(kChangeGuid2, mango_network_id(),
/*specifics=*/base::nullopt);
AssertTrackerHasMatchingUpdate(change_guid, fred_network_id());
AssertTrackerHasMatchingUpdate(second_change_guid, mango_network_id());
EXPECT_TRUE(DoesPrefContainPendingUpdate(fred_network_id(), change_guid));
EXPECT_TRUE(
DoesPrefContainPendingUpdate(mango_network_id(), second_change_guid));
AssertTrackerHasMatchingUpdate(kChangeGuid1, fred_network_id());
AssertTrackerHasMatchingUpdate(kChangeGuid2, mango_network_id());
EXPECT_TRUE(DoesPrefContainPendingUpdate(fred_network_id(), kChangeGuid1));
EXPECT_TRUE(DoesPrefContainPendingUpdate(mango_network_id(), kChangeGuid2));
EXPECT_EQ(2u, GetPref()->DictSize());
}
TEST_F(PendingNetworkConfigurationTrackerImplTest, TestGetPendingUpdates) {
std::string change_guid =
tracker()->TrackPendingUpdate(fred_network_id(),
tracker()->TrackPendingUpdate(kChangeGuid1, fred_network_id(),
/*specifics=*/base::nullopt);
std::string second_change_guid =
tracker()->TrackPendingUpdate(mango_network_id(),
tracker()->TrackPendingUpdate(kChangeGuid2, mango_network_id(),
/*specifics=*/base::nullopt);
std::vector<PendingNetworkConfigurationUpdate> list =
tracker()->GetPendingUpdates();
EXPECT_EQ(2u, list.size());
EXPECT_EQ(change_guid, list[0].change_guid());
EXPECT_EQ(kChangeGuid1, list[0].change_guid());
EXPECT_EQ(fred_network_id(), list[0].id());
EXPECT_EQ(second_change_guid, list[1].change_guid());
EXPECT_EQ(kChangeGuid2, list[1].change_guid());
EXPECT_EQ(mango_network_id(), list[1].id());
tracker()->MarkComplete(change_guid, fred_network_id());
tracker()->MarkComplete(kChangeGuid1, fred_network_id());
list = tracker()->GetPendingUpdates();
EXPECT_EQ(1u, list.size());
EXPECT_EQ(second_change_guid, list[0].change_guid());
EXPECT_EQ(kChangeGuid2, list[0].change_guid());
EXPECT_EQ(mango_network_id(), list[0].id());
}
TEST_F(PendingNetworkConfigurationTrackerImplTest, TestGetPendingUpdate) {
sync_pb::WifiConfigurationSpecificsData specifics =
GenerateTestWifiSpecifics(fred_network_id());
std::string change_guid =
tracker()->TrackPendingUpdate(fred_network_id(), specifics);
tracker()->TrackPendingUpdate(kChangeGuid1, fred_network_id(), specifics);
AssertTrackerHasMatchingUpdate(change_guid, fred_network_id(),
AssertTrackerHasMatchingUpdate(kChangeGuid1, fred_network_id(),
/*completed_attempts=*/0, specifics);
EXPECT_FALSE(tracker()->GetPendingUpdate(kChangeGuid2, mango_network_id()));
}
TEST_F(PendingNetworkConfigurationTrackerImplTest, TestRetryCounting) {
std::string change_guid =
tracker()->TrackPendingUpdate(fred_network_id(),
tracker()->TrackPendingUpdate(kChangeGuid1, fred_network_id(),
/*specifics=*/base::nullopt);
AssertTrackerHasMatchingUpdate(change_guid, fred_network_id());
AssertTrackerHasMatchingUpdate(kChangeGuid1, fred_network_id());
EXPECT_EQ(1u, GetPref()->DictSize());
EXPECT_EQ(0, tracker()
->GetPendingUpdate(change_guid, fred_network_id())
->GetPendingUpdate(kChangeGuid1, fred_network_id())
->completed_attempts());
tracker()->IncrementCompletedAttempts(change_guid, fred_network_id());
tracker()->IncrementCompletedAttempts(change_guid, fred_network_id());
tracker()->IncrementCompletedAttempts(change_guid, fred_network_id());
tracker()->IncrementCompletedAttempts(kChangeGuid1, fred_network_id());
tracker()->IncrementCompletedAttempts(kChangeGuid1, fred_network_id());
tracker()->IncrementCompletedAttempts(kChangeGuid1, fred_network_id());
EXPECT_EQ(3, tracker()
->GetPendingUpdate(change_guid, fred_network_id())
->GetPendingUpdate(kChangeGuid1, fred_network_id())
->completed_attempts());
tracker()->IncrementCompletedAttempts(change_guid, fred_network_id());
tracker()->IncrementCompletedAttempts(change_guid, fred_network_id());
tracker()->IncrementCompletedAttempts(kChangeGuid1, fred_network_id());
tracker()->IncrementCompletedAttempts(kChangeGuid1, fred_network_id());
EXPECT_EQ(5, tracker()
->GetPendingUpdate(change_guid, fred_network_id())
->GetPendingUpdate(kChangeGuid1, fred_network_id())
->completed_attempts());
}
......
......@@ -24,9 +24,6 @@ PendingNetworkConfigurationUpdate::PendingNetworkConfigurationUpdate(
PendingNetworkConfigurationUpdate::~PendingNetworkConfigurationUpdate() =
default;
PendingNetworkConfigurationUpdate& PendingNetworkConfigurationUpdate::operator=(
PendingNetworkConfigurationUpdate& update) = default;
bool PendingNetworkConfigurationUpdate::IsDeleteOperation() const {
return !specifics_.has_value();
}
......
......@@ -28,8 +28,6 @@ class PendingNetworkConfigurationUpdate {
int completed_attempts);
PendingNetworkConfigurationUpdate(
const PendingNetworkConfigurationUpdate& update);
PendingNetworkConfigurationUpdate& operator=(
PendingNetworkConfigurationUpdate& update);
virtual ~PendingNetworkConfigurationUpdate();
// The identifier for the network.
......@@ -51,9 +49,9 @@ class PendingNetworkConfigurationUpdate {
bool IsDeleteOperation() const;
private:
NetworkIdentifier id_;
std::string change_guid_;
base::Optional<sync_pb::WifiConfigurationSpecificsData> specifics_;
const NetworkIdentifier id_;
const std::string change_guid_;
const base::Optional<sync_pb::WifiConfigurationSpecificsData> specifics_;
int completed_attempts_;
};
......
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chromeos/components/sync_wifi/synced_network_updater_impl.h"
#include "base/bind.h"
#include "base/guid.h"
#include "base/values.h"
#include "chromeos/components/sync_wifi/network_type_conversions.h"
#include "chromeos/network/network_configuration_handler.h"
#include "chromeos/network/network_profile_handler.h"
#include "chromeos/network/network_state.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 chromeos {
namespace sync_wifi {
SyncedNetworkUpdaterImpl::SyncedNetworkUpdaterImpl(
std::unique_ptr<PendingNetworkConfigurationTracker> tracker,
network_config::mojom::CrosNetworkConfig* cros_network_config)
: tracker_(std::move(tracker)), cros_network_config_(cros_network_config) {
cros_network_config_->AddObserver(
cros_network_config_observer_receiver_.BindNewPipeAndPassRemote());
// Load the current list of networks.
OnNetworkStateListChanged();
}
SyncedNetworkUpdaterImpl::~SyncedNetworkUpdaterImpl() = default;
void SyncedNetworkUpdaterImpl::AddOrUpdateNetwork(
const sync_pb::WifiConfigurationSpecificsData& specifics) {
auto id = NetworkIdentifier::FromProto(specifics);
network_config::mojom::NetworkStatePropertiesPtr existing_network =
FindLocalNetwork(id);
std::string change_guid = tracker_->TrackPendingUpdate(id, specifics);
network_config::mojom::ConfigPropertiesPtr config =
MojoNetworkConfigFromProto(specifics);
if (existing_network) {
cros_network_config_->SetProperties(
existing_network->guid, std::move(config),
base::BindOnce(&SyncedNetworkUpdaterImpl::OnSetPropertiesResult,
weak_ptr_factory_.GetWeakPtr(), change_guid, id));
return;
}
cros_network_config_->ConfigureNetwork(
std::move(config), /* shared= */ false,
base::BindOnce(&SyncedNetworkUpdaterImpl::OnConfigureNetworkResult,
weak_ptr_factory_.GetWeakPtr(), change_guid, id));
}
void SyncedNetworkUpdaterImpl::RemoveNetwork(const NetworkIdentifier& id) {
network_config::mojom::NetworkStatePropertiesPtr network =
FindLocalNetwork(id);
if (!network)
return;
std::string change_guid =
tracker_->TrackPendingUpdate(id, /*specifics=*/base::nullopt);
cros_network_config_->ForgetNetwork(
network->guid,
base::BindOnce(&SyncedNetworkUpdaterImpl::OnForgetNetworkResult,
weak_ptr_factory_.GetWeakPtr(), change_guid, id));
}
network_config::mojom::NetworkStatePropertiesPtr
SyncedNetworkUpdaterImpl::FindLocalNetwork(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::OnError(const std::string& change_guid,
const NetworkIdentifier& id,
const std::string& error_name) {
NET_LOG(ERROR) << "Failed to update id:" << id.SerializeToString()
<< " error:" << error_name;
}
void SyncedNetworkUpdaterImpl::OnConfigureNetworkResult(
const std::string& change_guid,
const NetworkIdentifier& id,
const base::Optional<std::string>& guid,
const std::string& error_message) {
if (!guid) {
OnError(change_guid, id, "Failed to configure network.");
return;
}
VLOG(1) << "Successfully updated network with id " << id.SerializeToString();
CleanupUpdate(change_guid, id);
}
void SyncedNetworkUpdaterImpl::OnSetPropertiesResult(
const std::string& change_guid,
const NetworkIdentifier& id,
bool success,
const std::string& error_message) {
if (!success) {
OnError(change_guid, id, "Failed to update properties on network.");
return;
}
VLOG(1) << "Successfully updated network with id " << id.SerializeToString();
CleanupUpdate(change_guid, id);
}
void SyncedNetworkUpdaterImpl::OnForgetNetworkResult(
const std::string& change_guid,
const NetworkIdentifier& id,
bool success) {
if (!success) {
OnError(change_guid, id, "Failed to remove network.");
return;
}
VLOG(1) << "Successfully deleted network with id " << id.SerializeToString();
CleanupUpdate(change_guid, id);
}
void SyncedNetworkUpdaterImpl::CleanupUpdate(const std::string& change_guid,
const NetworkIdentifier& id) {
tracker_->MarkComplete(change_guid, id);
}
} // namespace sync_wifi
} // namespace chromeos
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROMEOS_COMPONENTS_SYNC_WIFI_SYNCED_NETWORK_UPDATER_IMPL_H_
#define CHROMEOS_COMPONENTS_SYNC_WIFI_SYNCED_NETWORK_UPDATER_IMPL_H_
#include "base/memory/weak_ptr.h"
#include "base/values.h"
#include "chromeos/components/sync_wifi/network_identifier.h"
#include "chromeos/components/sync_wifi/pending_network_configuration_tracker.h"
#include "chromeos/components/sync_wifi/synced_network_updater.h"
#include "chromeos/services/network_config/public/mojom/cros_network_config.mojom.h"
#include "components/sync/protocol/model_type_state.pb.h"
#include "mojo/public/cpp/bindings/receiver.h"
namespace chromeos {
namespace sync_wifi {
// Implementation of SyncedNetworkUpdater. This class takes add/update/delete
// requests from the sync backend and applies them to the local network stack
// using chromeos::NetworkConfigurationHandler.
class SyncedNetworkUpdaterImpl
: public SyncedNetworkUpdater,
public chromeos::network_config::mojom::CrosNetworkConfigObserver {
public:
// |cros_network_config| must outlive this class.
SyncedNetworkUpdaterImpl(
std::unique_ptr<PendingNetworkConfigurationTracker> tracker,
network_config::mojom::CrosNetworkConfig* cros_network_config);
~SyncedNetworkUpdaterImpl() override;
void AddOrUpdateNetwork(
const sync_pb::WifiConfigurationSpecificsData& specifics) override;
void RemoveNetwork(const NetworkIdentifier& id) override;
// CrosNetworkConfigObserver:
void OnNetworkStateListChanged() override;
void OnActiveNetworksChanged(
std::vector<
network_config::mojom::NetworkStatePropertiesPtr> /* networks */)
override {}
void OnNetworkStateChanged(
chromeos::network_config::mojom::NetworkStatePropertiesPtr /* network */)
override {}
void OnDeviceStateListChanged() override {}
void OnVpnProvidersChanged() override {}
void OnNetworkCertificatesChanged() override {}
private:
void CleanupUpdate(const std::string& change_guid,
const NetworkIdentifier& id);
network_config::mojom::NetworkStatePropertiesPtr FindLocalNetwork(
const NetworkIdentifier& id);
base::Optional<base::DictionaryValue> ConvertToDictionary(
const sync_pb::WifiConfigurationSpecificsData& specifics,
const std::string& guid);
void OnGetNetworkList(
std::vector<network_config::mojom::NetworkStatePropertiesPtr> networks);
void OnError(const std::string& change_guid,
const NetworkIdentifier& id,
const std::string& error_name);
void OnSetPropertiesResult(const std::string& change_guid,
const NetworkIdentifier& id,
bool success,
const std::string& error_message);
void OnConfigureNetworkResult(const std::string& change_guid,
const NetworkIdentifier& id,
const base::Optional<std::string>& guid,
const std::string& error_message);
void OnForgetNetworkResult(const std::string& change_guid,
const NetworkIdentifier& id,
bool success);
std::unique_ptr<PendingNetworkConfigurationTracker> tracker_;
network_config::mojom::CrosNetworkConfig* cros_network_config_;
mojo::Receiver<chromeos::network_config::mojom::CrosNetworkConfigObserver>
cros_network_config_observer_receiver_{this};
std::vector<network_config::mojom::NetworkStatePropertiesPtr> networks_;
base::WeakPtrFactory<SyncedNetworkUpdaterImpl> weak_ptr_factory_{this};
};
} // namespace sync_wifi
} // namespace chromeos
#endif // CHROMEOS_COMPONENTS_SYNC_WIFI_SYNCED_NETWORK_UPDATER_IMPL_H_
......@@ -6,13 +6,9 @@
#include <utility>
#include "ash/public/cpp/network_config_service.h"
#include "base/bind_helpers.h"
#include "base/time/default_clock.h"
#include "chromeos/components/sync_wifi/pending_network_configuration_tracker_impl.h"
#include "chromeos/components/sync_wifi/synced_network_updater_impl.h"
#include "chromeos/components/sync_wifi/wifi_configuration_bridge.h"
#include "chromeos/services/network_config/public/mojom/cros_network_config.mojom.h"
#include "components/sync/base/report_unrecoverable_error.h"
#include "components/sync/model/model_type_store.h"
#include "components/sync/model_impl/client_tag_based_model_type_processor.h"
......@@ -23,15 +19,9 @@ namespace sync_wifi {
WifiConfigurationSyncService::WifiConfigurationSyncService(
version_info::Channel channel,
PrefService* pref_service,
syncer::OnceModelTypeStoreFactory create_store_callback) {
ash::GetNetworkConfigService(
remote_cros_network_config_.BindNewPipeAndPassReceiver());
updater_ = std::make_unique<SyncedNetworkUpdaterImpl>(
std::make_unique<PendingNetworkConfigurationTrackerImpl>(pref_service),
remote_cros_network_config_.get());
bridge_ = std::make_unique<sync_wifi::WifiConfigurationBridge>(
updater_.get(),
/* synced_network_updater= */ nullptr,
std::make_unique<syncer::ClientTagBasedModelTypeProcessor>(
syncer::WIFI_CONFIGURATIONS,
base::BindRepeating(&syncer::ReportUnrecoverableError, channel)),
......
......@@ -9,13 +9,10 @@
#include <string>
#include "base/memory/weak_ptr.h"
#include "chromeos/services/network_config/public/mojom/cros_network_config.mojom.h"
#include "components/keyed_service/core/keyed_service.h"
#include "components/prefs/pref_service.h"
#include "components/sync/model/model_type_store.h"
#include "components/sync/model/model_type_store_service.h"
#include "components/version_info/channel.h"
#include "mojo/public/cpp/bindings/remote.h"
namespace syncer {
class ModelTypeControllerDelegate;
......@@ -25,7 +22,6 @@ namespace chromeos {
namespace sync_wifi {
class SyncedNetworkUpdaterImpl;
class WifiConfigurationBridge;
// A profile keyed service which instantiates and provides access to an instance
......@@ -34,7 +30,6 @@ class WifiConfigurationSyncService : public KeyedService {
public:
WifiConfigurationSyncService(
version_info::Channel channel,
PrefService* pref_service,
syncer::OnceModelTypeStoreFactory create_store_callback);
~WifiConfigurationSyncService() override;
......@@ -42,9 +37,6 @@ class WifiConfigurationSyncService : public KeyedService {
private:
std::unique_ptr<WifiConfigurationBridge> bridge_;
std::unique_ptr<SyncedNetworkUpdaterImpl> updater_;
mojo::Remote<chromeos::network_config::mojom::CrosNetworkConfig>
remote_cros_network_config_;
DISALLOW_COPY_AND_ASSIGN(WifiConfigurationSyncService);
};
......
......@@ -369,20 +369,8 @@ void FakeShillManagerClient::ConfigureService(
return;
}
if (type == shill::kTypeWifi) {
if (type == shill::kTypeWifi)
properties.GetString(shill::kSSIDProperty, &name);
if (name.empty()) {
std::string hex_name;
properties.GetString(shill::kWifiHexSsid, &hex_name);
if (!hex_name.empty()) {
std::vector<uint8_t> bytes;
if (base::HexStringToBytes(hex_name, &bytes)) {
name.assign(reinterpret_cast<const char*>(&bytes[0]), bytes.size());
}
}
}
}
if (name.empty())
properties.GetString(shill::kNameProperty, &name);
if (name.empty())
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment