Skip to content

Commit bc7b921

Browse files
committed
Legacy and PerBrowser implementations
1 parent cdc2b11 commit bc7b921

File tree

4 files changed

+103
-31
lines changed

4 files changed

+103
-31
lines changed

CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ namespace CefSharp
3535
CefString _jsBindingPropertyNameCamelCase;
3636

3737
// The serialized registered object data waiting to be used.
38-
gcroot<JavaScriptObjectCache^> _javascriptObjectCache;
38+
gcroot<IJavaScriptObjectCache^> _javascriptObjectCache;
3939

4040
gcroot<RegisterBoundObjectRegistry^> _registerBoundObjectRegistry;
4141

@@ -49,7 +49,7 @@ namespace CefSharp
4949
_onBrowserDestroyed = onBrowserDestroyed;
5050
_browserWrappers = gcnew ConcurrentDictionary<int, CefBrowserWrapper^>();
5151
_focusedNodeChangedEnabled = enableFocusedNodeChanged;
52-
_javascriptObjectCache = gcnew JavaScriptObjectCache();
52+
_javascriptObjectCache = gcnew LegacyJavaScriptObjectCache();
5353
_registerBoundObjectRegistry = gcnew RegisterBoundObjectRegistry();
5454
_legacyBindingEnabled = false;
5555
_jsBindingPropertyName = "CefSharp";
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright © 2023 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+
using System.Collections.Generic;
6+
7+
namespace CefSharp.Internals
8+
{
9+
/// <summary>
10+
/// Render Process JavaScript Binding (JSB) object cache
11+
/// </summary>
12+
public interface IJavaScriptObjectCache
13+
{
14+
/// <summary>
15+
/// Remove the Browser specific Cache
16+
/// </summary>
17+
/// <param name="browserId">browser Id</param>
18+
void ClearCache(int browserId);
19+
/// <summary>
20+
/// Gets the browser specific cache (dictionary) based on it's Id
21+
/// </summary>
22+
/// <param name="browserId">browser Id</param>
23+
/// <returns>Dictionary of cache <see cref="JavascriptObject"/>'s.</returns>
24+
/// <exception cref="InvalidOperationException"></exception>
25+
Dictionary<string, JavascriptObject> GetCache(int browserId);
26+
/// <summary>
27+
/// Gets a collection of <see cref="JavascriptObject"/>s
28+
/// for the given <paramref name="browserId"/>
29+
/// </summary>
30+
/// <param name="browserId">browser Id</param>
31+
/// <returns>Collection of current bound objects for the browser</returns>
32+
/// <exception cref="InvalidOperationException"></exception>
33+
ICollection<JavascriptObject> GetCacheValues(int browserId);
34+
/// <summary>
35+
/// Insert or Update the <paramref name="javascriptObject"/> within the Cache
36+
/// </summary>
37+
/// <param name="browserId">browser id</param>
38+
/// <param name="javascriptObject">JavaScript object</param>
39+
/// <exception cref="InvalidOperationException"></exception>
40+
void InsertOrUpdate(int browserId, IList<JavascriptObject> javascriptObjects);
41+
}
42+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright © 2023 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+
using System.Collections.Generic;
6+
7+
namespace CefSharp.Internals
8+
{
9+
/// <summary>
10+
/// Render Process JavaScript Binding (JSB) object cache
11+
/// Legacy Behaviour, objects are cache per process.
12+
/// </summary>
13+
public class LegacyJavaScriptObjectCache : IJavaScriptObjectCache
14+
{
15+
private readonly Dictionary<string, JavascriptObject> cache
16+
= new Dictionary<string, JavascriptObject>();
17+
18+
/// <inheritdoc/>
19+
public void ClearCache(int browserId)
20+
{
21+
// NO OP
22+
}
23+
24+
/// <inheritdoc/>
25+
public void InsertOrUpdate(int browserId, IList<JavascriptObject> javascriptObjects)
26+
{
27+
foreach (var obj in javascriptObjects)
28+
{
29+
if (cache.ContainsKey(obj.Name))
30+
{
31+
cache.Remove(obj.Name);
32+
}
33+
34+
cache.Add(obj.Name, obj);
35+
}
36+
}
37+
38+
/// <inheritdoc/>
39+
public ICollection<JavascriptObject> GetCacheValues(int browserId)
40+
{
41+
return cache.Values;
42+
}
43+
44+
/// <inheritdoc/>
45+
public Dictionary<string, JavascriptObject> GetCache(int browserId)
46+
{
47+
return cache;
48+
}
49+
}
50+
}

CefSharp/RenderProcess/JavaScriptObjectCache.cs renamed to CefSharp/Internals/PerBrowserJavaScriptObjectCache.cs

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,26 @@
44

55
using System;
66
using System.Collections.Generic;
7-
using CefSharp.Internals;
87

9-
namespace CefSharp.RenderProcess
8+
namespace CefSharp.Internals
109
{
1110
/// <summary>
12-
/// JavaScriptObjectCache is used in the RenderProcess to cache
13-
/// JavaScript bound objects at a CefBrowser level
11+
/// Render Process JavaScript Binding (JSB) object cache
12+
/// Stores bound objects per CefBrowser.
1413
/// </summary>
15-
public class JavaScriptObjectCache
14+
public class PerBrowserJavaScriptObjectCache : IJavaScriptObjectCache
1615
{
1716
private readonly Dictionary<int, Dictionary<string, JavascriptObject>> cache
1817
= new Dictionary<int, Dictionary<string, JavascriptObject>>();
1918

20-
/// <summary>
21-
/// Remove the Browser specific Cache
22-
/// </summary>
23-
/// <param name="browserId">browser Id</param>
19+
/// <inheritdoc/>
2420
public void ClearCache(int browserId)
2521
{
2622
cache.Remove(browserId);
2723
}
2824

29-
/// <summary>
30-
/// Insert or Update the <paramref name="javascriptObject"/> within the Cache
31-
/// </summary>
32-
/// <param name="browserId">browser id</param>
33-
/// <param name="javascriptObject">JavaScript object</param>
34-
/// <exception cref="InvalidOperationException"></exception>
35-
public void InsertOrUpdate(int browserId, IList<JavascriptObject> javascriptObjects)
25+
/// <inheritdoc/>
26+
public void InsertOrUpdate(int browserId, IList<JavascriptObject> javascriptObjects)
3627
{
3728
var dict = GetCacheInternal(browserId);
3829

@@ -47,13 +38,7 @@ public void InsertOrUpdate(int browserId, IList<JavascriptObject> javascriptObj
4738
}
4839
}
4940

50-
/// <summary>
51-
/// Gets a collection of <see cref="JavascriptObject"/>s
52-
/// for the given <paramref name="browserId"/>
53-
/// </summary>
54-
/// <param name="browserId">browser Id</param>
55-
/// <returns>Collection of current bound objects for the browser</returns>
56-
/// <exception cref="InvalidOperationException"></exception>
41+
/// <inheritdoc/>
5742
public ICollection<JavascriptObject> GetCacheValues(int browserId)
5843
{
5944
if (cache.TryGetValue(browserId, out var dict))
@@ -64,12 +49,7 @@ public ICollection<JavascriptObject> GetCacheValues(int browserId)
6449
return new List<JavascriptObject>();
6550
}
6651

67-
/// <summary>
68-
/// Gets the browser specific cache (dictionary) based on it's Id
69-
/// </summary>
70-
/// <param name="browserId">browser Id</param>
71-
/// <returns>Dictionary of cache <see cref="JavascriptObject"/>'s.</returns>
72-
/// <exception cref="InvalidOperationException"></exception>
52+
/// <inheritdoc/>
7353
public Dictionary<string, JavascriptObject> GetCache(int browserId)
7454
{
7555
var dict = GetCacheInternal(browserId);

0 commit comments

Comments
 (0)