Skip to content

Commit cda78d0

Browse files
committed
Core - Add IBrowserProcessHandler.OnAlreadyRunningAppRelaunch
Issue #4668
1 parent 470e1c8 commit cda78d0

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed

CefSharp.Core.Runtime/Internals/CefSharpApp.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,36 @@ namespace CefSharp
121121
_app->BrowserProcessHandler->OnScheduleMessagePumpWork(delay_ms);
122122
}
123123

124+
virtual bool OnAlreadyRunningAppRelaunch(CefRefPtr<CefCommandLine> commandLine, const CefString& currentDirectory) override
125+
{
126+
if (Object::ReferenceEquals(_app, nullptr) || Object::ReferenceEquals(_app->BrowserProcessHandler, nullptr))
127+
{
128+
return false;
129+
}
130+
131+
auto managedArgs = gcnew Dictionary<String^, String^>();
132+
133+
CefCommandLine::ArgumentList args;
134+
commandLine->GetArguments(args);
135+
136+
for (auto arg : args)
137+
{
138+
managedArgs->Add(StringUtils::ToClr(arg), String::Empty);
139+
}
140+
141+
CefCommandLine::SwitchMap switches;
142+
commandLine->GetSwitches(switches);
143+
144+
for (auto s : switches)
145+
{
146+
managedArgs->Add(StringUtils::ToClr(s.first), StringUtils::ToClr(s.second));
147+
}
148+
149+
auto readOnlyArgs = gcnew System::Collections::ObjectModel::ReadOnlyDictionary<String^, String^>(managedArgs);
150+
151+
return _app->BrowserProcessHandler->OnAlreadyRunningAppRelaunch(readOnlyArgs, StringUtils::ToClr(currentDirectory));
152+
}
153+
124154
virtual void OnBeforeChildProcessLaunch(CefRefPtr<CefCommandLine> commandLine) override
125155
{
126156
#ifndef NETCOREAPP

CefSharp/Handler/BrowserProcessHandler.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
44

55
using System;
6+
using System.Collections.Generic;
67

78
namespace CefSharp.Handler
89
{
@@ -55,6 +56,43 @@ protected virtual void OnScheduleMessagePumpWork(long delay)
5556

5657
}
5758

59+
/// <inheritdoc/>
60+
bool IBrowserProcessHandler.OnAlreadyRunningAppRelaunch(IReadOnlyDictionary<string, string> commandLine, string currentDirectory)
61+
{
62+
return OnAlreadyRunningAppRelaunch(commandLine, currentDirectory);
63+
}
64+
65+
/// <summary>
66+
/// Implement this method to provide app-specific behavior when an already
67+
/// running app is relaunched with the same CefSettings.RootCachePath value.
68+
/// For example, activate an existing app window or create a new app window.
69+
///
70+
/// To avoid cache corruption only a single app instance is allowed to run for
71+
/// a given CefSettings.RootCachePath value. On relaunch the app checks a
72+
/// process singleton lock and then forwards the new launch arguments to the
73+
/// already running app process before exiting early. Client apps should
74+
/// therefore check the Cef.Initialize() return value for early exit before
75+
/// proceeding.
76+
///
77+
/// It's important to note that this method will be called on a CEF UI thread,
78+
/// which by default is not the same as your application UI thread.
79+
///
80+
/// </summary>
81+
/// <param name="commandLine">Command line arugments/switches</param>
82+
/// <param name="currentDirectory">current directory (optional)</param>
83+
/// <returns>
84+
/// Return true if the relaunch is handled or false for default relaunch
85+
/// behavior. Default behavior will create a new default styled Chrome window.
86+
/// </returns>
87+
/// <remarks>
88+
/// The <paramref name="commandLine"/> dictionary may contain keys that have empty string values
89+
/// (arugments).
90+
/// </remarks>
91+
protected virtual bool OnAlreadyRunningAppRelaunch(IReadOnlyDictionary<string, string> commandLine, string currentDirectory)
92+
{
93+
return false;
94+
}
95+
5896
/// <summary>
5997
/// IsDisposed
6098
/// </summary>

CefSharp/Handler/IBrowserProcessHandler.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
44

55
using System;
6+
using System.Collections.Generic;
67

78
namespace CefSharp
89
{
@@ -34,5 +35,33 @@ public interface IBrowserProcessHandler : IDisposable
3435
/// specified delay and any currently pending scheduled call should be
3536
/// cancelled.</param>
3637
void OnScheduleMessagePumpWork(long delay);
38+
39+
/// <summary>
40+
/// Implement this method to provide app-specific behavior when an already
41+
/// running app is relaunched with the same CefSettings.RootCachePath value.
42+
/// For example, activate an existing app window or create a new app window.
43+
///
44+
/// To avoid cache corruption only a single app instance is allowed to run for
45+
/// a given CefSettings.RootCachePath value. On relaunch the app checks a
46+
/// process singleton lock and then forwards the new launch arguments to the
47+
/// already running app process before exiting early. Client apps should
48+
/// therefore check the Cef.Initialize() return value for early exit before
49+
/// proceeding.
50+
///
51+
/// It's important to note that this method will be called on a CEF UI thread,
52+
/// which by default is not the same as your application UI thread.
53+
///
54+
/// </summary>
55+
/// <param name="commandLine">Readonly command line args</param>
56+
/// <param name="currentDirectory">current directory (optional)</param>
57+
/// <returns>
58+
/// Return true if the relaunch is handled or false for default relaunch
59+
/// behavior. Default behavior will create a new default styled Chrome window.
60+
/// </returns>
61+
/// <remarks>
62+
/// The <paramref name="commandLine"/> dictionary may contain keys that have empty string values
63+
/// (arugments).
64+
/// </remarks>
65+
bool OnAlreadyRunningAppRelaunch(IReadOnlyDictionary<string, string> commandLine, string currentDirectory);
3766
}
3867
}

0 commit comments

Comments
 (0)