WebClient.cpp 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022
  1. //
  2. // Copyright (c) 2014-2016, THUNDERBEAST GAMES LLC All rights reserved
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #ifdef ATOMIC_PLATFORM_WINDOWS
  23. #include <windows.h>
  24. #undef LoadString
  25. #endif
  26. #include <include/cef_app.h>
  27. #include <include/cef_client.h>
  28. #include <include/cef_browser.h>
  29. #include <include/wrapper/cef_helpers.h>
  30. #include <include/base/cef_bind.h>
  31. #include <include/wrapper/cef_closure_task.h>
  32. #include "include/wrapper/cef_message_router.h"
  33. #include <Atomic/Core/ProcessUtils.h>
  34. #include <Atomic/Core/CoreEvents.h>
  35. #include <Atomic/IO/Log.h>
  36. #include <Atomic/Input/Input.h>
  37. #include <Atomic/Graphics/Graphics.h>
  38. #include "Internal/WebAppBrowser.h"
  39. #include "WebBrowserHost.h"
  40. #include "WebMessageHandler.h"
  41. #include "WebClient.h"
  42. #include "WebKeyboard.h"
  43. #include "WebViewEvents.h"
  44. #include "WebString.h"
  45. #include <SDL/include/SDL.h>
  46. #ifdef ATOMIC_PLATFORM_LINUX
  47. #ifndef SDL_VIDEO_DRIVER_X11
  48. #define SDL_VIDEO_DRIVER_X11
  49. #endif
  50. #endif
  51. #include <ThirdParty/SDL/include/SDL_syswm.h>
  52. namespace Atomic
  53. {
  54. #ifdef ATOMIC_PLATFORM_OSX
  55. void* GetNSWindowContentView(void* window);
  56. #endif
  57. class WebClientPrivate : public CefClient,
  58. public CefLifeSpanHandler,
  59. public CefLoadHandler,
  60. public CefDisplayHandler,
  61. public CefRequestHandler,
  62. public CefKeyboardHandler
  63. {
  64. friend class WebClient;
  65. public:
  66. WebClientPrivate(WebClient* client)
  67. {
  68. webClient_ = client;
  69. webBrowserHost_ = webClient_->GetSubsystem<WebBrowserHost>();
  70. CefMessageRouterConfig config;
  71. config.js_query_function = WebBrowserHost::GetJSMessageQueryFunctionName().CString();
  72. config.js_cancel_function = WebBrowserHost::GetJSMessageQueryCancelFunctionName().CString();
  73. browserSideRouter_ = CefMessageRouterBrowserSide::Create(config);
  74. }
  75. virtual ~WebClientPrivate()
  76. {
  77. }
  78. CefRefPtr<CefRenderHandler> GetRenderHandler() OVERRIDE
  79. {
  80. if (webClient_.Null() || webClient_->renderHandler_.Null())
  81. return nullptr;
  82. return webClient_->renderHandler_->GetCEFRenderHandler();
  83. }
  84. CefRefPtr<CefLifeSpanHandler> GetLifeSpanHandler() OVERRIDE
  85. {
  86. return this;
  87. }
  88. CefRefPtr<CefLoadHandler> GetLoadHandler() OVERRIDE
  89. {
  90. return this;
  91. }
  92. CefRefPtr<CefDisplayHandler> GetDisplayHandler() OVERRIDE
  93. {
  94. return this;
  95. }
  96. CefRefPtr<CefRequestHandler> GetRequestHandler() OVERRIDE
  97. {
  98. return this;
  99. }
  100. CefRefPtr<CefKeyboardHandler> GetKeyboardHandler() OVERRIDE
  101. {
  102. return this;
  103. }
  104. // CefKeyboardHandler
  105. virtual bool OnPreKeyEvent(CefRefPtr<CefBrowser> browser,
  106. const CefKeyEvent& event,
  107. CefEventHandle os_event,
  108. bool* is_keyboard_shortcut) OVERRIDE
  109. {
  110. return false;
  111. }
  112. // CefRequestHandler methods
  113. void OnRenderViewReady(CefRefPtr<CefBrowser> browser) OVERRIDE
  114. {
  115. }
  116. bool OnBeforeBrowse(CefRefPtr<CefBrowser> browser,
  117. CefRefPtr<CefFrame> frame,
  118. CefRefPtr<CefRequest> request,
  119. bool is_redirect) OVERRIDE
  120. {
  121. CEF_REQUIRE_UI_THREAD();
  122. browserSideRouter_->OnBeforeBrowse(browser, frame);
  123. return false;
  124. }
  125. bool OnProcessMessageReceived(
  126. CefRefPtr<CefBrowser> browser,
  127. CefProcessId source_process,
  128. CefRefPtr<CefProcessMessage> message) OVERRIDE
  129. {
  130. CEF_REQUIRE_UI_THREAD();
  131. const CefString& message_name = message->GetName();
  132. if (message_name == "atomic_eval_javascript_result")
  133. {
  134. if (webClient_.Null())
  135. return false;
  136. unsigned evalID = (unsigned) message->GetArgumentList()->GetInt(0);
  137. bool result = message->GetArgumentList()->GetBool(1);
  138. String value;
  139. ConvertCEFString(message->GetArgumentList()->GetString(2), value);
  140. webClient_->EvalJavaScriptResult(evalID, result, value);
  141. return true;
  142. }
  143. if (browserSideRouter_->OnProcessMessageReceived(browser, source_process, message))
  144. {
  145. return true;
  146. }
  147. return false;
  148. }
  149. void OnRenderProcessTerminated(CefRefPtr<CefBrowser> browser,
  150. TerminationStatus status) OVERRIDE
  151. {
  152. CEF_REQUIRE_UI_THREAD();
  153. browserSideRouter_->OnRenderProcessTerminated(browser);
  154. }
  155. // CefLoadHandler
  156. void OnLoadStart(CefRefPtr<CefBrowser> browser,
  157. CefRefPtr<CefFrame> frame, TransitionType transition_type) OVERRIDE
  158. {
  159. if (webClient_.Null() || !frame->IsMain())
  160. return;
  161. VariantMap eventData;
  162. eventData[WebViewLoadStart::P_CLIENT] = webClient_;
  163. CefString cefURL = frame->GetURL();
  164. String url;
  165. ConvertCEFString(cefURL, url);
  166. eventData[WebViewLoadStart::P_URL] = url;
  167. webClient_->SendEvent(E_WEBVIEWLOADSTART, eventData);
  168. }
  169. void OnLoadEnd(CefRefPtr<CefBrowser> browser,
  170. CefRefPtr<CefFrame> frame,
  171. int httpStatusCode) OVERRIDE
  172. {
  173. if (webClient_.Null() || !frame->IsMain())
  174. return;
  175. VariantMap eventData;
  176. eventData[WebViewLoadEnd::P_CLIENT] = webClient_;
  177. CefString cefURL = frame->GetURL();
  178. String url;
  179. ConvertCEFString(cefURL, url);
  180. eventData[WebViewLoadEnd::P_URL] = url;
  181. webClient_->SendEvent(E_WEBVIEWLOADEND, eventData);
  182. }
  183. void OnLoadError(CefRefPtr<CefBrowser> browser,
  184. CefRefPtr<CefFrame> frame,
  185. ErrorCode errorCode,
  186. const CefString& errorText,
  187. const CefString& failedUrl) OVERRIDE
  188. {
  189. if (webClient_.Null())
  190. return;
  191. }
  192. void OnLoadingStateChange(CefRefPtr<CefBrowser> browser,
  193. bool isLoading,
  194. bool canGoBack,
  195. bool canGoForward) OVERRIDE
  196. {
  197. if (webClient_.Null())
  198. return;
  199. VariantMap eventData;
  200. eventData[WebViewLoadStateChange::P_CLIENT] = webClient_;
  201. eventData[WebViewLoadStateChange::P_LOADING] = isLoading;
  202. eventData[WebViewLoadStateChange::P_CANGOBACK] = canGoBack;
  203. eventData[WebViewLoadStateChange::P_CANGOFORWARD] = canGoForward;
  204. webClient_->SendEvent(E_WEBVIEWLOADSTATECHANGE, eventData);
  205. }
  206. // CefDisplayHandler
  207. void OnAddressChange(CefRefPtr<CefBrowser> browser,
  208. CefRefPtr<CefFrame> frame,
  209. const CefString& url) OVERRIDE
  210. {
  211. if (webClient_.Null() || !frame->IsMain())
  212. return;
  213. VariantMap eventData;
  214. eventData[WebViewAddressChange::P_CLIENT] = webClient_;
  215. String _url;
  216. ConvertCEFString(url, _url);
  217. eventData[WebViewAddressChange::P_URL] = _url;
  218. webClient_->SendEvent(E_WEBVIEWADDRESSCHANGE, eventData);
  219. }
  220. void OnTitleChange(CefRefPtr<CefBrowser> browser,
  221. const CefString& title) OVERRIDE
  222. {
  223. if (webClient_.Null())
  224. return;
  225. VariantMap eventData;
  226. eventData[WebViewTitleChange::P_CLIENT] = webClient_;
  227. String _title;
  228. ConvertCEFString(title, _title);
  229. eventData[WebViewTitleChange::P_TITLE] = _title;
  230. webClient_->SendEvent(E_WEBVIEWTITLECHANGE, eventData);
  231. }
  232. ///
  233. // Called to display a console message. Return true to stop the message from
  234. // being output to the console.
  235. ///
  236. /*--cef(optional_param=message,optional_param=source)--*/
  237. virtual bool OnConsoleMessage(CefRefPtr<CefBrowser> browser,
  238. const CefString& message,
  239. const CefString& source,
  240. int line) OVERRIDE
  241. {
  242. if (webClient_.Null())
  243. return false;
  244. String _message;
  245. ConvertCEFString(message, _message);
  246. String _source;
  247. ConvertCEFString(source, _source);
  248. ATOMIC_LOGINFOF("WebViewJS: %s (%s:%i)", _message.CString(), _source.CString(), line);
  249. return false;
  250. }
  251. bool CreateBrowser(const String& initialURL, int width, int height)
  252. {
  253. if (browser_.get())
  254. {
  255. ATOMIC_LOGERROR("WebClient::CreateBrowser - Browser already created");
  256. return false;
  257. }
  258. if (webClient_->renderHandler_.Null())
  259. {
  260. ATOMIC_LOGERROR("WebClient::CreateBrowser - No render handler specified");
  261. return false;
  262. }
  263. CefWindowInfo windowInfo;
  264. CefBrowserSettings browserSettings;
  265. browserSettings.webgl = STATE_ENABLED;
  266. browserSettings.file_access_from_file_urls = STATE_ENABLED;
  267. browserSettings.universal_access_from_file_urls = STATE_ENABLED;
  268. browserSettings.web_security = WebBrowserHost::GetWebSecurity() ? STATE_ENABLED : STATE_DISABLED;
  269. windowInfo.width = width;
  270. windowInfo.height = height;
  271. windowInfo.transparent_painting_enabled = 1;
  272. Graphics* graphics = webClient_->GetSubsystem<Graphics>();
  273. if (graphics)
  274. {
  275. SDL_Window* sdlWindow = static_cast<SDL_Window*>(graphics->GetSDLWindow());
  276. SDL_SysWMinfo info;
  277. SDL_VERSION(&info.version);
  278. if(SDL_GetWindowWMInfo(sdlWindow, &info))
  279. {
  280. #ifdef ATOMIC_PLATFORM_OSX
  281. NSView* view = (NSView*) GetNSWindowContentView(info.info.cocoa.window);
  282. windowInfo.SetAsWindowless(view, false);
  283. #endif
  284. #ifdef ATOMIC_PLATFORM_WINDOWS
  285. windowInfo.SetAsWindowless(info.info.win.window, /*transparent*/ true);
  286. #endif
  287. #ifdef ATOMIC_PLATFORM_LINUX
  288. if ( info.subsystem == SDL_SYSWM_X11 )
  289. windowInfo.SetAsWindowless(info.info.x11.window, true);
  290. #endif
  291. }
  292. }
  293. else
  294. {
  295. #ifndef ATOMIC_PLATFORM_LINUX
  296. // headless
  297. windowInfo.SetAsWindowless(nullptr, true);
  298. #endif
  299. }
  300. // TODO: There seems to be a CEF bug when loading a string into a browser
  301. // which was created with an empty URL, this workaround gets things going
  302. // NOTE: I also tried loading the string, delaying 5 seconds and still won't
  303. // load a string until a URL has been passed into the view
  304. String _initialURL = initialLoadString_.Length() ? "x" : initialURL;
  305. webClient_->renderHandler_->SetSize(width, height);
  306. CefRefPtr<CefBrowser> browser = CefBrowserHost::CreateBrowserSync(windowInfo, this,
  307. _initialURL.CString(), browserSettings, nullptr);
  308. if (!browser.get())
  309. return false;
  310. browser_ = browser;
  311. if (initialLoadString_.Length())
  312. {
  313. webClient_->LoadString(initialLoadString_, initialLoadStringURL_);
  314. }
  315. return true;
  316. }
  317. // CefLifeSpanHandler methods:
  318. virtual void OnAfterCreated(CefRefPtr<CefBrowser> browser) OVERRIDE
  319. {
  320. CEF_REQUIRE_UI_THREAD();
  321. }
  322. virtual bool DoClose(CefRefPtr<CefBrowser> browser) OVERRIDE
  323. {
  324. return false;
  325. }
  326. virtual void OnBeforeClose(CefRefPtr<CefBrowser> browser) OVERRIDE
  327. {
  328. CEF_REQUIRE_UI_THREAD();
  329. browser_ = nullptr;
  330. }
  331. virtual bool OnBeforePopup(CefRefPtr<CefBrowser> browser,
  332. CefRefPtr<CefFrame> frame,
  333. const CefString& target_url,
  334. const CefString& target_frame_name,
  335. CefLifeSpanHandler::WindowOpenDisposition target_disposition,
  336. bool user_gesture,
  337. const CefPopupFeatures& popupFeatures,
  338. CefWindowInfo& windowInfo,
  339. CefRefPtr<CefClient>& client,
  340. CefBrowserSettings& settings,
  341. bool* no_javascript_access) OVERRIDE
  342. {
  343. // Called on the IO thread, cancel and convert to popup request
  344. assert(!CefCurrentlyOn(TID_UI));
  345. // Execute on the UI thread.
  346. CefPostTask(TID_UI,
  347. base::Bind(&WebClientPrivate::OnPopupRequest, this, target_url));
  348. return true;
  349. }
  350. void OnPopupRequest(const CefString& target_url)
  351. {
  352. if (webClient_.Null())
  353. return;
  354. String url;
  355. ConvertCEFString(target_url, url);
  356. VariantMap eventData;
  357. eventData[WebViewPopupRequest::P_CLIENT] = webClient_;
  358. eventData[WebViewPopupRequest::P_URL] = url;
  359. webClient_->SendEvent(E_WEBVIEWPOPUPREQUEST, eventData);
  360. }
  361. void CloseBrowser(bool force_close)
  362. {
  363. if (!CefCurrentlyOn(TID_UI))
  364. {
  365. // Execute on the UI thread.
  366. CefPostTask(TID_UI,
  367. base::Bind(&WebClientPrivate::CloseBrowser, this, force_close));
  368. return;
  369. }
  370. if (!browser_.get())
  371. return;
  372. browser_->GetHost()->CloseBrowser(force_close);
  373. }
  374. void SetInitialLoadString(const String& loadString, const String& url)
  375. {
  376. initialLoadString_ = loadString;
  377. initialLoadStringURL_ = url;
  378. }
  379. IMPLEMENT_REFCOUNTING(WebClientPrivate);
  380. void ClearReferences()
  381. {
  382. browser_ = nullptr;
  383. webBrowserHost_ = nullptr;
  384. webClient_ = nullptr;
  385. browserSideRouter_ = nullptr;
  386. }
  387. private:
  388. String initialLoadString_;
  389. String initialLoadStringURL_;
  390. CefRefPtr<CefBrowser> browser_;
  391. WeakPtr<WebBrowserHost> webBrowserHost_;
  392. WeakPtr<WebClient> webClient_;
  393. CefRefPtr<CefMessageRouterBrowserSide> browserSideRouter_;
  394. };
  395. WebClient::WebClient(Context* context) : Object(context)
  396. {
  397. d_ = new WebClientPrivate(this);
  398. d_->AddRef();
  399. SubscribeToEvent(E_WEBVIEWGLOBALPROPERTIESCHANGED, ATOMIC_HANDLER(WebClient, HandleWebViewGlobalPropertiesChanged));
  400. }
  401. WebClient::~WebClient()
  402. {
  403. if (d_)
  404. {
  405. List<SharedPtr<WebMessageHandler>>::Iterator itr = messageHandlers_.Begin();
  406. while (itr != messageHandlers_.End())
  407. {
  408. CefMessageRouterBrowserSide::Handler* handler = static_cast<CefMessageRouterBrowserSide::Handler*>((*itr)->GetCefHandler());
  409. d_->browserSideRouter_->RemoveHandler(handler);
  410. itr++;
  411. }
  412. d_->CloseBrowser(true);
  413. d_->ClearReferences();
  414. d_->Release();
  415. }
  416. d_ = nullptr;
  417. renderHandler_ = 0;
  418. }
  419. void WebClient::SendMouseClickEvent(int x, int y, unsigned button, bool mouseUp, unsigned modifier, int clickCount) const
  420. {
  421. if (!d_->browser_.get())
  422. return;
  423. CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
  424. CefMouseEvent mevent;
  425. mevent.x = x;
  426. mevent.y = y;
  427. mevent.modifiers = 0;
  428. //MBT_LEFT = 0,
  429. //MBT_MIDDLE,
  430. //MBT_RIGHT,
  431. host->SendMouseClickEvent(mevent, (CefBrowserHost::MouseButtonType) button, mouseUp, clickCount);
  432. }
  433. void WebClient::SendMousePressEvent(int x, int y, unsigned button, unsigned modifier, int clickCount) const
  434. {
  435. SendMouseClickEvent(x, y, button, false, modifier, clickCount);
  436. SendMouseClickEvent(x, y, button, true, modifier, clickCount);
  437. }
  438. void WebClient::SendMouseMoveEvent(int x, int y, unsigned modifier, bool mouseLeave) const
  439. {
  440. if (!d_->browser_.get())
  441. return;
  442. CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
  443. CefMouseEvent mevent;
  444. mevent.x = x;
  445. mevent.y = y;
  446. mevent.modifiers = 0;
  447. Input* input = GetSubsystem<Input>();
  448. if (input->GetMouseButtonDown(MOUSEB_LEFT))
  449. mevent.modifiers |= EVENTFLAG_LEFT_MOUSE_BUTTON;
  450. if (input->GetMouseButtonDown(MOUSEB_MIDDLE))
  451. mevent.modifiers |= EVENTFLAG_MIDDLE_MOUSE_BUTTON;
  452. if (input->GetMouseButtonDown(MOUSEB_RIGHT))
  453. mevent.modifiers |= EVENTFLAG_RIGHT_MOUSE_BUTTON;
  454. host->SendMouseMoveEvent(mevent, mouseLeave);
  455. }
  456. void WebClient::SendMouseWheelEvent(int x, int y, unsigned modifier,int deltaX, int deltaY) const
  457. {
  458. if (!d_->browser_.get())
  459. return;
  460. CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
  461. CefMouseEvent mevent;
  462. mevent.x = x;
  463. mevent.y = y;
  464. mevent.modifiers = 0;
  465. deltaY = -deltaY * 5;
  466. #ifndef ATOMIC_PLATFORM_OSX
  467. deltaY *= 5;
  468. #endif
  469. host->SendMouseWheelEvent(mevent, deltaX, deltaY);
  470. }
  471. /*
  472. EVENTFLAG_CAPS_LOCK_ON = 1 << 0,
  473. EVENTFLAG_SHIFT_DOWN = 1 << 1,
  474. EVENTFLAG_CONTROL_DOWN = 1 << 2,
  475. EVENTFLAG_ALT_DOWN = 1 << 3,
  476. EVENTFLAG_LEFT_MOUSE_BUTTON = 1 << 4,
  477. EVENTFLAG_MIDDLE_MOUSE_BUTTON = 1 << 5,
  478. EVENTFLAG_RIGHT_MOUSE_BUTTON = 1 << 6,
  479. // Mac OS-X command key.
  480. EVENTFLAG_COMMAND_DOWN = 1 << 7,
  481. EVENTFLAG_NUM_LOCK_ON = 1 << 8,
  482. EVENTFLAG_IS_KEY_PAD = 1 << 9,
  483. EVENTFLAG_IS_LEFT = 1 << 10,
  484. EVENTFLAG_IS_RIGHT = 1 << 11,
  485. } cef_event_flags_t;
  486. */
  487. void WebClient::SendKeyEvent(const StringHash eventType, VariantMap& eventData)
  488. {
  489. if (!d_->browser_.get())
  490. return;
  491. CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
  492. CefKeyEvent keyEvent;
  493. if (!ConvertKeyEvent(GetSubsystem<Input>(), eventType, eventData, keyEvent))
  494. return;
  495. host->SendKeyEvent(keyEvent);
  496. #ifdef ATOMIC_PLATFORM_WINDOWS
  497. // RETURN KEY: We need to send both keydown and char for return key
  498. // this allows it to be used both to confirm entry on popups,
  499. // while also being used for text input
  500. if (keyEvent.windows_key_code == 13)
  501. {
  502. keyEvent.type = KEYEVENT_CHAR;
  503. host->SendKeyEvent(keyEvent);
  504. }
  505. #endif
  506. #ifdef ATOMIC_PLATFORM_OSX
  507. // RETURN KEY: We need to send both keydown and char for return key
  508. // this allows it to be used both to confirm entry on popups,
  509. // while also being used for text input
  510. if (keyEvent.native_key_code == 36)
  511. {
  512. keyEvent.type = KEYEVENT_CHAR;
  513. host->SendKeyEvent(keyEvent);
  514. }
  515. // Send an empty key event on OSX, which seems to fix
  516. // keyboard problems on OSX with cefclient
  517. // ./cefclient --off-screen-rendering-enabled
  518. // return does not work at all on cef client with offscreen
  519. // bad interaction with arrow keys (for example here, after
  520. // hitting arrow keys, return/text takes a couple presses to register
  521. memset((void*)&keyEvent, 0, sizeof(keyEvent));
  522. keyEvent.type = KEYEVENT_KEYDOWN;
  523. keyEvent.modifiers = 0;
  524. keyEvent.native_key_code = 0;
  525. host->SendKeyEvent(keyEvent);
  526. #endif
  527. #ifdef ATOMIC_PLATFORM_LINUX
  528. if (keyEvent.windows_key_code == 0x0D)
  529. {
  530. keyEvent.type = KEYEVENT_CHAR;
  531. host->SendKeyEvent(keyEvent);
  532. }
  533. #endif
  534. }
  535. void WebClient::SendTextInputEvent(const StringHash eventType, VariantMap& eventData)
  536. {
  537. if (!d_->browser_.get())
  538. return;
  539. CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
  540. CefKeyEvent keyEvent;
  541. if (!ConvertTextInputEvent(eventType, eventData, keyEvent))
  542. return;
  543. host->SendKeyEvent(keyEvent);
  544. }
  545. void WebClient::SendFocusEvent(bool focus)
  546. {
  547. if (!d_->browser_.get())
  548. return;
  549. CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
  550. host->SendFocusEvent(focus);
  551. }
  552. // Javascript
  553. void WebClient::ExecuteJavaScript(const String& script)
  554. {
  555. if (!d_->browser_.get())
  556. return;
  557. d_->browser_->GetMainFrame()->ExecuteJavaScript(CefString(script.CString()), "", 0);
  558. }
  559. void WebClient::EvalJavaScript(unsigned evalID, const String& script)
  560. {
  561. if (!d_->browser_.get())
  562. return;
  563. // Create the message object.
  564. CefRefPtr<CefProcessMessage> msg= CefProcessMessage::Create("atomic_eval_javascript");
  565. // Retrieve the argument list object.
  566. CefRefPtr<CefListValue> args = msg->GetArgumentList();
  567. // Populate the argument values.
  568. args->SetInt(0, (int) evalID);
  569. args->SetString(1, CefString(script.CString()));
  570. // Send the process message to the render process.
  571. // Use PID_BROWSER instead when sending a message to the browser process.
  572. d_->browser_->SendProcessMessage(PID_RENDERER, msg);
  573. }
  574. void WebClient::EvalJavaScriptResult(unsigned evalID, bool result, const String& value)
  575. {
  576. using namespace WebViewJSEvalResult;
  577. VariantMap eventData;
  578. eventData[P_CLIENT] = this;
  579. eventData[P_EVALID] = evalID;
  580. eventData[P_RESULT] = result;
  581. eventData[P_VALUE] = value;
  582. SendEvent(E_WEBVIEWJSEVALRESULT, eventData);
  583. }
  584. void WebClient::AddMessageHandler(WebMessageHandler* handler, bool first)
  585. {
  586. SharedPtr<WebMessageHandler> _handler(handler);
  587. if (handler->GetWebClient())
  588. {
  589. ATOMIC_LOGWARNING("WebClient::AddMessageHandler - message handler already added to another client");
  590. return;
  591. }
  592. if (messageHandlers_.Contains(_handler))
  593. {
  594. ATOMIC_LOGWARNING("WebClient::AddMessageHandler - message handler already added to this client");
  595. return;
  596. }
  597. _handler->SetWebClient(this);
  598. messageHandlers_.Push(_handler);
  599. d_->browserSideRouter_->AddHandler(static_cast<CefMessageRouterBrowserSide::Handler*>(handler->GetCefHandler()), first);
  600. }
  601. void WebClient::RemoveMessageHandler(WebMessageHandler* handler)
  602. {
  603. SharedPtr<WebMessageHandler> _handler(handler);
  604. List<SharedPtr<WebMessageHandler>>::Iterator itr = messageHandlers_.Find(_handler);
  605. if (itr == messageHandlers_.End())
  606. {
  607. ATOMIC_LOGWARNING("WebClient::RemoveMessageHandler - message handler not found");
  608. return;
  609. }
  610. d_->browserSideRouter_->RemoveHandler(static_cast<CefMessageRouterBrowserSide::Handler*>(handler->GetCefHandler()));
  611. messageHandlers_.Erase(itr);
  612. }
  613. // Navigation
  614. void WebClient::LoadURL(const String& url)
  615. {
  616. if (!d_->browser_.get())
  617. {
  618. return;
  619. }
  620. CefString _url(url.CString());
  621. d_->browser_->GetMainFrame()->LoadURL(_url);
  622. }
  623. void WebClient::LoadString(const String& source, const String& url)
  624. {
  625. if (!d_->browser_.get())
  626. {
  627. d_->SetInitialLoadString(source, url);
  628. return;
  629. }
  630. // We need to make sure global properties are updated when loading web content from source string
  631. // This is handled differently internally then we requests
  632. UpdateGlobalProperties();
  633. d_->browser_->GetMainFrame()->LoadString(source.CString(), url.CString());
  634. }
  635. void WebClient::GoBack()
  636. {
  637. if (!d_->browser_.get())
  638. return;
  639. d_->browser_->GoBack();
  640. }
  641. void WebClient::GoForward()
  642. {
  643. if (!d_->browser_.get())
  644. return;
  645. d_->browser_->GoForward();
  646. }
  647. bool WebClient::IsLoading()
  648. {
  649. if (!d_->browser_.get())
  650. return false;
  651. return d_->browser_->IsLoading();
  652. }
  653. void WebClient::Reload()
  654. {
  655. if (!d_->browser_.get())
  656. return;
  657. d_->browser_->Reload();
  658. }
  659. void WebClient::ShortcutCut()
  660. {
  661. if (!d_->browser_.get())
  662. return;
  663. d_->browser_->GetFocusedFrame()->Cut();
  664. }
  665. void WebClient::ShortcutCopy()
  666. {
  667. if (!d_->browser_.get())
  668. return;
  669. d_->browser_->GetFocusedFrame()->Copy();
  670. }
  671. void WebClient::ShortcutPaste()
  672. {
  673. if (!d_->browser_.get())
  674. return;
  675. d_->browser_->GetFocusedFrame()->Paste();
  676. }
  677. void WebClient::ShortcutSelectAll()
  678. {
  679. if (!d_->browser_.get())
  680. return;
  681. d_->browser_->GetFocusedFrame()->SelectAll();
  682. }
  683. void WebClient::ShortcutUndo()
  684. {
  685. if (!d_->browser_.get())
  686. return;
  687. d_->browser_->GetFocusedFrame()->Undo();
  688. }
  689. void WebClient::ShortcutRedo()
  690. {
  691. if (!d_->browser_.get())
  692. return;
  693. d_->browser_->GetFocusedFrame()->Redo();
  694. }
  695. void WebClient::ShortcutDelete()
  696. {
  697. if (!d_->browser_.get())
  698. return;
  699. d_->browser_->GetFocusedFrame()->Delete();
  700. }
  701. void WebClient::WasResized()
  702. {
  703. if (!d_->browser_.get())
  704. return;
  705. CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
  706. host->WasResized();;
  707. }
  708. bool WebClient::CreateBrowser(const String& initialURL, int width, int height)
  709. {
  710. bool result = d_->CreateBrowser(initialURL, width, height);
  711. return result;
  712. }
  713. void WebClient::UpdateGlobalProperties()
  714. {
  715. if (!d_->browser_.get())
  716. return;
  717. CefRefPtr<CefDictionaryValue> globalProps;
  718. if (!WebAppBrowser::CreateGlobalProperties(globalProps))
  719. return;
  720. // Create the message object.
  721. CefRefPtr<CefProcessMessage> msg = CefProcessMessage::Create("atomic_set_globalproperties");
  722. // Retrieve the argument list object.
  723. CefRefPtr<CefListValue> args = msg->GetArgumentList();
  724. args->SetDictionary(0, globalProps);
  725. // Send the process message to the render process.
  726. if (!d_->browser_->SendProcessMessage(PID_RENDERER, msg))
  727. {
  728. ATOMIC_LOGERROR("WebClient::UpdateGlobalProperties - Failed to send message");
  729. }
  730. }
  731. void WebClient::HandleWebViewGlobalPropertiesChanged(StringHash eventType, VariantMap& eventData)
  732. {
  733. UpdateGlobalProperties();
  734. }
  735. void WebClient::SetSize(int width, int height)
  736. {
  737. if (renderHandler_.Null())
  738. return;
  739. if (renderHandler_->GetWidth() == width && renderHandler_->GetHeight() == height)
  740. return;
  741. renderHandler_->SetSize(width, height);
  742. WasResized();
  743. }
  744. void WebClient::SetZoomLevel(float zoomLevel)
  745. {
  746. if (!d_->browser_.get())
  747. return;
  748. d_->browser_->GetHost()->SetZoomLevel(zoomLevel);
  749. }
  750. void WebClient::SetWebRenderHandler(WebRenderHandler* handler)
  751. {
  752. handler->SetWebClient(this);
  753. renderHandler_ = handler;
  754. }
  755. CefClient* WebClient::GetCefClient()
  756. {
  757. return d_;
  758. }
  759. }