Ejemplo n.º 1
0
 internal OnQueryEventArgs(CefBrowser _browser, CefFrame _frame, long _queryId, string _request, bool _persistent, CefMessageRouterBrowserSide.Callback _callback)
 {
     browser    = _browser;
     frame      = _frame;
     queryId    = _queryId;
     request    = _request;
     persistent = _persistent;
     callback   = _callback;
     Handled    = false; //default return value
 }
        /// <summary>
        /// The on query.
        /// </summary>
        /// <param name="browser">
        /// The browser.
        /// </param>
        /// <param name="frame">
        /// The frame.
        /// </param>
        /// <param name="queryId">
        /// The query id.
        /// </param>
        /// <param name="request">
        /// The request.
        /// </param>
        /// <param name="persistent">
        /// The persistent.
        /// </param>
        /// <param name="callback">
        /// The callback.
        /// </param>
        /// <returns>
        /// The <see cref="bool"/>.
        /// </returns>
        public override bool OnQuery(CefBrowser browser, CefFrame frame, long queryId, string request, bool persistent, CefMessageRouterBrowserSide.Callback callback)
        {
            JsonData requestData = JsonMapper.ToObject(request);
            string   method      = requestData["method"].ToString();

            method = string.IsNullOrWhiteSpace(method) ? string.Empty : method;

            if (method.Equals("Get", StringComparison.InvariantCultureIgnoreCase) ||
                method.Equals("Post", StringComparison.InvariantCultureIgnoreCase))
            {
                new Task(() =>
                {
                    string path       = requestData["url"].ToString();
                    object parameters = requestData["parameters"];
                    object postData   = requestData["postData"];

                    var routePath    = new RoutePath(method, path);
                    var response     = RequestTaskRunner.Run(routePath, parameters, postData);
                    var jsonResponse = response.EnsureJson();

                    callback.Success(jsonResponse);
                }).Start();

                return(true);
            }

            callback.Failure(100, "Request is not valid.");
            return(false);
        }
Ejemplo n.º 3
0
        public override bool OnQuery(CefBrowser browser, CefFrame frame, long queryId, string request, bool persistent, CefMessageRouterBrowserSide.Callback callback)
        {
            var options = new JsonSerializerOptions();

            options.ReadCommentHandling = JsonCommentHandling.Skip;
            options.AllowTrailingCommas = true;
            var requestData = JsonSerializer.Deserialize <request>(request, options);

            var method = requestData.method ?? string.Empty;

            if (RoutePath.ValidMethod(method))
            {
                var id        = requestData.id ?? string.Empty;
                var path      = requestData.url ?? string.Empty;
                var routePath = new RoutePath(method, path);

                bool isRequestAsync = ServiceRouteProvider.IsActionRouteAsync(_container, routePath);

                if (isRequestAsync)
                {
                    Task.Run(async() =>
                    {
                        var parameters = requestData.parameters;
                        var postData   = requestData.postData;

                        var response     = await _requestTaskRunner.RunAsync(id, routePath, parameters, postData, request);
                        var jsonResponse = response.ToJson();

                        callback.Success(jsonResponse);
                    });
                }
                else
                {
                    Task.Run(() =>
                    {
                        var parameters = requestData.parameters;
                        var postData   = requestData.postData;

                        var response     = _requestTaskRunner.Run(id, routePath, parameters, postData, request);
                        var jsonResponse = response.ToJson();

                        callback.Success(jsonResponse);
                    });
                }

                return(true);
            }

            callback.Failure(100, "Request is not valid.");
            return(false);
        }
Ejemplo n.º 4
0
 public override bool OnQuery(CefBrowser browser, CefFrame frame, long queryId, string request, bool persistent,
                              CefMessageRouterBrowserSide.Callback callback)
 {
     return(base.OnQuery(browser, frame, queryId, request, persistent, callback));
 }
Ejemplo n.º 5
0
            public override bool OnQuery(CefBrowser browser, CefFrame frame, long queryId, string request, bool persistent, CefMessageRouterBrowserSide.Callback callback)
            {
                if (request == "wait5")
                {
                    new Thread(() =>
                    {
                        Thread.Sleep(5000);
                        callback.Success("success! responded after 5 sec timeout."); // TODO: at this place crash can occurs, if application closed
                    }).Start();
                    return(true);
                }

                if (request == "wait5f")
                {
                    new Thread(() =>
                    {
                        Thread.Sleep(5000);
                        callback.Failure(12345, "success! responded after 5 sec timeout. responded as failure.");
                    }).Start();
                    return(true);
                }

                if (request == "wait30")
                {
                    new Thread(() =>
                    {
                        Thread.Sleep(30000);
                        callback.Success("success! responded after 30 sec timeout.");
                    }).Start();
                    return(true);
                }

                if (request == "noanswer")
                {
                    return(true);
                }

                var chars = request.ToCharArray();

                Array.Reverse(chars);
                var response = new string(chars);

                callback.Success(response);
                return(true);
            }
