blob: cc8e8d5bb88df72be85d0ac32331efe444f90b33 [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
lfgbee1e0a2016-06-08 21:24:21183class WebWidgetLockTarget : public content::MouseLockDispatcher::LockTarget {
184 public:
185 explicit WebWidgetLockTarget(blink::WebWidget* webwidget)
186 : webwidget_(webwidget) {}
187
188 void OnLockMouseACK(bool succeeded) override {
189 if (succeeded)
Blink Reformat1c4d759e2017-04-09 16:34:54190 webwidget_->DidAcquirePointerLock();
lfgbee1e0a2016-06-08 21:24:21191 else
Blink Reformat1c4d759e2017-04-09 16:34:54192 webwidget_->DidNotAcquirePointerLock();
lfgbee1e0a2016-06-08 21:24:21193 }
194
Blink Reformat1c4d759e2017-04-09 16:34:54195 void OnMouseLockLost() override { webwidget_->DidLosePointerLock(); }
lfgbee1e0a2016-06-08 21:24:21196
197 bool HandleMouseLockedInputEvent(const blink::WebMouseEvent& event) override {
198 // The WebWidget handles mouse lock in Blink's handleInputEvent().
199 return false;
200 }
201
202 private:
203 blink::WebWidget* webwidget_;
204};
205
Stephen Chenneyd56b5382019-01-29 00:37:02206class ScopedUkmRafAlignedInputTimer {
207 public:
208 explicit ScopedUkmRafAlignedInputTimer(blink::WebWidget* webwidget)
209 : webwidget_(webwidget) {
210 webwidget_->BeginRafAlignedInput();
211 }
212
213 ~ScopedUkmRafAlignedInputTimer() { webwidget_->EndRafAlignedInput(); }
214
215 private:
216 blink::WebWidget* webwidget_;
217
218 DISALLOW_COPY_AND_ASSIGN(ScopedUkmRafAlignedInputTimer);
219};
220
dglazkov97b6c2b2016-10-25 17:35:55221bool IsDateTimeInput(ui::TextInputType type) {
222 return type == ui::TEXT_INPUT_TYPE_DATE ||
223 type == ui::TEXT_INPUT_TYPE_DATE_TIME ||
224 type == ui::TEXT_INPUT_TYPE_DATE_TIME_LOCAL ||
225 type == ui::TEXT_INPUT_TYPE_MONTH ||
226 type == ui::TEXT_INPUT_TYPE_TIME || type == ui::TEXT_INPUT_TYPE_WEEK;
227}
228
paulmeyer90f6c31d2016-11-12 00:17:59229WebDragData DropMetaDataToWebDragData(
230 const std::vector<DropData::Metadata>& drop_meta_data) {
231 std::vector<WebDragData::Item> item_list;
232 for (const auto& meta_data_item : drop_meta_data) {
233 if (meta_data_item.kind == DropData::Kind::STRING) {
234 WebDragData::Item item;
Blink Reformat1c4d759e2017-04-09 16:34:54235 item.storage_type = WebDragData::Item::kStorageTypeString;
236 item.string_type = WebString::FromUTF16(meta_data_item.mime_type);
paulmeyer90f6c31d2016-11-12 00:17:59237 // Have to pass a dummy URL here instead of an empty URL because the
238 // DropData received by browser_plugins goes through a round trip:
239 // DropData::MetaData --> WebDragData-->DropData. In the end, DropData
240 // will contain an empty URL (which means no URL is dragged) if the URL in
241 // WebDragData is empty.
Maksim Sisova33072d22019-01-24 15:44:25242 if (base::EqualsASCII(meta_data_item.mime_type, ui::kMimeTypeURIList)) {
Blink Reformat1c4d759e2017-04-09 16:34:54243 item.string_data = WebString::FromUTF8("about:dragdrop-placeholder");
paulmeyer90f6c31d2016-11-12 00:17:59244 }
245 item_list.push_back(item);
246 continue;
247 }
248
249 // TODO(hush): crbug.com/584789. Blink needs to support creating a file with
250 // just the mimetype. This is needed to drag files to WebView on Android
251 // platform.
252 if ((meta_data_item.kind == DropData::Kind::FILENAME) &&
253 !meta_data_item.filename.empty()) {
254 WebDragData::Item item;
Blink Reformat1c4d759e2017-04-09 16:34:54255 item.storage_type = WebDragData::Item::kStorageTypeFilename;
256 item.filename_data = blink::FilePathToWebString(meta_data_item.filename);
paulmeyer90f6c31d2016-11-12 00:17:59257 item_list.push_back(item);
258 continue;
259 }
260
261 if (meta_data_item.kind == DropData::Kind::FILESYSTEMFILE) {
262 WebDragData::Item item;
Blink Reformat1c4d759e2017-04-09 16:34:54263 item.storage_type = WebDragData::Item::kStorageTypeFileSystemFile;
264 item.file_system_url = meta_data_item.file_system_url;
paulmeyer90f6c31d2016-11-12 00:17:59265 item_list.push_back(item);
266 continue;
267 }
268 }
269
270 WebDragData result;
Blink Reformat1c4d759e2017-04-09 16:34:54271 result.Initialize();
272 result.SetItems(item_list);
paulmeyer90f6c31d2016-11-12 00:17:59273 return result;
274}
275
276WebDragData DropDataToWebDragData(const DropData& drop_data) {
277 std::vector<WebDragData::Item> item_list;
278
279 // These fields are currently unused when dragging into WebKit.
280 DCHECK(drop_data.download_metadata.empty());
281 DCHECK(drop_data.file_contents.empty());
dcheng3dd85612017-02-08 10:46:23282 DCHECK(drop_data.file_contents_content_disposition.empty());
paulmeyer90f6c31d2016-11-12 00:17:59283
284 if (!drop_data.text.is_null()) {
285 WebDragData::Item item;
Blink Reformat1c4d759e2017-04-09 16:34:54286 item.storage_type = WebDragData::Item::kStorageTypeString;
Maksim Sisova33072d22019-01-24 15:44:25287 item.string_type = WebString::FromUTF8(ui::kMimeTypeText);
Blink Reformat1c4d759e2017-04-09 16:34:54288 item.string_data = WebString::FromUTF16(drop_data.text.string());
paulmeyer90f6c31d2016-11-12 00:17:59289 item_list.push_back(item);
290 }
291
292 if (!drop_data.url.is_empty()) {
293 WebDragData::Item item;
Blink Reformat1c4d759e2017-04-09 16:34:54294 item.storage_type = WebDragData::Item::kStorageTypeString;
Maksim Sisova33072d22019-01-24 15:44:25295 item.string_type = WebString::FromUTF8(ui::kMimeTypeURIList);
Blink Reformat1c4d759e2017-04-09 16:34:54296 item.string_data = WebString::FromUTF8(drop_data.url.spec());
297 item.title = WebString::FromUTF16(drop_data.url_title);
paulmeyer90f6c31d2016-11-12 00:17:59298 item_list.push_back(item);
299 }
300
301 if (!drop_data.html.is_null()) {
302 WebDragData::Item item;
Blink Reformat1c4d759e2017-04-09 16:34:54303 item.storage_type = WebDragData::Item::kStorageTypeString;
Maksim Sisova33072d22019-01-24 15:44:25304 item.string_type = WebString::FromUTF8(ui::kMimeTypeHTML);
Blink Reformat1c4d759e2017-04-09 16:34:54305 item.string_data = WebString::FromUTF16(drop_data.html.string());
306 item.base_url = drop_data.html_base_url;
paulmeyer90f6c31d2016-11-12 00:17:59307 item_list.push_back(item);
308 }
309
Darwin Huangb2ad4892019-05-13 20:15:39310 for (const ui::FileInfo& filename : drop_data.filenames) {
paulmeyer90f6c31d2016-11-12 00:17:59311 WebDragData::Item item;
Blink Reformat1c4d759e2017-04-09 16:34:54312 item.storage_type = WebDragData::Item::kStorageTypeFilename;
Darwin Huangb2ad4892019-05-13 20:15:39313 item.filename_data = blink::FilePathToWebString(filename.path);
Blink Reformat1c4d759e2017-04-09 16:34:54314 item.display_name_data =
Darwin Huangb2ad4892019-05-13 20:15:39315 blink::FilePathToWebString(base::FilePath(filename.display_name));
paulmeyer90f6c31d2016-11-12 00:17:59316 item_list.push_back(item);
317 }
318
Darwin Huangb2ad4892019-05-13 20:15:39319 for (const DropData::FileSystemFileInfo& file : drop_data.file_system_files) {
paulmeyer90f6c31d2016-11-12 00:17:59320 WebDragData::Item item;
Blink Reformat1c4d759e2017-04-09 16:34:54321 item.storage_type = WebDragData::Item::kStorageTypeFileSystemFile;
Darwin Huangb2ad4892019-05-13 20:15:39322 item.file_system_url = file.url;
323 item.file_system_file_size = file.size;
324 item.file_system_id = blink::WebString::FromASCII(file.filesystem_id);
paulmeyer90f6c31d2016-11-12 00:17:59325 item_list.push_back(item);
326 }
327
Darwin Huangb2ad4892019-05-13 20:15:39328 for (const auto& data : drop_data.custom_data) {
paulmeyer90f6c31d2016-11-12 00:17:59329 WebDragData::Item item;
Blink Reformat1c4d759e2017-04-09 16:34:54330 item.storage_type = WebDragData::Item::kStorageTypeString;
Darwin Huangb2ad4892019-05-13 20:15:39331 item.string_type = WebString::FromUTF16(data.first);
332 item.string_data = WebString::FromUTF16(data.second);
paulmeyer90f6c31d2016-11-12 00:17:59333 item_list.push_back(item);
334 }
335
336 WebDragData result;
Blink Reformat1c4d759e2017-04-09 16:34:54337 result.Initialize();
338 result.SetItems(item_list);
339 result.SetFilesystemId(WebString::FromUTF16(drop_data.filesystem_id));
paulmeyer90f6c31d2016-11-12 00:17:59340 return result;
341}
342
dtapuskac4dd5be2016-10-25 15:11:10343ui::TextInputType ConvertWebTextInputType(blink::WebTextInputType type) {
344 // Check the type is in the range representable by ui::TextInputType.
345 DCHECK_LE(type, static_cast<int>(ui::TEXT_INPUT_TYPE_MAX))
346 << "blink::WebTextInputType and ui::TextInputType not synchronized";
347 return static_cast<ui::TextInputType>(type);
348}
349
350ui::TextInputMode ConvertWebTextInputMode(blink::WebTextInputMode mode) {
351 // Check the mode is in the range representable by ui::TextInputMode.
352 DCHECK_LE(mode, static_cast<int>(ui::TEXT_INPUT_MODE_MAX))
353 << "blink::WebTextInputMode and ui::TextInputMode not synchronized";
354 return static_cast<ui::TextInputMode>(mode);
355}
356
Mitsuru Oshima3c07b332018-02-06 04:56:50357// Returns true if the device scale is high enough that losing subpixel
358// antialiasing won't have a noticeable effect on text quality.
359static bool DeviceScaleEnsuresTextQuality(float device_scale_factor) {
360#if defined(OS_ANDROID) || defined(OS_CHROMEOS)
361 // On Android, we never have subpixel antialiasing. On Chrome OS we prefer to
362 // composite all scrollers so that we get animated overlay scrollbars.
363 return true;
364#else
365 // 1.5 is a common touchscreen tablet device scale factor. For such
366 // devices main thread antialiasing is a heavy burden.
367 return device_scale_factor >= 1.5f;
368#endif
369}
370
371static bool PreferCompositingToLCDText(CompositorDependencies* compositor_deps,
372 float device_scale_factor) {
373 const base::CommandLine& command_line =
374 *base::CommandLine::ForCurrentProcess();
375 if (command_line.HasSwitch(switches::kDisablePreferCompositingToLCDText))
376 return false;
377 if (command_line.HasSwitch(switches::kEnablePreferCompositingToLCDText))
378 return true;
379 if (!compositor_deps->IsLcdTextEnabled())
380 return true;
381 return DeviceScaleEnsuresTextQuality(device_scale_factor);
382}
383
[email protected]b256eca2013-07-11 10:57:40384} // namespace
385
[email protected]b2e4c70132013-10-03 02:07:51386// RenderWidget ---------------------------------------------------------------
387
Albert J. Wong2727e8a82019-02-15 16:56:11388// static
389void RenderWidget::InstallCreateForFrameHook(
390 CreateRenderWidgetFunction create_widget) {
391 g_create_render_widget_for_frame = create_widget;
392}
393
394scoped_refptr<RenderWidget> RenderWidget::CreateForFrame(
395 int32_t widget_routing_id,
396 CompositorDependencies* compositor_deps,
397 const ScreenInfo& screen_info,
398 blink::WebDisplayMode display_mode,
399 bool is_frozen,
400 bool hidden,
401 bool never_visible,
402 mojom::WidgetRequest widget_request) {
403 if (g_create_render_widget_for_frame) {
404 return g_create_render_widget_for_frame(
405 widget_routing_id, compositor_deps, screen_info, display_mode,
406 is_frozen, hidden, never_visible, std::move(widget_request));
407 }
408
409 return base::WrapRefCounted(new RenderWidget(
410 widget_routing_id, compositor_deps, screen_info, display_mode, is_frozen,
411 hidden, never_visible, std::move(widget_request)));
412}
413
414scoped_refptr<RenderWidget> RenderWidget::CreateForPopup(
415 int32_t widget_routing_id,
416 CompositorDependencies* compositor_deps,
417 const ScreenInfo& screen_info,
418 blink::WebDisplayMode display_mode,
419 bool is_frozen,
420 bool hidden,
421 bool never_visible,
422 mojom::WidgetRequest widget_request) {
423 return base::WrapRefCounted(new RenderWidget(
424 widget_routing_id, compositor_deps, screen_info, display_mode, is_frozen,
425 hidden, never_visible, std::move(widget_request)));
426}
427
danakj829cdd142018-09-14 21:13:27428RenderWidget::RenderWidget(int32_t widget_routing_id,
429 CompositorDependencies* compositor_deps,
danakj829cdd142018-09-14 21:13:27430 const ScreenInfo& screen_info,
431 blink::WebDisplayMode display_mode,
danakj6dcbc5962018-11-16 16:45:42432 bool is_frozen,
danakj829cdd142018-09-14 21:13:27433 bool hidden,
434 bool never_visible,
435 mojom::WidgetRequest widget_request)
nick8331f8ad2016-11-15 20:42:45436 : routing_id_(widget_routing_id),
dcheng35d31c112015-07-22 00:17:36437 compositor_deps_(compositor_deps),
lfg8ff33912016-09-13 20:59:21438 webwidget_internal_(nullptr),
[email protected]847a2582013-03-09 02:29:51439 auto_resize_mode_(false),
[email protected]1ac10dca2013-08-20 20:47:04440 is_hidden_(hidden),
danakj2d2889a2019-04-05 21:22:08441 compositor_never_visible_(never_visible),
mikhail.pozdnyakovf2c902a2015-04-14 08:09:12442 is_fullscreen_granted_(false),
danakj73dd3032018-07-28 17:49:53443 display_mode_(display_mode),
changwanf2a707b2015-10-30 08:22:16444 ime_event_guard_(nullptr),
danakj6dcbc5962018-11-16 16:45:42445 is_frozen_(is_frozen),
dglazkov97b6c2b2016-10-25 17:35:55446 text_input_type_(ui::TEXT_INPUT_TYPE_NONE),
[email protected]b256eca2013-07-11 10:57:40447 text_input_mode_(ui::TEXT_INPUT_MODE_DEFAULT),
shuchen82ce8c52014-10-23 01:55:20448 text_input_flags_(0),
AJITH KUMAR V041c0b02017-08-08 10:39:20449 next_previous_flags_(kInvalidNextPreviousFlagsValue),
[email protected]86ba5fcb2013-09-04 00:36:53450 can_compose_inline_(true),
nonafa291792016-08-10 02:36:18451 composition_range_(gfx::Range::InvalidRange()),
[email protected]867125a02009-12-10 06:01:48452 pending_window_rect_count_(0),
Scott Violetae08f332018-07-27 17:37:23453 screen_info_(screen_info),
nonafa291792016-08-10 02:36:18454 monitor_composition_info_(false),
[email protected]b2e4c70132013-10-03 02:07:51455 popup_origin_scale_for_emulation_(0.f),
samans26510e442017-06-01 22:29:12456 frame_swap_message_queue_(new FrameSwapMessageQueue(routing_id_)),
lfge0c2792ec2016-05-11 18:52:08457 has_host_context_menu_location_(false),
ekaramad2a46d632016-07-19 13:33:09458 has_focus_(false),
danakj6dcbc5962018-11-16 16:45:42459 for_child_local_root_frame_(false),
ekaramad330ba4232016-09-23 17:57:47460#if defined(OS_MACOSX)
461 text_input_client_observer_(new TextInputClientObserver(this)),
462#endif
Takashi SAKAMOTO870f6262017-08-22 04:08:27463 first_update_visual_state_after_hidden_(false),
tasakb95dbb50c2017-02-08 18:07:50464 was_shown_time_(base::TimeTicks::Now()),
kenrb5d78b842017-03-06 21:06:01465 current_content_source_id_(0),
danakj2d2889a2019-04-05 21:22:08466 widget_binding_(this, std::move(widget_request)) {
nick8331f8ad2016-11-15 20:42:45467 DCHECK_NE(routing_id_, MSG_ROUTING_NONE);
Avi Drissman07d22452019-03-25 17:56:39468 DCHECK(RenderThread::IsMainThread());
alexclarke7fa93942015-10-21 15:37:11469
470 // In tests there may not be a RenderThreadImpl.
471 if (RenderThreadImpl::current()) {
472 render_widget_scheduling_state_ = RenderThreadImpl::current()
Yuta Kitamura3331f5c2018-04-05 11:12:25473 ->GetWebMainThreadScheduler()
dcheng07945f632015-12-26 07:59:32474 ->NewRenderWidgetSchedulingState();
alexclarke7fa93942015-10-21 15:37:11475 render_widget_scheduling_state_->SetHidden(is_hidden_);
476 }
Albert J. Wongcb004632018-07-10 22:58:25477
478 if (routing_id_ != MSG_ROUTING_NONE)
479 g_routing_id_widget_map.Get().emplace(routing_id_, this);
initial.commit09911bf2008-07-26 23:55:29480}
481
482RenderWidget::~RenderWidget() {
lfg8ff33912016-09-13 20:59:21483 DCHECK(!webwidget_internal_) << "Leaking our WebWidget!";
Albert J. Wong7bbf22d2018-12-20 00:27:27484
Albert J. Wongcb004632018-07-10 22:58:25485 // TODO(ajwong): Add in check that routing_id_ has been removed from
486 // g_routing_id_widget_map once the shutdown semantics for RenderWidget
487 // and RenderViewImpl are rationalized. Currently, too many unit and
488 // browser tests delete a RenderWidget without correclty going through
489 // the shutdown. https://crbug.com/545684
initial.commit09911bf2008-07-26 23:55:29490}
491
[email protected]484955942010-08-19 16:13:18492// static
Albert J. Wongcb004632018-07-10 22:58:25493RenderWidget* RenderWidget::FromRoutingID(int32_t routing_id) {
Avi Drissman07d22452019-03-25 17:56:39494 DCHECK(RenderThread::IsMainThread());
Albert J. Wongcb004632018-07-10 22:58:25495 RoutingIDWidgetMap* widgets = g_routing_id_widget_map.Pointer();
jdoerrie5a73d0fa2018-10-02 23:54:05496 auto it = widgets->find(routing_id);
Albert J. Wongcb004632018-07-10 22:58:25497 return it == widgets->end() ? NULL : it->second;
498}
499
danakj25ec6e2b2018-09-26 17:01:31500void RenderWidget::InitForPopup(ShowCallback show_callback,
501 blink::WebPagePopup* web_page_popup) {
502 // Init() increments the reference count on |this|, making it
503 // self-referencing.
504 Init(std::move(show_callback), web_page_popup);
initial.commit09911bf2008-07-26 23:55:29505}
506
danakj6a816592018-09-25 18:30:56507void RenderWidget::InitForChildLocalRoot(
508 blink::WebFrameWidget* web_frame_widget) {
danakj6dcbc5962018-11-16 16:45:42509 for_child_local_root_frame_ = true;
danakj6a816592018-09-25 18:30:56510 // Init() increments the reference count on |this|, making it
511 // self-referencing.
512 Init(base::NullCallback(), web_frame_widget);
dchengda9b4bb2015-07-20 20:58:08513}
514
dchengda9b4bb2015-07-20 20:58:08515void RenderWidget::CloseForFrame() {
danakj8f0bd6c2019-03-14 15:35:24516 DCHECK(for_child_local_root_frame_);
dchengd96a27a2015-07-24 20:17:32517 OnClose();
kenrba7199832015-01-22 23:44:59518}
519
Scott Violetae08f332018-07-27 17:37:23520void RenderWidget::Init(ShowCallback show_callback, WebWidget* web_widget) {
lfg8ff33912016-09-13 20:59:21521 DCHECK(!webwidget_internal_);
nick4df698d82016-11-11 20:39:23522 DCHECK_NE(routing_id_, MSG_ROUTING_NONE);
initial.commit09911bf2008-07-26 23:55:29523
danakj17216f4d2018-10-16 22:57:32524 RenderThreadImpl* render_thread_impl = RenderThreadImpl::current();
525
Jeremy Roman04f27c372017-10-27 15:20:55526 input_handler_ = std::make_unique<RenderWidgetInputHandler>(this, this);
nick8331f8ad2016-11-15 20:42:45527
danakj17216f4d2018-10-16 22:57:32528 LayerTreeView* layer_tree_view = InitializeLayerTreeView();
danakje3bd12622019-02-13 21:54:16529 web_widget->SetLayerTreeView(layer_tree_view,
530 layer_tree_view->animation_host());
Dave Tapuska9db80842017-07-24 17:24:26531
danakj31699d5b2018-09-18 15:39:17532 blink::scheduler::WebThreadScheduler* main_thread_scheduler = nullptr;
533 if (render_thread_impl)
534 main_thread_scheduler = render_thread_impl->GetWebMainThreadScheduler();
Alexander Timin41e917d2018-07-02 16:25:35535 blink::scheduler::WebThreadScheduler* compositor_thread_scheduler =
536 blink::scheduler::WebThreadScheduler::CompositorThreadScheduler();
danakj31699d5b2018-09-18 15:39:17537 scoped_refptr<base::SingleThreadTaskRunner> compositor_input_task_runner;
danakj89ae4532018-12-12 22:52:55538 // Use the compositor thread task runner unless this is a popup or other such
539 // non-frame widgets. The |compositor_thread_scheduler| can be null in tests
540 // without a compositor thread.
541 if (for_frame() && compositor_thread_scheduler) {
542 compositor_input_task_runner =
543 compositor_thread_scheduler->InputTaskRunner();
danakj31699d5b2018-09-18 15:39:17544 }
Alexander Timin41e917d2018-07-02 16:25:35545
David Bokan3fc68482019-03-13 04:48:49546 // We only use an external input handler for frame RenderWidgets because only
547 // frames use the compositor for input handling. Other kinds of RenderWidgets
548 // (e.g. popups, plugins) must forward their input directly through
549 // RenderWidgetInputHandler into Blink.
550 bool uses_input_handler = for_frame();
Dave Tapuska04bc5ee92018-04-17 19:03:31551 widget_input_handler_manager_ = WidgetInputHandlerManager::Create(
danakj31699d5b2018-09-18 15:39:17552 weak_ptr_factory_.GetWeakPtr(), std::move(compositor_input_task_runner),
David Bokan3fc68482019-03-13 04:48:49553 main_thread_scheduler, uses_input_handler);
Dave Tapuska9db80842017-07-24 17:24:26554
danakjdf1ceb72018-07-18 20:02:25555 show_callback_ = std::move(show_callback);
initial.commit09911bf2008-07-26 23:55:29556
lfg8ff33912016-09-13 20:59:21557 webwidget_internal_ = web_widget;
558 webwidget_mouse_lock_target_.reset(
559 new WebWidgetLockTarget(webwidget_internal_));
lfgbee1e0a2016-06-08 21:24:21560 mouse_lock_dispatcher_.reset(new RenderWidgetMouseLockDispatcher(this));
initial.commit09911bf2008-07-26 23:55:29561
nick4df698d82016-11-11 20:39:23562 RenderThread::Get()->AddRoute(routing_id_, this);
563 // Take a reference on behalf of the RenderThread. This will be balanced
Albert J. Wong3c93c182018-09-27 17:29:43564 // when we receive WidgetMsg_Close.
nick4df698d82016-11-11 20:39:23565 AddRef();
initial.commit09911bf2008-07-26 23:55:29566}
567
danakj53802692018-07-25 21:46:44568void RenderWidget::ApplyEmulatedScreenMetricsForPopupWidget(
569 RenderWidget* origin_widget) {
570 RenderWidgetScreenMetricsEmulator* emulator =
571 origin_widget->screen_metrics_emulator_.get();
572 if (!emulator)
573 return;
[email protected]b2e4c70132013-10-03 02:07:51574 popup_origin_scale_for_emulation_ = emulator->scale();
[email protected]19193682014-04-03 15:01:43575 popup_view_origin_for_emulation_ = emulator->applied_widget_rect().origin();
Pavel Feldmancc099f72017-07-20 23:09:00576 popup_screen_origin_for_emulation_ =
Lei Zhang9b359d32017-11-28 00:57:02577 emulator->original_screen_rect().origin();
Fady Samuel1c1ad3692018-11-06 23:28:40578 UpdateSurfaceAndScreenInfo(local_surface_id_allocation_from_parent_,
danakj0d963bd2019-02-20 18:00:46579 CompositorViewportSize(),
Fady Samuel1c1ad3692018-11-06 23:28:40580 emulator->original_screen_info());
[email protected]b2e4c70132013-10-03 02:07:51581}
582
[email protected]2d6836f42014-07-02 17:25:31583gfx::Rect RenderWidget::AdjustValidationMessageAnchor(const gfx::Rect& anchor) {
584 if (screen_metrics_emulator_)
585 return screen_metrics_emulator_->AdjustValidationMessageAnchor(anchor);
586 return anchor;
587}
588
thakis18e426412017-03-15 12:06:37589#if BUILDFLAG(USE_EXTERNAL_POPUP_MENU)
[email protected]b2e4c70132013-10-03 02:07:51590void RenderWidget::SetExternalPopupOriginAdjustmentsForEmulation(
Albert J. Wong7bbf22d2018-12-20 00:27:27591 ExternalPopupMenu* popup) {
592 if (screen_metrics_emulator_)
593 popup->SetOriginScaleForEmulation(screen_metrics_emulator_->scale());
[email protected]b2e4c70132013-10-03 02:07:51594}
[email protected]53907862014-03-25 15:42:40595#endif
[email protected]b2e4c70132013-10-03 02:07:51596
597void RenderWidget::OnShowHostContextMenu(ContextMenuParams* params) {
598 if (screen_metrics_emulator_)
599 screen_metrics_emulator_->OnShowContextMenu(params);
600}
601
[email protected]a95986a82010-12-24 06:19:28602bool RenderWidget::OnMessageReceived(const IPC::Message& message) {
ekaramad330ba4232016-09-23 17:57:47603#if defined(OS_MACOSX)
604 if (IPC_MESSAGE_CLASS(message) == TextInputClientMsgStart)
605 return text_input_client_observer_->OnMessageReceived(message);
606#endif
lfgbee1e0a2016-06-08 21:24:21607 if (mouse_lock_dispatcher_ &&
608 mouse_lock_dispatcher_->OnMessageReceived(message))
609 return true;
610
[email protected]a95986a82010-12-24 06:19:28611 bool handled = true;
612 IPC_BEGIN_MESSAGE_MAP(RenderWidget, message)
Albert J. Wong3c93c182018-09-27 17:29:43613 IPC_MESSAGE_HANDLER(WidgetMsg_ShowContextMenu, OnShowContextMenu)
614 IPC_MESSAGE_HANDLER(WidgetMsg_Close, OnClose)
615 IPC_MESSAGE_HANDLER(WidgetMsg_SynchronizeVisualProperties,
Fady Samuel799e72192018-04-25 21:16:57616 OnSynchronizeVisualProperties)
Albert J. Wong3c93c182018-09-27 17:29:43617 IPC_MESSAGE_HANDLER(WidgetMsg_EnableDeviceEmulation,
dgozman9260b0a12015-03-16 13:45:20618 OnEnableDeviceEmulation)
Albert J. Wong3c93c182018-09-27 17:29:43619 IPC_MESSAGE_HANDLER(WidgetMsg_DisableDeviceEmulation,
dgozman9260b0a12015-03-16 13:45:20620 OnDisableDeviceEmulation)
Albert J. Wong3c93c182018-09-27 17:29:43621 IPC_MESSAGE_HANDLER(WidgetMsg_WasHidden, OnWasHidden)
622 IPC_MESSAGE_HANDLER(WidgetMsg_WasShown, OnWasShown)
623 IPC_MESSAGE_HANDLER(WidgetMsg_SetActive, OnSetActive)
Albert J. Wong3c93c182018-09-27 17:29:43624 IPC_MESSAGE_HANDLER(WidgetMsg_SetTextDirection, OnSetTextDirection)
625 IPC_MESSAGE_HANDLER(WidgetMsg_SetBounds_ACK, OnRequestSetBoundsAck)
626 IPC_MESSAGE_HANDLER(WidgetMsg_UpdateScreenRects, OnUpdateScreenRects)
627 IPC_MESSAGE_HANDLER(WidgetMsg_ForceRedraw, OnForceRedraw)
628 IPC_MESSAGE_HANDLER(WidgetMsg_SetViewportIntersection,
kenrbea731792017-01-13 15:10:48629 OnSetViewportIntersection)
Albert J. Wong3c93c182018-09-27 17:29:43630 IPC_MESSAGE_HANDLER(WidgetMsg_SetIsInert, OnSetIsInert)
631 IPC_MESSAGE_HANDLER(WidgetMsg_SetInheritedEffectiveTouchAction,
sunxd540a9962018-05-24 22:51:06632 OnSetInheritedEffectiveTouchAction)
Albert J. Wong3c93c182018-09-27 17:29:43633 IPC_MESSAGE_HANDLER(WidgetMsg_UpdateRenderThrottlingStatus,
Ken Buchanan8a319fb2017-11-15 18:37:12634 OnUpdateRenderThrottlingStatus)
Albert J. Wong3c93c182018-09-27 17:29:43635 IPC_MESSAGE_HANDLER(WidgetMsg_WaitForNextFrameForTests,
lfg43e08e62016-02-03 18:51:37636 OnWaitNextFrameForTests)
paulmeyer90f6c31d2016-11-12 00:17:59637 IPC_MESSAGE_HANDLER(DragMsg_TargetDragEnter, OnDragTargetDragEnter)
638 IPC_MESSAGE_HANDLER(DragMsg_TargetDragOver, OnDragTargetDragOver)
639 IPC_MESSAGE_HANDLER(DragMsg_TargetDragLeave, OnDragTargetDragLeave)
640 IPC_MESSAGE_HANDLER(DragMsg_TargetDrop, OnDragTargetDrop)
paulmeyer8fc8ea92016-11-15 05:12:21641 IPC_MESSAGE_HANDLER(DragMsg_SourceEnded, OnDragSourceEnded)
642 IPC_MESSAGE_HANDLER(DragMsg_SourceSystemDragEnded,
643 OnDragSourceSystemDragEnded)
[email protected]a95986a82010-12-24 06:19:28644 IPC_MESSAGE_UNHANDLED(handled = false)
645 IPC_END_MESSAGE_MAP()
646 return handled;
647}
initial.commit09911bf2008-07-26 23:55:29648
649bool RenderWidget::Send(IPC::Message* message) {
Nasko Oskovf97e8b02017-07-25 02:45:42650 // Don't send any messages after the browser has told us to close, and filter
danakj6dcbc5962018-11-16 16:45:42651 // most outgoing messages when frozen.
652 if (closing_) {
653 delete message;
654 return false;
655 }
656 if (is_frozen_ && !SwappedOutMessages::CanSendWhileSwappedOut(message)) {
initial.commit09911bf2008-07-26 23:55:29657 delete message;
658 return false;
659 }
660
661 // If given a messsage without a routing ID, then assign our routing ID.
662 if (message->routing_id() == MSG_ROUTING_NONE)
663 message->set_routing_id(routing_id_);
664
[email protected]380244092011-10-07 17:26:27665 return RenderThread::Get()->Send(message);
[email protected]8085dbc82008-09-26 22:53:44666}
667
dtapuska014ed082016-11-11 21:58:48668void RenderWidget::SendOrCrash(IPC::Message* message) {
669 bool result = Send(message);
670 CHECK(closing_ || result) << "Failed to send message";
671}
672
ekaramad5aff1942017-01-06 01:26:35673bool RenderWidget::ShouldHandleImeEvents() const {
Albert J. Wong7bbf22d2018-12-20 00:27:27674 if (delegate())
danakj89ae4532018-12-12 22:52:55675 return has_focus_;
676 if (for_child_local_root_frame_) {
677 // TODO(ekaramad): We track page focus in all RenderViews on the page but
678 // the RenderWidgets corresponding to child local roots do not get the
679 // update. For now, this method returns true when the RenderWidget is for a
680 // child local frame, i.e., IME events will be processed regardless of page
681 // focus. We should revisit this after page focus for OOPIFs has been fully
682 // resolved (https://crbug.com/689777).
683 return true;
684 }
685 // Not a frame widget.
686 return false;
ekaramad5aff1942017-01-06 01:26:35687}
688
initial.commit09911bf2008-07-26 23:55:29689void RenderWidget::OnClose() {
Avi Drissman07d22452019-03-25 17:56:39690 DCHECK(RenderThread::IsMainThread());
dchengd96a27a2015-07-24 20:17:32691 if (closing_)
692 return;
danakj292b63e2018-11-19 20:47:06693 for (auto& observer : render_frames_)
694 observer.WidgetWillClose();
dchengd96a27a2015-07-24 20:17:32695 closing_ = true;
696
697 // Browser correspondence is no longer needed at this point.
698 if (routing_id_ != MSG_ROUTING_NONE) {
699 RenderThread::Get()->RemoveRoute(routing_id_);
Albert J. Wongcb004632018-07-10 22:58:25700 g_routing_id_widget_map.Get().erase(routing_id_);
dchengd96a27a2015-07-24 20:17:32701 }
702
danakj327a4fe2019-01-26 00:00:35703 // Stop handling main thread input events immediately so we don't have them
704 // running while things are partly shut down.
705 if (input_event_queue_)
706 input_event_queue_->ClearClient();
707
danakj6dcbc5962018-11-16 16:45:42708 if (for_child_local_root_frame_) {
dchengd96a27a2015-07-24 20:17:32709 // Widgets for frames may be created and closed at any time while the frame
Ken Buchananed449bb2018-02-01 21:02:05710 // is alive. However, WebWidget must be closed synchronously because frame
711 // widgets and frames hold pointers to each other. The deferred call to
712 // Close() will complete cleanup and release |this|, but CloseWebWidget()
713 // prevents Close() from attempting to access members of an
714 // already-deleted frame.
715 CloseWebWidget();
dchengd96a27a2015-07-24 20:17:32716 }
Ken Buchananed449bb2018-02-01 21:02:05717 // If there is a Send call on the stack, then it could be dangerous to close
718 // now. Post a task that only gets invoked when there are no nested message
719 // loops.
Albert J. Wong2727e8a82019-02-15 16:56:11720 //
721 // The asynchronous Close() takes an owning reference to |this| keeping the
722 // object alive beyond the Release() below. It is the last reference to this
723 // object.
724 //
725 // TODO(https://crbug.com/545684): The actual lifetime for RenderWidget
726 // seems to be single-owner. It is either owned by "IPC" events (popup,
727 // mainframe, and fullscreen), or a RenderFrame. If Close() self-deleting,
728 // all the ref-counting mess could be removed.
Hajime Hoshi315a61f2018-08-14 17:27:28729 GetCleanupTaskRunner()->PostNonNestableTask(
730 FROM_HERE, base::BindOnce(&RenderWidget::Close, this));
dchengd96a27a2015-07-24 20:17:32731
732 // Balances the AddRef taken when we called AddRoute.
733 Release();
initial.commit09911bf2008-07-26 23:55:29734}
735
Fady Samuel799e72192018-04-25 21:16:57736void RenderWidget::OnSynchronizeVisualProperties(
danakja2c9d0a92018-07-25 20:01:18737 const VisualProperties& original_params) {
738 TRACE_EVENT0("renderer", "RenderWidget::OnSynchronizeVisualProperties");
EhsanKbd2cea992017-11-23 18:49:08739
danakja2c9d0a92018-07-25 20:01:18740 VisualProperties params = original_params;
danakj01474dd2019-01-28 10:46:04741 // Web tests can override the device scale factor in the renderer.
742 if (device_scale_factor_for_testing_) {
danakjb4cec102019-01-30 19:22:13743 params.screen_info.device_scale_factor = device_scale_factor_for_testing_;
danakj01474dd2019-01-28 10:46:04744 params.compositor_viewport_pixel_size = gfx::ScaleToCeiledSize(
745 params.new_size, params.screen_info.device_scale_factor);
746 }
danakj04d128a2019-01-25 02:10:20747
748 // Inform the rendering thread of the color space indicating the presence of
749 // HDR capabilities. The HDR bit happens to be globally true/false for all
750 // browser windows (on Windows OS) and thus would be the same for all
751 // RenderWidgets, so clobbering each other works out since only the HDR bit is
752 // used. See https://crbug.com/803451 and
753 // https://chromium-review.googlesource.com/c/chromium/src/+/852912/15#message-68bbd3e25c3b421a79cd028b2533629527d21fee
754 //
755 // The RenderThreadImpl can be null in tests.
756 {
757 RenderThreadImpl* render_thread = RenderThreadImpl::current();
758 if (render_thread)
759 render_thread->SetRenderingColorSpace(params.screen_info.color_space);
760 }
761
Albert J. Wong7bbf22d2018-12-20 00:27:27762 if (delegate()) {
danakj84bbf552019-01-21 21:33:31763 if (size_ != params.new_size) {
764 // Only hide popups when the size changes. Eg https://crbug.com/761908.
765 delegate()->CancelPagePopupForWidget();
766 }
danakja2c9d0a92018-07-25 20:01:18767
768 if (display_mode_ != params.display_mode) {
769 display_mode_ = params.display_mode;
Albert J. Wong7bbf22d2018-12-20 00:27:27770 delegate()->ApplyNewDisplayModeForWidget(params.display_mode);
danakja2c9d0a92018-07-25 20:01:18771 }
772
773 bool auto_resize_mode_changed =
774 auto_resize_mode_ != params.auto_resize_enabled;
775 auto_resize_mode_ = params.auto_resize_enabled;
776 min_size_for_auto_resize_ = params.min_size_for_auto_resize;
777 max_size_for_auto_resize_ = params.max_size_for_auto_resize;
778
779 if (auto_resize_mode_) {
780 gfx::Size min_auto_size = min_size_for_auto_resize_;
781 gfx::Size max_auto_size = max_size_for_auto_resize_;
782 if (compositor_deps_->IsUseZoomForDSFEnabled()) {
783 min_auto_size = gfx::ScaleToCeiledSize(
784 min_auto_size, params.screen_info.device_scale_factor);
785 max_auto_size = gfx::ScaleToCeiledSize(
786 max_auto_size, params.screen_info.device_scale_factor);
787 }
Albert J. Wong7bbf22d2018-12-20 00:27:27788 delegate()->ApplyAutoResizeLimitsForWidget(min_auto_size, max_auto_size);
danakja2c9d0a92018-07-25 20:01:18789 } else if (auto_resize_mode_changed) {
Albert J. Wong7bbf22d2018-12-20 00:27:27790 delegate()->DisableAutoResizeForWidget();
danakja2c9d0a92018-07-25 20:01:18791 if (params.new_size.IsEmpty())
792 return;
793 }
794
795 browser_controls_shrink_blink_size_ =
796 params.browser_controls_shrink_blink_size;
797 top_controls_height_ = params.top_controls_height;
798 bottom_controls_height_ = params.bottom_controls_height;
[email protected]b2e4c70132013-10-03 02:07:51799 }
800
danakj7602f4f2019-01-31 18:03:23801 bool ignore_resize_ipc = false;
802 if (synchronous_resize_mode_for_testing_) {
803 // We can ignore browser-initialized resizing during synchronous
804 // (renderer-controlled) mode, unless it is switching us to/from
805 // fullsreen mode or changing the device scale factor.
806 // TODO(danakj): Does the browser actually change DSF inside a web test??
807 // TODO(danakj): Isn't the display mode check redundant with the fullscreen
808 // one?
809 if (params.is_fullscreen_granted == is_fullscreen_granted_ &&
810 params.display_mode == display_mode_ &&
811 params.screen_info.device_scale_factor ==
812 screen_info_.device_scale_factor)
813 ignore_resize_ipc = true;
814 }
815
816 // When controlling the size in the renderer, we should ignore sizes given by
817 // the browser IPC here.
818 // TODO(danakj): There are many things also being ignored that aren't the
819 // widget's size params. It works because tests that use this mode don't
820 // change those parameters, I guess. But it's more complicated then because it
821 // looks like they are related to sync resize mode. Let's move them out of
822 // this block.
823 // TODO(danakj): It would be nice if we can still use the emulator to emulate
824 // things other than the size if we are in sync resize mode - if the emulator
825 // is even used in sync resize tests. It probably isn't though, so either way
826 // it'd be good to get the emulator out of this block (maybe by overwriting
827 // some of |params| in sync resize mode instead of just skipping the emulator.
828 if (!ignore_resize_ipc) {
danakja2c9d0a92018-07-25 20:01:18829 if (screen_metrics_emulator_) {
danakj7602f4f2019-01-31 18:03:23830 // This will call our SynchronizeVisualProperties() method with a
831 // different set of VisualProperties, holding emulated values. Though not
832 // all VisualProperties are modified by the metrics emulator, so it's a
833 // bit unclear to do this with the full structure. Anything it does not
834 // modify can be consumed directly here instead of in
835 // SynchronizeVisualProperties().
danakja2c9d0a92018-07-25 20:01:18836 screen_metrics_emulator_->OnSynchronizeVisualProperties(params);
837 } else {
danakj0246bba02019-01-23 23:12:27838 if (!delegate()) {
839 // The main frame controls the page scale factor, from blink. For other
840 // frame widgets, the page scale is received from its parent as part of
841 // the visual properties here. While blink doesn't need to know this
842 // page scale factor outside the main frame, the compositor does in
843 // order to produce its output at the correct scale.
W. James MacLean2cd99b62019-04-08 13:54:43844 layer_tree_view_->SetExternalPageScaleFactor(
845 params.page_scale_factor, params.is_pinch_gesture_active);
danakj0246bba02019-01-23 23:12:27846 // Store the value to give to any new RenderFrameProxy that is
847 // registered.
848 page_scale_factor_from_mainframe_ = params.page_scale_factor;
W. James MacLean2cd99b62019-04-08 13:54:43849 // Similarly, only the main frame knows when a pinch gesture is active,
850 // but this information is needed in subframes so they can throttle
851 // re-rastering in the same manner as the main frame.
852 // |is_pinch_gesture_active| follows the same path to the subframe
853 // compositor(s) as |page_scale_factor|.
854 is_pinch_gesture_active_from_mainframe_ =
855 params.is_pinch_gesture_active;
danakj0246bba02019-01-23 23:12:27856 // Push the page scale factor down to any child RenderWidgets via our
857 // child proxy frames.
858 // TODO(danakj): This ends up setting the page scale factor in the
859 // RenderWidgetHost of the child RenderWidget, so that it can bounce
860 // the value down to its RenderWidget. Since this is essentially a
861 // global value per-page, we could instead store it once in the browser
862 // (such as in RenderViewHost) and distribute it to each frame-hosted
863 // RenderWidget from there.
W. James MacLean2cd99b62019-04-08 13:54:43864 for (auto& child_proxy : render_frame_proxies_) {
865 child_proxy.OnPageScaleFactorChanged(params.page_scale_factor,
866 params.is_pinch_gesture_active);
867 }
danakj0246bba02019-01-23 23:12:27868 }
869
danakja2c9d0a92018-07-25 20:01:18870 gfx::Size old_visible_viewport_size = visible_viewport_size_;
871 SynchronizeVisualProperties(params);
872 if (old_visible_viewport_size != visible_viewport_size_) {
873 for (auto& render_frame : render_frames_)
Sushanth Rajasankarceaee202019-03-18 15:44:17874 render_frame.ResetHasScrolledFocusedEditableIntoView();
danakja2c9d0a92018-07-25 20:01:18875 }
876 }
Fady Samuel799e72192018-04-25 21:16:57877 }
878
Sushanth Rajasankarceaee202019-03-18 15:44:17879 // TODO(crbug.com/939118): ScrollFocusedNodeIntoViewForWidget does not work
880 // when the focused node is inside an OOPIF. This code path where
881 // scroll_focused_node_into_view is set is used only for WebView, crbug
882 // 939118 tracks fixing webviews to not use scroll_focused_node_into_view.
Albert J. Wong7bbf22d2018-12-20 00:27:27883 if (delegate() && params.scroll_focused_node_into_view)
884 delegate()->ScrollFocusedNodeIntoViewForWidget();
initial.commit09911bf2008-07-26 23:55:29885}
886
dgozman9260b0a12015-03-16 13:45:20887void RenderWidget::OnEnableDeviceEmulation(
Lei Zhang9b359d32017-11-28 00:57:02888 const blink::WebDeviceEmulationParams& params) {
mfomitchev2600fd7c2016-02-17 20:53:39889 if (!screen_metrics_emulator_) {
Fady Samuel4255c182018-05-24 20:42:36890 VisualProperties visual_properties;
891 visual_properties.screen_info = screen_info_;
892 visual_properties.new_size = size_;
danakj0d963bd2019-02-20 18:00:46893 visual_properties.compositor_viewport_pixel_size = CompositorViewportSize();
Fady Samuel84d8a1f72018-11-08 00:26:17894 visual_properties.local_surface_id_allocation =
895 local_surface_id_allocation_from_parent_;
Fady Samuel4255c182018-05-24 20:42:36896 visual_properties.visible_viewport_size = visible_viewport_size_;
897 visual_properties.is_fullscreen_granted = is_fullscreen_granted_;
898 visual_properties.display_mode = display_mode_;
mfomitchev2600fd7c2016-02-17 20:53:39899 screen_metrics_emulator_.reset(new RenderWidgetScreenMetricsEmulator(
Albert J. Wong3c93c182018-09-27 17:29:43900 this, params, visual_properties, widget_screen_rect_,
Fady Samuel4255c182018-05-24 20:42:36901 window_screen_rect_));
oshima50872a72016-03-04 13:26:18902 screen_metrics_emulator_->Apply();
mfomitchev2600fd7c2016-02-17 20:53:39903 } else {
dgozman9260b0a12015-03-16 13:45:20904 screen_metrics_emulator_->ChangeEmulationParams(params);
mfomitchev2600fd7c2016-02-17 20:53:39905 }
dgozman9260b0a12015-03-16 13:45:20906}
907
908void RenderWidget::OnDisableDeviceEmulation() {
909 screen_metrics_emulator_.reset();
910}
911
initial.commit09911bf2008-07-26 23:55:29912void RenderWidget::OnWasHidden() {
danakj14e00a8322019-02-07 17:12:06913 // A frozen main frame widget will never be hidden since that would require it
914 // to be shown first. It must be thawed before changing visibility.
915 DCHECK(!is_frozen_);
916
[email protected]9c3085f2011-06-09 02:10:31917 TRACE_EVENT0("renderer", "RenderWidget::OnWasHidden");
danakj14e00a8322019-02-07 17:12:06918
[email protected]bee16aab2009-08-26 15:55:03919 SetHidden(true);
danakj14e00a8322019-02-07 17:12:06920
Francois Doray687e70a2019-05-16 02:24:55921 tab_switch_time_recorder_.TabWasHidden();
922
ericwilligers88e69742016-10-17 19:29:55923 for (auto& observer : render_frames_)
924 observer.WasHidden();
initial.commit09911bf2008-07-26 23:55:29925}
926
Sebastien Marchand5f06ca32019-05-17 18:03:42927void RenderWidget::OnWasShown(
928 base::TimeTicks show_request_timestamp,
929 bool was_evicted,
930 const base::Optional<content::RecordTabSwitchTimeRequest>&
931 record_tab_switch_time_request) {
danakj14e00a8322019-02-07 17:12:06932 // A frozen main frame widget does not become shown, since it has no frame
933 // associated with it. It must be thawed before changing visibility.
934 DCHECK(!is_frozen_);
935
Edwin Joea4069f42019-02-20 15:06:24936 TRACE_EVENT_WITH_FLOW0("renderer", "RenderWidget::OnWasShown", routing_id(),
937 TRACE_EVENT_FLAG_FLOW_IN);
danakj678f0252018-11-09 21:46:34938
tasakb95dbb50c2017-02-08 18:07:50939 was_shown_time_ = base::TimeTicks::Now();
danakj14e00a8322019-02-07 17:12:06940
[email protected]bee16aab2009-08-26 15:55:03941 SetHidden(false);
Sebastien Marchand5f06ca32019-05-17 18:03:42942 if (record_tab_switch_time_request) {
danakja6c10012018-07-06 14:25:36943 layer_tree_view_->layer_tree_host()->RequestPresentationTimeForNextFrame(
Sebastien Marchand5f06ca32019-05-17 18:03:42944 tab_switch_time_recorder_.TabWasShown(
945 false /* has_saved_frames */,
946 record_tab_switch_time_request.value(), show_request_timestamp));
[email protected]3399dd822014-08-09 11:14:24947 }
danakj14e00a8322019-02-07 17:12:06948
949 for (auto& observer : render_frames_)
950 observer.WasShown();
951 if (was_evicted) {
952 for (auto& observer : render_frame_proxies_)
953 observer.WasEvicted();
954 }
initial.commit09911bf2008-07-26 23:55:29955}
956
Avi Drissman014dec72018-06-07 02:34:01957void RenderWidget::OnRequestSetBoundsAck() {
[email protected]53d3f302009-12-21 04:42:05958 DCHECK(pending_window_rect_count_);
959 pending_window_rect_count_--;
960}
961
Albert J. Wongcb004632018-07-10 22:58:25962void RenderWidget::OnForceRedraw(int snapshot_id) {
David Bokanf92eaf22019-03-01 01:54:51963 RequestPresentation(base::BindOnce(&RenderWidget::DidPresentForceDrawFrame,
964 weak_ptr_factory_.GetWeakPtr(),
965 snapshot_id));
966}
967
968void RenderWidget::RequestPresentation(PresentationTimeCallback callback) {
danakj125a29e2019-01-24 23:25:29969 layer_tree_view_->layer_tree_host()->RequestPresentationTimeForNextFrame(
David Bokanf92eaf22019-03-01 01:54:51970 std::move(callback));
danakjda15621e2019-04-05 20:34:43971 layer_tree_view_->layer_tree_host()->SetNeedsCommitWithForcedRedraw();
Albert J. Wongcb004632018-07-10 22:58:25972}
973
974void RenderWidget::DidPresentForceDrawFrame(
975 int snapshot_id,
976 const gfx::PresentationFeedback& feedback) {
Albert J. Wong3c93c182018-09-27 17:29:43977 Send(new WidgetHostMsg_ForceRedrawComplete(routing_id(), snapshot_id));
Albert J. Wongcb004632018-07-10 22:58:25978}
979
Xianzhu Wangf02017ac2018-10-17 01:47:33980viz::FrameSinkId RenderWidget::GetFrameSinkIdAtPoint(const gfx::PointF& point,
Ken Buchanan44d7e2f2018-08-23 14:18:05981 gfx::PointF* local_point) {
982 return input_handler_->GetFrameSinkIdAtPoint(point, local_point);
Navid Zolghadr0d86e5f2017-10-23 18:09:22983}
984
danakj77821e252019-03-27 19:58:04985bool RenderWidget::HasPendingPageScaleAnimation() const {
986 return layer_tree_view_->layer_tree_host()->HasPendingPageScaleAnimation();
987}
988
danakj327a4fe2019-01-26 00:00:35989bool RenderWidget::HandleInputEvent(
dtapuska9ec1a912017-04-21 15:18:31990 const blink::WebCoalescedInputEvent& input_event,
991 const ui::LatencyInfo& latency_info,
Dave Tapuskaea83d3bd2017-06-13 16:14:55992 HandledEventCallback callback) {
danakj327a4fe2019-01-26 00:00:35993 if (is_frozen_)
994 return false;
Dave Tapuskaea83d3bd2017-06-13 16:14:55995 input_handler_->HandleInputEvent(input_event, latency_info,
996 std::move(callback));
danakj327a4fe2019-01-26 00:00:35997 return true;
dtapuska9ec1a912017-04-21 15:18:31998}
999
danakj125a29e2019-01-24 23:25:291000void RenderWidget::SetNeedsMainFrame() {
danakj0719e0652019-03-14 21:19:111001 ScheduleAnimation();
danakj125a29e2019-01-24 23:25:291002}
1003
dtapuska9d46ef7d2017-05-26 19:06:061004scoped_refptr<MainThreadEventQueue> RenderWidget::GetInputEventQueue() {
1005 return input_event_queue_;
1006}
1007
fsamuel78f86e42016-01-20 04:10:231008void RenderWidget::OnCursorVisibilityChange(bool is_visible) {
lfg8ff33912016-09-13 20:59:211009 if (GetWebWidget())
Blink Reformat1c4d759e2017-04-09 16:34:541010 GetWebWidget()->SetCursorVisibilityState(is_visible);
fsamuel78f86e42016-01-20 04:10:231011}
1012
[email protected]76b082c2019-03-22 03:45:461013void RenderWidget::OnFallbackCursorModeToggled(bool is_on) {
1014 if (GetWebWidget())
1015 GetWebWidget()->OnFallbackCursorModeToggled(is_on);
1016}
1017
fsamuel78f86e42016-01-20 04:10:231018void RenderWidget::OnMouseCaptureLost() {
lfg8ff33912016-09-13 20:59:211019 if (GetWebWidget())
Blink Reformat1c4d759e2017-04-09 16:34:541020 GetWebWidget()->MouseCaptureLost();
fsamuel78f86e42016-01-20 04:10:231021}
1022
alexmos56567492016-09-13 00:52:461023void RenderWidget::OnSetEditCommandsForNextKeyEvent(
1024 const EditCommands& edit_commands) {
1025 edit_commands_ = edit_commands;
1026}
1027
Albert J. Wongcb004632018-07-10 22:58:251028void RenderWidget::OnSetActive(bool active) {
Albert J. Wong7bbf22d2018-12-20 00:27:271029 if (delegate())
1030 delegate()->SetActiveForWidget(active);
Albert J. Wongcb004632018-07-10 22:58:251031}
1032
fsamuel78f86e42016-01-20 04:10:231033void RenderWidget::OnSetFocus(bool enable) {
Albert J. Wong7bbf22d2018-12-20 00:27:271034 if (delegate())
1035 delegate()->DidReceiveSetFocusEventForWidget();
danakja2c9d0a92018-07-25 20:01:181036 SetFocus(enable);
1037}
1038
1039void RenderWidget::SetFocus(bool enable) {
lfge0c2792ec2016-05-11 18:52:081040 has_focus_ = enable;
1041
lfg8ff33912016-09-13 20:59:211042 if (GetWebWidget())
Blink Reformat1c4d759e2017-04-09 16:34:541043 GetWebWidget()->SetFocus(enable);
lfge0c2792ec2016-05-11 18:52:081044
ericwilligers88e69742016-10-17 19:29:551045 for (auto& observer : render_frames_)
1046 observer.RenderWidgetSetFocus(enable);
danakja2c9d0a92018-07-25 20:01:181047
Albert J. Wong7bbf22d2018-12-20 00:27:271048 if (delegate())
1049 delegate()->DidChangeFocusForWidget();
fsamuel78f86e42016-01-20 04:10:231050}
1051
1052///////////////////////////////////////////////////////////////////////////////
danakja6c10012018-07-06 14:25:361053// LayerTreeViewDelegate
fsamuel78f86e42016-01-20 04:10:231054
David Bokanc8e38d02018-10-08 21:56:011055void RenderWidget::ApplyViewportChanges(
1056 const cc::ApplyViewportChangesArgs& args) {
Ken Buchananed449bb2018-02-01 21:02:051057 if (!GetWebWidget())
1058 return;
David Bokanc8e38d02018-10-08 21:56:011059 GetWebWidget()->ApplyViewportChanges(args);
fsamuel78f86e42016-01-20 04:10:231060}
1061
sahel1b47fda72017-03-28 17:03:071062void RenderWidget::RecordWheelAndTouchScrollingCount(
1063 bool has_scrolled_by_wheel,
1064 bool has_scrolled_by_touch) {
Ken Buchananed449bb2018-02-01 21:02:051065 if (!GetWebWidget())
1066 return;
Blink Reformat1c4d759e2017-04-09 16:34:541067 GetWebWidget()->RecordWheelAndTouchScrollingCount(has_scrolled_by_wheel,
sahel1b47fda72017-03-28 17:03:071068 has_scrolled_by_touch);
1069}
1070
Sahel Sharify676580c2019-01-10 21:49:491071void RenderWidget::SendOverscrollEventFromImplSide(
1072 const gfx::Vector2dF& overscroll_delta,
1073 cc::ElementId scroll_latched_element_id) {
1074 if (!GetWebWidget())
1075 return;
1076 GetWebWidget()->SendOverscrollEventFromImplSide(overscroll_delta,
1077 scroll_latched_element_id);
1078}
1079void RenderWidget::SendScrollEndEventFromImplSide(
1080 cc::ElementId scroll_latched_element_id) {
1081 if (!GetWebWidget())
1082 return;
1083 GetWebWidget()->SendScrollEndEventFromImplSide(scroll_latched_element_id);
1084}
1085
Daniel Cheng224569ee2018-04-25 05:45:061086void RenderWidget::BeginMainFrame(base::TimeTicks frame_time) {
Ken Buchananed449bb2018-02-01 21:02:051087 if (!GetWebWidget())
1088 return;
dtapuska3d5624d32016-08-30 04:34:001089
Stephen Chenneyd56b5382019-01-29 00:37:021090 // We record metrics only when running in multi-threaded mode, not
1091 // single-thread mode for testing.
1092 bool record_main_frame_metrics =
1093 !!compositor_deps_->GetCompositorImplThreadTaskRunner();
1094 if (input_event_queue_) {
1095 base::Optional<ScopedUkmRafAlignedInputTimer> ukm_timer;
Stephen Chenney1d064cf2019-06-06 14:54:101096 if (record_main_frame_metrics) {
Stephen Chenneyd56b5382019-01-29 00:37:021097 ukm_timer.emplace(GetWebWidget());
Stephen Chenney1d064cf2019-06-06 14:54:101098 }
Stephen Chenneyd56b5382019-01-29 00:37:021099 input_event_queue_->DispatchRafAlignedInput(frame_time);
1100 }
1101
Stephen Chenney1d064cf2019-06-06 14:54:101102 // The input handler wants to know about the main frame for metric purposes
1103 DCHECK(widget_input_handler_manager_);
1104 widget_input_handler_manager_->MarkBeginMainFrame();
1105
Stephen Chenneyd56b5382019-01-29 00:37:021106 GetWebWidget()->BeginFrame(frame_time, record_main_frame_metrics);
fsamuel78f86e42016-01-20 04:10:231107}
1108
Stefan Zager148248c2019-02-20 23:24:081109void RenderWidget::DidBeginMainFrame() {
1110 if (!GetWebWidget())
1111 return;
1112 GetWebWidget()->DidBeginFrame();
1113}
1114
danakjc7afae52017-06-20 21:12:411115void RenderWidget::RequestNewLayerTreeFrameSink(
danakja6c10012018-07-06 14:25:361116 LayerTreeFrameSinkCallback callback) {
[email protected]7912e822014-04-16 02:37:031117 // For widgets that are never visible, we don't start the compositor, so we
danakjc7afae52017-06-20 21:12:411118 // never get a request for a cc::LayerTreeFrameSink.
sievers71c62dd52015-10-07 01:44:391119 DCHECK(!compositor_never_visible_);
danakjf87460792018-12-04 20:14:221120 // Frozen RenderWidgets should not be doing any compositing.
Scott Violetb04f16c2018-11-27 00:48:461121 DCHECK(!is_frozen_);
danakj678f0252018-11-09 21:46:341122
1123 if (is_closing()) {
1124 // In this case, we drop the request which means the compositor waits
1125 // forever, which is fine since we're going to destroy it.
1126 return;
1127 }
jonrossa2ff4f82018-02-16 17:27:461128
danakjf87460792018-12-04 20:14:221129 // If we have a warmup in progress, wait for that and store the callback
1130 // to be run when the warmup completes.
1131 if (warmup_frame_sink_request_pending_) {
1132 after_warmup_callback_ = std::move(callback);
1133 return;
1134 }
1135 // If a warmup previously completed, use the result.
1136 if (warmup_frame_sink_) {
1137 std::move(callback).Run(std::move(warmup_frame_sink_));
1138 return;
1139 }
1140
1141 DoRequestNewLayerTreeFrameSink(std::move(callback));
1142}
1143
1144void RenderWidget::DoRequestNewLayerTreeFrameSink(
1145 LayerTreeFrameSinkCallback callback) {
jonrossa2ff4f82018-02-16 17:27:461146 // TODO(jonross): have this generated by the LayerTreeFrameSink itself, which
1147 // would then handle binding.
1148 mojom::RenderFrameMetadataObserverPtr ptr;
1149 mojom::RenderFrameMetadataObserverRequest request = mojo::MakeRequest(&ptr);
1150 mojom::RenderFrameMetadataObserverClientPtrInfo client_info;
1151 mojom::RenderFrameMetadataObserverClientRequest client_request =
1152 mojo::MakeRequest(&client_info);
danakj4b347212018-07-04 17:55:171153 auto render_frame_metadata_observer =
1154 std::make_unique<RenderFrameMetadataObserverImpl>(std::move(request),
1155 std::move(client_info));
danakja6c10012018-07-06 14:25:361156 layer_tree_view_->SetRenderFrameObserver(
danakj4b347212018-07-04 17:55:171157 std::move(render_frame_metadata_observer));
danakjf51754372018-12-13 23:20:011158 GURL url = GetWebWidget()->GetURLForDebugTrace();
danakjefabf312018-10-05 19:03:071159 // The |url| is not always available, fallback to a fixed string.
1160 if (url.is_empty())
1161 url = GURL("chrome://gpu/RenderWidget::RequestNewLayerTreeFrameSink");
danakj6dcbc5962018-11-16 16:45:421162 // TODO(danakj): This may not be accurate, depending on the intent. A child
1163 // local root could be in the same process as the view, so if the client is
1164 // meant to designate the process type, it seems kRenderer would be the
1165 // correct choice. If client is meant to designate the widget type, then
1166 // kOOPIF would denote that it is not for the main frame. However, kRenderer
1167 // would also be used for other widgets such as popups.
1168 const char* client_name = for_child_local_root_frame_ ? kOOPIF : kRenderer;
danakj2d2889a2019-04-05 21:22:081169 compositor_deps_->RequestNewLayerTreeFrameSink(
Scott Violetb04f16c2018-11-27 00:48:461170 routing_id_, frame_swap_message_queue_, std::move(url),
1171 std::move(callback), std::move(client_request), std::move(ptr),
1172 client_name);
[email protected]ba91a792013-02-06 09:48:281173}
1174
fsamuel78f86e42016-01-20 04:10:231175void RenderWidget::DidCommitAndDrawCompositorFrame() {
1176 // NOTE: Tests may break if this event is renamed or moved. See
1177 // tab_capture_performancetest.cc.
1178 TRACE_EVENT0("gpu", "RenderWidget::DidCommitAndDrawCompositorFrame");
lfge0c2792ec2016-05-11 18:52:081179
Stephen Chenney1d064cf2019-06-06 14:54:101180 // The input handler wants to know about the commit for metric purposes
1181 DCHECK(widget_input_handler_manager_);
1182 widget_input_handler_manager_->MarkCompositorCommit();
1183
ericwilligers88e69742016-10-17 19:29:551184 for (auto& observer : render_frames_)
1185 observer.DidCommitAndDrawCompositorFrame();
lfge0c2792ec2016-05-11 18:52:081186
fsamuel78f86e42016-01-20 04:10:231187 // Notify subclasses that we initiated the paint operation.
1188 DidInitiatePaint();
Yiming Zhou0f8836c22018-09-13 22:22:111189
Albert J. Wongfb64e142018-10-16 01:10:451190 Send(new WidgetHostMsg_DidCommitAndDrawCompositorFrame(routing_id_));
fsamuel78f86e42016-01-20 04:10:231191}
1192
Stephen Chenney23880392019-04-02 18:36:541193void RenderWidget::WillCommitCompositorFrame() {
1194 if (GetWebWidget())
1195 GetWebWidget()->BeginCommitCompositorFrame();
1196}
1197
Saman Sami766e0b02018-01-31 17:19:201198void RenderWidget::DidCommitCompositorFrame() {
Albert J. Wong7bbf22d2018-12-20 00:27:271199 if (delegate())
1200 delegate()->DidCommitCompositorFrameForWidget();
Stephen Chenney23880392019-04-02 18:36:541201 if (GetWebWidget())
1202 GetWebWidget()->EndCommitCompositorFrame();
Saman Sami766e0b02018-01-31 17:19:201203}
fsamuel78f86e42016-01-20 04:10:231204
danakja2c9d0a92018-07-25 20:01:181205void RenderWidget::DidCompletePageScaleAnimation() {
Albert J. Wong7bbf22d2018-12-20 00:27:271206 if (delegate())
1207 delegate()->DidCompletePageScaleAnimationForWidget();
danakja2c9d0a92018-07-25 20:01:181208}
fsamuel78f86e42016-01-20 04:10:231209
danakjcd417502019-03-13 16:13:211210void RenderWidget::SetLayerTreeMutator(
1211 std::unique_ptr<cc::LayerTreeMutator> mutator) {
1212 layer_tree_view_->layer_tree_host()->SetLayerTreeMutator(std::move(mutator));
1213}
1214
Xida Chen83f1a0d12019-03-21 18:21:301215void RenderWidget::SetPaintWorkletLayerPainterClient(
1216 std::unique_ptr<cc::PaintWorkletLayerPainter> client) {
1217 layer_tree_view_->layer_tree_host()->SetPaintWorkletLayerPainter(
1218 std::move(client));
1219}
1220
danakj26b99912019-02-08 18:58:191221void RenderWidget::SetRootLayer(scoped_refptr<cc::Layer> layer) {
1222 layer_tree_view_->layer_tree_host()->SetRootLayer(std::move(layer));
1223}
1224
danakj53f46b82018-12-11 20:55:551225void RenderWidget::ScheduleAnimation() {
1226 // This call is not needed in single thread mode for tests without a
danakj0719e0652019-03-14 21:19:111227 // scheduler, but they override this method in order to schedule a synchronous
1228 // composite task themselves.
danakj53f46b82018-12-11 20:55:551229 layer_tree_view_->SetNeedsBeginFrame();
initial.commit09911bf2008-07-26 23:55:291230}
1231
danakjefcb8432019-01-16 18:47:141232void RenderWidget::SetShowFPSCounter(bool show) {
1233 cc::LayerTreeHost* host = layer_tree_view_->layer_tree_host();
1234 cc::LayerTreeDebugState debug_state = host->GetDebugState();
1235 debug_state.show_fps_counter = show;
1236 host->SetDebugState(debug_state);
1237}
1238
rnasricabcba42019-06-07 15:30:331239void RenderWidget::SetShowLayoutShiftRegions(bool show) {
1240 cc::LayerTreeHost* host = layer_tree_view_->layer_tree_host();
1241 cc::LayerTreeDebugState debug_state = host->GetDebugState();
1242 debug_state.show_layout_shift_regions = show;
1243 host->SetDebugState(debug_state);
1244}
1245
danakje691e852019-01-16 22:49:041246void RenderWidget::SetShowPaintRects(bool show) {
1247 cc::LayerTreeHost* host = layer_tree_view_->layer_tree_host();
1248 cc::LayerTreeDebugState debug_state = host->GetDebugState();
1249 debug_state.show_paint_rects = show;
1250 host->SetDebugState(debug_state);
1251}
1252
1253void RenderWidget::SetShowDebugBorders(bool show) {
1254 cc::LayerTreeHost* host = layer_tree_view_->layer_tree_host();
1255 cc::LayerTreeDebugState debug_state = host->GetDebugState();
1256 if (show)
1257 debug_state.show_debug_borders.set();
1258 else
1259 debug_state.show_debug_borders.reset();
1260 host->SetDebugState(debug_state);
1261}
1262
1263void RenderWidget::SetShowScrollBottleneckRects(bool show) {
1264 cc::LayerTreeHost* host = layer_tree_view_->layer_tree_host();
1265 cc::LayerTreeDebugState debug_state = host->GetDebugState();
1266 debug_state.show_touch_event_handler_rects = show;
1267 debug_state.show_wheel_event_handler_rects = show;
1268 debug_state.show_non_fast_scrollable_rects = show;
1269 host->SetDebugState(debug_state);
1270}
1271
1272void RenderWidget::SetShowHitTestBorders(bool show) {
1273 cc::LayerTreeHost* host = layer_tree_view_->layer_tree_host();
1274 cc::LayerTreeDebugState debug_state = host->GetDebugState();
1275 debug_state.show_hit_test_borders = show;
1276 host->SetDebugState(debug_state);
1277}
1278
danakj40d07932019-02-28 18:07:101279void RenderWidget::SetBackgroundColor(SkColor color) {
1280 layer_tree_view_->layer_tree_host()->set_background_color(color);
1281}
1282
Stephen Chenneyd56b5382019-01-29 00:37:021283void RenderWidget::UpdateVisualState() {
Ken Buchananed449bb2018-02-01 21:02:051284 if (!GetWebWidget())
1285 return;
1286
Stephen Chenneyd56b5382019-01-29 00:37:021287 // We record metrics only when running in multi-threaded mode, not
1288 // single-thread mode for testing.
1289 bool record_main_frame_metrics =
1290 !!compositor_deps_->GetCompositorImplThreadTaskRunner();
1291
Stephen Chenneyec4312612018-11-21 12:40:161292 // When recording main frame metrics set the lifecycle reason to
1293 // kBeginMainFrame, because this is the calller of UpdateLifecycle
1294 // for the main frame. Otherwise, set the reason to kTests, which is
1295 // the oinly other reason this method is called.
1296 WebWidget::LifecycleUpdateReason lifecycle_reason =
1297 record_main_frame_metrics
1298 ? WebWidget::LifecycleUpdateReason::kBeginMainFrame
1299 : WebWidget::LifecycleUpdateReason::kTest;
1300 GetWebWidget()->UpdateLifecycle(WebWidget::LifecycleUpdate::kAll,
1301 lifecycle_reason);
Blink Reformat1c4d759e2017-04-09 16:34:541302 GetWebWidget()->SetSuppressFrameRequestsWorkaroundFor704763Only(false);
tasakb95dbb50c2017-02-08 18:07:501303
Robert Flack3ef2ffb2018-09-06 20:38:201304 if (first_update_visual_state_after_hidden_) {
Takashi SAKAMOTO870f6262017-08-22 04:08:271305 RecordTimeToFirstActivePaint();
1306 first_update_visual_state_after_hidden_ = false;
1307 }
1308}
tasakb95dbb50c2017-02-08 18:07:501309
Takashi SAKAMOTO870f6262017-08-22 04:08:271310void RenderWidget::RecordTimeToFirstActivePaint() {
tasakb95dbb50c2017-02-08 18:07:501311 RenderThreadImpl* render_thread_impl = RenderThreadImpl::current();
tasakb95dbb50c2017-02-08 18:07:501312 base::TimeDelta sample = base::TimeTicks::Now() - was_shown_time_;
Takashi SAKAMOTO870f6262017-08-22 04:08:271313 if (render_thread_impl->NeedsToRecordFirstActivePaint(TTFAP_AFTER_PURGED)) {
1314 UMA_HISTOGRAM_TIMES("PurgeAndSuspend.Experimental.TimeToFirstActivePaint",
1315 sample);
1316 }
1317 if (render_thread_impl->NeedsToRecordFirstActivePaint(
1318 TTFAP_5MIN_AFTER_BACKGROUNDED)) {
1319 UMA_HISTOGRAM_TIMES(
1320 "PurgeAndSuspend.Experimental.TimeToFirstActivePaint."
1321 "AfterBackgrounded.5min",
1322 sample);
1323 }
fsamuel78f86e42016-01-20 04:10:231324}
1325
Stephen Chenneyd56b5382019-01-29 00:37:021326void RenderWidget::RecordStartOfFrameMetrics() {
Stephen Chenney23880392019-04-02 18:36:541327 if (GetWebWidget())
1328 GetWebWidget()->RecordStartOfFrameMetrics();
Stephen Chenneyd56b5382019-01-29 00:37:021329}
1330
Stephen Chenney9879a5c2018-09-26 14:21:421331void RenderWidget::RecordEndOfFrameMetrics(base::TimeTicks frame_begin_time) {
Stephen Chenney23880392019-04-02 18:36:541332 if (GetWebWidget())
1333 GetWebWidget()->RecordEndOfFrameMetrics(frame_begin_time);
1334}
Stephen Chenney9879a5c2018-09-26 14:21:421335
Stephen Chenney23880392019-04-02 18:36:541336void RenderWidget::BeginUpdateLayers() {
1337 if (GetWebWidget())
1338 GetWebWidget()->BeginUpdateLayers();
1339}
1340
1341void RenderWidget::EndUpdateLayers() {
1342 if (GetWebWidget())
Stephen Chenneyc3c5987d2019-04-15 20:40:071343 GetWebWidget()->EndUpdateLayers();
Stephen Chenney9879a5c2018-09-26 14:21:421344}
1345
fsamuel78f86e42016-01-20 04:10:231346void RenderWidget::WillBeginCompositorFrame() {
1347 TRACE_EVENT0("gpu", "RenderWidget::willBeginCompositorFrame");
1348
Ken Buchananed449bb2018-02-01 21:02:051349 if (!GetWebWidget())
1350 return;
1351
Blink Reformat1c4d759e2017-04-09 16:34:541352 GetWebWidget()->SetSuppressFrameRequestsWorkaroundFor704763Only(true);
alancutter317a8e02017-04-05 10:09:291353
dglazkov06854c5c2016-08-31 00:19:591354 // The UpdateTextInputState can result in further layout and possibly
1355 // enable GPU acceleration so they need to be called before any painting
1356 // is done.
changwan75e3b2072017-01-16 02:55:001357 UpdateTextInputState();
dglazkov06854c5c2016-08-31 00:19:591358 UpdateSelectionBounds();
1359
ericwilligers88e69742016-10-17 19:29:551360 for (auto& observer : render_frame_proxies_)
1361 observer.WillBeginCompositorFrame();
initial.commit09911bf2008-07-26 23:55:291362}
1363
fsamuel72464894f2015-12-15 06:59:311364///////////////////////////////////////////////////////////////////////////////
1365// RenderWidgetInputHandlerDelegate
1366
avid7d6b2e2016-03-04 19:41:171367void RenderWidget::FocusChangeComplete() {
Ehsan Karamad655d7b8a2018-01-12 18:38:381368 blink::WebFrameWidget* frame_widget = GetFrameWidget();
1369 if (!frame_widget)
ekaramad27ca69b12017-04-20 18:34:291370 return;
Ehsan Karamad655d7b8a2018-01-12 18:38:381371
ekaramad27ca69b12017-04-20 18:34:291372 blink::WebLocalFrame* focused =
Ehsan Karamad655d7b8a2018-01-12 18:38:381373 frame_widget->LocalRoot()->View()->FocusedFrame();
1374
ekaramad27ca69b12017-04-20 18:34:291375 if (focused && focused->AutofillClient())
1376 focused->AutofillClient()->DidCompleteFocusChangeInFrame();
avid7d6b2e2016-03-04 19:41:171377}
fsamuel72464894f2015-12-15 06:59:311378
dtapuska1827dd22016-03-11 15:24:591379void RenderWidget::ObserveGestureEventAndResult(
1380 const blink::WebGestureEvent& gesture_event,
1381 const gfx::Vector2dF& unused_delta,
Sandra Sunac5cdd832017-12-11 03:27:131382 const cc::OverscrollBehavior& overscroll_behavior,
dtapuska1827dd22016-03-11 15:24:591383 bool event_processed) {
1384 if (!compositor_deps_->IsElasticOverscrollEnabled())
1385 return;
1386
1387 cc::InputHandlerScrollResult scroll_result;
1388 scroll_result.did_scroll = event_processed;
1389 scroll_result.did_overscroll_root = !unused_delta.IsZero();
1390 scroll_result.unused_scroll_delta = unused_delta;
Sandra Sunac5cdd832017-12-11 03:27:131391 scroll_result.overscroll_behavior = overscroll_behavior;
dtapuska1827dd22016-03-11 15:24:591392
Dave Tapuska04bc5ee92018-04-17 19:03:311393 widget_input_handler_manager_->ObserveGestureEventOnMainThread(gesture_event,
1394 scroll_result);
dtapuska1827dd22016-03-11 15:24:591395}
1396
avid7d6b2e2016-03-04 19:41:171397void RenderWidget::OnDidHandleKeyEvent() {
alexmos56567492016-09-13 00:52:461398 ClearEditCommands();
1399}
1400
1401void RenderWidget::SetEditCommandForNextKeyEvent(const std::string& name,
1402 const std::string& value) {
1403 ClearEditCommands();
1404 edit_commands_.emplace_back(name, value);
1405}
1406
1407void RenderWidget::ClearEditCommands() {
1408 edit_commands_.clear();
avid7d6b2e2016-03-04 19:41:171409}
fsamuel72464894f2015-12-15 06:59:311410
chongza8ba91fc2016-08-16 21:39:171411void RenderWidget::OnDidOverscroll(const ui::DidOverscrollParams& params) {
Dave Tapuska04bc5ee92018-04-17 19:03:311412 if (mojom::WidgetInputHandlerHost* host =
1413 widget_input_handler_manager_->GetWidgetInputHandlerHost()) {
1414 host->DidOverscroll(params);
Dave Tapuska139a72f2017-09-06 21:57:031415 }
fsamuel72464894f2015-12-15 06:59:311416}
1417
fsamuele8326c742016-01-12 00:49:391418void RenderWidget::SetInputHandler(RenderWidgetInputHandler* input_handler) {
1419 // Nothing to do here. RenderWidget created the |input_handler| and will take
1420 // ownership of it. We just verify here that we don't already have an input
1421 // handler.
1422 DCHECK(!input_handler_);
1423}
1424
changwan75e3b2072017-01-16 02:55:001425void RenderWidget::ShowVirtualKeyboard() {
danakj125a29e2019-01-24 23:25:291426 // Blink can continue running and change input state between the Close IPC
1427 // and the task that actually closes this class.
1428 if (closing_)
1429 return;
changwan75e3b2072017-01-16 02:55:001430 UpdateTextInputStateInternal(true, false);
1431}
1432
changwan62f57292017-02-17 08:28:251433void RenderWidget::ClearTextInputState() {
1434 text_input_info_ = blink::WebTextInputInfo();
1435 text_input_type_ = ui::TextInputType::TEXT_INPUT_TYPE_NONE;
1436 text_input_mode_ = ui::TextInputMode::TEXT_INPUT_MODE_DEFAULT;
1437 can_compose_inline_ = false;
1438 text_input_flags_ = 0;
AJITH KUMAR V041c0b02017-08-08 10:39:201439 next_previous_flags_ = kInvalidNextPreviousFlagsValue;
changwan62f57292017-02-17 08:28:251440}
1441
changwan75e3b2072017-01-16 02:55:001442void RenderWidget::UpdateTextInputState() {
danakj125a29e2019-01-24 23:25:291443 // Blink can continue running and change input state between the Close IPC
1444 // and the task that actually closes this class.
1445 if (closing_)
1446 return;
changwan75e3b2072017-01-16 02:55:001447 UpdateTextInputStateInternal(false, false);
1448}
1449
1450void RenderWidget::UpdateTextInputStateInternal(bool show_virtual_keyboard,
1451 bool reply_to_request) {
fsamuel72464894f2015-12-15 06:59:311452 TRACE_EVENT0("renderer", "RenderWidget::UpdateTextInputState");
changwan75e3b2072017-01-16 02:55:001453
fsamuel72464894f2015-12-15 06:59:311454 if (ime_event_guard_) {
changwan75e3b2072017-01-16 02:55:001455 DCHECK(!reply_to_request);
1456 // show_virtual_keyboard should still be effective even if it was set inside
1457 // the IME
fsamuel72464894f2015-12-15 06:59:311458 // event guard.
changwan75e3b2072017-01-16 02:55:001459 if (show_virtual_keyboard)
1460 ime_event_guard_->set_show_virtual_keyboard(true);
fsamuel72464894f2015-12-15 06:59:311461 return;
1462 }
1463
dglazkov97b6c2b2016-10-25 17:35:551464 ui::TextInputType new_type = GetTextInputType();
1465 if (IsDateTimeInput(new_type))
1466 return; // Not considered as a text input field in WebKit/Chromium.
fsamuel72464894f2015-12-15 06:59:311467
dglazkov97b6c2b2016-10-25 17:35:551468 blink::WebTextInputInfo new_info;
ekaramad5aff1942017-01-06 01:26:351469 if (auto* controller = GetInputMethodController())
Blink Reformat1c4d759e2017-04-09 16:34:541470 new_info = controller->TextInputInfo();
dtapuskac4dd5be2016-10-25 15:11:101471 const ui::TextInputMode new_mode =
Blink Reformat1c4d759e2017-04-09 16:34:541472 ConvertWebTextInputMode(new_info.input_mode);
fsamuel72464894f2015-12-15 06:59:311473
1474 bool new_can_compose_inline = CanComposeInline();
1475
1476 // Only sends text input params if they are changed or if the ime should be
1477 // shown.
changwan75e3b2072017-01-16 02:55:001478 if (show_virtual_keyboard || reply_to_request ||
changwan38c3eb612016-12-09 01:45:561479 text_input_type_ != new_type || text_input_mode_ != new_mode ||
1480 text_input_info_ != new_info ||
1481 can_compose_inline_ != new_can_compose_inline) {
ekaramad9053e57b2016-04-26 20:00:381482 TextInputState params;
dglazkov97b6c2b2016-10-25 17:35:551483 params.type = new_type;
fsamuel72464894f2015-12-15 06:59:311484 params.mode = new_mode;
Dave Tapuskaf0698ee2019-05-21 21:12:331485 params.action = new_info.action;
fsamuel72464894f2015-12-15 06:59:311486 params.flags = new_info.flags;
AJITH KUMAR V9e7b5572017-11-14 00:54:211487#if defined(OS_ANDROID)
AJITH KUMAR V041c0b02017-08-08 10:39:201488 if (next_previous_flags_ == kInvalidNextPreviousFlagsValue) {
1489 // Due to a focus change, values will be reset by the frame.
1490 // That case we only need fresh NEXT/PREVIOUS information.
Albert J. Wong3c93c182018-09-27 17:29:431491 // Also we won't send WidgetHostMsg_TextInputStateChanged if next/previous
AJITH KUMAR V041c0b02017-08-08 10:39:201492 // focusable status is changed.
1493 if (auto* controller = GetInputMethodController()) {
1494 next_previous_flags_ =
1495 controller->ComputeWebTextInputNextPreviousFlags();
1496 } else {
1497 // For safety in case GetInputMethodController() is null, because -1 is
1498 // invalid value to send to browser process.
1499 next_previous_flags_ = 0;
1500 }
1501 }
AJITH KUMAR V9e7b5572017-11-14 00:54:211502#else
1503 next_previous_flags_ = 0;
1504#endif
AJITH KUMAR V041c0b02017-08-08 10:39:201505 params.flags |= next_previous_flags_;
Yuichiro Hanada1b19cf852019-01-18 16:12:261506 params.value = new_info.value.Utf16();
Blink Reformat1c4d759e2017-04-09 16:34:541507 params.selection_start = new_info.selection_start;
1508 params.selection_end = new_info.selection_end;
1509 params.composition_start = new_info.composition_start;
1510 params.composition_end = new_info.composition_end;
fsamuel72464894f2015-12-15 06:59:311511 params.can_compose_inline = new_can_compose_inline;
changwan75e3b2072017-01-16 02:55:001512 // TODO(changwan): change instances of show_ime_if_needed to
1513 // show_virtual_keyboard.
1514 params.show_ime_if_needed = show_virtual_keyboard;
1515 params.reply_to_request = reply_to_request;
Albert J. Wong3c93c182018-09-27 17:29:431516 Send(new WidgetHostMsg_TextInputStateChanged(routing_id(), params));
fsamuel72464894f2015-12-15 06:59:311517
1518 text_input_info_ = new_info;
dglazkov97b6c2b2016-10-25 17:35:551519 text_input_type_ = new_type;
fsamuel72464894f2015-12-15 06:59:311520 text_input_mode_ = new_mode;
1521 can_compose_inline_ = new_can_compose_inline;
1522 text_input_flags_ = new_info.flags;
Eric Karl706ea6712018-12-21 22:41:381523
1524#if defined(OS_ANDROID)
1525 // If we send a new TextInputStateChanged message, we must also deliver a
1526 // new RenderFrameMetadata, as the IME will need this info to be updated.
1527 // TODO(ericrk): Consider folding the above IPC into RenderFrameMetadata.
1528 // https://crbug.com/912309
danakj125a29e2019-01-24 23:25:291529 if (IsSurfaceSynchronizationEnabled()) {
Eric Karl706ea6712018-12-21 22:41:381530 layer_tree_view_->RequestForceSendMetadata();
1531 }
1532#endif
fsamuel72464894f2015-12-15 06:59:311533 }
1534}
1535
1536bool RenderWidget::WillHandleGestureEvent(const blink::WebGestureEvent& event) {
paulmeyer6ef5a792016-11-08 20:33:581537 possible_drag_event_info_.event_source =
1538 ui::DragDropTypes::DRAG_EVENT_SOURCE_TOUCH;
1539 possible_drag_event_info_.event_location =
Ella Ge1116059d2018-03-21 02:06:131540 gfx::ToFlooredPoint(event.PositionInScreen());
avid7d6b2e2016-03-04 19:41:171541
fsamuel72464894f2015-12-15 06:59:311542 return false;
1543}
1544
1545bool RenderWidget::WillHandleMouseEvent(const blink::WebMouseEvent& event) {
ericwilligers88e69742016-10-17 19:29:551546 for (auto& observer : render_frames_)
1547 observer.RenderWidgetWillHandleMouseEvent();
lfge0c2792ec2016-05-11 18:52:081548
paulmeyer6ef5a792016-11-08 20:33:581549 possible_drag_event_info_.event_source =
1550 ui::DragDropTypes::DRAG_EVENT_SOURCE_MOUSE;
1551 possible_drag_event_info_.event_location =
Blink Reformat1c4d759e2017-04-09 16:34:541552 gfx::Point(event.PositionInScreen().x, event.PositionInScreen().y);
paulmeyer6ef5a792016-11-08 20:33:581553
Albert J. Wong7bbf22d2018-12-20 00:27:271554 if (delegate())
1555 return delegate()->RenderWidgetWillHandleMouseEventForWidget(event);
avid7d6b2e2016-03-04 19:41:171556
fsamuel72464894f2015-12-15 06:59:311557 return false;
[email protected]fd847792013-10-24 17:12:351558}
1559
initial.commit09911bf2008-07-26 23:55:291560///////////////////////////////////////////////////////////////////////////////
mfomitchev2600fd7c2016-02-17 20:53:391561// RenderWidgetScreenMetricsDelegate
1562
bokanc63441c2016-04-27 15:49:121563void RenderWidget::ResizeWebWidget() {
danakja2c9d0a92018-07-25 20:01:181564 gfx::Size size = GetSizeForWebWidget();
Albert J. Wong7bbf22d2018-12-20 00:27:271565 if (delegate()) {
1566 delegate()->ResizeWebWidgetForWidget(size, top_controls_height_,
1567 bottom_controls_height_,
1568 browser_controls_shrink_blink_size_);
danakja2c9d0a92018-07-25 20:01:181569 return;
1570 }
1571 GetWebWidget()->Resize(size);
bokanc63441c2016-04-27 15:49:121572}
1573
1574gfx::Size RenderWidget::GetSizeForWebWidget() const {
danakj4b347212018-07-04 17:55:171575 if (compositor_deps_->IsUseZoomForDSFEnabled()) {
Fady Samuela863f152018-03-09 16:10:031576 return gfx::ScaleToCeiledSize(size_,
1577 GetOriginalScreenInfo().device_scale_factor);
1578 }
bokanc63441c2016-04-27 15:49:121579
1580 return size_;
1581}
1582
danakj0d963bd2019-02-20 18:00:461583gfx::Size RenderWidget::CompositorViewportSize() const {
1584 return layer_tree_view_->layer_tree_host()->device_viewport_size();
1585}
1586
akaba3483d8f2018-07-10 21:43:091587void RenderWidget::UpdateZoom(double zoom_level) {
akabac6bd1212018-06-25 20:10:481588 blink::WebFrameWidget* frame_widget = GetFrameWidget();
1589 if (!frame_widget)
1590 return;
1591 RenderFrameImpl* render_frame =
1592 RenderFrameImpl::FromWebFrame(frame_widget->LocalRoot());
1593
akaba3483d8f2018-07-10 21:43:091594 // Return early if zoom level is unchanged.
1595 if (render_frame->GetZoomLevel() == zoom_level) {
akabac6bd1212018-06-25 20:10:481596 return;
1597 }
1598
akaba3483d8f2018-07-10 21:43:091599 render_frame->SetZoomLevel(zoom_level);
akabac6bd1212018-06-25 20:10:481600
1601 for (auto& observer : render_frame_proxies_)
akaba3483d8f2018-07-10 21:43:091602 observer.OnZoomLevelChanged(zoom_level);
akabac6bd1212018-06-25 20:10:481603
1604 for (auto& plugin : browser_plugins_)
akaba3483d8f2018-07-10 21:43:091605 plugin.OnZoomLevelChanged(zoom_level);
akabac6bd1212018-06-25 20:10:481606}
1607
Fady Samuel799e72192018-04-25 21:16:571608void RenderWidget::SynchronizeVisualProperties(const VisualProperties& params) {
Miguel Casas5e1018052018-01-09 19:17:361609
Fady Samuele62be402018-03-29 03:19:531610 gfx::Size new_compositor_viewport_pixel_size =
1611 params.auto_resize_enabled
1612 ? gfx::ScaleToCeiledSize(size_,
1613 params.screen_info.device_scale_factor)
1614 : params.compositor_viewport_pixel_size;
Fady Samuel84d8a1f72018-11-08 00:26:171615 UpdateSurfaceAndScreenInfo(params.local_surface_id_allocation.value_or(
1616 viz::LocalSurfaceIdAllocation()),
1617 new_compositor_viewport_pixel_size,
1618 params.screen_info);
Vladimir Levin98d76dad2018-04-21 00:21:291619 UpdateCaptureSequenceNumber(params.capture_sequence_number);
danakj125a29e2019-01-24 23:25:291620 layer_tree_view_->SetBrowserControlsHeight(
1621 params.top_controls_height, params.bottom_controls_height,
1622 params.browser_controls_shrink_blink_size);
mfomitchev2600fd7c2016-02-17 20:53:391623
akaba3483d8f2018-07-10 21:43:091624 UpdateZoom(params.zoom_level);
akabac6bd1212018-06-25 20:10:481625
danakj21604ce2018-12-19 16:01:221626 if (!params.auto_resize_enabled) {
1627 visible_viewport_size_ = params.visible_viewport_size;
danakj21604ce2018-12-19 16:01:221628 display_mode_ = params.display_mode;
danakj21604ce2018-12-19 16:01:221629 size_ = params.new_size;
mfomitchev2600fd7c2016-02-17 20:53:391630
danakj21604ce2018-12-19 16:01:221631 ResizeWebWidget();
mfomitchev2600fd7c2016-02-17 20:53:391632
danakjb4cec102019-01-30 19:22:131633 gfx::Size visual_viewport_size = visible_viewport_size_;
danakj21604ce2018-12-19 16:01:221634 if (compositor_deps_->IsUseZoomForDSFEnabled()) {
danakjb4cec102019-01-30 19:22:131635 visual_viewport_size = gfx::ScaleToCeiledSize(
1636 visual_viewport_size, GetOriginalScreenInfo().device_scale_factor);
danakj21604ce2018-12-19 16:01:221637 }
1638 GetWebWidget()->ResizeVisualViewport(visual_viewport_size);
bokan71cb5b12016-04-27 03:45:221639
danakjf9284772019-01-23 18:29:141640 // NOTE: We may have entered fullscreen mode without changing our size.
1641 SetIsFullscreen(params.is_fullscreen_granted);
mfomitchev2600fd7c2016-02-17 20:53:391642 }
mfomitchev2600fd7c2016-02-17 20:53:391643}
1644
1645void RenderWidget::SetScreenMetricsEmulationParameters(
1646 bool enabled,
1647 const blink::WebDeviceEmulationParams& params) {
Albert J. Wong7bbf22d2018-12-20 00:27:271648 // This is only supported in RenderView, which has an delegate().
1649 DCHECK(delegate());
1650 delegate()->SetScreenMetricsEmulationParametersForWidget(enabled, params);
mfomitchev2600fd7c2016-02-17 20:53:391651}
1652
Albert J. Wong3c93c182018-09-27 17:29:431653void RenderWidget::SetScreenRects(const gfx::Rect& widget_screen_rect,
mfomitchev2600fd7c2016-02-17 20:53:391654 const gfx::Rect& window_screen_rect) {
Albert J. Wong3c93c182018-09-27 17:29:431655 widget_screen_rect_ = widget_screen_rect;
mfomitchev2600fd7c2016-02-17 20:53:391656 window_screen_rect_ = window_screen_rect;
1657}
1658
1659///////////////////////////////////////////////////////////////////////////////
[email protected]f98d7e3c2010-09-13 22:30:461660// WebWidgetClient
initial.commit09911bf2008-07-26 23:55:291661
Chris Harrelsond7ab99b2018-01-24 17:51:361662void RenderWidget::IntrinsicSizingInfoChanged(
1663 const blink::WebIntrinsicSizingInfo& sizing_info) {
Albert J. Wong3c93c182018-09-27 17:29:431664 Send(new WidgetHostMsg_IntrinsicSizingInfoChanged(routing_id_, sizing_info));
Chris Harrelsond7ab99b2018-01-24 17:51:361665}
1666
Blink Reformat1c4d759e2017-04-09 16:34:541667void RenderWidget::DidMeaningfulLayout(blink::WebMeaningfulLayout layout_type) {
1668 if (layout_type == blink::WebMeaningfulLayout::kVisuallyNonEmpty) {
Albert J. Wongfb64e142018-10-16 01:10:451669 QueueMessage(new WidgetHostMsg_DidFirstVisuallyNonEmptyPaint(routing_id_));
dglazkovf0e1d6d2015-10-10 02:13:481670 }
dglazkov79c426102015-08-31 21:22:431671
ericwilligers88e69742016-10-17 19:29:551672 for (auto& observer : render_frames_)
1673 observer.DidMeaningfulLayout(layout_type);
dglazkov79c426102015-08-31 21:22:431674}
1675
[email protected]586871b2014-07-22 17:05:111676// static
dchengcedca5612016-04-09 01:40:151677std::unique_ptr<cc::SwapPromise> RenderWidget::QueueMessageImpl(
[email protected]586871b2014-07-22 17:05:111678 IPC::Message* msg,
[email protected]586871b2014-07-22 17:05:111679 FrameSwapMessageQueue* frame_swap_message_queue,
1680 scoped_refptr<IPC::SyncMessageFilter> sync_message_filter,
[email protected]586871b2014-07-22 17:05:111681 int source_frame_number) {
[email protected]586871b2014-07-22 17:05:111682 bool first_message_for_frame = false;
Sadrul Habib Chowdhury4e0335cf2018-07-31 20:08:491683 frame_swap_message_queue->QueueMessageForFrame(
1684 source_frame_number, base::WrapUnique(msg), &first_message_for_frame);
danakj2d2889a2019-04-05 21:22:081685 if (!first_message_for_frame)
1686 return nullptr;
1687 return std::make_unique<QueueMessageSwapPromise>(
1688 sync_message_filter, frame_swap_message_queue, source_frame_number);
[email protected]586871b2014-07-22 17:05:111689}
1690
danakj53802692018-07-25 21:46:441691void RenderWidget::SetHandlingInputEvent(bool handling_input_event) {
1692 input_handler_->set_handling_input_event(handling_input_event);
1693}
1694
Sadrul Habib Chowdhury4e0335cf2018-07-31 20:08:491695void RenderWidget::QueueMessage(IPC::Message* msg) {
danakj125a29e2019-01-24 23:25:291696 if (closing_)
1697 return;
1698
[email protected]586871b2014-07-22 17:05:111699 // RenderThreadImpl::current() is NULL in some tests.
danakj125a29e2019-01-24 23:25:291700 if (!RenderThreadImpl::current()) {
[email protected]586871b2014-07-22 17:05:111701 Send(msg);
1702 return;
1703 }
1704
dchengcedca5612016-04-09 01:40:151705 std::unique_ptr<cc::SwapPromise> swap_promise =
Sadrul Habib Chowdhury4e0335cf2018-07-31 20:08:491706 QueueMessageImpl(msg, frame_swap_message_queue_.get(),
[email protected]586871b2014-07-22 17:05:111707 RenderThreadImpl::current()->sync_message_filter(),
danakja6c10012018-07-06 14:25:361708 layer_tree_view_->GetSourceFrameNumber());
danakj2d2889a2019-04-05 21:22:081709 if (swap_promise) {
1710 layer_tree_view_->layer_tree_host()->QueueSwapPromise(
1711 std::move(swap_promise));
1712 }
[email protected]586871b2014-07-22 17:05:111713}
1714
Blink Reformat1c4d759e2017-04-09 16:34:541715void RenderWidget::DidChangeCursor(const WebCursorInfo& cursor_info) {
[email protected]7c51b0ee2009-07-08 21:49:301716 // TODO(darin): Eliminate this temporary.
Mike Wassermanf95c2d9d2019-04-17 00:44:211717 WebCursor cursor((CursorInfo(cursor_info)));
initial.commit09911bf2008-07-26 23:55:291718 // Only send a SetCursor message if we need to make a change.
Navid Zolghadr293bb862018-12-12 18:15:411719 if (input_handler_->DidChangeCursor(cursor))
Albert J. Wong3c93c182018-09-27 17:29:431720 Send(new WidgetHostMsg_SetCursor(routing_id_, cursor));
initial.commit09911bf2008-07-26 23:55:291721}
1722
aelias5971e47d2017-06-16 02:39:381723void RenderWidget::AutoscrollStart(const blink::WebFloatPoint& point) {
Albert J. Wong3c93c182018-09-27 17:29:431724 Send(new WidgetHostMsg_AutoscrollStart(routing_id_, point));
aelias5971e47d2017-06-16 02:39:381725}
1726
1727void RenderWidget::AutoscrollFling(const blink::WebFloatSize& velocity) {
Albert J. Wong3c93c182018-09-27 17:29:431728 Send(new WidgetHostMsg_AutoscrollFling(routing_id_, velocity));
aelias5971e47d2017-06-16 02:39:381729}
1730
1731void RenderWidget::AutoscrollEnd() {
Albert J. Wong3c93c182018-09-27 17:29:431732 Send(new WidgetHostMsg_AutoscrollEnd(routing_id_));
aelias5971e47d2017-06-16 02:39:381733}
1734
initial.commit09911bf2008-07-26 23:55:291735// We are supposed to get a single call to Show for a newly created RenderWidget
1736// that was created via RenderWidget::CreateWebView. So, we wait until this
1737// point to dispatch the ShowWidget message.
1738//
1739// This method provides us with the information about how to display the newly
[email protected]5f9de5882011-09-30 23:36:281740// created RenderWidget (i.e., as a blocked popup or as a new tab).
initial.commit09911bf2008-07-26 23:55:291741//
Blink Reformat1c4d759e2017-04-09 16:34:541742void RenderWidget::Show(WebNavigationPolicy policy) {
danakj65f423b72018-10-10 17:21:201743 if (!show_callback_) {
Albert J. Wong7bbf22d2018-12-20 00:27:271744 if (delegate()) {
danakja2c9d0a92018-07-25 20:01:181745 // When SupportsMultipleWindows is disabled, popups are reusing
danakj65f423b72018-10-10 17:21:201746 // the view's RenderWidget. In some scenarios, this makes blink to call
1747 // Show() twice. But otherwise, if it is enabled, we should not visit
1748 // Show() more than once.
Albert J. Wong7bbf22d2018-12-20 00:27:271749 DCHECK(!delegate()->SupportsMultipleWindowsForWidget());
danakj65f423b72018-10-10 17:21:201750 return;
danakja2c9d0a92018-07-25 20:01:181751 } else {
danakj65f423b72018-10-10 17:21:201752 NOTREACHED() << "received extraneous Show call";
danakja2c9d0a92018-07-25 20:01:181753 }
danakja2c9d0a92018-07-25 20:01:181754 }
1755
initial.commit09911bf2008-07-26 23:55:291756 DCHECK(routing_id_ != MSG_ROUTING_NONE);
nickf7b38222016-11-22 21:59:351757
1758 // The opener is responsible for actually showing this widget.
danakjdf1ceb72018-07-18 20:02:251759 std::move(show_callback_).Run(this, policy, initial_rect_);
nickf7b38222016-11-22 21:59:351760
bokanc007c3a2015-02-03 07:15:561761 // NOTE: initial_rect_ may still have its default values at this point, but
[email protected]8de12d942010-11-17 20:42:441762 // that's okay. It'll be ignored if as_popup is false, or the browser
1763 // process will impose a default position otherwise.
bokanc007c3a2015-02-03 07:15:561764 SetPendingWindowRect(initial_rect_);
initial.commit09911bf2008-07-26 23:55:291765}
1766
danakj17216f4d2018-10-16 22:57:321767LayerTreeView* RenderWidget::InitializeLayerTreeView() {
1768 TRACE_EVENT0("blink", "RenderWidget::InitializeLayerTreeView");
danakj17216f4d2018-10-16 22:57:321769
1770 layer_tree_view_ = std::make_unique<LayerTreeView>(
1771 this, compositor_deps_->GetCompositorMainThreadTaskRunner(),
1772 compositor_deps_->GetCompositorImplThreadTaskRunner(),
1773 compositor_deps_->GetTaskGraphRunner(),
1774 compositor_deps_->GetWebMainThreadScheduler());
1775 layer_tree_view_->Initialize(
danakj6dcbc5962018-11-16 16:45:421776 GenerateLayerTreeSettings(compositor_deps_, for_child_local_root_frame_,
danakj17216f4d2018-10-16 22:57:321777 screen_info_.rect.size(),
1778 screen_info_.device_scale_factor),
1779 compositor_deps_->CreateUkmRecorderFactory());
1780
Fady Samuel1c1ad3692018-11-06 23:28:401781 UpdateSurfaceAndScreenInfo(local_surface_id_allocation_from_parent_,
danakj0d963bd2019-02-20 18:00:461782 CompositorViewportSize(), screen_info_);
danakj17216f4d2018-10-16 22:57:321783 layer_tree_view_->SetContentSourceId(current_content_source_id_);
danakj953af052019-02-06 19:13:181784 // If the widget is hidden, delay starting the compositor until the user shows
1785 // it. Also if the RenderWidget is frozen, we delay starting the compositor
1786 // until we expect to use the widget, which will be signaled through
1787 // WarmupCompositor().
1788 if (!is_frozen_ && !is_hidden_)
1789 StartStopCompositor();
danakj17216f4d2018-10-16 22:57:321790
danakj17216f4d2018-10-16 22:57:321791 DCHECK_NE(MSG_ROUTING_NONE, routing_id_);
1792 layer_tree_view_->SetFrameSinkId(
1793 viz::FrameSinkId(RenderThread::Get()->GetClientId(), routing_id_));
1794
1795 RenderThreadImpl* render_thread = RenderThreadImpl::current();
1796 if (render_thread) {
1797 input_event_queue_ = base::MakeRefCounted<MainThreadEventQueue>(
1798 this, render_thread->GetWebMainThreadScheduler()->InputTaskRunner(),
danakj953af052019-02-06 19:13:181799 render_thread->GetWebMainThreadScheduler(),
1800 /*allow_raf_aligned_input=*/!compositor_never_visible_);
danakj17216f4d2018-10-16 22:57:321801 }
1802
1803 return layer_tree_view_.get();
1804}
1805
danakj953af052019-02-06 19:13:181806void RenderWidget::StartStopCompositor() {
1807 if (compositor_never_visible_)
1808 return;
danakjde8b7482018-11-15 23:45:021809
danakj953af052019-02-06 19:13:181810 if (is_frozen_) {
1811 layer_tree_view_->SetVisible(false);
1812 // Drop all gpu resources, this makes SetVisible(true) more expensive/slower
1813 // but we don't expect to use this RenderWidget again until some possible
1814 // future navigation. This brings us a bit closer to emulating deleting the
1815 // RenderWidget instead of just stopping the compositor.
1816 layer_tree_view_->ReleaseLayerTreeFrameSink();
1817 } else if (is_hidden_) {
1818 layer_tree_view_->SetVisible(false);
1819 } else {
1820 layer_tree_view_->SetVisible(true);
1821 }
danakjde8b7482018-11-15 23:45:021822}
1823
danakj6dcbc5962018-11-16 16:45:421824void RenderWidget::SetIsFrozen(bool is_frozen) {
1825 DCHECK_NE(is_frozen, is_frozen_);
1826 is_frozen_ = is_frozen;
danakj953af052019-02-06 19:13:181827 // If hidden, then frozen changing doesn't change anything with the
1828 // compositor since when hidden the compositor is always stopped.
1829 if (!is_hidden_)
1830 StartStopCompositor();
danakjde8b7482018-11-15 23:45:021831}
1832
danakjf87460792018-12-04 20:14:221833void RenderWidget::WarmupCompositor() {
1834 DCHECK(is_frozen_);
danakj953af052019-02-06 19:13:181835 if (compositor_never_visible_)
danakjf87460792018-12-04 20:14:221836 return;
1837
1838 // Keeping things simple. This would cancel any outstanding warmup if we
1839 // happened to have one (this should be basically impossible). This avoids any
1840 // extra book keeping about the outstanding reqeust.
1841 warmup_weak_ptr_factory_.InvalidateWeakPtrs();
1842 // And if we already did a warmup then we're done.
1843 if (warmup_frame_sink_)
1844 return;
1845
1846 // Mark us pending the warmup frame sink *before* calling
1847 // DoRequestNewLayerTreeFrameSink() as it may run the reply callback
1848 // synchronously. So we don't want to change any state after the call
1849 // to DoRequestNewLayerTreeFrameSink() here.
1850 warmup_frame_sink_request_pending_ = true;
1851
1852 auto cb = base::BindOnce(&RenderWidget::OnReplyForWarmupCompositor,
1853 warmup_weak_ptr_factory_.GetWeakPtr());
1854 DoRequestNewLayerTreeFrameSink(std::move(cb));
1855}
1856
1857void RenderWidget::OnReplyForWarmupCompositor(
1858 std::unique_ptr<cc::LayerTreeFrameSink> sink) {
1859 warmup_frame_sink_request_pending_ = false;
1860
1861 if (after_warmup_callback_)
1862 std::move(after_warmup_callback_).Run(std::move(sink));
1863 else
1864 warmup_frame_sink_ = std::move(sink);
1865}
1866
1867void RenderWidget::AbortWarmupCompositor() {
1868 warmup_frame_sink_request_pending_ = false;
1869 // Drop any pending warmup.
1870 warmup_weak_ptr_factory_.InvalidateWeakPtrs();
1871 // And drop any completed one.
1872 warmup_frame_sink_.reset();
1873
1874 // If we had saved a callback to run after warmup, just do so now indicating
1875 // failure.
1876 if (after_warmup_callback_)
1877 std::move(after_warmup_callback_).Run(nullptr);
1878}
1879
[email protected]2533ce12009-05-09 00:02:241880void RenderWidget::DoDeferredClose() {
Albert J. Wong3c93c182018-09-27 17:29:431881 Send(new WidgetHostMsg_Close(routing_id_));
[email protected]2533ce12009-05-09 00:02:241882}
1883
danakj45e4a4f2019-03-12 22:50:421884void RenderWidget::ClosePopupWidgetSoon() {
1885 // Only should be called for popup widgets.
1886 DCHECK(!for_child_local_root_frame_);
1887 DCHECK(!delegate_);
1888
1889 CloseWidgetSoon();
1890}
1891
Blink Reformat1c4d759e2017-04-09 16:34:541892void RenderWidget::CloseWidgetSoon() {
Avi Drissman07d22452019-03-25 17:56:391893 DCHECK(RenderThread::IsMainThread());
danakj292b63e2018-11-19 20:47:061894 // Prevent compositor from setting up new IPC channels, since we know a
1895 // WidgetMsg_Close is coming. We do this immediately, not in DoDeferredClose,
1896 // as the caller (eg WebPagePopupImpl) may start tearing down things after
1897 // calling this method, including detaching the frame from this RenderWidget.
1898 // Then trying to make a LayerTreeFrameSink would crash.
1899 // https://crbug.com/906340
1900 host_will_close_this_ = true;
1901
initial.commit09911bf2008-07-26 23:55:291902 // If a page calls window.close() twice, we'll end up here twice, but that's
1903 // OK. It is safe to send multiple Close messages.
danakj292b63e2018-11-19 20:47:061904 //
[email protected]2533ce12009-05-09 00:02:241905 // Ask the RenderWidgetHost to initiate close. We could be called from deep
danakj295c43f182019-03-13 16:37:171906 // in Javascript. If we ask the RenderWidgetHost to close now, the window
1907 // could be closed before the JS finishes executing, thanks to nested message
1908 // loops running and handling the resuliting Close IPC. So instead, post a
[email protected]2533ce12009-05-09 00:02:241909 // message back to the message loop, which won't run until the JS is
danakj295c43f182019-03-13 16:37:171910 // complete, and then the Close request can be sent.
Hajime Hoshi315a61f2018-08-14 17:27:281911 GetCleanupTaskRunner()->PostTask(
1912 FROM_HERE, base::BindOnce(&RenderWidget::DoDeferredClose, this));
initial.commit09911bf2008-07-26 23:55:291913}
1914
1915void RenderWidget::Close() {
danakj6dcbc5962018-11-16 16:45:421916 // This was done immediately in the |for_child_local_root_frame_| case in the
1917 // OnClose() IPC handler.
1918 if (!for_child_local_root_frame_)
danakj5c5269b2018-11-06 00:39:111919 CloseWebWidget();
1920
danakja6c10012018-07-06 14:25:361921 layer_tree_view_.reset();
Albert J. Wong7bbf22d2018-12-20 00:27:271922 if (delegate())
1923 delegate()->DidCloseWidget();
Albert J. Wongd593de532018-09-06 05:42:151924 // Note the ACK is a control message going to the RenderProcessHost.
Albert J. Wong3c93c182018-09-27 17:29:431925 RenderThread::Get()->Send(new WidgetHostMsg_Close_ACK(routing_id()));
danakj6ec39d302019-03-15 22:43:221926 closed_ = true;
Ken Buchananed449bb2018-02-01 21:02:051927}
1928
1929void RenderWidget::CloseWebWidget() {
danakj35314ab2018-10-19 16:37:341930 // If the browser has not sent OnDisableDeviceEmulation, we have an emulator
1931 // hanging out still. Destroying it must happen *after* the IPC route is
1932 // removed so that another IPC does not arrive and re-create the emulator
1933 // during closing.
1934 //
1935 // This destruction is normally part of an IPC and expects objects to be alive
1936 // that would be alive while the IPC route is active such as the
1937 // |layer_tree_view_|. So we ensure that it is the first thing to be
1938 // destroyed here before deleting things from the RenderWidget or the
Albert J. Wong7bbf22d2018-12-20 00:27:271939 // delegate().
danakj35314ab2018-10-19 16:37:341940 //
1941 // TODO(danakj): The emulator could reset to non-emulated values in an
1942 // explicit method call (instead of in the destructor) that occurs when
1943 // emulation is disabled, but does not need to occur during RenderWidget
1944 // closing. Then we would not have to destroy this so carefully.
1945 screen_metrics_emulator_.reset();
1946
Albert J. Wong7bbf22d2018-12-20 00:27:271947 // When delegate() is present, the RenderWidget is for a main frame,
danakj974b8ae2019-02-05 00:26:261948 // and the GetWebWidget() will be a WebFrameWidget, which is not the same as
1949 // |webwidget_internal_|. The WebFrameWidget will be closed when the main
1950 // frame is detached, so we do not close it here. But it does not close the
1951 // |webwidget_internal_| since this class takes responsibility for that here
1952 // in all cases.
danakj5c5269b2018-11-06 00:39:111953 webwidget_internal_->Close();
1954 webwidget_internal_ = nullptr;
danakj2d2889a2019-04-05 21:22:081955
1956 close_weak_ptr_factory_.InvalidateWeakPtrs();
initial.commit09911bf2008-07-26 23:55:291957}
1958
Mitsuru Oshima3c07b332018-02-06 04:56:501959void RenderWidget::UpdateWebViewWithDeviceScaleFactor() {
1960 blink::WebFrameWidget* frame_widget = GetFrameWidget();
1961 blink::WebFrame* current_frame =
1962 frame_widget ? frame_widget->LocalRoot() : nullptr;
1963 blink::WebView* webview = current_frame ? current_frame->View() : nullptr;
1964 if (webview) {
danakj4b347212018-07-04 17:55:171965 if (compositor_deps_->IsUseZoomForDSFEnabled())
Fady Samuela863f152018-03-09 16:10:031966 webview->SetZoomFactorForDeviceScaleFactor(
1967 GetWebScreenInfo().device_scale_factor);
Mitsuru Oshima3c07b332018-02-06 04:56:501968 else
Fady Samuela863f152018-03-09 16:10:031969 webview->SetDeviceScaleFactor(GetWebScreenInfo().device_scale_factor);
Mitsuru Oshima3c07b332018-02-06 04:56:501970
1971 webview->GetSettings()->SetPreferCompositingToLCDTextEnabled(
Christopher Camerone9a30c12018-03-07 08:23:381972 PreferCompositingToLCDText(compositor_deps_,
Fady Samuela863f152018-03-09 16:10:031973 GetWebScreenInfo().device_scale_factor));
Mitsuru Oshima3c07b332018-02-06 04:56:501974 }
1975}
1976
Ehsan Karamad655d7b8a2018-01-12 18:38:381977blink::WebFrameWidget* RenderWidget::GetFrameWidget() const {
danakj89ae4532018-12-12 22:52:551978 // TODO(danakj): Remove this check and don't call this method for non-frames.
1979 if (!for_frame())
1980 return nullptr;
1981 // TODO(danakj): Is this needed? IPCs stop after closing, but code used to
1982 // check for a null WebWidget.
1983 if (closing_)
Ehsan Karamad655d7b8a2018-01-12 18:38:381984 return nullptr;
1985
danakj89ae4532018-12-12 22:52:551986 blink::WebWidget* widget;
Albert J. Wong7bbf22d2018-12-20 00:27:271987 if (delegate()) {
danakj89ae4532018-12-12 22:52:551988 // Main frame WebFrameWidgets are held by the delegate, the internal widget
1989 // points directly to the WebView.
1990 // TODO(ekaramad): We should drop IPCs when |is_frozen_| instead of
1991 // handling them and finding a null here. However there is also the case
1992 // of the frame being detached without the widget being frozen to be
1993 // resolved (https://crbug.com/906340). So for now this can return null.
Albert J. Wong7bbf22d2018-12-20 00:27:271994 widget = delegate()->GetWebWidgetForWidget();
danakj89ae4532018-12-12 22:52:551995 } else {
1996 // Subframes always have a WebFrameWidget themselves.
1997 widget = webwidget_internal_;
Ehsan Karamad655d7b8a2018-01-12 18:38:381998 }
danakj89ae4532018-12-12 22:52:551999 return static_cast<blink::WebFrameWidget*>(widget);
Ehsan Karamad655d7b8a2018-01-12 18:38:382000}
2001
bokan841fdc72016-10-06 00:16:352002void RenderWidget::ScreenRectToEmulatedIfNeeded(WebRect* window_rect) const {
2003 DCHECK(window_rect);
2004 float scale = popup_origin_scale_for_emulation_;
2005 if (!scale)
2006 return;
2007 window_rect->x =
2008 popup_view_origin_for_emulation_.x() +
2009 (window_rect->x - popup_screen_origin_for_emulation_.x()) / scale;
2010 window_rect->y =
2011 popup_view_origin_for_emulation_.y() +
2012 (window_rect->y - popup_screen_origin_for_emulation_.y()) / scale;
2013}
2014
2015void RenderWidget::EmulatedToScreenRectIfNeeded(WebRect* window_rect) const {
2016 DCHECK(window_rect);
2017 float scale = popup_origin_scale_for_emulation_;
2018 if (!scale)
2019 return;
2020 window_rect->x =
2021 popup_screen_origin_for_emulation_.x() +
2022 (window_rect->x - popup_view_origin_for_emulation_.x()) * scale;
2023 window_rect->y =
2024 popup_screen_origin_for_emulation_.y() +
2025 (window_rect->y - popup_view_origin_for_emulation_.y()) * scale;
2026}
2027
Blink Reformat1c4d759e2017-04-09 16:34:542028WebRect RenderWidget::WindowRect() {
bokan841fdc72016-10-06 00:16:352029 WebRect rect;
bokan6b08cd22016-10-05 00:55:212030 if (pending_window_rect_count_) {
2031 // NOTE(mbelshe): If there is a pending_window_rect_, then getting
2032 // the RootWindowRect is probably going to return wrong results since the
2033 // browser may not have processed the Move yet. There isn't really anything
2034 // good to do in this case, and it shouldn't happen - since this size is
2035 // only really needed for windowToScreen, which is only used for Popups.
bokan841fdc72016-10-06 00:16:352036 rect = pending_window_rect_;
2037 } else {
2038 rect = window_screen_rect_;
bokan6b08cd22016-10-05 00:55:212039 }
[email protected]2533ce12009-05-09 00:02:242040
bokan841fdc72016-10-06 00:16:352041 ScreenRectToEmulatedIfNeeded(&rect);
2042 return rect;
bokan6b08cd22016-10-05 00:55:212043}
2044
Blink Reformat1c4d759e2017-04-09 16:34:542045WebRect RenderWidget::ViewRect() {
Albert J. Wong3c93c182018-09-27 17:29:432046 WebRect rect = widget_screen_rect_;
bokan841fdc72016-10-06 00:16:352047 ScreenRectToEmulatedIfNeeded(&rect);
2048 return rect;
initial.commit09911bf2008-07-26 23:55:292049}
2050
Blink Reformat1c4d759e2017-04-09 16:34:542051void RenderWidget::SetToolTipText(const blink::WebString& text,
[email protected]8a9d6ca32011-06-06 20:11:302052 WebTextDirection hint) {
Albert J. Wong3c93c182018-09-27 17:29:432053 Send(new WidgetHostMsg_SetTooltipText(routing_id_, text.Utf16(), hint));
[email protected]8a9d6ca32011-06-06 20:11:302054}
2055
Blink Reformat1c4d759e2017-04-09 16:34:542056void RenderWidget::SetWindowRect(const WebRect& rect_in_screen) {
danakj81d601e62018-10-09 22:15:052057 // This path is for the renderer to change the on-screen position/size of
2058 // the widget by changing its window rect. This is not possible for
2059 // RenderWidgets whose position/size are controlled by layout from another
danakj6dcbc5962018-11-16 16:45:422060 // frame tree (ie. child local root frames), as the window rect can only be
2061 // set by the browser.
2062 if (for_child_local_root_frame_)
danakj81d601e62018-10-09 22:15:052063 return;
2064
oshima33ec97cd2015-12-14 19:40:242065 WebRect window_rect = rect_in_screen;
bokan841fdc72016-10-06 00:16:352066 EmulatedToScreenRectIfNeeded(&window_rect);
[email protected]b2e4c70132013-10-03 02:07:512067
danakj7602f4f2019-01-31 18:03:232068 if (synchronous_resize_mode_for_testing_) {
Kent Tamura21d1de62018-12-10 04:45:202069 // This is a web-test-only path. At one point, it was planned to be
danakj65f423b72018-10-10 17:21:202070 // removed. See https://crbug.com/309760.
bokanc007c3a2015-02-03 07:15:562071 SetWindowRectSynchronously(window_rect);
danakj65f423b72018-10-10 17:21:202072 return;
2073 }
2074
2075 if (show_callback_) {
2076 // The widget is not shown yet. Delay the |window_rect| being sent to the
2077 // browser until Show() is called so it can be sent with that IPC, once the
2078 // browser is ready for the info.
2079 initial_rect_ = window_rect;
2080 } else {
2081 Send(new WidgetHostMsg_RequestSetBounds(routing_id_, window_rect));
2082 SetPendingWindowRect(window_rect);
initial.commit09911bf2008-07-26 23:55:292083 }
2084}
2085
[email protected]2533ce12009-05-09 00:02:242086void RenderWidget::SetPendingWindowRect(const WebRect& rect) {
2087 pending_window_rect_ = rect;
2088 pending_window_rect_count_++;
[email protected]2533ce12009-05-09 00:02:242089
bokan6b08cd22016-10-05 00:55:212090 // Popups don't get size updates back from the browser so just store the set
2091 // values.
danakj89ae4532018-12-12 22:52:552092 if (!for_frame()) {
Blink Reformat1c4d759e2017-04-09 16:34:542093 window_screen_rect_ = rect;
Albert J. Wong3c93c182018-09-27 17:29:432094 widget_screen_rect_ = rect;
[email protected]2533ce12009-05-09 00:02:242095 }
[email protected]d4547452008-08-28 18:36:372096}
2097
wjmacleanee244e02017-05-26 21:06:072098void RenderWidget::OnShowContextMenu(ui::MenuSourceType source_type,
2099 const gfx::Point& location) {
wjmacleanee244e02017-05-26 21:06:072100 has_host_context_menu_location_ = true;
2101 host_context_menu_location_ = location;
2102 if (GetWebWidget()) {
2103 GetWebWidget()->ShowContextMenu(
2104 static_cast<blink::WebMenuSourceType>(source_type));
2105 }
2106 has_host_context_menu_location_ = false;
2107}
2108
[email protected]fa7b1dc2010-06-23 17:53:042109void RenderWidget::OnImeSetComposition(
[email protected]fcf75d42013-12-03 20:11:262110 const base::string16& text,
Ryan Landay9e42fd742017-08-12 01:59:112111 const std::vector<WebImeTextSpan>& ime_text_spans,
chongz7eb752802016-01-27 21:28:072112 const gfx::Range& replacement_range,
Ryan Landay9e42fd742017-08-12 01:59:112113 int selection_start,
2114 int selection_end) {
ekaramad5aff1942017-01-06 01:26:352115 if (!ShouldHandleImeEvents())
2116 return;
2117
brettw4b461082016-11-19 18:55:162118#if BUILDFLAG(ENABLE_PLUGINS)
EhsanK22d482e2017-08-10 17:29:492119 if (auto* plugin = GetFocusedPepperPluginInsideWidget()) {
Ryan Landay9e42fd742017-08-12 01:59:112120 plugin->render_frame()->OnImeSetComposition(text, ime_text_spans,
EhsanK22d482e2017-08-10 17:29:492121 selection_start, selection_end);
ekaramad2a46d632016-07-19 13:33:092122 return;
2123 }
2124#endif
[email protected]66fca5bc2013-05-23 06:58:292125 ImeEventGuard guard(this);
ekaramad2daaf672016-11-10 20:29:012126 blink::WebInputMethodController* controller = GetInputMethodController();
ekaramad2daaf672016-11-10 20:29:012127 if (!controller ||
Blink Reformat1c4d759e2017-04-09 16:34:542128 !controller->SetComposition(
Ryan Landay9e42fd742017-08-12 01:59:112129 WebString::FromUTF16(text), WebVector<WebImeTextSpan>(ime_text_spans),
ekaramadce32ef9f2017-02-09 17:33:562130 replacement_range.IsValid()
2131 ? WebRange(replacement_range.start(), replacement_range.length())
2132 : WebRange(),
2133 selection_start, selection_end)) {
[email protected]fa7b1dc2010-06-23 17:53:042134 // If we failed to set the composition text, then we need to let the browser
2135 // process to cancel the input method's ongoing composition session, to make
2136 // sure we are in a consistent state.
Dave Tapuska04bc5ee92018-04-17 19:03:312137 if (mojom::WidgetInputHandlerHost* host =
2138 widget_input_handler_manager_->GetWidgetInputHandlerHost()) {
2139 host->ImeCancelComposition();
Dave Tapuska139a72f2017-09-06 21:57:032140 }
[email protected]7f00efa2010-04-15 05:01:262141 }
nonafa291792016-08-10 02:36:182142 UpdateCompositionInfo(false /* not an immediate request */);
[email protected]fa7b1dc2010-06-23 17:53:042143}
2144
rlanday7efe2302017-01-11 00:14:282145void RenderWidget::OnImeCommitText(
2146 const base::string16& text,
Ryan Landay9e42fd742017-08-12 01:59:112147 const std::vector<WebImeTextSpan>& ime_text_spans,
rlanday7efe2302017-01-11 00:14:282148 const gfx::Range& replacement_range,
2149 int relative_cursor_pos) {
ekaramad5aff1942017-01-06 01:26:352150 if (!ShouldHandleImeEvents())
2151 return;
2152
brettw4b461082016-11-19 18:55:162153#if BUILDFLAG(ENABLE_PLUGINS)
EhsanK22d482e2017-08-10 17:29:492154 if (auto* plugin = GetFocusedPepperPluginInsideWidget()) {
2155 plugin->render_frame()->OnImeCommitText(text, replacement_range,
2156 relative_cursor_pos);
ekaramad2a46d632016-07-19 13:33:092157 return;
2158 }
2159#endif
[email protected]66fca5bc2013-05-23 06:58:292160 ImeEventGuard guard(this);
fsamuele8326c742016-01-12 00:49:392161 input_handler_->set_handling_input_event(true);
ekaramadb6483a052017-02-10 02:23:262162 if (auto* controller = GetInputMethodController()) {
Blink Reformat1c4d759e2017-04-09 16:34:542163 controller->CommitText(
Ryan Landay9e42fd742017-08-12 01:59:112164 WebString::FromUTF16(text), WebVector<WebImeTextSpan>(ime_text_spans),
ekaramadce32ef9f2017-02-09 17:33:562165 replacement_range.IsValid()
2166 ? WebRange(replacement_range.start(), replacement_range.length())
2167 : WebRange(),
2168 relative_cursor_pos);
ekaramadb6483a052017-02-10 02:23:262169 }
aelias87b8f7c2016-09-14 03:19:292170 input_handler_->set_handling_input_event(false);
2171 UpdateCompositionInfo(false /* not an immediate request */);
2172}
2173
2174void RenderWidget::OnImeFinishComposingText(bool keep_selection) {
ekaramad5aff1942017-01-06 01:26:352175 if (!ShouldHandleImeEvents())
2176 return;
2177
brettw4b461082016-11-19 18:55:162178#if BUILDFLAG(ENABLE_PLUGINS)
EhsanK22d482e2017-08-10 17:29:492179 if (auto* plugin = GetFocusedPepperPluginInsideWidget()) {
2180 plugin->render_frame()->OnImeFinishComposingText(keep_selection);
aelias87b8f7c2016-09-14 03:19:292181 return;
2182 }
2183#endif
2184
changwan38c3eb612016-12-09 01:45:562185 if (!GetWebWidget())
aelias87b8f7c2016-09-14 03:19:292186 return;
2187 ImeEventGuard guard(this);
2188 input_handler_->set_handling_input_event(true);
ekaramad2daaf672016-11-10 20:29:012189 if (auto* controller = GetInputMethodController()) {
Blink Reformat1c4d759e2017-04-09 16:34:542190 controller->FinishComposingText(
2191 keep_selection ? WebInputMethodController::kKeepSelection
2192 : WebInputMethodController::kDoNotKeepSelection);
ekaramad2daaf672016-11-10 20:29:012193 }
fsamuele8326c742016-01-12 00:49:392194 input_handler_->set_handling_input_event(false);
nonafa291792016-08-10 02:36:182195 UpdateCompositionInfo(false /* not an immediate request */);
initial.commit09911bf2008-07-26 23:55:292196}
2197
Christopher Camerone9a30c12018-03-07 08:23:382198void RenderWidget::UpdateSurfaceAndScreenInfo(
Fady Samuel1c1ad3692018-11-06 23:28:402199 const viz::LocalSurfaceIdAllocation& new_local_surface_id_allocation,
danakj9c4e7b812019-02-19 22:04:272200 const gfx::Size& compositor_viewport_pixel_size,
Christopher Camerone9a30c12018-03-07 08:23:382201 const ScreenInfo& new_screen_info) {
Christopher Camerone9a30c12018-03-07 08:23:382202 bool orientation_changed =
2203 screen_info_.orientation_angle != new_screen_info.orientation_angle ||
2204 screen_info_.orientation_type != new_screen_info.orientation_type;
2205 bool web_device_scale_factor_changed =
2206 screen_info_.device_scale_factor != new_screen_info.device_scale_factor;
Fady Samuela863f152018-03-09 16:10:032207 ScreenInfo previous_original_screen_info = GetOriginalScreenInfo();
Christopher Cameronbe7ecea2018-02-27 00:47:192208
Fady Samuel1c1ad3692018-11-06 23:28:402209 local_surface_id_allocation_from_parent_ = new_local_surface_id_allocation;
Christopher Camerone9a30c12018-03-07 08:23:382210 screen_info_ = new_screen_info;
Christopher Cameronbe7ecea2018-02-27 00:47:192211
danakj125a29e2019-01-24 23:25:292212 // Note carefully that the DSF specified in |new_screen_info| is not the
2213 // DSF used by the compositor during device emulation!
2214 layer_tree_view_->SetViewportSizeAndScale(
danakj9c4e7b812019-02-19 22:04:272215 compositor_viewport_pixel_size,
danakj125a29e2019-01-24 23:25:292216 GetOriginalScreenInfo().device_scale_factor,
2217 local_surface_id_allocation_from_parent_);
danakj9c4e7b812019-02-19 22:04:272218 // The ViewportVisibleRect derives from the LayerTreeView's viewport size,
2219 // which is set above.
2220 layer_tree_view_->SetViewportVisibleRect(ViewportVisibleRect());
danakj04d128a2019-01-25 02:10:202221 layer_tree_view_->SetRasterColorSpace(
2222 screen_info_.color_space.GetRasterColorSpace());
Christopher Cameronbe7ecea2018-02-27 00:47:192223
Christopher Camerone9a30c12018-03-07 08:23:382224 if (orientation_changed)
2225 OnOrientationChange();
Christopher Cameronbe7ecea2018-02-27 00:47:192226
Fady Samuela863f152018-03-09 16:10:032227 if (previous_original_screen_info != GetOriginalScreenInfo()) {
Christopher Camerone9a30c12018-03-07 08:23:382228 for (auto& observer : render_frame_proxies_)
Fady Samuela863f152018-03-09 16:10:032229 observer.OnScreenInfoChanged(GetOriginalScreenInfo());
Christopher Camerone9a30c12018-03-07 08:23:382230
2231 // Notify all embedded BrowserPlugins of the updated ScreenInfo.
2232 for (auto& observer : browser_plugins_)
Fady Samuela863f152018-03-09 16:10:032233 observer.ScreenInfoChanged(GetOriginalScreenInfo());
Christopher Camerone9a30c12018-03-07 08:23:382234 }
2235
2236 if (web_device_scale_factor_changed)
Christopher Cameronbe7ecea2018-02-27 00:47:192237 UpdateWebViewWithDeviceScaleFactor();
oshimad5279032015-12-16 18:22:332238}
2239
danakja2c9d0a92018-07-25 20:01:182240void RenderWidget::SetWindowRectSynchronously(
2241 const gfx::Rect& new_window_rect) {
2242 VisualProperties visual_properties;
2243 visual_properties.screen_info = screen_info_;
2244 visual_properties.new_size = new_window_rect.size();
2245 visual_properties.compositor_viewport_pixel_size = gfx::ScaleToCeiledSize(
2246 new_window_rect.size(), GetWebScreenInfo().device_scale_factor);
2247 visual_properties.visible_viewport_size = new_window_rect.size();
2248 visual_properties.is_fullscreen_granted = is_fullscreen_granted_;
2249 visual_properties.display_mode = display_mode_;
Fady Samuel84d8a1f72018-11-08 00:26:172250 visual_properties.local_surface_id_allocation =
2251 local_surface_id_allocation_from_parent_;
danakja2c9d0a92018-07-25 20:01:182252 // We are resizing the window from the renderer, so allocate a new
2253 // viz::LocalSurfaceId to avoid surface invariants violations in tests.
danakj125a29e2019-01-24 23:25:292254 layer_tree_view_->RequestNewLocalSurfaceId();
danakja2c9d0a92018-07-25 20:01:182255 SynchronizeVisualProperties(visual_properties);
2256
Albert J. Wong3c93c182018-09-27 17:29:432257 widget_screen_rect_ = new_window_rect;
danakja2c9d0a92018-07-25 20:01:182258 window_screen_rect_ = new_window_rect;
danakj65f423b72018-10-10 17:21:202259 if (show_callback_) {
2260 // Tests may call here directly to control the window rect. If
2261 // Show() did not happen yet, the rect is stored to be passed to the
2262 // browser when the RenderWidget requests Show().
danakja2c9d0a92018-07-25 20:01:182263 initial_rect_ = new_window_rect;
danakj65f423b72018-10-10 17:21:202264 }
danakja2c9d0a92018-07-25 20:01:182265}
2266
Vladimir Levin98d76dad2018-04-21 00:21:292267void RenderWidget::UpdateCaptureSequenceNumber(
2268 uint32_t capture_sequence_number) {
2269 if (capture_sequence_number == last_capture_sequence_number_)
2270 return;
2271 last_capture_sequence_number_ = capture_sequence_number;
2272
2273 // Notify observers of the new capture sequence number.
2274 for (auto& observer : render_frame_proxies_)
2275 observer.UpdateCaptureSequenceNumber(capture_sequence_number);
2276 for (auto& observer : browser_plugins_)
2277 observer.UpdateCaptureSequenceNumber(capture_sequence_number);
2278}
2279
[email protected]4873c7d2009-07-16 06:36:282280void RenderWidget::OnSetTextDirection(WebTextDirection direction) {
EhsanK955ba582017-11-30 21:14:402281 if (auto* frame = GetFocusedWebLocalFrameInWidget())
2282 frame->SetTextDirection(direction);
[email protected]07f953332009-03-25 04:31:112283}
2284
Albert J. Wong3c93c182018-09-27 17:29:432285void RenderWidget::OnUpdateScreenRects(const gfx::Rect& widget_screen_rect,
[email protected]80ad8622012-11-07 16:33:032286 const gfx::Rect& window_screen_rect) {
[email protected]b2e4c70132013-10-03 02:07:512287 if (screen_metrics_emulator_) {
Albert J. Wong3c93c182018-09-27 17:29:432288 screen_metrics_emulator_->OnUpdateScreenRects(widget_screen_rect,
mfomitchev2600fd7c2016-02-17 20:53:392289 window_screen_rect);
[email protected]b2e4c70132013-10-03 02:07:512290 } else {
Albert J. Wong3c93c182018-09-27 17:29:432291 SetScreenRects(widget_screen_rect, window_screen_rect);
[email protected]b2e4c70132013-10-03 02:07:512292 }
Albert J. Wong3c93c182018-09-27 17:29:432293 Send(new WidgetHostMsg_UpdateScreenRects_ACK(routing_id()));
[email protected]80ad8622012-11-07 16:33:032294}
2295
kenrbea731792017-01-13 15:10:482296void RenderWidget::OnSetViewportIntersection(
Ken Buchananb2c9e262018-03-10 16:53:312297 const gfx::Rect& viewport_intersection,
Stefan Zager54e25832018-08-14 22:15:312298 const gfx::Rect& compositor_visible_rect,
Stefan Zager0926c9c2019-03-12 02:46:092299 blink::FrameOcclusionState occlusion_state) {
Ehsan Karamadf3ca2a82018-01-20 01:05:252300 if (auto* frame_widget = GetFrameWidget()) {
Ken Buchananb2c9e262018-03-10 16:53:312301 compositor_visible_rect_ = compositor_visible_rect;
Stefan Zager54e25832018-08-14 22:15:312302 frame_widget->SetRemoteViewportIntersection(viewport_intersection,
Stefan Zager0926c9c2019-03-12 02:46:092303 occlusion_state);
danakja6c10012018-07-06 14:25:362304 layer_tree_view_->SetViewportVisibleRect(ViewportVisibleRect());
kenrbea731792017-01-13 15:10:482305 }
2306}
2307
kenrb04323782017-06-23 01:23:322308void RenderWidget::OnSetIsInert(bool inert) {
danakj89ae4532018-12-12 22:52:552309 if (auto* frame_widget = GetFrameWidget())
Ehsan Karamadf3ca2a82018-01-20 01:05:252310 frame_widget->SetIsInert(inert);
kenrb04323782017-06-23 01:23:322311}
2312
sunxd540a9962018-05-24 22:51:062313void RenderWidget::OnSetInheritedEffectiveTouchAction(
2314 cc::TouchAction touch_action) {
danakj89ae4532018-12-12 22:52:552315 if (auto* frame_widget = GetFrameWidget())
sunxd540a9962018-05-24 22:51:062316 frame_widget->SetInheritedEffectiveTouchAction(touch_action);
sunxd540a9962018-05-24 22:51:062317}
2318
Ken Buchanan8a319fb2017-11-15 18:37:122319void RenderWidget::OnUpdateRenderThrottlingStatus(bool is_throttled,
2320 bool subtree_throttled) {
danakj89ae4532018-12-12 22:52:552321 if (auto* frame_widget = GetFrameWidget())
Ehsan Karamadf3ca2a82018-01-20 01:05:252322 frame_widget->UpdateRenderThrottlingStatus(is_throttled, subtree_throttled);
Ken Buchanan8a319fb2017-11-15 18:37:122323}
2324
paulmeyer90f6c31d2016-11-12 00:17:592325void RenderWidget::OnDragTargetDragEnter(
2326 const std::vector<DropData::Metadata>& drop_meta_data,
Ella Ge80a52dce2017-11-15 18:01:522327 const gfx::PointF& client_point,
2328 const gfx::PointF& screen_point,
paulmeyer90f6c31d2016-11-12 00:17:592329 WebDragOperationsMask ops,
2330 int key_modifiers) {
Ehsan Karamadf3ca2a82018-01-20 01:05:252331 blink::WebFrameWidget* frame_widget = GetFrameWidget();
2332 if (!frame_widget)
paulmeyer90f6c31d2016-11-12 00:17:592333 return;
2334
Ehsan Karamadf3ca2a82018-01-20 01:05:252335 WebDragOperation operation = frame_widget->DragTargetDragEnter(
2336 DropMetaDataToWebDragData(drop_meta_data), client_point, screen_point,
2337 ops, key_modifiers);
paulmeyer90f6c31d2016-11-12 00:17:592338
2339 Send(new DragHostMsg_UpdateDragCursor(routing_id(), operation));
2340}
2341
Ella Ge80a52dce2017-11-15 18:01:522342void RenderWidget::OnDragTargetDragOver(const gfx::PointF& client_point,
2343 const gfx::PointF& screen_point,
paulmeyer90f6c31d2016-11-12 00:17:592344 WebDragOperationsMask ops,
2345 int key_modifiers) {
Ehsan Karamadf3ca2a82018-01-20 01:05:252346 blink::WebFrameWidget* frame_widget = GetFrameWidget();
2347 if (!frame_widget)
paulmeyer90f6c31d2016-11-12 00:17:592348 return;
2349
Ehsan Karamadf3ca2a82018-01-20 01:05:252350 WebDragOperation operation = frame_widget->DragTargetDragOver(
2351 ConvertWindowPointToViewport(client_point), screen_point, ops,
2352 key_modifiers);
paulmeyer90f6c31d2016-11-12 00:17:592353
2354 Send(new DragHostMsg_UpdateDragCursor(routing_id(), operation));
2355}
2356
Ella Ge80a52dce2017-11-15 18:01:522357void RenderWidget::OnDragTargetDragLeave(const gfx::PointF& client_point,
2358 const gfx::PointF& screen_point) {
Ehsan Karamadf3ca2a82018-01-20 01:05:252359 blink::WebFrameWidget* frame_widget = GetFrameWidget();
2360 if (!frame_widget)
paulmeyer90f6c31d2016-11-12 00:17:592361 return;
Ehsan Karamadf3ca2a82018-01-20 01:05:252362
2363 frame_widget
Blink Reformat1c4d759e2017-04-09 16:34:542364 ->DragTargetDragLeave(ConvertWindowPointToViewport(client_point),
kenrb07c272802017-02-07 23:48:172365 screen_point);
paulmeyer90f6c31d2016-11-12 00:17:592366}
2367
2368void RenderWidget::OnDragTargetDrop(const DropData& drop_data,
Ella Ge80a52dce2017-11-15 18:01:522369 const gfx::PointF& client_point,
2370 const gfx::PointF& screen_point,
paulmeyer90f6c31d2016-11-12 00:17:592371 int key_modifiers) {
Ehsan Karamadf3ca2a82018-01-20 01:05:252372 blink::WebFrameWidget* frame_widget = GetFrameWidget();
2373 if (!frame_widget)
paulmeyer90f6c31d2016-11-12 00:17:592374 return;
2375
Ehsan Karamadf3ca2a82018-01-20 01:05:252376 frame_widget->DragTargetDrop(DropDataToWebDragData(drop_data),
2377 ConvertWindowPointToViewport(client_point),
2378 screen_point, key_modifiers);
paulmeyer90f6c31d2016-11-12 00:17:592379}
2380
Ella Ge80a52dce2017-11-15 18:01:522381void RenderWidget::OnDragSourceEnded(const gfx::PointF& client_point,
2382 const gfx::PointF& screen_point,
paulmeyer8fc8ea92016-11-15 05:12:212383 WebDragOperation op) {
Ehsan Karamadf3ca2a82018-01-20 01:05:252384 blink::WebFrameWidget* frame_widget = GetFrameWidget();
2385 if (!frame_widget)
paulmeyer8fc8ea92016-11-15 05:12:212386 return;
2387
Ehsan Karamadf3ca2a82018-01-20 01:05:252388 frame_widget->DragSourceEndedAt(ConvertWindowPointToViewport(client_point),
2389 screen_point, op);
paulmeyer8fc8ea92016-11-15 05:12:212390}
2391
2392void RenderWidget::OnDragSourceSystemDragEnded() {
danakj503545c2019-01-11 19:03:092393 blink::WebFrameWidget* frame_widget = GetFrameWidget();
2394 if (!frame_widget)
paulmeyer8fc8ea92016-11-15 05:12:212395 return;
2396
danakj503545c2019-01-11 19:03:092397 frame_widget->DragSourceSystemDragEnded();
paulmeyer8fc8ea92016-11-15 05:12:212398}
2399
Blink Reformat1c4d759e2017-04-09 16:34:542400void RenderWidget::ShowVirtualKeyboardOnElementFocus() {
okaa398f50b2017-03-21 06:30:262401#if defined(OS_CHROMEOS)
2402 // On ChromeOS, virtual keyboard is triggered only when users leave the
2403 // mouse button or the finger and a text input element is focused at that
2404 // time. Focus event itself shouldn't trigger virtual keyboard.
2405 UpdateTextInputState();
2406#else
changwan75e3b2072017-01-16 02:55:002407 ShowVirtualKeyboard();
okaa398f50b2017-03-21 06:30:262408#endif
boliu7b2be2f2016-11-04 04:58:312409
2410// TODO(rouslan): Fix ChromeOS and Windows 8 behavior of autofill popup with
2411// virtual keyboard.
2412#if !defined(OS_ANDROID)
2413 FocusChangeComplete();
2414#endif
[email protected]0d1ebed12013-08-05 22:01:132415}
2416
fsamuel72464894f2015-12-15 06:59:312417ui::TextInputType RenderWidget::GetTextInputType() {
brettw4b461082016-11-19 18:55:162418#if BUILDFLAG(ENABLE_PLUGINS)
EhsanK22d482e2017-08-10 17:29:492419 if (auto* plugin = GetFocusedPepperPluginInsideWidget())
2420 return plugin->text_input_type();
ekaramad2a46d632016-07-19 13:33:092421#endif
ekaramad5aff1942017-01-06 01:26:352422 if (auto* controller = GetInputMethodController())
Blink Reformat1c4d759e2017-04-09 16:34:542423 return ConvertWebTextInputType(controller->TextInputType());
fsamuel72464894f2015-12-15 06:59:312424 return ui::TEXT_INPUT_TYPE_NONE;
2425}
2426
nonafa291792016-08-10 02:36:182427void RenderWidget::UpdateCompositionInfo(bool immediate_request) {
2428 if (!monitor_composition_info_ && !immediate_request)
2429 return; // Do not calculate composition info if not requested.
nonadac0c7a2016-08-01 02:30:592430
nonafa291792016-08-10 02:36:182431 TRACE_EVENT0("renderer", "RenderWidget::UpdateCompositionInfo");
2432 gfx::Range range;
2433 std::vector<gfx::Rect> character_bounds;
2434
2435 if (GetTextInputType() == ui::TEXT_INPUT_TYPE_NONE) {
2436 // Composition information is only available on editable node.
2437 range = gfx::Range::InvalidRange();
2438 } else {
2439 GetCompositionRange(&range);
2440 GetCompositionCharacterBounds(&character_bounds);
2441 }
2442
2443 if (!immediate_request &&
2444 !ShouldUpdateCompositionInfo(range, character_bounds)) {
fsamuel72464894f2015-12-15 06:59:312445 return;
nonafa291792016-08-10 02:36:182446 }
fsamuel72464894f2015-12-15 06:59:312447 composition_character_bounds_ = character_bounds;
2448 composition_range_ = range;
Dave Tapuska04bc5ee92018-04-17 19:03:312449 if (mojom::WidgetInputHandlerHost* host =
2450 widget_input_handler_manager_->GetWidgetInputHandlerHost()) {
2451 host->ImeCompositionRangeChanged(composition_range_,
2452 composition_character_bounds_);
Dave Tapuska139a72f2017-09-06 21:57:032453 }
fsamuel72464894f2015-12-15 06:59:312454}
2455
Blink Reformat1c4d759e2017-04-09 16:34:542456void RenderWidget::ConvertViewportToWindow(blink::WebRect* rect) {
danakj4b347212018-07-04 17:55:172457 if (compositor_deps_->IsUseZoomForDSFEnabled()) {
Fady Samuela863f152018-03-09 16:10:032458 float reverse = 1 / GetOriginalScreenInfo().device_scale_factor;
oshimad5279032015-12-16 18:22:332459 // TODO(oshima): We may need to allow pixel precision here as the the
oshimaf866dab2015-12-05 00:41:542460 // anchor element can be placed at half pixel.
lfg15b235a32016-08-25 17:45:462461 gfx::Rect window_rect =
2462 gfx::ScaleToEnclosedRect(gfx::Rect(*rect), reverse);
2463 rect->x = window_rect.x();
2464 rect->y = window_rect.y();
2465 rect->width = window_rect.width();
2466 rect->height = window_rect.height();
oshimaf866dab2015-12-05 00:41:542467 }
2468}
2469
Blink Reformat1c4d759e2017-04-09 16:34:542470void RenderWidget::ConvertWindowToViewport(blink::WebFloatRect* rect) {
danakj4b347212018-07-04 17:55:172471 if (compositor_deps_->IsUseZoomForDSFEnabled()) {
Fady Samuela863f152018-03-09 16:10:032472 rect->x *= GetOriginalScreenInfo().device_scale_factor;
2473 rect->y *= GetOriginalScreenInfo().device_scale_factor;
2474 rect->width *= GetOriginalScreenInfo().device_scale_factor;
2475 rect->height *= GetOriginalScreenInfo().device_scale_factor;
oshimaa6985b62016-01-27 08:58:302476 }
2477}
2478
changwan8c342742016-02-26 00:53:392479void RenderWidget::OnRequestTextInputStateUpdate() {
Dave Tapuska9db80842017-07-24 17:24:262480#if defined(OS_ANDROID)
danakj125a29e2019-01-24 23:25:292481 // This task may run between the Close IPC and the task that actually closes
2482 // this class.
2483 if (closing_)
2484 return;
changwan8c342742016-02-26 00:53:392485 DCHECK(!ime_event_guard_);
2486 UpdateSelectionBounds();
changwan75e3b2072017-01-16 02:55:002487 UpdateTextInputStateInternal(false, true /* reply_to_request */);
[email protected]105dffb42013-02-20 03:46:212488#endif
Dave Tapuska9db80842017-07-24 17:24:262489}
[email protected]105dffb42013-02-20 03:46:212490
ekaramadc9b70a72017-03-23 16:14:232491void RenderWidget::OnRequestCompositionUpdates(bool immediate_request,
2492 bool monitor_updates) {
2493 monitor_composition_info_ = monitor_updates;
nonafa291792016-08-10 02:36:182494 if (!immediate_request)
2495 return;
2496 UpdateCompositionInfo(true /* immediate request */);
2497}
2498
[email protected]fcdc5642014-05-09 14:32:242499void RenderWidget::OnOrientationChange() {
Ehsan Karamad655d7b8a2018-01-12 18:38:382500 if (auto* frame_widget = GetFrameWidget()) {
Alex Moshchuk0b409c42017-09-13 02:09:002501 // LocalRoot() might return null for provisional main frames. In this case,
2502 // the frame hasn't committed a navigation and is not swapped into the tree
2503 // yet, so it doesn't make sense to send orientation change events to it.
2504 //
2505 // TODO(https://crbug.com/578349): This check should be cleaned up
2506 // once provisional frames are gone.
Ehsan Karamad655d7b8a2018-01-12 18:38:382507 if (frame_widget->LocalRoot())
2508 frame_widget->LocalRoot()->SendOrientationChangeEvent();
lfg8d649cc2017-04-28 18:04:302509 }
[email protected]fcdc5642014-05-09 14:32:242510}
2511
[email protected]bee16aab2009-08-26 15:55:032512void RenderWidget::SetHidden(bool hidden) {
danakj14e00a8322019-02-07 17:12:062513 // A frozen main frame widget does not become shown or hidden, since it has
2514 // no frame associated with it. It must be thawed before changing visibility.
2515 DCHECK(!is_frozen_);
2516
[email protected]bee16aab2009-08-26 15:55:032517 if (is_hidden_ == hidden)
2518 return;
2519
jdduke8fac9d102014-12-20 02:40:132520 // The status has changed. Tell the RenderThread about it and ensure
2521 // throttled acks are released in case frame production ceases.
[email protected]bee16aab2009-08-26 15:55:032522 is_hidden_ = hidden;
jdduke8fac9d102014-12-20 02:40:132523
Scott Violete3b1a3cb2019-05-02 16:51:522524 if (is_hidden_)
Aditya Keerthic0d45b42019-02-25 18:53:002525 first_update_visual_state_after_hidden_ = true;
alexclarke7fa93942015-10-21 15:37:112526
2527 if (render_widget_scheduling_state_)
2528 render_widget_scheduling_state_->SetHidden(hidden);
danakj1b35e9d2018-11-07 19:09:282529
David Bokanf92eaf22019-03-01 01:54:512530 // If the renderer was hidden, resolve any pending synthetic gestures so they
2531 // aren't blocked waiting for a compositor frame to be generated.
2532 if (is_hidden_)
2533 widget_input_handler_manager_->InvokeInputProcessedCallback();
2534
danakj14e00a8322019-02-07 17:12:062535 StartStopCompositor();
[email protected]bee16aab2009-08-26 15:55:032536}
2537
danakjf9284772019-01-23 18:29:142538void RenderWidget::SetIsFullscreen(bool fullscreen) {
2539 if (fullscreen == is_fullscreen_granted_)
[email protected]2b624c562011-10-27 22:58:262540 return;
danakjf9284772019-01-23 18:29:142541 is_fullscreen_granted_ = fullscreen;
mikhail.pozdnyakovf2c902a2015-04-14 08:09:122542 if (is_fullscreen_granted_) {
Blink Reformat1c4d759e2017-04-09 16:34:542543 GetWebWidget()->DidEnterFullscreen();
[email protected]2b624c562011-10-27 22:58:262544 } else {
Blink Reformat1c4d759e2017-04-09 16:34:542545 GetWebWidget()->DidExitFullscreen();
[email protected]2b624c562011-10-27 22:58:262546 }
[email protected]2b624c562011-10-27 22:58:262547}
2548
changwanf2a707b2015-10-30 08:22:162549void RenderWidget::OnImeEventGuardStart(ImeEventGuard* guard) {
2550 if (!ime_event_guard_)
2551 ime_event_guard_ = guard;
[email protected]66fca5bc2013-05-23 06:58:292552}
2553
changwanf2a707b2015-10-30 08:22:162554void RenderWidget::OnImeEventGuardFinish(ImeEventGuard* guard) {
Jeremy Roman0b261f42018-08-28 15:36:012555 if (ime_event_guard_ != guard)
changwanf2a707b2015-10-30 08:22:162556 return;
changwanf2a707b2015-10-30 08:22:162557 ime_event_guard_ = nullptr;
2558
[email protected]66fca5bc2013-05-23 06:58:292559 // While handling an ime event, text input state and selection bounds updates
2560 // are ignored. These must explicitly be updated once finished handling the
2561 // ime event.
2562 UpdateSelectionBounds();
[email protected]cb9e2632013-06-18 11:26:472563#if defined(OS_ANDROID)
changwan75e3b2072017-01-16 02:55:002564 if (guard->show_virtual_keyboard())
2565 ShowVirtualKeyboard();
2566 else
2567 UpdateTextInputState();
[email protected]cb9e2632013-06-18 11:26:472568#endif
[email protected]66fca5bc2013-05-23 06:58:292569}
2570
[email protected]7c8873e2013-02-05 08:03:012571void RenderWidget::GetSelectionBounds(gfx::Rect* focus, gfx::Rect* anchor) {
brettw4b461082016-11-19 18:55:162572#if BUILDFLAG(ENABLE_PLUGINS)
EhsanK22d482e2017-08-10 17:29:492573 if (auto* plugin = GetFocusedPepperPluginInsideWidget()) {
ekaramad2a46d632016-07-19 13:33:092574 // TODO(kinaba) http://crbug.com/101101
2575 // Current Pepper IME API does not handle selection bounds. So we simply
2576 // use the caret position as an empty range for now. It will be updated
2577 // after Pepper API equips features related to surrounding text retrieval.
EhsanK22d482e2017-08-10 17:29:492578 blink::WebRect caret(plugin->GetCaretBounds());
Blink Reformat1c4d759e2017-04-09 16:34:542579 ConvertViewportToWindow(&caret);
ekaramad2a46d632016-07-19 13:33:092580 *focus = caret;
2581 *anchor = caret;
2582 return;
2583 }
2584#endif
[email protected]7c8873e2013-02-05 08:03:012585 WebRect focus_webrect;
2586 WebRect anchor_webrect;
Blink Reformat1c4d759e2017-04-09 16:34:542587 GetWebWidget()->SelectionBounds(focus_webrect, anchor_webrect);
2588 ConvertViewportToWindow(&focus_webrect);
2589 ConvertViewportToWindow(&anchor_webrect);
oshima33ec97cd2015-12-14 19:40:242590 *focus = focus_webrect;
2591 *anchor = anchor_webrect;
[email protected]73bf95812011-10-12 11:38:322592}
2593
[email protected]e99ef6f2011-10-16 01:13:002594void RenderWidget::UpdateSelectionBounds() {
jdduke1aebad8e2015-07-22 23:25:082595 TRACE_EVENT0("renderer", "RenderWidget::UpdateSelectionBounds");
lfg8ff33912016-09-13 20:59:212596 if (!GetWebWidget())
[email protected]e99ef6f2011-10-16 01:13:002597 return;
changwanf2a707b2015-10-30 08:22:162598 if (ime_event_guard_)
[email protected]66fca5bc2013-05-23 06:58:292599 return;
[email protected]e99ef6f2011-10-16 01:13:002600
mohsenb0eeba72015-08-09 06:20:082601#if defined(USE_AURA)
2602 // TODO(mohsen): For now, always send explicit selection IPC notifications for
2603 // Aura beucause composited selection updates are not working for webview tags
2604 // which regresses IME inside webview. Remove this when composited selection
2605 // updates are fixed for webviews. See, http://crbug.com/510568.
2606 bool send_ipc = true;
2607#else
jddukeacf809e2014-09-23 20:38:382608 // With composited selection updates, the selection bounds will be reported
2609 // directly by the compositor, in which case explicit IPC selection
2610 // notifications should be suppressed.
mohsenb0eeba72015-08-09 06:20:082611 bool send_ipc =
Blink Reformat1c4d759e2017-04-09 16:34:542612 !blink::WebRuntimeFeatures::IsCompositedSelectionUpdateEnabled();
mohsenb0eeba72015-08-09 06:20:082613#endif
2614 if (send_ipc) {
Albert J. Wong3c93c182018-09-27 17:29:432615 WidgetHostMsg_SelectionBounds_Params params;
EhsanK955ba582017-11-30 21:14:402616 params.is_anchor_first = false;
jddukeacf809e2014-09-23 20:38:382617 GetSelectionBounds(&params.anchor_rect, &params.focus_rect);
2618 if (selection_anchor_rect_ != params.anchor_rect ||
2619 selection_focus_rect_ != params.focus_rect) {
2620 selection_anchor_rect_ = params.anchor_rect;
2621 selection_focus_rect_ = params.focus_rect;
EhsanK955ba582017-11-30 21:14:402622 if (auto* focused_frame = GetFocusedWebLocalFrameInWidget()) {
2623 focused_frame->SelectionTextDirection(params.focus_dir,
2624 params.anchor_dir);
2625 params.is_anchor_first = focused_frame->IsSelectionAnchorFirst();
2626 }
Albert J. Wong3c93c182018-09-27 17:29:432627 Send(new WidgetHostMsg_SelectionBoundsChanged(routing_id_, params));
jddukeacf809e2014-09-23 20:38:382628 }
[email protected]58b48a0d2012-06-13 07:01:352629 }
jddukeacf809e2014-09-23 20:38:382630
nonafa291792016-08-10 02:36:182631 UpdateCompositionInfo(false /* not an immediate request */);
[email protected]e99ef6f2011-10-16 01:13:002632}
2633
lfgb00fcad2016-07-14 14:16:332634void RenderWidget::DidAutoResize(const gfx::Size& new_size) {
danakj125a29e2019-01-24 23:25:292635 // Blink can continue running and do a layout/resize between the Close IPC
2636 // and the task that actually closes this class.
2637 if (closing_)
2638 return;
2639
lfgb00fcad2016-07-14 14:16:332640 WebRect new_size_in_window(0, 0, new_size.width(), new_size.height());
Blink Reformat1c4d759e2017-04-09 16:34:542641 ConvertViewportToWindow(&new_size_in_window);
lfgb00fcad2016-07-14 14:16:332642 if (size_.width() != new_size_in_window.width ||
2643 size_.height() != new_size_in_window.height) {
2644 size_ = gfx::Size(new_size_in_window.width, new_size_in_window.height);
2645
danakj7602f4f2019-01-31 18:03:232646 if (synchronous_resize_mode_for_testing_) {
Blink Reformat1c4d759e2017-04-09 16:34:542647 gfx::Rect new_pos(WindowRect().x, WindowRect().y, size_.width(),
2648 size_.height());
Albert J. Wong3c93c182018-09-27 17:29:432649 widget_screen_rect_ = new_pos;
lfgb00fcad2016-07-14 14:16:332650 window_screen_rect_ = new_pos;
2651 }
2652
Christopher Camerone9a30c12018-03-07 08:23:382653 // TODO(ccameron): Note that this destroys any information differentiating
danakj9c4e7b812019-02-19 22:04:272654 // |size_| from the compositor's viewport size. Also note that the
Christopher Cameronaad15a0d2018-03-09 03:47:332655 // calculation of |new_compositor_viewport_pixel_size| does not appear to
2656 // take into account device emulation.
danakj125a29e2019-01-24 23:25:292657 layer_tree_view_->RequestNewLocalSurfaceId();
Christopher Cameronaad15a0d2018-03-09 03:47:332658 gfx::Size new_compositor_viewport_pixel_size =
Fady Samuela863f152018-03-09 16:10:032659 gfx::ScaleToCeiledSize(size_, GetWebScreenInfo().device_scale_factor);
Fady Samuel1c1ad3692018-11-06 23:28:402660 UpdateSurfaceAndScreenInfo(local_surface_id_allocation_from_parent_,
2661 new_compositor_viewport_pixel_size,
2662 screen_info_);
lfgb00fcad2016-07-14 14:16:332663 }
2664}
2665
[email protected]58b48a0d2012-06-13 07:01:352666void RenderWidget::GetCompositionCharacterBounds(
2667 std::vector<gfx::Rect>* bounds) {
2668 DCHECK(bounds);
2669 bounds->clear();
ekaramad2a46d632016-07-19 13:33:092670
brettw4b461082016-11-19 18:55:162671#if BUILDFLAG(ENABLE_PLUGINS)
EhsanK22d482e2017-08-10 17:29:492672 if (GetFocusedPepperPluginInsideWidget())
ekaramad2a46d632016-07-19 13:33:092673 return;
2674#endif
2675
Ehsan Karamad4975af6d2018-02-09 16:58:162676 blink::WebInputMethodController* controller = GetInputMethodController();
2677 if (!controller)
ekaramad2a46d632016-07-19 13:33:092678 return;
2679 blink::WebVector<blink::WebRect> bounds_from_blink;
Ehsan Karamad4975af6d2018-02-09 16:58:162680 if (!controller->GetCompositionCharacterBounds(bounds_from_blink))
ekaramad2a46d632016-07-19 13:33:092681 return;
2682
2683 for (size_t i = 0; i < bounds_from_blink.size(); ++i) {
Blink Reformat1c4d759e2017-04-09 16:34:542684 ConvertViewportToWindow(&bounds_from_blink[i]);
ekaramad2a46d632016-07-19 13:33:092685 bounds->push_back(bounds_from_blink[i]);
2686 }
[email protected]58b48a0d2012-06-13 07:01:352687}
2688
[email protected]db4fc1e2013-09-06 20:01:512689void RenderWidget::GetCompositionRange(gfx::Range* range) {
brettw4b461082016-11-19 18:55:162690#if BUILDFLAG(ENABLE_PLUGINS)
EhsanK22d482e2017-08-10 17:29:492691 if (GetFocusedPepperPluginInsideWidget())
ekaramad2a46d632016-07-19 13:33:092692 return;
2693#endif
EhsanKcdee7b72017-11-08 21:30:092694 blink::WebInputMethodController* controller = GetInputMethodController();
2695 WebRange web_range = controller ? controller->CompositionRange() : WebRange();
Blink Reformat1c4d759e2017-04-09 16:34:542696 if (web_range.IsNull()) {
nona2363a3d2016-11-09 03:26:212697 *range = gfx::Range::InvalidRange();
2698 return;
[email protected]88dbe32f2013-06-20 23:31:362699 }
Blink Reformat1c4d759e2017-04-09 16:34:542700 range->set_start(web_range.StartOffset());
2701 range->set_end(web_range.EndOffset());
[email protected]88dbe32f2013-06-20 23:31:362702}
2703
[email protected]501ea13d2013-07-09 17:03:292704bool RenderWidget::ShouldUpdateCompositionInfo(
[email protected]db4fc1e2013-09-06 20:01:512705 const gfx::Range& range,
[email protected]501ea13d2013-07-09 17:03:292706 const std::vector<gfx::Rect>& bounds) {
changwan2418e1b2016-12-12 12:43:082707 if (!range.IsValid())
2708 return false;
[email protected]501ea13d2013-07-09 17:03:292709 if (composition_range_ != range)
2710 return true;
2711 if (bounds.size() != composition_character_bounds_.size())
2712 return true;
2713 for (size_t i = 0; i < bounds.size(); ++i) {
2714 if (bounds[i] != composition_character_bounds_[i])
2715 return true;
2716 }
2717 return false;
2718}
[email protected]501ea13d2013-07-09 17:03:292719
[email protected]ad26ef42011-06-17 07:59:452720bool RenderWidget::CanComposeInline() {
brettw4b461082016-11-19 18:55:162721#if BUILDFLAG(ENABLE_PLUGINS)
EhsanK22d482e2017-08-10 17:29:492722 if (auto* plugin = GetFocusedPepperPluginInsideWidget())
2723 return plugin->IsPluginAcceptingCompositionEvents();
ekaramad2a46d632016-07-19 13:33:092724#endif
[email protected]ad26ef42011-06-17 07:59:452725 return true;
[email protected]56ea1a62011-05-30 07:05:572726}
2727
Blink Reformat1c4d759e2017-04-09 16:34:542728void RenderWidget::DidHandleGestureEvent(const WebGestureEvent& event,
2729 bool event_cancelled) {
danakja2c9d0a92018-07-25 20:01:182730 if (event_cancelled) {
Albert J. Wong7bbf22d2018-12-20 00:27:272731 // The delegate() doesn't need to hear about cancelled events.
[email protected]c68c3e4e2013-01-24 00:36:562732 return;
danakja2c9d0a92018-07-25 20:01:182733 }
2734
2735#if defined(OS_ANDROID) || defined(USE_AURA)
Blink Reformat1c4d759e2017-04-09 16:34:542736 if (event.GetType() == WebInputEvent::kGestureTap) {
changwan75e3b2072017-01-16 02:55:002737 ShowVirtualKeyboard();
Blink Reformat1c4d759e2017-04-09 16:34:542738 } else if (event.GetType() == WebInputEvent::kGestureLongPress) {
lfg8ff33912016-09-13 20:59:212739 DCHECK(GetWebWidget());
ekaramad5aff1942017-01-06 01:26:352740 blink::WebInputMethodController* controller = GetInputMethodController();
Blink Reformat1c4d759e2017-04-09 16:34:542741 if (!controller || controller->TextInputInfo().value.IsEmpty())
changwan75e3b2072017-01-16 02:55:002742 UpdateTextInputState();
[email protected]07c70d22014-08-21 08:33:462743 else
changwan75e3b2072017-01-16 02:55:002744 ShowVirtualKeyboard();
[email protected]c68c3e4e2013-01-24 00:36:562745 }
ekaramada110f642016-12-21 19:47:282746// TODO(ananta): Piggyback off existing IPCs to communicate this information,
2747// crbug/420130.
2748#if defined(OS_WIN)
danakja2c9d0a92018-07-25 20:01:182749 if (event.GetType() == blink::WebGestureEvent::kGestureTap) {
2750 // TODO(estade): hit test the event against focused node to make sure
2751 // the tap actually hit the focused node.
2752 blink::WebInputMethodController* controller = GetInputMethodController();
2753 blink::WebTextInputType text_input_type =
2754 controller ? controller->TextInputType() : blink::kWebTextInputTypeNone;
ekaramada110f642016-12-21 19:47:282755
Albert J. Wong3c93c182018-09-27 17:29:432756 Send(new WidgetHostMsg_FocusedNodeTouched(
danakja2c9d0a92018-07-25 20:01:182757 routing_id_, text_input_type != blink::kWebTextInputTypeNone));
2758 }
ekaramada110f642016-12-21 19:47:282759#endif
[email protected]c68c3e4e2013-01-24 00:36:562760#endif
danakja2c9d0a92018-07-25 20:01:182761
Albert J. Wong7bbf22d2018-12-20 00:27:272762 // The delegate() gets to respond to handling gestures last.
2763 if (delegate())
2764 delegate()->DidHandleGestureEventForWidget(event);
[email protected]c68c3e4e2013-01-24 00:36:562765}
2766
Blink Reformat1c4d759e2017-04-09 16:34:542767void RenderWidget::DidOverscroll(
danakj1b05cf22019-04-12 23:47:052768 const blink::WebFloatSize& overscroll_delta,
2769 const blink::WebFloatSize& accumulated_overscroll,
sataya.m582c9ce2015-06-09 08:03:422770 const blink::WebFloatPoint& position,
danakj1b05cf22019-04-12 23:47:052771 const blink::WebFloatSize& velocity) {
bokan731ec382016-04-07 03:16:482772#if defined(OS_MACOSX)
2773 // On OSX the user can disable the elastic overscroll effect. If that's the
2774 // case, don't forward the overscroll notification.
2775 DCHECK(compositor_deps());
2776 if (!compositor_deps()->IsElasticOverscrollEnabled())
2777 return;
2778#endif
danakj1b05cf22019-04-12 23:47:052779 input_handler_->DidOverscrollFromBlink(
2780 overscroll_delta, accumulated_overscroll, position, velocity,
2781 layer_tree_view_->layer_tree_host()->overscroll_behavior());
2782}
2783
Daniel Libby688fcb52019-05-25 01:08:472784void RenderWidget::InjectGestureScrollEvent(
2785 blink::WebGestureDevice device,
2786 const blink::WebFloatSize& delta,
2787 ui::input_types::ScrollGranularity granularity,
2788 cc::ElementId scrollable_area_element_id,
2789 blink::WebInputEvent::Type injected_type) {
2790 input_handler_->InjectGestureScrollEvent(
2791 device, delta, granularity, scrollable_area_element_id, injected_type);
2792}
2793
danakj1b05cf22019-04-12 23:47:052794void RenderWidget::SetOverscrollBehavior(
2795 const cc::OverscrollBehavior& behavior) {
2796 layer_tree_view_->layer_tree_host()->SetOverscrollBehavior(behavior);
sataya.m582c9ce2015-06-09 08:03:422797}
2798
danakj4b347212018-07-04 17:55:172799// static
2800cc::LayerTreeSettings RenderWidget::GenerateLayerTreeSettings(
2801 CompositorDependencies* compositor_deps,
2802 bool is_for_subframe,
2803 const gfx::Size& initial_screen_size,
2804 float initial_device_scale_factor) {
2805 const bool is_threaded =
2806 !!compositor_deps->GetCompositorImplThreadTaskRunner();
2807
2808 const base::CommandLine& cmd = *base::CommandLine::ForCurrentProcess();
2809 cc::LayerTreeSettings settings;
2810
Rahul Arakerie510b8c2019-04-01 21:17:542811 settings.compositor_threaded_scrollbar_scrolling =
2812 base::FeatureList::IsEnabled(
2813 features::kCompositorThreadedScrollbarScrolling);
2814
danakj4b347212018-07-04 17:55:172815 settings.resource_settings.use_r16_texture =
2816 base::FeatureList::IsEnabled(media::kUseR16Texture);
2817
2818 settings.commit_to_active_tree = !is_threaded;
2819 settings.is_layer_tree_for_subframe = is_for_subframe;
2820
2821 // For web contents, layer transforms should scale up the contents of layers
2822 // to keep content always crisp when possible.
2823 settings.layer_transforms_should_scale_layer_contents = true;
2824
2825 settings.main_frame_before_activation_enabled =
2826 cmd.HasSwitch(cc::switches::kEnableMainFrameBeforeActivation);
2827
2828 // Checkerimaging is not supported for synchronous single-threaded mode, which
2829 // is what the renderer uses if its not threaded.
2830 settings.enable_checker_imaging =
2831 !cmd.HasSwitch(cc::switches::kDisableCheckerImaging) && is_threaded;
2832
2833#if defined(OS_ANDROID)
2834 // We can use a more aggressive limit on Android since decodes tend to take
2835 // longer on these devices.
2836 settings.min_image_bytes_to_checker = 512 * 1024; // 512kB
2837
2838 // Re-rasterization of checker-imaged content with software raster can be too
2839 // costly on Android.
2840 settings.only_checker_images_with_gpu_raster = true;
2841#endif
2842
2843 auto switch_value_as_int = [](const base::CommandLine& command_line,
2844 const std::string& switch_string, int min_value,
2845 int max_value, int* result) {
2846 std::string string_value = command_line.GetSwitchValueASCII(switch_string);
2847 int int_value;
2848 if (base::StringToInt(string_value, &int_value) && int_value >= min_value &&
2849 int_value <= max_value) {
2850 *result = int_value;
2851 return true;
2852 } else {
danakjda15621e2019-04-05 20:34:432853 DLOG(WARNING) << "Failed to parse switch " << switch_string << ": "
2854 << string_value;
danakj4b347212018-07-04 17:55:172855 return false;
2856 }
2857 };
2858
2859 int default_tile_size = 256;
2860#if defined(OS_ANDROID)
2861 const gfx::Size screen_size =
2862 gfx::ScaleToFlooredSize(initial_screen_size, initial_device_scale_factor);
2863 int display_width = screen_size.width();
2864 int display_height = screen_size.height();
2865 int numTiles = (display_width * display_height) / (256 * 256);
2866 if (numTiles > 16)
2867 default_tile_size = 384;
2868 if (numTiles >= 40)
2869 default_tile_size = 512;
2870
2871 // Adjust for some resolutions that barely straddle an extra
2872 // tile when in portrait mode. This helps worst case scroll/raster
2873 // by not needing a full extra tile for each row.
2874 constexpr int tolerance = 10; // To avoid rounding errors.
2875 int portrait_width = std::min(display_width, display_height);
2876 if (default_tile_size == 256 && std::abs(portrait_width - 768) < tolerance)
2877 default_tile_size += 32;
2878 if (default_tile_size == 384 && std::abs(portrait_width - 1200) < tolerance)
2879 default_tile_size += 32;
2880#elif defined(OS_CHROMEOS) || defined(OS_MACOSX)
2881 // Use 512 for high DPI (dsf=2.0f) devices.
2882 if (initial_device_scale_factor >= 2.0f)
2883 default_tile_size = 512;
2884#endif
2885
2886 // TODO(danakj): This should not be a setting O_O; it should change when the
2887 // device scale factor on LayerTreeHost changes.
2888 settings.default_tile_size = gfx::Size(default_tile_size, default_tile_size);
2889 if (cmd.HasSwitch(switches::kDefaultTileWidth)) {
2890 int tile_width = 0;
2891 switch_value_as_int(cmd, switches::kDefaultTileWidth, 1,
2892 std::numeric_limits<int>::max(), &tile_width);
2893 settings.default_tile_size.set_width(tile_width);
2894 }
2895 if (cmd.HasSwitch(switches::kDefaultTileHeight)) {
2896 int tile_height = 0;
2897 switch_value_as_int(cmd, switches::kDefaultTileHeight, 1,
2898 std::numeric_limits<int>::max(), &tile_height);
2899 settings.default_tile_size.set_height(tile_height);
2900 }
2901
2902 int max_untiled_layer_width = settings.max_untiled_layer_size.width();
2903 if (cmd.HasSwitch(switches::kMaxUntiledLayerWidth)) {
2904 switch_value_as_int(cmd, switches::kMaxUntiledLayerWidth, 1,
2905 std::numeric_limits<int>::max(),
2906 &max_untiled_layer_width);
2907 }
2908 int max_untiled_layer_height = settings.max_untiled_layer_size.height();
2909 if (cmd.HasSwitch(switches::kMaxUntiledLayerHeight)) {
2910 switch_value_as_int(cmd, switches::kMaxUntiledLayerHeight, 1,
2911 std::numeric_limits<int>::max(),
2912 &max_untiled_layer_height);
2913 }
2914
2915 settings.max_untiled_layer_size =
2916 gfx::Size(max_untiled_layer_width, max_untiled_layer_height);
2917
2918 settings.gpu_rasterization_msaa_sample_count =
2919 compositor_deps->GetGpuRasterizationMSAASampleCount();
2920 settings.gpu_rasterization_forced =
2921 compositor_deps->IsGpuRasterizationForced();
2922
2923 settings.can_use_lcd_text = compositor_deps->IsLcdTextEnabled();
2924 settings.use_zero_copy = compositor_deps->IsZeroCopyEnabled();
2925 settings.use_partial_raster = compositor_deps->IsPartialRasterEnabled();
2926 settings.enable_elastic_overscroll =
2927 compositor_deps->IsElasticOverscrollEnabled();
2928 settings.resource_settings.use_gpu_memory_buffer_resources =
2929 compositor_deps->IsGpuMemoryBufferCompositorResourcesEnabled();
2930 settings.use_painted_device_scale_factor =
2931 compositor_deps->IsUseZoomForDSFEnabled();
2932
2933 // Build LayerTreeSettings from command line args.
2934 if (cmd.HasSwitch(cc::switches::kBrowserControlsShowThreshold)) {
2935 std::string top_threshold_str =
2936 cmd.GetSwitchValueASCII(cc::switches::kBrowserControlsShowThreshold);
2937 double show_threshold;
2938 if (base::StringToDouble(top_threshold_str, &show_threshold) &&
2939 show_threshold >= 0.f && show_threshold <= 1.f)
2940 settings.top_controls_show_threshold = show_threshold;
2941 }
2942
2943 if (cmd.HasSwitch(cc::switches::kBrowserControlsHideThreshold)) {
2944 std::string top_threshold_str =
2945 cmd.GetSwitchValueASCII(cc::switches::kBrowserControlsHideThreshold);
2946 double hide_threshold;
2947 if (base::StringToDouble(top_threshold_str, &hide_threshold) &&
2948 hide_threshold >= 0.f && hide_threshold <= 1.f)
2949 settings.top_controls_hide_threshold = hide_threshold;
2950 }
2951
Philip Rogersc55ab6a2018-12-05 17:29:192952 // Blink sends cc a layer list and property trees when either
2953 // BlinkGenPropertyTrees or CompositeAfterPaint are enabled.
Philip Rogersfc2f1162018-10-17 18:28:312954 settings.use_layer_lists =
2955 blink::WebRuntimeFeatures::IsBlinkGenPropertyTreesEnabled() ||
Philip Rogersc55ab6a2018-12-05 17:29:192956 blink::WebRuntimeFeatures::IsCompositeAfterPaintEnabled();
danakj4b347212018-07-04 17:55:172957
David Bokan163b6a72018-10-25 22:03:492958 // Blink currently doesn't support setting fractional scroll offsets so CC
2959 // must send integer values. We plan to eventually make Blink use fractional
2960 // offsets internally: https://crbug.com/414283.
2961 settings.commit_fractional_scroll_deltas =
2962 blink::WebRuntimeFeatures::IsFractionalScrollOffsetsEnabled();
2963
danakj4b347212018-07-04 17:55:172964 // The means the renderer compositor has 2 possible modes:
2965 // - Threaded compositing with a scheduler.
Kent Tamura21d1de62018-12-10 04:45:202966 // - Single threaded compositing without a scheduler (for web tests only).
2967 // Using the scheduler in web tests introduces additional composite steps
danakj4b347212018-07-04 17:55:172968 // that create flakiness.
2969 settings.single_thread_proxy_scheduler = false;
2970
2971 // These flags should be mirrored by UI versions in ui/compositor/.
2972 if (cmd.HasSwitch(cc::switches::kShowCompositedLayerBorders))
2973 settings.initial_debug_state.show_debug_borders.set();
2974 settings.initial_debug_state.show_layer_animation_bounds_rects =
2975 cmd.HasSwitch(cc::switches::kShowLayerAnimationBounds);
2976 settings.initial_debug_state.show_paint_rects =
2977 cmd.HasSwitch(switches::kShowPaintRects);
rnasri2e621942019-05-14 21:24:252978 settings.initial_debug_state.show_layout_shift_regions =
2979 cmd.HasSwitch(switches::kShowLayoutShiftRegions);
danakj4b347212018-07-04 17:55:172980 settings.initial_debug_state.show_property_changed_rects =
2981 cmd.HasSwitch(cc::switches::kShowPropertyChangedRects);
2982 settings.initial_debug_state.show_surface_damage_rects =
2983 cmd.HasSwitch(cc::switches::kShowSurfaceDamageRects);
2984 settings.initial_debug_state.show_screen_space_rects =
2985 cmd.HasSwitch(cc::switches::kShowScreenSpaceRects);
2986
2987 settings.initial_debug_state.SetRecordRenderingStats(
2988 cmd.HasSwitch(cc::switches::kEnableGpuBenchmarking));
2989 settings.enable_surface_synchronization =
2990 features::IsSurfaceSynchronizationEnabled();
2991 settings.build_hit_test_data = features::IsVizHitTestingSurfaceLayerEnabled();
2992
2993 if (cmd.HasSwitch(cc::switches::kSlowDownRasterScaleFactor)) {
2994 const int kMinSlowDownScaleFactor = 0;
2995 const int kMaxSlowDownScaleFactor = INT_MAX;
2996 switch_value_as_int(
2997 cmd, cc::switches::kSlowDownRasterScaleFactor, kMinSlowDownScaleFactor,
2998 kMaxSlowDownScaleFactor,
2999 &settings.initial_debug_state.slow_down_raster_scale_factor);
3000 }
3001
3002 // This is default overlay scrollbar settings for Android and DevTools mobile
3003 // emulator. Aura Overlay Scrollbar will override below.
3004 settings.scrollbar_animator = cc::LayerTreeSettings::ANDROID_OVERLAY;
3005 settings.solid_color_scrollbar_color = SkColorSetARGB(128, 128, 128, 128);
3006 settings.scrollbar_fade_delay = base::TimeDelta::FromMilliseconds(300);
3007 settings.scrollbar_fade_duration = base::TimeDelta::FromMilliseconds(300);
3008
Daniel Libby43972d82019-02-28 21:35:363009 if (cmd.HasSwitch(cc::switches::kCCScrollAnimationDurationForTesting)) {
3010 const int kMinScrollAnimationDuration = 0;
3011 const int kMaxScrollAnimationDuration = INT_MAX;
3012 int duration;
3013 if (switch_value_as_int(cmd,
3014 cc::switches::kCCScrollAnimationDurationForTesting,
3015 kMinScrollAnimationDuration,
3016 kMaxScrollAnimationDuration, &duration)) {
3017 settings.scroll_animation_duration_for_testing =
3018 base::TimeDelta::FromSeconds(duration);
3019 }
3020 }
3021
danakj4b347212018-07-04 17:55:173022#if defined(OS_ANDROID)
3023 bool using_synchronous_compositor =
3024 compositor_deps->UsingSynchronousCompositing();
3025 bool using_low_memory_policy = base::SysInfo::IsLowEndDevice();
3026
3027 settings.use_stream_video_draw_quad = true;
3028 settings.using_synchronous_renderer_compositor = using_synchronous_compositor;
3029 if (using_synchronous_compositor) {
3030 // Android WebView uses system scrollbars, so make ours invisible.
3031 // http://crbug.com/677348: This can't be done using hide_scrollbars
3032 // setting because supporting -webkit custom scrollbars is still desired
3033 // on sublayers.
3034 settings.scrollbar_animator = cc::LayerTreeSettings::NO_ANIMATOR;
3035 settings.solid_color_scrollbar_color = SK_ColorTRANSPARENT;
3036
3037 settings.enable_early_damage_check =
3038 cmd.HasSwitch(cc::switches::kCheckDamageEarly);
3039 }
3040 // Android WebView handles root layer flings itself.
3041 settings.ignore_root_layer_flings = using_synchronous_compositor;
3042 // Memory policy on Android WebView does not depend on whether device is
3043 // low end, so always use default policy.
3044 if (using_low_memory_policy && !using_synchronous_compositor) {
3045 // On low-end we want to be very carefull about killing other
3046 // apps. So initially we use 50% more memory to avoid flickering
3047 // or raster-on-demand.
3048 settings.max_memory_for_prepaint_percentage = 67;
3049 } else {
3050 // On other devices we have increased memory excessively to avoid
3051 // raster-on-demand already, so now we reserve 50% _only_ to avoid
3052 // raster-on-demand, and use 50% of the memory otherwise.
3053 settings.max_memory_for_prepaint_percentage = 50;
3054 }
3055
3056 // TODO(danakj): Only do this on low end devices.
3057 settings.create_low_res_tiling = true;
3058
3059#else // defined(OS_ANDROID)
3060 bool using_synchronous_compositor = false; // Only for Android WebView.
3061 // On desktop, we never use the low memory policy unless we are simulating
3062 // low-end mode via a switch.
3063 bool using_low_memory_policy =
3064 cmd.HasSwitch(switches::kEnableLowEndDeviceMode);
3065
3066 if (ui::IsOverlayScrollbarEnabled()) {
3067 settings.scrollbar_animator = cc::LayerTreeSettings::AURA_OVERLAY;
3068 settings.scrollbar_fade_delay = ui::kOverlayScrollbarFadeDelay;
3069 settings.scrollbar_fade_duration = ui::kOverlayScrollbarFadeDuration;
3070 settings.scrollbar_thinning_duration =
3071 ui::kOverlayScrollbarThinningDuration;
3072 settings.scrollbar_flash_after_any_scroll_update =
3073 ui::OverlayScrollbarFlashAfterAnyScrollUpdate();
3074 settings.scrollbar_flash_when_mouse_enter =
3075 ui::OverlayScrollbarFlashWhenMouseEnter();
3076 }
3077
3078 // On desktop, if there's over 4GB of memory on the machine, increase the
3079 // working set size to 256MB for both gpu and software.
3080 const int kImageDecodeMemoryThresholdMB = 4 * 1024;
3081 if (base::SysInfo::AmountOfPhysicalMemoryMB() >=
3082 kImageDecodeMemoryThresholdMB) {
3083 settings.decoded_image_working_set_budget_bytes = 256 * 1024 * 1024;
3084 } else {
3085 // This is the default, but recorded here as well.
3086 settings.decoded_image_working_set_budget_bytes = 128 * 1024 * 1024;
3087 }
3088#endif // defined(OS_ANDROID)
3089
3090 if (using_low_memory_policy) {
3091 // RGBA_4444 textures are only enabled:
3092 // - If the user hasn't explicitly disabled them
3093 // - If system ram is <= 512MB (1GB devices are sometimes low-end).
3094 // - If we are not running in a WebView, where 4444 isn't supported.
3095 if (!cmd.HasSwitch(switches::kDisableRGBA4444Textures) &&
3096 base::SysInfo::AmountOfPhysicalMemoryMB() <= 512 &&
3097 !using_synchronous_compositor) {
3098 settings.use_rgba_4444 = viz::RGBA_4444;
3099
3100 // If we are going to unpremultiply and dither these tiles, we need to
3101 // allocate an additional RGBA_8888 intermediate for each tile
3102 // rasterization when rastering to RGBA_4444 to allow for dithering.
3103 // Setting a reasonable sized max tile size allows this intermediate to
3104 // be consistently reused.
3105 if (base::FeatureList::IsEnabled(
3106 kUnpremultiplyAndDitherLowBitDepthTiles)) {
3107 settings.max_gpu_raster_tile_size = gfx::Size(512, 256);
3108 settings.unpremultiply_and_dither_low_bit_depth_tiles = true;
3109 }
3110 }
3111 }
3112
3113 if (cmd.HasSwitch(switches::kEnableLowResTiling))
3114 settings.create_low_res_tiling = true;
3115 if (cmd.HasSwitch(switches::kDisableLowResTiling))
3116 settings.create_low_res_tiling = false;
3117
3118 if (cmd.HasSwitch(switches::kEnableRGBA4444Textures) &&
3119 !cmd.HasSwitch(switches::kDisableRGBA4444Textures)) {
3120 settings.use_rgba_4444 = true;
3121 }
3122
3123 settings.max_staging_buffer_usage_in_bytes = 32 * 1024 * 1024; // 32MB
3124 // Use 1/4th of staging buffers on low-end devices.
3125 if (base::SysInfo::IsLowEndDevice())
3126 settings.max_staging_buffer_usage_in_bytes /= 4;
3127
3128 cc::ManagedMemoryPolicy defaults = settings.memory_policy;
3129 settings.memory_policy = GetGpuMemoryPolicy(defaults, initial_screen_size,
3130 initial_device_scale_factor);
3131
3132 settings.disallow_non_exact_resource_reuse =
3133 cmd.HasSwitch(switches::kDisallowNonExactResourceReuse);
3134#if defined(OS_ANDROID)
3135 // TODO(crbug.com/746931): This feature appears to be causing visual
3136 // corruption on certain android devices. Will investigate and re-enable.
3137 settings.disallow_non_exact_resource_reuse = true;
3138#endif
3139
3140 if (cmd.HasSwitch(switches::kRunAllCompositorStagesBeforeDraw)) {
3141 settings.wait_for_all_pipeline_stages_before_draw = true;
3142 settings.enable_latency_recovery = false;
3143 }
Eric Karl1e6ffca2019-02-23 22:08:503144#if defined(OS_ANDROID)
3145 if (features::IsSurfaceSynchronizationEnabled()) {
3146 // TODO(crbug.com/933846): LatencyRecovery is causing jank on Android.
Eric Karl75cd8952019-03-19 21:33:103147 // Disable in viz mode for now, with plan to disable more widely once
Eric Karl1e6ffca2019-02-23 22:08:503148 // viz launches.
3149 settings.enable_latency_recovery = false;
3150 }
3151#endif
danakj4b347212018-07-04 17:55:173152
3153 settings.enable_image_animation_resync =
3154 !cmd.HasSwitch(switches::kDisableImageAnimationResync);
3155
Karolina Soltys4d60b402018-10-01 19:28:203156 settings.send_compositor_frame_ack = false;
3157
danakj4b347212018-07-04 17:55:173158 return settings;
3159}
3160
3161// static
3162cc::ManagedMemoryPolicy RenderWidget::GetGpuMemoryPolicy(
3163 const cc::ManagedMemoryPolicy& default_policy,
3164 const gfx::Size& initial_screen_size,
3165 float initial_device_scale_factor) {
3166 cc::ManagedMemoryPolicy actual = default_policy;
3167 actual.bytes_limit_when_visible = 0;
3168
3169 // If the value was overridden on the command line, use the specified value.
3170 static bool client_hard_limit_bytes_overridden =
3171 base::CommandLine::ForCurrentProcess()->HasSwitch(
3172 switches::kForceGpuMemAvailableMb);
3173 if (client_hard_limit_bytes_overridden) {
3174 if (base::StringToSizeT(
3175 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
3176 switches::kForceGpuMemAvailableMb),
3177 &actual.bytes_limit_when_visible))
3178 actual.bytes_limit_when_visible *= 1024 * 1024;
3179 return actual;
3180 }
3181
3182#if defined(OS_ANDROID)
3183 // We can't query available GPU memory from the system on Android.
3184 // Physical memory is also mis-reported sometimes (eg. Nexus 10 reports
3185 // 1262MB when it actually has 2GB, while Razr M has 1GB but only reports
3186 // 128MB java heap size). First we estimate physical memory using both.
3187 size_t dalvik_mb = base::SysInfo::DalvikHeapSizeMB();
3188 size_t physical_mb = base::SysInfo::AmountOfPhysicalMemoryMB();
3189 size_t physical_memory_mb = 0;
3190 if (base::SysInfo::IsLowEndDevice()) {
3191 // TODO(crbug.com/742534): The code below appears to no longer work.
3192 // |dalvik_mb| no longer follows the expected heuristic pattern, causing us
3193 // to over-estimate memory on low-end devices. This entire section probably
3194 // needs to be re-written, but for now we can address the low-end Android
3195 // issues by ignoring |dalvik_mb|.
3196 physical_memory_mb = physical_mb;
3197 } else if (dalvik_mb >= 256) {
3198 physical_memory_mb = dalvik_mb * 4;
3199 } else {
3200 physical_memory_mb = std::max(dalvik_mb * 4, (physical_mb * 4) / 3);
3201 }
3202
3203 // Now we take a default of 1/8th of memory on high-memory devices,
3204 // and gradually scale that back for low-memory devices (to be nicer
3205 // to other apps so they don't get killed). Examples:
3206 // Nexus 4/10(2GB) 256MB (normally 128MB)
3207 // Droid Razr M(1GB) 114MB (normally 57MB)
3208 // Galaxy Nexus(1GB) 100MB (normally 50MB)
3209 // Xoom(1GB) 100MB (normally 50MB)
3210 // Nexus S(low-end) 8MB (normally 8MB)
3211 // Note that the compositor now uses only some of this memory for
3212 // pre-painting and uses the rest only for 'emergencies'.
3213 if (actual.bytes_limit_when_visible == 0) {
3214 // NOTE: Non-low-end devices use only 50% of these limits,
3215 // except during 'emergencies' where 100% can be used.
3216 if (physical_memory_mb >= 1536)
3217 actual.bytes_limit_when_visible = physical_memory_mb / 8; // >192MB
3218 else if (physical_memory_mb >= 1152)
3219 actual.bytes_limit_when_visible = physical_memory_mb / 8; // >144MB
3220 else if (physical_memory_mb >= 768)
3221 actual.bytes_limit_when_visible = physical_memory_mb / 10; // >76MB
3222 else if (physical_memory_mb >= 513)
3223 actual.bytes_limit_when_visible = physical_memory_mb / 12; // <64MB
3224 else
3225 // Devices with this little RAM have very little headroom so we hardcode
3226 // the limit rather than relying on the heuristics above. (They also use
3227 // 4444 textures so we can use a lower limit.)
3228 actual.bytes_limit_when_visible = 8;
3229
3230 actual.bytes_limit_when_visible =
3231 actual.bytes_limit_when_visible * 1024 * 1024;
3232 // Clamp the observed value to a specific range on Android.
3233 actual.bytes_limit_when_visible = std::max(
3234 actual.bytes_limit_when_visible, static_cast<size_t>(8 * 1024 * 1024));
3235 actual.bytes_limit_when_visible =
3236 std::min(actual.bytes_limit_when_visible,
3237 static_cast<size_t>(256 * 1024 * 1024));
3238 }
3239 actual.priority_cutoff_when_visible =
3240 gpu::MemoryAllocation::CUTOFF_ALLOW_EVERYTHING;
3241#else
3242 // Ignore what the system said and give all clients the same maximum
3243 // allocation on desktop platforms.
3244 actual.bytes_limit_when_visible = 512 * 1024 * 1024;
3245 actual.priority_cutoff_when_visible =
3246 gpu::MemoryAllocation::CUTOFF_ALLOW_NICE_TO_HAVE;
3247
3248 // For large monitors (4k), double the tile memory to avoid frequent out of
3249 // memory problems. 4k could mean a screen width of anywhere from 3840 to 4096
3250 // (see https://en.wikipedia.org/wiki/4K_resolution). We use 3500 as a proxy
3251 // for "large enough".
3252 static const int kLargeDisplayThreshold = 3500;
3253 int display_width =
3254 std::round(initial_screen_size.width() * initial_device_scale_factor);
3255 if (display_width >= kLargeDisplayThreshold)
3256 actual.bytes_limit_when_visible *= 2;
3257#endif
3258 return actual;
3259}
3260
Navid Zolghadr4ed3ae42019-05-03 17:02:123261void RenderWidget::HasPointerRawUpdateEventHandlers(bool has_handlers) {
Navid Zolghadr8fdb6114e2018-08-29 19:00:543262 if (input_event_queue_)
Navid Zolghadr4ed3ae42019-05-03 17:02:123263 input_event_queue_->HasPointerRawUpdateEventHandlers(has_handlers);
Navid Zolghadr8fdb6114e2018-08-29 19:00:543264}
3265
Blink Reformat1c4d759e2017-04-09 16:34:543266void RenderWidget::HasTouchEventHandlers(bool has_handlers) {
W. James MacLean662d5382018-03-29 18:33:083267 if (has_touch_handlers_ && *has_touch_handlers_ == has_handlers)
3268 return;
3269
3270 has_touch_handlers_ = has_handlers;
alexclarke7fa93942015-10-21 15:37:113271 if (render_widget_scheduling_state_)
3272 render_widget_scheduling_state_->SetHasTouchHandler(has_handlers);
Albert J. Wongeb28e7e2018-10-12 00:57:433273 Send(new WidgetHostMsg_HasTouchEventHandlers(routing_id_, has_handlers));
[email protected]ce6689f2013-03-29 12:52:553274}
3275
Dave Tapuskadfe486c12017-06-09 16:53:133276void RenderWidget::SetNeedsLowLatencyInput(bool needs_low_latency) {
3277 if (input_event_queue_)
3278 input_event_queue_->SetNeedsLowLatency(needs_low_latency);
3279}
3280
Pavel Feldman6708eefa2019-01-17 03:14:323281void RenderWidget::SetNeedsUnbufferedInputForDebugger(bool unbuffered) {
3282 if (input_event_queue_)
3283 input_event_queue_->SetNeedsUnbufferedInputForDebugger(unbuffered);
3284}
3285
W. James MacLeand973a55b2018-11-29 21:39:133286void RenderWidget::AnimateDoubleTapZoomInMainFrame(
3287 const blink::WebPoint& point,
3288 const blink::WebRect& rect_to_zoom) {
3289 // Only oopif subframes should be sending this message.
Albert J. Wong7bbf22d2018-12-20 00:27:273290 DCHECK(!delegate());
W. James MacLeand973a55b2018-11-29 21:39:133291 Send(new WidgetHostMsg_AnimateDoubleTapZoomInMainFrame(routing_id(), point,
3292 rect_to_zoom));
3293}
3294
W. James MacLean5372eb72018-12-19 12:56:363295void RenderWidget::ZoomToFindInPageRectInMainFrame(
3296 const blink::WebRect& rect_to_zoom) {
3297 // Only oopif subframes should be sending this message.
Albert J. Wong7bbf22d2018-12-20 00:27:273298 DCHECK(!delegate_);
W. James MacLean5372eb72018-12-19 12:56:363299 Send(new WidgetHostMsg_ZoomToFindInPageRectInMainFrame(routing_id(),
3300 rect_to_zoom));
3301}
3302
danakj4886cad2019-02-26 22:15:563303void RenderWidget::RegisterViewportLayers(const cc::ViewportLayers& layers) {
3304 layer_tree_view_->layer_tree_host()->RegisterViewportLayers(layers);
3305}
3306
3307void RenderWidget::RegisterSelection(const cc::LayerSelection& selection) {
3308 layer_tree_view_->layer_tree_host()->RegisterSelection(selection);
3309}
3310
chaopeng161241b72019-03-21 18:25:173311void RenderWidget::FallbackCursorModeLockCursor(bool left,
3312 bool right,
3313 bool up,
3314 bool down) {
3315 widget_input_handler_manager_->FallbackCursorModeLockCursor(left, right, up,
3316 down);
3317}
3318
3319void RenderWidget::FallbackCursorModeSetCursorVisibility(bool visible) {
3320 widget_input_handler_manager_->FallbackCursorModeSetCursorVisibility(visible);
3321}
3322
danakj35c203a2019-04-16 17:12:393323void RenderWidget::SetAllowGpuRasterization(bool allow_gpu_raster) {
3324 layer_tree_view_->layer_tree_host()->SetHasGpuRasterizationTrigger(
3325 allow_gpu_raster);
3326}
3327
W. James MacLean233a64ff2019-05-18 16:18:493328void RenderWidget::SetPageScaleStateAndLimits(float page_scale_factor,
3329 bool is_pinch_gesture_active,
3330 float minimum,
3331 float maximum) {
danakj77821e252019-03-27 19:58:043332 layer_tree_view_->layer_tree_host()->SetPageScaleFactorAndLimits(
3333 page_scale_factor, minimum, maximum);
W. James MacLean233a64ff2019-05-18 16:18:493334
3335 // Only continue if this is a mainframe, or something's actually changed.
3336 if (!delegate() ||
3337 (page_scale_factor == page_scale_factor_from_mainframe_ &&
3338 is_pinch_gesture_active == is_pinch_gesture_active_from_mainframe_)) {
3339 return;
3340 }
3341
3342 // The page scale is controlled by the WebView for the local main frame of
3343 // the Page. So this is called from blink by for the RenderWidget of that
3344 // local main frame. We forward the value on to each child RenderWidget (each
3345 // of which will be via proxy child frame). These will each in turn forward
3346 // the message to their child RenderWidgets (through their proxy child
3347 // frames).
3348 DCHECK(!is_frozen_);
3349
3350 for (auto& observer : render_frame_proxies_) {
3351 observer.OnPageScaleFactorChanged(page_scale_factor,
3352 is_pinch_gesture_active);
3353 }
3354 // Store the value to give to any new RenderFrameProxy that is registered.
3355 page_scale_factor_from_mainframe_ = page_scale_factor;
3356 is_pinch_gesture_active_from_mainframe_ = is_pinch_gesture_active;
danakj77821e252019-03-27 19:58:043357}
3358
3359void RenderWidget::StartPageScaleAnimation(const gfx::Vector2d& target_offset,
3360 bool use_anchor,
3361 float new_page_scale,
3362 double duration_sec) {
3363 base::TimeDelta duration = base::TimeDelta::FromSecondsD(duration_sec);
3364 layer_tree_view_->layer_tree_host()->StartPageScaleAnimation(
3365 target_offset, use_anchor, new_page_scale, duration);
3366}
3367
danakjda15621e2019-04-05 20:34:433368void RenderWidget::RequestDecode(const cc::PaintImage& image,
3369 base::OnceCallback<void(bool)> callback) {
3370 layer_tree_view_->layer_tree_host()->QueueImageDecode(image,
3371 std::move(callback));
3372}
3373
danakj2d2889a2019-04-05 21:22:083374// Enables measuring and reporting both presentation times and swap times in
3375// swap promises.
3376class ReportTimeSwapPromise : public cc::SwapPromise {
3377 using ReportTimeCallback = blink::WebWidgetClient::ReportTimeCallback;
3378
3379 public:
Mohsen Izadid6715832019-06-05 00:32:513380 ReportTimeSwapPromise(ReportTimeCallback swap_time_callback,
3381 ReportTimeCallback presentation_time_callback,
danakj2d2889a2019-04-05 21:22:083382 scoped_refptr<base::SingleThreadTaskRunner> task_runner,
3383 base::WeakPtr<RenderWidget> render_widget)
Mohsen Izadid6715832019-06-05 00:32:513384 : swap_time_callback_(std::move(swap_time_callback)),
3385 presentation_time_callback_(std::move(presentation_time_callback)),
danakj2d2889a2019-04-05 21:22:083386 task_runner_(std::move(task_runner)),
3387 render_widget_(std::move(render_widget)) {}
3388 ~ReportTimeSwapPromise() override = default;
3389
3390 void DidActivate() override {}
3391
3392 void WillSwap(viz::CompositorFrameMetadata* metadata) override {
3393 DCHECK_GT(metadata->frame_token, 0u);
3394 // The interval between the current swap and its presentation time is
3395 // reported in UMA (see corresponding code in DidSwap() below).
3396 frame_token_ = metadata->frame_token;
3397 }
3398 void DidSwap() override {
3399 DCHECK_GT(frame_token_, 0u);
3400 task_runner_->PostTask(
Mohsen Izadid6715832019-06-05 00:32:513401 FROM_HERE, base::BindOnce(&RunCallbackAfterSwap, base::TimeTicks::Now(),
3402 std::move(swap_time_callback_),
3403 std::move(presentation_time_callback_),
3404 std::move(render_widget_), frame_token_));
danakj2d2889a2019-04-05 21:22:083405 }
3406
Bo Liudef993a2019-04-24 20:08:493407 cc::SwapPromise::DidNotSwapAction DidNotSwap(
3408 DidNotSwapReason reason) override {
danakj2d2889a2019-04-05 21:22:083409 blink::WebWidgetClient::SwapResult result;
3410 switch (reason) {
3411 case cc::SwapPromise::DidNotSwapReason::SWAP_FAILS:
3412 result = blink::WebWidgetClient::SwapResult::kDidNotSwapSwapFails;
3413 break;
3414 case cc::SwapPromise::DidNotSwapReason::COMMIT_FAILS:
3415 result = blink::WebWidgetClient::SwapResult::kDidNotSwapCommitFails;
3416 break;
3417 case cc::SwapPromise::DidNotSwapReason::COMMIT_NO_UPDATE:
3418 result = blink::WebWidgetClient::SwapResult::kDidNotSwapCommitNoUpdate;
3419 break;
3420 case cc::SwapPromise::DidNotSwapReason::ACTIVATION_FAILS:
3421 result = blink::WebWidgetClient::SwapResult::kDidNotSwapActivationFails;
3422 break;
3423 }
3424 // During a failed swap, return the current time regardless of whether we're
3425 // using presentation or swap timestamps.
3426 task_runner_->PostTask(
3427 FROM_HERE,
Mohsen Izadid6715832019-06-05 00:32:513428 base::BindOnce(
3429 [](blink::WebWidgetClient::SwapResult result,
3430 base::TimeTicks swap_time, ReportTimeCallback swap_time_callback,
3431 ReportTimeCallback presentation_time_callback) {
3432 ReportTime(std::move(swap_time_callback), result, swap_time);
3433 ReportTime(std::move(presentation_time_callback), result,
3434 swap_time);
3435 },
3436 result, base::TimeTicks::Now(), std::move(swap_time_callback_),
3437 std::move(presentation_time_callback_)));
Bo Liudef993a2019-04-24 20:08:493438 return DidNotSwapAction::BREAK_PROMISE;
danakj2d2889a2019-04-05 21:22:083439 }
3440
3441 int64_t TraceId() const override { return 0; }
3442
3443 private:
Mohsen Izadid6715832019-06-05 00:32:513444 static void RunCallbackAfterSwap(
3445 base::TimeTicks swap_time,
3446 ReportTimeCallback swap_time_callback,
3447 ReportTimeCallback presentation_time_callback,
3448 base::WeakPtr<RenderWidget> render_widget,
3449 int frame_token) {
3450 if (render_widget) {
3451 render_widget->layer_tree_view()->AddPresentationCallback(
3452 frame_token,
3453 base::BindOnce(&RunCallbackAfterPresentation,
3454 std::move(presentation_time_callback), swap_time));
3455 ReportTime(std::move(swap_time_callback),
3456 blink::WebWidgetClient::SwapResult::kDidSwap, swap_time);
3457 } else {
3458 ReportTime(std::move(swap_time_callback),
3459 blink::WebWidgetClient::SwapResult::kDidSwap, swap_time);
3460 ReportTime(std::move(presentation_time_callback),
3461 blink::WebWidgetClient::SwapResult::kDidSwap, swap_time);
3462 }
3463 }
3464
3465 static void RunCallbackAfterPresentation(
3466 ReportTimeCallback presentation_time_callback,
danakj2d2889a2019-04-05 21:22:083467 base::TimeTicks swap_time,
3468 base::TimeTicks presentation_time) {
3469 DCHECK(!swap_time.is_null());
3470 bool presentation_time_is_valid =
3471 !presentation_time.is_null() && (presentation_time > swap_time);
3472 UMA_HISTOGRAM_BOOLEAN("PageLoad.Internal.Renderer.PresentationTime.Valid",
3473 presentation_time_is_valid);
3474 if (presentation_time_is_valid) {
3475 // This measures from 1ms to 10seconds.
3476 UMA_HISTOGRAM_TIMES(
3477 "PageLoad.Internal.Renderer.PresentationTime.DeltaFromSwapTime",
3478 presentation_time - swap_time);
3479 }
Mohsen Izadid6715832019-06-05 00:32:513480 ReportTime(std::move(presentation_time_callback),
3481 blink::WebWidgetClient::SwapResult::kDidSwap,
3482 presentation_time_is_valid ? presentation_time : swap_time);
danakj2d2889a2019-04-05 21:22:083483 }
3484
Mohsen Izadid6715832019-06-05 00:32:513485 static void ReportTime(ReportTimeCallback callback,
3486 blink::WebWidgetClient::SwapResult result,
3487 base::TimeTicks time) {
3488 if (callback)
3489 std::move(callback).Run(result, time);
3490 }
3491
3492 ReportTimeCallback swap_time_callback_;
3493 ReportTimeCallback presentation_time_callback_;
danakj2d2889a2019-04-05 21:22:083494 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
3495 base::WeakPtr<RenderWidget> render_widget_;
3496 uint32_t frame_token_ = 0;
3497
3498 DISALLOW_COPY_AND_ASSIGN(ReportTimeSwapPromise);
3499};
3500
3501void RenderWidget::NotifySwapTime(ReportTimeCallback callback) {
Mohsen Izadid6715832019-06-05 00:32:513502 NotifySwapAndPresentationTime(base::NullCallback(), std::move(callback));
3503}
3504
3505void RenderWidget::NotifySwapAndPresentationTime(
3506 ReportTimeCallback swap_time_callback,
3507 ReportTimeCallback presentation_time_callback) {
danakj2d2889a2019-04-05 21:22:083508 cc::LayerTreeHost* layer_tree_host = layer_tree_view_->layer_tree_host();
3509 // When the WebWidget is closed we cancel any pending SwapPromise that would
3510 // call back into blink, so we use |close_weak_ptr_factory_|.
3511 layer_tree_host->QueueSwapPromise(std::make_unique<ReportTimeSwapPromise>(
Mohsen Izadid6715832019-06-05 00:32:513512 std::move(swap_time_callback), std::move(presentation_time_callback),
danakj2d2889a2019-04-05 21:22:083513 layer_tree_host->GetTaskRunnerProvider()->MainThreadTaskRunner(),
3514 close_weak_ptr_factory_.GetWeakPtr()));
3515}
3516
Dave Tapuskabafc2ba32017-11-28 01:54:373517void RenderWidget::RequestUnbufferedInputEvents() {
3518 if (input_event_queue_)
3519 input_event_queue_->RequestUnbufferedInputEvents();
3520}
3521
xidachenfa0199e72017-05-11 11:34:263522void RenderWidget::SetTouchAction(cc::TouchAction touch_action) {
Dave Tapuskab28a1082017-08-30 15:37:263523 if (!input_handler_->ProcessTouchAction(touch_action))
[email protected]5d0bbdfa92013-12-10 00:35:513524 return;
3525
Dave Tapuska04bc5ee92018-04-17 19:03:313526 widget_input_handler_manager_->ProcessTouchAction(touch_action);
[email protected]5d0bbdfa92013-12-10 00:35:513527}
3528
[email protected]e3244ed2014-06-20 20:04:273529void RenderWidget::RegisterRenderFrameProxy(RenderFrameProxy* proxy) {
3530 render_frame_proxies_.AddObserver(proxy);
danakj0246bba02019-01-23 23:12:273531 // Page scale factor is propagated down the RenderWidget tree (across
3532 // frame trees). A new RenderFrameProxy means there is a new child
3533 // RenderWidget in another frame tree. In order for it to hear about
3534 // the page scale factor we pass along the last seen value here.
W. James MacLean2cd99b62019-04-08 13:54:433535 proxy->OnPageScaleFactorChanged(page_scale_factor_from_mainframe_,
3536 is_pinch_gesture_active_from_mainframe_);
[email protected]bffc8302014-01-23 20:52:163537}
3538
[email protected]e3244ed2014-06-20 20:04:273539void RenderWidget::UnregisterRenderFrameProxy(RenderFrameProxy* proxy) {
3540 render_frame_proxies_.RemoveObserver(proxy);
[email protected]bffc8302014-01-23 20:52:163541}
3542
[email protected]de3c5d82014-05-28 22:12:593543void RenderWidget::RegisterRenderFrame(RenderFrameImpl* frame) {
3544 render_frames_.AddObserver(frame);
3545}
3546
3547void RenderWidget::UnregisterRenderFrame(RenderFrameImpl* frame) {
3548 render_frames_.RemoveObserver(frame);
3549}
3550
Saman Samid189e5dfd2017-12-20 22:55:313551void RenderWidget::RegisterBrowserPlugin(BrowserPlugin* browser_plugin) {
3552 browser_plugins_.AddObserver(browser_plugin);
Saman Sami50d1e0c2018-03-13 20:03:493553 browser_plugin->ScreenInfoChanged(GetOriginalScreenInfo());
Saman Samid189e5dfd2017-12-20 22:55:313554}
3555
3556void RenderWidget::UnregisterBrowserPlugin(BrowserPlugin* browser_plugin) {
3557 browser_plugins_.RemoveObserver(browser_plugin);
3558}
3559
Albert J. Wong3c93c182018-09-27 17:29:433560void RenderWidget::OnWaitNextFrameForTests(
3561 int main_frame_thread_observer_routing_id) {
danakj53802692018-07-25 21:46:443562 // Sends an ACK to the browser process during the next compositor frame.
Albert J. Wong3c93c182018-09-27 17:29:433563 QueueMessage(new WidgetHostMsg_WaitForNextFrameForTests_ACK(
3564 main_frame_thread_observer_routing_id));
lfg43e08e62016-02-03 18:51:373565}
3566
Fady Samuela863f152018-03-09 16:10:033567const ScreenInfo& RenderWidget::GetWebScreenInfo() const {
3568 return screen_info_;
Christopher Camerone9a30c12018-03-07 08:23:383569}
3570
Fady Samuela863f152018-03-09 16:10:033571const ScreenInfo& RenderWidget::GetOriginalScreenInfo() const {
Christopher Camerone9a30c12018-03-07 08:23:383572 return screen_metrics_emulator_
3573 ? screen_metrics_emulator_->original_screen_info()
Fady Samuela863f152018-03-09 16:10:033574 : screen_info_;
oshima50872a72016-03-04 13:26:183575}
3576
Ella Ge80a52dce2017-11-15 18:01:523577gfx::PointF RenderWidget::ConvertWindowPointToViewport(
3578 const gfx::PointF& point) {
paulmeyer90f6c31d2016-11-12 00:17:593579 blink::WebFloatRect point_in_viewport(point.x(), point.y(), 0, 0);
Blink Reformat1c4d759e2017-04-09 16:34:543580 ConvertWindowToViewport(&point_in_viewport);
Ella Ge80a52dce2017-11-15 18:01:523581 return gfx::PointF(point_in_viewport.x, point_in_viewport.y);
3582}
3583
3584gfx::Point RenderWidget::ConvertWindowPointToViewport(const gfx::Point& point) {
3585 return gfx::ToRoundedPoint(ConvertWindowPointToViewport(gfx::PointF(point)));
paulmeyer90f6c31d2016-11-12 00:17:593586}
3587
Blink Reformat1c4d759e2017-04-09 16:34:543588bool RenderWidget::RequestPointerLock() {
lfgbee1e0a2016-06-08 21:24:213589 return mouse_lock_dispatcher_->LockMouse(webwidget_mouse_lock_target_.get());
3590}
3591
Blink Reformat1c4d759e2017-04-09 16:34:543592void RenderWidget::RequestPointerUnlock() {
lfgbee1e0a2016-06-08 21:24:213593 mouse_lock_dispatcher_->UnlockMouse(webwidget_mouse_lock_target_.get());
3594}
3595
Blink Reformat1c4d759e2017-04-09 16:34:543596bool RenderWidget::IsPointerLocked() {
lfgbee1e0a2016-06-08 21:24:213597 return mouse_lock_dispatcher_->IsMouseLockedTo(
3598 webwidget_mouse_lock_target_.get());
3599}
3600
Richard Li49fe04d2018-10-21 09:07:193601void RenderWidget::StartDragging(network::mojom::ReferrerPolicy policy,
paulmeyer6ef5a792016-11-08 20:33:583602 const WebDragData& data,
3603 WebDragOperationsMask mask,
danakj0c75ad82018-07-10 19:50:123604 const SkBitmap& drag_image,
danakjef1735a2018-11-13 19:34:443605 const gfx::Point& web_image_offset) {
3606 blink::WebRect offset_in_window(web_image_offset.x(), web_image_offset.y(), 0,
3607 0);
Blink Reformat1c4d759e2017-04-09 16:34:543608 ConvertViewportToWindow(&offset_in_window);
paulmeyer6ef5a792016-11-08 20:33:583609 DropData drop_data(DropDataBuilder::Build(data));
3610 drop_data.referrer_policy = policy;
danakj0c75ad82018-07-10 19:50:123611 gfx::Vector2d image_offset(offset_in_window.x, offset_in_window.y);
3612 Send(new DragHostMsg_StartDragging(routing_id(), drop_data, mask, drag_image,
3613 image_offset, possible_drag_event_info_));
paulmeyer6ef5a792016-11-08 20:33:583614}
3615
kenrb5d78b842017-03-06 21:06:013616uint32_t RenderWidget::GetContentSourceId() {
3617 return current_content_source_id_;
3618}
3619
Saman Samif7731342018-01-24 22:18:443620void RenderWidget::DidNavigate() {
danakj125a29e2019-01-24 23:25:293621 // Blink may be navigating still between the Close IPC and the task that
3622 // actually closes this class, and for a main frame that would come through
3623 // this method. But since we are closing we can skip it.
3624 if (closing_)
Kinuko Yasudabef2b642019-01-24 04:53:333625 return;
danakj125a29e2019-01-24 23:25:293626
3627 ++current_content_source_id_;
danakja6c10012018-07-06 14:25:363628 layer_tree_view_->SetContentSourceId(current_content_source_id_);
3629 layer_tree_view_->ClearCachesOnNextCommit();
kenrb5d78b842017-03-06 21:06:013630}
3631
lfg8ff33912016-09-13 20:59:213632blink::WebWidget* RenderWidget::GetWebWidget() const {
Albert J. Wong7bbf22d2018-12-20 00:27:273633 if (delegate()) {
3634 blink::WebWidget* delegate_widget = delegate()->GetWebWidgetForWidget();
danakja2c9d0a92018-07-25 20:01:183635 if (delegate_widget)
3636 return delegate_widget;
3637 }
lfg8ff33912016-09-13 20:59:213638 return webwidget_internal_;
3639}
3640
ekaramad2daaf672016-11-10 20:29:013641blink::WebInputMethodController* RenderWidget::GetInputMethodController()
3642 const {
Ehsan Karamad655d7b8a2018-01-12 18:38:383643 if (auto* frame_widget = GetFrameWidget())
3644 return frame_widget->GetActiveWebInputMethodController();
3645
3646 return nullptr;
ekaramad2daaf672016-11-10 20:29:013647}
3648
Dave Tapuska139a72f2017-09-06 21:57:033649void RenderWidget::SetupWidgetInputHandler(
3650 mojom::WidgetInputHandlerRequest request,
3651 mojom::WidgetInputHandlerHostPtr host) {
Dave Tapuskab66c28f2017-11-15 17:18:473652 widget_input_handler_manager_->AddInterface(std::move(request),
3653 std::move(host));
Dave Tapuska525eb15e2017-08-17 21:05:503654}
3655
Dave Tapuska485aca92017-08-08 00:47:583656void RenderWidget::SetWidgetBinding(mojom::WidgetRequest request) {
3657 // Close the old binding if there was one.
3658 // A RenderWidgetHost should not need more than one channel.
3659 widget_binding_.Close();
3660 widget_binding_.Bind(std::move(request));
3661}
3662
Ken Buchanan94c0beb62018-06-22 19:56:243663void RenderWidget::SetMouseCapture(bool capture) {
3664 if (mojom::WidgetInputHandlerHost* host =
3665 widget_input_handler_manager_->GetWidgetInputHandlerHost()) {
3666 host->SetMouseCapture(capture);
3667 }
3668}
3669
Fady Samuelca9ecb72018-05-05 05:59:273670bool RenderWidget::IsSurfaceSynchronizationEnabled() const {
danakja6c10012018-07-06 14:25:363671 return layer_tree_view_ &&
3672 layer_tree_view_->IsSurfaceSynchronizationEnabled();
Fady Samuelca9ecb72018-05-05 05:59:273673}
3674
danakj53802692018-07-25 21:46:443675void RenderWidget::UseSynchronousResizeModeForTesting(bool enable) {
danakj7602f4f2019-01-31 18:03:233676 synchronous_resize_mode_for_testing_ = enable;
danakja2c9d0a92018-07-25 20:01:183677}
3678
3679void RenderWidget::SetDeviceScaleFactorForTesting(float factor) {
danakjb4cec102019-01-30 19:22:133680 DCHECK_GT(factor, 0.f);
danakja2c9d0a92018-07-25 20:01:183681
danakja2c9d0a92018-07-25 20:01:183682 // We are changing the device scale factor from the renderer, so allocate a
3683 // new viz::LocalSurfaceId to avoid surface invariants violations in tests.
danakj125a29e2019-01-24 23:25:293684 layer_tree_view_->RequestNewLocalSurfaceId();
danakjb4cec102019-01-30 19:22:133685
3686 ScreenInfo info = screen_info_;
3687 info.device_scale_factor = factor;
3688 gfx::Size viewport_pixel_size = gfx::ScaleToCeiledSize(size_, factor);
3689 UpdateSurfaceAndScreenInfo(local_surface_id_allocation_from_parent_,
3690 viewport_pixel_size, info);
3691
3692 ResizeWebWidget(); // This picks up the new device scale factor in |info|.
3693
3694 gfx::Size visible_viewport_size = visible_viewport_size_;
3695 if (compositor_deps_->IsUseZoomForDSFEnabled()) {
3696 visible_viewport_size =
3697 gfx::ScaleToCeiledSize(visible_viewport_size, factor);
3698 }
3699 GetWebWidget()->ResizeVisualViewport(visible_viewport_size);
3700
3701 // Make sure the DSF override stays for future VisualProperties updates, and
3702 // that includes overriding the VisualProperties'
3703 // compositor_viewport_pixel_size with size * this for-testing DSF.
3704 device_scale_factor_for_testing_ = factor;
danakja2c9d0a92018-07-25 20:01:183705}
3706
3707void RenderWidget::SetDeviceColorSpaceForTesting(
3708 const gfx::ColorSpace& color_space) {
danakjb4cec102019-01-30 19:22:133709 // We are changing the device color space from the renderer, so allocate a
3710 // new viz::LocalSurfaceId to avoid surface invariants violations in tests.
3711 layer_tree_view_->RequestNewLocalSurfaceId();
3712
danakj04d128a2019-01-25 02:10:203713 ScreenInfo info = screen_info_;
3714 info.color_space = color_space;
3715 UpdateSurfaceAndScreenInfo(local_surface_id_allocation_from_parent_,
danakj0d963bd2019-02-20 18:00:463716 CompositorViewportSize(), info);
danakja2c9d0a92018-07-25 20:01:183717}
3718
3719void RenderWidget::SetWindowRectSynchronouslyForTesting(
3720 const gfx::Rect& new_window_rect) {
3721 SetWindowRectSynchronously(new_window_rect);
3722}
3723
3724void RenderWidget::EnableAutoResizeForTesting(const gfx::Size& min_size,
3725 const gfx::Size& max_size) {
3726 VisualProperties visual_properties;
3727 visual_properties.auto_resize_enabled = true;
3728 visual_properties.min_size_for_auto_resize = min_size;
3729 visual_properties.max_size_for_auto_resize = max_size;
Fady Samuel84d8a1f72018-11-08 00:26:173730 visual_properties.local_surface_id_allocation =
3731 base::Optional<viz::LocalSurfaceIdAllocation>(
3732 viz::LocalSurfaceIdAllocation(
3733 viz::LocalSurfaceId(1, 1, base::UnguessableToken::Create()),
3734 base::TimeTicks::Now()));
danakja2c9d0a92018-07-25 20:01:183735 OnSynchronizeVisualProperties(visual_properties);
3736}
3737
3738void RenderWidget::DisableAutoResizeForTesting(const gfx::Size& new_size) {
3739 if (!auto_resize_mode_)
3740 return;
3741
3742 VisualProperties visual_properties;
3743 visual_properties.auto_resize_enabled = false;
3744 visual_properties.screen_info = screen_info_;
3745 visual_properties.new_size = new_size;
danakj0d963bd2019-02-20 18:00:463746 visual_properties.compositor_viewport_pixel_size = CompositorViewportSize();
danakja2c9d0a92018-07-25 20:01:183747 visual_properties.browser_controls_shrink_blink_size =
3748 browser_controls_shrink_blink_size_;
3749 visual_properties.top_controls_height = top_controls_height_;
3750 visual_properties.visible_viewport_size = visible_viewport_size_;
3751 visual_properties.is_fullscreen_granted = is_fullscreen_granted_;
3752 visual_properties.display_mode = display_mode_;
3753 OnSynchronizeVisualProperties(visual_properties);
3754}
3755
EhsanK955ba582017-11-30 21:14:403756blink::WebLocalFrame* RenderWidget::GetFocusedWebLocalFrameInWidget() const {
Ehsan Karamad655d7b8a2018-01-12 18:38:383757 if (auto* frame_widget = GetFrameWidget())
3758 return frame_widget->FocusedWebLocalFrameInWidget();
3759 return nullptr;
EhsanK955ba582017-11-30 21:14:403760}
3761
EhsanK22d482e2017-08-10 17:29:493762#if BUILDFLAG(ENABLE_PLUGINS)
3763PepperPluginInstanceImpl* RenderWidget::GetFocusedPepperPluginInsideWidget() {
Ehsan Karamad655d7b8a2018-01-12 18:38:383764 blink::WebFrameWidget* frame_widget = GetFrameWidget();
3765 if (!frame_widget)
EhsanK22d482e2017-08-10 17:29:493766 return nullptr;
3767
3768 // Focused pepper instance might not always be in the focused frame. For
3769 // instance if a pepper instance and its embedder frame are focused an then
3770 // another frame takes focus using javascript, the embedder frame will no
3771 // longer be focused while the pepper instance is (the embedder frame's
3772 // |focused_pepper_plugin_| is not nullptr). Especially, if the pepper plugin
3773 // is fullscreen, clicking into the pepper will not refocus the embedder
3774 // frame. This is why we have to traverse the whole frame tree to find the
3775 // focused plugin.
Ehsan Karamad655d7b8a2018-01-12 18:38:383776 blink::WebFrame* current_frame = frame_widget->LocalRoot();
EhsanK22d482e2017-08-10 17:29:493777 while (current_frame) {
3778 RenderFrameImpl* render_frame =
3779 current_frame->IsWebLocalFrame()
3780 ? RenderFrameImpl::FromWebFrame(current_frame)
3781 : nullptr;
3782 if (render_frame && render_frame->focused_pepper_plugin())
3783 return render_frame->focused_pepper_plugin();
3784 current_frame = current_frame->TraverseNext();
3785 }
3786 return nullptr;
3787}
3788#endif
3789
Ken Buchananb2c9e262018-03-10 16:53:313790gfx::Rect RenderWidget::ViewportVisibleRect() {
danakj9c4e7b812019-02-19 22:04:273791 if (for_child_local_root_frame_)
3792 return compositor_visible_rect_;
danakj0d963bd2019-02-20 18:00:463793 return gfx::Rect(CompositorViewportSize());
Ken Buchananb2c9e262018-03-10 16:53:313794}
3795
Hajime Hoshi315a61f2018-08-14 17:27:283796// static
3797scoped_refptr<base::SingleThreadTaskRunner>
3798RenderWidget::GetCleanupTaskRunner() {
3799 return RenderThreadImpl::current_blink_platform_impl()
3800 ->main_thread_scheduler()
3801 ->CleanupTaskRunner();
3802}
3803
Saman Sami50d1e0c2018-03-13 20:03:493804base::WeakPtr<RenderWidget> RenderWidget::AsWeakPtr() {
3805 return weak_ptr_factory_.GetWeakPtr();
3806}
3807
[email protected]e9ff79c2012-10-19 21:31:263808} // namespace content