BrowserWindow.cpp 29 KB

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