Introduce ProfileManagerObserver.

Currently there are two methods, OnProfileAdded (to replace
NOTIFICATION_PROFILE_ADDED and NOTIFICATION_PROFILE_CREATED) and
OnProfileMarkedForPermanentDeletion (to replace
NOTIFICATION_PROFILE_DESTRUCTION_STARTED).

Use the new interface in NoteTakingHelper and ExtensionService.

Another option would be to have OnProfileMarkedForPermanentDeletion
be a part of a ProfileObserver interface, but it seems best to
minimize the number of observer interfaces.

Bug: 268984
Change-Id: I96ba72e558ee3da30729207adcd8e22e0755293a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1776682
Commit-Queue: Evan Stade <[email protected]>
Reviewed-by: Scott Violet <[email protected]>
Cr-Commit-Position: refs/heads/master@{#693858}
diff --git a/chrome/browser/extensions/extension_service.cc b/chrome/browser/extensions/extension_service.cc
index d2fd26b6..3ea9b0cd 100644
--- a/chrome/browser/extensions/extension_service.cc
+++ b/chrome/browser/extensions/extension_service.cc
@@ -32,6 +32,7 @@
 #include "base/time/time.h"
 #include "base/trace_event/trace_event.h"
 #include "build/build_config.h"
+#include "chrome/browser/browser_process.h"
 #include "chrome/browser/chrome_notification_types.h"
 #include "chrome/browser/extensions/api/content_settings/content_settings_custom_extension_provider.h"
 #include "chrome/browser/extensions/api/content_settings/content_settings_service.h"
@@ -322,9 +323,9 @@
                  content::NotificationService::AllBrowserContextsAndSources());
   registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
                  content::NotificationService::AllBrowserContextsAndSources());
-  registrar_.Add(this,
-                 chrome::NOTIFICATION_PROFILE_DESTRUCTION_STARTED,
-                 content::Source<Profile>(profile_));
+  // The ProfileManager may be null in unit tests.
+  if (g_browser_process->profile_manager())
+    profile_manager_observer_.Add(g_browser_process->profile_manager());
 
   UpgradeDetector::GetInstance()->AddObserver(this);
 
@@ -1842,10 +1843,6 @@
                                     system_->info_map(), process->GetID()));
       break;
     }
-    case chrome::NOTIFICATION_PROFILE_DESTRUCTION_STARTED: {
-      OnProfileDestructionStarted();
-      break;
-    }
 
     default:
       NOTREACHED() << "Unexpected notification type.";
@@ -1999,6 +1996,15 @@
   return !extension || CanBlockExtension(extension);
 }
 
+void ExtensionService::OnProfileMarkedForPermanentDeletion(Profile* profile) {
+  if (profile != profile_)
+    return;
+
+  ExtensionIdSet ids_to_unload = registry_->enabled_extensions().GetIDs();
+  for (auto it = ids_to_unload.begin(); it != ids_to_unload.end(); ++it)
+    UnloadExtension(*it, UnloadedExtensionReason::PROFILE_SHUTDOWN);
+}
+
 void ExtensionService::ManageBlacklist(
     const Blacklist::BlacklistStateMap& state_map) {
   DCHECK_CURRENTLY_ON(BrowserThread::UI);
@@ -2166,13 +2172,6 @@
   // been disabled or uninstalled.
 }
 
-void ExtensionService::OnProfileDestructionStarted() {
-  ExtensionIdSet ids_to_unload = registry_->enabled_extensions().GetIDs();
-  for (auto it = ids_to_unload.begin(); it != ids_to_unload.end(); ++it) {
-    UnloadExtension(*it, UnloadedExtensionReason::PROFILE_SHUTDOWN);
-  }
-}
-
 void ExtensionService::OnInstalledExtensionsLoaded() {
   if (updater_)
     updater_->Start();