#include #include #include "../utils/log.h" #include "webview_environment.h" #include "webview_environment_manager.h" namespace flutter_inappwebview_plugin { using namespace Microsoft::WRL; WebViewEnvironment::WebViewEnvironment(const FlutterInappwebviewWindowsPlugin* plugin, const std::string& id) : plugin(plugin), id(id), channelDelegate(std::make_unique(this, plugin->registrar->messenger())) {} void WebViewEnvironment::create(const std::unique_ptr settings, const std::function completionHandler) { if (!plugin) { if (completionHandler) { completionHandler(E_FAIL); } return; } auto hwnd = plugin->webViewEnvironmentManager->getHWND(); if (!hwnd) { if (completionHandler) { completionHandler(E_FAIL); } return; } auto options = Make(); if (settings) { if (settings->additionalBrowserArguments.has_value()) { options->put_AdditionalBrowserArguments(utf8_to_wide(settings->additionalBrowserArguments.value()).c_str()); } if (settings->allowSingleSignOnUsingOSPrimaryAccount.has_value()) { options->put_AllowSingleSignOnUsingOSPrimaryAccount(settings->allowSingleSignOnUsingOSPrimaryAccount.value()); } if (settings->language.has_value()) { options->put_Language(utf8_to_wide(settings->language.value()).c_str()); } if (settings->targetCompatibleBrowserVersion.has_value()) { options->put_TargetCompatibleBrowserVersion(utf8_to_wide(settings->targetCompatibleBrowserVersion.value()).c_str()); } } auto hr = CreateCoreWebView2EnvironmentWithOptions( settings && settings->browserExecutableFolder.has_value() ? utf8_to_wide(settings->browserExecutableFolder.value()).c_str() : nullptr, settings && settings->userDataFolder.has_value() ? utf8_to_wide(settings->userDataFolder.value()).c_str() : nullptr, options.Get(), Callback( [this, hwnd, completionHandler](HRESULT result, wil::com_ptr environment) -> HRESULT { if (succeededOrLog(result)) { environment_ = std::move(environment); auto hr = environment_->CreateCoreWebView2Controller(hwnd, Callback( [this, completionHandler](HRESULT result, wil::com_ptr controller) -> HRESULT { if (succeededOrLog(result)) { webViewController_ = std::move(controller); webViewController_->get_CoreWebView2(&webView_); webViewController_->put_IsVisible(false); } if (completionHandler) { completionHandler(result); } return S_OK; }).Get()); if (failedAndLog(hr) && completionHandler) { completionHandler(hr); } } else if (completionHandler) { completionHandler(result); } return S_OK; }).Get()); if (failedAndLog(hr) && completionHandler) { completionHandler(hr); } } WebViewEnvironment::~WebViewEnvironment() { debugLog("dealloc WebViewEnvironment"); } }