# HG changeset patch # User bsmith@81767d24-ef19-dc11-ae90-00e081727c95 # Date 1583162704 0 # Node ID 21bc72ff40cbecbab5547d463e4324cc1a006311 # Parent c1afe013b07c278a3d35ef3f0dc5420af0835890 Win: Update Edge (Chromium) support for the 0.9.430 WebView2 SDK release. This SDK update completely refactored the API, breaking pretty much everything. Hopefully this will be the last beta version of the API with breaking changes. diff -r c1afe013b07c -r 21bc72ff40cb win/edge.cpp --- a/win/edge.cpp Fri Jan 03 23:01:26 2020 +0000 +++ b/win/edge.cpp Mon Mar 02 15:25:04 2020 +0000 @@ -35,7 +35,7 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam); BOOL Detect(VOID); protected: - Microsoft::WRL::ComPtr Env; + Microsoft::WRL::ComPtr Env; }; class EdgeWebView @@ -46,11 +46,12 @@ int URL(const char* url); int JavascriptRun(const char* script, void* scriptdata); VOID DoSize(VOID); - VOID Setup(HWND hwnd, IWebView2WebView* webview); + VOID Setup(HWND hwnd, ICoreWebView2Host* webview); VOID Close(VOID); protected: HWND hWnd = nullptr; - Microsoft::WRL::ComPtr WebView; + Microsoft::WRL::ComPtr WebView; + Microsoft::WRL::ComPtr WebHost; }; LRESULT CALLBACK EdgeBrowser::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) @@ -83,76 +84,94 @@ { // Step 3 - Create a single WebView within the parent window // Create a WebView, whose parent is the main window hWnd - Env->CreateWebView(hWnd, Callback( - [hWnd](HRESULT result, IWebView2WebView* webview) -> HRESULT { + Env->CreateCoreWebView2Host(hWnd, Callback( + [hWnd](HRESULT result, ICoreWebView2Host* webhost) -> HRESULT { EdgeWebView* WebView = new EdgeWebView; + ICoreWebView2* webview; - WebView->Setup(hWnd, webview); + WebView->Setup(hWnd, webhost); dw_window_set_data(hWnd, _DW_HTML_DATA_NAME, DW_POINTER(WebView)); - // Add a few settings for the webview - // this is a redundant demo step as they are the default settings values - IWebView2Settings* Settings; - webview->get_Settings(&Settings); - Settings->put_IsScriptEnabled(TRUE); - Settings->put_AreDefaultScriptDialogsEnabled(TRUE); - Settings->put_IsWebMessageEnabled(TRUE); + if (SUCCEEDED(webhost->get_CoreWebView2(&webview))) { + // Add a few settings for the webview + // this is a redundant demo step as they are the default settings values + ICoreWebView2Settings* Settings; + webview->get_Settings(&Settings); + Settings->put_IsScriptEnabled(TRUE); + Settings->put_AreDefaultScriptDialogsEnabled(TRUE); + Settings->put_IsWebMessageEnabled(TRUE); #ifndef DEBUG - Settings->put_AreDevToolsEnabled(FALSE); + Settings->put_AreDevToolsEnabled(FALSE); #endif + // Save the token, we might need to dw_window_set_data() this value + // for later use to remove the handlers + EventRegistrationToken token; + + // Register a handler for the NavigationStarting event. + webview->add_NavigationStarting( + Callback( + [hWnd](ICoreWebView2* sender, + ICoreWebView2NavigationStartingEventArgs* args) -> HRESULT + { + LPWSTR uri; + sender->get_Source(&uri); + + _wndproc(hWnd, WM_USER + 101, (WPARAM)DW_INT_TO_POINTER(DW_HTML_CHANGE_STARTED), + !wcscmp(uri, L"about:blank") ? (LPARAM)"" : (LPARAM)WideToUTF8((LPWSTR)uri)); + + return S_OK; + }).Get(), &token); + + // Register a handler for the SourceChanged event. + webview->add_SourceChanged( + Callback( + [hWnd](ICoreWebView2* sender, + ICoreWebView2SourceChangedEventArgs* args) -> HRESULT + { + LPWSTR uri; + sender->get_Source(&uri); + + _wndproc(hWnd, WM_USER + 101, (WPARAM)DW_INT_TO_POINTER(DW_HTML_CHANGE_REDIRECT), + !wcscmp(uri, L"about:blank") ? (LPARAM)"" : (LPARAM)WideToUTF8((LPWSTR)uri)); + + return S_OK; + }).Get(), &token); + + // Register a handler for the ContentLoading event. + webview->add_ContentLoading( + Callback( + [hWnd](ICoreWebView2* sender, + ICoreWebView2ContentLoadingEventArgs* args) -> HRESULT + { + LPWSTR uri; + sender->get_Source(&uri); + + _wndproc(hWnd, WM_USER + 101, (WPARAM)DW_INT_TO_POINTER(DW_HTML_CHANGE_LOADING), + !wcscmp(uri, L"about:blank") ? (LPARAM)"" : (LPARAM)WideToUTF8((LPWSTR)uri)); + + return S_OK; + }).Get(), &token); + + // Register a handler for the NavigationCompleted event. + webview->add_NavigationCompleted( + Callback( + [hWnd](ICoreWebView2* sender, + ICoreWebView2NavigationCompletedEventArgs* args) -> HRESULT + { + LPWSTR uri; + sender->get_Source(&uri); + + _wndproc(hWnd, WM_USER + 101, (WPARAM)DW_INT_TO_POINTER(DW_HTML_CHANGE_COMPLETE), + !wcscmp(uri, L"about:blank") ? (LPARAM)"" : (LPARAM)WideToUTF8((LPWSTR)uri)); + + return S_OK; + }).Get(), &token); + } + // Resize WebView to fit the bounds of the parent window WebView->DoSize(); - // Save the token, we might need to dw_window_set_data() this value - // for later use to remove the handlers - EventRegistrationToken token; - - // Register a handler for the NavigationStarting event. - webview->add_NavigationStarting( - Callback( - [hWnd](IWebView2WebView* sender, - IWebView2NavigationStartingEventArgs* args) -> HRESULT - { - LPWSTR uri; - sender->get_Source(&uri); - - _wndproc(hWnd, WM_USER + 101, (WPARAM)DW_INT_TO_POINTER(DW_HTML_CHANGE_STARTED), - !wcscmp(uri, L"about:blank") ? (LPARAM)"" : (LPARAM)WideToUTF8((LPWSTR)uri)); - - return S_OK; - }).Get(), &token); - - // Register a handler for the DocumentStateChanged event. - webview->add_DocumentStateChanged( - Callback( - [hWnd](IWebView2WebView* sender, - IWebView2DocumentStateChangedEventArgs* args) -> HRESULT - { - LPWSTR uri; - sender->get_Source(&uri); - - _wndproc(hWnd, WM_USER + 101, (WPARAM)DW_INT_TO_POINTER(DW_HTML_CHANGE_LOADING), - !wcscmp(uri, L"about:blank") ? (LPARAM)"" : (LPARAM)WideToUTF8((LPWSTR)uri)); - - return S_OK; - }).Get(), &token); - - // Register a handler for the NavigationCompleted event. - webview->add_NavigationCompleted( - Callback( - [hWnd](IWebView2WebView* sender, - IWebView2NavigationCompletedEventArgs* args) -> HRESULT - { - LPWSTR uri; - sender->get_Source(&uri); - - _wndproc(hWnd, WM_USER + 101, (WPARAM)DW_INT_TO_POINTER(DW_HTML_CHANGE_COMPLETE), - !wcscmp(uri, L"about:blank") ? (LPARAM)"" : (LPARAM)WideToUTF8((LPWSTR)uri)); - - return S_OK; - }).Get(), &token); - // Handle cached load requests due to delayed // loading of the edge webview contexts char *url = (char *)dw_window_get_data(hWnd, _DW_HTML_DATA_LOCATION); @@ -200,17 +219,17 @@ BOOL isVisible; GetClientRect(hWnd, &bounds); - WebView->put_Bounds(bounds); - WebView->get_IsVisible(&isVisible); + WebHost->put_Bounds(bounds); + WebHost->get_IsVisible(&isVisible); if(!isVisible) - WebView->put_IsVisible(TRUE); + WebHost->put_IsVisible(TRUE); } BOOL EdgeBrowser::Detect(VOID) { - CreateWebView2EnvironmentWithDetails(nullptr, nullptr, nullptr, - Callback( - [this](HRESULT result, IWebView2Environment* env) -> HRESULT { + CreateCoreWebView2EnvironmentWithDetails(nullptr, nullptr, nullptr, + Callback( + [this](HRESULT result, ICoreWebView2Environment* env) -> HRESULT { // Successfully created Edge environment, return TRUE Env = env; return S_OK; @@ -290,7 +309,7 @@ if (WebView) WebView->ExecuteScript(UTF8toWide(script), - Callback( + Callback( [thishwnd, scriptdata](HRESULT error, PCWSTR result) -> HRESULT { char *scriptresult; @@ -316,16 +335,17 @@ return DW_ERROR_NONE; } -VOID EdgeWebView::Setup(HWND hwnd, IWebView2WebView* webview) +VOID EdgeWebView::Setup(HWND hwnd, ICoreWebView2Host* host) { hWnd = hwnd; - WebView = webview; + WebHost = host; + host->get_CoreWebView2(&WebView); } VOID EdgeWebView::Close(VOID) { - if (WebView) - WebView->Close(); + if (WebHost) + WebHost->Close(); } EdgeBrowser *DW_EDGE = NULL;