Skip to content

Truly independent web browser

Hub+UI/Qt: Make only suggestion and report requests for the UI

Andreas Kling675a2cc0ee24
diff --git a/Libraries/LibWebView/Autocomplete.cpp b/Libraries/LibWebView/Autocomplete.cpp
index c3bd7b92cf5..753afe9adda 100644
--- a/Libraries/LibWebView/Autocomplete.cpp
+++ b/Libraries/LibWebView/Autocomplete.cpp
@@ -12,7 +12,6 @@
#include <LibCore/EventLoop.h>
#include <LibCore/Timer.h>
#include <LibTextCodec/Decoder.h>
-#include <LibURL/Parser.h>
#include <LibURL/URL.h>
#include <LibWebCommon/MimeSniff/MimeType.h>
#include <LibWebView/Autocomplete.h>
@@ -360,16 +359,13 @@ void Autocomplete::start_remote_query(AutocompleteQueryID query_id, SearchEngine
{
dbgln_if(WEBVIEW_HISTORY_DEBUG, "[History] Fetching remote autocomplete suggestions from {} for '{}'", engine.name, query);
- auto url_string = MUST(engine.suggestions->query_url.replace("%s"sv, URL::percent_encode(query), ReplaceMode::All));
- auto url = URL::Parser::basic_parse(url_string);
-
- if (!url.has_value()) {
+ if (!engine.suggestions_url_for_query(query).has_value()) {
m_remote_query_complete = true;
deliver_current_result();
return;
}
- m_cancel_remote_query = m_host.fetch_remote_suggestions(*url, m_is_private, [this, query_id, engine, query = move(query)](AutocompleteHost::RemoteResponse response) {
+ m_cancel_remote_query = m_host.fetch_remote_suggestions(engine, query, m_is_private, [this, query_id, engine, query](AutocompleteHost::RemoteResponse response) {
// NB: The request is over. Canceling the query from inside the callbacks below must not cancel it again.
m_cancel_remote_query = nullptr;
diff --git a/Libraries/LibWebView/Autocomplete.h b/Libraries/LibWebView/Autocomplete.h
index ef67f3c4076..c77b01e6b02 100644
--- a/Libraries/LibWebView/Autocomplete.h
+++ b/Libraries/LibWebView/Autocomplete.h
@@ -112,8 +112,9 @@ public:
virtual Settings& autocomplete_settings() = 0;
virtual void resolve_external_url_handler(URL::URL const&, ExternalURLHandlerCallback) const = 0;
- // Returns a function that cancels the request. on_response is never called after that.
- virtual Function<void()> fetch_remote_suggestions(URL::URL const&, IsPrivate, Function<void(RemoteResponse)> on_response) = 0;
+ // Fetches the engine's suggestions for the query. Returns a function that cancels the request. on_response is never
+ // called after that.
+ virtual Function<void()> fetch_remote_suggestions(SearchEngine const&, String const& query, IsPrivate, Function<void(RemoteResponse)> on_response) = 0;
};
class WEBVIEW_API Autocomplete : public Weakable<Autocomplete>
diff --git a/Libraries/LibWebView/ReportSubmission.cpp b/Libraries/LibWebView/ReportSubmission.cpp
index 19f0fe42627..7e7576c4b76 100644
--- a/Libraries/LibWebView/ReportSubmission.cpp
+++ b/Libraries/LibWebView/ReportSubmission.cpp
@@ -371,13 +371,30 @@ void ReportSubmission::send_report(ProofOfWork proof)
}
// A response outside the 2xx range fails this attempt, and the activity names what was going on in that message.
-void ReportSubmission::post(StringView path, StringView activity, StringView content_type, ByteString body,
- Optional<ProofOfWork> proof, Function<void(ByteString)> on_success)
+static Optional<URL::URL> report_server_url(StringView path)
{
auto endpoint = Core::Environment::get("LADYBIRD_REPORTS_ENDPOINT"sv).value_or(default_endpoint);
auto url = URL::Parser::basic_parse(MUST(String::formatted("{}{}{}", endpoint,
endpoint.ends_with('/') ? ""sv : "/"sv, path.substring_view(1))));
- if (!url.has_value() || (url->scheme() != "https"sv && url->scheme() != "http"sv)) {
+ if (!url.has_value() || (url->scheme() != "https"sv && url->scheme() != "http"sv))
+ return {};
+ return url;
+}
+
+bool ReportSubmission::is_report_server_url(URL::URL const& url)
+{
+ for (auto path : { challenge_path, report_path }) {
+ if (auto report_url = report_server_url(path); report_url.has_value() && *report_url == url)
+ return true;
+ }
+ return false;
+}
+
+void ReportSubmission::post(StringView path, StringView activity, StringView content_type, ByteString body,
+ Optional<ProofOfWork> proof, Function<void(ByteString)> on_success)
+{
+ auto url = report_server_url(path);
+ if (!url.has_value()) {
fail("Invalid report server configuration."_string, ShouldRetry::No);
return;
}
@@ -390,8 +407,12 @@ void ReportSubmission::post(StringView path, StringView activity, StringView con
}
if (s_post_function) {
- m_cancel_post = s_post_function(*url, headers->headers(), move(body),
- [weak_this = make_weak_ptr<ReportSubmission>(), serial = ++m_post_serial, activity = ByteString { activity },
+ // NB: The hook may answer before it returns, and that answer can end this submission or start a retry.
+ NonnullRefPtr protector = *this;
+ auto serial = ++m_post_serial;
+ arm_request_timeout();
+ auto cancel_post = s_post_function(*url, headers->headers(), move(body),
+ [weak_this = make_weak_ptr<ReportSubmission>(), serial, activity = ByteString { activity },
on_success = move(on_success)](PostResponse response) {
auto self = weak_this.strong_ref();
if (!self || self->m_post_serial != serial)
@@ -400,7 +421,8 @@ void ReportSubmission::post(StringView path, StringView activity, StringView con
self->did_receive_response(activity, response.had_network_error, response.response_code,
*HTTP::HeaderList::create(move(response.headers)), response.body.bytes(), on_success);
});
- arm_request_timeout();
+ if (m_post_serial == serial && m_request_timeout)
+ m_cancel_post = move(cancel_post);
return;
}
diff --git a/Libraries/LibWebView/ReportSubmission.h b/Libraries/LibWebView/ReportSubmission.h
index fbe05f70492..b5290f1fc95 100644
--- a/Libraries/LibWebView/ReportSubmission.h
+++ b/Libraries/LibWebView/ReportSubmission.h
@@ -89,6 +89,8 @@ public:
using PostFunction = Function<Function<void()>(URL::URL const&, Vector<HTTP::Header>, ByteString body, Function<void(PostResponse)>)>;
// A process with no RequestServer of its own posts its reports through this.
static void set_post_function(PostFunction);
+ // Whether the URL is one that reports are posted to.
+ static bool is_report_server_url(URL::URL const&);
virtual ~ReportSubmission();
diff --git a/Libraries/LibWebView/SearchEngine.cpp b/Libraries/LibWebView/SearchEngine.cpp
index ab7266ace81..c33e629f84e 100644
--- a/Libraries/LibWebView/SearchEngine.cpp
+++ b/Libraries/LibWebView/SearchEngine.cpp
@@ -5,6 +5,7 @@
*/
#include <AK/Utf8View.h>
+#include <LibURL/Parser.h>
#include <LibURL/URL.h>
#include <LibWebView/SearchEngine.h>
@@ -45,4 +46,11 @@ String SearchEngine::format_search_query_for_navigation(StringView query) const
return MUST(query_url.replace("%s"sv, URL::percent_encode(query), ReplaceMode::All));
}
+Optional<URL::URL> SearchEngine::suggestions_url_for_query(StringView query) const
+{
+ if (!suggestions.has_value())
+ return {};
+ return URL::Parser::basic_parse(MUST(suggestions->query_url.replace("%s"sv, URL::percent_encode(query), ReplaceMode::All)));
+}
+
}
diff --git a/Libraries/LibWebView/SearchEngine.h b/Libraries/LibWebView/SearchEngine.h
index 1b90cf28949..2bab7486f7b 100644
--- a/Libraries/LibWebView/SearchEngine.h
+++ b/Libraries/LibWebView/SearchEngine.h
@@ -10,6 +10,7 @@
#include <AK/Span.h>
#include <AK/String.h>
#include <AK/StringView.h>
+#include <LibURL/Forward.h>
#include <LibWebView/Forward.h>
namespace WebView {
@@ -28,6 +29,7 @@ struct SearchSuggestions {
struct SearchEngine {
WEBVIEW_API String format_search_query_for_display(StringView query) const;
WEBVIEW_API String format_search_query_for_navigation(StringView query) const;
+ WEBVIEW_API Optional<URL::URL> suggestions_url_for_query(StringView query) const;
String name;
String query_url;
diff --git a/Services/Hub/ConnectionFromUI.cpp b/Services/Hub/ConnectionFromUI.cpp
index 1c9816d93e4..7ac40f74c30 100644
--- a/Services/Hub/ConnectionFromUI.cpp
+++ b/Services/Hub/ConnectionFromUI.cpp
@@ -4,6 +4,8 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
+#include <AK/AnyOf.h>
+#include <AK/Array.h>
#include <Hub/Application.h>
#include <Hub/ConnectionFromUI.h>
#include <Hub/Proxies.h>
@@ -17,6 +19,8 @@
#include <LibWebView/MicrophoneAuthorizer.h>
#include <LibWebView/PageTranslation.h>
#include <LibWebView/ProcessSnapshot.h>
+#include <LibWebView/ReportSubmission.h>
+#include <LibWebView/Settings.h>
#include <LibWebView/TabPerformanceMonitor.h>
#include <LibWebView/WebCompatReport.h>
#include <LibWebView/WebUI/ApplicationWebUI.h>
@@ -585,21 +589,59 @@ void ConnectionFromUI::ui_did_start()
Application::the().ui_did_start();
}
-void ConnectionFromUI::start_request(u64 request_id, ByteString method, URL::URL url, Vector<HTTP::Header> headers, ByteString body, HTTP::CacheMode cache_mode, HTTP::Cookie::IncludeCredentials include_credentials, WebView::IsPrivate is_private)
+// NB: The UI makes no requests of its own choosing. It names a search engine and a query, and the Hub makes the request
+// for that engine's suggestions, as the UI's own settings say.
+void ConnectionFromUI::fetch_search_suggestions(u64 request_id, String engine_name, String query, WebView::IsPrivate is_private)
+{
+ Optional<URL::URL> url;
+ if (auto engine = Application::settings().find_search_engine_by_name(engine_name); engine.has_value())
+ url = engine->suggestions_url_for_query(query);
+ if (!url.has_value()) {
+ async_request_finished(request_id, true, {}, {}, {});
+ return;
+ }
+ static constexpr Array response_header_names { "Content-Type"sv };
+ start_request(request_id, "GET"sv, *url, *HTTP::HeaderList::create(), {}, HTTP::CacheMode::Default, HTTP::Cookie::IncludeCredentials::Yes, is_private, response_header_names);
+}
+
+// NB: A report goes only to the report server, with no credentials, and with only the headers that reports carry.
+void ConnectionFromUI::post_report(u64 request_id, URL::URL url, Vector<HTTP::Header> headers, ByteString body)
+{
+ if (!WebView::ReportSubmission::is_report_server_url(url)) {
+ async_request_finished(request_id, true, {}, {}, {});
+ return;
+ }
+ static constexpr Array report_header_names { "Content-Type"sv, "X-Ladybird-Challenge"sv, "X-Ladybird-Nonce"sv };
+ auto request_headers = HTTP::HeaderList::create();
+ for (auto& header : headers) {
+ if (any_of(report_header_names, [&](auto name) { return header.name.equals_ignoring_ascii_case(name); }))
+ request_headers->append(move(header));
+ }
+ static constexpr Array response_header_names { "Retry-After"sv };
+ start_request(request_id, "POST"sv, url, *request_headers, body, HTTP::CacheMode::NoStore, HTTP::Cookie::IncludeCredentials::No, WebView::IsPrivate::No, response_header_names);
+}
+
+void ConnectionFromUI::start_request(u64 request_id, ByteString const& method, URL::URL const& url, HTTP::HeaderList const& headers, ByteString const& body, HTTP::CacheMode cache_mode, HTTP::Cookie::IncludeCredentials include_credentials, WebView::IsPrivate is_private, ReadonlySpan<StringView> response_header_names)
{
RefPtr<Requests::Request> request;
if (auto client = WebView::Application::request_server_client(is_private); !client.is_error())
- request = client.value()->start_request(method, url, HTTP::Cookie::SameSiteContext::same_site(), *HTTP::HeaderList::create(move(headers)), body.bytes(), cache_mode, include_credentials);
+ request = client.value()->start_request(method, url, HTTP::Cookie::SameSiteContext::same_site(), headers, body.bytes(), cache_mode, include_credentials);
if (!request) {
async_request_finished(request_id, true, {}, {}, {});
return;
}
request->set_buffered_request_finished_callback(
- [this, request_id](u64, Requests::RequestTimingInfo const&, Optional<Requests::NetworkError> const& network_error,
+ [this, request_id, response_header_names](u64, Requests::RequestTimingInfo const&, Optional<Requests::NetworkError> const& network_error,
HTTP::HeaderList const& response_headers, Optional<u32> response_code, Optional<String> const&,
Requests::CacheState, Core::ImmutableBytes payload) {
- async_request_finished(request_id, network_error.has_value(), response_code, response_headers.headers(), ByteString::copy(payload.bytes()));
+ // NB: The UI gets only the headers that the response is read for.
+ Vector<HTTP::Header> headers_for_ui;
+ for (auto name : response_header_names) {
+ if (auto value = response_headers.get(name); value.has_value())
+ headers_for_ui.append({ name, value.release_value() });
+ }
+ async_request_finished(request_id, network_error.has_value(), response_code, move(headers_for_ui), ByteString::copy(payload.bytes()));
// A request cannot be destroyed from inside its own callback.
Core::deferred_invoke([this, request_id] {
diff --git a/Services/Hub/ConnectionFromUI.h b/Services/Hub/ConnectionFromUI.h
index bc284a4a8c1..dd0c9121918 100644
--- a/Services/Hub/ConnectionFromUI.h
+++ b/Services/Hub/ConnectionFromUI.h
@@ -63,7 +63,9 @@ private:
virtual void add_web_ui_page(String host, String title) override;
virtual void send_web_ui_message(u64 web_ui_id, String name, JsonValue data) override;
virtual Messages::HubServer::WebUiConsumeUserActivationResponse web_ui_consume_user_activation(u64 web_ui_id) override;
- virtual void start_request(u64 request_id, ByteString method, URL::URL url, Vector<HTTP::Header> headers, ByteString body, HTTP::CacheMode cache_mode, HTTP::Cookie::IncludeCredentials include_credentials, WebView::IsPrivate is_private) override;
+ virtual void fetch_search_suggestions(u64 request_id, String engine_name, String query, WebView::IsPrivate is_private) override;
+ virtual void post_report(u64 request_id, URL::URL url, Vector<HTTP::Header> headers, ByteString body) override;
+ void start_request(u64 request_id, ByteString const& method, URL::URL const&, HTTP::HeaderList const& headers, ByteString const& body, HTTP::CacheMode, HTTP::Cookie::IncludeCredentials, WebView::IsPrivate, ReadonlySpan<StringView> response_header_names);
virtual void stop_request(u64 request_id) override;
virtual void toggle_devtools_enabled() override;
virtual void set_preferred_contrast(Web::CSS::PreferredContrast) override;
diff --git a/Services/Hub/HubServer.ipc b/Services/Hub/HubServer.ipc
index f4f608a3c22..93f40952a76 100644
--- a/Services/Hub/HubServer.ipc
+++ b/Services/Hub/HubServer.ipc
@@ -122,7 +122,8 @@ endpoint HubServer
add_web_ui_page(String host, String title) =|
send_web_ui_message(u64 web_ui_id, String name, JsonValue data) =|
web_ui_consume_user_activation(u64 web_ui_id) => (bool consumed)
- start_request(u64 request_id, ByteString method, URL::URL url, Vector<HTTP::Header> headers, ByteString body, HTTP::CacheMode cache_mode, HTTP::Cookie::IncludeCredentials include_credentials, WebView::IsPrivate is_private) =|
+ fetch_search_suggestions(u64 request_id, String engine_name, String query, WebView::IsPrivate is_private) =|
+ post_report(u64 request_id, URL::URL url, Vector<HTTP::Header> headers, ByteString body) =|
stop_request(u64 request_id) =|
toggle_devtools_enabled() =|
diff --git a/Tests/LibWebView/TestAutocomplete.cpp b/Tests/LibWebView/TestAutocomplete.cpp
index a1d847abd1f..4a81c7f3788 100644
--- a/Tests/LibWebView/TestAutocomplete.cpp
+++ b/Tests/LibWebView/TestAutocomplete.cpp
@@ -64,14 +64,15 @@ public:
virtual WebView::Settings& autocomplete_settings() override { return WebView::Application::settings(); }
- virtual Function<void()> fetch_remote_suggestions(URL::URL const& url, WebView::IsPrivate is_private, Function<void(RemoteResponse)> on_response) override
+ virtual Function<void()> fetch_remote_suggestions(WebView::SearchEngine const& engine, String const& query, WebView::IsPrivate is_private, Function<void(RemoteResponse)> on_response) override
{
+ auto url = engine.suggestions_url_for_query(query);
auto client = WebView::Application::request_server_client(is_private);
- if (client.is_error()) {
+ if (!url.has_value() || client.is_error()) {
on_response({ .had_network_error = true, .response_code = {}, .content_type = {}, .body = {} });
return [] { };
}
- auto request = client.value()->start_request("GET"sv, url);
+ auto request = client.value()->start_request("GET"sv, *url, HTTP::Cookie::SameSiteContext::same_site());
auto request_id = m_next_request_id++;
request->set_buffered_request_finished_callback(
[this, request_id, on_response = move(on_response)](u64, Requests::RequestTimingInfo const&, Optional<Requests::NetworkError> const& network_error,
diff --git a/UI/Qt/Application.cpp b/UI/Qt/Application.cpp
index c175e99a7d1..a7430efac87 100644
--- a/UI/Qt/Application.cpp
+++ b/UI/Qt/Application.cpp
@@ -853,10 +853,9 @@ ErrorOr<void> Application::connect_to_hub(Main::Arguments const& arguments)
create_platform_actions();
WebView::ReportSubmission::set_post_function([](auto const& url, auto headers, auto body, auto on_response) -> Function<void()> {
- auto request_id = HubConnection::the().start_request("POST"sv, url, move(headers), move(body), HTTP::CacheMode::NoStore,
- HTTP::Cookie::IncludeCredentials::No, WebView::IsPrivate::No, [on_response = move(on_response)](auto response) {
- on_response({ .had_network_error = response.had_network_error, .response_code = response.response_code, .headers = move(response.headers), .body = move(response.body) });
- });
+ auto request_id = HubConnection::the().post_report(url, move(headers), move(body), [on_response = move(on_response)](auto response) {
+ on_response({ .had_network_error = response.had_network_error, .response_code = response.response_code, .headers = move(response.headers), .body = move(response.body) });
+ });
return [request_id] {
if (auto* hub = HubConnection::the_if_connected())
hub->stop_request(request_id);
@@ -1351,9 +1350,9 @@ void Application::create_history_store(ByteString const& data_directory, bool us
create_autocomplete_service({});
}
-Function<void()> Application::fetch_remote_suggestions(URL::URL const& url, WebView::IsPrivate is_private, Function<void(RemoteResponse)> on_response)
+Function<void()> Application::fetch_remote_suggestions(WebView::SearchEngine const& engine, String const& query, WebView::IsPrivate is_private, Function<void(RemoteResponse)> on_response)
{
- auto request_id = HubConnection::the().start_request("GET"sv, url, {}, {}, HTTP::CacheMode::Default, HTTP::Cookie::IncludeCredentials::Yes, is_private,
+ auto request_id = HubConnection::the().fetch_search_suggestions(engine.name, query, is_private,
[on_response = move(on_response)](HubConnection::RequestResponse response) {
auto content_type = HTTP::HeaderList::create(move(response.headers))->get("Content-Type"sv);
on_response({
diff --git a/UI/Qt/Application.h b/UI/Qt/Application.h
index ea55d68d839..f3a97e601e6 100644
--- a/UI/Qt/Application.h
+++ b/UI/Qt/Application.h
@@ -104,7 +104,7 @@ public:
// The location bars make their suggestions from the history, and from the search engine through the Hub.
virtual WebView::AutocompleteService& autocomplete_service() override { return *m_autocomplete_service; }
virtual WebView::Settings& autocomplete_settings() override { return *m_settings; }
- virtual Function<void()> fetch_remote_suggestions(URL::URL const&, WebView::IsPrivate, Function<void(RemoteResponse)>) override;
+ virtual Function<void()> fetch_remote_suggestions(WebView::SearchEngine const&, String const& query, WebView::IsPrivate, Function<void(RemoteResponse)>) override;
static WebUIPages& web_ui_pages() { return *s_the->m_web_ui_pages; }
String const& command_line() const { return m_command_line; }
diff --git a/UI/Qt/HubConnection.cpp b/UI/Qt/HubConnection.cpp
index 51d87124201..f9a7e74a8e7 100644
--- a/UI/Qt/HubConnection.cpp
+++ b/UI/Qt/HubConnection.cpp
@@ -68,11 +68,19 @@ void HubConnection::crash_reports_changed()
#endif
}
-u64 HubConnection::start_request(ByteString method, URL::URL const& url, Vector<HTTP::Header> headers, ByteString body, HTTP::CacheMode cache_mode, HTTP::Cookie::IncludeCredentials include_credentials, WebView::IsPrivate is_private, Function<void(RequestResponse)> on_response)
+u64 HubConnection::fetch_search_suggestions(String engine_name, String query, WebView::IsPrivate is_private, Function<void(RequestResponse)> on_response)
{
auto request_id = m_next_request_id++;
m_requests.set(request_id, move(on_response));
- async_start_request(request_id, move(method), url, move(headers), move(body), cache_mode, include_credentials, is_private);
+ async_fetch_search_suggestions(request_id, move(engine_name), move(query), is_private);
+ return request_id;
+}
+
+u64 HubConnection::post_report(URL::URL const& url, Vector<HTTP::Header> headers, ByteString body, Function<void(RequestResponse)> on_response)
+{
+ auto request_id = m_next_request_id++;
+ m_requests.set(request_id, move(on_response));
+ async_post_report(request_id, url, move(headers), move(body));
return request_id;
}
diff --git a/UI/Qt/HubConnection.h b/UI/Qt/HubConnection.h
index cf11b26d074..100683fc237 100644
--- a/UI/Qt/HubConnection.h
+++ b/UI/Qt/HubConnection.h
@@ -32,14 +32,16 @@ public:
// The Hub has answered initialize(), so from now on the UI cannot go on without it.
void did_initialize();
- // The UI has no network of its own, so the Hub makes its requests. Returns an ID for stop_request().
+ // The UI has no network of its own. The Hub makes the few requests it needs, each of a kind the Hub knows and checks.
+ // These return an ID for stop_request().
struct RequestResponse {
bool had_network_error { false };
Optional<u32> response_code;
Vector<HTTP::Header> headers;
ByteString body;
};
- u64 start_request(ByteString method, URL::URL const&, Vector<HTTP::Header>, ByteString body, HTTP::CacheMode, HTTP::Cookie::IncludeCredentials, WebView::IsPrivate, Function<void(RequestResponse)>);
+ u64 fetch_search_suggestions(String engine_name, String query, WebView::IsPrivate, Function<void(RequestResponse)>);
+ u64 post_report(URL::URL const&, Vector<HTTP::Header>, ByteString body, Function<void(RequestResponse)>);
void stop_request(u64 request_id);
void set_settings_from_hub(JsonValue const&);