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
|
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ash/crosapi/file_system_provider_service_ash.h"
#include "base/base64.h"
#include "base/numerics/safe_conversions.h"
#include "base/trace_event/trace_event.h"
#include "base/types/expected.h"
#include "chrome/browser/ash/file_system_provider/cloud_file_info.h"
#include "chrome/browser/ash/file_system_provider/icon_set.h"
#include "chrome/browser/ash/file_system_provider/operation_request_manager.h"
#include "chrome/browser/ash/file_system_provider/provided_file_system_info.h"
#include "chrome/browser/ash/file_system_provider/provider_interface.h"
#include "chrome/browser/ash/file_system_provider/request_value.h"
#include "chrome/browser/ash/file_system_provider/service.h"
#include "chrome/browser/chromeos/extensions/file_system_provider/provider_function.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "extensions/common/extension_id.h"
#include "ui/gfx/codec/png_codec.h"
#include "url/url_constants.h"
using ash::file_system_provider::IconSet;
using ash::file_system_provider::MountOptions;
using ash::file_system_provider::OpenedFiles;
using ash::file_system_provider::ProvidedFileSystemInfo;
using ash::file_system_provider::ProvidedFileSystemInterface;
using ash::file_system_provider::ProvidedFileSystemObserver;
using ash::file_system_provider::ProviderId;
using ash::file_system_provider::RequestValue;
using ash::file_system_provider::Service;
using ash::file_system_provider::Watchers;
namespace crosapi {
namespace {
constexpr char kDeserializationError[] = "deserialization error";
// Either returns a valid request manager for provider-level requests, or else
// an error string.
base::expected<ash::file_system_provider::RequestManager*, std::string>
GetProviderRequestManager(Profile* profile,
extensions::ExtensionId extension_id) {
Service* service = Service::Get(profile);
if (!service) {
return base::unexpected("File system provider service not found.");
}
ash::file_system_provider::ProviderInterface* provider =
service->GetProvider(ProviderId::CreateFromExtensionId(extension_id));
if (!provider) {
return base::unexpected(
extensions::FileErrorToString(base::File::FILE_ERROR_NOT_FOUND));
}
return provider->GetRequestManager();
}
// Either returns a valid request manager for file system level requests, or
// else an error string.
base::expected<ash::file_system_provider::OperationRequestManager*, std::string>
GetProvidedFileSystemRequestManager(
Profile* profile,
const mojom::FileSystemIdPtr& file_system_id) {
Service* service = Service::Get(profile);
if (!service) {
return base::unexpected("File system provider service not found.");
}
ProvidedFileSystemInterface* file_system = service->GetProvidedFileSystem(
ProviderId::CreateFromExtensionId(file_system_id->provider),
file_system_id->id);
if (!file_system) {
return base::unexpected(
extensions::FileErrorToString(base::File::FILE_ERROR_NOT_FOUND));
}
return file_system->GetRequestManager();
}
// Forwards an operation response from an extension to the request manager and
// then returns the error message. Empty string means success.
std::string ForwardOperationResponse(mojom::FileSystemIdPtr file_system_id,
int64_t request_id,
const RequestValue& value,
bool has_more,
Profile* profile) {
auto manager = GetProvidedFileSystemRequestManager(profile, file_system_id);
if (!manager.has_value())
return manager.error();
const base::File::Error result =
manager.value()->FulfillRequest(request_id, value, has_more);
if (result != base::File::FILE_OK) {
return extensions::FileErrorToString(result);
}
return "";
}
// Forwards an operation failure from an extension to the request manager and
// then returns the error message. Empty string means success.
std::string ForwardOperationFailure(mojom::FileSystemIdPtr file_system_id,
int64_t request_id,
const RequestValue& value,
base::File::Error error,
Profile* profile) {
auto manager = GetProvidedFileSystemRequestManager(profile, file_system_id);
if (!manager.has_value())
return manager.error();
const base::File::Error result =
manager.value()->RejectRequest(request_id, value, error);
if (result != base::File::FILE_OK) {
return extensions::FileErrorToString(result);
}
return "";
}
// Convert |result| to a string, empty string for success and invokes
// |callback|.
void RunErrorCallback(base::OnceCallback<void(const std::string&)> callback,
const base::File::Error result) {
std::string error;
if (result != base::File::FILE_OK) {
error = extensions::FileErrorToString(result);
}
std::move(callback).Run(std::move(error));
}
// Converts from native filesystem watchers to their mojom counterparts
std::vector<crosapi::mojom::FSPWatcherPtr> ConvertWatchersToMojom(
const Watchers& watchers) {
std::vector<crosapi::mojom::FSPWatcherPtr> mojom_watchers;
for (const auto& watcher : watchers) {
crosapi::mojom::FSPWatcherPtr watcher_item =
crosapi::mojom::FSPWatcher::New();
watcher_item->entry_path = watcher.second.entry_path;
watcher_item->recursive = watcher.second.recursive;
watcher_item->last_tag = watcher.second.last_tag;
mojom_watchers.push_back(std::move(watcher_item));
}
return mojom_watchers;
}
// Converts from native filesystem opened files to their mojom counterparts
std::vector<crosapi::mojom::OpenedFilePtr> ConvertOpenedFilesToMojom(
const OpenedFiles& opened_files) {
std::vector<crosapi::mojom::OpenedFilePtr> mojom_opened_files;
for (const auto& opened_file : opened_files) {
crosapi::mojom::OpenedFilePtr opened_file_item =
crosapi::mojom::OpenedFile::New();
opened_file_item->open_request_id = opened_file.first;
opened_file_item->file_path = opened_file.second.file_path.value();
switch (opened_file.second.mode) {
case ash::file_system_provider::OPEN_FILE_MODE_READ:
opened_file_item->mode = crosapi::mojom::OpenFileMode::kRead;
break;
case ash::file_system_provider::OPEN_FILE_MODE_WRITE:
opened_file_item->mode = crosapi::mojom::OpenFileMode::kWrite;
break;
}
mojom_opened_files.push_back(std::move(opened_file_item));
}
return mojom_opened_files;
}
// Converts native file system to mojom.
crosapi::mojom::FileSystemInfoPtr ConvertFileSystemToMojom(
Profile* profile,
const ProvidedFileSystemInfo& file_system_info,
const std::string& provider) {
Service* const service = Service::Get(profile);
crosapi::mojom::FileSystemInfoPtr item =
crosapi::mojom::FileSystemInfo::New();
item->metadata = crosapi::mojom::FileSystemMetadata::New();
item->metadata->file_system_id = crosapi::mojom::FileSystemId::New();
item->metadata->file_system_id->provider = provider;
ProvidedFileSystemInterface* const file_system =
service->GetProvidedFileSystem(file_system_info.provider_id(),
file_system_info.file_system_id());
DCHECK(file_system);
item->watchers = ConvertWatchersToMojom(
file_system_info.watchable() ? *file_system->GetWatchers() : Watchers());
item->opened_files = ConvertOpenedFilesToMojom(file_system->GetOpenedFiles());
item->metadata->file_system_id->id = file_system_info.file_system_id();
item->metadata->display_name = file_system_info.display_name();
item->metadata->writable = file_system_info.writable();
item->metadata->opened_files_limit = file_system_info.opened_files_limit();
item->metadata->supports_notify = file_system_info.supports_notify_tag();
return item;
}
storage::WatcherManager::ChangeType ParseChangeType(mojom::FSPChangeType type) {
switch (type) {
case mojom::FSPChangeType::kChanged:
return storage::WatcherManager::CHANGED;
case mojom::FSPChangeType::kDeleted:
return storage::WatcherManager::DELETED;
}
}
std::unique_ptr<ash::file_system_provider::CloudFileInfo> ParseCloudFileInfo(
mojom::CloudFileInfoPtr cloud_file_info) {
if (cloud_file_info.is_null()) {
return nullptr;
}
if (!cloud_file_info->version_tag.has_value()) {
return nullptr;
}
return std::make_unique<ash::file_system_provider::CloudFileInfo>(
cloud_file_info->version_tag.value());
}
// Convert the change from the mojom type to a native type.
ProvidedFileSystemObserver::Change ParseChange(mojom::FSPChangePtr change) {
return ProvidedFileSystemObserver::Change(
change->path, ParseChangeType(change->type),
ParseCloudFileInfo(std::move(change->cloud_file_info)));
}
// Converts a list of child changes from the mojom type to a native type.
std::unique_ptr<ProvidedFileSystemObserver::Changes> ParseChanges(
std::vector<mojom::FSPChangePtr> changes) {
auto results = std::make_unique<ProvidedFileSystemObserver::Changes>();
for (auto& change : changes) {
results->push_back(ParseChange(std::move(change)));
}
return results;
}
std::optional<GURL> ToPNGDataURL(const gfx::ImageSkia& image) {
if (image.isNull()) {
return std::nullopt;
}
std::optional<std::vector<uint8_t>> output =
gfx::PNGCodec::EncodeBGRASkBitmap(*image.bitmap(), false);
GURL url("data:image/png;base64," +
base::Base64Encode(output.value_or(std::vector<uint8_t>())));
if (url.spec().size() > url::kMaxURLChars) {
return std::nullopt;
}
return url;
}
} // namespace
FileSystemProviderServiceAsh::FileSystemProviderServiceAsh() = default;
FileSystemProviderServiceAsh::~FileSystemProviderServiceAsh() = default;
void FileSystemProviderServiceAsh::Mount(mojom::FileSystemMetadataPtr metadata,
bool persistent,
MountCallback callback) {
MountWithProfile(std::move(metadata), persistent, std::move(callback),
ProfileManager::GetPrimaryUserProfile());
}
void FileSystemProviderServiceAsh::Unmount(
mojom::FileSystemIdPtr file_system_id,
UnmountCallback callback) {
UnmountWithProfile(std::move(file_system_id), std::move(callback),
ProfileManager::GetPrimaryUserProfile());
}
void FileSystemProviderServiceAsh::GetAll(const std::string& provider,
GetAllCallback callback) {
GetAllWithProfile(provider, std::move(callback),
ProfileManager::GetPrimaryUserProfile());
}
void FileSystemProviderServiceAsh::Get(mojom::FileSystemIdPtr file_system_id,
GetCallback callback) {
GetWithProfile(std::move(file_system_id), std::move(callback),
ProfileManager::GetPrimaryUserProfile());
}
void FileSystemProviderServiceAsh::Notify(
mojom::FileSystemIdPtr file_system_id,
mojom::FSPWatcherPtr watcher,
mojom::FSPChangeType type,
std::vector<mojom::FSPChangePtr> changes,
NotifyCallback callback) {
NotifyWithProfile(std::move(file_system_id), std::move(watcher), type,
std::move(changes), std::move(callback),
ProfileManager::GetPrimaryUserProfile());
}
void FileSystemProviderServiceAsh::DeprecatedOperationFinished(
mojom::FSPOperationResponse response,
mojom::FileSystemIdPtr file_system_id,
int64_t request_id,
std::vector<base::Value> args,
OperationFinishedCallback callback) {
base::Value::List list;
for (auto& value : args) {
list.Append(std::move(value));
}
OperationFinished(response, std::move(file_system_id), request_id,
std::move(list), std::move(callback));
}
void FileSystemProviderServiceAsh::OperationFinished(
mojom::FSPOperationResponse response,
mojom::FileSystemIdPtr file_system_id,
int64_t request_id,
base::Value::List args,
OperationFinishedCallback callback) {
OperationFinishedWithProfile(response, std::move(file_system_id), request_id,
std::move(args), std::move(callback),
ProfileManager::GetPrimaryUserProfile());
}
void FileSystemProviderServiceAsh::OpenFileFinishedSuccessfully(
mojom::FileSystemIdPtr file_system_id,
int64_t request_id,
base::Value::List args,
OperationFinishedCallback callback) {
OpenFileFinishedSuccessfullyWithProfile(
std::move(file_system_id), request_id, std::move(args),
std::move(callback), ProfileManager::GetPrimaryUserProfile());
}
void FileSystemProviderServiceAsh::MountFinished(
const std::string& extension_id,
int64_t request_id,
base::Value::List args,
MountFinishedCallback callback) {
MountFinishedWithProfile(extension_id, request_id, std::move(args),
std::move(callback),
ProfileManager::GetPrimaryUserProfile());
}
void FileSystemProviderServiceAsh::ExtensionLoadedDeprecated(
bool configurable,
bool watchable,
bool multiple_mounts,
mojom::FileSystemSource source,
const std::string& name,
const std::string& id) {
ExtensionLoaded(configurable, watchable, multiple_mounts, source, name, id,
/*icon16x16=*/gfx::ImageSkia(),
/*icon32x32=*/gfx::ImageSkia());
}
void FileSystemProviderServiceAsh::ExtensionLoaded(
bool configurable,
bool watchable,
bool multiple_mounts,
mojom::FileSystemSource source,
const std::string& name,
const std::string& id,
const gfx::ImageSkia& icon16x16,
const gfx::ImageSkia& icon32x32) {
Service* const service =
Service::Get(ProfileManager::GetPrimaryUserProfile());
DCHECK(service);
ProviderId provider_id = ProviderId::CreateFromExtensionId(id);
extensions::FileSystemProviderSource extension_source;
switch (source) {
case crosapi::mojom::FileSystemSource::kFile:
extension_source = extensions::FileSystemProviderSource::SOURCE_FILE;
break;
case crosapi::mojom::FileSystemSource::kNetwork:
extension_source = extensions::FileSystemProviderSource::SOURCE_NETWORK;
break;
case crosapi::mojom::FileSystemSource::kDevice:
extension_source = extensions::FileSystemProviderSource::SOURCE_DEVICE;
break;
}
std::optional<IconSet> icon_set;
std::optional<GURL> url_icon16x16 = ToPNGDataURL(icon16x16);
std::optional<GURL> url_icon32x32 = ToPNGDataURL(icon32x32);
if (url_icon16x16 && url_icon32x32) {
icon_set = IconSet();
icon_set->SetIcon(IconSet::IconSize::SIZE_16x16, *url_icon16x16);
icon_set->SetIcon(IconSet::IconSize::SIZE_32x32, *url_icon32x32);
}
auto provider =
std::make_unique<ash::file_system_provider::ExtensionProvider>(
ProfileManager::GetPrimaryUserProfile(), std::move(provider_id),
ash::file_system_provider::Capabilities{
.configurable = configurable,
.watchable = watchable,
.multiple_mounts = multiple_mounts,
.source = extension_source},
name, icon_set);
service->RegisterProvider(std::move(provider));
}
void FileSystemProviderServiceAsh::ExtensionUnloaded(const std::string& id,
bool due_to_shutdown) {
Service* const service =
Service::Get(ProfileManager::GetPrimaryUserProfile());
DCHECK(service);
ProviderId provider_id = ProviderId::CreateFromExtensionId(id);
service->UnregisterProvider(
provider_id,
due_to_shutdown
? ash::file_system_provider::Service::UNMOUNT_REASON_SHUTDOWN
: ash::file_system_provider::Service::UNMOUNT_REASON_USER);
}
void FileSystemProviderServiceAsh::MountWithProfile(
mojom::FileSystemMetadataPtr metadata,
bool persistent,
MountCallback callback,
Profile* profile) {
Service* const service = Service::Get(profile);
DCHECK(service);
MountOptions options;
options.file_system_id = metadata->file_system_id->id;
options.display_name = metadata->display_name;
options.writable = metadata->writable;
options.opened_files_limit =
base::saturated_cast<int>(metadata->opened_files_limit);
options.supports_notify_tag = metadata->supports_notify;
options.persistent = persistent;
const base::File::Error result = service->MountFileSystem(
ProviderId::CreateFromExtensionId(metadata->file_system_id->provider),
options);
RunErrorCallback(std::move(callback), result);
}
void FileSystemProviderServiceAsh::UnmountWithProfile(
mojom::FileSystemIdPtr file_system_id,
UnmountCallback callback,
Profile* profile) {
Service* const service = Service::Get(profile);
const base::File::Error result = service->UnmountFileSystem(
ProviderId::CreateFromExtensionId(file_system_id->provider),
file_system_id->id, Service::UNMOUNT_REASON_USER);
RunErrorCallback(std::move(callback), result);
}
void FileSystemProviderServiceAsh::GetAllWithProfile(
const std::string& provider,
GetAllCallback callback,
Profile* profile) {
Service* const service = Service::Get(profile);
ProviderId provider_id = ProviderId::CreateFromExtensionId(provider);
const std::vector<ProvidedFileSystemInfo> file_systems =
service->GetProvidedFileSystemInfoList(provider_id);
std::vector<crosapi::mojom::FileSystemInfoPtr> items;
for (const auto& file_system_info : file_systems) {
items.push_back(
ConvertFileSystemToMojom(profile, file_system_info, provider));
}
std::move(callback).Run(std::move(items));
}
void FileSystemProviderServiceAsh::FileSystemProviderServiceAsh::GetWithProfile(
mojom::FileSystemIdPtr file_system_id,
GetCallback callback,
Profile* profile) {
Service* const service = Service::Get(profile);
DCHECK(service);
ProvidedFileSystemInterface* file_system = service->GetProvidedFileSystem(
ProviderId::CreateFromExtensionId(file_system_id->provider),
file_system_id->id);
if (!file_system) {
std::move(callback).Run(nullptr);
return;
}
std::move(callback).Run(ConvertFileSystemToMojom(
profile, file_system->GetFileSystemInfo(), file_system_id->provider));
}
void FileSystemProviderServiceAsh::NotifyWithProfile(
mojom::FileSystemIdPtr file_system_id,
mojom::FSPWatcherPtr watcher,
mojom::FSPChangeType type,
std::vector<mojom::FSPChangePtr> changes,
NotifyCallback callback,
Profile* profile) {
Service* const service = Service::Get(profile);
DCHECK(service);
ProvidedFileSystemInterface* const file_system =
service->GetProvidedFileSystem(
ProviderId::CreateFromExtensionId(file_system_id->provider),
file_system_id->id);
if (!file_system) {
std::move(callback).Run(
extensions::FileErrorToString(base::File::FILE_ERROR_NOT_FOUND));
return;
}
file_system->Notify(watcher->entry_path, watcher->recursive,
ParseChangeType(type), ParseChanges(std::move(changes)),
watcher->last_tag,
base::BindOnce(&RunErrorCallback, std::move(callback)));
}
void FileSystemProviderServiceAsh::OperationFinishedWithProfile(
mojom::FSPOperationResponse response,
mojom::FileSystemIdPtr file_system_id,
int64_t request_id,
base::Value::List args,
OperationFinishedCallback callback,
Profile* profile) {
std::string error;
switch (response) {
case mojom::FSPOperationResponse::kUnknown:
error = "unknown operation response";
break;
case mojom::FSPOperationResponse::kUnmountSuccess: {
using extensions::api::file_system_provider_internal::
UnmountRequestedSuccess::Params;
std::optional<Params> params = Params::Create(std::move(args));
if (!params) {
error = kDeserializationError;
break;
}
auto value = RequestValue::CreateForUnmountSuccess(std::move(*params));
error = ForwardOperationResponse(std::move(file_system_id), request_id,
value, /*has_more=*/false, profile);
break;
}
case mojom::FSPOperationResponse::kGetEntryMetadataSuccess: {
using extensions::api::file_system_provider_internal::
GetMetadataRequestedSuccess::Params;
std::optional<Params> params = Params::Create(std::move(args));
if (!params) {
error = kDeserializationError;
break;
}
auto value =
RequestValue::CreateForGetMetadataSuccess(std::move(*params));
error = ForwardOperationResponse(std::move(file_system_id), request_id,
value, /*has_more=*/false, profile);
break;
}
case mojom::FSPOperationResponse::kGetActionsSuccess: {
using extensions::api::file_system_provider_internal::
GetActionsRequestedSuccess::Params;
std::optional<Params> params = Params::Create(std::move(args));
if (!params) {
error = kDeserializationError;
break;
}
auto value = RequestValue::CreateForGetActionsSuccess(std::move(*params));
error = ForwardOperationResponse(std::move(file_system_id), request_id,
value, /*has_more=*/false, profile);
break;
}
case mojom::FSPOperationResponse::kReadDirectorySuccess: {
using extensions::api::file_system_provider_internal::
ReadDirectoryRequestedSuccess::Params;
std::optional<Params> params = Params::Create(std::move(args));
if (!params) {
error = kDeserializationError;
break;
}
bool has_more = params->has_more;
auto value =
RequestValue::CreateForReadDirectorySuccess(std::move(*params));
error = ForwardOperationResponse(std::move(file_system_id), request_id,
value, has_more, profile);
break;
}
case mojom::FSPOperationResponse::kReadFileSuccess: {
TRACE_EVENT0("file_system_provider", "ReadFileSuccessWithProfile");
using extensions::api::file_system_provider_internal::
ReadFileRequestedSuccess::Params;
std::optional<Params> params = Params::Create(std::move(args));
if (!params) {
error = kDeserializationError;
break;
}
bool has_more = params->has_more;
auto value = RequestValue::CreateForReadFileSuccess(std::move(*params));
error = ForwardOperationResponse(std::move(file_system_id), request_id,
value, has_more, profile);
break;
}
case mojom::FSPOperationResponse::kOpenFileSuccess: {
TRACE_EVENT0("file_system_provider", "OpenFileSuccessWithProfile");
using extensions::api::file_system_provider_internal::
OpenFileRequestedSuccess::Params;
std::optional<Params> params = Params::Create(std::move(args));
if (!params) {
error = kDeserializationError;
break;
}
auto value = RequestValue::CreateForOpenFileSuccess(std::move(*params));
error = ForwardOperationResponse(std::move(file_system_id), request_id,
value, /*has_more=*/false, profile);
break;
}
case mojom::FSPOperationResponse::kGenericSuccess: {
using extensions::api::file_system_provider_internal::
OperationRequestedSuccess::Params;
std::optional<Params> params = Params::Create(std::move(args));
if (!params) {
error = kDeserializationError;
break;
}
auto value = RequestValue::CreateForOperationSuccess(std::move(*params));
error = ForwardOperationResponse(std::move(file_system_id), request_id,
value, /*has_more=*/false, profile);
break;
}
case mojom::FSPOperationResponse::kGenericFailure: {
using extensions::api::file_system_provider_internal::
OperationRequestedError::Params;
std::optional<Params> params = Params::Create(std::move(args));
if (!params) {
error = kDeserializationError;
break;
}
base::File::Error operation_error =
extensions::ProviderErrorToFileError(params->error);
auto value = RequestValue::CreateForOperationError(std::move(*params));
error = ForwardOperationFailure(std::move(file_system_id), request_id,
value, operation_error, profile);
break;
}
}
std::move(callback).Run(std::move(error));
}
void FileSystemProviderServiceAsh::OpenFileFinishedSuccessfullyWithProfile(
mojom::FileSystemIdPtr file_system_id,
int64_t request_id,
base::Value::List args,
OperationFinishedCallback callback,
Profile* profile) {
using extensions::api::file_system_provider_internal::
OpenFileRequestedSuccess::Params;
std::optional<Params> params = Params::Create(std::move(args));
if (!params) {
std::move(callback).Run(kDeserializationError);
}
auto value = RequestValue::CreateForOpenFileSuccess(std::move(*params));
std::string error =
ForwardOperationResponse(std::move(file_system_id), request_id, value,
/*has_more=*/false, profile);
std::move(callback).Run(std::move(error));
}
void FileSystemProviderServiceAsh::MountFinishedWithProfile(
const std::string& extension_id,
int64_t request_id,
base::Value::List args,
MountFinishedCallback callback,
Profile* profile) {
auto manager = GetProviderRequestManager(profile, extension_id);
if (!manager.has_value()) {
std::move(callback).Run(manager.error());
return;
}
using extensions::api::file_system_provider_internal::RespondToMountRequest::
Params;
std::optional<Params> params = Params::Create(std::move(args));
if (!params) {
std::move(callback).Run(kDeserializationError);
return;
}
base::File::Error mount_error =
extensions::ProviderErrorToFileError(params->error);
base::File::Error result =
mount_error == base::File::FILE_OK
? manager.value()->FulfillRequest(request_id,
/*response=*/RequestValue(),
/*has_more=*/false)
: manager.value()->RejectRequest(
request_id, /*response=*/RequestValue(), mount_error);
std::string error_str;
if (result != base::File::FILE_OK)
error_str = extensions::FileErrorToString(result);
std::move(callback).Run(error_str);
}
} // namespace crosapi
|