mirror of
https://github.com/cefsharp/CefSharp.git
synced 2026-12-04 10:22:55 +00:00
459 lines
19 KiB
C++
459 lines
19 KiB
C++
// 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 "Stdafx.h"
|
|
|
|
#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/CookieManager.h"
|
|
#include "Internals/PluginVisitor.h"
|
|
#include "CefSettings.h"
|
|
#include "SchemeHandlerFactoryWrapper.h"
|
|
#include "Internals/CefTaskScheduler.h"
|
|
#include "Internals/CefGetGeolocationCallbackWrapper.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>
|
|
/// Gets a value that indicates the Git Hash for CEF version currently being used.
|
|
/// </summary>
|
|
/// <value>The Git Commit Hash</value>
|
|
static property String^ CefCommitHash
|
|
{
|
|
String^ get()
|
|
{
|
|
return CEF_COMMIT_HASH;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Initializes CefSharp with the default settings.
|
|
/// This function should be called on the main application thread to initialize the CEF browser process.
|
|
/// </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.
|
|
/// This function should be called on the main application thread to initialize the CEF browser process.
|
|
/// </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.
|
|
/// This function should be called on the main application thread to initialize the CEF browser process.
|
|
/// </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)
|
|
{
|
|
if (IsInitialized)
|
|
{
|
|
// NOTE: Can only initialize Cef once, to make this explicitly clear throw exception on subsiquent attempts
|
|
throw gcnew Exception("Cef can only be initialized once. Use Cef.IsInitialized to guard against this exception.");
|
|
}
|
|
|
|
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));
|
|
|
|
auto success = CefInitialize(main_args, *(cefSettings->_cefSettings), app.get(), NULL);
|
|
|
|
//Register SchemeHandlerFactories - must be called after CefInitialize
|
|
for each (CefCustomScheme^ cefCustomScheme in cefSettings->CefCustomSchemes)
|
|
{
|
|
auto domainName = cefCustomScheme->DomainName ? cefCustomScheme->DomainName : String::Empty;
|
|
|
|
CefRefPtr<CefSchemeHandlerFactory> wrapper = new SchemeHandlerFactoryWrapper(cefCustomScheme->SchemeHandlerFactory);
|
|
CefRegisterSchemeHandlerFactory(StringUtils::ToNative(cefCustomScheme->SchemeName), StringUtils::ToNative(domainName), wrapper);
|
|
}
|
|
|
|
_initialized = success;
|
|
|
|
if (_initialized && shutdownOnProcessExit)
|
|
{
|
|
AppDomain::CurrentDomain->ProcessExit += gcnew EventHandler(ParentProcessExitHandler);
|
|
}
|
|
|
|
return success;
|
|
}
|
|
|
|
/// <summary>Perform a single iteration of CEF message loop processing. This function is
|
|
/// used to integrate the CEF message loop into an existing application message
|
|
/// loop. Care must be taken to balance performance against excessive CPU usage.
|
|
/// This function should only be called on the main application thread and only
|
|
/// if CefInitialize() is called with a CefSettings.multi_threaded_message_loop
|
|
/// value of false. This function will not block.</summary>
|
|
static void DoMessageLoopWork()
|
|
{
|
|
CefDoMessageLoopWork();
|
|
}
|
|
|
|
/// <summary>Add an entry to the cross-origin whitelist.</summary>
|
|
/// <param name="sourceOrigin">The origin allowed to be accessed by the target protocol/domain.</param>
|
|
/// <param name="targetProtocol">The target protocol allowed to access the source origin.</param>
|
|
/// <param name="targetDomain">The optional target domain allowed to access the source origin.</param>
|
|
/// <param name="allowTargetSubdomains">If set to true would allow a blah.example.com if the
|
|
/// <paramref name="targetDomain"/> was set to example.com
|
|
/// </param>
|
|
/// <remarks>
|
|
/// The same-origin policy restricts how scripts hosted from different origins
|
|
/// (scheme + domain + port) can communicate. By default, scripts can only access
|
|
/// resources with the same origin. Scripts hosted on the HTTP and HTTPS schemes
|
|
/// (but no other schemes) can use the "Access-Control-Allow-Origin" header to
|
|
/// allow cross-origin requests. For example, https://source.example.com can make
|
|
/// XMLHttpRequest requests on http://target.example.com if the
|
|
/// http://target.example.com request returns an "Access-Control-Allow-Origin:
|
|
/// https://source.example.com" response header.
|
|
//
|
|
/// Scripts in separate frames or iframes and hosted from the same protocol and
|
|
/// domain suffix can execute cross-origin JavaScript if both pages set the
|
|
/// document.domain value to the same domain suffix. For example,
|
|
/// scheme://foo.example.com and scheme://bar.example.com can communicate using
|
|
/// JavaScript if both domains set document.domain="example.com".
|
|
//
|
|
/// This method is used to allow access to origins that would otherwise violate
|
|
/// the same-origin policy. Scripts hosted underneath the fully qualified
|
|
/// <paramref name="sourceOrigin"/> URL (like http://www.example.com) will be allowed access to
|
|
/// all resources hosted on the specified <paramref name="targetProtocol"/> and <paramref name="targetDomain"/>.
|
|
/// If <paramref name="targetDomain"/> is non-empty and <paramref name="allowTargetSubdomains"/> if false only
|
|
/// exact domain matches will be allowed. If <paramref name="targetDomain"/> contains a top-
|
|
/// level domain component (like "example.com") and <paramref name="allowTargetSubdomains"/> is
|
|
/// true sub-domain matches will be allowed. If <paramref name="targetDomain"/> is empty and
|
|
/// <paramref name="allowTargetSubdomains"/> if true all domains and IP addresses will be
|
|
/// allowed.
|
|
//
|
|
/// This method cannot be used to bypass the restrictions on local or display
|
|
/// isolated schemes. See the comments on <see cref="CefCustomScheme"/> for more
|
|
/// information.
|
|
///
|
|
/// This function may be called on any thread. Returns false if <paramref name="sourceOrigin"/>
|
|
/// is invalid or the whitelist cannot be accessed.
|
|
/// </remarks>
|
|
static bool AddCrossOriginWhitelistEntry(
|
|
String^ sourceOrigin,
|
|
String^ targetProtocol,
|
|
String^ targetDomain,
|
|
bool allowTargetSubdomains)
|
|
{
|
|
return CefAddCrossOriginWhitelistEntry(
|
|
StringUtils::ToNative(sourceOrigin),
|
|
StringUtils::ToNative(targetProtocol),
|
|
StringUtils::ToNative(targetDomain),
|
|
allowTargetSubdomains);
|
|
}
|
|
|
|
/// <summary>Remove entry from cross-origin whitelist</summary>
|
|
/// <param name="sourceOrigin">The origin allowed to be accessed by the target protocol/domain.</param>
|
|
/// <param name="targetProtocol">The target protocol allowed to access the source origin.</param>
|
|
/// <param name="targetDomain">The optional target domain allowed to access the source origin.</param>
|
|
/// <param name="allowTargetSubdomains">If set to true would allow a blah.example.com if the
|
|
/// <paramref name="targetDomain"/> was set to example.com
|
|
/// </param>
|
|
/// <remarks>
|
|
/// Remove an entry from the cross-origin access whitelist. Returns false if
|
|
/// <paramref name="sourceOrigin"/> is invalid or the whitelist cannot be accessed.
|
|
/// </remarks>
|
|
static bool RemoveCrossOriginWhitelistEntry(String^ sourceOrigin,
|
|
String^ targetProtocol,
|
|
String^ targetDomain,
|
|
bool allowTargetSubdomains)
|
|
|
|
{
|
|
return CefRemoveCrossOriginWhitelistEntry(
|
|
StringUtils::ToNative(sourceOrigin),
|
|
StringUtils::ToNative(targetProtocol),
|
|
StringUtils::ToNative(targetDomain),
|
|
allowTargetSubdomains);
|
|
}
|
|
|
|
/// <summary>Remove all entries from the cross-origin access whitelist.</summary>
|
|
/// <remarks>
|
|
/// Remove all entries from the cross-origin access whitelist. Returns false if
|
|
/// the whitelist cannot be accessed.
|
|
/// </remarks>
|
|
static bool ClearCrossOriginWhitelist()
|
|
{
|
|
return CefClearCrossOriginWhitelist();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns the global cookie manager.
|
|
/// </summary>
|
|
static ICookieManager^ GetGlobalCookieManager()
|
|
{
|
|
auto cookieManager = CefCookieManager::GetGlobalManager(NULL);
|
|
return gcnew CookieManager(cookieManager);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Shuts down CefSharp and the underlying CEF infrastructure. This method is safe to call multiple times; it will only
|
|
/// shut down CEF on the first call (all subsequent calls will be ignored).
|
|
/// This function should be called on the main application thread to shut down the CEF browser process before the application exits.
|
|
/// </summary>
|
|
static void Shutdown()
|
|
{
|
|
if (IsInitialized)
|
|
{
|
|
OnContextInitialized = nullptr;
|
|
|
|
msclr::lock l(_sync);
|
|
|
|
UIThreadTaskFactory = nullptr;
|
|
IOThreadTaskFactory = nullptr;
|
|
FileThreadTaskFactory = nullptr;
|
|
|
|
for each(IDisposable^ diposable in Enumerable::ToList(_disposables))
|
|
{
|
|
delete diposable;
|
|
}
|
|
|
|
GC::Collect();
|
|
GC::WaitForPendingFinalizers();
|
|
|
|
CefShutdown();
|
|
IsInitialized = false;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Clear all registered scheme handler factories.
|
|
/// </summary>
|
|
/// <return>Returns false on error.</return>
|
|
static bool ClearSchemeHandlerFactories()
|
|
{
|
|
return CefClearSchemeHandlerFactories();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Async returns a list containing Plugin Information
|
|
/// (Wrapper around CefVisitWebPluginInfo)
|
|
/// The Task will be cancelled and a TaskCanceledException throw
|
|
/// if the Task does not complete within 2000ms
|
|
/// Documentation for CefWebPluginInfoVisitor.Visit states
|
|
/// `This method may never be called if no plugins are found.`
|
|
/// hence the Task cancelled timeout
|
|
/// </summary>
|
|
/// <return>Returns List of <see cref="Plugin"/> structs.</return>
|
|
static Task<List<Plugin>^>^ GetPlugins()
|
|
{
|
|
CefRefPtr<PluginVisitor> visitor = new PluginVisitor();
|
|
|
|
CefVisitWebPluginInfo(visitor);
|
|
|
|
return visitor->GetTask();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Add a plugin path (directory + file). This change may not take affect until after RefreshWebPlugins() is called.
|
|
/// </summary>
|
|
/// <param name="path">Path (directory + file).</param>
|
|
static void AddWebPluginPath(String^ path)
|
|
{
|
|
CefAddWebPluginPath(StringUtils::ToNative(path));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Add a plugin directory. This change may not take affect until after CefRefreshWebPlugins() is called.
|
|
/// </summary>
|
|
/// <param name="directory">Directory.</param>
|
|
static void AddWebPluginDirectory(String^ directory)
|
|
{
|
|
CefAddWebPluginDirectory(StringUtils::ToNative(directory));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Cause the plugin list to refresh the next time it is accessed regardless of whether it has already been loaded.
|
|
/// </summary>
|
|
static void RefreshWebPlugins()
|
|
{
|
|
CefRefreshWebPlugins();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Remove a plugin path (directory + file). This change may not take affect until after RefreshWebPlugins() is called.
|
|
/// </summary>
|
|
/// <param name="path">Path (directory + file).</param>
|
|
static void RemoveWebPluginPath(String^ path)
|
|
{
|
|
CefRemoveWebPluginPath(StringUtils::ToNative(path));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Unregister an internal plugin. This may be undone the next time RefreshWebPlugins() is called.
|
|
/// </summary>
|
|
/// <param name="path">Path (directory + file).</param>
|
|
static void UnregisterInternalWebPlugin(String^ path)
|
|
{
|
|
CefUnregisterInternalWebPlugin(StringUtils::ToNative(path));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Force a plugin to shutdown.
|
|
/// </summary>
|
|
/// <param name="path">Path (directory + file).</param>
|
|
static void ForceWebPluginShutdown(String^ path)
|
|
{
|
|
CefForceWebPluginShutdown(StringUtils::ToNative(path));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Call during process startup to enable High-DPI support on Windows 7 or newer.
|
|
/// Older versions of Windows should be left DPI-unaware because they do not
|
|
/// support DirectWrite and GDI fonts are kerned very badly.
|
|
/// </summary>
|
|
static void EnableHighDPISupport()
|
|
{
|
|
CefEnableHighDPISupport();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Request a one-time geolocation update.
|
|
/// This function bypasses any user permission checks so should only be
|
|
/// used by code that is allowed to access location information.
|
|
/// </summary>
|
|
/// <return>Returns 'best available' location info or, if the location update failed, with error info.</return>
|
|
static Task<Geoposition^>^ GetGeolocationAsync()
|
|
{
|
|
auto callback = new CefGetGeolocationCallbackWrapper();
|
|
|
|
CefGetGeolocation(callback);
|
|
|
|
return callback->GetTask();
|
|
}
|
|
};
|
|
}
|