@@ -20,57 +20,92 @@ browser instance at runtime), which means separate cookies, user preference sett
2020data storage etc., to help you build a more wonderful experience for your application.
2121
2222# Examples
23- <!-- TEMPLATE
24- Use this section to explain the features of the API, showing
25- example code with each description in both C# (for our WinRT API or .NET API) and
26- in C++ for our COM API. Use snippets of the sample code you wrote for the sample apps.
27- The sample code for C++ and C# should demonstrate the same thing.
2823
29- The general format is:
24+ ## Provide options to create WebView2 with a specific profile
3025
31- ## FirstFeatureName
26+ ### Win32 C++
3227
33- Feature explanation text goes here, including why an app would use it, how it
34- replaces or supplements existing functionality.
28+ ``` cpp
29+ HRESULT AppWindow::CreateControllerWithOptions ()
30+ {
31+ auto webViewEnvironment4 =
32+ m_webViewEnvironment.try_query<ICoreWebView2Environment4>();
3533
36- ```c#
37- void SampleMethod()
34+ Microsoft::WRL::ComPtr<ICoreWebView2ControllerOptions> options;
35+ HRESULT hr = webViewEnvironment4->CreateCoreWebView2ControllerOptions(
36+ m_webviewOption.profile.c_str(), m_webviewOption.isInPrivate, options.GetAddressOf());
37+ if (hr == E_INVALIDARG)
3838 {
39- var show = new AnExampleOf();
40- show.SomeMembers = AndWhyItMight(be, interesting)
39+ ShowFailure (hr, L"Invalid profile name, create WebView failed! Window is closing.");
40+ CloseAppWindow();
41+ return S_FALSE;
4142 }
42- ```
43-
44- ```cpp
45- void SampleClass::SampleMethod()
46- {
47- winrt::com_ptr<ICoreWebView2> webview2 = ...
48- }
49- ```
50-
51- ## SecondFeatureName
43+ CHECK_FAILURE(hr);
5244
53- Feature explanation text goes here, including why an app would use it, how it
54- replaces or supplements existing functionality.
55-
56- ```c#
57- void SampleMethod()
45+ # ifdef USE_WEBVIEW2_WIN10
46+ if (webViewEnvironment4 && (m_dcompDevice || m_wincompCompositor))
47+ #else
48+ if (webViewEnvironment4 && m_dcompDevice)
49+ # endif
5850 {
59- var show = new AnExampleOf();
60- show.SomeMembers = AndWhyItMight(be, interesting)
51+ CHECK_FAILURE (webViewEnvironment4->CreateCoreWebView2CompositionControllerWithOptions(
52+ m_mainWindow, options.Get(),
53+ Callback<ICoreWebView2CreateCoreWebView2CompositionControllerCompletedHandler >(
54+ [ this] (
55+ HRESULT result,
56+ ICoreWebView2CompositionController* compositionController) -> HRESULT {
57+ auto controller =
58+ wil::com_ptr<ICoreWebView2CompositionController >(compositionController)
59+ .query<ICoreWebView2Controller >();
60+ return OnCreateCoreWebView2ControllerCompleted(result, controller.get());
61+ })
62+ .Get()));
6163 }
62- ```
63-
64- ```cpp
65- void SampleClass::SampleMethod()
64+ else
6665 {
67- winrt::com_ptr<ICoreWebView2> webview2 = ...
66+ CHECK_FAILURE(webViewEnvironment4->CreateCoreWebView2ControllerWithOptions(
67+ m_mainWindow, options.Get(),
68+ Callback<ICoreWebView2CreateCoreWebView2ControllerCompletedHandler >(
69+ this, &AppWindow::OnCreateCoreWebView2ControllerCompleted)
70+ .Get()));
6871 }
69- ```
7072
71- As an example of this section, see the Examples section for the Custom Downloads
72- APIs (https://github.com/MicrosoftEdge/WebView2Feedback/blob/master/specs/CustomDownload.md).
73- -->
73+ return S_OK;
74+ }
75+ ```
76+
77+ ## Access the profile property of WebView2
78+
79+ ### Win32 C++
80+
81+ ``` cpp
82+ HRESULT AppWindow::OnCreateCoreWebView2ControllerCompleted (HRESULT result, ICoreWebView2Controller* controller)
83+ {
84+ // ...
85+
86+ m_controller = controller;
87+
88+ // Gets the webview object from controller.
89+ wil::com_ptr<ICoreWebView2> coreWebView2;
90+ CHECK_FAILURE(m_controller->get_CoreWebView2(&coreWebView2));
91+ coreWebView2.query_to(&m_webView);
92+ m_webView3 = coreWebView2.try_query<ICoreWebView2_3>();
93+
94+ // Gets the profile property of webview.
95+ wil::com_ptr<ICoreWebView2Profile> profile;
96+ CHECK_FAILURE(m_webView3->get_Profile(&profile));
97+
98+ // Accesses the profile object.
99+ wil::unique_cotaskmem_string name;
100+ CHECK_FAILURE(profile->get_ProfileName(&name));
101+ BOOL inPrivateModeEnabled;
102+ CHECK_FAILURE(profile->get_InPrivateModeEnablede(&inPrivateModeEnabled));
103+ wil::unique_cotaskmem_string path;
104+ CHECK_FAILURE(profile->get_ProfilePath(&path));
105+
106+ // ...
107+ }
108+ ```
74109
75110# API Details
76111
0 commit comments