Support configuration of the "Accept-Language" HTTP header globally or on a per-browser basis using new CefSettings.accept_language_list and CefBrowserSettings.accept_language_list members (issue #318).

git-svn-id: https://chromiumembedded.googlecode.com/svn/trunk@2033 5089003a-bbd8-11dd-ad1f-f1f9622dbc98
This commit is contained in:
Marshall Greenblatt 2015-02-18 17:28:56 +00:00
parent 7a2ce64096
commit f67b6fecaf
11 changed files with 117 additions and 3 deletions

View File

@ -27,3 +27,4 @@ Mihai Tica <mitica@adobe.com>
Czarek Tomczak <czarek.tomczak@gmail.com>
Felix Bruns <felixbruns@spotify.com>
YuTeh Shen <shenyute@gmail.com>
Andrei Kurushin <ajax16384@gmail.com>

View File

@ -381,6 +381,14 @@ typedef struct _cef_settings_t {
// of the background color but will be otherwise ignored.
///
cef_color_t background_color;
///
// Comma delimited ordered list of language codes without any whitespace that
// will be used in the "Accept-Language" HTTP header. May be overridden on a
// per-browser basis using the CefBrowserSettings.accept_language_list value.
// If both values are empty then "en-US,en" will be used.
///
cef_string_t accept_language_list;
} cef_settings_t;
///
@ -564,6 +572,14 @@ typedef struct _cef_browser_settings_t {
// 0 to enable use of the background color but will be otherwise ignored.
///
cef_color_t background_color;
///
// Comma delimited ordered list of language codes without any whitespace that
// will be used in the "Accept-Language" HTTP header. May be set globally
// using the CefBrowserSettings.accept_language_list value. If both values are
// empty then "en-US,en" will be used.
///
cef_string_t accept_language_list;
} cef_browser_settings_t;
///

View File

@ -276,8 +276,8 @@ struct CefScreenInfoTraits {
};
///
// Class representing the virtual screen information for use when window rendering
// is disabled.
// Class representing the virtual screen information for use when window
// rendering is disabled.
///
class CefScreenInfo : public CefStructBase<CefScreenInfoTraits> {
public:
@ -425,6 +425,7 @@ struct CefSettingsTraits {
cef_string_clear(&s->javascript_flags);
cef_string_clear(&s->resources_dir_path);
cef_string_clear(&s->locales_dir_path);
cef_string_clear(&s->accept_language_list);
}
static inline void set(const struct_type* src, struct_type* target,
@ -464,6 +465,9 @@ struct CefSettingsTraits {
target->context_safety_implementation = src->context_safety_implementation;
target->ignore_certificate_errors = src->ignore_certificate_errors;
target->background_color = src->background_color;
cef_string_set(src->accept_language_list.str,
src->accept_language_list.length, &target->accept_language_list, copy);
}
};
@ -488,6 +492,7 @@ struct CefBrowserSettingsTraits {
cef_string_clear(&s->cursive_font_family);
cef_string_clear(&s->fantasy_font_family);
cef_string_clear(&s->default_encoding);
cef_string_clear(&s->accept_language_list);
}
static inline void set(const struct_type* src, struct_type* target,
@ -540,6 +545,9 @@ struct CefBrowserSettingsTraits {
target->webgl = src->webgl;
target->background_color = src->background_color;
cef_string_set(src->accept_language_list.str,
src->accept_language_list.length, &target->accept_language_list, copy);
}
};

View File

@ -13,6 +13,7 @@
#include "libcef/common/request_impl.h"
#include "net/base/net_errors.h"
#include "net/http/http_util.h"
#include "net/url_request/url_request.h"
namespace {
@ -92,6 +93,14 @@ int CefNetworkDelegate::OnBeforeURLRequest(
CefRefPtr<CefBrowserHostImpl> browser =
CefBrowserHostImpl::GetBrowserForRequest(request);
if (browser.get()) {
const CefBrowserSettings& browser_settings = browser->settings();
if (browser_settings.accept_language_list.length > 0) {
const std::string& accept_language =
net::HttpUtil::GenerateAcceptLanguageHeader(
CefString(&browser_settings.accept_language_list));
request->SetExtraRequestHeaderByName(
net::HttpRequestHeaders::kAcceptLanguage, accept_language, false);
}
CefRefPtr<CefClient> client = browser->GetClient();
if (client.get()) {
CefRefPtr<CefRequestHandler> handler = client->GetRequestHandler();

View File

@ -144,8 +144,17 @@ net::URLRequestContext* CefURLRequestContextGetterImpl::GetURLRequestContext() {
new net::ChannelIDService(
new net::DefaultChannelIDStore(NULL),
base::WorkerPool::GetTaskRunner(true))));
std::string accept_language;
if (settings.accept_language_list.length > 0) {
accept_language =
net::HttpUtil::GenerateAcceptLanguageHeader(
CefString(&settings.accept_language_list));
} else {
accept_language = "en-US,en";
}
storage_->set_http_user_agent_settings(
new CefHttpUserAgentSettings("en-us,en"));
new CefHttpUserAgentSettings(accept_language));
storage_->set_host_resolver(net::HostResolver::CreateDefaultResolver(NULL));
storage_->set_cert_verifier(net::CertVerifier::CreateDefault());

View File

@ -13,6 +13,7 @@
#include "include/cef_scheme.h"
#include "include/wrapper/cef_closure_task.h"
#include "tests/unittests/test_handler.h"
#include "tests/unittests/test_suite.h"
namespace {
@ -34,6 +35,7 @@ class TestResults {
sub_status_code = 0;
sub_allow_origin.clear();
exit_url.clear();
accept_language.clear();
delay = 0;
got_request.reset();
got_read.reset();
@ -60,6 +62,9 @@ class TestResults {
std::string sub_redirect_url;
std::string exit_url;
// Used for testing per-browser Accept-Language.
std::string accept_language;
// Delay for returning scheme handler results.
int delay;
@ -87,6 +92,13 @@ class TestSchemeHandler : public TestHandler {
g_current_handler = this;
}
void PopulateBrowserSettings(CefBrowserSettings* settings) override {
if (!test_results_->accept_language.empty()) {
CefString(&settings->accept_language_list) =
test_results_->accept_language;
}
}
void RunTest() override {
CreateBrowser(test_results_->url);
@ -194,6 +206,27 @@ class ClientSchemeHandler : public CefResourceHandler {
handled = true;
}
std::string accept_language;
CefRequest::HeaderMap headerMap;
CefRequest::HeaderMap::iterator headerIter;
request->GetHeaderMap(headerMap);
headerIter = headerMap.find("Accept-Language");
if (headerIter != headerMap.end())
accept_language = headerIter->second;
EXPECT_TRUE(!accept_language.empty());
if (!test_results_->accept_language.empty()) {
// Value from CefBrowserSettings.accept_language set in
// PopulateBrowserSettings().
EXPECT_STREQ(test_results_->accept_language.data(),
accept_language.data());
} else {
// Value from CefSettings.accept_language set in
// CefTestSuite::GetSettings().
EXPECT_STREQ(CEF_SETTINGS_ACCEPT_LANGUAGE,
accept_language.data());
}
if (handled) {
if (test_results_->delay > 0) {
// Continue after the delay.
@ -1563,6 +1596,29 @@ TEST(SchemeHandlerTest,
ClearTestSchemes();
}
// Test per-browser setting of Accept-Language.
TEST(SchemeHandlerTest, AcceptLanguage) {
RegisterTestScheme("customstd", "test");
g_TestResults.url = "customstd://test/run.html";
g_TestResults.html =
"<html><head></head><body><h1>Success!</h1></body></html>";
g_TestResults.status_code = 200;
// Value that will be set via CefBrowserSettings.accept_language in
// PopulateBrowserSettings().
g_TestResults.accept_language = "uk";
CefRefPtr<TestSchemeHandler> handler = new TestSchemeHandler(&g_TestResults);
handler->ExecuteTest();
ReleaseAndWaitForDestructor(handler);
EXPECT_TRUE(g_TestResults.got_request);
EXPECT_TRUE(g_TestResults.got_read);
EXPECT_TRUE(g_TestResults.got_output);
ClearTestSchemes();
}
// Entry point for registering custom schemes.
// Called from client_app_delegates.cc.

View File

@ -287,6 +287,7 @@ void TestHandler::CreateBrowser(
windowInfo.SetAsPopup(NULL, "CefUnitTest");
windowInfo.style |= WS_VISIBLE;
#endif
PopulateBrowserSettings(&settings);
CefBrowserHost::CreateBrowser(windowInfo, this, url, settings,
request_context);
}

View File

@ -214,6 +214,9 @@ class TestHandler : public CefClient,
// SetTestTimeout(). Calls DestroyTest() by default.
virtual void OnTestTimeout(int timeout_ms);
// Called from CreateBrowser() to optionally set per-browser settings.
virtual void PopulateBrowserSettings(CefBrowserSettings* settings) {}
void CreateBrowser(const CefString& url,
CefRefPtr<CefRequestContext> request_context = NULL);

View File

@ -69,6 +69,9 @@ void CefTestSuite::GetSettings(CefSettings& settings) {
// Necessary for the OSRTest tests.
settings.windowless_rendering_enabled = true;
// For Accept-Language test
CefString(&settings.accept_language_list) = CEF_SETTINGS_ACCEPT_LANGUAGE;
}
// static

View File

@ -36,4 +36,6 @@ class CefTestSuite : public TestSuite {
static base::CommandLine* commandline_;
};
#define CEF_SETTINGS_ACCEPT_LANGUAGE "en-GB"
#endif // CEF_TESTS_UNITTESTS_TEST_SUITE_H_

View File

@ -21,6 +21,7 @@
#include "testing/gtest/include/gtest/gtest.h"
#include "tests/cefclient/renderer/client_app_renderer.h"
#include "tests/unittests/test_handler.h"
#include "tests/unittests/test_suite.h"
#include "tests/unittests/test_util.h"
using client::ClientAppRenderer;
@ -212,6 +213,11 @@ class RequestSchemeHandler : public CefResourceHandler {
headerIter = headerMap.find("Accept-Language");
EXPECT_TRUE(headerIter != headerMap.end() && !headerIter->second.empty());
// Verify that we get the value that was set via
// CefSettings.accept_language_list in CefTestSuite::GetSettings().
EXPECT_STREQ(CEF_SETTINGS_ACCEPT_LANGUAGE,
headerIter->second.ToString().data());
// Check if the request cookie was sent.
bool has_send_cookie = false;
headerIter = headerMap.find("Cookie");