| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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_management/providers/child_process_task_provider.h
" | 5 #include "chrome/browser/task_management/providers/child_process_task_provider.h
" |
| 6 | 6 |
| 7 #include "base/process/process.h" | 7 #include "base/process/process.h" |
| 8 #include "base/stl_util.h" | |
| 9 #include "chrome/browser/task_management/providers/child_process_task.h" | 8 #include "chrome/browser/task_management/providers/child_process_task.h" |
| 10 #include "content/public/browser/browser_child_process_host_iterator.h" | 9 #include "content/public/browser/browser_child_process_host_iterator.h" |
| 11 #include "content/public/browser/browser_thread.h" | 10 #include "content/public/browser/browser_thread.h" |
| 12 #include "content/public/browser/child_process_data.h" | 11 #include "content/public/browser/child_process_data.h" |
| 13 | 12 |
| 14 using content::BrowserChildProcessHostIterator; | 13 using content::BrowserChildProcessHostIterator; |
| 15 using content::BrowserThread; | 14 using content::BrowserThread; |
| 16 using content::ChildProcessData; | 15 using content::ChildProcessData; |
| 17 | 16 |
| 18 namespace task_management { | 17 namespace task_management { |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 // we must invalidate the weak pointers. | 96 // we must invalidate the weak pointers. |
| 98 weak_ptr_factory_.InvalidateWeakPtrs(); | 97 weak_ptr_factory_.InvalidateWeakPtrs(); |
| 99 | 98 |
| 100 // First, stop observing. | 99 // First, stop observing. |
| 101 BrowserChildProcessObserver::Remove(this); | 100 BrowserChildProcessObserver::Remove(this); |
| 102 | 101 |
| 103 // Remember: You can't notify the observer of tasks removal here, | 102 // Remember: You can't notify the observer of tasks removal here, |
| 104 // StopUpdating() is called after the observer has been cleared. | 103 // StopUpdating() is called after the observer has been cleared. |
| 105 | 104 |
| 106 // Then delete all tasks (if any). | 105 // Then delete all tasks (if any). |
| 107 STLDeleteValues(&tasks_by_handle_); // This will clear |tasks_by_handle_|. | 106 tasks_by_handle_.clear(); |
| 108 tasks_by_pid_.clear(); | 107 tasks_by_pid_.clear(); |
| 109 } | 108 } |
| 110 | 109 |
| 111 void ChildProcessTaskProvider::ChildProcessDataCollected( | 110 void ChildProcessTaskProvider::ChildProcessDataCollected( |
| 112 std::unique_ptr<const std::vector<content::ChildProcessData>> | 111 std::unique_ptr<const std::vector<content::ChildProcessData>> |
| 113 child_processes) { | 112 child_processes) { |
| 114 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 113 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 115 | 114 |
| 116 for (const auto& process_data : *child_processes) | 115 for (const auto& process_data : *child_processes) |
| 117 CreateTask(process_data); | 116 CreateTask(process_data); |
| 118 | 117 |
| 119 // Now start observing. | 118 // Now start observing. |
| 120 BrowserChildProcessObserver::Add(this); | 119 BrowserChildProcessObserver::Add(this); |
| 121 } | 120 } |
| 122 | 121 |
| 123 void ChildProcessTaskProvider::CreateTask( | 122 void ChildProcessTaskProvider::CreateTask( |
| 124 const content::ChildProcessData& data) { | 123 const content::ChildProcessData& data) { |
| 125 if (tasks_by_handle_.find(data.handle) != tasks_by_handle_.end()) { | 124 std::unique_ptr<ChildProcessTask>& task = tasks_by_handle_[data.handle]; |
| 126 // This case can happen when some of the child process data we collect upon | 125 if (task) { |
| 127 // StartUpdating() might be of BrowserChildProcessHosts whose process | 126 // This task is already known to us. This case can happen when some of the |
| 128 // hadn't launched yet. So we just return. | 127 // child process data we collect upon StartUpdating() might be of |
| 128 // BrowserChildProcessHosts whose process hadn't launched yet. So we just |
| 129 // return. |
| 129 return; | 130 return; |
| 130 } | 131 } |
| 131 | 132 |
| 132 // Create the task and notify the observer. | 133 // Create the task and notify the observer. |
| 133 ChildProcessTask* task = new ChildProcessTask(data); | 134 task.reset(new ChildProcessTask(data)); |
| 134 tasks_by_handle_[data.handle] = task; | 135 tasks_by_pid_[task->process_id()] = task.get(); |
| 135 tasks_by_pid_[task->process_id()] = task; | 136 NotifyObserverTaskAdded(task.get()); |
| 136 NotifyObserverTaskAdded(task); | |
| 137 } | 137 } |
| 138 | 138 |
| 139 void ChildProcessTaskProvider::DeleteTask(base::ProcessHandle handle) { | 139 void ChildProcessTaskProvider::DeleteTask(base::ProcessHandle handle) { |
| 140 auto itr = tasks_by_handle_.find(handle); | 140 auto itr = tasks_by_handle_.find(handle); |
| 141 | 141 |
| 142 // The following case should never happen since we start observing | 142 // The following case should never happen since we start observing |
| 143 // |BrowserChildProcessObserver| only after we collect all pre-existing child | 143 // |BrowserChildProcessObserver| only after we collect all pre-existing child |
| 144 // processes and are notified (on the UI thread) that the collection is | 144 // processes and are notified (on the UI thread) that the collection is |
| 145 // completed at |ChildProcessDataCollected()|. | 145 // completed at |ChildProcessDataCollected()|. |
| 146 if (itr == tasks_by_handle_.end()) { | 146 if (itr == tasks_by_handle_.end()) { |
| 147 // BUG(crbug.com/611067): Temporarily removing due to test flakes. The | 147 // BUG(crbug.com/611067): Temporarily removing due to test flakes. The |
| 148 // reason why this happens is well understood (see bug), but there's no | 148 // reason why this happens is well understood (see bug), but there's no |
| 149 // quick and easy fix. | 149 // quick and easy fix. |
| 150 // NOTREACHED(); | 150 // NOTREACHED(); |
| 151 return; | 151 return; |
| 152 } | 152 } |
| 153 | 153 |
| 154 ChildProcessTask* task = itr->second; | 154 NotifyObserverTaskRemoved(itr->second.get()); |
| 155 | 155 |
| 156 NotifyObserverTaskRemoved(task); | 156 // Clear from the pid index. |
| 157 tasks_by_pid_.erase(itr->second->process_id()); |
| 157 | 158 |
| 158 // Finally delete the task. | 159 // Finally delete the task. |
| 159 tasks_by_handle_.erase(itr); | 160 tasks_by_handle_.erase(itr); |
| 160 tasks_by_pid_.erase(task->process_id()); | |
| 161 delete task; | |
| 162 } | 161 } |
| 163 | 162 |
| 164 } // namespace task_management | 163 } // namespace task_management |
| OLD | NEW |