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/background_information.h"
7 #include "base/i18n/rtl.h"
8 #include "base/strings/string16.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/browser/background/background_contents_service.h"
11 #include "chrome/browser/background/background_contents_service_factory.h"
12 #include "chrome/browser/browser_process.h"
13 #include "chrome/browser/chrome_notification_types.h"
14 #include "chrome/browser/extensions/extension_service.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/profiles/profile_manager.h"
17 #include "chrome/browser/tab_contents/background_contents.h"
18 #include "chrome/browser/task_manager/renderer_resource.h"
19 #include "chrome/browser/task_manager/resource_provider.h"
20 #include "chrome/browser/task_manager/task_manager.h"
21 #include "content/public/browser/notification_service.h"
22 #include "content/public/browser/render_frame_host.h"
23 #include "content/public/browser/render_process_host.h"
24 #include "content/public/browser/render_view_host.h"
25 #include "content/public/browser/web_contents.h"
26 #include "extensions/browser/view_type_utils.h"
27 #include "extensions/common/extension.h"
28 #include "grit/generated_resources.h"
29 #include "grit/theme_resources.h"
30 #include "ui/base/l10n/l10n_util.h"
31 #include "ui/base/resource/resource_bundle.h"
32 #include "ui/gfx/image/image_skia.h"
34 using content::RenderProcessHost
;
35 using content::RenderViewHost
;
36 using content::WebContents
;
37 using extensions::Extension
;
39 namespace task_manager
{
41 class BackgroundContentsResource
: public RendererResource
{
43 BackgroundContentsResource(BackgroundContents
* background_contents
,
44 const base::string16
& application_name
);
45 virtual ~BackgroundContentsResource();
48 virtual base::string16
GetTitle() const OVERRIDE
;
49 virtual gfx::ImageSkia
GetIcon() const OVERRIDE
;
51 const base::string16
& application_name() const { return application_name_
; }
54 BackgroundContents
* background_contents_
;
56 base::string16 application_name_
;
58 // The icon painted for BackgroundContents.
59 // TODO(atwilson): Use the favicon when there's a way to get the favicon for
60 // BackgroundContents.
61 static gfx::ImageSkia
* default_icon_
;
63 DISALLOW_COPY_AND_ASSIGN(BackgroundContentsResource
);
66 gfx::ImageSkia
* BackgroundContentsResource::default_icon_
= NULL
;
68 BackgroundContentsResource::BackgroundContentsResource(
69 BackgroundContents
* background_contents
,
70 const base::string16
& application_name
)
72 background_contents
->web_contents()->GetRenderProcessHost()->
74 background_contents
->web_contents()->GetRenderViewHost()),
75 background_contents_(background_contents
),
76 application_name_(application_name
) {
77 // Just use the same icon that other extension resources do.
78 // TODO(atwilson): Use the favicon when that's available.
80 ResourceBundle
& rb
= ResourceBundle::GetSharedInstance();
81 default_icon_
= rb
.GetImageSkiaNamed(IDR_PLUGINS_FAVICON
);
83 // Ensure that the string has the appropriate direction markers (see comment
84 // in TabContentsResource::GetTitle()).
85 base::i18n::AdjustStringForLocaleDirection(&application_name_
);
88 BackgroundContentsResource::~BackgroundContentsResource() {}
90 base::string16
BackgroundContentsResource::GetTitle() const {
91 base::string16 title
= application_name_
;
94 // No title (can't locate the parent app for some reason) so just display
95 // the URL (properly forced to be LTR).
96 title
= base::i18n::GetDisplayStringInLTRDirectionality(
97 base::UTF8ToUTF16(background_contents_
->GetURL().spec()));
99 return l10n_util::GetStringFUTF16(IDS_TASK_MANAGER_BACKGROUND_PREFIX
, title
);
102 gfx::ImageSkia
BackgroundContentsResource::GetIcon() const {
103 return *default_icon_
;
106 ////////////////////////////////////////////////////////////////////////////////
107 // BackgroundInformation class
108 ////////////////////////////////////////////////////////////////////////////////
110 BackgroundInformation::BackgroundInformation() {}
112 BackgroundInformation::~BackgroundInformation() {}
114 bool BackgroundInformation::CheckOwnership(WebContents
* web_contents
) {
115 extensions::ViewType view_type
= extensions::GetViewType(web_contents
);
116 return view_type
== extensions::VIEW_TYPE_BACKGROUND_CONTENTS
;
119 void BackgroundInformation::GetAll(const NewWebContentsCallback
& callback
) {
120 // Add all the existing BackgroundContents from every profile, including
121 // incognito profiles.
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());
130 for (size_t i
= 0; i
< profiles
.size(); ++i
) {
131 BackgroundContentsService
* background_contents_service
=
132 BackgroundContentsServiceFactory::GetForProfile(profiles
[i
]);
133 std::vector
<BackgroundContents
*> contents
=
134 background_contents_service
->GetBackgroundContents();
135 for (std::vector
<BackgroundContents
*>::iterator iterator
= contents
.begin();
136 iterator
!= contents
.end();
138 callback
.Run((*iterator
)->web_contents());
143 scoped_ptr
<RendererResource
> BackgroundInformation::MakeResource(
144 WebContents
* web_contents
) {
146 Profile::FromBrowserContext(web_contents
->GetBrowserContext());
147 ExtensionService
* extension_service
= profile
->GetExtensionService();
148 BackgroundContentsService
* background_contents_service
=
149 BackgroundContentsServiceFactory::GetForProfile(profile
);
150 std::vector
<BackgroundContents
*> contents
=
151 background_contents_service
->GetBackgroundContents();
152 for (std::vector
<BackgroundContents
*>::iterator iterator
= contents
.begin();
153 iterator
!= contents
.end();
155 if ((*iterator
)->web_contents() == web_contents
) {
156 base::string16 application_name
;
157 // Lookup the name from the parent extension.
158 if (extension_service
) {
159 const base::string16
& application_id
=
160 background_contents_service
->GetParentApplicationId(*iterator
);
161 const Extension
* extension
= extension_service
->GetExtensionById(
162 base::UTF16ToUTF8(application_id
), false);
164 application_name
= base::UTF8ToUTF16(extension
->name());
166 return scoped_ptr
<RendererResource
>(
167 new BackgroundContentsResource(*iterator
, application_name
));
171 return scoped_ptr
<RendererResource
>();
174 } // namespace task_manager