From 8e016789c9bcbefb2cbf5d98e566cb6f92dad957 Mon Sep 17 00:00:00 2001
From: amaitland Custom Schemes
...and the actual scheme handler class like this:
- internal class CefSharpSchemeHandler : IResourceHandler
+ //This Resource handler (scheme handler factory returns IResourceHandler) downloads a file
+ // and servces the content as the response, you could just as easily read from a database or disk
+ public class FlashResourceHandler : ResourceHandler
{
- private string mimeType;
- private MemoryStream stream;
-
- public bool ProcessRequestAsync(IRequest request, ICallback callback)
+ public override bool ProcessRequestAsync(IRequest request, ICallback callback)
{
- // NOTE: We suggest you structure your code in an async fashion
- // First examine the "request" object for info about the URI being requested and so forth.
- // If the Url is valid then spawn a task
-
Task.Run(() =>
{
- // In this task you can perform your time consuming operations, e.g query a database
- // NOTE: We suggest that you wrap callbacks in a using statemnt so that they're disposed
- // even if there is an exception as they wrap an unmanaged response which will cause memory
- // leaks if not freed
using (callback)
{
- // Read the data in, set the mime type
- var bytes = Encoding.UTF8.GetBytes(resource);
- stream = new MemoryStream(bytes);
- var fileExtension = Path.GetExtension(fileName);
- mimeType = ResourceHandler.GetMimeType(fileExtension);
-
- // When your finished processing execute the callback.
- // Most callbacks have multiple methods, so checkout their interface for details
+ var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://samples.mplayerhq.hu/SWF/zeldaADPCM5bit.swf");
+
+ var httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
+
+ // Get the stream associated with the response.
+ var receiveStream = httpWebResponse.GetResponseStream();
+ var mime = httpWebResponse.ContentType;
+
+ var stream = new MemoryStream();
+ receiveStream.CopyTo(stream);
+ httpWebResponse.Close();
+
+ //Reset the stream position to 0 so the stream can be copied into the underlying unmanaged buffer
+ stream.Position = 0;
+
+ //Populate the response values - No longer need to implement GetResponseHeaders (unless you need to perform a redirect)
+ ResponseLength = stream.Length;
+ MimeType = mime;
+ StatusCode = (int)HttpStatusCode.OK;
+ Stream = stream;
+
callback.Continue();
}
});
- // Return true to indicate that you've handled the request, you can return false which will cancel the request
return true;
}
-
- public Stream GetResponse(IResponse response, out long responseLength, out string redirectUrl)
- {
- // NOTE: GetResponse will be executed after you have called Continue()
- // You simple need to populate the relevant details, note you can also redirec to another url if required
- responseLength = stream.Length;
- redirectUrl = null;
- response.StatusCode = (int)HttpStatusCode.OK;
- response.StatusText = "OK";
- response.MimeType = mimeType;
- return stream;
- }
}
Finally, you have to register this scheme handler using some code like this:
@@ -319,11 +310,13 @@ JavaScript integration
of the block below will be modified/inspected by the script code.
You can modify the value of this text field using JavaScript!
+ For more details and further examples read General Usage Guide - Javascript Integration
The C# code for performing these kinds of interactions is quite simple. Like this:
+ using CefSharp; //EvaluateScriptAsync is an extension method webBrowser.ExecuteScriptAsync(someScriptCode);@@ -342,7 +335,8 @@
- var result = webBrowser.EvaluateScript("10 + 20"); + using CefSharp; //EvaluateScriptAsync is an extension method + var result = await webBrowser.EvaluateScriptAsync("10 + 20");
@@ -350,7 +344,6 @@