blob: 93ba70e0f483401c91b5313ee585359c843dbd73 [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"
Maksim Sisova33072d22019-01-24 15:44:25109#include "ui/base/clipboard/clipboard_constants.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.
Maksim Sisova33072d22019-01-24 15:44:25229 if (base::EqualsASCII(meta_data_item.mime_type, ui::kMimeTypeURIList)) {
Blink Reformat1c4d759e2017-04-09 16:34:54230 item.string_data = WebString::FromUTF8("about:dragdrop-placeholder");
paulmeyer90f6c31d2016-11-12 00:17:59231 }
232 item_list.push_back(item);
233 continue;
234 }
235
236 // TODO(hush): crbug.com/584789. Blink needs to support creating a file with
237 // just the mimetype. This is needed to drag files to WebView on Android
238 // platform.
239 if ((meta_data_item.kind == DropData::Kind::FILENAME) &&
240 !meta_data_item.filename.empty()) {
241 WebDragData::Item item;
Blink Reformat1c4d759e2017-04-09 16:34:54242 item.storage_type = WebDragData::Item::kStorageTypeFilename;
243 item.filename_data = blink::FilePathToWebString(meta_data_item.filename);
paulmeyer90f6c31d2016-11-12 00:17:59244 item_list.push_back(item);
245 continue;
246 }
247
248 if (meta_data_item.kind == DropData::Kind::FILESYSTEMFILE) {
249 WebDragData::Item item;
Blink Reformat1c4d759e2017-04-09 16:34:54250 item.storage_type = WebDragData::Item::kStorageTypeFileSystemFile;
251 item.file_system_url = meta_data_item.file_system_url;
paulmeyer90f6c31d2016-11-12 00:17:59252 item_list.push_back(item);
253 continue;
254 }
255 }
256
257 WebDragData result;
Blink Reformat1c4d759e2017-04-09 16:34:54258 result.Initialize();
259 result.SetItems(item_list);
paulmeyer90f6c31d2016-11-12 00:17:59260 return result;
261}
262
263WebDragData DropDataToWebDragData(const DropData& drop_data) {
264 std::vector<WebDragData::Item> item_list;
265
266 // These fields are currently unused when dragging into WebKit.
267 DCHECK(drop_data.download_metadata.empty());
268 DCHECK(drop_data.file_contents.empty());
dcheng3dd85612017-02-08 10:46:23269 DCHECK(drop_data.file_contents_content_disposition.empty());
paulmeyer90f6c31d2016-11-12 00:17:59270
271 if (!drop_data.text.is_null()) {
272 WebDragData::Item item;
Blink Reformat1c4d759e2017-04-09 16:34:54273 item.storage_type = WebDragData::Item::kStorageTypeString;
Maksim Sisova33072d22019-01-24 15:44:25274 item.string_type = WebString::FromUTF8(ui::kMimeTypeText);
Blink Reformat1c4d759e2017-04-09 16:34:54275 item.string_data = WebString::FromUTF16(drop_data.text.string());
paulmeyer90f6c31d2016-11-12 00:17:59276 item_list.push_back(item);
277 }
278
279 if (!drop_data.url.is_empty()) {
280 WebDragData::Item item;
Blink Reformat1c4d759e2017-04-09 16:34:54281 item.storage_type = WebDragData::Item::kStorageTypeString;
Maksim Sisova33072d22019-01-24 15:44:25282 item.string_type = WebString::FromUTF8(ui::kMimeTypeURIList);
Blink Reformat1c4d759e2017-04-09 16:34:54283 item.string_data = WebString::FromUTF8(drop_data.url.spec());
284 item.title = WebString::FromUTF16(drop_data.url_title);
paulmeyer90f6c31d2016-11-12 00:17:59285 item_list.push_back(item);
286 }
287
288 if (!drop_data.html.is_null()) {
289 WebDragData::Item item;
Blink Reformat1c4d759e2017-04-09 16:34:54290 item.storage_type = WebDragData::Item::kStorageTypeString;
Maksim Sisova33072d22019-01-24 15:44:25291 item.string_type = WebString::FromUTF8(ui::kMimeTypeHTML);
Blink Reformat1c4d759e2017-04-09 16:34:54292 item.string_data = WebString::FromUTF16(drop_data.html.string());
293 item.base_url = drop_data.html_base_url;
paulmeyer90f6c31d2016-11-12 00:17:59294 item_list.push_back(item);
295 }
296
jdoerrie5a73d0fa2018-10-02 23:54:05297 for (auto it = drop_data.filenames.begin(); it != drop_data.filenames.end();
paulmeyer90f6c31d2016-11-12 00:17:59298 ++it) {
299 WebDragData::Item item;
Blink Reformat1c4d759e2017-04-09 16:34:54300 item.storage_type = WebDragData::Item::kStorageTypeFilename;
301 item.filename_data = blink::FilePathToWebString(it->path);
302 item.display_name_data =
kinukoa4293022017-01-27 06:43:25303 blink::FilePathToWebString(base::FilePath(it->display_name));
paulmeyer90f6c31d2016-11-12 00:17:59304 item_list.push_back(item);
305 }
306
jdoerrie5a73d0fa2018-10-02 23:54:05307 for (auto it = drop_data.file_system_files.begin();
308 it != drop_data.file_system_files.end(); ++it) {
paulmeyer90f6c31d2016-11-12 00:17:59309 WebDragData::Item item;
Blink Reformat1c4d759e2017-04-09 16:34:54310 item.storage_type = WebDragData::Item::kStorageTypeFileSystemFile;
311 item.file_system_url = it->url;
312 item.file_system_file_size = it->size;
313 item.file_system_id = blink::WebString::FromASCII(it->filesystem_id);
paulmeyer90f6c31d2016-11-12 00:17:59314 item_list.push_back(item);
315 }
316
Stuart Langley48300392017-11-02 03:54:44317 for (const auto& it : drop_data.custom_data) {
paulmeyer90f6c31d2016-11-12 00:17:59318 WebDragData::Item item;
Blink Reformat1c4d759e2017-04-09 16:34:54319 item.storage_type = WebDragData::Item::kStorageTypeString;
Stuart Langley48300392017-11-02 03:54:44320 item.string_type = WebString::FromUTF16(it.first);
321 item.string_data = WebString::FromUTF16(it.second);
paulmeyer90f6c31d2016-11-12 00:17:59322 item_list.push_back(item);
323 }
324
325 WebDragData result;
Blink Reformat1c4d759e2017-04-09 16:34:54326 result.Initialize();
327 result.SetItems(item_list);
328 result.SetFilesystemId(WebString::FromUTF16(drop_data.filesystem_id));
paulmeyer90f6c31d2016-11-12 00:17:59329 return result;
330}
331
dtapuskac4dd5be2016-10-25 15:11:10332ui::TextInputType ConvertWebTextInputType(blink::WebTextInputType type) {
333 // Check the type is in the range representable by ui::TextInputType.
334 DCHECK_LE(type, static_cast<int>(ui::TEXT_INPUT_TYPE_MAX))
335 << "blink::WebTextInputType and ui::TextInputType not synchronized";
336 return static_cast<ui::TextInputType>(type);
337}
338
339ui::TextInputMode ConvertWebTextInputMode(blink::WebTextInputMode mode) {
340 // Check the mode is in the range representable by ui::TextInputMode.
341 DCHECK_LE(mode, static_cast<int>(ui::TEXT_INPUT_MODE_MAX))
342 << "blink::WebTextInputMode and ui::TextInputMode not synchronized";
343 return static_cast<ui::TextInputMode>(mode);
344}
345
Mitsuru Oshima3c07b332018-02-06 04:56:50346// Returns true if the device scale is high enough that losing subpixel
347// antialiasing won't have a noticeable effect on text quality.
348static bool DeviceScaleEnsuresTextQuality(float device_scale_factor) {
349#if defined(OS_ANDROID) || defined(OS_CHROMEOS)
350 // On Android, we never have subpixel antialiasing. On Chrome OS we prefer to
351 // composite all scrollers so that we get animated overlay scrollbars.
352 return true;
353#else
354 // 1.5 is a common touchscreen tablet device scale factor. For such
355 // devices main thread antialiasing is a heavy burden.
356 return device_scale_factor >= 1.5f;
357#endif
358}
359
360static bool PreferCompositingToLCDText(CompositorDependencies* compositor_deps,
361 float device_scale_factor) {
362 const base::CommandLine& command_line =
363 *base::CommandLine::ForCurrentProcess();
364 if (command_line.HasSwitch(switches::kDisablePreferCompositingToLCDText))
365 return false;
366 if (command_line.HasSwitch(switches::kEnablePreferCompositingToLCDText))
367 return true;
368 if (!compositor_deps->IsLcdTextEnabled())
369 return true;
370 return DeviceScaleEnsuresTextQuality(device_scale_factor);
371}
372
[email protected]b256eca2013-07-11 10:57:40373} // namespace
374
[email protected]b2e4c70132013-10-03 02:07:51375// RenderWidget ---------------------------------------------------------------
376
danakj829cdd142018-09-14 21:13:27377RenderWidget::RenderWidget(int32_t widget_routing_id,
378 CompositorDependencies* compositor_deps,
danakj829cdd142018-09-14 21:13:27379 const ScreenInfo& screen_info,
380 blink::WebDisplayMode display_mode,
danakj6dcbc5962018-11-16 16:45:42381 bool is_frozen,
danakj829cdd142018-09-14 21:13:27382 bool hidden,
383 bool never_visible,
384 mojom::WidgetRequest widget_request)
nick8331f8ad2016-11-15 20:42:45385 : routing_id_(widget_routing_id),
dcheng35d31c112015-07-22 00:17:36386 compositor_deps_(compositor_deps),
lfg8ff33912016-09-13 20:59:21387 webwidget_internal_(nullptr),
[email protected]847a2582013-03-09 02:29:51388 auto_resize_mode_(false),
[email protected]1ac10dca2013-08-20 20:47:04389 is_hidden_(hidden),
sievers71c62dd52015-10-07 01:44:39390 compositor_never_visible_(never_visible),
mikhail.pozdnyakovf2c902a2015-04-14 08:09:12391 is_fullscreen_granted_(false),
danakj73dd3032018-07-28 17:49:53392 display_mode_(display_mode),
changwanf2a707b2015-10-30 08:22:16393 ime_event_guard_(nullptr),
[email protected]661eb9d2009-02-03 02:11:48394 closing_(false),
danakj5c5269b2018-11-06 00:39:11395 host_will_close_this_(false),
danakj6dcbc5962018-11-16 16:45:42396 is_frozen_(is_frozen),
dglazkov97b6c2b2016-10-25 17:35:55397 text_input_type_(ui::TEXT_INPUT_TYPE_NONE),
[email protected]b256eca2013-07-11 10:57:40398 text_input_mode_(ui::TEXT_INPUT_MODE_DEFAULT),
shuchen82ce8c52014-10-23 01:55:20399 text_input_flags_(0),
AJITH KUMAR V041c0b02017-08-08 10:39:20400 next_previous_flags_(kInvalidNextPreviousFlagsValue),
[email protected]86ba5fcb2013-09-04 00:36:53401 can_compose_inline_(true),
nonafa291792016-08-10 02:36:18402 composition_range_(gfx::Range::InvalidRange()),
[email protected]867125a02009-12-10 06:01:48403 pending_window_rect_count_(0),
Scott Violetae08f332018-07-27 17:37:23404 screen_info_(screen_info),
nonafa291792016-08-10 02:36:18405 monitor_composition_info_(false),
[email protected]b2e4c70132013-10-03 02:07:51406 popup_origin_scale_for_emulation_(0.f),
samans26510e442017-06-01 22:29:12407 frame_swap_message_queue_(new FrameSwapMessageQueue(routing_id_)),
[email protected]a09d53ce2014-01-31 00:46:42408 resizing_mode_selector_(new ResizingModeSelector()),
lfge0c2792ec2016-05-11 18:52:08409 has_host_context_menu_location_(false),
ekaramad2a46d632016-07-19 13:33:09410 has_focus_(false),
danakj6dcbc5962018-11-16 16:45:42411 for_child_local_root_frame_(false),
ekaramad330ba4232016-09-23 17:57:47412#if defined(OS_MACOSX)
413 text_input_client_observer_(new TextInputClientObserver(this)),
414#endif
Takashi SAKAMOTO870f6262017-08-22 04:08:27415 first_update_visual_state_after_hidden_(false),
tasakb95dbb50c2017-02-08 18:07:50416 was_shown_time_(base::TimeTicks::Now()),
kenrb5d78b842017-03-06 21:06:01417 current_content_source_id_(0),
Dave Tapuska1bdf1832017-07-07 18:07:19418 widget_binding_(this, std::move(widget_request)),
danakjf87460792018-12-04 20:14:22419 warmup_weak_ptr_factory_(this),
wjmaclean1d970622017-01-21 22:28:24420 weak_ptr_factory_(this) {
nick8331f8ad2016-11-15 20:42:45421 DCHECK_NE(routing_id_, MSG_ROUTING_NONE);
[email protected]380244092011-10-07 17:26:27422 DCHECK(RenderThread::Get());
alexclarke7fa93942015-10-21 15:37:11423
424 // In tests there may not be a RenderThreadImpl.
425 if (RenderThreadImpl::current()) {
426 render_widget_scheduling_state_ = RenderThreadImpl::current()
Yuta Kitamura3331f5c2018-04-05 11:12:25427 ->GetWebMainThreadScheduler()
dcheng07945f632015-12-26 07:59:32428 ->NewRenderWidgetSchedulingState();
alexclarke7fa93942015-10-21 15:37:11429 render_widget_scheduling_state_->SetHidden(is_hidden_);
430 }
sadrul602ce1362017-01-26 06:41:10431#if defined(USE_AURA)
fsamuel098eade2017-03-21 18:06:14432 RendererWindowTreeClient::CreateIfNecessary(routing_id_);
Scott Violete46b3352018-09-22 00:37:07433 if (features::IsMultiProcessMash())
Scott Violet1098538e2017-10-05 19:23:33434 RendererWindowTreeClient::Get(routing_id_)->SetVisible(!is_hidden_);
sadrul602ce1362017-01-26 06:41:10435#endif
Albert J. Wongcb004632018-07-10 22:58:25436
437 if (routing_id_ != MSG_ROUTING_NONE)
438 g_routing_id_widget_map.Get().emplace(routing_id_, this);
initial.commit09911bf2008-07-26 23:55:29439}
440
441RenderWidget::~RenderWidget() {
lfg8ff33912016-09-13 20:59:21442 DCHECK(!webwidget_internal_) << "Leaking our WebWidget!";
Albert J. Wong7bbf22d2018-12-20 00:27:27443
Albert J. Wongcb004632018-07-10 22:58:25444 // TODO(ajwong): Add in check that routing_id_ has been removed from
445 // g_routing_id_widget_map once the shutdown semantics for RenderWidget
446 // and RenderViewImpl are rationalized. Currently, too many unit and
447 // browser tests delete a RenderWidget without correclty going through
448 // the shutdown. https://crbug.com/545684
[email protected]bffc8302014-01-23 20:52:16449
Kinuko Yasudabef2b642019-01-24 04:53:33450 if (input_event_queue_)
451 input_event_queue_->ClearClient();
452
sadrul8185d972017-01-31 18:30:11453#if defined(USE_AURA)
fsamuel098eade2017-03-21 18:06:14454 // It is possible for a RenderWidget to be destroyed before it was embedded
455 // in a mus window. The RendererWindowTreeClient will leak in such cases. So
456 // explicitly delete it here.
457 RendererWindowTreeClient::Destroy(routing_id_);
sadrul8185d972017-01-31 18:30:11458#endif
initial.commit09911bf2008-07-26 23:55:29459}
460
[email protected]484955942010-08-19 16:13:18461// static
Albert J. Wongcb004632018-07-10 22:58:25462RenderWidget* RenderWidget::FromRoutingID(int32_t routing_id) {
463 RoutingIDWidgetMap* widgets = g_routing_id_widget_map.Pointer();
jdoerrie5a73d0fa2018-10-02 23:54:05464 auto it = widgets->find(routing_id);
Albert J. Wongcb004632018-07-10 22:58:25465 return it == widgets->end() ? NULL : it->second;
466}
467
danakj25ec6e2b2018-09-26 17:01:31468void RenderWidget::InitForPopup(ShowCallback show_callback,
469 blink::WebPagePopup* web_page_popup) {
470 // Init() increments the reference count on |this|, making it
471 // self-referencing.
472 Init(std::move(show_callback), web_page_popup);
initial.commit09911bf2008-07-26 23:55:29473}
474
danakj6a816592018-09-25 18:30:56475void RenderWidget::InitForChildLocalRoot(
476 blink::WebFrameWidget* web_frame_widget) {
danakj6dcbc5962018-11-16 16:45:42477 for_child_local_root_frame_ = true;
danakj6a816592018-09-25 18:30:56478 // Init() increments the reference count on |this|, making it
479 // self-referencing.
480 Init(base::NullCallback(), web_frame_widget);
dchengda9b4bb2015-07-20 20:58:08481}
482
dchengda9b4bb2015-07-20 20:58:08483void RenderWidget::CloseForFrame() {
dchengd96a27a2015-07-24 20:17:32484 OnClose();
kenrba7199832015-01-22 23:44:59485}
486
Scott Violetae08f332018-07-27 17:37:23487void RenderWidget::Init(ShowCallback show_callback, WebWidget* web_widget) {
lfg8ff33912016-09-13 20:59:21488 DCHECK(!webwidget_internal_);
nick4df698d82016-11-11 20:39:23489 DCHECK_NE(routing_id_, MSG_ROUTING_NONE);
initial.commit09911bf2008-07-26 23:55:29490
danakj17216f4d2018-10-16 22:57:32491 RenderThreadImpl* render_thread_impl = RenderThreadImpl::current();
492
Jeremy Roman04f27c372017-10-27 15:20:55493 input_handler_ = std::make_unique<RenderWidgetInputHandler>(this, this);
nick8331f8ad2016-11-15 20:42:45494
danakj17216f4d2018-10-16 22:57:32495 LayerTreeView* layer_tree_view = InitializeLayerTreeView();
496 web_widget->SetLayerTreeView(layer_tree_view);
Dave Tapuska9db80842017-07-24 17:24:26497
danakj31699d5b2018-09-18 15:39:17498 blink::scheduler::WebThreadScheduler* main_thread_scheduler = nullptr;
499 if (render_thread_impl)
500 main_thread_scheduler = render_thread_impl->GetWebMainThreadScheduler();
Alexander Timin41e917d2018-07-02 16:25:35501 blink::scheduler::WebThreadScheduler* compositor_thread_scheduler =
502 blink::scheduler::WebThreadScheduler::CompositorThreadScheduler();
danakj31699d5b2018-09-18 15:39:17503 scoped_refptr<base::SingleThreadTaskRunner> compositor_input_task_runner;
danakj89ae4532018-12-12 22:52:55504 // Use the compositor thread task runner unless this is a popup or other such
505 // non-frame widgets. The |compositor_thread_scheduler| can be null in tests
506 // without a compositor thread.
507 if (for_frame() && compositor_thread_scheduler) {
508 compositor_input_task_runner =
509 compositor_thread_scheduler->InputTaskRunner();
danakj31699d5b2018-09-18 15:39:17510 }
Alexander Timin41e917d2018-07-02 16:25:35511
Dave Tapuska04bc5ee92018-04-17 19:03:31512 widget_input_handler_manager_ = WidgetInputHandlerManager::Create(
danakj31699d5b2018-09-18 15:39:17513 weak_ptr_factory_.GetWeakPtr(), std::move(compositor_input_task_runner),
514 main_thread_scheduler);
Dave Tapuska9db80842017-07-24 17:24:26515
danakjdf1ceb72018-07-18 20:02:25516 show_callback_ = std::move(show_callback);
initial.commit09911bf2008-07-26 23:55:29517
lfg8ff33912016-09-13 20:59:21518 webwidget_internal_ = web_widget;
519 webwidget_mouse_lock_target_.reset(
520 new WebWidgetLockTarget(webwidget_internal_));
lfgbee1e0a2016-06-08 21:24:21521 mouse_lock_dispatcher_.reset(new RenderWidgetMouseLockDispatcher(this));
initial.commit09911bf2008-07-26 23:55:29522
nick4df698d82016-11-11 20:39:23523 RenderThread::Get()->AddRoute(routing_id_, this);
524 // Take a reference on behalf of the RenderThread. This will be balanced
Albert J. Wong3c93c182018-09-27 17:29:43525 // when we receive WidgetMsg_Close.
nick4df698d82016-11-11 20:39:23526 AddRef();
527 if (RenderThreadImpl::current()) {
528 RenderThreadImpl::current()->WidgetCreated();
529 if (is_hidden_)
530 RenderThreadImpl::current()->WidgetHidden();
initial.commit09911bf2008-07-26 23:55:29531 }
532}
533
danakj53802692018-07-25 21:46:44534void RenderWidget::ApplyEmulatedScreenMetricsForPopupWidget(
535 RenderWidget* origin_widget) {
536 RenderWidgetScreenMetricsEmulator* emulator =
537 origin_widget->screen_metrics_emulator_.get();
538 if (!emulator)
539 return;
[email protected]b2e4c70132013-10-03 02:07:51540 popup_origin_scale_for_emulation_ = emulator->scale();
[email protected]19193682014-04-03 15:01:43541 popup_view_origin_for_emulation_ = emulator->applied_widget_rect().origin();
Pavel Feldmancc099f72017-07-20 23:09:00542 popup_screen_origin_for_emulation_ =
Lei Zhang9b359d32017-11-28 00:57:02543 emulator->original_screen_rect().origin();
Fady Samuel1c1ad3692018-11-06 23:28:40544 UpdateSurfaceAndScreenInfo(local_surface_id_allocation_from_parent_,
545 compositor_viewport_pixel_size_,
546 emulator->original_screen_info());
[email protected]b2e4c70132013-10-03 02:07:51547}
548
[email protected]2d6836f42014-07-02 17:25:31549gfx::Rect RenderWidget::AdjustValidationMessageAnchor(const gfx::Rect& anchor) {
550 if (screen_metrics_emulator_)
551 return screen_metrics_emulator_->AdjustValidationMessageAnchor(anchor);
552 return anchor;
553}
554
thakis18e426412017-03-15 12:06:37555#if BUILDFLAG(USE_EXTERNAL_POPUP_MENU)
[email protected]b2e4c70132013-10-03 02:07:51556void RenderWidget::SetExternalPopupOriginAdjustmentsForEmulation(
Albert J. Wong7bbf22d2018-12-20 00:27:27557 ExternalPopupMenu* popup) {
558 if (screen_metrics_emulator_)
559 popup->SetOriginScaleForEmulation(screen_metrics_emulator_->scale());
[email protected]b2e4c70132013-10-03 02:07:51560}
[email protected]53907862014-03-25 15:42:40561#endif
[email protected]b2e4c70132013-10-03 02:07:51562
563void RenderWidget::OnShowHostContextMenu(ContextMenuParams* params) {
564 if (screen_metrics_emulator_)
565 screen_metrics_emulator_->OnShowContextMenu(params);
566}
567
[email protected]a95986a82010-12-24 06:19:28568bool RenderWidget::OnMessageReceived(const IPC::Message& message) {
ekaramad330ba4232016-09-23 17:57:47569#if defined(OS_MACOSX)
570 if (IPC_MESSAGE_CLASS(message) == TextInputClientMsgStart)
571 return text_input_client_observer_->OnMessageReceived(message);
572#endif
lfgbee1e0a2016-06-08 21:24:21573 if (mouse_lock_dispatcher_ &&
574 mouse_lock_dispatcher_->OnMessageReceived(message))
575 return true;
576
[email protected]a95986a82010-12-24 06:19:28577 bool handled = true;
578 IPC_BEGIN_MESSAGE_MAP(RenderWidget, message)
Albert J. Wong3c93c182018-09-27 17:29:43579 IPC_MESSAGE_HANDLER(WidgetMsg_ShowContextMenu, OnShowContextMenu)
580 IPC_MESSAGE_HANDLER(WidgetMsg_Close, OnClose)
581 IPC_MESSAGE_HANDLER(WidgetMsg_SynchronizeVisualProperties,
Fady Samuel799e72192018-04-25 21:16:57582 OnSynchronizeVisualProperties)
Albert J. Wong3c93c182018-09-27 17:29:43583 IPC_MESSAGE_HANDLER(WidgetMsg_EnableDeviceEmulation,
dgozman9260b0a12015-03-16 13:45:20584 OnEnableDeviceEmulation)
Albert J. Wong3c93c182018-09-27 17:29:43585 IPC_MESSAGE_HANDLER(WidgetMsg_DisableDeviceEmulation,
dgozman9260b0a12015-03-16 13:45:20586 OnDisableDeviceEmulation)
Albert J. Wong3c93c182018-09-27 17:29:43587 IPC_MESSAGE_HANDLER(WidgetMsg_WasHidden, OnWasHidden)
588 IPC_MESSAGE_HANDLER(WidgetMsg_WasShown, OnWasShown)
589 IPC_MESSAGE_HANDLER(WidgetMsg_SetActive, OnSetActive)
Albert J. Wong3c93c182018-09-27 17:29:43590 IPC_MESSAGE_HANDLER(WidgetMsg_SetTextDirection, OnSetTextDirection)
591 IPC_MESSAGE_HANDLER(WidgetMsg_SetBounds_ACK, OnRequestSetBoundsAck)
592 IPC_MESSAGE_HANDLER(WidgetMsg_UpdateScreenRects, OnUpdateScreenRects)
593 IPC_MESSAGE_HANDLER(WidgetMsg_ForceRedraw, OnForceRedraw)
594 IPC_MESSAGE_HANDLER(WidgetMsg_SetViewportIntersection,
kenrbea731792017-01-13 15:10:48595 OnSetViewportIntersection)
Albert J. Wong3c93c182018-09-27 17:29:43596 IPC_MESSAGE_HANDLER(WidgetMsg_SetIsInert, OnSetIsInert)
597 IPC_MESSAGE_HANDLER(WidgetMsg_SetInheritedEffectiveTouchAction,
sunxd540a9962018-05-24 22:51:06598 OnSetInheritedEffectiveTouchAction)
Albert J. Wong3c93c182018-09-27 17:29:43599 IPC_MESSAGE_HANDLER(WidgetMsg_UpdateRenderThrottlingStatus,
Ken Buchanan8a319fb2017-11-15 18:37:12600 OnUpdateRenderThrottlingStatus)
Albert J. Wong3c93c182018-09-27 17:29:43601 IPC_MESSAGE_HANDLER(WidgetMsg_WaitForNextFrameForTests,
lfg43e08e62016-02-03 18:51:37602 OnWaitNextFrameForTests)
paulmeyer90f6c31d2016-11-12 00:17:59603 IPC_MESSAGE_HANDLER(DragMsg_TargetDragEnter, OnDragTargetDragEnter)
604 IPC_MESSAGE_HANDLER(DragMsg_TargetDragOver, OnDragTargetDragOver)
605 IPC_MESSAGE_HANDLER(DragMsg_TargetDragLeave, OnDragTargetDragLeave)
606 IPC_MESSAGE_HANDLER(DragMsg_TargetDrop, OnDragTargetDrop)
paulmeyer8fc8ea92016-11-15 05:12:21607 IPC_MESSAGE_HANDLER(DragMsg_SourceEnded, OnDragSourceEnded)
608 IPC_MESSAGE_HANDLER(DragMsg_SourceSystemDragEnded,
609 OnDragSourceSystemDragEnded)
[email protected]a95986a82010-12-24 06:19:28610 IPC_MESSAGE_UNHANDLED(handled = false)
611 IPC_END_MESSAGE_MAP()
612 return handled;
613}
initial.commit09911bf2008-07-26 23:55:29614
615bool RenderWidget::Send(IPC::Message* message) {
Nasko Oskovf97e8b02017-07-25 02:45:42616 // Don't send any messages after the browser has told us to close, and filter
danakj6dcbc5962018-11-16 16:45:42617 // most outgoing messages when frozen.
618 if (closing_) {
619 delete message;
620 return false;
621 }
622 if (is_frozen_ && !SwappedOutMessages::CanSendWhileSwappedOut(message)) {
initial.commit09911bf2008-07-26 23:55:29623 delete message;
624 return false;
625 }
626
627 // If given a messsage without a routing ID, then assign our routing ID.
628 if (message->routing_id() == MSG_ROUTING_NONE)
629 message->set_routing_id(routing_id_);
630
[email protected]380244092011-10-07 17:26:27631 return RenderThread::Get()->Send(message);
[email protected]8085dbc82008-09-26 22:53:44632}
633
dtapuska014ed082016-11-11 21:58:48634void RenderWidget::SendOrCrash(IPC::Message* message) {
635 bool result = Send(message);
636 CHECK(closing_ || result) << "Failed to send message";
637}
638
ekaramad5aff1942017-01-06 01:26:35639bool RenderWidget::ShouldHandleImeEvents() const {
Albert J. Wong7bbf22d2018-12-20 00:27:27640 if (delegate())
danakj89ae4532018-12-12 22:52:55641 return has_focus_;
642 if (for_child_local_root_frame_) {
643 // TODO(ekaramad): We track page focus in all RenderViews on the page but
644 // the RenderWidgets corresponding to child local roots do not get the
645 // update. For now, this method returns true when the RenderWidget is for a
646 // child local frame, i.e., IME events will be processed regardless of page
647 // focus. We should revisit this after page focus for OOPIFs has been fully
648 // resolved (https://crbug.com/689777).
649 return true;
650 }
651 // Not a frame widget.
652 return false;
ekaramad5aff1942017-01-06 01:26:35653}
654
initial.commit09911bf2008-07-26 23:55:29655void RenderWidget::OnClose() {
dchengd96a27a2015-07-24 20:17:32656 DCHECK(content::RenderThread::Get());
657 if (closing_)
658 return;
danakj292b63e2018-11-19 20:47:06659 for (auto& observer : render_frames_)
660 observer.WidgetWillClose();
dchengd96a27a2015-07-24 20:17:32661 closing_ = true;
662
663 // Browser correspondence is no longer needed at this point.
664 if (routing_id_ != MSG_ROUTING_NONE) {
665 RenderThread::Get()->RemoveRoute(routing_id_);
Albert J. Wongcb004632018-07-10 22:58:25666 g_routing_id_widget_map.Get().erase(routing_id_);
danakj678f0252018-11-09 21:46:34667 if (RenderThreadImpl::current()) {
668 // RenderWidgets may be hidden when they are closed. If we were previously
669 // hidden, we are being counted as such in RenderThreadImpl. Thus we
670 // remove that count here by calling WidgetRestored() even though we're
671 // clearly not becoming visible here.
672 if (is_hidden_)
673 RenderThreadImpl::current()->WidgetRestored();
dchengd96a27a2015-07-24 20:17:32674 RenderThreadImpl::current()->WidgetDestroyed();
danakj678f0252018-11-09 21:46:34675 }
dchengd96a27a2015-07-24 20:17:32676 }
677
danakj6dcbc5962018-11-16 16:45:42678 if (for_child_local_root_frame_) {
dchengd96a27a2015-07-24 20:17:32679 // Widgets for frames may be created and closed at any time while the frame
Ken Buchananed449bb2018-02-01 21:02:05680 // is alive. However, WebWidget must be closed synchronously because frame
681 // widgets and frames hold pointers to each other. The deferred call to
682 // Close() will complete cleanup and release |this|, but CloseWebWidget()
683 // prevents Close() from attempting to access members of an
684 // already-deleted frame.
685 CloseWebWidget();
dchengd96a27a2015-07-24 20:17:32686 }
Ken Buchananed449bb2018-02-01 21:02:05687 // If there is a Send call on the stack, then it could be dangerous to close
688 // now. Post a task that only gets invoked when there are no nested message
689 // loops.
Hajime Hoshi315a61f2018-08-14 17:27:28690 GetCleanupTaskRunner()->PostNonNestableTask(
691 FROM_HERE, base::BindOnce(&RenderWidget::Close, this));
dchengd96a27a2015-07-24 20:17:32692
693 // Balances the AddRef taken when we called AddRoute.
694 Release();
initial.commit09911bf2008-07-26 23:55:29695}
696
Fady Samuel799e72192018-04-25 21:16:57697void RenderWidget::OnSynchronizeVisualProperties(
danakja2c9d0a92018-07-25 20:01:18698 const VisualProperties& original_params) {
699 TRACE_EVENT0("renderer", "RenderWidget::OnSynchronizeVisualProperties");
EhsanKbd2cea992017-11-23 18:49:08700
danakja2c9d0a92018-07-25 20:01:18701 VisualProperties params = original_params;
Albert J. Wong7bbf22d2018-12-20 00:27:27702 if (delegate()) {
danakj84bbf552019-01-21 21:33:31703 if (size_ != params.new_size) {
704 // Only hide popups when the size changes. Eg https://crbug.com/761908.
705 delegate()->CancelPagePopupForWidget();
706 }
danakja2c9d0a92018-07-25 20:01:18707
708 if (display_mode_ != params.display_mode) {
709 display_mode_ = params.display_mode;
Albert J. Wong7bbf22d2018-12-20 00:27:27710 delegate()->ApplyNewDisplayModeForWidget(params.display_mode);
danakja2c9d0a92018-07-25 20:01:18711 }
712
713 bool auto_resize_mode_changed =
714 auto_resize_mode_ != params.auto_resize_enabled;
715 auto_resize_mode_ = params.auto_resize_enabled;
716 min_size_for_auto_resize_ = params.min_size_for_auto_resize;
717 max_size_for_auto_resize_ = params.max_size_for_auto_resize;
718
719 if (auto_resize_mode_) {
720 gfx::Size min_auto_size = min_size_for_auto_resize_;
721 gfx::Size max_auto_size = max_size_for_auto_resize_;
722 if (compositor_deps_->IsUseZoomForDSFEnabled()) {
723 min_auto_size = gfx::ScaleToCeiledSize(
724 min_auto_size, params.screen_info.device_scale_factor);
725 max_auto_size = gfx::ScaleToCeiledSize(
726 max_auto_size, params.screen_info.device_scale_factor);
727 }
Albert J. Wong7bbf22d2018-12-20 00:27:27728 delegate()->ApplyAutoResizeLimitsForWidget(min_auto_size, max_auto_size);
danakja2c9d0a92018-07-25 20:01:18729 } else if (auto_resize_mode_changed) {
Albert J. Wong7bbf22d2018-12-20 00:27:27730 delegate()->DisableAutoResizeForWidget();
danakja2c9d0a92018-07-25 20:01:18731 if (params.new_size.IsEmpty())
732 return;
733 }
734
735 browser_controls_shrink_blink_size_ =
736 params.browser_controls_shrink_blink_size;
737 top_controls_height_ = params.top_controls_height;
738 bottom_controls_height_ = params.bottom_controls_height;
739
740 // TODO(fsamuel): Modifies the |params| that are used for
741 // SynchronizeVisualProperties(), but does not used this modified
742 // device_scale_factor for the auto-resize values, which is probably a bug.
743 if (device_scale_factor_for_testing_) {
744 params.screen_info.device_scale_factor =
745 *device_scale_factor_for_testing_;
746 params.compositor_viewport_pixel_size = gfx::ScaleToCeiledSize(
747 params.new_size, params.screen_info.device_scale_factor);
748 }
[email protected]b2e4c70132013-10-03 02:07:51749 }
750
danakja2c9d0a92018-07-25 20:01:18751 if (!resizing_mode_selector_->ShouldAbortOnResize(this, params)) {
752 if (screen_metrics_emulator_) {
753 screen_metrics_emulator_->OnSynchronizeVisualProperties(params);
754 } else {
danakj0246bba02019-01-23 23:12:27755 if (!delegate()) {
756 // The main frame controls the page scale factor, from blink. For other
757 // frame widgets, the page scale is received from its parent as part of
758 // the visual properties here. While blink doesn't need to know this
759 // page scale factor outside the main frame, the compositor does in
760 // order to produce its output at the correct scale.
761 layer_tree_view_->SetExternalPageScaleFactor(params.page_scale_factor);
762 // Store the value to give to any new RenderFrameProxy that is
763 // registered.
764 page_scale_factor_from_mainframe_ = params.page_scale_factor;
765 // Push the page scale factor down to any child RenderWidgets via our
766 // child proxy frames.
767 // TODO(danakj): This ends up setting the page scale factor in the
768 // RenderWidgetHost of the child RenderWidget, so that it can bounce
769 // the value down to its RenderWidget. Since this is essentially a
770 // global value per-page, we could instead store it once in the browser
771 // (such as in RenderViewHost) and distribute it to each frame-hosted
772 // RenderWidget from there.
773 for (auto& child_proxy : render_frame_proxies_)
774 child_proxy.OnPageScaleFactorChanged(params.page_scale_factor);
775 }
776
danakja2c9d0a92018-07-25 20:01:18777 gfx::Size old_visible_viewport_size = visible_viewport_size_;
778 SynchronizeVisualProperties(params);
779 if (old_visible_viewport_size != visible_viewport_size_) {
780 for (auto& render_frame : render_frames_)
781 render_frame.DidChangeVisibleViewport();
782 }
783 }
Fady Samuel799e72192018-04-25 21:16:57784 }
785
Albert J. Wong7bbf22d2018-12-20 00:27:27786 if (delegate() && params.scroll_focused_node_into_view)
787 delegate()->ScrollFocusedNodeIntoViewForWidget();
initial.commit09911bf2008-07-26 23:55:29788}
789
dgozman9260b0a12015-03-16 13:45:20790void RenderWidget::OnEnableDeviceEmulation(
Lei Zhang9b359d32017-11-28 00:57:02791 const blink::WebDeviceEmulationParams& params) {
mfomitchev2600fd7c2016-02-17 20:53:39792 if (!screen_metrics_emulator_) {
Fady Samuel4255c182018-05-24 20:42:36793 VisualProperties visual_properties;
794 visual_properties.screen_info = screen_info_;
795 visual_properties.new_size = size_;
796 visual_properties.compositor_viewport_pixel_size =
Christopher Cameronaad15a0d2018-03-09 03:47:33797 compositor_viewport_pixel_size_;
Fady Samuel84d8a1f72018-11-08 00:26:17798 visual_properties.local_surface_id_allocation =
799 local_surface_id_allocation_from_parent_;
Fady Samuel4255c182018-05-24 20:42:36800 visual_properties.visible_viewport_size = visible_viewport_size_;
801 visual_properties.is_fullscreen_granted = is_fullscreen_granted_;
802 visual_properties.display_mode = display_mode_;
mfomitchev2600fd7c2016-02-17 20:53:39803 screen_metrics_emulator_.reset(new RenderWidgetScreenMetricsEmulator(
Albert J. Wong3c93c182018-09-27 17:29:43804 this, params, visual_properties, widget_screen_rect_,
Fady Samuel4255c182018-05-24 20:42:36805 window_screen_rect_));
oshima50872a72016-03-04 13:26:18806 screen_metrics_emulator_->Apply();
mfomitchev2600fd7c2016-02-17 20:53:39807 } else {
dgozman9260b0a12015-03-16 13:45:20808 screen_metrics_emulator_->ChangeEmulationParams(params);
mfomitchev2600fd7c2016-02-17 20:53:39809 }
dgozman9260b0a12015-03-16 13:45:20810}
811
812void RenderWidget::OnDisableDeviceEmulation() {
813 screen_metrics_emulator_.reset();
814}
815
initial.commit09911bf2008-07-26 23:55:29816void RenderWidget::OnWasHidden() {
[email protected]9c3085f2011-06-09 02:10:31817 TRACE_EVENT0("renderer", "RenderWidget::OnWasHidden");
initial.commit09911bf2008-07-26 23:55:29818 // Go into a mode where we stop generating paint and scrolling events.
[email protected]bee16aab2009-08-26 15:55:03819 SetHidden(true);
ericwilligers88e69742016-10-17 19:29:55820 for (auto& observer : render_frames_)
821 observer.WasHidden();
initial.commit09911bf2008-07-26 23:55:29822}
823
Saman Samie61e5c032018-11-05 22:11:44824void RenderWidget::OnWasShown(base::TimeTicks show_request_timestamp,
825 bool was_evicted) {
[email protected]9e2e4632012-07-27 16:38:41826 TRACE_EVENT0("renderer", "RenderWidget::OnWasShown");
danakj57f139f2018-11-19 19:23:46827 // TODO(danakj): Nothing should happen ideally if the RenderWidget is frozen!
828 // It's not visible! However.. the RenderView needs to see it as visible in
829 // order to make the Page visible /o\ so this is hard. We need to detangle
830 // page visibility from the main widget. https://crbug.com/419087
danakj678f0252018-11-09 21:46:34831
tasakb95dbb50c2017-02-08 18:07:50832 was_shown_time_ = base::TimeTicks::Now();
initial.commit09911bf2008-07-26 23:55:29833 // See OnWasHidden
[email protected]bee16aab2009-08-26 15:55:03834 SetHidden(false);
ericwilligers88e69742016-10-17 19:29:55835 for (auto& observer : render_frames_)
836 observer.WasShown();
initial.commit09911bf2008-07-26 23:55:29837
Saman Samie61e5c032018-11-05 22:11:44838 if (was_evicted) {
839 for (auto& observer : render_frame_proxies_) {
840 observer.WasEvicted();
841 }
842 }
843
Kinuko Yasudabef2b642019-01-24 04:53:33844 if (layer_tree_view_ && !show_request_timestamp.is_null()) {
danakja6c10012018-07-06 14:25:36845 layer_tree_view_->layer_tree_host()->RequestPresentationTimeForNextFrame(
Sadrul Habib Chowdhury8bc43cc2018-06-06 01:48:03846 CreateTabSwitchingTimeRecorder(show_request_timestamp));
[email protected]3399dd822014-08-09 11:14:24847 }
initial.commit09911bf2008-07-26 23:55:29848}
849
Avi Drissman014dec72018-06-07 02:34:01850void RenderWidget::OnRequestSetBoundsAck() {
[email protected]53d3f302009-12-21 04:42:05851 DCHECK(pending_window_rect_count_);
852 pending_window_rect_count_--;
853}
854
Albert J. Wongcb004632018-07-10 22:58:25855void RenderWidget::OnForceRedraw(int snapshot_id) {
Kinuko Yasudabef2b642019-01-24 04:53:33856 if (LayerTreeView* ltv = layer_tree_view()) {
857 ltv->layer_tree_host()->RequestPresentationTimeForNextFrame(
858 base::BindOnce(&RenderWidget::DidPresentForceDrawFrame,
859 weak_ptr_factory_.GetWeakPtr(), snapshot_id));
860 ltv->SetNeedsForcedRedraw();
861 }
Albert J. Wongcb004632018-07-10 22:58:25862}
863
864void RenderWidget::DidPresentForceDrawFrame(
865 int snapshot_id,
866 const gfx::PresentationFeedback& feedback) {
Albert J. Wong3c93c182018-09-27 17:29:43867 Send(new WidgetHostMsg_ForceRedrawComplete(routing_id(), snapshot_id));
Albert J. Wongcb004632018-07-10 22:58:25868}
869
Xianzhu Wangf02017ac2018-10-17 01:47:33870viz::FrameSinkId RenderWidget::GetFrameSinkIdAtPoint(const gfx::PointF& point,
Ken Buchanan44d7e2f2018-08-23 14:18:05871 gfx::PointF* local_point) {
872 return input_handler_->GetFrameSinkIdAtPoint(point, local_point);
Navid Zolghadr0d86e5f2017-10-23 18:09:22873}
874
Dave Tapuskaea83d3bd2017-06-13 16:14:55875void RenderWidget::HandleInputEvent(
dtapuska9ec1a912017-04-21 15:18:31876 const blink::WebCoalescedInputEvent& input_event,
877 const ui::LatencyInfo& latency_info,
Dave Tapuskaea83d3bd2017-06-13 16:14:55878 HandledEventCallback callback) {
Albert J. Wong7bbf22d2018-12-20 00:27:27879 if (delegate() && is_frozen_) {
danakja2c9d0a92018-07-25 20:01:18880 std::move(callback).Run(INPUT_EVENT_ACK_STATE_NOT_CONSUMED, latency_info,
881 nullptr, base::nullopt);
882 return;
883 }
Dave Tapuskaea83d3bd2017-06-13 16:14:55884 input_handler_->HandleInputEvent(input_event, latency_info,
885 std::move(callback));
dtapuska9ec1a912017-04-21 15:18:31886}
887
dtapuska9d46ef7d2017-05-26 19:06:06888scoped_refptr<MainThreadEventQueue> RenderWidget::GetInputEventQueue() {
889 return input_event_queue_;
890}
891
fsamuel78f86e42016-01-20 04:10:23892void RenderWidget::OnCursorVisibilityChange(bool is_visible) {
lfg8ff33912016-09-13 20:59:21893 if (GetWebWidget())
Blink Reformat1c4d759e2017-04-09 16:34:54894 GetWebWidget()->SetCursorVisibilityState(is_visible);
fsamuel78f86e42016-01-20 04:10:23895}
896
897void RenderWidget::OnMouseCaptureLost() {
lfg8ff33912016-09-13 20:59:21898 if (GetWebWidget())
Blink Reformat1c4d759e2017-04-09 16:34:54899 GetWebWidget()->MouseCaptureLost();
fsamuel78f86e42016-01-20 04:10:23900}
901
alexmos56567492016-09-13 00:52:46902void RenderWidget::OnSetEditCommandsForNextKeyEvent(
903 const EditCommands& edit_commands) {
904 edit_commands_ = edit_commands;
905}
906
Albert J. Wongcb004632018-07-10 22:58:25907void RenderWidget::OnSetActive(bool active) {
Albert J. Wong7bbf22d2018-12-20 00:27:27908 if (delegate())
909 delegate()->SetActiveForWidget(active);
Albert J. Wongcb004632018-07-10 22:58:25910}
911
fsamuel78f86e42016-01-20 04:10:23912void RenderWidget::OnSetFocus(bool enable) {
Albert J. Wong7bbf22d2018-12-20 00:27:27913 if (delegate())
914 delegate()->DidReceiveSetFocusEventForWidget();
danakja2c9d0a92018-07-25 20:01:18915 SetFocus(enable);
916}
917
918void RenderWidget::SetFocus(bool enable) {
lfge0c2792ec2016-05-11 18:52:08919 has_focus_ = enable;
920
lfg8ff33912016-09-13 20:59:21921 if (GetWebWidget())
Blink Reformat1c4d759e2017-04-09 16:34:54922 GetWebWidget()->SetFocus(enable);
lfge0c2792ec2016-05-11 18:52:08923
ericwilligers88e69742016-10-17 19:29:55924 for (auto& observer : render_frames_)
925 observer.RenderWidgetSetFocus(enable);
danakja2c9d0a92018-07-25 20:01:18926
Albert J. Wong7bbf22d2018-12-20 00:27:27927 if (delegate())
928 delegate()->DidChangeFocusForWidget();
fsamuel78f86e42016-01-20 04:10:23929}
930
dtapuska3d5624d32016-08-30 04:34:00931void RenderWidget::SetNeedsMainFrame() {
danakj53f46b82018-12-11 20:55:55932 // The WebWidgetClient is not |this| if tests override it for the WebView and
933 // WebViewClient.
934 blink::WebWidgetClient* client =
Albert J. Wong7bbf22d2018-12-20 00:27:27935 delegate() ? delegate()->GetWebWidgetClientForWidget() : this;
danakj53f46b82018-12-11 20:55:55936 client->ScheduleAnimation();
dtapuska3d5624d32016-08-30 04:34:00937}
938
fsamuel78f86e42016-01-20 04:10:23939///////////////////////////////////////////////////////////////////////////////
danakja6c10012018-07-06 14:25:36940// LayerTreeViewDelegate
fsamuel78f86e42016-01-20 04:10:23941
David Bokanc8e38d02018-10-08 21:56:01942void RenderWidget::ApplyViewportChanges(
943 const cc::ApplyViewportChangesArgs& args) {
Ken Buchananed449bb2018-02-01 21:02:05944 if (!GetWebWidget())
945 return;
David Bokanc8e38d02018-10-08 21:56:01946 GetWebWidget()->ApplyViewportChanges(args);
fsamuel78f86e42016-01-20 04:10:23947}
948
sahel1b47fda72017-03-28 17:03:07949void RenderWidget::RecordWheelAndTouchScrollingCount(
950 bool has_scrolled_by_wheel,
951 bool has_scrolled_by_touch) {
Ken Buchananed449bb2018-02-01 21:02:05952 if (!GetWebWidget())
953 return;
Blink Reformat1c4d759e2017-04-09 16:34:54954 GetWebWidget()->RecordWheelAndTouchScrollingCount(has_scrolled_by_wheel,
sahel1b47fda72017-03-28 17:03:07955 has_scrolled_by_touch);
956}
957
Sahel Sharify676580c2019-01-10 21:49:49958void RenderWidget::SendOverscrollEventFromImplSide(
959 const gfx::Vector2dF& overscroll_delta,
960 cc::ElementId scroll_latched_element_id) {
961 if (!GetWebWidget())
962 return;
963 GetWebWidget()->SendOverscrollEventFromImplSide(overscroll_delta,
964 scroll_latched_element_id);
965}
966void RenderWidget::SendScrollEndEventFromImplSide(
967 cc::ElementId scroll_latched_element_id) {
968 if (!GetWebWidget())
969 return;
970 GetWebWidget()->SendScrollEndEventFromImplSide(scroll_latched_element_id);
971}
972
Daniel Cheng224569ee2018-04-25 05:45:06973void RenderWidget::BeginMainFrame(base::TimeTicks frame_time) {
Ken Buchananed449bb2018-02-01 21:02:05974 if (!GetWebWidget())
975 return;
Daniel Cheng224569ee2018-04-25 05:45:06976 if (input_event_queue_)
977 input_event_queue_->DispatchRafAlignedInput(frame_time);
dtapuska3d5624d32016-08-30 04:34:00978
Daniel Cheng224569ee2018-04-25 05:45:06979 GetWebWidget()->BeginFrame(frame_time);
fsamuel78f86e42016-01-20 04:10:23980}
981
danakjc7afae52017-06-20 21:12:41982void RenderWidget::RequestNewLayerTreeFrameSink(
danakja6c10012018-07-06 14:25:36983 LayerTreeFrameSinkCallback callback) {
[email protected]7912e822014-04-16 02:37:03984 // For widgets that are never visible, we don't start the compositor, so we
danakjc7afae52017-06-20 21:12:41985 // never get a request for a cc::LayerTreeFrameSink.
sievers71c62dd52015-10-07 01:44:39986 DCHECK(!compositor_never_visible_);
danakjf87460792018-12-04 20:14:22987 // Frozen RenderWidgets should not be doing any compositing.
Scott Violetb04f16c2018-11-27 00:48:46988 DCHECK(!is_frozen_);
danakj678f0252018-11-09 21:46:34989
990 if (is_closing()) {
991 // In this case, we drop the request which means the compositor waits
992 // forever, which is fine since we're going to destroy it.
993 return;
994 }
jonrossa2ff4f82018-02-16 17:27:46995
danakjf87460792018-12-04 20:14:22996 // If we have a warmup in progress, wait for that and store the callback
997 // to be run when the warmup completes.
998 if (warmup_frame_sink_request_pending_) {
999 after_warmup_callback_ = std::move(callback);
1000 return;
1001 }
1002 // If a warmup previously completed, use the result.
1003 if (warmup_frame_sink_) {
1004 std::move(callback).Run(std::move(warmup_frame_sink_));
1005 return;
1006 }
1007
1008 DoRequestNewLayerTreeFrameSink(std::move(callback));
1009}
1010
1011void RenderWidget::DoRequestNewLayerTreeFrameSink(
1012 LayerTreeFrameSinkCallback callback) {
jonrossa2ff4f82018-02-16 17:27:461013 // TODO(jonross): have this generated by the LayerTreeFrameSink itself, which
1014 // would then handle binding.
1015 mojom::RenderFrameMetadataObserverPtr ptr;
1016 mojom::RenderFrameMetadataObserverRequest request = mojo::MakeRequest(&ptr);
1017 mojom::RenderFrameMetadataObserverClientPtrInfo client_info;
1018 mojom::RenderFrameMetadataObserverClientRequest client_request =
1019 mojo::MakeRequest(&client_info);
danakj4b347212018-07-04 17:55:171020 auto render_frame_metadata_observer =
1021 std::make_unique<RenderFrameMetadataObserverImpl>(std::move(request),
1022 std::move(client_info));
danakja6c10012018-07-06 14:25:361023 layer_tree_view_->SetRenderFrameObserver(
danakj4b347212018-07-04 17:55:171024 std::move(render_frame_metadata_observer));
danakjf51754372018-12-13 23:20:011025 GURL url = GetWebWidget()->GetURLForDebugTrace();
danakjefabf312018-10-05 19:03:071026 // The |url| is not always available, fallback to a fixed string.
1027 if (url.is_empty())
1028 url = GURL("chrome://gpu/RenderWidget::RequestNewLayerTreeFrameSink");
danakj6dcbc5962018-11-16 16:45:421029 // TODO(danakj): This may not be accurate, depending on the intent. A child
1030 // local root could be in the same process as the view, so if the client is
1031 // meant to designate the process type, it seems kRenderer would be the
1032 // correct choice. If client is meant to designate the widget type, then
1033 // kOOPIF would denote that it is not for the main frame. However, kRenderer
1034 // would also be used for other widgets such as popups.
1035 const char* client_name = for_child_local_root_frame_ ? kOOPIF : kRenderer;
Scott Violetb04f16c2018-11-27 00:48:461036 RenderThreadImpl::current()->RequestNewLayerTreeFrameSink(
1037 routing_id_, frame_swap_message_queue_, std::move(url),
1038 std::move(callback), std::move(client_request), std::move(ptr),
1039 client_name);
[email protected]ba91a792013-02-06 09:48:281040}
1041
fsamuel78f86e42016-01-20 04:10:231042void RenderWidget::DidCommitAndDrawCompositorFrame() {
1043 // NOTE: Tests may break if this event is renamed or moved. See
1044 // tab_capture_performancetest.cc.
1045 TRACE_EVENT0("gpu", "RenderWidget::DidCommitAndDrawCompositorFrame");
lfge0c2792ec2016-05-11 18:52:081046
ericwilligers88e69742016-10-17 19:29:551047 for (auto& observer : render_frames_)
1048 observer.DidCommitAndDrawCompositorFrame();
lfge0c2792ec2016-05-11 18:52:081049
fsamuel78f86e42016-01-20 04:10:231050 // Notify subclasses that we initiated the paint operation.
1051 DidInitiatePaint();
Yiming Zhou0f8836c22018-09-13 22:22:111052
Albert J. Wongfb64e142018-10-16 01:10:451053 Send(new WidgetHostMsg_DidCommitAndDrawCompositorFrame(routing_id_));
fsamuel78f86e42016-01-20 04:10:231054}
1055
Saman Sami766e0b02018-01-31 17:19:201056void RenderWidget::DidCommitCompositorFrame() {
Albert J. Wong7bbf22d2018-12-20 00:27:271057 if (delegate())
1058 delegate()->DidCommitCompositorFrameForWidget();
Saman Sami766e0b02018-01-31 17:19:201059}
fsamuel78f86e42016-01-20 04:10:231060
danakja2c9d0a92018-07-25 20:01:181061void RenderWidget::DidCompletePageScaleAnimation() {
Albert J. Wong7bbf22d2018-12-20 00:27:271062 if (delegate())
1063 delegate()->DidCompletePageScaleAnimationForWidget();
danakja2c9d0a92018-07-25 20:01:181064}
fsamuel78f86e42016-01-20 04:10:231065
danakj53f46b82018-12-11 20:55:551066void RenderWidget::ScheduleAnimation() {
1067 // This call is not needed in single thread mode for tests without a
1068 // scheduler, but they need to override the WebWidgetClient and replace this
1069 // method in order to schedule a synchronous composite task themselves.
1070 layer_tree_view_->SetNeedsBeginFrame();
initial.commit09911bf2008-07-26 23:55:291071}
1072
danakjefcb8432019-01-16 18:47:141073void RenderWidget::SetShowFPSCounter(bool show) {
1074 cc::LayerTreeHost* host = layer_tree_view_->layer_tree_host();
1075 cc::LayerTreeDebugState debug_state = host->GetDebugState();
1076 debug_state.show_fps_counter = show;
1077 host->SetDebugState(debug_state);
1078}
1079
danakje691e852019-01-16 22:49:041080void RenderWidget::SetShowPaintRects(bool show) {
1081 cc::LayerTreeHost* host = layer_tree_view_->layer_tree_host();
1082 cc::LayerTreeDebugState debug_state = host->GetDebugState();
1083 debug_state.show_paint_rects = show;
1084 host->SetDebugState(debug_state);
1085}
1086
1087void RenderWidget::SetShowDebugBorders(bool show) {
1088 cc::LayerTreeHost* host = layer_tree_view_->layer_tree_host();
1089 cc::LayerTreeDebugState debug_state = host->GetDebugState();
1090 if (show)
1091 debug_state.show_debug_borders.set();
1092 else
1093 debug_state.show_debug_borders.reset();
1094 host->SetDebugState(debug_state);
1095}
1096
1097void RenderWidget::SetShowScrollBottleneckRects(bool show) {
1098 cc::LayerTreeHost* host = layer_tree_view_->layer_tree_host();
1099 cc::LayerTreeDebugState debug_state = host->GetDebugState();
1100 debug_state.show_touch_event_handler_rects = show;
1101 debug_state.show_wheel_event_handler_rects = show;
1102 debug_state.show_non_fast_scrollable_rects = show;
1103 host->SetDebugState(debug_state);
1104}
1105
1106void RenderWidget::SetShowHitTestBorders(bool show) {
1107 cc::LayerTreeHost* host = layer_tree_view_->layer_tree_host();
1108 cc::LayerTreeDebugState debug_state = host->GetDebugState();
1109 debug_state.show_hit_test_borders = show;
1110 host->SetDebugState(debug_state);
1111}
1112
Stephen Chenneyec4312612018-11-21 12:40:161113void RenderWidget::UpdateVisualState(bool record_main_frame_metrics) {
Ken Buchananed449bb2018-02-01 21:02:051114 if (!GetWebWidget())
1115 return;
1116
Stephen Chenneyec4312612018-11-21 12:40:161117 // When recording main frame metrics set the lifecycle reason to
1118 // kBeginMainFrame, because this is the calller of UpdateLifecycle
1119 // for the main frame. Otherwise, set the reason to kTests, which is
1120 // the oinly other reason this method is called.
1121 WebWidget::LifecycleUpdateReason lifecycle_reason =
1122 record_main_frame_metrics
1123 ? WebWidget::LifecycleUpdateReason::kBeginMainFrame
1124 : WebWidget::LifecycleUpdateReason::kTest;
1125 GetWebWidget()->UpdateLifecycle(WebWidget::LifecycleUpdate::kAll,
1126 lifecycle_reason);
Blink Reformat1c4d759e2017-04-09 16:34:541127 GetWebWidget()->SetSuppressFrameRequestsWorkaroundFor704763Only(false);
tasakb95dbb50c2017-02-08 18:07:501128
Robert Flack3ef2ffb2018-09-06 20:38:201129 if (first_update_visual_state_after_hidden_) {
Takashi SAKAMOTO870f6262017-08-22 04:08:271130 RecordTimeToFirstActivePaint();
1131 first_update_visual_state_after_hidden_ = false;
1132 }
1133}
tasakb95dbb50c2017-02-08 18:07:501134
Takashi SAKAMOTO870f6262017-08-22 04:08:271135void RenderWidget::RecordTimeToFirstActivePaint() {
tasakb95dbb50c2017-02-08 18:07:501136 RenderThreadImpl* render_thread_impl = RenderThreadImpl::current();
tasakb95dbb50c2017-02-08 18:07:501137 base::TimeDelta sample = base::TimeTicks::Now() - was_shown_time_;
Takashi SAKAMOTO870f6262017-08-22 04:08:271138 if (render_thread_impl->NeedsToRecordFirstActivePaint(TTFAP_AFTER_PURGED)) {
1139 UMA_HISTOGRAM_TIMES("PurgeAndSuspend.Experimental.TimeToFirstActivePaint",
1140 sample);
1141 }
1142 if (render_thread_impl->NeedsToRecordFirstActivePaint(
1143 TTFAP_5MIN_AFTER_BACKGROUNDED)) {
1144 UMA_HISTOGRAM_TIMES(
1145 "PurgeAndSuspend.Experimental.TimeToFirstActivePaint."
1146 "AfterBackgrounded.5min",
1147 sample);
1148 }
fsamuel78f86e42016-01-20 04:10:231149}
1150
Stephen Chenney9879a5c2018-09-26 14:21:421151void RenderWidget::RecordEndOfFrameMetrics(base::TimeTicks frame_begin_time) {
1152 if (!GetWebWidget())
1153 return;
1154
1155 GetWebWidget()->RecordEndOfFrameMetrics(frame_begin_time);
1156}
1157
fsamuel78f86e42016-01-20 04:10:231158void RenderWidget::WillBeginCompositorFrame() {
1159 TRACE_EVENT0("gpu", "RenderWidget::willBeginCompositorFrame");
1160
Ken Buchananed449bb2018-02-01 21:02:051161 if (!GetWebWidget())
1162 return;
1163
Blink Reformat1c4d759e2017-04-09 16:34:541164 GetWebWidget()->SetSuppressFrameRequestsWorkaroundFor704763Only(true);
alancutter317a8e02017-04-05 10:09:291165
dglazkov06854c5c2016-08-31 00:19:591166 // The UpdateTextInputState can result in further layout and possibly
1167 // enable GPU acceleration so they need to be called before any painting
1168 // is done.
changwan75e3b2072017-01-16 02:55:001169 UpdateTextInputState();
dglazkov06854c5c2016-08-31 00:19:591170 UpdateSelectionBounds();
1171
ericwilligers88e69742016-10-17 19:29:551172 for (auto& observer : render_frame_proxies_)
1173 observer.WillBeginCompositorFrame();
initial.commit09911bf2008-07-26 23:55:291174}
1175
Kent Tamura21d1de62018-12-10 04:45:201176std::unique_ptr<cc::SwapPromise> RenderWidget::RequestCopyOfOutputForWebTest(
Fady Samueldfecb7d2017-07-26 11:41:041177 std::unique_ptr<viz::CopyOutputRequest> request) {
Kent Tamura21d1de62018-12-10 04:45:201178 return RenderThreadImpl::current()->RequestCopyOfOutputForWebTest(
jbroman6ccbc7d472016-07-27 04:45:411179 routing_id_, std::move(request));
1180}
1181
fsamuel72464894f2015-12-15 06:59:311182///////////////////////////////////////////////////////////////////////////////
1183// RenderWidgetInputHandlerDelegate
1184
avid7d6b2e2016-03-04 19:41:171185void RenderWidget::FocusChangeComplete() {
Ehsan Karamad655d7b8a2018-01-12 18:38:381186 blink::WebFrameWidget* frame_widget = GetFrameWidget();
1187 if (!frame_widget)
ekaramad27ca69b12017-04-20 18:34:291188 return;
Ehsan Karamad655d7b8a2018-01-12 18:38:381189
ekaramad27ca69b12017-04-20 18:34:291190 blink::WebLocalFrame* focused =
Ehsan Karamad655d7b8a2018-01-12 18:38:381191 frame_widget->LocalRoot()->View()->FocusedFrame();
1192
ekaramad27ca69b12017-04-20 18:34:291193 if (focused && focused->AutofillClient())
1194 focused->AutofillClient()->DidCompleteFocusChangeInFrame();
avid7d6b2e2016-03-04 19:41:171195}
fsamuel72464894f2015-12-15 06:59:311196
dtapuska1827dd22016-03-11 15:24:591197void RenderWidget::ObserveGestureEventAndResult(
1198 const blink::WebGestureEvent& gesture_event,
1199 const gfx::Vector2dF& unused_delta,
Sandra Sunac5cdd832017-12-11 03:27:131200 const cc::OverscrollBehavior& overscroll_behavior,
dtapuska1827dd22016-03-11 15:24:591201 bool event_processed) {
1202 if (!compositor_deps_->IsElasticOverscrollEnabled())
1203 return;
1204
1205 cc::InputHandlerScrollResult scroll_result;
1206 scroll_result.did_scroll = event_processed;
1207 scroll_result.did_overscroll_root = !unused_delta.IsZero();
1208 scroll_result.unused_scroll_delta = unused_delta;
Sandra Sunac5cdd832017-12-11 03:27:131209 scroll_result.overscroll_behavior = overscroll_behavior;
dtapuska1827dd22016-03-11 15:24:591210
Dave Tapuska04bc5ee92018-04-17 19:03:311211 widget_input_handler_manager_->ObserveGestureEventOnMainThread(gesture_event,
1212 scroll_result);
dtapuska1827dd22016-03-11 15:24:591213}
1214
avid7d6b2e2016-03-04 19:41:171215void RenderWidget::OnDidHandleKeyEvent() {
alexmos56567492016-09-13 00:52:461216 ClearEditCommands();
1217}
1218
1219void RenderWidget::SetEditCommandForNextKeyEvent(const std::string& name,
1220 const std::string& value) {
1221 ClearEditCommands();
1222 edit_commands_.emplace_back(name, value);
1223}
1224
1225void RenderWidget::ClearEditCommands() {
1226 edit_commands_.clear();
avid7d6b2e2016-03-04 19:41:171227}
fsamuel72464894f2015-12-15 06:59:311228
chongza8ba91fc2016-08-16 21:39:171229void RenderWidget::OnDidOverscroll(const ui::DidOverscrollParams& params) {
Dave Tapuska04bc5ee92018-04-17 19:03:311230 if (mojom::WidgetInputHandlerHost* host =
1231 widget_input_handler_manager_->GetWidgetInputHandlerHost()) {
1232 host->DidOverscroll(params);
Dave Tapuska139a72f2017-09-06 21:57:031233 }
fsamuel72464894f2015-12-15 06:59:311234}
1235
fsamuele8326c742016-01-12 00:49:391236void RenderWidget::SetInputHandler(RenderWidgetInputHandler* input_handler) {
1237 // Nothing to do here. RenderWidget created the |input_handler| and will take
1238 // ownership of it. We just verify here that we don't already have an input
1239 // handler.
1240 DCHECK(!input_handler_);
1241}
1242
changwan75e3b2072017-01-16 02:55:001243void RenderWidget::ShowVirtualKeyboard() {
1244 UpdateTextInputStateInternal(true, false);
1245}
1246
changwan62f57292017-02-17 08:28:251247void RenderWidget::ClearTextInputState() {
1248 text_input_info_ = blink::WebTextInputInfo();
1249 text_input_type_ = ui::TextInputType::TEXT_INPUT_TYPE_NONE;
1250 text_input_mode_ = ui::TextInputMode::TEXT_INPUT_MODE_DEFAULT;
1251 can_compose_inline_ = false;
1252 text_input_flags_ = 0;
AJITH KUMAR V041c0b02017-08-08 10:39:201253 next_previous_flags_ = kInvalidNextPreviousFlagsValue;
changwan62f57292017-02-17 08:28:251254}
1255
changwan75e3b2072017-01-16 02:55:001256void RenderWidget::UpdateTextInputState() {
1257 UpdateTextInputStateInternal(false, false);
1258}
1259
1260void RenderWidget::UpdateTextInputStateInternal(bool show_virtual_keyboard,
1261 bool reply_to_request) {
fsamuel72464894f2015-12-15 06:59:311262 TRACE_EVENT0("renderer", "RenderWidget::UpdateTextInputState");
changwan75e3b2072017-01-16 02:55:001263
fsamuel72464894f2015-12-15 06:59:311264 if (ime_event_guard_) {
changwan75e3b2072017-01-16 02:55:001265 DCHECK(!reply_to_request);
1266 // show_virtual_keyboard should still be effective even if it was set inside
1267 // the IME
fsamuel72464894f2015-12-15 06:59:311268 // event guard.
changwan75e3b2072017-01-16 02:55:001269 if (show_virtual_keyboard)
1270 ime_event_guard_->set_show_virtual_keyboard(true);
fsamuel72464894f2015-12-15 06:59:311271 return;
1272 }
1273
dglazkov97b6c2b2016-10-25 17:35:551274 ui::TextInputType new_type = GetTextInputType();
1275 if (IsDateTimeInput(new_type))
1276 return; // Not considered as a text input field in WebKit/Chromium.
fsamuel72464894f2015-12-15 06:59:311277
dglazkov97b6c2b2016-10-25 17:35:551278 blink::WebTextInputInfo new_info;
ekaramad5aff1942017-01-06 01:26:351279 if (auto* controller = GetInputMethodController())
Blink Reformat1c4d759e2017-04-09 16:34:541280 new_info = controller->TextInputInfo();
dtapuskac4dd5be2016-10-25 15:11:101281 const ui::TextInputMode new_mode =
Blink Reformat1c4d759e2017-04-09 16:34:541282 ConvertWebTextInputMode(new_info.input_mode);
fsamuel72464894f2015-12-15 06:59:311283
1284 bool new_can_compose_inline = CanComposeInline();
1285
1286 // Only sends text input params if they are changed or if the ime should be
1287 // shown.
changwan75e3b2072017-01-16 02:55:001288 if (show_virtual_keyboard || reply_to_request ||
changwan38c3eb612016-12-09 01:45:561289 text_input_type_ != new_type || text_input_mode_ != new_mode ||
1290 text_input_info_ != new_info ||
1291 can_compose_inline_ != new_can_compose_inline) {
ekaramad9053e57b2016-04-26 20:00:381292 TextInputState params;
dglazkov97b6c2b2016-10-25 17:35:551293 params.type = new_type;
fsamuel72464894f2015-12-15 06:59:311294 params.mode = new_mode;
1295 params.flags = new_info.flags;
AJITH KUMAR V9e7b5572017-11-14 00:54:211296#if defined(OS_ANDROID)
AJITH KUMAR V041c0b02017-08-08 10:39:201297 if (next_previous_flags_ == kInvalidNextPreviousFlagsValue) {
1298 // Due to a focus change, values will be reset by the frame.
1299 // That case we only need fresh NEXT/PREVIOUS information.
Albert J. Wong3c93c182018-09-27 17:29:431300 // Also we won't send WidgetHostMsg_TextInputStateChanged if next/previous
AJITH KUMAR V041c0b02017-08-08 10:39:201301 // focusable status is changed.
1302 if (auto* controller = GetInputMethodController()) {
1303 next_previous_flags_ =
1304 controller->ComputeWebTextInputNextPreviousFlags();
1305 } else {
1306 // For safety in case GetInputMethodController() is null, because -1 is
1307 // invalid value to send to browser process.
1308 next_previous_flags_ = 0;
1309 }
1310 }
AJITH KUMAR V9e7b5572017-11-14 00:54:211311#else
1312 next_previous_flags_ = 0;
1313#endif
AJITH KUMAR V041c0b02017-08-08 10:39:201314 params.flags |= next_previous_flags_;
Yuichiro Hanada1b19cf852019-01-18 16:12:261315 params.value = new_info.value.Utf16();
Blink Reformat1c4d759e2017-04-09 16:34:541316 params.selection_start = new_info.selection_start;
1317 params.selection_end = new_info.selection_end;
1318 params.composition_start = new_info.composition_start;
1319 params.composition_end = new_info.composition_end;
fsamuel72464894f2015-12-15 06:59:311320 params.can_compose_inline = new_can_compose_inline;
changwan75e3b2072017-01-16 02:55:001321 // TODO(changwan): change instances of show_ime_if_needed to
1322 // show_virtual_keyboard.
1323 params.show_ime_if_needed = show_virtual_keyboard;
1324 params.reply_to_request = reply_to_request;
Albert J. Wong3c93c182018-09-27 17:29:431325 Send(new WidgetHostMsg_TextInputStateChanged(routing_id(), params));
fsamuel72464894f2015-12-15 06:59:311326
1327 text_input_info_ = new_info;
dglazkov97b6c2b2016-10-25 17:35:551328 text_input_type_ = new_type;
fsamuel72464894f2015-12-15 06:59:311329 text_input_mode_ = new_mode;
1330 can_compose_inline_ = new_can_compose_inline;
1331 text_input_flags_ = new_info.flags;
Eric Karl706ea6712018-12-21 22:41:381332
1333#if defined(OS_ANDROID)
1334 // If we send a new TextInputStateChanged message, we must also deliver a
1335 // new RenderFrameMetadata, as the IME will need this info to be updated.
1336 // TODO(ericrk): Consider folding the above IPC into RenderFrameMetadata.
1337 // https://crbug.com/912309
Kinuko Yasudabef2b642019-01-24 04:53:331338 if (layer_tree_view_ && IsSurfaceSynchronizationEnabled()) {
Eric Karl706ea6712018-12-21 22:41:381339 layer_tree_view_->RequestForceSendMetadata();
1340 }
1341#endif
fsamuel72464894f2015-12-15 06:59:311342 }
1343}
1344
1345bool RenderWidget::WillHandleGestureEvent(const blink::WebGestureEvent& event) {
paulmeyer6ef5a792016-11-08 20:33:581346 possible_drag_event_info_.event_source =
1347 ui::DragDropTypes::DRAG_EVENT_SOURCE_TOUCH;
1348 possible_drag_event_info_.event_location =
Ella Ge1116059d2018-03-21 02:06:131349 gfx::ToFlooredPoint(event.PositionInScreen());
avid7d6b2e2016-03-04 19:41:171350
fsamuel72464894f2015-12-15 06:59:311351 return false;
1352}
1353
1354bool RenderWidget::WillHandleMouseEvent(const blink::WebMouseEvent& event) {
ericwilligers88e69742016-10-17 19:29:551355 for (auto& observer : render_frames_)
1356 observer.RenderWidgetWillHandleMouseEvent();
lfge0c2792ec2016-05-11 18:52:081357
paulmeyer6ef5a792016-11-08 20:33:581358 possible_drag_event_info_.event_source =
1359 ui::DragDropTypes::DRAG_EVENT_SOURCE_MOUSE;
1360 possible_drag_event_info_.event_location =
Blink Reformat1c4d759e2017-04-09 16:34:541361 gfx::Point(event.PositionInScreen().x, event.PositionInScreen().y);
paulmeyer6ef5a792016-11-08 20:33:581362
Albert J. Wong7bbf22d2018-12-20 00:27:271363 if (delegate())
1364 return delegate()->RenderWidgetWillHandleMouseEventForWidget(event);
avid7d6b2e2016-03-04 19:41:171365
fsamuel72464894f2015-12-15 06:59:311366 return false;
[email protected]fd847792013-10-24 17:12:351367}
1368
initial.commit09911bf2008-07-26 23:55:291369///////////////////////////////////////////////////////////////////////////////
mfomitchev2600fd7c2016-02-17 20:53:391370// RenderWidgetScreenMetricsDelegate
1371
bokanc63441c2016-04-27 15:49:121372void RenderWidget::ResizeWebWidget() {
danakja2c9d0a92018-07-25 20:01:181373 gfx::Size size = GetSizeForWebWidget();
Albert J. Wong7bbf22d2018-12-20 00:27:271374 if (delegate()) {
1375 delegate()->ResizeWebWidgetForWidget(size, top_controls_height_,
1376 bottom_controls_height_,
1377 browser_controls_shrink_blink_size_);
danakja2c9d0a92018-07-25 20:01:181378 return;
1379 }
1380 GetWebWidget()->Resize(size);
bokanc63441c2016-04-27 15:49:121381}
1382
1383gfx::Size RenderWidget::GetSizeForWebWidget() const {
danakj4b347212018-07-04 17:55:171384 if (compositor_deps_->IsUseZoomForDSFEnabled()) {
Fady Samuela863f152018-03-09 16:10:031385 return gfx::ScaleToCeiledSize(size_,
1386 GetOriginalScreenInfo().device_scale_factor);
1387 }
bokanc63441c2016-04-27 15:49:121388
1389 return size_;
1390}
1391
akaba3483d8f2018-07-10 21:43:091392void RenderWidget::UpdateZoom(double zoom_level) {
akabac6bd1212018-06-25 20:10:481393 blink::WebFrameWidget* frame_widget = GetFrameWidget();
1394 if (!frame_widget)
1395 return;
1396 RenderFrameImpl* render_frame =
1397 RenderFrameImpl::FromWebFrame(frame_widget->LocalRoot());
1398
akaba3483d8f2018-07-10 21:43:091399 // Return early if zoom level is unchanged.
1400 if (render_frame->GetZoomLevel() == zoom_level) {
akabac6bd1212018-06-25 20:10:481401 return;
1402 }
1403
akaba3483d8f2018-07-10 21:43:091404 render_frame->SetZoomLevel(zoom_level);
akabac6bd1212018-06-25 20:10:481405
1406 for (auto& observer : render_frame_proxies_)
akaba3483d8f2018-07-10 21:43:091407 observer.OnZoomLevelChanged(zoom_level);
akabac6bd1212018-06-25 20:10:481408
1409 for (auto& plugin : browser_plugins_)
akaba3483d8f2018-07-10 21:43:091410 plugin.OnZoomLevelChanged(zoom_level);
akabac6bd1212018-06-25 20:10:481411}
1412
Fady Samuel799e72192018-04-25 21:16:571413void RenderWidget::SynchronizeVisualProperties(const VisualProperties& params) {
Christopher Cameronbe7ecea2018-02-27 00:47:191414 // Inform the rendering thread of the color space indicate the presence of HDR
1415 // capabilities.
Miguel Casas5e1018052018-01-09 19:17:361416 RenderThreadImpl* render_thread = RenderThreadImpl::current();
1417 if (render_thread)
Christopher Camerone9a30c12018-03-07 08:23:381418 render_thread->SetRenderingColorSpace(params.screen_info.color_space);
Miguel Casas5e1018052018-01-09 19:17:361419
Fady Samuele62be402018-03-29 03:19:531420 gfx::Size new_compositor_viewport_pixel_size =
1421 params.auto_resize_enabled
1422 ? gfx::ScaleToCeiledSize(size_,
1423 params.screen_info.device_scale_factor)
1424 : params.compositor_viewport_pixel_size;
Fady Samuel84d8a1f72018-11-08 00:26:171425 UpdateSurfaceAndScreenInfo(params.local_surface_id_allocation.value_or(
1426 viz::LocalSurfaceIdAllocation()),
1427 new_compositor_viewport_pixel_size,
1428 params.screen_info);
Vladimir Levin98d76dad2018-04-21 00:21:291429 UpdateCaptureSequenceNumber(params.capture_sequence_number);
Kinuko Yasudabef2b642019-01-24 04:53:331430 if (layer_tree_view_) {
1431 layer_tree_view_->SetBrowserControlsHeight(
1432 params.top_controls_height, params.bottom_controls_height,
1433 params.browser_controls_shrink_blink_size);
1434 layer_tree_view_->SetRasterColorSpace(
1435 screen_info_.color_space.GetRasterColorSpace());
1436 }
mfomitchev2600fd7c2016-02-17 20:53:391437
akaba3483d8f2018-07-10 21:43:091438 UpdateZoom(params.zoom_level);
akabac6bd1212018-06-25 20:10:481439
danakj21604ce2018-12-19 16:01:221440 if (!params.auto_resize_enabled) {
1441 visible_viewport_size_ = params.visible_viewport_size;
Fady Samuele62be402018-03-29 03:19:531442
danakj21604ce2018-12-19 16:01:221443 display_mode_ = params.display_mode;
mfomitchev2600fd7c2016-02-17 20:53:391444
danakj21604ce2018-12-19 16:01:221445 size_ = params.new_size;
mfomitchev2600fd7c2016-02-17 20:53:391446
danakj21604ce2018-12-19 16:01:221447 ResizeWebWidget();
mfomitchev2600fd7c2016-02-17 20:53:391448
danakj21604ce2018-12-19 16:01:221449 WebSize visual_viewport_size;
1450 if (compositor_deps_->IsUseZoomForDSFEnabled()) {
1451 visual_viewport_size =
1452 gfx::ScaleToCeiledSize(params.visible_viewport_size,
1453 GetOriginalScreenInfo().device_scale_factor);
1454 } else {
1455 visual_viewport_size = visible_viewport_size_;
1456 }
1457 GetWebWidget()->ResizeVisualViewport(visual_viewport_size);
bokan71cb5b12016-04-27 03:45:221458
danakjf9284772019-01-23 18:29:141459 // NOTE: We may have entered fullscreen mode without changing our size.
1460 SetIsFullscreen(params.is_fullscreen_granted);
mfomitchev2600fd7c2016-02-17 20:53:391461 }
mfomitchev2600fd7c2016-02-17 20:53:391462}
1463
1464void RenderWidget::SetScreenMetricsEmulationParameters(
1465 bool enabled,
1466 const blink::WebDeviceEmulationParams& params) {
Albert J. Wong7bbf22d2018-12-20 00:27:271467 // This is only supported in RenderView, which has an delegate().
1468 DCHECK(delegate());
1469 delegate()->SetScreenMetricsEmulationParametersForWidget(enabled, params);
mfomitchev2600fd7c2016-02-17 20:53:391470}
1471
Albert J. Wong3c93c182018-09-27 17:29:431472void RenderWidget::SetScreenRects(const gfx::Rect& widget_screen_rect,
mfomitchev2600fd7c2016-02-17 20:53:391473 const gfx::Rect& window_screen_rect) {
Albert J. Wong3c93c182018-09-27 17:29:431474 widget_screen_rect_ = widget_screen_rect;
mfomitchev2600fd7c2016-02-17 20:53:391475 window_screen_rect_ = window_screen_rect;
1476}
1477
1478///////////////////////////////////////////////////////////////////////////////
[email protected]f98d7e3c2010-09-13 22:30:461479// WebWidgetClient
initial.commit09911bf2008-07-26 23:55:291480
Chris Harrelsond7ab99b2018-01-24 17:51:361481void RenderWidget::IntrinsicSizingInfoChanged(
1482 const blink::WebIntrinsicSizingInfo& sizing_info) {
Albert J. Wong3c93c182018-09-27 17:29:431483 Send(new WidgetHostMsg_IntrinsicSizingInfoChanged(routing_id_, sizing_info));
Chris Harrelsond7ab99b2018-01-24 17:51:361484}
1485
Blink Reformat1c4d759e2017-04-09 16:34:541486void RenderWidget::DidMeaningfulLayout(blink::WebMeaningfulLayout layout_type) {
1487 if (layout_type == blink::WebMeaningfulLayout::kVisuallyNonEmpty) {
Albert J. Wongfb64e142018-10-16 01:10:451488 QueueMessage(new WidgetHostMsg_DidFirstVisuallyNonEmptyPaint(routing_id_));
dglazkovf0e1d6d2015-10-10 02:13:481489 }
dglazkov79c426102015-08-31 21:22:431490
ericwilligers88e69742016-10-17 19:29:551491 for (auto& observer : render_frames_)
1492 observer.DidMeaningfulLayout(layout_type);
dglazkov79c426102015-08-31 21:22:431493}
1494
[email protected]586871b2014-07-22 17:05:111495// static
dchengcedca5612016-04-09 01:40:151496std::unique_ptr<cc::SwapPromise> RenderWidget::QueueMessageImpl(
[email protected]586871b2014-07-22 17:05:111497 IPC::Message* msg,
[email protected]586871b2014-07-22 17:05:111498 FrameSwapMessageQueue* frame_swap_message_queue,
1499 scoped_refptr<IPC::SyncMessageFilter> sync_message_filter,
[email protected]586871b2014-07-22 17:05:111500 int source_frame_number) {
[email protected]586871b2014-07-22 17:05:111501 bool first_message_for_frame = false;
Sadrul Habib Chowdhury4e0335cf2018-07-31 20:08:491502 frame_swap_message_queue->QueueMessageForFrame(
1503 source_frame_number, base::WrapUnique(msg), &first_message_for_frame);
[email protected]586871b2014-07-22 17:05:111504 if (first_message_for_frame) {
dchengcedca5612016-04-09 01:40:151505 std::unique_ptr<cc::SwapPromise> promise(new QueueMessageSwapPromise(
[email protected]586871b2014-07-22 17:05:111506 sync_message_filter, frame_swap_message_queue, source_frame_number));
dcheng4b6b5ff2014-10-16 00:42:061507 return promise;
[email protected]586871b2014-07-22 17:05:111508 }
dcheng4b6b5ff2014-10-16 00:42:061509 return nullptr;
[email protected]586871b2014-07-22 17:05:111510}
1511
danakj53802692018-07-25 21:46:441512void RenderWidget::SetHandlingInputEvent(bool handling_input_event) {
1513 input_handler_->set_handling_input_event(handling_input_event);
1514}
1515
Sadrul Habib Chowdhury4e0335cf2018-07-31 20:08:491516void RenderWidget::QueueMessage(IPC::Message* msg) {
[email protected]586871b2014-07-22 17:05:111517 // RenderThreadImpl::current() is NULL in some tests.
Kinuko Yasudabef2b642019-01-24 04:53:331518 if (!layer_tree_view_ || !RenderThreadImpl::current()) {
[email protected]586871b2014-07-22 17:05:111519 Send(msg);
1520 return;
1521 }
1522
dchengcedca5612016-04-09 01:40:151523 std::unique_ptr<cc::SwapPromise> swap_promise =
Sadrul Habib Chowdhury4e0335cf2018-07-31 20:08:491524 QueueMessageImpl(msg, frame_swap_message_queue_.get(),
[email protected]586871b2014-07-22 17:05:111525 RenderThreadImpl::current()->sync_message_filter(),
danakja6c10012018-07-06 14:25:361526 layer_tree_view_->GetSourceFrameNumber());
[email protected]586871b2014-07-22 17:05:111527
Ken Buchanance710eb2017-08-30 23:46:521528 if (swap_promise)
danakja6c10012018-07-06 14:25:361529 layer_tree_view_->QueueSwapPromise(std::move(swap_promise));
[email protected]586871b2014-07-22 17:05:111530}
1531
Blink Reformat1c4d759e2017-04-09 16:34:541532void RenderWidget::DidChangeCursor(const WebCursorInfo& cursor_info) {
[email protected]7c51b0ee2009-07-08 21:49:301533 // TODO(darin): Eliminate this temporary.
[email protected]9ec87712013-05-24 23:23:521534 WebCursor cursor;
tfarina75a0abf2015-10-06 15:07:181535 InitializeCursorFromWebCursorInfo(&cursor, cursor_info);
initial.commit09911bf2008-07-26 23:55:291536 // Only send a SetCursor message if we need to make a change.
Navid Zolghadr293bb862018-12-12 18:15:411537 if (input_handler_->DidChangeCursor(cursor))
Albert J. Wong3c93c182018-09-27 17:29:431538 Send(new WidgetHostMsg_SetCursor(routing_id_, cursor));
initial.commit09911bf2008-07-26 23:55:291539}
1540
aelias5971e47d2017-06-16 02:39:381541void RenderWidget::AutoscrollStart(const blink::WebFloatPoint& point) {
Albert J. Wong3c93c182018-09-27 17:29:431542 Send(new WidgetHostMsg_AutoscrollStart(routing_id_, point));
aelias5971e47d2017-06-16 02:39:381543}
1544
1545void RenderWidget::AutoscrollFling(const blink::WebFloatSize& velocity) {
Albert J. Wong3c93c182018-09-27 17:29:431546 Send(new WidgetHostMsg_AutoscrollFling(routing_id_, velocity));
aelias5971e47d2017-06-16 02:39:381547}
1548
1549void RenderWidget::AutoscrollEnd() {
Albert J. Wong3c93c182018-09-27 17:29:431550 Send(new WidgetHostMsg_AutoscrollEnd(routing_id_));
aelias5971e47d2017-06-16 02:39:381551}
1552
initial.commit09911bf2008-07-26 23:55:291553// We are supposed to get a single call to Show for a newly created RenderWidget
1554// that was created via RenderWidget::CreateWebView. So, we wait until this
1555// point to dispatch the ShowWidget message.
1556//
1557// This method provides us with the information about how to display the newly
[email protected]5f9de5882011-09-30 23:36:281558// created RenderWidget (i.e., as a blocked popup or as a new tab).
initial.commit09911bf2008-07-26 23:55:291559//
Blink Reformat1c4d759e2017-04-09 16:34:541560void RenderWidget::Show(WebNavigationPolicy policy) {
danakj65f423b72018-10-10 17:21:201561 if (!show_callback_) {
Albert J. Wong7bbf22d2018-12-20 00:27:271562 if (delegate()) {
danakja2c9d0a92018-07-25 20:01:181563 // When SupportsMultipleWindows is disabled, popups are reusing
danakj65f423b72018-10-10 17:21:201564 // the view's RenderWidget. In some scenarios, this makes blink to call
1565 // Show() twice. But otherwise, if it is enabled, we should not visit
1566 // Show() more than once.
Albert J. Wong7bbf22d2018-12-20 00:27:271567 DCHECK(!delegate()->SupportsMultipleWindowsForWidget());
danakj65f423b72018-10-10 17:21:201568 return;
danakja2c9d0a92018-07-25 20:01:181569 } else {
danakj65f423b72018-10-10 17:21:201570 NOTREACHED() << "received extraneous Show call";
danakja2c9d0a92018-07-25 20:01:181571 }
danakja2c9d0a92018-07-25 20:01:181572 }
1573
initial.commit09911bf2008-07-26 23:55:291574 DCHECK(routing_id_ != MSG_ROUTING_NONE);
nickf7b38222016-11-22 21:59:351575
1576 // The opener is responsible for actually showing this widget.
danakjdf1ceb72018-07-18 20:02:251577 std::move(show_callback_).Run(this, policy, initial_rect_);
nickf7b38222016-11-22 21:59:351578
bokanc007c3a2015-02-03 07:15:561579 // NOTE: initial_rect_ may still have its default values at this point, but
[email protected]8de12d942010-11-17 20:42:441580 // that's okay. It'll be ignored if as_popup is false, or the browser
1581 // process will impose a default position otherwise.
bokanc007c3a2015-02-03 07:15:561582 SetPendingWindowRect(initial_rect_);
initial.commit09911bf2008-07-26 23:55:291583}
1584
danakj17216f4d2018-10-16 22:57:321585LayerTreeView* RenderWidget::InitializeLayerTreeView() {
1586 TRACE_EVENT0("blink", "RenderWidget::InitializeLayerTreeView");
danakj17216f4d2018-10-16 22:57:321587
1588 layer_tree_view_ = std::make_unique<LayerTreeView>(
1589 this, compositor_deps_->GetCompositorMainThreadTaskRunner(),
1590 compositor_deps_->GetCompositorImplThreadTaskRunner(),
1591 compositor_deps_->GetTaskGraphRunner(),
1592 compositor_deps_->GetWebMainThreadScheduler());
1593 layer_tree_view_->Initialize(
danakj6dcbc5962018-11-16 16:45:421594 GenerateLayerTreeSettings(compositor_deps_, for_child_local_root_frame_,
danakj17216f4d2018-10-16 22:57:321595 screen_info_.rect.size(),
1596 screen_info_.device_scale_factor),
1597 compositor_deps_->CreateUkmRecorderFactory());
1598
Fady Samuel1c1ad3692018-11-06 23:28:401599 UpdateSurfaceAndScreenInfo(local_surface_id_allocation_from_parent_,
danakj17216f4d2018-10-16 22:57:321600 compositor_viewport_pixel_size_, screen_info_);
1601 layer_tree_view_->SetRasterColorSpace(
1602 screen_info_.color_space.GetRasterColorSpace());
1603 layer_tree_view_->SetContentSourceId(current_content_source_id_);
1604 // For background pages and certain tests, we don't want to trigger
1605 // LayerTreeFrameSink creation.
1606 bool should_generate_frame_sink =
1607 !compositor_never_visible_ && RenderThreadImpl::current();
danakje9be3162018-11-05 21:32:001608 if (!should_generate_frame_sink) {
1609 // Prevents SetVisible() from blink from doing anything.
danakj17216f4d2018-10-16 22:57:321610 layer_tree_view_->SetNeverVisible();
danakj6dcbc5962018-11-16 16:45:421611 } else if (!is_frozen_) {
danakje9be3162018-11-05 21:32:001612 // Begins the compositor's scheduler to start producing frames.
1613 // Don't do this if the RenderWidget is attached to a RenderViewImpl for a
danakjbc6ba9d22018-11-17 04:03:411614 // proxy main frame, as this RenderWidget is frozen then, and won't be
danakje9be3162018-11-05 21:32:001615 // used for compositing until a WebFrameWidget is attached.
1616 StartCompositor();
1617 }
danakj17216f4d2018-10-16 22:57:321618
danakj17216f4d2018-10-16 22:57:321619 DCHECK_NE(MSG_ROUTING_NONE, routing_id_);
1620 layer_tree_view_->SetFrameSinkId(
1621 viz::FrameSinkId(RenderThread::Get()->GetClientId(), routing_id_));
1622
1623 RenderThreadImpl* render_thread = RenderThreadImpl::current();
1624 if (render_thread) {
1625 input_event_queue_ = base::MakeRefCounted<MainThreadEventQueue>(
1626 this, render_thread->GetWebMainThreadScheduler()->InputTaskRunner(),
1627 render_thread->GetWebMainThreadScheduler(), should_generate_frame_sink);
1628 }
1629
1630 return layer_tree_view_.get();
1631}
1632
danakjde8b7482018-11-15 23:45:021633void RenderWidget::StartCompositor() {
1634 if (!is_hidden_)
1635 layer_tree_view_->SetVisible(true);
1636}
1637
1638void RenderWidget::StopCompositor() {
1639 layer_tree_view_->SetVisible(false);
1640 // Drop all gpu resources, this makes SetVisible(true) more expensive/slower
1641 // but we don't expect to use this RenderWidget again until some possible
1642 // future navigation. This brings us a bit closer to emulating deleting the
1643 // RenderWidget instead of just stopping the compositor.
1644 layer_tree_view_->ReleaseLayerTreeFrameSink();
1645}
1646
danakj6dcbc5962018-11-16 16:45:421647void RenderWidget::SetIsFrozen(bool is_frozen) {
1648 DCHECK_NE(is_frozen, is_frozen_);
1649 is_frozen_ = is_frozen;
1650 if (is_frozen)
danakjde8b7482018-11-15 23:45:021651 StopCompositor();
1652 else
1653 StartCompositor();
1654}
1655
danakjf87460792018-12-04 20:14:221656void RenderWidget::WarmupCompositor() {
1657 DCHECK(is_frozen_);
1658
1659 // Tests have no RenderThreadImpl /o\.
1660 if (!RenderThreadImpl::current())
1661 return;
1662
1663 // Keeping things simple. This would cancel any outstanding warmup if we
1664 // happened to have one (this should be basically impossible). This avoids any
1665 // extra book keeping about the outstanding reqeust.
1666 warmup_weak_ptr_factory_.InvalidateWeakPtrs();
1667 // And if we already did a warmup then we're done.
1668 if (warmup_frame_sink_)
1669 return;
1670
1671 // Mark us pending the warmup frame sink *before* calling
1672 // DoRequestNewLayerTreeFrameSink() as it may run the reply callback
1673 // synchronously. So we don't want to change any state after the call
1674 // to DoRequestNewLayerTreeFrameSink() here.
1675 warmup_frame_sink_request_pending_ = true;
1676
1677 auto cb = base::BindOnce(&RenderWidget::OnReplyForWarmupCompositor,
1678 warmup_weak_ptr_factory_.GetWeakPtr());
1679 DoRequestNewLayerTreeFrameSink(std::move(cb));
1680}
1681
1682void RenderWidget::OnReplyForWarmupCompositor(
1683 std::unique_ptr<cc::LayerTreeFrameSink> sink) {
1684 warmup_frame_sink_request_pending_ = false;
1685
1686 if (after_warmup_callback_)
1687 std::move(after_warmup_callback_).Run(std::move(sink));
1688 else
1689 warmup_frame_sink_ = std::move(sink);
1690}
1691
1692void RenderWidget::AbortWarmupCompositor() {
1693 warmup_frame_sink_request_pending_ = false;
1694 // Drop any pending warmup.
1695 warmup_weak_ptr_factory_.InvalidateWeakPtrs();
1696 // And drop any completed one.
1697 warmup_frame_sink_.reset();
1698
1699 // If we had saved a callback to run after warmup, just do so now indicating
1700 // failure.
1701 if (after_warmup_callback_)
1702 std::move(after_warmup_callback_).Run(nullptr);
1703}
1704
[email protected]2533ce12009-05-09 00:02:241705void RenderWidget::DoDeferredClose() {
Albert J. Wong3c93c182018-09-27 17:29:431706 Send(new WidgetHostMsg_Close(routing_id_));
[email protected]2533ce12009-05-09 00:02:241707}
1708
Blink Reformat1c4d759e2017-04-09 16:34:541709void RenderWidget::CloseWidgetSoon() {
skyostiled8969c2015-07-20 16:57:081710 DCHECK(content::RenderThread::Get());
danakj6dcbc5962018-11-16 16:45:421711 if (is_frozen_) {
1712 // This widget is currently not active. The active main frame widget is in a
[email protected]e1c3a552012-05-04 20:51:321713 // different process. Have the browser route the close request to the
1714 // active widget instead, so that the correct unload handlers are run.
Albert J. Wongfb64e142018-10-16 01:10:451715 Send(new WidgetHostMsg_RouteCloseEvent(routing_id_));
[email protected]e1c3a552012-05-04 20:51:321716 return;
1717 }
1718
danakj292b63e2018-11-19 20:47:061719 // Prevent compositor from setting up new IPC channels, since we know a
1720 // WidgetMsg_Close is coming. We do this immediately, not in DoDeferredClose,
1721 // as the caller (eg WebPagePopupImpl) may start tearing down things after
1722 // calling this method, including detaching the frame from this RenderWidget.
1723 // Then trying to make a LayerTreeFrameSink would crash.
1724 // https://crbug.com/906340
1725 host_will_close_this_ = true;
1726
initial.commit09911bf2008-07-26 23:55:291727 // If a page calls window.close() twice, we'll end up here twice, but that's
1728 // OK. It is safe to send multiple Close messages.
danakj292b63e2018-11-19 20:47:061729 //
[email protected]2533ce12009-05-09 00:02:241730 // Ask the RenderWidgetHost to initiate close. We could be called from deep
1731 // in Javascript. If we ask the RendwerWidgetHost to close now, the window
1732 // could be closed before the JS finishes executing. So instead, post a
1733 // message back to the message loop, which won't run until the JS is
1734 // complete, and then the Close message can be sent.
Hajime Hoshi315a61f2018-08-14 17:27:281735 GetCleanupTaskRunner()->PostTask(
1736 FROM_HERE, base::BindOnce(&RenderWidget::DoDeferredClose, this));
initial.commit09911bf2008-07-26 23:55:291737}
1738
1739void RenderWidget::Close() {
danakj6dcbc5962018-11-16 16:45:421740 // This was done immediately in the |for_child_local_root_frame_| case in the
1741 // OnClose() IPC handler.
1742 if (!for_child_local_root_frame_)
danakj5c5269b2018-11-06 00:39:111743 CloseWebWidget();
1744
danakja6c10012018-07-06 14:25:361745 layer_tree_view_.reset();
Albert J. Wong7bbf22d2018-12-20 00:27:271746 if (delegate())
1747 delegate()->DidCloseWidget();
Albert J. Wongd593de532018-09-06 05:42:151748 // Note the ACK is a control message going to the RenderProcessHost.
Albert J. Wong3c93c182018-09-27 17:29:431749 RenderThread::Get()->Send(new WidgetHostMsg_Close_ACK(routing_id()));
Ken Buchananed449bb2018-02-01 21:02:051750}
1751
1752void RenderWidget::CloseWebWidget() {
danakj35314ab2018-10-19 16:37:341753 // If the browser has not sent OnDisableDeviceEmulation, we have an emulator
1754 // hanging out still. Destroying it must happen *after* the IPC route is
1755 // removed so that another IPC does not arrive and re-create the emulator
1756 // during closing.
1757 //
1758 // This destruction is normally part of an IPC and expects objects to be alive
1759 // that would be alive while the IPC route is active such as the
1760 // |layer_tree_view_|. So we ensure that it is the first thing to be
1761 // destroyed here before deleting things from the RenderWidget or the
Albert J. Wong7bbf22d2018-12-20 00:27:271762 // delegate().
danakj35314ab2018-10-19 16:37:341763 //
1764 // TODO(danakj): The emulator could reset to non-emulated values in an
1765 // explicit method call (instead of in the destructor) that occurs when
1766 // emulation is disabled, but does not need to occur during RenderWidget
1767 // closing. Then we would not have to destroy this so carefully.
1768 screen_metrics_emulator_.reset();
1769
danakj5c5269b2018-11-06 00:39:111770 // Informs the WebWidget that compositor is being destroyed, so it can remove
1771 // references to it first.
1772 //
Albert J. Wong7bbf22d2018-12-20 00:27:271773 // When delegate() is present, the RenderWidget is for a main frame,
danakj5c5269b2018-11-06 00:39:111774 // and the GetWebWidget() is not the same as |webwidget_internal_|. However
1775 // that widget is responsible for doing WillCloseLayerTreeView() on the
1776 // |webwidget_internal_|, not us. Otherwise, they are the same and this is
1777 // notifying |webwidget_internal_|.
1778 GetWebWidget()->WillCloseLayerTreeView();
1779
Albert J. Wong7bbf22d2018-12-20 00:27:271780 // While the wrapping WebWidget from an delegate() is responsible for
danakj5c5269b2018-11-06 00:39:111781 // doing WillCloseLayerTreeView() on the |webwidget_internal_|, this class is
1782 // responsible for calling Close() on it. Notably, then, the wrapping
1783 // WebWidget does not.
1784 webwidget_internal_->Close();
1785 webwidget_internal_ = nullptr;
initial.commit09911bf2008-07-26 23:55:291786}
1787
Mitsuru Oshima3c07b332018-02-06 04:56:501788void RenderWidget::UpdateWebViewWithDeviceScaleFactor() {
1789 blink::WebFrameWidget* frame_widget = GetFrameWidget();
1790 blink::WebFrame* current_frame =
1791 frame_widget ? frame_widget->LocalRoot() : nullptr;
1792 blink::WebView* webview = current_frame ? current_frame->View() : nullptr;
1793 if (webview) {
danakj4b347212018-07-04 17:55:171794 if (compositor_deps_->IsUseZoomForDSFEnabled())
Fady Samuela863f152018-03-09 16:10:031795 webview->SetZoomFactorForDeviceScaleFactor(
1796 GetWebScreenInfo().device_scale_factor);
Mitsuru Oshima3c07b332018-02-06 04:56:501797 else
Fady Samuela863f152018-03-09 16:10:031798 webview->SetDeviceScaleFactor(GetWebScreenInfo().device_scale_factor);
Mitsuru Oshima3c07b332018-02-06 04:56:501799
1800 webview->GetSettings()->SetPreferCompositingToLCDTextEnabled(
Christopher Camerone9a30c12018-03-07 08:23:381801 PreferCompositingToLCDText(compositor_deps_,
Fady Samuela863f152018-03-09 16:10:031802 GetWebScreenInfo().device_scale_factor));
Mitsuru Oshima3c07b332018-02-06 04:56:501803 }
1804}
1805
Ehsan Karamad655d7b8a2018-01-12 18:38:381806blink::WebFrameWidget* RenderWidget::GetFrameWidget() const {
danakj89ae4532018-12-12 22:52:551807 // TODO(danakj): Remove this check and don't call this method for non-frames.
1808 if (!for_frame())
1809 return nullptr;
1810 // TODO(danakj): Is this needed? IPCs stop after closing, but code used to
1811 // check for a null WebWidget.
1812 if (closing_)
Ehsan Karamad655d7b8a2018-01-12 18:38:381813 return nullptr;
1814
danakj89ae4532018-12-12 22:52:551815 blink::WebWidget* widget;
Albert J. Wong7bbf22d2018-12-20 00:27:271816 if (delegate()) {
danakj89ae4532018-12-12 22:52:551817 // Main frame WebFrameWidgets are held by the delegate, the internal widget
1818 // points directly to the WebView.
1819 // TODO(ekaramad): We should drop IPCs when |is_frozen_| instead of
1820 // handling them and finding a null here. However there is also the case
1821 // of the frame being detached without the widget being frozen to be
1822 // resolved (https://crbug.com/906340). So for now this can return null.
Albert J. Wong7bbf22d2018-12-20 00:27:271823 widget = delegate()->GetWebWidgetForWidget();
danakj89ae4532018-12-12 22:52:551824 } else {
1825 // Subframes always have a WebFrameWidget themselves.
1826 widget = webwidget_internal_;
Ehsan Karamad655d7b8a2018-01-12 18:38:381827 }
danakj89ae4532018-12-12 22:52:551828 return static_cast<blink::WebFrameWidget*>(widget);
Ehsan Karamad655d7b8a2018-01-12 18:38:381829}
1830
bokan841fdc72016-10-06 00:16:351831void RenderWidget::ScreenRectToEmulatedIfNeeded(WebRect* window_rect) const {
1832 DCHECK(window_rect);
1833 float scale = popup_origin_scale_for_emulation_;
1834 if (!scale)
1835 return;
1836 window_rect->x =
1837 popup_view_origin_for_emulation_.x() +
1838 (window_rect->x - popup_screen_origin_for_emulation_.x()) / scale;
1839 window_rect->y =
1840 popup_view_origin_for_emulation_.y() +
1841 (window_rect->y - popup_screen_origin_for_emulation_.y()) / scale;
1842}
1843
1844void RenderWidget::EmulatedToScreenRectIfNeeded(WebRect* window_rect) const {
1845 DCHECK(window_rect);
1846 float scale = popup_origin_scale_for_emulation_;
1847 if (!scale)
1848 return;
1849 window_rect->x =
1850 popup_screen_origin_for_emulation_.x() +
1851 (window_rect->x - popup_view_origin_for_emulation_.x()) * scale;
1852 window_rect->y =
1853 popup_screen_origin_for_emulation_.y() +
1854 (window_rect->y - popup_view_origin_for_emulation_.y()) * scale;
1855}
1856
Blink Reformat1c4d759e2017-04-09 16:34:541857WebRect RenderWidget::WindowRect() {
bokan841fdc72016-10-06 00:16:351858 WebRect rect;
bokan6b08cd22016-10-05 00:55:211859 if (pending_window_rect_count_) {
1860 // NOTE(mbelshe): If there is a pending_window_rect_, then getting
1861 // the RootWindowRect is probably going to return wrong results since the
1862 // browser may not have processed the Move yet. There isn't really anything
1863 // good to do in this case, and it shouldn't happen - since this size is
1864 // only really needed for windowToScreen, which is only used for Popups.
bokan841fdc72016-10-06 00:16:351865 rect = pending_window_rect_;
1866 } else {
1867 rect = window_screen_rect_;
bokan6b08cd22016-10-05 00:55:211868 }
[email protected]2533ce12009-05-09 00:02:241869
bokan841fdc72016-10-06 00:16:351870 ScreenRectToEmulatedIfNeeded(&rect);
1871 return rect;
bokan6b08cd22016-10-05 00:55:211872}
1873
Blink Reformat1c4d759e2017-04-09 16:34:541874WebRect RenderWidget::ViewRect() {
Albert J. Wong3c93c182018-09-27 17:29:431875 WebRect rect = widget_screen_rect_;
bokan841fdc72016-10-06 00:16:351876 ScreenRectToEmulatedIfNeeded(&rect);
1877 return rect;
initial.commit09911bf2008-07-26 23:55:291878}
1879
Blink Reformat1c4d759e2017-04-09 16:34:541880void RenderWidget::SetToolTipText(const blink::WebString& text,
[email protected]8a9d6ca32011-06-06 20:11:301881 WebTextDirection hint) {
Albert J. Wong3c93c182018-09-27 17:29:431882 Send(new WidgetHostMsg_SetTooltipText(routing_id_, text.Utf16(), hint));
[email protected]8a9d6ca32011-06-06 20:11:301883}
1884
Blink Reformat1c4d759e2017-04-09 16:34:541885void RenderWidget::SetWindowRect(const WebRect& rect_in_screen) {
danakj81d601e62018-10-09 22:15:051886 // This path is for the renderer to change the on-screen position/size of
1887 // the widget by changing its window rect. This is not possible for
1888 // RenderWidgets whose position/size are controlled by layout from another
danakj6dcbc5962018-11-16 16:45:421889 // frame tree (ie. child local root frames), as the window rect can only be
1890 // set by the browser.
1891 if (for_child_local_root_frame_)
danakj81d601e62018-10-09 22:15:051892 return;
1893
oshima33ec97cd2015-12-14 19:40:241894 WebRect window_rect = rect_in_screen;
bokan841fdc72016-10-06 00:16:351895 EmulatedToScreenRectIfNeeded(&window_rect);
[email protected]b2e4c70132013-10-03 02:07:511896
danakj65f423b72018-10-10 17:21:201897 if (resizing_mode_selector_->is_synchronous_mode()) {
Kent Tamura21d1de62018-12-10 04:45:201898 // This is a web-test-only path. At one point, it was planned to be
danakj65f423b72018-10-10 17:21:201899 // removed. See https://crbug.com/309760.
bokanc007c3a2015-02-03 07:15:561900 SetWindowRectSynchronously(window_rect);
danakj65f423b72018-10-10 17:21:201901 return;
1902 }
1903
1904 if (show_callback_) {
1905 // The widget is not shown yet. Delay the |window_rect| being sent to the
1906 // browser until Show() is called so it can be sent with that IPC, once the
1907 // browser is ready for the info.
1908 initial_rect_ = window_rect;
1909 } else {
1910 Send(new WidgetHostMsg_RequestSetBounds(routing_id_, window_rect));
1911 SetPendingWindowRect(window_rect);
initial.commit09911bf2008-07-26 23:55:291912 }
1913}
1914
[email protected]2533ce12009-05-09 00:02:241915void RenderWidget::SetPendingWindowRect(const WebRect& rect) {
1916 pending_window_rect_ = rect;
1917 pending_window_rect_count_++;
[email protected]2533ce12009-05-09 00:02:241918
bokan6b08cd22016-10-05 00:55:211919 // Popups don't get size updates back from the browser so just store the set
1920 // values.
danakj89ae4532018-12-12 22:52:551921 if (!for_frame()) {
Blink Reformat1c4d759e2017-04-09 16:34:541922 window_screen_rect_ = rect;
Albert J. Wong3c93c182018-09-27 17:29:431923 widget_screen_rect_ = rect;
[email protected]2533ce12009-05-09 00:02:241924 }
[email protected]d4547452008-08-28 18:36:371925}
1926
wjmacleanee244e02017-05-26 21:06:071927void RenderWidget::OnShowContextMenu(ui::MenuSourceType source_type,
1928 const gfx::Point& location) {
wjmacleanee244e02017-05-26 21:06:071929 has_host_context_menu_location_ = true;
1930 host_context_menu_location_ = location;
1931 if (GetWebWidget()) {
1932 GetWebWidget()->ShowContextMenu(
1933 static_cast<blink::WebMenuSourceType>(source_type));
1934 }
1935 has_host_context_menu_location_ = false;
1936}
1937
[email protected]fa7b1dc2010-06-23 17:53:041938void RenderWidget::OnImeSetComposition(
[email protected]fcf75d42013-12-03 20:11:261939 const base::string16& text,
Ryan Landay9e42fd742017-08-12 01:59:111940 const std::vector<WebImeTextSpan>& ime_text_spans,
chongz7eb752802016-01-27 21:28:071941 const gfx::Range& replacement_range,
Ryan Landay9e42fd742017-08-12 01:59:111942 int selection_start,
1943 int selection_end) {
ekaramad5aff1942017-01-06 01:26:351944 if (!ShouldHandleImeEvents())
1945 return;
1946
brettw4b461082016-11-19 18:55:161947#if BUILDFLAG(ENABLE_PLUGINS)
EhsanK22d482e2017-08-10 17:29:491948 if (auto* plugin = GetFocusedPepperPluginInsideWidget()) {
Ryan Landay9e42fd742017-08-12 01:59:111949 plugin->render_frame()->OnImeSetComposition(text, ime_text_spans,
EhsanK22d482e2017-08-10 17:29:491950 selection_start, selection_end);
ekaramad2a46d632016-07-19 13:33:091951 return;
1952 }
1953#endif
[email protected]66fca5bc2013-05-23 06:58:291954 ImeEventGuard guard(this);
ekaramad2daaf672016-11-10 20:29:011955 blink::WebInputMethodController* controller = GetInputMethodController();
ekaramad2daaf672016-11-10 20:29:011956 if (!controller ||
Blink Reformat1c4d759e2017-04-09 16:34:541957 !controller->SetComposition(
Ryan Landay9e42fd742017-08-12 01:59:111958 WebString::FromUTF16(text), WebVector<WebImeTextSpan>(ime_text_spans),
ekaramadce32ef9f2017-02-09 17:33:561959 replacement_range.IsValid()
1960 ? WebRange(replacement_range.start(), replacement_range.length())
1961 : WebRange(),
1962 selection_start, selection_end)) {
[email protected]fa7b1dc2010-06-23 17:53:041963 // If we failed to set the composition text, then we need to let the browser
1964 // process to cancel the input method's ongoing composition session, to make
1965 // sure we are in a consistent state.
Dave Tapuska04bc5ee92018-04-17 19:03:311966 if (mojom::WidgetInputHandlerHost* host =
1967 widget_input_handler_manager_->GetWidgetInputHandlerHost()) {
1968 host->ImeCancelComposition();
Dave Tapuska139a72f2017-09-06 21:57:031969 }
[email protected]7f00efa2010-04-15 05:01:261970 }
nonafa291792016-08-10 02:36:181971 UpdateCompositionInfo(false /* not an immediate request */);
[email protected]fa7b1dc2010-06-23 17:53:041972}
1973
rlanday7efe2302017-01-11 00:14:281974void RenderWidget::OnImeCommitText(
1975 const base::string16& text,
Ryan Landay9e42fd742017-08-12 01:59:111976 const std::vector<WebImeTextSpan>& ime_text_spans,
rlanday7efe2302017-01-11 00:14:281977 const gfx::Range& replacement_range,
1978 int relative_cursor_pos) {
ekaramad5aff1942017-01-06 01:26:351979 if (!ShouldHandleImeEvents())
1980 return;
1981
brettw4b461082016-11-19 18:55:161982#if BUILDFLAG(ENABLE_PLUGINS)
EhsanK22d482e2017-08-10 17:29:491983 if (auto* plugin = GetFocusedPepperPluginInsideWidget()) {
1984 plugin->render_frame()->OnImeCommitText(text, replacement_range,
1985 relative_cursor_pos);
ekaramad2a46d632016-07-19 13:33:091986 return;
1987 }
1988#endif
[email protected]66fca5bc2013-05-23 06:58:291989 ImeEventGuard guard(this);
fsamuele8326c742016-01-12 00:49:391990 input_handler_->set_handling_input_event(true);
ekaramadb6483a052017-02-10 02:23:261991 if (auto* controller = GetInputMethodController()) {
Blink Reformat1c4d759e2017-04-09 16:34:541992 controller->CommitText(
Ryan Landay9e42fd742017-08-12 01:59:111993 WebString::FromUTF16(text), WebVector<WebImeTextSpan>(ime_text_spans),
ekaramadce32ef9f2017-02-09 17:33:561994 replacement_range.IsValid()
1995 ? WebRange(replacement_range.start(), replacement_range.length())
1996 : WebRange(),
1997 relative_cursor_pos);
ekaramadb6483a052017-02-10 02:23:261998 }
aelias87b8f7c2016-09-14 03:19:291999 input_handler_->set_handling_input_event(false);
2000 UpdateCompositionInfo(false /* not an immediate request */);
2001}
2002
2003void RenderWidget::OnImeFinishComposingText(bool keep_selection) {
ekaramad5aff1942017-01-06 01:26:352004 if (!ShouldHandleImeEvents())
2005 return;
2006
brettw4b461082016-11-19 18:55:162007#if BUILDFLAG(ENABLE_PLUGINS)
EhsanK22d482e2017-08-10 17:29:492008 if (auto* plugin = GetFocusedPepperPluginInsideWidget()) {
2009 plugin->render_frame()->OnImeFinishComposingText(keep_selection);
aelias87b8f7c2016-09-14 03:19:292010 return;
2011 }
2012#endif
2013
changwan38c3eb612016-12-09 01:45:562014 if (!GetWebWidget())
aelias87b8f7c2016-09-14 03:19:292015 return;
2016 ImeEventGuard guard(this);
2017 input_handler_->set_handling_input_event(true);
ekaramad2daaf672016-11-10 20:29:012018 if (auto* controller = GetInputMethodController()) {
Blink Reformat1c4d759e2017-04-09 16:34:542019 controller->FinishComposingText(
2020 keep_selection ? WebInputMethodController::kKeepSelection
2021 : WebInputMethodController::kDoNotKeepSelection);
ekaramad2daaf672016-11-10 20:29:012022 }
fsamuele8326c742016-01-12 00:49:392023 input_handler_->set_handling_input_event(false);
nonafa291792016-08-10 02:36:182024 UpdateCompositionInfo(false /* not an immediate request */);
initial.commit09911bf2008-07-26 23:55:292025}
2026
Christopher Camerone9a30c12018-03-07 08:23:382027void RenderWidget::UpdateSurfaceAndScreenInfo(
Fady Samuel1c1ad3692018-11-06 23:28:402028 const viz::LocalSurfaceIdAllocation& new_local_surface_id_allocation,
Christopher Cameronaad15a0d2018-03-09 03:47:332029 const gfx::Size& new_compositor_viewport_pixel_size,
Christopher Camerone9a30c12018-03-07 08:23:382030 const ScreenInfo& new_screen_info) {
Christopher Camerone9a30c12018-03-07 08:23:382031 bool orientation_changed =
2032 screen_info_.orientation_angle != new_screen_info.orientation_angle ||
2033 screen_info_.orientation_type != new_screen_info.orientation_type;
2034 bool web_device_scale_factor_changed =
2035 screen_info_.device_scale_factor != new_screen_info.device_scale_factor;
Fady Samuela863f152018-03-09 16:10:032036 ScreenInfo previous_original_screen_info = GetOriginalScreenInfo();
Christopher Cameronbe7ecea2018-02-27 00:47:192037
Fady Samuel1c1ad3692018-11-06 23:28:402038 local_surface_id_allocation_from_parent_ = new_local_surface_id_allocation;
Christopher Cameronaad15a0d2018-03-09 03:47:332039 compositor_viewport_pixel_size_ = new_compositor_viewport_pixel_size;
Christopher Camerone9a30c12018-03-07 08:23:382040 screen_info_ = new_screen_info;
Christopher Cameronbe7ecea2018-02-27 00:47:192041
Kinuko Yasudabef2b642019-01-24 04:53:332042 if (layer_tree_view_) {
2043 layer_tree_view_->SetViewportVisibleRect(ViewportVisibleRect());
2044 // Note carefully that the DSF specified in |new_screen_info| is not the
2045 // DSF used by the compositor during device emulation!
2046 layer_tree_view_->SetViewportSizeAndScale(
2047 compositor_viewport_pixel_size_,
2048 GetOriginalScreenInfo().device_scale_factor,
2049 local_surface_id_allocation_from_parent_);
2050 }
Christopher Cameronbe7ecea2018-02-27 00:47:192051
Christopher Camerone9a30c12018-03-07 08:23:382052 if (orientation_changed)
2053 OnOrientationChange();
Christopher Cameronbe7ecea2018-02-27 00:47:192054
Fady Samuela863f152018-03-09 16:10:032055 if (previous_original_screen_info != GetOriginalScreenInfo()) {
Christopher Camerone9a30c12018-03-07 08:23:382056 for (auto& observer : render_frame_proxies_)
Fady Samuela863f152018-03-09 16:10:032057 observer.OnScreenInfoChanged(GetOriginalScreenInfo());
Christopher Camerone9a30c12018-03-07 08:23:382058
2059 // Notify all embedded BrowserPlugins of the updated ScreenInfo.
2060 for (auto& observer : browser_plugins_)
Fady Samuela863f152018-03-09 16:10:032061 observer.ScreenInfoChanged(GetOriginalScreenInfo());
Christopher Camerone9a30c12018-03-07 08:23:382062 }
2063
2064 if (web_device_scale_factor_changed)
Christopher Cameronbe7ecea2018-02-27 00:47:192065 UpdateWebViewWithDeviceScaleFactor();
oshimad5279032015-12-16 18:22:332066}
2067
danakja2c9d0a92018-07-25 20:01:182068void RenderWidget::SetWindowRectSynchronously(
2069 const gfx::Rect& new_window_rect) {
2070 VisualProperties visual_properties;
2071 visual_properties.screen_info = screen_info_;
2072 visual_properties.new_size = new_window_rect.size();
2073 visual_properties.compositor_viewport_pixel_size = gfx::ScaleToCeiledSize(
2074 new_window_rect.size(), GetWebScreenInfo().device_scale_factor);
2075 visual_properties.visible_viewport_size = new_window_rect.size();
2076 visual_properties.is_fullscreen_granted = is_fullscreen_granted_;
2077 visual_properties.display_mode = display_mode_;
Fady Samuel84d8a1f72018-11-08 00:26:172078 visual_properties.local_surface_id_allocation =
2079 local_surface_id_allocation_from_parent_;
danakja2c9d0a92018-07-25 20:01:182080 // We are resizing the window from the renderer, so allocate a new
2081 // viz::LocalSurfaceId to avoid surface invariants violations in tests.
Kinuko Yasudabef2b642019-01-24 04:53:332082 if (layer_tree_view_)
2083 layer_tree_view_->RequestNewLocalSurfaceId();
danakja2c9d0a92018-07-25 20:01:182084 SynchronizeVisualProperties(visual_properties);
2085
Albert J. Wong3c93c182018-09-27 17:29:432086 widget_screen_rect_ = new_window_rect;
danakja2c9d0a92018-07-25 20:01:182087 window_screen_rect_ = new_window_rect;
danakj65f423b72018-10-10 17:21:202088 if (show_callback_) {
2089 // Tests may call here directly to control the window rect. If
2090 // Show() did not happen yet, the rect is stored to be passed to the
2091 // browser when the RenderWidget requests Show().
danakja2c9d0a92018-07-25 20:01:182092 initial_rect_ = new_window_rect;
danakj65f423b72018-10-10 17:21:202093 }
danakja2c9d0a92018-07-25 20:01:182094}
2095
Vladimir Levin98d76dad2018-04-21 00:21:292096void RenderWidget::UpdateCaptureSequenceNumber(
2097 uint32_t capture_sequence_number) {
2098 if (capture_sequence_number == last_capture_sequence_number_)
2099 return;
2100 last_capture_sequence_number_ = capture_sequence_number;
2101
2102 // Notify observers of the new capture sequence number.
2103 for (auto& observer : render_frame_proxies_)
2104 observer.UpdateCaptureSequenceNumber(capture_sequence_number);
2105 for (auto& observer : browser_plugins_)
2106 observer.UpdateCaptureSequenceNumber(capture_sequence_number);
2107}
2108
[email protected]4873c7d2009-07-16 06:36:282109void RenderWidget::OnSetTextDirection(WebTextDirection direction) {
EhsanK955ba582017-11-30 21:14:402110 if (auto* frame = GetFocusedWebLocalFrameInWidget())
2111 frame->SetTextDirection(direction);
[email protected]07f953332009-03-25 04:31:112112}
2113
Albert J. Wong3c93c182018-09-27 17:29:432114void RenderWidget::OnUpdateScreenRects(const gfx::Rect& widget_screen_rect,
[email protected]80ad8622012-11-07 16:33:032115 const gfx::Rect& window_screen_rect) {
[email protected]b2e4c70132013-10-03 02:07:512116 if (screen_metrics_emulator_) {
Albert J. Wong3c93c182018-09-27 17:29:432117 screen_metrics_emulator_->OnUpdateScreenRects(widget_screen_rect,
mfomitchev2600fd7c2016-02-17 20:53:392118 window_screen_rect);
[email protected]b2e4c70132013-10-03 02:07:512119 } else {
Albert J. Wong3c93c182018-09-27 17:29:432120 SetScreenRects(widget_screen_rect, window_screen_rect);
[email protected]b2e4c70132013-10-03 02:07:512121 }
Albert J. Wong3c93c182018-09-27 17:29:432122 Send(new WidgetHostMsg_UpdateScreenRects_ACK(routing_id()));
[email protected]80ad8622012-11-07 16:33:032123}
2124
kenrbea731792017-01-13 15:10:482125void RenderWidget::OnSetViewportIntersection(
Ken Buchananb2c9e262018-03-10 16:53:312126 const gfx::Rect& viewport_intersection,
Stefan Zager54e25832018-08-14 22:15:312127 const gfx::Rect& compositor_visible_rect,
2128 bool occluded_or_obscured) {
Ehsan Karamadf3ca2a82018-01-20 01:05:252129 if (auto* frame_widget = GetFrameWidget()) {
Ken Buchananb2c9e262018-03-10 16:53:312130 compositor_visible_rect_ = compositor_visible_rect;
Stefan Zager54e25832018-08-14 22:15:312131 frame_widget->SetRemoteViewportIntersection(viewport_intersection,
2132 occluded_or_obscured);
danakja6c10012018-07-06 14:25:362133 layer_tree_view_->SetViewportVisibleRect(ViewportVisibleRect());
kenrbea731792017-01-13 15:10:482134 }
2135}
2136
kenrb04323782017-06-23 01:23:322137void RenderWidget::OnSetIsInert(bool inert) {
danakj89ae4532018-12-12 22:52:552138 if (auto* frame_widget = GetFrameWidget())
Ehsan Karamadf3ca2a82018-01-20 01:05:252139 frame_widget->SetIsInert(inert);
kenrb04323782017-06-23 01:23:322140}
2141
sunxd540a9962018-05-24 22:51:062142void RenderWidget::OnSetInheritedEffectiveTouchAction(
2143 cc::TouchAction touch_action) {
danakj89ae4532018-12-12 22:52:552144 if (auto* frame_widget = GetFrameWidget())
sunxd540a9962018-05-24 22:51:062145 frame_widget->SetInheritedEffectiveTouchAction(touch_action);
sunxd540a9962018-05-24 22:51:062146}
2147
Ken Buchanan8a319fb2017-11-15 18:37:122148void RenderWidget::OnUpdateRenderThrottlingStatus(bool is_throttled,
2149 bool subtree_throttled) {
danakj89ae4532018-12-12 22:52:552150 if (auto* frame_widget = GetFrameWidget())
Ehsan Karamadf3ca2a82018-01-20 01:05:252151 frame_widget->UpdateRenderThrottlingStatus(is_throttled, subtree_throttled);
Ken Buchanan8a319fb2017-11-15 18:37:122152}
2153
paulmeyer90f6c31d2016-11-12 00:17:592154void RenderWidget::OnDragTargetDragEnter(
2155 const std::vector<DropData::Metadata>& drop_meta_data,
Ella Ge80a52dce2017-11-15 18:01:522156 const gfx::PointF& client_point,
2157 const gfx::PointF& screen_point,
paulmeyer90f6c31d2016-11-12 00:17:592158 WebDragOperationsMask ops,
2159 int key_modifiers) {
Ehsan Karamadf3ca2a82018-01-20 01:05:252160 blink::WebFrameWidget* frame_widget = GetFrameWidget();
2161 if (!frame_widget)
paulmeyer90f6c31d2016-11-12 00:17:592162 return;
2163
Ehsan Karamadf3ca2a82018-01-20 01:05:252164 WebDragOperation operation = frame_widget->DragTargetDragEnter(
2165 DropMetaDataToWebDragData(drop_meta_data), client_point, screen_point,
2166 ops, key_modifiers);
paulmeyer90f6c31d2016-11-12 00:17:592167
2168 Send(new DragHostMsg_UpdateDragCursor(routing_id(), operation));
2169}
2170
Ella Ge80a52dce2017-11-15 18:01:522171void RenderWidget::OnDragTargetDragOver(const gfx::PointF& client_point,
2172 const gfx::PointF& screen_point,
paulmeyer90f6c31d2016-11-12 00:17:592173 WebDragOperationsMask ops,
2174 int key_modifiers) {
Ehsan Karamadf3ca2a82018-01-20 01:05:252175 blink::WebFrameWidget* frame_widget = GetFrameWidget();
2176 if (!frame_widget)
paulmeyer90f6c31d2016-11-12 00:17:592177 return;
2178
Ehsan Karamadf3ca2a82018-01-20 01:05:252179 WebDragOperation operation = frame_widget->DragTargetDragOver(
2180 ConvertWindowPointToViewport(client_point), screen_point, ops,
2181 key_modifiers);
paulmeyer90f6c31d2016-11-12 00:17:592182
2183 Send(new DragHostMsg_UpdateDragCursor(routing_id(), operation));
2184}
2185
Ella Ge80a52dce2017-11-15 18:01:522186void RenderWidget::OnDragTargetDragLeave(const gfx::PointF& client_point,
2187 const gfx::PointF& screen_point) {
Ehsan Karamadf3ca2a82018-01-20 01:05:252188 blink::WebFrameWidget* frame_widget = GetFrameWidget();
2189 if (!frame_widget)
paulmeyer90f6c31d2016-11-12 00:17:592190 return;
Ehsan Karamadf3ca2a82018-01-20 01:05:252191
2192 frame_widget
Blink Reformat1c4d759e2017-04-09 16:34:542193 ->DragTargetDragLeave(ConvertWindowPointToViewport(client_point),
kenrb07c272802017-02-07 23:48:172194 screen_point);
paulmeyer90f6c31d2016-11-12 00:17:592195}
2196
2197void RenderWidget::OnDragTargetDrop(const DropData& drop_data,
Ella Ge80a52dce2017-11-15 18:01:522198 const gfx::PointF& client_point,
2199 const gfx::PointF& screen_point,
paulmeyer90f6c31d2016-11-12 00:17:592200 int key_modifiers) {
Ehsan Karamadf3ca2a82018-01-20 01:05:252201 blink::WebFrameWidget* frame_widget = GetFrameWidget();
2202 if (!frame_widget)
paulmeyer90f6c31d2016-11-12 00:17:592203 return;
2204
Ehsan Karamadf3ca2a82018-01-20 01:05:252205 frame_widget->DragTargetDrop(DropDataToWebDragData(drop_data),
2206 ConvertWindowPointToViewport(client_point),
2207 screen_point, key_modifiers);
paulmeyer90f6c31d2016-11-12 00:17:592208}
2209
Ella Ge80a52dce2017-11-15 18:01:522210void RenderWidget::OnDragSourceEnded(const gfx::PointF& client_point,
2211 const gfx::PointF& screen_point,
paulmeyer8fc8ea92016-11-15 05:12:212212 WebDragOperation op) {
Ehsan Karamadf3ca2a82018-01-20 01:05:252213 blink::WebFrameWidget* frame_widget = GetFrameWidget();
2214 if (!frame_widget)
paulmeyer8fc8ea92016-11-15 05:12:212215 return;
2216
Ehsan Karamadf3ca2a82018-01-20 01:05:252217 frame_widget->DragSourceEndedAt(ConvertWindowPointToViewport(client_point),
2218 screen_point, op);
paulmeyer8fc8ea92016-11-15 05:12:212219}
2220
2221void RenderWidget::OnDragSourceSystemDragEnded() {
danakj503545c2019-01-11 19:03:092222 blink::WebFrameWidget* frame_widget = GetFrameWidget();
2223 if (!frame_widget)
paulmeyer8fc8ea92016-11-15 05:12:212224 return;
2225
danakj503545c2019-01-11 19:03:092226 frame_widget->DragSourceSystemDragEnded();
paulmeyer8fc8ea92016-11-15 05:12:212227}
2228
Blink Reformat1c4d759e2017-04-09 16:34:542229void RenderWidget::ShowVirtualKeyboardOnElementFocus() {
okaa398f50b2017-03-21 06:30:262230#if defined(OS_CHROMEOS)
2231 // On ChromeOS, virtual keyboard is triggered only when users leave the
2232 // mouse button or the finger and a text input element is focused at that
2233 // time. Focus event itself shouldn't trigger virtual keyboard.
2234 UpdateTextInputState();
2235#else
changwan75e3b2072017-01-16 02:55:002236 ShowVirtualKeyboard();
okaa398f50b2017-03-21 06:30:262237#endif
boliu7b2be2f2016-11-04 04:58:312238
2239// TODO(rouslan): Fix ChromeOS and Windows 8 behavior of autofill popup with
2240// virtual keyboard.
2241#if !defined(OS_ANDROID)
2242 FocusChangeComplete();
2243#endif
[email protected]0d1ebed12013-08-05 22:01:132244}
2245
fsamuel72464894f2015-12-15 06:59:312246ui::TextInputType RenderWidget::GetTextInputType() {
brettw4b461082016-11-19 18:55:162247#if BUILDFLAG(ENABLE_PLUGINS)
EhsanK22d482e2017-08-10 17:29:492248 if (auto* plugin = GetFocusedPepperPluginInsideWidget())
2249 return plugin->text_input_type();
ekaramad2a46d632016-07-19 13:33:092250#endif
ekaramad5aff1942017-01-06 01:26:352251 if (auto* controller = GetInputMethodController())
Blink Reformat1c4d759e2017-04-09 16:34:542252 return ConvertWebTextInputType(controller->TextInputType());
fsamuel72464894f2015-12-15 06:59:312253 return ui::TEXT_INPUT_TYPE_NONE;
2254}
2255
nonafa291792016-08-10 02:36:182256void RenderWidget::UpdateCompositionInfo(bool immediate_request) {
2257 if (!monitor_composition_info_ && !immediate_request)
2258 return; // Do not calculate composition info if not requested.
nonadac0c7a2016-08-01 02:30:592259
nonafa291792016-08-10 02:36:182260 TRACE_EVENT0("renderer", "RenderWidget::UpdateCompositionInfo");
2261 gfx::Range range;
2262 std::vector<gfx::Rect> character_bounds;
2263
2264 if (GetTextInputType() == ui::TEXT_INPUT_TYPE_NONE) {
2265 // Composition information is only available on editable node.
2266 range = gfx::Range::InvalidRange();
2267 } else {
2268 GetCompositionRange(&range);
2269 GetCompositionCharacterBounds(&character_bounds);
2270 }
2271
2272 if (!immediate_request &&
2273 !ShouldUpdateCompositionInfo(range, character_bounds)) {
fsamuel72464894f2015-12-15 06:59:312274 return;
nonafa291792016-08-10 02:36:182275 }
fsamuel72464894f2015-12-15 06:59:312276 composition_character_bounds_ = character_bounds;
2277 composition_range_ = range;
Dave Tapuska04bc5ee92018-04-17 19:03:312278 if (mojom::WidgetInputHandlerHost* host =
2279 widget_input_handler_manager_->GetWidgetInputHandlerHost()) {
2280 host->ImeCompositionRangeChanged(composition_range_,
2281 composition_character_bounds_);
Dave Tapuska139a72f2017-09-06 21:57:032282 }
fsamuel72464894f2015-12-15 06:59:312283}
2284
Blink Reformat1c4d759e2017-04-09 16:34:542285void RenderWidget::ConvertViewportToWindow(blink::WebRect* rect) {
danakj4b347212018-07-04 17:55:172286 if (compositor_deps_->IsUseZoomForDSFEnabled()) {
Fady Samuela863f152018-03-09 16:10:032287 float reverse = 1 / GetOriginalScreenInfo().device_scale_factor;
oshimad5279032015-12-16 18:22:332288 // TODO(oshima): We may need to allow pixel precision here as the the
oshimaf866dab2015-12-05 00:41:542289 // anchor element can be placed at half pixel.
lfg15b235a32016-08-25 17:45:462290 gfx::Rect window_rect =
2291 gfx::ScaleToEnclosedRect(gfx::Rect(*rect), reverse);
2292 rect->x = window_rect.x();
2293 rect->y = window_rect.y();
2294 rect->width = window_rect.width();
2295 rect->height = window_rect.height();
oshimaf866dab2015-12-05 00:41:542296 }
2297}
2298
Blink Reformat1c4d759e2017-04-09 16:34:542299void RenderWidget::ConvertWindowToViewport(blink::WebFloatRect* rect) {
danakj4b347212018-07-04 17:55:172300 if (compositor_deps_->IsUseZoomForDSFEnabled()) {
Fady Samuela863f152018-03-09 16:10:032301 rect->x *= GetOriginalScreenInfo().device_scale_factor;
2302 rect->y *= GetOriginalScreenInfo().device_scale_factor;
2303 rect->width *= GetOriginalScreenInfo().device_scale_factor;
2304 rect->height *= GetOriginalScreenInfo().device_scale_factor;
oshimaa6985b62016-01-27 08:58:302305 }
2306}
2307
changwan8c342742016-02-26 00:53:392308void RenderWidget::OnRequestTextInputStateUpdate() {
Dave Tapuska9db80842017-07-24 17:24:262309#if defined(OS_ANDROID)
changwan8c342742016-02-26 00:53:392310 DCHECK(!ime_event_guard_);
2311 UpdateSelectionBounds();
changwan75e3b2072017-01-16 02:55:002312 UpdateTextInputStateInternal(false, true /* reply_to_request */);
[email protected]105dffb42013-02-20 03:46:212313#endif
Dave Tapuska9db80842017-07-24 17:24:262314}
[email protected]105dffb42013-02-20 03:46:212315
ekaramadc9b70a72017-03-23 16:14:232316void RenderWidget::OnRequestCompositionUpdates(bool immediate_request,
2317 bool monitor_updates) {
2318 monitor_composition_info_ = monitor_updates;
nonafa291792016-08-10 02:36:182319 if (!immediate_request)
2320 return;
2321 UpdateCompositionInfo(true /* immediate request */);
2322}
2323
[email protected]fcdc5642014-05-09 14:32:242324void RenderWidget::OnOrientationChange() {
Ehsan Karamad655d7b8a2018-01-12 18:38:382325 if (auto* frame_widget = GetFrameWidget()) {
Alex Moshchuk0b409c42017-09-13 02:09:002326 // LocalRoot() might return null for provisional main frames. In this case,
2327 // the frame hasn't committed a navigation and is not swapped into the tree
2328 // yet, so it doesn't make sense to send orientation change events to it.
2329 //
2330 // TODO(https://crbug.com/578349): This check should be cleaned up
2331 // once provisional frames are gone.
Ehsan Karamad655d7b8a2018-01-12 18:38:382332 if (frame_widget->LocalRoot())
2333 frame_widget->LocalRoot()->SendOrientationChangeEvent();
lfg8d649cc2017-04-28 18:04:302334 }
[email protected]fcdc5642014-05-09 14:32:242335}
2336
[email protected]bee16aab2009-08-26 15:55:032337void RenderWidget::SetHidden(bool hidden) {
2338 if (is_hidden_ == hidden)
2339 return;
2340
jdduke8fac9d102014-12-20 02:40:132341 // The status has changed. Tell the RenderThread about it and ensure
2342 // throttled acks are released in case frame production ceases.
[email protected]bee16aab2009-08-26 15:55:032343 is_hidden_ = hidden;
jdduke8fac9d102014-12-20 02:40:132344
Scott Violet1098538e2017-10-05 19:23:332345#if defined(USE_AURA)
Scott Violete46b3352018-09-22 00:37:072346 if (features::IsMultiProcessMash())
Scott Violet1098538e2017-10-05 19:23:332347 RendererWindowTreeClient::Get(routing_id_)->SetVisible(!hidden);
2348#endif
2349
Saman Sami58ed4392018-01-31 20:54:192350 // RenderThreadImpl::current() could be null in tests.
2351 if (RenderThreadImpl::current()) {
2352 if (is_hidden_) {
2353 RenderThreadImpl::current()->WidgetHidden();
2354 first_update_visual_state_after_hidden_ = true;
2355 } else {
2356 RenderThreadImpl::current()->WidgetRestored();
2357 }
Lei Zhang9b359d32017-11-28 00:57:022358 }
alexclarke7fa93942015-10-21 15:37:112359
2360 if (render_widget_scheduling_state_)
2361 render_widget_scheduling_state_->SetHidden(hidden);
danakj1b35e9d2018-11-07 19:09:282362
danakj57f139f2018-11-19 19:23:462363 // When the RenderWidget is frozen, visibility of the compositor is overridden
2364 // to always be hidden to prevent it from using resources that are not needed.
2365 // Unfortunately the main RenderWidget for a RenderView must be marked visible
2366 // even if the RenderView has a proxy main frame (and the RenderWidget is
2367 // frozen), in order for the RenderView to use the visibility signal from the
2368 // RenderWidget. This is bad. But we don't need to show the compositor to
2369 // satisfy that requirement.
2370 if (!is_frozen_)
2371 layer_tree_view_->SetVisible(!is_hidden_);
[email protected]bee16aab2009-08-26 15:55:032372}
2373
danakjf9284772019-01-23 18:29:142374void RenderWidget::SetIsFullscreen(bool fullscreen) {
2375 if (fullscreen == is_fullscreen_granted_)
[email protected]2b624c562011-10-27 22:58:262376 return;
danakjf9284772019-01-23 18:29:142377 is_fullscreen_granted_ = fullscreen;
mikhail.pozdnyakovf2c902a2015-04-14 08:09:122378 if (is_fullscreen_granted_) {
Blink Reformat1c4d759e2017-04-09 16:34:542379 GetWebWidget()->DidEnterFullscreen();
[email protected]2b624c562011-10-27 22:58:262380 } else {
Blink Reformat1c4d759e2017-04-09 16:34:542381 GetWebWidget()->DidExitFullscreen();
[email protected]2b624c562011-10-27 22:58:262382 }
[email protected]2b624c562011-10-27 22:58:262383}
2384
changwanf2a707b2015-10-30 08:22:162385void RenderWidget::OnImeEventGuardStart(ImeEventGuard* guard) {
2386 if (!ime_event_guard_)
2387 ime_event_guard_ = guard;
[email protected]66fca5bc2013-05-23 06:58:292388}
2389
changwanf2a707b2015-10-30 08:22:162390void RenderWidget::OnImeEventGuardFinish(ImeEventGuard* guard) {
Jeremy Roman0b261f42018-08-28 15:36:012391 if (ime_event_guard_ != guard)
changwanf2a707b2015-10-30 08:22:162392 return;
changwanf2a707b2015-10-30 08:22:162393 ime_event_guard_ = nullptr;
2394
[email protected]66fca5bc2013-05-23 06:58:292395 // While handling an ime event, text input state and selection bounds updates
2396 // are ignored. These must explicitly be updated once finished handling the
2397 // ime event.
2398 UpdateSelectionBounds();
[email protected]cb9e2632013-06-18 11:26:472399#if defined(OS_ANDROID)
changwan75e3b2072017-01-16 02:55:002400 if (guard->show_virtual_keyboard())
2401 ShowVirtualKeyboard();
2402 else
2403 UpdateTextInputState();
[email protected]cb9e2632013-06-18 11:26:472404#endif
[email protected]66fca5bc2013-05-23 06:58:292405}
2406
[email protected]7c8873e2013-02-05 08:03:012407void RenderWidget::GetSelectionBounds(gfx::Rect* focus, gfx::Rect* anchor) {
brettw4b461082016-11-19 18:55:162408#if BUILDFLAG(ENABLE_PLUGINS)
EhsanK22d482e2017-08-10 17:29:492409 if (auto* plugin = GetFocusedPepperPluginInsideWidget()) {
ekaramad2a46d632016-07-19 13:33:092410 // TODO(kinaba) http://crbug.com/101101
2411 // Current Pepper IME API does not handle selection bounds. So we simply
2412 // use the caret position as an empty range for now. It will be updated
2413 // after Pepper API equips features related to surrounding text retrieval.
EhsanK22d482e2017-08-10 17:29:492414 blink::WebRect caret(plugin->GetCaretBounds());
Blink Reformat1c4d759e2017-04-09 16:34:542415 ConvertViewportToWindow(&caret);
ekaramad2a46d632016-07-19 13:33:092416 *focus = caret;
2417 *anchor = caret;
2418 return;
2419 }
2420#endif
[email protected]7c8873e2013-02-05 08:03:012421 WebRect focus_webrect;
2422 WebRect anchor_webrect;
Blink Reformat1c4d759e2017-04-09 16:34:542423 GetWebWidget()->SelectionBounds(focus_webrect, anchor_webrect);
2424 ConvertViewportToWindow(&focus_webrect);
2425 ConvertViewportToWindow(&anchor_webrect);
oshima33ec97cd2015-12-14 19:40:242426 *focus = focus_webrect;
2427 *anchor = anchor_webrect;
[email protected]73bf95812011-10-12 11:38:322428}
2429
[email protected]e99ef6f2011-10-16 01:13:002430void RenderWidget::UpdateSelectionBounds() {
jdduke1aebad8e2015-07-22 23:25:082431 TRACE_EVENT0("renderer", "RenderWidget::UpdateSelectionBounds");
lfg8ff33912016-09-13 20:59:212432 if (!GetWebWidget())
[email protected]e99ef6f2011-10-16 01:13:002433 return;
changwanf2a707b2015-10-30 08:22:162434 if (ime_event_guard_)
[email protected]66fca5bc2013-05-23 06:58:292435 return;
[email protected]e99ef6f2011-10-16 01:13:002436
mohsenb0eeba72015-08-09 06:20:082437#if defined(USE_AURA)
2438 // TODO(mohsen): For now, always send explicit selection IPC notifications for
2439 // Aura beucause composited selection updates are not working for webview tags
2440 // which regresses IME inside webview. Remove this when composited selection
2441 // updates are fixed for webviews. See, http://crbug.com/510568.
2442 bool send_ipc = true;
2443#else
jddukeacf809e2014-09-23 20:38:382444 // With composited selection updates, the selection bounds will be reported
2445 // directly by the compositor, in which case explicit IPC selection
2446 // notifications should be suppressed.
mohsenb0eeba72015-08-09 06:20:082447 bool send_ipc =
Blink Reformat1c4d759e2017-04-09 16:34:542448 !blink::WebRuntimeFeatures::IsCompositedSelectionUpdateEnabled();
mohsenb0eeba72015-08-09 06:20:082449#endif
2450 if (send_ipc) {
Albert J. Wong3c93c182018-09-27 17:29:432451 WidgetHostMsg_SelectionBounds_Params params;
EhsanK955ba582017-11-30 21:14:402452 params.is_anchor_first = false;
jddukeacf809e2014-09-23 20:38:382453 GetSelectionBounds(&params.anchor_rect, &params.focus_rect);
2454 if (selection_anchor_rect_ != params.anchor_rect ||
2455 selection_focus_rect_ != params.focus_rect) {
2456 selection_anchor_rect_ = params.anchor_rect;
2457 selection_focus_rect_ = params.focus_rect;
EhsanK955ba582017-11-30 21:14:402458 if (auto* focused_frame = GetFocusedWebLocalFrameInWidget()) {
2459 focused_frame->SelectionTextDirection(params.focus_dir,
2460 params.anchor_dir);
2461 params.is_anchor_first = focused_frame->IsSelectionAnchorFirst();
2462 }
Albert J. Wong3c93c182018-09-27 17:29:432463 Send(new WidgetHostMsg_SelectionBoundsChanged(routing_id_, params));
jddukeacf809e2014-09-23 20:38:382464 }
[email protected]58b48a0d2012-06-13 07:01:352465 }
jddukeacf809e2014-09-23 20:38:382466
nonafa291792016-08-10 02:36:182467 UpdateCompositionInfo(false /* not an immediate request */);
[email protected]e99ef6f2011-10-16 01:13:002468}
2469
lfgb00fcad2016-07-14 14:16:332470void RenderWidget::DidAutoResize(const gfx::Size& new_size) {
2471 WebRect new_size_in_window(0, 0, new_size.width(), new_size.height());
Blink Reformat1c4d759e2017-04-09 16:34:542472 ConvertViewportToWindow(&new_size_in_window);
lfgb00fcad2016-07-14 14:16:332473 if (size_.width() != new_size_in_window.width ||
2474 size_.height() != new_size_in_window.height) {
2475 size_ = gfx::Size(new_size_in_window.width, new_size_in_window.height);
2476
2477 if (resizing_mode_selector_->is_synchronous_mode()) {
Blink Reformat1c4d759e2017-04-09 16:34:542478 gfx::Rect new_pos(WindowRect().x, WindowRect().y, size_.width(),
2479 size_.height());
Albert J. Wong3c93c182018-09-27 17:29:432480 widget_screen_rect_ = new_pos;
lfgb00fcad2016-07-14 14:16:332481 window_screen_rect_ = new_pos;
2482 }
2483
Christopher Camerone9a30c12018-03-07 08:23:382484 // TODO(ccameron): Note that this destroys any information differentiating
Christopher Cameronaad15a0d2018-03-09 03:47:332485 // |size_| from |compositor_viewport_pixel_size_|. Also note that the
2486 // calculation of |new_compositor_viewport_pixel_size| does not appear to
2487 // take into account device emulation.
Kinuko Yasudabef2b642019-01-24 04:53:332488 if (layer_tree_view_)
2489 layer_tree_view_->RequestNewLocalSurfaceId();
Christopher Cameronaad15a0d2018-03-09 03:47:332490 gfx::Size new_compositor_viewport_pixel_size =
Fady Samuela863f152018-03-09 16:10:032491 gfx::ScaleToCeiledSize(size_, GetWebScreenInfo().device_scale_factor);
Fady Samuel1c1ad3692018-11-06 23:28:402492 UpdateSurfaceAndScreenInfo(local_surface_id_allocation_from_parent_,
2493 new_compositor_viewport_pixel_size,
2494 screen_info_);
lfgb00fcad2016-07-14 14:16:332495 }
2496}
2497
[email protected]58b48a0d2012-06-13 07:01:352498void RenderWidget::GetCompositionCharacterBounds(
2499 std::vector<gfx::Rect>* bounds) {
2500 DCHECK(bounds);
2501 bounds->clear();
ekaramad2a46d632016-07-19 13:33:092502
brettw4b461082016-11-19 18:55:162503#if BUILDFLAG(ENABLE_PLUGINS)
EhsanK22d482e2017-08-10 17:29:492504 if (GetFocusedPepperPluginInsideWidget())
ekaramad2a46d632016-07-19 13:33:092505 return;
2506#endif
2507
Ehsan Karamad4975af6d2018-02-09 16:58:162508 blink::WebInputMethodController* controller = GetInputMethodController();
2509 if (!controller)
ekaramad2a46d632016-07-19 13:33:092510 return;
2511 blink::WebVector<blink::WebRect> bounds_from_blink;
Ehsan Karamad4975af6d2018-02-09 16:58:162512 if (!controller->GetCompositionCharacterBounds(bounds_from_blink))
ekaramad2a46d632016-07-19 13:33:092513 return;
2514
2515 for (size_t i = 0; i < bounds_from_blink.size(); ++i) {
Blink Reformat1c4d759e2017-04-09 16:34:542516 ConvertViewportToWindow(&bounds_from_blink[i]);
ekaramad2a46d632016-07-19 13:33:092517 bounds->push_back(bounds_from_blink[i]);
2518 }
[email protected]58b48a0d2012-06-13 07:01:352519}
2520
[email protected]db4fc1e2013-09-06 20:01:512521void RenderWidget::GetCompositionRange(gfx::Range* range) {
brettw4b461082016-11-19 18:55:162522#if BUILDFLAG(ENABLE_PLUGINS)
EhsanK22d482e2017-08-10 17:29:492523 if (GetFocusedPepperPluginInsideWidget())
ekaramad2a46d632016-07-19 13:33:092524 return;
2525#endif
EhsanKcdee7b72017-11-08 21:30:092526 blink::WebInputMethodController* controller = GetInputMethodController();
2527 WebRange web_range = controller ? controller->CompositionRange() : WebRange();
Blink Reformat1c4d759e2017-04-09 16:34:542528 if (web_range.IsNull()) {
nona2363a3d2016-11-09 03:26:212529 *range = gfx::Range::InvalidRange();
2530 return;
[email protected]88dbe32f2013-06-20 23:31:362531 }
Blink Reformat1c4d759e2017-04-09 16:34:542532 range->set_start(web_range.StartOffset());
2533 range->set_end(web_range.EndOffset());
[email protected]88dbe32f2013-06-20 23:31:362534}
2535
[email protected]501ea13d2013-07-09 17:03:292536bool RenderWidget::ShouldUpdateCompositionInfo(
[email protected]db4fc1e2013-09-06 20:01:512537 const gfx::Range& range,
[email protected]501ea13d2013-07-09 17:03:292538 const std::vector<gfx::Rect>& bounds) {
changwan2418e1b2016-12-12 12:43:082539 if (!range.IsValid())
2540 return false;
[email protected]501ea13d2013-07-09 17:03:292541 if (composition_range_ != range)
2542 return true;
2543 if (bounds.size() != composition_character_bounds_.size())
2544 return true;
2545 for (size_t i = 0; i < bounds.size(); ++i) {
2546 if (bounds[i] != composition_character_bounds_[i])
2547 return true;
2548 }
2549 return false;
2550}
[email protected]501ea13d2013-07-09 17:03:292551
[email protected]ad26ef42011-06-17 07:59:452552bool RenderWidget::CanComposeInline() {
brettw4b461082016-11-19 18:55:162553#if BUILDFLAG(ENABLE_PLUGINS)
EhsanK22d482e2017-08-10 17:29:492554 if (auto* plugin = GetFocusedPepperPluginInsideWidget())
2555 return plugin->IsPluginAcceptingCompositionEvents();
ekaramad2a46d632016-07-19 13:33:092556#endif
[email protected]ad26ef42011-06-17 07:59:452557 return true;
[email protected]56ea1a62011-05-30 07:05:572558}
2559
Blink Reformat1c4d759e2017-04-09 16:34:542560void RenderWidget::DidHandleGestureEvent(const WebGestureEvent& event,
2561 bool event_cancelled) {
danakja2c9d0a92018-07-25 20:01:182562 if (event_cancelled) {
Albert J. Wong7bbf22d2018-12-20 00:27:272563 // The delegate() doesn't need to hear about cancelled events.
[email protected]c68c3e4e2013-01-24 00:36:562564 return;
danakja2c9d0a92018-07-25 20:01:182565 }
2566
2567#if defined(OS_ANDROID) || defined(USE_AURA)
Blink Reformat1c4d759e2017-04-09 16:34:542568 if (event.GetType() == WebInputEvent::kGestureTap) {
changwan75e3b2072017-01-16 02:55:002569 ShowVirtualKeyboard();
Blink Reformat1c4d759e2017-04-09 16:34:542570 } else if (event.GetType() == WebInputEvent::kGestureLongPress) {
lfg8ff33912016-09-13 20:59:212571 DCHECK(GetWebWidget());
ekaramad5aff1942017-01-06 01:26:352572 blink::WebInputMethodController* controller = GetInputMethodController();
Blink Reformat1c4d759e2017-04-09 16:34:542573 if (!controller || controller->TextInputInfo().value.IsEmpty())
changwan75e3b2072017-01-16 02:55:002574 UpdateTextInputState();
[email protected]07c70d22014-08-21 08:33:462575 else
changwan75e3b2072017-01-16 02:55:002576 ShowVirtualKeyboard();
[email protected]c68c3e4e2013-01-24 00:36:562577 }
ekaramada110f642016-12-21 19:47:282578// TODO(ananta): Piggyback off existing IPCs to communicate this information,
2579// crbug/420130.
2580#if defined(OS_WIN)
danakja2c9d0a92018-07-25 20:01:182581 if (event.GetType() == blink::WebGestureEvent::kGestureTap) {
2582 // TODO(estade): hit test the event against focused node to make sure
2583 // the tap actually hit the focused node.
2584 blink::WebInputMethodController* controller = GetInputMethodController();
2585 blink::WebTextInputType text_input_type =
2586 controller ? controller->TextInputType() : blink::kWebTextInputTypeNone;
ekaramada110f642016-12-21 19:47:282587
Albert J. Wong3c93c182018-09-27 17:29:432588 Send(new WidgetHostMsg_FocusedNodeTouched(
danakja2c9d0a92018-07-25 20:01:182589 routing_id_, text_input_type != blink::kWebTextInputTypeNone));
2590 }
ekaramada110f642016-12-21 19:47:282591#endif
[email protected]c68c3e4e2013-01-24 00:36:562592#endif
danakja2c9d0a92018-07-25 20:01:182593
Albert J. Wong7bbf22d2018-12-20 00:27:272594 // The delegate() gets to respond to handling gestures last.
2595 if (delegate())
2596 delegate()->DidHandleGestureEventForWidget(event);
[email protected]c68c3e4e2013-01-24 00:36:562597}
2598
Blink Reformat1c4d759e2017-04-09 16:34:542599void RenderWidget::DidOverscroll(
bokane53a10f2016-04-13 23:48:312600 const blink::WebFloatSize& overscrollDelta,
2601 const blink::WebFloatSize& accumulatedOverscroll,
sataya.m582c9ce2015-06-09 08:03:422602 const blink::WebFloatPoint& position,
sunyunjiabbea8a92017-08-31 11:18:542603 const blink::WebFloatSize& velocity,
danakje819b622018-05-01 20:27:142604 const cc::OverscrollBehavior& behavior) {
bokan731ec382016-04-07 03:16:482605#if defined(OS_MACOSX)
2606 // On OSX the user can disable the elastic overscroll effect. If that's the
2607 // case, don't forward the overscroll notification.
2608 DCHECK(compositor_deps());
2609 if (!compositor_deps()->IsElasticOverscrollEnabled())
2610 return;
2611#endif
bokane53a10f2016-04-13 23:48:312612 input_handler_->DidOverscrollFromBlink(overscrollDelta, accumulatedOverscroll,
sunyunjiabbea8a92017-08-31 11:18:542613 position, velocity, behavior);
sataya.m582c9ce2015-06-09 08:03:422614}
2615
danakj4b347212018-07-04 17:55:172616// static
2617cc::LayerTreeSettings RenderWidget::GenerateLayerTreeSettings(
2618 CompositorDependencies* compositor_deps,
2619 bool is_for_subframe,
2620 const gfx::Size& initial_screen_size,
2621 float initial_device_scale_factor) {
2622 const bool is_threaded =
2623 !!compositor_deps->GetCompositorImplThreadTaskRunner();
2624
2625 const base::CommandLine& cmd = *base::CommandLine::ForCurrentProcess();
2626 cc::LayerTreeSettings settings;
2627
2628 settings.resource_settings.use_r16_texture =
2629 base::FeatureList::IsEnabled(media::kUseR16Texture);
2630
2631 settings.commit_to_active_tree = !is_threaded;
2632 settings.is_layer_tree_for_subframe = is_for_subframe;
2633
2634 // For web contents, layer transforms should scale up the contents of layers
2635 // to keep content always crisp when possible.
2636 settings.layer_transforms_should_scale_layer_contents = true;
2637
2638 settings.main_frame_before_activation_enabled =
2639 cmd.HasSwitch(cc::switches::kEnableMainFrameBeforeActivation);
2640
2641 // Checkerimaging is not supported for synchronous single-threaded mode, which
2642 // is what the renderer uses if its not threaded.
2643 settings.enable_checker_imaging =
2644 !cmd.HasSwitch(cc::switches::kDisableCheckerImaging) && is_threaded;
2645
2646#if defined(OS_ANDROID)
2647 // We can use a more aggressive limit on Android since decodes tend to take
2648 // longer on these devices.
2649 settings.min_image_bytes_to_checker = 512 * 1024; // 512kB
2650
2651 // Re-rasterization of checker-imaged content with software raster can be too
2652 // costly on Android.
2653 settings.only_checker_images_with_gpu_raster = true;
2654#endif
2655
2656 auto switch_value_as_int = [](const base::CommandLine& command_line,
2657 const std::string& switch_string, int min_value,
2658 int max_value, int* result) {
2659 std::string string_value = command_line.GetSwitchValueASCII(switch_string);
2660 int int_value;
2661 if (base::StringToInt(string_value, &int_value) && int_value >= min_value &&
2662 int_value <= max_value) {
2663 *result = int_value;
2664 return true;
2665 } else {
2666 LOG(WARNING) << "Failed to parse switch " << switch_string << ": "
2667 << string_value;
2668 return false;
2669 }
2670 };
2671
2672 int default_tile_size = 256;
2673#if defined(OS_ANDROID)
2674 const gfx::Size screen_size =
2675 gfx::ScaleToFlooredSize(initial_screen_size, initial_device_scale_factor);
2676 int display_width = screen_size.width();
2677 int display_height = screen_size.height();
2678 int numTiles = (display_width * display_height) / (256 * 256);
2679 if (numTiles > 16)
2680 default_tile_size = 384;
2681 if (numTiles >= 40)
2682 default_tile_size = 512;
2683
2684 // Adjust for some resolutions that barely straddle an extra
2685 // tile when in portrait mode. This helps worst case scroll/raster
2686 // by not needing a full extra tile for each row.
2687 constexpr int tolerance = 10; // To avoid rounding errors.
2688 int portrait_width = std::min(display_width, display_height);
2689 if (default_tile_size == 256 && std::abs(portrait_width - 768) < tolerance)
2690 default_tile_size += 32;
2691 if (default_tile_size == 384 && std::abs(portrait_width - 1200) < tolerance)
2692 default_tile_size += 32;
2693#elif defined(OS_CHROMEOS) || defined(OS_MACOSX)
2694 // Use 512 for high DPI (dsf=2.0f) devices.
2695 if (initial_device_scale_factor >= 2.0f)
2696 default_tile_size = 512;
2697#endif
2698
2699 // TODO(danakj): This should not be a setting O_O; it should change when the
2700 // device scale factor on LayerTreeHost changes.
2701 settings.default_tile_size = gfx::Size(default_tile_size, default_tile_size);
2702 if (cmd.HasSwitch(switches::kDefaultTileWidth)) {
2703 int tile_width = 0;
2704 switch_value_as_int(cmd, switches::kDefaultTileWidth, 1,
2705 std::numeric_limits<int>::max(), &tile_width);
2706 settings.default_tile_size.set_width(tile_width);
2707 }
2708 if (cmd.HasSwitch(switches::kDefaultTileHeight)) {
2709 int tile_height = 0;
2710 switch_value_as_int(cmd, switches::kDefaultTileHeight, 1,
2711 std::numeric_limits<int>::max(), &tile_height);
2712 settings.default_tile_size.set_height(tile_height);
2713 }
2714
2715 int max_untiled_layer_width = settings.max_untiled_layer_size.width();
2716 if (cmd.HasSwitch(switches::kMaxUntiledLayerWidth)) {
2717 switch_value_as_int(cmd, switches::kMaxUntiledLayerWidth, 1,
2718 std::numeric_limits<int>::max(),
2719 &max_untiled_layer_width);
2720 }
2721 int max_untiled_layer_height = settings.max_untiled_layer_size.height();
2722 if (cmd.HasSwitch(switches::kMaxUntiledLayerHeight)) {
2723 switch_value_as_int(cmd, switches::kMaxUntiledLayerHeight, 1,
2724 std::numeric_limits<int>::max(),
2725 &max_untiled_layer_height);
2726 }
2727
2728 settings.max_untiled_layer_size =
2729 gfx::Size(max_untiled_layer_width, max_untiled_layer_height);
2730
2731 settings.gpu_rasterization_msaa_sample_count =
2732 compositor_deps->GetGpuRasterizationMSAASampleCount();
2733 settings.gpu_rasterization_forced =
2734 compositor_deps->IsGpuRasterizationForced();
2735
2736 settings.can_use_lcd_text = compositor_deps->IsLcdTextEnabled();
2737 settings.use_zero_copy = compositor_deps->IsZeroCopyEnabled();
2738 settings.use_partial_raster = compositor_deps->IsPartialRasterEnabled();
2739 settings.enable_elastic_overscroll =
2740 compositor_deps->IsElasticOverscrollEnabled();
2741 settings.resource_settings.use_gpu_memory_buffer_resources =
2742 compositor_deps->IsGpuMemoryBufferCompositorResourcesEnabled();
2743 settings.use_painted_device_scale_factor =
2744 compositor_deps->IsUseZoomForDSFEnabled();
2745
2746 // Build LayerTreeSettings from command line args.
2747 if (cmd.HasSwitch(cc::switches::kBrowserControlsShowThreshold)) {
2748 std::string top_threshold_str =
2749 cmd.GetSwitchValueASCII(cc::switches::kBrowserControlsShowThreshold);
2750 double show_threshold;
2751 if (base::StringToDouble(top_threshold_str, &show_threshold) &&
2752 show_threshold >= 0.f && show_threshold <= 1.f)
2753 settings.top_controls_show_threshold = show_threshold;
2754 }
2755
2756 if (cmd.HasSwitch(cc::switches::kBrowserControlsHideThreshold)) {
2757 std::string top_threshold_str =
2758 cmd.GetSwitchValueASCII(cc::switches::kBrowserControlsHideThreshold);
2759 double hide_threshold;
2760 if (base::StringToDouble(top_threshold_str, &hide_threshold) &&
2761 hide_threshold >= 0.f && hide_threshold <= 1.f)
2762 settings.top_controls_hide_threshold = hide_threshold;
2763 }
2764
Philip Rogersc55ab6a2018-12-05 17:29:192765 // Blink sends cc a layer list and property trees when either
2766 // BlinkGenPropertyTrees or CompositeAfterPaint are enabled.
Philip Rogersfc2f1162018-10-17 18:28:312767 settings.use_layer_lists =
2768 blink::WebRuntimeFeatures::IsBlinkGenPropertyTreesEnabled() ||
Philip Rogersc55ab6a2018-12-05 17:29:192769 blink::WebRuntimeFeatures::IsCompositeAfterPaintEnabled();
danakj4b347212018-07-04 17:55:172770
David Bokan163b6a72018-10-25 22:03:492771 // Blink currently doesn't support setting fractional scroll offsets so CC
2772 // must send integer values. We plan to eventually make Blink use fractional
2773 // offsets internally: https://crbug.com/414283.
2774 settings.commit_fractional_scroll_deltas =
2775 blink::WebRuntimeFeatures::IsFractionalScrollOffsetsEnabled();
2776
danakj4b347212018-07-04 17:55:172777 // The means the renderer compositor has 2 possible modes:
2778 // - Threaded compositing with a scheduler.
Kent Tamura21d1de62018-12-10 04:45:202779 // - Single threaded compositing without a scheduler (for web tests only).
2780 // Using the scheduler in web tests introduces additional composite steps
danakj4b347212018-07-04 17:55:172781 // that create flakiness.
2782 settings.single_thread_proxy_scheduler = false;
2783
2784 // These flags should be mirrored by UI versions in ui/compositor/.
2785 if (cmd.HasSwitch(cc::switches::kShowCompositedLayerBorders))
2786 settings.initial_debug_state.show_debug_borders.set();
2787 settings.initial_debug_state.show_layer_animation_bounds_rects =
2788 cmd.HasSwitch(cc::switches::kShowLayerAnimationBounds);
2789 settings.initial_debug_state.show_paint_rects =
2790 cmd.HasSwitch(switches::kShowPaintRects);
2791 settings.initial_debug_state.show_property_changed_rects =
2792 cmd.HasSwitch(cc::switches::kShowPropertyChangedRects);
2793 settings.initial_debug_state.show_surface_damage_rects =
2794 cmd.HasSwitch(cc::switches::kShowSurfaceDamageRects);
2795 settings.initial_debug_state.show_screen_space_rects =
2796 cmd.HasSwitch(cc::switches::kShowScreenSpaceRects);
2797
2798 settings.initial_debug_state.SetRecordRenderingStats(
2799 cmd.HasSwitch(cc::switches::kEnableGpuBenchmarking));
2800 settings.enable_surface_synchronization =
2801 features::IsSurfaceSynchronizationEnabled();
2802 settings.build_hit_test_data = features::IsVizHitTestingSurfaceLayerEnabled();
2803
2804 if (cmd.HasSwitch(cc::switches::kSlowDownRasterScaleFactor)) {
2805 const int kMinSlowDownScaleFactor = 0;
2806 const int kMaxSlowDownScaleFactor = INT_MAX;
2807 switch_value_as_int(
2808 cmd, cc::switches::kSlowDownRasterScaleFactor, kMinSlowDownScaleFactor,
2809 kMaxSlowDownScaleFactor,
2810 &settings.initial_debug_state.slow_down_raster_scale_factor);
2811 }
2812
2813 // This is default overlay scrollbar settings for Android and DevTools mobile
2814 // emulator. Aura Overlay Scrollbar will override below.
2815 settings.scrollbar_animator = cc::LayerTreeSettings::ANDROID_OVERLAY;
2816 settings.solid_color_scrollbar_color = SkColorSetARGB(128, 128, 128, 128);
2817 settings.scrollbar_fade_delay = base::TimeDelta::FromMilliseconds(300);
2818 settings.scrollbar_fade_duration = base::TimeDelta::FromMilliseconds(300);
2819
2820#if defined(OS_ANDROID)
2821 bool using_synchronous_compositor =
2822 compositor_deps->UsingSynchronousCompositing();
2823 bool using_low_memory_policy = base::SysInfo::IsLowEndDevice();
2824
2825 settings.use_stream_video_draw_quad = true;
2826 settings.using_synchronous_renderer_compositor = using_synchronous_compositor;
2827 if (using_synchronous_compositor) {
2828 // Android WebView uses system scrollbars, so make ours invisible.
2829 // http://crbug.com/677348: This can't be done using hide_scrollbars
2830 // setting because supporting -webkit custom scrollbars is still desired
2831 // on sublayers.
2832 settings.scrollbar_animator = cc::LayerTreeSettings::NO_ANIMATOR;
2833 settings.solid_color_scrollbar_color = SK_ColorTRANSPARENT;
2834
2835 settings.enable_early_damage_check =
2836 cmd.HasSwitch(cc::switches::kCheckDamageEarly);
2837 }
2838 // Android WebView handles root layer flings itself.
2839 settings.ignore_root_layer_flings = using_synchronous_compositor;
2840 // Memory policy on Android WebView does not depend on whether device is
2841 // low end, so always use default policy.
2842 if (using_low_memory_policy && !using_synchronous_compositor) {
2843 // On low-end we want to be very carefull about killing other
2844 // apps. So initially we use 50% more memory to avoid flickering
2845 // or raster-on-demand.
2846 settings.max_memory_for_prepaint_percentage = 67;
2847 } else {
2848 // On other devices we have increased memory excessively to avoid
2849 // raster-on-demand already, so now we reserve 50% _only_ to avoid
2850 // raster-on-demand, and use 50% of the memory otherwise.
2851 settings.max_memory_for_prepaint_percentage = 50;
2852 }
2853
2854 // TODO(danakj): Only do this on low end devices.
2855 settings.create_low_res_tiling = true;
2856
2857#else // defined(OS_ANDROID)
2858 bool using_synchronous_compositor = false; // Only for Android WebView.
2859 // On desktop, we never use the low memory policy unless we are simulating
2860 // low-end mode via a switch.
2861 bool using_low_memory_policy =
2862 cmd.HasSwitch(switches::kEnableLowEndDeviceMode);
2863
2864 if (ui::IsOverlayScrollbarEnabled()) {
2865 settings.scrollbar_animator = cc::LayerTreeSettings::AURA_OVERLAY;
2866 settings.scrollbar_fade_delay = ui::kOverlayScrollbarFadeDelay;
2867 settings.scrollbar_fade_duration = ui::kOverlayScrollbarFadeDuration;
2868 settings.scrollbar_thinning_duration =
2869 ui::kOverlayScrollbarThinningDuration;
2870 settings.scrollbar_flash_after_any_scroll_update =
2871 ui::OverlayScrollbarFlashAfterAnyScrollUpdate();
2872 settings.scrollbar_flash_when_mouse_enter =
2873 ui::OverlayScrollbarFlashWhenMouseEnter();
2874 }
2875
2876 // On desktop, if there's over 4GB of memory on the machine, increase the
2877 // working set size to 256MB for both gpu and software.
2878 const int kImageDecodeMemoryThresholdMB = 4 * 1024;
2879 if (base::SysInfo::AmountOfPhysicalMemoryMB() >=
2880 kImageDecodeMemoryThresholdMB) {
2881 settings.decoded_image_working_set_budget_bytes = 256 * 1024 * 1024;
2882 } else {
2883 // This is the default, but recorded here as well.
2884 settings.decoded_image_working_set_budget_bytes = 128 * 1024 * 1024;
2885 }
2886#endif // defined(OS_ANDROID)
2887
2888 if (using_low_memory_policy) {
2889 // RGBA_4444 textures are only enabled:
2890 // - If the user hasn't explicitly disabled them
2891 // - If system ram is <= 512MB (1GB devices are sometimes low-end).
2892 // - If we are not running in a WebView, where 4444 isn't supported.
2893 if (!cmd.HasSwitch(switches::kDisableRGBA4444Textures) &&
2894 base::SysInfo::AmountOfPhysicalMemoryMB() <= 512 &&
2895 !using_synchronous_compositor) {
2896 settings.use_rgba_4444 = viz::RGBA_4444;
2897
2898 // If we are going to unpremultiply and dither these tiles, we need to
2899 // allocate an additional RGBA_8888 intermediate for each tile
2900 // rasterization when rastering to RGBA_4444 to allow for dithering.
2901 // Setting a reasonable sized max tile size allows this intermediate to
2902 // be consistently reused.
2903 if (base::FeatureList::IsEnabled(
2904 kUnpremultiplyAndDitherLowBitDepthTiles)) {
2905 settings.max_gpu_raster_tile_size = gfx::Size(512, 256);
2906 settings.unpremultiply_and_dither_low_bit_depth_tiles = true;
2907 }
2908 }
2909 }
2910
2911 if (cmd.HasSwitch(switches::kEnableLowResTiling))
2912 settings.create_low_res_tiling = true;
2913 if (cmd.HasSwitch(switches::kDisableLowResTiling))
2914 settings.create_low_res_tiling = false;
2915
2916 if (cmd.HasSwitch(switches::kEnableRGBA4444Textures) &&
2917 !cmd.HasSwitch(switches::kDisableRGBA4444Textures)) {
2918 settings.use_rgba_4444 = true;
2919 }
2920
2921 settings.max_staging_buffer_usage_in_bytes = 32 * 1024 * 1024; // 32MB
2922 // Use 1/4th of staging buffers on low-end devices.
2923 if (base::SysInfo::IsLowEndDevice())
2924 settings.max_staging_buffer_usage_in_bytes /= 4;
2925
2926 cc::ManagedMemoryPolicy defaults = settings.memory_policy;
2927 settings.memory_policy = GetGpuMemoryPolicy(defaults, initial_screen_size,
2928 initial_device_scale_factor);
2929
2930 settings.disallow_non_exact_resource_reuse =
2931 cmd.HasSwitch(switches::kDisallowNonExactResourceReuse);
2932#if defined(OS_ANDROID)
2933 // TODO(crbug.com/746931): This feature appears to be causing visual
2934 // corruption on certain android devices. Will investigate and re-enable.
2935 settings.disallow_non_exact_resource_reuse = true;
2936#endif
2937
2938 if (cmd.HasSwitch(switches::kRunAllCompositorStagesBeforeDraw)) {
2939 settings.wait_for_all_pipeline_stages_before_draw = true;
2940 settings.enable_latency_recovery = false;
2941 }
2942
2943 settings.enable_image_animation_resync =
2944 !cmd.HasSwitch(switches::kDisableImageAnimationResync);
2945
Karolina Soltys4d60b402018-10-01 19:28:202946 settings.send_compositor_frame_ack = false;
2947
danakj4b347212018-07-04 17:55:172948 return settings;
2949}
2950
2951// static
2952cc::ManagedMemoryPolicy RenderWidget::GetGpuMemoryPolicy(
2953 const cc::ManagedMemoryPolicy& default_policy,
2954 const gfx::Size& initial_screen_size,
2955 float initial_device_scale_factor) {
2956 cc::ManagedMemoryPolicy actual = default_policy;
2957 actual.bytes_limit_when_visible = 0;
2958
2959 // If the value was overridden on the command line, use the specified value.
2960 static bool client_hard_limit_bytes_overridden =
2961 base::CommandLine::ForCurrentProcess()->HasSwitch(
2962 switches::kForceGpuMemAvailableMb);
2963 if (client_hard_limit_bytes_overridden) {
2964 if (base::StringToSizeT(
2965 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
2966 switches::kForceGpuMemAvailableMb),
2967 &actual.bytes_limit_when_visible))
2968 actual.bytes_limit_when_visible *= 1024 * 1024;
2969 return actual;
2970 }
2971
2972#if defined(OS_ANDROID)
2973 // We can't query available GPU memory from the system on Android.
2974 // Physical memory is also mis-reported sometimes (eg. Nexus 10 reports
2975 // 1262MB when it actually has 2GB, while Razr M has 1GB but only reports
2976 // 128MB java heap size). First we estimate physical memory using both.
2977 size_t dalvik_mb = base::SysInfo::DalvikHeapSizeMB();
2978 size_t physical_mb = base::SysInfo::AmountOfPhysicalMemoryMB();
2979 size_t physical_memory_mb = 0;
2980 if (base::SysInfo::IsLowEndDevice()) {
2981 // TODO(crbug.com/742534): The code below appears to no longer work.
2982 // |dalvik_mb| no longer follows the expected heuristic pattern, causing us
2983 // to over-estimate memory on low-end devices. This entire section probably
2984 // needs to be re-written, but for now we can address the low-end Android
2985 // issues by ignoring |dalvik_mb|.
2986 physical_memory_mb = physical_mb;
2987 } else if (dalvik_mb >= 256) {
2988 physical_memory_mb = dalvik_mb * 4;
2989 } else {
2990 physical_memory_mb = std::max(dalvik_mb * 4, (physical_mb * 4) / 3);
2991 }
2992
2993 // Now we take a default of 1/8th of memory on high-memory devices,
2994 // and gradually scale that back for low-memory devices (to be nicer
2995 // to other apps so they don't get killed). Examples:
2996 // Nexus 4/10(2GB) 256MB (normally 128MB)
2997 // Droid Razr M(1GB) 114MB (normally 57MB)
2998 // Galaxy Nexus(1GB) 100MB (normally 50MB)
2999 // Xoom(1GB) 100MB (normally 50MB)
3000 // Nexus S(low-end) 8MB (normally 8MB)
3001 // Note that the compositor now uses only some of this memory for
3002 // pre-painting and uses the rest only for 'emergencies'.
3003 if (actual.bytes_limit_when_visible == 0) {
3004 // NOTE: Non-low-end devices use only 50% of these limits,
3005 // except during 'emergencies' where 100% can be used.
3006 if (physical_memory_mb >= 1536)
3007 actual.bytes_limit_when_visible = physical_memory_mb / 8; // >192MB
3008 else if (physical_memory_mb >= 1152)
3009 actual.bytes_limit_when_visible = physical_memory_mb / 8; // >144MB
3010 else if (physical_memory_mb >= 768)
3011 actual.bytes_limit_when_visible = physical_memory_mb / 10; // >76MB
3012 else if (physical_memory_mb >= 513)
3013 actual.bytes_limit_when_visible = physical_memory_mb / 12; // <64MB
3014 else
3015 // Devices with this little RAM have very little headroom so we hardcode
3016 // the limit rather than relying on the heuristics above. (They also use
3017 // 4444 textures so we can use a lower limit.)
3018 actual.bytes_limit_when_visible = 8;
3019
3020 actual.bytes_limit_when_visible =
3021 actual.bytes_limit_when_visible * 1024 * 1024;
3022 // Clamp the observed value to a specific range on Android.
3023 actual.bytes_limit_when_visible = std::max(
3024 actual.bytes_limit_when_visible, static_cast<size_t>(8 * 1024 * 1024));
3025 actual.bytes_limit_when_visible =
3026 std::min(actual.bytes_limit_when_visible,
3027 static_cast<size_t>(256 * 1024 * 1024));
3028 }
3029 actual.priority_cutoff_when_visible =
3030 gpu::MemoryAllocation::CUTOFF_ALLOW_EVERYTHING;
3031#else
3032 // Ignore what the system said and give all clients the same maximum
3033 // allocation on desktop platforms.
3034 actual.bytes_limit_when_visible = 512 * 1024 * 1024;
3035 actual.priority_cutoff_when_visible =
3036 gpu::MemoryAllocation::CUTOFF_ALLOW_NICE_TO_HAVE;
3037
3038 // For large monitors (4k), double the tile memory to avoid frequent out of
3039 // memory problems. 4k could mean a screen width of anywhere from 3840 to 4096
3040 // (see https://en.wikipedia.org/wiki/4K_resolution). We use 3500 as a proxy
3041 // for "large enough".
3042 static const int kLargeDisplayThreshold = 3500;
3043 int display_width =
3044 std::round(initial_screen_size.width() * initial_device_scale_factor);
3045 if (display_width >= kLargeDisplayThreshold)
3046 actual.bytes_limit_when_visible *= 2;
3047#endif
3048 return actual;
3049}
3050
Navid Zolghadr8fdb6114e2018-08-29 19:00:543051void RenderWidget::HasPointerRawMoveEventHandlers(bool has_handlers) {
3052 if (input_event_queue_)
3053 input_event_queue_->HasPointerRawMoveEventHandlers(has_handlers);
3054}
3055
Blink Reformat1c4d759e2017-04-09 16:34:543056void RenderWidget::HasTouchEventHandlers(bool has_handlers) {
W. James MacLean662d5382018-03-29 18:33:083057 if (has_touch_handlers_ && *has_touch_handlers_ == has_handlers)
3058 return;
3059
3060 has_touch_handlers_ = has_handlers;
alexclarke7fa93942015-10-21 15:37:113061 if (render_widget_scheduling_state_)
3062 render_widget_scheduling_state_->SetHasTouchHandler(has_handlers);
Albert J. Wongeb28e7e2018-10-12 00:57:433063 Send(new WidgetHostMsg_HasTouchEventHandlers(routing_id_, has_handlers));
[email protected]ce6689f2013-03-29 12:52:553064}
3065
Dave Tapuskadfe486c12017-06-09 16:53:133066void RenderWidget::SetNeedsLowLatencyInput(bool needs_low_latency) {
3067 if (input_event_queue_)
3068 input_event_queue_->SetNeedsLowLatency(needs_low_latency);
3069}
3070
Pavel Feldman6708eefa2019-01-17 03:14:323071void RenderWidget::SetNeedsUnbufferedInputForDebugger(bool unbuffered) {
3072 if (input_event_queue_)
3073 input_event_queue_->SetNeedsUnbufferedInputForDebugger(unbuffered);
3074}
3075
W. James MacLeand973a55b2018-11-29 21:39:133076void RenderWidget::AnimateDoubleTapZoomInMainFrame(
3077 const blink::WebPoint& point,
3078 const blink::WebRect& rect_to_zoom) {
3079 // Only oopif subframes should be sending this message.
Albert J. Wong7bbf22d2018-12-20 00:27:273080 DCHECK(!delegate());
W. James MacLeand973a55b2018-11-29 21:39:133081 Send(new WidgetHostMsg_AnimateDoubleTapZoomInMainFrame(routing_id(), point,
3082 rect_to_zoom));
3083}
3084
W. James MacLean5372eb72018-12-19 12:56:363085void RenderWidget::ZoomToFindInPageRectInMainFrame(
3086 const blink::WebRect& rect_to_zoom) {
3087 // Only oopif subframes should be sending this message.
Albert J. Wong7bbf22d2018-12-20 00:27:273088 DCHECK(!delegate_);
W. James MacLean5372eb72018-12-19 12:56:363089 Send(new WidgetHostMsg_ZoomToFindInPageRectInMainFrame(routing_id(),
3090 rect_to_zoom));
3091}
3092
Dave Tapuskabafc2ba32017-11-28 01:54:373093void RenderWidget::RequestUnbufferedInputEvents() {
3094 if (input_event_queue_)
3095 input_event_queue_->RequestUnbufferedInputEvents();
3096}
3097
xidachenfa0199e72017-05-11 11:34:263098void RenderWidget::SetTouchAction(cc::TouchAction touch_action) {
Dave Tapuskab28a1082017-08-30 15:37:263099 if (!input_handler_->ProcessTouchAction(touch_action))
[email protected]5d0bbdfa92013-12-10 00:35:513100 return;
3101
Dave Tapuska04bc5ee92018-04-17 19:03:313102 widget_input_handler_manager_->ProcessTouchAction(touch_action);
[email protected]5d0bbdfa92013-12-10 00:35:513103}
3104
[email protected]e3244ed2014-06-20 20:04:273105void RenderWidget::RegisterRenderFrameProxy(RenderFrameProxy* proxy) {
3106 render_frame_proxies_.AddObserver(proxy);
danakj0246bba02019-01-23 23:12:273107 // Page scale factor is propagated down the RenderWidget tree (across
3108 // frame trees). A new RenderFrameProxy means there is a new child
3109 // RenderWidget in another frame tree. In order for it to hear about
3110 // the page scale factor we pass along the last seen value here.
W. James MacLean2a90bff2018-11-05 20:52:473111 proxy->OnPageScaleFactorChanged(page_scale_factor_from_mainframe_);
[email protected]bffc8302014-01-23 20:52:163112}
3113
[email protected]e3244ed2014-06-20 20:04:273114void RenderWidget::UnregisterRenderFrameProxy(RenderFrameProxy* proxy) {
3115 render_frame_proxies_.RemoveObserver(proxy);
[email protected]bffc8302014-01-23 20:52:163116}
3117
[email protected]de3c5d82014-05-28 22:12:593118void RenderWidget::RegisterRenderFrame(RenderFrameImpl* frame) {
3119 render_frames_.AddObserver(frame);
3120}
3121
3122void RenderWidget::UnregisterRenderFrame(RenderFrameImpl* frame) {
3123 render_frames_.RemoveObserver(frame);
3124}
3125
Saman Samid189e5dfd2017-12-20 22:55:313126void RenderWidget::RegisterBrowserPlugin(BrowserPlugin* browser_plugin) {
3127 browser_plugins_.AddObserver(browser_plugin);
Saman Sami50d1e0c2018-03-13 20:03:493128 browser_plugin->ScreenInfoChanged(GetOriginalScreenInfo());
Saman Samid189e5dfd2017-12-20 22:55:313129}
3130
3131void RenderWidget::UnregisterBrowserPlugin(BrowserPlugin* browser_plugin) {
3132 browser_plugins_.RemoveObserver(browser_plugin);
3133}
3134
Albert J. Wong3c93c182018-09-27 17:29:433135void RenderWidget::OnWaitNextFrameForTests(
3136 int main_frame_thread_observer_routing_id) {
danakj53802692018-07-25 21:46:443137 // Sends an ACK to the browser process during the next compositor frame.
Albert J. Wong3c93c182018-09-27 17:29:433138 QueueMessage(new WidgetHostMsg_WaitForNextFrameForTests_ACK(
3139 main_frame_thread_observer_routing_id));
lfg43e08e62016-02-03 18:51:373140}
3141
Fady Samuela863f152018-03-09 16:10:033142const ScreenInfo& RenderWidget::GetWebScreenInfo() const {
3143 return screen_info_;
Christopher Camerone9a30c12018-03-07 08:23:383144}
3145
Fady Samuela863f152018-03-09 16:10:033146const ScreenInfo& RenderWidget::GetOriginalScreenInfo() const {
Christopher Camerone9a30c12018-03-07 08:23:383147 return screen_metrics_emulator_
3148 ? screen_metrics_emulator_->original_screen_info()
Fady Samuela863f152018-03-09 16:10:033149 : screen_info_;
oshima50872a72016-03-04 13:26:183150}
3151
Ella Ge80a52dce2017-11-15 18:01:523152gfx::PointF RenderWidget::ConvertWindowPointToViewport(
3153 const gfx::PointF& point) {
paulmeyer90f6c31d2016-11-12 00:17:593154 blink::WebFloatRect point_in_viewport(point.x(), point.y(), 0, 0);
Blink Reformat1c4d759e2017-04-09 16:34:543155 ConvertWindowToViewport(&point_in_viewport);
Ella Ge80a52dce2017-11-15 18:01:523156 return gfx::PointF(point_in_viewport.x, point_in_viewport.y);
3157}
3158
3159gfx::Point RenderWidget::ConvertWindowPointToViewport(const gfx::Point& point) {
3160 return gfx::ToRoundedPoint(ConvertWindowPointToViewport(gfx::PointF(point)));
paulmeyer90f6c31d2016-11-12 00:17:593161}
3162
Blink Reformat1c4d759e2017-04-09 16:34:543163bool RenderWidget::RequestPointerLock() {
lfgbee1e0a2016-06-08 21:24:213164 return mouse_lock_dispatcher_->LockMouse(webwidget_mouse_lock_target_.get());
3165}
3166
Blink Reformat1c4d759e2017-04-09 16:34:543167void RenderWidget::RequestPointerUnlock() {
lfgbee1e0a2016-06-08 21:24:213168 mouse_lock_dispatcher_->UnlockMouse(webwidget_mouse_lock_target_.get());
3169}
3170
Blink Reformat1c4d759e2017-04-09 16:34:543171bool RenderWidget::IsPointerLocked() {
lfgbee1e0a2016-06-08 21:24:213172 return mouse_lock_dispatcher_->IsMouseLockedTo(
3173 webwidget_mouse_lock_target_.get());
3174}
3175
Richard Li49fe04d2018-10-21 09:07:193176void RenderWidget::StartDragging(network::mojom::ReferrerPolicy policy,
paulmeyer6ef5a792016-11-08 20:33:583177 const WebDragData& data,
3178 WebDragOperationsMask mask,
danakj0c75ad82018-07-10 19:50:123179 const SkBitmap& drag_image,
danakjef1735a2018-11-13 19:34:443180 const gfx::Point& web_image_offset) {
3181 blink::WebRect offset_in_window(web_image_offset.x(), web_image_offset.y(), 0,
3182 0);
Blink Reformat1c4d759e2017-04-09 16:34:543183 ConvertViewportToWindow(&offset_in_window);
paulmeyer6ef5a792016-11-08 20:33:583184 DropData drop_data(DropDataBuilder::Build(data));
3185 drop_data.referrer_policy = policy;
danakj0c75ad82018-07-10 19:50:123186 gfx::Vector2d image_offset(offset_in_window.x, offset_in_window.y);
3187 Send(new DragHostMsg_StartDragging(routing_id(), drop_data, mask, drag_image,
3188 image_offset, possible_drag_event_info_));
paulmeyer6ef5a792016-11-08 20:33:583189}
3190
kenrb5d78b842017-03-06 21:06:013191uint32_t RenderWidget::GetContentSourceId() {
3192 return current_content_source_id_;
3193}
3194
Saman Samif7731342018-01-24 22:18:443195void RenderWidget::DidNavigate() {
Saman Sami26a1fcd2018-04-10 17:12:213196 ++current_content_source_id_;
Kinuko Yasudabef2b642019-01-24 04:53:333197 if (!layer_tree_view_)
3198 return;
danakja6c10012018-07-06 14:25:363199 layer_tree_view_->SetContentSourceId(current_content_source_id_);
3200 layer_tree_view_->ClearCachesOnNextCommit();
kenrb5d78b842017-03-06 21:06:013201}
3202
lfg8ff33912016-09-13 20:59:213203blink::WebWidget* RenderWidget::GetWebWidget() const {
Albert J. Wong7bbf22d2018-12-20 00:27:273204 if (delegate()) {
3205 blink::WebWidget* delegate_widget = delegate()->GetWebWidgetForWidget();
danakja2c9d0a92018-07-25 20:01:183206 if (delegate_widget)
3207 return delegate_widget;
3208 }
lfg8ff33912016-09-13 20:59:213209 return webwidget_internal_;
3210}
3211
ekaramad2daaf672016-11-10 20:29:013212blink::WebInputMethodController* RenderWidget::GetInputMethodController()
3213 const {
Ehsan Karamad655d7b8a2018-01-12 18:38:383214 if (auto* frame_widget = GetFrameWidget())
3215 return frame_widget->GetActiveWebInputMethodController();
3216
3217 return nullptr;
ekaramad2daaf672016-11-10 20:29:013218}
3219
Dave Tapuska139a72f2017-09-06 21:57:033220void RenderWidget::SetupWidgetInputHandler(
3221 mojom::WidgetInputHandlerRequest request,
3222 mojom::WidgetInputHandlerHostPtr host) {
Dave Tapuskab66c28f2017-11-15 17:18:473223 widget_input_handler_manager_->AddInterface(std::move(request),
3224 std::move(host));
Dave Tapuska525eb15e2017-08-17 21:05:503225}
3226
Dave Tapuska485aca92017-08-08 00:47:583227void RenderWidget::SetWidgetBinding(mojom::WidgetRequest request) {
3228 // Close the old binding if there was one.
3229 // A RenderWidgetHost should not need more than one channel.
3230 widget_binding_.Close();
3231 widget_binding_.Bind(std::move(request));
3232}
3233
Ken Buchanan94c0beb62018-06-22 19:56:243234void RenderWidget::SetMouseCapture(bool capture) {
3235 if (mojom::WidgetInputHandlerHost* host =
3236 widget_input_handler_manager_->GetWidgetInputHandlerHost()) {
3237 host->SetMouseCapture(capture);
3238 }
3239}
3240
danakj53802692018-07-25 21:46:443241void RenderWidget::SetWindowScreenRect(const gfx::Rect& window_screen_rect) {
3242 if (screen_metrics_emulator_)
3243 screen_metrics_emulator_->OnUpdateWindowScreenRect(window_screen_rect);
3244 else
3245 window_screen_rect_ = window_screen_rect;
3246}
3247
Fady Samuelca9ecb72018-05-05 05:59:273248bool RenderWidget::IsSurfaceSynchronizationEnabled() const {
danakja6c10012018-07-06 14:25:363249 return layer_tree_view_ &&
3250 layer_tree_view_->IsSurfaceSynchronizationEnabled();
Fady Samuelca9ecb72018-05-05 05:59:273251}
3252
W. James MacLean2a90bff2018-11-05 20:52:473253void RenderWidget::PageScaleFactorChanged(float page_scale_factor) {
danakj0246bba02019-01-23 23:12:273254 // The page scale is controlled by the WebView for the local main frame of
3255 // the Page. So this is called from blink by for the RenderWidget of that
3256 // local main frame. We forward the value on to each child RenderWidget (each
3257 // of which will be via proxy child frame). These will each in turn forward
3258 // the message to their child RenderWidgets (through their proxy child
3259 // frames).
3260 // TODO(crbug.com/924336): This value is continuously propagated during a
3261 // pinch-zoom, causing the child RenderWidgets to re-raster, while the main
3262 // frame is able to throttle re-raster to powers of 2. We could find some way
3263 // to throttle child RenderWidgets also, perhaps by informing them when the
3264 // pinch-zoom gesture is started and stopped.
3265 DCHECK(!is_frozen_);
3266 DCHECK(delegate());
W. James MacLean2a90bff2018-11-05 20:52:473267
W. James MacLean2a90bff2018-11-05 20:52:473268 for (auto& observer : render_frame_proxies_)
3269 observer.OnPageScaleFactorChanged(page_scale_factor);
danakj0246bba02019-01-23 23:12:273270 // Store the value to give to any new RenderFrameProxy that is registered.
3271 page_scale_factor_from_mainframe_ = page_scale_factor;
W. James MacLean2a90bff2018-11-05 20:52:473272}
3273
danakj53802692018-07-25 21:46:443274void RenderWidget::UseSynchronousResizeModeForTesting(bool enable) {
3275 resizing_mode_selector_->set_is_synchronous_mode(enable);
danakja2c9d0a92018-07-25 20:01:183276}
3277
3278void RenderWidget::SetDeviceScaleFactorForTesting(float factor) {
3279 device_scale_factor_for_testing_ = factor;
3280
3281 VisualProperties visual_properties;
3282 visual_properties.screen_info = screen_info_;
3283 visual_properties.screen_info.device_scale_factor = factor;
3284 visual_properties.new_size = size();
3285 visual_properties.visible_viewport_size = visible_viewport_size_;
3286 visual_properties.compositor_viewport_pixel_size =
3287 gfx::ScaleToCeiledSize(size(), factor);
3288 visual_properties.browser_controls_shrink_blink_size = false;
3289 visual_properties.top_controls_height = 0.f;
3290 visual_properties.is_fullscreen_granted = is_fullscreen_granted_;
3291 visual_properties.display_mode = display_mode_;
Fady Samuel84d8a1f72018-11-08 00:26:173292 visual_properties.local_surface_id_allocation =
3293 local_surface_id_allocation_from_parent_;
danakja2c9d0a92018-07-25 20:01:183294 // We are changing the device scale factor from the renderer, so allocate a
3295 // new viz::LocalSurfaceId to avoid surface invariants violations in tests.
Kinuko Yasudabef2b642019-01-24 04:53:333296 if (layer_tree_view_)
3297 layer_tree_view_->RequestNewLocalSurfaceId();
3298
danakja2c9d0a92018-07-25 20:01:183299 OnSynchronizeVisualProperties(visual_properties);
3300}
3301
3302void RenderWidget::SetDeviceColorSpaceForTesting(
3303 const gfx::ColorSpace& color_space) {
3304 VisualProperties visual_properties;
3305 visual_properties.screen_info = screen_info_;
3306 visual_properties.screen_info.color_space = color_space;
3307 visual_properties.new_size = size();
3308 visual_properties.visible_viewport_size = visible_viewport_size_;
3309 visual_properties.compositor_viewport_pixel_size =
3310 compositor_viewport_pixel_size_;
3311 visual_properties.browser_controls_shrink_blink_size = false;
3312 visual_properties.top_controls_height = 0.f;
3313 visual_properties.is_fullscreen_granted = is_fullscreen_granted_;
3314 visual_properties.display_mode = display_mode_;
Fady Samuel84d8a1f72018-11-08 00:26:173315 visual_properties.local_surface_id_allocation =
3316 local_surface_id_allocation_from_parent_;
danakja2c9d0a92018-07-25 20:01:183317 // We are changing the device color space from the renderer, so allocate a
3318 // new viz::LocalSurfaceId to avoid surface invariants violations in tests.
Kinuko Yasudabef2b642019-01-24 04:53:333319 if (layer_tree_view_)
danakja2c9d0a92018-07-25 20:01:183320 layer_tree_view_->RequestNewLocalSurfaceId();
3321 OnSynchronizeVisualProperties(visual_properties);
3322}
3323
3324void RenderWidget::SetWindowRectSynchronouslyForTesting(
3325 const gfx::Rect& new_window_rect) {
3326 SetWindowRectSynchronously(new_window_rect);
3327}
3328
3329void RenderWidget::EnableAutoResizeForTesting(const gfx::Size& min_size,
3330 const gfx::Size& max_size) {
3331 VisualProperties visual_properties;
3332 visual_properties.auto_resize_enabled = true;
3333 visual_properties.min_size_for_auto_resize = min_size;
3334 visual_properties.max_size_for_auto_resize = max_size;
Fady Samuel84d8a1f72018-11-08 00:26:173335 visual_properties.local_surface_id_allocation =
3336 base::Optional<viz::LocalSurfaceIdAllocation>(
3337 viz::LocalSurfaceIdAllocation(
3338 viz::LocalSurfaceId(1, 1, base::UnguessableToken::Create()),
3339 base::TimeTicks::Now()));
danakja2c9d0a92018-07-25 20:01:183340 OnSynchronizeVisualProperties(visual_properties);
3341}
3342
3343void RenderWidget::DisableAutoResizeForTesting(const gfx::Size& new_size) {
3344 if (!auto_resize_mode_)
3345 return;
3346
3347 VisualProperties visual_properties;
3348 visual_properties.auto_resize_enabled = false;
3349 visual_properties.screen_info = screen_info_;
3350 visual_properties.new_size = new_size;
3351 visual_properties.compositor_viewport_pixel_size =
3352 compositor_viewport_pixel_size_;
3353 visual_properties.browser_controls_shrink_blink_size =
3354 browser_controls_shrink_blink_size_;
3355 visual_properties.top_controls_height = top_controls_height_;
3356 visual_properties.visible_viewport_size = visible_viewport_size_;
3357 visual_properties.is_fullscreen_granted = is_fullscreen_granted_;
3358 visual_properties.display_mode = display_mode_;
danakja2c9d0a92018-07-25 20:01:183359 OnSynchronizeVisualProperties(visual_properties);
3360}
3361
EhsanK955ba582017-11-30 21:14:403362blink::WebLocalFrame* RenderWidget::GetFocusedWebLocalFrameInWidget() const {
Ehsan Karamad655d7b8a2018-01-12 18:38:383363 if (auto* frame_widget = GetFrameWidget())
3364 return frame_widget->FocusedWebLocalFrameInWidget();
3365 return nullptr;
EhsanK955ba582017-11-30 21:14:403366}
3367
EhsanK22d482e2017-08-10 17:29:493368#if BUILDFLAG(ENABLE_PLUGINS)
3369PepperPluginInstanceImpl* RenderWidget::GetFocusedPepperPluginInsideWidget() {
Ehsan Karamad655d7b8a2018-01-12 18:38:383370 blink::WebFrameWidget* frame_widget = GetFrameWidget();
3371 if (!frame_widget)
EhsanK22d482e2017-08-10 17:29:493372 return nullptr;
3373
3374 // Focused pepper instance might not always be in the focused frame. For
3375 // instance if a pepper instance and its embedder frame are focused an then
3376 // another frame takes focus using javascript, the embedder frame will no
3377 // longer be focused while the pepper instance is (the embedder frame's
3378 // |focused_pepper_plugin_| is not nullptr). Especially, if the pepper plugin
3379 // is fullscreen, clicking into the pepper will not refocus the embedder
3380 // frame. This is why we have to traverse the whole frame tree to find the
3381 // focused plugin.
Ehsan Karamad655d7b8a2018-01-12 18:38:383382 blink::WebFrame* current_frame = frame_widget->LocalRoot();
EhsanK22d482e2017-08-10 17:29:493383 while (current_frame) {
3384 RenderFrameImpl* render_frame =
3385 current_frame->IsWebLocalFrame()
3386 ? RenderFrameImpl::FromWebFrame(current_frame)
3387 : nullptr;
3388 if (render_frame && render_frame->focused_pepper_plugin())
3389 return render_frame->focused_pepper_plugin();
3390 current_frame = current_frame->TraverseNext();
3391 }
3392 return nullptr;
3393}
3394#endif
3395
Ken Buchananb2c9e262018-03-10 16:53:313396gfx::Rect RenderWidget::ViewportVisibleRect() {
danakj6dcbc5962018-11-16 16:45:423397 return for_child_local_root_frame_
3398 ? compositor_visible_rect_
3399 : gfx::Rect(compositor_viewport_pixel_size_);
Ken Buchananb2c9e262018-03-10 16:53:313400}
3401
Hajime Hoshi315a61f2018-08-14 17:27:283402// static
3403scoped_refptr<base::SingleThreadTaskRunner>
3404RenderWidget::GetCleanupTaskRunner() {
3405 return RenderThreadImpl::current_blink_platform_impl()
3406 ->main_thread_scheduler()
3407 ->CleanupTaskRunner();
3408}
3409
Saman Sami50d1e0c2018-03-13 20:03:493410base::WeakPtr<RenderWidget> RenderWidget::AsWeakPtr() {
3411 return weak_ptr_factory_.GetWeakPtr();
3412}
3413
[email protected]e9ff79c2012-10-19 21:31:263414} // namespace content