TimeoutsConfiguration.cpp 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/JsonObject.h>
  7. #include <WebDriver/HttpError.h>
  8. #include <WebDriver/TimeoutsConfiguration.h>
  9. namespace WebDriver {
  10. // https://w3c.github.io/webdriver/#dfn-timeouts-object
  11. JsonObject timeouts_object(TimeoutsConfiguration const& timeouts)
  12. {
  13. // The timeouts object for a timeouts configuration timeouts is an object initialized with the following properties:
  14. auto timeouts_object = JsonObject {};
  15. // "script"
  16. // timeouts' script timeout value, if set, or its default value.
  17. if (timeouts.script_timeout.has_value())
  18. timeouts_object.set("script", *timeouts.script_timeout);
  19. else
  20. timeouts_object.set("script", JsonValue {});
  21. // "pageLoad"
  22. // timeouts' page load timeout’s value, if set, or its default value.
  23. timeouts_object.set("pageLoad", timeouts.page_load_timeout);
  24. // "implicit"
  25. // timeouts' implicit wait timeout’s value, if set, or its default value.
  26. timeouts_object.set("implicit", timeouts.implicit_wait_timeout);
  27. return timeouts_object;
  28. }
  29. // https://w3c.github.io/webdriver/#ref-for-dfn-json-deserialize-3
  30. ErrorOr<TimeoutsConfiguration, HttpError> json_deserialize_as_a_timeouts_configuration(JsonValue const& value)
  31. {
  32. constexpr i64 max_safe_integer = 9007199254740991;
  33. // 1. Let timeouts be a new timeouts configuration.
  34. auto timeouts = TimeoutsConfiguration {};
  35. // 2. If value is not a JSON Object, return error with error code invalid argument.
  36. if (!value.is_object())
  37. return HttpError { 400, "invalid argument", "Payload is not a JSON object" };
  38. // 3. If value has a property with the key "script":
  39. if (value.as_object().has("script"sv)) {
  40. // 1. Let script duration be the value of property "script".
  41. auto const& script_duration = value.as_object().get("script"sv);
  42. // 2. If script duration is a number and less than 0 or greater than maximum safe integer, or it is not null, return error with error code invalid argument.
  43. if ((script_duration.is_number() && (script_duration.to_i64() < 0 || script_duration.to_i64() > max_safe_integer)) || !script_duration.is_null())
  44. return HttpError { 400, "invalid argument", "Invalid script duration" };
  45. // 3. Set timeouts’s script timeout to script duration.
  46. timeouts.script_timeout = script_duration.is_null() ? Optional<u64> {} : script_duration.to_u64();
  47. }
  48. // 4. If value has a property with the key "pageLoad":
  49. if (value.as_object().has("pageLoad"sv)) {
  50. // 1. Let page load duration be the value of property "pageLoad".
  51. auto const& page_load_duration = value.as_object().get("pageLoad"sv);
  52. // 2. If page load duration is less than 0 or greater than maximum safe integer, return error with error code invalid argument.
  53. if (!page_load_duration.is_number() || page_load_duration.to_i64() < 0 || page_load_duration.to_i64() > max_safe_integer)
  54. return HttpError { 400, "invalid argument", "Invalid page load duration" };
  55. // 3. Set timeouts’s page load timeout to page load duration.
  56. timeouts.page_load_timeout = page_load_duration.to_u64();
  57. }
  58. // 5. If value has a property with the key "implicit":
  59. if (value.as_object().has("implicit"sv)) {
  60. // 1. Let implicit duration be the value of property "implicit".
  61. auto const& implicit_duration = value.as_object().get("implicit"sv);
  62. // 2. If implicit duration is less than 0 or greater than maximum safe integer, return error with error code invalid argument.
  63. if (!implicit_duration.is_number() || implicit_duration.to_i64() < 0 || implicit_duration.to_i64() > max_safe_integer)
  64. return HttpError { 400, "invalid argument", "Invalid implicit duration" };
  65. // 3. Set timeouts’s implicit wait timeout to implicit duration.
  66. timeouts.implicit_wait_timeout = implicit_duration.to_u64();
  67. }
  68. // 6. Return success with data timeouts.
  69. return timeouts;
  70. }
  71. }