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.
4 
5 #include "content/browser/renderer_host/render_widget_host_impl.h"
6 
7 #include <math.h>
8 
9 #include <algorithm>
10 #include <set>
11 #include <tuple>
12 #include <unordered_map>
13 #include <utility>
14 
15 #include "base/auto_reset.h"
16 #include "base/bind.h"
17 #include "base/bind_helpers.h"
18 #include "base/command_line.h"
19 #include "base/hash/hash.h"
20 #include "base/i18n/rtl.h"
21 #include "base/lazy_instance.h"
22 #include "base/location.h"
23 #include "base/macros.h"
24 #include "base/message_loop/message_loop_current.h"
25 #include "base/metrics/field_trial.h"
26 #include "base/metrics/histogram_macros.h"
27 #include "base/single_thread_task_runner.h"
28 #include "base/strings/string_number_conversions.h"
29 #include "base/strings/utf_string_conversions.h"
30 #include "base/task/post_task.h"
31 #include "base/threading/thread_task_runner_handle.h"
32 #include "base/time/default_tick_clock.h"
33 #include "base/trace_event/trace_event.h"
34 #include "build/build_config.h"
35 #include "cc/base/switches.h"
36 #include "cc/trees/browser_controls_params.h"
37 #include "cc/trees/render_frame_metadata.h"
38 #include "components/viz/common/features.h"
39 #include "components/viz/host/host_frame_sink_manager.h"
40 #include "content/browser/accessibility/browser_accessibility_state_impl.h"
41 #include "content/browser/bad_message.h"
42 #include "content/browser/browser_main_loop.h"
43 #include "content/browser/browser_plugin/browser_plugin_guest.h"
44 #include "content/browser/child_process_security_policy_impl.h"
45 #include "content/browser/compositor/surface_utils.h"
46 #include "content/browser/file_system/browser_file_system_helper.h"
47 #include "content/browser/gpu/compositor_util.h"
48 #include "content/browser/renderer_host/dip_util.h"
49 #include "content/browser/renderer_host/display_util.h"
50 #include "content/browser/renderer_host/frame_token_message_queue.h"
51 #include "content/browser/renderer_host/input/fling_scheduler.h"
52 #include "content/browser/renderer_host/input/input_router_config_helper.h"
53 #include "content/browser/renderer_host/input/input_router_impl.h"
54 #include "content/browser/renderer_host/input/synthetic_gesture.h"
55 #include "content/browser/renderer_host/input/synthetic_gesture_controller.h"
56 #include "content/browser/renderer_host/input/synthetic_gesture_target.h"
57 #include "content/browser/renderer_host/input/timeout_monitor.h"
58 #include "content/browser/renderer_host/input/touch_emulator.h"
59 #include "content/browser/renderer_host/render_view_host_delegate.h"
60 #include "content/browser/renderer_host/render_view_host_delegate_view.h"
61 #include "content/browser/renderer_host/render_view_host_impl.h"
62 #include "content/browser/renderer_host/render_widget_helper.h"
63 #include "content/browser/renderer_host/render_widget_host_input_event_router.h"
64 #include "content/browser/renderer_host/render_widget_host_owner_delegate.h"
65 #include "content/browser/renderer_host/render_widget_host_view_base.h"
66 #include "content/browser/renderer_host/render_widget_host_view_child_frame.h"
67 #include "content/common/content_constants_internal.h"
68 #include "content/common/cursors/webcursor.h"
69 #include "content/common/drag_messages.h"
70 #include "content/common/frame_messages.h"
71 #include "content/common/input/sync_compositor_messages.h"
72 #include "content/common/input_messages.h"
73 #include "content/common/text_input_state.h"
74 #include "content/common/view_messages.h"
75 #include "content/common/visual_properties.h"
76 #include "content/common/widget_messages.h"
77 #include "content/public/browser/browser_context.h"
78 #include "content/public/browser/browser_thread.h"
79 #include "content/public/browser/device_service.h"
80 #include "content/public/browser/keyboard_event_processing_result.h"
81 #include "content/public/browser/native_web_keyboard_event.h"
82 #include "content/public/browser/notification_service.h"
83 #include "content/public/browser/notification_types.h"
84 #include "content/public/browser/peak_gpu_memory_tracker.h"
85 #include "content/public/browser/render_frame_metadata_provider.h"
86 #include "content/public/browser/render_widget_host_iterator.h"
87 #include "content/public/browser/render_widget_host_observer.h"
88 #include "content/public/browser/storage_partition.h"
89 #include "content/public/common/content_constants.h"
90 #include "content/public/common/content_features.h"
91 #include "content/public/common/content_switches.h"
92 #include "content/public/common/result_codes.h"
93 #include "content/public/common/use_zoom_for_dsf_policy.h"
94 #include "content/public/common/web_preferences.h"
95 #include "gpu/GLES2/gl2extchromium.h"
96 #include "gpu/command_buffer/service/gpu_switches.h"
97 #include "gpu/ipc/common/gpu_messages.h"
98 #include "mojo/public/cpp/bindings/pending_receiver.h"
99 #include "mojo/public/cpp/system/platform_handle.h"
100 #include "net/base/filename_util.h"
101 #include "skia/ext/image_operations.h"
102 #include "skia/ext/platform_canvas.h"
103 #include "storage/browser/file_system/isolated_context.h"
104 #include "third_party/blink/public/web/web_ime_text_span.h"
105 #include "ui/base/clipboard/clipboard_constants.h"
106 #include "ui/base/ui_base_switches.h"
107 #include "ui/display/display_switches.h"
108 #include "ui/display/screen.h"
109 #include "ui/events/blink/web_input_event_traits.h"
110 #include "ui/events/event.h"
111 #include "ui/events/keycodes/dom/dom_code.h"
112 #include "ui/gfx/color_space.h"
113 #include "ui/gfx/geometry/size_conversions.h"
114 #include "ui/gfx/geometry/vector2d_conversions.h"
115 #include "ui/gfx/geometry/vector2d_f.h"
116 #include "ui/gfx/image/image.h"
117 #include "ui/gfx/image/image_skia.h"
118 #include "ui/gfx/skbitmap_operations.h"
119 #include "ui/snapshot/snapshot.h"
120 
121 #if defined(OS_ANDROID)
122 #include "content/browser/renderer_host/input/fling_scheduler_android.h"
123 #include "ui/android/view_android.h"
124 #endif
125 
126 #if defined(OS_MACOSX)
127 #include "content/browser/renderer_host/input/fling_scheduler_mac.h"
128 #include "services/device/public/mojom/wake_lock_provider.mojom.h"
129 #include "ui/accelerated_widget_mac/window_resize_helper_mac.h"
130 #endif
131 
132 using base::TimeDelta;
133 using base::TimeTicks;
134 using blink::WebDragOperation;
135 using blink::WebDragOperationsMask;
136 using blink::WebGestureEvent;
137 using blink::WebInputEvent;
138 using blink::WebKeyboardEvent;
139 using blink::WebMouseEvent;
140 using blink::WebMouseWheelEvent;
141 
142 namespace content {
143 namespace {
144 
145 bool g_check_for_pending_visual_properties_ack = true;
146 
ShouldDisableHangMonitor()147 bool ShouldDisableHangMonitor() {
148   return base::CommandLine::ForCurrentProcess()->HasSwitch(
149       switches::kDisableHangMonitor);
150 }
151 
152 // <process id, routing id>
153 using RenderWidgetHostID = std::pair<int32_t, int32_t>;
154 using RoutingIDWidgetMap =
155     std::unordered_map<RenderWidgetHostID,
156                        RenderWidgetHostImpl*,
157                        base::IntPairHash<RenderWidgetHostID>>;
158 base::LazyInstance<RoutingIDWidgetMap>::DestructorAtExit
159     g_routing_id_widget_map = LAZY_INSTANCE_INITIALIZER;
160 
161 // Implements the RenderWidgetHostIterator interface. It keeps a list of
162 // RenderWidgetHosts, and makes sure it returns a live RenderWidgetHost at each
163 // iteration (or NULL if there isn't any left).
164 class RenderWidgetHostIteratorImpl : public RenderWidgetHostIterator {
165  public:
RenderWidgetHostIteratorImpl()166   RenderWidgetHostIteratorImpl()
167       : current_index_(0) {
168   }
169 
~RenderWidgetHostIteratorImpl()170   ~RenderWidgetHostIteratorImpl() override {}
171 
Add(RenderWidgetHost * host)172   void Add(RenderWidgetHost* host) {
173     hosts_.push_back(RenderWidgetHostID(host->GetProcess()->GetID(),
174                                         host->GetRoutingID()));
175   }
176 
177   // RenderWidgetHostIterator:
GetNextHost()178   RenderWidgetHost* GetNextHost() override {
179     RenderWidgetHost* host = nullptr;
180     while (current_index_ < hosts_.size() && !host) {
181       RenderWidgetHostID id = hosts_[current_index_];
182       host = RenderWidgetHost::FromID(id.first, id.second);
183       ++current_index_;
184     }
185     return host;
186   }
187 
188  private:
189   std::vector<RenderWidgetHostID> hosts_;
190   size_t current_index_;
191 
192   DISALLOW_COPY_AND_ASSIGN(RenderWidgetHostIteratorImpl);
193 };
194 
DropDataToMetaData(const DropData & drop_data)195 std::vector<DropData::Metadata> DropDataToMetaData(const DropData& drop_data) {
196   std::vector<DropData::Metadata> metadata;
197   if (!drop_data.text.is_null()) {
198     metadata.push_back(DropData::Metadata::CreateForMimeType(
199         DropData::Kind::STRING, base::ASCIIToUTF16(ui::kMimeTypeText)));
200   }
201 
202   if (drop_data.url.is_valid()) {
203     metadata.push_back(DropData::Metadata::CreateForMimeType(
204         DropData::Kind::STRING, base::ASCIIToUTF16(ui::kMimeTypeURIList)));
205   }
206 
207   if (!drop_data.html.is_null()) {
208     metadata.push_back(DropData::Metadata::CreateForMimeType(
209         DropData::Kind::STRING, base::ASCIIToUTF16(ui::kMimeTypeHTML)));
210   }
211 
212   // On Aura, filenames are available before drop.
213   for (const auto& file_info : drop_data.filenames) {
214     if (!file_info.path.empty()) {
215       metadata.push_back(DropData::Metadata::CreateForFilePath(file_info.path));
216     }
217   }
218 
219   // On Android, only files' mime types are available before drop.
220   for (const auto& mime_type : drop_data.file_mime_types) {
221     if (!mime_type.empty()) {
222       metadata.push_back(DropData::Metadata::CreateForMimeType(
223           DropData::Kind::FILENAME, mime_type));
224     }
225   }
226 
227   for (const auto& file_system_file : drop_data.file_system_files) {
228     if (!file_system_file.url.is_empty()) {
229       metadata.push_back(
230           DropData::Metadata::CreateForFileSystemUrl(file_system_file.url));
231     }
232   }
233 
234   for (const auto& custom_data_item : drop_data.custom_data) {
235     metadata.push_back(DropData::Metadata::CreateForMimeType(
236         DropData::Kind::STRING, custom_data_item.first));
237   }
238 
239   return metadata;
240 }
241 
242 class UnboundWidgetInputHandler : public mojom::WidgetInputHandler {
243  public:
SetFocus(bool focused)244   void SetFocus(bool focused) override {
245     DLOG(WARNING) << "Input request on unbound interface";
246   }
MouseCaptureLost()247   void MouseCaptureLost() override {
248     DLOG(WARNING) << "Input request on unbound interface";
249   }
MouseLockLost()250   void MouseLockLost() override {
251     DLOG(WARNING) << "Input request on unbound interface";
252   }
SetEditCommandsForNextKeyEvent(const std::vector<content::EditCommand> & commands)253   void SetEditCommandsForNextKeyEvent(
254       const std::vector<content::EditCommand>& commands) override {
255     DLOG(WARNING) << "Input request on unbound interface";
256   }
CursorVisibilityChanged(bool visible)257   void CursorVisibilityChanged(bool visible) override {
258     DLOG(WARNING) << "Input request on unbound interface";
259   }
FallbackCursorModeToggled(bool is_on)260   void FallbackCursorModeToggled(bool is_on) override {
261     DLOG(WARNING) << "Input request on unbound interface";
262   }
ImeSetComposition(const base::string16 & text,const std::vector<ui::ImeTextSpan> & ime_text_spans,const gfx::Range & range,int32_t start,int32_t end)263   void ImeSetComposition(const base::string16& text,
264                          const std::vector<ui::ImeTextSpan>& ime_text_spans,
265                          const gfx::Range& range,
266                          int32_t start,
267                          int32_t end) override {
268     DLOG(WARNING) << "Input request on unbound interface";
269   }
ImeCommitText(const base::string16 & text,const std::vector<ui::ImeTextSpan> & ime_text_spans,const gfx::Range & range,int32_t relative_cursor_position,ImeCommitTextCallback callback)270   void ImeCommitText(const base::string16& text,
271                      const std::vector<ui::ImeTextSpan>& ime_text_spans,
272                      const gfx::Range& range,
273                      int32_t relative_cursor_position,
274                      ImeCommitTextCallback callback) override {
275     DLOG(WARNING) << "Input request on unbound interface";
276   }
ImeFinishComposingText(bool keep_selection)277   void ImeFinishComposingText(bool keep_selection) override {
278     DLOG(WARNING) << "Input request on unbound interface";
279   }
RequestTextInputStateUpdate()280   void RequestTextInputStateUpdate() override {
281     DLOG(WARNING) << "Input request on unbound interface";
282   }
RequestCompositionUpdates(bool immediate_request,bool monitor_request)283   void RequestCompositionUpdates(bool immediate_request,
284                                  bool monitor_request) override {
285     DLOG(WARNING) << "Input request on unbound interface";
286   }
DispatchEvent(std::unique_ptr<content::InputEvent> event,DispatchEventCallback callback)287   void DispatchEvent(std::unique_ptr<content::InputEvent> event,
288                      DispatchEventCallback callback) override {
289     DLOG(WARNING) << "Input request on unbound interface";
290   }
DispatchNonBlockingEvent(std::unique_ptr<content::InputEvent> event)291   void DispatchNonBlockingEvent(
292       std::unique_ptr<content::InputEvent> event) override {
293     DLOG(WARNING) << "Input request on unbound interface";
294   }
WaitForInputProcessed(WaitForInputProcessedCallback callback)295   void WaitForInputProcessed(WaitForInputProcessedCallback callback) override {
296     DLOG(WARNING) << "Input request on unbound interface";
297   }
AttachSynchronousCompositor(mojo::PendingRemote<mojom::SynchronousCompositorControlHost> control_host,mojo::PendingAssociatedRemote<mojom::SynchronousCompositorHost> host,mojo::PendingAssociatedReceiver<mojom::SynchronousCompositor> compositor_request)298   void AttachSynchronousCompositor(
299       mojo::PendingRemote<mojom::SynchronousCompositorControlHost> control_host,
300       mojo::PendingAssociatedRemote<mojom::SynchronousCompositorHost> host,
301       mojo::PendingAssociatedReceiver<mojom::SynchronousCompositor>
302           compositor_request) override {
303     NOTREACHED() << "Input request on unbound interface";
304   }
305 };
306 
307 base::LazyInstance<UnboundWidgetInputHandler>::Leaky g_unbound_input_handler =
308     LAZY_INSTANCE_INITIALIZER;
309 
310 }  // namespace
311 
312 ///////////////////////////////////////////////////////////////////////////////
313 // RenderWidgetHostImpl
314 
RenderWidgetHostImpl(RenderWidgetHostDelegate * delegate,RenderProcessHost * process,int32_t routing_id,mojo::PendingRemote<mojom::Widget> widget,bool hidden,std::unique_ptr<FrameTokenMessageQueue> frame_token_message_queue)315 RenderWidgetHostImpl::RenderWidgetHostImpl(
316     RenderWidgetHostDelegate* delegate,
317     RenderProcessHost* process,
318     int32_t routing_id,
319     mojo::PendingRemote<mojom::Widget> widget,
320     bool hidden,
321     std::unique_ptr<FrameTokenMessageQueue> frame_token_message_queue)
322     : delegate_(delegate),
323       process_(process),
324       routing_id_(routing_id),
325       clock_(base::DefaultTickClock::GetInstance()),
326       is_hidden_(hidden),
327       latency_tracker_(delegate_),
328       hung_renderer_delay_(TimeDelta::FromMilliseconds(kHungRendererDelayMs)),
329       new_content_rendering_delay_(
330           TimeDelta::FromMilliseconds(kNewContentRenderingDelayMs)),
331       frame_token_message_queue_(std::move(frame_token_message_queue)),
332       render_frame_metadata_provider_(
333 #if defined(OS_MACOSX)
334           ui::WindowResizeHelperMac::Get()->task_runner(),
335 #else
336           base::ThreadTaskRunnerHandle::Get(),
337 #endif
338           frame_token_message_queue_.get()),
339       frame_sink_id_(base::checked_cast<uint32_t>(process_->GetID()),
340                      base::checked_cast<uint32_t>(routing_id_)) {
341   DCHECK(frame_token_message_queue_);
342   frame_token_message_queue_->Init(this);
343 
344 #if defined(OS_MACOSX)
345   fling_scheduler_ = std::make_unique<FlingSchedulerMac>(this);
346 #elif defined(OS_ANDROID)
347   fling_scheduler_ = std::make_unique<FlingSchedulerAndroid>(this);
348 #else
349   fling_scheduler_ = std::make_unique<FlingScheduler>(this);
350 #endif
351   CHECK(delegate_);
352   CHECK_NE(MSG_ROUTING_NONE, routing_id_);
353   DCHECK(base::ThreadPoolInstance::Get())
354       << "Ref. Prerequisite section of post_task.h";
355 
356   std::pair<RoutingIDWidgetMap::iterator, bool> result =
357       g_routing_id_widget_map.Get().insert(std::make_pair(
358           RenderWidgetHostID(process->GetID(), routing_id_), this));
359   CHECK(result.second) << "Inserting a duplicate item!";
360   process_->AddRoute(routing_id_, this);
361   process_->AddObserver(this);
362   render_process_blocked_state_changed_subscription_ =
363       process_->RegisterBlockStateChangedCallback(base::BindRepeating(
364           &RenderWidgetHostImpl::RenderProcessBlockedStateChanged,
365           base::Unretained(this)));
366   process_->AddPriorityClient(this);
367 
368   SetupInputRouter();
369   SetWidget(std::move(widget));
370 
371   const auto* command_line = base::CommandLine::ForCurrentProcess();
372   if (!command_line->HasSwitch(switches::kDisableNewContentRenderingTimeout)) {
373     new_content_rendering_timeout_ = std::make_unique<TimeoutMonitor>(
374         base::BindRepeating(&RenderWidgetHostImpl::ClearDisplayedGraphics,
375                             weak_factory_.GetWeakPtr()));
376   }
377 
378   delegate_->RenderWidgetCreated(this);
379   render_frame_metadata_provider_.AddObserver(this);
380 }
381 
~RenderWidgetHostImpl()382 RenderWidgetHostImpl::~RenderWidgetHostImpl() {
383   render_frame_metadata_provider_.RemoveObserver(this);
384   if (!destroyed_)
385     Destroy(false);
386 }
387 
388 // static
FromID(int32_t process_id,int32_t routing_id)389 RenderWidgetHost* RenderWidgetHost::FromID(
390     int32_t process_id,
391     int32_t routing_id) {
392   return RenderWidgetHostImpl::FromID(process_id, routing_id);
393 }
394 
395 // static
FromID(int32_t process_id,int32_t routing_id)396 RenderWidgetHostImpl* RenderWidgetHostImpl::FromID(
397     int32_t process_id,
398     int32_t routing_id) {
399   DCHECK_CURRENTLY_ON(BrowserThread::UI);
400   RoutingIDWidgetMap* widgets = g_routing_id_widget_map.Pointer();
401   auto it = widgets->find(RenderWidgetHostID(process_id, routing_id));
402   return it != widgets->end() ? it->second : nullptr;
403 }
404 
405 // static
406 std::unique_ptr<RenderWidgetHostIterator>
GetRenderWidgetHosts()407 RenderWidgetHost::GetRenderWidgetHosts() {
408   auto hosts = std::make_unique<RenderWidgetHostIteratorImpl>();
409   for (auto& it : g_routing_id_widget_map.Get()) {
410     RenderWidgetHostImpl* widget = it.second;
411     RenderWidgetHostOwnerDelegate* owner_delegate = widget->owner_delegate();
412     // If the widget is not for a main frame, add to |hosts|.
413     if (!owner_delegate) {
414       hosts->Add(widget);
415       continue;
416     }
417 
418     // If the widget is for a main frame, only add if there is a RenderWidget in
419     // the renderer process. When this is false, there is no main RenderFrame
420     // and so no RenderWidget for this RenderWidgetHost.
421     if (owner_delegate->IsMainFrameActive())
422       hosts->Add(widget);
423   }
424 
425   return std::move(hosts);
426 }
427 
428 // static
429 std::unique_ptr<RenderWidgetHostIterator>
GetAllRenderWidgetHosts()430 RenderWidgetHostImpl::GetAllRenderWidgetHosts() {
431   auto hosts = std::make_unique<RenderWidgetHostIteratorImpl>();
432   for (auto& it : g_routing_id_widget_map.Get())
433     hosts->Add(it.second);
434 
435   return std::move(hosts);
436 }
437 
438 // static
From(RenderWidgetHost * rwh)439 RenderWidgetHostImpl* RenderWidgetHostImpl::From(RenderWidgetHost* rwh) {
440   return static_cast<RenderWidgetHostImpl*>(rwh);
441 }
442 
SetView(RenderWidgetHostViewBase * view)443 void RenderWidgetHostImpl::SetView(RenderWidgetHostViewBase* view) {
444   if (view) {
445     view_ = view->GetWeakPtr();
446     if (!create_frame_sink_callback_.is_null())
447       std::move(create_frame_sink_callback_).Run(view_->GetFrameSinkId());
448   } else {
449     view_.reset();
450   }
451 
452   synthetic_gesture_controller_.reset();
453 }
454 
455 // static
456 const base::TimeDelta RenderWidgetHostImpl::kActivationNotificationExpireTime =
457     base::TimeDelta::FromMilliseconds(300);
458 
GetProcess()459 RenderProcessHost* RenderWidgetHostImpl::GetProcess() {
460   return process_;
461 }
462 
GetRoutingID()463 int RenderWidgetHostImpl::GetRoutingID() {
464   return routing_id_;
465 }
466 
GetView()467 RenderWidgetHostViewBase* RenderWidgetHostImpl::GetView() {
468   return view_.get();
469 }
470 
GetFrameSinkId()471 const viz::FrameSinkId& RenderWidgetHostImpl::GetFrameSinkId() {
472   return frame_sink_id_;
473 }
474 
SendScreenRects()475 void RenderWidgetHostImpl::SendScreenRects() {
476   if (!renderer_initialized_ || waiting_for_screen_rects_ack_)
477     return;
478 
479   if (is_hidden_) {
480     // On GTK, this comes in for backgrounded tabs. Ignore, to match what
481     // happens on Win & Mac, and when the view is shown it'll call this again.
482     return;
483   }
484 
485   if (!view_)
486     return;
487 
488   last_view_screen_rect_ = view_->GetViewBounds();
489   last_window_screen_rect_ = view_->GetBoundsInRootWindow();
490   view_->WillSendScreenRects();
491   Send(new WidgetMsg_UpdateScreenRects(GetRoutingID(), last_view_screen_rect_,
492                                        last_window_screen_rect_));
493   waiting_for_screen_rects_ack_ = true;
494 }
495 
SetFrameDepth(unsigned int depth)496 void RenderWidgetHostImpl::SetFrameDepth(unsigned int depth) {
497   if (frame_depth_ == depth)
498     return;
499 
500   frame_depth_ = depth;
501   UpdatePriority();
502 }
503 
SetIntersectsViewport(bool intersects)504 void RenderWidgetHostImpl::SetIntersectsViewport(bool intersects) {
505   if (intersects_viewport_ == intersects)
506     return;
507 
508   intersects_viewport_ = intersects;
509   UpdatePriority();
510 }
511 
UpdatePriority()512 void RenderWidgetHostImpl::UpdatePriority() {
513   if (!destroyed_)
514     process_->UpdateClientPriority(this);
515 }
516 
Init()517 void RenderWidgetHostImpl::Init() {
518   DCHECK(process_->IsInitializedAndNotDead());
519 
520   renderer_initialized_ = true;
521 
522   SendScreenRects();
523   SynchronizeVisualProperties();
524 
525   if (owner_delegate_)
526     owner_delegate_->RenderWidgetDidInit();
527 
528   if (view_)
529     view_->OnRenderWidgetInit();
530 }
531 
532 std::pair<mojo::PendingAssociatedRemote<blink::mojom::WidgetHost>,
533           mojo::PendingAssociatedReceiver<blink::mojom::Widget>>
BindNewWidgetInterfaces()534 RenderWidgetHostImpl::BindNewWidgetInterfaces() {
535   // This API may get called on a RenderWidgetHostImpl from a
536   // reused RenderViewHostImpl so we need to ensure old channels are dropped.
537   blink_widget_host_receiver_.reset();
538   blink_widget_.reset();
539   return std::make_pair(
540       blink_widget_host_receiver_.BindNewEndpointAndPassRemote(),
541       blink_widget_.BindNewEndpointAndPassReceiver());
542 }
543 
BindWidgetInterfaces(mojo::PendingAssociatedReceiver<blink::mojom::WidgetHost> widget_host,mojo::PendingAssociatedRemote<blink::mojom::Widget> widget)544 void RenderWidgetHostImpl::BindWidgetInterfaces(
545     mojo::PendingAssociatedReceiver<blink::mojom::WidgetHost> widget_host,
546     mojo::PendingAssociatedRemote<blink::mojom::Widget> widget) {
547   // This API may get called on a RenderWidgetHostImpl from a
548   // reused RenderViewHostImpl so we need to ensure old channels are dropped.
549   blink_widget_host_receiver_.reset();
550   blink_widget_.reset();
551   blink_widget_host_receiver_.Bind(std::move(widget_host));
552   blink_widget_.Bind(std::move(widget));
553 }
554 
555 std::pair<mojo::PendingAssociatedRemote<blink::mojom::FrameWidgetHost>,
556           mojo::PendingAssociatedReceiver<blink::mojom::FrameWidget>>
BindNewFrameWidgetInterfaces()557 RenderWidgetHostImpl::BindNewFrameWidgetInterfaces() {
558   // This API may get called on a RenderWidgetHostImpl from a
559   // reused RenderViewHostImpl so we need to ensure old channels are dropped.
560   blink_frame_widget_host_receiver_.reset();
561   blink_frame_widget_.reset();
562   return std::make_pair(
563       blink_frame_widget_host_receiver_.BindNewEndpointAndPassRemote(),
564       blink_frame_widget_.BindNewEndpointAndPassReceiver());
565 }
566 
BindFrameWidgetInterfaces(mojo::PendingAssociatedReceiver<blink::mojom::FrameWidgetHost> frame_widget_host,mojo::PendingAssociatedRemote<blink::mojom::FrameWidget> frame_widget)567 void RenderWidgetHostImpl::BindFrameWidgetInterfaces(
568     mojo::PendingAssociatedReceiver<blink::mojom::FrameWidgetHost>
569         frame_widget_host,
570     mojo::PendingAssociatedRemote<blink::mojom::FrameWidget> frame_widget) {
571   // This API may get called on a RenderWidgetHostImpl from a
572   // reused RenderViewHostImpl so we need to ensure old channels are dropped.
573   blink_frame_widget_host_receiver_.reset();
574   blink_frame_widget_.reset();
575   blink_frame_widget_host_receiver_.Bind(std::move(frame_widget_host));
576   blink_frame_widget_.Bind(std::move(frame_widget));
577 }
578 
InitForFrame()579 void RenderWidgetHostImpl::InitForFrame() {
580   DCHECK(process_->IsInitializedAndNotDead());
581   renderer_initialized_ = true;
582 
583   if (view_)
584     view_->OnRenderWidgetInit();
585 }
586 
ShouldShowStaleContentOnEviction()587 bool RenderWidgetHostImpl::ShouldShowStaleContentOnEviction() {
588   return delegate_ && delegate_->ShouldShowStaleContentOnEviction();
589 }
590 
ShutdownAndDestroyWidget(bool also_delete)591 void RenderWidgetHostImpl::ShutdownAndDestroyWidget(bool also_delete) {
592   CancelKeyboardLock();
593   RejectMouseLockOrUnlockIfNecessary(
594       blink::mojom::PointerLockResult::kElementDestroyed);
595 
596   if (process_->IsInitializedAndNotDead() && !owner_delegate()) {
597     // Tell the RendererWidget to close. We only want to do this if the
598     // RenderWidget is the root of the renderer object graph, which is for
599     // pepper fullscreen and popups.
600     bool rv = Send(new WidgetMsg_Close(routing_id_));
601     DCHECK(rv);
602   }
603 
604   Destroy(also_delete);
605 }
606 
OnMessageReceived(const IPC::Message & msg)607 bool RenderWidgetHostImpl::OnMessageReceived(const IPC::Message &msg) {
608   // Only process most messages if the RenderWidget is alive.
609   if (!renderer_initialized())
610     return false;
611 
612   bool handled = true;
613   IPC_BEGIN_MESSAGE_MAP(RenderWidgetHostImpl, msg)
614     IPC_MESSAGE_HANDLER(WidgetHostMsg_Close, OnClose)
615     IPC_MESSAGE_HANDLER(WidgetHostMsg_UpdateScreenRects_ACK,
616                         OnUpdateScreenRectsAck)
617     IPC_MESSAGE_HANDLER(WidgetHostMsg_RequestSetBounds, OnRequestSetBounds)
618     IPC_MESSAGE_HANDLER(WidgetHostMsg_SetTooltipText, OnSetTooltipText)
619     IPC_MESSAGE_HANDLER(WidgetHostMsg_SetCursor, OnSetCursor)
620     IPC_MESSAGE_HANDLER(WidgetHostMsg_AutoscrollStart, OnAutoscrollStart)
621     IPC_MESSAGE_HANDLER(WidgetHostMsg_AutoscrollFling, OnAutoscrollFling)
622     IPC_MESSAGE_HANDLER(WidgetHostMsg_AutoscrollEnd, OnAutoscrollEnd)
623     IPC_MESSAGE_HANDLER(WidgetHostMsg_TextInputStateChanged,
624                         OnTextInputStateChanged)
625     IPC_MESSAGE_HANDLER(WidgetHostMsg_SelectionBoundsChanged,
626                         OnSelectionBoundsChanged)
627     IPC_MESSAGE_HANDLER(DragHostMsg_StartDragging, OnStartDragging)
628     IPC_MESSAGE_HANDLER(DragHostMsg_UpdateDragCursor, OnUpdateDragCursor)
629     IPC_MESSAGE_HANDLER(WidgetHostMsg_FrameSwapMessages,
630                         OnFrameSwapMessagesReceived)
631     IPC_MESSAGE_HANDLER(WidgetHostMsg_ForceRedrawComplete,
632                         OnForceRedrawComplete)
633     IPC_MESSAGE_HANDLER(WidgetHostMsg_DidFirstVisuallyNonEmptyPaint,
634                         OnFirstVisuallyNonEmptyPaint)
635     IPC_MESSAGE_HANDLER(WidgetHostMsg_HasTouchEventHandlers,
636                         OnHasTouchEventHandlers)
637     IPC_MESSAGE_HANDLER(WidgetHostMsg_IntrinsicSizingInfoChanged,
638                         OnIntrinsicSizingInfoChanged)
639     IPC_MESSAGE_HANDLER(WidgetHostMsg_AnimateDoubleTapZoomInMainFrame,
640                         OnAnimateDoubleTapZoomInMainFrame)
641     IPC_MESSAGE_HANDLER(WidgetHostMsg_ZoomToFindInPageRectInMainFrame,
642                         OnZoomToFindInPageRectInMainFrame)
643     IPC_MESSAGE_UNHANDLED(handled = false)
644   IPC_END_MESSAGE_MAP()
645 
646   return handled;
647 }
648 
Send(IPC::Message * msg)649 bool RenderWidgetHostImpl::Send(IPC::Message* msg) {
650   return process_->Send(msg);
651 }
652 
SetIsLoading(bool is_loading)653 void RenderWidgetHostImpl::SetIsLoading(bool is_loading) {
654   is_loading_ = is_loading;
655   if (view_)
656     view_->SetIsLoading(is_loading);
657 }
658 
WasHidden()659 void RenderWidgetHostImpl::WasHidden() {
660   if (is_hidden_)
661     return;
662 
663   RejectMouseLockOrUnlockIfNecessary(
664       blink::mojom::PointerLockResult::kWrongDocument);
665 
666   TRACE_EVENT0("renderer_host", "RenderWidgetHostImpl::WasHidden");
667   is_hidden_ = true;
668 
669   // Unthrottle SynchronizeVisualProperties IPCs so that the first call after
670   // show goes through immediately.
671   visual_properties_ack_pending_ = false;
672 
673   // Don't bother reporting hung state when we aren't active.
674   StopInputEventAckTimeout();
675 
676   // If we have a renderer, then inform it that we are being hidden so it can
677   // reduce its resource utilization.
678   Send(new WidgetMsg_WasHidden(routing_id_));
679 
680   // Tell the RenderProcessHost we were hidden.
681   process_->UpdateClientPriority(this);
682 
683   bool is_visible = false;
684   NotificationService::current()->Notify(
685       NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED,
686       Source<RenderWidgetHost>(this), Details<bool>(&is_visible));
687   for (auto& observer : observers_)
688     observer.RenderWidgetHostVisibilityChanged(this, false);
689 }
690 
WasShown(const base::Optional<RecordContentToVisibleTimeRequest> & record_tab_switch_time_request)691 void RenderWidgetHostImpl::WasShown(
692     const base::Optional<RecordContentToVisibleTimeRequest>&
693         record_tab_switch_time_request) {
694   if (!is_hidden_)
695     return;
696 
697   TRACE_EVENT_WITH_FLOW0("renderer_host", "RenderWidgetHostImpl::WasShown",
698                          routing_id_, TRACE_EVENT_FLAG_FLOW_OUT);
699   is_hidden_ = false;
700 
701   // If we navigated in background, clear the displayed graphics of the
702   // previous page before going visible.
703   ForceFirstFrameAfterNavigationTimeout();
704 
705   SendScreenRects();
706   RestartInputEventAckTimeoutIfNecessary();
707 
708   Send(new WidgetMsg_WasShown(
709       routing_id_,
710       record_tab_switch_time_request ? base::TimeTicks::Now()
711                                      : base::TimeTicks(),
712       view_->is_evicted(), record_tab_switch_time_request));
713   view_->reset_is_evicted();
714 
715   process_->UpdateClientPriority(this);
716 
717   bool is_visible = true;
718   NotificationService::current()->Notify(
719       NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED,
720       Source<RenderWidgetHost>(this), Details<bool>(&is_visible));
721   for (auto& observer : observers_)
722     observer.RenderWidgetHostVisibilityChanged(this, true);
723 
724   // It's possible for our size to be out of sync with the renderer. The
725   // following is one case that leads to this:
726   // 1. SynchronizeVisualProperties -> Send
727   // WidgetMsg_SynchronizeVisualProperties
728   //    to render.
729   // 2. SynchronizeVisualProperties -> do nothing as
730   //    sync_visual_props_ack_pending_ is true
731   // 3. WasHidden
732   // By invoking SynchronizeVisualProperties the renderer is updated as
733   // necessary. SynchronizeVisualProperties does nothing if the sizes are
734   // already in sync.
735   //
736   // TODO: ideally WidgetMsg_WasShown would take a size. This way, the renderer
737   // could handle both the restore and resize at once. This isn't that big a
738   // deal as RenderWidget::WasShown delays updating, so that the resize from
739   // SynchronizeVisualProperties is usually processed before the renderer is
740   // painted.
741   SynchronizeVisualProperties();
742 }
743 
744 #if defined(OS_ANDROID)
SetImportance(ChildProcessImportance importance)745 void RenderWidgetHostImpl::SetImportance(ChildProcessImportance importance) {
746   if (importance_ == importance)
747     return;
748   importance_ = importance;
749   process_->UpdateClientPriority(this);
750 }
751 
AddImeInputEventObserver(RenderWidgetHost::InputEventObserver * observer)752 void RenderWidgetHostImpl::AddImeInputEventObserver(
753     RenderWidgetHost::InputEventObserver* observer) {
754   if (!ime_input_event_observers_.HasObserver(observer)) {
755     ime_input_event_observers_.AddObserver(observer);
756   }
757 }
758 
RemoveImeInputEventObserver(RenderWidgetHost::InputEventObserver * observer)759 void RenderWidgetHostImpl::RemoveImeInputEventObserver(
760     RenderWidgetHost::InputEventObserver* observer) {
761   ime_input_event_observers_.RemoveObserver(observer);
762 }
763 #endif
764 
GetInitialVisualProperties()765 VisualProperties RenderWidgetHostImpl::GetInitialVisualProperties() {
766   VisualProperties initial_props = GetVisualProperties();
767 
768   // A RenderWidget being created in the renderer means the browser should
769   // reset any state that may be set for the previous RenderWidget but which
770   // will be incorrect with a fresh RenderWidget.
771   ResetStateForCreatedRenderWidget(initial_props);
772 
773   return initial_props;
774 }
775 
GetVisualProperties()776 VisualProperties RenderWidgetHostImpl::GetVisualProperties() {
777   // This is only called while the RenderWidgetHost is attached to a delegate
778   // still.
779   DCHECK(delegate_);
780   // When the renderer process is gone, there's no need for VisualProperties
781   // which are to be sent to the renderer process.
782   DCHECK(view_);
783 
784   VisualProperties visual_properties;
785 
786   GetScreenInfo(&visual_properties.screen_info);
787 
788   visual_properties.is_fullscreen_granted =
789       delegate_->IsFullscreenForCurrentTab();
790   visual_properties.display_mode = delegate_->GetDisplayMode(this);
791   visual_properties.zoom_level = delegate_->GetPendingPageZoomLevel();
792 
793   RenderViewHostDelegateView* rvh_delegate_view = delegate_->GetDelegateView();
794   DCHECK(rvh_delegate_view);
795 
796   visual_properties.browser_controls_params.browser_controls_shrink_blink_size =
797       rvh_delegate_view->DoBrowserControlsShrinkRendererSize();
798   visual_properties.browser_controls_params
799       .animate_browser_controls_height_changes =
800       rvh_delegate_view->ShouldAnimateBrowserControlsHeightChanges();
801 
802   float top_controls_height = rvh_delegate_view->GetTopControlsHeight();
803   float top_controls_min_height = rvh_delegate_view->GetTopControlsMinHeight();
804   float bottom_controls_height = rvh_delegate_view->GetBottomControlsHeight();
805   float bottom_controls_min_height =
806       rvh_delegate_view->GetBottomControlsMinHeight();
807   float browser_controls_dsf_multiplier = 1.f;
808   // The top and bottom control sizes are physical pixels but the IPC wants
809   // DIPs *when not using page zoom for DSF* because blink layout is working
810   // in DIPs then.
811   if (!IsUseZoomForDSFEnabled()) {
812     browser_controls_dsf_multiplier =
813         visual_properties.screen_info.device_scale_factor;
814   }
815   visual_properties.browser_controls_params.top_controls_height =
816       top_controls_height / browser_controls_dsf_multiplier;
817   visual_properties.browser_controls_params.top_controls_min_height =
818       top_controls_min_height / browser_controls_dsf_multiplier;
819   visual_properties.browser_controls_params.bottom_controls_height =
820       bottom_controls_height / browser_controls_dsf_multiplier;
821   visual_properties.browser_controls_params.bottom_controls_min_height =
822       bottom_controls_min_height / browser_controls_dsf_multiplier;
823 
824   visual_properties.auto_resize_enabled = auto_resize_enabled_;
825   visual_properties.min_size_for_auto_resize = min_size_for_auto_resize_;
826   visual_properties.max_size_for_auto_resize = max_size_for_auto_resize_;
827 
828   visual_properties.new_size = view_->GetRequestedRendererSize();
829 
830   // This widget is for a frame that is the main frame of the outermost frame
831   // tree. That makes it the top-most frame. OR this is a non-frame widget.
832   const bool is_top_most_widget = !view_->IsRenderWidgetHostViewChildFrame();
833   // This widget is for a frame, but not the main frame of its frame tree.
834   const bool is_child_frame_widget =
835       view_->IsRenderWidgetHostViewChildFrame() && !owner_delegate_;
836 
837   // These properties come from the main frame RenderWidget and flow down the
838   // tree of RenderWidgets. Some properties are global across all nested
839   // WebContents/frame trees. Some properties are global only within their
840   // WebContents/frame tree.
841   //
842   // Each child frame RenderWidgetHost that inherits values gets them from their
843   // parent RenderWidget in the renderer process. It then passes them along to
844   // its own RenderWidget, and the process repeats down the tree.
845   //
846   // The plumbing goes:
847   // 1. Browser:    parent RenderWidgetHost
848   // 2. IPC           -> WidgetMsg_UpdateVisualProperties
849   // 3. Renderer A: parent RenderWidget
850   //                  (sometimes blink involved)
851   // 4. Renderer A: child  RenderFrameProxy
852   // 5. IPC           -> FrameHostMsg_SynchronizeVisualProperties
853   // 6. Browser:    child  CrossProcessFrameConnector
854   // 7. Browser:    parent RenderWidgetHost (We're here if |is_child_frame|.)
855   // 8. IPC           -> WidgetMsg_UpdateVisualProperties
856   // 9. Renderer B: child  RenderWidget
857 
858   // This property comes from the top-level main frame.
859   if (is_top_most_widget) {
860     visual_properties.compositor_viewport_pixel_rect =
861         gfx::Rect(view_->GetCompositorViewportPixelSize());
862   } else {
863     visual_properties.compositor_viewport_pixel_rect =
864         properties_from_parent_local_root_.compositor_viewport;
865     if (!IsUseZoomForDSFEnabled()) {
866       // If UseZoomForDSF is not used, the coordinates were not scaled by DSF
867       // when coming from the renderer.
868       visual_properties.compositor_viewport_pixel_rect =
869           gfx::ScaleToEnclosingRect(
870               visual_properties.compositor_viewport_pixel_rect,
871               visual_properties.screen_info.device_scale_factor);
872     }
873   }
874 
875   // These properties come from the top-level main frame's renderer. The
876   // top-level main frame in the browser doesn't specify a value.
877   if (!is_top_most_widget) {
878     visual_properties.page_scale_factor =
879         properties_from_parent_local_root_.page_scale_factor;
880     visual_properties.is_pinch_gesture_active =
881         properties_from_parent_local_root_.is_pinch_gesture_active;
882   }
883 
884   // The |visible_viewport_size| is affected by auto-resize which is magical and
885   // tricky.
886   //
887   // For the top-level main frame, auto resize ends up asynchronously resizing
888   // the widget's RenderWidgetHostView and the size will show up there, so
889   // nothing needs to be written in here.
890   //
891   // For nested main frames, auto resize happens in the renderer so we need to
892   // store the size on this class and use that. When auto-resize is not enabled
893   // we use the size of the nested main frame's RenderWidgetHostView.
894   //
895   // For child frames, we always use the value provided from the parent.
896   //
897   // For non-frame widgets, there is no auto-resize and we behave like the top-
898   // level main frame.
899   gfx::Size viewport;
900   if (is_child_frame_widget)
901     viewport = properties_from_parent_local_root_.visible_viewport_size;
902   else
903     viewport = view_->GetVisibleViewportSize();
904   visual_properties.visible_viewport_size = viewport;
905 
906   visual_properties.capture_sequence_number = view_->GetCaptureSequenceNumber();
907 
908   // TODO(ccameron): GetLocalSurfaceId is not synchronized with the device
909   // scale factor of the surface. Fix this.
910   viz::LocalSurfaceIdAllocation local_surface_id_allocation =
911       view_->GetLocalSurfaceIdAllocation();
912   if (local_surface_id_allocation.IsValid()) {
913     visual_properties.local_surface_id_allocation = local_surface_id_allocation;
914   }
915 
916   if (screen_orientation_type_for_testing_) {
917     visual_properties.screen_info.orientation_type =
918         *screen_orientation_type_for_testing_;
919   }
920 
921   if (screen_orientation_angle_for_testing_) {
922     visual_properties.screen_info.orientation_angle =
923         *screen_orientation_angle_for_testing_;
924   }
925 
926   return visual_properties;
927 }
928 
SynchronizeVisualProperties()929 bool RenderWidgetHostImpl::SynchronizeVisualProperties() {
930   return SynchronizeVisualProperties(false);
931 }
932 
SynchronizeVisualPropertiesIgnoringPendingAck()933 void RenderWidgetHostImpl::SynchronizeVisualPropertiesIgnoringPendingAck() {
934   visual_properties_ack_pending_ = false;
935   SynchronizeVisualProperties();
936 }
937 
SynchronizeVisualProperties(bool scroll_focused_node_into_view)938 bool RenderWidgetHostImpl::SynchronizeVisualProperties(
939     bool scroll_focused_node_into_view) {
940   // If the RenderViewHost is inactive, then there is no RenderWidget that can
941   // receive visual properties yet, even though we are setting them on the
942   // browser side. Wait until there is a local main frame with a RenderWidget
943   // to receive these before sending the visual properties.
944   //
945   // When the RenderViewHost becomes active, a SynchronizeVisualProperties()
946   // call does not explicitly get made. That is because RenderWidgets for frames
947   // are created and initialized with a valid VisualProperties already, and once
948   // their initial navigation completes (and they are in the foreground) the
949   // RenderWidget will be shown, which means a VisualProperties update happens
950   // at the time where compositing begins.
951   //
952   // Note that this drops |scroll_focused_node_into_view| but this value does
953   // not make sense for an inactive RenderViewHost's top level RenderWidgetHost,
954   // because there is no frames associated with the RenderWidget when it is
955   // inactive, so there is no focused node, or anything to scroll and display.
956   if (owner_delegate_ && !owner_delegate_->IsMainFrameActive())
957     return false;
958   // This is similar to the above but when the renderer process has crashed, so
959   // more objects are gone than the RenderWidget.
960   if (!renderer_initialized_)
961     return false;
962 
963   // Skip if the |delegate_| has already been detached because it's web contents
964   // is being deleted, or if LocalSurfaceIdAllocation is suppressed, as we are
965   // first updating our internal state from a child's request, before
966   // subsequently merging ids to send.
967   if (visual_properties_ack_pending_ || !process_->IsInitializedAndNotDead() ||
968       !view_ || !view_->HasSize() || !delegate_ ||
969       surface_id_allocation_suppressed_ ||
970       !view_->CanSynchronizeVisualProperties()) {
971     return false;
972   }
973 
974   auto visual_properties = std::make_unique<VisualProperties>();
975   *visual_properties = GetVisualProperties();
976   if (!StoredVisualPropertiesNeedsUpdate(old_visual_properties_,
977                                          *visual_properties))
978     return false;
979 
980   visual_properties->scroll_focused_node_into_view =
981       scroll_focused_node_into_view;
982 
983   Send(new WidgetMsg_UpdateVisualProperties(routing_id_, *visual_properties));
984 
985   bool width_changed =
986       !old_visual_properties_ || old_visual_properties_->new_size.width() !=
987                                      visual_properties->new_size.width();
988 
989   // This is copied from RenderWidget::UpdateSurfaceAndScreenInfo and used to
990   // detect if there is a screen orientation change.
991   // TODO(lanwei): clean the duplicate code.
992   if (visual_properties && old_visual_properties_) {
993     bool orientation_changed =
994         old_visual_properties_->screen_info.orientation_angle !=
995             visual_properties->screen_info.orientation_angle ||
996         old_visual_properties_->screen_info.orientation_type !=
997             visual_properties->screen_info.orientation_type;
998     if (orientation_changed)
999       delegate_->DidChangeScreenOrientation();
1000   }
1001 
1002   TRACE_EVENT_WITH_FLOW2(
1003       TRACE_DISABLED_BY_DEFAULT("viz.surface_id_flow"),
1004       "RenderWidgetHostImpl::SynchronizeVisualProperties send message",
1005       visual_properties->local_surface_id_allocation
1006           .value_or(viz::LocalSurfaceIdAllocation())
1007           .local_surface_id()
1008           .submission_trace_id(),
1009       TRACE_EVENT_FLAG_FLOW_OUT, "message",
1010       "WidgetMsg_SynchronizeVisualProperties", "local_surface_id",
1011       visual_properties->local_surface_id_allocation
1012           .value_or(viz::LocalSurfaceIdAllocation())
1013           .local_surface_id()
1014           .ToString());
1015   visual_properties_ack_pending_ =
1016       DoesVisualPropertiesNeedAck(old_visual_properties_, *visual_properties);
1017   old_visual_properties_ = std::move(visual_properties);
1018 
1019   // Warning: |visual_properties| invalid after this point.
1020 
1021   if (delegate_) {
1022     delegate_->RenderWidgetWasResized(this, width_changed);
1023   }
1024 
1025   return true;
1026 }
1027 
GotFocus()1028 void RenderWidgetHostImpl::GotFocus() {
1029   Focus();
1030   if (owner_delegate_)
1031     owner_delegate_->RenderWidgetGotFocus();
1032   if (delegate_)
1033     delegate_->RenderWidgetGotFocus(this);
1034 }
1035 
LostFocus()1036 void RenderWidgetHostImpl::LostFocus() {
1037   Blur();
1038   if (owner_delegate_)
1039     owner_delegate_->RenderWidgetLostFocus();
1040   if (delegate_)
1041     delegate_->RenderWidgetLostFocus(this);
1042 }
1043 
Focus()1044 void RenderWidgetHostImpl::Focus() {
1045   RenderWidgetHostImpl* focused_widget =
1046       delegate_ ? delegate_->GetRenderWidgetHostWithPageFocus() : nullptr;
1047 
1048   if (!focused_widget)
1049     focused_widget = this;
1050   focused_widget->SetPageFocus(true);
1051 }
1052 
Blur()1053 void RenderWidgetHostImpl::Blur() {
1054   RenderWidgetHostImpl* focused_widget =
1055       delegate_ ? delegate_->GetRenderWidgetHostWithPageFocus() : nullptr;
1056 
1057   if (!focused_widget)
1058     focused_widget = this;
1059   focused_widget->SetPageFocus(false);
1060 }
1061 
FlushForTesting()1062 void RenderWidgetHostImpl::FlushForTesting() {
1063   if (associated_widget_input_handler_)
1064     return associated_widget_input_handler_.FlushForTesting();
1065   if (widget_input_handler_)
1066     return widget_input_handler_.FlushForTesting();
1067 }
1068 
SetPageFocus(bool focused)1069 void RenderWidgetHostImpl::SetPageFocus(bool focused) {
1070   is_focused_ = focused;
1071 
1072   // Portals should never get page focus.
1073   DCHECK(!delegate_ || !delegate_->IsPortal() || !focused);
1074 
1075   if (!focused) {
1076     // If there is a pending mouse lock request, we don't want to reject it at
1077     // this point. The user can switch focus back to this view and approve the
1078     // request later.
1079     if (IsMouseLocked())
1080       view_->UnlockMouse();
1081 
1082     if (IsKeyboardLocked())
1083       UnlockKeyboard();
1084 
1085     if (auto* touch_emulator = GetExistingTouchEmulator())
1086       touch_emulator->CancelTouch();
1087   } else if (keyboard_lock_allowed_) {
1088     LockKeyboard();
1089   }
1090 
1091   GetWidgetInputHandler()->SetFocus(focused);
1092 
1093   // Also send page-level focus state to other SiteInstances involved in
1094   // rendering the current FrameTree, if this widget is for a main frame.
1095   if (owner_delegate_ && delegate_)
1096     delegate_->ReplicatePageFocus(focused);
1097 }
1098 
LostCapture()1099 void RenderWidgetHostImpl::LostCapture() {
1100   if (auto* touch_emulator = GetExistingTouchEmulator())
1101     touch_emulator->CancelTouch();
1102 
1103   GetWidgetInputHandler()->MouseCaptureLost();
1104 
1105   if (delegate_)
1106     delegate_->LostCapture(this);
1107 }
1108 
SetActive(bool active)1109 void RenderWidgetHostImpl::SetActive(bool active) {
1110   Send(new WidgetMsg_SetActive(routing_id_, active));
1111 }
1112 
LostMouseLock()1113 void RenderWidgetHostImpl::LostMouseLock() {
1114   if (delegate_)
1115     delegate_->LostMouseLock(this);
1116 }
1117 
SendMouseLockLost()1118 void RenderWidgetHostImpl::SendMouseLockLost() {
1119   widget_input_handler_->MouseLockLost();
1120 }
1121 
ViewDestroyed()1122 void RenderWidgetHostImpl::ViewDestroyed() {
1123   CancelKeyboardLock();
1124   RejectMouseLockOrUnlockIfNecessary(
1125       blink::mojom::PointerLockResult::kElementDestroyed);
1126 
1127   // TODO(evanm): tracking this may no longer be necessary;
1128   // eliminate this function if so.
1129   SetView(nullptr);
1130 }
1131 
RequestRepaintForTesting()1132 bool RenderWidgetHostImpl::RequestRepaintForTesting() {
1133   if (!view_)
1134     return false;
1135 
1136   return view_->RequestRepaintForTesting();
1137 }
1138 
RenderProcessBlockedStateChanged(bool blocked)1139 void RenderWidgetHostImpl::RenderProcessBlockedStateChanged(bool blocked) {
1140   if (blocked)
1141     StopInputEventAckTimeout();
1142   else
1143     RestartInputEventAckTimeoutIfNecessary();
1144 }
1145 
StartInputEventAckTimeout()1146 void RenderWidgetHostImpl::StartInputEventAckTimeout() {
1147   if (ShouldDisableHangMonitor())
1148     return;
1149 
1150   if (!input_event_ack_timeout_.IsRunning()) {
1151     input_event_ack_timeout_.Start(
1152         FROM_HERE, hung_renderer_delay_,
1153         base::BindOnce(&RenderWidgetHostImpl::OnInputEventAckTimeout,
1154                        weak_factory_.GetWeakPtr()));
1155     input_event_ack_start_time_ = clock_->NowTicks();
1156   }
1157 }
1158 
RestartInputEventAckTimeoutIfNecessary()1159 void RenderWidgetHostImpl::RestartInputEventAckTimeoutIfNecessary() {
1160   if (!GetProcess()->IsBlocked() && !ShouldDisableHangMonitor() &&
1161       in_flight_event_count_ > 0 && !is_hidden_) {
1162     input_event_ack_timeout_.Start(
1163         FROM_HERE, hung_renderer_delay_,
1164         base::BindOnce(&RenderWidgetHostImpl::OnInputEventAckTimeout,
1165                        weak_factory_.GetWeakPtr()));
1166   }
1167 }
1168 
IsCurrentlyUnresponsive()1169 bool RenderWidgetHostImpl::IsCurrentlyUnresponsive() {
1170   return is_unresponsive_;
1171 }
1172 
StopInputEventAckTimeout()1173 void RenderWidgetHostImpl::StopInputEventAckTimeout() {
1174   input_event_ack_timeout_.Stop();
1175 
1176   if (!input_event_ack_start_time_.is_null()) {
1177     base::TimeDelta elapsed = clock_->NowTicks() - input_event_ack_start_time_;
1178     const base::TimeDelta kMinimumHangTimeToReport =
1179         base::TimeDelta::FromSeconds(5);
1180     if (elapsed >= kMinimumHangTimeToReport)
1181       UMA_HISTOGRAM_LONG_TIMES("Renderer.Hung.Duration", elapsed);
1182 
1183     input_event_ack_start_time_ = TimeTicks();
1184   }
1185   RendererIsResponsive();
1186 }
1187 
DidNavigate()1188 void RenderWidgetHostImpl::DidNavigate() {
1189   // Stop the flinging after navigating to a new page.
1190   StopFling();
1191 
1192   // Resize messages before navigation are not acked, so reset
1193   // |visual_properties_ack_pending_| and make sure the next resize will be
1194   // acked if the last resize before navigation was supposed to be acked.
1195   visual_properties_ack_pending_ = false;
1196   if (view_)
1197     view_->DidNavigate();
1198 
1199   if (!new_content_rendering_timeout_)
1200     return;
1201 
1202   new_content_rendering_timeout_->Start(new_content_rendering_delay_);
1203 
1204   ClearPendingUserActivation();
1205 }
1206 
ForwardMouseEvent(const WebMouseEvent & mouse_event)1207 void RenderWidgetHostImpl::ForwardMouseEvent(const WebMouseEvent& mouse_event) {
1208   // VrController moves the pointer during the scrolling and fling. To ensure
1209   // that scroll performance is not affected we drop mouse events during
1210   // scroll/fling.
1211   if (GetView()->IsInVR() && (is_in_gesture_scroll_[static_cast<int>(
1212                                   blink::WebGestureDevice::kTouchpad)] ||
1213                               is_in_touchpad_gesture_fling_)) {
1214     return;
1215   }
1216 
1217   ForwardMouseEventWithLatencyInfo(mouse_event,
1218                                    ui::LatencyInfo(ui::SourceEventType::MOUSE));
1219   if (owner_delegate_)
1220     owner_delegate_->RenderWidgetDidForwardMouseEvent(mouse_event);
1221 }
1222 
ForwardMouseEventWithLatencyInfo(const blink::WebMouseEvent & mouse_event,const ui::LatencyInfo & latency)1223 void RenderWidgetHostImpl::ForwardMouseEventWithLatencyInfo(
1224     const blink::WebMouseEvent& mouse_event,
1225     const ui::LatencyInfo& latency) {
1226   TRACE_EVENT2("input", "RenderWidgetHostImpl::ForwardMouseEvent", "x",
1227                mouse_event.PositionInWidget().x(), "y",
1228                mouse_event.PositionInWidget().y());
1229 
1230   DCHECK_GE(mouse_event.GetType(), blink::WebInputEvent::kMouseTypeFirst);
1231   DCHECK_LE(mouse_event.GetType(), blink::WebInputEvent::kMouseTypeLast);
1232 
1233   for (size_t i = 0; i < mouse_event_callbacks_.size(); ++i) {
1234     if (mouse_event_callbacks_[i].Run(mouse_event))
1235       return;
1236   }
1237 
1238   if (IsIgnoringInputEvents())
1239     return;
1240 
1241   // Delegate must be non-null, due to |IsIgnoringInputEvents()| test.
1242   if (delegate_->PreHandleMouseEvent(mouse_event))
1243     return;
1244 
1245   auto* touch_emulator = GetExistingTouchEmulator();
1246   if (touch_emulator &&
1247       touch_emulator->HandleMouseEvent(mouse_event, GetView())) {
1248     return;
1249   }
1250 
1251   MouseEventWithLatencyInfo mouse_with_latency(mouse_event, latency);
1252   DispatchInputEventWithLatencyInfo(mouse_event, &mouse_with_latency.latency);
1253   input_router_->SendMouseEvent(
1254       mouse_with_latency, base::BindOnce(&RenderWidgetHostImpl::OnMouseEventAck,
1255                                          weak_factory_.GetWeakPtr()));
1256 }
1257 
ForwardWheelEvent(const WebMouseWheelEvent & wheel_event)1258 void RenderWidgetHostImpl::ForwardWheelEvent(
1259     const WebMouseWheelEvent& wheel_event) {
1260   ForwardWheelEventWithLatencyInfo(wheel_event,
1261                                    ui::LatencyInfo(ui::SourceEventType::WHEEL));
1262 }
1263 
ForwardWheelEventWithLatencyInfo(const blink::WebMouseWheelEvent & wheel_event,const ui::LatencyInfo & latency)1264 void RenderWidgetHostImpl::ForwardWheelEventWithLatencyInfo(
1265     const blink::WebMouseWheelEvent& wheel_event,
1266     const ui::LatencyInfo& latency) {
1267   TRACE_EVENT2("input", "RenderWidgetHostImpl::ForwardWheelEvent", "dx",
1268                wheel_event.delta_x, "dy", wheel_event.delta_y);
1269 
1270   if (IsIgnoringInputEvents())
1271     return;
1272 
1273   auto* touch_emulator = GetExistingTouchEmulator();
1274   if (touch_emulator && touch_emulator->HandleMouseWheelEvent(wheel_event))
1275     return;
1276 
1277   MouseWheelEventWithLatencyInfo wheel_with_latency(wheel_event, latency);
1278   DispatchInputEventWithLatencyInfo(wheel_event, &wheel_with_latency.latency);
1279   input_router_->SendWheelEvent(wheel_with_latency);
1280 }
1281 
WaitForInputProcessed(SyntheticGestureParams::GestureType type,SyntheticGestureParams::GestureSourceType source,base::OnceClosure callback)1282 void RenderWidgetHostImpl::WaitForInputProcessed(
1283     SyntheticGestureParams::GestureType type,
1284     SyntheticGestureParams::GestureSourceType source,
1285     base::OnceClosure callback) {
1286   // TODO(bokan): Input can be queued and delayed in InputRouterImpl based on
1287   // the kind of events we're getting. To be truly robust, we should wait until
1288   // those queues are flushed before issuing this message. This will be done in
1289   // a follow-up and is the reason for the currently unused type and source
1290   // params. https://crbug.com/902446.
1291   WaitForInputProcessed(std::move(callback));
1292 }
1293 
WaitForInputProcessed(base::OnceClosure callback)1294 void RenderWidgetHostImpl::WaitForInputProcessed(base::OnceClosure callback) {
1295   // TODO(bokan): The RequestPresentationCallback mechanism doesn't seem to
1296   // work in OOPIFs. For now, just callback immediately. Remove when fixed.
1297   // https://crbug.com/924646.
1298   if (GetView()->IsRenderWidgetHostViewChildFrame()) {
1299     std::move(callback).Run();
1300     return;
1301   }
1302 
1303   input_router_->WaitForInputProcessed(std::move(callback));
1304 }
1305 
ForwardGestureEvent(const blink::WebGestureEvent & gesture_event)1306 void RenderWidgetHostImpl::ForwardGestureEvent(
1307     const blink::WebGestureEvent& gesture_event) {
1308   ForwardGestureEventWithLatencyInfo(
1309       gesture_event,
1310       ui::WebInputEventTraits::CreateLatencyInfoForWebGestureEvent(
1311           gesture_event));
1312 }
1313 
ForwardGestureEventWithLatencyInfo(const blink::WebGestureEvent & gesture_event,const ui::LatencyInfo & latency)1314 void RenderWidgetHostImpl::ForwardGestureEventWithLatencyInfo(
1315     const blink::WebGestureEvent& gesture_event,
1316     const ui::LatencyInfo& latency) {
1317   TRACE_EVENT1("input", "RenderWidgetHostImpl::ForwardGestureEvent", "type",
1318                WebInputEvent::GetName(gesture_event.GetType()));
1319   // Early out if necessary, prior to performing latency logic.
1320   if (IsIgnoringInputEvents())
1321     return;
1322 
1323   // The gesture events must have a known source.
1324   DCHECK_NE(gesture_event.SourceDevice(),
1325             blink::WebGestureDevice::kUninitialized);
1326 
1327   if (gesture_event.GetType() == blink::WebInputEvent::kGestureScrollBegin) {
1328     DCHECK(
1329         !is_in_gesture_scroll_[static_cast<int>(gesture_event.SourceDevice())]);
1330     is_in_gesture_scroll_[static_cast<int>(gesture_event.SourceDevice())] =
1331         true;
1332     scroll_peak_gpu_mem_tracker_ =
1333         PeakGpuMemoryTracker::Create(PeakGpuMemoryTracker::Usage::SCROLL);
1334   } else if (gesture_event.GetType() ==
1335              blink::WebInputEvent::kGestureScrollEnd) {
1336     DCHECK(
1337         is_in_gesture_scroll_[static_cast<int>(gesture_event.SourceDevice())]);
1338     is_in_gesture_scroll_[static_cast<int>(gesture_event.SourceDevice())] =
1339         false;
1340     is_in_touchpad_gesture_fling_ = false;
1341     if (view_) {
1342       if (scroll_peak_gpu_mem_tracker_ &&
1343           !view_->is_currently_scrolling_viewport()) {
1344         // We start tracking peak gpu-memory usage when the initial scroll-begin
1345         // is dispatched. However, it is possible that the scroll-begin did not
1346         // trigger any scrolls (e.g. the page is not scrollable). In such cases,
1347         // we do not want to report the peak-memory usage metric. So it is
1348         // canceled here.
1349         scroll_peak_gpu_mem_tracker_->Cancel();
1350       }
1351 
1352       view_->set_is_currently_scrolling_viewport(false);
1353     }
1354     scroll_peak_gpu_mem_tracker_ = nullptr;
1355   } else if (gesture_event.GetType() ==
1356              blink::WebInputEvent::kGestureFlingStart) {
1357     if (gesture_event.SourceDevice() == blink::WebGestureDevice::kTouchpad) {
1358       // TODO(sahel): Remove the VR specific case when motion events are used
1359       // for Android VR event processing and VR touchpad scrolling is handled by
1360       // sending wheel events rather than directly injecting Gesture Scroll
1361       // Events. https://crbug.com/797322
1362       if (GetView()->IsInVR()) {
1363         // Regardless of the state of the wheel scroll latching
1364         // WebContentsEventForwarder doesn't inject any GSE events before GFS.
1365         DCHECK(is_in_gesture_scroll_[static_cast<int>(
1366             gesture_event.SourceDevice())]);
1367 
1368         // Reset the is_in_gesture_scroll since while scrolling in Android VR
1369         // the first wheel event sent by the FlingController will cause a GSB
1370         // generation in MouseWheelEventQueue. This is because GSU events before
1371         // the GFS are directly injected to RWHI rather than being generated
1372         // from wheel events in MouseWheelEventQueue.
1373         is_in_gesture_scroll_[static_cast<int>(gesture_event.SourceDevice())] =
1374             false;
1375       }
1376       // a GSB event is generated from the first wheel event in a sequence after
1377       // the event is acked as not consumed by the renderer. Sometimes when the
1378       // main thread is busy/slow (e.g ChromeOS debug builds) a GFS arrives
1379       // before the first wheel is acked. In these cases no GSB will arrive
1380       // before the GFS. With browser side fling the out of order GFS arrival
1381       // does not need a DCHECK since the fling controller will process the GFS
1382       // and start queuing wheel events which will follow the one currently
1383       // awaiting ACK and the renderer receives the events in order.
1384 
1385       is_in_touchpad_gesture_fling_ = true;
1386     } else {
1387       DCHECK(is_in_gesture_scroll_[static_cast<int>(
1388           gesture_event.SourceDevice())]);
1389 
1390       // The FlingController handles GFS with touchscreen source and sends GSU
1391       // events with inertial state to the renderer to progress the fling.
1392       // is_in_gesture_scroll must stay true till the fling progress is
1393       // finished. Then the FlingController will generate and send a GSE which
1394       // shows the end of a scroll sequence and resets is_in_gesture_scroll_.
1395     }
1396   }
1397 
1398   // Delegate must be non-null, due to |IsIgnoringInputEvents()| test.
1399   if (delegate_->PreHandleGestureEvent(gesture_event))
1400     return;
1401 
1402   GestureEventWithLatencyInfo gesture_with_latency(gesture_event, latency);
1403   DispatchInputEventWithLatencyInfo(gesture_event,
1404                                     &gesture_with_latency.latency);
1405   input_router_->SendGestureEvent(gesture_with_latency);
1406 }
1407 
ForwardTouchEventWithLatencyInfo(const blink::WebTouchEvent & touch_event,const ui::LatencyInfo & latency)1408 void RenderWidgetHostImpl::ForwardTouchEventWithLatencyInfo(
1409     const blink::WebTouchEvent& touch_event,
1410     const ui::LatencyInfo& latency) {
1411   TRACE_EVENT0("input", "RenderWidgetHostImpl::ForwardTouchEvent");
1412 
1413   // Always forward TouchEvents for touch stream consistency. They will be
1414   // ignored if appropriate in FilterInputEvent().
1415 
1416   TouchEventWithLatencyInfo touch_with_latency(touch_event, latency);
1417   DispatchInputEventWithLatencyInfo(touch_event, &touch_with_latency.latency);
1418   input_router_->SendTouchEvent(touch_with_latency);
1419 }
1420 
ForwardKeyboardEvent(const NativeWebKeyboardEvent & key_event)1421 void RenderWidgetHostImpl::ForwardKeyboardEvent(
1422     const NativeWebKeyboardEvent& key_event) {
1423   ui::LatencyInfo latency_info;
1424 
1425   if (key_event.GetType() == WebInputEvent::kRawKeyDown ||
1426       key_event.GetType() == WebInputEvent::kChar) {
1427     latency_info.set_source_event_type(ui::SourceEventType::KEY_PRESS);
1428   }
1429   ForwardKeyboardEventWithLatencyInfo(key_event, latency_info);
1430 }
1431 
ForwardKeyboardEventWithLatencyInfo(const NativeWebKeyboardEvent & key_event,const ui::LatencyInfo & latency)1432 void RenderWidgetHostImpl::ForwardKeyboardEventWithLatencyInfo(
1433     const NativeWebKeyboardEvent& key_event,
1434     const ui::LatencyInfo& latency) {
1435   ForwardKeyboardEventWithCommands(key_event, latency, nullptr, nullptr);
1436 }
1437 
ForwardKeyboardEventWithCommands(const NativeWebKeyboardEvent & key_event,const ui::LatencyInfo & latency,const std::vector<EditCommand> * commands,bool * update_event)1438 void RenderWidgetHostImpl::ForwardKeyboardEventWithCommands(
1439     const NativeWebKeyboardEvent& key_event,
1440     const ui::LatencyInfo& latency,
1441     const std::vector<EditCommand>* commands,
1442     bool* update_event) {
1443   DCHECK(WebInputEvent::IsKeyboardEventType(key_event.GetType()));
1444 
1445   TRACE_EVENT0("input", "RenderWidgetHostImpl::ForwardKeyboardEvent");
1446   if (owner_delegate_ &&
1447       !owner_delegate_->MayRenderWidgetForwardKeyboardEvent(key_event)) {
1448     return;
1449   }
1450 
1451   if (IsIgnoringInputEvents())
1452     return;
1453 
1454   if (!process_->IsInitializedAndNotDead())
1455     return;
1456 
1457   // First, let keypress listeners take a shot at handling the event.  If a
1458   // listener handles the event, it should not be propagated to the renderer.
1459   if (KeyPressListenersHandleEvent(key_event)) {
1460     // Some keypresses that are accepted by the listener may be followed by Char
1461     // and KeyUp events, which should be ignored.
1462     if (key_event.GetType() == WebKeyboardEvent::kRawKeyDown)
1463       suppress_events_until_keydown_ = true;
1464     return;
1465   }
1466 
1467   if (suppress_events_until_keydown_) {
1468     // If the preceding RawKeyDown event was handled by the browser, then we
1469     // need to suppress all events generated by it until the next RawKeyDown or
1470     // KeyDown event.
1471     if (key_event.GetType() == WebKeyboardEvent::kKeyUp ||
1472         key_event.GetType() == WebKeyboardEvent::kChar)
1473       return;
1474     DCHECK(key_event.GetType() == WebKeyboardEvent::kRawKeyDown ||
1475            key_event.GetType() == WebKeyboardEvent::kKeyDown);
1476     suppress_events_until_keydown_ = false;
1477   }
1478 
1479   bool is_shortcut = false;
1480 
1481   // Only pre-handle the key event if it's not handled by the input method.
1482   if (delegate_ && !key_event.skip_in_browser) {
1483     // We need to set |suppress_events_until_keydown_| to true if
1484     // PreHandleKeyboardEvent() handles the event, but |this| may already be
1485     // destroyed at that time. So set |suppress_events_until_keydown_| true
1486     // here, then revert it afterwards when necessary.
1487     if (key_event.GetType() == WebKeyboardEvent::kRawKeyDown)
1488       suppress_events_until_keydown_ = true;
1489 
1490     // Tab switching/closing accelerators aren't sent to the renderer to avoid
1491     // a hung/malicious renderer from interfering.
1492     switch (delegate_->PreHandleKeyboardEvent(key_event)) {
1493       case KeyboardEventProcessingResult::HANDLED:
1494         return;
1495 #if defined(USE_AURA)
1496       case KeyboardEventProcessingResult::HANDLED_DONT_UPDATE_EVENT:
1497         if (update_event)
1498           *update_event = false;
1499         return;
1500 #endif
1501       case KeyboardEventProcessingResult::NOT_HANDLED:
1502         break;
1503       case KeyboardEventProcessingResult::NOT_HANDLED_IS_SHORTCUT:
1504         is_shortcut = true;
1505         break;
1506     }
1507 
1508     if (key_event.GetType() == WebKeyboardEvent::kRawKeyDown)
1509       suppress_events_until_keydown_ = false;
1510   }
1511 
1512   auto* touch_emulator = GetExistingTouchEmulator();
1513   if (touch_emulator && touch_emulator->HandleKeyboardEvent(key_event))
1514     return;
1515   NativeWebKeyboardEventWithLatencyInfo key_event_with_latency(key_event,
1516                                                                latency);
1517   key_event_with_latency.event.is_browser_shortcut = is_shortcut;
1518   DispatchInputEventWithLatencyInfo(key_event, &key_event_with_latency.latency);
1519   // TODO(foolip): |InputRouter::SendKeyboardEvent()| may filter events, in
1520   // which the commands will be treated as belonging to the next key event.
1521   // WidgetInputHandler::SetEditCommandsForNextKeyEvent should only be sent if
1522   // WidgetInputHandler::DispatchEvent is, but has to be sent first.
1523   // https://crbug.com/684298
1524   if (commands && !commands->empty())
1525     GetWidgetInputHandler()->SetEditCommandsForNextKeyEvent(*commands);
1526 
1527   input_router_->SendKeyboardEvent(
1528       key_event_with_latency,
1529       base::BindOnce(&RenderWidgetHostImpl::OnKeyboardEventAck,
1530                      weak_factory_.GetWeakPtr()));
1531 }
1532 
CreateSyntheticGestureControllerIfNecessary()1533 void RenderWidgetHostImpl::CreateSyntheticGestureControllerIfNecessary() {
1534   if (!synthetic_gesture_controller_ && view_) {
1535     synthetic_gesture_controller_ =
1536         std::make_unique<SyntheticGestureController>(
1537             this, view_->CreateSyntheticGestureTarget());
1538   }
1539 }
1540 
QueueSyntheticGesture(std::unique_ptr<SyntheticGesture> synthetic_gesture,base::OnceCallback<void (SyntheticGesture::Result)> on_complete)1541 void RenderWidgetHostImpl::QueueSyntheticGesture(
1542     std::unique_ptr<SyntheticGesture> synthetic_gesture,
1543     base::OnceCallback<void(SyntheticGesture::Result)> on_complete) {
1544   CreateSyntheticGestureControllerIfNecessary();
1545   if (synthetic_gesture_controller_) {
1546     synthetic_gesture_controller_->QueueSyntheticGesture(
1547         std::move(synthetic_gesture), std::move(on_complete));
1548   }
1549 }
1550 
QueueSyntheticGestureCompleteImmediately(std::unique_ptr<SyntheticGesture> synthetic_gesture)1551 void RenderWidgetHostImpl::QueueSyntheticGestureCompleteImmediately(
1552     std::unique_ptr<SyntheticGesture> synthetic_gesture) {
1553   CreateSyntheticGestureControllerIfNecessary();
1554   if (synthetic_gesture_controller_) {
1555     synthetic_gesture_controller_->QueueSyntheticGestureCompleteImmediately(
1556         std::move(synthetic_gesture));
1557   }
1558 }
1559 
EnsureReadyForSyntheticGestures(base::OnceClosure on_ready)1560 void RenderWidgetHostImpl::EnsureReadyForSyntheticGestures(
1561     base::OnceClosure on_ready) {
1562   CreateSyntheticGestureControllerIfNecessary();
1563   if (synthetic_gesture_controller_) {
1564     synthetic_gesture_controller_->EnsureRendererInitialized(
1565         std::move(on_ready));
1566   } else {
1567     // If we couldn't create a SyntheticGestureController then we won't ever be
1568     // ready.  Invoke the callback to unblock the calling code.
1569     std::move(on_ready).Run();
1570   }
1571 }
1572 
TakeSyntheticGestureController(RenderWidgetHostImpl * host)1573 void RenderWidgetHostImpl::TakeSyntheticGestureController(
1574     RenderWidgetHostImpl* host) {
1575   DCHECK(!synthetic_gesture_controller_);
1576   if (host->synthetic_gesture_controller_) {
1577     synthetic_gesture_controller_ =
1578         std::move(host->synthetic_gesture_controller_);
1579     synthetic_gesture_controller_->UpdateSyntheticGestureTarget(
1580         view_->CreateSyntheticGestureTarget(), this);
1581   }
1582 }
1583 
SetCursor(const WebCursor & cursor)1584 void RenderWidgetHostImpl::SetCursor(const WebCursor& cursor) {
1585   if (!view_)
1586     return;
1587   view_->UpdateCursor(cursor);
1588 }
1589 
ShowContextMenuAtPoint(const gfx::Point & point,const ui::MenuSourceType source_type)1590 void RenderWidgetHostImpl::ShowContextMenuAtPoint(
1591     const gfx::Point& point,
1592     const ui::MenuSourceType source_type) {
1593   Send(new WidgetMsg_ShowContextMenu(GetRoutingID(), source_type, point));
1594 }
1595 
OnCursorVisibilityStateChanged(bool is_visible)1596 void RenderWidgetHostImpl::OnCursorVisibilityStateChanged(bool is_visible) {
1597   GetWidgetInputHandler()->CursorVisibilityChanged(is_visible);
1598 }
1599 
OnFallbackCursorModeToggled(bool is_on)1600 void RenderWidgetHostImpl::OnFallbackCursorModeToggled(bool is_on) {
1601   GetWidgetInputHandler()->FallbackCursorModeToggled(is_on);
1602 }
1603 
1604 // static
DisableResizeAckCheckForTesting()1605 void RenderWidgetHostImpl::DisableResizeAckCheckForTesting() {
1606   g_check_for_pending_visual_properties_ack = false;
1607 }
1608 
AddKeyPressEventCallback(const KeyPressEventCallback & callback)1609 void RenderWidgetHostImpl::AddKeyPressEventCallback(
1610     const KeyPressEventCallback& callback) {
1611   key_press_event_callbacks_.push_back(callback);
1612 }
1613 
RemoveKeyPressEventCallback(const KeyPressEventCallback & callback)1614 void RenderWidgetHostImpl::RemoveKeyPressEventCallback(
1615     const KeyPressEventCallback& callback) {
1616   for (size_t i = 0; i < key_press_event_callbacks_.size(); ++i) {
1617     if (key_press_event_callbacks_[i] == callback) {
1618       key_press_event_callbacks_.erase(
1619           key_press_event_callbacks_.begin() + i);
1620       return;
1621     }
1622   }
1623 }
1624 
AddMouseEventCallback(const MouseEventCallback & callback)1625 void RenderWidgetHostImpl::AddMouseEventCallback(
1626     const MouseEventCallback& callback) {
1627   mouse_event_callbacks_.push_back(callback);
1628 }
1629 
RemoveMouseEventCallback(const MouseEventCallback & callback)1630 void RenderWidgetHostImpl::RemoveMouseEventCallback(
1631     const MouseEventCallback& callback) {
1632   for (size_t i = 0; i < mouse_event_callbacks_.size(); ++i) {
1633     if (mouse_event_callbacks_[i] == callback) {
1634       mouse_event_callbacks_.erase(mouse_event_callbacks_.begin() + i);
1635       return;
1636     }
1637   }
1638 }
1639 
AddInputEventObserver(RenderWidgetHost::InputEventObserver * observer)1640 void RenderWidgetHostImpl::AddInputEventObserver(
1641     RenderWidgetHost::InputEventObserver* observer) {
1642   if (!input_event_observers_.HasObserver(observer))
1643     input_event_observers_.AddObserver(observer);
1644 }
1645 
RemoveInputEventObserver(RenderWidgetHost::InputEventObserver * observer)1646 void RenderWidgetHostImpl::RemoveInputEventObserver(
1647     RenderWidgetHost::InputEventObserver* observer) {
1648   input_event_observers_.RemoveObserver(observer);
1649 }
1650 
AddObserver(RenderWidgetHostObserver * observer)1651 void RenderWidgetHostImpl::AddObserver(RenderWidgetHostObserver* observer) {
1652   observers_.AddObserver(observer);
1653 }
1654 
RemoveObserver(RenderWidgetHostObserver * observer)1655 void RenderWidgetHostImpl::RemoveObserver(RenderWidgetHostObserver* observer) {
1656   observers_.RemoveObserver(observer);
1657 }
1658 
GetScreenInfo(ScreenInfo * result)1659 void RenderWidgetHostImpl::GetScreenInfo(ScreenInfo* result) {
1660   TRACE_EVENT0("renderer_host", "RenderWidgetHostImpl::GetScreenInfo");
1661   if (view_)
1662     view_->GetScreenInfo(result);
1663   else
1664     DisplayUtil::GetDefaultScreenInfo(result);
1665 
1666   if (display::Display::HasForceRasterColorProfile())
1667     result->color_space = display::Display::GetForcedRasterColorProfile();
1668 
1669   // TODO(sievers): find a way to make this done another way so the method
1670   // can be const.
1671   if (IsUseZoomForDSFEnabled())
1672     input_router_->SetDeviceScaleFactor(result->device_scale_factor);
1673 }
1674 
DragTargetDragEnter(const DropData & drop_data,const gfx::PointF & client_pt,const gfx::PointF & screen_pt,WebDragOperationsMask operations_allowed,int key_modifiers)1675 void RenderWidgetHostImpl::DragTargetDragEnter(
1676     const DropData& drop_data,
1677     const gfx::PointF& client_pt,
1678     const gfx::PointF& screen_pt,
1679     WebDragOperationsMask operations_allowed,
1680     int key_modifiers) {
1681   DragTargetDragEnterWithMetaData(DropDataToMetaData(drop_data), client_pt,
1682                                   screen_pt, operations_allowed, key_modifiers);
1683 }
1684 
DragTargetDragEnterWithMetaData(const std::vector<DropData::Metadata> & metadata,const gfx::PointF & client_pt,const gfx::PointF & screen_pt,WebDragOperationsMask operations_allowed,int key_modifiers)1685 void RenderWidgetHostImpl::DragTargetDragEnterWithMetaData(
1686     const std::vector<DropData::Metadata>& metadata,
1687     const gfx::PointF& client_pt,
1688     const gfx::PointF& screen_pt,
1689     WebDragOperationsMask operations_allowed,
1690     int key_modifiers) {
1691   Send(new DragMsg_TargetDragEnter(GetRoutingID(), metadata, client_pt,
1692                                    screen_pt, operations_allowed,
1693                                    key_modifiers));
1694 }
1695 
DragTargetDragOver(const gfx::PointF & client_pt,const gfx::PointF & screen_pt,WebDragOperationsMask operations_allowed,int key_modifiers)1696 void RenderWidgetHostImpl::DragTargetDragOver(
1697     const gfx::PointF& client_pt,
1698     const gfx::PointF& screen_pt,
1699     WebDragOperationsMask operations_allowed,
1700     int key_modifiers) {
1701   Send(new DragMsg_TargetDragOver(GetRoutingID(), client_pt, screen_pt,
1702                                   operations_allowed, key_modifiers));
1703 }
1704 
DragTargetDragLeave(const gfx::PointF & client_point,const gfx::PointF & screen_point)1705 void RenderWidgetHostImpl::DragTargetDragLeave(
1706     const gfx::PointF& client_point,
1707     const gfx::PointF& screen_point) {
1708   Send(new DragMsg_TargetDragLeave(GetRoutingID(), client_point, screen_point));
1709 }
1710 
DragTargetDrop(const DropData & drop_data,const gfx::PointF & client_pt,const gfx::PointF & screen_pt,int key_modifiers)1711 void RenderWidgetHostImpl::DragTargetDrop(const DropData& drop_data,
1712                                           const gfx::PointF& client_pt,
1713                                           const gfx::PointF& screen_pt,
1714                                           int key_modifiers) {
1715   DropData drop_data_with_permissions(drop_data);
1716   GrantFileAccessFromDropData(&drop_data_with_permissions);
1717   Send(new DragMsg_TargetDrop(GetRoutingID(), drop_data_with_permissions,
1718                               client_pt, screen_pt, key_modifiers));
1719 }
1720 
DragSourceEndedAt(const gfx::PointF & client_pt,const gfx::PointF & screen_pt,blink::WebDragOperation operation)1721 void RenderWidgetHostImpl::DragSourceEndedAt(
1722     const gfx::PointF& client_pt,
1723     const gfx::PointF& screen_pt,
1724     blink::WebDragOperation operation) {
1725   Send(new DragMsg_SourceEnded(GetRoutingID(),
1726                                client_pt,
1727                                screen_pt,
1728                                operation));
1729 }
1730 
DragSourceSystemDragEnded()1731 void RenderWidgetHostImpl::DragSourceSystemDragEnded() {
1732   // TODO(dtapuska): Remove this null check once all of the Drag IPCs
1733   // come over the mojo channels. It will be guaranteed that this
1734   // will be non-null.
1735   if (blink_frame_widget_)
1736     blink_frame_widget_->DragSourceSystemDragEnded();
1737 }
1738 
FilterDropData(DropData * drop_data)1739 void RenderWidgetHostImpl::FilterDropData(DropData* drop_data) {
1740 #if DCHECK_IS_ON()
1741   drop_data->view_id = GetRoutingID();
1742 #endif  // DCHECK_IS_ON()
1743 
1744   GetProcess()->FilterURL(true, &drop_data->url);
1745   if (drop_data->did_originate_from_renderer) {
1746     drop_data->filenames.clear();
1747   }
1748 }
1749 
SetCursor(const ui::Cursor & cursor)1750 void RenderWidgetHostImpl::SetCursor(const ui::Cursor& cursor) {
1751   SetCursor(WebCursor(cursor));
1752 }
1753 
GetPriority()1754 RenderProcessHost::Priority RenderWidgetHostImpl::GetPriority() {
1755   RenderProcessHost::Priority priority = {
1756     is_hidden_,
1757     frame_depth_,
1758     intersects_viewport_,
1759 #if defined(OS_ANDROID)
1760     importance_,
1761 #endif
1762   };
1763   if (owner_delegate_ &&
1764       !owner_delegate_->ShouldContributePriorityToProcess()) {
1765     priority.is_hidden = true;
1766     priority.frame_depth = RenderProcessHostImpl::kMaxFrameDepthForPriority;
1767 #if defined(OS_ANDROID)
1768     priority.importance = ChildProcessImportance::NORMAL;
1769 #endif
1770   }
1771   return priority;
1772 }
1773 
RenderProcessExited(RenderProcessHost * host,const ChildProcessTerminationInfo & info)1774 void RenderWidgetHostImpl::RenderProcessExited(
1775     RenderProcessHost* host,
1776     const ChildProcessTerminationInfo& info) {
1777   // When the RenderViewHost or the RenderFrameHost own this instance, they
1778   // manage its destruction. Otherwise it is owned by the renderer process and
1779   // must self-destroy when it exits.
1780   if (!owner_delegate_ && !owned_by_render_frame_host_)
1781     Destroy(true);
1782 }
1783 
GetWidgetInputHandler()1784 mojom::WidgetInputHandler* RenderWidgetHostImpl::GetWidgetInputHandler() {
1785   if (associated_widget_input_handler_)
1786     return associated_widget_input_handler_.get();
1787   if (widget_input_handler_)
1788     return widget_input_handler_.get();
1789   // TODO(dtapuska): Remove the need for the unbound interface. It is
1790   // possible that a RVHI may make calls to a WidgetInputHandler when
1791   // the main frame is remote. This is because of ordering issues during
1792   // widget shutdown, so we present an UnboundWidgetInputHandler had
1793   // DLOGS the message calls.
1794   return g_unbound_input_handler.Pointer();
1795 }
1796 
NotifyScreenInfoChanged()1797 void RenderWidgetHostImpl::NotifyScreenInfoChanged() {
1798   // The resize message (which may not happen immediately) will carry with it
1799   // the screen info as well as the new size (if the screen has changed scale
1800   // factor).
1801   SynchronizeVisualProperties();
1802 
1803   // The device scale factor will be same for all the views contained by the
1804   // MainFrame, so just set it once.
1805   if (delegate_ && !delegate_->IsWidgetForMainFrame(this))
1806     return;
1807 
1808   // The delegate may not have an input event router in tests.
1809   if (auto* touch_emulator = GetExistingTouchEmulator())
1810     touch_emulator->SetDeviceScaleFactor(GetScaleFactorForView(view_.get()));
1811 }
1812 
GetSnapshotFromBrowser(GetSnapshotFromBrowserCallback callback,bool from_surface)1813 void RenderWidgetHostImpl::GetSnapshotFromBrowser(
1814     GetSnapshotFromBrowserCallback callback,
1815     bool from_surface) {
1816   int snapshot_id = next_browser_snapshot_id_++;
1817   if (from_surface) {
1818     pending_surface_browser_snapshots_.insert(
1819         std::make_pair(snapshot_id, std::move(callback)));
1820     Send(new WidgetMsg_ForceRedraw(GetRoutingID(), snapshot_id));
1821     return;
1822   }
1823 
1824 #if defined(OS_MACOSX)
1825   // MacOS version of underlying GrabViewSnapshot() blocks while
1826   // display/GPU are in a power-saving mode, so make sure display
1827   // does not go to sleep for the duration of reading a snapshot.
1828   if (pending_browser_snapshots_.empty())
1829     GetWakeLock()->RequestWakeLock();
1830 #endif
1831   // TODO(nzolghadr): Remove the duplication here and the if block just above.
1832   pending_browser_snapshots_.insert(
1833       std::make_pair(snapshot_id, std::move(callback)));
1834   Send(new WidgetMsg_ForceRedraw(GetRoutingID(), snapshot_id));
1835 }
1836 
SelectionChanged(const base::string16 & text,uint32_t offset,const gfx::Range & range,bool user_initiated)1837 void RenderWidgetHostImpl::SelectionChanged(const base::string16& text,
1838                                             uint32_t offset,
1839                                             const gfx::Range& range,
1840                                             bool user_initiated) {
1841   if (view_) {
1842     view_->SelectionChanged(text, static_cast<size_t>(offset), range,
1843                             user_initiated);
1844   }
1845 }
1846 
OnSelectionBoundsChanged(const WidgetHostMsg_SelectionBounds_Params & params)1847 void RenderWidgetHostImpl::OnSelectionBoundsChanged(
1848     const WidgetHostMsg_SelectionBounds_Params& params) {
1849   if (view_)
1850     view_->SelectionBoundsChanged(params);
1851 }
1852 
OnStartDragging(const DropData & drop_data,blink::WebDragOperationsMask drag_operations_mask,const SkBitmap & bitmap,const gfx::Vector2d & bitmap_offset_in_dip,const DragEventSourceInfo & event_info)1853 void RenderWidgetHostImpl::OnStartDragging(
1854     const DropData& drop_data,
1855     blink::WebDragOperationsMask drag_operations_mask,
1856     const SkBitmap& bitmap,
1857     const gfx::Vector2d& bitmap_offset_in_dip,
1858     const DragEventSourceInfo& event_info) {
1859   RenderViewHostDelegateView* view = delegate_->GetDelegateView();
1860   if (!view || !GetView()) {
1861     // Need to clear drag and drop state in blink.
1862     DragSourceSystemDragEnded();
1863     return;
1864   }
1865 
1866   DropData filtered_data(drop_data);
1867   RenderProcessHost* process = GetProcess();
1868   ChildProcessSecurityPolicyImpl* policy =
1869       ChildProcessSecurityPolicyImpl::GetInstance();
1870 
1871   // Allow drag of Javascript URLs to enable bookmarklet drag to bookmark bar.
1872   if (!filtered_data.url.SchemeIs(url::kJavaScriptScheme))
1873     process->FilterURL(true, &filtered_data.url);
1874   process->FilterURL(false, &filtered_data.html_base_url);
1875   // Filter out any paths that the renderer didn't have access to. This prevents
1876   // the following attack on a malicious renderer:
1877   // 1. StartDragging IPC sent with renderer-specified filesystem paths that it
1878   //    doesn't have read permissions for.
1879   // 2. We initiate a native DnD operation.
1880   // 3. DnD operation immediately ends since mouse is not held down. DnD events
1881   //    still fire though, which causes read permissions to be granted to the
1882   //    renderer for any file paths in the drop.
1883   filtered_data.filenames.clear();
1884   for (const auto& file_info : drop_data.filenames) {
1885     if (policy->CanReadFile(GetProcess()->GetID(), file_info.path))
1886       filtered_data.filenames.push_back(file_info);
1887   }
1888 
1889   storage::FileSystemContext* file_system_context =
1890       GetProcess()->GetStoragePartition()->GetFileSystemContext();
1891   filtered_data.file_system_files.clear();
1892   for (size_t i = 0; i < drop_data.file_system_files.size(); ++i) {
1893     storage::FileSystemURL file_system_url =
1894         file_system_context->CrackURL(drop_data.file_system_files[i].url);
1895     if (policy->CanReadFileSystemFile(GetProcess()->GetID(), file_system_url))
1896       filtered_data.file_system_files.push_back(drop_data.file_system_files[i]);
1897   }
1898 
1899   float scale = GetScaleFactorForView(GetView());
1900   gfx::ImageSkia image(gfx::ImageSkiaRep(bitmap, scale));
1901   view->StartDragging(filtered_data, drag_operations_mask, image,
1902                       bitmap_offset_in_dip, event_info, this);
1903 }
1904 
OnUpdateDragCursor(WebDragOperation current_op)1905 void RenderWidgetHostImpl::OnUpdateDragCursor(WebDragOperation current_op) {
1906   if (delegate_->OnUpdateDragCursor())
1907     return;
1908 
1909   RenderViewHostDelegateView* view = delegate_->GetDelegateView();
1910   if (view)
1911     view->UpdateDragCursor(current_op);
1912 }
1913 
OnFrameSwapMessagesReceived(uint32_t frame_token,std::vector<IPC::Message> messages)1914 void RenderWidgetHostImpl::OnFrameSwapMessagesReceived(
1915     uint32_t frame_token,
1916     std::vector<IPC::Message> messages) {
1917   frame_token_message_queue_->OnFrameSwapMessagesReceived(frame_token,
1918                                                           std::move(messages));
1919 }
1920 
OnForceRedrawComplete(int snapshot_id)1921 void RenderWidgetHostImpl::OnForceRedrawComplete(int snapshot_id) {
1922 #if defined(OS_MACOSX) || defined(OS_WIN)
1923   // On Mac, when using CoreAnimation, or Win32 when using GDI, there is a
1924   // delay between when content is drawn to the screen, and when the
1925   // snapshot will actually pick up that content. Insert a manual delay of
1926   // 1/6th of a second (to simulate 10 frames at 60 fps) before actually
1927   // taking the snapshot.
1928   base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
1929       FROM_HERE,
1930       base::BindOnce(&RenderWidgetHostImpl::WindowSnapshotReachedScreen,
1931                      weak_factory_.GetWeakPtr(), snapshot_id),
1932       TimeDelta::FromSecondsD(1. / 6));
1933 #else
1934   WindowSnapshotReachedScreen(snapshot_id);
1935 #endif
1936 }
1937 
OnFirstVisuallyNonEmptyPaint()1938 void RenderWidgetHostImpl::OnFirstVisuallyNonEmptyPaint() {
1939   if (owner_delegate_)
1940     owner_delegate_->RenderWidgetDidFirstVisuallyNonEmptyPaint();
1941 }
1942 
RendererExited()1943 void RenderWidgetHostImpl::RendererExited() {
1944   if (!renderer_initialized_)
1945     return;
1946 
1947   // Clearing this flag causes us to re-create the renderer when recovering
1948   // from a crashed renderer.
1949   renderer_initialized_ = false;
1950 
1951   // After the renderer crashes, the view is destroyed and so the
1952   // RenderWidgetHost cannot track its visibility anymore. We assume such
1953   // RenderWidgetHost to be invisible for the sake of internal accounting - be
1954   // careful about changing this - see http://crbug.com/401859 and
1955   // http://crbug.com/522795.
1956   //
1957   // We need to at least make sure that the RenderProcessHost is notified about
1958   // the |is_hidden_| change, so that the renderer will have correct visibility
1959   // set when respawned.
1960   if (!is_hidden_) {
1961     is_hidden_ = true;
1962     if (!destroyed_)
1963       process_->UpdateClientPriority(this);
1964   }
1965 
1966   if (view_) {
1967     view_->RenderProcessGone();
1968     SetView(nullptr);  // The View should be deleted by RenderProcessGone.
1969   }
1970 }
1971 
ResetStateForCreatedRenderWidget(const VisualProperties & initial_props)1972 void RenderWidgetHostImpl::ResetStateForCreatedRenderWidget(
1973     const VisualProperties& initial_props) {
1974   // When the RenderWidget was destroyed, the ack may never come back. Don't
1975   // let that prevent us from speaking to the next RenderWidget.
1976   waiting_for_screen_rects_ack_ = false;
1977 
1978   visual_properties_ack_pending_ =
1979       DoesVisualPropertiesNeedAck(nullptr, initial_props);
1980   old_visual_properties_ = std::make_unique<VisualProperties>(initial_props);
1981 
1982   // Reconstruct the input router to ensure that it has fresh state for a new
1983   // RenderWidget. Otherwise it may be stuck waiting for the old renderer to ack
1984   // an event. (In particular, the above call to view_->RenderProcessGone() will
1985   // destroy the aura window, which may dispatch a synthetic mouse move.)
1986   //
1987   // This also stops the event ack timeout to ensure the hung renderer mechanism
1988   // is working properly.
1989   SetupInputRouter();
1990 
1991   frame_token_message_queue_->Reset();
1992 }
1993 
UpdateTextDirection(base::i18n::TextDirection direction)1994 void RenderWidgetHostImpl::UpdateTextDirection(
1995     base::i18n::TextDirection direction) {
1996   text_direction_updated_ = true;
1997   text_direction_ = direction;
1998 }
1999 
NotifyTextDirection()2000 void RenderWidgetHostImpl::NotifyTextDirection() {
2001   if (!text_direction_updated_)
2002     return;
2003   Send(new WidgetMsg_SetTextDirection(GetRoutingID(), text_direction_));
2004   text_direction_updated_ = false;
2005 }
2006 
ImeSetComposition(const base::string16 & text,const std::vector<ui::ImeTextSpan> & ime_text_spans,const gfx::Range & replacement_range,int selection_start,int selection_end)2007 void RenderWidgetHostImpl::ImeSetComposition(
2008     const base::string16& text,
2009     const std::vector<ui::ImeTextSpan>& ime_text_spans,
2010     const gfx::Range& replacement_range,
2011     int selection_start,
2012     int selection_end) {
2013   GetWidgetInputHandler()->ImeSetComposition(
2014       text, ime_text_spans, replacement_range, selection_start, selection_end);
2015 #if defined(OS_ANDROID)
2016   for (auto& observer : ime_input_event_observers_) {
2017     observer.OnImeSetComposingTextEvent(text);
2018   }
2019 #endif
2020 }
2021 
ImeCommitText(const base::string16 & text,const std::vector<ui::ImeTextSpan> & ime_text_spans,const gfx::Range & replacement_range,int relative_cursor_pos)2022 void RenderWidgetHostImpl::ImeCommitText(
2023     const base::string16& text,
2024     const std::vector<ui::ImeTextSpan>& ime_text_spans,
2025     const gfx::Range& replacement_range,
2026     int relative_cursor_pos) {
2027   GetWidgetInputHandler()->ImeCommitText(text, ime_text_spans,
2028                                          replacement_range, relative_cursor_pos,
2029                                          base::OnceClosure());
2030 #if defined(OS_ANDROID)
2031   for (auto& observer : ime_input_event_observers_) {
2032     observer.OnImeTextCommittedEvent(text);
2033   }
2034 #endif
2035 }
2036 
ImeFinishComposingText(bool keep_selection)2037 void RenderWidgetHostImpl::ImeFinishComposingText(bool keep_selection) {
2038   GetWidgetInputHandler()->ImeFinishComposingText(keep_selection);
2039 #if defined(OS_ANDROID)
2040   for (auto& observer : ime_input_event_observers_) {
2041     observer.OnImeFinishComposingTextEvent();
2042   }
2043 #endif
2044 }
2045 
ImeCancelComposition()2046 void RenderWidgetHostImpl::ImeCancelComposition() {
2047   GetWidgetInputHandler()->ImeSetComposition(base::string16(),
2048                                              std::vector<ui::ImeTextSpan>(),
2049                                              gfx::Range::InvalidRange(), 0, 0);
2050 }
2051 
RejectMouseLockOrUnlockIfNecessary(blink::mojom::PointerLockResult reason)2052 void RenderWidgetHostImpl::RejectMouseLockOrUnlockIfNecessary(
2053     blink::mojom::PointerLockResult reason) {
2054   DCHECK(!pending_mouse_lock_request_ || !IsMouseLocked());
2055   DCHECK(reason != blink::mojom::PointerLockResult::kSuccess);
2056   if (pending_mouse_lock_request_) {
2057     DCHECK(request_mouse_callback_);
2058     pending_mouse_lock_request_ = false;
2059     mouse_lock_raw_movement_ = false;
2060     std::move(request_mouse_callback_).Run(reason);
2061 
2062   } else if (IsMouseLocked()) {
2063     view_->UnlockMouse();
2064   }
2065 }
2066 
IsKeyboardLocked() const2067 bool RenderWidgetHostImpl::IsKeyboardLocked() const {
2068   return view_ ? view_->IsKeyboardLocked() : false;
2069 }
2070 
GetContentRenderingTimeoutFrom(RenderWidgetHostImpl * other)2071 void RenderWidgetHostImpl::GetContentRenderingTimeoutFrom(
2072     RenderWidgetHostImpl* other) {
2073   if (other->new_content_rendering_timeout_ &&
2074       other->new_content_rendering_timeout_->IsRunning()) {
2075     new_content_rendering_timeout_->Start(
2076         other->new_content_rendering_timeout_->GetCurrentDelay());
2077   }
2078 }
2079 
OnMouseEventAck(const MouseEventWithLatencyInfo & mouse_event,InputEventAckSource ack_source,InputEventAckState ack_result)2080 void RenderWidgetHostImpl::OnMouseEventAck(
2081     const MouseEventWithLatencyInfo& mouse_event,
2082     InputEventAckSource ack_source,
2083     InputEventAckState ack_result) {
2084   latency_tracker_.OnInputEventAck(mouse_event.event, &mouse_event.latency,
2085                                    ack_result);
2086   for (auto& input_event_observer : input_event_observers_)
2087     input_event_observer.OnInputEventAck(ack_source, ack_result,
2088                                          mouse_event.event);
2089 
2090   // Give the delegate the ability to handle a mouse event that wasn't consumed
2091   // by the renderer. eg. Back/Forward mouse buttons.
2092   if (delegate_ && ack_result != INPUT_EVENT_ACK_STATE_CONSUMED && !is_hidden())
2093     delegate_->HandleMouseEvent(mouse_event.event);
2094 }
2095 
IsMouseLocked() const2096 bool RenderWidgetHostImpl::IsMouseLocked() const {
2097   return view_ ? view_->IsMouseLocked() : false;
2098 }
2099 
SetVisualPropertiesFromParentFrame(float page_scale_factor,bool is_pinch_gesture_active,const gfx::Size & visible_viewport_size,const gfx::Rect & compositor_viewport)2100 void RenderWidgetHostImpl::SetVisualPropertiesFromParentFrame(
2101     float page_scale_factor,
2102     bool is_pinch_gesture_active,
2103     const gfx::Size& visible_viewport_size,
2104     const gfx::Rect& compositor_viewport) {
2105   properties_from_parent_local_root_.page_scale_factor = page_scale_factor;
2106   properties_from_parent_local_root_.is_pinch_gesture_active =
2107       is_pinch_gesture_active;
2108   properties_from_parent_local_root_.visible_viewport_size =
2109       visible_viewport_size;
2110   properties_from_parent_local_root_.compositor_viewport = compositor_viewport;
2111 }
2112 
SetAutoResize(bool enable,const gfx::Size & min_size,const gfx::Size & max_size)2113 void RenderWidgetHostImpl::SetAutoResize(bool enable,
2114                                          const gfx::Size& min_size,
2115                                          const gfx::Size& max_size) {
2116   auto_resize_enabled_ = enable;
2117   min_size_for_auto_resize_ = min_size;
2118   max_size_for_auto_resize_ = max_size;
2119 }
2120 
Destroy(bool also_delete)2121 void RenderWidgetHostImpl::Destroy(bool also_delete) {
2122   DCHECK(!destroyed_);
2123   destroyed_ = true;
2124 
2125   for (auto& observer : observers_)
2126     observer.RenderWidgetHostDestroyed(this);
2127   NotificationService::current()->Notify(
2128       NOTIFICATION_RENDER_WIDGET_HOST_DESTROYED, Source<RenderWidgetHost>(this),
2129       NotificationService::NoDetails());
2130 
2131   // Tell the view to die.
2132   // Note that in the process of the view shutting down, it can call a ton
2133   // of other messages on us.  So if you do any other deinitialization here,
2134   // do it after this call to view_->Destroy().
2135   if (view_) {
2136     view_->Destroy();
2137     view_.reset();
2138   }
2139 
2140   render_process_blocked_state_changed_subscription_.reset();
2141   process_->RemovePriorityClient(this);
2142   process_->RemoveObserver(this);
2143   process_->RemoveRoute(routing_id_);
2144   g_routing_id_widget_map.Get().erase(
2145       RenderWidgetHostID(process_->GetID(), routing_id_));
2146 
2147   // The |delegate_| may have been destroyed (or is in the process of being
2148   // destroyed) and detached first.
2149   if (delegate_)
2150     delegate_->RenderWidgetDeleted(this);
2151 
2152   if (also_delete) {
2153     CHECK(!owner_delegate_);
2154     delete this;
2155   }
2156 }
2157 
OnInputEventAckTimeout()2158 void RenderWidgetHostImpl::OnInputEventAckTimeout() {
2159   RendererIsUnresponsive(base::BindRepeating(
2160       &RenderWidgetHostImpl::RestartInputEventAckTimeoutIfNecessary,
2161       weak_factory_.GetWeakPtr()));
2162 }
2163 
RendererIsUnresponsive(base::RepeatingClosure restart_hang_monitor_timeout)2164 void RenderWidgetHostImpl::RendererIsUnresponsive(
2165     base::RepeatingClosure restart_hang_monitor_timeout) {
2166   NotificationService::current()->Notify(
2167       NOTIFICATION_RENDER_WIDGET_HOST_HANG,
2168       Source<RenderWidgetHost>(this),
2169       NotificationService::NoDetails());
2170   is_unresponsive_ = true;
2171 
2172   if (delegate_) {
2173     delegate_->RendererUnresponsive(this,
2174                                     std::move(restart_hang_monitor_timeout));
2175   }
2176 
2177   // Do not add code after this since the Delegate may delete this
2178   // RenderWidgetHostImpl in RendererUnresponsive.
2179 }
2180 
RendererIsResponsive()2181 void RenderWidgetHostImpl::RendererIsResponsive() {
2182   if (is_unresponsive_) {
2183     is_unresponsive_ = false;
2184     if (delegate_)
2185       delegate_->RendererResponsive(this);
2186   }
2187 }
2188 
ClearDisplayedGraphics()2189 void RenderWidgetHostImpl::ClearDisplayedGraphics() {
2190   NotifyNewContentRenderingTimeoutForTesting();
2191   if (view_)
2192     view_->ResetFallbackToFirstNavigationSurface();
2193 }
2194 
OnKeyboardEventAck(const NativeWebKeyboardEventWithLatencyInfo & event,InputEventAckSource ack_source,InputEventAckState ack_result)2195 void RenderWidgetHostImpl::OnKeyboardEventAck(
2196     const NativeWebKeyboardEventWithLatencyInfo& event,
2197     InputEventAckSource ack_source,
2198     InputEventAckState ack_result) {
2199   latency_tracker_.OnInputEventAck(event.event, &event.latency, ack_result);
2200   for (auto& input_event_observer : input_event_observers_)
2201     input_event_observer.OnInputEventAck(ack_source, ack_result, event.event);
2202 
2203   bool processed = (INPUT_EVENT_ACK_STATE_CONSUMED == ack_result);
2204 
2205   // The view may be destroyed by the time we get this ack, as is the case with
2206   // portal activations.
2207   if (!processed && GetView())
2208     processed = GetView()->OnUnconsumedKeyboardEventAck(event);
2209 
2210   // We only send unprocessed key event upwards if we are not hidden,
2211   // because the user has moved away from us and no longer expect any effect
2212   // of this key event.
2213   if (delegate_ && !processed && !is_hidden() && !event.event.skip_in_browser)
2214     delegate_->HandleKeyboardEvent(event.event);
2215   // WARNING: This RenderWidgetHostImpl can be deallocated at this point
2216   // (i.e.  in the case of Ctrl+W, where the call to
2217   // HandleKeyboardEvent destroys this RenderWidgetHostImpl).
2218 }
2219 
OnClose()2220 void RenderWidgetHostImpl::OnClose() {
2221   if (owner_delegate_) {
2222     owner_delegate_->RenderWidgetDidClose();
2223   } else {
2224     ShutdownAndDestroyWidget(true);
2225   }
2226 }
2227 
OnSetTooltipText(const base::string16 & tooltip_text,base::i18n::TextDirection text_direction_hint)2228 void RenderWidgetHostImpl::OnSetTooltipText(
2229     const base::string16& tooltip_text,
2230     base::i18n::TextDirection text_direction_hint) {
2231   if (!GetView())
2232     return;
2233 
2234   // First, add directionality marks around tooltip text if necessary.
2235   // A naive solution would be to simply always wrap the text. However, on
2236   // windows, Unicode directional embedding characters can't be displayed on
2237   // systems that lack RTL fonts and are instead displayed as empty squares.
2238   //
2239   // To get around this we only wrap the string when we deem it necessary i.e.
2240   // when the locale direction is different than the tooltip direction hint.
2241   //
2242   // Currently, we use element's directionality as the tooltip direction hint.
2243   // An alternate solution would be to set the overall directionality based on
2244   // trying to detect the directionality from the tooltip text rather than the
2245   // element direction.  One could argue that would be a preferable solution
2246   // but we use the current approach to match Fx & IE's behavior.
2247   base::string16 wrapped_tooltip_text = tooltip_text;
2248   if (!tooltip_text.empty()) {
2249     if (text_direction_hint == base::i18n::LEFT_TO_RIGHT) {
2250       // Force the tooltip to have LTR directionality.
2251       wrapped_tooltip_text =
2252           base::i18n::GetDisplayStringInLTRDirectionality(wrapped_tooltip_text);
2253     } else if (text_direction_hint == base::i18n::RIGHT_TO_LEFT &&
2254                !base::i18n::IsRTL()) {
2255       // Force the tooltip to have RTL directionality.
2256       base::i18n::WrapStringWithRTLFormatting(&wrapped_tooltip_text);
2257     }
2258   }
2259   view_->SetTooltipText(wrapped_tooltip_text);
2260 }
2261 
OnUpdateScreenRectsAck()2262 void RenderWidgetHostImpl::OnUpdateScreenRectsAck() {
2263   waiting_for_screen_rects_ack_ = false;
2264   if (!view_)
2265     return;
2266 
2267   if (view_->GetViewBounds() == last_view_screen_rect_ &&
2268       view_->GetBoundsInRootWindow() == last_window_screen_rect_) {
2269     return;
2270   }
2271 
2272   SendScreenRects();
2273 }
2274 
OnRequestSetBounds(const gfx::Rect & bounds)2275 void RenderWidgetHostImpl::OnRequestSetBounds(const gfx::Rect& bounds) {
2276   if (owner_delegate_) {
2277     owner_delegate_->RequestSetBounds(bounds);
2278   } else if (view_) {
2279     view_->SetBounds(bounds);
2280   }
2281   Send(new WidgetMsg_SetBounds_ACK(routing_id_));
2282 }
2283 
OnLocalSurfaceIdChanged(const cc::RenderFrameMetadata & metadata)2284 void RenderWidgetHostImpl::OnLocalSurfaceIdChanged(
2285     const cc::RenderFrameMetadata& metadata) {
2286   TRACE_EVENT_WITH_FLOW1(
2287       "renderer_host,disabled-by-default-viz.surface_id_flow",
2288       "RenderWidgetHostImpl::OnLocalSurfaceIdChanged",
2289       metadata.local_surface_id_allocation &&
2290               metadata.local_surface_id_allocation->IsValid()
2291           ? metadata.local_surface_id_allocation->local_surface_id()
2292                     .submission_trace_id() +
2293                 metadata.local_surface_id_allocation->local_surface_id()
2294                     .embed_trace_id()
2295           : 0,
2296       TRACE_EVENT_FLAG_FLOW_IN, "local_surface_id_allocation",
2297       metadata.local_surface_id_allocation
2298           ? metadata.local_surface_id_allocation->ToString()
2299           : "null");
2300 
2301   // Update our knowledge of the RenderWidget's size.
2302   DCHECK(!metadata.viewport_size_in_pixels.IsEmpty());
2303 
2304   visual_properties_ack_pending_ = false;
2305 
2306   NotificationService::current()->Notify(
2307       NOTIFICATION_RENDER_WIDGET_HOST_DID_UPDATE_VISUAL_PROPERTIES,
2308       Source<RenderWidgetHost>(this), NotificationService::NoDetails());
2309 
2310   if (!view_)
2311     return;
2312 
2313   viz::ScopedSurfaceIdAllocator scoped_allocator =
2314       view_->DidUpdateVisualProperties(metadata);
2315   base::AutoReset<bool> auto_reset(&surface_id_allocation_suppressed_, true);
2316 
2317   if (auto_resize_enabled_ && delegate_) {
2318     // TODO(fsamuel): The fact that we translate the viewport_size from pixels
2319     // to DIP is concerning. This could result in invariants violations.
2320     gfx::Size viewport_size_in_dip = gfx::ScaleToCeiledSize(
2321         metadata.viewport_size_in_pixels, 1.f / metadata.device_scale_factor);
2322     delegate_->ResizeDueToAutoResize(this, viewport_size_in_dip);
2323   }
2324 }
2325 
2326 // static
DidVisualPropertiesSizeChange(const VisualProperties & old_visual_properties,const VisualProperties & new_visual_properties)2327 bool RenderWidgetHostImpl::DidVisualPropertiesSizeChange(
2328     const VisualProperties& old_visual_properties,
2329     const VisualProperties& new_visual_properties) {
2330   return old_visual_properties.auto_resize_enabled !=
2331              new_visual_properties.auto_resize_enabled ||
2332          (old_visual_properties.auto_resize_enabled &&
2333           (old_visual_properties.min_size_for_auto_resize !=
2334                new_visual_properties.min_size_for_auto_resize ||
2335            old_visual_properties.max_size_for_auto_resize !=
2336                new_visual_properties.max_size_for_auto_resize)) ||
2337          (!old_visual_properties.auto_resize_enabled &&
2338           (old_visual_properties.new_size != new_visual_properties.new_size ||
2339            (old_visual_properties.compositor_viewport_pixel_rect.IsEmpty() &&
2340             !new_visual_properties.compositor_viewport_pixel_rect.IsEmpty())));
2341 }
2342 
2343 // static
DoesVisualPropertiesNeedAck(const std::unique_ptr<VisualProperties> & old_visual_properties,const VisualProperties & new_visual_properties)2344 bool RenderWidgetHostImpl::DoesVisualPropertiesNeedAck(
2345     const std::unique_ptr<VisualProperties>& old_visual_properties,
2346     const VisualProperties& new_visual_properties) {
2347   // We should throttle sending updated VisualProperties to the renderer to
2348   // the rate of commit. This ensures we don't overwhelm the renderer with
2349   // visual updates faster than it can keep up.  |needs_ack| corresponds to
2350   // cases where a commit is expected.
2351   bool is_acking_applicable =
2352       g_check_for_pending_visual_properties_ack &&
2353       !new_visual_properties.auto_resize_enabled &&
2354       !new_visual_properties.new_size.IsEmpty() &&
2355       !new_visual_properties.compositor_viewport_pixel_rect.IsEmpty() &&
2356       new_visual_properties.local_surface_id_allocation;
2357 
2358   // If acking is applicable, then check if there has been an
2359   // |old_visual_properties| stored which would indicate an update has been
2360   // sent. If so, then acking is defined by size changing.
2361   return is_acking_applicable &&
2362          (!old_visual_properties ||
2363           DidVisualPropertiesSizeChange(*old_visual_properties,
2364                                         new_visual_properties));
2365 }
2366 
2367 // static
StoredVisualPropertiesNeedsUpdate(const std::unique_ptr<VisualProperties> & old_visual_properties,const VisualProperties & new_visual_properties)2368 bool RenderWidgetHostImpl::StoredVisualPropertiesNeedsUpdate(
2369     const std::unique_ptr<VisualProperties>& old_visual_properties,
2370     const VisualProperties& new_visual_properties) {
2371   if (!old_visual_properties)
2372     return true;
2373 
2374   const bool size_changed = DidVisualPropertiesSizeChange(
2375       *old_visual_properties, new_visual_properties);
2376 
2377   // Hold on the the LocalSurfaceIdAllocation in a local variable otherwise the
2378   // LocalSurfaceId may become invalid when used later.
2379   viz::LocalSurfaceIdAllocation old_parent_local_surface_id_allocation =
2380       old_visual_properties->local_surface_id_allocation.value_or(
2381           viz::LocalSurfaceIdAllocation());
2382   viz::LocalSurfaceIdAllocation new_parent_local_surface_id_allocation =
2383       new_visual_properties.local_surface_id_allocation.value_or(
2384           viz::LocalSurfaceIdAllocation());
2385 
2386   const viz::LocalSurfaceId& old_parent_local_surface_id =
2387       old_parent_local_surface_id_allocation.local_surface_id();
2388   const viz::LocalSurfaceId& new_parent_local_surface_id =
2389       new_parent_local_surface_id_allocation.local_surface_id();
2390 
2391   const bool parent_local_surface_id_changed =
2392       old_parent_local_surface_id.parent_sequence_number() !=
2393           new_parent_local_surface_id.parent_sequence_number() ||
2394       old_parent_local_surface_id.embed_token() !=
2395           new_parent_local_surface_id.embed_token();
2396 
2397   const bool zoom_changed =
2398       old_visual_properties->zoom_level != new_visual_properties.zoom_level;
2399 
2400   return zoom_changed || size_changed || parent_local_surface_id_changed ||
2401          old_visual_properties->screen_info !=
2402              new_visual_properties.screen_info ||
2403          old_visual_properties->compositor_viewport_pixel_rect !=
2404              new_visual_properties.compositor_viewport_pixel_rect ||
2405          old_visual_properties->compositor_viewport_pixel_rect !=
2406              new_visual_properties.compositor_viewport_pixel_rect ||
2407          old_visual_properties->is_fullscreen_granted !=
2408              new_visual_properties.is_fullscreen_granted ||
2409          old_visual_properties->display_mode !=
2410              new_visual_properties.display_mode ||
2411          old_visual_properties->browser_controls_params !=
2412              new_visual_properties.browser_controls_params ||
2413          old_visual_properties->visible_viewport_size !=
2414              new_visual_properties.visible_viewport_size ||
2415          old_visual_properties->capture_sequence_number !=
2416              new_visual_properties.capture_sequence_number ||
2417          old_visual_properties->page_scale_factor !=
2418              new_visual_properties.page_scale_factor ||
2419          old_visual_properties->is_pinch_gesture_active !=
2420              new_visual_properties.is_pinch_gesture_active;
2421 }
2422 
OnSetCursor(const WebCursor & cursor)2423 void RenderWidgetHostImpl::OnSetCursor(const WebCursor& cursor) {
2424   SetCursor(cursor);
2425 }
2426 
OnAutoscrollStart(const gfx::PointF & position)2427 void RenderWidgetHostImpl::OnAutoscrollStart(const gfx::PointF& position) {
2428   GetView()->OnAutoscrollStart();
2429   sent_autoscroll_scroll_begin_ = false;
2430   autoscroll_in_progress_ = true;
2431   delegate()->GetInputEventRouter()->SetAutoScrollInProgress(
2432       autoscroll_in_progress_);
2433   autoscroll_start_position_ = position;
2434 }
2435 
OnAutoscrollFling(const gfx::Vector2dF & velocity)2436 void RenderWidgetHostImpl::OnAutoscrollFling(const gfx::Vector2dF& velocity) {
2437   DCHECK(autoscroll_in_progress_);
2438   if (!sent_autoscroll_scroll_begin_ && velocity != gfx::Vector2dF()) {
2439     // Send a GSB event with valid delta hints.
2440     WebGestureEvent scroll_begin = SyntheticWebGestureEventBuilder::Build(
2441         WebInputEvent::kGestureScrollBegin,
2442         blink::WebGestureDevice::kSyntheticAutoscroll);
2443     scroll_begin.SetPositionInWidget(autoscroll_start_position_);
2444     scroll_begin.data.scroll_begin.delta_x_hint = velocity.x();
2445     scroll_begin.data.scroll_begin.delta_y_hint = velocity.y();
2446 
2447     ForwardGestureEventWithLatencyInfo(
2448         scroll_begin, ui::LatencyInfo(ui::SourceEventType::OTHER));
2449     sent_autoscroll_scroll_begin_ = true;
2450   }
2451 
2452   WebGestureEvent event = SyntheticWebGestureEventBuilder::Build(
2453       WebInputEvent::kGestureFlingStart,
2454       blink::WebGestureDevice::kSyntheticAutoscroll);
2455   event.SetPositionInWidget(autoscroll_start_position_);
2456   event.data.fling_start.velocity_x = velocity.x();
2457   event.data.fling_start.velocity_y = velocity.y();
2458 
2459   ForwardGestureEventWithLatencyInfo(
2460       event, ui::LatencyInfo(ui::SourceEventType::OTHER));
2461 }
2462 
OnAutoscrollEnd()2463 void RenderWidgetHostImpl::OnAutoscrollEnd() {
2464   autoscroll_in_progress_ = false;
2465 
2466   delegate()->GetInputEventRouter()->SetAutoScrollInProgress(
2467       autoscroll_in_progress_);
2468   // Don't send a GFC if no GSB is sent.
2469   if (!sent_autoscroll_scroll_begin_)
2470     return;
2471 
2472   sent_autoscroll_scroll_begin_ = false;
2473   WebGestureEvent cancel_event = SyntheticWebGestureEventBuilder::Build(
2474       WebInputEvent::kGestureFlingCancel,
2475       blink::WebGestureDevice::kSyntheticAutoscroll);
2476   cancel_event.data.fling_cancel.prevent_boosting = true;
2477   cancel_event.SetPositionInWidget(autoscroll_start_position_);
2478 
2479   ForwardGestureEventWithLatencyInfo(
2480       cancel_event, ui::LatencyInfo(ui::SourceEventType::OTHER));
2481 }
2482 
IsAutoscrollInProgress()2483 bool RenderWidgetHostImpl::IsAutoscrollInProgress() {
2484   return autoscroll_in_progress_;
2485 }
2486 
GetTouchEmulator()2487 TouchEmulator* RenderWidgetHostImpl::GetTouchEmulator() {
2488   if (!delegate_ || !delegate_->GetInputEventRouter())
2489     return nullptr;
2490 
2491   return delegate_->GetInputEventRouter()->GetTouchEmulator();
2492 }
2493 
GetExistingTouchEmulator()2494 TouchEmulator* RenderWidgetHostImpl::GetExistingTouchEmulator() {
2495   if (!delegate_ || !delegate_->GetInputEventRouter() ||
2496       !delegate_->GetInputEventRouter()->has_touch_emulator()) {
2497     return nullptr;
2498   }
2499 
2500   return delegate_->GetInputEventRouter()->GetTouchEmulator();
2501 }
2502 
OnTextInputStateChanged(const TextInputState & params)2503 void RenderWidgetHostImpl::OnTextInputStateChanged(
2504     const TextInputState& params) {
2505   if (view_)
2506     view_->TextInputStateChanged(params);
2507 }
2508 
OnImeCompositionRangeChanged(const gfx::Range & range,const std::vector<gfx::Rect> & character_bounds)2509 void RenderWidgetHostImpl::OnImeCompositionRangeChanged(
2510     const gfx::Range& range,
2511     const std::vector<gfx::Rect>& character_bounds) {
2512   if (view_)
2513     view_->ImeCompositionRangeChanged(range, character_bounds);
2514 }
2515 
OnImeCancelComposition()2516 void RenderWidgetHostImpl::OnImeCancelComposition() {
2517   if (view_)
2518     view_->ImeCancelComposition();
2519 }
2520 
IsWheelScrollInProgress()2521 bool RenderWidgetHostImpl::IsWheelScrollInProgress() {
2522   return is_in_gesture_scroll_[static_cast<int>(
2523       blink::WebGestureDevice::kTouchpad)];
2524 }
2525 
SetMouseCapture(bool capture)2526 void RenderWidgetHostImpl::SetMouseCapture(bool capture) {
2527   if (!delegate_ || !delegate_->GetInputEventRouter())
2528     return;
2529 
2530   delegate_->GetInputEventRouter()->SetMouseCaptureTarget(GetView(), capture);
2531 }
2532 
RequestMouseLock(bool from_user_gesture,bool privileged,bool unadjusted_movement,InputRouterImpl::RequestMouseLockCallback response)2533 void RenderWidgetHostImpl::RequestMouseLock(
2534     bool from_user_gesture,
2535     bool privileged,
2536     bool unadjusted_movement,
2537     InputRouterImpl::RequestMouseLockCallback response) {
2538   if (pending_mouse_lock_request_) {
2539     std::move(response).Run(blink::mojom::PointerLockResult::kAlreadyLocked);
2540     return;
2541   }
2542 
2543   if (!view_ || !view_->HasFocus()) {
2544     std::move(response).Run(blink::mojom::PointerLockResult::kWrongDocument);
2545     return;
2546   }
2547 
2548   request_mouse_callback_ = std::move(response);
2549 
2550   pending_mouse_lock_request_ = true;
2551   mouse_lock_raw_movement_ = unadjusted_movement;
2552   if (delegate_) {
2553     delegate_->RequestToLockMouse(this, from_user_gesture,
2554                                   is_last_unlocked_by_target_,
2555                                   privileged && allow_privileged_mouse_lock_);
2556     // We need to reset |is_last_unlocked_by_target_| here as we don't know
2557     // request source in |LostMouseLock()|.
2558     is_last_unlocked_by_target_ = false;
2559     return;
2560   }
2561 
2562   // Directly reject or approve the mouse lock based on privilege.
2563   if (allow_privileged_mouse_lock_ && privileged)
2564     GotResponseToLockMouseRequest(blink::mojom::PointerLockResult::kSuccess);
2565   else
2566     GotResponseToLockMouseRequest(
2567         blink::mojom::PointerLockResult::kPermissionDenied);
2568 }
2569 
RequestMouseLockChange(bool unadjusted_movement,InputRouterImpl::RequestMouseLockCallback response)2570 void RenderWidgetHostImpl::RequestMouseLockChange(
2571     bool unadjusted_movement,
2572     InputRouterImpl::RequestMouseLockCallback response) {
2573   if (pending_mouse_lock_request_) {
2574     std::move(response).Run(blink::mojom::PointerLockResult::kAlreadyLocked);
2575     return;
2576   }
2577 
2578   if (!view_ || !view_->HasFocus()) {
2579     std::move(response).Run(blink::mojom::PointerLockResult::kWrongDocument);
2580     return;
2581   }
2582 
2583   std::move(response).Run(view_->ChangeMouseLock(unadjusted_movement));
2584 }
2585 
UnlockMouse()2586 void RenderWidgetHostImpl::UnlockMouse() {
2587   // Got unlock request from renderer. Will update |is_last_unlocked_by_target_|
2588   // for silent re-lock.
2589   const bool was_mouse_locked = !pending_mouse_lock_request_ && IsMouseLocked();
2590   RejectMouseLockOrUnlockIfNecessary(
2591       blink::mojom::PointerLockResult::kUserRejected);
2592   if (was_mouse_locked)
2593     is_last_unlocked_by_target_ = true;
2594 }
2595 
FallbackCursorModeLockCursor(bool left,bool right,bool up,bool down)2596 void RenderWidgetHostImpl::FallbackCursorModeLockCursor(bool left,
2597                                                         bool right,
2598                                                         bool up,
2599                                                         bool down) {
2600   GetView()->FallbackCursorModeLockCursor(left, right, up, down);
2601 }
2602 
FallbackCursorModeSetCursorVisibility(bool visible)2603 void RenderWidgetHostImpl::FallbackCursorModeSetCursorVisibility(bool visible) {
2604   GetView()->FallbackCursorModeSetCursorVisibility(visible);
2605 }
2606 
OnInvalidFrameToken(uint32_t frame_token)2607 void RenderWidgetHostImpl::OnInvalidFrameToken(uint32_t frame_token) {
2608   bad_message::ReceivedBadMessage(GetProcess(),
2609                                   bad_message::RWH_INVALID_FRAME_TOKEN);
2610 }
2611 
OnMessageDispatchError(const IPC::Message & message)2612 void RenderWidgetHostImpl::OnMessageDispatchError(const IPC::Message& message) {
2613   RenderProcessHost* rph = GetProcess();
2614   rph->OnBadMessageReceived(message);
2615 }
2616 
OnProcessSwapMessage(const IPC::Message & message)2617 void RenderWidgetHostImpl::OnProcessSwapMessage(const IPC::Message& message) {
2618   RenderProcessHost* rph = GetProcess();
2619   rph->OnMessageReceived(message);
2620 }
2621 
RequestKeyboardLock(base::Optional<base::flat_set<ui::DomCode>> codes)2622 bool RenderWidgetHostImpl::RequestKeyboardLock(
2623     base::Optional<base::flat_set<ui::DomCode>> codes) {
2624   if (!delegate_) {
2625     CancelKeyboardLock();
2626     return false;
2627   }
2628 
2629   DCHECK(!codes.has_value() || !codes.value().empty());
2630   keyboard_keys_to_lock_ = std::move(codes);
2631   keyboard_lock_requested_ = true;
2632 
2633   const bool esc_requested =
2634       !keyboard_keys_to_lock_.has_value() ||
2635       base::Contains(keyboard_keys_to_lock_.value(), ui::DomCode::ESCAPE);
2636 
2637   if (!delegate_->RequestKeyboardLock(this, esc_requested)) {
2638     CancelKeyboardLock();
2639     return false;
2640   }
2641 
2642   return true;
2643 }
2644 
CancelKeyboardLock()2645 void RenderWidgetHostImpl::CancelKeyboardLock() {
2646   if (delegate_)
2647     delegate_->CancelKeyboardLock(this);
2648 
2649   UnlockKeyboard();
2650 
2651   keyboard_lock_allowed_ = false;
2652   keyboard_lock_requested_ = false;
2653   keyboard_keys_to_lock_.reset();
2654 }
2655 
2656 base::flat_map<std::string, std::string>
GetKeyboardLayoutMap()2657 RenderWidgetHostImpl::GetKeyboardLayoutMap() {
2658   if (!view_)
2659     return {};
2660   return view_->GetKeyboardLayoutMap();
2661 }
2662 
KeyPressListenersHandleEvent(const NativeWebKeyboardEvent & event)2663 bool RenderWidgetHostImpl::KeyPressListenersHandleEvent(
2664     const NativeWebKeyboardEvent& event) {
2665   if (event.skip_in_browser || event.GetType() != WebKeyboardEvent::kRawKeyDown)
2666     return false;
2667 
2668   for (size_t i = 0; i < key_press_event_callbacks_.size(); i++) {
2669     size_t original_size = key_press_event_callbacks_.size();
2670     if (key_press_event_callbacks_[i].Run(event))
2671       return true;
2672 
2673     // Check whether the callback that just ran removed itself, in which case
2674     // the iterator needs to be decremented to properly account for the removal.
2675     size_t current_size = key_press_event_callbacks_.size();
2676     if (current_size != original_size) {
2677       DCHECK_EQ(original_size - 1, current_size);
2678       --i;
2679     }
2680   }
2681 
2682   return false;
2683 }
2684 
FilterInputEvent(const blink::WebInputEvent & event,const ui::LatencyInfo & latency_info)2685 InputEventAckState RenderWidgetHostImpl::FilterInputEvent(
2686     const blink::WebInputEvent& event, const ui::LatencyInfo& latency_info) {
2687   // Don't ignore touch cancel events, since they may be sent while input
2688   // events are being ignored in order to keep the renderer from getting
2689   // confused about how many touches are active.
2690   if (IsIgnoringInputEvents() && event.GetType() != WebInputEvent::kTouchCancel)
2691     return INPUT_EVENT_ACK_STATE_NO_CONSUMER_EXISTS;
2692 
2693   if (!process_->IsInitializedAndNotDead())
2694     return INPUT_EVENT_ACK_STATE_UNKNOWN;
2695 
2696   if (delegate_) {
2697     if (event.GetType() == WebInputEvent::kMouseDown ||
2698         event.GetType() == WebInputEvent::kTouchStart ||
2699         event.GetType() == WebInputEvent::kGestureTap) {
2700       delegate_->FocusOwningWebContents(this);
2701     }
2702     delegate_->DidReceiveInputEvent(this, event.GetType());
2703   }
2704 
2705   return view_ ? view_->FilterInputEvent(event)
2706                : INPUT_EVENT_ACK_STATE_NOT_CONSUMED;
2707 }
2708 
IncrementInFlightEventCount()2709 void RenderWidgetHostImpl::IncrementInFlightEventCount() {
2710   ++in_flight_event_count_;
2711   if (!is_hidden_)
2712     StartInputEventAckTimeout();
2713 }
2714 
DecrementInFlightEventCount(InputEventAckSource ack_source)2715 void RenderWidgetHostImpl::DecrementInFlightEventCount(
2716     InputEventAckSource ack_source) {
2717   --in_flight_event_count_;
2718   if (in_flight_event_count_ <= 0) {
2719     // Cancel pending hung renderer checks since the renderer is responsive.
2720     StopInputEventAckTimeout();
2721   } else {
2722     // Only restart the hang monitor timer if we got a response from the
2723     // main thread.
2724     if (ack_source == InputEventAckSource::MAIN_THREAD)
2725       RestartInputEventAckTimeoutIfNecessary();
2726   }
2727 }
2728 
OnHasTouchEventHandlers(bool has_handlers)2729 void RenderWidgetHostImpl::OnHasTouchEventHandlers(bool has_handlers) {
2730   input_router_->OnHasTouchEventHandlers(has_handlers);
2731   has_touch_handler_ = has_handlers;
2732 }
2733 
OnIntrinsicSizingInfoChanged(blink::WebIntrinsicSizingInfo info)2734 void RenderWidgetHostImpl::OnIntrinsicSizingInfoChanged(
2735     blink::WebIntrinsicSizingInfo info) {
2736   if (view_)
2737     view_->UpdateIntrinsicSizingInfo(info);
2738 }
2739 
DidOverscroll(const ui::DidOverscrollParams & params)2740 void RenderWidgetHostImpl::DidOverscroll(
2741     const ui::DidOverscrollParams& params) {
2742   if (view_)
2743     view_->DidOverscroll(params);
2744 }
2745 
DidStopFlinging()2746 void RenderWidgetHostImpl::DidStopFlinging() {
2747   is_in_touchpad_gesture_fling_ = false;
2748   if (view_)
2749     view_->DidStopFlinging();
2750 }
2751 
DidStartScrollingViewport()2752 void RenderWidgetHostImpl::DidStartScrollingViewport() {
2753   if (view_)
2754     view_->set_is_currently_scrolling_viewport(true);
2755 }
2756 
AddPendingUserActivation(const WebInputEvent & event)2757 void RenderWidgetHostImpl::AddPendingUserActivation(
2758     const WebInputEvent& event) {
2759   if ((base::FeatureList::IsEnabled(
2760            features::kBrowserVerifiedUserActivationMouse) &&
2761        event.GetType() == WebInputEvent::kMouseDown) ||
2762       (base::FeatureList::IsEnabled(
2763            features::kBrowserVerifiedUserActivationKeyboard) &&
2764        (event.GetType() == WebInputEvent::kKeyDown ||
2765         event.GetType() == WebInputEvent::kRawKeyDown))) {
2766     pending_user_activation_timer_.Start(
2767         FROM_HERE, kActivationNotificationExpireTime,
2768         base::BindOnce(&RenderWidgetHostImpl::ClearPendingUserActivation,
2769                        base::Unretained(this)));
2770     pending_user_activation_counter_++;
2771   }
2772 }
2773 
ClearPendingUserActivation()2774 void RenderWidgetHostImpl::ClearPendingUserActivation() {
2775   pending_user_activation_counter_ = 0;
2776   pending_user_activation_timer_.Stop();
2777 }
2778 
RemovePendingUserActivationIfAvailable()2779 bool RenderWidgetHostImpl::RemovePendingUserActivationIfAvailable() {
2780   if (pending_user_activation_counter_ > 0) {
2781     pending_user_activation_counter_--;
2782     return true;
2783   }
2784   return false;
2785 }
2786 
2787 const mojo::AssociatedRemote<blink::mojom::FrameWidget>&
GetAssociatedFrameWidget()2788 RenderWidgetHostImpl::GetAssociatedFrameWidget() {
2789   return blink_frame_widget_;
2790 }
2791 
DispatchInputEventWithLatencyInfo(const blink::WebInputEvent & event,ui::LatencyInfo * latency)2792 void RenderWidgetHostImpl::DispatchInputEventWithLatencyInfo(
2793     const blink::WebInputEvent& event,
2794     ui::LatencyInfo* latency) {
2795   latency_tracker_.OnInputEvent(event, latency);
2796   AddPendingUserActivation(event);
2797   for (auto& observer : input_event_observers_)
2798     observer.OnInputEvent(event);
2799 }
2800 
OnWheelEventAck(const MouseWheelEventWithLatencyInfo & wheel_event,InputEventAckSource ack_source,InputEventAckState ack_result)2801 void RenderWidgetHostImpl::OnWheelEventAck(
2802     const MouseWheelEventWithLatencyInfo& wheel_event,
2803     InputEventAckSource ack_source,
2804     InputEventAckState ack_result) {
2805   latency_tracker_.OnInputEventAck(wheel_event.event, &wheel_event.latency,
2806                                    ack_result);
2807   for (auto& input_event_observer : input_event_observers_)
2808     input_event_observer.OnInputEventAck(ack_source, ack_result,
2809                                          wheel_event.event);
2810 
2811   if (!is_hidden() && view_) {
2812     if (ack_result != INPUT_EVENT_ACK_STATE_CONSUMED &&
2813         delegate_ && delegate_->HandleWheelEvent(wheel_event.event)) {
2814       ack_result = INPUT_EVENT_ACK_STATE_CONSUMED;
2815     }
2816     view_->WheelEventAck(wheel_event.event, ack_result);
2817   }
2818 }
2819 
OnGestureEventAck(const GestureEventWithLatencyInfo & event,InputEventAckSource ack_source,InputEventAckState ack_result)2820 void RenderWidgetHostImpl::OnGestureEventAck(
2821     const GestureEventWithLatencyInfo& event,
2822     InputEventAckSource ack_source,
2823     InputEventAckState ack_result) {
2824   latency_tracker_.OnInputEventAck(event.event, &event.latency, ack_result);
2825   for (auto& input_event_observer : input_event_observers_)
2826     input_event_observer.OnInputEventAck(ack_source, ack_result, event.event);
2827 
2828   // If the TouchEmulator didn't exist when this GestureEvent was sent, we
2829   // shouldn't create it here.
2830   if (auto* touch_emulator = GetExistingTouchEmulator())
2831     touch_emulator->OnGestureEventAck(event.event, GetView());
2832 
2833   if (view_)
2834     view_->GestureEventAck(event.event, ack_result);
2835 }
2836 
OnTouchEventAck(const TouchEventWithLatencyInfo & event,InputEventAckSource ack_source,InputEventAckState ack_result)2837 void RenderWidgetHostImpl::OnTouchEventAck(
2838     const TouchEventWithLatencyInfo& event,
2839     InputEventAckSource ack_source,
2840     InputEventAckState ack_result) {
2841   latency_tracker_.OnInputEventAck(event.event, &event.latency, ack_result);
2842   for (auto& input_event_observer : input_event_observers_)
2843     input_event_observer.OnInputEventAck(ack_source, ack_result, event.event);
2844 
2845   auto* input_event_router =
2846       delegate() ? delegate()->GetInputEventRouter() : nullptr;
2847 
2848   // At present interstitial pages might not have an input event router, so we
2849   // just have the view process the ack directly in that case; the view is
2850   // guaranteed to be a top-level view with an appropriate implementation of
2851   // ProcessAckedTouchEvent().
2852   if (input_event_router)
2853     input_event_router->ProcessAckedTouchEvent(event, ack_result, view_.get());
2854   else if (view_)
2855     view_->ProcessAckedTouchEvent(event, ack_result);
2856 }
2857 
IsIgnoringInputEvents() const2858 bool RenderWidgetHostImpl::IsIgnoringInputEvents() const {
2859   return process_->IsBlocked() || !delegate_ ||
2860          delegate_->ShouldIgnoreInputEvents();
2861 }
2862 
GotResponseToLockMouseRequest(blink::mojom::PointerLockResult response)2863 bool RenderWidgetHostImpl::GotResponseToLockMouseRequest(
2864     blink::mojom::PointerLockResult response) {
2865   if (response != blink::mojom::PointerLockResult::kSuccess) {
2866     RejectMouseLockOrUnlockIfNecessary(response);
2867   }
2868   if (!pending_mouse_lock_request_) {
2869     // This is possible, e.g., the plugin sends us an unlock request before
2870     // the user allows to lock to mouse.
2871     return false;
2872   }
2873 
2874   DCHECK(request_mouse_callback_);
2875   pending_mouse_lock_request_ = false;
2876   if (view_ && view_->HasFocus()) {
2877     blink::mojom::PointerLockResult result =
2878         view_->LockMouse(mouse_lock_raw_movement_);
2879     std::move(request_mouse_callback_).Run(result);
2880     return result == blink::mojom::PointerLockResult::kSuccess;
2881   }
2882 
2883   std::move(request_mouse_callback_)
2884       .Run(blink::mojom::PointerLockResult::kWrongDocument);
2885   return false;
2886 }
2887 
GotResponseToKeyboardLockRequest(bool allowed)2888 void RenderWidgetHostImpl::GotResponseToKeyboardLockRequest(bool allowed) {
2889   DCHECK(keyboard_lock_requested_);
2890   keyboard_lock_allowed_ = allowed;
2891 
2892   if (keyboard_lock_allowed_)
2893     LockKeyboard();
2894   else
2895     UnlockKeyboard();
2896 }
2897 
DetachDelegate()2898 void RenderWidgetHostImpl::DetachDelegate() {
2899   delegate_ = nullptr;
2900   latency_tracker_.reset_delegate();
2901 }
2902 
WindowSnapshotReachedScreen(int snapshot_id)2903 void RenderWidgetHostImpl::WindowSnapshotReachedScreen(int snapshot_id) {
2904   DCHECK(base::MessageLoopCurrentForUI::IsSet());
2905 
2906   if (!pending_surface_browser_snapshots_.empty()) {
2907     GetView()->CopyFromSurface(
2908         gfx::Rect(), gfx::Size(),
2909         base::BindOnce(&RenderWidgetHostImpl::OnSnapshotFromSurfaceReceived,
2910                        weak_factory_.GetWeakPtr(), snapshot_id, 0));
2911   }
2912 
2913   if (!pending_browser_snapshots_.empty()) {
2914 #if defined(OS_ANDROID)
2915     // On Android, call sites should pass in the bounds with correct offset
2916     // to capture the intended content area.
2917     gfx::Rect snapshot_bounds(GetView()->GetViewBounds());
2918     snapshot_bounds.Offset(0, GetView()->GetNativeView()->content_offset());
2919 #else
2920     gfx::Rect snapshot_bounds(GetView()->GetViewBounds().size());
2921 #endif
2922 
2923     gfx::Image image;
2924     if (ui::GrabViewSnapshot(GetView()->GetNativeView(), snapshot_bounds,
2925                              &image)) {
2926       OnSnapshotReceived(snapshot_id, image);
2927       return;
2928     }
2929 
2930     ui::GrabViewSnapshotAsync(
2931         GetView()->GetNativeView(), snapshot_bounds,
2932         base::BindOnce(&RenderWidgetHostImpl::OnSnapshotReceived,
2933                        weak_factory_.GetWeakPtr(), snapshot_id));
2934   }
2935 }
2936 
OnSnapshotFromSurfaceReceived(int snapshot_id,int retry_count,const SkBitmap & bitmap)2937 void RenderWidgetHostImpl::OnSnapshotFromSurfaceReceived(
2938     int snapshot_id,
2939     int retry_count,
2940     const SkBitmap& bitmap) {
2941   static constexpr int kMaxRetries = 5;
2942   if (bitmap.drawsNothing() && retry_count < kMaxRetries) {
2943     GetView()->CopyFromSurface(
2944         gfx::Rect(), gfx::Size(),
2945         base::BindOnce(&RenderWidgetHostImpl::OnSnapshotFromSurfaceReceived,
2946                        weak_factory_.GetWeakPtr(), snapshot_id,
2947                        retry_count + 1));
2948     return;
2949   }
2950   // If all retries have failed, we return an empty image.
2951   gfx::Image image;
2952   if (!bitmap.drawsNothing())
2953     image = gfx::Image::CreateFrom1xBitmap(bitmap);
2954   // Any pending snapshots with a lower ID than the one received are considered
2955   // to be implicitly complete, and returned the same snapshot data.
2956   auto it = pending_surface_browser_snapshots_.begin();
2957   while (it != pending_surface_browser_snapshots_.end()) {
2958     if (it->first <= snapshot_id) {
2959       std::move(it->second).Run(image);
2960       pending_surface_browser_snapshots_.erase(it++);
2961     } else {
2962       ++it;
2963     }
2964   }
2965 }
2966 
OnSnapshotReceived(int snapshot_id,gfx::Image image)2967 void RenderWidgetHostImpl::OnSnapshotReceived(int snapshot_id,
2968                                               gfx::Image image) {
2969   // Any pending snapshots with a lower ID than the one received are considered
2970   // to be implicitly complete, and returned the same snapshot data.
2971   auto it = pending_browser_snapshots_.begin();
2972   while (it != pending_browser_snapshots_.end()) {
2973     if (it->first <= snapshot_id) {
2974       std::move(it->second).Run(image);
2975       pending_browser_snapshots_.erase(it++);
2976     } else {
2977       ++it;
2978     }
2979   }
2980 #if defined(OS_MACOSX)
2981   if (pending_browser_snapshots_.empty())
2982     GetWakeLock()->CancelWakeLock();
2983 #endif
2984 }
2985 
2986 BrowserAccessibilityManager*
GetRootBrowserAccessibilityManager()2987     RenderWidgetHostImpl::GetRootBrowserAccessibilityManager() {
2988   return delegate_ ? delegate_->GetRootBrowserAccessibilityManager() : nullptr;
2989 }
2990 
2991 BrowserAccessibilityManager*
GetOrCreateRootBrowserAccessibilityManager()2992     RenderWidgetHostImpl::GetOrCreateRootBrowserAccessibilityManager() {
2993   return delegate_ ? delegate_->GetOrCreateRootBrowserAccessibilityManager()
2994                    : nullptr;
2995 }
2996 
GrantFileAccessFromDropData(DropData * drop_data)2997 void RenderWidgetHostImpl::GrantFileAccessFromDropData(DropData* drop_data) {
2998   DCHECK_EQ(GetRoutingID(), drop_data->view_id);
2999   RenderProcessHost* process = GetProcess();
3000   PrepareDropDataForChildProcess(
3001       drop_data, ChildProcessSecurityPolicyImpl::GetInstance(),
3002       process->GetID(), process->GetStoragePartition()->GetFileSystemContext());
3003 }
3004 
RequestCompositionUpdates(bool immediate_request,bool monitor_updates)3005 void RenderWidgetHostImpl::RequestCompositionUpdates(bool immediate_request,
3006                                                      bool monitor_updates) {
3007   if (!immediate_request && monitor_updates == monitoring_composition_info_)
3008     return;
3009   monitoring_composition_info_ = monitor_updates;
3010   GetWidgetInputHandler()->RequestCompositionUpdates(immediate_request,
3011                                                      monitor_updates);
3012 }
3013 
RequestCompositorFrameSink(mojo::PendingReceiver<viz::mojom::CompositorFrameSink> compositor_frame_sink_receiver,mojo::PendingRemote<viz::mojom::CompositorFrameSinkClient> compositor_frame_sink_client)3014 void RenderWidgetHostImpl::RequestCompositorFrameSink(
3015     mojo::PendingReceiver<viz::mojom::CompositorFrameSink>
3016         compositor_frame_sink_receiver,
3017     mojo::PendingRemote<viz::mojom::CompositorFrameSinkClient>
3018         compositor_frame_sink_client) {
3019   // Connects the viz process end of CompositorFrameSink message pipes. The
3020   // renderer compositor may request a new CompositorFrameSink on context
3021   // loss, which will destroy the existing CompositorFrameSink.
3022   auto callback = base::BindOnce(
3023       [](mojo::PendingReceiver<viz::mojom::CompositorFrameSink> receiver,
3024          mojo::PendingRemote<viz::mojom::CompositorFrameSinkClient> client,
3025          const viz::FrameSinkId& frame_sink_id) {
3026         GetHostFrameSinkManager()->CreateCompositorFrameSink(
3027             frame_sink_id, std::move(receiver), std::move(client));
3028       },
3029       std::move(compositor_frame_sink_receiver),
3030       std::move(compositor_frame_sink_client));
3031 
3032   if (view_)
3033     std::move(callback).Run(view_->GetFrameSinkId());
3034   else
3035     create_frame_sink_callback_ = std::move(callback);
3036 }
3037 
RegisterRenderFrameMetadataObserver(mojo::PendingReceiver<mojom::RenderFrameMetadataObserverClient> render_frame_metadata_observer_client_receiver,mojo::PendingRemote<mojom::RenderFrameMetadataObserver> render_frame_metadata_observer)3038 void RenderWidgetHostImpl::RegisterRenderFrameMetadataObserver(
3039     mojo::PendingReceiver<mojom::RenderFrameMetadataObserverClient>
3040         render_frame_metadata_observer_client_receiver,
3041     mojo::PendingRemote<mojom::RenderFrameMetadataObserver>
3042         render_frame_metadata_observer) {
3043   render_frame_metadata_provider_.Bind(
3044       std::move(render_frame_metadata_observer_client_receiver),
3045       std::move(render_frame_metadata_observer));
3046 }
3047 
HasGestureStopped()3048 bool RenderWidgetHostImpl::HasGestureStopped() {
3049   if (delegate_ && delegate_->GetInputEventRouter() &&
3050       delegate_->GetInputEventRouter()->HasEventsPendingDispatch()) {
3051     return false;
3052   }
3053 
3054   if (input_router_->HasPendingEvents())
3055     return false;
3056 
3057   std::unique_ptr<RenderWidgetHostIterator> child_widgets(
3058       GetEmbeddedRenderWidgetHosts());
3059   while (RenderWidgetHost* child = child_widgets->GetNextHost()) {
3060     auto* child_impl = static_cast<RenderWidgetHostImpl*>(child);
3061     if (!child_impl->HasGestureStopped()) {
3062       return false;
3063     }
3064   }
3065 
3066   return true;
3067 }
3068 
DidProcessFrame(uint32_t frame_token)3069 void RenderWidgetHostImpl::DidProcessFrame(uint32_t frame_token) {
3070   frame_token_message_queue_->DidProcessFrame(frame_token);
3071 }
3072 
3073 #if defined(OS_MACOSX)
GetWakeLock()3074 device::mojom::WakeLock* RenderWidgetHostImpl::GetWakeLock() {
3075   // Here is a lazy binding, and will not reconnect after connection error.
3076   if (!wake_lock_) {
3077     mojo::Remote<device::mojom::WakeLockProvider> wake_lock_provider;
3078     GetDeviceService().BindWakeLockProvider(
3079         wake_lock_provider.BindNewPipeAndPassReceiver());
3080     wake_lock_provider->GetWakeLockWithoutContext(
3081         device::mojom::WakeLockType::kPreventDisplaySleep,
3082         device::mojom::WakeLockReason::kOther, "GetSnapshot",
3083         wake_lock_.BindNewPipeAndPassReceiver());
3084   }
3085   return wake_lock_.get();
3086 }
3087 #endif
3088 
SetupInputRouter()3089 void RenderWidgetHostImpl::SetupInputRouter() {
3090   in_flight_event_count_ = 0;
3091   suppress_events_until_keydown_ = false;
3092   monitoring_composition_info_ = false;
3093   StopInputEventAckTimeout();
3094   associated_widget_input_handler_.reset();
3095   widget_input_handler_.reset();
3096 
3097   input_router_ = std::make_unique<InputRouterImpl>(
3098       this, this, fling_scheduler_.get(), GetInputRouterConfigForPlatform());
3099 
3100   // input_router_ recreated, need to update the force_enable_zoom_ state.
3101   input_router_->SetForceEnableZoom(force_enable_zoom_);
3102 
3103   if (IsUseZoomForDSFEnabled()) {
3104     input_router_->SetDeviceScaleFactor(GetScaleFactorForView(view_.get()));
3105   }
3106 }
3107 
SetForceEnableZoom(bool enabled)3108 void RenderWidgetHostImpl::SetForceEnableZoom(bool enabled) {
3109   force_enable_zoom_ = enabled;
3110   input_router_->SetForceEnableZoom(enabled);
3111 }
3112 
SetFrameInputHandler(mojom::FrameInputHandler * frame_input_handler)3113 void RenderWidgetHostImpl::SetFrameInputHandler(
3114     mojom::FrameInputHandler* frame_input_handler) {
3115   if (!frame_input_handler)
3116     return;
3117   frame_input_handler->GetWidgetInputHandler(
3118       associated_widget_input_handler_.BindNewEndpointAndPassReceiver(),
3119       input_router_->BindNewFrameHost());
3120 }
3121 
SetInputTargetClient(mojo::Remote<viz::mojom::InputTargetClient> input_target_client)3122 void RenderWidgetHostImpl::SetInputTargetClient(
3123     mojo::Remote<viz::mojom::InputTargetClient> input_target_client) {
3124   input_target_client_ = std::move(input_target_client);
3125 }
3126 
SetWidget(mojo::PendingRemote<mojom::Widget> widget_remote)3127 void RenderWidgetHostImpl::SetWidget(
3128     mojo::PendingRemote<mojom::Widget> widget_remote) {
3129   if (!widget_remote)
3130     return;
3131 
3132   // If we have a bound handler ensure that we destroy the old input router
3133   // while we reset the |widget_input_handler_|.
3134   if (widget_input_handler_.is_bound())
3135     SetupInputRouter();
3136 
3137   mojo::Remote<mojom::Widget> widget(std::move(widget_remote));
3138   widget->SetupWidgetInputHandler(
3139       widget_input_handler_.BindNewPipeAndPassReceiver(),
3140       input_router_->BindNewHost());
3141 }
3142 
ProgressFlingIfNeeded(TimeTicks current_time)3143 void RenderWidgetHostImpl::ProgressFlingIfNeeded(TimeTicks current_time) {
3144   fling_scheduler_->ProgressFlingOnBeginFrameIfneeded(current_time);
3145 }
3146 
ForceFirstFrameAfterNavigationTimeout()3147 void RenderWidgetHostImpl::ForceFirstFrameAfterNavigationTimeout() {
3148   if (!new_content_rendering_timeout_ ||
3149       !new_content_rendering_timeout_->IsRunning()) {
3150     return;
3151   }
3152   new_content_rendering_timeout_->Stop();
3153   ClearDisplayedGraphics();
3154 }
3155 
StopFling()3156 void RenderWidgetHostImpl::StopFling() {
3157   input_router_->StopFling();
3158 }
3159 
SetScreenOrientationForTesting(uint16_t angle,ScreenOrientationValues type)3160 void RenderWidgetHostImpl::SetScreenOrientationForTesting(
3161     uint16_t angle,
3162     ScreenOrientationValues type) {
3163   screen_orientation_angle_for_testing_ = angle;
3164   screen_orientation_type_for_testing_ = type;
3165   SynchronizeVisualProperties();
3166 }
3167 
LockKeyboard()3168 bool RenderWidgetHostImpl::LockKeyboard() {
3169   if (!keyboard_lock_allowed_ || !is_focused_ || !view_)
3170     return false;
3171 
3172   // KeyboardLock can be activated and deactivated several times per request,
3173   // for example when a fullscreen tab loses and gains focus multiple times,
3174   // so we need to retain a copy of the keys requested.
3175   base::Optional<base::flat_set<ui::DomCode>> copy = keyboard_keys_to_lock_;
3176   return view_->LockKeyboard(std::move(copy));
3177 }
3178 
UnlockKeyboard()3179 void RenderWidgetHostImpl::UnlockKeyboard() {
3180   if (IsKeyboardLocked())
3181     view_->UnlockKeyboard();
3182 }
3183 
OnRenderFrameMetadataChangedBeforeActivation(const cc::RenderFrameMetadata & metadata)3184 void RenderWidgetHostImpl::OnRenderFrameMetadataChangedBeforeActivation(
3185     const cc::RenderFrameMetadata& metadata) {}
3186 
OnRenderFrameMetadataChangedAfterActivation()3187 void RenderWidgetHostImpl::OnRenderFrameMetadataChangedAfterActivation() {
3188   const auto& metadata =
3189       render_frame_metadata_provider_.LastRenderFrameMetadata();
3190 
3191   bool is_mobile_optimized = metadata.is_mobile_optimized;
3192   input_router_->NotifySiteIsMobileOptimized(is_mobile_optimized);
3193   if (auto* touch_emulator = GetExistingTouchEmulator())
3194     touch_emulator->SetDoubleTapSupportForPageEnabled(!is_mobile_optimized);
3195 
3196   // The value |kNull| is only used to indicate an absence of vertical scroll
3197   // direction and should therefore be ignored.
3198   if (metadata.new_vertical_scroll_direction ==
3199       viz::VerticalScrollDirection::kNull) {
3200     return;
3201   }
3202 
3203   // Changes in vertical scroll direction are only propagated for main frames.
3204   // If there is no |owner_delegate|, this is not a main frame.
3205   if (!owner_delegate())
3206     return;
3207 
3208   if (!delegate())
3209     return;
3210 
3211   delegate()->OnVerticalScrollDirectionChanged(
3212       metadata.new_vertical_scroll_direction);
3213 }
3214 
3215 std::vector<viz::SurfaceId>
CollectSurfaceIdsForEviction()3216 RenderWidgetHostImpl::CollectSurfaceIdsForEviction() {
3217   RenderViewHostImpl* rvh = RenderViewHostImpl::From(this);
3218   // A corresponding RenderViewHostImpl may not exist in unit tests.
3219   if (!rvh)
3220     return {};
3221   return rvh->CollectSurfaceIdsForEviction();
3222 }
3223 
3224 std::unique_ptr<RenderWidgetHostIterator>
GetEmbeddedRenderWidgetHosts()3225 RenderWidgetHostImpl::GetEmbeddedRenderWidgetHosts() {
3226   // This iterates over all RenderWidgetHosts and returns those whose Views
3227   // are children of this host's View.
3228   auto hosts = std::make_unique<RenderWidgetHostIteratorImpl>();
3229   auto* parent_view = static_cast<RenderWidgetHostViewBase*>(GetView());
3230   for (auto& it : g_routing_id_widget_map.Get()) {
3231     RenderWidgetHost* widget = it.second;
3232 
3233     auto* view = static_cast<RenderWidgetHostViewBase*>(widget->GetView());
3234     if (view && view->IsRenderWidgetHostViewChildFrame() &&
3235         static_cast<RenderWidgetHostViewChildFrame*>(view)->GetParentView() ==
3236             parent_view) {
3237       hosts->Add(widget);
3238     }
3239   }
3240 
3241   return std::move(hosts);
3242 }
3243 
3244 namespace {
3245 
TransformPointAndRectToRootView(RenderWidgetHostViewBase * view,RenderWidgetHostViewBase * root_view,gfx::Point * transformed_point,gfx::Rect * transformed_rect)3246 bool TransformPointAndRectToRootView(RenderWidgetHostViewBase* view,
3247                                      RenderWidgetHostViewBase* root_view,
3248                                      gfx::Point* transformed_point,
3249                                      gfx::Rect* transformed_rect) {
3250   gfx::Transform transform_to_main_frame;
3251   if (!view->GetTransformToViewCoordSpace(root_view, &transform_to_main_frame))
3252     return false;
3253 
3254   if (transformed_point)
3255     transform_to_main_frame.TransformPoint(transformed_point);
3256 
3257   if (transformed_rect) {
3258     gfx::RectF transformed_rect_f(*transformed_rect);
3259     transform_to_main_frame.TransformRect(&transformed_rect_f);
3260     *transformed_rect = gfx::ToEnclosingRect(transformed_rect_f);
3261   }
3262 
3263   return true;
3264 }
3265 
3266 }  // namespace
3267 
OnAnimateDoubleTapZoomInMainFrame(const gfx::Point & point,const gfx::Rect & rect_to_zoom)3268 void RenderWidgetHostImpl::OnAnimateDoubleTapZoomInMainFrame(
3269     const gfx::Point& point,
3270     const gfx::Rect& rect_to_zoom) {
3271   if (!view_)
3272     return;
3273 
3274   auto* root_view = view_->GetRootView();
3275   gfx::Point transformed_point(point);
3276   gfx::Rect transformed_rect_to_zoom(rect_to_zoom);
3277   if (!TransformPointAndRectToRootView(view_.get(), root_view,
3278                                        &transformed_point,
3279                                        &transformed_rect_to_zoom)) {
3280     return;
3281   }
3282 
3283   auto* root_rvhi = RenderViewHostImpl::From(root_view->GetRenderWidgetHost());
3284   root_rvhi->AnimateDoubleTapZoom(transformed_point, transformed_rect_to_zoom);
3285 }
3286 
OnZoomToFindInPageRectInMainFrame(const gfx::Rect & rect_to_zoom)3287 void RenderWidgetHostImpl::OnZoomToFindInPageRectInMainFrame(
3288     const gfx::Rect& rect_to_zoom) {
3289   if (!view_)
3290     return;
3291 
3292   auto* root_view = view_->GetRootView();
3293   gfx::Rect transformed_rect_to_zoom(rect_to_zoom);
3294   if (!TransformPointAndRectToRootView(view_.get(), root_view, nullptr,
3295                                        &transformed_rect_to_zoom)) {
3296     return;
3297   }
3298 
3299   auto* root_rvhi = RenderViewHostImpl::From(root_view->GetRenderWidgetHost());
3300   root_rvhi->ZoomToFindInPageRect(transformed_rect_to_zoom);
3301 }
3302 
GetRootWidgetViewportSize()3303 gfx::Size RenderWidgetHostImpl::GetRootWidgetViewportSize() {
3304   if (!view_)
3305     return gfx::Size();
3306 
3307   // if |view_| is RWHVCF and |frame_connector_| is destroyed, then call to
3308   // GetRootView will return null pointer.
3309   auto* root_view = view_->GetRootView();
3310   if (!root_view)
3311     return gfx::Size();
3312 
3313   return root_view->GetVisibleViewportSize();
3314 }
3315 
3316 }  // namespace content
3317