// Copyright © 2015 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.Concurrent;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace CefSharp.Internals
{
///
/// Class to store TaskCompletionSources indexed by a unique id. There are two distinct ConcurrentDictionary
/// instances as we have some Tasks that are created from the browser process (EvaluateScriptAsync) calls, and
/// some that are created for instances for which the Id's are created
/// in the render process.
///
/// The type of the result produced by the tasks held.
public sealed class PendingTaskRepository
{
private readonly ConcurrentDictionary> framePendingTasks =
new ConcurrentDictionary>();
//should only be accessed by Interlocked.Increment
private long lastId;
///
/// Creates a new pending task with a timeout.
///
/// The frame id in which the task is created.
/// The maximum running time of the task.
/// The unique id of the newly created pending task and the newly created .
public KeyValuePair> CreatePendingTask(string frameId, TimeSpan? timeout = null)
{
var taskCompletionSource = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
var id = Interlocked.Increment(ref lastId);
var result = new KeyValuePair>(id, taskCompletionSource);
#if NETCOREAPP
framePendingTasks.AddOrUpdate(
frameId,
(key, state) => { var value = new FramePendingTaskRepository(); value.PendingTasks.TryAdd(state.Key, state.Value); return value; },
(key, value, state) => { value.PendingTasks.TryAdd(state.Key, state.Value); return value; },
result
);
#else
framePendingTasks.AddOrUpdate(
frameId,
(key) => { var value = new FramePendingTaskRepository(); value.PendingTasks.TryAdd(id, taskCompletionSource); return value; },
(key, value) => { value.PendingTasks.TryAdd(id, taskCompletionSource); return value; }
);
#endif
if (timeout.HasValue)
{
taskCompletionSource.WithTimeout(timeout.Value, () => RemovePendingTask(frameId, id));
}
return result;
}
///
/// Creates a new pending task with a timeout.
///
/// The frame id in which the task is created.
/// Id passed in from the render process
/// The maximum running time of the task.
/// The unique id of the newly created pending task and the newly created .
public KeyValuePair> CreateJavascriptCallbackPendingTask(string frameId, long id, TimeSpan? timeout = null)
{
var taskCompletionSource = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
var result = new KeyValuePair>(id, taskCompletionSource);
#if NETCOREAPP
framePendingTasks.AddOrUpdate(
frameId,
(key, state) => { var value = new FramePendingTaskRepository(); value.CallbackPendingTasks.TryAdd(state.Key, state.Value); return value; },
(key, value, state) => { value.CallbackPendingTasks.TryAdd(state.Key, state.Value); return value; },
result
);
#else
framePendingTasks.AddOrUpdate(
frameId,
(key) => { var value = new FramePendingTaskRepository(); value.CallbackPendingTasks.TryAdd(id, taskCompletionSource); return value; },
(key, value) => { value.CallbackPendingTasks.TryAdd(id, taskCompletionSource); return value; }
);
#endif
if (timeout.HasValue)
{
taskCompletionSource.WithTimeout(timeout.Value, () => RemoveJavascriptCallbackPendingTask(frameId, id));
}
return result;
}
///
/// If a is found matching
/// then it is removed from the ConcurrentDictionary and returned.
///
/// The frame id.
/// Unique id of the pending task.
///
/// The associated with the given id
/// or null if no matching TaskComplectionSource found.
///
public TaskCompletionSource RemovePendingTask(string frameId, long id)
{
FramePendingTaskRepository repository;
if (framePendingTasks.TryGetValue(frameId, out repository))
{
TaskCompletionSource result;
if (repository.PendingTasks.TryRemove(id, out result))
{
return result;
}
}
return null;
}
///
/// If a is found matching
/// then it is removed from the ConcurrentDictionary and returned.
///
/// The frame id.
/// Unique id of the pending task.
///
/// The associated with the given id
/// or null if no matching TaskComplectionSource found.
///
public TaskCompletionSource RemoveJavascriptCallbackPendingTask(string frameId, long id)
{
FramePendingTaskRepository repository;
if (framePendingTasks.TryGetValue(frameId, out repository))
{
TaskCompletionSource result;
if (repository.CallbackPendingTasks.TryRemove(id, out result))
{
return result;
}
}
return null;
}
///
/// Cancels all pending tasks of a frame.
///
/// The frame id.
public void CancelPendingTasks(string frameId)
{
FramePendingTaskRepository repository;
if (framePendingTasks.TryRemove(frameId, out repository))
{
repository.Dispose();
}
}
///
/// Cancels all pending tasks.
///
public void CancelPendingTasks()
{
foreach (var repository in framePendingTasks.Values)
{
repository.Dispose();
}
framePendingTasks.Clear();
}
}
}