CefJSDialogHandler.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. /// Implement this interface to handle events related to JavaScript dialogs. The
  10. /// methods of this class will be called on the UI thread.
  11. /// </summary>
  12. public abstract unsafe partial class CefJSDialogHandler
  13. {
  14. private int on_jsdialog(cef_jsdialog_handler_t* self, cef_browser_t* browser, cef_string_t* origin_url, CefJSDialogType dialog_type, cef_string_t* message_text, cef_string_t* default_prompt_text, cef_jsdialog_callback_t* callback, int* suppress_message)
  15. {
  16. CheckSelf(self);
  17. var m_browser = CefBrowser.FromNative(browser);
  18. var m_origin_url = cef_string_t.ToString(origin_url);
  19. var m_message_text = cef_string_t.ToString(message_text);
  20. var m_default_prompt_text = cef_string_t.ToString(default_prompt_text);
  21. var m_callback = CefJSDialogCallback.FromNative(callback);
  22. bool m_suppress_message;
  23. var result = OnJSDialog(m_browser, m_origin_url, dialog_type, m_message_text, m_default_prompt_text, m_callback, out m_suppress_message);
  24. *suppress_message = m_suppress_message ? 1 : 0;
  25. return result ? 1 : 0;
  26. }
  27. /// <summary>
  28. /// Called to run a JavaScript dialog. If |origin_url| is non-empty it can be
  29. /// passed to the CefFormatUrlForSecurityDisplay function to retrieve a secure
  30. /// and user-friendly display string. The |default_prompt_text| value will be
  31. /// specified for prompt dialogs only. Set |suppress_message| to true and
  32. /// return false to suppress the message (suppressing messages is preferable to
  33. /// immediately executing the callback as this is used to detect presumably
  34. /// malicious behavior like spamming alert messages in onbeforeunload). Set
  35. /// |suppress_message| to false and return false to use the default
  36. /// implementation (the default implementation will show one modal dialog at a
  37. /// time and suppress any additional dialog requests until the displayed dialog
  38. /// is dismissed). Return true if the application will use a custom dialog or
  39. /// if the callback has been executed immediately. Custom dialogs may be either
  40. /// modal or modeless. If a custom dialog is used the application must execute
  41. /// |callback| once the custom dialog is dismissed.
  42. /// </summary>
  43. protected abstract bool OnJSDialog(CefBrowser browser, string originUrl, CefJSDialogType dialogType, string message_text, string default_prompt_text, CefJSDialogCallback callback, out bool suppress_message);
  44. private int on_before_unload_dialog(cef_jsdialog_handler_t* self, cef_browser_t* browser, cef_string_t* message_text, int is_reload, cef_jsdialog_callback_t* callback)
  45. {
  46. CheckSelf(self);
  47. var m_browser = CefBrowser.FromNative(browser);
  48. var m_message_text = cef_string_t.ToString(message_text);
  49. var m_callback = CefJSDialogCallback.FromNative(callback);
  50. return OnBeforeUnloadDialog(m_browser, m_message_text, is_reload != 0, m_callback) ? 1 : 0;
  51. }
  52. /// <summary>
  53. /// Called to run a dialog asking the user if they want to leave a page. Return
  54. /// false to use the default dialog implementation. Return true if the
  55. /// application will use a custom dialog or if the callback has been executed
  56. /// immediately. Custom dialogs may be either modal or modeless. If a custom
  57. /// dialog is used the application must execute |callback| once the custom
  58. /// dialog is dismissed.
  59. /// </summary>
  60. protected abstract bool OnBeforeUnloadDialog(CefBrowser browser, string messageText, bool isReload, CefJSDialogCallback callback);
  61. private void on_reset_dialog_state(cef_jsdialog_handler_t* self, cef_browser_t* browser)
  62. {
  63. CheckSelf(self);
  64. var m_browser = CefBrowser.FromNative(browser);
  65. OnResetDialogState(m_browser);
  66. }
  67. /// <summary>
  68. /// Called to cancel any pending dialogs and reset any saved dialog state. Will
  69. /// be called due to events like page navigation irregardless of whether any
  70. /// dialogs are currently pending.
  71. /// </summary>
  72. protected abstract void OnResetDialogState(CefBrowser browser);
  73. private void on_dialog_closed(cef_jsdialog_handler_t* self, cef_browser_t* browser)
  74. {
  75. CheckSelf(self);
  76. var m_browser = CefBrowser.FromNative(browser);
  77. OnDialogClosed(m_browser);
  78. }
  79. /// <summary>
  80. /// Called when the default implementation dialog is closed.
  81. /// </summary>
  82. protected abstract void OnDialogClosed(CefBrowser browser);
  83. }
  84. }