How to store cookies in different CefRequestContext

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

How to store cookies in different CefRequestContext

Postby Vilsonei » Mon Jan 20, 2020 8:45 pm

In my application I need to open several browsers in different contexts but store cookies for each browser separately.

When closing the system and opening it again, I would like each browser to load its cookies and resume sessions automatically.

In the version of java-cef 3.2xxx there was something like this:

cookieManager_ = CefCookieManager.createManager(cookiePath, false);
requestContext = CefRequestContext.createContext( new CefRequestContextHandlerAdapter() {
@Override
public CefCookieManager getCookieManager() {
return cookieManager_;
}
}


In the most current version of java-cef (78.2xxx) I couldn't find a way to get a custom CefCookieManager.

Could anyone help me on this issue?
Vilsonei
Newbie
 
Posts: 5
Joined: Mon Jan 20, 2020 8:32 pm

Re: How to store cookies in different CefRequestContext

Postby magreenblatt » Mon Jan 20, 2020 11:53 pm

Cookie managers are now associated with a request context, which is specified when creating a browser. They can no longer be changed dynamically.
magreenblatt
Site Admin
 
Posts: 9848
Joined: Fri May 29, 2009 6:57 pm

Re: How to store cookies in different CefRequestContext

Postby Vilsonei » Tue Jan 21, 2020 4:59 am

Thanks for the reply magreenblatt!

Is there a way to persist the context to recover it when the application is closed and reopened?

Because I need to link to each context an "ID" so that I can access and load them in the correct browsers.
Vilsonei
Newbie
 
Posts: 5
Joined: Mon Jan 20, 2020 8:32 pm

Re: How to store cookies in different CefRequestContext

Postby magreenblatt » Tue Jan 21, 2020 12:24 pm

The C++ API allows you to specify a cache_path via CefRequestContextSettings when creating a request context. This is not currently exposed by JCEF, but it should be possible to add support to CefRequestContext.createContext.
magreenblatt
Site Admin
 
Posts: 9848
Joined: Fri May 29, 2009 6:57 pm

Re: How to store cookies in different CefRequestContext

Postby Vilsonei » Wed Jan 22, 2020 12:51 pm

magreenblatt what would you suggest to store cookies in contexts created at runtime, create a native CEF implementation or migrate the application to C ++?

I saw that in project issues, request # 88 solves this problem, but it seems that it has been lost in time.

How to open a request in java-cef to create a context-specific CefCookieManager just like in C ++?
Vilsonei
Newbie
 
Posts: 5
Joined: Mon Jan 20, 2020 8:32 pm

Re: How to store cookies in different CefRequestContext

Postby magreenblatt » Wed Jan 22, 2020 1:06 pm

You would need to define a CefRequestContextSettings object in Java, pass it to the CefRequestContext.createContext method and use it to populate a C++ CefRequestContextSettings object. See CefSettings.java implementation and usage for patterns that you can follow.
magreenblatt
Site Admin
 
Posts: 9848
Joined: Fri May 29, 2009 6:57 pm

Re: How to store cookies in different CefRequestContext

Postby Vilsonei » Wed Jan 22, 2020 9:14 pm

I would like to know how do I assign a jstring to a cef_string_t, I am not fluent in C ++, I'm taking a beating on this assignment, see my code:

Code: Select all
JNIEXPORT jobject JNICALL
Java_org_cef_browser_CefRequestContext_1N_N_1CreateContext(JNIEnv* env,
                                                           jclass cls,
                                                           jstring jcachePath,
                                                           jobject jhandler) {
  CefRefPtr<CefRequestContextHandler> handler = NULL;
  if (jhandler != NULL) {
    handler = new RequestContextHandler(env, jhandler);
  }

  CefString cachePath = GetJNIString(env, jcachePath);
  CefRequestContextSettings settings;
  settings.cache_path = cachePath; // I don't know how to do this!

  CefRefPtr<CefRequestContext> context = CefRequestContext::CreateContext(settings, handler);
  if (!context.get())
    return NULL;

  jobject jContext = NewJNIObject(env, cls);
  if (!jContext)
    return NULL;

  SetCefForJNIObject(env, jContext, context.get(), "CefRequestContext");
  return jContext;
}
Vilsonei
Newbie
 
Posts: 5
Joined: Mon Jan 20, 2020 8:32 pm

Re: How to store cookies in different CefRequestContext

Postby Vilsonei » Thu Jan 23, 2020 11:37 am

I solved my question with the following code:

JNIEXPORT jobject JNICALL
Java_org_cef_browser_CefRequestContext_1N_N_1CreateContext(JNIEnv* env,
jclass cls,
jstring jcachePath,
jobject jhandler) {
CefRefPtr<CefRequestContextHandler> handler = NULL;
if (jhandler != NULL) {
handler = new RequestContextHandler(env, jhandler);
}

CefString cachePath = GetJNIString(env, jcachePath);
CefRequestContextSettings settings;
cef_string_copy(cachePath.c_str(), cachePath.length(), &settings.cache_path);

CefRefPtr<CefRequestContext> context = CefRequestContext::CreateContext(settings, handler);
if (!context.get())
return NULL;

jobject jContext = NewJNIObject(env, cls);
if (!jContext)
return NULL;

SetCefForJNIObject(env, jContext, context.get(), "CefRequestContext");
return jContext;
}

Vilsonei
Newbie
 
Posts: 5
Joined: Mon Jan 20, 2020 8:32 pm

Re: How to store cookies in different CefRequestContext

Postby magreenblatt » Thu Jan 23, 2020 1:49 pm

A short-hand version of the same thing would be:
Code: Select all
GetString(&settings.cache_path) = GetJNIString(env, jcachePath);
magreenblatt
Site Admin
 
Posts: 9848
Joined: Fri May 29, 2009 6:57 pm

Re: How to store cookies in different CefRequestContext

Postby JohnnyTheHun » Sun Feb 02, 2020 3:03 am

Could this be added to the source? I too relied on createManager to have a separate Cookie Manager for each Browser I created (so I could use separate PHP sessions / browser window).

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

Next

Return to JCEF Forum

Who is online

Users browsing this forum: No registered users and 1 guest