main.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * Copyright (c) 2018-2021, 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 "Browser.h"
  28. #include "InspectorWidget.h"
  29. #include "Tab.h"
  30. #include "WindowActions.h"
  31. #include <AK/StringBuilder.h>
  32. #include <Applications/Browser/BrowserWindowGML.h>
  33. #include <LibCore/ArgsParser.h>
  34. #include <LibCore/ConfigFile.h>
  35. #include <LibCore/File.h>
  36. #include <LibCore/StandardPaths.h>
  37. #include <LibDesktop/Launcher.h>
  38. #include <LibGUI/AboutDialog.h>
  39. #include <LibGUI/Application.h>
  40. #include <LibGUI/BoxLayout.h>
  41. #include <LibGUI/Icon.h>
  42. #include <LibGUI/TabWidget.h>
  43. #include <LibGUI/Window.h>
  44. #include <LibGfx/Bitmap.h>
  45. #include <LibWeb/Loader/ContentFilter.h>
  46. #include <LibWeb/Loader/ResourceLoader.h>
  47. #include <stdio.h>
  48. #include <stdlib.h>
  49. namespace Browser {
  50. String g_home_url;
  51. static bool s_single_process = false;
  52. static String bookmarks_file_path()
  53. {
  54. StringBuilder builder;
  55. builder.append(Core::StandardPaths::config_directory());
  56. builder.append("/bookmarks.json");
  57. return builder.to_string();
  58. }
  59. }
  60. int main(int argc, char** argv)
  61. {
  62. if (getuid() == 0) {
  63. warnln("Refusing to run as root");
  64. return 1;
  65. }
  66. if (pledge("stdio recvfd sendfd accept unix cpath rpath wpath fattr", nullptr) < 0) {
  67. perror("pledge");
  68. return 1;
  69. }
  70. const char* specified_url = nullptr;
  71. Core::ArgsParser args_parser;
  72. args_parser.add_option(Browser::s_single_process, "Single-process mode", "single-process", 's');
  73. args_parser.add_positional_argument(specified_url, "URL to open", "url", Core::ArgsParser::Required::No);
  74. args_parser.parse(argc, argv);
  75. auto app = GUI::Application::construct(argc, argv);
  76. // Connect to the ProtocolServer immediately so we can drop the "unix" pledge.
  77. Web::ResourceLoader::the();
  78. // Connect to LaunchServer immediately and let it know that we won't ask for anything other than opening
  79. // the user's downloads directory.
  80. // FIXME: This should go away with a standalone download manager at some point.
  81. if (!Desktop::Launcher::add_allowed_url(URL::create_with_file_protocol(Core::StandardPaths::downloads_directory()))
  82. || !Desktop::Launcher::seal_allowlist()) {
  83. warnln("Failed to set up allowed launch URLs");
  84. return 1;
  85. }
  86. if (pledge("stdio recvfd sendfd accept unix cpath rpath wpath", nullptr) < 0) {
  87. perror("pledge");
  88. return 1;
  89. }
  90. if (unveil("/home", "rwc") < 0) {
  91. perror("unveil");
  92. return 1;
  93. }
  94. if (unveil("/res", "r") < 0) {
  95. perror("unveil");
  96. return 1;
  97. }
  98. if (unveil("/etc/passwd", "r") < 0) {
  99. perror("unveil");
  100. return 1;
  101. }
  102. if (unveil("/tmp/portal/image", "rw") < 0) {
  103. perror("unveil");
  104. return 1;
  105. }
  106. if (unveil("/tmp/portal/webcontent", "rw") < 0) {
  107. perror("unveil");
  108. return 1;
  109. }
  110. unveil(nullptr, nullptr);
  111. auto app_icon = GUI::Icon::default_icon("app-browser");
  112. auto m_config = Core::ConfigFile::get_for_app("Browser");
  113. Browser::g_home_url = m_config->read_entry("Preferences", "Home", "about:blank");
  114. auto ad_filter_list_or_error = Core::File::open(String::formatted("{}/BrowserContentFilters.txt", Core::StandardPaths::config_directory()), Core::IODevice::ReadOnly);
  115. if (!ad_filter_list_or_error.is_error()) {
  116. auto& ad_filter_list = *ad_filter_list_or_error.value();
  117. while (!ad_filter_list.eof()) {
  118. auto line = ad_filter_list.read_line();
  119. if (line.is_empty())
  120. continue;
  121. Web::ContentFilter::the().add_pattern(line);
  122. }
  123. }
  124. bool bookmarksbar_enabled = true;
  125. auto bookmarks_bar = Browser::BookmarksBarWidget::construct(Browser::bookmarks_file_path(), bookmarksbar_enabled);
  126. auto window = GUI::Window::construct();
  127. window->resize(640, 480);
  128. window->set_icon(app_icon.bitmap_for_size(16));
  129. window->set_title("Browser");
  130. auto& widget = window->set_main_widget<GUI::Widget>();
  131. widget.load_from_gml(browser_window_gml);
  132. auto& tab_widget = *widget.find_descendant_of_type_named<GUI::TabWidget>("tab_widget");
  133. auto default_favicon = Gfx::Bitmap::load_from_file("/res/icons/16x16/filetype-html.png");
  134. ASSERT(default_favicon);
  135. tab_widget.on_change = [&](auto& active_widget) {
  136. auto& tab = static_cast<Browser::Tab&>(active_widget);
  137. window->set_title(String::formatted("{} - Browser", tab.title()));
  138. tab.did_become_active();
  139. };
  140. tab_widget.on_middle_click = [&](auto& clicked_widget) {
  141. auto& tab = static_cast<Browser::Tab&>(clicked_widget);
  142. tab.on_tab_close_request(tab);
  143. };
  144. tab_widget.on_context_menu_request = [&](auto& clicked_widget, const GUI::ContextMenuEvent& context_menu_event) {
  145. auto& tab = static_cast<Browser::Tab&>(clicked_widget);
  146. tab.context_menu_requested(context_menu_event.screen_position());
  147. };
  148. Browser::WindowActions window_actions(*window);
  149. Function<void(URL url, bool activate)> create_new_tab;
  150. create_new_tab = [&](auto url, auto activate) {
  151. auto type = Browser::s_single_process ? Browser::Tab::Type::InProcessWebView : Browser::Tab::Type::OutOfProcessWebView;
  152. auto& new_tab = tab_widget.add_tab<Browser::Tab>("New tab", type);
  153. tab_widget.set_bar_visible(!window->is_fullscreen() && tab_widget.children().size() > 1);
  154. tab_widget.set_tab_icon(new_tab, default_favicon);
  155. new_tab.on_title_change = [&](auto title) {
  156. tab_widget.set_tab_title(new_tab, title);
  157. if (tab_widget.active_widget() == &new_tab)
  158. window->set_title(String::formatted("{} - Browser", title));
  159. };
  160. new_tab.on_favicon_change = [&](auto& bitmap) {
  161. tab_widget.set_tab_icon(new_tab, &bitmap);
  162. };
  163. new_tab.on_tab_open_request = [&](auto& url) {
  164. create_new_tab(url, true);
  165. };
  166. new_tab.on_tab_close_request = [&](auto& tab) {
  167. tab_widget.deferred_invoke([&](auto&) {
  168. tab_widget.remove_tab(tab);
  169. tab_widget.set_bar_visible(!window->is_fullscreen() && tab_widget.children().size() > 1);
  170. if (tab_widget.children().is_empty())
  171. app->quit();
  172. });
  173. };
  174. new_tab.load(url);
  175. dbgln("Added new tab {:p}, loading {}", &new_tab, url);
  176. if (activate)
  177. tab_widget.set_active_widget(&new_tab);
  178. };
  179. URL first_url = Browser::g_home_url;
  180. if (specified_url) {
  181. if (Core::File::exists(specified_url)) {
  182. first_url = URL::create_with_file_protocol(Core::File::real_path_for(specified_url));
  183. } else {
  184. first_url = Browser::url_from_user_input(specified_url);
  185. }
  186. }
  187. window_actions.on_create_new_tab = [&] {
  188. create_new_tab(Browser::g_home_url, true);
  189. };
  190. window_actions.on_next_tab = [&] {
  191. tab_widget.activate_next_tab();
  192. };
  193. window_actions.on_previous_tab = [&] {
  194. tab_widget.activate_previous_tab();
  195. };
  196. window_actions.on_about = [&] {
  197. GUI::AboutDialog::show("Browser", app_icon.bitmap_for_size(32), window);
  198. };
  199. window_actions.on_show_bookmarks_bar = [&](auto& action) {
  200. Browser::BookmarksBarWidget::the().set_visible(action.is_checked());
  201. };
  202. window_actions.show_bookmarks_bar_action().set_checked(bookmarksbar_enabled);
  203. create_new_tab(first_url, true);
  204. window->show();
  205. return app->exec();
  206. }