OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 CHROME_BROWSER_BROWSER_CHILD_PROCESS_HOST_H_ | 5 #ifndef CHROME_BROWSER_BROWSER_CHILD_PROCESS_HOST_H_ |
6 #define CHROME_BROWSER_BROWSER_CHILD_PROCESS_HOST_H_ | 6 #define CHROME_BROWSER_BROWSER_CHILD_PROCESS_HOST_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <list> | 9 #include <list> |
10 | 10 |
11 #include "chrome/browser/child_process_launcher.h" | 11 #include "chrome/browser/child_process_launcher.h" |
12 #include "chrome/browser/renderer_host/resource_dispatcher_host.h" | 12 #include "chrome/browser/renderer_host/resource_message_filter.h" |
13 #include "chrome/common/child_process_host.h" | 13 #include "chrome/common/child_process_host.h" |
| 14 #include "chrome/common/child_process_info.h" |
14 | 15 |
| 16 class ResourceDispatcherHost; |
15 | 17 |
16 // Plugins/workers and other child processes that live on the IO thread should | 18 // Plugins/workers and other child processes that live on the IO thread should |
17 // derive from this class. | 19 // derive from this class. |
18 // | 20 // |
19 // [Browser]RenderProcessHost is the main exception that doesn't derive from | 21 // [Browser]RenderProcessHost is the main exception that doesn't derive from |
20 // this class. That project lives on the UI thread. | 22 // this class. That project lives on the UI thread. |
21 class BrowserChildProcessHost : public ResourceDispatcherHost::Receiver, | 23 class BrowserChildProcessHost : public ChildProcessHost, |
22 public ChildProcessHost, | 24 public ChildProcessInfo, |
23 public ChildProcessLauncher::Client { | 25 public ChildProcessLauncher::Client { |
24 public: | 26 public: |
25 virtual ~BrowserChildProcessHost(); | 27 virtual ~BrowserChildProcessHost(); |
26 | 28 |
27 // Prepares command_line for crash reporting as appropriate. On Linux and | 29 // Prepares command_line for crash reporting as appropriate. On Linux and |
28 // Mac, a command-line flag to enable crash reporting in the child process | 30 // Mac, a command-line flag to enable crash reporting in the child process |
29 // will be appended if needed, because the child process may not have access | 31 // will be appended if needed, because the child process may not have access |
30 // to the data that determines the status of crash reporting in the | 32 // to the data that determines the status of crash reporting in the |
31 // currently-executing process. This function is a no-op on Windows. | 33 // currently-executing process. This function is a no-op on Windows. |
32 static void SetCrashReporterCommandLine(CommandLine* command_line); | 34 static void SetCrashReporterCommandLine(CommandLine* command_line); |
33 | 35 |
34 // Terminates all child processes and deletes each ChildProcessHost instance. | 36 // Terminates all child processes and deletes each ChildProcessHost instance. |
35 static void TerminateAll(); | 37 static void TerminateAll(); |
36 | 38 |
37 // ResourceDispatcherHost::Receiver implementation: | |
38 virtual bool Send(IPC::Message* msg); | |
39 | |
40 // The Iterator class allows iteration through either all child processes, or | 39 // The Iterator class allows iteration through either all child processes, or |
41 // ones of a specific type, depending on which constructor is used. Note that | 40 // ones of a specific type, depending on which constructor is used. Note that |
42 // this should be done from the IO thread and that the iterator should not be | 41 // this should be done from the IO thread and that the iterator should not be |
43 // kept around as it may be invalidated on subsequent event processing in the | 42 // kept around as it may be invalidated on subsequent event processing in the |
44 // event loop. | 43 // event loop. |
45 class Iterator { | 44 class Iterator { |
46 public: | 45 public: |
47 Iterator(); | 46 Iterator(); |
48 explicit Iterator(ProcessType type); | 47 explicit Iterator(ChildProcessInfo::ProcessType type); |
49 BrowserChildProcessHost* operator->() { return *iterator_; } | 48 BrowserChildProcessHost* operator->() { return *iterator_; } |
50 BrowserChildProcessHost* operator*() { return *iterator_; } | 49 BrowserChildProcessHost* operator*() { return *iterator_; } |
51 BrowserChildProcessHost* operator++(); | 50 BrowserChildProcessHost* operator++(); |
52 bool Done(); | 51 bool Done(); |
53 | 52 |
54 private: | 53 private: |
55 bool all_; | 54 bool all_; |
56 ProcessType type_; | 55 ChildProcessInfo::ProcessType type_; |
57 std::list<BrowserChildProcessHost*>::iterator iterator_; | 56 std::list<BrowserChildProcessHost*>::iterator iterator_; |
58 }; | 57 }; |
59 | 58 |
60 protected: | 59 protected: |
61 // The resource_dispatcher_host may be NULL to indicate none is needed for | 60 // |resource_dispatcher_host| may be NULL to indicate none is needed for |
62 // this process type. | 61 // this process type. |
63 BrowserChildProcessHost(ProcessType type, | 62 // |url_request_context_getter| allows derived classes to override the |
64 ResourceDispatcherHost* resource_dispatcher_host); | 63 // URLRequestContext. |
| 64 BrowserChildProcessHost( |
| 65 ChildProcessInfo::ProcessType type, |
| 66 ResourceDispatcherHost* resource_dispatcher_host, |
| 67 ResourceMessageFilter::URLRequestContextOverride* |
| 68 url_request_context_override); |
| 69 |
| 70 // A convenient constructor for those classes that want to use the default |
| 71 // URLRequestContext. |
| 72 BrowserChildProcessHost( |
| 73 ChildProcessInfo::ProcessType type, |
| 74 ResourceDispatcherHost* resource_dispatcher_host); |
65 | 75 |
66 // Derived classes call this to launch the child process asynchronously. | 76 // Derived classes call this to launch the child process asynchronously. |
67 void Launch( | 77 void Launch( |
68 #if defined(OS_WIN) | 78 #if defined(OS_WIN) |
69 const FilePath& exposed_dir, | 79 const FilePath& exposed_dir, |
70 #elif defined(OS_POSIX) | 80 #elif defined(OS_POSIX) |
71 bool use_zygote, | 81 bool use_zygote, |
72 const base::environment_vector& environ, | 82 const base::environment_vector& environ, |
73 #endif | 83 #endif |
74 CommandLine* cmd_line); | 84 CommandLine* cmd_line); |
75 | 85 |
76 // Returns the handle of the child process. This must be called only after | 86 // Returns the handle of the child process. This can be called only after |
77 // OnProcessLaunched is called or it will be invalid and may crash. | 87 // OnProcessLaunched is called or it will be invalid and may crash. |
78 base::ProcessHandle GetChildProcessHandle() const; | 88 base::ProcessHandle GetChildProcessHandle() const; |
79 | 89 |
80 // ChildProcessLauncher::Client implementation. | 90 // ChildProcessLauncher::Client implementation. |
81 virtual void OnProcessLaunched() {} | 91 virtual void OnProcessLaunched() {} |
82 | 92 |
83 // Derived classes can override this to know if the process crashed. | 93 // Derived classes can override this to know if the process crashed. |
84 // |exit_code| is the status returned when the process crashed (for | 94 // |exit_code| is the status returned when the process crashed (for |
85 // posix, as returned from waitpid(), for Windows, as returned from | 95 // posix, as returned from waitpid(), for Windows, as returned from |
86 // GetExitCodeProcess()). | 96 // GetExitCodeProcess()). |
87 virtual void OnProcessCrashed(int exit_code) {} | 97 virtual void OnProcessCrashed(int exit_code) {} |
88 | 98 |
89 // Derived classes can override this to know if the process was | 99 // Derived classes can override this to know if the process was |
90 // killed. |exit_code| is the status returned when the process | 100 // killed. |exit_code| is the status returned when the process |
91 // was killed (for posix, as returned from waitpid(), for Windows, | 101 // was killed (for posix, as returned from waitpid(), for Windows, |
92 // as returned from GetExitCodeProcess()). | 102 // as returned from GetExitCodeProcess()). |
93 virtual void OnProcessWasKilled(int exit_code) {} | 103 virtual void OnProcessWasKilled(int exit_code) {} |
94 | 104 |
95 // Returns the termination status of a child. |exit_code| is the | 105 // Returns the termination status of a child. |exit_code| is the |
96 // status returned when the process exited (for posix, as returned | 106 // status returned when the process exited (for posix, as returned |
97 // from waitpid(), for Windows, as returned from | 107 // from waitpid(), for Windows, as returned from |
98 // GetExitCodeProcess()). |exit_code| may be NULL. | 108 // GetExitCodeProcess()). |exit_code| may be NULL. |
99 virtual base::TerminationStatus GetChildTerminationStatus(int* exit_code); | 109 virtual base::TerminationStatus GetChildTerminationStatus(int* exit_code); |
100 | 110 |
101 // Overrides from ChildProcessHost | 111 // Overrides from ChildProcessHost |
102 virtual void OnChildDied(); | 112 virtual void OnChildDied(); |
103 virtual bool InterceptMessageFromChild(const IPC::Message& msg); | 113 virtual void ShutdownStarted(); |
104 virtual void Notify(NotificationType type); | 114 virtual void Notify(NotificationType type); |
105 // Extends the base class implementation and removes this host from | 115 // Extends the base class implementation and removes this host from |
106 // the host list. Calls ChildProcessHost::ForceShutdown | 116 // the host list. Calls ChildProcessHost::ForceShutdown |
107 virtual void ForceShutdown(); | 117 virtual void ForceShutdown(); |
108 | 118 |
109 private: | 119 private: |
| 120 void Initialize(ResourceMessageFilter::URLRequestContextOverride* |
| 121 url_request_context_override); |
| 122 |
110 // By using an internal class as the ChildProcessLauncher::Client, we can | 123 // By using an internal class as the ChildProcessLauncher::Client, we can |
111 // intercept OnProcessLaunched and do our own processing before | 124 // intercept OnProcessLaunched and do our own processing before |
112 // calling the subclass' implementation. | 125 // calling the subclass' implementation. |
113 class ClientHook : public ChildProcessLauncher::Client { | 126 class ClientHook : public ChildProcessLauncher::Client { |
114 public: | 127 public: |
115 explicit ClientHook(BrowserChildProcessHost* host); | 128 explicit ClientHook(BrowserChildProcessHost* host); |
116 virtual void OnProcessLaunched(); | 129 virtual void OnProcessLaunched(); |
117 private: | 130 private: |
118 BrowserChildProcessHost* host_; | 131 BrowserChildProcessHost* host_; |
119 }; | 132 }; |
120 ClientHook client_; | 133 ClientHook client_; |
121 // May be NULL if this current process has no resource dispatcher host. | 134 // May be NULL if this current process has no resource dispatcher host. |
122 ResourceDispatcherHost* resource_dispatcher_host_; | 135 ResourceDispatcherHost* resource_dispatcher_host_; |
123 scoped_ptr<ChildProcessLauncher> child_process_; | 136 scoped_ptr<ChildProcessLauncher> child_process_; |
124 }; | 137 }; |
125 | 138 |
126 #endif // CHROME_BROWSER_BROWSER_CHILD_PROCESS_HOST_H_ | 139 #endif // CHROME_BROWSER_BROWSER_CHILD_PROCESS_HOST_H_ |
OLD | NEW |