WebBrowserHost.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. //
  2. // Copyright (c) 2014-2016, THUNDERBEAST GAMES LLC All rights reserved
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include <SDL/include/SDL.h>
  23. #include <ThirdParty/SDL/include/SDL_syswm.h>
  24. #include <include/cef_app.h>
  25. #include <include/cef_client.h>
  26. #include <include/wrapper/cef_helpers.h>
  27. #include <include/base/cef_bind.h>
  28. #include <include/wrapper/cef_closure_task.h>
  29. #include <Atomic/Core/ProcessUtils.h>
  30. #include <Atomic/Core/CoreEvents.h>
  31. #include <Atomic/IO/Log.h>
  32. #include <Atomic/Graphics/Graphics.h>
  33. #include "Internal/WebAppBrowser.h"
  34. #include "WebSchemeHandler.h"
  35. #include "WebClient.h"
  36. #include "WebBrowserHost.h"
  37. #ifdef ATOMIC_PLATFORM_LINUX
  38. #include <X11/Xlib.h>
  39. #include <stdlib.h>
  40. #include <unistd.h>
  41. #include <signal.h>
  42. static int XErrorHandlerImpl(Display *display, XErrorEvent *event)
  43. {
  44. return 0;
  45. }
  46. static int XIOErrorHandlerImpl(Display *display)
  47. {
  48. return 0;
  49. }
  50. static void TerminationSignalHandler(int signatl)
  51. {
  52. }
  53. #endif
  54. namespace Atomic
  55. {
  56. class WebBrowserHostPrivate
  57. {
  58. friend class WebBrowserHost;
  59. public:
  60. WebBrowserHostPrivate(WebBrowserHost* host)
  61. {
  62. host_ = host;
  63. app_ = new WebAppBrowser();
  64. }
  65. virtual ~WebBrowserHostPrivate()
  66. {
  67. host_ = 0;
  68. }
  69. private:
  70. WeakPtr<WebBrowserHost> host_;
  71. CefRefPtr<CefApp> app_;
  72. };
  73. WebBrowserHost::WebBrowserHost(Context* context) : Object (context)
  74. {
  75. const Vector<String>& arguments = GetArguments();
  76. #ifdef ATOMIC_PLATFORM_LINUX
  77. XSetErrorHandler(XErrorHandlerImpl);
  78. XSetIOErrorHandler(XIOErrorHandlerImpl);
  79. // Install a signal handler so we clean up after ourselves.
  80. signal(SIGINT, TerminationSignalHandler);
  81. signal(SIGTERM, TerminationSignalHandler);
  82. #endif
  83. // IMPORTANT: Cef::App contains virtual void OnBeforeCommandLineProcessing(), which should make it possible
  84. // to setup args on Windows
  85. #ifdef ATOMIC_PLATFORM_OSX
  86. const char* _argv[3] = { "", "--enable-media-stream", "--enable-usermedia-screen-capturing" };
  87. CefMainArgs args(3, (char**) &_argv);
  88. #elif ATOMIC_PLATFORM_LINUX
  89. static const char* _argv[3] = { "AtomicWebView", "--disable-setuid-sandbox", "--off-screen-rendering-enabled" };
  90. CefMainArgs args(3, (char**) &_argv);
  91. #else
  92. CefMainArgs args;
  93. #endif
  94. CefSettings settings;
  95. settings.windowless_rendering_enabled = true;
  96. d_ = new WebBrowserHostPrivate(this);
  97. // If losing OSX system menu, it means we're calling this
  98. // before initializing graphics subsystem
  99. if (!CefInitialize(args, settings, d_->app_, nullptr))
  100. {
  101. LOGERROR("CefInitialize - Error");
  102. }
  103. RegisterWebSchemeHandlers(this);
  104. SubscribeToEvent(E_BEGINFRAME, HANDLER(WebBrowserHost, HandleBeginFrame));
  105. }
  106. WebBrowserHost::~WebBrowserHost()
  107. {
  108. CefClearSchemeHandlerFactories();
  109. CefShutdown();
  110. }
  111. void WebBrowserHost::HandleBeginFrame(StringHash eventType, VariantMap& eventData)
  112. {
  113. CefDoMessageLoopWork();
  114. }
  115. }