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