Commit 38cc33ef authored by Ramin Halavati's avatar Ramin Halavati Committed by Commit Bot

Replace independent OTR profiles in Hats Survey with newer APIs.

Profile API are updated for better support of multiple off-the-record
profiles (issue 1033903) and Independent OTR profiles will be
deprecated.

This CL updates HatsHatsSurveyStatusChecker to use the newer profile
APIs and removes the dependency on Independent OTRs profiles.

Bug: 1033903
Change-Id: Ief2ff3e1462c469d6adab2de3f2f40531b245835
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2144036
Commit-Queue: Ramin Halavati <rhalavati@chromium.org>
Reviewed-by: default avatarAllen Bauer <kylixrd@chromium.org>
Cr-Commit-Position: refs/heads/master@{#759143}
parent b551eb0b
......@@ -6,6 +6,7 @@
#include "base/strings/string_util.h"
#include "base/timer/timer.h"
#include "chrome/browser/profiles/profile_destroyer.h"
#include "content/public/browser/storage_partition.h"
#include "net/base/load_flags.h"
#include "net/cookies/canonical_cookie.h"
......@@ -21,16 +22,11 @@ constexpr char HatsSurveyStatusChecker::kHatsSurveyDataPath[];
constexpr char HatsSurveyStatusChecker::kReasonHeader[];
constexpr char HatsSurveyStatusChecker::kReasonOverCapacity[];
HatsSurveyStatusChecker::HatsSurveyStatusChecker(Profile* profile)
: otr_profile_registration_(
IndependentOTRProfileManager::GetInstance()
->CreateFromOriginalProfile(
profile,
base::BindOnce(
&HatsSurveyStatusChecker::OnOriginalProfileDestroyed,
base::Unretained(this)))) {
DCHECK(
otr_profile_registration_->profile()->IsIndependentOffTheRecordProfile());
HatsSurveyStatusChecker::HatsSurveyStatusChecker(Profile* profile) {
Profile::OTRProfileID otr_profile_id =
Profile::OTRProfileID::CreateUnique("HaTS::SurveyStatusChecker");
otr_profile_ = profile->GetOffTheRecordProfile(otr_profile_id);
otr_profile_->AddObserver(this);
// HaTS client first downloads a javascript library from
// https://www.google.com/insights/consumersurveys/async_survey?site=<site_id>.
......@@ -44,17 +40,26 @@ HatsSurveyStatusChecker::HatsSurveyStatusChecker(Profile* profile)
auto survey_cookie = net::CanonicalCookie::Create(
GURL("https://www.google.com"), "PAIDCONTENT=0", base::Time::Now(),
base::nullopt);
content::StoragePartition* partition =
content::BrowserContext::GetDefaultStoragePartition(
otr_profile_registration_->profile());
network::mojom::CookieManager* cookie_manager =
partition->GetCookieManagerForBrowserProcess();
GetStoragePartition()->GetCookieManagerForBrowserProcess();
cookie_manager->SetCanonicalCookie(*survey_cookie, "https",
net::CookieOptions::MakeAllInclusive(),
base::DoNothing());
}
HatsSurveyStatusChecker::~HatsSurveyStatusChecker() = default;
HatsSurveyStatusChecker::~HatsSurveyStatusChecker() {
if (otr_profile_) {
otr_profile_->RemoveObserver(this);
ProfileDestroyer::DestroyProfileWhenAppropriate(otr_profile_);
otr_profile_ = nullptr;
}
}
content::StoragePartition* HatsSurveyStatusChecker::GetStoragePartition()
const {
DCHECK(otr_profile_);
return content::BrowserContext::GetDefaultStoragePartition(otr_profile_);
}
void HatsSurveyStatusChecker::CheckSurveyStatus(
const std::string& site_id,
......@@ -101,11 +106,8 @@ void HatsSurveyStatusChecker::CheckSurveyStatus(
policy_exception_justification:
"Not implemented."
})"));
content::StoragePartition* partition =
content::BrowserContext::GetDefaultStoragePartition(
otr_profile_registration_->profile());
url_loader_->DownloadHeadersOnly(
partition->GetURLLoaderFactoryForBrowserProcess().get(),
GetStoragePartition()->GetURLLoaderFactoryForBrowserProcess().get(),
base::BindOnce(&HatsSurveyStatusChecker::OnURLLoadComplete,
base::Unretained(this)));
request_timer_.Start(FROM_HERE,
......@@ -130,11 +132,8 @@ int HatsSurveyStatusChecker::SurveyCheckTimeoutSecs() {
return kTimeoutSecs;
}
void HatsSurveyStatusChecker::OnOriginalProfileDestroyed(Profile* profile) {
if (otr_profile_registration_ &&
profile == otr_profile_registration_->profile()) {
otr_profile_registration_.reset();
}
void HatsSurveyStatusChecker::OnProfileWillBeDestroyed(Profile* profile) {
otr_profile_ = nullptr;
}
void HatsSurveyStatusChecker::OnURLLoadComplete(
......
......@@ -10,13 +10,12 @@
#include "base/callback.h"
#include "base/callback_forward.h"
#include "base/memory/scoped_refptr.h"
#include "chrome/browser/profiles/independent_otr_profile_manager.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_observer.h"
#include "net/http/http_response_headers.h"
#include "services/network/public/cpp/simple_url_loader.h"
class Profile;
class HatsSurveyStatusChecker {
class HatsSurveyStatusChecker : public ProfileObserver {
public:
enum class Status {
kSuccess,
......@@ -35,7 +34,7 @@ class HatsSurveyStatusChecker {
explicit HatsSurveyStatusChecker(Profile* profile);
HatsSurveyStatusChecker(const HatsSurveyStatusChecker&) = delete;
virtual ~HatsSurveyStatusChecker();
~HatsSurveyStatusChecker() override;
HatsSurveyStatusChecker& operator=(const HatsSurveyStatusChecker&) = delete;
......@@ -58,15 +57,17 @@ class HatsSurveyStatusChecker {
virtual int SurveyCheckTimeoutSecs();
private:
void OnOriginalProfileDestroyed(Profile* profile);
// ProfileObserver:
void OnProfileWillBeDestroyed(Profile* profile) override;
// Callbacks for survey capacity checking.
void OnURLLoadComplete(scoped_refptr<net::HttpResponseHeaders> headers);
void OnTimeout();
content::StoragePartition* GetStoragePartition() const;
// The off the record profile used for fetching survey status.
std::unique_ptr<IndependentOTRProfileManager::OTRProfileRegistration>
otr_profile_registration_;
Profile* otr_profile_ = nullptr;
std::unique_ptr<network::SimpleURLLoader> url_loader_;
base::OnceClosure on_success_;
......
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