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/install_tracker_factory.h"
     10 #include "chrome/browser/profiles/profile.h"
     11 #include "chrome/common/pref_names.h"
     12 #include "content/public/browser/notification_service.h"
     13 #include "extensions/browser/extension_prefs.h"
     14 #include "extensions/browser/pref_names.h"
     15 
     16 namespace extensions {
     17 
     18 InstallTracker::InstallTracker(Profile* profile,
     19                                extensions::ExtensionPrefs* prefs) {
     20   registrar_.Add(this,
     21                  chrome::NOTIFICATION_EXTENSION_UPDATE_DISABLED,
     22                  content::Source<Profile>(profile));
     23   AppSorting* sorting = prefs->app_sorting();
     24   registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LAUNCHER_REORDERED,
     25       content::Source<AppSorting>(sorting));
     26   registrar_.Add(this, chrome::NOTIFICATION_APP_INSTALLED_TO_APPLIST,
     27       content::Source<Profile>(profile));
     28 
     29   pref_change_registrar_.Init(prefs->pref_service());
     30   pref_change_registrar_.Add(pref_names::kExtensions,
     31                              base::Bind(&InstallTracker::OnAppsReordered,
     32                                         base::Unretained(this)));
     33 }
     34 
     35 InstallTracker::~InstallTracker() {
     36 }
     37 
     38 // static
     39 InstallTracker* InstallTracker::Get(content::BrowserContext* context) {
     40   return InstallTrackerFactory::GetForProfile(
     41       Profile::FromBrowserContext(context));
     42 }
     43 
     44 void InstallTracker::AddObserver(InstallObserver* observer) {
     45   observers_.AddObserver(observer);
     46 }
     47 
     48 void InstallTracker::RemoveObserver(InstallObserver* observer) {
     49   observers_.RemoveObserver(observer);
     50 }
     51 
     52 void InstallTracker::OnBeginExtensionInstall(
     53     const InstallObserver::ExtensionInstallParams& params) {
     54   FOR_EACH_OBSERVER(InstallObserver, observers_,
     55                     OnBeginExtensionInstall(params));
     56 }
     57 
     58 void InstallTracker::OnBeginExtensionDownload(const std::string& extension_id) {
     59   FOR_EACH_OBSERVER(
     60       InstallObserver, observers_, OnBeginExtensionDownload(extension_id));
     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::OnBeginCrxInstall(const std::string& extension_id) {
     70   FOR_EACH_OBSERVER(
     71       InstallObserver, observers_, OnBeginCrxInstall(extension_id));
     72 }
     73 
     74 void InstallTracker::OnFinishCrxInstall(const std::string& extension_id,
     75                                         bool success) {
     76   FOR_EACH_OBSERVER(
     77       InstallObserver, observers_, OnFinishCrxInstall(extension_id, success));
     78 }
     79 
     80 void InstallTracker::OnInstallFailure(
     81     const std::string& extension_id) {
     82   FOR_EACH_OBSERVER(InstallObserver, observers_,
     83                     OnInstallFailure(extension_id));
     84 }
     85 
     86 void InstallTracker::Shutdown() {
     87   FOR_EACH_OBSERVER(InstallObserver, observers_, OnShutdown());
     88 }
     89 
     90 void InstallTracker::Observe(int type,
     91                              const content::NotificationSource& source,
     92                              const content::NotificationDetails& details) {
     93   switch (type) {
     94     case chrome::NOTIFICATION_EXTENSION_UPDATE_DISABLED: {
     95       const Extension* extension =
     96           content::Details<const Extension>(details).ptr();
     97       FOR_EACH_OBSERVER(
     98           InstallObserver, observers_, OnDisabledExtensionUpdated(extension));
     99       break;
    100     }
    101     case chrome::NOTIFICATION_EXTENSION_LAUNCHER_REORDERED: {
    102       FOR_EACH_OBSERVER(InstallObserver, observers_, OnAppsReordered());
    103       break;
    104     }
    105     case chrome::NOTIFICATION_APP_INSTALLED_TO_APPLIST: {
    106       const std::string& extension_id(
    107           *content::Details<const std::string>(details).ptr());
    108       FOR_EACH_OBSERVER(InstallObserver, observers_,
    109                         OnAppInstalledToAppList(extension_id));
    110       break;
    111     }
    112     default:
    113       NOTREACHED();
    114   }
    115 }
    116 
    117 void InstallTracker::OnAppsReordered() {
    118   FOR_EACH_OBSERVER(InstallObserver, observers_, OnAppsReordered());
    119 }
    120 
    121 }  // namespace extensions
    122