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/extension_information.h" 6 7 #include "base/strings/string16.h" 8 #include "base/strings/utf_string_conversions.h" 9 #include "chrome/browser/browser_process.h" 10 #include "chrome/browser/guest_view/guest_view_base.h" 11 #include "chrome/browser/profiles/profile.h" 12 #include "chrome/browser/profiles/profile_manager.h" 13 #include "chrome/browser/task_manager/renderer_resource.h" 14 #include "chrome/browser/task_manager/task_manager_util.h" 15 #include "content/public/browser/render_frame_host.h" 16 #include "content/public/browser/render_process_host.h" 17 #include "content/public/browser/render_view_host.h" 18 #include "content/public/browser/web_contents.h" 19 #include "extensions/browser/extension_system.h" 20 #include "extensions/browser/process_manager.h" 21 #include "extensions/browser/view_type_utils.h" 22 #include "extensions/common/extension.h" 23 #include "grit/theme_resources.h" 24 #include "ui/base/l10n/l10n_util.h" 25 #include "ui/base/resource/resource_bundle.h" 26 #include "ui/gfx/image/image_skia.h" 27 28 using content::WebContents; 29 using extensions::Extension; 30 31 namespace { 32 33 const Extension* GetExtensionForWebContents(WebContents* web_contents) { 34 Profile* profile = 35 Profile::FromBrowserContext(web_contents->GetBrowserContext()); 36 extensions::ProcessManager* process_manager = 37 extensions::ExtensionSystem::Get(profile)->process_manager(); 38 return process_manager->GetExtensionForRenderViewHost( 39 web_contents->GetRenderViewHost()); 40 } 41 42 } // namespace 43 44 namespace task_manager { 45 46 class ExtensionProcessResource : public RendererResource { 47 public: 48 explicit ExtensionProcessResource(const Extension* extension, 49 content::RenderViewHost* render_view_host); 50 virtual ~ExtensionProcessResource(); 51 52 // Resource methods: 53 virtual base::string16 GetTitle() const OVERRIDE; 54 virtual gfx::ImageSkia GetIcon() const OVERRIDE; 55 virtual Type GetType() const OVERRIDE; 56 57 private: 58 // Returns true if the associated extension has a background page. 59 bool IsBackground() const; 60 61 // The icon painted for the extension process. 62 static gfx::ImageSkia* default_icon_; 63 64 // Cached data about the extension. 65 base::string16 title_; 66 67 DISALLOW_COPY_AND_ASSIGN(ExtensionProcessResource); 68 }; 69 70 gfx::ImageSkia* ExtensionProcessResource::default_icon_ = NULL; 71 72 ExtensionProcessResource::ExtensionProcessResource( 73 const Extension* extension, 74 content::RenderViewHost* render_view_host) 75 : RendererResource(render_view_host->GetProcess()->GetHandle(), 76 render_view_host) { 77 if (!default_icon_) { 78 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); 79 default_icon_ = rb.GetImageSkiaNamed(IDR_PLUGINS_FAVICON); 80 } 81 base::string16 extension_name = base::UTF8ToUTF16(extension->name()); 82 DCHECK(!extension_name.empty()); 83 84 Profile* profile = Profile::FromBrowserContext( 85 render_view_host->GetProcess()->GetBrowserContext()); 86 int message_id = util::GetMessagePrefixID(extension->is_app(), 87 true, // is_extension 88 profile->IsOffTheRecord(), 89 false, // is_prerender 90 IsBackground()); 91 title_ = l10n_util::GetStringFUTF16(message_id, extension_name); 92 } 93 94 ExtensionProcessResource::~ExtensionProcessResource() {} 95 96 base::string16 ExtensionProcessResource::GetTitle() const { return title_; } 97 98 gfx::ImageSkia ExtensionProcessResource::GetIcon() const { 99 return *default_icon_; 100 } 101 102 Resource::Type ExtensionProcessResource::GetType() const { return EXTENSION; } 103 104 bool ExtensionProcessResource::IsBackground() const { 105 WebContents* web_contents = 106 WebContents::FromRenderViewHost(render_view_host()); 107 extensions::ViewType view_type = extensions::GetViewType(web_contents); 108 return view_type == extensions::VIEW_TYPE_EXTENSION_BACKGROUND_PAGE; 109 } 110 111 //////////////////////////////////////////////////////////////////////////////// 112 // ExtensionInformation class 113 //////////////////////////////////////////////////////////////////////////////// 114 115 ExtensionInformation::ExtensionInformation() {} 116 117 ExtensionInformation::~ExtensionInformation() {} 118 119 void ExtensionInformation::GetAll(const NewWebContentsCallback& callback) { 120 // Add all the existing extension views from all Profiles, including those 121 // from incognito split mode. 122 ProfileManager* profile_manager = g_browser_process->profile_manager(); 123 std::vector<Profile*> profiles(profile_manager->GetLoadedProfiles()); 124 size_t num_default_profiles = profiles.size(); 125 for (size_t i = 0; i < num_default_profiles; ++i) { 126 if (profiles[i]->HasOffTheRecordProfile()) { 127 profiles.push_back(profiles[i]->GetOffTheRecordProfile()); 128 } 129 } 130 131 for (size_t i = 0; i < profiles.size(); ++i) { 132 extensions::ProcessManager* process_manager = 133 extensions::ExtensionSystem::Get(profiles[i])->process_manager(); 134 if (process_manager) { 135 const extensions::ProcessManager::ViewSet all_views = 136 process_manager->GetAllViews(); 137 extensions::ProcessManager::ViewSet::const_iterator jt = 138 all_views.begin(); 139 for (; jt != all_views.end(); ++jt) { 140 WebContents* web_contents = WebContents::FromRenderViewHost(*jt); 141 if (CheckOwnership(web_contents)) 142 callback.Run(web_contents); 143 } 144 } 145 } 146 } 147 148 bool ExtensionInformation::CheckOwnership(WebContents* web_contents) { 149 // Don't add WebContents that belong to a guest (those are handled by 150 // GuestInformation). Otherwise they will be added twice. 151 if (GuestViewBase::IsGuest(web_contents)) 152 return false; 153 154 // Extension WebContents tracked by this class will always host extension 155 // content. 156 if (!GetExtensionForWebContents(web_contents)) 157 return false; 158 159 extensions::ViewType view_type = extensions::GetViewType(web_contents); 160 161 // Don't add tab contents (those are handled by TabContentsResourceProvider) 162 // or background contents (handled by BackgroundInformation) or panels 163 // (handled by PanelInformation) 164 return (view_type != extensions::VIEW_TYPE_INVALID && 165 view_type != extensions::VIEW_TYPE_TAB_CONTENTS && 166 view_type != extensions::VIEW_TYPE_BACKGROUND_CONTENTS && 167 view_type != extensions::VIEW_TYPE_PANEL); 168 } 169 170 scoped_ptr<RendererResource> ExtensionInformation::MakeResource( 171 WebContents* web_contents) { 172 const Extension* extension = GetExtensionForWebContents(web_contents); 173 if (!extension) { 174 NOTREACHED(); 175 return scoped_ptr<RendererResource>(); 176 } 177 return scoped_ptr<RendererResource>(new ExtensionProcessResource( 178 extension, web_contents->GetRenderViewHost())); 179 } 180 181 } // namespace task_manager 182