WebClient.cpp 11 KB

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