mirror of
https://bitbucket.org/chromiumembedded/cef
synced 2025-06-05 21:39:12 +02:00
cefclient: Add global option to https://tests/preferences (see #3892)
This commit is contained in:
@@ -30,6 +30,7 @@ const char kNameValueSet[] = "preferences_set";
|
|||||||
const char kNameValueState[] = "preferences_state";
|
const char kNameValueState[] = "preferences_state";
|
||||||
|
|
||||||
// Used with "preferences_get" messages.
|
// Used with "preferences_get" messages.
|
||||||
|
const char kGlobalPrefsKey[] = "global_prefs";
|
||||||
const char kIncludeDefaultsKey[] = "include_defaults";
|
const char kIncludeDefaultsKey[] = "include_defaults";
|
||||||
|
|
||||||
// Used with "preferences_set" messages.
|
// Used with "preferences_set" messages.
|
||||||
@@ -73,30 +74,34 @@ class Handler : public CefMessageRouterBrowserSide::Handler {
|
|||||||
if (message_name == kNameValueGet) {
|
if (message_name == kNameValueGet) {
|
||||||
// JavaScript is requesting a JSON representation of the preferences tree.
|
// JavaScript is requesting a JSON representation of the preferences tree.
|
||||||
|
|
||||||
// Verify the "include_defaults" key.
|
// Verify the "global_prefs" and "include_defaults" keys.
|
||||||
if (!VerifyKey(request_dict, kIncludeDefaultsKey, VTYPE_BOOL, callback)) {
|
if (!VerifyKey(request_dict, kGlobalPrefsKey, VTYPE_BOOL, callback) ||
|
||||||
|
!VerifyKey(request_dict, kIncludeDefaultsKey, VTYPE_BOOL, callback)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const bool global_prefs = request_dict->GetBool(kGlobalPrefsKey);
|
||||||
const bool include_defaults = request_dict->GetBool(kIncludeDefaultsKey);
|
const bool include_defaults = request_dict->GetBool(kIncludeDefaultsKey);
|
||||||
|
|
||||||
OnPreferencesGet(browser, include_defaults, callback);
|
OnPreferencesGet(browser, global_prefs, include_defaults, callback);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
} else if (message_name == kNameValueSet) {
|
} else if (message_name == kNameValueSet) {
|
||||||
// JavaScript is requesting that preferences be updated to match the
|
// JavaScript is requesting that preferences be updated to match the
|
||||||
// specified JSON representation.
|
// specified JSON representation.
|
||||||
|
|
||||||
// Verify the "preferences" key.
|
// Verify the "global_prefs" and "preferences" keys.
|
||||||
if (!VerifyKey(request_dict, kPreferencesKey, VTYPE_DICTIONARY,
|
if (!VerifyKey(request_dict, kGlobalPrefsKey, VTYPE_BOOL, callback) ||
|
||||||
|
!VerifyKey(request_dict, kPreferencesKey, VTYPE_DICTIONARY,
|
||||||
callback)) {
|
callback)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const bool global_prefs = request_dict->GetBool(kGlobalPrefsKey);
|
||||||
CefRefPtr<CefDictionaryValue> preferences =
|
CefRefPtr<CefDictionaryValue> preferences =
|
||||||
request_dict->GetDictionary(kPreferencesKey);
|
request_dict->GetDictionary(kPreferencesKey);
|
||||||
|
|
||||||
OnPreferencesSet(browser, preferences, callback);
|
OnPreferencesSet(browser, global_prefs, preferences, callback);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
} else if (message_name == kNameValueState) {
|
} else if (message_name == kNameValueState) {
|
||||||
@@ -113,14 +118,19 @@ class Handler : public CefMessageRouterBrowserSide::Handler {
|
|||||||
private:
|
private:
|
||||||
// Execute |callback| with the preferences dictionary as a JSON string.
|
// Execute |callback| with the preferences dictionary as a JSON string.
|
||||||
static void OnPreferencesGet(CefRefPtr<CefBrowser> browser,
|
static void OnPreferencesGet(CefRefPtr<CefBrowser> browser,
|
||||||
|
bool global_prefs,
|
||||||
bool include_defaults,
|
bool include_defaults,
|
||||||
CefRefPtr<Callback> callback) {
|
CefRefPtr<Callback> callback) {
|
||||||
CefRefPtr<CefRequestContext> context =
|
CefRefPtr<CefPreferenceManager> pref_manager;
|
||||||
browser->GetHost()->GetRequestContext();
|
if (global_prefs) {
|
||||||
|
pref_manager = CefPreferenceManager::GetGlobalPreferenceManager();
|
||||||
|
} else {
|
||||||
|
pref_manager = browser->GetHost()->GetRequestContext();
|
||||||
|
}
|
||||||
|
|
||||||
// Retrieve all preference values.
|
// Retrieve all preference values.
|
||||||
CefRefPtr<CefDictionaryValue> prefs =
|
CefRefPtr<CefDictionaryValue> prefs =
|
||||||
context->GetAllPreferences(include_defaults);
|
pref_manager->GetAllPreferences(include_defaults);
|
||||||
|
|
||||||
// Serialize the preferences to JSON and return to the JavaScript caller.
|
// Serialize the preferences to JSON and return to the JavaScript caller.
|
||||||
callback->Success(GetJSON(prefs));
|
callback->Success(GetJSON(prefs));
|
||||||
@@ -129,10 +139,15 @@ class Handler : public CefMessageRouterBrowserSide::Handler {
|
|||||||
// Set preferences based on the contents of |preferences|. Execute |callback|
|
// Set preferences based on the contents of |preferences|. Execute |callback|
|
||||||
// with a descriptive result message.
|
// with a descriptive result message.
|
||||||
static void OnPreferencesSet(CefRefPtr<CefBrowser> browser,
|
static void OnPreferencesSet(CefRefPtr<CefBrowser> browser,
|
||||||
|
bool global_prefs,
|
||||||
CefRefPtr<CefDictionaryValue> preferences,
|
CefRefPtr<CefDictionaryValue> preferences,
|
||||||
CefRefPtr<Callback> callback) {
|
CefRefPtr<Callback> callback) {
|
||||||
CefRefPtr<CefRequestContext> context =
|
CefRefPtr<CefPreferenceManager> pref_manager;
|
||||||
browser->GetHost()->GetRequestContext();
|
if (global_prefs) {
|
||||||
|
pref_manager = CefPreferenceManager::GetGlobalPreferenceManager();
|
||||||
|
} else {
|
||||||
|
pref_manager = browser->GetHost()->GetRequestContext();
|
||||||
|
}
|
||||||
|
|
||||||
CefRefPtr<CefValue> value = CefValue::Create();
|
CefRefPtr<CefValue> value = CefValue::Create();
|
||||||
value->SetDictionary(preferences);
|
value->SetDictionary(preferences);
|
||||||
@@ -142,7 +157,7 @@ class Handler : public CefMessageRouterBrowserSide::Handler {
|
|||||||
|
|
||||||
// Apply preferences. This may result in errors.
|
// Apply preferences. This may result in errors.
|
||||||
const bool success =
|
const bool success =
|
||||||
ApplyPrefs(context, std::string(), value, error, changed_names);
|
ApplyPrefs(pref_manager, std::string(), value, error, changed_names);
|
||||||
|
|
||||||
// Create a message that accurately represents the result.
|
// Create a message that accurately represents the result.
|
||||||
std::string message;
|
std::string message;
|
||||||
@@ -245,14 +260,14 @@ class Handler : public CefMessageRouterBrowserSide::Handler {
|
|||||||
// Apply preferences. Returns true on success. Returns false and sets |error|
|
// Apply preferences. Returns true on success. Returns false and sets |error|
|
||||||
// to a descriptive error string on failure. |changed_names| is the list of
|
// to a descriptive error string on failure. |changed_names| is the list of
|
||||||
// preferences that were successfully changed.
|
// preferences that were successfully changed.
|
||||||
static bool ApplyPrefs(CefRefPtr<CefRequestContext> context,
|
static bool ApplyPrefs(CefRefPtr<CefPreferenceManager> pref_manager,
|
||||||
const std::string& name,
|
const std::string& name,
|
||||||
CefRefPtr<CefValue> value,
|
CefRefPtr<CefValue> value,
|
||||||
std::string& error,
|
std::string& error,
|
||||||
NameVector& changed_names) {
|
NameVector& changed_names) {
|
||||||
if (!name.empty() && context->HasPreference(name)) {
|
if (!name.empty() && pref_manager->HasPreference(name)) {
|
||||||
// The preference exists. Set the value.
|
// The preference exists. Set the value.
|
||||||
return SetPref(context, name, value, error, changed_names);
|
return SetPref(pref_manager, name, value, error, changed_names);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (value->GetType() == VTYPE_DICTIONARY) {
|
if (value->GetType() == VTYPE_DICTIONARY) {
|
||||||
@@ -265,7 +280,7 @@ class Handler : public CefMessageRouterBrowserSide::Handler {
|
|||||||
for (const auto& i : keys) {
|
for (const auto& i : keys) {
|
||||||
const std::string& key = i;
|
const std::string& key = i;
|
||||||
const std::string& current_name = name.empty() ? key : name + "." + key;
|
const std::string& current_name = name.empty() ? key : name + "." + key;
|
||||||
if (!ApplyPrefs(context, current_name, dict->GetValue(key), error,
|
if (!ApplyPrefs(pref_manager, current_name, dict->GetValue(key), error,
|
||||||
changed_names)) {
|
changed_names)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -282,12 +297,12 @@ class Handler : public CefMessageRouterBrowserSide::Handler {
|
|||||||
// successfully or has not changed. If the value has changed then |name| will
|
// successfully or has not changed. If the value has changed then |name| will
|
||||||
// be added to |changed_names|. Returns false and sets |error| to a
|
// be added to |changed_names|. Returns false and sets |error| to a
|
||||||
// descriptive error string on failure.
|
// descriptive error string on failure.
|
||||||
static bool SetPref(CefRefPtr<CefRequestContext> context,
|
static bool SetPref(CefRefPtr<CefPreferenceManager> pref_manager,
|
||||||
const std::string& name,
|
const std::string& name,
|
||||||
CefRefPtr<CefValue> value,
|
CefRefPtr<CefValue> value,
|
||||||
std::string& error,
|
std::string& error,
|
||||||
NameVector& changed_names) {
|
NameVector& changed_names) {
|
||||||
CefRefPtr<CefValue> existing_value = context->GetPreference(name);
|
CefRefPtr<CefValue> existing_value = pref_manager->GetPreference(name);
|
||||||
DCHECK(existing_value);
|
DCHECK(existing_value);
|
||||||
|
|
||||||
if (value->GetType() == VTYPE_STRING &&
|
if (value->GetType() == VTYPE_STRING &&
|
||||||
@@ -322,7 +337,7 @@ class Handler : public CefMessageRouterBrowserSide::Handler {
|
|||||||
|
|
||||||
// Attempt to set the preference.
|
// Attempt to set the preference.
|
||||||
CefString error_str;
|
CefString error_str;
|
||||||
if (!context->SetPreference(name, value, error_str)) {
|
if (!pref_manager->SetPreference(name, value, error_str)) {
|
||||||
error = error_str.ToString() + ": " + name;
|
error = error_str.ToString() + ": " + name;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@@ -96,7 +96,8 @@
|
|||||||
<tr>
|
<tr>
|
||||||
<td align="left">
|
<td align="left">
|
||||||
<input type="button" value="Refresh" onClick="refreshEditor()"/>
|
<input type="button" value="Refresh" onClick="refreshEditor()"/>
|
||||||
<input type="checkbox" id="hide_defaults"/> Show modified preferences only
|
<input type="checkbox" id="global_prefs" onChange="refreshEditor()"/> Show global preferences
|
||||||
|
<input type="checkbox" id="hide_defaults" onChange="refreshEditor()"/> Show modified preferences only
|
||||||
</td>
|
</td>
|
||||||
<td align="right">
|
<td align="right">
|
||||||
<input type="button" value="Apply Changes" onClick="applyEditorChanges()"/>
|
<input type="button" value="Apply Changes" onClick="applyEditorChanges()"/>
|
||||||
@@ -161,10 +162,11 @@
|
|||||||
|
|
||||||
// Get the preferences and execute |onSuccessCallback| with the resulting
|
// Get the preferences and execute |onSuccessCallback| with the resulting
|
||||||
// JSON object.
|
// JSON object.
|
||||||
function getPreferences(include_defaults, onSuccessCallback) {
|
function getPreferences(global_prefs, include_defaults, onSuccessCallback) {
|
||||||
// Create the request object.
|
// Create the request object.
|
||||||
var request = {};
|
var request = {};
|
||||||
request.name = "preferences_get";
|
request.name = "preferences_get";
|
||||||
|
request.global_prefs = global_prefs;
|
||||||
request.include_defaults = include_defaults;
|
request.include_defaults = include_defaults;
|
||||||
|
|
||||||
// Send the request to C++.
|
// Send the request to C++.
|
||||||
@@ -177,10 +179,11 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Set the preferences.
|
// Set the preferences.
|
||||||
function setPreferences(preferences) {
|
function setPreferences(global_prefs, preferences) {
|
||||||
// Create the request object.
|
// Create the request object.
|
||||||
var request = {};
|
var request = {};
|
||||||
request.name = "preferences_set";
|
request.name = "preferences_set";
|
||||||
|
request.global_prefs = global_prefs;
|
||||||
request.preferences = preferences;
|
request.preferences = preferences;
|
||||||
|
|
||||||
// Send the request to C++.
|
// Send the request to C++.
|
||||||
@@ -214,8 +217,9 @@
|
|||||||
|
|
||||||
// Refresh the editor view contents.
|
// Refresh the editor view contents.
|
||||||
function refreshEditor() {
|
function refreshEditor() {
|
||||||
|
global_prefs = document.getElementById("global_prefs").checked;
|
||||||
include_defaults = !document.getElementById("hide_defaults").checked;
|
include_defaults = !document.getElementById("hide_defaults").checked;
|
||||||
getPreferences(include_defaults, function(response) {
|
getPreferences(global_prefs, include_defaults, function(response) {
|
||||||
// Set the JSON in the editor.
|
// Set the JSON in the editor.
|
||||||
editor.set(response);
|
editor.set(response);
|
||||||
});
|
});
|
||||||
@@ -223,12 +227,13 @@
|
|||||||
|
|
||||||
// Apply changes from the editor view.
|
// Apply changes from the editor view.
|
||||||
function applyEditorChanges() {
|
function applyEditorChanges() {
|
||||||
setPreferences(editor.get());
|
global_prefs = document.getElementById("global_prefs").checked;
|
||||||
|
setPreferences(global_prefs, editor.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Refresh the simple view contents.
|
// Refresh the simple view contents.
|
||||||
function refreshSimple() {
|
function refreshSimple() {
|
||||||
getPreferences(true, function(response) {
|
getPreferences(false, true, function(response) {
|
||||||
// Spellcheck settings.
|
// Spellcheck settings.
|
||||||
if (preferences_state.spellcheck_disabled) {
|
if (preferences_state.spellcheck_disabled) {
|
||||||
// Cannot enable spell checking when disabled via the command-line.
|
// Cannot enable spell checking when disabled via the command-line.
|
||||||
@@ -318,7 +323,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (has_preferences)
|
if (has_preferences)
|
||||||
setPreferences(preferences);
|
setPreferences(false, preferences);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Called when the proxy type is changed.
|
// Called when the proxy type is changed.
|
||||||
|
Reference in New Issue
Block a user