From 2be184f49c891dd0d57643448ad519b6f242084f Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Fri, 24 Apr 2020 22:26:53 +0200 Subject: [PATCH] LibWeb: Try fetching a favicon when loading a non-file URL in HtmlView If valid and decodable, the favicon will be provided to clients via the on_favicon_change hook. :^) --- Libraries/LibWeb/HtmlView.cpp | 24 ++++++++++++++++++++++++ Libraries/LibWeb/HtmlView.h | 1 + 2 files changed, 25 insertions(+) diff --git a/Libraries/LibWeb/HtmlView.cpp b/Libraries/LibWeb/HtmlView.cpp index 8036d136bd..fbae7946de 100644 --- a/Libraries/LibWeb/HtmlView.cpp +++ b/Libraries/LibWeb/HtmlView.cpp @@ -379,6 +379,30 @@ void HtmlView::load(const URL& url) [this, url](auto error) { load_error_page(url, error); }); + + if (url.protocol() != "file") { + URL favicon_url; + favicon_url.set_protocol(url.protocol()); + favicon_url.set_host(url.host()); + favicon_url.set_port(url.port()); + favicon_url.set_path("/favicon.ico"); + + ResourceLoader::the().load( + favicon_url, + [this, favicon_url](auto data) { + dbg() << "Favicon downloaded, " << data.size() << " bytes from " << favicon_url.to_string(); + auto decoder = Gfx::ImageDecoder::create(data.data(), data.size()); + auto bitmap = decoder->bitmap(); + if (!bitmap) { + dbg() << "Could not decode favicon " << favicon_url.to_string(); + return; + } + dbg() << "Decoded favicon, " << bitmap->size(); + if (on_favicon_change) + on_favicon_change(*bitmap); + }); + } + this->scroll_to_top(); } diff --git a/Libraries/LibWeb/HtmlView.h b/Libraries/LibWeb/HtmlView.h index 51de2194b3..7c6c37bdad 100644 --- a/Libraries/LibWeb/HtmlView.h +++ b/Libraries/LibWeb/HtmlView.h @@ -61,6 +61,7 @@ public: Function on_link_hover; Function on_title_change; Function on_load_start; + Function on_favicon_change; virtual bool accepts_focus() const override { return true; }