BrowserWindow.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  1. /*
  2. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, networkException <networkexception@serenityos.org>
  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 "InspectorWidget.h"
  13. #include "Tab.h"
  14. #include <Applications/Browser/BrowserWindowGML.h>
  15. #include <Applications/BrowserSettings/Defaults.h>
  16. #include <LibConfig/Client.h>
  17. #include <LibCore/StandardPaths.h>
  18. #include <LibDesktop/Launcher.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/Process.h>
  27. #include <LibGUI/SeparatorWidget.h>
  28. #include <LibGUI/Statusbar.h>
  29. #include <LibGUI/TabWidget.h>
  30. #include <LibGUI/ToolbarContainer.h>
  31. #include <LibGUI/Widget.h>
  32. #include <LibWeb/CSS/PreferredColorScheme.h>
  33. #include <LibWeb/Dump.h>
  34. #include <LibWeb/Layout/Viewport.h>
  35. #include <LibWeb/Loader/ResourceLoader.h>
  36. #include <LibWebView/CookieJar.h>
  37. #include <LibWebView/OutOfProcessWebView.h>
  38. #include <LibWebView/SearchEngine.h>
  39. #include <LibWebView/UserAgent.h>
  40. #include <LibWebView/WebContentClient.h>
  41. namespace Browser {
  42. static ByteString bookmarks_file_path()
  43. {
  44. StringBuilder builder;
  45. builder.append(Core::StandardPaths::config_directory());
  46. builder.append("/bookmarks.json"sv);
  47. return builder.to_byte_string();
  48. }
  49. BrowserWindow::BrowserWindow(WebView::CookieJar& cookie_jar, Vector<URL> const& initial_urls, StringView const man_file)
  50. : m_cookie_jar(cookie_jar)
  51. , m_window_actions(*this)
  52. {
  53. auto app_icon = GUI::Icon::default_icon("app-browser"sv);
  54. m_bookmarks_bar = Browser::BookmarksBarWidget::construct(Browser::bookmarks_file_path(), true);
  55. restore_size_and_position("Browser"sv, "Window"sv, { { 730, 560 } });
  56. save_size_and_position_on_close("Browser"sv, "Window"sv);
  57. set_icon(app_icon.bitmap_for_size(16));
  58. set_title("Ladybird");
  59. auto widget = set_main_widget<GUI::Widget>();
  60. widget->load_from_gml(browser_window_gml).release_value_but_fixme_should_propagate_errors();
  61. auto& top_line = *widget->find_descendant_of_type_named<GUI::HorizontalSeparator>("top_line");
  62. m_tab_widget = *widget->find_descendant_of_type_named<GUI::TabWidget>("tab_widget");
  63. m_tab_widget->on_tab_count_change = [&top_line](size_t tab_count) {
  64. top_line.set_visible(tab_count > 1);
  65. };
  66. m_tab_widget->on_change = [this](auto& active_widget) {
  67. auto& tab = static_cast<Browser::Tab&>(active_widget);
  68. set_window_title_for_tab(tab);
  69. tab.did_become_active();
  70. update_displayed_zoom_level();
  71. };
  72. m_tab_widget->on_middle_click = [](auto& clicked_widget) {
  73. auto& tab = static_cast<Browser::Tab&>(clicked_widget);
  74. tab.on_tab_close_request(tab);
  75. };
  76. m_tab_widget->on_tab_close_click = [](auto& clicked_widget) {
  77. auto& tab = static_cast<Browser::Tab&>(clicked_widget);
  78. tab.on_tab_close_request(tab);
  79. };
  80. m_tab_widget->on_context_menu_request = [](auto& clicked_widget, const GUI::ContextMenuEvent& context_menu_event) {
  81. auto& tab = static_cast<Browser::Tab&>(clicked_widget);
  82. tab.context_menu_requested(context_menu_event.screen_position());
  83. };
  84. m_window_actions.on_create_new_tab = [this] {
  85. create_new_tab(Browser::g_new_tab_url, Web::HTML::ActivateTab::Yes);
  86. };
  87. m_window_actions.on_create_new_window = [this] {
  88. create_new_window(g_home_url);
  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. for (size_t i = 0; i <= 7; ++i) {
  97. m_window_actions.on_tabs.append([this, i] {
  98. if (i >= m_tab_widget->tab_count())
  99. return;
  100. m_tab_widget->set_tab_index(i);
  101. });
  102. }
  103. m_window_actions.on_tabs.append([this] {
  104. m_tab_widget->activate_last_tab();
  105. });
  106. m_window_actions.on_show_bookmarks_bar = [](auto& action) {
  107. Browser::BookmarksBarWidget::the().set_visible(action.is_checked());
  108. Config::write_bool("Browser"sv, "Preferences"sv, "ShowBookmarksBar"sv, action.is_checked());
  109. };
  110. bool show_bookmarks_bar = Config::read_bool("Browser"sv, "Preferences"sv, "ShowBookmarksBar"sv, Browser::default_show_bookmarks_bar);
  111. m_window_actions.show_bookmarks_bar_action().set_checked(show_bookmarks_bar);
  112. Browser::BookmarksBarWidget::the().set_visible(show_bookmarks_bar);
  113. m_window_actions.on_vertical_tabs = [this](auto& action) {
  114. m_tab_widget->set_tab_position(action.is_checked() ? TabPosition::Left : TabPosition::Top);
  115. Config::write_bool("Browser"sv, "Preferences"sv, "VerticalTabs"sv, action.is_checked());
  116. };
  117. bool vertical_tabs = Config::read_bool("Browser"sv, "Preferences"sv, "VerticalTabs"sv, false);
  118. m_window_actions.vertical_tabs_action().set_checked(vertical_tabs);
  119. m_tab_widget->set_tab_position(vertical_tabs ? TabPosition::Left : TabPosition::Top);
  120. build_menus(man_file);
  121. for (size_t i = 0; i < initial_urls.size(); ++i)
  122. create_new_tab(initial_urls[i], (i == 0) ? Web::HTML::ActivateTab::Yes : Web::HTML::ActivateTab::No);
  123. }
  124. void BrowserWindow::build_menus(StringView const man_file)
  125. {
  126. auto file_menu = add_menu("&File"_string);
  127. file_menu->add_action(WindowActions::the().create_new_tab_action());
  128. file_menu->add_action(WindowActions::the().create_new_window_action());
  129. auto close_tab_action = GUI::CommonActions::make_close_tab_action([this](auto&) {
  130. active_tab().on_tab_close_request(active_tab());
  131. },
  132. this);
  133. file_menu->add_action(close_tab_action);
  134. file_menu->add_separator();
  135. file_menu->add_action(GUI::CommonActions::make_quit_action([](auto&) {
  136. GUI::Application::the()->quit();
  137. }));
  138. auto view_menu = add_menu("&View"_string);
  139. view_menu->add_action(WindowActions::the().show_bookmarks_bar_action());
  140. view_menu->add_action(WindowActions::the().vertical_tabs_action());
  141. view_menu->add_separator();
  142. m_zoom_menu = view_menu->add_submenu("&Zoom"_string);
  143. m_zoom_menu->add_action(GUI::CommonActions::make_zoom_in_action(
  144. [this](auto&) {
  145. auto& tab = active_tab();
  146. tab.view().zoom_in();
  147. update_displayed_zoom_level();
  148. },
  149. this));
  150. m_zoom_menu->add_action(GUI::CommonActions::make_zoom_out_action(
  151. [this](auto&) {
  152. auto& tab = active_tab();
  153. tab.view().zoom_out();
  154. update_displayed_zoom_level();
  155. },
  156. this));
  157. m_zoom_menu->add_action(GUI::CommonActions::make_reset_zoom_action(
  158. [this](auto&) {
  159. auto& tab = active_tab();
  160. tab.view().reset_zoom();
  161. update_displayed_zoom_level();
  162. },
  163. this));
  164. view_menu->add_separator();
  165. view_menu->add_action(GUI::CommonActions::make_fullscreen_action(
  166. [this](auto&) {
  167. auto& tab = active_tab();
  168. set_fullscreen(!is_fullscreen());
  169. auto is_fullscreen = this->is_fullscreen();
  170. tab_widget().set_bar_visible(!is_fullscreen && tab_widget().children().size() > 1);
  171. tab.m_toolbar_container->set_visible(!is_fullscreen);
  172. tab.m_statusbar->set_visible(!is_fullscreen);
  173. if (is_fullscreen)
  174. tab.view().set_frame_style(Gfx::FrameStyle::NoFrame);
  175. else
  176. tab.view().set_frame_style(Gfx::FrameStyle::SunkenContainer);
  177. },
  178. this));
  179. m_go_back_action = GUI::CommonActions::make_go_back_action([this](auto&) { active_tab().go_back(); }, this);
  180. m_go_forward_action = GUI::CommonActions::make_go_forward_action([this](auto&) { active_tab().go_forward(); }, this);
  181. m_go_home_action = GUI::CommonActions::make_go_home_action([this](auto&) { active_tab().load(g_home_url); }, this);
  182. m_go_home_action->set_status_tip("Go to home page"_string);
  183. m_reload_action = GUI::CommonActions::make_reload_action([this](auto&) { active_tab().reload(); }, this);
  184. m_reload_action->set_status_tip("Reload current page"_string);
  185. auto go_menu = add_menu("&Go"_string);
  186. go_menu->add_action(*m_go_back_action);
  187. go_menu->add_action(*m_go_forward_action);
  188. go_menu->add_action(*m_go_home_action);
  189. go_menu->add_separator();
  190. go_menu->add_action(*m_reload_action);
  191. m_copy_selection_action = GUI::CommonActions::make_copy_action([this](auto&) {
  192. auto& tab = active_tab();
  193. auto selected_text = tab.view().selected_text();
  194. if (!selected_text.is_empty())
  195. GUI::Clipboard::the().set_plain_text(selected_text);
  196. });
  197. m_select_all_action = GUI::CommonActions::make_select_all_action([this](auto&) {
  198. active_tab().view().select_all();
  199. });
  200. m_view_source_action = GUI::Action::create(
  201. "View &Source", { Mod_Ctrl, Key_U }, g_icon_bag.code, [this](auto&) {
  202. active_tab().view().get_source();
  203. },
  204. this);
  205. m_view_source_action->set_status_tip("View source code of the current page"_string);
  206. m_inspect_dom_tree_action = GUI::Action::create(
  207. "Inspect &DOM Tree", { Mod_Ctrl | Mod_Shift, Key_I }, { Mod_None, Key_F12 }, g_icon_bag.dom_tree, [this](auto&) {
  208. active_tab().show_inspector_window(Tab::InspectorTarget::Document);
  209. },
  210. this);
  211. m_inspect_dom_tree_action->set_status_tip("Open inspector window for this page"_string);
  212. m_inspect_dom_node_action = GUI::Action::create(
  213. "&Inspect Element", g_icon_bag.inspect, [this](auto&) {
  214. active_tab().show_inspector_window(Tab::InspectorTarget::HoveredElement);
  215. },
  216. this);
  217. m_inspect_dom_node_action->set_status_tip("Open inspector for this element"_string);
  218. auto inspect_menu = add_menu("&Inspect"_string);
  219. inspect_menu->add_action(*m_view_source_action);
  220. inspect_menu->add_action(*m_inspect_dom_tree_action);
  221. auto storage_window_action = GUI::Action::create(
  222. "Open S&torage Inspector", g_icon_bag.cookie, [this](auto&) {
  223. active_tab().show_storage_inspector();
  224. },
  225. this);
  226. storage_window_action->set_status_tip("Show Storage inspector for this page"_string);
  227. inspect_menu->add_action(storage_window_action);
  228. auto history_window_action = GUI::Action::create(
  229. "Open &History Window", g_icon_bag.history, [this](auto&) {
  230. active_tab().show_history_inspector();
  231. },
  232. this);
  233. storage_window_action->set_status_tip("Show History inspector for this tab"_string);
  234. inspect_menu->add_action(history_window_action);
  235. auto settings_menu = add_menu("&Settings"_string);
  236. m_change_homepage_action = GUI::Action::create(
  237. "Set Homepage URL...", g_icon_bag.go_home, [this](auto&) {
  238. String homepage_url = String::from_byte_string(Config::read_string("Browser"sv, "Preferences"sv, "Home"sv, Browser::default_homepage_url)).release_value_but_fixme_should_propagate_errors();
  239. if (GUI::InputBox::show(this, homepage_url, "Enter a URL:"sv, "Change Homepage"sv) == GUI::InputBox::ExecResult::OK) {
  240. if (URL(homepage_url).is_valid()) {
  241. Config::write_string("Browser"sv, "Preferences"sv, "Home"sv, homepage_url);
  242. Browser::g_home_url = homepage_url.to_byte_string();
  243. } else {
  244. GUI::MessageBox::show_error(this, "The URL you have entered is not valid"sv);
  245. }
  246. }
  247. },
  248. this);
  249. settings_menu->add_action(*m_change_homepage_action);
  250. auto load_search_engines_result = load_search_engines(settings_menu);
  251. if (load_search_engines_result.is_error()) {
  252. dbgln("Failed to open search-engines file: {}", load_search_engines_result.error());
  253. }
  254. auto color_scheme_menu = settings_menu->add_submenu("&Color Scheme"_string);
  255. color_scheme_menu->set_icon(g_icon_bag.color_chooser);
  256. {
  257. auto current_setting = Web::CSS::preferred_color_scheme_from_string(Config::read_string("Browser"sv, "Preferences"sv, "ColorScheme"sv, Browser::default_color_scheme));
  258. m_color_scheme_actions.set_exclusive(true);
  259. auto add_color_scheme_action = [&](auto& name, Web::CSS::PreferredColorScheme preference_value) {
  260. auto action = GUI::Action::create_checkable(
  261. name, [=, this](auto&) {
  262. Config::write_string("Browser"sv, "Preferences"sv, "ColorScheme"sv, Web::CSS::preferred_color_scheme_to_string(preference_value));
  263. active_tab().view().set_preferred_color_scheme(preference_value);
  264. },
  265. this);
  266. if (current_setting == preference_value)
  267. action->set_checked(true);
  268. color_scheme_menu->add_action(action);
  269. m_color_scheme_actions.add_action(action);
  270. };
  271. add_color_scheme_action("Follow System Theme", Web::CSS::PreferredColorScheme::Auto);
  272. add_color_scheme_action("Light", Web::CSS::PreferredColorScheme::Light);
  273. add_color_scheme_action("Dark", Web::CSS::PreferredColorScheme::Dark);
  274. }
  275. settings_menu->add_separator();
  276. auto open_settings_action = GUI::Action::create("Browser &Settings", Gfx::Bitmap::load_from_file("/res/icons/16x16/settings.png"sv).release_value_but_fixme_should_propagate_errors(),
  277. [this](auto&) {
  278. GUI::Process::spawn_or_show_error(this, "/bin/BrowserSettings"sv);
  279. });
  280. settings_menu->add_action(move(open_settings_action));
  281. auto debug_menu = add_menu("&Debug"_string);
  282. debug_menu->add_action(GUI::Action::create(
  283. "Dump &DOM Tree", g_icon_bag.dom_tree, [this](auto&) {
  284. active_tab().view().debug_request("dump-dom-tree");
  285. },
  286. this));
  287. debug_menu->add_action(GUI::Action::create(
  288. "Dump &Layout Tree", g_icon_bag.layout, [this](auto&) {
  289. active_tab().view().debug_request("dump-layout-tree");
  290. },
  291. this));
  292. debug_menu->add_action(GUI::Action::create(
  293. "Dump &Paint Tree", g_icon_bag.layout, [this](auto&) {
  294. active_tab().view().debug_request("dump-paint-tree");
  295. },
  296. this));
  297. debug_menu->add_action(GUI::Action::create(
  298. "Dump S&tacking Context Tree", g_icon_bag.layers, [this](auto&) {
  299. active_tab().view().debug_request("dump-stacking-context-tree");
  300. },
  301. this));
  302. debug_menu->add_action(GUI::Action::create(
  303. "Dump &Style Sheets", g_icon_bag.filetype_css, [this](auto&) {
  304. active_tab().view().debug_request("dump-style-sheets");
  305. },
  306. this));
  307. debug_menu->add_action(GUI::Action::create(
  308. "Dump &All Resolved Styles", g_icon_bag.filetype_css, [this](auto&) {
  309. active_tab().view().debug_request("dump-all-resolved-styles");
  310. },
  311. this));
  312. debug_menu->add_action(GUI::Action::create("Dump &History", { Mod_Ctrl, Key_H }, g_icon_bag.history, [this](auto&) {
  313. active_tab().m_history.dump();
  314. }));
  315. debug_menu->add_action(GUI::Action::create("Dump C&ookies", g_icon_bag.cookie, [this](auto&) {
  316. m_cookie_jar.dump_cookies();
  317. }));
  318. debug_menu->add_action(GUI::Action::create("Dump Loc&al Storage", g_icon_bag.local_storage, [this](auto&) {
  319. active_tab().view().debug_request("dump-local-storage");
  320. }));
  321. debug_menu->add_separator();
  322. auto line_box_borders_action = GUI::Action::create_checkable(
  323. "Line &Box Borders", [this](auto& action) {
  324. active_tab().view().debug_request("set-line-box-borders", action.is_checked() ? "on" : "off");
  325. },
  326. this);
  327. line_box_borders_action->set_checked(false);
  328. debug_menu->add_action(line_box_borders_action);
  329. debug_menu->add_separator();
  330. debug_menu->add_action(GUI::Action::create("Collect &Garbage", { Mod_Ctrl | Mod_Shift, Key_G }, g_icon_bag.trash_can, [this](auto&) {
  331. active_tab().view().debug_request("collect-garbage");
  332. }));
  333. debug_menu->add_action(GUI::Action::create("Clear &Cache", { Mod_Ctrl | Mod_Shift, Key_C }, g_icon_bag.clear_cache, [this](auto&) {
  334. active_tab().view().debug_request("clear-cache");
  335. }));
  336. m_user_agent_spoof_actions.set_exclusive(true);
  337. auto spoof_user_agent_menu = debug_menu->add_submenu("Spoof &User Agent"_string);
  338. m_disable_user_agent_spoofing = GUI::Action::create_checkable("Disabled", [this](auto&) {
  339. active_tab().view().debug_request("spoof-user-agent", Web::default_user_agent);
  340. });
  341. m_disable_user_agent_spoofing->set_status_tip(String::from_utf8(Web::default_user_agent).release_value_but_fixme_should_propagate_errors());
  342. spoof_user_agent_menu->add_action(*m_disable_user_agent_spoofing);
  343. spoof_user_agent_menu->set_icon(g_icon_bag.spoof);
  344. m_user_agent_spoof_actions.add_action(*m_disable_user_agent_spoofing);
  345. m_disable_user_agent_spoofing->set_checked(true);
  346. auto add_user_agent = [this, &spoof_user_agent_menu](auto& name, auto user_agent) {
  347. auto action = GUI::Action::create_checkable(name, [this, user_agent](auto&) {
  348. active_tab().view().debug_request("spoof-user-agent", user_agent);
  349. });
  350. action->set_status_tip(String::from_utf8(user_agent).release_value_but_fixme_should_propagate_errors());
  351. spoof_user_agent_menu->add_action(action);
  352. m_user_agent_spoof_actions.add_action(action);
  353. };
  354. for (auto const& user_agent : WebView::user_agents)
  355. add_user_agent(user_agent.key, user_agent.value);
  356. auto custom_user_agent = GUI::Action::create_checkable("Custom...", [this](auto& action) {
  357. String user_agent;
  358. if (GUI::InputBox::show(this, user_agent, "Enter User Agent:"sv, "Custom User Agent"sv, GUI::InputType::NonemptyText) != GUI::InputBox::ExecResult::OK) {
  359. m_disable_user_agent_spoofing->activate();
  360. return;
  361. }
  362. active_tab().view().debug_request("spoof-user-agent", user_agent.to_byte_string());
  363. action.set_status_tip(user_agent);
  364. });
  365. spoof_user_agent_menu->add_action(custom_user_agent);
  366. m_user_agent_spoof_actions.add_action(custom_user_agent);
  367. debug_menu->add_separator();
  368. auto scripting_enabled_action = GUI::Action::create_checkable(
  369. "Enable Scripting", [this](auto& action) {
  370. active_tab().view().debug_request("scripting", action.is_checked() ? "on" : "off");
  371. },
  372. this);
  373. scripting_enabled_action->set_checked(true);
  374. debug_menu->add_action(scripting_enabled_action);
  375. auto block_pop_ups_action = GUI::Action::create_checkable(
  376. "Block Pop-ups", [this](auto& action) {
  377. active_tab().view().debug_request("block-pop-ups", action.is_checked() ? "on" : "off");
  378. },
  379. this);
  380. block_pop_ups_action->set_checked(true);
  381. debug_menu->add_action(block_pop_ups_action);
  382. auto same_origin_policy_action = GUI::Action::create_checkable(
  383. "Enable Same-Origin &Policy", [this](auto& action) {
  384. active_tab().view().debug_request("same-origin-policy", action.is_checked() ? "on" : "off");
  385. },
  386. this);
  387. same_origin_policy_action->set_checked(false);
  388. debug_menu->add_action(same_origin_policy_action);
  389. auto help_menu = add_menu("&Help"_string);
  390. help_menu->add_action(GUI::CommonActions::make_command_palette_action(this));
  391. help_menu->add_action(GUI::CommonActions::make_help_action([man_file](auto&) {
  392. Desktop::Launcher::open(URL::create_with_file_scheme(man_file), "/bin/Help");
  393. }));
  394. help_menu->add_action(WindowActions::the().about_action());
  395. }
  396. ErrorOr<void> BrowserWindow::load_search_engines(GUI::Menu& settings_menu)
  397. {
  398. m_search_engine_actions.set_exclusive(true);
  399. auto search_engine_menu = settings_menu.add_submenu("&Search Engine"_string);
  400. search_engine_menu->set_icon(g_icon_bag.find);
  401. bool search_engine_set = false;
  402. m_disable_search_engine_action = GUI::Action::create_checkable(
  403. "Disable", [](auto&) {
  404. g_search_engine = {};
  405. Config::write_string("Browser"sv, "Preferences"sv, "SearchEngine"sv, g_search_engine);
  406. },
  407. this);
  408. search_engine_menu->add_action(*m_disable_search_engine_action);
  409. m_search_engine_actions.add_action(*m_disable_search_engine_action);
  410. m_disable_search_engine_action->set_checked(true);
  411. for (auto [name, url_format] : WebView::search_engines()) {
  412. auto action = GUI::Action::create_checkable(
  413. name, [&, url_format](auto&) {
  414. g_search_engine = url_format;
  415. Config::write_string("Browser"sv, "Preferences"sv, "SearchEngine"sv, g_search_engine);
  416. },
  417. this);
  418. search_engine_menu->add_action(action);
  419. m_search_engine_actions.add_action(action);
  420. if (g_search_engine == url_format) {
  421. action->set_checked(true);
  422. search_engine_set = true;
  423. }
  424. action->set_status_tip(TRY(String::from_utf8(url_format)));
  425. }
  426. auto custom_search_engine_action = GUI::Action::create_checkable("Custom...", [&](auto& action) {
  427. String search_engine;
  428. if (GUI::InputBox::show(this, search_engine, "Enter URL template:"sv, "Custom Search Engine"sv, GUI::InputType::NonemptyText, "https://host/search?q={}"sv) != GUI::InputBox::ExecResult::OK) {
  429. m_disable_search_engine_action->activate();
  430. return;
  431. }
  432. auto argument_count = AK::StringUtils::count(search_engine, "{}"sv);
  433. if (argument_count != 1) {
  434. GUI::MessageBox::show(this, "Invalid format, must contain '{}' once!"sv, "Error"sv, GUI::MessageBox::Type::Error);
  435. m_disable_search_engine_action->activate();
  436. return;
  437. }
  438. g_search_engine = search_engine.to_byte_string();
  439. Config::write_string("Browser"sv, "Preferences"sv, "SearchEngine"sv, g_search_engine);
  440. action.set_status_tip(search_engine);
  441. });
  442. search_engine_menu->add_action(custom_search_engine_action);
  443. m_search_engine_actions.add_action(custom_search_engine_action);
  444. if (!search_engine_set && !g_search_engine.is_empty()) {
  445. custom_search_engine_action->set_checked(true);
  446. custom_search_engine_action->set_status_tip(TRY(String::from_byte_string(g_search_engine)));
  447. }
  448. return {};
  449. }
  450. GUI::TabWidget& BrowserWindow::tab_widget()
  451. {
  452. return *m_tab_widget;
  453. }
  454. Tab& BrowserWindow::active_tab()
  455. {
  456. return verify_cast<Tab>(*tab_widget().active_widget());
  457. }
  458. void BrowserWindow::set_window_title_for_tab(Tab const& tab)
  459. {
  460. auto& title = tab.title();
  461. auto url = tab.url();
  462. set_title(ByteString::formatted("{} - Ladybird", title.is_empty() ? url.to_byte_string() : title));
  463. }
  464. Tab& BrowserWindow::create_new_tab(URL const& url, Web::HTML::ActivateTab activate)
  465. {
  466. auto& new_tab = m_tab_widget->add_tab<Browser::Tab>("New tab"_string, *this);
  467. m_tab_widget->set_bar_visible(!is_fullscreen() && m_tab_widget->children().size() > 1);
  468. new_tab.on_title_change = [this, &new_tab](auto& title) {
  469. m_tab_widget->set_tab_title(new_tab, String::from_byte_string(title).release_value_but_fixme_should_propagate_errors());
  470. if (m_tab_widget->active_widget() == &new_tab)
  471. set_window_title_for_tab(new_tab);
  472. };
  473. new_tab.on_favicon_change = [this, &new_tab](auto& bitmap) {
  474. m_tab_widget->set_tab_icon(new_tab, &bitmap);
  475. };
  476. new_tab.on_tab_open_request = [this](auto& url) {
  477. create_new_tab(url, Web::HTML::ActivateTab::Yes);
  478. };
  479. new_tab.on_activate_tab_request = [this](auto& tab) {
  480. m_tab_widget->set_active_widget(&tab);
  481. };
  482. new_tab.on_tab_close_request = [this](auto& tab) {
  483. m_tab_widget->deferred_invoke([this, &tab] {
  484. m_tab_widget->remove_tab(tab);
  485. m_tab_widget->set_bar_visible(!is_fullscreen() && m_tab_widget->children().size() > 1);
  486. if (m_tab_widget->children().is_empty())
  487. close();
  488. });
  489. };
  490. new_tab.on_tab_close_other_request = [this](auto& tab) {
  491. m_tab_widget->deferred_invoke([this, &tab] {
  492. m_tab_widget->remove_all_tabs_except(tab);
  493. VERIFY(m_tab_widget->children().size() == 1);
  494. m_tab_widget->set_bar_visible(false);
  495. });
  496. };
  497. new_tab.on_window_open_request = [this](auto& url) {
  498. create_new_window(url);
  499. };
  500. new_tab.view().on_get_all_cookies = [this](auto& url) {
  501. return m_cookie_jar.get_all_cookies(url);
  502. };
  503. new_tab.view().on_get_named_cookie = [this](auto& url, auto& name) {
  504. return m_cookie_jar.get_named_cookie(url, name);
  505. };
  506. new_tab.view().on_get_cookie = [this](auto& url, auto source) {
  507. return m_cookie_jar.get_cookie(url, source);
  508. };
  509. new_tab.view().on_set_cookie = [this](auto& url, auto& cookie, auto source) {
  510. m_cookie_jar.set_cookie(url, cookie, source);
  511. };
  512. new_tab.view().on_update_cookie = [this](auto const& cookie) {
  513. m_cookie_jar.update_cookie(cookie);
  514. };
  515. new_tab.on_get_cookies_entries = [this]() {
  516. return m_cookie_jar.get_all_cookies();
  517. };
  518. new_tab.on_get_local_storage_entries = [this]() {
  519. return active_tab().view().get_local_storage_entries();
  520. };
  521. new_tab.on_get_session_storage_entries = [this]() {
  522. return active_tab().view().get_session_storage_entries();
  523. };
  524. new_tab.load(url);
  525. dbgln_if(SPAM_DEBUG, "Added new tab {:p}, loading {}", &new_tab, url);
  526. if (activate == Web::HTML::ActivateTab::Yes)
  527. m_tab_widget->set_active_widget(&new_tab);
  528. return new_tab;
  529. }
  530. void BrowserWindow::create_new_window(URL const& url)
  531. {
  532. GUI::Process::spawn_or_show_error(this, "/bin/Browser"sv, Array { url.to_byte_string() });
  533. }
  534. void BrowserWindow::content_filters_changed()
  535. {
  536. tab_widget().for_each_child_of_type<Browser::Tab>([](auto& tab) {
  537. tab.content_filters_changed();
  538. return IterationDecision::Continue;
  539. });
  540. }
  541. void BrowserWindow::autoplay_allowlist_changed()
  542. {
  543. tab_widget().for_each_child_of_type<Browser::Tab>([](auto& tab) {
  544. tab.autoplay_allowlist_changed();
  545. return IterationDecision::Continue;
  546. });
  547. }
  548. void BrowserWindow::proxy_mappings_changed()
  549. {
  550. tab_widget().for_each_child_of_type<Browser::Tab>([](auto& tab) {
  551. tab.proxy_mappings_changed();
  552. return IterationDecision::Continue;
  553. });
  554. }
  555. void BrowserWindow::config_string_did_change(StringView domain, StringView group, StringView key, StringView value)
  556. {
  557. if (domain != "Browser")
  558. return;
  559. if (group == "Preferences") {
  560. if (key == "SearchEngine")
  561. Browser::g_search_engine = value;
  562. else if (key == "Home")
  563. Browser::g_home_url = value;
  564. else if (key == "NewTab")
  565. Browser::g_new_tab_url = value;
  566. } else if (group.starts_with("Proxy:"sv)) {
  567. dbgln("Proxy mapping changed: {}/{} = {}", group, key, value);
  568. auto proxy_spec = group.substring_view(6);
  569. auto existing_proxy = Browser::g_proxies.find(proxy_spec);
  570. if (existing_proxy.is_end())
  571. Browser::g_proxies.append(proxy_spec);
  572. Browser::g_proxy_mappings.set(key, existing_proxy.index());
  573. proxy_mappings_changed();
  574. }
  575. // TODO: ColorScheme
  576. }
  577. void BrowserWindow::config_bool_did_change(StringView domain, StringView group, StringView key, bool value)
  578. {
  579. dbgln("{} {} {} {}", domain, group, key, value);
  580. if (domain != "Browser" || group != "Preferences")
  581. return;
  582. if (key == "ShowBookmarksBar") {
  583. m_window_actions.show_bookmarks_bar_action().set_checked(value);
  584. Browser::BookmarksBarWidget::the().set_visible(value);
  585. } else if (key == "EnableContentFilters") {
  586. Browser::g_content_filters_enabled = value;
  587. content_filters_changed();
  588. } else if (key == "AllowAutoplayOnAllWebsites") {
  589. Browser::g_autoplay_allowed_on_all_websites = value;
  590. autoplay_allowlist_changed();
  591. }
  592. // NOTE: CloseDownloadWidgetOnFinish is read each time in DownloadWindow
  593. }
  594. void BrowserWindow::broadcast_window_position(Gfx::IntPoint position)
  595. {
  596. tab_widget().for_each_child_of_type<Browser::Tab>([&](auto& tab) {
  597. tab.window_position_changed(position);
  598. return IterationDecision::Continue;
  599. });
  600. }
  601. void BrowserWindow::broadcast_window_size(Gfx::IntSize size)
  602. {
  603. tab_widget().for_each_child_of_type<Browser::Tab>([&](auto& tab) {
  604. tab.window_size_changed(size);
  605. return IterationDecision::Continue;
  606. });
  607. }
  608. void BrowserWindow::event(Core::Event& event)
  609. {
  610. switch (event.type()) {
  611. case GUI::Event::Move:
  612. broadcast_window_position(static_cast<GUI::MoveEvent&>(event).position());
  613. break;
  614. case GUI::Event::Resize:
  615. broadcast_window_size(static_cast<GUI::ResizeEvent&>(event).size());
  616. break;
  617. case GUI::Event::KeyDown:
  618. // FIXME: add support for hotkeys *within* ladybird
  619. event.ignore();
  620. break;
  621. default:
  622. break;
  623. }
  624. Window::event(event);
  625. }
  626. void BrowserWindow::update_zoom_menu()
  627. {
  628. VERIFY(m_zoom_menu);
  629. auto zoom_level_text = String::formatted("&Zoom ({}%)", round_to<int>(active_tab().view().zoom_level() * 100)).release_value_but_fixme_should_propagate_errors();
  630. m_zoom_menu->set_name(zoom_level_text);
  631. }
  632. void BrowserWindow::update_displayed_zoom_level()
  633. {
  634. active_tab().update_reset_zoom_button();
  635. update_zoom_menu();
  636. }
  637. }