Project

General

Profile

SO3Engine
SO3EmbeddedWebNavigator.cpp
Go to the documentation of this file.
1/*
2-----------------------------------------------------------------------------
3This source file is part of OpenSpace3D
4For the latest info, see http://www.openspace3d.com
5
6Copyright (c) 2011 I-maginer
7
8This program is free software; you can redistribute it and/or modify it under
9the terms of the GNU Lesser General Public License as published by the Free Software
10Foundation; either version 2 of the License, or (at your option) any later
11version.
12
13This program is distributed in the hope that it will be useful, but WITHOUT
14ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
16
17You should have received a copy of the GNU Lesser General Public License along with
18this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19Place - Suite 330, Boston, MA 02111-1307, USA, or go to
20http://www.gnu.org/copyleft/lesser.txt
21-----------------------------------------------------------------------------
22*/
23
26#if SO3_WEB_NAVIGATOR_BUILD == 1
27#include "include/cef_task.h"
28#include "include/wrapper/cef_helpers.h"
29
30namespace SO3
31{
32 namespace EmbeddedWebNavigator
33 {
34 WebNavigator::WebNavigator(const unsigned long navigatorUniqueId, const ScolWindowHandle& scolMainWindow, const ScolWindowHandle& parentWindowHandleInstance, int xPosition, int yPosition, int width, int height, std::string url) : uniqueId(navigatorUniqueId),
35 scolMainWindowHandle(scolMainWindow),
36 parentWindowHandle(parentWindowHandleInstance),
37 mOffscreen(false),
38 mTransparency(false)
39 {
40 CommonConstructorSequence(std::pair<int, int>(xPosition, yPosition), std::pair<int, int>(width, height), url);
41 }
42
43 WebNavigator::WebNavigator(const unsigned long navigatorUniqueId, const ScolWindowHandle& scolMainWindow, const ScolWindowHandle& parentWindowHandleInstance, int width, int height, std::string url, bool transparency) : uniqueId(navigatorUniqueId),
44 scolMainWindowHandle(scolMainWindow),
45 parentWindowHandle(parentWindowHandleInstance),
46 mOffscreen(true),
47 mTransparency(transparency)
48 {
49 CommonConstructorSequence(std::pair<int, int>(0, 0), std::pair<int, int>(width, height), url);
50 }
51
52 void WebNavigator::CommonConstructorSequence(std::pair<int, int> newWebNavigatorPosition, std::pair<int, int> newWebNavigatorSize, std::string url)
53 {
54 // Init member variables
55 actualPosition = newWebNavigatorPosition;
56 actualSize = newWebNavigatorSize;
57 listener = 0;
58
59 // Create the CEF client instance
60 webNavigatorClient = new WebNavigatorClient(this, scolMainWindowHandle, mOffscreen);
61
62 // CEF browser init structures
63 CefWindowInfo windowInfo;
64 CefBrowserSettings settings;
65
66 // Setting up windows hierarchy
67 if (mOffscreen)
68 {
69 // Set as off-screen rendering window.
70 windowInfo.SetAsWindowless(parentWindowHandle);
71 }
72 else
73 {
74 // Set as child browser window
75 RECT clientArea;
76 GetClientRect(parentWindowHandle, &clientArea);
77 windowInfo.SetAsChild(parentWindowHandle, clientArea);
78 }
79
80 // Enable transparency
81 if (!mTransparency)
82 settings.background_color = CefColorSetARGB(255, 255, 255, 255);
83
84 // Create synchronisation objects
85 synchroEventCanGoBack = CreateEvent(NULL, TRUE, FALSE, NULL);
86 synchroEventCanGoForward = CreateEvent(NULL, TRUE, FALSE, NULL);
87 synchroEventGetZoomLevel = CreateEvent(NULL, TRUE, FALSE, NULL);
88 synchroEventGetSource = CreateEvent(NULL, TRUE, FALSE, NULL);
89 synchroEventGetURL = CreateEvent(NULL, TRUE, FALSE, NULL);
90 synchroEventGetText = CreateEvent(NULL, TRUE, FALSE, NULL);
91
92 // Create the browser. Will call WebNavigatorClient::OnAfterCreated
93 CefBrowserHost::CreateBrowser(windowInfo, webNavigatorClient.get(), url, settings, NULL);
94 }
95
96 WebNavigator::WebNavigator() : uniqueId(0),
97 scolMainWindowHandle(0),
98 parentWindowHandle(0),
99 mOffscreen(false),
100 mTransparency(false)
101 {
102 // Forbiden constructor
103 }
104
106 {
107 // Delete CEF client instance
108 webNavigatorClient->_Cleanup();
109 webNavigatorClient = 0;
110
111 // Delete synchronisation objects
112 CloseHandle(synchroEventCanGoBack);
113 synchroEventCanGoBack = 0;
114 CloseHandle(synchroEventCanGoForward);
115 synchroEventCanGoForward = 0;
116 CloseHandle(synchroEventGetZoomLevel);
117 synchroEventGetZoomLevel = 0;
118 CloseHandle(synchroEventGetSource);
119 synchroEventGetSource = 0;
120 CloseHandle(synchroEventGetURL);
121 synchroEventGetURL = 0;
122 CloseHandle(synchroEventGetText);
123 synchroEventGetText = 0;
124 }
125
127 {
128 return uniqueId;
129 }
130
131 CefRefPtr<WebNavigatorClient>& WebNavigator::GetWebNavigatorClient()
132 {
133 return webNavigatorClient;
134 }
135
137 {
138 return parentWindowHandle;
139 }
140
141 void WebNavigator::LoadURL(const std::string& url, const std::string& frameName)
142 {
143 CefPostTask(TID_UI, base::Bind(&WebNavigatorThread::LoadURL, uniqueId, url, frameName));
144 }
145
146 void WebNavigator::LoadFile(const std::string& file, const std::string& frameName)
147 {
148 CefPostTask(TID_UI, base::Bind(&WebNavigatorThread::LoadFile, uniqueId, file, frameName));
149 }
150
151 void WebNavigator::LoadHTML(const std::string& html, const std::string& frameName)
152 {
153 CefPostTask(TID_UI, base::Bind(&WebNavigatorThread::LoadHTML, uniqueId, html, frameName));
154 }
155
157 {
158 ResetEvent(synchroEventCanGoBack);
159 bool canGoBack = false;
160 CefPostTask(TID_UI, base::Bind(&WebNavigatorThread::CanGoBack, uniqueId, &canGoBack, synchroEventCanGoBack));
161 WaitForSingleObject(synchroEventCanGoBack, WEB_NAVIGATOR_SYNCHRO_DEFAULT_WAITING_TIME);
162 return canGoBack;
163 }
164
166 {
167 CefPostTask(TID_UI, base::Bind(&WebNavigatorThread::GoBack, uniqueId));
168 }
169
171 {
172 ResetEvent(synchroEventCanGoForward);
173 bool canGoForward = false;
174 CefPostTask(TID_UI, base::Bind(&WebNavigatorThread::CanGoForward, uniqueId, &canGoForward, synchroEventCanGoForward));
175 WaitForSingleObject(synchroEventCanGoForward, WEB_NAVIGATOR_SYNCHRO_DEFAULT_WAITING_TIME);
176 return canGoForward;
177 }
178
180 {
181 CefPostTask(TID_UI, base::Bind(&WebNavigatorThread::GoForward, uniqueId));
182 }
183
185 {
186 CefPostTask(TID_UI, base::Bind(&WebNavigatorThread::Reload, uniqueId));
187 }
188
190 {
191 CefPostTask(TID_UI, base::Bind(&WebNavigatorThread::ReloadIgnoreCache, uniqueId));
192 }
193
195 {
196 CefPostTask(TID_UI, base::Bind(&WebNavigatorThread::StopLoad, uniqueId));
197 }
198
199 void WebNavigator::Find(int identifier, const CefString& searchText, bool forward, bool matchCase, bool findNext)
200 {
201 CefPostTask(TID_UI, base::Bind(&WebNavigatorThread::Find, uniqueId, identifier, searchText, forward, matchCase, findNext));
202 }
203
204 void WebNavigator::StopFinding(bool clearSelection)
205 {
206 CefPostTask(TID_UI, base::Bind(&WebNavigatorThread::StopFinding, uniqueId, clearSelection));
207 }
208
210 {
211 ResetEvent(synchroEventGetZoomLevel);
212 double zoomFactor = 0.0;
213 CefPostTask(TID_UI, base::Bind(&WebNavigatorThread::GetZoomLevel, uniqueId, &zoomFactor, synchroEventGetZoomLevel));
214 WaitForSingleObject(synchroEventGetZoomLevel, WEB_NAVIGATOR_SYNCHRO_DEFAULT_WAITING_TIME);
215 return zoomFactor;
216 }
217
218 void WebNavigator::SetZoomLevel(double zoomLevel)
219 {
220 CefPostTask(TID_UI, base::Bind(&WebNavigatorThread::SetZoomLevel, uniqueId, zoomLevel));
221 }
222
224 {
225 CefPostTask(TID_UI, base::Bind(&WebNavigatorThread::ClearHistory, uniqueId));
226 }
227
229 {
230 CefPostTask(TID_UI, base::Bind(&WebNavigatorThread::ShowDevTools, uniqueId));
231 }
232
234 {
235 CefPostTask(TID_UI, base::Bind(&WebNavigatorThread::CloseDevTools, uniqueId));
236 }
237
238 std::pair<int, int> WebNavigator::GetSize()
239 {
240 return actualSize;
241 }
242
243 void WebNavigator::SetSize(std::pair<int, int> newSize)
244 {
245 actualSize = newSize;
246 ApplyPositionAndSize();
247 }
248
249 std::pair<int, int> WebNavigator::GetPosition()
250 {
251 return actualPosition;
252 }
253
254 void WebNavigator::SetPosition(std::pair<int, int> newPosition)
255 {
256 actualPosition = newPosition;
257 ApplyPositionAndSize();
258 }
259
260 void WebNavigator::ApplyPositionAndSize()
261 {
262 CefRefPtr<CefBrowser> cefBrowser = webNavigatorClient->GetBrowser();
263 if (cefBrowser.get())
264 {
265 if (!mOffscreen)
266 {
267 CefWindowHandle cefBrowserWindowHandle = cefBrowser->GetHost()->GetWindowHandle();
268 if (cefBrowserWindowHandle)
269 {
270 RECT clientArea;
271 GetClientRect(parentWindowHandle, &clientArea);
272 SetWindowPos(cefBrowserWindowHandle, 0,
273 clientArea.left + actualPosition.first, clientArea.top + actualPosition.second,
274 actualSize.first, actualSize.second, SWP_NOZORDER);
275 }
276 }
277 else
278 {
279 // Set the view size to match the render size.
280 cefBrowser->GetHost()->WasResized();
281 }
282 }
283 }
284
285 std::string WebNavigator::GetURL(const std::string& frameName)
286 {
287 ResetEvent(synchroEventGetURL);
288 std::string url = "";
289 CefPostTask(TID_UI, base::Bind(&WebNavigatorThread::GetURL, uniqueId, &url, synchroEventGetURL, frameName));
290 WaitForSingleObject(synchroEventGetURL, WEB_NAVIGATOR_SYNCHRO_DEFAULT_WAITING_TIME);
291 return url;
292 }
293
294 void WebNavigator::Undo(const std::string& frameName)
295 {
296 CefPostTask(TID_UI, base::Bind(&WebNavigatorThread::Undo, uniqueId, frameName));
297 }
298
299 void WebNavigator::Redo(const std::string& frameName)
300 {
301 CefPostTask(TID_UI, base::Bind(&WebNavigatorThread::Redo, uniqueId, frameName));
302 }
303
304 void WebNavigator::Cut(const std::string& frameName)
305 {
306 CefPostTask(TID_UI, base::Bind(&WebNavigatorThread::Cut, uniqueId, frameName));
307 }
308
309 void WebNavigator::Copy(const std::string& frameName)
310 {
311 CefPostTask(TID_UI, base::Bind(&WebNavigatorThread::Copy, uniqueId, frameName));
312 }
313
314 void WebNavigator::Paste(const std::string& frameName)
315 {
316 CefPostTask(TID_UI, base::Bind(&WebNavigatorThread::Paste, uniqueId, frameName));
317 }
318
319 void WebNavigator::Delete(const std::string& frameName)
320 {
321 CefPostTask(TID_UI, base::Bind(&WebNavigatorThread::Delete, uniqueId, frameName));
322 }
323
324 void WebNavigator::SelectAll(const std::string& frameName)
325 {
326 CefPostTask(TID_UI, base::Bind(&WebNavigatorThread::SelectAll, uniqueId, frameName));
327 }
328
329 void WebNavigator::Print(const std::string& frameName)
330 {
331 CefPostTask(TID_UI, base::Bind(&WebNavigatorThread::Print, uniqueId, frameName));
332 }
333
334 void WebNavigator::ViewSource(const std::string& frameName)
335 {
336 CefPostTask(TID_UI, base::Bind(&WebNavigatorThread::ViewSource, uniqueId, frameName));
337 }
338
339 std::string WebNavigator::GetSource(const std::string& frameName)
340 {
341 ResetEvent(synchroEventGetSource);
342 std::string sourceText = "";
343 CefPostTask(TID_UI, base::Bind(&WebNavigatorThread::GetSource, uniqueId, &sourceText, synchroEventGetSource, frameName));
344 WaitForSingleObject(synchroEventGetSource, WEB_NAVIGATOR_SYNCHRO_DEFAULT_WAITING_TIME);
345 return sourceText;
346 }
347
348 std::string WebNavigator::GetText(const std::string& frameName)
349 {
350 ResetEvent(synchroEventGetText);
351 std::string text = "";
352 CefPostTask(TID_UI, base::Bind(&WebNavigatorThread::GetText, uniqueId, &text, synchroEventGetText, frameName));
353 WaitForSingleObject(synchroEventGetText, WEB_NAVIGATOR_SYNCHRO_DEFAULT_WAITING_TIME);
354 return text;
355 }
356
357 void WebNavigator::ExecuteJavaScript(const std::string& jsCode, const std::string& scriptUrl, int startLine, const std::string& frameName)
358 {
359 CefPostTask(TID_UI, base::Bind(&WebNavigatorThread::ExecuteJavaScript, uniqueId, jsCode, scriptUrl, startLine, frameName));
360 }
361
363 {
364 return mOffscreen;
365 }
366
367 void WebNavigator::InjectKeyMsg(int msg, LPARAM lparam, WPARAM wparam)
368 {
369 CefRefPtr<CefBrowser> cefBrowser = webNavigatorClient->GetBrowser();
370 if (cefBrowser.get())
371 {
372 CefKeyEvent event;
373 event.windows_key_code = wparam;
374 event.native_key_code = lparam;
375 event.is_system_key = msg == WM_SYSCHAR || msg == WM_SYSKEYDOWN || msg == WM_SYSKEYUP;
376
377 if (msg == WM_KEYDOWN || msg == WM_SYSKEYDOWN)
378 event.type = KEYEVENT_RAWKEYDOWN;
379 else if (msg == WM_KEYUP || msg == WM_SYSKEYUP)
380 event.type = KEYEVENT_KEYUP;
381 else
382 event.type = KEYEVENT_CHAR;
383 event.modifiers = GetCefKeyboardModifiers(wparam, lparam);
384
385 cefBrowser->GetHost()->SendKeyEvent(event);
386 }
387 }
388
389 void WebNavigator::InjectKey(int scanCode, int keydata, bool down)
390 {
391 CefRefPtr<CefBrowser> cefBrowser = webNavigatorClient->GetBrowser();
392 CefKeyEvent event;
393 event.is_system_key = false;
394 event.modifiers = 0;
395
396 BYTE VkCode = LOBYTE(VkKeyScanA(scanCode));
397 event.native_key_code = MapVirtualKey(scanCode, 1) << 16 | 1;
398 event.windows_key_code = VkCode;
399 event.type = KEYEVENT_KEYDOWN;
400
401 if (!down)
402 {
403 event.native_key_code |= 0xC0000000;
404 event.type = KEYEVENT_KEYUP;
405 }
406
407 if (cefBrowser.get())
408 cefBrowser->GetHost()->SendKeyEvent(event);
409 }
410
411 void WebNavigator::InjectChar(int scanCode, int keydata)
412 {
413 CefRefPtr<CefBrowser> cefBrowser = webNavigatorClient->GetBrowser();
414 CefKeyEvent event;
415 event.is_system_key = false;
416 event.modifiers = 0;
417
418 BYTE VkCode = LOBYTE(VkKeyScanA(keydata));
419 event.native_key_code = keydata;
420 event.windows_key_code = VkCode;
421 event.type = KEYEVENT_CHAR;
422
423 if (cefBrowser.get())
424 cefBrowser->GetHost()->SendKeyEvent(event);
425 }
426
427 void WebNavigator::InjectMouseClick(int x, int y, int buttonType, bool mouseUp, int clickCount)
428 {
429 CefRefPtr<CefBrowser> cefBrowser = webNavigatorClient->GetBrowser();
430 CefMouseEvent event;
431 event.x = x;
432 event.y = y;
433 event.modifiers = 0;
434
435 if (cefBrowser.get())
436 cefBrowser->GetHost()->SendMouseClickEvent(event, ConvertMouseButton(buttonType), mouseUp, clickCount);
437 }
438
439 void WebNavigator::InjectMouseMove(int x, int y, bool mouseLeave)
440 {
441 CefRefPtr<CefBrowser> cefBrowser = webNavigatorClient->GetBrowser();
442 CefMouseEvent event;
443 event.x = x;
444 event.y = y;
445 event.modifiers = 0;
446
447 if (cefBrowser.get())
448 {
449 cefBrowser->GetHost()->SendMouseMoveEvent(event, mouseLeave);
450 //cefBrowser->GetHost()->Invalidate(PET_VIEW);
451 }
452 }
453
454 void WebNavigator::InjectMouseWheel(int x, int y, int delta)
455 {
456 CefRefPtr<CefBrowser> cefBrowser = webNavigatorClient->GetBrowser();
457 CefMouseEvent event;
458 event.x = x;
459 event.y = y;
460 event.modifiers = 0;
461
462 if (cefBrowser.get())
463 cefBrowser->GetHost()->SendMouseWheelEvent(event, 0, delta * 100);
464 }
465
466 void WebNavigator::InjectFocus(bool setFocus)
467 {
468 CefRefPtr<CefBrowser> cefBrowser = webNavigatorClient->GetBrowser();
469 if (cefBrowser.get())
470 cefBrowser->GetHost()->SendFocusEvent(setFocus);
471 }
472
473 CefBrowserHost::MouseButtonType WebNavigator::ConvertMouseButton(int scolButton)
474 {
475 // Cef button type values are 0/1/2 and scol are 1/16/2
476 switch (scolButton)
477 {
478 case 1:
479 return MBT_LEFT;
480 break;
481 case 16:
482 return MBT_MIDDLE;
483 break;
484 case 2:
485 return MBT_RIGHT;
486 break;
487 default:
488 return MBT_LEFT;
489 }
490 }
491
493 {
494 listener = newListener;
495 }
496
498 {
499 return listener;
500 }
501 }
502}
503
504#endif
SCOL_EXPORT void SCOL_PTR_TYPE WPARAM LPARAM lparam
Definition SO3SCOL.cpp:5130
SCOL_EXPORT void SCOL_PTR_TYPE WPARAM wparam
Definition SO3SCOL.cpp:5128
SCOL_EXPORT void SCOL_PTR_TYPE event
Definition SO3SCOL.cpp:5128
void Cut(const std::string &frameName="")
void InjectMouseClick(int x, int y, int buttonType, bool mouseUp, int clickCount)
void LoadFile(const std::string &file, const std::string &frameName="")
std::string GetText(const std::string &frameName="")
void Delete(const std::string &frameName="")
void Redo(const std::string &frameName="")
void Undo(const std::string &frameName="")
std::string GetSource(const std::string &frameName="")
void SetSize(std::pair< int, int > newSize)
void ViewSource(const std::string &frameName="")
std::string GetURL(const std::string &frameName="")
void SelectAll(const std::string &frameName="")
void LoadURL(const std::string &url, const std::string &frameName="")
void SetPosition(std::pair< int, int > newPosition)
void InjectKeyMsg(int msg, LPARAM lparam, WPARAM wparam)
void InjectMouseMove(int x, int y, bool mouseLeave)
void Find(int identifier, const CefString &searchText, bool forward, bool matchCase, bool findNext)
void Print(const std::string &frameName="")
CefRefPtr< WebNavigatorClient > & GetWebNavigatorClient()
void Paste(const std::string &frameName="")
void SetListener(WebNavigatorListener *newListener)
void LoadHTML(const std::string &html, const std::string &frameName="")
void ExecuteJavaScript(const std::string &jsCode, const std::string &scriptUrl, int startLine, const std::string &frameName="")
void Copy(const std::string &frameName="")
void InjectKey(int scanCode, int keydata, bool down)
static void Delete(unsigned long webNavigatorId, const std::string frameName="")
static void ClearHistory(unsigned long webNavigatorId)
static void Find(unsigned long webNavigatorId, int identifier, const CefString searchText, bool forward, bool matchCase, bool findNext)
static void ReloadIgnoreCache(unsigned long webNavigatorId)
static void GetSource(unsigned long webNavigatorId, std::string *frameSource, HANDLE synchro, const std::string frameName="")
static void CanGoBack(unsigned long webNavigatorId, bool *canGoBack, HANDLE synchro)
static void Cut(unsigned long webNavigatorId, const std::string frameName="")
static void CloseDevTools(unsigned long webNavigatorId)
static void GoForward(unsigned long webNavigatorId)
static void LoadHTML(unsigned long webNavigatorId, const std::string html, const std::string frameName="")
static void LoadFile(unsigned long webNavigatorId, const std::string file, const std::string frameName="")
static void LoadURL(unsigned long webNavigatorId, const std::string url, const std::string frameName="")
static void Print(unsigned long webNavigatorId, const std::string frameName="")
static void SelectAll(unsigned long webNavigatorId, const std::string frameName="")
static void StopFinding(unsigned long webNavigatorId, bool clearSelection)
static void CanGoForward(unsigned long webNavigatorId, bool *canGoForward, HANDLE synchro)
static void Copy(unsigned long webNavigatorId, const std::string frameName="")
static void GetZoomLevel(unsigned long webNavigatorId, double *zoomFactor, HANDLE synchro)
static void GetURL(unsigned long webNavigatorId, std::string *frameUrl, HANDLE synchro, const std::string frameName="")
static void Undo(unsigned long webNavigatorId, const std::string frameName="")
static void GetText(unsigned long webNavigatorId, std::string *frameText, HANDLE synchro, const std::string frameName="")
static void ShowDevTools(unsigned long webNavigatorId)
static void Redo(unsigned long webNavigatorId, const std::string frameName="")
static void ViewSource(unsigned long webNavigatorId, const std::string frameName="")
static void Paste(unsigned long webNavigatorId, const std::string frameName="")
static void ExecuteJavaScript(unsigned long webNavigatorId, const std::string jsCode, const std::string scriptUrl, int startLine, const std::string frameName="")
static void SetZoomLevel(unsigned long webNavigatorId, double zoomLevel)