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/panel_information.h"
7 #include "base/i18n/rtl.h"
8 #include "chrome/browser/extensions/extension_service.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/task_manager/renderer_resource.h"
11 #include "chrome/browser/task_manager/task_manager_util.h"
12 #include "chrome/browser/ui/panels/panel.h"
13 #include "chrome/browser/ui/panels/panel_manager.h"
14 #include "content/public/browser/notification_service.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/view_type_utils.h"
20 #include "extensions/common/extension.h"
21 #include "ui/base/l10n/l10n_util.h"
22 #include "ui/gfx/image/image_skia.h"
24 using content::RenderProcessHost
;
25 using content::RenderViewHost
;
26 using content::WebContents
;
27 using extensions::Extension
;
29 namespace task_manager
{
31 class PanelResource
: public RendererResource
{
33 explicit PanelResource(Panel
* panel
);
34 virtual ~PanelResource();
37 virtual Type
GetType() const OVERRIDE
;
38 virtual base::string16
GetTitle() const OVERRIDE
;
39 virtual gfx::ImageSkia
GetIcon() const OVERRIDE
;
40 virtual content::WebContents
* GetWebContents() const OVERRIDE
;
44 // Determines prefix for title reflecting whether extensions are apps
45 // or in incognito mode.
46 int message_prefix_id_
;
48 DISALLOW_COPY_AND_ASSIGN(PanelResource
);
51 PanelResource::PanelResource(Panel
* panel
)
53 panel
->GetWebContents()->GetRenderProcessHost()->GetHandle(),
54 panel
->GetWebContents()->GetRenderViewHost()),
56 ExtensionService
* service
= panel_
->profile()->GetExtensionService();
57 message_prefix_id_
= util::GetMessagePrefixID(
58 service
->extensions()->GetByID(panel_
->extension_id())->is_app(),
60 panel_
->profile()->IsOffTheRecord(),
61 false, // is_prerender
62 false); // is_background
65 PanelResource::~PanelResource() {
68 Resource::Type
PanelResource::GetType() const {
72 base::string16
PanelResource::GetTitle() const {
73 base::string16 title
= panel_
->GetWindowTitle();
74 // Since the title will be concatenated with an IDS_TASK_MANAGER_* prefix
75 // we need to explicitly set the title to be LTR format if there is no
76 // strong RTL charater in it. Otherwise, if the task manager prefix is an
77 // RTL word, the concatenated result might be wrong. For example,
78 // a page whose title is "Yahoo! Mail: The best web-based Email!", without
79 // setting it explicitly as LTR format, the concatenated result will be
80 // "!Yahoo! Mail: The best web-based Email :PPA", in which the capital
81 // letters "PPA" stands for the Hebrew word for "app".
82 base::i18n::AdjustStringForLocaleDirection(&title
);
84 return l10n_util::GetStringFUTF16(message_prefix_id_
, title
);
87 gfx::ImageSkia
PanelResource::GetIcon() const {
88 gfx::Image icon
= panel_
->GetCurrentPageIcon();
89 return icon
.IsEmpty() ? gfx::ImageSkia() : *icon
.ToImageSkia();
92 WebContents
* PanelResource::GetWebContents() const {
93 return panel_
->GetWebContents();
96 ////////////////////////////////////////////////////////////////////////////////
97 // PanelInformation class
98 ////////////////////////////////////////////////////////////////////////////////
100 PanelInformation::PanelInformation() {}
102 PanelInformation::~PanelInformation() {}
104 bool PanelInformation::CheckOwnership(WebContents
* web_contents
) {
105 std::vector
<Panel
*> panels
= PanelManager::GetInstance()->panels();
106 for (size_t i
= 0; i
< panels
.size(); ++i
) {
107 if (panels
[i
]->GetWebContents() == web_contents
) {
114 void PanelInformation::GetAll(const NewWebContentsCallback
& callback
) {
115 // Add all the Panels.
116 std::vector
<Panel
*> panels
= PanelManager::GetInstance()->panels();
117 for (size_t i
= 0; i
< panels
.size(); ++i
)
118 callback
.Run(panels
[i
]->GetWebContents());
121 scoped_ptr
<RendererResource
> PanelInformation::MakeResource(
122 WebContents
* web_contents
) {
123 std::vector
<Panel
*> panels
= PanelManager::GetInstance()->panels();
124 for (size_t i
= 0; i
< panels
.size(); ++i
) {
125 if (panels
[i
]->GetWebContents() == web_contents
) {
126 return scoped_ptr
<RendererResource
>(new PanelResource(panels
[i
]));
130 return scoped_ptr
<RendererResource
>();
133 } // namespace task_manager