mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-01-04 01:05:58 +03:00
FileSystemAccessServer: Use Core::Stream
This patch also updates corresponding functions from `LibFileSystemAccessServerClient`. From the FileSystemAccessClient point of view, it only makes the server take `Core::Stream::OpenMode` instead of `Core::OpenMode`. So, `enum` conversions only happen within deprecated functions and not in the new `Core::Stream` friendly API. On the server side, it just removes two usages of `Core::File::open()`.
This commit is contained in:
parent
4e164c9de7
commit
c0bc3b9814
Notes:
sideshowbarker
2024-07-17 02:05:39 +09:00
Author: https://github.com/LucasChollet Commit: https://github.com/SerenityOS/serenity/commit/c0bc3b9814 Pull-request: https://github.com/SerenityOS/serenity/pull/16553 Reviewed-by: https://github.com/AtkinsSJ ✅
@ -48,7 +48,28 @@ DeprecatedResult Client::try_request_file_read_only_approved(GUI::Window* parent
|
||||
return handle_promise<DeprecatedResult>(id);
|
||||
}
|
||||
|
||||
DeprecatedResult Client::try_request_file(GUI::Window* parent_window, DeprecatedString const& path, Core::OpenMode mode)
|
||||
static Core::Stream::OpenMode to_stream_open_mode(Core::OpenMode open_mode)
|
||||
{
|
||||
Core::Stream::OpenMode result {};
|
||||
if ((open_mode & Core::OpenMode::ReadOnly) == Core::OpenMode::ReadOnly)
|
||||
result |= Core::Stream::OpenMode::Read;
|
||||
if ((open_mode & Core::OpenMode::WriteOnly) == Core::OpenMode::WriteOnly)
|
||||
result |= Core::Stream::OpenMode::Write;
|
||||
if ((open_mode & Core::OpenMode::ReadWrite) == Core::OpenMode::ReadWrite)
|
||||
result |= Core::Stream::OpenMode::ReadWrite;
|
||||
if ((open_mode & Core::OpenMode::Append) == Core::OpenMode::Append)
|
||||
result |= Core::Stream::OpenMode::Append;
|
||||
if ((open_mode & Core::OpenMode::Truncate) == Core::OpenMode::Truncate)
|
||||
result |= Core::Stream::OpenMode::Truncate;
|
||||
if ((open_mode & Core::OpenMode::MustBeNew) == Core::OpenMode::MustBeNew)
|
||||
result |= Core::Stream::OpenMode::MustBeNew;
|
||||
if ((open_mode & Core::OpenMode::KeepOnExec) == Core::OpenMode::KeepOnExec)
|
||||
result |= Core::Stream::OpenMode::KeepOnExec;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
DeprecatedResult Client::try_request_file(GUI::Window* parent_window, DeprecatedString const& path, Core::OpenMode deprecated_mode)
|
||||
{
|
||||
auto const id = get_new_id();
|
||||
m_promises.set(id, PromiseAndWindow { { Core::Promise<DeprecatedResult>::construct() }, parent_window });
|
||||
@ -63,6 +84,8 @@ DeprecatedResult Client::try_request_file(GUI::Window* parent_window, Deprecated
|
||||
GUI::ConnectionToWindowServer::the().remove_window_stealing_for_client(child_window_server_client_id, parent_window_id);
|
||||
});
|
||||
|
||||
auto const mode = to_stream_open_mode(deprecated_mode);
|
||||
|
||||
if (path.starts_with('/')) {
|
||||
async_request_file(id, parent_window_server_client_id, parent_window_id, path, mode);
|
||||
} else {
|
||||
@ -73,7 +96,7 @@ DeprecatedResult Client::try_request_file(GUI::Window* parent_window, Deprecated
|
||||
return handle_promise<DeprecatedResult>(id);
|
||||
}
|
||||
|
||||
DeprecatedResult Client::try_open_file(GUI::Window* parent_window, DeprecatedString const& window_title, StringView path, Core::OpenMode requested_access)
|
||||
DeprecatedResult Client::try_open_file(GUI::Window* parent_window, DeprecatedString const& window_title, StringView path, Core::OpenMode deprecated_requested_access)
|
||||
{
|
||||
auto const id = get_new_id();
|
||||
m_promises.set(id, PromiseAndWindow { { Core::Promise<DeprecatedResult>::construct() }, parent_window });
|
||||
@ -88,12 +111,14 @@ DeprecatedResult Client::try_open_file(GUI::Window* parent_window, DeprecatedStr
|
||||
GUI::ConnectionToWindowServer::the().remove_window_stealing_for_client(child_window_server_client_id, parent_window_id);
|
||||
});
|
||||
|
||||
auto const requested_access = to_stream_open_mode(deprecated_requested_access);
|
||||
|
||||
async_prompt_open_file(id, parent_window_server_client_id, parent_window_id, window_title, path, requested_access);
|
||||
|
||||
return handle_promise<DeprecatedResult>(id);
|
||||
}
|
||||
|
||||
DeprecatedResult Client::try_save_file_deprecated(GUI::Window* parent_window, DeprecatedString const& name, DeprecatedString const ext, Core::OpenMode requested_access)
|
||||
DeprecatedResult Client::try_save_file_deprecated(GUI::Window* parent_window, DeprecatedString const& name, DeprecatedString const ext, Core::OpenMode deprecated_requested_access)
|
||||
{
|
||||
auto const id = get_new_id();
|
||||
m_promises.set(id, PromiseAndWindow { { Core::Promise<DeprecatedResult>::construct() }, parent_window });
|
||||
@ -108,6 +133,8 @@ DeprecatedResult Client::try_save_file_deprecated(GUI::Window* parent_window, De
|
||||
GUI::ConnectionToWindowServer::the().remove_window_stealing_for_client(child_window_server_client_id, parent_window_id);
|
||||
});
|
||||
|
||||
auto const requested_access = to_stream_open_mode(deprecated_requested_access);
|
||||
|
||||
async_prompt_save_file(id, parent_window_server_client_id, parent_window_id, name.is_null() ? "Untitled" : name, ext.is_null() ? "txt" : ext, Core::StandardPaths::home_directory(), requested_access);
|
||||
|
||||
return handle_promise<DeprecatedResult>(id);
|
||||
@ -128,10 +155,7 @@ Result Client::save_file(GUI::Window* parent_window, DeprecatedString const& nam
|
||||
GUI::ConnectionToWindowServer::the().remove_window_stealing_for_client(child_window_server_client_id, parent_window_id);
|
||||
});
|
||||
|
||||
// The endpoint only cares about ReadOnly, WriteOnly and ReadWrite and both enum shares the same layout for these.
|
||||
Core::OpenMode deprecated_requested_access = static_cast<Core::OpenMode>(requested_access);
|
||||
|
||||
async_prompt_save_file(id, parent_window_server_client_id, parent_window_id, name.is_null() ? "Untitled" : name, ext.is_null() ? "txt" : ext, Core::StandardPaths::home_directory(), deprecated_requested_access);
|
||||
async_prompt_save_file(id, parent_window_server_client_id, parent_window_id, name.is_null() ? "Untitled" : name, ext.is_null() ? "txt" : ext, Core::StandardPaths::home_directory(), requested_access);
|
||||
|
||||
return handle_promise<Result>(id);
|
||||
}
|
||||
|
@ -6,7 +6,7 @@
|
||||
|
||||
#include <FileSystemAccessServer/ConnectionFromClient.h>
|
||||
#include <LibCore/File.h>
|
||||
#include <LibCore/IODevice.h>
|
||||
#include <LibCore/Stream.h>
|
||||
#include <LibGUI/Application.h>
|
||||
#include <LibGUI/ConnectionToWindowServer.h>
|
||||
#include <LibGUI/FilePicker.h>
|
||||
@ -42,15 +42,15 @@ RefPtr<GUI::Window> ConnectionFromClient::create_dummy_child_window(i32 window_s
|
||||
return window;
|
||||
}
|
||||
|
||||
void ConnectionFromClient::request_file_handler(i32 request_id, i32 window_server_client_id, i32 parent_window_id, DeprecatedString const& path, Core::OpenMode const& requested_access, ShouldPrompt prompt)
|
||||
void ConnectionFromClient::request_file_handler(i32 request_id, i32 window_server_client_id, i32 parent_window_id, DeprecatedString const& path, Core::Stream::OpenMode requested_access, ShouldPrompt prompt)
|
||||
{
|
||||
VERIFY(path.starts_with("/"sv));
|
||||
|
||||
bool approved = false;
|
||||
auto maybe_permissions = m_approved_files.get(path);
|
||||
|
||||
auto relevant_permissions = requested_access & (Core::OpenMode::ReadOnly | Core::OpenMode::WriteOnly);
|
||||
VERIFY(relevant_permissions != Core::OpenMode::NotOpen);
|
||||
auto relevant_permissions = requested_access & (Core::Stream::OpenMode::Read | Core::Stream::OpenMode::Write);
|
||||
VERIFY(relevant_permissions != Core::Stream::OpenMode::NotOpen);
|
||||
|
||||
if (maybe_permissions.has_value())
|
||||
approved = has_flag(maybe_permissions.value(), relevant_permissions);
|
||||
@ -58,11 +58,11 @@ void ConnectionFromClient::request_file_handler(i32 request_id, i32 window_serve
|
||||
if (!approved) {
|
||||
DeprecatedString access_string;
|
||||
|
||||
if (has_flag(requested_access, Core::OpenMode::ReadWrite))
|
||||
if (has_flag(requested_access, Core::Stream::OpenMode::ReadWrite))
|
||||
access_string = "read and write";
|
||||
else if (has_flag(requested_access, Core::OpenMode::ReadOnly))
|
||||
else if (has_flag(requested_access, Core::Stream::OpenMode::Read))
|
||||
access_string = "read from";
|
||||
else if (has_flag(requested_access, Core::OpenMode::WriteOnly))
|
||||
else if (has_flag(requested_access, Core::Stream::OpenMode::Write))
|
||||
access_string = "write to";
|
||||
|
||||
auto pid = this->socket().peer_pid().release_value_but_fixme_should_propagate_errors();
|
||||
@ -90,13 +90,13 @@ void ConnectionFromClient::request_file_handler(i32 request_id, i32 window_serve
|
||||
}
|
||||
|
||||
if (approved) {
|
||||
auto file = Core::File::open(path, requested_access);
|
||||
auto file = Core::Stream::File::open(path, requested_access);
|
||||
|
||||
if (file.is_error()) {
|
||||
dbgln("FileSystemAccessServer: Couldn't open {}, error {}", path, file.error());
|
||||
async_handle_prompt_end(request_id, errno, Optional<IPC::File> {}, path);
|
||||
} else {
|
||||
async_handle_prompt_end(request_id, 0, IPC::File(file.value()->leak_fd(), IPC::File::CloseAfterSending), path);
|
||||
async_handle_prompt_end(request_id, 0, IPC::File(*file.release_value(), IPC::File::CloseAfterSending), path);
|
||||
}
|
||||
} else {
|
||||
async_handle_prompt_end(request_id, -1, Optional<IPC::File> {}, path);
|
||||
@ -105,18 +105,18 @@ void ConnectionFromClient::request_file_handler(i32 request_id, i32 window_serve
|
||||
|
||||
void ConnectionFromClient::request_file_read_only_approved(i32 request_id, i32 window_server_client_id, i32 parent_window_id, DeprecatedString const& path)
|
||||
{
|
||||
request_file_handler(request_id, window_server_client_id, parent_window_id, path, Core::OpenMode::ReadOnly, ShouldPrompt::No);
|
||||
request_file_handler(request_id, window_server_client_id, parent_window_id, path, Core::Stream::OpenMode::Read, ShouldPrompt::No);
|
||||
}
|
||||
|
||||
void ConnectionFromClient::request_file(i32 request_id, i32 window_server_client_id, i32 parent_window_id, DeprecatedString const& path, Core::OpenMode const& requested_access)
|
||||
void ConnectionFromClient::request_file(i32 request_id, i32 window_server_client_id, i32 parent_window_id, DeprecatedString const& path, Core::Stream::OpenMode requested_access)
|
||||
{
|
||||
request_file_handler(request_id, window_server_client_id, parent_window_id, path, requested_access, ShouldPrompt::Yes);
|
||||
}
|
||||
|
||||
void ConnectionFromClient::prompt_open_file(i32 request_id, i32 window_server_client_id, i32 parent_window_id, DeprecatedString const& window_title, DeprecatedString const& path_to_view, Core::OpenMode const& requested_access)
|
||||
void ConnectionFromClient::prompt_open_file(i32 request_id, i32 window_server_client_id, i32 parent_window_id, DeprecatedString const& window_title, DeprecatedString const& path_to_view, Core::Stream::OpenMode requested_access)
|
||||
{
|
||||
auto relevant_permissions = requested_access & (Core::OpenMode::ReadOnly | Core::OpenMode::WriteOnly);
|
||||
VERIFY(relevant_permissions != Core::OpenMode::NotOpen);
|
||||
auto relevant_permissions = requested_access & (Core::Stream::OpenMode::Read | Core::Stream::OpenMode::Write);
|
||||
VERIFY(relevant_permissions != Core::Stream::OpenMode::NotOpen);
|
||||
|
||||
auto main_window = create_dummy_child_window(window_server_client_id, parent_window_id);
|
||||
|
||||
@ -125,10 +125,10 @@ void ConnectionFromClient::prompt_open_file(i32 request_id, i32 window_server_cl
|
||||
prompt_helper(request_id, user_picked_file, requested_access);
|
||||
}
|
||||
|
||||
void ConnectionFromClient::prompt_save_file(i32 request_id, i32 window_server_client_id, i32 parent_window_id, DeprecatedString const& name, DeprecatedString const& ext, DeprecatedString const& path_to_view, Core::OpenMode const& requested_access)
|
||||
void ConnectionFromClient::prompt_save_file(i32 request_id, i32 window_server_client_id, i32 parent_window_id, DeprecatedString const& name, DeprecatedString const& ext, DeprecatedString const& path_to_view, Core::Stream::OpenMode requested_access)
|
||||
{
|
||||
auto relevant_permissions = requested_access & (Core::OpenMode::ReadOnly | Core::OpenMode::WriteOnly);
|
||||
VERIFY(relevant_permissions != Core::OpenMode::NotOpen);
|
||||
auto relevant_permissions = requested_access & (Core::Stream::OpenMode::Read | Core::Stream::OpenMode::Write);
|
||||
VERIFY(relevant_permissions != Core::Stream::OpenMode::NotOpen);
|
||||
|
||||
auto main_window = create_dummy_child_window(window_server_client_id, parent_window_id);
|
||||
|
||||
@ -137,11 +137,11 @@ void ConnectionFromClient::prompt_save_file(i32 request_id, i32 window_server_cl
|
||||
prompt_helper(request_id, user_picked_file, requested_access);
|
||||
}
|
||||
|
||||
void ConnectionFromClient::prompt_helper(i32 request_id, Optional<DeprecatedString> const& user_picked_file, Core::OpenMode const& requested_access)
|
||||
void ConnectionFromClient::prompt_helper(i32 request_id, Optional<DeprecatedString> const& user_picked_file, Core::Stream::OpenMode requested_access)
|
||||
{
|
||||
if (user_picked_file.has_value()) {
|
||||
VERIFY(user_picked_file->starts_with("/"sv));
|
||||
auto file = Core::File::open(user_picked_file.value(), requested_access);
|
||||
auto file = Core::Stream::File::open(user_picked_file.value(), requested_access);
|
||||
|
||||
if (file.is_error()) {
|
||||
dbgln("FileSystemAccessServer: Couldn't open {}, error {}", user_picked_file.value(), file.error());
|
||||
@ -149,13 +149,13 @@ void ConnectionFromClient::prompt_helper(i32 request_id, Optional<DeprecatedStri
|
||||
async_handle_prompt_end(request_id, errno, Optional<IPC::File> {}, user_picked_file);
|
||||
} else {
|
||||
auto maybe_permissions = m_approved_files.get(user_picked_file.value());
|
||||
auto new_permissions = requested_access & (Core::OpenMode::ReadOnly | Core::OpenMode::WriteOnly);
|
||||
auto new_permissions = requested_access & (Core::Stream::OpenMode::Read | Core::Stream::OpenMode::Write);
|
||||
if (maybe_permissions.has_value())
|
||||
new_permissions |= maybe_permissions.value();
|
||||
|
||||
m_approved_files.set(user_picked_file.value(), new_permissions);
|
||||
|
||||
async_handle_prompt_end(request_id, 0, IPC::File(file.value()->leak_fd(), IPC::File::CloseAfterSending), user_picked_file);
|
||||
async_handle_prompt_end(request_id, 0, IPC::File(*file.release_value(), IPC::File::CloseAfterSending), user_picked_file);
|
||||
}
|
||||
} else {
|
||||
async_handle_prompt_end(request_id, -1, Optional<IPC::File> {}, Optional<DeprecatedString> {});
|
||||
|
@ -28,22 +28,22 @@ private:
|
||||
explicit ConnectionFromClient(NonnullOwnPtr<Core::Stream::LocalSocket>);
|
||||
|
||||
virtual void request_file_read_only_approved(i32, i32, i32, DeprecatedString const&) override;
|
||||
virtual void request_file(i32, i32, i32, DeprecatedString const&, Core::OpenMode const&) override;
|
||||
virtual void prompt_open_file(i32, i32, i32, DeprecatedString const&, DeprecatedString const&, Core::OpenMode const&) override;
|
||||
virtual void prompt_save_file(i32, i32, i32, DeprecatedString const&, DeprecatedString const&, DeprecatedString const&, Core::OpenMode const&) override;
|
||||
virtual void request_file(i32, i32, i32, DeprecatedString const&, Core::Stream::OpenMode) override;
|
||||
virtual void prompt_open_file(i32, i32, i32, DeprecatedString const&, DeprecatedString const&, Core::Stream::OpenMode) override;
|
||||
virtual void prompt_save_file(i32, i32, i32, DeprecatedString const&, DeprecatedString const&, DeprecatedString const&, Core::Stream::OpenMode) override;
|
||||
|
||||
void prompt_helper(i32, Optional<DeprecatedString> const&, Core::OpenMode const&);
|
||||
void prompt_helper(i32, Optional<DeprecatedString> const&, Core::Stream::OpenMode);
|
||||
RefPtr<GUI::Window> create_dummy_child_window(i32, i32);
|
||||
|
||||
enum class ShouldPrompt {
|
||||
No,
|
||||
Yes
|
||||
};
|
||||
void request_file_handler(i32, i32, i32, DeprecatedString const&, Core::OpenMode const&, ShouldPrompt);
|
||||
void request_file_handler(i32, i32, i32, DeprecatedString const&, Core::Stream::OpenMode, ShouldPrompt);
|
||||
|
||||
virtual Messages::FileSystemAccessServer::ExposeWindowServerClientIdResponse expose_window_server_client_id() override;
|
||||
|
||||
HashMap<DeprecatedString, Core::OpenMode> m_approved_files;
|
||||
HashMap<DeprecatedString, Core::Stream::OpenMode> m_approved_files;
|
||||
};
|
||||
|
||||
}
|
||||
|
@ -1,12 +1,12 @@
|
||||
#include <AK/URL.h>
|
||||
#include <LibCore/IODevice.h>
|
||||
#include <LibCore/Stream.h>
|
||||
|
||||
endpoint FileSystemAccessServer
|
||||
{
|
||||
request_file_read_only_approved(i32 request_id, i32 window_server_client_id, i32 window_id, DeprecatedString path) =|
|
||||
request_file(i32 request_id, i32 window_server_client_id, i32 window_id, DeprecatedString path, Core::OpenMode requested_access) =|
|
||||
prompt_open_file(i32 request_id, i32 window_server_client_id, i32 window_id, DeprecatedString window_title, DeprecatedString path_to_view, Core::OpenMode requested_access) =|
|
||||
prompt_save_file(i32 request_id, i32 window_server_client_id, i32 window_id, DeprecatedString title, DeprecatedString ext, DeprecatedString path_to_view, Core::OpenMode requested_access) =|
|
||||
request_file(i32 request_id, i32 window_server_client_id, i32 window_id, DeprecatedString path, Core::Stream::OpenMode requested_access) =|
|
||||
prompt_open_file(i32 request_id, i32 window_server_client_id, i32 window_id, DeprecatedString window_title, DeprecatedString path_to_view, Core::Stream::OpenMode requested_access) =|
|
||||
prompt_save_file(i32 request_id, i32 window_server_client_id, i32 window_id, DeprecatedString title, DeprecatedString ext, DeprecatedString path_to_view, Core::Stream::OpenMode requested_access) =|
|
||||
|
||||
expose_window_server_client_id() => (i32 client_id)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user