blob: b96296156075d262fd2e969f9aa9320a40756550 [file] [log] [blame]
[email protected]0f34d9082012-10-08 19:16:441// 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]0f34d9082012-10-08 19:16:447#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
16namespace extensions {
17
[email protected]5ed5ec52012-10-12 21:28:3018ExtensionActionManager::ExtensionActionManager(Profile* profile) {
[email protected]0f34d9082012-10-08 19:16:4419 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
27ExtensionActionManager::~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]5ed5ec52012-10-12 21:28:3032ExtensionActionManager* ExtensionActionManager::Get(Profile* profile) {
33 return ExtensionSystem::Get(profile)->extension_action_manager();
[email protected]0f34d9082012-10-08 19:16:4434}
35
36void 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]5ed5ec52012-10-12 21:28:3052 if (switch_utils::AreScriptBadgesEnabled())
[email protected]0f34d9082012-10-08 19:16:4453 browser_actions_[extension->id()] = page_action;
[email protected]5ed5ec52012-10-12 21:28:3054 else
[email protected]0f34d9082012-10-08 19:16:4455 page_actions_[extension->id()] = page_action;
[email protected]0f34d9082012-10-08 19:16:4456 }
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]0f34d9082012-10-08 19:16:4464 }
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]0f34d9082012-10-08 19:16:4472 }
73 break;
74 }
75 case chrome::NOTIFICATION_EXTENSION_UNLOADED: {
76 const Extension* extension =
77 content::Details<UnloadedExtensionInfo>(details)->extension;
[email protected]5ed5ec52012-10-12 21:28:3078 page_actions_.erase(extension->id());
79 browser_actions_.erase(extension->id());
80 script_badges_.erase(extension->id());
[email protected]0f34d9082012-10-08 19:16:4481 break;
82 }
83 }
84}
85
[email protected]5ed5ec52012-10-12 21:28:3086namespace {
87
88template<typename MapT>
89typename 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
99ExtensionAction* ExtensionActionManager::GetPageAction(
100 const extensions::Extension& extension) const {
101 return GetOrDefault(page_actions_, extension.id()).get();
102}
103
104ExtensionAction* ExtensionActionManager::GetBrowserAction(
105 const extensions::Extension& extension) const {
106 return GetOrDefault(browser_actions_, extension.id()).get();
107}
108
109ExtensionAction* ExtensionActionManager::GetScriptBadge(
110 const extensions::Extension& extension) const {
111 return GetOrDefault(script_badges_, extension.id()).get();
112}
113
[email protected]0f34d9082012-10-08 19:16:44114}