| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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_CHILD_PROCESS_HOST_H_ | 5 #ifndef CHROME_BROWSER_CHILD_PROCESS_HOST_H_ |
| 6 #define CHROME_BROWSER_CHILD_PROCESS_HOST_H_ | 6 #define CHROME_BROWSER_CHILD_PROCESS_HOST_H_ |
| 7 | 7 |
| 8 #include <list> | 8 #include <list> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 // Must be included early (e.g. before chrome/common/plugin_messages.h) | 11 // Must be included early (e.g. before chrome/common/plugin_messages.h) |
| 12 #include "ipc/ipc_logging.h" | 12 #include "ipc/ipc_logging.h" |
| 13 | 13 |
| 14 #include "base/basictypes.h" | 14 #include "base/basictypes.h" |
| 15 #include "base/mp/mp_child_process_host.h" |
| 16 #include "base/mp/mp_child_process_launcher.h" |
| 15 #include "base/scoped_ptr.h" | 17 #include "base/scoped_ptr.h" |
| 16 #include "chrome/browser/child_process_launcher.h" | 18 #include "chrome/browser/child_process_context.h" |
| 17 #include "chrome/browser/renderer_host/resource_dispatcher_host.h" | 19 #include "chrome/browser/renderer_host/resource_dispatcher_host.h" |
| 18 #include "ipc/ipc_channel.h" | 20 #include "ipc/ipc_channel.h" |
| 19 | 21 |
| 20 class CommandLine; | 22 class CommandLine; |
| 21 class NotificationType; | 23 class NotificationType; |
| 22 | 24 |
| 23 // Plugins/workers and other child processes that live on the IO thread should | 25 // Plugins/workers and other child processes that live on the IO thread should |
| 24 // derive from this class. | 26 // derive from this class. |
| 25 // | 27 // |
| 26 // [Browser]RenderProcessHost is the main exception that doesn't derive from | 28 // [Browser]RenderProcessHost is the main exception that doesn't derive from |
| 27 // this class. That project lives on the UI thread. | 29 // this class. That project lives on the UI thread. |
| 28 class ChildProcessHost : public ResourceDispatcherHost::Receiver, | 30 class ChildProcessHost : public ResourceDispatcherHost::Receiver, |
| 29 public IPC::Channel::Listener, | 31 public base::MpChildProcessHost { |
| 30 public ChildProcessLauncher::Client { | |
| 31 public: | 32 public: |
| 32 virtual ~ChildProcessHost(); | 33 virtual ~ChildProcessHost(); |
| 33 | 34 |
| 34 // Returns the pathname to be used for a child process. If a subprocess | 35 // Returns the pathname to be used for a child process. If a subprocess |
| 35 // pathname was specified on the command line, that will be used. Otherwise, | 36 // pathname was specified on the command line, that will be used. Otherwise, |
| 36 // the default child process pathname will be returned. On most platforms, | 37 // the default child process pathname will be returned. On most platforms, |
| 37 // this will be the same as the currently-executing process. | 38 // this will be the same as the currently-executing process. |
| 38 // | 39 // |
| 39 // The argument allow_self is used on Linux to indicate that we allow us to | 40 // The argument allow_self is used on Linux to indicate that we allow us to |
| 40 // fork from /proc/self/exe rather than using the "real" app path. This | 41 // fork from /proc/self/exe rather than using the "real" app path. This |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 76 ProcessType type_; | 77 ProcessType type_; |
| 77 std::list<ChildProcessHost*>::iterator iterator_; | 78 std::list<ChildProcessHost*>::iterator iterator_; |
| 78 }; | 79 }; |
| 79 | 80 |
| 80 protected: | 81 protected: |
| 81 // The resource_dispatcher_host may be NULL to indicate none is needed for | 82 // The resource_dispatcher_host may be NULL to indicate none is needed for |
| 82 // this process type. | 83 // this process type. |
| 83 ChildProcessHost(ProcessType type, | 84 ChildProcessHost(ProcessType type, |
| 84 ResourceDispatcherHost* resource_dispatcher_host); | 85 ResourceDispatcherHost* resource_dispatcher_host); |
| 85 | 86 |
| 86 // Derived classes call this to launch the child process asynchronously. | 87 private: |
| 87 void Launch( | 88 // base::MpChildProcessHost implementation: |
| 88 #if defined(OS_WIN) | 89 virtual void Notify(base::MpChildProcessHost::MpNotificationType type); |
| 89 const FilePath& exposed_dir, | |
| 90 #elif defined(OS_POSIX) | |
| 91 bool use_zygote, | |
| 92 const base::environment_vector& environ, | |
| 93 #endif | |
| 94 CommandLine* cmd_line); | |
| 95 | 90 |
| 96 // Derived classes return true if it's ok to shut down the child process. | 91 virtual bool OnDispatchMessageReceived(const IPC::Message& msg, |
| 97 virtual bool CanShutdown() = 0; | 92 bool *msg_is_ok); |
| 98 | 93 |
| 99 // Send the shutdown message to the child process, and remove this host from | 94 virtual void OnProcessLaunched(); |
| 100 // the host list. Does not check if CanShutdown is true. | |
| 101 void ForceShutdown(); | |
| 102 | 95 |
| 103 // Creates the IPC channel. Returns true iff it succeeded. | 96 virtual int GetType(); |
| 104 bool CreateChannel(); | |
| 105 | |
| 106 // Notifies us that an instance has been created on this child process. | |
| 107 void InstanceCreated(); | |
| 108 | |
| 109 // IPC::Channel::Listener implementation: | |
| 110 virtual void OnMessageReceived(const IPC::Message& msg) { } | |
| 111 virtual void OnChannelConnected(int32 peer_pid) { } | |
| 112 virtual void OnChannelError() { } | |
| 113 | |
| 114 // ChildProcessLauncher::Client implementation. | |
| 115 virtual void OnProcessLaunched() {} | |
| 116 | |
| 117 // Derived classes can override this to know if the process crashed. | |
| 118 virtual void OnProcessCrashed() {} | |
| 119 | |
| 120 bool opening_channel() { return opening_channel_; } | |
| 121 const std::string& channel_id() { return channel_id_; } | |
| 122 | |
| 123 virtual bool DidChildCrash(); | |
| 124 | |
| 125 // Called when the child process goes away. | |
| 126 virtual void OnChildDied(); | |
| 127 | |
| 128 private: | |
| 129 // Sends the given notification to the notification service on the UI thread. | |
| 130 void Notify(NotificationType type); | |
| 131 | |
| 132 // By using an internal class as the IPC::Channel::Listener, we can intercept | |
| 133 // OnMessageReceived/OnChannelConnected and do our own processing before | |
| 134 // calling the subclass' implementation. | |
| 135 class ListenerHook : public IPC::Channel::Listener, | |
| 136 public ChildProcessLauncher::Client { | |
| 137 public: | |
| 138 explicit ListenerHook(ChildProcessHost* host); | |
| 139 virtual void OnMessageReceived(const IPC::Message& msg); | |
| 140 virtual void OnChannelConnected(int32 peer_pid); | |
| 141 virtual void OnChannelError(); | |
| 142 virtual void OnProcessLaunched(); | |
| 143 private: | |
| 144 ChildProcessHost* host_; | |
| 145 }; | |
| 146 | |
| 147 ListenerHook listener_; | |
| 148 | 97 |
| 149 // May be NULL if this current process has no resource dispatcher host. | 98 // May be NULL if this current process has no resource dispatcher host. |
| 150 ResourceDispatcherHost* resource_dispatcher_host_; | 99 ResourceDispatcherHost* resource_dispatcher_host_; |
| 151 | 100 ChildProcessContext context_; |
| 152 bool opening_channel_; // True while we're waiting the channel to be opened. | |
| 153 scoped_ptr<IPC::Channel> channel_; | |
| 154 std::string channel_id_; | |
| 155 scoped_ptr<ChildProcessLauncher> child_process_; | |
| 156 }; | 101 }; |
| 157 | 102 |
| 158 #endif // CHROME_BROWSER_CHILD_PROCESS_HOST_H_ | 103 #endif // CHROME_BROWSER_CHILD_PROCESS_HOST_H_ |
| OLD | NEW |