WebClient.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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()
  46. {
  47. CefWindowInfo windowInfo;
  48. CefBrowserSettings browserSettings;
  49. Graphics* graphics = webClient_->GetSubsystem<Graphics>();
  50. SDL_Window* sdlWindow = static_cast<SDL_Window*>(graphics->GetSDLWindow());
  51. SDL_SysWMinfo info;
  52. SDL_VERSION(&info.version);
  53. if(SDL_GetWindowWMInfo(sdlWindow, &info))
  54. {
  55. NSView* view = (NSView*) GetNSWindowContentView(info.info.cocoa.window);
  56. windowInfo.SetAsWindowless(view, false);
  57. CefRefPtr<CefBrowser> browser = CefBrowserHost::CreateBrowserSync(windowInfo, this,
  58. "https://html5test.com/", browserSettings, nullptr);
  59. if (!browser.get())
  60. return false;
  61. browsers_.Push(browser);
  62. return true;
  63. }
  64. return false;
  65. }
  66. // CefLifeSpanHandler methods:
  67. virtual void OnAfterCreated(CefRefPtr<CefBrowser> browser) OVERRIDE
  68. {
  69. CEF_REQUIRE_UI_THREAD();
  70. }
  71. virtual bool DoClose(CefRefPtr<CefBrowser> browser) OVERRIDE
  72. {
  73. return false;
  74. }
  75. virtual void OnBeforeClose(CefRefPtr<CefBrowser> browser) OVERRIDE
  76. {
  77. CEF_REQUIRE_UI_THREAD();
  78. // Remove from the list of existing browsers.
  79. Vector<CefRefPtr<CefBrowser>>::Iterator itr = browsers_.Begin();
  80. while (itr != browsers_.End())
  81. {
  82. if ((*itr)->IsSame(browser))
  83. {
  84. browsers_.Erase(itr);
  85. break;
  86. }
  87. itr++;
  88. }
  89. }
  90. void CloseAllBrowsers(bool force_close)
  91. {
  92. if (!CefCurrentlyOn(TID_UI))
  93. {
  94. // Execute on the UI thread.
  95. CefPostTask(TID_UI,
  96. base::Bind(&WebClientPrivate::CloseAllBrowsers, this, force_close));
  97. return;
  98. }
  99. if (!browsers_.Size())
  100. return;
  101. // make a copy of vector, as we'll be erasing as we go
  102. Vector<CefRefPtr<CefBrowser>> browsers = browsers_;
  103. Vector<CefRefPtr<CefBrowser>>::Iterator itr = browsers.Begin();
  104. while (itr != browsers.End())
  105. {
  106. (*itr)->GetHost()->CloseBrowser(force_close);
  107. itr++;
  108. }
  109. browsers_.Clear();
  110. }
  111. IMPLEMENT_REFCOUNTING(WebClientPrivate);
  112. private:
  113. Vector<CefRefPtr<CefBrowser>> browsers_;
  114. WeakPtr<WebBrowserHost> webBrowserHost_;
  115. WeakPtr<WebClient> webClient_;
  116. };
  117. WebClient::WebClient(Context* context) : Object(context)
  118. {
  119. d_ = new WebClientPrivate(this);
  120. }
  121. WebClient::~WebClient()
  122. {
  123. renderHandler_ = 0;
  124. //d_->Release();
  125. }
  126. void WebClient::SendMouseClickEvent(int x, int y, unsigned button, bool mouseUp, unsigned modifier) const
  127. {
  128. if (!d_->browsers_.Size())
  129. return;
  130. CefRefPtr<CefBrowser> browser = d_->browsers_[0];
  131. CefRefPtr<CefBrowserHost> host = browser->GetHost();
  132. CefMouseEvent mevent;
  133. mevent.x = x;
  134. mevent.y = y;
  135. mevent.modifiers = 0;
  136. //MBT_LEFT = 0,
  137. //MBT_MIDDLE,
  138. //MBT_RIGHT,
  139. host->SendMouseClickEvent(mevent, (CefBrowserHost::MouseButtonType) button, mouseUp, 1);
  140. }
  141. void WebClient::SendMouseMoveEvent(int x, int y, unsigned modifier, bool mouseLeave) const
  142. {
  143. if (!d_->browsers_.Size())
  144. return;
  145. CefRefPtr<CefBrowser> browser = d_->browsers_[0];
  146. CefRefPtr<CefBrowserHost> host = browser->GetHost();
  147. CefMouseEvent mevent;
  148. mevent.x = x;
  149. mevent.y = y;
  150. mevent.modifiers = 0;
  151. //MBT_LEFT = 0,
  152. //MBT_MIDDLE,
  153. //MBT_RIGHT,
  154. host->SendMouseMoveEvent(mevent, mouseLeave);
  155. }
  156. void WebClient::WasResized()
  157. {
  158. if (!d_->browsers_.Size())
  159. return;
  160. d_->browsers_[0]->GetHost()->WasResized();;
  161. }
  162. bool WebClient::CreateBrowser()
  163. {
  164. return d_->CreateBrowser();
  165. }
  166. void WebClient::SetWebRenderHandler(WebRenderHandler* handler)
  167. {
  168. handler->SetWebClient(this);
  169. renderHandler_ = handler;
  170. }
  171. CefClient* WebClient::GetCefClient()
  172. {
  173. return d_;
  174. }
  175. }