// Copyright © 2023 The CefSharp Authors. All rights reserved.
//
// 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.Internals
{
///
/// Render Process JavaScript Binding (JSB) object cache
/// Stores bound objects per CefBrowser.
///
public class PerBrowserJavaScriptObjectCache : IJavaScriptObjectCache
{
private readonly Dictionary> cache
= new Dictionary>();
///
public void ClearCache(int browserId)
{
cache.Remove(browserId);
}
///
public void InsertOrUpdate(int browserId, IReadOnlyCollection javascriptObjects)
{
var dict = GetCacheInternal(browserId);
foreach (var obj in javascriptObjects)
{
dict[obj.Name] = obj;
}
}
///
public ICollection GetCacheValues(int browserId)
{
if (cache.TryGetValue(browserId, out var dict))
{
return dict.Values;
}
return new List();
}
///
public IDictionary GetCache(int browserId)
{
var dict = GetCacheInternal(browserId);
return dict;
}
private IDictionary GetCacheInternal(int browserId)
{
Dictionary dict;
if (!cache.TryGetValue(browserId, out dict))
{
dict = new Dictionary();
cache.Add(browserId, dict);
}
return dict;
}
}
}