BrowserWindow.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. /*
  2. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "BrowserWindow.h"
  7. #include "BookmarksBarWidget.h"
  8. #include "Browser.h"
  9. #include "ConsoleWidget.h"
  10. #include "CookieJar.h"
  11. #include "InspectorWidget.h"
  12. #include "Tab.h"
  13. #include <Applications/Browser/BrowserWindowGML.h>
  14. #include <LibCore/ConfigFile.h>
  15. #include <LibCore/StandardPaths.h>
  16. #include <LibGUI/AboutDialog.h>
  17. #include <LibGUI/Application.h>
  18. #include <LibGUI/Icon.h>
  19. #include <LibGUI/InputBox.h>
  20. #include <LibGUI/Menu.h>
  21. #include <LibGUI/Menubar.h>
  22. #include <LibGUI/SeparatorWidget.h>
  23. #include <LibGUI/Statusbar.h>
  24. #include <LibGUI/TabWidget.h>
  25. #include <LibGUI/ToolbarContainer.h>
  26. #include <LibGUI/Widget.h>
  27. #include <LibJS/Interpreter.h>
  28. #include <LibWeb/Dump.h>
  29. #include <LibWeb/InProcessWebView.h>
  30. #include <LibWeb/Layout/InitialContainingBlockBox.h>
  31. #include <LibWeb/Loader/ResourceLoader.h>
  32. #include <LibWeb/OutOfProcessWebView.h>
  33. namespace Browser {
  34. static String bookmarks_file_path()
  35. {
  36. StringBuilder builder;
  37. builder.append(Core::StandardPaths::config_directory());
  38. builder.append("/bookmarks.json");
  39. return builder.to_string();
  40. }
  41. BrowserWindow::BrowserWindow(CookieJar& cookie_jar, URL url)
  42. : m_cookie_jar(cookie_jar)
  43. , m_window_actions(*this)
  44. {
  45. auto app_icon = GUI::Icon::default_icon("app-browser");
  46. m_bookmarks_bar = Browser::BookmarksBarWidget::construct(Browser::bookmarks_file_path(), true);
  47. resize(640, 480);
  48. set_icon(app_icon.bitmap_for_size(16));
  49. set_title("Browser");
  50. auto& widget = set_main_widget<GUI::Widget>();
  51. widget.load_from_gml(browser_window_gml);
  52. auto& top_line = *widget.find_descendant_of_type_named<GUI::HorizontalSeparator>("top_line");
  53. m_tab_widget = *widget.find_descendant_of_type_named<GUI::TabWidget>("tab_widget");
  54. m_tab_widget->on_tab_count_change = [&top_line](size_t tab_count) {
  55. top_line.set_visible(tab_count > 1);
  56. };
  57. m_tab_widget->on_change = [this](auto& active_widget) {
  58. auto& tab = static_cast<Browser::Tab&>(active_widget);
  59. set_window_title_for_tab(tab);
  60. tab.did_become_active();
  61. };
  62. m_tab_widget->on_middle_click = [](auto& clicked_widget) {
  63. auto& tab = static_cast<Browser::Tab&>(clicked_widget);
  64. tab.on_tab_close_request(tab);
  65. };
  66. m_tab_widget->on_context_menu_request = [](auto& clicked_widget, const GUI::ContextMenuEvent& context_menu_event) {
  67. auto& tab = static_cast<Browser::Tab&>(clicked_widget);
  68. tab.context_menu_requested(context_menu_event.screen_position());
  69. };
  70. m_window_actions.on_create_new_tab = [this] {
  71. create_new_tab(Browser::g_home_url, true);
  72. };
  73. m_window_actions.on_next_tab = [this] {
  74. m_tab_widget->activate_next_tab();
  75. };
  76. m_window_actions.on_previous_tab = [this] {
  77. m_tab_widget->activate_previous_tab();
  78. };
  79. m_window_actions.on_about = [this] {
  80. auto app_icon = GUI::Icon::default_icon("app-browser");
  81. GUI::AboutDialog::show("Browser", app_icon.bitmap_for_size(32), this);
  82. };
  83. m_window_actions.on_show_bookmarks_bar = [](auto& action) {
  84. Browser::BookmarksBarWidget::the().set_visible(action.is_checked());
  85. };
  86. m_window_actions.show_bookmarks_bar_action().set_checked(true);
  87. build_menus();
  88. create_new_tab(move(url), true);
  89. }
  90. BrowserWindow::~BrowserWindow()
  91. {
  92. }
  93. void BrowserWindow::build_menus()
  94. {
  95. auto menubar = GUI::Menubar::construct();
  96. auto& file_menu = menubar->add_menu("&File");
  97. file_menu.add_action(WindowActions::the().create_new_tab_action());
  98. auto close_tab_action = GUI::Action::create(
  99. "&Close Tab", { Mod_Ctrl, Key_W }, Gfx::Bitmap::load_from_file("/res/icons/16x16/close-tab.png"), [this](auto&) {
  100. active_tab().on_tab_close_request(active_tab());
  101. },
  102. this);
  103. close_tab_action->set_status_tip("Close current tab");
  104. file_menu.add_action(close_tab_action);
  105. file_menu.add_separator();
  106. file_menu.add_action(GUI::CommonActions::make_quit_action([](auto&) {
  107. GUI::Application::the()->quit();
  108. }));
  109. auto& view_menu = menubar->add_menu("&View");
  110. view_menu.add_action(WindowActions::the().show_bookmarks_bar_action());
  111. view_menu.add_separator();
  112. view_menu.add_action(GUI::CommonActions::make_fullscreen_action(
  113. [this](auto&) {
  114. auto& tab = active_tab();
  115. set_fullscreen(!is_fullscreen());
  116. auto is_fullscreen = this->is_fullscreen();
  117. tab_widget().set_bar_visible(!is_fullscreen && tab_widget().children().size() > 1);
  118. tab.m_toolbar_container->set_visible(!is_fullscreen);
  119. tab.m_statusbar->set_visible(!is_fullscreen);
  120. if (is_fullscreen) {
  121. tab.view().set_frame_thickness(0);
  122. } else {
  123. tab.view().set_frame_thickness(2);
  124. }
  125. },
  126. this));
  127. m_go_back_action = GUI::CommonActions::make_go_back_action([this](auto&) { active_tab().go_back(); }, this);
  128. m_go_forward_action = GUI::CommonActions::make_go_forward_action([this](auto&) { active_tab().go_forward(); }, this);
  129. m_go_home_action = GUI::CommonActions::make_go_home_action([this](auto&) { active_tab().load(g_home_url); }, this);
  130. m_go_home_action->set_status_tip("Go to home page");
  131. m_reload_action = GUI::CommonActions::make_reload_action([this](auto&) { active_tab().reload(); }, this);
  132. m_reload_action->set_status_tip("Reload current page");
  133. auto& go_menu = menubar->add_menu("&Go");
  134. go_menu.add_action(*m_go_back_action);
  135. go_menu.add_action(*m_go_forward_action);
  136. go_menu.add_action(*m_go_home_action);
  137. go_menu.add_separator();
  138. go_menu.add_action(*m_reload_action);
  139. m_view_source_action = GUI::Action::create(
  140. "View &Source", { Mod_Ctrl, Key_U }, [this](auto&) {
  141. auto& tab = active_tab();
  142. if (tab.m_type == Tab::Type::InProcessWebView) {
  143. VERIFY(tab.m_page_view->document());
  144. auto url = tab.m_page_view->document()->url();
  145. auto source = tab.m_page_view->document()->source();
  146. tab.view_source(url, source);
  147. } else {
  148. tab.m_web_content_view->get_source();
  149. }
  150. },
  151. this);
  152. m_view_source_action->set_status_tip("View source code of the current page");
  153. m_inspect_dom_tree_action = GUI::Action::create(
  154. "Inspect &DOM Tree", { Mod_None, Key_F12 }, [this](auto&) {
  155. auto& tab = active_tab();
  156. if (tab.m_type == Tab::Type::InProcessWebView) {
  157. if (!tab.m_dom_inspector_window) {
  158. tab.m_dom_inspector_window = GUI::Window::construct(this);
  159. tab.m_dom_inspector_window->resize(300, 500);
  160. tab.m_dom_inspector_window->set_title("DOM inspector");
  161. tab.m_dom_inspector_window->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/inspector-object.png"));
  162. tab.m_dom_inspector_window->set_main_widget<InspectorWidget>();
  163. }
  164. auto* inspector_widget = static_cast<InspectorWidget*>(tab.m_dom_inspector_window->main_widget());
  165. inspector_widget->set_document(tab.m_page_view->document());
  166. tab.m_dom_inspector_window->show();
  167. tab.m_dom_inspector_window->move_to_front();
  168. } else {
  169. TODO();
  170. }
  171. },
  172. this);
  173. m_inspect_dom_tree_action->set_status_tip("Open DOM inspector window for this page");
  174. auto& inspect_menu = menubar->add_menu("&Inspect");
  175. inspect_menu.add_action(*m_view_source_action);
  176. inspect_menu.add_action(*m_inspect_dom_tree_action);
  177. auto js_console_action = GUI::Action::create(
  178. "Open &JS Console", { Mod_Ctrl, Key_I }, [this](auto&) {
  179. auto& tab = active_tab();
  180. if (tab.m_type == Tab::Type::InProcessWebView) {
  181. if (!tab.m_console_window) {
  182. tab.m_console_window = GUI::Window::construct(this);
  183. tab.m_console_window->resize(500, 300);
  184. tab.m_console_window->set_title("JS Console");
  185. tab.m_console_window->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/filetype-javascript.png"));
  186. tab.m_console_window->set_main_widget<ConsoleWidget>();
  187. }
  188. auto* console_widget = static_cast<ConsoleWidget*>(tab.m_console_window->main_widget());
  189. console_widget->set_interpreter(tab.m_page_view->document()->interpreter().make_weak_ptr());
  190. tab.m_console_window->show();
  191. tab.m_console_window->move_to_front();
  192. } else {
  193. if (!tab.m_console_window) {
  194. tab.m_console_window = GUI::Window::construct(this);
  195. tab.m_console_window->resize(500, 300);
  196. tab.m_console_window->set_title("JS Console");
  197. tab.m_console_window->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/filetype-javascript.png"));
  198. tab.m_console_window->set_main_widget<ConsoleWidget>();
  199. }
  200. auto* console_widget = static_cast<ConsoleWidget*>(tab.m_console_window->main_widget());
  201. console_widget->on_js_input = [&tab](const String& js_source) {
  202. tab.m_web_content_view->js_console_input(js_source);
  203. };
  204. console_widget->clear_output();
  205. tab.m_web_content_view->js_console_initialize();
  206. tab.m_console_window->show();
  207. tab.m_console_window->move_to_front();
  208. }
  209. },
  210. this);
  211. js_console_action->set_status_tip("Open JavaScript console for this page");
  212. inspect_menu.add_action(js_console_action);
  213. auto& settings_menu = menubar->add_menu("&Settings");
  214. m_search_engine_actions.set_exclusive(true);
  215. auto& search_engine_menu = settings_menu.add_submenu("&Search Engine");
  216. auto add_search_engine = [this, &search_engine_menu](auto& name, auto& url_format) {
  217. auto action = GUI::Action::create_checkable(
  218. name, [url_format](auto&) {
  219. g_search_engine = url_format;
  220. auto m_config = Core::ConfigFile::get_for_app("Browser");
  221. m_config->write_entry("Preferences", "SearchEngine", g_search_engine);
  222. },
  223. this);
  224. search_engine_menu.add_action(action);
  225. m_search_engine_actions.add_action(action);
  226. if (g_search_engine == url_format) {
  227. action->set_checked(true);
  228. }
  229. action->set_status_tip(url_format);
  230. };
  231. auto disable_search_engine_action = GUI::Action::create_checkable(
  232. "Disable", [](auto&) {
  233. g_search_engine = {};
  234. auto config = Core::ConfigFile::get_for_app("Browser");
  235. config->write_entry("Preferences", "SearchEngine", g_search_engine);
  236. },
  237. this);
  238. search_engine_menu.add_action(disable_search_engine_action);
  239. m_search_engine_actions.add_action(disable_search_engine_action);
  240. disable_search_engine_action->set_checked(true);
  241. // FIXME: Support adding custom search engines
  242. add_search_engine("Bing", "https://www.bing.com/search?q={}");
  243. add_search_engine("DuckDuckGo", "https://duckduckgo.com/?q={}");
  244. add_search_engine("FrogFind", "http://frogfind.com/?q={}");
  245. add_search_engine("GitHub", "https://github.com/search?q={}");
  246. add_search_engine("Google", "https://google.com/search?q={}");
  247. add_search_engine("Yandex", "https://yandex.com/search/?text={}");
  248. auto& debug_menu = menubar->add_menu("&Debug");
  249. debug_menu.add_action(GUI::Action::create(
  250. "Dump &DOM Tree", [this](auto&) {
  251. auto& tab = active_tab();
  252. if (tab.m_type == Tab::Type::InProcessWebView) {
  253. Web::dump_tree(*tab.m_page_view->document());
  254. } else {
  255. tab.m_web_content_view->debug_request("dump-dom-tree");
  256. }
  257. },
  258. this));
  259. debug_menu.add_action(GUI::Action::create(
  260. "Dump &Layout Tree", [this](auto&) {
  261. auto& tab = active_tab();
  262. if (tab.m_type == Tab::Type::InProcessWebView) {
  263. Web::dump_tree(*tab.m_page_view->document()->layout_node());
  264. } else {
  265. tab.m_web_content_view->debug_request("dump-layout-tree");
  266. }
  267. },
  268. this));
  269. debug_menu.add_action(GUI::Action::create(
  270. "Dump &Style Sheets", [this](auto&) {
  271. auto& tab = active_tab();
  272. if (tab.m_type == Tab::Type::InProcessWebView) {
  273. for (auto& sheet : tab.m_page_view->document()->style_sheets().sheets()) {
  274. Web::dump_sheet(sheet);
  275. }
  276. } else {
  277. tab.m_web_content_view->debug_request("dump-style-sheets");
  278. }
  279. },
  280. this));
  281. debug_menu.add_action(GUI::Action::create("Dump &History", { Mod_Ctrl, Key_H }, [this](auto&) {
  282. active_tab().m_history.dump();
  283. }));
  284. debug_menu.add_action(GUI::Action::create("Dump C&ookies", [this](auto&) {
  285. auto& tab = active_tab();
  286. if (tab.on_dump_cookies)
  287. tab.on_dump_cookies();
  288. }));
  289. debug_menu.add_separator();
  290. auto line_box_borders_action = GUI::Action::create_checkable(
  291. "&Line Box Borders", [this](auto& action) {
  292. auto& tab = active_tab();
  293. if (tab.m_type == Tab::Type::InProcessWebView) {
  294. tab.m_page_view->set_should_show_line_box_borders(action.is_checked());
  295. tab.m_page_view->update();
  296. } else {
  297. tab.m_web_content_view->debug_request("set-line-box-borders", action.is_checked() ? "on" : "off");
  298. }
  299. },
  300. this);
  301. line_box_borders_action->set_checked(false);
  302. debug_menu.add_action(line_box_borders_action);
  303. debug_menu.add_separator();
  304. debug_menu.add_action(GUI::Action::create("Collect &Garbage", { Mod_Ctrl | Mod_Shift, Key_G }, [this](auto&) {
  305. auto& tab = active_tab();
  306. if (tab.m_type == Tab::Type::InProcessWebView) {
  307. if (auto* document = tab.m_page_view->document()) {
  308. document->interpreter().heap().collect_garbage(JS::Heap::CollectionType::CollectGarbage, true);
  309. }
  310. } else {
  311. tab.m_web_content_view->debug_request("collect-garbage");
  312. }
  313. }));
  314. debug_menu.add_action(GUI::Action::create("Clear &Cache", { Mod_Ctrl | Mod_Shift, Key_C }, [this](auto&) {
  315. auto& tab = active_tab();
  316. if (tab.m_type == Tab::Type::InProcessWebView) {
  317. Web::ResourceLoader::the().clear_cache();
  318. } else {
  319. tab.m_web_content_view->debug_request("clear-cache");
  320. }
  321. }));
  322. m_user_agent_spoof_actions.set_exclusive(true);
  323. auto& spoof_user_agent_menu = debug_menu.add_submenu("Spoof User Agent");
  324. m_disable_user_agent_spoofing = GUI::Action::create_checkable("Disabled", [this](auto&) {
  325. auto& tab = active_tab();
  326. if (tab.m_type == Tab::Type::InProcessWebView) {
  327. Web::ResourceLoader::the().set_user_agent(Web::default_user_agent);
  328. } else {
  329. tab.m_web_content_view->debug_request("spoof-user-agent", Web::default_user_agent);
  330. }
  331. });
  332. m_disable_user_agent_spoofing->set_status_tip(Web::default_user_agent);
  333. spoof_user_agent_menu.add_action(*m_disable_user_agent_spoofing);
  334. m_user_agent_spoof_actions.add_action(*m_disable_user_agent_spoofing);
  335. m_disable_user_agent_spoofing->set_checked(true);
  336. auto add_user_agent = [this, &spoof_user_agent_menu](auto& name, auto& user_agent) {
  337. auto action = GUI::Action::create_checkable(name, [this, user_agent](auto&) {
  338. auto& tab = active_tab();
  339. if (tab.m_type == Tab::Type::InProcessWebView) {
  340. Web::ResourceLoader::the().set_user_agent(user_agent);
  341. } else {
  342. tab.m_web_content_view->debug_request("spoof-user-agent", user_agent);
  343. }
  344. });
  345. action->set_status_tip(user_agent);
  346. spoof_user_agent_menu.add_action(action);
  347. m_user_agent_spoof_actions.add_action(action);
  348. };
  349. 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");
  350. add_user_agent("Firefox Linux Desktop", "Mozilla/5.0 (X11; Linux i686; rv:87.0) Gecko/20100101 Firefox/87.0");
  351. 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");
  352. 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");
  353. add_user_agent("Firefox Android Mobile", "Mozilla/5.0 (Android 11; Mobile; rv:68.0) Gecko/68.0 Firefox/86.0");
  354. 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");
  355. auto custom_user_agent = GUI::Action::create_checkable("Custom", [this](auto& action) {
  356. auto& tab = active_tab();
  357. String user_agent;
  358. if (GUI::InputBox::show(this, user_agent, "Enter User Agent:", "Custom User Agent") != GUI::InputBox::ExecOK || user_agent.is_empty() || user_agent.is_null()) {
  359. m_disable_user_agent_spoofing->activate();
  360. return;
  361. }
  362. if (tab.m_type == Tab::Type::InProcessWebView) {
  363. Web::ResourceLoader::the().set_user_agent(user_agent);
  364. } else {
  365. tab.m_web_content_view->debug_request("spoof-user-agent", user_agent);
  366. }
  367. action.set_status_tip(user_agent);
  368. });
  369. spoof_user_agent_menu.add_action(custom_user_agent);
  370. m_user_agent_spoof_actions.add_action(custom_user_agent);
  371. auto& help_menu = menubar->add_menu("&Help");
  372. help_menu.add_action(WindowActions::the().about_action());
  373. set_menubar(move(menubar));
  374. }
  375. GUI::TabWidget& BrowserWindow::tab_widget()
  376. {
  377. return *m_tab_widget;
  378. }
  379. Tab& BrowserWindow::active_tab()
  380. {
  381. return downcast<Tab>(*tab_widget().active_widget());
  382. }
  383. void BrowserWindow::set_window_title_for_tab(Tab const& tab)
  384. {
  385. auto& title = tab.title();
  386. auto url = tab.url();
  387. set_title(String::formatted("{} - Browser", title.is_empty() ? url.to_string() : title));
  388. }
  389. void BrowserWindow::create_new_tab(URL url, bool activate)
  390. {
  391. auto type = Browser::g_single_process ? Browser::Tab::Type::InProcessWebView : Browser::Tab::Type::OutOfProcessWebView;
  392. auto& new_tab = m_tab_widget->add_tab<Browser::Tab>("New tab", *this, type);
  393. m_tab_widget->set_bar_visible(!is_fullscreen() && m_tab_widget->children().size() > 1);
  394. auto default_favicon = Gfx::Bitmap::load_from_file("/res/icons/16x16/filetype-html.png");
  395. VERIFY(default_favicon);
  396. m_tab_widget->set_tab_icon(new_tab, default_favicon);
  397. new_tab.on_title_change = [this, &new_tab](auto title) {
  398. m_tab_widget->set_tab_title(new_tab, title);
  399. if (m_tab_widget->active_widget() == &new_tab)
  400. set_window_title_for_tab(new_tab);
  401. };
  402. new_tab.on_favicon_change = [this, &new_tab](auto& bitmap) {
  403. m_tab_widget->set_tab_icon(new_tab, &bitmap);
  404. };
  405. new_tab.on_tab_open_request = [this](auto& url) {
  406. create_new_tab(url, true);
  407. };
  408. new_tab.on_tab_close_request = [this](auto& tab) {
  409. m_tab_widget->deferred_invoke([this, &tab](auto&) {
  410. m_tab_widget->remove_tab(tab);
  411. m_tab_widget->set_bar_visible(!is_fullscreen() && m_tab_widget->children().size() > 1);
  412. if (m_tab_widget->children().is_empty())
  413. close();
  414. });
  415. };
  416. new_tab.on_get_cookie = [this](auto& url, auto source) -> String {
  417. return m_cookie_jar.get_cookie(url, source);
  418. };
  419. new_tab.on_set_cookie = [this](auto& url, auto& cookie, auto source) {
  420. m_cookie_jar.set_cookie(url, cookie, source);
  421. };
  422. new_tab.on_dump_cookies = [this]() {
  423. m_cookie_jar.dump_cookies();
  424. };
  425. new_tab.load(url);
  426. dbgln("Added new tab {:p}, loading {}", &new_tab, url);
  427. if (activate)
  428. m_tab_widget->set_active_widget(&new_tab);
  429. }
  430. }