Tab.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "Tab.h"
  27. #include "BookmarksBarWidget.h"
  28. #include "Browser.h"
  29. #include "ConsoleWidget.h"
  30. #include "DownloadWidget.h"
  31. #include "InspectorWidget.h"
  32. #include "WindowActions.h"
  33. #include <AK/StringBuilder.h>
  34. #include <LibGUI/Action.h>
  35. #include <LibGUI/Application.h>
  36. #include <LibGUI/BoxLayout.h>
  37. #include <LibGUI/Button.h>
  38. #include <LibGUI/Clipboard.h>
  39. #include <LibGUI/Menu.h>
  40. #include <LibGUI/MenuBar.h>
  41. #include <LibGUI/StatusBar.h>
  42. #include <LibGUI/TabWidget.h>
  43. #include <LibGUI/TextBox.h>
  44. #include <LibGUI/ToolBar.h>
  45. #include <LibGUI/ToolBarContainer.h>
  46. #include <LibGUI/Window.h>
  47. #include <LibJS/Interpreter.h>
  48. #include <LibWeb/CSS/Parser/CSSParser.h>
  49. #include <LibWeb/DOM/Element.h>
  50. #include <LibWeb/DOMTreeModel.h>
  51. #include <LibWeb/Dump.h>
  52. #include <LibWeb/Layout/LayoutBlock.h>
  53. #include <LibWeb/Layout/LayoutDocument.h>
  54. #include <LibWeb/Layout/LayoutInline.h>
  55. #include <LibWeb/Layout/LayoutNode.h>
  56. #include <LibWeb/Loader/ResourceLoader.h>
  57. #include <LibWeb/Page/Frame.h>
  58. #include <LibWeb/PageView.h>
  59. #include <LibWeb/WebContentView.h>
  60. namespace Browser {
  61. URL url_from_user_input(const String& input)
  62. {
  63. auto url = URL(input);
  64. if (url.is_valid())
  65. return url;
  66. StringBuilder builder;
  67. builder.append("http://");
  68. builder.append(input);
  69. return URL(builder.build());
  70. }
  71. Tab::Tab(Type type)
  72. : m_type(type)
  73. {
  74. auto& widget = *this;
  75. set_layout<GUI::VerticalBoxLayout>();
  76. m_toolbar_container = widget.add<GUI::ToolBarContainer>();
  77. auto& toolbar = m_toolbar_container->add<GUI::ToolBar>();
  78. if (m_type == Type::InProcessWebView)
  79. m_page_view = widget.add<Web::PageView>();
  80. else
  81. m_web_content_view = widget.add<WebContentView>();
  82. m_go_back_action = GUI::CommonActions::make_go_back_action([this](auto&) { go_back(); }, this);
  83. m_go_forward_action = GUI::CommonActions::make_go_forward_action([this](auto&) { go_forward(); }, this);
  84. toolbar.add_action(*m_go_back_action);
  85. toolbar.add_action(*m_go_forward_action);
  86. toolbar.add_action(GUI::CommonActions::make_go_home_action([this](auto&) { load(g_home_url); }, this));
  87. m_reload_action = GUI::CommonActions::make_reload_action([this](auto&) { reload(); }, this);
  88. toolbar.add_action(*m_reload_action);
  89. m_location_box = toolbar.add<GUI::TextBox>();
  90. m_location_box->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
  91. m_location_box->set_preferred_size(0, 22);
  92. m_location_box->on_return_pressed = [this] {
  93. auto url = url_from_user_input(m_location_box->text());
  94. load(url);
  95. view().set_focus(true);
  96. };
  97. m_location_box->add_custom_context_menu_action(GUI::Action::create("Paste & Go", [this](auto&) {
  98. m_location_box->set_text(GUI::Clipboard::the().data());
  99. m_location_box->on_return_pressed();
  100. }));
  101. m_bookmark_button = toolbar.add<GUI::Button>();
  102. m_bookmark_button->set_button_style(Gfx::ButtonStyle::CoolBar);
  103. m_bookmark_button->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/bookmark-contour.png"));
  104. m_bookmark_button->set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fixed);
  105. m_bookmark_button->set_preferred_size(22, 22);
  106. m_bookmark_button->on_click = [this](auto) {
  107. auto url = this->url().to_string();
  108. if (BookmarksBarWidget::the().contains_bookmark(url)) {
  109. BookmarksBarWidget::the().remove_bookmark(url);
  110. } else {
  111. BookmarksBarWidget::the().add_bookmark(url, m_title);
  112. }
  113. update_bookmark_button(url);
  114. };
  115. hooks().on_load_start = [this](auto& url) {
  116. m_location_box->set_icon(nullptr);
  117. m_location_box->set_text(url.to_string());
  118. update_actions();
  119. update_bookmark_button(url.to_string());
  120. };
  121. hooks().on_link_click = [this](auto& url, auto& target, unsigned modifiers) {
  122. if (target == "_blank" || modifiers == Mod_Ctrl) {
  123. on_tab_open_request(url);
  124. } else {
  125. load(url);
  126. }
  127. };
  128. m_link_context_menu = GUI::Menu::construct();
  129. m_link_context_menu->add_action(GUI::Action::create("Open", [this](auto&) {
  130. hooks().on_link_click(m_link_context_menu_url, "", 0);
  131. }));
  132. m_link_context_menu->add_action(GUI::Action::create("Open in new tab", [this](auto&) {
  133. hooks().on_link_click(m_link_context_menu_url, "_blank", 0);
  134. }));
  135. m_link_context_menu->add_action(GUI::Action::create("Copy link", [this](auto&) {
  136. GUI::Clipboard::the().set_data(m_link_context_menu_url.to_string());
  137. }));
  138. m_link_context_menu->add_separator();
  139. m_link_context_menu->add_action(GUI::Action::create("Download", [this](auto&) {
  140. auto window = GUI::Window::construct();
  141. window->resize(300, 150);
  142. auto url = m_link_context_menu_url;
  143. window->set_title(String::format("0%% of %s", url.basename().characters()));
  144. window->set_resizable(false);
  145. window->set_main_widget<DownloadWidget>(url);
  146. window->show();
  147. (void)window.leak_ref();
  148. }));
  149. hooks().on_link_context_menu_request = [this](auto& url, auto& screen_position) {
  150. m_link_context_menu_url = url;
  151. m_link_context_menu->popup(screen_position);
  152. };
  153. hooks().on_link_middle_click = [this](auto& href, auto&, auto) {
  154. hooks().on_link_click(href, "_blank", 0);
  155. };
  156. hooks().on_title_change = [this](auto& title) {
  157. if (title.is_null()) {
  158. m_title = url().to_string();
  159. } else {
  160. m_title = title;
  161. }
  162. if (on_title_change)
  163. on_title_change(m_title);
  164. };
  165. hooks().on_favicon_change = [this](auto& icon) {
  166. m_icon = icon;
  167. m_location_box->set_icon(&icon);
  168. if (on_favicon_change)
  169. on_favicon_change(icon);
  170. };
  171. // FIXME: Support JS console in multi-process mode.
  172. if (m_type == Type::InProcessWebView) {
  173. hooks().on_set_document = [this](auto* document) {
  174. if (document && m_console_window) {
  175. auto* console_widget = static_cast<ConsoleWidget*>(m_console_window->main_widget());
  176. console_widget->set_interpreter(document->interpreter().make_weak_ptr());
  177. }
  178. };
  179. }
  180. auto focus_location_box_action = GUI::Action::create(
  181. "Focus location box", { Mod_Ctrl, Key_L }, [this](auto&) {
  182. m_location_box->select_all();
  183. m_location_box->set_focus(true);
  184. },
  185. this);
  186. m_statusbar = widget.add<GUI::StatusBar>();
  187. hooks().on_link_hover = [this](auto& url) {
  188. if (url.is_valid())
  189. m_statusbar->set_text(url.to_string());
  190. else
  191. m_statusbar->set_text("");
  192. };
  193. hooks().on_url_drop = [this](auto& url) {
  194. load(url);
  195. };
  196. m_menubar = GUI::MenuBar::construct();
  197. auto& app_menu = m_menubar->add_menu("Browser");
  198. app_menu.add_action(WindowActions::the().create_new_tab_action());
  199. app_menu.add_action(GUI::Action::create(
  200. "Close tab", { Mod_Ctrl, Key_W }, Gfx::Bitmap::load_from_file("/res/icons/16x16/close-tab.png"), [this](auto&) {
  201. on_tab_close_request(*this);
  202. },
  203. this));
  204. app_menu.add_action(*m_reload_action);
  205. app_menu.add_separator();
  206. app_menu.add_action(GUI::CommonActions::make_quit_action([](auto&) {
  207. GUI::Application::the()->quit();
  208. }));
  209. auto& view_menu = m_menubar->add_menu("View");
  210. view_menu.add_action(GUI::CommonActions::make_fullscreen_action(
  211. [this](auto&) {
  212. window()->set_fullscreen(!window()->is_fullscreen());
  213. auto is_fullscreen = window()->is_fullscreen();
  214. auto* tab_widget = static_cast<GUI::TabWidget*>(parent_widget());
  215. tab_widget->set_bar_visible(!is_fullscreen && tab_widget->children().size() > 1);
  216. m_toolbar_container->set_visible(!is_fullscreen);
  217. m_statusbar->set_visible(!is_fullscreen);
  218. },
  219. this));
  220. auto view_source_action = GUI::Action::create(
  221. "View source", { Mod_Ctrl, Key_U }, [this](auto&) {
  222. if (m_type == Type::InProcessWebView) {
  223. ASSERT(m_page_view->document());
  224. auto url = m_page_view->document()->url().to_string();
  225. auto source = m_page_view->document()->source();
  226. auto window = GUI::Window::construct();
  227. auto& editor = window->set_main_widget<GUI::TextEditor>();
  228. editor.set_text(source);
  229. editor.set_mode(GUI::TextEditor::ReadOnly);
  230. editor.set_ruler_visible(true);
  231. window->resize(640, 480);
  232. window->set_title(url);
  233. window->show();
  234. (void)window.leak_ref();
  235. } else {
  236. TODO();
  237. }
  238. },
  239. this);
  240. auto inspect_dom_tree_action = GUI::Action::create(
  241. "Inspect DOM tree", { Mod_None, Key_F12 }, [this](auto&) {
  242. if (m_type == Type::InProcessWebView) {
  243. if (!m_dom_inspector_window) {
  244. m_dom_inspector_window = GUI::Window::construct();
  245. m_dom_inspector_window->resize(300, 500);
  246. m_dom_inspector_window->set_title("DOM inspector");
  247. m_dom_inspector_window->set_main_widget<InspectorWidget>();
  248. }
  249. auto* inspector_widget = static_cast<InspectorWidget*>(m_dom_inspector_window->main_widget());
  250. inspector_widget->set_document(m_page_view->document());
  251. m_dom_inspector_window->show();
  252. m_dom_inspector_window->move_to_front();
  253. } else {
  254. TODO();
  255. }
  256. },
  257. this);
  258. auto& inspect_menu = m_menubar->add_menu("Inspect");
  259. inspect_menu.add_action(*view_source_action);
  260. inspect_menu.add_action(*inspect_dom_tree_action);
  261. inspect_menu.add_action(GUI::Action::create(
  262. "Open JS Console", { Mod_Ctrl, Key_I }, [this](auto&) {
  263. if (m_type == Type::InProcessWebView) {
  264. if (!m_console_window) {
  265. m_console_window = GUI::Window::construct();
  266. m_console_window->resize(500, 300);
  267. m_console_window->set_title("JS Console");
  268. m_console_window->set_main_widget<ConsoleWidget>();
  269. }
  270. auto* console_widget = static_cast<ConsoleWidget*>(m_console_window->main_widget());
  271. console_widget->set_interpreter(m_page_view->document()->interpreter().make_weak_ptr());
  272. m_console_window->show();
  273. m_console_window->move_to_front();
  274. } else {
  275. TODO();
  276. }
  277. },
  278. this));
  279. auto& debug_menu = m_menubar->add_menu("Debug");
  280. debug_menu.add_action(GUI::Action::create(
  281. "Dump DOM tree", [this](auto&) {
  282. if (m_type == Type::InProcessWebView) {
  283. Web::dump_tree(*m_page_view->document());
  284. } else {
  285. TODO();
  286. }
  287. },
  288. this));
  289. debug_menu.add_action(GUI::Action::create(
  290. "Dump Layout tree", [this](auto&) {
  291. if (m_type == Type::InProcessWebView) {
  292. Web::dump_tree(*m_page_view->document()->layout_node());
  293. } else {
  294. TODO();
  295. }
  296. },
  297. this));
  298. debug_menu.add_action(GUI::Action::create(
  299. "Dump Style sheets", [this](auto&) {
  300. if (m_type == Type::InProcessWebView) {
  301. for (auto& sheet : m_page_view->document()->style_sheets().sheets()) {
  302. Web::dump_sheet(sheet);
  303. }
  304. } else {
  305. TODO();
  306. }
  307. },
  308. this));
  309. debug_menu.add_action(GUI::Action::create("Dump history", { Mod_Ctrl, Key_H }, [&](auto&) {
  310. m_history.dump();
  311. }));
  312. debug_menu.add_separator();
  313. auto line_box_borders_action = GUI::Action::create_checkable(
  314. "Line box borders", [this](auto& action) {
  315. if (m_type == Type::InProcessWebView) {
  316. m_page_view->set_should_show_line_box_borders(action.is_checked());
  317. m_page_view->update();
  318. } else {
  319. TODO();
  320. }
  321. },
  322. this);
  323. line_box_borders_action->set_checked(false);
  324. debug_menu.add_action(line_box_borders_action);
  325. auto& bookmarks_menu = m_menubar->add_menu("Bookmarks");
  326. bookmarks_menu.add_action(WindowActions::the().show_bookmarks_bar_action());
  327. auto& help_menu = m_menubar->add_menu("Help");
  328. help_menu.add_action(WindowActions::the().about_action());
  329. m_tab_context_menu = GUI::Menu::construct();
  330. m_tab_context_menu->add_action(GUI::Action::create("Reload Tab", [this](auto&) {
  331. m_reload_action->activate();
  332. }));
  333. m_tab_context_menu->add_action(GUI::Action::create("Close Tab", [this](auto&) {
  334. on_tab_close_request(*this);
  335. }));
  336. m_page_context_menu = GUI::Menu::construct();
  337. m_page_context_menu->add_action(*m_go_back_action);
  338. m_page_context_menu->add_action(*m_go_forward_action);
  339. m_page_context_menu->add_action(*m_reload_action);
  340. m_page_context_menu->add_separator();
  341. m_page_context_menu->add_action(*view_source_action);
  342. m_page_context_menu->add_action(*inspect_dom_tree_action);
  343. hooks().on_context_menu_request = [&](auto& screen_position) {
  344. m_page_context_menu->popup(screen_position);
  345. };
  346. }
  347. Tab::~Tab()
  348. {
  349. }
  350. void Tab::load(const URL& url, LoadType load_type)
  351. {
  352. if (load_type == LoadType::Normal)
  353. m_history.push(url);
  354. if (m_type == Type::InProcessWebView)
  355. m_page_view->load(url);
  356. else
  357. m_web_content_view->load(url);
  358. }
  359. URL Tab::url() const
  360. {
  361. if (m_type == Type::InProcessWebView)
  362. return m_page_view->url();
  363. return m_web_content_view->url();
  364. }
  365. void Tab::reload()
  366. {
  367. load(url());
  368. }
  369. void Tab::go_back()
  370. {
  371. m_history.go_back();
  372. update_actions();
  373. load(m_history.current(), LoadType::HistoryNavigation);
  374. }
  375. void Tab::go_forward()
  376. {
  377. m_history.go_forward();
  378. update_actions();
  379. load(m_history.current(), LoadType::HistoryNavigation);
  380. }
  381. void Tab::update_actions()
  382. {
  383. m_go_back_action->set_enabled(m_history.can_go_back());
  384. m_go_forward_action->set_enabled(m_history.can_go_forward());
  385. }
  386. void Tab::update_bookmark_button(const String& url)
  387. {
  388. if (BookmarksBarWidget::the().contains_bookmark(url)) {
  389. m_bookmark_button->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/bookmark-filled.png"));
  390. m_bookmark_button->set_tooltip("Remove Bookmark");
  391. } else {
  392. m_bookmark_button->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/bookmark-contour.png"));
  393. m_bookmark_button->set_tooltip("Add Bookmark");
  394. }
  395. }
  396. void Tab::did_become_active()
  397. {
  398. Web::ResourceLoader::the().on_load_counter_change = [this] {
  399. if (Web::ResourceLoader::the().pending_loads() == 0) {
  400. m_statusbar->set_text("");
  401. return;
  402. }
  403. m_statusbar->set_text(String::format("Loading (%d pending resources...)", Web::ResourceLoader::the().pending_loads()));
  404. };
  405. BookmarksBarWidget::the().on_bookmark_click = [this](auto& url, unsigned modifiers) {
  406. if (modifiers & Mod_Ctrl)
  407. on_tab_open_request(url);
  408. else
  409. load(url);
  410. };
  411. BookmarksBarWidget::the().on_bookmark_hover = [this](auto&, auto& url) {
  412. m_statusbar->set_text(url);
  413. };
  414. BookmarksBarWidget::the().remove_from_parent();
  415. m_toolbar_container->add_child(BookmarksBarWidget::the());
  416. auto is_fullscreen = window()->is_fullscreen();
  417. m_toolbar_container->set_visible(!is_fullscreen);
  418. m_statusbar->set_visible(!is_fullscreen);
  419. GUI::Application::the()->set_menubar(m_menubar);
  420. }
  421. void Tab::context_menu_requested(const Gfx::IntPoint& screen_position)
  422. {
  423. m_tab_context_menu->popup(screen_position);
  424. }
  425. GUI::Widget& Tab::view()
  426. {
  427. if (m_type == Type::InProcessWebView)
  428. return *m_page_view;
  429. return *m_web_content_view;
  430. }
  431. Web::WebViewHooks& Tab::hooks()
  432. {
  433. if (m_type == Type::InProcessWebView)
  434. return *m_page_view;
  435. return *m_web_content_view;
  436. }
  437. }