[email protected] | 0f34d908 | 2012-10-08 19:16:44 | [diff] [blame] | 1 | // Copyright (c) 2012 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/extension_action_manager.h" |
| 6 | |
[email protected] | 0f34d908 | 2012-10-08 19:16:44 | [diff] [blame] | 7 | #include "chrome/browser/extensions/extension_system.h" |
| 8 | #include "chrome/browser/profiles/profile.h" |
| 9 | #include "chrome/common/chrome_notification_types.h" |
| 10 | #include "chrome/common/extensions/extension.h" |
| 11 | #include "chrome/common/extensions/extension_action.h" |
| 12 | #include "chrome/common/extensions/extension_switch_utils.h" |
| 13 | #include "content/public/browser/notification_service.h" |
| 14 | #include "content/public/browser/notification_source.h" |
| 15 | |
| 16 | namespace extensions { |
| 17 | |
[email protected] | 5ed5ec5 | 2012-10-12 21:28:30 | [diff] [blame^] | 18 | ExtensionActionManager::ExtensionActionManager(Profile* profile) { |
[email protected] | 0f34d908 | 2012-10-08 19:16:44 | [diff] [blame] | 19 | CHECK_EQ(profile, profile->GetOriginalProfile()) |
| 20 | << "Don't instantiate this with an incognito profile."; |
| 21 | registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED, |
| 22 | content::Source<Profile>(profile)); |
| 23 | registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, |
| 24 | content::Source<Profile>(profile)); |
| 25 | } |
| 26 | |
| 27 | ExtensionActionManager::~ExtensionActionManager() { |
| 28 | // Don't assert that the ExtensionAction maps are empty because Extensions are |
| 29 | // sometimes (only in tests?) not unloaded before the Profile is destroyed. |
| 30 | } |
| 31 | |
[email protected] | 5ed5ec5 | 2012-10-12 21:28:30 | [diff] [blame^] | 32 | ExtensionActionManager* ExtensionActionManager::Get(Profile* profile) { |
| 33 | return ExtensionSystem::Get(profile)->extension_action_manager(); |
[email protected] | 0f34d908 | 2012-10-08 19:16:44 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | void ExtensionActionManager::Observe( |
| 37 | int type, |
| 38 | const content::NotificationSource& source, |
| 39 | const content::NotificationDetails& details) { |
| 40 | switch (type) { |
| 41 | case chrome::NOTIFICATION_EXTENSION_LOADED: { |
| 42 | const Extension* extension = |
| 43 | content::Details<const Extension>(details).ptr(); |
| 44 | if (extension->page_action_info() && |
| 45 | !ContainsKey(page_actions_, extension->id())) { |
| 46 | linked_ptr<ExtensionAction> page_action( |
| 47 | new ExtensionAction(extension->id(), |
| 48 | Extension::ActionInfo::TYPE_PAGE, |
| 49 | *extension->page_action_info())); |
| 50 | // The action box changes the meaning of the page action area, so we |
| 51 | // need to convert page actions into browser actions. |
[email protected] | 5ed5ec5 | 2012-10-12 21:28:30 | [diff] [blame^] | 52 | if (switch_utils::AreScriptBadgesEnabled()) |
[email protected] | 0f34d908 | 2012-10-08 19:16:44 | [diff] [blame] | 53 | browser_actions_[extension->id()] = page_action; |
[email protected] | 5ed5ec5 | 2012-10-12 21:28:30 | [diff] [blame^] | 54 | else |
[email protected] | 0f34d908 | 2012-10-08 19:16:44 | [diff] [blame] | 55 | page_actions_[extension->id()] = page_action; |
[email protected] | 0f34d908 | 2012-10-08 19:16:44 | [diff] [blame] | 56 | } |
| 57 | if (extension->browser_action_info() && |
| 58 | !ContainsKey(browser_actions_, extension->id())) { |
| 59 | linked_ptr<ExtensionAction> browser_action( |
| 60 | new ExtensionAction(extension->id(), |
| 61 | Extension::ActionInfo::TYPE_BROWSER, |
| 62 | *extension->browser_action_info())); |
| 63 | browser_actions_[extension->id()] = browser_action; |
[email protected] | 0f34d908 | 2012-10-08 19:16:44 | [diff] [blame] | 64 | } |
| 65 | DCHECK(extension->script_badge_info()); |
| 66 | if (!ContainsKey(script_badges_, extension->id())) { |
| 67 | linked_ptr<ExtensionAction> script_badge( |
| 68 | new ExtensionAction(extension->id(), |
| 69 | Extension::ActionInfo::TYPE_SCRIPT_BADGE, |
| 70 | *extension->script_badge_info())); |
| 71 | script_badges_[extension->id()] = script_badge; |
[email protected] | 0f34d908 | 2012-10-08 19:16:44 | [diff] [blame] | 72 | } |
| 73 | break; |
| 74 | } |
| 75 | case chrome::NOTIFICATION_EXTENSION_UNLOADED: { |
| 76 | const Extension* extension = |
| 77 | content::Details<UnloadedExtensionInfo>(details)->extension; |
[email protected] | 5ed5ec5 | 2012-10-12 21:28:30 | [diff] [blame^] | 78 | page_actions_.erase(extension->id()); |
| 79 | browser_actions_.erase(extension->id()); |
| 80 | script_badges_.erase(extension->id()); |
[email protected] | 0f34d908 | 2012-10-08 19:16:44 | [diff] [blame] | 81 | break; |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
[email protected] | 5ed5ec5 | 2012-10-12 21:28:30 | [diff] [blame^] | 86 | namespace { |
| 87 | |
| 88 | template<typename MapT> |
| 89 | typename MapT::mapped_type GetOrDefault(const MapT& map, |
| 90 | const typename MapT::key_type& key) { |
| 91 | typename MapT::const_iterator it = map.find(key); |
| 92 | if (it == map.end()) |
| 93 | return typename MapT::mapped_type(); |
| 94 | return it->second; |
| 95 | } |
| 96 | |
| 97 | } |
| 98 | |
| 99 | ExtensionAction* ExtensionActionManager::GetPageAction( |
| 100 | const extensions::Extension& extension) const { |
| 101 | return GetOrDefault(page_actions_, extension.id()).get(); |
| 102 | } |
| 103 | |
| 104 | ExtensionAction* ExtensionActionManager::GetBrowserAction( |
| 105 | const extensions::Extension& extension) const { |
| 106 | return GetOrDefault(browser_actions_, extension.id()).get(); |
| 107 | } |
| 108 | |
| 109 | ExtensionAction* ExtensionActionManager::GetScriptBadge( |
| 110 | const extensions::Extension& extension) const { |
| 111 | return GetOrDefault(script_badges_, extension.id()).get(); |
| 112 | } |
| 113 | |
[email protected] | 0f34d908 | 2012-10-08 19:16:44 | [diff] [blame] | 114 | } |