[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" |
| 8 | #include "chrome/browser/extensions/extension_prefs.h" |
| 9 | #include "chrome/common/chrome_notification_types.h" |
| 10 | #include "chrome/common/pref_names.h" |
| 11 | #include "content/public/browser/notification_service.h" |
| 12 | |
[email protected] | ef9d274 | 2013-02-26 12:48:35 | [diff] [blame] | 13 | namespace extensions { |
| 14 | |
[email protected] | 67e0a26 | 2013-03-15 13:14:49 | [diff] [blame] | 15 | InstallTracker::InstallTracker(Profile* profile, |
| 16 | extensions::ExtensionPrefs* prefs) { |
| 17 | ExtensionSorting* sorting = prefs->extension_sorting(); |
| 18 | |
| 19 | registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED, |
| 20 | content::Source<Profile>(profile)); |
| 21 | registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, |
| 22 | content::Source<Profile>(profile)); |
[email protected] | 8ed2d35 | 2013-07-03 22:26:46 | [diff] [blame^] | 23 | registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNINSTALLED, |
| 24 | content::Source<Profile>(profile)); |
[email protected] | 67e0a26 | 2013-03-15 13:14:49 | [diff] [blame] | 25 | registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LAUNCHER_REORDERED, |
| 26 | content::Source<ExtensionSorting>(sorting)); |
| 27 | registrar_.Add(this, chrome::NOTIFICATION_APP_INSTALLED_TO_APPLIST, |
| 28 | content::Source<Profile>(profile)); |
| 29 | |
| 30 | pref_change_registrar_.Init(prefs->pref_service()); |
| 31 | pref_change_registrar_.Add(extensions::ExtensionPrefs::kExtensionsPref, |
| 32 | base::Bind(&InstallTracker::OnAppsReordered, |
| 33 | base::Unretained(this))); |
[email protected] | ef9d274 | 2013-02-26 12:48:35 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | InstallTracker::~InstallTracker() { |
| 37 | } |
| 38 | |
| 39 | void InstallTracker::AddObserver(InstallObserver* observer) { |
| 40 | observers_.AddObserver(observer); |
| 41 | } |
| 42 | |
| 43 | void InstallTracker::RemoveObserver(InstallObserver* observer) { |
| 44 | observers_.RemoveObserver(observer); |
| 45 | } |
| 46 | |
| 47 | void InstallTracker::OnBeginExtensionInstall( |
| 48 | const std::string& extension_id, |
| 49 | const std::string& extension_name, |
| 50 | const gfx::ImageSkia& installing_icon, |
[email protected] | 5e69f25 | 2013-03-14 18:22:37 | [diff] [blame] | 51 | bool is_app, |
| 52 | bool is_platform_app) { |
[email protected] | ef9d274 | 2013-02-26 12:48:35 | [diff] [blame] | 53 | FOR_EACH_OBSERVER(InstallObserver, observers_, |
| 54 | OnBeginExtensionInstall(extension_id, |
| 55 | extension_name, |
| 56 | installing_icon, |
[email protected] | 5e69f25 | 2013-03-14 18:22:37 | [diff] [blame] | 57 | is_app, |
| 58 | is_platform_app)); |
[email protected] | ef9d274 | 2013-02-26 12:48:35 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | void InstallTracker::OnDownloadProgress(const std::string& extension_id, |
| 62 | int percent_downloaded) { |
| 63 | FOR_EACH_OBSERVER(InstallObserver, observers_, |
| 64 | OnDownloadProgress(extension_id, percent_downloaded)); |
| 65 | } |
| 66 | |
| 67 | void InstallTracker::OnInstallFailure( |
| 68 | const std::string& extension_id) { |
| 69 | FOR_EACH_OBSERVER(InstallObserver, observers_, |
| 70 | OnInstallFailure(extension_id)); |
| 71 | } |
| 72 | |
[email protected] | 523352c92 | 2013-02-28 01:38:52 | [diff] [blame] | 73 | void InstallTracker::Shutdown() { |
| 74 | FOR_EACH_OBSERVER(InstallObserver, observers_, OnShutdown()); |
| 75 | } |
| 76 | |
[email protected] | 67e0a26 | 2013-03-15 13:14:49 | [diff] [blame] | 77 | void InstallTracker::Observe(int type, |
| 78 | const content::NotificationSource& source, |
| 79 | const content::NotificationDetails& details) { |
| 80 | switch (type) { |
| 81 | case chrome::NOTIFICATION_EXTENSION_LOADED: { |
| 82 | const Extension* extension = |
| 83 | content::Details<const Extension>(details).ptr(); |
| 84 | FOR_EACH_OBSERVER(InstallObserver, observers_, |
| 85 | OnExtensionInstalled(extension)); |
| 86 | break; |
| 87 | } |
| 88 | case chrome::NOTIFICATION_EXTENSION_UNLOADED: { |
| 89 | const content::Details<extensions::UnloadedExtensionInfo>& unload_info( |
| 90 | details); |
| 91 | const Extension* extension = unload_info->extension; |
| 92 | if (unload_info->reason == extension_misc::UNLOAD_REASON_UNINSTALL) { |
| 93 | FOR_EACH_OBSERVER(InstallObserver, observers_, |
| 94 | OnExtensionUninstalled(extension)); |
| 95 | } else { |
| 96 | FOR_EACH_OBSERVER(InstallObserver, observers_, |
| 97 | OnExtensionDisabled(extension)); |
| 98 | } |
| 99 | break; |
| 100 | } |
[email protected] | 8ed2d35 | 2013-07-03 22:26:46 | [diff] [blame^] | 101 | case chrome::NOTIFICATION_EXTENSION_UNINSTALLED: { |
| 102 | const Extension* extension = |
| 103 | content::Details<const Extension>(details).ptr(); |
| 104 | |
| 105 | FOR_EACH_OBSERVER(InstallObserver, observers_, |
| 106 | OnExtensionUninstalled(extension)); |
| 107 | break; |
| 108 | } |
[email protected] | 67e0a26 | 2013-03-15 13:14:49 | [diff] [blame] | 109 | case chrome::NOTIFICATION_EXTENSION_LAUNCHER_REORDERED: { |
| 110 | FOR_EACH_OBSERVER(InstallObserver, observers_, OnAppsReordered()); |
| 111 | break; |
| 112 | } |
| 113 | case chrome::NOTIFICATION_APP_INSTALLED_TO_APPLIST: { |
| 114 | const std::string& extension_id( |
| 115 | *content::Details<const std::string>(details).ptr()); |
| 116 | FOR_EACH_OBSERVER(InstallObserver, observers_, |
| 117 | OnAppInstalledToAppList(extension_id)); |
| 118 | break; |
| 119 | } |
| 120 | default: |
| 121 | NOTREACHED(); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | void InstallTracker::OnAppsReordered() { |
| 126 | FOR_EACH_OBSERVER(InstallObserver, observers_, OnAppsReordered()); |
| 127 | } |
| 128 | |
[email protected] | ef9d274 | 2013-02-26 12:48:35 | [diff] [blame] | 129 | } // namespace extensions |