WebClient.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  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. #include <include/cef_app.h>
  23. #include <include/cef_client.h>
  24. #include <include/cef_browser.h>
  25. #include <include/wrapper/cef_helpers.h>
  26. #include <include/base/cef_bind.h>
  27. #include <include/wrapper/cef_closure_task.h>
  28. #include "include/wrapper/cef_message_router.h"
  29. #include <Atomic/Core/ProcessUtils.h>
  30. #include <Atomic/Core/CoreEvents.h>
  31. #include <Atomic/IO/Log.h>
  32. #include <Atomic/Input/Input.h>
  33. #include <Atomic/Graphics/Graphics.h>
  34. #include "WebBrowserHost.h"
  35. #include "WebMessageHandler.h"
  36. #include "WebClient.h"
  37. #include "WebKeyboard.h"
  38. #include "WebViewEvents.h"
  39. #include "WebString.h"
  40. #include <SDL/include/SDL.h>
  41. #include <ThirdParty/SDL/include/SDL_syswm.h>
  42. namespace Atomic
  43. {
  44. #ifdef ATOMIC_PLATFORM_OSX
  45. void* GetNSWindowContentView(void* window);
  46. #endif
  47. class WebClientPrivate : public CefClient,
  48. public CefLifeSpanHandler,
  49. public CefLoadHandler,
  50. public CefDisplayHandler,
  51. public CefRequestHandler,
  52. public CefKeyboardHandler
  53. {
  54. friend class WebClient;
  55. public:
  56. WebClientPrivate(WebClient* client)
  57. {
  58. webClient_ = client;
  59. webBrowserHost_ = webClient_->GetSubsystem<WebBrowserHost>();
  60. CefMessageRouterConfig config;
  61. config.js_query_function = "atomicQuery";
  62. config.js_cancel_function = "atomicQueryCancel";
  63. browserSideRouter_ = CefMessageRouterBrowserSide::Create(config);
  64. }
  65. virtual ~WebClientPrivate()
  66. {
  67. }
  68. CefRefPtr<CefRenderHandler> GetRenderHandler() OVERRIDE
  69. {
  70. if (webClient_->renderHandler_.Null())
  71. return nullptr;
  72. return webClient_->renderHandler_->GetCEFRenderHandler();
  73. }
  74. CefRefPtr<CefLifeSpanHandler> GetLifeSpanHandler() OVERRIDE
  75. {
  76. return this;
  77. }
  78. CefRefPtr<CefLoadHandler> GetLoadHandler() OVERRIDE
  79. {
  80. return this;
  81. }
  82. CefRefPtr<CefDisplayHandler> GetDisplayHandler() OVERRIDE
  83. {
  84. return this;
  85. }
  86. CefRefPtr<CefRequestHandler> GetRequestHandler() OVERRIDE
  87. {
  88. return this;
  89. }
  90. CefRefPtr<CefKeyboardHandler> GetKeyboardHandler() OVERRIDE
  91. {
  92. return this;
  93. }
  94. // CefKeyboardHandler
  95. virtual bool OnPreKeyEvent(CefRefPtr<CefBrowser> browser,
  96. const CefKeyEvent& event,
  97. CefEventHandle os_event,
  98. bool* is_keyboard_shortcut) OVERRIDE
  99. {
  100. return false;
  101. }
  102. // CefRequestHandler methods
  103. bool OnBeforeBrowse(CefRefPtr<CefBrowser> browser,
  104. CefRefPtr<CefFrame> frame,
  105. CefRefPtr<CefRequest> request,
  106. bool is_redirect) OVERRIDE
  107. {
  108. CEF_REQUIRE_UI_THREAD();
  109. browserSideRouter_->OnBeforeBrowse(browser, frame);
  110. return false;
  111. }
  112. bool OnProcessMessageReceived(
  113. CefRefPtr<CefBrowser> browser,
  114. CefProcessId source_process,
  115. CefRefPtr<CefProcessMessage> message) OVERRIDE
  116. {
  117. CEF_REQUIRE_UI_THREAD();
  118. const CefString& message_name = message->GetName();
  119. if (message_name == "atomic_eval_javascript_result")
  120. {
  121. if (webClient_.Null())
  122. return false;
  123. unsigned evalID = (unsigned) message->GetArgumentList()->GetInt(0);
  124. bool result = message->GetArgumentList()->GetBool(1);
  125. String value;
  126. ConvertCEFString(message->GetArgumentList()->GetString(2), value);
  127. webClient_->EvalJavaScriptResult(evalID, result, value);
  128. return true;
  129. }
  130. if (browserSideRouter_->OnProcessMessageReceived(browser, source_process, message))
  131. {
  132. return true;
  133. }
  134. return false;
  135. }
  136. void OnRenderProcessTerminated(CefRefPtr<CefBrowser> browser,
  137. TerminationStatus status) OVERRIDE
  138. {
  139. CEF_REQUIRE_UI_THREAD();
  140. browserSideRouter_->OnRenderProcessTerminated(browser);
  141. }
  142. // CefLoadHandler
  143. void OnLoadStart(CefRefPtr<CefBrowser> browser,
  144. CefRefPtr<CefFrame> frame) OVERRIDE
  145. {
  146. if (webClient_.Null() || !frame->IsMain())
  147. return;
  148. VariantMap eventData;
  149. eventData[WebViewLoadStart::P_CLIENT] = webClient_;
  150. CefString cefURL = frame->GetURL();
  151. String url;
  152. ConvertCEFString(cefURL, url);
  153. eventData[WebViewLoadStart::P_URL] = url;
  154. webClient_->SendEvent(E_WEBVIEWLOADSTART, eventData);
  155. }
  156. void OnLoadEnd(CefRefPtr<CefBrowser> browser,
  157. CefRefPtr<CefFrame> frame,
  158. int httpStatusCode) OVERRIDE
  159. {
  160. if (webClient_.Null() || !frame->IsMain())
  161. return;
  162. VariantMap eventData;
  163. eventData[WebViewLoadEnd::P_CLIENT] = webClient_;
  164. CefString cefURL = frame->GetURL();
  165. String url;
  166. ConvertCEFString(cefURL, url);
  167. eventData[WebViewLoadEnd::P_URL] = url;
  168. webClient_->SendEvent(E_WEBVIEWLOADEND, eventData);
  169. }
  170. void OnLoadError(CefRefPtr<CefBrowser> browser,
  171. CefRefPtr<CefFrame> frame,
  172. ErrorCode errorCode,
  173. const CefString& errorText,
  174. const CefString& failedUrl) OVERRIDE
  175. {
  176. if (webClient_.Null())
  177. return;
  178. }
  179. void OnLoadingStateChange(CefRefPtr<CefBrowser> browser,
  180. bool isLoading,
  181. bool canGoBack,
  182. bool canGoForward) OVERRIDE
  183. {
  184. if (webClient_.Null())
  185. return;
  186. VariantMap eventData;
  187. eventData[WebViewLoadStateChange::P_CLIENT] = webClient_;
  188. eventData[WebViewLoadStateChange::P_LOADING] = isLoading;
  189. eventData[WebViewLoadStateChange::P_CANGOBACK] = canGoBack;
  190. eventData[WebViewLoadStateChange::P_CANGOFORWARD] = canGoForward;
  191. webClient_->SendEvent(E_WEBVIEWLOADSTATECHANGE, eventData);
  192. }
  193. // CefDisplayHandler
  194. void OnAddressChange(CefRefPtr<CefBrowser> browser,
  195. CefRefPtr<CefFrame> frame,
  196. const CefString& url) OVERRIDE
  197. {
  198. if (webClient_.Null() || !frame->IsMain())
  199. return;
  200. VariantMap eventData;
  201. eventData[WebViewAddressChange::P_CLIENT] = webClient_;
  202. String _url;
  203. ConvertCEFString(url, _url);
  204. eventData[WebViewAddressChange::P_URL] = _url;
  205. webClient_->SendEvent(E_WEBVIEWADDRESSCHANGE, eventData);
  206. }
  207. void OnTitleChange(CefRefPtr<CefBrowser> browser,
  208. const CefString& title) OVERRIDE
  209. {
  210. if (webClient_.Null())
  211. return;
  212. VariantMap eventData;
  213. eventData[WebViewTitleChange::P_CLIENT] = webClient_;
  214. String _title;
  215. ConvertCEFString(title, _title);
  216. eventData[WebViewTitleChange::P_TITLE] = _title;
  217. webClient_->SendEvent(E_WEBVIEWTITLECHANGE, eventData);
  218. }
  219. ///
  220. // Called to display a console message. Return true to stop the message from
  221. // being output to the console.
  222. ///
  223. /*--cef(optional_param=message,optional_param=source)--*/
  224. virtual bool OnConsoleMessage(CefRefPtr<CefBrowser> browser,
  225. const CefString& message,
  226. const CefString& source,
  227. int line) OVERRIDE
  228. {
  229. if (webClient_.Null())
  230. return false;
  231. String _message;
  232. ConvertCEFString(message, _message);
  233. String _source;
  234. ConvertCEFString(source, _source);
  235. LOGINFOF("WebViewJS: %s (%s:%i)", _message.CString(), _source.CString(), line);
  236. return false;
  237. }
  238. bool CreateBrowser(const String& initialURL, int width, int height)
  239. {
  240. if (webClient_->renderHandler_.Null())
  241. {
  242. LOGERROR("WebClient::CreateBrowser - No render handler specified");
  243. return false;
  244. }
  245. CefWindowInfo windowInfo;
  246. CefBrowserSettings browserSettings;
  247. browserSettings.webgl = STATE_ENABLED;
  248. browserSettings.file_access_from_file_urls = STATE_ENABLED;
  249. browserSettings.universal_access_from_file_urls = STATE_ENABLED;
  250. windowInfo.width = width;
  251. windowInfo.height = height;
  252. Graphics* graphics = webClient_->GetSubsystem<Graphics>();
  253. if (graphics)
  254. {
  255. SDL_Window* sdlWindow = static_cast<SDL_Window*>(graphics->GetSDLWindow());
  256. SDL_SysWMinfo info;
  257. SDL_VERSION(&info.version);
  258. if(SDL_GetWindowWMInfo(sdlWindow, &info))
  259. {
  260. #ifdef ATOMIC_PLATFORM_OSX
  261. NSView* view = (NSView*) GetNSWindowContentView(info.info.cocoa.window);
  262. windowInfo.SetAsWindowless(view, false);
  263. #endif
  264. #ifdef ATOMIC_PLATFORM_WINDOWS
  265. windowInfo.SetAsWindowless(info.info.win.window, false);
  266. #endif
  267. }
  268. }
  269. else
  270. {
  271. #ifndef ATOMIC_PLATFORM_LINUX
  272. // headless
  273. windowInfo.SetAsWindowless(nullptr, false);
  274. #endif
  275. }
  276. webClient_->renderHandler_->SetSize(width, height);
  277. CefRefPtr<CefBrowser> browser = CefBrowserHost::CreateBrowserSync(windowInfo, this,
  278. initialURL.CString(), browserSettings, nullptr);
  279. if (!browser.get())
  280. return false;
  281. browser_ = browser;
  282. return true;
  283. }
  284. // CefLifeSpanHandler methods:
  285. virtual void OnAfterCreated(CefRefPtr<CefBrowser> browser) OVERRIDE
  286. {
  287. CEF_REQUIRE_UI_THREAD();
  288. }
  289. virtual bool DoClose(CefRefPtr<CefBrowser> browser) OVERRIDE
  290. {
  291. return false;
  292. }
  293. virtual void OnBeforeClose(CefRefPtr<CefBrowser> browser) OVERRIDE
  294. {
  295. CEF_REQUIRE_UI_THREAD();
  296. browser_ = nullptr;
  297. }
  298. void CloseBrowser(bool force_close)
  299. {
  300. if (!CefCurrentlyOn(TID_UI))
  301. {
  302. // Execute on the UI thread.
  303. CefPostTask(TID_UI,
  304. base::Bind(&WebClientPrivate::CloseBrowser, this, force_close));
  305. return;
  306. }
  307. if (!browser_.get())
  308. return;
  309. browser_->GetHost()->CloseBrowser(force_close);
  310. }
  311. IMPLEMENT_REFCOUNTING(WebClientPrivate);
  312. private:
  313. CefRefPtr<CefBrowser> browser_;
  314. WeakPtr<WebBrowserHost> webBrowserHost_;
  315. WeakPtr<WebClient> webClient_;
  316. CefRefPtr<CefMessageRouterBrowserSide> browserSideRouter_;
  317. };
  318. WebClient::WebClient(Context* context) : Object(context)
  319. {
  320. d_ = new WebClientPrivate(this);
  321. }
  322. WebClient::~WebClient()
  323. {
  324. if (d_)
  325. {
  326. List<SharedPtr<WebMessageHandler>>::Iterator itr = messageHandlers_.Begin();
  327. while (itr != messageHandlers_.End())
  328. {
  329. CefMessageRouterBrowserSide::Handler* handler = static_cast<CefMessageRouterBrowserSide::Handler*>((*itr)->GetCefHandler());
  330. d_->browserSideRouter_->RemoveHandler(handler);
  331. itr++;
  332. }
  333. d_->CloseBrowser(true);
  334. }
  335. renderHandler_ = 0;
  336. //d_->Release();
  337. }
  338. void WebClient::SendMouseClickEvent(int x, int y, unsigned button, bool mouseUp, unsigned modifier, int clickCount) const
  339. {
  340. if (!d_->browser_.get())
  341. return;
  342. CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
  343. CefMouseEvent mevent;
  344. mevent.x = x;
  345. mevent.y = y;
  346. mevent.modifiers = 0;
  347. //MBT_LEFT = 0,
  348. //MBT_MIDDLE,
  349. //MBT_RIGHT,
  350. host->SendMouseClickEvent(mevent, (CefBrowserHost::MouseButtonType) button, mouseUp, clickCount);
  351. }
  352. void WebClient::SendMousePressEvent(int x, int y, unsigned button, unsigned modifier, int clickCount) const
  353. {
  354. SendMouseClickEvent(x, y, button, false, modifier, clickCount);
  355. SendMouseClickEvent(x, y, button, true, modifier, clickCount);
  356. }
  357. void WebClient::SendMouseMoveEvent(int x, int y, unsigned modifier, bool mouseLeave) const
  358. {
  359. if (!d_->browser_.get())
  360. return;
  361. CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
  362. CefMouseEvent mevent;
  363. mevent.x = x;
  364. mevent.y = y;
  365. mevent.modifiers = 0;
  366. Input* input = GetSubsystem<Input>();
  367. if (input->GetMouseButtonDown(MOUSEB_LEFT))
  368. mevent.modifiers |= EVENTFLAG_LEFT_MOUSE_BUTTON;
  369. if (input->GetMouseButtonDown(MOUSEB_MIDDLE))
  370. mevent.modifiers |= EVENTFLAG_MIDDLE_MOUSE_BUTTON;
  371. if (input->GetMouseButtonDown(MOUSEB_RIGHT))
  372. mevent.modifiers |= EVENTFLAG_RIGHT_MOUSE_BUTTON;
  373. host->SendMouseMoveEvent(mevent, mouseLeave);
  374. }
  375. void WebClient::SendMouseWheelEvent(int x, int y, unsigned modifier,int deltaX, int deltaY) const
  376. {
  377. if (!d_->browser_.get())
  378. return;
  379. CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
  380. CefMouseEvent mevent;
  381. mevent.x = x;
  382. mevent.y = y;
  383. mevent.modifiers = 0;
  384. deltaY = -deltaY * 5;
  385. #ifndef ATOMIC_PLATFORM_OSX
  386. deltaY *= 5;
  387. #endif
  388. host->SendMouseWheelEvent(mevent, deltaX, deltaY);
  389. }
  390. /*
  391. EVENTFLAG_CAPS_LOCK_ON = 1 << 0,
  392. EVENTFLAG_SHIFT_DOWN = 1 << 1,
  393. EVENTFLAG_CONTROL_DOWN = 1 << 2,
  394. EVENTFLAG_ALT_DOWN = 1 << 3,
  395. EVENTFLAG_LEFT_MOUSE_BUTTON = 1 << 4,
  396. EVENTFLAG_MIDDLE_MOUSE_BUTTON = 1 << 5,
  397. EVENTFLAG_RIGHT_MOUSE_BUTTON = 1 << 6,
  398. // Mac OS-X command key.
  399. EVENTFLAG_COMMAND_DOWN = 1 << 7,
  400. EVENTFLAG_NUM_LOCK_ON = 1 << 8,
  401. EVENTFLAG_IS_KEY_PAD = 1 << 9,
  402. EVENTFLAG_IS_LEFT = 1 << 10,
  403. EVENTFLAG_IS_RIGHT = 1 << 11,
  404. } cef_event_flags_t;
  405. */
  406. void WebClient::SendKeyEvent(const StringHash eventType, VariantMap& eventData)
  407. {
  408. if (!d_->browser_.get())
  409. return;
  410. CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
  411. CefKeyEvent keyEvent;
  412. if (!ConvertKeyEvent(GetSubsystem<Input>(), eventType, eventData, keyEvent))
  413. return;
  414. host->SendKeyEvent(keyEvent);
  415. #ifdef ATOMIC_PLATFORM_WINDOWS
  416. // RETURN KEY: We need to send both keydown and char for return key
  417. // this allows it to be used both to confirm entry on popups,
  418. // while also being used for text input
  419. if (keyEvent.windows_key_code == 13)
  420. {
  421. keyEvent.type = KEYEVENT_CHAR;
  422. host->SendKeyEvent(keyEvent);
  423. }
  424. #endif
  425. #ifdef ATOMIC_PLATFORM_OSX
  426. // RETURN KEY: We need to send both keydown and char for return key
  427. // this allows it to be used both to confirm entry on popups,
  428. // while also being used for text input
  429. if (keyEvent.native_key_code == 36)
  430. {
  431. keyEvent.type = KEYEVENT_CHAR;
  432. host->SendKeyEvent(keyEvent);
  433. }
  434. // Send an empty key event on OSX, which seems to fix
  435. // keyboard problems on OSX with cefclient
  436. // ./cefclient --off-screen-rendering-enabled
  437. // return does not work at all on cef client with offscreen
  438. // bad interaction with arrow keys (for example here, after
  439. // hitting arrow keys, return/text takes a couple presses to register
  440. memset((void*)&keyEvent, 0, sizeof(keyEvent));
  441. keyEvent.type = KEYEVENT_KEYDOWN;
  442. keyEvent.modifiers = 0;
  443. keyEvent.native_key_code = 0;
  444. host->SendKeyEvent(keyEvent);
  445. #endif
  446. }
  447. void WebClient::SendTextInputEvent(const StringHash eventType, VariantMap& eventData)
  448. {
  449. if (!d_->browser_.get())
  450. return;
  451. CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
  452. CefKeyEvent keyEvent;
  453. if (!ConvertTextInputEvent(eventType, eventData, keyEvent))
  454. return;
  455. host->SendKeyEvent(keyEvent);
  456. }
  457. void WebClient::SendFocusEvent(bool focus)
  458. {
  459. if (!d_->browser_.get())
  460. return;
  461. CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
  462. host->SendFocusEvent(focus);
  463. }
  464. // Javascript
  465. void WebClient::ExecuteJavaScript(const String& script)
  466. {
  467. if (!d_->browser_.get())
  468. return;
  469. d_->browser_->GetMainFrame()->ExecuteJavaScript(CefString(script.CString()), "", 0);
  470. }
  471. void WebClient::EvalJavaScript(unsigned evalID, const String& script)
  472. {
  473. if (!d_->browser_.get())
  474. return;
  475. // Create the message object.
  476. CefRefPtr<CefProcessMessage> msg= CefProcessMessage::Create("atomic_eval_javascript");
  477. // Retrieve the argument list object.
  478. CefRefPtr<CefListValue> args = msg->GetArgumentList();
  479. // Populate the argument values.
  480. args->SetInt(0, (int) evalID);
  481. args->SetString(1, CefString(script.CString()));
  482. // Send the process message to the render process.
  483. // Use PID_BROWSER instead when sending a message to the browser process.
  484. d_->browser_->SendProcessMessage(PID_RENDERER, msg);
  485. }
  486. void WebClient::EvalJavaScriptResult(unsigned evalID, bool result, const String& value)
  487. {
  488. using namespace WebViewJSEvalResult;
  489. VariantMap eventData;
  490. eventData[P_CLIENT] = this;
  491. eventData[P_EVALID] = evalID;
  492. eventData[P_RESULT] = result;
  493. eventData[P_VALUE] = value;
  494. SendEvent(E_WEBVIEWJSEVALRESULT, eventData);
  495. }
  496. void WebClient::AddMessageHandler(WebMessageHandler* handler, bool first)
  497. {
  498. SharedPtr<WebMessageHandler> _handler(handler);
  499. if (handler->GetWebClient())
  500. {
  501. LOGWARNING("WebClient::AddMessageHandler - message handler already added to another client");
  502. return;
  503. }
  504. if (messageHandlers_.Contains(_handler))
  505. {
  506. LOGWARNING("WebClient::AddMessageHandler - message handler already added to this client");
  507. return;
  508. }
  509. _handler->SetWebClient(this);
  510. messageHandlers_.Push(_handler);
  511. d_->browserSideRouter_->AddHandler(static_cast<CefMessageRouterBrowserSide::Handler*>(handler->GetCefHandler()), first);
  512. }
  513. void WebClient::RemoveMessageHandler(WebMessageHandler* handler)
  514. {
  515. SharedPtr<WebMessageHandler> _handler(handler);
  516. List<SharedPtr<WebMessageHandler>>::Iterator itr = messageHandlers_.Find(_handler);
  517. if (itr == messageHandlers_.End())
  518. {
  519. LOGWARNING("WebClient::RemoveMessageHandler - message handler not found");
  520. return;
  521. }
  522. d_->browserSideRouter_->RemoveHandler(static_cast<CefMessageRouterBrowserSide::Handler*>(handler->GetCefHandler()));
  523. messageHandlers_.Erase(itr);
  524. }
  525. // Navigation
  526. void WebClient::LoadURL(const String& url)
  527. {
  528. if (!d_->browser_.get())
  529. {
  530. return;
  531. }
  532. CefString _url(url.CString());
  533. d_->browser_->GetMainFrame()->LoadURL(_url);
  534. }
  535. void WebClient::GoBack()
  536. {
  537. if (!d_->browser_.get())
  538. return;
  539. d_->browser_->GoBack();
  540. }
  541. void WebClient::GoForward()
  542. {
  543. if (!d_->browser_.get())
  544. return;
  545. d_->browser_->GoForward();
  546. }
  547. bool WebClient::IsLoading()
  548. {
  549. if (!d_->browser_.get())
  550. return false;
  551. return d_->browser_->IsLoading();
  552. }
  553. void WebClient::Reload()
  554. {
  555. if (!d_->browser_.get())
  556. return;
  557. d_->browser_->Reload();
  558. }
  559. void WebClient::ShortcutCut()
  560. {
  561. if (!d_->browser_.get())
  562. return;
  563. d_->browser_->GetFocusedFrame()->Cut();
  564. }
  565. void WebClient::ShortcutCopy()
  566. {
  567. if (!d_->browser_.get())
  568. return;
  569. d_->browser_->GetFocusedFrame()->Copy();
  570. }
  571. void WebClient::ShortcutPaste()
  572. {
  573. if (!d_->browser_.get())
  574. return;
  575. d_->browser_->GetFocusedFrame()->Paste();
  576. }
  577. void WebClient::ShortcutSelectAll()
  578. {
  579. if (!d_->browser_.get())
  580. return;
  581. d_->browser_->GetFocusedFrame()->SelectAll();
  582. }
  583. void WebClient::ShortcutUndo()
  584. {
  585. if (!d_->browser_.get())
  586. return;
  587. d_->browser_->GetFocusedFrame()->Undo();
  588. }
  589. void WebClient::ShortcutRedo()
  590. {
  591. if (!d_->browser_.get())
  592. return;
  593. d_->browser_->GetFocusedFrame()->Redo();
  594. }
  595. void WebClient::ShortcutDelete()
  596. {
  597. if (!d_->browser_.get())
  598. return;
  599. d_->browser_->GetFocusedFrame()->Delete();
  600. }
  601. void WebClient::WasResized()
  602. {
  603. if (!d_->browser_.get())
  604. return;
  605. CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
  606. host->WasResized();;
  607. }
  608. bool WebClient::CreateBrowser(const String& initialURL, int width, int height)
  609. {
  610. bool result = d_->CreateBrowser(initialURL, width, height);
  611. return result;
  612. }
  613. void WebClient::SetSize(int width, int height)
  614. {
  615. if (renderHandler_.Null())
  616. return;
  617. if (renderHandler_->GetWidth() == width && renderHandler_->GetHeight() == height)
  618. return;
  619. renderHandler_->SetSize(width, height);
  620. WasResized();
  621. }
  622. void WebClient::SetWebRenderHandler(WebRenderHandler* handler)
  623. {
  624. handler->SetWebClient(this);
  625. renderHandler_ = handler;
  626. }
  627. CefClient* WebClient::GetCefClient()
  628. {
  629. return d_;
  630. }
  631. }