/
browser.cxx
236 lines (203 loc) · 8.33 KB
/
browser.cxx
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
#include "browser.hxx"
#include "browser/client.hxx"
#include "include/views/cef_window.h"
#include <algorithm>
#include <fmt/core.h>
Browser::Window::Window(CefRefPtr<Browser::Client> client, Browser::Details details, CefString url, bool show_devtools):
client(client.get()), show_devtools(show_devtools), details(details), window(nullptr), browser_view(nullptr), browser(nullptr), pending_child(nullptr)
{
fmt::print("[B] Browser::Window constructor, this={}\n", reinterpret_cast<uintptr_t>(this));
this->Init(client, details, url, show_devtools);
}
Browser::Window::Window(CefRefPtr<Browser::Client> client, Browser::Details details, bool show_devtools):
client(client.get()), show_devtools(show_devtools), details(details), window(nullptr), browser_view(nullptr), browser(nullptr), pending_child(nullptr)
{
fmt::print("[B] Browser::Window popup constructor, this={}\n", reinterpret_cast<uintptr_t>(this));
}
bool Browser::Window::IsLauncher() const {
return false;
}
void Browser::Window::Init(CefRefPtr<CefClient> client, Browser::Details details, CefString url, bool show_devtools) {
CefBrowserSettings browser_settings;
browser_settings.background_color = CefColorSetARGB(0, 0, 0, 0);
this->browser_view = CefBrowserView::CreateBrowserView(client, url, browser_settings, nullptr, nullptr, this);
CefWindow::CreateTopLevelWindow(this);
}
void Browser::Window::Refresh() const {
if (this->browser) {
this->browser->GetMainFrame()->SendProcessMessage(PID_RENDERER, CefProcessMessage::Create("__bolt_refresh"));
}
}
void Browser::Window::OnWindowCreated(CefRefPtr<CefWindow> window) {
fmt::print("[B] OnWindowCreated {} this={}\n", window->GetID(), reinterpret_cast<uintptr_t>(this));
this->client->OnBoltWindowCreated(window);
this->window = std::move(window);
this->window->AddChildView(this->browser_view);
if (this->details.center_on_open) {
this->window->CenterWindow(CefSize(this->details.preferred_width, this->details.preferred_height));
}
this->window->SetTitle("Bolt Launcher");
this->window->Show();
}
void Browser::Window::OnWindowClosing(CefRefPtr<CefWindow> window) {
fmt::print("[B] OnWindowClosing {} this={}\n", window->GetID(), reinterpret_cast<uintptr_t>(this));
this->window = nullptr;
this->browser_view = nullptr;
this->pending_child = nullptr;
}
CefRect Browser::Window::GetInitialBounds(CefRefPtr<CefWindow> window) {
return cef_rect_t {
.x = this->details.startx,
.y = this->details.starty,
.width = this->details.preferred_width,
.height = this->details.preferred_height,
};
}
cef_show_state_t Browser::Window::GetInitialShowState(CefRefPtr<CefWindow>) {
return CEF_SHOW_STATE_NORMAL;
}
bool Browser::Window::IsFrameless(CefRefPtr<CefWindow>) {
return !this->details.frame;
}
bool Browser::Window::CanResize(CefRefPtr<CefWindow>) {
return this->details.resizeable;
}
bool Browser::Window::CanMaximize(CefRefPtr<CefWindow>) {
return false;
}
bool Browser::Window::CanMinimize(CefRefPtr<CefWindow>) {
return false;
}
bool Browser::Window::CanClose(CefRefPtr<CefWindow> win) {
fmt::print("[B] CanClose for window {}, this={}\n", window->GetID(), reinterpret_cast<uintptr_t>(this));
// Empty this->children by swapping it with a local empty vector, then iterate the local vector
std::vector<CefRefPtr<Browser::Window>> children;
std::swap(this->children, children);
for (CefRefPtr<Window>& window: children) {
window->browser->GetHost()->CloseBrowser(true);
}
// CEF will call CefLifeSpanHandler::DoClose (implemented in Client), giving us a chance to
// do cleanup and then call TryCloseBrowser() a second time.
// This strategy is suggested by official examples, e.g. cefsimple:
// https://github.com/chromiumembedded/cef/blob/5735/tests/cefsimple/simple_app.cc#L38-L45
CefRefPtr<CefBrowser> browser = this->browser_view->GetBrowser();
if (browser) {
return browser->GetHost()->TryCloseBrowser();
} else {
return true;
}
}
CefSize Browser::Window::GetPreferredSize(CefRefPtr<CefView>) {
return CefSize(this->details.preferred_width, this->details.preferred_height);
}
CefRefPtr<CefBrowserViewDelegate> Browser::Window::GetDelegateForPopupBrowserView(CefRefPtr<CefBrowserView>, const CefBrowserSettings&, CefRefPtr<CefClient>, bool is_devtools) {
fmt::print("[B] GetDelegateForPopupBrowserView this={}\n", reinterpret_cast<uintptr_t>(this));
Browser::Details details = {
.preferred_width = this->popup_features.widthSet ? this->popup_features.width : 800,
.preferred_height = this->popup_features.heightSet ? this->popup_features.height : 608,
.startx = this->popup_features.x,
.starty = this->popup_features.y,
.center_on_open = !this->popup_features.xSet || !this->popup_features.ySet,
.resizeable = true,
.frame = true,
.is_devtools = is_devtools,
};
this->pending_child = new Browser::Window(this->client, details, this->show_devtools);
return this->pending_child;
}
bool Browser::Window::OnPopupBrowserViewCreated(CefRefPtr<CefBrowserView> browser_view, CefRefPtr<CefBrowserView> popup_browser_view, bool is_devtools) {
fmt::print("[B] OnPopupBrowserViewCreated this={}\n", reinterpret_cast<uintptr_t>(this));
this->pending_child->browser_view = popup_browser_view;
CefWindow::CreateTopLevelWindow(this->pending_child);
this->children.push_back(this->pending_child);
CefRefPtr<Browser::Window> child = nullptr;
std::swap(this->pending_child, child);
return true;
}
void Browser::Window::OnBrowserCreated(CefRefPtr<CefBrowserView> browser_view, CefRefPtr<CefBrowser> browser) {
fmt::print("[B] OnBrowserCreated this={} {}\n", reinterpret_cast<uintptr_t>(this), browser->GetIdentifier());
this->browser = browser;
if (this->show_devtools && !this->details.is_devtools) {
this->ShowDevTools();
}
}
void Browser::Window::OnBrowserDestroyed(CefRefPtr<CefBrowserView>, CefRefPtr<CefBrowser>) {
fmt::print("[B] OnBrowserDestroyed this={}\n", reinterpret_cast<uintptr_t>(this));
this->browser = nullptr;
}
CefRefPtr<CefResourceRequestHandler> Browser::Window::GetResourceRequestHandler(
CefRefPtr<CefBrowser>,
CefRefPtr<CefFrame>,
CefRefPtr<CefRequest>,
bool,
bool,
const CefString&,
bool&
) {
// Custom resource handling is implemented by overriding this function in child classes
return nullptr;
}
bool Browser::Window::HasBrowser(CefRefPtr<CefBrowser> browser) const {
if (this->browser->IsSame(browser)) {
return true;
}
bool ret = std::any_of(
this->children.begin(),
this->children.end(),
[&browser](const CefRefPtr<Browser::Window>& window){ return window->HasBrowser(browser); });
return ret;
}
size_t Browser::Window::CountBrowsers() const {
size_t ret = 1;
for (const CefRefPtr<Browser::Window>& child: this->children) {
ret += child->CountBrowsers();
}
return ret;
}
void Browser::Window::Focus() const {
CefRefPtr<CefBrowserHost> host = this->browser->GetHost();
if (host) {
host->SetFocus(true);
}
}
void Browser::Window::Close() {
fmt::print("[B] Close this={}\n", reinterpret_cast<uintptr_t>(this));
// Children will be closed when CEF calls DoClose for this instance
this->browser->GetHost()->CloseBrowser(true);
}
void Browser::Window::CloseChildrenExceptDevtools() {
for (CefRefPtr<Browser::Window>& window: this->children) {
if (!window->details.is_devtools) {
window->Close();
}
}
}
bool Browser::Window::OnBrowserClosing(CefRefPtr<CefBrowser> browser) {
fmt::print("[B] OnBrowserClosing this={}\n", reinterpret_cast<uintptr_t>(this));
this->children.erase(
std::remove_if(
this->children.begin(),
this->children.end(),
[&browser](const CefRefPtr<Browser::Window>& window){ return window->OnBrowserClosing(browser); }
),
this->children.end()
);
return this->browser->IsSame(browser);
}
void Browser::Window::SetPopupFeaturesForBrowser(CefRefPtr<CefBrowser> browser, const CefPopupFeatures& popup_features) {
for (CefRefPtr<Window>& window: this->children) {
window->SetPopupFeaturesForBrowser(browser, popup_features);
}
if (this->browser_view->GetBrowser()->IsSame(browser)) {
this->popup_features = popup_features;
}
}
void Browser::Window::ShowDevTools() {
CefRefPtr<CefBrowserHost> browser_host = browser->GetHost();
CefWindowInfo window_info; // ignored, because this is a BrowserView
CefBrowserSettings browser_settings;
browser_host->ShowDevTools(window_info, browser_host->GetClient(), browser_settings, CefPoint());
}
void Browser::Window::SendMessage(CefString str) {
this->browser->GetMainFrame()->SendProcessMessage(PID_RENDERER, CefProcessMessage::Create(str));
}