Skip to content

Commit

Permalink
[DriveFsBulkPinning] Get total pinned size from DriveFS
Browse files Browse the repository at this point in the history
Get the total size of offline files from DriveFS and lift
it to the settings > storage page.

Test: unit_tests *StorageHandlerBulkPinningTest.OfflineSize*
Bug: b/266631443
Change-Id: I1ea9bc401a3ac5b776997b13d138197a3f6b680c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4193931
Auto-Submit: Marcello Salomao <msalomao@google.com>
Reviewed-by: Xiaohui Chen <xiaohuic@chromium.org>
Commit-Queue: Xiaohui Chen <xiaohuic@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1101069}
  • Loading branch information
Marcello Salomao authored and Chromium LUCI CQ committed Feb 3, 2023
1 parent 2b49b95 commit a62d444
Show file tree
Hide file tree
Showing 13 changed files with 252 additions and 17 deletions.
51 changes: 51 additions & 0 deletions chrome/browser/ash/drive/drive_integration_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "chrome/browser/ash/drive/drive_integration_service.h"

#include <cstdint>
#include <memory>
#include <set>
#include <string>
Expand All @@ -16,6 +17,7 @@
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/functional/callback_forward.h"
#include "base/functional/callback_helpers.h"
#include "base/hash/md5.h"
#include "base/logging.h"
Expand Down Expand Up @@ -1196,6 +1198,55 @@ void DriveIntegrationService::ToggleBulkPinning() {
pin_manager_->Enable(enabled);
}

void DriveIntegrationService::GetTotalPinnedSize(
base::OnceCallback<void(int64_t)> callback) {
if (!ash::features::IsDriveFsBulkPinningEnabled() || !IsMounted() ||
!GetDriveFsInterface()) {
std::move(callback).Run(-1);
return;
}

auto query_params = drivefs::mojom::QueryParameters::New();
query_params->query_source =
drivefs::mojom::QueryParameters::QuerySource::kLocalOnly;
query_params->available_offline = true;

int64_t total_size = 0;
mojo::Remote<drivefs::mojom::SearchQuery> search_query;

GetDriveFsInterface()->StartSearchQuery(
search_query.BindNewPipeAndPassReceiver(), std::move(query_params));

auto* raw_search_query = search_query.get();
raw_search_query->GetNextPage(
base::BindOnce(&DriveIntegrationService::OnGetOfflineItemsPage,
weak_ptr_factory_.GetWeakPtr(), total_size,
std::move(search_query), std::move(callback)));
}

void DriveIntegrationService::OnGetOfflineItemsPage(
int64_t total_size,
mojo::Remote<drivefs::mojom::SearchQuery> search_query,
base::OnceCallback<void(int64_t)> callback,
drive::FileError error,
absl::optional<std::vector<drivefs::mojom::QueryItemPtr>> results) {
if (!ash::features::IsDriveFsBulkPinningEnabled() ||
error != drive::FILE_ERROR_OK || results->empty()) {
std::move(callback).Run(total_size);
return;
}

for (auto& result : *results) {
total_size += result->metadata->size;
}

auto* raw_search_query = search_query.get();
raw_search_query->GetNextPage(
base::BindOnce(&DriveIntegrationService::OnGetOfflineItemsPage,
weak_ptr_factory_.GetWeakPtr(), total_size,
std::move(search_query), std::move(callback)));
}

