OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #ifndef CONTENT_BROWSER_BROWSER_CHILD_PROCESS_HOST_H_ | 5 #ifndef CONTENT_BROWSER_BROWSER_CHILD_PROCESS_HOST_H_ |
6 #define CONTENT_BROWSER_BROWSER_CHILD_PROCESS_HOST_H_ | 6 #define CONTENT_BROWSER_BROWSER_CHILD_PROCESS_HOST_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <list> | 9 #include <list> |
10 | 10 |
11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
13 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
14 #include "base/process.h" | 14 #include "base/process.h" |
15 #include "base/synchronization/waitable_event_watcher.h" | 15 #include "base/synchronization/waitable_event_watcher.h" |
16 #include "content/browser/child_process_launcher.h" | 16 #include "content/browser/child_process_launcher.h" |
17 #include "content/common/content_export.h" | 17 #include "content/public/browser/browser_child_process_host.h" |
18 #include "content/public/browser/child_process_data.h" | 18 #include "content/public/browser/child_process_data.h" |
19 #include "content/public/common/child_process_host_delegate.h" | 19 #include "content/public/common/child_process_host_delegate.h" |
20 #include "ipc/ipc_message.h" | |
21 | 20 |
22 namespace base { | 21 namespace content { |
23 class WaitableEvent; | 22 class BrowserChildProcessHostIterator; |
24 } | 23 } |
25 | 24 |
26 namespace content { | 25 // Plugins/workers and other child processes that live on the IO thread use this |
27 class ChildProcessHost; | 26 // class. RenderProcessHostImpl is the main exception that doesn't use this |
28 } | 27 /// class because it lives on the UI thread. |
29 | |
30 // Plugins/workers and other child processes that live on the IO thread should | |
31 // derive from this class. | |
32 // | |
33 // [Browser]RenderProcessHost is the main exception that doesn't derive from | |
34 // this class. That project lives on the UI thread. | |
35 class CONTENT_EXPORT BrowserChildProcessHost : | 28 class CONTENT_EXPORT BrowserChildProcessHost : |
| 29 public NON_EXPORTED_BASE(content::BrowserChildProcessHost), |
36 public NON_EXPORTED_BASE(content::ChildProcessHostDelegate), | 30 public NON_EXPORTED_BASE(content::ChildProcessHostDelegate), |
37 public ChildProcessLauncher::Client, | 31 public ChildProcessLauncher::Client, |
38 public base::WaitableEventWatcher::Delegate, | 32 public base::WaitableEventWatcher::Delegate { |
39 public IPC::Message::Sender { | |
40 public: | 33 public: |
| 34 BrowserChildProcessHost(content::ProcessType type, |
| 35 content::BrowserChildProcessHostDelegate* delegate); |
41 virtual ~BrowserChildProcessHost(); | 36 virtual ~BrowserChildProcessHost(); |
42 | 37 |
43 virtual void OnWaitableEventSignaled( | |
44 base::WaitableEvent* waitable_event) OVERRIDE; | |
45 | |
46 // Terminates all child processes and deletes each BrowserChildProcessHost | 38 // Terminates all child processes and deletes each BrowserChildProcessHost |
47 // instance. | 39 // instance. |
48 static void TerminateAll(); | 40 static void TerminateAll(); |
49 | 41 |
50 // The Iterator class allows iteration through either all child processes, or | 42 // BrowserChildProcessHost implementation: |
51 // ones of a specific type, depending on which constructor is used. Note that | |
52 // this should be done from the IO thread and that the iterator should not be | |
53 // kept around as it may be invalidated on subsequent event processing in the | |
54 // event loop. | |
55 class CONTENT_EXPORT Iterator { | |
56 public: | |
57 Iterator(); | |
58 explicit Iterator(content::ProcessType type); | |
59 BrowserChildProcessHost* operator->() { return *iterator_; } | |
60 BrowserChildProcessHost* operator*() { return *iterator_; } | |
61 BrowserChildProcessHost* operator++(); | |
62 bool Done(); | |
63 | |
64 private: | |
65 bool all_; | |
66 content::ProcessType type_; | |
67 std::list<BrowserChildProcessHost*>::iterator iterator_; | |
68 }; | |
69 | |
70 // IPC::Message::Sender override | |
71 virtual bool Send(IPC::Message* message) OVERRIDE; | 43 virtual bool Send(IPC::Message* message) OVERRIDE; |
72 | 44 virtual void Launch( |
73 const content::ChildProcessData& data() const { return data_; } | |
74 bool disconnect_was_alive() const { return disconnect_was_alive_; } | |
75 | |
76 protected: | |
77 explicit BrowserChildProcessHost(content::ProcessType type); | |
78 | |
79 // Derived classes call this to launch the child process asynchronously. | |
80 void Launch( | |
81 #if defined(OS_WIN) | 45 #if defined(OS_WIN) |
82 const FilePath& exposed_dir, | 46 const FilePath& exposed_dir, |
83 #elif defined(OS_POSIX) | 47 #elif defined(OS_POSIX) |
84 bool use_zygote, | 48 bool use_zygote, |
85 const base::environment_vector& environ, | 49 const base::environment_vector& environ, |
86 #endif | 50 #endif |
87 CommandLine* cmd_line); | 51 CommandLine* cmd_line) OVERRIDE; |
| 52 virtual const content::ChildProcessData& GetData() const OVERRIDE; |
| 53 virtual content::ChildProcessHost* GetHost() const OVERRIDE; |
| 54 virtual base::TerminationStatus GetTerminationStatus(int* exit_code) OVERRIDE; |
| 55 virtual void SetName(const string16& name) OVERRIDE; |
| 56 virtual void SetHandle(base::ProcessHandle handle) OVERRIDE; |
88 | 57 |
89 // TODO(jam): below is what will be in the BrowserChildProcessHostDelegate | 58 bool disconnect_was_alive() const { return disconnect_was_alive_; } |
90 // interface. | |
91 | |
92 // ChildProcessLauncher::Client implementation. | |
93 virtual void OnProcessLaunched() OVERRIDE {} | |
94 | |
95 // Derived classes can override this to know if the process crashed. | |
96 // |exit_code| is the status returned when the process crashed (for | |
97 // posix, as returned from waitpid(), for Windows, as returned from | |
98 // GetExitCodeProcess()). | |
99 virtual void OnProcessCrashed(int exit_code) {} | |
100 | |
101 // Overrides from ChildProcessHostDelegate | |
102 virtual bool CanShutdown() OVERRIDE; | |
103 virtual void OnChildDisconnected() OVERRIDE; | |
104 virtual void ShutdownStarted() OVERRIDE; | |
105 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; | |
106 virtual void OnChannelConnected(int32 peer_pid) OVERRIDE; | |
107 | |
108 // Returns the termination status of a child. |exit_code| is the | |
109 // status returned when the process exited (for posix, as returned | |
110 // from waitpid(), for Windows, as returned from | |
111 // GetExitCodeProcess()). |exit_code| may be NULL. | |
112 base::TerminationStatus GetChildTerminationStatus(int* exit_code); | |
113 | 59 |
114 // Returns the handle of the child process. This can be called only after | 60 // Returns the handle of the child process. This can be called only after |
115 // OnProcessLaunched is called or it will be invalid and may crash. | 61 // OnProcessLaunched is called or it will be invalid and may crash. |
116 base::ProcessHandle GetChildProcessHandle() const; | 62 base::ProcessHandle GetHandle() const; |
117 | |
118 // Sets the user-visible name of the process. | |
119 void SetName(const string16& name); | |
120 | |
121 // Set the handle of the process. BrowserChildProcessHost will do this when | |
122 // the Launch method is used to start the process. However if the owner | |
123 // of this object doesn't call Launch and starts the process in another way, | |
124 // they need to call this method so that the process handle is associated with | |
125 // this object. | |
126 void SetHandle(base::ProcessHandle handle); | |
127 | 63 |
128 // Removes this host from the host list. Calls ChildProcessHost::ForceShutdown | 64 // Removes this host from the host list. Calls ChildProcessHost::ForceShutdown |
129 void ForceShutdown(); | 65 void ForceShutdown(); |
130 | 66 |
131 // Controls whether the child process should be terminated on browser | 67 // Controls whether the child process should be terminated on browser |
132 // shutdown. Default is to always terminate. | 68 // shutdown. Default is to always terminate. |
133 void SetTerminateChildOnShutdown(bool terminate_on_shutdown); | 69 void SetTerminateChildOnShutdown(bool terminate_on_shutdown); |
134 | 70 |
135 // Sends the given notification on the UI thread. | 71 // Sends the given notification on the UI thread. |
136 void Notify(int type); | 72 void Notify(int type); |
137 | 73 |
138 content::ChildProcessHost* child_process_host() const { | 74 content::BrowserChildProcessHostDelegate* delegate() const { |
139 return child_process_host_.get(); | 75 return delegate_; |
140 } | 76 } |
141 | 77 |
| 78 typedef std::list<BrowserChildProcessHost*> BrowserChildProcessList; |
142 private: | 79 private: |
143 // By using an internal class as the ChildProcessLauncher::Client, we can | 80 friend class content::BrowserChildProcessHostIterator; |
144 // intercept OnProcessLaunched and do our own processing before | 81 |
145 // calling the subclass' implementation. | 82 static BrowserChildProcessList* GetIterator(); |
146 class ClientHook : public ChildProcessLauncher::Client { | 83 |
147 public: | 84 // ChildProcessHostDelegate implementation: |
148 explicit ClientHook(BrowserChildProcessHost* host); | 85 virtual bool CanShutdown() OVERRIDE; |
149 virtual void OnProcessLaunched() OVERRIDE; | 86 virtual void OnChildDisconnected() OVERRIDE; |
150 private: | 87 virtual void ShutdownStarted() OVERRIDE; |
151 BrowserChildProcessHost* host_; | 88 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; |
152 }; | 89 virtual void OnChannelConnected(int32 peer_pid) OVERRIDE; |
| 90 virtual void OnChannelError() OVERRIDE; |
| 91 |
| 92 // ChildProcessLauncher::Client implementation. |
| 93 virtual void OnProcessLaunched() OVERRIDE; |
| 94 |
| 95 // public base::WaitableEventWatcher::Delegate implementation: |
| 96 virtual void OnWaitableEventSignaled( |
| 97 base::WaitableEvent* waitable_event) OVERRIDE; |
153 | 98 |
154 content::ChildProcessData data_; | 99 content::ChildProcessData data_; |
| 100 content::BrowserChildProcessHostDelegate* delegate_; |
155 scoped_ptr<content::ChildProcessHost> child_process_host_; | 101 scoped_ptr<content::ChildProcessHost> child_process_host_; |
156 | 102 |
157 ClientHook client_; | |
158 scoped_ptr<ChildProcessLauncher> child_process_; | 103 scoped_ptr<ChildProcessLauncher> child_process_; |
159 #if defined(OS_WIN) | 104 #if defined(OS_WIN) |
160 base::WaitableEventWatcher child_watcher_; | 105 base::WaitableEventWatcher child_watcher_; |
161 #else | 106 #else |
162 base::WeakPtrFactory<BrowserChildProcessHost> task_factory_; | 107 base::WeakPtrFactory<BrowserChildProcessHost> task_factory_; |
163 #endif | 108 #endif |
164 bool disconnect_was_alive_; | 109 bool disconnect_was_alive_; |
165 }; | 110 }; |
166 | 111 |
167 #endif // CONTENT_BROWSER_BROWSER_CHILD_PROCESS_HOST_H_ | 112 #endif // CONTENT_BROWSER_BROWSER_CHILD_PROCESS_HOST_H_ |
OLD | NEW |