Need JNI help implementing CefRequestContext.getCookieM....

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

Need JNI help implementing CefRequestContext.getCookieM....

Postby JohnnyTheHun » Thu Jan 07, 2021 5:35 am

I am not a c programmer and never used JNI. I have run into an error which I think should be easy to solve for someone who has already used JNI, please help.

I need to be able to get the cookie manager for a specific context so I can read and set cookies for that context.
I have noticed there is a GetCookieManager function in CEF:
Code: Select all
public virtual CefRefPtr< CefCookieManager > GetCookieManager( CefRefPtr< CefCompletionCallback > callback )= 0;

https://magpcss.org/ceforum/apidocs3/projects/(default)/CefRequestContext.html#GetCookieManager(CefRefPtr%3CCefCompletionCallback%3E)
So I tried implementing it copying bits of code from here and there:

CefRequestContext_N.java:
Code: Select all
...
import org.cef.network.CefCookieManager;
...
@Override
    public CefCookieManager getCookieManager() {
        try {
            return N_GetCookieManager();
        } catch (UnsatisfiedLinkError ule) {
            ule.printStackTrace();
        }
        return null;
    }
...
private final native CefCookieManager N_GetCookieManager();
...


CefRequestContext.java:
Code: Select all
...
import org.cef.network.CefCookieManager;
...
public abstract CefCookieManager getCookieManager();
...


after this I generated headers with make_jni_header.bat win64 org.cef.browser.CefRequestContext_N

CefRequestContext_N.cpp:
Code: Select all
...
#include "include/cef_cookie.h"
...
JNIEXPORT jobject JNICALL Java_org_cef_browser_CefRequestContext_1N_N_1GetCookieManager
  (JNIEnv* env, jobject obj) {
   
  CefRefPtr<CefRequestContext> context =
      GetCefFromJNIObject<CefRequestContext>(env, obj, "CefRequestContext");
  CefRefPtr<CefCookieManager> manager =
      context -> GetCookieManager(NULL);
  if (!manager)
    return NULL;

   //Which to use?
  //ScopedJNIObjectLocal jManager(env, NewJNIObject(env, "CefCookieManager"));
  jobject jManager = NewJNIObject(env, "CefCookieManager");
  if (!jManager)
    return NULL;

  SetCefForJNIObject(env, jManager, manager.get(), "CefCookieManager");
  return jManager;
  }
...


After this I built it using vs2017, then compile.bat win64 and make_distrib.bat

The error:
When I call this in my java app:
Code: Select all
CefCookieManager cm = requestContext.getCookieManager();


I get the following error:
Code: Select all
java.lang.ClassNotFoundException: CefCookieManager
   at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
   at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
   at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:355)
   at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
   at org.cef.browser.CefRequestContext_N.N_GetCookieManager(Native Method)
   at org.cef.browser.CefRequestContext_N.getCookieManager(CefRequestContext_N.java:30)


Where have I missed an import?
JohnnyTheHun
Techie
 
Posts: 42
Joined: Tue Apr 10, 2018 12:24 pm

Re: Need JNI help implementing CefRequestContext.getCookieM.

Postby JohnnyTheHun » Thu Jan 07, 2021 11:08 am

I seem to have been able to solve the problem. But before I try to figure out how to add a pull request or whatever it's called it would be nice if somebody took a look and said what a wonderful solution it is or that it'll break in a few minutes:

I changed a few things in CefRequestContext_N.cpp in my GetCookieManager implementation:


Code: Select all
JNIEXPORT jobject JNICALL Java_org_cef_browser_CefRequestContext_1N_N_1GetCookieManager
  (JNIEnv* env, jobject obj) {
   
  CefRefPtr<CefRequestContext> context =
      GetCefFromJNIObject<CefRequestContext>(env, obj, "CefRequestContext");
  CefRefPtr<CefCookieManager> manager =
      context -> GetCookieManager(NULL);
  if (!manager)
    return NULL;

  //Which to use?
  //ScopedJNIObjectLocal jManager(env, NewJNIObject(env, "CefCookieManager"));
  //jobject jManager = NewJNIObject(env, "CefCookieManager");
  jclass cefCMClass = env->FindClass("org/cef/network/CefCookieManager_N");   
  ScopedJNIObjectLocal jManager(env, NewJNIObject(env, cefCMClass));
 
  if (!jManager)
    return NULL;

  SetCefForJNIObject(env, jManager, manager.get(), "CefCookieManager");
  //return jManager;
  return jManager.Release();
}
JohnnyTheHun
Techie
 
Posts: 42
Joined: Tue Apr 10, 2018 12:24 pm

Re: Need JNI help implementing CefRequestContext.getCookieM.

Postby magreenblatt » Thu Jan 07, 2021 1:50 pm

You can add a ScopedJNICookieManager class (similar to ScopedJNIPostData, etc, in jni_scoped_helpers.[h|cpp]) and then use:
Code: Select all
ScopedJNICookieManager cookieManager(env, manager);
return cookieManager.Release();
magreenblatt
Site Admin
 
Posts: 10937
Joined: Fri May 29, 2009 6:57 pm

Re: Need JNI help implementing CefRequestContext.getCookieM.

Postby JohnnyTheHun » Sat Jan 09, 2021 1:24 pm

I did it and it works, I'll figure out how to do a PR.
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 1 guest