Ladybird/Qt: Make String allocation infallible

This commit is contained in:
Timothy Flynn 2023-12-04 10:08:16 -05:00 committed by Andrew Kaster
parent 82c827fc56
commit a21998003c
Notes: sideshowbarker 2024-07-17 21:11:12 +09:00
10 changed files with 27 additions and 32 deletions

View File

@ -121,10 +121,10 @@ ErrorOr<void> AutoComplete::got_network_response(QNetworkReply* reply)
return Error::from_string_view("Invalid engine name"sv);
}
ErrorOr<String> AutoComplete::auto_complete_url_from_query(StringView query)
String AutoComplete::auto_complete_url_from_query(StringView query)
{
auto autocomplete_engine = TRY(ak_string_from_qstring(Settings::the()->autocomplete_engine().url));
return autocomplete_engine.replace("{}"sv, AK::URL::percent_encode(query), ReplaceMode::FirstOnly);
auto autocomplete_engine = ak_string_from_qstring(Settings::the()->autocomplete_engine().url);
return MUST(autocomplete_engine.replace("{}"sv, AK::URL::percent_encode(query), ReplaceMode::FirstOnly));
}
void AutoComplete::clear_suggestions()
@ -132,19 +132,17 @@ void AutoComplete::clear_suggestions()
m_auto_complete_model->clear();
}
ErrorOr<void> AutoComplete::get_search_suggestions(StringView search_string)
void AutoComplete::get_search_suggestions(String search_string)
{
m_query = TRY(String::from_utf8(search_string));
m_query = move(search_string);
if (m_reply)
m_reply->abort();
m_auto_complete_model->clear();
m_auto_complete_model->add(m_query);
QNetworkRequest request { QUrl(qstring_from_ak_string(TRY(auto_complete_url_from_query(m_query)))) };
QNetworkRequest request { QUrl(qstring_from_ak_string(auto_complete_url_from_query(m_query))) };
m_reply = m_manager->get(request);
return {};
}
}

View File

@ -48,6 +48,7 @@ public:
private:
AK::Vector<String> m_suggestions;
};
class AutoComplete final : public QCompleter {
Q_OBJECT
@ -59,14 +60,15 @@ public:
return index.data(Qt::DisplayRole).toString();
}
ErrorOr<void> get_search_suggestions(StringView);
void get_search_suggestions(String);
void clear_suggestions();
static ErrorOr<String> auto_complete_url_from_query(StringView query);
signals:
void activated(QModelIndex const&);
private:
static String auto_complete_url_from_query(StringView query);
ErrorOr<void> got_network_response(QNetworkReply* reply);
ErrorOr<void> parse_google_autocomplete(Vector<JsonValue> const&);

View File

