Advertisement
Guest User

Awesomium ResourceInterceptor implementation

a guest
Aug 22nd, 2014
607
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.19 KB | None | 0 0
  1. class ResourceInterceptor : IResourceInterceptor
  2.     {
  3.         public ResourceResponse OnRequest(ResourceRequest request)
  4.         {
  5.             Stream stream = null;
  6.             if (request.Url.ToString() == "url you want to intercept")
  7.             {
  8.         //you may want to route to another local file
  9.                 stream = = new FileStream("yourLocalFile.txt", FileMode.Open);
  10.             }
  11.             else
  12.             {
  13.                 return null;
  14.             }
  15.             byte[] buffer = new byte[stream.Length];
  16.             stream.Read(buffer, 0, buffer.Length);
  17.  
  18.             // Initialize unmanaged memory to hold the array.
  19.             int size = Marshal.SizeOf(buffer[0]) * buffer.Length;
  20.             IntPtr pnt = Marshal.AllocHGlobal(size);
  21.  
  22.             try
  23.             {
  24.                 // Copy the array to unmanaged memory.
  25.                 Marshal.Copy(buffer, 0, pnt, buffer.Length);
  26.  
  27.                 // Alternatively, you can pin the array in the managed heap.
  28.                 // Note however that pinning objects seriously disrupts GC operation.
  29.                 // Being able to move objects around in the heap is one of the reasons
  30.                 // why modern GCs can (somewhat) keep up with manual memory management.
  31.                 // By pinning objects in the managed heap, the GC looses it's one
  32.                 // performance advantage over manual memory management:
  33.                 // a relatively un-fragmented heap.
  34.                 //GCHandle handle = GCHandle.Alloc( buffer, GCHandleType.Pinned );
  35.                 //IntPtr pnt = handle.AddrOfPinnedObject();
  36.  
  37.                 // Create a ResourceResponse. A copy is made of the supplied buffer.
  38.                 return ResourceResponse.Create((uint)buffer.Length, pnt, "text/html");
  39.             }
  40.             finally
  41.             {
  42.                 // Data is not owned by the ResourceResponse. A copy is made
  43.                 // of the supplied buffer. We can safely free the unmanaged memory.
  44.                 Marshal.FreeHGlobal(pnt);
  45.                 stream.Close();
  46.             }
  47.         }
  48.  
  49.         public bool OnFilterNavigation(NavigationRequest request)
  50.         {
  51.             return false;
  52.         }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement