Truly independent web browser
/*
* Copyright (c) 2023, Andreas Kling <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/HashTable.h>
#include <LibGC/Heap.h>
#include <LibGfx/Bitmap.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/DOMURL/DOMURL.h>
#include <LibWeb/Fetch/Fetching/Fetching.h>
#include <LibWeb/Fetch/Infrastructure/FetchAlgorithms.h>
#include <LibWeb/Fetch/Infrastructure/FetchController.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Responses.h>
#include <LibWeb/HTML/BitmapDecodedImageData.h>
#include <LibWeb/HTML/DecodedImageData.h>
#include <LibWeb/HTML/ImageRequest.h>
#include <LibWeb/HTML/ListOfAvailableImages.h>
#include <LibWeb/HTML/SharedResourceRequest.h>
#include <LibWeb/Platform/ImageCodecPlugin.h>
#include <LibWeb/SVG/SVGDecodedImageData.h>
namespace Web::HTML {
GC_DEFINE_ALLOCATOR(ImageRequest);
GC::Ref<ImageRequest> ImageRequest::create()
{
return GC::Heap::the().allocate<ImageRequest>();
}
ImageRequest::ImageRequest() = default;
ImageRequest::~ImageRequest()
{
}
void ImageRequest::visit_edges(JS::Cell::Visitor& visitor)
{
Base::visit_edges(visitor);
visitor.visit(m_shared_resource_request);
visitor.visit(m_image_data);
}
// https://html.spec.whatwg.org/multipage/images.html#img-available
bool ImageRequest::is_available() const
{
// When an image request's state is either partially available or completely available, the image request is said to be available.
return m_state == State::PartiallyAvailable || m_state == State::CompletelyAvailable;
}
bool ImageRequest::is_fetching() const
{
return m_shared_resource_request && m_shared_resource_request->is_fetching();
}
ImageRequest::State ImageRequest::state() const
{
return m_state;
}
void ImageRequest::set_state(State state)
{
m_state = state;
}
void ImageRequest::set_current_url(DOM::Document& document, String url, CORSSettingAttribute cors_setting)
{
m_current_url = Utf16String::from_utf8(url);
if (auto parsed_url = DOMURL::parse(m_current_url); parsed_url.has_value()) {
auto shared_resource_request = SharedResourceRequest::get_or_create(document, parsed_url.release_value(), cors_setting);
if (shared_resource_request != m_shared_resource_request)
stop_waiting_for_fetch();
m_shared_resource_request = shared_resource_request;
}
}
void ImageRequest::set_current_url(DOM::Document& document, Utf16String url, CORSSettingAttribute cors_setting)
{
set_current_url(document, url.to_utf8(), cors_setting);
}
void ImageRequest::set_current_url(Utf16String url, GC::Ref<SharedResourceRequest> shared_resource_request)
{
m_current_url = move(url);
stop_waiting_for_fetch();
m_shared_resource_request = shared_resource_request;
}
// https://html.spec.whatwg.org/multipage/images.html#abort-the-image-request
void abort_the_image_request(GC::Ptr<ImageRequest> image_request)
{
// 1. If image request is null, then return.
if (!image_request)
return;
// 2. Forget image request's image data, if any.
image_request->set_image_data(nullptr);
// 3. Abort any instance of the fetching algorithm for image request,
// discarding any pending tasks generated by that algorithm.
// AD-HOC: We don't abort the fetch itself, since our SharedResourceRequest may be used by someone else.
// Instead, mark the image request as aborted so that its fetch callbacks discard their work.
image_request->set_was_aborted();
}
GC::Ptr<DecodedImageData> ImageRequest::image_data() const
{
return m_image_data;
}
void ImageRequest::set_image_data(GC::Ptr<DecodedImageData> data)
{
m_image_data = data;
}
// https://html.spec.whatwg.org/multipage/images.html#prepare-an-image-for-presentation
void ImageRequest::prepare_for_presentation(HTMLImageElement&)
{
// FIXME: 1. Let exifTagMap be the EXIF tags obtained from req's image data, as defined by the relevant codec. [EXIF]
// FIXME: 2. Let physicalWidth and physicalHeight be the width and height obtained from req's image data, as defined by the relevant codec.
// FIXME: 3. Let dimX be the value of exifTagMap's tag 0xA002 (PixelXDimension).
// FIXME: 4. Let dimY be the value of exifTagMap's tag 0xA003 (PixelYDimension).
// FIXME: 5. Let resX be the value of exifTagMap's tag 0x011A (XResolution).
// FIXME: 6. Let resY be the value of exifTagMap's tag 0x011B (YResolution).
// FIXME: 7. Let resUnit be the value of exifTagMap's tag 0x0128 (ResolutionUnit).
// FIXME: 8. If all the following are true:
// ...
// FIXME: 9. Update req's img element's presentation appropriately.
}
void ImageRequest::fetch_image(NonnullRefPtr<Fetch::Infrastructure::Request> request)
{
VERIFY(m_shared_resource_request);
if (m_shared_resource_request->needs_fetching())
m_shared_resource_request->fetch_resource(request);
}
void ImageRequest::add_callbacks(GC::Ptr<GC::Function<void()>> on_finish, GC::Ptr<GC::Function<void()>> on_fail, GC::Ptr<GC::Function<void()>> on_stop, SharedResourceRequest::DelaysLoadEvent delays_load_event)
{
VERIFY(m_shared_resource_request);
if (auto id = m_shared_resource_request->add_removable_callbacks(on_finish, on_fail, on_stop, delays_load_event); id.has_value())
m_shared_resource_request_callbacks_ids.append(*id);
}
void ImageRequest::add_callbacks_without_delaying_the_load_event(GC::Ptr<GC::Function<void()>> on_finish, GC::Ptr<GC::Function<void()>> on_fail)
{
VERIFY(m_shared_resource_request);
(void)m_shared_resource_request->add_removable_callbacks(on_finish, on_fail, {}, SharedResourceRequest::DelaysLoadEvent::No);
}
void ImageRequest::stop_waiting_for_fetch()
{
if (m_shared_resource_request) {
for (auto id : m_shared_resource_request_callbacks_ids)
m_shared_resource_request->remove_callbacks(id);
}
m_shared_resource_request_callbacks_ids.clear();
}
}