WebBrowserHost.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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/IO/FileSystem.h>
  33. #include <Atomic/Graphics/Graphics.h>
  34. #include "Internal/WebAppBrowser.h"
  35. #include "WebViewEvents.h"
  36. #include "WebSchemeHandler.h"
  37. #include "WebClient.h"
  38. #include "WebBrowserHost.h"
  39. #ifdef ATOMIC_PLATFORM_LINUX
  40. #include <X11/Xlib.h>
  41. #include <stdlib.h>
  42. #include <unistd.h>
  43. #include <signal.h>
  44. static int XErrorHandlerImpl(Display *display, XErrorEvent *event)
  45. {
  46. if ( display && event )
  47. {
  48. char msg[132];
  49. XGetErrorText(display, event->error_code, msg, sizeof(msg));
  50. fprintf(stderr, "X11 Error %d (%s): request %d.%d \n",
  51. event->error_code,
  52. msg,
  53. event->request_code,
  54. event->minor_code );
  55. }
  56. return 0;
  57. }
  58. static int XIOErrorHandlerImpl(Display *display)
  59. {
  60. if ( display )
  61. fprintf(stderr, "XIO Error on display %p, quitting.\n", (void*)display );
  62. return 0;
  63. }
  64. static void TerminationSignalHandler(int signatl)
  65. {
  66. fprintf(stderr,"Received signal %d, quitting.\n", signatl );
  67. exit(0);
  68. }
  69. #endif
  70. namespace Atomic
  71. {
  72. class WebBrowserHostPrivate
  73. {
  74. friend class WebBrowserHost;
  75. public:
  76. WebBrowserHostPrivate(WebBrowserHost* host)
  77. {
  78. host_ = host;
  79. app_ = new WebAppBrowser();
  80. }
  81. virtual ~WebBrowserHostPrivate()
  82. {
  83. host_ = 0;
  84. }
  85. private:
  86. WeakPtr<WebBrowserHost> host_;
  87. CefRefPtr<CefApp> app_;
  88. };
  89. GlobalPropertyMap WebBrowserHost::globalProperties_;
  90. WeakPtr<WebBrowserHost> WebBrowserHost::instance_;
  91. String WebBrowserHost::rootCacheFolder_;
  92. String WebBrowserHost::cacheName_;
  93. String WebBrowserHost::userAgent_;
  94. String WebBrowserHost::productVersion_;
  95. String WebBrowserHost::jsMessageQueryFunctionName_ = "atomicQuery";
  96. String WebBrowserHost::jsMessageQueryCancelFunctionName_ = "atomicQueryCancel";
  97. int WebBrowserHost::debugPort_ = 3335;
  98. bool WebBrowserHost::webSecurity_ = true;
  99. WebBrowserHost::WebBrowserHost(Context* context) : Object (context)
  100. {
  101. const Vector<String>& arguments = GetArguments();
  102. #ifdef ATOMIC_PLATFORM_LINUX
  103. XSetErrorHandler(XErrorHandlerImpl);
  104. XSetIOErrorHandler(XIOErrorHandlerImpl);
  105. // Install a signal handler so we clean up after ourselves.
  106. signal(SIGINT, TerminationSignalHandler);
  107. signal(SIGTERM, TerminationSignalHandler);
  108. #endif
  109. // IMPORTANT: See flags being set in implementation of void WebAppBrowser::OnBeforeCommandLineProcessing
  110. // these include "--enable-media-stream", "--enable-usermedia-screen-capturing", "--off-screen-rendering-enabled", "--transparent-painting-enabled"
  111. #ifdef ATOMIC_PLATFORM_LINUX
  112. static const char* _argv[2] = { "AtomicWebView", "--disable-setuid-sandbox" };
  113. CefMainArgs args(2, (char**) &_argv);
  114. #else
  115. CefMainArgs args;
  116. #endif
  117. CefSettings settings;
  118. settings.windowless_rendering_enabled = 1;
  119. // default background is white, add a setting
  120. // settings.background_color = 0;
  121. if (productVersion_.Length())
  122. {
  123. CefString(&settings.product_version).FromASCII(productVersion_.CString());
  124. }
  125. if (userAgent_.Length())
  126. {
  127. CefString(&settings.user_agent).FromASCII(userAgent_.CString());
  128. }
  129. FileSystem* fs = GetSubsystem<FileSystem>();
  130. String fullPath;
  131. // If we've specified the absolute path to a root cache folder, use it
  132. if (rootCacheFolder_.Length() && cacheName_.Length())
  133. {
  134. fullPath = rootCacheFolder_ + "/" + cacheName_;
  135. CefString(&settings.cache_path).FromASCII(fullPath.CString());
  136. }
  137. else
  138. {
  139. fullPath = fs->GetAppPreferencesDir(cacheName_.CString(), "WebCache");
  140. }
  141. CefString(&settings.cache_path).FromASCII(fullPath.CString());
  142. settings.remote_debugging_port = debugPort_;
  143. d_ = new WebBrowserHostPrivate(this);
  144. // If losing OSX system menu, it means we're calling this
  145. // before initializing graphics subsystem
  146. if (!CefInitialize(args, settings, d_->app_, nullptr))
  147. {
  148. LOGERROR("CefInitialize - Error");
  149. }
  150. RegisterWebSchemeHandlers(this);
  151. // Ensure cookie manager is created
  152. CefCookieManager::GetGlobalManager(nullptr);
  153. SubscribeToEvent(E_UPDATE, HANDLER(WebBrowserHost, HandleUpdate));
  154. instance_ = this;
  155. }
  156. WebBrowserHost::~WebBrowserHost()
  157. {
  158. instance_ = 0;
  159. // TODO: Better place for this? If there are issues with cookies not persisting
  160. // it is possible the async nature of this call could be a culprit
  161. CefRefPtr<CefCookieManager> manager = CefCookieManager::GetGlobalManager(nullptr);
  162. if (manager.get())
  163. manager->FlushStore(nullptr);
  164. CefClearSchemeHandlerFactories();
  165. CefShutdown();
  166. }
  167. void WebBrowserHost::SetGlobalBoolProperty(const String& globalVar, const String& property, bool value)
  168. {
  169. Variant v(value);
  170. SetGlobalProperty(globalVar, property, v);
  171. }
  172. void WebBrowserHost::SetGlobalStringProperty(const String& globalVar, const String& property, const String& value)
  173. {
  174. Variant v(value);
  175. SetGlobalProperty(globalVar, property, v);
  176. }
  177. void WebBrowserHost::SetGlobalNumberProperty(const String& globalVar, const String& property, double value)
  178. {
  179. Variant v(value);
  180. SetGlobalProperty(globalVar, property, v);
  181. }
  182. void WebBrowserHost::SetGlobalProperty(const String& globalVar, const String &property, Variant& value)
  183. {
  184. globalProperties_[globalVar][property] = value;
  185. if (!instance_.NotNull())
  186. instance_->SendEvent(E_WEBVIEWGLOBALPROPERTIESCHANGED);
  187. }
  188. void WebBrowserHost::HandleUpdate(StringHash eventType, VariantMap& eventData)
  189. {
  190. CefDoMessageLoopWork();
  191. }
  192. void WebBrowserHost::ClearCookies(const String& url, const String& cookieName)
  193. {
  194. CefRefPtr<CefCookieManager> manager = CefCookieManager::GetGlobalManager(nullptr);
  195. if (!manager.get())
  196. return;
  197. CefString cefUrl;
  198. cefUrl.FromASCII(url.CString());
  199. CefString cefCookieName;
  200. cefCookieName.FromASCII(cookieName.CString());
  201. manager->DeleteCookies(cefUrl, cefCookieName, nullptr);
  202. }
  203. }