Commit 034cff69 authored by Gheorghe Comanici's avatar Gheorghe Comanici Committed by Commit Bot

Reland "Add timestamp to requests for experimental contextual suggestions.

https://chromium.googlesource.com/chromium/src/+/e76e0e99dc3b8b2b7ad502603331ea7104745a1a

This reverts commit 8e96399b.

The original CL was reverted because of two consecutive unconditional returns within the same function, which was causing Windows MSVC build failures. This CL adds the preprocessor command to avoid this issue.

TBR=rohitrao
for mechanical changes to ios/c/b/a/*

Change-Id: I823b815ae29f154df25391c21c0c43ec485192d5
Reviewed-on: https://chromium-review.googlesource.com/889698Reviewed-by: default avatarMark Pearson <mpearson@chromium.org>
Commit-Queue: Gheorghe Comanici <gcomanici@chromium.org>
Cr-Commit-Position: refs/heads/master@{#532145}
parent ef61a51b
......@@ -37,6 +37,7 @@
#include "components/prefs/pref_service.h"
#include "components/signin/core/browser/signin_manager.h"
#include "components/sync/driver/sync_service_utils.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/service_worker_context.h"
#include "content/public/browser/storage_partition.h"
......@@ -225,6 +226,30 @@ ChromeAutocompleteProviderClient::GetBuiltinsToProvideAsUserTypes() {
return builtins_to_provide;
}
base::Time ChromeAutocompleteProviderClient::GetCurrentVisitTimestamp() const {
// The timestamp is currenly used only for contextual zero suggest suggestions
// on desktop. Consider updating this if this will be used for mobile services.
#if !defined(OS_ANDROID)
const Browser* active_browser = BrowserList::GetInstance()->GetLastActive();
if (!active_browser)
return base::Time();
const content::WebContents* active_tab =
active_browser->tab_strip_model()->GetActiveWebContents();
if (!active_tab)
return base::Time();
const content::NavigationEntry* navigation =
active_tab->GetController().GetLastCommittedEntry();
if (!navigation)
return base::Time();
return navigation->GetTimestamp();
#else
return base::Time();
#endif // !defined(OS_ANDROID)
}
bool ChromeAutocompleteProviderClient::IsOffTheRecord() const {
return profile_->IsOffTheRecord();
}
......
......@@ -45,6 +45,9 @@ class ChromeAutocompleteProviderClient : public AutocompleteProviderClient {
std::string GetEmbedderRepresentationOfAboutScheme() override;
std::vector<base::string16> GetBuiltinURLs() override;
std::vector<base::string16> GetBuiltinsToProvideAsUserTypes() override;
// GetCurrentVisitTimestamp is only implemented for desktop users. For mobile
// users, the function returns base::Time().
base::Time GetCurrentVisitTimestamp() const override;
bool IsOffTheRecord() const override;
bool SearchSuggestEnabled() const override;
bool TabSyncEnabledAndUnencrypted() const override;
......
......@@ -170,6 +170,7 @@ static_library("browser") {
"//components/sessions",
"//components/signin/core/browser",
"//components/strings",
"//components/sync",
"//components/toolbar",
"//components/url_formatter",
"//components/variations",
......
......@@ -16,6 +16,7 @@ include_rules = [
"+components/search",
"+components/search_engines",
"+components/sessions",
"+components/sync",
"+components/signin/core/browser",
"+components/strings/grit/components_strings.h",
"+components/toolbar",
......
......@@ -92,6 +92,10 @@ class AutocompleteProviderClient {
// most commonly-used URLs from that set.
virtual std::vector<base::string16> GetBuiltinsToProvideAsUserTypes() = 0;
// The timestamp for the last visit of the page being displayed in the current
// tab.
virtual base::Time GetCurrentVisitTimestamp() const = 0;
virtual bool IsOffTheRecord() const = 0;
virtual bool SearchSuggestEnabled() const = 0;
......
......@@ -15,6 +15,7 @@
#include "components/data_use_measurement/core/data_use_user_data.h"
#include "components/omnibox/browser/omnibox_field_trial.h"
#include "components/search_engines/template_url_service.h"
#include "components/sync/base/time.h"
#include "components/variations/net/variations_http_headers.h"
#include "net/base/escape.h"
#include "net/base/load_flags.h"
......@@ -56,18 +57,22 @@ void AddVariationHeaders(const std::unique_ptr<net::URLFetcher>& fetcher) {
//
// urls: {
// url : <current_url>
// // timestamp_usec is the timestamp for the page visit time.
// timestamp_usec: <visit_time>
// }
// // stream_type = 1 corresponds to zero suggest suggestions.
// stream_type: 1
// // experiment_id is only set when <experiment_id> is well defined.
// experiment_id: <experiment_id>
//
std::string FormatRequestBodyExperimentalService(
const std::string& current_url) {
std::string FormatRequestBodyExperimentalService(const std::string& current_url,
const base::Time& visit_time) {
auto request = std::make_unique<base::DictionaryValue>();
auto url_list = std::make_unique<base::ListValue>();
auto url_entry = std::make_unique<base::DictionaryValue>();
url_entry->SetString("url", current_url);
url_entry->SetString("timestamp_usec",
std::to_string(syncer::TimeToProtoTime(visit_time)));
url_list->Append(std::move(url_entry));
request->Set("urls", std::move(url_list));
// stream_type = 1 corresponds to zero suggest suggestions.
......@@ -96,13 +101,14 @@ ContextualSuggestionsService::~ContextualSuggestionsService() {}
void ContextualSuggestionsService::CreateContextualSuggestionsRequest(
const std::string& current_url,
const base::Time& visit_time,
const TemplateURLService* template_url_service,
net::URLFetcherDelegate* fetcher_delegate,
ContextualSuggestionsCallback callback) {
const GURL experimental_suggest_url =
ExperimentalContextualSuggestionsUrl(current_url, template_url_service);
if (experimental_suggest_url.is_valid())
CreateExperimentalRequest(current_url, experimental_suggest_url,
CreateExperimentalRequest(current_url, visit_time, experimental_suggest_url,
fetcher_delegate, std::move(callback));
else
CreateDefaultRequest(current_url, template_url_service, fetcher_delegate,
......@@ -234,6 +240,7 @@ void ContextualSuggestionsService::CreateDefaultRequest(
void ContextualSuggestionsService::CreateExperimentalRequest(
const std::string& current_url,
const base::Time& visit_time,
const GURL& suggest_url,
net::URLFetcherDelegate* fetcher_delegate,
ContextualSuggestionsCallback callback) {
......@@ -272,7 +279,8 @@ void ContextualSuggestionsService::CreateExperimentalRequest(
}
})");
const int kFetcherID = 1;
std::string request_body = FormatRequestBodyExperimentalService(current_url);
std::string request_body =
FormatRequestBodyExperimentalService(current_url, visit_time);
std::unique_ptr<net::URLFetcher> fetcher =
net::URLFetcher::Create(kFetcherID, suggest_url,
/*request_type=*/net::URLFetcher::POST,
......
......@@ -39,6 +39,9 @@ class ContextualSuggestionsService : public KeyedService {
// experimental suggestions service. It's possible the non-experimental
// service may decide to offer general-purpose suggestions.
//
// |visit_time| is the time of the visit for the URL for which suggestions
// should be fetched.
//
// |template_url_service| may be null, but some services may be disabled.
//
// |fetcher_delegate| is used to create a fetcher that is used to perform a
......@@ -55,6 +58,7 @@ class ContextualSuggestionsService : public KeyedService {
// instantiates |token_fetcher_|.
void CreateContextualSuggestionsRequest(
const std::string& current_url,
const base::Time& visit_time,
const TemplateURLService* template_url_service,
net::URLFetcherDelegate* fetcher_delegate,
ContextualSuggestionsCallback callback);
......@@ -112,6 +116,7 @@ class ContextualSuggestionsService : public KeyedService {
// This function is called by CreateContextualSuggestionsRequest. See its
// function definition for details on the parameters.
void CreateExperimentalRequest(const std::string& current_url,
const base::Time& visit_time,
const GURL& suggest_url,
net::URLFetcherDelegate* fetcher_delegate,
ContextualSuggestionsCallback callback);
......
......@@ -72,6 +72,7 @@ class MockAutocompleteProviderClient : public AutocompleteProviderClient {
MOCK_METHOD0(GetEmbedderRepresentationOfAboutScheme, std::string());
MOCK_METHOD0(GetBuiltinURLs, std::vector<base::string16>());
MOCK_METHOD0(GetBuiltinsToProvideAsUserTypes, std::vector<base::string16>());
MOCK_CONST_METHOD0(GetCurrentVisitTimestamp, base::Time());
MOCK_CONST_METHOD0(IsOffTheRecord, bool());
MOCK_CONST_METHOD0(SearchSuggestEnabled, bool());
MOCK_CONST_METHOD0(TabSyncEnabledAndUnencrypted, bool());
......
......@@ -182,7 +182,8 @@ void ZeroSuggestProvider::Start(const AutocompleteInput& input,
client()
->GetContextualSuggestionsService(/*create_if_necessary=*/true)
->CreateContextualSuggestionsRequest(
current_url, client()->GetTemplateURLService(),
current_url, client()->GetCurrentVisitTimestamp(),
client()->GetTemplateURLService(),
/*fetcher_delegate=*/this,
base::BindOnce(
&ZeroSuggestProvider::OnContextualSuggestionsFetcherAvailable,
......
......@@ -144,6 +144,10 @@ AutocompleteProviderClientImpl::GetBuiltinsToProvideAsUserTypes() {
base::ASCIIToUTF16(kChromeUIVersionURL)};
}
base::Time AutocompleteProviderClientImpl::GetCurrentVisitTimestamp() const {
return base::Time();
}
bool AutocompleteProviderClientImpl::IsOffTheRecord() const {
return browser_state_->IsOffTheRecord();
}
......
......@@ -46,6 +46,9 @@ class AutocompleteProviderClientImpl : public AutocompleteProviderClient {
std::string GetEmbedderRepresentationOfAboutScheme() override;
std::vector<base::string16> GetBuiltinURLs() override;
std::vector<base::string16> GetBuiltinsToProvideAsUserTypes() override;
// GetCurrentVisitTimestamp is only used by the contextual zero suggest
// suggestions for desktop users. This implementation returns base::Time().
base::Time GetCurrentVisitTimestamp() const override;
bool IsOffTheRecord() const override;
bool SearchSuggestEnabled() const override;
bool TabSyncEnabledAndUnencrypted() const override;
......
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