void DriveIntegrationService::GetQuickAccessItems(
int max_number,
GetQuickAccessItemsCallback callback) {
Expand Down
11 changes: 11 additions & 0 deletions chrome/browser/ash/drive/drive_integration_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,10 @@ class DriveIntegrationService : public KeyedService,
void GetReadOnlyAuthenticationToken(
GetReadOnlyAuthenticationTokenCallback callback);

// Returns via callback the amount of storage taken by all currently pinned
// files.
void GetTotalPinnedSize(base::OnceCallback<void(int64_t)> callback);

private:
enum State {
NOT_INITIALIZED,
Expand Down Expand Up @@ -373,6 +377,13 @@ class DriveIntegrationService : public KeyedService,
// Enable or disable DriveFS bulk pinning.
void ToggleBulkPinning();

void OnGetOfflineItemsPage(
int64_t total_size,
mojo::Remote<drivefs::mojom::SearchQuery> search_query,
base::OnceCallback<void(int64_t)> callback,
drive::FileError error,
absl::optional<std::vector<drivefs::mojom::QueryItemPtr>> results);

void OnGetQuickAccessItems(
GetQuickAccessItemsCallback callback,
drive::FileError error,
Expand Down
71 changes: 71 additions & 0 deletions chrome/browser/ash/drive/drive_integration_service_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <memory>

#include "ash/constants/ash_features.h"
#include "ash/constants/ash_switches.h"
#include "base/base_switches.h"
Expand All @@ -13,10 +15,12 @@
#include "base/test/gmock_callback_support.h"
#include "base/test/mock_callback.h"
#include "base/threading/thread_restrictions.h"
#include "chrome/browser/ash/drive/drive_integration_service.h"
#include "chrome/browser/ash/drive/drive_integration_service_browser_test_base.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chromeos/ash/components/drivefs/fake_drivefs.h"
#include "chromeos/ash/components/drivefs/mojom/drivefs.mojom.h"
#include "components/drive/drive_pref_names.h"
#include "components/prefs/pref_service.h"
#include "components/prefs/pref_test_utils.h"
Expand Down Expand Up @@ -365,6 +369,18 @@ class DriveIntegrationBrowserTestWithMirrorSyncEnabled
base::test::ScopedFeatureList scoped_feature_list_;
};

class DriveIntegrationBrowserTestWithBulkPinningEnabled
: public DriveIntegrationServiceBrowserTest {
public:
DriveIntegrationBrowserTestWithBulkPinningEnabled() {
scoped_feature_list_.InitWithFeatures({ash::features::kDriveFsBulkPinning},
{});
}

private:
base::test::ScopedFeatureList scoped_feature_list_;
};

IN_PROC_BROWSER_TEST_F(DriveIntegrationBrowserTestWithMirrorSyncEnabled,
EnableMirrorSync) {
auto* drive_service =
Expand Down Expand Up @@ -558,4 +574,59 @@ IN_PROC_BROWSER_TEST_F(DriveIntegrationBrowserTestWithMirrorSyncEnabled,
run_loop.Run();
}

class FakeSearchQueryImpl : public drivefs::mojom::SearchQuery {
public:
// Produces 2 pages of fake results, each page with 2 items with a combined
// size of 75 bytes (50 + 25). From the 3rd call onwards, returns an empty
// page.
void GetNextPage(GetNextPageCallback reply) override {
std::vector<drivefs::mojom::QueryItemPtr> page;

if (pages_counter_ < 2) {
pages_counter_++;
auto qi1 = drivefs::mojom::QueryItem::New();
auto qi2 = drivefs::mojom::QueryItem::New();
qi1->metadata = drivefs::mojom::FileMetadata::New();
qi2->metadata = drivefs::mojom::FileMetadata::New();
qi1->metadata->capabilities = drivefs::mojom::Capabilities::New();
qi2->metadata->capabilities = drivefs::mojom::Capabilities::New();
qi1->metadata->size = 50;
qi2->metadata->size = 25;
page.emplace_back(std::move(qi1));
page.emplace_back(std::move(qi2));
}

std::move(reply).Run(drive::FILE_ERROR_OK, std::move(page));
}

private:
int pages_counter_ = 0;
};

IN_PROC_BROWSER_TEST_F(DriveIntegrationBrowserTestWithBulkPinningEnabled,
GetTotalPinnedSize) {
auto* drive_integration_service =
DriveIntegrationServiceFactory::FindForProfile(browser()->profile());
auto* fake_drivefs = GetFakeDriveFsForProfile(browser()->profile());

FakeSearchQueryImpl fake_search_query_impl;
mojo::Receiver<drivefs::mojom::SearchQuery> receiver(&fake_search_query_impl);

EXPECT_CALL(*fake_drivefs, StartSearchQuery(_, _))
.WillOnce([&](mojo::PendingReceiver<drivefs::mojom::SearchQuery>
pending_receiver,
drivefs::mojom::QueryParametersPtr query_params) {
receiver.Bind(std::move(pending_receiver));
});

base::RunLoop run_loop;
base::MockOnceCallback<void(int64_t)> mock_callback;
EXPECT_CALL(mock_callback, Run(/* 2 * (25 + 50) = */ 150))
.WillOnce(RunClosure(run_loop.QuitClosure()));

drive_integration_service->GetTotalPinnedSize(mock_callback.Get());

run_loop.Run();
}

} // namespace drive
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,8 @@
label="$i18n{storageItemApps}"
sub-label="$i18n{storageSizeComputing}" external>
</cr-link-row>
<template is="dom-if" if="[[showOfflineStorage_]]">
<cr-link-row id="offlineSize" class="hr" on-click="onOfflineTap_"
<template is="dom-if" if="[[showDriveOfflineStorage_]]">
<cr-link-row id="driveOfflineSize" class="hr" on-click="onDriveOfflineTap_"
label="$i18n{storageItemOffline}"
sub-label="$i18n{storageSizeComputing}" external>
</cr-link-row>
Expand Down
18 changes: 15 additions & 3 deletions chrome/browser/resources/settings/chromeos/device_page/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class SettingsStorageElement extends SettingsStorageElementBase {
value: false,
},

showOfflineStorage_: {
showDriveOfflineStorage_: {
type: Boolean,
value: loadTimeData.getBoolean('enableDriveFsBulkPinning'),
readonly: true,
Expand Down Expand Up @@ -96,7 +96,7 @@ class SettingsStorageElement extends SettingsStorageElementBase {
private isGuest_: boolean;
private route_: Route;
private showCrostiniStorage_: boolean;
private showOfflineStorage_: boolean;
private showDriveOfflineStorage_: boolean;
private showOtherUsers_: boolean;
private sizeStat_: StorageSizeStat;
private updateTimerId_: number;
Expand Down Expand Up @@ -130,6 +130,9 @@ class SettingsStorageElement extends SettingsStorageElementBase {
this.addWebUiListener(
'storage-apps-size-changed',
(size: string) => this.handleAppsSizeChanged_(size));
this.addWebUiListener(
'storage-drive-offline-size-changed',
(size: string) => this.handleDriveOfflineSizeChanged_(size));
this.addWebUiListener(
'storage-crostini-size-changed',
(size: string) => this.handleCrostiniSizeChanged_(size));
Expand Down Expand Up @@ -198,7 +201,7 @@ class SettingsStorageElement extends SettingsStorageElementBase {
/**
* Handler for tapping the "Offline files" item.
*/
private onOfflineTap_(): void {
private onDriveOfflineTap_(): void {
// TODO(b/266631636): Offline files row should redirect users to Files
// settings > Drive settings.
}
Expand Down Expand Up @@ -258,6 +261,15 @@ class SettingsStorageElement extends SettingsStorageElementBase {
size;
}

/**
* @param size Formatted string representing the size of pinned files in
* Google Drive.
*/
private handleDriveOfflineSizeChanged_(size: string): void {
this.shadowRoot!.querySelector<CrLinkRowElement>(
'#driveOfflineSize')!.subLabel = size;
}

/**
* @param size Formatted string representing the size of Crostini storage.
*/
Expand Down
29 changes: 29 additions & 0 deletions chrome/browser/ui/webui/settings/ash/calculator/size_calculator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@

#include "chrome/browser/ui/webui/settings/ash/calculator/size_calculator.h"

#include <cstdint>
#include <numeric>

#include "ash/components/arc/session/arc_bridge_service.h"
#include "ash/components/arc/session/arc_service_manager.h"
#include "ash/components/arc/storage_manager/arc_storage_manager.h"
#include "ash/constants/ash_features.h"
#include "base/containers/contains.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/scoped_refptr.h"
Expand All @@ -19,6 +21,7 @@
#include "chrome/browser/ash/borealis/borealis_service.h"
#include "chrome/browser/ash/crostini/crostini_features.h"
#include "chrome/browser/ash/crostini/crostini_pref_names.h"
#include "chrome/browser/ash/drive/drive_integration_service.h"
#include "chrome/browser/ash/file_manager/path_util.h"
#include "chrome/browser/ash/profiles/profile_helper.h"
#include "chrome/browser/browsing_data/browsing_data_file_system_util.h"
Expand Down Expand Up @@ -178,6 +181,32 @@ void FreeDiskSpaceCalculator::OnGetFreeDiskSpace(int64_t* available_bytes) {
NotifySizeCalculated(*available_bytes);
}

DriveOfflineSizeCalculator::DriveOfflineSizeCalculator(Profile* profile)
: SizeCalculator(CalculationType::kDriveOfflineFiles), profile_(profile) {}

DriveOfflineSizeCalculator::~DriveOfflineSizeCalculator() = default;

void DriveOfflineSizeCalculator::PerformCalculation() {
if (!base::FeatureList::IsEnabled(ash::features::kDriveFsBulkPinning)) {
return;
}

drive::DriveIntegrationService* integration_service =
drive::DriveIntegrationServiceFactory::FindForProfile(profile_);

if (!integration_service) {
return;
}

integration_service->GetTotalPinnedSize(
base::BindOnce(&DriveOfflineSizeCalculator::OnGetOfflineItemsSize,
weak_ptr_factory_.GetWeakPtr()));
}

void DriveOfflineSizeCalculator::OnGetOfflineItemsSize(int64_t offline_bytes) {
NotifySizeCalculated(offline_bytes);
}

MyFilesSizeCalculator::MyFilesSizeCalculator(Profile* profile)
: SizeCalculator(CalculationType::kMyFiles), profile_(profile) {}

Expand Down
22 changes: 22 additions & 0 deletions chrome/browser/ui/webui/settings/ash/calculator/size_calculator.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class SizeCalculator {
kMyFiles,
kBrowsingData,
kAppsExtensions,
kDriveOfflineFiles,
kCrostini,
kOtherUsers,
kLastCalculationItem = kOtherUsers,
Expand Down Expand Up @@ -133,6 +134,27 @@ class FreeDiskSpaceCalculator : public SizeCalculator {
base::WeakPtrFactory<FreeDiskSpaceCalculator> weak_ptr_factory_{this};
};

class DriveOfflineSizeCalculator : public SizeCalculator {
public:
explicit DriveOfflineSizeCalculator(Profile* profile);

DriveOfflineSizeCalculator(const DriveOfflineSizeCalculator&) = delete;
DriveOfflineSizeCalculator& operator=(const DriveOfflineSizeCalculator&) =
delete;

~DriveOfflineSizeCalculator() override;

private:
friend class DriveOfflineSizeTestAPI;

void PerformCalculation() override;

void OnGetOfflineItemsSize(int64_t offline_bytes);

Profile* profile_;
base::WeakPtrFactory<DriveOfflineSizeCalculator> weak_ptr_factory_{this};
};

// Class handling the calculation of the size of the user's personal files: My
// files + Android Play files.
class MyFilesSizeCalculator : public SizeCalculator {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,23 @@ class FreeDiskSpaceTestAPI {
FreeDiskSpaceCalculator free_disk_space_calculator_;
};

class DriveOfflineSizeTestAPI {
public:
DriveOfflineSizeTestAPI(StorageHandler* handler, Profile* profile)
: drive_offline_size_calculator_(profile) {
drive_offline_size_calculator_.AddObserver(handler);
}

void StartCalculation() { drive_offline_size_calculator_.StartCalculation(); }

void SimulateOnGetOfflineItemsSize(int64_t offline_bytes) {
drive_offline_size_calculator_.OnGetOfflineItemsSize(offline_bytes);
}

private:
DriveOfflineSizeCalculator drive_offline_size_calculator_;
};

class MyFilesSizeTestAPI {
public:
MyFilesSizeTestAPI(StorageHandler* handler, Profile* profile)
Expand Down

0 comments on commit a62d444

Please sign in to comment.