Ejemplo n.º 1
0
        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);
        }
Ejemplo n.º 2
0
        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"));
        }