Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(183)

Unified Diff: chrome/test/base/testing_profile.cc

Issue 556173002: Ensure incognito TestingProfiles are incognito for their whole lifetime. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: respond to comments Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « chrome/test/base/testing_profile.h ('k') | chrome/test/base/testing_profile_manager.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/test/base/testing_profile.cc
diff --git a/chrome/test/base/testing_profile.cc b/chrome/test/base/testing_profile.cc
index 84aa2519a04bdd64e3d25238ac7553df235557ec..335b68548a3eb95281ed1937d24f3f7e0a68b371 100644
--- a/chrome/test/base/testing_profile.cc
+++ b/chrome/test/base/testing_profile.cc
@@ -184,7 +184,6 @@ const char TestingProfile::kTestUserProfileDir[] = "Default";
TestingProfile::TestingProfile()
: start_time_(Time::Now()),
testing_prefs_(NULL),
- incognito_(false),
force_incognito_(false),
original_profile_(NULL),
guest_session_(false),
@@ -203,7 +202,6 @@ TestingProfile::TestingProfile()
TestingProfile::TestingProfile(const base::FilePath& path)
: start_time_(Time::Now()),
testing_prefs_(NULL),
- incognito_(false),
force_incognito_(false),
original_profile_(NULL),
guest_session_(false),
@@ -221,7 +219,6 @@ TestingProfile::TestingProfile(const base::FilePath& path,
Delegate* delegate)
: start_time_(Time::Now()),
testing_prefs_(NULL),
- incognito_(false),
force_incognito_(false),
original_profile_(NULL),
guest_session_(false),
@@ -248,7 +245,7 @@ TestingProfile::TestingProfile(
scoped_refptr<ExtensionSpecialStoragePolicy> extension_policy,
#endif
scoped_ptr<PrefServiceSyncable> prefs,
- bool incognito,
+ TestingProfile* parent,
bool guest_session,
const std::string& supervised_user_id,
scoped_ptr<policy::PolicyService> policy_service,
@@ -256,9 +253,8 @@ TestingProfile::TestingProfile(
: start_time_(Time::Now()),
prefs_(prefs.release()),
testing_prefs_(NULL),
- incognito_(incognito),
force_incognito_(false),
- original_profile_(NULL),
+ original_profile_(parent),
guest_session_(guest_session),
supervised_user_id_(supervised_user_id),
last_session_exited_cleanly_(true),
@@ -271,6 +267,9 @@ TestingProfile::TestingProfile(
resource_context_(NULL),
delegate_(delegate),
policy_service_(policy_service.release()) {
+ if (parent)
+ parent->SetOffTheRecordProfile(scoped_ptr<Profile>(this));
+
// If no profile path was supplied, create one.
if (profile_path_.empty()) {
CreateTempProfileDir();
@@ -342,6 +341,8 @@ void TestingProfile::Init() {
if (prefs_.get())
user_prefs::UserPrefs::Set(this, prefs_.get());
+ else if (IsOffTheRecord())
+ CreateIncognitoPrefService();
else
CreateTestingPrefService();
@@ -359,10 +360,10 @@ void TestingProfile::Init() {
this, extensions::TestExtensionSystem::Build);
#endif
- // If no original profile was specified for this profile: register preferences
- // even if this is an incognito profile - this allows tests to create a
- // standalone incognito profile while still having prefs registered.
- if (!IsOffTheRecord() || !original_profile_) {
+ // Prefs for incognito profiles are set in CreateIncognitoPrefService() by
+ // simulating ProfileImpl::GetOffTheRecordPrefs().
+ if (!IsOffTheRecord()) {
+ DCHECK(!original_profile_);
user_prefs::PrefRegistrySyncable* pref_registry =
static_cast<user_prefs::PrefRegistrySyncable*>(
prefs_->DeprecatedGetPrefRegistry());
@@ -380,11 +381,13 @@ void TestingProfile::Init() {
#endif
#if defined(ENABLE_MANAGED_USERS)
- SupervisedUserSettingsService* settings_service =
- SupervisedUserSettingsServiceFactory::GetForProfile(this);
- TestingPrefStore* store = new TestingPrefStore();
- settings_service->Init(store);
- store->SetInitializationCompleted();
+ if (!IsOffTheRecord()) {
+ SupervisedUserSettingsService* settings_service =
+ SupervisedUserSettingsServiceFactory::GetForProfile(this);
+ TestingPrefStore* store = new TestingPrefStore();
+ settings_service->Init(store);
+ store->SetInitializationCompleted();
+ }
#endif
profile_name_ = "testing_profile";
@@ -409,6 +412,9 @@ TestingProfile::~TestingProfile() {
// Revert to non-incognito mode before shutdown.
force_incognito_ = false;
+ // If this profile owns an incognito profile, tear it down first.
+ incognito_profile_.reset();
+
// Any objects holding live URLFetchers should be deleted before teardown.
TemplateURLFetcherFactory::ShutdownForProfile(this);
@@ -601,8 +607,7 @@ scoped_refptr<base::SequencedTaskRunner> TestingProfile::GetIOTaskRunner() {
}
TestingPrefServiceSyncable* TestingProfile::GetTestingPrefService() {
- if (!prefs_.get())
- CreateTestingPrefService();
+ DCHECK(prefs_);
DCHECK(testing_prefs_);
return testing_prefs_;
}
@@ -618,35 +623,26 @@ std::string TestingProfile::GetProfileName() {
Profile::ProfileType TestingProfile::GetProfileType() const {
if (guest_session_)
return GUEST_PROFILE;
- if (force_incognito_ || incognito_)
+ if (force_incognito_ || original_profile_)
return INCOGNITO_PROFILE;
return REGULAR_PROFILE;
}
bool TestingProfile::IsOffTheRecord() const {
- return force_incognito_ || incognito_;
+ return force_incognito_ || original_profile_;
}
void TestingProfile::SetOffTheRecordProfile(scoped_ptr<Profile> profile) {
DCHECK(!IsOffTheRecord());
+ DCHECK_EQ(this, profile->GetOriginalProfile());
incognito_profile_ = profile.Pass();
}
-void TestingProfile::SetOriginalProfile(Profile* profile) {
- DCHECK(IsOffTheRecord());
- original_profile_ = profile;
-}
-
Profile* TestingProfile::GetOffTheRecordProfile() {
if (IsOffTheRecord())
return this;
- if (!incognito_profile_) {
- TestingProfile::Builder builder;
- builder.SetIncognito();
- scoped_ptr<TestingProfile> incognito_test_profile(builder.Build());
- incognito_test_profile->SetOriginalProfile(this);
- SetOffTheRecordProfile(incognito_test_profile.PassAs<Profile>());
- }
+ if (!incognito_profile_)
+ TestingProfile::Builder().BuildIncognito(this);
return incognito_profile_.get();
}
@@ -697,6 +693,15 @@ void TestingProfile::CreateTestingPrefService() {
chrome::RegisterUserProfilePrefs(testing_prefs_->registry());
}
+void TestingProfile::CreateIncognitoPrefService() {
+ DCHECK(original_profile_);
+ DCHECK(!testing_prefs_);
+ // Simplified version of ProfileImpl::GetOffTheRecordPrefs(). Note this
+ // leaves testing_prefs_ unset.
+ prefs_.reset(original_profile_->prefs_->CreateIncognitoPrefService(NULL));
+ user_prefs::UserPrefs::Set(this, prefs_.get());
+}
+
void TestingProfile::CreateProfilePolicyConnector() {
#if defined(ENABLE_CONFIGURATION_POLICY)
schema_registry_service_ =
@@ -723,9 +728,7 @@ if (!policy_service_) {
}
PrefService* TestingProfile::GetPrefs() {
- if (!prefs_.get()) {
- CreateTestingPrefService();
- }
+ DCHECK(prefs_);
return prefs_.get();
}
@@ -921,7 +924,6 @@ Profile::ExitType TestingProfile::GetLastSessionExitType() {
TestingProfile::Builder::Builder()
: build_called_(false),
delegate_(NULL),
- incognito_(false),
guest_session_(false) {
}
@@ -948,10 +950,6 @@ void TestingProfile::Builder::SetPrefService(
pref_service_ = prefs.Pass();
}
-void TestingProfile::Builder::SetIncognito() {
- incognito_ = true;
-}
-
void TestingProfile::Builder::SetGuestSession() {
guest_session_ = true;
}
@@ -976,16 +974,35 @@ scoped_ptr<TestingProfile> TestingProfile::Builder::Build() {
DCHECK(!build_called_);
build_called_ = true;
- return scoped_ptr<TestingProfile>(new TestingProfile(
- path_,
- delegate_,
+ return scoped_ptr<TestingProfile>(new TestingProfile(path_,
+ delegate_,
+#if defined(ENABLE_EXTENSIONS)
+ extension_policy_,
+#endif
+ pref_service_.Pass(),
+ NULL,
+ guest_session_,
+ supervised_user_id_,
+ policy_service_.Pass(),
+ testing_factories_));
+}
+
+TestingProfile* TestingProfile::Builder::BuildIncognito(
+ TestingProfile* original_profile) {
+ DCHECK(!build_called_);
+ DCHECK(original_profile);
+ build_called_ = true;
+
+ // Note: Owned by |original_profile|.
+ return new TestingProfile(path_,
+ delegate_,
#if defined(ENABLE_EXTENSIONS)
- extension_policy_,
+ extension_policy_,
#endif
- pref_service_.Pass(),
- incognito_,
- guest_session_,
- supervised_user_id_,
- policy_service_.Pass(),
- testing_factories_));
+ pref_service_.Pass(),
+ original_profile,
+ guest_session_,
+ supervised_user_id_,
+ policy_service_.Pass(),
+ testing_factories_);
}
« no previous file with comments | « chrome/test/base/testing_profile.h ('k') | chrome/test/base/testing_profile_manager.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698