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.
4
5#include "chrome/browser/task_manager/panel_information.h"
6
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/extension_registry.h"
20#include "extensions/browser/view_type_utils.h"
21#include "extensions/common/extension.h"
22#include "ui/base/l10n/l10n_util.h"
23#include "ui/gfx/image/image_skia.h"
24
25using content::RenderProcessHost;
26using content::RenderViewHost;
27using content::WebContents;
28using extensions::Extension;
29
30namespace task_manager {
31
32class PanelResource : public RendererResource {
33 public:
34  explicit PanelResource(Panel* panel);
35  virtual ~PanelResource();
36
37  // Resource methods:
38  virtual Type GetType() const OVERRIDE;
39  virtual base::string16 GetTitle() const OVERRIDE;
40  virtual gfx::ImageSkia GetIcon() const OVERRIDE;
41  virtual content::WebContents* GetWebContents() const OVERRIDE;
42
43 private:
44  Panel* panel_;
45  // Determines prefix for title reflecting whether extensions are apps
46  // or in incognito mode.
47  int message_prefix_id_;
48
49  DISALLOW_COPY_AND_ASSIGN(PanelResource);
50};
51
52PanelResource::PanelResource(Panel* panel)
53    : RendererResource(
54        panel->GetWebContents()->GetRenderProcessHost()->GetHandle(),
55        panel->GetWebContents()->GetRenderViewHost()),
56      panel_(panel) {
57  extensions::ExtensionRegistry* registry =
58      extensions::ExtensionRegistry::Get(panel_->profile());
59  message_prefix_id_ = util::GetMessagePrefixID(
60      registry->enabled_extensions().GetByID(panel_->extension_id())->is_app(),
61      true,  // is_extension
62      panel_->profile()->IsOffTheRecord(),
63      false,   // is_prerender
64      false);  // is_background
65}
66
67PanelResource::~PanelResource() {
68}
69
70Resource::Type PanelResource::GetType() const {
71  return EXTENSION;
72}
73
74base::string16 PanelResource::GetTitle() const {
75  base::string16 title = panel_->GetWindowTitle();
76  // Since the title will be concatenated with an IDS_TASK_MANAGER_* prefix
77  // we need to explicitly set the title to be LTR format if there is no
78  // strong RTL charater in it. Otherwise, if the task manager prefix is an
79  // RTL word, the concatenated result might be wrong. For example,
80  // a page whose title is "Yahoo! Mail: The best web-based Email!", without
81  // setting it explicitly as LTR format, the concatenated result will be
82  // "!Yahoo! Mail: The best web-based Email :PPA", in which the capital
83  // letters "PPA" stands for the Hebrew word for "app".
84  base::i18n::AdjustStringForLocaleDirection(&title);
85
86  return l10n_util::GetStringFUTF16(message_prefix_id_, title);
87}
88
89gfx::ImageSkia PanelResource::GetIcon() const {
90  gfx::Image icon = panel_->GetCurrentPageIcon();
91  return icon.IsEmpty() ? gfx::ImageSkia() : *icon.ToImageSkia();
92}
93
94WebContents* PanelResource::GetWebContents() const {
95  return panel_->GetWebContents();
96}
97
98////////////////////////////////////////////////////////////////////////////////
99// PanelInformation class
100////////////////////////////////////////////////////////////////////////////////
101
102PanelInformation::PanelInformation() {}
103
104PanelInformation::~PanelInformation() {}
105
106bool PanelInformation::CheckOwnership(WebContents* web_contents) {
107  std::vector<Panel*> panels = PanelManager::GetInstance()->panels();
108  for (size_t i = 0; i < panels.size(); ++i) {
109    if (panels[i]->GetWebContents() == web_contents) {
110      return true;
111    }
112  }
113  return false;
114}
115
116void PanelInformation::GetAll(const NewWebContentsCallback& callback) {
117  // Add all the Panels.
118  std::vector<Panel*> panels = PanelManager::GetInstance()->panels();
119  for (size_t i = 0; i < panels.size(); ++i)
120    callback.Run(panels[i]->GetWebContents());
121}
122
123scoped_ptr<RendererResource> PanelInformation::MakeResource(
124    WebContents* web_contents) {
125  std::vector<Panel*> panels = PanelManager::GetInstance()->panels();
126  for (size_t i = 0; i < panels.size(); ++i) {
127    if (panels[i]->GetWebContents() == web_contents) {
128      return scoped_ptr<RendererResource>(new PanelResource(panels[i]));
129    }
130  }
131  NOTREACHED();
132  return scoped_ptr<RendererResource>();
133}
134
135}  // namespace task_manager
136