WebBrowserHost.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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_render_handler.h>
  6. #include <ThirdParty/CEF/include/wrapper/cef_helpers.h>
  7. #include <Atomic/Core/ProcessUtils.h>
  8. #include <Atomic/Core/CoreEvents.h>
  9. #include <Atomic/IO/Log.h>
  10. #include <Atomic/Graphics/Graphics.h>
  11. #include "WebClient.h"
  12. #include "WebBrowserHost.h"
  13. class SimpleApp : public CefApp,
  14. public CefBrowserProcessHandler {
  15. public:
  16. SimpleApp();
  17. // CefApp methods:
  18. virtual CefRefPtr<CefBrowserProcessHandler> GetBrowserProcessHandler()
  19. OVERRIDE { return this; }
  20. // CefBrowserProcessHandler methods:
  21. virtual void OnContextInitialized() OVERRIDE;
  22. private:
  23. // Include the default reference counting implementation.
  24. IMPLEMENT_REFCOUNTING(SimpleApp);
  25. };
  26. SimpleApp::SimpleApp() {
  27. }
  28. void SimpleApp::OnContextInitialized() {
  29. CEF_REQUIRE_UI_THREAD();
  30. }
  31. namespace Atomic
  32. {
  33. #ifdef ATOMIC_PLATFORM_OSX
  34. void* GetNSWindowContentView(void* window);
  35. #endif
  36. WebBrowserHost::WebBrowserHost(Context* context) : Object (context)
  37. {
  38. const Vector<String>& arguments = GetArguments();
  39. const char** _argv { 0 };
  40. PODVector<const char*> argv;
  41. for (unsigned i = 0; i < arguments.Size(); i++)
  42. argv.Push(arguments[i].CString());
  43. CefMainArgs args(arguments.Size(), arguments.Size() ? (char**) &argv[0] : (char **) _argv);
  44. int result = CefExecuteProcess(args, nullptr, nullptr);
  45. if (result >= 0)
  46. {
  47. LOGERROR("CEFExecuteProcess - Error");
  48. }
  49. CefSettings settings;
  50. settings.windowless_rendering_enabled = true;
  51. // If losing OSX system menu, it means we're calling this
  52. // before initializing graphics subsystem
  53. if (!CefInitialize(args, settings, nullptr, nullptr))
  54. {
  55. LOGERROR("CefInitialize - Error");
  56. }
  57. SubscribeToEvent(E_BEGINFRAME, HANDLER(WebBrowserHost, HandleBeginFrame));
  58. }
  59. WebBrowserHost::~WebBrowserHost()
  60. {
  61. CefShutdown();
  62. }
  63. bool WebBrowserHost::CreateBrowser(WebClient* webClient)
  64. {
  65. CefWindowInfo windowInfo;
  66. CefBrowserSettings browserSettings;
  67. Graphics* graphics = GetSubsystem<Graphics>();
  68. SDL_Window* sdlWindow = static_cast<SDL_Window*>(graphics->GetSDLWindow());
  69. SDL_SysWMinfo info;
  70. SDL_VERSION(&info.version);
  71. if(SDL_GetWindowWMInfo(sdlWindow, &info))
  72. {
  73. NSView* view = (NSView*) GetNSWindowContentView(info.info.cocoa.window);
  74. windowInfo.SetAsWindowless(view, false);
  75. return CefBrowserHost::CreateBrowser(windowInfo, (CefClient*) webClient->d_,
  76. "https://html5test.com/", browserSettings, nullptr);
  77. }
  78. return false;
  79. }
  80. void WebBrowserHost::HandleBeginFrame(StringHash eventType, VariantMap& eventData)
  81. {
  82. CefDoMessageLoopWork();
  83. }
  84. }