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