Skip to content

Commit

Permalink
Added support for the webRequestedEventArgs.resourceContext property.
Browse files Browse the repository at this point in the history
- Added support for the `webRequestedEventArgs.resourceContext` property.
- Changed `wv2env_t` to `wv2environment_t`
  • Loading branch information
smok95 committed Jun 3, 2024
1 parent 3c60f2d commit 82fa3d8
Show file tree
Hide file tree
Showing 15 changed files with 218 additions and 51 deletions.
15 changes: 14 additions & 1 deletion example/example_win32/example_win32.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}

Expand Down
4 changes: 4 additions & 0 deletions example/example_win32/example_win32.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
<PreprocessorDefinitions>WIN32;_DEBUG;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>..\..\include\</AdditionalIncludeDirectories>
<CompileAs>CompileAsC</CompileAs>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
Expand All @@ -109,6 +110,7 @@
<PreprocessorDefinitions>WIN32;NDEBUG;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>..\..\include\</AdditionalIncludeDirectories>
<CompileAs>CompileAsC</CompileAs>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
Expand All @@ -125,6 +127,7 @@
<PreprocessorDefinitions>_DEBUG;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>..\..\include\</AdditionalIncludeDirectories>
<CompileAs>CompileAsC</CompileAs>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
Expand All @@ -141,6 +144,7 @@
<PreprocessorDefinitions>NDEBUG;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>..\..\include\</AdditionalIncludeDirectories>
<CompileAs>CompileAsC</CompileAs>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
Expand Down
75 changes: 49 additions & 26 deletions include/wv2.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <windows.h>
#include <stdbool.h>
#include <stdint.h>
#include <objidl.h>
#include "wv2envOpts.h"

#if defined(_MSC_VER) && _MSC_VER < 1900 // Visual Studio 2013
Expand Down Expand Up @@ -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 {
Expand All @@ -103,7 +110,6 @@ typedef struct wv2bool {
HRESULT hr;
} wv2bool;

///////////////////////////////////////////////////////////////////////////////
typedef void* wv2deferral_t; // ICoreWebView2Deferral
WV2_API HRESULT wv2deferral_complete(wv2deferral_t d);

Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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);
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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() {};

Expand Down Expand Up @@ -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(){};
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/cwv2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,7 @@ EventRegistrationToken emptyEventRegistrationToken() {
return token;
}

wv2env* cwv2::getEnvironment() {
wv2environment* cwv2::getEnvironment() {
return &env_;
}

Expand Down
2 changes: 1 addition & 1 deletion src/cwv2.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
15 changes: 15 additions & 0 deletions src/cwv2env.cpp
Original file line number Diff line number Diff line change
@@ -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() {

Expand Down Expand Up @@ -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<ICoreWebView2WebResourceResponse> response;
const HRESULT hr = env2_->CreateWebResourceResponse(content, statusCode, reasonPhrase, headers, &response);
if (FAILED(hr)) return nullptr;
return new cwv2webResourceResponse(response);
}
5 changes: 4 additions & 1 deletion src/cwv2env.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_; }
Expand Down
13 changes: 12 additions & 1 deletion src/cwv2types.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,15 @@ static inline wv2bool wv2boolDefault() {
wv2bool r = {0,};
r.hr = S_OK;
return r;
}
}

template <typename Func>
LPWSTR getStrVal(Func func) {
LPWSTR result = nullptr;
LPWSTR value = nullptr;
if (SUCCEEDED(func(&value))) {
result = _wcsdup(value);
CoTaskMemFree((void*)value);
}
return result;
}
35 changes: 35 additions & 0 deletions src/cwv2webResourceResponse.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include "cwv2webResourceResponse.h"
#include "cwv2types.h"

cwv2webResourceResponse::cwv2webResourceResponse() {
}
cwv2webResourceResponse::cwv2webResourceResponse(CComPtr<ICoreWebView2WebResourceResponse> response) {
setResponse(response);
}

void cwv2webResourceResponse::setResponse(CComPtr<ICoreWebView2WebResourceResponse> 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);
}
20 changes: 20 additions & 0 deletions src/cwv2webResourceResponse.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#pragma once
#include "WebView2.h"
#include "wv2.h"
#include <atlcomcli.h>


class cwv2webResourceResponse : public wv2webResourceResponse {
public:
cwv2webResourceResponse();
cwv2webResourceResponse(CComPtr<ICoreWebView2WebResourceResponse> response);

void setResponse(CComPtr<ICoreWebView2WebResourceResponse> response);
int32_t statusCode() override;
HRESULT setStatusCode(int32_t statusCode) override;
LPWSTR reasonPhrase() override;
HRESULT setReasonPhrase(LPCWSTR reasonPhrase) override;

private:
CComPtr<ICoreWebView2WebResourceResponse> response_;
};
Loading

0 comments on commit 82fa3d8

Please sign in to comment.