OLD | NEW |
| (Empty) |
1 // Copyright 2013 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/task_manager_panel_resource_provider.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/task_manager_render_resource.h" | |
11 #include "chrome/browser/task_manager/task_manager_resource_util.h" | |
12 #include "chrome/browser/ui/panels/panel.h" | |
13 #include "chrome/browser/ui/panels/panel_manager.h" | |
14 #include "chrome/common/chrome_notification_types.h" | |
15 #include "chrome/common/extensions/extension.h" | |
16 #include "content/public/browser/notification_service.h" | |
17 #include "content/public/browser/render_process_host.h" | |
18 #include "content/public/browser/render_view_host.h" | |
19 #include "content/public/browser/web_contents.h" | |
20 #include "extensions/browser/view_type_utils.h" | |
21 #include "ui/base/l10n/l10n_util.h" | |
22 | |
23 using content::RenderProcessHost; | |
24 using content::RenderViewHost; | |
25 using content::WebContents; | |
26 using extensions::Extension; | |
27 | |
28 class TaskManagerPanelResource : public TaskManagerRendererResource { | |
29 public: | |
30 explicit TaskManagerPanelResource(Panel* panel); | |
31 virtual ~TaskManagerPanelResource(); | |
32 | |
33 // TaskManager::Resource methods: | |
34 virtual Type GetType() const OVERRIDE; | |
35 virtual string16 GetTitle() const OVERRIDE; | |
36 virtual string16 GetProfileName() const OVERRIDE; | |
37 virtual gfx::ImageSkia GetIcon() const OVERRIDE; | |
38 virtual content::WebContents* GetWebContents() const OVERRIDE; | |
39 virtual const extensions::Extension* GetExtension() const OVERRIDE; | |
40 | |
41 private: | |
42 Panel* panel_; | |
43 // Determines prefix for title reflecting whether extensions are apps | |
44 // or in incognito mode. | |
45 int message_prefix_id_; | |
46 | |
47 DISALLOW_COPY_AND_ASSIGN(TaskManagerPanelResource); | |
48 }; | |
49 | |
50 TaskManagerPanelResource::TaskManagerPanelResource(Panel* panel) | |
51 : TaskManagerRendererResource( | |
52 panel->GetWebContents()->GetRenderProcessHost()->GetHandle(), | |
53 panel->GetWebContents()->GetRenderViewHost()), | |
54 panel_(panel) { | |
55 message_prefix_id_ = TaskManagerResourceUtil::GetMessagePrefixID( | |
56 GetExtension()->is_app(), | |
57 true, // is_extension | |
58 panel->profile()->IsOffTheRecord(), | |
59 false, // is_prerender | |
60 false, // is_instant_overlay | |
61 false); // is_background | |
62 } | |
63 | |
64 TaskManagerPanelResource::~TaskManagerPanelResource() { | |
65 } | |
66 | |
67 TaskManager::Resource::Type TaskManagerPanelResource::GetType() const { | |
68 return EXTENSION; | |
69 } | |
70 | |
71 string16 TaskManagerPanelResource::GetTitle() const { | |
72 string16 title = panel_->GetWindowTitle(); | |
73 // Since the title will be concatenated with an IDS_TASK_MANAGER_* prefix | |
74 // we need to explicitly set the title to be LTR format if there is no | |
75 // strong RTL charater in it. Otherwise, if the task manager prefix is an | |
76 // RTL word, the concatenated result might be wrong. For example, | |
77 // a page whose title is "Yahoo! Mail: The best web-based Email!", without | |
78 // setting it explicitly as LTR format, the concatenated result will be | |
79 // "!Yahoo! Mail: The best web-based Email :PPA", in which the capital | |
80 // letters "PPA" stands for the Hebrew word for "app". | |
81 base::i18n::AdjustStringForLocaleDirection(&title); | |
82 | |
83 return l10n_util::GetStringFUTF16(message_prefix_id_, title); | |
84 } | |
85 | |
86 string16 TaskManagerPanelResource::GetProfileName() const { | |
87 return TaskManagerResourceUtil::GetProfileNameFromInfoCache( | |
88 panel_->profile()); | |
89 } | |
90 | |
91 gfx::ImageSkia TaskManagerPanelResource::GetIcon() const { | |
92 gfx::Image icon = panel_->GetCurrentPageIcon(); | |
93 return icon.IsEmpty() ? gfx::ImageSkia() : *icon.ToImageSkia(); | |
94 } | |
95 | |
96 WebContents* TaskManagerPanelResource::GetWebContents() const { | |
97 return panel_->GetWebContents(); | |
98 } | |
99 | |
100 const Extension* TaskManagerPanelResource::GetExtension() const { | |
101 ExtensionService* extension_service = | |
102 panel_->profile()->GetExtensionService(); | |
103 return extension_service->extensions()->GetByID(panel_->extension_id()); | |
104 } | |
105 | |
106 //////////////////////////////////////////////////////////////////////////////// | |
107 // TaskManagerPanelResourceProvider class | |
108 //////////////////////////////////////////////////////////////////////////////// | |
109 | |
110 TaskManagerPanelResourceProvider::TaskManagerPanelResourceProvider( | |
111 TaskManager* task_manager) | |
112 : updating_(false), | |
113 task_manager_(task_manager) { | |
114 } | |
115 | |
116 TaskManagerPanelResourceProvider::~TaskManagerPanelResourceProvider() { | |
117 } | |
118 | |
119 TaskManager::Resource* TaskManagerPanelResourceProvider::GetResource( | |
120 int origin_pid, | |
121 int render_process_host_id, | |
122 int routing_id) { | |
123 // If an origin PID was specified, the request is from a plugin, not the | |
124 // render view host process | |
125 if (origin_pid) | |
126 return NULL; | |
127 | |
128 for (PanelResourceMap::iterator i = resources_.begin(); | |
129 i != resources_.end(); ++i) { | |
130 WebContents* contents = i->first->GetWebContents(); | |
131 if (contents && | |
132 contents->GetRenderProcessHost()->GetID() == render_process_host_id && | |
133 contents->GetRenderViewHost()->GetRoutingID() == routing_id) { | |
134 return i->second; | |
135 } | |
136 } | |
137 | |
138 // Can happen if the panel went away while a network request was being | |
139 // performed. | |
140 return NULL; | |
141 } | |
142 | |
143 void TaskManagerPanelResourceProvider::StartUpdating() { | |
144 DCHECK(!updating_); | |
145 updating_ = true; | |
146 | |
147 // Add all the Panels. | |
148 std::vector<Panel*> panels = PanelManager::GetInstance()->panels(); | |
149 for (size_t i = 0; i < panels.size(); ++i) | |
150 Add(panels[i]); | |
151 | |
152 // Then we register for notifications to get new and remove closed panels. | |
153 registrar_.Add(this, content::NOTIFICATION_WEB_CONTENTS_CONNECTED, | |
154 content::NotificationService::AllSources()); | |
155 registrar_.Add(this, content::NOTIFICATION_WEB_CONTENTS_DISCONNECTED, | |
156 content::NotificationService::AllSources()); | |
157 } | |
158 | |
159 void TaskManagerPanelResourceProvider::StopUpdating() { | |
160 DCHECK(updating_); | |
161 updating_ = false; | |
162 | |
163 // Unregister for notifications about new/removed panels. | |
164 registrar_.Remove(this, content::NOTIFICATION_WEB_CONTENTS_CONNECTED, | |
165 content::NotificationService::AllSources()); | |
166 registrar_.Remove(this, content::NOTIFICATION_WEB_CONTENTS_DISCONNECTED, | |
167 content::NotificationService::AllSources()); | |
168 | |
169 // Delete all the resources. | |
170 STLDeleteContainerPairSecondPointers(resources_.begin(), resources_.end()); | |
171 resources_.clear(); | |
172 } | |
173 | |
174 void TaskManagerPanelResourceProvider::Add(Panel* panel) { | |
175 if (!updating_) | |
176 return; | |
177 | |
178 PanelResourceMap::const_iterator iter = resources_.find(panel); | |
179 if (iter != resources_.end()) | |
180 return; | |
181 | |
182 TaskManagerPanelResource* resource = new TaskManagerPanelResource(panel); | |
183 resources_[panel] = resource; | |
184 task_manager_->AddResource(resource); | |
185 } | |
186 | |
187 void TaskManagerPanelResourceProvider::Remove(Panel* panel) { | |
188 if (!updating_) | |
189 return; | |
190 | |
191 PanelResourceMap::iterator iter = resources_.find(panel); | |
192 if (iter == resources_.end()) | |
193 return; | |
194 | |
195 TaskManagerPanelResource* resource = iter->second; | |
196 task_manager_->RemoveResource(resource); | |
197 resources_.erase(iter); | |
198 delete resource; | |
199 } | |
200 | |
201 void TaskManagerPanelResourceProvider::Observe(int type, | |
202 const content::NotificationSource& source, | |
203 const content::NotificationDetails& details) { | |
204 WebContents* web_contents = content::Source<WebContents>(source).ptr(); | |
205 if (extensions::GetViewType(web_contents) != extensions::VIEW_TYPE_PANEL) | |
206 return; | |
207 | |
208 switch (type) { | |
209 case content::NOTIFICATION_WEB_CONTENTS_CONNECTED: | |
210 { | |
211 std::vector<Panel*>panels = PanelManager::GetInstance()->panels(); | |
212 for (size_t i = 0; i < panels.size(); ++i) { | |
213 if (panels[i]->GetWebContents() == web_contents) { | |
214 Add(panels[i]); | |
215 break; | |
216 } | |
217 } | |
218 break; | |
219 } | |
220 case content::NOTIFICATION_WEB_CONTENTS_DISCONNECTED: | |
221 { | |
222 for (PanelResourceMap::iterator iter = resources_.begin(); | |
223 iter != resources_.end(); ++iter) { | |
224 Panel* panel = iter->first; | |
225 WebContents* panel_contents = panel->GetWebContents(); | |
226 if (!panel_contents || panel_contents == web_contents) { | |
227 Remove(panel); | |
228 break; | |
229 } | |
230 } | |
231 break; | |
232 } | |
233 default: | |
234 NOTREACHED() << "Unexpected notificiation."; | |
235 break; | |
236 } | |
237 } | |
OLD | NEW |