Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(62)

Side by Side Diff: chrome/browser/task_manager/task_manager_guest_resource_provider.cc

Issue 15196003: Create task_manager namespace and wrap classes related to TaskManager with it. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: RendererResource Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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_guest_resource_provider.h"
6
7 #include <string>
8
9 #include "base/basictypes.h"
10 #include "base/i18n/rtl.h"
11 #include "base/string16.h"
12 #include "base/utf_string_conversions.h"
13 #include "chrome/browser/browser_process.h"
14 #include "chrome/browser/favicon/favicon_tab_helper.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/profiles/profile_info_cache.h"
17 #include "chrome/browser/profiles/profile_manager.h"
18 #include "chrome/common/chrome_notification_types.h"
19 #include "content/public/browser/notification_service.h"
20 #include "content/public/browser/render_process_host.h"
21 #include "content/public/browser/render_view_host.h"
22 #include "content/public/browser/site_instance.h"
23 #include "content/public/browser/web_contents.h"
24 #include "grit/generated_resources.h"
25 #include "ui/base/l10n/l10n_util.h"
26
27 using content::RenderProcessHost;
28 using content::RenderViewHost;
29 using content::RenderWidgetHost;
30 using content::WebContents;
31 using extensions::Extension;
32
33 namespace {
34
35 string16 GetProfileNameFromInfoCache(Profile* profile) {
36 ProfileInfoCache& cache =
37 g_browser_process->profile_manager()->GetProfileInfoCache();
38 size_t index = cache.GetIndexOfProfileWithPath(
39 profile->GetOriginalProfile()->GetPath());
40 if (index == std::string::npos)
41 return string16();
42 else
43 return cache.GetNameOfProfileAtIndex(index);
44 }
45
46 string16 GetTitleFromWebContents(WebContents* web_contents) {
47 string16 title = web_contents->GetTitle();
48 if (title.empty()) {
49 GURL url = web_contents->GetURL();
50 title = UTF8ToUTF16(url.spec());
51 // Force URL to be LTR.
52 title = base::i18n::GetDisplayStringInLTRDirectionality(title);
53 } else {
54 // Since the tab_title will be concatenated with
55 // IDS_TASK_MANAGER_TAB_PREFIX, we need to explicitly set the tab_title to
56 // be LTR format if there is no strong RTL charater in it. Otherwise, if
57 // IDS_TASK_MANAGER_TAB_PREFIX is an RTL word, the concatenated result
58 // might be wrong. For example, http://mail.yahoo.com, whose title is
59 // "Yahoo! Mail: The best web-based Email!", without setting it explicitly
60 // as LTR format, the concatenated result will be "!Yahoo! Mail: The best
61 // web-based Email :BAT", in which the capital letters "BAT" stands for
62 // the Hebrew word for "tab".
63 base::i18n::AdjustStringForLocaleDirection(&title);
64 }
65 return title;
66 }
67
68 } // namespace
69
70 ////////////////////////////////////////////////////////////////////////////////
71 // TaskManagerGuestResource class
72 ////////////////////////////////////////////////////////////////////////////////
73
74 TaskManagerGuestResource::TaskManagerGuestResource(
75 RenderViewHost* render_view_host)
76 : TaskManagerRendererResource(
77 render_view_host->GetSiteInstance()->GetProcess()->GetHandle(),
78 render_view_host) {
79 }
80
81 TaskManagerGuestResource::~TaskManagerGuestResource() {
82 }
83
84 TaskManager::Resource::Type TaskManagerGuestResource::GetType() const {
85 return GUEST;
86 }
87
88 string16 TaskManagerGuestResource::GetTitle() const {
89 WebContents* web_contents = GetWebContents();
90 const int message_id = IDS_TASK_MANAGER_WEBVIEW_TAG_PREFIX;
91 if (web_contents) {
92 string16 title = GetTitleFromWebContents(web_contents);
93 return l10n_util::GetStringFUTF16(message_id, title);
94 }
95 return l10n_util::GetStringFUTF16(message_id, string16());
96 }
97
98 string16 TaskManagerGuestResource::GetProfileName() const {
99 WebContents* web_contents = GetWebContents();
100 if (web_contents) {
101 Profile* profile = Profile::FromBrowserContext(
102 web_contents->GetBrowserContext());
103 return GetProfileNameFromInfoCache(profile);
104 }
105 return string16();
106 }
107
108 gfx::ImageSkia TaskManagerGuestResource::GetIcon() const {
109 WebContents* web_contents = GetWebContents();
110 if (web_contents && FaviconTabHelper::FromWebContents(web_contents)) {
111 return FaviconTabHelper::FromWebContents(web_contents)->
112 GetFavicon().AsImageSkia();
113 }
114 return gfx::ImageSkia();
115 }
116
117 WebContents* TaskManagerGuestResource::GetWebContents() const {
118 return WebContents::FromRenderViewHost(render_view_host());
119 }
120
121 const Extension* TaskManagerGuestResource::GetExtension() const {
122 return NULL;
123 }
124
125 ////////////////////////////////////////////////////////////////////////////////
126 // TaskManagerGuestContentsResourceProvider class
127 ////////////////////////////////////////////////////////////////////////////////
128
129 TaskManagerGuestResourceProvider::
130 TaskManagerGuestResourceProvider(TaskManager* task_manager)
131 : updating_(false),
132 task_manager_(task_manager) {
133 }
134
135 TaskManagerGuestResourceProvider::~TaskManagerGuestResourceProvider() {
136 }
137
138 TaskManager::Resource* TaskManagerGuestResourceProvider::GetResource(
139 int origin_pid,
140 int render_process_host_id,
141 int routing_id) {
142 // If an origin PID was specified then the request originated in a plugin
143 // working on the WebContents's behalf, so ignore it.
144 if (origin_pid)
145 return NULL;
146
147 for (GuestResourceMap::iterator i = resources_.begin();
148 i != resources_.end(); ++i) {
149 WebContents* contents = WebContents::FromRenderViewHost(i->first);
150 if (contents &&
151 contents->GetRenderProcessHost()->GetID() == render_process_host_id &&
152 contents->GetRenderViewHost()->GetRoutingID() == routing_id) {
153 return i->second;
154 }
155 }
156
157 return NULL;
158 }
159
160 void TaskManagerGuestResourceProvider::StartUpdating() {
161 DCHECK(!updating_);
162 updating_ = true;
163
164 // Add all the existing guest WebContents.
165 for (RenderProcessHost::iterator i(
166 RenderProcessHost::AllHostsIterator());
167 !i.IsAtEnd(); i.Advance()) {
168 RenderProcessHost::RenderWidgetHostsIterator iter =
169 i.GetCurrentValue()->GetRenderWidgetHostsIterator();
170 for (; !iter.IsAtEnd(); iter.Advance()) {
171 const RenderWidgetHost* widget = iter.GetCurrentValue();
172 if (widget->IsRenderView()) {
173 RenderViewHost* rvh =
174 RenderViewHost::From(const_cast<RenderWidgetHost*>(widget));
175 if (rvh->IsSubframe())
176 Add(rvh);
177 }
178 }
179 }
180
181 // Then we register for notifications to get new guests.
182 registrar_.Add(this, content::NOTIFICATION_WEB_CONTENTS_CONNECTED,
183 content::NotificationService::AllBrowserContextsAndSources());
184 registrar_.Add(this, content::NOTIFICATION_WEB_CONTENTS_DISCONNECTED,
185 content::NotificationService::AllBrowserContextsAndSources());
186 }
187
188 void TaskManagerGuestResourceProvider::StopUpdating() {
189 DCHECK(updating_);
190 updating_ = false;
191
192 // Unregister for notifications.
193 registrar_.Remove(this, content::NOTIFICATION_WEB_CONTENTS_CONNECTED,
194 content::NotificationService::AllBrowserContextsAndSources());
195 registrar_.Remove(this, content::NOTIFICATION_WEB_CONTENTS_DISCONNECTED,
196 content::NotificationService::AllBrowserContextsAndSources());
197
198 // Delete all the resources.
199 STLDeleteContainerPairSecondPointers(resources_.begin(), resources_.end());
200
201 resources_.clear();
202 }
203
204 void TaskManagerGuestResourceProvider::Add(
205 RenderViewHost* render_view_host) {
206 TaskManagerGuestResource* resource =
207 new TaskManagerGuestResource(render_view_host);
208 resources_[render_view_host] = resource;
209 task_manager_->AddResource(resource);
210 }
211
212 void TaskManagerGuestResourceProvider::Remove(
213 RenderViewHost* render_view_host) {
214 if (!updating_)
215 return;
216
217 GuestResourceMap::iterator iter = resources_.find(render_view_host);
218 if (iter == resources_.end())
219 return;
220
221 TaskManagerGuestResource* resource = iter->second;
222 task_manager_->RemoveResource(resource);
223 resources_.erase(iter);
224 delete resource;
225 }
226
227 void TaskManagerGuestResourceProvider::Observe(int type,
228 const content::NotificationSource& source,
229 const content::NotificationDetails& details) {
230 WebContents* web_contents = content::Source<WebContents>(source).ptr();
231 if (!web_contents || !web_contents->GetRenderViewHost()->IsSubframe())
232 return;
233
234 switch (type) {
235 case content::NOTIFICATION_WEB_CONTENTS_CONNECTED:
236 Add(web_contents->GetRenderViewHost());
237 break;
238 case content::NOTIFICATION_WEB_CONTENTS_DISCONNECTED:
239 Remove(web_contents->GetRenderViewHost());
240 break;
241 default:
242 NOTREACHED() << "Unexpected notification.";
243 }
244 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698