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
114 lines (102 loc) · 3.53 KB
/
ProcessComponent.cpp
File metadata and controls
114 lines (102 loc) · 3.53 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
// 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 "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 if the browser process failed, and asks the user if
// they want to recreate the webview.
CHECK_FAILURE(m_webView->add_ProcessFailed(
Callback<ICoreWebView2ProcessFailedEventHandler>(
[this](ICoreWebView2* sender,
ICoreWebView2ProcessFailedEventArgs* args) -> HRESULT
{
CORE_WEBVIEW2_PROCESS_FAILED_KIND failureType;
CHECK_FAILURE(args->get_ProcessFailedKind(&failureType));
if (failureType == CORE_WEBVIEW2_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 (failureType == CORE_WEBVIEW2_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();
}
}
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;
}
}
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");
}
/*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);
}