Session.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * Copyright (c) 2022, Florent Castelli <florent.castelli@gmail.com>
  3. * Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
  4. * Copyright (c) 2022, Tobias Christiansen <tobyase@serenityos.org>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include "Session.h"
  9. #include "BrowserConnection.h"
  10. #include "Client.h"
  11. #include <LibCore/LocalServer.h>
  12. #include <LibCore/Stream.h>
  13. #include <LibCore/System.h>
  14. #include <LibWeb/Cookie/Cookie.h>
  15. #include <unistd.h>
  16. namespace WebDriver {
  17. Session::Session(unsigned session_id, NonnullRefPtr<Client> client)
  18. : m_client(move(client))
  19. , m_id(session_id)
  20. {
  21. }
  22. Session::~Session()
  23. {
  24. if (m_started) {
  25. auto error = stop();
  26. if (error.is_error()) {
  27. warnln("Failed to stop session {}: {}", m_id, error.error());
  28. }
  29. }
  30. }
  31. ErrorOr<void> Session::start()
  32. {
  33. auto socket_path = String::formatted("/tmp/browser_webdriver_{}_{}", getpid(), m_id);
  34. dbgln("Listening for WebDriver connection on {}", socket_path);
  35. // FIXME: Use Core::LocalServer
  36. struct sockaddr_un addr;
  37. int listen_socket = TRY(Core::System::socket(AF_UNIX, SOCK_STREAM, 0));
  38. ::memset(&addr, 0, sizeof(struct sockaddr_un));
  39. addr.sun_family = AF_UNIX;
  40. ::strncpy(addr.sun_path, socket_path.characters(), sizeof(addr.sun_path) - 1);
  41. TRY(Core::System::bind(listen_socket, (const struct sockaddr*)&addr, sizeof(struct sockaddr_un)));
  42. TRY(Core::System::listen(listen_socket, 1));
  43. char const* argv[] = { "/bin/Browser", "--webdriver", socket_path.characters(), nullptr };
  44. TRY(Core::System::posix_spawn("/bin/Browser"sv, nullptr, nullptr, const_cast<char**>(argv), environ));
  45. int data_socket = TRY(Core::System::accept(listen_socket, nullptr, nullptr));
  46. auto socket = TRY(Core::Stream::LocalSocket::adopt_fd(data_socket));
  47. TRY(socket->set_blocking(true));
  48. m_browser_connection = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) BrowserConnection(move(socket), m_client, session_id())));
  49. dbgln("Browser is connected");
  50. m_started = true;
  51. m_windows.set("main", make<Session::Window>("main", true));
  52. m_current_window_handle = "main";
  53. return {};
  54. }
  55. ErrorOr<void> Session::stop()
  56. {
  57. m_browser_connection->async_quit();
  58. return {};
  59. }
  60. // DELETE /session/{session id}/window https://w3c.github.io/webdriver/#dfn-close-window
  61. ErrorOr<void, Variant<HttpError, Error>> Session::delete_window()
  62. {
  63. // 1. If the current top-level browsing context is no longer open, return error with error code no such window.
  64. auto current_window = get_window_object();
  65. if (!current_window.has_value())
  66. return Variant<HttpError, Error>(HttpError { 404, "no such window", "Window not found" });
  67. // 2. Close the current top-level browsing context.
  68. m_windows.remove(m_current_window_handle);
  69. // 3. If there are no more open top-level browsing contexts, then close the session.
  70. if (m_windows.is_empty()) {
  71. auto result = stop();
  72. if (result.is_error()) {
  73. return Variant<HttpError, Error>(result.release_error());
  74. }
  75. }
  76. return {};
  77. }
  78. // POST /session/{session id}/url https://w3c.github.io/webdriver/#dfn-navigate-to
  79. ErrorOr<JsonValue, HttpError> Session::post_url(JsonValue const& payload)
  80. {
  81. // 1. If the current top-level browsing context is no longer open, return error with error code no such window.
  82. auto current_window = get_window_object();
  83. if (!current_window.has_value())
  84. return HttpError { 404, "no such window", "Window not found" };
  85. // FIXME 2. Handle any user prompts and return its value if it is an error.
  86. // 3. If the url property is missing from the parameters argument or it is not a string, return error with error code invalid argument.
  87. if (!payload.is_object() || !payload.as_object().has_string("url"sv)) {
  88. return HttpError { 400, "invalid argument", "Payload doesn't have a string url" };
  89. }
  90. // 4. Let url be the result of getting a property named url from the parameters argument.
  91. URL url(payload.as_object().get_ptr("url"sv)->as_string());
  92. // FIXME: 5. If url is not an absolute URL or an absolute URL with fragment, return error with error code invalid argument. [URL]
  93. // 6. Let url be the result of getting a property named url from the parameters argument.
  94. // Duplicate step?
  95. // 7. Navigate the current top-level browsing context to url.
  96. m_browser_connection->async_set_url(url);
  97. // FIXME: 8. Run the post-navigation checks and return its value if it is an error.
  98. // FIXME: 9. Wait for navigation to complete and return its value if it is an error.
  99. // FIXME: 10. Set the current browsing context to the current top-level browsing context.
  100. // 11. Return success with data null.
  101. return JsonValue();
  102. }
  103. // GET /session/{session id}/url https://w3c.github.io/webdriver/#dfn-get-current-url
  104. ErrorOr<JsonValue, HttpError> Session::get_url()
  105. {
  106. // 1. If the current top-level browsing context is no longer open, return error with error code no such window.
  107. auto current_window = get_window_object();
  108. if (!current_window.has_value())
  109. return HttpError { 404, "no such window", "Window not found" };
  110. // FIXME: 2. Handle any user prompts and return its value if it is an error.
  111. // 3. Let url be the serialization of the current top-level browsing context’s active document’s document URL.
  112. auto url = m_browser_connection->get_url().to_string();
  113. // 4. Return success with data url.
  114. return JsonValue(url);
  115. }
  116. // GET /session/{session id}/title https://w3c.github.io/webdriver/#dfn-get-title
  117. ErrorOr<JsonValue, HttpError> Session::get_title()
  118. {
  119. // 1. If the current top-level browsing context is no longer open, return error with error code no such window.
  120. auto current_window = get_window_object();
  121. if (!current_window.has_value())
  122. return HttpError { 404, "no such window", "Window not found" };
  123. // FIXME: 2. Handle any user prompts and return its value if it is an error.
  124. // 3. Let title be the initial value of the title IDL attribute of the current top-level browsing context's active document.
  125. // 4. Return success with data title.
  126. return JsonValue(m_browser_connection->get_title());
  127. }
  128. // POST /session/{session id}/refresh https://w3c.github.io/webdriver/#dfn-refresh
  129. ErrorOr<JsonValue, HttpError> Session::refresh()
  130. {
  131. // 1. If the current top-level browsing context is no longer open, return error with error code no such window.
  132. auto current_window = get_window_object();
  133. if (!current_window.has_value())
  134. return HttpError { 404, "no such window", "Window not found" };
  135. // FIXME: 2. Handle any user prompts and return its value if it is an error.
  136. // 3. Initiate an overridden reload of the current top-level browsing context’s active document.
  137. m_browser_connection->async_refresh();
  138. // FIXME: 4. If url is special except for file:
  139. // FIXME: 1. Try to wait for navigation to complete.
  140. // FIXME: 2. Try to run the post-navigation checks.
  141. // FIXME: 5. Set the current browsing context with current top-level browsing context.
  142. // 6. Return success with data null.
  143. return JsonValue();
  144. }
  145. // POST /session/{session id}/back https://w3c.github.io/webdriver/#dfn-back
  146. ErrorOr<JsonValue, HttpError> Session::back()
  147. {
  148. // 1. If the current top-level browsing context is no longer open, return error with error code no such window.
  149. auto current_window = get_window_object();
  150. if (!current_window.has_value())
  151. return HttpError { 404, "no such window", "Window not found" };
  152. // FIXME: 2. Handle any user prompts and return its value if it is an error.
  153. // 3. Traverse the history by a delta –1 for the current browsing context.
  154. m_browser_connection->async_back();
  155. // FIXME: 4. If the previous step completed results in a pageHide event firing, wait until pageShow event
  156. // fires or for the session page load timeout milliseconds to pass, whichever occurs sooner.
  157. // FIXME: 5. If the previous step completed by the session page load timeout being reached, and user
  158. // prompts have been handled, return error with error code timeout.
  159. // 6. Return success with data null.
  160. return JsonValue();
  161. }
  162. // POST /session/{session id}/forward https://w3c.github.io/webdriver/#dfn-forward
  163. ErrorOr<JsonValue, HttpError> Session::forward()
  164. {
  165. // 1. If the current top-level browsing context is no longer open, return error with error code no such window.
  166. auto current_window = get_window_object();
  167. if (!current_window.has_value())
  168. return HttpError { 404, "no such window", "Window not found" };
  169. // FIXME: 2. Handle any user prompts and return its value if it is an error.
  170. // 3. Traverse the history by a delta 1 for the current browsing context.
  171. m_browser_connection->async_forward();
  172. // FIXME: 4. If the previous step completed results in a pageHide event firing, wait until pageShow event
  173. // fires or for the session page load timeout milliseconds to pass, whichever occurs sooner.
  174. // FIXME: 5. If the previous step completed by the session page load timeout being reached, and user
  175. // prompts have been handled, return error with error code timeout.
  176. // 6. Return success with data null.
  177. return JsonValue();
  178. }
  179. // https://w3c.github.io/webdriver/#dfn-serialized-cookie
  180. static JsonObject serialize_cookie(Web::Cookie::Cookie const& cookie)
  181. {
  182. JsonObject serialized_cookie = {};
  183. serialized_cookie.set("name", cookie.name);
  184. serialized_cookie.set("value", cookie.value);
  185. serialized_cookie.set("path", cookie.path);
  186. serialized_cookie.set("domain", cookie.domain);
  187. serialized_cookie.set("secure", cookie.secure);
  188. serialized_cookie.set("httpOnly", cookie.http_only);
  189. serialized_cookie.set("expiry", cookie.expiry_time.timestamp());
  190. // FIXME: Add sameSite to Cookie and serialize it here too.
  191. return serialized_cookie;
  192. }
  193. // GET /session/{session id}/cookie https://w3c.github.io/webdriver/#dfn-get-all-cookies
  194. ErrorOr<JsonValue, HttpError> Session::get_all_cookies()
  195. {
  196. // 1. If the current browsing context is no longer open, return error with error code no such window.
  197. auto current_window = get_window_object();
  198. if (!current_window.has_value())
  199. return HttpError { 404, "no such window", "Window not found" };
  200. // FIXME: 2. Handle any user prompts, and return its value if it is an error.
  201. // 3. Let cookies be a new JSON List.
  202. JsonArray cookies = {};
  203. // 4. For each cookie in all associated cookies of the current browsing context’s active document:
  204. for (auto const& cookie : m_browser_connection->get_all_cookies()) {
  205. // 1. Let serialized cookie be the result of serializing cookie.
  206. auto serialized_cookie = serialize_cookie(cookie);
  207. // 2. Append serialized cookie to cookies
  208. cookies.append(serialized_cookie);
  209. }
  210. // 5. Return success with data cookies.
  211. return JsonValue(cookies);
  212. }
  213. }