ProfileManager::RegisterTestingProfile() now takes a std::unique_ptr

This commit clarifies previously established ownership semantics of
Profiles within ProfileManager through the use of unique pointers.
Changes were made to the parameters of
ProfileManager::RegisterTestingProfile() and
ProfileManager::RegisterProfile() to take a std::unique_ptr instead of a
raw pointer. A further change was made to the return type of
ProfileManager::CreateProfileAsyncHelper() from a raw pointer to a
unique pointer. All callers to the aforementioned functions (including
unit tests) that made calls to the two functions are updated to pass in
unique pointers.

Bug: 968187
Change-Id: I05cab32d75fa8aad30359e8cc5b4a85efaaf9d12
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1639183
Reviewed-by: Lei Zhang <[email protected]>
Commit-Queue: Daniel Hosseinian <[email protected]>
Cr-Commit-Position: refs/heads/master@{#667232}
diff --git a/chrome/browser/app_controller_mac_browsertest.mm b/chrome/browser/app_controller_mac_browsertest.mm
index fcbe58d1..0a0bf508b 100644
--- a/chrome/browser/app_controller_mac_browsertest.mm
+++ b/chrome/browser/app_controller_mac_browsertest.mm
@@ -701,12 +701,12 @@
   // Create profile 2.
   base::ScopedAllowBlockingForTesting allow_blocking;
   base::FilePath path2 = profile_manager->GenerateNextProfileDirectoryPath();
-  Profile* profile2 =
-      Profile::CreateProfile(path2, NULL, Profile::CREATE_MODE_SYNCHRONOUS)
-          .release();
-  profile_manager->RegisterTestingProfile(profile2, false, true);
+  std::unique_ptr<Profile> profile2 =
+      Profile::CreateProfile(path2, NULL, Profile::CREATE_MODE_SYNCHRONOUS);
+  Profile* profile2_ptr = profile2.get();
+  profile_manager->RegisterTestingProfile(std::move(profile2), false, true);
   bookmarks::test::WaitForBookmarkModelToLoad(
-      BookmarkModelFactory::GetForBrowserContext(profile2));
+      BookmarkModelFactory::GetForBrowserContext(profile2_ptr));
 
   // Switch to profile 1, create bookmark 1 and force the menu to build.
   [ac windowChangedToProfile:profile1];
@@ -717,7 +717,7 @@
   [[profile1_submenu delegate] menuNeedsUpdate:profile1_submenu];
 
   // Switch to profile 2, create bookmark 2 and force the menu to build.
-  [ac windowChangedToProfile:profile2];
+  [ac windowChangedToProfile:profile2_ptr];
   [ac bookmarkMenuBridge]->GetBookmarkModel()->AddURL(
       [ac bookmarkMenuBridge]->GetBookmarkModel()->bookmark_bar_node(),
       0, title2, url2);
diff --git a/chrome/browser/chromeos/lock_screen_apps/lock_screen_profile_creator_impl_unittest.cc b/chrome/browser/chromeos/lock_screen_apps/lock_screen_profile_creator_impl_unittest.cc
index c8182ce..d0886ece 100644
--- a/chrome/browser/chromeos/lock_screen_apps/lock_screen_profile_creator_impl_unittest.cc
+++ b/chrome/browser/chromeos/lock_screen_apps/lock_screen_profile_creator_impl_unittest.cc
@@ -182,8 +182,9 @@
     return new TestingProfile(path, NULL);
   }
 
-  Profile* CreateProfileAsyncHelper(const base::FilePath& path,
-                                    Delegate* delegate) override {
+  std::unique_ptr<Profile> CreateProfileAsyncHelper(
+      const base::FilePath& path,
+      Delegate* delegate) override {
     pending_profile_creation_.Set(path, delegate);
 
     auto new_profile =
@@ -195,7 +196,7 @@
     incognito_builder.SetPath(path);
     incognito_builder.BuildIncognito(new_profile.get());
 
-    return new_profile.release();
+    return new_profile;
   }
 
  private:
@@ -337,7 +338,7 @@
         std::make_unique<TestingProfile>(user_profile_path);
     primary_profile_ = primary_profile.get();
     profile_manager_->RegisterTestingProfile(
-        primary_profile.release(), false /*add_to_storage*/,
+        std::move(primary_profile), false /*add_to_storage*/,
         false /*start_deferred_task_runner*/);
     InitExtensionSystem(primary_profile_);
 
diff --git a/chrome/browser/lifetime/browser_close_manager_browsertest.cc b/chrome/browser/lifetime/browser_close_manager_browsertest.cc
index 5532e35fb..abcfa1f 100644
--- a/chrome/browser/lifetime/browser_close_manager_browsertest.cc
+++ b/chrome/browser/lifetime/browser_close_manager_browsertest.cc
@@ -1146,7 +1146,8 @@
         Profile::CreateProfile(path, NULL, Profile::CREATE_MODE_SYNCHRONOUS);
   }
   Profile* other_profile_ptr = other_profile.get();
