forked from MicrosoftEdge/WebView2Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcessComponent.cpp
More file actions
137 lines (125 loc) · 4.68 KB
/
ProcessComponent.cpp
File metadata and controls
137 lines (125 loc) · 4.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// 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 "ProcessComponent.h"
#include <sstream>
#include "CheckFailure.h"
using namespace Microsoft::WRL;
ProcessComponent::ProcessComponent(AppWindow* appWindow)
: m_appWindow(appWindow), m_webView(appWindow->GetWebView())
{
//! [ProcessFailed]
// Register a handler for the ProcessFailed event.
// This handler checks the failure kind and tries to:
// * Recreate the webview for browser failure and render unresponsive.
// * Reload the webview for render failure.
CHECK_FAILURE(m_webView->add_ProcessFailed(
Callback<ICoreWebView2ProcessFailedEventHandler>(
[this](ICoreWebView2* sender, ICoreWebView2ProcessFailedEventArgs* argsRaw)
-> HRESULT {
wil::com_ptr<ICoreWebView2ProcessFailedEventArgs> args = argsRaw;
COREWEBVIEW2_PROCESS_FAILED_KIND failureKind;
CHECK_FAILURE(args->get_ProcessFailedKind(&failureKind));
if (failureKind == COREWEBVIEW2_PROCESS_FAILED_KIND_BROWSER_PROCESS_EXITED)
{
int button = MessageBox(
m_appWindow->GetMainWindow(),
L"Browser process exited unexpectedly. Recreate webview?",
L"Browser process exited", MB_YESNO);
if (button == IDYES)
{
m_appWindow->ReinitializeWebView();
}
}
else if (
failureKind == COREWEBVIEW2_PROCESS_FAILED_KIND_RENDER_PROCESS_UNRESPONSIVE)
{
int button = MessageBox(
m_appWindow->GetMainWindow(),
L"Browser render process has stopped responding. Recreate webview?",
L"Web page unresponsive", MB_YESNO);
if (button == IDYES)
{
m_appWindow->ReinitializeWebView();
}
}
else if (failureKind == COREWEBVIEW2_PROCESS_FAILED_KIND_RENDER_PROCESS_EXITED)
{
int button = MessageBox(
m_appWindow->GetMainWindow(),
L"Browser render process exited unexpectedly. Reload page?",
L"Web page unresponsive", MB_YESNO);
if (button == IDYES)
{
CHECK_FAILURE(m_webView->Reload());
}
}
return S_OK;
})
.Get(),
&m_processFailedToken));
//! [ProcessFailed]
}
bool ProcessComponent::HandleWindowMessage(
HWND hWnd,
UINT message,
WPARAM wParam,
LPARAM lParam,
LRESULT* result)
{
if (message == WM_COMMAND)
{
switch (LOWORD(wParam))
{
case IDM_PROCESS_INFO:
ShowBrowserProcessInfo();
return true;
case IDM_CRASH_PROCESS:
CrashBrowserProcess();
return true;
case IDM_CRASH_RENDER_PROCESS:
CrashRenderProcess();
return true;
}
}
return false;
}
// Show the WebView's PID to the user.
void ProcessComponent::ShowBrowserProcessInfo() {
UINT32 processId;
m_webView->get_BrowserProcessId(&processId);
WCHAR buffer[4096] = L"";
StringCchPrintf(buffer, ARRAYSIZE(buffer), L"Process ID: %u\n", processId);
MessageBox(m_appWindow->GetMainWindow(), buffer, L"Process Info", MB_OK);
}
// Crash the browser's process on command, to test crash handlers.
void ProcessComponent::CrashBrowserProcess()
{
m_webView->Navigate(L"edge://inducebrowsercrashforrealz");
}
// Crash the browser's render process on command, to test crash handlers.
void ProcessComponent::CrashRenderProcess()
{
m_webView->Navigate(L"edge://kill");
}
/*static*/ void ProcessComponent::EnsureProcessIsClosed(UINT processId, int timeoutMs)
{
UINT exitCode = 1;
if (processId != 0)
{
HANDLE hBrowserProcess = ::OpenProcess(PROCESS_TERMINATE, false, processId);
// Wait for the process to exit by itself
DWORD waitResult = ::WaitForSingleObject(hBrowserProcess, timeoutMs);
if (waitResult != WAIT_OBJECT_0)
{
// Force kill the process if it doesn't exit by itself
BOOL result = ::TerminateProcess(hBrowserProcess, exitCode);
::CloseHandle(hBrowserProcess);
}
}
}
ProcessComponent::~ProcessComponent()
{
m_webView->remove_ProcessFailed(m_processFailedToken);
}