Page 1 of 2

How to store cookies in different CefRequestContext

PostPosted: Mon Jan 20, 2020 8:45 pm
by Vilsonei
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?

Re: How to store cookies in different CefRequestContext

PostPosted: Mon Jan 20, 2020 11:53 pm
by magreenblatt
Cookie managers are now associated with a request context, which is specified when creating a browser. They can no longer be changed dynamically.

Re: How to store cookies in different CefRequestContext

PostPosted: Tue Jan 21, 2020 4:59 am
by Vilsonei
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.

Re: How to store cookies in different CefRequestContext

PostPosted: Tue Jan 21, 2020 12:24 pm
by magreenblatt
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.

Re: How to store cookies in different CefRequestContext

PostPosted: Wed Jan 22, 2020 12:51 pm
by Vilsonei
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 ++?

Re: How to store cookies in different CefRequestContext

PostPosted: Wed Jan 22, 2020 1:06 pm
by magreenblatt
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.

Re: How to store cookies in different CefRequestContext

PostPosted: Wed Jan 22, 2020 9:14 pm
by Vilsonei
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;
}

Re: How to store cookies in different CefRequestContext

PostPosted: Thu Jan 23, 2020 11:37 am
by Vilsonei
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;
}


Re: How to store cookies in different CefRequestContext

PostPosted: Thu Jan 23, 2020 1:49 pm
by magreenblatt
A short-hand version of the same thing would be:
Code: Select all
GetString(&settings.cache_path) = GetJNIString(env, jcachePath);

Re: How to store cookies in different CefRequestContext

PostPosted: Sun Feb 02, 2020 3:03 am
by JohnnyTheHun
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!