Commit 1173223d authored by Lei Zhang's avatar Lei Zhang Committed by Commit Bot

Change ProfileManager::CreateProfileHelper() to return std::unique_ptr.

Clearly indicate pointer ownership. Fix nits along the way.

Change-Id: I721a6c543197f18effe5b050885b7a9a8f3f524b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1843394Reviewed-by: default avatarSylvain Defresne <sdefresne@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
Cr-Commit-Position: refs/heads/master@{#703800}
parent 92bfdd38
...@@ -178,8 +178,9 @@ class UnittestProfileManager : public ::ProfileManagerWithoutInit { ...@@ -178,8 +178,9 @@ class UnittestProfileManager : public ::ProfileManagerWithoutInit {
} }
protected: protected:
Profile* CreateProfileHelper(const base::FilePath& path) override { std::unique_ptr<Profile> CreateProfileHelper(
return new TestingProfile(path, NULL); const base::FilePath& path) override {
return std::make_unique<TestingProfile>(path);
} }
std::unique_ptr<Profile> CreateProfileAsyncHelper( std::unique_ptr<Profile> CreateProfileAsyncHelper(
......
...@@ -44,12 +44,11 @@ class UnittestProfileManager : public ::ProfileManagerWithoutInit { ...@@ -44,12 +44,11 @@ class UnittestProfileManager : public ::ProfileManagerWithoutInit {
: ::ProfileManagerWithoutInit(user_data_dir) {} : ::ProfileManagerWithoutInit(user_data_dir) {}
protected: protected:
Profile* CreateProfileHelper(const base::FilePath& file_path) override { std::unique_ptr<Profile> CreateProfileHelper(
if (!base::PathExists(file_path)) { const base::FilePath& path) override {
if (!base::CreateDirectory(file_path)) if (!base::PathExists(path) && !base::CreateDirectory(path))
return NULL; return nullptr;
} return std::make_unique<TestingProfile>(path);
return new TestingProfile(file_path, NULL);
} }
}; };
......
...@@ -39,8 +39,8 @@ using content::WebContentsTester; ...@@ -39,8 +39,8 @@ using content::WebContentsTester;
namespace { namespace {
static const int kDefaultSourceCount = 2; constexpr int kDefaultSourceCount = 2;
static const int kThumbnailSize = 50; constexpr int kThumbnailSize = 50;
class UnittestProfileManager : public ::ProfileManagerWithoutInit { class UnittestProfileManager : public ::ProfileManagerWithoutInit {
public: public:
...@@ -48,12 +48,11 @@ class UnittestProfileManager : public ::ProfileManagerWithoutInit { ...@@ -48,12 +48,11 @@ class UnittestProfileManager : public ::ProfileManagerWithoutInit {
: ::ProfileManagerWithoutInit(user_data_dir) {} : ::ProfileManagerWithoutInit(user_data_dir) {}
protected: protected:
Profile* CreateProfileHelper(const base::FilePath& file_path) override { std::unique_ptr<Profile> CreateProfileHelper(
if (!base::PathExists(file_path)) { const base::FilePath& path) override {
if (!base::CreateDirectory(file_path)) if (!base::PathExists(path) && !base::CreateDirectory(path))
return NULL; return nullptr;
} return std::make_unique<TestingProfile>(path);
return new TestingProfile(file_path, NULL);
} }
}; };
......
...@@ -371,7 +371,7 @@ ProfileManager::~ProfileManager() { ...@@ -371,7 +371,7 @@ ProfileManager::~ProfileManager() {
// static // static
void ProfileManager::ShutdownSessionServices() { void ProfileManager::ShutdownSessionServices() {
ProfileManager* pm = g_browser_process->profile_manager(); ProfileManager* pm = g_browser_process->profile_manager();
if (!pm) // Is NULL when running unit tests. if (!pm) // Is nullptr when running unit tests.
return; return;
std::vector<Profile*> profiles(pm->GetLoadedProfiles()); std::vector<Profile*> profiles(pm->GetLoadedProfiles());
for (size_t i = 0; i < profiles.size(); ++i) for (size_t i = 0; i < profiles.size(); ++i)
...@@ -549,13 +549,13 @@ void ProfileManager::CreateProfileAsync(const base::FilePath& profile_path, ...@@ -549,13 +549,13 @@ void ProfileManager::CreateProfileAsync(const base::FilePath& profile_path,
// Make sure that this profile is not pending deletion. // Make sure that this profile is not pending deletion.
if (IsProfileDirectoryMarkedForDeletion(profile_path)) { if (IsProfileDirectoryMarkedForDeletion(profile_path)) {
if (!callback.is_null()) if (!callback.is_null())
callback.Run(NULL, Profile::CREATE_STATUS_LOCAL_FAIL); callback.Run(nullptr, Profile::CREATE_STATUS_LOCAL_FAIL);
return; return;
} }
// Create the profile if needed and collect its ProfileInfo. // Create the profile if needed and collect its ProfileInfo.
auto iter = profiles_info_.find(profile_path); auto iter = profiles_info_.find(profile_path);
ProfileInfo* info = NULL; ProfileInfo* info = nullptr;
if (iter != profiles_info_.end()) { if (iter != profiles_info_.end()) {
info = iter->second.get(); info = iter->second.get();
...@@ -1133,7 +1133,7 @@ void ProfileManager::OnProfileCreated(Profile* profile, ...@@ -1133,7 +1133,7 @@ void ProfileManager::OnProfileCreated(Profile* profile,
if (go_off_the_record) if (go_off_the_record)
profile = profile->GetOffTheRecordProfile(); profile = profile->GetOffTheRecordProfile();
} else { } else {
profile = NULL; profile = nullptr;
profiles_info_.erase(iter); profiles_info_.erase(iter);
} }
...@@ -1154,11 +1154,12 @@ void ProfileManager::OnProfileCreated(Profile* profile, ...@@ -1154,11 +1154,12 @@ void ProfileManager::OnProfileCreated(Profile* profile,
Profile::CREATE_STATUS_LOCAL_FAIL); Profile::CREATE_STATUS_LOCAL_FAIL);
} }
Profile* ProfileManager::CreateProfileHelper(const base::FilePath& path) { std::unique_ptr<Profile> ProfileManager::CreateProfileHelper(
const base::FilePath& path) {
TRACE_EVENT0("browser", "ProfileManager::CreateProfileHelper"); TRACE_EVENT0("browser", "ProfileManager::CreateProfileHelper");
return Profile::CreateProfile(path, nullptr, Profile::CREATE_MODE_SYNCHRONOUS) return Profile::CreateProfile(path, nullptr,
.release(); Profile::CREATE_MODE_SYNCHRONOUS);
} }
std::unique_ptr<Profile> ProfileManager::CreateProfileAsyncHelper( std::unique_ptr<Profile> ProfileManager::CreateProfileAsyncHelper(
...@@ -1392,12 +1393,14 @@ Profile* ProfileManager::CreateAndInitializeProfile( ...@@ -1392,12 +1393,14 @@ Profile* ProfileManager::CreateAndInitializeProfile(
// already started loading the profile but it is not fully initialized yet, // already started loading the profile but it is not fully initialized yet,
// which would make Bad Things happen if we returned it. // which would make Bad Things happen if we returned it.
CHECK(!GetProfileByPathInternal(profile_dir)); CHECK(!GetProfileByPathInternal(profile_dir));
Profile* profile = CreateProfileHelper(profile_dir); std::unique_ptr<Profile> profile = CreateProfileHelper(profile_dir);
if (profile) { if (!profile)
bool result = AddProfile(base::WrapUnique(profile)); return nullptr;
DCHECK(result);
} Profile* profile_ptr = profile.get();
return profile; bool result = AddProfile(std::move(profile));
DCHECK(result);
return profile_ptr;
} }
#if !defined(OS_ANDROID) #if !defined(OS_ANDROID)
...@@ -1410,8 +1413,7 @@ void ProfileManager::EnsureActiveProfileExistsBeforeDeletion( ...@@ -1410,8 +1413,7 @@ void ProfileManager::EnsureActiveProfileExistsBeforeDeletion(
const base::FilePath guest_profile_path = GetGuestProfilePath(); const base::FilePath guest_profile_path = GetGuestProfilePath();
Profile* last_used_profile = GetProfileByPath(last_used_profile_path); Profile* last_used_profile = GetProfileByPath(last_used_profile_path);
if (last_used_profile_path != profile_dir && if (last_used_profile_path != profile_dir &&
last_used_profile_path != guest_profile_path && last_used_profile_path != guest_profile_path && last_used_profile &&
last_used_profile != nullptr &&
!last_used_profile->IsLegacySupervised()) { !last_used_profile->IsLegacySupervised()) {
FinishDeletingProfile(profile_dir, last_used_profile_path); FinishDeletingProfile(profile_dir, last_used_profile_path);
return; return;
...@@ -1563,8 +1565,8 @@ ProfileManager::ProfileInfo* ProfileManager::RegisterProfile( ...@@ -1563,8 +1565,8 @@ ProfileManager::ProfileInfo* ProfileManager::RegisterProfile(
ProfileManager::ProfileInfo* ProfileManager::GetProfileInfoByPath( ProfileManager::ProfileInfo* ProfileManager::GetProfileInfoByPath(
const base::FilePath& path) const { const base::FilePath& path) const {
auto iter = profiles_info_.find(path); auto it = profiles_info_.find(path);
return (iter == profiles_info_.end()) ? NULL : iter->second.get(); return it != profiles_info_.end() ? it->second.get() : nullptr;
} }
void ProfileManager::AddProfileToStorage(Profile* profile) { void ProfileManager::AddProfileToStorage(Profile* profile) {
......
...@@ -36,9 +36,9 @@ class ProfileManagerObserver; ...@@ -36,9 +36,9 @@ class ProfileManagerObserver;
class ProfileManager : public content::NotificationObserver, class ProfileManager : public content::NotificationObserver,
public Profile::Delegate { public Profile::Delegate {
public: public:
typedef base::RepeatingCallback<void(Profile*, Profile::CreateStatus)> using CreateCallback =
CreateCallback; base::RepeatingCallback<void(Profile*, Profile::CreateStatus)>;
typedef base::OnceCallback<void(Profile*)> ProfileLoadedCallback; using ProfileLoadedCallback = base::OnceCallback<void(Profile*)>;
explicit ProfileManager(const base::FilePath& user_data_dir); explicit ProfileManager(const base::FilePath& user_data_dir);
~ProfileManager() override; ~ProfileManager() override;
...@@ -254,7 +254,8 @@ class ProfileManager : public content::NotificationObserver, ...@@ -254,7 +254,8 @@ class ProfileManager : public content::NotificationObserver,
// Creates a new profile by calling into the profile's profile creation // Creates a new profile by calling into the profile's profile creation
// method. Virtual so that unittests can return a TestingProfile instead // method. Virtual so that unittests can return a TestingProfile instead
// of the Profile's result. Returns null if creation fails. // of the Profile's result. Returns null if creation fails.
virtual Profile* CreateProfileHelper(const base::FilePath& path); virtual std::unique_ptr<Profile> CreateProfileHelper(
const base::FilePath& path);
// Creates a new profile asynchronously by calling into the profile's // Creates a new profile asynchronously by calling into the profile's
// asynchronous profile creation method. Virtual so that unittests can return // asynchronous profile creation method. Virtual so that unittests can return
......
...@@ -89,12 +89,11 @@ class UnittestProfileManager : public ProfileManagerWithoutInit { ...@@ -89,12 +89,11 @@ class UnittestProfileManager : public ProfileManagerWithoutInit {
~UnittestProfileManager() override = default; ~UnittestProfileManager() override = default;
protected: protected:
Profile* CreateProfileHelper(const base::FilePath& file_path) override { std::unique_ptr<Profile> CreateProfileHelper(
if (!base::PathExists(file_path)) { const base::FilePath& path) override {
if (!base::CreateDirectory(file_path)) if (!base::PathExists(path) && !base::CreateDirectory(path))
return nullptr; return nullptr;
} return std::make_unique<TestingProfile>(path);
return new TestingProfile(file_path, nullptr);
} }
std::unique_ptr<Profile> CreateProfileAsyncHelper( std::unique_ptr<Profile> CreateProfileAsyncHelper(
...@@ -579,12 +578,12 @@ class UnittestGuestProfileManager : public UnittestProfileManager { ...@@ -579,12 +578,12 @@ class UnittestGuestProfileManager : public UnittestProfileManager {
: UnittestProfileManager(user_data_dir) {} : UnittestProfileManager(user_data_dir) {}
protected: protected:
Profile* CreateProfileHelper(const base::FilePath& file_path) override { std::unique_ptr<Profile> CreateProfileHelper(
const base::FilePath& path) override {
TestingProfile::Builder builder; TestingProfile::Builder builder;
builder.SetGuestSession(); builder.SetGuestSession();
builder.SetPath(file_path); builder.SetPath(path);
TestingProfile* testing_profile = builder.Build().release(); return builder.Build();
return testing_profile;
} }
}; };
......
...@@ -27,12 +27,11 @@ class UnittestProfileManager : public ProfileManagerWithoutInit { ...@@ -27,12 +27,11 @@ class UnittestProfileManager : public ProfileManagerWithoutInit {
~UnittestProfileManager() override = default; ~UnittestProfileManager() override = default;
protected: protected:
Profile* CreateProfileHelper(const base::FilePath& file_path) override { std::unique_ptr<Profile> CreateProfileHelper(
if (!base::PathExists(file_path)) { const base::FilePath& path) override {
if (!base::CreateDirectory(file_path)) if (!base::PathExists(path) && !base::CreateDirectory(path))
return nullptr; return nullptr;
} return std::make_unique<TestingProfile>(path);
return new TestingProfile(file_path, nullptr);
} }
std::unique_ptr<Profile> CreateProfileAsyncHelper( std::unique_ptr<Profile> CreateProfileAsyncHelper(
......
...@@ -101,10 +101,11 @@ class UnittestProfileManager : public ProfileManagerWithoutInit { ...@@ -101,10 +101,11 @@ class UnittestProfileManager : public ProfileManagerWithoutInit {
: ProfileManagerWithoutInit(user_data_dir) {} : ProfileManagerWithoutInit(user_data_dir) {}
protected: protected:
Profile* CreateProfileHelper(const base::FilePath& file_path) override { std::unique_ptr<Profile> CreateProfileHelper(
if (!base::PathExists(file_path) && !base::CreateDirectory(file_path)) const base::FilePath& path) override {
if (!base::PathExists(path) && !base::CreateDirectory(path))
return nullptr; return nullptr;
return BuildTestingProfile(file_path, /*delegate=*/nullptr).release(); return BuildTestingProfile(path, /*delegate=*/nullptr);
} }
std::unique_ptr<Profile> CreateProfileAsyncHelper( std::unique_ptr<Profile> CreateProfileAsyncHelper(
......
...@@ -36,8 +36,9 @@ class ProfileManager : public ::ProfileManagerWithoutInit { ...@@ -36,8 +36,9 @@ class ProfileManager : public ::ProfileManagerWithoutInit {
: ::ProfileManagerWithoutInit(user_data_dir) {} : ::ProfileManagerWithoutInit(user_data_dir) {}
protected: protected:
Profile* CreateProfileHelper(const base::FilePath& file_path) override { std::unique_ptr<Profile> CreateProfileHelper(
return new TestingProfile(file_path); const base::FilePath& path) override {
return std::make_unique<TestingProfile>(path);
} }
}; };
......
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