Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
|
ncarter (slow)
2017/07/28 17:46:29
2017
cburn
2017/07/28 18:08:21
Done.
| |
| 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/providers/render_process_host_task_provide r.h" | |
| 6 | |
| 7 #include "base/process/process.h" | |
| 8 #include "chrome/browser/task_manager/providers/child_process_task.h" | |
| 9 #include "content/public/browser/browser_child_process_host_iterator.h" | |
| 10 #include "content/public/browser/browser_thread.h" | |
| 11 #include "content/public/browser/child_process_data.h" | |
| 12 #include "content/public/browser/notification_service.h" | |
| 13 #include "content/public/browser/notification_types.h" | |
| 14 #include "content/public/browser/render_process_host.h" | |
| 15 #include "content/public/common/process_type.h" | |
| 16 #include "extensions/features/features.h" | |
| 17 | |
| 18 using content::RenderProcessHost; | |
| 19 using content::BrowserThread; | |
| 20 using content::ChildProcessData; | |
| 21 | |
| 22 #if BUILDFLAG(ENABLE_EXTENSIONS) | |
| 23 #include "extensions/browser/process_map.h" | |
| 24 #endif | |
| 25 | |
| 26 namespace task_manager { | |
| 27 | |
| 28 RenderProcessHostTaskProvider::RenderProcessHostTaskProvider() | |
| 29 : weak_ptr_factory_(this) { | |
| 30 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CREATED, | |
| 31 content::NotificationService::AllBrowserContextsAndSources()); | |
| 32 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED, | |
| 33 content::NotificationService::AllBrowserContextsAndSources()); | |
| 34 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, | |
| 35 content::NotificationService::AllBrowserContextsAndSources()); | |
| 36 } | |
| 37 | |
| 38 RenderProcessHostTaskProvider::~RenderProcessHostTaskProvider() {} | |
| 39 | |
| 40 Task* RenderProcessHostTaskProvider::GetTaskOfUrlRequest(int origin_pid, | |
| 41 int child_id, | |
| 42 int route_id) { | |
| 43 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 44 auto itr = tasks_by_rph_id_.find(child_id); | |
| 45 if (itr == tasks_by_rph_id_.end()) | |
| 46 return nullptr; | |
| 47 | |
| 48 return itr->second.get(); | |
| 49 } | |
| 50 | |
| 51 void RenderProcessHostTaskProvider::StartUpdating() { | |
| 52 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 53 DCHECK(tasks_by_rph_id_.empty()); | |
| 54 for (RenderProcessHost::iterator it(RenderProcessHost::AllHostsIterator()); | |
| 55 !it.IsAtEnd(); it.Advance()) { | |
| 56 RenderProcessHost* host = it.GetCurrentValue(); | |
| 57 if (host->GetHandle()) { | |
| 58 CreateTask(host->GetID()); | |
| 59 } else { | |
| 60 // If the host isn't ready do nothing and we will learn of its creation | |
| 61 // from the notification service. | |
| 62 } | |
| 63 } | |
| 64 } | |
| 65 | |
| 66 void RenderProcessHostTaskProvider::StopUpdating() { | |
| 67 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 68 | |
| 69 // ChildProcessDataCollected() should never be called after this, and hence | |
|
ncarter (slow)
2017/07/28 17:46:29
This comment looks like copy/paste cruft
cburn
2017/07/28 18:08:21
It is. Done
| |
| 70 // we must invalidate the weak pointers. | |
| 71 weak_ptr_factory_.InvalidateWeakPtrs(); | |
|
ncarter (slow)
2017/07/28 17:46:29
It looks like we can remove the weak_ptr_factory_
cburn
2017/07/28 18:08:21
Yes, removed now.
| |
| 72 | |
| 73 // Then delete all tasks (if any). | |
| 74 tasks_by_rph_id_.clear(); | |
| 75 } | |
| 76 | |
| 77 void RenderProcessHostTaskProvider::CreateTask( | |
| 78 const int render_process_host_id) { | |
| 79 // Checks that the task by RenderProcessHost ID isn't already a task in the | |
| 80 // map and deletes it if it is so they new task can be cleanly added. | |
| 81 DeleteTask(render_process_host_id); | |
| 82 std::unique_ptr<ChildProcessTask>& task = | |
| 83 tasks_by_rph_id_[render_process_host_id]; | |
| 84 | |
| 85 RenderProcessHost* host = RenderProcessHost::FromID(render_process_host_id); | |
| 86 | |
| 87 // Create the task and notify the observer. | |
| 88 ChildProcessData data(content::PROCESS_TYPE_RENDERER); | |
| 89 data.handle = host->GetHandle(); | |
| 90 data.id = host->GetID(); | |
| 91 task.reset(new ChildProcessTask(data)); | |
| 92 NotifyObserverTaskAdded(task.get()); | |
| 93 } | |
| 94 | |
| 95 void RenderProcessHostTaskProvider::DeleteTask( | |
| 96 const int render_process_host_id) { | |
| 97 auto itr = tasks_by_rph_id_.find(render_process_host_id); | |
| 98 // If the render process host id isn't being tracked in |tasks_by_rph_id| do | |
| 99 // nothing. | |
| 100 if (itr == tasks_by_rph_id_.end()) | |
| 101 return; | |
| 102 | |
| 103 NotifyObserverTaskRemoved(itr->second.get()); | |
| 104 | |
| 105 // Finally delete the task. | |
| 106 tasks_by_rph_id_.erase(itr); | |
| 107 } | |
| 108 | |
| 109 void RenderProcessHostTaskProvider::Observe( | |
| 110 int type, | |
| 111 const content::NotificationSource& source, | |
| 112 const content::NotificationDetails& details) { | |
| 113 content::RenderProcessHost* host = | |
| 114 content::Source<content::RenderProcessHost>(source).ptr(); | |
| 115 ChildProcessData data(content::PROCESS_TYPE_RENDERER); | |
| 116 switch (type) { | |
| 117 case content::NOTIFICATION_RENDERER_PROCESS_CREATED: | |
| 118 CreateTask(host->GetID()); | |
| 119 break; | |
| 120 case content::NOTIFICATION_RENDERER_PROCESS_CLOSED: | |
| 121 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: | |
| 122 DeleteTask(host->GetID()); | |
| 123 break; | |
| 124 default: | |
| 125 NOTREACHED(); | |
| 126 break; | |
| 127 } | |
| 128 } | |
| 129 | |
| 130 } // namespace task_manager | |
| OLD | NEW |