Ejemplo n.º 6
0
        /// <summary>
        /// The on query.
        /// </summary>
        /// <param name="browser">
        /// The browser.
        /// </param>
        /// <param name="frame">
        /// The frame.
        /// </param>
        /// <param name="queryId">
        /// The query id.
        /// </param>
        /// <param name="request">
        /// The request.
        /// </param>
        /// <param name="persistent">
        /// The persistent.
        /// </param>
        /// <param name="callback">
        /// The callback.
        /// </param>
        /// <returns>
        /// The <see cref="bool"/>.
        /// </returns>
        public override bool OnQuery(CefBrowser browser, CefFrame frame, long queryId, string request, bool persistent, CefMessageRouterBrowserSide.Callback callback)
        {
            var requestData = JsonMapper.ToObject(request);
            var method      = requestData.Keys.Contains("method") ? requestData["method"].ToString() : string.Empty;

            method = string.IsNullOrWhiteSpace(method) ? string.Empty : method;

            if (RoutePath.ValidMethod(method))
            {
                new Task(() =>
                {
                    var id         = requestData.Keys.Contains("id") ? requestData["id"].ToString() : string.Empty;
                    var path       = requestData.Keys.Contains("url") ? requestData["url"].ToString() : string.Empty;
                    var parameters = requestData.Keys.Contains("parameters") ? requestData["parameters"] : null;
                    var postData   = requestData.Keys.Contains("postData") ? requestData["postData"] : null;

                    var routePath       = new RoutePath(method, path);
                    var response        = RequestTaskRunner.Run(id, routePath, parameters, postData, request);
                    string jsonResponse = response.EnsureJson();

                    callback.Success(jsonResponse);
                }).Start();

                return(true);
            }

            callback.Failure(100, "Request is not valid.");
            return(false);
        }
Ejemplo n.º 7
0
    public override bool OnQuery(CefBrowser browser, CefFrame frame, long queryId, string request, bool persistent, CefMessageRouterBrowserSide.Callback callback)
    {
        request?requestData = null;

        try
        {
            requestData = JsonSerializer.Deserialize <request>(request, _dataTransferOptions.SerializerOptions as JsonSerializerOptions);

            if (requestData is not null)
            {
                var id   = requestData.id ?? string.Empty;
                var path = requestData.url ?? string.Empty;

                bool isRequestAsync = _routeProvider.IsRouteAsync(path);

                if (isRequestAsync)
                {
                    Task.Run(async() =>
                    {
                        var parameters = requestData.parameters;
                        var postData   = requestData.postData;

                        var response     = await _requestHandler.ExecuteAsync(id, path, parameters, postData, request);
                        var jsonResponse = _dataTransferOptions.ConvertObjectToJson(response);

                        callback.Success(jsonResponse);
                    });
                }
                else
                {
                    Task.Run(() =>
                    {
                        var parameters = requestData.parameters;
                        var postData   = requestData.postData;

                        var response     = _requestHandler.Execute(id, path, parameters, postData, request);
                        var jsonResponse = _dataTransferOptions.ConvertObjectToJson(response);

                        callback.Success(jsonResponse);
                    });
                }

                return(true);
            }
        }
        catch (Exception exception)
        {
            var chromelyRequest = requestData?.ToRequest();
            if (chromelyRequest is null)
            {
                chromelyRequest = new ChromelyRequest();
            }

            var response     = _chromelyErrorHandler.HandleError(chromelyRequest, exception);
            var jsonResponse = _dataTransferOptions.ConvertObjectToJson(response);
            callback.Failure(100, jsonResponse);
            return(false);
        }

        callback.Failure(100, "Request is not valid.");
        return(false);
    }
Ejemplo n.º 8
0
        public override bool OnQuery(CefBrowser browser, CefFrame frame, long queryId, string request, bool persistent, CefMessageRouterBrowserSide.Callback callback)
        {
            Debug.WriteLine(DBGPREFIX + "OnQuery called, [" + queryId + " " + (persistent ? "" : "not" + " persistent]: ") + request);
            var handler = OnQueryEvent;

            if (handler != null)
            {
                Debug.WriteLine(DBGPREFIX + "OnQuery Delegate");
                var e = new OnQueryEventArgs(browser, frame, queryId, request, persistent, callback);
                handler(this, e);
                return(e.Handled);
            }
            return(false);
        }
 public void Callback(string result)
 {
     _lastCallback?.Success(result);
     _lastCallback = null;
 }
 public override bool OnQuery(CefBrowser browser, CefFrame frame, long queryId, string request, bool persistent, CefMessageRouterBrowserSide.Callback callback)
 {
     OnBrowserQuery?.Invoke(request);
     _lastCallback = callback;
     return(true);
 }