WebBrowserHost.cpp 8.6 KB

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