Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
michael@0 | 2 | // Use of this source code is governed by a BSD-style license that can be |
michael@0 | 3 | // found in the LICENSE file. |
michael@0 | 4 | |
michael@0 | 5 | #ifndef CHROME_COMMON_CHILD_PROCESS_HOST_H_ |
michael@0 | 6 | #define CHROME_COMMON_CHILD_PROCESS_HOST_H_ |
michael@0 | 7 | |
michael@0 | 8 | #include "build/build_config.h" |
michael@0 | 9 | |
michael@0 | 10 | #include <list> |
michael@0 | 11 | |
michael@0 | 12 | #include "base/basictypes.h" |
michael@0 | 13 | #include "base/scoped_ptr.h" |
michael@0 | 14 | #include "base/waitable_event_watcher.h" |
michael@0 | 15 | #include "chrome/common/child_process_info.h" |
michael@0 | 16 | #include "chrome/common/ipc_channel.h" |
michael@0 | 17 | |
michael@0 | 18 | namespace mozilla { |
michael@0 | 19 | namespace ipc { |
michael@0 | 20 | class FileDescriptor; |
michael@0 | 21 | } |
michael@0 | 22 | } |
michael@0 | 23 | |
michael@0 | 24 | class NotificationType; |
michael@0 | 25 | |
michael@0 | 26 | // Plugins/workers and other child processes that live on the IO thread should |
michael@0 | 27 | // derive from this class. |
michael@0 | 28 | class ChildProcessHost : |
michael@0 | 29 | public IPC::Message::Sender, |
michael@0 | 30 | public ChildProcessInfo, |
michael@0 | 31 | public base::WaitableEventWatcher::Delegate, |
michael@0 | 32 | public IPC::Channel::Listener { |
michael@0 | 33 | public: |
michael@0 | 34 | virtual ~ChildProcessHost(); |
michael@0 | 35 | |
michael@0 | 36 | // ResourceDispatcherHost::Receiver implementation: |
michael@0 | 37 | virtual bool Send(IPC::Message* msg); |
michael@0 | 38 | |
michael@0 | 39 | // The Iterator class allows iteration through either all child processes, or |
michael@0 | 40 | // ones of a specific type, depending on which constructor is used. Note that |
michael@0 | 41 | // this should be done from the IO thread and that the iterator should not be |
michael@0 | 42 | // kept around as it may be invalidated on subsequent event processing in the |
michael@0 | 43 | // event loop. |
michael@0 | 44 | class Iterator { |
michael@0 | 45 | public: |
michael@0 | 46 | Iterator(); |
michael@0 | 47 | Iterator(ProcessType type); |
michael@0 | 48 | ChildProcessHost* operator->() { return *iterator_; } |
michael@0 | 49 | ChildProcessHost* operator*() { return *iterator_; } |
michael@0 | 50 | ChildProcessHost* operator++(); |
michael@0 | 51 | bool Done(); |
michael@0 | 52 | |
michael@0 | 53 | private: |
michael@0 | 54 | bool all_; |
michael@0 | 55 | ProcessType type_; |
michael@0 | 56 | std::list<ChildProcessHost*>::iterator iterator_; |
michael@0 | 57 | }; |
michael@0 | 58 | |
michael@0 | 59 | protected: |
michael@0 | 60 | explicit ChildProcessHost(ProcessType type); |
michael@0 | 61 | |
michael@0 | 62 | // Derived classes return true if it's ok to shut down the child process. |
michael@0 | 63 | virtual bool CanShutdown() = 0; |
michael@0 | 64 | |
michael@0 | 65 | // Creates the IPC channel. Returns true iff it succeeded. |
michael@0 | 66 | bool CreateChannel(); |
michael@0 | 67 | |
michael@0 | 68 | bool CreateChannel(mozilla::ipc::FileDescriptor& aFileDescriptor); |
michael@0 | 69 | |
michael@0 | 70 | // Once the subclass gets a handle to the process, it needs to tell |
michael@0 | 71 | // ChildProcessHost using this function. |
michael@0 | 72 | void SetHandle(base::ProcessHandle handle); |
michael@0 | 73 | |
michael@0 | 74 | // Notifies us that an instance has been created on this child process. |
michael@0 | 75 | void InstanceCreated(); |
michael@0 | 76 | |
michael@0 | 77 | // IPC::Channel::Listener implementation: |
michael@0 | 78 | virtual void OnMessageReceived(const IPC::Message& msg) { } |
michael@0 | 79 | virtual void OnChannelConnected(int32_t peer_pid) { } |
michael@0 | 80 | virtual void OnChannelError() { } |
michael@0 | 81 | |
michael@0 | 82 | bool opening_channel() { return opening_channel_; } |
michael@0 | 83 | const std::wstring& channel_id() { return channel_id_; } |
michael@0 | 84 | |
michael@0 | 85 | base::WaitableEvent* GetProcessEvent() { return process_event_.get(); } |
michael@0 | 86 | |
michael@0 | 87 | const IPC::Channel& channel() const { return *channel_; } |
michael@0 | 88 | IPC::Channel* channelp() const { return channel_.get(); } |
michael@0 | 89 | |
michael@0 | 90 | private: |
michael@0 | 91 | // Sends the given notification to the notification service on the UI thread. |
michael@0 | 92 | void Notify(NotificationType type); |
michael@0 | 93 | |
michael@0 | 94 | protected: |
michael@0 | 95 | // WaitableEventWatcher::Delegate implementation: |
michael@0 | 96 | virtual void OnWaitableEventSignaled(base::WaitableEvent *event); |
michael@0 | 97 | |
michael@0 | 98 | private: |
michael@0 | 99 | // By using an internal class as the IPC::Channel::Listener, we can intercept |
michael@0 | 100 | // OnMessageReceived/OnChannelConnected and do our own processing before |
michael@0 | 101 | // calling the subclass' implementation. |
michael@0 | 102 | class ListenerHook : public IPC::Channel::Listener { |
michael@0 | 103 | public: |
michael@0 | 104 | ListenerHook(ChildProcessHost* host); |
michael@0 | 105 | virtual void OnMessageReceived(const IPC::Message& msg); |
michael@0 | 106 | virtual void OnChannelConnected(int32_t peer_pid); |
michael@0 | 107 | virtual void OnChannelError(); |
michael@0 | 108 | virtual void GetQueuedMessages(std::queue<IPC::Message>& queue); |
michael@0 | 109 | private: |
michael@0 | 110 | ChildProcessHost* host_; |
michael@0 | 111 | }; |
michael@0 | 112 | |
michael@0 | 113 | ListenerHook listener_; |
michael@0 | 114 | |
michael@0 | 115 | // True while we're waiting the channel to be opened. |
michael@0 | 116 | bool opening_channel_; |
michael@0 | 117 | |
michael@0 | 118 | // The IPC::Channel. |
michael@0 | 119 | scoped_ptr<IPC::Channel> channel_; |
michael@0 | 120 | |
michael@0 | 121 | // IPC Channel's id. |
michael@0 | 122 | std::wstring channel_id_; |
michael@0 | 123 | |
michael@0 | 124 | // Used to watch the child process handle. |
michael@0 | 125 | base::WaitableEventWatcher watcher_; |
michael@0 | 126 | |
michael@0 | 127 | scoped_ptr<base::WaitableEvent> process_event_; |
michael@0 | 128 | }; |
michael@0 | 129 | |
michael@0 | 130 | #endif // CHROME_COMMON_CHILD_PROCESS_HOST_H_ |