|
@@ -10,11 +10,14 @@
|
|
|
#include "Session.h"
|
|
|
#include "BrowserConnection.h"
|
|
|
#include "Client.h"
|
|
|
+#include <AK/Base64.h>
|
|
|
#include <AK/NumericLimits.h>
|
|
|
#include <AK/Time.h>
|
|
|
+#include <AK/URL.h>
|
|
|
#include <LibCore/LocalServer.h>
|
|
|
#include <LibCore/Stream.h>
|
|
|
#include <LibCore/System.h>
|
|
|
+#include <LibGfx/PNGWriter.h>
|
|
|
#include <LibGfx/Point.h>
|
|
|
#include <LibGfx/Rect.h>
|
|
|
#include <LibGfx/Size.h>
|
|
@@ -1123,4 +1126,52 @@ ErrorOr<JsonValue, WebDriverError> Session::delete_all_cookies()
|
|
|
return JsonValue();
|
|
|
}
|
|
|
|
|
|
+// https://w3c.github.io/webdriver/#dfn-encoding-a-canvas-as-base64
|
|
|
+static ErrorOr<String, WebDriverError> encode_bitmap_as_canvas_element(Gfx::Bitmap const& bitmap)
|
|
|
+{
|
|
|
+ // 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.
|
|
|
+
|
|
|
+ // 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.
|
|
|
+ if (bitmap.width() == 0 || bitmap.height() == 0)
|
|
|
+ return WebDriverError::from_code(ErrorCode::UnableToCaptureScreen, "Captured screenshot is empty"sv);
|
|
|
+
|
|
|
+ // 3. Let file be a serialization of the canvas element’s bitmap as a file, using "image/png" as an argument.
|
|
|
+ auto file = Gfx::PNGWriter::encode(bitmap);
|
|
|
+
|
|
|
+ // 4. Let data url be a data: URL representing file. [RFC2397]
|
|
|
+ auto data_url = AK::URL::create_with_data("image/png"sv, encode_base64(file), true).to_string();
|
|
|
+
|
|
|
+ // 5. Let index be the index of "," in data url.
|
|
|
+ auto index = data_url.find(',');
|
|
|
+ VERIFY(index.has_value());
|
|
|
+
|
|
|
+ // 6. Let encoded string be a substring of data url using (index + 1) as the start argument.
|
|
|
+ auto encoded_string = data_url.substring(*index + 1);
|
|
|
+
|
|
|
+ // 7. Return success with data encoded string.
|
|
|
+ return encoded_string;
|
|
|
+}
|
|
|
+
|
|
|
+// 17.1 Take Screenshot, https://w3c.github.io/webdriver/#take-screenshot
|
|
|
+ErrorOr<JsonValue, WebDriverError> Session::take_screenshot()
|
|
|
+{
|
|
|
+ // 1. If the current top-level browsing context is no longer open, return error with error code no such window.
|
|
|
+ TRY(check_for_open_top_level_browsing_context_or_return_error());
|
|
|
+
|
|
|
+ // 2. When the user agent is next to run the animation frame callbacks:
|
|
|
+ // a. Let root rect be the current top-level browsing context’s document element’s rectangle.
|
|
|
+ // b. Let screenshot result be the result of trying to call draw a bounding box from the framebuffer, given root rect as an argument.
|
|
|
+ auto screenshot = m_browser_connection->take_screenshot();
|
|
|
+ if (!screenshot.is_valid())
|
|
|
+ return WebDriverError::from_code(ErrorCode::UnableToCaptureScreen, "Unable to capture screenshot"sv);
|
|
|
+
|
|
|
+ // c. Let canvas be a canvas element of screenshot result’s data.
|
|
|
+ // d. Let encoding result be the result of trying encoding a canvas as Base64 canvas.
|
|
|
+ // e. Let encoded string be encoding result’s data.
|
|
|
+ auto encoded_string = TRY(encode_bitmap_as_canvas_element(*screenshot.bitmap()));
|
|
|
+
|
|
|
+ // 3. Return success with data encoded string.
|
|
|
+ return encoded_string;
|
|
|
+}
|
|
|
+
|
|
|
}
|