|
| 1 | +using System; |
| 2 | +using System.Runtime.InteropServices; |
| 3 | +using System.Threading; |
| 4 | + |
| 5 | +// --- Version 2.0.0-beta.3 --- |
| 6 | +// Breaking Change: Sender-Aware Events (Issue #52) |
| 7 | + |
| 8 | +namespace NetWebView2Lib |
| 9 | +{ |
| 10 | + /// <summary> |
| 11 | + /// Delegate for detecting when messages are received from JavaScript. |
| 12 | + /// v2.0.0: Now includes sender object and parent window handle. |
| 13 | + /// </summary> |
| 14 | + /// <param name="sender">The WebViewManager instance that owns this bridge.</param> |
| 15 | + /// <param name="parentHandle">The parent window handle (Advanced Window Description).</param> |
| 16 | + /// <param name="message">The message content.</param> |
| 17 | + [ComVisible(true)] |
| 18 | + public delegate void OnMessageReceivedDelegate(object sender, string parentHandle, string message); |
| 19 | + |
| 20 | + /// <summary> |
| 21 | + /// Event interface for receiving messages from JavaScript via AutoIt. |
| 22 | + /// v2.0.0: Sender-Aware pattern. |
| 23 | + /// </summary> |
| 24 | + [Guid("3E4F5A6B-7C8D-9E0F-1A2B-3C4D5E6F7A8B")] |
| 25 | + [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)] |
| 26 | + [ComVisible(true)] |
| 27 | + public interface IBridgeEvents |
| 28 | + { |
| 29 | + /// <summary> |
| 30 | + /// Triggered when a message is received from JavaScript. |
| 31 | + /// </summary> |
| 32 | + /// <param name="sender">The WebViewManager instance.</param> |
| 33 | + /// <param name="parentHandle">The parent window handle.</param> |
| 34 | + /// <param name="message">The message content.</param> |
| 35 | + [DispId(1)] |
| 36 | + void OnMessageReceived(object sender, string parentHandle, string message); |
| 37 | + } |
| 38 | + |
| 39 | + /// <summary> |
| 40 | + /// Action interface for sending messages from JavaScript to AutoIt. |
| 41 | + /// </summary> |
| 42 | + [Guid("2D3E4F5A-6A7A-4A9B-8C7D-2E3F4A5B6C7D")] |
| 43 | + [InterfaceType(ComInterfaceType.InterfaceIsDual)] |
| 44 | + [ComVisible(true)] |
| 45 | + public interface IBridgeActions |
| 46 | + { |
| 47 | + /// <summary> |
| 48 | + /// Send a message to AutoIt. |
| 49 | + /// </summary> |
| 50 | + /// <param name="message">The message to send.</param> |
| 51 | + [DispId(1)] |
| 52 | + void RaiseMessage(string message); |
| 53 | + |
| 54 | + /// <summary>Gets the version of the DLL.</summary> |
| 55 | + [DispId(2)] string Version { get; } |
| 56 | + } |
| 57 | + |
| 58 | + /// <summary> |
| 59 | + /// Implementation of the bridge between WebView2 JavaScript and AutoIt. |
| 60 | + /// v2.0.0: Supports Sender-Aware event pattern. |
| 61 | + /// </summary> |
| 62 | + [Guid("8F9A0B1C-2D3E-4F5A-6B7C-8D9E0F1A2B3C")] |
| 63 | + [ClassInterface(ClassInterfaceType.None)] |
| 64 | + [ComSourceInterfaces(typeof(IBridgeEvents))] |
| 65 | + [ComVisible(true)] |
| 66 | + [ProgId("NetWebView2Lib.WebView2Bridge")] |
| 67 | + public class WebView2Bridge : IBridgeActions |
| 68 | + { |
| 69 | + /// <summary> |
| 70 | + /// Event fired when a message is received from JavaScript. |
| 71 | + /// </summary> |
| 72 | + public event OnMessageReceivedDelegate OnMessageReceived; |
| 73 | + |
| 74 | + private SynchronizationContext _syncContext; |
| 75 | + private readonly System.Diagnostics.Stopwatch _throttleStopwatch = System.Diagnostics.Stopwatch.StartNew(); |
| 76 | + private long _lastMessageTicks = 0; |
| 77 | + private const long ThrottlingIntervalTicks = TimeSpan.TicksPerSecond / 50; // max 50 calls/sec |
| 78 | + |
| 79 | + // v2.0.0: Parent context for Sender-Aware events |
| 80 | + private object _sender; |
| 81 | + private string _parentHandle; |
| 82 | + |
| 83 | + /// <summary> |
| 84 | + /// Initializes a new instance of the WebView2Bridge class. |
| 85 | + /// </summary> |
| 86 | + public WebView2Bridge() |
| 87 | + { |
| 88 | + _syncContext = SynchronizationContext.Current ?? new SynchronizationContext(); |
| 89 | + } |
| 90 | + |
| 91 | + /// <summary> |
| 92 | + /// Sets the parent context for Sender-Aware events. |
| 93 | + /// Must be called immediately after construction. |
| 94 | + /// </summary> |
| 95 | + public void SetParentContext(object sender, object hwnd) |
| 96 | + { |
| 97 | + _sender = sender; |
| 98 | + |
| 99 | + if (hwnd is IntPtr ptr) |
| 100 | + { |
| 101 | + _parentHandle = "[HANDLE:0x" + ptr.ToString("X").PadLeft(IntPtr.Size * 2, '0') + "]"; |
| 102 | + } |
| 103 | + else |
| 104 | + { |
| 105 | + _parentHandle = hwnd?.ToString() ?? "[HANDLE:0x" + IntPtr.Zero.ToString("X").PadLeft(IntPtr.Size * 2, '0') + "]"; |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + /// <summary> |
| 110 | + /// Updates the SynchronizationContext used for raising events. |
| 111 | + /// </summary> |
| 112 | + /// <param name="context">The new context.</param> |
| 113 | + public void SetSyncContext(SynchronizationContext context) |
| 114 | + { |
| 115 | + _syncContext = context; |
| 116 | + } |
| 117 | + |
| 118 | + /// <summary> |
| 119 | + /// Send a message from JavaScript to AutoIt. |
| 120 | + /// v2.0.0: Now passes sender and parentHandle to the event. |
| 121 | + /// </summary> |
| 122 | + /// <param name="message">The message content.</param> |
| 123 | + public void RaiseMessage(string message) |
| 124 | + { |
| 125 | + if (OnMessageReceived != null) |
| 126 | + { |
| 127 | + // Throttling: Max 50 messages per second (Law #2: Performance) |
| 128 | + long currentTicks = _throttleStopwatch.ElapsedTicks; |
| 129 | + if (currentTicks - _lastMessageTicks < ThrottlingIntervalTicks) return; |
| 130 | + _lastMessageTicks = currentTicks; |
| 131 | + |
| 132 | + // v2.0.0: Pass sender and parentHandle for multi-instance support |
| 133 | + _syncContext?.Post(_ => OnMessageReceived?.Invoke(_sender, _parentHandle, message), null); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + /// <summary>Gets the version of the DLL.</summary> |
| 138 | + public string Version => AssemblyUtils.GetVersion(); |
| 139 | + } |
| 140 | + |
| 141 | + /// <summary> |
| 142 | + /// Compatibility Layer for WebViewBridge (Legacy name). |
| 143 | + /// Inherits all functionality from WebView2Bridge. |
| 144 | + /// </summary> |
| 145 | + [Guid("1A2B3C4D-5E6F-4A8B-9C0D-1E2F3A4B5C6D")] // OLD GUID |
| 146 | + [ClassInterface(ClassInterfaceType.None)] |
| 147 | + [ComSourceInterfaces(typeof(IBridgeEvents))] |
| 148 | + [ComVisible(true)] |
| 149 | + [ProgId("NetWebView2Lib.WebViewBridge")] // Explicit OLD ProgID |
| 150 | + public class WebViewBridge : WebView2Bridge, IBridgeActions |
| 151 | + { |
| 152 | + // Inherits everything |
| 153 | + } |
| 154 | +} |
0 commit comments