1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/extensions/extension_action_manager.h"
#include "chrome/browser/extensions/extension_action.h"
#include "chrome/browser/extensions/extension_system.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_dependency_manager.h"
#include "chrome/browser/profiles/profile_keyed_service_factory.h"
#include "chrome/common/chrome_notification_types.h"
#include "chrome/common/extensions/extension.h"
#include "chrome/common/extensions/feature_switch.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/notification_source.h"
namespace extensions {
namespace {
// ProfileKeyedServiceFactory for ExtensionActionManager.
class ExtensionActionManagerFactory : public ProfileKeyedServiceFactory {
public:
// ProfileKeyedServiceFactory implementation:
static ExtensionActionManager* GetForProfile(Profile* profile) {
return static_cast<ExtensionActionManager*>(
GetInstance()->GetServiceForProfile(profile, true));
}
static ExtensionActionManagerFactory* GetInstance();
private:
friend struct DefaultSingletonTraits<ExtensionActionManagerFactory>;
ExtensionActionManagerFactory()
: ProfileKeyedServiceFactory("ExtensionActionManager",
ProfileDependencyManager::GetInstance()) {
}
virtual ProfileKeyedService* BuildServiceInstanceFor(
Profile* profile) const OVERRIDE {
return new ExtensionActionManager(profile);
}
virtual bool ServiceRedirectedInIncognito() const OVERRIDE {
return true;
}
};
ExtensionActionManagerFactory*
ExtensionActionManagerFactory::GetInstance() {
return Singleton<ExtensionActionManagerFactory>::get();
}
} // namespace
ExtensionActionManager::ExtensionActionManager(Profile* profile) {
CHECK_EQ(profile, profile->GetOriginalProfile())
<< "Don't instantiate this with an incognito profile.";
registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED,
content::Source<Profile>(profile));
}
ExtensionActionManager::~ExtensionActionManager() {
// Don't assert that the ExtensionAction maps are empty because Extensions are
// sometimes (only in tests?) not unloaded before the Profile is destroyed.
}
ExtensionActionManager* ExtensionActionManager::Get(Profile* profile) {
return ExtensionActionManagerFactory::GetForProfile(profile);
}
void ExtensionActionManager::Observe(
int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) {
switch (type) {
case chrome::NOTIFICATION_EXTENSION_UNLOADED: {
const Extension* extension =
content::Details<UnloadedExtensionInfo>(details)->extension;
page_actions_.erase(extension->id());
browser_actions_.erase(extension->id());
script_badges_.erase(extension->id());
break;
}
}
}
namespace {
// Returns map[extension_id] if that entry exists. Otherwise, if
// action_info!=NULL, creates an ExtensionAction from it, fills in the map, and
// returns that. Otherwise (action_info==NULL), returns NULL.
ExtensionAction* GetOrCreateOrNull(
std::map<std::string, linked_ptr<ExtensionAction> >* map,
const std::string& extension_id,
Extension::ActionInfo::Type action_type,
const Extension::ActionInfo* action_info) {
std::map<std::string, linked_ptr<ExtensionAction> >::const_iterator it =
map->find(extension_id);
if (it != map->end())
return it->second.get();
if (!action_info)
return NULL;
linked_ptr<ExtensionAction> action(new ExtensionAction(
extension_id, action_type, *action_info));
(*map)[extension_id] = action;
return action.get();
}
}
ExtensionAction* ExtensionActionManager::GetPageAction(
const extensions::Extension& extension) const {
// The action box changes the meaning of the page action area, so we
// need to convert page actions into browser actions.
if (FeatureSwitch::script_badges()->IsEnabled())
return NULL;
return GetOrCreateOrNull(&page_actions_, extension.id(),
Extension::ActionInfo::TYPE_PAGE,
extension.page_action_info());
}
ExtensionAction* ExtensionActionManager::GetBrowserAction(
const extensions::Extension& extension) const {
const Extension::ActionInfo* action_info = extension.browser_action_info();
Extension::ActionInfo::Type action_type = Extension::ActionInfo::TYPE_BROWSER;
if (FeatureSwitch::script_badges()->IsEnabled() &&
extension.page_action_info()) {
// The action box changes the meaning of the page action area, so we
// need to convert page actions into browser actions.
action_info = extension.page_action_info();
action_type = Extension::ActionInfo::TYPE_PAGE;
}
return GetOrCreateOrNull(&browser_actions_, extension.id(),
action_type, action_info);
}
ExtensionAction* ExtensionActionManager::GetScriptBadge(
const extensions::Extension& extension) const {
return GetOrCreateOrNull(&script_badges_, extension.id(),
Extension::ActionInfo::TYPE_SCRIPT_BADGE,
extension.script_badge_info());
}
}
|