Skip to content

Commit 7cf8eab

Browse files
trflynn89kalenikaliaksandr
authored andcommitted
Browser: Use LibWeb's history state for history navigation
1 parent 8e2b1a8 commit 7cf8eab

File tree

7 files changed

+48
-92
lines changed

7 files changed

+48
-92
lines changed

Userland/Applications/Browser/BrowserWindow.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ void BrowserWindow::build_menus(StringView const man_file)
377377
},
378378
this));
379379
debug_menu->add_action(GUI::Action::create("Dump &History", { Mod_Ctrl, Key_H }, g_icon_bag.history, [this](auto&) {
380-
active_tab().m_history.dump();
380+
active_tab().view().debug_request("dump-session-history");
381381
}));
382382
debug_menu->add_action(GUI::Action::create("Dump C&ookies", g_icon_bag.cookie, [this](auto&) {
383383
m_cookie_jar.dump_cookies();

Userland/Applications/Browser/History/HistoryModel.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99

1010
namespace Browser {
1111

12-
void HistoryModel::set_items(AK::Vector<WebView::History::URLTitlePair> items)
12+
void HistoryModel::set_items(Vector<URLTitlePair> items)
1313
{
1414
begin_insert_rows({}, m_entries.size(), m_entries.size());
15-
m_entries = items;
15+
m_entries = move(items);
1616
end_insert_rows();
1717

1818
did_update(DontInvalidateIndices);

Userland/Applications/Browser/History/HistoryModel.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,20 @@
66

77
#pragma once
88

9+
#include <AK/ByteString.h>
910
#include <AK/Vector.h>
1011
#include <LibGUI/Model.h>
1112
#include <LibGUI/Widget.h>
12-
#include <LibWebView/History.h>
13+
#include <LibURL/URL.h>
1314

1415
namespace Browser {
1516

17+
// FIXME: Reimplement viewing history entries using WebContent's history.
18+
struct URLTitlePair {
19+
URL::URL url;
20+
ByteString title;
21+
};
22+
1623
class HistoryModel final : public GUI::Model {
1724
public:
1825
enum Column {
@@ -21,7 +28,7 @@ class HistoryModel final : public GUI::Model {
2128
__Count,
2229
};
2330

24-
void set_items(AK::Vector<WebView::History::URLTitlePair> items);
31+
void set_items(Vector<URLTitlePair> items);
2532
void clear_items();
2633
virtual int row_count(GUI::ModelIndex const&) const override;
2734
virtual int column_count(GUI::ModelIndex const& = GUI::ModelIndex()) const override { return Column::__Count; }
@@ -31,7 +38,7 @@ class HistoryModel final : public GUI::Model {
3138
virtual GUI::Model::MatchResult data_matches(GUI::ModelIndex const& index, GUI::Variant const& term) const override;
3239

3340
private:
34-
AK::Vector<WebView::History::URLTitlePair> m_entries;
41+
Vector<URLTitlePair> m_entries;
3542
};
3643

3744
}

Userland/Applications/Browser/History/HistoryWidget.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ HistoryWidget::HistoryWidget()
3131
m_table_view->set_alternating_row_colors(true);
3232
}
3333

34-
void HistoryWidget::set_history_entries(Vector<WebView::History::URLTitlePair> entries)
34+
void HistoryWidget::set_history_entries(Vector<URLTitlePair> entries)
3535
{
36-
m_model->set_items(entries);
36+
m_model->set_items(move(entries));
3737
}
3838

3939
void HistoryWidget::clear_history_entries()

Userland/Applications/Browser/History/HistoryWidget.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#include <LibGUI/FilteringProxyModel.h>
1111
#include <LibGUI/TextBox.h>
1212
#include <LibGUI/Widget.h>
13-
#include <LibWebView/History.h>
1413

1514
namespace Browser {
1615

@@ -20,7 +19,7 @@ class HistoryWidget final : public GUI::Widget {
2019
public:
2120
virtual ~HistoryWidget() override = default;
2221

23-
void set_history_entries(Vector<WebView::History::URLTitlePair> entries);
22+
void set_history_entries(Vector<URLTitlePair> entries);
2423
void clear_history_entries();
2524

2625
private:

Userland/Applications/Browser/Tab.cpp

Lines changed: 26 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
#include <LibGUI/Window.h>
4444
#include <LibURL/URL.h>
4545
#include <LibWeb/HTML/BrowsingContext.h>
46-
#include <LibWeb/HTML/HistoryHandlingBehavior.h>
4746
#include <LibWeb/HTML/SelectedFile.h>
4847
#include <LibWeb/HTML/SyntaxHighlighter/SyntaxHighlighter.h>
4948
#include <LibWeb/Layout/BlockContainer.h>
@@ -134,28 +133,18 @@ Tab::Tab(BrowserWindow& window)
134133

135134
auto& go_back_button = toolbar.add_action(window.go_back_action());
136135
go_back_button.on_context_menu_request = [&](auto&) {
137-
if (!m_history.can_go_back())
136+
if (!m_can_navigate_back)
138137
return;
139-
int i = 0;
140-
m_go_back_context_menu = GUI::Menu::construct();
141-
for (auto& url : m_history.get_back_title_history()) {
142-
i++;
143-
m_go_back_context_menu->add_action(GUI::Action::create(url.to_byte_string(), g_icon_bag.filetype_html, [this, i](auto&) { go_back(i); }));
144-
}
145-
m_go_back_context_menu->popup(go_back_button.screen_relative_rect().bottom_left().moved_up(1));
138+
139+
// FIXME: Reimplement selecting a specific entry using WebContent's history.
146140
};
147141

148142
auto& go_forward_button = toolbar.add_action(window.go_forward_action());
149143
go_forward_button.on_context_menu_request = [&](auto&) {
150-
if (!m_history.can_go_forward())
144+
if (!m_can_navigate_forward)
151145
return;
152-
int i = 0;
153-
m_go_forward_context_menu = GUI::Menu::construct();
154-
for (auto& url : m_history.get_forward_title_history()) {
155-
i++;
156-
m_go_forward_context_menu->add_action(GUI::Action::create(url.to_byte_string(), g_icon_bag.filetype_html, [this, i](auto&) { go_forward(i); }));
157-
}
158-
m_go_forward_context_menu->popup(go_forward_button.screen_relative_rect().bottom_left().moved_up(1));
146+
147+
// FIXME: Reimplement selecting a specific entry using WebContent's history.
159148
};
160149

161150
auto& go_home_button = toolbar.add_action(window.go_home_action());
@@ -215,16 +204,10 @@ Tab::Tab(BrowserWindow& window)
215204
m_bookmark_button->set_icon(g_icon_bag.bookmark_contour);
216205
m_bookmark_button->set_fixed_size(22, 22);
217206

218-
view().on_load_start = [this](auto& url, bool is_redirect) {
207+
view().on_load_start = [this](auto& url, bool) {
219208
m_navigating_url = url;
220209
m_loaded = false;
221210

222-
// If we are loading due to a redirect, we replace the current history entry
223-
// with the loaded URL
224-
if (is_redirect) {
225-
m_history.replace_current(url, title());
226-
}
227-
228211
auto url_serialized = url.serialize();
229212

230213
m_title = url_serialized;
@@ -235,17 +218,10 @@ Tab::Tab(BrowserWindow& window)
235218
if (on_favicon_change)
236219
on_favicon_change(*m_icon);
237220

238-
update_status();
239-
240221
m_location_box->set_icon(nullptr);
241222
m_location_box->set_text(url_serialized);
242223

243-
// don't add to history if back or forward is pressed
244-
if (!m_is_history_navigation)
245-
m_history.push(url, title());
246-
m_is_history_navigation = false;
247-
248-
update_actions();
224+
update_status();
249225
update_bookmark_button(url_serialized);
250226

251227
if (m_dom_inspector_widget)
@@ -262,29 +238,25 @@ Tab::Tab(BrowserWindow& window)
262238
m_dom_inspector_widget->inspect();
263239
};
264240

265-
view().on_history_api_push_or_replace = [this](auto const& url, auto history_behavior) {
266-
switch (history_behavior) {
267-
case Web::HTML::HistoryHandlingBehavior::Push:
268-
m_history.push(url, m_title);
269-
break;
270-
case Web::HTML::HistoryHandlingBehavior::Replace:
271-
m_history.replace_current(url, m_title);
272-
break;
273-
}
274-
241+
view().on_url_change = [this](auto const& url) {
275242
auto url_serialized = url.serialize();
276243
m_location_box->set_text(url_serialized);
277244

278-
update_actions();
279245
update_bookmark_button(url_serialized);
280246
};
281247

248+
view().on_navigation_buttons_state_changed = [this](auto back_enabled, auto forward_enabled) {
249+
m_can_navigate_back = back_enabled;
250+
m_can_navigate_forward = forward_enabled;
251+
update_actions();
252+
};
253+
282254
view().on_navigate_back = [this]() {
283-
go_back(1);
255+
go_back();
284256
};
285257

286258
view().on_navigate_forward = [this]() {
287-
go_forward(1);
259+
go_forward();
288260
};
289261

290262
view().on_refresh = [this]() {
@@ -499,7 +471,6 @@ Tab::Tab(BrowserWindow& window)
499471
};
500472

501473
view().on_title_change = [this](auto const& title) {
502-
m_history.update_title(title);
503474
m_title = title;
504475

505476
if (on_title_change)
@@ -831,9 +802,8 @@ void Tab::update_reset_zoom_button()
831802
}
832803
}
833804

834-
void Tab::load(URL::URL const& url, LoadType load_type)
805+
void Tab::load(URL::URL const& url)
835806
{
836-
m_is_history_navigation = (load_type == LoadType::HistoryNavigation);
837807
m_web_content_view->load(url);
838808
m_location_box->set_focus(false);
839809
}
@@ -845,40 +815,26 @@ URL::URL Tab::url() const
845815

846816
void Tab::reload()
847817
{
848-
if (m_history.is_empty())
849-
return;
850-
851-
load(url());
818+
view().reload();
852819
}
853820

854-
void Tab::go_back(int steps)
821+
void Tab::go_back()
855822
{
856-
if (!m_history.can_go_back(steps))
857-
return;
858-
859-
m_history.go_back(steps);
860-
update_actions();
861-
load(m_history.current().url, LoadType::HistoryNavigation);
823+
view().traverse_the_history_by_delta(-1);
862824
}
863825

864-
void Tab::go_forward(int steps)
826+
void Tab::go_forward()
865827
{
866-
if (!m_history.can_go_forward(steps))
867-
return;
868-
869-
m_history.go_forward(steps);
870-
update_actions();
871-
load(m_history.current().url, LoadType::HistoryNavigation);
828+
view().traverse_the_history_by_delta(1);
872829
}
873830

874831
void Tab::update_actions()
875832
{
876833
auto& window = this->window();
877834
if (this != &window.active_tab())
878835
return;
879-
window.go_back_action().set_enabled(m_history.can_go_back());
880-
window.go_forward_action().set_enabled(m_history.can_go_forward());
881-
window.reload_action().set_enabled(!m_history.is_empty());
836+
window.go_back_action().set_enabled(m_can_navigate_back);
837+
window.go_forward_action().set_enabled(m_can_navigate_forward);
882838
}
883839

884840
ErrorOr<void> Tab::bookmark_current_url()
@@ -1077,8 +1033,8 @@ void Tab::show_history_inspector()
10771033
m_history_widget = history_window->set_main_widget<HistoryWidget>();
10781034
}
10791035

1036+
// FIXME: Reimplement viewing history entries using WebContent's history.
10801037
m_history_widget->clear_history_entries();
1081-
m_history_widget->set_history_entries(m_history.get_all_history_entries());
10821038

10831039
auto* window = m_history_widget->window();
10841040
window->show();

Userland/Applications/Browser/Tab.h

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
#include <LibHTTP/Job.h>
1515
#include <LibURL/URL.h>
1616
#include <LibWeb/Forward.h>
17-
#include <LibWebView/History.h>
1817
#include <LibWebView/ViewImplementation.h>
1918

2019
namespace WebView {
@@ -40,16 +39,11 @@ class Tab final : public GUI::Widget {
4039

4140
URL::URL url() const;
4241

43-
enum class LoadType {
44-
Normal,
45-
HistoryNavigation,
46-
};
47-
48-
void load(URL::URL const&, LoadType = LoadType::Normal);
42+
void load(URL::URL const&);
4943

5044
void reload();
51-
void go_back(int steps = 1);
52-
void go_forward(int steps = 1);
45+
void go_back();
46+
void go_forward();
5347

5448
void did_become_active();
5549
void context_menu_requested(Gfx::IntPoint screen_position);
@@ -109,8 +103,6 @@ class Tab final : public GUI::Widget {
109103
void update_status(Optional<String> text_override = {}, i32 count_waiting = 0);
110104
void close_sub_widgets();
111105

112-
WebView::History m_history;
113-
114106
RefPtr<WebView::OutOfProcessWebView> m_web_content_view;
115107

116108
RefPtr<URLBox> m_location_box;
@@ -158,7 +150,9 @@ class Tab final : public GUI::Widget {
158150
Optional<URL::URL> m_navigating_url;
159151

160152
bool m_loaded { false };
161-
bool m_is_history_navigation { false };
153+
154+
bool m_can_navigate_back { false };
155+
bool m_can_navigate_forward { false };
162156
};
163157

164158
}

0 commit comments

Comments
 (0)