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