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_RENDER_WIDGET_HELPER_H_
6 #define CONTENT_BROWSER_RENDERER_HOST_RENDER_WIDGET_HELPER_H_
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"
29 struct ViewHostMsg_CreateWindow_Params
;
30 struct ViewMsg_SwapOut_Params
;
34 class ResourceDispatcherHostImpl
;
35 class SessionStorageNamespace
;
37 // Instantiated per RenderProcessHost to provide various optimizations on
38 // behalf of a RenderWidgetHost. This class bridges between the IO thread
39 // where the RenderProcessHost's MessageFilter lives and the UI thread where
40 // the RenderWidgetHost lives.
43 // OPTIMIZED TAB SWITCHING
45 // When a RenderWidgetHost is in a background tab, it is flagged as hidden.
46 // This causes the corresponding RenderWidget to stop sending BackingStore
47 // messages. The RenderWidgetHost also discards its backingstore when it is
48 // hidden, which helps free up memory. As a result, when a RenderWidgetHost
49 // is restored, it can be momentarily be without a backingstore. (Restoring
50 // a RenderWidgetHost results in a WasShown message being sent to the
51 // RenderWidget, which triggers a full BackingStore message.) This can lead
52 // to an observed rendering glitch as the WebContentsImpl will just have to
53 // fill white overtop the RenderWidgetHost until the RenderWidgetHost
54 // receives a BackingStore message to refresh its backingstore.
56 // To avoid this 'white flash', the RenderWidgetHost again makes use of the
57 // RenderWidgetHelper's WaitForBackingStoreMsg method. When the
58 // RenderWidgetHost's GetBackingStore method is called, it will call
59 // WaitForBackingStoreMsg if it has no backingstore.
61 // TRANSPORT DIB CREATION
63 // On some platforms (currently the Mac) the renderer cannot create transport
64 // DIBs because of sandbox limitations. Thus, it has to make synchronous IPCs
65 // to the browser for them. Since these requests are synchronous, they cannot
66 // terminate on the UI thread. Thus, in this case, this object performs the
67 // allocation and maintains the set of allocated transport DIBs which the
68 // renderers can refer to.
71 class RenderWidgetHelper
72 : public base::RefCountedThreadSafe
<RenderWidgetHelper
,
73 BrowserThread::DeleteOnIOThread
> {
77 void Init(int render_process_id
,
78 ResourceDispatcherHostImpl
* resource_dispatcher_host
);
80 // Gets the next available routing id. This is thread safe.
81 int GetNextRoutingID();
83 // IO THREAD ONLY -----------------------------------------------------------
85 // Lookup the RenderWidgetHelper from the render_process_host_id. Returns NULL
86 // if not found. NOTE: The raw pointer is for temporary use only. To retain,
87 // store in a scoped_refptr.
88 static RenderWidgetHelper
* FromProcessHostID(int render_process_host_id
);
90 // UI THREAD ONLY -----------------------------------------------------------
92 // These two functions provide the backend implementation of the
93 // corresponding functions in RenderProcessHost. See those declarations
95 void ResumeDeferredNavigation(const GlobalRequestID
& request_id
);
97 // Called to resume the requests for a view after it's ready. The view was
98 // created by CreateNewWindow which initially blocked the requests.
99 void ResumeRequestsForView(int route_id
);
101 // IO THREAD ONLY -----------------------------------------------------------
103 void CreateNewWindow(
104 const ViewHostMsg_CreateWindow_Params
& params
,
105 bool no_javascript_access
,
106 base::ProcessHandle render_process
,
108 int* main_frame_route_id
,
110 SessionStorageNamespace
* session_storage_namespace
);
111 void CreateNewWidget(int opener_id
,
112 blink::WebPopupType popup_type
,
115 void CreateNewFullscreenWidget(int opener_id
, int* route_id
, int* surface_id
);
118 friend class base::RefCountedThreadSafe
<RenderWidgetHelper
>;
119 friend struct BrowserThread::DeleteOnThread
<BrowserThread::IO
>;
120 friend class base::DeleteHelper
<RenderWidgetHelper
>;
122 ~RenderWidgetHelper();
124 // Called on the UI thread to finish creating a window.
125 void OnCreateWindowOnUI(
126 const ViewHostMsg_CreateWindow_Params
& params
,
128 int main_frame_route_id
,
129 SessionStorageNamespace
* session_storage_namespace
);
131 // Called on the IO thread after a window was created on the UI thread.
132 void OnResumeRequestsForView(int route_id
);
134 // Called on the UI thread to finish creating a widget.
135 void OnCreateWidgetOnUI(int opener_id
,
137 blink::WebPopupType popup_type
);
139 // Called on the UI thread to create a fullscreen widget.
140 void OnCreateFullscreenWidgetOnUI(int opener_id
, int route_id
);
142 // Called on the IO thread to resume a paused navigation in the network
143 // stack without transferring it to a new renderer process.
144 void OnResumeDeferredNavigation(const GlobalRequestID
& request_id
);
146 // Called on the IO thread to resume a navigation paused immediately after
147 // receiving response headers.
148 void OnResumeResponseDeferredAtStart(const GlobalRequestID
& request_id
);
150 int render_process_id_
;
152 // The next routing id to use.
153 base::AtomicSequenceNumber next_routing_id_
;
155 ResourceDispatcherHostImpl
* resource_dispatcher_host_
;
157 DISALLOW_COPY_AND_ASSIGN(RenderWidgetHelper
);
160 } // namespace content
162 #endif // CONTENT_BROWSER_RENDERER_HOST_RENDER_WIDGET_HELPER_H_