BrowserWindow.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. /*
  2. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Jakob-Niklas See <git@nwex.de>
  4. * Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include "BrowserWindow.h"
  9. #include "BookmarksBarWidget.h"
  10. #include "Browser.h"
  11. #include "ConsoleWidget.h"
  12. #include "CookieJar.h"
  13. #include "InspectorWidget.h"
  14. #include "Tab.h"
  15. #include <Applications/Browser/BrowserWindowGML.h>
  16. #include <LibConfig/Client.h>
  17. #include <LibCore/StandardPaths.h>
  18. #include <LibGUI/AboutDialog.h>
  19. #include <LibGUI/Application.h>
  20. #include <LibGUI/Clipboard.h>
  21. #include <LibGUI/Icon.h>
  22. #include <LibGUI/InputBox.h>
  23. #include <LibGUI/Menu.h>
  24. #include <LibGUI/Menubar.h>
  25. #include <LibGUI/MessageBox.h>
  26. #include <LibGUI/SeparatorWidget.h>
  27. #include <LibGUI/Statusbar.h>
  28. #include <LibGUI/TabWidget.h>
  29. #include <LibGUI/ToolbarContainer.h>
  30. #include <LibGUI/Widget.h>
  31. #include <LibJS/Interpreter.h>
  32. #include <LibWeb/CSS/PreferredColorScheme.h>
  33. #include <LibWeb/Dump.h>
  34. #include <LibWeb/Layout/InitialContainingBlock.h>
  35. #include <LibWeb/Loader/ResourceLoader.h>
  36. #include <LibWeb/OutOfProcessWebView.h>
  37. namespace Browser {
  38. static String bookmarks_file_path()
  39. {
  40. StringBuilder builder;
  41. builder.append(Core::StandardPaths::config_directory());
  42. builder.append("/bookmarks.json");
  43. return builder.to_string();
  44. }
  45. static String search_engines_file_path()
  46. {
  47. StringBuilder builder;
  48. builder.append(Core::StandardPaths::config_directory());
  49. builder.append("/SearchEngines.json");
  50. return builder.to_string();
  51. }
  52. BrowserWindow::BrowserWindow(CookieJar& cookie_jar, URL url)
  53. : m_cookie_jar(cookie_jar)
  54. , m_window_actions(*this)
  55. {
  56. auto app_icon = GUI::Icon::default_icon("app-browser");
  57. m_bookmarks_bar = Browser::BookmarksBarWidget::construct(Browser::bookmarks_file_path(), true);
  58. resize(640, 480);
  59. set_icon(app_icon.bitmap_for_size(16));
  60. set_title("Browser");
  61. auto& widget = set_main_widget<GUI::Widget>();
  62. widget.load_from_gml(browser_window_gml);
  63. auto& top_line = *widget.find_descendant_of_type_named<GUI::HorizontalSeparator>("top_line");
  64. m_tab_widget = *widget.find_descendant_of_type_named<GUI::TabWidget>("tab_widget");
  65. m_tab_widget->set_reorder_allowed(true);
  66. m_tab_widget->set_close_button_enabled(true);
  67. m_tab_widget->on_tab_count_change = [&top_line](size_t tab_count) {
  68. top_line.set_visible(tab_count > 1);
  69. };
  70. m_tab_widget->on_change = [this](auto& active_widget) {
  71. auto& tab = static_cast<Browser::Tab&>(active_widget);
  72. set_window_title_for_tab(tab);
  73. tab.did_become_active();
  74. };
  75. m_tab_widget->on_middle_click = [](auto& clicked_widget) {
  76. auto& tab = static_cast<Browser::Tab&>(clicked_widget);
  77. tab.on_tab_close_request(tab);
  78. };
  79. m_tab_widget->on_tab_close_click = [](auto& clicked_widget) {
  80. auto& tab = static_cast<Browser::Tab&>(clicked_widget);
  81. tab.on_tab_close_request(tab);
  82. };
  83. m_tab_widget->on_context_menu_request = [](auto& clicked_widget, const GUI::ContextMenuEvent& context_menu_event) {
  84. auto& tab = static_cast<Browser::Tab&>(clicked_widget);
  85. tab.context_menu_requested(context_menu_event.screen_position());
  86. };
  87. m_window_actions.on_create_new_tab = [this] {
  88. create_new_tab(Browser::g_home_url, true);
  89. };
  90. m_window_actions.on_next_tab = [this] {
  91. m_tab_widget->activate_next_tab();
  92. };
  93. m_window_actions.on_previous_tab = [this] {
  94. m_tab_widget->activate_previous_tab();
  95. };
  96. m_window_actions.on_about = [this] {
  97. auto app_icon = GUI::Icon::default_icon("app-browser");
  98. GUI::AboutDialog::show("Browser", app_icon.bitmap_for_size(32), this);
  99. };
  100. m_window_actions.on_show_bookmarks_bar = [](auto& action) {
  101. Browser::BookmarksBarWidget::the().set_visible(action.is_checked());
  102. Config::write_bool("Browser", "Preferences", "ShowBookmarksBar", action.is_checked());
  103. };
  104. bool show_bookmarks_bar = Config::read_bool("Browser", "Preferences", "ShowBookmarksBar", true);
  105. m_window_actions.show_bookmarks_bar_action().set_checked(show_bookmarks_bar);
  106. Browser::BookmarksBarWidget::the().set_visible(show_bookmarks_bar);
  107. build_menus();
  108. create_new_tab(move(url), true);
  109. }
  110. BrowserWindow::~BrowserWindow()
  111. {
  112. }
  113. void BrowserWindow::build_menus()
  114. {
  115. auto& file_menu = add_menu("&File");
  116. file_menu.add_action(WindowActions::the().create_new_tab_action());
  117. auto close_tab_action = GUI::CommonActions::make_close_tab_action([this](auto&) {
  118. active_tab().on_tab_close_request(active_tab());
  119. },
  120. this);
  121. file_menu.add_action(close_tab_action);
  122. file_menu.add_separator();
  123. file_menu.add_action(GUI::CommonActions::make_quit_action([](auto&) {
  124. GUI::Application::the()->quit();
  125. }));
  126. auto& view_menu = add_menu("&View");
  127. view_menu.add_action(WindowActions::the().show_bookmarks_bar_action());
  128. view_menu.add_separator();
  129. view_menu.add_action(GUI::CommonActions::make_fullscreen_action(
  130. [this](auto&) {
  131. auto& tab = active_tab();
  132. set_fullscreen(!is_fullscreen());
  133. auto is_fullscreen = this->is_fullscreen();
  134. tab_widget().set_bar_visible(!is_fullscreen && tab_widget().children().size() > 1);
  135. tab.m_toolbar_container->set_visible(!is_fullscreen);
  136. tab.m_statusbar->set_visible(!is_fullscreen);
  137. if (is_fullscreen) {
  138. tab.view().set_frame_thickness(0);
  139. } else {
  140. tab.view().set_frame_thickness(2);
  141. }
  142. },
  143. this));
  144. m_go_back_action = GUI::CommonActions::make_go_back_action([this](auto&) { active_tab().go_back(); }, this);
  145. m_go_forward_action = GUI::CommonActions::make_go_forward_action([this](auto&) { active_tab().go_forward(); }, this);
  146. m_go_home_action = GUI::CommonActions::make_go_home_action([this](auto&) { active_tab().load(g_home_url); }, this);
  147. m_go_home_action->set_status_tip("Go to home page");
  148. m_reload_action = GUI::CommonActions::make_reload_action([this](auto&) { active_tab().reload(); }, this);
  149. m_reload_action->set_status_tip("Reload current page");
  150. auto& go_menu = add_menu("&Go");
  151. go_menu.add_action(*m_go_back_action);
  152. go_menu.add_action(*m_go_forward_action);
  153. go_menu.add_action(*m_go_home_action);
  154. go_menu.add_separator();
  155. go_menu.add_action(*m_reload_action);
  156. m_copy_selection_action = GUI::CommonActions::make_copy_action([this](auto&) {
  157. auto& tab = active_tab();
  158. auto selected_text = tab.m_web_content_view->selected_text();
  159. if (!selected_text.is_empty())
  160. GUI::Clipboard::the().set_plain_text(selected_text);
  161. });
  162. m_select_all_action = GUI::CommonActions::make_select_all_action([this](auto&) {
  163. active_tab().m_web_content_view->select_all();
  164. });
  165. m_view_source_action = GUI::Action::create(
  166. "View &Source", { Mod_Ctrl, Key_U }, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/code.png").release_value_but_fixme_should_propagate_errors(), [this](auto&) {
  167. active_tab().m_web_content_view->get_source();
  168. },
  169. this);
  170. m_view_source_action->set_status_tip("View source code of the current page");
  171. m_inspect_dom_tree_action = GUI::Action::create(
  172. "Inspect &DOM Tree", { Mod_None, Key_F12 }, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/tree.png").release_value_but_fixme_should_propagate_errors(), [this](auto&) {
  173. active_tab().show_inspector_window(Tab::InspectorTarget::Document);
  174. },
  175. this);
  176. m_inspect_dom_tree_action->set_status_tip("Open inspector window for this page");
  177. m_inspect_dom_node_action = GUI::Action::create(
  178. "&Inspect Element", Gfx::Bitmap::try_load_from_file("/res/icons/16x16/inspect.png").release_value_but_fixme_should_propagate_errors(), [this](auto&) {
  179. active_tab().show_inspector_window(Tab::InspectorTarget::HoveredElement);
  180. },
  181. this);
  182. m_inspect_dom_node_action->set_status_tip("Open inspector for this element");
  183. auto& inspect_menu = add_menu("&Inspect");
  184. inspect_menu.add_action(*m_view_source_action);
  185. inspect_menu.add_action(*m_inspect_dom_tree_action);
  186. auto js_console_action = GUI::Action::create(
  187. "Open &JS Console", { Mod_Ctrl, Key_I }, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/filetype-javascript.png").release_value_but_fixme_should_propagate_errors(), [this](auto&) {
  188. active_tab().show_console_window();
  189. },
  190. this);
  191. js_console_action->set_status_tip("Open JavaScript console for this page");
  192. inspect_menu.add_action(js_console_action);
  193. auto& settings_menu = add_menu("&Settings");
  194. m_change_homepage_action = GUI::Action::create(
  195. "Set Homepage URL...", [this](auto&) {
  196. auto homepage_url = Config::read_string("Browser", "Preferences", "Home", "about:blank");
  197. if (GUI::InputBox::show(this, homepage_url, "Enter URL", "Change homepage URL") == GUI::InputBox::ExecOK) {
  198. if (URL(homepage_url).is_valid()) {
  199. Config::write_string("Browser", "Preferences", "Home", homepage_url);
  200. Browser::g_home_url = homepage_url;
  201. } else {
  202. GUI::MessageBox::show_error(this, "The URL you have entered is not valid");
  203. }
  204. }
  205. },
  206. this);
  207. settings_menu.add_action(*m_change_homepage_action);
  208. m_search_engine_actions.set_exclusive(true);
  209. auto& search_engine_menu = settings_menu.add_submenu("&Search Engine");
  210. search_engine_menu.set_icon(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/find.png").release_value_but_fixme_should_propagate_errors());
  211. bool search_engine_set = false;
  212. m_disable_search_engine_action = GUI::Action::create_checkable(
  213. "Disable", [](auto&) {
  214. g_search_engine = {};
  215. Config::write_string("Browser", "Preferences", "SearchEngine", g_search_engine);
  216. },
  217. this);
  218. search_engine_menu.add_action(*m_disable_search_engine_action);
  219. m_search_engine_actions.add_action(*m_disable_search_engine_action);
  220. m_disable_search_engine_action->set_checked(true);
  221. auto search_engines_file = Core::File::construct(Browser::search_engines_file_path());
  222. if (search_engines_file->open(Core::OpenMode::ReadOnly)) {
  223. if (auto maybe_json = JsonValue::from_string(search_engines_file->read_all()); !maybe_json.is_error() && maybe_json.value().is_array()) {
  224. auto json = maybe_json.release_value().as_array();
  225. for (auto& json_item : json.values()) {
  226. if (!json_item.is_object())
  227. continue;
  228. auto search_engine = json_item.as_object();
  229. auto name = search_engine.get("title").to_string();
  230. auto url_format = search_engine.get("url_format").to_string();
  231. auto action = GUI::Action::create_checkable(
  232. name, [&, url_format](auto&) {
  233. g_search_engine = url_format;
  234. Config::write_string("Browser", "Preferences", "SearchEngine", g_search_engine);
  235. },
  236. this);
  237. search_engine_menu.add_action(action);
  238. m_search_engine_actions.add_action(action);
  239. if (g_search_engine == url_format) {
  240. action->set_checked(true);
  241. search_engine_set = true;
  242. }
  243. action->set_status_tip(url_format);
  244. }
  245. }
  246. }
  247. auto custom_search_engine_action = GUI::Action::create_checkable("Custom...", [&](auto& action) {
  248. String search_engine;
  249. if (GUI::InputBox::show(this, search_engine, "Enter URL template:", "Custom Search Engine", "https://host/search?q={}") != GUI::InputBox::ExecOK || search_engine.is_empty()) {
  250. m_disable_search_engine_action->activate();
  251. return;
  252. }
  253. auto argument_count = search_engine.count("{}"sv);
  254. if (argument_count != 1) {
  255. GUI::MessageBox::show(this, "Invalid format, must contain '{}' once!", "Error", GUI::MessageBox::Type::Error);
  256. m_disable_search_engine_action->activate();
  257. return;
  258. }
  259. g_search_engine = search_engine;
  260. Config::write_string("Browser", "Preferences", "SearchEngine", g_search_engine);
  261. action.set_status_tip(search_engine);
  262. });
  263. search_engine_menu.add_action(custom_search_engine_action);
  264. m_search_engine_actions.add_action(custom_search_engine_action);
  265. if (!search_engine_set && !g_search_engine.is_empty()) {
  266. custom_search_engine_action->set_checked(true);
  267. custom_search_engine_action->set_status_tip(g_search_engine);
  268. }
  269. auto& color_scheme_menu = settings_menu.add_submenu("&Color Scheme");
  270. color_scheme_menu.set_icon(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/color-chooser.png").release_value_but_fixme_should_propagate_errors());
  271. {
  272. auto current_setting = Web::CSS::preferred_color_scheme_from_string(Config::read_string("Browser", "Preferences", "ColorScheme", "auto"));
  273. m_color_scheme_actions.set_exclusive(true);
  274. auto add_color_scheme_action = [&](auto& name, Web::CSS::PreferredColorScheme preference_value) {
  275. auto action = GUI::Action::create_checkable(
  276. name, [=, this](auto&) {
  277. Config::write_string("Browser", "Preferences", "ColorScheme", Web::CSS::preferred_color_scheme_to_string(preference_value));
  278. active_tab().m_web_content_view->set_preferred_color_scheme(preference_value);
  279. },
  280. this);
  281. if (current_setting == preference_value)
  282. action->set_checked(true);
  283. color_scheme_menu.add_action(action);
  284. m_color_scheme_actions.add_action(action);
  285. };
  286. add_color_scheme_action("Follow system theme", Web::CSS::PreferredColorScheme::Auto);
  287. add_color_scheme_action("Light", Web::CSS::PreferredColorScheme::Light);
  288. add_color_scheme_action("Dark", Web::CSS::PreferredColorScheme::Dark);
  289. }
  290. auto& debug_menu = add_menu("&Debug");
  291. debug_menu.add_action(GUI::Action::create(
  292. "Dump &DOM Tree", Gfx::Bitmap::try_load_from_file("/res/icons/16x16/tree.png").release_value_but_fixme_should_propagate_errors(), [this](auto&) {
  293. active_tab().m_web_content_view->debug_request("dump-dom-tree");
  294. },
  295. this));
  296. debug_menu.add_action(GUI::Action::create(
  297. "Dump &Layout Tree", [this](auto&) {
  298. active_tab().m_web_content_view->debug_request("dump-layout-tree");
  299. },
  300. this));
  301. debug_menu.add_action(GUI::Action::create(
  302. "Dump &Style Sheets", [this](auto&) {
  303. active_tab().m_web_content_view->debug_request("dump-style-sheets");
  304. },
  305. this));
  306. debug_menu.add_action(GUI::Action::create("Dump &History", { Mod_Ctrl, Key_H }, [this](auto&) {
  307. active_tab().m_history.dump();
  308. }));
  309. debug_menu.add_action(GUI::Action::create("Dump C&ookies", Gfx::Bitmap::try_load_from_file("/res/icons/16x16/cookie.png").release_value_but_fixme_should_propagate_errors(), [this](auto&) {
  310. auto& tab = active_tab();
  311. if (tab.on_dump_cookies)
  312. tab.on_dump_cookies();
  313. }));
  314. debug_menu.add_separator();
  315. auto line_box_borders_action = GUI::Action::create_checkable(
  316. "Line &Box Borders", [this](auto& action) {
  317. active_tab().m_web_content_view->debug_request("set-line-box-borders", action.is_checked() ? "on" : "off");
  318. },
  319. this);
  320. line_box_borders_action->set_checked(false);
  321. debug_menu.add_action(line_box_borders_action);
  322. debug_menu.add_separator();
  323. debug_menu.add_action(GUI::Action::create("Collect &Garbage", { Mod_Ctrl | Mod_Shift, Key_G }, [this](auto&) {
  324. active_tab().m_web_content_view->debug_request("collect-garbage");
  325. }));
  326. debug_menu.add_action(GUI::Action::create("Clear &Cache", { Mod_Ctrl | Mod_Shift, Key_C }, [this](auto&) {
  327. active_tab().m_web_content_view->debug_request("clear-cache");
  328. }));
  329. m_user_agent_spoof_actions.set_exclusive(true);
  330. auto& spoof_user_agent_menu = debug_menu.add_submenu("Spoof &User Agent");
  331. m_disable_user_agent_spoofing = GUI::Action::create_checkable("Disabled", [this](auto&) {
  332. active_tab().m_web_content_view->debug_request("spoof-user-agent", Web::default_user_agent);
  333. });
  334. m_disable_user_agent_spoofing->set_status_tip(Web::default_user_agent);
  335. spoof_user_agent_menu.add_action(*m_disable_user_agent_spoofing);
  336. m_user_agent_spoof_actions.add_action(*m_disable_user_agent_spoofing);
  337. m_disable_user_agent_spoofing->set_checked(true);
  338. auto add_user_agent = [this, &spoof_user_agent_menu](auto& name, auto& user_agent) {
  339. auto action = GUI::Action::create_checkable(name, [this, user_agent](auto&) {
  340. active_tab().m_web_content_view->debug_request("spoof-user-agent", user_agent);
  341. });
  342. action->set_status_tip(user_agent);
  343. spoof_user_agent_menu.add_action(action);
  344. m_user_agent_spoof_actions.add_action(action);
  345. };
  346. 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");
  347. add_user_agent("Firefox Linux Desktop", "Mozilla/5.0 (X11; Linux i686; rv:87.0) Gecko/20100101 Firefox/87.0");
  348. 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");
  349. 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");
  350. add_user_agent("Firefox Android Mobile", "Mozilla/5.0 (Android 11; Mobile; rv:68.0) Gecko/68.0 Firefox/86.0");
  351. 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");
  352. auto custom_user_agent = GUI::Action::create_checkable("Custom...", [this](auto& action) {
  353. String user_agent;
  354. if (GUI::InputBox::show(this, user_agent, "Enter User Agent:", "Custom User Agent") != GUI::InputBox::ExecOK || user_agent.is_empty() || user_agent.is_null()) {
  355. m_disable_user_agent_spoofing->activate();
  356. return;
  357. }
  358. active_tab().m_web_content_view->debug_request("spoof-user-agent", user_agent);
  359. action.set_status_tip(user_agent);
  360. });
  361. spoof_user_agent_menu.add_action(custom_user_agent);
  362. m_user_agent_spoof_actions.add_action(custom_user_agent);
  363. debug_menu.add_separator();
  364. auto same_origin_policy_action = GUI::Action::create_checkable(
  365. "Enable Same &Origin Policy", [this](auto& action) {
  366. active_tab().m_web_content_view->debug_request("same-origin-policy", action.is_checked() ? "on" : "off");
  367. },
  368. this);
  369. same_origin_policy_action->set_checked(false);
  370. debug_menu.add_action(same_origin_policy_action);
  371. auto& help_menu = add_menu("&Help");
  372. help_menu.add_action(WindowActions::the().about_action());
  373. }
  374. GUI::TabWidget& BrowserWindow::tab_widget()
  375. {
  376. return *m_tab_widget;
  377. }
  378. Tab& BrowserWindow::active_tab()
  379. {
  380. return verify_cast<Tab>(*tab_widget().active_widget());
  381. }
  382. void BrowserWindow::set_window_title_for_tab(Tab const& tab)
  383. {
  384. auto& title = tab.title();
  385. auto url = tab.url();
  386. set_title(String::formatted("{} - Browser", title.is_empty() ? url.to_string() : title));
  387. }
  388. void BrowserWindow::create_new_tab(URL url, bool activate)
  389. {
  390. auto& new_tab = m_tab_widget->add_tab<Browser::Tab>("New tab", *this);
  391. m_tab_widget->set_bar_visible(!is_fullscreen() && m_tab_widget->children().size() > 1);
  392. new_tab.on_title_change = [this, &new_tab](auto& title) {
  393. m_tab_widget->set_tab_title(new_tab, title);
  394. if (m_tab_widget->active_widget() == &new_tab)
  395. set_window_title_for_tab(new_tab);
  396. };
  397. new_tab.on_favicon_change = [this, &new_tab](auto& bitmap) {
  398. m_tab_widget->set_tab_icon(new_tab, &bitmap);
  399. };
  400. new_tab.on_tab_open_request = [this](auto& url) {
  401. create_new_tab(url, true);
  402. };
  403. new_tab.on_tab_close_request = [this](auto& tab) {
  404. m_tab_widget->deferred_invoke([this, &tab] {
  405. m_tab_widget->remove_tab(tab);
  406. m_tab_widget->set_bar_visible(!is_fullscreen() && m_tab_widget->children().size() > 1);
  407. if (m_tab_widget->children().is_empty())
  408. close();
  409. });
  410. };
  411. new_tab.on_tab_close_other_request = [this](auto& tab) {
  412. m_tab_widget->deferred_invoke([this, &tab] {
  413. m_tab_widget->remove_all_tabs_except(tab);
  414. VERIFY(m_tab_widget->children().size() == 1);
  415. m_tab_widget->set_bar_visible(false);
  416. });
  417. };
  418. new_tab.on_get_cookie = [this](auto& url, auto source) -> String {
  419. return m_cookie_jar.get_cookie(url, source);
  420. };
  421. new_tab.on_set_cookie = [this](auto& url, auto& cookie, auto source) {
  422. m_cookie_jar.set_cookie(url, cookie, source);
  423. };
  424. new_tab.on_dump_cookies = [this]() {
  425. m_cookie_jar.dump_cookies();
  426. };
  427. new_tab.load(url);
  428. dbgln_if(SPAM_DEBUG, "Added new tab {:p}, loading {}", &new_tab, url);
  429. if (activate)
  430. m_tab_widget->set_active_widget(&new_tab);
  431. }
  432. }