1

I need to access a silverlight 5 application from a browser embedded in a MonoMac application. For the browser I am using MonoMac.WebKit.WebView When trying to access any silverlight application for instance

webView.MainFrame.LoadRequest(new NSUrlRequest (new NSUrl ("http://samples.msdn.microsoft.com/Silverlight/SampleBrowser/")));

I get the following error http://pastebin.com/s8PDfDyq. I tried the same with the native WebView in XCode and it loads correctly so I believe is the mono bridge that is affecting it.

Is there any other alternative for embedding a browser in a Mono OSX application besides WebView ?

Thanks, Claudio

2 Answers2

5

After lots of web searches and trying different things, I was able to get things working in the latest MonoDevelop. There are actually two separate issues here. The first affects OSX 10.6 and later in my testing (no access to 10.5). The second affects OSX 10.7 and later.

  1. Many silverlight websites will crash in a webView even in OSX 10.6. You can fix the crash by changing the useragent as so

    webBrowser.CustomUserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/537.13+ (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2";
    

    It is hard to believe that this fixed things, but it did, even though the useragent string without this is very similar:

    Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/534.57.2 (KHTML, like Gecko)
    
  2. Even with this fix, the latest MonoDevelop webView + Silverlight on OSX 10.7 or later = crash. Why? It appears that the silverlight plugin has issues with the heap execution security stuff in 10.7 and later. MonoDevelop 2.8.8.4 did not set the flag on the unix executable to prevent heap execution, but the latest MonoDevelop does. From MonoDevelop, I don't know if you can get to a place where you change the parameters passed to ld to do -allow_heap_execute. But you can modify the flag yourself in the unix executable in the appbundle with a hex editor...

    I grabbed HexFiend and then put it in my applications folder. Show the contents of your output app bundle (via right click) and then dig into Contents/MacOS/ . There will be one file there. Open it in HexFiend. You will see bytes like this:

    CEFAEDFE 07000000 03000000 02000000 13000000 3C080000 85002001
    

    See that last 1? That is the no heap execution flag. Turn it off by changing the 1 to a 0. This got my webView that shows pages with Silverlight to run without a hitch on OSX 10.8

    I ended up writing a little mono console app that I call as a post build step to do this for me. It is invoked as

    mono "<path-to-console-app-exe>" "<path-to-mac.app-package>"
    

    Here is the main function:

    public static void Main (string[] args)
    {
        if (args.Length > 0)
        {
            if (Directory.Exists(args[0]))
            {
                string containingFolder = Path.Combine (Path.Combine(args[0], "Contents"), "MacOS");
                var files = Directory.GetFiles (containingFolder);
                if (files.Length == 1)
                {
                    var bytes = File.ReadAllBytes(files[0]);
                    byte one = (byte)1;
                    byte allButOne = (byte)~one;
                    bytes[27] = (byte)(bytes[27] & allButOne); 
                    File.WriteAllBytes(files[0], bytes);
                    return;
                }
            }
        }
        throw new InvalidOperationException("Failed to allow heap execution");
    }
    

For what it's worth, Silverlight on Mac seems to be a very brittle proposition. I don't really have a short-term choice about it, and you may not either, but figuring this out led me through all kinds of search results of people having various problems with Silverlight and updated versions of OSX.

Here are the links that helped me along to my answers:

Firefox crash with silverlight and same stack where on fix considered was turning off the heap protection stuff

Linked firefox crash where problem was resolved by running silverlight plugin out of process where heap execution is allowed

Apple resource on the flags in the header

Chromium bug that actually lists what flag is the heap execute one - not in apple's docs - awesome!

Bug 1 filed on Connect about the crash fixed by changing the user agent string

Bug 2 filed on Connect about the crash fixed by changing the user agent string

aggieNick02
  • 2,557
  • 2
  • 23
  • 36
  • Any news of improvements on this? – Magnus Ahlin Mar 24 '14 at 10:48
  • No, sorry. I don't think Microsoft is likely to change anything about Silverlight unless they really have to (e.g., Safari update breaks it completely). Having said that, it has been over a year since we had to deal with this, so things could have changed. In addition, MonoMac has been superceded by Xamarin's Mac offering, so they might have better support for setting the heap execute flag, but I've not used it so I don't know. – aggieNick02 Mar 25 '14 at 14:29
0

To try to help... we are running into the exact same issue right now.

Particularly interesting is that we had this working in an older version of MonoDevelop+MonoMac (2.8.8.4). We upgraded to the latest MonoDevelop recently to rectify some other issues and now see this issue.

Also interesting is that the crash appears to only happen on OSX 10.7 and later. Our app runs fine on OSX 10.6 machines...

In researching the crash stack, I came across a few interesting things that caught my attention but so far I've gotten nowhere with them. One was

https://bugzilla.mozilla.org/show_bug.cgi?id=753248

where Silverlight was crashing firefox but only on OSX 10.7 and later, with a very similar stack.

aggieNick02
  • 2,557
  • 2
  • 23
  • 36