Index: base/mp/mp_child_process_host.h |
=================================================================== |
--- base/mp/mp_child_process_host.h (revision 0) |
+++ base/mp/mp_child_process_host.h (revision 0) |
@@ -0,0 +1,130 @@ |
+// Copyright (c) 2010 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#ifndef BASE_MP_CHILD_PROCESS_HOST_H_ |
+#define BASE_MP_CHILD_PROCESS_HOST_H_ |
+ |
+#include <list> |
+#include <string> |
+ |
+// Must be included early (e.g. before chrome/common/plugin_messages.h) |
+#include "ipc/ipc_logging.h" |
+ |
+#include "base/basictypes.h" |
+#include "base/scoped_ptr.h" |
+#include "base/mp/mp_child_process_context.h" |
+#include "base/mp/mp_child_process_launcher.h" |
+#include "ipc/ipc_channel.h" |
+ |
+class CommandLine; |
+ |
+namespace base { |
+ |
+// Plugins/workers and other child processes that live on the IO thread should |
+// derive from this class. |
+// |
+// [Browser]RenderProcessHost is the main exception that doesn't derive from |
+// this class. That project lives on the UI thread. |
+class MpChildProcessHost : public IPC::Channel::Listener, |
+ public MpChildProcessLauncher::Client { |
+ public: |
+ // Notifications that should be forwarded to the UI thread. |
+ enum MpNotificationType { |
+ CHILD_INSTANCE_CREATED, |
+ CHILD_PROCESS_CRASHED, |
+ CHILD_PROCESS_HOST_CONNECTED, |
+ CHILD_PROCESS_HOST_DISCONNECTED, |
+ }; |
+ |
+ virtual ~MpChildProcessHost(); |
+ |
+ virtual bool Send(IPC::Message* msg); |
+ |
+ protected: |
+ MpChildProcessHost(MpChildProcessContext* context); |
+ |
+ // Launches the child process asynchronously. |
+ void Launch( |
+#if defined(OS_WIN) |
+ const FilePath& exposed_dir, |
+#elif defined(OS_POSIX) |
+ bool use_zygote, |
+ const base::environment_vector& environ, |
+#endif |
+ CommandLine* cmd_line); |
+ |
+ // Derived classes should send the given notification to the notification |
+ // service on the UI thread. |
+ virtual void Notify(MpNotificationType type) = 0; |
+ |
+ // Derived classes override this method to handle messages. |
+ virtual bool OnDispatchMessageReceived(const IPC::Message& msg, |
+ bool *msg_is_ok) = 0; |
+ |
+ // Derived classes return the application-specific child process type. |
+ virtual int GetType() = 0; |
+ |
+ // Derived classes return true if it's ok to shut down the child process. |
+ virtual bool CanShutdown() = 0; |
+ |
+ // Send the shutdown message to the child process, and remove this host from |
+ // the host list. Does not check if CanShutdown is true. |
+ void ForceShutdown(); |
+ |
+ // Creates the IPC channel. Returns true iff it succeeded. |
+ bool CreateChannel(); |
+ |
+ // Notifies us that an instance has been created on this child process. |
+ void InstanceCreated(); |
+ |
+ // IPC::Channel::Listener implementation: |
+ virtual void OnMessageReceived(const IPC::Message& msg) { } |
+ virtual void OnChannelConnected(int32 peer_pid) { } |
+ virtual void OnChannelError() { } |
+ |
+ // MpChildProcessLauncher::Client implementation. |
+ virtual void OnProcessLaunched() {} |
+ |
+ // Derived classes can override this to know if the process crashed. |
+ virtual void OnProcessCrashed() {} |
+ |
+ bool opening_channel() { return opening_channel_; } |
+ const std::string& channel_id() { return channel_id_; } |
+ |
+ virtual bool DidChildCrash(); |
+ |
+ // Called when the child process goes away. |
+ virtual void OnChildDied(); |
+ |
+ // Getter to the process handle. |
+ base::ProcessHandle GetHandle() const { return child_process_->GetHandle(); } |
+ |
+ private: |
+ // By using an internal class as the IPC::Channel::Listener, we can intercept |
+ // OnMessageReceived/OnChannelConnected and do our own processing before |
+ // calling the subclass' implementation. |
+ class ListenerHook : public IPC::Channel::Listener, |
+ public MpChildProcessLauncher::Client { |
+ public: |
+ explicit ListenerHook(MpChildProcessHost* host); |
+ virtual void OnMessageReceived(const IPC::Message& msg); |
+ virtual void OnChannelConnected(int32 peer_pid); |
+ virtual void OnChannelError(); |
+ virtual void OnProcessLaunched(); |
+ private: |
+ MpChildProcessHost* host_; |
+ }; |
+ |
+ ListenerHook listener_; |
+ |
+ bool opening_channel_; // True while we're waiting the channel to be opened. |
+ scoped_ptr<IPC::Channel> channel_; |
+ std::string channel_id_; |
+ scoped_ptr<MpChildProcessLauncher> child_process_; |
+ MpChildProcessContext* context_; |
+}; |
+ |
+} // namespace base |
+ |
+#endif // BASE_MP_CHILD_PROCESS_HOST_H_ |