[email protected] | 9f4f332 | 2012-01-18 22:29:56 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 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_ |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 7 | |
[email protected] | aa4117f | 2011-12-09 22:19:21 | [diff] [blame] | 8 | #include <deque> |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 9 | #include <map> |
| 10 | |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 11 | #include "base/atomic_sequence_num.h" |
[email protected] | 14c1c23 | 2013-06-11 17:52:44 | [diff] [blame] | 12 | #include "base/containers/hash_tables.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 13 | #include "base/memory/ref_counted.h" |
[email protected] | fa20e00 | 2013-07-23 21:20:54 | [diff] [blame] | 14 | #include "base/process/process.h" |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 15 | #include "base/synchronization/lock.h" |
| 16 | #include "base/synchronization/waitable_event.h" |
[email protected] | c63b4d4 | 2012-04-26 01:01:07 | [diff] [blame] | 17 | #include "content/public/browser/browser_thread.h" |
[email protected] | 03b6d55 | 2012-03-29 04:03:01 | [diff] [blame] | 18 | #include "content/public/browser/content_browser_client.h" |
[email protected] | e198683 | 2013-06-14 07:27:28 | [diff] [blame] | 19 | #include "content/public/browser/global_request_id.h" |
[email protected] | 87f3c08 | 2011-10-19 18:07:44 | [diff] [blame] | 20 | #include "content/public/common/window_container_type.h" |
[email protected] | 2255a933 | 2013-06-17 05:12:31 | [diff] [blame] | 21 | #include "third_party/WebKit/public/web/WebPopupType.h" |
[email protected] | 2d7c855 | 2011-06-27 19:21:55 | [diff] [blame] | 22 | #include "ui/gfx/native_widget_types.h" |
[email protected] | d353541f | 2012-05-03 22:45:41 | [diff] [blame] | 23 | #include "ui/surface/transport_dib.h" |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 24 | |
| 25 | namespace IPC { |
| 26 | class Message; |
| 27 | } |
| 28 | |
| 29 | namespace base { |
| 30 | class TimeDelta; |
| 31 | } |
| 32 | |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 33 | struct ViewHostMsg_CreateWindow_Params; |
[email protected] | 992db4c | 2011-05-12 15:37:15 | [diff] [blame] | 34 | struct ViewMsg_SwapOut_Params; |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 35 | |
[email protected] | fc4616f | 2012-07-21 01:29:58 | [diff] [blame] | 36 | namespace content { |
| 37 | class ResourceDispatcherHostImpl; |
| 38 | class SessionStorageNamespace; |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 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 |
[email protected] | c63b4d4 | 2012-04-26 01:01:07 | [diff] [blame] | 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. |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 59 | // |
| 60 | // Back in the browser process, when the RenderProcessHost's MessageFilter |
[email protected] | c63b4d4 | 2012-04-26 01:01:07 | [diff] [blame] | 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. |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 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 |
[email protected] | c63b4d4 | 2012-04-26 01:01:07 | [diff] [blame] | 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. |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 76 | // |
| 77 | // |
| 78 | // OPTIMIZED TAB SWITCHING |
| 79 | // |
| 80 | // When a RenderWidgetHost is in a background tab, it is flagged as hidden. |
[email protected] | c63b4d4 | 2012-04-26 01:01:07 | [diff] [blame] | 81 | // This causes the corresponding RenderWidget to stop sending BackingStore |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 82 | // messages. The RenderWidgetHost also discards its backingstore when it is |
| 83 | // hidden, which helps free up memory. As a result, when a RenderWidgetHost |
[email protected] | 9e2e463 | 2012-07-27 16:38:41 | [diff] [blame] | 84 | // is restored, it can be momentarily be without a backingstore. (Restoring |
| 85 | // a RenderWidgetHost results in a WasShown message being sent to the |
[email protected] | c63b4d4 | 2012-04-26 01:01:07 | [diff] [blame] | 86 | // RenderWidget, which triggers a full BackingStore message.) This can lead |
| 87 | // to an observed rendering glitch as the WebContentsImpl will just have to |
[email protected] | 9e2e463 | 2012-07-27 16:38:41 | [diff] [blame] | 88 | // fill white overtop the RenderWidgetHost until the RenderWidgetHost |
| 89 | // receives a BackingStore message to refresh its backingstore. |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 90 | // |
| 91 | // To avoid this 'white flash', the RenderWidgetHost again makes use of the |
[email protected] | c63b4d4 | 2012-04-26 01:01:07 | [diff] [blame] | 92 | // RenderWidgetHelper's WaitForBackingStoreMsg method. When the |
| 93 | // RenderWidgetHost's GetBackingStore method is called, it will call |
| 94 | // WaitForBackingStoreMsg if it has no backingstore. |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 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 |
[email protected] | fc4616f | 2012-07-21 01:29:58 | [diff] [blame] | 106 | : public base::RefCountedThreadSafe<RenderWidgetHelper, |
| 107 | BrowserThread::DeleteOnIOThread> { |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 108 | public: |
| 109 | RenderWidgetHelper(); |
| 110 | |
| 111 | void Init(int render_process_id, |
[email protected] | fc4616f | 2012-07-21 01:29:58 | [diff] [blame] | 112 | ResourceDispatcherHostImpl* resource_dispatcher_host); |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 113 | |
| 114 | // Gets the next available routing id. This is thread safe. |
| 115 | int GetNextRoutingID(); |
| 116 | |
[email protected] | c63b4d4 | 2012-04-26 01:01:07 | [diff] [blame] | 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); |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 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. |
[email protected] | e198683 | 2013-06-14 07:27:28 | [diff] [blame] | 129 | void ResumeDeferredNavigation(const GlobalRequestID& request_id); |
[email protected] | c63b4d4 | 2012-04-26 01:01:07 | [diff] [blame] | 130 | bool WaitForBackingStoreMsg(int render_widget_id, |
| 131 | const base::TimeDelta& max_delay, |
| 132 | IPC::Message* msg); |
[email protected] | aacd1d7 | 2012-07-23 17:35:09 | [diff] [blame] | 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); |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 136 | |
[email protected] | 1dfb8bef | 2013-05-17 07:31:44 | [diff] [blame] | 137 | #if defined(OS_POSIX) && !defined(TOOLKIT_GTK) && !defined(OS_ANDROID) |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 138 | // Given the id of a transport DIB, return a mapping to it or NULL on error. |
| 139 | TransportDIB* MapTransportDIB(TransportDIB::Id dib_id); |
| 140 | #endif |
| 141 | |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 142 | // IO THREAD ONLY ----------------------------------------------------------- |
| 143 | |
[email protected] | c63b4d4 | 2012-04-26 01:01:07 | [diff] [blame] | 144 | // Called on the IO thread when a BackingStore message is received. |
| 145 | void DidReceiveBackingStoreMsg(const IPC::Message& msg); |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 146 | |
[email protected] | 97714c8 | 2012-06-06 10:15:13 | [diff] [blame] | 147 | void CreateNewWindow( |
| 148 | const ViewHostMsg_CreateWindow_Params& params, |
| 149 | bool no_javascript_access, |
| 150 | base::ProcessHandle render_process, |
| 151 | int* route_id, |
[email protected] | 227692c5 | 2013-05-31 22:43:04 | [diff] [blame] | 152 | int* main_frame_route_id, |
[email protected] | 97714c8 | 2012-06-06 10:15:13 | [diff] [blame] | 153 | int* surface_id, |
[email protected] | fc4616f | 2012-07-21 01:29:58 | [diff] [blame] | 154 | SessionStorageNamespace* session_storage_namespace); |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 155 | void CreateNewWidget(int opener_id, |
| 156 | WebKit::WebPopupType popup_type, |
[email protected] | 9f4f332 | 2012-01-18 22:29:56 | [diff] [blame] | 157 | int* route_id, |
| 158 | int* surface_id); |
| 159 | void CreateNewFullscreenWidget(int opener_id, int* route_id, int* surface_id); |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 160 | |
[email protected] | 1dfb8bef | 2013-05-17 07:31:44 | [diff] [blame] | 161 | #if defined(OS_POSIX) |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 162 | // Called on the IO thread to handle the allocation of a TransportDIB. If |
| 163 | // |cache_in_browser| is |true|, then a copy of the shmem is kept by the |
| 164 | // browser, and it is the caller's repsonsibility to call |
| 165 | // FreeTransportDIB(). In all cases, the caller is responsible for deleting |
| 166 | // the resulting TransportDIB. |
[email protected] | 324f4b1 | 2013-06-04 06:15:22 | [diff] [blame] | 167 | void AllocTransportDIB(uint32 size, |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 168 | bool cache_in_browser, |
| 169 | TransportDIB::Handle* result); |
| 170 | |
| 171 | // Called on the IO thread to handle the freeing of a transport DIB |
| 172 | void FreeTransportDIB(TransportDIB::Id dib_id); |
| 173 | #endif |
| 174 | |
| 175 | private: |
| 176 | // A class used to proxy a paint message. PaintMsgProxy objects are created |
| 177 | // on the IO thread and destroyed on the UI thread. |
[email protected] | c63b4d4 | 2012-04-26 01:01:07 | [diff] [blame] | 178 | class BackingStoreMsgProxy; |
| 179 | friend class BackingStoreMsgProxy; |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 180 | friend class base::RefCountedThreadSafe<RenderWidgetHelper>; |
[email protected] | fc4616f | 2012-07-21 01:29:58 | [diff] [blame] | 181 | friend struct BrowserThread::DeleteOnThread<BrowserThread::IO>; |
[email protected] | c63b4d4 | 2012-04-26 01:01:07 | [diff] [blame] | 182 | friend class base::DeleteHelper<RenderWidgetHelper>; |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 183 | |
[email protected] | c63b4d4 | 2012-04-26 01:01:07 | [diff] [blame] | 184 | typedef std::deque<BackingStoreMsgProxy*> BackingStoreMsgProxyQueue; |
[email protected] | aa4117f | 2011-12-09 22:19:21 | [diff] [blame] | 185 | // Map from render_widget_id to a queue of live PaintMsgProxy instances. |
[email protected] | c63b4d4 | 2012-04-26 01:01:07 | [diff] [blame] | 186 | typedef base::hash_map<int, BackingStoreMsgProxyQueue > |
| 187 | BackingStoreMsgProxyMap; |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 188 | |
| 189 | ~RenderWidgetHelper(); |
| 190 | |
| 191 | // Called on the UI thread to discard a paint message. |
[email protected] | c63b4d4 | 2012-04-26 01:01:07 | [diff] [blame] | 192 | void OnDiscardBackingStoreMsg(BackingStoreMsgProxy* proxy); |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 193 | |
| 194 | // Called on the UI thread to dispatch a paint message if necessary. |
[email protected] | c63b4d4 | 2012-04-26 01:01:07 | [diff] [blame] | 195 | void OnDispatchBackingStoreMsg(BackingStoreMsgProxy* proxy); |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 196 | |
| 197 | // Called on the UI thread to finish creating a window. |
[email protected] | 97714c8 | 2012-06-06 10:15:13 | [diff] [blame] | 198 | void OnCreateWindowOnUI( |
| 199 | const ViewHostMsg_CreateWindow_Params& params, |
| 200 | int route_id, |
[email protected] | 227692c5 | 2013-05-31 22:43:04 | [diff] [blame] | 201 | int main_frame_route_id, |
[email protected] | fc4616f | 2012-07-21 01:29:58 | [diff] [blame] | 202 | SessionStorageNamespace* session_storage_namespace); |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 203 | |
| 204 | // Called on the IO thread after a window was created on the UI thread. |
[email protected] | aacd1d7 | 2012-07-23 17:35:09 | [diff] [blame] | 205 | void OnResumeRequestsForView(int route_id); |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 206 | |
| 207 | // Called on the UI thread to finish creating a widget. |
| 208 | void OnCreateWidgetOnUI(int opener_id, |
| 209 | int route_id, |
| 210 | WebKit::WebPopupType popup_type); |
| 211 | |
| 212 | // Called on the UI thread to create a fullscreen widget. |
| 213 | void OnCreateFullscreenWidgetOnUI(int opener_id, int route_id); |
| 214 | |
[email protected] | e198683 | 2013-06-14 07:27:28 | [diff] [blame] | 215 | // Called on the IO thread to resume a paused navigation in the network |
| 216 | // stack without transferring it to a new renderer process. |
| 217 | void OnResumeDeferredNavigation(const GlobalRequestID& request_id); |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 218 | |
[email protected] | 1dfb8bef | 2013-05-17 07:31:44 | [diff] [blame] | 219 | #if defined(OS_POSIX) |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 220 | // Called on destruction to release all allocated transport DIBs |
| 221 | void ClearAllocatedDIBs(); |
| 222 | |
[email protected] | 1dfb8bef | 2013-05-17 07:31:44 | [diff] [blame] | 223 | // On POSIX we keep file descriptors to all the allocated DIBs around until |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 224 | // the renderer frees them. |
| 225 | base::Lock allocated_dibs_lock_; |
| 226 | std::map<TransportDIB::Id, int> allocated_dibs_; |
| 227 | #endif |
| 228 | |
| 229 | // A map of live paint messages. Must hold pending_paints_lock_ to access. |
[email protected] | c63b4d4 | 2012-04-26 01:01:07 | [diff] [blame] | 230 | // The BackingStoreMsgProxy objects are not owned by this map. (See |
| 231 | // BackingStoreMsgProxy for details about how the lifetime of instances are |
| 232 | // managed.) |
| 233 | BackingStoreMsgProxyMap pending_paints_; |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 234 | base::Lock pending_paints_lock_; |
| 235 | |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 236 | int render_process_id_; |
| 237 | |
[email protected] | c63b4d4 | 2012-04-26 01:01:07 | [diff] [blame] | 238 | // Event used to implement WaitForBackingStoreMsg. |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 239 | base::WaitableEvent event_; |
| 240 | |
| 241 | // The next routing id to use. |
| 242 | base::AtomicSequenceNumber next_routing_id_; |
| 243 | |
[email protected] | fc4616f | 2012-07-21 01:29:58 | [diff] [blame] | 244 | ResourceDispatcherHostImpl* resource_dispatcher_host_; |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 245 | |
| 246 | DISALLOW_COPY_AND_ASSIGN(RenderWidgetHelper); |
| 247 | }; |
| 248 | |
[email protected] | fc4616f | 2012-07-21 01:29:58 | [diff] [blame] | 249 | } // namespace content |
| 250 | |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 251 | #endif // CONTENT_BROWSER_RENDERER_HOST_RENDER_WIDGET_HELPER_H_ |