Skip to content

Commit f853006

Browse files
committed
Core - Add Cef.PostAction and Cef.PostDelayedAction
1 parent bde0bf3 commit f853006

File tree

8 files changed

+104
-0
lines changed

8 files changed

+104
-0
lines changed

CefSharp.Core.Runtime.RefAssembly/CefSharp.Core.Runtime.netcore.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ public static void EnableWaitForBrowsersToClose() { }
8181
public static bool Initialize(CefSharp.Core.CefSettingsBase cefSettings, bool performDependencyCheck, CefSharp.IApp cefApp) { throw null; }
8282
public static bool Initialize(CefSharp.Core.CefSettingsBase cefSettings, bool performDependencyCheck, CefSharp.IBrowserProcessHandler browserProcessHandler) { throw null; }
8383
public static CefSharp.UrlParts ParseUrl(string url) { throw null; }
84+
public static bool PostAction(CefSharp.CefThreadIds threadId, System.Action action) { throw null; }
85+
public static bool PostDelayedAction(CefSharp.CefThreadIds threadId, System.Action action, int delayInMs) { throw null; }
8486
public static void PreShutdown() { }
8587
public static void QuitMessageLoop() { }
8688
public static bool RemoveCrossOriginWhitelistEntry(string sourceOrigin, string targetProtocol, string targetDomain, bool allowTargetSubdomains) { throw null; }

CefSharp.Core.Runtime/Cef.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@
1515
#include <include/cef_origin_whitelist.h>
1616
#include <include/cef_crash_util.h>
1717
#include <include/cef_parser.h>
18+
#include <include/cef_task.h>
1819
#include <include/internal/cef_types.h>
1920

2021
#include "Internals/CefSharpApp.h"
2122
#include "Internals/CefTaskScheduler.h"
23+
#include "Internals/CefTaskDelegate.h"
2224
#include "CookieManager.h"
2325
#include "CefSettingsBase.h"
2426
#include "RequestContext.h"
@@ -883,6 +885,33 @@ namespace CefSharp
883885
//A few extra ms to allow for CEF to finish
884886
Thread::Sleep(50);
885887
}
888+
889+
/// <summary>
890+
/// Post an action for delayed execution on the specified thread.
891+
/// </summary>
892+
/// <param name="threadId">thread id</param>
893+
/// <param name="action">action to execute</param>
894+
/// <param name="delayInMs">delay in ms</param>
895+
/// <returns>bool</returns>
896+
static bool PostDelayedAction(CefThreadIds threadId, Action^ action, int delayInMs)
897+
{
898+
auto task = new CefTaskDelegate(action);
899+
900+
return CefPostDelayedTask((cef_thread_id_t)threadId, task, delayInMs);
901+
}
902+
903+
/// <summary>
904+
/// Post an action for execution on the specified thread.
905+
/// </summary>
906+
/// <param name="threadId">thread id</param>
907+
/// <param name="action">action to execute</param>
908+
/// <returns>bool</returns>
909+
static bool PostAction(CefThreadIds threadId, Action^ action)
910+
{
911+
auto task = new CefTaskDelegate(action);
912+
913+
return CefPostTask((cef_thread_id_t)threadId, task);
914+
}
886915
};
887916
}
888917
}

CefSharp.Core.Runtime/CefSharp.Core.Runtime.netcore.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,7 @@
373373
<ClInclude Include="Internals\CefResourceSkipCallbackWrapper.h" />
374374
<ClInclude Include="Internals\CefRunFileDialogCallbackAdapter.h" />
375375
<ClInclude Include="Internals\CefSchemeRegistrarWrapper.h" />
376+
<ClInclude Include="Internals\CefTaskDelegate.h" />
376377
<ClInclude Include="Internals\CefUrlRequestClientAdapter.h" />
377378
<ClInclude Include="Internals\CefValueWrapper.h" />
378379
<ClInclude Include="Internals\CefWriteHandlerWrapper.h" />

