private static DevToolsProtocolClient GetProtocolClient(CefBrowserHost browserHost) { if (browserHost is null) { throw new ArgumentNullException(nameof(browserHost)); } DevToolsProtocolClient client; long browserId = browserHost.Browser.Identifier; lock (_Clients) { if (!_Clients.TryGetValue(browserId, out client)) { var webview = browserHost.Client.GetWebView() as IChromiumWebViewPrivate; if (webview is null) { throw new InvalidOperationException("This browser is not associated with a WebView control."); } client = new DevToolsProtocolClient(webview); _Clients.Add(browserId, client); } } return(client); }
private static async Task <byte[]> ExecuteDevToolsMethodInternalAsync(IChromiumWebView webview, string method, CefDictionaryValue parameters, CancellationToken cancellationToken) { CefBrowser browser = webview.BrowserObject; if (browser is null) { throw new InvalidOperationException(); } cancellationToken.ThrowIfCancellationRequested(); CefBrowserHost browserHost = browser.Host; DevToolsProtocolClient protocolClient = GetProtocolClient(browserHost); await CefNetSynchronizationContextAwaiter.GetForThread(CefThreadId.UI); cancellationToken.ThrowIfCancellationRequested(); int messageId = browserHost.ExecuteDevToolsMethod(protocolClient.IncrementMessageId(), method, parameters); protocolClient.UpdateLastMessageId(messageId); DevToolsMethodResult r; if (cancellationToken.CanBeCanceled) { Task <DevToolsMethodResult> waitTask = protocolClient.WaitForMessageAsync(messageId); await Task.WhenAny(waitTask, Task.Delay(Timeout.Infinite, cancellationToken)).ConfigureAwait(false); cancellationToken.ThrowIfCancellationRequested(); r = waitTask.Result; } else { r = await protocolClient.WaitForMessageAsync(messageId).ConfigureAwait(false); } if (r.Success) { return(r.Result); } CefValue errorValue = CefApi.CefParseJSONBuffer(r.Result, CefJsonParserOptions.AllowTrailingCommas); if (errorValue is null) { throw new DevToolsProtocolException($"An unknown error occurred while trying to execute the '{method}' method."); } throw new DevToolsProtocolException(errorValue.GetDictionary().GetString("message")); }
/// <summary> /// Returns a WebView control for this <see cref="CefBrowser"/>. /// </summary> /// <param name="browser">The <see cref="CefBrowser"/> object.</param> /// <returns>An <see cref="IChromiumWebView"/> object associated with this client or null.</returns> public static IChromiumWebView GetWebView(this CefBrowser browser) { if (browser is null) { throw new ArgumentNullException(nameof(browser)); } CefBrowserHost host = browser.Host; if (host is null) { return(null); } CefClient client = host.Client; if (client is null) { return(null); } return(GetWebView(client)); }
/// <summary> /// Returns a number that uniquely identifies the DevTools Protocol message. /// </summary> /// <param name="browserHost">The browser instance.</param> /// <returns>A number that uniquely identifies the protocol message.</returns> public static int GetNextDevToolsMessageId(this CefBrowserHost browserHost) { return(GetProtocolClient(browserHost).IncrementMessageId()); }