WebClient.cpp 5.9 KB

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