1 // Copyright (c) 2012 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.
5 #ifndef CONTENT_PUBLIC_BROWSER_RENDER_PROCESS_HOST_H_
6 #define CONTENT_PUBLIC_BROWSER_RENDER_PROCESS_HOST_H_
10 #include "base/basictypes.h"
11 #include "base/id_map.h"
12 #include "base/process/kill.h"
13 #include "base/process/process_handle.h"
14 #include "base/supports_user_data.h"
15 #include "content/common/content_export.h"
16 #include "ipc/ipc_channel_proxy.h"
17 #include "ipc/ipc_sender.h"
18 #include "ui/gfx/native_widget_types.h"
21 struct ViewMsg_SwapOut_Params
;
32 class AudioOutputController
;
38 class BrowserMessageFilter
;
39 class RenderProcessHostObserver
;
40 class RenderWidgetHost
;
41 class ServiceRegistry
;
42 class StoragePartition
;
43 struct GlobalRequestID
;
45 // Interface that represents the browser side of the browser <-> renderer
46 // communication channel. There will generally be one RenderProcessHost per
48 class CONTENT_EXPORT RenderProcessHost
: public IPC::Sender
,
50 public base::SupportsUserData
{
52 typedef IDMap
<RenderProcessHost
>::iterator iterator
;
54 // Details for RENDERER_PROCESS_CLOSED notifications.
55 struct RendererClosedDetails
{
56 RendererClosedDetails(base::TerminationStatus status
,
58 this->status
= status
;
59 this->exit_code
= exit_code
;
61 base::TerminationStatus status
;
65 // General functions ---------------------------------------------------------
67 ~RenderProcessHost() override
{}
69 // Initialize the new renderer process, returning true on success. This must
70 // be called once before the object can be used, but can be called after
71 // that with no effect. Therefore, if the caller isn't sure about whether
72 // the process has been created, it should just call Init().
73 virtual bool Init() = 0;
75 // Gets the next available routing id.
76 virtual int GetNextRoutingID() = 0;
78 // These methods add or remove listener for a specific message routing ID.
79 // Used for refcounting, each holder of this object must AddRoute and
80 // RemoveRoute. This object should be allocated on the heap; when no
81 // listeners own it any more, it will delete itself.
82 virtual void AddRoute(int32 routing_id
, IPC::Listener
* listener
) = 0;
83 virtual void RemoveRoute(int32 routing_id
) = 0;
85 // Add and remove observers for lifecycle events. The order in which
86 // notifications are sent to observers is undefined. Observers must be sure to
87 // remove the observer before they go away.
88 virtual void AddObserver(RenderProcessHostObserver
* observer
) = 0;
89 virtual void RemoveObserver(RenderProcessHostObserver
* observer
) = 0;
91 // Called when a received message cannot be decoded. Terminates the renderer.
92 // Most callers should not call this directly, but instead should call
93 // bad_message::BadMessageReceived() or an equivalent method outside of the
95 virtual void ShutdownForBadMessage() = 0;
97 // Track the count of visible widgets. Called by listeners to register and
98 // unregister visibility.
99 virtual void WidgetRestored() = 0;
100 virtual void WidgetHidden() = 0;
101 virtual int VisibleWidgetCount() const = 0;
103 // Indicates whether the current RenderProcessHost is associated with an
104 // isolated guest renderer process. Not all guest renderers are created equal.
105 // A guest, as indicated by BrowserPluginGuest::IsGuest, may coexist with
106 // other non-guest renderers in the same process if IsIsolatedGuest is false.
107 virtual bool IsIsolatedGuest() const = 0;
109 // Returns the storage partition associated with this process.
111 // TODO(nasko): Remove this function from the public API once
112 // URLRequestContextGetter's creation is moved into StoragePartition.
113 // http://crbug.com/158595
114 virtual StoragePartition
* GetStoragePartition() const = 0;
116 // Try to shut down the associated renderer process without running unload
117 // handlers, etc, giving it the specified exit code. If |wait| is true, wait
118 // for the process to be actually terminated before returning.
119 // Returns true if it was able to shut down.
120 virtual bool Shutdown(int exit_code
, bool wait
) = 0;
122 // Try to shut down the associated renderer process as fast as possible.
123 // If this renderer has any RenderViews with unload handlers, then this
124 // function does nothing.
125 // Returns true if it was able to do fast shutdown.
126 virtual bool FastShutdownIfPossible() = 0;
128 // Returns true if fast shutdown was started for the renderer.
129 virtual bool FastShutdownStarted() const = 0;
131 // Dump the child process' handle table before shutting down.
132 virtual void DumpHandles() = 0;
134 // Returns the process object associated with the child process. In certain
135 // tests or single-process mode, this will actually represent the current
138 // NOTE: this is not necessarily valid immediately after calling Init, as
139 // Init starts the process asynchronously. It's guaranteed to be valid after
140 // the first IPC arrives.
141 virtual base::ProcessHandle
GetHandle() const = 0;
143 // Returns the user browser context associated with this renderer process.
144 virtual content::BrowserContext
* GetBrowserContext() const = 0;
146 // Returns whether this process is using the same StoragePartition as
148 virtual bool InSameStoragePartition(StoragePartition
* partition
) const = 0;
150 // Returns the unique ID for this child process host. This can be used later
151 // in a call to FromID() to get back to this object (this is used to avoid
152 // sending non-threadsafe pointers to other threads).
154 // This ID will be unique across all child process hosts, including workers,
157 // This will never return ChildProcessHost::kInvalidUniqueID.
158 virtual int GetID() const = 0;
160 // Returns true iff channel_ has been set to non-nullptr. Use this for
161 // checking if there is connection or not. Virtual for mocking out for tests.
162 virtual bool HasConnection() const = 0;
164 // Call this to allow queueing of IPC messages that are sent before the
165 // process is launched.
166 virtual void EnableSendQueue() = 0;
168 // Returns the renderer channel.
169 virtual IPC::ChannelProxy
* GetChannel() = 0;
171 // Adds a message filter to the IPC channel.
172 virtual void AddFilter(BrowserMessageFilter
* filter
) = 0;
174 // Try to shutdown the associated render process as fast as possible
175 virtual bool FastShutdownForPageCount(size_t count
) = 0;
178 // Revisit whether the virtual functions declared from here on need to be
179 // part of the interface.
180 virtual void SetIgnoreInputEvents(bool ignore_input_events
) = 0;
181 virtual bool IgnoreInputEvents() const = 0;
183 // Schedules the host for deletion and removes it from the all_hosts list.
184 virtual void Cleanup() = 0;
186 // Track the count of pending views that are being swapped back in. Called
187 // by listeners to register and unregister pending views to prevent the
188 // process from exiting.
189 virtual void AddPendingView() = 0;
190 virtual void RemovePendingView() = 0;
192 // Sets a flag indicating that the process can be abnormally terminated.
193 virtual void SetSuddenTerminationAllowed(bool allowed
) = 0;
194 // Returns true if the process can be abnormally terminated.
195 virtual bool SuddenTerminationAllowed() const = 0;
197 // Returns how long the child has been idle. The definition of idle
198 // depends on when a derived class calls mark_child_process_activity_time().
199 // This is a rough indicator and its resolution should not be better than
201 virtual base::TimeDelta
GetChildProcessIdleTime() const = 0;
203 // Called to resume the requests for a view created through window.open that
204 // were initially blocked.
205 virtual void ResumeRequestsForView(int route_id
) = 0;
207 // Checks that the given renderer can request |url|, if not it sets it to
209 // |empty_allowed| must be set to false for navigations for security reasons.
210 virtual void FilterURL(bool empty_allowed
, GURL
* url
) = 0;
212 #if defined(ENABLE_WEBRTC)
213 virtual void EnableAecDump(const base::FilePath
& file
) = 0;
214 virtual void DisableAecDump() = 0;
216 // When set, |callback| receives log messages regarding, for example, media
217 // devices (webcams, mics, etc) that were initially requested in the render
218 // process associated with this RenderProcessHost.
219 virtual void SetWebRtcLogMessageCallback(
220 base::Callback
<void(const std::string
&)> callback
) = 0;
222 typedef base::Callback
<void(scoped_ptr
<uint8
[]> packet_header
,
223 size_t header_length
,
224 size_t packet_length
,
225 bool incoming
)> WebRtcRtpPacketCallback
;
227 typedef base::Callback
<void(bool incoming
, bool outgoing
)>
228 WebRtcStopRtpDumpCallback
;
230 // Starts passing RTP packets to |packet_callback| and returns the callback
231 // used to stop dumping.
232 virtual WebRtcStopRtpDumpCallback
StartRtpDump(
235 const WebRtcRtpPacketCallback
& packet_callback
) = 0;
238 // Tells the ResourceDispatcherHost to resume a deferred navigation without
239 // transferring it to a new renderer process.
240 virtual void ResumeDeferredNavigation(const GlobalRequestID
& request_id
) = 0;
242 // Notifies the renderer that the timezone configuration of the system might
244 virtual void NotifyTimezoneChange() = 0;
246 // Returns the ServiceRegistry for this process.
247 virtual ServiceRegistry
* GetServiceRegistry() = 0;
250 // Returns the time the first call to Init completed successfully (after a new
251 // renderer process was created); further calls to Init won't change this
253 // Note: Do not use! Will disappear after PlzNavitate is completed.
254 virtual const base::TimeTicks
& GetInitTimeForNavigationMetrics() const = 0;
256 // Returns whether or not the CHROMIUM_subscribe_uniform WebGL extension
257 // is currently enabled
258 virtual bool SubscribeUniformEnabled() const = 0;
260 // Handlers for subscription target changes to update subscription_set_
261 virtual void OnAddSubscription(unsigned int target
) = 0;
262 virtual void OnRemoveSubscription(unsigned int target
) = 0;
264 // Send a new ValueState to the Gpu Service to update a subscription target
265 virtual void SendUpdateValueState(
266 unsigned int target
, const gpu::ValueState
& state
) = 0;
268 // Retrieves the list of AudioOutputController objects associated
269 // with this object and passes it to the callback you specify, on
270 // the same thread on which you called the method.
271 typedef std::list
<scoped_refptr
<media::AudioOutputController
>>
272 AudioOutputControllerList
;
273 typedef base::Callback
<void(const AudioOutputControllerList
&)>
274 GetAudioOutputControllersCallback
;
275 virtual void GetAudioOutputControllers(
276 const GetAudioOutputControllersCallback
& callback
) const = 0;
278 #if defined(ENABLE_BROWSER_CDMS)
279 // Returns the ::media::BrowserCdm instance associated with |render_frame_id|
280 // and |cdm_id|, or nullptr if not found.
281 virtual media::BrowserCdm
* GetBrowserCdm(int render_frame_id
,
282 int cdm_id
) const = 0;
285 // Returns the current number of active views in this process. Excludes
286 // any RenderViewHosts that are swapped out.
287 int GetActiveViewCount();
289 // Static management functions -----------------------------------------------
291 // Flag to run the renderer in process. This is primarily
292 // for debugging purposes. When running "in process", the
293 // browser maintains a single RenderProcessHost which communicates
294 // to a RenderProcess which is instantiated in the same process
295 // with the Browser. All IPC between the Browser and the
296 // Renderer is the same, it's just not crossing a process boundary.
298 static bool run_renderer_in_process();
300 // This also calls out to ContentBrowserClient::GetApplicationLocale and
301 // modifies the current process' command line.
302 static void SetRunRendererInProcess(bool value
);
304 // Allows iteration over all the RenderProcessHosts in the browser. Note
305 // that each host may not be active, and therefore may have nullptr channels.
306 static iterator
AllHostsIterator();
308 // Returns the RenderProcessHost given its ID. Returns nullptr if the ID does
309 // not correspond to a live RenderProcessHost.
310 static RenderProcessHost
* FromID(int render_process_id
);
312 // Returns whether the process-per-site model is in use (globally or just for
313 // the current site), in which case we should ensure there is only one
314 // RenderProcessHost per site for the entire browser context.
315 static bool ShouldUseProcessPerSite(content::BrowserContext
* browser_context
,
318 // Returns true if the caller should attempt to use an existing
319 // RenderProcessHost rather than creating a new one.
320 static bool ShouldTryToUseExistingProcessHost(
321 content::BrowserContext
* browser_context
, const GURL
& site_url
);
323 // Get an existing RenderProcessHost associated with the given browser
324 // context, if possible. The renderer process is chosen randomly from
325 // suitable renderers that share the same context and type (determined by the
327 // Returns nullptr if no suitable renderer process is available, in which case
328 // the caller is free to create a new renderer.
329 static RenderProcessHost
* GetExistingProcessHost(
330 content::BrowserContext
* browser_context
, const GURL
& site_url
);
332 // Overrides the default heuristic for limiting the max renderer process
333 // count. This is useful for unit testing process limit behaviors. It is
334 // also used to allow a command line parameter to configure the max number of
335 // renderer processes and should only be called once during startup.
336 // A value of zero means to use the default heuristic.
337 static void SetMaxRendererProcessCount(size_t count
);
339 // Returns the current maximum number of renderer process hosts kept by the
341 static size_t GetMaxRendererProcessCount();
344 } // namespace content.
346 #endif // CONTENT_PUBLIC_BROWSER_RENDER_PROCESS_HOST_H_