| 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 <string> | |
| 8 | |
| 9 #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" | |
| 13 #include "chrome/browser/profiles/profile.h" | |
| 14 #include "chrome/browser/profiles/profile_info_cache.h" | |
| 15 #include "chrome/browser/profiles/profile_manager.h" | |
| 16 #include "chrome/browser/ui/panels/panel.h" | |
| 17 #include "chrome/browser/ui/panels/panel_manager.h" | |
| 18 #include "chrome/common/chrome_notification_types.h" | |
| 19 #include "chrome/common/extensions/extension.h" | |
| 20 #include "content/public/browser/notification_service.h" | |
| 21 #include "content/public/browser/render_process_host.h" | |
| 22 #include "content/public/browser/render_view_host.h" | |
| 23 #include "content/public/browser/web_contents.h" | |
| 24 #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" | |
| 28 | |
| 29 using content::RenderProcessHost; | |
| 30 using content::RenderViewHost; | |
| 31 using content::WebContents; | |
| 32 using extensions::Extension; | |
| 33 | |
| 34 namespace { | |
| 35 | |
| 36 // Returns the appropriate message prefix ID for tabs and extensions, | |
| 37 // reflecting whether they are apps or in incognito mode. | |
| 38 int GetMessagePrefixID(bool is_app, | |
| 39 bool is_extension, | |
| 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 | |
| 66 string16 GetProfileNameFromInfoCache(Profile* profile) { | |
| 67 ProfileInfoCache& cache = | |
| 68 g_browser_process->profile_manager()->GetProfileInfoCache(); | |
| 69 size_t index = cache.GetIndexOfProfileWithPath( | |
| 70 profile->GetOriginalProfile()->GetPath()); | |
| 71 if (index == std::string::npos) | |
| 72 return string16(); | |
| 73 else | |
| 74 return cache.GetNameOfProfileAtIndex(index); | |
| 75 } | |
| 76 | |
| 77 } // namespace | |
| 78 | |
| 79 //////////////////////////////////////////////////////////////////////////////// | |
| 80 // TaskManagerPanelResource class | |
| 81 //////////////////////////////////////////////////////////////////////////////// | |
| 82 | |
| 83 TaskManagerPanelResource::TaskManagerPanelResource(Panel* panel) | |
| 84 : TaskManagerRendererResource( | |
| 85 panel->GetWebContents()->GetRenderProcessHost()->GetHandle(), | |
| 86 panel->GetWebContents()->GetRenderViewHost()), | |
| 87 panel_(panel) { | |
| 88 message_prefix_id_ = GetMessagePrefixID( | |
| 89 GetExtension()->is_app(), true, panel->profile()->IsOffTheRecord(), | |
| 90 false, false, false); | |
| 91 } | |
| 92 | |
| 93 TaskManagerPanelResource::~TaskManagerPanelResource() { | |
| 94 } | |
| 95 | |
| 96 TaskManager::Resource::Type TaskManagerPanelResource::GetType() const { | |
| 97 return EXTENSION; | |
| 98 } | |
| 99 | |
| 100 string16 TaskManagerPanelResource::GetTitle() const { | |
| 101 string16 title = panel_->GetWindowTitle(); | |
| 102 // 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 | |
| 104 // strong RTL charater in it. Otherwise, if the task manager prefix is an | |
| 105 // RTL word, the concatenated result might be wrong. For example, | |
| 106 // 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 | |
| 108 // "!Yahoo! Mail: The best web-based Email :PPA", in which the capital | |
| 109 // letters "PPA" stands for the Hebrew word for "app". | |
| 110 base::i18n::AdjustStringForLocaleDirection(&title); | |
| 111 | |
| 112 return l10n_util::GetStringFUTF16(message_prefix_id_, title); | |
| 113 } | |
| 114 | |
| 115 string16 TaskManagerPanelResource::GetProfileName() const { | |
| 116 return GetProfileNameFromInfoCache(panel_->profile()); | |
| 117 } | |
| 118 | |
| 119 gfx::ImageSkia TaskManagerPanelResource::GetIcon() const { | |
| 120 gfx::Image icon = panel_->GetCurrentPageIcon(); | |
| 121 return icon.IsEmpty() ? gfx::ImageSkia() : *icon.ToImageSkia(); | |
| 122 } | |
| 123 | |
| 124 WebContents* TaskManagerPanelResource::GetWebContents() const { | |
| 125 return panel_->GetWebContents(); | |
| 126 } | |
| 127 | |
| 128 const Extension* TaskManagerPanelResource::GetExtension() const { | |
| 129 ExtensionService* extension_service = | |
| 130 panel_->profile()->GetExtensionService(); | |
| 131 return extension_service->extensions()->GetByID(panel_->extension_id()); | |
| 132 } | |
| 133 | |
| 134 //////////////////////////////////////////////////////////////////////////////// | |
| 135 // TaskManagerPanelResourceProvider class | |
| 136 //////////////////////////////////////////////////////////////////////////////// | |
| 137 | |
| 138 TaskManagerPanelResourceProvider::TaskManagerPanelResourceProvider( | |
| 139 TaskManager* task_manager) | |
| 140 : updating_(false), | |
| 141 task_manager_(task_manager) { | |
| 142 } | |
| 143 | |
| 144 TaskManagerPanelResourceProvider::~TaskManagerPanelResourceProvider() { | |
| 145 } | |
| 146 | |
| 147 TaskManager::Resource* TaskManagerPanelResourceProvider::GetResource( | |
| 148 int origin_pid, | |
| 149 int render_process_host_id, | |
| 150 int routing_id) { | |
| 151 // If an origin PID was specified, the request is from a plugin, not the | |
| 152 // render view host process | |
| 153 if (origin_pid) | |
| 154 return NULL; | |
| 155 | |
| 156 for (PanelResourceMap::iterator i = resources_.begin(); | |
| 157 i != resources_.end(); ++i) { | |
| 158 WebContents* contents = i->first->GetWebContents(); | |
| 159 if (contents && | |
| 160 contents->GetRenderProcessHost()->GetID() == render_process_host_id && | |
| 161 contents->GetRenderViewHost()->GetRoutingID() == routing_id) { | |
| 162 return i->second; | |
| 163 } | |
| 164 } | |
| 165 | |
| 166 // Can happen if the panel went away while a network request was being | |
| 167 // performed. | |
| 168 return NULL; | |
| 169 } | |
| 170 | |
| 171 void TaskManagerPanelResourceProvider::StartUpdating() { | |
| 172 DCHECK(!updating_); | |
| 173 updating_ = true; | |
| 174 | |
| 175 // Add all the Panels. | |
| 176 std::vector<Panel*> panels = PanelManager::GetInstance()->panels(); | |
| 177 for (size_t i = 0; i < panels.size(); ++i) | |
| 178 Add(panels[i]); | |
| 179 | |
| 180 // Then we register for notifications to get new and remove closed panels. | |
| 181 registrar_.Add(this, content::NOTIFICATION_WEB_CONTENTS_CONNECTED, | |
| 182 content::NotificationService::AllSources()); | |
| 183 registrar_.Add(this, content::NOTIFICATION_WEB_CONTENTS_DISCONNECTED, | |
| 184 content::NotificationService::AllSources()); | |
| 185 } | |
| 186 | |
| 187 void TaskManagerPanelResourceProvider::StopUpdating() { | |
| 188 DCHECK(updating_); | |
| 189 updating_ = false; | |
| 190 | |
| 191 // Unregister for notifications about new/removed panels. | |
| 192 registrar_.Remove(this, content::NOTIFICATION_WEB_CONTENTS_CONNECTED, | |
| 193 content::NotificationService::AllSources()); | |
| 194 registrar_.Remove(this, content::NOTIFICATION_WEB_CONTENTS_DISCONNECTED, | |
| 195 content::NotificationService::AllSources()); | |
| 196 | |
| 197 // Delete all the resources. | |
| 198 STLDeleteContainerPairSecondPointers(resources_.begin(), resources_.end()); | |
| 199 resources_.clear(); | |
| 200 } | |
| 201 | |
| 202 void TaskManagerPanelResourceProvider::Add(Panel* panel) { | |
| 203 if (!updating_) | |
| 204 return; | |
| 205 | |
| 206 PanelResourceMap::const_iterator iter = resources_.find(panel); | |
| 207 if (iter != resources_.end()) | |
| 208 return; | |
| 209 | |
| 210 TaskManagerPanelResource* resource = new TaskManagerPanelResource(panel); | |
| 211 resources_[panel] = resource; | |
| 212 task_manager_->AddResource(resource); | |
| 213 } | |
| 214 | |
| 215 void TaskManagerPanelResourceProvider::Remove(Panel* panel) { | |
| 216 if (!updating_) | |
| 217 return; | |
| 218 | |
| 219 PanelResourceMap::iterator iter = resources_.find(panel); | |
| 220 if (iter == resources_.end()) | |
| 221 return; | |
| 222 | |
| 223 TaskManagerPanelResource* resource = iter->second; | |
| 224 task_manager_->RemoveResource(resource); | |
| 225 resources_.erase(iter); | |
| 226 delete resource; | |
| 227 } | |
| 228 | |
| 229 void TaskManagerPanelResourceProvider::Observe(int type, | |
| 230 const content::NotificationSource& source, | |
| 231 const content::NotificationDetails& details) { | |
| 232 WebContents* web_contents = content::Source<WebContents>(source).ptr(); | |
| 233 if (extensions::GetViewType(web_contents) != extensions::VIEW_TYPE_PANEL) | |
| 234 return; | |
| 235 | |
| 236 switch (type) { | |
| 237 case content::NOTIFICATION_WEB_CONTENTS_CONNECTED: | |
| 238 { | |
| 239 std::vector<Panel*>panels = PanelManager::GetInstance()->panels(); | |
| 240 for (size_t i = 0; i < panels.size(); ++i) { | |
| 241 if (panels[i]->GetWebContents() == web_contents) { | |
| 242 Add(panels[i]); | |
| 243 break; | |
| 244 } | |
| 245 } | |
| 246 break; | |
| 247 } | |
| 248 case content::NOTIFICATION_WEB_CONTENTS_DISCONNECTED: | |
| 249 { | |
| 250 for (PanelResourceMap::iterator iter = resources_.begin(); | |
| 251 iter != resources_.end(); ++iter) { | |
| 252 Panel* panel = iter->first; | |
| 253 WebContents* panel_contents = panel->GetWebContents(); | |
| 254 if (!panel_contents || panel_contents == web_contents) { | |
| 255 Remove(panel); | |
| 256 break; | |
| 257 } | |
| 258 } | |
| 259 break; | |
| 260 } | |
| 261 default: | |
| 262 NOTREACHED() << "Unexpected notificiation."; | |
| 263 break; | |
| 264 } | |
| 265 } | |
| OLD | NEW |