Make castv2 performance test work.
[chromium-blink-merge.git] / chrome / browser / task_manager / background_information.cc
blobadc88a2e14c141a64785709f49aa8a2b0322e560
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.h"
11 #include "chrome/browser/background/background_contents_service.h"
12 #include "chrome/browser/background/background_contents_service_factory.h"
13 #include "chrome/browser/browser_process.h"
14 #include "chrome/browser/chrome_notification_types.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/profiles/profile_manager.h"
17 #include "chrome/browser/task_manager/renderer_resource.h"
18 #include "chrome/browser/task_manager/resource_provider.h"
19 #include "chrome/browser/task_manager/task_manager.h"
20 #include "chrome/grit/generated_resources.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/extension_registry.h"
27 #include "extensions/browser/view_type_utils.h"
28 #include "extensions/common/extension.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 {
42 public:
43 BackgroundContentsResource(BackgroundContents* background_contents,
44 const base::string16& application_name);
45 ~BackgroundContentsResource() override;
47 // Resource methods:
48 base::string16 GetTitle() const override;
49 gfx::ImageSkia GetIcon() const override;
51 const base::string16& application_name() const { return application_name_; }
53 private:
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)
71 : RendererResource(
72 background_contents->web_contents()->GetRenderProcessHost()->
73 GetHandle(),
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.
79 if (!default_icon_) {
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_;
93 if (title.empty()) {
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();
137 ++iterator) {
138 callback.Run((*iterator)->web_contents());
143 scoped_ptr<RendererResource> BackgroundInformation::MakeResource(
144 WebContents* web_contents) {
145 Profile* profile =
146 Profile::FromBrowserContext(web_contents->GetBrowserContext());
147 const extensions::ExtensionSet& extensions_set =
148 extensions::ExtensionRegistry::Get(profile)->enabled_extensions();
149 BackgroundContentsService* background_contents_service =
150 BackgroundContentsServiceFactory::GetForProfile(profile);
151 std::vector<BackgroundContents*> contents =
152 background_contents_service->GetBackgroundContents();
153 for (std::vector<BackgroundContents*>::iterator iterator = contents.begin();
154 iterator != contents.end();
155 ++iterator) {
156 if ((*iterator)->web_contents() == web_contents) {
157 base::string16 application_name;
158 // Lookup the name from the parent extension.
159 const base::string16& application_id =
160 background_contents_service->GetParentApplicationId(*iterator);
161 const Extension* extension =
162 extensions_set.GetByID(base::UTF16ToUTF8(application_id));
163 if (extension)
164 application_name = base::UTF8ToUTF16(extension->name());
165 return scoped_ptr<RendererResource>(
166 new BackgroundContentsResource(*iterator, application_name));
169 NOTREACHED();
170 return scoped_ptr<RendererResource>();
173 } // namespace task_manager