Client.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * Copyright (c) 2021, timmot <tiwwot@protonmail.com>
  3. * Copyright (c) 2022, Mustafa Quraish <mustafa@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/LexicalPath.h>
  8. #include <LibFileSystem/FileSystem.h>
  9. #include <LibFileSystemAccessClient/Client.h>
  10. #include <LibGUI/ConnectionToWindowServer.h>
  11. #include <LibGUI/MessageBox.h>
  12. #include <LibGUI/Window.h>
  13. namespace FileSystemAccessClient {
  14. static RefPtr<Client> s_the = nullptr;
  15. Client& Client::the()
  16. {
  17. if (!s_the || !s_the->is_open())
  18. s_the = Client::try_create().release_value_but_fixme_should_propagate_errors();
  19. return *s_the;
  20. }
  21. Result Client::request_file_read_only_approved(GUI::Window* parent_window, DeprecatedString const& path)
  22. {
  23. auto const id = get_new_id();
  24. m_promises.set(id, RequestData { { Core::Promise<Result>::construct() }, parent_window, Core::File::OpenMode::Read });
  25. auto parent_window_server_client_id = GUI::ConnectionToWindowServer::the().expose_client_id();
  26. auto child_window_server_client_id = expose_window_server_client_id();
  27. auto parent_window_id = parent_window->window_id();
  28. GUI::ConnectionToWindowServer::the().add_window_stealing_for_client(child_window_server_client_id, parent_window_id);
  29. ScopeGuard guard([parent_window_id, child_window_server_client_id] {
  30. GUI::ConnectionToWindowServer::the().remove_window_stealing_for_client(child_window_server_client_id, parent_window_id);
  31. });
  32. if (path.starts_with('/')) {
  33. async_request_file_read_only_approved(id, parent_window_server_client_id, parent_window_id, path);
  34. } else {
  35. auto full_path = LexicalPath::join(TRY(FileSystem::current_working_directory()), path).string();
  36. async_request_file_read_only_approved(id, parent_window_server_client_id, parent_window_id, full_path);
  37. }
  38. return handle_promise(id);
  39. }
  40. Result Client::request_file(GUI::Window* parent_window, DeprecatedString const& path, Core::File::OpenMode mode)
  41. {
  42. auto const id = get_new_id();
  43. m_promises.set(id, RequestData { { Core::Promise<Result>::construct() }, parent_window, mode });
  44. auto parent_window_server_client_id = GUI::ConnectionToWindowServer::the().expose_client_id();
  45. auto child_window_server_client_id = expose_window_server_client_id();
  46. auto parent_window_id = parent_window->window_id();
  47. GUI::ConnectionToWindowServer::the().add_window_stealing_for_client(child_window_server_client_id, parent_window_id);
  48. ScopeGuard guard([parent_window_id, child_window_server_client_id] {
  49. GUI::ConnectionToWindowServer::the().remove_window_stealing_for_client(child_window_server_client_id, parent_window_id);
  50. });
  51. if (path.starts_with('/')) {
  52. async_request_file(id, parent_window_server_client_id, parent_window_id, path, mode);
  53. } else {
  54. auto full_path = LexicalPath::join(TRY(FileSystem::current_working_directory()), path).string();
  55. async_request_file(id, parent_window_server_client_id, parent_window_id, full_path, mode);
  56. }
  57. return handle_promise(id);
  58. }
  59. Result Client::open_file(GUI::Window* parent_window, DeprecatedString const& window_title, StringView path, Core::File::OpenMode requested_access, Optional<Vector<GUI::FileTypeFilter>> const& allowed_file_types)
  60. {
  61. auto const id = get_new_id();
  62. m_promises.set(id, RequestData { { Core::Promise<Result>::construct() }, parent_window, requested_access });
  63. auto parent_window_server_client_id = GUI::ConnectionToWindowServer::the().expose_client_id();
  64. auto child_window_server_client_id = expose_window_server_client_id();
  65. auto parent_window_id = parent_window->window_id();
  66. GUI::ConnectionToWindowServer::the().add_window_stealing_for_client(child_window_server_client_id, parent_window_id);
  67. ScopeGuard guard([parent_window_id, child_window_server_client_id] {
  68. GUI::ConnectionToWindowServer::the().remove_window_stealing_for_client(child_window_server_client_id, parent_window_id);
  69. });
  70. async_prompt_open_file(id, parent_window_server_client_id, parent_window_id, window_title, path, requested_access, allowed_file_types);
  71. return handle_promise(id);
  72. }
  73. Result Client::save_file(GUI::Window* parent_window, DeprecatedString const& name, DeprecatedString const ext, Core::File::OpenMode requested_access)
  74. {
  75. auto const id = get_new_id();
  76. m_promises.set(id, RequestData { { Core::Promise<Result>::construct() }, parent_window, requested_access });
  77. auto parent_window_server_client_id = GUI::ConnectionToWindowServer::the().expose_client_id();
  78. auto child_window_server_client_id = expose_window_server_client_id();
  79. auto parent_window_id = parent_window->window_id();
  80. GUI::ConnectionToWindowServer::the().add_window_stealing_for_client(child_window_server_client_id, parent_window_id);
  81. ScopeGuard guard([parent_window_id, child_window_server_client_id] {
  82. GUI::ConnectionToWindowServer::the().remove_window_stealing_for_client(child_window_server_client_id, parent_window_id);
  83. });
  84. 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);
  85. return handle_promise(id);
  86. }
  87. void Client::handle_prompt_end(i32 request_id, i32 error, Optional<IPC::File> const& ipc_file, Optional<DeprecatedString> const& chosen_file)
  88. {
  89. auto potential_data = m_promises.get(request_id);
  90. VERIFY(potential_data.has_value());
  91. auto& request_data = potential_data.value();
  92. auto action = "Requesting"sv;
  93. if (has_flag(request_data.mode, Core::File::OpenMode::Read))
  94. action = "Opening"sv;
  95. else if (has_flag(request_data.mode, Core::File::OpenMode::Write))
  96. action = "Saving"sv;
  97. if (ipc_file.has_value()) {
  98. if (FileSystem::is_device(ipc_file->fd()))
  99. error = is_silencing_devices() ? ESUCCESS : EINVAL;
  100. else if (FileSystem::is_directory(ipc_file->fd()))
  101. error = is_silencing_directories() ? ESUCCESS : EISDIR;
  102. }
  103. switch (error) {
  104. case ESUCCESS:
  105. case ECANCELED:
  106. break;
  107. case ENOENT:
  108. if (is_silencing_nonexistent_entries())
  109. break;
  110. [[fallthrough]];
  111. default:
  112. auto maybe_message = ErrorOr<String>({});
  113. if (error == ECONNRESET)
  114. maybe_message = String::formatted("FileSystemAccessClient: {}", Error::from_errno(error));
  115. else
  116. maybe_message = String::formatted("{} \"{}\" failed: {}", action, *chosen_file, Error::from_errno(error));
  117. if (!maybe_message.is_error())
  118. (void)GUI::MessageBox::try_show_error(request_data.parent_window, maybe_message.release_value());
  119. }
  120. if (error != ESUCCESS)
  121. return (void)request_data.promise->resolve(Error::from_errno(error));
  122. auto file_or_error = [&]() -> ErrorOr<File> {
  123. auto stream = TRY(Core::File::adopt_fd(ipc_file->take_fd(), Core::File::OpenMode::ReadWrite));
  124. auto filename = TRY(String::from_deprecated_string(*chosen_file));
  125. return File({}, move(stream), filename);
  126. }();
  127. if (file_or_error.is_error()) {
  128. auto maybe_message = String::formatted("{} \"{}\" failed: {}", action, *chosen_file, file_or_error.error());
  129. if (!maybe_message.is_error())
  130. (void)GUI::MessageBox::try_show_error(request_data.parent_window, maybe_message.release_value());
  131. return (void)request_data.promise->resolve(file_or_error.release_error());
  132. }
  133. (void)request_data.promise->resolve(file_or_error.release_value());
  134. }
  135. void Client::die()
  136. {
  137. for (auto const& entry : m_promises)
  138. handle_prompt_end(entry.key, ECONNRESET, {}, "");
  139. }
  140. int Client::get_new_id()
  141. {
  142. auto const new_id = m_last_id++;
  143. // Note: This verify shouldn't fail, and we should provide a valid ID
  144. // But we probably have more issues if this test fails.
  145. VERIFY(!m_promises.contains(new_id));
  146. return new_id;
  147. }
  148. Result Client::handle_promise(int id)
  149. {
  150. auto result = TRY(m_promises.get(id)->promise->await());
  151. m_promises.remove(id);
  152. return result;
  153. }
  154. }