CefWriteHandler.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. namespace Xilium.CefGlue
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.IO;
  7. using System.Runtime.InteropServices;
  8. using Xilium.CefGlue.Interop;
  9. /// <summary>
  10. /// Interface the client can implement to provide a custom stream writer. The
  11. /// methods of this class may be called on any thread.
  12. /// </summary>
  13. public abstract unsafe partial class CefWriteHandler
  14. {
  15. private UIntPtr write(cef_write_handler_t* self, void* ptr, UIntPtr size, UIntPtr n)
  16. {
  17. CheckSelf(self);
  18. var length = (long)size * (long)n;
  19. using (var stream = new UnmanagedMemoryStream((byte*)ptr, length, length, FileAccess.Write))
  20. {
  21. return (UIntPtr)Write(stream, length);
  22. }
  23. }
  24. /// <summary>
  25. /// Write raw binary data.
  26. /// </summary>
  27. protected abstract long Write(Stream stream, long length);
  28. private int seek(cef_write_handler_t* self, long offset, int whence)
  29. {
  30. CheckSelf(self);
  31. return Seek(offset, (SeekOrigin)whence) ? 0 : -1;
  32. }
  33. /// <summary>
  34. /// Seek to the specified offset position. |whence| may be any one of
  35. /// SEEK_CUR, SEEK_END or SEEK_SET. Return zero on success and non-zero on
  36. /// failure.
  37. /// </summary>
  38. protected abstract bool Seek(long offset, SeekOrigin whence);
  39. private long tell(cef_write_handler_t* self)
  40. {
  41. CheckSelf(self);
  42. return Tell();
  43. }
  44. /// <summary>
  45. /// Return the current offset position.
  46. /// </summary>
  47. protected abstract long Tell();
  48. private int flush(cef_write_handler_t* self)
  49. {
  50. CheckSelf(self);
  51. return Flush() ? 0 : -1;
  52. }
  53. /// <summary>
  54. /// Flush the stream.
  55. /// </summary>
  56. protected abstract bool Flush();
  57. private int may_block(cef_write_handler_t* self)
  58. {
  59. CheckSelf(self);
  60. return MayBlock() ? 1 : 0;
  61. }
  62. /// <summary>
  63. /// Return true if this handler performs work like accessing the file system
  64. /// which may block. Used as a hint for determining the thread to access the
  65. /// handler from.
  66. /// </summary>
  67. protected abstract bool MayBlock();
  68. }
  69. }