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