Add ICU message format support
[chromium-blink-merge.git] / chrome / browser / task_manager / extension_information.cc
blob527b7c20de0ccee5d3f7f8fa442b763353019fd0
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 "components/guest_view/browser/guest_view_base.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/process_manager.h"
20 #include "extensions/browser/view_type_utils.h"
21 #include "extensions/common/extension.h"
22 #include "grit/theme_resources.h"
23 #include "ui/base/l10n/l10n_util.h"
24 #include "ui/base/resource/resource_bundle.h"
25 #include "ui/gfx/image/image_skia.h"
27 using content::WebContents;
28 using extensions::Extension;
30 namespace {
32 const Extension* GetExtensionForWebContents(WebContents* web_contents) {
33 return extensions::ProcessManager::Get(web_contents->GetBrowserContext())
34 ->GetExtensionForWebContents(web_contents);
37 } // namespace
39 namespace task_manager {
41 class ExtensionProcessResource : public RendererResource {
42 public:
43 explicit ExtensionProcessResource(const Extension* extension,
44 content::RenderViewHost* render_view_host);
45 ~ExtensionProcessResource() override;
47 // Resource methods:
48 base::string16 GetTitle() const override;
49 gfx::ImageSkia GetIcon() const override;
50 Type GetType() const override;
52 private:
53 // Returns true if the associated extension has a background page.
54 bool IsBackground() const;
56 // The icon painted for the extension process.
57 static gfx::ImageSkia* default_icon_;
59 // Cached data about the extension.
60 base::string16 title_;
62 DISALLOW_COPY_AND_ASSIGN(ExtensionProcessResource);
65 gfx::ImageSkia* ExtensionProcessResource::default_icon_ = NULL;
67 ExtensionProcessResource::ExtensionProcessResource(
68 const Extension* extension,
69 content::RenderViewHost* render_view_host)
70 : RendererResource(render_view_host->GetProcess()->GetHandle(),
71 render_view_host) {
72 if (!default_icon_) {
73 ResourceBundle& rb = ResourceBundle::GetSharedInstance();
74 default_icon_ = rb.GetImageSkiaNamed(IDR_PLUGINS_FAVICON);
76 base::string16 extension_name = base::UTF8ToUTF16(extension->name());
77 DCHECK(!extension_name.empty());
79 Profile* profile = Profile::FromBrowserContext(
80 render_view_host->GetProcess()->GetBrowserContext());
81 int message_id = util::GetMessagePrefixID(extension->is_app(),
82 true, // is_extension
83 profile->IsOffTheRecord(),
84 false, // is_prerender
85 IsBackground());
86 title_ = l10n_util::GetStringFUTF16(message_id, extension_name);
89 ExtensionProcessResource::~ExtensionProcessResource() {}
91 base::string16 ExtensionProcessResource::GetTitle() const { return title_; }
93 gfx::ImageSkia ExtensionProcessResource::GetIcon() const {
94 return *default_icon_;
97 Resource::Type ExtensionProcessResource::GetType() const { return EXTENSION; }
99 bool ExtensionProcessResource::IsBackground() const {
100 WebContents* web_contents =
101 WebContents::FromRenderViewHost(render_view_host());
102 extensions::ViewType view_type = extensions::GetViewType(web_contents);
103 return view_type == extensions::VIEW_TYPE_EXTENSION_BACKGROUND_PAGE;
106 ////////////////////////////////////////////////////////////////////////////////
107 // ExtensionInformation class
108 ////////////////////////////////////////////////////////////////////////////////
110 ExtensionInformation::ExtensionInformation() {}
112 ExtensionInformation::~ExtensionInformation() {}
114 void ExtensionInformation::GetAll(const NewWebContentsCallback& callback) {
115 // Add all the existing extension views from all Profiles, including those
116 // from incognito split mode.
117 ProfileManager* profile_manager = g_browser_process->profile_manager();
118 std::vector<Profile*> profiles(profile_manager->GetLoadedProfiles());
119 size_t num_default_profiles = profiles.size();
120 for (size_t i = 0; i < num_default_profiles; ++i) {
121 if (profiles[i]->HasOffTheRecordProfile()) {
122 profiles.push_back(profiles[i]->GetOffTheRecordProfile());
126 for (size_t i = 0; i < profiles.size(); ++i) {
127 const extensions::ProcessManager::FrameSet all_frames =
128 extensions::ProcessManager::Get(profiles[i])->GetAllFrames();
129 for (content::RenderFrameHost* host : all_frames) {
130 WebContents* web_contents = WebContents::FromRenderFrameHost(host);
131 if (CheckOwnership(web_contents))
132 callback.Run(web_contents);
137 bool ExtensionInformation::CheckOwnership(WebContents* web_contents) {
138 // Don't add WebContents that belong to a guest (those are handled by
139 // GuestInformation). Otherwise they will be added twice.
140 if (guest_view::GuestViewBase::IsGuest(web_contents))
141 return false;
143 // Extension WebContents tracked by this class will always host extension
144 // content.
145 if (!GetExtensionForWebContents(web_contents))
146 return false;
148 extensions::ViewType view_type = extensions::GetViewType(web_contents);
150 // Don't add tab contents (those are handled by TabContentsResourceProvider)
151 // or background contents (handled by BackgroundInformation) or panels
152 // (handled by PanelInformation)
153 return (view_type != extensions::VIEW_TYPE_INVALID &&
154 view_type != extensions::VIEW_TYPE_TAB_CONTENTS &&
155 view_type != extensions::VIEW_TYPE_BACKGROUND_CONTENTS &&
156 view_type != extensions::VIEW_TYPE_PANEL);
159 scoped_ptr<RendererResource> ExtensionInformation::MakeResource(
160 WebContents* web_contents) {
161 const Extension* extension = GetExtensionForWebContents(web_contents);
162 if (!extension) {
163 NOTREACHED();
164 return scoped_ptr<RendererResource>();
166 return scoped_ptr<RendererResource>(new ExtensionProcessResource(
167 extension, web_contents->GetRenderViewHost()));
170 } // namespace task_manager