Commit dd9b16e2 authored by Sam McNally's avatar Sam McNally Committed by Commit Bot

Pause DriveFS syncing when on cellular and the option is enabled.

Drive syncing can be paused when on a cellular connection. Monitor the
pref backing this option and the network connectivity type, and use it
to toggle DriveFS syncing.

Bug: 854155
Change-Id: I0e6602b4055f5bf8926728933b84c78fa2fa19d2
Reviewed-on: https://chromium-review.googlesource.com/1147801Reviewed-by: default avatarStuart Langley <slangley@chromium.org>
Commit-Queue: Sam McNally <sammc@chromium.org>
Cr-Commit-Position: refs/heads/master@{#577425}
parent bc82708f
......@@ -57,6 +57,7 @@
#include "content/public/common/user_agent.h"
#include "google_apis/drive/auth_service.h"
#include "mojo/public/cpp/bindings/interface_request.h"
#include "net/base/network_change_notifier.h"
#include "net/traffic_annotation/network_traffic_annotation.h"
#include "services/device/public/mojom/constants.mojom.h"
#include "services/device/public/mojom/wake_lock_provider.mojom.h"
......@@ -209,7 +210,8 @@ FileError InitializeMetadata(
} // namespace
// Observes drive disable Preference's change.
class DriveIntegrationService::PreferenceWatcher {
class DriveIntegrationService::PreferenceWatcher
: public net::NetworkChangeNotifier::NetworkChangeObserver {
public:
explicit PreferenceWatcher(PrefService* pref_service)
: pref_service_(pref_service),
......@@ -219,12 +221,29 @@ class DriveIntegrationService::PreferenceWatcher {
pref_change_registrar_.Init(pref_service);
pref_change_registrar_.Add(
prefs::kDisableDrive,
base::Bind(&PreferenceWatcher::OnPreferenceChanged,
weak_ptr_factory_.GetWeakPtr()));
base::BindRepeating(&PreferenceWatcher::OnPreferenceChanged,
weak_ptr_factory_.GetWeakPtr()));
pref_change_registrar_.Add(
prefs::kDisableDriveOverCellular,
base::BindRepeating(&PreferenceWatcher::UpdateSyncPauseState,
weak_ptr_factory_.GetWeakPtr()));
}
~PreferenceWatcher() override {
if (integration_service_ && integration_service_->drivefs_holder_) {
net::NetworkChangeNotifier::RemoveNetworkChangeObserver(this);
}
}
void set_integration_service(DriveIntegrationService* integration_service) {
integration_service_ = integration_service;
if (integration_service_->drivefs_holder_) {
net::NetworkChangeNotifier::AddNetworkChangeObserver(this);
}
}
void UpdateSyncPauseState() {
OnNetworkChanged(net::NetworkChangeNotifier::GetConnectionType());
}
private:
......@@ -234,6 +253,17 @@ class DriveIntegrationService::PreferenceWatcher {
!pref_service_->GetBoolean(prefs::kDisableDrive));
}
// net::NetworkChangeNotifier::NetworkChangeObserver
void OnNetworkChanged(
net::NetworkChangeNotifier::ConnectionType type) override {
if (!integration_service_->GetDriveFsInterface())
return;
integration_service_->GetDriveFsInterface()->SetPauseSyncing(
net::NetworkChangeNotifier::IsConnectionCellular(type) &&
pref_service_->GetBoolean(prefs::kDisableDriveOverCellular));
}
PrefService* pref_service_;
PrefChangeRegistrar pref_change_registrar_;
DriveIntegrationService* integration_service_;
......@@ -626,6 +656,10 @@ void DriveIntegrationService::AddDriveMountPointAfterMounted() {
for (auto& observer : observers_)
observer.OnFileSystemMounted();
}
if (drivefs_holder_ && preference_watcher_) {
preference_watcher_->UpdateSyncPauseState();
}
}
void DriveIntegrationService::RemoveDriveMountPoint() {
......
......@@ -183,4 +183,6 @@ void FakeDriveFs::SetPinned(const base::FilePath& path,
std::move(callback).Run(drive::FILE_ERROR_OK);
}
void FakeDriveFs::SetPauseSyncing(bool pause) {}
} // namespace drivefs
......@@ -55,6 +55,8 @@ class FakeDriveFs : public drivefs::mojom::DriveFs,
bool pinned,
SetPinnedCallback callback) override;
void SetPauseSyncing(bool pause) override;
const base::FilePath mount_path_;
std::map<base::FilePath, FileMetadata> metadata_;
......
......@@ -29,6 +29,9 @@ interface DriveFs {
// Sets the file at |path| to pinned or unpinned depending on the value of
// |pinned|.
SetPinned(mojo_base.mojom.FilePath path, bool pinned) => (FileError error);
// Set whether syncing should be paused.
SetPauseSyncing(bool pause);
};
// Implemented by Chrome, used from DriveFS.
......
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