BrowserWindow.cpp 30 KB

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