Commit 14ac32ed authored by Sergei Datsenko's avatar Sergei Datsenko Committed by Commit Bot

Expose Drive lookup by document ID

Bug: 1105381
Change-Id: I4f63f8d7de3bf9bd530dbe842fe47d736e2b04d6
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2413567Reviewed-by: default avatarSam McNally <sammc@chromium.org>
Reviewed-by: default avatarAustin Tankiang <austinct@chromium.org>
Commit-Queue: Sergei Datsenko <dats@chromium.org>
Cr-Commit-Position: refs/heads/master@{#807844}
parent f4e7778e
......@@ -1104,6 +1104,15 @@ void DriveIntegrationService::GetMetadata(
std::move(callback), drive::FILE_ERROR_SERVICE_UNAVAILABLE, nullptr));
}
void DriveIntegrationService::LocateFilesByItemIds(
const std::vector<std::string>& item_ids,
drivefs::mojom::DriveFs::LocateFilesByItemIdsCallback callback) {
if (!IsMounted() || !GetDriveFsInterface()) {
std::move(callback).Run({});
}
GetDriveFsInterface()->LocateFilesByItemIds(item_ids, std::move(callback));
}
void DriveIntegrationService::RestartDrive() {
MaybeRemountFileSystem(base::TimeDelta(), false);
}
......
......@@ -166,6 +166,12 @@ class DriveIntegrationService : public KeyedService,
void GetMetadata(const base::FilePath& local_path,
drivefs::mojom::DriveFs::GetMetadataCallback callback);
// Locates files or dirs by their server-side ID. Paths are relative to the
// mount point.
void LocateFilesByItemIds(
const std::vector<std::string>& item_ids,
drivefs::mojom::DriveFs::LocateFilesByItemIdsCallback callback);
void RestartDrive();
// Sets the arguments to be parsed by DriveFS on startup. Should only be
......
......@@ -21,6 +21,7 @@
namespace drive {
class DriveIntegrationServiceBrowserTest : public InProcessBrowserTest {
public:
bool SetUpUserDataDirectory() override {
return drive::SetUpUserDataDirectoryForDriveFsTest();
}
......@@ -34,6 +35,10 @@ class DriveIntegrationServiceBrowserTest : public InProcessBrowserTest {
&create_drive_integration_service_);
}
drivefs::FakeDriveFs* GetFakeDriveFsForProfile(Profile* profile) {
return &fake_drivefs_helpers_[profile]->fake_drivefs();
}
private:
drive::DriveIntegrationService* CreateDriveIntegrationService(
Profile* profile) {
......@@ -169,4 +174,45 @@ IN_PROC_BROWSER_TEST_F(DriveIntegrationServiceBrowserTest, GetMetadata) {
run_loop.Run();
}
}
IN_PROC_BROWSER_TEST_F(DriveIntegrationServiceBrowserTest,
LocateFilesByItemIds) {
base::ScopedAllowBlockingForTesting allow_blocking;
auto* drive_service =
DriveIntegrationServiceFactory::FindForProfile(browser()->profile());
drivefs::FakeDriveFs* fake = GetFakeDriveFsForProfile(browser()->profile());
base::FilePath mount_path = drive_service->GetMountPointPath();
base::FilePath path;
base::CreateTemporaryFileInDir(mount_path, &path);
base::FilePath some_file("/");
CHECK(mount_path.AppendRelativePath(path, &some_file));
base::FilePath dir_path;
base::CreateTemporaryDirInDir(mount_path, "tmp-", &dir_path);
base::CreateTemporaryFileInDir(dir_path, &path);
base::FilePath some_other_file("/");
CHECK(mount_path.AppendRelativePath(path, &some_other_file));
fake->SetMetadata(some_file, "text/plain", some_file.BaseName().value(),
false, false, {}, {}, "abc123");
fake->SetMetadata(some_other_file, "text/plain", some_file.BaseName().value(),
false, false, {}, {}, "qwertyqwerty");
{
base::RunLoop run_loop;
auto quit_closure = run_loop.QuitClosure();
drive_service->LocateFilesByItemIds(
{"qwertyqwerty", "foobar"},
base::BindLambdaForTesting(
[=](base::Optional<std::vector<drivefs::mojom::FilePathOrErrorPtr>>
result) {
ASSERT_EQ(2u, result->size());
EXPECT_EQ(some_other_file,
base::FilePath("/").Append(result->at(0)->get_path()));
EXPECT_EQ(FILE_ERROR_NOT_FOUND, result->at(1)->get_error());
quit_closure.Run();
}));
run_loop.Run();
}
}
} // namespace drive
......@@ -1136,7 +1136,8 @@ class DriveFsTestVolume : public TestVolume {
entry.capabilities.can_add_children},
{entry.folder_feature.is_machine_root,
entry.folder_feature.is_arbitrary_sync_folder,
entry.folder_feature.is_external_media});
entry.folder_feature.is_external_media},
"");
ASSERT_TRUE(UpdateModifiedTime(entry));
}
......
......@@ -96,6 +96,7 @@ struct FakeDriveFs::FileMetadata {
std::string original_name;
mojom::Capabilities capabilities;
mojom::FolderFeature folder_feature;
std::string doc_id;
};
class FakeDriveFs::SearchQuery : public mojom::SearchQuery {
......@@ -272,13 +273,15 @@ void FakeDriveFs::SetMetadata(const base::FilePath& path,
bool pinned,
bool shared,
const mojom::Capabilities& capabilities,
const mojom::FolderFeature& folder_feature) {
const mojom::FolderFeature& folder_feature,
const std::string& doc_id) {
auto& stored_metadata = metadata_[path];
stored_metadata.mime_type = mime_type;
stored_metadata.original_name = original_name;
stored_metadata.hosted = (original_name != path.BaseName().value());
stored_metadata.capabilities = capabilities;
stored_metadata.folder_feature = folder_feature;
stored_metadata.doc_id = doc_id;
if (pinned) {
stored_metadata.pinned = true;
}
......@@ -451,4 +454,39 @@ void FakeDriveFs::CreateNativeHostSession(
mojo::PendingReceiver<drivefs::mojom::NativeMessagingHost> session,
mojo::PendingRemote<drivefs::mojom::NativeMessagingPort> port) {}
void FakeDriveFs::LocateFilesByItemIds(
const std::vector<std::string>& item_ids,
drivefs::mojom::DriveFs::LocateFilesByItemIdsCallback callback) {
base::flat_map<std::string, base::FilePath> results;
{
base::ScopedAllowBlockingForTesting allow_io;
base::FileEnumerator enumerator(
mount_path_, true,
base::FileEnumerator::FILES | base::FileEnumerator::DIRECTORIES);
base::FilePath path = enumerator.Next();
while (!path.empty()) {
base::FilePath relative_path;
CHECK(mount_path_.AppendRelativePath(path, &relative_path));
const auto& stored_metadata =
metadata_[base::FilePath("/").Append(relative_path)];
if (!stored_metadata.doc_id.empty() &&
base::Contains(item_ids, stored_metadata.doc_id)) {
results[stored_metadata.doc_id] = relative_path;
}
path = enumerator.Next();
}
}
std::vector<drivefs::mojom::FilePathOrErrorPtr> response;
for (const auto& id : item_ids) {
auto it = results.find(id);
if (it == results.end()) {
response.push_back(drivefs::mojom::FilePathOrError::NewError(
drive::FileError::FILE_ERROR_NOT_FOUND));
} else {
response.push_back(drivefs::mojom::FilePathOrError::NewPath(it->second));
}
}
std::move(callback).Run(std::move(response));
}
} // namespace drivefs
......@@ -55,7 +55,8 @@ class FakeDriveFs : public drivefs::mojom::DriveFs,
bool pinned,
bool shared,
const mojom::Capabilities& capabilities,
const mojom::FolderFeature& folder_feature);
const mojom::FolderFeature& folder_feature,
const std::string& doc_id);
const base::FilePath& mount_path() { return mount_path_; }
......@@ -122,6 +123,10 @@ class FakeDriveFs : public drivefs::mojom::DriveFs,
mojo::PendingReceiver<drivefs::mojom::NativeMessagingHost> session,
mojo::PendingRemote<drivefs::mojom::NativeMessagingPort> port) override;
void LocateFilesByItemIds(
const std::vector<std::string>& item_ids,
drivefs::mojom::DriveFs::LocateFilesByItemIdsCallback callback) override;
const base::FilePath mount_path_;
std::map<base::FilePath, FileMetadata> metadata_;
......
......@@ -100,6 +100,11 @@ interface DriveFs {
CreateNativeHostSession(ExtensionConnectionParams params,
pending_receiver<NativeMessagingHost> session,
pending_remote<NativeMessagingPort> port);
// Find the files corresponding to Drive's item IDs provided.
// Paths returned are relative to the mount point.
LocateFilesByItemIds(array<string> item_ids) => (
array<FilePathOrError>? response);
};
// Implemented by Chrome, used from DriveFS.
......@@ -463,3 +468,9 @@ interface NativeMessagingHost {
struct ExtensionConnectionParams {
string extension_id;
};
// A single file path resolution result.
union FilePathOrError {
FileError error;
mojo_base.mojom.FilePath path;
};
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