WebClient.cpp 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015
  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 = WebBrowserHost::GetJSMessageQueryFunctionName().CString();
  67. config.js_cancel_function = WebBrowserHost::GetJSMessageQueryCancelFunctionName().CString();
  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, TransitionType transition_type) 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. browserSettings.web_security = WebBrowserHost::GetWebSecurity() ? STATE_ENABLED : STATE_DISABLED;
  264. windowInfo.width = width;
  265. windowInfo.height = height;
  266. windowInfo.transparent_painting_enabled = 1;
  267. Graphics* graphics = webClient_->GetSubsystem<Graphics>();
  268. if (graphics)
  269. {
  270. SDL_Window* sdlWindow = static_cast<SDL_Window*>(graphics->GetSDLWindow());
  271. SDL_SysWMinfo info;
  272. SDL_VERSION(&info.version);
  273. if(SDL_GetWindowWMInfo(sdlWindow, &info))
  274. {
  275. #ifdef ATOMIC_PLATFORM_OSX
  276. NSView* view = (NSView*) GetNSWindowContentView(info.info.cocoa.window);
  277. windowInfo.SetAsWindowless(view, false);
  278. #endif
  279. #ifdef ATOMIC_PLATFORM_WINDOWS
  280. windowInfo.SetAsWindowless(info.info.win.window, /*transparent*/ true);
  281. #endif
  282. #ifdef ATOMIC_PLATFORM_LINUX
  283. if ( info.subsystem == SDL_SYSWM_X11 )
  284. windowInfo.SetAsWindowless(info.info.x11.window, true);
  285. #endif
  286. }
  287. }
  288. else
  289. {
  290. #ifndef ATOMIC_PLATFORM_LINUX
  291. // headless
  292. windowInfo.SetAsWindowless(nullptr, true);
  293. #endif
  294. }
  295. // TODO: There seems to be a CEF bug when loading a string into a browser
  296. // which was created with an empty URL, this workaround gets things going
  297. // NOTE: I also tried loading the string, delaying 5 seconds and still won't
  298. // load a string until a URL has been passed into the view
  299. String _initialURL = initialLoadString_.Length() ? "x" : initialURL;
  300. webClient_->renderHandler_->SetSize(width, height);
  301. CefRefPtr<CefBrowser> browser = CefBrowserHost::CreateBrowserSync(windowInfo, this,
  302. _initialURL.CString(), browserSettings, nullptr);
  303. if (!browser.get())
  304. return false;
  305. browser_ = browser;
  306. if (initialLoadString_.Length())
  307. {
  308. webClient_->LoadString(initialLoadString_, initialLoadStringURL_);
  309. }
  310. return true;
  311. }
  312. // CefLifeSpanHandler methods:
  313. virtual void OnAfterCreated(CefRefPtr<CefBrowser> browser) OVERRIDE
  314. {
  315. CEF_REQUIRE_UI_THREAD();
  316. }
  317. virtual bool DoClose(CefRefPtr<CefBrowser> browser) OVERRIDE
  318. {
  319. return false;
  320. }
  321. virtual void OnBeforeClose(CefRefPtr<CefBrowser> browser) OVERRIDE
  322. {
  323. CEF_REQUIRE_UI_THREAD();
  324. browser_ = nullptr;
  325. }
  326. virtual bool OnBeforePopup(CefRefPtr<CefBrowser> browser,
  327. CefRefPtr<CefFrame> frame,
  328. const CefString& target_url,
  329. const CefString& target_frame_name,
  330. CefLifeSpanHandler::WindowOpenDisposition target_disposition,
  331. bool user_gesture,
  332. const CefPopupFeatures& popupFeatures,
  333. CefWindowInfo& windowInfo,
  334. CefRefPtr<CefClient>& client,
  335. CefBrowserSettings& settings,
  336. bool* no_javascript_access) OVERRIDE
  337. {
  338. // Called on the IO thread, cancel and convert to popup request
  339. assert(!CefCurrentlyOn(TID_UI));
  340. // Execute on the UI thread.
  341. CefPostTask(TID_UI,
  342. base::Bind(&WebClientPrivate::OnPopupRequest, this, target_url));
  343. return true;
  344. }
  345. void OnPopupRequest(const CefString& target_url)
  346. {
  347. if (webClient_.Null())
  348. return;
  349. String url;
  350. ConvertCEFString(target_url, url);
  351. VariantMap eventData;
  352. eventData[WebViewPopupRequest::P_CLIENT] = webClient_;
  353. eventData[WebViewPopupRequest::P_URL] = url;
  354. webClient_->SendEvent(E_WEBVIEWPOPUPREQUEST, eventData);
  355. }
  356. void CloseBrowser(bool force_close)
  357. {
  358. if (!CefCurrentlyOn(TID_UI))
  359. {
  360. // Execute on the UI thread.
  361. CefPostTask(TID_UI,
  362. base::Bind(&WebClientPrivate::CloseBrowser, this, force_close));
  363. return;
  364. }
  365. if (!browser_.get())
  366. return;
  367. browser_->GetHost()->CloseBrowser(force_close);
  368. }
  369. void SetInitialLoadString(const String& loadString, const String& url)
  370. {
  371. initialLoadString_ = loadString;
  372. initialLoadStringURL_ = url;
  373. }
  374. IMPLEMENT_REFCOUNTING(WebClientPrivate);
  375. void ClearReferences()
  376. {
  377. browser_ = nullptr;
  378. webBrowserHost_ = nullptr;
  379. webClient_ = nullptr;
  380. browserSideRouter_ = nullptr;
  381. }
  382. private:
  383. String initialLoadString_;
  384. String initialLoadStringURL_;
  385. CefRefPtr<CefBrowser> browser_;
  386. WeakPtr<WebBrowserHost> webBrowserHost_;
  387. WeakPtr<WebClient> webClient_;
  388. CefRefPtr<CefMessageRouterBrowserSide> browserSideRouter_;
  389. };
  390. WebClient::WebClient(Context* context) : Object(context)
  391. {
  392. d_ = new WebClientPrivate(this);
  393. d_->AddRef();
  394. SubscribeToEvent(E_WEBVIEWGLOBALPROPERTIESCHANGED, HANDLER(WebClient, HandleWebViewGlobalPropertiesChanged));
  395. }
  396. WebClient::~WebClient()
  397. {
  398. if (d_)
  399. {
  400. List<SharedPtr<WebMessageHandler>>::Iterator itr = messageHandlers_.Begin();
  401. while (itr != messageHandlers_.End())
  402. {
  403. CefMessageRouterBrowserSide::Handler* handler = static_cast<CefMessageRouterBrowserSide::Handler*>((*itr)->GetCefHandler());
  404. d_->browserSideRouter_->RemoveHandler(handler);
  405. itr++;
  406. }
  407. d_->CloseBrowser(true);
  408. d_->ClearReferences();
  409. d_->Release();
  410. }
  411. d_ = nullptr;
  412. renderHandler_ = 0;
  413. }
  414. void WebClient::SendMouseClickEvent(int x, int y, unsigned button, bool mouseUp, unsigned modifier, int clickCount) const
  415. {
  416. if (!d_->browser_.get())
  417. return;
  418. CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
  419. CefMouseEvent mevent;
  420. mevent.x = x;
  421. mevent.y = y;
  422. mevent.modifiers = 0;
  423. //MBT_LEFT = 0,
  424. //MBT_MIDDLE,
  425. //MBT_RIGHT,
  426. host->SendMouseClickEvent(mevent, (CefBrowserHost::MouseButtonType) button, mouseUp, clickCount);
  427. }
  428. void WebClient::SendMousePressEvent(int x, int y, unsigned button, unsigned modifier, int clickCount) const
  429. {
  430. SendMouseClickEvent(x, y, button, false, modifier, clickCount);
  431. SendMouseClickEvent(x, y, button, true, modifier, clickCount);
  432. }
  433. void WebClient::SendMouseMoveEvent(int x, int y, unsigned modifier, bool mouseLeave) const
  434. {
  435. if (!d_->browser_.get())
  436. return;
  437. CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
  438. CefMouseEvent mevent;
  439. mevent.x = x;
  440. mevent.y = y;
  441. mevent.modifiers = 0;
  442. Input* input = GetSubsystem<Input>();
  443. if (input->GetMouseButtonDown(MOUSEB_LEFT))
  444. mevent.modifiers |= EVENTFLAG_LEFT_MOUSE_BUTTON;
  445. if (input->GetMouseButtonDown(MOUSEB_MIDDLE))
  446. mevent.modifiers |= EVENTFLAG_MIDDLE_MOUSE_BUTTON;
  447. if (input->GetMouseButtonDown(MOUSEB_RIGHT))
  448. mevent.modifiers |= EVENTFLAG_RIGHT_MOUSE_BUTTON;
  449. host->SendMouseMoveEvent(mevent, mouseLeave);
  450. }
  451. void WebClient::SendMouseWheelEvent(int x, int y, unsigned modifier,int deltaX, int deltaY) const
  452. {
  453. if (!d_->browser_.get())
  454. return;
  455. CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
  456. CefMouseEvent mevent;
  457. mevent.x = x;
  458. mevent.y = y;
  459. mevent.modifiers = 0;
  460. deltaY = -deltaY * 5;
  461. #ifndef ATOMIC_PLATFORM_OSX
  462. deltaY *= 5;
  463. #endif
  464. host->SendMouseWheelEvent(mevent, deltaX, deltaY);
  465. }
  466. /*
  467. EVENTFLAG_CAPS_LOCK_ON = 1 << 0,
  468. EVENTFLAG_SHIFT_DOWN = 1 << 1,
  469. EVENTFLAG_CONTROL_DOWN = 1 << 2,
  470. EVENTFLAG_ALT_DOWN = 1 << 3,
  471. EVENTFLAG_LEFT_MOUSE_BUTTON = 1 << 4,
  472. EVENTFLAG_MIDDLE_MOUSE_BUTTON = 1 << 5,
  473. EVENTFLAG_RIGHT_MOUSE_BUTTON = 1 << 6,
  474. // Mac OS-X command key.
  475. EVENTFLAG_COMMAND_DOWN = 1 << 7,
  476. EVENTFLAG_NUM_LOCK_ON = 1 << 8,
  477. EVENTFLAG_IS_KEY_PAD = 1 << 9,
  478. EVENTFLAG_IS_LEFT = 1 << 10,
  479. EVENTFLAG_IS_RIGHT = 1 << 11,
  480. } cef_event_flags_t;
  481. */
  482. void WebClient::SendKeyEvent(const StringHash eventType, VariantMap& eventData)
  483. {
  484. if (!d_->browser_.get())
  485. return;
  486. CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
  487. CefKeyEvent keyEvent;
  488. if (!ConvertKeyEvent(GetSubsystem<Input>(), eventType, eventData, keyEvent))
  489. return;
  490. host->SendKeyEvent(keyEvent);
  491. #ifdef ATOMIC_PLATFORM_WINDOWS
  492. // RETURN KEY: We need to send both keydown and char for return key
  493. // this allows it to be used both to confirm entry on popups,
  494. // while also being used for text input
  495. if (keyEvent.windows_key_code == 13)
  496. {
  497. keyEvent.type = KEYEVENT_CHAR;
  498. host->SendKeyEvent(keyEvent);
  499. }
  500. #endif
  501. #ifdef ATOMIC_PLATFORM_OSX
  502. // RETURN KEY: We need to send both keydown and char for return key
  503. // this allows it to be used both to confirm entry on popups,
  504. // while also being used for text input
  505. if (keyEvent.native_key_code == 36)
  506. {
  507. keyEvent.type = KEYEVENT_CHAR;
  508. host->SendKeyEvent(keyEvent);
  509. }
  510. // Send an empty key event on OSX, which seems to fix
  511. // keyboard problems on OSX with cefclient
  512. // ./cefclient --off-screen-rendering-enabled
  513. // return does not work at all on cef client with offscreen
  514. // bad interaction with arrow keys (for example here, after
  515. // hitting arrow keys, return/text takes a couple presses to register
  516. memset((void*)&keyEvent, 0, sizeof(keyEvent));
  517. keyEvent.type = KEYEVENT_KEYDOWN;
  518. keyEvent.modifiers = 0;
  519. keyEvent.native_key_code = 0;
  520. host->SendKeyEvent(keyEvent);
  521. #endif
  522. #ifdef ATOMIC_PLATFORM_LINUX
  523. if (keyEvent.windows_key_code == 0x0D)
  524. {
  525. keyEvent.type = KEYEVENT_CHAR;
  526. host->SendKeyEvent(keyEvent);
  527. }
  528. #endif
  529. }
  530. void WebClient::SendTextInputEvent(const StringHash eventType, VariantMap& eventData)
  531. {
  532. if (!d_->browser_.get())
  533. return;
  534. CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
  535. CefKeyEvent keyEvent;
  536. if (!ConvertTextInputEvent(eventType, eventData, keyEvent))
  537. return;
  538. host->SendKeyEvent(keyEvent);
  539. }
  540. void WebClient::SendFocusEvent(bool focus)
  541. {
  542. if (!d_->browser_.get())
  543. return;
  544. CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
  545. host->SendFocusEvent(focus);
  546. }
  547. // Javascript
  548. void WebClient::ExecuteJavaScript(const String& script)
  549. {
  550. if (!d_->browser_.get())
  551. return;
  552. d_->browser_->GetMainFrame()->ExecuteJavaScript(CefString(script.CString()), "", 0);
  553. }
  554. void WebClient::EvalJavaScript(unsigned evalID, const String& script)
  555. {
  556. if (!d_->browser_.get())
  557. return;
  558. // Create the message object.
  559. CefRefPtr<CefProcessMessage> msg= CefProcessMessage::Create("atomic_eval_javascript");
  560. // Retrieve the argument list object.
  561. CefRefPtr<CefListValue> args = msg->GetArgumentList();
  562. // Populate the argument values.
  563. args->SetInt(0, (int) evalID);
  564. args->SetString(1, CefString(script.CString()));
  565. // Send the process message to the render process.
  566. // Use PID_BROWSER instead when sending a message to the browser process.
  567. d_->browser_->SendProcessMessage(PID_RENDERER, msg);
  568. }
  569. void WebClient::EvalJavaScriptResult(unsigned evalID, bool result, const String& value)
  570. {
  571. using namespace WebViewJSEvalResult;
  572. VariantMap eventData;
  573. eventData[P_CLIENT] = this;
  574. eventData[P_EVALID] = evalID;
  575. eventData[P_RESULT] = result;
  576. eventData[P_VALUE] = value;
  577. SendEvent(E_WEBVIEWJSEVALRESULT, eventData);
  578. }
  579. void WebClient::AddMessageHandler(WebMessageHandler* handler, bool first)
  580. {
  581. SharedPtr<WebMessageHandler> _handler(handler);
  582. if (handler->GetWebClient())
  583. {
  584. LOGWARNING("WebClient::AddMessageHandler - message handler already added to another client");
  585. return;
  586. }
  587. if (messageHandlers_.Contains(_handler))
  588. {
  589. LOGWARNING("WebClient::AddMessageHandler - message handler already added to this client");
  590. return;
  591. }
  592. _handler->SetWebClient(this);
  593. messageHandlers_.Push(_handler);
  594. d_->browserSideRouter_->AddHandler(static_cast<CefMessageRouterBrowserSide::Handler*>(handler->GetCefHandler()), first);
  595. }
  596. void WebClient::RemoveMessageHandler(WebMessageHandler* handler)
  597. {
  598. SharedPtr<WebMessageHandler> _handler(handler);
  599. List<SharedPtr<WebMessageHandler>>::Iterator itr = messageHandlers_.Find(_handler);
  600. if (itr == messageHandlers_.End())
  601. {
  602. LOGWARNING("WebClient::RemoveMessageHandler - message handler not found");
  603. return;
  604. }
  605. d_->browserSideRouter_->RemoveHandler(static_cast<CefMessageRouterBrowserSide::Handler*>(handler->GetCefHandler()));
  606. messageHandlers_.Erase(itr);
  607. }
  608. // Navigation
  609. void WebClient::LoadURL(const String& url)
  610. {
  611. if (!d_->browser_.get())
  612. {
  613. return;
  614. }
  615. CefString _url(url.CString());
  616. d_->browser_->GetMainFrame()->LoadURL(_url);
  617. }
  618. void WebClient::LoadString(const String& source, const String& url)
  619. {
  620. if (!d_->browser_.get())
  621. {
  622. d_->SetInitialLoadString(source, url);
  623. return;
  624. }
  625. // We need to make sure global properties are updated when loading web content from source string
  626. // This is handled differently internally then we requests
  627. UpdateGlobalProperties();
  628. d_->browser_->GetMainFrame()->LoadString(source.CString(), url.CString());
  629. }
  630. void WebClient::GoBack()
  631. {
  632. if (!d_->browser_.get())
  633. return;
  634. d_->browser_->GoBack();
  635. }
  636. void WebClient::GoForward()
  637. {
  638. if (!d_->browser_.get())
  639. return;
  640. d_->browser_->GoForward();
  641. }
  642. bool WebClient::IsLoading()
  643. {
  644. if (!d_->browser_.get())
  645. return false;
  646. return d_->browser_->IsLoading();
  647. }
  648. void WebClient::Reload()
  649. {
  650. if (!d_->browser_.get())
  651. return;
  652. d_->browser_->Reload();
  653. }
  654. void WebClient::ShortcutCut()
  655. {
  656. if (!d_->browser_.get())
  657. return;
  658. d_->browser_->GetFocusedFrame()->Cut();
  659. }
  660. void WebClient::ShortcutCopy()
  661. {
  662. if (!d_->browser_.get())
  663. return;
  664. d_->browser_->GetFocusedFrame()->Copy();
  665. }
  666. void WebClient::ShortcutPaste()
  667. {
  668. if (!d_->browser_.get())
  669. return;
  670. d_->browser_->GetFocusedFrame()->Paste();
  671. }
  672. void WebClient::ShortcutSelectAll()
  673. {
  674. if (!d_->browser_.get())
  675. return;
  676. d_->browser_->GetFocusedFrame()->SelectAll();
  677. }
  678. void WebClient::ShortcutUndo()
  679. {
  680. if (!d_->browser_.get())
  681. return;
  682. d_->browser_->GetFocusedFrame()->Undo();
  683. }
  684. void WebClient::ShortcutRedo()
  685. {
  686. if (!d_->browser_.get())
  687. return;
  688. d_->browser_->GetFocusedFrame()->Redo();
  689. }
  690. void WebClient::ShortcutDelete()
  691. {
  692. if (!d_->browser_.get())
  693. return;
  694. d_->browser_->GetFocusedFrame()->Delete();
  695. }
  696. void WebClient::WasResized()
  697. {
  698. if (!d_->browser_.get())
  699. return;
  700. CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
  701. host->WasResized();;
  702. }
  703. bool WebClient::CreateBrowser(const String& initialURL, int width, int height)
  704. {
  705. bool result = d_->CreateBrowser(initialURL, width, height);
  706. return result;
  707. }
  708. void WebClient::UpdateGlobalProperties()
  709. {
  710. if (!d_->browser_.get())
  711. return;
  712. CefRefPtr<CefDictionaryValue> globalProps;
  713. if (!WebAppBrowser::CreateGlobalProperties(globalProps))
  714. return;
  715. // Create the message object.
  716. CefRefPtr<CefProcessMessage> msg = CefProcessMessage::Create("atomic_set_globalproperties");
  717. // Retrieve the argument list object.
  718. CefRefPtr<CefListValue> args = msg->GetArgumentList();
  719. args->SetDictionary(0, globalProps);
  720. // Send the process message to the render process.
  721. if (!d_->browser_->SendProcessMessage(PID_RENDERER, msg))
  722. {
  723. LOGERROR("WebClient::UpdateGlobalProperties - Failed to send message");
  724. }
  725. }
  726. void WebClient::HandleWebViewGlobalPropertiesChanged(StringHash eventType, VariantMap& eventData)
  727. {
  728. UpdateGlobalProperties();
  729. }
  730. void WebClient::SetSize(int width, int height)
  731. {
  732. if (renderHandler_.Null())
  733. return;
  734. if (renderHandler_->GetWidth() == width && renderHandler_->GetHeight() == height)
  735. return;
  736. renderHandler_->SetSize(width, height);
  737. WasResized();
  738. }
  739. void WebClient::SetZoomLevel(float zoomLevel)
  740. {
  741. if (!d_->browser_.get())
  742. return;
  743. d_->browser_->GetHost()->SetZoomLevel(zoomLevel);
  744. }
  745. void WebClient::SetWebRenderHandler(WebRenderHandler* handler)
  746. {
  747. handler->SetWebClient(this);
  748. renderHandler_ = handler;
  749. }
  750. CefClient* WebClient::GetCefClient()
  751. {
  752. return d_;
  753. }
  754. }