[email protected] | ef9d274 | 2013-02-26 12:48:35 | [diff] [blame] | 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 | |
[email protected] | 67e0a26 | 2013-03-15 13:14:49 | [diff] [blame] | 7 | #include "base/bind.h" |
[email protected] | 49a01e64 | 2013-07-12 00:29:45 | [diff] [blame] | 8 | #include "chrome/browser/chrome_notification_types.h" |
[email protected] | 90878c5 | 2014-04-04 18:21:02 | [diff] [blame] | 9 | #include "chrome/browser/extensions/install_tracker_factory.h" |
[email protected] | 67e0a26 | 2013-03-15 13:14:49 | [diff] [blame] | 10 | #include "content/public/browser/notification_service.h" |
[email protected] | 489db084 | 2014-01-22 18:20:03 | [diff] [blame] | 11 | #include "extensions/browser/extension_prefs.h" |
deepak.m1 | 4ba69e6 | 2015-11-17 05:42:12 | [diff] [blame] | 12 | #include "extensions/browser/extension_system.h" |
[email protected] | 234fc5ff | 2014-01-16 23:32:28 | [diff] [blame] | 13 | #include "extensions/browser/pref_names.h" |
[email protected] | 67e0a26 | 2013-03-15 13:14:49 | [diff] [blame] | 14 | |
[email protected] | ef9d274 | 2013-02-26 12:48:35 | [diff] [blame] | 15 | namespace extensions { |
| 16 | |
[email protected] | 6d6579a | 2014-07-30 14:24:13 | [diff] [blame] | 17 | InstallTracker::InstallTracker(content::BrowserContext* browser_context, |
Evan Stade | 75872a6 | 2019-09-06 21:17:38 | [diff] [blame] | 18 | extensions::ExtensionPrefs* prefs) { |
[email protected] | 8c17903 | 2014-03-20 22:49:10 | [diff] [blame] | 19 | registrar_.Add(this, |
[email protected] | adf5a10 | 2014-07-31 12:44:06 | [diff] [blame] | 20 | extensions::NOTIFICATION_EXTENSION_UPDATE_DISABLED, |
[email protected] | 6d6579a | 2014-07-30 14:24:13 | [diff] [blame] | 21 | content::Source<content::BrowserContext>(browser_context)); |
[email protected] | 6d6579a | 2014-07-30 14:24:13 | [diff] [blame] | 22 | extension_registry_observer_.Add(ExtensionRegistry::Get(browser_context)); |
[email protected] | 67e0a26 | 2013-03-15 13:14:49 | [diff] [blame] | 23 | |
[email protected] | 773272b | 2014-07-18 05:48:35 | [diff] [blame] | 24 | // Prefs may be null in tests. |
| 25 | if (prefs) { |
deepak.m1 | 4ba69e6 | 2015-11-17 05:42:12 | [diff] [blame] | 26 | AppSorting* sorting = ExtensionSystem::Get(browser_context)->app_sorting(); |
[email protected] | 773272b | 2014-07-18 05:48:35 | [diff] [blame] | 27 | registrar_.Add(this, |
[email protected] | adf5a10 | 2014-07-31 12:44:06 | [diff] [blame] | 28 | chrome::NOTIFICATION_APP_LAUNCHER_REORDERED, |
[email protected] | 773272b | 2014-07-18 05:48:35 | [diff] [blame] | 29 | content::Source<AppSorting>(sorting)); |
| 30 | pref_change_registrar_.Init(prefs->pref_service()); |
| 31 | pref_change_registrar_.Add( |
| 32 | pref_names::kExtensions, |
| 33 | base::Bind(&InstallTracker::OnAppsReordered, base::Unretained(this))); |
| 34 | } |
[email protected] | ef9d274 | 2013-02-26 12:48:35 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | InstallTracker::~InstallTracker() { |
| 38 | } |
| 39 | |
[email protected] | 90878c5 | 2014-04-04 18:21:02 | [diff] [blame] | 40 | // static |
| 41 | InstallTracker* InstallTracker::Get(content::BrowserContext* context) { |
[email protected] | 6d6579a | 2014-07-30 14:24:13 | [diff] [blame] | 42 | return InstallTrackerFactory::GetForBrowserContext(context); |
[email protected] | 90878c5 | 2014-04-04 18:21:02 | [diff] [blame] | 43 | } |
| 44 | |
[email protected] | ef9d274 | 2013-02-26 12:48:35 | [diff] [blame] | 45 | void InstallTracker::AddObserver(InstallObserver* observer) { |
| 46 | observers_.AddObserver(observer); |
| 47 | } |
| 48 | |
| 49 | void InstallTracker::RemoveObserver(InstallObserver* observer) { |
| 50 | observers_.RemoveObserver(observer); |
| 51 | } |
| 52 | |
[email protected] | 773272b | 2014-07-18 05:48:35 | [diff] [blame] | 53 | const ActiveInstallData* InstallTracker::GetActiveInstall( |
| 54 | const std::string& extension_id) const { |
jdoerrie | 13cd648c8 | 2018-10-02 21:21:02 | [diff] [blame] | 55 | auto install_data = active_installs_.find(extension_id); |
[email protected] | 773272b | 2014-07-18 05:48:35 | [diff] [blame] | 56 | if (install_data == active_installs_.end()) |
| 57 | return NULL; |
| 58 | else |
| 59 | return &(install_data->second); |
| 60 | } |
| 61 | |
| 62 | void InstallTracker::AddActiveInstall(const ActiveInstallData& install_data) { |
| 63 | DCHECK(!install_data.extension_id.empty()); |
| 64 | DCHECK(active_installs_.find(install_data.extension_id) == |
| 65 | active_installs_.end()); |
| 66 | active_installs_.insert( |
| 67 | std::make_pair(install_data.extension_id, install_data)); |
| 68 | } |
| 69 | |
| 70 | void InstallTracker::RemoveActiveInstall(const std::string& extension_id) { |
| 71 | active_installs_.erase(extension_id); |
| 72 | } |
| 73 | |
[email protected] | ef9d274 | 2013-02-26 12:48:35 | [diff] [blame] | 74 | void InstallTracker::OnBeginExtensionInstall( |
[email protected] | fa517d0 | 2013-11-18 09:33:09 | [diff] [blame] | 75 | const InstallObserver::ExtensionInstallParams& params) { |
jdoerrie | 13cd648c8 | 2018-10-02 21:21:02 | [diff] [blame] | 76 | auto install_data = active_installs_.find(params.extension_id); |
[email protected] | 773272b | 2014-07-18 05:48:35 | [diff] [blame] | 77 | if (install_data == active_installs_.end()) { |
| 78 | ActiveInstallData install_data(params.extension_id); |
[email protected] | 773272b | 2014-07-18 05:48:35 | [diff] [blame] | 79 | active_installs_.insert(std::make_pair(params.extension_id, install_data)); |
| 80 | } |
| 81 | |
ericwilligers | b5f79de | 2016-10-19 04:15:10 | [diff] [blame] | 82 | for (auto& observer : observers_) |
| 83 | observer.OnBeginExtensionInstall(params); |
[email protected] | ef9d274 | 2013-02-26 12:48:35 | [diff] [blame] | 84 | } |
| 85 | |
[email protected] | c80fe5f | 2014-03-26 04:36:30 | [diff] [blame] | 86 | void InstallTracker::OnBeginExtensionDownload(const std::string& extension_id) { |
ericwilligers | b5f79de | 2016-10-19 04:15:10 | [diff] [blame] | 87 | for (auto& observer : observers_) |
| 88 | observer.OnBeginExtensionDownload(extension_id); |
[email protected] | c80fe5f | 2014-03-26 04:36:30 | [diff] [blame] | 89 | } |
| 90 | |
[email protected] | ef9d274 | 2013-02-26 12:48:35 | [diff] [blame] | 91 | void InstallTracker::OnDownloadProgress(const std::string& extension_id, |
| 92 | int percent_downloaded) { |
jdoerrie | 13cd648c8 | 2018-10-02 21:21:02 | [diff] [blame] | 93 | auto install_data = active_installs_.find(extension_id); |
[email protected] | 773272b | 2014-07-18 05:48:35 | [diff] [blame] | 94 | if (install_data != active_installs_.end()) { |
| 95 | install_data->second.percent_downloaded = percent_downloaded; |
| 96 | } else { |
| 97 | NOTREACHED(); |
| 98 | } |
| 99 | |
ericwilligers | b5f79de | 2016-10-19 04:15:10 | [diff] [blame] | 100 | for (auto& observer : observers_) |
| 101 | observer.OnDownloadProgress(extension_id, percent_downloaded); |
[email protected] | ef9d274 | 2013-02-26 12:48:35 | [diff] [blame] | 102 | } |
| 103 | |
[email protected] | c80fe5f | 2014-03-26 04:36:30 | [diff] [blame] | 104 | void InstallTracker::OnBeginCrxInstall(const std::string& extension_id) { |
ericwilligers | b5f79de | 2016-10-19 04:15:10 | [diff] [blame] | 105 | for (auto& observer : observers_) |
| 106 | observer.OnBeginCrxInstall(extension_id); |
[email protected] | c80fe5f | 2014-03-26 04:36:30 | [diff] [blame] | 107 | } |
| 108 | |
[email protected] | 73cd487c | 2014-04-16 05:22:50 | [diff] [blame] | 109 | void InstallTracker::OnFinishCrxInstall(const std::string& extension_id, |
| 110 | bool success) { |
ericwilligers | b5f79de | 2016-10-19 04:15:10 | [diff] [blame] | 111 | for (auto& observer : observers_) |
| 112 | observer.OnFinishCrxInstall(extension_id, success); |
[email protected] | 73cd487c | 2014-04-16 05:22:50 | [diff] [blame] | 113 | } |
| 114 | |
[email protected] | ef9d274 | 2013-02-26 12:48:35 | [diff] [blame] | 115 | void InstallTracker::OnInstallFailure( |
| 116 | const std::string& extension_id) { |
[email protected] | 773272b | 2014-07-18 05:48:35 | [diff] [blame] | 117 | RemoveActiveInstall(extension_id); |
ericwilligers | b5f79de | 2016-10-19 04:15:10 | [diff] [blame] | 118 | for (auto& observer : observers_) |
| 119 | observer.OnInstallFailure(extension_id); |
[email protected] | ef9d274 | 2013-02-26 12:48:35 | [diff] [blame] | 120 | } |
| 121 | |
[email protected] | 523352c92 | 2013-02-28 01:38:52 | [diff] [blame] | 122 | void InstallTracker::Shutdown() { |
ericwilligers | b5f79de | 2016-10-19 04:15:10 | [diff] [blame] | 123 | for (auto& observer : observers_) |
| 124 | observer.OnShutdown(); |
[email protected] | 523352c92 | 2013-02-28 01:38:52 | [diff] [blame] | 125 | } |
| 126 | |
[email protected] | 67e0a26 | 2013-03-15 13:14:49 | [diff] [blame] | 127 | void InstallTracker::Observe(int type, |
| 128 | const content::NotificationSource& source, |
| 129 | const content::NotificationDetails& details) { |
| 130 | switch (type) { |
[email protected] | adf5a10 | 2014-07-31 12:44:06 | [diff] [blame] | 131 | case extensions::NOTIFICATION_EXTENSION_UPDATE_DISABLED: { |
[email protected] | 8c17903 | 2014-03-20 22:49:10 | [diff] [blame] | 132 | const Extension* extension = |
| 133 | content::Details<const Extension>(details).ptr(); |
ericwilligers | b5f79de | 2016-10-19 04:15:10 | [diff] [blame] | 134 | for (auto& observer : observers_) |
| 135 | observer.OnDisabledExtensionUpdated(extension); |
[email protected] | 8c17903 | 2014-03-20 22:49:10 | [diff] [blame] | 136 | break; |
| 137 | } |
[email protected] | adf5a10 | 2014-07-31 12:44:06 | [diff] [blame] | 138 | case chrome::NOTIFICATION_APP_LAUNCHER_REORDERED: { |
ericwilligers | b5f79de | 2016-10-19 04:15:10 | [diff] [blame] | 139 | for (auto& observer : observers_) |
| 140 | observer.OnAppsReordered(); |
[email protected] | 67e0a26 | 2013-03-15 13:14:49 | [diff] [blame] | 141 | break; |
| 142 | } |
[email protected] | 67e0a26 | 2013-03-15 13:14:49 | [diff] [blame] | 143 | default: |
| 144 | NOTREACHED(); |
| 145 | } |
| 146 | } |
| 147 | |
[email protected] | 773272b | 2014-07-18 05:48:35 | [diff] [blame] | 148 | void InstallTracker::OnExtensionInstalled( |
| 149 | content::BrowserContext* browser_context, |
| 150 | const Extension* extension, |
| 151 | bool is_update) { |
| 152 | RemoveActiveInstall(extension->id()); |
| 153 | } |
| 154 | |
[email protected] | 67e0a26 | 2013-03-15 13:14:49 | [diff] [blame] | 155 | void InstallTracker::OnAppsReordered() { |
ericwilligers | b5f79de | 2016-10-19 04:15:10 | [diff] [blame] | 156 | for (auto& observer : observers_) |
| 157 | observer.OnAppsReordered(); |
[email protected] | 67e0a26 | 2013-03-15 13:14:49 | [diff] [blame] | 158 | } |
| 159 | |
[email protected] | ef9d274 | 2013-02-26 12:48:35 | [diff] [blame] | 160 | } // namespace extensions |