Пример #1
0
 /// <summary>
 /// Handle the <see cref="IWebView.DOMContentLoaded" /> event, ensuring the JavaScript bridge is loaded.
 /// </summary>
 /// <param name="sender">The sender.</param>
 /// <param name="e">The <see cref="WebViewControlDOMContentLoadedEventArgs"/> instance containing the event data.</param>
 /// <remarks>
 /// The bridge can be loaded by
 ///   - Explicitly using a script tag in the markup of the page, or
 ///   - Explicitly through handling the navigation or DOM events
 ///
 /// If the JavaScript bridge is already loaded, it will not be loaded again.
 /// </remarks>
 /// <seealso cref="AllowedScriptNotifyUris"/>
 /// <seealso cref="LoadBridge"/>
 private void OnWebViewDOMContentLoaded(object sender, WebViewControlDOMContentLoadedEventArgs e)
 {
     if (e.Uri != null &&
         AllowedScriptNotifyUris != null &&
         AllowedScriptNotifyUris.Contains(e.Uri) &&
         sender is IWebView webView)
     {
         LoadBridge();
     }
 }
Пример #2
0
        /// <summary>
        /// Handles the <see cref="IWebView.ScriptNotify" /> event.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="WebViewControlScriptNotifyEventArgs"/> instance containing the event data.</param>
        /// <remarks>
        /// Since there may be other uses of <code>window.external.notify</code> in use, this method checks to see if the
        /// message came from a URI within <see cref="AllowedScriptNotifyUris"/> and the bridge before processing it. If it did,
        /// it triggers processing of all queued messages within the bridge asynchronously.
        /// </remarks>
        /// <seealso cref="AllowedScriptNotifyUris"/>
        /// <seealso cref="IsJavaScriptBridgeMessage"/>
        /// <seealso cref="FlushAsync"/>
        private void OnWebViewScriptNotify(object sender, WebViewControlScriptNotifyEventArgs e)
        {
            Trace.WriteLine($"Entering {typeof(JavaScriptBridge).FullName}.{nameof(OnWebViewScriptNotify)}", Constants.TraceName);

            // White list
            // WinForms/WPF do not validate the origins for ScriptNotify like UWP does
            // This needs to be done manually for security
            if (e.Uri != null && AllowedScriptNotifyUris != null && AllowedScriptNotifyUris.Contains(e.Uri))
            {
                var message = e.Value;

                if (sender is IWebView webView && IsJavaScriptBridgeMessage(message))
                {
                    Trace.WriteLine($"Received command {message}", Constants.TraceName);

                    // Asynchronously flush the messages from the JavaScript bridge
                    FlushAsync();
                }
            }
            else
            {
                Trace.WriteLine($"{nameof(IWebView.ScriptNotify)} received from unknown origin {e.Uri?.Host}", Constants.TraceName);
            }
        }