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

Close tab on window.close() #1084

Closed
udo75 opened this issue Jun 17, 2015 · 6 comments
Closed

Close tab on window.close() #1084

udo75 opened this issue Jun 17, 2015 · 6 comments

Comments

@udo75
Copy link

udo75 commented Jun 17, 2015

Hi all,

I have one question concerning handling of window.close() calls from the page hosted in CefSharp browser control.
We have built a tabbed window where we host a CefSharp browser in each tab. We have now the situation that we have web applications hosted in the tabs where the user can trigger a close action in the hosted page which basically calls a window.close() javascript code. In this case we would like to get notified in our C# application (so basically the tab hosting the CefSharp browser) so that we can also close the tab.
Is there a way of doing this with CefSharp?

Thank you in advance and best regards,
Uzay

@amaitland
Copy link
Member

You can implement ILifeSpanHandler.OnBeforeClose or use Javascript binding as suggested in #439

@udo75
Copy link
Author

udo75 commented Jun 17, 2015

Hi Alex,

thank you for your quick response. I tried to follow the OnBeforeClose approach as the #439 will not work for me. The hosted web applications are not in our hand to be changed.

I tried following test application: Having a Window which hosts the CefSharp browser and which also implements ILifeSpanHandler. When triggering the closeWindow() javascript I saw once that the browser was closed but the handler OnBeforeClose was not hit. It seems general an issue to get window.close() working properly in Chrome. But when it closes I don't see the method OnBeforeClose() being called. Did I missed something here?

Cheers,
Uzay

public partial class MainWindow : Window, ILifeSpanHandler
    {
        public const string TestResourceUrl = "http://test/resource/load";

        public MainWindow()
        {
            InitializeComponent();
            Cef.Initialize(new CefSettings());
            //browser.Address = "http://www.google.de";
            var handler = browser.ResourceHandler;
            const string responseBody = "<html><head><script type='text/javascript'> function closeWindow(){window.open('', '_self', '');window.close();}</script></head><body><h1>Success</h1><p>This document is loaded from a System.IO.Stream</p><button onclick='closeWindow()'>Close window</button></body></html>";
            handler.RegisterHandler(TestResourceUrl, ResourceHandler.FromString(responseBody));

            browser.Address = TestResourceUrl;
        }

        public void OnBeforeClose(IWebBrowser browser)
        {
            throw new NotImplementedException();
        }

        public bool OnBeforePopup(IWebBrowser browser, string url, ref int x, ref int y, ref int width, ref int height)
        {
            throw new NotImplementedException();
        }
    }

@amaitland
Copy link
Member

It's not enough to implement ILifeSpanHandler, you also have to assign it.
https://github.com/cefsharp/CefSharp/blob/cefsharp/41/CefSharp/IWebBrowser.cs#L121

Based on your code something like

browser.LifeSpanHandler = this;

An example of setting the MenuHandler, same principal applies.
https://github.com/cefsharp/CefSharp/blob/cefsharp/41/CefSharp.Wpf.Example/Views/BrowserTabView.xaml.cs#L20

Also your call to Cef.Initialize() is redundant where it is. The browser instance will already to initialized Cef for you. If you don't need custom settings then it's safe to remove, otherwise I'd suggest you move it to your App.xaml

(It's also helpful if you include flavor, e.g. WPF or WinForms and the version your using when you open any issue).

@amaitland
Copy link
Member

The hosted web applications are not in our hand to be changed.

Also, as they're javascript and html you can pretty much inject anything you like, so you could hook onbeforeunload by injecting some js or maybe even override the window.close() method it's self (though I haven't tried).

@udo75
Copy link
Author

udo75 commented Jun 17, 2015

Hi Alex,

thank you very much for your valuable hints. After assigning the browser.LifeSpanHandler and trying a lot to get window.close() also somehow working for the browser (it is more a hack) I finally made it working thanks to your help. So the OnBeforeClose is now called properly in my test scenario.

Sorry for the poor information about the version and the flavor. I'm using CefSharp 39 pre03 for my tests. We are working in the repository code with stable version 39 . The UI is WPF based.

About the java script injection. You are right, this is also a possibility to get the things working. But my first intension was to get it run with CefSharp mechanism and without altering the hosted pages.

Thank you again for you great support!
Uzay

@amaitland
Copy link
Member

I finally made it working thanks to your help. So the OnBeforeClose is now called properly in my test scenario.

Great 👍

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