Beispiel #1
0
        private void JavascriptFunctionLoadInternalScript(CefV8Value obj, CefV8Value[] arguments, out CefV8Value returnValue)
        {
            if (arguments == null)
            {
                throw new ArgumentNullException("arguments");
            }

            if (arguments.Length < 1 || !arguments[0].IsString)
            {
                throw new ArgumentException("Invalid arguments.", "arguments");
            }

            returnValue = null;

            var scriptName = "Scripts/" + arguments[0].GetStringValue();

            if (!ResourceUtility.ResourceExists(scriptName))
            {
                throw new InvalidOperationException(string.Format("Script '{0}' not found.", scriptName));
            }

            CefV8Value     evalReturnValue = null;
            CefV8Exception evalException   = null;

            string script  = ResourceUtility.GetResourceAsString(scriptName);
            var    context = CefV8Context.GetCurrentContext();

            if (!context.TryEval(script, out evalReturnValue, out evalException))
            {
                throw new InvalidOperationException(string.Format("Javascript exception: {0}.", JsonConvert.SerializeObject(evalException)));
            }
        }
Beispiel #2
0
        /// <summary>
        /// Gets the response headers.
        /// </summary>
        /// <param name="response"></param>
        /// <param name="responseLength"></param>
        /// <param name="redirectUrl"></param>
        protected override void GetResponseHeaders(CefResponse response, out long responseLength, out string redirectUrl)
        {
            if (response == null)
            {
                throw new ArgumentNullException("response");
            }

            redirectUrl = null;

            if (Exception != null)
            {
                Data = Encoding.UTF8.GetBytes(string.Format(ResourceUtility.GetResourceAsString("Views/ContentRequestException.html"),
                                                            Url,
                                                            Exception.ToString().Replace(Environment.NewLine, "<br>")));

                responseLength      = Data.Length;
                response.Status     = 500;
                response.StatusText = "Internal Server Error";
                response.MimeType   = "text/html";
            }
            else if (Data != null)
            {
                responseLength      = Data.Length;
                response.Status     = 200;
                response.StatusText = "OK";

                // mime type
                response.MimeType = "application/octet-stream";
                var extension = Path.GetExtension(Url);

                if (!string.IsNullOrWhiteSpace(extension) && Application.Current.MimeTypes.ContainsKey(extension))
                {
                    response.MimeType = Application.Current.MimeTypes[extension];
                }
            }
            else
            {
                Data = Encoding.UTF8.GetBytes(string.Format(ResourceUtility.GetResourceAsString("Views/ContentNotFound.html"), Url));

                responseLength      = Data.Length;
                response.Status     = 404;
                response.StatusText = "Not Found";
                response.MimeType   = "text/html";
            }
        }
Beispiel #3
0
        /// <summary>
        /// Gets the HTML UI script.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="mapping">if set to <c>true</c> include mapping.</param>
        /// <returns></returns>
        private string GetHtmlUiScript(string name, bool mapping)
        {
            var scriptName = string.Format("Scripts/htmlUi.{0}.js", name);
            var script     = ResourceUtility.GetResourceAsString(scriptName);

            var constants = new string[]
            {
                CreateStringConstant("_nativeRequestUrl", Application.NativeRequestUrl)
            };

            var processedExtensionResource = script
                                             .Replace("// !native ", "native ")
                                             .Replace("// !inject-constants", string.Join(Environment.NewLine, constants));

            processedExtensionResource = Regex.Replace(processedExtensionResource, @"^/// <reference path=.*$", string.Empty, RegexOptions.Multiline);

            if (mapping)
            {
                var typescriptName = string.Format("TypeScript/htmlUi.{0}.ts", name);
                var typescript     = ResourceUtility.GetResourceAsString(typescriptName);

                processedExtensionResource = Regex.Replace(processedExtensionResource, @"^//# sourceMappingURL=.*$", (match) =>
                {
                    var sourceMappingURL = match.Groups[0].Value.Substring("//# sourceMappingURL=".Length);

                    var map            = JsonConvert.DeserializeObject <SourceMap>(ResourceUtility.GetResourceAsString(string.Format("Scripts/{0}", sourceMappingURL)));
                    map.SourcesContent = new List <string> {
                        typescript
                    };
                    map.File       = null;
                    map.SourceRoot = "/Scripts";

                    return(string.Format("//# sourceMappingURL=data:application/octet-stream;base64,{0}",
                                         Convert.ToBase64String(Encoding.UTF8.GetBytes(JsonUtility.SerializeToJson(map)))));
                }, RegexOptions.Multiline);
            }
            else
            {
                processedExtensionResource = Regex.Replace(processedExtensionResource, @"^//# sourceMappingURL=.*$", string.Empty, RegexOptions.Multiline);
            }

            return(processedExtensionResource);
        }
Beispiel #4
0
 /// <summary>
 /// Gets the default MIME types.
 /// </summary>
 /// <returns></returns>
 private Dictionary <string, string> GetDefaultMimeTypes()
 {
     return(JsonConvert.DeserializeObject <Dictionary <string, string> >(ResourceUtility.GetResourceAsString("MimeTypes.json")));
 }
Beispiel #5
0
        /// <summary>
        /// Sends the request to the browser process.
        /// </summary>
        /// <param name="name"></param>
        /// <param name="obj"></param>
        /// <param name="arguments"></param>
        /// <param name="returnValue"></param>
        /// <param name="exception"></param>
        /// <returns></returns>
        protected override bool Execute(string name, CefV8Value obj, CefV8Value[] arguments, out CefV8Value returnValue, out string exception)
        {
            if (arguments == null)
            {
                throw new ArgumentNullException("arguments");
            }

            returnValue = null;
            exception   = null;

            // native
            if (name == "native")
            {
                var functionName     = arguments[0].GetStringValue();
                var jsonData         = arguments[1].GetStringValue();
                var callbackFunction = arguments.Length > 2 && arguments[2].IsFunction ? arguments[2] : null;

                var callbackId = AddCallback(callbackFunction, CefV8Context.GetCurrentContext());

                MessageUtility.SendMessage(CefBrowser, "native", callbackId, new CallNative {
                    Name = functionName, Json = jsonData
                });

                return(true);
            }

            // register function
            else if (name == "registerFunction")
            {
                var functionName = arguments[0].GetStringValue();
                var function     = arguments[1];

                if (!Functions.ContainsKey(functionName))
                {
                    Functions.Add(functionName, new JavascriptFunction(function, CefV8Context.GetCurrentContext()));
                }
                else
                {
                    exception = string.Format("Function '{0}' is already registered.", functionName);
                }

                return(true);
            }

            // load internal script
            else if (name == "loadInternalScript")
            {
                if (arguments.Length > 0 && arguments[0].IsString)
                {
                    var scriptName = "Scripts/" + arguments[0].GetStringValue();

                    if (ResourceUtility.ResourceExists(scriptName))
                    {
                        CefV8Value     evalReturnValue = null;
                        CefV8Exception evalException   = null;

                        string script  = ResourceUtility.GetResourceAsString(scriptName);
                        var    context = CefV8Context.GetCurrentContext();

                        if (!context.TryEval(script, out evalReturnValue, out evalException))
                        {
                            exception = string.Format("Exception: {0}.", JsonConvert.SerializeObject(exception));
                        }
                    }
                    else
                    {
                        exception = "Script not found.";
                    }
                }
                else
                {
                    exception = "Missing script name.";
                }

                return(true);
            }

            return(false);
        }