Commit 1cb41b94 authored by Tibor Goldschwendt's avatar Tibor Goldschwendt Committed by Commit Bot

[ntp][modules] Set hl param to app locale for shopping tasks request

The hl param overrides the account language. By setting it to Chrome's
application locale we ensure that the shopping tasks module's and
Chrome's UI language will match.

Fixed: 1133409
Change-Id: I195734b8dbb46b35cd69b6e250f6d559f640aa5c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2439022
Commit-Queue: Tibor Goldschwendt <tiborg@chromium.org>
Commit-Queue: Moe Ahmadi <mahmadi@chromium.org>
Auto-Submit: Tibor Goldschwendt <tiborg@chromium.org>
Reviewed-by: default avatarMoe Ahmadi <mahmadi@chromium.org>
Cr-Commit-Position: refs/heads/master@{#811970}
parent f8db21ff
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#include "chrome/common/webui_url_constants.h" #include "chrome/common/webui_url_constants.h"
#include "components/google/core/common/google_util.h" #include "components/google/core/common/google_util.h"
#include "net/base/url_util.h"
#include "services/network/public/cpp/resource_request.h" #include "services/network/public/cpp/resource_request.h"
#include "services/network/public/cpp/shared_url_loader_factory.h" #include "services/network/public/cpp/shared_url_loader_factory.h"
#include "services/network/public/cpp/simple_url_loader.h" #include "services/network/public/cpp/simple_url_loader.h"
...@@ -14,19 +15,23 @@ namespace { ...@@ -14,19 +15,23 @@ namespace {
const char kNewTabShoppingTasksApiPath[] = "/async/newtab_shopping_tasks"; const char kNewTabShoppingTasksApiPath[] = "/async/newtab_shopping_tasks";
const char kXSSIResponsePreamble[] = ")]}'"; const char kXSSIResponsePreamble[] = ")]}'";
GURL GetApiUrl() { GURL GetApiUrl(const std::string& application_locale) {
GURL google_base_url = google_util::CommandLineGoogleBaseURL(); GURL google_base_url = google_util::CommandLineGoogleBaseURL();
if (!google_base_url.is_valid()) { if (!google_base_url.is_valid()) {
google_base_url = GURL(google_util::kGoogleHomepageURL); google_base_url = GURL(google_util::kGoogleHomepageURL);
} }
return google_base_url.Resolve(kNewTabShoppingTasksApiPath); return net::AppendQueryParameter(
google_base_url.Resolve(kNewTabShoppingTasksApiPath), "hl",
application_locale);
} }
} // namespace } // namespace
ShoppingTasksService::ShoppingTasksService( ShoppingTasksService::ShoppingTasksService(
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory, scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
Profile* profile) Profile* profile,
: url_loader_factory_(url_loader_factory) {} const std::string& application_locale)
: url_loader_factory_(url_loader_factory),
application_locale_(application_locale) {}
ShoppingTasksService::~ShoppingTasksService() = default; ShoppingTasksService::~ShoppingTasksService() = default;
...@@ -70,7 +75,7 @@ void ShoppingTasksService::GetPrimaryShoppingTask( ...@@ -70,7 +75,7 @@ void ShoppingTasksService::GetPrimaryShoppingTask(
})"); })");
auto resource_request = std::make_unique<network::ResourceRequest>(); auto resource_request = std::make_unique<network::ResourceRequest>();
resource_request->url = GetApiUrl(); resource_request->url = GetApiUrl(application_locale_);
resource_request->credentials_mode = resource_request->credentials_mode =
network::mojom::CredentialsMode::kInclude; network::mojom::CredentialsMode::kInclude;
resource_request->request_initiator = resource_request->request_initiator =
......
...@@ -26,7 +26,8 @@ class ShoppingTasksService : public KeyedService { ...@@ -26,7 +26,8 @@ class ShoppingTasksService : public KeyedService {
public: public:
ShoppingTasksService( ShoppingTasksService(
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory, scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
Profile* profile); Profile* profile,
const std::string& application_locale);
ShoppingTasksService(const ShoppingTasksService&) = delete; ShoppingTasksService(const ShoppingTasksService&) = delete;
~ShoppingTasksService() override; ~ShoppingTasksService() override;
...@@ -50,6 +51,7 @@ class ShoppingTasksService : public KeyedService { ...@@ -50,6 +51,7 @@ class ShoppingTasksService : public KeyedService {
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_; scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_;
std::list<std::unique_ptr<network::SimpleURLLoader>> loaders_; std::list<std::unique_ptr<network::SimpleURLLoader>> loaders_;
std::string application_locale_;
base::WeakPtrFactory<ShoppingTasksService> weak_ptr_factory_{this}; base::WeakPtrFactory<ShoppingTasksService> weak_ptr_factory_{this};
}; };
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#include "chrome/browser/search/shopping_tasks/shopping_tasks_service_factory.h" #include "chrome/browser/search/shopping_tasks/shopping_tasks_service_factory.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/content_settings/cookie_settings_factory.h" #include "chrome/browser/content_settings/cookie_settings_factory.h"
#include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile.h"
#include "chrome/browser/search/shopping_tasks/shopping_tasks_service.h" #include "chrome/browser/search/shopping_tasks/shopping_tasks_service.h"
...@@ -38,5 +39,6 @@ KeyedService* ShoppingTasksServiceFactory::BuildServiceInstanceFor( ...@@ -38,5 +39,6 @@ KeyedService* ShoppingTasksServiceFactory::BuildServiceInstanceFor(
content::BrowserContext::GetDefaultStoragePartition(context) content::BrowserContext::GetDefaultStoragePartition(context)
->GetURLLoaderFactoryForBrowserProcess(); ->GetURLLoaderFactoryForBrowserProcess();
return new ShoppingTasksService(url_loader_factory, return new ShoppingTasksService(url_loader_factory,
Profile::FromBrowserContext(context)); Profile::FromBrowserContext(context),
g_browser_process->GetApplicationLocale());
} }
...@@ -28,7 +28,7 @@ class ShoppingTasksServiceTest : public testing::Test { ...@@ -28,7 +28,7 @@ class ShoppingTasksServiceTest : public testing::Test {
service_ = std::make_unique<ShoppingTasksService>( service_ = std::make_unique<ShoppingTasksService>(
base::MakeRefCounted<network::WeakWrapperSharedURLLoaderFactory>( base::MakeRefCounted<network::WeakWrapperSharedURLLoaderFactory>(
&test_url_loader_factory_), &test_url_loader_factory_),
&profile_); &profile_, "en-US");
} }
void TearDown() override { void TearDown() override {
...@@ -49,7 +49,7 @@ class ShoppingTasksServiceTest : public testing::Test { ...@@ -49,7 +49,7 @@ class ShoppingTasksServiceTest : public testing::Test {
// Verifies correct parsing of well-formed JSON. // Verifies correct parsing of well-formed JSON.
TEST_F(ShoppingTasksServiceTest, GoodResponse) { TEST_F(ShoppingTasksServiceTest, GoodResponse) {
test_url_loader_factory_.AddResponse( test_url_loader_factory_.AddResponse(
"https://www.google.com/async/newtab_shopping_tasks", "https://www.google.com/async/newtab_shopping_tasks?hl=en-US",
R"()]}' R"()]}'
{ {
"update": { "update": {
...@@ -123,7 +123,7 @@ TEST_F(ShoppingTasksServiceTest, GoodResponse) { ...@@ -123,7 +123,7 @@ TEST_F(ShoppingTasksServiceTest, GoodResponse) {
// Verifies service can handle multiple in flight requests. // Verifies service can handle multiple in flight requests.
TEST_F(ShoppingTasksServiceTest, MultiRequest) { TEST_F(ShoppingTasksServiceTest, MultiRequest) {
test_url_loader_factory_.AddResponse( test_url_loader_factory_.AddResponse(
"https://www.google.com/async/newtab_shopping_tasks", "https://www.google.com/async/newtab_shopping_tasks?hl=en-US",
R"()]}' R"()]}'
{ {
"update": { "update": {
...@@ -179,7 +179,7 @@ TEST_F(ShoppingTasksServiceTest, MultiRequest) { ...@@ -179,7 +179,7 @@ TEST_F(ShoppingTasksServiceTest, MultiRequest) {
// Verifies error if JSON is malformed. // Verifies error if JSON is malformed.
TEST_F(ShoppingTasksServiceTest, BadResponse) { TEST_F(ShoppingTasksServiceTest, BadResponse) {
test_url_loader_factory_.AddResponse( test_url_loader_factory_.AddResponse(
"https://www.google.com/async/newtab_shopping_tasks", "https://www.google.com/async/newtab_shopping_tasks?hl=en-US",
")]}'{\"update\":{\"promotions\":{}}}"); ")]}'{\"update\":{\"promotions\":{}}}");
shopping_tasks::mojom::ShoppingTaskPtr result; shopping_tasks::mojom::ShoppingTaskPtr result;
...@@ -200,7 +200,7 @@ TEST_F(ShoppingTasksServiceTest, BadResponse) { ...@@ -200,7 +200,7 @@ TEST_F(ShoppingTasksServiceTest, BadResponse) {
// Verifies error if no products. // Verifies error if no products.
TEST_F(ShoppingTasksServiceTest, NoProducts) { TEST_F(ShoppingTasksServiceTest, NoProducts) {
test_url_loader_factory_.AddResponse( test_url_loader_factory_.AddResponse(
"https://www.google.com/async/newtab_shopping_tasks", "https://www.google.com/async/newtab_shopping_tasks?hl=en-US",
R"()]}' R"()]}'
{ {
"update": { "update": {
...@@ -237,7 +237,7 @@ TEST_F(ShoppingTasksServiceTest, NoProducts) { ...@@ -237,7 +237,7 @@ TEST_F(ShoppingTasksServiceTest, NoProducts) {
// Verifies error if download fails. // Verifies error if download fails.
TEST_F(ShoppingTasksServiceTest, ErrorResponse) { TEST_F(ShoppingTasksServiceTest, ErrorResponse) {
test_url_loader_factory_.AddResponse( test_url_loader_factory_.AddResponse(
GURL("https://www.google.com/async/newtab_shopping_tasks"), GURL("https://www.google.com/async/newtab_shopping_tasks?hl=en-US"),
network::mojom::URLResponseHead::New(), std::string(), network::mojom::URLResponseHead::New(), std::string(),
network::URLLoaderCompletionStatus(net::HTTP_NOT_FOUND)); network::URLLoaderCompletionStatus(net::HTTP_NOT_FOUND));
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment