Index: components/ntp_snippets/remote/ntp_snippets_fetcher.cc |
diff --git a/components/ntp_snippets/remote/ntp_snippets_fetcher.cc b/components/ntp_snippets/remote/ntp_snippets_fetcher.cc |
index 881bd8b76bad2af34697a6ead510fb043988de62..c69665d7ad232f0547837411c229d2929cac7f76 100644 |
--- a/components/ntp_snippets/remote/ntp_snippets_fetcher.cc |
+++ b/components/ntp_snippets/remote/ntp_snippets_fetcher.cc |
@@ -26,6 +26,7 @@ |
#include "components/signin/core/browser/profile_oauth2_token_service.h" |
#include "components/signin/core/browser/signin_manager.h" |
#include "components/signin/core/browser/signin_manager_base.h" |
+#include "components/translate/core/browser/language_model.h" |
#include "components/variations/net/variations_http_headers.h" |
#include "components/variations/variations_associated_data.h" |
#include "net/base/load_flags.h" |
@@ -40,6 +41,7 @@ using net::URLFetcher; |
using net::URLRequestContextGetter; |
using net::HttpRequestHeaders; |
using net::URLRequestStatus; |
+using translate::LanguageModel; |
namespace ntp_snippets { |
@@ -166,6 +168,16 @@ std::string PosixLocaleFromBCP47Language(const std::string& language_code) { |
return locale; |
} |
+std::string ISO639FromPosixLocale(const std::string& locale) { |
+ char language[ULOC_LANG_CAPACITY]; |
Bernhard Bauer
2016/10/07 12:26:13
This is not going to be zero-initialized, right? S
jkrcal
2016/10/07 12:58:43
Good point! This applies to the function above as
|
+ UErrorCode error = U_ZERO_ERROR; |
+ uloc_getLanguage(locale.c_str(), language, ULOC_LANG_CAPACITY, &error); |
Bernhard Bauer
2016/10/07 12:26:13
#include "third_party/icu/source/common/unicode/ul
jkrcal
2016/10/07 12:58:43
It is already there, isn't it? ;)
|
+ DLOG_IF(WARNING, U_ZERO_ERROR != error) |
Bernhard Bauer
2016/10/07 12:26:13
Write the condition the natural way around? A mode
jkrcal
2016/10/07 12:58:43
Done.
|
+ << "Error in translating locale string to a ISO639 language code: " |
+ << error; |
+ return language; |
+} |
+ |
} // namespace |
NTPSnippetsFetcher::FetchedCategory::FetchedCategory(Category c) |
@@ -183,6 +195,7 @@ NTPSnippetsFetcher::NTPSnippetsFetcher( |
scoped_refptr<URLRequestContextGetter> url_request_context_getter, |
PrefService* pref_service, |
CategoryFactory* category_factory, |
+ LanguageModel* language_model, |
const ParseJSONCallback& parse_json_callback, |
const std::string& api_key) |
: OAuth2TokenService::Consumer("ntp_snippets"), |
@@ -191,6 +204,7 @@ NTPSnippetsFetcher::NTPSnippetsFetcher( |
waiting_for_refresh_token_(false), |
url_request_context_getter_(std::move(url_request_context_getter)), |
category_factory_(category_factory), |
+ language_model_(language_model), |
parse_json_callback_(parse_json_callback), |
count_to_fetch_(0), |
fetch_url_(GetFetchEndpoint()), |
@@ -274,6 +288,17 @@ void NTPSnippetsFetcher::FetchSnippetsFromHosts( |
} |
} |
+NTPSnippetsFetcher::ContentLanguageInfo::ContentLanguageInfo() |
+ : language_code(), frequency(0) {} |
+ |
+void NTPSnippetsFetcher::ContentLanguageInfo::AppendToList( |
+ base::ListValue* list) { |
+ auto lang = base::MakeUnique<base::DictionaryValue>(); |
+ lang->SetString("language", language_code); |
+ lang->SetDouble("frequency", frequency); |
+ list->Append(std::move(lang)); |
+} |
+ |
NTPSnippetsFetcher::RequestParams::RequestParams() |
: fetch_api(), |
obfuscated_gaia_id(), |
@@ -357,6 +382,16 @@ std::string NTPSnippetsFetcher::RequestParams::BuildRequest() { |
} |
request->Set("excludedSuggestionIds", std::move(excluded)); |
+ if (ui_language.frequency == 0 && other_top_language.frequency == 0) |
+ break; |
+ |
+ auto language_list = base::MakeUnique<base::ListValue>(); |
+ if (ui_language.frequency > 0) |
+ ui_language.AppendToList(language_list.get()); |
+ if (other_top_language.frequency > 0) |
+ other_top_language.AppendToList(language_list.get()); |
+ request->Set("top_languages", std::move(language_list)); |
+ |
// TODO(sfiera): support authentication and personalization |
// TODO(sfiera): support count_to_fetch |
break; |
@@ -409,18 +444,43 @@ bool NTPSnippetsFetcher::UsesAuthentication() const { |
personalization_ == Personalization::kBoth); |
} |
+void NTPSnippetsFetcher::SetUpCommonFetchingParameters( |
+ RequestParams* params) const { |
+ params->fetch_api = fetch_api_; |
+ params->host_restricts = hosts_; |
+ params->user_locale = locale_; |
+ params->excluded_ids = excluded_ids_; |
+ params->count_to_fetch = count_to_fetch_; |
+ params->interactive_request = interactive_request_; |
+ |
+ // TODO(jkrcal): Add language model factory for iOS and add fakes to tests so |
+ // that |language_model_| is never nullptr. Remove this check and add a DCHECK |
+ // into the constructor. |
+ if (!language_model_) |
+ return; |
+ |
+ params->ui_language.language_code = ISO639FromPosixLocale(locale_); |
+ params->ui_language.frequency = |
+ language_model_->GetLanguageFrequency(params->ui_language.language_code); |
+ |
+ std::vector<LanguageModel::LanguageInfo> top_languages = |
+ language_model_->GetTopLanguages(); |
+ for (const LanguageModel::LanguageInfo& info : top_languages) { |
+ if (info.language_code != params->ui_language.language_code) { |
+ params->other_top_language.language_code = info.language_code; |
+ params->other_top_language.frequency = info.frequency; |
+ break; |
+ } |
+ } |
+} |
+ |
void NTPSnippetsFetcher::FetchSnippetsNonAuthenticated() { |
// When not providing OAuth token, we need to pass the Google API key. |
GURL url(base::StringPrintf(kSnippetsServerNonAuthorizedFormat, |
fetch_url_.spec().c_str(), api_key_.c_str())); |
RequestParams params; |
- params.fetch_api = fetch_api_; |
- params.host_restricts = hosts_; |
- params.excluded_ids = excluded_ids_; |
- params.count_to_fetch = count_to_fetch_; |
- params.interactive_request = interactive_request_; |
- params.user_locale = locale_; |
+ SetUpCommonFetchingParameters(¶ms); |
FetchSnippetsImpl(url, std::string(), params.BuildRequest()); |
} |
@@ -428,15 +488,10 @@ void NTPSnippetsFetcher::FetchSnippetsAuthenticated( |
const std::string& account_id, |
const std::string& oauth_access_token) { |
RequestParams params; |
- params.fetch_api = fetch_api_; |
+ SetUpCommonFetchingParameters(¶ms); |
params.obfuscated_gaia_id = account_id; |
params.only_return_personalized_results = |
personalization_ == Personalization::kPersonal; |
- params.user_locale = locale_; |
- params.host_restricts = hosts_; |
- params.excluded_ids = excluded_ids_; |
- params.count_to_fetch = count_to_fetch_; |
- params.interactive_request = interactive_request_; |
// TODO(jkrcal, treib): Add unit-tests for authenticated fetches. |
FetchSnippetsImpl(fetch_url_, |
base::StringPrintf(kAuthorizationRequestHeaderFormat, |