Skip to content

Commit 00d7066

Browse files
Update webview-c
1 parent 82f5734 commit 00d7066

File tree

6 files changed

+61
-150
lines changed

6 files changed

+61
-150
lines changed

rock.mk

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ CFLAGS_windows = -Wall \
3333
-Wno-unused-parameter \
3434
-Wstrict-prototypes \
3535
-I$(WEBVIEW_C) \
36+
-I$(MS_WEBVIEW2)/include \
3637
-I$(LUA_INCDIR) \
3738
-DWEBVIEW_WINAPI=1
3839

@@ -69,15 +70,15 @@ SOURCES = webview.c
6970

7071
OBJS = webview.o
7172

72-
lib: $(TARGET) WebView2Win32-$(PLAT)
73+
lib: $(TARGET)
7374

7475
install: install-$(PLAT)
7576
cp $(TARGET) $(INST_LIBDIR)
7677

7778
install-linux:
7879

7980
install-windows:
80-
cp WebView2Win32.dll $(MS_WEBVIEW2)/$(WEBVIEW_ARCH)/WebView2Loader.dll $(INST_BINDIR)
81+
cp $(MS_WEBVIEW2)/$(WEBVIEW_ARCH)/WebView2Loader.dll $(INST_BINDIR)
8182

8283
show:
8384
@echo PLAT: $(PLAT)
@@ -100,17 +101,6 @@ show-install:
100101
$(TARGET): $(OBJS)
101102
$(CC) $(OBJS) $(LIBFLAG) $(LIBFLAG_$(PLAT)) -o $(TARGET)
102103

103-
WebView2Win32-linux:
104-
105-
WebView2Win32-windows:
106-
$(CC) $(WEBVIEW_C)/WebView2Win32.c \
107-
-shared \
108-
-static-libgcc \
109-
-Wl,-s \
110-
-I$(WEBVIEW_C) -I$(MS_WEBVIEW2)/include \
111-
-L$(MS_WEBVIEW2)/$(WEBVIEW_ARCH) -lWebView2Loader \
112-
-o WebView2Win32.dll
113-
114104
clean:
115105
-$(RM) $(OBJS) $(TARGET)
116106

webview-c/README.md

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,22 +36,15 @@ gcc webview-example.c -DWEBVIEW_GTK=1 `pkg-config --cflags --libs gtk+-3.0 webki
3636
gcc webview-example.c -DWEBVIEW_COCOA=1 -framework WebKit -o webview-example
3737

3838
# Windows (mingw)
39-
gcc webview-example.c -DWEBVIEW_WINAPI=1 -lole32 -lcomctl32 -loleaut32 -luuid -lgdi32 -o webview-example.exe
39+
gcc webview-example.c -DWEBVIEW_WINAPI=1 -Ims.webview2.0.8.355/include -lole32 -lcomctl32 -loleaut32 -luuid -lgdi32 -o webview-example.exe
4040
```
4141

4242
## Edge
4343

