-
Notifications
You must be signed in to change notification settings - Fork 1
/
handler.cpp
379 lines (296 loc) · 9.91 KB
/
handler.cpp
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
// Copyright (c) 2013 The Chromium Embedded Framework Authors. All rights
// reserved. Use of this source code is governed by a BSD-style license that
// can be found in the LICENSE file.
#include "handler.h"
#include <sstream>
#include <string>
#include "include/base/cef_bind.h"
#include "include/cef_app.h"
#include "include/cef_parser.h"
#include "include/views/cef_browser_view.h"
#include "include/views/cef_window.h"
#include "include/wrapper/cef_closure_task.h"
#include "include/wrapper/cef_helpers.h"
#include "app.h"
#include "Manager.h"
#include "AppManager.h"
namespace
{
SimpleHandler* g_instance = nullptr;
// Returns a data: URI with the specified contents.
std::string GetDataURI(const std::string& data, const std::string& mime_type)
{
return "data:" + mime_type + ";base64," + CefURIEncode(CefBase64Encode(data.data(), data.size()), false).ToString();
}
} // namespace
SimpleHandler::SimpleHandler(bool use_views) : use_views_(use_views), is_closing_(false)
{
DCHECK(!g_instance);
g_instance = this;
}
SimpleHandler::~SimpleHandler()
{
g_instance = nullptr;
}
// static
SimpleHandler* SimpleHandler::GetInstance()
{
return g_instance;
}
void SimpleHandler::OnLoadEnd(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
int httpStatusCode)
{
if (!frame->IsMain()) {
return;
}
//
if (!GLOBAL::GetBrowser())
{
GLOBAL::get().browser_ = browser;
GLOBAL::get().appmgr_ = new APP_MGR;
GLOBAL::get().appmgr_->Init();
}
// SHOW THE WINDOW WHEN THE MAIN FRAME IS LOADED
if (!IsWindowVisible(GLOBAL::GetWindow()))
{
ShowWindow(GLOBAL::GetWindow(), SW_SHOW);
UpdateWindow(GLOBAL::GetWindow());
BringWindowToTop(GLOBAL::GetWindow());
}
printf("Main UI loaded! \n");
}
void SimpleHandler::OnRenderProcessTerminated(CefRefPtr<CefBrowser> browser,
TerminationStatus status)
{
printf("RENDER PROCESS ENDED, CODE %d \n", status);
browser->Reload();
}
bool SimpleHandler::OnProcessMessageReceived(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
CefProcessId source_process,
CefRefPtr<CefProcessMessage> message)
{
const auto name = message->GetName();
printf("PROCESS MESSAGE RECEIVED NAME: %s\n", name.ToString().c_str());
if (name == "SYNC_CONF_INT")
{
printf("CONF SYNC REC %s, %d \n", message->GetArgumentList()->GetString(0).ToString().c_str(), message->GetArgumentList()->GetInt(1));
GLOBAL::GetConfig()->SetValue(message->GetArgumentList()->GetString(0).ToString().c_str(), message->GetArgumentList()->GetInt(1));
return true;
}
const auto arg = message->GetArgumentList()->GetInt(0);
if (signal::ProcessSignal(arg)) {
return true;
}
return false;
}
void SimpleHandler::OnAfterCreated(CefRefPtr<CefBrowser> browser)
{
// Add to the list of existing browsers.
browser_list_.push_back(browser);
}
bool SimpleHandler::DoClose(CefRefPtr<CefBrowser> browser)
{
CEF_REQUIRE_UI_THREAD();
if (browser_list_.size() == 1)
{
// Set a flag to indicate that the window close should be allowed.
is_closing_ = true;
}
return false;
}
void SimpleHandler::OnBeforeClose(CefRefPtr<CefBrowser> browser)
{
CEF_REQUIRE_UI_THREAD();
// Remove from the list of existing browsers.
auto bit = browser_list_.begin();
for (; bit != browser_list_.end(); ++bit)
{
if ((*bit)->IsSame(browser))
{
browser_list_.erase(bit);
break;
}
}
if(browser_list_.empty())
{
// All browser windows have closed. Quit the application message loop.
CefQuitMessageLoop();
}
}
void SimpleHandler::OnLoadError(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
ErrorCode errorCode,
const CefString& errorText,
const CefString& failedUrl)
{
CEF_REQUIRE_UI_THREAD();
// Don't display an error for downloaded files.
if (errorCode == ERR_ABORTED)
return;
// Display a load error message using a data: URI.
std::stringstream ss;
ss << "<html><body bgcolor=\"white\">"
"<h2>Failed to load URL "
<< std::string(failedUrl) << " with error " << std::string(errorText)
<< " (" << errorCode << ").</h2></body></html>";
frame->LoadURL(GetDataURI(ss.str(), "text/html"));
}
void SimpleHandler::OnLoadingStateChange(CefRefPtr<CefBrowser> browser,
bool isLoading,
bool canGoBack,
bool canGoForward)
{
}
void SimpleHandler::CloseAllBrowsers(bool force_close)
{
CEF_REQUIRE_UI_THREAD();
if (!CefCurrentlyOn(TID_UI))
{
// Execute on the UI thread.
CefPostTask(TID_UI, base::Bind(&SimpleHandler::CloseAllBrowsers, this, force_close));
return;
}
if (browser_list_.empty())
return;
for (auto it = browser_list_.begin(); it != browser_list_.end(); ++it)
{
(*it)->GetHost()->CloseBrowser(force_close);
}
}
void SimpleHandler::OnTitleChange(CefRefPtr<CefBrowser> browser, const CefString& title)
{
CEF_REQUIRE_UI_THREAD();
if (use_views_)
{
// Set the title of the window using the Views framework.
CefRefPtr<CefBrowserView> browser_view = CefBrowserView::GetForBrowser(browser);
if (browser_view)
{
CefRefPtr<CefWindow> window = browser_view->GetWindow();
if (window)
window->SetTitle(title);
}
}
else
{
// Set the title of the window using platform APIs.
PlatformTitleChange(browser, title);
}
}
//Disable context menu
//Define below two functions to essentially do nothing, overwriting defaults
//See change in simple_handler.h
void SimpleHandler::OnBeforeContextMenu(
CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
CefRefPtr<CefContextMenuParams> params,
CefRefPtr<CefMenuModel> model)
{
CEF_REQUIRE_UI_THREAD();
model->Clear();
}
bool SimpleHandler::OnContextMenuCommand(
CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
CefRefPtr<CefContextMenuParams> params,
int command_id,
EventFlags event_flags)
{
CEF_REQUIRE_UI_THREAD();
// MessageBox(browser->GetHost()->GetWindowHandle(), L"The requested action is not supported", L"Unsupported Action", MB_OK | MB_ICONINFORMATION);
return false;
}
void SimpleHandler::PlatformTitleChange(CefRefPtr<CefBrowser> browser, const CefString& title)
{
CEF_REQUIRE_UI_THREAD();
CefWindowHandle hwnd = browser->GetHost()->GetWindowHandle();
if (hwnd)
{
SetWindowText(hwnd, std::wstring(title).c_str());
}
}
// ************************************/
LPCWSTR kParentWndProc = L"CefParentWndProc";
LPCWSTR kDraggableRegion = L"CefDraggableRegion";
LRESULT CALLBACK SubclassedWindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
WNDPROC hParentWndProc = reinterpret_cast<WNDPROC>(::GetPropW(hWnd, kParentWndProc));
HRGN hRegion = reinterpret_cast<HRGN>(::GetPropW(hWnd, kDraggableRegion));
if (message == WM_NCHITTEST)
{
LRESULT hit = CallWindowProc(hParentWndProc, hWnd, message, wParam, lParam);
if (hit == HTCLIENT) {
POINTS points = MAKEPOINTS(lParam);
POINT point = { points.x, points.y };
::ScreenToClient(hWnd, &point);
if (::PtInRegion(hRegion, point.x, point.y)) {
// Let the parent window handle WM_NCHITTEST by returning HTTRANSPARENT
// in child windows.
return HTTRANSPARENT;
}
}
return hit;
}
return CallWindowProc(hParentWndProc, hWnd, message, wParam, lParam);
}
void SubclassWindow(HWND hWnd, HRGN hRegion)
{
HANDLE hParentWndProc = ::GetPropW(hWnd, kParentWndProc);
if (hParentWndProc)
{
return;
}
SetLastError(0);
LONG_PTR hOldWndProc = SetWindowLongPtr(hWnd, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(SubclassedWindowProc));
if (hOldWndProc == 0 && GetLastError() != ERROR_SUCCESS)
{
return;
}
::SetPropW(hWnd, kParentWndProc, reinterpret_cast<HANDLE>(hOldWndProc));
::SetPropW(hWnd, kDraggableRegion, reinterpret_cast<HANDLE>(hRegion));
}
void UnSubclassWindow(HWND hWnd)
{
LONG_PTR hParentWndProc = reinterpret_cast<LONG_PTR>(::GetPropW(hWnd, kParentWndProc));
if (hParentWndProc)
{
LONG_PTR hPreviousWndProc = SetWindowLongPtr(hWnd, GWLP_WNDPROC, hParentWndProc);
ALLOW_UNUSED_LOCAL(hPreviousWndProc);
DCHECK_EQ(hPreviousWndProc, reinterpret_cast<LONG_PTR>(SubclassedWindowProc));
}
::RemovePropW(hWnd, kParentWndProc);
::RemovePropW(hWnd, kDraggableRegion);
}
BOOL CALLBACK SubclassWindowsProc(HWND hwnd, LPARAM lParam)
{
SubclassWindow(hwnd, reinterpret_cast<HRGN>(lParam));
return TRUE;
}
BOOL CALLBACK UnSubclassWindowsProc(HWND hwnd, LPARAM lParam)
{
UnSubclassWindow(hwnd);
return TRUE;
}
void SimpleHandler::OnDraggableRegionsChanged(
CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
const std::vector<CefDraggableRegion>& regions)
{
CEF_REQUIRE_UI_THREAD();
// Reset draggable region.
::SetRectRgn(GLOBAL::get().draggable_region_, 0, 0, 0, 0);
for (auto it = regions.begin(); it != regions.end(); ++it)
{
HRGN region = ::CreateRectRgn(it->bounds.x, it->bounds.y,
it->bounds.x + it->bounds.width,
it->bounds.y + it->bounds.height);
::CombineRgn(GLOBAL::get().draggable_region_, GLOBAL::get().draggable_region_, region,
it->draggable ? RGN_OR : RGN_DIFF);
::DeleteObject(region);
}
auto hwnd_ = GLOBAL::GetWindow();
WNDENUMPROC proc = !regions.empty() ? SubclassWindowsProc : UnSubclassWindowsProc;
::EnumChildWindows(hwnd_, proc, reinterpret_cast<LPARAM>(GLOBAL::get().draggable_region_));
}