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_BROWSER_RENDERER_HOST_BROWSER_RENDER_PROCESS_HOST_IMPL_H_
6 #define CONTENT_BROWSER_RENDERER_HOST_BROWSER_RENDER_PROCESS_HOST_IMPL_H_
12 #include "base/memory/scoped_ptr.h"
13 #include "base/process.h"
14 #include "base/timer.h"
15 #include "content/browser/child_process_launcher.h"
16 #include "content/common/content_export.h"
17 #include "content/public/browser/global_request_id.h"
18 #include "content/public/browser/gpu_data_manager_observer.h"
19 #include "content/public/browser/render_process_host.h"
20 #include "ipc/ipc_channel_proxy.h"
21 #include "ui/surface/transport_dib.h"
24 struct ViewHostMsg_CompositorSurfaceBuffersSwapped_Params
;
35 class GpuMessageFilter
;
36 class PeerConnectionTrackerHost
;
37 class RendererMainThread
;
38 class RenderWidgetHelper
;
39 class RenderWidgetHost
;
40 class RenderWidgetHostImpl
;
41 class RenderWidgetHostViewFrameSubscriber
;
42 class StoragePartition
;
43 class StoragePartitionImpl
;
45 // Implements a concrete RenderProcessHost for the browser process for talking
46 // to actual renderer processes (as opposed to mocks).
48 // Represents the browser side of the browser <--> renderer communication
49 // channel. There will be one RenderProcessHost per renderer process.
51 // This object is refcounted so that it can release its resources when all
52 // hosts using it go away.
54 // This object communicates back and forth with the RenderProcess object
55 // running in the renderer process. Each RenderProcessHost and RenderProcess
56 // keeps a list of RenderView (renderer) and WebContentsImpl (browser) which
57 // are correlated with IDs. This way, the Views and the corresponding ViewHosts
58 // communicate through the two process objects.
60 // A RenderProcessHost is also associated with one and only one
61 // StoragePartition. This allows us to implement strong storage isolation
62 // because all the IPCs from the RenderViews (renderer) will only ever be able
63 // to access the partition they are assigned to.
64 class CONTENT_EXPORT RenderProcessHostImpl
65 : public RenderProcessHost
,
66 public ChildProcessLauncher::Client
,
67 public GpuDataManagerObserver
{
69 RenderProcessHostImpl(BrowserContext
* browser_context
,
70 StoragePartitionImpl
* storage_partition_impl
,
71 bool supports_browser_plugin
,
73 virtual ~RenderProcessHostImpl();
75 // RenderProcessHost implementation (public portion).
76 virtual void EnableSendQueue() OVERRIDE
;
77 virtual bool Init() OVERRIDE
;
78 virtual int GetNextRoutingID() OVERRIDE
;
79 virtual void AddRoute(int32 routing_id
, IPC::Listener
* listener
) OVERRIDE
;
80 virtual void RemoveRoute(int32 routing_id
) OVERRIDE
;
81 virtual bool WaitForBackingStoreMsg(int render_widget_id
,
82 const base::TimeDelta
& max_delay
,
83 IPC::Message
* msg
) OVERRIDE
;
84 virtual void ReceivedBadMessage() OVERRIDE
;
85 virtual void WidgetRestored() OVERRIDE
;
86 virtual void WidgetHidden() OVERRIDE
;
87 virtual int VisibleWidgetCount() const OVERRIDE
;
88 virtual bool IsGuest() const OVERRIDE
;
89 virtual StoragePartition
* GetStoragePartition() const OVERRIDE
;
90 virtual bool FastShutdownIfPossible() OVERRIDE
;
91 virtual void DumpHandles() OVERRIDE
;
92 virtual base::ProcessHandle
GetHandle() const OVERRIDE
;
93 virtual TransportDIB
* GetTransportDIB(TransportDIB::Id dib_id
) OVERRIDE
;
94 virtual TransportDIB
* MapTransportDIB(TransportDIB::Id dib_id
) OVERRIDE
;
95 virtual BrowserContext
* GetBrowserContext() const OVERRIDE
;
96 virtual bool InSameStoragePartition(
97 StoragePartition
* partition
) const OVERRIDE
;
98 virtual int GetID() const OVERRIDE
;
99 virtual bool HasConnection() const OVERRIDE
;
100 virtual void SetIgnoreInputEvents(bool ignore_input_events
) OVERRIDE
;
101 virtual bool IgnoreInputEvents() const OVERRIDE
;
102 virtual void Cleanup() OVERRIDE
;
103 virtual void AddPendingView() OVERRIDE
;
104 virtual void RemovePendingView() OVERRIDE
;
105 virtual void SetSuddenTerminationAllowed(bool enabled
) OVERRIDE
;
106 virtual bool SuddenTerminationAllowed() const OVERRIDE
;
107 virtual IPC::ChannelProxy
* GetChannel() OVERRIDE
;
108 virtual bool FastShutdownForPageCount(size_t count
) OVERRIDE
;
109 virtual bool FastShutdownStarted() const OVERRIDE
;
110 virtual base::TimeDelta
GetChildProcessIdleTime() const OVERRIDE
;
111 virtual void SurfaceUpdated(int32 surface_id
) OVERRIDE
;
112 virtual void ResumeRequestsForView(int route_id
) OVERRIDE
;
114 // IPC::Sender via RenderProcessHost.
115 virtual bool Send(IPC::Message
* msg
) OVERRIDE
;
117 // IPC::Listener via RenderProcessHost.
118 virtual bool OnMessageReceived(const IPC::Message
& msg
) OVERRIDE
;
119 virtual void OnChannelConnected(int32 peer_pid
) OVERRIDE
;
120 virtual void OnChannelError() OVERRIDE
;
122 // ChildProcessLauncher::Client implementation.
123 virtual void OnProcessLaunched() OVERRIDE
;
125 // Tells the ResourceDispatcherHost to resume a deferred navigation without
126 // transferring it to a new renderer process.
127 void ResumeDeferredNavigation(const GlobalRequestID
& request_id
);
129 // Call this function when it is evident that the child process is actively
130 // performing some operation, for example if we just received an IPC message.
131 void mark_child_process_activity_time() {
132 child_process_activity_time_
= base::TimeTicks::Now();
135 // Returns the current number of active views in this process. Excludes
136 // any RenderViewHosts that are swapped out.
137 int GetActiveViewCount();
139 // Start and end frame subscription for a specific renderer.
140 // This API only supports subscription to accelerated composited frames.
141 void BeginFrameSubscription(
143 scoped_ptr
<RenderWidgetHostViewFrameSubscriber
> subscriber
);
144 void EndFrameSubscription(int route_id
);
146 // Register/unregister the host identified by the host id in the global host
148 static void RegisterHost(int host_id
, RenderProcessHost
* host
);
149 static void UnregisterHost(int host_id
);
151 // Returns true if |host| is suitable for launching a new view with |site_url|
152 // in the given |browser_context|.
153 static bool IsSuitableHost(RenderProcessHost
* host
,
154 BrowserContext
* browser_context
,
155 const GURL
& site_url
);
157 // Returns an existing RenderProcessHost for |url| in |browser_context|,
158 // if one exists. Otherwise a new RenderProcessHost should be created and
159 // registered using RegisterProcessHostForSite().
160 // This should only be used for process-per-site mode, which can be enabled
161 // globally with a command line flag or per-site, as determined by
162 // SiteInstanceImpl::ShouldUseProcessPerSite.
163 static RenderProcessHost
* GetProcessHostForSite(
164 BrowserContext
* browser_context
,
167 // Registers the given |process| to be used for any instance of |url|
168 // within |browser_context|.
169 // This should only be used for process-per-site mode, which can be enabled
170 // globally with a command line flag or per-site, as determined by
171 // SiteInstanceImpl::ShouldUseProcessPerSite.
172 static void RegisterProcessHostForSite(
173 BrowserContext
* browser_context
,
174 RenderProcessHost
* process
,
177 static base::MessageLoop
* GetInProcessRendererThreadForTesting();
180 // A proxy for our IPC::Channel that lives on the IO thread (see
181 // browser_process.h)
182 scoped_ptr
<IPC::ChannelProxy
> channel_
;
184 // True if fast shutdown has been performed on this RPH.
185 bool fast_shutdown_started_
;
187 // True if we've posted a DeleteTask and will be deleted soon.
190 // The count of currently swapped out but pending RenderViews. We have
191 // started to swap these in, so the renderer process should not exit if
192 // this count is non-zero.
193 int32 pending_views_
;
196 friend class VisitRelayingRenderProcessHost
;
198 // Creates and adds the IO thread message filters.
199 void CreateMessageFilters();
201 // Control message handlers.
202 void OnShutdownRequest();
203 void OnDumpHandlesDone();
204 void SuddenTerminationChanged(bool enabled
);
205 void OnUserMetricsRecordAction(const std::string
& action
);
206 void OnSavedPageAsMHTML(int job_id
, int64 mhtml_file_size
);
208 // CompositorSurfaceBuffersSwapped handler when there's no RWH.
209 void OnCompositorSurfaceBuffersSwappedNoHost(
210 const ViewHostMsg_CompositorSurfaceBuffersSwapped_Params
& params
);
212 // Generates a command line to be used to spawn a renderer and appends the
213 // results to |*command_line|.
214 void AppendRendererCommandLine(CommandLine
* command_line
) const;
216 // Copies applicable command line switches from the given |browser_cmd| line
217 // flags to the output |renderer_cmd| line flags. Not all switches will be
219 void PropagateBrowserCommandLineToRenderer(const CommandLine
& browser_cmd
,
220 CommandLine
* renderer_cmd
) const;
222 // Callers can reduce the RenderProcess' priority.
223 void SetBackgrounded(bool backgrounded
);
225 // Handle termination of our process.
226 void ProcessDied(bool already_dead
);
228 virtual void OnGpuSwitching() OVERRIDE
;
230 // The registered IPC listener objects. When this list is empty, we should
232 IDMap
<IPC::Listener
> listeners_
;
234 // The count of currently visible widgets. Since the host can be a container
235 // for multiple widgets, it uses this count to determine when it should be
237 int32 visible_widgets_
;
239 // Does this process have backgrounded priority.
242 // Used to allow a RenderWidgetHost to intercept various messages on the
244 scoped_refptr
<RenderWidgetHelper
> widget_helper_
;
246 // The filter for GPU-related messages coming from the renderer.
247 // Thread safety note: this field is to be accessed from the UI thread.
248 // We don't keep a reference to it, to avoid it being destroyed on the UI
249 // thread, but we clear this field when we clear channel_. When channel_ goes
250 // away, it posts a task to the IO thread to destroy it there, so we know that
251 // it's valid if non-NULL.
252 GpuMessageFilter
* gpu_message_filter_
;
254 // A map of transport DIB ids to cached TransportDIBs
255 std::map
<TransportDIB::Id
, TransportDIB
*> cached_dibs_
;
258 // This is the maximum size of |cached_dibs_|
259 MAX_MAPPED_TRANSPORT_DIBS
= 3,
262 void ClearTransportDIBCache();
263 // This is used to clear our cache five seconds after the last use.
264 base::DelayTimer
<RenderProcessHostImpl
> cached_dibs_cleaner_
;
266 // Used in single-process mode.
267 scoped_ptr
<RendererMainThread
> in_process_renderer_
;
269 // True after Init() has been called. We can't just check channel_ because we
270 // also reset that in the case of process termination.
271 bool is_initialized_
;
273 // Used to launch and terminate the process without blocking the UI thread.
274 scoped_ptr
<ChildProcessLauncher
> child_process_launcher_
;
276 // Messages we queue while waiting for the process handle. We queue them here
277 // instead of in the channel so that we ensure they're sent after init related
278 // messages that are sent once the process handle is available. This is
279 // because the queued messages may have dependencies on the init messages.
280 std::queue
<IPC::Message
*> queued_messages_
;
282 // The globally-unique identifier for this RPH.
285 BrowserContext
* browser_context_
;
287 // Owned by |browser_context_|.
288 StoragePartitionImpl
* storage_partition_impl_
;
290 // True if the process can be shut down suddenly. If this is true, then we're
291 // sure that all the RenderViews in the process can be shutdown suddenly. If
292 // it's false, then specific RenderViews might still be allowed to be shutdown
293 // suddenly by checking their SuddenTerminationAllowed() flag. This can occur
294 // if one WebContents has an unload event listener but another WebContents in
295 // the same process doesn't.
296 bool sudden_termination_allowed_
;
298 // Set to true if we shouldn't send input events. We actually do the
299 // filtering for this at the render widget level.
300 bool ignore_input_events_
;
302 // Records the last time we regarded the child process active.
303 base::TimeTicks child_process_activity_time_
;
305 // Indicates whether this is a RenderProcessHost that has permission to embed
307 bool supports_browser_plugin_
;
309 // Indicates whether this is a RenderProcessHost of a Browser Plugin guest
313 // Forwards messages between WebRTCInternals in the browser process
314 // and PeerConnectionTracker in the renderer process.
315 scoped_refptr
<PeerConnectionTrackerHost
> peer_connection_tracker_host_
;
317 // Prevents the class from being added as a GpuDataManagerImpl observer more
319 bool gpu_observer_registered_
;
321 DISALLOW_COPY_AND_ASSIGN(RenderProcessHostImpl
);
324 } // namespace content
326 #endif // CONTENT_BROWSER_RENDERER_HOST_BROWSER_RENDER_PROCESS_HOST_IMPL_H_