Skip to content

Commit 55af252

Browse files
committed
Core - Refactor JavascriptObjectRepository to IJavascriptObjectRepositoryInternal
Basic refactoring in preparation for allowing users(advanced) to provide their own custom JavascriptObjectRepository implementation
1 parent 83ae939 commit 55af252

11 files changed

+59
-22
lines changed

CefSharp.Core/ManagedCefBrowserAdapter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ IJavascriptCallbackFactory^ ManagedCefBrowserAdapter::JavascriptCallbackFactory:
179179
return _javascriptCallbackFactory;
180180
}
181181

182-
JavascriptObjectRepository^ ManagedCefBrowserAdapter::JavascriptObjectRepository::get()
182+
IJavascriptObjectRepositoryInternal^ ManagedCefBrowserAdapter::JavascriptObjectRepository::get()
183183
{
184184
return _javaScriptObjectRepository;
185185
}

CefSharp.Core/ManagedCefBrowserAdapter.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ namespace CefSharp
3636
BrowserProcessServiceHost^ _browserProcessServiceHost;
3737
#endif
3838
IWebBrowserInternal^ _webBrowserInternal;
39-
JavascriptObjectRepository^ _javaScriptObjectRepository;
39+
IJavascriptObjectRepositoryInternal^ _javaScriptObjectRepository;
4040
JavascriptCallbackFactory^ _javascriptCallbackFactory;
4141
IMethodRunnerQueue^ _methodRunnerQueue;
4242
IBrowser^ _browserWrapper;
@@ -138,9 +138,9 @@ namespace CefSharp
138138
CefSharp::Internals::IJavascriptCallbackFactory^ get();
139139
}
140140

141-
virtual property JavascriptObjectRepository^ JavascriptObjectRepository
141+
virtual property IJavascriptObjectRepositoryInternal^ JavascriptObjectRepository
142142
{
143-
CefSharp::Internals::JavascriptObjectRepository^ get();
143+
CefSharp::Internals::IJavascriptObjectRepositoryInternal^ get();
144144
}
145145

146146
virtual property IMethodRunnerQueue^ MethodRunnerQueue

