Commit 7aee0790 authored by Mathieu Perreault's avatar Mathieu Perreault Committed by Commit Bot

[Translate] Add Variations Headers to the request for the translate script

Bug: 881650
Test: Run chrome with --force-variations-ids=12345, look for the headers in the
request to translate in chrome://net-internals

Change-Id: I51cc33d4d7b0cf9d6a74a433b277f0890b990b01
Reviewed-on: https://chromium-review.googlesource.com/1212403
Commit-Queue: Mathieu Perreault <mathp@chromium.org>
Reviewed-by: default avatarMichael Martis <martis@chromium.org>
Cr-Commit-Position: refs/heads/master@{#589416}
parent f3334641
...@@ -59,6 +59,7 @@ static_library("browser") { ...@@ -59,6 +59,7 @@ static_library("browser") {
"//components/strings", "//components/strings",
"//components/translate/core/common", "//components/translate/core/common",
"//components/variations", "//components/variations",
"//components/variations/net",
"//google_apis", "//google_apis",
"//net", "//net",
"//services/metrics/public/cpp:metrics_cpp", "//services/metrics/public/cpp:metrics_cpp",
......
...@@ -227,8 +227,12 @@ void TranslateLanguageList::RequestLanguageList() { ...@@ -227,8 +227,12 @@ void TranslateLanguageList::RequestLanguageList() {
NotifyEvent(__LINE__, message); NotifyEvent(__LINE__, message);
bool result = language_list_fetcher_->Request( bool result = language_list_fetcher_->Request(
url, base::BindOnce(&TranslateLanguageList::OnLanguageListFetchComplete, url,
base::Unretained(this))); base::BindOnce(&TranslateLanguageList::OnLanguageListFetchComplete,
base::Unretained(this)),
// Use the strictest mode for request headers, since incognito state is
// not known.
/*is_incognito=*/true);
if (!result) if (!result)
NotifyEvent(__LINE__, "Request is omitted due to retry limit"); NotifyEvent(__LINE__, "Request is omitted due to retry limit");
} }
......
...@@ -369,7 +369,7 @@ void TranslateManager::TranslatePage(const std::string& original_source_lang, ...@@ -369,7 +369,7 @@ void TranslateManager::TranslatePage(const std::string& original_source_lang,
base::Bind(&TranslateManager::OnTranslateScriptFetchComplete, base::Bind(&TranslateManager::OnTranslateScriptFetchComplete,
GetWeakPtr(), source_lang, target_lang); GetWeakPtr(), source_lang, target_lang);
script->Request(callback); script->Request(callback, translate_driver_->IsIncognito());
} }
void TranslateManager::RevertTranslation() { void TranslateManager::RevertTranslation() {
......
...@@ -59,7 +59,8 @@ TranslateScript::TranslateScript() ...@@ -59,7 +59,8 @@ TranslateScript::TranslateScript()
TranslateScript::~TranslateScript() { TranslateScript::~TranslateScript() {
} }
void TranslateScript::Request(const RequestCallback& callback) { void TranslateScript::Request(const RequestCallback& callback,
bool is_incognito) {
script_fetch_start_time_ = base::Time::Now().ToJsTime(); script_fetch_start_time_ = base::Time::Now().ToJsTime();
DCHECK(data_.empty()) << "Do not fetch the script if it is already fetched"; DCHECK(data_.empty()) << "Do not fetch the script if it is already fetched";
...@@ -73,11 +74,12 @@ void TranslateScript::Request(const RequestCallback& callback) { ...@@ -73,11 +74,12 @@ void TranslateScript::Request(const RequestCallback& callback) {
GURL translate_script_url = GetTranslateScriptURL(); GURL translate_script_url = GetTranslateScriptURL();
fetcher_.reset(new TranslateURLFetcher); fetcher_ = std::make_unique<TranslateURLFetcher>();
fetcher_->set_extra_request_header(kRequestHeader); fetcher_->set_extra_request_header(kRequestHeader);
fetcher_->Request(translate_script_url, fetcher_->Request(translate_script_url,
base::BindOnce(&TranslateScript::OnScriptFetchComplete, base::BindOnce(&TranslateScript::OnScriptFetchComplete,
base::Unretained(this))); base::Unretained(this)),
is_incognito);
} }
// static // static
......
...@@ -43,8 +43,9 @@ class TranslateScript { ...@@ -43,8 +43,9 @@ class TranslateScript {
void Clear() { data_.clear(); } void Clear() { data_.clear(); }
// Fetches the JS translate script (the script that is injected in the page // Fetches the JS translate script (the script that is injected in the page
// to translate it). // to translate it). |is_incognito| is used during the fetch to determine
void Request(const RequestCallback& callback); // which variations headers to add.
void Request(const RequestCallback& callback, bool is_incognito);
// Returns the URL to be used to load the translate script. // Returns the URL to be used to load the translate script.
static GURL GetTranslateScriptURL(); static GURL GetTranslateScriptURL();
......
...@@ -46,8 +46,9 @@ class TranslateScriptTest : public testing::Test { ...@@ -46,8 +46,9 @@ class TranslateScriptTest : public testing::Test {
} }
void Request() { void Request() {
script_->Request( script_->Request(base::BindRepeating(&TranslateScriptTest::OnComplete,
base::Bind(&TranslateScriptTest::OnComplete, base::Unretained(this))); base::Unretained(this)),
/*is_incognito=*/false);
} }
const std::string& GetData() { return script_->data(); } const std::string& GetData() { return script_->data(); }
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include "base/memory/ref_counted.h" #include "base/memory/ref_counted.h"
#include "components/data_use_measurement/core/data_use_user_data.h" #include "components/data_use_measurement/core/data_use_user_data.h"
#include "components/translate/core/browser/translate_download_manager.h" #include "components/translate/core/browser/translate_download_manager.h"
#include "components/variations/net/variations_http_headers.h"
#include "net/base/load_flags.h" #include "net/base/load_flags.h"
#include "net/http/http_status_code.h" #include "net/http/http_status_code.h"
#include "net/traffic_annotation/network_traffic_annotation.h" #include "net/traffic_annotation/network_traffic_annotation.h"
...@@ -28,7 +29,8 @@ TranslateURLFetcher::TranslateURLFetcher() ...@@ -28,7 +29,8 @@ TranslateURLFetcher::TranslateURLFetcher()
TranslateURLFetcher::~TranslateURLFetcher() {} TranslateURLFetcher::~TranslateURLFetcher() {}
bool TranslateURLFetcher::Request(const GURL& url, bool TranslateURLFetcher::Request(const GURL& url,
TranslateURLFetcher::Callback callback) { TranslateURLFetcher::Callback callback,
bool is_incognito) {
// This function is not supposed to be called if the previous operation is not // This function is not supposed to be called if the previous operation is not
// finished. // finished.
if (state_ == REQUESTING) { if (state_ == REQUESTING) {
...@@ -98,8 +100,12 @@ bool TranslateURLFetcher::Request(const GURL& url, ...@@ -98,8 +100,12 @@ bool TranslateURLFetcher::Request(const GURL& url,
if (!extra_request_header_.empty()) if (!extra_request_header_.empty())
resource_request->headers.AddHeadersFromString(extra_request_header_); resource_request->headers.AddHeadersFromString(extra_request_header_);
simple_loader_ = network::SimpleURLLoader::Create(std::move(resource_request), simple_loader_ =
traffic_annotation); variations::CreateSimpleURLLoaderWithVariationsHeadersUnknownSignedIn(
std::move(resource_request),
is_incognito ? variations::InIncognito::kYes
: variations::InIncognito::kNo,
traffic_annotation);
// Set retry parameter for HTTP status code 5xx. This doesn't work against // Set retry parameter for HTTP status code 5xx. This doesn't work against
// 106 (net::ERR_INTERNET_DISCONNECTED) and so on. // 106 (net::ERR_INTERNET_DISCONNECTED) and so on.
// TranslateLanguageList handles network status, and implements retry. // TranslateLanguageList handles network status, and implements retry.
......
...@@ -52,8 +52,9 @@ class TranslateURLFetcher { ...@@ -52,8 +52,9 @@ class TranslateURLFetcher {
// Requests to |url|. |callback| will be invoked when the function returns // Requests to |url|. |callback| will be invoked when the function returns
// true, and the request is finished asynchronously. // true, and the request is finished asynchronously.
// Returns false if the previous request is not finished, or the request // Returns false if the previous request is not finished, or the request
// is omitted due to retry limitation. // is omitted due to retry limitation. |is_incognito| is used during the fetch
bool Request(const GURL& url, Callback callback); // to determine which variations headers to add.
bool Request(const GURL& url, Callback callback, bool is_incognito);
// Gets internal state. // Gets internal state.
State state() { return state_; } State state() { return state_; }
......
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