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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
|
// Copyright 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/task_manager/tab_contents_resource_provider.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/favicon/favicon_tab_helper.h"
#include "chrome/browser/prerender/prerender_manager.h"
#include "chrome/browser/prerender/prerender_manager_factory.h"
#include "chrome/browser/printing/background_printing_manager.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/search/search.h"
#include "chrome/browser/tab_contents/tab_util.h"
#include "chrome/browser/task_manager/renderer_resource.h"
#include "chrome/browser/task_manager/resource_provider.h"
#include "chrome/browser/task_manager/task_manager.h"
#include "chrome/browser/task_manager/task_manager_util.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_instant_controller.h"
#include "chrome/browser/ui/browser_iterator.h"
#include "chrome/browser/ui/tab_contents/tab_contents_iterator.h"
#include "chrome/common/chrome_notification_types.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/web_contents.h"
#include "extensions/common/constants.h"
#include "grit/theme_resources.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/image/image_skia.h"
using content::WebContents;
using extensions::Extension;
namespace {
bool IsContentsPrerendering(WebContents* web_contents) {
Profile* profile =
Profile::FromBrowserContext(web_contents->GetBrowserContext());
prerender::PrerenderManager* prerender_manager =
prerender::PrerenderManagerFactory::GetForProfile(profile);
return prerender_manager &&
prerender_manager->IsWebContentsPrerendering(web_contents, NULL);
}
bool IsContentsBackgroundPrinted(WebContents* web_contents) {
printing::BackgroundPrintingManager* printing_manager =
g_browser_process->background_printing_manager();
return printing_manager->HasPrintPreviewDialog(web_contents);
}
} // namespace
namespace task_manager {
// Tracks a single tab contents, prerendered page, Instant page, or background
// printing page.
class TabContentsResource : public RendererResource {
public:
explicit TabContentsResource(content::WebContents* web_contents);
virtual ~TabContentsResource();
// Resource methods:
virtual Type GetType() const OVERRIDE;
virtual string16 GetTitle() const OVERRIDE;
virtual string16 GetProfileName() const OVERRIDE;
virtual gfx::ImageSkia GetIcon() const OVERRIDE;
virtual content::WebContents* GetWebContents() const OVERRIDE;
virtual const extensions::Extension* GetExtension() const OVERRIDE;
private:
// Returns true if contains content rendered by an extension.
bool HostsExtension() const;
static gfx::ImageSkia* prerender_icon_;
content::WebContents* web_contents_;
Profile* profile_;
bool is_instant_ntp_;
DISALLOW_COPY_AND_ASSIGN(TabContentsResource);
};
gfx::ImageSkia* TabContentsResource::prerender_icon_ = NULL;
TabContentsResource::TabContentsResource(
WebContents* web_contents)
: RendererResource(web_contents->GetRenderProcessHost()->GetHandle(),
web_contents->GetRenderViewHost()),
web_contents_(web_contents),
profile_(Profile::FromBrowserContext(web_contents->GetBrowserContext())),
is_instant_ntp_(chrome::IsPreloadedInstantExtendedNTP(web_contents)) {
if (!prerender_icon_) {
ResourceBundle& rb = ResourceBundle::GetSharedInstance();
prerender_icon_ = rb.GetImageSkiaNamed(IDR_PRERENDER);
}
}
TabContentsResource::~TabContentsResource() {
}
bool TabContentsResource::HostsExtension() const {
return web_contents_->GetURL().SchemeIs(extensions::kExtensionScheme);
}
Resource::Type TabContentsResource::GetType() const {
return HostsExtension() ? EXTENSION : RENDERER;
}
string16 TabContentsResource::GetTitle() const {
// Fall back on the URL if there's no title.
GURL url = web_contents_->GetURL();
string16 tab_title = util::GetTitleFromWebContents(web_contents_);
// Only classify as an app if the URL is an app and the tab is hosting an
// extension process. (It's possible to be showing the URL from before it
// was installed as an app.)
ExtensionService* extension_service = profile_->GetExtensionService();
extensions::ProcessMap* process_map = extension_service->process_map();
bool is_app = extension_service->IsInstalledApp(url) &&
process_map->Contains(web_contents_->GetRenderProcessHost()->GetID());
int message_id = util::GetMessagePrefixID(
is_app,
HostsExtension(),
profile_->IsOffTheRecord(),
IsContentsPrerendering(web_contents_),
is_instant_ntp_,
false); // is_background
return l10n_util::GetStringFUTF16(message_id, tab_title);
}
string16 TabContentsResource::GetProfileName() const {
return util::GetProfileNameFromInfoCache(profile_);
}
gfx::ImageSkia TabContentsResource::GetIcon() const {
if (IsContentsPrerendering(web_contents_))
return *prerender_icon_;
return FaviconTabHelper::FromWebContents(web_contents_)->
GetFavicon().AsImageSkia();
}
WebContents* TabContentsResource::GetWebContents() const {
return web_contents_;
}
const Extension* TabContentsResource::GetExtension() const {
if (HostsExtension()) {
ExtensionService* extension_service = profile_->GetExtensionService();
return extension_service->extensions()->GetByID(
web_contents_->GetURL().host());
}
return NULL;
}
////////////////////////////////////////////////////////////////////////////////
// TabContentsResourceProvider class
////////////////////////////////////////////////////////////////////////////////
TabContentsResourceProvider::
TabContentsResourceProvider(TaskManager* task_manager)
: updating_(false),
task_manager_(task_manager) {
}
TabContentsResourceProvider::~TabContentsResourceProvider() {
}
Resource* TabContentsResourceProvider::GetResource(
int origin_pid,
int render_process_host_id,
int routing_id) {
WebContents* web_contents =
tab_util::GetWebContentsByID(render_process_host_id, routing_id);
if (!web_contents) // Not one of our resource.
return NULL;
// If an origin PID was specified then the request originated in a plugin
// working on the WebContents's behalf, so ignore it.
if (origin_pid)
return NULL;
std::map<WebContents*, TabContentsResource*>::iterator
res_iter = resources_.find(web_contents);
if (res_iter == resources_.end()) {
// Can happen if the tab was closed while a network request was being
// performed.
return NULL;
}
return res_iter->second;
}
void TabContentsResourceProvider::StartUpdating() {
DCHECK(!updating_);
updating_ = true;
// The contents that are tracked by this resource provider are those that
// are tab contents (WebContents serving as a tab in a Browser), Instant
// pages, prerender pages, and background printed pages.
// Add all the existing WebContentses.
for (TabContentsIterator iterator; !iterator.done(); iterator.Next())
Add(*iterator);
// Add all the Instant pages.
for (chrome::BrowserIterator it; !it.done(); it.Next()) {
if (it->instant_controller()) {
if (it->instant_controller()->instant()->GetNTPContents())
Add(it->instant_controller()->instant()->GetNTPContents());
}
}
// Add all the prerender pages.
std::vector<Profile*> profiles(
g_browser_process->profile_manager()->GetLoadedProfiles());
for (size_t i = 0; i < profiles.size(); ++i) {
prerender::PrerenderManager* prerender_manager =
prerender::PrerenderManagerFactory::GetForProfile(profiles[i]);
if (prerender_manager) {
const std::vector<content::WebContents*> contentses =
prerender_manager->GetAllPrerenderingContents();
for (size_t j = 0; j < contentses.size(); ++j)
Add(contentses[j]);
}
}
// Add all the pages being background printed.
printing::BackgroundPrintingManager* printing_manager =
g_browser_process->background_printing_manager();
for (printing::BackgroundPrintingManager::WebContentsSet::iterator i =
printing_manager->begin();
i != printing_manager->end(); ++i) {
Add(*i);
}
// Then we register for notifications to get new web contents.
registrar_.Add(this, content::NOTIFICATION_WEB_CONTENTS_CONNECTED,
content::NotificationService::AllBrowserContextsAndSources());
registrar_.Add(this, content::NOTIFICATION_WEB_CONTENTS_SWAPPED,
content::NotificationService::AllBrowserContextsAndSources());
registrar_.Add(this, content::NOTIFICATION_WEB_CONTENTS_DISCONNECTED,
content::NotificationService::AllBrowserContextsAndSources());
}
void TabContentsResourceProvider::StopUpdating() {
DCHECK(updating_);
updating_ = false;
// Then we unregister for notifications to get new web contents.
registrar_.Remove(this, content::NOTIFICATION_WEB_CONTENTS_CONNECTED,
content::NotificationService::AllBrowserContextsAndSources());
registrar_.Remove(this, content::NOTIFICATION_WEB_CONTENTS_SWAPPED,
content::NotificationService::AllBrowserContextsAndSources());
registrar_.Remove(this, content::NOTIFICATION_WEB_CONTENTS_DISCONNECTED,
content::NotificationService::AllBrowserContextsAndSources());
// Delete all the resources.
STLDeleteContainerPairSecondPointers(resources_.begin(), resources_.end());
resources_.clear();
}
void TabContentsResourceProvider::AddToTaskManager(WebContents* web_contents) {
TabContentsResource* resource = new TabContentsResource(web_contents);
resources_[web_contents] = resource;
task_manager_->AddResource(resource);
}
void TabContentsResourceProvider::Add(WebContents* web_contents) {
if (!updating_)
return;
// The contents that are tracked by this resource provider are those that
// are tab contents (WebContents serving as a tab in a Browser), Instant
// pages, prerender pages, and background printed pages.
if (!chrome::FindBrowserWithWebContents(web_contents) &&
!IsContentsPrerendering(web_contents) &&
!chrome::IsPreloadedInstantExtendedNTP(web_contents) &&
!IsContentsBackgroundPrinted(web_contents)) {
return;
}
// Don't add dead tabs or tabs that haven't yet connected.
if (!web_contents->GetRenderProcessHost()->GetHandle() ||
!web_contents->WillNotifyDisconnection()) {
return;
}
if (resources_.count(web_contents)) {
// The case may happen that we have added a WebContents as part of the
// iteration performed during StartUpdating() call but the notification that
// it has connected was not fired yet. So when the notification happens, we
// already know about this tab and just ignore it.
return;
}
AddToTaskManager(web_contents);
}
void TabContentsResourceProvider::Remove(WebContents* web_contents) {
if (!updating_)
return;
std::map<WebContents*, TabContentsResource*>::iterator
iter = resources_.find(web_contents);
if (iter == resources_.end()) {
// Since WebContents are destroyed asynchronously (see TabContentsCollector
// in navigation_controller.cc), we can be notified of a tab being removed
// that we don't know. This can happen if the user closes a tab and quickly
// opens the task manager, before the tab is actually destroyed.
return;
}
// Remove the resource from the Task Manager.
TabContentsResource* resource = iter->second;
task_manager_->RemoveResource(resource);
// And from the provider.
resources_.erase(iter);
// Finally, delete the resource.
delete resource;
}
void TabContentsResourceProvider::Observe(
int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) {
WebContents* web_contents = content::Source<WebContents>(source).ptr();
switch (type) {
case content::NOTIFICATION_WEB_CONTENTS_CONNECTED:
Add(web_contents);
break;
case content::NOTIFICATION_WEB_CONTENTS_SWAPPED:
Remove(web_contents);
Add(web_contents);
break;
case content::NOTIFICATION_WEB_CONTENTS_DISCONNECTED:
Remove(web_contents);
break;
default:
NOTREACHED() << "Unexpected notification.";
return;
}
}
} // namespace task_manager
|