admin
2020-06-10 a610f2ab6e543d2cb78c1ef212ac6a74ddc067d9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Copyright © 2019 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.Threading;
using System.Threading.Tasks;
 
namespace CefSharp.Internals
{
    /// <summary>
    /// ConcurrentMethodRunnerQueue - Async Javascript Binding methods are run
    /// on the ThreadPool in parallel, when a method returns a Task
    /// the we use ContinueWith to be notified of completion then
    /// raise the MethodInvocationComplete event
    /// </summary>
    public class ConcurrentMethodRunnerQueue : IMethodRunnerQueue
    {
        private readonly JavascriptObjectRepository repository;
        private CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
 
        public event EventHandler<MethodInvocationCompleteArgs> MethodInvocationComplete;
 
        public ConcurrentMethodRunnerQueue(JavascriptObjectRepository repository)
        {
            this.repository = repository;
        }
 
        public void Dispose()
        {
            cancellationTokenSource.Cancel();
        }
 
        public void Enqueue(MethodInvocation methodInvocation)
        {
            var task = new Task(() =>
            {
                var result = ExecuteMethodInvocation(methodInvocation);
 
                //If the call failed or returned null then we'll fire the event immediately
                if (!result.Success || result.Result == null)
                {
                    OnMethodInvocationComplete(result, cancellationTokenSource.Token);
                }
                else
                {
                    var resultType = result.Result.GetType();
 
                    //If the returned type is Task then we'll ContinueWith and perform the processing then.
                    if (typeof(Task).IsAssignableFrom(resultType))
                    {
                        var resultTask = (Task)result.Result;
 
                        if (resultType.IsGenericType)
                        {
                            resultTask.ContinueWith((t) =>
                            {
                                if (t.Status == TaskStatus.RanToCompletion)
                                {
                                    //We use some reflection to get the Result
                                    //If someone has a better way of doing this then please submit a PR
                                    result.Result = resultType.GetProperty("Result").GetValue(resultTask);
                                }
                                else
                                {
                                    result.Success = false;
                                    result.Result = null;
                                    var aggregateException = t.Exception;
                                    //TODO: Add support for passing a more complex message
                                    // to better represent the Exception
                                    if (aggregateException.InnerExceptions.Count == 1)
                                    {
                                        result.Message = aggregateException.InnerExceptions[0].ToString();
                                    }
                                    else
                                    {
                                        result.Message = t.Exception.ToString();
                                    }
                                }
 
                                OnMethodInvocationComplete(result, cancellationTokenSource.Token);
                            },
                            cancellationTokenSource.Token, TaskContinuationOptions.None, TaskScheduler.Default);
                        }
                        else
                        {
                            //If it's not a generic Task then it doesn't have a return object
                            //So we'll just set the result to null and continue on
                            result.Result = null;
 
                            OnMethodInvocationComplete(result, cancellationTokenSource.Token);
                        }
                    }
                    else
                    {
                        OnMethodInvocationComplete(result, cancellationTokenSource.Token);
                    }
                }
 
            }, cancellationTokenSource.Token);
 
            task.Start(TaskScheduler.Default);
        }
 
        private MethodInvocationResult ExecuteMethodInvocation(MethodInvocation methodInvocation)
        {
            object result = null;
            string exception;
            var success = false;
 
            //make sure we don't throw exceptions in the executor task
            try
            {
                success = repository.TryCallMethod(methodInvocation.ObjectId, methodInvocation.MethodName, methodInvocation.Parameters.ToArray(), out result, out exception);
            }
            catch (Exception e)
            {
                exception = e.Message;
            }
 
            return new MethodInvocationResult
            {
                BrowserId = methodInvocation.BrowserId,
                CallbackId = methodInvocation.CallbackId,
                FrameId = methodInvocation.FrameId,
                Message = exception,
                Result = result,
                Success = success
            };
        }
 
        private void OnMethodInvocationComplete(MethodInvocationResult e, CancellationToken token)
        {
            //If cancellation has been requested we don't need to continue.
            if (!token.IsCancellationRequested)
            {
                MethodInvocationComplete?.Invoke(this, new MethodInvocationCompleteArgs(e));
            }
        }
    }
}