comparison win/edge.cpp @ 2060:21bc72ff40cb

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.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Mon, 02 Mar 2020 15:25:04 +0000
parents cfa0a9554118
children 2dacac5e4023
comparison
equal deleted inserted replaced
2059:c1afe013b07c 2060:21bc72ff40cb
33 { 33 {
34 public: 34 public:
35 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam); 35 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
36 BOOL Detect(VOID); 36 BOOL Detect(VOID);
37 protected: 37 protected:
38 Microsoft::WRL::ComPtr<IWebView2Environment> Env; 38 Microsoft::WRL::ComPtr<ICoreWebView2Environment> Env;
39 }; 39 };
40 40
41 class EdgeWebView 41 class EdgeWebView
42 { 42 {
43 public: 43 public:
44 VOID Action(int action); 44 VOID Action(int action);
45 int Raw(const char* string); 45 int Raw(const char* string);
46 int URL(const char* url); 46 int URL(const char* url);
47 int JavascriptRun(const char* script, void* scriptdata); 47 int JavascriptRun(const char* script, void* scriptdata);
48 VOID DoSize(VOID); 48 VOID DoSize(VOID);
49 VOID Setup(HWND hwnd, IWebView2WebView* webview); 49 VOID Setup(HWND hwnd, ICoreWebView2Host* webview);
50 VOID Close(VOID); 50 VOID Close(VOID);
51 protected: 51 protected:
52 HWND hWnd = nullptr; 52 HWND hWnd = nullptr;
53 Microsoft::WRL::ComPtr<IWebView2WebView> WebView; 53 Microsoft::WRL::ComPtr<ICoreWebView2> WebView;
54 Microsoft::WRL::ComPtr<ICoreWebView2Host> WebHost;
54 }; 55 };
55 56
56 LRESULT CALLBACK EdgeBrowser::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) 57 LRESULT CALLBACK EdgeBrowser::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
57 { 58 {
58 switch (uMsg) 59 switch (uMsg)
81 82
82 case WM_CREATE: 83 case WM_CREATE:
83 { 84 {
84 // Step 3 - Create a single WebView within the parent window 85 // Step 3 - Create a single WebView within the parent window
85 // Create a WebView, whose parent is the main window hWnd 86 // Create a WebView, whose parent is the main window hWnd
86 Env->CreateWebView(hWnd, Callback<IWebView2CreateWebViewCompletedHandler>( 87 Env->CreateCoreWebView2Host(hWnd, Callback<ICoreWebView2CreateCoreWebView2HostCompletedHandler>(
87 [hWnd](HRESULT result, IWebView2WebView* webview) -> HRESULT { 88 [hWnd](HRESULT result, ICoreWebView2Host* webhost) -> HRESULT {
88 EdgeWebView* WebView = new EdgeWebView; 89 EdgeWebView* WebView = new EdgeWebView;
89 90 ICoreWebView2* webview;
90 WebView->Setup(hWnd, webview); 91
92 WebView->Setup(hWnd, webhost);
91 dw_window_set_data(hWnd, _DW_HTML_DATA_NAME, DW_POINTER(WebView)); 93 dw_window_set_data(hWnd, _DW_HTML_DATA_NAME, DW_POINTER(WebView));
92 94
93 // Add a few settings for the webview 95 if (SUCCEEDED(webhost->get_CoreWebView2(&webview))) {
94 // this is a redundant demo step as they are the default settings values 96 // Add a few settings for the webview
95 IWebView2Settings* Settings; 97 // this is a redundant demo step as they are the default settings values
96 webview->get_Settings(&Settings); 98 ICoreWebView2Settings* Settings;
97 Settings->put_IsScriptEnabled(TRUE); 99 webview->get_Settings(&Settings);
98 Settings->put_AreDefaultScriptDialogsEnabled(TRUE); 100 Settings->put_IsScriptEnabled(TRUE);
99 Settings->put_IsWebMessageEnabled(TRUE); 101 Settings->put_AreDefaultScriptDialogsEnabled(TRUE);
102 Settings->put_IsWebMessageEnabled(TRUE);
100 #ifndef DEBUG 103 #ifndef DEBUG
101 Settings->put_AreDevToolsEnabled(FALSE); 104 Settings->put_AreDevToolsEnabled(FALSE);
102 #endif 105 #endif
106
107 // Save the token, we might need to dw_window_set_data() this value
108 // for later use to remove the handlers
109 EventRegistrationToken token;
110
111 // Register a handler for the NavigationStarting event.
112 webview->add_NavigationStarting(
113 Callback<ICoreWebView2NavigationStartingEventHandler>(
114 [hWnd](ICoreWebView2* sender,
115 ICoreWebView2NavigationStartingEventArgs* args) -> HRESULT
116 {
117 LPWSTR uri;
118 sender->get_Source(&uri);
119
120 _wndproc(hWnd, WM_USER + 101, (WPARAM)DW_INT_TO_POINTER(DW_HTML_CHANGE_STARTED),
121 !wcscmp(uri, L"about:blank") ? (LPARAM)"" : (LPARAM)WideToUTF8((LPWSTR)uri));
122
123 return S_OK;
124 }).Get(), &token);
125
126 // Register a handler for the SourceChanged event.
127 webview->add_SourceChanged(
128 Callback<ICoreWebView2SourceChangedEventHandler >(
129 [hWnd](ICoreWebView2* sender,
130 ICoreWebView2SourceChangedEventArgs* args) -> HRESULT
131 {
132 LPWSTR uri;
133 sender->get_Source(&uri);
134
135 _wndproc(hWnd, WM_USER + 101, (WPARAM)DW_INT_TO_POINTER(DW_HTML_CHANGE_REDIRECT),
136 !wcscmp(uri, L"about:blank") ? (LPARAM)"" : (LPARAM)WideToUTF8((LPWSTR)uri));
137
138 return S_OK;
139 }).Get(), &token);
140
141 // Register a handler for the ContentLoading event.
142 webview->add_ContentLoading(
143 Callback<ICoreWebView2ContentLoadingEventHandler >(
144 [hWnd](ICoreWebView2* sender,
145 ICoreWebView2ContentLoadingEventArgs* args) -> HRESULT
146 {
147 LPWSTR uri;
148 sender->get_Source(&uri);
149
150 _wndproc(hWnd, WM_USER + 101, (WPARAM)DW_INT_TO_POINTER(DW_HTML_CHANGE_LOADING),
151 !wcscmp(uri, L"about:blank") ? (LPARAM)"" : (LPARAM)WideToUTF8((LPWSTR)uri));
152
153 return S_OK;
154 }).Get(), &token);
155
156 // Register a handler for the NavigationCompleted event.
157 webview->add_NavigationCompleted(
158 Callback<ICoreWebView2NavigationCompletedEventHandler>(
159 [hWnd](ICoreWebView2* sender,
160 ICoreWebView2NavigationCompletedEventArgs* args) -> HRESULT
161 {
162 LPWSTR uri;
163 sender->get_Source(&uri);
164
165 _wndproc(hWnd, WM_USER + 101, (WPARAM)DW_INT_TO_POINTER(DW_HTML_CHANGE_COMPLETE),
166 !wcscmp(uri, L"about:blank") ? (LPARAM)"" : (LPARAM)WideToUTF8((LPWSTR)uri));
167
168 return S_OK;
169 }).Get(), &token);
170 }
103 171
104 // Resize WebView to fit the bounds of the parent window 172 // Resize WebView to fit the bounds of the parent window
105 WebView->DoSize(); 173 WebView->DoSize();
106
107 // Save the token, we might need to dw_window_set_data() this value
108 // for later use to remove the handlers
109 EventRegistrationToken token;
110
111 // Register a handler for the NavigationStarting event.
112 webview->add_NavigationStarting(
113 Callback<IWebView2NavigationStartingEventHandler>(
114 [hWnd](IWebView2WebView* sender,
115 IWebView2NavigationStartingEventArgs* args) -> HRESULT
116 {
117 LPWSTR uri;
118 sender->get_Source(&uri);
119
120 _wndproc(hWnd, WM_USER + 101, (WPARAM)DW_INT_TO_POINTER(DW_HTML_CHANGE_STARTED),
121 !wcscmp(uri, L"about:blank") ? (LPARAM)"" : (LPARAM)WideToUTF8((LPWSTR)uri));
122
123 return S_OK;
124 }).Get(), &token);
125
126 // Register a handler for the DocumentStateChanged event.
127 webview->add_DocumentStateChanged(
128 Callback<IWebView2DocumentStateChangedEventHandler>(
129 [hWnd](IWebView2WebView* sender,
130 IWebView2DocumentStateChangedEventArgs* args) -> HRESULT
131 {
132 LPWSTR uri;
133 sender->get_Source(&uri);
134
135 _wndproc(hWnd, WM_USER + 101, (WPARAM)DW_INT_TO_POINTER(DW_HTML_CHANGE_LOADING),
136 !wcscmp(uri, L"about:blank") ? (LPARAM)"" : (LPARAM)WideToUTF8((LPWSTR)uri));
137
138 return S_OK;
139 }).Get(), &token);
140
141 // Register a handler for the NavigationCompleted event.
142 webview->add_NavigationCompleted(
143 Callback<IWebView2NavigationCompletedEventHandler>(
144 [hWnd](IWebView2WebView* sender,
145 IWebView2NavigationCompletedEventArgs* args) -> HRESULT
146 {
147 LPWSTR uri;
148 sender->get_Source(&uri);
149
150 _wndproc(hWnd, WM_USER + 101, (WPARAM)DW_INT_TO_POINTER(DW_HTML_CHANGE_COMPLETE),
151 !wcscmp(uri, L"about:blank") ? (LPARAM)"" : (LPARAM)WideToUTF8((LPWSTR)uri));
152
153 return S_OK;
154 }).Get(), &token);
155 174
156 // Handle cached load requests due to delayed 175 // Handle cached load requests due to delayed
157 // loading of the edge webview contexts 176 // loading of the edge webview contexts
158 char *url = (char *)dw_window_get_data(hWnd, _DW_HTML_DATA_LOCATION); 177 char *url = (char *)dw_window_get_data(hWnd, _DW_HTML_DATA_LOCATION);
159 if (url) 178 if (url)
198 { 217 {
199 RECT bounds; 218 RECT bounds;
200 BOOL isVisible; 219 BOOL isVisible;
201 220
202 GetClientRect(hWnd, &bounds); 221 GetClientRect(hWnd, &bounds);
203 WebView->put_Bounds(bounds); 222 WebHost->put_Bounds(bounds);
204 WebView->get_IsVisible(&isVisible); 223 WebHost->get_IsVisible(&isVisible);
205 if(!isVisible) 224 if(!isVisible)
206 WebView->put_IsVisible(TRUE); 225 WebHost->put_IsVisible(TRUE);
207 } 226 }
208 227
209 BOOL EdgeBrowser::Detect(VOID) 228 BOOL EdgeBrowser::Detect(VOID)
210 { 229 {
211 CreateWebView2EnvironmentWithDetails(nullptr, nullptr, nullptr, 230 CreateCoreWebView2EnvironmentWithDetails(nullptr, nullptr, nullptr,
212 Callback<IWebView2CreateWebView2EnvironmentCompletedHandler>( 231 Callback<ICoreWebView2CreateCoreWebView2EnvironmentCompletedHandler>(
213 [this](HRESULT result, IWebView2Environment* env) -> HRESULT { 232 [this](HRESULT result, ICoreWebView2Environment* env) -> HRESULT {
214 // Successfully created Edge environment, return TRUE 233 // Successfully created Edge environment, return TRUE
215 Env = env; 234 Env = env;
216 return S_OK; 235 return S_OK;
217 }).Get()); 236 }).Get());
218 return Env ? TRUE : FALSE; 237 return Env ? TRUE : FALSE;
288 { 307 {
289 HWND thishwnd = hWnd; 308 HWND thishwnd = hWnd;
290 309
291 if (WebView) 310 if (WebView)
292 WebView->ExecuteScript(UTF8toWide(script), 311 WebView->ExecuteScript(UTF8toWide(script),
293 Callback<IWebView2ExecuteScriptCompletedHandler>( 312 Callback<ICoreWebView2ExecuteScriptCompletedHandler>(
294 [thishwnd, scriptdata](HRESULT error, PCWSTR result) -> HRESULT 313 [thishwnd, scriptdata](HRESULT error, PCWSTR result) -> HRESULT
295 { 314 {
296 char *scriptresult; 315 char *scriptresult;
297 316
298 /* Result is unquoted "null" when we should return NULL */ 317 /* Result is unquoted "null" when we should return NULL */
314 return S_OK; 333 return S_OK;
315 }).Get()); 334 }).Get());
316 return DW_ERROR_NONE; 335 return DW_ERROR_NONE;
317 } 336 }
318 337
319 VOID EdgeWebView::Setup(HWND hwnd, IWebView2WebView* webview) 338 VOID EdgeWebView::Setup(HWND hwnd, ICoreWebView2Host* host)
320 { 339 {
321 hWnd = hwnd; 340 hWnd = hwnd;
322 WebView = webview; 341 WebHost = host;
342 host->get_CoreWebView2(&WebView);
323 } 343 }
324 344
325 VOID EdgeWebView::Close(VOID) 345 VOID EdgeWebView::Close(VOID)
326 { 346 {
327 if (WebView) 347 if (WebHost)
328 WebView->Close(); 348 WebHost->Close();
329 } 349 }
330 350
331 EdgeBrowser *DW_EDGE = NULL; 351 EdgeBrowser *DW_EDGE = NULL;
332 352
333 extern "C" { 353 extern "C" {