WebClient.cpp 26 KB

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