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