-  profile_manager->RegisterTestingProfile(other_profile.release(), true, false);
+  profile_manager->RegisterTestingProfile(std::move(other_profile), true,
+                                          false);
   Browser* other_profile_browser = CreateBrowser(other_profile_ptr);
 
   ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser()));
diff --git a/chrome/browser/notifications/notification_platform_bridge_win_interactive_uitest.cc b/chrome/browser/notifications/notification_platform_bridge_win_interactive_uitest.cc
index f072e05..9d455c58 100644
--- a/chrome/browser/notifications/notification_platform_bridge_win_interactive_uitest.cc
+++ b/chrome/browser/notifications/notification_platform_bridge_win_interactive_uitest.cc
@@ -63,7 +63,7 @@
   std::unique_ptr<Profile> profile =
       Profile::CreateProfile(path, nullptr, Profile::CREATE_MODE_SYNCHRONOUS);
   Profile* profile_ptr = profile.get();
-  profile_manager->RegisterTestingProfile(profile.release(), true, false);
+  profile_manager->RegisterTestingProfile(std::move(profile), true, false);
   EXPECT_EQ(starting_number_of_profiles + 1,
             profile_manager->GetNumberOfProfiles());
   return profile_ptr;
diff --git a/chrome/browser/profiles/profile_manager.cc b/chrome/browser/profiles/profile_manager.cc
index 4cce9ca9..aeabf03 100644
--- a/chrome/browser/profiles/profile_manager.cc
+++ b/chrome/browser/profiles/profile_manager.cc
@@ -1093,17 +1093,18 @@
 #endif  // !defined(OS_ANDROID)
 }
 
-void ProfileManager::RegisterTestingProfile(Profile* profile,
+void ProfileManager::RegisterTestingProfile(std::unique_ptr<Profile> profile,
                                             bool add_to_storage,
                                             bool start_deferred_task_runners) {
-  RegisterProfile(profile, true);
+  Profile* profile_ptr = profile.get();
+  RegisterProfile(std::move(profile), true);
   if (add_to_storage) {
-    InitProfileUserPrefs(profile);
-    AddProfileToStorage(profile);
+    InitProfileUserPrefs(profile_ptr);
+    AddProfileToStorage(profile_ptr);
   }
   if (start_deferred_task_runners) {
-    StartupTaskRunnerServiceFactory::GetForProfile(profile)->
-        StartDeferredTaskRunners();
+    StartupTaskRunnerServiceFactory::GetForProfile(profile_ptr)
+        ->StartDeferredTaskRunners();
   }
 }
 
@@ -1412,11 +1413,11 @@
       .release();
 }
 
-Profile* ProfileManager::CreateProfileAsyncHelper(const base::FilePath& path,
-                                                  Delegate* delegate) {
+std::unique_ptr<Profile> ProfileManager::CreateProfileAsyncHelper(
+    const base::FilePath& path,
+    Delegate* delegate) {
   return Profile::CreateProfile(path, delegate,
-                                Profile::CREATE_MODE_ASYNCHRONOUS)
-      .release();
+                                Profile::CREATE_MODE_ASYNCHRONOUS);
 }
 
 Profile* ProfileManager::GetActiveUserOrOffTheRecordProfileFromPath(
@@ -1471,7 +1472,7 @@
     return false;
   }
 
-  RegisterProfile(profile.release(), true);
+  RegisterProfile(std::move(profile), true);
   InitProfileUserPrefs(profile_ptr);
   DoFinalInit(profile_ptr, ShouldGoOffTheRecord(profile_ptr));
   return true;
