1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/ipc/chromium/src/chrome/common/child_process_host.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,130 @@ 1.4 +// Copyright (c) 2009 The Chromium Authors. All rights reserved. 1.5 +// Use of this source code is governed by a BSD-style license that can be 1.6 +// found in the LICENSE file. 1.7 + 1.8 +#ifndef CHROME_COMMON_CHILD_PROCESS_HOST_H_ 1.9 +#define CHROME_COMMON_CHILD_PROCESS_HOST_H_ 1.10 + 1.11 +#include "build/build_config.h" 1.12 + 1.13 +#include <list> 1.14 + 1.15 +#include "base/basictypes.h" 1.16 +#include "base/scoped_ptr.h" 1.17 +#include "base/waitable_event_watcher.h" 1.18 +#include "chrome/common/child_process_info.h" 1.19 +#include "chrome/common/ipc_channel.h" 1.20 + 1.21 +namespace mozilla { 1.22 +namespace ipc { 1.23 +class FileDescriptor; 1.24 +} 1.25 +} 1.26 + 1.27 +class NotificationType; 1.28 + 1.29 +// Plugins/workers and other child processes that live on the IO thread should 1.30 +// derive from this class. 1.31 +class ChildProcessHost : 1.32 + public IPC::Message::Sender, 1.33 + public ChildProcessInfo, 1.34 + public base::WaitableEventWatcher::Delegate, 1.35 + public IPC::Channel::Listener { 1.36 + public: 1.37 + virtual ~ChildProcessHost(); 1.38 + 1.39 + // ResourceDispatcherHost::Receiver implementation: 1.40 + virtual bool Send(IPC::Message* msg); 1.41 + 1.42 + // The Iterator class allows iteration through either all child processes, or 1.43 + // ones of a specific type, depending on which constructor is used. Note that 1.44 + // this should be done from the IO thread and that the iterator should not be 1.45 + // kept around as it may be invalidated on subsequent event processing in the 1.46 + // event loop. 1.47 + class Iterator { 1.48 + public: 1.49 + Iterator(); 1.50 + Iterator(ProcessType type); 1.51 + ChildProcessHost* operator->() { return *iterator_; } 1.52 + ChildProcessHost* operator*() { return *iterator_; } 1.53 + ChildProcessHost* operator++(); 1.54 + bool Done(); 1.55 + 1.56 + private: 1.57 + bool all_; 1.58 + ProcessType type_; 1.59 + std::list<ChildProcessHost*>::iterator iterator_; 1.60 + }; 1.61 + 1.62 + protected: 1.63 + explicit ChildProcessHost(ProcessType type); 1.64 + 1.65 + // Derived classes return true if it's ok to shut down the child process. 1.66 + virtual bool CanShutdown() = 0; 1.67 + 1.68 + // Creates the IPC channel. Returns true iff it succeeded. 1.69 + bool CreateChannel(); 1.70 + 1.71 + bool CreateChannel(mozilla::ipc::FileDescriptor& aFileDescriptor); 1.72 + 1.73 + // Once the subclass gets a handle to the process, it needs to tell 1.74 + // ChildProcessHost using this function. 1.75 + void SetHandle(base::ProcessHandle handle); 1.76 + 1.77 + // Notifies us that an instance has been created on this child process. 1.78 + void InstanceCreated(); 1.79 + 1.80 + // IPC::Channel::Listener implementation: 1.81 + virtual void OnMessageReceived(const IPC::Message& msg) { } 1.82 + virtual void OnChannelConnected(int32_t peer_pid) { } 1.83 + virtual void OnChannelError() { } 1.84 + 1.85 + bool opening_channel() { return opening_channel_; } 1.86 + const std::wstring& channel_id() { return channel_id_; } 1.87 + 1.88 + base::WaitableEvent* GetProcessEvent() { return process_event_.get(); } 1.89 + 1.90 + const IPC::Channel& channel() const { return *channel_; } 1.91 + IPC::Channel* channelp() const { return channel_.get(); } 1.92 + 1.93 + private: 1.94 + // Sends the given notification to the notification service on the UI thread. 1.95 + void Notify(NotificationType type); 1.96 + 1.97 + protected: 1.98 + // WaitableEventWatcher::Delegate implementation: 1.99 + virtual void OnWaitableEventSignaled(base::WaitableEvent *event); 1.100 + 1.101 + private: 1.102 + // By using an internal class as the IPC::Channel::Listener, we can intercept 1.103 + // OnMessageReceived/OnChannelConnected and do our own processing before 1.104 + // calling the subclass' implementation. 1.105 + class ListenerHook : public IPC::Channel::Listener { 1.106 + public: 1.107 + ListenerHook(ChildProcessHost* host); 1.108 + virtual void OnMessageReceived(const IPC::Message& msg); 1.109 + virtual void OnChannelConnected(int32_t peer_pid); 1.110 + virtual void OnChannelError(); 1.111 + virtual void GetQueuedMessages(std::queue<IPC::Message>& queue); 1.112 + private: 1.113 + ChildProcessHost* host_; 1.114 + }; 1.115 + 1.116 + ListenerHook listener_; 1.117 + 1.118 + // True while we're waiting the channel to be opened. 1.119 + bool opening_channel_; 1.120 + 1.121 + // The IPC::Channel. 1.122 + scoped_ptr<IPC::Channel> channel_; 1.123 + 1.124 + // IPC Channel's id. 1.125 + std::wstring channel_id_; 1.126 + 1.127 + // Used to watch the child process handle. 1.128 + base::WaitableEventWatcher watcher_; 1.129 + 1.130 + scoped_ptr<base::WaitableEvent> process_event_; 1.131 +}; 1.132 + 1.133 +#endif // CHROME_COMMON_CHILD_PROCESS_HOST_H_