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