Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(385)

Side by Side Diff: content/common/child_process_host.h

Issue 8774040: Don't make classes derive from ChildProcessHost, and instead have them use it through composition... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: fix linker errors on posix Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 CONTENT_COMMON_CHILD_PROCESS_HOST_H_ 5 #ifndef CONTENT_COMMON_CHILD_PROCESS_HOST_H_
6 #define CONTENT_COMMON_CHILD_PROCESS_HOST_H_ 6 #define CONTENT_COMMON_CHILD_PROCESS_HOST_H_
7 #pragma once 7 #pragma once
8 8
9 #include <string> 9 #include <string>
10 #include <vector> 10 #include <vector>
11 11
12 #include "build/build_config.h" 12 #include "build/build_config.h"
13 13
14 #include "base/basictypes.h" 14 #include "base/basictypes.h"
15 #include "base/memory/scoped_ptr.h" 15 #include "base/memory/scoped_ptr.h"
16 #include "base/memory/singleton.h" 16 #include "base/memory/singleton.h"
17 #include "base/shared_memory.h" 17 #include "base/shared_memory.h"
18 #include "base/string16.h" 18 #include "base/string16.h"
19 #include "content/common/content_export.h" 19 #include "content/common/content_export.h"
20 #include "ipc/ipc_channel_proxy.h" 20 #include "ipc/ipc_channel_proxy.h"
21 21
22 class FilePath; 22 class FilePath;
23 23
24 namespace IPC { 24 namespace content {
25 class Message; 25 class ChildProcessHostDelegate;
26 } 26 }
27 27
28 // Provides common functionality for hosting a child process and processing IPC 28 // Provides common functionality for hosting a child process and processing IPC
29 // messages between the host and the child process. Subclasses are responsible 29 // messages between the host and the child process. Users are responsible
30 // for the actual launching and terminating of the child processes. 30 // for the actual launching and terminating of the child processes.
31 class CONTENT_EXPORT ChildProcessHost : public IPC::Channel::Listener, 31 class CONTENT_EXPORT ChildProcessHost : public IPC::Channel::Listener,
32 public IPC::Message::Sender { 32 public IPC::Message::Sender {
33 public: 33 public:
34 // These flags may be passed to GetChildPath in order to alter its behavior, 34 // These flags may be passed to GetChildPath in order to alter its behavior,
35 // causing it to return a child path more suited to a specific task. 35 // causing it to return a child path more suited to a specific task.
36 enum { 36 enum {
37 // No special behavior requested. 37 // No special behavior requested.
38 CHILD_NORMAL = 0, 38 CHILD_NORMAL = 0,
39 39
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
73 // the default child process pathname will be returned. On most platforms, 73 // the default child process pathname will be returned. On most platforms,
74 // this will be the same as the currently-executing process. 74 // this will be the same as the currently-executing process.
75 // 75 //
76 // The |flags| argument accepts one or more flags such as CHILD_ALLOW_SELF 76 // The |flags| argument accepts one or more flags such as CHILD_ALLOW_SELF
77 // and CHILD_ALLOW_HEAP_EXECUTION as defined above. Pass only CHILD_NORMAL 77 // and CHILD_ALLOW_HEAP_EXECUTION as defined above. Pass only CHILD_NORMAL
78 // if none of these special behaviors are required. 78 // if none of these special behaviors are required.
79 // 79 //
80 // On failure, returns an empty FilePath. 80 // On failure, returns an empty FilePath.
81 static FilePath GetChildPath(int flags); 81 static FilePath GetChildPath(int flags);
82 82
83 explicit ChildProcessHost(content::ChildProcessHostDelegate* delegate);
84
83 // IPC::Message::Sender implementation. 85 // IPC::Message::Sender implementation.
84 virtual bool Send(IPC::Message* message) OVERRIDE; 86 virtual bool Send(IPC::Message* message) OVERRIDE;
85 87
86 // Adds an IPC message filter. A reference will be kept to the filter. 88 // Adds an IPC message filter. A reference will be kept to the filter.
87 void AddFilter(IPC::ChannelProxy::MessageFilter* filter); 89 void AddFilter(IPC::ChannelProxy::MessageFilter* filter);
88 90
89 // Public and static for reuse by RenderMessageFilter. 91 // Public and static for reuse by RenderMessageFilter.
90 static void OnAllocateSharedMemory( 92 static void AllocateSharedMemory(
91 uint32 buffer_size, base::ProcessHandle child_process, 93 uint32 buffer_size, base::ProcessHandle child_process,
92 base::SharedMemoryHandle* handle); 94 base::SharedMemoryHandle* handle);
93 95
94 // Generates a unique channel name for a child process. 96 // Generates a unique channel name for a child process.
95 // The "instance" pointer value is baked into the channel id. 97 // The "instance" pointer value is baked into the channel id.
96 static std::string GenerateRandomChannelID(void* instance); 98 static std::string GenerateRandomChannelID(void* instance);
97 99
98 // Returns a unique ID to identify a child process. On construction, this 100 // Returns a unique ID to identify a child process. On construction, this
99 // function will be used to generate the id_, but it is also used to generate 101 // function will be used to generate the id_, but it is also used to generate
100 // IDs for the RenderProcessHost, which doesn't inherit from us, and whose IDs 102 // IDs for the RenderProcessHost, which doesn't inherit from us, and whose IDs
101 // must be unique for all child processes. 103 // must be unique for all child processes.
102 // 104 //
103 // This function is threadsafe since RenderProcessHost is on the UI thread, 105 // This function is threadsafe since RenderProcessHost is on the UI thread,
104 // but normally this will be used on the IO thread. 106 // but normally this will be used on the IO thread.
105 static int GenerateChildProcessUniqueId(); 107 static int GenerateChildProcessUniqueId();
106 108
107 protected:
108 ChildProcessHost();
109
110 // Derived classes return true if it's ok to shut down the child process.
111 virtual bool CanShutdown() = 0;
112
113 // Send the shutdown message to the child process. 109 // Send the shutdown message to the child process.
114 // Does not check if CanShutdown is true. 110 // Does not check if CanShutdown is true.
115 virtual void ForceShutdown(); 111 void ForceShutdown();
116 112
117 // Creates the IPC channel. Returns true iff it succeeded. 113 // Creates the IPC channel. Returns true iff it succeeded.
118 virtual bool CreateChannel(); 114 bool CreateChannel();
119
120 // IPC::Channel::Listener implementation:
121 virtual bool OnMessageReceived(const IPC::Message& msg) OVERRIDE;
122 virtual void OnChannelConnected(int32 peer_pid) OVERRIDE;
123 virtual void OnChannelError() OVERRIDE;
124 115
125 bool opening_channel() { return opening_channel_; } 116 bool opening_channel() { return opening_channel_; }
126 const std::string& channel_id() { return channel_id_; } 117 const std::string& channel_id() { return channel_id_; }
127 IPC::Channel* channel() { return channel_.get(); } 118 IPC::Channel* channel() { return channel_.get(); }
128 119
129 // Called when the child process goes away. See OnChildDisconnected(). 120 private:
130 virtual void OnChildDied(); 121 // IPC::Channel::Listener methods:
122 virtual bool OnMessageReceived(const IPC::Message& msg) OVERRIDE;
123 virtual void OnChannelConnected(int32 peer_pid) OVERRIDE;
124 virtual void OnChannelError() OVERRIDE;
131 125
132 // Called when the child process unexpected closes the IPC channel. The 126 // Message handlers:
133 // default action is to call OnChildDied(). Subclasses might want to override 127 void OnShutdownRequest();
134 // this behavior. 128 void OnAllocateSharedMemory(uint32 buffer_size,
135 virtual void OnChildDisconnected(); 129 base::SharedMemoryHandle* handle);
136 130
137 // Notifies the derived class that we told the child process to kill itself. 131 content::ChildProcessHostDelegate* delegate_;
138 virtual void ShutdownStarted(); 132 base::ProcessHandle peer_handle_;
139
140 private:
141 // By using an internal class as the IPC::Channel::Listener, we can intercept
142 // OnMessageReceived/OnChannelConnected and do our own processing before
143 // calling the subclass' implementation.
144 class ListenerHook : public IPC::Channel::Listener {
145 public:
146 explicit ListenerHook(ChildProcessHost* host);
147 virtual ~ListenerHook();
148
149 void Shutdown();
150
151 // IPC::Channel::Listener methods:
152 virtual bool OnMessageReceived(const IPC::Message& msg) OVERRIDE;
153 virtual void OnChannelConnected(int32 peer_pid) OVERRIDE;
154 virtual void OnChannelError() OVERRIDE;
155
156 bool Send(IPC::Message* message);
157
158 private:
159 void OnShutdownRequest();
160 void OnAllocateSharedMemory(uint32 buffer_size,
161 base::SharedMemoryHandle* handle);
162 ChildProcessHost* host_;
163 base::ProcessHandle peer_handle_;
164 DISALLOW_COPY_AND_ASSIGN(ListenerHook);
165 };
166
167 ListenerHook listener_;
168
169 bool opening_channel_; // True while we're waiting the channel to be opened. 133 bool opening_channel_; // True while we're waiting the channel to be opened.
170 scoped_ptr<IPC::Channel> channel_; 134 scoped_ptr<IPC::Channel> channel_;
171 std::string channel_id_; 135 std::string channel_id_;
172 136
173 // Holds all the IPC message filters. Since this object lives on the IO 137 // Holds all the IPC message filters. Since this object lives on the IO
174 // thread, we don't have a IPC::ChannelProxy and so we manage filters 138 // thread, we don't have a IPC::ChannelProxy and so we manage filters
175 // manually. 139 // manually.
176 std::vector<scoped_refptr<IPC::ChannelProxy::MessageFilter> > filters_; 140 std::vector<scoped_refptr<IPC::ChannelProxy::MessageFilter> > filters_;
177 141
178 DISALLOW_COPY_AND_ASSIGN(ChildProcessHost); 142 DISALLOW_COPY_AND_ASSIGN(ChildProcessHost);
179 }; 143 };
180 144
181 #endif // CONTENT_COMMON_CHILD_PROCESS_HOST_H_ 145 #endif // CONTENT_COMMON_CHILD_PROCESS_HOST_H_
OLDNEW
« no previous file with comments | « content/browser/worker_host/worker_process_host.cc ('k') | content/common/child_process_host.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698