blob: 70a3e142d9f4987c694dced5433611d8e937eb75 [file] [log] [blame]
[email protected]60a50072012-01-11 02:05:351// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]2cff0052011-03-18 16:51:445#include "content/renderer/render_widget.h"
initial.commit09911bf2008-07-26 23:55:296
danakj4b347212018-07-04 17:55:177#include <cmath>
8#include <limits>
dchengcedca5612016-04-09 01:40:159#include <memory>
dcheng07945f632015-12-26 07:59:3210#include <utility>
11
[email protected]c27dd4f2014-05-22 18:05:1912#include "base/auto_reset.h"
danakj4b347212018-07-04 17:55:1713#include "base/base_switches.h"
[email protected]32876ae2011-11-15 22:25:2114#include "base/bind.h"
Sebastien Marchand17fa2782019-01-25 19:28:1015#include "base/bind_helpers.h"
[email protected]4fb66842009-12-04 21:41:0016#include "base/command_line.h"
changwan7ded3752016-03-09 23:25:1217#include "base/feature_list.h"
initial.commit09911bf2008-07-26 23:55:2918#include "base/logging.h"
avi1023d012015-12-25 02:39:1419#include "base/macros.h"
dchengcedca5612016-04-09 01:40:1520#include "base/memory/ptr_util.h"
[email protected]b256eca2013-07-11 10:57:4021#include "base/memory/singleton.h"
asvitkine8d51e9d2016-09-02 23:55:4322#include "base/metrics/histogram_macros.h"
[email protected]aa4117f2011-12-09 22:19:2123#include "base/stl_util.h"
danakj4b347212018-07-04 17:55:1724#include "base/strings/string_number_conversions.h"
[email protected]74ebfb12013-06-07 20:48:0025#include "base/strings/utf_string_conversions.h"
Sebastien Marchand75a7cdf2018-11-13 23:47:0326#include "base/system/sys_info.h"
fdoraya19b7702016-12-23 14:19:3127#include "base/threading/thread_task_runner_handle.h"
primiano9e38d552015-01-28 04:18:0128#include "base/trace_event/trace_event.h"
[email protected]661eb9d2009-02-03 02:11:4829#include "build/build_config.h"
loyso6e6efc42017-01-21 02:23:0330#include "cc/animation/animation_host.h"
danakj4b347212018-07-04 17:55:1731#include "cc/base/switches.h"
xidachenfa0199e72017-05-11 11:34:2632#include "cc/input/touch_action.h"
danakjba65a0912017-09-21 16:38:4233#include "cc/trees/layer_tree_frame_sink.h"
loyso6e6efc42017-01-21 02:23:0334#include "cc/trees/layer_tree_host.h"
danakj4c987652018-07-05 15:49:4135#include "cc/trees/ukm_manager.h"
danakj4b347212018-07-04 17:55:1736#include "components/viz/common/features.h"
Fady Samuelc645ffe2017-07-24 17:28:2037#include "components/viz/common/frame_sinks/begin_frame_source.h"
danakjf20f4502017-09-26 17:13:3138#include "components/viz/common/frame_sinks/copy_output_request.h"
danakj4b347212018-07-04 17:55:1739#include "components/viz/common/switches.h"
40#include "content/common/content_switches_internal.h"
paulmeyer6ef5a792016-11-08 20:33:5841#include "content/common/drag_event_source_info.h"
42#include "content/common/drag_messages.h"
jonrossa2ff4f82018-02-16 17:27:4643#include "content/common/render_frame_metadata.mojom.h"
jcivelliae1560a2016-11-01 22:40:2344#include "content/common/render_message_filter.mojom.h"
Nasko Oskovf97e8b02017-07-25 02:45:4245#include "content/common/swapped_out_messages.h"
ekaramad9053e57b2016-04-26 20:00:3846#include "content/common/text_input_state.h"
Albert J. Wong3c93c182018-09-27 17:29:4347#include "content/common/widget_messages.h"
mlamouriafca06dd2017-01-27 23:35:1848#include "content/public/common/content_client.h"
changwan7ded3752016-03-09 23:25:1249#include "content/public/common/content_features.h"
[email protected]c08950d22011-10-13 22:20:2950#include "content/public/common/content_switches.h"
[email protected]a09d53ce2014-01-31 00:46:4251#include "content/public/common/context_menu_params.h"
paulmeyer90f6c31d2016-11-12 00:17:5952#include "content/public/common/drop_data.h"
Khushal3e96e662017-10-30 23:16:5053#include "content/public/common/service_names.mojom.h"
Jaebaek Seo655110e2017-12-19 23:27:3954#include "content/public/common/use_zoom_for_dsf_policy.h"
mlamouriafca06dd2017-01-27 23:35:1855#include "content/public/renderer/content_renderer_client.h"
Avi Drissman07d22452019-03-25 17:56:3956#include "content/public/renderer/render_thread.h"
Saman Samid189e5dfd2017-12-20 22:55:3157#include "content/renderer/browser_plugin/browser_plugin.h"
Sadrul Habib Chowdhury31c98712018-12-11 04:15:1358#include "content/renderer/compositor/layer_tree_view.h"
paulmeyer6ef5a792016-11-08 20:33:5859#include "content/renderer/drop_data_builder.h"
[email protected]b2e4c70132013-10-03 02:07:5160#include "content/renderer/external_popup_menu.h"
Sadrul Habib Chowdhury31c98712018-12-11 04:15:1361#include "content/renderer/frame_swap_message_queue.h"
[email protected]66fca5bc2013-05-23 06:58:2962#include "content/renderer/ime_event_guard.h"
dtapuska9ec1a912017-04-21 15:18:3163#include "content/renderer/input/main_thread_event_queue.h"
Dave Tapuska9db80842017-07-24 17:24:2664#include "content/renderer/input/widget_input_handler_manager.h"
[email protected]adab2332013-07-25 18:04:3265#include "content/renderer/pepper/pepper_plugin_instance_impl.h"
Sadrul Habib Chowdhury31c98712018-12-11 04:15:1366#include "content/renderer/queue_message_swap_promise.h"
[email protected]bffc8302014-01-23 20:52:1667#include "content/renderer/render_frame_impl.h"
danakj4b347212018-07-04 17:55:1768#include "content/renderer/render_frame_metadata_observer_impl.h"
[email protected]e3244ed2014-06-20 20:04:2769#include "content/renderer/render_frame_proxy.h"
[email protected]8704f89b2011-04-15 00:30:0570#include "content/renderer/render_process.h"
[email protected]f1a29a02011-10-06 23:08:4471#include "content/renderer/render_thread_impl.h"
dcheng3ce04b62015-10-26 23:30:5572#include "content/renderer/render_view_impl.h"
Abhijeet Kandalkardccbd612019-04-08 17:37:0973#include "content/renderer/render_widget_screen_metrics_emulator.h"
tfarina556a7232014-10-05 01:02:0974#include "content/renderer/renderer_blink_platform_impl.h"
danakj4b347212018-07-04 17:55:1775#include "gpu/command_buffer/service/gpu_switches.h"
ekaramad330ba4232016-09-23 17:57:4776#include "ipc/ipc_message_start.h"
[email protected]484955942010-08-19 16:13:1877#include "ipc/ipc_sync_message.h"
Antoine Laboure55c9ef82017-11-10 18:51:2378#include "ipc/ipc_sync_message_filter.h"
danakj4b347212018-07-04 17:55:1779#include "media/base/media_switches.h"
Scott Violet02e38b92018-03-27 23:42:1480#include "ppapi/buildflags/buildflags.h"
[email protected]661eb9d2009-02-03 02:11:4881#include "skia/ext/platform_canvas.h"
Blink Reformata30d4232018-04-07 15:31:0682#include "third_party/blink/public/platform/file_path_conversion.h"
Hajime Hoshi315a61f2018-08-14 17:27:2883#include "third_party/blink/public/platform/platform.h"
Blink Reformata30d4232018-04-07 15:31:0684#include "third_party/blink/public/platform/scheduler/web_render_widget_scheduling_state.h"
Yuta Kitamuradeb91bb2018-05-29 05:53:2085#include "third_party/blink/public/platform/scheduler/web_thread_scheduler.h"
Blink Reformata30d4232018-04-07 15:31:0686#include "third_party/blink/public/platform/web_cursor_info.h"
87#include "third_party/blink/public/platform/web_drag_data.h"
88#include "third_party/blink/public/platform/web_drag_operation.h"
89#include "third_party/blink/public/platform/web_mouse_event.h"
90#include "third_party/blink/public/platform/web_point.h"
91#include "third_party/blink/public/platform/web_rect.h"
92#include "third_party/blink/public/platform/web_runtime_features.h"
93#include "third_party/blink/public/platform/web_size.h"
94#include "third_party/blink/public/platform/web_string.h"
95#include "third_party/blink/public/web/web_autofill_client.h"
96#include "third_party/blink/public/web/web_device_emulation_params.h"
97#include "third_party/blink/public/web/web_frame_widget.h"
98#include "third_party/blink/public/web/web_input_method_controller.h"
99#include "third_party/blink/public/web/web_local_frame.h"
100#include "third_party/blink/public/web/web_node.h"
101#include "third_party/blink/public/web/web_page_popup.h"
102#include "third_party/blink/public/web/web_popup_menu_info.h"
103#include "third_party/blink/public/web/web_range.h"
104#include "third_party/blink/public/web/web_settings.h"
105#include "third_party/blink/public/web/web_view.h"
106#include "third_party/blink/public/web/web_widget.h"
[email protected]d353541f2012-05-03 22:45:41107#include "third_party/skia/include/core/SkShader.h"
Maksim Sisova33072d22019-01-24 15:44:25108#include "ui/base/clipboard/clipboard_constants.h"
Scott Violet8ff9c302018-02-22 22:28:35109#include "ui/base/ui_base_features.h"
danakj4b347212018-07-04 17:55:17110#include "ui/base/ui_base_switches.h"
dtapuska97286c882017-02-24 23:14:43111#include "ui/events/base_event_utils.h"
tfarina655f81d2014-12-23 02:38:50112#include "ui/gfx/geometry/point_conversions.h"
tfarina3b0452d2014-12-31 15:20:09113#include "ui/gfx/geometry/rect_conversions.h"
tfarinaebe974f02015-01-03 04:25:32114#include "ui/gfx/geometry/size_conversions.h"
[email protected]1835b9e2012-02-28 13:12:48115#include "ui/gfx/skia_util.h"
danakj4b347212018-07-04 17:55:17116#include "ui/gfx/switches.h"
[email protected]c9e2cbbb2012-05-12 21:17:27117#include "ui/gl/gl_switches.h"
danakj4b347212018-07-04 17:55:17118#include "ui/native_theme/native_theme_features.h"
119#include "ui/native_theme/overlay_scrollbar_constants_aura.h"
[email protected]d353541f2012-05-03 22:45:41120#include "ui/surface/transport_dib.h"
[email protected]661eb9d2009-02-03 02:11:48121
[email protected]eeb93112013-05-01 19:41:10122#if defined(OS_ANDROID)
[email protected]cefe9b152014-03-27 18:16:15123#include <android/keycodes.h>
wjmaclean1d970622017-01-21 22:28:24124#include "base/time/time.h"
[email protected]eeb93112013-05-01 19:41:10125#endif
126
[email protected]661eb9d2009-02-03 02:11:48127#if defined(OS_POSIX)
[email protected]d5282e72009-05-13 13:16:52128#include "third_party/skia/include/core/SkMallocPixelRef.h"
[email protected]d353541f2012-05-03 22:45:41129#include "third_party/skia/include/core/SkPixelRef.h"
[email protected]661eb9d2009-02-03 02:11:48130#endif // defined(OS_POSIX)
[email protected]8085dbc82008-09-26 22:53:44131
ekaramad330ba4232016-09-23 17:57:47132#if defined(OS_MACOSX)
133#include "content/renderer/text_input_client_observer.h"
134#endif
135
Ryan Landay9e42fd742017-08-12 01:59:11136using blink::WebImeTextSpan;
[email protected]180ef242013-11-07 06:50:46137using blink::WebCursorInfo;
[email protected]19193682014-04-03 15:01:43138using blink::WebDeviceEmulationParams;
paulmeyer90f6c31d2016-11-12 00:17:59139using blink::WebDragOperation;
paulmeyer6ef5a792016-11-08 20:33:58140using blink::WebDragOperationsMask;
141using blink::WebDragData;
paulmeyer90f6c31d2016-11-12 00:17:59142using blink::WebFrameWidget;
[email protected]180ef242013-11-07 06:50:46143using blink::WebGestureEvent;
144using blink::WebInputEvent;
dtapuska5d2e9c32015-12-03 16:39:49145using blink::WebInputEventResult;
ekaramad2daaf672016-11-10 20:29:01146using blink::WebInputMethodController;
paulmeyer6ef5a792016-11-08 20:33:58147using blink::WebLocalFrame;
[email protected]180ef242013-11-07 06:50:46148using blink::WebMouseEvent;
149using blink::WebMouseWheelEvent;
150using blink::WebNavigationPolicy;
donnda070f3c2015-01-16 19:54:11151using blink::WebNode;
[email protected]180ef242013-11-07 06:50:46152using blink::WebPagePopup;
dglazkove353a372016-09-01 01:33:48153using blink::WebRange;
[email protected]180ef242013-11-07 06:50:46154using blink::WebRect;
[email protected]180ef242013-11-07 06:50:46155using blink::WebSize;
paulmeyer90f6c31d2016-11-12 00:17:59156using blink::WebString;
[email protected]180ef242013-11-07 06:50:46157using blink::WebTextDirection;
158using blink::WebTouchEvent;
[email protected]f8ed4722013-12-03 03:27:25159using blink::WebTouchPoint;
[email protected]180ef242013-11-07 06:50:46160using blink::WebVector;
161using blink::WebWidget;
[email protected]e9ff79c2012-10-19 21:31:26162
paulmeyer90f6c31d2016-11-12 00:17:59163namespace content {
164
[email protected]6a4d7f62013-01-07 21:32:13165namespace {
[email protected]b256eca2013-07-11 10:57:40166
Albert J. Wong2727e8a82019-02-15 16:56:11167RenderWidget::CreateRenderWidgetFunction g_create_render_widget_for_frame =
168 nullptr;
169
Albert J. Wongcb004632018-07-10 22:58:25170using RoutingIDWidgetMap = std::map<int32_t, RenderWidget*>;
171base::LazyInstance<RoutingIDWidgetMap>::Leaky g_routing_id_widget_map =
172 LAZY_INSTANCE_INITIALIZER;
173
danakj4b347212018-07-04 17:55:17174const base::Feature kUnpremultiplyAndDitherLowBitDepthTiles = {
175 "UnpremultiplyAndDitherLowBitDepthTiles", base::FEATURE_ENABLED_BY_DEFAULT};
176
[email protected]b256eca2013-07-11 10:57:40177typedef std::map<std::string, ui::TextInputMode> TextInputModeMap;
178
AJITH KUMAR V041c0b02017-08-08 10:39:20179static const int kInvalidNextPreviousFlagsValue = -1;
yiyix6b90ef52018-10-09 19:55:30180static const char* kOOPIF = "OOPIF";
181static const char* kRenderer = "Renderer";
AJITH KUMAR V041c0b02017-08-08 10:39:20182
Ian Vollick7a847a82019-06-14 19:58:59183#if defined(OS_ANDROID)
184// With 32 bit pixels, this would mean less than 400kb per buffer. Much less
185// than required for, say, nHD.
186static const int kSmallScreenPixelThreshold = 1e5;
187bool IsSmallScreen(const gfx::Size& size) {
188 int area = 0;
189 if (!size.GetCheckedArea().AssignIfValid(&area))
190 return false;
191 return area < kSmallScreenPixelThreshold;
192}
193#endif
194
lfgbee1e0a2016-06-08 21:24:21195class WebWidgetLockTarget : public content::MouseLockDispatcher::LockTarget {
196 public:
197 explicit WebWidgetLockTarget(blink::WebWidget* webwidget)
198 : webwidget_(webwidget) {}
199
200 void OnLockMouseACK(bool succeeded) override {
201 if (succeeded)
Blink Reformat1c4d759e2017-04-09 16:34:54202 webwidget_->DidAcquirePointerLock();
lfgbee1e0a2016-06-08 21:24:21203 else
Blink Reformat1c4d759e2017-04-09 16:34:54204 webwidget_->DidNotAcquirePointerLock();
lfgbee1e0a2016-06-08 21:24:21205 }
206
Blink Reformat1c4d759e2017-04-09 16:34:54207 void OnMouseLockLost() override { webwidget_->DidLosePointerLock(); }
lfgbee1e0a2016-06-08 21:24:21208
209 bool HandleMouseLockedInputEvent(const blink::WebMouseEvent& event) override {
210 // The WebWidget handles mouse lock in Blink's handleInputEvent().
211 return false;
212 }
213
214 private:
215 blink::WebWidget* webwidget_;
216};
217
Stephen Chenneyd56b5382019-01-29 00:37:02218class ScopedUkmRafAlignedInputTimer {
219 public:
220 explicit ScopedUkmRafAlignedInputTimer(blink::WebWidget* webwidget)
221 : webwidget_(webwidget) {
222 webwidget_->BeginRafAlignedInput();
223 }
224
225 ~ScopedUkmRafAlignedInputTimer() { webwidget_->EndRafAlignedInput(); }
226
227 private:
228 blink::WebWidget* webwidget_;
229
230 DISALLOW_COPY_AND_ASSIGN(ScopedUkmRafAlignedInputTimer);
231};
232
dglazkov97b6c2b2016-10-25 17:35:55233bool IsDateTimeInput(ui::TextInputType type) {
234 return type == ui::TEXT_INPUT_TYPE_DATE ||
235 type == ui::TEXT_INPUT_TYPE_DATE_TIME ||
236 type == ui::TEXT_INPUT_TYPE_DATE_TIME_LOCAL ||
237 type == ui::TEXT_INPUT_TYPE_MONTH ||
238 type == ui::TEXT_INPUT_TYPE_TIME || type == ui::TEXT_INPUT_TYPE_WEEK;
239}
240
paulmeyer90f6c31d2016-11-12 00:17:59241WebDragData DropMetaDataToWebDragData(
242 const std::vector<DropData::Metadata>& drop_meta_data) {
243 std::vector<WebDragData::Item> item_list;
244 for (const auto& meta_data_item : drop_meta_data) {
245 if (meta_data_item.kind == DropData::Kind::STRING) {
246 WebDragData::Item item;
Blink Reformat1c4d759e2017-04-09 16:34:54247 item.storage_type = WebDragData::Item::kStorageTypeString;
248 item.string_type = WebString::FromUTF16(meta_data_item.mime_type);
paulmeyer90f6c31d2016-11-12 00:17:59249 // Have to pass a dummy URL here instead of an empty URL because the
250 // DropData received by browser_plugins goes through a round trip:
251 // DropData::MetaData --> WebDragData-->DropData. In the end, DropData
252 // will contain an empty URL (which means no URL is dragged) if the URL in
253 // WebDragData is empty.
Maksim Sisova33072d22019-01-24 15:44:25254 if (base::EqualsASCII(meta_data_item.mime_type, ui::kMimeTypeURIList)) {
Blink Reformat1c4d759e2017-04-09 16:34:54255 item.string_data = WebString::FromUTF8("about:dragdrop-placeholder");
paulmeyer90f6c31d2016-11-12 00:17:59256 }
257 item_list.push_back(item);
258 continue;
259 }
260
261 // TODO(hush): crbug.com/584789. Blink needs to support creating a file with
262 // just the mimetype. This is needed to drag files to WebView on Android
263 // platform.
264 if ((meta_data_item.kind == DropData::Kind::FILENAME) &&
265 !meta_data_item.filename.empty()) {
266 WebDragData::Item item;
Blink Reformat1c4d759e2017-04-09 16:34:54267 item.storage_type = WebDragData::Item::kStorageTypeFilename;
268 item.filename_data = blink::FilePathToWebString(meta_data_item.filename);
paulmeyer90f6c31d2016-11-12 00:17:59269 item_list.push_back(item);
270 continue;
271 }
272
273 if (meta_data_item.kind == DropData::Kind::FILESYSTEMFILE) {
274 WebDragData::Item item;
Blink Reformat1c4d759e2017-04-09 16:34:54275 item.storage_type = WebDragData::Item::kStorageTypeFileSystemFile;
276 item.file_system_url = meta_data_item.file_system_url;
paulmeyer90f6c31d2016-11-12 00:17:59277 item_list.push_back(item);
278 continue;
279 }
280 }
281
282 WebDragData result;
Blink Reformat1c4d759e2017-04-09 16:34:54283 result.Initialize();
284 result.SetItems(item_list);
paulmeyer90f6c31d2016-11-12 00:17:59285 return result;
286}
287
288WebDragData DropDataToWebDragData(const DropData& drop_data) {
289 std::vector<WebDragData::Item> item_list;
290
291 // These fields are currently unused when dragging into WebKit.
292 DCHECK(drop_data.download_metadata.empty());
293 DCHECK(drop_data.file_contents.empty());
dcheng3dd85612017-02-08 10:46:23294 DCHECK(drop_data.file_contents_content_disposition.empty());
paulmeyer90f6c31d2016-11-12 00:17:59295
296 if (!drop_data.text.is_null()) {
297 WebDragData::Item item;
Blink Reformat1c4d759e2017-04-09 16:34:54298 item.storage_type = WebDragData::Item::kStorageTypeString;
Maksim Sisova33072d22019-01-24 15:44:25299 item.string_type = WebString::FromUTF8(ui::kMimeTypeText);
Blink Reformat1c4d759e2017-04-09 16:34:54300 item.string_data = WebString::FromUTF16(drop_data.text.string());
paulmeyer90f6c31d2016-11-12 00:17:59301 item_list.push_back(item);
302 }
303
304 if (!drop_data.url.is_empty()) {
305 WebDragData::Item item;
Blink Reformat1c4d759e2017-04-09 16:34:54306 item.storage_type = WebDragData::Item::kStorageTypeString;
Maksim Sisova33072d22019-01-24 15:44:25307 item.string_type = WebString::FromUTF8(ui::kMimeTypeURIList);
Blink Reformat1c4d759e2017-04-09 16:34:54308 item.string_data = WebString::FromUTF8(drop_data.url.spec());
309 item.title = WebString::FromUTF16(drop_data.url_title);
paulmeyer90f6c31d2016-11-12 00:17:59310 item_list.push_back(item);
311 }
312
313 if (!drop_data.html.is_null()) {
314 WebDragData::Item item;
Blink Reformat1c4d759e2017-04-09 16:34:54315 item.storage_type = WebDragData::Item::kStorageTypeString;
Maksim Sisova33072d22019-01-24 15:44:25316 item.string_type = WebString::FromUTF8(ui::kMimeTypeHTML);
Blink Reformat1c4d759e2017-04-09 16:34:54317 item.string_data = WebString::FromUTF16(drop_data.html.string());
318 item.base_url = drop_data.html_base_url;
paulmeyer90f6c31d2016-11-12 00:17:59319 item_list.push_back(item);
320 }
321
Darwin Huangb2ad4892019-05-13 20:15:39322 for (const ui::FileInfo& filename : drop_data.filenames) {
paulmeyer90f6c31d2016-11-12 00:17:59323 WebDragData::Item item;
Blink Reformat1c4d759e2017-04-09 16:34:54324 item.storage_type = WebDragData::Item::kStorageTypeFilename;
Darwin Huangb2ad4892019-05-13 20:15:39325 item.filename_data = blink::FilePathToWebString(filename.path);
Blink Reformat1c4d759e2017-04-09 16:34:54326 item.display_name_data =
Darwin Huangb2ad4892019-05-13 20:15:39327 blink::FilePathToWebString(base::FilePath(filename.display_name));
paulmeyer90f6c31d2016-11-12 00:17:59328 item_list.push_back(item);
329 }
330
Darwin Huangb2ad4892019-05-13 20:15:39331 for (const DropData::FileSystemFileInfo& file : drop_data.file_system_files) {
paulmeyer90f6c31d2016-11-12 00:17:59332 WebDragData::Item item;
Blink Reformat1c4d759e2017-04-09 16:34:54333 item.storage_type = WebDragData::Item::kStorageTypeFileSystemFile;
Darwin Huangb2ad4892019-05-13 20:15:39334 item.file_system_url = file.url;
335 item.file_system_file_size = file.size;
336 item.file_system_id = blink::WebString::FromASCII(file.filesystem_id);
paulmeyer90f6c31d2016-11-12 00:17:59337 item_list.push_back(item);
338 }
339
Darwin Huangb2ad4892019-05-13 20:15:39340 for (const auto& data : drop_data.custom_data) {
paulmeyer90f6c31d2016-11-12 00:17:59341 WebDragData::Item item;
Blink Reformat1c4d759e2017-04-09 16:34:54342 item.storage_type = WebDragData::Item::kStorageTypeString;
Darwin Huangb2ad4892019-05-13 20:15:39343 item.string_type = WebString::FromUTF16(data.first);
344 item.string_data = WebString::FromUTF16(data.second);
paulmeyer90f6c31d2016-11-12 00:17:59345 item_list.push_back(item);
346 }
347
348 WebDragData result;
Blink Reformat1c4d759e2017-04-09 16:34:54349 result.Initialize();
350 result.SetItems(item_list);
351 result.SetFilesystemId(WebString::FromUTF16(drop_data.filesystem_id));
paulmeyer90f6c31d2016-11-12 00:17:59352 return result;
353}
354
dtapuskac4dd5be2016-10-25 15:11:10355ui::TextInputType ConvertWebTextInputType(blink::WebTextInputType type) {
356 // Check the type is in the range representable by ui::TextInputType.
357 DCHECK_LE(type, static_cast<int>(ui::TEXT_INPUT_TYPE_MAX))
358 << "blink::WebTextInputType and ui::TextInputType not synchronized";
359 return static_cast<ui::TextInputType>(type);
360}
361
362ui::TextInputMode ConvertWebTextInputMode(blink::WebTextInputMode mode) {
363 // Check the mode is in the range representable by ui::TextInputMode.
364 DCHECK_LE(mode, static_cast<int>(ui::TEXT_INPUT_MODE_MAX))
365 << "blink::WebTextInputMode and ui::TextInputMode not synchronized";
366 return static_cast<ui::TextInputMode>(mode);
367}
368
Mitsuru Oshima3c07b332018-02-06 04:56:50369// Returns true if the device scale is high enough that losing subpixel
370// antialiasing won't have a noticeable effect on text quality.
371static bool DeviceScaleEnsuresTextQuality(float device_scale_factor) {
372#if defined(OS_ANDROID) || defined(OS_CHROMEOS)
373 // On Android, we never have subpixel antialiasing. On Chrome OS we prefer to
374 // composite all scrollers so that we get animated overlay scrollbars.
375 return true;
376#else
377 // 1.5 is a common touchscreen tablet device scale factor. For such
378 // devices main thread antialiasing is a heavy burden.
379 return device_scale_factor >= 1.5f;
380#endif
381}
382
383static bool PreferCompositingToLCDText(CompositorDependencies* compositor_deps,
384 float device_scale_factor) {
385 const base::CommandLine& command_line =
386 *base::CommandLine::ForCurrentProcess();
387 if (command_line.HasSwitch(switches::kDisablePreferCompositingToLCDText))
388 return false;
389 if (command_line.HasSwitch(switches::kEnablePreferCompositingToLCDText))
390 return true;
391 if (!compositor_deps->IsLcdTextEnabled())
392 return true;
393 return DeviceScaleEnsuresTextQuality(device_scale_factor);
394}
395
[email protected]b256eca2013-07-11 10:57:40396} // namespace
397
[email protected]b2e4c70132013-10-03 02:07:51398// RenderWidget ---------------------------------------------------------------
399
Albert J. Wong2727e8a82019-02-15 16:56:11400// static
401void RenderWidget::InstallCreateForFrameHook(
402 CreateRenderWidgetFunction create_widget) {
403 g_create_render_widget_for_frame = create_widget;
404}
405
406scoped_refptr<RenderWidget> RenderWidget::CreateForFrame(
407 int32_t widget_routing_id,
408 CompositorDependencies* compositor_deps,
409 const ScreenInfo& screen_info,
410 blink::WebDisplayMode display_mode,
411 bool is_frozen,
412 bool hidden,
413 bool never_visible,
414 mojom::WidgetRequest widget_request) {
415 if (g_create_render_widget_for_frame) {
416 return g_create_render_widget_for_frame(
417 widget_routing_id, compositor_deps, screen_info, display_mode,
418 is_frozen, hidden, never_visible, std::move(widget_request));
419 }
420
421 return base::WrapRefCounted(new RenderWidget(
422 widget_routing_id, compositor_deps, screen_info, display_mode, is_frozen,
423 hidden, never_visible, std::move(widget_request)));
424}
425
426scoped_refptr<RenderWidget> RenderWidget::CreateForPopup(
427 int32_t widget_routing_id,
428 CompositorDependencies* compositor_deps,
429 const ScreenInfo& screen_info,
430 blink::WebDisplayMode display_mode,
431 bool is_frozen,
432 bool hidden,
433 bool never_visible,
434 mojom::WidgetRequest widget_request) {
435 return base::WrapRefCounted(new RenderWidget(
436 widget_routing_id, compositor_deps, screen_info, display_mode, is_frozen,
437 hidden, never_visible, std::move(widget_request)));
438}
439
danakj829cdd142018-09-14 21:13:27440RenderWidget::RenderWidget(int32_t widget_routing_id,
441 CompositorDependencies* compositor_deps,
danakj829cdd142018-09-14 21:13:27442 const ScreenInfo& screen_info,
443 blink::WebDisplayMode display_mode,
danakj6dcbc5962018-11-16 16:45:42444 bool is_frozen,
danakj829cdd142018-09-14 21:13:27445 bool hidden,
446 bool never_visible,
447 mojom::WidgetRequest widget_request)
nick8331f8ad2016-11-15 20:42:45448 : routing_id_(widget_routing_id),
dcheng35d31c112015-07-22 00:17:36449 compositor_deps_(compositor_deps),
lfg8ff33912016-09-13 20:59:21450 webwidget_internal_(nullptr),
[email protected]847a2582013-03-09 02:29:51451 auto_resize_mode_(false),
[email protected]1ac10dca2013-08-20 20:47:04452 is_hidden_(hidden),
danakj2d2889a2019-04-05 21:22:08453 compositor_never_visible_(never_visible),
mikhail.pozdnyakovf2c902a2015-04-14 08:09:12454 is_fullscreen_granted_(false),
danakj73dd3032018-07-28 17:49:53455 display_mode_(display_mode),
changwanf2a707b2015-10-30 08:22:16456 ime_event_guard_(nullptr),
danakj6dcbc5962018-11-16 16:45:42457 is_frozen_(is_frozen),
dglazkov97b6c2b2016-10-25 17:35:55458 text_input_type_(ui::TEXT_INPUT_TYPE_NONE),
[email protected]b256eca2013-07-11 10:57:40459 text_input_mode_(ui::TEXT_INPUT_MODE_DEFAULT),
shuchen82ce8c52014-10-23 01:55:20460 text_input_flags_(0),
AJITH KUMAR V041c0b02017-08-08 10:39:20461 next_previous_flags_(kInvalidNextPreviousFlagsValue),
[email protected]86ba5fcb2013-09-04 00:36:53462 can_compose_inline_(true),
nonafa291792016-08-10 02:36:18463 composition_range_(gfx::Range::InvalidRange()),
[email protected]867125a02009-12-10 06:01:48464 pending_window_rect_count_(0),
Scott Violetae08f332018-07-27 17:37:23465 screen_info_(screen_info),
nonafa291792016-08-10 02:36:18466 monitor_composition_info_(false),
[email protected]b2e4c70132013-10-03 02:07:51467 popup_origin_scale_for_emulation_(0.f),
samans26510e442017-06-01 22:29:12468 frame_swap_message_queue_(new FrameSwapMessageQueue(routing_id_)),
lfge0c2792ec2016-05-11 18:52:08469 has_host_context_menu_location_(false),
ekaramad2a46d632016-07-19 13:33:09470 has_focus_(false),
danakj6dcbc5962018-11-16 16:45:42471 for_child_local_root_frame_(false),
ekaramad330ba4232016-09-23 17:57:47472#if defined(OS_MACOSX)
473 text_input_client_observer_(new TextInputClientObserver(this)),
474#endif
Takashi SAKAMOTO870f6262017-08-22 04:08:27475 first_update_visual_state_after_hidden_(false),
tasakb95dbb50c2017-02-08 18:07:50476 was_shown_time_(base::TimeTicks::Now()),
danakj2d2889a2019-04-05 21:22:08477 widget_binding_(this, std::move(widget_request)) {
nick8331f8ad2016-11-15 20:42:45478 DCHECK_NE(routing_id_, MSG_ROUTING_NONE);
Avi Drissman07d22452019-03-25 17:56:39479 DCHECK(RenderThread::IsMainThread());
alexclarke7fa93942015-10-21 15:37:11480
481 // In tests there may not be a RenderThreadImpl.
482 if (RenderThreadImpl::current()) {
483 render_widget_scheduling_state_ = RenderThreadImpl::current()
Yuta Kitamura3331f5c2018-04-05 11:12:25484 ->GetWebMainThreadScheduler()
dcheng07945f632015-12-26 07:59:32485 ->NewRenderWidgetSchedulingState();
alexclarke7fa93942015-10-21 15:37:11486 render_widget_scheduling_state_->SetHidden(is_hidden_);
487 }
Albert J. Wongcb004632018-07-10 22:58:25488
489 if (routing_id_ != MSG_ROUTING_NONE)
490 g_routing_id_widget_map.Get().emplace(routing_id_, this);
initial.commit09911bf2008-07-26 23:55:29491}
492
493RenderWidget::~RenderWidget() {
lfg8ff33912016-09-13 20:59:21494 DCHECK(!webwidget_internal_) << "Leaking our WebWidget!";
Albert J. Wong7bbf22d2018-12-20 00:27:27495
Albert J. Wongcb004632018-07-10 22:58:25496 // TODO(ajwong): Add in check that routing_id_ has been removed from
497 // g_routing_id_widget_map once the shutdown semantics for RenderWidget
498 // and RenderViewImpl are rationalized. Currently, too many unit and
499 // browser tests delete a RenderWidget without correclty going through
500 // the shutdown. https://crbug.com/545684
initial.commit09911bf2008-07-26 23:55:29501}
502
[email protected]484955942010-08-19 16:13:18503// static
Albert J. Wongcb004632018-07-10 22:58:25504RenderWidget* RenderWidget::FromRoutingID(int32_t routing_id) {
Avi Drissman07d22452019-03-25 17:56:39505 DCHECK(RenderThread::IsMainThread());
Albert J. Wongcb004632018-07-10 22:58:25506 RoutingIDWidgetMap* widgets = g_routing_id_widget_map.Pointer();
jdoerrie5a73d0fa2018-10-02 23:54:05507 auto it = widgets->find(routing_id);
Albert J. Wongcb004632018-07-10 22:58:25508 return it == widgets->end() ? NULL : it->second;
509}
510
danakj25ec6e2b2018-09-26 17:01:31511void RenderWidget::InitForPopup(ShowCallback show_callback,
512 blink::WebPagePopup* web_page_popup) {
513 // Init() increments the reference count on |this|, making it
514 // self-referencing.
515 Init(std::move(show_callback), web_page_popup);
initial.commit09911bf2008-07-26 23:55:29516}
517
danakj6a816592018-09-25 18:30:56518void RenderWidget::InitForChildLocalRoot(
519 blink::WebFrameWidget* web_frame_widget) {
danakj6dcbc5962018-11-16 16:45:42520 for_child_local_root_frame_ = true;
danakj6a816592018-09-25 18:30:56521 // Init() increments the reference count on |this|, making it
522 // self-referencing.
523 Init(base::NullCallback(), web_frame_widget);
dchengda9b4bb2015-07-20 20:58:08524}
525
dchengda9b4bb2015-07-20 20:58:08526void RenderWidget::CloseForFrame() {
danakj8f0bd6c2019-03-14 15:35:24527 DCHECK(for_child_local_root_frame_);
dchengd96a27a2015-07-24 20:17:32528 OnClose();
kenrba7199832015-01-22 23:44:59529}
530
Scott Violetae08f332018-07-27 17:37:23531void RenderWidget::Init(ShowCallback show_callback, WebWidget* web_widget) {
lfg8ff33912016-09-13 20:59:21532 DCHECK(!webwidget_internal_);
nick4df698d82016-11-11 20:39:23533 DCHECK_NE(routing_id_, MSG_ROUTING_NONE);
initial.commit09911bf2008-07-26 23:55:29534
danakj17216f4d2018-10-16 22:57:32535 RenderThreadImpl* render_thread_impl = RenderThreadImpl::current();
536
Jeremy Roman04f27c372017-10-27 15:20:55537 input_handler_ = std::make_unique<RenderWidgetInputHandler>(this, this);
nick8331f8ad2016-11-15 20:42:45538
danakj17216f4d2018-10-16 22:57:32539 LayerTreeView* layer_tree_view = InitializeLayerTreeView();
danakje3bd12622019-02-13 21:54:16540 web_widget->SetLayerTreeView(layer_tree_view,
541 layer_tree_view->animation_host());
Dave Tapuska9db80842017-07-24 17:24:26542
danakj31699d5b2018-09-18 15:39:17543 blink::scheduler::WebThreadScheduler* main_thread_scheduler = nullptr;
544 if (render_thread_impl)
545 main_thread_scheduler = render_thread_impl->GetWebMainThreadScheduler();
Alexander Timin41e917d2018-07-02 16:25:35546 blink::scheduler::WebThreadScheduler* compositor_thread_scheduler =
547 blink::scheduler::WebThreadScheduler::CompositorThreadScheduler();
danakj31699d5b2018-09-18 15:39:17548 scoped_refptr<base::SingleThreadTaskRunner> compositor_input_task_runner;
danakj89ae4532018-12-12 22:52:55549 // Use the compositor thread task runner unless this is a popup or other such
550 // non-frame widgets. The |compositor_thread_scheduler| can be null in tests
551 // without a compositor thread.
552 if (for_frame() && compositor_thread_scheduler) {
553 compositor_input_task_runner =
554 compositor_thread_scheduler->InputTaskRunner();
danakj31699d5b2018-09-18 15:39:17555 }
Alexander Timin41e917d2018-07-02 16:25:35556
David Bokan3fc68482019-03-13 04:48:49557 // We only use an external input handler for frame RenderWidgets because only
558 // frames use the compositor for input handling. Other kinds of RenderWidgets
559 // (e.g. popups, plugins) must forward their input directly through
560 // RenderWidgetInputHandler into Blink.
561 bool uses_input_handler = for_frame();
Dave Tapuska04bc5ee92018-04-17 19:03:31562 widget_input_handler_manager_ = WidgetInputHandlerManager::Create(
danakj31699d5b2018-09-18 15:39:17563 weak_ptr_factory_.GetWeakPtr(), std::move(compositor_input_task_runner),
David Bokan3fc68482019-03-13 04:48:49564 main_thread_scheduler, uses_input_handler);
Dave Tapuska9db80842017-07-24 17:24:26565
danakjdf1ceb72018-07-18 20:02:25566 show_callback_ = std::move(show_callback);
initial.commit09911bf2008-07-26 23:55:29567
lfg8ff33912016-09-13 20:59:21568 webwidget_internal_ = web_widget;
569 webwidget_mouse_lock_target_.reset(
570 new WebWidgetLockTarget(webwidget_internal_));
lfgbee1e0a2016-06-08 21:24:21571 mouse_lock_dispatcher_.reset(new RenderWidgetMouseLockDispatcher(this));
initial.commit09911bf2008-07-26 23:55:29572
nick4df698d82016-11-11 20:39:23573 RenderThread::Get()->AddRoute(routing_id_, this);
574 // Take a reference on behalf of the RenderThread. This will be balanced
Albert J. Wong3c93c182018-09-27 17:29:43575 // when we receive WidgetMsg_Close.
nick4df698d82016-11-11 20:39:23576 AddRef();
initial.commit09911bf2008-07-26 23:55:29577}
578
danakj53802692018-07-25 21:46:44579void RenderWidget::ApplyEmulatedScreenMetricsForPopupWidget(
580 RenderWidget* origin_widget) {
581 RenderWidgetScreenMetricsEmulator* emulator =
582 origin_widget->screen_metrics_emulator_.get();
583 if (!emulator)
584 return;
[email protected]b2e4c70132013-10-03 02:07:51585 popup_origin_scale_for_emulation_ = emulator->scale();
[email protected]19193682014-04-03 15:01:43586 popup_view_origin_for_emulation_ = emulator->applied_widget_rect().origin();
Pavel Feldmancc099f72017-07-20 23:09:00587 popup_screen_origin_for_emulation_ =
Lei Zhang9b359d32017-11-28 00:57:02588 emulator->original_screen_rect().origin();
Fady Samuel1c1ad3692018-11-06 23:28:40589 UpdateSurfaceAndScreenInfo(local_surface_id_allocation_from_parent_,
danakj0d963bd2019-02-20 18:00:46590 CompositorViewportSize(),
Fady Samuel1c1ad3692018-11-06 23:28:40591 emulator->original_screen_info());
[email protected]b2e4c70132013-10-03 02:07:51592}
593
[email protected]2d6836f42014-07-02 17:25:31594gfx::Rect RenderWidget::AdjustValidationMessageAnchor(const gfx::Rect& anchor) {
595 if (screen_metrics_emulator_)
596 return screen_metrics_emulator_->AdjustValidationMessageAnchor(anchor);
597 return anchor;
598}
599
thakis18e426412017-03-15 12:06:37600#if BUILDFLAG(USE_EXTERNAL_POPUP_MENU)
[email protected]b2e4c70132013-10-03 02:07:51601void RenderWidget::SetExternalPopupOriginAdjustmentsForEmulation(
Albert J. Wong7bbf22d2018-12-20 00:27:27602 ExternalPopupMenu* popup) {
603 if (screen_metrics_emulator_)
604 popup->SetOriginScaleForEmulation(screen_metrics_emulator_->scale());
[email protected]b2e4c70132013-10-03 02:07:51605}
[email protected]53907862014-03-25 15:42:40606#endif
[email protected]b2e4c70132013-10-03 02:07:51607
608void RenderWidget::OnShowHostContextMenu(ContextMenuParams* params) {
609 if (screen_metrics_emulator_)
610 screen_metrics_emulator_->OnShowContextMenu(params);
611}
612
[email protected]a95986a82010-12-24 06:19:28613bool RenderWidget::OnMessageReceived(const IPC::Message& message) {
ekaramad330ba4232016-09-23 17:57:47614#if defined(OS_MACOSX)
615 if (IPC_MESSAGE_CLASS(message) == TextInputClientMsgStart)
616 return text_input_client_observer_->OnMessageReceived(message);
617#endif
lfgbee1e0a2016-06-08 21:24:21618 if (mouse_lock_dispatcher_ &&
619 mouse_lock_dispatcher_->OnMessageReceived(message))
620 return true;
621
[email protected]a95986a82010-12-24 06:19:28622 bool handled = true;
623 IPC_BEGIN_MESSAGE_MAP(RenderWidget, message)
Albert J. Wong3c93c182018-09-27 17:29:43624 IPC_MESSAGE_HANDLER(WidgetMsg_ShowContextMenu, OnShowContextMenu)
625 IPC_MESSAGE_HANDLER(WidgetMsg_Close, OnClose)
626 IPC_MESSAGE_HANDLER(WidgetMsg_SynchronizeVisualProperties,
Fady Samuel799e72192018-04-25 21:16:57627 OnSynchronizeVisualProperties)
Albert J. Wong3c93c182018-09-27 17:29:43628 IPC_MESSAGE_HANDLER(WidgetMsg_EnableDeviceEmulation,
dgozman9260b0a12015-03-16 13:45:20629 OnEnableDeviceEmulation)
Albert J. Wong3c93c182018-09-27 17:29:43630 IPC_MESSAGE_HANDLER(WidgetMsg_DisableDeviceEmulation,
dgozman9260b0a12015-03-16 13:45:20631 OnDisableDeviceEmulation)
Albert J. Wong3c93c182018-09-27 17:29:43632 IPC_MESSAGE_HANDLER(WidgetMsg_WasHidden, OnWasHidden)
633 IPC_MESSAGE_HANDLER(WidgetMsg_WasShown, OnWasShown)
634 IPC_MESSAGE_HANDLER(WidgetMsg_SetActive, OnSetActive)
Albert J. Wong3c93c182018-09-27 17:29:43635 IPC_MESSAGE_HANDLER(WidgetMsg_SetTextDirection, OnSetTextDirection)
636 IPC_MESSAGE_HANDLER(WidgetMsg_SetBounds_ACK, OnRequestSetBoundsAck)
637 IPC_MESSAGE_HANDLER(WidgetMsg_UpdateScreenRects, OnUpdateScreenRects)
638 IPC_MESSAGE_HANDLER(WidgetMsg_ForceRedraw, OnForceRedraw)
639 IPC_MESSAGE_HANDLER(WidgetMsg_SetViewportIntersection,
kenrbea731792017-01-13 15:10:48640 OnSetViewportIntersection)
Albert J. Wong3c93c182018-09-27 17:29:43641 IPC_MESSAGE_HANDLER(WidgetMsg_SetIsInert, OnSetIsInert)
642 IPC_MESSAGE_HANDLER(WidgetMsg_SetInheritedEffectiveTouchAction,
sunxd540a9962018-05-24 22:51:06643 OnSetInheritedEffectiveTouchAction)
Albert J. Wong3c93c182018-09-27 17:29:43644 IPC_MESSAGE_HANDLER(WidgetMsg_UpdateRenderThrottlingStatus,
Ken Buchanan8a319fb2017-11-15 18:37:12645 OnUpdateRenderThrottlingStatus)
Albert J. Wong3c93c182018-09-27 17:29:43646 IPC_MESSAGE_HANDLER(WidgetMsg_WaitForNextFrameForTests,
lfg43e08e62016-02-03 18:51:37647 OnWaitNextFrameForTests)
paulmeyer90f6c31d2016-11-12 00:17:59648 IPC_MESSAGE_HANDLER(DragMsg_TargetDragEnter, OnDragTargetDragEnter)
649 IPC_MESSAGE_HANDLER(DragMsg_TargetDragOver, OnDragTargetDragOver)
650 IPC_MESSAGE_HANDLER(DragMsg_TargetDragLeave, OnDragTargetDragLeave)
651 IPC_MESSAGE_HANDLER(DragMsg_TargetDrop, OnDragTargetDrop)
paulmeyer8fc8ea92016-11-15 05:12:21652 IPC_MESSAGE_HANDLER(DragMsg_SourceEnded, OnDragSourceEnded)
653 IPC_MESSAGE_HANDLER(DragMsg_SourceSystemDragEnded,
654 OnDragSourceSystemDragEnded)
[email protected]a95986a82010-12-24 06:19:28655 IPC_MESSAGE_UNHANDLED(handled = false)
656 IPC_END_MESSAGE_MAP()
657 return handled;
658}
initial.commit09911bf2008-07-26 23:55:29659
660bool RenderWidget::Send(IPC::Message* message) {
Nasko Oskovf97e8b02017-07-25 02:45:42661 // Don't send any messages after the browser has told us to close, and filter
danakj6dcbc5962018-11-16 16:45:42662 // most outgoing messages when frozen.
663 if (closing_) {
664 delete message;
665 return false;
666 }
667 if (is_frozen_ && !SwappedOutMessages::CanSendWhileSwappedOut(message)) {
initial.commit09911bf2008-07-26 23:55:29668 delete message;
669 return false;
670 }
671
672 // If given a messsage without a routing ID, then assign our routing ID.
673 if (message->routing_id() == MSG_ROUTING_NONE)
674 message->set_routing_id(routing_id_);
675
[email protected]380244092011-10-07 17:26:27676 return RenderThread::Get()->Send(message);
[email protected]8085dbc82008-09-26 22:53:44677}
678
dtapuska014ed082016-11-11 21:58:48679void RenderWidget::SendOrCrash(IPC::Message* message) {
680 bool result = Send(message);
681 CHECK(closing_ || result) << "Failed to send message";
682}
683
ekaramad5aff1942017-01-06 01:26:35684bool RenderWidget::ShouldHandleImeEvents() const {
Albert J. Wong7bbf22d2018-12-20 00:27:27685 if (delegate())
danakj89ae4532018-12-12 22:52:55686 return has_focus_;
687 if (for_child_local_root_frame_) {
688 // TODO(ekaramad): We track page focus in all RenderViews on the page but
689 // the RenderWidgets corresponding to child local roots do not get the
690 // update. For now, this method returns true when the RenderWidget is for a
691 // child local frame, i.e., IME events will be processed regardless of page
692 // focus. We should revisit this after page focus for OOPIFs has been fully
693 // resolved (https://crbug.com/689777).
694 return true;
695 }
696 // Not a frame widget.
697 return false;
ekaramad5aff1942017-01-06 01:26:35698}
699
initial.commit09911bf2008-07-26 23:55:29700void RenderWidget::OnClose() {
Avi Drissman07d22452019-03-25 17:56:39701 DCHECK(RenderThread::IsMainThread());
dchengd96a27a2015-07-24 20:17:32702 if (closing_)
703 return;
danakj292b63e2018-11-19 20:47:06704 for (auto& observer : render_frames_)
705 observer.WidgetWillClose();
dchengd96a27a2015-07-24 20:17:32706 closing_ = true;
707
708 // Browser correspondence is no longer needed at this point.
709 if (routing_id_ != MSG_ROUTING_NONE) {
710 RenderThread::Get()->RemoveRoute(routing_id_);
Albert J. Wongcb004632018-07-10 22:58:25711 g_routing_id_widget_map.Get().erase(routing_id_);
dchengd96a27a2015-07-24 20:17:32712 }
713
danakj327a4fe2019-01-26 00:00:35714 // Stop handling main thread input events immediately so we don't have them
715 // running while things are partly shut down.
716 if (input_event_queue_)
717 input_event_queue_->ClearClient();
718
danakj6dcbc5962018-11-16 16:45:42719 if (for_child_local_root_frame_) {
dchengd96a27a2015-07-24 20:17:32720 // Widgets for frames may be created and closed at any time while the frame
Ken Buchananed449bb2018-02-01 21:02:05721 // is alive. However, WebWidget must be closed synchronously because frame
722 // widgets and frames hold pointers to each other. The deferred call to
723 // Close() will complete cleanup and release |this|, but CloseWebWidget()
724 // prevents Close() from attempting to access members of an
725 // already-deleted frame.
726 CloseWebWidget();
dchengd96a27a2015-07-24 20:17:32727 }
Ken Buchananed449bb2018-02-01 21:02:05728 // If there is a Send call on the stack, then it could be dangerous to close
729 // now. Post a task that only gets invoked when there are no nested message
730 // loops.
Albert J. Wong2727e8a82019-02-15 16:56:11731 //
732 // The asynchronous Close() takes an owning reference to |this| keeping the
733 // object alive beyond the Release() below. It is the last reference to this
734 // object.
735 //
736 // TODO(https://crbug.com/545684): The actual lifetime for RenderWidget
737 // seems to be single-owner. It is either owned by "IPC" events (popup,
738 // mainframe, and fullscreen), or a RenderFrame. If Close() self-deleting,
739 // all the ref-counting mess could be removed.
Hajime Hoshi315a61f2018-08-14 17:27:28740 GetCleanupTaskRunner()->PostNonNestableTask(
741 FROM_HERE, base::BindOnce(&RenderWidget::Close, this));
dchengd96a27a2015-07-24 20:17:32742
743 // Balances the AddRef taken when we called AddRoute.
744 Release();
initial.commit09911bf2008-07-26 23:55:29745}
746
Fady Samuel799e72192018-04-25 21:16:57747void RenderWidget::OnSynchronizeVisualProperties(
danakja2c9d0a92018-07-25 20:01:18748 const VisualProperties& original_params) {
749 TRACE_EVENT0("renderer", "RenderWidget::OnSynchronizeVisualProperties");
EhsanKbd2cea992017-11-23 18:49:08750
danakja2c9d0a92018-07-25 20:01:18751 VisualProperties params = original_params;
danakj01474dd2019-01-28 10:46:04752 // Web tests can override the device scale factor in the renderer.
753 if (device_scale_factor_for_testing_) {
danakjb4cec102019-01-30 19:22:13754 params.screen_info.device_scale_factor = device_scale_factor_for_testing_;
danakj01474dd2019-01-28 10:46:04755 params.compositor_viewport_pixel_size = gfx::ScaleToCeiledSize(
756 params.new_size, params.screen_info.device_scale_factor);
757 }
danakj04d128a2019-01-25 02:10:20758
759 // Inform the rendering thread of the color space indicating the presence of
760 // HDR capabilities. The HDR bit happens to be globally true/false for all
761 // browser windows (on Windows OS) and thus would be the same for all
762 // RenderWidgets, so clobbering each other works out since only the HDR bit is
763 // used. See https://crbug.com/803451 and
764 // https://chromium-review.googlesource.com/c/chromium/src/+/852912/15#message-68bbd3e25c3b421a79cd028b2533629527d21fee
765 //
766 // The RenderThreadImpl can be null in tests.
767 {
768 RenderThreadImpl* render_thread = RenderThreadImpl::current();
769 if (render_thread)
770 render_thread->SetRenderingColorSpace(params.screen_info.color_space);
771 }
772
Albert J. Wong7bbf22d2018-12-20 00:27:27773 if (delegate()) {
danakj84bbf552019-01-21 21:33:31774 if (size_ != params.new_size) {
775 // Only hide popups when the size changes. Eg https://crbug.com/761908.
776 delegate()->CancelPagePopupForWidget();
777 }
danakja2c9d0a92018-07-25 20:01:18778
779 if (display_mode_ != params.display_mode) {
780 display_mode_ = params.display_mode;
Albert J. Wong7bbf22d2018-12-20 00:27:27781 delegate()->ApplyNewDisplayModeForWidget(params.display_mode);
danakja2c9d0a92018-07-25 20:01:18782 }
783
784 bool auto_resize_mode_changed =
785 auto_resize_mode_ != params.auto_resize_enabled;
786 auto_resize_mode_ = params.auto_resize_enabled;
787 min_size_for_auto_resize_ = params.min_size_for_auto_resize;
788 max_size_for_auto_resize_ = params.max_size_for_auto_resize;
789
790 if (auto_resize_mode_) {
791 gfx::Size min_auto_size = min_size_for_auto_resize_;
792 gfx::Size max_auto_size = max_size_for_auto_resize_;
793 if (compositor_deps_->IsUseZoomForDSFEnabled()) {
794 min_auto_size = gfx::ScaleToCeiledSize(
795 min_auto_size, params.screen_info.device_scale_factor);
796 max_auto_size = gfx::ScaleToCeiledSize(
797 max_auto_size, params.screen_info.device_scale_factor);
798 }
Albert J. Wong7bbf22d2018-12-20 00:27:27799 delegate()->ApplyAutoResizeLimitsForWidget(min_auto_size, max_auto_size);
danakja2c9d0a92018-07-25 20:01:18800 } else if (auto_resize_mode_changed) {
Albert J. Wong7bbf22d2018-12-20 00:27:27801 delegate()->DisableAutoResizeForWidget();
danakja2c9d0a92018-07-25 20:01:18802 if (params.new_size.IsEmpty())
803 return;
804 }
805
806 browser_controls_shrink_blink_size_ =
807 params.browser_controls_shrink_blink_size;
808 top_controls_height_ = params.top_controls_height;
809 bottom_controls_height_ = params.bottom_controls_height;
[email protected]b2e4c70132013-10-03 02:07:51810 }
811
danakj7602f4f2019-01-31 18:03:23812 bool ignore_resize_ipc = false;
813 if (synchronous_resize_mode_for_testing_) {
814 // We can ignore browser-initialized resizing during synchronous
815 // (renderer-controlled) mode, unless it is switching us to/from
816 // fullsreen mode or changing the device scale factor.
817 // TODO(danakj): Does the browser actually change DSF inside a web test??
818 // TODO(danakj): Isn't the display mode check redundant with the fullscreen
819 // one?
820 if (params.is_fullscreen_granted == is_fullscreen_granted_ &&
821 params.display_mode == display_mode_ &&
822 params.screen_info.device_scale_factor ==
823 screen_info_.device_scale_factor)
824 ignore_resize_ipc = true;
825 }
826
827 // When controlling the size in the renderer, we should ignore sizes given by
828 // the browser IPC here.
829 // TODO(danakj): There are many things also being ignored that aren't the
830 // widget's size params. It works because tests that use this mode don't
831 // change those parameters, I guess. But it's more complicated then because it
832 // looks like they are related to sync resize mode. Let's move them out of
833 // this block.
834 // TODO(danakj): It would be nice if we can still use the emulator to emulate
835 // things other than the size if we are in sync resize mode - if the emulator
836 // is even used in sync resize tests. It probably isn't though, so either way
837 // it'd be good to get the emulator out of this block (maybe by overwriting
838 // some of |params| in sync resize mode instead of just skipping the emulator.
839 if (!ignore_resize_ipc) {
danakja2c9d0a92018-07-25 20:01:18840 if (screen_metrics_emulator_) {
danakj7602f4f2019-01-31 18:03:23841 // This will call our SynchronizeVisualProperties() method with a
842 // different set of VisualProperties, holding emulated values. Though not
843 // all VisualProperties are modified by the metrics emulator, so it's a
844 // bit unclear to do this with the full structure. Anything it does not
845 // modify can be consumed directly here instead of in
846 // SynchronizeVisualProperties().
danakja2c9d0a92018-07-25 20:01:18847 screen_metrics_emulator_->OnSynchronizeVisualProperties(params);
848 } else {
danakj0246bba02019-01-23 23:12:27849 if (!delegate()) {
850 // The main frame controls the page scale factor, from blink. For other
851 // frame widgets, the page scale is received from its parent as part of
852 // the visual properties here. While blink doesn't need to know this
853 // page scale factor outside the main frame, the compositor does in
854 // order to produce its output at the correct scale.
W. James MacLean2cd99b62019-04-08 13:54:43855 layer_tree_view_->SetExternalPageScaleFactor(
856 params.page_scale_factor, params.is_pinch_gesture_active);
danakj0246bba02019-01-23 23:12:27857 // Store the value to give to any new RenderFrameProxy that is
858 // registered.
859 page_scale_factor_from_mainframe_ = params.page_scale_factor;
W. James MacLean2cd99b62019-04-08 13:54:43860 // Similarly, only the main frame knows when a pinch gesture is active,
861 // but this information is needed in subframes so they can throttle
862 // re-rastering in the same manner as the main frame.
863 // |is_pinch_gesture_active| follows the same path to the subframe
864 // compositor(s) as |page_scale_factor|.
865 is_pinch_gesture_active_from_mainframe_ =
866 params.is_pinch_gesture_active;
danakj0246bba02019-01-23 23:12:27867 // Push the page scale factor down to any child RenderWidgets via our
868 // child proxy frames.
869 // TODO(danakj): This ends up setting the page scale factor in the
870 // RenderWidgetHost of the child RenderWidget, so that it can bounce
871 // the value down to its RenderWidget. Since this is essentially a
872 // global value per-page, we could instead store it once in the browser
873 // (such as in RenderViewHost) and distribute it to each frame-hosted
874 // RenderWidget from there.
W. James MacLean2cd99b62019-04-08 13:54:43875 for (auto& child_proxy : render_frame_proxies_) {
876 child_proxy.OnPageScaleFactorChanged(params.page_scale_factor,
877 params.is_pinch_gesture_active);
878 }
danakj0246bba02019-01-23 23:12:27879 }
880
danakja2c9d0a92018-07-25 20:01:18881 gfx::Size old_visible_viewport_size = visible_viewport_size_;
882 SynchronizeVisualProperties(params);
883 if (old_visible_viewport_size != visible_viewport_size_) {
884 for (auto& render_frame : render_frames_)
Sushanth Rajasankarceaee202019-03-18 15:44:17885 render_frame.ResetHasScrolledFocusedEditableIntoView();
danakja2c9d0a92018-07-25 20:01:18886 }
887 }
Fady Samuel799e72192018-04-25 21:16:57888 }
889
Sushanth Rajasankarceaee202019-03-18 15:44:17890 // TODO(crbug.com/939118): ScrollFocusedNodeIntoViewForWidget does not work
891 // when the focused node is inside an OOPIF. This code path where
892 // scroll_focused_node_into_view is set is used only for WebView, crbug
893 // 939118 tracks fixing webviews to not use scroll_focused_node_into_view.
Albert J. Wong7bbf22d2018-12-20 00:27:27894 if (delegate() && params.scroll_focused_node_into_view)
895 delegate()->ScrollFocusedNodeIntoViewForWidget();
initial.commit09911bf2008-07-26 23:55:29896}
897
dgozman9260b0a12015-03-16 13:45:20898void RenderWidget::OnEnableDeviceEmulation(
Lei Zhang9b359d32017-11-28 00:57:02899 const blink::WebDeviceEmulationParams& params) {
mfomitchev2600fd7c2016-02-17 20:53:39900 if (!screen_metrics_emulator_) {
Fady Samuel4255c182018-05-24 20:42:36901 VisualProperties visual_properties;
902 visual_properties.screen_info = screen_info_;
903 visual_properties.new_size = size_;
danakj0d963bd2019-02-20 18:00:46904 visual_properties.compositor_viewport_pixel_size = CompositorViewportSize();
Fady Samuel84d8a1f72018-11-08 00:26:17905 visual_properties.local_surface_id_allocation =
906 local_surface_id_allocation_from_parent_;
Fady Samuel4255c182018-05-24 20:42:36907 visual_properties.visible_viewport_size = visible_viewport_size_;
908 visual_properties.is_fullscreen_granted = is_fullscreen_granted_;
909 visual_properties.display_mode = display_mode_;
mfomitchev2600fd7c2016-02-17 20:53:39910 screen_metrics_emulator_.reset(new RenderWidgetScreenMetricsEmulator(
Albert J. Wong3c93c182018-09-27 17:29:43911 this, params, visual_properties, widget_screen_rect_,
Fady Samuel4255c182018-05-24 20:42:36912 window_screen_rect_));
oshima50872a72016-03-04 13:26:18913 screen_metrics_emulator_->Apply();
mfomitchev2600fd7c2016-02-17 20:53:39914 } else {
dgozman9260b0a12015-03-16 13:45:20915 screen_metrics_emulator_->ChangeEmulationParams(params);
mfomitchev2600fd7c2016-02-17 20:53:39916 }
dgozman9260b0a12015-03-16 13:45:20917}
918
919void RenderWidget::OnDisableDeviceEmulation() {
920 screen_metrics_emulator_.reset();
921}
922
initial.commit09911bf2008-07-26 23:55:29923void RenderWidget::OnWasHidden() {
danakj14e00a8322019-02-07 17:12:06924 // A frozen main frame widget will never be hidden since that would require it
925 // to be shown first. It must be thawed before changing visibility.
926 DCHECK(!is_frozen_);
927
[email protected]9c3085f2011-06-09 02:10:31928 TRACE_EVENT0("renderer", "RenderWidget::OnWasHidden");
danakj14e00a8322019-02-07 17:12:06929
[email protected]bee16aab2009-08-26 15:55:03930 SetHidden(true);
danakj14e00a8322019-02-07 17:12:06931
Francois Doray687e70a2019-05-16 02:24:55932 tab_switch_time_recorder_.TabWasHidden();
933
ericwilligers88e69742016-10-17 19:29:55934 for (auto& observer : render_frames_)
935 observer.WasHidden();
initial.commit09911bf2008-07-26 23:55:29936}
937
Sebastien Marchand5f06ca32019-05-17 18:03:42938void RenderWidget::OnWasShown(
939 base::TimeTicks show_request_timestamp,
940 bool was_evicted,
941 const base::Optional<content::RecordTabSwitchTimeRequest>&
942 record_tab_switch_time_request) {
danakj14e00a8322019-02-07 17:12:06943 // A frozen main frame widget does not become shown, since it has no frame
944 // associated with it. It must be thawed before changing visibility.
945 DCHECK(!is_frozen_);
946
Edwin Joea4069f42019-02-20 15:06:24947 TRACE_EVENT_WITH_FLOW0("renderer", "RenderWidget::OnWasShown", routing_id(),
948 TRACE_EVENT_FLAG_FLOW_IN);
danakj678f0252018-11-09 21:46:34949
tasakb95dbb50c2017-02-08 18:07:50950 was_shown_time_ = base::TimeTicks::Now();
danakj14e00a8322019-02-07 17:12:06951
[email protected]bee16aab2009-08-26 15:55:03952 SetHidden(false);
Sebastien Marchand5f06ca32019-05-17 18:03:42953 if (record_tab_switch_time_request) {
danakja6c10012018-07-06 14:25:36954 layer_tree_view_->layer_tree_host()->RequestPresentationTimeForNextFrame(
Sebastien Marchand5f06ca32019-05-17 18:03:42955 tab_switch_time_recorder_.TabWasShown(
956 false /* has_saved_frames */,
957 record_tab_switch_time_request.value(), show_request_timestamp));
[email protected]3399dd822014-08-09 11:14:24958 }
danakj14e00a8322019-02-07 17:12:06959
960 for (auto& observer : render_frames_)
961 observer.WasShown();
962 if (was_evicted) {
963 for (auto& observer : render_frame_proxies_)
964 observer.WasEvicted();
965 }
initial.commit09911bf2008-07-26 23:55:29966}
967
Avi Drissman014dec72018-06-07 02:34:01968void RenderWidget::OnRequestSetBoundsAck() {
[email protected]53d3f302009-12-21 04:42:05969 DCHECK(pending_window_rect_count_);
970 pending_window_rect_count_--;
971}
972
Albert J. Wongcb004632018-07-10 22:58:25973void RenderWidget::OnForceRedraw(int snapshot_id) {
David Bokanf92eaf22019-03-01 01:54:51974 RequestPresentation(base::BindOnce(&RenderWidget::DidPresentForceDrawFrame,
975 weak_ptr_factory_.GetWeakPtr(),
976 snapshot_id));
977}
978
979void RenderWidget::RequestPresentation(PresentationTimeCallback callback) {
danakj125a29e2019-01-24 23:25:29980 layer_tree_view_->layer_tree_host()->RequestPresentationTimeForNextFrame(
David Bokanf92eaf22019-03-01 01:54:51981 std::move(callback));
danakjda15621e2019-04-05 20:34:43982 layer_tree_view_->layer_tree_host()->SetNeedsCommitWithForcedRedraw();
Albert J. Wongcb004632018-07-10 22:58:25983}
984
985void RenderWidget::DidPresentForceDrawFrame(
986 int snapshot_id,
987 const gfx::PresentationFeedback& feedback) {
Albert J. Wong3c93c182018-09-27 17:29:43988 Send(new WidgetHostMsg_ForceRedrawComplete(routing_id(), snapshot_id));
Albert J. Wongcb004632018-07-10 22:58:25989}
990
Xianzhu Wangf02017ac2018-10-17 01:47:33991viz::FrameSinkId RenderWidget::GetFrameSinkIdAtPoint(const gfx::PointF& point,
Ken Buchanan44d7e2f2018-08-23 14:18:05992 gfx::PointF* local_point) {
993 return input_handler_->GetFrameSinkIdAtPoint(point, local_point);
Navid Zolghadr0d86e5f2017-10-23 18:09:22994}
995
danakj77821e252019-03-27 19:58:04996bool RenderWidget::HasPendingPageScaleAnimation() const {
997 return layer_tree_view_->layer_tree_host()->HasPendingPageScaleAnimation();
998}
999
danakj327a4fe2019-01-26 00:00:351000bool RenderWidget::HandleInputEvent(
dtapuska9ec1a912017-04-21 15:18:311001 const blink::WebCoalescedInputEvent& input_event,
1002 const ui::LatencyInfo& latency_info,
Dave Tapuskaea83d3bd2017-06-13 16:14:551003 HandledEventCallback callback) {
danakj327a4fe2019-01-26 00:00:351004 if (is_frozen_)
1005 return false;
Dave Tapuskaea83d3bd2017-06-13 16:14:551006 input_handler_->HandleInputEvent(input_event, latency_info,
1007 std::move(callback));
danakj327a4fe2019-01-26 00:00:351008 return true;
dtapuska9ec1a912017-04-21 15:18:311009}
1010
danakj125a29e2019-01-24 23:25:291011void RenderWidget::SetNeedsMainFrame() {
danakj0719e0652019-03-14 21:19:111012 ScheduleAnimation();
danakj125a29e2019-01-24 23:25:291013}
1014
dtapuska9d46ef7d2017-05-26 19:06:061015scoped_refptr<MainThreadEventQueue> RenderWidget::GetInputEventQueue() {
1016 return input_event_queue_;
1017}
1018
fsamuel78f86e42016-01-20 04:10:231019void RenderWidget::OnCursorVisibilityChange(bool is_visible) {
lfg8ff33912016-09-13 20:59:211020 if (GetWebWidget())
Blink Reformat1c4d759e2017-04-09 16:34:541021 GetWebWidget()->SetCursorVisibilityState(is_visible);
fsamuel78f86e42016-01-20 04:10:231022}
1023
[email protected]76b082c2019-03-22 03:45:461024void RenderWidget::OnFallbackCursorModeToggled(bool is_on) {
1025 if (GetWebWidget())
1026 GetWebWidget()->OnFallbackCursorModeToggled(is_on);
1027}
1028
fsamuel78f86e42016-01-20 04:10:231029void RenderWidget::OnMouseCaptureLost() {
lfg8ff33912016-09-13 20:59:211030 if (GetWebWidget())
Blink Reformat1c4d759e2017-04-09 16:34:541031 GetWebWidget()->MouseCaptureLost();
fsamuel78f86e42016-01-20 04:10:231032}
1033
alexmos56567492016-09-13 00:52:461034void RenderWidget::OnSetEditCommandsForNextKeyEvent(
1035 const EditCommands& edit_commands) {
1036 edit_commands_ = edit_commands;
1037}
1038
Albert J. Wongcb004632018-07-10 22:58:251039void RenderWidget::OnSetActive(bool active) {
Albert J. Wong7bbf22d2018-12-20 00:27:271040 if (delegate())
1041 delegate()->SetActiveForWidget(active);
Albert J. Wongcb004632018-07-10 22:58:251042}
1043
fsamuel78f86e42016-01-20 04:10:231044void RenderWidget::OnSetFocus(bool enable) {
Albert J. Wong7bbf22d2018-12-20 00:27:271045 if (delegate())
1046 delegate()->DidReceiveSetFocusEventForWidget();
danakja2c9d0a92018-07-25 20:01:181047 SetFocus(enable);
1048}
1049
1050void RenderWidget::SetFocus(bool enable) {
lfge0c2792ec2016-05-11 18:52:081051 has_focus_ = enable;
1052
lfg8ff33912016-09-13 20:59:211053 if (GetWebWidget())
Blink Reformat1c4d759e2017-04-09 16:34:541054 GetWebWidget()->SetFocus(enable);
lfge0c2792ec2016-05-11 18:52:081055
ericwilligers88e69742016-10-17 19:29:551056 for (auto& observer : render_frames_)
1057 observer.RenderWidgetSetFocus(enable);
danakja2c9d0a92018-07-25 20:01:181058
Albert J. Wong7bbf22d2018-12-20 00:27:271059 if (delegate())
1060 delegate()->DidChangeFocusForWidget();
fsamuel78f86e42016-01-20 04:10:231061}
1062
1063///////////////////////////////////////////////////////////////////////////////
danakja6c10012018-07-06 14:25:361064// LayerTreeViewDelegate
fsamuel78f86e42016-01-20 04:10:231065
David Bokanc8e38d02018-10-08 21:56:011066void RenderWidget::ApplyViewportChanges(
1067 const cc::ApplyViewportChangesArgs& args) {
Ken Buchananed449bb2018-02-01 21:02:051068 if (!GetWebWidget())
1069 return;
David Bokanc8e38d02018-10-08 21:56:011070 GetWebWidget()->ApplyViewportChanges(args);
fsamuel78f86e42016-01-20 04:10:231071}
1072
Allison Pastewka5ac6e7eb2019-06-11 23:13:471073void RenderWidget::RecordManipulationTypeCounts(cc::ManipulationInfo info) {
Ken Buchananed449bb2018-02-01 21:02:051074 if (!GetWebWidget())
1075 return;
Allison Pastewka5ac6e7eb2019-06-11 23:13:471076 GetWebWidget()->RecordManipulationTypeCounts(info);
sahel1b47fda72017-03-28 17:03:071077}
1078
Sahel Sharify676580c2019-01-10 21:49:491079void RenderWidget::SendOverscrollEventFromImplSide(
1080 const gfx::Vector2dF& overscroll_delta,
1081 cc::ElementId scroll_latched_element_id) {
1082 if (!GetWebWidget())
1083 return;
1084 GetWebWidget()->SendOverscrollEventFromImplSide(overscroll_delta,
1085 scroll_latched_element_id);
1086}
1087void RenderWidget::SendScrollEndEventFromImplSide(
1088 cc::ElementId scroll_latched_element_id) {
1089 if (!GetWebWidget())
1090 return;
1091 GetWebWidget()->SendScrollEndEventFromImplSide(scroll_latched_element_id);
1092}
1093
Daniel Cheng224569ee2018-04-25 05:45:061094void RenderWidget::BeginMainFrame(base::TimeTicks frame_time) {
Ken Buchananed449bb2018-02-01 21:02:051095 if (!GetWebWidget())
1096 return;
dtapuska3d5624d32016-08-30 04:34:001097
Stephen Chenneyd56b5382019-01-29 00:37:021098 // We record metrics only when running in multi-threaded mode, not
1099 // single-thread mode for testing.
1100 bool record_main_frame_metrics =
1101 !!compositor_deps_->GetCompositorImplThreadTaskRunner();
1102 if (input_event_queue_) {
1103 base::Optional<ScopedUkmRafAlignedInputTimer> ukm_timer;
Stephen Chenney1d064cf2019-06-06 14:54:101104 if (record_main_frame_metrics) {
Stephen Chenneyd56b5382019-01-29 00:37:021105 ukm_timer.emplace(GetWebWidget());
Stephen Chenney1d064cf2019-06-06 14:54:101106 }
Stephen Chenneyd56b5382019-01-29 00:37:021107 input_event_queue_->DispatchRafAlignedInput(frame_time);
1108 }
1109
Stephen Chenney1d064cf2019-06-06 14:54:101110 // The input handler wants to know about the main frame for metric purposes
1111 DCHECK(widget_input_handler_manager_);
1112 widget_input_handler_manager_->MarkBeginMainFrame();
1113
Stephen Chenneyd56b5382019-01-29 00:37:021114 GetWebWidget()->BeginFrame(frame_time, record_main_frame_metrics);
fsamuel78f86e42016-01-20 04:10:231115}
1116
Stefan Zager148248c2019-02-20 23:24:081117void RenderWidget::DidBeginMainFrame() {
1118 if (!GetWebWidget())
1119 return;
1120 GetWebWidget()->DidBeginFrame();
1121}
1122
danakjc7afae52017-06-20 21:12:411123void RenderWidget::RequestNewLayerTreeFrameSink(
danakja6c10012018-07-06 14:25:361124 LayerTreeFrameSinkCallback callback) {
[email protected]7912e822014-04-16 02:37:031125 // For widgets that are never visible, we don't start the compositor, so we
danakjc7afae52017-06-20 21:12:411126 // never get a request for a cc::LayerTreeFrameSink.
sievers71c62dd52015-10-07 01:44:391127 DCHECK(!compositor_never_visible_);
danakjf87460792018-12-04 20:14:221128 // Frozen RenderWidgets should not be doing any compositing.
Scott Violetb04f16c2018-11-27 00:48:461129 DCHECK(!is_frozen_);
danakj678f0252018-11-09 21:46:341130
1131 if (is_closing()) {
1132 // In this case, we drop the request which means the compositor waits
1133 // forever, which is fine since we're going to destroy it.
1134 return;
1135 }
jonrossa2ff4f82018-02-16 17:27:461136
danakjf87460792018-12-04 20:14:221137 // If we have a warmup in progress, wait for that and store the callback
1138 // to be run when the warmup completes.
1139 if (warmup_frame_sink_request_pending_) {
1140 after_warmup_callback_ = std::move(callback);
1141 return;
1142 }
1143 // If a warmup previously completed, use the result.
1144 if (warmup_frame_sink_) {
1145 std::move(callback).Run(std::move(warmup_frame_sink_));
1146 return;
1147 }
1148
1149 DoRequestNewLayerTreeFrameSink(std::move(callback));
1150}
1151
1152void RenderWidget::DoRequestNewLayerTreeFrameSink(
1153 LayerTreeFrameSinkCallback callback) {
jonrossa2ff4f82018-02-16 17:27:461154 // TODO(jonross): have this generated by the LayerTreeFrameSink itself, which
1155 // would then handle binding.
1156 mojom::RenderFrameMetadataObserverPtr ptr;
1157 mojom::RenderFrameMetadataObserverRequest request = mojo::MakeRequest(&ptr);
1158 mojom::RenderFrameMetadataObserverClientPtrInfo client_info;
1159 mojom::RenderFrameMetadataObserverClientRequest client_request =
1160 mojo::MakeRequest(&client_info);
danakj4b347212018-07-04 17:55:171161 auto render_frame_metadata_observer =
1162 std::make_unique<RenderFrameMetadataObserverImpl>(std::move(request),
1163 std::move(client_info));
danakja6c10012018-07-06 14:25:361164 layer_tree_view_->SetRenderFrameObserver(
danakj4b347212018-07-04 17:55:171165 std::move(render_frame_metadata_observer));
danakjf51754372018-12-13 23:20:011166 GURL url = GetWebWidget()->GetURLForDebugTrace();
danakjefabf312018-10-05 19:03:071167 // The |url| is not always available, fallback to a fixed string.
1168 if (url.is_empty())
1169 url = GURL("chrome://gpu/RenderWidget::RequestNewLayerTreeFrameSink");
danakj6dcbc5962018-11-16 16:45:421170 // TODO(danakj): This may not be accurate, depending on the intent. A child
1171 // local root could be in the same process as the view, so if the client is
1172 // meant to designate the process type, it seems kRenderer would be the
1173 // correct choice. If client is meant to designate the widget type, then
1174 // kOOPIF would denote that it is not for the main frame. However, kRenderer
1175 // would also be used for other widgets such as popups.
1176 const char* client_name = for_child_local_root_frame_ ? kOOPIF : kRenderer;
danakj2d2889a2019-04-05 21:22:081177 compositor_deps_->RequestNewLayerTreeFrameSink(
Scott Violetb04f16c2018-11-27 00:48:461178 routing_id_, frame_swap_message_queue_, std::move(url),
1179 std::move(callback), std::move(client_request), std::move(ptr),
1180 client_name);
[email protected]ba91a792013-02-06 09:48:281181}
1182
fsamuel78f86e42016-01-20 04:10:231183void RenderWidget::DidCommitAndDrawCompositorFrame() {
1184 // NOTE: Tests may break if this event is renamed or moved. See
1185 // tab_capture_performancetest.cc.
1186 TRACE_EVENT0("gpu", "RenderWidget::DidCommitAndDrawCompositorFrame");
lfge0c2792ec2016-05-11 18:52:081187
Stephen Chenney1d064cf2019-06-06 14:54:101188 // The input handler wants to know about the commit for metric purposes
1189 DCHECK(widget_input_handler_manager_);
1190 widget_input_handler_manager_->MarkCompositorCommit();
1191
ericwilligers88e69742016-10-17 19:29:551192 for (auto& observer : render_frames_)
1193 observer.DidCommitAndDrawCompositorFrame();
lfge0c2792ec2016-05-11 18:52:081194
fsamuel78f86e42016-01-20 04:10:231195 // Notify subclasses that we initiated the paint operation.
1196 DidInitiatePaint();
Yiming Zhou0f8836c22018-09-13 22:22:111197
Albert J. Wongfb64e142018-10-16 01:10:451198 Send(new WidgetHostMsg_DidCommitAndDrawCompositorFrame(routing_id_));
fsamuel78f86e42016-01-20 04:10:231199}
1200
Stephen Chenney23880392019-04-02 18:36:541201void RenderWidget::WillCommitCompositorFrame() {
1202 if (GetWebWidget())
1203 GetWebWidget()->BeginCommitCompositorFrame();
1204}
1205
Saman Sami766e0b02018-01-31 17:19:201206void RenderWidget::DidCommitCompositorFrame() {
Albert J. Wong7bbf22d2018-12-20 00:27:271207 if (delegate())
1208 delegate()->DidCommitCompositorFrameForWidget();
Stephen Chenney23880392019-04-02 18:36:541209 if (GetWebWidget())
1210 GetWebWidget()->EndCommitCompositorFrame();
Saman Sami766e0b02018-01-31 17:19:201211}
fsamuel78f86e42016-01-20 04:10:231212
danakja2c9d0a92018-07-25 20:01:181213void RenderWidget::DidCompletePageScaleAnimation() {
Albert J. Wong7bbf22d2018-12-20 00:27:271214 if (delegate())
1215 delegate()->DidCompletePageScaleAnimationForWidget();
danakja2c9d0a92018-07-25 20:01:181216}
fsamuel78f86e42016-01-20 04:10:231217
danakjcd417502019-03-13 16:13:211218void RenderWidget::SetLayerTreeMutator(
1219 std::unique_ptr<cc::LayerTreeMutator> mutator) {
1220 layer_tree_view_->layer_tree_host()->SetLayerTreeMutator(std::move(mutator));
1221}
1222
Xida Chen83f1a0d12019-03-21 18:21:301223void RenderWidget::SetPaintWorkletLayerPainterClient(
1224 std::unique_ptr<cc::PaintWorkletLayerPainter> client) {
1225 layer_tree_view_->layer_tree_host()->SetPaintWorkletLayerPainter(
1226 std::move(client));
1227}
1228
danakj26b99912019-02-08 18:58:191229void RenderWidget::SetRootLayer(scoped_refptr<cc::Layer> layer) {
1230 layer_tree_view_->layer_tree_host()->SetRootLayer(std::move(layer));
1231}
1232
danakj53f46b82018-12-11 20:55:551233void RenderWidget::ScheduleAnimation() {
1234 // This call is not needed in single thread mode for tests without a
danakj0719e0652019-03-14 21:19:111235 // scheduler, but they override this method in order to schedule a synchronous
1236 // composite task themselves.
danakj53f46b82018-12-11 20:55:551237 layer_tree_view_->SetNeedsBeginFrame();
initial.commit09911bf2008-07-26 23:55:291238}
1239
danakjefcb8432019-01-16 18:47:141240void RenderWidget::SetShowFPSCounter(bool show) {
1241 cc::LayerTreeHost* host = layer_tree_view_->layer_tree_host();
1242 cc::LayerTreeDebugState debug_state = host->GetDebugState();
1243 debug_state.show_fps_counter = show;
1244 host->SetDebugState(debug_state);
1245}
1246
rnasricabcba42019-06-07 15:30:331247void RenderWidget::SetShowLayoutShiftRegions(bool show) {
1248 cc::LayerTreeHost* host = layer_tree_view_->layer_tree_host();
1249 cc::LayerTreeDebugState debug_state = host->GetDebugState();
1250 debug_state.show_layout_shift_regions = show;
1251 host->SetDebugState(debug_state);
1252}
1253
danakje691e852019-01-16 22:49:041254void RenderWidget::SetShowPaintRects(bool show) {
1255 cc::LayerTreeHost* host = layer_tree_view_->layer_tree_host();
1256 cc::LayerTreeDebugState debug_state = host->GetDebugState();
1257 debug_state.show_paint_rects = show;
1258 host->SetDebugState(debug_state);
1259}
1260
1261void RenderWidget::SetShowDebugBorders(bool show) {
1262 cc::LayerTreeHost* host = layer_tree_view_->layer_tree_host();
1263 cc::LayerTreeDebugState debug_state = host->GetDebugState();
1264 if (show)
1265 debug_state.show_debug_borders.set();
1266 else
1267 debug_state.show_debug_borders.reset();
1268 host->SetDebugState(debug_state);
1269}
1270
1271void RenderWidget::SetShowScrollBottleneckRects(bool show) {
1272 cc::LayerTreeHost* host = layer_tree_view_->layer_tree_host();
1273 cc::LayerTreeDebugState debug_state = host->GetDebugState();
1274 debug_state.show_touch_event_handler_rects = show;
1275 debug_state.show_wheel_event_handler_rects = show;
1276 debug_state.show_non_fast_scrollable_rects = show;
1277 host->SetDebugState(debug_state);
1278}
1279
1280void RenderWidget::SetShowHitTestBorders(bool show) {
1281 cc::LayerTreeHost* host = layer_tree_view_->layer_tree_host();
1282 cc::LayerTreeDebugState debug_state = host->GetDebugState();
1283 debug_state.show_hit_test_borders = show;
1284 host->SetDebugState(debug_state);
1285}
1286
danakj40d07932019-02-28 18:07:101287void RenderWidget::SetBackgroundColor(SkColor color) {
1288 layer_tree_view_->layer_tree_host()->set_background_color(color);
1289}
1290
Stephen Chenneyd56b5382019-01-29 00:37:021291void RenderWidget::UpdateVisualState() {
Ken Buchananed449bb2018-02-01 21:02:051292 if (!GetWebWidget())
1293 return;
1294
Stephen Chenneyd56b5382019-01-29 00:37:021295 // We record metrics only when running in multi-threaded mode, not
1296 // single-thread mode for testing.
1297 bool record_main_frame_metrics =
1298 !!compositor_deps_->GetCompositorImplThreadTaskRunner();
1299
Stephen Chenneyec4312612018-11-21 12:40:161300 // When recording main frame metrics set the lifecycle reason to
1301 // kBeginMainFrame, because this is the calller of UpdateLifecycle
1302 // for the main frame. Otherwise, set the reason to kTests, which is
1303 // the oinly other reason this method is called.
1304 WebWidget::LifecycleUpdateReason lifecycle_reason =
1305 record_main_frame_metrics
1306 ? WebWidget::LifecycleUpdateReason::kBeginMainFrame
1307 : WebWidget::LifecycleUpdateReason::kTest;
1308 GetWebWidget()->UpdateLifecycle(WebWidget::LifecycleUpdate::kAll,
1309 lifecycle_reason);
Blink Reformat1c4d759e2017-04-09 16:34:541310 GetWebWidget()->SetSuppressFrameRequestsWorkaroundFor704763Only(false);
tasakb95dbb50c2017-02-08 18:07:501311
Robert Flack3ef2ffb2018-09-06 20:38:201312 if (first_update_visual_state_after_hidden_) {
Takashi SAKAMOTO870f6262017-08-22 04:08:271313 RecordTimeToFirstActivePaint();
1314 first_update_visual_state_after_hidden_ = false;
1315 }
1316}
tasakb95dbb50c2017-02-08 18:07:501317
Takashi SAKAMOTO870f6262017-08-22 04:08:271318void RenderWidget::RecordTimeToFirstActivePaint() {
tasakb95dbb50c2017-02-08 18:07:501319 RenderThreadImpl* render_thread_impl = RenderThreadImpl::current();
tasakb95dbb50c2017-02-08 18:07:501320 base::TimeDelta sample = base::TimeTicks::Now() - was_shown_time_;
Takashi SAKAMOTO870f6262017-08-22 04:08:271321 if (render_thread_impl->NeedsToRecordFirstActivePaint(TTFAP_AFTER_PURGED)) {
1322 UMA_HISTOGRAM_TIMES("PurgeAndSuspend.Experimental.TimeToFirstActivePaint",
1323 sample);
1324 }
1325 if (render_thread_impl->NeedsToRecordFirstActivePaint(
1326 TTFAP_5MIN_AFTER_BACKGROUNDED)) {
1327 UMA_HISTOGRAM_TIMES(
1328 "PurgeAndSuspend.Experimental.TimeToFirstActivePaint."
1329 "AfterBackgrounded.5min",
1330 sample);
1331 }
fsamuel78f86e42016-01-20 04:10:231332}
1333
Stephen Chenneyd56b5382019-01-29 00:37:021334void RenderWidget::RecordStartOfFrameMetrics() {
Stephen Chenney23880392019-04-02 18:36:541335 if (GetWebWidget())
1336 GetWebWidget()->RecordStartOfFrameMetrics();
Stephen Chenneyd56b5382019-01-29 00:37:021337}
1338
Stephen Chenney9879a5c2018-09-26 14:21:421339void RenderWidget::RecordEndOfFrameMetrics(base::TimeTicks frame_begin_time) {
Stephen Chenney23880392019-04-02 18:36:541340 if (GetWebWidget())
1341 GetWebWidget()->RecordEndOfFrameMetrics(frame_begin_time);
1342}
Stephen Chenney9879a5c2018-09-26 14:21:421343
Stephen Chenney23880392019-04-02 18:36:541344void RenderWidget::BeginUpdateLayers() {
1345 if (GetWebWidget())
1346 GetWebWidget()->BeginUpdateLayers();
1347}
1348
1349void RenderWidget::EndUpdateLayers() {
1350 if (GetWebWidget())
Stephen Chenneyc3c5987d2019-04-15 20:40:071351 GetWebWidget()->EndUpdateLayers();
Stephen Chenney9879a5c2018-09-26 14:21:421352}
1353
fsamuel78f86e42016-01-20 04:10:231354void RenderWidget::WillBeginCompositorFrame() {
1355 TRACE_EVENT0("gpu", "RenderWidget::willBeginCompositorFrame");
1356
Ken Buchananed449bb2018-02-01 21:02:051357 if (!GetWebWidget())
1358 return;
1359
Blink Reformat1c4d759e2017-04-09 16:34:541360 GetWebWidget()->SetSuppressFrameRequestsWorkaroundFor704763Only(true);
alancutter317a8e02017-04-05 10:09:291361
dglazkov06854c5c2016-08-31 00:19:591362 // The UpdateTextInputState can result in further layout and possibly
1363 // enable GPU acceleration so they need to be called before any painting
1364 // is done.
changwan75e3b2072017-01-16 02:55:001365 UpdateTextInputState();
dglazkov06854c5c2016-08-31 00:19:591366 UpdateSelectionBounds();
1367
ericwilligers88e69742016-10-17 19:29:551368 for (auto& observer : render_frame_proxies_)
1369 observer.WillBeginCompositorFrame();
initial.commit09911bf2008-07-26 23:55:291370}
1371
fsamuel72464894f2015-12-15 06:59:311372///////////////////////////////////////////////////////////////////////////////
1373// RenderWidgetInputHandlerDelegate
1374
avid7d6b2e2016-03-04 19:41:171375void RenderWidget::FocusChangeComplete() {
Ehsan Karamad655d7b8a2018-01-12 18:38:381376 blink::WebFrameWidget* frame_widget = GetFrameWidget();
1377 if (!frame_widget)
ekaramad27ca69b12017-04-20 18:34:291378 return;
Ehsan Karamad655d7b8a2018-01-12 18:38:381379
ekaramad27ca69b12017-04-20 18:34:291380 blink::WebLocalFrame* focused =
Ehsan Karamad655d7b8a2018-01-12 18:38:381381 frame_widget->LocalRoot()->View()->FocusedFrame();
1382
ekaramad27ca69b12017-04-20 18:34:291383 if (focused && focused->AutofillClient())
1384 focused->AutofillClient()->DidCompleteFocusChangeInFrame();
avid7d6b2e2016-03-04 19:41:171385}
fsamuel72464894f2015-12-15 06:59:311386
dtapuska1827dd22016-03-11 15:24:591387void RenderWidget::ObserveGestureEventAndResult(
1388 const blink::WebGestureEvent& gesture_event,
1389 const gfx::Vector2dF& unused_delta,
Sandra Sunac5cdd832017-12-11 03:27:131390 const cc::OverscrollBehavior& overscroll_behavior,
dtapuska1827dd22016-03-11 15:24:591391 bool event_processed) {
1392 if (!compositor_deps_->IsElasticOverscrollEnabled())
1393 return;
1394
1395 cc::InputHandlerScrollResult scroll_result;
1396 scroll_result.did_scroll = event_processed;
1397 scroll_result.did_overscroll_root = !unused_delta.IsZero();
1398 scroll_result.unused_scroll_delta = unused_delta;
Sandra Sunac5cdd832017-12-11 03:27:131399 scroll_result.overscroll_behavior = overscroll_behavior;
dtapuska1827dd22016-03-11 15:24:591400
Dave Tapuska04bc5ee92018-04-17 19:03:311401 widget_input_handler_manager_->ObserveGestureEventOnMainThread(gesture_event,
1402 scroll_result);
dtapuska1827dd22016-03-11 15:24:591403}
1404
avid7d6b2e2016-03-04 19:41:171405void RenderWidget::OnDidHandleKeyEvent() {
alexmos56567492016-09-13 00:52:461406 ClearEditCommands();
1407}
1408
1409void RenderWidget::SetEditCommandForNextKeyEvent(const std::string& name,
1410 const std::string& value) {
1411 ClearEditCommands();
1412 edit_commands_.emplace_back(name, value);
1413}
1414
1415void RenderWidget::ClearEditCommands() {
1416 edit_commands_.clear();
avid7d6b2e2016-03-04 19:41:171417}
fsamuel72464894f2015-12-15 06:59:311418
chongza8ba91fc2016-08-16 21:39:171419void RenderWidget::OnDidOverscroll(const ui::DidOverscrollParams& params) {
Dave Tapuska04bc5ee92018-04-17 19:03:311420 if (mojom::WidgetInputHandlerHost* host =
1421 widget_input_handler_manager_->GetWidgetInputHandlerHost()) {
1422 host->DidOverscroll(params);
Dave Tapuska139a72f2017-09-06 21:57:031423 }
fsamuel72464894f2015-12-15 06:59:311424}
1425
fsamuele8326c742016-01-12 00:49:391426void RenderWidget::SetInputHandler(RenderWidgetInputHandler* input_handler) {
1427 // Nothing to do here. RenderWidget created the |input_handler| and will take
1428 // ownership of it. We just verify here that we don't already have an input
1429 // handler.
1430 DCHECK(!input_handler_);
1431}
1432
changwan75e3b2072017-01-16 02:55:001433void RenderWidget::ShowVirtualKeyboard() {
danakj125a29e2019-01-24 23:25:291434 // Blink can continue running and change input state between the Close IPC
1435 // and the task that actually closes this class.
1436 if (closing_)
1437 return;
changwan75e3b2072017-01-16 02:55:001438 UpdateTextInputStateInternal(true, false);
1439}
1440
changwan62f57292017-02-17 08:28:251441void RenderWidget::ClearTextInputState() {
1442 text_input_info_ = blink::WebTextInputInfo();
1443 text_input_type_ = ui::TextInputType::TEXT_INPUT_TYPE_NONE;
1444 text_input_mode_ = ui::TextInputMode::TEXT_INPUT_MODE_DEFAULT;
1445 can_compose_inline_ = false;
1446 text_input_flags_ = 0;
AJITH KUMAR V041c0b02017-08-08 10:39:201447 next_previous_flags_ = kInvalidNextPreviousFlagsValue;
changwan62f57292017-02-17 08:28:251448}
1449
changwan75e3b2072017-01-16 02:55:001450void RenderWidget::UpdateTextInputState() {
danakj125a29e2019-01-24 23:25:291451 // Blink can continue running and change input state between the Close IPC
1452 // and the task that actually closes this class.
1453 if (closing_)
1454 return;
changwan75e3b2072017-01-16 02:55:001455 UpdateTextInputStateInternal(false, false);
1456}
1457
1458void RenderWidget::UpdateTextInputStateInternal(bool show_virtual_keyboard,
1459 bool reply_to_request) {
fsamuel72464894f2015-12-15 06:59:311460 TRACE_EVENT0("renderer", "RenderWidget::UpdateTextInputState");
changwan75e3b2072017-01-16 02:55:001461
fsamuel72464894f2015-12-15 06:59:311462 if (ime_event_guard_) {
changwan75e3b2072017-01-16 02:55:001463 DCHECK(!reply_to_request);
1464 // show_virtual_keyboard should still be effective even if it was set inside
1465 // the IME
fsamuel72464894f2015-12-15 06:59:311466 // event guard.
changwan75e3b2072017-01-16 02:55:001467 if (show_virtual_keyboard)
1468 ime_event_guard_->set_show_virtual_keyboard(true);
fsamuel72464894f2015-12-15 06:59:311469 return;
1470 }
1471
dglazkov97b6c2b2016-10-25 17:35:551472 ui::TextInputType new_type = GetTextInputType();
1473 if (IsDateTimeInput(new_type))
1474 return; // Not considered as a text input field in WebKit/Chromium.
fsamuel72464894f2015-12-15 06:59:311475
dglazkov97b6c2b2016-10-25 17:35:551476 blink::WebTextInputInfo new_info;
ekaramad5aff1942017-01-06 01:26:351477 if (auto* controller = GetInputMethodController())
Blink Reformat1c4d759e2017-04-09 16:34:541478 new_info = controller->TextInputInfo();
dtapuskac4dd5be2016-10-25 15:11:101479 const ui::TextInputMode new_mode =
Blink Reformat1c4d759e2017-04-09 16:34:541480 ConvertWebTextInputMode(new_info.input_mode);
fsamuel72464894f2015-12-15 06:59:311481
1482 bool new_can_compose_inline = CanComposeInline();
1483
1484 // Only sends text input params if they are changed or if the ime should be
1485 // shown.
changwan75e3b2072017-01-16 02:55:001486 if (show_virtual_keyboard || reply_to_request ||
changwan38c3eb612016-12-09 01:45:561487 text_input_type_ != new_type || text_input_mode_ != new_mode ||
1488 text_input_info_ != new_info ||
1489 can_compose_inline_ != new_can_compose_inline) {
ekaramad9053e57b2016-04-26 20:00:381490 TextInputState params;
dglazkov97b6c2b2016-10-25 17:35:551491 params.type = new_type;
fsamuel72464894f2015-12-15 06:59:311492 params.mode = new_mode;
Dave Tapuskaf0698ee2019-05-21 21:12:331493 params.action = new_info.action;
fsamuel72464894f2015-12-15 06:59:311494 params.flags = new_info.flags;
AJITH KUMAR V9e7b5572017-11-14 00:54:211495#if defined(OS_ANDROID)
AJITH KUMAR V041c0b02017-08-08 10:39:201496 if (next_previous_flags_ == kInvalidNextPreviousFlagsValue) {
1497 // Due to a focus change, values will be reset by the frame.
1498 // That case we only need fresh NEXT/PREVIOUS information.
Albert J. Wong3c93c182018-09-27 17:29:431499 // Also we won't send WidgetHostMsg_TextInputStateChanged if next/previous
AJITH KUMAR V041c0b02017-08-08 10:39:201500 // focusable status is changed.
1501 if (auto* controller = GetInputMethodController()) {
1502 next_previous_flags_ =
1503 controller->ComputeWebTextInputNextPreviousFlags();
1504 } else {
1505 // For safety in case GetInputMethodController() is null, because -1 is
1506 // invalid value to send to browser process.
1507 next_previous_flags_ = 0;
1508 }
1509 }
AJITH KUMAR V9e7b5572017-11-14 00:54:211510#else
1511 next_previous_flags_ = 0;
1512#endif
AJITH KUMAR V041c0b02017-08-08 10:39:201513 params.flags |= next_previous_flags_;
Yuichiro Hanada1b19cf852019-01-18 16:12:261514 params.value = new_info.value.Utf16();
Blink Reformat1c4d759e2017-04-09 16:34:541515 params.selection_start = new_info.selection_start;
1516 params.selection_end = new_info.selection_end;
1517 params.composition_start = new_info.composition_start;
1518 params.composition_end = new_info.composition_end;
fsamuel72464894f2015-12-15 06:59:311519 params.can_compose_inline = new_can_compose_inline;
changwan75e3b2072017-01-16 02:55:001520 // TODO(changwan): change instances of show_ime_if_needed to
1521 // show_virtual_keyboard.
1522 params.show_ime_if_needed = show_virtual_keyboard;
1523 params.reply_to_request = reply_to_request;
Albert J. Wong3c93c182018-09-27 17:29:431524 Send(new WidgetHostMsg_TextInputStateChanged(routing_id(), params));
fsamuel72464894f2015-12-15 06:59:311525
1526 text_input_info_ = new_info;
dglazkov97b6c2b2016-10-25 17:35:551527 text_input_type_ = new_type;
fsamuel72464894f2015-12-15 06:59:311528 text_input_mode_ = new_mode;
1529 can_compose_inline_ = new_can_compose_inline;
1530 text_input_flags_ = new_info.flags;
Eric Karl706ea6712018-12-21 22:41:381531
1532#if defined(OS_ANDROID)
1533 // If we send a new TextInputStateChanged message, we must also deliver a
1534 // new RenderFrameMetadata, as the IME will need this info to be updated.
1535 // TODO(ericrk): Consider folding the above IPC into RenderFrameMetadata.
1536 // https://crbug.com/912309
danakj125a29e2019-01-24 23:25:291537 if (IsSurfaceSynchronizationEnabled()) {
Eric Karl706ea6712018-12-21 22:41:381538 layer_tree_view_->RequestForceSendMetadata();
1539 }
1540#endif
fsamuel72464894f2015-12-15 06:59:311541 }
1542}
1543
1544bool RenderWidget::WillHandleGestureEvent(const blink::WebGestureEvent& event) {
paulmeyer6ef5a792016-11-08 20:33:581545 possible_drag_event_info_.event_source =
1546 ui::DragDropTypes::DRAG_EVENT_SOURCE_TOUCH;
1547 possible_drag_event_info_.event_location =
Ella Ge1116059d2018-03-21 02:06:131548 gfx::ToFlooredPoint(event.PositionInScreen());
avid7d6b2e2016-03-04 19:41:171549
fsamuel72464894f2015-12-15 06:59:311550 return false;
1551}
1552
1553bool RenderWidget::WillHandleMouseEvent(const blink::WebMouseEvent& event) {
ericwilligers88e69742016-10-17 19:29:551554 for (auto& observer : render_frames_)
1555 observer.RenderWidgetWillHandleMouseEvent();
lfge0c2792ec2016-05-11 18:52:081556
paulmeyer6ef5a792016-11-08 20:33:581557 possible_drag_event_info_.event_source =
1558 ui::DragDropTypes::DRAG_EVENT_SOURCE_MOUSE;
1559 possible_drag_event_info_.event_location =
Blink Reformat1c4d759e2017-04-09 16:34:541560 gfx::Point(event.PositionInScreen().x, event.PositionInScreen().y);
paulmeyer6ef5a792016-11-08 20:33:581561
Albert J. Wong7bbf22d2018-12-20 00:27:271562 if (delegate())
1563 return delegate()->RenderWidgetWillHandleMouseEventForWidget(event);
avid7d6b2e2016-03-04 19:41:171564
fsamuel72464894f2015-12-15 06:59:311565 return false;
[email protected]fd847792013-10-24 17:12:351566}
1567
initial.commit09911bf2008-07-26 23:55:291568///////////////////////////////////////////////////////////////////////////////
mfomitchev2600fd7c2016-02-17 20:53:391569// RenderWidgetScreenMetricsDelegate
1570
bokanc63441c2016-04-27 15:49:121571void RenderWidget::ResizeWebWidget() {
danakja2c9d0a92018-07-25 20:01:181572 gfx::Size size = GetSizeForWebWidget();
Albert J. Wong7bbf22d2018-12-20 00:27:271573 if (delegate()) {
1574 delegate()->ResizeWebWidgetForWidget(size, top_controls_height_,
1575 bottom_controls_height_,
1576 browser_controls_shrink_blink_size_);
danakja2c9d0a92018-07-25 20:01:181577 return;
1578 }
1579 GetWebWidget()->Resize(size);
bokanc63441c2016-04-27 15:49:121580}
1581
1582gfx::Size RenderWidget::GetSizeForWebWidget() const {
danakj4b347212018-07-04 17:55:171583 if (compositor_deps_->IsUseZoomForDSFEnabled()) {
Fady Samuela863f152018-03-09 16:10:031584 return gfx::ScaleToCeiledSize(size_,
1585 GetOriginalScreenInfo().device_scale_factor);
1586 }
bokanc63441c2016-04-27 15:49:121587
1588 return size_;
1589}
1590
danakj0d963bd2019-02-20 18:00:461591gfx::Size RenderWidget::CompositorViewportSize() const {
1592 return layer_tree_view_->layer_tree_host()->device_viewport_size();
1593}
1594
akaba3483d8f2018-07-10 21:43:091595void RenderWidget::UpdateZoom(double zoom_level) {
akabac6bd1212018-06-25 20:10:481596 blink::WebFrameWidget* frame_widget = GetFrameWidget();
1597 if (!frame_widget)
1598 return;
1599 RenderFrameImpl* render_frame =
1600 RenderFrameImpl::FromWebFrame(frame_widget->LocalRoot());
1601
akaba3483d8f2018-07-10 21:43:091602 // Return early if zoom level is unchanged.
1603 if (render_frame->GetZoomLevel() == zoom_level) {
akabac6bd1212018-06-25 20:10:481604 return;
1605 }
1606
akaba3483d8f2018-07-10 21:43:091607 render_frame->SetZoomLevel(zoom_level);
akabac6bd1212018-06-25 20:10:481608
1609 for (auto& observer : render_frame_proxies_)
akaba3483d8f2018-07-10 21:43:091610 observer.OnZoomLevelChanged(zoom_level);
akabac6bd1212018-06-25 20:10:481611
1612 for (auto& plugin : browser_plugins_)
akaba3483d8f2018-07-10 21:43:091613 plugin.OnZoomLevelChanged(zoom_level);
akabac6bd1212018-06-25 20:10:481614}
1615
Fady Samuel799e72192018-04-25 21:16:571616void RenderWidget::SynchronizeVisualProperties(const VisualProperties& params) {
Miguel Casas5e1018052018-01-09 19:17:361617
Fady Samuele62be402018-03-29 03:19:531618 gfx::Size new_compositor_viewport_pixel_size =
1619 params.auto_resize_enabled
1620 ? gfx::ScaleToCeiledSize(size_,
1621 params.screen_info.device_scale_factor)
1622 : params.compositor_viewport_pixel_size;
Fady Samuel84d8a1f72018-11-08 00:26:171623 UpdateSurfaceAndScreenInfo(params.local_surface_id_allocation.value_or(
1624 viz::LocalSurfaceIdAllocation()),
1625 new_compositor_viewport_pixel_size,
1626 params.screen_info);
Vladimir Levin98d76dad2018-04-21 00:21:291627 UpdateCaptureSequenceNumber(params.capture_sequence_number);
danakj125a29e2019-01-24 23:25:291628 layer_tree_view_->SetBrowserControlsHeight(
1629 params.top_controls_height, params.bottom_controls_height,
1630 params.browser_controls_shrink_blink_size);
mfomitchev2600fd7c2016-02-17 20:53:391631
akaba3483d8f2018-07-10 21:43:091632 UpdateZoom(params.zoom_level);
akabac6bd1212018-06-25 20:10:481633
danakj21604ce2018-12-19 16:01:221634 if (!params.auto_resize_enabled) {
1635 visible_viewport_size_ = params.visible_viewport_size;
danakj21604ce2018-12-19 16:01:221636 display_mode_ = params.display_mode;
danakj21604ce2018-12-19 16:01:221637 size_ = params.new_size;
mfomitchev2600fd7c2016-02-17 20:53:391638
danakj21604ce2018-12-19 16:01:221639 ResizeWebWidget();
mfomitchev2600fd7c2016-02-17 20:53:391640
danakjb4cec102019-01-30 19:22:131641 gfx::Size visual_viewport_size = visible_viewport_size_;
danakj21604ce2018-12-19 16:01:221642 if (compositor_deps_->IsUseZoomForDSFEnabled()) {
danakjb4cec102019-01-30 19:22:131643 visual_viewport_size = gfx::ScaleToCeiledSize(
1644 visual_viewport_size, GetOriginalScreenInfo().device_scale_factor);
danakj21604ce2018-12-19 16:01:221645 }
W. James MacLean7c8581072019-06-18 18:07:531646 // When this function is invoked for a main frame widget, it is only if
1647 // (i) we are initializing the widget, or (ii) the widget supports a local
1648 // mainframe. For widgets supporting a remote main frame, the visual
1649 // viewport is updated through the RenderView.
1650 if (delegate())
1651 delegate()->ResizeVisualViewportForWidget(visual_viewport_size);
bokan71cb5b12016-04-27 03:45:221652
danakjf9284772019-01-23 18:29:141653 // NOTE: We may have entered fullscreen mode without changing our size.
1654 SetIsFullscreen(params.is_fullscreen_granted);
mfomitchev2600fd7c2016-02-17 20:53:391655 }
mfomitchev2600fd7c2016-02-17 20:53:391656}
1657
1658void RenderWidget::SetScreenMetricsEmulationParameters(
1659 bool enabled,
1660 const blink::WebDeviceEmulationParams& params) {
Albert J. Wong7bbf22d2018-12-20 00:27:271661 // This is only supported in RenderView, which has an delegate().
1662 DCHECK(delegate());
1663 delegate()->SetScreenMetricsEmulationParametersForWidget(enabled, params);
mfomitchev2600fd7c2016-02-17 20:53:391664}
1665
Albert J. Wong3c93c182018-09-27 17:29:431666void RenderWidget::SetScreenRects(const gfx::Rect& widget_screen_rect,
mfomitchev2600fd7c2016-02-17 20:53:391667 const gfx::Rect& window_screen_rect) {
Albert J. Wong3c93c182018-09-27 17:29:431668 widget_screen_rect_ = widget_screen_rect;
mfomitchev2600fd7c2016-02-17 20:53:391669 window_screen_rect_ = window_screen_rect;
1670}
1671
1672///////////////////////////////////////////////////////////////////////////////
[email protected]f98d7e3c2010-09-13 22:30:461673// WebWidgetClient
initial.commit09911bf2008-07-26 23:55:291674
Chris Harrelsond7ab99b2018-01-24 17:51:361675void RenderWidget::IntrinsicSizingInfoChanged(
1676 const blink::WebIntrinsicSizingInfo& sizing_info) {
Albert J. Wong3c93c182018-09-27 17:29:431677 Send(new WidgetHostMsg_IntrinsicSizingInfoChanged(routing_id_, sizing_info));
Chris Harrelsond7ab99b2018-01-24 17:51:361678}
1679
Blink Reformat1c4d759e2017-04-09 16:34:541680void RenderWidget::DidMeaningfulLayout(blink::WebMeaningfulLayout layout_type) {
1681 if (layout_type == blink::WebMeaningfulLayout::kVisuallyNonEmpty) {
Albert J. Wongfb64e142018-10-16 01:10:451682 QueueMessage(new WidgetHostMsg_DidFirstVisuallyNonEmptyPaint(routing_id_));
dglazkovf0e1d6d2015-10-10 02:13:481683 }
dglazkov79c426102015-08-31 21:22:431684
ericwilligers88e69742016-10-17 19:29:551685 for (auto& observer : render_frames_)
1686 observer.DidMeaningfulLayout(layout_type);
dglazkov79c426102015-08-31 21:22:431687}
1688
[email protected]586871b2014-07-22 17:05:111689// static
dchengcedca5612016-04-09 01:40:151690std::unique_ptr<cc::SwapPromise> RenderWidget::QueueMessageImpl(
[email protected]586871b2014-07-22 17:05:111691 IPC::Message* msg,
[email protected]586871b2014-07-22 17:05:111692 FrameSwapMessageQueue* frame_swap_message_queue,
1693 scoped_refptr<IPC::SyncMessageFilter> sync_message_filter,
[email protected]586871b2014-07-22 17:05:111694 int source_frame_number) {
[email protected]586871b2014-07-22 17:05:111695 bool first_message_for_frame = false;
Sadrul Habib Chowdhury4e0335cf2018-07-31 20:08:491696 frame_swap_message_queue->QueueMessageForFrame(
1697 source_frame_number, base::WrapUnique(msg), &first_message_for_frame);
danakj2d2889a2019-04-05 21:22:081698 if (!first_message_for_frame)
1699 return nullptr;
1700 return std::make_unique<QueueMessageSwapPromise>(
1701 sync_message_filter, frame_swap_message_queue, source_frame_number);
[email protected]586871b2014-07-22 17:05:111702}
1703
danakj53802692018-07-25 21:46:441704void RenderWidget::SetHandlingInputEvent(bool handling_input_event) {
1705 input_handler_->set_handling_input_event(handling_input_event);
1706}
1707
Sadrul Habib Chowdhury4e0335cf2018-07-31 20:08:491708void RenderWidget::QueueMessage(IPC::Message* msg) {
danakj125a29e2019-01-24 23:25:291709 if (closing_)
1710 return;
1711
[email protected]586871b2014-07-22 17:05:111712 // RenderThreadImpl::current() is NULL in some tests.
danakj125a29e2019-01-24 23:25:291713 if (!RenderThreadImpl::current()) {
[email protected]586871b2014-07-22 17:05:111714 Send(msg);
1715 return;
1716 }
1717
dchengcedca5612016-04-09 01:40:151718 std::unique_ptr<cc::SwapPromise> swap_promise =
Sadrul Habib Chowdhury4e0335cf2018-07-31 20:08:491719 QueueMessageImpl(msg, frame_swap_message_queue_.get(),
[email protected]586871b2014-07-22 17:05:111720 RenderThreadImpl::current()->sync_message_filter(),
danakja6c10012018-07-06 14:25:361721 layer_tree_view_->GetSourceFrameNumber());
danakj2d2889a2019-04-05 21:22:081722 if (swap_promise) {
1723 layer_tree_view_->layer_tree_host()->QueueSwapPromise(
1724 std::move(swap_promise));
1725 }
[email protected]586871b2014-07-22 17:05:111726}
1727
Blink Reformat1c4d759e2017-04-09 16:34:541728void RenderWidget::DidChangeCursor(const WebCursorInfo& cursor_info) {
[email protected]7c51b0ee2009-07-08 21:49:301729 // TODO(darin): Eliminate this temporary.
Mike Wassermanf95c2d9d2019-04-17 00:44:211730 WebCursor cursor((CursorInfo(cursor_info)));
initial.commit09911bf2008-07-26 23:55:291731 // Only send a SetCursor message if we need to make a change.
Navid Zolghadr293bb862018-12-12 18:15:411732 if (input_handler_->DidChangeCursor(cursor))
Albert J. Wong3c93c182018-09-27 17:29:431733 Send(new WidgetHostMsg_SetCursor(routing_id_, cursor));
initial.commit09911bf2008-07-26 23:55:291734}
1735
aelias5971e47d2017-06-16 02:39:381736void RenderWidget::AutoscrollStart(const blink::WebFloatPoint& point) {
Albert J. Wong3c93c182018-09-27 17:29:431737 Send(new WidgetHostMsg_AutoscrollStart(routing_id_, point));
aelias5971e47d2017-06-16 02:39:381738}
1739
1740void RenderWidget::AutoscrollFling(const blink::WebFloatSize& velocity) {
Albert J. Wong3c93c182018-09-27 17:29:431741 Send(new WidgetHostMsg_AutoscrollFling(routing_id_, velocity));
aelias5971e47d2017-06-16 02:39:381742}
1743
1744void RenderWidget::AutoscrollEnd() {
Albert J. Wong3c93c182018-09-27 17:29:431745 Send(new WidgetHostMsg_AutoscrollEnd(routing_id_));
aelias5971e47d2017-06-16 02:39:381746}
1747
initial.commit09911bf2008-07-26 23:55:291748// We are supposed to get a single call to Show for a newly created RenderWidget
1749// that was created via RenderWidget::CreateWebView. So, we wait until this
1750// point to dispatch the ShowWidget message.
1751//
1752// This method provides us with the information about how to display the newly
[email protected]5f9de5882011-09-30 23:36:281753// created RenderWidget (i.e., as a blocked popup or as a new tab).
initial.commit09911bf2008-07-26 23:55:291754//
Blink Reformat1c4d759e2017-04-09 16:34:541755void RenderWidget::Show(WebNavigationPolicy policy) {
danakj65f423b72018-10-10 17:21:201756 if (!show_callback_) {
Albert J. Wong7bbf22d2018-12-20 00:27:271757 if (delegate()) {
danakja2c9d0a92018-07-25 20:01:181758 // When SupportsMultipleWindows is disabled, popups are reusing
danakj65f423b72018-10-10 17:21:201759 // the view's RenderWidget. In some scenarios, this makes blink to call
1760 // Show() twice. But otherwise, if it is enabled, we should not visit
1761 // Show() more than once.
Albert J. Wong7bbf22d2018-12-20 00:27:271762 DCHECK(!delegate()->SupportsMultipleWindowsForWidget());
danakj65f423b72018-10-10 17:21:201763 return;
danakja2c9d0a92018-07-25 20:01:181764 } else {
danakj65f423b72018-10-10 17:21:201765 NOTREACHED() << "received extraneous Show call";
danakja2c9d0a92018-07-25 20:01:181766 }
danakja2c9d0a92018-07-25 20:01:181767 }
1768
initial.commit09911bf2008-07-26 23:55:291769 DCHECK(routing_id_ != MSG_ROUTING_NONE);
nickf7b38222016-11-22 21:59:351770
1771 // The opener is responsible for actually showing this widget.
danakjdf1ceb72018-07-18 20:02:251772 std::move(show_callback_).Run(this, policy, initial_rect_);
nickf7b38222016-11-22 21:59:351773
bokanc007c3a2015-02-03 07:15:561774 // NOTE: initial_rect_ may still have its default values at this point, but
[email protected]8de12d942010-11-17 20:42:441775 // that's okay. It'll be ignored if as_popup is false, or the browser
1776 // process will impose a default position otherwise.
bokanc007c3a2015-02-03 07:15:561777 SetPendingWindowRect(initial_rect_);
initial.commit09911bf2008-07-26 23:55:291778}
1779
danakj17216f4d2018-10-16 22:57:321780LayerTreeView* RenderWidget::InitializeLayerTreeView() {
1781 TRACE_EVENT0("blink", "RenderWidget::InitializeLayerTreeView");
danakj17216f4d2018-10-16 22:57:321782
1783 layer_tree_view_ = std::make_unique<LayerTreeView>(
1784 this, compositor_deps_->GetCompositorMainThreadTaskRunner(),
1785 compositor_deps_->GetCompositorImplThreadTaskRunner(),
1786 compositor_deps_->GetTaskGraphRunner(),
1787 compositor_deps_->GetWebMainThreadScheduler());
1788 layer_tree_view_->Initialize(
danakj6dcbc5962018-11-16 16:45:421789 GenerateLayerTreeSettings(compositor_deps_, for_child_local_root_frame_,
danakj17216f4d2018-10-16 22:57:321790 screen_info_.rect.size(),
1791 screen_info_.device_scale_factor),
1792 compositor_deps_->CreateUkmRecorderFactory());
1793
Fady Samuel1c1ad3692018-11-06 23:28:401794 UpdateSurfaceAndScreenInfo(local_surface_id_allocation_from_parent_,
danakj0d963bd2019-02-20 18:00:461795 CompositorViewportSize(), screen_info_);
danakj953af052019-02-06 19:13:181796 // If the widget is hidden, delay starting the compositor until the user shows
1797 // it. Also if the RenderWidget is frozen, we delay starting the compositor
1798 // until we expect to use the widget, which will be signaled through
1799 // WarmupCompositor().
1800 if (!is_frozen_ && !is_hidden_)
1801 StartStopCompositor();
danakj17216f4d2018-10-16 22:57:321802
danakj17216f4d2018-10-16 22:57:321803 DCHECK_NE(MSG_ROUTING_NONE, routing_id_);
1804 layer_tree_view_->SetFrameSinkId(
1805 viz::FrameSinkId(RenderThread::Get()->GetClientId(), routing_id_));
1806
1807 RenderThreadImpl* render_thread = RenderThreadImpl::current();
1808 if (render_thread) {
1809 input_event_queue_ = base::MakeRefCounted<MainThreadEventQueue>(
1810 this, render_thread->GetWebMainThreadScheduler()->InputTaskRunner(),
danakj953af052019-02-06 19:13:181811 render_thread->GetWebMainThreadScheduler(),
1812 /*allow_raf_aligned_input=*/!compositor_never_visible_);
danakj17216f4d2018-10-16 22:57:321813 }
1814
1815 return layer_tree_view_.get();
1816}
1817
danakj953af052019-02-06 19:13:181818void RenderWidget::StartStopCompositor() {
1819 if (compositor_never_visible_)
1820 return;
danakjde8b7482018-11-15 23:45:021821
danakj953af052019-02-06 19:13:181822 if (is_frozen_) {
1823 layer_tree_view_->SetVisible(false);
1824 // Drop all gpu resources, this makes SetVisible(true) more expensive/slower
1825 // but we don't expect to use this RenderWidget again until some possible
1826 // future navigation. This brings us a bit closer to emulating deleting the
1827 // RenderWidget instead of just stopping the compositor.
1828 layer_tree_view_->ReleaseLayerTreeFrameSink();
1829 } else if (is_hidden_) {
1830 layer_tree_view_->SetVisible(false);
1831 } else {
1832 layer_tree_view_->SetVisible(true);
1833 }
danakjde8b7482018-11-15 23:45:021834}
1835
danakj6dcbc5962018-11-16 16:45:421836void RenderWidget::SetIsFrozen(bool is_frozen) {
1837 DCHECK_NE(is_frozen, is_frozen_);
1838 is_frozen_ = is_frozen;
danakj953af052019-02-06 19:13:181839 // If hidden, then frozen changing doesn't change anything with the
1840 // compositor since when hidden the compositor is always stopped.
1841 if (!is_hidden_)
1842 StartStopCompositor();
danakjde8b7482018-11-15 23:45:021843}
1844
danakjf87460792018-12-04 20:14:221845void RenderWidget::WarmupCompositor() {
1846 DCHECK(is_frozen_);
danakj953af052019-02-06 19:13:181847 if (compositor_never_visible_)
danakjf87460792018-12-04 20:14:221848 return;
1849
1850 // Keeping things simple. This would cancel any outstanding warmup if we
1851 // happened to have one (this should be basically impossible). This avoids any
1852 // extra book keeping about the outstanding reqeust.
1853 warmup_weak_ptr_factory_.InvalidateWeakPtrs();
1854 // And if we already did a warmup then we're done.
1855 if (warmup_frame_sink_)
1856 return;
1857
1858 // Mark us pending the warmup frame sink *before* calling
1859 // DoRequestNewLayerTreeFrameSink() as it may run the reply callback
1860 // synchronously. So we don't want to change any state after the call
1861 // to DoRequestNewLayerTreeFrameSink() here.
1862 warmup_frame_sink_request_pending_ = true;
1863
1864 auto cb = base::BindOnce(&RenderWidget::OnReplyForWarmupCompositor,
1865 warmup_weak_ptr_factory_.GetWeakPtr());
1866 DoRequestNewLayerTreeFrameSink(std::move(cb));
1867}
1868
1869void RenderWidget::OnReplyForWarmupCompositor(
1870 std::unique_ptr<cc::LayerTreeFrameSink> sink) {
1871 warmup_frame_sink_request_pending_ = false;
1872
1873 if (after_warmup_callback_)
1874 std::move(after_warmup_callback_).Run(std::move(sink));
1875 else
1876 warmup_frame_sink_ = std::move(sink);
1877}
1878
1879void RenderWidget::AbortWarmupCompositor() {
1880 warmup_frame_sink_request_pending_ = false;
1881 // Drop any pending warmup.
1882 warmup_weak_ptr_factory_.InvalidateWeakPtrs();
1883 // And drop any completed one.
1884 warmup_frame_sink_.reset();
1885
1886 // If we had saved a callback to run after warmup, just do so now indicating
1887 // failure.
1888 if (after_warmup_callback_)
1889 std::move(after_warmup_callback_).Run(nullptr);
1890}
1891
[email protected]2533ce12009-05-09 00:02:241892void RenderWidget::DoDeferredClose() {
Albert J. Wong3c93c182018-09-27 17:29:431893 Send(new WidgetHostMsg_Close(routing_id_));
[email protected]2533ce12009-05-09 00:02:241894}
1895
danakj45e4a4f2019-03-12 22:50:421896void RenderWidget::ClosePopupWidgetSoon() {
1897 // Only should be called for popup widgets.
1898 DCHECK(!for_child_local_root_frame_);
1899 DCHECK(!delegate_);
1900
1901 CloseWidgetSoon();
1902}
1903
Blink Reformat1c4d759e2017-04-09 16:34:541904void RenderWidget::CloseWidgetSoon() {
Avi Drissman07d22452019-03-25 17:56:391905 DCHECK(RenderThread::IsMainThread());
danakj292b63e2018-11-19 20:47:061906 // Prevent compositor from setting up new IPC channels, since we know a
1907 // WidgetMsg_Close is coming. We do this immediately, not in DoDeferredClose,
1908 // as the caller (eg WebPagePopupImpl) may start tearing down things after
1909 // calling this method, including detaching the frame from this RenderWidget.
1910 // Then trying to make a LayerTreeFrameSink would crash.
1911 // https://crbug.com/906340
1912 host_will_close_this_ = true;
1913
initial.commit09911bf2008-07-26 23:55:291914 // If a page calls window.close() twice, we'll end up here twice, but that's
1915 // OK. It is safe to send multiple Close messages.
danakj292b63e2018-11-19 20:47:061916 //
[email protected]2533ce12009-05-09 00:02:241917 // Ask the RenderWidgetHost to initiate close. We could be called from deep
danakj295c43f182019-03-13 16:37:171918 // in Javascript. If we ask the RenderWidgetHost to close now, the window
1919 // could be closed before the JS finishes executing, thanks to nested message
1920 // loops running and handling the resuliting Close IPC. So instead, post a
[email protected]2533ce12009-05-09 00:02:241921 // message back to the message loop, which won't run until the JS is
danakj295c43f182019-03-13 16:37:171922 // complete, and then the Close request can be sent.
Hajime Hoshi315a61f2018-08-14 17:27:281923 GetCleanupTaskRunner()->PostTask(
1924 FROM_HERE, base::BindOnce(&RenderWidget::DoDeferredClose, this));
initial.commit09911bf2008-07-26 23:55:291925}
1926
1927void RenderWidget::Close() {
danakj6dcbc5962018-11-16 16:45:421928 // This was done immediately in the |for_child_local_root_frame_| case in the
1929 // OnClose() IPC handler.
1930 if (!for_child_local_root_frame_)
danakj5c5269b2018-11-06 00:39:111931 CloseWebWidget();
1932
danakja6c10012018-07-06 14:25:361933 layer_tree_view_.reset();
Albert J. Wong7bbf22d2018-12-20 00:27:271934 if (delegate())
1935 delegate()->DidCloseWidget();
Albert J. Wongd593de532018-09-06 05:42:151936 // Note the ACK is a control message going to the RenderProcessHost.
Albert J. Wong3c93c182018-09-27 17:29:431937 RenderThread::Get()->Send(new WidgetHostMsg_Close_ACK(routing_id()));
danakj6ec39d302019-03-15 22:43:221938 closed_ = true;
Ken Buchananed449bb2018-02-01 21:02:051939}
1940
1941void RenderWidget::CloseWebWidget() {
danakj35314ab2018-10-19 16:37:341942 // If the browser has not sent OnDisableDeviceEmulation, we have an emulator
1943 // hanging out still. Destroying it must happen *after* the IPC route is
1944 // removed so that another IPC does not arrive and re-create the emulator
1945 // during closing.
1946 //
1947 // This destruction is normally part of an IPC and expects objects to be alive
1948 // that would be alive while the IPC route is active such as the
1949 // |layer_tree_view_|. So we ensure that it is the first thing to be
1950 // destroyed here before deleting things from the RenderWidget or the
Albert J. Wong7bbf22d2018-12-20 00:27:271951 // delegate().
danakj35314ab2018-10-19 16:37:341952 //
1953 // TODO(danakj): The emulator could reset to non-emulated values in an
1954 // explicit method call (instead of in the destructor) that occurs when
1955 // emulation is disabled, but does not need to occur during RenderWidget
1956 // closing. Then we would not have to destroy this so carefully.
1957 screen_metrics_emulator_.reset();
1958
Albert J. Wong7bbf22d2018-12-20 00:27:271959 // When delegate() is present, the RenderWidget is for a main frame,
danakj974b8ae2019-02-05 00:26:261960 // and the GetWebWidget() will be a WebFrameWidget, which is not the same as
1961 // |webwidget_internal_|. The WebFrameWidget will be closed when the main
1962 // frame is detached, so we do not close it here. But it does not close the
1963 // |webwidget_internal_| since this class takes responsibility for that here
1964 // in all cases.
danakj5c5269b2018-11-06 00:39:111965 webwidget_internal_->Close();
1966 webwidget_internal_ = nullptr;
danakj2d2889a2019-04-05 21:22:081967
1968 close_weak_ptr_factory_.InvalidateWeakPtrs();
initial.commit09911bf2008-07-26 23:55:291969}
1970
Mitsuru Oshima3c07b332018-02-06 04:56:501971void RenderWidget::UpdateWebViewWithDeviceScaleFactor() {
1972 blink::WebFrameWidget* frame_widget = GetFrameWidget();
1973 blink::WebFrame* current_frame =
1974 frame_widget ? frame_widget->LocalRoot() : nullptr;
1975 blink::WebView* webview = current_frame ? current_frame->View() : nullptr;
1976 if (webview) {
danakj4b347212018-07-04 17:55:171977 if (compositor_deps_->IsUseZoomForDSFEnabled())
Fady Samuela863f152018-03-09 16:10:031978 webview->SetZoomFactorForDeviceScaleFactor(
1979 GetWebScreenInfo().device_scale_factor);
Mitsuru Oshima3c07b332018-02-06 04:56:501980 else
Fady Samuela863f152018-03-09 16:10:031981 webview->SetDeviceScaleFactor(GetWebScreenInfo().device_scale_factor);
Mitsuru Oshima3c07b332018-02-06 04:56:501982
1983 webview->GetSettings()->SetPreferCompositingToLCDTextEnabled(
Christopher Camerone9a30c12018-03-07 08:23:381984 PreferCompositingToLCDText(compositor_deps_,
Fady Samuela863f152018-03-09 16:10:031985 GetWebScreenInfo().device_scale_factor));
Mitsuru Oshima3c07b332018-02-06 04:56:501986 }
1987}
1988
Ehsan Karamad655d7b8a2018-01-12 18:38:381989blink::WebFrameWidget* RenderWidget::GetFrameWidget() const {
danakj89ae4532018-12-12 22:52:551990 // TODO(danakj): Remove this check and don't call this method for non-frames.
1991 if (!for_frame())
1992 return nullptr;
1993 // TODO(danakj): Is this needed? IPCs stop after closing, but code used to
1994 // check for a null WebWidget.
1995 if (closing_)
Ehsan Karamad655d7b8a2018-01-12 18:38:381996 return nullptr;
1997
danakj89ae4532018-12-12 22:52:551998 blink::WebWidget* widget;
Albert J. Wong7bbf22d2018-12-20 00:27:271999 if (delegate()) {
danakj89ae4532018-12-12 22:52:552000 // Main frame WebFrameWidgets are held by the delegate, the internal widget
2001 // points directly to the WebView.
2002 // TODO(ekaramad): We should drop IPCs when |is_frozen_| instead of
2003 // handling them and finding a null here. However there is also the case
2004 // of the frame being detached without the widget being frozen to be
2005 // resolved (https://crbug.com/906340). So for now this can return null.
Albert J. Wong7bbf22d2018-12-20 00:27:272006 widget = delegate()->GetWebWidgetForWidget();
danakj89ae4532018-12-12 22:52:552007 } else {
2008 // Subframes always have a WebFrameWidget themselves.
2009 widget = webwidget_internal_;
Ehsan Karamad655d7b8a2018-01-12 18:38:382010 }
danakj89ae4532018-12-12 22:52:552011 return static_cast<blink::WebFrameWidget*>(widget);
Ehsan Karamad655d7b8a2018-01-12 18:38:382012}
2013
bokan841fdc72016-10-06 00:16:352014void RenderWidget::ScreenRectToEmulatedIfNeeded(WebRect* window_rect) const {
2015 DCHECK(window_rect);
2016 float scale = popup_origin_scale_for_emulation_;
2017 if (!scale)
2018 return;
2019 window_rect->x =
2020 popup_view_origin_for_emulation_.x() +
2021 (window_rect->x - popup_screen_origin_for_emulation_.x()) / scale;
2022 window_rect->y =
2023 popup_view_origin_for_emulation_.y() +
2024 (window_rect->y - popup_screen_origin_for_emulation_.y()) / scale;
2025}
2026
2027void RenderWidget::EmulatedToScreenRectIfNeeded(WebRect* window_rect) const {
2028 DCHECK(window_rect);
2029 float scale = popup_origin_scale_for_emulation_;
2030 if (!scale)
2031 return;
2032 window_rect->x =
2033 popup_screen_origin_for_emulation_.x() +
2034 (window_rect->x - popup_view_origin_for_emulation_.x()) * scale;
2035 window_rect->y =
2036 popup_screen_origin_for_emulation_.y() +
2037 (window_rect->y - popup_view_origin_for_emulation_.y()) * scale;
2038}
2039
Blink Reformat1c4d759e2017-04-09 16:34:542040WebRect RenderWidget::WindowRect() {
bokan841fdc72016-10-06 00:16:352041 WebRect rect;
bokan6b08cd22016-10-05 00:55:212042 if (pending_window_rect_count_) {
2043 // NOTE(mbelshe): If there is a pending_window_rect_, then getting
2044 // the RootWindowRect is probably going to return wrong results since the
2045 // browser may not have processed the Move yet. There isn't really anything
2046 // good to do in this case, and it shouldn't happen - since this size is
2047 // only really needed for windowToScreen, which is only used for Popups.
bokan841fdc72016-10-06 00:16:352048 rect = pending_window_rect_;
2049 } else {
2050 rect = window_screen_rect_;
bokan6b08cd22016-10-05 00:55:212051 }
[email protected]2533ce12009-05-09 00:02:242052
bokan841fdc72016-10-06 00:16:352053 ScreenRectToEmulatedIfNeeded(&rect);
2054 return rect;
bokan6b08cd22016-10-05 00:55:212055}
2056
Blink Reformat1c4d759e2017-04-09 16:34:542057WebRect RenderWidget::ViewRect() {
Albert J. Wong3c93c182018-09-27 17:29:432058 WebRect rect = widget_screen_rect_;
bokan841fdc72016-10-06 00:16:352059 ScreenRectToEmulatedIfNeeded(&rect);
2060 return rect;
initial.commit09911bf2008-07-26 23:55:292061}
2062
Blink Reformat1c4d759e2017-04-09 16:34:542063void RenderWidget::SetToolTipText(const blink::WebString& text,
[email protected]8a9d6ca32011-06-06 20:11:302064 WebTextDirection hint) {
Albert J. Wong3c93c182018-09-27 17:29:432065 Send(new WidgetHostMsg_SetTooltipText(routing_id_, text.Utf16(), hint));
[email protected]8a9d6ca32011-06-06 20:11:302066}
2067
Blink Reformat1c4d759e2017-04-09 16:34:542068void RenderWidget::SetWindowRect(const WebRect& rect_in_screen) {
danakj81d601e62018-10-09 22:15:052069 // This path is for the renderer to change the on-screen position/size of
2070 // the widget by changing its window rect. This is not possible for
2071 // RenderWidgets whose position/size are controlled by layout from another
danakj6dcbc5962018-11-16 16:45:422072 // frame tree (ie. child local root frames), as the window rect can only be
2073 // set by the browser.
2074 if (for_child_local_root_frame_)
danakj81d601e62018-10-09 22:15:052075 return;
2076
oshima33ec97cd2015-12-14 19:40:242077 WebRect window_rect = rect_in_screen;
bokan841fdc72016-10-06 00:16:352078 EmulatedToScreenRectIfNeeded(&window_rect);
[email protected]b2e4c70132013-10-03 02:07:512079
danakj7602f4f2019-01-31 18:03:232080 if (synchronous_resize_mode_for_testing_) {
Kent Tamura21d1de62018-12-10 04:45:202081 // This is a web-test-only path. At one point, it was planned to be
danakj65f423b72018-10-10 17:21:202082 // removed. See https://crbug.com/309760.
bokanc007c3a2015-02-03 07:15:562083 SetWindowRectSynchronously(window_rect);
danakj65f423b72018-10-10 17:21:202084 return;
2085 }
2086
2087 if (show_callback_) {
2088 // The widget is not shown yet. Delay the |window_rect| being sent to the
2089 // browser until Show() is called so it can be sent with that IPC, once the
2090 // browser is ready for the info.
2091 initial_rect_ = window_rect;
2092 } else {
2093 Send(new WidgetHostMsg_RequestSetBounds(routing_id_, window_rect));
2094 SetPendingWindowRect(window_rect);
initial.commit09911bf2008-07-26 23:55:292095 }
2096}
2097
[email protected]2533ce12009-05-09 00:02:242098void RenderWidget::SetPendingWindowRect(const WebRect& rect) {
2099 pending_window_rect_ = rect;
2100 pending_window_rect_count_++;
[email protected]2533ce12009-05-09 00:02:242101
bokan6b08cd22016-10-05 00:55:212102 // Popups don't get size updates back from the browser so just store the set
2103 // values.
danakj89ae4532018-12-12 22:52:552104 if (!for_frame()) {
Blink Reformat1c4d759e2017-04-09 16:34:542105 window_screen_rect_ = rect;
Albert J. Wong3c93c182018-09-27 17:29:432106 widget_screen_rect_ = rect;
[email protected]2533ce12009-05-09 00:02:242107 }
[email protected]d4547452008-08-28 18:36:372108}
2109
wjmacleanee244e02017-05-26 21:06:072110void RenderWidget::OnShowContextMenu(ui::MenuSourceType source_type,
2111 const gfx::Point& location) {
wjmacleanee244e02017-05-26 21:06:072112 has_host_context_menu_location_ = true;
2113 host_context_menu_location_ = location;
2114 if (GetWebWidget()) {
2115 GetWebWidget()->ShowContextMenu(
2116 static_cast<blink::WebMenuSourceType>(source_type));
2117 }
2118 has_host_context_menu_location_ = false;
2119}
2120
[email protected]fa7b1dc2010-06-23 17:53:042121void RenderWidget::OnImeSetComposition(
[email protected]fcf75d42013-12-03 20:11:262122 const base::string16& text,
Ryan Landay9e42fd742017-08-12 01:59:112123 const std::vector<WebImeTextSpan>& ime_text_spans,
chongz7eb752802016-01-27 21:28:072124 const gfx::Range& replacement_range,
Ryan Landay9e42fd742017-08-12 01:59:112125 int selection_start,
2126 int selection_end) {
ekaramad5aff1942017-01-06 01:26:352127 if (!ShouldHandleImeEvents())
2128 return;
2129
brettw4b461082016-11-19 18:55:162130#if BUILDFLAG(ENABLE_PLUGINS)
EhsanK22d482e2017-08-10 17:29:492131 if (auto* plugin = GetFocusedPepperPluginInsideWidget()) {
Ryan Landay9e42fd742017-08-12 01:59:112132 plugin->render_frame()->OnImeSetComposition(text, ime_text_spans,
EhsanK22d482e2017-08-10 17:29:492133 selection_start, selection_end);
ekaramad2a46d632016-07-19 13:33:092134 return;
2135 }
2136#endif
[email protected]66fca5bc2013-05-23 06:58:292137 ImeEventGuard guard(this);
ekaramad2daaf672016-11-10 20:29:012138 blink::WebInputMethodController* controller = GetInputMethodController();
ekaramad2daaf672016-11-10 20:29:012139 if (!controller ||
Blink Reformat1c4d759e2017-04-09 16:34:542140 !controller->SetComposition(
Ryan Landay9e42fd742017-08-12 01:59:112141 WebString::FromUTF16(text), WebVector<WebImeTextSpan>(ime_text_spans),
ekaramadce32ef9f2017-02-09 17:33:562142 replacement_range.IsValid()
2143 ? WebRange(replacement_range.start(), replacement_range.length())
2144 : WebRange(),
2145 selection_start, selection_end)) {
[email protected]fa7b1dc2010-06-23 17:53:042146 // If we failed to set the composition text, then we need to let the browser
2147 // process to cancel the input method's ongoing composition session, to make
2148 // sure we are in a consistent state.
Dave Tapuska04bc5ee92018-04-17 19:03:312149 if (mojom::WidgetInputHandlerHost* host =
2150 widget_input_handler_manager_->GetWidgetInputHandlerHost()) {
2151 host->ImeCancelComposition();
Dave Tapuska139a72f2017-09-06 21:57:032152 }
[email protected]7f00efa2010-04-15 05:01:262153 }
nonafa291792016-08-10 02:36:182154 UpdateCompositionInfo(false /* not an immediate request */);
[email protected]fa7b1dc2010-06-23 17:53:042155}
2156
rlanday7efe2302017-01-11 00:14:282157void RenderWidget::OnImeCommitText(
2158 const base::string16& text,
Ryan Landay9e42fd742017-08-12 01:59:112159 const std::vector<WebImeTextSpan>& ime_text_spans,
rlanday7efe2302017-01-11 00:14:282160 const gfx::Range& replacement_range,
2161 int relative_cursor_pos) {
ekaramad5aff1942017-01-06 01:26:352162 if (!ShouldHandleImeEvents())
2163 return;
2164
brettw4b461082016-11-19 18:55:162165#if BUILDFLAG(ENABLE_PLUGINS)
EhsanK22d482e2017-08-10 17:29:492166 if (auto* plugin = GetFocusedPepperPluginInsideWidget()) {
2167 plugin->render_frame()->OnImeCommitText(text, replacement_range,
2168 relative_cursor_pos);
ekaramad2a46d632016-07-19 13:33:092169 return;
2170 }
2171#endif
[email protected]66fca5bc2013-05-23 06:58:292172 ImeEventGuard guard(this);
fsamuele8326c742016-01-12 00:49:392173 input_handler_->set_handling_input_event(true);
ekaramadb6483a052017-02-10 02:23:262174 if (auto* controller = GetInputMethodController()) {
Blink Reformat1c4d759e2017-04-09 16:34:542175 controller->CommitText(
Ryan Landay9e42fd742017-08-12 01:59:112176 WebString::FromUTF16(text), WebVector<WebImeTextSpan>(ime_text_spans),
ekaramadce32ef9f2017-02-09 17:33:562177 replacement_range.IsValid()
2178 ? WebRange(replacement_range.start(), replacement_range.length())
2179 : WebRange(),
2180 relative_cursor_pos);
ekaramadb6483a052017-02-10 02:23:262181 }
aelias87b8f7c2016-09-14 03:19:292182 input_handler_->set_handling_input_event(false);
2183 UpdateCompositionInfo(false /* not an immediate request */);
2184}
2185
2186void RenderWidget::OnImeFinishComposingText(bool keep_selection) {
ekaramad5aff1942017-01-06 01:26:352187 if (!ShouldHandleImeEvents())
2188 return;
2189
brettw4b461082016-11-19 18:55:162190#if BUILDFLAG(ENABLE_PLUGINS)
EhsanK22d482e2017-08-10 17:29:492191 if (auto* plugin = GetFocusedPepperPluginInsideWidget()) {
2192 plugin->render_frame()->OnImeFinishComposingText(keep_selection);
aelias87b8f7c2016-09-14 03:19:292193 return;
2194 }
2195#endif
2196
changwan38c3eb612016-12-09 01:45:562197 if (!GetWebWidget())
aelias87b8f7c2016-09-14 03:19:292198 return;
2199 ImeEventGuard guard(this);
2200 input_handler_->set_handling_input_event(true);
ekaramad2daaf672016-11-10 20:29:012201 if (auto* controller = GetInputMethodController()) {
Blink Reformat1c4d759e2017-04-09 16:34:542202 controller->FinishComposingText(
2203 keep_selection ? WebInputMethodController::kKeepSelection
2204 : WebInputMethodController::kDoNotKeepSelection);
ekaramad2daaf672016-11-10 20:29:012205 }
fsamuele8326c742016-01-12 00:49:392206 input_handler_->set_handling_input_event(false);
nonafa291792016-08-10 02:36:182207 UpdateCompositionInfo(false /* not an immediate request */);
initial.commit09911bf2008-07-26 23:55:292208}
2209
Christopher Camerone9a30c12018-03-07 08:23:382210void RenderWidget::UpdateSurfaceAndScreenInfo(
Fady Samuel1c1ad3692018-11-06 23:28:402211 const viz::LocalSurfaceIdAllocation& new_local_surface_id_allocation,
danakj9c4e7b812019-02-19 22:04:272212 const gfx::Size& compositor_viewport_pixel_size,
Christopher Camerone9a30c12018-03-07 08:23:382213 const ScreenInfo& new_screen_info) {
Christopher Camerone9a30c12018-03-07 08:23:382214 bool orientation_changed =
2215 screen_info_.orientation_angle != new_screen_info.orientation_angle ||
2216 screen_info_.orientation_type != new_screen_info.orientation_type;
2217 bool web_device_scale_factor_changed =
2218 screen_info_.device_scale_factor != new_screen_info.device_scale_factor;
Fady Samuela863f152018-03-09 16:10:032219 ScreenInfo previous_original_screen_info = GetOriginalScreenInfo();
Christopher Cameronbe7ecea2018-02-27 00:47:192220
Fady Samuel1c1ad3692018-11-06 23:28:402221 local_surface_id_allocation_from_parent_ = new_local_surface_id_allocation;
Christopher Camerone9a30c12018-03-07 08:23:382222 screen_info_ = new_screen_info;
Christopher Cameronbe7ecea2018-02-27 00:47:192223
danakj125a29e2019-01-24 23:25:292224 // Note carefully that the DSF specified in |new_screen_info| is not the
2225 // DSF used by the compositor during device emulation!
2226 layer_tree_view_->SetViewportSizeAndScale(
danakj9c4e7b812019-02-19 22:04:272227 compositor_viewport_pixel_size,
danakj125a29e2019-01-24 23:25:292228 GetOriginalScreenInfo().device_scale_factor,
2229 local_surface_id_allocation_from_parent_);
danakj9c4e7b812019-02-19 22:04:272230 // The ViewportVisibleRect derives from the LayerTreeView's viewport size,
2231 // which is set above.
2232 layer_tree_view_->SetViewportVisibleRect(ViewportVisibleRect());
danakj04d128a2019-01-25 02:10:202233 layer_tree_view_->SetRasterColorSpace(
2234 screen_info_.color_space.GetRasterColorSpace());
Christopher Cameronbe7ecea2018-02-27 00:47:192235
Christopher Camerone9a30c12018-03-07 08:23:382236 if (orientation_changed)
2237 OnOrientationChange();
Christopher Cameronbe7ecea2018-02-27 00:47:192238
Fady Samuela863f152018-03-09 16:10:032239 if (previous_original_screen_info != GetOriginalScreenInfo()) {
Christopher Camerone9a30c12018-03-07 08:23:382240 for (auto& observer : render_frame_proxies_)
Fady Samuela863f152018-03-09 16:10:032241 observer.OnScreenInfoChanged(GetOriginalScreenInfo());
Christopher Camerone9a30c12018-03-07 08:23:382242
2243 // Notify all embedded BrowserPlugins of the updated ScreenInfo.
2244 for (auto& observer : browser_plugins_)
Fady Samuela863f152018-03-09 16:10:032245 observer.ScreenInfoChanged(GetOriginalScreenInfo());
Christopher Camerone9a30c12018-03-07 08:23:382246 }
2247
2248 if (web_device_scale_factor_changed)
Christopher Cameronbe7ecea2018-02-27 00:47:192249 UpdateWebViewWithDeviceScaleFactor();
oshimad5279032015-12-16 18:22:332250}
2251
danakja2c9d0a92018-07-25 20:01:182252void RenderWidget::SetWindowRectSynchronously(
2253 const gfx::Rect& new_window_rect) {
2254 VisualProperties visual_properties;
2255 visual_properties.screen_info = screen_info_;
2256 visual_properties.new_size = new_window_rect.size();
2257 visual_properties.compositor_viewport_pixel_size = gfx::ScaleToCeiledSize(
2258 new_window_rect.size(), GetWebScreenInfo().device_scale_factor);
2259 visual_properties.visible_viewport_size = new_window_rect.size();
2260 visual_properties.is_fullscreen_granted = is_fullscreen_granted_;
2261 visual_properties.display_mode = display_mode_;
Fady Samuel84d8a1f72018-11-08 00:26:172262 visual_properties.local_surface_id_allocation =
2263 local_surface_id_allocation_from_parent_;
danakja2c9d0a92018-07-25 20:01:182264 // We are resizing the window from the renderer, so allocate a new
2265 // viz::LocalSurfaceId to avoid surface invariants violations in tests.
danakj125a29e2019-01-24 23:25:292266 layer_tree_view_->RequestNewLocalSurfaceId();
danakja2c9d0a92018-07-25 20:01:182267 SynchronizeVisualProperties(visual_properties);
2268
Albert J. Wong3c93c182018-09-27 17:29:432269 widget_screen_rect_ = new_window_rect;
danakja2c9d0a92018-07-25 20:01:182270 window_screen_rect_ = new_window_rect;
danakj65f423b72018-10-10 17:21:202271 if (show_callback_) {
2272 // Tests may call here directly to control the window rect. If
2273 // Show() did not happen yet, the rect is stored to be passed to the
2274 // browser when the RenderWidget requests Show().
danakja2c9d0a92018-07-25 20:01:182275 initial_rect_ = new_window_rect;
danakj65f423b72018-10-10 17:21:202276 }
danakja2c9d0a92018-07-25 20:01:182277}
2278
Vladimir Levin98d76dad2018-04-21 00:21:292279void RenderWidget::UpdateCaptureSequenceNumber(
2280 uint32_t capture_sequence_number) {
2281 if (capture_sequence_number == last_capture_sequence_number_)
2282 return;
2283 last_capture_sequence_number_ = capture_sequence_number;
2284
2285 // Notify observers of the new capture sequence number.
2286 for (auto& observer : render_frame_proxies_)
2287 observer.UpdateCaptureSequenceNumber(capture_sequence_number);
2288 for (auto& observer : browser_plugins_)
2289 observer.UpdateCaptureSequenceNumber(capture_sequence_number);
2290}
2291
[email protected]4873c7d2009-07-16 06:36:282292void RenderWidget::OnSetTextDirection(WebTextDirection direction) {
EhsanK955ba582017-11-30 21:14:402293 if (auto* frame = GetFocusedWebLocalFrameInWidget())
2294 frame->SetTextDirection(direction);
[email protected]07f953332009-03-25 04:31:112295}
2296
Albert J. Wong3c93c182018-09-27 17:29:432297void RenderWidget::OnUpdateScreenRects(const gfx::Rect& widget_screen_rect,
[email protected]80ad8622012-11-07 16:33:032298 const gfx::Rect& window_screen_rect) {
[email protected]b2e4c70132013-10-03 02:07:512299 if (screen_metrics_emulator_) {
Albert J. Wong3c93c182018-09-27 17:29:432300 screen_metrics_emulator_->OnUpdateScreenRects(widget_screen_rect,
mfomitchev2600fd7c2016-02-17 20:53:392301 window_screen_rect);
[email protected]b2e4c70132013-10-03 02:07:512302 } else {
Albert J. Wong3c93c182018-09-27 17:29:432303 SetScreenRects(widget_screen_rect, window_screen_rect);
[email protected]b2e4c70132013-10-03 02:07:512304 }
Albert J. Wong3c93c182018-09-27 17:29:432305 Send(new WidgetHostMsg_UpdateScreenRects_ACK(routing_id()));
[email protected]80ad8622012-11-07 16:33:032306}
2307
kenrbea731792017-01-13 15:10:482308void RenderWidget::OnSetViewportIntersection(
Ken Buchananb2c9e262018-03-10 16:53:312309 const gfx::Rect& viewport_intersection,
Stefan Zager54e25832018-08-14 22:15:312310 const gfx::Rect& compositor_visible_rect,
Stefan Zager0926c9c2019-03-12 02:46:092311 blink::FrameOcclusionState occlusion_state) {
Ehsan Karamadf3ca2a82018-01-20 01:05:252312 if (auto* frame_widget = GetFrameWidget()) {
Ken Buchananb2c9e262018-03-10 16:53:312313 compositor_visible_rect_ = compositor_visible_rect;
Stefan Zager54e25832018-08-14 22:15:312314 frame_widget->SetRemoteViewportIntersection(viewport_intersection,
Stefan Zager0926c9c2019-03-12 02:46:092315 occlusion_state);
danakja6c10012018-07-06 14:25:362316 layer_tree_view_->SetViewportVisibleRect(ViewportVisibleRect());
kenrbea731792017-01-13 15:10:482317 }
2318}
2319
kenrb04323782017-06-23 01:23:322320void RenderWidget::OnSetIsInert(bool inert) {
danakj89ae4532018-12-12 22:52:552321 if (auto* frame_widget = GetFrameWidget())
Ehsan Karamadf3ca2a82018-01-20 01:05:252322 frame_widget->SetIsInert(inert);
kenrb04323782017-06-23 01:23:322323}
2324
sunxd540a9962018-05-24 22:51:062325void RenderWidget::OnSetInheritedEffectiveTouchAction(
2326 cc::TouchAction touch_action) {
danakj89ae4532018-12-12 22:52:552327 if (auto* frame_widget = GetFrameWidget())
sunxd540a9962018-05-24 22:51:062328 frame_widget->SetInheritedEffectiveTouchAction(touch_action);
sunxd540a9962018-05-24 22:51:062329}
2330
Ken Buchanan8a319fb2017-11-15 18:37:122331void RenderWidget::OnUpdateRenderThrottlingStatus(bool is_throttled,
2332 bool subtree_throttled) {
danakj89ae4532018-12-12 22:52:552333 if (auto* frame_widget = GetFrameWidget())
Ehsan Karamadf3ca2a82018-01-20 01:05:252334 frame_widget->UpdateRenderThrottlingStatus(is_throttled, subtree_throttled);
Ken Buchanan8a319fb2017-11-15 18:37:122335}
2336
paulmeyer90f6c31d2016-11-12 00:17:592337void RenderWidget::OnDragTargetDragEnter(
2338 const std::vector<DropData::Metadata>& drop_meta_data,
Ella Ge80a52dce2017-11-15 18:01:522339 const gfx::PointF& client_point,
2340 const gfx::PointF& screen_point,
paulmeyer90f6c31d2016-11-12 00:17:592341 WebDragOperationsMask ops,
2342 int key_modifiers) {
Ehsan Karamadf3ca2a82018-01-20 01:05:252343 blink::WebFrameWidget* frame_widget = GetFrameWidget();
2344 if (!frame_widget)
paulmeyer90f6c31d2016-11-12 00:17:592345 return;
2346
Ehsan Karamadf3ca2a82018-01-20 01:05:252347 WebDragOperation operation = frame_widget->DragTargetDragEnter(
2348 DropMetaDataToWebDragData(drop_meta_data), client_point, screen_point,
2349 ops, key_modifiers);
paulmeyer90f6c31d2016-11-12 00:17:592350
2351 Send(new DragHostMsg_UpdateDragCursor(routing_id(), operation));
2352}
2353
Ella Ge80a52dce2017-11-15 18:01:522354void RenderWidget::OnDragTargetDragOver(const gfx::PointF& client_point,
2355 const gfx::PointF& screen_point,
paulmeyer90f6c31d2016-11-12 00:17:592356 WebDragOperationsMask ops,
2357 int key_modifiers) {
Ehsan Karamadf3ca2a82018-01-20 01:05:252358 blink::WebFrameWidget* frame_widget = GetFrameWidget();
2359 if (!frame_widget)
paulmeyer90f6c31d2016-11-12 00:17:592360 return;
2361
Ehsan Karamadf3ca2a82018-01-20 01:05:252362 WebDragOperation operation = frame_widget->DragTargetDragOver(
2363 ConvertWindowPointToViewport(client_point), screen_point, ops,
2364 key_modifiers);
paulmeyer90f6c31d2016-11-12 00:17:592365
2366 Send(new DragHostMsg_UpdateDragCursor(routing_id(), operation));
2367}
2368
Ella Ge80a52dce2017-11-15 18:01:522369void RenderWidget::OnDragTargetDragLeave(const gfx::PointF& client_point,
2370 const gfx::PointF& screen_point) {
Ehsan Karamadf3ca2a82018-01-20 01:05:252371 blink::WebFrameWidget* frame_widget = GetFrameWidget();
2372 if (!frame_widget)
paulmeyer90f6c31d2016-11-12 00:17:592373 return;
Ehsan Karamadf3ca2a82018-01-20 01:05:252374
2375 frame_widget
Blink Reformat1c4d759e2017-04-09 16:34:542376 ->DragTargetDragLeave(ConvertWindowPointToViewport(client_point),
kenrb07c272802017-02-07 23:48:172377 screen_point);
paulmeyer90f6c31d2016-11-12 00:17:592378}
2379
2380void RenderWidget::OnDragTargetDrop(const DropData& drop_data,
Ella Ge80a52dce2017-11-15 18:01:522381 const gfx::PointF& client_point,
2382 const gfx::PointF& screen_point,
paulmeyer90f6c31d2016-11-12 00:17:592383 int key_modifiers) {
Ehsan Karamadf3ca2a82018-01-20 01:05:252384 blink::WebFrameWidget* frame_widget = GetFrameWidget();
2385 if (!frame_widget)
paulmeyer90f6c31d2016-11-12 00:17:592386 return;
2387
Ehsan Karamadf3ca2a82018-01-20 01:05:252388 frame_widget->DragTargetDrop(DropDataToWebDragData(drop_data),
2389 ConvertWindowPointToViewport(client_point),
2390 screen_point, key_modifiers);
paulmeyer90f6c31d2016-11-12 00:17:592391}
2392
Ella Ge80a52dce2017-11-15 18:01:522393void RenderWidget::OnDragSourceEnded(const gfx::PointF& client_point,
2394 const gfx::PointF& screen_point,
paulmeyer8fc8ea92016-11-15 05:12:212395 WebDragOperation op) {
Ehsan Karamadf3ca2a82018-01-20 01:05:252396 blink::WebFrameWidget* frame_widget = GetFrameWidget();
2397 if (!frame_widget)
paulmeyer8fc8ea92016-11-15 05:12:212398 return;
2399
Ehsan Karamadf3ca2a82018-01-20 01:05:252400 frame_widget->DragSourceEndedAt(ConvertWindowPointToViewport(client_point),
2401 screen_point, op);
paulmeyer8fc8ea92016-11-15 05:12:212402}
2403
2404void RenderWidget::OnDragSourceSystemDragEnded() {
danakj503545c2019-01-11 19:03:092405 blink::WebFrameWidget* frame_widget = GetFrameWidget();
2406 if (!frame_widget)
paulmeyer8fc8ea92016-11-15 05:12:212407 return;
2408
danakj503545c2019-01-11 19:03:092409 frame_widget->DragSourceSystemDragEnded();
paulmeyer8fc8ea92016-11-15 05:12:212410}
2411
Blink Reformat1c4d759e2017-04-09 16:34:542412void RenderWidget::ShowVirtualKeyboardOnElementFocus() {
okaa398f50b2017-03-21 06:30:262413#if defined(OS_CHROMEOS)
2414 // On ChromeOS, virtual keyboard is triggered only when users leave the
2415 // mouse button or the finger and a text input element is focused at that
2416 // time. Focus event itself shouldn't trigger virtual keyboard.
2417 UpdateTextInputState();
2418#else
changwan75e3b2072017-01-16 02:55:002419 ShowVirtualKeyboard();
okaa398f50b2017-03-21 06:30:262420#endif
boliu7b2be2f2016-11-04 04:58:312421
2422// TODO(rouslan): Fix ChromeOS and Windows 8 behavior of autofill popup with
2423// virtual keyboard.
2424#if !defined(OS_ANDROID)
2425 FocusChangeComplete();
2426#endif
[email protected]0d1ebed12013-08-05 22:01:132427}
2428
fsamuel72464894f2015-12-15 06:59:312429ui::TextInputType RenderWidget::GetTextInputType() {
brettw4b461082016-11-19 18:55:162430#if BUILDFLAG(ENABLE_PLUGINS)
EhsanK22d482e2017-08-10 17:29:492431 if (auto* plugin = GetFocusedPepperPluginInsideWidget())
2432 return plugin->text_input_type();
ekaramad2a46d632016-07-19 13:33:092433#endif
ekaramad5aff1942017-01-06 01:26:352434 if (auto* controller = GetInputMethodController())
Blink Reformat1c4d759e2017-04-09 16:34:542435 return ConvertWebTextInputType(controller->TextInputType());
fsamuel72464894f2015-12-15 06:59:312436 return ui::TEXT_INPUT_TYPE_NONE;
2437}
2438
nonafa291792016-08-10 02:36:182439void RenderWidget::UpdateCompositionInfo(bool immediate_request) {
2440 if (!monitor_composition_info_ && !immediate_request)
2441 return; // Do not calculate composition info if not requested.
nonadac0c7a2016-08-01 02:30:592442
nonafa291792016-08-10 02:36:182443 TRACE_EVENT0("renderer", "RenderWidget::UpdateCompositionInfo");
2444 gfx::Range range;
2445 std::vector<gfx::Rect> character_bounds;
2446
2447 if (GetTextInputType() == ui::TEXT_INPUT_TYPE_NONE) {
2448 // Composition information is only available on editable node.
2449 range = gfx::Range::InvalidRange();
2450 } else {
2451 GetCompositionRange(&range);
2452 GetCompositionCharacterBounds(&character_bounds);
2453 }
2454
2455 if (!immediate_request &&
2456 !ShouldUpdateCompositionInfo(range, character_bounds)) {
fsamuel72464894f2015-12-15 06:59:312457 return;
nonafa291792016-08-10 02:36:182458 }
fsamuel72464894f2015-12-15 06:59:312459 composition_character_bounds_ = character_bounds;
2460 composition_range_ = range;
Dave Tapuska04bc5ee92018-04-17 19:03:312461 if (mojom::WidgetInputHandlerHost* host =
2462 widget_input_handler_manager_->GetWidgetInputHandlerHost()) {
2463 host->ImeCompositionRangeChanged(composition_range_,
2464 composition_character_bounds_);
Dave Tapuska139a72f2017-09-06 21:57:032465 }
fsamuel72464894f2015-12-15 06:59:312466}
2467
Blink Reformat1c4d759e2017-04-09 16:34:542468void RenderWidget::ConvertViewportToWindow(blink::WebRect* rect) {
danakj4b347212018-07-04 17:55:172469 if (compositor_deps_->IsUseZoomForDSFEnabled()) {
Fady Samuela863f152018-03-09 16:10:032470 float reverse = 1 / GetOriginalScreenInfo().device_scale_factor;
oshimad5279032015-12-16 18:22:332471 // TODO(oshima): We may need to allow pixel precision here as the the
oshimaf866dab2015-12-05 00:41:542472 // anchor element can be placed at half pixel.
lfg15b235a32016-08-25 17:45:462473 gfx::Rect window_rect =
2474 gfx::ScaleToEnclosedRect(gfx::Rect(*rect), reverse);
2475 rect->x = window_rect.x();
2476 rect->y = window_rect.y();
2477 rect->width = window_rect.width();
2478 rect->height = window_rect.height();
oshimaf866dab2015-12-05 00:41:542479 }
2480}
2481
Blink Reformat1c4d759e2017-04-09 16:34:542482void RenderWidget::ConvertWindowToViewport(blink::WebFloatRect* rect) {
danakj4b347212018-07-04 17:55:172483 if (compositor_deps_->IsUseZoomForDSFEnabled()) {
Fady Samuela863f152018-03-09 16:10:032484 rect->x *= GetOriginalScreenInfo().device_scale_factor;
2485 rect->y *= GetOriginalScreenInfo().device_scale_factor;
2486 rect->width *= GetOriginalScreenInfo().device_scale_factor;
2487 rect->height *= GetOriginalScreenInfo().device_scale_factor;
oshimaa6985b62016-01-27 08:58:302488 }
2489}
2490
changwan8c342742016-02-26 00:53:392491void RenderWidget::OnRequestTextInputStateUpdate() {
Dave Tapuska9db80842017-07-24 17:24:262492#if defined(OS_ANDROID)
danakj125a29e2019-01-24 23:25:292493 // This task may run between the Close IPC and the task that actually closes
2494 // this class.
2495 if (closing_)
2496 return;
changwan8c342742016-02-26 00:53:392497 DCHECK(!ime_event_guard_);
2498 UpdateSelectionBounds();
changwan75e3b2072017-01-16 02:55:002499 UpdateTextInputStateInternal(false, true /* reply_to_request */);
[email protected]105dffb42013-02-20 03:46:212500#endif
Dave Tapuska9db80842017-07-24 17:24:262501}
[email protected]105dffb42013-02-20 03:46:212502
ekaramadc9b70a72017-03-23 16:14:232503void RenderWidget::OnRequestCompositionUpdates(bool immediate_request,
2504 bool monitor_updates) {
2505 monitor_composition_info_ = monitor_updates;
nonafa291792016-08-10 02:36:182506 if (!immediate_request)
2507 return;
2508 UpdateCompositionInfo(true /* immediate request */);
2509}
2510
[email protected]fcdc5642014-05-09 14:32:242511void RenderWidget::OnOrientationChange() {
Ehsan Karamad655d7b8a2018-01-12 18:38:382512 if (auto* frame_widget = GetFrameWidget()) {
Alex Moshchuk0b409c42017-09-13 02:09:002513 // LocalRoot() might return null for provisional main frames. In this case,
2514 // the frame hasn't committed a navigation and is not swapped into the tree
2515 // yet, so it doesn't make sense to send orientation change events to it.
2516 //
2517 // TODO(https://crbug.com/578349): This check should be cleaned up
2518 // once provisional frames are gone.
Ehsan Karamad655d7b8a2018-01-12 18:38:382519 if (frame_widget->LocalRoot())
2520 frame_widget->LocalRoot()->SendOrientationChangeEvent();
lfg8d649cc2017-04-28 18:04:302521 }
[email protected]fcdc5642014-05-09 14:32:242522}
2523
[email protected]bee16aab2009-08-26 15:55:032524void RenderWidget::SetHidden(bool hidden) {
danakj14e00a8322019-02-07 17:12:062525 // A frozen main frame widget does not become shown or hidden, since it has
2526 // no frame associated with it. It must be thawed before changing visibility.
2527 DCHECK(!is_frozen_);
2528
[email protected]bee16aab2009-08-26 15:55:032529 if (is_hidden_ == hidden)
2530 return;
2531
jdduke8fac9d102014-12-20 02:40:132532 // The status has changed. Tell the RenderThread about it and ensure
2533 // throttled acks are released in case frame production ceases.
[email protected]bee16aab2009-08-26 15:55:032534 is_hidden_ = hidden;
jdduke8fac9d102014-12-20 02:40:132535
Scott Violete3b1a3cb2019-05-02 16:51:522536 if (is_hidden_)
Aditya Keerthic0d45b42019-02-25 18:53:002537 first_update_visual_state_after_hidden_ = true;
alexclarke7fa93942015-10-21 15:37:112538
2539 if (render_widget_scheduling_state_)
2540 render_widget_scheduling_state_->SetHidden(hidden);
danakj1b35e9d2018-11-07 19:09:282541
David Bokanf92eaf22019-03-01 01:54:512542 // If the renderer was hidden, resolve any pending synthetic gestures so they
2543 // aren't blocked waiting for a compositor frame to be generated.
2544 if (is_hidden_)
2545 widget_input_handler_manager_->InvokeInputProcessedCallback();
2546
danakj14e00a8322019-02-07 17:12:062547 StartStopCompositor();
[email protected]bee16aab2009-08-26 15:55:032548}
2549
danakjf9284772019-01-23 18:29:142550void RenderWidget::SetIsFullscreen(bool fullscreen) {
2551 if (fullscreen == is_fullscreen_granted_)
[email protected]2b624c562011-10-27 22:58:262552 return;
danakjf9284772019-01-23 18:29:142553 is_fullscreen_granted_ = fullscreen;
mikhail.pozdnyakovf2c902a2015-04-14 08:09:122554 if (is_fullscreen_granted_) {
Blink Reformat1c4d759e2017-04-09 16:34:542555 GetWebWidget()->DidEnterFullscreen();
[email protected]2b624c562011-10-27 22:58:262556 } else {
Blink Reformat1c4d759e2017-04-09 16:34:542557 GetWebWidget()->DidExitFullscreen();
[email protected]2b624c562011-10-27 22:58:262558 }
[email protected]2b624c562011-10-27 22:58:262559}
2560
changwanf2a707b2015-10-30 08:22:162561void RenderWidget::OnImeEventGuardStart(ImeEventGuard* guard) {
2562 if (!ime_event_guard_)
2563 ime_event_guard_ = guard;
[email protected]66fca5bc2013-05-23 06:58:292564}
2565
changwanf2a707b2015-10-30 08:22:162566void RenderWidget::OnImeEventGuardFinish(ImeEventGuard* guard) {
Jeremy Roman0b261f42018-08-28 15:36:012567 if (ime_event_guard_ != guard)
changwanf2a707b2015-10-30 08:22:162568 return;
changwanf2a707b2015-10-30 08:22:162569 ime_event_guard_ = nullptr;
2570
[email protected]66fca5bc2013-05-23 06:58:292571 // While handling an ime event, text input state and selection bounds updates
2572 // are ignored. These must explicitly be updated once finished handling the
2573 // ime event.
2574 UpdateSelectionBounds();
[email protected]cb9e2632013-06-18 11:26:472575#if defined(OS_ANDROID)
changwan75e3b2072017-01-16 02:55:002576 if (guard->show_virtual_keyboard())
2577 ShowVirtualKeyboard();
2578 else
2579 UpdateTextInputState();
[email protected]cb9e2632013-06-18 11:26:472580#endif
[email protected]66fca5bc2013-05-23 06:58:292581}
2582
[email protected]7c8873e2013-02-05 08:03:012583void RenderWidget::GetSelectionBounds(gfx::Rect* focus, gfx::Rect* anchor) {
brettw4b461082016-11-19 18:55:162584#if BUILDFLAG(ENABLE_PLUGINS)
EhsanK22d482e2017-08-10 17:29:492585 if (auto* plugin = GetFocusedPepperPluginInsideWidget()) {
ekaramad2a46d632016-07-19 13:33:092586 // TODO(kinaba) http://crbug.com/101101
2587 // Current Pepper IME API does not handle selection bounds. So we simply
2588 // use the caret position as an empty range for now. It will be updated
2589 // after Pepper API equips features related to surrounding text retrieval.
EhsanK22d482e2017-08-10 17:29:492590 blink::WebRect caret(plugin->GetCaretBounds());
Blink Reformat1c4d759e2017-04-09 16:34:542591 ConvertViewportToWindow(&caret);
ekaramad2a46d632016-07-19 13:33:092592 *focus = caret;
2593 *anchor = caret;
2594 return;
2595 }
2596#endif
[email protected]7c8873e2013-02-05 08:03:012597 WebRect focus_webrect;
2598 WebRect anchor_webrect;
Blink Reformat1c4d759e2017-04-09 16:34:542599 GetWebWidget()->SelectionBounds(focus_webrect, anchor_webrect);
2600 ConvertViewportToWindow(&focus_webrect);
2601 ConvertViewportToWindow(&anchor_webrect);
oshima33ec97cd2015-12-14 19:40:242602 *focus = focus_webrect;
2603 *anchor = anchor_webrect;
[email protected]73bf95812011-10-12 11:38:322604}
2605
[email protected]e99ef6f2011-10-16 01:13:002606void RenderWidget::UpdateSelectionBounds() {
jdduke1aebad8e2015-07-22 23:25:082607 TRACE_EVENT0("renderer", "RenderWidget::UpdateSelectionBounds");
lfg8ff33912016-09-13 20:59:212608 if (!GetWebWidget())
[email protected]e99ef6f2011-10-16 01:13:002609 return;
changwanf2a707b2015-10-30 08:22:162610 if (ime_event_guard_)
[email protected]66fca5bc2013-05-23 06:58:292611 return;
[email protected]e99ef6f2011-10-16 01:13:002612
mohsenb0eeba72015-08-09 06:20:082613#if defined(USE_AURA)
2614 // TODO(mohsen): For now, always send explicit selection IPC notifications for
2615 // Aura beucause composited selection updates are not working for webview tags
2616 // which regresses IME inside webview. Remove this when composited selection
2617 // updates are fixed for webviews. See, http://crbug.com/510568.
2618 bool send_ipc = true;
2619#else
jddukeacf809e2014-09-23 20:38:382620 // With composited selection updates, the selection bounds will be reported
2621 // directly by the compositor, in which case explicit IPC selection
2622 // notifications should be suppressed.
mohsenb0eeba72015-08-09 06:20:082623 bool send_ipc =
Blink Reformat1c4d759e2017-04-09 16:34:542624 !blink::WebRuntimeFeatures::IsCompositedSelectionUpdateEnabled();
mohsenb0eeba72015-08-09 06:20:082625#endif
2626 if (send_ipc) {
Albert J. Wong3c93c182018-09-27 17:29:432627 WidgetHostMsg_SelectionBounds_Params params;
EhsanK955ba582017-11-30 21:14:402628 params.is_anchor_first = false;
jddukeacf809e2014-09-23 20:38:382629 GetSelectionBounds(&params.anchor_rect, &params.focus_rect);
2630 if (selection_anchor_rect_ != params.anchor_rect ||
2631 selection_focus_rect_ != params.focus_rect) {
2632 selection_anchor_rect_ = params.anchor_rect;
2633 selection_focus_rect_ = params.focus_rect;
EhsanK955ba582017-11-30 21:14:402634 if (auto* focused_frame = GetFocusedWebLocalFrameInWidget()) {
2635 focused_frame->SelectionTextDirection(params.focus_dir,
2636 params.anchor_dir);
2637 params.is_anchor_first = focused_frame->IsSelectionAnchorFirst();
2638 }
Albert J. Wong3c93c182018-09-27 17:29:432639 Send(new WidgetHostMsg_SelectionBoundsChanged(routing_id_, params));
jddukeacf809e2014-09-23 20:38:382640 }
[email protected]58b48a0d2012-06-13 07:01:352641 }
jddukeacf809e2014-09-23 20:38:382642
nonafa291792016-08-10 02:36:182643 UpdateCompositionInfo(false /* not an immediate request */);
[email protected]e99ef6f2011-10-16 01:13:002644}
2645
lfgb00fcad2016-07-14 14:16:332646void RenderWidget::DidAutoResize(const gfx::Size& new_size) {
danakj125a29e2019-01-24 23:25:292647 // Blink can continue running and do a layout/resize between the Close IPC
2648 // and the task that actually closes this class.
2649 if (closing_)
2650 return;
2651
lfgb00fcad2016-07-14 14:16:332652 WebRect new_size_in_window(0, 0, new_size.width(), new_size.height());
Blink Reformat1c4d759e2017-04-09 16:34:542653 ConvertViewportToWindow(&new_size_in_window);
lfgb00fcad2016-07-14 14:16:332654 if (size_.width() != new_size_in_window.width ||
2655 size_.height() != new_size_in_window.height) {
2656 size_ = gfx::Size(new_size_in_window.width, new_size_in_window.height);
2657
danakj7602f4f2019-01-31 18:03:232658 if (synchronous_resize_mode_for_testing_) {
Blink Reformat1c4d759e2017-04-09 16:34:542659 gfx::Rect new_pos(WindowRect().x, WindowRect().y, size_.width(),
2660 size_.height());
Albert J. Wong3c93c182018-09-27 17:29:432661 widget_screen_rect_ = new_pos;
lfgb00fcad2016-07-14 14:16:332662 window_screen_rect_ = new_pos;
2663 }
2664
Christopher Camerone9a30c12018-03-07 08:23:382665 // TODO(ccameron): Note that this destroys any information differentiating
danakj9c4e7b812019-02-19 22:04:272666 // |size_| from the compositor's viewport size. Also note that the
Christopher Cameronaad15a0d2018-03-09 03:47:332667 // calculation of |new_compositor_viewport_pixel_size| does not appear to
2668 // take into account device emulation.
danakj125a29e2019-01-24 23:25:292669 layer_tree_view_->RequestNewLocalSurfaceId();
Christopher Cameronaad15a0d2018-03-09 03:47:332670 gfx::Size new_compositor_viewport_pixel_size =
Fady Samuela863f152018-03-09 16:10:032671 gfx::ScaleToCeiledSize(size_, GetWebScreenInfo().device_scale_factor);
Fady Samuel1c1ad3692018-11-06 23:28:402672 UpdateSurfaceAndScreenInfo(local_surface_id_allocation_from_parent_,
2673 new_compositor_viewport_pixel_size,
2674 screen_info_);
lfgb00fcad2016-07-14 14:16:332675 }
2676}
2677
[email protected]58b48a0d2012-06-13 07:01:352678void RenderWidget::GetCompositionCharacterBounds(
2679 std::vector<gfx::Rect>* bounds) {
2680 DCHECK(bounds);
2681 bounds->clear();
ekaramad2a46d632016-07-19 13:33:092682
brettw4b461082016-11-19 18:55:162683#if BUILDFLAG(ENABLE_PLUGINS)
EhsanK22d482e2017-08-10 17:29:492684 if (GetFocusedPepperPluginInsideWidget())
ekaramad2a46d632016-07-19 13:33:092685 return;
2686#endif
2687
Ehsan Karamad4975af6d2018-02-09 16:58:162688 blink::WebInputMethodController* controller = GetInputMethodController();
2689 if (!controller)
ekaramad2a46d632016-07-19 13:33:092690 return;
2691 blink::WebVector<blink::WebRect> bounds_from_blink;
Ehsan Karamad4975af6d2018-02-09 16:58:162692 if (!controller->GetCompositionCharacterBounds(bounds_from_blink))
ekaramad2a46d632016-07-19 13:33:092693 return;
2694
2695 for (size_t i = 0; i < bounds_from_blink.size(); ++i) {
Blink Reformat1c4d759e2017-04-09 16:34:542696 ConvertViewportToWindow(&bounds_from_blink[i]);
ekaramad2a46d632016-07-19 13:33:092697 bounds->push_back(bounds_from_blink[i]);
2698 }
[email protected]58b48a0d2012-06-13 07:01:352699}
2700
[email protected]db4fc1e2013-09-06 20:01:512701void RenderWidget::GetCompositionRange(gfx::Range* range) {
brettw4b461082016-11-19 18:55:162702#if BUILDFLAG(ENABLE_PLUGINS)
EhsanK22d482e2017-08-10 17:29:492703 if (GetFocusedPepperPluginInsideWidget())
ekaramad2a46d632016-07-19 13:33:092704 return;
2705#endif
EhsanKcdee7b72017-11-08 21:30:092706 blink::WebInputMethodController* controller = GetInputMethodController();
2707 WebRange web_range = controller ? controller->CompositionRange() : WebRange();
Blink Reformat1c4d759e2017-04-09 16:34:542708 if (web_range.IsNull()) {
nona2363a3d2016-11-09 03:26:212709 *range = gfx::Range::InvalidRange();
2710 return;
[email protected]88dbe32f2013-06-20 23:31:362711 }
Blink Reformat1c4d759e2017-04-09 16:34:542712 range->set_start(web_range.StartOffset());
2713 range->set_end(web_range.EndOffset());
[email protected]88dbe32f2013-06-20 23:31:362714}
2715
[email protected]501ea13d2013-07-09 17:03:292716bool RenderWidget::ShouldUpdateCompositionInfo(
[email protected]db4fc1e2013-09-06 20:01:512717 const gfx::Range& range,
[email protected]501ea13d2013-07-09 17:03:292718 const std::vector<gfx::Rect>& bounds) {
changwan2418e1b2016-12-12 12:43:082719 if (!range.IsValid())
2720 return false;
[email protected]501ea13d2013-07-09 17:03:292721 if (composition_range_ != range)
2722 return true;
2723 if (bounds.size() != composition_character_bounds_.size())
2724 return true;
2725 for (size_t i = 0; i < bounds.size(); ++i) {
2726 if (bounds[i] != composition_character_bounds_[i])
2727 return true;
2728 }
2729 return false;
2730}
[email protected]501ea13d2013-07-09 17:03:292731
[email protected]ad26ef42011-06-17 07:59:452732bool RenderWidget::CanComposeInline() {
brettw4b461082016-11-19 18:55:162733#if BUILDFLAG(ENABLE_PLUGINS)
EhsanK22d482e2017-08-10 17:29:492734 if (auto* plugin = GetFocusedPepperPluginInsideWidget())
2735 return plugin->IsPluginAcceptingCompositionEvents();
ekaramad2a46d632016-07-19 13:33:092736#endif
[email protected]ad26ef42011-06-17 07:59:452737 return true;
[email protected]56ea1a62011-05-30 07:05:572738}
2739
Blink Reformat1c4d759e2017-04-09 16:34:542740void RenderWidget::DidHandleGestureEvent(const WebGestureEvent& event,
2741 bool event_cancelled) {
danakja2c9d0a92018-07-25 20:01:182742 if (event_cancelled) {
Albert J. Wong7bbf22d2018-12-20 00:27:272743 // The delegate() doesn't need to hear about cancelled events.
[email protected]c68c3e4e2013-01-24 00:36:562744 return;
danakja2c9d0a92018-07-25 20:01:182745 }
2746
2747#if defined(OS_ANDROID) || defined(USE_AURA)
Blink Reformat1c4d759e2017-04-09 16:34:542748 if (event.GetType() == WebInputEvent::kGestureTap) {
changwan75e3b2072017-01-16 02:55:002749 ShowVirtualKeyboard();
Blink Reformat1c4d759e2017-04-09 16:34:542750 } else if (event.GetType() == WebInputEvent::kGestureLongPress) {
lfg8ff33912016-09-13 20:59:212751 DCHECK(GetWebWidget());
ekaramad5aff1942017-01-06 01:26:352752 blink::WebInputMethodController* controller = GetInputMethodController();
Blink Reformat1c4d759e2017-04-09 16:34:542753 if (!controller || controller->TextInputInfo().value.IsEmpty())
changwan75e3b2072017-01-16 02:55:002754 UpdateTextInputState();
[email protected]07c70d22014-08-21 08:33:462755 else
changwan75e3b2072017-01-16 02:55:002756 ShowVirtualKeyboard();
[email protected]c68c3e4e2013-01-24 00:36:562757 }
ekaramada110f642016-12-21 19:47:282758// TODO(ananta): Piggyback off existing IPCs to communicate this information,
2759// crbug/420130.
2760#if defined(OS_WIN)
danakja2c9d0a92018-07-25 20:01:182761 if (event.GetType() == blink::WebGestureEvent::kGestureTap) {
2762 // TODO(estade): hit test the event against focused node to make sure
2763 // the tap actually hit the focused node.
2764 blink::WebInputMethodController* controller = GetInputMethodController();
2765 blink::WebTextInputType text_input_type =
2766 controller ? controller->TextInputType() : blink::kWebTextInputTypeNone;
ekaramada110f642016-12-21 19:47:282767
Albert J. Wong3c93c182018-09-27 17:29:432768 Send(new WidgetHostMsg_FocusedNodeTouched(
danakja2c9d0a92018-07-25 20:01:182769 routing_id_, text_input_type != blink::kWebTextInputTypeNone));
2770 }
ekaramada110f642016-12-21 19:47:282771#endif
[email protected]c68c3e4e2013-01-24 00:36:562772#endif
danakja2c9d0a92018-07-25 20:01:182773
Albert J. Wong7bbf22d2018-12-20 00:27:272774 // The delegate() gets to respond to handling gestures last.
2775 if (delegate())
2776 delegate()->DidHandleGestureEventForWidget(event);
[email protected]c68c3e4e2013-01-24 00:36:562777}
2778
Blink Reformat1c4d759e2017-04-09 16:34:542779void RenderWidget::DidOverscroll(
danakj1b05cf22019-04-12 23:47:052780 const blink::WebFloatSize& overscroll_delta,
2781 const blink::WebFloatSize& accumulated_overscroll,
sataya.m582c9ce2015-06-09 08:03:422782 const blink::WebFloatPoint& position,
danakj1b05cf22019-04-12 23:47:052783 const blink::WebFloatSize& velocity) {
bokan731ec382016-04-07 03:16:482784#if defined(OS_MACOSX)
2785 // On OSX the user can disable the elastic overscroll effect. If that's the
2786 // case, don't forward the overscroll notification.
2787 DCHECK(compositor_deps());
2788 if (!compositor_deps()->IsElasticOverscrollEnabled())
2789 return;
2790#endif
danakj1b05cf22019-04-12 23:47:052791 input_handler_->DidOverscrollFromBlink(
2792 overscroll_delta, accumulated_overscroll, position, velocity,
2793 layer_tree_view_->layer_tree_host()->overscroll_behavior());
2794}
2795
Daniel Libby688fcb52019-05-25 01:08:472796void RenderWidget::InjectGestureScrollEvent(
2797 blink::WebGestureDevice device,
2798 const blink::WebFloatSize& delta,
2799 ui::input_types::ScrollGranularity granularity,
2800 cc::ElementId scrollable_area_element_id,
2801 blink::WebInputEvent::Type injected_type) {
2802 input_handler_->InjectGestureScrollEvent(
2803 device, delta, granularity, scrollable_area_element_id, injected_type);
2804}
2805
danakj1b05cf22019-04-12 23:47:052806void RenderWidget::SetOverscrollBehavior(
2807 const cc::OverscrollBehavior& behavior) {
2808 layer_tree_view_->layer_tree_host()->SetOverscrollBehavior(behavior);
sataya.m582c9ce2015-06-09 08:03:422809}
2810
danakj4b347212018-07-04 17:55:172811// static
2812cc::LayerTreeSettings RenderWidget::GenerateLayerTreeSettings(
2813 CompositorDependencies* compositor_deps,
2814 bool is_for_subframe,
2815 const gfx::Size& initial_screen_size,
2816 float initial_device_scale_factor) {
2817 const bool is_threaded =
2818 !!compositor_deps->GetCompositorImplThreadTaskRunner();
2819
2820 const base::CommandLine& cmd = *base::CommandLine::ForCurrentProcess();
2821 cc::LayerTreeSettings settings;
2822
Rahul Arakerie510b8c2019-04-01 21:17:542823 settings.compositor_threaded_scrollbar_scrolling =
2824 base::FeatureList::IsEnabled(
2825 features::kCompositorThreadedScrollbarScrolling);
2826
danakj4b347212018-07-04 17:55:172827 settings.resource_settings.use_r16_texture =
2828 base::FeatureList::IsEnabled(media::kUseR16Texture);
2829
2830 settings.commit_to_active_tree = !is_threaded;
2831 settings.is_layer_tree_for_subframe = is_for_subframe;
2832
2833 // For web contents, layer transforms should scale up the contents of layers
2834 // to keep content always crisp when possible.
2835 settings.layer_transforms_should_scale_layer_contents = true;
2836
2837 settings.main_frame_before_activation_enabled =
2838 cmd.HasSwitch(cc::switches::kEnableMainFrameBeforeActivation);
2839
2840 // Checkerimaging is not supported for synchronous single-threaded mode, which
2841 // is what the renderer uses if its not threaded.
2842 settings.enable_checker_imaging =
2843 !cmd.HasSwitch(cc::switches::kDisableCheckerImaging) && is_threaded;
2844
2845#if defined(OS_ANDROID)
2846 // We can use a more aggressive limit on Android since decodes tend to take
2847 // longer on these devices.
2848 settings.min_image_bytes_to_checker = 512 * 1024; // 512kB
2849
2850 // Re-rasterization of checker-imaged content with software raster can be too
2851 // costly on Android.
2852 settings.only_checker_images_with_gpu_raster = true;
2853#endif
2854
2855 auto switch_value_as_int = [](const base::CommandLine& command_line,
2856 const std::string& switch_string, int min_value,
2857 int max_value, int* result) {
2858 std::string string_value = command_line.GetSwitchValueASCII(switch_string);
2859 int int_value;
2860 if (base::StringToInt(string_value, &int_value) && int_value >= min_value &&
2861 int_value <= max_value) {
2862 *result = int_value;
2863 return true;
2864 } else {
danakjda15621e2019-04-05 20:34:432865 DLOG(WARNING) << "Failed to parse switch " << switch_string << ": "
2866 << string_value;
danakj4b347212018-07-04 17:55:172867 return false;
2868 }
2869 };
2870
2871 int default_tile_size = 256;
2872#if defined(OS_ANDROID)
2873 const gfx::Size screen_size =
2874 gfx::ScaleToFlooredSize(initial_screen_size, initial_device_scale_factor);
2875 int display_width = screen_size.width();
2876 int display_height = screen_size.height();
2877 int numTiles = (display_width * display_height) / (256 * 256);
2878 if (numTiles > 16)
2879 default_tile_size = 384;
2880 if (numTiles >= 40)
2881 default_tile_size = 512;
2882
2883 // Adjust for some resolutions that barely straddle an extra
2884 // tile when in portrait mode. This helps worst case scroll/raster
2885 // by not needing a full extra tile for each row.
2886 constexpr int tolerance = 10; // To avoid rounding errors.
2887 int portrait_width = std::min(display_width, display_height);
2888 if (default_tile_size == 256 && std::abs(portrait_width - 768) < tolerance)
2889 default_tile_size += 32;
2890 if (default_tile_size == 384 && std::abs(portrait_width - 1200) < tolerance)
2891 default_tile_size += 32;
2892#elif defined(OS_CHROMEOS) || defined(OS_MACOSX)
2893 // Use 512 for high DPI (dsf=2.0f) devices.
2894 if (initial_device_scale_factor >= 2.0f)
2895 default_tile_size = 512;
2896#endif
2897
2898 // TODO(danakj): This should not be a setting O_O; it should change when the
2899 // device scale factor on LayerTreeHost changes.
2900 settings.default_tile_size = gfx::Size(default_tile_size, default_tile_size);
2901 if (cmd.HasSwitch(switches::kDefaultTileWidth)) {
2902 int tile_width = 0;
2903 switch_value_as_int(cmd, switches::kDefaultTileWidth, 1,
2904 std::numeric_limits<int>::max(), &tile_width);
2905 settings.default_tile_size.set_width(tile_width);
2906 }
2907 if (cmd.HasSwitch(switches::kDefaultTileHeight)) {
2908 int tile_height = 0;
2909 switch_value_as_int(cmd, switches::kDefaultTileHeight, 1,
2910 std::numeric_limits<int>::max(), &tile_height);
2911 settings.default_tile_size.set_height(tile_height);
2912 }
2913
2914 int max_untiled_layer_width = settings.max_untiled_layer_size.width();
2915 if (cmd.HasSwitch(switches::kMaxUntiledLayerWidth)) {
2916 switch_value_as_int(cmd, switches::kMaxUntiledLayerWidth, 1,
2917 std::numeric_limits<int>::max(),
2918 &max_untiled_layer_width);
2919 }
2920 int max_untiled_layer_height = settings.max_untiled_layer_size.height();
2921 if (cmd.HasSwitch(switches::kMaxUntiledLayerHeight)) {
2922 switch_value_as_int(cmd, switches::kMaxUntiledLayerHeight, 1,
2923 std::numeric_limits<int>::max(),
2924 &max_untiled_layer_height);
2925 }
2926
2927 settings.max_untiled_layer_size =
2928 gfx::Size(max_untiled_layer_width, max_untiled_layer_height);
2929
2930 settings.gpu_rasterization_msaa_sample_count =
2931 compositor_deps->GetGpuRasterizationMSAASampleCount();
2932 settings.gpu_rasterization_forced =
2933 compositor_deps->IsGpuRasterizationForced();
2934
2935 settings.can_use_lcd_text = compositor_deps->IsLcdTextEnabled();
2936 settings.use_zero_copy = compositor_deps->IsZeroCopyEnabled();
2937 settings.use_partial_raster = compositor_deps->IsPartialRasterEnabled();
2938 settings.enable_elastic_overscroll =
2939 compositor_deps->IsElasticOverscrollEnabled();
2940 settings.resource_settings.use_gpu_memory_buffer_resources =
2941 compositor_deps->IsGpuMemoryBufferCompositorResourcesEnabled();
2942 settings.use_painted_device_scale_factor =
2943 compositor_deps->IsUseZoomForDSFEnabled();
2944
2945 // Build LayerTreeSettings from command line args.
2946 if (cmd.HasSwitch(cc::switches::kBrowserControlsShowThreshold)) {
2947 std::string top_threshold_str =
2948 cmd.GetSwitchValueASCII(cc::switches::kBrowserControlsShowThreshold);
2949 double show_threshold;
2950 if (base::StringToDouble(top_threshold_str, &show_threshold) &&
2951 show_threshold >= 0.f && show_threshold <= 1.f)
2952 settings.top_controls_show_threshold = show_threshold;
2953 }
2954
2955 if (cmd.HasSwitch(cc::switches::kBrowserControlsHideThreshold)) {
2956 std::string top_threshold_str =
2957 cmd.GetSwitchValueASCII(cc::switches::kBrowserControlsHideThreshold);
2958 double hide_threshold;
2959 if (base::StringToDouble(top_threshold_str, &hide_threshold) &&
2960 hide_threshold >= 0.f && hide_threshold <= 1.f)
2961 settings.top_controls_hide_threshold = hide_threshold;
2962 }
2963
Philip Rogersc55ab6a2018-12-05 17:29:192964 // Blink sends cc a layer list and property trees when either
2965 // BlinkGenPropertyTrees or CompositeAfterPaint are enabled.
Philip Rogersfc2f1162018-10-17 18:28:312966 settings.use_layer_lists =
2967 blink::WebRuntimeFeatures::IsBlinkGenPropertyTreesEnabled() ||
Philip Rogersc55ab6a2018-12-05 17:29:192968 blink::WebRuntimeFeatures::IsCompositeAfterPaintEnabled();
danakj4b347212018-07-04 17:55:172969
David Bokan163b6a72018-10-25 22:03:492970 // Blink currently doesn't support setting fractional scroll offsets so CC
2971 // must send integer values. We plan to eventually make Blink use fractional
2972 // offsets internally: https://crbug.com/414283.
2973 settings.commit_fractional_scroll_deltas =
2974 blink::WebRuntimeFeatures::IsFractionalScrollOffsetsEnabled();
2975
danakj4b347212018-07-04 17:55:172976 // The means the renderer compositor has 2 possible modes:
2977 // - Threaded compositing with a scheduler.
Kent Tamura21d1de62018-12-10 04:45:202978 // - Single threaded compositing without a scheduler (for web tests only).
2979 // Using the scheduler in web tests introduces additional composite steps
danakj4b347212018-07-04 17:55:172980 // that create flakiness.
2981 settings.single_thread_proxy_scheduler = false;
2982
2983 // These flags should be mirrored by UI versions in ui/compositor/.
2984 if (cmd.HasSwitch(cc::switches::kShowCompositedLayerBorders))
2985 settings.initial_debug_state.show_debug_borders.set();
2986 settings.initial_debug_state.show_layer_animation_bounds_rects =
2987 cmd.HasSwitch(cc::switches::kShowLayerAnimationBounds);
2988 settings.initial_debug_state.show_paint_rects =
2989 cmd.HasSwitch(switches::kShowPaintRects);
rnasri2e621942019-05-14 21:24:252990 settings.initial_debug_state.show_layout_shift_regions =
2991 cmd.HasSwitch(switches::kShowLayoutShiftRegions);
danakj4b347212018-07-04 17:55:172992 settings.initial_debug_state.show_property_changed_rects =
2993 cmd.HasSwitch(cc::switches::kShowPropertyChangedRects);
2994 settings.initial_debug_state.show_surface_damage_rects =
2995 cmd.HasSwitch(cc::switches::kShowSurfaceDamageRects);
2996 settings.initial_debug_state.show_screen_space_rects =
2997 cmd.HasSwitch(cc::switches::kShowScreenSpaceRects);
2998
2999 settings.initial_debug_state.SetRecordRenderingStats(
3000 cmd.HasSwitch(cc::switches::kEnableGpuBenchmarking));
3001 settings.enable_surface_synchronization =
3002 features::IsSurfaceSynchronizationEnabled();
3003 settings.build_hit_test_data = features::IsVizHitTestingSurfaceLayerEnabled();
3004
3005 if (cmd.HasSwitch(cc::switches::kSlowDownRasterScaleFactor)) {
3006 const int kMinSlowDownScaleFactor = 0;
3007 const int kMaxSlowDownScaleFactor = INT_MAX;
3008 switch_value_as_int(
3009 cmd, cc::switches::kSlowDownRasterScaleFactor, kMinSlowDownScaleFactor,
3010 kMaxSlowDownScaleFactor,
3011 &settings.initial_debug_state.slow_down_raster_scale_factor);
3012 }
3013
3014 // This is default overlay scrollbar settings for Android and DevTools mobile
3015 // emulator. Aura Overlay Scrollbar will override below.
3016 settings.scrollbar_animator = cc::LayerTreeSettings::ANDROID_OVERLAY;
3017 settings.solid_color_scrollbar_color = SkColorSetARGB(128, 128, 128, 128);
3018 settings.scrollbar_fade_delay = base::TimeDelta::FromMilliseconds(300);
3019 settings.scrollbar_fade_duration = base::TimeDelta::FromMilliseconds(300);
3020
Daniel Libby43972d82019-02-28 21:35:363021 if (cmd.HasSwitch(cc::switches::kCCScrollAnimationDurationForTesting)) {
3022 const int kMinScrollAnimationDuration = 0;
3023 const int kMaxScrollAnimationDuration = INT_MAX;
3024 int duration;
3025 if (switch_value_as_int(cmd,
3026 cc::switches::kCCScrollAnimationDurationForTesting,
3027 kMinScrollAnimationDuration,
3028 kMaxScrollAnimationDuration, &duration)) {
3029 settings.scroll_animation_duration_for_testing =
3030 base::TimeDelta::FromSeconds(duration);
3031 }
3032 }
3033
danakj4b347212018-07-04 17:55:173034#if defined(OS_ANDROID)
3035 bool using_synchronous_compositor =
3036 compositor_deps->UsingSynchronousCompositing();
Ian Vollick7a847a82019-06-14 19:58:593037 bool using_low_memory_policy =
3038 base::SysInfo::IsLowEndDevice() && !IsSmallScreen(screen_size);
danakj4b347212018-07-04 17:55:173039
3040 settings.use_stream_video_draw_quad = true;
3041 settings.using_synchronous_renderer_compositor = using_synchronous_compositor;
3042 if (using_synchronous_compositor) {
3043 // Android WebView uses system scrollbars, so make ours invisible.
3044 // http://crbug.com/677348: This can't be done using hide_scrollbars
3045 // setting because supporting -webkit custom scrollbars is still desired
3046 // on sublayers.
3047 settings.scrollbar_animator = cc::LayerTreeSettings::NO_ANIMATOR;
3048 settings.solid_color_scrollbar_color = SK_ColorTRANSPARENT;
3049
3050 settings.enable_early_damage_check =
3051 cmd.HasSwitch(cc::switches::kCheckDamageEarly);
3052 }
3053 // Android WebView handles root layer flings itself.
3054 settings.ignore_root_layer_flings = using_synchronous_compositor;
3055 // Memory policy on Android WebView does not depend on whether device is
3056 // low end, so always use default policy.
3057 if (using_low_memory_policy && !using_synchronous_compositor) {
3058 // On low-end we want to be very carefull about killing other
3059 // apps. So initially we use 50% more memory to avoid flickering
3060 // or raster-on-demand.
3061 settings.max_memory_for_prepaint_percentage = 67;
3062 } else {
3063 // On other devices we have increased memory excessively to avoid
3064 // raster-on-demand already, so now we reserve 50% _only_ to avoid
3065 // raster-on-demand, and use 50% of the memory otherwise.
3066 settings.max_memory_for_prepaint_percentage = 50;
3067 }
3068
3069 // TODO(danakj): Only do this on low end devices.
3070 settings.create_low_res_tiling = true;
3071
3072#else // defined(OS_ANDROID)
3073 bool using_synchronous_compositor = false; // Only for Android WebView.
3074 // On desktop, we never use the low memory policy unless we are simulating
3075 // low-end mode via a switch.
3076 bool using_low_memory_policy =
3077 cmd.HasSwitch(switches::kEnableLowEndDeviceMode);
3078
3079 if (ui::IsOverlayScrollbarEnabled()) {
3080 settings.scrollbar_animator = cc::LayerTreeSettings::AURA_OVERLAY;
3081 settings.scrollbar_fade_delay = ui::kOverlayScrollbarFadeDelay;
3082 settings.scrollbar_fade_duration = ui::kOverlayScrollbarFadeDuration;
3083 settings.scrollbar_thinning_duration =
3084 ui::kOverlayScrollbarThinningDuration;
3085 settings.scrollbar_flash_after_any_scroll_update =
3086 ui::OverlayScrollbarFlashAfterAnyScrollUpdate();
3087 settings.scrollbar_flash_when_mouse_enter =
3088 ui::OverlayScrollbarFlashWhenMouseEnter();
3089 }
3090
3091 // On desktop, if there's over 4GB of memory on the machine, increase the
3092 // working set size to 256MB for both gpu and software.
3093 const int kImageDecodeMemoryThresholdMB = 4 * 1024;
3094 if (base::SysInfo::AmountOfPhysicalMemoryMB() >=
3095 kImageDecodeMemoryThresholdMB) {
3096 settings.decoded_image_working_set_budget_bytes = 256 * 1024 * 1024;
3097 } else {
3098 // This is the default, but recorded here as well.
3099 settings.decoded_image_working_set_budget_bytes = 128 * 1024 * 1024;
3100 }
3101#endif // defined(OS_ANDROID)
3102
3103 if (using_low_memory_policy) {
3104 // RGBA_4444 textures are only enabled:
3105 // - If the user hasn't explicitly disabled them
3106 // - If system ram is <= 512MB (1GB devices are sometimes low-end).
3107 // - If we are not running in a WebView, where 4444 isn't supported.
3108 if (!cmd.HasSwitch(switches::kDisableRGBA4444Textures) &&
3109 base::SysInfo::AmountOfPhysicalMemoryMB() <= 512 &&
3110 !using_synchronous_compositor) {
3111 settings.use_rgba_4444 = viz::RGBA_4444;
3112
3113 // If we are going to unpremultiply and dither these tiles, we need to
3114 // allocate an additional RGBA_8888 intermediate for each tile
3115 // rasterization when rastering to RGBA_4444 to allow for dithering.
3116 // Setting a reasonable sized max tile size allows this intermediate to
3117 // be consistently reused.
3118 if (base::FeatureList::IsEnabled(
3119 kUnpremultiplyAndDitherLowBitDepthTiles)) {
3120 settings.max_gpu_raster_tile_size = gfx::Size(512, 256);
3121 settings.unpremultiply_and_dither_low_bit_depth_tiles = true;
3122 }
3123 }
3124 }
3125
3126 if (cmd.HasSwitch(switches::kEnableLowResTiling))
3127 settings.create_low_res_tiling = true;
3128 if (cmd.HasSwitch(switches::kDisableLowResTiling))
3129 settings.create_low_res_tiling = false;
3130
3131 if (cmd.HasSwitch(switches::kEnableRGBA4444Textures) &&
3132 !cmd.HasSwitch(switches::kDisableRGBA4444Textures)) {
3133 settings.use_rgba_4444 = true;
3134 }
3135
3136 settings.max_staging_buffer_usage_in_bytes = 32 * 1024 * 1024; // 32MB
3137 // Use 1/4th of staging buffers on low-end devices.
3138 if (base::SysInfo::IsLowEndDevice())
3139 settings.max_staging_buffer_usage_in_bytes /= 4;
3140
3141 cc::ManagedMemoryPolicy defaults = settings.memory_policy;
3142 settings.memory_policy = GetGpuMemoryPolicy(defaults, initial_screen_size,
3143 initial_device_scale_factor);
3144
3145 settings.disallow_non_exact_resource_reuse =
3146 cmd.HasSwitch(switches::kDisallowNonExactResourceReuse);
3147#if defined(OS_ANDROID)
3148 // TODO(crbug.com/746931): This feature appears to be causing visual
3149 // corruption on certain android devices. Will investigate and re-enable.
3150 settings.disallow_non_exact_resource_reuse = true;
3151#endif
3152
3153 if (cmd.HasSwitch(switches::kRunAllCompositorStagesBeforeDraw)) {
3154 settings.wait_for_all_pipeline_stages_before_draw = true;
3155 settings.enable_latency_recovery = false;
3156 }
Eric Karl1e6ffca2019-02-23 22:08:503157#if defined(OS_ANDROID)
3158 if (features::IsSurfaceSynchronizationEnabled()) {
3159 // TODO(crbug.com/933846): LatencyRecovery is causing jank on Android.
Eric Karlcb306e72019-06-26 23:21:173160 // Disable in viz mode for now, with plan to disable more widely once
Eric Karl1e6ffca2019-02-23 22:08:503161 // viz launches.
3162 settings.enable_latency_recovery = false;
3163 }
3164#endif
danakj4b347212018-07-04 17:55:173165
3166 settings.enable_image_animation_resync =
3167 !cmd.HasSwitch(switches::kDisableImageAnimationResync);
3168
Karolina Soltys4d60b402018-10-01 19:28:203169 settings.send_compositor_frame_ack = false;
3170
danakj4b347212018-07-04 17:55:173171 return settings;
3172}
3173
3174// static
3175cc::ManagedMemoryPolicy RenderWidget::GetGpuMemoryPolicy(
3176 const cc::ManagedMemoryPolicy& default_policy,
3177 const gfx::Size& initial_screen_size,
3178 float initial_device_scale_factor) {
3179 cc::ManagedMemoryPolicy actual = default_policy;
3180 actual.bytes_limit_when_visible = 0;
3181
3182 // If the value was overridden on the command line, use the specified value.
3183 static bool client_hard_limit_bytes_overridden =
3184 base::CommandLine::ForCurrentProcess()->HasSwitch(
3185 switches::kForceGpuMemAvailableMb);
3186 if (client_hard_limit_bytes_overridden) {
3187 if (base::StringToSizeT(
3188 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
3189 switches::kForceGpuMemAvailableMb),
3190 &actual.bytes_limit_when_visible))
3191 actual.bytes_limit_when_visible *= 1024 * 1024;
3192 return actual;
3193 }
3194
3195#if defined(OS_ANDROID)
3196 // We can't query available GPU memory from the system on Android.
3197 // Physical memory is also mis-reported sometimes (eg. Nexus 10 reports
3198 // 1262MB when it actually has 2GB, while Razr M has 1GB but only reports
3199 // 128MB java heap size). First we estimate physical memory using both.
3200 size_t dalvik_mb = base::SysInfo::DalvikHeapSizeMB();
3201 size_t physical_mb = base::SysInfo::AmountOfPhysicalMemoryMB();
3202 size_t physical_memory_mb = 0;
3203 if (base::SysInfo::IsLowEndDevice()) {
3204 // TODO(crbug.com/742534): The code below appears to no longer work.
3205 // |dalvik_mb| no longer follows the expected heuristic pattern, causing us
3206 // to over-estimate memory on low-end devices. This entire section probably
3207 // needs to be re-written, but for now we can address the low-end Android
3208 // issues by ignoring |dalvik_mb|.
3209 physical_memory_mb = physical_mb;
3210 } else if (dalvik_mb >= 256) {
3211 physical_memory_mb = dalvik_mb * 4;
3212 } else {
3213 physical_memory_mb = std::max(dalvik_mb * 4, (physical_mb * 4) / 3);
3214 }
3215
3216 // Now we take a default of 1/8th of memory on high-memory devices,
3217 // and gradually scale that back for low-memory devices (to be nicer
3218 // to other apps so they don't get killed). Examples:
3219 // Nexus 4/10(2GB) 256MB (normally 128MB)
3220 // Droid Razr M(1GB) 114MB (normally 57MB)
3221 // Galaxy Nexus(1GB) 100MB (normally 50MB)
3222 // Xoom(1GB) 100MB (normally 50MB)
3223 // Nexus S(low-end) 8MB (normally 8MB)
3224 // Note that the compositor now uses only some of this memory for
3225 // pre-painting and uses the rest only for 'emergencies'.
3226 if (actual.bytes_limit_when_visible == 0) {
3227 // NOTE: Non-low-end devices use only 50% of these limits,
3228 // except during 'emergencies' where 100% can be used.
3229 if (physical_memory_mb >= 1536)
3230 actual.bytes_limit_when_visible = physical_memory_mb / 8; // >192MB
3231 else if (physical_memory_mb >= 1152)
3232 actual.bytes_limit_when_visible = physical_memory_mb / 8; // >144MB
3233 else if (physical_memory_mb >= 768)
3234 actual.bytes_limit_when_visible = physical_memory_mb / 10; // >76MB
3235 else if (physical_memory_mb >= 513)
3236 actual.bytes_limit_when_visible = physical_memory_mb / 12; // <64MB
3237 else
3238 // Devices with this little RAM have very little headroom so we hardcode
3239 // the limit rather than relying on the heuristics above. (They also use
3240 // 4444 textures so we can use a lower limit.)
3241 actual.bytes_limit_when_visible = 8;
3242
3243 actual.bytes_limit_when_visible =
3244 actual.bytes_limit_when_visible * 1024 * 1024;
3245 // Clamp the observed value to a specific range on Android.
3246 actual.bytes_limit_when_visible = std::max(
3247 actual.bytes_limit_when_visible, static_cast<size_t>(8 * 1024 * 1024));
3248 actual.bytes_limit_when_visible =
3249 std::min(actual.bytes_limit_when_visible,
3250 static_cast<size_t>(256 * 1024 * 1024));
3251 }
3252 actual.priority_cutoff_when_visible =
3253 gpu::MemoryAllocation::CUTOFF_ALLOW_EVERYTHING;
3254#else
3255 // Ignore what the system said and give all clients the same maximum
3256 // allocation on desktop platforms.
3257 actual.bytes_limit_when_visible = 512 * 1024 * 1024;
3258 actual.priority_cutoff_when_visible =
3259 gpu::MemoryAllocation::CUTOFF_ALLOW_NICE_TO_HAVE;
3260
3261 // For large monitors (4k), double the tile memory to avoid frequent out of
3262 // memory problems. 4k could mean a screen width of anywhere from 3840 to 4096
3263 // (see https://en.wikipedia.org/wiki/4K_resolution). We use 3500 as a proxy
3264 // for "large enough".
3265 static const int kLargeDisplayThreshold = 3500;
3266 int display_width =
3267 std::round(initial_screen_size.width() * initial_device_scale_factor);
3268 if (display_width >= kLargeDisplayThreshold)
3269 actual.bytes_limit_when_visible *= 2;
3270#endif
3271 return actual;
3272}
3273
Navid Zolghadr4ed3ae42019-05-03 17:02:123274void RenderWidget::HasPointerRawUpdateEventHandlers(bool has_handlers) {
Navid Zolghadr8fdb6114e2018-08-29 19:00:543275 if (input_event_queue_)
Navid Zolghadr4ed3ae42019-05-03 17:02:123276 input_event_queue_->HasPointerRawUpdateEventHandlers(has_handlers);
Navid Zolghadr8fdb6114e2018-08-29 19:00:543277}
3278
Blink Reformat1c4d759e2017-04-09 16:34:543279void RenderWidget::HasTouchEventHandlers(bool has_handlers) {
W. James MacLean662d5382018-03-29 18:33:083280 if (has_touch_handlers_ && *has_touch_handlers_ == has_handlers)
3281 return;
3282
3283 has_touch_handlers_ = has_handlers;
alexclarke7fa93942015-10-21 15:37:113284 if (render_widget_scheduling_state_)
3285 render_widget_scheduling_state_->SetHasTouchHandler(has_handlers);
Albert J. Wongeb28e7e2018-10-12 00:57:433286 Send(new WidgetHostMsg_HasTouchEventHandlers(routing_id_, has_handlers));
[email protected]ce6689f2013-03-29 12:52:553287}
3288
Dave Tapuskadfe486c12017-06-09 16:53:133289void RenderWidget::SetNeedsLowLatencyInput(bool needs_low_latency) {
3290 if (input_event_queue_)
3291 input_event_queue_->SetNeedsLowLatency(needs_low_latency);
3292}
3293
Pavel Feldman6708eefa2019-01-17 03:14:323294void RenderWidget::SetNeedsUnbufferedInputForDebugger(bool unbuffered) {
3295 if (input_event_queue_)
3296 input_event_queue_->SetNeedsUnbufferedInputForDebugger(unbuffered);
3297}
3298
W. James MacLeand973a55b2018-11-29 21:39:133299void RenderWidget::AnimateDoubleTapZoomInMainFrame(
3300 const blink::WebPoint& point,
3301 const blink::WebRect& rect_to_zoom) {
3302 // Only oopif subframes should be sending this message.
Albert J. Wong7bbf22d2018-12-20 00:27:273303 DCHECK(!delegate());
W. James MacLeand973a55b2018-11-29 21:39:133304 Send(new WidgetHostMsg_AnimateDoubleTapZoomInMainFrame(routing_id(), point,
3305 rect_to_zoom));
3306}
3307
W. James MacLean5372eb72018-12-19 12:56:363308void RenderWidget::ZoomToFindInPageRectInMainFrame(
3309 const blink::WebRect& rect_to_zoom) {
3310 // Only oopif subframes should be sending this message.
Albert J. Wong7bbf22d2018-12-20 00:27:273311 DCHECK(!delegate_);
W. James MacLean5372eb72018-12-19 12:56:363312 Send(new WidgetHostMsg_ZoomToFindInPageRectInMainFrame(routing_id(),
3313 rect_to_zoom));
3314}
3315
danakj4886cad2019-02-26 22:15:563316void RenderWidget::RegisterViewportLayers(const cc::ViewportLayers& layers) {
3317 layer_tree_view_->layer_tree_host()->RegisterViewportLayers(layers);
3318}
3319
3320void RenderWidget::RegisterSelection(const cc::LayerSelection& selection) {
3321 layer_tree_view_->layer_tree_host()->RegisterSelection(selection);
3322}
3323
chaopeng161241b72019-03-21 18:25:173324void RenderWidget::FallbackCursorModeLockCursor(bool left,
3325 bool right,
3326 bool up,
3327 bool down) {
3328 widget_input_handler_manager_->FallbackCursorModeLockCursor(left, right, up,
3329 down);
3330}
3331
3332void RenderWidget::FallbackCursorModeSetCursorVisibility(bool visible) {
3333 widget_input_handler_manager_->FallbackCursorModeSetCursorVisibility(visible);
3334}
3335
danakj35c203a2019-04-16 17:12:393336void RenderWidget::SetAllowGpuRasterization(bool allow_gpu_raster) {
3337 layer_tree_view_->layer_tree_host()->SetHasGpuRasterizationTrigger(
3338 allow_gpu_raster);
3339}
3340
W. James MacLean233a64ff2019-05-18 16:18:493341void RenderWidget::SetPageScaleStateAndLimits(float page_scale_factor,
3342 bool is_pinch_gesture_active,
3343 float minimum,
3344 float maximum) {
danakj77821e252019-03-27 19:58:043345 layer_tree_view_->layer_tree_host()->SetPageScaleFactorAndLimits(
3346 page_scale_factor, minimum, maximum);
W. James MacLean233a64ff2019-05-18 16:18:493347
3348 // Only continue if this is a mainframe, or something's actually changed.
3349 if (!delegate() ||
3350 (page_scale_factor == page_scale_factor_from_mainframe_ &&
3351 is_pinch_gesture_active == is_pinch_gesture_active_from_mainframe_)) {
3352 return;
3353 }
3354
3355 // The page scale is controlled by the WebView for the local main frame of
3356 // the Page. So this is called from blink by for the RenderWidget of that
3357 // local main frame. We forward the value on to each child RenderWidget (each
3358 // of which will be via proxy child frame). These will each in turn forward
3359 // the message to their child RenderWidgets (through their proxy child
3360 // frames).
3361 DCHECK(!is_frozen_);
3362
3363 for (auto& observer : render_frame_proxies_) {
3364 observer.OnPageScaleFactorChanged(page_scale_factor,
3365 is_pinch_gesture_active);
3366 }
3367 // Store the value to give to any new RenderFrameProxy that is registered.
3368 page_scale_factor_from_mainframe_ = page_scale_factor;
3369 is_pinch_gesture_active_from_mainframe_ = is_pinch_gesture_active;
danakj77821e252019-03-27 19:58:043370}
3371
3372void RenderWidget::StartPageScaleAnimation(const gfx::Vector2d& target_offset,
3373 bool use_anchor,
3374 float new_page_scale,
3375 double duration_sec) {
3376 base::TimeDelta duration = base::TimeDelta::FromSecondsD(duration_sec);
3377 layer_tree_view_->layer_tree_host()->StartPageScaleAnimation(
3378 target_offset, use_anchor, new_page_scale, duration);
3379}
3380
danakjda15621e2019-04-05 20:34:433381void RenderWidget::RequestDecode(const cc::PaintImage& image,
3382 base::OnceCallback<void(bool)> callback) {
3383 layer_tree_view_->layer_tree_host()->QueueImageDecode(image,
3384 std::move(callback));
3385}
3386
danakj2d2889a2019-04-05 21:22:083387// Enables measuring and reporting both presentation times and swap times in
3388// swap promises.
3389class ReportTimeSwapPromise : public cc::SwapPromise {
3390 using ReportTimeCallback = blink::WebWidgetClient::ReportTimeCallback;
3391
3392 public:
Mohsen Izadid6715832019-06-05 00:32:513393 ReportTimeSwapPromise(ReportTimeCallback swap_time_callback,
3394 ReportTimeCallback presentation_time_callback,
danakj2d2889a2019-04-05 21:22:083395 scoped_refptr<base::SingleThreadTaskRunner> task_runner,
3396 base::WeakPtr<RenderWidget> render_widget)
Mohsen Izadid6715832019-06-05 00:32:513397 : swap_time_callback_(std::move(swap_time_callback)),
3398 presentation_time_callback_(std::move(presentation_time_callback)),
danakj2d2889a2019-04-05 21:22:083399 task_runner_(std::move(task_runner)),
3400 render_widget_(std::move(render_widget)) {}
3401 ~ReportTimeSwapPromise() override = default;
3402
3403 void DidActivate() override {}
3404
3405 void WillSwap(viz::CompositorFrameMetadata* metadata) override {
3406 DCHECK_GT(metadata->frame_token, 0u);
3407 // The interval between the current swap and its presentation time is
3408 // reported in UMA (see corresponding code in DidSwap() below).
3409 frame_token_ = metadata->frame_token;
3410 }
3411 void DidSwap() override {
3412 DCHECK_GT(frame_token_, 0u);
3413 task_runner_->PostTask(
Mohsen Izadid6715832019-06-05 00:32:513414 FROM_HERE, base::BindOnce(&RunCallbackAfterSwap, base::TimeTicks::Now(),
3415 std::move(swap_time_callback_),
3416 std::move(presentation_time_callback_),
3417 std::move(render_widget_), frame_token_));
danakj2d2889a2019-04-05 21:22:083418 }
3419
Bo Liudef993a2019-04-24 20:08:493420 cc::SwapPromise::DidNotSwapAction DidNotSwap(
3421 DidNotSwapReason reason) override {
danakj2d2889a2019-04-05 21:22:083422 blink::WebWidgetClient::SwapResult result;
3423 switch (reason) {
3424 case cc::SwapPromise::DidNotSwapReason::SWAP_FAILS:
3425 result = blink::WebWidgetClient::SwapResult::kDidNotSwapSwapFails;
3426 break;
3427 case cc::SwapPromise::DidNotSwapReason::COMMIT_FAILS:
3428 result = blink::WebWidgetClient::SwapResult::kDidNotSwapCommitFails;
3429 break;
3430 case cc::SwapPromise::DidNotSwapReason::COMMIT_NO_UPDATE:
3431 result = blink::WebWidgetClient::SwapResult::kDidNotSwapCommitNoUpdate;
3432 break;
3433 case cc::SwapPromise::DidNotSwapReason::ACTIVATION_FAILS:
3434 result = blink::WebWidgetClient::SwapResult::kDidNotSwapActivationFails;
3435 break;
3436 }
3437 // During a failed swap, return the current time regardless of whether we're
3438 // using presentation or swap timestamps.
3439 task_runner_->PostTask(
3440 FROM_HERE,
Mohsen Izadid6715832019-06-05 00:32:513441 base::BindOnce(
3442 [](blink::WebWidgetClient::SwapResult result,
3443 base::TimeTicks swap_time, ReportTimeCallback swap_time_callback,
3444 ReportTimeCallback presentation_time_callback) {
3445 ReportTime(std::move(swap_time_callback), result, swap_time);
3446 ReportTime(std::move(presentation_time_callback), result,
3447 swap_time);
3448 },
3449 result, base::TimeTicks::Now(), std::move(swap_time_callback_),
3450 std::move(presentation_time_callback_)));
Bo Liudef993a2019-04-24 20:08:493451 return DidNotSwapAction::BREAK_PROMISE;
danakj2d2889a2019-04-05 21:22:083452 }
3453
3454 int64_t TraceId() const override { return 0; }
3455
3456 private:
Mohsen Izadid6715832019-06-05 00:32:513457 static void RunCallbackAfterSwap(
3458 base::TimeTicks swap_time,
3459 ReportTimeCallback swap_time_callback,
3460 ReportTimeCallback presentation_time_callback,
3461 base::WeakPtr<RenderWidget> render_widget,
3462 int frame_token) {
3463 if (render_widget) {
3464 render_widget->layer_tree_view()->AddPresentationCallback(
3465 frame_token,
3466 base::BindOnce(&RunCallbackAfterPresentation,
3467 std::move(presentation_time_callback), swap_time));
3468 ReportTime(std::move(swap_time_callback),
3469 blink::WebWidgetClient::SwapResult::kDidSwap, swap_time);
3470 } else {
3471 ReportTime(std::move(swap_time_callback),
3472 blink::WebWidgetClient::SwapResult::kDidSwap, swap_time);
3473 ReportTime(std::move(presentation_time_callback),
3474 blink::WebWidgetClient::SwapResult::kDidSwap, swap_time);
3475 }
3476 }
3477
3478 static void RunCallbackAfterPresentation(
3479 ReportTimeCallback presentation_time_callback,
danakj2d2889a2019-04-05 21:22:083480 base::TimeTicks swap_time,
3481 base::TimeTicks presentation_time) {
3482 DCHECK(!swap_time.is_null());
3483 bool presentation_time_is_valid =
3484 !presentation_time.is_null() && (presentation_time > swap_time);
3485 UMA_HISTOGRAM_BOOLEAN("PageLoad.Internal.Renderer.PresentationTime.Valid",
3486 presentation_time_is_valid);
3487 if (presentation_time_is_valid) {
3488 // This measures from 1ms to 10seconds.
3489 UMA_HISTOGRAM_TIMES(
3490 "PageLoad.Internal.Renderer.PresentationTime.DeltaFromSwapTime",
3491 presentation_time - swap_time);
3492 }
Mohsen Izadid6715832019-06-05 00:32:513493 ReportTime(std::move(presentation_time_callback),
3494 blink::WebWidgetClient::SwapResult::kDidSwap,
3495 presentation_time_is_valid ? presentation_time : swap_time);
danakj2d2889a2019-04-05 21:22:083496 }
3497
Mohsen Izadid6715832019-06-05 00:32:513498 static void ReportTime(ReportTimeCallback callback,
3499 blink::WebWidgetClient::SwapResult result,
3500 base::TimeTicks time) {
3501 if (callback)
3502 std::move(callback).Run(result, time);
3503 }
3504
3505 ReportTimeCallback swap_time_callback_;
3506 ReportTimeCallback presentation_time_callback_;
danakj2d2889a2019-04-05 21:22:083507 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
3508 base::WeakPtr<RenderWidget> render_widget_;
3509 uint32_t frame_token_ = 0;
3510
3511 DISALLOW_COPY_AND_ASSIGN(ReportTimeSwapPromise);
3512};
3513
3514void RenderWidget::NotifySwapTime(ReportTimeCallback callback) {
Mohsen Izadid6715832019-06-05 00:32:513515 NotifySwapAndPresentationTime(base::NullCallback(), std::move(callback));
3516}
3517
3518void RenderWidget::NotifySwapAndPresentationTime(
3519 ReportTimeCallback swap_time_callback,
3520 ReportTimeCallback presentation_time_callback) {
danakj2d2889a2019-04-05 21:22:083521 cc::LayerTreeHost* layer_tree_host = layer_tree_view_->layer_tree_host();
3522 // When the WebWidget is closed we cancel any pending SwapPromise that would
3523 // call back into blink, so we use |close_weak_ptr_factory_|.
3524 layer_tree_host->QueueSwapPromise(std::make_unique<ReportTimeSwapPromise>(
Mohsen Izadid6715832019-06-05 00:32:513525 std::move(swap_time_callback), std::move(presentation_time_callback),
danakj2d2889a2019-04-05 21:22:083526 layer_tree_host->GetTaskRunnerProvider()->MainThreadTaskRunner(),
3527 close_weak_ptr_factory_.GetWeakPtr()));
3528}
3529
Dave Tapuskabafc2ba32017-11-28 01:54:373530void RenderWidget::RequestUnbufferedInputEvents() {
3531 if (input_event_queue_)
3532 input_event_queue_->RequestUnbufferedInputEvents();
3533}
3534
xidachenfa0199e72017-05-11 11:34:263535void RenderWidget::SetTouchAction(cc::TouchAction touch_action) {
Dave Tapuskab28a1082017-08-30 15:37:263536 if (!input_handler_->ProcessTouchAction(touch_action))
[email protected]5d0bbdfa92013-12-10 00:35:513537 return;
3538
Dave Tapuska04bc5ee92018-04-17 19:03:313539 widget_input_handler_manager_->ProcessTouchAction(touch_action);
[email protected]5d0bbdfa92013-12-10 00:35:513540}
3541
[email protected]e3244ed2014-06-20 20:04:273542void RenderWidget::RegisterRenderFrameProxy(RenderFrameProxy* proxy) {
3543 render_frame_proxies_.AddObserver(proxy);
danakj0246bba02019-01-23 23:12:273544 // Page scale factor is propagated down the RenderWidget tree (across
3545 // frame trees). A new RenderFrameProxy means there is a new child
3546 // RenderWidget in another frame tree. In order for it to hear about
3547 // the page scale factor we pass along the last seen value here.
W. James MacLean2cd99b62019-04-08 13:54:433548 proxy->OnPageScaleFactorChanged(page_scale_factor_from_mainframe_,
3549 is_pinch_gesture_active_from_mainframe_);
[email protected]bffc8302014-01-23 20:52:163550}
3551
[email protected]e3244ed2014-06-20 20:04:273552void RenderWidget::UnregisterRenderFrameProxy(RenderFrameProxy* proxy) {
3553 render_frame_proxies_.RemoveObserver(proxy);
[email protected]bffc8302014-01-23 20:52:163554}
3555
[email protected]de3c5d82014-05-28 22:12:593556void RenderWidget::RegisterRenderFrame(RenderFrameImpl* frame) {
3557 render_frames_.AddObserver(frame);
3558}
3559
3560void RenderWidget::UnregisterRenderFrame(RenderFrameImpl* frame) {
3561 render_frames_.RemoveObserver(frame);
3562}
3563
Saman Samid189e5dfd2017-12-20 22:55:313564void RenderWidget::RegisterBrowserPlugin(BrowserPlugin* browser_plugin) {
3565 browser_plugins_.AddObserver(browser_plugin);
Saman Sami50d1e0c2018-03-13 20:03:493566 browser_plugin->ScreenInfoChanged(GetOriginalScreenInfo());
Saman Samid189e5dfd2017-12-20 22:55:313567}
3568
3569void RenderWidget::UnregisterBrowserPlugin(BrowserPlugin* browser_plugin) {
3570 browser_plugins_.RemoveObserver(browser_plugin);
3571}
3572
Albert J. Wong3c93c182018-09-27 17:29:433573void RenderWidget::OnWaitNextFrameForTests(
3574 int main_frame_thread_observer_routing_id) {
danakj53802692018-07-25 21:46:443575 // Sends an ACK to the browser process during the next compositor frame.
Albert J. Wong3c93c182018-09-27 17:29:433576 QueueMessage(new WidgetHostMsg_WaitForNextFrameForTests_ACK(
3577 main_frame_thread_observer_routing_id));
lfg43e08e62016-02-03 18:51:373578}
3579
Fady Samuela863f152018-03-09 16:10:033580const ScreenInfo& RenderWidget::GetWebScreenInfo() const {
3581 return screen_info_;
Christopher Camerone9a30c12018-03-07 08:23:383582}
3583
Fady Samuela863f152018-03-09 16:10:033584const ScreenInfo& RenderWidget::GetOriginalScreenInfo() const {
Christopher Camerone9a30c12018-03-07 08:23:383585 return screen_metrics_emulator_
3586 ? screen_metrics_emulator_->original_screen_info()
Fady Samuela863f152018-03-09 16:10:033587 : screen_info_;
oshima50872a72016-03-04 13:26:183588}
3589
Ella Ge80a52dce2017-11-15 18:01:523590gfx::PointF RenderWidget::ConvertWindowPointToViewport(
3591 const gfx::PointF& point) {
paulmeyer90f6c31d2016-11-12 00:17:593592 blink::WebFloatRect point_in_viewport(point.x(), point.y(), 0, 0);
Blink Reformat1c4d759e2017-04-09 16:34:543593 ConvertWindowToViewport(&point_in_viewport);
Ella Ge80a52dce2017-11-15 18:01:523594 return gfx::PointF(point_in_viewport.x, point_in_viewport.y);
3595}
3596
3597gfx::Point RenderWidget::ConvertWindowPointToViewport(const gfx::Point& point) {
3598 return gfx::ToRoundedPoint(ConvertWindowPointToViewport(gfx::PointF(point)));
paulmeyer90f6c31d2016-11-12 00:17:593599}
3600
Blink Reformat1c4d759e2017-04-09 16:34:543601bool RenderWidget::RequestPointerLock() {
lfgbee1e0a2016-06-08 21:24:213602 return mouse_lock_dispatcher_->LockMouse(webwidget_mouse_lock_target_.get());
3603}
3604
Blink Reformat1c4d759e2017-04-09 16:34:543605void RenderWidget::RequestPointerUnlock() {
lfgbee1e0a2016-06-08 21:24:213606 mouse_lock_dispatcher_->UnlockMouse(webwidget_mouse_lock_target_.get());
3607}
3608
Blink Reformat1c4d759e2017-04-09 16:34:543609bool RenderWidget::IsPointerLocked() {
lfgbee1e0a2016-06-08 21:24:213610 return mouse_lock_dispatcher_->IsMouseLockedTo(
3611 webwidget_mouse_lock_target_.get());
3612}
3613
Richard Li49fe04d2018-10-21 09:07:193614void RenderWidget::StartDragging(network::mojom::ReferrerPolicy policy,
paulmeyer6ef5a792016-11-08 20:33:583615 const WebDragData& data,
3616 WebDragOperationsMask mask,
danakj0c75ad82018-07-10 19:50:123617 const SkBitmap& drag_image,
danakjef1735a2018-11-13 19:34:443618 const gfx::Point& web_image_offset) {
3619 blink::WebRect offset_in_window(web_image_offset.x(), web_image_offset.y(), 0,
3620 0);
Blink Reformat1c4d759e2017-04-09 16:34:543621 ConvertViewportToWindow(&offset_in_window);
paulmeyer6ef5a792016-11-08 20:33:583622 DropData drop_data(DropDataBuilder::Build(data));
3623 drop_data.referrer_policy = policy;
danakj0c75ad82018-07-10 19:50:123624 gfx::Vector2d image_offset(offset_in_window.x, offset_in_window.y);
3625 Send(new DragHostMsg_StartDragging(routing_id(), drop_data, mask, drag_image,
3626 image_offset, possible_drag_event_info_));
paulmeyer6ef5a792016-11-08 20:33:583627}
3628
Saman Samif7731342018-01-24 22:18:443629void RenderWidget::DidNavigate() {
danakj125a29e2019-01-24 23:25:293630 // Blink may be navigating still between the Close IPC and the task that
3631 // actually closes this class, and for a main frame that would come through
3632 // this method. But since we are closing we can skip it.
3633 if (closing_)
Kinuko Yasudabef2b642019-01-24 04:53:333634 return;
danakj125a29e2019-01-24 23:25:293635
danakja6c10012018-07-06 14:25:363636 layer_tree_view_->ClearCachesOnNextCommit();
kenrb5d78b842017-03-06 21:06:013637}
3638
lfg8ff33912016-09-13 20:59:213639blink::WebWidget* RenderWidget::GetWebWidget() const {
Albert J. Wong7bbf22d2018-12-20 00:27:273640 if (delegate()) {
3641 blink::WebWidget* delegate_widget = delegate()->GetWebWidgetForWidget();
danakja2c9d0a92018-07-25 20:01:183642 if (delegate_widget)
3643 return delegate_widget;
3644 }
lfg8ff33912016-09-13 20:59:213645 return webwidget_internal_;
3646}
3647
ekaramad2daaf672016-11-10 20:29:013648blink::WebInputMethodController* RenderWidget::GetInputMethodController()
3649 const {
Ehsan Karamad655d7b8a2018-01-12 18:38:383650 if (auto* frame_widget = GetFrameWidget())
3651 return frame_widget->GetActiveWebInputMethodController();
3652
3653 return nullptr;
ekaramad2daaf672016-11-10 20:29:013654}
3655
Dave Tapuska139a72f2017-09-06 21:57:033656void RenderWidget::SetupWidgetInputHandler(
3657 mojom::WidgetInputHandlerRequest request,
3658 mojom::WidgetInputHandlerHostPtr host) {
Dave Tapuskab66c28f2017-11-15 17:18:473659 widget_input_handler_manager_->AddInterface(std::move(request),
3660 std::move(host));
Dave Tapuska525eb15e2017-08-17 21:05:503661}
3662
Dave Tapuska485aca92017-08-08 00:47:583663void RenderWidget::SetWidgetBinding(mojom::WidgetRequest request) {
3664 // Close the old binding if there was one.
3665 // A RenderWidgetHost should not need more than one channel.
3666 widget_binding_.Close();
3667 widget_binding_.Bind(std::move(request));
3668}
3669
Ken Buchanan94c0beb62018-06-22 19:56:243670void RenderWidget::SetMouseCapture(bool capture) {
3671 if (mojom::WidgetInputHandlerHost* host =
3672 widget_input_handler_manager_->GetWidgetInputHandlerHost()) {
3673 host->SetMouseCapture(capture);
3674 }
3675}
3676
Fady Samuelca9ecb72018-05-05 05:59:273677bool RenderWidget::IsSurfaceSynchronizationEnabled() const {
danakja6c10012018-07-06 14:25:363678 return layer_tree_view_ &&
3679 layer_tree_view_->IsSurfaceSynchronizationEnabled();
Fady Samuelca9ecb72018-05-05 05:59:273680}
3681
danakj53802692018-07-25 21:46:443682void RenderWidget::UseSynchronousResizeModeForTesting(bool enable) {
danakj7602f4f2019-01-31 18:03:233683 synchronous_resize_mode_for_testing_ = enable;
danakja2c9d0a92018-07-25 20:01:183684}
3685
3686void RenderWidget::SetDeviceScaleFactorForTesting(float factor) {
danakjb4cec102019-01-30 19:22:133687 DCHECK_GT(factor, 0.f);
danakja2c9d0a92018-07-25 20:01:183688
danakja2c9d0a92018-07-25 20:01:183689 // We are changing the device scale factor from the renderer, so allocate a
3690 // new viz::LocalSurfaceId to avoid surface invariants violations in tests.
danakj125a29e2019-01-24 23:25:293691 layer_tree_view_->RequestNewLocalSurfaceId();
danakjb4cec102019-01-30 19:22:133692
3693 ScreenInfo info = screen_info_;
3694 info.device_scale_factor = factor;
3695 gfx::Size viewport_pixel_size = gfx::ScaleToCeiledSize(size_, factor);
3696 UpdateSurfaceAndScreenInfo(local_surface_id_allocation_from_parent_,
3697 viewport_pixel_size, info);
3698
3699 ResizeWebWidget(); // This picks up the new device scale factor in |info|.
3700
3701 gfx::Size visible_viewport_size = visible_viewport_size_;
3702 if (compositor_deps_->IsUseZoomForDSFEnabled()) {
3703 visible_viewport_size =
3704 gfx::ScaleToCeiledSize(visible_viewport_size, factor);
3705 }
W. James MacLean7c8581072019-06-18 18:07:533706
3707 DCHECK(delegate()) << "Resizing the viewport for a cross-process subframe "
3708 "must be done via the RenderView.";
3709 delegate()->ResizeVisualViewportForWidget(visible_viewport_size);
danakjb4cec102019-01-30 19:22:133710
3711 // Make sure the DSF override stays for future VisualProperties updates, and
3712 // that includes overriding the VisualProperties'
3713 // compositor_viewport_pixel_size with size * this for-testing DSF.
3714 device_scale_factor_for_testing_ = factor;
danakja2c9d0a92018-07-25 20:01:183715}
3716
3717void RenderWidget::SetDeviceColorSpaceForTesting(
3718 const gfx::ColorSpace& color_space) {
danakjb4cec102019-01-30 19:22:133719 // We are changing the device color space from the renderer, so allocate a
3720 // new viz::LocalSurfaceId to avoid surface invariants violations in tests.
3721 layer_tree_view_->RequestNewLocalSurfaceId();
3722
danakj04d128a2019-01-25 02:10:203723 ScreenInfo info = screen_info_;
3724 info.color_space = color_space;
3725 UpdateSurfaceAndScreenInfo(local_surface_id_allocation_from_parent_,
danakj0d963bd2019-02-20 18:00:463726 CompositorViewportSize(), info);
danakja2c9d0a92018-07-25 20:01:183727}
3728
3729void RenderWidget::SetWindowRectSynchronouslyForTesting(
3730 const gfx::Rect& new_window_rect) {
3731 SetWindowRectSynchronously(new_window_rect);
3732}
3733
3734void RenderWidget::EnableAutoResizeForTesting(const gfx::Size& min_size,
3735 const gfx::Size& max_size) {
3736 VisualProperties visual_properties;
3737 visual_properties.auto_resize_enabled = true;
3738 visual_properties.min_size_for_auto_resize = min_size;
3739 visual_properties.max_size_for_auto_resize = max_size;
Fady Samuel84d8a1f72018-11-08 00:26:173740 visual_properties.local_surface_id_allocation =
3741 base::Optional<viz::LocalSurfaceIdAllocation>(
3742 viz::LocalSurfaceIdAllocation(
3743 viz::LocalSurfaceId(1, 1, base::UnguessableToken::Create()),
3744 base::TimeTicks::Now()));
danakja2c9d0a92018-07-25 20:01:183745 OnSynchronizeVisualProperties(visual_properties);
3746}
3747
3748void RenderWidget::DisableAutoResizeForTesting(const gfx::Size& new_size) {
3749 if (!auto_resize_mode_)
3750 return;
3751
3752 VisualProperties visual_properties;
3753 visual_properties.auto_resize_enabled = false;
3754 visual_properties.screen_info = screen_info_;
3755 visual_properties.new_size = new_size;
danakj0d963bd2019-02-20 18:00:463756 visual_properties.compositor_viewport_pixel_size = CompositorViewportSize();
danakja2c9d0a92018-07-25 20:01:183757 visual_properties.browser_controls_shrink_blink_size =
3758 browser_controls_shrink_blink_size_;
3759 visual_properties.top_controls_height = top_controls_height_;
3760 visual_properties.visible_viewport_size = visible_viewport_size_;
3761 visual_properties.is_fullscreen_granted = is_fullscreen_granted_;
3762 visual_properties.display_mode = display_mode_;
3763 OnSynchronizeVisualProperties(visual_properties);
3764}
3765
EhsanK955ba582017-11-30 21:14:403766blink::WebLocalFrame* RenderWidget::GetFocusedWebLocalFrameInWidget() const {
Ehsan Karamad655d7b8a2018-01-12 18:38:383767 if (auto* frame_widget = GetFrameWidget())
3768 return frame_widget->FocusedWebLocalFrameInWidget();
3769 return nullptr;
EhsanK955ba582017-11-30 21:14:403770}
3771
EhsanK22d482e2017-08-10 17:29:493772#if BUILDFLAG(ENABLE_PLUGINS)
3773PepperPluginInstanceImpl* RenderWidget::GetFocusedPepperPluginInsideWidget() {
Ehsan Karamad655d7b8a2018-01-12 18:38:383774 blink::WebFrameWidget* frame_widget = GetFrameWidget();
3775 if (!frame_widget)
EhsanK22d482e2017-08-10 17:29:493776 return nullptr;
3777
3778 // Focused pepper instance might not always be in the focused frame. For
3779 // instance if a pepper instance and its embedder frame are focused an then
3780 // another frame takes focus using javascript, the embedder frame will no
3781 // longer be focused while the pepper instance is (the embedder frame's
3782 // |focused_pepper_plugin_| is not nullptr). Especially, if the pepper plugin
3783 // is fullscreen, clicking into the pepper will not refocus the embedder
3784 // frame. This is why we have to traverse the whole frame tree to find the
3785 // focused plugin.
Ehsan Karamad655d7b8a2018-01-12 18:38:383786 blink::WebFrame* current_frame = frame_widget->LocalRoot();
EhsanK22d482e2017-08-10 17:29:493787 while (current_frame) {
3788 RenderFrameImpl* render_frame =
3789 current_frame->IsWebLocalFrame()
3790 ? RenderFrameImpl::FromWebFrame(current_frame)
3791 : nullptr;
3792 if (render_frame && render_frame->focused_pepper_plugin())
3793 return render_frame->focused_pepper_plugin();
3794 current_frame = current_frame->TraverseNext();
3795 }
3796 return nullptr;
3797}
3798#endif
3799
Ken Buchananb2c9e262018-03-10 16:53:313800gfx::Rect RenderWidget::ViewportVisibleRect() {
danakj9c4e7b812019-02-19 22:04:273801 if (for_child_local_root_frame_)
3802 return compositor_visible_rect_;
danakj0d963bd2019-02-20 18:00:463803 return gfx::Rect(CompositorViewportSize());
Ken Buchananb2c9e262018-03-10 16:53:313804}
3805
Hajime Hoshi315a61f2018-08-14 17:27:283806// static
3807scoped_refptr<base::SingleThreadTaskRunner>
3808RenderWidget::GetCleanupTaskRunner() {
3809 return RenderThreadImpl::current_blink_platform_impl()
3810 ->main_thread_scheduler()
3811 ->CleanupTaskRunner();
3812}
3813
Saman Sami50d1e0c2018-03-13 20:03:493814base::WeakPtr<RenderWidget> RenderWidget::AsWeakPtr() {
3815 return weak_ptr_factory_.GetWeakPtr();
3816}
3817
[email protected]e9ff79c2012-10-19 21:31:263818} // namespace content