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
;
33 class ResourceDispatcherHostImpl
;
34 class SessionStorageNamespace
;
36 // Instantiated per RenderProcessHost to provide various optimizations on
37 // behalf of a RenderWidgetHost. This class bridges between the IO thread
38 // where the RenderProcessHost's MessageFilter lives and the UI thread where
39 // the RenderWidgetHost lives.
42 // OPTIMIZED TAB SWITCHING
44 // When a RenderWidgetHost is in a background tab, it is flagged as hidden.
45 // This causes the corresponding RenderWidget to stop sending BackingStore
46 // messages. The RenderWidgetHost also discards its backingstore when it is
47 // hidden, which helps free up memory. As a result, when a RenderWidgetHost
48 // is restored, it can be momentarily be without a backingstore. (Restoring
49 // a RenderWidgetHost results in a WasShown message being sent to the
50 // RenderWidget, which triggers a full BackingStore message.) This can lead
51 // to an observed rendering glitch as the WebContentsImpl will just have to
52 // fill white overtop the RenderWidgetHost until the RenderWidgetHost
53 // receives a BackingStore message to refresh its backingstore.
55 // To avoid this 'white flash', the RenderWidgetHost again makes use of the
56 // RenderWidgetHelper's WaitForBackingStoreMsg method. When the
57 // RenderWidgetHost's GetBackingStore method is called, it will call
58 // WaitForBackingStoreMsg if it has no backingstore.
60 // TRANSPORT DIB CREATION
62 // On some platforms (currently the Mac) the renderer cannot create transport
63 // DIBs because of sandbox limitations. Thus, it has to make synchronous IPCs
64 // to the browser for them. Since these requests are synchronous, they cannot
65 // terminate on the UI thread. Thus, in this case, this object performs the
66 // allocation and maintains the set of allocated transport DIBs which the
67 // renderers can refer to.
70 class RenderWidgetHelper
71 : public base::RefCountedThreadSafe
<RenderWidgetHelper
,
72 BrowserThread::DeleteOnIOThread
> {
76 void Init(int render_process_id
,
77 ResourceDispatcherHostImpl
* resource_dispatcher_host
);
79 // Gets the next available routing id. This is thread safe.
80 int GetNextRoutingID();
82 // IO THREAD ONLY -----------------------------------------------------------
84 // Lookup the RenderWidgetHelper from the render_process_host_id. Returns NULL
85 // if not found. NOTE: The raw pointer is for temporary use only. To retain,
86 // store in a scoped_refptr.
87 static RenderWidgetHelper
* FromProcessHostID(int render_process_host_id
);
89 // UI THREAD ONLY -----------------------------------------------------------
91 // These two functions provide the backend implementation of the
92 // corresponding functions in RenderProcessHost. See those declarations
94 void ResumeDeferredNavigation(const GlobalRequestID
& request_id
);
96 // Called to resume the requests for a view after it's ready. The view was
97 // created by CreateNewWindow which initially blocked the requests.
98 void ResumeRequestsForView(int route_id
);
100 // IO THREAD ONLY -----------------------------------------------------------
102 void CreateNewWindow(
103 const ViewHostMsg_CreateWindow_Params
& params
,
104 bool no_javascript_access
,
105 base::ProcessHandle render_process
,
107 int* main_frame_route_id
,
109 SessionStorageNamespace
* session_storage_namespace
);
110 void CreateNewWidget(int opener_id
,
111 blink::WebPopupType popup_type
,
114 void CreateNewFullscreenWidget(int opener_id
, int* route_id
, int* surface_id
);
117 friend class base::RefCountedThreadSafe
<RenderWidgetHelper
>;
118 friend struct BrowserThread::DeleteOnThread
<BrowserThread::IO
>;
119 friend class base::DeleteHelper
<RenderWidgetHelper
>;
121 ~RenderWidgetHelper();
123 // Called on the UI thread to finish creating a window.
124 void OnCreateWindowOnUI(
125 const ViewHostMsg_CreateWindow_Params
& params
,
127 int main_frame_route_id
,
128 SessionStorageNamespace
* session_storage_namespace
);
130 // Called on the IO thread after a window was created on the UI thread.
131 void OnResumeRequestsForView(int route_id
);
133 // Called on the UI thread to finish creating a widget.
134 void OnCreateWidgetOnUI(int opener_id
,
136 blink::WebPopupType popup_type
);
138 // Called on the UI thread to create a fullscreen widget.
139 void OnCreateFullscreenWidgetOnUI(int opener_id
, int route_id
);
141 // Called on the IO thread to resume a paused navigation in the network
142 // stack without transferring it to a new renderer process.
143 void OnResumeDeferredNavigation(const GlobalRequestID
& request_id
);
145 // Called on the IO thread to resume a navigation paused immediately after
146 // receiving response headers.
147 void OnResumeResponseDeferredAtStart(const GlobalRequestID
& request_id
);
149 int render_process_id_
;
151 // The next routing id to use.
152 base::AtomicSequenceNumber next_routing_id_
;
154 ResourceDispatcherHostImpl
* resource_dispatcher_host_
;
156 DISALLOW_COPY_AND_ASSIGN(RenderWidgetHelper
);
159 } // namespace content
161 #endif // CONTENT_BROWSER_RENDERER_HOST_RENDER_WIDGET_HELPER_H_