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