CefSharp.Core.Runtime/CefSharp.Core.Runtime.netcore.vcxproj.filters

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,9 @@
328328
<ClInclude Include="Internals\CefRefCountManaged.h">
329329
<Filter>Header Files</Filter>
330330
</ClInclude>
331+
<ClInclude Include="Internals\CefTaskDelegate.h">
332+
<Filter>Header Files</Filter>
333+
</ClInclude>
331334
</ItemGroup>
332335
<ItemGroup>
333336
<ClInclude Include="Internals\CefFrameWrapper.h">

CefSharp.Core.Runtime/CefSharp.Core.Runtime.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@
278278
<ClInclude Include="Internals\CefResourceSkipCallbackWrapper.h" />
279279
<ClInclude Include="Internals\CefRunFileDialogCallbackAdapter.h" />
280280
<ClInclude Include="Internals\CefSchemeRegistrarWrapper.h" />
281+
<ClInclude Include="Internals\CefTaskDelegate.h" />
281282
<ClInclude Include="Internals\CefUrlRequestClientAdapter.h" />
282283
<ClInclude Include="Internals\CefValueWrapper.h" />
283284
<ClInclude Include="Internals\CefWriteHandlerWrapper.h" />

CefSharp.Core.Runtime/CefSharp.Core.Runtime.vcxproj.filters

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,9 @@
328328
<ClInclude Include="Internals\CefRefCountManaged.h">
329329
<Filter>Header Files</Filter>
330330
</ClInclude>
331+
<ClInclude Include="Internals\CefTaskDelegate.h">
332+
<Filter>Header Files</Filter>
333+
</ClInclude>
331334
</ItemGroup>
332335
<ItemGroup>
333336
<ClInclude Include="Internals\CefFrameWrapper.h">
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright © 2022 The CefSharp Authors. All rights reserved.
2+
//
3+
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
4+
5+
#pragma once
6+
7+
#include "Stdafx.h"
8+
#include "include/cef_task.h"
9+
10+
#include "ReportUnhandledExceptions.h"
11+
12+
namespace CefSharp
13+
{
14+
namespace Internals
15+
{
16+
private class CefTaskDelegate : public CefTask
17+
{
18+
private:
19+
gcroot<Action^> _action;
20+
public:
21+
CefTaskDelegate(Action^ action) :
22+
_action(action)
23+
{
24+
};
25+
26+
virtual void Execute() override
27+
{
28+
try
29+
{
30+
_action->Invoke();
31+
}
32+
catch (Exception^ e)
33+
{
34+
auto msg = gcnew String(L"CefTaskDelegate caught an unexpected exception. This exception has been redirected onto the ThreadPool, add a try catch.");
35+
ReportUnhandledExceptions::Report(msg, e);
36+
}
37+
};
38+
39+
IMPLEMENT_REFCOUNTINGM(CefTaskDelegate);
40+
};
41+
}
42+
}

CefSharp.Core/Cef.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -644,5 +644,28 @@ public static void WaitForBrowsersToClose(int timeoutInMiliseconds)
644644
{
645645
Core.Cef.WaitForBrowsersToClose(timeoutInMiliseconds);
646646
}
647+
648+
/// <summary>
649+
/// Post an action for delayed execution on the specified thread.
650+
/// </summary>
651+
/// <param name="threadId">thread id</param>
652+
/// <param name="action">action to execute</param>
653+
/// <param name="delayInMs">delay in ms</param>
654+
/// <returns>bool</returns>
655+
public static bool PostDelayedAction(CefThreadIds threadId, Action action, int delayInMs)
656+
{
657+
return Core.Cef.PostDelayedAction(threadId, action, delayInMs);
658+
}
659+
660+
/// <summary>
661+
/// Post an action for execution on the specified thread.
662+
/// </summary>
663+
/// <param name="threadId">thread id</param>
664+
/// <param name="action">action to execute</param>
665+
/// <returns>bool</returns>
666+
public static bool PostAction(CefThreadIds threadId, Action action)
667+
{
668+
return Core.Cef.PostAction(threadId, action);
669+
}
647670
}
648671
}

0 commit comments

Comments
 (0)