Skip to content

Commit

Permalink
Core - Add IBrowserProcessHandler.OnAlreadyRunningAppRelaunch
Browse files Browse the repository at this point in the history
Issue #4668
  • Loading branch information
amaitland committed Jan 13, 2024
1 parent 470e1c8 commit cda78d0
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 0 deletions.
30 changes: 30 additions & 0 deletions CefSharp.Core.Runtime/Internals/CefSharpApp.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,36 @@ namespace CefSharp
_app->BrowserProcessHandler->OnScheduleMessagePumpWork(delay_ms);
}

virtual bool OnAlreadyRunningAppRelaunch(CefRefPtr<CefCommandLine> commandLine, const CefString& currentDirectory) override
{
if (Object::ReferenceEquals(_app, nullptr) || Object::ReferenceEquals(_app->BrowserProcessHandler, nullptr))
{
return false;
}

auto managedArgs = gcnew Dictionary<String^, String^>();

CefCommandLine::ArgumentList args;
commandLine->GetArguments(args);

for (auto arg : args)
{
managedArgs->Add(StringUtils::ToClr(arg), String::Empty);
}

CefCommandLine::SwitchMap switches;
commandLine->GetSwitches(switches);

for (auto s : switches)
{
managedArgs->Add(StringUtils::ToClr(s.first), StringUtils::ToClr(s.second));
}

auto readOnlyArgs = gcnew System::Collections::ObjectModel::ReadOnlyDictionary<String^, String^>(managedArgs);

return _app->BrowserProcessHandler->OnAlreadyRunningAppRelaunch(readOnlyArgs, StringUtils::ToClr(currentDirectory));
}

virtual void OnBeforeChildProcessLaunch(CefRefPtr<CefCommandLine> commandLine) override
{
#ifndef NETCOREAPP
Expand Down
38 changes: 38 additions & 0 deletions CefSharp/Handler/BrowserProcessHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

using System;
using System.Collections.Generic;

namespace CefSharp.Handler
{
Expand Down Expand Up @@ -55,6 +56,43 @@ protected virtual void OnScheduleMessagePumpWork(long delay)

}

/// <inheritdoc/>
bool IBrowserProcessHandler.OnAlreadyRunningAppRelaunch(IReadOnlyDictionary<string, string> commandLine, string currentDirectory)
{
return OnAlreadyRunningAppRelaunch(commandLine, currentDirectory);
}

/// <summary>
/// Implement this method to provide app-specific behavior when an already
/// running app is relaunched with the same CefSettings.RootCachePath value.
/// For example, activate an existing app window or create a new app window.
///
/// To avoid cache corruption only a single app instance is allowed to run for
/// a given CefSettings.RootCachePath value. On relaunch the app checks a
/// process singleton lock and then forwards the new launch arguments to the
/// already running app process before exiting early. Client apps should
/// therefore check the Cef.Initialize() return value for early exit before
/// proceeding.
///
/// It's important to note that this method will be called on a CEF UI thread,
/// which by default is not the same as your application UI thread.
///
/// </summary>
/// <param name="commandLine">Command line arugments/switches</param>
/// <param name="currentDirectory">current directory (optional)</param>
/// <returns>
/// Return true if the relaunch is handled or false for default relaunch
/// behavior. Default behavior will create a new default styled Chrome window.
/// </returns>
/// <remarks>
/// The <paramref name="commandLine"/> dictionary may contain keys that have empty string values
/// (arugments).
/// </remarks>
protected virtual bool OnAlreadyRunningAppRelaunch(IReadOnlyDictionary<string, string> commandLine, string currentDirectory)
{
return false;
}

/// <summary>
/// IsDisposed
/// </summary>
Expand Down
29 changes: 29 additions & 0 deletions CefSharp/Handler/IBrowserProcessHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

using System;
using System.Collections.Generic;

namespace CefSharp
{
Expand Down Expand Up @@ -34,5 +35,33 @@ public interface IBrowserProcessHandler : IDisposable
/// specified delay and any currently pending scheduled call should be
/// cancelled.</param>
void OnScheduleMessagePumpWork(long delay);

/// <summary>
/// Implement this method to provide app-specific behavior when an already
/// running app is relaunched with the same CefSettings.RootCachePath value.
/// For example, activate an existing app window or create a new app window.
///
/// To avoid cache corruption only a single app instance is allowed to run for
/// a given CefSettings.RootCachePath value. On relaunch the app checks a
/// process singleton lock and then forwards the new launch arguments to the
/// already running app process before exiting early. Client apps should
/// therefore check the Cef.Initialize() return value for early exit before
/// proceeding.
///
/// It's important to note that this method will be called on a CEF UI thread,
/// which by default is not the same as your application UI thread.
///
/// </summary>
/// <param name="commandLine">Readonly command line args</param>
/// <param name="currentDirectory">current directory (optional)</param>
/// <returns>
/// Return true if the relaunch is handled or false for default relaunch
/// behavior. Default behavior will create a new default styled Chrome window.
/// </returns>
/// <remarks>
/// The <paramref name="commandLine"/> dictionary may contain keys that have empty string values
/// (arugments).
/// </remarks>
bool OnAlreadyRunningAppRelaunch(IReadOnlyDictionary<string, string> commandLine, string currentDirectory);
}
}

0 comments on commit cda78d0

Please sign in to comment.