<commit_before>// Copyright © 2010-2015 The CefSharp Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

#pragma once

#include <msclr/lock.h>
#include <include/cef_version.h>
#include <include/cef_runnable.h>
#include <include/cef_origin_whitelist.h>
#include <include/cef_web_plugin.h>

#include "Internals/CefSharpApp.h"
#include "Internals/CookieVisitor.h"
#include "Internals/CompletionHandler.h"
#include "Internals/StringUtils.h"
#include "Internals/PluginVisitor.h"
#include "ManagedCefBrowserAdapter.h"
#include "CefSettings.h"
#include "SchemeHandlerWrapper.h"
#include "Internals/CefTaskScheduler.h"
#include "CookieAsyncWrapper.h"

using namespace System::Collections::Generic; 
using namespace System::Linq;
using namespace System::Reflection;

namespace CefSharp
{
    public ref class Cef sealed
    {
    private:
        static Object^ _sync;

        static bool _initialized = false;
        static HashSet<IDisposable^>^ _disposables;

        static Cef()
        {
            _sync = gcnew Object();
            _disposables = gcnew HashSet<IDisposable^>();
        }

        static void ParentProcessExitHandler(Object^ sender, EventArgs^ e)
        {
            if (Cef::IsInitialized)
            {
                Cef::Shutdown();
            }
        }

    public:
        /// <summary>
        /// Called on the browser process UI thread immediately after the CEF context has been initialized. 
        /// </summary>
        static property Action^ OnContextInitialized;

        static property TaskFactory^ UIThreadTaskFactory;
        static property TaskFactory^ IOThreadTaskFactory;
        static property TaskFactory^ FileThreadTaskFactory;

        static void AddDisposable(IDisposable^ item)
        {
            msclr::lock l(_sync);

            _disposables->Add(item);
        }

        static void RemoveDisposable(IDisposable^ item)
        {
            msclr::lock l(_sync);

            _disposables->Remove(item);
        }

        /// <summary>Gets a value that indicates whether CefSharp is initialized.</summary>
        /// <value>true if CefSharp is initialized; otherwise, false.</value>
        static property bool IsInitialized
        {
            bool get()
            {
                return _initialized;
            }

        private:
            void set(bool value)
            {
                _initialized = value;
            }
        }

        /// <summary>Gets a value that indicates the version of CefSharp currently being used.</summary>
        /// <value>The CefSharp version.</value>
        static property String^ CefSharpVersion
        {
            String^ get()
            {
                Assembly^ assembly = Assembly::GetAssembly(Cef::typeid);
                return assembly->GetName()->Version->ToString();
            }
        }

        /// <summary>Gets a value that indicates the CEF version currently being used.</summary>
        /// <value>The CEF Version</value>
        static property String^ CefVersion
        {
            String^ get()
            {
                return String::Format("r{0}", CEF_VERSION);
            }
        }

        /// <summary>Gets a value that indicates the Chromium version currently being used.</summary>
        /// <value>The Chromium version.</value>
        static property String^ ChromiumVersion
        {
            String^ get()
            {
                // Need explicit cast here to avoid C4965 warning when the minor version is zero.
                return String::Format("{0}.{1}.{2}.{3}",
                    CHROME_VERSION_MAJOR, (Object^) CHROME_VERSION_MINOR,
                    CHROME_VERSION_BUILD, CHROME_VERSION_PATCH);
            }
        }

        /// <summary>Initializes CefSharp with the default settings.</summary>
        /// <return>true if successful; otherwise, false.</return>
        static bool Initialize()
        {
            auto cefSettings = gcnew CefSettings();
            return Initialize(cefSettings);
        }

        /// <summary>Initializes CefSharp with user-provided settings.</summary>
        /// <param name="cefSettings">CefSharp configuration settings.</param>
        /// <return>true if successful; otherwise, false.</return>
        static bool Initialize(CefSettings^ cefSettings)
        {
            return Initialize(cefSettings, true, false);
        }

        /// <summary>Initializes CefSharp with user-provided settings.</summary>
        /// <param name="cefSettings">CefSharp configuration settings.</param>
        /// <param name="shutdownOnProcessExit">When the Current AppDomain (relative to the thread called on)
        /// Exits(ProcessExit event) then Shudown will be called.</param>
        /// <param name="performDependencyCheck">Check that all relevant dependencies avaliable, throws exception if any are missing</param>
        /// <return>true if successful; otherwise, false.</return>
        static bool Initialize(CefSettings^ cefSettings, bool shutdownOnProcessExit, bool performDependencyCheck)
        {
            bool success = false;

            // NOTE: Can only initialize Cef once, so subsiquent calls are ignored.
            if (!IsInitialized)
            {
                if (cefSettings->BrowserSubprocessPath == nullptr)
                {
                    throw gcnew Exception("CefSettings BrowserSubprocessPath cannot be null.");
                }

                if(performDependencyCheck)
                {
                    DependencyChecker::AssertAllDependenciesPresent(cefSettings->Locale, cefSettings->LocalesDirPath, cefSettings->ResourcesDirPath, cefSettings->PackLoadingDisabled, cefSettings->BrowserSubprocessPath);
                }

                UIThreadTaskFactory = gcnew TaskFactory(gcnew CefTaskScheduler(TID_UI));
                IOThreadTaskFactory = gcnew TaskFactory(gcnew CefTaskScheduler(TID_IO));
                FileThreadTaskFactory = gcnew TaskFactory(gcnew CefTaskScheduler(TID_FILE));

                CefMainArgs main_args;
                CefRefPtr<CefSharpApp> app(new CefSharpApp(cefSettings, OnContextInitialized));

                success = CefInitialize(main_args, *(cefSettings->_cefSettings), app.get(), NULL);
                app->CompleteSchemeRegistrations();
                _initialized = success;<commit_msg>Pedantic code cleanup - no need to call .get() as the argument is expecting a `CefRefPtr`<commit_after>// Copyright © 2010-2015 The CefSharp Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

#pragma once

#include <msclr/lock.h>
#include <include/cef_version.h>
#include <include/cef_runnable.h>
#include <include/cef_origin_whitelist.h>
#include <include/cef_web_plugin.h>

#include "Internals/CefSharpApp.h"
#include "Internals/CookieVisitor.h"
#include "Internals/CompletionHandler.h"
#include "Internals/StringUtils.h"
#include "Internals/PluginVisitor.h"
#include "ManagedCefBrowserAdapter.h"
#include "CefSettings.h"
#include "SchemeHandlerWrapper.h"
#include "Internals/CefTaskScheduler.h"
#include "CookieAsyncWrapper.h"

using namespace System::Collections::Generic; 
using namespace System::Linq;
using namespace System::Reflection;

namespace CefSharp
{
    public ref class Cef sealed
    {
    private:
        static Object^ _sync;

        static bool _initialized = false;
        static HashSet<IDisposable^>^ _disposables;

        static Cef()
        {
            _sync = gcnew Object();
            _disposables = gcnew HashSet<IDisposable^>();
        }

        static void ParentProcessExitHandler(Object^ sender, EventArgs^ e)
        {
            if (Cef::IsInitialized)
            {
                Cef::Shutdown();
            }
        }

    public:
        /// <summary>
        /// Called on the browser process UI thread immediately after the CEF context has been initialized. 
        /// </summary>
        static property Action^ OnContextInitialized;

        static property TaskFactory^ UIThreadTaskFactory;
        static property TaskFactory^ IOThreadTaskFactory;
        static property TaskFactory^ FileThreadTaskFactory;

        static void AddDisposable(IDisposable^ item)
        {
            msclr::lock l(_sync);

            _disposables->Add(item);
        }

        static void RemoveDisposable(IDisposable^ item)
        {
            msclr::lock l(_sync);

            _disposables->Remove(item);
        }

        /// <summary>Gets a value that indicates whether CefSharp is initialized.</summary>
        /// <value>true if CefSharp is initialized; otherwise, false.</value>
        static property bool IsInitialized
        {
            bool get()
            {
                return _initialized;
            }

        private:
            void set(bool value)
            {
                _initialized = value;
            }
        }

        /// <summary>Gets a value that indicates the version of CefSharp currently being used.</summary>
        /// <value>The CefSharp version.</value>
        static property String^ CefSharpVersion
        {
            String^ get()
            {
                Assembly^ assembly = Assembly::GetAssembly(Cef::typeid);
                return assembly->GetName()->Version->ToString();
            }
        }

        /// <summary>Gets a value that indicates the CEF version currently being used.</summary>
        /// <value>The CEF Version</value>
        static property String^ CefVersion
        {
            String^ get()
            {
                return String::Format("r{0}", CEF_VERSION);
            }
        }

        /// <summary>Gets a value that indicates the Chromium version currently being used.</summary>
        /// <value>The Chromium version.</value>
        static property String^ ChromiumVersion
        {
            String^ get()
            {
                // Need explicit cast here to avoid C4965 warning when the minor version is zero.
                return String::Format("{0}.{1}.{2}.{3}",
                    CHROME_VERSION_MAJOR, (Object^) CHROME_VERSION_MINOR,
                    CHROME_VERSION_BUILD, CHROME_VERSION_PATCH);
            }
        }

        /// <summary>Initializes CefSharp with the default settings.</summary>
        /// <return>true if successful; otherwise, false.</return>
        static bool Initialize()
        {
            auto cefSettings = gcnew CefSettings();
            return Initialize(cefSettings);
        }

        /// <summary>Initializes CefSharp with user-provided settings.</summary>
        /// <param name="cefSettings">CefSharp configuration settings.</param>
        /// <return>true if successful; otherwise, false.</return>
        static bool Initialize(CefSettings^ cefSettings)
        {
            return Initialize(cefSettings, true, false);
        }

        /// <summary>Initializes CefSharp with user-provided settings.</summary>
        /// <param name="cefSettings">CefSharp configuration settings.</param>
        /// <param name="shutdownOnProcessExit">When the Current AppDomain (relative to the thread called on)
        /// Exits(ProcessExit event) then Shudown will be called.</param>
        /// <param name="performDependencyCheck">Check that all relevant dependencies avaliable, throws exception if any are missing</param>
        /// <return>true if successful; otherwise, false.</return>
        static bool Initialize(CefSettings^ cefSettings, bool shutdownOnProcessExit, bool performDependencyCheck)
        {
            bool success = false;

            // NOTE: Can only initialize Cef once, so subsiquent calls are ignored.
            if (!IsInitialized)
            {
                if (cefSettings->BrowserSubprocessPath == nullptr)
                {
                    throw gcnew Exception("CefSettings BrowserSubprocessPath cannot be null.");
                }

                if(performDependencyCheck)
                {
                    DependencyChecker::AssertAllDependenciesPresent(cefSettings->Locale, cefSettings->LocalesDirPath, cefSettings->ResourcesDirPath, cefSettings->PackLoadingDisabled, cefSettings->BrowserSubprocessPath);
                }

                UIThreadTaskFactory = gcnew TaskFactory(gcnew CefTaskScheduler(TID_UI));
                IOThreadTaskFactory = gcnew TaskFactory(gcnew CefTaskScheduler(TID_IO));
                FileThreadTaskFactory = gcnew TaskFactory(gcnew CefTaskScheduler(TID_FILE));

                CefMainArgs main_args;
                CefRefPtr<CefSharpApp> app = new CefSharpApp(cefSettings, OnContextInitialized);

                success = CefInitialize(main_args, *(cefSettings->_cefSettings), app, NULL);
                app->CompleteSchemeRegistrations();
                _initialized = success;
{'fraction_non_alphanumeric': 0.094285492750252, 'fraction_numerical': 0.0026362720012405986, 'mean_word_length': 2.187052137385718, 'pattern_counts': {'":': 0, '<': 95, '<?xml version=': 0, '>': 121, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2052879', 'n_tokens_mistral': 3435, 'n_tokens_neox': 3262, 'n_words': 868}