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