diff --git a/tests/cefclient/browser/preferences_test.cc b/tests/cefclient/browser/preferences_test.cc index a2ca88bf9..e57ed223c 100644 --- a/tests/cefclient/browser/preferences_test.cc +++ b/tests/cefclient/browser/preferences_test.cc @@ -30,6 +30,7 @@ const char kNameValueSet[] = "preferences_set"; const char kNameValueState[] = "preferences_state"; // Used with "preferences_get" messages. +const char kGlobalPrefsKey[] = "global_prefs"; const char kIncludeDefaultsKey[] = "include_defaults"; // Used with "preferences_set" messages. @@ -73,30 +74,34 @@ class Handler : public CefMessageRouterBrowserSide::Handler { if (message_name == kNameValueGet) { // JavaScript is requesting a JSON representation of the preferences tree. - // Verify the "include_defaults" key. - if (!VerifyKey(request_dict, kIncludeDefaultsKey, VTYPE_BOOL, callback)) { + // Verify the "global_prefs" and "include_defaults" keys. + if (!VerifyKey(request_dict, kGlobalPrefsKey, VTYPE_BOOL, callback) || + !VerifyKey(request_dict, kIncludeDefaultsKey, VTYPE_BOOL, callback)) { return true; } + const bool global_prefs = request_dict->GetBool(kGlobalPrefsKey); const bool include_defaults = request_dict->GetBool(kIncludeDefaultsKey); - OnPreferencesGet(browser, include_defaults, callback); + OnPreferencesGet(browser, global_prefs, include_defaults, callback); return true; } else if (message_name == kNameValueSet) { // JavaScript is requesting that preferences be updated to match the // specified JSON representation. - // Verify the "preferences" key. - if (!VerifyKey(request_dict, kPreferencesKey, VTYPE_DICTIONARY, + // Verify the "global_prefs" and "preferences" keys. + if (!VerifyKey(request_dict, kGlobalPrefsKey, VTYPE_BOOL, callback) || + !VerifyKey(request_dict, kPreferencesKey, VTYPE_DICTIONARY, callback)) { return true; } + const bool global_prefs = request_dict->GetBool(kGlobalPrefsKey); CefRefPtr preferences = request_dict->GetDictionary(kPreferencesKey); - OnPreferencesSet(browser, preferences, callback); + OnPreferencesSet(browser, global_prefs, preferences, callback); return true; } else if (message_name == kNameValueState) { @@ -113,14 +118,19 @@ class Handler : public CefMessageRouterBrowserSide::Handler { private: // Execute |callback| with the preferences dictionary as a JSON string. static void OnPreferencesGet(CefRefPtr browser, + bool global_prefs, bool include_defaults, CefRefPtr callback) { - CefRefPtr context = - browser->GetHost()->GetRequestContext(); + CefRefPtr pref_manager; + if (global_prefs) { + pref_manager = CefPreferenceManager::GetGlobalPreferenceManager(); + } else { + pref_manager = browser->GetHost()->GetRequestContext(); + } // Retrieve all preference values. CefRefPtr prefs = - context->GetAllPreferences(include_defaults); + pref_manager->GetAllPreferences(include_defaults); // Serialize the preferences to JSON and return to the JavaScript caller. callback->Success(GetJSON(prefs)); @@ -129,10 +139,15 @@ class Handler : public CefMessageRouterBrowserSide::Handler { // Set preferences based on the contents of |preferences|. Execute |callback| // with a descriptive result message. static void OnPreferencesSet(CefRefPtr browser, + bool global_prefs, CefRefPtr preferences, CefRefPtr callback) { - CefRefPtr context = - browser->GetHost()->GetRequestContext(); + CefRefPtr pref_manager; + if (global_prefs) { + pref_manager = CefPreferenceManager::GetGlobalPreferenceManager(); + } else { + pref_manager = browser->GetHost()->GetRequestContext(); + } CefRefPtr value = CefValue::Create(); value->SetDictionary(preferences); @@ -142,7 +157,7 @@ class Handler : public CefMessageRouterBrowserSide::Handler { // Apply preferences. This may result in errors. 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. std::string message; @@ -245,14 +260,14 @@ class Handler : public CefMessageRouterBrowserSide::Handler { // Apply preferences. Returns true on success. Returns false and sets |error| // to a descriptive error string on failure. |changed_names| is the list of // preferences that were successfully changed. - static bool ApplyPrefs(CefRefPtr context, + static bool ApplyPrefs(CefRefPtr pref_manager, const std::string& name, CefRefPtr value, std::string& error, NameVector& changed_names) { - if (!name.empty() && context->HasPreference(name)) { + if (!name.empty() && pref_manager->HasPreference(name)) { // 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) { @@ -265,7 +280,7 @@ class Handler : public CefMessageRouterBrowserSide::Handler { for (const auto& i : keys) { const std::string& key = i; 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)) { return false; } @@ -282,12 +297,12 @@ class Handler : public CefMessageRouterBrowserSide::Handler { // 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 // descriptive error string on failure. - static bool SetPref(CefRefPtr context, + static bool SetPref(CefRefPtr pref_manager, const std::string& name, CefRefPtr value, std::string& error, NameVector& changed_names) { - CefRefPtr existing_value = context->GetPreference(name); + CefRefPtr existing_value = pref_manager->GetPreference(name); DCHECK(existing_value); if (value->GetType() == VTYPE_STRING && @@ -322,7 +337,7 @@ class Handler : public CefMessageRouterBrowserSide::Handler { // Attempt to set the preference. CefString error_str; - if (!context->SetPreference(name, value, error_str)) { + if (!pref_manager->SetPreference(name, value, error_str)) { error = error_str.ToString() + ": " + name; return false; } diff --git a/tests/cefclient/resources/preferences.html b/tests/cefclient/resources/preferences.html index 1062f1eaf..8ebe74690 100644 --- a/tests/cefclient/resources/preferences.html +++ b/tests/cefclient/resources/preferences.html @@ -96,7 +96,8 @@ - Show modified preferences only + Show global preferences + Show modified preferences only @@ -161,10 +162,11 @@ // Get the preferences and execute |onSuccessCallback| with the resulting // JSON object. - function getPreferences(include_defaults, onSuccessCallback) { + function getPreferences(global_prefs, include_defaults, onSuccessCallback) { // Create the request object. var request = {}; request.name = "preferences_get"; + request.global_prefs = global_prefs; request.include_defaults = include_defaults; // Send the request to C++. @@ -177,10 +179,11 @@ } // Set the preferences. - function setPreferences(preferences) { + function setPreferences(global_prefs, preferences) { // Create the request object. var request = {}; request.name = "preferences_set"; + request.global_prefs = global_prefs; request.preferences = preferences; // Send the request to C++. @@ -214,8 +217,9 @@ // Refresh the editor view contents. function refreshEditor() { + global_prefs = document.getElementById("global_prefs").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. editor.set(response); }); @@ -223,12 +227,13 @@ // Apply changes from the editor view. function applyEditorChanges() { - setPreferences(editor.get()); + global_prefs = document.getElementById("global_prefs").checked; + setPreferences(global_prefs, editor.get()); } // Refresh the simple view contents. function refreshSimple() { - getPreferences(true, function(response) { + getPreferences(false, true, function(response) { // Spellcheck settings. if (preferences_state.spellcheck_disabled) { // Cannot enable spell checking when disabled via the command-line. @@ -318,7 +323,7 @@ } if (has_preferences) - setPreferences(preferences); + setPreferences(false, preferences); } // Called when the proxy type is changed.