guest_information.cc revision 03b57e008b61dfcb1fbad3aea950ae0e001748b0
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/guest_information.h" 6 7#include "base/strings/string16.h" 8#include "chrome/browser/chrome_notification_types.h" 9#include "chrome/browser/favicon/favicon_tab_helper.h" 10#include "chrome/browser/profiles/profile.h" 11#include "chrome/browser/task_manager/renderer_resource.h" 12#include "chrome/browser/task_manager/resource_provider.h" 13#include "chrome/browser/task_manager/task_manager.h" 14#include "chrome/browser/task_manager/task_manager_util.h" 15#include "chrome/grit/generated_resources.h" 16#include "content/public/browser/notification_service.h" 17#include "content/public/browser/render_frame_host.h" 18#include "content/public/browser/render_process_host.h" 19#include "content/public/browser/render_view_host.h" 20#include "content/public/browser/render_widget_host_iterator.h" 21#include "content/public/browser/site_instance.h" 22#include "content/public/browser/web_contents.h" 23#include "ui/base/l10n/l10n_util.h" 24#include "ui/gfx/image/image.h" 25#include "ui/gfx/image/image_skia.h" 26 27using content::RenderProcessHost; 28using content::RenderViewHost; 29using content::RenderWidgetHost; 30using content::WebContents; 31using extensions::Extension; 32 33namespace task_manager { 34 35class GuestResource : public RendererResource { 36 public: 37 explicit GuestResource(content::RenderViewHost* render_view_host); 38 virtual ~GuestResource(); 39 40 // Resource methods: 41 virtual Type GetType() const OVERRIDE; 42 virtual base::string16 GetTitle() const OVERRIDE; 43 virtual gfx::ImageSkia GetIcon() const OVERRIDE; 44 virtual content::WebContents* GetWebContents() const OVERRIDE; 45 46 private: 47 DISALLOW_COPY_AND_ASSIGN(GuestResource); 48}; 49 50GuestResource::GuestResource(RenderViewHost* render_view_host) 51 : RendererResource( 52 render_view_host->GetSiteInstance()->GetProcess()->GetHandle(), 53 render_view_host) { 54} 55 56GuestResource::~GuestResource() { 57} 58 59Resource::Type GuestResource::GetType() const { 60 return GUEST; 61} 62 63base::string16 GuestResource::GetTitle() const { 64 WebContents* web_contents = GetWebContents(); 65 const int message_id = IDS_TASK_MANAGER_WEBVIEW_TAG_PREFIX; 66 if (web_contents) { 67 base::string16 title = util::GetTitleFromWebContents(web_contents); 68 return l10n_util::GetStringFUTF16(message_id, title); 69 } 70 return l10n_util::GetStringFUTF16(message_id, base::string16()); 71} 72 73gfx::ImageSkia GuestResource::GetIcon() const { 74 WebContents* web_contents = GetWebContents(); 75 if (web_contents && FaviconTabHelper::FromWebContents(web_contents)) { 76 return FaviconTabHelper::FromWebContents(web_contents)-> 77 GetFavicon().AsImageSkia(); 78 } 79 return gfx::ImageSkia(); 80} 81 82WebContents* GuestResource::GetWebContents() const { 83 return WebContents::FromRenderViewHost(render_view_host()); 84} 85 86//////////////////////////////////////////////////////////////////////////////// 87// GuestInformation class 88//////////////////////////////////////////////////////////////////////////////// 89 90GuestInformation::GuestInformation() {} 91 92GuestInformation::~GuestInformation() {} 93 94bool GuestInformation::CheckOwnership(WebContents* web_contents) { 95 // Guest WebContentses are created and owned internally by the content layer. 96 return web_contents->IsSubframe(); 97} 98 99void GuestInformation::GetAll(const NewWebContentsCallback& callback) { 100 scoped_ptr<content::RenderWidgetHostIterator> widgets( 101 content::RenderWidgetHost::GetRenderWidgetHosts()); 102 while (content::RenderWidgetHost* widget = widgets->GetNextHost()) { 103 if (widget->IsRenderView()) { 104 content::RenderViewHost* rvh = content::RenderViewHost::From(widget); 105 WebContents* web_contents = WebContents::FromRenderViewHost(rvh); 106 if (web_contents && web_contents->IsSubframe()) 107 callback.Run(web_contents); 108 } 109 } 110} 111 112scoped_ptr<RendererResource> GuestInformation::MakeResource( 113 WebContents* web_contents) { 114 return scoped_ptr<RendererResource>( 115 new GuestResource(web_contents->GetRenderViewHost())); 116} 117 118} // namespace task_manager 119