main.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. * Copyright (c) 2018-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 "BookmarksBarWidget.h"
  27. #include "History.h"
  28. #include "InspectorWidget.h"
  29. #include <LibCore/File.h>
  30. #include <LibGUI/AboutDialog.h>
  31. #include <LibGUI/Action.h>
  32. #include <LibGUI/Application.h>
  33. #include <LibGUI/BoxLayout.h>
  34. #include <LibGUI/Menu.h>
  35. #include <LibGUI/MenuBar.h>
  36. #include <LibGUI/StatusBar.h>
  37. #include <LibGUI/TextBox.h>
  38. #include <LibGUI/ToolBar.h>
  39. #include <LibGUI/Window.h>
  40. #include <LibWeb/CSS/StyleResolver.h>
  41. #include <LibWeb/DOM/Element.h>
  42. #include <LibWeb/DOMTreeModel.h>
  43. #include <LibWeb/Dump.h>
  44. #include <LibWeb/HtmlView.h>
  45. #include <LibWeb/Layout/LayoutBlock.h>
  46. #include <LibWeb/Layout/LayoutDocument.h>
  47. #include <LibWeb/Layout/LayoutInline.h>
  48. #include <LibWeb/Layout/LayoutNode.h>
  49. #include <LibWeb/Parser/CSSParser.h>
  50. #include <LibWeb/Parser/HTMLParser.h>
  51. #include <LibWeb/ResourceLoader.h>
  52. #include <stdio.h>
  53. #include <stdlib.h>
  54. static const char* home_url = "file:///home/anon/www/welcome.html";
  55. static const char* bookmarks_filename = "/home/anon/bookmarks.json";
  56. int main(int argc, char** argv)
  57. {
  58. if (pledge("stdio shared_buffer accept unix cpath rpath fattr", nullptr) < 0) {
  59. perror("pledge");
  60. return 1;
  61. }
  62. GUI::Application app(argc, argv);
  63. // Connect to the ProtocolServer immediately so we can drop the "unix" pledge.
  64. Web::ResourceLoader::the();
  65. if (pledge("stdio shared_buffer accept rpath", nullptr) < 0) {
  66. perror("pledge");
  67. return 1;
  68. }
  69. auto window = GUI::Window::construct();
  70. window->set_rect(100, 100, 640, 480);
  71. auto& widget = window->set_main_widget<GUI::Widget>();
  72. widget.set_fill_with_background_color(true);
  73. widget.set_layout<GUI::VerticalBoxLayout>();
  74. widget.layout()->set_spacing(0);
  75. bool bookmarksbar_enabled = true;
  76. auto& toolbar = widget.add<GUI::ToolBar>();
  77. auto& bookmarksbar = widget.add<BookmarksBarWidget>(bookmarks_filename, bookmarksbar_enabled);
  78. auto& html_widget = widget.add<Web::HtmlView>();
  79. bookmarksbar.on_bookmark_click = [&](auto&, auto& url) {
  80. html_widget.load(url);
  81. };
  82. History<URL> history;
  83. RefPtr<GUI::Action> go_back_action;
  84. RefPtr<GUI::Action> go_forward_action;
  85. auto update_actions = [&]() {
  86. go_back_action->set_enabled(history.can_go_back());
  87. go_forward_action->set_enabled(history.can_go_forward());
  88. };
  89. bool should_push_loads_to_history = true;
  90. go_back_action = GUI::CommonActions::make_go_back_action([&](auto&) {
  91. history.go_back();
  92. update_actions();
  93. TemporaryChange<bool> change(should_push_loads_to_history, false);
  94. html_widget.load(history.current());
  95. });
  96. go_forward_action = GUI::CommonActions::make_go_forward_action([&](auto&) {
  97. history.go_forward();
  98. update_actions();
  99. TemporaryChange<bool> change(should_push_loads_to_history, false);
  100. html_widget.load(history.current());
  101. });
  102. toolbar.add_action(*go_back_action);
  103. toolbar.add_action(*go_forward_action);
  104. toolbar.add_action(GUI::CommonActions::make_go_home_action([&](auto&) {
  105. html_widget.load(home_url);
  106. }));
  107. toolbar.add_action(GUI::CommonActions::make_reload_action([&](auto&) {
  108. TemporaryChange<bool> change(should_push_loads_to_history, false);
  109. html_widget.reload();
  110. }));
  111. auto& location_box = toolbar.add<GUI::TextBox>();
  112. location_box.on_return_pressed = [&] {
  113. html_widget.load(location_box.text());
  114. };
  115. html_widget.on_load_start = [&](auto& url) {
  116. location_box.set_text(url.to_string());
  117. if (should_push_loads_to_history)
  118. history.push(url);
  119. update_actions();
  120. };
  121. html_widget.on_link_click = [&](auto& url) {
  122. if (url.starts_with("#")) {
  123. html_widget.scroll_to_anchor(url.substring_view(1, url.length() - 1));
  124. } else {
  125. html_widget.load(html_widget.document()->complete_url(url));
  126. }
  127. };
  128. html_widget.on_title_change = [&](auto& title) {
  129. window->set_title(String::format("%s - Browser", title.characters()));
  130. };
  131. auto focus_location_box_action = GUI::Action::create("Focus location box", { Mod_Ctrl, Key_L }, [&](auto&) {
  132. location_box.select_all();
  133. location_box.set_focus(true);
  134. });
  135. auto& statusbar = widget.add<GUI::StatusBar>();
  136. html_widget.on_link_hover = [&](auto& href) {
  137. statusbar.set_text(href);
  138. };
  139. bookmarksbar.on_bookmark_hover = [&](auto&, auto& url) {
  140. statusbar.set_text(url);
  141. };
  142. Web::ResourceLoader::the().on_load_counter_change = [&] {
  143. if (Web::ResourceLoader::the().pending_loads() == 0) {
  144. statusbar.set_text("");
  145. return;
  146. }
  147. statusbar.set_text(String::format("Loading (%d pending resources...)", Web::ResourceLoader::the().pending_loads()));
  148. };
  149. auto menubar = make<GUI::MenuBar>();
  150. auto app_menu = GUI::Menu::construct("Browser");
  151. app_menu->add_action(GUI::CommonActions::make_quit_action([&](auto&) {
  152. app.quit();
  153. }));
  154. menubar->add_menu(move(app_menu));
  155. RefPtr<GUI::Window> dom_inspector_window;
  156. auto inspect_menu = GUI::Menu::construct("Inspect");
  157. inspect_menu->add_action(GUI::Action::create("View source", { Mod_Ctrl, Key_U }, [&](auto&) {
  158. String filename_to_open;
  159. char tmp_filename[] = "/tmp/view-source.XXXXXX";
  160. ASSERT(html_widget.document());
  161. if (html_widget.document()->url().protocol() == "file") {
  162. filename_to_open = html_widget.document()->url().path();
  163. } else {
  164. int fd = mkstemp(tmp_filename);
  165. ASSERT(fd >= 0);
  166. auto source = html_widget.document()->source();
  167. write(fd, source.characters(), source.length());
  168. close(fd);
  169. filename_to_open = tmp_filename;
  170. }
  171. if (fork() == 0) {
  172. execl("/bin/TextEditor", "TextEditor", filename_to_open.characters(), nullptr);
  173. ASSERT_NOT_REACHED();
  174. }
  175. }));
  176. inspect_menu->add_action(GUI::Action::create("Inspect DOM tree", { Mod_None, Key_F12 }, [&](auto&) {
  177. if (!dom_inspector_window) {
  178. dom_inspector_window = GUI::Window::construct();
  179. dom_inspector_window->set_rect(100, 100, 300, 500);
  180. dom_inspector_window->set_title("DOM inspector");
  181. dom_inspector_window->set_main_widget<InspectorWidget>();
  182. }
  183. auto* inspector_widget = static_cast<InspectorWidget*>(dom_inspector_window->main_widget());
  184. inspector_widget->set_document(html_widget.document());
  185. dom_inspector_window->show();
  186. dom_inspector_window->move_to_front();
  187. }));
  188. menubar->add_menu(move(inspect_menu));
  189. auto debug_menu = GUI::Menu::construct("Debug");
  190. debug_menu->add_action(GUI::Action::create("Dump DOM tree", [&](auto&) {
  191. dump_tree(*html_widget.document());
  192. }));
  193. debug_menu->add_action(GUI::Action::create("Dump Layout tree", [&](auto&) {
  194. dump_tree(*html_widget.document()->layout_node());
  195. }));
  196. debug_menu->add_action(GUI::Action::create("Dump Style sheets", [&](auto&) {
  197. for (auto& sheet : html_widget.document()->stylesheets()) {
  198. dump_sheet(sheet);
  199. }
  200. }));
  201. debug_menu->add_separator();
  202. auto line_box_borders_action = GUI::Action::create("Line box borders", [&](auto& action) {
  203. action.set_checked(!action.is_checked());
  204. html_widget.set_should_show_line_box_borders(action.is_checked());
  205. html_widget.update();
  206. });
  207. line_box_borders_action->set_checkable(true);
  208. line_box_borders_action->set_checked(false);
  209. debug_menu->add_action(line_box_borders_action);
  210. menubar->add_menu(move(debug_menu));
  211. auto bookmarks_menu = GUI::Menu::construct("Bookmarks");
  212. auto show_bookmarksbar_action = GUI::Action::create("Show bookmarks bar", [&](auto& action) {
  213. action.set_checked(!action.is_checked());
  214. bookmarksbar.set_visible(action.is_checked());
  215. bookmarksbar.update();
  216. });
  217. show_bookmarksbar_action->set_checkable(true);
  218. show_bookmarksbar_action->set_checked(bookmarksbar_enabled);
  219. bookmarks_menu->add_action(show_bookmarksbar_action);
  220. menubar->add_menu(move(bookmarks_menu));
  221. auto help_menu = GUI::Menu::construct("Help");
  222. help_menu->add_action(GUI::Action::create("About", [&](const GUI::Action&) {
  223. GUI::AboutDialog::show("Browser", Gfx::Bitmap::load_from_file("/res/icons/32x32/filetype-html.png"), window);
  224. }));
  225. menubar->add_menu(move(help_menu));
  226. app.set_menubar(move(menubar));
  227. window->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/filetype-html.png"));
  228. window->set_title("Browser");
  229. window->show();
  230. URL url_to_load = home_url;
  231. if (app.args().size() >= 1) {
  232. url_to_load = URL();
  233. url_to_load.set_protocol("file");
  234. url_to_load.set_path(app.args()[0]);
  235. }
  236. html_widget.load(url_to_load);
  237. return app.exec();
  238. }