-
Notifications
You must be signed in to change notification settings - Fork 11
/
webview_win.h
350 lines (313 loc) · 10.9 KB
/
webview_win.h
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
//
// ====================================================================
//
// This implementation uses Win32 API to create a native window. It can
// use either EdgeHTML or Edge/Chromium backend as a browser engine.
//
// ====================================================================
//
#define WIN32_LEAN_AND_MEAN
//#define WINVER 0x0605
#include <Shlwapi.h>
#include <codecvt>
#include <stdlib.h>
#include <windows.h>
#include <wrl.h>
#include <winuser.h>
#include <string>
#include <locale>
#include <iostream>
#include "webview2.h"
namespace webview {
using msg_cb_t = std::function<void(const std::string)>;
// Common interface for EdgeHTML and Edge/Chromium
class browser {
public:
virtual ~browser() = default;
virtual bool embed(HWND, bool, msg_cb_t) = 0;
virtual void navigate(const std::string url) = 0;
virtual void eval(const std::string js) = 0;
virtual void init(const std::string js) = 0;
virtual void resize(HWND) = 0;
};
//
// Edge/Chromium browser engine
//
class edge_chromium : public browser {
public:
bool embed(HWND wnd, bool debug, msg_cb_t cb) override {
m_debug = debug;
CoInitialize(0);
std::atomic_flag flag = ATOMIC_FLAG_INIT;
flag.test_and_set();
char currentExePath[MAX_PATH];
GetModuleFileNameA(NULL, currentExePath, MAX_PATH);
char *currentExeName = PathFindFileNameA(currentExePath);
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> wideCharConverter;
std::wstring userDataFolder = wideCharConverter.from_bytes(std::getenv("APPDATA"));
std::wstring currentExeNameW = wideCharConverter.from_bytes(currentExeName);
HRESULT res = CreateCoreWebView2EnvironmentWithOptions(
nullptr, (userDataFolder + L"/" + currentExeNameW).c_str(), nullptr,
new webview2_com_handler(wnd, cb,
[&](ICoreWebView2Controller *controller) {
m_controller = controller;
m_controller->get_CoreWebView2(&m_webview);
m_webview->AddRef();
flag.clear();
}));
if (res != S_OK) {
CoUninitialize();
return false;
}
MSG msg = {};
while (flag.test_and_set() && GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
ICoreWebView2Settings* Settings;
m_webview->get_Settings(&Settings);
Settings->put_AreDevToolsEnabled(m_debug);
init("window.external={invoke:s=>window.chrome.webview.postMessage(s)}");
return true;
}
void resize(HWND wnd) override {
if (m_controller == nullptr) {
return;
}
RECT bounds;
GetClientRect(wnd, &bounds);
m_controller->put_Bounds(bounds);
}
void navigate(const std::string url) override {
auto wurl = to_lpwstr(url);
m_webview->Navigate(wurl);
delete[] wurl;
}
void init(const std::string js) override {
LPCWSTR wjs = to_lpwstr(js);
m_webview->AddScriptToExecuteOnDocumentCreated(wjs, nullptr);
delete[] wjs;
}
void eval(const std::string js) override {
LPCWSTR wjs = to_lpwstr(js);
m_webview->ExecuteScript(wjs, nullptr);
delete[] wjs;
}
private:
LPWSTR to_lpwstr(const std::string s) {
int n = MultiByteToWideChar(CP_UTF8, 0, s.c_str(), -1, NULL, 0);
wchar_t *ws = new wchar_t[n];
MultiByteToWideChar(CP_UTF8, 0, s.c_str(), -1, ws, n);
return ws;
}
bool m_debug;
ICoreWebView2 *m_webview = nullptr;
ICoreWebView2Controller *m_controller = nullptr;
class webview2_com_handler
: public ICoreWebView2CreateCoreWebView2EnvironmentCompletedHandler,
public ICoreWebView2CreateCoreWebView2ControllerCompletedHandler,
public ICoreWebView2WebMessageReceivedEventHandler,
public ICoreWebView2PermissionRequestedEventHandler {
using webview2_com_handler_cb_t = std::function<void(ICoreWebView2Controller *)>;
public:
webview2_com_handler(HWND hwnd, msg_cb_t msgCb,
webview2_com_handler_cb_t cb)
: m_window(hwnd), m_msgCb(msgCb), m_cb(cb) {}
ULONG STDMETHODCALLTYPE AddRef() { return 1; }
ULONG STDMETHODCALLTYPE Release() { return 1; }
HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, LPVOID *ppv) {
return S_OK;
}
HRESULT STDMETHODCALLTYPE Invoke(HRESULT res, ICoreWebView2Environment *env) {
if(res != 0x0){
return res;
}
env->CreateCoreWebView2Controller(m_window, this);
return S_OK;
}
HRESULT STDMETHODCALLTYPE Invoke(HRESULT res,ICoreWebView2Controller *controller) {
if(res != 0x0){
return res;
}
controller->AddRef();
ICoreWebView2 *webview;
::EventRegistrationToken token;
controller->get_CoreWebView2(&webview);
webview->add_WebMessageReceived(this, &token);
webview->add_PermissionRequested(this, &token);
m_cb(controller);
return S_OK;
}
HRESULT STDMETHODCALLTYPE Invoke(ICoreWebView2 *sender, ICoreWebView2WebMessageReceivedEventArgs *args) {
LPWSTR message;
args->TryGetWebMessageAsString(&message);
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> wideCharConverter;
m_msgCb(wideCharConverter.to_bytes(message));
sender->PostWebMessageAsString(message);
CoTaskMemFree(message);
return S_OK;
}
HRESULT STDMETHODCALLTYPE Invoke(ICoreWebView2 *sender,ICoreWebView2PermissionRequestedEventArgs *args) {
COREWEBVIEW2_PERMISSION_KIND kind;
args->get_PermissionKind(&kind);
if (kind == COREWEBVIEW2_PERMISSION_KIND_CLIPBOARD_READ) {
args->put_State(COREWEBVIEW2_PERMISSION_STATE_ALLOW);
}
return S_OK;
}
private:
HWND m_window;
msg_cb_t m_msgCb;
webview2_com_handler_cb_t m_cb;
};
};
class win32_edge_engine {
public:
win32_edge_engine(int width,int height,bool hide,bool debug) {
m_hide = hide;
HINSTANCE hInstance = GetModuleHandle(nullptr);
HICON icon = (HICON)LoadImage(hInstance, IDI_WINLOGO, IMAGE_ICON, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_DEFAULTCOLOR);
WNDCLASSEX wc;
ZeroMemory(&wc, sizeof(WNDCLASSEX));
wc.cbSize = sizeof(WNDCLASSEX);
wc.hInstance = hInstance;
wc.lpszClassName = "webview";
wc.hIcon = icon;
wc.hIconSm = icon;
wc.hbrBackground = CreateSolidBrush(RGB(255,255,255));
wc.lpfnWndProc =
(WNDPROC)(+[](HWND hwnd, UINT msg, WPARAM wp, LPARAM lp) -> int {
auto w = (win32_edge_engine *)GetWindowLongPtr(hwnd, GWLP_USERDATA);
switch (msg) {
case WM_SIZE:
w->m_browser->resize(hwnd);
break;
case WM_CLOSE:
if(w->m_hide){
ShowWindow(w->m_window, SW_HIDE);
}else{
DestroyWindow(hwnd);
}
break;
case WM_DESTROY:
w->terminate();
break;
case WM_GETMINMAXINFO: {
auto lpmmi = (LPMINMAXINFO)lp;
if (w == nullptr) {
return 0;
}
if (w->m_maxsz.x > 0 && w->m_maxsz.y > 0) {
lpmmi->ptMaxSize = w->m_maxsz;
lpmmi->ptMaxTrackSize = w->m_maxsz;
}
if (w->m_minsz.x > 0 && w->m_minsz.y > 0) {
lpmmi->ptMinTrackSize = w->m_minsz;
}
} break;
default:
return DefWindowProc(hwnd, msg, wp, lp);
}
return 0;
});
RegisterClassEx(&wc);
m_window = CreateWindow("webview", "", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,
CW_USEDEFAULT, width, height, nullptr, nullptr,
GetModuleHandle(nullptr), nullptr);
SetWindowLongPtr(m_window, GWLP_USERDATA, (LONG_PTR)this);
int scrWidth = GetSystemMetrics(SM_CXSCREEN);
int scrHeight = GetSystemMetrics(SM_CYSCREEN);
RECT rect;
rect.right = width;// 设置窗口宽高
rect.bottom = height;
rect.left = (scrWidth - rect.right) / 2;
rect.top = (scrHeight - rect.bottom) / 2;
AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, 0);
MoveWindow(m_window,rect.left, rect.top, rect.right, rect.bottom, 1);// 居中
//SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_UNAWARE);
ShowWindow(m_window, SW_SHOW);
UpdateWindow(m_window);
SetFocus(m_window);
auto cb = std::bind(&win32_edge_engine::on_message, this, std::placeholders::_1);
bool flag = m_browser->embed(m_window, debug, cb);
if(!flag){
MessageBox(NULL,TEXT("can't load webview2,please install webveiw2 runtime"),TEXT("Alert"),MB_OK);
exit(0);
}
m_browser->resize(m_window);
}
void run() {
MSG msg;
BOOL res;
while ((res = GetMessage(&msg, nullptr, 0, 0)) != -1) {
if (msg.hwnd) {
TranslateMessage(&msg);
DispatchMessage(&msg);
continue;
}
if (msg.message == WM_APP) {
auto f = (dispatch_fn_t *)(msg.lParam);
(*f)();
delete f;
} else if (msg.message == WM_QUIT) {
return;
}
}
}
void *window() { return (void *)m_window; }
void hide() {
ShowWindow(m_window, SW_HIDE);
}
void show() {
ShowWindow(m_window, SW_SHOW);
}
void terminate() { PostQuitMessage(0); }
void dispatch(dispatch_fn_t f) {
PostThreadMessage(m_main_thread, WM_APP, 0, (LPARAM) new dispatch_fn_t(f));
}
void set_icon(const std::string icon) {
}
void set_title(const std::string title) {
SetWindowText(m_window, title.c_str());
}
void set_size(int width, int height, int hints) {
auto style = GetWindowLong(m_window, GWL_STYLE);
if (hints == WEBVIEW_HINT_FIXED) {
style &= ~(WS_THICKFRAME | WS_MAXIMIZEBOX);
} else {
style |= (WS_THICKFRAME | WS_MAXIMIZEBOX);
}
SetWindowLong(m_window, GWL_STYLE, style);
if (hints == WEBVIEW_HINT_MAX) {
m_maxsz.x = width;
m_maxsz.y = height;
} else if (hints == WEBVIEW_HINT_MIN) {
m_minsz.x = width;
m_minsz.y = height;
} else {
int scrWidth = GetSystemMetrics(SM_CXSCREEN);
int scrHeight = GetSystemMetrics(SM_CYSCREEN);
RECT rect;
rect.right = width;// 设置窗口宽高
rect.bottom = height;
rect.left = (scrWidth - rect.right) / 2;
rect.top = (scrHeight - rect.bottom) / 2;
AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, 0);
MoveWindow(m_window,rect.left, rect.top, rect.right, rect.bottom, 1);// 居中
m_browser->resize(m_window);
}
}
void navigate(const std::string url) { m_browser->navigate(url); }
void eval(const std::string js) { m_browser->eval(js); }
void init(const std::string js) { m_browser->init(js); }
private:
virtual void on_message(const std::string msg) = 0;
bool m_hide;
HWND m_window;
POINT m_minsz = POINT{0, 0};
POINT m_maxsz = POINT{0, 0};
DWORD m_main_thread = GetCurrentThreadId();
std::unique_ptr<webview::browser> m_browser = std::unique_ptr<webview::edge_chromium>(new webview::edge_chromium());
};
using browser_engine = win32_edge_engine;
} // namespace webview