WebClient.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993
  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::SetWebRenderHandler(WebRenderHandler* handler)
  728. {
  729. handler->SetWebClient(this);
  730. renderHandler_ = handler;
  731. }
  732. CefClient* WebClient::GetCefClient()
  733. {
  734. return d_;
  735. }
  736. }