44-
The Edge (Chromium) implementation requires the extra library `WebView2Loader.dll`
45-
part of the [Microsoft Edge WebView2](https://docs.microsoft.com/en-gb/microsoft-edge/hosting/webview2) SDK,
46-
this library is linked to the `WebView2Win32.dll` library which can be loaded and used by webview.
47-
48-
You can build it using the following command line.
49-
50-
```bash
51-
gcc WebView2Win32.c -shared -static-libgcc -Wl,-s -I. -Ims.webview2.0.8.355/include -Lms.webview2.0.8.355/x64 -lWebView2Loader -o WebView2Win32.dll
52-
```
44+
Microsoft Edge (Chromium) shall be installed with the same architecture, x64 or x86.
5345

54-
Microsoft Edge (Chromium) shall be installed otherwise MSHTML will be used. This implementation will create a folder for user data.
46+
The Edge (Chromium) implementation requires the extra library `WebView2Loader.dll`
47+
part of the [Microsoft Edge WebView2](https://docs.microsoft.com/en-gb/microsoft-edge/hosting/webview2) SDK.
48+
The environment variable `WEBVIEW2_WIN32_PATH` can be used to pass the folder containing the extra library.
5549

56-
The environment variable `WEBVIEW2_WIN32_PATH` can be used to pass the folder containing the extra libraries
57-
in order to enable, or disable, the Edge implementation.
50+
Note that this implementation creates a folder for the user data.

webview-c/WebView2Win32.h

Lines changed: 0 additions & 29 deletions
This file was deleted.

webview-c/WebView2Win32.c renamed to webview-c/webview-win32-edge.c

Lines changed: 35 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,14 @@
1-
#include "WebView2Win32.h"
2-
31
#include "webview2.h"
42

53
#include <stddef.h>
6-
#include <stdio.h>
4+
//#include <stdio.h>
75

86
#define WEBVIEW2_WIN32_API extern
97

10-
static inline WCHAR *webview_to_utf16(const char *s) {
11-
DWORD size = MultiByteToWideChar(CP_UTF8, 0, s, -1, 0, 0);
12-
WCHAR *ws = (WCHAR *)GlobalAlloc(GMEM_FIXED, sizeof(WCHAR) * size);
13-
if (ws != NULL) {
14-
MultiByteToWideChar(CP_UTF8, 0, s, -1, ws, size);
15-
}
16-
return ws;
17-
}
18-
19-
static inline char *webview_from_utf16(WCHAR *ws) {
20-
int n = WideCharToMultiByte(CP_UTF8, 0, ws, -1, NULL, 0, NULL, NULL);
21-
char *s = (char *)GlobalAlloc(GMEM_FIXED, n);
22-
if (s != NULL) {
23-
WideCharToMultiByte(CP_UTF8, 0, ws, -1, s, n, NULL, NULL);
24-
}
25-
return s;
26-
}
8+
typedef void (*WebView2CallbackFn) (webview2 *wv, const char *message, void *context);
279

10+
typedef HRESULT (*CreateWebView2EnvironmentWithDetailsFnType) (PCWSTR browserExecutableFolder, PCWSTR userDataFolder, PCWSTR additionalBrowserArguments, IWebView2CreateWebView2EnvironmentCompletedHandler* environment_created_handler);
11+
typedef HRESULT (*GetWebView2BrowserVersionInfoFnType) (PCWSTR browserExecutableFolder, LPWSTR* versionInfo);
2812

2913
typedef struct webview2_struct {
3014
HWND hwnd;
@@ -43,6 +27,7 @@ typedef struct webview2_struct {
4327
#define WEBVIEW2_PTR_FROM(_cp, _field) \
4428
((webview2 *) ((char *) (_cp) - offsetof(webview2, _field)))
4529

30+
static CreateWebView2EnvironmentWithDetailsFnType CreateWebView2EnvironmentWithDetailsFn = NULL;
4631

4732
static HRESULT WebView2WebMessageReceivedEventHandleInvoke(IWebView2WebMessageReceivedEventHandler * This, IWebView2WebView *webView, IWebView2WebMessageReceivedEventArgs *args) {
4833
LPWSTR webMessage;
@@ -84,7 +69,6 @@ static HRESULT CreateWebView2_Invoke(IWebView2CreateWebViewCompletedHandler * Th
8469
return S_OK;
8570
}
8671

87-
8872
static HRESULT CreateWebView2Environment_Invoke(IWebView2CreateWebView2EnvironmentCompletedHandler * This, HRESULT result, IWebView2Environment * webViewEnvironment) {
8973
webview2 *pwv2 = WEBVIEW2_PTR_FROM(This, env_created_handler);
9074
webViewEnvironment->lpVtbl->CreateWebView(webViewEnvironment, pwv2->hwnd, &pwv2->webview_created_handler);
@@ -101,7 +85,7 @@ static ULONG NoOpRelease(void * This) {
10185
return 1;
10286
}
10387

104-
static void init_webview2(webview2 *pwv2) {
88+
static void InitWebView2(webview2 *pwv2) {
10589
pwv2->env_created_handler_vtbl.QueryInterface = (HRESULT (*)(IWebView2CreateWebView2EnvironmentCompletedHandler * This, REFIID riid, void **ppvObject)) &NoOpQueryInterface;
10690
pwv2->env_created_handler_vtbl.AddRef = (ULONG (*)(IWebView2CreateWebView2EnvironmentCompletedHandler * This)) &NoOpAddRef;
10791
pwv2->env_created_handler_vtbl.Release = (ULONG (*)(IWebView2CreateWebView2EnvironmentCompletedHandler * This)) &NoOpRelease;
@@ -122,11 +106,11 @@ static void init_webview2(webview2 *pwv2) {
122106
WEBVIEW2_WIN32_API webview2 * CreateWebView2(HWND hwnd, const char *url) {
123107
webview2 *pwv2 = (webview2 *)GlobalAlloc(GMEM_FIXED, sizeof(webview2));
124108
if (pwv2 != NULL) {
125-
init_webview2(pwv2);
109+
InitWebView2(pwv2);
126110
pwv2->webview = NULL;
127111
pwv2->hwnd = hwnd;
128112
pwv2->url = url;
129-
HRESULT res = CreateWebView2EnvironmentWithDetails(NULL, NULL, NULL, &pwv2->env_created_handler);
113+
HRESULT res = CreateWebView2EnvironmentWithDetailsFn(NULL, NULL, NULL, &pwv2->env_created_handler);
130114
if (res != S_OK) {
131115
GlobalFree(pwv2);
132116
pwv2 = NULL;
@@ -182,27 +166,33 @@ WEBVIEW2_WIN32_API int WebView2Eval(webview2 *pwv2, const char *js) {
182166
return 1;
183167
}
184168

185-
static int WebView2Check() {
186-
LPWSTR versionInfo = NULL;
187-
HRESULT res = GetWebView2BrowserVersionInfo(NULL, &versionInfo);
188-
return (res == S_OK) && (versionInfo != NULL);
189-
}
190-
191-
static webview2_win32 * webview2_win32_prt = NULL;
192-
193-
static webview2_win32 webview2_win32_ref;
194-
195-
WEBVIEW2_WIN32_API webview2_win32 * GetWebView2Win32(void) {
196-
if (webview2_win32_prt == NULL) {
197-
webview2_win32_prt = &webview2_win32_ref;
198-
webview2_win32_prt->create = &CreateWebView2;
199-
webview2_win32_prt->release = &ReleaseWebView2;
200-
webview2_win32_prt->eval = &WebView2Eval;
201-
webview2_win32_prt->setBounds = &WebView2SetBounds;
202-
webview2_win32_prt->registerCallback = &WebView2RegisterCallback;
169+
static int WebView2Enable() {
170+
TCHAR modulePath[MAX_PATH + 22];
171+
char * webView2Win32Path = getenv("WEBVIEW2_WIN32_PATH");
172+
if ((webView2Win32Path != NULL) && (strlen(webView2Win32Path) > MAX_PATH)) {
173+
webView2Win32Path = NULL;
203174
}
204-
if (WebView2Check()) {
205-
return webview2_win32_prt;
175+
if (webView2Win32Path == NULL) {
176+
strcpy(modulePath, "WebView2Loader.dll");
177+
} else {
178+
sprintf(modulePath, "%s\\WebView2Loader.dll", webView2Win32Path);
179+
}
180+
HMODULE hWebView2LoaderModule = LoadLibraryA(modulePath);
181+
if (hWebView2LoaderModule != NULL) {
182+
GetWebView2BrowserVersionInfoFnType GetWebView2BrowserVersionInfoFn = (GetWebView2BrowserVersionInfoFnType)GetProcAddress(hWebView2LoaderModule, "GetWebView2BrowserVersionInfo");
183+
CreateWebView2EnvironmentWithDetailsFn = (CreateWebView2EnvironmentWithDetailsFnType)GetProcAddress(hWebView2LoaderModule, "CreateWebView2EnvironmentWithDetails");
184+
if ((CreateWebView2EnvironmentWithDetailsFn != NULL) && (GetWebView2BrowserVersionInfoFn != NULL)) {
185+
LPWSTR versionInfo = NULL;
186+
HRESULT res = GetWebView2BrowserVersionInfoFn(NULL, &versionInfo);
187+
if ((res == S_OK) && (versionInfo != NULL)) {
188+
webview_print_log("WebView2 enabled");
189+
return 1;
190+
}
191+
}
192+
} else {
193+
webview_print_log(modulePath);
206194
}
207-
return NULL;
195+
CreateWebView2EnvironmentWithDetailsFn = NULL;
196+
webview_print_log("WebView2Loader not available");
197+
return 0;
208198
}

webview-c/webview-win32.c

Lines changed: 16 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -733,7 +733,9 @@ static int DisplayHTMLPage(struct webview *w) {
733733
return (-5);
734734
}
735735

736-
static webview2_win32 * WebView2Win32 = NULL;
736+
#include "webview-win32-edge.c"
737+
738+
static int webview_webview2_enabled = 0;
737739

738740
static void WebView2Callback(webview2 *wv, const char *message, void *context) {
739741
struct webview *w = (struct webview *)context;
@@ -752,15 +754,15 @@ static LRESULT CALLBACK wndproc(HWND hwnd, UINT uMsg, WPARAM wParam,
752754
case WM_CREATE:
753755
w = (struct webview *)((CREATESTRUCT *)lParam)->lpCreateParams;
754756
w->priv.hwnd = hwnd;
755-
if (WebView2Win32 != NULL) {
756-
w->priv.webview2 = WebView2Win32->create(hwnd, w->url);
757-
WebView2Win32->registerCallback(w->priv.webview2, &WebView2Callback, w);
757+
if (webview_webview2_enabled) {
758+
w->priv.webview2 = CreateWebView2(hwnd, w->url);
759+
WebView2RegisterCallback(w->priv.webview2, &WebView2Callback, w);
758760
return TRUE;
759761
}
760762
return EmbedBrowserObject(w);
761763
case WM_DESTROY:
762-
if (WebView2Win32 != NULL) {
763-
WebView2Win32->release(w->priv.webview2);
764+
if (webview_webview2_enabled) {
765+
ReleaseWebView2(w->priv.webview2);
764766
w->priv.webview2 = NULL;
765767
} else {
766768
UnEmbedBrowserObject(w);
@@ -769,8 +771,8 @@ static LRESULT CALLBACK wndproc(HWND hwnd, UINT uMsg, WPARAM wParam,
769771
return TRUE;
770772
case WM_SIZE:
771773
GetClientRect(hwnd, &rect);
772-
if (WebView2Win32 != NULL) {
773-
WebView2Win32->setBounds(w->priv.webview2, rect);
774+
if (webview_webview2_enabled) {
775+
WebView2SetBounds(w->priv.webview2, rect);
774776
} else {
775777
IWebBrowser2 *webBrowser2;
776778
IOleObject *browser = *w->priv.browser;
@@ -819,50 +821,15 @@ static int webview_fix_ie_compat_mode() {
819821
return 0;
820822
}
821823

822-
static int webview_load_webview2() {
823-
TCHAR modulePath[MAX_PATH + 22];
824-
char * webView2Win32Path = getenv("WEBVIEW2_WIN32_PATH");
825-
if ((webView2Win32Path != NULL) && (strlen(webView2Win32Path) > MAX_PATH)) {
826-
webView2Win32Path = NULL;
827-
}
828-
if (webView2Win32Path == NULL) {
829-
strcpy(modulePath, "WebView2Loader.dll");
830-
} else {
831-
sprintf(modulePath, "%s\\WebView2Loader.dll", webView2Win32Path);
832-
}
833-
HMODULE hWebView2LoaderModule = LoadLibraryA(modulePath);
834-
if (hWebView2LoaderModule != NULL) {
835-
if (webView2Win32Path == NULL) {
836-
strcpy(modulePath, "WebView2Win32.dll");
837-
} else {
838-
sprintf(modulePath, "%s\\WebView2Win32.dll", webView2Win32Path);
839-
}
840-
HMODULE hWebView2Win32Module = LoadLibraryA(modulePath);
841-
if (hWebView2Win32Module != NULL) {
842-
GetWebView2Win32Fn GetWebView2Win32 = (GetWebView2Win32Fn)GetProcAddress(hWebView2Win32Module, "GetWebView2Win32");
843-
WebView2Win32 = GetWebView2Win32();
844-
}
845-
} else {
846-
webview_print_log(modulePath);
847-
}
848-
if (WebView2Win32 != NULL) {
849-
webview_print_log("WebView2Win32 activated");
850-
} else {
851-
webview_print_log("WebView2Win32 not found");
852-
}
853-
return 0;
854-
}
855-
856824
WEBVIEW_API int webview_init(struct webview *w) {
857825
WNDCLASSEX wc;
858826
HINSTANCE hInstance;
859827
DWORD style;
860828
RECT clientRect;
861829
RECT rect;
862830

863-
if (webview_load_webview2() < 0) {
864-
return -1;
865-
}
831+
webview_webview2_enabled = WebView2Enable();
832+
866833
if (webview_fix_ie_compat_mode() < 0) {
867834
return -1;
868835
}
@@ -911,7 +878,7 @@ WEBVIEW_API int webview_init(struct webview *w) {
911878

912879
SetWindowLongPtr(w->priv.hwnd, GWLP_USERDATA, (LONG_PTR)w);
913880

914-
if (WebView2Win32 == NULL) {
881+
if (!webview_webview2_enabled) {
915882
DisplayHTMLPage(w);
916883
}
917884

@@ -936,7 +903,7 @@ WEBVIEW_API int webview_loop(struct webview *w, int blocking) {
936903
case WM_COMMAND:
937904
case WM_KEYDOWN:
938905
case WM_KEYUP:
939-
if (WebView2Win32 == NULL) {
906+
if (!webview_webview2_enabled) {
940907
HRESULT r = S_OK;
941908
IWebBrowser2 *webBrowser2;
942909
IOleObject *browser = *w->priv.browser;
@@ -963,8 +930,8 @@ WEBVIEW_API int webview_loop(struct webview *w, int blocking) {
963930
}
964931

965932
WEBVIEW_API int webview_eval(struct webview *w, const char *js) {
966-
if (WebView2Win32 != NULL) {
967-
return WebView2Win32->eval(w->priv.webview2, js);
933+
if (webview_webview2_enabled) {
934+
return WebView2Eval(w->priv.webview2, js);
968935
}
969936
IWebBrowser2 *webBrowser2;
970937
IHTMLDocument2 *htmlDoc2;

webview-c/webview.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ struct webview_priv {
6565

6666
#include <stdio.h>
6767

68-
#include "WebView2Win32.h"
68+
typedef struct webview2_struct webview2;
6969

7070
struct webview_priv {
7171
HWND hwnd;

0 commit comments

Comments
 (0)