Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / task_management / providers / child_process_task_provider.h
blob9cce5b7ed29d3387d4dab602c3e7ce656f95c293
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.
5 #ifndef CHROME_BROWSER_TASK_MANAGEMENT_PROVIDERS_CHILD_PROCESS_TASK_PROVIDER_H_
6 #define CHROME_BROWSER_TASK_MANAGEMENT_PROVIDERS_CHILD_PROCESS_TASK_PROVIDER_H_
8 #include <map>
9 #include <vector>
11 #include "base/memory/weak_ptr.h"
12 #include "chrome/browser/task_management/providers/task_provider.h"
13 #include "content/public/browser/browser_child_process_observer.h"
15 namespace content {
16 struct ChildProcessData;
19 namespace task_management {
21 class ChildProcessTask;
23 // Defines a provider to provide the tasks that represent various types of child
24 // processes such as the GPU process or a plugin process ... etc.
25 class ChildProcessTaskProvider
26 : public TaskProvider,
27 public content::BrowserChildProcessObserver {
28 public:
29 ChildProcessTaskProvider();
30 ~ChildProcessTaskProvider() override;
32 // task_management::TaskProvider:
33 Task* GetTaskOfUrlRequest(int origin_pid,
34 int child_id,
35 int route_id) override;
37 // content::BrowserChildProcessObserver:
38 void BrowserChildProcessHostConnected(
39 const content::ChildProcessData& data) override;
40 void BrowserChildProcessHostDisconnected(
41 const content::ChildProcessData& data) override;
43 private:
44 friend class ChildProcessTaskTest;
46 // task_management::TaskProvider:
47 void StartUpdating() override;
48 void StopUpdating() override;
50 // The pre-existing child processes data will be collected on the IO thread.
51 // When that is done, we will be notified on the UI thread by receiving a call
52 // to this method.
53 void ChildProcessDataCollected(
54 scoped_ptr<const std::vector<content::ChildProcessData>> child_processes);
56 // Creates a ChildProcessTask from the given |data| and notifies the observer
57 // of its addition.
58 void CreateTask(const content::ChildProcessData& data);
60 // Deletes a ChildProcessTask whose |handle| is provided after notifying the
61 // observer of its deletion.
62 void DeleteTask(base::ProcessHandle handle);
64 // A map to track ChildProcessTask's by their handles.
65 typedef std::map<base::ProcessHandle, ChildProcessTask*> HandleToTaskMap;
66 HandleToTaskMap tasks_by_handle_;
68 // A map to track ChildProcessTask's by their PIDs.
70 // Why have both |tasks_by_handle_| and |tasks_by_pid_|? On Windows, where
71 // handles are not the same as PIDs, |DeleteTask| gets only a handle, which
72 // may be closed, making it impossible to query the PID from the handle. So
73 // we need an index on the handle. Meanwhile, we also need an index on the
74 // PID so that we can efficiently implement |GetTaskOfUrlRequest()|, which
75 // gets only a PID.
77 // TODO(afakhry): Fix this either by keeping the handle open via
78 // |base::Process|, or amending the |BrowserChildProcessObserver| interface to
79 // supply the PID.
80 typedef std::map<base::ProcessId, ChildProcessTask*> PidToTaskMap;
81 PidToTaskMap tasks_by_pid_;
83 // Always keep this the last member of this class to make sure it's the
84 // first thing to be destructed.
85 base::WeakPtrFactory<ChildProcessTaskProvider> weak_ptr_factory_;
87 DISALLOW_COPY_AND_ASSIGN(ChildProcessTaskProvider);
90 } // namespace task_management
92 #endif // CHROME_BROWSER_TASK_MANAGEMENT_PROVIDERS_CHILD_PROCESS_TASK_PROVIDER_H_