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