OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef BASE_MP_CHILD_PROCESS_HOST_H_ |
| 6 #define BASE_MP_CHILD_PROCESS_HOST_H_ |
| 7 |
| 8 #include <list> |
| 9 #include <string> |
| 10 |
| 11 // Must be included early (e.g. before chrome/common/plugin_messages.h) |
| 12 #include "ipc/ipc_logging.h" |
| 13 |
| 14 #include "base/basictypes.h" |
| 15 #include "base/scoped_ptr.h" |
| 16 #include "base/mp/mp_child_process_context.h" |
| 17 #include "base/mp/mp_child_process_launcher.h" |
| 18 #include "ipc/ipc_channel.h" |
| 19 |
| 20 class CommandLine; |
| 21 |
| 22 namespace base { |
| 23 |
| 24 // Plugins/workers and other child processes that live on the IO thread should |
| 25 // derive from this class. |
| 26 // |
| 27 // [Browser]RenderProcessHost is the main exception that doesn't derive from |
| 28 // this class. That project lives on the UI thread. |
| 29 class MpChildProcessHost : public IPC::Channel::Listener, |
| 30 public MpChildProcessLauncher::Client { |
| 31 public: |
| 32 // Notifications that should be forwarded to the UI thread. |
| 33 enum MpNotificationType { |
| 34 CHILD_INSTANCE_CREATED, |
| 35 CHILD_PROCESS_CRASHED, |
| 36 CHILD_PROCESS_HOST_CONNECTED, |
| 37 CHILD_PROCESS_HOST_DISCONNECTED, |
| 38 }; |
| 39 |
| 40 virtual ~MpChildProcessHost(); |
| 41 |
| 42 virtual bool Send(IPC::Message* msg); |
| 43 |
| 44 protected: |
| 45 MpChildProcessHost(MpChildProcessContext* context); |
| 46 |
| 47 // Launches the child process asynchronously. |
| 48 void Launch( |
| 49 #if defined(OS_WIN) |
| 50 const FilePath& exposed_dir, |
| 51 #elif defined(OS_POSIX) |
| 52 bool use_zygote, |
| 53 const base::environment_vector& environ, |
| 54 #endif |
| 55 CommandLine* cmd_line); |
| 56 |
| 57 // Derived classes should send the given notification to the notification |
| 58 // service on the UI thread. |
| 59 virtual void Notify(MpNotificationType type) = 0; |
| 60 |
| 61 // Derived classes override this method to handle messages. |
| 62 virtual bool OnDispatchMessageReceived(const IPC::Message& msg, |
| 63 bool *msg_is_ok) = 0; |
| 64 |
| 65 // Derived classes return the application-specific child process type. |
| 66 virtual int GetType() = 0; |
| 67 |
| 68 // Derived classes return true if it's ok to shut down the child process. |
| 69 virtual bool CanShutdown() = 0; |
| 70 |
| 71 // Send the shutdown message to the child process, and remove this host from |
| 72 // the host list. Does not check if CanShutdown is true. |
| 73 void ForceShutdown(); |
| 74 |
| 75 // Creates the IPC channel. Returns true iff it succeeded. |
| 76 bool CreateChannel(); |
| 77 |
| 78 // Notifies us that an instance has been created on this child process. |
| 79 void InstanceCreated(); |
| 80 |
| 81 // IPC::Channel::Listener implementation: |
| 82 virtual void OnMessageReceived(const IPC::Message& msg) { } |
| 83 virtual void OnChannelConnected(int32 peer_pid) { } |
| 84 virtual void OnChannelError() { } |
| 85 |
| 86 // MpChildProcessLauncher::Client implementation. |
| 87 virtual void OnProcessLaunched() {} |
| 88 |
| 89 // Derived classes can override this to know if the process crashed. |
| 90 virtual void OnProcessCrashed() {} |
| 91 |
| 92 bool opening_channel() { return opening_channel_; } |
| 93 const std::string& channel_id() { return channel_id_; } |
| 94 |
| 95 virtual bool DidChildCrash(); |
| 96 |
| 97 // Called when the child process goes away. |
| 98 virtual void OnChildDied(); |
| 99 |
| 100 // Getter to the process handle. |
| 101 base::ProcessHandle GetHandle() const { return child_process_->GetHandle(); } |
| 102 |
| 103 private: |
| 104 // By using an internal class as the IPC::Channel::Listener, we can intercept |
| 105 // OnMessageReceived/OnChannelConnected and do our own processing before |
| 106 // calling the subclass' implementation. |
| 107 class ListenerHook : public IPC::Channel::Listener, |
| 108 public MpChildProcessLauncher::Client { |
| 109 public: |
| 110 explicit ListenerHook(MpChildProcessHost* host); |
| 111 virtual void OnMessageReceived(const IPC::Message& msg); |
| 112 virtual void OnChannelConnected(int32 peer_pid); |
| 113 virtual void OnChannelError(); |
| 114 virtual void OnProcessLaunched(); |
| 115 private: |
| 116 MpChildProcessHost* host_; |
| 117 }; |
| 118 |
| 119 ListenerHook listener_; |
| 120 |
| 121 bool opening_channel_; // True while we're waiting the channel to be opened. |
| 122 scoped_ptr<IPC::Channel> channel_; |
| 123 std::string channel_id_; |
| 124 scoped_ptr<MpChildProcessLauncher> child_process_; |
| 125 MpChildProcessContext* context_; |
| 126 }; |
| 127 |
| 128 } // namespace base |
| 129 |
| 130 #endif // BASE_MP_CHILD_PROCESS_HOST_H_ |
OLD | NEW |