Project

General

Profile

SO3Engine
SO3EmbeddedWebNavigatorManager.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
27#if SO3_WEB_NAVIGATOR_BUILD == 1
28#include "include/cef_app.h"
29#include "include/cef_cookie.h"
30#include "include/cef_task.h"
31
32namespace SO3
33{
34 namespace EmbeddedWebNavigator
35 {
36 // Instanciate static member
37 WebNavigatorManager* WebNavigatorManager::thisInstance = 0;
38
39 WebNavigatorManager::WebNavigatorManager(const ScolWindowHandle& scolMainWindow, std::string cacheDirectory) : scolMainWindowHandle(scolMainWindow)
40 {
41 // Initialise singleton first
42 assert(!WebNavigatorManager::thisInstance);
43 WebNavigatorManager::thisInstance = this;
44 nextUniqueId = 1; // 0 is reserved id
45
46 sCachePath = cacheDirectory;
47 std::replace(sCachePath.begin(), sCachePath.end(), '/', '\\');
48
49 // Create synchro events
50 synchroEventSetCookie = CreateEvent(NULL, TRUE, FALSE, NULL);
51 synchroEventDeleteCookie = CreateEvent(NULL, TRUE, FALSE, NULL);
52
53 bInitialized = false;
54 bContextInitialized = false;
55 }
56
57 WebNavigatorManager::WebNavigatorManager() : scolMainWindowHandle(0)
58 {
59 // Forbiden
60 }
61
63 {
64 // Destroy synchro events
65 CloseHandle(synchroEventSetCookie);
66 synchroEventSetCookie = 0;
67 CloseHandle(synchroEventDeleteCookie);
68 synchroEventDeleteCookie = 0;
69
70 // Destroy all the remaining WebNavigator instances
71 WebNavigatorList webNavigatorListCopy = webNavigatorList;
72 WebNavigatorList::iterator iWebNavigator = webNavigatorListCopy.begin();
73 if (iWebNavigator != webNavigatorListCopy.end())
74 {
75 DestroyNavigator(iWebNavigator->second);
76 iWebNavigator++;
77 }
78
79 // Reset singleton
80 assert(WebNavigatorManager::thisInstance);
81 WebNavigatorManager::thisInstance = 0;
82 }
83
85 {
86 bContextInitialized = true;
87 }
88
89 void WebNavigatorManager::init()
90 {
91 // Enable High-DPI support on Windows 7 or newer.
92 CefEnableHighDPISupport();
93
94 // Specify a cache path value.
95 CefSettings settings;
96 CefString(&settings.cache_path).FromString(sCachePath);
97 CefString(&settings.log_file).FromString(sCachePath + "\\libcef.log");
98
99 char exePath[512];
100 std::string strexePath;
101 GetModuleFileName(NULL, exePath, 512);
102 strexePath = exePath;
103 strexePath = strexePath.substr(0, strexePath.find_last_of('\\'));
104
105 CefString(&settings.resources_dir_path).FromString(strexePath + "\\plugins\\cefres");
106 CefString(&settings.locales_dir_path).FromString(strexePath + "\\plugins\\cefres\\locales");
107
108 // Use the English language locale.
109 CefString(&settings.locale).FromASCII("en-US");
110
111#ifdef NDEBUG
112 // Only log error messages and higher in release build.
113 settings.log_severity = LOGSEVERITY_ERROR;
114#endif
115
116 // processed using a separate UI thread.
117 settings.multi_threaded_message_loop = true;
118 settings.windowless_rendering_enabled = true;
119 settings.ignore_certificate_errors = true;
120 settings.no_sandbox = true;
121
122 // Init CEF.
123 CefMainArgs args;
124 CefInitialize(args, settings, this, NULL);
125 bInitialized = true;
126 }
127
129 {
130 assert(WebNavigatorManager::thisInstance);
131 return *WebNavigatorManager::thisInstance;
132 }
133
135 {
136 return WebNavigatorManager::thisInstance;
137 }
138
140 {
141 bool cefkill = (WebNavigatorManager::thisInstance && WebNavigatorManager::thisInstance->IsInitialized()) ? true : false;
142 SAFE_DELETE(WebNavigatorManager::thisInstance);
143
144 if (cefkill)
145 CefShutdown();
146 }
147
149 {
150 return bInitialized;
151 }
152
153 WebNavigator* WebNavigatorManager::CreateNavigator(const ScolWindowHandle& parentWindowHandle, int xPosition, int yPosition, int width, int height, std::string url)
154 {
155 if (!bInitialized)
156 init();
157
158 while (!bContextInitialized)
159 std::this_thread::sleep_for(std::chrono::milliseconds(1));
160
161 // Create it.
162 WebNavigator* newWebNavigator = new WebNavigator(nextUniqueId, scolMainWindowHandle, parentWindowHandle, xPosition, yPosition, width, height, url);
163
164 // Store a reference to the newly created web navigator.
165 webNavigatorList.insert(WebNavigatorList::value_type(nextUniqueId, newWebNavigator));
166 nextUniqueId++;
167 return newWebNavigator;
168 }
169
170 WebNavigator* WebNavigatorManager::CreateNavigator(const ScolWindowHandle& parentWindowHandle, int width, int height, std::string url, bool transparency)
171 {
172 if (!bInitialized)
173 init();
174
175 while (!bContextInitialized)
176 std::this_thread::sleep_for(std::chrono::milliseconds(1));
177
178 // Create it.
179 WebNavigator* newWebNavigator = new WebNavigator(nextUniqueId, scolMainWindowHandle, parentWindowHandle, width, height, url, transparency);
180
181 // Store a reference to the newly created web navigator.
182 webNavigatorList.insert(WebNavigatorList::value_type(nextUniqueId, newWebNavigator));
183 nextUniqueId++;
184 return newWebNavigator;
185 }
186
188 {
189 if (webNavigator)
190 {
191 //to be sure this never happend on update
192 std::unique_lock< std::shared_mutex > lock(webMessageQueueCriticalSection);
193
194 // Check if a web navigator instance with this parent window exist.
195 WebNavigatorList::iterator iWebNavigatorSearched = webNavigatorList.find(webNavigator->GetUniqueId());
196 if (iWebNavigatorSearched != webNavigatorList.end())
197 {
198 // Erase from first list
199 WebNavigator* webNavigator = iWebNavigatorSearched->second;
200 webNavigatorList.erase(iWebNavigatorSearched);
201
202 // Delete web navigator instance
203 delete(webNavigator);
204 }
205 }
206 }
207
209 {
210 WebNavigatorList::iterator iSearchedNavigator = webNavigatorList.find(uniqueId);
211 if (iSearchedNavigator != webNavigatorList.end())
212 return iSearchedNavigator->second;
213 else
214 return 0;
215 }
216
217 bool WebNavigatorManager::SetCookie(const std::string& url, const std::string& name, const std::string& value, const std::string& domain, const std::string& path, const bool& secure, const bool& httponly)
218 {
219 bool success = false;
220 CefCookie newCookie;
221 CefString(&newCookie.name).FromString(name);
222 CefString(&newCookie.value).FromString(value);
223 CefString(&newCookie.domain).FromString(domain);
224 CefString(&newCookie.path).FromString(path);
225 newCookie.secure = secure;
226 newCookie.httponly = httponly;
227
228 //
229 ResetEvent(synchroEventSetCookie);
230 CefPostTask(TID_IO, base::Bind(&WebNavigatorThread::SetCookie, url, newCookie, &success, synchroEventSetCookie));
231 WaitForSingleObject(synchroEventSetCookie, WEB_NAVIGATOR_SYNCHRO_DEFAULT_WAITING_TIME);
232 return success;
233 }
234
235 bool WebNavigatorManager::DeleteCookies(const std::string& url, const std::string& cookie_name)
236 {
237 bool success = false;
238 ResetEvent(synchroEventDeleteCookie);
239 CefPostTask(TID_IO, base::Bind(&WebNavigatorThread::DeleteCookies, url, cookie_name, &success, synchroEventDeleteCookie));
240 WaitForSingleObject(synchroEventDeleteCookie, WEB_NAVIGATOR_SYNCHRO_DEFAULT_WAITING_TIME);
241 return success;
242 }
243
245 {
246 if (bInitialized && bContextInitialized)
247 {
248 std::unique_lock< std::shared_mutex > lock(webMessageQueueCriticalSection);
250 }
251 }
252
253 void WebNavigatorManager::InvokeExternalCall(unsigned long uniqueId, SScriptFunction functionCalled)
254 {
255 WebNavigator* targetWebNavigator = GetNavigator(uniqueId);
256 if (targetWebNavigator != 0)
257 if (targetWebNavigator->GetListener() != 0)
258 targetWebNavigator->GetListener()->OnExternalCall(targetWebNavigator, functionCalled);
259 }
260
261 void WebNavigatorManager::InvokeExternalCallStr(unsigned long uniqueId, SScriptFunction functionCalled)
262 {
263 WebNavigator* targetWebNavigator = GetNavigator(uniqueId);
264 if (targetWebNavigator != 0)
265 if (targetWebNavigator->GetListener() != 0)
266 targetWebNavigator->GetListener()->OnExternalCallStr(targetWebNavigator, functionCalled);
267 }
268
269 void WebNavigatorManager::InvokeNavStateChange(unsigned long uniqueId, bool canGoBack, bool canGoForward)
270 {
271 WebNavigator* targetWebNavigator = GetNavigator(uniqueId);
272 if (targetWebNavigator != 0)
273 {
274 if (targetWebNavigator->GetListener() != 0)
275 targetWebNavigator->GetListener()->OnNavStateChange(targetWebNavigator, canGoBack, canGoForward);
276 }
277 }
278
279 void WebNavigatorManager::InvokeAddressChange(unsigned long uniqueId, const std::string& frameName, const std::string& url)
280 {
281 WebNavigator* targetWebNavigator = GetNavigator(uniqueId);
282 if (targetWebNavigator != 0)
283 {
284 if (targetWebNavigator->GetListener() != 0)
285 targetWebNavigator->GetListener()->OnAddressChange(targetWebNavigator, frameName, url);
286 }
287 }
288
289 void WebNavigatorManager::InvokeTitleChange(unsigned long uniqueId, const std::string& title)
290 {
291 WebNavigator* targetWebNavigator = GetNavigator(uniqueId);
292 if (targetWebNavigator != 0)
293 {
294 if (targetWebNavigator->GetListener() != 0)
295 targetWebNavigator->GetListener()->OnTitleChange(targetWebNavigator, title);
296 }
297 }
298
299 void WebNavigatorManager::InvokeTooltip(unsigned long uniqueId, std::string& text)
300 {
301 WebNavigator* targetWebNavigator = GetNavigator(uniqueId);
302 if (targetWebNavigator != 0)
303 {
304 if (targetWebNavigator->GetListener() != 0)
305 targetWebNavigator->GetListener()->OnTooltip(targetWebNavigator, text);
306 }
307 }
308
309 void WebNavigatorManager::InvokeCursorChange(unsigned long uniqueId, HCURSOR newCursor)
310 {
311 WebNavigator* targetWebNavigator = GetNavigator(uniqueId);
312 if (targetWebNavigator != 0)
313 {
314 if (targetWebNavigator->GetListener() != 0)
315 targetWebNavigator->GetListener()->OnCursorChange(targetWebNavigator, newCursor);
316 }
317 }
318
319 void WebNavigatorManager::InvokeStatusMessage(unsigned long uniqueId, const std::string& value)
320 {
321 WebNavigator* targetWebNavigator = GetNavigator(uniqueId);
322 if (targetWebNavigator != 0)
323 {
324 if (targetWebNavigator->GetListener() != 0)
325 targetWebNavigator->GetListener()->OnStatusMessage(targetWebNavigator, value);
326 }
327 }
328
329 void WebNavigatorManager::InvokeConsoleMessage(unsigned long uniqueId, const std::string& message, const std::string& source, int line)
330 {
331 WebNavigator* targetWebNavigator = GetNavigator(uniqueId);
332 if (targetWebNavigator != 0)
333 {
334 if (targetWebNavigator->GetListener() != 0)
335 targetWebNavigator->GetListener()->OnConsoleMessage(targetWebNavigator, message, source, line);
336 }
337 }
338
339 void WebNavigatorManager::InvokeBeforePopup(unsigned long uniqueId, int popupX, int popupY, int popupWidth, int popupHeight, std::string popupUrl)
340 {
341 WebNavigator* targetWebNavigator = GetNavigator(uniqueId);
342 if (targetWebNavigator != 0)
343 {
344 if (targetWebNavigator->GetListener() != 0)
345 targetWebNavigator->GetListener()->OnBeforePopup(targetWebNavigator, popupX, popupY, popupWidth, popupHeight, popupUrl);
346 }
347 }
348
349 void WebNavigatorManager::InvokeRunModal(unsigned long uniqueId)
350 {
351 WebNavigator* targetWebNavigator = GetNavigator(uniqueId);
352 if (targetWebNavigator != 0)
353 {
354 if (targetWebNavigator->GetListener() != 0)
355 targetWebNavigator->GetListener()->RunModal(targetWebNavigator);
356 }
357 }
358
359 void WebNavigatorManager::InvokeLoadStart(unsigned long uniqueId, const std::string& frameName)
360 {
361 WebNavigator* targetWebNavigator = GetNavigator(uniqueId);
362 if (targetWebNavigator != 0)
363 {
364 if (targetWebNavigator->GetListener() != 0)
365 targetWebNavigator->GetListener()->OnLoadStart(targetWebNavigator, frameName);
366 }
367 }
368
369 void WebNavigatorManager::InvokeLoadEnd(unsigned long uniqueId, const std::string& frameName, int httpStatusCode)
370 {
371 WebNavigator* targetWebNavigator = GetNavigator(uniqueId);
372 if (targetWebNavigator != 0)
373 {
374 if (targetWebNavigator->GetListener() != 0)
375 targetWebNavigator->GetListener()->OnLoadEnd(targetWebNavigator, frameName, httpStatusCode);
376 }
377 }
378
379 void WebNavigatorManager::InvokeLoadError(unsigned long uniqueId, const std::string& frameName, int errorCode, const std::string& failedUrl, std::string& errorText)
380 {
381 WebNavigator* targetWebNavigator = GetNavigator(uniqueId);
382 if (targetWebNavigator != 0)
383 {
384 if (targetWebNavigator->GetListener() != 0)
385 targetWebNavigator->GetListener()->OnLoadError(targetWebNavigator, frameName, errorCode, failedUrl, errorText);
386 }
387 }
388
389 void WebNavigatorManager::InvokeFindResult(unsigned long uniqueId, int identifier, int count, const CefRect& selectionRect, int activeMatchOrdinal, bool finalUpdate)
390 {
391 WebNavigator* targetWebNavigator = GetNavigator(uniqueId);
392 if (targetWebNavigator != 0)
393 {
394 if (targetWebNavigator->GetListener() != 0)
395 targetWebNavigator->GetListener()->OnFindResult(targetWebNavigator, identifier, count, selectionRect, activeMatchOrdinal, finalUpdate);
396 }
397 }
398 }
399}
400
401#endif
void * HCURSOR
Definition SO3Android.h:60
virtual void OnLoadEnd(WebNavigator *sourceWebNavigator, const std::string &frameName, int httpStatusCode)
virtual void OnConsoleMessage(WebNavigator *sourceWebNavigator, const std::string &message, const std::string &source, int line)
virtual void RunModal(WebNavigator *sourceWebNavigator)
virtual void OnExternalCall(WebNavigator *sourceWebNavigator, const SScriptFunction &functionCalled)
virtual void OnLoadError(WebNavigator *sourceWebNavigator, const std::string &frameName, int errorCode, const std::string &failedUrl, std::string &errorText)
virtual void OnAddressChange(WebNavigator *sourceWebNavigator, const std::string &frameName, const std::string &url)
virtual void OnCursorChange(WebNavigator *sourceWebNavigator, HCURSOR newCursor)
virtual void OnExternalCallStr(WebNavigator *sourceWebNavigator, const SScriptFunction &functionCalled)
virtual void OnLoadStart(WebNavigator *sourceWebNavigator, const std::string &frameName)
virtual void OnStatusMessage(WebNavigator *sourceWebNavigator, const std::string &value)
virtual void OnTooltip(WebNavigator *sourceWebNavigator, std::string &text)
virtual void OnBeforePopup(WebNavigator *sourceWebNavigator, int popupX, int popupY, int popupWidth, int popupHeight, std::string popupUrl)
virtual void OnTitleChange(WebNavigator *sourceWebNavigator, const std::string &title)
virtual void OnNavStateChange(WebNavigator *sourceWebNavigator, bool canGoBack, bool canGoForward)
virtual void OnFindResult(WebNavigator *sourceWebNavigator, int identifier, int count, const CefRect &selectionRect, int activeMatchOrdinal, bool finalUpdate)
void InvokeStatusMessage(unsigned long uniqueId, const std::string &value)
bool DeleteCookies(const std::string &url, const std::string &cookie_name)
void InvokeTooltip(unsigned long uniqueId, std::string &text)
std::map< unsigned long, WebNavigator * > WebNavigatorList
void InvokeNavStateChange(unsigned long uniqueId, bool canGoBack, bool canGoForward)
void InvokeConsoleMessage(unsigned long uniqueId, const std::string &message, const std::string &source, int line)
void InvokeExternalCall(unsigned long uniqueId, SScriptFunction functionCalled)
void InvokeLoadEnd(unsigned long uniqueId, const std::string &frameName, int httpStatusCode)
void InvokeLoadError(unsigned long uniqueId, const std::string &frameName, int errorCode, const std::string &failedUrl, std::string &errorText)
WebNavigator * CreateNavigator(const ScolWindowHandle &parentWindowHandle, int xPosition, int yPosition, int width, int height, std::string url)
void InvokeCursorChange(unsigned long uniqueId, HCURSOR newCursor)
void InvokeExternalCallStr(unsigned long uniqueId, SScriptFunction functionCalled)
void InvokeTitleChange(unsigned long uniqueId, const std::string &title)
void InvokeFindResult(unsigned long uniqueId, int identifier, int count, const CefRect &selectionRect, int activeMatchOrdinal, bool finalUpdate)
void InvokeLoadStart(unsigned long uniqueId, const std::string &frameName)
bool SetCookie(const std::string &url, const std::string &name, const std::string &value, const std::string &domain, const std::string &path, const bool &secure, const bool &httponly)
void InvokeBeforePopup(unsigned long uniqueId, int popupX, int popupY, int popupWidth, int popupHeight, std::string popupUrl)
void InvokeAddressChange(unsigned long uniqueId, const std::string &frameName, const std::string &url)
static void DeleteCookies(std::string url, std::string cookie_name, bool *result, HANDLE synchro)
static void SetCookie(std::string url, CefCookie cookie, bool *result, HANDLE synchro)