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 exclusively hosting
104 // guest RenderFrames. Not all guest RenderFrames are created equal. A guest,
105 // as indicated by BrowserPluginGuest::IsGuest, may coexist with other
106 // non-guest RenderFrames in the same process if IsForGuestsOnly() is false.
107 virtual bool IsForGuestsOnly() 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 // Returns the process object associated with the child process. In certain
132 // tests or single-process mode, this will actually represent the current
135 // NOTE: this is not necessarily valid immediately after calling Init, as
136 // Init starts the process asynchronously. It's guaranteed to be valid after
137 // the first IPC arrives.
138 virtual base::ProcessHandle
GetHandle() const = 0;
140 // Returns the user browser context associated with this renderer process.
141 virtual content::BrowserContext
* GetBrowserContext() const = 0;
143 // Returns whether this process is using the same StoragePartition as
145 virtual bool InSameStoragePartition(StoragePartition
* partition
) const = 0;
147 // Returns the unique ID for this child process host. This can be used later
148 // in a call to FromID() to get back to this object (this is used to avoid
149 // sending non-threadsafe pointers to other threads).
151 // This ID will be unique across all child process hosts, including workers,
154 // This will never return ChildProcessHost::kInvalidUniqueID.
155 virtual int GetID() const = 0;
157 // Returns true iff channel_ has been set to non-nullptr. Use this for
158 // checking if there is connection or not. Virtual for mocking out for tests.
159 virtual bool HasConnection() const = 0;
161 // Call this to allow queueing of IPC messages that are sent before the
162 // process is launched.
163 virtual void EnableSendQueue() = 0;
165 // Returns the renderer channel.
166 virtual IPC::ChannelProxy
* GetChannel() = 0;
168 // Adds a message filter to the IPC channel.
169 virtual void AddFilter(BrowserMessageFilter
* filter
) = 0;
171 // Try to shutdown the associated render process as fast as possible
172 virtual bool FastShutdownForPageCount(size_t count
) = 0;
175 // Revisit whether the virtual functions declared from here on need to be
176 // part of the interface.
177 virtual void SetIgnoreInputEvents(bool ignore_input_events
) = 0;
178 virtual bool IgnoreInputEvents() const = 0;
180 // Schedules the host for deletion and removes it from the all_hosts list.
181 virtual void Cleanup() = 0;
183 // Track the count of pending views that are being swapped back in. Called
184 // by listeners to register and unregister pending views to prevent the
185 // process from exiting.
186 virtual void AddPendingView() = 0;
187 virtual void RemovePendingView() = 0;
189 // Sets a flag indicating that the process can be abnormally terminated.
190 virtual void SetSuddenTerminationAllowed(bool allowed
) = 0;
191 // Returns true if the process can be abnormally terminated.
192 virtual bool SuddenTerminationAllowed() const = 0;
194 // Returns how long the child has been idle. The definition of idle
195 // depends on when a derived class calls mark_child_process_activity_time().
196 // This is a rough indicator and its resolution should not be better than
198 virtual base::TimeDelta
GetChildProcessIdleTime() const = 0;
200 // Called to resume the requests for a view created through window.open that
201 // were initially blocked.
202 virtual void ResumeRequestsForView(int route_id
) = 0;
204 // Checks that the given renderer can request |url|, if not it sets it to
206 // |empty_allowed| must be set to false for navigations for security reasons.
207 virtual void FilterURL(bool empty_allowed
, GURL
* url
) = 0;
209 #if defined(ENABLE_WEBRTC)
210 virtual void EnableAecDump(const base::FilePath
& file
) = 0;
211 virtual void DisableAecDump() = 0;
213 // When set, |callback| receives log messages regarding, for example, media
214 // devices (webcams, mics, etc) that were initially requested in the render
215 // process associated with this RenderProcessHost.
216 virtual void SetWebRtcLogMessageCallback(
217 base::Callback
<void(const std::string
&)> callback
) = 0;
219 typedef base::Callback
<void(scoped_ptr
<uint8
[]> packet_header
,
220 size_t header_length
,
221 size_t packet_length
,
222 bool incoming
)> WebRtcRtpPacketCallback
;
224 typedef base::Callback
<void(bool incoming
, bool outgoing
)>
225 WebRtcStopRtpDumpCallback
;
227 // Starts passing RTP packets to |packet_callback| and returns the callback
228 // used to stop dumping.
229 virtual WebRtcStopRtpDumpCallback
StartRtpDump(
232 const WebRtcRtpPacketCallback
& packet_callback
) = 0;
235 // Tells the ResourceDispatcherHost to resume a deferred navigation without
236 // transferring it to a new renderer process.
237 virtual void ResumeDeferredNavigation(const GlobalRequestID
& request_id
) = 0;
239 // Notifies the renderer that the timezone configuration of the system might
241 virtual void NotifyTimezoneChange(const std::string
& zone_id
) = 0;
243 // Returns the ServiceRegistry for this process.
244 virtual ServiceRegistry
* GetServiceRegistry() = 0;
247 // Returns the time the first call to Init completed successfully (after a new
248 // renderer process was created); further calls to Init won't change this
250 // Note: Do not use! Will disappear after PlzNavitate is completed.
251 virtual const base::TimeTicks
& GetInitTimeForNavigationMetrics() const = 0;
253 // Returns whether or not the CHROMIUM_subscribe_uniform WebGL extension
254 // is currently enabled
255 virtual bool SubscribeUniformEnabled() const = 0;
257 // Handlers for subscription target changes to update subscription_set_
258 virtual void OnAddSubscription(unsigned int target
) = 0;
259 virtual void OnRemoveSubscription(unsigned int target
) = 0;
261 // Send a new ValueState to the Gpu Service to update a subscription target
262 virtual void SendUpdateValueState(
263 unsigned int target
, const gpu::ValueState
& state
) = 0;
265 // Retrieves the list of AudioOutputController objects associated
266 // with this object and passes it to the callback you specify, on
267 // the same thread on which you called the method.
268 typedef std::list
<scoped_refptr
<media::AudioOutputController
>>
269 AudioOutputControllerList
;
270 typedef base::Callback
<void(const AudioOutputControllerList
&)>
271 GetAudioOutputControllersCallback
;
272 virtual void GetAudioOutputControllers(
273 const GetAudioOutputControllersCallback
& callback
) const = 0;
275 #if defined(ENABLE_BROWSER_CDMS)
276 // Returns the ::media::BrowserCdm instance associated with |render_frame_id|
277 // and |cdm_id|, or nullptr if not found.
278 virtual media::BrowserCdm
* GetBrowserCdm(int render_frame_id
,
279 int cdm_id
) const = 0;
282 // Returns the current number of active views in this process. Excludes
283 // any RenderViewHosts that are swapped out.
284 int GetActiveViewCount();
286 // Static management functions -----------------------------------------------
288 // Flag to run the renderer in process. This is primarily
289 // for debugging purposes. When running "in process", the
290 // browser maintains a single RenderProcessHost which communicates
291 // to a RenderProcess which is instantiated in the same process
292 // with the Browser. All IPC between the Browser and the
293 // Renderer is the same, it's just not crossing a process boundary.
295 static bool run_renderer_in_process();
297 // This also calls out to ContentBrowserClient::GetApplicationLocale and
298 // modifies the current process' command line.
299 static void SetRunRendererInProcess(bool value
);
301 // Allows iteration over all the RenderProcessHosts in the browser. Note
302 // that each host may not be active, and therefore may have nullptr channels.
303 static iterator
AllHostsIterator();
305 // Returns the RenderProcessHost given its ID. Returns nullptr if the ID does
306 // not correspond to a live RenderProcessHost.
307 static RenderProcessHost
* FromID(int render_process_id
);
309 // Returns whether the process-per-site model is in use (globally or just for
310 // the current site), in which case we should ensure there is only one
311 // RenderProcessHost per site for the entire browser context.
312 static bool ShouldUseProcessPerSite(content::BrowserContext
* browser_context
,
315 // Returns true if the caller should attempt to use an existing
316 // RenderProcessHost rather than creating a new one.
317 static bool ShouldTryToUseExistingProcessHost(
318 content::BrowserContext
* browser_context
, const GURL
& site_url
);
320 // Get an existing RenderProcessHost associated with the given browser
321 // context, if possible. The renderer process is chosen randomly from
322 // suitable renderers that share the same context and type (determined by the
324 // Returns nullptr if no suitable renderer process is available, in which case
325 // the caller is free to create a new renderer.
326 static RenderProcessHost
* GetExistingProcessHost(
327 content::BrowserContext
* browser_context
, const GURL
& site_url
);
329 // Overrides the default heuristic for limiting the max renderer process
330 // count. This is useful for unit testing process limit behaviors. It is
331 // also used to allow a command line parameter to configure the max number of
332 // renderer processes and should only be called once during startup.
333 // A value of zero means to use the default heuristic.
334 static void SetMaxRendererProcessCount(size_t count
);
336 // Returns the current maximum number of renderer process hosts kept by the
338 static size_t GetMaxRendererProcessCount();
341 } // namespace content.
343 #endif // CONTENT_PUBLIC_BROWSER_RENDER_PROCESS_HOST_H_