Skip to content

Commit

Permalink
fusebox: add Storage{At,De}tached signals
Browse files Browse the repository at this point in the history
Prior to this commit, the FuseBoxReverseService had {At,De}tachStorage
methods. These (1-to-1) methods still exist after this commit, but the
equivalent (1-to-N) signals enable experimenting with simplifying or
even removing the FuseBoxReverseService interface, which would allow the
FuseBoxService to serve multiple clients simultaneously, not just one.

Bug: 1249754
Change-Id: Icb96fa01a4613f4701ce599d720263b211c2f8d0
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3826416
Reviewed-by: Ben Reich <benreich@chromium.org>
Reviewed-by: Ryo Hashimoto <hashimoto@chromium.org>
Commit-Queue: Nigel Tao <nigeltao@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1034936}
  • Loading branch information
Nigel Tao authored and Chromium LUCI CQ committed Aug 15, 2022
1 parent dcbed62 commit afad9ed
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 5 deletions.
26 changes: 25 additions & 1 deletion chrome/browser/ash/dbus/fusebox_service_provider.cc
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ void ReplyToStat(dbus::MethodCall* method_call,

} // namespace

FuseBoxServiceProvider::FuseBoxServiceProvider() = default;
FuseBoxServiceProvider::FuseBoxServiceProvider() : server_(this) {}

FuseBoxServiceProvider::~FuseBoxServiceProvider() = default;

Expand All @@ -146,6 +146,7 @@ void FuseBoxServiceProvider::Start(scoped_refptr<dbus::ExportedObject> object) {
return;
}

exported_object_ = object;
object->ExportMethod(fusebox::kFuseBoxServiceInterface, fusebox::kCloseMethod,
base::BindRepeating(&FuseBoxServiceProvider::Close,
weak_ptr_factory_.GetWeakPtr()),
Expand All @@ -169,6 +170,29 @@ void FuseBoxServiceProvider::Start(scoped_refptr<dbus::ExportedObject> object) {
base::BindOnce(&OnExportedCallback));
}

void FuseBoxServiceProvider::OnRegisterFSURLPrefix(const std::string& subdir) {
if (!exported_object_) {
return;
}
dbus::Signal signal(fusebox::kFuseBoxServiceInterface,
fusebox::kStorageAttachedSignal);
dbus::MessageWriter writer(&signal);
writer.AppendString(subdir);
exported_object_->SendSignal(&signal);
}

void FuseBoxServiceProvider::OnUnregisterFSURLPrefix(
const std::string& subdir) {
if (!exported_object_) {
return;
}
dbus::Signal signal(fusebox::kFuseBoxServiceInterface,
fusebox::kStorageDetachedSignal);
dbus::MessageWriter writer(&signal);
writer.AppendString(subdir);
exported_object_->SendSignal(&signal);
}

void FuseBoxServiceProvider::Close(
dbus::MethodCall* method_call,
dbus::ExportedObject::ResponseSender sender) {
Expand Down
9 changes: 7 additions & 2 deletions chrome/browser/ash/dbus/fusebox_service_provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ namespace ash {

// FuseBoxServiceProvider implements the org.chromium.FuseBoxService D-Bus
// interface.
class FuseBoxServiceProvider
: public CrosDBusService::ServiceProviderInterface {
class FuseBoxServiceProvider : public CrosDBusService::ServiceProviderInterface,
public fusebox::Server::Delegate {
public:
FuseBoxServiceProvider();
FuseBoxServiceProvider(const FuseBoxServiceProvider&) = delete;
Expand All @@ -28,6 +28,10 @@ class FuseBoxServiceProvider
void Start(scoped_refptr<dbus::ExportedObject> object) override;

private:
// fusebox::Server::Delegate overrides:
void OnRegisterFSURLPrefix(const std::string& subdir) override;
void OnUnregisterFSURLPrefix(const std::string& subdir) override;

// D-Bus methods.
//
// In terms of semantics, they're roughly equivalent to the C standard
Expand All @@ -44,6 +48,7 @@ class FuseBoxServiceProvider
void Stat(dbus::MethodCall* method_call,
dbus::ExportedObject::ResponseSender sender);

scoped_refptr<dbus::ExportedObject> exported_object_;
fusebox::Server server_;

// base::WeakPtr{this} factory.
Expand Down
8 changes: 7 additions & 1 deletion chrome/browser/ash/fusebox/fusebox_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ Server* Server::GetInstance() {
return g_server_instance;
}

Server::Server() {
Server::Server(Delegate* delegate) : delegate_(delegate) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
DCHECK(!g_server_instance);
g_server_instance = this;
Expand Down Expand Up @@ -300,6 +300,9 @@ void Server::RegisterFSURLPrefix(const std::string& subdir,
std::string trimmed =
std::string(base::TrimString(fs_url_prefix, "/", base::TRIM_TRAILING));
prefix_map_.insert({subdir, PrefixMapEntry(trimmed, read_only)});
if (delegate_) {
delegate_->OnRegisterFSURLPrefix(subdir);
}
}

void Server::UnregisterFSURLPrefix(const std::string& subdir) {
Expand All @@ -309,6 +312,9 @@ void Server::UnregisterFSURLPrefix(const std::string& subdir) {
if (iter != prefix_map_.end()) {
prefix_map_.erase(iter);
}
if (delegate_) {
delegate_->OnUnregisterFSURLPrefix(subdir);
}
}

void Server::Close(std::string fs_url_as_string, CloseCallback callback) {
Expand Down
12 changes: 11 additions & 1 deletion chrome/browser/ash/fusebox/fusebox_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,19 @@ namespace fusebox {

class Server {
public:
struct Delegate {
// These methods cause D-Bus signals to be sent that a storage unit (as
// named by the "subdir" in "/media/fuse/fusebox/subdir") has been attached
// or detached.
virtual void OnRegisterFSURLPrefix(const std::string& subdir) = 0;
virtual void OnUnregisterFSURLPrefix(const std::string& subdir) = 0;
};

// Returns a pointer to the global Server instance.
static Server* GetInstance();

Server();
// The delegate should live longer than the server.
explicit Server(Delegate* delegate);
Server(const Server&) = delete;
Server& operator=(const Server&) = delete;
~Server();
Expand Down Expand Up @@ -99,6 +108,7 @@ class Server {
using PrefixMap = std::map<std::string, PrefixMapEntry>;

private:
Delegate* delegate_;
fusebox::MonikerMap moniker_map_;
PrefixMap prefix_map_;
};
Expand Down

0 comments on commit afad9ed

Please sign in to comment.