Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 84 additions & 26 deletions platform/windows/WebViewWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,60 @@ std::string toUtf8(const wchar_t* text) {
return utf8;
}

std::string windowsError(HRESULT result) {
char code[32];
std::snprintf(code, sizeof(code), "HRESULT 0x%08lX", static_cast<unsigned long>(result));
const DWORD messageCode = HRESULT_FACILITY(result) == FACILITY_WIN32 ? HRESULT_CODE(result) : static_cast<DWORD>(result);
wchar_t* text = nullptr;
FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
nullptr, messageCode, 0, reinterpret_cast<LPWSTR>(&text), 0, nullptr);
const std::string message = std::string(code) + (text ? ": " + toUtf8(text) : "");
LocalFree(text);
return message;
}

std::string runtimeSettings() {
std::string message;
for (const auto* name : {L"WEBVIEW2_BROWSER_EXECUTABLE_FOLDER", L"WEBVIEW2_USER_DATA_FOLDER", L"WEBVIEW2_RELEASE_CHANNELS", L"WEBVIEW2_CHANNEL_SEARCH_KIND"}) {
wchar_t value[32768] = {};
const DWORD length = GetEnvironmentVariableW(name, value, 32768);
if (length && length < 32768) message += "\n" + toUtf8(name) + " = " + toUtf8(value);
}
return message + "\nCheck WebView2 environment overrides and policies for stale paths or restrictive settings.";
}

std::string runtimeAdvice(HRESULT result) {
if (result == E_ACCESSDENIED) return "WebView2 was denied access. Check profile-folder permissions and security software.";
if (result == HRESULT_FROM_WIN32(ERROR_FILE_EXISTS)) return "A file occupies the WebView2 profile-folder path. Move it and retry.";
if (result == HRESULT_FROM_WIN32(ERROR_DISK_FULL)) return "Free disk space and close WebView2 applications so old runtime versions can be removed.";
if (result == HRESULT_FROM_WIN32(ERROR_INVALID_STATE)) return "Close other applications sharing this WebView2 profile; their runtime options conflict.";
if (result == HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED)) return "The configured runtime path isn't supported. Use a WebView2 runtime, not the regular Edge Application folder.";
if (result == HRESULT_FROM_WIN32(ERROR_BAD_EXE_FORMAT)) return "A runtime executable or DLL is damaged or has an incompatible architecture. Repair the WebView2 runtime.";
if (result == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) || result == HRESULT_FROM_WIN32(ERROR_PRODUCT_UNINSTALLED)) return "No compatible runtime is available to this Windows user. Check overrides and repair or install Evergreen for this user or all users; another user's installation isn't sufficient.";
return "Check security software for a blocked WebView2 process and repair the runtime. If needed, close the application and back up its profile folder before recreating it.";
}

std::string runtimeError(HRESULT result) {
return windowsError(result) + "\n" + runtimeAdvice(result) + runtimeSettings();
}

HMODULE webView2Loader() {
static HMODULE loader = [] {
const auto path = executableFolder() / "WebView2Loader.dll";
HMODULE module = LoadLibraryW(path.c_str());
if (!module) {
const DWORD error = GetLastError();
throw std::runtime_error("Windows couldn't load " + toUtf8(path.c_str()) + ": " + windowsError(HRESULT_FROM_WIN32(error)) +
"\nCheck file permissions, quarantine, and that the DLL and its dependencies match the application's architecture.");
}
return module;
}();
return loader;
}

template <typename F>
F loaderFunction(const char* name) {
static HMODULE loader = LoadLibraryW((executableFolder() / "WebView2Loader.dll").c_str());
if (!loader) {
throw std::runtime_error("WebView2Loader.dll couldn't be loaded. Install the WebView2 runtime.");
}
FARPROC address = GetProcAddress(loader, name);
FARPROC address = GetProcAddress(webView2Loader(), name);
if (!address) {
throw std::runtime_error(std::string("WebView2Loader.dll is missing ") + name + ".");
}
Expand All @@ -86,6 +133,7 @@ struct NativeWindow {
HWND hwnd = nullptr;
// What the window is waiting for, for the message when its page doesn't report ready in time.
std::string stage = "Creating the WebView2 environment";
std::filesystem::path profileFolder;
// Set once the web view has failed, so only the first failure is reported.
bool failed = false;
ICoreWebView2Controller* controller = nullptr;
Expand Down Expand Up @@ -189,9 +237,11 @@ bool checkWebView(NativeWindow* native, HRESULT result, const char* operation) {
if (SUCCEEDED(result)) {
return true;
}
char code[32];
std::snprintf(code, sizeof(code), " (HRESULT 0x%08lX)", static_cast<unsigned long>(result));
failWebView(native, std::string(operation) + code);
std::string message = std::string(operation) + "\n" + (native->controller ? windowsError(result) : runtimeError(result));
if (!native->controller) {
message += "\nRequested profile folder: " + toUtf8(native->profileFolder.c_str());
}
failWebView(native, message);
return false;
}

Expand Down Expand Up @@ -937,7 +987,7 @@ void WebViewWindow::platformOpen() {
return;
}
if (!environment) {
failWebView(native, "WebView2 returned no environment. Is the WebView2 runtime installed?");
failWebView(native, "WebView2 was discovered but returned no environment. Repair the runtime and check security software." + runtimeSettings());
return;
}
native->stage = "Creating the WebView2 controller";
Expand Down Expand Up @@ -993,11 +1043,13 @@ void WebViewWindow::platformOpen() {
})),
"Starting the WebView2 controller failed");
};
native->profileFolder = options.webViewDataFolder;
if (!options.webViewDataFolder.empty()) {
std::error_code error;
std::filesystem::create_directories(options.webViewDataFolder, error);
if (error) {
throw std::runtime_error("The WebView2 data folder couldn't be created: " + options.webViewDataFolder.string());
throw std::runtime_error("The WebView2 data folder couldn't be created: " + toUtf8(options.webViewDataFolder.c_str()) +
"\n" + error.message() + "\nCheck permissions and whether a file occupies that path.");
}
}
const std::filesystem::path dataFolder = options.webViewDataFolder;
Expand Down Expand Up @@ -1074,23 +1126,26 @@ std::optional<ScreenRect> WebViewWindow::platformFrame() const {
}

