QUIC - cleanup changes to sync chromium tree with internal source.
[chromium-blink-merge.git] / chrome / browser / task_manager / tab_contents_information.cc
blob05ce45b0ededdb2b662f007185c254a0907cae51
1 // Copyright 2014 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.
5 #include "chrome/browser/task_manager/tab_contents_information.h"
7 #include "base/callback.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/browser/browser_process.h"
10 #include "chrome/browser/devtools/devtools_window.h"
11 #include "chrome/browser/favicon/favicon_utils.h"
12 #include "chrome/browser/prerender/prerender_manager.h"
13 #include "chrome/browser/prerender/prerender_manager_factory.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/profiles/profile_manager.h"
16 #include "chrome/browser/task_manager/renderer_resource.h"
17 #include "chrome/browser/task_manager/task_manager_util.h"
18 #include "chrome/browser/ui/browser.h"
19 #include "chrome/browser/ui/browser_finder.h"
20 #include "chrome/browser/ui/browser_iterator.h"
21 #include "chrome/browser/ui/tab_contents/tab_contents_iterator.h"
22 #include "components/favicon/content/content_favicon_driver.h"
23 #include "content/public/browser/render_process_host.h"
24 #include "content/public/browser/web_contents.h"
25 #include "extensions/browser/extension_registry.h"
26 #include "extensions/browser/process_map.h"
27 #include "extensions/common/constants.h"
28 #include "extensions/common/extension_set.h"
29 #include "grit/theme_resources.h"
30 #include "ui/base/l10n/l10n_util.h"
31 #include "ui/base/resource/resource_bundle.h"
32 #include "ui/gfx/image/image_skia.h"
34 using content::WebContents;
36 namespace task_manager {
38 namespace {
40 // Returns true if the WebContents is currently owned by the prerendering
41 // manager.
42 bool IsContentsPrerendering(WebContents* web_contents) {
43 Profile* profile =
44 Profile::FromBrowserContext(web_contents->GetBrowserContext());
45 prerender::PrerenderManager* prerender_manager =
46 prerender::PrerenderManagerFactory::GetForProfile(profile);
47 return prerender_manager &&
48 prerender_manager->IsWebContentsPrerendering(web_contents, NULL);
51 } // namespace
53 // Tracks a single tab contents, prerendered page, or Instant page.
54 class TabContentsResource : public RendererResource {
55 public:
56 explicit TabContentsResource(content::WebContents* web_contents);
57 ~TabContentsResource() override;
59 // Resource methods:
60 Type GetType() const override;
61 base::string16 GetTitle() const override;
62 gfx::ImageSkia GetIcon() const override;
63 content::WebContents* GetWebContents() const override;
65 private:
66 // Returns true if contains content rendered by an extension.
67 bool HostsExtension() const;
69 static gfx::ImageSkia* prerender_icon_;
70 content::WebContents* web_contents_;
71 Profile* profile_;
73 DISALLOW_COPY_AND_ASSIGN(TabContentsResource);
76 gfx::ImageSkia* TabContentsResource::prerender_icon_ = NULL;
78 TabContentsResource::TabContentsResource(WebContents* web_contents)
79 : RendererResource(web_contents->GetRenderProcessHost()->GetHandle(),
80 web_contents->GetRenderViewHost()),
81 web_contents_(web_contents),
82 profile_(Profile::FromBrowserContext(web_contents->GetBrowserContext())) {
83 if (!prerender_icon_) {
84 ResourceBundle& rb = ResourceBundle::GetSharedInstance();
85 prerender_icon_ = rb.GetImageSkiaNamed(IDR_PRERENDER);
89 TabContentsResource::~TabContentsResource() {}
91 bool TabContentsResource::HostsExtension() const {
92 return web_contents_->GetURL().SchemeIs(extensions::kExtensionScheme);
95 Resource::Type TabContentsResource::GetType() const {
96 // A tab that loads an extension URL is considered to be an extension for
97 // these purposes, although it's tracked as a TabContentsResource.
98 return HostsExtension() ? EXTENSION : RENDERER;
101 base::string16 TabContentsResource::GetTitle() const {
102 // Fall back on the URL if there's no title.
103 GURL url = web_contents_->GetURL();
104 base::string16 tab_title = util::GetTitleFromWebContents(web_contents_);
106 // Only classify as an app if the URL is an app and the tab is hosting an
107 // extension process. (It's possible to be showing the URL from before it
108 // was installed as an app.)
109 extensions::ProcessMap* process_map = extensions::ProcessMap::Get(profile_);
110 bool is_app =
111 extensions::ExtensionRegistry::Get(profile_)
112 ->enabled_extensions().GetAppByURL(url) != NULL &&
113 process_map->Contains(web_contents_->GetRenderProcessHost()->GetID());
115 int message_id = util::GetMessagePrefixID(
116 is_app,
117 HostsExtension(),
118 profile_->IsOffTheRecord(),
119 IsContentsPrerendering(web_contents_),
120 false); // is_background
121 return l10n_util::GetStringFUTF16(message_id, tab_title);
124 gfx::ImageSkia TabContentsResource::GetIcon() const {
125 if (IsContentsPrerendering(web_contents_))
126 return *prerender_icon_;
127 favicon::CreateContentFaviconDriverForWebContents(web_contents_);
128 return favicon::ContentFaviconDriver::FromWebContents(web_contents_)
129 ->GetFavicon()
130 .AsImageSkia();
133 WebContents* TabContentsResource::GetWebContents() const {
134 return web_contents_;
137 TabContentsInformation::TabContentsInformation() {}
139 TabContentsInformation::~TabContentsInformation() {}
141 bool TabContentsInformation::CheckOwnership(
142 content::WebContents* web_contents) {
143 return chrome::FindBrowserWithWebContents(web_contents) ||
144 DevToolsWindow::IsDevToolsWindow(web_contents) ||
145 IsContentsPrerendering(web_contents);
148 void TabContentsInformation::GetAll(const NewWebContentsCallback& callback) {
149 for (TabContentsIterator iterator; !iterator.done(); iterator.Next()) {
150 callback.Run(*iterator);
151 WebContents* devtools =
152 DevToolsWindow::GetInTabWebContents(*iterator, NULL);
153 if (devtools)
154 callback.Run(devtools);
157 // Because a WebContents* may start its life as a prerender, and later be
158 // put into a tab, this class tracks the prerender contents in the same
159 // way as the tab contents.
160 std::vector<Profile*> profiles(
161 g_browser_process->profile_manager()->GetLoadedProfiles());
162 for (size_t i = 0; i < profiles.size(); ++i) {
163 prerender::PrerenderManager* prerender_manager =
164 prerender::PrerenderManagerFactory::GetForProfile(profiles[i]);
165 if (prerender_manager) {
166 const std::vector<content::WebContents*> contentses =
167 prerender_manager->GetAllPrerenderingContents();
168 for (size_t j = 0; j < contentses.size(); ++j)
169 callback.Run(contentses[j]);
174 scoped_ptr<RendererResource> TabContentsInformation::MakeResource(
175 content::WebContents* web_contents) {
176 return scoped_ptr<RendererResource>(new TabContentsResource(web_contents));
179 } // namespace task_manager