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