1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
|
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "BookmarksBarWidget.h"
#include "Browser.h"
#include "InspectorWidget.h"
#include "Tab.h"
#include "WindowActions.h"
#include <AK/StringBuilder.h>
#include <LibCore/ArgsParser.h>
#include <LibCore/ConfigFile.h>
#include <LibCore/File.h>
#include <LibCore/StandardPaths.h>
#include <LibGUI/AboutDialog.h>
#include <LibGUI/Application.h>
#include <LibGUI/BoxLayout.h>
#include <LibGUI/TabWidget.h>
#include <LibGUI/Window.h>
#include <LibGfx/Bitmap.h>
#include <LibWeb/Loader/ResourceLoader.h>
#include <stdio.h>
#include <stdlib.h>
namespace Browser {
String g_home_url;
bool g_multi_process = false;
static String bookmarks_file_path()
{
StringBuilder builder;
builder.append(Core::StandardPaths::config_directory());
builder.append("/bookmarks.json");
return builder.to_string();
}
}
int main(int argc, char** argv)
{
if (getuid() == 0) {
fprintf(stderr, "Refusing to run as root\n");
return 1;
}
if (pledge("stdio shared_buffer accept unix cpath rpath wpath fattr", nullptr) < 0) {
perror("pledge");
return 1;
}
const char* specified_url = nullptr;
Core::ArgsParser args_parser;
args_parser.add_option(Browser::g_multi_process, "Multi-process mode", "multi-process", 'm');
args_parser.add_positional_argument(specified_url, "URL to open", "url", Core::ArgsParser::Required::No);
args_parser.parse(argc, argv);
auto app = GUI::Application::construct(argc, argv);
// Connect to the ProtocolServer immediately so we can drop the "unix" pledge.
Web::ResourceLoader::the();
// FIXME: Once there is a standalone Download Manager, we can drop the "unix" pledge.
if (pledge("stdio shared_buffer accept unix cpath rpath wpath", nullptr) < 0) {
perror("pledge");
return 1;
}
if (unveil("/home", "rwc") < 0) {
perror("unveil");
return 1;
}
if (unveil("/res", "r") < 0) {
perror("unveil");
return 1;
}
if (unveil("/etc/passwd", "r") < 0) {
perror("unveil");
return 1;
}
// FIXME: Once there is a standalone Download Manager, we don't need to unveil this
if (unveil("/tmp/portal/launch", "rw") < 0) {
perror("unveil");
return 1;
}
if (unveil("/tmp/portal/image", "rw") < 0) {
perror("unveil");
return 1;
}
if (unveil("/tmp/portal/webcontent", "rw") < 0) {
perror("unveil");
return 1;
}
unveil(nullptr, nullptr);
auto m_config = Core::ConfigFile::get_for_app("Browser");
Browser::g_home_url = m_config->read_entry("Preferences", "Home", "about:blank");
bool bookmarksbar_enabled = true;
auto bookmarks_bar = Browser::BookmarksBarWidget::construct(Browser::bookmarks_file_path(), bookmarksbar_enabled);
auto window = GUI::Window::construct();
window->resize(640, 480);
window->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/app-browser.png"));
window->set_title("Browser");
auto& widget = window->set_main_widget<GUI::Widget>();
widget.set_fill_with_background_color(true);
widget.set_layout<GUI::VerticalBoxLayout>();
widget.layout()->set_spacing(2);
auto& tab_widget = widget.add<GUI::TabWidget>();
tab_widget.set_text_alignment(Gfx::TextAlignment::CenterLeft);
tab_widget.set_container_padding(0);
tab_widget.set_uniform_tabs(true);
auto default_favicon = Gfx::Bitmap::load_from_file("/res/icons/16x16/filetype-html.png");
ASSERT(default_favicon);
tab_widget.on_change = [&](auto& active_widget) {
auto& tab = static_cast<Browser::Tab&>(active_widget);
window->set_title(String::format("%s - Browser", tab.title().characters()));
tab.did_become_active();
};
tab_widget.on_middle_click = [&](auto& clicked_widget) {
auto& tab = static_cast<Browser::Tab&>(clicked_widget);
tab.on_tab_close_request(tab);
};
tab_widget.on_context_menu_request = [&](auto& clicked_widget, const GUI::ContextMenuEvent& context_menu_event) {
auto& tab = static_cast<Browser::Tab&>(clicked_widget);
tab.context_menu_requested(context_menu_event.screen_position());
};
Browser::WindowActions window_actions(*window);
Function<void(URL url, bool activate)> create_new_tab;
create_new_tab = [&](auto url, auto activate) {
auto type = Browser::g_multi_process ? Browser::Tab::Type::OutOfProcessWebView : Browser::Tab::Type::InProcessWebView;
auto& new_tab = tab_widget.add_tab<Browser::Tab>("New tab", type);
tab_widget.set_bar_visible(!window->is_fullscreen() && tab_widget.children().size() > 1);
tab_widget.set_tab_icon(new_tab, default_favicon);
new_tab.on_title_change = [&](auto title) {
tab_widget.set_tab_title(new_tab, title);
if (tab_widget.active_widget() == &new_tab)
window->set_title(String::format("%s - Browser", title.characters()));
};
new_tab.on_favicon_change = [&](auto& bitmap) {
tab_widget.set_tab_icon(new_tab, &bitmap);
};
new_tab.on_tab_open_request = [&](auto& url) {
create_new_tab(url, true);
};
new_tab.on_tab_close_request = [&](auto& tab) {
tab_widget.deferred_invoke([&](auto&) {
tab_widget.remove_tab(tab);
tab_widget.set_bar_visible(!window->is_fullscreen() && tab_widget.children().size() > 1);
if (tab_widget.children().is_empty())
app->quit();
});
};
new_tab.load(url);
dbg() << "Added new tab " << &new_tab << ", loading " << url;
if (activate)
tab_widget.set_active_widget(&new_tab);
};
URL first_url = Browser::g_home_url;
if (specified_url) {
if (Core::File::exists(specified_url)) {
first_url = URL::create_with_file_protocol(Core::File::real_path_for(specified_url));
} else {
first_url = Browser::url_from_user_input(specified_url);
}
}
window_actions.on_create_new_tab = [&] {
create_new_tab(Browser::g_home_url, true);
};
window_actions.on_next_tab = [&] {
tab_widget.activate_next_tab();
};
window_actions.on_previous_tab = [&] {
tab_widget.activate_previous_tab();
};
window_actions.on_about = [&] {
GUI::AboutDialog::show("Browser", Gfx::Bitmap::load_from_file("/res/icons/32x32/app-browser.png"), window);
};
window_actions.on_show_bookmarks_bar = [&](auto& action) {
Browser::BookmarksBarWidget::the().set_visible(action.is_checked());
};
window_actions.show_bookmarks_bar_action().set_checked(bookmarksbar_enabled);
create_new_tab(first_url, true);
window->show();
return app->exec();
}
|