@@ -1647,12 +1648,13 @@
 #endif  // !defined(OS_ANDROID)
 
 ProfileManager::ProfileInfo* ProfileManager::RegisterProfile(
-    Profile* profile,
+    std::unique_ptr<Profile> profile,
     bool created) {
   TRACE_EVENT0("browser", "ProfileManager::RegisterProfile");
-  auto info = std::make_unique<ProfileInfo>(profile, created);
+  base::FilePath path = profile->GetPath();
+  auto info = std::make_unique<ProfileInfo>(std::move(profile), created);
   ProfileInfo* info_raw = info.get();
-  profiles_info_.insert(std::make_pair(profile->GetPath(), std::move(info)));
+  profiles_info_.insert(std::make_pair(path, std::move(info)));
   return info_raw;
 }
 
@@ -1770,12 +1772,9 @@
     callbacks[i].Run(profile, status);
 }
 
-ProfileManager::ProfileInfo::ProfileInfo(
-    Profile* profile,
-    bool created)
-    : profile(profile),
-      created(created) {
-}
+ProfileManager::ProfileInfo::ProfileInfo(std::unique_ptr<Profile> profile,
+                                         bool created)
+    : profile(std::move(profile)), created(created) {}
 
 ProfileManager::ProfileInfo::~ProfileInfo() {
   ProfileDestroyer::DestroyProfileWhenAppropriate(profile.release());
diff --git a/chrome/browser/profiles/profile_manager.h b/chrome/browser/profiles/profile_manager.h
index 21a24618..4b4fa3f 100644
--- a/chrome/browser/profiles/profile_manager.h
+++ b/chrome/browser/profiles/profile_manager.h
@@ -10,7 +10,9 @@
 #include <stddef.h>
 
 #include <list>
+#include <map>
 #include <memory>
+#include <string>
 #include <vector>
 
 #include "base/files/file_path.h"
@@ -227,7 +229,7 @@
   // for testing. If |addToStorage|, adds to ProfileAttributesStorage as well.
   // If |start_deferred_task_runners|, starts the deferred task runners.
   // Use ONLY in tests.
-  void RegisterTestingProfile(Profile* profile,
+  void RegisterTestingProfile(std::unique_ptr<Profile> profile,
                               bool addToStorage,
                               bool start_deferred_task_runners);
 
@@ -260,8 +262,9 @@
   // Creates a new profile asynchronously by calling into the profile's
   // asynchronous profile creation method. Virtual so that unittests can return
   // a TestingProfile instead of the Profile's result.
-  virtual Profile* CreateProfileAsyncHelper(const base::FilePath& path,
-                                            Delegate* delegate);
+  virtual std::unique_ptr<Profile> CreateProfileAsyncHelper(
+      const base::FilePath& path,
+      Delegate* delegate);
 
  private:
   friend class TestingProfileManager;
@@ -271,7 +274,7 @@
   // This struct contains information about profiles which are being loaded or
   // were loaded.
   struct ProfileInfo {
-    ProfileInfo(Profile* profile, bool created);
+    ProfileInfo(std::unique_ptr<Profile> profile, bool created);
 
     ~ProfileInfo();
 
@@ -324,7 +327,7 @@
 
   // Registers profile with given info. Returns pointer to created ProfileInfo
   // entry.
-  ProfileInfo* RegisterProfile(Profile* profile, bool created);
+  ProfileInfo* RegisterProfile(std::unique_ptr<Profile> profile, bool created);
 
   // Returns ProfileInfo associated with given |path|, registered earlier with
   // RegisterProfile.
diff --git a/chrome/browser/profiles/profile_manager_unittest.cc b/chrome/browser/profiles/profile_manager_unittest.cc
index 36ff895..8d8578f9 100644
--- a/chrome/browser/profiles/profile_manager_unittest.cc
+++ b/chrome/browser/profiles/profile_manager_unittest.cc
@@ -5,6 +5,7 @@
 #include <stddef.h>
 
 #include <string>
+#include <utility>
 
 #include "base/bind.h"
 #include "base/bind_helpers.h"
@@ -98,15 +99,16 @@
     return new TestingProfile(file_path, nullptr);
   }
 
-  Profile* CreateProfileAsyncHelper(const base::FilePath& path,
-                                    Delegate* delegate) override {
+  std::unique_ptr<Profile> CreateProfileAsyncHelper(
+      const base::FilePath& path,
+      Delegate* delegate) override {
     // ThreadTaskRunnerHandle::Get() is TestingProfile's "async" IOTaskRunner
     // (ref. TestingProfile::GetIOTaskRunner()).
     base::ThreadTaskRunnerHandle::Get()->PostTask(
         FROM_HERE,
         base::BindOnce(base::IgnoreResult(&base::CreateDirectory), path));
 
-    return new TestingProfile(path, this);
+    return std::make_unique<TestingProfile>(path, this);
   }
 };
 
@@ -522,22 +524,24 @@
 #if BUILDFLAG(ENABLE_SUPERVISED_USERS)
   const base::FilePath supervised_path =
       temp_dir_.GetPath().AppendASCII("Supervised");
-  TestingProfile* supervised_profile =
-      new TestingProfile(supervised_path, nullptr);
+  auto supervised_profile =
+      std::make_unique<TestingProfile>(supervised_path, nullptr);
   supervised_profile->GetPrefs()->SetString(
       prefs::kSupervisedUserId, supervised_users::kChildAccountSUID);
 
   // RegisterTestingProfile adds the profile to the cache and takes ownership.
-  profile_manager->RegisterTestingProfile(supervised_profile, true, false);
+  profile_manager->RegisterTestingProfile(std::move(supervised_profile), true,
+                                          false);
   ASSERT_EQ(1u, storage.GetNumberOfProfiles());
   EXPECT_TRUE(storage.GetAllProfilesAttributesSortedByName()[0]->IsOmitted());
 #endif
 
   const base::FilePath nonsupervised_path =
       temp_dir_.GetPath().AppendASCII("Non-Supervised");
-  TestingProfile* nonsupervised_profile =
-      new TestingProfile(nonsupervised_path, nullptr);
-  profile_manager->RegisterTestingProfile(nonsupervised_profile, true, false);
+  auto nonsupervised_profile =
+      std::make_unique<TestingProfile>(nonsupervised_path, nullptr);
+  profile_manager->RegisterTestingProfile(std::move(nonsupervised_profile),
+                                          true, false);
 
 #if BUILDFLAG(ENABLE_SUPERVISED_USERS)
   EXPECT_EQ(2u, storage.GetNumberOfProfiles());
@@ -1363,10 +1367,10 @@
   TestingProfile::Builder builder;
   builder.SetGuestSession();
   builder.SetPath(ProfileManager::GetGuestProfilePath());
-  TestingProfile* guest_profile = builder.Build().release();
+  std::unique_ptr<TestingProfile> guest_profile = builder.Build();
   guest_profile->set_profile_name(guest_profile_name);
-  // Registering the profile passes ownership to the ProfileManager.
-  profile_manager->RegisterTestingProfile(guest_profile, false, false);
+  profile_manager->RegisterTestingProfile(std::move(guest_profile), false,
+                                          false);
 
   // The Guest profile does not get added to the ProfileAttributesStorage.
   EXPECT_EQ(2u, profile_manager->GetLoadedProfiles().size());
diff --git a/chrome/browser/sync/test/integration/sync_test.cc b/chrome/browser/sync/test/integration/sync_test.cc
index 4194981..53a59d1c 100644
--- a/chrome/browser/sync/test/integration/sync_test.cc
+++ b/chrome/browser/sync/test/integration/sync_test.cc
@@ -127,8 +127,8 @@
   void OnProfileCreated(Profile* profile,
                         bool success,
                         bool is_new_profile) override {
-    g_browser_process->profile_manager()->RegisterTestingProfile(profile, true,
-                                                                 false);
+    g_browser_process->profile_manager()->RegisterTestingProfile(
+        base::WrapUnique(profile), true, false);
 
     // Perform any custom work needed before the profile is initialized.
     if (!on_profile_created_callback_.is_null())
diff --git a/chrome/browser/ui/startup/startup_browser_creator_browsertest.cc b/chrome/browser/ui/startup/startup_browser_creator_browsertest.cc
index f7e0b9ef..93760a8b 100644
--- a/chrome/browser/ui/startup/startup_browser_creator_browsertest.cc
+++ b/chrome/browser/ui/startup/startup_browser_creator_browsertest.cc
@@ -1244,7 +1244,7 @@
   }
   Profile* profile1_ptr = profile1.get();
   ASSERT_TRUE(profile1_ptr);
-  profile_manager->RegisterTestingProfile(profile1.release(), true, false);
+  profile_manager->RegisterTestingProfile(std::move(profile1), true, false);
 
   Browser* browser = OpenNewBrowser(profile1_ptr);
   ASSERT_TRUE(browser);
@@ -1318,7 +1318,7 @@
   }
   Profile* profile1_ptr = profile1.get();
   ASSERT_TRUE(profile1_ptr);
-  profile_manager->RegisterTestingProfile(profile1.release(), true, false);
+  profile_manager->RegisterTestingProfile(std::move(profile1), true, false);
 
   Browser* browser = OpenNewBrowser(profile1_ptr);
   ASSERT_TRUE(browser);
diff --git a/chrome/browser/ui/startup/startup_browser_creator_triggered_reset_browsertest_win.cc b/chrome/browser/ui/startup/startup_browser_creator_triggered_reset_browsertest_win.cc
index 8b64fca..c51e56f3 100644
--- a/chrome/browser/ui/startup/startup_browser_creator_triggered_reset_browsertest_win.cc
+++ b/chrome/browser/ui/startup/startup_browser_creator_triggered_reset_browsertest_win.cc
@@ -250,7 +250,8 @@
         Profile::CreateProfile(path, nullptr, Profile::CREATE_MODE_SYNCHRONOUS);
   }
   Profile* other_profile_ptr = other_profile.get();
