diff --git a/SampleApps/WebView2APISample/AppWindow.cpp b/SampleApps/WebView2APISample/AppWindow.cpp index 78842b2..4d47daf 100644 --- a/SampleApps/WebView2APISample/AppWindow.cpp +++ b/SampleApps/WebView2APISample/AppWindow.cpp @@ -55,6 +55,7 @@ #include "ScenarioSaveAs.h" #include "ScenarioScreenCapture.h" #include "ScenarioSensitivityLabel.h" +#include "ScenarioServiceWorkerWRR.h" #include "ScenarioSharedBuffer.h" #include "ScenarioSharedWorkerWRR.h" #include "ScenarioThrottlingControl.h" @@ -611,6 +612,11 @@ bool AppWindow::ExecuteWebViewCommands(WPARAM wParam, LPARAM lParam) NewComponent(this); return true; } + case IDM_SCENARIO_SERVICE_WORKER_WRR: + { + NewComponent(this); + return true; + } case IDM_SCENARIO_SHARED_BUFFER: { NewComponent(this); diff --git a/SampleApps/WebView2APISample/AppWindow.h b/SampleApps/WebView2APISample/AppWindow.h index 0d0b35f..ddfc84c 100644 --- a/SampleApps/WebView2APISample/AppWindow.h +++ b/SampleApps/WebView2APISample/AppWindow.h @@ -172,7 +172,6 @@ class AppWindow { return m_webviewOption; } - private: static PCWSTR GetWindowClass(); @@ -291,6 +290,7 @@ class AppWindow bool m_CustomCrashReportingEnabled = false; bool m_TrackingPreventionEnabled = true; +private: // Fullscreen related code RECT m_previousWindowRect; HMENU m_hMenu; @@ -299,6 +299,7 @@ class AppWindow bool m_isPopupWindow = false; void EnterFullScreen(); void ExitFullScreen(); + // Compositor creation helper methods HRESULT DCompositionCreateDevice2(IUnknown* renderingDevice, REFIID riid, void** ppv); HRESULT TryCreateDispatcherQueue(); diff --git a/SampleApps/WebView2APISample/ScenarioServiceWorkerWRR.cpp b/SampleApps/WebView2APISample/ScenarioServiceWorkerWRR.cpp new file mode 100644 index 0000000..c2c91b2 --- /dev/null +++ b/SampleApps/WebView2APISample/ScenarioServiceWorkerWRR.cpp @@ -0,0 +1,100 @@ +// Copyright (C) Microsoft Corporation. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "stdafx.h" + +#include "ScenarioServiceWorkerWRR.h" + +#include "AppWindow.h" +#include "CheckFailure.h" + +#include "shlwapi.h" + +using namespace Microsoft::WRL; + +static constexpr WCHAR c_samplePath[] = + L"https://mdn.github.io/dom-examples/service-worker/simple-service-worker/"; +static constexpr WCHAR c_wrrUrlPattern[] = L"*"; + +ScenarioServiceWorkerWRR::ScenarioServiceWorkerWRR(AppWindow* appWindow) + : m_appWindow(appWindow), m_webView(appWindow->GetWebView()), m_sampleUri(c_samplePath) +{ + wil::com_ptr webView_22 = m_webView.try_query(); + CHECK_FEATURE_RETURN_EMPTY(webView_22); + + webView_22->AddWebResourceRequestedFilterWithRequestSourceKinds( + c_wrrUrlPattern, COREWEBVIEW2_WEB_RESOURCE_CONTEXT_ALL, + COREWEBVIEW2_WEB_RESOURCE_REQUEST_SOURCE_KINDS_ALL); + webView_22->add_WebResourceRequested( + Callback( + [this](ICoreWebView2* sender, ICoreWebView2WebResourceRequestedEventArgs* args) + -> HRESULT + { + COREWEBVIEW2_WEB_RESOURCE_CONTEXT resourceContext; + args->get_ResourceContext(&resourceContext); + wil::com_ptr request; + CHECK_FAILURE(args->get_Request(&request)); + wil::unique_cotaskmem_string uri; + CHECK_FAILURE(request->get_Uri(&uri)); + if (wcscmp(uri.get(), c_samplePath) != 0) + { + return S_OK; + } + + wil::com_ptr stream; + // Create a stream with some text content + std::string content = "

Response from WV2 " + "interceptor!

