Exemplo n.º 1
0
        public ChromiumSession(ChromiumConnection chromiumConnection, TargetType targetType, string sessionId)
        {
            _connection = chromiumConnection;
            _targetType = targetType;
            _sessionId  = sessionId;

            chromiumConnection.MessageReceived += OnMessageReceived;
        }
Exemplo n.º 2
0
        internal void OnClosed(string reason)
        {
            foreach (var callback in _callbacks)
            {
                callback.Value.TaskWrapper.TrySetException(new TargetClosedException($"Protocol error ({callback.Value.Method}): Target closed. {reason}"));
            }

            _callbacks.Clear();
            Connection = null;
            Disconnected?.Invoke(this, EventArgs.Empty);
        }
Exemplo n.º 3
0
        private async Task OnAttachedToTargetAsync(TargetAttachedToTargetChromiumEvent e)
        {
            if (e.TargetInfo.Type != "worker")
            {
                return;
            }

            string url     = e.TargetInfo.Url;
            var    session = ChromiumConnection.FromSession(Client).GetSession(e.SessionId);
            var    worker  = new Worker(url);

            Page.AddWorker(e.SessionId, worker);

            void HandleRuntimeExecutionContextCreated(object sender, IChromiumEvent e)
            {
                switch (e)
                {
                case RuntimeExecutionContextCreatedChromiumEvent runtimeExecutionContextCreated:

                    worker.CreateExecutionContext(new ChromiumExecutionContext(session, runtimeExecutionContextCreated.Context));
                    session.MessageReceived -= HandleRuntimeExecutionContextCreated;
                    break;

                case RuntimeConsoleAPICalledChromiumEvent runtimeConsoleAPICalled:
                    var args = runtimeConsoleAPICalled.Args.Select(o =>
                                                                   worker.ExistingExecutionContext.CreateHandle(o));
                    Page.AddConsoleMessage(
                        runtimeConsoleAPICalled.Type.ToEnum <ConsoleType>(),
                        args.ToArray(),
                        ToConsoleMessageLocation(runtimeConsoleAPICalled.StackTrace));
                    break;

                case RuntimeExceptionThrownChromiumEvent runtimeExceptionThrown:
                    Page.OnPageError(ExceptionToError(runtimeExceptionThrown.ExceptionDetails));
                    break;
                }
            }

            session.MessageReceived += HandleRuntimeExecutionContextCreated;

            try
            {
                await Task.WhenAll(
                    session.SendAsync(new RuntimeEnableRequest()),
                    session.SendAsync(new NetworkEnableRequest())).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex);
            }
        }
Exemplo n.º 4
0
        internal static async Task <IBrowser> ConnectAsync(IBrowserApp app, ConnectOptions options)
        {
            var transport = await BrowserHelper.CreateTransportAsync(options).ConfigureAwait(false);

            var connection = new ChromiumConnection(transport);
            var response   = await connection.RootSession.SendAsync(new TargetGetBrowserContextsRequest()).ConfigureAwait(false);

            var browser = new ChromiumBrowser(app, connection, response.BrowserContextIds);
            await connection.RootSession.SendAsync(new TargetSetDiscoverTargetsRequest { Discover = true }).ConfigureAwait(false);

            await browser.WaitForTargetAsync(t => t.Type == TargetType.Page).ConfigureAwait(false);

            return(browser);
        }
        private ChromiumBrowser(ChromiumConnection connection, string[] browserContextIds)
        {
            _connection = connection;
            _client     = connection.RootSession;

            DefaultContext = new BrowserContext(new ChromiumBrowserContext(connection.RootSession, this));

            _contexts = browserContextIds.ToDictionary(
                contextId => contextId,
                contextId => (IBrowserContext) new BrowserContext(new ChromiumBrowserContext(connection.RootSession, this, contextId, null)));

            _connection.Disconnected += Connection_OnDisconnected;
            _client.MessageReceived  += Client_MessageReceived;
        }
Exemplo n.º 6
0
        private ChromiumBrowser(IBrowserApp app, ChromiumConnection connection, string[] browserContextIds)
        {
            _app        = app;
            _connection = connection;
            _session    = connection.RootSession;

            DefaultContext = new BrowserContext(new ChromiumBrowserContext(connection.RootSession, this));

            _contexts = browserContextIds.ToDictionary(
                contextId => contextId,
                contextId => (IBrowserContext) new BrowserContext(new ChromiumBrowserContext(connection.RootSession, this, contextId, null)));

            _session.MessageReceived += Session_MessageReceived;
            _connection.Disconnected += (sender, e) => Disconnected?.Invoke(this, EventArgs.Empty);
        }
Exemplo n.º 7
0
 public ChromiumSession(ChromiumConnection chromiumConnection, TargetType targetType, string sessionId)
 {
     Connection  = chromiumConnection;
     _targetType = targetType;
     _sessionId  = sessionId;
 }