WebClient.cpp 25 KB

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