WebClient.cpp 17 KB

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