-  profile_manager->RegisterTestingProfile(other_profile.release(), true, false);
+  profile_manager->RegisterTestingProfile(std::move(other_profile), true,
+                                          false);
 
   // Use a couple same-site HTTP URLs.
   ASSERT_TRUE(embedded_test_server()->Start());
diff --git a/chrome/browser/ui/views/profiles/profile_chooser_view_browsertest.cc b/chrome/browser/ui/views/profiles/profile_chooser_view_browsertest.cc
index bf77c242..dc8a8f7df 100644
--- a/chrome/browser/ui/views/profiles/profile_chooser_view_browsertest.cc
+++ b/chrome/browser/ui/views/profiles/profile_chooser_view_browsertest.cc
@@ -61,7 +61,7 @@
   std::unique_ptr<Profile> profile =
       Profile::CreateProfile(path, nullptr, Profile::CREATE_MODE_SYNCHRONOUS);
   Profile* profile_ptr = profile.get();
-  profile_manager->RegisterTestingProfile(profile.release(), true, false);
+  profile_manager->RegisterTestingProfile(std::move(profile), true, false);
   EXPECT_EQ(starting_number_of_profiles + 1,
             profile_manager->GetNumberOfProfiles());
   return profile_ptr;
diff --git a/chrome/browser/ui/webui/signin/dice_turn_sync_on_helper_unittest.cc b/chrome/browser/ui/webui/signin/dice_turn_sync_on_helper_unittest.cc
index 6450d64..b6aa3286 100644
--- a/chrome/browser/ui/webui/signin/dice_turn_sync_on_helper_unittest.cc
+++ b/chrome/browser/ui/webui/signin/dice_turn_sync_on_helper_unittest.cc
@@ -4,6 +4,8 @@
 
 #include "chrome/browser/ui/webui/signin/dice_turn_sync_on_helper.h"
 
+#include <utility>
+
 #include "base/bind.h"
 #include "base/callback.h"
 #include "base/files/file_path.h"
@@ -107,12 +109,13 @@
     return BuildTestingProfile(file_path, /*delegate=*/nullptr).release();
   }
 
-  Profile* CreateProfileAsyncHelper(const base::FilePath& path,
-                                    Delegate* delegate) override {
+  std::unique_ptr<Profile> CreateProfileAsyncHelper(
+      const base::FilePath& path,
+      Delegate* delegate) override {
     base::ThreadTaskRunnerHandle::Get()->PostTask(
         FROM_HERE,
         base::BindOnce(base::IgnoreResult(&base::CreateDirectory), path));
-    return BuildTestingProfile(path, this).release();
+    return BuildTestingProfile(path, this);
   }
 };