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
// 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.Tasks;
 
namespace CefSharp.Internals
{
    /// <summary>
    /// CookieManager with additional checks to ensure the store is initialized.
    /// Throws an exception when attempting to access the store before it's ready.
    /// </summary>
    public class CookieManagerDecorator : ICookieManager
    {
        private const string NotInitialziedExceptionMsg = "CookieManager store is not initialized.";
        private ICookieManager manager;
        private volatile bool managerReady;
 
        internal CookieManagerDecorator(ICookieManager manager, TaskCompletionCallback callback)
        {
            if (manager == null)
            {
                throw new ArgumentNullException("manager");
            }
            if (callback == null)
            {
                throw new ArgumentNullException("manager");
            }
 
            this.manager = manager;
 
            if (callback.Task.Status == TaskStatus.RanToCompletion)
            {
                managerReady = callback.Task.Result;
            }
            else
            {
                callback.Task.ContinueWith(x =>
                {
                    managerReady = x.Result;
                });
            }
        }
 
        bool ICookieManager.IsDisposed
        {
            get { return manager.IsDisposed; }
        }
 
        bool ICookieManager.DeleteCookies(string url, string name, IDeleteCookiesCallback callback)
        {
            if (managerReady)
            {
                return manager.DeleteCookies(url, name, callback);
            }
 
            throw new InvalidOperationException(NotInitialziedExceptionMsg);
        }
 
        void IDisposable.Dispose()
        {
            manager.Dispose();
        }
 
        bool ICookieManager.FlushStore(ICompletionCallback callback)
        {
            if (managerReady)
            {
                return manager.FlushStore(callback);
            }
 
            throw new InvalidOperationException(NotInitialziedExceptionMsg);
        }
 
        bool ICookieManager.SetCookie(string url, Cookie cookie, ISetCookieCallback callback)
        {
            if (managerReady)
            {
                return manager.SetCookie(url, cookie, callback);
            }
 
            throw new InvalidOperationException(NotInitialziedExceptionMsg);
        }
 
        void ICookieManager.SetSupportedSchemes(string[] schemes, bool includeDefaults, ICompletionCallback callback)
        {
            if (managerReady)
            {
                manager.SetSupportedSchemes(schemes, includeDefaults, callback);
            }
 
            throw new InvalidOperationException(NotInitialziedExceptionMsg);
        }
 
        bool ICookieManager.VisitAllCookies(ICookieVisitor visitor)
        {
            if (managerReady)
            {
                return manager.VisitAllCookies(visitor);
            }
 
            throw new InvalidOperationException(NotInitialziedExceptionMsg);
        }
 
        bool ICookieManager.VisitUrlCookies(string url, bool includeHttpOnly, ICookieVisitor visitor)
        {
            if (managerReady)
            {
                return manager.VisitUrlCookies(url, includeHttpOnly, visitor);
            }
 
            throw new InvalidOperationException(NotInitialziedExceptionMsg);
        }
    }
}