std::optional<RuntimeProblem> missingWindowingRuntime() {
// The loader is found beside the executable, and it reports the runtime's version when the runtime is installed.
HMODULE loader = LoadLibraryW((executableFolder() / "WebView2Loader.dll").c_str());
if (!loader) {
return RuntimeProblem{"WebView2Loader.dll isn't next to the executable, so the windows can't open.", ""};
}
using VersionFn = HRESULT(WINAPI*)(LPCWSTR, LPWSTR*);
auto version = reinterpret_cast<VersionFn>(GetProcAddress(loader, "GetAvailableCoreWebView2BrowserVersionString"));
LPWSTR text = nullptr;
if (!version || FAILED(version(nullptr, &text)) || !text) {
return RuntimeProblem{"The Microsoft Edge WebView2 runtime isn't installed, so the windows can't open. Install it, then start the application again.", kWebView2Download};
VersionFn version;
try {
version = loaderFunction<VersionFn>("GetAvailableCoreWebView2BrowserVersionString");
loaderFunction<CreateEnvironmentFn>("CreateCoreWebView2EnvironmentWithOptions");
} catch (const std::exception& error) {
return RuntimeProblem{error.what(), ""};
}
LPWSTR text = nullptr;
const HRESULT result = version(nullptr, &text);
const bool found = SUCCEEDED(result) && text && *text;
CoTaskMemFree(text);
return std::nullopt;
if (found) return std::nullopt;
if (SUCCEEDED(result)) return RuntimeProblem{"The WebView2 loader returned no version string. Repair the application and runtime.", ""};
const bool missing = result == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
return RuntimeProblem{"WebView2 discovery failed.\n" + runtimeError(result), missing ? kWebView2Download : ""};
}

void offerLink(const std::string& message, const std::string& link) {
const std::wstring text = toWide(message + "\n\nOpen the WebView2 download page now?");
const std::wstring text = toWide(message + "\n\nOpen the WebView2 download page? Run the downloaded installer, then restart the application.");
if (MessageBoxW(nullptr, text.c_str(), toWide(applicationName()).c_str(), MB_YESNO | MB_ICONERROR) == IDYES) {
const std::wstring url = toWide(link);
ShellExecuteW(nullptr, L"open", url.c_str(), nullptr, nullptr, SW_SHOWNORMAL);
Expand Down Expand Up @@ -1136,14 +1191,18 @@ void installWebView2Loader() {
{
std::ofstream file(staged, std::ios::binary | std::ios::trunc);
file.write(bytes, static_cast<std::streamsize>(size));
if (!file) {
throw std::runtime_error("The WebView2 loader couldn't be written to " + staged.string());
file.close();
const auto actual = std::filesystem::file_size(staged, error);
if (!file || error || actual != size) {
throw std::runtime_error("The WebView2 loader couldn't be written and size-verified: " + toUtf8(staged.c_str()));
}
}
std::filesystem::rename(staged, target, error);
if (error) {
std::filesystem::remove(staged, error);
throw std::runtime_error("Can't replace the WebView2 loader while it's in use: " + target.string());
const auto reason = error.message();
std::error_code ignored;
std::filesystem::remove(staged, ignored);
throw std::runtime_error("Couldn't replace " + toUtf8(target.c_str()) + ": " + reason);
}
}

Expand Down Expand Up @@ -1461,4 +1520,3 @@ std::optional<ScreenRect> workAreaAt(const ScreenRect& rect) {
const RECT& area = info.rcWork;
return ScreenRect{area.left, area.top, area.right - area.left, area.bottom - area.top};
}

Loading