Chromium Code Reviews| Index: chrome/browser/extensions/api/media_galleries_private/media_gallery_extension_notification_observer.cc |
| diff --git a/chrome/browser/extensions/api/media_galleries_private/media_gallery_extension_notification_observer.cc b/chrome/browser/extensions/api/media_galleries_private/media_gallery_extension_notification_observer.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..4c916b44d0793661ccf52be5eb8fd5e6751a2b0b |
| --- /dev/null |
| +++ b/chrome/browser/extensions/api/media_galleries_private/media_gallery_extension_notification_observer.cc |
| @@ -0,0 +1,87 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +// MediaGalleryExtensionNotificationObserver implementation. |
| + |
| +#include "chrome/browser/extensions/api/media_galleries_private/media_gallery_extension_notification_observer.h" |
| + |
| +#include <string> |
| + |
| +#include "base/bind.h" |
| +#include "base/location.h" |
| +#include "chrome/browser/extensions/api/media_galleries_private/gallery_watch_manager.h" |
| +#include "chrome/browser/extensions/api/media_galleries_private/gallery_watch_manager_factory.h" |
| +#include "chrome/browser/extensions/extension_host.h" |
| +#include "chrome/browser/profiles/profile.h" |
| +#include "chrome/common/chrome_notification_types.h" |
| +#include "chrome/common/extensions/extension.h" |
| +#include "content/public/browser/browser_thread.h" |
| +#include "content/public/browser/notification_details.h" |
| +#include "content/public/browser/notification_service.h" |
| + |
| +namespace extensions { |
| + |
| +using content::BrowserThread; |
| + |
| +namespace { |
| + |
| +// Handles the extension destroyed event on the file thread. |
| +void HandleExtensionDestroyedOnFileThread(const Profile* profile, |
| + const std::string& extension_id) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| + if (!GalleryWatchManagerFactory::GetInstance()->HasForProfile(profile)) |
| + return; |
| + GalleryWatchManager* manager = |
| + GalleryWatchManagerFactory::GetInstance()->GetForProfile(profile); |
| + if (!manager) |
|
Lei Zhang
2012/12/15 01:11:54
How can this happen when HasForProfile() just retu
kmadhusu
2012/12/17 23:58:05
You are right. |manager| can never be null here. F
|
| + return; |
| + manager->OnExtensionDestroyed(extension_id); |
| +} |
| + |
| +} // namespace |
| + |
| +MediaGalleryExtensionNotificationObserver:: |
| +MediaGalleryExtensionNotificationObserver(Profile* profile) |
| + : profile_(profile) { |
| + DCHECK(profile_); |
| + registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED, |
| + content::Source<Profile>(profile_->GetOriginalProfile())); |
| + registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, |
| + content::Source<Profile>(profile_->GetOriginalProfile())); |
| +} |
| + |
| +MediaGalleryExtensionNotificationObserver:: |
| +~MediaGalleryExtensionNotificationObserver() { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| +} |
| + |
| +void MediaGalleryExtensionNotificationObserver::Observe( |
| + int type, |
| + const content::NotificationSource& source, |
| + const content::NotificationDetails& details) { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + if (type == chrome::NOTIFICATION_EXTENSION_UNLOADED) { |
| + const Extension* extension = |
| + content::Details<extensions::UnloadedExtensionInfo>(details)->extension; |
| + content::BrowserThread::PostTask( |
| + content::BrowserThread::FILE, FROM_HERE, |
| + base::Bind(&HandleExtensionDestroyedOnFileThread, |
| + profile_, |
| + extension->id())); |
| + |
| + } else if (type == chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED) { |
| + ExtensionHost* host = content::Details<ExtensionHost>(details).ptr(); |
| + const Extension* extension = host->extension(); |
| + content::BrowserThread::PostTask( |
|
Lei Zhang
2012/12/15 01:11:54
This block looks a lot like the previous one. You
kmadhusu
2012/12/17 23:58:05
Done.
|
| + content::BrowserThread::FILE, FROM_HERE, |
| + base::Bind(&HandleExtensionDestroyedOnFileThread, |
| + profile_, |
| + extension->id())); |
| + |
| + } else { |
| + NOTREACHED(); |
|
Lei Zhang
2012/12/15 01:11:54
You don't need this. You can write the code as:
if
kmadhusu
2012/12/17 23:58:05
Done.
|
| + } |
| + } |
| + |
| +} // namespace extensions |