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.
5 #include "chrome/browser/extensions/install_tracker.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 #include "extensions/browser/pref_names.h"
14 namespace extensions
{
16 InstallTracker::InstallTracker(Profile
* profile
,
17 extensions::ExtensionPrefs
* prefs
) {
18 AppSorting
* sorting
= prefs
->app_sorting();
20 registrar_
.Add(this, chrome::NOTIFICATION_EXTENSION_INSTALLED
,
21 content::Source
<Profile
>(profile
));
22 registrar_
.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED
,
23 content::Source
<Profile
>(profile
));
24 registrar_
.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED
,
25 content::Source
<Profile
>(profile
));
26 registrar_
.Add(this, chrome::NOTIFICATION_EXTENSION_UNINSTALLED
,
27 content::Source
<Profile
>(profile
));
28 registrar_
.Add(this, chrome::NOTIFICATION_EXTENSION_LAUNCHER_REORDERED
,
29 content::Source
<AppSorting
>(sorting
));
30 registrar_
.Add(this, chrome::NOTIFICATION_APP_INSTALLED_TO_APPLIST
,
31 content::Source
<Profile
>(profile
));
33 pref_change_registrar_
.Init(prefs
->pref_service());
34 pref_change_registrar_
.Add(pref_names::kExtensions
,
35 base::Bind(&InstallTracker::OnAppsReordered
,
36 base::Unretained(this)));
39 InstallTracker::~InstallTracker() {
42 void InstallTracker::AddObserver(InstallObserver
* observer
) {
43 observers_
.AddObserver(observer
);
46 void InstallTracker::RemoveObserver(InstallObserver
* observer
) {
47 observers_
.RemoveObserver(observer
);
50 void InstallTracker::OnBeginExtensionInstall(
51 const InstallObserver::ExtensionInstallParams
& params
) {
52 FOR_EACH_OBSERVER(InstallObserver
, observers_
,
53 OnBeginExtensionInstall(params
));
56 void InstallTracker::OnDownloadProgress(const std::string
& extension_id
,
57 int percent_downloaded
) {
58 FOR_EACH_OBSERVER(InstallObserver
, observers_
,
59 OnDownloadProgress(extension_id
, percent_downloaded
));
62 void InstallTracker::OnInstallFailure(
63 const std::string
& extension_id
) {
64 FOR_EACH_OBSERVER(InstallObserver
, observers_
,
65 OnInstallFailure(extension_id
));
68 void InstallTracker::Shutdown() {
69 FOR_EACH_OBSERVER(InstallObserver
, observers_
, OnShutdown());
72 void InstallTracker::Observe(int type
,
73 const content::NotificationSource
& source
,
74 const content::NotificationDetails
& details
) {
76 case chrome::NOTIFICATION_EXTENSION_INSTALLED
: {
77 const Extension
* extension
=
78 content::Details
<const InstalledExtensionInfo
>(details
).ptr()->
80 FOR_EACH_OBSERVER(InstallObserver
, observers_
,
81 OnExtensionInstalled(extension
));
84 case chrome::NOTIFICATION_EXTENSION_LOADED
: {
85 const Extension
* extension
=
86 content::Details
<const Extension
>(details
).ptr();
87 FOR_EACH_OBSERVER(InstallObserver
, observers_
,
88 OnExtensionLoaded(extension
));
91 case chrome::NOTIFICATION_EXTENSION_UNLOADED
: {
92 const content::Details
<extensions::UnloadedExtensionInfo
>& unload_info(
94 const Extension
* extension
= unload_info
->extension
;
95 FOR_EACH_OBSERVER(InstallObserver
, observers_
,
96 OnExtensionUnloaded(extension
));
99 case chrome::NOTIFICATION_EXTENSION_UNINSTALLED
: {
100 const Extension
* extension
=
101 content::Details
<const Extension
>(details
).ptr();
103 FOR_EACH_OBSERVER(InstallObserver
, observers_
,
104 OnExtensionUninstalled(extension
));
107 case chrome::NOTIFICATION_EXTENSION_LAUNCHER_REORDERED
: {
108 FOR_EACH_OBSERVER(InstallObserver
, observers_
, OnAppsReordered());
111 case chrome::NOTIFICATION_APP_INSTALLED_TO_APPLIST
: {
112 const std::string
& extension_id(
113 *content::Details
<const std::string
>(details
).ptr());
114 FOR_EACH_OBSERVER(InstallObserver
, observers_
,
115 OnAppInstalledToAppList(extension_id
));
123 void InstallTracker::OnAppsReordered() {
124 FOR_EACH_OBSERVER(InstallObserver
, observers_
, OnAppsReordered());
127 } // namespace extensions