OLD | NEW |
| (Empty) |
1 // Copyright (c) 2010 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 #pragma once | |
8 | |
9 #include <string> | |
10 #include <vector> | |
11 | |
12 #include "build/build_config.h" | |
13 | |
14 #if defined(OS_WIN) | |
15 #include <windows.h> | |
16 #endif // defined(OS_WIN) | |
17 | |
18 #include "base/basictypes.h" | |
19 #include "base/scoped_ptr.h" | |
20 #include "chrome/common/notification_type.h" | |
21 #include "ipc/ipc_channel_proxy.h" | |
22 | |
23 class CommandLine; | |
24 class FilePath; | |
25 | |
26 namespace IPC { | |
27 class Message; | |
28 } | |
29 | |
30 // Provides common functionality for hosting a child process and processing IPC | |
31 // messages between the host and the child process. Subclasses are responsible | |
32 // for the actual launching and terminating of the child processes. | |
33 class ChildProcessHost : public IPC::Channel::Listener, | |
34 public IPC::Message::Sender { | |
35 public: | |
36 virtual ~ChildProcessHost(); | |
37 | |
38 // Returns the pathname to be used for a child process. If a subprocess | |
39 // pathname was specified on the command line, that will be used. Otherwise, | |
40 // the default child process pathname will be returned. On most platforms, | |
41 // this will be the same as the currently-executing process. | |
42 // | |
43 // The argument allow_self is used on Linux to indicate that we allow us to | |
44 // fork from /proc/self/exe rather than using the "real" app path. This | |
45 // prevents autoupdate from confusing us if it changes the file out from | |
46 // under us. You will generally want to set this to true, except when there | |
47 // is an override to the command line (for example, we're forking a renderer | |
48 // in gdb). In this case, you'd use GetChildPath to get the real executable | |
49 // file name, and then prepend the GDB command to the command line. | |
50 // | |
51 // On failure, returns an empty FilePath. | |
52 static FilePath GetChildPath(bool allow_self); | |
53 | |
54 #if defined(OS_WIN) | |
55 // See comments in the cc file. This is a common hack needed for a process | |
56 // hosting a sandboxed child process. Hence it lives in this file. | |
57 static void PreCacheFont(LOGFONT font); | |
58 #endif // defined(OS_WIN) | |
59 | |
60 // IPC::Message::Sender implementation. | |
61 virtual bool Send(IPC::Message* message); | |
62 | |
63 protected: | |
64 ChildProcessHost(); | |
65 | |
66 // Adds an IPC message filter. A reference will be kept to the filter. | |
67 void AddFilter(IPC::ChannelProxy::MessageFilter* filter); | |
68 | |
69 // Derived classes return true if it's ok to shut down the child process. | |
70 virtual bool CanShutdown() = 0; | |
71 | |
72 // Send the shutdown message to the child process. | |
73 // Does not check if CanShutdown is true. | |
74 virtual void ForceShutdown(); | |
75 | |
76 // Creates the IPC channel. Returns true iff it succeeded. | |
77 virtual bool CreateChannel(); | |
78 | |
79 // Notifies us that an instance has been created on this child process. | |
80 virtual void InstanceCreated(); | |
81 | |
82 // IPC::Channel::Listener implementation: | |
83 virtual bool OnMessageReceived(const IPC::Message& msg); | |
84 virtual void OnChannelConnected(int32 peer_pid); | |
85 virtual void OnChannelError(); | |
86 | |
87 bool opening_channel() { return opening_channel_; } | |
88 const std::string& channel_id() { return channel_id_; } | |
89 IPC::Channel* channel() { return channel_.get(); } | |
90 | |
91 // Called when the child process goes away. | |
92 virtual void OnChildDied(); | |
93 // Notifies the derived class that we told the child process to kill itself. | |
94 virtual void ShutdownStarted(); | |
95 // Subclasses can implement specific notification methods. | |
96 virtual void Notify(NotificationType type); | |
97 | |
98 private: | |
99 // By using an internal class as the IPC::Channel::Listener, we can intercept | |
100 // OnMessageReceived/OnChannelConnected and do our own processing before | |
101 // calling the subclass' implementation. | |
102 class ListenerHook : public IPC::Channel::Listener { | |
103 public: | |
104 explicit ListenerHook(ChildProcessHost* host); | |
105 virtual bool OnMessageReceived(const IPC::Message& msg); | |
106 virtual void OnChannelConnected(int32 peer_pid); | |
107 virtual void OnChannelError(); | |
108 private: | |
109 ChildProcessHost* host_; | |
110 }; | |
111 | |
112 ListenerHook listener_; | |
113 | |
114 bool opening_channel_; // True while we're waiting the channel to be opened. | |
115 scoped_ptr<IPC::Channel> channel_; | |
116 std::string channel_id_; | |
117 | |
118 // Holds all the IPC message filters. Since this object lives on the IO | |
119 // thread, we don't have a IPC::ChannelProxy and so we manage filters | |
120 // manually. | |
121 std::vector<scoped_refptr<IPC::ChannelProxy::MessageFilter> > filters_; | |
122 | |
123 DISALLOW_COPY_AND_ASSIGN(ChildProcessHost); | |
124 }; | |
125 | |
126 #endif // CHROME_COMMON_CHILD_PROCESS_HOST_H_ | |
OLD | NEW |