WebClient.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. #include <SDL/include/SDL.h>
  2. #include <ThirdParty/SDL/include/SDL_syswm.h>
  3. #include <ThirdParty/CEF/include/cef_app.h>
  4. #include <ThirdParty/CEF/include/cef_client.h>
  5. #include <ThirdParty/CEF/include/cef_browser.h>
  6. #include <ThirdParty/CEF/include/wrapper/cef_helpers.h>
  7. #include <ThirdParty/CEF/include/base/cef_bind.h>
  8. #include <ThirdParty/CEF/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. namespace Atomic
  17. {
  18. #ifdef ATOMIC_PLATFORM_OSX
  19. void* GetNSWindowContentView(void* window);
  20. #endif
  21. class WebClientPrivate : public CefClient, public CefLifeSpanHandler
  22. {
  23. friend class WebClient;
  24. public:
  25. WebClientPrivate(WebClient* client)
  26. {
  27. webClient_ = client;
  28. webBrowserHost_ = webClient_->GetSubsystem<WebBrowserHost>();
  29. }
  30. CefRefPtr<CefRenderHandler> GetRenderHandler() OVERRIDE
  31. {
  32. if (webClient_->renderHandler_.Null())
  33. return nullptr;
  34. return webClient_->renderHandler_->GetCEFRenderHandler();
  35. }
  36. virtual CefRefPtr<CefLifeSpanHandler> GetLifeSpanHandler() OVERRIDE
  37. {
  38. return this;
  39. }
  40. bool OnProcessMessageReceived(CefRefPtr<CefBrowser> browser,
  41. CefProcessId source_process,
  42. CefRefPtr<CefProcessMessage> message) OVERRIDE
  43. {
  44. return false;
  45. }
  46. bool CreateBrowser(const String& initialURL, int width, int height)
  47. {
  48. if (webClient_->renderHandler_.Null())
  49. {
  50. LOGERROR("WebClient::CreateBrowser - No render handler specified");
  51. return false;
  52. }
  53. CefWindowInfo windowInfo;
  54. CefBrowserSettings browserSettings;
  55. //browserSettings.webgl = STATE_ENABLED;
  56. windowInfo.width = width;
  57. windowInfo.height = height;
  58. Graphics* graphics = webClient_->GetSubsystem<Graphics>();
  59. SDL_Window* sdlWindow = static_cast<SDL_Window*>(graphics->GetSDLWindow());
  60. SDL_SysWMinfo info;
  61. SDL_VERSION(&info.version);
  62. if(SDL_GetWindowWMInfo(sdlWindow, &info))
  63. {
  64. NSView* view = (NSView*) GetNSWindowContentView(info.info.cocoa.window);
  65. windowInfo.SetAsWindowless(view, false);
  66. webClient_->renderHandler_->SetSize(width, height);
  67. CefRefPtr<CefBrowser> browser = CefBrowserHost::CreateBrowserSync(windowInfo, this,
  68. initialURL.CString(), browserSettings, nullptr);
  69. if (!browser.get())
  70. return false;
  71. browser_ = browser;
  72. return true;
  73. }
  74. return false;
  75. }
  76. // CefLifeSpanHandler methods:
  77. virtual void OnAfterCreated(CefRefPtr<CefBrowser> browser) OVERRIDE
  78. {
  79. CEF_REQUIRE_UI_THREAD();
  80. }
  81. virtual bool DoClose(CefRefPtr<CefBrowser> browser) OVERRIDE
  82. {
  83. return false;
  84. }
  85. virtual void OnBeforeClose(CefRefPtr<CefBrowser> browser) OVERRIDE
  86. {
  87. CEF_REQUIRE_UI_THREAD();
  88. if (browser->IsSame(browser_))
  89. browser_ = nullptr;
  90. }
  91. void CloseBrowser(bool force_close)
  92. {
  93. if (!CefCurrentlyOn(TID_UI))
  94. {
  95. // Execute on the UI thread.
  96. CefPostTask(TID_UI,
  97. base::Bind(&WebClientPrivate::CloseBrowser, this, force_close));
  98. return;
  99. }
  100. if (!browser_.get())
  101. return;
  102. browser_->GetHost()->CloseBrowser(force_close);
  103. }
  104. IMPLEMENT_REFCOUNTING(WebClientPrivate);
  105. private:
  106. CefRefPtr<CefBrowser> browser_;
  107. WeakPtr<WebBrowserHost> webBrowserHost_;
  108. WeakPtr<WebClient> webClient_;
  109. };
  110. WebClient::WebClient(Context* context) : Object(context)
  111. {
  112. d_ = new WebClientPrivate(this);
  113. }
  114. WebClient::~WebClient()
  115. {
  116. renderHandler_ = 0;
  117. //d_->Release();
  118. }
  119. void WebClient::SendMouseClickEvent(int x, int y, unsigned button, bool mouseUp, unsigned modifier) const
  120. {
  121. if (!d_->browser_.get())
  122. return;
  123. CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
  124. CefMouseEvent mevent;
  125. mevent.x = x;
  126. mevent.y = y;
  127. mevent.modifiers = 0;
  128. //MBT_LEFT = 0,
  129. //MBT_MIDDLE,
  130. //MBT_RIGHT,
  131. host->SendMouseClickEvent(mevent, (CefBrowserHost::MouseButtonType) button, mouseUp, 1);
  132. }
  133. void WebClient::SendMouseMoveEvent(int x, int y, unsigned modifier, bool mouseLeave) const
  134. {
  135. if (!d_->browser_.get())
  136. return;
  137. CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
  138. CefMouseEvent mevent;
  139. mevent.x = x;
  140. mevent.y = y;
  141. mevent.modifiers = 0;
  142. Input* input = GetSubsystem<Input>();
  143. if (input->GetMouseButtonDown(MOUSEB_LEFT))
  144. mevent.modifiers |= EVENTFLAG_LEFT_MOUSE_BUTTON;
  145. if (input->GetMouseButtonDown(MOUSEB_MIDDLE))
  146. mevent.modifiers |= EVENTFLAG_MIDDLE_MOUSE_BUTTON;
  147. if (input->GetMouseButtonDown(MOUSEB_RIGHT))
  148. mevent.modifiers |= EVENTFLAG_RIGHT_MOUSE_BUTTON;
  149. host->SendMouseMoveEvent(mevent, mouseLeave);
  150. }
  151. void WebClient::SendMouseWheelEvent(int x, int y, unsigned modifier,int deltaX, int deltaY) const
  152. {
  153. if (!d_->browser_.get())
  154. return;
  155. CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
  156. CefMouseEvent mevent;
  157. mevent.x = x;
  158. mevent.y = y;
  159. mevent.modifiers = 0;
  160. #ifdef ATOMIC_PLATFORM_OSX
  161. deltaY = -deltaY;
  162. #endif
  163. host->SendMouseWheelEvent(mevent, deltaX, deltaY * 5);
  164. }
  165. void WebClient::WasResized()
  166. {
  167. if (!d_->browser_.get())
  168. return;
  169. CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
  170. host->WasResized();;
  171. }
  172. bool WebClient::CreateBrowser(const String& initialURL, int width, int height)
  173. {
  174. return d_->CreateBrowser(initialURL, width, height);
  175. }
  176. void WebClient::SetSize(int width, int height)
  177. {
  178. if (renderHandler_.Null())
  179. return;
  180. if (renderHandler_->GetWidth() == width && renderHandler_->GetHeight() == height)
  181. return;
  182. renderHandler_->SetSize(width, height);
  183. WasResized();
  184. }
  185. void WebClient::SetWebRenderHandler(WebRenderHandler* handler)
  186. {
  187. handler->SetWebClient(this);
  188. renderHandler_ = handler;
  189. }
  190. CefClient* WebClient::GetCefClient()
  191. {
  192. return d_;
  193. }
  194. }