2015-10-03 01:03:16 +02:00
|
|
|
// Copyright (c) 2015 The Chromium Embedded Framework Authors. All rights
|
|
|
|
// reserved. Use of this source code is governed by a BSD-style license that
|
|
|
|
// can be found in the LICENSE file.
|
|
|
|
|
2016-11-18 00:52:42 +01:00
|
|
|
#include "tests/cefclient/browser/preferences_test.h"
|
2015-10-03 01:03:16 +02:00
|
|
|
|
|
|
|
#include <sstream>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "include/base/cef_logging.h"
|
|
|
|
#include "include/cef_command_line.h"
|
|
|
|
#include "include/cef_parser.h"
|
2017-11-09 20:00:47 +01:00
|
|
|
#include "tests/cefclient/browser/test_runner.h"
|
2015-10-03 01:03:16 +02:00
|
|
|
|
|
|
|
namespace client {
|
|
|
|
namespace preferences_test {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2017-11-09 20:00:47 +01:00
|
|
|
const char kTestUrlPath[] = "/preferences";
|
2015-10-03 01:03:16 +02:00
|
|
|
|
|
|
|
// Application-specific error codes.
|
|
|
|
const int kMessageFormatError = 1;
|
|
|
|
const int kPreferenceApplicationError = 1;
|
|
|
|
|
|
|
|
// Common to all messages.
|
|
|
|
const char kNameKey[] = "name";
|
|
|
|
const char kNameValueGet[] = "preferences_get";
|
|
|
|
const char kNameValueSet[] = "preferences_set";
|
|
|
|
const char kNameValueState[] = "preferences_state";
|
|
|
|
|
|
|
|
// Used with "preferences_get" messages.
|
|
|
|
const char kIncludeDefaultsKey[] = "include_defaults";
|
|
|
|
|
|
|
|
// Used with "preferences_set" messages.
|
|
|
|
const char kPreferencesKey[] = "preferences";
|
|
|
|
|
|
|
|
// Handle messages in the browser process. Only accessed on the UI thread.
|
|
|
|
class Handler : public CefMessageRouterBrowserSide::Handler {
|
|
|
|
public:
|
|
|
|
typedef std::vector<std::string> NameVector;
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
Handler() { CEF_REQUIRE_UI_THREAD(); }
|
2015-10-03 01:03:16 +02:00
|
|
|
|
|
|
|
// Called due to cefQuery execution in preferences.html.
|
|
|
|
bool OnQuery(CefRefPtr<CefBrowser> browser,
|
|
|
|
CefRefPtr<CefFrame> frame,
|
2023-06-01 16:06:15 +02:00
|
|
|
int64_t query_id,
|
2015-10-03 01:03:16 +02:00
|
|
|
const CefString& request,
|
|
|
|
bool persistent,
|
2021-06-17 21:43:06 +02:00
|
|
|
CefRefPtr<Callback> callback) override {
|
2015-10-03 01:03:16 +02:00
|
|
|
CEF_REQUIRE_UI_THREAD();
|
|
|
|
|
|
|
|
// Only handle messages from the test URL.
|
2017-11-09 20:00:47 +01:00
|
|
|
const std::string& url = frame->GetURL();
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!test_runner::IsTestURL(url, kTestUrlPath)) {
|
2015-10-03 01:03:16 +02:00
|
|
|
return false;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2015-10-03 01:03:16 +02:00
|
|
|
|
|
|
|
// Parse |request| as a JSON dictionary.
|
|
|
|
CefRefPtr<CefDictionaryValue> request_dict = ParseJSON(request);
|
|
|
|
if (!request_dict) {
|
|
|
|
callback->Failure(kMessageFormatError, "Incorrect message format");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the "name" key.
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!VerifyKey(request_dict, kNameKey, VTYPE_STRING, callback)) {
|
2015-10-03 01:03:16 +02:00
|
|
|
return true;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2015-10-03 01:03:16 +02:00
|
|
|
|
|
|
|
const std::string& message_name = request_dict->GetString(kNameKey);
|
|
|
|
if (message_name == kNameValueGet) {
|
|
|
|
// JavaScript is requesting a JSON representation of the preferences tree.
|
|
|
|
|
|
|
|
// Verify the "include_defaults" key.
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!VerifyKey(request_dict, kIncludeDefaultsKey, VTYPE_BOOL, callback)) {
|
2015-10-03 01:03:16 +02:00
|
|
|
return true;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2015-10-03 01:03:16 +02:00
|
|
|
|
|
|
|
const bool include_defaults = request_dict->GetBool(kIncludeDefaultsKey);
|
|
|
|
|
|
|
|
OnPreferencesGet(browser, 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.
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!VerifyKey(request_dict, kPreferencesKey, VTYPE_DICTIONARY,
|
|
|
|
callback)) {
|
2015-10-03 01:03:16 +02:00
|
|
|
return true;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2015-10-03 01:03:16 +02:00
|
|
|
|
|
|
|
CefRefPtr<CefDictionaryValue> preferences =
|
|
|
|
request_dict->GetDictionary(kPreferencesKey);
|
|
|
|
|
|
|
|
OnPreferencesSet(browser, preferences, callback);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
} else if (message_name == kNameValueState) {
|
|
|
|
// JavaScript is requesting global state information.
|
|
|
|
|
|
|
|
OnPreferencesState(browser, callback);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Execute |callback| with the preferences dictionary as a JSON string.
|
|
|
|
static void OnPreferencesGet(CefRefPtr<CefBrowser> browser,
|
|
|
|
bool include_defaults,
|
|
|
|
CefRefPtr<Callback> callback) {
|
|
|
|
CefRefPtr<CefRequestContext> context =
|
|
|
|
browser->GetHost()->GetRequestContext();
|
|
|
|
|
|
|
|
// Retrieve all preference values.
|
|
|
|
CefRefPtr<CefDictionaryValue> prefs =
|
|
|
|
context->GetAllPreferences(include_defaults);
|
|
|
|
|
|
|
|
// Serialize the preferences to JSON and return to the JavaScript caller.
|
|
|
|
callback->Success(GetJSON(prefs));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set preferences based on the contents of |preferences|. Execute |callback|
|
|
|
|
// with a descriptive result message.
|
|
|
|
static void OnPreferencesSet(CefRefPtr<CefBrowser> browser,
|
|
|
|
CefRefPtr<CefDictionaryValue> preferences,
|
|
|
|
CefRefPtr<Callback> callback) {
|
|
|
|
CefRefPtr<CefRequestContext> context =
|
|
|
|
browser->GetHost()->GetRequestContext();
|
|
|
|
|
|
|
|
CefRefPtr<CefValue> value = CefValue::Create();
|
|
|
|
value->SetDictionary(preferences);
|
|
|
|
|
|
|
|
std::string error;
|
|
|
|
NameVector changed_names;
|
|
|
|
|
|
|
|
// Apply preferences. This may result in errors.
|
2017-05-17 11:29:28 +02:00
|
|
|
const bool success =
|
|
|
|
ApplyPrefs(context, std::string(), value, error, changed_names);
|
2015-10-03 01:03:16 +02:00
|
|
|
|
|
|
|
// Create a message that accurately represents the result.
|
|
|
|
std::string message;
|
|
|
|
if (!changed_names.empty()) {
|
|
|
|
std::stringstream ss;
|
2017-05-17 11:29:28 +02:00
|
|
|
ss << "Successfully changed " << changed_names.size() << " preferences; ";
|
2015-10-03 01:03:16 +02:00
|
|
|
for (size_t i = 0; i < changed_names.size(); ++i) {
|
|
|
|
ss << changed_names[i];
|
2023-01-02 23:59:03 +01:00
|
|
|
if (i < changed_names.size() - 1) {
|
2015-10-03 01:03:16 +02:00
|
|
|
ss << ", ";
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2015-10-03 01:03:16 +02:00
|
|
|
}
|
|
|
|
message = ss.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!success) {
|
|
|
|
DCHECK(!error.empty());
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!message.empty()) {
|
2015-10-03 01:03:16 +02:00
|
|
|
message += "\n";
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2015-10-03 01:03:16 +02:00
|
|
|
message += error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (changed_names.empty()) {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!message.empty()) {
|
2015-10-03 01:03:16 +02:00
|
|
|
message += "\n";
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2015-10-03 01:03:16 +02:00
|
|
|
message += "No preferences changed.";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the message to the JavaScript caller.
|
2023-01-02 23:59:03 +01:00
|
|
|
if (success) {
|
2015-10-03 01:03:16 +02:00
|
|
|
callback->Success(message);
|
2023-01-02 23:59:03 +01:00
|
|
|
} else {
|
2015-10-03 01:03:16 +02:00
|
|
|
callback->Failure(kPreferenceApplicationError, message);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2015-10-03 01:03:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Execute |callback| with the global state dictionary as a JSON string.
|
|
|
|
static void OnPreferencesState(CefRefPtr<CefBrowser> browser,
|
|
|
|
CefRefPtr<Callback> callback) {
|
|
|
|
CefRefPtr<CefCommandLine> command_line =
|
|
|
|
CefCommandLine::GetGlobalCommandLine();
|
|
|
|
|
|
|
|
CefRefPtr<CefDictionaryValue> dict = CefDictionaryValue::Create();
|
|
|
|
|
|
|
|
// If spell checking is disabled via the command-line then it cannot be
|
|
|
|
// enabled via preferences.
|
|
|
|
dict->SetBool("spellcheck_disabled",
|
2017-05-17 11:29:28 +02:00
|
|
|
command_line->HasSwitch("disable-spell-checking"));
|
2015-10-03 01:03:16 +02:00
|
|
|
|
|
|
|
// If proxy settings are configured via the command-line then they cannot
|
|
|
|
// be modified via preferences.
|
|
|
|
dict->SetBool("proxy_configured",
|
2017-05-17 11:29:28 +02:00
|
|
|
command_line->HasSwitch("no-proxy-server") ||
|
|
|
|
command_line->HasSwitch("proxy-auto-detect") ||
|
|
|
|
command_line->HasSwitch("proxy-pac-url") ||
|
|
|
|
command_line->HasSwitch("proxy-server"));
|
2015-10-03 01:03:16 +02:00
|
|
|
|
2015-10-17 02:44:00 +02:00
|
|
|
// If allow running insecure content is enabled via the command-line then it
|
|
|
|
// cannot be enabled via preferences.
|
|
|
|
dict->SetBool("allow_running_insecure_content",
|
2017-05-17 11:29:28 +02:00
|
|
|
command_line->HasSwitch("allow-running-insecure-content"));
|
2015-10-17 02:44:00 +02:00
|
|
|
|
2015-10-03 01:03:16 +02:00
|
|
|
// Serialize the state to JSON and return to the JavaScript caller.
|
|
|
|
callback->Success(GetJSON(dict));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert a JSON string to a dictionary value.
|
|
|
|
static CefRefPtr<CefDictionaryValue> ParseJSON(const CefString& string) {
|
|
|
|
CefRefPtr<CefValue> value = CefParseJSON(string, JSON_PARSER_RFC);
|
2023-01-02 23:59:03 +01:00
|
|
|
if (value.get() && value->GetType() == VTYPE_DICTIONARY) {
|
2015-10-03 01:03:16 +02:00
|
|
|
return value->GetDictionary();
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2020-01-15 15:28:12 +01:00
|
|
|
return nullptr;
|
2015-10-03 01:03:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Convert a dictionary value to a JSON string.
|
|
|
|
static CefString GetJSON(CefRefPtr<CefDictionaryValue> dictionary) {
|
|
|
|
CefRefPtr<CefValue> value = CefValue::Create();
|
|
|
|
value->SetDictionary(dictionary);
|
|
|
|
return CefWriteJSON(value, JSON_WRITER_DEFAULT);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that |key| exists in |dictionary| and has type |value_type|. Fails
|
|
|
|
// |callback| and returns false on failure.
|
|
|
|
static bool VerifyKey(CefRefPtr<CefDictionaryValue> dictionary,
|
|
|
|
const char* key,
|
|
|
|
cef_value_type_t value_type,
|
|
|
|
CefRefPtr<Callback> callback) {
|
|
|
|
if (!dictionary->HasKey(key) || dictionary->GetType(key) != value_type) {
|
2017-05-17 11:29:28 +02:00
|
|
|
callback->Failure(
|
|
|
|
kMessageFormatError,
|
|
|
|
"Missing or incorrectly formatted message key: " + std::string(key));
|
2015-10-03 01:03:16 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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<CefRequestContext> context,
|
|
|
|
const std::string& name,
|
|
|
|
CefRefPtr<CefValue> value,
|
|
|
|
std::string& error,
|
|
|
|
NameVector& changed_names) {
|
|
|
|
if (!name.empty() && context->HasPreference(name)) {
|
|
|
|
// The preference exists. Set the value.
|
|
|
|
return SetPref(context, name, value, error, changed_names);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value->GetType() == VTYPE_DICTIONARY) {
|
|
|
|
// A dictionary type value that is not an existing preference. Try to set
|
|
|
|
// each of the elements individually.
|
|
|
|
CefRefPtr<CefDictionaryValue> dict = value->GetDictionary();
|
|
|
|
|
|
|
|
CefDictionaryValue::KeyList keys;
|
|
|
|
dict->GetKeys(keys);
|
|
|
|
for (size_t i = 0; i < keys.size(); ++i) {
|
|
|
|
const std::string& key = keys[i];
|
2017-05-17 11:29:28 +02:00
|
|
|
const std::string& current_name = name.empty() ? key : name + "." + key;
|
2015-10-03 01:03:16 +02:00
|
|
|
if (!ApplyPrefs(context, current_name, dict->GetValue(key), error,
|
|
|
|
changed_names)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
error = "Trying to create an unregistered preference: " + name;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set a specific preference value. Returns true if the value is set
|
|
|
|
// 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<CefRequestContext> context,
|
|
|
|
const std::string& name,
|
|
|
|
CefRefPtr<CefValue> value,
|
|
|
|
std::string& error,
|
|
|
|
NameVector& changed_names) {
|
|
|
|
CefRefPtr<CefValue> existing_value = context->GetPreference(name);
|
|
|
|
DCHECK(existing_value);
|
|
|
|
|
|
|
|
if (value->GetType() == VTYPE_STRING &&
|
|
|
|
existing_value->GetType() != VTYPE_STRING) {
|
|
|
|
// Since |value| is coming from JSON all basic types will be represented
|
|
|
|
// as strings. Convert to the expected data type.
|
|
|
|
const std::string& string_val = value->GetString();
|
|
|
|
switch (existing_value->GetType()) {
|
|
|
|
case VTYPE_BOOL:
|
2023-01-02 23:59:03 +01:00
|
|
|
if (string_val == "true" || string_val == "1") {
|
2015-10-03 01:03:16 +02:00
|
|
|
value->SetBool(true);
|
2023-01-02 23:59:03 +01:00
|
|
|
} else if (string_val == "false" || string_val == "0") {
|
2015-10-03 01:03:16 +02:00
|
|
|
value->SetBool(false);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2015-10-03 01:03:16 +02:00
|
|
|
break;
|
|
|
|
case VTYPE_INT:
|
|
|
|
value->SetInt(atoi(string_val.c_str()));
|
|
|
|
break;
|
|
|
|
case VTYPE_DOUBLE:
|
|
|
|
value->SetInt(atof(string_val.c_str()));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// Other types cannot be converted.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nothing to do if the value hasn't changed.
|
2023-01-02 23:59:03 +01:00
|
|
|
if (existing_value->IsEqual(value)) {
|
2015-10-03 01:03:16 +02:00
|
|
|
return true;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2015-10-03 01:03:16 +02:00
|
|
|
|
|
|
|
// Attempt to set the preference.
|
|
|
|
CefString error_str;
|
|
|
|
if (!context->SetPreference(name, value, error_str)) {
|
|
|
|
error = error_str.ToString() + ": " + name;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The preference was set successfully.
|
|
|
|
changed_names.push_back(name);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(Handler);
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
void CreateMessageHandlers(test_runner::MessageHandlerSet& handlers) {
|
|
|
|
handlers.insert(new Handler());
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace preferences_test
|
|
|
|
} // namespace client
|