CefDownloadHandler.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 handle file downloads. The methods of this class will called
  10. /// on the browser process UI thread.
  11. /// </summary>
  12. public abstract unsafe partial class CefDownloadHandler
  13. {
  14. private void on_before_download(cef_download_handler_t* self, cef_browser_t* browser, cef_download_item_t* download_item, cef_string_t* suggested_name, cef_before_download_callback_t* callback)
  15. {
  16. CheckSelf(self);
  17. var m_browser = CefBrowser.FromNative(browser);
  18. using (var m_download_item = CefDownloadItem.FromNative(download_item))
  19. {
  20. var m_suggested_name = cef_string_t.ToString(suggested_name);
  21. var m_callback = CefBeforeDownloadCallback.FromNative(callback);
  22. OnBeforeDownload(m_browser, m_download_item, m_suggested_name, m_callback);
  23. }
  24. }
  25. /// <summary>
  26. /// Called before a download begins. |suggested_name| is the suggested name for
  27. /// the download file. By default the download will be canceled. Execute
  28. /// |callback| either asynchronously or in this method to continue the download
  29. /// if desired. Do not keep a reference to |download_item| outside of this
  30. /// method.
  31. /// </summary>
  32. protected virtual void OnBeforeDownload(CefBrowser browser, CefDownloadItem downloadItem, string suggestedName, CefBeforeDownloadCallback callback)
  33. {
  34. }
  35. private void on_download_updated(cef_download_handler_t* self, cef_browser_t* browser, cef_download_item_t* download_item, cef_download_item_callback_t* callback)
  36. {
  37. CheckSelf(self);
  38. var m_browser = CefBrowser.FromNative(browser);
  39. using (var m_download_item = CefDownloadItem.FromNative(download_item))
  40. {
  41. var m_callback = CefDownloadItemCallback.FromNative(callback);
  42. OnDownloadUpdated(m_browser, m_download_item, m_callback);
  43. }
  44. }
  45. /// <summary>
  46. /// Called when a download's status or progress information has been updated.
  47. /// This may be called multiple times before and after OnBeforeDownload().
  48. /// Execute |callback| either asynchronously or in this method to cancel the
  49. /// download if desired. Do not keep a reference to |download_item| outside of
  50. /// this method.
  51. /// </summary>
  52. protected virtual void OnDownloadUpdated(CefBrowser browser, CefDownloadItem downloadItem, CefDownloadItemCallback callback)
  53. {
  54. }
  55. }
  56. }