MCPcopy Create free account
hub / github.com/MicrosoftEdge/WebView2Samples / InitializeWebView

Method InitializeWebView

SampleApps/WebView2APISample/AppWindow.cpp:1779–1948  ·  view source on GitHub ↗

[CreateCoreWebView2Controller] Create or recreate the WebView and its environment.

Source from the content-addressed store, hash-verified

1777//! [CreateCoreWebView2Controller]
1778// Create or recreate the WebView and its environment.
1779void AppWindow::InitializeWebView()
1780{
1781 // To ensure browser switches get applied correctly, we need to close
1782 // the existing WebView. This will result in a new browser process
1783 // getting created which will apply the browser switches.
1784 CloseWebView();
1785 m_dcompDevice = nullptr;
1786 m_wincompCompositor = nullptr;
1787 LPCWSTR subFolder = nullptr;
1788
1789 if (m_creationModeId == IDM_CREATION_MODE_VISUAL_DCOMP ||
1790 m_creationModeId == IDM_CREATION_MODE_TARGET_DCOMP)
1791 {
1792 HRESULT hr = DCompositionCreateDevice2(nullptr, IID_PPV_ARGS(&m_dcompDevice));
1793 if (!SUCCEEDED(hr))
1794 {
1795 MessageBox(
1796 m_mainWindow,
1797 L"Attempting to create WebView using DComp Visual is not supported.\r\n"
1798 "DComp device creation failed.\r\n"
1799 "Current OS may not support DComp.",
1800 L"Create with Windowless DComp Visual Failed", MB_OK);
1801 return;
1802 }
1803 }
1804 else if (m_creationModeId == IDM_CREATION_MODE_VISUAL_WINCOMP)
1805 {
1806 HRESULT hr = TryCreateDispatcherQueue();
1807 if (!SUCCEEDED(hr))
1808 {
1809 MessageBox(
1810 m_mainWindow,
1811 L"Attempting to create WebView using WinComp Visual is not supported.\r\n"
1812 "WinComp compositor creation failed.\r\n"
1813 "Current OS may not support WinComp.",
1814 L"Create with Windowless WinComp Visual Failed", MB_OK);
1815 return;
1816 }
1817 m_wincompCompositor = winrtComp::Compositor();
1818 }
1819 //! [CreateCoreWebView2EnvironmentWithOptions]
1820
1821 std::wstring args;
1822 // Page Interaction Restriction Manager requires msPageInteractionManagerWebview2 to be
1823 // enabled from the args, as by default it's disabled in the browser. If you want to
1824 // test these scenarios, this flag should be enabled.
1825 args.append(
1826 L"--enable-features=ThirdPartyStoragePartitioning,PartitionedCookies,"
1827 L"msPageInteractionManagerWebview2");
1828 auto options = Microsoft::WRL::Make<CoreWebView2ExperimentalEnvironmentOptions>();
1829 options->put_AdditionalBrowserArguments(args.c_str());
1830 CHECK_FAILURE(
1831 options->put_AllowSingleSignOnUsingOSPrimaryAccount(m_AADSSOEnabled ? TRUE : FALSE));
1832 CHECK_FAILURE(options->put_ExclusiveUserDataFolderAccess(
1833 m_ExclusiveUserDataFolderAccess ? TRUE : FALSE));
1834 if (!m_language.empty())
1835 CHECK_FAILURE(options->put_Language(m_language.c_str()));
1836 CHECK_FAILURE(options->put_IsCustomCrashReportingEnabled(

Callers

nothing calls this directly

Calls 1

ShowFailureFunction · 0.70

Tested by

no test coverage detected