CefPostDataElement.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. namespace Xilium.CefGlue
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.Runtime.InteropServices;
  7. using Xilium.CefGlue.Interop;
  8. /// <summary>
  9. /// Class used to represent a single element in the request post data. The
  10. /// methods of this class may be called on any thread.
  11. /// </summary>
  12. public sealed unsafe partial class CefPostDataElement
  13. {
  14. /// <summary>
  15. /// Create a new CefPostDataElement object.
  16. /// </summary>
  17. public static CefPostDataElement Create()
  18. {
  19. return CefPostDataElement.FromNative(
  20. cef_post_data_element_t.create()
  21. );
  22. }
  23. /// <summary>
  24. /// Returns true if this object is read-only.
  25. /// </summary>
  26. public bool IsReadOnly
  27. {
  28. get { return cef_post_data_element_t.is_read_only(_self) != 0; }
  29. }
  30. /// <summary>
  31. /// Remove all contents from the post data element.
  32. /// </summary>
  33. public void SetToEmpty()
  34. {
  35. cef_post_data_element_t.set_to_empty(_self);
  36. }
  37. /// <summary>
  38. /// The post data element will represent a file.
  39. /// </summary>
  40. public void SetToFile(string fileName)
  41. {
  42. if (string.IsNullOrEmpty(fileName)) throw new ArgumentException("Argument can't be null or empty.", "fileName");
  43. fixed (char* fileName_str = fileName)
  44. {
  45. var n_fileName = new cef_string_t(fileName_str, fileName.Length);
  46. cef_post_data_element_t.set_to_file(_self, &n_fileName);
  47. }
  48. }
  49. /// <summary>
  50. /// The post data element will represent bytes. The bytes passed
  51. /// in will be copied.
  52. /// </summary>
  53. public void SetToBytes(byte[] bytes)
  54. {
  55. if (bytes == null) throw new ArgumentNullException("bytes");
  56. fixed (byte* bytes_ptr = bytes)
  57. {
  58. cef_post_data_element_t.set_to_bytes(_self, (UIntPtr)bytes.Length, bytes_ptr);
  59. }
  60. }
  61. /// <summary>
  62. /// Return the type of this post data element.
  63. /// </summary>
  64. public CefPostDataElementType ElementType
  65. {
  66. get { return cef_post_data_element_t.get_type(_self); }
  67. }
  68. /// <summary>
  69. /// Return the file name.
  70. /// </summary>
  71. public string GetFile()
  72. {
  73. var n_result = cef_post_data_element_t.get_file(_self);
  74. return cef_string_userfree.ToString(n_result);
  75. }
  76. /// <summary>
  77. /// Return the number of bytes.
  78. /// </summary>
  79. public long BytesCount
  80. {
  81. get { return (long)cef_post_data_element_t.get_bytes_count(_self); }
  82. }
  83. /// <summary>
  84. /// Read up to |size| bytes into |bytes| and return the number of bytes
  85. /// actually read.
  86. /// </summary>
  87. public byte[] GetBytes()
  88. {
  89. var size = BytesCount;
  90. if (size == 0) return new byte[0];
  91. var bytes = new byte[size];
  92. fixed (byte* bytes_ptr = bytes)
  93. {
  94. var written = (long)cef_post_data_element_t.get_bytes(_self, (UIntPtr)size, bytes_ptr);
  95. if (written != size) throw new InvalidOperationException();
  96. }
  97. return bytes;
  98. }
  99. }
  100. }