Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / content / browser / renderer_host / render_widget_helper.h
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
25 namespace IPC {
26 class Message;
27 }
28
29 namespace base {
30 class TimeDelta;
31 }
32
33 struct ViewHostMsg_CreateWindow_Params;
34 struct ViewMsg_SwapOut_Params;
35
36 namespace content {
37 class ResourceDispatcherHostImpl;
38 class 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 //
105 class 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   // IO THREAD ONLY -----------------------------------------------------------
138
139   // Called on the IO thread when a BackingStore message is received.
140   void DidReceiveBackingStoreMsg(const IPC::Message& msg);
141
142   void CreateNewWindow(
143       const ViewHostMsg_CreateWindow_Params& params,
144       bool no_javascript_access,
145       base::ProcessHandle render_process,
146       int* route_id,
147       int* main_frame_route_id,
148       int* surface_id,
149       SessionStorageNamespace* session_storage_namespace);
150   void CreateNewWidget(int opener_id,
151                        blink::WebPopupType popup_type,
152                        int* route_id,
153                        int* surface_id);
154   void CreateNewFullscreenWidget(int opener_id, int* route_id, int* surface_id);
155
156 #if defined(OS_POSIX)
157   // Called on the IO thread to handle the allocation of a TransportDIB.  If
158   // |cache_in_browser| is |true|, then a copy of the shmem is kept by the
159   // browser, and it is the caller's repsonsibility to call
160   // FreeTransportDIB().  In all cases, the caller is responsible for deleting
161   // the resulting TransportDIB.
162   void AllocTransportDIB(uint32 size,
163                          bool cache_in_browser,
164                          TransportDIB::Handle* result);
165
166   // Called on the IO thread to handle the freeing of a transport DIB
167   void FreeTransportDIB(TransportDIB::Id dib_id);
168 #endif
169
170  private:
171   // A class used to proxy a paint message.  PaintMsgProxy objects are created
172   // on the IO thread and destroyed on the UI thread.
173   class BackingStoreMsgProxy;
174   friend class BackingStoreMsgProxy;
175   friend class base::RefCountedThreadSafe<RenderWidgetHelper>;
176   friend struct BrowserThread::DeleteOnThread<BrowserThread::IO>;
177   friend class base::DeleteHelper<RenderWidgetHelper>;
178
179   typedef std::deque<BackingStoreMsgProxy*> BackingStoreMsgProxyQueue;
180   // Map from render_widget_id to a queue of live PaintMsgProxy instances.
181   typedef base::hash_map<int, BackingStoreMsgProxyQueue >
182       BackingStoreMsgProxyMap;
183
184   ~RenderWidgetHelper();
185
186   // Called on the UI thread to discard a paint message.
187   void OnDiscardBackingStoreMsg(BackingStoreMsgProxy* proxy);
188
189   // Called on the UI thread to dispatch a paint message if necessary.
190   void OnDispatchBackingStoreMsg(BackingStoreMsgProxy* proxy);
191
192   // Called on the UI thread to finish creating a window.
193   void OnCreateWindowOnUI(
194       const ViewHostMsg_CreateWindow_Params& params,
195       int route_id,
196       int main_frame_route_id,
197       SessionStorageNamespace* session_storage_namespace);
198
199   // Called on the IO thread after a window was created on the UI thread.
200   void OnResumeRequestsForView(int route_id);
201
202   // Called on the UI thread to finish creating a widget.
203   void OnCreateWidgetOnUI(int opener_id,
204                           int route_id,
205                           blink::WebPopupType popup_type);
206
207   // Called on the UI thread to create a fullscreen widget.
208   void OnCreateFullscreenWidgetOnUI(int opener_id, int route_id);
209
210   // Called on the IO thread to resume a paused navigation in the network
211   // stack without transferring it to a new renderer process.
212   void OnResumeDeferredNavigation(const GlobalRequestID& request_id);
213
214 #if defined(OS_POSIX)
215   // Called on destruction to release all allocated transport DIBs
216   void ClearAllocatedDIBs();
217
218   // On POSIX we keep file descriptors to all the allocated DIBs around until
219   // the renderer frees them.
220   base::Lock allocated_dibs_lock_;
221   std::map<TransportDIB::Id, int> allocated_dibs_;
222 #endif
223
224   // A map of live paint messages.  Must hold pending_paints_lock_ to access.
225   // The BackingStoreMsgProxy objects are not owned by this map. (See
226   // BackingStoreMsgProxy for details about how the lifetime of instances are
227   // managed.)
228   BackingStoreMsgProxyMap pending_paints_;
229   base::Lock pending_paints_lock_;
230
231   int render_process_id_;
232
233   // Event used to implement WaitForBackingStoreMsg.
234   base::WaitableEvent event_;
235
236   // The next routing id to use.
237   base::AtomicSequenceNumber next_routing_id_;
238
239   ResourceDispatcherHostImpl* resource_dispatcher_host_;
240
241   DISALLOW_COPY_AND_ASSIGN(RenderWidgetHelper);
242 };
243
244 }  // namespace content
245
246 #endif  // CONTENT_BROWSER_RENDERER_HOST_RENDER_WIDGET_HELPER_H_