WebClient.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  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 <Atomic/Core/ProcessUtils.h>
  10. #include <Atomic/Core/CoreEvents.h>
  11. #include <Atomic/IO/Log.h>
  12. #include <Atomic/Input/Input.h>
  13. #include <Atomic/Graphics/Graphics.h>
  14. #include "WebBrowserHost.h"
  15. #include "WebClient.h"
  16. #include "WebKeyboard.h"
  17. #include "WebViewEvents.h"
  18. #include "WebString.h"
  19. namespace Atomic
  20. {
  21. #ifdef ATOMIC_PLATFORM_OSX
  22. void* GetNSWindowContentView(void* window);
  23. #endif
  24. class WebClientPrivate : public CefClient, public CefLifeSpanHandler, public CefLoadHandler, public CefDisplayHandler
  25. {
  26. friend class WebClient;
  27. public:
  28. WebClientPrivate(WebClient* client)
  29. {
  30. webClient_ = client;
  31. webBrowserHost_ = webClient_->GetSubsystem<WebBrowserHost>();
  32. }
  33. CefRefPtr<CefRenderHandler> GetRenderHandler() OVERRIDE
  34. {
  35. if (webClient_->renderHandler_.Null())
  36. return nullptr;
  37. return webClient_->renderHandler_->GetCEFRenderHandler();
  38. }
  39. virtual CefRefPtr<CefLifeSpanHandler> GetLifeSpanHandler() OVERRIDE
  40. {
  41. return this;
  42. }
  43. virtual CefRefPtr<CefLoadHandler> GetLoadHandler() OVERRIDE {
  44. return this;
  45. }
  46. virtual CefRefPtr<CefDisplayHandler> GetDisplayHandler() OVERRIDE {
  47. return this;
  48. }
  49. // CefLoadHandler
  50. void OnLoadStart(CefRefPtr<CefBrowser> browser,
  51. CefRefPtr<CefFrame> frame) OVERRIDE
  52. {
  53. if (webClient_.Null() || !frame->IsMain())
  54. return;
  55. VariantMap eventData;
  56. eventData[WebViewLoadStart::P_CLIENT] = webClient_;
  57. CefString cefURL = frame->GetURL();
  58. String url;
  59. ConvertCEFString(cefURL, url);
  60. eventData[WebViewLoadStart::P_URL] = url;
  61. webClient_->SendEvent(E_WEBVIEWLOADSTART, eventData);
  62. }
  63. void OnLoadEnd(CefRefPtr<CefBrowser> browser,
  64. CefRefPtr<CefFrame> frame,
  65. int httpStatusCode) OVERRIDE
  66. {
  67. if (webClient_.Null() || !frame->IsMain())
  68. return;
  69. VariantMap eventData;
  70. eventData[WebViewLoadEnd::P_CLIENT] = webClient_;
  71. CefString cefURL = frame->GetURL();
  72. String url;
  73. ConvertCEFString(cefURL, url);
  74. eventData[WebViewLoadEnd::P_URL] = url;
  75. webClient_->SendEvent(E_WEBVIEWLOADEND, eventData);
  76. }
  77. void OnLoadError(CefRefPtr<CefBrowser> browser,
  78. CefRefPtr<CefFrame> frame,
  79. ErrorCode errorCode,
  80. const CefString& errorText,
  81. const CefString& failedUrl) OVERRIDE
  82. {
  83. if (webClient_.Null())
  84. return;
  85. }
  86. void OnLoadingStateChange(CefRefPtr<CefBrowser> browser,
  87. bool isLoading,
  88. bool canGoBack,
  89. bool canGoForward) OVERRIDE
  90. {
  91. if (webClient_.Null())
  92. return;
  93. VariantMap eventData;
  94. eventData[WebViewLoadStateChange::P_CLIENT] = webClient_;
  95. eventData[WebViewLoadStateChange::P_LOADING] = isLoading;
  96. eventData[WebViewLoadStateChange::P_CANGOBACK] = canGoBack;
  97. eventData[WebViewLoadStateChange::P_CANGOFORWARD] = canGoForward;
  98. webClient_->SendEvent(E_WEBVIEWLOADSTATECHANGE, eventData);
  99. }
  100. // CefDisplayHandler
  101. void OnAddressChange(CefRefPtr<CefBrowser> browser,
  102. CefRefPtr<CefFrame> frame,
  103. const CefString& url) OVERRIDE
  104. {
  105. if (webClient_.Null() || !frame->IsMain())
  106. return;
  107. VariantMap eventData;
  108. eventData[WebViewAddressChange::P_CLIENT] = webClient_;
  109. String _url;
  110. ConvertCEFString(url, _url);
  111. eventData[WebViewAddressChange::P_URL] = _url;
  112. webClient_->SendEvent(E_WEBVIEWADDRESSCHANGE, eventData);
  113. }
  114. void OnTitleChange(CefRefPtr<CefBrowser> browser,
  115. const CefString& title) OVERRIDE
  116. {
  117. if (webClient_.Null())
  118. return;
  119. VariantMap eventData;
  120. eventData[WebViewTitleChange::P_CLIENT] = webClient_;
  121. String _title;
  122. ConvertCEFString(title, _title);
  123. eventData[WebViewTitleChange::P_TITLE] = _title;
  124. webClient_->SendEvent(E_WEBVIEWTITLECHANGE, eventData);
  125. }
  126. bool OnProcessMessageReceived(CefRefPtr<CefBrowser> browser,
  127. CefProcessId source_process,
  128. CefRefPtr<CefProcessMessage> message) OVERRIDE
  129. {
  130. return false;
  131. }
  132. bool CreateBrowser(const String& initialURL, int width, int height)
  133. {
  134. if (webClient_->renderHandler_.Null())
  135. {
  136. LOGERROR("WebClient::CreateBrowser - No render handler specified");
  137. return false;
  138. }
  139. CefWindowInfo windowInfo;
  140. CefBrowserSettings browserSettings;
  141. browserSettings.webgl = STATE_ENABLED;
  142. windowInfo.width = width;
  143. windowInfo.height = height;
  144. Graphics* graphics = webClient_->GetSubsystem<Graphics>();
  145. SDL_Window* sdlWindow = static_cast<SDL_Window*>(graphics->GetSDLWindow());
  146. SDL_SysWMinfo info;
  147. SDL_VERSION(&info.version);
  148. if(SDL_GetWindowWMInfo(sdlWindow, &info))
  149. {
  150. #ifdef ATOMIC_PLATFORM_OSX
  151. NSView* view = (NSView*) GetNSWindowContentView(info.info.cocoa.window);
  152. windowInfo.SetAsWindowless(view, false);
  153. #endif
  154. #ifdef ATOMIC_PLATFORM_WINDOWS
  155. windowInfo.SetAsWindowless(info.info.win.window, false);
  156. #endif
  157. webClient_->renderHandler_->SetSize(width, height);
  158. CefRefPtr<CefBrowser> browser = CefBrowserHost::CreateBrowserSync(windowInfo, this,
  159. initialURL.CString(), browserSettings, nullptr);
  160. if (!browser.get())
  161. return false;
  162. browser_ = browser;
  163. return true;
  164. }
  165. return false;
  166. }
  167. // CefLifeSpanHandler methods:
  168. virtual void OnAfterCreated(CefRefPtr<CefBrowser> browser) OVERRIDE
  169. {
  170. CEF_REQUIRE_UI_THREAD();
  171. }
  172. virtual bool DoClose(CefRefPtr<CefBrowser> browser) OVERRIDE
  173. {
  174. return false;
  175. }
  176. virtual void OnBeforeClose(CefRefPtr<CefBrowser> browser) OVERRIDE
  177. {
  178. CEF_REQUIRE_UI_THREAD();
  179. if (browser->IsSame(browser_))
  180. browser_ = nullptr;
  181. }
  182. void CloseBrowser(bool force_close)
  183. {
  184. if (!CefCurrentlyOn(TID_UI))
  185. {
  186. // Execute on the UI thread.
  187. CefPostTask(TID_UI,
  188. base::Bind(&WebClientPrivate::CloseBrowser, this, force_close));
  189. return;
  190. }
  191. if (!browser_.get())
  192. return;
  193. browser_->GetHost()->CloseBrowser(force_close);
  194. }
  195. IMPLEMENT_REFCOUNTING(WebClientPrivate);
  196. private:
  197. CefRefPtr<CefBrowser> browser_;
  198. WeakPtr<WebBrowserHost> webBrowserHost_;
  199. WeakPtr<WebClient> webClient_;
  200. };
  201. WebClient::WebClient(Context* context) : Object(context)
  202. {
  203. d_ = new WebClientPrivate(this);
  204. }
  205. WebClient::~WebClient()
  206. {
  207. if (d_)
  208. d_->CloseBrowser(true);
  209. renderHandler_ = 0;
  210. //d_->Release();
  211. }
  212. void WebClient::SendMouseClickEvent(int x, int y, unsigned button, bool mouseUp, unsigned modifier) const
  213. {
  214. if (!d_->browser_.get())
  215. return;
  216. CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
  217. CefMouseEvent mevent;
  218. mevent.x = x;
  219. mevent.y = y;
  220. mevent.modifiers = 0;
  221. //MBT_LEFT = 0,
  222. //MBT_MIDDLE,
  223. //MBT_RIGHT,
  224. host->SendMouseClickEvent(mevent, (CefBrowserHost::MouseButtonType) button, mouseUp, 1);
  225. }
  226. void WebClient::SendMousePressEvent(int x, int y, unsigned button, unsigned modifier) const
  227. {
  228. SendMouseClickEvent(x, y, button, false, modifier);
  229. SendMouseClickEvent(x, y, button, true, modifier);
  230. }
  231. void WebClient::SendMouseMoveEvent(int x, int y, unsigned modifier, bool mouseLeave) const
  232. {
  233. if (!d_->browser_.get())
  234. return;
  235. CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
  236. CefMouseEvent mevent;
  237. mevent.x = x;
  238. mevent.y = y;
  239. mevent.modifiers = 0;
  240. Input* input = GetSubsystem<Input>();
  241. if (input->GetMouseButtonDown(MOUSEB_LEFT))
  242. mevent.modifiers |= EVENTFLAG_LEFT_MOUSE_BUTTON;
  243. if (input->GetMouseButtonDown(MOUSEB_MIDDLE))
  244. mevent.modifiers |= EVENTFLAG_MIDDLE_MOUSE_BUTTON;
  245. if (input->GetMouseButtonDown(MOUSEB_RIGHT))
  246. mevent.modifiers |= EVENTFLAG_RIGHT_MOUSE_BUTTON;
  247. host->SendMouseMoveEvent(mevent, mouseLeave);
  248. }
  249. void WebClient::SendMouseWheelEvent(int x, int y, unsigned modifier,int deltaX, int deltaY) const
  250. {
  251. if (!d_->browser_.get())
  252. return;
  253. CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
  254. CefMouseEvent mevent;
  255. mevent.x = x;
  256. mevent.y = y;
  257. mevent.modifiers = 0;
  258. #ifdef ATOMIC_PLATFORM_OSX
  259. deltaY = -deltaY;
  260. #endif
  261. host->SendMouseWheelEvent(mevent, deltaX, deltaY * 5);
  262. }
  263. /*
  264. EVENTFLAG_CAPS_LOCK_ON = 1 << 0,
  265. EVENTFLAG_SHIFT_DOWN = 1 << 1,
  266. EVENTFLAG_CONTROL_DOWN = 1 << 2,
  267. EVENTFLAG_ALT_DOWN = 1 << 3,
  268. EVENTFLAG_LEFT_MOUSE_BUTTON = 1 << 4,
  269. EVENTFLAG_MIDDLE_MOUSE_BUTTON = 1 << 5,
  270. EVENTFLAG_RIGHT_MOUSE_BUTTON = 1 << 6,
  271. // Mac OS-X command key.
  272. EVENTFLAG_COMMAND_DOWN = 1 << 7,
  273. EVENTFLAG_NUM_LOCK_ON = 1 << 8,
  274. EVENTFLAG_IS_KEY_PAD = 1 << 9,
  275. EVENTFLAG_IS_LEFT = 1 << 10,
  276. EVENTFLAG_IS_RIGHT = 1 << 11,
  277. } cef_event_flags_t;
  278. */
  279. void WebClient::SendKeyEvent(const StringHash eventType, VariantMap& eventData)
  280. {
  281. if (!d_->browser_.get())
  282. return;
  283. CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
  284. CefKeyEvent keyEvent;
  285. if (!ConvertKeyEvent(eventType, eventData, keyEvent))
  286. return;
  287. host->SendKeyEvent(keyEvent);
  288. #ifdef ATOMIC_PLATFORM_OSX
  289. // Send an empty key event on OSX, which seems to fix
  290. // keyboard problems on OSX with cefclient
  291. // ./cefclient --off-screen-rendering-enabled
  292. // return does not work at all on cef client with offscreen
  293. // bad interaction with arrow keys (for example here, after
  294. // hitting arrow keys, return/text takes a couple presses to register
  295. if (eventType == "KeyDown")
  296. keyEvent.type = KEYEVENT_KEYDOWN;
  297. else
  298. keyEvent.type = KEYEVENT_KEYUP;
  299. keyEvent.modifiers = 0;
  300. keyEvent.native_key_code = 0;
  301. host->SendKeyEvent(keyEvent);
  302. #endif
  303. }
  304. void WebClient::SendTextInputEvent(const StringHash eventType, VariantMap& eventData)
  305. {
  306. if (!d_->browser_.get())
  307. return;
  308. CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
  309. CefKeyEvent keyEvent;
  310. if (!ConvertTextInputEvent(eventType, eventData, keyEvent))
  311. return;
  312. host->SendKeyEvent(keyEvent);
  313. }
  314. void WebClient::SendFocusEvent(bool focus)
  315. {
  316. if (!d_->browser_.get())
  317. return;
  318. CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
  319. host->SendFocusEvent(focus);
  320. }
  321. // Navigation
  322. void WebClient::LoadURL(const String& url)
  323. {
  324. if (!d_->browser_.get())
  325. return;
  326. CefString _url(url.CString());
  327. d_->browser_->GetMainFrame()->LoadURL(_url);
  328. }
  329. void WebClient::GoBack()
  330. {
  331. if (!d_->browser_.get())
  332. return;
  333. d_->browser_->GoBack();
  334. }
  335. void WebClient::GoForward()
  336. {
  337. if (!d_->browser_.get())
  338. return;
  339. d_->browser_->GoForward();
  340. }
  341. bool WebClient::IsLoading()
  342. {
  343. if (!d_->browser_.get())
  344. return false;
  345. return d_->browser_->IsLoading();
  346. }
  347. void WebClient::Reload()
  348. {
  349. if (!d_->browser_.get())
  350. return;
  351. d_->browser_->Reload();
  352. }
  353. void WebClient::ShortcutCut()
  354. {
  355. if (!d_->browser_.get())
  356. return;
  357. d_->browser_->GetFocusedFrame()->Cut();
  358. }
  359. void WebClient::ShortcutCopy()
  360. {
  361. if (!d_->browser_.get())
  362. return;
  363. d_->browser_->GetFocusedFrame()->Copy();
  364. }
  365. void WebClient::ShortcutPaste()
  366. {
  367. if (!d_->browser_.get())
  368. return;
  369. d_->browser_->GetFocusedFrame()->Paste();
  370. }
  371. void WebClient::ShortcutSelectAll()
  372. {
  373. if (!d_->browser_.get())
  374. return;
  375. d_->browser_->GetFocusedFrame()->SelectAll();
  376. }
  377. void WebClient::ShortcutUndo()
  378. {
  379. if (!d_->browser_.get())
  380. return;
  381. d_->browser_->GetFocusedFrame()->Undo();
  382. }
  383. void WebClient::ShortcutRedo()
  384. {
  385. if (!d_->browser_.get())
  386. return;
  387. d_->browser_->GetFocusedFrame()->Redo();
  388. }
  389. void WebClient::ShortcutDelete()
  390. {
  391. if (!d_->browser_.get())
  392. return;
  393. d_->browser_->GetFocusedFrame()->Delete();
  394. }
  395. void WebClient::WasResized()
  396. {
  397. if (!d_->browser_.get())
  398. return;
  399. CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
  400. host->WasResized();;
  401. }
  402. bool WebClient::CreateBrowser(const String& initialURL, int width, int height)
  403. {
  404. return d_->CreateBrowser(initialURL, width, height);
  405. }
  406. void WebClient::SetSize(int width, int height)
  407. {
  408. if (renderHandler_.Null())
  409. return;
  410. if (renderHandler_->GetWidth() == width && renderHandler_->GetHeight() == height)
  411. return;
  412. renderHandler_->SetSize(width, height);
  413. WasResized();
  414. }
  415. void WebClient::SetWebRenderHandler(WebRenderHandler* handler)
  416. {
  417. handler->SetWebClient(this);
  418. renderHandler_ = handler;
  419. }
  420. CefClient* WebClient::GetCefClient()
  421. {
  422. return d_;
  423. }
  424. }