forked from hardkoded/puppeteer-sharp
-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathWebBrowserExtensions.cs
More file actions
142 lines (120 loc) · 5.76 KB
/
WebBrowserExtensions.cs
File metadata and controls
142 lines (120 loc) · 5.76 KB
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
using System;
using System.ComponentModel;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
namespace CefSharp.Dom
{
/// <summary>
/// WebBrowserExtensions
/// </summary>
public static class WebBrowserExtensions
{
/// <summary>
/// Asynchronously creates a new instance of <see cref="DevToolsContext"/>. It's reccommended that you only
/// create a single <see cref="DevToolsContext"/> per ChromiumWebBrowser instance. Store and reuse a single reference.
/// If you need to create multiple, make sure to Dispose of the previous instance before creating a new instance.
/// </summary>
/// <param name="chromiumWebBrowser">ChromiumWebBrowser/ChromiumHostControl instance</param>
/// <param name="ignoreHTTPSerrors">ignore HTTPS errors</param>
/// <param name="factory">Logger factory</param>
/// <returns>A Task</returns>
public static async Task<DevToolsContext> CreateDevToolsContextAsync(
this IChromiumWebBrowserBase chromiumWebBrowser,
bool ignoreHTTPSerrors = false,
ILoggerFactory factory = null)
{
if (chromiumWebBrowser == null)
{
throw new ArgumentNullException(nameof(chromiumWebBrowser));
}
if (chromiumWebBrowser.IsDisposed)
{
throw new ObjectDisposedException(chromiumWebBrowser.GetType().Name);
}
DevToolsContext ctx;
var internalWebBrowser = chromiumWebBrowser as Internals.IWebBrowserInternal;
if (internalWebBrowser != null)
{
ctx = internalWebBrowser.DevToolsContext as DevToolsContext;
if (ctx != null && !ctx.IsDisposed)
{
// We already have an existing DevToolsContext, reuse it as having
// multiple concurrent isn't supported.
return ctx;
}
}
var browserHost = chromiumWebBrowser.GetBrowserHost();
if (browserHost == null)
{
CefSharp.WebBrowserExtensions.ThrowExceptionIfBrowserHostNull(browserHost);
}
#pragma warning disable CA2000 // Dispose objects before losing scope
var connection = DevToolsConnection.Attach(
new CefSharpConnectionTransport(chromiumWebBrowser.BrowserCore.Identifier, browserHost), factory);
#pragma warning restore CA2000 // Dispose objects before losing scope
ctx = await DevToolsContext.CreateDevToolsContextAsync(connection, ignoreHTTPSerrors: ignoreHTTPSerrors)
.ConfigureAwait(false);
if (internalWebBrowser != null)
{
internalWebBrowser.DevToolsContext = ctx;
}
return ctx;
}
/// <summary>
/// Asynchronously creates a new instance of <see cref="DevToolsContext"/>. It's reccommended that you only
/// create a single <see cref="DevToolsContext"/> per <see cref="IBrowser"/> instance. Store and reuse a single reference.
/// If you need to create multiple, make sure to Dispose of the previous instance before creating a new instance.
/// </summary>
/// <param name="browser">CEF Browser instance</param>
/// <param name="ignoreHTTPSerrors">ignore HTTPS errors</param>
/// <param name="factory">Logger factory</param>
/// <returns>A Task</returns>
public static async Task<DevToolsContext> CreateDevToolsContextAsync(
this IBrowser browser,
bool ignoreHTTPSerrors = false,
ILoggerFactory factory = null)
{
if (browser == null)
{
throw new ArgumentNullException(nameof(browser));
}
if (browser.IsDisposed)
{
throw new ObjectDisposedException(browser.GetType().Name);
}
if (browser.IsValid)
{
throw new Exception("Invalid Browser");
}
DevToolsContext ctx;
var browserHost = browser.GetHost();
if (browserHost == null)
{
CefSharp.WebBrowserExtensions.ThrowExceptionIfBrowserHostNull(browserHost);
}
#pragma warning disable CA2000 // Dispose objects before losing scope
var connection = DevToolsConnection.Attach(new CefSharpConnectionTransport(browser.Identifier, browserHost), factory);
#pragma warning restore CA2000 // Dispose objects before losing scope
ctx = await DevToolsContext.CreateDevToolsContextAsync(connection, ignoreHTTPSerrors: ignoreHTTPSerrors)
.ConfigureAwait(false);
return ctx;
}
/// <summary>
/// Asynchronously creates a new instance of <see cref="DevToolsContext"/>. It's reccommended that you only
/// create a single <see cref="DevToolsContext"/> per ChromiumWebBrowser instance. Store and reuse a single reference.
/// If you need to create multiple, make sure to Dispose of the previous instance before creating a new instance.
/// </summary>
/// <param name="chromiumWebBrowser">ChromiumWebBrowser/ChromiumHostControl instance</param>
/// <param name="ignoreHTTPSerrors">ignore HTTPS errors</param>
/// <param name="factory">Logger factory</param>
/// <returns>A Task</returns>
[EditorBrowsable(EditorBrowsableState.Never)]
public static Task<DevToolsContext> GetDevToolsContextAsync(
this IChromiumWebBrowserBase chromiumWebBrowser,
bool ignoreHTTPSerrors = false,
ILoggerFactory factory = null)
{
return chromiumWebBrowser.CreateDevToolsContextAsync(ignoreHTTPSerrors, factory);
}
}
}