WebClient.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  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. ///
  127. // Called to display a console message. Return true to stop the message from
  128. // being output to the console.
  129. ///
  130. /*--cef(optional_param=message,optional_param=source)--*/
  131. virtual bool OnConsoleMessage(CefRefPtr<CefBrowser> browser,
  132. const CefString& message,
  133. const CefString& source,
  134. int line) OVERRIDE
  135. {
  136. if (webClient_.Null())
  137. return false;
  138. String _message;
  139. ConvertCEFString(message, _message);
  140. String _source;
  141. ConvertCEFString(source, _source);
  142. LOGINFOF("WebViewJS: %s (%s:%i)", _message.CString(), _source.CString(), line);
  143. return false;
  144. }
  145. bool OnProcessMessageReceived(CefRefPtr<CefBrowser> browser,
  146. CefProcessId source_process,
  147. CefRefPtr<CefProcessMessage> message) OVERRIDE
  148. {
  149. return false;
  150. }
  151. bool CreateBrowser(const String& initialURL, int width, int height)
  152. {
  153. if (webClient_->renderHandler_.Null())
  154. {
  155. LOGERROR("WebClient::CreateBrowser - No render handler specified");
  156. return false;
  157. }
  158. CefWindowInfo windowInfo;
  159. CefBrowserSettings browserSettings;
  160. browserSettings.webgl = STATE_ENABLED;
  161. windowInfo.width = width;
  162. windowInfo.height = height;
  163. Graphics* graphics = webClient_->GetSubsystem<Graphics>();
  164. SDL_Window* sdlWindow = static_cast<SDL_Window*>(graphics->GetSDLWindow());
  165. SDL_SysWMinfo info;
  166. SDL_VERSION(&info.version);
  167. if(SDL_GetWindowWMInfo(sdlWindow, &info))
  168. {
  169. #ifdef ATOMIC_PLATFORM_OSX
  170. NSView* view = (NSView*) GetNSWindowContentView(info.info.cocoa.window);
  171. windowInfo.SetAsWindowless(view, false);
  172. #endif
  173. #ifdef ATOMIC_PLATFORM_WINDOWS
  174. windowInfo.SetAsWindowless(info.info.win.window, false);
  175. #endif
  176. webClient_->renderHandler_->SetSize(width, height);
  177. CefRefPtr<CefBrowser> browser = CefBrowserHost::CreateBrowserSync(windowInfo, this,
  178. initialURL.CString(), browserSettings, nullptr);
  179. if (!browser.get())
  180. return false;
  181. browser_ = browser;
  182. return true;
  183. }
  184. return false;
  185. }
  186. // CefLifeSpanHandler methods:
  187. virtual void OnAfterCreated(CefRefPtr<CefBrowser> browser) OVERRIDE
  188. {
  189. CEF_REQUIRE_UI_THREAD();
  190. }
  191. virtual bool DoClose(CefRefPtr<CefBrowser> browser) OVERRIDE
  192. {
  193. return false;
  194. }
  195. virtual void OnBeforeClose(CefRefPtr<CefBrowser> browser) OVERRIDE
  196. {
  197. CEF_REQUIRE_UI_THREAD();
  198. if (browser->IsSame(browser_))
  199. browser_ = nullptr;
  200. }
  201. void CloseBrowser(bool force_close)
  202. {
  203. if (!CefCurrentlyOn(TID_UI))
  204. {
  205. // Execute on the UI thread.
  206. CefPostTask(TID_UI,
  207. base::Bind(&WebClientPrivate::CloseBrowser, this, force_close));
  208. return;
  209. }
  210. if (!browser_.get())
  211. return;
  212. browser_->GetHost()->CloseBrowser(force_close);
  213. }
  214. IMPLEMENT_REFCOUNTING(WebClientPrivate);
  215. private:
  216. CefRefPtr<CefBrowser> browser_;
  217. WeakPtr<WebBrowserHost> webBrowserHost_;
  218. WeakPtr<WebClient> webClient_;
  219. };
  220. WebClient::WebClient(Context* context) : Object(context)
  221. {
  222. d_ = new WebClientPrivate(this);
  223. }
  224. WebClient::~WebClient()
  225. {
  226. if (d_)
  227. d_->CloseBrowser(true);
  228. renderHandler_ = 0;
  229. //d_->Release();
  230. }
  231. void WebClient::SendMouseClickEvent(int x, int y, unsigned button, bool mouseUp, unsigned modifier) 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. //MBT_LEFT = 0,
  241. //MBT_MIDDLE,
  242. //MBT_RIGHT,
  243. host->SendMouseClickEvent(mevent, (CefBrowserHost::MouseButtonType) button, mouseUp, 1);
  244. }
  245. void WebClient::SendMousePressEvent(int x, int y, unsigned button, unsigned modifier) const
  246. {
  247. SendMouseClickEvent(x, y, button, false, modifier);
  248. SendMouseClickEvent(x, y, button, true, modifier);
  249. }
  250. void WebClient::SendMouseMoveEvent(int x, int y, unsigned modifier, bool mouseLeave) const
  251. {
  252. if (!d_->browser_.get())
  253. return;
  254. CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
  255. CefMouseEvent mevent;
  256. mevent.x = x;
  257. mevent.y = y;
  258. mevent.modifiers = 0;
  259. Input* input = GetSubsystem<Input>();
  260. if (input->GetMouseButtonDown(MOUSEB_LEFT))
  261. mevent.modifiers |= EVENTFLAG_LEFT_MOUSE_BUTTON;
  262. if (input->GetMouseButtonDown(MOUSEB_MIDDLE))
  263. mevent.modifiers |= EVENTFLAG_MIDDLE_MOUSE_BUTTON;
  264. if (input->GetMouseButtonDown(MOUSEB_RIGHT))
  265. mevent.modifiers |= EVENTFLAG_RIGHT_MOUSE_BUTTON;
  266. host->SendMouseMoveEvent(mevent, mouseLeave);
  267. }
  268. void WebClient::SendMouseWheelEvent(int x, int y, unsigned modifier,int deltaX, int deltaY) const
  269. {
  270. if (!d_->browser_.get())
  271. return;
  272. CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
  273. CefMouseEvent mevent;
  274. mevent.x = x;
  275. mevent.y = y;
  276. mevent.modifiers = 0;
  277. #ifdef ATOMIC_PLATFORM_OSX
  278. deltaY = -deltaY;
  279. #endif
  280. host->SendMouseWheelEvent(mevent, deltaX, deltaY * 5);
  281. }
  282. /*
  283. EVENTFLAG_CAPS_LOCK_ON = 1 << 0,
  284. EVENTFLAG_SHIFT_DOWN = 1 << 1,
  285. EVENTFLAG_CONTROL_DOWN = 1 << 2,
  286. EVENTFLAG_ALT_DOWN = 1 << 3,
  287. EVENTFLAG_LEFT_MOUSE_BUTTON = 1 << 4,
  288. EVENTFLAG_MIDDLE_MOUSE_BUTTON = 1 << 5,
  289. EVENTFLAG_RIGHT_MOUSE_BUTTON = 1 << 6,
  290. // Mac OS-X command key.
  291. EVENTFLAG_COMMAND_DOWN = 1 << 7,
  292. EVENTFLAG_NUM_LOCK_ON = 1 << 8,
  293. EVENTFLAG_IS_KEY_PAD = 1 << 9,
  294. EVENTFLAG_IS_LEFT = 1 << 10,
  295. EVENTFLAG_IS_RIGHT = 1 << 11,
  296. } cef_event_flags_t;
  297. */
  298. void WebClient::SendKeyEvent(const StringHash eventType, VariantMap& eventData)
  299. {
  300. if (!d_->browser_.get())
  301. return;
  302. CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
  303. CefKeyEvent keyEvent;
  304. if (!ConvertKeyEvent(eventType, eventData, keyEvent))
  305. return;
  306. host->SendKeyEvent(keyEvent);
  307. #ifdef ATOMIC_PLATFORM_OSX
  308. // Send an empty key event on OSX, which seems to fix
  309. // keyboard problems on OSX with cefclient
  310. // ./cefclient --off-screen-rendering-enabled
  311. // return does not work at all on cef client with offscreen
  312. // bad interaction with arrow keys (for example here, after
  313. // hitting arrow keys, return/text takes a couple presses to register
  314. if (eventType == "KeyDown")
  315. keyEvent.type = KEYEVENT_KEYDOWN;
  316. else
  317. keyEvent.type = KEYEVENT_KEYUP;
  318. keyEvent.modifiers = 0;
  319. keyEvent.native_key_code = 0;
  320. host->SendKeyEvent(keyEvent);
  321. #endif
  322. }
  323. void WebClient::SendTextInputEvent(const StringHash eventType, VariantMap& eventData)
  324. {
  325. if (!d_->browser_.get())
  326. return;
  327. CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
  328. CefKeyEvent keyEvent;
  329. if (!ConvertTextInputEvent(eventType, eventData, keyEvent))
  330. return;
  331. host->SendKeyEvent(keyEvent);
  332. }
  333. void WebClient::SendFocusEvent(bool focus)
  334. {
  335. if (!d_->browser_.get())
  336. return;
  337. CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
  338. host->SendFocusEvent(focus);
  339. }
  340. // Javascript
  341. void WebClient::ExecuteJavaScript(const String& script)
  342. {
  343. if (!d_->browser_.get())
  344. return;
  345. d_->browser_->GetMainFrame()->ExecuteJavaScript(CefString(script.CString()), "", 0);
  346. }
  347. // Navigation
  348. void WebClient::LoadURL(const String& url)
  349. {
  350. if (!d_->browser_.get())
  351. {
  352. return;
  353. }
  354. CefString _url(url.CString());
  355. d_->browser_->GetMainFrame()->LoadURL(_url);
  356. }
  357. void WebClient::GoBack()
  358. {
  359. if (!d_->browser_.get())
  360. return;
  361. d_->browser_->GoBack();
  362. }
  363. void WebClient::GoForward()
  364. {
  365. if (!d_->browser_.get())
  366. return;
  367. d_->browser_->GoForward();
  368. }
  369. bool WebClient::IsLoading()
  370. {
  371. if (!d_->browser_.get())
  372. return false;
  373. return d_->browser_->IsLoading();
  374. }
  375. void WebClient::Reload()
  376. {
  377. if (!d_->browser_.get())
  378. return;
  379. d_->browser_->Reload();
  380. }
  381. void WebClient::ShortcutCut()
  382. {
  383. if (!d_->browser_.get())
  384. return;
  385. d_->browser_->GetFocusedFrame()->Cut();
  386. }
  387. void WebClient::ShortcutCopy()
  388. {
  389. if (!d_->browser_.get())
  390. return;
  391. d_->browser_->GetFocusedFrame()->Copy();
  392. }
  393. void WebClient::ShortcutPaste()
  394. {
  395. if (!d_->browser_.get())
  396. return;
  397. d_->browser_->GetFocusedFrame()->Paste();
  398. }
  399. void WebClient::ShortcutSelectAll()
  400. {
  401. if (!d_->browser_.get())
  402. return;
  403. d_->browser_->GetFocusedFrame()->SelectAll();
  404. }
  405. void WebClient::ShortcutUndo()
  406. {
  407. if (!d_->browser_.get())
  408. return;
  409. d_->browser_->GetFocusedFrame()->Undo();
  410. }
  411. void WebClient::ShortcutRedo()
  412. {
  413. if (!d_->browser_.get())
  414. return;
  415. d_->browser_->GetFocusedFrame()->Redo();
  416. }
  417. void WebClient::ShortcutDelete()
  418. {
  419. if (!d_->browser_.get())
  420. return;
  421. d_->browser_->GetFocusedFrame()->Delete();
  422. }
  423. void WebClient::WasResized()
  424. {
  425. if (!d_->browser_.get())
  426. return;
  427. CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
  428. host->WasResized();;
  429. }
  430. bool WebClient::CreateBrowser(const String& initialURL, int width, int height)
  431. {
  432. return d_->CreateBrowser(initialURL, width, height);
  433. }
  434. void WebClient::SetSize(int width, int height)
  435. {
  436. if (renderHandler_.Null())
  437. return;
  438. if (renderHandler_->GetWidth() == width && renderHandler_->GetHeight() == height)
  439. return;
  440. renderHandler_->SetSize(width, height);
  441. WasResized();
  442. }
  443. void WebClient::SetWebRenderHandler(WebRenderHandler* handler)
  444. {
  445. handler->SetWebClient(this);
  446. renderHandler_ = handler;
  447. }
  448. CefClient* WebClient::GetCefClient()
  449. {
  450. return d_;
  451. }
  452. }