BrowserWindow.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. /*
  2. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Jakob-Niklas See <git@nwex.de>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include "BrowserWindow.h"
  8. #include "BookmarksBarWidget.h"
  9. #include "Browser.h"
  10. #include "ConsoleWidget.h"
  11. #include "CookieJar.h"
  12. #include "InspectorWidget.h"
  13. #include "Tab.h"
  14. #include <Applications/Browser/BrowserWindowGML.h>
  15. #include <LibCore/ConfigFile.h>
  16. #include <LibCore/StandardPaths.h>
  17. #include <LibGUI/AboutDialog.h>
  18. #include <LibGUI/Application.h>
  19. #include <LibGUI/Clipboard.h>
  20. #include <LibGUI/Icon.h>
  21. #include <LibGUI/InputBox.h>
  22. #include <LibGUI/Menu.h>
  23. #include <LibGUI/Menubar.h>
  24. #include <LibGUI/MessageBox.h>
  25. #include <LibGUI/SeparatorWidget.h>
  26. #include <LibGUI/Statusbar.h>
  27. #include <LibGUI/TabWidget.h>
  28. #include <LibGUI/ToolbarContainer.h>
  29. #include <LibGUI/Widget.h>
  30. #include <LibJS/Interpreter.h>
  31. #include <LibWeb/Dump.h>
  32. #include <LibWeb/Layout/InitialContainingBlockBox.h>
  33. #include <LibWeb/Loader/ResourceLoader.h>
  34. #include <LibWeb/OutOfProcessWebView.h>
  35. namespace Browser {
  36. static String bookmarks_file_path()
  37. {
  38. StringBuilder builder;
  39. builder.append(Core::StandardPaths::config_directory());
  40. builder.append("/bookmarks.json");
  41. return builder.to_string();
  42. }
  43. BrowserWindow::BrowserWindow(CookieJar& cookie_jar, URL url)
  44. : m_cookie_jar(cookie_jar)
  45. , m_window_actions(*this)
  46. {
  47. auto app_icon = GUI::Icon::default_icon("app-browser");
  48. m_bookmarks_bar = Browser::BookmarksBarWidget::construct(Browser::bookmarks_file_path(), true);
  49. resize(640, 480);
  50. set_icon(app_icon.bitmap_for_size(16));
  51. set_title("Browser");
  52. auto& widget = set_main_widget<GUI::Widget>();
  53. widget.load_from_gml(browser_window_gml);
  54. auto& top_line = *widget.find_descendant_of_type_named<GUI::HorizontalSeparator>("top_line");
  55. m_tab_widget = *widget.find_descendant_of_type_named<GUI::TabWidget>("tab_widget");
  56. m_tab_widget->set_close_button_enabled(true);
  57. m_tab_widget->on_tab_count_change = [&top_line](size_t tab_count) {
  58. top_line.set_visible(tab_count > 1);
  59. };
  60. m_tab_widget->on_change = [this](auto& active_widget) {
  61. auto& tab = static_cast<Browser::Tab&>(active_widget);
  62. set_window_title_for_tab(tab);
  63. tab.did_become_active();
  64. };
  65. m_tab_widget->on_middle_click = [](auto& clicked_widget) {
  66. auto& tab = static_cast<Browser::Tab&>(clicked_widget);
  67. tab.on_tab_close_request(tab);
  68. };
  69. m_tab_widget->on_tab_close_click = [](auto& clicked_widget) {
  70. auto& tab = static_cast<Browser::Tab&>(clicked_widget);
  71. tab.on_tab_close_request(tab);
  72. };
  73. m_tab_widget->on_context_menu_request = [](auto& clicked_widget, const GUI::ContextMenuEvent& context_menu_event) {
  74. auto& tab = static_cast<Browser::Tab&>(clicked_widget);
  75. tab.context_menu_requested(context_menu_event.screen_position());
  76. };
  77. m_window_actions.on_create_new_tab = [this] {
  78. create_new_tab(Browser::g_home_url, true);
  79. };
  80. m_window_actions.on_next_tab = [this] {
  81. m_tab_widget->activate_next_tab();
  82. };
  83. m_window_actions.on_previous_tab = [this] {
  84. m_tab_widget->activate_previous_tab();
  85. };
  86. m_window_actions.on_about = [this] {
  87. auto app_icon = GUI::Icon::default_icon("app-browser");
  88. GUI::AboutDialog::show("Browser", app_icon.bitmap_for_size(32), this);
  89. };
  90. m_window_actions.on_show_bookmarks_bar = [](auto& action) {
  91. Browser::BookmarksBarWidget::the().set_visible(action.is_checked());
  92. };
  93. m_window_actions.show_bookmarks_bar_action().set_checked(true);
  94. build_menus();
  95. create_new_tab(move(url), true);
  96. }
  97. BrowserWindow::~BrowserWindow()
  98. {
  99. }
  100. void BrowserWindow::build_menus()
  101. {
  102. auto& file_menu = add_menu("&File");
  103. file_menu.add_action(WindowActions::the().create_new_tab_action());
  104. auto close_tab_action = GUI::Action::create(
  105. "&Close Tab", { Mod_Ctrl, Key_W }, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/close-tab.png"), [this](auto&) {
  106. active_tab().on_tab_close_request(active_tab());
  107. },
  108. this);
  109. close_tab_action->set_status_tip("Close current tab");
  110. file_menu.add_action(close_tab_action);
  111. file_menu.add_separator();
  112. file_menu.add_action(GUI::CommonActions::make_quit_action([](auto&) {
  113. GUI::Application::the()->quit();
  114. }));
  115. auto& view_menu = add_menu("&View");
  116. view_menu.add_action(WindowActions::the().show_bookmarks_bar_action());
  117. view_menu.add_separator();
  118. view_menu.add_action(GUI::CommonActions::make_fullscreen_action(
  119. [this](auto&) {
  120. auto& tab = active_tab();
  121. set_fullscreen(!is_fullscreen());
  122. auto is_fullscreen = this->is_fullscreen();
  123. tab_widget().set_bar_visible(!is_fullscreen && tab_widget().children().size() > 1);
  124. tab.m_toolbar_container->set_visible(!is_fullscreen);
  125. tab.m_statusbar->set_visible(!is_fullscreen);
  126. if (is_fullscreen) {
  127. tab.view().set_frame_thickness(0);
  128. } else {
  129. tab.view().set_frame_thickness(2);
  130. }
  131. },
  132. this));
  133. m_go_back_action = GUI::CommonActions::make_go_back_action([this](auto&) { active_tab().go_back(); }, this);
  134. m_go_forward_action = GUI::CommonActions::make_go_forward_action([this](auto&) { active_tab().go_forward(); }, this);
  135. m_go_home_action = GUI::CommonActions::make_go_home_action([this](auto&) { active_tab().load(g_home_url); }, this);
  136. m_go_home_action->set_status_tip("Go to home page");
  137. m_reload_action = GUI::CommonActions::make_reload_action([this](auto&) { active_tab().reload(); }, this);
  138. m_reload_action->set_status_tip("Reload current page");
  139. auto& go_menu = add_menu("&Go");
  140. go_menu.add_action(*m_go_back_action);
  141. go_menu.add_action(*m_go_forward_action);
  142. go_menu.add_action(*m_go_home_action);
  143. go_menu.add_separator();
  144. go_menu.add_action(*m_reload_action);
  145. m_copy_selection_action = GUI::CommonActions::make_copy_action([this](auto&) {
  146. auto& tab = active_tab();
  147. String selected_text;
  148. if (tab.m_type == Tab::Type::InProcessWebView)
  149. selected_text = tab.m_page_view->selected_text();
  150. else
  151. selected_text = tab.m_web_content_view->selected_text();
  152. if (!selected_text.is_empty())
  153. GUI::Clipboard::the().set_plain_text(selected_text);
  154. });
  155. m_select_all_action = GUI::CommonActions::make_select_all_action([this](auto&) {
  156. auto& tab = active_tab();
  157. if (tab.m_type == Tab::Type::InProcessWebView)
  158. tab.m_page_view->select_all();
  159. else
  160. tab.m_web_content_view->select_all();
  161. });
  162. m_view_source_action = GUI::Action::create(
  163. "View &Source", { Mod_Ctrl, Key_U }, [this](auto&) {
  164. auto& tab = active_tab();
  165. if (tab.m_type == Tab::Type::InProcessWebView) {
  166. VERIFY(tab.m_page_view->document());
  167. auto url = tab.m_page_view->document()->url();
  168. auto source = tab.m_page_view->document()->source();
  169. tab.view_source(url, source);
  170. } else {
  171. tab.m_web_content_view->get_source();
  172. }
  173. },
  174. this);
  175. m_view_source_action->set_status_tip("View source code of the current page");
  176. m_inspect_dom_tree_action = GUI::Action::create(
  177. "Inspect &DOM Tree", { Mod_None, Key_F12 }, [this](auto&) {
  178. auto& tab = active_tab();
  179. if (tab.m_type == Tab::Type::InProcessWebView) {
  180. if (!tab.m_dom_inspector_window) {
  181. tab.m_dom_inspector_window = GUI::Window::construct(this);
  182. tab.m_dom_inspector_window->resize(300, 500);
  183. tab.m_dom_inspector_window->set_title("DOM inspector");
  184. tab.m_dom_inspector_window->set_icon(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/inspector-object.png"));
  185. tab.m_dom_inspector_window->set_main_widget<InspectorWidget>();
  186. tab.m_dom_inspector_window->on_close = [&]() {
  187. tab.m_page_view->document()->set_inspected_node(nullptr);
  188. };
  189. }
  190. auto* inspector_widget = static_cast<InspectorWidget*>(tab.m_dom_inspector_window->main_widget());
  191. inspector_widget->set_document(tab.m_page_view->document());
  192. tab.m_dom_inspector_window->show();
  193. tab.m_dom_inspector_window->move_to_front();
  194. } else {
  195. tab.m_web_content_view->inspect_dom_tree();
  196. }
  197. },
  198. this);
  199. m_inspect_dom_tree_action->set_status_tip("Open DOM inspector window for this page");
  200. auto& inspect_menu = add_menu("&Inspect");
  201. inspect_menu.add_action(*m_view_source_action);
  202. inspect_menu.add_action(*m_inspect_dom_tree_action);
  203. auto js_console_action = GUI::Action::create(
  204. "Open &JS Console", { Mod_Ctrl, Key_I }, [this](auto&) {
  205. auto& tab = active_tab();
  206. if (tab.m_type == Tab::Type::InProcessWebView) {
  207. if (!tab.m_console_window) {
  208. tab.m_console_window = GUI::Window::construct(this);
  209. tab.m_console_window->resize(500, 300);
  210. tab.m_console_window->set_title("JS Console");
  211. tab.m_console_window->set_icon(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/filetype-javascript.png"));
  212. tab.m_console_window->set_main_widget<ConsoleWidget>();
  213. }
  214. auto* console_widget = static_cast<ConsoleWidget*>(tab.m_console_window->main_widget());
  215. console_widget->set_interpreter(tab.m_page_view->document()->interpreter().make_weak_ptr());
  216. tab.m_console_window->show();
  217. tab.m_console_window->move_to_front();
  218. } else {
  219. if (!tab.m_console_window) {
  220. tab.m_console_window = GUI::Window::construct(this);
  221. tab.m_console_window->resize(500, 300);
  222. tab.m_console_window->set_title("JS Console");
  223. tab.m_console_window->set_icon(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/filetype-javascript.png"));
  224. tab.m_console_window->set_main_widget<ConsoleWidget>();
  225. }
  226. auto* console_widget = static_cast<ConsoleWidget*>(tab.m_console_window->main_widget());
  227. console_widget->on_js_input = [&tab](const String& js_source) {
  228. tab.m_web_content_view->js_console_input(js_source);
  229. };
  230. console_widget->clear_output();
  231. tab.m_web_content_view->js_console_initialize();
  232. tab.m_console_window->show();
  233. tab.m_console_window->move_to_front();
  234. }
  235. },
  236. this);
  237. js_console_action->set_status_tip("Open JavaScript console for this page");
  238. inspect_menu.add_action(js_console_action);
  239. auto& settings_menu = add_menu("&Settings");
  240. m_change_homepage_action = GUI::Action::create(
  241. "Set Homepage URL", [this](auto&) {
  242. auto config = Core::ConfigFile::open_for_app("Browser", Core::ConfigFile::AllowWriting::Yes);
  243. String homepage_url = config->read_entry("Preferences", "Home", "about:blank");
  244. if (GUI::InputBox::show(this, homepage_url, "Enter URL", "Change homepage URL") == GUI::InputBox::ExecOK) {
  245. if (URL(homepage_url).is_valid()) {
  246. config->write_entry("Preferences", "Home", homepage_url);
  247. Browser::g_home_url = homepage_url;
  248. } else {
  249. GUI::MessageBox::show_error(this, "The URL you have entered is not valid");
  250. }
  251. }
  252. },
  253. this);
  254. settings_menu.add_action(*m_change_homepage_action);
  255. m_search_engine_actions.set_exclusive(true);
  256. auto& search_engine_menu = settings_menu.add_submenu("&Search Engine");
  257. bool search_engine_set = false;
  258. auto add_search_engine = [&](auto& name, auto& url_format) {
  259. auto action = GUI::Action::create_checkable(
  260. name, [&](auto&) {
  261. g_search_engine = url_format;
  262. auto config = Core::ConfigFile::open_for_app("Browser", Core::ConfigFile::AllowWriting::Yes);
  263. config->write_entry("Preferences", "SearchEngine", g_search_engine);
  264. },
  265. this);
  266. search_engine_menu.add_action(action);
  267. m_search_engine_actions.add_action(action);
  268. if (g_search_engine == url_format) {
  269. action->set_checked(true);
  270. search_engine_set = true;
  271. }
  272. action->set_status_tip(url_format);
  273. };
  274. m_disable_search_engine_action = GUI::Action::create_checkable(
  275. "Disable", [](auto&) {
  276. g_search_engine = {};
  277. auto config = Core::ConfigFile::open_for_app("Browser", Core::ConfigFile::AllowWriting::Yes);
  278. config->write_entry("Preferences", "SearchEngine", g_search_engine);
  279. },
  280. this);
  281. search_engine_menu.add_action(*m_disable_search_engine_action);
  282. m_search_engine_actions.add_action(*m_disable_search_engine_action);
  283. m_disable_search_engine_action->set_checked(true);
  284. add_search_engine("Bing", "https://www.bing.com/search?q={}");
  285. add_search_engine("DuckDuckGo", "https://duckduckgo.com/?q={}");
  286. add_search_engine("FrogFind", "http://frogfind.com/?q={}");
  287. add_search_engine("GitHub", "https://github.com/search?q={}");
  288. add_search_engine("Google", "https://google.com/search?q={}");
  289. add_search_engine("Yandex", "https://yandex.com/search/?text={}");
  290. auto custom_search_engine_action = GUI::Action::create_checkable("Custom...", [&](auto& action) {
  291. String search_engine;
  292. if (GUI::InputBox::show(this, search_engine, "Enter URL template:", "Custom Search Engine", "https://host/search?q={}") != GUI::InputBox::ExecOK || search_engine.is_empty()) {
  293. m_disable_search_engine_action->activate();
  294. return;
  295. }
  296. int argument_count = search_engine.replace("{}", "{}", true);
  297. if (argument_count != 1) {
  298. GUI::MessageBox::show(this, "Invalid format, must contain '{}' once!", "Error", GUI::MessageBox::Type::Error);
  299. m_disable_search_engine_action->activate();
  300. return;
  301. }
  302. g_search_engine = search_engine;
  303. auto config = Core::ConfigFile::open_for_app("Browser", Core::ConfigFile::AllowWriting::Yes);
  304. config->write_entry("Preferences", "SearchEngine", g_search_engine);
  305. action.set_status_tip(search_engine);
  306. });
  307. search_engine_menu.add_action(custom_search_engine_action);
  308. m_search_engine_actions.add_action(custom_search_engine_action);
  309. if (!search_engine_set && !g_search_engine.is_empty()) {
  310. custom_search_engine_action->set_checked(true);
  311. custom_search_engine_action->set_status_tip(g_search_engine);
  312. }
  313. auto& debug_menu = add_menu("&Debug");
  314. debug_menu.add_action(GUI::Action::create(
  315. "Dump &DOM Tree", [this](auto&) {
  316. auto& tab = active_tab();
  317. if (tab.m_type == Tab::Type::InProcessWebView) {
  318. Web::dump_tree(*tab.m_page_view->document());
  319. } else {
  320. tab.m_web_content_view->debug_request("dump-dom-tree");
  321. }
  322. },
  323. this));
  324. debug_menu.add_action(GUI::Action::create(
  325. "Dump &Layout Tree", [this](auto&) {
  326. auto& tab = active_tab();
  327. if (tab.m_type == Tab::Type::InProcessWebView) {
  328. Web::dump_tree(*tab.m_page_view->document()->layout_node());
  329. } else {
  330. tab.m_web_content_view->debug_request("dump-layout-tree");
  331. }
  332. },
  333. this));
  334. debug_menu.add_action(GUI::Action::create(
  335. "Dump &Style Sheets", [this](auto&) {
  336. auto& tab = active_tab();
  337. if (tab.m_type == Tab::Type::InProcessWebView) {
  338. for (auto& sheet : tab.m_page_view->document()->style_sheets().sheets()) {
  339. Web::dump_sheet(sheet);
  340. }
  341. } else {
  342. tab.m_web_content_view->debug_request("dump-style-sheets");
  343. }
  344. },
  345. this));
  346. debug_menu.add_action(GUI::Action::create("Dump &History", { Mod_Ctrl, Key_H }, [this](auto&) {
  347. active_tab().m_history.dump();
  348. }));
  349. debug_menu.add_action(GUI::Action::create("Dump C&ookies", [this](auto&) {
  350. auto& tab = active_tab();
  351. if (tab.on_dump_cookies)
  352. tab.on_dump_cookies();
  353. }));
  354. debug_menu.add_separator();
  355. auto line_box_borders_action = GUI::Action::create_checkable(
  356. "Line &Box Borders", [this](auto& action) {
  357. auto& tab = active_tab();
  358. if (tab.m_type == Tab::Type::InProcessWebView) {
  359. tab.m_page_view->set_should_show_line_box_borders(action.is_checked());
  360. tab.m_page_view->update();
  361. } else {
  362. tab.m_web_content_view->debug_request("set-line-box-borders", action.is_checked() ? "on" : "off");
  363. }
  364. },
  365. this);
  366. line_box_borders_action->set_checked(false);
  367. debug_menu.add_action(line_box_borders_action);
  368. debug_menu.add_separator();
  369. debug_menu.add_action(GUI::Action::create("Collect &Garbage", { Mod_Ctrl | Mod_Shift, Key_G }, [this](auto&) {
  370. auto& tab = active_tab();
  371. if (tab.m_type == Tab::Type::InProcessWebView) {
  372. if (auto* document = tab.m_page_view->document()) {
  373. document->interpreter().heap().collect_garbage(JS::Heap::CollectionType::CollectGarbage, true);
  374. }
  375. } else {
  376. tab.m_web_content_view->debug_request("collect-garbage");
  377. }
  378. }));
  379. debug_menu.add_action(GUI::Action::create("Clear &Cache", { Mod_Ctrl | Mod_Shift, Key_C }, [this](auto&) {
  380. auto& tab = active_tab();
  381. if (tab.m_type == Tab::Type::InProcessWebView) {
  382. Web::ResourceLoader::the().clear_cache();
  383. } else {
  384. tab.m_web_content_view->debug_request("clear-cache");
  385. }
  386. }));
  387. m_user_agent_spoof_actions.set_exclusive(true);
  388. auto& spoof_user_agent_menu = debug_menu.add_submenu("Spoof &User Agent");
  389. m_disable_user_agent_spoofing = GUI::Action::create_checkable("Disabled", [this](auto&) {
  390. auto& tab = active_tab();
  391. if (tab.m_type == Tab::Type::InProcessWebView) {
  392. Web::ResourceLoader::the().set_user_agent(Web::default_user_agent);
  393. } else {
  394. tab.m_web_content_view->debug_request("spoof-user-agent", Web::default_user_agent);
  395. }
  396. });
  397. m_disable_user_agent_spoofing->set_status_tip(Web::default_user_agent);
  398. spoof_user_agent_menu.add_action(*m_disable_user_agent_spoofing);
  399. m_user_agent_spoof_actions.add_action(*m_disable_user_agent_spoofing);
  400. m_disable_user_agent_spoofing->set_checked(true);
  401. auto add_user_agent = [this, &spoof_user_agent_menu](auto& name, auto& user_agent) {
  402. auto action = GUI::Action::create_checkable(name, [this, user_agent](auto&) {
  403. auto& tab = active_tab();
  404. if (tab.m_type == Tab::Type::InProcessWebView) {
  405. Web::ResourceLoader::the().set_user_agent(user_agent);
  406. } else {
  407. tab.m_web_content_view->debug_request("spoof-user-agent", user_agent);
  408. }
  409. });
  410. action->set_status_tip(user_agent);
  411. spoof_user_agent_menu.add_action(action);
  412. m_user_agent_spoof_actions.add_action(action);
  413. };
  414. 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");
  415. add_user_agent("Firefox Linux Desktop", "Mozilla/5.0 (X11; Linux i686; rv:87.0) Gecko/20100101 Firefox/87.0");
  416. 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");
  417. 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");
  418. add_user_agent("Firefox Android Mobile", "Mozilla/5.0 (Android 11; Mobile; rv:68.0) Gecko/68.0 Firefox/86.0");
  419. 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");
  420. auto custom_user_agent = GUI::Action::create_checkable("Custom...", [this](auto& action) {
  421. auto& tab = active_tab();
  422. String user_agent;
  423. if (GUI::InputBox::show(this, user_agent, "Enter User Agent:", "Custom User Agent") != GUI::InputBox::ExecOK || user_agent.is_empty() || user_agent.is_null()) {
  424. m_disable_user_agent_spoofing->activate();
  425. return;
  426. }
  427. if (tab.m_type == Tab::Type::InProcessWebView) {
  428. Web::ResourceLoader::the().set_user_agent(user_agent);
  429. } else {
  430. tab.m_web_content_view->debug_request("spoof-user-agent", user_agent);
  431. }
  432. action.set_status_tip(user_agent);
  433. });
  434. spoof_user_agent_menu.add_action(custom_user_agent);
  435. m_user_agent_spoof_actions.add_action(custom_user_agent);
  436. auto& help_menu = add_menu("&Help");
  437. help_menu.add_action(WindowActions::the().about_action());
  438. }
  439. GUI::TabWidget& BrowserWindow::tab_widget()
  440. {
  441. return *m_tab_widget;
  442. }
  443. Tab& BrowserWindow::active_tab()
  444. {
  445. return verify_cast<Tab>(*tab_widget().active_widget());
  446. }
  447. void BrowserWindow::set_window_title_for_tab(Tab const& tab)
  448. {
  449. auto& title = tab.title();
  450. auto url = tab.url();
  451. set_title(String::formatted("{} - Browser", title.is_empty() ? url.to_string() : title));
  452. }
  453. void BrowserWindow::create_new_tab(URL url, bool activate)
  454. {
  455. auto type = Browser::g_single_process ? Browser::Tab::Type::InProcessWebView : Browser::Tab::Type::OutOfProcessWebView;
  456. auto& new_tab = m_tab_widget->add_tab<Browser::Tab>("New tab", *this, type);
  457. m_tab_widget->set_bar_visible(!is_fullscreen() && m_tab_widget->children().size() > 1);
  458. new_tab.on_title_change = [this, &new_tab](auto& title) {
  459. m_tab_widget->set_tab_title(new_tab, title);
  460. if (m_tab_widget->active_widget() == &new_tab)
  461. set_window_title_for_tab(new_tab);
  462. };
  463. new_tab.on_favicon_change = [this, &new_tab](auto& bitmap) {
  464. m_tab_widget->set_tab_icon(new_tab, &bitmap);
  465. };
  466. new_tab.on_tab_open_request = [this](auto& url) {
  467. create_new_tab(url, true);
  468. };
  469. new_tab.on_tab_close_request = [this](auto& tab) {
  470. m_tab_widget->deferred_invoke([this, &tab](auto&) {
  471. m_tab_widget->remove_tab(tab);
  472. m_tab_widget->set_bar_visible(!is_fullscreen() && m_tab_widget->children().size() > 1);
  473. if (m_tab_widget->children().is_empty())
  474. close();
  475. });
  476. };
  477. new_tab.on_tab_close_other_request = [this](auto& tab) {
  478. m_tab_widget->deferred_invoke([this, &tab](auto&) {
  479. m_tab_widget->remove_all_tabs_except(tab);
  480. VERIFY(m_tab_widget->children().size() == 1);
  481. m_tab_widget->set_bar_visible(false);
  482. });
  483. };
  484. new_tab.on_get_cookie = [this](auto& url, auto source) -> String {
  485. return m_cookie_jar.get_cookie(url, source);
  486. };
  487. new_tab.on_set_cookie = [this](auto& url, auto& cookie, auto source) {
  488. m_cookie_jar.set_cookie(url, cookie, source);
  489. };
  490. new_tab.on_dump_cookies = [this]() {
  491. m_cookie_jar.dump_cookies();
  492. };
  493. new_tab.load(url);
  494. dbgln_if(SPAM_DEBUG, "Added new tab {:p}, loading {}", &new_tab, url);
  495. if (activate)
  496. m_tab_widget->set_active_widget(&new_tab);
  497. }
  498. }