况洋洋
2025-07-04 0d247bd2a17e0f99f3609774a1ce54ae00857997
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 © 2020 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.Text;
using System.Threading;
 
namespace CefSharp.Internals
{
    /// <summary>
    /// Tracks the number of browser instances currently open
    /// The cound will be incrmented and decremented each time a CefBrowser is created/closed.
    /// This includes CefBrowser popup instances.
    /// </summary>
    /// <remarks>
    /// Roughtly based on <see cref="CountdownEvent"/>, unforeunately <see cref="CountdownEvent.AddCount(int)"/>
    /// doesn't reset the internal <see cref="ManualResetEventSlim"/> when Count is aleady 0.
    /// In our case it's valid to increase the number of browsers and reset the event.
    /// </remarks>
    public sealed class BrowserRefCounter : IBrowserRefCounter
    {
        private volatile int count = 0;
        private ManualResetEventSlim manualResetEvent = new ManualResetEventSlim();
        private bool loggingEnabled = false;
        private StringBuilder logger = new StringBuilder();
 
        /// TODO: Refactor this so it's not static.
        public static IBrowserRefCounter Instance = new NoOpBrowserRefCounter();
 
        /// <summary>
        /// If logging is enabled the <paramref name="line"/> will be appended to
        /// the internal log.
        /// </summary>
        /// <param name="line">text to append to log if logging enabled.</param>
        public void AppendLineToLog(string line)
        {
            if(loggingEnabled)
            {
                logger.AppendLine(line);
            }
        }
 
        /// <inheritdoc/>
        void IBrowserRefCounter.Increment(Type type)
        {
            var newCount = Interlocked.Increment(ref count);
 
            if (newCount > 0)
            {
                manualResetEvent.Reset();
 
                AppendLineToLog($"{type} - Incremented (ManualResetEvent was reset)");
            }
            else if(loggingEnabled)
            {
                logger.AppendLine($"New Count <= 0 - {newCount} ");
            }
        }
 
        /// <inheritdoc/>
        bool IBrowserRefCounter.Decrement(Type type)
        {
            var newCount = Interlocked.Decrement(ref count);
 
            AppendLineToLog($"{type} - Decremented (Current Count {newCount})");
 
            if (newCount == 0)
            {
                manualResetEvent.Set();
                return true;
            }
 
            if (newCount < 0)
            {
                AppendLineToLog($"{type} - Decremented (Less than 0 : Current Count {newCount})");
 
                //If we went below 0 then reset to 0
                // TODO: something went wrong with our tracking
                Interlocked.Exchange(ref count, 0);
 
                manualResetEvent.Set();
            }
 
            return false;
        }
 
        /// <inheritdoc/>
        int IBrowserRefCounter.Count
        {
            get
            {
                int observedCount = count;
                return observedCount < 0 ? 0 : observedCount;
            }
        }
 
        /// <inheritdoc/>
        void IBrowserRefCounter.WaitForBrowsersToClose(int timeoutInMiliseconds)
        {
            AppendLineToLog($"WaitForBrowsersToClose - Current Count {count}");
 
            if (!manualResetEvent.IsSet)
            {
                manualResetEvent.Wait(timeoutInMiliseconds);
            }
 
            AppendLineToLog($"WaitForBrowsersToClose - Updated Count {count}");
        }
 
        /// <inheritdoc/>
        void IBrowserRefCounter.WaitForBrowsersToClose(int timeoutInMiliseconds, CancellationToken cancellationToken)
        {
            AppendLineToLog($"WaitForBrowsersToClose - Current Count {count}");
 
            if (!manualResetEvent.IsSet)
            {
                manualResetEvent.Wait(timeoutInMiliseconds, cancellationToken);
            }
 
            AppendLineToLog($"WaitForBrowsersToClose - Updated Count {count}");
        }
 
        /// <inheritdoc/>
        void IBrowserRefCounter.EnableLogging()
        {
            loggingEnabled = true;
        }
 
        /// <inheritdoc/>
        string IBrowserRefCounter.GetLog()
        {
            return logger.ToString();
        }
 
        public void Dispose()
        {
            manualResetEvent.Dispose();
        }
    }
}