Home | History | Annotate | Download | only in extensions
      1 // Copyright 2013 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "chrome/browser/extensions/install_tracker.h"
      6 
      7 #include "base/bind.h"
      8 #include "chrome/browser/chrome_notification_types.h"
      9 #include "chrome/browser/extensions/extension_prefs.h"
     10 #include "chrome/common/pref_names.h"
     11 #include "content/public/browser/notification_service.h"
     12 
     13 namespace extensions {
     14 
     15 InstallTracker::InstallTracker(Profile* profile,
     16                                extensions::ExtensionPrefs* prefs) {
     17   ExtensionSorting* sorting = prefs->extension_sorting();
     18 
     19   registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_INSTALLED,
     20       content::Source<Profile>(profile));
     21   registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED,
     22       content::Source<Profile>(profile));
     23   registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED,
     24       content::Source<Profile>(profile));
     25   registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNINSTALLED,
     26       content::Source<Profile>(profile));
     27   registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LAUNCHER_REORDERED,
     28       content::Source<ExtensionSorting>(sorting));
     29   registrar_.Add(this, chrome::NOTIFICATION_APP_INSTALLED_TO_APPLIST,
     30       content::Source<Profile>(profile));
     31 
     32   pref_change_registrar_.Init(prefs->pref_service());
     33   pref_change_registrar_.Add(extensions::ExtensionPrefs::kExtensionsPref,
     34                              base::Bind(&InstallTracker::OnAppsReordered,
     35                                         base::Unretained(this)));
     36 }
     37 
     38 InstallTracker::~InstallTracker() {
     39 }
     40 
     41 void InstallTracker::AddObserver(InstallObserver* observer) {
     42   observers_.AddObserver(observer);
     43 }
     44 
     45 void InstallTracker::RemoveObserver(InstallObserver* observer) {
     46   observers_.RemoveObserver(observer);
     47 }
     48 
     49 void InstallTracker::OnBeginExtensionInstall(
     50     const std::string& extension_id,
     51     const std::string& extension_name,
     52     const gfx::ImageSkia& installing_icon,
     53     bool is_app,
     54     bool is_platform_app) {
     55   FOR_EACH_OBSERVER(InstallObserver, observers_,
     56                     OnBeginExtensionInstall(extension_id,
     57                                             extension_name,
     58                                             installing_icon,
     59                                             is_app,
     60                                             is_platform_app));
     61 }
     62 
     63 void InstallTracker::OnDownloadProgress(const std::string& extension_id,
     64                                         int percent_downloaded) {
     65   FOR_EACH_OBSERVER(InstallObserver, observers_,
     66                     OnDownloadProgress(extension_id, percent_downloaded));
     67 }
     68 
     69 void InstallTracker::OnInstallFailure(
     70     const std::string& extension_id) {
     71   FOR_EACH_OBSERVER(InstallObserver, observers_,
     72                     OnInstallFailure(extension_id));
     73 }
     74 
     75 void InstallTracker::Shutdown() {
     76   FOR_EACH_OBSERVER(InstallObserver, observers_, OnShutdown());
     77 }
     78 
     79 void InstallTracker::Observe(int type,
     80                              const content::NotificationSource& source,
     81                              const content::NotificationDetails& details) {
     82   switch (type) {
     83     case chrome::NOTIFICATION_EXTENSION_INSTALLED: {
     84       const Extension* extension =
     85           content::Details<const InstalledExtensionInfo>(details).ptr()->
     86               extension;
     87       FOR_EACH_OBSERVER(InstallObserver, observers_,
     88                         OnExtensionInstalled(extension));
     89       break;
     90     }
     91     case chrome::NOTIFICATION_EXTENSION_LOADED: {
     92       const Extension* extension =
     93           content::Details<const Extension>(details).ptr();
     94       FOR_EACH_OBSERVER(InstallObserver, observers_,
     95                         OnExtensionLoaded(extension));
     96       break;
     97     }
     98     case chrome::NOTIFICATION_EXTENSION_UNLOADED: {
     99       const content::Details<extensions::UnloadedExtensionInfo>& unload_info(
    100           details);
    101       const Extension* extension = unload_info->extension;
    102       FOR_EACH_OBSERVER(InstallObserver, observers_,
    103                         OnExtensionUnloaded(extension));
    104       break;
    105     }
    106     case chrome::NOTIFICATION_EXTENSION_UNINSTALLED: {
    107       const Extension* extension =
    108           content::Details<const Extension>(details).ptr();
    109 
    110       FOR_EACH_OBSERVER(InstallObserver, observers_,
    111                         OnExtensionUninstalled(extension));
    112       break;
    113     }
    114     case chrome::NOTIFICATION_EXTENSION_LAUNCHER_REORDERED: {
    115       FOR_EACH_OBSERVER(InstallObserver, observers_, OnAppsReordered());
    116       break;
    117     }
    118     case chrome::NOTIFICATION_APP_INSTALLED_TO_APPLIST: {
    119       const std::string& extension_id(
    120           *content::Details<const std::string>(details).ptr());
    121       FOR_EACH_OBSERVER(InstallObserver, observers_,
    122                         OnAppInstalledToAppList(extension_id));
    123       break;
    124     }
    125     default:
    126       NOTREACHED();
    127   }
    128 }
    129 
    130 void InstallTracker::OnAppsReordered() {
    131   FOR_EACH_OBSERVER(InstallObserver, observers_, OnAppsReordered());
    132 }
    133 
    134 }  // namespace extensions
    135