Esempio n. 1
0
        private void ProcessIncomingMessage(ConnectionResponse obj)
        {
            if (!obj.Params.HasValue)
            {
                GetSession(obj.SessionId ?? string.Empty)?.OnMessage(obj);
                return;
            }

            var param = ChromiumProtocolTypes.ParseEvent(obj.Method, obj.Params.Value.GetRawText());

            if (obj.Id == BrowserCloseMessageId)
            {
                return;
            }

            if (param is TargetAttachedToTargetChromiumEvent targetAttachedToTarget)
            {
                string          sessionId = targetAttachedToTarget.SessionId;
                ChromiumSession session   = new ChromiumSession(this, targetAttachedToTarget.TargetInfo.GetTargetType(), sessionId);
                _asyncSessions.AddItem(sessionId, session);
            }
            else if (param is TargetDetachedFromTargetChromiumEvent targetDetachedFromTarget)
            {
                string sessionId = targetDetachedFromTarget.SessionId;
                if (_sessions.TryRemove(sessionId, out var session) && !session.IsClosed)
                {
                    session.OnClosed(targetDetachedFromTarget.InternalName);
                }
            }

            GetSession(obj.SessionId ?? string.Empty).OnMessageReceived(param);
        }
        private void ProcessIncomingMessage(ConnectionResponse obj)
        {
            if (obj.Id == BrowserCloseMessageId)
            {
                return;
            }

            IFirefoxEvent param = null;

            if (obj.Params?.ValueKind == JsonValueKind.Object)
            {
                param = FirefoxProtocolTypes.ParseEvent(obj.Method, obj.Params.Value.GetRawText());
                if (param is TargetAttachedToTargetFirefoxEvent targetAttachedToTarget)
                {
                    string sessionId = targetAttachedToTarget.SessionId;
                    var    session   = new FirefoxSession(this, targetAttachedToTarget.TargetInfo.Type.ToString(), sessionId, (id, request) =>
                                                          RawSendAsync(new ConnectionRequest
                    {
                        Id        = id,
                        Method    = request.Command,
                        Params    = request,
                        SessionId = sessionId,
                    }));
                    _asyncSessions.AddItem(sessionId, session);
                }
                else if (param is TargetDetachedFromTargetFirefoxEvent targetDetachedFromTarget)
                {
                    string sessionId = targetDetachedFromTarget.SessionId;
                    if (_sessions.TryRemove(sessionId, out var session))
                    {
                        session.OnClosed(targetDetachedFromTarget.InternalName);
                    }
                }
            }

            if (obj.SessionId != null)
            {
                GetSession(obj.SessionId)?.OnMessage(obj);
            }
            else if (obj.Id.HasValue && _callbacks.TryRemove(obj.Id.Value, out var callback))
            {
                if (obj.Error != null)
                {
                    callback.TaskWrapper.TrySetException(new MessageException(callback, obj.Error));
                }
                else
                {
                    callback.TaskWrapper.TrySetResult(FirefoxProtocolTypes.ParseResponse(callback.Method, obj.Result?.GetRawText()));
                }
            }
            else if (param != null)
            {
                MessageReceived?.Invoke(this, param);
            }
        }
Esempio n. 3
0
        private void ProcessIncomingMessage(ConnectionResponse obj)
        {
            var method = obj.Method;
            var param  = obj.Params?.ToObject <ConnectionResponseParams>();

            if (method == "Target.attachedToTarget")
            {
                var sessionId = param.SessionId;
                var session   = new CDPSession(this, param.TargetInfo.Type, sessionId);
                _asyncSessions.AddItem(sessionId, session);

                SessionAttached?.Invoke(this, new SessionAttachedEventArgs {
                    Session = session
                });

                if (obj.SessionId != null && _sessions.TryGetValue(obj.SessionId, out var parentSession))
                {
                    parentSession.OnSessionAttached(session);
                }
            }
            else if (method == "Target.detachedFromTarget")
            {
                var sessionId = param.SessionId;
                if (_sessions.TryRemove(sessionId, out var session) && !session.IsClosed)
                {
                    session.Close("Target.detachedFromTarget");
                }
            }

            if (!string.IsNullOrEmpty(obj.SessionId))
            {
                var session = GetSession(obj.SessionId);
                session?.OnMessage(obj);
            }
            else if (obj.Id.HasValue)
            {
                // If we get the object we are waiting for we return if
                // if not we add this to the list, sooner or later some one will come for it
                if (_callbacks.TryRemove(obj.Id.Value, out var callback))
                {
                    MessageQueue.Enqueue(callback, obj);
                }
            }
            else
            {
                MessageReceived?.Invoke(this, new MessageEventArgs
                {
                    MessageID   = method,
                    MessageData = obj.Params
                });
            }
        }
        private async Task OnFrameNavigatedAsync(FramePayload framePayload)
        {
            var isMainFrame = string.IsNullOrEmpty(framePayload.ParentId);
            var frame       = isMainFrame ? MainFrame : await GetFrameAsync(framePayload.Id).ConfigureAwait(false);

            Contract.Assert(isMainFrame || frame != null, "We either navigate top level or have old version of the navigated frame");

            // Detach all child frames first.
            if (frame != null)
            {
                while (frame.ChildFrames.Count > 0)
                {
                    RemoveFramesRecursively(frame.ChildFrames[0]);
                }
            }

            // Update or create main frame.
            if (isMainFrame)
            {
                if (frame != null)
                {
                    // Update frame id to retain frame identity on cross-process navigation.
                    if (frame.Id != null)
                    {
                        _frames.TryRemove(frame.Id, out _);
                    }
                    frame.Id = framePayload.Id;
                }
                else
                {
                    // Initial main frame navigation.
                    frame = new Frame(this, null, framePayload.Id);
                }
                _asyncFrames.AddItem(framePayload.Id, frame);
                MainFrame = frame;
            }

            // Update frame payload.
            frame.Navigated(framePayload);

            FrameNavigated?.Invoke(this, new FrameEventArgs(frame));
        }