Multiple browser programs, same cookie path: allowed?

Having problems with building or using the JCEF Java binding? Ask your questions here.

Multiple browser programs, same cookie path: allowed?

Postby JohnnyTheHun » Sun Oct 14, 2018 3:48 am

If I have multiple programs
all with an embedded jcef browser showing the same url
AND all programs set the request to use the same cookie path
can this cause some kind of cookie file corruption or other problems? Or is it an allowed scenario?

And am I right to assume that if i do not persist session cookies then all of the above browser instances will have their own session cookies (and sessions)?
JohnnyTheHun
Techie
 
Posts: 42
Joined: Tue Apr 10, 2018 12:24 pm

Re: Multiple browser programs, same cookie path: allowed?

Postby magreenblatt » Sun Oct 14, 2018 7:37 am

Cookie paths cannot be shared across multiple executable instances. You can instead create multiple browsers using the same executable instance, and those can share cookies. Each cookie manager instance will have separate seasion cookies that are not persisted to disk by default.
magreenblatt
Site Admin
 
Posts: 12379
Joined: Fri May 29, 2009 6:57 pm

Re: Multiple browser programs, same cookie path: allowed?

Postby JohnnyTheHun » Thu Aug 22, 2019 4:49 pm

Thanks, I have a quick question about this:
1. So If I use separate cookie managers created with the same cookie path (e.g. c:\...\cookies) for each browser it won't cause a problem?
E.g.
Code: Select all
cookieManager1 = CefCookieManager.createManager("c:/.../cookies", false);
cookieManager2 = CefCookieManager.createManager("c:/.../cookies", false);

browser1 = client_.createBrowser(url, false, false,CefRequestContext.createContext(new CefRequestContextHandlerAdapter() {
 public CefCookieManager getCookieManager() { return cookieManager1 };
});
browser2 = client_.createBrowser(url, false, false,CefRequestContext.createContext(new CefRequestContextHandlerAdapter() {
 public CefCookieManager getCookieManager() { return cookieManager2 };
});



2. I want to create multiple Windows(JFrame) with browsers showing different urls. Would you recommend
- creating 1 CefClient and using that to create multiple browsers
- OR create a new CefClient for each browser instance?
E.g. the detailed example creates a new CefClient if you select the "New Window" in the menubar.

Thank you!
JohnnyTheHun
Techie
 
Posts: 42
Joined: Tue Apr 10, 2018 12:24 pm

Re: Multiple browser programs, same cookie path: allowed?

Postby JohnnyTheHun » Sun Sep 27, 2020 3:55 pm

magreenblatt wrote:Cookie paths cannot be shared across multiple executable instances. You can instead create multiple browsers using the same executable instance, and those can share cookies. Each cookie manager instance will have separate seasion cookies that are not persisted to disk by default.


Cookie handling has changed drastically not long ago and I am unsure on how to achieve separate browser windows with separate session cookies. Beforehand it was easy: I just had to create my own cookiemanager via cookieManager = CefCookieManager.createManager(cookiePath.getAbsolutePath(), false);
and implement CefRequestContextHandler which gave back my cookiemanager.

Now the CefRequestContextHandler interface doesn't have the function to return a custom cookiemanager anymore, instead it has a complicated getResourceRequestHandler​.

Can you help me in pointing me to the right direction? How can I have multiple browsers with separate cookies (at least separate session cookies that are not persisted)?

Thank you!
JohnnyTheHun
Techie
 
Posts: 42
Joined: Tue Apr 10, 2018 12:24 pm

Re: Multiple browser programs, same cookie path: allowed?

Postby magreenblatt » Sun Sep 27, 2020 6:18 pm

Use a separate CefRequestContext for each browser. It’s an argument passed to CreateBrowser.
magreenblatt
Site Admin
 
Posts: 12379
Joined: Fri May 29, 2009 6:57 pm

Re: Multiple browser programs, same cookie path: allowed?

Postby JohnnyTheHun » Mon Sep 28, 2020 1:04 am

magreenblatt wrote:Use a separate CefRequestContext for each browser. It’s an argument passed to CreateBrowser.


I understand, but how do I create a separate CefRequestContext? From what I see CefRequestContext.createContext​(CefRequestContextHandler handler) is the only way. But then I need to implement CefRequestContextHandler to pass as an argument. And I also need to implement CefResourceRequestHandler because I have to return something in CefRequestContextHandler.getResourceRequestHandler (I tried returning null, but the cookies didn't get persisted then). And also CefResourceHandler because of CefResourceRequestHandler.getResourceHandler.

I looked at the XYHandlerAdapter classes but they have empty functions so I'd need to implement a good few functions which I do not yet know what they do.

Or am I missing something?

Thank you!
JohnnyTheHun
Techie
 
Posts: 42
Joined: Tue Apr 10, 2018 12:24 pm

Re: Multiple browser programs, same cookie path: allowed?

Postby ndesktop » Tue Sep 29, 2020 10:16 am

Look in cefclient sample for RootWindowManager::GetRequestContext.
You will need to implement CefRequestContextHandlerinto a class (passed to CefRequestContext::CreateContext).

My implementation is using a method CreateBrowser when creating a browser:
Code: Select all
    return CefBrowserHost::CreateBrowser(
        info,
        static_cast<CefRefPtr<CefClient>>(m_Handler),
        url,
        settings,
   NULL,
        GetRequestContext(m_Handler.get())
    );

which delegates into my client handler:
Code: Select all
CefRefPtr<CefRequestContext> Container::GetRequestContext(
    ClientHandler* client_handler)
{
    DCHECK(CefCurrentlyOn(TID_UI));
    return client_handler->GetRequestContext(client_handler);
}


then you can choose if all browsers are using a shared context ...
Code: Select all
CefRefPtr<CefRequestContext> ClientHandler::GetRequestContext(
    ClientHandler* client_handler)
{
...
    //  all browsers are sharing the same request context
    winapi::sync::critical_section_lock_t lock(&_csSharedRequestContext);
    if(m_SharedRequestContext.get() == nullptr)
    {
        m_SharedRequestContext =
            CefRequestContext::CreateContext(
                CefRequestContext::GetGlobalContext(),
                new ClientRequestContextHandler(this)
            );
    }
    DCHECK(m_SharedRequestContext.get());
    return m_SharedRequestContext;
}


... or create a new one:
Code: Select all
CefRefPtr<CefRequestContext> ClientHandler::GetRequestContext(
    ClientHandler* client_handler)
{
...
    //  each browser has its own request context
    return CefRequestContext::CreateContext(
        CefRequestContext::GetGlobalContext(),
        new ClientRequestContextHandler(this)
    );
}


ClientRequestContextHandler example in the same file.
ndesktop
Master
 
Posts: 748
Joined: Thu Dec 03, 2015 10:10 am

Re: Multiple browser programs, same cookie path: allowed?

Postby JohnnyTheHun » Wed Oct 14, 2020 5:14 am

Thank you for your post but I am a Java programmer and have little knowledge of C++. What I see is that CefRequestContext has a method to return the CookieManager but the java bindings are missing. Is there someone who had the same problem and created the JNI code?
JohnnyTheHun
Techie
 
Posts: 42
Joined: Tue Apr 10, 2018 12:24 pm


Return to JCEF Forum

Who is online

Users browsing this forum: No registered users and 4 guests