Cleanup: Remove NOTIFICATION_EXTENSION_UNLOADED usage in MediaScanManager.

BUG=354046

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

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@262295 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/base/scoped_observer.h b/base/scoped_observer.h
index eae63672..3754ed5 100644
--- a/base/scoped_observer.h
+++ b/base/scoped_observer.h
@@ -19,8 +19,7 @@
   explicit ScopedObserver(Observer* observer) : observer_(observer) {}
 
   ~ScopedObserver() {
-    for (size_t i = 0; i < sources_.size(); ++i)
-      sources_[i]->RemoveObserver(observer_);
+    RemoveAll();
   }
 
   // Adds the object passed to the constructor as an observer on |source|.
@@ -35,12 +34,15 @@
     source->RemoveObserver(observer_);
   }
 
+  void RemoveAll() {
+    for (size_t i = 0; i < sources_.size(); ++i)
+      sources_[i]->RemoveObserver(observer_);
+    sources_.clear();
+  }
+
   bool IsObserving(Source* source) const {
-    for (size_t i = 0; i < sources_.size(); ++i) {
-      if (sources_[i] == source)
-        return true;
-    }
-    return false;
+    return std::find(sources_.begin(), sources_.end(), source) !=
+        sources_.end();
   }
 
  private:
diff --git a/chrome/browser/extensions/api/media_galleries_private/gallery_watch_state_tracker.cc b/chrome/browser/extensions/api/media_galleries_private/gallery_watch_state_tracker.cc
index 7bda3b8a..3629f38f 100644
--- a/chrome/browser/extensions/api/media_galleries_private/gallery_watch_state_tracker.cc
+++ b/chrome/browser/extensions/api/media_galleries_private/gallery_watch_state_tracker.cc
@@ -191,7 +191,9 @@
   WriteToStorage(extension_id);
 }
 
