WebClient.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  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::SendMouseMoveEvent(int x, int y, unsigned modifier, bool mouseLeave) const
  227. {
  228. if (!d_->browser_.get())
  229. return;
  230. CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
  231. CefMouseEvent mevent;
  232. mevent.x = x;
  233. mevent.y = y;
  234. mevent.modifiers = 0;
  235. Input* input = GetSubsystem<Input>();
  236. if (input->GetMouseButtonDown(MOUSEB_LEFT))
  237. mevent.modifiers |= EVENTFLAG_LEFT_MOUSE_BUTTON;
  238. if (input->GetMouseButtonDown(MOUSEB_MIDDLE))
  239. mevent.modifiers |= EVENTFLAG_MIDDLE_MOUSE_BUTTON;
  240. if (input->GetMouseButtonDown(MOUSEB_RIGHT))
  241. mevent.modifiers |= EVENTFLAG_RIGHT_MOUSE_BUTTON;
  242. host->SendMouseMoveEvent(mevent, mouseLeave);
  243. }
  244. void WebClient::SendMouseWheelEvent(int x, int y, unsigned modifier,int deltaX, int deltaY) const
  245. {
  246. if (!d_->browser_.get())
  247. return;
  248. CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
  249. CefMouseEvent mevent;
  250. mevent.x = x;
  251. mevent.y = y;
  252. mevent.modifiers = 0;
  253. #ifdef ATOMIC_PLATFORM_OSX
  254. deltaY = -deltaY;
  255. #endif
  256. host->SendMouseWheelEvent(mevent, deltaX, deltaY * 5);
  257. }
  258. /*
  259. EVENTFLAG_CAPS_LOCK_ON = 1 << 0,
  260. EVENTFLAG_SHIFT_DOWN = 1 << 1,
  261. EVENTFLAG_CONTROL_DOWN = 1 << 2,
  262. EVENTFLAG_ALT_DOWN = 1 << 3,
  263. EVENTFLAG_LEFT_MOUSE_BUTTON = 1 << 4,
  264. EVENTFLAG_MIDDLE_MOUSE_BUTTON = 1 << 5,
  265. EVENTFLAG_RIGHT_MOUSE_BUTTON = 1 << 6,
  266. // Mac OS-X command key.
  267. EVENTFLAG_COMMAND_DOWN = 1 << 7,
  268. EVENTFLAG_NUM_LOCK_ON = 1 << 8,
  269. EVENTFLAG_IS_KEY_PAD = 1 << 9,
  270. EVENTFLAG_IS_LEFT = 1 << 10,
  271. EVENTFLAG_IS_RIGHT = 1 << 11,
  272. } cef_event_flags_t;
  273. */
  274. void WebClient::SendKeyEvent(const StringHash eventType, VariantMap& eventData)
  275. {
  276. if (!d_->browser_.get())
  277. return;
  278. CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
  279. CefKeyEvent keyEvent;
  280. if (!ConvertKeyEvent(eventType, eventData, keyEvent))
  281. return;
  282. host->SendKeyEvent(keyEvent);
  283. #ifdef ATOMIC_PLATFORM_OSX
  284. // Send an empty key event on OSX, which seems to fix
  285. // keyboard problems on OSX with cefclient
  286. // ./cefclient --off-screen-rendering-enabled
  287. // return does not work at all on cef client with offscreen
  288. // bad interaction with arrow keys (for example here, after
  289. // hitting arrow keys, return/text takes a couple presses to register
  290. if (eventType == "KeyDown")
  291. keyEvent.type = KEYEVENT_KEYDOWN;
  292. else
  293. keyEvent.type = KEYEVENT_KEYUP;
  294. keyEvent.modifiers = 0;
  295. keyEvent.native_key_code = 0;
  296. host->SendKeyEvent(keyEvent);
  297. #endif
  298. }
  299. void WebClient::SendTextInputEvent(const StringHash eventType, VariantMap& eventData)
  300. {
  301. if (!d_->browser_.get())
  302. return;
  303. CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
  304. CefKeyEvent keyEvent;
  305. if (!ConvertTextInputEvent(eventType, eventData, keyEvent))
  306. return;
  307. host->SendKeyEvent(keyEvent);
  308. }
  309. void WebClient::SendFocusEvent(bool focus)
  310. {
  311. if (!d_->browser_.get())
  312. return;
  313. CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
  314. host->SendFocusEvent(focus);
  315. }
  316. // Navigation
  317. void WebClient::LoadURL(const String& url)
  318. {
  319. if (!d_->browser_.get())
  320. return;
  321. CefString _url(url.CString());
  322. d_->browser_->GetMainFrame()->LoadURL(_url);
  323. }
  324. void WebClient::GoBack()
  325. {
  326. if (!d_->browser_.get())
  327. return;
  328. d_->browser_->GoBack();
  329. }
  330. void WebClient::GoForward()
  331. {
  332. if (!d_->browser_.get())
  333. return;
  334. d_->browser_->GoForward();
  335. }
  336. bool WebClient::IsLoading()
  337. {
  338. if (!d_->browser_.get())
  339. return false;
  340. return d_->browser_->IsLoading();
  341. }
  342. void WebClient::Reload()
  343. {
  344. if (!d_->browser_.get())
  345. return;
  346. d_->browser_->Reload();
  347. }
  348. void WebClient::ShortcutCut()
  349. {
  350. if (!d_->browser_.get())
  351. return;
  352. d_->browser_->GetFocusedFrame()->Cut();
  353. }
  354. void WebClient::ShortcutCopy()
  355. {
  356. if (!d_->browser_.get())
  357. return;
  358. d_->browser_->GetFocusedFrame()->Copy();
  359. }
  360. void WebClient::ShortcutPaste()
  361. {
  362. if (!d_->browser_.get())
  363. return;
  364. d_->browser_->GetFocusedFrame()->Paste();
  365. }
  366. void WebClient::ShortcutSelectAll()
  367. {
  368. if (!d_->browser_.get())
  369. return;
  370. d_->browser_->GetFocusedFrame()->SelectAll();
  371. }
  372. void WebClient::ShortcutUndo()
  373. {
  374. if (!d_->browser_.get())
  375. return;
  376. d_->browser_->GetFocusedFrame()->Undo();
  377. }
  378. void WebClient::ShortcutRedo()
  379. {
  380. if (!d_->browser_.get())
  381. return;
  382. d_->browser_->GetFocusedFrame()->Redo();
  383. }
  384. void WebClient::ShortcutDelete()
  385. {
  386. if (!d_->browser_.get())
  387. return;
  388. d_->browser_->GetFocusedFrame()->Delete();
  389. }
  390. void WebClient::WasResized()
  391. {
  392. if (!d_->browser_.get())
  393. return;
  394. CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
  395. host->WasResized();;
  396. }
  397. bool WebClient::CreateBrowser(const String& initialURL, int width, int height)
  398. {
  399. return d_->CreateBrowser(initialURL, width, height);
  400. }
  401. void WebClient::SetSize(int width, int height)
  402. {
  403. if (renderHandler_.Null())
  404. return;
  405. if (renderHandler_->GetWidth() == width && renderHandler_->GetHeight() == height)
  406. return;
  407. renderHandler_->SetSize(width, height);
  408. WasResized();
  409. }
  410. void WebClient::SetWebRenderHandler(WebRenderHandler* handler)
  411. {
  412. handler->SetWebClient(this);
  413. renderHandler_ = handler;
  414. }
  415. CefClient* WebClient::GetCefClient()
  416. {
  417. return d_;
  418. }
  419. }