"; + + stream.attach(SHCreateMemStream( + reinterpret_cast(content.c_str()), + static_cast(content.size()))); + + wil::com_ptr response; + wil::com_ptr environment; + wil::com_ptr webview2; + m_webView->QueryInterface(IID_PPV_ARGS(&webview2)); + webview2->get_Environment(&environment); + environment->CreateWebResourceResponse( + stream.get(), 200, L"OK", L"Content-Type: text/html", &response); + args->put_Response(response.get()); + return S_OK; + }) + .Get(), + &m_webResourceRequestedToken); + + // Turn off this scenario if we navigate away from the sample page. + CHECK_FAILURE(m_webView->add_ContentLoading( + Callback( + [this](ICoreWebView2* sender, ICoreWebView2ContentLoadingEventArgs* args) -> HRESULT + { + wil::unique_cotaskmem_string uri; + sender->get_Source(&uri); + if (uri.get() != m_sampleUri) + { + m_appWindow->DeleteComponent(this); + } + return S_OK; + }) + .Get(), + &m_contentLoadingToken)); + + CHECK_FAILURE(m_webView->Navigate(m_sampleUri.c_str())); +} + +ScenarioServiceWorkerWRR::~ScenarioServiceWorkerWRR() +{ + wil::com_ptr webView_22 = m_webView.try_query(); + if (webView_22) + { + CHECK_FAILURE(webView_22->RemoveWebResourceRequestedFilterWithRequestSourceKinds( + c_wrrUrlPattern, COREWEBVIEW2_WEB_RESOURCE_CONTEXT_ALL, + COREWEBVIEW2_WEB_RESOURCE_REQUEST_SOURCE_KINDS_ALL)); + } + + CHECK_FAILURE(m_webView->remove_WebResourceRequested(m_webResourceRequestedToken)); + m_webView->remove_ContentLoading(m_contentLoadingToken); +} diff --git a/SampleApps/WebView2APISample/ScenarioServiceWorkerWRR.h b/SampleApps/WebView2APISample/ScenarioServiceWorkerWRR.h new file mode 100644 index 0000000..5d0e6ce --- /dev/null +++ b/SampleApps/WebView2APISample/ScenarioServiceWorkerWRR.h @@ -0,0 +1,26 @@ +// Copyright (C) Microsoft Corporation. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "stdafx.h" + +#include + +#include "AppWindow.h" +#include "ComponentBase.h" + +// This sample demonstrates how to intercept Service Worker requests with +// WebResourceRequested event. +class ScenarioServiceWorkerWRR : public ComponentBase +{ +public: + ScenarioServiceWorkerWRR(AppWindow* appWindow); + ~ScenarioServiceWorkerWRR() override; + +private: + AppWindow* m_appWindow; + wil::com_ptr m_webView; + std::wstring m_sampleUri; + EventRegistrationToken m_contentLoadingToken = {}; + EventRegistrationToken m_webResourceRequestedToken = {}; +}; diff --git a/SampleApps/WebView2APISample/WebView2APISample.rc b/SampleApps/WebView2APISample/WebView2APISample.rc index fece213..15d0562 100644 --- a/SampleApps/WebView2APISample/WebView2APISample.rc +++ b/SampleApps/WebView2APISample/WebView2APISample.rc @@ -261,9 +261,13 @@ BEGIN MENUITEM "Install Default Extensions", IDM_SCENARIO_EXTENSIONS_MANAGEMENT_INSTALL_DEFAULT MENUITEM "Offload Default Extensions", IDM_SCENARIO_EXTENSIONS_MANAGEMENT_OFFLOAD_DEFAULT END - MENUITEM "Custom scheme WebResourceRequested CORS", IDM_SCENARIO_CUSTOM_SCHEME - MENUITEM "Navigate to custom scheme via WebResourceRequested", IDM_SCENARIO_CUSTOM_SCHEME_NAVIGATE - MENUITEM "Shared worker WebResourceRequested", IDM_SCENARIO_SHARED_WORKER + POPUP "WebResourceRequested" + BEGIN + MENUITEM "Custom scheme WebResourceRequested CORS", IDM_SCENARIO_CUSTOM_SCHEME + MENUITEM "Navigate to custom scheme via WebResourceRequested", IDM_SCENARIO_CUSTOM_SCHEME_NAVIGATE + MENUITEM "Service worker WebResourceRequested", IDM_SCENARIO_SERVICE_WORKER_WRR + MENUITEM "Shared worker WebResourceRequested", IDM_SCENARIO_SHARED_WORKER + END POPUP "Custom Download Experience" BEGIN MENUITEM "Use Deferred Download Dialog", IDM_SCENARIO_USE_DEFERRED_DOWNLOAD @@ -297,7 +301,6 @@ BEGIN MENUITEM "Web Messaging", IDM_SCENARIO_POST_WEB_MESSAGE MENUITEM "WebView Event Monitor", IDM_SCENARIO_WEB_VIEW_EVENT_MONITOR MENUITEM "WebView Window Controls Overlay", IDM_SCENARIO_WINDOW_CONTROLS_OVERLAY - MENUITEM "Dropped file path", IDM_SCENARIO_DROPPED_FILE_PATH POPUP "Save As" BEGIN diff --git a/SampleApps/WebView2APISample/WebView2APISample.vcxproj b/SampleApps/WebView2APISample/WebView2APISample.vcxproj index d13bd02..b088b82 100644 --- a/SampleApps/WebView2APISample/WebView2APISample.vcxproj +++ b/SampleApps/WebView2APISample/WebView2APISample.vcxproj @@ -253,6 +253,7 @@ + @@ -315,6 +316,7 @@ + @@ -451,6 +453,9 @@ $(OutDir)\assets + + $(OutDir)\assets + $(OutDir)\assets @@ -525,13 +530,13 @@ - + This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. - + diff --git a/SampleApps/WebView2APISample/assets/ScenarioSensitivityLabelChanged.html b/SampleApps/WebView2APISample/assets/ScenarioSensitivityLabelChanged.html index f21c5e8..60c5eb0 100644 --- a/SampleApps/WebView2APISample/assets/ScenarioSensitivityLabelChanged.html +++ b/SampleApps/WebView2APISample/assets/ScenarioSensitivityLabelChanged.html @@ -2,119 +2,69 @@ - Sensitivity Label Changed Scenario + Sensitivity Label API Sample
-

Sensitivity Label Changed Event Scenario

+

Sensitivity Label API Sample

+
-

About This Scenario

-

This scenario demonstrates the SensitivityLabelChanged event in WebView2. This event is fired when the sensitivity label of the document changes, typically used in Microsoft Information Protection (MIP) scenarios.

- -

The WebView2 control has registered an event handler that will show a popup dialog whenever a sensitivity label change is detected.

-
- - -