render_process_host.h revision 0529e5d033099cbfc42635f6f6183833b09dff6e
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.
4
5#ifndef CONTENT_PUBLIC_BROWSER_RENDER_PROCESS_HOST_H_
6#define CONTENT_PUBLIC_BROWSER_RENDER_PROCESS_HOST_H_
7
8#include "base/basictypes.h"
9#include "base/id_map.h"
10#include "base/process/kill.h"
11#include "base/process/process_handle.h"
12#include "base/supports_user_data.h"
13#include "content/common/content_export.h"
14#include "ipc/ipc_channel_proxy.h"
15#include "ipc/ipc_sender.h"
16#include "ui/gfx/native_widget_types.h"
17#include "ui/surface/transport_dib.h"
18
19class GURL;
20struct ViewMsg_SwapOut_Params;
21
22namespace base {
23class TimeDelta;
24}
25
26namespace content {
27class BrowserContext;
28class BrowserMessageFilter;
29class RenderProcessHostObserver;
30class RenderWidgetHost;
31class StoragePartition;
32struct GlobalRequestID;
33
34// Interface that represents the browser side of the browser <-> renderer
35// communication channel. There will generally be one RenderProcessHost per
36// renderer process.
37class CONTENT_EXPORT RenderProcessHost : public IPC::Sender,
38                                         public IPC::Listener,
39                                         public base::SupportsUserData {
40 public:
41  typedef IDMap<RenderProcessHost>::iterator iterator;
42
43  // Details for RENDERER_PROCESS_CLOSED notifications.
44  struct RendererClosedDetails {
45    RendererClosedDetails(base::ProcessHandle handle,
46                          base::TerminationStatus status,
47                          int exit_code) {
48      this->handle = handle;
49      this->status = status;
50      this->exit_code = exit_code;
51    }
52    base::ProcessHandle handle;
53    base::TerminationStatus status;
54    int exit_code;
55  };
56
57  // General functions ---------------------------------------------------------
58
59  virtual ~RenderProcessHost() {}
60
61  // Initialize the new renderer process, returning true on success. This must
62  // be called once before the object can be used, but can be called after
63  // that with no effect. Therefore, if the caller isn't sure about whether
64  // the process has been created, it should just call Init().
65  virtual bool Init() = 0;
66
67  // Gets the next available routing id.
68  virtual int GetNextRoutingID() = 0;
69
70  // These methods add or remove listener for a specific message routing ID.
71  // Used for refcounting, each holder of this object must AddRoute and
72  // RemoveRoute. This object should be allocated on the heap; when no
73  // listeners own it any more, it will delete itself.
74  virtual void AddRoute(int32 routing_id, IPC::Listener* listener) = 0;
75  virtual void RemoveRoute(int32 routing_id) = 0;
76
77  // Add and remove observers for lifecycle events. The order in which
78  // notifications are sent to observers is undefined. Observers must be sure to
79  // remove the observer before they go away.
80  virtual void AddObserver(RenderProcessHostObserver* observer) = 0;
81  virtual void RemoveObserver(RenderProcessHostObserver* observer) = 0;
82
83  // Called to wait for the next UpdateRect message for the specified render
84  // widget.  Returns true if successful, and the msg out-param will contain a
85  // copy of the received UpdateRect message.
86  virtual bool WaitForBackingStoreMsg(int render_widget_id,
87                                      const base::TimeDelta& max_delay,
88                                      IPC::Message* msg) = 0;
89
90  // Called when a received message cannot be decoded.
91  virtual void ReceivedBadMessage() = 0;
92
93  // Track the count of visible widgets. Called by listeners to register and
94  // unregister visibility.
95  virtual void WidgetRestored() = 0;
96  virtual void WidgetHidden() = 0;
97  virtual int VisibleWidgetCount() const = 0;
98
99  // Indicates whether the current RenderProcessHost associated with a guest
100  // renderer process.
101  virtual bool IsGuest() const = 0;
102
103  // Returns the storage partition associated with this process.
104  //
105  // TODO(nasko): Remove this function from the public API once
106  // URLRequestContextGetter's creation is moved into StoragePartition.
107  // http://crbug.com/158595
108  virtual StoragePartition* GetStoragePartition() const = 0;
109
110  // Try to shutdown the associated renderer process as fast as possible.
111  // If this renderer has any RenderViews with unload handlers, then this
112  // function does nothing.  The current implementation uses TerminateProcess.
113  // Returns True if it was able to do fast shutdown.
114  virtual bool FastShutdownIfPossible() = 0;
115
116  // Returns true if fast shutdown was started for the renderer.
117  virtual bool FastShutdownStarted() const = 0;
118
119  // Dump the child process' handle table before shutting down.
120  virtual void DumpHandles() = 0;
121
122  // Returns the process object associated with the child process.  In certain
123  // tests or single-process mode, this will actually represent the current
124  // process.
125  //
126  // NOTE: this is not necessarily valid immediately after calling Init, as
127  // Init starts the process asynchronously.  It's guaranteed to be valid after
128  // the first IPC arrives.
129  virtual base::ProcessHandle GetHandle() const = 0;
130
131  // Transport DIB functions ---------------------------------------------------
132
133  // Return the TransportDIB for the given id. On Linux, this can involve
134  // mapping shared memory. On Mac, the shared memory is created in the browser
135  // process and the cached metadata is returned. On Windows, this involves
136  // duplicating the handle from the remote process.  The RenderProcessHost
137  // still owns the returned DIB.
138  virtual TransportDIB* GetTransportDIB(TransportDIB::Id dib_id) = 0;
139
140  // Return the TransportDIB for the given id. In contrast to GetTransportDIB,
141  // the caller owns the resulting TransportDIB.
142  virtual TransportDIB* MapTransportDIB(TransportDIB::Id dib_id) = 0;
143
144  // Returns the user browser context associated with this renderer process.
145  virtual content::BrowserContext* GetBrowserContext() const = 0;
146
147  // Returns whether this process is using the same StoragePartition as
148  // |partition|.
149  virtual bool InSameStoragePartition(StoragePartition* partition) const = 0;
150
151  // Returns the unique ID for this child process host. This can be used later
152  // in a call to FromID() to get back to this object (this is used to avoid
153  // sending non-threadsafe pointers to other threads).
154  //
155  // This ID will be unique across all child process hosts, including workers,
156  // plugins, etc.
157  //
158  // This will never return ChildProcessHost::kInvalidUniqueID.
159  virtual int GetID() const = 0;
160
161  // Returns true iff channel_ has been set to non-NULL. Use this for checking
162  // if there is connection or not. Virtual for mocking out for tests.
163  virtual bool HasConnection() const = 0;
164
165  // Call this to allow queueing of IPC messages that are sent before the
166  // process is launched.
167  virtual void EnableSendQueue() = 0;
168
169  // Returns the renderer channel.
170  virtual IPC::ChannelProxy* GetChannel() = 0;
171
172  // Adds a message filter to the IPC channel.
173  virtual void AddFilter(BrowserMessageFilter* filter) = 0;
174
175  // Try to shutdown the associated render process as fast as possible
176  virtual bool FastShutdownForPageCount(size_t count) = 0;
177
178  // TODO(ananta)
179  // Revisit whether the virtual functions declared from here on need to be
180  // part of the interface.
181  virtual void SetIgnoreInputEvents(bool ignore_input_events) = 0;
182  virtual bool IgnoreInputEvents() const = 0;
183
184  // Schedules the host for deletion and removes it from the all_hosts list.
185  virtual void Cleanup() = 0;
186
187  // Track the count of pending views that are being swapped back in.  Called
188  // by listeners to register and unregister pending views to prevent the
189  // process from exiting.
190  virtual void AddPendingView() = 0;
191  virtual void RemovePendingView() = 0;
192
193  // Sets a flag indicating that the process can be abnormally terminated.
194  virtual void SetSuddenTerminationAllowed(bool allowed) = 0;
195  // Returns true if the process can be abnormally terminated.
196  virtual bool SuddenTerminationAllowed() const = 0;
197
198  // Returns how long the child has been idle. The definition of idle
199  // depends on when a derived class calls mark_child_process_activity_time().
200  // This is a rough indicator and its resolution should not be better than
201  // 10 milliseconds.
202  virtual base::TimeDelta GetChildProcessIdleTime() const = 0;
203
204  // Called to resume the requests for a view created through window.open that
205  // were initially blocked.
206  virtual void ResumeRequestsForView(int route_id) = 0;
207
208  // Checks that the given renderer can request |url|, if not it sets it to
209  // about:blank.
210  // |empty_allowed| must be set to false for navigations for security reasons.
211  virtual void FilterURL(bool empty_allowed, GURL* url) = 0;
212
213#if defined(ENABLE_WEBRTC)
214  virtual void EnableAecDump(const base::FilePath& file) = 0;
215  virtual void DisableAecDump() = 0;
216
217  // When set, |callback| receives log messages regarding, for example, media
218  // devices (webcams, mics, etc) that were initially requested in the render
219  // process associated with this RenderProcessHost.
220  virtual void SetWebRtcLogMessageCallback(
221      base::Callback<void(const std::string&)> callback) = 0;
222#endif
223
224  // Tells the ResourceDispatcherHost to resume a deferred navigation without
225  // transferring it to a new renderer process.
226  virtual void ResumeDeferredNavigation(const GlobalRequestID& request_id) = 0;
227
228  // Notifies the renderer that the timezone configuration of the system might
229  // have changed.
230  virtual void NotifyTimezoneChange() = 0;
231
232  // Static management functions -----------------------------------------------
233
234  // Flag to run the renderer in process.  This is primarily
235  // for debugging purposes.  When running "in process", the
236  // browser maintains a single RenderProcessHost which communicates
237  // to a RenderProcess which is instantiated in the same process
238  // with the Browser.  All IPC between the Browser and the
239  // Renderer is the same, it's just not crossing a process boundary.
240
241  static bool run_renderer_in_process();
242
243  // This also calls out to ContentBrowserClient::GetApplicationLocale and
244  // modifies the current process' command line.
245  static void SetRunRendererInProcess(bool value);
246
247  // Allows iteration over all the RenderProcessHosts in the browser. Note
248  // that each host may not be active, and therefore may have NULL channels.
249  static iterator AllHostsIterator();
250
251  // Returns the RenderProcessHost given its ID.  Returns NULL if the ID does
252  // not correspond to a live RenderProcessHost.
253  static RenderProcessHost* FromID(int render_process_id);
254
255  // Returns whether the process-per-site model is in use (globally or just for
256  // the current site), in which case we should ensure there is only one
257  // RenderProcessHost per site for the entire browser context.
258  static bool ShouldUseProcessPerSite(content::BrowserContext* browser_context,
259                                      const GURL& url);
260
261  // Returns true if the caller should attempt to use an existing
262  // RenderProcessHost rather than creating a new one.
263  static bool ShouldTryToUseExistingProcessHost(
264      content::BrowserContext* browser_context, const GURL& site_url);
265
266  // Get an existing RenderProcessHost associated with the given browser
267  // context, if possible.  The renderer process is chosen randomly from
268  // suitable renderers that share the same context and type (determined by the
269  // site url).
270  // Returns NULL if no suitable renderer process is available, in which case
271  // the caller is free to create a new renderer.
272  static RenderProcessHost* GetExistingProcessHost(
273      content::BrowserContext* browser_context, const GURL& site_url);
274
275  // Overrides the default heuristic for limiting the max renderer process
276  // count.  This is useful for unit testing process limit behaviors.  It is
277  // also used to allow a command line parameter to configure the max number of
278  // renderer processes and should only be called once during startup.
279  // A value of zero means to use the default heuristic.
280  static void SetMaxRendererProcessCount(size_t count);
281
282  // Returns the current max number of renderer processes used by the content
283  // module.
284  static size_t GetMaxRendererProcessCount();
285};
286
287}  // namespace content.
288
289#endif  // CONTENT_PUBLIC_BROWSER_RENDER_PROCESS_HOST_H_
290