[email protected] | 7efaed2 | 2013-05-13 15:18:07 | [diff] [blame] | 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 | |
[email protected] | 44a41aa | 2013-05-20 11:49:22 | [diff] [blame] | 5 | #include "chrome/browser/task_manager/guest_resource_provider.h" |
[email protected] | 7efaed2 | 2013-05-13 15:18:07 | [diff] [blame] | 6 | |
[email protected] | 9f0abdb | 2013-06-10 21:49:34 | [diff] [blame] | 7 | #include "base/strings/string16.h" |
[email protected] | 7efaed2 | 2013-05-13 15:18:07 | [diff] [blame] | 8 | #include "chrome/browser/favicon/favicon_tab_helper.h" |
| 9 | #include "chrome/browser/profiles/profile.h" |
[email protected] | 44a41aa | 2013-05-20 11:49:22 | [diff] [blame] | 10 | #include "chrome/browser/task_manager/renderer_resource.h" |
[email protected] | b18943a | 2013-05-23 15:59:15 | [diff] [blame] | 11 | #include "chrome/browser/task_manager/resource_provider.h" |
| 12 | #include "chrome/browser/task_manager/task_manager.h" |
[email protected] | 44a41aa | 2013-05-20 11:49:22 | [diff] [blame] | 13 | #include "chrome/browser/task_manager/task_manager_util.h" |
[email protected] | 7efaed2 | 2013-05-13 15:18:07 | [diff] [blame] | 14 | #include "chrome/common/chrome_notification_types.h" |
| 15 | #include "content/public/browser/notification_service.h" |
| 16 | #include "content/public/browser/render_process_host.h" |
| 17 | #include "content/public/browser/render_view_host.h" |
| 18 | #include "content/public/browser/site_instance.h" |
| 19 | #include "content/public/browser/web_contents.h" |
| 20 | #include "grit/generated_resources.h" |
| 21 | #include "ui/base/l10n/l10n_util.h" |
[email protected] | 299e454 | 2013-05-16 03:09:06 | [diff] [blame] | 22 | #include "ui/gfx/image/image.h" |
[email protected] | 6a9d9d7 | 2013-05-15 15:49:09 | [diff] [blame] | 23 | #include "ui/gfx/image/image_skia.h" |
[email protected] | 7efaed2 | 2013-05-13 15:18:07 | [diff] [blame] | 24 | |
| 25 | using content::RenderProcessHost; |
| 26 | using content::RenderViewHost; |
| 27 | using content::RenderWidgetHost; |
| 28 | using content::WebContents; |
| 29 | using extensions::Extension; |
| 30 | |
[email protected] | 44a41aa | 2013-05-20 11:49:22 | [diff] [blame] | 31 | namespace task_manager { |
| 32 | |
| 33 | class GuestResource : public RendererResource { |
[email protected] | 6a9d9d7 | 2013-05-15 15:49:09 | [diff] [blame] | 34 | public: |
[email protected] | 44a41aa | 2013-05-20 11:49:22 | [diff] [blame] | 35 | explicit GuestResource(content::RenderViewHost* render_view_host); |
| 36 | virtual ~GuestResource(); |
[email protected] | 7efaed2 | 2013-05-13 15:18:07 | [diff] [blame] | 37 | |
[email protected] | b18943a | 2013-05-23 15:59:15 | [diff] [blame] | 38 | // Resource methods: |
[email protected] | 6a9d9d7 | 2013-05-15 15:49:09 | [diff] [blame] | 39 | virtual Type GetType() const OVERRIDE; |
| 40 | virtual string16 GetTitle() const OVERRIDE; |
| 41 | virtual string16 GetProfileName() const OVERRIDE; |
| 42 | virtual gfx::ImageSkia GetIcon() const OVERRIDE; |
| 43 | virtual content::WebContents* GetWebContents() const OVERRIDE; |
| 44 | virtual const extensions::Extension* GetExtension() const OVERRIDE; |
[email protected] | 7efaed2 | 2013-05-13 15:18:07 | [diff] [blame] | 45 | |
[email protected] | 6a9d9d7 | 2013-05-15 15:49:09 | [diff] [blame] | 46 | private: |
[email protected] | 44a41aa | 2013-05-20 11:49:22 | [diff] [blame] | 47 | DISALLOW_COPY_AND_ASSIGN(GuestResource); |
[email protected] | 6a9d9d7 | 2013-05-15 15:49:09 | [diff] [blame] | 48 | }; |
[email protected] | 7efaed2 | 2013-05-13 15:18:07 | [diff] [blame] | 49 | |
[email protected] | 44a41aa | 2013-05-20 11:49:22 | [diff] [blame] | 50 | GuestResource::GuestResource(RenderViewHost* render_view_host) |
| 51 | : RendererResource( |
[email protected] | 7efaed2 | 2013-05-13 15:18:07 | [diff] [blame] | 52 | render_view_host->GetSiteInstance()->GetProcess()->GetHandle(), |
| 53 | render_view_host) { |
| 54 | } |
| 55 | |
[email protected] | 44a41aa | 2013-05-20 11:49:22 | [diff] [blame] | 56 | GuestResource::~GuestResource() { |
[email protected] | 7efaed2 | 2013-05-13 15:18:07 | [diff] [blame] | 57 | } |
| 58 | |
[email protected] | b18943a | 2013-05-23 15:59:15 | [diff] [blame] | 59 | Resource::Type GuestResource::GetType() const { |
[email protected] | 7efaed2 | 2013-05-13 15:18:07 | [diff] [blame] | 60 | return GUEST; |
| 61 | } |
| 62 | |
[email protected] | 44a41aa | 2013-05-20 11:49:22 | [diff] [blame] | 63 | string16 GuestResource::GetTitle() const { |
[email protected] | 7efaed2 | 2013-05-13 15:18:07 | [diff] [blame] | 64 | WebContents* web_contents = GetWebContents(); |
| 65 | const int message_id = IDS_TASK_MANAGER_WEBVIEW_TAG_PREFIX; |
| 66 | if (web_contents) { |
[email protected] | 44a41aa | 2013-05-20 11:49:22 | [diff] [blame] | 67 | string16 title = util::GetTitleFromWebContents(web_contents); |
[email protected] | 7efaed2 | 2013-05-13 15:18:07 | [diff] [blame] | 68 | return l10n_util::GetStringFUTF16(message_id, title); |
| 69 | } |
| 70 | return l10n_util::GetStringFUTF16(message_id, string16()); |
| 71 | } |
| 72 | |
[email protected] | 44a41aa | 2013-05-20 11:49:22 | [diff] [blame] | 73 | string16 GuestResource::GetProfileName() const { |
[email protected] | 7efaed2 | 2013-05-13 15:18:07 | [diff] [blame] | 74 | WebContents* web_contents = GetWebContents(); |
| 75 | if (web_contents) { |
| 76 | Profile* profile = Profile::FromBrowserContext( |
| 77 | web_contents->GetBrowserContext()); |
[email protected] | 44a41aa | 2013-05-20 11:49:22 | [diff] [blame] | 78 | return util::GetProfileNameFromInfoCache(profile); |
[email protected] | 7efaed2 | 2013-05-13 15:18:07 | [diff] [blame] | 79 | } |
| 80 | return string16(); |
| 81 | } |
| 82 | |
[email protected] | 44a41aa | 2013-05-20 11:49:22 | [diff] [blame] | 83 | gfx::ImageSkia GuestResource::GetIcon() const { |
[email protected] | 7efaed2 | 2013-05-13 15:18:07 | [diff] [blame] | 84 | WebContents* web_contents = GetWebContents(); |
| 85 | if (web_contents && FaviconTabHelper::FromWebContents(web_contents)) { |
| 86 | return FaviconTabHelper::FromWebContents(web_contents)-> |
| 87 | GetFavicon().AsImageSkia(); |
| 88 | } |
| 89 | return gfx::ImageSkia(); |
| 90 | } |
| 91 | |
[email protected] | 44a41aa | 2013-05-20 11:49:22 | [diff] [blame] | 92 | WebContents* GuestResource::GetWebContents() const { |
[email protected] | 7efaed2 | 2013-05-13 15:18:07 | [diff] [blame] | 93 | return WebContents::FromRenderViewHost(render_view_host()); |
| 94 | } |
| 95 | |
[email protected] | 44a41aa | 2013-05-20 11:49:22 | [diff] [blame] | 96 | const Extension* GuestResource::GetExtension() const { |
[email protected] | 7efaed2 | 2013-05-13 15:18:07 | [diff] [blame] | 97 | return NULL; |
| 98 | } |
| 99 | |
[email protected] | 44a41aa | 2013-05-20 11:49:22 | [diff] [blame] | 100 | GuestResourceProvider::GuestResourceProvider(TaskManager* task_manager) |
[email protected] | 7efaed2 | 2013-05-13 15:18:07 | [diff] [blame] | 101 | : updating_(false), |
| 102 | task_manager_(task_manager) { |
| 103 | } |
| 104 | |
[email protected] | 44a41aa | 2013-05-20 11:49:22 | [diff] [blame] | 105 | GuestResourceProvider::~GuestResourceProvider() { |
[email protected] | 7efaed2 | 2013-05-13 15:18:07 | [diff] [blame] | 106 | } |
| 107 | |
[email protected] | b18943a | 2013-05-23 15:59:15 | [diff] [blame] | 108 | Resource* GuestResourceProvider::GetResource( |
[email protected] | 7efaed2 | 2013-05-13 15:18:07 | [diff] [blame] | 109 | int origin_pid, |
| 110 | int render_process_host_id, |
| 111 | int routing_id) { |
| 112 | // If an origin PID was specified then the request originated in a plugin |
| 113 | // working on the WebContents's behalf, so ignore it. |
| 114 | if (origin_pid) |
| 115 | return NULL; |
| 116 | |
| 117 | for (GuestResourceMap::iterator i = resources_.begin(); |
| 118 | i != resources_.end(); ++i) { |
| 119 | WebContents* contents = WebContents::FromRenderViewHost(i->first); |
| 120 | if (contents && |
| 121 | contents->GetRenderProcessHost()->GetID() == render_process_host_id && |
| 122 | contents->GetRenderViewHost()->GetRoutingID() == routing_id) { |
| 123 | return i->second; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | return NULL; |
| 128 | } |
| 129 | |
[email protected] | 44a41aa | 2013-05-20 11:49:22 | [diff] [blame] | 130 | void GuestResourceProvider::StartUpdating() { |
[email protected] | 7efaed2 | 2013-05-13 15:18:07 | [diff] [blame] | 131 | DCHECK(!updating_); |
| 132 | updating_ = true; |
| 133 | |
| 134 | // Add all the existing guest WebContents. |
[email protected] | 039b84a4 | 2013-06-21 20:23:37 | [diff] [blame] | 135 | RenderWidgetHost::List widgets = RenderWidgetHost::GetRenderWidgetHosts(); |
| 136 | for (size_t i = 0; i < widgets.size(); ++i) { |
| 137 | if (widgets[i]->IsRenderView()) { |
| 138 | RenderViewHost* rvh = RenderViewHost::From(widgets[i]); |
| 139 | if (rvh->IsSubframe()) |
| 140 | Add(rvh); |
[email protected] | 7efaed2 | 2013-05-13 15:18:07 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | |
| 144 | // Then we register for notifications to get new guests. |
| 145 | registrar_.Add(this, content::NOTIFICATION_WEB_CONTENTS_CONNECTED, |
| 146 | content::NotificationService::AllBrowserContextsAndSources()); |
| 147 | registrar_.Add(this, content::NOTIFICATION_WEB_CONTENTS_DISCONNECTED, |
| 148 | content::NotificationService::AllBrowserContextsAndSources()); |
| 149 | } |
| 150 | |
[email protected] | 44a41aa | 2013-05-20 11:49:22 | [diff] [blame] | 151 | void GuestResourceProvider::StopUpdating() { |
[email protected] | 7efaed2 | 2013-05-13 15:18:07 | [diff] [blame] | 152 | DCHECK(updating_); |
| 153 | updating_ = false; |
| 154 | |
| 155 | // Unregister for notifications. |
| 156 | registrar_.Remove(this, content::NOTIFICATION_WEB_CONTENTS_CONNECTED, |
| 157 | content::NotificationService::AllBrowserContextsAndSources()); |
| 158 | registrar_.Remove(this, content::NOTIFICATION_WEB_CONTENTS_DISCONNECTED, |
| 159 | content::NotificationService::AllBrowserContextsAndSources()); |
| 160 | |
| 161 | // Delete all the resources. |
| 162 | STLDeleteContainerPairSecondPointers(resources_.begin(), resources_.end()); |
| 163 | |
| 164 | resources_.clear(); |
| 165 | } |
| 166 | |
[email protected] | 44a41aa | 2013-05-20 11:49:22 | [diff] [blame] | 167 | void GuestResourceProvider::Add(RenderViewHost* render_view_host) { |
| 168 | GuestResource* resource = new GuestResource(render_view_host); |
[email protected] | 7efaed2 | 2013-05-13 15:18:07 | [diff] [blame] | 169 | resources_[render_view_host] = resource; |
| 170 | task_manager_->AddResource(resource); |
| 171 | } |
| 172 | |
[email protected] | 44a41aa | 2013-05-20 11:49:22 | [diff] [blame] | 173 | void GuestResourceProvider::Remove(RenderViewHost* render_view_host) { |
[email protected] | 7efaed2 | 2013-05-13 15:18:07 | [diff] [blame] | 174 | if (!updating_) |
| 175 | return; |
| 176 | |
| 177 | GuestResourceMap::iterator iter = resources_.find(render_view_host); |
| 178 | if (iter == resources_.end()) |
| 179 | return; |
| 180 | |
[email protected] | 44a41aa | 2013-05-20 11:49:22 | [diff] [blame] | 181 | GuestResource* resource = iter->second; |
[email protected] | 7efaed2 | 2013-05-13 15:18:07 | [diff] [blame] | 182 | task_manager_->RemoveResource(resource); |
| 183 | resources_.erase(iter); |
| 184 | delete resource; |
| 185 | } |
| 186 | |
[email protected] | 44a41aa | 2013-05-20 11:49:22 | [diff] [blame] | 187 | void GuestResourceProvider::Observe(int type, |
[email protected] | 7efaed2 | 2013-05-13 15:18:07 | [diff] [blame] | 188 | const content::NotificationSource& source, |
| 189 | const content::NotificationDetails& details) { |
| 190 | WebContents* web_contents = content::Source<WebContents>(source).ptr(); |
| 191 | if (!web_contents || !web_contents->GetRenderViewHost()->IsSubframe()) |
| 192 | return; |
| 193 | |
| 194 | switch (type) { |
| 195 | case content::NOTIFICATION_WEB_CONTENTS_CONNECTED: |
| 196 | Add(web_contents->GetRenderViewHost()); |
| 197 | break; |
| 198 | case content::NOTIFICATION_WEB_CONTENTS_DISCONNECTED: |
| 199 | Remove(web_contents->GetRenderViewHost()); |
| 200 | break; |
| 201 | default: |
| 202 | NOTREACHED() << "Unexpected notification."; |
| 203 | } |
| 204 | } |
[email protected] | 44a41aa | 2013-05-20 11:49:22 | [diff] [blame] | 205 | |
| 206 | } // namespace task_manager |