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);

    // 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");
    }

    RegisterWebSchemeHandlers(this);

    // Ensure cookie manager is created
    CefCookieManager::GetGlobalManager(nullptr);

    SubscribeToEvent(E_UPDATE, ATOMIC_HANDLER(WebBrowserHost, HandleUpdate));

    instance_ = this;

}