OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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/external_install_manager.h" |
| 6 |
| 7 #include <string> |
| 8 |
| 9 #include "base/logging.h" |
| 10 #include "chrome/browser/chrome_notification_types.h" |
| 11 #include "chrome/browser/extensions/extension_service.h" |
| 12 #include "chrome/browser/extensions/external_install_error.h" |
| 13 #include "chrome/browser/profiles/profile.h" |
| 14 #include "chrome/common/extensions/manifest_url_handler.h" |
| 15 #include "content/public/browser/notification_details.h" |
| 16 #include "content/public/browser/notification_source.h" |
| 17 #include "extensions/browser/extension_registry.h" |
| 18 #include "extensions/browser/extension_system.h" |
| 19 #include "extensions/common/extension.h" |
| 20 |
| 21 namespace extensions { |
| 22 |
| 23 ExternalInstallManager::ExternalInstallManager( |
| 24 content::BrowserContext* browser_context) |
| 25 : browser_context_(browser_context), |
| 26 extension_registry_observer_(this) { |
| 27 DCHECK(browser_context_); |
| 28 extension_registry_observer_.Add(ExtensionRegistry::Get(browser_context_)); |
| 29 registrar_.Add(this, |
| 30 chrome::NOTIFICATION_EXTENSION_REMOVED, |
| 31 content::Source<Profile>( |
| 32 Profile::FromBrowserContext(browser_context_))); |
| 33 } |
| 34 |
| 35 ExternalInstallManager::~ExternalInstallManager() { |
| 36 } |
| 37 |
| 38 void ExternalInstallManager::AddExternalInstallError(const Extension* extension, |
| 39 bool is_new_profile) { |
| 40 if (HasExternalInstallError()) |
| 41 return; // Only have one external install error at a time. |
| 42 |
| 43 if (ManifestURL::UpdatesFromGallery(extension) && !is_new_profile) { |
| 44 error_.reset(new ExternalInstallError(browser_context_, |
| 45 extension->id(), |
| 46 ExternalInstallError::BUBBLE_ALERT, |
| 47 this)); |
| 48 } else { |
| 49 error_.reset(new ExternalInstallError(browser_context_, |
| 50 extension->id(), |
| 51 ExternalInstallError::MENU_ALERT, |
| 52 this)); |
| 53 } |
| 54 } |
| 55 |
| 56 void ExternalInstallManager::RemoveExternalInstallError() { |
| 57 error_.reset(); |
| 58 ExtensionSystem::Get(browser_context_) |
| 59 ->extension_service() |
| 60 ->UpdateExternalExtensionAlert(); |
| 61 } |
| 62 |
| 63 bool ExternalInstallManager::HasExternalInstallError() const { |
| 64 return error_.get() != NULL; |
| 65 } |
| 66 |
| 67 bool ExternalInstallManager::HasExternalInstallBubble() const { |
| 68 return error_.get() && |
| 69 error_->alert_type() == ExternalInstallError::BUBBLE_ALERT; |
| 70 } |
| 71 |
| 72 void ExternalInstallManager::OnExtensionLoaded( |
| 73 content::BrowserContext* browser_context, |
| 74 const Extension* extension) { |
| 75 // The error is invalidated if the extension has been loaded or removed. |
| 76 if (error_.get() && extension->id() == error_->extension_id()) { |
| 77 // We treat loading as acknowledgement (since the user consciously chose to |
| 78 // re-enable the extension). |
| 79 error_->AcknowledgeExtension(); |
| 80 RemoveExternalInstallError(); |
| 81 } |
| 82 } |
| 83 |
| 84 void ExternalInstallManager::Observe( |
| 85 int type, |
| 86 const content::NotificationSource& source, |
| 87 const content::NotificationDetails& details) { |
| 88 DCHECK_EQ(chrome::NOTIFICATION_EXTENSION_REMOVED, type); |
| 89 // The error is invalidated if the extension has been loaded or removed. |
| 90 // It's a shame we have to use the notification system (instead of the |
| 91 // registry observer) for this, but the ExtensionUnloaded notification is |
| 92 // not sent out if the extension is disabled (which it is here). |
| 93 if (error_.get() && |
| 94 content::Details<const Extension>(details).ptr()->id() == |
| 95 error_->extension_id()) { |
| 96 RemoveExternalInstallError(); |
| 97 } |
| 98 } |
| 99 |
| 100 } // namespace extensions |
OLD | NEW |