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

Code before:

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 {
#if DEBUG
        NSLog(@"NATIVE DEBUG: C# callback (new_window_requested) was NULL.");
#endif

Review comment (bug):
We don't ship DEBUG binaries for this native library. IOW the `NSLog` output is only if you build from sources.

At runtime there's a ton of logs (at some point we'll have to make it easier to enable/disable some). So we often have to do search thru them, so it's easier if each function has the same prefix (like the name of the method here, `createWebViewWithConfiguration`, so VSCode can filter them).


Code after:

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
        return nil;
    }