@ -34,7 +34,7 @@ LocationEdit::LocationEdit(QWidget* parent)
if (Settings::the()->enable_search())
search_engine_url = Settings::the()->search_engine().query_url;
auto query = MUST(ak_string_from_qstring(text()));
auto query = ak_string_from_qstring(text());
if (auto url = WebView::sanitize_url(query, search_engine_url); url.has_value())
setText(qstring_from_ak_string(url->serialize()));
@ -48,12 +48,7 @@ LocationEdit::LocationEdit(QWidget* parent)
auto cursor_position = cursorPosition();
auto result = m_autocomplete->get_search_suggestions(ak_deprecated_string_from_qstring(text()));
if (result.is_error()) {
dbgln("LocationEdit::textEdited: get_search_suggestions failed: {}", result.error());
return;
}
m_autocomplete->get_search_suggestions(ak_string_from_qstring(text()));
setCursorPosition(cursor_position);
});
@ -75,7 +70,7 @@ void LocationEdit::focusOutEvent(QFocusEvent* event)
void LocationEdit::highlight_location()
{
auto url = MUST(ak_string_from_qstring(text()));
auto url = ak_string_from_qstring(text());
QList<QInputMethodEvent::Attribute> attributes;
if (auto url_parts = WebView::break_url_into_parts(url); url_parts.has_value()) {

View File

@ -20,7 +20,7 @@ Settings::Settings()
auto default_search_engine_name = qstring_from_ak_string(default_search_engine.name);
auto search_engine_name = m_qsettings->value("search_engine_name", default_search_engine_name).toString();
auto search_engine = WebView::find_search_engine_by_name(MUST(ak_string_from_qstring(search_engine_name)));
auto search_engine = WebView::find_search_engine_by_name(ak_string_from_qstring(search_engine_name));
if (search_engine.has_value())
m_search_engine = search_engine.release_value();

View File

@ -37,11 +37,11 @@ SettingsDialog::SettingsDialog(QMainWindow* window)
m_new_tab_page = make<QLineEdit>(this);
m_new_tab_page->setText(Settings::the()->new_tab_page());
QObject::connect(m_new_tab_page, &QLineEdit::textChanged, this, [this] {
auto url_string = MUST(ak_string_from_qstring(m_new_tab_page->text()));
auto url_string = ak_string_from_qstring(m_new_tab_page->text());
m_new_tab_page->setStyleSheet(URL(url_string).is_valid() ? "" : "border: 1px solid red;");
});
QObject::connect(m_new_tab_page, &QLineEdit::editingFinished, this, [this] {
auto url_string = MUST(ak_string_from_qstring(m_new_tab_page->text()));
auto url_string = ak_string_from_qstring(m_new_tab_page->text());
if (URL(url_string).is_valid())
Settings::the()->set_new_tab_page(m_new_tab_page->text());
});

View File

@ -11,10 +11,10 @@ AK::DeprecatedString ak_deprecated_string_from_qstring(QString const& qstring)
return AK::DeprecatedString(qstring.toUtf8().data());
}
ErrorOr<String> ak_string_from_qstring(QString const& qstring)
String ak_string_from_qstring(QString const& qstring)
{
auto utf8_data = qstring.toUtf8();
return String::from_utf8(StringView(utf8_data.data(), utf8_data.size()));
return MUST(String::from_utf8(StringView(utf8_data.data(), utf8_data.size())));
}
QString qstring_from_ak_string(StringView ak_string)

View File

@ -13,5 +13,5 @@
#include <QString>
AK::DeprecatedString ak_deprecated_string_from_qstring(QString const&);
ErrorOr<String> ak_string_from_qstring(QString const&);
String ak_string_from_qstring(QString const&);
QString qstring_from_ak_string(StringView);

View File

@ -189,7 +189,7 @@ Tab::Tab(BrowserWindow* window, WebContentOptions const& web_content_options, St
dialog.setTextValue(qstring_from_ak_string(default_));
if (dialog.exec() == QDialog::Accepted)
view().prompt_closed(ak_string_from_qstring(dialog.textValue()).release_value_but_fixme_should_propagate_errors());
view().prompt_closed(ak_string_from_qstring(dialog.textValue()));
else
view().prompt_closed({});
@ -295,7 +295,7 @@ Tab::Tab(BrowserWindow* window, WebContentOptions const& web_content_options, St
take_visible_screenshot_action->setIcon(load_icon_from_uri("resource://icons/16x16/filetype-image.png"sv));
QObject::connect(take_visible_screenshot_action, &QAction::triggered, this, [this]() {
if (auto result = view().take_screenshot(WebView::ViewImplementation::ScreenshotType::Visible); result.is_error()) {
auto error = String::formatted("{}", result.error()).release_value_but_fixme_should_propagate_errors();
auto error = MUST(String::formatted("{}", result.error()));
QMessageBox::warning(this, "Ladybird", qstring_from_ak_string(error));
}
});
@ -304,7 +304,7 @@ Tab::Tab(BrowserWindow* window, WebContentOptions const& web_content_options, St
take_full_screenshot_action->setIcon(load_icon_from_uri("resource://icons/16x16/filetype-image.png"sv));
QObject::connect(take_full_screenshot_action, &QAction::triggered, this, [this]() {
if (auto result = view().take_screenshot(WebView::ViewImplementation::ScreenshotType::Full); result.is_error()) {
auto error = String::formatted("{}", result.error()).release_value_but_fixme_should_propagate_errors();
auto error = MUST(String::formatted("{}", result.error()));
QMessageBox::warning(this, "Ladybird", qstring_from_ak_string(error));
}
});
@ -577,7 +577,7 @@ void Tab::focus_location_editor()
void Tab::navigate(QString const& url_qstring)
{
auto url_string = MUST(ak_string_from_qstring(url_qstring));
auto url_string = ak_string_from_qstring(url_qstring);
view().load(url_string);
}

View File

@ -53,7 +53,7 @@ void WebSocketImplQt::connect(WebSocket::ConnectionInfo const& connection_info)
if (connection_info.is_secure()) {
auto ssl_socket = make<QSslSocket>();
ssl_socket->connectToHostEncrypted(
qstring_from_ak_string(connection_info.url().serialized_host().release_value_but_fixme_should_propagate_errors()),
qstring_from_ak_string(MUST(connection_info.url().serialized_host())),
connection_info.url().port_or_default());
QObject::connect(ssl_socket.ptr(), &QSslSocket::alertReceived, [this](QSsl::AlertLevel level, QSsl::AlertType, QString const&) {
if (level == QSsl::AlertLevel::Fatal)
@ -63,7 +63,7 @@ void WebSocketImplQt::connect(WebSocket::ConnectionInfo const& connection_info)
} else {
m_socket = make<QTcpSocket>();
m_socket->connectToHost(
qstring_from_ak_string(connection_info.url().serialized_host().release_value_but_fixme_should_propagate_errors()),
qstring_from_ak_string(MUST(connection_info.url().serialized_host())),
connection_info.url().port_or_default());
}

View File

@ -72,7 +72,7 @@ public:
break;
auto const& open_event = *static_cast<QFileOpenEvent const*>(event);
auto file = MUST(ak_string_from_qstring(open_event.file()));
auto file = ak_string_from_qstring(open_event.file());
if (auto file_url = WebView::sanitize_url(file); file_url.has_value())
on_open_file(file_url.release_value());
@ -137,7 +137,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
if (initial_urls.is_empty()) {
auto new_tab_page = Ladybird::Settings::the()->new_tab_page();
initial_urls.append(MUST(ak_string_from_qstring(new_tab_page)));
initial_urls.append(ak_string_from_qstring(new_tab_page));
}
Ladybird::WebContentOptions web_content_options {