Commit d4ea8fa0 authored by Austin Tankiang's avatar Austin Tankiang Committed by Chromium LUCI CQ

Add SearchDriveByFileName() to Drive integration service

Bug: 1126359
Change-Id: Ie422f123dbb15d4e7b9e2ffd5708be36f0eec142
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2577105
Commit-Queue: Austin Tankiang <austinct@chromium.org>
Reviewed-by: default avatarSergei Datsenko <dats@chromium.org>
Cr-Commit-Position: refs/heads/master@{#834420}
parent 20a84c24
......@@ -1082,6 +1082,52 @@ void DriveIntegrationService::OnGetQuickAccessItems(
std::move(callback).Run(error, std::move(result));
}
void DriveIntegrationService::SearchDriveByFileName(
std::string query,
int max_results,
drivefs::mojom::QueryParameters::SortField sort_field,
drivefs::mojom::QueryParameters::SortDirection sort_direction,
SearchDriveByFileNameCallback callback) const {
if (!GetDriveFsHost()) {
std::move(callback).Run(drive::FileError::FILE_ERROR_SERVICE_UNAVAILABLE,
{});
return;
}
auto drive_query = drivefs::mojom::QueryParameters::New();
drive_query->title = query;
drive_query->page_size = max_results;
drive_query->sort_field = sort_field;
drive_query->sort_direction = sort_direction;
auto on_response =
base::BindOnce(&DriveIntegrationService::OnSearchDriveByFileName,
weak_ptr_factory_.GetWeakPtr(), std::move(callback));
GetDriveFsHost()->PerformSearch(
std::move(drive_query),
mojo::WrapCallbackWithDefaultInvokeIfNotRun(
std::move(on_response), drive::FileError::FILE_ERROR_ABORT,
base::Optional<std::vector<drivefs::mojom::QueryItemPtr>>()));
}
void DriveIntegrationService::OnSearchDriveByFileName(
SearchDriveByFileNameCallback callback,
drive::FileError error,
base::Optional<std::vector<drivefs::mojom::QueryItemPtr>> items) {
if (error != drive::FILE_ERROR_OK || !items.has_value()) {
std::move(callback).Run(error, {});
return;
}
std::vector<base::FilePath> result;
result.reserve(items->size());
for (const auto& item : *items) {
result.emplace_back(item->path);
}
std::move(callback).Run(error, std::move(result));
}
void DriveIntegrationService::GetMetadata(
const base::FilePath& local_path,
drivefs::mojom::DriveFs::GetMetadataCallback callback) {
......
......@@ -99,6 +99,8 @@ class DriveIntegrationService : public KeyedService,
std::unique_ptr<drivefs::DriveFsBootstrapListener>()>;
using GetQuickAccessItemsCallback =
base::OnceCallback<void(drive::FileError, std::vector<QuickAccessItem>)>;
using SearchDriveByFileNameCallback =
base::OnceCallback<void(drive::FileError, std::vector<base::FilePath>)>;
// test_mount_point_name, test_cache_root and
// test_drivefs_mojo_listener_factory are used by tests to inject customized
......@@ -162,6 +164,13 @@ class DriveIntegrationService : public KeyedService,
void GetQuickAccessItems(int max_number,
GetQuickAccessItemsCallback callback);
void SearchDriveByFileName(
std::string query,
int max_results,
drivefs::mojom::QueryParameters::SortField sort_field,
drivefs::mojom::QueryParameters::SortDirection sort_direction,
SearchDriveByFileNameCallback callback) const;
// Returns the metadata for Drive file at |local_path|.
void GetMetadata(const base::FilePath& local_path,
drivefs::mojom::DriveFs::GetMetadataCallback callback);
......@@ -280,6 +289,11 @@ class DriveIntegrationService : public KeyedService,
drive::FileError error,
base::Optional<std::vector<drivefs::mojom::QueryItemPtr>> items);
void OnSearchDriveByFileName(
SearchDriveByFileNameCallback callback,
drive::FileError error,
base::Optional<std::vector<drivefs::mojom::QueryItemPtr>> items);
friend class DriveIntegrationServiceFactory;
Profile* profile_;
......
......@@ -119,6 +119,37 @@ IN_PROC_BROWSER_TEST_F(DriveIntegrationServiceBrowserTest,
EXPECT_FALSE(integration_service->is_enabled());
}
IN_PROC_BROWSER_TEST_F(DriveIntegrationServiceBrowserTest,
SearchDriveByFileNameTest) {
base::ScopedAllowBlockingForTesting allow_blocking;
drive::DriveIntegrationService* drive_service =
drive::DriveIntegrationServiceFactory::FindForProfile(
browser()->profile());
base::FilePath mount_path = drive_service->GetMountPointPath();
ASSERT_TRUE(base::WriteFile(mount_path.Append("bar"), ""));
ASSERT_TRUE(base::WriteFile(mount_path.Append("baz"), ""));
auto base_time = base::Time::Now() - base::TimeDelta::FromSeconds(10);
auto earlier_time = base_time - base::TimeDelta::FromSeconds(10);
ASSERT_TRUE(base::TouchFile(mount_path.Append("bar"), base_time, base_time));
ASSERT_TRUE(
base::TouchFile(mount_path.Append("baz"), earlier_time, earlier_time));
base::RunLoop run_loop;
auto quit_closure = run_loop.QuitClosure();
drive_service->SearchDriveByFileName(
"ba", 10, drivefs::mojom::QueryParameters::SortField::kLastViewedByMe,
drivefs::mojom::QueryParameters::SortDirection::kAscending,
base::BindLambdaForTesting(
[=](FileError error, std::vector<base::FilePath> paths) {
EXPECT_EQ(2, paths.size());
EXPECT_EQ("baz", paths[0].BaseName().value());
EXPECT_EQ("bar", paths[1].BaseName().value());
quit_closure.Run();
}));
run_loop.Run();
}
class DriveIntegrationServiceWithGaiaDisabledBrowserTest
: public DriveIntegrationServiceBrowserTest {
void SetUpCommandLine(base::CommandLine* command_line) override {
......
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