From 6b97ff6ebc7d8512856681bfbfb2a13b40801f6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Degros?= Date: Mon, 13 Mar 2023 06:00:32 +0000 Subject: [PATCH] PinManager: Add SetOnline method MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit And get the DriveIntegrationService to call it. Bug: b:272631054 Test: chromeos_unittests --gtest_filter=DriveFsPinManagerTest.* Change-Id: I55f57198319ae8fb7976c19e61a10f8660dbc618 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4333398 Reviewed-by: Marcello Salomao Commit-Queue: François Degros Cr-Commit-Position: refs/heads/main@{#1116243} --- .../ash/drive/drive_integration_service.cc | 22 +++-- .../components/drivefs/drivefs_pin_manager.cc | 93 +------------------ .../components/drivefs/drivefs_pin_manager.h | 35 +------ 3 files changed, 22 insertions(+), 128 deletions(-) diff --git a/chrome/browser/ash/drive/drive_integration_service.cc b/chrome/browser/ash/drive/drive_integration_service.cc index 677b443bb3c6c..b958eafd87e46 100644 --- a/chrome/browser/ash/drive/drive_integration_service.cc +++ b/chrome/browser/ash/drive/drive_integration_service.cc @@ -494,16 +494,23 @@ class DriveIntegrationService::PreferenceWatcher } // NetworkConnectionTracker::NetworkConnectionObserver - void OnConnectionChanged(ConnectionType type) override { + void OnConnectionChanged(const ConnectionType type) override { DCHECK(integration_service_); - if (DriveFs* const drivefs = integration_service_->GetDriveFsInterface()) { - const bool pause_syncing = - NetworkConnectionTracker::IsConnectionCellular(type) && - pref_service_->GetBoolean(prefs::kDisableDriveOverCellular); - const bool is_offline = type == ConnectionType::CONNECTION_NONE; + const bool online = type != ConnectionType::CONNECTION_NONE; + const bool pause_syncing = + NetworkConnectionTracker::IsConnectionCellular(type) && + pref_service_->GetBoolean(prefs::kDisableDriveOverCellular); + + VLOG(1) << "OnConnectionChanged {type: " << type << ", online: " << online + << ", pause_syncing: " << pause_syncing << "}"; + + if (DriveFs* const drivefs = integration_service_->GetDriveFsInterface()) { + drivefs->UpdateNetworkState(pause_syncing, !online); + } - drivefs->UpdateNetworkState(pause_syncing, is_offline); + if (PinManager* const pin_manager = integration_service_->GetPinManager()) { + pin_manager->SetOnline(online); } } @@ -1033,6 +1040,7 @@ bool DriveIntegrationService::AddDriveMountPointAfterMounted() { if (preference_watcher_) { preference_watcher_->UpdateSyncPauseState(); } + if (!GetPrefs()->GetBoolean(prefs::kDriveFsPinnedMigrated)) { MigratePinnedFiles(); } diff --git a/chromeos/ash/components/drivefs/drivefs_pin_manager.cc b/chromeos/ash/components/drivefs/drivefs_pin_manager.cc index 142949db298b3..0070c9a72afd1 100644 --- a/chromeos/ash/components/drivefs/drivefs_pin_manager.cc +++ b/chromeos/ash/components/drivefs/drivefs_pin_manager.cc @@ -17,8 +17,6 @@ #include "base/task/sequenced_task_runner.h" #include "chromeos/ash/components/dbus/spaced/spaced_client.h" #include "chromeos/ash/components/drivefs/mojom/drivefs.mojom.h" -#include "chromeos/ash/components/network/network_handler.h" -#include "chromeos/ash/components/network/network_state.h" #include "components/drive/file_errors.h" #include "third_party/cros_system_api/constants/cryptohome.h" @@ -251,18 +249,6 @@ ostream& operator<<(ostream& out, Quoter q) { << " " << Quote(e.path) << "}"; } -ostream& operator<<(ostream& out, Quoter q) { - const auto& ip = q.value.GetIpAddress(); - return out << "{type: " << q.value.type() - << ", device: " << q.value.device_path() - << ", guid: " << q.value.guid() - << ", ip: " << (ip.empty() ? "(none)" : ip) - << ", connection_state: " << q.value.connection_state() - << ", portal_state: " << q.value.GetPortalState() - << ", connected: " << q.value.IsConnectedState() - << ", online: " << q.value.IsOnline() << "}"; -} - // Rounds the given size to the next multiple of 4-KB. int64_t RoundToBlockSize(int64_t size) { const int64_t block_size = 4 << 10; // 4 KB @@ -580,17 +566,12 @@ PinManager::PinManager(Path profile_path, mojom::DriveFs* const drivefs) drivefs_(drivefs), space_getter_(base::BindRepeating(&GetFreeSpace)) { DCHECK(drivefs_); - RegisterNetworkObserver(); } PinManager::~PinManager() { DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK(!InProgress(progress_.stage)) << "Pin manager is " << progress_.stage; - if (network_state_handler_) { - network_state_handler_->RemoveObserver(this, FROM_HERE); - } - for (Observer& observer : observers_) { observer.OnDrop(); } @@ -1183,43 +1164,9 @@ void PinManager::OnMetadataForModifiedFile( } } -void PinManager::RegisterNetworkObserver() { - using ash::NetworkHandler; - if (!NetworkHandler::IsInitialized()) { - // Probably in test environment. - LOG(WARNING) << "No network handler"; - return; - } - - NetworkHandler* const network_handler = NetworkHandler::Get(); - DCHECK(network_handler); - - DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); - DCHECK(!network_state_handler_); - network_state_handler_ = network_handler->network_state_handler(); - DCHECK(network_state_handler_); - network_state_handler_->AddObserver(this, FROM_HERE); - - DefaultNetworkChanged(network_state_handler_->DefaultNetwork()); -} - -void PinManager::OnShuttingDown() { +void PinManager::SetOnline(const bool online) { DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); - DCHECK(network_state_handler_); - network_state_handler_->RemoveObserver(this, FROM_HERE); - network_state_handler_ = nullptr; -} - -void PinManager::DefaultNetworkChanged(const NetworkState* const network) { - DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); - - if (network) { - VLOG(1) << "Default network changed to " << Quote(*network); - is_online_ = network->IsOnline(); - } else { - VLOG(1) << "Default network changed to no network"; - is_online_ = false; - } + is_online_ = online; if (!is_online_ && InProgress(progress_.stage)) { VLOG(1) << "Going offline..."; @@ -1232,40 +1179,4 @@ void PinManager::DefaultNetworkChanged(const NetworkState* const network) { } } -void PinManager::PortalStateChanged(const NetworkState* const network, - const PortalState portal_state) { - DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); - - if (network) { - DCHECK_EQ(portal_state, network->GetPortalState()); - VLOG(2) << "Network portal state changed to " << Quote(*network); - } else { - DCHECK_EQ(portal_state, PortalState::kUnknown); - VLOG(2) << "Network portal state changed to no network"; - } -} - -void PinManager::ActiveNetworksChanged( - const std::vector& networks) { - DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); - switch (networks.size()) { - case 0: - VLOG(2) << "There are no active networks"; - break; - - case 1: - VLOG(2) << "There is 1 active network"; - break; - - default: - VLOG(2) << "There are " << networks.size() << " active networks"; - } - - int i = 0; - for (const NetworkState* const network : networks) { - DCHECK(network); - VLOG(2) << "Network #" << i++ << ": " << Quote(*network); - } -} - } // namespace drivefs::pinning diff --git a/chromeos/ash/components/drivefs/drivefs_pin_manager.h b/chromeos/ash/components/drivefs/drivefs_pin_manager.h index daa6b3e63b9a2..0b85d463178c1 100644 --- a/chromeos/ash/components/drivefs/drivefs_pin_manager.h +++ b/chromeos/ash/components/drivefs/drivefs_pin_manager.h @@ -25,8 +25,6 @@ #include "base/timer/elapsed_timer.h" #include "chromeos/ash/components/drivefs/drivefs_host_observer.h" #include "chromeos/ash/components/drivefs/mojom/drivefs.mojom.h" -#include "chromeos/ash/components/network/network_state_handler.h" -#include "chromeos/ash/components/network/network_state_handler_observer.h" #include "components/drive/file_errors.h" #include "mojo/public/cpp/bindings/remote.h" #include "third_party/abseil-cpp/absl/types/optional.h" @@ -135,8 +133,7 @@ struct COMPONENT_EXPORT(CHROMEOS_ASH_COMPONENTS_DRIVEFS) Progress { // - Rebuild the progress of bulk pinned items (if turned off mid way through a // bulk pinning event). class COMPONENT_EXPORT(CHROMEOS_ASH_COMPONENTS_DRIVEFS) PinManager - : public DriveFsHostObserver, - ash::NetworkStateHandlerObserver { + : public DriveFsHostObserver { public: using Path = base::FilePath; @@ -238,6 +235,10 @@ class COMPONENT_EXPORT(CHROMEOS_ASH_COMPONENTS_DRIVEFS) PinManager should_check_stalled_files_ = b; } + // Sets the online or offline network status, and starts or pauses the Pin + // manager accordingly. + void SetOnline(bool online); + private: // Progress of a file being synced or to be synced. struct File { @@ -359,38 +360,12 @@ class COMPONENT_EXPORT(CHROMEOS_ASH_COMPONENTS_DRIVEFS) PinManager [](const Files::value_type& entry) { return entry.second.pinned; }); } - // Types related to network state. - using NetworkStateHandler = ash::NetworkStateHandler; - using NetworkState = ash::NetworkState; - using PortalState = NetworkState::PortalState; - - // If possible, registers this PinManager as a network observer. - void RegisterNetworkObserver(); - - // Called just before NetworkStateHandler is destroyed so that this observer - // can safely stop observing. - void OnShuttingDown() override; - - // Called by the NetworkStateHandler to signal that the network conditions - // have changed. - void DefaultNetworkChanged(const NetworkState* network) override; - - void PortalStateChanged(const NetworkState* default_network, - PortalState portal_state) override; - - void ActiveNetworksChanged( - const std::vector& active_networks) override; - SEQUENCE_CHECKER(sequence_checker_); const Path profile_path_ GUARDED_BY_CONTEXT(sequence_checker_); const raw_ptr drivefs_ GUARDED_BY_CONTEXT(sequence_checker_); - // Source of NetworkState events. - raw_ptr network_state_handler_ - GUARDED_BY_CONTEXT(sequence_checker_) = nullptr; - // Is the device connected to a suitable network? Assume it is online for // tests. bool is_online_ GUARDED_BY_CONTEXT(sequence_checker_) = true;