1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 // Copyright (c) 2009 The Chromium Authors. All rights reserved. 4 // Use of this source code is governed by a BSD-style license that can be 5 // found in the LICENSE file. 6 7 #ifndef CHROME_COMMON_CHILD_PROCESS_HOST_H_ 8 #define CHROME_COMMON_CHILD_PROCESS_HOST_H_ 9 10 #include "build/build_config.h" 11 12 #include <list> 13 14 #include "base/basictypes.h" 15 #include "chrome/common/ipc_channel.h" 16 #include "mozilla/UniquePtr.h" 17 18 namespace mozilla { 19 namespace ipc { 20 class FileDescriptor; 21 } 22 } // namespace mozilla 23 24 // Plugins/workers and other child processes that live on the IO thread should 25 // derive from this class. 26 class ChildProcessHost : public IPC::Channel::Listener { 27 public: 28 virtual ~ChildProcessHost(); 29 30 using ChannelId = IPC::Channel::ChannelId; 31 32 protected: 33 explicit ChildProcessHost(); 34 35 // Derived classes return true if it's ok to shut down the child process. 36 virtual bool CanShutdown() = 0; 37 38 // Creates the IPC channel. Returns true iff it succeeded. 39 bool CreateChannel(); 40 41 // IPC::Channel::Listener implementation: OnMessageReceived(IPC::Message && msg)42 virtual void OnMessageReceived(IPC::Message&& msg) override {} OnChannelConnected(int32_t peer_pid)43 virtual void OnChannelConnected(int32_t peer_pid) override {} OnChannelError()44 virtual void OnChannelError() override {} 45 opening_channel()46 bool opening_channel() { return opening_channel_; } channel_id()47 const ChannelId& channel_id() { return channel_id_; } 48 channelp()49 IPC::Channel* channelp() const { return channel_.get(); } TakeChannel()50 mozilla::UniquePtr<IPC::Channel> TakeChannel() { return std::move(channel_); } 51 52 private: 53 // By using an internal class as the IPC::Channel::Listener, we can intercept 54 // OnMessageReceived/OnChannelConnected and do our own processing before 55 // calling the subclass' implementation. 56 class ListenerHook : public IPC::Channel::Listener { 57 public: 58 explicit ListenerHook(ChildProcessHost* host); 59 virtual void OnMessageReceived(IPC::Message&& msg) override; 60 virtual void OnChannelConnected(int32_t peer_pid) override; 61 virtual void OnChannelError() override; 62 virtual void GetQueuedMessages(std::queue<IPC::Message>& queue) override; 63 64 private: 65 ChildProcessHost* host_; 66 }; 67 68 ListenerHook listener_; 69 70 // True while we're waiting the channel to be opened. 71 bool opening_channel_; 72 73 // The IPC::Channel. 74 mozilla::UniquePtr<IPC::Channel> channel_; 75 76 // IPC Channel's id. 77 ChannelId channel_id_; 78 }; 79 80 #endif // CHROME_COMMON_CHILD_PROCESS_HOST_H_ 81