MessageDispatcher.cs 947 B

123456789101112131415161718192021222324
  1. using System;
  2. using System.Collections.Generic;
  3. namespace Xilium.CefGlue.Common.Shared.Helpers
  4. {
  5. internal class MessageDispatcher
  6. {
  7. private readonly Dictionary<string, Action<MessageReceivedEventArgs>> _messageHandlers = new Dictionary<string, Action<MessageReceivedEventArgs>>();
  8. public void DispatchMessage(CefBrowser browser, CefFrame frame, CefProcessId sourceProcess, CefProcessMessage message)
  9. {
  10. if (_messageHandlers.TryGetValue(message.Name, out var existingHandler))
  11. {
  12. existingHandler(new MessageReceivedEventArgs(browser, frame, sourceProcess, message));
  13. }
  14. }
  15. public void RegisterMessageHandler(string messageName, Action<MessageReceivedEventArgs> handler)
  16. {
  17. _messageHandlers.TryGetValue(messageName, out var existingHandler);
  18. _messageHandlers[messageName] = existingHandler + handler;
  19. }
  20. }
  21. }