Skip to content

Commit 1695c32

Browse files
S1artiemagreenblatt
authored andcommitted
Added CefRequestContext preferences get/set API (fixes chromiumembedded#306)
1 parent abb2bd0 commit 1695c32

File tree

8 files changed

+587
-2
lines changed

8 files changed

+587
-2
lines changed

java/org/cef/browser/CefBrowser.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ public interface CefBrowser {
4949
*/
5050
public CefRenderHandler getRenderHandler();
5151

52+
/**
53+
* Retrieves the request context used by this browser instance. May be the
54+
* global request context if this browser does not have a specific request
55+
* context.
56+
*/
57+
public CefRequestContext getRequestContext();
58+
5259
/**
5360
* Get an implementation of CefWindowHandler if any.
5461
* @return An instance of CefWindowHandler or null.

java/org/cef/browser/CefBrowser_N.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,9 @@ protected String getUrl() {
6868
return url_;
6969
}
7070

71-
protected CefRequestContext getRequestContext() {
72-
return request_context_;
71+
@Override
72+
public CefRequestContext getRequestContext() {
73+
return request_context_ != null ? request_context_ : CefRequestContext.getGlobalContext();
7374
}
7475

7576
protected CefBrowser_N getParentBrowser() {

java/org/cef/browser/CefRequestContext.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
import org.cef.handler.CefRequestContextHandler;
88

9+
import java.util.HashMap;
10+
import java.util.Map;
11+
912
/**
1013
* A request context provides request handling for a set of related browser
1114
* objects. A request context is specified when creating a new browser object
@@ -50,4 +53,48 @@ public static final CefRequestContext createContext(CefRequestContextHandler han
5053
* Returns the handler for this context if any.
5154
*/
5255
public abstract CefRequestContextHandler getHandler();
56+
57+
/**
58+
* Returns true if a preference with the specified |name| exists.
59+
* <p>
60+
* This method must be called on the browser process UI thread, otherwise it will always return
61+
* false. It is easiest to ensure the correct calling thread by using a callback method invoked
62+
* by the browser process UI thread, such as CefLifeSpanHandler.onAfterCreated(CefBrowser), to
63+
* configure the preferences.
64+
*/
65+
public abstract boolean hasPreference(String name);
66+
67+
/**
68+
* Returns the value for the preference with the specified |name|. Returns
69+
* NULL if the preference does not exist.
70+
* This method must be called on the browser process UI thread, otherwise it will always return
71+
* null.
72+
*/
73+
public abstract Object getPreference(String name);
74+
75+
/**
76+
* Returns all preferences as a dictionary. If |includeDefaults| is true then
77+
* preferences currently at their default value will be included. The returned
78+
* object can be modified but modifications will not persist. This method must
79+
* be called on the browser process UI thread, otherwise it will always return null.
80+
*/
81+
public abstract Map<String, Object> getAllPreferences(boolean includeDefaults);
82+
83+
/**
84+
* Returns true if the preference with the specified |name| can be modified
85+
* using setPreference. As one example preferences set via the command-line
86+
* usually cannot be modified. This method must be called on the browser
87+
* process UI thread, otherwise it will always return false.
88+
*/
89+
public abstract boolean canSetPreference(String name);
90+
91+
/**
92+
* Set the |value| associated with preference |name|. Returns null if the
93+
* value is set successfully, an error string otherwise. If |value| is NULL the
94+
* preference will be restored to its default value. If setting the preference
95+
* fails then a detailed description of the problem will be returned.
96+
* This method must be called on the browser process UI thread, otherwise it will always return
97+
* an error string.
98+
*/
99+
public abstract String setPreference(String name, Object value);
53100
}

java/org/cef/browser/CefRequestContext_N.java

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77
import org.cef.callback.CefNative;
88
import org.cef.handler.CefRequestContextHandler;
9+
import org.cef.misc.StringRef;
10+
11+
import java.util.HashMap;
12+
import java.util.Map;
913

1014
class CefRequestContext_N extends CefRequestContext implements CefNative {
1115
// Used internally to store a pointer to the CEF object.
@@ -73,6 +77,56 @@ public boolean isGlobal() {
7377
return false;
7478
}
7579

80+
@Override
81+
public boolean hasPreference(String name) {
82+
try {
83+
return N_HasPreference(name);
84+
} catch (UnsatisfiedLinkError ule) {
85+
ule.printStackTrace();
86+
}
87+
return false;
88+
}
89+
90+
@Override
91+
public Object getPreference(String name) {
92+
try {
93+
return N_GetPreference(name);
94+
} catch (UnsatisfiedLinkError ule) {
95+
ule.printStackTrace();
96+
}
97+
return null;
98+
}
99+
100+
@Override
101+
public Map<String, Object> getAllPreferences(boolean includeDefaults) {
102+
try {
103+
return N_GetAllPreferences(includeDefaults);
104+
} catch (UnsatisfiedLinkError ule) {
105+
ule.printStackTrace();
106+
}
107+
return new HashMap<String, Object>();
108+
}
109+
110+
@Override
111+
public boolean canSetPreference(String name) {
112+
try {
113+
return N_CanSetPreference(name);
114+
} catch (UnsatisfiedLinkError ule) {
115+
ule.printStackTrace();
116+
}
117+
return false;
118+
}
119+
120+
@Override
121+
public String setPreference(String name, Object value) {
122+
try {
123+
return N_SetPreference(name, value);
124+
} catch (UnsatisfiedLinkError ule) {
125+
ule.printStackTrace();
126+
return ule.getMessage();
127+
}
128+
}
129+
76130
@Override
77131
public CefRequestContextHandler getHandler() {
78132
return handler;
@@ -82,5 +136,10 @@ public CefRequestContextHandler getHandler() {
82136
private final static native CefRequestContext_N N_CreateContext(
83137
CefRequestContextHandler handler);
84138
private final native boolean N_IsGlobal();
139+
private final native boolean N_HasPreference(String name);
140+
private final native Object N_GetPreference(String name);
141+
private final native Map<String, Object> N_GetAllPreferences(boolean includeDefaults);
142+
private final native boolean N_CanSetPreference(String name);
143+
private final native String N_SetPreference(String name, Object value);
85144
private final native void N_CefRequestContext_DTOR();
86145
}

native/CefRequestContext_N.cpp

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,104 @@ Java_org_cef_browser_CefRequestContext_1N_N_1IsGlobal(JNIEnv* env,
5656
return context->IsGlobal() ? JNI_TRUE : JNI_FALSE;
5757
}
5858

59+
JNIEXPORT jboolean JNICALL
60+
Java_org_cef_browser_CefRequestContext_1N_N_1HasPreference(JNIEnv* env,
61+
jobject obj,
62+
jstring jname) {
63+
CefRefPtr<CefRequestContext> context =
64+
GetCefFromJNIObject<CefRequestContext>(env, obj, "CefRequestContext");
65+
if (!context.get())
66+
return JNI_FALSE;
67+
68+
CefString name = GetJNIString(env, jname);
69+
return context->HasPreference(name) ? JNI_TRUE : JNI_FALSE;
70+
}
71+
72+
JNIEXPORT jobject JNICALL
73+
Java_org_cef_browser_CefRequestContext_1N_N_1GetPreference(JNIEnv* env,
74+
jobject obj,
75+
jstring jname) {
76+
CefRefPtr<CefRequestContext> context =
77+
GetCefFromJNIObject<CefRequestContext>(env, obj, "CefRequestContext");
78+
if (!context.get())
79+
return nullptr;
80+
81+
CefString name = GetJNIString(env, jname);
82+
CefRefPtr<CefValue> value = context->GetPreference(name);
83+
if (!value)
84+
return nullptr;
85+
86+
return NewJNIObjectFromCefValue(env, value);
87+
}
88+
89+
JNIEXPORT jobject JNICALL
90+
Java_org_cef_browser_CefRequestContext_1N_N_1GetAllPreferences(
91+
JNIEnv* env,
92+
jobject obj,
93+
jboolean includeDefaults) {
94+
CefRefPtr<CefRequestContext> context =
95+
GetCefFromJNIObject<CefRequestContext>(env, obj, "CefRequestContext");
96+
if (!context.get())
97+
return nullptr;
98+
99+
CefRefPtr<CefDictionaryValue> value =
100+
context->GetAllPreferences(includeDefaults == JNI_TRUE);
101+
if (!value)
102+
return nullptr;
103+
104+
jobject jmap = NewJNIHashMap(env);
105+
CefDictionaryValue::KeyList keys;
106+
value->GetKeys(keys);
107+
for (const CefString& key : keys) {
108+
jstring jkey = NewJNIString(env, key);
109+
jobject jvalue = NewJNIObjectFromCefValue(env, value->GetValue(key));
110+
JNI_CALL_VOID_METHOD(
111+
env, jmap, "put",
112+
"(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;", jkey,
113+
jvalue);
114+
}
115+
return jmap;
116+
}
117+
118+
JNIEXPORT jboolean JNICALL
119+
Java_org_cef_browser_CefRequestContext_1N_N_1CanSetPreference(JNIEnv* env,
120+
jobject obj,
121+
jstring jname) {
122+
CefRefPtr<CefRequestContext> context =
123+
GetCefFromJNIObject<CefRequestContext>(env, obj, "CefRequestContext");
124+
if (!context.get())
125+
return JNI_FALSE;
126+
127+
CefString name = GetJNIString(env, jname);
128+
return context->CanSetPreference(name) ? JNI_TRUE : JNI_FALSE;
129+
}
130+
131+
JNIEXPORT jstring JNICALL
132+
Java_org_cef_browser_CefRequestContext_1N_N_1SetPreference(JNIEnv* env,
133+
jobject obj,
134+
jstring jname,
135+
jobject jvalue) {
136+
if (!CefCurrentlyOn(TID_UI))
137+
return NewJNIString(env, "called on invalid thread");
138+
139+
CefRefPtr<CefRequestContext> context =
140+
GetCefFromJNIObject<CefRequestContext>(env, obj, "CefRequestContext");
141+
if (!context.get())
142+
return NewJNIString(env, "no request context");
143+
144+
CefString name = GetJNIString(env, jname);
145+
CefRefPtr<CefValue> value = GetCefValueFromJNIObject(env, jvalue);
146+
if (!value)
147+
return NewJNIString(env, "no value to set");
148+
149+
CefString error;
150+
bool result = context->SetPreference(name, value, error);
151+
if (!result)
152+
return NewJNIString(env, error);
153+
154+
return nullptr;
155+
}
156+
59157
JNIEXPORT void JNICALL
60158
Java_org_cef_browser_CefRequestContext_1N_N_1CefRequestContext_1DTOR(
61159
JNIEnv* env,

native/CefRequestContext_N.h

Lines changed: 51 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)