WebBrowserHost.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 "WebViewEvents.h"
  35. #include "WebSchemeHandler.h"
  36. #include "WebClient.h"
  37. #include "WebBrowserHost.h"
  38. #ifdef ATOMIC_PLATFORM_LINUX
  39. #include <X11/Xlib.h>
  40. #include <stdlib.h>
  41. #include <unistd.h>
  42. #include <signal.h>
  43. static int XErrorHandlerImpl(Display *display, XErrorEvent *event)
  44. {
  45. return 0;
  46. }
  47. static int XIOErrorHandlerImpl(Display *display)
  48. {
  49. return 0;
  50. }
  51. static void TerminationSignalHandler(int signatl)
  52. {
  53. }
  54. #endif
  55. namespace Atomic
  56. {
  57. class WebBrowserHostPrivate
  58. {
  59. friend class WebBrowserHost;
  60. public:
  61. WebBrowserHostPrivate(WebBrowserHost* host)
  62. {
  63. host_ = host;
  64. app_ = new WebAppBrowser();
  65. }
  66. virtual ~WebBrowserHostPrivate()
  67. {
  68. host_ = 0;
  69. }
  70. private:
  71. WeakPtr<WebBrowserHost> host_;
  72. CefRefPtr<CefApp> app_;
  73. };
  74. GlobalPropertyMap WebBrowserHost::globalProperties_;
  75. WeakPtr<WebBrowserHost> WebBrowserHost::instance_;
  76. String WebBrowserHost::userAgent_;
  77. String WebBrowserHost::productVersion_;
  78. int WebBrowserHost::debugPort_ = 3335;
  79. WebBrowserHost::WebBrowserHost(Context* context) : Object (context)
  80. {
  81. const Vector<String>& arguments = GetArguments();
  82. #ifdef ATOMIC_PLATFORM_LINUX
  83. XSetErrorHandler(XErrorHandlerImpl);
  84. XSetIOErrorHandler(XIOErrorHandlerImpl);
  85. // Install a signal handler so we clean up after ourselves.
  86. signal(SIGINT, TerminationSignalHandler);
  87. signal(SIGTERM, TerminationSignalHandler);
  88. #endif
  89. // IMPORTANT: See flags being set in implementation of void WebAppBrowser::OnBeforeCommandLineProcessing
  90. // these include "--enable-media-stream", "--enable-usermedia-screen-capturing", "--off-screen-rendering-enabled", "--transparent-painting-enabled"
  91. #ifdef ATOMIC_PLATFORM_LINUX
  92. static const char* _argv[3] = { "AtomicWebView", "--disable-setuid-sandbox" };
  93. CefMainArgs args(3, (char**) &_argv);
  94. #else
  95. CefMainArgs args;
  96. #endif
  97. CefSettings settings;
  98. settings.windowless_rendering_enabled = 1;
  99. // default background is white, add a setting
  100. // settings.background_color = 0;
  101. if (productVersion_.Length())
  102. {
  103. CefString(&settings.product_version).FromASCII(productVersion_.CString());
  104. }
  105. if (userAgent_.Length())
  106. {
  107. CefString(&settings.user_agent).FromASCII(userAgent_.CString());
  108. }
  109. settings.remote_debugging_port = debugPort_;
  110. d_ = new WebBrowserHostPrivate(this);
  111. // If losing OSX system menu, it means we're calling this
  112. // before initializing graphics subsystem
  113. if (!CefInitialize(args, settings, d_->app_, nullptr))
  114. {
  115. LOGERROR("CefInitialize - Error");
  116. }
  117. RegisterWebSchemeHandlers(this);
  118. SubscribeToEvent(E_UPDATE, HANDLER(WebBrowserHost, HandleUpdate));
  119. instance_ = this;
  120. }
  121. WebBrowserHost::~WebBrowserHost()
  122. {
  123. instance_ = 0;
  124. CefClearSchemeHandlerFactories();
  125. CefShutdown();
  126. }
  127. void WebBrowserHost::SetGlobalBoolProperty(const String& globalVar, const String& property, bool value)
  128. {
  129. Variant v(value);
  130. SetGlobalProperty(globalVar, property, v);
  131. }
  132. void WebBrowserHost::SetGlobalStringProperty(const String& globalVar, const String& property, const String& value)
  133. {
  134. Variant v(value);
  135. SetGlobalProperty(globalVar, property, v);
  136. }
  137. void WebBrowserHost::SetGlobalNumberProperty(const String& globalVar, const String& property, double value)
  138. {
  139. Variant v(value);
  140. SetGlobalProperty(globalVar, property, v);
  141. }
  142. void WebBrowserHost::SetGlobalProperty(const String& globalVar, const String &property, Variant& value)
  143. {
  144. globalProperties_[globalVar][property] = value;
  145. if (!instance_.NotNull())
  146. instance_->SendEvent(E_WEBVIEWGLOBALPROPERTIESCHANGED);
  147. }
  148. void WebBrowserHost::HandleUpdate(StringHash eventType, VariantMap& eventData)
  149. {
  150. CefDoMessageLoopWork();
  151. }
  152. }