Nui
webview2_environment_options.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <objbase.h>
4 #include "wrl/implements.h"
5 
6 #include "webview2.h"
7 #define CORE_WEBVIEW_TARGET_PRODUCT_VERSION L"118.0.2088.41"
8 
9 #define COREWEBVIEW2ENVIRONMENTOPTIONS_STRING_PROPERTY(p) \
10 \
11  public: \
12  HRESULT STDMETHODCALLTYPE get_##p(LPWSTR* value) override \
13  { \
14  if (!value) \
15  return E_POINTER; \
16  *value = m_##p.Copy(); \
17  if ((*value == nullptr) && (m_##p.Get() != nullptr)) \
18  return HRESULT_FROM_WIN32(GetLastError()); \
19  return S_OK; \
20  } \
21  HRESULT STDMETHODCALLTYPE put_##p(LPCWSTR value) override \
22  { \
23  LPCWSTR result = m_##p.Set(value); \
24  if ((result == nullptr) && (value != nullptr)) \
25  return HRESULT_FROM_WIN32(GetLastError()); \
26  return S_OK; \
27  } \
28 \
29  protected: \
30  AutoCoMemString m_##p;
31 
32 #define COREWEBVIEW2ENVIRONMENTOPTIONS_BOOL_PROPERTY(p, defPVal) \
33 \
34  public: \
35  HRESULT STDMETHODCALLTYPE get_##p(BOOL* value) override \
36  { \
37  if (!value) \
38  return E_POINTER; \
39  *value = m_##p; \
40  return S_OK; \
41  } \
42  HRESULT STDMETHODCALLTYPE put_##p(BOOL value) override \
43  { \
44  m_##p = value; \
45  return S_OK; \
46  } \
47 \
48  protected: \
49  BOOL m_##p = defPVal ? TRUE : FALSE;
50 
51 #define DEFINE_AUTO_COMEM_STRING() \
52 \
53  protected: \
54  class AutoCoMemString \
55  { \
56  public: \
57  AutoCoMemString() \
58  {} \
59  ~AutoCoMemString() \
60  { \
61  Release(); \
62  } \
63  void Release() \
64  { \
65  if (m_string) \
66  { \
67  deallocate_fn(m_string); \
68  m_string = nullptr; \
69  } \
70  } \
71 \
72  LPCWSTR Set(LPCWSTR str) \
73  { \
74  Release(); \
75  if (str) \
76  { \
77  m_string = MakeCoMemString(str); \
78  } \
79  return m_string; \
80  } \
81  LPCWSTR Get() \
82  { \
83  return m_string; \
84  } \
85  LPWSTR Copy() \
86  { \
87  if (m_string) \
88  return MakeCoMemString(m_string); \
89  return nullptr; \
90  } \
91 \
92  protected: \
93  LPWSTR MakeCoMemString(LPCWSTR source) \
94  { \
95  const size_t length = wcslen(source); \
96  const size_t bytes = (length + 1) * sizeof(*source); \
97 \
98  if (bytes <= length) \
99  { \
100  return nullptr; \
101  } \
102 \
103  wchar_t* result = reinterpret_cast<wchar_t*>(allocate_fn(bytes)); \
104 \
105  if (result) \
106  memcpy(result, source, bytes); \
107  return result; \
108  } \
109  LPWSTR m_string = nullptr; \
110  };
111 
112 template <typename allocate_fn_t, allocate_fn_t allocate_fn, typename deallocate_fn_t, deallocate_fn_t deallocate_fn>
115  Microsoft::WRL::RuntimeClassFlags<Microsoft::WRL::ClassicCom>,
116  ICoreWebView2CustomSchemeRegistration>
117 {
118  public:
120  {
121  m_schemeName.Set(schemeName);
122  }
123 
126  {
128  }
129 
130  HRESULT STDMETHODCALLTYPE get_SchemeName(LPWSTR* schemeName) override
131  {
132  if (!schemeName)
133  return E_POINTER;
134  *schemeName = m_schemeName.Copy();
135  if ((*schemeName == nullptr) && (m_schemeName.Get() != nullptr))
136  return HRESULT_FROM_WIN32(GetLastError());
137  return S_OK;
138  }
139 
140  HRESULT STDMETHODCALLTYPE GetAllowedOrigins(UINT32* allowedOriginsCount, LPWSTR** allowedOrigins) override
141  {
142  if (!allowedOrigins || !allowedOriginsCount)
143  {
144  return E_POINTER;
145  }
146  *allowedOriginsCount = 0;
147  if (m_allowedOriginsCount == 0)
148  {
149  *allowedOrigins = nullptr;
150  return S_OK;
151  }
152  else
153  {
154  *allowedOrigins = reinterpret_cast<LPWSTR*>(allocate_fn(m_allowedOriginsCount * sizeof(LPWSTR)));
155  if (!(*allowedOrigins))
156  {
157  return HRESULT_FROM_WIN32(GetLastError());
158  }
159  ZeroMemory(*allowedOrigins, m_allowedOriginsCount * sizeof(LPWSTR));
160  for (UINT32 i = 0; i < m_allowedOriginsCount; i++)
161  {
162  (*allowedOrigins)[i] = m_allowedOrigins[i].Copy();
163  if (!(*allowedOrigins)[i])
164  {
165  HRESULT hr = HRESULT_FROM_WIN32(GetLastError());
166  for (UINT32 j = 0; j < i; j++)
167  {
168  deallocate_fn((*allowedOrigins)[j]);
169  }
170  deallocate_fn(*allowedOrigins);
171  return hr;
172  }
173  }
174  *allowedOriginsCount = m_allowedOriginsCount;
175  return S_OK;
176  }
177  }
178 
179  HRESULT STDMETHODCALLTYPE SetAllowedOrigins(UINT32 allowedOriginsCount, LPCWSTR* allowedOrigins) override
180  {
182  if (allowedOriginsCount == 0)
183  {
184  return S_OK;
185  }
186  else
187  {
188  m_allowedOrigins = new AutoCoMemString[allowedOriginsCount];
189  if (!m_allowedOrigins)
190  {
191  return HRESULT_FROM_WIN32(GetLastError());
192  }
193  for (UINT32 i = 0; i < allowedOriginsCount; i++)
194  {
195  m_allowedOrigins[i].Set(allowedOrigins[i]);
196  if (!m_allowedOrigins[i].Get())
197  {
198  HRESULT hr = HRESULT_FROM_WIN32(GetLastError());
199  for (UINT32 j = 0; j < i; j++)
200  {
201  m_allowedOrigins[j].Release();
202  }
204  delete[] (m_allowedOrigins);
205  return hr;
206  }
207  }
208  m_allowedOriginsCount = allowedOriginsCount;
209  return S_OK;
210  }
211  }
212 
213  protected:
215 
217  {
218  if (m_allowedOrigins)
219  {
220  delete[] (m_allowedOrigins);
221  m_allowedOrigins = nullptr;
222  }
223  }
224 
225  AutoCoMemString m_schemeName;
227  COREWEBVIEW2ENVIRONMENTOPTIONS_BOOL_PROPERTY(HasAuthorityComponent, false)
228  AutoCoMemString* m_allowedOrigins = nullptr;
229  unsigned int m_allowedOriginsCount = 0;
230 };
231 
232 // This is a base COM class that implements ICoreWebView2EnvironmentOptions.
233 template <typename allocate_fn_t, allocate_fn_t allocate_fn, typename deallocate_fn_t, deallocate_fn_t deallocate_fn>
235  : public Microsoft::WRL::Implements<
236  Microsoft::WRL::RuntimeClassFlags<Microsoft::WRL::ClassicCom>,
237  ICoreWebView2EnvironmentOptions,
238  ICoreWebView2EnvironmentOptions2,
239  ICoreWebView2EnvironmentOptions3,
240  ICoreWebView2EnvironmentOptions4,
241  ICoreWebView2EnvironmentOptions5>
242 {
243  public:
245  {
246  // Initialize the target compatible browser version value to the version
247  // of the browser binaries corresponding to this version of the SDK.
248  m_TargetCompatibleBrowserVersion.Set(CORE_WEBVIEW_TARGET_PRODUCT_VERSION);
249  }
250 
251  protected:
253  {
254  ReleaseCustomSchemeRegistrations();
255  };
256 
258  {
259  if (m_customSchemeRegistrations)
260  {
261  for (UINT32 i = 0; i < m_customSchemeRegistrationsCount; i++)
262  {
263  m_customSchemeRegistrations[i]->Release();
264  }
265  deallocate_fn(m_customSchemeRegistrations);
266  m_customSchemeRegistrations = nullptr;
267  m_customSchemeRegistrationsCount = 0;
268  }
269  }
270 
271  private:
272  ICoreWebView2CustomSchemeRegistration** m_customSchemeRegistrations = nullptr;
273  unsigned int m_customSchemeRegistrationsCount = 0;
274 
276 
277  public:
278  // ICoreWebView2EnvironmentOptions
279  COREWEBVIEW2ENVIRONMENTOPTIONS_STRING_PROPERTY(AdditionalBrowserArguments)
281  COREWEBVIEW2ENVIRONMENTOPTIONS_STRING_PROPERTY(TargetCompatibleBrowserVersion)
282  COREWEBVIEW2ENVIRONMENTOPTIONS_BOOL_PROPERTY(AllowSingleSignOnUsingOSPrimaryAccount, false)
283 
284  // ICoreWebView2EnvironmentOptions2
285  COREWEBVIEW2ENVIRONMENTOPTIONS_BOOL_PROPERTY(ExclusiveUserDataFolderAccess, false)
286 
287  // ICoreWebView2EnvironmentOptions3
288  COREWEBVIEW2ENVIRONMENTOPTIONS_BOOL_PROPERTY(IsCustomCrashReportingEnabled, false)
289 
290  // ICoreWebView2EnvironmentOptions4
291 
292  public:
293  HRESULT STDMETHODCALLTYPE
294  GetCustomSchemeRegistrations(UINT32* count, ICoreWebView2CustomSchemeRegistration*** schemeRegistrations) override
295  {
296  if (!count || !schemeRegistrations)
297  {
298  return E_POINTER;
299  }
300  *count = 0;
301  if (m_customSchemeRegistrationsCount == 0)
302  {
303  *schemeRegistrations = nullptr;
304  return S_OK;
305  }
306  else
307  {
308  *schemeRegistrations = reinterpret_cast<ICoreWebView2CustomSchemeRegistration**>(
309  allocate_fn(sizeof(ICoreWebView2CustomSchemeRegistration*) * m_customSchemeRegistrationsCount));
310  if (!*schemeRegistrations)
311  {
312  return HRESULT_FROM_WIN32(GetLastError());
313  }
314  for (UINT32 i = 0; i < m_customSchemeRegistrationsCount; i++)
315  {
316  (*schemeRegistrations)[i] = m_customSchemeRegistrations[i];
317  (*schemeRegistrations)[i]->AddRef();
318  }
319  *count = m_customSchemeRegistrationsCount;
320  return S_OK;
321  }
322  }
323 
324  HRESULT STDMETHODCALLTYPE
325  SetCustomSchemeRegistrations(UINT32 count, ICoreWebView2CustomSchemeRegistration** schemeRegistrations) override
326  {
327  ReleaseCustomSchemeRegistrations();
328  m_customSchemeRegistrations = reinterpret_cast<ICoreWebView2CustomSchemeRegistration**>(
329  allocate_fn(sizeof(ICoreWebView2CustomSchemeRegistration*) * count));
330  if (!m_customSchemeRegistrations)
331  {
332  return static_cast<HRESULT>(GetLastError());
333  }
334  for (UINT32 i = 0; i < count; i++)
335  {
336  m_customSchemeRegistrations[i] = schemeRegistrations[i];
337  m_customSchemeRegistrations[i]->AddRef();
338  }
339  m_customSchemeRegistrationsCount = count;
340  return S_OK;
341  }
342 
343  // ICoreWebView2EnvironmentOptions5
344  COREWEBVIEW2ENVIRONMENTOPTIONS_BOOL_PROPERTY(EnableTrackingPrevention, true)
345 };
346 
347 template <typename allocate_fn_t, allocate_fn_t allocate_fn, typename deallocate_fn_t, deallocate_fn_t deallocate_fn>
350  Microsoft::WRL::RuntimeClassFlags<Microsoft::WRL::ClassicCom>,
351  CoreWebView2EnvironmentOptionsBase<allocate_fn_t, allocate_fn, deallocate_fn_t, deallocate_fn>>
352 {
353  public:
355  {}
356 
357  protected:
359  {}
360 };
361 
363  decltype(&::CoTaskMemAlloc),
364  ::CoTaskMemAlloc,
365  decltype(&::CoTaskMemFree),
366  ::CoTaskMemFree>
368 
370  decltype(&::CoTaskMemAlloc),
371  ::CoTaskMemAlloc,
372  decltype(&::CoTaskMemFree),
373  ::CoTaskMemFree>
Definition: webview2_environment_options.hpp:117
HRESULT STDMETHODCALLTYPE SetAllowedOrigins(UINT32 allowedOriginsCount, LPCWSTR *allowedOrigins) override
Definition: webview2_environment_options.hpp:179
AutoCoMemString * m_allowedOrigins
Definition: webview2_environment_options.hpp:228
HRESULT STDMETHODCALLTYPE GetAllowedOrigins(UINT32 *allowedOriginsCount, LPWSTR **allowedOrigins) override
Definition: webview2_environment_options.hpp:140
AutoCoMemString m_schemeName
Definition: webview2_environment_options.hpp:225
~CoreWebView2CustomSchemeRegistrationBase()
Definition: webview2_environment_options.hpp:125
CoreWebView2CustomSchemeRegistrationBase(LPCWSTR schemeName)
Definition: webview2_environment_options.hpp:119
void ReleaseAllowedOrigins()
Definition: webview2_environment_options.hpp:216
CoreWebView2CustomSchemeRegistrationBase(CoreWebView2CustomSchemeRegistrationBase &&)=default
HRESULT STDMETHODCALLTYPE get_SchemeName(LPWSTR *schemeName) override
Definition: webview2_environment_options.hpp:130
unsigned int m_allowedOriginsCount
Definition: webview2_environment_options.hpp:229
Definition: webview2_environment_options.hpp:352
~CoreWebView2EnvironmentOptionsBaseClass() override
Definition: webview2_environment_options.hpp:358
CoreWebView2EnvironmentOptionsBaseClass()
Definition: webview2_environment_options.hpp:354
Definition: webview2_environment_options.hpp:242
void ReleaseCustomSchemeRegistrations()
Definition: webview2_environment_options.hpp:257
~CoreWebView2EnvironmentOptionsBase()
Definition: webview2_environment_options.hpp:252
CoreWebView2EnvironmentOptionsBase()
Definition: webview2_environment_options.hpp:244
HRESULT STDMETHODCALLTYPE SetCustomSchemeRegistrations(UINT32 count, ICoreWebView2CustomSchemeRegistration **schemeRegistrations) override
Definition: webview2_environment_options.hpp:325
Definition: implements.h:2422
@ ClassicCom
Definition: implements.h:80
This file has no copyright assigned and is placed in the Public Domain.
Definition: client.h:26
#define COREWEBVIEW2ENVIRONMENTOPTIONS_BOOL_PROPERTY(p, defPVal)
Definition: webview2_environment_options.hpp:32
CoreWebView2CustomSchemeRegistrationBase< decltype(&::CoTaskMemAlloc), ::CoTaskMemAlloc, decltype(&::CoTaskMemFree), ::CoTaskMemFree > CoreWebView2CustomSchemeRegistration
Definition: webview2_environment_options.hpp:367
CoreWebView2EnvironmentOptionsBaseClass< decltype(&::CoTaskMemAlloc), ::CoTaskMemAlloc, decltype(&::CoTaskMemFree), ::CoTaskMemFree > CoreWebView2EnvironmentOptions
Definition: webview2_environment_options.hpp:374
#define CORE_WEBVIEW_TARGET_PRODUCT_VERSION
Definition: webview2_environment_options.hpp:7
#define DEFINE_AUTO_COMEM_STRING()
Definition: webview2_environment_options.hpp:51
#define COREWEBVIEW2ENVIRONMENTOPTIONS_STRING_PROPERTY(p)
Definition: webview2_environment_options.hpp:9