1// Copyright 2014 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_RESIZE_HELPER_H_ 6#define CONTENT_BROWSER_RENDERER_HOST_RENDER_WIDGET_RESIZE_HELPER_H_ 7 8#include "base/lazy_instance.h" 9#include "base/single_thread_task_runner.h" 10#include "base/synchronization/lock.h" 11#include "base/synchronization/waitable_event.h" 12#include "ipc/ipc_message.h" 13 14namespace content { 15 16// RenderWidgetResizeHelper is used to make resize appear smooth. That is to 17// say, make sure that the window size and the size of the content being drawn 18// in that window are resized in lock-step. This is accomplished by waiting 19// inside -[RenderWidgetHostViewCocoa setFrameSize:] for the renderer (and 20// potentially browser compositor as well) to produce a frame of same size 21// as the RenderWidgetHostViewCocoa. 22// 23// The function of waiting for a frame of the correct size is done inside 24// RenderWidgetHostImpl::WaitForSurface. That function will call 25// RenderWidgetResizeHelper::WaitForSingleTaskToRun until a timeout occurs, 26// or the corresponding RenderWidgetHostViewCocoa has a renderer frame of the 27// same size as its NSView. 28// 29// This is somewhat complicated because waiting for frames requires that 30// that the browser handle the IPCs (from the renderer and the GPU processes) 31// that are required to pick up a new frame. In the ordinary run of things 32// (ignoring RenderWidgetResizeHelper), those IPCs arrive on the IO thread 33// and are posted as tasks to the UI thread either by the RenderMessageFilter 34// (for renderer processes) or the GpuProcessHostUIShim (for the GPU process). 35// The IPCs that are required to create new frames for smooth resize are sent 36// to the RenderWidgetResizeHelper using the PostRendererProcessMsg and 37// PostGpuProcessMsg methods. These functions will post them as tasks to the UI 38// thread (as usual), and will also enqueue them into a queue which will be 39// read and run in RenderWidgetResizeHelper::WaitForSingleTaskToRun, potentially 40// before the task posted to the UI thread is run. Some care is taken (see 41// WrappedTask) to make sure that the messages are only executed once. 42// 43// This is further complicated because, in order for a frame to appear, it is 44// necessary to run tasks posted by the ui::Compositor. To accomplish this, the 45// RenderWidgetResizeHelper provides a base::SingleThreadTaskRunner which, 46// when a task is posted to it, enqueues the task in the aforementioned queue, 47// which may be pumped by RenderWidgetResizeHelper::WaitForSingleTaskToRun. 48// 49class RenderWidgetResizeHelper { 50 public: 51 static RenderWidgetResizeHelper* Get(); 52 scoped_refptr<base::SingleThreadTaskRunner> task_runner() const; 53 54 // UI THREAD ONLY ----------------------------------------------------------- 55 56 // Waits at most |max_delay| for a task to run. Returns true if a task ran, 57 // false if no task ran. 58 bool WaitForSingleTaskToRun(const base::TimeDelta& max_delay); 59 60 // IO THREAD ONLY ----------------------------------------------------------- 61 62 // This will cause |msg| to be handled by the RenderProcessHost corresponding 63 // to |render_process_id|, on the UI thread. This will either happen when the 64 // ordinary message loop would run it, or potentially earlier in a call to 65 // WaitForSingleTaskToRun . 66 void PostRendererProcessMsg(int render_process_id, const IPC::Message& msg); 67 68 // This is similar to PostRendererProcessMsg, but will handle the message in 69 // the GpuProcessHostUIShim corresponding to |gpu_host_id|. 70 void PostGpuProcessMsg(int gpu_host_id, const IPC::Message& msg); 71 72 private: 73 friend struct base::DefaultLazyInstanceTraits<RenderWidgetResizeHelper>; 74 RenderWidgetResizeHelper(); 75 ~RenderWidgetResizeHelper(); 76 77 // This helper is needed to create a ScopedAllowWait inside the scope of a 78 // class where it is allowed. 79 static void EventTimedWait( 80 base::WaitableEvent* event, 81 base::TimeDelta delay); 82 83 // The task runner to which the helper will post tasks. This also maintains 84 // the task queue and does the actual work for WaitForSingleTaskToRun. 85 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; 86 87 DISALLOW_COPY_AND_ASSIGN(RenderWidgetResizeHelper); 88}; 89 90} // namespace content 91 92#endif // CONTENT_BROWSER_RENDERER_HOST_RENDER_WIDGET_RESIZE_HELPER_H_ 93