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. List<SharedPtr<WebMessageHandler>>::Iterator itr = webClient_->messageHandlers_.Begin();
  238. while (itr != webClient_->messageHandlers_.End())
  239. {
  240. CefMessageRouterBrowserSide::Handler* handler = static_cast<CefMessageRouterBrowserSide::Handler*>((*itr)->GetCefHandler());
  241. browserSideRouter_->RemoveHandler(handler);
  242. itr++;
  243. }
  244. webClient_->messageHandlers_.Clear();
  245. browser_ = nullptr;
  246. }
  247. void CloseBrowser(bool force_close)
  248. {
  249. if (!CefCurrentlyOn(TID_UI))
  250. {
  251. // Execute on the UI thread.
  252. CefPostTask(TID_UI,
  253. base::Bind(&WebClientPrivate::CloseBrowser, this, force_close));
  254. return;
  255. }
  256. if (!browser_.get())
  257. return;
  258. browser_->GetHost()->CloseBrowser(force_close);
  259. }
  260. IMPLEMENT_REFCOUNTING(WebClientPrivate);
  261. private:
  262. CefRefPtr<CefBrowser> browser_;
  263. WeakPtr<WebBrowserHost> webBrowserHost_;
  264. WeakPtr<WebClient> webClient_;
  265. CefRefPtr<CefMessageRouterBrowserSide> browserSideRouter_;
  266. };
  267. WebClient::WebClient(Context* context) : Object(context)
  268. {
  269. d_ = new WebClientPrivate(this);
  270. }
  271. WebClient::~WebClient()
  272. {
  273. if (d_)
  274. d_->CloseBrowser(true);
  275. renderHandler_ = 0;
  276. //d_->Release();
  277. }
  278. void WebClient::SendMouseClickEvent(int x, int y, unsigned button, bool mouseUp, unsigned modifier) const
  279. {
  280. if (!d_->browser_.get())
  281. return;
  282. CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
  283. CefMouseEvent mevent;
  284. mevent.x = x;
  285. mevent.y = y;
  286. mevent.modifiers = 0;
  287. //MBT_LEFT = 0,
  288. //MBT_MIDDLE,
  289. //MBT_RIGHT,
  290. host->SendMouseClickEvent(mevent, (CefBrowserHost::MouseButtonType) button, mouseUp, 1);
  291. }
  292. void WebClient::SendMousePressEvent(int x, int y, unsigned button, unsigned modifier) const
  293. {
  294. SendMouseClickEvent(x, y, button, false, modifier);
  295. SendMouseClickEvent(x, y, button, true, modifier);
  296. }
  297. void WebClient::SendMouseMoveEvent(int x, int y, unsigned modifier, bool mouseLeave) const
  298. {
  299. if (!d_->browser_.get())
  300. return;
  301. CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
  302. CefMouseEvent mevent;
  303. mevent.x = x;
  304. mevent.y = y;
  305. mevent.modifiers = 0;
  306. Input* input = GetSubsystem<Input>();
  307. if (input->GetMouseButtonDown(MOUSEB_LEFT))
  308. mevent.modifiers |= EVENTFLAG_LEFT_MOUSE_BUTTON;
  309. if (input->GetMouseButtonDown(MOUSEB_MIDDLE))
  310. mevent.modifiers |= EVENTFLAG_MIDDLE_MOUSE_BUTTON;
  311. if (input->GetMouseButtonDown(MOUSEB_RIGHT))
  312. mevent.modifiers |= EVENTFLAG_RIGHT_MOUSE_BUTTON;
  313. host->SendMouseMoveEvent(mevent, mouseLeave);
  314. }
  315. void WebClient::SendMouseWheelEvent(int x, int y, unsigned modifier,int deltaX, int deltaY) const
  316. {
  317. if (!d_->browser_.get())
  318. return;
  319. CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
  320. CefMouseEvent mevent;
  321. mevent.x = x;
  322. mevent.y = y;
  323. mevent.modifiers = 0;
  324. #ifdef ATOMIC_PLATFORM_OSX
  325. deltaY = -deltaY;
  326. #endif
  327. host->SendMouseWheelEvent(mevent, deltaX, deltaY * 5);
  328. }
  329. /*
  330. EVENTFLAG_CAPS_LOCK_ON = 1 << 0,
  331. EVENTFLAG_SHIFT_DOWN = 1 << 1,
  332. EVENTFLAG_CONTROL_DOWN = 1 << 2,
  333. EVENTFLAG_ALT_DOWN = 1 << 3,
  334. EVENTFLAG_LEFT_MOUSE_BUTTON = 1 << 4,
  335. EVENTFLAG_MIDDLE_MOUSE_BUTTON = 1 << 5,
  336. EVENTFLAG_RIGHT_MOUSE_BUTTON = 1 << 6,
  337. // Mac OS-X command key.
  338. EVENTFLAG_COMMAND_DOWN = 1 << 7,
  339. EVENTFLAG_NUM_LOCK_ON = 1 << 8,
  340. EVENTFLAG_IS_KEY_PAD = 1 << 9,
  341. EVENTFLAG_IS_LEFT = 1 << 10,
  342. EVENTFLAG_IS_RIGHT = 1 << 11,
  343. } cef_event_flags_t;
  344. */
  345. void WebClient::SendKeyEvent(const StringHash eventType, VariantMap& eventData)
  346. {
  347. if (!d_->browser_.get())
  348. return;
  349. CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
  350. CefKeyEvent keyEvent;
  351. if (!ConvertKeyEvent(GetSubsystem<Input>(), eventType, eventData, keyEvent))
  352. return;
  353. host->SendKeyEvent(keyEvent);
  354. #ifdef ATOMIC_PLATFORM_OSX
  355. // Send an empty key event on OSX, which seems to fix
  356. // keyboard problems on OSX with cefclient
  357. // ./cefclient --off-screen-rendering-enabled
  358. // return does not work at all on cef client with offscreen
  359. // bad interaction with arrow keys (for example here, after
  360. // hitting arrow keys, return/text takes a couple presses to register
  361. if (eventType == "KeyDown")
  362. keyEvent.type = KEYEVENT_KEYDOWN;
  363. else
  364. keyEvent.type = KEYEVENT_KEYUP;
  365. keyEvent.modifiers = 0;
  366. keyEvent.native_key_code = 0;
  367. host->SendKeyEvent(keyEvent);
  368. #endif
  369. }
  370. void WebClient::SendTextInputEvent(const StringHash eventType, VariantMap& eventData)
  371. {
  372. if (!d_->browser_.get())
  373. return;
  374. CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
  375. CefKeyEvent keyEvent;
  376. if (!ConvertTextInputEvent(eventType, eventData, keyEvent))
  377. return;
  378. host->SendKeyEvent(keyEvent);
  379. }
  380. void WebClient::SendFocusEvent(bool focus)
  381. {
  382. if (!d_->browser_.get())
  383. return;
  384. CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
  385. host->SendFocusEvent(focus);
  386. }
  387. // Javascript
  388. void WebClient::ExecuteJavaScript(const String& script)
  389. {
  390. if (!d_->browser_.get())
  391. return;
  392. d_->browser_->GetMainFrame()->ExecuteJavaScript(CefString(script.CString()), "", 0);
  393. }
  394. void WebClient::AddMessageHandler(WebMessageHandler* handler, bool first)
  395. {
  396. SharedPtr<WebMessageHandler> _handler(handler);
  397. if (handler->GetWebClient())
  398. {
  399. LOGWARNING("WebClient::AddMessageHandler - message handler already added to another client");
  400. return;
  401. }
  402. if (messageHandlers_.Contains(_handler))
  403. {
  404. LOGWARNING("WebClient::AddMessageHandler - message handler already added to this client");
  405. return;
  406. }
  407. _handler->SetWebClient(this);
  408. messageHandlers_.Push(_handler);
  409. d_->browserSideRouter_->AddHandler(static_cast<CefMessageRouterBrowserSide::Handler*>(handler->GetCefHandler()), first);
  410. }
  411. void WebClient::RemoveMessageHandler(WebMessageHandler* handler)
  412. {
  413. SharedPtr<WebMessageHandler> _handler(handler);
  414. List<SharedPtr<WebMessageHandler>>::Iterator itr = messageHandlers_.Find(_handler);
  415. if (itr == messageHandlers_.End())
  416. {
  417. LOGWARNING("WebClient::RemoveMessageHandler - message handler not found");
  418. return;
  419. }
  420. d_->browserSideRouter_->RemoveHandler(static_cast<CefMessageRouterBrowserSide::Handler*>(handler->GetCefHandler()));
  421. messageHandlers_.Erase(itr);
  422. }
  423. // Navigation
  424. void WebClient::LoadURL(const String& url)
  425. {
  426. if (!d_->browser_.get())
  427. {
  428. return;
  429. }
  430. CefString _url(url.CString());
  431. d_->browser_->GetMainFrame()->LoadURL(_url);
  432. }
  433. void WebClient::GoBack()
  434. {
  435. if (!d_->browser_.get())
  436. return;
  437. d_->browser_->GoBack();
  438. }
  439. void WebClient::GoForward()
  440. {
  441. if (!d_->browser_.get())
  442. return;
  443. d_->browser_->GoForward();
  444. }
  445. bool WebClient::IsLoading()
  446. {
  447. if (!d_->browser_.get())
  448. return false;
  449. return d_->browser_->IsLoading();
  450. }
  451. void WebClient::Reload()
  452. {
  453. if (!d_->browser_.get())
  454. return;
  455. d_->browser_->Reload();
  456. }
  457. void WebClient::ShortcutCut()
  458. {
  459. if (!d_->browser_.get())
  460. return;
  461. d_->browser_->GetFocusedFrame()->Cut();
  462. }
  463. void WebClient::ShortcutCopy()
  464. {
  465. if (!d_->browser_.get())
  466. return;
  467. d_->browser_->GetFocusedFrame()->Copy();
  468. }
  469. void WebClient::ShortcutPaste()
  470. {
  471. if (!d_->browser_.get())
  472. return;
  473. d_->browser_->GetFocusedFrame()->Paste();
  474. }
  475. void WebClient::ShortcutSelectAll()
  476. {
  477. if (!d_->browser_.get())
  478. return;
  479. d_->browser_->GetFocusedFrame()->SelectAll();
  480. }
  481. void WebClient::ShortcutUndo()
  482. {
  483. if (!d_->browser_.get())
  484. return;
  485. d_->browser_->GetFocusedFrame()->Undo();
  486. }
  487. void WebClient::ShortcutRedo()
  488. {
  489. if (!d_->browser_.get())
  490. return;
  491. d_->browser_->GetFocusedFrame()->Redo();
  492. }
  493. void WebClient::ShortcutDelete()
  494. {
  495. if (!d_->browser_.get())
  496. return;
  497. d_->browser_->GetFocusedFrame()->Delete();
  498. }
  499. void WebClient::WasResized()
  500. {
  501. if (!d_->browser_.get())
  502. return;
  503. CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
  504. host->WasResized();;
  505. }
  506. bool WebClient::CreateBrowser(const String& initialURL, int width, int height)
  507. {
  508. return d_->CreateBrowser(initialURL, width, height);
  509. }
  510. void WebClient::SetSize(int width, int height)
  511. {
  512. if (renderHandler_.Null())
  513. return;
  514. if (renderHandler_->GetWidth() == width && renderHandler_->GetHeight() == height)
  515. return;
  516. renderHandler_->SetSize(width, height);
  517. WasResized();
  518. }
  519. void WebClient::SetWebRenderHandler(WebRenderHandler* handler)
  520. {
  521. handler->SetWebClient(this);
  522. renderHandler_ = handler;
  523. }
  524. CefClient* WebClient::GetCefClient()
  525. {
  526. return d_;
  527. }
  528. }