BrowserWindow.cpp 33 KB

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