| OLD | NEW |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/task_manager/task_manager_tab_contents_resource_provide
r.h" | 5 #include "chrome/browser/task_manager/tab_contents_resource_provider.h" |
| 6 | 6 |
| 7 #include <string> | |
| 8 | |
| 9 #include "base/i18n/rtl.h" | |
| 10 #include "base/utf_string_conversions.h" | |
| 11 #include "chrome/browser/browser_process.h" | 7 #include "chrome/browser/browser_process.h" |
| 12 #include "chrome/browser/extensions/extension_service.h" | 8 #include "chrome/browser/extensions/extension_service.h" |
| 13 #include "chrome/browser/favicon/favicon_tab_helper.h" | 9 #include "chrome/browser/favicon/favicon_tab_helper.h" |
| 14 #include "chrome/browser/prerender/prerender_manager.h" | 10 #include "chrome/browser/prerender/prerender_manager.h" |
| 15 #include "chrome/browser/prerender/prerender_manager_factory.h" | 11 #include "chrome/browser/prerender/prerender_manager_factory.h" |
| 16 #include "chrome/browser/printing/background_printing_manager.h" | 12 #include "chrome/browser/printing/background_printing_manager.h" |
| 17 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
| 18 #include "chrome/browser/profiles/profile_info_cache.h" | |
| 19 #include "chrome/browser/profiles/profile_manager.h" | 14 #include "chrome/browser/profiles/profile_manager.h" |
| 20 #include "chrome/browser/tab_contents/tab_util.h" | 15 #include "chrome/browser/tab_contents/tab_util.h" |
| 16 #include "chrome/browser/task_manager/renderer_resource.h" |
| 17 #include "chrome/browser/task_manager/resource_util.h" |
| 21 #include "chrome/browser/ui/browser.h" | 18 #include "chrome/browser/ui/browser.h" |
| 22 #include "chrome/browser/ui/browser_finder.h" | 19 #include "chrome/browser/ui/browser_finder.h" |
| 23 #include "chrome/browser/ui/browser_instant_controller.h" | 20 #include "chrome/browser/ui/browser_instant_controller.h" |
| 24 #include "chrome/browser/ui/browser_iterator.h" | 21 #include "chrome/browser/ui/browser_iterator.h" |
| 25 #include "chrome/browser/ui/tab_contents/tab_contents_iterator.h" | 22 #include "chrome/browser/ui/tab_contents/tab_contents_iterator.h" |
| 26 #include "chrome/common/chrome_notification_types.h" | 23 #include "chrome/common/chrome_notification_types.h" |
| 27 #include "chrome/common/extensions/extension.h" | |
| 28 #include "content/public/browser/notification_service.h" | 24 #include "content/public/browser/notification_service.h" |
| 29 #include "content/public/browser/render_process_host.h" | 25 #include "content/public/browser/render_process_host.h" |
| 30 #include "content/public/browser/web_contents.h" | 26 #include "content/public/browser/web_contents.h" |
| 31 #include "extensions/common/constants.h" | 27 #include "extensions/common/constants.h" |
| 32 #include "grit/generated_resources.h" | |
| 33 #include "grit/theme_resources.h" | 28 #include "grit/theme_resources.h" |
| 34 #include "ui/base/l10n/l10n_util.h" | 29 #include "ui/base/l10n/l10n_util.h" |
| 35 #include "ui/base/resource/resource_bundle.h" | 30 #include "ui/base/resource/resource_bundle.h" |
| 36 #include "ui/gfx/image/image_skia.h" | 31 #include "ui/gfx/image/image_skia.h" |
| 37 | 32 |
| 38 using content::WebContents; | 33 using content::WebContents; |
| 39 using extensions::Extension; | 34 using extensions::Extension; |
| 40 | 35 |
| 41 namespace { | 36 namespace { |
| 42 | 37 |
| 43 // Returns the appropriate message prefix ID for tabs and extensions, | |
| 44 // reflecting whether they are apps or in incognito mode. | |
| 45 int GetMessagePrefixID(bool is_app, | |
| 46 bool is_extension, | |
| 47 bool is_incognito, | |
| 48 bool is_prerender, | |
| 49 bool is_instant_overlay, | |
| 50 bool is_background) { | |
| 51 if (is_app) { | |
| 52 if (is_background) { | |
| 53 return IDS_TASK_MANAGER_BACKGROUND_PREFIX; | |
| 54 } else if (is_incognito) { | |
| 55 return IDS_TASK_MANAGER_APP_INCOGNITO_PREFIX; | |
| 56 } else { | |
| 57 return IDS_TASK_MANAGER_APP_PREFIX; | |
| 58 } | |
| 59 } else if (is_extension) { | |
| 60 if (is_incognito) | |
| 61 return IDS_TASK_MANAGER_EXTENSION_INCOGNITO_PREFIX; | |
| 62 else | |
| 63 return IDS_TASK_MANAGER_EXTENSION_PREFIX; | |
| 64 } else if (is_prerender) { | |
| 65 return IDS_TASK_MANAGER_PRERENDER_PREFIX; | |
| 66 } else if (is_instant_overlay) { | |
| 67 return IDS_TASK_MANAGER_INSTANT_OVERLAY_PREFIX; | |
| 68 } else { | |
| 69 return IDS_TASK_MANAGER_TAB_PREFIX; | |
| 70 } | |
| 71 } | |
| 72 | |
| 73 string16 GetProfileNameFromInfoCache(Profile* profile) { | |
| 74 ProfileInfoCache& cache = | |
| 75 g_browser_process->profile_manager()->GetProfileInfoCache(); | |
| 76 size_t index = cache.GetIndexOfProfileWithPath( | |
| 77 profile->GetOriginalProfile()->GetPath()); | |
| 78 if (index == std::string::npos) | |
| 79 return string16(); | |
| 80 else | |
| 81 return cache.GetNameOfProfileAtIndex(index); | |
| 82 } | |
| 83 | |
| 84 string16 GetTitleFromWebContents(WebContents* web_contents) { | |
| 85 string16 title = web_contents->GetTitle(); | |
| 86 if (title.empty()) { | |
| 87 GURL url = web_contents->GetURL(); | |
| 88 title = UTF8ToUTF16(url.spec()); | |
| 89 // Force URL to be LTR. | |
| 90 title = base::i18n::GetDisplayStringInLTRDirectionality(title); | |
| 91 } else { | |
| 92 // Since the tab_title will be concatenated with | |
| 93 // IDS_TASK_MANAGER_TAB_PREFIX, we need to explicitly set the tab_title to | |
| 94 // be LTR format if there is no strong RTL charater in it. Otherwise, if | |
| 95 // IDS_TASK_MANAGER_TAB_PREFIX is an RTL word, the concatenated result | |
| 96 // might be wrong. For example, http://mail.yahoo.com, whose title is | |
| 97 // "Yahoo! Mail: The best web-based Email!", without setting it explicitly | |
| 98 // as LTR format, the concatenated result will be "!Yahoo! Mail: The best | |
| 99 // web-based Email :BAT", in which the capital letters "BAT" stands for | |
| 100 // the Hebrew word for "tab". | |
| 101 base::i18n::AdjustStringForLocaleDirection(&title); | |
| 102 } | |
| 103 return title; | |
| 104 } | |
| 105 | |
| 106 bool IsContentsPrerendering(WebContents* web_contents) { | 38 bool IsContentsPrerendering(WebContents* web_contents) { |
| 107 Profile* profile = | 39 Profile* profile = |
| 108 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | 40 Profile::FromBrowserContext(web_contents->GetBrowserContext()); |
| 109 prerender::PrerenderManager* prerender_manager = | 41 prerender::PrerenderManager* prerender_manager = |
| 110 prerender::PrerenderManagerFactory::GetForProfile(profile); | 42 prerender::PrerenderManagerFactory::GetForProfile(profile); |
| 111 return prerender_manager && | 43 return prerender_manager && |
| 112 prerender_manager->IsWebContentsPrerendering(web_contents, NULL); | 44 prerender_manager->IsWebContentsPrerendering(web_contents, NULL); |
| 113 } | 45 } |
| 114 | 46 |
| 115 bool IsContentsInstant(WebContents* web_contents) { | 47 bool IsContentsInstant(WebContents* web_contents) { |
| 116 for (chrome::BrowserIterator it; !it.done(); it.Next()) { | 48 for (chrome::BrowserIterator it; !it.done(); it.Next()) { |
| 117 if (it->instant_controller() && | 49 if (it->instant_controller() && |
| 118 it->instant_controller()->instant()-> | 50 it->instant_controller()->instant()-> |
| 119 GetOverlayContents() == web_contents) { | 51 GetOverlayContents() == web_contents) { |
| 120 return true; | 52 return true; |
| 121 } | 53 } |
| 122 } | 54 } |
| 123 | 55 |
| 124 return false; | 56 return false; |
| 125 } | 57 } |
| 126 | 58 |
| 127 bool IsContentsBackgroundPrinted(WebContents* web_contents) { | 59 bool IsContentsBackgroundPrinted(WebContents* web_contents) { |
| 128 printing::BackgroundPrintingManager* printing_manager = | 60 printing::BackgroundPrintingManager* printing_manager = |
| 129 g_browser_process->background_printing_manager(); | 61 g_browser_process->background_printing_manager(); |
| 130 return printing_manager->HasPrintPreviewDialog(web_contents); | 62 return printing_manager->HasPrintPreviewDialog(web_contents); |
| 131 } | 63 } |
| 132 | 64 |
| 133 } // namespace | 65 } // namespace |
| 134 | 66 |
| 135 //////////////////////////////////////////////////////////////////////////////// | 67 namespace task_manager { |
| 136 // TaskManagerTabContentsResource class | |
| 137 //////////////////////////////////////////////////////////////////////////////// | |
| 138 | 68 |
| 139 // static | 69 // Tracks a single tab contents, prerendered page, Instant page, or background |
| 140 gfx::ImageSkia* TaskManagerTabContentsResource::prerender_icon_ = NULL; | 70 // printing page. |
| 71 class TabContentsResource : public RendererResource { |
| 72 public: |
| 73 explicit TabContentsResource(content::WebContents* web_contents); |
| 74 virtual ~TabContentsResource(); |
| 141 | 75 |
| 142 TaskManagerTabContentsResource::TaskManagerTabContentsResource( | 76 // Called when the underlying web_contents has been committed and is no |
| 77 // longer an Instant overlay. |
| 78 void InstantCommitted(); |
| 79 |
| 80 // TaskManager::Resource methods: |
| 81 virtual Type GetType() const OVERRIDE; |
| 82 virtual string16 GetTitle() const OVERRIDE; |
| 83 virtual string16 GetProfileName() const OVERRIDE; |
| 84 virtual gfx::ImageSkia GetIcon() const OVERRIDE; |
| 85 virtual content::WebContents* GetWebContents() const OVERRIDE; |
| 86 virtual const extensions::Extension* GetExtension() const OVERRIDE; |
| 87 |
| 88 private: |
| 89 // Returns true if contains content rendered by an extension. |
| 90 bool HostsExtension() const; |
| 91 |
| 92 static gfx::ImageSkia* prerender_icon_; |
| 93 content::WebContents* web_contents_; |
| 94 Profile* profile_; |
| 95 bool is_instant_overlay_; |
| 96 |
| 97 DISALLOW_COPY_AND_ASSIGN(TabContentsResource); |
| 98 }; |
| 99 |
| 100 gfx::ImageSkia* TabContentsResource::prerender_icon_ = NULL; |
| 101 |
| 102 TabContentsResource::TabContentsResource( |
| 143 WebContents* web_contents) | 103 WebContents* web_contents) |
| 144 : TaskManagerRendererResource( | 104 : RendererResource(web_contents->GetRenderProcessHost()->GetHandle(), |
| 145 web_contents->GetRenderProcessHost()->GetHandle(), | 105 web_contents->GetRenderViewHost()), |
| 146 web_contents->GetRenderViewHost()), | |
| 147 web_contents_(web_contents), | 106 web_contents_(web_contents), |
| 148 profile_(Profile::FromBrowserContext(web_contents->GetBrowserContext())), | 107 profile_(Profile::FromBrowserContext(web_contents->GetBrowserContext())), |
| 149 is_instant_overlay_(IsContentsInstant(web_contents)) { | 108 is_instant_overlay_(IsContentsInstant(web_contents)) { |
| 150 if (!prerender_icon_) { | 109 if (!prerender_icon_) { |
| 151 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | 110 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
| 152 prerender_icon_ = rb.GetImageSkiaNamed(IDR_PRERENDER); | 111 prerender_icon_ = rb.GetImageSkiaNamed(IDR_PRERENDER); |
| 153 } | 112 } |
| 154 } | 113 } |
| 155 | 114 |
| 156 TaskManagerTabContentsResource::~TaskManagerTabContentsResource() { | 115 TabContentsResource::~TabContentsResource() { |
| 157 } | 116 } |
| 158 | 117 |
| 159 void TaskManagerTabContentsResource::InstantCommitted() { | 118 void TabContentsResource::InstantCommitted() { |
| 160 DCHECK(is_instant_overlay_); | 119 DCHECK(is_instant_overlay_); |
| 161 is_instant_overlay_ = false; | 120 is_instant_overlay_ = false; |
| 162 } | 121 } |
| 163 | 122 |
| 164 bool TaskManagerTabContentsResource::HostsExtension() const { | 123 bool TabContentsResource::HostsExtension() const { |
| 165 return web_contents_->GetURL().SchemeIs(extensions::kExtensionScheme); | 124 return web_contents_->GetURL().SchemeIs(extensions::kExtensionScheme); |
| 166 } | 125 } |
| 167 | 126 |
| 168 TaskManager::Resource::Type TaskManagerTabContentsResource::GetType() const { | 127 TaskManager::Resource::Type TabContentsResource::GetType() const { |
| 169 return HostsExtension() ? EXTENSION : RENDERER; | 128 return HostsExtension() ? EXTENSION : RENDERER; |
| 170 } | 129 } |
| 171 | 130 |
| 172 string16 TaskManagerTabContentsResource::GetTitle() const { | 131 string16 TabContentsResource::GetTitle() const { |
| 173 // Fall back on the URL if there's no title. | 132 // Fall back on the URL if there's no title. |
| 174 GURL url = web_contents_->GetURL(); | 133 GURL url = web_contents_->GetURL(); |
| 175 string16 tab_title = GetTitleFromWebContents(web_contents_); | 134 string16 tab_title = ResourceUtil::GetTitleFromWebContents(web_contents_); |
| 176 | 135 |
| 177 // Only classify as an app if the URL is an app and the tab is hosting an | 136 // Only classify as an app if the URL is an app and the tab is hosting an |
| 178 // extension process. (It's possible to be showing the URL from before it | 137 // extension process. (It's possible to be showing the URL from before it |
| 179 // was installed as an app.) | 138 // was installed as an app.) |
| 180 ExtensionService* extension_service = profile_->GetExtensionService(); | 139 ExtensionService* extension_service = profile_->GetExtensionService(); |
| 181 extensions::ProcessMap* process_map = extension_service->process_map(); | 140 extensions::ProcessMap* process_map = extension_service->process_map(); |
| 182 bool is_app = extension_service->IsInstalledApp(url) && | 141 bool is_app = extension_service->IsInstalledApp(url) && |
| 183 process_map->Contains(web_contents_->GetRenderProcessHost()->GetID()); | 142 process_map->Contains(web_contents_->GetRenderProcessHost()->GetID()); |
| 184 | 143 |
| 185 int message_id = GetMessagePrefixID( | 144 int message_id = ResourceUtil::GetMessagePrefixID( |
| 186 is_app, | 145 is_app, |
| 187 HostsExtension(), | 146 HostsExtension(), |
| 188 profile_->IsOffTheRecord(), | 147 profile_->IsOffTheRecord(), |
| 189 IsContentsPrerendering(web_contents_), | 148 IsContentsPrerendering(web_contents_), |
| 190 is_instant_overlay_, | 149 is_instant_overlay_, |
| 191 false); | 150 false); // is_background |
| 192 return l10n_util::GetStringFUTF16(message_id, tab_title); | 151 return l10n_util::GetStringFUTF16(message_id, tab_title); |
| 193 } | 152 } |
| 194 | 153 |
| 195 string16 TaskManagerTabContentsResource::GetProfileName() const { | 154 string16 TabContentsResource::GetProfileName() const { |
| 196 return GetProfileNameFromInfoCache(profile_); | 155 return ResourceUtil::GetProfileNameFromInfoCache(profile_); |
| 197 } | 156 } |
| 198 | 157 |
| 199 gfx::ImageSkia TaskManagerTabContentsResource::GetIcon() const { | 158 gfx::ImageSkia TabContentsResource::GetIcon() const { |
| 200 if (IsContentsPrerendering(web_contents_)) | 159 if (IsContentsPrerendering(web_contents_)) |
| 201 return *prerender_icon_; | 160 return *prerender_icon_; |
| 202 return FaviconTabHelper::FromWebContents(web_contents_)-> | 161 return FaviconTabHelper::FromWebContents(web_contents_)-> |
| 203 GetFavicon().AsImageSkia(); | 162 GetFavicon().AsImageSkia(); |
| 204 } | 163 } |
| 205 | 164 |
| 206 WebContents* TaskManagerTabContentsResource::GetWebContents() const { | 165 WebContents* TabContentsResource::GetWebContents() const { |
| 207 return web_contents_; | 166 return web_contents_; |
| 208 } | 167 } |
| 209 | 168 |
| 210 const Extension* TaskManagerTabContentsResource::GetExtension() const { | 169 const Extension* TabContentsResource::GetExtension() const { |
| 211 if (HostsExtension()) { | 170 if (HostsExtension()) { |
| 212 ExtensionService* extension_service = profile_->GetExtensionService(); | 171 ExtensionService* extension_service = profile_->GetExtensionService(); |
| 213 return extension_service->extensions()->GetByID( | 172 return extension_service->extensions()->GetByID( |
| 214 web_contents_->GetURL().host()); | 173 web_contents_->GetURL().host()); |
| 215 } | 174 } |
| 216 | 175 |
| 217 return NULL; | 176 return NULL; |
| 218 } | 177 } |
| 219 | 178 |
| 220 //////////////////////////////////////////////////////////////////////////////// | 179 //////////////////////////////////////////////////////////////////////////////// |
| 221 // TaskManagerTabContentsResourceProvider class | 180 // TabContentsResourceProvider class |
| 222 //////////////////////////////////////////////////////////////////////////////// | 181 //////////////////////////////////////////////////////////////////////////////// |
| 223 | 182 |
| 224 TaskManagerTabContentsResourceProvider:: | 183 TabContentsResourceProvider:: |
| 225 TaskManagerTabContentsResourceProvider(TaskManager* task_manager) | 184 TabContentsResourceProvider(TaskManager* task_manager) |
| 226 : updating_(false), | 185 : updating_(false), |
| 227 task_manager_(task_manager) { | 186 task_manager_(task_manager) { |
| 228 } | 187 } |
| 229 | 188 |
| 230 TaskManagerTabContentsResourceProvider:: | 189 TabContentsResourceProvider::~TabContentsResourceProvider() { |
| 231 ~TaskManagerTabContentsResourceProvider() { | |
| 232 } | 190 } |
| 233 | 191 |
| 234 TaskManager::Resource* TaskManagerTabContentsResourceProvider::GetResource( | 192 TaskManager::Resource* TabContentsResourceProvider::GetResource( |
| 235 int origin_pid, | 193 int origin_pid, |
| 236 int render_process_host_id, | 194 int render_process_host_id, |
| 237 int routing_id) { | 195 int routing_id) { |
| 238 WebContents* web_contents = | 196 WebContents* web_contents = |
| 239 tab_util::GetWebContentsByID(render_process_host_id, routing_id); | 197 tab_util::GetWebContentsByID(render_process_host_id, routing_id); |
| 240 if (!web_contents) // Not one of our resource. | 198 if (!web_contents) // Not one of our resource. |
| 241 return NULL; | 199 return NULL; |
| 242 | 200 |
| 243 // If an origin PID was specified then the request originated in a plugin | 201 // If an origin PID was specified then the request originated in a plugin |
| 244 // working on the WebContents's behalf, so ignore it. | 202 // working on the WebContents's behalf, so ignore it. |
| 245 if (origin_pid) | 203 if (origin_pid) |
| 246 return NULL; | 204 return NULL; |
| 247 | 205 |
| 248 std::map<WebContents*, TaskManagerTabContentsResource*>::iterator | 206 std::map<WebContents*, TabContentsResource*>::iterator |
| 249 res_iter = resources_.find(web_contents); | 207 res_iter = resources_.find(web_contents); |
| 250 if (res_iter == resources_.end()) { | 208 if (res_iter == resources_.end()) { |
| 251 // Can happen if the tab was closed while a network request was being | 209 // Can happen if the tab was closed while a network request was being |
| 252 // performed. | 210 // performed. |
| 253 return NULL; | 211 return NULL; |
| 254 } | 212 } |
| 255 return res_iter->second; | 213 return res_iter->second; |
| 256 } | 214 } |
| 257 | 215 |
| 258 void TaskManagerTabContentsResourceProvider::StartUpdating() { | 216 void TabContentsResourceProvider::StartUpdating() { |
| 259 DCHECK(!updating_); | 217 DCHECK(!updating_); |
| 260 updating_ = true; | 218 updating_ = true; |
| 261 | 219 |
| 262 // The contents that are tracked by this resource provider are those that | 220 // The contents that are tracked by this resource provider are those that |
| 263 // are tab contents (WebContents serving as a tab in a Browser), Instant | 221 // are tab contents (WebContents serving as a tab in a Browser), Instant |
| 264 // pages, prerender pages, and background printed pages. | 222 // pages, prerender pages, and background printed pages. |
| 265 | 223 |
| 266 // Add all the existing WebContentses. | 224 // Add all the existing WebContentses. |
| 267 for (TabContentsIterator iterator; !iterator.done(); iterator.Next()) | 225 for (TabContentsIterator iterator; !iterator.done(); iterator.Next()) |
| 268 Add(*iterator); | 226 Add(*iterator); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 302 registrar_.Add(this, content::NOTIFICATION_WEB_CONTENTS_CONNECTED, | 260 registrar_.Add(this, content::NOTIFICATION_WEB_CONTENTS_CONNECTED, |
| 303 content::NotificationService::AllBrowserContextsAndSources()); | 261 content::NotificationService::AllBrowserContextsAndSources()); |
| 304 registrar_.Add(this, content::NOTIFICATION_WEB_CONTENTS_SWAPPED, | 262 registrar_.Add(this, content::NOTIFICATION_WEB_CONTENTS_SWAPPED, |
| 305 content::NotificationService::AllBrowserContextsAndSources()); | 263 content::NotificationService::AllBrowserContextsAndSources()); |
| 306 registrar_.Add(this, content::NOTIFICATION_WEB_CONTENTS_DISCONNECTED, | 264 registrar_.Add(this, content::NOTIFICATION_WEB_CONTENTS_DISCONNECTED, |
| 307 content::NotificationService::AllBrowserContextsAndSources()); | 265 content::NotificationService::AllBrowserContextsAndSources()); |
| 308 registrar_.Add(this, chrome::NOTIFICATION_INSTANT_COMMITTED, | 266 registrar_.Add(this, chrome::NOTIFICATION_INSTANT_COMMITTED, |
| 309 content::NotificationService::AllBrowserContextsAndSources()); | 267 content::NotificationService::AllBrowserContextsAndSources()); |
| 310 } | 268 } |
| 311 | 269 |
| 312 void TaskManagerTabContentsResourceProvider::StopUpdating() { | 270 void TabContentsResourceProvider::StopUpdating() { |
| 313 DCHECK(updating_); | 271 DCHECK(updating_); |
| 314 updating_ = false; | 272 updating_ = false; |
| 315 | 273 |
| 316 // Then we unregister for notifications to get new web contents. | 274 // Then we unregister for notifications to get new web contents. |
| 317 registrar_.Remove(this, content::NOTIFICATION_WEB_CONTENTS_CONNECTED, | 275 registrar_.Remove(this, content::NOTIFICATION_WEB_CONTENTS_CONNECTED, |
| 318 content::NotificationService::AllBrowserContextsAndSources()); | 276 content::NotificationService::AllBrowserContextsAndSources()); |
| 319 registrar_.Remove(this, content::NOTIFICATION_WEB_CONTENTS_SWAPPED, | 277 registrar_.Remove(this, content::NOTIFICATION_WEB_CONTENTS_SWAPPED, |
| 320 content::NotificationService::AllBrowserContextsAndSources()); | 278 content::NotificationService::AllBrowserContextsAndSources()); |
| 321 registrar_.Remove(this, content::NOTIFICATION_WEB_CONTENTS_DISCONNECTED, | 279 registrar_.Remove(this, content::NOTIFICATION_WEB_CONTENTS_DISCONNECTED, |
| 322 content::NotificationService::AllBrowserContextsAndSources()); | 280 content::NotificationService::AllBrowserContextsAndSources()); |
| 323 registrar_.Remove(this, chrome::NOTIFICATION_INSTANT_COMMITTED, | 281 registrar_.Remove(this, chrome::NOTIFICATION_INSTANT_COMMITTED, |
| 324 content::NotificationService::AllBrowserContextsAndSources()); | 282 content::NotificationService::AllBrowserContextsAndSources()); |
| 325 | 283 |
| 326 // Delete all the resources. | 284 // Delete all the resources. |
| 327 STLDeleteContainerPairSecondPointers(resources_.begin(), resources_.end()); | 285 STLDeleteContainerPairSecondPointers(resources_.begin(), resources_.end()); |
| 328 | 286 |
| 329 resources_.clear(); | 287 resources_.clear(); |
| 330 } | 288 } |
| 331 | 289 |
| 332 void TaskManagerTabContentsResourceProvider::AddToTaskManager( | 290 void TabContentsResourceProvider::AddToTaskManager(WebContents* web_contents) { |
| 333 WebContents* web_contents) { | 291 TabContentsResource* resource = new TabContentsResource(web_contents); |
| 334 TaskManagerTabContentsResource* resource = | |
| 335 new TaskManagerTabContentsResource(web_contents); | |
| 336 resources_[web_contents] = resource; | 292 resources_[web_contents] = resource; |
| 337 task_manager_->AddResource(resource); | 293 task_manager_->AddResource(resource); |
| 338 } | 294 } |
| 339 | 295 |
| 340 void TaskManagerTabContentsResourceProvider::Add(WebContents* web_contents) { | 296 void TabContentsResourceProvider::Add(WebContents* web_contents) { |
| 341 if (!updating_) | 297 if (!updating_) |
| 342 return; | 298 return; |
| 343 | 299 |
| 344 // The contents that are tracked by this resource provider are those that | 300 // The contents that are tracked by this resource provider are those that |
| 345 // are tab contents (WebContents serving as a tab in a Browser), Instant | 301 // are tab contents (WebContents serving as a tab in a Browser), Instant |
| 346 // pages, prerender pages, and background printed pages. | 302 // pages, prerender pages, and background printed pages. |
| 347 if (!chrome::FindBrowserWithWebContents(web_contents) && | 303 if (!chrome::FindBrowserWithWebContents(web_contents) && |
| 348 !IsContentsPrerendering(web_contents) && | 304 !IsContentsPrerendering(web_contents) && |
| 349 !IsContentsInstant(web_contents) && | 305 !IsContentsInstant(web_contents) && |
| 350 !IsContentsBackgroundPrinted(web_contents)) { | 306 !IsContentsBackgroundPrinted(web_contents)) { |
| 351 return; | 307 return; |
| 352 } | 308 } |
| 353 | 309 |
| 354 // Don't add dead tabs or tabs that haven't yet connected. | 310 // Don't add dead tabs or tabs that haven't yet connected. |
| 355 if (!web_contents->GetRenderProcessHost()->GetHandle() || | 311 if (!web_contents->GetRenderProcessHost()->GetHandle() || |
| 356 !web_contents->WillNotifyDisconnection()) { | 312 !web_contents->WillNotifyDisconnection()) { |
| 357 return; | 313 return; |
| 358 } | 314 } |
| 359 | 315 |
| 360 if (resources_.count(web_contents)) { | 316 if (resources_.count(web_contents)) { |
| 361 // The case may happen that we have added a WebContents as part of the | 317 // The case may happen that we have added a WebContents as part of the |
| 362 // iteration performed during StartUpdating() call but the notification that | 318 // iteration performed during StartUpdating() call but the notification that |
| 363 // it has connected was not fired yet. So when the notification happens, we | 319 // it has connected was not fired yet. So when the notification happens, we |
| 364 // already know about this tab and just ignore it. | 320 // already know about this tab and just ignore it. |
| 365 return; | 321 return; |
| 366 } | 322 } |
| 367 AddToTaskManager(web_contents); | 323 AddToTaskManager(web_contents); |
| 368 } | 324 } |
| 369 | 325 |
| 370 void TaskManagerTabContentsResourceProvider::Remove(WebContents* web_contents) { | 326 void TabContentsResourceProvider::Remove(WebContents* web_contents) { |
| 371 if (!updating_) | 327 if (!updating_) |
| 372 return; | 328 return; |
| 373 std::map<WebContents*, TaskManagerTabContentsResource*>::iterator | 329 std::map<WebContents*, TabContentsResource*>::iterator |
| 374 iter = resources_.find(web_contents); | 330 iter = resources_.find(web_contents); |
| 375 if (iter == resources_.end()) { | 331 if (iter == resources_.end()) { |
| 376 // Since WebContents are destroyed asynchronously (see TabContentsCollector | 332 // Since WebContents are destroyed asynchronously (see TabContentsCollector |
| 377 // in navigation_controller.cc), we can be notified of a tab being removed | 333 // in navigation_controller.cc), we can be notified of a tab being removed |
| 378 // that we don't know. This can happen if the user closes a tab and quickly | 334 // that we don't know. This can happen if the user closes a tab and quickly |
| 379 // opens the task manager, before the tab is actually destroyed. | 335 // opens the task manager, before the tab is actually destroyed. |
| 380 return; | 336 return; |
| 381 } | 337 } |
| 382 | 338 |
| 383 // Remove the resource from the Task Manager. | 339 // Remove the resource from the Task Manager. |
| 384 TaskManagerTabContentsResource* resource = iter->second; | 340 TabContentsResource* resource = iter->second; |
| 385 task_manager_->RemoveResource(resource); | 341 task_manager_->RemoveResource(resource); |
| 386 // And from the provider. | 342 // And from the provider. |
| 387 resources_.erase(iter); | 343 resources_.erase(iter); |
| 388 // Finally, delete the resource. | 344 // Finally, delete the resource. |
| 389 delete resource; | 345 delete resource; |
| 390 } | 346 } |
| 391 | 347 |
| 392 void TaskManagerTabContentsResourceProvider::InstantCommitted( | 348 void TabContentsResourceProvider::InstantCommitted(WebContents* web_contents) { |
| 393 WebContents* web_contents) { | |
| 394 if (!updating_) | 349 if (!updating_) |
| 395 return; | 350 return; |
| 396 std::map<WebContents*, TaskManagerTabContentsResource*>::iterator | 351 std::map<WebContents*, TabContentsResource*>::iterator |
| 397 iter = resources_.find(web_contents); | 352 iter = resources_.find(web_contents); |
| 398 DCHECK(iter != resources_.end()); | 353 DCHECK(iter != resources_.end()); |
| 399 if (iter != resources_.end()) | 354 if (iter != resources_.end()) |
| 400 iter->second->InstantCommitted(); | 355 iter->second->InstantCommitted(); |
| 401 } | 356 } |
| 402 | 357 |
| 403 void TaskManagerTabContentsResourceProvider::Observe( | 358 void TabContentsResourceProvider::Observe( |
| 404 int type, | 359 int type, |
| 405 const content::NotificationSource& source, | 360 const content::NotificationSource& source, |
| 406 const content::NotificationDetails& details) { | 361 const content::NotificationDetails& details) { |
| 407 WebContents* web_contents = content::Source<WebContents>(source).ptr(); | 362 WebContents* web_contents = content::Source<WebContents>(source).ptr(); |
| 408 | 363 |
| 409 switch (type) { | 364 switch (type) { |
| 410 case content::NOTIFICATION_WEB_CONTENTS_CONNECTED: | 365 case content::NOTIFICATION_WEB_CONTENTS_CONNECTED: |
| 411 Add(web_contents); | 366 Add(web_contents); |
| 412 break; | 367 break; |
| 413 case content::NOTIFICATION_WEB_CONTENTS_SWAPPED: | 368 case content::NOTIFICATION_WEB_CONTENTS_SWAPPED: |
| 414 Remove(web_contents); | 369 Remove(web_contents); |
| 415 Add(web_contents); | 370 Add(web_contents); |
| 416 break; | 371 break; |
| 417 case content::NOTIFICATION_WEB_CONTENTS_DISCONNECTED: | 372 case content::NOTIFICATION_WEB_CONTENTS_DISCONNECTED: |
| 418 Remove(web_contents); | 373 Remove(web_contents); |
| 419 break; | 374 break; |
| 420 case chrome::NOTIFICATION_INSTANT_COMMITTED: | 375 case chrome::NOTIFICATION_INSTANT_COMMITTED: |
| 421 InstantCommitted(web_contents); | 376 InstantCommitted(web_contents); |
| 422 break; | 377 break; |
| 423 default: | 378 default: |
| 424 NOTREACHED() << "Unexpected notification."; | 379 NOTREACHED() << "Unexpected notification."; |
| 425 return; | 380 return; |
| 426 } | 381 } |
| 427 } | 382 } |
| 383 |
| 384 } // namespace task_manager |
| OLD | NEW |