Commit 61f18296 authored by Patrick Noland's avatar Patrick Noland Committed by Commit Bot

[EoC] Include cookie when personalized collection is enabled

Bug: 834001
Change-Id: I4f21239a17fa91d824f143a8043a1114e6853e2f
Reviewed-on: https://chromium-review.googlesource.com/1159000
Commit-Queue: Patrick Noland <pnoland@chromium.org>
Reviewed-by: default avatarMihai Sardarescu <msarda@chromium.org>
Reviewed-by: default avatarFilip Gorski <fgorski@chromium.org>
Cr-Commit-Position: refs/heads/master@{#580938}
parent 3bcccb0a
......@@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <utility>
#include "chrome/browser/ntp_snippets/contextual_content_suggestions_service_factory.h"
#include "base/files/file_path.h"
......@@ -11,6 +13,8 @@
#include "chrome/browser/browser_process.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/search/suggestions/image_decoder_impl.h"
#include "chrome/browser/signin/unified_consent_helper.h"
#include "chrome/browser/sync/profile_sync_service_factory.h"
#include "components/image_fetcher/core/image_decoder.h"
#include "components/image_fetcher/core/image_fetcher.h"
#include "components/image_fetcher/core/image_fetcher_impl.h"
......@@ -79,6 +83,7 @@ ContextualContentSuggestionsServiceFactory::
: BrowserContextKeyedServiceFactory(
"ContextualContentSuggestionsService",
BrowserContextDependencyManager::GetInstance()) {
DependsOn(ProfileSyncServiceFactory::GetInstance());
}
ContextualContentSuggestionsServiceFactory::
......@@ -96,10 +101,20 @@ ContextualContentSuggestionsServiceFactory::BuildServiceInstanceFor(
PrefService* pref_service = profile->GetPrefs();
content::StoragePartition* storage_partition =
content::BrowserContext::GetDefaultStoragePartition(context);
std::unique_ptr<unified_consent::UrlKeyedDataCollectionConsentHelper>
consent_helper;
if (IsUnifiedConsentEnabled(profile)) {
consent_helper = unified_consent::UrlKeyedDataCollectionConsentHelper::
NewPersonalizedDataCollectionConsentHelper(
true, /*is_unified_consent_enabled*/
ProfileSyncServiceFactory::GetSyncServiceForBrowserContext(
profile));
}
auto contextual_suggestions_fetcher =
std::make_unique<ContextualSuggestionsFetcherImpl>(
storage_partition->GetURLLoaderFactoryForBrowserProcess(),
g_browser_process->GetApplicationLocale());
std::move(consent_helper), g_browser_process->GetApplicationLocale());
const base::FilePath::CharType kDatabaseFolder[] =
FILE_PATH_LITERAL("contextualSuggestionsDatabase");
base::FilePath database_dir(profile->GetPath().Append(kDatabaseFolder));
......
......@@ -168,6 +168,7 @@ static_library("ntp_snippets") {
"//components/sessions",
"//components/strings",
"//components/sync_sessions",
"//components/unified_consent",
"//components/url_formatter",
"//components/variations",
"//components/variations/net",
......
......@@ -16,6 +16,7 @@ include_rules = [
"+components/sync_preferences/testing_pref_service_syncable.h",
"+components/sync/driver",
"+components/ukm",
"+components/unified_consent",
"+components/url_formatter",
"+components/variations",
"+components/version_info",
......
......@@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <vector>
#include "components/ntp_snippets/contextual/contextual_suggestions_fetch.h"
#include "base/base64.h"
......@@ -179,8 +181,11 @@ ContextualSuggestionsResult ResultFromResponse(
ContextualSuggestionsFetch::ContextualSuggestionsFetch(
const GURL& url,
const std::string& bcp_language_code)
: url_(url), bcp_language_code_(bcp_language_code) {}
const std::string& bcp_language_code,
bool include_cookies)
: url_(url),
bcp_language_code_(bcp_language_code),
include_cookies_(include_cookies) {}
ContextualSuggestionsFetch::~ContextualSuggestionsFetch() = default;
......@@ -252,11 +257,11 @@ ContextualSuggestionsFetch::MakeURLLoader() const {
auto resource_request = std::make_unique<network::ResourceRequest>();
resource_request->url = GURL(GetFetchEndpoint());
// TODO(pnoland): include cookies once the suggestions endpoint can make use
// of them.
resource_request->load_flags =
net::LOAD_BYPASS_CACHE | net::LOAD_DO_NOT_SAVE_COOKIES |
net::LOAD_DO_NOT_SEND_COOKIES | net::LOAD_DO_NOT_SEND_AUTH_DATA;
int cookie_flag = include_cookies_ ? 0 : net::LOAD_DO_NOT_SEND_COOKIES;
resource_request->load_flags = net::LOAD_BYPASS_CACHE |
net::LOAD_DO_NOT_SAVE_COOKIES | cookie_flag |
net::LOAD_DO_NOT_SEND_AUTH_DATA;
resource_request->headers = MakeHeaders();
resource_request->method = "GET";
......
......@@ -12,6 +12,7 @@
#include "base/callback.h"
#include "components/ntp_snippets/contextual/contextual_suggestions_metrics_reporter.h"
#include "components/ntp_snippets/contextual/contextual_suggestions_result.h"
#include "net/base/load_flags.h"
#include "net/http/http_request_headers.h"
#include "url/gurl.h"
......@@ -27,7 +28,9 @@ namespace contextual_suggestions {
// body protos and parsing the response body protos.
class ContextualSuggestionsFetch {
public:
ContextualSuggestionsFetch(const GURL& url, const std::string& bcp_language);
ContextualSuggestionsFetch(const GURL& url,
const std::string& bcp_language,
bool include_cookies);
~ContextualSuggestionsFetch();
// Get the url used to fetch suggestions.
......@@ -54,6 +57,8 @@ class ContextualSuggestionsFetch {
// Identifier for the spoken language in BCP47 format.
const std::string bcp_language_code_;
bool include_cookies_ = false;
// The loader for downloading the suggestions. Only non-null if a fetch is
// currently ongoing.
std::unique_ptr<network::SimpleURLLoader> url_loader_;
......
......@@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <utility>
#include "components/ntp_snippets/contextual/contextual_suggestions_fetcher_impl.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
......@@ -10,8 +12,11 @@ namespace contextual_suggestions {
ContextualSuggestionsFetcherImpl::ContextualSuggestionsFetcherImpl(
const scoped_refptr<network::SharedURLLoaderFactory>& loader_factory,
std::unique_ptr<unified_consent::UrlKeyedDataCollectionConsentHelper>
consent_helper,
const std::string& application_language_code)
: loader_factory_(loader_factory),
consent_helper_(std::move(consent_helper)),
bcp_language_code_(application_language_code) {}
ContextualSuggestionsFetcherImpl::~ContextualSuggestionsFetcherImpl() = default;
......@@ -20,8 +25,9 @@ void ContextualSuggestionsFetcherImpl::FetchContextualSuggestionsClusters(
const GURL& url,
FetchClustersCallback callback,
ReportFetchMetricsCallback metrics_callback) {
auto fetch =
std::make_unique<ContextualSuggestionsFetch>(url, bcp_language_code_);
bool include_cookies = consent_helper_ && consent_helper_->IsEnabled();
auto fetch = std::make_unique<ContextualSuggestionsFetch>(
url, bcp_language_code_, include_cookies);
ContextualSuggestionsFetch* fetch_unowned = fetch.get();
pending_requests_.emplace(std::move(fetch));
......
......@@ -15,6 +15,7 @@
#include "components/ntp_snippets/contextual/contextual_suggestion.h"
#include "components/ntp_snippets/contextual/contextual_suggestions_fetch.h"
#include "components/ntp_snippets/contextual/contextual_suggestions_fetcher.h"
#include "components/unified_consent/url_keyed_data_collection_consent_helper.h"
namespace network {
class SharedURLLoaderFactory;
......@@ -30,6 +31,8 @@ class ContextualSuggestionsFetcherImpl : public ContextualSuggestionsFetcher {
public:
ContextualSuggestionsFetcherImpl(
const scoped_refptr<network::SharedURLLoaderFactory>& loader_factory,
std::unique_ptr<unified_consent::UrlKeyedDataCollectionConsentHelper>
consent_helper,
const std::string& application_language_code);
~ContextualSuggestionsFetcherImpl() override;
......@@ -45,6 +48,8 @@ class ContextualSuggestionsFetcherImpl : public ContextualSuggestionsFetcher {
ContextualSuggestionsResult result);
const scoped_refptr<network::SharedURLLoaderFactory> loader_factory_;
std::unique_ptr<unified_consent::UrlKeyedDataCollectionConsentHelper>
consent_helper_;
/// BCP47 formatted language code to use.
const std::string bcp_language_code_;
......
......@@ -147,18 +147,36 @@ std::string SerializedResponseProto(const std::string& peek_text,
} // namespace
class TestUrlKeyedDataCollectionConsentHelper
: public unified_consent::UrlKeyedDataCollectionConsentHelper {
public:
TestUrlKeyedDataCollectionConsentHelper() = default;
~TestUrlKeyedDataCollectionConsentHelper() override = default;
bool IsEnabled() override { return is_enabled_; }
void SetIsEnabled(bool enabled) { is_enabled_ = enabled; }
private:
bool is_enabled_ = false;
};
class ContextualSuggestionsFetcherTest : public testing::Test {
public:
ContextualSuggestionsFetcherTest() {
shared_url_loader_factory_ =
base::MakeRefCounted<network::WeakWrapperSharedURLLoaderFactory>(
&test_factory_);
auto consent_helper =
std::make_unique<TestUrlKeyedDataCollectionConsentHelper>();
consent_helper_ = consent_helper.get();
fetcher_ = std::make_unique<ContextualSuggestionsFetcherImpl>(
shared_url_loader_factory_, "en");
shared_url_loader_factory_, std::move(consent_helper), "en");
}
~ContextualSuggestionsFetcherTest() override {}
void SetUp() override { consent_helper()->SetIsEnabled(true); }
void SetFakeResponse(const std::string& response_data,
net::HttpStatusCode response_code = net::HTTP_OK,
network::URLLoaderCompletionStatus status =
......@@ -192,10 +210,15 @@ class ContextualSuggestionsFetcherTest : public testing::Test {
TestURLLoaderFactory* test_factory() { return &test_factory_; }
TestUrlKeyedDataCollectionConsentHelper* consent_helper() {
return consent_helper_;
}
private:
base::test::ScopedTaskEnvironment scoped_task_environment_;
network::TestURLLoaderFactory test_factory_;
scoped_refptr<network::SharedURLLoaderFactory> shared_url_loader_factory_;
TestUrlKeyedDataCollectionConsentHelper* consent_helper_;
std::unique_ptr<ContextualSuggestionsFetcherImpl> fetcher_;
DISALLOW_COPY_AND_ASSIGN(ContextualSuggestionsFetcherTest);
......@@ -412,6 +435,42 @@ TEST_F(ContextualSuggestionsFetcherTest, RequestHeaderSetCorrectly) {
"ContextualSuggestions.FetchRequestProtoSizeKB", 1);
}
TEST_F(ContextualSuggestionsFetcherTest, CookiesIncludedWhenConsentIsEnabled) {
network::ResourceRequest last_resource_request;
test_factory()->SetInterceptor(
base::BindLambdaForTesting([&](const network::ResourceRequest& request) {
last_resource_request = request;
}));
SetFakeResponse(SerializedResponseProto("Peek Text", DefaultClusters()));
MockClustersCallback callback;
SendAndAwaitResponse(GURL("http://www.article.com/"), &callback);
int load_flags = last_resource_request.load_flags;
EXPECT_EQ(0, load_flags & net::LOAD_DO_NOT_SEND_COOKIES);
}
TEST_F(ContextualSuggestionsFetcherTest, CookiesExcludedWhenConsentIsDisabled) {
consent_helper()->SetIsEnabled(false);
network::ResourceRequest last_resource_request;
test_factory()->SetInterceptor(
base::BindLambdaForTesting([&](const network::ResourceRequest& request) {
last_resource_request = request;
}));
SetFakeResponse(SerializedResponseProto("Peek Text", DefaultClusters()));
MockClustersCallback callback;
SendAndAwaitResponse(GURL("http://www.article.com/"), &callback);
int load_flags = last_resource_request.load_flags;
EXPECT_EQ(net::LOAD_DO_NOT_SEND_COOKIES,
load_flags & net::LOAD_DO_NOT_SEND_COOKIES);
}
TEST_F(ContextualSuggestionsFetcherTest, ProtocolError) {
MockClustersCallback callback;
MockMetricsCallback metrics_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