BrowserWindow.cpp 31 KB

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