LibWeb: Turn FrameLoader into a ResourceClient

We now use the new resource-based loader for the main resource in each
Frame. This gives us access to caching and sharing. :^)
This commit is contained in:
Andreas Kling 2020-06-06 13:38:08 +02:00
parent 52fcaae71c
commit de6028dfa7
3 changed files with 58 additions and 43 deletions

View file

@ -178,48 +178,9 @@ bool FrameLoader::load(const URL& url)
return false;
}
ResourceLoader::the().load(
url,
[this, url](auto data, auto& response_headers) {
// FIXME: Also check HTTP status code before redirecting
auto location = response_headers.get("Location");
if (location.has_value()) {
load(location.value());
return;
}
if (data.is_null()) {
load_error_page(url, "No data");
return;
}
String encoding = "utf-8";
String mime_type;
auto content_type = response_headers.get("Content-Type");
if (content_type.has_value()) {
dbg() << "Content-Type header: _" << content_type.value() << "_";
encoding = encoding_from_content_type(content_type.value());
mime_type = mime_type_from_content_type(content_type.value());
} else {
dbg() << "No Content-Type header to go on! Guessing based on filename...";
mime_type = guess_mime_type_based_on_filename(url);
}
dbg() << "I believe this content has MIME type '" << mime_type << "', encoding '" << encoding << "'";
auto document = create_document_from_mime_type(data, url, mime_type, encoding);
ASSERT(document);
frame().set_document(document);
if (!url.fragment().is_empty())
frame().scroll_to_anchor(url.fragment());
if (frame().on_title_change)
frame().on_title_change(document->title());
},
[this, url](auto error) {
load_error_page(url, error);
});
LoadRequest request;
request.set_url(url);
set_resource(ResourceLoader::the().load_resource(Resource::Type::Generic, request));
if (frame().on_load_start)
frame().on_load_start(url);
@ -273,4 +234,50 @@ void FrameLoader::load_error_page(const URL& failed_url, const String& error)
});
}
void FrameLoader::resource_did_load()
{
auto url = resource()->url();
if (!resource()->has_encoded_data()) {
load_error_page(url, "No data");
return;
}
// FIXME: Also check HTTP status code before redirecting
auto location = resource()->response_headers().get("Location");
if (location.has_value()) {
load(location.value());
return;
}
String encoding = "utf-8";
String mime_type;
auto content_type = resource()->response_headers().get("Content-Type");
if (content_type.has_value()) {
dbg() << "Content-Type header: _" << content_type.value() << "_";
encoding = encoding_from_content_type(content_type.value());
mime_type = mime_type_from_content_type(content_type.value());
} else {
dbg() << "No Content-Type header to go on! Guessing based on filename...";
mime_type = guess_mime_type_based_on_filename(url);
}
dbg() << "I believe this content has MIME type '" << mime_type << "', encoding '" << encoding << "'";
auto document = create_document_from_mime_type(resource()->encoded_data(), url, mime_type, encoding);
ASSERT(document);
frame().set_document(document);
if (!url.fragment().is_empty())
frame().scroll_to_anchor(url.fragment());
if (frame().on_title_change)
frame().on_title_change(document->title());
}
void FrameLoader::resource_did_fail()
{
load_error_page(resource()->url(), resource()->error());
}
}

View file

@ -28,10 +28,12 @@
#include <AK/Forward.h>
#include <LibWeb/Forward.h>
#include <LibWeb/Loader/Resource.h>
namespace Web {
class FrameLoader {
class FrameLoader final
: public ResourceClient {
public:
explicit FrameLoader(Frame&);
~FrameLoader();
@ -44,6 +46,10 @@ public:
void set_use_old_parser(bool b) { m_use_old_parser = b; }
private:
// ^ResourceClient
virtual void resource_did_load() override;
virtual void resource_did_fail() override;
void load_error_page(const URL& failed_url, const String& error_message);
RefPtr<Document> create_document_from_mime_type(const ByteBuffer&, const URL&, const String& mime_type, const String& encoding);

View file

@ -67,6 +67,8 @@ public:
const URL& url() const { return m_request.url(); }
const ByteBuffer& encoded_data() const { return m_encoded_data; }
const HashMap<String, String, CaseInsensitiveStringTraits>& response_headers() const { return m_response_headers; }
void register_client(Badge<ResourceClient>, ResourceClient&);
void unregister_client(Badge<ResourceClient>, ResourceClient&);