Commit 16f6892a authored by Jan Krcal's avatar Jan Krcal Committed by Commit Bot

[NTP] Expose request priority as a get parameter in fetch url.

In addition to setting request priority in the request itself, add it as
a get parameter to the fetch url.

This is needed for load shedding later.

Bug: 809986
Change-Id: I4d6520218d6ef9d25feada409cf894485e3e2872
Reviewed-on: https://chromium-review.googlesource.com/913618Reviewed-by: default avatarChris Pickel <sfiera@chromium.org>
Commit-Queue: Jan Krcal <jkrcal@chromium.org>
Cr-Commit-Position: refs/heads/master@{#537286}
parent c6c67160
......@@ -98,6 +98,8 @@ class JsonRequest : public net::URLFetcherDelegate {
return *this;
}
bool is_interactive_request() const { return params_.interactive_request; }
private:
std::string BuildHeaders() const;
std::string BuildBody() const;
......
......@@ -13,8 +13,11 @@
#include "base/values.h"
#include "components/language/core/browser/url_language_histogram.h"
#include "components/ntp_snippets/category.h"
#include "components/ntp_snippets/features.h"
#include "components/ntp_snippets/ntp_snippets_constants.h"
#include "components/ntp_snippets/user_classifier.h"
#include "components/variations/variations_associated_data.h"
#include "net/base/url_util.h"
#include "net/url_request/url_fetcher.h"
#include "net/url_request/url_request_status.h"
#include "services/identity/public/cpp/identity_manager.h"
......@@ -32,11 +35,38 @@ using internal::JsonRequest;
namespace {
const char kSnippetsServerNonAuthorizedFormat[] = "%s?key=%s";
const char kApiKeyQueryParam[] = "key";
const char kPriorityQueryParam[] = "priority";
const char kInteractivePriority[] = "user_action";
const char kNonInteractivePriority[] = "background_prefetch";
const char kAuthorizationRequestHeaderFormat[] = "Bearer %s";
const int kFetchTimeHistogramResolution = 5;
// Enables appending request priority as a query parameter to the fetch url,
// when fetching article suggestions.
const char kAppendRequestPriorityAsQueryParameterParamName[] =
"append_request_priority_as_query_parameter";
const bool kAppendRequestPriorityAsQueryParameterParamDefault = true;
bool IsAppendingRequestPriorityAsQueryParameterEnabled() {
return variations::GetVariationParamByFeatureAsBool(
ntp_snippets::kArticleSuggestionsFeature,
kAppendRequestPriorityAsQueryParameterParamName,
kAppendRequestPriorityAsQueryParameterParamDefault);
}
GURL AppendPriorityQueryParameterIfEnabled(const GURL& url,
bool is_interactive_request) {
if (IsAppendingRequestPriorityAsQueryParameterEnabled()) {
return net::AppendQueryParameter(url, kPriorityQueryParam,
is_interactive_request
? kInteractivePriority
: kNonInteractivePriority);
}
return url;
}
std::string FetchResultToString(FetchResult result) {
switch (result) {
case FetchResult::SUCCESS:
......@@ -202,9 +232,11 @@ void RemoteSuggestionsFetcherImpl::FetchSnippetsNonAuthenticated(
return;
}
// When not providing OAuth token, we need to pass the Google API key.
builder.SetUrl(
GURL(base::StringPrintf(kSnippetsServerNonAuthorizedFormat,
fetch_url_.spec().c_str(), api_key_.c_str())));
GURL url = net::AppendQueryParameter(fetch_url_, kApiKeyQueryParam, api_key_);
url = AppendPriorityQueryParameterIfEnabled(url,
builder.is_interactive_request());
builder.SetUrl(url);
StartRequest(std::move(builder), std::move(callback),
/*is_authenticated=*/false);
}
......@@ -213,8 +245,11 @@ void RemoteSuggestionsFetcherImpl::FetchSnippetsAuthenticated(
JsonRequest::Builder builder,
SnippetsAvailableCallback callback,
const std::string& oauth_access_token) {
builder.SetUrl(fetch_url_)
.SetAuthentication(identity_manager_->GetPrimaryAccountInfo().account_id,
GURL url = AppendPriorityQueryParameterIfEnabled(
fetch_url_, builder.is_interactive_request());
builder.SetUrl(url).SetAuthentication(
identity_manager_->GetPrimaryAccountInfo().account_id,
base::StringPrintf(kAuthorizationRequestHeaderFormat,
oauth_access_token.c_str()));
StartRequest(std::move(builder), std::move(callback),
......
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