protected override bool OnBeforeBrowse(CefBrowser browser, CefFrame frame, CefRequest request, CefHandlerNavType navType, bool isRedirect) { #if DIAGNOSTICS Cef.Logger.Trace(LogTarget.CefRequestHandler, "OnBeforeBrowse: Method=[{0}] Url=[{1}] NavType=[{2}] IsRedirect=[{3}]", request.GetMethod(), request.GetURL(), navType, isRedirect); #endif var cancel = this.context.OnNavigating(frame, request, navType, isRedirect); if (cancel) return true; if (frame.IsMain) { // browser navigating to a new page this.context.ClearFrames(); this.context.AttachMainFrame(frame); this.context.ClearReadyState(); this.context.SetFrameReadyState(null, FrameReadyState.Navigating); } else if ((this.context.ReadyOptions & CefReadyOptions.Frames) != 0) { this.context.SetFrameReadyState(frame.GetName(), FrameReadyState.Navigating); } return false; }
protected override bool ProcessRequest(CefRequest request, CefSchemeHandlerCallback callback) { var urlString = request.GetURL(); string errorMessage = null; int errorStatus = 0; string errorStatusText = null; try { var uri = new Uri(urlString); var path = uri.Host + uri.AbsolutePath; // ignore host var asm = typeof(ClientSchemeHandler).Assembly; var resPrefix = "CefGlue.Client.Resources."; // convert path to resource name var parts = path.Split('/'); for (var i = 0; i < parts.Length - 1; i++) { var filename = Path.GetFileNameWithoutExtension(parts[i]); var extension = Path.GetExtension(parts[i]); parts[i] = filename.Replace(".", "._").Replace('-', '_') + extension; } var resName = resPrefix + string.Join(".", parts); this.stream = asm.GetManifestResourceStream(resName); if (this.stream != null) { // found this.responseLength = -1; this.status = 200; this.statusText = "OK"; this.mimeType = GetMimeTypeFromUriSuffix(path); callback.HeadersAvailable(); return(true); } } catch (Exception ex) { errorStatus = 500; errorStatusText = "Internal Error"; errorMessage = "<!doctype html><html><body><h1>Internal Error!</h1><pre>" + ex.ToString() + "</pre></body></html>"; } // not found or error while processing request errorMessage = errorMessage ?? "<!doctype html><html><body><h1>Not Found!</h1><p>The requested url [" + urlString + "] not found!</p></body></html>"; var bytes = Encoding.UTF8.GetBytes(errorMessage); this.stream = new MemoryStream(bytes, false); this.responseLength = -1; this.status = errorStatus != 0 ? errorStatus : 404; this.statusText = errorStatusText ?? "Not Found"; this.mimeType = "text/html"; callback.HeadersAvailable(); return(true); }
// TODO: use OnBeforeResourceLoad to OnResourceResponse to detect resource loading, and waiting when all resources will be loaded protected override bool OnBeforeResourceLoad(CefBrowser browser, CefRequest request, out string redirectUrl, out CefStreamReader resourceStream, CefResponse response, int loadFlags) { var prevColor = Console.ForegroundColor; Console.ForegroundColor = ConsoleColor.DarkMagenta; Console.WriteLine("OnBeforeResourceLoad: {0}", request.GetURL()); Console.ForegroundColor = prevColor; redirectUrl = null; resourceStream = null; return false; }
// TODO: use OnBeforeResourceLoad to OnResourceResponse to detect resource loading, and waiting when all resources will be loaded protected override bool OnBeforeResourceLoad(CefBrowser browser, CefRequest request, out string redirectUrl, out CefStreamReader resourceStream, CefResponse response, int loadFlags) { var prevColor = Console.ForegroundColor; Console.ForegroundColor = ConsoleColor.DarkMagenta; Console.WriteLine("OnBeforeResourceLoad: {0}", request.GetURL()); Console.ForegroundColor = prevColor; redirectUrl = null; resourceStream = null; return(false); }
protected override bool ProcessRequest(CefRequest request, CefSchemeHandlerCallback callback) { var urlString = request.GetURL(); string errorMessage = null; int errorStatus = 0; string errorStatusText = null; try { var uri = new Uri(urlString); var path = uri.Host + uri.AbsolutePath; // ignore host switch (uri.Host) { case "plugins": CefGlue.Threading.CefThread.UI.Send(_ => { var pluginCount = Cef.GetWebPluginCount(); var s = new StringBuilder(); for (int i = 0; i < pluginCount; i++) { var plugin = Cef.GetWebPluginInfo(i); s.AppendLine(string.Format("Name: {0}", plugin.Name)); s.AppendLine(string.Format("Description: {0}", plugin.Description)); s.AppendLine(string.Format("Version: {0}", plugin.Version)); s.AppendLine(string.Format("Path: {0}", plugin.Path)); s.AppendLine(); } this.stream = new MemoryStream(Encoding.UTF8.GetBytes(s.ToString()), false); }, null ); break; default: throw new Exception(); } if (this.stream != null) { // found this.responseLength = -1; this.status = 200; this.statusText = "OK"; this.mimeType = "text/plain"; callback.HeadersAvailable(); return(true); } } catch (Exception ex) { errorStatus = 500; errorStatusText = "Internal Error"; errorMessage = "<!doctype html><html><body><h1>Internal Error!</h1><pre>" + ex.ToString() + "</pre></body></html>"; } // not found or error while processing request errorMessage = errorMessage ?? "<!doctype html><html><body><h1>Not Found!</h1><p>The requested url [" + urlString + "] was not found!</p></body></html>"; var bytes = Encoding.UTF8.GetBytes(errorMessage); this.stream = new MemoryStream(bytes, false); this.responseLength = -1; this.status = errorStatus != 0 ? errorStatus : 404; this.statusText = errorStatusText ?? "Not Found"; this.mimeType = "text/html"; callback.HeadersAvailable(); return(true); }
protected override bool OnBeforeBrowse(CefBrowser browser, CefFrame frame, CefRequest request, CefHandlerNavType navType, bool isRedirect) { #if DIAGNOSTICS Cef.Logger.Trace(LogTarget.CefRequestHandler, "OnBeforeBrowse: Method=[{0}] Url=[{1}] NavType=[{2}] IsRedirect=[{3}]", request.GetMethod(), request.GetURL(), navType, isRedirect); #endif var cancel = this.context.OnNavigating(frame, request, navType, isRedirect); if (cancel) { return(true); } if (frame.IsMain) { // browser navigating to a new page this.context.ClearFrames(); this.context.AttachMainFrame(frame); this.context.ClearReadyState(); this.context.SetFrameReadyState(null, FrameReadyState.Navigating); } else if ((this.context.ReadyOptions & CefReadyOptions.Frames) != 0) { this.context.SetFrameReadyState(frame.GetName(), FrameReadyState.Navigating); } return(false); }