OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 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/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_pid_.find(static_cast<base::ProcessId>(origin_pid)); | |
45 if (itr == tasks_by_pid_.end()) | |
46 return nullptr; | |
ncarter (slow)
2017/07/26 21:22:34
Did you experiment with doing: tasks_by_rph_id_.fi
cburn
2017/07/27 19:28:29
Done. This was the only reason tasks_by_pid_.
| |
47 | |
48 return itr->second; | |
49 } | |
50 | |
51 void RenderProcessHostTaskProvider::StartUpdating() { | |
52 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
53 DCHECK(tasks_by_rph_id_.empty()); | |
54 DCHECK(tasks_by_pid_.empty()); | |
55 for (RenderProcessHost::iterator it(RenderProcessHost::AllHostsIterator()); | |
56 !it.IsAtEnd(); it.Advance()) { | |
57 RenderProcessHost* host = it.GetCurrentValue(); | |
58 ChildProcessData data(content::PROCESS_TYPE_RENDERER); | |
59 data.handle = host->GetHandle(); | |
60 data.id = host->GetID(); | |
61 CreateTask(data); | |
ncarter (slow)
2017/07/26 21:22:34
What if host->IsReady() is false when the TaskMana
cburn
2017/07/27 19:28:29
I like the second option. It looks cleaner to me a
| |
62 } | |
63 } | |
64 | |
65 void RenderProcessHostTaskProvider::StopUpdating() { | |
66 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
67 | |
68 // ChildProcessDataCollected() should never be called after this, and hence | |
69 // we must invalidate the weak pointers. | |
70 weak_ptr_factory_.InvalidateWeakPtrs(); | |
71 | |
72 // Then delete all tasks (if any). | |
73 tasks_by_rph_id_.clear(); | |
74 tasks_by_pid_.clear(); | |
75 } | |
76 | |
77 void RenderProcessHostTaskProvider::CreateTask( | |
78 const content::ChildProcessData& data) { | |
ncarter (slow)
2017/07/26 21:22:34
I'm thinking this argument should either be an RPH
cburn
2017/07/27 19:28:29
I agree. I went with rph_id so it is parallel with
| |
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 without | |
81 // causing crashes. | |
82 std::unique_ptr<ChildProcessTask>& check_task = tasks_by_rph_id_[data.id]; | |
83 if (check_task.get() != nullptr) { | |
84 DeleteTask(data.id); | |
ncarter (slow)
2017/07/26 21:22:34
After DeleteTask returns, check_task references fr
cburn
2017/07/27 19:28:30
Done.
| |
85 } | |
86 std::unique_ptr<ChildProcessTask>& task = tasks_by_rph_id_[data.id]; | |
87 // Create the task and notify the observer. | |
88 task.reset(new ChildProcessTask(data)); | |
89 tasks_by_pid_[task->process_id()] = task.get(); | |
90 NotifyObserverTaskAdded(task.get()); | |
91 } | |
92 | |
93 void RenderProcessHostTaskProvider::DeleteTask( | |
94 const int render_process_host_ID) { | |
95 auto itr = tasks_by_rph_id_.find(render_process_host_ID); | |
96 // The following case should never happen since we start observing | |
97 // |RenderProcessHostObserver| only after we collect all pre-existing child | |
98 // processes and are notified (on the UI thread) that the collection is | |
99 // completed at |ChildProcessDataCollected()|. | |
ncarter (slow)
2017/07/26 21:22:34
Does this comment make sense?
cburn
2017/07/27 19:28:30
It does not.
| |
100 if (itr == tasks_by_rph_id_.end()) { | |
101 // BUG(crbug.com/611067): Temporarily removing due to test flakes. The | |
102 // reason why this happens is well understood (see bug), but there's no | |
103 // quick and easy fix. | |
104 // NOTREACHED(); | |
ncarter (slow)
2017/07/26 21:22:34
Delete this comment, it looks like it's a copy/pas
cburn
2017/07/27 19:28:30
Done.
| |
105 return; | |
106 } | |
107 | |
108 NotifyObserverTaskRemoved(itr->second.get()); | |
109 | |
110 // Clear from the pid index. | |
111 tasks_by_pid_.erase(itr->second->process_id()); | |
112 | |
113 // Finally delete the task. | |
114 tasks_by_rph_id_.erase(itr); | |
115 } | |
116 | |
117 void RenderProcessHostTaskProvider::Observe( | |
118 int type, | |
119 const content::NotificationSource& source, | |
120 const content::NotificationDetails& details) { | |
121 content::RenderProcessHost* host = | |
122 content::Source<content::RenderProcessHost>(source).ptr(); | |
123 ChildProcessData data(content::PROCESS_TYPE_RENDERER); | |
124 switch (type) { | |
125 case content::NOTIFICATION_RENDERER_PROCESS_CREATED: | |
126 data.id = host->GetID(); | |
127 data.handle = host->GetHandle(); | |
ncarter (slow)
2017/07/26 21:22:34
It looks like NOTIFICATION_RENDERER_PROCESS_CREATE
cburn
2017/07/27 19:28:29
I don't think we need to do the PostTaskWhenProces
| |
128 host->PostTaskWhenProcessIsReady( | |
129 base::Bind(&RenderProcessHostTaskProvider::CreateTask, | |
130 weak_ptr_factory_.GetWeakPtr(), data)); | |
131 break; | |
132 case content::NOTIFICATION_RENDERER_PROCESS_CLOSED: | |
133 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: | |
134 DeleteTask(host->GetID()); | |
135 break; | |
136 default: | |
137 NOTREACHED(); | |
138 break; | |
139 } | |
140 } | |
141 | |
142 } // namespace task_manager | |
OLD | NEW |