WebClient.cpp 25 KB

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