CefUrlRequestClient.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. using System.IO;
  9. /// <summary>
  10. /// Interface that should be implemented by the CefURLRequest client. The
  11. /// methods of this class will be called on the same thread that created the
  12. /// request unless otherwise documented.
  13. /// </summary>
  14. public abstract unsafe partial class CefUrlRequestClient
  15. {
  16. private void on_request_complete(cef_urlrequest_client_t* self, cef_urlrequest_t* request)
  17. {
  18. CheckSelf(self);
  19. var m_request = CefUrlRequest.FromNative(request); // TODO dispose?
  20. OnRequestComplete(m_request);
  21. }
  22. /// <summary>
  23. /// Notifies the client that the request has completed. Use the
  24. /// CefURLRequest::GetRequestStatus method to determine if the request was
  25. /// successful or not.
  26. /// </summary>
  27. protected abstract void OnRequestComplete(CefUrlRequest request);
  28. private void on_upload_progress(cef_urlrequest_client_t* self, cef_urlrequest_t* request, long current, long total)
  29. {
  30. CheckSelf(self);
  31. var m_request = CefUrlRequest.FromNative(request); // TODO dispose?
  32. OnUploadProgress(m_request, current, total);
  33. }
  34. /// <summary>
  35. /// Notifies the client of upload progress. |current| denotes the number of
  36. /// bytes sent so far and |total| is the total size of uploading data (or -1 if
  37. /// chunked upload is enabled). This method will only be called if the
  38. /// UR_FLAG_REPORT_UPLOAD_PROGRESS flag is set on the request.
  39. /// </summary>
  40. protected abstract void OnUploadProgress(CefUrlRequest request, long current, long total);
  41. private void on_download_progress(cef_urlrequest_client_t* self, cef_urlrequest_t* request, long current, long total)
  42. {
  43. CheckSelf(self);
  44. var m_request = CefUrlRequest.FromNative(request); // TODO dispose?
  45. OnDownloadProgress(m_request, current, total);
  46. }
  47. /// <summary>
  48. /// Notifies the client of download progress. |current| denotes the number of
  49. /// bytes received up to the call and |total| is the expected total size of the
  50. /// response (or -1 if not determined).
  51. /// </summary>
  52. protected abstract void OnDownloadProgress(CefUrlRequest request, long current, long total);
  53. private void on_download_data(cef_urlrequest_client_t* self, cef_urlrequest_t* request, void* data, UIntPtr data_length)
  54. {
  55. CheckSelf(self);
  56. var m_request = CefUrlRequest.FromNative(request); // TODO dispose?
  57. using (var stream = new UnmanagedMemoryStream((byte*)data, (long)data_length))
  58. {
  59. OnDownloadData(m_request, stream);
  60. }
  61. }
  62. /// <summary>
  63. /// Called when some part of the response is read. |data| contains the current
  64. /// bytes received since the last call. This method will not be called if the
  65. /// UR_FLAG_NO_DOWNLOAD_DATA flag is set on the request.
  66. /// </summary>
  67. protected abstract void OnDownloadData(CefUrlRequest request, Stream data);
  68. private int get_auth_credentials(cef_urlrequest_client_t* self, int isProxy, cef_string_t* host, int port, cef_string_t* realm, cef_string_t* scheme, cef_auth_callback_t* callback)
  69. {
  70. CheckSelf(self);
  71. var m_isProxy = isProxy != 0;
  72. var m_host = cef_string_t.ToString(host);
  73. var m_realm = cef_string_t.ToString(realm);
  74. var m_scheme = cef_string_t.ToString(scheme);
  75. var m_callback = CefAuthCallback.FromNative(callback);
  76. var m_result = GetAuthCredentials(m_isProxy, m_host, port, m_realm, m_scheme, m_callback);
  77. return m_result ? 1 : 0;
  78. }
  79. /// <summary>
  80. /// Called on the IO thread when the browser needs credentials from the user.
  81. /// |isProxy| indicates whether the host is a proxy server. |host| contains the
  82. /// hostname and |port| contains the port number. Return true to continue the
  83. /// request and call CefAuthCallback::Continue() when the authentication
  84. /// information is available. If the request has an associated browser/frame
  85. /// then returning false will result in a call to GetAuthCredentials on the
  86. /// CefRequestHandler associated with that browser, if any. Otherwise,
  87. /// returning false will cancel the request immediately. This method will only
  88. /// be called for requests initiated from the browser process.
  89. /// </summary>
  90. protected virtual bool GetAuthCredentials(bool isProxy, string host, int port, string realm, string scheme, CefAuthCallback callback)
  91. {
  92. return false;
  93. }
  94. }
  95. }