Remove DEPRECATED extension notification from background_contents_service.*

[email protected]
BUG=354458

Review URL: https://codereview.chromium.org/413563002

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@285633 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/chrome/browser/background/background_contents_service.cc b/chrome/browser/background/background_contents_service.cc
index 687d87a..1b22c25c 100644
--- a/chrome/browser/background/background_contents_service.cc
+++ b/chrome/browser/background/background_contents_service.cc
@@ -261,7 +261,7 @@
 
 BackgroundContentsService::BackgroundContentsService(
     Profile* profile, const CommandLine* command_line)
-    : prefs_(NULL) {
+    : prefs_(NULL), extension_registry_observer_(this) {
   // Don't load/store preferences if the parent profile is incognito.
   if (!profile->IsOffTheRecord())
     prefs_ = profile->GetPrefs();
@@ -329,12 +329,6 @@
   registrar_.Add(this, chrome::NOTIFICATION_BACKGROUND_CONTENTS_NAVIGATED,
                  content::Source<Profile>(profile));
 
-  // Listen for new extension installs so that we can load any associated
-  // background page.
-  registrar_.Add(this,
-                 chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED,
-                 content::Source<Profile>(profile));
-
   // Track when the extensions crash so that the user can be notified
   // about it, and the crashed contents can be restarted.
   registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED,
@@ -342,18 +336,8 @@
   registrar_.Add(this, chrome::NOTIFICATION_BACKGROUND_CONTENTS_TERMINATED,
                  content::Source<Profile>(profile));
 
-  // Listen for extensions to be unloaded so we can shutdown associated
-  // BackgroundContents.
-  registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED,
-                 content::Source<Profile>(profile));
-
-  // Make sure the extension-crash balloons are removed when the extension is
-  // uninstalled/reloaded. We cannot do this from UNLOADED since a crashed
-  // extension is unloaded immediately after the crash, not when user reloads or
-  // uninstalls the extension.
-  registrar_.Add(this,
-                 chrome::NOTIFICATION_EXTENSION_UNINSTALLED_DEPRECATED,
-                 content::Source<Profile>(profile));
+  // Listen for extension uninstall, load, unloaded notification.
+  extension_registry_observer_.Add(extensions::ExtensionRegistry::Get(profile));
 }
 
 void BackgroundContentsService::Observe(
@@ -401,35 +385,6 @@
       RegisterBackgroundContents(bgcontents);
       break;
     }
-    case chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED: {
-      const Extension* extension =
-          content::Details<const Extension>(details).ptr();
-      Profile* profile = content::Source<Profile>(source).ptr();
-      if (extension->is_hosted_app() &&
-          BackgroundInfo::HasBackgroundPage(extension)) {
-        // If there is a background page specified in the manifest for a hosted
-        // app, then blow away registered urls in the pref.
-        ShutdownAssociatedBackgroundContents(
-            base::ASCIIToUTF16(extension->id()));
-
-        ExtensionService* service =
-            extensions::ExtensionSystem::Get(profile)->extension_service();
-        if (service && service->is_ready()) {
-          // Now load the manifest-specified background page. If service isn't
-          // ready, then the background page will be loaded from the
-          // EXTENSIONS_READY callback.
-          LoadBackgroundContents(profile,
-                                 BackgroundInfo::GetBackgroundURL(extension),
-                                 base::ASCIIToUTF16("background"),
-                                 base::UTF8ToUTF16(extension->id()));
-        }
-      }
-
-      // Close the crash notification balloon for the app/extension, if any.
-      ScheduleCloseBalloon(extension->id());
-      SendChangeNotification(profile);
-      break;
-    }
     case chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED:
     case chrome::NOTIFICATION_BACKGROUND_CONTENTS_TERMINATED: {
       Profile* profile = content::Source<Profile>(source).ptr();
@@ -462,48 +417,6 @@
       }
       break;
     }
