Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

webview - crash when viewing local server content? #8

Closed
rorywalsh opened this issue Mar 30, 2023 · 4 comments
Closed

webview - crash when viewing local server content? #8

rorywalsh opened this issue Mar 30, 2023 · 4 comments

Comments

@rorywalsh
Copy link

rorywalsh commented Mar 30, 2023

I'm trying to display some content running on a local server (via python http>server, or VSCode's liver-server extensions). The html couldn't be simpler:

<!DOCTYPE html>
<html lang="en">
<head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.6.0/p5.js"></script>
    <meta charset="utf-8" />
</head>
<body>
    <main>
    </main>
    <script>
       function setup(){
        print("test")
       }
    </script>
</body>
</html>

It runs fine in all of the browsers I've installed, but it causes an exception with choc_WebView.h here:

image

I'm just using the demo code with navigate() instead of setHTML() Any ideas? I'm well out of my depth here. Am I correct in assuming that if it runs in Edge it should run fine?

[edit] It seems that it might be something unique to p5js that causes this problem. If I try with another js library, it works fine. I'd still love to know if it could be resolved...

@julianstorer
Copy link
Collaborator

Hmm - we just had a go at recreating this and couldn't get it to fail.

a) Does the unmodified choc test app also crash?
b) Does it crash if you navigate to a normal site, e.g. google.com?
c) Exactly what have you modified in your code? It kind of looks like a dangling pointer error, so perhaps you've accidentally deleted the webview somehow...?

@rorywalsh
Copy link
Author

a) Does the unmodified choc test app also crash?

Not, it works fine.

b) Does it crash if you navigate to a normal site, e.g. google.com?

No, all good there:

c) Exactly what have you modified in your code? It kind of looks like a dangling pointer error, so perhaps you've accidentally deleted the webview somehow...?

static int openDemoWebViewWindow()
{
    choc::ui::setWindowsDPIAwareness(); // For Windows, we need to tell the OS we're high-DPI-aware

    choc::ui::DesktopWindow window ({ 100, 100, 800, 600 });

    window.setWindowTitle ("Hello");
    window.setResizable (true);
    window.setMinimumSize (300, 300);
    window.setMaximumSize (1500, 1200);
    window.windowClosed = [] { choc::messageloop::stop(); };

    choc::ui::WebView webview;

    window.setContent (webview.getViewHandle());

    // webview.bind ("eventCallbackFn", [] (const choc::value::ValueView& args) -> choc::value::Value
    // {
    //     auto message = "eventCallbackFn() called with args: " + choc::json::toString (args);

    //     // This just shows how to invoke an async callback
    //     choc::messageloop::postMessage ([message]
    //     {
    //         std::cout << "WebView callback message: " << message << std::endl;
    //     });

    //     return choc::value::createString (message);
    // });

    // webview.bind ("loadCHOCWebsite", [&webview] (const choc::value::ValueView&) -> choc::value::Value
    // {
    //     webview.navigate ("https://github.com/Tracktion/choc");
    //     return {};
    // });

webview.navigate("http://127.0.0.1:56278/index.html");
    // webview.setHTML (R"xxx(
    //   <!DOCTYPE html> <html>
    //     <head> <title>Page Title</title> </head>
    //     <script>
    //       var eventCounter = 0;

    //       // invokes a call to eventCallbackFn() and displays the return value
    //       function sendEvent()
    //       {
    //         // When you invoke a function, it returns a Promise object
    //         eventCallbackFn({ counter: ++eventCounter }, "Hello World")
    //           .then ((result) => { document.getElementById ("eventResultDisplay").innerText = result; });
    //       }
    //     </script>

    //     <body>
    //       <h1>CHOC WebView Demo</h1>
    //       <p>This is a demo of a choc::webview::WebView window</p>
    //       <p><button onclick="sendEvent()">Click to invoke an event callback</button></p>
    //       <p><button onclick="loadCHOCWebsite()">Click to visit the CHOC github repo</button></p>
    //       <p id="eventResultDisplay"></p>
    //     </body>
    //   </html>
    // )xxx");

    window.toFront();
    choc::messageloop::run();
    return 0;
}

I'm trying various other js libraries and they seem to work fine. It's very odd..

LucasThompson added a commit that referenced this issue Mar 30, 2023
* quick workaround to resolve #8
* for some reason the enviornment completed handler is being called
  multiple times, far later than actual creation, needs more
  investigation, but this prevents crashing
LucasThompson added a commit that referenced this issue Mar 30, 2023
* our ICoreWebView2 declaration for `add_PermissionRequested` didn't
  have the actual type as the first param (we've used void* in many
  places to avoid copying other bits of the webview header we don't
  need), and as we actually call that method with an `EventHandler`
  instance, the wrong method ended up being called when a page requests
  a permission (the first virtual method gets called instead, e.g the
  enviornment created handler)
  - we could instead just remove the permissions handler, as its clearly
    never been called before, but we might end up putting more logic
    there, so will keep it setup for now
* this is the actual fix for #8
@julianstorer
Copy link
Collaborator

Pretty sure we've fixed this, so going to close the issue - thanks for reporting, and let us know if you spot any other oddities!

@rorywalsh
Copy link
Author

Nice one. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants