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/install_tracker_factory.h"
10 #include "content/public/browser/notification_service.h"
11 #include "extensions/browser/extension_prefs.h"
12 #include "extensions/browser/extension_registry.h"
13 #include "extensions/browser/pref_names.h"
15 namespace extensions
{
17 InstallTracker::InstallTracker(content::BrowserContext
* browser_context
,
18 extensions::ExtensionPrefs
* prefs
)
19 : extension_registry_observer_(this) {
21 extensions::NOTIFICATION_EXTENSION_UPDATE_DISABLED
,
22 content::Source
<content::BrowserContext
>(browser_context
));
23 extension_registry_observer_
.Add(ExtensionRegistry::Get(browser_context
));
25 // Prefs may be null in tests.
27 AppSorting
* sorting
= prefs
->app_sorting();
29 chrome::NOTIFICATION_APP_LAUNCHER_REORDERED
,
30 content::Source
<AppSorting
>(sorting
));
31 pref_change_registrar_
.Init(prefs
->pref_service());
32 pref_change_registrar_
.Add(
33 pref_names::kExtensions
,
34 base::Bind(&InstallTracker::OnAppsReordered
, base::Unretained(this)));
38 InstallTracker::~InstallTracker() {
42 InstallTracker
* InstallTracker::Get(content::BrowserContext
* context
) {
43 return InstallTrackerFactory::GetForBrowserContext(context
);
46 void InstallTracker::AddObserver(InstallObserver
* observer
) {
47 observers_
.AddObserver(observer
);
50 void InstallTracker::RemoveObserver(InstallObserver
* observer
) {
51 observers_
.RemoveObserver(observer
);
54 const ActiveInstallData
* InstallTracker::GetActiveInstall(
55 const std::string
& extension_id
) const {
56 ActiveInstallsMap::const_iterator install_data
=
57 active_installs_
.find(extension_id
);
58 if (install_data
== active_installs_
.end())
61 return &(install_data
->second
);
64 void InstallTracker::AddActiveInstall(const ActiveInstallData
& install_data
) {
65 DCHECK(!install_data
.extension_id
.empty());
66 DCHECK(active_installs_
.find(install_data
.extension_id
) ==
67 active_installs_
.end());
68 active_installs_
.insert(
69 std::make_pair(install_data
.extension_id
, install_data
));
72 void InstallTracker::RemoveActiveInstall(const std::string
& extension_id
) {
73 active_installs_
.erase(extension_id
);
76 void InstallTracker::OnBeginExtensionInstall(
77 const InstallObserver::ExtensionInstallParams
& params
) {
78 ActiveInstallsMap::iterator install_data
=
79 active_installs_
.find(params
.extension_id
);
80 if (install_data
== active_installs_
.end()) {
81 ActiveInstallData
install_data(params
.extension_id
);
82 install_data
.is_ephemeral
= params
.is_ephemeral
;
83 active_installs_
.insert(std::make_pair(params
.extension_id
, install_data
));
86 FOR_EACH_OBSERVER(InstallObserver
, observers_
,
87 OnBeginExtensionInstall(params
));
90 void InstallTracker::OnBeginExtensionDownload(const std::string
& extension_id
) {
92 InstallObserver
, observers_
, OnBeginExtensionDownload(extension_id
));
95 void InstallTracker::OnDownloadProgress(const std::string
& extension_id
,
96 int percent_downloaded
) {
97 ActiveInstallsMap::iterator install_data
=
98 active_installs_
.find(extension_id
);
99 if (install_data
!= active_installs_
.end()) {
100 install_data
->second
.percent_downloaded
= percent_downloaded
;
105 FOR_EACH_OBSERVER(InstallObserver
, observers_
,
106 OnDownloadProgress(extension_id
, percent_downloaded
));
109 void InstallTracker::OnBeginCrxInstall(const std::string
& extension_id
) {
111 InstallObserver
, observers_
, OnBeginCrxInstall(extension_id
));
114 void InstallTracker::OnFinishCrxInstall(const std::string
& extension_id
,
117 InstallObserver
, observers_
, OnFinishCrxInstall(extension_id
, success
));
120 void InstallTracker::OnInstallFailure(
121 const std::string
& extension_id
) {
122 RemoveActiveInstall(extension_id
);
123 FOR_EACH_OBSERVER(InstallObserver
, observers_
,
124 OnInstallFailure(extension_id
));
127 void InstallTracker::Shutdown() {
128 FOR_EACH_OBSERVER(InstallObserver
, observers_
, OnShutdown());
131 void InstallTracker::Observe(int type
,
132 const content::NotificationSource
& source
,
133 const content::NotificationDetails
& details
) {
135 case extensions::NOTIFICATION_EXTENSION_UPDATE_DISABLED
: {
136 const Extension
* extension
=
137 content::Details
<const Extension
>(details
).ptr();
139 InstallObserver
, observers_
, OnDisabledExtensionUpdated(extension
));
142 case chrome::NOTIFICATION_APP_LAUNCHER_REORDERED
: {
143 FOR_EACH_OBSERVER(InstallObserver
, observers_
, OnAppsReordered());
151 void InstallTracker::OnExtensionInstalled(
152 content::BrowserContext
* browser_context
,
153 const Extension
* extension
,
155 RemoveActiveInstall(extension
->id());
158 void InstallTracker::OnAppsReordered() {
159 FOR_EACH_OBSERVER(InstallObserver
, observers_
, OnAppsReordered());
162 } // namespace extensions