-    case chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED:
-      switch (content::Details<UnloadedExtensionInfo>(details)->reason) {
-        case UnloadedExtensionInfo::REASON_DISABLE:    // Fall through.
-        case UnloadedExtensionInfo::REASON_TERMINATE:  // Fall through.
-        case UnloadedExtensionInfo::REASON_UNINSTALL:  // Fall through.
-        case UnloadedExtensionInfo::REASON_BLACKLIST:  // Fall through.
-        case UnloadedExtensionInfo::REASON_PROFILE_SHUTDOWN:
-          ShutdownAssociatedBackgroundContents(base::ASCIIToUTF16(
-              content::Details<UnloadedExtensionInfo>(details)->
-                  extension->id()));
-          SendChangeNotification(content::Source<Profile>(source).ptr());
-          break;
-        case UnloadedExtensionInfo::REASON_UPDATE: {
-          // If there is a manifest specified background page, then shut it down
-          // here, since if the updated extension still has the background page,
-          // then it will be loaded from LOADED callback. Otherwise, leave
-          // BackgroundContents in place.
-          // We don't call SendChangeNotification here - it will be generated
-          // from the LOADED callback.
-          const Extension* extension =
-              content::Details<UnloadedExtensionInfo>(details)->extension;
-          if (BackgroundInfo::HasBackgroundPage(extension)) {
-            ShutdownAssociatedBackgroundContents(
-                base::ASCIIToUTF16(extension->id()));
-          }
-          break;
-        }
-        default:
-          NOTREACHED();
-          ShutdownAssociatedBackgroundContents(base::ASCIIToUTF16(
-              content::Details<UnloadedExtensionInfo>(details)->
-                  extension->id()));
-          break;
-      }
-      break;
-
-    case chrome::NOTIFICATION_EXTENSION_UNINSTALLED_DEPRECATED: {
-      // Close the crash notification balloon for the app/extension, if any.
-      ScheduleCloseBalloon(
-          content::Details<const Extension>(details).ptr()->id());
-      break;
-    }
 
     default:
       NOTREACHED();
@@ -511,6 +424,78 @@
   }
 }
 
