Commit 1d7f248f authored by limasdf@gmail.com's avatar limasdf@gmail.com

Add TriggerOnUninstalled to ExtensionRegistry.

And change ExtensionToolbarModel to use it instead of NOTIFICATION_EXTENSION_UNINSTALLED.

R=kalman@chromium.org
BUG=354459
TEST=unit_test

Review URL: https://codereview.chromium.org/289073008

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@272649 0039d316-1c4b-4281-b951-d872f2087c98
parent c093918c
...@@ -768,6 +768,7 @@ bool ExtensionService::UninstallExtension( ...@@ -768,6 +768,7 @@ bool ExtensionService::UninstallExtension(
chrome::NOTIFICATION_EXTENSION_UNINSTALLED, chrome::NOTIFICATION_EXTENSION_UNINSTALLED,
content::Source<Profile>(profile_), content::Source<Profile>(profile_),
content::Details<const Extension>(extension.get())); content::Details<const Extension>(extension.get()));
ExtensionRegistry::Get(profile_)->TriggerOnUninstalled(extension.get());
if (extension_sync_service_) { if (extension_sync_service_) {
extension_sync_service_->ProcessSyncUninstallExtension(extension->id(), extension_sync_service_->ProcessSyncUninstallExtension(extension->id(),
...@@ -1426,6 +1427,7 @@ void ExtensionService::RemoveComponentExtension( ...@@ -1426,6 +1427,7 @@ void ExtensionService::RemoveComponentExtension(
chrome::NOTIFICATION_EXTENSION_UNINSTALLED, chrome::NOTIFICATION_EXTENSION_UNINSTALLED,
content::Source<Profile>(profile_), content::Source<Profile>(profile_),
content::Details<const Extension>(extension.get())); content::Details<const Extension>(extension.get()));
ExtensionRegistry::Get(profile_)->TriggerOnUninstalled(extension.get());
} }
} }
......
...@@ -191,18 +191,27 @@ void ExtensionToolbarModel::OnExtensionUnloaded( ...@@ -191,18 +191,27 @@ void ExtensionToolbarModel::OnExtensionUnloaded(
RemoveExtension(extension); RemoveExtension(extension);
} }
void ExtensionToolbarModel::OnExtensionUninstalled(
content::BrowserContext* browser_context,
const Extension* extension) {
// Remove the extension id from the ordered list, if it exists (the extension
// might not be represented in the list because it might not have an icon).
ExtensionIdList::iterator pos =
std::find(last_known_positions_.begin(),
last_known_positions_.end(), extension->id());
if (pos != last_known_positions_.end()) {
last_known_positions_.erase(pos);
UpdatePrefs();
}
}
void ExtensionToolbarModel::Observe( void ExtensionToolbarModel::Observe(
int type, int type,
const content::NotificationSource& source, const content::NotificationSource& source,
const content::NotificationDetails& details) { const content::NotificationDetails& details) {
switch (type) { DCHECK_EQ(chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_VISIBILITY_CHANGED,
case chrome::NOTIFICATION_EXTENSION_UNINSTALLED: { type);
const Extension* extension =
content::Details<const Extension>(details).ptr();
UninstalledExtension(extension);
break;
}
case chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_VISIBILITY_CHANGED: {
const Extension* extension = const Extension* extension =
ExtensionRegistry::Get(profile_)->GetExtensionById( ExtensionRegistry::Get(profile_)->GetExtensionById(
*content::Details<const std::string>(details).ptr(), *content::Details<const std::string>(details).ptr(),
...@@ -213,11 +222,6 @@ void ExtensionToolbarModel::Observe( ...@@ -213,11 +222,6 @@ void ExtensionToolbarModel::Observe(
} else { } else {
RemoveExtension(extension); RemoveExtension(extension);
} }
break;
}
default:
NOTREACHED() << "Received unexpected notification";
}
} }
void ExtensionToolbarModel::OnReady() { void ExtensionToolbarModel::OnReady() {
...@@ -227,9 +231,6 @@ void ExtensionToolbarModel::OnReady() { ...@@ -227,9 +231,6 @@ void ExtensionToolbarModel::OnReady() {
// changes so that the toolbar buttons can be shown in their stable ordering // changes so that the toolbar buttons can be shown in their stable ordering
// taken from prefs. // taken from prefs.
extension_registry_observer_.Add(registry); extension_registry_observer_.Add(registry);
registrar_.Add(this,
chrome::NOTIFICATION_EXTENSION_UNINSTALLED,
content::Source<Profile>(profile_));
registrar_.Add( registrar_.Add(
this, this,
chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_VISIBILITY_CHANGED, chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_VISIBILITY_CHANGED,
...@@ -325,19 +326,6 @@ void ExtensionToolbarModel::RemoveExtension(const Extension* extension) { ...@@ -325,19 +326,6 @@ void ExtensionToolbarModel::RemoveExtension(const Extension* extension) {
UpdatePrefs(); UpdatePrefs();
} }
void ExtensionToolbarModel::UninstalledExtension(const Extension* extension) {
// Remove the extension id from the ordered list, if it exists (the extension
// might not be represented in the list because it might not have an icon).
ExtensionIdList::iterator pos =
std::find(last_known_positions_.begin(),
last_known_positions_.end(), extension->id());
if (pos != last_known_positions_.end()) {
last_known_positions_.erase(pos);
UpdatePrefs();
}
}
// Combine the currently enabled extensions that have browser actions (which // Combine the currently enabled extensions that have browser actions (which
// we get from the ExtensionRegistry) with the ordering we get from the // we get from the ExtensionRegistry) with the ordering we get from the
// pref service. For robustness we use a somewhat inefficient process: // pref service. For robustness we use a somewhat inefficient process:
...@@ -589,6 +577,6 @@ void ExtensionToolbarModel::StopHighlighting() { ...@@ -589,6 +577,6 @@ void ExtensionToolbarModel::StopHighlighting() {
} }
FOR_EACH_OBSERVER(Observer, observers_, HighlightModeChanged(false)); FOR_EACH_OBSERVER(Observer, observers_, HighlightModeChanged(false));
} }
}; }
} // namespace extensions } // namespace extensions
...@@ -151,6 +151,8 @@ class ExtensionToolbarModel : public content::NotificationObserver, ...@@ -151,6 +151,8 @@ class ExtensionToolbarModel : public content::NotificationObserver,
content::BrowserContext* browser_context, content::BrowserContext* browser_context,
const Extension* extension, const Extension* extension,
UnloadedExtensionInfo::Reason reason) OVERRIDE; UnloadedExtensionInfo::Reason reason) OVERRIDE;
virtual void OnExtensionUninstalled(content::BrowserContext* browser_context,
const Extension* extension) OVERRIDE;
// To be called after the extension service is ready; gets loaded extensions // To be called after the extension service is ready; gets loaded extensions
// from the extension service and their saved order from the pref service // from the extension service and their saved order from the pref service
...@@ -171,7 +173,6 @@ class ExtensionToolbarModel : public content::NotificationObserver, ...@@ -171,7 +173,6 @@ class ExtensionToolbarModel : public content::NotificationObserver,
void AddExtension(const Extension* extension); void AddExtension(const Extension* extension);
void RemoveExtension(const Extension* extension); void RemoveExtension(const Extension* extension);
void UninstalledExtension(const Extension* extension);
// The Profile this toolbar model is for. // The Profile this toolbar model is for.
Profile* profile_; Profile* profile_;
......
...@@ -65,6 +65,13 @@ void ExtensionRegistry::TriggerOnWillBeInstalled(const Extension* extension, ...@@ -65,6 +65,13 @@ void ExtensionRegistry::TriggerOnWillBeInstalled(const Extension* extension,
browser_context_, extension, is_update, old_name)); browser_context_, extension, is_update, old_name));
} }
void ExtensionRegistry::TriggerOnUninstalled(const Extension* extension) {
DCHECK(!GenerateInstalledExtensionsSet()->Contains(extension->id()));
FOR_EACH_OBSERVER(ExtensionRegistryObserver,
observers_,
OnExtensionUninstalled(browser_context_, extension));
}
const Extension* ExtensionRegistry::GetExtensionById(const std::string& id, const Extension* ExtensionRegistry::GetExtensionById(const std::string& id,
int include_mask) const { int include_mask) const {
std::string lowercase_id = StringToLowerASCII(id); std::string lowercase_id = StringToLowerASCII(id);
......
...@@ -82,6 +82,10 @@ class ExtensionRegistry : public KeyedService { ...@@ -82,6 +82,10 @@ class ExtensionRegistry : public KeyedService {
bool is_update, bool is_update,
const std::string& old_name); const std::string& old_name);
// Invokes the observer method OnExtensionUninstalled(). The extension must
// not be any installed extension with |extension|'s ID.
void TriggerOnUninstalled(const Extension* extension);
// Find an extension by ID using |include_mask| to pick the sets to search: // Find an extension by ID using |include_mask| to pick the sets to search:
// * enabled_extensions() --> ExtensionRegistry::ENABLED // * enabled_extensions() --> ExtensionRegistry::ENABLED
// * disabled_extensions() --> ExtensionRegistry::DISABLED // * disabled_extensions() --> ExtensionRegistry::DISABLED
......
...@@ -48,6 +48,11 @@ class ExtensionRegistryObserver { ...@@ -48,6 +48,11 @@ class ExtensionRegistryObserver {
const Extension* extension, const Extension* extension,
bool is_update, bool is_update,
const std::string& old_name) {} const std::string& old_name) {}
// Called after an extension is uninstalled. The extension no longer exsit in
// any of the ExtensionRegistry sets (enabled, disabled, etc.).
virtual void OnExtensionUninstalled(content::BrowserContext* browser_context,
const Extension* extension) {}
}; };
} // namespace extensions } // namespace extensions
......
...@@ -39,11 +39,13 @@ class TestObserver : public ExtensionRegistryObserver { ...@@ -39,11 +39,13 @@ class TestObserver : public ExtensionRegistryObserver {
loaded_.clear(); loaded_.clear();
unloaded_.clear(); unloaded_.clear();
installed_.clear(); installed_.clear();
uninstalled_.clear();
} }
const ExtensionList& loaded() { return loaded_; } const ExtensionList& loaded() { return loaded_; }
const ExtensionList& unloaded() { return unloaded_; } const ExtensionList& unloaded() { return unloaded_; }
const ExtensionList& installed() { return installed_; } const ExtensionList& installed() { return installed_; }
const ExtensionList& uninstalled() { return uninstalled_; }
private: private:
virtual void OnExtensionLoaded(content::BrowserContext* browser_context, virtual void OnExtensionLoaded(content::BrowserContext* browser_context,
...@@ -66,9 +68,15 @@ class TestObserver : public ExtensionRegistryObserver { ...@@ -66,9 +68,15 @@ class TestObserver : public ExtensionRegistryObserver {
installed_.push_back(extension); installed_.push_back(extension);
} }
virtual void OnExtensionUninstalled(content::BrowserContext* browser_context,
const Extension* extension) OVERRIDE {
uninstalled_.push_back(extension);
}
ExtensionList loaded_; ExtensionList loaded_;
ExtensionList unloaded_; ExtensionList unloaded_;
ExtensionList installed_; ExtensionList installed_;
ExtensionList uninstalled_;
}; };
TEST_F(ExtensionRegistryTest, FillAndClearRegistry) { TEST_F(ExtensionRegistryTest, FillAndClearRegistry) {
...@@ -251,6 +259,10 @@ TEST_F(ExtensionRegistryTest, Observer) { ...@@ -251,6 +259,10 @@ TEST_F(ExtensionRegistryTest, Observer) {
EXPECT_TRUE(HasSingleExtension(observer.unloaded(), extension.get())); EXPECT_TRUE(HasSingleExtension(observer.unloaded(), extension.get()));
observer.Reset(); observer.Reset();
registry.TriggerOnUninstalled(extension);
EXPECT_TRUE(observer.installed().empty());
EXPECT_TRUE(HasSingleExtension(observer.uninstalled(), extension.get()));
registry.RemoveObserver(&observer); registry.RemoveObserver(&observer);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment