Commit 3014d931 authored by Jon Mann's avatar Jon Mann Committed by Commit Bot

Inject NetworkMetadataStore in wifi sync classes which depend on it.

The instance of NetworkMetadataStore for a user profile is created
after the sync service is already initialized.  This change does
the necessary plumbing to get the metadata store instance to
the wifi sync classes.

Bug: 966270
Change-Id: Icd235cd780df97a367f09f281f3e957893baa93b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2110079
Commit-Queue: Jon Mann <jonmann@chromium.org>
Reviewed-by: default avatarMarc Treib <treib@chromium.org>
Reviewed-by: default avatarJames Vecore <vecore@google.com>
Reviewed-by: default avatarSteven Bennetts <stevenjb@chromium.org>
Cr-Commit-Position: refs/heads/master@{#752273}
parent 8cc4152a
......@@ -111,6 +111,7 @@ source_set("chromeos") {
"//chromeos/components/quick_answers/public/cpp:prefs",
"//chromeos/components/smbfs",
"//chromeos/components/smbfs/mojom",
"//chromeos/components/sync_wifi",
"//chromeos/components/tether",
"//chromeos/constants",
"//chromeos/cryptohome",
......
......@@ -8,6 +8,8 @@
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/chromeos/profiles/profile_helper.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/sync/wifi_configuration_sync_service_factory.h"
#include "chromeos/components/sync_wifi/wifi_configuration_sync_service.h"
#include "chromeos/network/network_handler.h"
#include "content/public/browser/notification_service.h"
......@@ -40,6 +42,14 @@ void NetworkPrefStateObserver::Observe(
if (ProfileHelper::IsPrimaryProfile(profile)) {
InitializeNetworkPrefServices(profile);
notification_registrar_.RemoveAll();
auto* wifi_sync_service =
WifiConfigurationSyncServiceFactory::GetForProfile(profile,
/*create=*/false);
if (wifi_sync_service) {
wifi_sync_service->SetNetworkMetadataStore(
NetworkHandler::Get()->network_metadata_store());
}
}
}
......
......@@ -613,7 +613,8 @@ ChromeSyncClient::GetControllerDelegateForModelType(syncer::ModelType type) {
->change_processor()
->GetControllerDelegate();
case syncer::WIFI_CONFIGURATIONS:
return WifiConfigurationSyncServiceFactory::GetForProfile(profile_)
return WifiConfigurationSyncServiceFactory::GetForProfile(profile_,
/*create=*/true)
->GetControllerDelegate();
#endif // defined(OS_CHROMEOS)
case syncer::SHARING_MESSAGE:
......
......@@ -16,9 +16,10 @@
// static
chromeos::sync_wifi::WifiConfigurationSyncService*
WifiConfigurationSyncServiceFactory::GetForProfile(Profile* profile) {
WifiConfigurationSyncServiceFactory::GetForProfile(Profile* profile,
bool create) {
return static_cast<chromeos::sync_wifi::WifiConfigurationSyncService*>(
GetInstance()->GetServiceForBrowserContext(profile, true));
GetInstance()->GetServiceForBrowserContext(profile, create));
}
// static
......
......@@ -27,7 +27,8 @@ class WifiConfigurationSyncServiceFactory
: public BrowserContextKeyedServiceFactory {
public:
static chromeos::sync_wifi::WifiConfigurationSyncService* GetForProfile(
Profile* profile);
Profile* profile,
bool create);
static WifiConfigurationSyncServiceFactory* GetInstance();
private:
......
......@@ -40,6 +40,9 @@ void FakeLocalNetworkCollector::ClearNetworks() {
networks_.clear();
}
void FakeLocalNetworkCollector::SetNetworkMetadataStore(
NetworkMetadataStore* network_metadata_store) {}
} // namespace sync_wifi
} // namespace chromeos
\ No newline at end of file
} // namespace chromeos
......@@ -29,6 +29,8 @@ class FakeLocalNetworkCollector : public LocalNetworkCollector {
void AddNetwork(sync_pb::WifiConfigurationSpecifics proto);
void ClearNetworks();
void SetNetworkMetadataStore(
NetworkMetadataStore* network_metadata_store) override;
private:
std::vector<sync_pb::WifiConfigurationSpecifics> networks_;
......
......@@ -19,6 +19,8 @@ class WifiConfigurationSpecifics;
namespace chromeos {
class NetworkMetadataStore;
namespace sync_wifi {
class NetworkIdentifier;
......@@ -48,6 +50,10 @@ class LocalNetworkCollector {
// exist or isn't syncable it will provide base::nullopt to the callback.
virtual void GetSyncableNetwork(const NetworkIdentifier& id,
ProtoCallback callback) = 0;
// Provides the metadata store which gets constructed later.
virtual void SetNetworkMetadataStore(
NetworkMetadataStore* network_metadata_store) = 0;
};
} // namespace sync_wifi
......
......@@ -36,10 +36,8 @@ dbus::ObjectPath GetServicePathForGuid(const std::string& guid) {
} // namespace
LocalNetworkCollectorImpl::LocalNetworkCollectorImpl(
network_config::mojom::CrosNetworkConfig* cros_network_config,
NetworkMetadataStore* network_metadata_store)
: cros_network_config_(cros_network_config),
network_metadata_store_(network_metadata_store) {
network_config::mojom::CrosNetworkConfig* cros_network_config)
: cros_network_config_(cros_network_config) {
cros_network_config_->AddObserver(
cros_network_config_observer_receiver_.BindNewPipeAndPassRemote());
......@@ -97,6 +95,11 @@ void LocalNetworkCollectorImpl::GetSyncableNetwork(const NetworkIdentifier& id,
StartGetNetworkDetails(network, request_guid);
}
void LocalNetworkCollectorImpl::SetNetworkMetadataStore(
NetworkMetadataStore* network_metadata_store) {
network_metadata_store_ = network_metadata_store;
}
std::string LocalNetworkCollectorImpl::InitializeRequest() {
std::string request_guid = base::GenerateGUID();
request_guid_to_complete_protos_[request_guid] =
......
......@@ -38,9 +38,8 @@ class LocalNetworkCollectorImpl
// LocalNetworkCollector:
// |cros_network_config| and |network_metadata_store| must outlive this class.
LocalNetworkCollectorImpl(
network_config::mojom::CrosNetworkConfig* cros_network_config,
NetworkMetadataStore* network_metadata_store);
explicit LocalNetworkCollectorImpl(
network_config::mojom::CrosNetworkConfig* cros_network_config);
~LocalNetworkCollectorImpl() override;
// Can only execute one request at a time.
......@@ -50,6 +49,9 @@ class LocalNetworkCollectorImpl
void GetSyncableNetwork(const NetworkIdentifier& id,
ProtoCallback callback) override;
void SetNetworkMetadataStore(
NetworkMetadataStore* network_metadata_store) override;
// CrosNetworkConfigObserver:
void OnNetworkStateListChanged() override;
void OnActiveNetworksChanged(
......
......@@ -58,7 +58,8 @@ class LocalNetworkCollectorImplTest : public testing::Test {
testing::Test::SetUp();
helper()->SetUp();
local_network_collector_ = std::make_unique<LocalNetworkCollectorImpl>(
remote_cros_network_config_.get(),
remote_cros_network_config_.get());
local_network_collector_->SetNetworkMetadataStore(
NetworkHandler::Get()->network_metadata_store());
}
......
......@@ -15,6 +15,7 @@
#include "base/time/time.h"
#include "chromeos/components/sync_wifi/network_identifier.h"
#include "chromeos/components/sync_wifi/synced_network_updater.h"
#include "chromeos/network/network_metadata_store.h"
#include "components/device_event_log/device_event_log.h"
#include "components/sync/model/entity_change.h"
#include "components/sync/model/metadata_batch.h"
......@@ -45,14 +46,19 @@ WifiConfigurationBridge::WifiConfigurationBridge(
syncer::OnceModelTypeStoreFactory create_store_callback)
: ModelTypeSyncBridge(std::move(change_processor)),
synced_network_updater_(synced_network_updater),
local_network_collector_(local_network_collector) {
local_network_collector_(local_network_collector),
network_metadata_store_(nullptr) {
std::move(create_store_callback)
.Run(syncer::WIFI_CONFIGURATIONS,
base::BindOnce(&WifiConfigurationBridge::OnStoreCreated,
weak_ptr_factory_.GetWeakPtr()));
}
WifiConfigurationBridge::~WifiConfigurationBridge() {}
WifiConfigurationBridge::~WifiConfigurationBridge() {
if (network_metadata_store_) {
network_metadata_store_->RemoveObserver(this);
}
}
std::unique_ptr<syncer::MetadataChangeList>
WifiConfigurationBridge::CreateMetadataChangeList() {
......@@ -294,6 +300,20 @@ std::vector<NetworkIdentifier> WifiConfigurationBridge::GetAllIdsForTesting() {
return ids;
}
void WifiConfigurationBridge::OnFirstConnectionToNetwork(
const std::string& guid) {
// TODO(jonmann): Add network to sync.
}
void WifiConfigurationBridge::SetNetworkMetadataStore(
NetworkMetadataStore* network_metadata_store) {
if (network_metadata_store_) {
network_metadata_store->RemoveObserver(this);
}
network_metadata_store_ = network_metadata_store;
network_metadata_store->AddObserver(this);
}
} // namespace sync_wifi
} // namespace chromeos
......@@ -16,6 +16,7 @@
#include "base/time/time.h"
#include "chromeos/components/sync_wifi/local_network_collector.h"
#include "chromeos/components/sync_wifi/synced_network_updater.h"
#include "chromeos/network/network_metadata_observer.h"
#include "components/sync/base/model_type.h"
#include "components/sync/model/model_type_store.h"
#include "components/sync/model/model_type_sync_bridge.h"
......@@ -26,11 +27,14 @@ class ModelTypeChangeProcessor;
namespace chromeos {
class NetworkMetadataStore;
namespace sync_wifi {
// Receives updates to network configurations from the Chrome sync back end and
// from the system network stack and keeps both lists in sync.
class WifiConfigurationBridge : public syncer::ModelTypeSyncBridge {
class WifiConfigurationBridge : public syncer::ModelTypeSyncBridge,
public NetworkMetadataObserver {
public:
WifiConfigurationBridge(
SyncedNetworkUpdater* synced_network_updater,
......@@ -53,9 +57,14 @@ class WifiConfigurationBridge : public syncer::ModelTypeSyncBridge {
std::string GetClientTag(const syncer::EntityData& entity_data) override;
std::string GetStorageKey(const syncer::EntityData& entity_data) override;
// NetworkMetadataObserver:
void OnFirstConnectionToNetwork(const std::string& guid) override;
// Comes from |entries_| the in-memory map.
std::vector<NetworkIdentifier> GetAllIdsForTesting();
void SetNetworkMetadataStore(NetworkMetadataStore* network_metadata_store);
private:
void Commit(std::unique_ptr<syncer::ModelTypeStore::WriteBatch> batch);
......@@ -86,8 +95,8 @@ class WifiConfigurationBridge : public syncer::ModelTypeSyncBridge {
std::unique_ptr<syncer::ModelTypeStore> store_;
SyncedNetworkUpdater* synced_network_updater_;
LocalNetworkCollector* local_network_collector_;
NetworkMetadataStore* network_metadata_store_;
base::WeakPtrFactory<WifiConfigurationBridge> weak_ptr_factory_{this};
......
......@@ -34,8 +34,7 @@ WifiConfigurationSyncService::WifiConfigurationSyncService(
std::make_unique<PendingNetworkConfigurationTrackerImpl>(pref_service),
remote_cros_network_config_.get(), std::make_unique<TimerFactory>());
collector_ = std::make_unique<LocalNetworkCollectorImpl>(
remote_cros_network_config_.get(),
NetworkHandler::Get()->network_metadata_store());
remote_cros_network_config_.get());
bridge_ = std::make_unique<sync_wifi::WifiConfigurationBridge>(
updater_.get(), collector_.get(),
std::make_unique<syncer::ClientTagBasedModelTypeProcessor>(
......@@ -51,6 +50,12 @@ WifiConfigurationSyncService::GetControllerDelegate() {
return bridge_->change_processor()->GetControllerDelegate();
}
void WifiConfigurationSyncService::SetNetworkMetadataStore(
NetworkMetadataStore* network_metadata_store) {
bridge_->SetNetworkMetadataStore(network_metadata_store);
collector_->SetNetworkMetadataStore(network_metadata_store);
}
} // namespace sync_wifi
} // namespace chromeos
......@@ -23,6 +23,8 @@ class ModelTypeControllerDelegate;
namespace chromeos {
class NetworkMetadataStore;
namespace sync_wifi {
class LocalNetworkCollectorImpl;
......@@ -40,6 +42,7 @@ class WifiConfigurationSyncService : public KeyedService {
~WifiConfigurationSyncService() override;
base::WeakPtr<syncer::ModelTypeControllerDelegate> GetControllerDelegate();
void SetNetworkMetadataStore(NetworkMetadataStore* network_metadata_store);
private:
std::unique_ptr<WifiConfigurationBridge> bridge_;
......
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