Exemplo n.º 1
0
 internal static CdpResponse GetSessionMessage(this CdpResponse response)
 {
     if (response.HasToken("params.message"))
     {
         var tkval = response.GetStrValue("params.message");
         if (!string.IsNullOrEmpty(tkval))
         {
             return(new CdpResponse(tkval));
         }
     }
     return(null);
 }
Exemplo n.º 2
0
        internal static ActionContext GetActionContext(this CdpResponse response)
        {
            var name  = response.GetStrValue("params.name");
            var plstr = response.GetStrValue("params.payload");

            if (!string.IsNullOrEmpty(plstr))
            {
                var jo = JObject.Parse(plstr);
                if (jo.HasValues)
                {
                    var id = jo["id"].ToObject <int>();
                    return(new ActionContext(name, id, jo["args"]));
                }
            }
            return(null);
        }
Exemplo n.º 3
0
        private void HandleCdpResponse(CdpResponse response)
        {
            if (response.IsMsgTargetPageCreated())
            {
                _cdpTargetId = response.GetStrValue("params.targetInfo.targetId");
                _logger.LogInformation($"Target Id : {_cdpTargetId}");
                Send(CdpApi.GetOpenSessionMsg(_cdpTargetId)).Wait();
                return;
            }

            if (response.IsMsgSessionCreated())
            {
                _cdpSessionId = response.GetStrValue("result.sessionId");
                _logger.LogInformation($"Session Id : {_cdpSessionId}");
                foreach (var setting in CdpApi.Configuration)
                {
                    Publish(setting, new { });
                }

                Publish(CdpApi.Methods.TargetAutoAttach, CdpApi.TargetAutoAttachConfig);
                Publish(CdpApi.Methods.BrowserGetWinTarget, new { targetId = _cdpTargetId }, (rq, rs) =>
                {
                    rs.WithVal <int>("result.windowId", x => { _cdpWindowId = x; });
                    _logger.LogInformation($"Window Id : {_cdpWindowId}");
                    HandleActionResult(_routeService.Execute(NBlinkContext.DefaultRoute()));
                });
                return;
            }
            if (string.IsNullOrEmpty(_cdpSessionId))
            {
                return;
            }

            if (response.IsMsgFromTargetSession(_cdpSessionId))
            {
                HandleTargetSessionMsg(response);
                return;
            }

            if (response.IsMsgTargetDestroyed(_cdpTargetId))
            {
                _logger.LogError($"{_cdpTargetId} destroyed s******g down application...");
                _dispatcher.Publish(new ExitCmd());
            }
        }
Exemplo n.º 4
0
        private void HandleTargetSessionMsg(CdpResponse response)
        {
            var msg = response.GetSessionMessage();

            if (msg == null)
            {
                return;
            }

            if (msg.IsMsgRuntimeLog())
            {
                _logger.LogDebug($"Chrome Msg:  {msg}");
                return;
            }

            if (msg.IsMsgRuntimeBindCalled())
            {
                var actx = msg.GetActionContext();
                if (actx == null)
                {
                    return;
                }

                var rs = _routeService.Execute(actx);
                HandleActionResult(rs);
                return;
            }
            if (!msg.Id.HasValue)
            {
                return;
            }

            if (_cbMap.TryGetValue(msg.Id.Value, out SessionCallback cbk))
            {
                _cbMap.TryRemove(msg.Id.Value, out _);
                cbk.Callback(cbk.Request, msg);
            }
        }
Exemplo n.º 5
0
 internal static bool HasKeyValue(this CdpResponse response, string key, string value)
 {
     return(string.Compare(response.GetStrValue(key), value, true) == 0);
 }
Exemplo n.º 6
0
 internal static bool IsMsgTargetDestroyed(this CdpResponse response, string targetId)
 {
     return(response.IsMethod(CdpApi.Methods.TargetDestroyed) &&
            response.HasKeyValue("params.targetId", targetId));
 }
Exemplo n.º 7
0
 internal static bool IsMsgRuntimeBindCalled(this CdpResponse response)
 {
     return(response.IsMethod(CdpApi.Methods.RuntimeBindCalled));
 }
Exemplo n.º 8
0
 internal static bool IsMsgRuntimeLog(this CdpResponse response)
 {
     return(response.IsMethod(CdpApi.Methods.RuntimeConsoleApi) ||
            response.IsMethod(CdpApi.Methods.RuntimeException));
 }
Exemplo n.º 9
0
 internal static bool IsMsgFromTargetSession(this CdpResponse response, string sessionId)
 {
     return(response.IsMethod(CdpApi.Methods.TargetRecvMsg) &&
            response.HasKeyValue("params.sessionId", sessionId));
 }
Exemplo n.º 10
0
 internal static bool IsMsgSessionCreated(this CdpResponse response)
 {
     return(response.Id == 1 && response.HasToken("result.sessionId"));
 }
Exemplo n.º 11
0
 internal static bool IsMsgTargetPageCreated(this CdpResponse response)
 {
     return(response.IsMethod(CdpApi.Methods.TargetCreated) &&
            response.HasKeyValue("params.targetInfo.type", "page"));
 }
Exemplo n.º 12
0
 private void HandleCallback(CdpSession request, CdpResponse response)
 {
 }