| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305 |
- //
- // Copyright (c) 2014-2016, THUNDERBEAST GAMES LLC All rights reserved
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to deal
- // in the Software without restriction, including without limitation the rights
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- // copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- // THE SOFTWARE.
- //
- #include <SDL/include/SDL.h>
- #include <ThirdParty/SDL/include/SDL_syswm.h>
- #include <include/cef_app.h>
- #include <include/cef_client.h>
- #include <include/wrapper/cef_helpers.h>
- #include <include/base/cef_bind.h>
- #include <include/wrapper/cef_closure_task.h>
- #include <Atomic/Core/ProcessUtils.h>
- #include <Atomic/Core/CoreEvents.h>
- #include <Atomic/IO/Log.h>
- #include <Atomic/IO/File.h>
- #include <Atomic/IO/FileSystem.h>
- #include <Atomic/Graphics/Graphics.h>
- #include "Internal/WebAppBrowser.h"
- #include "WebViewEvents.h"
- #include "WebSchemeHandler.h"
- #include "WebClient.h"
- #include "WebBrowserHost.h"
- #ifdef ATOMIC_PLATFORM_LINUX
- #include <locale>
- #include <X11/Xlib.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <signal.h>
- static int XErrorHandlerImpl(Display *display, XErrorEvent *event)
- {
- if ( display && event )
- {
- char msg[132];
- XGetErrorText(display, event->error_code, msg, sizeof(msg));
- fprintf(stderr, "X11 Error %d (%s): request %d.%d \n",
- event->error_code,
- msg,
- event->request_code,
- event->minor_code );
- }
- return 0;
- }
- static int XIOErrorHandlerImpl(Display *display)
- {
- if ( display )
- fprintf(stderr, "XIO Error on display %p, quitting.\n", (void*)display );
- return 0;
- }
- static void TerminationSignalHandler(int signatl)
- {
- fprintf(stderr,"Received signal %d, quitting.\n", signatl );
- exit(0);
- }
- #endif
- namespace Atomic
- {
- class WebBrowserHostPrivate
- {
- friend class WebBrowserHost;
- public:
- WebBrowserHostPrivate(WebBrowserHost* host)
- {
- host_ = host;
- app_ = new WebAppBrowser();
- }
- virtual ~WebBrowserHostPrivate()
- {
- host_ = 0;
- }
- private:
- WeakPtr<WebBrowserHost> host_;
- CefRefPtr<CefApp> app_;
- };
- GlobalPropertyMap WebBrowserHost::globalProperties_;
- WeakPtr<WebBrowserHost> WebBrowserHost::instance_;
- String WebBrowserHost::rootCacheFolder_;
- String WebBrowserHost::cacheName_;
- String WebBrowserHost::userAgent_;
- String WebBrowserHost::productVersion_;
- String WebBrowserHost::jsMessageQueryFunctionName_ = "atomicQuery";
- String WebBrowserHost::jsMessageQueryCancelFunctionName_ = "atomicQueryCancel";
- int WebBrowserHost::debugPort_ = 3335;
- bool WebBrowserHost::webSecurity_ = true;
- WebBrowserHost::WebBrowserHost(Context* context) : Object (context)
- {
- const Vector<String>& arguments = GetArguments();
- #ifdef ATOMIC_PLATFORM_LINUX
- XSetErrorHandler(XErrorHandlerImpl);
- XSetIOErrorHandler(XIOErrorHandlerImpl);
- // Install a signal handler so we clean up after ourselves.
- signal(SIGINT, TerminationSignalHandler);
- signal(SIGTERM, TerminationSignalHandler);
- #endif
- // IMPORTANT: See flags being set in implementation of void WebAppBrowser::OnBeforeCommandLineProcessing
- // these include "--enable-media-stream", "--enable-usermedia-screen-capturing", "--off-screen-rendering-enabled", "--transparent-painting-enabled"
- #ifdef ATOMIC_PLATFORM_LINUX
- static const char* _argv[2] = { "AtomicWebView", "--disable-setuid-sandbox" };
- CefMainArgs args(2, (char**) &_argv);
- #else
- CefMainArgs args;
- #endif
- CefSettings settings;
- settings.windowless_rendering_enabled = 1;
- FileSystem* fs = GetSubsystem<FileSystem>();
- // Set CEF log file to existing log folder if any avoid attempting to write
- // to executable folder, which is likely not writeable
- Log* log = GetSubsystem<Log>();
- if (log && log->GetLogFile())
- {
- const File* logFile = log->GetLogFile();
- String logPath = logFile->GetName ();
- if (logPath.Length())
- {
- String pathName, fileName, ext;
- SplitPath(logPath, pathName, fileName, ext);
- if (pathName.Length())
- {
- pathName = AddTrailingSlash(pathName) + "CEF.log";
- CefString(&settings.log_file).FromASCII(GetNativePath(pathName).CString());
- }
-
- }
-
- }
- // default background is white, add a setting
- // settings.background_color = 0;
- if (productVersion_.Length())
- {
- CefString(&settings.product_version).FromASCII(productVersion_.CString());
- }
- if (userAgent_.Length())
- {
- CefString(&settings.user_agent).FromASCII(userAgent_.CString());
- }
- String fullPath;
- // If we've specified the absolute path to a root cache folder, use it
- if (rootCacheFolder_.Length() && cacheName_.Length())
- {
- fullPath = rootCacheFolder_ + "/" + cacheName_;
- CefString(&settings.cache_path).FromASCII(fullPath.CString());
- }
- else
- {
- fullPath = fs->GetAppPreferencesDir(cacheName_.CString(), "WebCache");
- }
- CefString(&settings.cache_path).FromASCII(fullPath.CString());
- settings.remote_debugging_port = debugPort_;
- d_ = new WebBrowserHostPrivate(this);
- #ifdef ATOMIC_PLATFORM_LINUX
- // On Linux systems, CEF ignores the locale in CefSettings and sets it based on environment variables.
- // On non-English systems this changes the decimal separator to a comma (e.g. with a German
- // locale such as de-AT), which in turn breaks the cross-platform behavior of strtod() and sprintf().
- std::locale oldLocale;
- #endif
- // If losing OSX system menu, it means we're calling this
- // before initializing graphics subsystem
- if (!CefInitialize(args, settings, d_->app_, nullptr))
- {
- ATOMIC_LOGERROR("CefInitialize - Error");
- }
- #ifdef ATOMIC_PLATFORM_LINUX
- std::locale::global(oldLocale);
- #endif
- RegisterWebSchemeHandlers(this);
- // Ensure cookie manager is created
- CefCookieManager::GetGlobalManager(nullptr);
- SubscribeToEvent(E_UPDATE, ATOMIC_HANDLER(WebBrowserHost, HandleUpdate));
- instance_ = this;
- }
- WebBrowserHost::~WebBrowserHost()
- {
- instance_ = 0;
- // TODO: Better place for this? If there are issues with cookies not persisting
- // it is possible the async nature of this call could be a culprit
- CefRefPtr<CefCookieManager> manager = CefCookieManager::GetGlobalManager(nullptr);
- if (manager.get())
- manager->FlushStore(nullptr);
- CefClearSchemeHandlerFactories();
- CefShutdown();
- }
- void WebBrowserHost::SetGlobalBoolProperty(const String& globalVar, const String& property, bool value)
- {
- Variant v(value);
- SetGlobalProperty(globalVar, property, v);
- }
- void WebBrowserHost::SetGlobalStringProperty(const String& globalVar, const String& property, const String& value)
- {
- Variant v(value);
- SetGlobalProperty(globalVar, property, v);
- }
- void WebBrowserHost::SetGlobalNumberProperty(const String& globalVar, const String& property, double value)
- {
- Variant v(value);
- SetGlobalProperty(globalVar, property, v);
- }
- void WebBrowserHost::SetGlobalProperty(const String& globalVar, const String &property, Variant& value)
- {
- globalProperties_[globalVar][property] = value;
- if (!instance_.NotNull())
- instance_->SendEvent(E_WEBVIEWGLOBALPROPERTIESCHANGED);
- }
- void WebBrowserHost::HandleUpdate(StringHash eventType, VariantMap& eventData)
- {
- CefDoMessageLoopWork();
- }
- void WebBrowserHost::ClearCookies(const String& url, const String& cookieName)
- {
- CefRefPtr<CefCookieManager> manager = CefCookieManager::GetGlobalManager(nullptr);
- if (!manager.get())
- return;
- CefString cefUrl;
- cefUrl.FromASCII(url.CString());
- CefString cefCookieName;
- cefCookieName.FromASCII(cookieName.CString());
- manager->DeleteCookies(cefUrl, cefCookieName, nullptr);
- }
- }
|