WebClient.cpp 21 KB

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