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_BROWSER_RENDERER_HOST_RENDER_WIDGET_HELPER_H_
6#define CONTENT_BROWSER_RENDERER_HOST_RENDER_WIDGET_HELPER_H_
7
8#include <deque>
9#include <map>
10
11#include "base/atomic_sequence_num.h"
12#include "base/containers/hash_tables.h"
13#include "base/memory/ref_counted.h"
14#include "base/process/process.h"
15#include "base/synchronization/lock.h"
16#include "base/synchronization/waitable_event.h"
17#include "content/public/browser/browser_thread.h"
18#include "content/public/browser/content_browser_client.h"
19#include "content/public/browser/global_request_id.h"
20#include "content/public/common/window_container_type.h"
21#include "third_party/WebKit/public/web/WebPopupType.h"
22#include "ui/gfx/native_widget_types.h"
23#include "ui/surface/transport_dib.h"
24
25namespace IPC {
26class Message;
27}
28
29namespace base {
30class TimeDelta;
31}
32
33struct ViewHostMsg_CreateWindow_Params;
34struct ViewMsg_SwapOut_Params;
35
36namespace content {
37class ResourceDispatcherHostImpl;
38class SessionStorageNamespace;
39
40// Instantiated per RenderProcessHost to provide various optimizations on
41// behalf of a RenderWidgetHost.  This class bridges between the IO thread
42// where the RenderProcessHost's MessageFilter lives and the UI thread where
43// the RenderWidgetHost lives.
44//
45//
46// OPTIMIZED RESIZE
47//
48//   RenderWidgetHelper is used to implement optimized resize.  When the
49//   RenderWidgetHost is resized, it sends a Resize message to its RenderWidget
50//   counterpart in the renderer process.  In response to the Resize message,
51//   the RenderWidget generates a new BackingStore and sends an UpdateRect
52//   message (or BuffersSwapped via the GPU process in the case of accelerated
53//   compositing), and it sets the IS_RESIZE_ACK flag in the UpdateRect message
54//   to true.  In the accelerated case, an UpdateRect is still sent from the
55//   renderer to the browser with acks and plugin moves even though the GPU
56//   BackingStore was sent earlier in the BuffersSwapped message. "BackingStore
57//   message" is used throughout this code and documentation to mean either a
58//   software UpdateRect or GPU BuffersSwapped message.
59//
60//   Back in the browser process, when the RenderProcessHost's MessageFilter
61//   sees an UpdateRect message (or when the GpuProcessHost sees a
62//   BuffersSwapped message), it directs it to the RenderWidgetHelper by calling
63//   the DidReceiveBackingStoreMsg method. That method stores the data for the
64//   message in a map, where it can be directly accessed by the RenderWidgetHost
65//   on the UI thread during a call to RenderWidgetHost's GetBackingStore
66//   method.
67//
68//   When the RenderWidgetHost's GetBackingStore method is called, it first
69//   checks to see if it is waiting for a resize ack.  If it is, then it calls
70//   the RenderWidgetHelper's WaitForBackingStoreMsg to check if there is
71//   already a resulting BackingStore message (or to wait a short amount of time
72//   for one to arrive).  The main goal of this mechanism is to short-cut the
73//   usual way in which IPC messages are proxied over to the UI thread via
74//   InvokeLater. This approach is necessary since window resize is followed up
75//   immediately by a request to repaint the window.
76//
77//
78// OPTIMIZED TAB SWITCHING
79//
80//   When a RenderWidgetHost is in a background tab, it is flagged as hidden.
81//   This causes the corresponding RenderWidget to stop sending BackingStore
82//   messages. The RenderWidgetHost also discards its backingstore when it is
83//   hidden, which helps free up memory.  As a result, when a RenderWidgetHost
84//   is restored, it can be momentarily be without a backingstore.  (Restoring
85//   a RenderWidgetHost results in a WasShown message being sent to the
86//   RenderWidget, which triggers a full BackingStore message.)  This can lead
87//   to an observed rendering glitch as the WebContentsImpl will just have to
88//   fill white overtop the RenderWidgetHost until the RenderWidgetHost
89//   receives a BackingStore message to refresh its backingstore.
90//
91//   To avoid this 'white flash', the RenderWidgetHost again makes use of the
92//   RenderWidgetHelper's WaitForBackingStoreMsg method.  When the
93//   RenderWidgetHost's GetBackingStore method is called, it will call
94//   WaitForBackingStoreMsg if it has no backingstore.
95//
96// TRANSPORT DIB CREATION
97//
98//   On some platforms (currently the Mac) the renderer cannot create transport
99//   DIBs because of sandbox limitations. Thus, it has to make synchronous IPCs
100//   to the browser for them. Since these requests are synchronous, they cannot
101//   terminate on the UI thread. Thus, in this case, this object performs the
102//   allocation and maintains the set of allocated transport DIBs which the
103//   renderers can refer to.
104//
105class RenderWidgetHelper
106    : public base::RefCountedThreadSafe<RenderWidgetHelper,
107                                        BrowserThread::DeleteOnIOThread> {
108 public:
109  RenderWidgetHelper();
110
111  void Init(int render_process_id,
112            ResourceDispatcherHostImpl* resource_dispatcher_host);
113
114  // Gets the next available routing id.  This is thread safe.
115  int GetNextRoutingID();
116
117  // IO THREAD ONLY -----------------------------------------------------------
118
119  // Lookup the RenderWidgetHelper from the render_process_host_id. Returns NULL
120  // if not found. NOTE: The raw pointer is for temporary use only. To retain,
121  // store in a scoped_refptr.
122  static RenderWidgetHelper* FromProcessHostID(int render_process_host_id);
123
124  // UI THREAD ONLY -----------------------------------------------------------
125
126  // These three functions provide the backend implementation of the
127  // corresponding functions in RenderProcessHost. See those declarations
128  // for documentation.
129  void ResumeDeferredNavigation(const GlobalRequestID& request_id);
130  bool WaitForBackingStoreMsg(int render_widget_id,
131                              const base::TimeDelta& max_delay,
132                              IPC::Message* msg);
133  // Called to resume the requests for a view after it's ready. The view was
134  // created by CreateNewWindow which initially blocked the requests.
135  void ResumeRequestsForView(int route_id);
136
137#if defined(OS_POSIX) && !defined(TOOLKIT_GTK) && !defined(OS_ANDROID)
138  // Given the id of a transport DIB, return a mapping to it or NULL on error.
139  TransportDIB* MapTransportDIB(TransportDIB::Id dib_id);
140#endif
141
142  // IO THREAD ONLY -----------------------------------------------------------
143
144  // Called on the IO thread when a BackingStore message is received.
145  void DidReceiveBackingStoreMsg(const IPC::Message& msg);
146
147  void CreateNewWindow(
148      const ViewHostMsg_CreateWindow_Params& params,
149      bool no_javascript_access,
150      base::ProcessHandle render_process,
151      int* route_id,
152      int* main_frame_route_id,
153      int* surface_id,
154      SessionStorageNamespace* session_storage_namespace);
155  void CreateNewWidget(int opener_id,
156                       WebKit::WebPopupType popup_type,
157                       int* route_id,
158                       int* surface_id);
159  void CreateNewFullscreenWidget(int opener_id, int* route_id, int* surface_id);
160
161#if defined(OS_POSIX)
162  // Called on the IO thread to handle the allocation of a TransportDIB.  If
163  // |cache_in_browser| is |true|, then a copy of the shmem is kept by the
164  // browser, and it is the caller's repsonsibility to call
165  // FreeTransportDIB().  In all cases, the caller is responsible for deleting
166  // the resulting TransportDIB.
167  void AllocTransportDIB(uint32 size,
168                         bool cache_in_browser,
169                         TransportDIB::Handle* result);
170
171  // Called on the IO thread to handle the freeing of a transport DIB
172  void FreeTransportDIB(TransportDIB::Id dib_id);
173#endif
174
175 private:
176  // A class used to proxy a paint message.  PaintMsgProxy objects are created
177  // on the IO thread and destroyed on the UI thread.
178  class BackingStoreMsgProxy;
179  friend class BackingStoreMsgProxy;
180  friend class base::RefCountedThreadSafe<RenderWidgetHelper>;
181  friend struct BrowserThread::DeleteOnThread<BrowserThread::IO>;
182  friend class base::DeleteHelper<RenderWidgetHelper>;
183
184  typedef std::deque<BackingStoreMsgProxy*> BackingStoreMsgProxyQueue;
185  // Map from render_widget_id to a queue of live PaintMsgProxy instances.
186  typedef base::hash_map<int, BackingStoreMsgProxyQueue >
187      BackingStoreMsgProxyMap;
188
189  ~RenderWidgetHelper();
190
191  // Called on the UI thread to discard a paint message.
192  void OnDiscardBackingStoreMsg(BackingStoreMsgProxy* proxy);
193
194  // Called on the UI thread to dispatch a paint message if necessary.
195  void OnDispatchBackingStoreMsg(BackingStoreMsgProxy* proxy);
196
197  // Called on the UI thread to finish creating a window.
198  void OnCreateWindowOnUI(
199      const ViewHostMsg_CreateWindow_Params& params,
200      int route_id,
201      int main_frame_route_id,
202      SessionStorageNamespace* session_storage_namespace);
203
204  // Called on the IO thread after a window was created on the UI thread.
205  void OnResumeRequestsForView(int route_id);
206
207  // Called on the UI thread to finish creating a widget.
208  void OnCreateWidgetOnUI(int opener_id,
209                          int route_id,
210                          WebKit::WebPopupType popup_type);
211
212  // Called on the UI thread to create a fullscreen widget.
213  void OnCreateFullscreenWidgetOnUI(int opener_id, int route_id);
214
215  // Called on the IO thread to resume a paused navigation in the network
216  // stack without transferring it to a new renderer process.
217  void OnResumeDeferredNavigation(const GlobalRequestID& request_id);
218
219#if defined(OS_POSIX)
220  // Called on destruction to release all allocated transport DIBs
221  void ClearAllocatedDIBs();
222
223  // On POSIX we keep file descriptors to all the allocated DIBs around until
224  // the renderer frees them.
225  base::Lock allocated_dibs_lock_;
226  std::map<TransportDIB::Id, int> allocated_dibs_;
227#endif
228
229  // A map of live paint messages.  Must hold pending_paints_lock_ to access.
230  // The BackingStoreMsgProxy objects are not owned by this map. (See
231  // BackingStoreMsgProxy for details about how the lifetime of instances are
232  // managed.)
233  BackingStoreMsgProxyMap pending_paints_;
234  base::Lock pending_paints_lock_;
235
236  int render_process_id_;
237
238  // Event used to implement WaitForBackingStoreMsg.
239  base::WaitableEvent event_;
240
241  // The next routing id to use.
242  base::AtomicSequenceNumber next_routing_id_;
243
244  ResourceDispatcherHostImpl* resource_dispatcher_host_;
245
246  DISALLOW_COPY_AND_ASSIGN(RenderWidgetHelper);
247};
248
249}  // namespace content
250
251#endif  // CONTENT_BROWSER_RENDERER_HOST_RENDER_WIDGET_HELPER_H_
252