This repository has been archived by the owner on Dec 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 102
/
BrowserWindow.cpp
418 lines (348 loc) · 17.5 KB
/
BrowserWindow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/*
* Copyright (c) 2022, Andreas Kling <[email protected]>
* Copyright (c) 2022, Matthew Costa <[email protected]>
* Copyright (c) 2022, Filiph Sandström <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "BrowserWindow.h"
#include "Settings.h"
#include "SettingsDialog.h"
#include "Utilities.h"
#include "WebContentView.h"
#include <AK/TypeCasts.h>
#include <Browser/CookieJar.h>
#include <LibWeb/Loader/ResourceLoader.h>
#include <QAction>
#include <QActionGroup>
#include <QInputDialog>
#include <QPlainTextEdit>
extern DeprecatedString s_serenity_resource_root;
extern Browser::Settings* s_settings;
BrowserWindow::BrowserWindow(Browser::CookieJar& cookie_jar, StringView webdriver_content_ipc_path)
: m_cookie_jar(cookie_jar)
, m_webdriver_content_ipc_path(webdriver_content_ipc_path)
{
m_tabs_container = new QTabWidget(this);
m_tabs_container->setElideMode(Qt::TextElideMode::ElideRight);
m_tabs_container->setMovable(true);
m_tabs_container->setTabsClosable(true);
m_tabs_container->setDocumentMode(true);
m_tabs_container->setTabBarAutoHide(true);
auto* menu = menuBar()->addMenu("&File");
auto* new_tab_action = new QAction("New &Tab", this);
new_tab_action->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_T));
menu->addAction(new_tab_action);
auto* settings_action = new QAction("&Settings", this);
settings_action->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_Comma));
menu->addAction(settings_action);
auto* close_current_tab_action = new QAction("Close Current Tab", this);
close_current_tab_action->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_W));
menu->addAction(close_current_tab_action);
auto* quit_action = new QAction("&Quit", this);
quit_action->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_Q));
menu->addAction(quit_action);
auto* view_menu = menuBar()->addMenu("&View");
auto* open_next_tab_action = new QAction("Open &Next Tab", this);
open_next_tab_action->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_PageDown));
view_menu->addAction(open_next_tab_action);
QObject::connect(open_next_tab_action, &QAction::triggered, this, &BrowserWindow::open_next_tab);
auto* open_previous_tab_action = new QAction("Open &Previous Tab", this);
open_previous_tab_action->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_PageUp));
view_menu->addAction(open_previous_tab_action);
QObject::connect(open_previous_tab_action, &QAction::triggered, this, &BrowserWindow::open_previous_tab);
view_menu->addSeparator();
auto* color_scheme_menu = view_menu->addMenu("&Color Scheme");
auto* color_scheme_group = new QActionGroup(this);
auto* auto_color_scheme = new QAction("&Auto", this);
auto_color_scheme->setCheckable(true);
color_scheme_group->addAction(auto_color_scheme);
color_scheme_menu->addAction(auto_color_scheme);
QObject::connect(auto_color_scheme, &QAction::triggered, this, &BrowserWindow::enable_auto_color_scheme);
auto* light_color_scheme = new QAction("&Light", this);
light_color_scheme->setCheckable(true);
color_scheme_group->addAction(light_color_scheme);
color_scheme_menu->addAction(light_color_scheme);
QObject::connect(light_color_scheme, &QAction::triggered, this, &BrowserWindow::enable_light_color_scheme);
auto* dark_color_scheme = new QAction("&Dark", this);
dark_color_scheme->setCheckable(true);
color_scheme_group->addAction(dark_color_scheme);
color_scheme_menu->addAction(dark_color_scheme);
QObject::connect(dark_color_scheme, &QAction::triggered, this, &BrowserWindow::enable_dark_color_scheme);
auto_color_scheme->setChecked(true);
auto* inspect_menu = menuBar()->addMenu("&Inspect");
auto* view_source_action = new QAction("View &Source", this);
view_source_action->setIcon(QIcon(QString("%1/res/icons/16x16/filetype-html.png").arg(s_serenity_resource_root.characters())));
view_source_action->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_U));
inspect_menu->addAction(view_source_action);
QObject::connect(view_source_action, &QAction::triggered, this, [this] {
if (m_current_tab) {
m_current_tab->view().get_source();
}
});
auto* js_console_action = new QAction("Show &JS Console", this);
js_console_action->setIcon(QIcon(QString("%1/res/icons/16x16/filetype-javascript.png").arg(s_serenity_resource_root.characters())));
js_console_action->setShortcut(QKeySequence(Qt::CTRL | Qt::SHIFT | Qt::Key_J));
inspect_menu->addAction(js_console_action);
QObject::connect(js_console_action, &QAction::triggered, this, [this] {
if (m_current_tab) {
m_current_tab->view().show_js_console();
}
});
auto* inspector_action = new QAction("Open &Inspector");
inspector_action->setIcon(QIcon(QString("%1/res/icons/browser/dom-tree.png").arg(s_serenity_resource_root.characters())));
inspector_action->setShortcut(QKeySequence("Ctrl+Shift+I"));
inspect_menu->addAction(inspector_action);
QObject::connect(inspector_action, &QAction::triggered, this, [this] {
if (m_current_tab) {
m_current_tab->view().show_inspector();
}
});
auto* debug_menu = menuBar()->addMenu("&Debug");
auto* dump_dom_tree_action = new QAction("Dump DOM Tree", this);
dump_dom_tree_action->setIcon(QIcon(QString("%1/res/icons/browser/dom-tree.png").arg(s_serenity_resource_root.characters())));
debug_menu->addAction(dump_dom_tree_action);
QObject::connect(dump_dom_tree_action, &QAction::triggered, this, [this] {
debug_request("dump-dom-tree");
});
auto* dump_layout_tree_action = new QAction("Dump Layout Tree", this);
dump_layout_tree_action->setIcon(QIcon(QString("%1/res/icons/16x16/layout.png").arg(s_serenity_resource_root.characters())));
debug_menu->addAction(dump_layout_tree_action);
QObject::connect(dump_layout_tree_action, &QAction::triggered, this, [this] {
debug_request("dump-layout-tree");
});
auto* dump_stacking_context_tree_action = new QAction("Dump Stacking Context Tree", this);
dump_stacking_context_tree_action->setIcon(QIcon(QString("%1/res/icons/16x16/layers.png").arg(s_serenity_resource_root.characters())));
debug_menu->addAction(dump_stacking_context_tree_action);
QObject::connect(dump_stacking_context_tree_action, &QAction::triggered, this, [this] {
debug_request("dump-stacking-context-tree");
});
auto* dump_style_sheets_action = new QAction("Dump Style Sheets", this);
dump_style_sheets_action->setIcon(QIcon(QString("%1/res/icons/16x16/filetype-css.png").arg(s_serenity_resource_root.characters())));
debug_menu->addAction(dump_style_sheets_action);
QObject::connect(dump_style_sheets_action, &QAction::triggered, this, [this] {
debug_request("dump-style-sheets");
});
auto* dump_history_action = new QAction("Dump History", this);
dump_history_action->setIcon(QIcon(QString("%1/res/icons/16x16/history.png").arg(s_serenity_resource_root.characters())));
debug_menu->addAction(dump_history_action);
QObject::connect(dump_history_action, &QAction::triggered, this, [this] {
debug_request("dump-history");
});
auto* dump_cookies_action = new QAction("Dump Cookies", this);
dump_cookies_action->setIcon(QIcon(QString("%1/res/icons/browser/cookie.png").arg(s_serenity_resource_root.characters())));
debug_menu->addAction(dump_cookies_action);
QObject::connect(dump_cookies_action, &QAction::triggered, this, [this] {
m_cookie_jar.dump_cookies();
});
auto* dump_local_storage_action = new QAction("Dump Local Storage", this);
dump_local_storage_action->setIcon(QIcon(QString("%1/res/icons/browser/local-storage.png").arg(s_serenity_resource_root.characters())));
debug_menu->addAction(dump_local_storage_action);
QObject::connect(dump_local_storage_action, &QAction::triggered, this, [this] {
debug_request("dump-local-storage");
});
debug_menu->addSeparator();
auto* show_line_box_borders_action = new QAction("Show Line Box Borders", this);
show_line_box_borders_action->setCheckable(true);
debug_menu->addAction(show_line_box_borders_action);
QObject::connect(show_line_box_borders_action, &QAction::triggered, this, [this, show_line_box_borders_action] {
bool state = show_line_box_borders_action->isChecked();
debug_request("set-line-box-borders", state ? "on" : "off");
});
debug_menu->addSeparator();
auto* collect_garbage_action = new QAction("Collect Garbage", this);
collect_garbage_action->setShortcut(QKeySequence(Qt::CTRL | Qt::SHIFT | Qt::Key_G));
collect_garbage_action->setIcon(QIcon(QString("%1/res/icons/16x16/trash-can.png").arg(s_serenity_resource_root.characters())));
debug_menu->addAction(collect_garbage_action);
QObject::connect(collect_garbage_action, &QAction::triggered, this, [this] {
debug_request("collect-garbage");
});
auto* clear_cache_action = new QAction("Clear Cache", this);
clear_cache_action->setIcon(QIcon(QString("%1/res/icons/browser/clear-cache.png").arg(s_serenity_resource_root.characters())));
debug_menu->addAction(clear_cache_action);
QObject::connect(clear_cache_action, &QAction::triggered, this, [this] {
debug_request("clear-cache");
});
auto* spoof_user_agent_menu = debug_menu->addMenu("Spoof User Agent");
spoof_user_agent_menu->setIcon(QIcon(QString("%1/res/icons/16x16/spoof.png").arg(s_serenity_resource_root.characters())));
auto* user_agent_group = new QActionGroup(this);
auto add_user_agent = [this, &user_agent_group, &spoof_user_agent_menu](auto& name, auto& user_agent) {
auto* action = new QAction(name);
action->setCheckable(true);
user_agent_group->addAction(action);
spoof_user_agent_menu->addAction(action);
QObject::connect(action, &QAction::triggered, this, [this, user_agent] {
debug_request("spoof-user-agent", user_agent);
debug_request("clear-cache"); // clear the cache to ensure requests are re-done with the new user agent
});
return action;
};
auto* disable_spoofing = add_user_agent("Disabled", Web::default_user_agent);
disable_spoofing->setChecked(true);
add_user_agent("Chrome Linux Desktop", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.128 Safari/537.36");
add_user_agent("Firefox Linux Desktop", "Mozilla/5.0 (X11; Linux i686; rv:87.0) Gecko/20100101 Firefox/87.0");
add_user_agent("Safari macOS Desktop", "Mozilla/5.0 (Macintosh; Intel Mac OS X 11_2_3) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0.3 Safari/605.1.15");
add_user_agent("Chrome Android Mobile", "Mozilla/5.0 (Linux; Android 10) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.66 Mobile Safari/537.36");
add_user_agent("Firefox Android Mobile", "Mozilla/5.0 (Android 11; Mobile; rv:68.0) Gecko/68.0 Firefox/86.0");
add_user_agent("Safari iOS Mobile", "Mozilla/5.0 (iPhone; CPU iPhone OS 14_4_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0 Mobile/15E148 Safari/604.1");
auto* custom_user_agent_action = new QAction("Custom...");
custom_user_agent_action->setCheckable(true);
user_agent_group->addAction(custom_user_agent_action);
spoof_user_agent_menu->addAction(custom_user_agent_action);
QObject::connect(custom_user_agent_action, &QAction::triggered, this, [this, disable_spoofing] {
auto user_agent = QInputDialog::getText(this, "Custom User Agent", "Enter User Agent:");
if (!user_agent.isEmpty()) {
debug_request("spoof-user-agent", ak_deprecated_string_from_qstring(user_agent));
debug_request("clear-cache"); // clear the cache to ensure requests are re-done with the new user agent
} else {
disable_spoofing->activate(QAction::Trigger);
}
});
debug_menu->addSeparator();
auto* enable_scripting_action = new QAction("Enable Scripting", this);
enable_scripting_action->setCheckable(true);
enable_scripting_action->setChecked(true);
debug_menu->addAction(enable_scripting_action);
QObject::connect(enable_scripting_action, &QAction::triggered, this, [this, enable_scripting_action] {
bool state = enable_scripting_action->isChecked();
debug_request("scripting", state ? "on" : "off");
});
auto* block_pop_ups_action = new QAction("Block Pop-ups", this);
block_pop_ups_action->setCheckable(true);
block_pop_ups_action->setChecked(true);
debug_menu->addAction(block_pop_ups_action);
QObject::connect(block_pop_ups_action, &QAction::triggered, this, [this, block_pop_ups_action] {
bool state = block_pop_ups_action->isChecked();
debug_request("block-pop-ups", state ? "on" : "off");
});
auto* enable_same_origin_policy_action = new QAction("Enable Same-Origin Policy", this);
enable_same_origin_policy_action->setCheckable(true);
debug_menu->addAction(enable_same_origin_policy_action);
QObject::connect(enable_same_origin_policy_action, &QAction::triggered, this, [this, enable_same_origin_policy_action] {
bool state = enable_same_origin_policy_action->isChecked();
debug_request("same-origin-policy", state ? "on" : "off");
});
QObject::connect(new_tab_action, &QAction::triggered, this, &BrowserWindow::new_tab);
QObject::connect(settings_action, &QAction::triggered, this, [this] {
new SettingsDialog(this);
});
QObject::connect(quit_action, &QAction::triggered, this, &QMainWindow::close);
QObject::connect(m_tabs_container, &QTabWidget::currentChanged, [this](int index) {
setWindowTitle(QString("%1 - Ladybird").arg(m_tabs_container->tabText(index)));
setWindowIcon(m_tabs_container->tabIcon(index));
m_current_tab = verify_cast<Tab>(m_tabs_container->widget(index));
});
QObject::connect(m_tabs_container, &QTabWidget::tabCloseRequested, this, &BrowserWindow::close_tab);
QObject::connect(close_current_tab_action, &QAction::triggered, this, &BrowserWindow::close_current_tab);
new_tab();
setCentralWidget(m_tabs_container);
}
void BrowserWindow::debug_request(DeprecatedString const& request, DeprecatedString const& argument)
{
if (!m_current_tab)
return;
m_current_tab->debug_request(request, argument);
}
void BrowserWindow::new_tab()
{
auto tab = make<Tab>(this, m_webdriver_content_ipc_path);
auto tab_ptr = tab.ptr();
m_tabs.append(std::move(tab));
if (m_current_tab == nullptr) {
m_current_tab = tab_ptr;
}
m_tabs_container->addTab(tab_ptr, "New Tab");
m_tabs_container->setCurrentWidget(tab_ptr);
QObject::connect(tab_ptr, &Tab::title_changed, this, &BrowserWindow::tab_title_changed);
QObject::connect(tab_ptr, &Tab::favicon_changed, this, &BrowserWindow::tab_favicon_changed);
tab_ptr->view().on_get_all_cookies = [this](auto const& url) {
return m_cookie_jar.get_all_cookies(url);
};
tab_ptr->view().on_get_named_cookie = [this](auto const& url, auto const& name) {
return m_cookie_jar.get_named_cookie(url, name);
};
tab_ptr->view().on_get_cookie = [this](auto& url, auto source) -> DeprecatedString {
return m_cookie_jar.get_cookie(url, source);
};
tab_ptr->view().on_set_cookie = [this](auto& url, auto& cookie, auto source) {
m_cookie_jar.set_cookie(url, cookie, source);
};
tab_ptr->view().on_update_cookie = [this](auto const& cookie) {
m_cookie_jar.update_cookie(cookie);
};
tab_ptr->focus_location_editor();
}
void BrowserWindow::close_tab(int index)
{
auto* tab = m_tabs_container->widget(index);
m_tabs_container->removeTab(index);
m_tabs.remove_first_matching([&](auto& entry) {
return entry == tab;
});
}
void BrowserWindow::close_current_tab()
{
auto count = m_tabs_container->count() - 1;
if (!count)
close();
else
close_tab(m_tabs_container->currentIndex());
}
int BrowserWindow::tab_index(Tab* tab)
{
return m_tabs_container->indexOf(tab);
}
void BrowserWindow::tab_title_changed(int index, QString const& title)
{
if (title.isEmpty()) {
m_tabs_container->setTabText(index, "...");
if (m_tabs_container->currentIndex() == index)
setWindowTitle("Ladybird");
} else {
m_tabs_container->setTabText(index, title);
if (m_tabs_container->currentIndex() == index)
setWindowTitle(QString("%1 - Ladybird").arg(title));
}
}
void BrowserWindow::tab_favicon_changed(int index, QIcon icon)
{
m_tabs_container->setTabIcon(index, icon);
if (m_tabs_container->currentIndex() == index)
setWindowIcon(icon);
}
void BrowserWindow::open_next_tab()
{
if (m_tabs_container->count() <= 1)
return;
auto next_index = m_tabs_container->currentIndex() + 1;
if (next_index >= m_tabs_container->count())
next_index = 0;
m_tabs_container->setCurrentIndex(next_index);
}
void BrowserWindow::open_previous_tab()
{
if (m_tabs_container->count() <= 1)
return;
auto next_index = m_tabs_container->currentIndex() - 1;
if (next_index < 0)
next_index = m_tabs_container->count() - 1;
m_tabs_container->setCurrentIndex(next_index);
}
void BrowserWindow::enable_auto_color_scheme()
{
for (auto& tab : m_tabs) {
tab.view().set_color_scheme(ColorScheme::Auto);
}
}
void BrowserWindow::enable_light_color_scheme()
{
for (auto& tab : m_tabs) {
tab.view().set_color_scheme(ColorScheme::Light);
}
}
void BrowserWindow::enable_dark_color_scheme()
{
for (auto& tab : m_tabs) {
tab.view().set_color_scheme(ColorScheme::Dark);
}
}