Update To 11.40.268.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 <map>
9
10 #include "base/atomic_sequence_num.h"
11 #include "base/containers/hash_tables.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/process/process.h"
14 #include "content/public/browser/browser_thread.h"
15 #include "content/public/browser/content_browser_client.h"
16 #include "content/public/browser/global_request_id.h"
17 #include "content/public/common/window_container_type.h"
18 #include "third_party/WebKit/public/web/WebPopupType.h"
19 #include "ui/gfx/native_widget_types.h"
20 #include "ui/surface/transport_dib.h"
21
22 namespace IPC {
23 class Message;
24 }
25
26 namespace base {
27 class TimeDelta;
28 }
29
30 struct ViewHostMsg_CreateWindow_Params;
31 struct ViewMsg_SwapOut_Params;
32
33 namespace content {
34 class GpuProcessHost;
35 class ResourceDispatcherHostImpl;
36 class SessionStorageNamespace;
37
38 // Instantiated per RenderProcessHost to provide various optimizations on
39 // behalf of a RenderWidgetHost.  This class bridges between the IO thread
40 // where the RenderProcessHost's MessageFilter lives and the UI thread where
41 // the RenderWidgetHost lives.
42 //
43 //
44 // OPTIMIZED TAB SWITCHING
45 //
46 //   When a RenderWidgetHost is in a background tab, it is flagged as hidden.
47 //   This causes the corresponding RenderWidget to stop sending BackingStore
48 //   messages. The RenderWidgetHost also discards its backingstore when it is
49 //   hidden, which helps free up memory.  As a result, when a RenderWidgetHost
50 //   is restored, it can be momentarily be without a backingstore.  (Restoring
51 //   a RenderWidgetHost results in a WasShown message being sent to the
52 //   RenderWidget, which triggers a full BackingStore message.)  This can lead
53 //   to an observed rendering glitch as the WebContentsImpl will just have to
54 //   fill white overtop the RenderWidgetHost until the RenderWidgetHost
55 //   receives a BackingStore message to refresh its backingstore.
56 //
57 //   To avoid this 'white flash', the RenderWidgetHost again makes use of the
58 //   RenderWidgetHelper's WaitForBackingStoreMsg method.  When the
59 //   RenderWidgetHost's GetBackingStore method is called, it will call
60 //   WaitForBackingStoreMsg if it has no backingstore.
61 //
62 // TRANSPORT DIB CREATION
63 //
64 //   On some platforms (currently the Mac) the renderer cannot create transport
65 //   DIBs because of sandbox limitations. Thus, it has to make synchronous IPCs
66 //   to the browser for them. Since these requests are synchronous, they cannot
67 //   terminate on the UI thread. Thus, in this case, this object performs the
68 //   allocation and maintains the set of allocated transport DIBs which the
69 //   renderers can refer to.
70 //
71
72 class RenderWidgetHelper
73     : public base::RefCountedThreadSafe<RenderWidgetHelper,
74                                         BrowserThread::DeleteOnIOThread> {
75  public:
76   RenderWidgetHelper();
77
78   void Init(int render_process_id,
79             ResourceDispatcherHostImpl* resource_dispatcher_host);
80
81   // Gets the next available routing id.  This is thread safe.
82   int GetNextRoutingID();
83
84   // IO THREAD ONLY -----------------------------------------------------------
85
86   // Lookup the RenderWidgetHelper from the render_process_host_id. Returns NULL
87   // if not found. NOTE: The raw pointer is for temporary use only. To retain,
88   // store in a scoped_refptr.
89   static RenderWidgetHelper* FromProcessHostID(int render_process_host_id);
90
91   // UI THREAD ONLY -----------------------------------------------------------
92
93   // These four functions provide the backend implementation of the
94   // corresponding functions in RenderProcessHost. See those declarations
95   // for documentation.
96   void ResumeDeferredNavigation(const GlobalRequestID& request_id);
97   void ResumeResponseDeferredAtStart(const GlobalRequestID& request_id);
98
99   // Called to resume the requests for a view after it's ready. The view was
100   // created by CreateNewWindow which initially blocked the requests.
101   void ResumeRequestsForView(int route_id);
102
103   // IO THREAD ONLY -----------------------------------------------------------
104
105   void CreateNewWindow(
106       const ViewHostMsg_CreateWindow_Params& params,
107       bool no_javascript_access,
108       base::ProcessHandle render_process,
109       int* route_id,
110       int* main_frame_route_id,
111       int* surface_id,
112       SessionStorageNamespace* session_storage_namespace);
113   void CreateNewWidget(int opener_id,
114                        blink::WebPopupType popup_type,
115                        int* route_id,
116                        int* surface_id);
117   void CreateNewFullscreenWidget(int opener_id, int* route_id, int* surface_id);
118
119 #if defined(OS_POSIX)
120   // Called on the IO thread to handle the allocation of a TransportDIB.  If
121   // |cache_in_browser| is |true|, then a copy of the shmem is kept by the
122   // browser, and it is the caller's repsonsibility to call
123   // FreeTransportDIB().  In all cases, the caller is responsible for deleting
124   // the resulting TransportDIB.
125   void AllocTransportDIB(uint32 size,
126                          bool cache_in_browser,
127                          TransportDIB::Handle* result);
128
129   // Called on the IO thread to handle the freeing of a transport DIB
130   void FreeTransportDIB(TransportDIB::Id dib_id);
131 #endif
132
133  private:
134   friend class base::RefCountedThreadSafe<RenderWidgetHelper>;
135   friend struct BrowserThread::DeleteOnThread<BrowserThread::IO>;
136   friend class base::DeleteHelper<RenderWidgetHelper>;
137
138   ~RenderWidgetHelper();
139
140   // Called on the UI thread to finish creating a window.
141   void OnCreateWindowOnUI(
142       const ViewHostMsg_CreateWindow_Params& params,
143       int route_id,
144       int main_frame_route_id,
145       SessionStorageNamespace* session_storage_namespace);
146
147   // Called on the IO thread after a window was created on the UI thread.
148   void OnResumeRequestsForView(int route_id);
149
150   // Called on the UI thread to finish creating a widget.
151   void OnCreateWidgetOnUI(int opener_id,
152                           int route_id,
153                           blink::WebPopupType popup_type);
154
155   // Called on the UI thread to create a fullscreen widget.
156   void OnCreateFullscreenWidgetOnUI(int opener_id, int route_id);
157
158   // Called on the IO thread to resume a paused navigation in the network
159   // stack without transferring it to a new renderer process.
160   void OnResumeDeferredNavigation(const GlobalRequestID& request_id);
161
162   // Called on the IO thread to resume a navigation paused immediately after
163   // receiving response headers.
164   void OnResumeResponseDeferredAtStart(const GlobalRequestID& request_id);
165
166 #if defined(OS_POSIX)
167   // Called on destruction to release all allocated transport DIBs
168   void ClearAllocatedDIBs();
169
170   // On POSIX we keep file descriptors to all the allocated DIBs around until
171   // the renderer frees them.
172   base::Lock allocated_dibs_lock_;
173   std::map<TransportDIB::Id, int> allocated_dibs_;
174 #endif
175
176   int render_process_id_;
177
178   // The next routing id to use.
179   base::AtomicSequenceNumber next_routing_id_;
180
181   ResourceDispatcherHostImpl* resource_dispatcher_host_;
182
183   DISALLOW_COPY_AND_ASSIGN(RenderWidgetHelper);
184 };
185
186 }  // namespace content
187
188 #endif  // CONTENT_BROWSER_RENDERER_HOST_RENDER_WIDGET_HELPER_H_