OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/memory_details.h" | 5 #include "chrome/browser/memory_details.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <set> | 8 #include <set> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
209 } | 209 } |
210 | 210 |
211 void MemoryDetails::CollectChildInfoOnUIThread() { | 211 void MemoryDetails::CollectChildInfoOnUIThread() { |
212 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 212 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
213 | 213 |
214 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID) | 214 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID) |
215 const pid_t zygote_pid = content::ZygoteHost::GetInstance()->GetPid(); | 215 const pid_t zygote_pid = content::ZygoteHost::GetInstance()->GetPid(); |
216 #endif | 216 #endif |
217 | 217 |
218 ProcessData* const chrome_browser = ChromeBrowser(); | 218 ProcessData* const chrome_browser = ChromeBrowser(); |
219 | |
220 // First pass, collate the widgets by process ID. | |
221 std::map<base::ProcessId, std::vector<RenderWidgetHost*>> widgets_by_pid; | |
222 scoped_ptr<content::RenderWidgetHostIterator> widget_it( | |
223 RenderWidgetHost::GetRenderWidgetHosts()); | |
224 while (content::RenderWidgetHost* widget = widget_it->GetNextHost()) { | |
225 // Ignore processes that don't have a connection, such as crashed tabs. | |
226 if (!widget->GetProcess()->HasConnection()) | |
227 continue; | |
228 base::ProcessId pid = base::GetProcId(widget->GetProcess()->GetHandle()); | |
229 widgets_by_pid[pid].push_back(widget); | |
230 } | |
231 | |
219 // Get more information about the process. | 232 // Get more information about the process. |
220 for (size_t index = 0; index < chrome_browser->processes.size(); | 233 for (ProcessMemoryInformation& process : chrome_browser->processes) { |
221 index++) { | 234 // If there's at least one widget in the process, it is some kind of |
222 // Check if it's a renderer, if so get the list of page titles in it and | 235 // renderer process belonging to this browser. All these widgets will share |
Charlie Reis
2015/10/29 20:13:42
nit: Merge conflict? Looks like you lost this lin
ncarter (slow)
2015/10/29 22:14:17
Nice catch, thank you. Done.
| |
223 // check if it's a diagnostics-related process. We skip about:memory pages. | 236 content::RenderProcessHost* render_process_host = nullptr; |
224 // Iterate the RenderProcessHosts to find the tab contents. | 237 if (!widgets_by_pid[process.pid].empty()) { |
225 ProcessMemoryInformation& process = | 238 // Mark it as a normal renderer process, if we don't refine it to some |
226 chrome_browser->processes[index]; | 239 // other |renderer_type| later. |
240 process.process_type = content::PROCESS_TYPE_RENDERER; | |
241 process.renderer_type = ProcessMemoryInformation::RENDERER_NORMAL; | |
242 render_process_host = widgets_by_pid[process.pid].front()->GetProcess(); | |
243 } | |
227 | 244 |
228 scoped_ptr<content::RenderWidgetHostIterator> widgets( | |
229 RenderWidgetHost::GetRenderWidgetHosts()); | |
230 while (content::RenderWidgetHost* widget = widgets->GetNextHost()) { | |
231 content::RenderProcessHost* render_process_host = | |
232 widget->GetProcess(); | |
233 DCHECK(render_process_host); | |
234 // Ignore processes that don't have a connection, such as crashed tabs. | |
235 if (!render_process_host->HasConnection() || | |
236 process.pid != base::GetProcId(render_process_host->GetHandle())) { | |
237 continue; | |
238 } | |
239 | |
240 // The RenderProcessHost may host multiple WebContentses. Any | |
241 // of them which contain diagnostics information make the whole | |
242 // process be considered a diagnostics process. | |
243 RenderViewHost* host = RenderViewHost::From(widget); | |
244 if (!host) | |
245 continue; | |
246 | |
247 process.process_type = content::PROCESS_TYPE_RENDERER; | |
248 bool is_extension = false; | |
249 #if defined(ENABLE_EXTENSIONS) | 245 #if defined(ENABLE_EXTENSIONS) |
246 // Determine if this is an extension process. | |
247 bool process_is_for_extensions = false; | |
248 if (render_process_host) { | |
250 content::BrowserContext* context = | 249 content::BrowserContext* context = |
251 render_process_host->GetBrowserContext(); | 250 render_process_host->GetBrowserContext(); |
252 extensions::ExtensionRegistry* extension_registry = | 251 extensions::ExtensionRegistry* extension_registry = |
253 extensions::ExtensionRegistry::Get(context); | 252 extensions::ExtensionRegistry::Get(context); |
254 extensions::ProcessMap* extension_process_map = | 253 extensions::ProcessMap* extension_process_map = |
255 extensions::ProcessMap::Get(context); | 254 extensions::ProcessMap::Get(context); |
256 is_extension = extension_process_map->Contains( | 255 process_is_for_extensions = |
257 host->GetProcess()->GetID()); | 256 extension_process_map->Contains(render_process_host->GetID()); |
257 | |
258 // For our purposes, don't count processes containing only hosted apps | |
259 // as extension processes. See also: crbug.com/102533. | |
260 std::set<std::string> extension_ids = | |
261 extension_process_map->GetExtensionsInProcess( | |
262 render_process_host->GetID()); | |
263 for (std::set<std::string>::iterator iter = extension_ids.begin(); | |
264 iter != extension_ids.end(); ++iter) { | |
265 const Extension* extension = | |
266 extension_registry->enabled_extensions().GetByID(*iter); | |
267 if (extension && !extension->is_hosted_app()) { | |
268 process.renderer_type = ProcessMemoryInformation::RENDERER_EXTENSION; | |
269 break; | |
270 } | |
271 } | |
272 } | |
258 #endif | 273 #endif |
259 | 274 |
260 WebContents* contents = WebContents::FromRenderViewHost(host); | 275 // Use the list of widgets to iterate over the WebContents instances whose |
261 GURL url; | 276 // main RenderFrameHosts are in |process|. Refine our determination of the |
262 if (contents) { | 277 // |process.renderer_type|, and record the page titles. |
263 url = contents->GetURL(); | 278 for (content::RenderWidgetHost* widget : widgets_by_pid[process.pid]) { |
264 SiteData* site_data = | 279 DCHECK_EQ(render_process_host, widget->GetProcess()); |
265 &chrome_browser->site_data[contents->GetBrowserContext()]; | 280 |
266 SiteDetails::CollectSiteInfo(contents, site_data); | 281 RenderViewHost* rvh = RenderViewHost::From(widget); |
282 if (!rvh) | |
283 continue; | |
284 | |
285 WebContents* contents = WebContents::FromRenderViewHost(rvh); | |
286 | |
287 // Assume that an RVH without a web contents is an interstitial. | |
288 if (!contents) { | |
289 process.renderer_type = ProcessMemoryInformation::RENDERER_INTERSTITIAL; | |
290 continue; | |
267 } | 291 } |
292 | |
293 // If this is a RVH for a subframe; skip it to avoid double-counting the | |
294 // WebContents. | |
295 if (rvh != contents->GetRenderViewHost()) | |
296 continue; | |
297 | |
298 // The rest of this block will happen only once per WebContents. | |
299 GURL page_url = contents->GetLastCommittedURL(); | |
300 SiteData* site_data = | |
301 &chrome_browser->site_data[contents->GetBrowserContext()]; | |
302 SiteDetails::CollectSiteInfo(contents, site_data); | |
303 | |
304 bool is_webui = | |
305 rvh->GetEnabledBindings() & content::BINDINGS_POLICY_WEB_UI; | |
306 | |
307 if (is_webui) { | |
308 process.renderer_type = ProcessMemoryInformation::RENDERER_CHROME; | |
309 } | |
310 | |
268 #if defined(ENABLE_EXTENSIONS) | 311 #if defined(ENABLE_EXTENSIONS) |
269 extensions::ViewType type = extensions::GetViewType(contents); | 312 if (!is_webui && process_is_for_extensions) { |
270 #endif | |
271 if (host->GetEnabledBindings() & content::BINDINGS_POLICY_WEB_UI) { | |
272 process.renderer_type = ProcessMemoryInformation::RENDERER_CHROME; | |
273 } else if (is_extension) { | |
274 #if defined(ENABLE_EXTENSIONS) | |
275 // For our purposes, don't count processes containing only hosted apps | |
276 // as extension processes. See also: crbug.com/102533. | |
277 std::set<std::string> extension_ids = | |
278 extension_process_map->GetExtensionsInProcess( | |
279 host->GetProcess()->GetID()); | |
280 for (std::set<std::string>::iterator iter = extension_ids.begin(); | |
281 iter != extension_ids.end(); ++iter) { | |
282 const Extension* extension = | |
283 extension_registry->enabled_extensions().GetByID(*iter); | |
284 if (extension && !extension->is_hosted_app()) { | |
285 process.renderer_type = | |
286 ProcessMemoryInformation::RENDERER_EXTENSION; | |
287 break; | |
288 } | |
289 } | |
290 #endif | |
291 } | |
292 #if defined(ENABLE_EXTENSIONS) | |
293 if (is_extension) { | |
294 const Extension* extension = | 313 const Extension* extension = |
295 extension_registry->enabled_extensions().GetByID(url.host()); | 314 extensions::ExtensionRegistry::Get( |
315 render_process_host->GetBrowserContext()) | |
316 ->enabled_extensions() | |
317 .GetByID(page_url.host()); | |
296 if (extension) { | 318 if (extension) { |
297 base::string16 title = base::UTF8ToUTF16(extension->name()); | 319 base::string16 title = base::UTF8ToUTF16(extension->name()); |
298 process.titles.push_back(title); | 320 process.titles.push_back(title); |
299 process.renderer_type = | 321 process.renderer_type = |
300 ProcessMemoryInformation::RENDERER_EXTENSION; | 322 ProcessMemoryInformation::RENDERER_EXTENSION; |
301 continue; | 323 continue; |
302 } | 324 } |
303 } | 325 } |
304 #endif | |
305 | 326 |
306 if (!contents) { | 327 extensions::ViewType type = extensions::GetViewType(contents); |
307 process.renderer_type = | |
308 ProcessMemoryInformation::RENDERER_INTERSTITIAL; | |
309 continue; | |
310 } | |
311 | |
312 #if defined(ENABLE_EXTENSIONS) | |
313 if (type == extensions::VIEW_TYPE_BACKGROUND_CONTENTS) { | 328 if (type == extensions::VIEW_TYPE_BACKGROUND_CONTENTS) { |
314 process.titles.push_back(base::UTF8ToUTF16(url.spec())); | 329 process.titles.push_back(base::UTF8ToUTF16(page_url.spec())); |
315 process.renderer_type = | 330 process.renderer_type = |
316 ProcessMemoryInformation::RENDERER_BACKGROUND_APP; | 331 ProcessMemoryInformation::RENDERER_BACKGROUND_APP; |
317 continue; | 332 continue; |
318 } | 333 } |
319 #endif | 334 #endif |
320 | 335 |
321 // Since we have a WebContents and and the renderer type hasn't been | |
322 // set yet, it must be a normal tabbed renderer. | |
323 if (process.renderer_type == ProcessMemoryInformation::RENDERER_UNKNOWN) | |
324 process.renderer_type = ProcessMemoryInformation::RENDERER_NORMAL; | |
325 | |
326 base::string16 title = contents->GetTitle(); | 336 base::string16 title = contents->GetTitle(); |
327 if (!title.length()) | 337 if (!title.length()) |
328 title = l10n_util::GetStringUTF16(IDS_DEFAULT_TAB_TITLE); | 338 title = l10n_util::GetStringUTF16(IDS_DEFAULT_TAB_TITLE); |
329 process.titles.push_back(title); | 339 process.titles.push_back(title); |
330 | 340 |
341 // The presence of a single WebContents with a diagnostics page will make | |
342 // make the whole process be considered a diagnostics process. | |
343 // | |
331 // We need to check the pending entry as well as the virtual_url to | 344 // We need to check the pending entry as well as the virtual_url to |
332 // see if it's a chrome://memory URL (we don't want to count these in | 345 // see if it's a chrome://memory URL (we don't want to count these in |
333 // the total memory usage of the browser). | 346 // the total memory usage of the browser). |
334 // | 347 // |
335 // When we reach here, chrome://memory will be the pending entry since | 348 // When we reach here, chrome://memory will be the pending entry since |
336 // we haven't responded with any data such that it would be committed. | 349 // we haven't responded with any data such that it would be committed. |
337 // If you have another chrome://memory tab open (which would be | 350 // If you have another chrome://memory tab open (which would be |
338 // committed), we don't want to count it either, so we also check the | 351 // committed), we don't want to count it either, so we also check the |
339 // last committed entry. | 352 // last committed entry. |
340 // | 353 // |
(...skipping 15 matching lines...) Expand all Loading... | |
356 } | 369 } |
357 | 370 |
358 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID) | 371 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID) |
359 if (process.pid == zygote_pid) { | 372 if (process.pid == zygote_pid) { |
360 process.process_type = content::PROCESS_TYPE_ZYGOTE; | 373 process.process_type = content::PROCESS_TYPE_ZYGOTE; |
361 } | 374 } |
362 #endif | 375 #endif |
363 } | 376 } |
364 | 377 |
365 // Get rid of other Chrome processes that are from a different profile. | 378 // Get rid of other Chrome processes that are from a different profile. |
366 for (size_t index = 0; index < chrome_browser->processes.size(); | 379 auto is_unknown = [](ProcessMemoryInformation& process) { |
367 index++) { | 380 return process.process_type == content::PROCESS_TYPE_UNKNOWN; |
368 if (chrome_browser->processes[index].process_type == | 381 }; |
369 content::PROCESS_TYPE_UNKNOWN) { | 382 auto& vector = chrome_browser->processes; |
370 chrome_browser->processes.erase( | 383 vector.erase(std::remove_if(vector.begin(), vector.end(), is_unknown), |
371 chrome_browser->processes.begin() + index); | 384 vector.end()); |
372 index--; | |
373 } | |
374 } | |
375 | 385 |
376 OnDetailsAvailable(); | 386 OnDetailsAvailable(); |
377 } | 387 } |
OLD | NEW |