+void BackgroundContentsService::OnExtensionLoaded(
+    content::BrowserContext* browser_context,
+    const extensions::Extension* extension) {
+  Profile* profile = Profile::FromBrowserContext(browser_context);
+  if (extension->is_hosted_app() &&
+      BackgroundInfo::HasBackgroundPage(extension)) {
+    // If there is a background page specified in the manifest for a hosted
+    // app, then blow away registered urls in the pref.
+    ShutdownAssociatedBackgroundContents(base::ASCIIToUTF16(extension->id()));
+
+    ExtensionService* service =
+        extensions::ExtensionSystem::Get(browser_context)->extension_service();
+    if (service && service->is_ready()) {
+      // Now load the manifest-specified background page. If service isn't
+      // ready, then the background page will be loaded from the
+      // EXTENSIONS_READY callback.
+      LoadBackgroundContents(profile,
+                             BackgroundInfo::GetBackgroundURL(extension),
+                             base::ASCIIToUTF16("background"),
+                             base::UTF8ToUTF16(extension->id()));
+    }
+  }
+
+  // Close the crash notification balloon for the app/extension, if any.
+  ScheduleCloseBalloon(extension->id());
+  SendChangeNotification(profile);
+}
+
+void BackgroundContentsService::OnExtensionUnloaded(
+    content::BrowserContext* browser_context,
+    const extensions::Extension* extension,
+    extensions::UnloadedExtensionInfo::Reason reason) {
+  switch (reason) {
+    case UnloadedExtensionInfo::REASON_DISABLE:    // Fall through.
+    case UnloadedExtensionInfo::REASON_TERMINATE:  // Fall through.
+    case UnloadedExtensionInfo::REASON_UNINSTALL:  // Fall through.
+    case UnloadedExtensionInfo::REASON_BLACKLIST:  // Fall through.
+    case UnloadedExtensionInfo::REASON_PROFILE_SHUTDOWN:
+      ShutdownAssociatedBackgroundContents(base::ASCIIToUTF16(extension->id()));
+      SendChangeNotification(Profile::FromBrowserContext(browser_context));
+      break;
+    case UnloadedExtensionInfo::REASON_UPDATE: {
+      // If there is a manifest specified background page, then shut it down
+      // here, since if the updated extension still has the background page,
+      // then it will be loaded from LOADED callback. Otherwise, leave
+      // BackgroundContents in place.
+      // We don't call SendChangeNotification here - it will be generated
+      // from the LOADED callback.
+      if (BackgroundInfo::HasBackgroundPage(extension)) {
+        ShutdownAssociatedBackgroundContents(
+            base::ASCIIToUTF16(extension->id()));
+      }
+      break;
+    }
+    default:
+      NOTREACHED();
+      ShutdownAssociatedBackgroundContents(base::ASCIIToUTF16(extension->id()));
+      break;
+  }
+}
+
+void BackgroundContentsService::OnExtensionUninstalled(
+    content::BrowserContext* browser_context,
+    const extensions::Extension* extension,
+    extensions::UninstallReason reason) {
+  // Make sure the extension-crash balloons are removed when the extension is
+  // uninstalled/reloaded. We cannot do this from UNLOADED since a crashed
+  // extension is unloaded immediately after the crash, not when user reloads or
+  // uninstalls the extension.
+  ScheduleCloseBalloon(extension->id());
+}
+
 void BackgroundContentsService::RestartForceInstalledExtensionOnCrash(
     const Extension* extension,
     Profile* profile) {
diff --git a/chrome/browser/background/background_contents_service.h b/chrome/browser/background/background_contents_service.h
index 05f0953..d9f859d 100644
--- a/chrome/browser/background/background_contents_service.h
+++ b/chrome/browser/background/background_contents_service.h
@@ -11,11 +11,13 @@
 
 #include "base/gtest_prod_util.h"
 #include "base/memory/ref_counted.h"
+#include "base/scoped_observer.h"
 #include "chrome/browser/tab_contents/background_contents.h"
 #include "components/keyed_service/core/keyed_service.h"
 #include "content/public/browser/notification_observer.h"
 #include "content/public/browser/notification_registrar.h"
 #include "content/public/common/window_container_type.h"
+#include "extensions/browser/extension_registry_observer.h"
 #include "ui/base/window_open_disposition.h"
 #include "url/gurl.h"
 
@@ -33,6 +35,7 @@
 
 namespace extensions {
 class Extension;
+class ExtensionRegistry;
 }
 
 namespace gfx {
@@ -50,6 +53,7 @@
 // BackgroundContents and their parent app, and shutting them down when the
 // parent app is unloaded.
 class BackgroundContentsService : private content::NotificationObserver,
+                                  public extensions::ExtensionRegistryObserver,
                                   public BackgroundContents::Delegate,
                                   public KeyedService {
  public:
@@ -140,6 +144,19 @@
                        const content::NotificationSource& source,
                        const content::NotificationDetails& details) OVERRIDE;
 
+  // extensions::ExtensionRegistryObserver implementation.
+  virtual void OnExtensionLoaded(
+      content::BrowserContext* browser_context,
+      const extensions::Extension* extension) OVERRIDE;
+  virtual void OnExtensionUnloaded(
+      content::BrowserContext* browser_context,
+      const extensions::Extension* extension,
+      extensions::UnloadedExtensionInfo::Reason reason) OVERRIDE;
+  virtual void OnExtensionUninstalled(
+      content::BrowserContext* browser_context,
+      const extensions::Extension* extension,
+      extensions::UninstallReason reason) OVERRIDE;
+
   // Restarts a force-installed app/extension after a crash.
   void RestartForceInstalledExtensionOnCrash(
       const extensions::Extension* extension,
@@ -218,6 +235,10 @@
       BackgroundContentsMap;
   BackgroundContentsMap contents_map_;
 
+  ScopedObserver<extensions::ExtensionRegistry,
+                 extensions::ExtensionRegistryObserver>
+      extension_registry_observer_;
+
   DISALLOW_COPY_AND_ASSIGN(BackgroundContentsService);
 };
 
diff --git a/chrome/browser/background/background_contents_service_factory.cc b/chrome/browser/background/background_contents_service_factory.cc
index e0326643..4289e8b5 100644
--- a/chrome/browser/background/background_contents_service_factory.cc
+++ b/chrome/browser/background/background_contents_service_factory.cc
@@ -12,6 +12,7 @@
 #include "chrome/common/pref_names.h"
 #include "components/keyed_service/content/browser_context_dependency_manager.h"
 #include "components/pref_registry/pref_registry_syncable.h"
+#include "extensions/browser/extension_registry_factory.h"
 
 // static
 BackgroundContentsService* BackgroundContentsServiceFactory::GetForProfile(
@@ -30,6 +31,7 @@
     : BrowserContextKeyedServiceFactory(
         "BackgroundContentsService",
         BrowserContextDependencyManager::GetInstance()) {
+  DependsOn(extensions::ExtensionRegistryFactory::GetInstance());
 }
 
 BackgroundContentsServiceFactory::~BackgroundContentsServiceFactory() {