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
142
143
144
145
146
147
148
149
150
151
152
153
// Copyright © 2017 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;
using System.Threading.Tasks;
 
namespace CefSharp
{
    /// <summary>
    /// Async extensions for different interfaces
    /// </summary>
    public static class AsyncExtensions
    {
        /// <summary>
        /// Deletes all cookies that matches all the provided parameters asynchronously.
        /// If both <paramref name="url"/> and <paramref name="name"/> are empty, all cookies will be deleted.
        /// </summary>
        /// <param name="cookieManager">cookie manager</param>
        /// <param name="url">The cookie URL. If an empty string is provided, any URL will be matched.</param>
        /// <param name="name">The name of the cookie. If an empty string is provided, any URL will be matched.</param>
        /// <returns>Returns -1 if a non-empty invalid URL is specified, or if cookies cannot be accessed;
        /// otherwise, a task that represents the delete operation. The value of the TResult will be the number of cookies that were deleted or -1 if unknown.</returns>
        public static Task<int> DeleteCookiesAsync(this ICookieManager cookieManager, string url = null, string name = null)
        {
            if (cookieManager == null)
            {
                throw new NullReferenceException("cookieManager");
            }
 
            if (cookieManager.IsDisposed)
            {
                throw new ObjectDisposedException("cookieManager");
            }
 
            var callback = new TaskDeleteCookiesCallback();
            if (cookieManager.DeleteCookies(url, name, callback))
            {
                return callback.Task;
            }
 
            //There was a problem deleting cookies
            return Task.FromResult(TaskDeleteCookiesCallback.InvalidNoOfCookiesDeleted);
        }
 
        /// <summary>
        /// Sets a cookie given a valid URL and explicit user-provided cookie attributes.
        /// This function expects each attribute to be well-formed. It will check for disallowed
        /// characters (e.g. the ';' character is disallowed within the cookie value attribute) and will return false without setting
        /// </summary>
        /// <param name="cookieManager">cookie manager</param>
        /// <param name="url">The cookie URL. If an empty string is provided, any URL will be matched.</param>
        /// <param name="cookie">the cookie to be set</param>
        /// <returns>returns false if the cookie cannot be set (e.g. if illegal charecters such as ';' are used);
        /// otherwise task that represents the set operation. The value of the TResult parameter contains a bool to indicate success.</returns>
        public static Task<bool> SetCookieAsync(this ICookieManager cookieManager, string url, Cookie cookie)
        {
            if (cookieManager == null)
            {
                throw new NullReferenceException("cookieManager");
            }
 
            if (cookieManager.IsDisposed)
            {
                throw new ObjectDisposedException("cookieManager");
            }
 
            var callback = new TaskSetCookieCallback();
            if (cookieManager.SetCookie(url, cookie, callback))
            {
                return callback.Task;
            }
 
            //There was a problem setting cookies
            return Task.FromResult(false);
        }
 
        /// <summary>
        /// Visits all cookies. The returned cookies are sorted by longest path, then by earliest creation date.
        /// </summary>
        /// <param name="cookieManager">cookie manager</param>
        /// <returns>A task that represents the VisitAllCookies operation. The value of the TResult parameter contains a List of cookies
        /// or null if cookies cannot be accessed.</returns>
        public static Task<List<Cookie>> VisitAllCookiesAsync(this ICookieManager cookieManager)
        {
            var cookieVisitor = new TaskCookieVisitor();
 
            if (cookieManager.VisitAllCookies(cookieVisitor))
            {
                return cookieVisitor.Task;
            }
 
            return Task.FromResult<List<Cookie>>(null);
        }
 
        /// <summary>
        /// Visits a subset of the cookies. The results are filtered by the given url scheme, host, domain and path. 
        /// If <paramref name="includeHttpOnly"/> is true, HTTP-only cookies will also be included in the results. The returned cookies 
        /// are sorted by longest path, then by earliest creation date.
        /// </summary>
        /// <param name="cookieManager">cookie manager</param>
        /// <param name="url">The URL to use for filtering a subset of the cookies available.</param>
        /// <param name="includeHttpOnly">A flag that determines whether HTTP-only cookies will be shown in results.</param>
        /// <returns>A task that represents the VisitUrlCookies operation. The value of the TResult parameter contains a List of cookies.
        /// or null if cookies cannot be accessed.</returns>
        public static Task<List<Cookie>> VisitUrlCookiesAsync(this ICookieManager cookieManager, string url, bool includeHttpOnly)
        {
            var cookieVisitor = new TaskCookieVisitor();
 
            if (cookieManager.VisitUrlCookies(url, includeHttpOnly, cookieVisitor))
            {
                return cookieVisitor.Task;
            }
 
            return Task.FromResult<List<Cookie>>(null);
        }
 
        /// <summary>
        /// Flush the backing store (if any) to disk.
        /// </summary>
        /// <param name="cookieManager">cookieManager instance</param>
        /// <returns>A task that represents the FlushStore operation. Result indicates if the flush completed successfully.
        /// Will return false if the cookikes cannot be accessed.</returns>
        public static Task<bool> FlushStoreAsync(this ICookieManager cookieManager)
        {
            var handler = new TaskCompletionCallback();
 
            if (cookieManager.FlushStore(handler))
            {
                return handler.Task;
            }
 
            //returns null if cookies cannot be accessed.
            return Task.FromResult(false);
        }
 
        /// <summary>
        /// Retrieve a snapshot of current navigation entries
        /// </summary>
        /// <param name="browserHost">browserHost</param>
        /// <param name="currentOnly">If true the List will only contain the current navigation entry.
        /// If false the List will include all navigation entries will be included. Default is false</param>
        public static Task<List<NavigationEntry>> GetNavigationEntriesAsync(this IBrowserHost browserHost, bool currentOnly = false)
        {
            var visitor = new TaskNavigationEntryVisitor();
 
            browserHost.GetNavigationEntries(visitor, currentOnly);
 
            return visitor.Task;
        }
    }
}