|
35 | 35 | #include "ScenarioCustomScheme.h"
|
36 | 36 | #include "ScenarioCustomSchemeNavigate.h"
|
37 | 37 | #include "ScenarioDOMContentLoaded.h"
|
| 38 | +#include "ScenarioExtensionsManagement.h" |
38 | 39 | #include "ScenarioIFrameDevicePermission.h"
|
39 | 40 | #include "ScenarioNavigateWithWebResourceRequest.h"
|
40 | 41 | #include "ScenarioSharedWorkerWRR.h"
|
|
46 | 47 | #include "SettingsComponent.h"
|
47 | 48 | #include "TextInputDialog.h"
|
48 | 49 | #include "ViewComponent.h"
|
49 |
| - |
50 | 50 | using namespace Microsoft::WRL;
|
51 | 51 | static constexpr size_t s_maxLoadString = 100;
|
52 | 52 | static constexpr UINT s_runAsyncWindowMessage = WM_APP;
|
@@ -472,6 +472,18 @@ bool AppWindow::ExecuteWebViewCommands(WPARAM wParam, LPARAM lParam)
|
472 | 472 | //! [GetUserDataFolder]
|
473 | 473 | return true;
|
474 | 474 | }
|
| 475 | + case IDM_GET_FAILURE_REPORT_FOLDER: |
| 476 | + { |
| 477 | + //! [GetFailureReportFolder] |
| 478 | + auto experimental_environment = |
| 479 | + m_webViewEnvironment.try_query<ICoreWebView2ExperimentalEnvironment>(); |
| 480 | + CHECK_FEATURE_RETURN(experimental_environment); |
| 481 | + wil::unique_cotaskmem_string failureReportFolder; |
| 482 | + experimental_environment->get_FailureReportFolderPath(&failureReportFolder); |
| 483 | + MessageBox(m_mainWindow, failureReportFolder.get(), L"Failure Report Folder", MB_OK); |
| 484 | + //! [GetFailureReportFolder] |
| 485 | + return true; |
| 486 | + } |
475 | 487 | case IDM_CLOSE_WEBVIEW:
|
476 | 488 | {
|
477 | 489 | CloseWebView();
|
@@ -592,6 +604,26 @@ bool AppWindow::ExecuteWebViewCommands(WPARAM wParam, LPARAM lParam)
|
592 | 604 | NewComponent<ScenarioIFrameDevicePermission>(this);
|
593 | 605 | return true;
|
594 | 606 | }
|
| 607 | + case IDM_SCENARIO_BROWSER_PRINT_PREVIEW: |
| 608 | + { |
| 609 | + return ShowPrintUI(COREWEBVIEW2_PRINT_DIALOG_KIND_BROWSER); |
| 610 | + } |
| 611 | + case IDM_SCENARIO_SYSTEM_PRINT: |
| 612 | + { |
| 613 | + return ShowPrintUI(COREWEBVIEW2_PRINT_DIALOG_KIND_SYSTEM); |
| 614 | + } |
| 615 | + case IDM_SCENARIO_PRINT_TO_DEFAULT_PRINTER: |
| 616 | + { |
| 617 | + return PrintToDefaultPrinter(); |
| 618 | + } |
| 619 | + case IDM_SCENARIO_PRINT_TO_PRINTER: |
| 620 | + { |
| 621 | + return PrintToPrinter(); |
| 622 | + } |
| 623 | + case IDM_SCENARIO_PRINT_TO_PDF_STREAM: |
| 624 | + { |
| 625 | + return PrintToPdfStream(); |
| 626 | + } |
595 | 627 | }
|
596 | 628 | return false;
|
597 | 629 | }
|
@@ -693,6 +725,9 @@ bool AppWindow::ExecuteAppCommands(WPARAM wParam, LPARAM lParam)
|
693 | 725 | case IDM_TOGGLE_EXCLUSIVE_USER_DATA_FOLDER_ACCESS:
|
694 | 726 | ToggleExclusiveUserDataFolderAccess();
|
695 | 727 | return true;
|
| 728 | + case IDM_TOGGLE_CUSTOM_CRASH_REPORTING: |
| 729 | + ToggleCustomCrashReporting(); |
| 730 | + return true; |
696 | 731 | case IDM_SCENARIO_CLEAR_BROWSING_DATA_COOKIES:
|
697 | 732 | {
|
698 | 733 | return ClearBrowsingData(COREWEBVIEW2_BROWSING_DATA_KINDS_COOKIES);
|
@@ -795,6 +830,249 @@ void AppWindow::ToggleExclusiveUserDataFolderAccess()
|
795 | 830 | L"Exclusive User Data Folder Access change", MB_OK);
|
796 | 831 | }
|
797 | 832 |
|
| 833 | +void AppWindow::ToggleCustomCrashReporting() |
| 834 | +{ |
| 835 | + m_CustomCrashReportingEnabled = !m_CustomCrashReportingEnabled; |
| 836 | + MessageBox( |
| 837 | + nullptr, |
| 838 | + m_CustomCrashReportingEnabled ? L"Crash reporting will be disabled for new WebView " |
| 839 | + L"created after all webviews are closed." |
| 840 | + : L"Crash reporting will be enabled for new WebView " |
| 841 | + L"created after all webviews are closed.", |
| 842 | + L"Custom Crash Reporting", MB_OK); |
| 843 | +} |
| 844 | + |
| 845 | +//! [ShowPrintUI] |
| 846 | +// Shows the user a print dialog. If `printDialogKind` is |
| 847 | +// COREWEBVIEW2_PRINT_DIALOG_KIND_BROWSER, opens a browser print preview dialog, |
| 848 | +// COREWEBVIEW2_PRINT_DIALOG_KIND_SYSTEM opens a system print dialog. |
| 849 | +bool AppWindow::ShowPrintUI(COREWEBVIEW2_PRINT_DIALOG_KIND printDialogKind) |
| 850 | +{ |
| 851 | + auto webView2Experimental17 = m_webView.try_query<ICoreWebView2Experimental17>(); |
| 852 | + CHECK_FEATURE_RETURN(webView2Experimental17); |
| 853 | + CHECK_FAILURE(webView2Experimental17->ShowPrintUI(printDialogKind)); |
| 854 | + return true; |
| 855 | +} |
| 856 | +//! [ShowPrintUI] |
| 857 | + |
| 858 | +// This example prints the current web page without a print dialog to default printer |
| 859 | +// with the default print settings. |
| 860 | +bool AppWindow::PrintToDefaultPrinter() |
| 861 | +{ |
| 862 | + wil::com_ptr<ICoreWebView2Experimental17> webView2Experimental17; |
| 863 | + CHECK_FAILURE(m_webView->QueryInterface(IID_PPV_ARGS(&webView2Experimental17))); |
| 864 | + |
| 865 | + wil::unique_cotaskmem_string title; |
| 866 | + CHECK_FAILURE(m_webView->get_DocumentTitle(&title)); |
| 867 | + |
| 868 | + // Passing nullptr for `ICoreWebView2PrintSettings` results in default print settings used. |
| 869 | + // Prints current web page with the default page and printer settings. |
| 870 | + CHECK_FAILURE(webView2Experimental17->Print( |
| 871 | + nullptr, Callback<ICoreWebView2ExperimentalPrintCompletedHandler>( |
| 872 | + [title = std::move(title), |
| 873 | + this](HRESULT errorCode, COREWEBVIEW2_PRINT_STATUS printStatus) -> HRESULT |
| 874 | + { |
| 875 | + std::wstring message = L""; |
| 876 | + if (errorCode == S_OK && |
| 877 | + printStatus == COREWEBVIEW2_PRINT_STATUS_SUCCEEDED) |
| 878 | + { |
| 879 | + message = L"Printing " + std::wstring(title.get()) + |
| 880 | + L" document to printer is succedded"; |
| 881 | + } |
| 882 | + else if ( |
| 883 | + errorCode == S_OK && |
| 884 | + printStatus == COREWEBVIEW2_PRINT_STATUS_PRINTER_UNAVAILABLE) |
| 885 | + { |
| 886 | + message = L"Printer is not available, offline or error state"; |
| 887 | + } |
| 888 | + else if (errorCode == E_ABORT) |
| 889 | + { |
| 890 | + message = L"Printing " + std::wstring(title.get()) + |
| 891 | + L" document is in progress"; |
| 892 | + } |
| 893 | + else |
| 894 | + { |
| 895 | + message = L"Printing " + std::wstring(title.get()) + |
| 896 | + L" document to printer is failed"; |
| 897 | + } |
| 898 | + |
| 899 | + AsyncMessageBox(message, L"Print to default printer"); |
| 900 | + |
| 901 | + return S_OK; |
| 902 | + }) |
| 903 | + .Get())); |
| 904 | + return true; |
| 905 | +} |
| 906 | + |
| 907 | +// Function to get printer name by displaying printer text input dialog to the user. |
| 908 | +// User has to specify the desired printer name by querying the installed printers list on the |
| 909 | +// OS to print the web page. You may also choose to display printers list to the user and return |
| 910 | +// user selected printer. |
| 911 | +std::wstring AppWindow::GetPrinterName() |
| 912 | +{ |
| 913 | + std::wstring printerName; |
| 914 | + |
| 915 | + TextInputDialog dialog( |
| 916 | + GetMainWindow(), L"Printer Name", L"Printer Name:", |
| 917 | + L"Specify a printer name from the installed printers list on the OS.", L""); |
| 918 | + |
| 919 | + if (dialog.confirmed) |
| 920 | + { |
| 921 | + printerName = (dialog.input).c_str(); |
| 922 | + } |
| 923 | + return printerName; |
| 924 | + |
| 925 | + // or |
| 926 | + // |
| 927 | + // Use win32 EnumPrinters function to get locally installed printers. |
| 928 | + // Display the printer list to the user and get the user desired printer to print. |
| 929 | + // Return the user selected printer name. |
| 930 | +} |
| 931 | + |
| 932 | +// Function to get print settings for the selected printer. |
| 933 | +// You may also choose get the capabilties from the native printer API, display to the user to |
| 934 | +// get the print settings for the current web page and for the selected printer. |
| 935 | +SamplePrintSettings AppWindow::GetSelectedPrinterPrintSettings(std::wstring printerName) |
| 936 | +{ |
| 937 | + SamplePrintSettings samplePrintSettings; |
| 938 | + samplePrintSettings.PrintBackgrounds = true; |
| 939 | + samplePrintSettings.HeaderAndFooter = true; |
| 940 | + |
| 941 | + return samplePrintSettings; |
| 942 | + |
| 943 | + // or |
| 944 | + // |
| 945 | + // Use win32 DeviceCapabilitiesA function to get the capabilities of the selected printer. |
| 946 | + // Display the printer capabilities to the user along with the page settings. |
| 947 | + // Return the user selected settings. |
| 948 | +} |
| 949 | + |
| 950 | +//! [PrintToPrinter] |
| 951 | +// This example prints the current web page to a specified printer with the settings. |
| 952 | +bool AppWindow::PrintToPrinter() |
| 953 | +{ |
| 954 | + std::wstring printerName = GetPrinterName(); |
| 955 | + // Host apps custom print settings based on the user selection. |
| 956 | + SamplePrintSettings samplePrintSettings = GetSelectedPrinterPrintSettings(printerName); |
| 957 | + |
| 958 | + wil::com_ptr<ICoreWebView2Experimental17> webView2Experimental17; |
| 959 | + CHECK_FAILURE(m_webView->QueryInterface(IID_PPV_ARGS(&webView2Experimental17))); |
| 960 | + |
| 961 | + wil::com_ptr<ICoreWebView2Environment6> webviewEnvironment6; |
| 962 | + CHECK_FAILURE(m_webViewEnvironment->QueryInterface(IID_PPV_ARGS(&webviewEnvironment6))); |
| 963 | + |
| 964 | + wil::com_ptr<ICoreWebView2PrintSettings> printSettings; |
| 965 | + CHECK_FAILURE(webviewEnvironment6->CreatePrintSettings(&printSettings)); |
| 966 | + |
| 967 | + wil::com_ptr<ICoreWebView2ExperimentalPrintSettings2> printSettings2; |
| 968 | + CHECK_FAILURE(printSettings->QueryInterface(IID_PPV_ARGS(&printSettings2))); |
| 969 | + |
| 970 | + CHECK_FAILURE(printSettings->put_Orientation(samplePrintSettings.Orientation)); |
| 971 | + CHECK_FAILURE(printSettings2->put_Copies(samplePrintSettings.Copies)); |
| 972 | + CHECK_FAILURE(printSettings2->put_PagesPerSide(samplePrintSettings.PagesPerSide)); |
| 973 | + CHECK_FAILURE(printSettings2->put_PageRanges(samplePrintSettings.Pages.c_str())); |
| 974 | + if (samplePrintSettings.Media == COREWEBVIEW2_PRINT_MEDIA_SIZE_CUSTOM) |
| 975 | + { |
| 976 | + CHECK_FAILURE(printSettings->put_PageWidth(samplePrintSettings.PaperWidth)); |
| 977 | + CHECK_FAILURE(printSettings->put_PageHeight(samplePrintSettings.PaperHeight)); |
| 978 | + } |
| 979 | + CHECK_FAILURE(printSettings2->put_ColorMode(samplePrintSettings.ColorMode)); |
| 980 | + CHECK_FAILURE(printSettings2->put_Collation(samplePrintSettings.Collation)); |
| 981 | + CHECK_FAILURE(printSettings2->put_Duplex(samplePrintSettings.Duplex)); |
| 982 | + CHECK_FAILURE(printSettings->put_ScaleFactor(samplePrintSettings.ScaleFactor)); |
| 983 | + CHECK_FAILURE( |
| 984 | + printSettings->put_ShouldPrintBackgrounds(samplePrintSettings.PrintBackgrounds)); |
| 985 | + CHECK_FAILURE( |
| 986 | + printSettings->put_ShouldPrintBackgrounds(samplePrintSettings.PrintBackgrounds)); |
| 987 | + CHECK_FAILURE( |
| 988 | + printSettings->put_ShouldPrintHeaderAndFooter(samplePrintSettings.HeaderAndFooter)); |
| 989 | + CHECK_FAILURE(printSettings->put_HeaderTitle(samplePrintSettings.HeaderTitle.c_str())); |
| 990 | + CHECK_FAILURE(printSettings->put_FooterUri(samplePrintSettings.FooterUri.c_str())); |
| 991 | + CHECK_FAILURE(printSettings2->put_PrinterName(printerName.c_str())); |
| 992 | + |
| 993 | + wil::unique_cotaskmem_string title; |
| 994 | + CHECK_FAILURE(m_webView->get_DocumentTitle(&title)); |
| 995 | + |
| 996 | + CHECK_FAILURE(webView2Experimental17->Print( |
| 997 | + printSettings.get(), |
| 998 | + Callback<ICoreWebView2ExperimentalPrintCompletedHandler>( |
| 999 | + [title = std::move(title), |
| 1000 | + this](HRESULT errorCode, COREWEBVIEW2_PRINT_STATUS printStatus) -> HRESULT |
| 1001 | + { |
| 1002 | + std::wstring message = L""; |
| 1003 | + if (errorCode == S_OK && printStatus == COREWEBVIEW2_PRINT_STATUS_SUCCEEDED) |
| 1004 | + { |
| 1005 | + message = L"Printing " + std::wstring(title.get()) + |
| 1006 | + L" document to printer is succedded"; |
| 1007 | + } |
| 1008 | + else if ( |
| 1009 | + errorCode == S_OK && |
| 1010 | + printStatus == COREWEBVIEW2_PRINT_STATUS_PRINTER_UNAVAILABLE) |
| 1011 | + { |
| 1012 | + message = L"Selected printer is not found, not available, offline or " |
| 1013 | + L"error state."; |
| 1014 | + } |
| 1015 | + else if (errorCode == E_INVALIDARG) |
| 1016 | + { |
| 1017 | + message = L"Invalid settings provided for the specified printer"; |
| 1018 | + } |
| 1019 | + else if (errorCode == E_ABORT) |
| 1020 | + { |
| 1021 | + message = L"Printing " + std::wstring(title.get()) + |
| 1022 | + L" document already in progress"; |
| 1023 | + } |
| 1024 | + else |
| 1025 | + { |
| 1026 | + message = L"Printing " + std::wstring(title.get()) + |
| 1027 | + L" document to printer is failed"; |
| 1028 | + } |
| 1029 | + |
| 1030 | + AsyncMessageBox(message, L"Print to printer"); |
| 1031 | + |
| 1032 | + return S_OK; |
| 1033 | + }) |
| 1034 | + .Get())); |
| 1035 | + return true; |
| 1036 | +} |
| 1037 | +//! [PrintToPrinter] |
| 1038 | + |
| 1039 | +// Function to display current web page pdf data in a custom print preview dialog. |
| 1040 | +static void DisplayPdfDataInPrintDialog(IStream* pdfData) |
| 1041 | +{ |
| 1042 | + // You can display the printable pdf data in a custom print preview dialog to the end user. |
| 1043 | +} |
| 1044 | + |
| 1045 | +//! [PrintToPdfStream] |
| 1046 | +// This example prints the Pdf data of the current page to a stream. |
| 1047 | +bool AppWindow::PrintToPdfStream() |
| 1048 | +{ |
| 1049 | + wil::com_ptr<ICoreWebView2Experimental17> webView2Experimental17; |
| 1050 | + CHECK_FAILURE(m_webView->QueryInterface(IID_PPV_ARGS(&webView2Experimental17))); |
| 1051 | + |
| 1052 | + wil::unique_cotaskmem_string title; |
| 1053 | + CHECK_FAILURE(m_webView->get_DocumentTitle(&title)); |
| 1054 | + |
| 1055 | + // Passing nullptr for `ICoreWebView2PrintSettings` results in default print settings used. |
| 1056 | + CHECK_FAILURE(webView2Experimental17->PrintToPdfStream( |
| 1057 | + nullptr, |
| 1058 | + Callback<ICoreWebView2ExperimentalPrintToPdfStreamCompletedHandler>( |
| 1059 | + [title = std::move(title), this](HRESULT errorCode, IStream* pdfData) -> HRESULT |
| 1060 | + { |
| 1061 | + DisplayPdfDataInPrintDialog(pdfData); |
| 1062 | + |
| 1063 | + std::wstring message = |
| 1064 | + L"Printing " + std::wstring(title.get()) + L" document to PDF Stream " + |
| 1065 | + ((errorCode == S_OK && pdfData != nullptr) ? L"succedded" : L"failed"); |
| 1066 | + |
| 1067 | + AsyncMessageBox(message, L"Print to PDF Stream"); |
| 1068 | + |
| 1069 | + return S_OK; |
| 1070 | + }) |
| 1071 | + .Get())); |
| 1072 | + return true; |
| 1073 | +} |
| 1074 | +//! [PrintToPdfStream] |
| 1075 | + |
798 | 1076 | // Message handler for about dialog.
|
799 | 1077 | INT_PTR CALLBACK AppWindow::About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
|
800 | 1078 | {
|
@@ -924,6 +1202,13 @@ void AppWindow::InitializeWebView()
|
924 | 1202 | }
|
925 | 1203 | //! [CoreWebView2CustomSchemeRegistration]
|
926 | 1204 |
|
| 1205 | + Microsoft::WRL::ComPtr<ICoreWebView2ExperimentalEnvironmentOptions2> optionsExperimental2; |
| 1206 | + if (options.As(&optionsExperimental2) == S_OK) |
| 1207 | + { |
| 1208 | + CHECK_FAILURE(optionsExperimental2->put_IsCustomCrashReportingEnabled( |
| 1209 | + m_CustomCrashReportingEnabled ? TRUE : FALSE)); |
| 1210 | + } |
| 1211 | + |
927 | 1212 | HRESULT hr = CreateCoreWebView2EnvironmentWithOptions(
|
928 | 1213 | subFolder, m_userDataFolder.c_str(), options.Get(),
|
929 | 1214 | Callback<ICoreWebView2CreateCoreWebView2EnvironmentCompletedHandler>(
|
|
0 commit comments