WebClient.cpp 23 KB

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