blob: ff843ae99de19963e460fd47dce75eb444572030 [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"
[email protected]4fb66842009-12-04 21:41:0015#include "base/command_line.h"
changwan7ded3752016-03-09 23:25:1216#include "base/feature_list.h"
initial.commit09911bf2008-07-26 23:55:2917#include "base/logging.h"
avi1023d012015-12-25 02:39:1418#include "base/macros.h"
dchengcedca5612016-04-09 01:40:1519#include "base/memory/ptr_util.h"
[email protected]b256eca2013-07-11 10:57:4020#include "base/memory/singleton.h"
asvitkine8d51e9d2016-09-02 23:55:4321#include "base/metrics/histogram_macros.h"
[email protected]aa4117f2011-12-09 22:19:2122#include "base/stl_util.h"
danakj4b347212018-07-04 17:55:1723#include "base/strings/string_number_conversions.h"
[email protected]74ebfb12013-06-07 20:48:0024#include "base/strings/utf_string_conversions.h"
Sebastien Marchand75a7cdf2018-11-13 23:47:0325#include "base/system/sys_info.h"
fdoraya19b7702016-12-23 14:19:3126#include "base/threading/thread_task_runner_handle.h"
primiano9e38d552015-01-28 04:18:0127#include "base/trace_event/trace_event.h"
[email protected]661eb9d2009-02-03 02:11:4828#include "build/build_config.h"
loyso6e6efc42017-01-21 02:23:0329#include "cc/animation/animation_host.h"
danakj4b347212018-07-04 17:55:1730#include "cc/base/switches.h"
xidachenfa0199e72017-05-11 11:34:2631#include "cc/input/touch_action.h"
danakjba65a0912017-09-21 16:38:4232#include "cc/trees/layer_tree_frame_sink.h"
loyso6e6efc42017-01-21 02:23:0333#include "cc/trees/layer_tree_host.h"
danakj4c987652018-07-05 15:49:4134#include "cc/trees/ukm_manager.h"
danakj4b347212018-07-04 17:55:1735#include "components/viz/common/features.h"
Fady Samuelc645ffe2017-07-24 17:28:2036#include "components/viz/common/frame_sinks/begin_frame_source.h"
danakjf20f4502017-09-26 17:13:3137#include "components/viz/common/frame_sinks/copy_output_request.h"
danakj4b347212018-07-04 17:55:1738#include "components/viz/common/switches.h"
39#include "content/common/content_switches_internal.h"
paulmeyer6ef5a792016-11-08 20:33:5840#include "content/common/drag_event_source_info.h"
41#include "content/common/drag_messages.h"
jonrossa2ff4f82018-02-16 17:27:4642#include "content/common/render_frame_metadata.mojom.h"
jcivelliae1560a2016-11-01 22:40:2343#include "content/common/render_message_filter.mojom.h"
Nasko Oskovf97e8b02017-07-25 02:45:4244#include "content/common/swapped_out_messages.h"
Sadrul Habib Chowdhury8bc43cc2018-06-06 01:48:0345#include "content/common/tab_switching_time_callback.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"
Saman Samid189e5dfd2017-12-20 22:55:3156#include "content/renderer/browser_plugin/browser_plugin.h"
Sadrul Habib Chowdhury31c98712018-12-11 04:15:1357#include "content/renderer/compositor/layer_tree_view.h"
[email protected]953bd0062013-08-01 00:58:4058#include "content/renderer/cursor_utils.h"
mfomitchev2600fd7c2016-02-17 20:53:3959#include "content/renderer/devtools/render_widget_screen_metrics_emulator.h"
paulmeyer6ef5a792016-11-08 20:33:5860#include "content/renderer/drop_data_builder.h"
[email protected]b2e4c70132013-10-03 02:07:5161#include "content/renderer/external_popup_menu.h"
Sadrul Habib Chowdhury31c98712018-12-11 04:15:1362#include "content/renderer/frame_swap_message_queue.h"
[email protected]66fca5bc2013-05-23 06:58:2963#include "content/renderer/ime_event_guard.h"
dtapuska9ec1a912017-04-21 15:18:3164#include "content/renderer/input/main_thread_event_queue.h"
Dave Tapuska9db80842017-07-24 17:24:2665#include "content/renderer/input/widget_input_handler_manager.h"
[email protected]adab2332013-07-25 18:04:3266#include "content/renderer/pepper/pepper_plugin_instance_impl.h"
Sadrul Habib Chowdhury31c98712018-12-11 04:15:1367#include "content/renderer/queue_message_swap_promise.h"
[email protected]bffc8302014-01-23 20:52:1668#include "content/renderer/render_frame_impl.h"
danakj4b347212018-07-04 17:55:1769#include "content/renderer/render_frame_metadata_observer_impl.h"
[email protected]e3244ed2014-06-20 20:04:2770#include "content/renderer/render_frame_proxy.h"
[email protected]8704f89b2011-04-15 00:30:0571#include "content/renderer/render_process.h"
[email protected]f1a29a02011-10-06 23:08:4472#include "content/renderer/render_thread_impl.h"
dcheng3ce04b62015-10-26 23:30:5573#include "content/renderer/render_view_impl.h"
tfarina556a7232014-10-05 01:02:0974#include "content/renderer/renderer_blink_platform_impl.h"
[email protected]5b45ad42013-10-25 00:42:0475#include "content/renderer/resizing_mode_selector.h"
danakj4b347212018-07-04 17:55:1776#include "gpu/command_buffer/service/gpu_switches.h"
ekaramad330ba4232016-09-23 17:57:4777#include "ipc/ipc_message_start.h"
[email protected]484955942010-08-19 16:13:1878#include "ipc/ipc_sync_message.h"
Antoine Laboure55c9ef82017-11-10 18:51:2379#include "ipc/ipc_sync_message_filter.h"
danakj4b347212018-07-04 17:55:1780#include "media/base/media_switches.h"
Scott Violet02e38b92018-03-27 23:42:1481#include "ppapi/buildflags/buildflags.h"
[email protected]661eb9d2009-02-03 02:11:4882#include "skia/ext/platform_canvas.h"
Blink Reformata30d4232018-04-07 15:31:0683#include "third_party/blink/public/platform/file_path_conversion.h"
Hajime Hoshi315a61f2018-08-14 17:27:2884#include "third_party/blink/public/platform/platform.h"
Blink Reformata30d4232018-04-07 15:31:0685#include "third_party/blink/public/platform/scheduler/web_render_widget_scheduling_state.h"
Yuta Kitamuradeb91bb2018-05-29 05:53:2086#include "third_party/blink/public/platform/scheduler/web_thread_scheduler.h"
Blink Reformata30d4232018-04-07 15:31:0687#include "third_party/blink/public/platform/web_cursor_info.h"
88#include "third_party/blink/public/platform/web_drag_data.h"
89#include "third_party/blink/public/platform/web_drag_operation.h"
90#include "third_party/blink/public/platform/web_mouse_event.h"
91#include "third_party/blink/public/platform/web_point.h"
92#include "third_party/blink/public/platform/web_rect.h"
93#include "third_party/blink/public/platform/web_runtime_features.h"
94#include "third_party/blink/public/platform/web_size.h"
95#include "third_party/blink/public/platform/web_string.h"
96#include "third_party/blink/public/web/web_autofill_client.h"
97#include "third_party/blink/public/web/web_device_emulation_params.h"
98#include "third_party/blink/public/web/web_frame_widget.h"
99#include "third_party/blink/public/web/web_input_method_controller.h"
100#include "third_party/blink/public/web/web_local_frame.h"
101#include "third_party/blink/public/web/web_node.h"
102#include "third_party/blink/public/web/web_page_popup.h"
103#include "third_party/blink/public/web/web_popup_menu_info.h"
104#include "third_party/blink/public/web/web_range.h"
105#include "third_party/blink/public/web/web_settings.h"
106#include "third_party/blink/public/web/web_view.h"
107#include "third_party/blink/public/web/web_widget.h"
[email protected]d353541f2012-05-03 22:45:41108#include "third_party/skia/include/core/SkShader.h"
paulmeyer90f6c31d2016-11-12 00:17:59109#include "ui/base/clipboard/clipboard.h"
Scott Violet8ff9c302018-02-22 22:28:35110#include "ui/base/ui_base_features.h"
danakj4b347212018-07-04 17:55:17111#include "ui/base/ui_base_switches.h"
dtapuska97286c882017-02-24 23:14:43112#include "ui/events/base_event_utils.h"
tfarina655f81d2014-12-23 02:38:50113#include "ui/gfx/geometry/point_conversions.h"
tfarina3b0452d2014-12-31 15:20:09114#include "ui/gfx/geometry/rect_conversions.h"
tfarinaebe974f02015-01-03 04:25:32115#include "ui/gfx/geometry/size_conversions.h"
[email protected]1835b9e2012-02-28 13:12:48116#include "ui/gfx/skia_util.h"
danakj4b347212018-07-04 17:55:17117#include "ui/gfx/switches.h"
[email protected]c9e2cbbb2012-05-12 21:17:27118#include "ui/gl/gl_switches.h"
danakj4b347212018-07-04 17:55:17119#include "ui/native_theme/native_theme_features.h"
120#include "ui/native_theme/overlay_scrollbar_constants_aura.h"
[email protected]d353541f2012-05-03 22:45:41121#include "ui/surface/transport_dib.h"
[email protected]661eb9d2009-02-03 02:11:48122
[email protected]eeb93112013-05-01 19:41:10123#if defined(OS_ANDROID)
[email protected]cefe9b152014-03-27 18:16:15124#include <android/keycodes.h>
wjmaclean1d970622017-01-21 22:28:24125#include "base/time/time.h"
[email protected]eeb93112013-05-01 19:41:10126#endif
127
[email protected]661eb9d2009-02-03 02:11:48128#if defined(OS_POSIX)
[email protected]d5282e72009-05-13 13:16:52129#include "third_party/skia/include/core/SkMallocPixelRef.h"
[email protected]d353541f2012-05-03 22:45:41130#include "third_party/skia/include/core/SkPixelRef.h"
[email protected]661eb9d2009-02-03 02:11:48131#endif // defined(OS_POSIX)
[email protected]8085dbc82008-09-26 22:53:44132
sadrul943e3b32016-08-04 18:22:59133#if defined(USE_AURA)
sadrul602ce1362017-01-26 06:41:10134#include "content/renderer/mus/renderer_window_tree_client.h"
penghuang28a5fa22015-12-02 17:58:19135#endif
136
ekaramad330ba4232016-09-23 17:57:47137#if defined(OS_MACOSX)
138#include "content/renderer/text_input_client_observer.h"
139#endif
140
Ryan Landay9e42fd742017-08-12 01:59:11141using blink::WebImeTextSpan;
[email protected]180ef242013-11-07 06:50:46142using blink::WebCursorInfo;
[email protected]19193682014-04-03 15:01:43143using blink::WebDeviceEmulationParams;
paulmeyer90f6c31d2016-11-12 00:17:59144using blink::WebDragOperation;
paulmeyer6ef5a792016-11-08 20:33:58145using blink::WebDragOperationsMask;
146using blink::WebDragData;
paulmeyer90f6c31d2016-11-12 00:17:59147using blink::WebFrameWidget;
[email protected]180ef242013-11-07 06:50:46148using blink::WebGestureEvent;
149using blink::WebInputEvent;
dtapuska5d2e9c32015-12-03 16:39:49150using blink::WebInputEventResult;
ekaramad2daaf672016-11-10 20:29:01151using blink::WebInputMethodController;
paulmeyer6ef5a792016-11-08 20:33:58152using blink::WebLocalFrame;
[email protected]180ef242013-11-07 06:50:46153using blink::WebMouseEvent;
154using blink::WebMouseWheelEvent;
155using blink::WebNavigationPolicy;
donnda070f3c2015-01-16 19:54:11156using blink::WebNode;
[email protected]180ef242013-11-07 06:50:46157using blink::WebPagePopup;
dglazkove353a372016-09-01 01:33:48158using blink::WebRange;
[email protected]180ef242013-11-07 06:50:46159using blink::WebRect;
[email protected]180ef242013-11-07 06:50:46160using blink::WebSize;
paulmeyer90f6c31d2016-11-12 00:17:59161using blink::WebString;
[email protected]180ef242013-11-07 06:50:46162using blink::WebTextDirection;
163using blink::WebTouchEvent;
[email protected]f8ed4722013-12-03 03:27:25164using blink::WebTouchPoint;
[email protected]180ef242013-11-07 06:50:46165using blink::WebVector;
166using blink::WebWidget;
[email protected]e9ff79c2012-10-19 21:31:26167
paulmeyer90f6c31d2016-11-12 00:17:59168namespace content {
169
[email protected]6a4d7f62013-01-07 21:32:13170namespace {
[email protected]b256eca2013-07-11 10:57:40171
Albert J. Wongcb004632018-07-10 22:58:25172using RoutingIDWidgetMap = std::map<int32_t, RenderWidget*>;
173base::LazyInstance<RoutingIDWidgetMap>::Leaky g_routing_id_widget_map =
174 LAZY_INSTANCE_INITIALIZER;
175
danakj4b347212018-07-04 17:55:17176const base::Feature kUnpremultiplyAndDitherLowBitDepthTiles = {
177 "UnpremultiplyAndDitherLowBitDepthTiles", base::FEATURE_ENABLED_BY_DEFAULT};
178
[email protected]b256eca2013-07-11 10:57:40179typedef std::map<std::string, ui::TextInputMode> TextInputModeMap;
180
AJITH KUMAR V041c0b02017-08-08 10:39:20181static const int kInvalidNextPreviousFlagsValue = -1;
yiyix6b90ef52018-10-09 19:55:30182static const char* kOOPIF = "OOPIF";
183static const char* kRenderer = "Renderer";
AJITH KUMAR V041c0b02017-08-08 10:39:20184
lfgbee1e0a2016-06-08 21:24:21185class WebWidgetLockTarget : public content::MouseLockDispatcher::LockTarget {
186 public:
187 explicit WebWidgetLockTarget(blink::WebWidget* webwidget)
188 : webwidget_(webwidget) {}
189
190 void OnLockMouseACK(bool succeeded) override {
191 if (succeeded)
Blink Reformat1c4d759e2017-04-09 16:34:54192 webwidget_->DidAcquirePointerLock();
lfgbee1e0a2016-06-08 21:24:21193 else
Blink Reformat1c4d759e2017-04-09 16:34:54194 webwidget_->DidNotAcquirePointerLock();
lfgbee1e0a2016-06-08 21:24:21195 }
196
Blink Reformat1c4d759e2017-04-09 16:34:54197 void OnMouseLockLost() override { webwidget_->DidLosePointerLock(); }
lfgbee1e0a2016-06-08 21:24:21198
199 bool HandleMouseLockedInputEvent(const blink::WebMouseEvent& event) override {
200 // The WebWidget handles mouse lock in Blink's handleInputEvent().
201 return false;
202 }
203
204 private:
205 blink::WebWidget* webwidget_;
206};
207
dglazkov97b6c2b2016-10-25 17:35:55208bool IsDateTimeInput(ui::TextInputType type) {
209 return type == ui::TEXT_INPUT_TYPE_DATE ||
210 type == ui::TEXT_INPUT_TYPE_DATE_TIME ||
211 type == ui::TEXT_INPUT_TYPE_DATE_TIME_LOCAL ||
212 type == ui::TEXT_INPUT_TYPE_MONTH ||
213 type == ui::TEXT_INPUT_TYPE_TIME || type == ui::TEXT_INPUT_TYPE_WEEK;
214}
215
paulmeyer90f6c31d2016-11-12 00:17:59216WebDragData DropMetaDataToWebDragData(
217 const std::vector<DropData::Metadata>& drop_meta_data) {
218 std::vector<WebDragData::Item> item_list;
219 for (const auto& meta_data_item : drop_meta_data) {
220 if (meta_data_item.kind == DropData::Kind::STRING) {
221 WebDragData::Item item;
Blink Reformat1c4d759e2017-04-09 16:34:54222 item.storage_type = WebDragData::Item::kStorageTypeString;
223 item.string_type = WebString::FromUTF16(meta_data_item.mime_type);
paulmeyer90f6c31d2016-11-12 00:17:59224 // Have to pass a dummy URL here instead of an empty URL because the
225 // DropData received by browser_plugins goes through a round trip:
226 // DropData::MetaData --> WebDragData-->DropData. In the end, DropData
227 // will contain an empty URL (which means no URL is dragged) if the URL in
228 // WebDragData is empty.
229 if (base::EqualsASCII(meta_data_item.mime_type,
230 ui::Clipboard::kMimeTypeURIList)) {
Blink Reformat1c4d759e2017-04-09 16:34:54231 item.string_data = WebString::FromUTF8("about:dragdrop-placeholder");
paulmeyer90f6c31d2016-11-12 00:17:59232 }
233 item_list.push_back(item);
234 continue;
235 }
236
237 // TODO(hush): crbug.com/584789. Blink needs to support creating a file with
238 // just the mimetype. This is needed to drag files to WebView on Android
239 // platform.
240 if ((meta_data_item.kind == DropData::Kind::FILENAME) &&
241 !meta_data_item.filename.empty()) {
242 WebDragData::Item item;
Blink Reformat1c4d759e2017-04-09 16:34:54243 item.storage_type = WebDragData::Item::kStorageTypeFilename;
244 item.filename_data = blink::FilePathToWebString(meta_data_item.filename);
paulmeyer90f6c31d2016-11-12 00:17:59245 item_list.push_back(item);
246 continue;
247 }
248
249 if (meta_data_item.kind == DropData::Kind::FILESYSTEMFILE) {
250 WebDragData::Item item;
Blink Reformat1c4d759e2017-04-09 16:34:54251 item.storage_type = WebDragData::Item::kStorageTypeFileSystemFile;
252 item.file_system_url = meta_data_item.file_system_url;
paulmeyer90f6c31d2016-11-12 00:17:59253 item_list.push_back(item);
254 continue;
255 }
256 }
257
258 WebDragData result;
Blink Reformat1c4d759e2017-04-09 16:34:54259 result.Initialize();
260 result.SetItems(item_list);
paulmeyer90f6c31d2016-11-12 00:17:59261 return result;
262}
263
264WebDragData DropDataToWebDragData(const DropData& drop_data) {
265 std::vector<WebDragData::Item> item_list;
266
267 // These fields are currently unused when dragging into WebKit.
268 DCHECK(drop_data.download_metadata.empty());
269 DCHECK(drop_data.file_contents.empty());
dcheng3dd85612017-02-08 10:46:23270 DCHECK(drop_data.file_contents_content_disposition.empty());
paulmeyer90f6c31d2016-11-12 00:17:59271
272 if (!drop_data.text.is_null()) {
273 WebDragData::Item item;
Blink Reformat1c4d759e2017-04-09 16:34:54274 item.storage_type = WebDragData::Item::kStorageTypeString;
275 item.string_type = WebString::FromUTF8(ui::Clipboard::kMimeTypeText);
276 item.string_data = WebString::FromUTF16(drop_data.text.string());
paulmeyer90f6c31d2016-11-12 00:17:59277 item_list.push_back(item);
278 }
279
280 if (!drop_data.url.is_empty()) {
281 WebDragData::Item item;
Blink Reformat1c4d759e2017-04-09 16:34:54282 item.storage_type = WebDragData::Item::kStorageTypeString;
283 item.string_type = WebString::FromUTF8(ui::Clipboard::kMimeTypeURIList);
284 item.string_data = WebString::FromUTF8(drop_data.url.spec());
285 item.title = WebString::FromUTF16(drop_data.url_title);
paulmeyer90f6c31d2016-11-12 00:17:59286 item_list.push_back(item);
287 }
288
289 if (!drop_data.html.is_null()) {
290 WebDragData::Item item;
Blink Reformat1c4d759e2017-04-09 16:34:54291 item.storage_type = WebDragData::Item::kStorageTypeString;
292 item.string_type = WebString::FromUTF8(ui::Clipboard::kMimeTypeHTML);
293 item.string_data = WebString::FromUTF16(drop_data.html.string());
294 item.base_url = drop_data.html_base_url;
paulmeyer90f6c31d2016-11-12 00:17:59295 item_list.push_back(item);
296 }
297
jdoerrie5a73d0fa2018-10-02 23:54:05298 for (auto it = drop_data.filenames.begin(); it != drop_data.filenames.end();
paulmeyer90f6c31d2016-11-12 00:17:59299 ++it) {
300 WebDragData::Item item;
Blink Reformat1c4d759e2017-04-09 16:34:54301 item.storage_type = WebDragData::Item::kStorageTypeFilename;
302 item.filename_data = blink::FilePathToWebString(it->path);
303 item.display_name_data =
kinukoa4293022017-01-27 06:43:25304 blink::FilePathToWebString(base::FilePath(it->display_name));
paulmeyer90f6c31d2016-11-12 00:17:59305 item_list.push_back(item);
306 }
307
jdoerrie5a73d0fa2018-10-02 23:54:05308 for (auto it = drop_data.file_system_files.begin();
309 it != drop_data.file_system_files.end(); ++it) {
paulmeyer90f6c31d2016-11-12 00:17:59310 WebDragData::Item item;
Blink Reformat1c4d759e2017-04-09 16:34:54311 item.storage_type = WebDragData::Item::kStorageTypeFileSystemFile;
312 item.file_system_url = it->url;
313 item.file_system_file_size = it->size;
314 item.file_system_id = blink::WebString::FromASCII(it->filesystem_id);
paulmeyer90f6c31d2016-11-12 00:17:59315 item_list.push_back(item);
316 }
317
Stuart Langley48300392017-11-02 03:54:44318 for (const auto& it : drop_data.custom_data) {
paulmeyer90f6c31d2016-11-12 00:17:59319 WebDragData::Item item;
Blink Reformat1c4d759e2017-04-09 16:34:54320 item.storage_type = WebDragData::Item::kStorageTypeString;
Stuart Langley48300392017-11-02 03:54:44321 item.string_type = WebString::FromUTF16(it.first);
322 item.string_data = WebString::FromUTF16(it.second);
paulmeyer90f6c31d2016-11-12 00:17:59323 item_list.push_back(item);
324 }
325
326 WebDragData result;
Blink Reformat1c4d759e2017-04-09 16:34:54327 result.Initialize();
328 result.SetItems(item_list);
329 result.SetFilesystemId(WebString::FromUTF16(drop_data.filesystem_id));
paulmeyer90f6c31d2016-11-12 00:17:59330 return result;
331}
332
dtapuskac4dd5be2016-10-25 15:11:10333ui::TextInputType ConvertWebTextInputType(blink::WebTextInputType type) {
334 // Check the type is in the range representable by ui::TextInputType.
335 DCHECK_LE(type, static_cast<int>(ui::TEXT_INPUT_TYPE_MAX))
336 << "blink::WebTextInputType and ui::TextInputType not synchronized";
337 return static_cast<ui::TextInputType>(type);
338}
339
340ui::TextInputMode ConvertWebTextInputMode(blink::WebTextInputMode mode) {
341 // Check the mode is in the range representable by ui::TextInputMode.
342 DCHECK_LE(mode, static_cast<int>(ui::TEXT_INPUT_MODE_MAX))
343 << "blink::WebTextInputMode and ui::TextInputMode not synchronized";
344 return static_cast<ui::TextInputMode>(mode);
345}
346
Mitsuru Oshima3c07b332018-02-06 04:56:50347// Returns true if the device scale is high enough that losing subpixel
348// antialiasing won't have a noticeable effect on text quality.
349static bool DeviceScaleEnsuresTextQuality(float device_scale_factor) {
350#if defined(OS_ANDROID) || defined(OS_CHROMEOS)
351 // On Android, we never have subpixel antialiasing. On Chrome OS we prefer to
352 // composite all scrollers so that we get animated overlay scrollbars.
353 return true;
354#else
355 // 1.5 is a common touchscreen tablet device scale factor. For such
356 // devices main thread antialiasing is a heavy burden.
357 return device_scale_factor >= 1.5f;
358#endif
359}
360
361static bool PreferCompositingToLCDText(CompositorDependencies* compositor_deps,
362 float device_scale_factor) {
363 const base::CommandLine& command_line =
364 *base::CommandLine::ForCurrentProcess();
365 if (command_line.HasSwitch(switches::kDisablePreferCompositingToLCDText))
366 return false;
367 if (command_line.HasSwitch(switches::kEnablePreferCompositingToLCDText))
368 return true;
369 if (!compositor_deps->IsLcdTextEnabled())
370 return true;
371 return DeviceScaleEnsuresTextQuality(device_scale_factor);
372}
373
[email protected]b256eca2013-07-11 10:57:40374} // namespace
375
[email protected]b2e4c70132013-10-03 02:07:51376// RenderWidget ---------------------------------------------------------------
377
danakj829cdd142018-09-14 21:13:27378RenderWidget::RenderWidget(int32_t widget_routing_id,
379 CompositorDependencies* compositor_deps,
danakj829cdd142018-09-14 21:13:27380 const ScreenInfo& screen_info,
381 blink::WebDisplayMode display_mode,
danakj6dcbc5962018-11-16 16:45:42382 bool is_frozen,
danakj829cdd142018-09-14 21:13:27383 bool hidden,
384 bool never_visible,
385 mojom::WidgetRequest widget_request)
nick8331f8ad2016-11-15 20:42:45386 : routing_id_(widget_routing_id),
dcheng35d31c112015-07-22 00:17:36387 compositor_deps_(compositor_deps),
lfg8ff33912016-09-13 20:59:21388 webwidget_internal_(nullptr),
[email protected]847a2582013-03-09 02:29:51389 auto_resize_mode_(false),
[email protected]1ac10dca2013-08-20 20:47:04390 is_hidden_(hidden),
sievers71c62dd52015-10-07 01:44:39391 compositor_never_visible_(never_visible),
mikhail.pozdnyakovf2c902a2015-04-14 08:09:12392 is_fullscreen_granted_(false),
danakj73dd3032018-07-28 17:49:53393 display_mode_(display_mode),
changwanf2a707b2015-10-30 08:22:16394 ime_event_guard_(nullptr),
[email protected]661eb9d2009-02-03 02:11:48395 closing_(false),
danakj5c5269b2018-11-06 00:39:11396 host_will_close_this_(false),
danakj6dcbc5962018-11-16 16:45:42397 is_frozen_(is_frozen),
dglazkov97b6c2b2016-10-25 17:35:55398 text_input_type_(ui::TEXT_INPUT_TYPE_NONE),
[email protected]b256eca2013-07-11 10:57:40399 text_input_mode_(ui::TEXT_INPUT_MODE_DEFAULT),
shuchen82ce8c52014-10-23 01:55:20400 text_input_flags_(0),
AJITH KUMAR V041c0b02017-08-08 10:39:20401 next_previous_flags_(kInvalidNextPreviousFlagsValue),
[email protected]86ba5fcb2013-09-04 00:36:53402 can_compose_inline_(true),
nonafa291792016-08-10 02:36:18403 composition_range_(gfx::Range::InvalidRange()),
[email protected]867125a02009-12-10 06:01:48404 pending_window_rect_count_(0),
Scott Violetae08f332018-07-27 17:37:23405 screen_info_(screen_info),
nonafa291792016-08-10 02:36:18406 monitor_composition_info_(false),
[email protected]b2e4c70132013-10-03 02:07:51407 popup_origin_scale_for_emulation_(0.f),
samans26510e442017-06-01 22:29:12408 frame_swap_message_queue_(new FrameSwapMessageQueue(routing_id_)),
[email protected]a09d53ce2014-01-31 00:46:42409 resizing_mode_selector_(new ResizingModeSelector()),
lfge0c2792ec2016-05-11 18:52:08410 has_host_context_menu_location_(false),
ekaramad2a46d632016-07-19 13:33:09411 has_focus_(false),
danakj6dcbc5962018-11-16 16:45:42412 for_child_local_root_frame_(false),
ekaramad330ba4232016-09-23 17:57:47413#if defined(OS_MACOSX)
414 text_input_client_observer_(new TextInputClientObserver(this)),
415#endif
Takashi SAKAMOTO870f6262017-08-22 04:08:27416 first_update_visual_state_after_hidden_(false),
tasakb95dbb50c2017-02-08 18:07:50417 was_shown_time_(base::TimeTicks::Now()),
kenrb5d78b842017-03-06 21:06:01418 current_content_source_id_(0),
Dave Tapuska1bdf1832017-07-07 18:07:19419 widget_binding_(this, std::move(widget_request)),
danakjf87460792018-12-04 20:14:22420 warmup_weak_ptr_factory_(this),
wjmaclean1d970622017-01-21 22:28:24421 weak_ptr_factory_(this) {
nick8331f8ad2016-11-15 20:42:45422 DCHECK_NE(routing_id_, MSG_ROUTING_NONE);
[email protected]380244092011-10-07 17:26:27423 DCHECK(RenderThread::Get());
alexclarke7fa93942015-10-21 15:37:11424
425 // In tests there may not be a RenderThreadImpl.
426 if (RenderThreadImpl::current()) {
427 render_widget_scheduling_state_ = RenderThreadImpl::current()
Yuta Kitamura3331f5c2018-04-05 11:12:25428 ->GetWebMainThreadScheduler()
dcheng07945f632015-12-26 07:59:32429 ->NewRenderWidgetSchedulingState();
alexclarke7fa93942015-10-21 15:37:11430 render_widget_scheduling_state_->SetHidden(is_hidden_);
431 }
sadrul602ce1362017-01-26 06:41:10432#if defined(USE_AURA)
fsamuel098eade2017-03-21 18:06:14433 RendererWindowTreeClient::CreateIfNecessary(routing_id_);
Scott Violete46b3352018-09-22 00:37:07434 if (features::IsMultiProcessMash())
Scott Violet1098538e2017-10-05 19:23:33435 RendererWindowTreeClient::Get(routing_id_)->SetVisible(!is_hidden_);
sadrul602ce1362017-01-26 06:41:10436#endif
Albert J. Wongcb004632018-07-10 22:58:25437
438 if (routing_id_ != MSG_ROUTING_NONE)
439 g_routing_id_widget_map.Get().emplace(routing_id_, this);
initial.commit09911bf2008-07-26 23:55:29440}
441
442RenderWidget::~RenderWidget() {
lfg8ff33912016-09-13 20:59:21443 DCHECK(!webwidget_internal_) << "Leaking our WebWidget!";
Albert J. Wong7bbf22d2018-12-20 00:27:27444
Albert J. Wongcb004632018-07-10 22:58:25445 // TODO(ajwong): Add in check that routing_id_ has been removed from
446 // g_routing_id_widget_map once the shutdown semantics for RenderWidget
447 // and RenderViewImpl are rationalized. Currently, too many unit and
448 // browser tests delete a RenderWidget without correclty going through
449 // the shutdown. https://crbug.com/545684
[email protected]bffc8302014-01-23 20:52:16450
Kinuko Yasudabef2b642019-01-24 04:53:33451 if (input_event_queue_)
452 input_event_queue_->ClearClient();
453
sadrul8185d972017-01-31 18:30:11454#if defined(USE_AURA)
fsamuel098eade2017-03-21 18:06:14455 // It is possible for a RenderWidget to be destroyed before it was embedded
456 // in a mus window. The RendererWindowTreeClient will leak in such cases. So
457 // explicitly delete it here.
458 RendererWindowTreeClient::Destroy(routing_id_);
sadrul8185d972017-01-31 18:30:11459#endif
initial.commit09911bf2008-07-26 23:55:29460}
461
[email protected]484955942010-08-19 16:13:18462// static
Albert J. Wongcb004632018-07-10 22:58:25463RenderWidget* RenderWidget::FromRoutingID(int32_t routing_id) {
464 RoutingIDWidgetMap* widgets = g_routing_id_widget_map.Pointer();
jdoerrie5a73d0fa2018-10-02 23:54:05465 auto it = widgets->find(routing_id);
Albert J. Wongcb004632018-07-10 22:58:25466 return it == widgets->end() ? NULL : it->second;
467}
468
danakj25ec6e2b2018-09-26 17:01:31469void RenderWidget::InitForPopup(ShowCallback show_callback,
470 blink::WebPagePopup* web_page_popup) {
471 // Init() increments the reference count on |this|, making it
472 // self-referencing.
473 Init(std::move(show_callback), web_page_popup);
initial.commit09911bf2008-07-26 23:55:29474}
475
danakj6a816592018-09-25 18:30:56476void RenderWidget::InitForChildLocalRoot(
477 blink::WebFrameWidget* web_frame_widget) {
danakj6dcbc5962018-11-16 16:45:42478 for_child_local_root_frame_ = true;
danakj6a816592018-09-25 18:30:56479 // Init() increments the reference count on |this|, making it
480 // self-referencing.
481 Init(base::NullCallback(), web_frame_widget);
dchengda9b4bb2015-07-20 20:58:08482}
483
dchengda9b4bb2015-07-20 20:58:08484void RenderWidget::CloseForFrame() {
dchengd96a27a2015-07-24 20:17:32485 OnClose();
kenrba7199832015-01-22 23:44:59486}
487
Scott Violetae08f332018-07-27 17:37:23488void RenderWidget::Init(ShowCallback show_callback, WebWidget* web_widget) {
lfg8ff33912016-09-13 20:59:21489 DCHECK(!webwidget_internal_);
nick4df698d82016-11-11 20:39:23490 DCHECK_NE(routing_id_, MSG_ROUTING_NONE);
initial.commit09911bf2008-07-26 23:55:29491
danakj17216f4d2018-10-16 22:57:32492 RenderThreadImpl* render_thread_impl = RenderThreadImpl::current();
493
Jeremy Roman04f27c372017-10-27 15:20:55494 input_handler_ = std::make_unique<RenderWidgetInputHandler>(this, this);
nick8331f8ad2016-11-15 20:42:45495
danakj17216f4d2018-10-16 22:57:32496 LayerTreeView* layer_tree_view = InitializeLayerTreeView();
497 web_widget->SetLayerTreeView(layer_tree_view);
Dave Tapuska9db80842017-07-24 17:24:26498
danakj31699d5b2018-09-18 15:39:17499 blink::scheduler::WebThreadScheduler* main_thread_scheduler = nullptr;
500 if (render_thread_impl)
501 main_thread_scheduler = render_thread_impl->GetWebMainThreadScheduler();
Alexander Timin41e917d2018-07-02 16:25:35502 blink::scheduler::WebThreadScheduler* compositor_thread_scheduler =
503 blink::scheduler::WebThreadScheduler::CompositorThreadScheduler();
danakj31699d5b2018-09-18 15:39:17504 scoped_refptr<base::SingleThreadTaskRunner> compositor_input_task_runner;
danakj89ae4532018-12-12 22:52:55505 // Use the compositor thread task runner unless this is a popup or other such
506 // non-frame widgets. The |compositor_thread_scheduler| can be null in tests
507 // without a compositor thread.
508 if (for_frame() && compositor_thread_scheduler) {
509 compositor_input_task_runner =
510 compositor_thread_scheduler->InputTaskRunner();
danakj31699d5b2018-09-18 15:39:17511 }
Alexander Timin41e917d2018-07-02 16:25:35512
Dave Tapuska04bc5ee92018-04-17 19:03:31513 widget_input_handler_manager_ = WidgetInputHandlerManager::Create(
danakj31699d5b2018-09-18 15:39:17514 weak_ptr_factory_.GetWeakPtr(), std::move(compositor_input_task_runner),
515 main_thread_scheduler);
Dave Tapuska9db80842017-07-24 17:24:26516
danakjdf1ceb72018-07-18 20:02:25517 show_callback_ = std::move(show_callback);
initial.commit09911bf2008-07-26 23:55:29518
lfg8ff33912016-09-13 20:59:21519 webwidget_internal_ = web_widget;
520 webwidget_mouse_lock_target_.reset(
521 new WebWidgetLockTarget(webwidget_internal_));
lfgbee1e0a2016-06-08 21:24:21522 mouse_lock_dispatcher_.reset(new RenderWidgetMouseLockDispatcher(this));
initial.commit09911bf2008-07-26 23:55:29523
nick4df698d82016-11-11 20:39:23524 RenderThread::Get()->AddRoute(routing_id_, this);
525 // Take a reference on behalf of the RenderThread. This will be balanced
Albert J. Wong3c93c182018-09-27 17:29:43526 // when we receive WidgetMsg_Close.
nick4df698d82016-11-11 20:39:23527 AddRef();
528 if (RenderThreadImpl::current()) {
529 RenderThreadImpl::current()->WidgetCreated();
530 if (is_hidden_)
531 RenderThreadImpl::current()->WidgetHidden();
initial.commit09911bf2008-07-26 23:55:29532 }
533}
534
danakj53802692018-07-25 21:46:44535void RenderWidget::ApplyEmulatedScreenMetricsForPopupWidget(
536 RenderWidget* origin_widget) {
537 RenderWidgetScreenMetricsEmulator* emulator =
538 origin_widget->screen_metrics_emulator_.get();
539 if (!emulator)
540 return;
[email protected]b2e4c70132013-10-03 02:07:51541 popup_origin_scale_for_emulation_ = emulator->scale();
[email protected]19193682014-04-03 15:01:43542 popup_view_origin_for_emulation_ = emulator->applied_widget_rect().origin();
Pavel Feldmancc099f72017-07-20 23:09:00543 popup_screen_origin_for_emulation_ =
Lei Zhang9b359d32017-11-28 00:57:02544 emulator->original_screen_rect().origin();
Fady Samuel1c1ad3692018-11-06 23:28:40545 UpdateSurfaceAndScreenInfo(local_surface_id_allocation_from_parent_,
546 compositor_viewport_pixel_size_,
547 emulator->original_screen_info());
[email protected]b2e4c70132013-10-03 02:07:51548}
549
[email protected]2d6836f42014-07-02 17:25:31550gfx::Rect RenderWidget::AdjustValidationMessageAnchor(const gfx::Rect& anchor) {
551 if (screen_metrics_emulator_)
552 return screen_metrics_emulator_->AdjustValidationMessageAnchor(anchor);
553 return anchor;
554}
555
thakis18e426412017-03-15 12:06:37556#if BUILDFLAG(USE_EXTERNAL_POPUP_MENU)
[email protected]b2e4c70132013-10-03 02:07:51557void RenderWidget::SetExternalPopupOriginAdjustmentsForEmulation(
Albert J. Wong7bbf22d2018-12-20 00:27:27558 ExternalPopupMenu* popup) {
559 if (screen_metrics_emulator_)
560 popup->SetOriginScaleForEmulation(screen_metrics_emulator_->scale());
[email protected]b2e4c70132013-10-03 02:07:51561}
[email protected]53907862014-03-25 15:42:40562#endif
[email protected]b2e4c70132013-10-03 02:07:51563
564void RenderWidget::OnShowHostContextMenu(ContextMenuParams* params) {
565 if (screen_metrics_emulator_)
566 screen_metrics_emulator_->OnShowContextMenu(params);
567}
568
[email protected]a95986a82010-12-24 06:19:28569bool RenderWidget::OnMessageReceived(const IPC::Message& message) {
ekaramad330ba4232016-09-23 17:57:47570#if defined(OS_MACOSX)
571 if (IPC_MESSAGE_CLASS(message) == TextInputClientMsgStart)
572 return text_input_client_observer_->OnMessageReceived(message);
573#endif
lfgbee1e0a2016-06-08 21:24:21574 if (mouse_lock_dispatcher_ &&
575 mouse_lock_dispatcher_->OnMessageReceived(message))
576 return true;
577
[email protected]a95986a82010-12-24 06:19:28578 bool handled = true;
579 IPC_BEGIN_MESSAGE_MAP(RenderWidget, message)
Albert J. Wong3c93c182018-09-27 17:29:43580 IPC_MESSAGE_HANDLER(WidgetMsg_ShowContextMenu, OnShowContextMenu)
581 IPC_MESSAGE_HANDLER(WidgetMsg_Close, OnClose)
582 IPC_MESSAGE_HANDLER(WidgetMsg_SynchronizeVisualProperties,
Fady Samuel799e72192018-04-25 21:16:57583 OnSynchronizeVisualProperties)
Albert J. Wong3c93c182018-09-27 17:29:43584 IPC_MESSAGE_HANDLER(WidgetMsg_EnableDeviceEmulation,
dgozman9260b0a12015-03-16 13:45:20585 OnEnableDeviceEmulation)
Albert J. Wong3c93c182018-09-27 17:29:43586 IPC_MESSAGE_HANDLER(WidgetMsg_DisableDeviceEmulation,
dgozman9260b0a12015-03-16 13:45:20587 OnDisableDeviceEmulation)
Albert J. Wong3c93c182018-09-27 17:29:43588 IPC_MESSAGE_HANDLER(WidgetMsg_WasHidden, OnWasHidden)
589 IPC_MESSAGE_HANDLER(WidgetMsg_WasShown, OnWasShown)
590 IPC_MESSAGE_HANDLER(WidgetMsg_SetActive, OnSetActive)
Albert J. Wong3c93c182018-09-27 17:29:43591 IPC_MESSAGE_HANDLER(WidgetMsg_SetTextDirection, OnSetTextDirection)
592 IPC_MESSAGE_HANDLER(WidgetMsg_SetBounds_ACK, OnRequestSetBoundsAck)
593 IPC_MESSAGE_HANDLER(WidgetMsg_UpdateScreenRects, OnUpdateScreenRects)
594 IPC_MESSAGE_HANDLER(WidgetMsg_ForceRedraw, OnForceRedraw)
595 IPC_MESSAGE_HANDLER(WidgetMsg_SetViewportIntersection,
kenrbea731792017-01-13 15:10:48596 OnSetViewportIntersection)
Albert J. Wong3c93c182018-09-27 17:29:43597 IPC_MESSAGE_HANDLER(WidgetMsg_SetIsInert, OnSetIsInert)
598 IPC_MESSAGE_HANDLER(WidgetMsg_SetInheritedEffectiveTouchAction,
sunxd540a9962018-05-24 22:51:06599 OnSetInheritedEffectiveTouchAction)
Albert J. Wong3c93c182018-09-27 17:29:43600 IPC_MESSAGE_HANDLER(WidgetMsg_UpdateRenderThrottlingStatus,
Ken Buchanan8a319fb2017-11-15 18:37:12601 OnUpdateRenderThrottlingStatus)
Albert J. Wong3c93c182018-09-27 17:29:43602 IPC_MESSAGE_HANDLER(WidgetMsg_WaitForNextFrameForTests,
lfg43e08e62016-02-03 18:51:37603 OnWaitNextFrameForTests)
paulmeyer90f6c31d2016-11-12 00:17:59604 IPC_MESSAGE_HANDLER(DragMsg_TargetDragEnter, OnDragTargetDragEnter)
605 IPC_MESSAGE_HANDLER(DragMsg_TargetDragOver, OnDragTargetDragOver)
606 IPC_MESSAGE_HANDLER(DragMsg_TargetDragLeave, OnDragTargetDragLeave)
607 IPC_MESSAGE_HANDLER(DragMsg_TargetDrop, OnDragTargetDrop)
paulmeyer8fc8ea92016-11-15 05:12:21608 IPC_MESSAGE_HANDLER(DragMsg_SourceEnded, OnDragSourceEnded)
609 IPC_MESSAGE_HANDLER(DragMsg_SourceSystemDragEnded,
610 OnDragSourceSystemDragEnded)
[email protected]a95986a82010-12-24 06:19:28611 IPC_MESSAGE_UNHANDLED(handled = false)
612 IPC_END_MESSAGE_MAP()
613 return handled;
614}
initial.commit09911bf2008-07-26 23:55:29615
616bool RenderWidget::Send(IPC::Message* message) {
Nasko Oskovf97e8b02017-07-25 02:45:42617 // Don't send any messages after the browser has told us to close, and filter
danakj6dcbc5962018-11-16 16:45:42618 // most outgoing messages when frozen.
619 if (closing_) {
620 delete message;
621 return false;
622 }
623 if (is_frozen_ && !SwappedOutMessages::CanSendWhileSwappedOut(message)) {
initial.commit09911bf2008-07-26 23:55:29624 delete message;
625 return false;
626 }
627
628 // If given a messsage without a routing ID, then assign our routing ID.
629 if (message->routing_id() == MSG_ROUTING_NONE)
630 message->set_routing_id(routing_id_);
631
[email protected]380244092011-10-07 17:26:27632 return RenderThread::Get()->Send(message);
[email protected]8085dbc82008-09-26 22:53:44633}
634
dtapuska014ed082016-11-11 21:58:48635void RenderWidget::SendOrCrash(IPC::Message* message) {
636 bool result = Send(message);
637 CHECK(closing_ || result) << "Failed to send message";
638}
639
ekaramad5aff1942017-01-06 01:26:35640bool RenderWidget::ShouldHandleImeEvents() const {
Albert J. Wong7bbf22d2018-12-20 00:27:27641 if (delegate())
danakj89ae4532018-12-12 22:52:55642 return has_focus_;
643 if (for_child_local_root_frame_) {
644 // TODO(ekaramad): We track page focus in all RenderViews on the page but
645 // the RenderWidgets corresponding to child local roots do not get the
646 // update. For now, this method returns true when the RenderWidget is for a
647 // child local frame, i.e., IME events will be processed regardless of page
648 // focus. We should revisit this after page focus for OOPIFs has been fully
649 // resolved (https://crbug.com/689777).
650 return true;
651 }
652 // Not a frame widget.
653 return false;
ekaramad5aff1942017-01-06 01:26:35654}
655
initial.commit09911bf2008-07-26 23:55:29656void RenderWidget::OnClose() {
dchengd96a27a2015-07-24 20:17:32657 DCHECK(content::RenderThread::Get());
658 if (closing_)
659 return;
danakj292b63e2018-11-19 20:47:06660 for (auto& observer : render_frames_)
661 observer.WidgetWillClose();
dchengd96a27a2015-07-24 20:17:32662 closing_ = true;
663
664 // Browser correspondence is no longer needed at this point.
665 if (routing_id_ != MSG_ROUTING_NONE) {
666 RenderThread::Get()->RemoveRoute(routing_id_);
Albert J. Wongcb004632018-07-10 22:58:25667 g_routing_id_widget_map.Get().erase(routing_id_);
danakj678f0252018-11-09 21:46:34668 if (RenderThreadImpl::current()) {
669 // RenderWidgets may be hidden when they are closed. If we were previously
670 // hidden, we are being counted as such in RenderThreadImpl. Thus we
671 // remove that count here by calling WidgetRestored() even though we're
672 // clearly not becoming visible here.
673 if (is_hidden_)
674 RenderThreadImpl::current()->WidgetRestored();
dchengd96a27a2015-07-24 20:17:32675 RenderThreadImpl::current()->WidgetDestroyed();
danakj678f0252018-11-09 21:46:34676 }
dchengd96a27a2015-07-24 20:17:32677 }
678
danakj6dcbc5962018-11-16 16:45:42679 if (for_child_local_root_frame_) {
dchengd96a27a2015-07-24 20:17:32680 // Widgets for frames may be created and closed at any time while the frame
Ken Buchananed449bb2018-02-01 21:02:05681 // is alive. However, WebWidget must be closed synchronously because frame
682 // widgets and frames hold pointers to each other. The deferred call to
683 // Close() will complete cleanup and release |this|, but CloseWebWidget()
684 // prevents Close() from attempting to access members of an
685 // already-deleted frame.
686 CloseWebWidget();
dchengd96a27a2015-07-24 20:17:32687 }
Ken Buchananed449bb2018-02-01 21:02:05688 // If there is a Send call on the stack, then it could be dangerous to close
689 // now. Post a task that only gets invoked when there are no nested message
690 // loops.
Hajime Hoshi315a61f2018-08-14 17:27:28691 GetCleanupTaskRunner()->PostNonNestableTask(
692 FROM_HERE, base::BindOnce(&RenderWidget::Close, this));
dchengd96a27a2015-07-24 20:17:32693
694 // Balances the AddRef taken when we called AddRoute.
695 Release();
initial.commit09911bf2008-07-26 23:55:29696}
697
Fady Samuel799e72192018-04-25 21:16:57698void RenderWidget::OnSynchronizeVisualProperties(
danakja2c9d0a92018-07-25 20:01:18699 const VisualProperties& original_params) {
700 TRACE_EVENT0("renderer", "RenderWidget::OnSynchronizeVisualProperties");
EhsanKbd2cea992017-11-23 18:49:08701
danakja2c9d0a92018-07-25 20:01:18702 VisualProperties params = original_params;
Albert J. Wong7bbf22d2018-12-20 00:27:27703 if (delegate()) {
danakj84bbf552019-01-21 21:33:31704 if (size_ != params.new_size) {
705 // Only hide popups when the size changes. Eg https://crbug.com/761908.
706 delegate()->CancelPagePopupForWidget();
707 }
danakja2c9d0a92018-07-25 20:01:18708
709 if (display_mode_ != params.display_mode) {
710 display_mode_ = params.display_mode;
Albert J. Wong7bbf22d2018-12-20 00:27:27711 delegate()->ApplyNewDisplayModeForWidget(params.display_mode);
danakja2c9d0a92018-07-25 20:01:18712 }
713
714 bool auto_resize_mode_changed =
715 auto_resize_mode_ != params.auto_resize_enabled;
716 auto_resize_mode_ = params.auto_resize_enabled;
717 min_size_for_auto_resize_ = params.min_size_for_auto_resize;
718 max_size_for_auto_resize_ = params.max_size_for_auto_resize;
719
720 if (auto_resize_mode_) {
721 gfx::Size min_auto_size = min_size_for_auto_resize_;
722 gfx::Size max_auto_size = max_size_for_auto_resize_;
723 if (compositor_deps_->IsUseZoomForDSFEnabled()) {
724 min_auto_size = gfx::ScaleToCeiledSize(
725 min_auto_size, params.screen_info.device_scale_factor);
726 max_auto_size = gfx::ScaleToCeiledSize(
727 max_auto_size, params.screen_info.device_scale_factor);
728 }
Albert J. Wong7bbf22d2018-12-20 00:27:27729 delegate()->ApplyAutoResizeLimitsForWidget(min_auto_size, max_auto_size);
danakja2c9d0a92018-07-25 20:01:18730 } else if (auto_resize_mode_changed) {
Albert J. Wong7bbf22d2018-12-20 00:27:27731 delegate()->DisableAutoResizeForWidget();
danakja2c9d0a92018-07-25 20:01:18732 if (params.new_size.IsEmpty())
733 return;
734 }
735
736 browser_controls_shrink_blink_size_ =
737 params.browser_controls_shrink_blink_size;
738 top_controls_height_ = params.top_controls_height;
739 bottom_controls_height_ = params.bottom_controls_height;
740
741 // TODO(fsamuel): Modifies the |params| that are used for
742 // SynchronizeVisualProperties(), but does not used this modified
743 // device_scale_factor for the auto-resize values, which is probably a bug.
744 if (device_scale_factor_for_testing_) {
745 params.screen_info.device_scale_factor =
746 *device_scale_factor_for_testing_;
747 params.compositor_viewport_pixel_size = gfx::ScaleToCeiledSize(
748 params.new_size, params.screen_info.device_scale_factor);
749 }
[email protected]b2e4c70132013-10-03 02:07:51750 }
751
danakja2c9d0a92018-07-25 20:01:18752 if (!resizing_mode_selector_->ShouldAbortOnResize(this, params)) {
753 if (screen_metrics_emulator_) {
754 screen_metrics_emulator_->OnSynchronizeVisualProperties(params);
755 } else {
danakj0246bba02019-01-23 23:12:27756 if (!delegate()) {
757 // The main frame controls the page scale factor, from blink. For other
758 // frame widgets, the page scale is received from its parent as part of
759 // the visual properties here. While blink doesn't need to know this
760 // page scale factor outside the main frame, the compositor does in
761 // order to produce its output at the correct scale.
762 layer_tree_view_->SetExternalPageScaleFactor(params.page_scale_factor);
763 // Store the value to give to any new RenderFrameProxy that is
764 // registered.
765 page_scale_factor_from_mainframe_ = params.page_scale_factor;
766 // Push the page scale factor down to any child RenderWidgets via our
767 // child proxy frames.
768 // TODO(danakj): This ends up setting the page scale factor in the
769 // RenderWidgetHost of the child RenderWidget, so that it can bounce
770 // the value down to its RenderWidget. Since this is essentially a
771 // global value per-page, we could instead store it once in the browser
772 // (such as in RenderViewHost) and distribute it to each frame-hosted
773 // RenderWidget from there.
774 for (auto& child_proxy : render_frame_proxies_)
775 child_proxy.OnPageScaleFactorChanged(params.page_scale_factor);
776 }
777
danakja2c9d0a92018-07-25 20:01:18778 gfx::Size old_visible_viewport_size = visible_viewport_size_;
779 SynchronizeVisualProperties(params);
780 if (old_visible_viewport_size != visible_viewport_size_) {
781 for (auto& render_frame : render_frames_)
782 render_frame.DidChangeVisibleViewport();
783 }
784 }
Fady Samuel799e72192018-04-25 21:16:57785 }
786
Albert J. Wong7bbf22d2018-12-20 00:27:27787 if (delegate() && params.scroll_focused_node_into_view)
788 delegate()->ScrollFocusedNodeIntoViewForWidget();
initial.commit09911bf2008-07-26 23:55:29789}
790
dgozman9260b0a12015-03-16 13:45:20791void RenderWidget::OnEnableDeviceEmulation(
Lei Zhang9b359d32017-11-28 00:57:02792 const blink::WebDeviceEmulationParams& params) {
mfomitchev2600fd7c2016-02-17 20:53:39793 if (!screen_metrics_emulator_) {
Fady Samuel4255c182018-05-24 20:42:36794 VisualProperties visual_properties;
795 visual_properties.screen_info = screen_info_;
796 visual_properties.new_size = size_;
797 visual_properties.compositor_viewport_pixel_size =
Christopher Cameronaad15a0d2018-03-09 03:47:33798 compositor_viewport_pixel_size_;
Fady Samuel84d8a1f72018-11-08 00:26:17799 visual_properties.local_surface_id_allocation =
800 local_surface_id_allocation_from_parent_;
Fady Samuel4255c182018-05-24 20:42:36801 visual_properties.visible_viewport_size = visible_viewport_size_;
802 visual_properties.is_fullscreen_granted = is_fullscreen_granted_;
803 visual_properties.display_mode = display_mode_;
mfomitchev2600fd7c2016-02-17 20:53:39804 screen_metrics_emulator_.reset(new RenderWidgetScreenMetricsEmulator(
Albert J. Wong3c93c182018-09-27 17:29:43805 this, params, visual_properties, widget_screen_rect_,
Fady Samuel4255c182018-05-24 20:42:36806 window_screen_rect_));
oshima50872a72016-03-04 13:26:18807 screen_metrics_emulator_->Apply();
mfomitchev2600fd7c2016-02-17 20:53:39808 } else {
dgozman9260b0a12015-03-16 13:45:20809 screen_metrics_emulator_->ChangeEmulationParams(params);
mfomitchev2600fd7c2016-02-17 20:53:39810 }
dgozman9260b0a12015-03-16 13:45:20811}
812
813void RenderWidget::OnDisableDeviceEmulation() {
814 screen_metrics_emulator_.reset();
815}
816
initial.commit09911bf2008-07-26 23:55:29817void RenderWidget::OnWasHidden() {
[email protected]9c3085f2011-06-09 02:10:31818 TRACE_EVENT0("renderer", "RenderWidget::OnWasHidden");
initial.commit09911bf2008-07-26 23:55:29819 // Go into a mode where we stop generating paint and scrolling events.
[email protected]bee16aab2009-08-26 15:55:03820 SetHidden(true);
ericwilligers88e69742016-10-17 19:29:55821 for (auto& observer : render_frames_)
822 observer.WasHidden();
initial.commit09911bf2008-07-26 23:55:29823}
824
Saman Samie61e5c032018-11-05 22:11:44825void RenderWidget::OnWasShown(base::TimeTicks show_request_timestamp,
826 bool was_evicted) {
[email protected]9e2e4632012-07-27 16:38:41827 TRACE_EVENT0("renderer", "RenderWidget::OnWasShown");
danakj57f139f2018-11-19 19:23:46828 // TODO(danakj): Nothing should happen ideally if the RenderWidget is frozen!
829 // It's not visible! However.. the RenderView needs to see it as visible in
830 // order to make the Page visible /o\ so this is hard. We need to detangle
831 // page visibility from the main widget. https://crbug.com/419087
danakj678f0252018-11-09 21:46:34832
tasakb95dbb50c2017-02-08 18:07:50833 was_shown_time_ = base::TimeTicks::Now();
initial.commit09911bf2008-07-26 23:55:29834 // See OnWasHidden
[email protected]bee16aab2009-08-26 15:55:03835 SetHidden(false);
ericwilligers88e69742016-10-17 19:29:55836 for (auto& observer : render_frames_)
837 observer.WasShown();
initial.commit09911bf2008-07-26 23:55:29838
Saman Samie61e5c032018-11-05 22:11:44839 if (was_evicted) {
840 for (auto& observer : render_frame_proxies_) {
841 observer.WasEvicted();
842 }
843 }
844
Kinuko Yasudabef2b642019-01-24 04:53:33845 if (layer_tree_view_ && !show_request_timestamp.is_null()) {
danakja6c10012018-07-06 14:25:36846 layer_tree_view_->layer_tree_host()->RequestPresentationTimeForNextFrame(
Sadrul Habib Chowdhury8bc43cc2018-06-06 01:48:03847 CreateTabSwitchingTimeRecorder(show_request_timestamp));
[email protected]3399dd822014-08-09 11:14:24848 }
initial.commit09911bf2008-07-26 23:55:29849}
850
Avi Drissman014dec72018-06-07 02:34:01851void RenderWidget::OnRequestSetBoundsAck() {
[email protected]53d3f302009-12-21 04:42:05852 DCHECK(pending_window_rect_count_);
853 pending_window_rect_count_--;
854}
855
Albert J. Wongcb004632018-07-10 22:58:25856void RenderWidget::OnForceRedraw(int snapshot_id) {
Kinuko Yasudabef2b642019-01-24 04:53:33857 if (LayerTreeView* ltv = layer_tree_view()) {
858 ltv->layer_tree_host()->RequestPresentationTimeForNextFrame(
859 base::BindOnce(&RenderWidget::DidPresentForceDrawFrame,
860 weak_ptr_factory_.GetWeakPtr(), snapshot_id));
861 ltv->SetNeedsForcedRedraw();
862 }
Albert J. Wongcb004632018-07-10 22:58:25863}
864
865void RenderWidget::DidPresentForceDrawFrame(
866 int snapshot_id,
867 const gfx::PresentationFeedback& feedback) {
Albert J. Wong3c93c182018-09-27 17:29:43868 Send(new WidgetHostMsg_ForceRedrawComplete(routing_id(), snapshot_id));
Albert J. Wongcb004632018-07-10 22:58:25869}
870
Xianzhu Wangf02017ac2018-10-17 01:47:33871viz::FrameSinkId RenderWidget::GetFrameSinkIdAtPoint(const gfx::PointF& point,
Ken Buchanan44d7e2f2018-08-23 14:18:05872 gfx::PointF* local_point) {
873 return input_handler_->GetFrameSinkIdAtPoint(point, local_point);
Navid Zolghadr0d86e5f2017-10-23 18:09:22874}
875
Dave Tapuskaea83d3bd2017-06-13 16:14:55876void RenderWidget::HandleInputEvent(
dtapuska9ec1a912017-04-21 15:18:31877 const blink::WebCoalescedInputEvent& input_event,
878 const ui::LatencyInfo& latency_info,
Dave Tapuskaea83d3bd2017-06-13 16:14:55879 HandledEventCallback callback) {
Albert J. Wong7bbf22d2018-12-20 00:27:27880 if (delegate() && is_frozen_) {
danakja2c9d0a92018-07-25 20:01:18881 std::move(callback).Run(INPUT_EVENT_ACK_STATE_NOT_CONSUMED, latency_info,
882 nullptr, base::nullopt);
883 return;
884 }
Dave Tapuskaea83d3bd2017-06-13 16:14:55885 input_handler_->HandleInputEvent(input_event, latency_info,
886 std::move(callback));
dtapuska9ec1a912017-04-21 15:18:31887}
888
dtapuska9d46ef7d2017-05-26 19:06:06889scoped_refptr<MainThreadEventQueue> RenderWidget::GetInputEventQueue() {
890 return input_event_queue_;
891}
892
fsamuel78f86e42016-01-20 04:10:23893void RenderWidget::OnCursorVisibilityChange(bool is_visible) {
lfg8ff33912016-09-13 20:59:21894 if (GetWebWidget())
Blink Reformat1c4d759e2017-04-09 16:34:54895 GetWebWidget()->SetCursorVisibilityState(is_visible);
fsamuel78f86e42016-01-20 04:10:23896}
897
898void RenderWidget::OnMouseCaptureLost() {
lfg8ff33912016-09-13 20:59:21899 if (GetWebWidget())
Blink Reformat1c4d759e2017-04-09 16:34:54900 GetWebWidget()->MouseCaptureLost();
fsamuel78f86e42016-01-20 04:10:23901}
902
alexmos56567492016-09-13 00:52:46903void RenderWidget::OnSetEditCommandsForNextKeyEvent(
904 const EditCommands& edit_commands) {
905 edit_commands_ = edit_commands;
906}
907
Albert J. Wongcb004632018-07-10 22:58:25908void RenderWidget::OnSetActive(bool active) {
Albert J. Wong7bbf22d2018-12-20 00:27:27909 if (delegate())
910 delegate()->SetActiveForWidget(active);
Albert J. Wongcb004632018-07-10 22:58:25911}
912
fsamuel78f86e42016-01-20 04:10:23913void RenderWidget::OnSetFocus(bool enable) {
Albert J. Wong7bbf22d2018-12-20 00:27:27914 if (delegate())
915 delegate()->DidReceiveSetFocusEventForWidget();
danakja2c9d0a92018-07-25 20:01:18916 SetFocus(enable);
917}
918
919void RenderWidget::SetFocus(bool enable) {
lfge0c2792ec2016-05-11 18:52:08920 has_focus_ = enable;
921
lfg8ff33912016-09-13 20:59:21922 if (GetWebWidget())
Blink Reformat1c4d759e2017-04-09 16:34:54923 GetWebWidget()->SetFocus(enable);
lfge0c2792ec2016-05-11 18:52:08924
ericwilligers88e69742016-10-17 19:29:55925 for (auto& observer : render_frames_)
926 observer.RenderWidgetSetFocus(enable);
danakja2c9d0a92018-07-25 20:01:18927
Albert J. Wong7bbf22d2018-12-20 00:27:27928 if (delegate())
929 delegate()->DidChangeFocusForWidget();
fsamuel78f86e42016-01-20 04:10:23930}
931
dtapuska3d5624d32016-08-30 04:34:00932void RenderWidget::SetNeedsMainFrame() {
danakj53f46b82018-12-11 20:55:55933 // The WebWidgetClient is not |this| if tests override it for the WebView and
934 // WebViewClient.
935 blink::WebWidgetClient* client =
Albert J. Wong7bbf22d2018-12-20 00:27:27936 delegate() ? delegate()->GetWebWidgetClientForWidget() : this;
danakj53f46b82018-12-11 20:55:55937 client->ScheduleAnimation();
dtapuska3d5624d32016-08-30 04:34:00938}
939
fsamuel78f86e42016-01-20 04:10:23940///////////////////////////////////////////////////////////////////////////////
danakja6c10012018-07-06 14:25:36941// LayerTreeViewDelegate
fsamuel78f86e42016-01-20 04:10:23942
David Bokanc8e38d02018-10-08 21:56:01943void RenderWidget::ApplyViewportChanges(
944 const cc::ApplyViewportChangesArgs& args) {
Ken Buchananed449bb2018-02-01 21:02:05945 if (!GetWebWidget())
946 return;
David Bokanc8e38d02018-10-08 21:56:01947 GetWebWidget()->ApplyViewportChanges(args);
fsamuel78f86e42016-01-20 04:10:23948}
949
sahel1b47fda72017-03-28 17:03:07950void RenderWidget::RecordWheelAndTouchScrollingCount(
951 bool has_scrolled_by_wheel,
952 bool has_scrolled_by_touch) {
Ken Buchananed449bb2018-02-01 21:02:05953 if (!GetWebWidget())
954 return;
Blink Reformat1c4d759e2017-04-09 16:34:54955 GetWebWidget()->RecordWheelAndTouchScrollingCount(has_scrolled_by_wheel,
sahel1b47fda72017-03-28 17:03:07956 has_scrolled_by_touch);
957}
958
Sahel Sharify676580c2019-01-10 21:49:49959void RenderWidget::SendOverscrollEventFromImplSide(
960 const gfx::Vector2dF& overscroll_delta,
961 cc::ElementId scroll_latched_element_id) {
962 if (!GetWebWidget())
963 return;
964 GetWebWidget()->SendOverscrollEventFromImplSide(overscroll_delta,
965 scroll_latched_element_id);
966}
967void RenderWidget::SendScrollEndEventFromImplSide(
968 cc::ElementId scroll_latched_element_id) {
969 if (!GetWebWidget())
970 return;
971 GetWebWidget()->SendScrollEndEventFromImplSide(scroll_latched_element_id);
972}
973
Daniel Cheng224569ee2018-04-25 05:45:06974void RenderWidget::BeginMainFrame(base::TimeTicks frame_time) {
Ken Buchananed449bb2018-02-01 21:02:05975 if (!GetWebWidget())
976 return;
Daniel Cheng224569ee2018-04-25 05:45:06977 if (input_event_queue_)
978 input_event_queue_->DispatchRafAlignedInput(frame_time);
dtapuska3d5624d32016-08-30 04:34:00979
Daniel Cheng224569ee2018-04-25 05:45:06980 GetWebWidget()->BeginFrame(frame_time);
fsamuel78f86e42016-01-20 04:10:23981}
982
danakjc7afae52017-06-20 21:12:41983void RenderWidget::RequestNewLayerTreeFrameSink(
danakja6c10012018-07-06 14:25:36984 LayerTreeFrameSinkCallback callback) {
[email protected]7912e822014-04-16 02:37:03985 // For widgets that are never visible, we don't start the compositor, so we
danakjc7afae52017-06-20 21:12:41986 // never get a request for a cc::LayerTreeFrameSink.
sievers71c62dd52015-10-07 01:44:39987 DCHECK(!compositor_never_visible_);
danakjf87460792018-12-04 20:14:22988 // Frozen RenderWidgets should not be doing any compositing.
Scott Violetb04f16c2018-11-27 00:48:46989 DCHECK(!is_frozen_);
danakj678f0252018-11-09 21:46:34990
991 if (is_closing()) {
992 // In this case, we drop the request which means the compositor waits
993 // forever, which is fine since we're going to destroy it.
994 return;
995 }
jonrossa2ff4f82018-02-16 17:27:46996
danakjf87460792018-12-04 20:14:22997 // If we have a warmup in progress, wait for that and store the callback
998 // to be run when the warmup completes.
999 if (warmup_frame_sink_request_pending_) {
1000 after_warmup_callback_ = std::move(callback);
1001 return;
1002 }
1003 // If a warmup previously completed, use the result.
1004 if (warmup_frame_sink_) {
1005 std::move(callback).Run(std::move(warmup_frame_sink_));
1006 return;
1007 }
1008
1009 DoRequestNewLayerTreeFrameSink(std::move(callback));
1010}
1011
1012void RenderWidget::DoRequestNewLayerTreeFrameSink(
1013 LayerTreeFrameSinkCallback callback) {
jonrossa2ff4f82018-02-16 17:27:461014 // TODO(jonross): have this generated by the LayerTreeFrameSink itself, which
1015 // would then handle binding.
1016 mojom::RenderFrameMetadataObserverPtr ptr;
1017 mojom::RenderFrameMetadataObserverRequest request = mojo::MakeRequest(&ptr);
1018 mojom::RenderFrameMetadataObserverClientPtrInfo client_info;
1019 mojom::RenderFrameMetadataObserverClientRequest client_request =
1020 mojo::MakeRequest(&client_info);
danakj4b347212018-07-04 17:55:171021 auto render_frame_metadata_observer =
1022 std::make_unique<RenderFrameMetadataObserverImpl>(std::move(request),
1023 std::move(client_info));
danakja6c10012018-07-06 14:25:361024 layer_tree_view_->SetRenderFrameObserver(
danakj4b347212018-07-04 17:55:171025 std::move(render_frame_metadata_observer));
danakjf51754372018-12-13 23:20:011026 GURL url = GetWebWidget()->GetURLForDebugTrace();
danakjefabf312018-10-05 19:03:071027 // The |url| is not always available, fallback to a fixed string.
1028 if (url.is_empty())
1029 url = GURL("chrome://gpu/RenderWidget::RequestNewLayerTreeFrameSink");
danakj6dcbc5962018-11-16 16:45:421030 // TODO(danakj): This may not be accurate, depending on the intent. A child
1031 // local root could be in the same process as the view, so if the client is
1032 // meant to designate the process type, it seems kRenderer would be the
1033 // correct choice. If client is meant to designate the widget type, then
1034 // kOOPIF would denote that it is not for the main frame. However, kRenderer
1035 // would also be used for other widgets such as popups.
1036 const char* client_name = for_child_local_root_frame_ ? kOOPIF : kRenderer;
Scott Violetb04f16c2018-11-27 00:48:461037 RenderThreadImpl::current()->RequestNewLayerTreeFrameSink(
1038 routing_id_, frame_swap_message_queue_, std::move(url),
1039 std::move(callback), std::move(client_request), std::move(ptr),
1040 client_name);
[email protected]ba91a792013-02-06 09:48:281041}
1042
fsamuel78f86e42016-01-20 04:10:231043void RenderWidget::DidCommitAndDrawCompositorFrame() {
1044 // NOTE: Tests may break if this event is renamed or moved. See
1045 // tab_capture_performancetest.cc.
1046 TRACE_EVENT0("gpu", "RenderWidget::DidCommitAndDrawCompositorFrame");
lfge0c2792ec2016-05-11 18:52:081047
ericwilligers88e69742016-10-17 19:29:551048 for (auto& observer : render_frames_)
1049 observer.DidCommitAndDrawCompositorFrame();
lfge0c2792ec2016-05-11 18:52:081050
fsamuel78f86e42016-01-20 04:10:231051 // Notify subclasses that we initiated the paint operation.
1052 DidInitiatePaint();
Yiming Zhou0f8836c22018-09-13 22:22:111053
Albert J. Wongfb64e142018-10-16 01:10:451054 Send(new WidgetHostMsg_DidCommitAndDrawCompositorFrame(routing_id_));
fsamuel78f86e42016-01-20 04:10:231055}
1056
Saman Sami766e0b02018-01-31 17:19:201057void RenderWidget::DidCommitCompositorFrame() {
Albert J. Wong7bbf22d2018-12-20 00:27:271058 if (delegate())
1059 delegate()->DidCommitCompositorFrameForWidget();
Saman Sami766e0b02018-01-31 17:19:201060}
fsamuel78f86e42016-01-20 04:10:231061
danakja2c9d0a92018-07-25 20:01:181062void RenderWidget::DidCompletePageScaleAnimation() {
Albert J. Wong7bbf22d2018-12-20 00:27:271063 if (delegate())
1064 delegate()->DidCompletePageScaleAnimationForWidget();
danakja2c9d0a92018-07-25 20:01:181065}
fsamuel78f86e42016-01-20 04:10:231066
danakj53f46b82018-12-11 20:55:551067void RenderWidget::ScheduleAnimation() {
1068 // This call is not needed in single thread mode for tests without a
1069 // scheduler, but they need to override the WebWidgetClient and replace this
1070 // method in order to schedule a synchronous composite task themselves.
1071 layer_tree_view_->SetNeedsBeginFrame();
initial.commit09911bf2008-07-26 23:55:291072}
1073
danakjefcb8432019-01-16 18:47:141074void RenderWidget::SetShowFPSCounter(bool show) {
1075 cc::LayerTreeHost* host = layer_tree_view_->layer_tree_host();
1076 cc::LayerTreeDebugState debug_state = host->GetDebugState();
1077 debug_state.show_fps_counter = show;
1078 host->SetDebugState(debug_state);
1079}
1080
danakje691e852019-01-16 22:49:041081void RenderWidget::SetShowPaintRects(bool show) {
1082 cc::LayerTreeHost* host = layer_tree_view_->layer_tree_host();
1083 cc::LayerTreeDebugState debug_state = host->GetDebugState();
1084 debug_state.show_paint_rects = show;
1085 host->SetDebugState(debug_state);
1086}
1087
1088void RenderWidget::SetShowDebugBorders(bool show) {
1089 cc::LayerTreeHost* host = layer_tree_view_->layer_tree_host();
1090 cc::LayerTreeDebugState debug_state = host->GetDebugState();
1091 if (show)
1092 debug_state.show_debug_borders.set();
1093 else
1094 debug_state.show_debug_borders.reset();
1095 host->SetDebugState(debug_state);
1096}
1097
1098void RenderWidget::SetShowScrollBottleneckRects(bool show) {
1099 cc::LayerTreeHost* host = layer_tree_view_->layer_tree_host();
1100 cc::LayerTreeDebugState debug_state = host->GetDebugState();
1101 debug_state.show_touch_event_handler_rects = show;
1102 debug_state.show_wheel_event_handler_rects = show;
1103 debug_state.show_non_fast_scrollable_rects = show;
1104 host->SetDebugState(debug_state);
1105}
1106
1107void RenderWidget::SetShowHitTestBorders(bool show) {
1108 cc::LayerTreeHost* host = layer_tree_view_->layer_tree_host();
1109 cc::LayerTreeDebugState debug_state = host->GetDebugState();
1110 debug_state.show_hit_test_borders = show;
1111 host->SetDebugState(debug_state);
1112}
1113
Stephen Chenneyec4312612018-11-21 12:40:161114void RenderWidget::UpdateVisualState(bool record_main_frame_metrics) {
Ken Buchananed449bb2018-02-01 21:02:051115 if (!GetWebWidget())
1116 return;
1117
Stephen Chenneyec4312612018-11-21 12:40:161118 // When recording main frame metrics set the lifecycle reason to
1119 // kBeginMainFrame, because this is the calller of UpdateLifecycle
1120 // for the main frame. Otherwise, set the reason to kTests, which is
1121 // the oinly other reason this method is called.
1122 WebWidget::LifecycleUpdateReason lifecycle_reason =
1123 record_main_frame_metrics
1124 ? WebWidget::LifecycleUpdateReason::kBeginMainFrame
1125 : WebWidget::LifecycleUpdateReason::kTest;
1126 GetWebWidget()->UpdateLifecycle(WebWidget::LifecycleUpdate::kAll,
1127 lifecycle_reason);
Blink Reformat1c4d759e2017-04-09 16:34:541128 GetWebWidget()->SetSuppressFrameRequestsWorkaroundFor704763Only(false);
tasakb95dbb50c2017-02-08 18:07:501129
Robert Flack3ef2ffb2018-09-06 20:38:201130 if (first_update_visual_state_after_hidden_) {
Takashi SAKAMOTO870f6262017-08-22 04:08:271131 RecordTimeToFirstActivePaint();
1132 first_update_visual_state_after_hidden_ = false;
1133 }
1134}
tasakb95dbb50c2017-02-08 18:07:501135
Takashi SAKAMOTO870f6262017-08-22 04:08:271136void RenderWidget::RecordTimeToFirstActivePaint() {
tasakb95dbb50c2017-02-08 18:07:501137 RenderThreadImpl* render_thread_impl = RenderThreadImpl::current();
tasakb95dbb50c2017-02-08 18:07:501138 base::TimeDelta sample = base::TimeTicks::Now() - was_shown_time_;
Takashi SAKAMOTO870f6262017-08-22 04:08:271139 if (render_thread_impl->NeedsToRecordFirstActivePaint(TTFAP_AFTER_PURGED)) {
1140 UMA_HISTOGRAM_TIMES("PurgeAndSuspend.Experimental.TimeToFirstActivePaint",
1141 sample);
1142 }
1143 if (render_thread_impl->NeedsToRecordFirstActivePaint(
1144 TTFAP_5MIN_AFTER_BACKGROUNDED)) {
1145 UMA_HISTOGRAM_TIMES(
1146 "PurgeAndSuspend.Experimental.TimeToFirstActivePaint."
1147 "AfterBackgrounded.5min",
1148 sample);
1149 }
fsamuel78f86e42016-01-20 04:10:231150}
1151
Stephen Chenney9879a5c2018-09-26 14:21:421152void RenderWidget::RecordEndOfFrameMetrics(base::TimeTicks frame_begin_time) {
1153 if (!GetWebWidget())
1154 return;
1155
1156 GetWebWidget()->RecordEndOfFrameMetrics(frame_begin_time);
1157}
1158
fsamuel78f86e42016-01-20 04:10:231159void RenderWidget::WillBeginCompositorFrame() {
1160 TRACE_EVENT0("gpu", "RenderWidget::willBeginCompositorFrame");
1161
Ken Buchananed449bb2018-02-01 21:02:051162 if (!GetWebWidget())
1163 return;
1164
Blink Reformat1c4d759e2017-04-09 16:34:541165 GetWebWidget()->SetSuppressFrameRequestsWorkaroundFor704763Only(true);
alancutter317a8e02017-04-05 10:09:291166
dglazkov06854c5c2016-08-31 00:19:591167 // The UpdateTextInputState can result in further layout and possibly
1168 // enable GPU acceleration so they need to be called before any painting
1169 // is done.
changwan75e3b2072017-01-16 02:55:001170 UpdateTextInputState();
dglazkov06854c5c2016-08-31 00:19:591171 UpdateSelectionBounds();
1172
ericwilligers88e69742016-10-17 19:29:551173 for (auto& observer : render_frame_proxies_)
1174 observer.WillBeginCompositorFrame();
initial.commit09911bf2008-07-26 23:55:291175}
1176
Kent Tamura21d1de62018-12-10 04:45:201177std::unique_ptr<cc::SwapPromise> RenderWidget::RequestCopyOfOutputForWebTest(
Fady Samueldfecb7d2017-07-26 11:41:041178 std::unique_ptr<viz::CopyOutputRequest> request) {
Kent Tamura21d1de62018-12-10 04:45:201179 return RenderThreadImpl::current()->RequestCopyOfOutputForWebTest(
jbroman6ccbc7d472016-07-27 04:45:411180 routing_id_, std::move(request));
1181}
1182
fsamuel72464894f2015-12-15 06:59:311183///////////////////////////////////////////////////////////////////////////////
1184// RenderWidgetInputHandlerDelegate
1185
avid7d6b2e2016-03-04 19:41:171186void RenderWidget::FocusChangeComplete() {
Ehsan Karamad655d7b8a2018-01-12 18:38:381187 blink::WebFrameWidget* frame_widget = GetFrameWidget();
1188 if (!frame_widget)
ekaramad27ca69b12017-04-20 18:34:291189 return;
Ehsan Karamad655d7b8a2018-01-12 18:38:381190
ekaramad27ca69b12017-04-20 18:34:291191 blink::WebLocalFrame* focused =
Ehsan Karamad655d7b8a2018-01-12 18:38:381192 frame_widget->LocalRoot()->View()->FocusedFrame();
1193
ekaramad27ca69b12017-04-20 18:34:291194 if (focused && focused->AutofillClient())
1195 focused->AutofillClient()->DidCompleteFocusChangeInFrame();
avid7d6b2e2016-03-04 19:41:171196}
fsamuel72464894f2015-12-15 06:59:311197
dtapuska1827dd22016-03-11 15:24:591198void RenderWidget::ObserveGestureEventAndResult(
1199 const blink::WebGestureEvent& gesture_event,
1200 const gfx::Vector2dF& unused_delta,
Sandra Sunac5cdd832017-12-11 03:27:131201 const cc::OverscrollBehavior& overscroll_behavior,
dtapuska1827dd22016-03-11 15:24:591202 bool event_processed) {
1203 if (!compositor_deps_->IsElasticOverscrollEnabled())
1204 return;
1205
1206 cc::InputHandlerScrollResult scroll_result;
1207 scroll_result.did_scroll = event_processed;
1208 scroll_result.did_overscroll_root = !unused_delta.IsZero();
1209 scroll_result.unused_scroll_delta = unused_delta;
Sandra Sunac5cdd832017-12-11 03:27:131210 scroll_result.overscroll_behavior = overscroll_behavior;
dtapuska1827dd22016-03-11 15:24:591211
Dave Tapuska04bc5ee92018-04-17 19:03:311212 widget_input_handler_manager_->ObserveGestureEventOnMainThread(gesture_event,
1213 scroll_result);
dtapuska1827dd22016-03-11 15:24:591214}
1215
avid7d6b2e2016-03-04 19:41:171216void RenderWidget::OnDidHandleKeyEvent() {
alexmos56567492016-09-13 00:52:461217 ClearEditCommands();
1218}
1219
1220void RenderWidget::SetEditCommandForNextKeyEvent(const std::string& name,
1221 const std::string& value) {
1222 ClearEditCommands();
1223 edit_commands_.emplace_back(name, value);
1224}
1225
1226void RenderWidget::ClearEditCommands() {
1227 edit_commands_.clear();
avid7d6b2e2016-03-04 19:41:171228}
fsamuel72464894f2015-12-15 06:59:311229
chongza8ba91fc2016-08-16 21:39:171230void RenderWidget::OnDidOverscroll(const ui::DidOverscrollParams& params) {
Dave Tapuska04bc5ee92018-04-17 19:03:311231 if (mojom::WidgetInputHandlerHost* host =
1232 widget_input_handler_manager_->GetWidgetInputHandlerHost()) {
1233 host->DidOverscroll(params);
Dave Tapuska139a72f2017-09-06 21:57:031234 }
fsamuel72464894f2015-12-15 06:59:311235}
1236
fsamuele8326c742016-01-12 00:49:391237void RenderWidget::SetInputHandler(RenderWidgetInputHandler* input_handler) {
1238 // Nothing to do here. RenderWidget created the |input_handler| and will take
1239 // ownership of it. We just verify here that we don't already have an input
1240 // handler.
1241 DCHECK(!input_handler_);
1242}
1243
changwan75e3b2072017-01-16 02:55:001244void RenderWidget::ShowVirtualKeyboard() {
1245 UpdateTextInputStateInternal(true, false);
1246}
1247
changwan62f57292017-02-17 08:28:251248void RenderWidget::ClearTextInputState() {
1249 text_input_info_ = blink::WebTextInputInfo();
1250 text_input_type_ = ui::TextInputType::TEXT_INPUT_TYPE_NONE;
1251 text_input_mode_ = ui::TextInputMode::TEXT_INPUT_MODE_DEFAULT;
1252 can_compose_inline_ = false;
1253 text_input_flags_ = 0;
AJITH KUMAR V041c0b02017-08-08 10:39:201254 next_previous_flags_ = kInvalidNextPreviousFlagsValue;
changwan62f57292017-02-17 08:28:251255}
1256
changwan75e3b2072017-01-16 02:55:001257void RenderWidget::UpdateTextInputState() {
1258 UpdateTextInputStateInternal(false, false);
1259}
1260
1261void RenderWidget::UpdateTextInputStateInternal(bool show_virtual_keyboard,
1262 bool reply_to_request) {
fsamuel72464894f2015-12-15 06:59:311263 TRACE_EVENT0("renderer", "RenderWidget::UpdateTextInputState");
changwan75e3b2072017-01-16 02:55:001264
fsamuel72464894f2015-12-15 06:59:311265 if (ime_event_guard_) {
changwan75e3b2072017-01-16 02:55:001266 DCHECK(!reply_to_request);
1267 // show_virtual_keyboard should still be effective even if it was set inside
1268 // the IME
fsamuel72464894f2015-12-15 06:59:311269 // event guard.
changwan75e3b2072017-01-16 02:55:001270 if (show_virtual_keyboard)
1271 ime_event_guard_->set_show_virtual_keyboard(true);
fsamuel72464894f2015-12-15 06:59:311272 return;
1273 }
1274
dglazkov97b6c2b2016-10-25 17:35:551275 ui::TextInputType new_type = GetTextInputType();
1276 if (IsDateTimeInput(new_type))
1277 return; // Not considered as a text input field in WebKit/Chromium.
fsamuel72464894f2015-12-15 06:59:311278
dglazkov97b6c2b2016-10-25 17:35:551279 blink::WebTextInputInfo new_info;
ekaramad5aff1942017-01-06 01:26:351280 if (auto* controller = GetInputMethodController())
Blink Reformat1c4d759e2017-04-09 16:34:541281 new_info = controller->TextInputInfo();
dtapuskac4dd5be2016-10-25 15:11:101282 const ui::TextInputMode new_mode =
Blink Reformat1c4d759e2017-04-09 16:34:541283 ConvertWebTextInputMode(new_info.input_mode);
fsamuel72464894f2015-12-15 06:59:311284
1285 bool new_can_compose_inline = CanComposeInline();
1286
1287 // Only sends text input params if they are changed or if the ime should be
1288 // shown.
changwan75e3b2072017-01-16 02:55:001289 if (show_virtual_keyboard || reply_to_request ||
changwan38c3eb612016-12-09 01:45:561290 text_input_type_ != new_type || text_input_mode_ != new_mode ||
1291 text_input_info_ != new_info ||
1292 can_compose_inline_ != new_can_compose_inline) {
ekaramad9053e57b2016-04-26 20:00:381293 TextInputState params;
dglazkov97b6c2b2016-10-25 17:35:551294 params.type = new_type;
fsamuel72464894f2015-12-15 06:59:311295 params.mode = new_mode;
1296 params.flags = new_info.flags;
AJITH KUMAR V9e7b5572017-11-14 00:54:211297#if defined(OS_ANDROID)
AJITH KUMAR V041c0b02017-08-08 10:39:201298 if (next_previous_flags_ == kInvalidNextPreviousFlagsValue) {
1299 // Due to a focus change, values will be reset by the frame.
1300 // That case we only need fresh NEXT/PREVIOUS information.
Albert J. Wong3c93c182018-09-27 17:29:431301 // Also we won't send WidgetHostMsg_TextInputStateChanged if next/previous
AJITH KUMAR V041c0b02017-08-08 10:39:201302 // focusable status is changed.
1303 if (auto* controller = GetInputMethodController()) {
1304 next_previous_flags_ =
1305 controller->ComputeWebTextInputNextPreviousFlags();
1306 } else {
1307 // For safety in case GetInputMethodController() is null, because -1 is
1308 // invalid value to send to browser process.
1309 next_previous_flags_ = 0;
1310 }
1311 }
AJITH KUMAR V9e7b5572017-11-14 00:54:211312#else
1313 next_previous_flags_ = 0;
1314#endif
AJITH KUMAR V041c0b02017-08-08 10:39:201315 params.flags |= next_previous_flags_;
Yuichiro Hanada1b19cf852019-01-18 16:12:261316 params.value = new_info.value.Utf16();
Blink Reformat1c4d759e2017-04-09 16:34:541317 params.selection_start = new_info.selection_start;
1318 params.selection_end = new_info.selection_end;
1319 params.composition_start = new_info.composition_start;
1320 params.composition_end = new_info.composition_end;
fsamuel72464894f2015-12-15 06:59:311321 params.can_compose_inline = new_can_compose_inline;
changwan75e3b2072017-01-16 02:55:001322 // TODO(changwan): change instances of show_ime_if_needed to
1323 // show_virtual_keyboard.
1324 params.show_ime_if_needed = show_virtual_keyboard;
1325 params.reply_to_request = reply_to_request;
Albert J. Wong3c93c182018-09-27 17:29:431326 Send(new WidgetHostMsg_TextInputStateChanged(routing_id(), params));
fsamuel72464894f2015-12-15 06:59:311327
1328 text_input_info_ = new_info;
dglazkov97b6c2b2016-10-25 17:35:551329 text_input_type_ = new_type;
fsamuel72464894f2015-12-15 06:59:311330 text_input_mode_ = new_mode;
1331 can_compose_inline_ = new_can_compose_inline;
1332 text_input_flags_ = new_info.flags;
Eric Karl706ea6712018-12-21 22:41:381333
1334#if defined(OS_ANDROID)
1335 // If we send a new TextInputStateChanged message, we must also deliver a
1336 // new RenderFrameMetadata, as the IME will need this info to be updated.
1337 // TODO(ericrk): Consider folding the above IPC into RenderFrameMetadata.
1338 // https://crbug.com/912309
Kinuko Yasudabef2b642019-01-24 04:53:331339 if (layer_tree_view_ && IsSurfaceSynchronizationEnabled()) {
Eric Karl706ea6712018-12-21 22:41:381340 layer_tree_view_->RequestForceSendMetadata();
1341 }
1342#endif
fsamuel72464894f2015-12-15 06:59:311343 }
1344}
1345
1346bool RenderWidget::WillHandleGestureEvent(const blink::WebGestureEvent& event) {
paulmeyer6ef5a792016-11-08 20:33:581347 possible_drag_event_info_.event_source =
1348 ui::DragDropTypes::DRAG_EVENT_SOURCE_TOUCH;
1349 possible_drag_event_info_.event_location =
Ella Ge1116059d2018-03-21 02:06:131350 gfx::ToFlooredPoint(event.PositionInScreen());
avid7d6b2e2016-03-04 19:41:171351
fsamuel72464894f2015-12-15 06:59:311352 return false;
1353}
1354
1355bool RenderWidget::WillHandleMouseEvent(const blink::WebMouseEvent& event) {
ericwilligers88e69742016-10-17 19:29:551356 for (auto& observer : render_frames_)
1357 observer.RenderWidgetWillHandleMouseEvent();
lfge0c2792ec2016-05-11 18:52:081358
paulmeyer6ef5a792016-11-08 20:33:581359 possible_drag_event_info_.event_source =
1360 ui::DragDropTypes::DRAG_EVENT_SOURCE_MOUSE;
1361 possible_drag_event_info_.event_location =
Blink Reformat1c4d759e2017-04-09 16:34:541362 gfx::Point(event.PositionInScreen().x, event.PositionInScreen().y);
paulmeyer6ef5a792016-11-08 20:33:581363
Albert J. Wong7bbf22d2018-12-20 00:27:271364 if (delegate())
1365 return delegate()->RenderWidgetWillHandleMouseEventForWidget(event);
avid7d6b2e2016-03-04 19:41:171366
fsamuel72464894f2015-12-15 06:59:311367 return false;
[email protected]fd847792013-10-24 17:12:351368}
1369
initial.commit09911bf2008-07-26 23:55:291370///////////////////////////////////////////////////////////////////////////////
mfomitchev2600fd7c2016-02-17 20:53:391371// RenderWidgetScreenMetricsDelegate
1372
bokanc63441c2016-04-27 15:49:121373void RenderWidget::ResizeWebWidget() {
danakja2c9d0a92018-07-25 20:01:181374 gfx::Size size = GetSizeForWebWidget();
Albert J. Wong7bbf22d2018-12-20 00:27:271375 if (delegate()) {
1376 delegate()->ResizeWebWidgetForWidget(size, top_controls_height_,
1377 bottom_controls_height_,
1378 browser_controls_shrink_blink_size_);
danakja2c9d0a92018-07-25 20:01:181379 return;
1380 }
1381 GetWebWidget()->Resize(size);
bokanc63441c2016-04-27 15:49:121382}
1383
1384gfx::Size RenderWidget::GetSizeForWebWidget() const {
danakj4b347212018-07-04 17:55:171385 if (compositor_deps_->IsUseZoomForDSFEnabled()) {
Fady Samuela863f152018-03-09 16:10:031386 return gfx::ScaleToCeiledSize(size_,
1387 GetOriginalScreenInfo().device_scale_factor);
1388 }
bokanc63441c2016-04-27 15:49:121389
1390 return size_;
1391}
1392
akaba3483d8f2018-07-10 21:43:091393void RenderWidget::UpdateZoom(double zoom_level) {
akabac6bd1212018-06-25 20:10:481394 blink::WebFrameWidget* frame_widget = GetFrameWidget();
1395 if (!frame_widget)
1396 return;
1397 RenderFrameImpl* render_frame =
1398 RenderFrameImpl::FromWebFrame(frame_widget->LocalRoot());
1399
akaba3483d8f2018-07-10 21:43:091400 // Return early if zoom level is unchanged.
1401 if (render_frame->GetZoomLevel() == zoom_level) {
akabac6bd1212018-06-25 20:10:481402 return;
1403 }
1404
akaba3483d8f2018-07-10 21:43:091405 render_frame->SetZoomLevel(zoom_level);
akabac6bd1212018-06-25 20:10:481406
1407 for (auto& observer : render_frame_proxies_)
akaba3483d8f2018-07-10 21:43:091408 observer.OnZoomLevelChanged(zoom_level);
akabac6bd1212018-06-25 20:10:481409
1410 for (auto& plugin : browser_plugins_)
akaba3483d8f2018-07-10 21:43:091411 plugin.OnZoomLevelChanged(zoom_level);
akabac6bd1212018-06-25 20:10:481412}
1413
Fady Samuel799e72192018-04-25 21:16:571414void RenderWidget::SynchronizeVisualProperties(const VisualProperties& params) {
Christopher Cameronbe7ecea2018-02-27 00:47:191415 // Inform the rendering thread of the color space indicate the presence of HDR
1416 // capabilities.
Miguel Casas5e1018052018-01-09 19:17:361417 RenderThreadImpl* render_thread = RenderThreadImpl::current();
1418 if (render_thread)
Christopher Camerone9a30c12018-03-07 08:23:381419 render_thread->SetRenderingColorSpace(params.screen_info.color_space);
Miguel Casas5e1018052018-01-09 19:17:361420
Fady Samuele62be402018-03-29 03:19:531421 gfx::Size new_compositor_viewport_pixel_size =
1422 params.auto_resize_enabled
1423 ? gfx::ScaleToCeiledSize(size_,
1424 params.screen_info.device_scale_factor)
1425 : params.compositor_viewport_pixel_size;
Fady Samuel84d8a1f72018-11-08 00:26:171426 UpdateSurfaceAndScreenInfo(params.local_surface_id_allocation.value_or(
1427 viz::LocalSurfaceIdAllocation()),
1428 new_compositor_viewport_pixel_size,
1429 params.screen_info);
Vladimir Levin98d76dad2018-04-21 00:21:291430 UpdateCaptureSequenceNumber(params.capture_sequence_number);
Kinuko Yasudabef2b642019-01-24 04:53:331431 if (layer_tree_view_) {
1432 layer_tree_view_->SetBrowserControlsHeight(
1433 params.top_controls_height, params.bottom_controls_height,
1434 params.browser_controls_shrink_blink_size);
1435 layer_tree_view_->SetRasterColorSpace(
1436 screen_info_.color_space.GetRasterColorSpace());
1437 }
mfomitchev2600fd7c2016-02-17 20:53:391438
akaba3483d8f2018-07-10 21:43:091439 UpdateZoom(params.zoom_level);
akabac6bd1212018-06-25 20:10:481440
danakj21604ce2018-12-19 16:01:221441 if (!params.auto_resize_enabled) {
1442 visible_viewport_size_ = params.visible_viewport_size;
Fady Samuele62be402018-03-29 03:19:531443
danakj21604ce2018-12-19 16:01:221444 display_mode_ = params.display_mode;
mfomitchev2600fd7c2016-02-17 20:53:391445
danakj21604ce2018-12-19 16:01:221446 size_ = params.new_size;
mfomitchev2600fd7c2016-02-17 20:53:391447
danakj21604ce2018-12-19 16:01:221448 ResizeWebWidget();
mfomitchev2600fd7c2016-02-17 20:53:391449
danakj21604ce2018-12-19 16:01:221450 WebSize visual_viewport_size;
1451 if (compositor_deps_->IsUseZoomForDSFEnabled()) {
1452 visual_viewport_size =
1453 gfx::ScaleToCeiledSize(params.visible_viewport_size,
1454 GetOriginalScreenInfo().device_scale_factor);
1455 } else {
1456 visual_viewport_size = visible_viewport_size_;
1457 }
1458 GetWebWidget()->ResizeVisualViewport(visual_viewport_size);
bokan71cb5b12016-04-27 03:45:221459
danakjf9284772019-01-23 18:29:141460 // NOTE: We may have entered fullscreen mode without changing our size.
1461 SetIsFullscreen(params.is_fullscreen_granted);
mfomitchev2600fd7c2016-02-17 20:53:391462 }
mfomitchev2600fd7c2016-02-17 20:53:391463}
1464
1465void RenderWidget::SetScreenMetricsEmulationParameters(
1466 bool enabled,
1467 const blink::WebDeviceEmulationParams& params) {
Albert J. Wong7bbf22d2018-12-20 00:27:271468 // This is only supported in RenderView, which has an delegate().
1469 DCHECK(delegate());
1470 delegate()->SetScreenMetricsEmulationParametersForWidget(enabled, params);
mfomitchev2600fd7c2016-02-17 20:53:391471}
1472
Albert J. Wong3c93c182018-09-27 17:29:431473void RenderWidget::SetScreenRects(const gfx::Rect& widget_screen_rect,
mfomitchev2600fd7c2016-02-17 20:53:391474 const gfx::Rect& window_screen_rect) {
Albert J. Wong3c93c182018-09-27 17:29:431475 widget_screen_rect_ = widget_screen_rect;
mfomitchev2600fd7c2016-02-17 20:53:391476 window_screen_rect_ = window_screen_rect;
1477}
1478
1479///////////////////////////////////////////////////////////////////////////////
[email protected]f98d7e3c2010-09-13 22:30:461480// WebWidgetClient
initial.commit09911bf2008-07-26 23:55:291481
Chris Harrelsond7ab99b2018-01-24 17:51:361482void RenderWidget::IntrinsicSizingInfoChanged(
1483 const blink::WebIntrinsicSizingInfo& sizing_info) {
Albert J. Wong3c93c182018-09-27 17:29:431484 Send(new WidgetHostMsg_IntrinsicSizingInfoChanged(routing_id_, sizing_info));
Chris Harrelsond7ab99b2018-01-24 17:51:361485}
1486
Blink Reformat1c4d759e2017-04-09 16:34:541487void RenderWidget::DidMeaningfulLayout(blink::WebMeaningfulLayout layout_type) {
1488 if (layout_type == blink::WebMeaningfulLayout::kVisuallyNonEmpty) {
Albert J. Wongfb64e142018-10-16 01:10:451489 QueueMessage(new WidgetHostMsg_DidFirstVisuallyNonEmptyPaint(routing_id_));
dglazkovf0e1d6d2015-10-10 02:13:481490 }
dglazkov79c426102015-08-31 21:22:431491
ericwilligers88e69742016-10-17 19:29:551492 for (auto& observer : render_frames_)
1493 observer.DidMeaningfulLayout(layout_type);
dglazkov79c426102015-08-31 21:22:431494}
1495
[email protected]586871b2014-07-22 17:05:111496// static
dchengcedca5612016-04-09 01:40:151497std::unique_ptr<cc::SwapPromise> RenderWidget::QueueMessageImpl(
[email protected]586871b2014-07-22 17:05:111498 IPC::Message* msg,
[email protected]586871b2014-07-22 17:05:111499 FrameSwapMessageQueue* frame_swap_message_queue,
1500 scoped_refptr<IPC::SyncMessageFilter> sync_message_filter,
[email protected]586871b2014-07-22 17:05:111501 int source_frame_number) {
[email protected]586871b2014-07-22 17:05:111502 bool first_message_for_frame = false;
Sadrul Habib Chowdhury4e0335cf2018-07-31 20:08:491503 frame_swap_message_queue->QueueMessageForFrame(
1504 source_frame_number, base::WrapUnique(msg), &first_message_for_frame);
[email protected]586871b2014-07-22 17:05:111505 if (first_message_for_frame) {
dchengcedca5612016-04-09 01:40:151506 std::unique_ptr<cc::SwapPromise> promise(new QueueMessageSwapPromise(
[email protected]586871b2014-07-22 17:05:111507 sync_message_filter, frame_swap_message_queue, source_frame_number));
dcheng4b6b5ff2014-10-16 00:42:061508 return promise;
[email protected]586871b2014-07-22 17:05:111509 }
dcheng4b6b5ff2014-10-16 00:42:061510 return nullptr;
[email protected]586871b2014-07-22 17:05:111511}
1512
danakj53802692018-07-25 21:46:441513void RenderWidget::SetHandlingInputEvent(bool handling_input_event) {
1514 input_handler_->set_handling_input_event(handling_input_event);
1515}
1516
Sadrul Habib Chowdhury4e0335cf2018-07-31 20:08:491517void RenderWidget::QueueMessage(IPC::Message* msg) {
[email protected]586871b2014-07-22 17:05:111518 // RenderThreadImpl::current() is NULL in some tests.
Kinuko Yasudabef2b642019-01-24 04:53:331519 if (!layer_tree_view_ || !RenderThreadImpl::current()) {
[email protected]586871b2014-07-22 17:05:111520 Send(msg);
1521 return;
1522 }
1523
dchengcedca5612016-04-09 01:40:151524 std::unique_ptr<cc::SwapPromise> swap_promise =
Sadrul Habib Chowdhury4e0335cf2018-07-31 20:08:491525 QueueMessageImpl(msg, frame_swap_message_queue_.get(),
[email protected]586871b2014-07-22 17:05:111526 RenderThreadImpl::current()->sync_message_filter(),
danakja6c10012018-07-06 14:25:361527 layer_tree_view_->GetSourceFrameNumber());
[email protected]586871b2014-07-22 17:05:111528
Ken Buchanance710eb2017-08-30 23:46:521529 if (swap_promise)
danakja6c10012018-07-06 14:25:361530 layer_tree_view_->QueueSwapPromise(std::move(swap_promise));
[email protected]586871b2014-07-22 17:05:111531}
1532
Blink Reformat1c4d759e2017-04-09 16:34:541533void RenderWidget::DidChangeCursor(const WebCursorInfo& cursor_info) {
[email protected]7c51b0ee2009-07-08 21:49:301534 // TODO(darin): Eliminate this temporary.
[email protected]9ec87712013-05-24 23:23:521535 WebCursor cursor;
tfarina75a0abf2015-10-06 15:07:181536 InitializeCursorFromWebCursorInfo(&cursor, cursor_info);
initial.commit09911bf2008-07-26 23:55:291537 // Only send a SetCursor message if we need to make a change.
Navid Zolghadr293bb862018-12-12 18:15:411538 if (input_handler_->DidChangeCursor(cursor))
Albert J. Wong3c93c182018-09-27 17:29:431539 Send(new WidgetHostMsg_SetCursor(routing_id_, cursor));
initial.commit09911bf2008-07-26 23:55:291540}
1541
aelias5971e47d2017-06-16 02:39:381542void RenderWidget::AutoscrollStart(const blink::WebFloatPoint& point) {
Albert J. Wong3c93c182018-09-27 17:29:431543 Send(new WidgetHostMsg_AutoscrollStart(routing_id_, point));
aelias5971e47d2017-06-16 02:39:381544}
1545
1546void RenderWidget::AutoscrollFling(const blink::WebFloatSize& velocity) {
Albert J. Wong3c93c182018-09-27 17:29:431547 Send(new WidgetHostMsg_AutoscrollFling(routing_id_, velocity));
aelias5971e47d2017-06-16 02:39:381548}
1549
1550void RenderWidget::AutoscrollEnd() {
Albert J. Wong3c93c182018-09-27 17:29:431551 Send(new WidgetHostMsg_AutoscrollEnd(routing_id_));
aelias5971e47d2017-06-16 02:39:381552}
1553
initial.commit09911bf2008-07-26 23:55:291554// We are supposed to get a single call to Show for a newly created RenderWidget
1555// that was created via RenderWidget::CreateWebView. So, we wait until this
1556// point to dispatch the ShowWidget message.
1557//
1558// This method provides us with the information about how to display the newly
[email protected]5f9de5882011-09-30 23:36:281559// created RenderWidget (i.e., as a blocked popup or as a new tab).
initial.commit09911bf2008-07-26 23:55:291560//
Blink Reformat1c4d759e2017-04-09 16:34:541561void RenderWidget::Show(WebNavigationPolicy policy) {
danakj65f423b72018-10-10 17:21:201562 if (!show_callback_) {
Albert J. Wong7bbf22d2018-12-20 00:27:271563 if (delegate()) {
danakja2c9d0a92018-07-25 20:01:181564 // When SupportsMultipleWindows is disabled, popups are reusing
danakj65f423b72018-10-10 17:21:201565 // the view's RenderWidget. In some scenarios, this makes blink to call
1566 // Show() twice. But otherwise, if it is enabled, we should not visit
1567 // Show() more than once.
Albert J. Wong7bbf22d2018-12-20 00:27:271568 DCHECK(!delegate()->SupportsMultipleWindowsForWidget());
danakj65f423b72018-10-10 17:21:201569 return;
danakja2c9d0a92018-07-25 20:01:181570 } else {
danakj65f423b72018-10-10 17:21:201571 NOTREACHED() << "received extraneous Show call";
danakja2c9d0a92018-07-25 20:01:181572 }
danakja2c9d0a92018-07-25 20:01:181573 }
1574
initial.commit09911bf2008-07-26 23:55:291575 DCHECK(routing_id_ != MSG_ROUTING_NONE);
nickf7b38222016-11-22 21:59:351576
1577 // The opener is responsible for actually showing this widget.
danakjdf1ceb72018-07-18 20:02:251578 std::move(show_callback_).Run(this, policy, initial_rect_);
nickf7b38222016-11-22 21:59:351579
bokanc007c3a2015-02-03 07:15:561580 // NOTE: initial_rect_ may still have its default values at this point, but
[email protected]8de12d942010-11-17 20:42:441581 // that's okay. It'll be ignored if as_popup is false, or the browser
1582 // process will impose a default position otherwise.
bokanc007c3a2015-02-03 07:15:561583 SetPendingWindowRect(initial_rect_);
initial.commit09911bf2008-07-26 23:55:291584}
1585
danakj17216f4d2018-10-16 22:57:321586LayerTreeView* RenderWidget::InitializeLayerTreeView() {
1587 TRACE_EVENT0("blink", "RenderWidget::InitializeLayerTreeView");
danakj17216f4d2018-10-16 22:57:321588
1589 layer_tree_view_ = std::make_unique<LayerTreeView>(
1590 this, compositor_deps_->GetCompositorMainThreadTaskRunner(),
1591 compositor_deps_->GetCompositorImplThreadTaskRunner(),
1592 compositor_deps_->GetTaskGraphRunner(),
1593 compositor_deps_->GetWebMainThreadScheduler());
1594 layer_tree_view_->Initialize(
danakj6dcbc5962018-11-16 16:45:421595 GenerateLayerTreeSettings(compositor_deps_, for_child_local_root_frame_,
danakj17216f4d2018-10-16 22:57:321596 screen_info_.rect.size(),
1597 screen_info_.device_scale_factor),
1598 compositor_deps_->CreateUkmRecorderFactory());
1599
Fady Samuel1c1ad3692018-11-06 23:28:401600 UpdateSurfaceAndScreenInfo(local_surface_id_allocation_from_parent_,
danakj17216f4d2018-10-16 22:57:321601 compositor_viewport_pixel_size_, screen_info_);
1602 layer_tree_view_->SetRasterColorSpace(
1603 screen_info_.color_space.GetRasterColorSpace());
1604 layer_tree_view_->SetContentSourceId(current_content_source_id_);
1605 // For background pages and certain tests, we don't want to trigger
1606 // LayerTreeFrameSink creation.
1607 bool should_generate_frame_sink =
1608 !compositor_never_visible_ && RenderThreadImpl::current();
danakje9be3162018-11-05 21:32:001609 if (!should_generate_frame_sink) {
1610 // Prevents SetVisible() from blink from doing anything.
danakj17216f4d2018-10-16 22:57:321611 layer_tree_view_->SetNeverVisible();
danakj6dcbc5962018-11-16 16:45:421612 } else if (!is_frozen_) {
danakje9be3162018-11-05 21:32:001613 // Begins the compositor's scheduler to start producing frames.
1614 // Don't do this if the RenderWidget is attached to a RenderViewImpl for a
danakjbc6ba9d22018-11-17 04:03:411615 // proxy main frame, as this RenderWidget is frozen then, and won't be
danakje9be3162018-11-05 21:32:001616 // used for compositing until a WebFrameWidget is attached.
1617 StartCompositor();
1618 }
danakj17216f4d2018-10-16 22:57:321619
danakj17216f4d2018-10-16 22:57:321620 DCHECK_NE(MSG_ROUTING_NONE, routing_id_);
1621 layer_tree_view_->SetFrameSinkId(
1622 viz::FrameSinkId(RenderThread::Get()->GetClientId(), routing_id_));
1623
1624 RenderThreadImpl* render_thread = RenderThreadImpl::current();
1625 if (render_thread) {
1626 input_event_queue_ = base::MakeRefCounted<MainThreadEventQueue>(
1627 this, render_thread->GetWebMainThreadScheduler()->InputTaskRunner(),
1628 render_thread->GetWebMainThreadScheduler(), should_generate_frame_sink);
1629 }
1630
1631 return layer_tree_view_.get();
1632}
1633
danakjde8b7482018-11-15 23:45:021634void RenderWidget::StartCompositor() {
1635 if (!is_hidden_)
1636 layer_tree_view_->SetVisible(true);
1637}
1638
1639void RenderWidget::StopCompositor() {
1640 layer_tree_view_->SetVisible(false);
1641 // Drop all gpu resources, this makes SetVisible(true) more expensive/slower
1642 // but we don't expect to use this RenderWidget again until some possible
1643 // future navigation. This brings us a bit closer to emulating deleting the
1644 // RenderWidget instead of just stopping the compositor.
1645 layer_tree_view_->ReleaseLayerTreeFrameSink();
1646}
1647
danakj6dcbc5962018-11-16 16:45:421648void RenderWidget::SetIsFrozen(bool is_frozen) {
1649 DCHECK_NE(is_frozen, is_frozen_);
1650 is_frozen_ = is_frozen;
1651 if (is_frozen)
danakjde8b7482018-11-15 23:45:021652 StopCompositor();
1653 else
1654 StartCompositor();
1655}
1656
danakjf87460792018-12-04 20:14:221657void RenderWidget::WarmupCompositor() {
1658 DCHECK(is_frozen_);
1659
1660 // Tests have no RenderThreadImpl /o\.
1661 if (!RenderThreadImpl::current())
1662 return;
1663
1664 // Keeping things simple. This would cancel any outstanding warmup if we
1665 // happened to have one (this should be basically impossible). This avoids any
1666 // extra book keeping about the outstanding reqeust.
1667 warmup_weak_ptr_factory_.InvalidateWeakPtrs();
1668 // And if we already did a warmup then we're done.
1669 if (warmup_frame_sink_)
1670 return;
1671
1672 // Mark us pending the warmup frame sink *before* calling
1673 // DoRequestNewLayerTreeFrameSink() as it may run the reply callback
1674 // synchronously. So we don't want to change any state after the call
1675 // to DoRequestNewLayerTreeFrameSink() here.
1676 warmup_frame_sink_request_pending_ = true;
1677
1678 auto cb = base::BindOnce(&RenderWidget::OnReplyForWarmupCompositor,
1679 warmup_weak_ptr_factory_.GetWeakPtr());
1680 DoRequestNewLayerTreeFrameSink(std::move(cb));
1681}
1682
1683void RenderWidget::OnReplyForWarmupCompositor(
1684 std::unique_ptr<cc::LayerTreeFrameSink> sink) {
1685 warmup_frame_sink_request_pending_ = false;
1686
1687 if (after_warmup_callback_)
1688 std::move(after_warmup_callback_).Run(std::move(sink));
1689 else
1690 warmup_frame_sink_ = std::move(sink);
1691}
1692
1693void RenderWidget::AbortWarmupCompositor() {
1694 warmup_frame_sink_request_pending_ = false;
1695 // Drop any pending warmup.
1696 warmup_weak_ptr_factory_.InvalidateWeakPtrs();
1697 // And drop any completed one.
1698 warmup_frame_sink_.reset();
1699
1700 // If we had saved a callback to run after warmup, just do so now indicating
1701 // failure.
1702 if (after_warmup_callback_)
1703 std::move(after_warmup_callback_).Run(nullptr);
1704}
1705
[email protected]2533ce12009-05-09 00:02:241706void RenderWidget::DoDeferredClose() {
Albert J. Wong3c93c182018-09-27 17:29:431707 Send(new WidgetHostMsg_Close(routing_id_));
[email protected]2533ce12009-05-09 00:02:241708}
1709
Blink Reformat1c4d759e2017-04-09 16:34:541710void RenderWidget::CloseWidgetSoon() {
skyostiled8969c2015-07-20 16:57:081711 DCHECK(content::RenderThread::Get());
danakj6dcbc5962018-11-16 16:45:421712 if (is_frozen_) {
1713 // This widget is currently not active. The active main frame widget is in a
[email protected]e1c3a552012-05-04 20:51:321714 // different process. Have the browser route the close request to the
1715 // active widget instead, so that the correct unload handlers are run.
Albert J. Wongfb64e142018-10-16 01:10:451716 Send(new WidgetHostMsg_RouteCloseEvent(routing_id_));
[email protected]e1c3a552012-05-04 20:51:321717 return;
1718 }
1719
danakj292b63e2018-11-19 20:47:061720 // Prevent compositor from setting up new IPC channels, since we know a
1721 // WidgetMsg_Close is coming. We do this immediately, not in DoDeferredClose,
1722 // as the caller (eg WebPagePopupImpl) may start tearing down things after
1723 // calling this method, including detaching the frame from this RenderWidget.
1724 // Then trying to make a LayerTreeFrameSink would crash.
1725 // https://crbug.com/906340
1726 host_will_close_this_ = true;
1727
initial.commit09911bf2008-07-26 23:55:291728 // If a page calls window.close() twice, we'll end up here twice, but that's
1729 // OK. It is safe to send multiple Close messages.
danakj292b63e2018-11-19 20:47:061730 //
[email protected]2533ce12009-05-09 00:02:241731 // Ask the RenderWidgetHost to initiate close. We could be called from deep
1732 // in Javascript. If we ask the RendwerWidgetHost to close now, the window
1733 // could be closed before the JS finishes executing. So instead, post a
1734 // message back to the message loop, which won't run until the JS is
1735 // complete, and then the Close message can be sent.
Hajime Hoshi315a61f2018-08-14 17:27:281736 GetCleanupTaskRunner()->PostTask(
1737 FROM_HERE, base::BindOnce(&RenderWidget::DoDeferredClose, this));
initial.commit09911bf2008-07-26 23:55:291738}
1739
1740void RenderWidget::Close() {
danakj6dcbc5962018-11-16 16:45:421741 // This was done immediately in the |for_child_local_root_frame_| case in the
1742 // OnClose() IPC handler.
1743 if (!for_child_local_root_frame_)
danakj5c5269b2018-11-06 00:39:111744 CloseWebWidget();
1745
danakja6c10012018-07-06 14:25:361746 layer_tree_view_.reset();
Albert J. Wong7bbf22d2018-12-20 00:27:271747 if (delegate())
1748 delegate()->DidCloseWidget();
Albert J. Wongd593de532018-09-06 05:42:151749 // Note the ACK is a control message going to the RenderProcessHost.
Albert J. Wong3c93c182018-09-27 17:29:431750 RenderThread::Get()->Send(new WidgetHostMsg_Close_ACK(routing_id()));
Ken Buchananed449bb2018-02-01 21:02:051751}
1752
1753void RenderWidget::CloseWebWidget() {
danakj35314ab2018-10-19 16:37:341754 // If the browser has not sent OnDisableDeviceEmulation, we have an emulator
1755 // hanging out still. Destroying it must happen *after* the IPC route is
1756 // removed so that another IPC does not arrive and re-create the emulator
1757 // during closing.
1758 //
1759 // This destruction is normally part of an IPC and expects objects to be alive
1760 // that would be alive while the IPC route is active such as the
1761 // |layer_tree_view_|. So we ensure that it is the first thing to be
1762 // destroyed here before deleting things from the RenderWidget or the
Albert J. Wong7bbf22d2018-12-20 00:27:271763 // delegate().
danakj35314ab2018-10-19 16:37:341764 //
1765 // TODO(danakj): The emulator could reset to non-emulated values in an
1766 // explicit method call (instead of in the destructor) that occurs when
1767 // emulation is disabled, but does not need to occur during RenderWidget
1768 // closing. Then we would not have to destroy this so carefully.
1769 screen_metrics_emulator_.reset();
1770
danakj5c5269b2018-11-06 00:39:111771 // Informs the WebWidget that compositor is being destroyed, so it can remove
1772 // references to it first.
1773 //
Albert J. Wong7bbf22d2018-12-20 00:27:271774 // When delegate() is present, the RenderWidget is for a main frame,
danakj5c5269b2018-11-06 00:39:111775 // and the GetWebWidget() is not the same as |webwidget_internal_|. However
1776 // that widget is responsible for doing WillCloseLayerTreeView() on the
1777 // |webwidget_internal_|, not us. Otherwise, they are the same and this is
1778 // notifying |webwidget_internal_|.
1779 GetWebWidget()->WillCloseLayerTreeView();
1780
Albert J. Wong7bbf22d2018-12-20 00:27:271781 // While the wrapping WebWidget from an delegate() is responsible for
danakj5c5269b2018-11-06 00:39:111782 // doing WillCloseLayerTreeView() on the |webwidget_internal_|, this class is
1783 // responsible for calling Close() on it. Notably, then, the wrapping
1784 // WebWidget does not.
1785 webwidget_internal_->Close();
1786 webwidget_internal_ = nullptr;
initial.commit09911bf2008-07-26 23:55:291787}
1788
Mitsuru Oshima3c07b332018-02-06 04:56:501789void RenderWidget::UpdateWebViewWithDeviceScaleFactor() {
1790 blink::WebFrameWidget* frame_widget = GetFrameWidget();
1791 blink::WebFrame* current_frame =
1792 frame_widget ? frame_widget->LocalRoot() : nullptr;
1793 blink::WebView* webview = current_frame ? current_frame->View() : nullptr;
1794 if (webview) {
danakj4b347212018-07-04 17:55:171795 if (compositor_deps_->IsUseZoomForDSFEnabled())
Fady Samuela863f152018-03-09 16:10:031796 webview->SetZoomFactorForDeviceScaleFactor(
1797 GetWebScreenInfo().device_scale_factor);
Mitsuru Oshima3c07b332018-02-06 04:56:501798 else
Fady Samuela863f152018-03-09 16:10:031799 webview->SetDeviceScaleFactor(GetWebScreenInfo().device_scale_factor);
Mitsuru Oshima3c07b332018-02-06 04:56:501800
1801 webview->GetSettings()->SetPreferCompositingToLCDTextEnabled(
Christopher Camerone9a30c12018-03-07 08:23:381802 PreferCompositingToLCDText(compositor_deps_,
Fady Samuela863f152018-03-09 16:10:031803 GetWebScreenInfo().device_scale_factor));
Mitsuru Oshima3c07b332018-02-06 04:56:501804 }
1805}
1806
Ehsan Karamad655d7b8a2018-01-12 18:38:381807blink::WebFrameWidget* RenderWidget::GetFrameWidget() const {
danakj89ae4532018-12-12 22:52:551808 // TODO(danakj): Remove this check and don't call this method for non-frames.
1809 if (!for_frame())
1810 return nullptr;
1811 // TODO(danakj): Is this needed? IPCs stop after closing, but code used to
1812 // check for a null WebWidget.
1813 if (closing_)
Ehsan Karamad655d7b8a2018-01-12 18:38:381814 return nullptr;
1815
danakj89ae4532018-12-12 22:52:551816 blink::WebWidget* widget;
Albert J. Wong7bbf22d2018-12-20 00:27:271817 if (delegate()) {
danakj89ae4532018-12-12 22:52:551818 // Main frame WebFrameWidgets are held by the delegate, the internal widget
1819 // points directly to the WebView.
1820 // TODO(ekaramad): We should drop IPCs when |is_frozen_| instead of
1821 // handling them and finding a null here. However there is also the case
1822 // of the frame being detached without the widget being frozen to be
1823 // resolved (https://crbug.com/906340). So for now this can return null.
Albert J. Wong7bbf22d2018-12-20 00:27:271824 widget = delegate()->GetWebWidgetForWidget();
danakj89ae4532018-12-12 22:52:551825 } else {
1826 // Subframes always have a WebFrameWidget themselves.
1827 widget = webwidget_internal_;
Ehsan Karamad655d7b8a2018-01-12 18:38:381828 }
danakj89ae4532018-12-12 22:52:551829 return static_cast<blink::WebFrameWidget*>(widget);
Ehsan Karamad655d7b8a2018-01-12 18:38:381830}
1831
bokan841fdc72016-10-06 00:16:351832void RenderWidget::ScreenRectToEmulatedIfNeeded(WebRect* window_rect) const {
1833 DCHECK(window_rect);
1834 float scale = popup_origin_scale_for_emulation_;
1835 if (!scale)
1836 return;
1837 window_rect->x =
1838 popup_view_origin_for_emulation_.x() +
1839 (window_rect->x - popup_screen_origin_for_emulation_.x()) / scale;
1840 window_rect->y =
1841 popup_view_origin_for_emulation_.y() +
1842 (window_rect->y - popup_screen_origin_for_emulation_.y()) / scale;
1843}
1844
1845void RenderWidget::EmulatedToScreenRectIfNeeded(WebRect* window_rect) const {
1846 DCHECK(window_rect);
1847 float scale = popup_origin_scale_for_emulation_;
1848 if (!scale)
1849 return;
1850 window_rect->x =
1851 popup_screen_origin_for_emulation_.x() +
1852 (window_rect->x - popup_view_origin_for_emulation_.x()) * scale;
1853 window_rect->y =
1854 popup_screen_origin_for_emulation_.y() +
1855 (window_rect->y - popup_view_origin_for_emulation_.y()) * scale;
1856}
1857
Blink Reformat1c4d759e2017-04-09 16:34:541858WebRect RenderWidget::WindowRect() {
bokan841fdc72016-10-06 00:16:351859 WebRect rect;
bokan6b08cd22016-10-05 00:55:211860 if (pending_window_rect_count_) {
1861 // NOTE(mbelshe): If there is a pending_window_rect_, then getting
1862 // the RootWindowRect is probably going to return wrong results since the
1863 // browser may not have processed the Move yet. There isn't really anything
1864 // good to do in this case, and it shouldn't happen - since this size is
1865 // only really needed for windowToScreen, which is only used for Popups.
bokan841fdc72016-10-06 00:16:351866 rect = pending_window_rect_;
1867 } else {
1868 rect = window_screen_rect_;
bokan6b08cd22016-10-05 00:55:211869 }
[email protected]2533ce12009-05-09 00:02:241870
bokan841fdc72016-10-06 00:16:351871 ScreenRectToEmulatedIfNeeded(&rect);
1872 return rect;
bokan6b08cd22016-10-05 00:55:211873}
1874
Blink Reformat1c4d759e2017-04-09 16:34:541875WebRect RenderWidget::ViewRect() {
Albert J. Wong3c93c182018-09-27 17:29:431876 WebRect rect = widget_screen_rect_;
bokan841fdc72016-10-06 00:16:351877 ScreenRectToEmulatedIfNeeded(&rect);
1878 return rect;
initial.commit09911bf2008-07-26 23:55:291879}
1880
Blink Reformat1c4d759e2017-04-09 16:34:541881void RenderWidget::SetToolTipText(const blink::WebString& text,
[email protected]8a9d6ca32011-06-06 20:11:301882 WebTextDirection hint) {
Albert J. Wong3c93c182018-09-27 17:29:431883 Send(new WidgetHostMsg_SetTooltipText(routing_id_, text.Utf16(), hint));
[email protected]8a9d6ca32011-06-06 20:11:301884}
1885
Blink Reformat1c4d759e2017-04-09 16:34:541886void RenderWidget::SetWindowRect(const WebRect& rect_in_screen) {
danakj81d601e62018-10-09 22:15:051887 // This path is for the renderer to change the on-screen position/size of
1888 // the widget by changing its window rect. This is not possible for
1889 // RenderWidgets whose position/size are controlled by layout from another
danakj6dcbc5962018-11-16 16:45:421890 // frame tree (ie. child local root frames), as the window rect can only be
1891 // set by the browser.
1892 if (for_child_local_root_frame_)
danakj81d601e62018-10-09 22:15:051893 return;
1894
oshima33ec97cd2015-12-14 19:40:241895 WebRect window_rect = rect_in_screen;
bokan841fdc72016-10-06 00:16:351896 EmulatedToScreenRectIfNeeded(&window_rect);
[email protected]b2e4c70132013-10-03 02:07:511897
danakj65f423b72018-10-10 17:21:201898 if (resizing_mode_selector_->is_synchronous_mode()) {
Kent Tamura21d1de62018-12-10 04:45:201899 // This is a web-test-only path. At one point, it was planned to be
danakj65f423b72018-10-10 17:21:201900 // removed. See https://crbug.com/309760.
bokanc007c3a2015-02-03 07:15:561901 SetWindowRectSynchronously(window_rect);
danakj65f423b72018-10-10 17:21:201902 return;
1903 }
1904
1905 if (show_callback_) {
1906 // The widget is not shown yet. Delay the |window_rect| being sent to the
1907 // browser until Show() is called so it can be sent with that IPC, once the
1908 // browser is ready for the info.
1909 initial_rect_ = window_rect;
1910 } else {
1911 Send(new WidgetHostMsg_RequestSetBounds(routing_id_, window_rect));
1912 SetPendingWindowRect(window_rect);
initial.commit09911bf2008-07-26 23:55:291913 }
1914}
1915
[email protected]2533ce12009-05-09 00:02:241916void RenderWidget::SetPendingWindowRect(const WebRect& rect) {
1917 pending_window_rect_ = rect;
1918 pending_window_rect_count_++;
[email protected]2533ce12009-05-09 00:02:241919
bokan6b08cd22016-10-05 00:55:211920 // Popups don't get size updates back from the browser so just store the set
1921 // values.
danakj89ae4532018-12-12 22:52:551922 if (!for_frame()) {
Blink Reformat1c4d759e2017-04-09 16:34:541923 window_screen_rect_ = rect;
Albert J. Wong3c93c182018-09-27 17:29:431924 widget_screen_rect_ = rect;
[email protected]2533ce12009-05-09 00:02:241925 }
[email protected]d4547452008-08-28 18:36:371926}
1927
wjmacleanee244e02017-05-26 21:06:071928void RenderWidget::OnShowContextMenu(ui::MenuSourceType source_type,
1929 const gfx::Point& location) {
wjmacleanee244e02017-05-26 21:06:071930 has_host_context_menu_location_ = true;
1931 host_context_menu_location_ = location;
1932 if (GetWebWidget()) {
1933 GetWebWidget()->ShowContextMenu(
1934 static_cast<blink::WebMenuSourceType>(source_type));
1935 }
1936 has_host_context_menu_location_ = false;
1937}
1938
[email protected]fa7b1dc2010-06-23 17:53:041939void RenderWidget::OnImeSetComposition(
[email protected]fcf75d42013-12-03 20:11:261940 const base::string16& text,
Ryan Landay9e42fd742017-08-12 01:59:111941 const std::vector<WebImeTextSpan>& ime_text_spans,
chongz7eb752802016-01-27 21:28:071942 const gfx::Range& replacement_range,
Ryan Landay9e42fd742017-08-12 01:59:111943 int selection_start,
1944 int selection_end) {
ekaramad5aff1942017-01-06 01:26:351945 if (!ShouldHandleImeEvents())
1946 return;
1947
brettw4b461082016-11-19 18:55:161948#if BUILDFLAG(ENABLE_PLUGINS)
EhsanK22d482e2017-08-10 17:29:491949 if (auto* plugin = GetFocusedPepperPluginInsideWidget()) {
Ryan Landay9e42fd742017-08-12 01:59:111950 plugin->render_frame()->OnImeSetComposition(text, ime_text_spans,
EhsanK22d482e2017-08-10 17:29:491951 selection_start, selection_end);
ekaramad2a46d632016-07-19 13:33:091952 return;
1953 }
1954#endif
[email protected]66fca5bc2013-05-23 06:58:291955 ImeEventGuard guard(this);
ekaramad2daaf672016-11-10 20:29:011956 blink::WebInputMethodController* controller = GetInputMethodController();
ekaramad2daaf672016-11-10 20:29:011957 if (!controller ||
Blink Reformat1c4d759e2017-04-09 16:34:541958 !controller->SetComposition(
Ryan Landay9e42fd742017-08-12 01:59:111959 WebString::FromUTF16(text), WebVector<WebImeTextSpan>(ime_text_spans),
ekaramadce32ef9f2017-02-09 17:33:561960 replacement_range.IsValid()
1961 ? WebRange(replacement_range.start(), replacement_range.length())
1962 : WebRange(),
1963 selection_start, selection_end)) {
[email protected]fa7b1dc2010-06-23 17:53:041964 // If we failed to set the composition text, then we need to let the browser
1965 // process to cancel the input method's ongoing composition session, to make
1966 // sure we are in a consistent state.
Dave Tapuska04bc5ee92018-04-17 19:03:311967 if (mojom::WidgetInputHandlerHost* host =
1968 widget_input_handler_manager_->GetWidgetInputHandlerHost()) {
1969 host->ImeCancelComposition();
Dave Tapuska139a72f2017-09-06 21:57:031970 }
[email protected]7f00efa2010-04-15 05:01:261971 }
nonafa291792016-08-10 02:36:181972 UpdateCompositionInfo(false /* not an immediate request */);
[email protected]fa7b1dc2010-06-23 17:53:041973}
1974
rlanday7efe2302017-01-11 00:14:281975void RenderWidget::OnImeCommitText(
1976 const base::string16& text,
Ryan Landay9e42fd742017-08-12 01:59:111977 const std::vector<WebImeTextSpan>& ime_text_spans,
rlanday7efe2302017-01-11 00:14:281978 const gfx::Range& replacement_range,
1979 int relative_cursor_pos) {
ekaramad5aff1942017-01-06 01:26:351980 if (!ShouldHandleImeEvents())
1981 return;
1982
brettw4b461082016-11-19 18:55:161983#if BUILDFLAG(ENABLE_PLUGINS)
EhsanK22d482e2017-08-10 17:29:491984 if (auto* plugin = GetFocusedPepperPluginInsideWidget()) {
1985 plugin->render_frame()->OnImeCommitText(text, replacement_range,
1986 relative_cursor_pos);
ekaramad2a46d632016-07-19 13:33:091987 return;
1988 }
1989#endif
[email protected]66fca5bc2013-05-23 06:58:291990 ImeEventGuard guard(this);
fsamuele8326c742016-01-12 00:49:391991 input_handler_->set_handling_input_event(true);
ekaramadb6483a052017-02-10 02:23:261992 if (auto* controller = GetInputMethodController()) {
Blink Reformat1c4d759e2017-04-09 16:34:541993 controller->CommitText(
Ryan Landay9e42fd742017-08-12 01:59:111994 WebString::FromUTF16(text), WebVector<WebImeTextSpan>(ime_text_spans),
ekaramadce32ef9f2017-02-09 17:33:561995 replacement_range.IsValid()
1996 ? WebRange(replacement_range.start(), replacement_range.length())
1997 : WebRange(),
1998 relative_cursor_pos);
ekaramadb6483a052017-02-10 02:23:261999 }
aelias87b8f7c2016-09-14 03:19:292000 input_handler_->set_handling_input_event(false);
2001 UpdateCompositionInfo(false /* not an immediate request */);
2002}
2003
2004void RenderWidget::OnImeFinishComposingText(bool keep_selection) {
ekaramad5aff1942017-01-06 01:26:352005 if (!ShouldHandleImeEvents())
2006 return;
2007
brettw4b461082016-11-19 18:55:162008#if BUILDFLAG(ENABLE_PLUGINS)
EhsanK22d482e2017-08-10 17:29:492009 if (auto* plugin = GetFocusedPepperPluginInsideWidget()) {
2010 plugin->render_frame()->OnImeFinishComposingText(keep_selection);
aelias87b8f7c2016-09-14 03:19:292011 return;
2012 }
2013#endif
2014
changwan38c3eb612016-12-09 01:45:562015 if (!GetWebWidget())
aelias87b8f7c2016-09-14 03:19:292016 return;
2017 ImeEventGuard guard(this);
2018 input_handler_->set_handling_input_event(true);
ekaramad2daaf672016-11-10 20:29:012019 if (auto* controller = GetInputMethodController()) {
Blink Reformat1c4d759e2017-04-09 16:34:542020 controller->FinishComposingText(
2021 keep_selection ? WebInputMethodController::kKeepSelection
2022 : WebInputMethodController::kDoNotKeepSelection);
ekaramad2daaf672016-11-10 20:29:012023 }
fsamuele8326c742016-01-12 00:49:392024 input_handler_->set_handling_input_event(false);
nonafa291792016-08-10 02:36:182025 UpdateCompositionInfo(false /* not an immediate request */);
initial.commit09911bf2008-07-26 23:55:292026}
2027
Christopher Camerone9a30c12018-03-07 08:23:382028void RenderWidget::UpdateSurfaceAndScreenInfo(
Fady Samuel1c1ad3692018-11-06 23:28:402029 const viz::LocalSurfaceIdAllocation& new_local_surface_id_allocation,
Christopher Cameronaad15a0d2018-03-09 03:47:332030 const gfx::Size& new_compositor_viewport_pixel_size,
Christopher Camerone9a30c12018-03-07 08:23:382031 const ScreenInfo& new_screen_info) {
Christopher Camerone9a30c12018-03-07 08:23:382032 bool orientation_changed =
2033 screen_info_.orientation_angle != new_screen_info.orientation_angle ||
2034 screen_info_.orientation_type != new_screen_info.orientation_type;
2035 bool web_device_scale_factor_changed =
2036 screen_info_.device_scale_factor != new_screen_info.device_scale_factor;
Fady Samuela863f152018-03-09 16:10:032037 ScreenInfo previous_original_screen_info = GetOriginalScreenInfo();
Christopher Cameronbe7ecea2018-02-27 00:47:192038
Fady Samuel1c1ad3692018-11-06 23:28:402039 local_surface_id_allocation_from_parent_ = new_local_surface_id_allocation;
Christopher Cameronaad15a0d2018-03-09 03:47:332040 compositor_viewport_pixel_size_ = new_compositor_viewport_pixel_size;
Christopher Camerone9a30c12018-03-07 08:23:382041 screen_info_ = new_screen_info;
Christopher Cameronbe7ecea2018-02-27 00:47:192042
Kinuko Yasudabef2b642019-01-24 04:53:332043 if (layer_tree_view_) {
2044 layer_tree_view_->SetViewportVisibleRect(ViewportVisibleRect());
2045 // Note carefully that the DSF specified in |new_screen_info| is not the
2046 // DSF used by the compositor during device emulation!
2047 layer_tree_view_->SetViewportSizeAndScale(
2048 compositor_viewport_pixel_size_,
2049 GetOriginalScreenInfo().device_scale_factor,
2050 local_surface_id_allocation_from_parent_);
2051 }
Christopher Cameronbe7ecea2018-02-27 00:47:192052
Christopher Camerone9a30c12018-03-07 08:23:382053 if (orientation_changed)
2054 OnOrientationChange();
Christopher Cameronbe7ecea2018-02-27 00:47:192055
Fady Samuela863f152018-03-09 16:10:032056 if (previous_original_screen_info != GetOriginalScreenInfo()) {
Christopher Camerone9a30c12018-03-07 08:23:382057 for (auto& observer : render_frame_proxies_)
Fady Samuela863f152018-03-09 16:10:032058 observer.OnScreenInfoChanged(GetOriginalScreenInfo());
Christopher Camerone9a30c12018-03-07 08:23:382059
2060 // Notify all embedded BrowserPlugins of the updated ScreenInfo.
2061 for (auto& observer : browser_plugins_)
Fady Samuela863f152018-03-09 16:10:032062 observer.ScreenInfoChanged(GetOriginalScreenInfo());
Christopher Camerone9a30c12018-03-07 08:23:382063 }
2064
2065 if (web_device_scale_factor_changed)
Christopher Cameronbe7ecea2018-02-27 00:47:192066 UpdateWebViewWithDeviceScaleFactor();
oshimad5279032015-12-16 18:22:332067}
2068
danakja2c9d0a92018-07-25 20:01:182069void RenderWidget::SetWindowRectSynchronously(
2070 const gfx::Rect& new_window_rect) {
2071 VisualProperties visual_properties;
2072 visual_properties.screen_info = screen_info_;
2073 visual_properties.new_size = new_window_rect.size();
2074 visual_properties.compositor_viewport_pixel_size = gfx::ScaleToCeiledSize(
2075 new_window_rect.size(), GetWebScreenInfo().device_scale_factor);
2076 visual_properties.visible_viewport_size = new_window_rect.size();
2077 visual_properties.is_fullscreen_granted = is_fullscreen_granted_;
2078 visual_properties.display_mode = display_mode_;
Fady Samuel84d8a1f72018-11-08 00:26:172079 visual_properties.local_surface_id_allocation =
2080 local_surface_id_allocation_from_parent_;
danakja2c9d0a92018-07-25 20:01:182081 // We are resizing the window from the renderer, so allocate a new
2082 // viz::LocalSurfaceId to avoid surface invariants violations in tests.
Kinuko Yasudabef2b642019-01-24 04:53:332083 if (layer_tree_view_)
2084 layer_tree_view_->RequestNewLocalSurfaceId();
danakja2c9d0a92018-07-25 20:01:182085 SynchronizeVisualProperties(visual_properties);
2086
Albert J. Wong3c93c182018-09-27 17:29:432087 widget_screen_rect_ = new_window_rect;
danakja2c9d0a92018-07-25 20:01:182088 window_screen_rect_ = new_window_rect;
danakj65f423b72018-10-10 17:21:202089 if (show_callback_) {
2090 // Tests may call here directly to control the window rect. If
2091 // Show() did not happen yet, the rect is stored to be passed to the
2092 // browser when the RenderWidget requests Show().
danakja2c9d0a92018-07-25 20:01:182093 initial_rect_ = new_window_rect;
danakj65f423b72018-10-10 17:21:202094 }
danakja2c9d0a92018-07-25 20:01:182095}
2096
Vladimir Levin98d76dad2018-04-21 00:21:292097void RenderWidget::UpdateCaptureSequenceNumber(
2098 uint32_t capture_sequence_number) {
2099 if (capture_sequence_number == last_capture_sequence_number_)
2100 return;
2101 last_capture_sequence_number_ = capture_sequence_number;
2102
2103 // Notify observers of the new capture sequence number.
2104 for (auto& observer : render_frame_proxies_)
2105 observer.UpdateCaptureSequenceNumber(capture_sequence_number);
2106 for (auto& observer : browser_plugins_)
2107 observer.UpdateCaptureSequenceNumber(capture_sequence_number);
2108}
2109
[email protected]4873c7d2009-07-16 06:36:282110void RenderWidget::OnSetTextDirection(WebTextDirection direction) {
EhsanK955ba582017-11-30 21:14:402111 if (auto* frame = GetFocusedWebLocalFrameInWidget())
2112 frame->SetTextDirection(direction);
[email protected]07f953332009-03-25 04:31:112113}
2114
Albert J. Wong3c93c182018-09-27 17:29:432115void RenderWidget::OnUpdateScreenRects(const gfx::Rect& widget_screen_rect,
[email protected]80ad8622012-11-07 16:33:032116 const gfx::Rect& window_screen_rect) {
[email protected]b2e4c70132013-10-03 02:07:512117 if (screen_metrics_emulator_) {
Albert J. Wong3c93c182018-09-27 17:29:432118 screen_metrics_emulator_->OnUpdateScreenRects(widget_screen_rect,
mfomitchev2600fd7c2016-02-17 20:53:392119 window_screen_rect);
[email protected]b2e4c70132013-10-03 02:07:512120 } else {
Albert J. Wong3c93c182018-09-27 17:29:432121 SetScreenRects(widget_screen_rect, window_screen_rect);
[email protected]b2e4c70132013-10-03 02:07:512122 }
Albert J. Wong3c93c182018-09-27 17:29:432123 Send(new WidgetHostMsg_UpdateScreenRects_ACK(routing_id()));
[email protected]80ad8622012-11-07 16:33:032124}
2125
kenrbea731792017-01-13 15:10:482126void RenderWidget::OnSetViewportIntersection(
Ken Buchananb2c9e262018-03-10 16:53:312127 const gfx::Rect& viewport_intersection,
Stefan Zager54e25832018-08-14 22:15:312128 const gfx::Rect& compositor_visible_rect,
2129 bool occluded_or_obscured) {
Ehsan Karamadf3ca2a82018-01-20 01:05:252130 if (auto* frame_widget = GetFrameWidget()) {
Ken Buchananb2c9e262018-03-10 16:53:312131 compositor_visible_rect_ = compositor_visible_rect;
Stefan Zager54e25832018-08-14 22:15:312132 frame_widget->SetRemoteViewportIntersection(viewport_intersection,
2133 occluded_or_obscured);
danakja6c10012018-07-06 14:25:362134 layer_tree_view_->SetViewportVisibleRect(ViewportVisibleRect());
kenrbea731792017-01-13 15:10:482135 }
2136}
2137
kenrb04323782017-06-23 01:23:322138void RenderWidget::OnSetIsInert(bool inert) {
danakj89ae4532018-12-12 22:52:552139 if (auto* frame_widget = GetFrameWidget())
Ehsan Karamadf3ca2a82018-01-20 01:05:252140 frame_widget->SetIsInert(inert);
kenrb04323782017-06-23 01:23:322141}
2142
sunxd540a9962018-05-24 22:51:062143void RenderWidget::OnSetInheritedEffectiveTouchAction(
2144 cc::TouchAction touch_action) {
danakj89ae4532018-12-12 22:52:552145 if (auto* frame_widget = GetFrameWidget())
sunxd540a9962018-05-24 22:51:062146 frame_widget->SetInheritedEffectiveTouchAction(touch_action);
sunxd540a9962018-05-24 22:51:062147}
2148
Ken Buchanan8a319fb2017-11-15 18:37:122149void RenderWidget::OnUpdateRenderThrottlingStatus(bool is_throttled,
2150 bool subtree_throttled) {
danakj89ae4532018-12-12 22:52:552151 if (auto* frame_widget = GetFrameWidget())
Ehsan Karamadf3ca2a82018-01-20 01:05:252152 frame_widget->UpdateRenderThrottlingStatus(is_throttled, subtree_throttled);
Ken Buchanan8a319fb2017-11-15 18:37:122153}
2154
paulmeyer90f6c31d2016-11-12 00:17:592155void RenderWidget::OnDragTargetDragEnter(
2156 const std::vector<DropData::Metadata>& drop_meta_data,
Ella Ge80a52dce2017-11-15 18:01:522157 const gfx::PointF& client_point,
2158 const gfx::PointF& screen_point,
paulmeyer90f6c31d2016-11-12 00:17:592159 WebDragOperationsMask ops,
2160 int key_modifiers) {
Ehsan Karamadf3ca2a82018-01-20 01:05:252161 blink::WebFrameWidget* frame_widget = GetFrameWidget();
2162 if (!frame_widget)
paulmeyer90f6c31d2016-11-12 00:17:592163 return;
2164
Ehsan Karamadf3ca2a82018-01-20 01:05:252165 WebDragOperation operation = frame_widget->DragTargetDragEnter(
2166 DropMetaDataToWebDragData(drop_meta_data), client_point, screen_point,
2167 ops, key_modifiers);
paulmeyer90f6c31d2016-11-12 00:17:592168
2169 Send(new DragHostMsg_UpdateDragCursor(routing_id(), operation));
2170}
2171
Ella Ge80a52dce2017-11-15 18:01:522172void RenderWidget::OnDragTargetDragOver(const gfx::PointF& client_point,
2173 const gfx::PointF& screen_point,
paulmeyer90f6c31d2016-11-12 00:17:592174 WebDragOperationsMask ops,
2175 int key_modifiers) {
Ehsan Karamadf3ca2a82018-01-20 01:05:252176 blink::WebFrameWidget* frame_widget = GetFrameWidget();
2177 if (!frame_widget)
paulmeyer90f6c31d2016-11-12 00:17:592178 return;
2179
Ehsan Karamadf3ca2a82018-01-20 01:05:252180 WebDragOperation operation = frame_widget->DragTargetDragOver(
2181 ConvertWindowPointToViewport(client_point), screen_point, ops,
2182 key_modifiers);
paulmeyer90f6c31d2016-11-12 00:17:592183
2184 Send(new DragHostMsg_UpdateDragCursor(routing_id(), operation));
2185}
2186
Ella Ge80a52dce2017-11-15 18:01:522187void RenderWidget::OnDragTargetDragLeave(const gfx::PointF& client_point,
2188 const gfx::PointF& screen_point) {
Ehsan Karamadf3ca2a82018-01-20 01:05:252189 blink::WebFrameWidget* frame_widget = GetFrameWidget();
2190 if (!frame_widget)
paulmeyer90f6c31d2016-11-12 00:17:592191 return;
Ehsan Karamadf3ca2a82018-01-20 01:05:252192
2193 frame_widget
Blink Reformat1c4d759e2017-04-09 16:34:542194 ->DragTargetDragLeave(ConvertWindowPointToViewport(client_point),
kenrb07c272802017-02-07 23:48:172195 screen_point);
paulmeyer90f6c31d2016-11-12 00:17:592196}
2197
2198void RenderWidget::OnDragTargetDrop(const DropData& drop_data,
Ella Ge80a52dce2017-11-15 18:01:522199 const gfx::PointF& client_point,
2200 const gfx::PointF& screen_point,
paulmeyer90f6c31d2016-11-12 00:17:592201 int key_modifiers) {
Ehsan Karamadf3ca2a82018-01-20 01:05:252202 blink::WebFrameWidget* frame_widget = GetFrameWidget();
2203 if (!frame_widget)
paulmeyer90f6c31d2016-11-12 00:17:592204 return;
2205
Ehsan Karamadf3ca2a82018-01-20 01:05:252206 frame_widget->DragTargetDrop(DropDataToWebDragData(drop_data),
2207 ConvertWindowPointToViewport(client_point),
2208 screen_point, key_modifiers);
paulmeyer90f6c31d2016-11-12 00:17:592209}
2210
Ella Ge80a52dce2017-11-15 18:01:522211void RenderWidget::OnDragSourceEnded(const gfx::PointF& client_point,
2212 const gfx::PointF& screen_point,
paulmeyer8fc8ea92016-11-15 05:12:212213 WebDragOperation op) {
Ehsan Karamadf3ca2a82018-01-20 01:05:252214 blink::WebFrameWidget* frame_widget = GetFrameWidget();
2215 if (!frame_widget)
paulmeyer8fc8ea92016-11-15 05:12:212216 return;
2217
Ehsan Karamadf3ca2a82018-01-20 01:05:252218 frame_widget->DragSourceEndedAt(ConvertWindowPointToViewport(client_point),
2219 screen_point, op);
paulmeyer8fc8ea92016-11-15 05:12:212220}
2221
2222void RenderWidget::OnDragSourceSystemDragEnded() {
danakj503545c2019-01-11 19:03:092223 blink::WebFrameWidget* frame_widget = GetFrameWidget();
2224 if (!frame_widget)
paulmeyer8fc8ea92016-11-15 05:12:212225 return;
2226
danakj503545c2019-01-11 19:03:092227 frame_widget->DragSourceSystemDragEnded();
paulmeyer8fc8ea92016-11-15 05:12:212228}
2229
Blink Reformat1c4d759e2017-04-09 16:34:542230void RenderWidget::ShowVirtualKeyboardOnElementFocus() {
okaa398f50b2017-03-21 06:30:262231#if defined(OS_CHROMEOS)
2232 // On ChromeOS, virtual keyboard is triggered only when users leave the
2233 // mouse button or the finger and a text input element is focused at that
2234 // time. Focus event itself shouldn't trigger virtual keyboard.
2235 UpdateTextInputState();
2236#else
changwan75e3b2072017-01-16 02:55:002237 ShowVirtualKeyboard();
okaa398f50b2017-03-21 06:30:262238#endif
boliu7b2be2f2016-11-04 04:58:312239
2240// TODO(rouslan): Fix ChromeOS and Windows 8 behavior of autofill popup with
2241// virtual keyboard.
2242#if !defined(OS_ANDROID)
2243 FocusChangeComplete();
2244#endif
[email protected]0d1ebed12013-08-05 22:01:132245}
2246
fsamuel72464894f2015-12-15 06:59:312247ui::TextInputType RenderWidget::GetTextInputType() {
brettw4b461082016-11-19 18:55:162248#if BUILDFLAG(ENABLE_PLUGINS)
EhsanK22d482e2017-08-10 17:29:492249 if (auto* plugin = GetFocusedPepperPluginInsideWidget())
2250 return plugin->text_input_type();
ekaramad2a46d632016-07-19 13:33:092251#endif
ekaramad5aff1942017-01-06 01:26:352252 if (auto* controller = GetInputMethodController())
Blink Reformat1c4d759e2017-04-09 16:34:542253 return ConvertWebTextInputType(controller->TextInputType());
fsamuel72464894f2015-12-15 06:59:312254 return ui::TEXT_INPUT_TYPE_NONE;
2255}
2256
nonafa291792016-08-10 02:36:182257void RenderWidget::UpdateCompositionInfo(bool immediate_request) {
2258 if (!monitor_composition_info_ && !immediate_request)
2259 return; // Do not calculate composition info if not requested.
nonadac0c7a2016-08-01 02:30:592260
nonafa291792016-08-10 02:36:182261 TRACE_EVENT0("renderer", "RenderWidget::UpdateCompositionInfo");
2262 gfx::Range range;
2263 std::vector<gfx::Rect> character_bounds;
2264
2265 if (GetTextInputType() == ui::TEXT_INPUT_TYPE_NONE) {
2266 // Composition information is only available on editable node.
2267 range = gfx::Range::InvalidRange();
2268 } else {
2269 GetCompositionRange(&range);
2270 GetCompositionCharacterBounds(&character_bounds);
2271 }
2272
2273 if (!immediate_request &&
2274 !ShouldUpdateCompositionInfo(range, character_bounds)) {
fsamuel72464894f2015-12-15 06:59:312275 return;
nonafa291792016-08-10 02:36:182276 }
fsamuel72464894f2015-12-15 06:59:312277 composition_character_bounds_ = character_bounds;
2278 composition_range_ = range;
Dave Tapuska04bc5ee92018-04-17 19:03:312279 if (mojom::WidgetInputHandlerHost* host =
2280 widget_input_handler_manager_->GetWidgetInputHandlerHost()) {
2281 host->ImeCompositionRangeChanged(composition_range_,
2282 composition_character_bounds_);
Dave Tapuska139a72f2017-09-06 21:57:032283 }
fsamuel72464894f2015-12-15 06:59:312284}
2285
Blink Reformat1c4d759e2017-04-09 16:34:542286void RenderWidget::ConvertViewportToWindow(blink::WebRect* rect) {
danakj4b347212018-07-04 17:55:172287 if (compositor_deps_->IsUseZoomForDSFEnabled()) {
Fady Samuela863f152018-03-09 16:10:032288 float reverse = 1 / GetOriginalScreenInfo().device_scale_factor;
oshimad5279032015-12-16 18:22:332289 // TODO(oshima): We may need to allow pixel precision here as the the
oshimaf866dab2015-12-05 00:41:542290 // anchor element can be placed at half pixel.
lfg15b235a32016-08-25 17:45:462291 gfx::Rect window_rect =
2292 gfx::ScaleToEnclosedRect(gfx::Rect(*rect), reverse);
2293 rect->x = window_rect.x();
2294 rect->y = window_rect.y();
2295 rect->width = window_rect.width();
2296 rect->height = window_rect.height();
oshimaf866dab2015-12-05 00:41:542297 }
2298}
2299
Blink Reformat1c4d759e2017-04-09 16:34:542300void RenderWidget::ConvertWindowToViewport(blink::WebFloatRect* rect) {
danakj4b347212018-07-04 17:55:172301 if (compositor_deps_->IsUseZoomForDSFEnabled()) {
Fady Samuela863f152018-03-09 16:10:032302 rect->x *= GetOriginalScreenInfo().device_scale_factor;
2303 rect->y *= GetOriginalScreenInfo().device_scale_factor;
2304 rect->width *= GetOriginalScreenInfo().device_scale_factor;
2305 rect->height *= GetOriginalScreenInfo().device_scale_factor;
oshimaa6985b62016-01-27 08:58:302306 }
2307}
2308
changwan8c342742016-02-26 00:53:392309void RenderWidget::OnRequestTextInputStateUpdate() {
Dave Tapuska9db80842017-07-24 17:24:262310#if defined(OS_ANDROID)
changwan8c342742016-02-26 00:53:392311 DCHECK(!ime_event_guard_);
2312 UpdateSelectionBounds();
changwan75e3b2072017-01-16 02:55:002313 UpdateTextInputStateInternal(false, true /* reply_to_request */);
[email protected]105dffb42013-02-20 03:46:212314#endif
Dave Tapuska9db80842017-07-24 17:24:262315}
[email protected]105dffb42013-02-20 03:46:212316
ekaramadc9b70a72017-03-23 16:14:232317void RenderWidget::OnRequestCompositionUpdates(bool immediate_request,
2318 bool monitor_updates) {
2319 monitor_composition_info_ = monitor_updates;
nonafa291792016-08-10 02:36:182320 if (!immediate_request)
2321 return;
2322 UpdateCompositionInfo(true /* immediate request */);
2323}
2324
[email protected]fcdc5642014-05-09 14:32:242325void RenderWidget::OnOrientationChange() {
Ehsan Karamad655d7b8a2018-01-12 18:38:382326 if (auto* frame_widget = GetFrameWidget()) {
Alex Moshchuk0b409c42017-09-13 02:09:002327 // LocalRoot() might return null for provisional main frames. In this case,
2328 // the frame hasn't committed a navigation and is not swapped into the tree
2329 // yet, so it doesn't make sense to send orientation change events to it.
2330 //
2331 // TODO(https://crbug.com/578349): This check should be cleaned up
2332 // once provisional frames are gone.
Ehsan Karamad655d7b8a2018-01-12 18:38:382333 if (frame_widget->LocalRoot())
2334 frame_widget->LocalRoot()->SendOrientationChangeEvent();
lfg8d649cc2017-04-28 18:04:302335 }
[email protected]fcdc5642014-05-09 14:32:242336}
2337
[email protected]bee16aab2009-08-26 15:55:032338void RenderWidget::SetHidden(bool hidden) {
2339 if (is_hidden_ == hidden)
2340 return;
2341
jdduke8fac9d102014-12-20 02:40:132342 // The status has changed. Tell the RenderThread about it and ensure
2343 // throttled acks are released in case frame production ceases.
[email protected]bee16aab2009-08-26 15:55:032344 is_hidden_ = hidden;
jdduke8fac9d102014-12-20 02:40:132345
Scott Violet1098538e2017-10-05 19:23:332346#if defined(USE_AURA)
Scott Violete46b3352018-09-22 00:37:072347 if (features::IsMultiProcessMash())
Scott Violet1098538e2017-10-05 19:23:332348 RendererWindowTreeClient::Get(routing_id_)->SetVisible(!hidden);
2349#endif
2350
Saman Sami58ed4392018-01-31 20:54:192351 // RenderThreadImpl::current() could be null in tests.
2352 if (RenderThreadImpl::current()) {
2353 if (is_hidden_) {
2354 RenderThreadImpl::current()->WidgetHidden();
2355 first_update_visual_state_after_hidden_ = true;
2356 } else {
2357 RenderThreadImpl::current()->WidgetRestored();
2358 }
Lei Zhang9b359d32017-11-28 00:57:022359 }
alexclarke7fa93942015-10-21 15:37:112360
2361 if (render_widget_scheduling_state_)
2362 render_widget_scheduling_state_->SetHidden(hidden);
danakj1b35e9d2018-11-07 19:09:282363
danakj57f139f2018-11-19 19:23:462364 // When the RenderWidget is frozen, visibility of the compositor is overridden
2365 // to always be hidden to prevent it from using resources that are not needed.
2366 // Unfortunately the main RenderWidget for a RenderView must be marked visible
2367 // even if the RenderView has a proxy main frame (and the RenderWidget is
2368 // frozen), in order for the RenderView to use the visibility signal from the
2369 // RenderWidget. This is bad. But we don't need to show the compositor to
2370 // satisfy that requirement.
2371 if (!is_frozen_)
2372 layer_tree_view_->SetVisible(!is_hidden_);
[email protected]bee16aab2009-08-26 15:55:032373}
2374
danakjf9284772019-01-23 18:29:142375void RenderWidget::SetIsFullscreen(bool fullscreen) {
2376 if (fullscreen == is_fullscreen_granted_)
[email protected]2b624c562011-10-27 22:58:262377 return;
danakjf9284772019-01-23 18:29:142378 is_fullscreen_granted_ = fullscreen;
mikhail.pozdnyakovf2c902a2015-04-14 08:09:122379 if (is_fullscreen_granted_) {
Blink Reformat1c4d759e2017-04-09 16:34:542380 GetWebWidget()->DidEnterFullscreen();
[email protected]2b624c562011-10-27 22:58:262381 } else {
Blink Reformat1c4d759e2017-04-09 16:34:542382 GetWebWidget()->DidExitFullscreen();
[email protected]2b624c562011-10-27 22:58:262383 }
[email protected]2b624c562011-10-27 22:58:262384}
2385
changwanf2a707b2015-10-30 08:22:162386void RenderWidget::OnImeEventGuardStart(ImeEventGuard* guard) {
2387 if (!ime_event_guard_)
2388 ime_event_guard_ = guard;
[email protected]66fca5bc2013-05-23 06:58:292389}
2390
changwanf2a707b2015-10-30 08:22:162391void RenderWidget::OnImeEventGuardFinish(ImeEventGuard* guard) {
Jeremy Roman0b261f42018-08-28 15:36:012392 if (ime_event_guard_ != guard)
changwanf2a707b2015-10-30 08:22:162393 return;
changwanf2a707b2015-10-30 08:22:162394 ime_event_guard_ = nullptr;
2395
[email protected]66fca5bc2013-05-23 06:58:292396 // While handling an ime event, text input state and selection bounds updates
2397 // are ignored. These must explicitly be updated once finished handling the
2398 // ime event.
2399 UpdateSelectionBounds();
[email protected]cb9e2632013-06-18 11:26:472400#if defined(OS_ANDROID)
changwan75e3b2072017-01-16 02:55:002401 if (guard->show_virtual_keyboard())
2402 ShowVirtualKeyboard();
2403 else
2404 UpdateTextInputState();
[email protected]cb9e2632013-06-18 11:26:472405#endif
[email protected]66fca5bc2013-05-23 06:58:292406}
2407
[email protected]7c8873e2013-02-05 08:03:012408void RenderWidget::GetSelectionBounds(gfx::Rect* focus, gfx::Rect* anchor) {
brettw4b461082016-11-19 18:55:162409#if BUILDFLAG(ENABLE_PLUGINS)
EhsanK22d482e2017-08-10 17:29:492410 if (auto* plugin = GetFocusedPepperPluginInsideWidget()) {
ekaramad2a46d632016-07-19 13:33:092411 // TODO(kinaba) http://crbug.com/101101
2412 // Current Pepper IME API does not handle selection bounds. So we simply
2413 // use the caret position as an empty range for now. It will be updated
2414 // after Pepper API equips features related to surrounding text retrieval.
EhsanK22d482e2017-08-10 17:29:492415 blink::WebRect caret(plugin->GetCaretBounds());
Blink Reformat1c4d759e2017-04-09 16:34:542416 ConvertViewportToWindow(&caret);
ekaramad2a46d632016-07-19 13:33:092417 *focus = caret;
2418 *anchor = caret;
2419 return;
2420 }
2421#endif
[email protected]7c8873e2013-02-05 08:03:012422 WebRect focus_webrect;
2423 WebRect anchor_webrect;
Blink Reformat1c4d759e2017-04-09 16:34:542424 GetWebWidget()->SelectionBounds(focus_webrect, anchor_webrect);
2425 ConvertViewportToWindow(&focus_webrect);
2426 ConvertViewportToWindow(&anchor_webrect);
oshima33ec97cd2015-12-14 19:40:242427 *focus = focus_webrect;
2428 *anchor = anchor_webrect;
[email protected]73bf95812011-10-12 11:38:322429}
2430
[email protected]e99ef6f2011-10-16 01:13:002431void RenderWidget::UpdateSelectionBounds() {
jdduke1aebad8e2015-07-22 23:25:082432 TRACE_EVENT0("renderer", "RenderWidget::UpdateSelectionBounds");
lfg8ff33912016-09-13 20:59:212433 if (!GetWebWidget())
[email protected]e99ef6f2011-10-16 01:13:002434 return;
changwanf2a707b2015-10-30 08:22:162435 if (ime_event_guard_)
[email protected]66fca5bc2013-05-23 06:58:292436 return;
[email protected]e99ef6f2011-10-16 01:13:002437
mohsenb0eeba72015-08-09 06:20:082438#if defined(USE_AURA)
2439 // TODO(mohsen): For now, always send explicit selection IPC notifications for
2440 // Aura beucause composited selection updates are not working for webview tags
2441 // which regresses IME inside webview. Remove this when composited selection
2442 // updates are fixed for webviews. See, http://crbug.com/510568.
2443 bool send_ipc = true;
2444#else
jddukeacf809e2014-09-23 20:38:382445 // With composited selection updates, the selection bounds will be reported
2446 // directly by the compositor, in which case explicit IPC selection
2447 // notifications should be suppressed.
mohsenb0eeba72015-08-09 06:20:082448 bool send_ipc =
Blink Reformat1c4d759e2017-04-09 16:34:542449 !blink::WebRuntimeFeatures::IsCompositedSelectionUpdateEnabled();
mohsenb0eeba72015-08-09 06:20:082450#endif
2451 if (send_ipc) {
Albert J. Wong3c93c182018-09-27 17:29:432452 WidgetHostMsg_SelectionBounds_Params params;
EhsanK955ba582017-11-30 21:14:402453 params.is_anchor_first = false;
jddukeacf809e2014-09-23 20:38:382454 GetSelectionBounds(&params.anchor_rect, &params.focus_rect);
2455 if (selection_anchor_rect_ != params.anchor_rect ||
2456 selection_focus_rect_ != params.focus_rect) {
2457 selection_anchor_rect_ = params.anchor_rect;
2458 selection_focus_rect_ = params.focus_rect;
EhsanK955ba582017-11-30 21:14:402459 if (auto* focused_frame = GetFocusedWebLocalFrameInWidget()) {
2460 focused_frame->SelectionTextDirection(params.focus_dir,
2461 params.anchor_dir);
2462 params.is_anchor_first = focused_frame->IsSelectionAnchorFirst();
2463 }
Albert J. Wong3c93c182018-09-27 17:29:432464 Send(new WidgetHostMsg_SelectionBoundsChanged(routing_id_, params));
jddukeacf809e2014-09-23 20:38:382465 }
[email protected]58b48a0d2012-06-13 07:01:352466 }
jddukeacf809e2014-09-23 20:38:382467
nonafa291792016-08-10 02:36:182468 UpdateCompositionInfo(false /* not an immediate request */);
[email protected]e99ef6f2011-10-16 01:13:002469}
2470
lfgb00fcad2016-07-14 14:16:332471void RenderWidget::DidAutoResize(const gfx::Size& new_size) {
2472 WebRect new_size_in_window(0, 0, new_size.width(), new_size.height());
Blink Reformat1c4d759e2017-04-09 16:34:542473 ConvertViewportToWindow(&new_size_in_window);
lfgb00fcad2016-07-14 14:16:332474 if (size_.width() != new_size_in_window.width ||
2475 size_.height() != new_size_in_window.height) {
2476 size_ = gfx::Size(new_size_in_window.width, new_size_in_window.height);
2477
2478 if (resizing_mode_selector_->is_synchronous_mode()) {
Blink Reformat1c4d759e2017-04-09 16:34:542479 gfx::Rect new_pos(WindowRect().x, WindowRect().y, size_.width(),
2480 size_.height());
Albert J. Wong3c93c182018-09-27 17:29:432481 widget_screen_rect_ = new_pos;
lfgb00fcad2016-07-14 14:16:332482 window_screen_rect_ = new_pos;
2483 }
2484
Christopher Camerone9a30c12018-03-07 08:23:382485 // TODO(ccameron): Note that this destroys any information differentiating
Christopher Cameronaad15a0d2018-03-09 03:47:332486 // |size_| from |compositor_viewport_pixel_size_|. Also note that the
2487 // calculation of |new_compositor_viewport_pixel_size| does not appear to
2488 // take into account device emulation.
Kinuko Yasudabef2b642019-01-24 04:53:332489 if (layer_tree_view_)
2490 layer_tree_view_->RequestNewLocalSurfaceId();
Christopher Cameronaad15a0d2018-03-09 03:47:332491 gfx::Size new_compositor_viewport_pixel_size =
Fady Samuela863f152018-03-09 16:10:032492 gfx::ScaleToCeiledSize(size_, GetWebScreenInfo().device_scale_factor);
Fady Samuel1c1ad3692018-11-06 23:28:402493 UpdateSurfaceAndScreenInfo(local_surface_id_allocation_from_parent_,
2494 new_compositor_viewport_pixel_size,
2495 screen_info_);
lfgb00fcad2016-07-14 14:16:332496 }
2497}
2498
[email protected]58b48a0d2012-06-13 07:01:352499void RenderWidget::GetCompositionCharacterBounds(
2500 std::vector<gfx::Rect>* bounds) {
2501 DCHECK(bounds);
2502 bounds->clear();
ekaramad2a46d632016-07-19 13:33:092503
brettw4b461082016-11-19 18:55:162504#if BUILDFLAG(ENABLE_PLUGINS)
EhsanK22d482e2017-08-10 17:29:492505 if (GetFocusedPepperPluginInsideWidget())
ekaramad2a46d632016-07-19 13:33:092506 return;
2507#endif
2508
Ehsan Karamad4975af6d2018-02-09 16:58:162509 blink::WebInputMethodController* controller = GetInputMethodController();
2510 if (!controller)
ekaramad2a46d632016-07-19 13:33:092511 return;
2512 blink::WebVector<blink::WebRect> bounds_from_blink;
Ehsan Karamad4975af6d2018-02-09 16:58:162513 if (!controller->GetCompositionCharacterBounds(bounds_from_blink))
ekaramad2a46d632016-07-19 13:33:092514 return;
2515
2516 for (size_t i = 0; i < bounds_from_blink.size(); ++i) {
Blink Reformat1c4d759e2017-04-09 16:34:542517 ConvertViewportToWindow(&bounds_from_blink[i]);
ekaramad2a46d632016-07-19 13:33:092518 bounds->push_back(bounds_from_blink[i]);
2519 }
[email protected]58b48a0d2012-06-13 07:01:352520}
2521
[email protected]db4fc1e2013-09-06 20:01:512522void RenderWidget::GetCompositionRange(gfx::Range* range) {
brettw4b461082016-11-19 18:55:162523#if BUILDFLAG(ENABLE_PLUGINS)
EhsanK22d482e2017-08-10 17:29:492524 if (GetFocusedPepperPluginInsideWidget())
ekaramad2a46d632016-07-19 13:33:092525 return;
2526#endif
EhsanKcdee7b72017-11-08 21:30:092527 blink::WebInputMethodController* controller = GetInputMethodController();
2528 WebRange web_range = controller ? controller->CompositionRange() : WebRange();
Blink Reformat1c4d759e2017-04-09 16:34:542529 if (web_range.IsNull()) {
nona2363a3d2016-11-09 03:26:212530 *range = gfx::Range::InvalidRange();
2531 return;
[email protected]88dbe32f2013-06-20 23:31:362532 }
Blink Reformat1c4d759e2017-04-09 16:34:542533 range->set_start(web_range.StartOffset());
2534 range->set_end(web_range.EndOffset());
[email protected]88dbe32f2013-06-20 23:31:362535}
2536
[email protected]501ea13d2013-07-09 17:03:292537bool RenderWidget::ShouldUpdateCompositionInfo(
[email protected]db4fc1e2013-09-06 20:01:512538 const gfx::Range& range,
[email protected]501ea13d2013-07-09 17:03:292539 const std::vector<gfx::Rect>& bounds) {
changwan2418e1b2016-12-12 12:43:082540 if (!range.IsValid())
2541 return false;
[email protected]501ea13d2013-07-09 17:03:292542 if (composition_range_ != range)
2543 return true;
2544 if (bounds.size() != composition_character_bounds_.size())
2545 return true;
2546 for (size_t i = 0; i < bounds.size(); ++i) {
2547 if (bounds[i] != composition_character_bounds_[i])
2548 return true;
2549 }
2550 return false;
2551}
[email protected]501ea13d2013-07-09 17:03:292552
[email protected]ad26ef42011-06-17 07:59:452553bool RenderWidget::CanComposeInline() {
brettw4b461082016-11-19 18:55:162554#if BUILDFLAG(ENABLE_PLUGINS)
EhsanK22d482e2017-08-10 17:29:492555 if (auto* plugin = GetFocusedPepperPluginInsideWidget())
2556 return plugin->IsPluginAcceptingCompositionEvents();
ekaramad2a46d632016-07-19 13:33:092557#endif
[email protected]ad26ef42011-06-17 07:59:452558 return true;
[email protected]56ea1a62011-05-30 07:05:572559}
2560
Blink Reformat1c4d759e2017-04-09 16:34:542561void RenderWidget::DidHandleGestureEvent(const WebGestureEvent& event,
2562 bool event_cancelled) {
danakja2c9d0a92018-07-25 20:01:182563 if (event_cancelled) {
Albert J. Wong7bbf22d2018-12-20 00:27:272564 // The delegate() doesn't need to hear about cancelled events.
[email protected]c68c3e4e2013-01-24 00:36:562565 return;
danakja2c9d0a92018-07-25 20:01:182566 }
2567
2568#if defined(OS_ANDROID) || defined(USE_AURA)
Blink Reformat1c4d759e2017-04-09 16:34:542569 if (event.GetType() == WebInputEvent::kGestureTap) {
changwan75e3b2072017-01-16 02:55:002570 ShowVirtualKeyboard();
Blink Reformat1c4d759e2017-04-09 16:34:542571 } else if (event.GetType() == WebInputEvent::kGestureLongPress) {
lfg8ff33912016-09-13 20:59:212572 DCHECK(GetWebWidget());
ekaramad5aff1942017-01-06 01:26:352573 blink::WebInputMethodController* controller = GetInputMethodController();
Blink Reformat1c4d759e2017-04-09 16:34:542574 if (!controller || controller->TextInputInfo().value.IsEmpty())
changwan75e3b2072017-01-16 02:55:002575 UpdateTextInputState();
[email protected]07c70d22014-08-21 08:33:462576 else
changwan75e3b2072017-01-16 02:55:002577 ShowVirtualKeyboard();
[email protected]c68c3e4e2013-01-24 00:36:562578 }
ekaramada110f642016-12-21 19:47:282579// TODO(ananta): Piggyback off existing IPCs to communicate this information,
2580// crbug/420130.
2581#if defined(OS_WIN)
danakja2c9d0a92018-07-25 20:01:182582 if (event.GetType() == blink::WebGestureEvent::kGestureTap) {
2583 // TODO(estade): hit test the event against focused node to make sure
2584 // the tap actually hit the focused node.
2585 blink::WebInputMethodController* controller = GetInputMethodController();
2586 blink::WebTextInputType text_input_type =
2587 controller ? controller->TextInputType() : blink::kWebTextInputTypeNone;
ekaramada110f642016-12-21 19:47:282588
Albert J. Wong3c93c182018-09-27 17:29:432589 Send(new WidgetHostMsg_FocusedNodeTouched(
danakja2c9d0a92018-07-25 20:01:182590 routing_id_, text_input_type != blink::kWebTextInputTypeNone));
2591 }
ekaramada110f642016-12-21 19:47:282592#endif
[email protected]c68c3e4e2013-01-24 00:36:562593#endif
danakja2c9d0a92018-07-25 20:01:182594
Albert J. Wong7bbf22d2018-12-20 00:27:272595 // The delegate() gets to respond to handling gestures last.
2596 if (delegate())
2597 delegate()->DidHandleGestureEventForWidget(event);
[email protected]c68c3e4e2013-01-24 00:36:562598}
2599
Blink Reformat1c4d759e2017-04-09 16:34:542600void RenderWidget::DidOverscroll(
bokane53a10f2016-04-13 23:48:312601 const blink::WebFloatSize& overscrollDelta,
2602 const blink::WebFloatSize& accumulatedOverscroll,
sataya.m582c9ce2015-06-09 08:03:422603 const blink::WebFloatPoint& position,
sunyunjiabbea8a92017-08-31 11:18:542604 const blink::WebFloatSize& velocity,
danakje819b622018-05-01 20:27:142605 const cc::OverscrollBehavior& behavior) {
bokan731ec382016-04-07 03:16:482606#if defined(OS_MACOSX)
2607 // On OSX the user can disable the elastic overscroll effect. If that's the
2608 // case, don't forward the overscroll notification.
2609 DCHECK(compositor_deps());
2610 if (!compositor_deps()->IsElasticOverscrollEnabled())
2611 return;
2612#endif
bokane53a10f2016-04-13 23:48:312613 input_handler_->DidOverscrollFromBlink(overscrollDelta, accumulatedOverscroll,
sunyunjiabbea8a92017-08-31 11:18:542614 position, velocity, behavior);
sataya.m582c9ce2015-06-09 08:03:422615}
2616
danakj4b347212018-07-04 17:55:172617// static
2618cc::LayerTreeSettings RenderWidget::GenerateLayerTreeSettings(
2619 CompositorDependencies* compositor_deps,
2620 bool is_for_subframe,
2621 const gfx::Size& initial_screen_size,
2622 float initial_device_scale_factor) {
2623 const bool is_threaded =
2624 !!compositor_deps->GetCompositorImplThreadTaskRunner();
2625
2626 const base::CommandLine& cmd = *base::CommandLine::ForCurrentProcess();
2627 cc::LayerTreeSettings settings;
2628
2629 settings.resource_settings.use_r16_texture =
2630 base::FeatureList::IsEnabled(media::kUseR16Texture);
2631
2632 settings.commit_to_active_tree = !is_threaded;
2633 settings.is_layer_tree_for_subframe = is_for_subframe;
2634
2635 // For web contents, layer transforms should scale up the contents of layers
2636 // to keep content always crisp when possible.
2637 settings.layer_transforms_should_scale_layer_contents = true;
2638
2639 settings.main_frame_before_activation_enabled =
2640 cmd.HasSwitch(cc::switches::kEnableMainFrameBeforeActivation);
2641
2642 // Checkerimaging is not supported for synchronous single-threaded mode, which
2643 // is what the renderer uses if its not threaded.
2644 settings.enable_checker_imaging =
2645 !cmd.HasSwitch(cc::switches::kDisableCheckerImaging) && is_threaded;
2646
2647#if defined(OS_ANDROID)
2648 // We can use a more aggressive limit on Android since decodes tend to take
2649 // longer on these devices.
2650 settings.min_image_bytes_to_checker = 512 * 1024; // 512kB
2651
2652 // Re-rasterization of checker-imaged content with software raster can be too
2653 // costly on Android.
2654 settings.only_checker_images_with_gpu_raster = true;
2655#endif
2656
2657 auto switch_value_as_int = [](const base::CommandLine& command_line,
2658 const std::string& switch_string, int min_value,
2659 int max_value, int* result) {
2660 std::string string_value = command_line.GetSwitchValueASCII(switch_string);
2661 int int_value;
2662 if (base::StringToInt(string_value, &int_value) && int_value >= min_value &&
2663 int_value <= max_value) {
2664 *result = int_value;
2665 return true;
2666 } else {
2667 LOG(WARNING) << "Failed to parse switch " << switch_string << ": "
2668 << string_value;
2669 return false;
2670 }
2671 };
2672
2673 int default_tile_size = 256;
2674#if defined(OS_ANDROID)
2675 const gfx::Size screen_size =
2676 gfx::ScaleToFlooredSize(initial_screen_size, initial_device_scale_factor);
2677 int display_width = screen_size.width();
2678 int display_height = screen_size.height();
2679 int numTiles = (display_width * display_height) / (256 * 256);
2680 if (numTiles > 16)
2681 default_tile_size = 384;
2682 if (numTiles >= 40)
2683 default_tile_size = 512;
2684
2685 // Adjust for some resolutions that barely straddle an extra
2686 // tile when in portrait mode. This helps worst case scroll/raster
2687 // by not needing a full extra tile for each row.
2688 constexpr int tolerance = 10; // To avoid rounding errors.
2689 int portrait_width = std::min(display_width, display_height);
2690 if (default_tile_size == 256 && std::abs(portrait_width - 768) < tolerance)
2691 default_tile_size += 32;
2692 if (default_tile_size == 384 && std::abs(portrait_width - 1200) < tolerance)
2693 default_tile_size += 32;
2694#elif defined(OS_CHROMEOS) || defined(OS_MACOSX)
2695 // Use 512 for high DPI (dsf=2.0f) devices.
2696 if (initial_device_scale_factor >= 2.0f)
2697 default_tile_size = 512;
2698#endif
2699
2700 // TODO(danakj): This should not be a setting O_O; it should change when the
2701 // device scale factor on LayerTreeHost changes.
2702 settings.default_tile_size = gfx::Size(default_tile_size, default_tile_size);
2703 if (cmd.HasSwitch(switches::kDefaultTileWidth)) {
2704 int tile_width = 0;
2705 switch_value_as_int(cmd, switches::kDefaultTileWidth, 1,
2706 std::numeric_limits<int>::max(), &tile_width);
2707 settings.default_tile_size.set_width(tile_width);
2708 }
2709 if (cmd.HasSwitch(switches::kDefaultTileHeight)) {
2710 int tile_height = 0;
2711 switch_value_as_int(cmd, switches::kDefaultTileHeight, 1,
2712 std::numeric_limits<int>::max(), &tile_height);
2713 settings.default_tile_size.set_height(tile_height);
2714 }
2715
2716 int max_untiled_layer_width = settings.max_untiled_layer_size.width();
2717 if (cmd.HasSwitch(switches::kMaxUntiledLayerWidth)) {
2718 switch_value_as_int(cmd, switches::kMaxUntiledLayerWidth, 1,
2719 std::numeric_limits<int>::max(),
2720 &max_untiled_layer_width);
2721 }
2722 int max_untiled_layer_height = settings.max_untiled_layer_size.height();
2723 if (cmd.HasSwitch(switches::kMaxUntiledLayerHeight)) {
2724 switch_value_as_int(cmd, switches::kMaxUntiledLayerHeight, 1,
2725 std::numeric_limits<int>::max(),
2726 &max_untiled_layer_height);
2727 }
2728
2729 settings.max_untiled_layer_size =
2730 gfx::Size(max_untiled_layer_width, max_untiled_layer_height);
2731
2732 settings.gpu_rasterization_msaa_sample_count =
2733 compositor_deps->GetGpuRasterizationMSAASampleCount();
2734 settings.gpu_rasterization_forced =
2735 compositor_deps->IsGpuRasterizationForced();
2736
2737 settings.can_use_lcd_text = compositor_deps->IsLcdTextEnabled();
2738 settings.use_zero_copy = compositor_deps->IsZeroCopyEnabled();
2739 settings.use_partial_raster = compositor_deps->IsPartialRasterEnabled();
2740 settings.enable_elastic_overscroll =
2741 compositor_deps->IsElasticOverscrollEnabled();
2742 settings.resource_settings.use_gpu_memory_buffer_resources =
2743 compositor_deps->IsGpuMemoryBufferCompositorResourcesEnabled();
2744 settings.use_painted_device_scale_factor =
2745 compositor_deps->IsUseZoomForDSFEnabled();
2746
2747 // Build LayerTreeSettings from command line args.
2748 if (cmd.HasSwitch(cc::switches::kBrowserControlsShowThreshold)) {
2749 std::string top_threshold_str =
2750 cmd.GetSwitchValueASCII(cc::switches::kBrowserControlsShowThreshold);
2751 double show_threshold;
2752 if (base::StringToDouble(top_threshold_str, &show_threshold) &&
2753 show_threshold >= 0.f && show_threshold <= 1.f)
2754 settings.top_controls_show_threshold = show_threshold;
2755 }
2756
2757 if (cmd.HasSwitch(cc::switches::kBrowserControlsHideThreshold)) {
2758 std::string top_threshold_str =
2759 cmd.GetSwitchValueASCII(cc::switches::kBrowserControlsHideThreshold);
2760 double hide_threshold;
2761 if (base::StringToDouble(top_threshold_str, &hide_threshold) &&
2762 hide_threshold >= 0.f && hide_threshold <= 1.f)
2763 settings.top_controls_hide_threshold = hide_threshold;
2764 }
2765
Philip Rogersc55ab6a2018-12-05 17:29:192766 // Blink sends cc a layer list and property trees when either
2767 // BlinkGenPropertyTrees or CompositeAfterPaint are enabled.
Philip Rogersfc2f1162018-10-17 18:28:312768 settings.use_layer_lists =
2769 blink::WebRuntimeFeatures::IsBlinkGenPropertyTreesEnabled() ||
Philip Rogersc55ab6a2018-12-05 17:29:192770 blink::WebRuntimeFeatures::IsCompositeAfterPaintEnabled();
danakj4b347212018-07-04 17:55:172771
David Bokan163b6a72018-10-25 22:03:492772 // Blink currently doesn't support setting fractional scroll offsets so CC
2773 // must send integer values. We plan to eventually make Blink use fractional
2774 // offsets internally: https://crbug.com/414283.
2775 settings.commit_fractional_scroll_deltas =
2776 blink::WebRuntimeFeatures::IsFractionalScrollOffsetsEnabled();
2777
danakj4b347212018-07-04 17:55:172778 // The means the renderer compositor has 2 possible modes:
2779 // - Threaded compositing with a scheduler.
Kent Tamura21d1de62018-12-10 04:45:202780 // - Single threaded compositing without a scheduler (for web tests only).
2781 // Using the scheduler in web tests introduces additional composite steps
danakj4b347212018-07-04 17:55:172782 // that create flakiness.
2783 settings.single_thread_proxy_scheduler = false;
2784
2785 // These flags should be mirrored by UI versions in ui/compositor/.
2786 if (cmd.HasSwitch(cc::switches::kShowCompositedLayerBorders))
2787 settings.initial_debug_state.show_debug_borders.set();
2788 settings.initial_debug_state.show_layer_animation_bounds_rects =
2789 cmd.HasSwitch(cc::switches::kShowLayerAnimationBounds);
2790 settings.initial_debug_state.show_paint_rects =
2791 cmd.HasSwitch(switches::kShowPaintRects);
2792 settings.initial_debug_state.show_property_changed_rects =
2793 cmd.HasSwitch(cc::switches::kShowPropertyChangedRects);
2794 settings.initial_debug_state.show_surface_damage_rects =
2795 cmd.HasSwitch(cc::switches::kShowSurfaceDamageRects);
2796 settings.initial_debug_state.show_screen_space_rects =
2797 cmd.HasSwitch(cc::switches::kShowScreenSpaceRects);
2798
2799 settings.initial_debug_state.SetRecordRenderingStats(
2800 cmd.HasSwitch(cc::switches::kEnableGpuBenchmarking));
2801 settings.enable_surface_synchronization =
2802 features::IsSurfaceSynchronizationEnabled();
2803 settings.build_hit_test_data = features::IsVizHitTestingSurfaceLayerEnabled();
2804
2805 if (cmd.HasSwitch(cc::switches::kSlowDownRasterScaleFactor)) {
2806 const int kMinSlowDownScaleFactor = 0;
2807 const int kMaxSlowDownScaleFactor = INT_MAX;
2808 switch_value_as_int(
2809 cmd, cc::switches::kSlowDownRasterScaleFactor, kMinSlowDownScaleFactor,
2810 kMaxSlowDownScaleFactor,
2811 &settings.initial_debug_state.slow_down_raster_scale_factor);
2812 }
2813
2814 // This is default overlay scrollbar settings for Android and DevTools mobile
2815 // emulator. Aura Overlay Scrollbar will override below.
2816 settings.scrollbar_animator = cc::LayerTreeSettings::ANDROID_OVERLAY;
2817 settings.solid_color_scrollbar_color = SkColorSetARGB(128, 128, 128, 128);
2818 settings.scrollbar_fade_delay = base::TimeDelta::FromMilliseconds(300);
2819 settings.scrollbar_fade_duration = base::TimeDelta::FromMilliseconds(300);
2820
2821#if defined(OS_ANDROID)
2822 bool using_synchronous_compositor =
2823 compositor_deps->UsingSynchronousCompositing();
2824 bool using_low_memory_policy = base::SysInfo::IsLowEndDevice();
2825
2826 settings.use_stream_video_draw_quad = true;
2827 settings.using_synchronous_renderer_compositor = using_synchronous_compositor;
2828 if (using_synchronous_compositor) {
2829 // Android WebView uses system scrollbars, so make ours invisible.
2830 // http://crbug.com/677348: This can't be done using hide_scrollbars
2831 // setting because supporting -webkit custom scrollbars is still desired
2832 // on sublayers.
2833 settings.scrollbar_animator = cc::LayerTreeSettings::NO_ANIMATOR;
2834 settings.solid_color_scrollbar_color = SK_ColorTRANSPARENT;
2835
2836 settings.enable_early_damage_check =
2837 cmd.HasSwitch(cc::switches::kCheckDamageEarly);
2838 }
2839 // Android WebView handles root layer flings itself.
2840 settings.ignore_root_layer_flings = using_synchronous_compositor;
2841 // Memory policy on Android WebView does not depend on whether device is
2842 // low end, so always use default policy.
2843 if (using_low_memory_policy && !using_synchronous_compositor) {
2844 // On low-end we want to be very carefull about killing other
2845 // apps. So initially we use 50% more memory to avoid flickering
2846 // or raster-on-demand.
2847 settings.max_memory_for_prepaint_percentage = 67;
2848 } else {
2849 // On other devices we have increased memory excessively to avoid
2850 // raster-on-demand already, so now we reserve 50% _only_ to avoid
2851 // raster-on-demand, and use 50% of the memory otherwise.
2852 settings.max_memory_for_prepaint_percentage = 50;
2853 }
2854
2855 // TODO(danakj): Only do this on low end devices.
2856 settings.create_low_res_tiling = true;
2857
2858#else // defined(OS_ANDROID)
2859 bool using_synchronous_compositor = false; // Only for Android WebView.
2860 // On desktop, we never use the low memory policy unless we are simulating
2861 // low-end mode via a switch.
2862 bool using_low_memory_policy =
2863 cmd.HasSwitch(switches::kEnableLowEndDeviceMode);
2864
2865 if (ui::IsOverlayScrollbarEnabled()) {
2866 settings.scrollbar_animator = cc::LayerTreeSettings::AURA_OVERLAY;
2867 settings.scrollbar_fade_delay = ui::kOverlayScrollbarFadeDelay;
2868 settings.scrollbar_fade_duration = ui::kOverlayScrollbarFadeDuration;
2869 settings.scrollbar_thinning_duration =
2870 ui::kOverlayScrollbarThinningDuration;
2871 settings.scrollbar_flash_after_any_scroll_update =
2872 ui::OverlayScrollbarFlashAfterAnyScrollUpdate();
2873 settings.scrollbar_flash_when_mouse_enter =
2874 ui::OverlayScrollbarFlashWhenMouseEnter();
2875 }
2876
2877 // On desktop, if there's over 4GB of memory on the machine, increase the
2878 // working set size to 256MB for both gpu and software.
2879 const int kImageDecodeMemoryThresholdMB = 4 * 1024;
2880 if (base::SysInfo::AmountOfPhysicalMemoryMB() >=
2881 kImageDecodeMemoryThresholdMB) {
2882 settings.decoded_image_working_set_budget_bytes = 256 * 1024 * 1024;
2883 } else {
2884 // This is the default, but recorded here as well.
2885 settings.decoded_image_working_set_budget_bytes = 128 * 1024 * 1024;
2886 }
2887#endif // defined(OS_ANDROID)
2888
2889 if (using_low_memory_policy) {
2890 // RGBA_4444 textures are only enabled:
2891 // - If the user hasn't explicitly disabled them
2892 // - If system ram is <= 512MB (1GB devices are sometimes low-end).
2893 // - If we are not running in a WebView, where 4444 isn't supported.
2894 if (!cmd.HasSwitch(switches::kDisableRGBA4444Textures) &&
2895 base::SysInfo::AmountOfPhysicalMemoryMB() <= 512 &&
2896 !using_synchronous_compositor) {
2897 settings.use_rgba_4444 = viz::RGBA_4444;
2898
2899 // If we are going to unpremultiply and dither these tiles, we need to
2900 // allocate an additional RGBA_8888 intermediate for each tile
2901 // rasterization when rastering to RGBA_4444 to allow for dithering.
2902 // Setting a reasonable sized max tile size allows this intermediate to
2903 // be consistently reused.
2904 if (base::FeatureList::IsEnabled(
2905 kUnpremultiplyAndDitherLowBitDepthTiles)) {
2906 settings.max_gpu_raster_tile_size = gfx::Size(512, 256);
2907 settings.unpremultiply_and_dither_low_bit_depth_tiles = true;
2908 }
2909 }
2910 }
2911
2912 if (cmd.HasSwitch(switches::kEnableLowResTiling))
2913 settings.create_low_res_tiling = true;
2914 if (cmd.HasSwitch(switches::kDisableLowResTiling))
2915 settings.create_low_res_tiling = false;
2916
2917 if (cmd.HasSwitch(switches::kEnableRGBA4444Textures) &&
2918 !cmd.HasSwitch(switches::kDisableRGBA4444Textures)) {
2919 settings.use_rgba_4444 = true;
2920 }
2921
2922 settings.max_staging_buffer_usage_in_bytes = 32 * 1024 * 1024; // 32MB
2923 // Use 1/4th of staging buffers on low-end devices.
2924 if (base::SysInfo::IsLowEndDevice())
2925 settings.max_staging_buffer_usage_in_bytes /= 4;
2926
2927 cc::ManagedMemoryPolicy defaults = settings.memory_policy;
2928 settings.memory_policy = GetGpuMemoryPolicy(defaults, initial_screen_size,
2929 initial_device_scale_factor);
2930
2931 settings.disallow_non_exact_resource_reuse =
2932 cmd.HasSwitch(switches::kDisallowNonExactResourceReuse);
2933#if defined(OS_ANDROID)
2934 // TODO(crbug.com/746931): This feature appears to be causing visual
2935 // corruption on certain android devices. Will investigate and re-enable.
2936 settings.disallow_non_exact_resource_reuse = true;
2937#endif
2938
2939 if (cmd.HasSwitch(switches::kRunAllCompositorStagesBeforeDraw)) {
2940 settings.wait_for_all_pipeline_stages_before_draw = true;
2941 settings.enable_latency_recovery = false;
2942 }
2943
2944 settings.enable_image_animation_resync =
2945 !cmd.HasSwitch(switches::kDisableImageAnimationResync);
2946
Karolina Soltys4d60b402018-10-01 19:28:202947 settings.send_compositor_frame_ack = false;
2948
danakj4b347212018-07-04 17:55:172949 return settings;
2950}
2951
2952// static
2953cc::ManagedMemoryPolicy RenderWidget::GetGpuMemoryPolicy(
2954 const cc::ManagedMemoryPolicy& default_policy,
2955 const gfx::Size& initial_screen_size,
2956 float initial_device_scale_factor) {
2957 cc::ManagedMemoryPolicy actual = default_policy;
2958 actual.bytes_limit_when_visible = 0;
2959
2960 // If the value was overridden on the command line, use the specified value.
2961 static bool client_hard_limit_bytes_overridden =
2962 base::CommandLine::ForCurrentProcess()->HasSwitch(
2963 switches::kForceGpuMemAvailableMb);
2964 if (client_hard_limit_bytes_overridden) {
2965 if (base::StringToSizeT(
2966 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
2967 switches::kForceGpuMemAvailableMb),
2968 &actual.bytes_limit_when_visible))
2969 actual.bytes_limit_when_visible *= 1024 * 1024;
2970 return actual;
2971 }
2972
2973#if defined(OS_ANDROID)
2974 // We can't query available GPU memory from the system on Android.
2975 // Physical memory is also mis-reported sometimes (eg. Nexus 10 reports
2976 // 1262MB when it actually has 2GB, while Razr M has 1GB but only reports
2977 // 128MB java heap size). First we estimate physical memory using both.
2978 size_t dalvik_mb = base::SysInfo::DalvikHeapSizeMB();
2979 size_t physical_mb = base::SysInfo::AmountOfPhysicalMemoryMB();
2980 size_t physical_memory_mb = 0;
2981 if (base::SysInfo::IsLowEndDevice()) {
2982 // TODO(crbug.com/742534): The code below appears to no longer work.
2983 // |dalvik_mb| no longer follows the expected heuristic pattern, causing us
2984 // to over-estimate memory on low-end devices. This entire section probably
2985 // needs to be re-written, but for now we can address the low-end Android
2986 // issues by ignoring |dalvik_mb|.
2987 physical_memory_mb = physical_mb;
2988 } else if (dalvik_mb >= 256) {
2989 physical_memory_mb = dalvik_mb * 4;
2990 } else {
2991 physical_memory_mb = std::max(dalvik_mb * 4, (physical_mb * 4) / 3);
2992 }
2993
2994 // Now we take a default of 1/8th of memory on high-memory devices,
2995 // and gradually scale that back for low-memory devices (to be nicer
2996 // to other apps so they don't get killed). Examples:
2997 // Nexus 4/10(2GB) 256MB (normally 128MB)
2998 // Droid Razr M(1GB) 114MB (normally 57MB)
2999 // Galaxy Nexus(1GB) 100MB (normally 50MB)
3000 // Xoom(1GB) 100MB (normally 50MB)
3001 // Nexus S(low-end) 8MB (normally 8MB)
3002 // Note that the compositor now uses only some of this memory for
3003 // pre-painting and uses the rest only for 'emergencies'.
3004 if (actual.bytes_limit_when_visible == 0) {
3005 // NOTE: Non-low-end devices use only 50% of these limits,
3006 // except during 'emergencies' where 100% can be used.
3007 if (physical_memory_mb >= 1536)
3008 actual.bytes_limit_when_visible = physical_memory_mb / 8; // >192MB
3009 else if (physical_memory_mb >= 1152)
3010 actual.bytes_limit_when_visible = physical_memory_mb / 8; // >144MB
3011 else if (physical_memory_mb >= 768)
3012 actual.bytes_limit_when_visible = physical_memory_mb / 10; // >76MB
3013 else if (physical_memory_mb >= 513)
3014 actual.bytes_limit_when_visible = physical_memory_mb / 12; // <64MB
3015 else
3016 // Devices with this little RAM have very little headroom so we hardcode
3017 // the limit rather than relying on the heuristics above. (They also use
3018 // 4444 textures so we can use a lower limit.)
3019 actual.bytes_limit_when_visible = 8;
3020
3021 actual.bytes_limit_when_visible =
3022 actual.bytes_limit_when_visible * 1024 * 1024;
3023 // Clamp the observed value to a specific range on Android.
3024 actual.bytes_limit_when_visible = std::max(
3025 actual.bytes_limit_when_visible, static_cast<size_t>(8 * 1024 * 1024));
3026 actual.bytes_limit_when_visible =
3027 std::min(actual.bytes_limit_when_visible,
3028 static_cast<size_t>(256 * 1024 * 1024));
3029 }
3030 actual.priority_cutoff_when_visible =
3031 gpu::MemoryAllocation::CUTOFF_ALLOW_EVERYTHING;
3032#else
3033 // Ignore what the system said and give all clients the same maximum
3034 // allocation on desktop platforms.
3035 actual.bytes_limit_when_visible = 512 * 1024 * 1024;
3036 actual.priority_cutoff_when_visible =
3037 gpu::MemoryAllocation::CUTOFF_ALLOW_NICE_TO_HAVE;
3038
3039 // For large monitors (4k), double the tile memory to avoid frequent out of
3040 // memory problems. 4k could mean a screen width of anywhere from 3840 to 4096
3041 // (see https://en.wikipedia.org/wiki/4K_resolution). We use 3500 as a proxy
3042 // for "large enough".
3043 static const int kLargeDisplayThreshold = 3500;
3044 int display_width =
3045 std::round(initial_screen_size.width() * initial_device_scale_factor);
3046 if (display_width >= kLargeDisplayThreshold)
3047 actual.bytes_limit_when_visible *= 2;
3048#endif
3049 return actual;
3050}
3051
Navid Zolghadr8fdb6114e2018-08-29 19:00:543052void RenderWidget::HasPointerRawMoveEventHandlers(bool has_handlers) {
3053 if (input_event_queue_)
3054 input_event_queue_->HasPointerRawMoveEventHandlers(has_handlers);
3055}
3056
Blink Reformat1c4d759e2017-04-09 16:34:543057void RenderWidget::HasTouchEventHandlers(bool has_handlers) {
W. James MacLean662d5382018-03-29 18:33:083058 if (has_touch_handlers_ && *has_touch_handlers_ == has_handlers)
3059 return;
3060
3061 has_touch_handlers_ = has_handlers;
alexclarke7fa93942015-10-21 15:37:113062 if (render_widget_scheduling_state_)
3063 render_widget_scheduling_state_->SetHasTouchHandler(has_handlers);
Albert J. Wongeb28e7e2018-10-12 00:57:433064 Send(new WidgetHostMsg_HasTouchEventHandlers(routing_id_, has_handlers));
[email protected]ce6689f2013-03-29 12:52:553065}
3066
Dave Tapuskadfe486c12017-06-09 16:53:133067void RenderWidget::SetNeedsLowLatencyInput(bool needs_low_latency) {
3068 if (input_event_queue_)
3069 input_event_queue_->SetNeedsLowLatency(needs_low_latency);
3070}
3071
Pavel Feldman6708eefa2019-01-17 03:14:323072void RenderWidget::SetNeedsUnbufferedInputForDebugger(bool unbuffered) {
3073 if (input_event_queue_)
3074 input_event_queue_->SetNeedsUnbufferedInputForDebugger(unbuffered);
3075}
3076
W. James MacLeand973a55b2018-11-29 21:39:133077void RenderWidget::AnimateDoubleTapZoomInMainFrame(
3078 const blink::WebPoint& point,
3079 const blink::WebRect& rect_to_zoom) {
3080 // Only oopif subframes should be sending this message.
Albert J. Wong7bbf22d2018-12-20 00:27:273081 DCHECK(!delegate());
W. James MacLeand973a55b2018-11-29 21:39:133082 Send(new WidgetHostMsg_AnimateDoubleTapZoomInMainFrame(routing_id(), point,
3083 rect_to_zoom));
3084}
3085
W. James MacLean5372eb72018-12-19 12:56:363086void RenderWidget::ZoomToFindInPageRectInMainFrame(
3087 const blink::WebRect& rect_to_zoom) {
3088 // Only oopif subframes should be sending this message.
Albert J. Wong7bbf22d2018-12-20 00:27:273089 DCHECK(!delegate_);
W. James MacLean5372eb72018-12-19 12:56:363090 Send(new WidgetHostMsg_ZoomToFindInPageRectInMainFrame(routing_id(),
3091 rect_to_zoom));
3092}
3093
Dave Tapuskabafc2ba32017-11-28 01:54:373094void RenderWidget::RequestUnbufferedInputEvents() {
3095 if (input_event_queue_)
3096 input_event_queue_->RequestUnbufferedInputEvents();
3097}
3098
xidachenfa0199e72017-05-11 11:34:263099void RenderWidget::SetTouchAction(cc::TouchAction touch_action) {
Dave Tapuskab28a1082017-08-30 15:37:263100 if (!input_handler_->ProcessTouchAction(touch_action))
[email protected]5d0bbdfa92013-12-10 00:35:513101 return;
3102
Dave Tapuska04bc5ee92018-04-17 19:03:313103 widget_input_handler_manager_->ProcessTouchAction(touch_action);
[email protected]5d0bbdfa92013-12-10 00:35:513104}
3105
[email protected]e3244ed2014-06-20 20:04:273106void RenderWidget::RegisterRenderFrameProxy(RenderFrameProxy* proxy) {
3107 render_frame_proxies_.AddObserver(proxy);
danakj0246bba02019-01-23 23:12:273108 // Page scale factor is propagated down the RenderWidget tree (across
3109 // frame trees). A new RenderFrameProxy means there is a new child
3110 // RenderWidget in another frame tree. In order for it to hear about
3111 // the page scale factor we pass along the last seen value here.
W. James MacLean2a90bff2018-11-05 20:52:473112 proxy->OnPageScaleFactorChanged(page_scale_factor_from_mainframe_);
[email protected]bffc8302014-01-23 20:52:163113}
3114
[email protected]e3244ed2014-06-20 20:04:273115void RenderWidget::UnregisterRenderFrameProxy(RenderFrameProxy* proxy) {
3116 render_frame_proxies_.RemoveObserver(proxy);
[email protected]bffc8302014-01-23 20:52:163117}
3118
[email protected]de3c5d82014-05-28 22:12:593119void RenderWidget::RegisterRenderFrame(RenderFrameImpl* frame) {
3120 render_frames_.AddObserver(frame);
3121}
3122
3123void RenderWidget::UnregisterRenderFrame(RenderFrameImpl* frame) {
3124 render_frames_.RemoveObserver(frame);
3125}
3126
Saman Samid189e5dfd2017-12-20 22:55:313127void RenderWidget::RegisterBrowserPlugin(BrowserPlugin* browser_plugin) {
3128 browser_plugins_.AddObserver(browser_plugin);
Saman Sami50d1e0c2018-03-13 20:03:493129 browser_plugin->ScreenInfoChanged(GetOriginalScreenInfo());
Saman Samid189e5dfd2017-12-20 22:55:313130}
3131
3132void RenderWidget::UnregisterBrowserPlugin(BrowserPlugin* browser_plugin) {
3133 browser_plugins_.RemoveObserver(browser_plugin);
3134}
3135
Albert J. Wong3c93c182018-09-27 17:29:433136void RenderWidget::OnWaitNextFrameForTests(
3137 int main_frame_thread_observer_routing_id) {
danakj53802692018-07-25 21:46:443138 // Sends an ACK to the browser process during the next compositor frame.
Albert J. Wong3c93c182018-09-27 17:29:433139 QueueMessage(new WidgetHostMsg_WaitForNextFrameForTests_ACK(
3140 main_frame_thread_observer_routing_id));
lfg43e08e62016-02-03 18:51:373141}
3142
Fady Samuela863f152018-03-09 16:10:033143const ScreenInfo& RenderWidget::GetWebScreenInfo() const {
3144 return screen_info_;
Christopher Camerone9a30c12018-03-07 08:23:383145}
3146
Fady Samuela863f152018-03-09 16:10:033147const ScreenInfo& RenderWidget::GetOriginalScreenInfo() const {
Christopher Camerone9a30c12018-03-07 08:23:383148 return screen_metrics_emulator_
3149 ? screen_metrics_emulator_->original_screen_info()
Fady Samuela863f152018-03-09 16:10:033150 : screen_info_;
oshima50872a72016-03-04 13:26:183151}
3152
Ella Ge80a52dce2017-11-15 18:01:523153gfx::PointF RenderWidget::ConvertWindowPointToViewport(
3154 const gfx::PointF& point) {
paulmeyer90f6c31d2016-11-12 00:17:593155 blink::WebFloatRect point_in_viewport(point.x(), point.y(), 0, 0);
Blink Reformat1c4d759e2017-04-09 16:34:543156 ConvertWindowToViewport(&point_in_viewport);
Ella Ge80a52dce2017-11-15 18:01:523157 return gfx::PointF(point_in_viewport.x, point_in_viewport.y);
3158}
3159
3160gfx::Point RenderWidget::ConvertWindowPointToViewport(const gfx::Point& point) {
3161 return gfx::ToRoundedPoint(ConvertWindowPointToViewport(gfx::PointF(point)));
paulmeyer90f6c31d2016-11-12 00:17:593162}
3163
Blink Reformat1c4d759e2017-04-09 16:34:543164bool RenderWidget::RequestPointerLock() {
lfgbee1e0a2016-06-08 21:24:213165 return mouse_lock_dispatcher_->LockMouse(webwidget_mouse_lock_target_.get());
3166}
3167
Blink Reformat1c4d759e2017-04-09 16:34:543168void RenderWidget::RequestPointerUnlock() {
lfgbee1e0a2016-06-08 21:24:213169 mouse_lock_dispatcher_->UnlockMouse(webwidget_mouse_lock_target_.get());
3170}
3171
Blink Reformat1c4d759e2017-04-09 16:34:543172bool RenderWidget::IsPointerLocked() {
lfgbee1e0a2016-06-08 21:24:213173 return mouse_lock_dispatcher_->IsMouseLockedTo(
3174 webwidget_mouse_lock_target_.get());
3175}
3176
Richard Li49fe04d2018-10-21 09:07:193177void RenderWidget::StartDragging(network::mojom::ReferrerPolicy policy,
paulmeyer6ef5a792016-11-08 20:33:583178 const WebDragData& data,
3179 WebDragOperationsMask mask,
danakj0c75ad82018-07-10 19:50:123180 const SkBitmap& drag_image,
danakjef1735a2018-11-13 19:34:443181 const gfx::Point& web_image_offset) {
3182 blink::WebRect offset_in_window(web_image_offset.x(), web_image_offset.y(), 0,
3183 0);
Blink Reformat1c4d759e2017-04-09 16:34:543184 ConvertViewportToWindow(&offset_in_window);
paulmeyer6ef5a792016-11-08 20:33:583185 DropData drop_data(DropDataBuilder::Build(data));
3186 drop_data.referrer_policy = policy;
danakj0c75ad82018-07-10 19:50:123187 gfx::Vector2d image_offset(offset_in_window.x, offset_in_window.y);
3188 Send(new DragHostMsg_StartDragging(routing_id(), drop_data, mask, drag_image,
3189 image_offset, possible_drag_event_info_));
paulmeyer6ef5a792016-11-08 20:33:583190}
3191
kenrb5d78b842017-03-06 21:06:013192uint32_t RenderWidget::GetContentSourceId() {
3193 return current_content_source_id_;
3194}
3195
Saman Samif7731342018-01-24 22:18:443196void RenderWidget::DidNavigate() {
Saman Sami26a1fcd2018-04-10 17:12:213197 ++current_content_source_id_;
Kinuko Yasudabef2b642019-01-24 04:53:333198 if (!layer_tree_view_)
3199 return;
danakja6c10012018-07-06 14:25:363200 layer_tree_view_->SetContentSourceId(current_content_source_id_);
3201 layer_tree_view_->ClearCachesOnNextCommit();
kenrb5d78b842017-03-06 21:06:013202}
3203
lfg8ff33912016-09-13 20:59:213204blink::WebWidget* RenderWidget::GetWebWidget() const {
Albert J. Wong7bbf22d2018-12-20 00:27:273205 if (delegate()) {
3206 blink::WebWidget* delegate_widget = delegate()->GetWebWidgetForWidget();
danakja2c9d0a92018-07-25 20:01:183207 if (delegate_widget)
3208 return delegate_widget;
3209 }
lfg8ff33912016-09-13 20:59:213210 return webwidget_internal_;
3211}
3212
ekaramad2daaf672016-11-10 20:29:013213blink::WebInputMethodController* RenderWidget::GetInputMethodController()
3214 const {
Ehsan Karamad655d7b8a2018-01-12 18:38:383215 if (auto* frame_widget = GetFrameWidget())
3216 return frame_widget->GetActiveWebInputMethodController();
3217
3218 return nullptr;
ekaramad2daaf672016-11-10 20:29:013219}
3220
Dave Tapuska139a72f2017-09-06 21:57:033221void RenderWidget::SetupWidgetInputHandler(
3222 mojom::WidgetInputHandlerRequest request,
3223 mojom::WidgetInputHandlerHostPtr host) {
Dave Tapuskab66c28f2017-11-15 17:18:473224 widget_input_handler_manager_->AddInterface(std::move(request),
3225 std::move(host));
Dave Tapuska525eb15e2017-08-17 21:05:503226}
3227
Dave Tapuska485aca92017-08-08 00:47:583228void RenderWidget::SetWidgetBinding(mojom::WidgetRequest request) {
3229 // Close the old binding if there was one.
3230 // A RenderWidgetHost should not need more than one channel.
3231 widget_binding_.Close();
3232 widget_binding_.Bind(std::move(request));
3233}
3234
Ken Buchanan94c0beb62018-06-22 19:56:243235void RenderWidget::SetMouseCapture(bool capture) {
3236 if (mojom::WidgetInputHandlerHost* host =
3237 widget_input_handler_manager_->GetWidgetInputHandlerHost()) {
3238 host->SetMouseCapture(capture);
3239 }
3240}
3241
danakj53802692018-07-25 21:46:443242void RenderWidget::SetWindowScreenRect(const gfx::Rect& window_screen_rect) {
3243 if (screen_metrics_emulator_)
3244 screen_metrics_emulator_->OnUpdateWindowScreenRect(window_screen_rect);
3245 else
3246 window_screen_rect_ = window_screen_rect;
3247}
3248
Fady Samuelca9ecb72018-05-05 05:59:273249bool RenderWidget::IsSurfaceSynchronizationEnabled() const {
danakja6c10012018-07-06 14:25:363250 return layer_tree_view_ &&
3251 layer_tree_view_->IsSurfaceSynchronizationEnabled();
Fady Samuelca9ecb72018-05-05 05:59:273252}
3253
W. James MacLean2a90bff2018-11-05 20:52:473254void RenderWidget::PageScaleFactorChanged(float page_scale_factor) {
danakj0246bba02019-01-23 23:12:273255 // The page scale is controlled by the WebView for the local main frame of
3256 // the Page. So this is called from blink by for the RenderWidget of that
3257 // local main frame. We forward the value on to each child RenderWidget (each
3258 // of which will be via proxy child frame). These will each in turn forward
3259 // the message to their child RenderWidgets (through their proxy child
3260 // frames).
3261 // TODO(crbug.com/924336): This value is continuously propagated during a
3262 // pinch-zoom, causing the child RenderWidgets to re-raster, while the main
3263 // frame is able to throttle re-raster to powers of 2. We could find some way
3264 // to throttle child RenderWidgets also, perhaps by informing them when the
3265 // pinch-zoom gesture is started and stopped.
3266 DCHECK(!is_frozen_);
3267 DCHECK(delegate());
W. James MacLean2a90bff2018-11-05 20:52:473268
W. James MacLean2a90bff2018-11-05 20:52:473269 for (auto& observer : render_frame_proxies_)
3270 observer.OnPageScaleFactorChanged(page_scale_factor);
danakj0246bba02019-01-23 23:12:273271 // Store the value to give to any new RenderFrameProxy that is registered.
3272 page_scale_factor_from_mainframe_ = page_scale_factor;
W. James MacLean2a90bff2018-11-05 20:52:473273}
3274
danakj53802692018-07-25 21:46:443275void RenderWidget::UseSynchronousResizeModeForTesting(bool enable) {
3276 resizing_mode_selector_->set_is_synchronous_mode(enable);
danakja2c9d0a92018-07-25 20:01:183277}
3278
3279void RenderWidget::SetDeviceScaleFactorForTesting(float factor) {
3280 device_scale_factor_for_testing_ = factor;
3281
3282 VisualProperties visual_properties;
3283 visual_properties.screen_info = screen_info_;
3284 visual_properties.screen_info.device_scale_factor = factor;
3285 visual_properties.new_size = size();
3286 visual_properties.visible_viewport_size = visible_viewport_size_;
3287 visual_properties.compositor_viewport_pixel_size =
3288 gfx::ScaleToCeiledSize(size(), factor);
3289 visual_properties.browser_controls_shrink_blink_size = false;
3290 visual_properties.top_controls_height = 0.f;
3291 visual_properties.is_fullscreen_granted = is_fullscreen_granted_;
3292 visual_properties.display_mode = display_mode_;
Fady Samuel84d8a1f72018-11-08 00:26:173293 visual_properties.local_surface_id_allocation =
3294 local_surface_id_allocation_from_parent_;
danakja2c9d0a92018-07-25 20:01:183295 // We are changing the device scale factor from the renderer, so allocate a
3296 // new viz::LocalSurfaceId to avoid surface invariants violations in tests.
Kinuko Yasudabef2b642019-01-24 04:53:333297 if (layer_tree_view_)
3298 layer_tree_view_->RequestNewLocalSurfaceId();
3299
danakja2c9d0a92018-07-25 20:01:183300 OnSynchronizeVisualProperties(visual_properties);
3301}
3302
3303void RenderWidget::SetDeviceColorSpaceForTesting(
3304 const gfx::ColorSpace& color_space) {
3305 VisualProperties visual_properties;
3306 visual_properties.screen_info = screen_info_;
3307 visual_properties.screen_info.color_space = color_space;
3308 visual_properties.new_size = size();
3309 visual_properties.visible_viewport_size = visible_viewport_size_;
3310 visual_properties.compositor_viewport_pixel_size =
3311 compositor_viewport_pixel_size_;
3312 visual_properties.browser_controls_shrink_blink_size = false;
3313 visual_properties.top_controls_height = 0.f;
3314 visual_properties.is_fullscreen_granted = is_fullscreen_granted_;
3315 visual_properties.display_mode = display_mode_;
Fady Samuel84d8a1f72018-11-08 00:26:173316 visual_properties.local_surface_id_allocation =
3317 local_surface_id_allocation_from_parent_;
danakja2c9d0a92018-07-25 20:01:183318 // We are changing the device color space from the renderer, so allocate a
3319 // new viz::LocalSurfaceId to avoid surface invariants violations in tests.
Kinuko Yasudabef2b642019-01-24 04:53:333320 if (layer_tree_view_)
danakja2c9d0a92018-07-25 20:01:183321 layer_tree_view_->RequestNewLocalSurfaceId();
3322 OnSynchronizeVisualProperties(visual_properties);
3323}
3324
3325void RenderWidget::SetWindowRectSynchronouslyForTesting(
3326 const gfx::Rect& new_window_rect) {
3327 SetWindowRectSynchronously(new_window_rect);
3328}
3329
3330void RenderWidget::EnableAutoResizeForTesting(const gfx::Size& min_size,
3331 const gfx::Size& max_size) {
3332 VisualProperties visual_properties;
3333 visual_properties.auto_resize_enabled = true;
3334 visual_properties.min_size_for_auto_resize = min_size;
3335 visual_properties.max_size_for_auto_resize = max_size;
Fady Samuel84d8a1f72018-11-08 00:26:173336 visual_properties.local_surface_id_allocation =
3337 base::Optional<viz::LocalSurfaceIdAllocation>(
3338 viz::LocalSurfaceIdAllocation(
3339 viz::LocalSurfaceId(1, 1, base::UnguessableToken::Create()),
3340 base::TimeTicks::Now()));
danakja2c9d0a92018-07-25 20:01:183341 OnSynchronizeVisualProperties(visual_properties);
3342}
3343
3344void RenderWidget::DisableAutoResizeForTesting(const gfx::Size& new_size) {
3345 if (!auto_resize_mode_)
3346 return;
3347
3348 VisualProperties visual_properties;
3349 visual_properties.auto_resize_enabled = false;
3350 visual_properties.screen_info = screen_info_;
3351 visual_properties.new_size = new_size;
3352 visual_properties.compositor_viewport_pixel_size =
3353 compositor_viewport_pixel_size_;
3354 visual_properties.browser_controls_shrink_blink_size =
3355 browser_controls_shrink_blink_size_;
3356 visual_properties.top_controls_height = top_controls_height_;
3357 visual_properties.visible_viewport_size = visible_viewport_size_;
3358 visual_properties.is_fullscreen_granted = is_fullscreen_granted_;
3359 visual_properties.display_mode = display_mode_;
danakja2c9d0a92018-07-25 20:01:183360 OnSynchronizeVisualProperties(visual_properties);
3361}
3362
EhsanK955ba582017-11-30 21:14:403363blink::WebLocalFrame* RenderWidget::GetFocusedWebLocalFrameInWidget() const {
Ehsan Karamad655d7b8a2018-01-12 18:38:383364 if (auto* frame_widget = GetFrameWidget())
3365 return frame_widget->FocusedWebLocalFrameInWidget();
3366 return nullptr;
EhsanK955ba582017-11-30 21:14:403367}
3368
EhsanK22d482e2017-08-10 17:29:493369#if BUILDFLAG(ENABLE_PLUGINS)
3370PepperPluginInstanceImpl* RenderWidget::GetFocusedPepperPluginInsideWidget() {
Ehsan Karamad655d7b8a2018-01-12 18:38:383371 blink::WebFrameWidget* frame_widget = GetFrameWidget();
3372 if (!frame_widget)
EhsanK22d482e2017-08-10 17:29:493373 return nullptr;
3374
3375 // Focused pepper instance might not always be in the focused frame. For
3376 // instance if a pepper instance and its embedder frame are focused an then
3377 // another frame takes focus using javascript, the embedder frame will no
3378 // longer be focused while the pepper instance is (the embedder frame's
3379 // |focused_pepper_plugin_| is not nullptr). Especially, if the pepper plugin
3380 // is fullscreen, clicking into the pepper will not refocus the embedder
3381 // frame. This is why we have to traverse the whole frame tree to find the
3382 // focused plugin.
Ehsan Karamad655d7b8a2018-01-12 18:38:383383 blink::WebFrame* current_frame = frame_widget->LocalRoot();
EhsanK22d482e2017-08-10 17:29:493384 while (current_frame) {
3385 RenderFrameImpl* render_frame =
3386 current_frame->IsWebLocalFrame()
3387 ? RenderFrameImpl::FromWebFrame(current_frame)
3388 : nullptr;
3389 if (render_frame && render_frame->focused_pepper_plugin())
3390 return render_frame->focused_pepper_plugin();
3391 current_frame = current_frame->TraverseNext();
3392 }
3393 return nullptr;
3394}
3395#endif
3396
Ken Buchananb2c9e262018-03-10 16:53:313397gfx::Rect RenderWidget::ViewportVisibleRect() {
danakj6dcbc5962018-11-16 16:45:423398 return for_child_local_root_frame_
3399 ? compositor_visible_rect_
3400 : gfx::Rect(compositor_viewport_pixel_size_);
Ken Buchananb2c9e262018-03-10 16:53:313401}
3402
Hajime Hoshi315a61f2018-08-14 17:27:283403// static
3404scoped_refptr<base::SingleThreadTaskRunner>
3405RenderWidget::GetCleanupTaskRunner() {
3406 return RenderThreadImpl::current_blink_platform_impl()
3407 ->main_thread_scheduler()
3408 ->CleanupTaskRunner();
3409}
3410
Saman Sami50d1e0c2018-03-13 20:03:493411base::WeakPtr<RenderWidget> RenderWidget::AsWeakPtr() {
3412 return weak_ptr_factory_.GetWeakPtr();
3413}
3414
[email protected]e9ff79c2012-10-19 21:31:263415} // namespace content