Repository: unoplatform/uno
File: src/Uno.UI.Runtime.Skia.MacOS/UnoNativeMac/UnoNativeMac/UNOWebView.m (Objective-C)

Code before:
}

static uno_webview_new_window_requested_fn_ptr new_window_requested;

inline uno_webview_new_window_requested_fn_ptr uno_get_webview_new_window_requested_callback(void)
{
    return new_window_requested;
}

void uno_set_webview_new_window_requested_callback(uno_webview_new_window_requested_fn_ptr fn_ptr)
{
    new_window_requested = fn_ptr;
}

- (WKWebView *)webView:(WKWebView *)webView
createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration
           forNavigationAction:(WKNavigationAction *)navigationAction
                windowFeatures:(WKWindowFeatures *)windowFeatures {

#if DEBUG
    NSLog(@"NATIVE DEBUG: createWebViewWithConfiguration fired for URL: %@", navigationAction.request.URL);
#endif

    uno_webview_new_window_requested_fn_ptr callback = uno_get_webview_new_window_requested_callback();
    
    if (callback) {
#if DEBUG
        NSLog(@"NATIVE DEBUG: Found C# callback. Attempting to call...");
#endif
        
        const char* targetUrl = [navigationAction.request.URL.absoluteString UTF8String];
        
        const char* refererUrl = [navigationAction.sourceFrame.request.URL.absoluteString UTF8String];
        if (refererUrl == NULL) {
            refererUrl = "about:blank";
        }

        int handled = callback(webView, targetUrl, refererUrl);

#if DEBUG
        NSLog(@"NATIVE DEBUG: C# callback returned: %d", handled);
#endif

        // Check if C# handled it (returned 1)
        if (handled == 1) {
#if DEBUG
            NSLog(@"NATIVE DEBUG: C# handled the request. Cancelling native new window.");
#endif
            return nil;
        }
    } else {

Review comment (bug):
minor: setting the callbacks is not optional so you can assume it's set (if not it will crash, which is faster/easier to detect than running all the tests).

It could be replaced with an `assert(callback);` too - for a nicer crash.

Code after:
}

static uno_webview_new_window_requested_fn_ptr new_window_requested;

inline uno_webview_new_window_requested_fn_ptr uno_get_webview_new_window_requested_callback(void)
{
    return new_window_requested;
}

void uno_set_webview_new_window_requested_callback(uno_webview_new_window_requested_fn_ptr fn_ptr)
{
    new_window_requested = fn_ptr;
}

- (WKWebView *)webView:(WKWebView *)webView
createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration
           forNavigationAction:(WKNavigationAction *)navigationAction
                windowFeatures:(WKWindowFeatures *)windowFeatures {

#if DEBUG
    NSLog(@"createWebViewWithConfiguration: fired for URL: %@", navigationAction.request.URL);
#endif

    uno_webview_new_window_requested_fn_ptr callback = uno_get_webview_new_window_requested_callback();
    
    assert(callback);
#if DEBUG
    NSLog(@"createWebViewWithConfiguration: Found C# callback. Attempting to call...");
#endif
        
    const char* targetUrl = [navigationAction.request.URL.absoluteString UTF8String];
    if (targetUrl == nil) {
        targetUrl = "about:blank";
    }
    
    const char* refererUrl = [navigationAction.sourceFrame.request.URL.absoluteString UTF8String];
    if (refererUrl == nil) {
        refererUrl = "about:blank";
    }

    int handled = callback(webView, targetUrl, refererUrl);

#if DEBUG
    NSLog(@"createWebViewWithConfiguration: C# callback returned: %d", handled);
#endif

    // Check if C# handled it (returned 1)
    if (handled == 1) {
#if DEBUG
        NSLog(@"createWebViewWithConfiguration: C# handled the request. Cancelling native new window.");
#endif