Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(428)

Side by Side Diff: chrome/browser/extensions/install_tracker.cc

Issue 389613006: Prevent duplicate concurrent installs of the same extension (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/extensions/install_tracker.h" 5 #include "chrome/browser/extensions/install_tracker.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "chrome/browser/chrome_notification_types.h" 8 #include "chrome/browser/chrome_notification_types.h"
9 #include "chrome/browser/extensions/install_tracker_factory.h" 9 #include "chrome/browser/extensions/install_tracker_factory.h"
10 #include "chrome/browser/profiles/profile.h" 10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/common/pref_names.h" 11 #include "chrome/common/pref_names.h"
12 #include "content/public/browser/notification_service.h" 12 #include "content/public/browser/notification_service.h"
13 #include "extensions/browser/extension_prefs.h" 13 #include "extensions/browser/extension_prefs.h"
14 #include "extensions/browser/extension_registry.h"
14 #include "extensions/browser/pref_names.h" 15 #include "extensions/browser/pref_names.h"
15 16
16 namespace extensions { 17 namespace extensions {
17 18
19 InstallTracker::InstallProgressData::InstallProgressData()
20 : percent_downloaded(0), is_ephemeral(false) {
21 }
22
23 InstallTracker::InstallProgressData::InstallProgressData(
24 const std::string& extension_id)
25 : extension_id(extension_id), percent_downloaded(0), is_ephemeral(false) {
26 }
27
18 InstallTracker::InstallTracker(Profile* profile, 28 InstallTracker::InstallTracker(Profile* profile,
19 extensions::ExtensionPrefs* prefs) { 29 extensions::ExtensionPrefs* prefs)
30 : extension_registry_observer_(this) {
20 registrar_.Add(this, 31 registrar_.Add(this,
21 chrome::NOTIFICATION_EXTENSION_UPDATE_DISABLED, 32 chrome::NOTIFICATION_EXTENSION_UPDATE_DISABLED,
22 content::Source<Profile>(profile)); 33 content::Source<Profile>(profile));
23 AppSorting* sorting = prefs->app_sorting();
24 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LAUNCHER_REORDERED,
25 content::Source<AppSorting>(sorting));
26 registrar_.Add(this, chrome::NOTIFICATION_APP_INSTALLED_TO_APPLIST, 34 registrar_.Add(this, chrome::NOTIFICATION_APP_INSTALLED_TO_APPLIST,
27 content::Source<Profile>(profile)); 35 content::Source<Profile>(profile));
36 extension_registry_observer_.Add(ExtensionRegistry::Get(profile));
28 37
29 pref_change_registrar_.Init(prefs->pref_service()); 38 // Prefs may be null in tests.
30 pref_change_registrar_.Add(pref_names::kExtensions, 39 if (prefs) {
31 base::Bind(&InstallTracker::OnAppsReordered, 40 AppSorting* sorting = prefs->app_sorting();
32 base::Unretained(this))); 41 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LAUNCHER_REORDERED,
42 content::Source<AppSorting>(sorting));
43 pref_change_registrar_.Init(prefs->pref_service());
44 pref_change_registrar_.Add(pref_names::kExtensions,
45 base::Bind(&InstallTracker::OnAppsReordered,
46 base::Unretained(this)));
47 }
33 } 48 }
34 49
35 InstallTracker::~InstallTracker() { 50 InstallTracker::~InstallTracker() {
36 } 51 }
37 52
38 // static 53 // static
39 InstallTracker* InstallTracker::Get(content::BrowserContext* context) { 54 InstallTracker* InstallTracker::Get(content::BrowserContext* context) {
40 return InstallTrackerFactory::GetForProfile( 55 return InstallTrackerFactory::GetForProfile(
41 Profile::FromBrowserContext(context)); 56 Profile::FromBrowserContext(context));
42 } 57 }
43 58
44 void InstallTracker::AddObserver(InstallObserver* observer) { 59 void InstallTracker::AddObserver(InstallObserver* observer) {
45 observers_.AddObserver(observer); 60 observers_.AddObserver(observer);
46 } 61 }
47 62
48 void InstallTracker::RemoveObserver(InstallObserver* observer) { 63 void InstallTracker::RemoveObserver(InstallObserver* observer) {
49 observers_.RemoveObserver(observer); 64 observers_.RemoveObserver(observer);
50 } 65 }
51 66
67 const InstallTracker::InstallProgressData* InstallTracker::GetActiveInstall(
68 const std::string& extension_id) const {
69 ActiveInstallsMap::const_iterator install_data =
70 active_installs_.find(extension_id);
71 if (install_data == active_installs_.end())
72 return NULL;
73 else
74 return &(install_data->second);
75 }
76
77 void InstallTracker::AddActiveInstall(const InstallProgressData& install_data) {
78 DCHECK(!install_data.extension_id.empty());
79 DCHECK(active_installs_.find(install_data.extension_id) ==
80 active_installs_.end());
81 active_installs_.insert(
82 std::make_pair(install_data.extension_id, install_data));
83 }
84
85 void InstallTracker::RemoveActiveInstall(const std::string& extension_id) {
86 active_installs_.erase(extension_id);
87 }
88
52 void InstallTracker::OnBeginExtensionInstall( 89 void InstallTracker::OnBeginExtensionInstall(
53 const InstallObserver::ExtensionInstallParams& params) { 90 const InstallObserver::ExtensionInstallParams& params) {
91 ActiveInstallsMap::iterator install_data =
92 active_installs_.find(params.extension_id);
93 if (install_data == active_installs_.end()) {
94 InstallProgressData install_data(params.extension_id);
95 install_data.is_ephemeral = params.is_ephemeral;
96 active_installs_.insert(std::make_pair(params.extension_id, install_data));
97 }
98
54 FOR_EACH_OBSERVER(InstallObserver, observers_, 99 FOR_EACH_OBSERVER(InstallObserver, observers_,
55 OnBeginExtensionInstall(params)); 100 OnBeginExtensionInstall(params));
56 } 101 }
57 102
58 void InstallTracker::OnBeginExtensionDownload(const std::string& extension_id) { 103 void InstallTracker::OnBeginExtensionDownload(const std::string& extension_id) {
59 FOR_EACH_OBSERVER( 104 FOR_EACH_OBSERVER(
60 InstallObserver, observers_, OnBeginExtensionDownload(extension_id)); 105 InstallObserver, observers_, OnBeginExtensionDownload(extension_id));
61 } 106 }
62 107
63 void InstallTracker::OnDownloadProgress(const std::string& extension_id, 108 void InstallTracker::OnDownloadProgress(const std::string& extension_id,
64 int percent_downloaded) { 109 int percent_downloaded) {
110 ActiveInstallsMap::iterator install_data =
111 active_installs_.find(extension_id);
112 if (install_data != active_installs_.end()) {
113 install_data->second.percent_downloaded = percent_downloaded;
114 } else {
115 NOTREACHED();
116 }
117
65 FOR_EACH_OBSERVER(InstallObserver, observers_, 118 FOR_EACH_OBSERVER(InstallObserver, observers_,
66 OnDownloadProgress(extension_id, percent_downloaded)); 119 OnDownloadProgress(extension_id, percent_downloaded));
67 } 120 }
68 121
69 void InstallTracker::OnBeginCrxInstall(const std::string& extension_id) { 122 void InstallTracker::OnBeginCrxInstall(const std::string& extension_id) {
70 FOR_EACH_OBSERVER( 123 FOR_EACH_OBSERVER(
71 InstallObserver, observers_, OnBeginCrxInstall(extension_id)); 124 InstallObserver, observers_, OnBeginCrxInstall(extension_id));
72 } 125 }
73 126
74 void InstallTracker::OnFinishCrxInstall(const std::string& extension_id, 127 void InstallTracker::OnFinishCrxInstall(const std::string& extension_id,
75 bool success) { 128 bool success) {
76 FOR_EACH_OBSERVER( 129 FOR_EACH_OBSERVER(
77 InstallObserver, observers_, OnFinishCrxInstall(extension_id, success)); 130 InstallObserver, observers_, OnFinishCrxInstall(extension_id, success));
78 } 131 }
79 132
80 void InstallTracker::OnInstallFailure( 133 void InstallTracker::OnInstallFailure(
81 const std::string& extension_id) { 134 const std::string& extension_id) {
135 RemoveActiveInstall(extension_id);
82 FOR_EACH_OBSERVER(InstallObserver, observers_, 136 FOR_EACH_OBSERVER(InstallObserver, observers_,
83 OnInstallFailure(extension_id)); 137 OnInstallFailure(extension_id));
84 } 138 }
85 139
86 void InstallTracker::Shutdown() { 140 void InstallTracker::Shutdown() {
87 FOR_EACH_OBSERVER(InstallObserver, observers_, OnShutdown()); 141 FOR_EACH_OBSERVER(InstallObserver, observers_, OnShutdown());
88 } 142 }
89 143
90 void InstallTracker::Observe(int type, 144 void InstallTracker::Observe(int type,
91 const content::NotificationSource& source, 145 const content::NotificationSource& source,
(...skipping 15 matching lines...) Expand all
107 *content::Details<const std::string>(details).ptr()); 161 *content::Details<const std::string>(details).ptr());
108 FOR_EACH_OBSERVER(InstallObserver, observers_, 162 FOR_EACH_OBSERVER(InstallObserver, observers_,
109 OnAppInstalledToAppList(extension_id)); 163 OnAppInstalledToAppList(extension_id));
110 break; 164 break;
111 } 165 }
112 default: 166 default:
113 NOTREACHED(); 167 NOTREACHED();
114 } 168 }
115 } 169 }
116 170
171 void InstallTracker::OnExtensionInstalled(
172 content::BrowserContext* browser_context,
173 const Extension* extension) {
174 RemoveActiveInstall(extension->id());
175 }
176
117 void InstallTracker::OnAppsReordered() { 177 void InstallTracker::OnAppsReordered() {
118 FOR_EACH_OBSERVER(InstallObserver, observers_, OnAppsReordered()); 178 FOR_EACH_OBSERVER(InstallObserver, observers_, OnAppsReordered());
119 } 179 }
120 180
121 } // namespace extensions 181 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698