-void GalleryWatchStateTracker::OnExtensionLoaded(const Extension* extension) {
+void GalleryWatchStateTracker::OnExtensionLoaded(
+    content::BrowserContext* browser_context,
+    const Extension* extension) {
   DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
   StateStore* storage = ExtensionSystem::Get(profile_)->state_store();
   if (!storage)
@@ -205,6 +207,7 @@
 }
 
 void GalleryWatchStateTracker::OnExtensionUnloaded(
+    content::BrowserContext* browser_context,
     const Extension* extension) {
   DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
   if (!ContainsKey(watched_extensions_map_, extension->id()))
diff --git a/chrome/browser/extensions/api/media_galleries_private/gallery_watch_state_tracker.h b/chrome/browser/extensions/api/media_galleries_private/gallery_watch_state_tracker.h
index 381354ae..985f63c 100644
--- a/chrome/browser/extensions/api/media_galleries_private/gallery_watch_state_tracker.h
+++ b/chrome/browser/extensions/api/media_galleries_private/gallery_watch_state_tracker.h
@@ -90,8 +90,10 @@
   typedef std::map<std::string, WatchedGalleriesMap> WatchedExtensionsMap;
 
   // extensions::ExtensionRegistryObserver implementation.
-  virtual void OnExtensionLoaded(const Extension* extension) OVERRIDE;
-  virtual void OnExtensionUnloaded(const Extension* extension) OVERRIDE;
+  virtual void OnExtensionLoaded(content::BrowserContext* browser_context,
+                                 const Extension* extension) OVERRIDE;
+  virtual void OnExtensionUnloaded(content::BrowserContext* browser_context,
+                                   const Extension* extension) OVERRIDE;
 
   // Syncs media gallery watch data for the given extension to/from the state
   // storage.
diff --git a/chrome/browser/extensions/error_console/error_console.cc b/chrome/browser/extensions/error_console/error_console.cc
index ee57d02..2429af6 100644
--- a/chrome/browser/extensions/error_console/error_console.cc
+++ b/chrome/browser/extensions/error_console/error_console.cc
@@ -210,11 +210,13 @@
   CheckEnabled();
 }
 
-void ErrorConsole::OnExtensionUnloaded(const Extension* extension) {
+void ErrorConsole::OnExtensionUnloaded(content::BrowserContext* browser_context,
+                                       const Extension* extension) {
   CheckEnabled();
 }
 
-void ErrorConsole::OnExtensionLoaded(const Extension* extension) {
+void ErrorConsole::OnExtensionLoaded(content::BrowserContext* browser_context,
+                                     const Extension* extension) {
   CheckEnabled();
 }
 
diff --git a/chrome/browser/extensions/error_console/error_console.h b/chrome/browser/extensions/error_console/error_console.h
index b1c4108b..f2d19eba 100644
--- a/chrome/browser/extensions/error_console/error_console.h
+++ b/chrome/browser/extensions/error_console/error_console.h
@@ -125,8 +125,10 @@
 
   // ExtensionRegistry implementation. If the Apps Developer Tools app is
   // installed or uninstalled, we may need to turn the ErrorConsole on/off.
-  virtual void OnExtensionUnloaded(const Extension* extension) OVERRIDE;
-  virtual void OnExtensionLoaded(const Extension* extension) OVERRIDE;
+  virtual void OnExtensionUnloaded(content::BrowserContext* browser_context,
+                                   const Extension* extension) OVERRIDE;
+  virtual void OnExtensionLoaded(content::BrowserContext* browser_context,
+                                 const Extension* extension) OVERRIDE;
 
   // Add manifest errors from an extension's install warnings.
   void AddManifestErrorsForExtension(const Extension* extension);
diff --git a/chrome/browser/media_galleries/media_scan_manager.cc b/chrome/browser/media_galleries/media_scan_manager.cc
index 8b164701..e880b98 100644
--- a/chrome/browser/media_galleries/media_scan_manager.cc
+++ b/chrome/browser/media_galleries/media_scan_manager.cc
@@ -9,7 +9,6 @@
 #include "base/logging.h"
 #include "base/metrics/histogram.h"
 #include "base/time/time.h"
-#include "chrome/browser/chrome_notification_types.h"
 #include "chrome/browser/extensions/extension_service.h"
 #include "chrome/browser/media_galleries/media_galleries_preferences.h"
 #include "chrome/browser/media_galleries/media_galleries_preferences_factory.h"
@@ -17,11 +16,12 @@
 #include "chrome/browser/profiles/profile.h"
 #include "chrome/common/extensions/api/media_galleries.h"
 #include "content/public/browser/browser_thread.h"
-#include "content/public/browser/notification_details.h"
-#include "content/public/browser/notification_source.h"
+#include "extensions/browser/extension_registry.h"
 #include "extensions/browser/extension_system.h"
 #include "extensions/common/extension.h"
 
+using extensions::ExtensionRegistry;
+
 namespace media_galleries = extensions::api::media_galleries;
 
 namespace {
@@ -304,7 +304,9 @@
 
 }  // namespace
 
-MediaScanManager::MediaScanManager() : weak_factory_(this) {
+MediaScanManager::MediaScanManager()
+    : scoped_extension_registry_observer_(this),
+      weak_factory_(this) {
   DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
 }
 
@@ -369,12 +371,8 @@
   }
 
   // On first scan for the |profile|, register to listen for extension unload.
-  if (scanning_extensions->empty()) {
-    registrar_.Add(
-        this,
-        chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED,
-        content::Source<Profile>(profile));
-  }
+  if (scanning_extensions->empty())
+    scoped_extension_registry_observer_.Add(ExtensionRegistry::Get(profile));
 
   scanning_extensions->insert(extension->id());
   scans_for_profile->second.observer->OnScanStarted(extension->id());
@@ -408,12 +406,8 @@
   scans_for_profile->second.observer->OnScanCancelled(extension->id());
 
   // No more scanning extensions for |profile|, so stop listening for unloads.
-  if (scans_for_profile->second.scanning_extensions.empty()) {
-    registrar_.Remove(
-        this,
-        chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED,
-        content::Source<Profile>(profile));
-  }
+  if (scans_for_profile->second.scanning_extensions.empty())
+    scoped_extension_registry_observer_.Remove(ExtensionRegistry::Get(profile));
 
   if (!ScanInProgress()) {
     folder_finder_.reset();
@@ -432,23 +426,11 @@
 MediaScanManager::ScanObservers::ScanObservers() : observer(NULL) {}
 MediaScanManager::ScanObservers::~ScanObservers() {}
 
-void MediaScanManager::Observe(
-    int type, const content::NotificationSource& source,
-    const content::NotificationDetails& details) {
+void MediaScanManager::OnExtensionUnloaded(
+    content::BrowserContext* browser_context,
+    const extensions::Extension* extension) {
   DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
-  switch (type) {
-    case chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED: {
-      Profile* profile = content::Source<Profile>(source).ptr();
-      extensions::Extension* extension = const_cast<extensions::Extension*>(
-          content::Details<extensions::UnloadedExtensionInfo>(
-              details)->extension);
-      DCHECK(extension);
-      CancelScan(profile, extension);
-      break;
-    }
-    default:
-      NOTREACHED();
-  }
+  CancelScan(Profile::FromBrowserContext(browser_context), extension);
 }
 
 bool MediaScanManager::ScanInProgress() const {
@@ -529,6 +511,6 @@
     scanning_extensions->clear();
     preferences->SetLastScanCompletionTime(base::Time::Now());
   }
-  registrar_.RemoveAll();
+  scoped_extension_registry_observer_.RemoveAll();
   folder_finder_.reset();
 }
diff --git a/chrome/browser/media_galleries/media_scan_manager.h b/chrome/browser/media_galleries/media_scan_manager.h
index 9b6be2d..24417584 100644
--- a/chrome/browser/media_galleries/media_scan_manager.h
+++ b/chrome/browser/media_galleries/media_scan_manager.h
@@ -13,24 +13,25 @@
 #include "base/compiler_specific.h"
 #include "base/memory/scoped_ptr.h"
 #include "base/memory/weak_ptr.h"
+#include "base/scoped_observer.h"
 #include "base/time/time.h"
 #include "chrome/browser/media_galleries/media_folder_finder.h"
 #include "chrome/browser/media_galleries/media_scan_types.h"
-#include "content/public/browser/notification_observer.h"
-#include "content/public/browser/notification_registrar.h"
+#include "extensions/browser/extension_registry_observer.h"
 
-class Profile;
 class MediaScanManagerObserver;
+class Profile;
 
 namespace extensions {
 class Extension;
-}  // namespace extensions
+class ExtensionRegistry;
+}
 
 // The MediaScanManager is owned by MediaFileSystemRegistry, which is global.
 // This class manages multiple 'virtual' media scans, up to one per extension
 // per profile, and also manages the one physical scan backing them.
 // This class lives and is called on the UI thread.
-class MediaScanManager : public content::NotificationObserver {
+class MediaScanManager : public extensions::ExtensionRegistryObserver {
  public:
   MediaScanManager();
   virtual ~MediaScanManager();
@@ -68,10 +69,10 @@
   };
   typedef std::map<Profile*, ScanObservers> ScanObserverMap;
 
-  // content::NotificationObserver implementation.
-  virtual void Observe(int type,
-                       const content::NotificationSource& source,
-                       const content::NotificationDetails& details) OVERRIDE;
+  // extensions::ExtensionRegistryObserver implementation.
+  virtual void OnExtensionUnloaded(
+      content::BrowserContext* browser_context,
+      const extensions::Extension* extension) OVERRIDE;
 
   bool ScanInProgress() const;
 
@@ -93,8 +94,9 @@
   // Set of extensions (on all profiles) that have an in-progress scan.
   ScanObserverMap observers_;
 
-  // Used to listen for NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED events.
-  content::NotificationRegistrar registrar_;
+  ScopedObserver<extensions::ExtensionRegistry,
+                 extensions::ExtensionRegistryObserver>
+      scoped_extension_registry_observer_;
 
   base::WeakPtrFactory<MediaScanManager> weak_factory_;
 
diff --git a/extensions/browser/extension_registry.cc b/extensions/browser/extension_registry.cc
index 1f52ee11..9f6b71f09 100644
--- a/extensions/browser/extension_registry.cc
+++ b/extensions/browser/extension_registry.cc
@@ -10,7 +10,8 @@
 
 namespace extensions {
 
-ExtensionRegistry::ExtensionRegistry() {}
+ExtensionRegistry::ExtensionRegistry(content::BrowserContext* browser_context)
+    : browser_context_(browser_context) {}
 ExtensionRegistry::~ExtensionRegistry() {}
 
 // static
@@ -38,14 +39,16 @@
 
 void ExtensionRegistry::TriggerOnLoaded(const Extension* extension) {
   DCHECK(enabled_extensions_.Contains(extension->id()));
-  FOR_EACH_OBSERVER(
-      ExtensionRegistryObserver, observers_, OnExtensionLoaded(extension));
+  FOR_EACH_OBSERVER(ExtensionRegistryObserver,
+                    observers_,
+                    OnExtensionLoaded(browser_context_, extension));
 }
 
 void ExtensionRegistry::TriggerOnUnloaded(const Extension* extension) {
   DCHECK(!enabled_extensions_.Contains(extension->id()));
-  FOR_EACH_OBSERVER(
-      ExtensionRegistryObserver, observers_, OnExtensionUnloaded(extension));
+  FOR_EACH_OBSERVER(ExtensionRegistryObserver,
+                    observers_,
+                    OnExtensionUnloaded(browser_context_, extension));
 }
 
 const Extension* ExtensionRegistry::GetExtensionById(const std::string& id,
diff --git a/extensions/browser/extension_registry.h b/extensions/browser/extension_registry.h
index 3030c07..26e30159 100644
--- a/extensions/browser/extension_registry.h
+++ b/extensions/browser/extension_registry.h
@@ -36,7 +36,7 @@
     EVERYTHING = (1 << 4) - 1,
   };
 
-  ExtensionRegistry();
+  explicit ExtensionRegistry(content::BrowserContext* browser_context);
   virtual ~ExtensionRegistry();
 
   // Returns the instance for the given |browser_context|.
@@ -137,6 +137,8 @@
 
   ObserverList<ExtensionRegistryObserver> observers_;
 
+  content::BrowserContext* const browser_context_;
+
   DISALLOW_COPY_AND_ASSIGN(ExtensionRegistry);
 };
 
diff --git a/extensions/browser/extension_registry_factory.cc b/extensions/browser/extension_registry_factory.cc
index 2349a66..dc3c6a0 100644
--- a/extensions/browser/extension_registry_factory.cc
+++ b/extensions/browser/extension_registry_factory.cc
@@ -35,7 +35,7 @@
 
 KeyedService* ExtensionRegistryFactory::BuildServiceInstanceFor(
     content::BrowserContext* context) const {
-  return new ExtensionRegistry;
+  return new ExtensionRegistry(context);
 }
 
 BrowserContext* ExtensionRegistryFactory::GetBrowserContextToUse(
diff --git a/extensions/browser/extension_registry_observer.h b/extensions/browser/extension_registry_observer.h
index ae1281ce..3b7114b 100644
--- a/extensions/browser/extension_registry_observer.h
+++ b/extensions/browser/extension_registry_observer.h
@@ -5,6 +5,10 @@
 #ifndef EXTENSIONS_BROWSER_EXTENSION_REGISTRY_OBSERVER_H_
 #define EXTENSIONS_BROWSER_EXTENSION_REGISTRY_OBSERVER_H_
 
+namespace content {
+class BrowserContext;
+}
+
 namespace extensions {
 
 class Extension;
@@ -17,13 +21,17 @@
 
   // Called after an extension is loaded. The extension will exclusively exist
   // in the enabled_extensions set of ExtensionRegistry.
-  virtual void OnExtensionLoaded(const Extension* extension) {}
+  virtual void OnExtensionLoaded(
+      content::BrowserContext* browser_context,
+      const Extension* extension) {}
 
   // Called after an extension is unloaded. The extension no longer exists in
   // any of the ExtensionRegistry sets (enabled, disabled, etc.).
-  virtual void OnExtensionUnloaded(const Extension* extension) {}
+  virtual void OnExtensionUnloaded(
+      content::BrowserContext* browser_context,
+      const Extension* extension) {}
 };
 
-}
+}  // namespace extensions
 
 #endif  // EXTENSIONS_BROWSER_EXTENSION_REGISTRY_OBSERVER_H_
diff --git a/extensions/browser/extension_registry_unittest.cc b/extensions/browser/extension_registry_unittest.cc
index 7eaec65..11ca3fc 100644
--- a/extensions/browser/extension_registry_unittest.cc
+++ b/extensions/browser/extension_registry_unittest.cc
@@ -43,11 +43,13 @@
   const ExtensionList& unloaded() { return unloaded_; }
 
  private:
-  virtual void OnExtensionLoaded(const Extension* extension) OVERRIDE {
+  virtual void OnExtensionLoaded(content::BrowserContext* browser_context,
+                                 const Extension* extension) OVERRIDE {
     loaded_.push_back(extension);
   }
 
-  virtual void OnExtensionUnloaded(const Extension* extension) OVERRIDE {
+  virtual void OnExtensionUnloaded(content::BrowserContext* browser_context,
+                                   const Extension* extension) OVERRIDE {
     unloaded_.push_back(extension);
   }
 
@@ -56,7 +58,7 @@
 };
 
 TEST_F(ExtensionRegistryTest, FillAndClearRegistry) {
-  ExtensionRegistry registry;
+  ExtensionRegistry registry(NULL);
   scoped_refptr<Extension> extension1 = test_util::CreateExtensionWithID("id1");
   scoped_refptr<Extension> extension2 = test_util::CreateExtensionWithID("id2");
   scoped_refptr<Extension> extension3 = test_util::CreateExtensionWithID("id3");
@@ -90,7 +92,7 @@
 
 // A simple test of adding and removing things from sets.
 TEST_F(ExtensionRegistryTest, AddAndRemoveExtensionFromRegistry) {
-  ExtensionRegistry registry;
+  ExtensionRegistry registry(NULL);
 
   // Adding an extension works.
   scoped_refptr<Extension> extension = test_util::CreateExtensionWithID("id");
@@ -111,7 +113,7 @@
 }
 
 TEST_F(ExtensionRegistryTest, AddExtensionToRegistryTwice) {
-  ExtensionRegistry registry;
+  ExtensionRegistry registry(NULL);
   scoped_refptr<Extension> extension = test_util::CreateExtensionWithID("id");
 
   // An extension can exist in two sets at once. It would be nice to eliminate
@@ -126,7 +128,7 @@
 }
 
 TEST_F(ExtensionRegistryTest, GetExtensionById) {
-  ExtensionRegistry registry;
+  ExtensionRegistry registry(NULL);
 
   // Trying to get an extension fails cleanly when the sets are empty.
   EXPECT_FALSE(
@@ -205,7 +207,7 @@
 }
 
 TEST_F(ExtensionRegistryTest, Observer) {
-  ExtensionRegistry registry;
+  ExtensionRegistry registry(NULL);
   TestObserver observer;
   registry.AddObserver(&observer);
 
diff --git a/extensions/browser/runtime_data.cc b/extensions/browser/runtime_data.cc
index a3b1c6c4..ea5568b 100644
--- a/extensions/browser/runtime_data.cc
+++ b/extensions/browser/runtime_data.cc
@@ -53,7 +53,9 @@
   extension_flags_.clear();
 }
 
-void RuntimeData::OnExtensionUnloaded(const Extension* extension) {
+void RuntimeData::OnExtensionUnloaded(
+    content::BrowserContext* browser_context,
+    const Extension* extension) {
   extension_flags_.erase(extension->id());
 }
 
diff --git a/extensions/browser/runtime_data.h b/extensions/browser/runtime_data.h
index 1b49800..77f475b 100644
--- a/extensions/browser/runtime_data.h
+++ b/extensions/browser/runtime_data.h
@@ -52,7 +52,8 @@
   void ClearAll();
 
   // ExtensionRegistryObserver overrides. Public for testing.
-  virtual void OnExtensionUnloaded(const Extension* extension) OVERRIDE;
+  virtual void OnExtensionUnloaded(content::BrowserContext* browser_context,
+                                   const Extension* extension) OVERRIDE;
 
  private:
   // Bitmasks for runtime states.
diff --git a/extensions/browser/runtime_data_unittest.cc b/extensions/browser/runtime_data_unittest.cc
index 207f8aa..48c1bbc 100644
--- a/extensions/browser/runtime_data_unittest.cc
+++ b/extensions/browser/runtime_data_unittest.cc
@@ -39,7 +39,7 @@
 
 class RuntimeDataTest : public testing::Test {
  public:
-  RuntimeDataTest() : runtime_data_(&registry_) {}
+  RuntimeDataTest() : registry_(NULL), runtime_data_(&registry_) {}
   virtual ~RuntimeDataTest() {}
 
  protected:
@@ -100,7 +100,7 @@
   runtime_data_.SetBackgroundPageReady(extension, true);
   ASSERT_TRUE(runtime_data_.HasExtensionForTesting(extension));
 
-  runtime_data_.OnExtensionUnloaded(extension);
+  runtime_data_.OnExtensionUnloaded(NULL, extension);
   EXPECT_FALSE(runtime_data_.HasExtensionForTesting(extension));
 }