Project

General

Profile

SO3Engine
SO3EmbeddedWebNavigatorClient.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
28#if SO3_WEB_NAVIGATOR_BUILD == 1
29#include "include/wrapper/cef_helpers.h"
30
31namespace SO3
32{
33 namespace EmbeddedWebNavigator
34 {
35
36 WebNavigatorClient::WebNavigatorClient(WebNavigator* parentWebNavigatorInstance, const ScolWindowHandle& scolMainWindow, bool offscreen) :
37 parentWebNavigator(parentWebNavigatorInstance),
38 scolMainWindowHandle(scolMainWindow)
39 {
40 displayHandler = new WebNavigatorDisplayHandler(CefRefPtr<WebNavigatorClient>(const_cast <WebNavigatorClient*> (this)), scolMainWindowHandle);
41 keyboardHandler = new WebNavigatorKeyboardHandler(CefRefPtr<WebNavigatorClient>(const_cast <WebNavigatorClient*> (this)), scolMainWindowHandle);
42 loadHandler = new WebNavigatorLoadHandler(CefRefPtr<WebNavigatorClient>(const_cast <WebNavigatorClient*> (this)), scolMainWindowHandle);
43 renderHandler = new WebNavigatorRenderHandler(CefRefPtr<WebNavigatorClient>(const_cast <WebNavigatorClient*> (this)), scolMainWindowHandle, offscreen);
44
45 cefBrowser = 0;
46 }
47
48 WebNavigatorClient::WebNavigatorClient() : parentWebNavigator(0),
49 scolMainWindowHandle(0)
50 {
51 // Forbiden ctor.
52 }
53
58
59 bool WebNavigatorClient::BlitOffscreenBitmap(unsigned char* dest, int destWidth, int destHeight, int destBytesPerPixel, int destBitsPerLine)
60 {
61 return renderHandler->BlitOffscreenBitmap(dest, destWidth, destHeight, destBytesPerPixel, destBitsPerLine);
62 }
63
64 bool WebNavigatorClient::BlitOffscreenAlphaBitmap(unsigned char* destColor, unsigned char* destAlpha, int destWidth, int destHeight, int destColorBytesPerPixel, int destColorBitsPerLine)
65 {
66 return renderHandler->BlitOffscreenAlphaBitmap(destColor, destAlpha, destWidth, destHeight, destColorBytesPerPixel, destColorBitsPerLine);
67 }
68
69 bool WebNavigatorClient::BlitOffscreenTexture(Ogre::TexturePtr& ogreTexture, bool forceUpdate)
70 {
71 return renderHandler->BlitOffscreenTexture(ogreTexture, forceUpdate);
72 }
73
74 bool WebNavigatorClient::CheckPixelAlpha(int posX, int posY, float transparentTresholdColor)
75 {
76 return renderHandler->CheckPixelAlpha(posX, posY, transparentTresholdColor);
77 }
78
80 {
81 parentWebNavigator = 0;
82
83 if (displayHandler.get())
84 displayHandler->_Cleanup();
85
86 if (keyboardHandler.get())
87 keyboardHandler->_Cleanup();
88
89 if (loadHandler.get())
90 loadHandler->_Cleanup();
91
92 if (renderHandler.get())
93 renderHandler->_Cleanup();
94
95 if (cefBrowser)
96 {
97 cefBrowser->GetHost()->CloseBrowser(true);
98 cefBrowser = 0;
99 }
100 }
101
102 CefRefPtr<CefBrowser> WebNavigatorClient::GetBrowser()
103 {
104 return cefBrowser;
105 }
106
108 {
109 return parentWebNavigator;
110 }
111
112 bool WebNavigatorClient::OnBeforePopup(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, const CefString& target_url, const CefString& target_frame_name, CefLifeSpanHandler::WindowOpenDisposition target_disposition, bool user_gesture, const CefPopupFeatures& popupFeatures, CefWindowInfo& windowInfo, CefRefPtr<CefClient>& client, CefBrowserSettings& settings, bool* no_javascript_access)
113 {
114 std::unique_lock< std::shared_mutex > lock(WebNavigatorManager::GetSingletonPtr()->webMessageQueueCriticalSection);
115 WebNavigatorManager::GetSingletonPtr()->mainThreadWebMessageQueue.post(std::bind(&WebNavigatorManager::InvokeBeforePopup, WebNavigatorManager::GetSingletonPtr(), parentWebNavigator->GetUniqueId(), popupFeatures.x, popupFeatures.y, popupFeatures.width, popupFeatures.height, target_url));
116
117 // Allways cancel popup creation, if we want it, then it's up to the scol developper to create a new web navigator with the given informations.
118 return true;
119 }
120
121 void WebNavigatorClient::OnAfterCreated(CefRefPtr<CefBrowser> browser)
122 {
123 CEF_REQUIRE_UI_THREAD();
124 AutoLock lock_scope(this);
125
126 // Register the newly created browser in our WebNavigatorClient object.
127 cefBrowser = browser;
128
129 //force the navigator zoom to 0 or it will take the last zoom value
130 cefBrowser->GetHost()->SetZoomLevel(0.0f);
131
132 // Set initial position and size
133 if (parentWebNavigator != 0)
134 {
135 parentWebNavigator->SetPosition(parentWebNavigator->GetPosition());
136 parentWebNavigator->SetSize(parentWebNavigator->GetSize());
137 }
138 }
139
140 bool WebNavigatorClient::DoClose(CefRefPtr<CefBrowser> browser)
141 {
142 // WARNING! DO NOT SEND MESSAGE TO SCOL HERE
143 // Because when the scol main thread will handle the reflexive call, the WebNavigator and Cef::Browser object will already have been destroyed!
144 return false;
145 }
146
147 void WebNavigatorClient::OnBeforeClose(CefRefPtr<CefBrowser> browser)
148 {
149 CEF_REQUIRE_UI_THREAD();
150 AutoLock lock_scope(this);
151
152 // Unregister the browser from our WebNavigatorClient object.
153 displayHandler = 0;
154 keyboardHandler = 0;
155 loadHandler = 0;
156 renderHandler = 0;
157 }
158
159 bool WebNavigatorClient::OnProcessMessageReceived(CefRefPtr<CefBrowser> browser, CefProcessId source_process, CefRefPtr<CefProcessMessage> message)
160 {
161 CEF_REQUIRE_UI_THREAD();
162
163 if (!message->IsValid())
164 return false;
165
166 if (parentWebNavigator)
167 {
168 std::vector<std::string> scolParameters;
169 CefRefPtr<CefListValue> arguments = message->GetArgumentList();
170 std::string funname;
171
172 //scol function name is first parameter
173 if (arguments->GetSize() >= 1)
174 {
175 funname = arguments->GetValue(0)->GetString();
176
177 if (arguments->GetSize() > 1)
178 for (unsigned int currentArg = 1; currentArg < arguments->GetSize(); currentArg++)
179 {
180 CefRefPtr<CefValue> arg = arguments->GetValue(currentArg);
181 scolParameters.push_back(arg->GetString());
182 }
183 }
184
185 // Create function object
186 SScriptFunction functionCalled(funname, scolParameters);
187 if (message->GetName() == "scolExternalCall")
188 {
189 std::unique_lock< std::shared_mutex > lock(WebNavigatorManager::GetSingletonPtr()->webMessageQueueCriticalSection);
191 }
192 else if (message->GetName() == "scolExternalCallStr")
193 {
194 std::unique_lock< std::shared_mutex > lock(WebNavigatorManager::GetSingletonPtr()->webMessageQueueCriticalSection);
196 }
197 }
198
199 return true;
200 }
201
202 void WebNavigatorClient::OnBeforeContextMenu(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, CefRefPtr<CefContextMenuParams> params, CefRefPtr<CefMenuModel> model)
203 {
204 CEF_REQUIRE_UI_THREAD();
205 model->Clear();
206 }
207
208 bool WebNavigatorClient::OnContextMenuCommand(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, CefRefPtr<CefContextMenuParams> params, int command_id, CefContextMenuHandler::EventFlags event_flags)
209 {
210 CEF_REQUIRE_UI_THREAD();
211 return false;
212 }
213 }
214}
215
216#endif
bool BlitOffscreenAlphaBitmap(unsigned char *destColor, unsigned char *destAlpha, int destWidth, int destHeight, int destColorBytesPerPixel, int destColorBitsPerLine)
virtual bool OnContextMenuCommand(CefRefPtr< CefBrowser > browser, CefRefPtr< CefFrame > frame, CefRefPtr< CefContextMenuParams > params, int command_id, CefContextMenuHandler::EventFlags event_flags) OVERRIDE
virtual void OnBeforeContextMenu(CefRefPtr< CefBrowser > browser, CefRefPtr< CefFrame > frame, CefRefPtr< CefContextMenuParams > params, CefRefPtr< CefMenuModel > model) OVERRIDE
virtual bool OnProcessMessageReceived(CefRefPtr< CefBrowser > browser, CefProcessId source_process, CefRefPtr< CefProcessMessage > message) OVERRIDE
virtual bool DoClose(CefRefPtr< CefBrowser > browser) OVERRIDE
bool CheckPixelAlpha(int posX, int posY, float transparentTresholdColor)
virtual bool OnBeforePopup(CefRefPtr< CefBrowser > browser, CefRefPtr< CefFrame > frame, const CefString &target_url, const CefString &target_frame_name, CefLifeSpanHandler::WindowOpenDisposition target_disposition, bool user_gesture, const CefPopupFeatures &popupFeatures, CefWindowInfo &windowInfo, CefRefPtr< CefClient > &client, CefBrowserSettings &settings, bool *no_javascript_access) OVERRIDE
bool BlitOffscreenTexture(Ogre::TexturePtr &ogreTexture, bool forceUpdate)
virtual void OnBeforeClose(CefRefPtr< CefBrowser > browser) OVERRIDE
bool BlitOffscreenBitmap(unsigned char *dest, int destWidth, int destHeight, int destBytesPerPixel, int destBitsPerLine)
virtual void OnAfterCreated(CefRefPtr< CefBrowser > browser) OVERRIDE
void SetSize(std::pair< int, int > newSize)
void SetPosition(std::pair< int, int > newPosition)
void InvokeExternalCall(unsigned long uniqueId, SScriptFunction functionCalled)
void InvokeExternalCallStr(unsigned long uniqueId, SScriptFunction functionCalled)
void InvokeBeforePopup(unsigned long uniqueId, int popupX, int popupY, int popupWidth, int popupHeight, std::string popupUrl)