Skip to content

Commit

Permalink
fileSystemProvider: Send mount requests via EventDispatcher
Browse files Browse the repository at this point in the history
Use EventDispatcher to send mount requests to the extension, in line
with other operations (introduced in https://crrev.com/c/4060490), and
deduplicate response handling boilerplate: everything is now handled in
EventDispatcherImpl.

Bug: b:255520330, b:249182641
Test: run through Office setup flow
Change-Id: I9d00bc70922a2cd13100ba54556ab0c205c406fb
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4113648
Reviewed-by: Noel Gordon <noel@chromium.org>
Commit-Queue: Alexander Bolodurin <alexbn@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1085270}
  • Loading branch information
Alexander Bolodurin authored and Chromium LUCI CQ committed Dec 20, 2022
1 parent db38367 commit 7fbdb83
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 82 deletions.
15 changes: 12 additions & 3 deletions chrome/browser/ash/file_system_provider/extension_provider.cc
Expand Up @@ -12,6 +12,7 @@
#include "chrome/browser/apps/app_service/app_icon/app_icon_source.h"
#include "chrome/browser/apps/app_service/app_service_proxy.h"
#include "chrome/browser/apps/app_service/app_service_proxy_factory.h"
#include "chrome/browser/ash/file_system_provider/event_dispatcher_impl.h"
#include "chrome/browser/ash/file_system_provider/mount_request_handler.h"
#include "chrome/browser/ash/file_system_provider/provided_file_system.h"
#include "chrome/browser/ash/file_system_provider/throttled_file_system.h"
Expand Down Expand Up @@ -111,7 +112,7 @@ bool ExtensionProvider::RequestMount(Profile* profile,
auto split_callback = base::SplitOnceCallback(std::move(callback));
const int request_id = request_manager_->CreateRequest(
REQUEST_MOUNT,
std::make_unique<MountRequestHandler>(event_router, provider_id_,
std::make_unique<MountRequestHandler>(event_dispatcher_.get(),
std::move(split_callback.first)));
if (!request_id) {
std::move(split_callback.second).Run(base::File::FILE_ERROR_FAILED);
Expand All @@ -127,7 +128,11 @@ ExtensionProvider::ExtensionProvider(
const ProvidingExtensionInfo& info)
: provider_id_(ProviderId::CreateFromExtensionId(extension_id)),
request_manager_(
new RequestManager(profile, /*notification_manager=*/nullptr)) {
new RequestManager(profile, /*notification_manager=*/nullptr)),
event_dispatcher_(std::make_unique<EventDispatcherImpl>(
extension_id,
extensions::EventRouter::Get(profile),
request_manager_.get())) {
capabilities_.configurable = info.capabilities.configurable();
capabilities_.watchable = info.capabilities.watchable();
capabilities_.multiple_mounts = info.capabilities.multiple_mounts();
Expand All @@ -144,7 +149,11 @@ ExtensionProvider::ExtensionProvider(Profile* profile,
capabilities_(std::move(capabilities)),
name_(std::move(name)),
request_manager_(
new RequestManager(profile, /*notification_manager=*/nullptr)) {
new RequestManager(profile, /*notification_manager=*/nullptr)),
event_dispatcher_(std::make_unique<EventDispatcherImpl>(
provider_id_.GetExtensionId(),
extensions::EventRouter::Get(profile),
request_manager_.get())) {
ObserveAppServiceForIcons(profile);
}

Expand Down
3 changes: 3 additions & 0 deletions chrome/browser/ash/file_system_provider/extension_provider.h
Expand Up @@ -24,6 +24,8 @@ class ExtensionRegistry;
namespace ash {
namespace file_system_provider {

class EventDispatcher;

// Holds information for a providing extension.
struct ProvidingExtensionInfo {
ProvidingExtensionInfo();
Expand Down Expand Up @@ -79,6 +81,7 @@ class ExtensionProvider : public ProviderInterface,
std::string name_;
IconSet icon_set_;
std::unique_ptr<RequestManager> request_manager_;
std::unique_ptr<EventDispatcher> event_dispatcher_;
};

} // namespace file_system_provider
Expand Down
82 changes: 13 additions & 69 deletions chrome/browser/ash/file_system_provider/mount_request_handler.cc
Expand Up @@ -9,6 +9,7 @@
#include "chrome/browser/ash/crosapi/crosapi_ash.h"
#include "chrome/browser/ash/crosapi/crosapi_manager.h"
#include "chrome/browser/ash/crosapi/file_system_provider_service_ash.h"
#include "chrome/browser/ash/file_system_provider/event_dispatcher_impl.h"
#include "chrome/browser/ash/file_system_provider/provided_file_system_info.h"
#include "chrome/browser/ash/file_system_provider/service.h"
#include "chrome/browser/profiles/profile_manager.h"
Expand All @@ -17,82 +18,25 @@
#include "extensions/browser/event_router.h"

namespace ash::file_system_provider {
namespace {

// This method is only used when Lacros is enabled. It's a callback from Lacros
// indicating whether the mount request was successfully forwarded.
void OperationForwarded(ash::file_system_provider::ProviderId provider_id,
int request_id,
bool delivery_failure) {
using ash::file_system_provider::Service;
// Successful deliveries will go through the FileSystemProvider mojom path.
if (!delivery_failure)
return;
// When Lacros is enabled the primary profile is the only profile.
Service* const service =
Service::Get(ProfileManager::GetPrimaryUserProfile());
DCHECK(service);
ProviderInterface* const provider = service->GetProvider(provider_id);
if (!provider)
return;
provider->GetRequestManager()->RejectRequest(request_id,
std::make_unique<RequestValue>(),
base::File::FILE_ERROR_FAILED);
}
MountRequestHandler::MountRequestHandler(EventDispatcher* dispatcher,
RequestMountCallback callback)
: event_dispatcher_(dispatcher), callback_(std::move(callback)) {}

MountRequestHandler::~MountRequestHandler() = default;

// Implementation for dispatching an event.
bool DispatchEventImpl(extensions::EventRouter* event_router,
ProviderId provider_id,
int request_id) {
bool MountRequestHandler::Execute(int request_id) {
base::Value::List event_args;
event_args.reserve(1);
event_args.Append(base::Value(request_id));

const extensions::ExtensionId extension_id = provider_id.GetExtensionId();
extensions::events::HistogramValue histogram_value =
extensions::events::FILE_SYSTEM_PROVIDER_ON_MOUNT_REQUESTED;
const std::string event_name =
extensions::api::file_system_provider::OnMountRequested::kEventName;

// If ash has a matching extension, forward the event. This should not be
// needed once Lacros is the only browser on all devices.
if (event_router->ExtensionHasEventListener(extension_id, event_name)) {
event_router->DispatchEventToExtension(
extension_id, std::make_unique<extensions::Event>(
histogram_value, event_name, std::move(event_args)));
return true;
}

// If there are any Lacros remotes, forward the message to the first one. This
// does not support multiple remotes.
auto& remotes = crosapi::CrosapiManager::Get()
->crosapi_ash()
->file_system_provider_service_ash()
->remotes();
if (!remotes.empty()) {
auto remote = remotes.begin();
auto callback =
base::BindOnce(&OperationForwarded, provider_id, request_id);
(*remote)->ForwardOperation(
extension_id, static_cast<int32_t>(histogram_value), event_name,
std::move(event_args), std::move(callback));
}
return !remotes.empty();
}

} // namespace

MountRequestHandler::MountRequestHandler(extensions::EventRouter* event_router,
ProviderId provider_id,
RequestMountCallback callback)
: dispatch_event_impl_(
base::BindRepeating(&DispatchEventImpl, event_router, provider_id)),
callback_(std::move(callback)) {}
auto event = std::make_unique<extensions::Event>(
extensions::events::FILE_SYSTEM_PROVIDER_ON_MOUNT_REQUESTED,
extensions::api::file_system_provider::OnMountRequested::kEventName,
std::move(event_args));

MountRequestHandler::~MountRequestHandler() = default;

bool MountRequestHandler::Execute(int request_id) {
return dispatch_event_impl_.Run(request_id);
return event_dispatcher_->DispatchEvent(request_id, absl::nullopt,
std::move(event));
}

void MountRequestHandler::OnSuccess(int /* request_id */,
Expand Down
14 changes: 4 additions & 10 deletions chrome/browser/ash/file_system_provider/mount_request_handler.h
Expand Up @@ -5,21 +5,17 @@
#ifndef CHROME_BROWSER_ASH_FILE_SYSTEM_PROVIDER_MOUNT_REQUEST_HANDLER_H_
#define CHROME_BROWSER_ASH_FILE_SYSTEM_PROVIDER_MOUNT_REQUEST_HANDLER_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_manager.h"

namespace extensions {
class EventRouter;
} // namespace extensions

namespace ash::file_system_provider {

class EventDispatcher;

// Base class for operation bridges between fileapi and providing extensions.
class MountRequestHandler : public RequestManager::HandlerInterface {
public:
MountRequestHandler(extensions::EventRouter* event_router,
ProviderId provider_id,
MountRequestHandler(EventDispatcher* dispatcher,
RequestMountCallback callback);

MountRequestHandler(const MountRequestHandler&) = delete;
Expand All @@ -37,9 +33,7 @@ class MountRequestHandler : public RequestManager::HandlerInterface {
base::File::Error error) override;

private:
using DispatchEventInternalCallback =
base::RepeatingCallback<bool(int request_id)>;
DispatchEventInternalCallback dispatch_event_impl_;
raw_ptr<EventDispatcher> event_dispatcher_;
RequestMountCallback callback_;
};

Expand Down

0 comments on commit 7fbdb83

Please sign in to comment.