Client.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 <LibCore/File.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. DeprecatedResult Client::try_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, PromiseAndWindow { { Core::Promise<DeprecatedResult>::construct() }, parent_window });
  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(Core::File::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<DeprecatedResult>(id);
  39. }
  40. static Core::Stream::OpenMode to_stream_open_mode(Core::OpenMode open_mode)
  41. {
  42. Core::Stream::OpenMode result {};
  43. if ((open_mode & Core::OpenMode::ReadOnly) == Core::OpenMode::ReadOnly)
  44. result |= Core::Stream::OpenMode::Read;
  45. if ((open_mode & Core::OpenMode::WriteOnly) == Core::OpenMode::WriteOnly)
  46. result |= Core::Stream::OpenMode::Write;
  47. if ((open_mode & Core::OpenMode::ReadWrite) == Core::OpenMode::ReadWrite)
  48. result |= Core::Stream::OpenMode::ReadWrite;
  49. if ((open_mode & Core::OpenMode::Append) == Core::OpenMode::Append)
  50. result |= Core::Stream::OpenMode::Append;
  51. if ((open_mode & Core::OpenMode::Truncate) == Core::OpenMode::Truncate)
  52. result |= Core::Stream::OpenMode::Truncate;
  53. if ((open_mode & Core::OpenMode::MustBeNew) == Core::OpenMode::MustBeNew)
  54. result |= Core::Stream::OpenMode::MustBeNew;
  55. if ((open_mode & Core::OpenMode::KeepOnExec) == Core::OpenMode::KeepOnExec)
  56. result |= Core::Stream::OpenMode::KeepOnExec;
  57. return result;
  58. }
  59. DeprecatedResult Client::try_request_file(GUI::Window* parent_window, DeprecatedString const& path, Core::OpenMode deprecated_mode)
  60. {
  61. auto const id = get_new_id();
  62. m_promises.set(id, PromiseAndWindow { { Core::Promise<DeprecatedResult>::construct() }, parent_window });
  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. auto const mode = to_stream_open_mode(deprecated_mode);
  71. if (path.starts_with('/')) {
  72. async_request_file(id, parent_window_server_client_id, parent_window_id, path, mode);
  73. } else {
  74. auto full_path = LexicalPath::join(Core::File::current_working_directory(), path).string();
  75. async_request_file(id, parent_window_server_client_id, parent_window_id, full_path, mode);
  76. }
  77. return handle_promise<DeprecatedResult>(id);
  78. }
  79. DeprecatedResult Client::try_open_file(GUI::Window* parent_window, DeprecatedString const& window_title, StringView path, Core::OpenMode deprecated_requested_access)
  80. {
  81. auto const id = get_new_id();
  82. m_promises.set(id, PromiseAndWindow { { Core::Promise<DeprecatedResult>::construct() }, parent_window });
  83. auto parent_window_server_client_id = GUI::ConnectionToWindowServer::the().expose_client_id();
  84. auto child_window_server_client_id = expose_window_server_client_id();
  85. auto parent_window_id = parent_window->window_id();
  86. GUI::ConnectionToWindowServer::the().add_window_stealing_for_client(child_window_server_client_id, parent_window_id);
  87. ScopeGuard guard([parent_window_id, child_window_server_client_id] {
  88. GUI::ConnectionToWindowServer::the().remove_window_stealing_for_client(child_window_server_client_id, parent_window_id);
  89. });
  90. auto const requested_access = to_stream_open_mode(deprecated_requested_access);
  91. async_prompt_open_file(id, parent_window_server_client_id, parent_window_id, window_title, path, requested_access);
  92. return handle_promise<DeprecatedResult>(id);
  93. }
  94. DeprecatedResult Client::try_save_file_deprecated(GUI::Window* parent_window, DeprecatedString const& name, DeprecatedString const ext, Core::OpenMode deprecated_requested_access)
  95. {
  96. auto const id = get_new_id();
  97. m_promises.set(id, PromiseAndWindow { { Core::Promise<DeprecatedResult>::construct() }, parent_window });
  98. auto parent_window_server_client_id = GUI::ConnectionToWindowServer::the().expose_client_id();
  99. auto child_window_server_client_id = expose_window_server_client_id();
  100. auto parent_window_id = parent_window->window_id();
  101. GUI::ConnectionToWindowServer::the().add_window_stealing_for_client(child_window_server_client_id, parent_window_id);
  102. ScopeGuard guard([parent_window_id, child_window_server_client_id] {
  103. GUI::ConnectionToWindowServer::the().remove_window_stealing_for_client(child_window_server_client_id, parent_window_id);
  104. });
  105. auto const requested_access = to_stream_open_mode(deprecated_requested_access);
  106. 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);
  107. return handle_promise<DeprecatedResult>(id);
  108. }
  109. Result Client::save_file(GUI::Window* parent_window, DeprecatedString const& name, DeprecatedString const ext, Core::Stream::OpenMode requested_access)
  110. {
  111. auto const id = get_new_id();
  112. m_promises.set(id, PromiseAndWindow { { Core::Promise<Result>::construct() }, parent_window });
  113. auto parent_window_server_client_id = GUI::ConnectionToWindowServer::the().expose_client_id();
  114. auto child_window_server_client_id = expose_window_server_client_id();
  115. auto parent_window_id = parent_window->window_id();
  116. GUI::ConnectionToWindowServer::the().add_window_stealing_for_client(child_window_server_client_id, parent_window_id);
  117. ScopeGuard guard([parent_window_id, child_window_server_client_id] {
  118. GUI::ConnectionToWindowServer::the().remove_window_stealing_for_client(child_window_server_client_id, parent_window_id);
  119. });
  120. 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);
  121. return handle_promise<Result>(id);
  122. }
  123. void Client::handle_prompt_end(i32 request_id, i32 error, Optional<IPC::File> const& ipc_file, Optional<DeprecatedString> const& chosen_file)
  124. {
  125. auto potential_data = m_promises.get(request_id);
  126. VERIFY(potential_data.has_value());
  127. auto& request_data = potential_data.value();
  128. auto const resolve_any_promise = [&promise = request_data.promise](Error&& error) {
  129. if (promise.has<PromiseType<DeprecatedResult>>()) {
  130. promise.get<PromiseType<DeprecatedResult>>()->resolve(move(error));
  131. return;
  132. }
  133. promise.get<PromiseType<Result>>()->resolve(move(error));
  134. };
  135. if (error != 0) {
  136. // We don't want to show an error message for non-existent files since some applications may want
  137. // to handle it as opening a new, named file.
  138. if (error != -1 && error != ENOENT)
  139. GUI::MessageBox::show_error(request_data.parent_window, DeprecatedString::formatted("Opening \"{}\" failed: {}", *chosen_file, strerror(error)));
  140. resolve_any_promise(Error::from_errno(error));
  141. return;
  142. }
  143. if (Core::File::is_device(ipc_file->fd())) {
  144. GUI::MessageBox::show_error(request_data.parent_window, DeprecatedString::formatted("Opening \"{}\" failed: Cannot open device files", *chosen_file));
  145. resolve_any_promise(Error::from_string_literal("Cannot open device files"));
  146. return;
  147. }
  148. if (Core::File::is_directory(ipc_file->fd())) {
  149. GUI::MessageBox::show_error(request_data.parent_window, DeprecatedString::formatted("Opening \"{}\" failed: Cannot open directory", *chosen_file));
  150. resolve_any_promise(Error::from_errno(EISDIR));
  151. return;
  152. }
  153. if (request_data.promise.has<PromiseType<DeprecatedResult>>()) {
  154. auto file = Core::File::construct();
  155. auto fd = ipc_file->take_fd();
  156. file->open(fd, Core::OpenMode::ReadWrite, Core::File::ShouldCloseFileDescriptor::Yes);
  157. file->set_filename(*chosen_file);
  158. request_data.promise.get<PromiseType<DeprecatedResult>>()->resolve(file);
  159. return;
  160. }
  161. auto file_or_error = Core::Stream::File::adopt_fd(ipc_file->take_fd(), Core::Stream::OpenMode::ReadWrite);
  162. if (file_or_error.is_error()) {
  163. resolve_any_promise(file_or_error.release_error());
  164. }
  165. request_data.promise.get<PromiseType<Result>>()->resolve(file_or_error.release_value());
  166. }
  167. void Client::die()
  168. {
  169. for (auto const& entry : m_promises)
  170. handle_prompt_end(entry.key, ECONNRESET, {}, "");
  171. }
  172. int Client::get_new_id()
  173. {
  174. auto const new_id = m_last_id++;
  175. // Note: This verify shouldn't fail, and we should provide a valid ID
  176. // But we probably have more issues if this test fails.
  177. VERIFY(!m_promises.contains(new_id));
  178. return new_id;
  179. }
  180. template<typename AnyResult>
  181. AnyResult Client::handle_promise(int id)
  182. {
  183. auto result = m_promises.get(id)->promise.get<PromiseType<AnyResult>>()->await();
  184. m_promises.remove(id);
  185. return result;
  186. }
  187. }