1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739
|
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chromeos/ash/components/drivefs/fake_drivefs.h"
#include <algorithm>
#include <string_view>
#include <tuple>
#include <utility>
#include <vector>
#include "base/containers/contains.h"
#include "base/files/file.h"
#include "base/files/file_enumerator.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/no_destructor.h"
#include "base/numerics/safe_conversions.h"
#include "base/strings/strcat.h"
#include "base/strings/string_util.h"
#include "base/task/thread_pool.h"
#include "base/threading/thread_restrictions.h"
#include "chromeos/ash/components/dbus/cros_disks/cros_disks_client.h"
#include "chromeos/ash/components/dbus/cros_disks/fake_cros_disks_client.h"
#include "chromeos/ash/components/drivefs/drivefs_util.h"
#include "chromeos/ash/components/drivefs/mojom/drivefs.mojom-shared.h"
#include "chromeos/ash/components/drivefs/mojom/drivefs.mojom.h"
#include "chromeos/components/drivefs/mojom/drivefs_native_messaging.mojom.h"
#include "components/drive/file_errors.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "mojo/public/cpp/bindings/self_owned_receiver.h"
#include "net/base/mime_util.h"
#include "url/gurl.h"
namespace drivefs {
FakeMetadata::FakeMetadata() = default;
FakeMetadata::~FakeMetadata() = default;
FakeMetadata::FakeMetadata(FakeMetadata&& other) = default;
FakeMetadata& FakeMetadata::operator=(FakeMetadata&& other) = default;
namespace {
std::vector<std::pair<base::RepeatingCallback<std::string()>,
base::WeakPtr<FakeDriveFs>>>&
GetRegisteredFakeDriveFsIntances() {
static base::NoDestructor<std::vector<std::pair<
base::RepeatingCallback<std::string()>, base::WeakPtr<FakeDriveFs>>>>
registered_fake_drivefs_instances;
return *registered_fake_drivefs_instances;
}
base::FilePath MaybeMountDriveFs(
const std::string& source_path,
const std::vector<std::string>& mount_options) {
GURL source_url(source_path);
DCHECK(source_url.is_valid());
if (source_url.scheme() != "drivefs") {
return {};
}
std::string datadir_suffix;
for (const auto& option : mount_options) {
if (base::StartsWith(option, "datadir=", base::CompareCase::SENSITIVE)) {
auto datadir =
base::FilePath(std::string_view(option).substr(strlen("datadir=")));
CHECK(datadir.IsAbsolute());
CHECK(!datadir.ReferencesParent());
datadir_suffix = datadir.BaseName().value();
break;
}
}
CHECK(!datadir_suffix.empty());
for (auto& registration : GetRegisteredFakeDriveFsIntances()) {
std::string account_id = registration.first.Run();
if (registration.second && !account_id.empty() &&
account_id == datadir_suffix) {
return registration.second->mount_path();
}
}
NOTREACHED() << datadir_suffix;
}
} // namespace
FakeDriveFsBootstrapListener::FakeDriveFsBootstrapListener(
mojo::PendingRemote<drivefs::mojom::DriveFsBootstrap> bootstrap)
: bootstrap_(std::move(bootstrap)) {}
FakeDriveFsBootstrapListener::~FakeDriveFsBootstrapListener() = default;
void FakeDriveFsBootstrapListener::SendInvitationOverPipe(base::ScopedFD) {}
mojo::PendingRemote<mojom::DriveFsBootstrap>
FakeDriveFsBootstrapListener::bootstrap() {
return std::move(bootstrap_);
}
class FakeDriveFs::SearchQuery : public mojom::SearchQuery {
public:
SearchQuery(base::WeakPtr<FakeDriveFs> drive_fs,
drivefs::mojom::QueryParametersPtr params)
: drive_fs_(std::move(drive_fs)), params_(std::move(params)) {}
SearchQuery(const SearchQuery&) = delete;
SearchQuery& operator=(const SearchQuery&) = delete;
private:
void GetNextPage(GetNextPageCallback callback) override {
if (!drive_fs_) {
std::move(callback).Run(drive::FileError::FILE_ERROR_ABORT, {});
} else if (next_page_called_) {
// If GetNextPage was previously called, on the next request send an empty
// array. This is the current way to identify if a query has no more
// results, however, this is slightly incorrect and b/277018122 tracks
// providing a more robust fix.
next_page_called_ = false;
std::move(callback).Run(drive::FILE_ERROR_OK, {});
} else {
// Default implementation: just search for a file name.
callback_ = std::move(callback);
base::ThreadPool::PostTaskAndReplyWithResult(
FROM_HERE, {base::MayBlock(), base::TaskPriority::BEST_EFFORT},
base::BindOnce(&SearchQuery::SearchFiles, drive_fs_->mount_path()),
base::BindOnce(&SearchQuery::GetMetadata,
weak_ptr_factory_.GetWeakPtr()));
next_page_called_ = true;
}
}
static std::vector<drivefs::mojom::QueryItemPtr> SearchFiles(
const base::FilePath& mount_path) {
std::vector<drivefs::mojom::QueryItemPtr> results;
base::FileEnumerator walker(
mount_path, true,
base::FileEnumerator::FILES | base::FileEnumerator::DIRECTORIES);
for (auto file = walker.Next(); !file.empty(); file = walker.Next()) {
auto item = drivefs::mojom::QueryItem::New();
item->path = base::FilePath("/");
CHECK(mount_path.AppendRelativePath(file, &item->path));
results.push_back(std::move(item));
}
return results;
}
void GetMetadata(std::vector<drivefs::mojom::QueryItemPtr> results) {
if (!drive_fs_) {
std::move(callback_).Run(drive::FileError::FILE_ERROR_ABORT, {});
} else {
results_ = std::move(results);
pending_callbacks_ = results_.size() + 1;
for (size_t i = 0; i < results_.size(); ++i) {
drive_fs_->GetMetadata(
results_[i]->path,
base::BindOnce(&SearchQuery::OnMetadata,
weak_ptr_factory_.GetWeakPtr(), i));
}
OnComplete();
}
}
void OnMetadata(size_t index,
drive::FileError error,
drivefs::mojom::FileMetadataPtr metadata) {
if (error == drive::FileError::FILE_ERROR_OK) {
results_[index]->metadata = std::move(metadata);
}
OnComplete();
}
void OnComplete() {
if (--pending_callbacks_ == 0) {
auto query = base::ToLowerASCII(
params_->title.value_or(params_->text_content.value_or("")));
std::vector<std::string> mime_types;
if (params_->mime_types.has_value()) {
mime_types = params_->mime_types.value();
}
if (params_->mime_type.has_value()) {
mime_types.emplace_back(params_->mime_type.value() + "/*");
}
// Filter out non-matching results.
std::erase_if(results_, [=, this](const auto& item_ptr) {
if (!item_ptr->metadata) {
return true;
}
const base::FilePath path = item_ptr->path;
const drivefs::mojom::FileMetadata* metadata = item_ptr->metadata.get();
if (!query.empty()) {
if (!base::Contains(base::ToLowerASCII(path.BaseName().value()),
query)) {
return true;
}
}
if (params_->available_offline) {
if (!metadata->available_offline && IsLocal(metadata->type)) {
return true;
}
}
if (params_->shared_with_me) {
if (!metadata->shared) {
return true;
}
}
if (params_->modified_time.has_value()) {
switch (params_->modified_time_operator) {
case mojom::QueryParameters::DateComparisonOperator::kLessThan:
if (metadata->modification_time >= *params_->modified_time) {
return true;
}
break;
case mojom::QueryParameters::DateComparisonOperator::kLessOrEqual:
if (metadata->modification_time > *params_->modified_time) {
return true;
}
break;
case mojom::QueryParameters::DateComparisonOperator::kEqual:
if (metadata->modification_time != *params_->modified_time) {
return true;
}
break;
case mojom::QueryParameters::DateComparisonOperator::
kGreaterOrEqual:
if (metadata->modification_time < *params_->modified_time) {
return true;
}
break;
case mojom::QueryParameters::DateComparisonOperator::kGreaterThan:
if (metadata->modification_time <= *params_->modified_time) {
return true;
}
break;
default:
break;
}
}
if (!mime_types.empty()) {
std::string content_mime_type = metadata->content_mime_type;
// If we do not know the MIME type the file may or may not match. Thus
// we only test MIME type match if we know the files MIME type.
if (!content_mime_type.empty()) {
if (std::ranges::none_of(
mime_types,
[content_mime_type](const std::string& mime_type) {
return net::MatchesMimeType(mime_type, content_mime_type);
})) {
return true;
}
}
}
return false;
});
const auto sort_direction = params_->sort_direction;
switch (params_->sort_field) {
case mojom::QueryParameters::SortField::kLastModified:
case mojom::QueryParameters::SortField::kLastViewedByMe:
std::sort(
results_.begin(), results_.end(),
[sort_direction](const auto& a, const auto& b) {
auto a_fields = std::tie(a->metadata->last_viewed_by_me_time,
a->metadata->modification_time);
auto b_fields = std::tie(b->metadata->last_viewed_by_me_time,
b->metadata->modification_time);
if (sort_direction ==
mojom::QueryParameters::SortDirection::kAscending) {
return a_fields < b_fields;
}
return b_fields < a_fields;
});
break;
case mojom::QueryParameters::SortField::kSharedWithMe:
NOTIMPLEMENTED();
break;
case mojom::QueryParameters::SortField::kFileSize:
NOTIMPLEMENTED();
break;
case mojom::QueryParameters::SortField::kNone:
break;
}
auto page_size = base::saturated_cast<size_t>(params_->page_size);
if (results_.size() > page_size) {
results_.resize(page_size);
}
std::move(callback_).Run(drive::FileError::FILE_ERROR_OK,
{std::move(results_)});
}
}
base::WeakPtr<FakeDriveFs> drive_fs_;
mojom::QueryParametersPtr params_;
GetNextPageCallback callback_;
std::vector<drivefs::mojom::QueryItemPtr> results_;
size_t pending_callbacks_ = 0;
bool next_page_called_ = false;
base::WeakPtrFactory<SearchQuery> weak_ptr_factory_{this};
};
FakeDriveFs::FileMetadata::FileMetadata() = default;
FakeDriveFs::FileMetadata::FileMetadata(const FakeDriveFs::FileMetadata&) =
default;
FakeDriveFs::FileMetadata& FakeDriveFs::FileMetadata::operator=(
const FakeDriveFs::FileMetadata&) = default;
FakeDriveFs::FileMetadata::~FileMetadata() = default;
FakeDriveFs::FakeDriveFs(const base::FilePath& mount_path)
: mount_path_(mount_path) {
CHECK(mount_path.IsAbsolute());
CHECK(!mount_path.ReferencesParent());
ON_CALL(*this, StartSearchQuery)
.WillByDefault(
[this](mojo::PendingReceiver<drivefs::mojom::SearchQuery> receiver,
drivefs::mojom::QueryParametersPtr query_params) {
auto search_query = std::make_unique<SearchQuery>(
weak_factory_.GetWeakPtr(), std::move(query_params));
mojo::MakeSelfOwnedReceiver(std::move(search_query),
std::move(receiver));
});
}
FakeDriveFs::~FakeDriveFs() = default;
void FakeDriveFs::RegisterMountingForAccountId(
base::RepeatingCallback<std::string()> account_id_getter) {
static_cast<ash::FakeCrosDisksClient*>(ash::CrosDisksClient::Get())
->AddCustomMountPointCallback(base::BindRepeating(&MaybeMountDriveFs));
GetRegisteredFakeDriveFsIntances().emplace_back(std::move(account_id_getter),
weak_factory_.GetWeakPtr());
}
std::unique_ptr<drivefs::DriveFsBootstrapListener>
FakeDriveFs::CreateMojoListener() {
delegate_.reset();
pending_delegate_receiver_ = delegate_.BindNewPipeAndPassReceiver();
delegate_->OnMounted();
bootstrap_receiver_.reset();
return std::make_unique<FakeDriveFsBootstrapListener>(
bootstrap_receiver_.BindNewPipeAndPassRemote());
}
void FakeDriveFs::SetMetadata(const FakeMetadata& metadata) {
auto& stored_metadata = metadata_[metadata.path];
stored_metadata.mime_type = metadata.mime_type;
stored_metadata.original_name = metadata.original_name;
stored_metadata.hosted =
(metadata.original_name != metadata.path.BaseName().value());
stored_metadata.capabilities = metadata.capabilities;
stored_metadata.folder_feature = metadata.folder_feature;
stored_metadata.doc_id = metadata.doc_id;
stored_metadata.pinned = metadata.pinned;
stored_metadata.dirty = metadata.dirty;
stored_metadata.available_offline = metadata.available_offline;
stored_metadata.shared = metadata.shared;
if (metadata.shortcut) {
drivefs::mojom::ShortcutDetails shortcut_details;
shortcut_details.target_lookup_status =
drivefs::mojom::ShortcutDetails::LookupStatus::kOk;
if (!metadata.shortcut_target_path.empty()) {
shortcut_details.target_path =
std::make_optional<base::FilePath>(metadata.shortcut_target_path);
}
stored_metadata.shortcut_details = std::move(shortcut_details);
}
stored_metadata.alternate_url = metadata.alternate_url;
stored_metadata.can_pin = metadata.can_pin;
}
void FakeDriveFs::DisplayConfirmDialog(
drivefs::mojom::DialogReasonPtr reason,
drivefs::mojom::DriveFsDelegate::DisplayConfirmDialogCallback callback) {
DCHECK(delegate_);
delegate_->DisplayConfirmDialog(std::move(reason), std::move(callback));
}
std::optional<bool> FakeDriveFs::IsItemPinned(const std::string& path) {
for (const auto& metadata : metadata_) {
if (metadata.first.value() == path) {
return metadata.second.pinned;
}
}
return std::nullopt;
}
std::optional<bool> FakeDriveFs::IsItemDirty(const std::string& path) {
for (const auto& metadata : metadata_) {
if (metadata.first.value() == path) {
return metadata.second.dirty;
}
}
return std::nullopt;
}
bool FakeDriveFs::SetCanPin(const std::string& path, bool can_pin) {
for (auto& metadata : metadata_) {
if (metadata.first.value() == path) {
metadata.second.can_pin = can_pin;
return true;
}
}
return false;
}
std::optional<FakeDriveFs::FileMetadata> FakeDriveFs::GetItemMetadata(
const base::FilePath& path) {
const auto& metadata = metadata_.find(path);
if (metadata == metadata_.end()) {
return std::nullopt;
}
if (metadata->second.stable_id == 0) {
metadata->second.stable_id = next_stable_id_++;
}
return metadata->second;
}
void FakeDriveFs::Init(
drivefs::mojom::DriveFsConfigurationPtr config,
mojo::PendingReceiver<drivefs::mojom::DriveFs> receiver,
mojo::PendingRemote<drivefs::mojom::DriveFsDelegate> delegate) {
mojo::FusePipes(std::move(pending_delegate_receiver_), std::move(delegate));
receiver_.reset();
receiver_.Bind(std::move(receiver));
}
void FakeDriveFs::GetMetadata(const base::FilePath& path,
GetMetadataCallback callback) {
base::FilePath absolute_path = mount_path_;
CHECK(base::FilePath("/").AppendRelativePath(path, &absolute_path));
base::File::Info info;
{
base::ScopedAllowBlockingForTesting allow_io;
if (!base::GetFileInfo(absolute_path, &info)) {
std::move(callback).Run(drive::FILE_ERROR_NOT_FOUND, nullptr);
return;
}
}
auto metadata = drivefs::mojom::FileMetadata::New();
metadata->size = info.size;
metadata->modification_time = info.last_modified;
metadata->last_viewed_by_me_time = info.last_accessed;
if (metadata_[path].stable_id == 0) {
metadata_[path].stable_id = next_stable_id_++;
}
const auto& stored_metadata = metadata_[path];
metadata->item_id = stored_metadata.doc_id;
metadata->pinned = stored_metadata.pinned;
metadata->dirty = stored_metadata.dirty;
metadata->available_offline =
stored_metadata.pinned || stored_metadata.available_offline;
metadata->shared = stored_metadata.shared;
metadata->content_mime_type = stored_metadata.mime_type;
metadata->type = stored_metadata.hosted ? mojom::FileMetadata::Type::kHosted
: info.is_directory ? mojom::FileMetadata::Type::kDirectory
: mojom::FileMetadata::Type::kFile;
if (!stored_metadata.alternate_url.empty()) {
metadata->alternate_url = stored_metadata.alternate_url;
} else {
std::string_view prefix;
if (stored_metadata.hosted) {
prefix = "https://document_alternate_link/";
} else if (info.is_directory) {
prefix = "https://folder_alternate_link/";
} else {
prefix = "https://file_alternate_link/";
}
std::string suffix = stored_metadata.original_name.empty()
? path.BaseName().value()
: stored_metadata.original_name;
metadata->alternate_url = GURL(base::StrCat({prefix, suffix})).spec();
}
metadata->capabilities = stored_metadata.capabilities.Clone();
metadata->stable_id = stored_metadata.stable_id;
using CanPinStatus = mojom::FileMetadata::CanPinStatus;
metadata->can_pin =
(stored_metadata.can_pin) ? CanPinStatus::kOk : CanPinStatus::kDisabled;
if (stored_metadata.hosted) {
metadata->can_pin = CanPinStatus::kDisabled;
}
if (stored_metadata.shortcut_details.has_value()) {
metadata->shortcut_details = stored_metadata.shortcut_details->Clone();
}
std::move(callback).Run(drive::FILE_ERROR_OK, std::move(metadata));
}
void FakeDriveFs::GetMetadataByStableId(int64_t stable_id,
GetMetadataCallback callback) {
for (const auto& [path, metadata] : metadata_) {
if (metadata.stable_id == stable_id) {
GetMetadata(path, std::move(callback));
return;
}
}
std::move(callback).Run(drive::FILE_ERROR_NOT_FOUND, nullptr);
}
void FakeDriveFs::SetPinned(const base::FilePath& path,
bool pinned,
SetPinnedCallback callback) {
metadata_[path].pinned = pinned;
std::move(callback).Run(drive::FILE_ERROR_OK);
}
void FakeDriveFs::UpdateNetworkState(bool pause_syncing, bool is_offline) {}
void FakeDriveFs::ResetCache(ResetCacheCallback callback) {
std::move(callback).Run(drive::FILE_ERROR_OK);
}
void FakeDriveFs::GetThumbnail(const base::FilePath& path,
bool crop_to_square,
GetThumbnailCallback callback) {
std::move(callback).Run(std::nullopt);
}
void FakeDriveFs::CopyFile(const base::FilePath& source,
const base::FilePath& target,
CopyFileCallback callback) {
base::ScopedAllowBlockingForTesting allow_io;
base::FilePath source_absolute_path = mount_path_;
base::FilePath target_absolute_path = mount_path_;
CHECK(base::FilePath("/").AppendRelativePath(source, &source_absolute_path));
CHECK(base::FilePath("/").AppendRelativePath(target, &target_absolute_path));
base::File::Info source_info;
if (!base::GetFileInfo(source_absolute_path, &source_info)) {
std::move(callback).Run(drive::FILE_ERROR_NOT_FOUND);
return;
}
if (source_info.is_directory) {
std::move(callback).Run(drive::FILE_ERROR_NOT_A_FILE);
return;
}
base::File::Info target_directory_info;
if (!base::GetFileInfo(target_absolute_path.DirName(),
&target_directory_info)) {
std::move(callback).Run(drive::FILE_ERROR_NOT_FOUND);
return;
}
if (!target_directory_info.is_directory) {
std::move(callback).Run(drive::FILE_ERROR_INVALID_OPERATION);
return;
}
if (base::PathExists(target_absolute_path)) {
std::move(callback).Run(drive::FILE_ERROR_INVALID_OPERATION);
return;
}
if (!base::CopyFile(source_absolute_path, target_absolute_path)) {
std::move(callback).Run(drive::FILE_ERROR_FAILED);
return;
}
metadata_[target_absolute_path] = metadata_[source_absolute_path];
metadata_[target_absolute_path].stable_id = next_stable_id_++;
std::move(callback).Run(drive::FILE_ERROR_OK);
}
void FakeDriveFs::FetchAllChangeLogs() {}
void FakeDriveFs::FetchChangeLog(
std::vector<mojom::FetchChangeLogOptionsPtr> options) {}
void FakeDriveFs::SendNativeMessageRequest(
const std::string& request,
SendNativeMessageRequestCallback callback) {
std::move(callback).Run(drive::FILE_ERROR_SERVICE_UNAVAILABLE, "");
}
void FakeDriveFs::SetStartupArguments(const std::string& arguments,
SetStartupArgumentsCallback callback) {
std::move(callback).Run(false);
}
void FakeDriveFs::GetStartupArguments(GetStartupArgumentsCallback callback) {
std::move(callback).Run("");
}
void FakeDriveFs::SetTracingEnabled(bool enabled) {}
void FakeDriveFs::SetNetworkingEnabled(bool enabled) {}
void FakeDriveFs::ForcePauseSyncing(bool enable) {}
void FakeDriveFs::DumpAccountSettings() {}
void FakeDriveFs::LoadAccountSettings() {}
void FakeDriveFs::CreateNativeHostSession(
drivefs::mojom::ExtensionConnectionParamsPtr params,
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));
}
void FakeDriveFs::GetQuotaUsage(
drivefs::mojom::DriveFs::GetQuotaUsageCallback callback) {
std::move(callback).Run(drive::FileError::FILE_ERROR_SERVICE_UNAVAILABLE,
mojom::QuotaUsage::New());
}
void FakeDriveFs::SetPooledStorageQuotaUsage(int64_t used_user_bytes,
int64_t total_user_bytes,
bool organization_limit_exceeded) {
pooled_quota_usage_.used_user_bytes = used_user_bytes;
pooled_quota_usage_.total_user_bytes = total_user_bytes;
pooled_quota_usage_.organization_limit_exceeded = organization_limit_exceeded;
}
void FakeDriveFs::GetPooledQuotaUsage(
drivefs::mojom::DriveFs::GetPooledQuotaUsageCallback callback) {
auto usage = mojom::PooledQuotaUsage::New();
usage->user_type = pooled_quota_usage_.user_type;
usage->used_user_bytes = pooled_quota_usage_.used_user_bytes;
usage->total_user_bytes = pooled_quota_usage_.total_user_bytes;
usage->organization_limit_exceeded =
pooled_quota_usage_.organization_limit_exceeded;
usage->organization_name = "Test organization";
std::move(callback).Run(drive::FileError::FILE_ERROR_OK, std::move(usage));
}
void FakeDriveFs::SetPinnedByStableId(int64_t stable_id,
bool pinned,
SetPinnedCallback callback) {
for (auto& [key, metadata] : metadata_) {
if (metadata.stable_id == stable_id) {
metadata.pinned = pinned;
std::move(callback).Run(drive::FILE_ERROR_OK);
return;
}
}
std::move(callback).Run(drive::FILE_ERROR_NOT_FOUND);
}
void FakeDriveFs::ToggleMirroring(
bool enabled,
drivefs::mojom::DriveFs::ToggleMirroringCallback callback) {
std::move(callback).Run(drivefs::mojom::MirrorSyncStatus::kSuccess);
}
void FakeDriveFs::ToggleSyncForPath(
const base::FilePath& path,
drivefs::mojom::MirrorPathStatus status,
drivefs::mojom::DriveFs::ToggleSyncForPathCallback callback) {
if (status == drivefs::mojom::MirrorPathStatus::kStart) {
syncing_paths_.push_back(path);
} else {
// status == drivefs::mojom::MirrorPathStatus::kStop.
auto element = std::ranges::find(syncing_paths_, path);
syncing_paths_.erase(element);
}
std::move(callback).Run(drive::FileError::FILE_ERROR_OK);
}
void FakeDriveFs::PollHostedFilePinStates() {}
void FakeDriveFs::CancelUploadByPath(
const base::FilePath& path,
drivefs::mojom::DriveFs::CancelUploadMode cancel_mode) {}
void FakeDriveFs::SetDocsOfflineEnabled(
bool enabled,
drivefs::mojom::DriveFs::SetDocsOfflineEnabledCallback callback) {
std::move(callback).Run(drive::FILE_ERROR_OK,
drivefs::mojom::DocsOfflineEnableStatus::kSuccess);
}
void FakeDriveFs::GetDocsOfflineStats(
drivefs::mojom::DriveFs::GetDocsOfflineStatsCallback callback) {
drivefs::mojom::DocsOfflineStatsPtr stats =
drivefs::mojom::DocsOfflineStats::New();
std::move(callback).Run(drive::FILE_ERROR_OK, std::move(stats));
}
void FakeDriveFs::GetMirrorSyncStatusForFile(
const base::FilePath& path,
drivefs::mojom::DriveFs::GetMirrorSyncStatusForFileCallback callback) {
std::move(callback).Run(drivefs::mojom::MirrorItemSyncingStatus::kSynced);
}
void FakeDriveFs::GetMirrorSyncStatusForDirectory(
const base::FilePath& path,
drivefs::mojom::DriveFs::GetMirrorSyncStatusForDirectoryCallback callback) {
std::move(callback).Run(drivefs::mojom::MirrorItemSyncingStatus::kSynced);
}
} // namespace drivefs
|