Skip to content

Commit

Permalink
PinManager: Add SetOnline method
Browse files Browse the repository at this point in the history
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 <msalomao@google.com>
Commit-Queue: François Degros <fdegros@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1116243}
  • Loading branch information
fdegros authored and Chromium LUCI CQ committed Mar 13, 2023
1 parent 77912f8 commit 6b97ff6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 128 deletions.
22 changes: 15 additions & 7 deletions chrome/browser/ash/drive/drive_integration_service.cc
Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -1033,6 +1040,7 @@ bool DriveIntegrationService::AddDriveMountPointAfterMounted() {
if (preference_watcher_) {
preference_watcher_->UpdateSyncPauseState();
}

if (!GetPrefs()->GetBoolean(prefs::kDriveFsPinnedMigrated)) {
MigratePinnedFiles();
}
Expand Down
93 changes: 2 additions & 91 deletions chromeos/ash/components/drivefs/drivefs_pin_manager.cc
Expand Up @@ -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"

Expand Down Expand Up @@ -251,18 +249,6 @@ ostream& operator<<(ostream& out, Quoter<mojom::DriveError> q) {
<< " " << Quote(e.path) << "}";
}

ostream& operator<<(ostream& out, Quoter<ash::NetworkState> 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
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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...";
Expand All @@ -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<const NetworkState*>& 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
35 changes: 5 additions & 30 deletions chromeos/ash/components/drivefs/drivefs_pin_manager.h
Expand Up @@ -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"
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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<const NetworkState*>& active_networks) override;

SEQUENCE_CHECKER(sequence_checker_);

const Path profile_path_ GUARDED_BY_CONTEXT(sequence_checker_);
const raw_ptr<mojom::DriveFs, DanglingUntriaged> drivefs_
GUARDED_BY_CONTEXT(sequence_checker_);

// Source of NetworkState events.
raw_ptr<NetworkStateHandler> 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;
Expand Down

0 comments on commit 6b97ff6

Please sign in to comment.