CefSharp.Test/Framework/ConcurrentMethodRunnerQueueFacts.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public async Task StopConcurrentMethodRunnerQueueWhenMethodRunning()
2626
{
2727
var boundObject = new AsyncBoundObject();
2828

29-
var objectRepository = new JavascriptObjectRepository();
29+
IJavascriptObjectRepositoryInternal objectRepository = new JavascriptObjectRepository();
3030
objectRepository.NameConverter = null;
3131
#if NETCOREAPP
3232
objectRepository.Register("testObject", boundObject, BindingOptions.DefaultBinder);
@@ -53,7 +53,7 @@ public async Task ValidateAsyncTaskMethodOutput()
5353
const string expectedResult = "Echo Me!";
5454
var boundObject = new AsyncBoundObject();
5555

56-
var objectRepository = new JavascriptObjectRepository();
56+
IJavascriptObjectRepositoryInternal objectRepository = new JavascriptObjectRepository();
5757
objectRepository.NameConverter = null;
5858
#if NETCOREAPP
5959
objectRepository.Register("testObject", boundObject, BindingOptions.DefaultBinder);

CefSharp/CefSharp.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@
139139
<Compile Include="Internals\BrowserRefCounter.cs" />
140140
<Compile Include="Internals\CommandLineArgDictionary.cs" />
141141
<Compile Include="Internals\CookieManagerDecorator.cs" />
142+
<Compile Include="Internals\IJavascriptObjectRepositoryInternal.cs" />
142143
<Compile Include="Internals\ParentProcessMonitor.cs" />
143144
<Compile Include="Internals\HeaderNameValueCollection.cs" />
144145
<Compile Include="Internals\StringCheck.cs" />

CefSharp/Internals/ConcurrentMethodRunnerQueue.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ namespace CefSharp.Internals
1616
/// </summary>
1717
public class ConcurrentMethodRunnerQueue : IMethodRunnerQueue
1818
{
19-
private readonly JavascriptObjectRepository repository;
19+
private readonly IJavascriptObjectRepositoryInternal repository;
2020
private CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
2121

2222
public event EventHandler<MethodInvocationCompleteArgs> MethodInvocationComplete;
2323

24-
public ConcurrentMethodRunnerQueue(JavascriptObjectRepository repository)
24+
public ConcurrentMethodRunnerQueue(IJavascriptObjectRepositoryInternal repository)
2525
{
2626
this.repository = repository;
2727
}

CefSharp/Internals/IBrowserAdapter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace CefSharp.Internals
1111
public interface IBrowserAdapter
1212
{
1313
IMethodRunnerQueue MethodRunnerQueue { get; }
14-
JavascriptObjectRepository JavascriptObjectRepository { get; }
14+
IJavascriptObjectRepositoryInternal JavascriptObjectRepository { get; }
1515
IJavascriptCallbackFactory JavascriptCallbackFactory { get; }
1616
void OnAfterBrowserCreated(IBrowser browser);
1717
IBrowser GetBrowser(int browserId);
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright © 2020 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+
6+
using System;
7+
using System.Collections.Generic;
8+
9+
namespace CefSharp.Internals
10+
{
11+
public interface IJavascriptObjectRepositoryInternal : IJavascriptObjectRepository
12+
{
13+
bool TryCallMethod(long objectId, string name, object[] parameters, out object result, out string exception);
14+
bool TryGetProperty(long objectId, string name, out object result, out string exception);
15+
bool TrySetProperty(long objectId, string name, object value, out string exception);
16+
bool IsBrowserInitialized { get; set; }
17+
List<JavascriptObject> GetObjects(List<string> names = null);
18+
List<JavascriptObject> GetLegacyBoundObjects();
19+
void ObjectsBound(List<Tuple<string, bool, bool>> objs);
20+
}
21+
}

CefSharp/Internals/JavascriptObjectRepository.cs

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ namespace CefSharp.Internals
3131
/// All of the registered objects are tracked via meta-data for the objects
3232
/// expressed starting with the JavaScriptObject type.
3333
/// </summary>
34-
public class JavascriptObjectRepository : FreezableBase, IJavascriptObjectRepository
34+
public class JavascriptObjectRepository : FreezableBase, IJavascriptObjectRepositoryInternal
3535
{
3636
public const string AllObjects = "All";
3737
public const string LegacyObjects = "Legacy";
@@ -47,7 +47,7 @@ public class JavascriptObjectRepository : FreezableBase, IJavascriptObjectReposi
4747
/// this is done to speed up finding the object in O(1) time
4848
/// instead of traversing the JavaScriptRootObject tree.
4949
/// </summary>
50-
private readonly ConcurrentDictionary<long, JavascriptObject> objects = new ConcurrentDictionary<long, JavascriptObject>();
50+
protected readonly ConcurrentDictionary<long, JavascriptObject> objects = new ConcurrentDictionary<long, JavascriptObject>();
5151

5252
/// <summary>
5353
/// Javascript Name converter
@@ -103,7 +103,7 @@ public bool IsBound(string name)
103103
return objects.Values.Any(x => x.Name == name);
104104
}
105105

106-
public List<JavascriptObject> GetLegacyBoundObjects()
106+
List<JavascriptObject> IJavascriptObjectRepositoryInternal.GetLegacyBoundObjects()
107107
{
108108
RaiseResolveObjectEvent(LegacyObjects);
109109

@@ -112,7 +112,7 @@ public List<JavascriptObject> GetLegacyBoundObjects()
112112

113113
//Ideally this would internal, unfurtunately it's used in C++
114114
//and it's hard to expose internals
115-
public List<JavascriptObject> GetObjects(List<string> names = null)
115+
List<JavascriptObject> IJavascriptObjectRepositoryInternal.GetObjects(List<string> names)
116116
{
117117
//If there are no objects names or the count is 0 then we will raise
118118
//the resolve event then return all objects that are registered,
@@ -140,7 +140,7 @@ public List<JavascriptObject> GetObjects(List<string> names = null)
140140
return objectsByName;
141141
}
142142

143-
public void ObjectsBound(List<Tuple<string, bool, bool>> objs)
143+
void IJavascriptObjectRepositoryInternal.ObjectsBound(List<Tuple<string, bool, bool>> objs)
144144
{
145145
var boundObjectHandler = ObjectBoundInJavascript;
146146
var boundObjectsHandler = ObjectsBoundInJavascript;
@@ -255,7 +255,12 @@ public bool UnRegister(string name)
255255
return false;
256256
}
257257

258-
internal bool TryCallMethod(long objectId, string name, object[] parameters, out object result, out string exception)
258+
bool IJavascriptObjectRepositoryInternal.TryCallMethod(long objectId, string name, object[] parameters, out object result, out string exception)
259+
{
260+
return TryCallMethod(objectId, name, parameters, out result, out exception);
261+
}
262+
263+
protected virtual bool TryCallMethod(long objectId, string name, object[] parameters, out object result, out string exception)
259264
{
260265
exception = "";
261266
result = null;
@@ -384,7 +389,12 @@ internal bool TryCallMethod(long objectId, string name, object[] parameters, out
384389
return false;
385390
}
386391

387-
internal bool TryGetProperty(long objectId, string name, out object result, out string exception)
392+
bool IJavascriptObjectRepositoryInternal.TryGetProperty(long objectId, string name, out object result, out string exception)
393+
{
394+
return TryGetProperty(objectId, name, out result, out exception);
395+
}
396+
397+
protected virtual bool TryGetProperty(long objectId, string name, out object result, out string exception)
388398
{
389399
exception = "";
390400
result = null;
@@ -414,7 +424,12 @@ internal bool TryGetProperty(long objectId, string name, out object result, out
414424
return false;
415425
}
416426

417-
internal bool TrySetProperty(long objectId, string name, object value, out string exception)
427+
bool IJavascriptObjectRepositoryInternal.TrySetProperty(long objectId, string name, object value, out string exception)
428+
{
429+
return TrySetProperty(objectId, name, value, out exception);
430+
}
431+
432+
protected virtual bool TrySetProperty(long objectId, string name, object value, out string exception)
418433
{
419434
exception = "";
420435
JavascriptObject obj;

CefSharp/Internals/MethodRunnerQueue.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ public sealed class MethodRunnerQueue : IMethodRunnerQueue
1414
//Limit to 1 task per methodRunnerQueue
1515
//https://social.msdn.microsoft.com/Forums/vstudio/en-US/d0bcb415-fb1e-42e4-90f8-c43a088537fb/aborting-a-long-running-task-in-tpl?forum=parallelextensions
1616
private readonly LimitedConcurrencyLevelTaskScheduler taskScheduler = new LimitedConcurrencyLevelTaskScheduler(1);
17-
private readonly JavascriptObjectRepository repository;
17+
private readonly IJavascriptObjectRepositoryInternal repository;
1818
private CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
1919

2020
public event EventHandler<MethodInvocationCompleteArgs> MethodInvocationComplete;
2121

22-
public MethodRunnerQueue(JavascriptObjectRepository repository)
22+
public MethodRunnerQueue(IJavascriptObjectRepositoryInternal repository)
2323
{
2424
this.repository = repository;
2525
}

CefSharp/Internals/Wcf/BrowserProcessService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace CefSharp.Internals.Wcf
99
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession, ConcurrencyMode = ConcurrencyMode.Multiple)]
1010
internal class BrowserProcessService : IBrowserProcess
1111
{
12-
private readonly JavascriptObjectRepository javascriptObjectRepository;
12+
private readonly IJavascriptObjectRepositoryInternal javascriptObjectRepository;
1313
private readonly BrowserProcessServiceHost host;
1414

1515
public BrowserProcessService()

0 commit comments

Comments
 (0)