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 four functions provide the backend implementation of the
93 // corresponding functions in RenderProcessHost. See those declarations
95 void ResumeDeferredNavigation(const GlobalRequestID
& request_id
);
96 void ResumeResponseDeferredAtStart(const GlobalRequestID
& request_id
);
98 // Called to resume the requests for a view after it's ready. The view was
99 // created by CreateNewWindow which initially blocked the requests.
100 void ResumeRequestsForView(int route_id
);
102 // IO THREAD ONLY -----------------------------------------------------------
104 void CreateNewWindow(
105 const ViewHostMsg_CreateWindow_Params
& params
,
106 bool no_javascript_access
,
107 base::ProcessHandle render_process
,
109 int* main_frame_route_id
,
111 SessionStorageNamespace
* session_storage_namespace
);
112 void CreateNewWidget(int opener_id
,
113 blink::WebPopupType popup_type
,
116 void CreateNewFullscreenWidget(int opener_id
, int* route_id
, int* surface_id
);
119 friend class base::RefCountedThreadSafe
<RenderWidgetHelper
>;
120 friend struct BrowserThread::DeleteOnThread
<BrowserThread::IO
>;
121 friend class base::DeleteHelper
<RenderWidgetHelper
>;
123 ~RenderWidgetHelper();
125 // Called on the UI thread to finish creating a window.
126 void OnCreateWindowOnUI(
127 const ViewHostMsg_CreateWindow_Params
& params
,
129 int main_frame_route_id
,
130 SessionStorageNamespace
* session_storage_namespace
);
132 // Called on the IO thread after a window was created on the UI thread.
133 void OnResumeRequestsForView(int route_id
);
135 // Called on the UI thread to finish creating a widget.
136 void OnCreateWidgetOnUI(int opener_id
,
138 blink::WebPopupType popup_type
);
140 // Called on the UI thread to create a fullscreen widget.
141 void OnCreateFullscreenWidgetOnUI(int opener_id
, int route_id
);
143 // Called on the IO thread to resume a paused navigation in the network
144 // stack without transferring it to a new renderer process.
145 void OnResumeDeferredNavigation(const GlobalRequestID
& request_id
);
147 // Called on the IO thread to resume a navigation paused immediately after
148 // receiving response headers.
149 void OnResumeResponseDeferredAtStart(const GlobalRequestID
& request_id
);
151 int render_process_id_
;
153 // The next routing id to use.
154 base::AtomicSequenceNumber next_routing_id_
;
156 ResourceDispatcherHostImpl
* resource_dispatcher_host_
;
158 DISALLOW_COPY_AND_ASSIGN(RenderWidgetHelper
);
161 } // namespace content
163 #endif // CONTENT_BROWSER_RENDERER_HOST_RENDER_WIDGET_HELPER_H_