BrowserWindow.cpp 26 KB

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