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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
|
// Copyright (c) 2009 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_process_manager.h"
#include "chrome/browser/browsing_instance.h"
#include "chrome/browser/extensions/extension_host.h"
#include "chrome/browser/extensions/extensions_service.h"
#include "chrome/browser/profile.h"
#include "chrome/browser/renderer_host/site_instance.h"
#include "chrome/browser/renderer_host/render_view_host.h"
#include "chrome/common/extensions/extension.h"
#include "chrome/common/notification_service.h"
#include "chrome/common/notification_type.h"
#include "chrome/common/render_messages.h"
static void CreateBackgroundHost(
ExtensionProcessManager* manager, Extension* extension) {
// Start the process for the master page, if it exists.
if (extension->background_url().is_valid())
manager->CreateBackgroundHost(extension, extension->background_url());
}
static void CreateBackgroundHosts(
ExtensionProcessManager* manager, const ExtensionList* extensions) {
for (ExtensionList::const_iterator extension = extensions->begin();
extension != extensions->end(); ++extension) {
CreateBackgroundHost(manager, *extension);
}
}
ExtensionProcessManager::ExtensionProcessManager(Profile* profile)
: browsing_instance_(new BrowsingInstance(profile)) {
registrar_.Add(this, NotificationType::EXTENSIONS_READY,
NotificationService::AllSources());
registrar_.Add(this, NotificationType::EXTENSION_LOADED,
NotificationService::AllSources());
registrar_.Add(this, NotificationType::EXTENSION_UNLOADED,
NotificationService::AllSources());
registrar_.Add(this, NotificationType::EXTENSION_HOST_DESTROYED,
Source<Profile>(profile));
registrar_.Add(this, NotificationType::RENDERER_PROCESS_TERMINATED,
NotificationService::AllSources());
registrar_.Add(this, NotificationType::RENDERER_PROCESS_CLOSED,
NotificationService::AllSources());
}
ExtensionProcessManager::~ExtensionProcessManager() {
// Copy all_hosts_ to avoid iterator invalidation issues.
ExtensionHostSet to_delete(background_hosts_.begin(),
background_hosts_.end());
ExtensionHostSet::iterator iter;
for (iter = to_delete.begin(); iter != to_delete.end(); ++iter)
delete *iter;
}
ExtensionHost* ExtensionProcessManager::CreateView(Extension* extension,
const GURL& url,
Browser* browser) {
DCHECK(extension);
DCHECK(browser);
ExtensionHost* host =
new ExtensionHost(extension, GetSiteInstanceForURL(url), url,
ViewType::EXTENSION_TOOLSTRIP);
host->CreateView(browser);
OnExtensionHostCreated(host, false);
return host;
}
ExtensionHost* ExtensionProcessManager::CreateView(const GURL& url,
Browser* browser) {
DCHECK(browser);
ExtensionsService* service =
browsing_instance_->profile()->GetExtensionsService();
if (service) {
Extension* extension = service->GetExtensionByURL(url);
if (extension)
return CreateView(extension, url, browser);
}
return NULL;
}
ExtensionHost* ExtensionProcessManager::CreateBackgroundHost(
Extension* extension, const GURL& url) {
ExtensionHost* host =
new ExtensionHost(extension, GetSiteInstanceForURL(url), url,
ViewType::EXTENSION_BACKGROUND_PAGE);
host->CreateRenderView(NULL); // create a RenderViewHost with no view
OnExtensionHostCreated(host, true);
return host;
}
void ExtensionProcessManager::RegisterExtensionProcess(
const std::string& extension_id, int process_id) {
ProcessIDMap::const_iterator it = process_ids_.find(extension_id);
if (it != process_ids_.end() && (*it).second == process_id)
return;
// Extension ids should get removed from the map before the process ids get
// reused from a dead renderer.
DCHECK(it == process_ids_.end());
process_ids_[extension_id] = process_id;
ExtensionsService* extension_service =
browsing_instance_->profile()->GetExtensionsService();
std::vector<std::string> page_action_ids;
Extension* extension = extension_service->GetExtensionById(extension_id);
for (PageActionMap::const_iterator i = extension->page_actions().begin();
i != extension->page_actions().end(); ++i) {
page_action_ids.push_back(i->first);
}
RenderProcessHost* rph = RenderProcessHost::FromID(process_id);
rph->Send(new ViewMsg_Extension_UpdatePageActions(extension_id,
page_action_ids));
}
void ExtensionProcessManager::UnregisterExtensionProcess(int process_id) {
ProcessIDMap::iterator it = process_ids_.begin();
while (it != process_ids_.end()) {
if (it->second == process_id)
process_ids_.erase(it++);
else
++it;
}
}
RenderProcessHost* ExtensionProcessManager::GetExtensionProcess(
const std::string& extension_id) {
ProcessIDMap::const_iterator it = process_ids_.find(extension_id);
if (it == process_ids_.end())
return NULL;
RenderProcessHost* rph = RenderProcessHost::FromID(it->second);
DCHECK(rph) << "We should have unregistered this host.";
return rph;
}
SiteInstance* ExtensionProcessManager::GetSiteInstanceForURL(const GURL& url) {
return browsing_instance_->GetSiteInstanceForURL(url);
}
void ExtensionProcessManager::Observe(NotificationType type,
const NotificationSource& source,
const NotificationDetails& details) {
switch (type.value) {
case NotificationType::EXTENSIONS_READY:
CreateBackgroundHosts(this,
Source<ExtensionsService>(source).ptr()->extensions());
break;
case NotificationType::EXTENSION_LOADED: {
ExtensionsService* service = Source<ExtensionsService>(source).ptr();
if (service->is_ready()) {
Extension* extension = Details<Extension>(details).ptr();
::CreateBackgroundHost(this, extension);
}
break;
}
case NotificationType::EXTENSION_UNLOADED: {
Extension* extension = Details<Extension>(details).ptr();
for (ExtensionHostSet::iterator iter = background_hosts_.begin();
iter != background_hosts_.end(); ++iter) {
ExtensionHost* host = *iter;
if (host->extension()->id() == extension->id()) {
delete host;
// |host| should deregister itself from our structures.
DCHECK(background_hosts_.find(host) == background_hosts_.end());
break;
}
}
break;
}
case NotificationType::EXTENSION_HOST_DESTROYED: {
ExtensionHost* host = Details<ExtensionHost>(details).ptr();
all_hosts_.erase(host);
background_hosts_.erase(host);
break;
}
case NotificationType::RENDERER_PROCESS_TERMINATED:
case NotificationType::RENDERER_PROCESS_CLOSED: {
RenderProcessHost* host = Source<RenderProcessHost>(source).ptr();
UnregisterExtensionProcess(host->id());
break;
}
default:
NOTREACHED();
}
}
void ExtensionProcessManager::OnExtensionHostCreated(ExtensionHost* host,
bool is_background) {
all_hosts_.insert(host);
if (is_background)
background_hosts_.insert(host);
NotificationService::current()->Notify(
NotificationType::EXTENSION_HOST_CREATED,
Source<ExtensionProcessManager>(this),
Details<ExtensionHost>(host));
}
|