main.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 "InspectorWidget.h"
  28. #include "Tab.h"
  29. #include "WindowActions.h"
  30. #include <LibCore/ArgsParser.h>
  31. #include <LibCore/ConfigFile.h>
  32. #include <LibCore/File.h>
  33. #include <LibGUI/AboutDialog.h>
  34. #include <LibGUI/Application.h>
  35. #include <LibGUI/BoxLayout.h>
  36. #include <LibGUI/TabWidget.h>
  37. #include <LibGUI/Window.h>
  38. #include <LibGfx/Bitmap.h>
  39. #include <LibWeb/ResourceLoader.h>
  40. #include <stdio.h>
  41. #include <stdlib.h>
  42. namespace Browser {
  43. static const char* bookmarks_filename = "/home/anon/bookmarks.json";
  44. String g_home_url;
  45. bool g_use_new_html_parser = false;
  46. }
  47. int main(int argc, char** argv)
  48. {
  49. if (getuid() == 0) {
  50. fprintf(stderr, "Refusing to run as root\n");
  51. return 1;
  52. }
  53. if (pledge("stdio shared_buffer accept unix cpath rpath wpath fattr", nullptr) < 0) {
  54. perror("pledge");
  55. return 1;
  56. }
  57. const char* specified_url = nullptr;
  58. Core::ArgsParser args_parser;
  59. args_parser.add_option(Browser::g_use_new_html_parser, "Use new HTML parser", "new-parser", 'n');
  60. args_parser.add_positional_argument(specified_url, "URL to open", "url", Core::ArgsParser::Required::No);
  61. args_parser.parse(argc, argv);
  62. GUI::Application app(argc, argv);
  63. // Connect to the ProtocolServer immediately so we can drop the "unix" pledge.
  64. Web::ResourceLoader::the();
  65. // FIXME: Once there is a standalone Download Manager, we can drop the "unix" pledge.
  66. if (pledge("stdio shared_buffer accept unix cpath rpath wpath", nullptr) < 0) {
  67. perror("pledge");
  68. return 1;
  69. }
  70. if (unveil("/home", "rwc") < 0) {
  71. perror("unveil");
  72. return 1;
  73. }
  74. if (unveil("/res", "r") < 0) {
  75. perror("unveil");
  76. return 1;
  77. }
  78. if (unveil("/etc/passwd", "r") < 0) {
  79. perror("unveil");
  80. return 1;
  81. }
  82. // FIXME: Once there is a standalone Download Manager, we don't need to unveil this
  83. if (unveil("/tmp/portal/launch", "rw") < 0) {
  84. perror("unveil");
  85. return 1;
  86. }
  87. unveil(nullptr, nullptr);
  88. auto m_config = Core::ConfigFile::get_for_app("Browser");
  89. Browser::g_home_url = m_config->read_entry("Preferences", "Home", "about:blank");
  90. bool bookmarksbar_enabled = true;
  91. auto bookmarks_bar = Browser::BookmarksBarWidget::construct(Browser::bookmarks_filename, bookmarksbar_enabled);
  92. auto window = GUI::Window::construct();
  93. window->set_rect(100, 100, 640, 480);
  94. window->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/app-browser.png"));
  95. window->set_title("Browser");
  96. auto& widget = window->set_main_widget<GUI::Widget>();
  97. widget.set_fill_with_background_color(true);
  98. widget.set_layout<GUI::VerticalBoxLayout>();
  99. widget.layout()->set_spacing(2);
  100. auto& tab_widget = widget.add<GUI::TabWidget>();
  101. tab_widget.set_text_alignment(Gfx::TextAlignment::CenterLeft);
  102. tab_widget.set_container_padding(0);
  103. tab_widget.set_uniform_tabs(true);
  104. auto default_favicon = Gfx::Bitmap::load_from_file("/res/icons/16x16/filetype-html.png");
  105. ASSERT(default_favicon);
  106. tab_widget.on_change = [&](auto& active_widget) {
  107. auto& tab = static_cast<Browser::Tab&>(active_widget);
  108. window->set_title(String::format("%s - Browser", tab.title().characters()));
  109. tab.did_become_active();
  110. };
  111. tab_widget.on_middle_click = [&](auto& clicked_widget) {
  112. auto& tab = static_cast<Browser::Tab&>(clicked_widget);
  113. tab.on_tab_close_request(tab);
  114. };
  115. tab_widget.on_context_menu_request = [&](auto& clicked_widget, const GUI::ContextMenuEvent& context_menu_event) {
  116. auto& tab = static_cast<Browser::Tab&>(clicked_widget);
  117. tab.context_menu_requested(context_menu_event.screen_position());
  118. };
  119. Browser::WindowActions window_actions(*window);
  120. Function<void(URL url, bool activate)> create_new_tab;
  121. create_new_tab = [&](auto url, auto activate) {
  122. auto& new_tab = tab_widget.add_tab<Browser::Tab>("New tab");
  123. tab_widget.set_bar_visible(!window->is_fullscreen() && tab_widget.children().size() > 1);
  124. tab_widget.set_tab_icon(new_tab, default_favicon);
  125. new_tab.on_title_change = [&](auto title) {
  126. tab_widget.set_tab_title(new_tab, title);
  127. if (tab_widget.active_widget() == &new_tab)
  128. window->set_title(String::format("%s - Browser", title.characters()));
  129. };
  130. new_tab.on_favicon_change = [&](auto& bitmap) {
  131. tab_widget.set_tab_icon(new_tab, &bitmap);
  132. };
  133. new_tab.on_tab_open_request = [&](auto& url) {
  134. create_new_tab(url, true);
  135. };
  136. new_tab.on_tab_close_request = [&](auto& tab) {
  137. tab_widget.deferred_invoke([&](auto&) {
  138. tab_widget.remove_tab(tab);
  139. tab_widget.set_bar_visible(!window->is_fullscreen() && tab_widget.children().size() > 1);
  140. if (tab_widget.children().is_empty())
  141. app.quit();
  142. });
  143. };
  144. new_tab.load(url);
  145. dbg() << "Added new tab " << &new_tab << ", loading " << url;
  146. if (activate)
  147. tab_widget.set_active_widget(&new_tab);
  148. };
  149. URL first_url = Browser::g_home_url;
  150. if (specified_url)
  151. first_url = URL::create_with_url_or_path(specified_url);
  152. window_actions.on_create_new_tab = [&] {
  153. create_new_tab(Browser::g_home_url, true);
  154. };
  155. window_actions.on_next_tab = [&] {
  156. tab_widget.activate_next_tab();
  157. };
  158. window_actions.on_previous_tab = [&] {
  159. tab_widget.activate_previous_tab();
  160. };
  161. window_actions.on_about = [&] {
  162. GUI::AboutDialog::show("Browser", Gfx::Bitmap::load_from_file("/res/icons/32x32/app-browser.png"), window);
  163. };
  164. window_actions.on_show_bookmarks_bar = [&](auto& action) {
  165. Browser::BookmarksBarWidget::the().set_visible(action.is_checked());
  166. };
  167. window_actions.show_bookmarks_bar_action().set_checked(bookmarksbar_enabled);
  168. create_new_tab(first_url, true);
  169. window->show();
  170. return app.exec();
  171. }