BrowserWindow.cpp 24 KB

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