Line data Source code
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 : }
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 : protected:
31 : explicit ChildProcessHost();
32 :
33 : // Derived classes return true if it's ok to shut down the child process.
34 : virtual bool CanShutdown() = 0;
35 :
36 : // Creates the IPC channel. Returns true iff it succeeded.
37 : bool CreateChannel();
38 :
39 : bool CreateChannel(mozilla::ipc::FileDescriptor& aFileDescriptor);
40 :
41 : // IPC::Channel::Listener implementation:
42 0 : virtual void OnMessageReceived(IPC::Message&& msg) { }
43 0 : virtual void OnChannelConnected(int32_t peer_pid) { }
44 0 : virtual void OnChannelError() { }
45 :
46 : bool opening_channel() { return opening_channel_; }
47 : const std::wstring& channel_id() { return channel_id_; }
48 :
49 2 : const IPC::Channel& channel() const { return *channel_; }
50 6 : IPC::Channel* channelp() const { return channel_.get(); }
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 0 : class ListenerHook : public IPC::Channel::Listener {
57 : public:
58 : explicit ListenerHook(ChildProcessHost* host);
59 : virtual void OnMessageReceived(IPC::Message&& msg);
60 : virtual void OnChannelConnected(int32_t peer_pid);
61 : virtual void OnChannelError();
62 : virtual void GetQueuedMessages(std::queue<IPC::Message>& queue);
63 : private:
64 : ChildProcessHost* host_;
65 : };
66 :
67 : ListenerHook listener_;
68 :
69 : // True while we're waiting the channel to be opened.
70 : bool opening_channel_;
71 :
72 : // The IPC::Channel.
73 : mozilla::UniquePtr<IPC::Channel> channel_;
74 :
75 : // IPC Channel's id.
76 : std::wstring channel_id_;
77 : };
78 :
79 : #endif // CHROME_COMMON_CHILD_PROCESS_HOST_H_
|