Adding instrumentation to locate the source of jankiness
[chromium-blink-merge.git] / chrome / browser / task_manager / extension_information.cc
blob272b22c04d01e4fb078fed89cae4257597b28fc9
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.
5 #include "chrome/browser/task_manager/extension_information.h"
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/profiles/profile.h"
11 #include "chrome/browser/profiles/profile_manager.h"
12 #include "chrome/browser/task_manager/renderer_resource.h"
13 #include "chrome/browser/task_manager/task_manager_util.h"
14 #include "content/public/browser/render_frame_host.h"
15 #include "content/public/browser/render_process_host.h"
16 #include "content/public/browser/render_view_host.h"
17 #include "content/public/browser/web_contents.h"
18 #include "extensions/browser/extension_system.h"
19 #include "extensions/browser/guest_view/guest_view_base.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"
28 using content::WebContents;
29 using extensions::Extension;
31 namespace {
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());
42 } // namespace
44 namespace task_manager {
46 class ExtensionProcessResource : public RendererResource {
47 public:
48 explicit ExtensionProcessResource(const Extension* extension,
49 content::RenderViewHost* render_view_host);
50 virtual ~ExtensionProcessResource();
52 // Resource methods:
53 virtual base::string16 GetTitle() const override;
54 virtual gfx::ImageSkia GetIcon() const override;
55 virtual Type GetType() const override;
57 private:
58 // Returns true if the associated extension has a background page.
59 bool IsBackground() const;
61 // The icon painted for the extension process.
62 static gfx::ImageSkia* default_icon_;
64 // Cached data about the extension.
65 base::string16 title_;
67 DISALLOW_COPY_AND_ASSIGN(ExtensionProcessResource);
70 gfx::ImageSkia* ExtensionProcessResource::default_icon_ = NULL;
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);
81 base::string16 extension_name = base::UTF8ToUTF16(extension->name());
82 DCHECK(!extension_name.empty());
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);
94 ExtensionProcessResource::~ExtensionProcessResource() {}
96 base::string16 ExtensionProcessResource::GetTitle() const { return title_; }
98 gfx::ImageSkia ExtensionProcessResource::GetIcon() const {
99 return *default_icon_;
102 Resource::Type ExtensionProcessResource::GetType() const { return EXTENSION; }
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;
111 ////////////////////////////////////////////////////////////////////////////////
112 // ExtensionInformation class
113 ////////////////////////////////////////////////////////////////////////////////
115 ExtensionInformation::ExtensionInformation() {}
117 ExtensionInformation::~ExtensionInformation() {}
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());
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);
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 (extensions::GuestViewBase::IsGuest(web_contents))
152 return false;
154 // Extension WebContents tracked by this class will always host extension
155 // content.
156 if (!GetExtensionForWebContents(web_contents))
157 return false;
159 extensions::ViewType view_type = extensions::GetViewType(web_contents);
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);
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>();
177 return scoped_ptr<RendererResource>(new ExtensionProcessResource(
178 extension, web_contents->GetRenderViewHost()));
181 } // namespace task_manager