public FirefoxNetworkManager(FirefoxSession session, Page page)
        {
            _session = session;
            _page    = page;

            _session.MessageReceived += OnMessageReceived;
        }
        public FirefoxInterceptableRequest(FirefoxSession session, Frame frame, List <Request> redirectChain, NetworkRequestWillBeSentFirefoxEvent payload)
        {
            Id       = payload.RequestId;
            _session = session;

            var headers = payload.Headers.ToDictionary(header => header.Name.ToLower(), header => header.Value, StringComparer.InvariantCultureIgnoreCase);

            Request = new Request(payload.IsIntercepted == true ? this : null, frame, redirectChain, payload.NavigationId, payload.Url, payload.GetResourceType(), new HttpMethod(payload.Method), payload.PostData, headers);
        }
        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);
            }
        }
Example #4
0
        public FirefoxPage(FirefoxSession session, IBrowserContext context, Func <Task <Page> > openerResolver)
        {
            _session        = session;
            _context        = context;
            _openerResolver = openerResolver;

            RawKeyboard     = new FirefoxRawKeyboard(session);
            RawMouse        = new FirefoxRawMouse(session);
            Page            = new Page(this, _context);
            _networkManager = new FirefoxNetworkManager(session, Page);

            session.MessageReceived += OnMessageReceived;
            Page.FrameDetached      += RemoveContextsForFrame;
        }
        private async Task <AccessibilityTree> GetAccessibilityTreeAsync(FirefoxSession session, IElementHandle needle)
        {
            string objectId = (needle as ElementHandle)?.RemoteObject.ObjectId;
            var    result   = await session.SendAsync(new AccessibilityGetFullAXTreeRequest
            {
                ObjectId = objectId,
            }).ConfigureAwait(false);

            var axNode = new FirefoxAXNode(result.Tree);

            return(new AccessibilityTree
            {
                Tree = axNode,
                Needle = needle != null?axNode.FindNeedle() : null,
            });
        }
Example #6
0
 public FirefoxExecutionContext(FirefoxSession workerSession, string executionContextId)
 {
     _session           = workerSession;
     ExecutionContextId = executionContextId;
 }
Example #7
0
 public WorkerSession(string frameId, FirefoxSession session) => (FrameId, Session) = (frameId, session);
Example #8
0
        private void OnWorkerCreated(PageWorkerCreatedFirefoxEvent e)
        {
            string         workerId          = e.WorkerId;
            var            worker            = new Worker(e.Url);
            FirefoxSession tempWorkerSession = null;
            var            workerSession     = new FirefoxSession(_session.Connection, "worker", workerId, async(id, request) =>
            {
                try
                {
                    await _session.SendAsync(new PageSendMessageToWorkerRequest
                    {
                        FrameId  = e.FrameId,
                        WorkerId = workerId,
                        Message  = new ConnectionRequest
                        {
                            Id     = id,
                            Method = request.Command,
                            Params = request,
                        }.ToJson(FirefoxJsonHelper.DefaultJsonSerializerOptions),
                    }).ConfigureAwait(false);
                }
                catch (Exception e)
                {
                    tempWorkerSession.OnMessage(new ConnectionResponse
                    {
                        Id     = id,
                        Method = string.Empty,
                        Error  = new ConnectionError
                        {
                            Message = e.Message,
                        },
                    });
                }
            });

            tempWorkerSession = workerSession;

            _workers[workerId] = new WorkerSession(e.FrameId, workerSession);
            Page.AddWorker(workerId, worker);
            void HandleRuntimeExecutionContextCreated(object sender, IFirefoxEvent e)
            {
                if (e is RuntimeExecutionContextCreatedFirefoxEvent runtimeExecutionContextCreated)
                {
                    worker.CreateExecutionContext(new FirefoxExecutionContext(workerSession, runtimeExecutionContextCreated.ExecutionContextId));
                    workerSession.MessageReceived -= HandleRuntimeExecutionContextCreated;
                }
            }

            workerSession.MessageReceived += HandleRuntimeExecutionContextCreated;
            workerSession.MessageReceived += (sender, e) =>
            {
                if (e is RuntimeConsoleFirefoxEvent runtimeConsole)
                {
                    var context  = worker.ExistingExecutionContext;
                    var type     = runtimeConsole.GetConsoleType();
                    var location = runtimeConsole.ToConsoleMessageLocation();

                    Page.AddConsoleMessage(type, Array.ConvertAll(runtimeConsole.Args, arg => context.CreateHandle(arg)), location);
                }
            };
        }