Screenshot.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * Copyright (c) 2022, Tim Flynn <trflynn89@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Optional.h>
  7. #include <LibGfx/Bitmap.h>
  8. #include <LibGfx/Rect.h>
  9. #include <LibWeb/DOM/Document.h>
  10. #include <LibWeb/DOM/ElementFactory.h>
  11. #include <LibWeb/HTML/AnimationFrameCallbackDriver.h>
  12. #include <LibWeb/HTML/BrowsingContext.h>
  13. #include <LibWeb/HTML/HTMLCanvasElement.h>
  14. #include <LibWeb/HTML/TagNames.h>
  15. #include <LibWeb/HTML/TraversableNavigable.h>
  16. #include <LibWeb/HTML/Window.h>
  17. #include <LibWeb/Namespace.h>
  18. #include <LibWeb/Page/Page.h>
  19. #include <LibWeb/Platform/EventLoopPlugin.h>
  20. #include <LibWeb/WebDriver/Error.h>
  21. #include <LibWeb/WebDriver/Screenshot.h>
  22. namespace Web::WebDriver {
  23. // https://w3c.github.io/webdriver/#dfn-encoding-a-canvas-as-base64
  24. static Response encode_canvas_element(HTML::HTMLCanvasElement& canvas)
  25. {
  26. // FIXME: 1. If the canvas element’s bitmap’s origin-clean flag is set to false, return error with error code unable to capture screen.
  27. // 2. If the canvas element’s bitmap has no pixels (i.e. either its horizontal dimension or vertical dimension is zero) then return error with error code unable to capture screen.
  28. if (canvas.bitmap()->width() == 0 || canvas.bitmap()->height() == 0)
  29. return Error::from_code(ErrorCode::UnableToCaptureScreen, "Captured screenshot is empty"sv);
  30. // 3. Let file be a serialization of the canvas element’s bitmap as a file, using "image/png" as an argument.
  31. // 4. Let data url be a data: URL representing file. [RFC2397]
  32. auto data_url = canvas.to_data_url("image/png"sv, {});
  33. // 5. Let index be the index of "," in data url.
  34. auto index = data_url.find_byte_offset(',');
  35. VERIFY(index.has_value());
  36. // 6. Let encoded string be a substring of data url using (index + 1) as the start argument.
  37. auto encoded_string = MUST(data_url.substring_from_byte_offset(*index + 1));
  38. // 7. Return success with data encoded string.
  39. return JsonValue { move(encoded_string) };
  40. }
  41. // Common animation callback steps between:
  42. // https://w3c.github.io/webdriver/#take-screenshot
  43. // https://w3c.github.io/webdriver/#take-element-screenshot
  44. Response capture_element_screenshot(Painter const& painter, Page& page, DOM::Element& element, Gfx::IntRect& rect)
  45. {
  46. Optional<Response> encoded_string_or_error;
  47. element.document().window()->animation_frame_callback_driver().add([&](auto) {
  48. auto viewport_rect = page.top_level_traversable()->viewport_rect();
  49. rect.intersect(page.enclosing_device_rect(viewport_rect).to_type<int>());
  50. auto canvas_element = DOM::create_element(element.document(), HTML::TagNames::canvas, Namespace::HTML).release_value_but_fixme_should_propagate_errors();
  51. auto& canvas = verify_cast<HTML::HTMLCanvasElement>(*canvas_element);
  52. // FIXME: Handle DevicePixelRatio in HiDPI mode.
  53. MUST(canvas.set_width(rect.width()));
  54. MUST(canvas.set_height(rect.height()));
  55. if (!canvas.create_bitmap(rect.width(), rect.height())) {
  56. encoded_string_or_error = Error::from_code(ErrorCode::UnableToCaptureScreen, "Unable to create a screenshot bitmap"sv);
  57. return;
  58. }
  59. painter(rect, *canvas.bitmap());
  60. encoded_string_or_error = encode_canvas_element(canvas);
  61. });
  62. Platform::EventLoopPlugin::the().spin_until([&]() { return encoded_string_or_error.has_value(); });
  63. return encoded_string_or_error.release_value();
  64. }
  65. }