From 82fa3d8f33f1588c277347897c57c9d2e5772602 Mon Sep 17 00:00:00 2001 From: "JK,Kim" Date: Mon, 3 Jun 2024 14:07:21 +0900 Subject: [PATCH] Added support for the `webRequestedEventArgs.resourceContext` property. - Added support for the `webRequestedEventArgs.resourceContext` property. - Changed `wv2env_t` to `wv2environment_t` --- example/example_win32/example_win32.c | 15 ++++- example/example_win32/example_win32.vcxproj | 4 ++ include/wv2.h | 75 ++++++++++++++------- src/cwv2.cpp | 2 +- src/cwv2.h | 2 +- src/cwv2env.cpp | 15 +++++ src/cwv2env.h | 5 +- src/cwv2types.h | 13 +++- src/cwv2webResourceResponse.cpp | 35 ++++++++++ src/cwv2webResourceResponse.h | 20 ++++++ src/eventHandler/eventHandler.h | 39 ++++++++--- src/wv2.cpp | 28 ++++++-- wv2.rc | 8 +-- wv2.vcxproj | 2 + wv2.vcxproj.filters | 6 ++ 15 files changed, 218 insertions(+), 51 deletions(-) create mode 100644 src/cwv2webResourceResponse.cpp create mode 100644 src/cwv2webResourceResponse.h diff --git a/example/example_win32/example_win32.c b/example/example_win32/example_win32.c index cf72625..54f93fd 100644 --- a/example/example_win32/example_win32.c +++ b/example/example_win32/example_win32.c @@ -150,7 +150,7 @@ BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) // set browserProcessExited event handler wv2env_t env = wv2getEnv(webview); if(env) { - wv2envSetBrowserProcessExitedHandler(env, OnBrowserProcessExited); + wv2environment_setBrowserProcessExitedHandler(env, OnBrowserProcessExited); } // set isMutedChanged event handler @@ -456,6 +456,19 @@ void OnWebResourceRequested(wv2_t sender, wv2webResourceRequestedEventArgs_t arg } } + wv2webResourceResponse_t response = wv2webResourceRequestedEventArgs_response(args); + if (response) { + int32_t statusCode = wv2webResourceResponse_statusCode(response); + LPWSTR reasonPhrase = wv2webResourceResponse_reasonPhrase(response); + WCHAR buf[2048]; + wsprintf(buf, L"Status Code: %d, Reason Phrase: %s", statusCode, reasonPhrase); + + wv2freeMemory((void*)reasonPhrase); + + MessageBox(NULL, buf, L"webResourceRequested", MB_OK | MB_ICONINFORMATION); + } + + wv2removeWebResourceRequestedFilter(webview, testFilterUri, wv2webResourceContext_all); } diff --git a/example/example_win32/example_win32.vcxproj b/example/example_win32/example_win32.vcxproj index df0fdac..f7437d0 100644 --- a/example/example_win32/example_win32.vcxproj +++ b/example/example_win32/example_win32.vcxproj @@ -93,6 +93,7 @@ WIN32;_DEBUG;_WINDOWS;%(PreprocessorDefinitions) true ..\..\include\ + CompileAsC Windows @@ -109,6 +110,7 @@ WIN32;NDEBUG;_WINDOWS;%(PreprocessorDefinitions) true ..\..\include\ + CompileAsC Windows @@ -125,6 +127,7 @@ _DEBUG;_WINDOWS;%(PreprocessorDefinitions) true ..\..\include\ + CompileAsC Windows @@ -141,6 +144,7 @@ NDEBUG;_WINDOWS;%(PreprocessorDefinitions) true ..\..\include\ + CompileAsC Windows diff --git a/include/wv2.h b/include/wv2.h index 01a16f3..e15e764 100644 --- a/include/wv2.h +++ b/include/wv2.h @@ -49,16 +49,21 @@ ## 0.11.0(26) 2024-05-28 - Added support for the `webResourceRequest.headers` property. + +## 0.12(27) 2024-06-03 +- Added support for the `webRequestedEventArgs.resourceContext` property. +- Changed `wv2env_t` to `wv2environment_t` */ #ifndef WEBVIEW2_C_WRAPPER_H_ #define WEBVIEW2_C_WRAPPER_H_ -#define WV2_VERSION "0.11.0" -#define WV2_VERSION_NUM 26 +#define WV2_VERSION "0.12" +#define WV2_VERSION_NUM 27 #include #include #include +#include #include "wv2envOpts.h" #if defined(_MSC_VER) && _MSC_VER < 1900 // Visual Studio 2013 @@ -93,7 +98,9 @@ typedef enum wv2HostResourceAccessKind { #endif // __wv2HostResourceAccessKind__DEFINED__ typedef void* wv2_t; -typedef void* wv2env_t; // CoreWebView2Environment +typedef void* wv2environment_t; // CoreWebView2Environment +typedef wv2environment_t wv2env_t; + // Structure representing the result and support status of a function typedef struct wv2bool { @@ -103,7 +110,6 @@ typedef struct wv2bool { HRESULT hr; } wv2bool; -/////////////////////////////////////////////////////////////////////////////// typedef void* wv2deferral_t; // ICoreWebView2Deferral WV2_API HRESULT wv2deferral_complete(wv2deferral_t d); @@ -189,6 +195,7 @@ typedef enum wv2scriptDialogKind { // @see COREWEBVIEW2_WEB_RESOURCE_CONTEXT typedef enum wv2webResourceContext { + wv2webResourceContext_undefined = -1, wv2webResourceContext_all = 0, wv2webResourceContext_document = (wv2webResourceContext_all + 1), wv2webResourceContext_stylesheet = (wv2webResourceContext_document + 1), @@ -402,7 +409,7 @@ WV2_API int32_t wv2webResourceResponse_statusCode(wv2webResourceResponse_t handle); WV2_API HRESULT -wv2webResourceResponse_setStatusCode(wv2webResourceResponse_t handle, int statusCode); +wv2webResourceResponse_setStatusCode(wv2webResourceResponse_t handle, int32_t statusCode); WV2_API LPWSTR wv2webResourceResponse_reasonPhrase(wv2webResourceResponse_t handle); @@ -415,12 +422,14 @@ typedef void* wv2webResourceRequestedEventArgs_t; // ICoreWebView2WebResourceReq WV2_API wv2webResourceRequest_t wv2webResourceRequestedEventArgs_request(wv2webResourceRequestedEventArgs_t args); -//WV2_API wv2webResourceResponse_t -//wv2webResourceRequestedEventArgs_response(wv2webResourceRequestedEventArgs_t args); +WV2_API wv2webResourceResponse_t +wv2webResourceRequestedEventArgs_response(wv2webResourceRequestedEventArgs_t args); +WV2_API wv2webResourceContext +wv2webResourceRequestedEventArgs_resourceContext(wv2webResourceRequestedEventArgs_t args); // HRESULT put_Response(ICoreWebView2WebResourceResponse* response) // HRESULT GetDeferral(ICoreWebView2Deferral** deferral) -// HRESULT get_ResourceContext(COREWEBVIEW2_WEB_RESOURCE_CONTEXT* context) + /////////////////////////////////////////////////////////////////////////////// typedef void @@ -450,7 +459,7 @@ typedef void typedef isMutedChanged isDocumentPlayingAudioChanged; typedef void -(*browserProcessExited)(wv2env_t sender, wv2browserProcessExitedEventArgs* e); +(*browserProcessExited)(wv2environment_t sender, wv2browserProcessExitedEventArgs* e); typedef void (*newWindowRequested)(wv2_t sender, wv2newWindowRequestedEventArgs_t args); @@ -490,14 +499,9 @@ WV2_API wv2_t wv2createSync(LPCWSTR browserExecutableFolder, LPCWSTR userDataFol WV2_API wv2_t wv2createSync2(LPCWSTR browserExecutableFolder, LPCWSTR userDataFolder, wv2envOpts_t environmentOptions, HWND parentWindow); -WV2_API wv2env_t wv2getEnv(wv2_t w); - -/* -@brief Set an event handler for the browserProcessExited event. -@remark Minimum WebView2 SDK version required: 1.0.992.28 -*/ -WV2_API wv2bool wv2envSetBrowserProcessExitedHandler(wv2env_t e, - browserProcessExited handler); +DEPRECATED("wv2getEnv deprecated. Use wv2environment instead.") +WV2_API wv2environment_t wv2getEnv(wv2_t w); +WV2_API wv2environment_t wv2getEnvironment(wv2_t w); WV2_API void wv2destroy(wv2_t* w); @@ -739,18 +743,26 @@ WV2_API HRESULT wv2removeWebResourceRequestedFilter(wv2_t w, WV2_API HRESULT wv2lastError(wv2_t w); +//////////////////////////////////////////////////////////////////////////////// + +/* +@brief Set an event handler for the browserProcessExited event. +@remark Minimum WebView2 SDK version required: 1.0.992.28 +*/ +WV2_API wv2bool wv2environment_setBrowserProcessExitedHandler(wv2environment_t env, + browserProcessExited handler); + +WV2_API wv2webResourceResponse_t +wv2environment_createWebResourceResponse(wv2environment_t env, IStream* content, + int32_t statusCode, LPCWSTR reasonPhrase, LPCWSTR headers); + +/////////////////////////////////////////////////////////////////////////////// #ifdef __cplusplus } #endif #ifdef __cplusplus -struct wv2env { - virtual ~wv2env() {}; - - virtual wv2bool setBrowserProcessExitedHandler(browserProcessExited handler) = 0; -}; - struct wv2deferral { virtual ~wv2deferral() {}; @@ -854,16 +866,27 @@ struct wv2webResourceRequest { struct wv2webResourceResponse { virtual int32_t statusCode() = 0; - virtual HRESULT setStatusCode(int statusCode) = 0; + virtual HRESULT setStatusCode(int32_t statusCode) = 0; virtual LPWSTR reasonPhrase() = 0; virtual HRESULT setReasonPhrase(LPCWSTR reasonPhrase) = 0; }; struct wv2webResourceRequestedEventArgs { virtual wv2webResourceRequest* request() = 0; - //virtual wv2webResourceResponse* response() = 0; + virtual wv2webResourceResponse* response() = 0; + virtual wv2webResourceContext resourceContext() = 0; }; +struct wv2environment { + virtual ~wv2environment() {}; + + virtual wv2bool setBrowserProcessExitedHandler(browserProcessExited handler) = 0; + + virtual wv2webResourceResponse* createWebResourceResponse( + IStream* content, int32_t statusCode, LPCWSTR reasonPhrase, LPCWSTR headers) = 0; +}; + + struct wv2 { virtual ~wv2(){}; @@ -916,7 +939,7 @@ struct wv2 { virtual wv2bool isDocumentPlayingAudio() = 0; virtual wv2bool openTaskManagerWindow() = 0; - virtual wv2env* getEnvironment() = 0; + virtual wv2environment* getEnvironment() = 0; virtual wv2bool setNewWindowRequestedHandler(newWindowRequested handler) = 0; virtual wv2bool setDocumentTitleChangedHandler(documentTitleChanged handler) = 0; diff --git a/src/cwv2.cpp b/src/cwv2.cpp index b81309e..eb6a9b7 100644 --- a/src/cwv2.cpp +++ b/src/cwv2.cpp @@ -712,7 +712,7 @@ EventRegistrationToken emptyEventRegistrationToken() { return token; } -wv2env* cwv2::getEnvironment() { +wv2environment* cwv2::getEnvironment() { return &env_; } diff --git a/src/cwv2.h b/src/cwv2.h index 532da23..e05b229 100644 --- a/src/cwv2.h +++ b/src/cwv2.h @@ -115,7 +115,7 @@ class cwv2 : wv2bool isDocumentPlayingAudio() OVERRIDE; wv2bool openTaskManagerWindow() OVERRIDE; - wv2env* getEnvironment() OVERRIDE; + wv2environment* getEnvironment() OVERRIDE; wv2bool setNewWindowRequestedHandler(newWindowRequested handler) OVERRIDE; wv2bool setDocumentTitleChangedHandler(documentTitleChanged handler) OVERRIDE; LPCWSTR documentTitle() OVERRIDE; diff --git a/src/cwv2env.cpp b/src/cwv2env.cpp index 7ed0585..2f3e069 100644 --- a/src/cwv2env.cpp +++ b/src/cwv2env.cpp @@ -1,11 +1,14 @@ #include "cwv2env.h" #include "cwv2types.h" +#include "cwv2webResourceResponse.h" +/* #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif +*/ void cwv2env::Release() { @@ -50,3 +53,15 @@ wv2bool cwv2env::setBrowserProcessExitedHandler(browserProcessExited handler) { r.value = true; return r; } + +wv2webResourceResponse* cwv2env::createWebResourceResponse( + IStream* content, int32_t statusCode, LPCWSTR reasonPhrase, LPCWSTR headers) { + if (!env2_) { + return nullptr; + } + + CComPtr response; + const HRESULT hr = env2_->CreateWebResourceResponse(content, statusCode, reasonPhrase, headers, &response); + if (FAILED(hr)) return nullptr; + return new cwv2webResourceResponse(response); +} \ No newline at end of file diff --git a/src/cwv2env.h b/src/cwv2env.h index b5d949c..377dd96 100644 --- a/src/cwv2env.h +++ b/src/cwv2env.h @@ -5,12 +5,15 @@ #include "eventHandler.h" class cwv2env: - public wv2env { + public wv2environment { public: wv2bool setBrowserProcessExitedHandler(browserProcessExited handler) override; HRESULT createCoreWebView2EnvironmentCompleted(ICoreWebView2Environment* env); + wv2webResourceResponse* createWebResourceResponse( + IStream* content, int32_t statusCode, LPCWSTR reasonPhrase, LPCWSTR headers) override; + void Release(); ICoreWebView2Environment2* getEnv2() { return env2_; } diff --git a/src/cwv2types.h b/src/cwv2types.h index a0c68e3..abdda61 100644 --- a/src/cwv2types.h +++ b/src/cwv2types.h @@ -17,4 +17,15 @@ static inline wv2bool wv2boolDefault() { wv2bool r = {0,}; r.hr = S_OK; return r; -} \ No newline at end of file +} + +template +LPWSTR getStrVal(Func func) { + LPWSTR result = nullptr; + LPWSTR value = nullptr; + if (SUCCEEDED(func(&value))) { + result = _wcsdup(value); + CoTaskMemFree((void*)value); + } + return result; +} diff --git a/src/cwv2webResourceResponse.cpp b/src/cwv2webResourceResponse.cpp new file mode 100644 index 0000000..c15fcaa --- /dev/null +++ b/src/cwv2webResourceResponse.cpp @@ -0,0 +1,35 @@ +#include "cwv2webResourceResponse.h" +#include "cwv2types.h" + +cwv2webResourceResponse::cwv2webResourceResponse() { +} +cwv2webResourceResponse::cwv2webResourceResponse(CComPtr response) { + setResponse(response); +} + +void cwv2webResourceResponse::setResponse(CComPtr response) { + response_ = response; +} + +int32_t cwv2webResourceResponse::statusCode() { + if (!response_) return 0; + int32_t statusCode = 0; + response_->get_StatusCode(&statusCode); + return statusCode; +} + +HRESULT cwv2webResourceResponse::setStatusCode(int32_t statusCode) { + if (!response_) return E_NOINTERFACE; + return response_->put_StatusCode(statusCode); +} +LPWSTR cwv2webResourceResponse::reasonPhrase() { + if (!response_) return nullptr; + return getStrVal([&](LPWSTR* value)->HRESULT { + return response_->get_ReasonPhrase(value); + }); +} + +HRESULT cwv2webResourceResponse::setReasonPhrase(LPCWSTR reasonPhrase) { + if (!response_) return E_NOINTERFACE; + return response_->put_ReasonPhrase(reasonPhrase); +} \ No newline at end of file diff --git a/src/cwv2webResourceResponse.h b/src/cwv2webResourceResponse.h new file mode 100644 index 0000000..a227579 --- /dev/null +++ b/src/cwv2webResourceResponse.h @@ -0,0 +1,20 @@ +#pragma once +#include "WebView2.h" +#include "wv2.h" +#include + + +class cwv2webResourceResponse : public wv2webResourceResponse { +public: + cwv2webResourceResponse(); + cwv2webResourceResponse(CComPtr response); + + void setResponse(CComPtr response); + int32_t statusCode() override; + HRESULT setStatusCode(int32_t statusCode) override; + LPWSTR reasonPhrase() override; + HRESULT setReasonPhrase(LPCWSTR reasonPhrase) override; + +private: + CComPtr response_; +}; \ No newline at end of file diff --git a/src/eventHandler/eventHandler.h b/src/eventHandler/eventHandler.h index 33b4c94..63c4c9e 100644 --- a/src/eventHandler/eventHandler.h +++ b/src/eventHandler/eventHandler.h @@ -3,17 +3,8 @@ #include "wv2.h" #include "WebView2.h" #include "cwv2deferral.h" - -template -LPWSTR getStrVal(Func func) { - LPWSTR result = nullptr; - LPWSTR value = nullptr; - if (SUCCEEDED(func(&value))) { - result = _wcsdup(value); - CoTaskMemFree((void*)value); - } - return result; -} +#include "cwv2types.h" +#include "cwv2webResourceResponse.h" template class EventHandlerBase: public I { @@ -484,6 +475,7 @@ class cwv2webResourceRequest : public wv2webResourceRequest { cwv2httpRequestHeaders headers_; }; + class cwv2webResourceRequestedEventArgs : public wv2webResourceRequestedEventArgs { public: cwv2webResourceRequestedEventArgs(ICoreWebView2WebResourceRequestedEventArgs& args) :args_(args) {} @@ -495,12 +487,37 @@ class cwv2webResourceRequestedEventArgs : public wv2webResourceRequestedEventArg } request_.setRequest(req); + + if (!req) { + return nullptr; + } return &request_; } + wv2webResourceResponse* response() override { + CComPtr res; + if (FAILED(args_.get_Response(&res))) { + return nullptr; + } + + response_.setResponse(res); + + if (!res) { + return nullptr; + } + return &response_; + } + + wv2webResourceContext resourceContext() override { + wv2webResourceContext context = wv2webResourceContext_undefined; + args_.get_ResourceContext((COREWEBVIEW2_WEB_RESOURCE_CONTEXT*)&context); + return context; + } + private: ICoreWebView2WebResourceRequestedEventArgs& args_; cwv2webResourceRequest request_; + cwv2webResourceResponse response_; }; class WebResourceRequested : diff --git a/src/wv2.cpp b/src/wv2.cpp index 75f604b..40b3101 100644 --- a/src/wv2.cpp +++ b/src/wv2.cpp @@ -94,16 +94,29 @@ wv2_t wv2createSync2(LPCWSTR browserExecutableFolder, LPCWSTR userDataFolder, } } -wv2env_t wv2getEnv(wv2_t w) { +wv2environment_t wv2getEnvironment(wv2_t w) { if (!w) return nullptr; return CWV2(w)->getEnvironment(); } -wv2bool wv2envSetBrowserProcessExitedHandler(wv2env_t e, browserProcessExited handler) { + +wv2environment_t wv2getEnv(wv2_t w) { + return wv2getEnvironment(w); +} + +wv2bool wv2environment_setBrowserProcessExitedHandler(wv2environment_t e, + browserProcessExited handler) { if (!e) return wv2boolInvalidArg(); return ((cwv2env*)e)->setBrowserProcessExitedHandler(handler); } +wv2webResourceResponse_t +wv2environment_createWebResourceResponse(wv2environment_t env, IStream* content, + int32_t statusCode, LPCWSTR reasonPhrase, LPCWSTR headers) { + if (!env) return nullptr; + return ((cwv2env*)env)->createWebResourceResponse(content, statusCode, + reasonPhrase, headers); +} void wv2destroy(wv2_t* h) { if (!h) return; @@ -683,7 +696,7 @@ wv2webResourceResponse_statusCode(wv2webResourceResponse_t handle) { } HRESULT -wv2webResourceResponse_setStatusCode(wv2webResourceResponse_t handle, int statusCode) { +wv2webResourceResponse_setStatusCode(wv2webResourceResponse_t handle, int32_t statusCode) { if (!handle) return E_INVALIDARG; return WRRES->setStatusCode(statusCode); } @@ -707,13 +720,18 @@ wv2webResourceRequestedEventArgs_request(wv2webResourceRequestedEventArgs_t args return WRREQUESTED_ARGS->request(); } -/* wv2webResourceResponse_t wv2webResourceRequestedEventArgs_response(wv2webResourceRequestedEventArgs_t args) { if (!args) return nullptr; return WRREQUESTED_ARGS->response(); } -*/ + +wv2webResourceContext +wv2webResourceRequestedEventArgs_resourceContext(wv2webResourceRequestedEventArgs_t args) { + if (!args) return wv2webResourceContext_undefined; + return WRREQUESTED_ARGS->resourceContext(); +} + /////////////////////////////////////////////////////////////////////////////// #define REQ_HDRS ((wv2httpRequestHeaders*)handle) diff --git a/wv2.rc b/wv2.rc index 37b4022..3be6ff8 100644 --- a/wv2.rc +++ b/wv2.rc @@ -51,8 +51,8 @@ END // VS_VERSION_INFO VERSIONINFO - FILEVERSION 0,11,0,0 - PRODUCTVERSION 0,11,0,0 + FILEVERSION 0,12,0,0 + PRODUCTVERSION 0,12,0,0 FILEFLAGSMASK 0x3fL #ifdef _DEBUG FILEFLAGS 0x1L @@ -69,12 +69,12 @@ BEGIN BEGIN VALUE "CompanyName", "Kim,Jong-Kook " VALUE "FileDescription", "WebView2 for C/C++" - VALUE "FileVersion", "0.11.0.0" + VALUE "FileVersion", "0.12.0.0" VALUE "InternalName", "wv2.dll" VALUE "LegalCopyright", "Kim,Jong-Kook " VALUE "OriginalFilename", "wv2.dll" VALUE "ProductName", "wv2" - VALUE "ProductVersion", "0.11.0.0" + VALUE "ProductVersion", "0.12.0.0" END END BLOCK "VarFileInfo" diff --git a/wv2.vcxproj b/wv2.vcxproj index d0e16cc..562d1df 100644 --- a/wv2.vcxproj +++ b/wv2.vcxproj @@ -179,6 +179,7 @@ + @@ -196,6 +197,7 @@ + diff --git a/wv2.vcxproj.filters b/wv2.vcxproj.filters index 010865f..55f8b88 100644 --- a/wv2.vcxproj.filters +++ b/wv2.vcxproj.filters @@ -56,6 +56,9 @@ src + + src + @@ -85,6 +88,9 @@ src + + src +