blob: baf77e0f5b8ad200e3cbb8064bd9089e23249a65 [file] [log] [blame]
[email protected]60a50072012-01-11 02:05:351// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]2cff0052011-03-18 16:51:445#include "content/renderer/render_widget.h"
initial.commit09911bf2008-07-26 23:55:296
danakj4b347212018-07-04 17:55:177#include <cmath>
8#include <limits>
dchengcedca5612016-04-09 01:40:159#include <memory>
dcheng07945f632015-12-26 07:59:3210#include <utility>
11
[email protected]c27dd4f2014-05-22 18:05:1912#include "base/auto_reset.h"
danakj4b347212018-07-04 17:55:1713#include "base/base_switches.h"
[email protected]32876ae2011-11-15 22:25:2114#include "base/bind.h"
[email protected]4fb66842009-12-04 21:41:0015#include "base/command_line.h"
changwan7ded3752016-03-09 23:25:1216#include "base/feature_list.h"
initial.commit09911bf2008-07-26 23:55:2917#include "base/logging.h"
avi1023d012015-12-25 02:39:1418#include "base/macros.h"
dchengcedca5612016-04-09 01:40:1519#include "base/memory/ptr_util.h"
[email protected]b256eca2013-07-11 10:57:4020#include "base/memory/singleton.h"
asvitkine8d51e9d2016-09-02 23:55:4321#include "base/metrics/histogram_macros.h"
[email protected]aa4117f2011-12-09 22:19:2122#include "base/stl_util.h"
danakj4b347212018-07-04 17:55:1723#include "base/strings/string_number_conversions.h"
[email protected]74ebfb12013-06-07 20:48:0024#include "base/strings/utf_string_conversions.h"
Sebastien Marchand75a7cdf2018-11-13 23:47:0325#include "base/system/sys_info.h"
fdoraya19b7702016-12-23 14:19:3126#include "base/threading/thread_task_runner_handle.h"
primiano9e38d552015-01-28 04:18:0127#include "base/trace_event/trace_event.h"
[email protected]661eb9d2009-02-03 02:11:4828#include "build/build_config.h"
loyso6e6efc42017-01-21 02:23:0329#include "cc/animation/animation_host.h"
danakj4b347212018-07-04 17:55:1730#include "cc/base/switches.h"
xidachenfa0199e72017-05-11 11:34:2631#include "cc/input/touch_action.h"
danakjba65a0912017-09-21 16:38:4232#include "cc/trees/layer_tree_frame_sink.h"
loyso6e6efc42017-01-21 02:23:0333#include "cc/trees/layer_tree_host.h"
danakj4c987652018-07-05 15:49:4134#include "cc/trees/ukm_manager.h"
danakj4b347212018-07-04 17:55:1735#include "components/viz/common/features.h"
Fady Samuelc645ffe2017-07-24 17:28:2036#include "components/viz/common/frame_sinks/begin_frame_source.h"
danakjf20f4502017-09-26 17:13:3137#include "components/viz/common/frame_sinks/copy_output_request.h"
danakj4b347212018-07-04 17:55:1738#include "components/viz/common/switches.h"
39#include "content/common/content_switches_internal.h"
paulmeyer6ef5a792016-11-08 20:33:5840#include "content/common/drag_event_source_info.h"
41#include "content/common/drag_messages.h"
jonrossa2ff4f82018-02-16 17:27:4642#include "content/common/render_frame_metadata.mojom.h"
jcivelliae1560a2016-11-01 22:40:2343#include "content/common/render_message_filter.mojom.h"
Nasko Oskovf97e8b02017-07-25 02:45:4244#include "content/common/swapped_out_messages.h"
Sadrul Habib Chowdhury8bc43cc2018-06-06 01:48:0345#include "content/common/tab_switching_time_callback.h"
ekaramad9053e57b2016-04-26 20:00:3846#include "content/common/text_input_state.h"
Albert J. Wong3c93c182018-09-27 17:29:4347#include "content/common/widget_messages.h"
mlamouriafca06dd2017-01-27 23:35:1848#include "content/public/common/content_client.h"
changwan7ded3752016-03-09 23:25:1249#include "content/public/common/content_features.h"
[email protected]c08950d22011-10-13 22:20:2950#include "content/public/common/content_switches.h"
[email protected]a09d53ce2014-01-31 00:46:4251#include "content/public/common/context_menu_params.h"
paulmeyer90f6c31d2016-11-12 00:17:5952#include "content/public/common/drop_data.h"
Khushal3e96e662017-10-30 23:16:5053#include "content/public/common/service_names.mojom.h"
Jaebaek Seo655110e2017-12-19 23:27:3954#include "content/public/common/use_zoom_for_dsf_policy.h"
mlamouriafca06dd2017-01-27 23:35:1855#include "content/public/renderer/content_renderer_client.h"
Saman Samid189e5dfd2017-12-20 22:55:3156#include "content/renderer/browser_plugin/browser_plugin.h"
Sadrul Habib Chowdhury31c98712018-12-11 04:15:1357#include "content/renderer/compositor/layer_tree_view.h"
[email protected]953bd0062013-08-01 00:58:4058#include "content/renderer/cursor_utils.h"
mfomitchev2600fd7c2016-02-17 20:53:3959#include "content/renderer/devtools/render_widget_screen_metrics_emulator.h"
paulmeyer6ef5a792016-11-08 20:33:5860#include "content/renderer/drop_data_builder.h"
[email protected]b2e4c70132013-10-03 02:07:5161#include "content/renderer/external_popup_menu.h"
Sadrul Habib Chowdhury31c98712018-12-11 04:15:1362#include "content/renderer/frame_swap_message_queue.h"
[email protected]66fca5bc2013-05-23 06:58:2963#include "content/renderer/ime_event_guard.h"
dtapuska9ec1a912017-04-21 15:18:3164#include "content/renderer/input/main_thread_event_queue.h"
Dave Tapuska9db80842017-07-24 17:24:2665#include "content/renderer/input/widget_input_handler_manager.h"
[email protected]adab2332013-07-25 18:04:3266#include "content/renderer/pepper/pepper_plugin_instance_impl.h"
Sadrul Habib Chowdhury31c98712018-12-11 04:15:1367#include "content/renderer/queue_message_swap_promise.h"
[email protected]bffc8302014-01-23 20:52:1668#include "content/renderer/render_frame_impl.h"
danakj4b347212018-07-04 17:55:1769#include "content/renderer/render_frame_metadata_observer_impl.h"
[email protected]e3244ed2014-06-20 20:04:2770#include "content/renderer/render_frame_proxy.h"
[email protected]8704f89b2011-04-15 00:30:0571#include "content/renderer/render_process.h"
[email protected]f1a29a02011-10-06 23:08:4472#include "content/renderer/render_thread_impl.h"
dcheng3ce04b62015-10-26 23:30:5573#include "content/renderer/render_view_impl.h"
tfarina556a7232014-10-05 01:02:0974#include "content/renderer/renderer_blink_platform_impl.h"
[email protected]5b45ad42013-10-25 00:42:0475#include "content/renderer/resizing_mode_selector.h"
danakj4b347212018-07-04 17:55:1776#include "gpu/command_buffer/service/gpu_switches.h"
ekaramad330ba4232016-09-23 17:57:4777#include "ipc/ipc_message_start.h"
[email protected]484955942010-08-19 16:13:1878#include "ipc/ipc_sync_message.h"
Antoine Laboure55c9ef82017-11-10 18:51:2379#include "ipc/ipc_sync_message_filter.h"
danakj4b347212018-07-04 17:55:1780#include "media/base/media_switches.h"
Scott Violet02e38b92018-03-27 23:42:1481#include "ppapi/buildflags/buildflags.h"
[email protected]661eb9d2009-02-03 02:11:4882#include "skia/ext/platform_canvas.h"
Blink Reformata30d4232018-04-07 15:31:0683#include "third_party/blink/public/platform/file_path_conversion.h"
Hajime Hoshi315a61f2018-08-14 17:27:2884#include "third_party/blink/public/platform/platform.h"
Blink Reformata30d4232018-04-07 15:31:0685#include "third_party/blink/public/platform/scheduler/web_render_widget_scheduling_state.h"
Yuta Kitamuradeb91bb2018-05-29 05:53:2086#include "third_party/blink/public/platform/scheduler/web_thread_scheduler.h"
Blink Reformata30d4232018-04-07 15:31:0687#include "third_party/blink/public/platform/web_cursor_info.h"
88#include "third_party/blink/public/platform/web_drag_data.h"
89#include "third_party/blink/public/platform/web_drag_operation.h"
90#include "third_party/blink/public/platform/web_mouse_event.h"
91#include "third_party/blink/public/platform/web_point.h"
92#include "third_party/blink/public/platform/web_rect.h"
93#include "third_party/blink/public/platform/web_runtime_features.h"
94#include "third_party/blink/public/platform/web_size.h"
95#include "third_party/blink/public/platform/web_string.h"
96#include "third_party/blink/public/web/web_autofill_client.h"
97#include "third_party/blink/public/web/web_device_emulation_params.h"
98#include "third_party/blink/public/web/web_frame_widget.h"
99#include "third_party/blink/public/web/web_input_method_controller.h"
100#include "third_party/blink/public/web/web_local_frame.h"
101#include "third_party/blink/public/web/web_node.h"
102#include "third_party/blink/public/web/web_page_popup.h"
103#include "third_party/blink/public/web/web_popup_menu_info.h"
104#include "third_party/blink/public/web/web_range.h"
105#include "third_party/blink/public/web/web_settings.h"
106#include "third_party/blink/public/web/web_view.h"
107#include "third_party/blink/public/web/web_widget.h"
[email protected]d353541f2012-05-03 22:45:41108#include "third_party/skia/include/core/SkShader.h"
paulmeyer90f6c31d2016-11-12 00:17:59109#include "ui/base/clipboard/clipboard.h"
Scott Violet8ff9c302018-02-22 22:28:35110#include "ui/base/ui_base_features.h"
danakj4b347212018-07-04 17:55:17111#include "ui/base/ui_base_switches.h"
dtapuska97286c882017-02-24 23:14:43112#include "ui/events/base_event_utils.h"
tfarina655f81d2014-12-23 02:38:50113#include "ui/gfx/geometry/point_conversions.h"
tfarina3b0452d2014-12-31 15:20:09114#include "ui/gfx/geometry/rect_conversions.h"
tfarinaebe974f02015-01-03 04:25:32115#include "ui/gfx/geometry/size_conversions.h"
[email protected]1835b9e2012-02-28 13:12:48116#include "ui/gfx/skia_util.h"
danakj4b347212018-07-04 17:55:17117#include "ui/gfx/switches.h"
[email protected]c9e2cbbb2012-05-12 21:17:27118#include "ui/gl/gl_switches.h"
danakj4b347212018-07-04 17:55:17119#include "ui/native_theme/native_theme_features.h"
120#include "ui/native_theme/overlay_scrollbar_constants_aura.h"
[email protected]d353541f2012-05-03 22:45:41121#include "ui/surface/transport_dib.h"
[email protected]661eb9d2009-02-03 02:11:48122
[email protected]eeb93112013-05-01 19:41:10123#if defined(OS_ANDROID)
[email protected]cefe9b152014-03-27 18:16:15124#include <android/keycodes.h>
wjmaclean1d970622017-01-21 22:28:24125#include "base/time/time.h"
[email protected]eeb93112013-05-01 19:41:10126#endif
127
[email protected]661eb9d2009-02-03 02:11:48128#if defined(OS_POSIX)
[email protected]d5282e72009-05-13 13:16:52129#include "third_party/skia/include/core/SkMallocPixelRef.h"
[email protected]d353541f2012-05-03 22:45:41130#include "third_party/skia/include/core/SkPixelRef.h"
[email protected]661eb9d2009-02-03 02:11:48131#endif // defined(OS_POSIX)
[email protected]8085dbc82008-09-26 22:53:44132
sadrul943e3b32016-08-04 18:22:59133#if defined(USE_AURA)
sadrul602ce1362017-01-26 06:41:10134#include "content/renderer/mus/renderer_window_tree_client.h"
penghuang28a5fa22015-12-02 17:58:19135#endif
136
ekaramad330ba4232016-09-23 17:57:47137#if defined(OS_MACOSX)
138#include "content/renderer/text_input_client_observer.h"
139#endif
140
Ryan Landay9e42fd742017-08-12 01:59:11141using blink::WebImeTextSpan;
[email protected]180ef242013-11-07 06:50:46142using blink::WebCursorInfo;
[email protected]19193682014-04-03 15:01:43143using blink::WebDeviceEmulationParams;
paulmeyer90f6c31d2016-11-12 00:17:59144using blink::WebDragOperation;
paulmeyer6ef5a792016-11-08 20:33:58145using blink::WebDragOperationsMask;
146using blink::WebDragData;
paulmeyer90f6c31d2016-11-12 00:17:59147using blink::WebFrameWidget;
[email protected]180ef242013-11-07 06:50:46148using blink::WebGestureEvent;
149using blink::WebInputEvent;
dtapuska5d2e9c32015-12-03 16:39:49150using blink::WebInputEventResult;
ekaramad2daaf672016-11-10 20:29:01151using blink::WebInputMethodController;
paulmeyer6ef5a792016-11-08 20:33:58152using blink::WebLocalFrame;
[email protected]180ef242013-11-07 06:50:46153using blink::WebMouseEvent;
154using blink::WebMouseWheelEvent;
155using blink::WebNavigationPolicy;
donnda070f3c2015-01-16 19:54:11156using blink::WebNode;
[email protected]180ef242013-11-07 06:50:46157using blink::WebPagePopup;
dglazkove353a372016-09-01 01:33:48158using blink::WebRange;
[email protected]180ef242013-11-07 06:50:46159using blink::WebRect;
[email protected]180ef242013-11-07 06:50:46160using blink::WebSize;
paulmeyer90f6c31d2016-11-12 00:17:59161using blink::WebString;
[email protected]180ef242013-11-07 06:50:46162using blink::WebTextDirection;
163using blink::WebTouchEvent;
[email protected]f8ed4722013-12-03 03:27:25164using blink::WebTouchPoint;
[email protected]180ef242013-11-07 06:50:46165using blink::WebVector;
166using blink::WebWidget;
[email protected]e9ff79c2012-10-19 21:31:26167
paulmeyer90f6c31d2016-11-12 00:17:59168namespace content {
169
[email protected]6a4d7f62013-01-07 21:32:13170namespace {
[email protected]b256eca2013-07-11 10:57:40171
Albert J. Wongcb004632018-07-10 22:58:25172using RoutingIDWidgetMap = std::map<int32_t, RenderWidget*>;
173base::LazyInstance<RoutingIDWidgetMap>::Leaky g_routing_id_widget_map =
174 LAZY_INSTANCE_INITIALIZER;
175
danakj4b347212018-07-04 17:55:17176const base::Feature kUnpremultiplyAndDitherLowBitDepthTiles = {
177 "UnpremultiplyAndDitherLowBitDepthTiles", base::FEATURE_ENABLED_BY_DEFAULT};
178
[email protected]b256eca2013-07-11 10:57:40179typedef std::map<std::string, ui::TextInputMode> TextInputModeMap;
180
AJITH KUMAR V041c0b02017-08-08 10:39:20181static const int kInvalidNextPreviousFlagsValue = -1;
yiyix6b90ef52018-10-09 19:55:30182static const char* kOOPIF = "OOPIF";
183static const char* kRenderer = "Renderer";
AJITH KUMAR V041c0b02017-08-08 10:39:20184
lfgbee1e0a2016-06-08 21:24:21185class WebWidgetLockTarget : public content::MouseLockDispatcher::LockTarget {
186 public:
187 explicit WebWidgetLockTarget(blink::WebWidget* webwidget)
188 : webwidget_(webwidget) {}
189
190 void OnLockMouseACK(bool succeeded) override {
191 if (succeeded)
Blink Reformat1c4d759e2017-04-09 16:34:54192 webwidget_->DidAcquirePointerLock();
lfgbee1e0a2016-06-08 21:24:21193 else
Blink Reformat1c4d759e2017-04-09 16:34:54194 webwidget_->DidNotAcquirePointerLock();
lfgbee1e0a2016-06-08 21:24:21195 }
196
Blink Reformat1c4d759e2017-04-09 16:34:54197 void OnMouseLockLost() override { webwidget_->DidLosePointerLock(); }
lfgbee1e0a2016-06-08 21:24:21198
199 bool HandleMouseLockedInputEvent(const blink::WebMouseEvent& event) override {
200 // The WebWidget handles mouse lock in Blink's handleInputEvent().
201 return false;
202 }
203
204 private:
205 blink::WebWidget* webwidget_;
206};
207
dglazkov97b6c2b2016-10-25 17:35:55208bool IsDateTimeInput(ui::TextInputType type) {
209 return type == ui::TEXT_INPUT_TYPE_DATE ||
210 type == ui::TEXT_INPUT_TYPE_DATE_TIME ||
211 type == ui::TEXT_INPUT_TYPE_DATE_TIME_LOCAL ||
212 type == ui::TEXT_INPUT_TYPE_MONTH ||
213 type == ui::TEXT_INPUT_TYPE_TIME || type == ui::TEXT_INPUT_TYPE_WEEK;
214}
215
paulmeyer90f6c31d2016-11-12 00:17:59216WebDragData DropMetaDataToWebDragData(
217 const std::vector<DropData::Metadata>& drop_meta_data) {
218 std::vector<WebDragData::Item> item_list;
219 for (const auto& meta_data_item : drop_meta_data) {
220 if (meta_data_item.kind == DropData::Kind::STRING) {
221 WebDragData::Item item;
Blink Reformat1c4d759e2017-04-09 16:34:54222 item.storage_type = WebDragData::Item::kStorageTypeString;
223 item.string_type = WebString::FromUTF16(meta_data_item.mime_type);
paulmeyer90f6c31d2016-11-12 00:17:59224 // Have to pass a dummy URL here instead of an empty URL because the
225 // DropData received by browser_plugins goes through a round trip:
226 // DropData::MetaData --> WebDragData-->DropData. In the end, DropData
227 // will contain an empty URL (which means no URL is dragged) if the URL in
228 // WebDragData is empty.
229 if (base::EqualsASCII(meta_data_item.mime_type,
230 ui::Clipboard::kMimeTypeURIList)) {
Blink Reformat1c4d759e2017-04-09 16:34:54231 item.string_data = WebString::FromUTF8("about:dragdrop-placeholder");
paulmeyer90f6c31d2016-11-12 00:17:59232 }
233 item_list.push_back(item);
234 continue;
235 }
236
237 // TODO(hush): crbug.com/584789. Blink needs to support creating a file with
238 // just the mimetype. This is needed to drag files to WebView on Android
239 // platform.
240 if ((meta_data_item.kind == DropData::Kind::FILENAME) &&
241 !meta_data_item.filename.empty()) {
242 WebDragData::Item item;
Blink Reformat1c4d759e2017-04-09 16:34:54243 item.storage_type = WebDragData::Item::kStorageTypeFilename;
244 item.filename_data = blink::FilePathToWebString(meta_data_item.filename);
paulmeyer90f6c31d2016-11-12 00:17:59245 item_list.push_back(item);
246 continue;
247 }
248
249 if (meta_data_item.kind == DropData::Kind::FILESYSTEMFILE) {
250 WebDragData::Item item;
Blink Reformat1c4d759e2017-04-09 16:34:54251 item.storage_type = WebDragData::Item::kStorageTypeFileSystemFile;
252 item.file_system_url = meta_data_item.file_system_url;
paulmeyer90f6c31d2016-11-12 00:17:59253 item_list.push_back(item);
254 continue;
255 }
256 }
257
258 WebDragData result;
Blink Reformat1c4d759e2017-04-09 16:34:54259 result.Initialize();
260 result.SetItems(item_list);
paulmeyer90f6c31d2016-11-12 00:17:59261 return result;
262}
263
264WebDragData DropDataToWebDragData(const DropData& drop_data) {
265 std::vector<WebDragData::Item> item_list;
266
267 // These fields are currently unused when dragging into WebKit.
268 DCHECK(drop_data.download_metadata.empty());
269 DCHECK(drop_data.file_contents.empty());
dcheng3dd85612017-02-08 10:46:23270 DCHECK(drop_data.file_contents_content_disposition.empty());
paulmeyer90f6c31d2016-11-12 00:17:59271
272 if (!drop_data.text.is_null()) {
273 WebDragData::Item item;
Blink Reformat1c4d759e2017-04-09 16:34:54274 item.storage_type = WebDragData::Item::kStorageTypeString;
275 item.string_type = WebString::FromUTF8(ui::Clipboard::kMimeTypeText);
276 item.string_data = WebString::FromUTF16(drop_data.text.string());
paulmeyer90f6c31d2016-11-12 00:17:59277 item_list.push_back(item);
278 }
279
280 if (!drop_data.url.is_empty()) {
281 WebDragData::Item item;
Blink Reformat1c4d759e2017-04-09 16:34:54282 item.storage_type = WebDragData::Item::kStorageTypeString;
283 item.string_type = WebString::FromUTF8(ui::Clipboard::kMimeTypeURIList);
284 item.string_data = WebString::FromUTF8(drop_data.url.spec());
285 item.title = WebString::FromUTF16(drop_data.url_title);
paulmeyer90f6c31d2016-11-12 00:17:59286 item_list.push_back(item);
287 }
288
289 if (!drop_data.html.is_null()) {
290 WebDragData::Item item;
Blink Reformat1c4d759e2017-04-09 16:34:54291 item.storage_type = WebDragData::Item::kStorageTypeString;
292 item.string_type = WebString::FromUTF8(ui::Clipboard::kMimeTypeHTML);
293 item.string_data = WebString::FromUTF16(drop_data.html.string());
294 item.base_url = drop_data.html_base_url;
paulmeyer90f6c31d2016-11-12 00:17:59295 item_list.push_back(item);
296 }
297
jdoerrie5a73d0fa2018-10-02 23:54:05298 for (auto it = drop_data.filenames.begin(); it != drop_data.filenames.end();
paulmeyer90f6c31d2016-11-12 00:17:59299 ++it) {
300 WebDragData::Item item;
Blink Reformat1c4d759e2017-04-09 16:34:54301 item.storage_type = WebDragData::Item::kStorageTypeFilename;
302 item.filename_data = blink::FilePathToWebString(it->path);
303 item.display_name_data =
kinukoa4293022017-01-27 06:43:25304 blink::FilePathToWebString(base::FilePath(it->display_name));
paulmeyer90f6c31d2016-11-12 00:17:59305 item_list.push_back(item);
306 }
307
jdoerrie5a73d0fa2018-10-02 23:54:05308 for (auto it = drop_data.file_system_files.begin();
309 it != drop_data.file_system_files.end(); ++it) {
paulmeyer90f6c31d2016-11-12 00:17:59310 WebDragData::Item item;
Blink Reformat1c4d759e2017-04-09 16:34:54311 item.storage_type = WebDragData::Item::kStorageTypeFileSystemFile;
312 item.file_system_url = it->url;
313 item.file_system_file_size = it->size;
314 item.file_system_id = blink::WebString::FromASCII(it->filesystem_id);
paulmeyer90f6c31d2016-11-12 00:17:59315 item_list.push_back(item);
316 }
317
Stuart Langley48300392017-11-02 03:54:44318 for (const auto& it : drop_data.custom_data) {
paulmeyer90f6c31d2016-11-12 00:17:59319 WebDragData::Item item;
Blink Reformat1c4d759e2017-04-09 16:34:54320 item.storage_type = WebDragData::Item::kStorageTypeString;
Stuart Langley48300392017-11-02 03:54:44321 item.string_type = WebString::FromUTF16(it.first);
322 item.string_data = WebString::FromUTF16(it.second);
paulmeyer90f6c31d2016-11-12 00:17:59323 item_list.push_back(item);
324 }
325
326 WebDragData result;
Blink Reformat1c4d759e2017-04-09 16:34:54327 result.Initialize();
328 result.SetItems(item_list);
329 result.SetFilesystemId(WebString::FromUTF16(drop_data.filesystem_id));
paulmeyer90f6c31d2016-11-12 00:17:59330 return result;
331}
332
dtapuskac4dd5be2016-10-25 15:11:10333ui::TextInputType ConvertWebTextInputType(blink::WebTextInputType type) {
334 // Check the type is in the range representable by ui::TextInputType.
335 DCHECK_LE(type, static_cast<int>(ui::TEXT_INPUT_TYPE_MAX))
336 << "blink::WebTextInputType and ui::TextInputType not synchronized";
337 return static_cast<ui::TextInputType>(type);
338}
339
340ui::TextInputMode ConvertWebTextInputMode(blink::WebTextInputMode mode) {
341 // Check the mode is in the range representable by ui::TextInputMode.
342 DCHECK_LE(mode, static_cast<int>(ui::TEXT_INPUT_MODE_MAX))
343 << "blink::WebTextInputMode and ui::TextInputMode not synchronized";
344 return static_cast<ui::TextInputMode>(mode);
345}
346
Mitsuru Oshima3c07b332018-02-06 04:56:50347// Returns true if the device scale is high enough that losing subpixel
348// antialiasing won't have a noticeable effect on text quality.
349static bool DeviceScaleEnsuresTextQuality(float device_scale_factor) {
350#if defined(OS_ANDROID) || defined(OS_CHROMEOS)
351 // On Android, we never have subpixel antialiasing. On Chrome OS we prefer to
352 // composite all scrollers so that we get animated overlay scrollbars.
353 return true;
354#else
355 // 1.5 is a common touchscreen tablet device scale factor. For such
356 // devices main thread antialiasing is a heavy burden.
357 return device_scale_factor >= 1.5f;
358#endif
359}
360
361static bool PreferCompositingToLCDText(CompositorDependencies* compositor_deps,
362 float device_scale_factor) {
363 const base::CommandLine& command_line =
364 *base::CommandLine::ForCurrentProcess();
365 if (command_line.HasSwitch(switches::kDisablePreferCompositingToLCDText))
366 return false;
367 if (command_line.HasSwitch(switches::kEnablePreferCompositingToLCDText))
368 return true;
369 if (!compositor_deps->IsLcdTextEnabled())
370 return true;
371 return DeviceScaleEnsuresTextQuality(device_scale_factor);
372}
373
[email protected]b256eca2013-07-11 10:57:40374} // namespace
375
[email protected]b2e4c70132013-10-03 02:07:51376// RenderWidget ---------------------------------------------------------------
377
danakj829cdd142018-09-14 21:13:27378RenderWidget::RenderWidget(int32_t widget_routing_id,
379 CompositorDependencies* compositor_deps,
danakj829cdd142018-09-14 21:13:27380 const ScreenInfo& screen_info,
381 blink::WebDisplayMode display_mode,
danakj6dcbc5962018-11-16 16:45:42382 bool is_frozen,
danakj829cdd142018-09-14 21:13:27383 bool hidden,
384 bool never_visible,
385 mojom::WidgetRequest widget_request)
nick8331f8ad2016-11-15 20:42:45386 : routing_id_(widget_routing_id),
dcheng35d31c112015-07-22 00:17:36387 compositor_deps_(compositor_deps),
lfg8ff33912016-09-13 20:59:21388 webwidget_internal_(nullptr),
[email protected]847a2582013-03-09 02:29:51389 auto_resize_mode_(false),
[email protected]1ac10dca2013-08-20 20:47:04390 is_hidden_(hidden),
sievers71c62dd52015-10-07 01:44:39391 compositor_never_visible_(never_visible),
mikhail.pozdnyakovf2c902a2015-04-14 08:09:12392 is_fullscreen_granted_(false),
danakj73dd3032018-07-28 17:49:53393 display_mode_(display_mode),
changwanf2a707b2015-10-30 08:22:16394 ime_event_guard_(nullptr),
[email protected]661eb9d2009-02-03 02:11:48395 closing_(false),
danakj5c5269b2018-11-06 00:39:11396 host_will_close_this_(false),
danakj6dcbc5962018-11-16 16:45:42397 is_frozen_(is_frozen),
dglazkov97b6c2b2016-10-25 17:35:55398 text_input_type_(ui::TEXT_INPUT_TYPE_NONE),
[email protected]b256eca2013-07-11 10:57:40399 text_input_mode_(ui::TEXT_INPUT_MODE_DEFAULT),
shuchen82ce8c52014-10-23 01:55:20400 text_input_flags_(0),
AJITH KUMAR V041c0b02017-08-08 10:39:20401 next_previous_flags_(kInvalidNextPreviousFlagsValue),
[email protected]86ba5fcb2013-09-04 00:36:53402 can_compose_inline_(true),
nonafa291792016-08-10 02:36:18403 composition_range_(gfx::Range::InvalidRange()),
[email protected]867125a02009-12-10 06:01:48404 pending_window_rect_count_(0),
Scott Violetae08f332018-07-27 17:37:23405 screen_info_(screen_info),
nonafa291792016-08-10 02:36:18406 monitor_composition_info_(false),
[email protected]b2e4c70132013-10-03 02:07:51407 popup_origin_scale_for_emulation_(0.f),
samans26510e442017-06-01 22:29:12408 frame_swap_message_queue_(new FrameSwapMessageQueue(routing_id_)),
[email protected]a09d53ce2014-01-31 00:46:42409 resizing_mode_selector_(new ResizingModeSelector()),
lfge0c2792ec2016-05-11 18:52:08410 has_host_context_menu_location_(false),
ekaramad2a46d632016-07-19 13:33:09411 has_focus_(false),
danakj6dcbc5962018-11-16 16:45:42412 for_child_local_root_frame_(false),
ekaramad330ba4232016-09-23 17:57:47413#if defined(OS_MACOSX)
414 text_input_client_observer_(new TextInputClientObserver(this)),
415#endif
Takashi SAKAMOTO870f6262017-08-22 04:08:27416 first_update_visual_state_after_hidden_(false),
tasakb95dbb50c2017-02-08 18:07:50417 was_shown_time_(base::TimeTicks::Now()),
kenrb5d78b842017-03-06 21:06:01418 current_content_source_id_(0),
Dave Tapuska1bdf1832017-07-07 18:07:19419 widget_binding_(this, std::move(widget_request)),
danakjf87460792018-12-04 20:14:22420 warmup_weak_ptr_factory_(this),
wjmaclean1d970622017-01-21 22:28:24421 weak_ptr_factory_(this) {
nick8331f8ad2016-11-15 20:42:45422 DCHECK_NE(routing_id_, MSG_ROUTING_NONE);
[email protected]380244092011-10-07 17:26:27423 DCHECK(RenderThread::Get());
alexclarke7fa93942015-10-21 15:37:11424
425 // In tests there may not be a RenderThreadImpl.
426 if (RenderThreadImpl::current()) {
427 render_widget_scheduling_state_ = RenderThreadImpl::current()
Yuta Kitamura3331f5c2018-04-05 11:12:25428 ->GetWebMainThreadScheduler()
dcheng07945f632015-12-26 07:59:32429 ->NewRenderWidgetSchedulingState();
alexclarke7fa93942015-10-21 15:37:11430 render_widget_scheduling_state_->SetHidden(is_hidden_);
431 }
sadrul602ce1362017-01-26 06:41:10432#if defined(USE_AURA)
fsamuel098eade2017-03-21 18:06:14433 RendererWindowTreeClient::CreateIfNecessary(routing_id_);
Scott Violete46b3352018-09-22 00:37:07434 if (features::IsMultiProcessMash())
Scott Violet1098538e2017-10-05 19:23:33435 RendererWindowTreeClient::Get(routing_id_)->SetVisible(!is_hidden_);
sadrul602ce1362017-01-26 06:41:10436#endif
Albert J. Wongcb004632018-07-10 22:58:25437
438 if (routing_id_ != MSG_ROUTING_NONE)
439 g_routing_id_widget_map.Get().emplace(routing_id_, this);
initial.commit09911bf2008-07-26 23:55:29440}
441
442RenderWidget::~RenderWidget() {
lfg8ff33912016-09-13 20:59:21443 DCHECK(!webwidget_internal_) << "Leaking our WebWidget!";
Albert J. Wong7bbf22d2018-12-20 00:27:27444
445 // TODO(ajwong): Test if this actually has to be reset here
446 // or if it is okay to tear down later in the destructor. This
447 // would allow for removing the ordering constraint in teardown.
448 // https://crbug.com/545684
449 delegate_.reset();
450
Albert J. Wongcb004632018-07-10 22:58:25451 // TODO(ajwong): Add in check that routing_id_ has been removed from
452 // g_routing_id_widget_map once the shutdown semantics for RenderWidget
453 // and RenderViewImpl are rationalized. Currently, too many unit and
454 // browser tests delete a RenderWidget without correclty going through
455 // the shutdown. https://crbug.com/545684
[email protected]bffc8302014-01-23 20:52:16456
dtapuska9ec1a912017-04-21 15:18:31457 if (input_event_queue_)
458 input_event_queue_->ClearClient();
459
sadrul8185d972017-01-31 18:30:11460#if defined(USE_AURA)
fsamuel098eade2017-03-21 18:06:14461 // It is possible for a RenderWidget to be destroyed before it was embedded
462 // in a mus window. The RendererWindowTreeClient will leak in such cases. So
463 // explicitly delete it here.
464 RendererWindowTreeClient::Destroy(routing_id_);
sadrul8185d972017-01-31 18:30:11465#endif
initial.commit09911bf2008-07-26 23:55:29466}
467
[email protected]484955942010-08-19 16:13:18468// static
Albert J. Wongcb004632018-07-10 22:58:25469RenderWidget* RenderWidget::FromRoutingID(int32_t routing_id) {
470 RoutingIDWidgetMap* widgets = g_routing_id_widget_map.Pointer();
jdoerrie5a73d0fa2018-10-02 23:54:05471 auto it = widgets->find(routing_id);
Albert J. Wongcb004632018-07-10 22:58:25472 return it == widgets->end() ? NULL : it->second;
473}
474
danakj25ec6e2b2018-09-26 17:01:31475void RenderWidget::InitForPopup(ShowCallback show_callback,
476 blink::WebPagePopup* web_page_popup) {
477 // Init() increments the reference count on |this|, making it
478 // self-referencing.
479 Init(std::move(show_callback), web_page_popup);
initial.commit09911bf2008-07-26 23:55:29480}
481
danakj6a816592018-09-25 18:30:56482void RenderWidget::InitForChildLocalRoot(
483 blink::WebFrameWidget* web_frame_widget) {
danakj6dcbc5962018-11-16 16:45:42484 for_child_local_root_frame_ = true;
danakj6a816592018-09-25 18:30:56485 // Init() increments the reference count on |this|, making it
486 // self-referencing.
487 Init(base::NullCallback(), web_frame_widget);
dchengda9b4bb2015-07-20 20:58:08488}
489
dchengda9b4bb2015-07-20 20:58:08490void RenderWidget::CloseForFrame() {
dchengd96a27a2015-07-24 20:17:32491 OnClose();
kenrba7199832015-01-22 23:44:59492}
493
Scott Violetae08f332018-07-27 17:37:23494void RenderWidget::Init(ShowCallback show_callback, WebWidget* web_widget) {
lfg8ff33912016-09-13 20:59:21495 DCHECK(!webwidget_internal_);
nick4df698d82016-11-11 20:39:23496 DCHECK_NE(routing_id_, MSG_ROUTING_NONE);
initial.commit09911bf2008-07-26 23:55:29497
danakj17216f4d2018-10-16 22:57:32498 RenderThreadImpl* render_thread_impl = RenderThreadImpl::current();
499
Jeremy Roman04f27c372017-10-27 15:20:55500 input_handler_ = std::make_unique<RenderWidgetInputHandler>(this, this);
nick8331f8ad2016-11-15 20:42:45501
danakj17216f4d2018-10-16 22:57:32502 LayerTreeView* layer_tree_view = InitializeLayerTreeView();
503 web_widget->SetLayerTreeView(layer_tree_view);
Dave Tapuska9db80842017-07-24 17:24:26504
danakj31699d5b2018-09-18 15:39:17505 blink::scheduler::WebThreadScheduler* main_thread_scheduler = nullptr;
506 if (render_thread_impl)
507 main_thread_scheduler = render_thread_impl->GetWebMainThreadScheduler();
Alexander Timin41e917d2018-07-02 16:25:35508 blink::scheduler::WebThreadScheduler* compositor_thread_scheduler =
509 blink::scheduler::WebThreadScheduler::CompositorThreadScheduler();
danakj31699d5b2018-09-18 15:39:17510 scoped_refptr<base::SingleThreadTaskRunner> compositor_input_task_runner;
danakj89ae4532018-12-12 22:52:55511 // Use the compositor thread task runner unless this is a popup or other such
512 // non-frame widgets. The |compositor_thread_scheduler| can be null in tests
513 // without a compositor thread.
514 if (for_frame() && compositor_thread_scheduler) {
515 compositor_input_task_runner =
516 compositor_thread_scheduler->InputTaskRunner();
danakj31699d5b2018-09-18 15:39:17517 }
Alexander Timin41e917d2018-07-02 16:25:35518
Dave Tapuska04bc5ee92018-04-17 19:03:31519 widget_input_handler_manager_ = WidgetInputHandlerManager::Create(
danakj31699d5b2018-09-18 15:39:17520 weak_ptr_factory_.GetWeakPtr(), std::move(compositor_input_task_runner),
521 main_thread_scheduler);
Dave Tapuska9db80842017-07-24 17:24:26522
danakjdf1ceb72018-07-18 20:02:25523 show_callback_ = std::move(show_callback);
initial.commit09911bf2008-07-26 23:55:29524
lfg8ff33912016-09-13 20:59:21525 webwidget_internal_ = web_widget;
526 webwidget_mouse_lock_target_.reset(
527 new WebWidgetLockTarget(webwidget_internal_));
lfgbee1e0a2016-06-08 21:24:21528 mouse_lock_dispatcher_.reset(new RenderWidgetMouseLockDispatcher(this));
initial.commit09911bf2008-07-26 23:55:29529
nick4df698d82016-11-11 20:39:23530 RenderThread::Get()->AddRoute(routing_id_, this);
531 // Take a reference on behalf of the RenderThread. This will be balanced
Albert J. Wong3c93c182018-09-27 17:29:43532 // when we receive WidgetMsg_Close.
nick4df698d82016-11-11 20:39:23533 AddRef();
534 if (RenderThreadImpl::current()) {
535 RenderThreadImpl::current()->WidgetCreated();
536 if (is_hidden_)
537 RenderThreadImpl::current()->WidgetHidden();
initial.commit09911bf2008-07-26 23:55:29538 }
539}
540
danakj53802692018-07-25 21:46:44541void RenderWidget::ApplyEmulatedScreenMetricsForPopupWidget(
542 RenderWidget* origin_widget) {
543 RenderWidgetScreenMetricsEmulator* emulator =
544 origin_widget->screen_metrics_emulator_.get();
545 if (!emulator)
546 return;
[email protected]b2e4c70132013-10-03 02:07:51547 popup_origin_scale_for_emulation_ = emulator->scale();
[email protected]19193682014-04-03 15:01:43548 popup_view_origin_for_emulation_ = emulator->applied_widget_rect().origin();
Pavel Feldmancc099f72017-07-20 23:09:00549 popup_screen_origin_for_emulation_ =
Lei Zhang9b359d32017-11-28 00:57:02550 emulator->original_screen_rect().origin();
Fady Samuel1c1ad3692018-11-06 23:28:40551 UpdateSurfaceAndScreenInfo(local_surface_id_allocation_from_parent_,
552 compositor_viewport_pixel_size_,
553 emulator->original_screen_info());
[email protected]b2e4c70132013-10-03 02:07:51554}
555
[email protected]2d6836f42014-07-02 17:25:31556gfx::Rect RenderWidget::AdjustValidationMessageAnchor(const gfx::Rect& anchor) {
557 if (screen_metrics_emulator_)
558 return screen_metrics_emulator_->AdjustValidationMessageAnchor(anchor);
559 return anchor;
560}
561
thakis18e426412017-03-15 12:06:37562#if BUILDFLAG(USE_EXTERNAL_POPUP_MENU)
[email protected]b2e4c70132013-10-03 02:07:51563void RenderWidget::SetExternalPopupOriginAdjustmentsForEmulation(
Albert J. Wong7bbf22d2018-12-20 00:27:27564 ExternalPopupMenu* popup) {
565 if (screen_metrics_emulator_)
566 popup->SetOriginScaleForEmulation(screen_metrics_emulator_->scale());
[email protected]b2e4c70132013-10-03 02:07:51567}
[email protected]53907862014-03-25 15:42:40568#endif
[email protected]b2e4c70132013-10-03 02:07:51569
570void RenderWidget::OnShowHostContextMenu(ContextMenuParams* params) {
571 if (screen_metrics_emulator_)
572 screen_metrics_emulator_->OnShowContextMenu(params);
573}
574
[email protected]a95986a82010-12-24 06:19:28575bool RenderWidget::OnMessageReceived(const IPC::Message& message) {
ekaramad330ba4232016-09-23 17:57:47576#if defined(OS_MACOSX)
577 if (IPC_MESSAGE_CLASS(message) == TextInputClientMsgStart)
578 return text_input_client_observer_->OnMessageReceived(message);
579#endif
lfgbee1e0a2016-06-08 21:24:21580 if (mouse_lock_dispatcher_ &&
581 mouse_lock_dispatcher_->OnMessageReceived(message))
582 return true;
583
[email protected]a95986a82010-12-24 06:19:28584 bool handled = true;
585 IPC_BEGIN_MESSAGE_MAP(RenderWidget, message)
Albert J. Wong3c93c182018-09-27 17:29:43586 IPC_MESSAGE_HANDLER(WidgetMsg_ShowContextMenu, OnShowContextMenu)
587 IPC_MESSAGE_HANDLER(WidgetMsg_Close, OnClose)
588 IPC_MESSAGE_HANDLER(WidgetMsg_SynchronizeVisualProperties,
Fady Samuel799e72192018-04-25 21:16:57589 OnSynchronizeVisualProperties)
Albert J. Wong3c93c182018-09-27 17:29:43590 IPC_MESSAGE_HANDLER(WidgetMsg_EnableDeviceEmulation,
dgozman9260b0a12015-03-16 13:45:20591 OnEnableDeviceEmulation)
Albert J. Wong3c93c182018-09-27 17:29:43592 IPC_MESSAGE_HANDLER(WidgetMsg_DisableDeviceEmulation,
dgozman9260b0a12015-03-16 13:45:20593 OnDisableDeviceEmulation)
Albert J. Wong3c93c182018-09-27 17:29:43594 IPC_MESSAGE_HANDLER(WidgetMsg_WasHidden, OnWasHidden)
595 IPC_MESSAGE_HANDLER(WidgetMsg_WasShown, OnWasShown)
596 IPC_MESSAGE_HANDLER(WidgetMsg_SetActive, OnSetActive)
597 IPC_MESSAGE_HANDLER(WidgetMsg_SetBackgroundOpaque, OnSetBackgroundOpaque)
598 IPC_MESSAGE_HANDLER(WidgetMsg_SetTextDirection, OnSetTextDirection)
599 IPC_MESSAGE_HANDLER(WidgetMsg_SetBounds_ACK, OnRequestSetBoundsAck)
600 IPC_MESSAGE_HANDLER(WidgetMsg_UpdateScreenRects, OnUpdateScreenRects)
601 IPC_MESSAGE_HANDLER(WidgetMsg_ForceRedraw, OnForceRedraw)
602 IPC_MESSAGE_HANDLER(WidgetMsg_SetViewportIntersection,
kenrbea731792017-01-13 15:10:48603 OnSetViewportIntersection)
Albert J. Wong3c93c182018-09-27 17:29:43604 IPC_MESSAGE_HANDLER(WidgetMsg_SetIsInert, OnSetIsInert)
605 IPC_MESSAGE_HANDLER(WidgetMsg_SetInheritedEffectiveTouchAction,
sunxd540a9962018-05-24 22:51:06606 OnSetInheritedEffectiveTouchAction)
Albert J. Wong3c93c182018-09-27 17:29:43607 IPC_MESSAGE_HANDLER(WidgetMsg_UpdateRenderThrottlingStatus,
Ken Buchanan8a319fb2017-11-15 18:37:12608 OnUpdateRenderThrottlingStatus)
Albert J. Wong3c93c182018-09-27 17:29:43609 IPC_MESSAGE_HANDLER(WidgetMsg_WaitForNextFrameForTests,
lfg43e08e62016-02-03 18:51:37610 OnWaitNextFrameForTests)
paulmeyer90f6c31d2016-11-12 00:17:59611 IPC_MESSAGE_HANDLER(DragMsg_TargetDragEnter, OnDragTargetDragEnter)
612 IPC_MESSAGE_HANDLER(DragMsg_TargetDragOver, OnDragTargetDragOver)
613 IPC_MESSAGE_HANDLER(DragMsg_TargetDragLeave, OnDragTargetDragLeave)
614 IPC_MESSAGE_HANDLER(DragMsg_TargetDrop, OnDragTargetDrop)
paulmeyer8fc8ea92016-11-15 05:12:21615 IPC_MESSAGE_HANDLER(DragMsg_SourceEnded, OnDragSourceEnded)
616 IPC_MESSAGE_HANDLER(DragMsg_SourceSystemDragEnded,
617 OnDragSourceSystemDragEnded)
[email protected]a95986a82010-12-24 06:19:28618 IPC_MESSAGE_UNHANDLED(handled = false)
619 IPC_END_MESSAGE_MAP()
620 return handled;
621}
initial.commit09911bf2008-07-26 23:55:29622
623bool RenderWidget::Send(IPC::Message* message) {
Nasko Oskovf97e8b02017-07-25 02:45:42624 // Don't send any messages after the browser has told us to close, and filter
danakj6dcbc5962018-11-16 16:45:42625 // most outgoing messages when frozen.
626 if (closing_) {
627 delete message;
628 return false;
629 }
630 if (is_frozen_ && !SwappedOutMessages::CanSendWhileSwappedOut(message)) {
initial.commit09911bf2008-07-26 23:55:29631 delete message;
632 return false;
633 }
634
635 // If given a messsage without a routing ID, then assign our routing ID.
636 if (message->routing_id() == MSG_ROUTING_NONE)
637 message->set_routing_id(routing_id_);
638
[email protected]380244092011-10-07 17:26:27639 return RenderThread::Get()->Send(message);
[email protected]8085dbc82008-09-26 22:53:44640}
641
dtapuska014ed082016-11-11 21:58:48642void RenderWidget::SendOrCrash(IPC::Message* message) {
643 bool result = Send(message);
644 CHECK(closing_ || result) << "Failed to send message";
645}
646
ekaramad5aff1942017-01-06 01:26:35647bool RenderWidget::ShouldHandleImeEvents() const {
Albert J. Wong7bbf22d2018-12-20 00:27:27648 if (delegate())
danakj89ae4532018-12-12 22:52:55649 return has_focus_;
650 if (for_child_local_root_frame_) {
651 // TODO(ekaramad): We track page focus in all RenderViews on the page but
652 // the RenderWidgets corresponding to child local roots do not get the
653 // update. For now, this method returns true when the RenderWidget is for a
654 // child local frame, i.e., IME events will be processed regardless of page
655 // focus. We should revisit this after page focus for OOPIFs has been fully
656 // resolved (https://crbug.com/689777).
657 return true;
658 }
659 // Not a frame widget.
660 return false;
ekaramad5aff1942017-01-06 01:26:35661}
662
initial.commit09911bf2008-07-26 23:55:29663void RenderWidget::OnClose() {
dchengd96a27a2015-07-24 20:17:32664 DCHECK(content::RenderThread::Get());
665 if (closing_)
666 return;
danakj292b63e2018-11-19 20:47:06667 for (auto& observer : render_frames_)
668 observer.WidgetWillClose();
dchengd96a27a2015-07-24 20:17:32669 closing_ = true;
670
671 // Browser correspondence is no longer needed at this point.
672 if (routing_id_ != MSG_ROUTING_NONE) {
673 RenderThread::Get()->RemoveRoute(routing_id_);
Albert J. Wongcb004632018-07-10 22:58:25674 g_routing_id_widget_map.Get().erase(routing_id_);
danakj678f0252018-11-09 21:46:34675 if (RenderThreadImpl::current()) {
676 // RenderWidgets may be hidden when they are closed. If we were previously
677 // hidden, we are being counted as such in RenderThreadImpl. Thus we
678 // remove that count here by calling WidgetRestored() even though we're
679 // clearly not becoming visible here.
680 if (is_hidden_)
681 RenderThreadImpl::current()->WidgetRestored();
dchengd96a27a2015-07-24 20:17:32682 RenderThreadImpl::current()->WidgetDestroyed();
danakj678f0252018-11-09 21:46:34683 }
dchengd96a27a2015-07-24 20:17:32684 }
685
danakj6dcbc5962018-11-16 16:45:42686 if (for_child_local_root_frame_) {
dchengd96a27a2015-07-24 20:17:32687 // Widgets for frames may be created and closed at any time while the frame
Ken Buchananed449bb2018-02-01 21:02:05688 // is alive. However, WebWidget must be closed synchronously because frame
689 // widgets and frames hold pointers to each other. The deferred call to
690 // Close() will complete cleanup and release |this|, but CloseWebWidget()
691 // prevents Close() from attempting to access members of an
692 // already-deleted frame.
693 CloseWebWidget();
dchengd96a27a2015-07-24 20:17:32694 }
Ken Buchananed449bb2018-02-01 21:02:05695 // If there is a Send call on the stack, then it could be dangerous to close
696 // now. Post a task that only gets invoked when there are no nested message
697 // loops.
Hajime Hoshi315a61f2018-08-14 17:27:28698 GetCleanupTaskRunner()->PostNonNestableTask(
699 FROM_HERE, base::BindOnce(&RenderWidget::Close, this));
dchengd96a27a2015-07-24 20:17:32700
701 // Balances the AddRef taken when we called AddRoute.
702 Release();
initial.commit09911bf2008-07-26 23:55:29703}
704
Fady Samuel799e72192018-04-25 21:16:57705void RenderWidget::OnSynchronizeVisualProperties(
danakja2c9d0a92018-07-25 20:01:18706 const VisualProperties& original_params) {
707 TRACE_EVENT0("renderer", "RenderWidget::OnSynchronizeVisualProperties");
EhsanKbd2cea992017-11-23 18:49:08708
danakja2c9d0a92018-07-25 20:01:18709 VisualProperties params = original_params;
Albert J. Wong7bbf22d2018-12-20 00:27:27710 if (delegate()) {
711 delegate()->ApplyNewSizeForWidget(size_, params.new_size);
danakja2c9d0a92018-07-25 20:01:18712
713 if (display_mode_ != params.display_mode) {
714 display_mode_ = params.display_mode;
Albert J. Wong7bbf22d2018-12-20 00:27:27715 delegate()->ApplyNewDisplayModeForWidget(params.display_mode);
danakja2c9d0a92018-07-25 20:01:18716 }
717
718 bool auto_resize_mode_changed =
719 auto_resize_mode_ != params.auto_resize_enabled;
720 auto_resize_mode_ = params.auto_resize_enabled;
721 min_size_for_auto_resize_ = params.min_size_for_auto_resize;
722 max_size_for_auto_resize_ = params.max_size_for_auto_resize;
723
724 if (auto_resize_mode_) {
725 gfx::Size min_auto_size = min_size_for_auto_resize_;
726 gfx::Size max_auto_size = max_size_for_auto_resize_;
727 if (compositor_deps_->IsUseZoomForDSFEnabled()) {
728 min_auto_size = gfx::ScaleToCeiledSize(
729 min_auto_size, params.screen_info.device_scale_factor);
730 max_auto_size = gfx::ScaleToCeiledSize(
731 max_auto_size, params.screen_info.device_scale_factor);
732 }
Albert J. Wong7bbf22d2018-12-20 00:27:27733 delegate()->ApplyAutoResizeLimitsForWidget(min_auto_size, max_auto_size);
danakja2c9d0a92018-07-25 20:01:18734 } else if (auto_resize_mode_changed) {
Albert J. Wong7bbf22d2018-12-20 00:27:27735 delegate()->DisableAutoResizeForWidget();
danakja2c9d0a92018-07-25 20:01:18736 if (params.new_size.IsEmpty())
737 return;
738 }
739
740 browser_controls_shrink_blink_size_ =
741 params.browser_controls_shrink_blink_size;
742 top_controls_height_ = params.top_controls_height;
743 bottom_controls_height_ = params.bottom_controls_height;
744
745 // TODO(fsamuel): Modifies the |params| that are used for
746 // SynchronizeVisualProperties(), but does not used this modified
747 // device_scale_factor for the auto-resize values, which is probably a bug.
748 if (device_scale_factor_for_testing_) {
749 params.screen_info.device_scale_factor =
750 *device_scale_factor_for_testing_;
751 params.compositor_viewport_pixel_size = gfx::ScaleToCeiledSize(
752 params.new_size, params.screen_info.device_scale_factor);
753 }
[email protected]b2e4c70132013-10-03 02:07:51754 }
755
danakja2c9d0a92018-07-25 20:01:18756 if (!resizing_mode_selector_->ShouldAbortOnResize(this, params)) {
757 if (screen_metrics_emulator_) {
758 screen_metrics_emulator_->OnSynchronizeVisualProperties(params);
759 } else {
760 gfx::Size old_visible_viewport_size = visible_viewport_size_;
761 SynchronizeVisualProperties(params);
762 if (old_visible_viewport_size != visible_viewport_size_) {
763 for (auto& render_frame : render_frames_)
764 render_frame.DidChangeVisibleViewport();
765 }
766 }
Fady Samuel799e72192018-04-25 21:16:57767 }
768
Albert J. Wong7bbf22d2018-12-20 00:27:27769 if (delegate() && params.scroll_focused_node_into_view)
770 delegate()->ScrollFocusedNodeIntoViewForWidget();
initial.commit09911bf2008-07-26 23:55:29771}
772
dgozman9260b0a12015-03-16 13:45:20773void RenderWidget::OnEnableDeviceEmulation(
Lei Zhang9b359d32017-11-28 00:57:02774 const blink::WebDeviceEmulationParams& params) {
mfomitchev2600fd7c2016-02-17 20:53:39775 if (!screen_metrics_emulator_) {
Fady Samuel4255c182018-05-24 20:42:36776 VisualProperties visual_properties;
777 visual_properties.screen_info = screen_info_;
778 visual_properties.new_size = size_;
779 visual_properties.compositor_viewport_pixel_size =
Christopher Cameronaad15a0d2018-03-09 03:47:33780 compositor_viewport_pixel_size_;
Fady Samuel84d8a1f72018-11-08 00:26:17781 visual_properties.local_surface_id_allocation =
782 local_surface_id_allocation_from_parent_;
Fady Samuel4255c182018-05-24 20:42:36783 visual_properties.visible_viewport_size = visible_viewport_size_;
784 visual_properties.is_fullscreen_granted = is_fullscreen_granted_;
785 visual_properties.display_mode = display_mode_;
W. James MacLean2a90bff2018-11-05 20:52:47786 visual_properties.page_scale_factor = page_scale_factor_from_mainframe_;
mfomitchev2600fd7c2016-02-17 20:53:39787 screen_metrics_emulator_.reset(new RenderWidgetScreenMetricsEmulator(
Albert J. Wong3c93c182018-09-27 17:29:43788 this, params, visual_properties, widget_screen_rect_,
Fady Samuel4255c182018-05-24 20:42:36789 window_screen_rect_));
oshima50872a72016-03-04 13:26:18790 screen_metrics_emulator_->Apply();
mfomitchev2600fd7c2016-02-17 20:53:39791 } else {
dgozman9260b0a12015-03-16 13:45:20792 screen_metrics_emulator_->ChangeEmulationParams(params);
mfomitchev2600fd7c2016-02-17 20:53:39793 }
dgozman9260b0a12015-03-16 13:45:20794}
795
796void RenderWidget::OnDisableDeviceEmulation() {
797 screen_metrics_emulator_.reset();
798}
799
initial.commit09911bf2008-07-26 23:55:29800void RenderWidget::OnWasHidden() {
[email protected]9c3085f2011-06-09 02:10:31801 TRACE_EVENT0("renderer", "RenderWidget::OnWasHidden");
initial.commit09911bf2008-07-26 23:55:29802 // Go into a mode where we stop generating paint and scrolling events.
[email protected]bee16aab2009-08-26 15:55:03803 SetHidden(true);
ericwilligers88e69742016-10-17 19:29:55804 for (auto& observer : render_frames_)
805 observer.WasHidden();
initial.commit09911bf2008-07-26 23:55:29806}
807
Saman Samie61e5c032018-11-05 22:11:44808void RenderWidget::OnWasShown(base::TimeTicks show_request_timestamp,
809 bool was_evicted) {
[email protected]9e2e4632012-07-27 16:38:41810 TRACE_EVENT0("renderer", "RenderWidget::OnWasShown");
danakj57f139f2018-11-19 19:23:46811 // TODO(danakj): Nothing should happen ideally if the RenderWidget is frozen!
812 // It's not visible! However.. the RenderView needs to see it as visible in
813 // order to make the Page visible /o\ so this is hard. We need to detangle
814 // page visibility from the main widget. https://crbug.com/419087
danakj678f0252018-11-09 21:46:34815
tasakb95dbb50c2017-02-08 18:07:50816 was_shown_time_ = base::TimeTicks::Now();
initial.commit09911bf2008-07-26 23:55:29817 // See OnWasHidden
[email protected]bee16aab2009-08-26 15:55:03818 SetHidden(false);
ericwilligers88e69742016-10-17 19:29:55819 for (auto& observer : render_frames_)
820 observer.WasShown();
initial.commit09911bf2008-07-26 23:55:29821
Saman Samie61e5c032018-11-05 22:11:44822 if (was_evicted) {
823 for (auto& observer : render_frame_proxies_) {
824 observer.WasEvicted();
825 }
826 }
827
danakja6c10012018-07-06 14:25:36828 if (layer_tree_view_ && !show_request_timestamp.is_null()) {
829 layer_tree_view_->layer_tree_host()->RequestPresentationTimeForNextFrame(
Sadrul Habib Chowdhury8bc43cc2018-06-06 01:48:03830 CreateTabSwitchingTimeRecorder(show_request_timestamp));
[email protected]3399dd822014-08-09 11:14:24831 }
initial.commit09911bf2008-07-26 23:55:29832}
833
Avi Drissman014dec72018-06-07 02:34:01834void RenderWidget::OnRequestSetBoundsAck() {
[email protected]53d3f302009-12-21 04:42:05835 DCHECK(pending_window_rect_count_);
836 pending_window_rect_count_--;
837}
838
Albert J. Wongcb004632018-07-10 22:58:25839void RenderWidget::OnForceRedraw(int snapshot_id) {
840 if (LayerTreeView* ltv = layer_tree_view()) {
841 ltv->layer_tree_host()->RequestPresentationTimeForNextFrame(
842 base::BindOnce(&RenderWidget::DidPresentForceDrawFrame,
843 weak_ptr_factory_.GetWeakPtr(), snapshot_id));
844 ltv->SetNeedsForcedRedraw();
845 }
846}
847
848void RenderWidget::DidPresentForceDrawFrame(
849 int snapshot_id,
850 const gfx::PresentationFeedback& feedback) {
Albert J. Wong3c93c182018-09-27 17:29:43851 Send(new WidgetHostMsg_ForceRedrawComplete(routing_id(), snapshot_id));
Albert J. Wongcb004632018-07-10 22:58:25852}
853
Xianzhu Wangf02017ac2018-10-17 01:47:33854viz::FrameSinkId RenderWidget::GetFrameSinkIdAtPoint(const gfx::PointF& point,
Ken Buchanan44d7e2f2018-08-23 14:18:05855 gfx::PointF* local_point) {
856 return input_handler_->GetFrameSinkIdAtPoint(point, local_point);
Navid Zolghadr0d86e5f2017-10-23 18:09:22857}
858
Dave Tapuskaea83d3bd2017-06-13 16:14:55859void RenderWidget::HandleInputEvent(
dtapuska9ec1a912017-04-21 15:18:31860 const blink::WebCoalescedInputEvent& input_event,
861 const ui::LatencyInfo& latency_info,
Dave Tapuskaea83d3bd2017-06-13 16:14:55862 HandledEventCallback callback) {
Albert J. Wong7bbf22d2018-12-20 00:27:27863 if (delegate() && is_frozen_) {
danakja2c9d0a92018-07-25 20:01:18864 std::move(callback).Run(INPUT_EVENT_ACK_STATE_NOT_CONSUMED, latency_info,
865 nullptr, base::nullopt);
866 return;
867 }
Dave Tapuskaea83d3bd2017-06-13 16:14:55868 input_handler_->HandleInputEvent(input_event, latency_info,
869 std::move(callback));
dtapuska9ec1a912017-04-21 15:18:31870}
871
dtapuska9d46ef7d2017-05-26 19:06:06872scoped_refptr<MainThreadEventQueue> RenderWidget::GetInputEventQueue() {
873 return input_event_queue_;
874}
875
fsamuel78f86e42016-01-20 04:10:23876void RenderWidget::OnCursorVisibilityChange(bool is_visible) {
lfg8ff33912016-09-13 20:59:21877 if (GetWebWidget())
Blink Reformat1c4d759e2017-04-09 16:34:54878 GetWebWidget()->SetCursorVisibilityState(is_visible);
fsamuel78f86e42016-01-20 04:10:23879}
880
881void RenderWidget::OnMouseCaptureLost() {
lfg8ff33912016-09-13 20:59:21882 if (GetWebWidget())
Blink Reformat1c4d759e2017-04-09 16:34:54883 GetWebWidget()->MouseCaptureLost();
fsamuel78f86e42016-01-20 04:10:23884}
885
alexmos56567492016-09-13 00:52:46886void RenderWidget::OnSetEditCommandsForNextKeyEvent(
887 const EditCommands& edit_commands) {
888 edit_commands_ = edit_commands;
889}
890
Albert J. Wongcb004632018-07-10 22:58:25891void RenderWidget::OnSetActive(bool active) {
Albert J. Wong7bbf22d2018-12-20 00:27:27892 if (delegate())
893 delegate()->SetActiveForWidget(active);
Albert J. Wongcb004632018-07-10 22:58:25894}
895
896void RenderWidget::OnSetBackgroundOpaque(bool opaque) {
danakj89ae4532018-12-12 22:52:55897 // This IPC never sent when frozen.
898 DCHECK(!is_frozen_);
danakj0ba0e7b2018-09-25 19:37:13899 // Background opaque-ness modification is only supported for the main frame.
Albert J. Wong7bbf22d2018-12-20 00:27:27900 // The delegate() is used as proxy for this RenderWidget being attached
danakj0ba0e7b2018-09-25 19:37:13901 // to the main frame.
Albert J. Wong7bbf22d2018-12-20 00:27:27902 if (!delegate())
danakj0ba0e7b2018-09-25 19:37:13903 return;
904
danakj89ae4532018-12-12 22:52:55905 blink::WebFrameWidget* web_frame_widget = GetFrameWidget();
danakj0ba0e7b2018-09-25 19:37:13906 if (opaque) {
907 web_frame_widget->ClearBaseBackgroundColorOverride();
908 web_frame_widget->ClearBackgroundColorOverride();
909 } else {
910 web_frame_widget->SetBaseBackgroundColorOverride(SK_ColorTRANSPARENT);
911 web_frame_widget->SetBackgroundColorOverride(SK_ColorTRANSPARENT);
912 }
Albert J. Wongcb004632018-07-10 22:58:25913}
914
fsamuel78f86e42016-01-20 04:10:23915void RenderWidget::OnSetFocus(bool enable) {
Albert J. Wong7bbf22d2018-12-20 00:27:27916 if (delegate())
917 delegate()->DidReceiveSetFocusEventForWidget();
danakja2c9d0a92018-07-25 20:01:18918 SetFocus(enable);
919}
920
921void RenderWidget::SetFocus(bool enable) {
lfge0c2792ec2016-05-11 18:52:08922 has_focus_ = enable;
923
lfg8ff33912016-09-13 20:59:21924 if (GetWebWidget())
Blink Reformat1c4d759e2017-04-09 16:34:54925 GetWebWidget()->SetFocus(enable);
lfge0c2792ec2016-05-11 18:52:08926
ericwilligers88e69742016-10-17 19:29:55927 for (auto& observer : render_frames_)
928 observer.RenderWidgetSetFocus(enable);
danakja2c9d0a92018-07-25 20:01:18929
Albert J. Wong7bbf22d2018-12-20 00:27:27930 if (delegate())
931 delegate()->DidChangeFocusForWidget();
fsamuel78f86e42016-01-20 04:10:23932}
933
dtapuska3d5624d32016-08-30 04:34:00934void RenderWidget::SetNeedsMainFrame() {
danakj53f46b82018-12-11 20:55:55935 // The WebWidgetClient is not |this| if tests override it for the WebView and
936 // WebViewClient.
937 blink::WebWidgetClient* client =
Albert J. Wong7bbf22d2018-12-20 00:27:27938 delegate() ? delegate()->GetWebWidgetClientForWidget() : this;
danakj53f46b82018-12-11 20:55:55939 client->ScheduleAnimation();
dtapuska3d5624d32016-08-30 04:34:00940}
941
fsamuel78f86e42016-01-20 04:10:23942///////////////////////////////////////////////////////////////////////////////
danakja6c10012018-07-06 14:25:36943// LayerTreeViewDelegate
fsamuel78f86e42016-01-20 04:10:23944
David Bokanc8e38d02018-10-08 21:56:01945void RenderWidget::ApplyViewportChanges(
946 const cc::ApplyViewportChangesArgs& args) {
Ken Buchananed449bb2018-02-01 21:02:05947 if (!GetWebWidget())
948 return;
David Bokanc8e38d02018-10-08 21:56:01949 GetWebWidget()->ApplyViewportChanges(args);
fsamuel78f86e42016-01-20 04:10:23950}
951
sahel1b47fda72017-03-28 17:03:07952void RenderWidget::RecordWheelAndTouchScrollingCount(
953 bool has_scrolled_by_wheel,
954 bool has_scrolled_by_touch) {
Ken Buchananed449bb2018-02-01 21:02:05955 if (!GetWebWidget())
956 return;
Blink Reformat1c4d759e2017-04-09 16:34:54957 GetWebWidget()->RecordWheelAndTouchScrollingCount(has_scrolled_by_wheel,
sahel1b47fda72017-03-28 17:03:07958 has_scrolled_by_touch);
959}
960
Daniel Cheng224569ee2018-04-25 05:45:06961void RenderWidget::BeginMainFrame(base::TimeTicks frame_time) {
Ken Buchananed449bb2018-02-01 21:02:05962 if (!GetWebWidget())
963 return;
Daniel Cheng224569ee2018-04-25 05:45:06964 if (input_event_queue_)
965 input_event_queue_->DispatchRafAlignedInput(frame_time);
dtapuska3d5624d32016-08-30 04:34:00966
Daniel Cheng224569ee2018-04-25 05:45:06967 GetWebWidget()->BeginFrame(frame_time);
fsamuel78f86e42016-01-20 04:10:23968}
969
danakjc7afae52017-06-20 21:12:41970void RenderWidget::RequestNewLayerTreeFrameSink(
danakja6c10012018-07-06 14:25:36971 LayerTreeFrameSinkCallback callback) {
[email protected]7912e822014-04-16 02:37:03972 // For widgets that are never visible, we don't start the compositor, so we
danakjc7afae52017-06-20 21:12:41973 // never get a request for a cc::LayerTreeFrameSink.
sievers71c62dd52015-10-07 01:44:39974 DCHECK(!compositor_never_visible_);
danakjf87460792018-12-04 20:14:22975 // Frozen RenderWidgets should not be doing any compositing.
Scott Violetb04f16c2018-11-27 00:48:46976 DCHECK(!is_frozen_);
danakj678f0252018-11-09 21:46:34977
978 if (is_closing()) {
979 // In this case, we drop the request which means the compositor waits
980 // forever, which is fine since we're going to destroy it.
981 return;
982 }
jonrossa2ff4f82018-02-16 17:27:46983
danakjf87460792018-12-04 20:14:22984 // If we have a warmup in progress, wait for that and store the callback
985 // to be run when the warmup completes.
986 if (warmup_frame_sink_request_pending_) {
987 after_warmup_callback_ = std::move(callback);
988 return;
989 }
990 // If a warmup previously completed, use the result.
991 if (warmup_frame_sink_) {
992 std::move(callback).Run(std::move(warmup_frame_sink_));
993 return;
994 }
995
996 DoRequestNewLayerTreeFrameSink(std::move(callback));
997}
998
999void RenderWidget::DoRequestNewLayerTreeFrameSink(
1000 LayerTreeFrameSinkCallback callback) {
jonrossa2ff4f82018-02-16 17:27:461001 // TODO(jonross): have this generated by the LayerTreeFrameSink itself, which
1002 // would then handle binding.
1003 mojom::RenderFrameMetadataObserverPtr ptr;
1004 mojom::RenderFrameMetadataObserverRequest request = mojo::MakeRequest(&ptr);
1005 mojom::RenderFrameMetadataObserverClientPtrInfo client_info;
1006 mojom::RenderFrameMetadataObserverClientRequest client_request =
1007 mojo::MakeRequest(&client_info);
danakj4b347212018-07-04 17:55:171008 auto render_frame_metadata_observer =
1009 std::make_unique<RenderFrameMetadataObserverImpl>(std::move(request),
1010 std::move(client_info));
danakja6c10012018-07-06 14:25:361011 layer_tree_view_->SetRenderFrameObserver(
danakj4b347212018-07-04 17:55:171012 std::move(render_frame_metadata_observer));
danakjf51754372018-12-13 23:20:011013 GURL url = GetWebWidget()->GetURLForDebugTrace();
danakjefabf312018-10-05 19:03:071014 // The |url| is not always available, fallback to a fixed string.
1015 if (url.is_empty())
1016 url = GURL("chrome://gpu/RenderWidget::RequestNewLayerTreeFrameSink");
danakj6dcbc5962018-11-16 16:45:421017 // TODO(danakj): This may not be accurate, depending on the intent. A child
1018 // local root could be in the same process as the view, so if the client is
1019 // meant to designate the process type, it seems kRenderer would be the
1020 // correct choice. If client is meant to designate the widget type, then
1021 // kOOPIF would denote that it is not for the main frame. However, kRenderer
1022 // would also be used for other widgets such as popups.
1023 const char* client_name = for_child_local_root_frame_ ? kOOPIF : kRenderer;
Scott Violetb04f16c2018-11-27 00:48:461024 RenderThreadImpl::current()->RequestNewLayerTreeFrameSink(
1025 routing_id_, frame_swap_message_queue_, std::move(url),
1026 std::move(callback), std::move(client_request), std::move(ptr),
1027 client_name);
[email protected]ba91a792013-02-06 09:48:281028}
1029
fsamuel78f86e42016-01-20 04:10:231030void RenderWidget::DidCommitAndDrawCompositorFrame() {
1031 // NOTE: Tests may break if this event is renamed or moved. See
1032 // tab_capture_performancetest.cc.
1033 TRACE_EVENT0("gpu", "RenderWidget::DidCommitAndDrawCompositorFrame");
lfge0c2792ec2016-05-11 18:52:081034
ericwilligers88e69742016-10-17 19:29:551035 for (auto& observer : render_frames_)
1036 observer.DidCommitAndDrawCompositorFrame();
lfge0c2792ec2016-05-11 18:52:081037
fsamuel78f86e42016-01-20 04:10:231038 // Notify subclasses that we initiated the paint operation.
1039 DidInitiatePaint();
Yiming Zhou0f8836c22018-09-13 22:22:111040
Albert J. Wongfb64e142018-10-16 01:10:451041 Send(new WidgetHostMsg_DidCommitAndDrawCompositorFrame(routing_id_));
fsamuel78f86e42016-01-20 04:10:231042}
1043
Saman Sami766e0b02018-01-31 17:19:201044void RenderWidget::DidCommitCompositorFrame() {
Albert J. Wong7bbf22d2018-12-20 00:27:271045 if (delegate())
1046 delegate()->DidCommitCompositorFrameForWidget();
Saman Sami766e0b02018-01-31 17:19:201047}
fsamuel78f86e42016-01-20 04:10:231048
danakja2c9d0a92018-07-25 20:01:181049void RenderWidget::DidCompletePageScaleAnimation() {
Albert J. Wong7bbf22d2018-12-20 00:27:271050 if (delegate())
1051 delegate()->DidCompletePageScaleAnimationForWidget();
danakja2c9d0a92018-07-25 20:01:181052}
fsamuel78f86e42016-01-20 04:10:231053
danakj53f46b82018-12-11 20:55:551054void RenderWidget::ScheduleAnimation() {
1055 // This call is not needed in single thread mode for tests without a
1056 // scheduler, but they need to override the WebWidgetClient and replace this
1057 // method in order to schedule a synchronous composite task themselves.
1058 layer_tree_view_->SetNeedsBeginFrame();
initial.commit09911bf2008-07-26 23:55:291059}
1060
Stephen Chenneyec4312612018-11-21 12:40:161061void RenderWidget::UpdateVisualState(bool record_main_frame_metrics) {
Ken Buchananed449bb2018-02-01 21:02:051062 if (!GetWebWidget())
1063 return;
1064
Stephen Chenneyec4312612018-11-21 12:40:161065 // When recording main frame metrics set the lifecycle reason to
1066 // kBeginMainFrame, because this is the calller of UpdateLifecycle
1067 // for the main frame. Otherwise, set the reason to kTests, which is
1068 // the oinly other reason this method is called.
1069 WebWidget::LifecycleUpdateReason lifecycle_reason =
1070 record_main_frame_metrics
1071 ? WebWidget::LifecycleUpdateReason::kBeginMainFrame
1072 : WebWidget::LifecycleUpdateReason::kTest;
1073 GetWebWidget()->UpdateLifecycle(WebWidget::LifecycleUpdate::kAll,
1074 lifecycle_reason);
Blink Reformat1c4d759e2017-04-09 16:34:541075 GetWebWidget()->SetSuppressFrameRequestsWorkaroundFor704763Only(false);
tasakb95dbb50c2017-02-08 18:07:501076
Robert Flack3ef2ffb2018-09-06 20:38:201077 if (first_update_visual_state_after_hidden_) {
Takashi SAKAMOTO870f6262017-08-22 04:08:271078 RecordTimeToFirstActivePaint();
1079 first_update_visual_state_after_hidden_ = false;
1080 }
1081}
tasakb95dbb50c2017-02-08 18:07:501082
Takashi SAKAMOTO870f6262017-08-22 04:08:271083void RenderWidget::RecordTimeToFirstActivePaint() {
tasakb95dbb50c2017-02-08 18:07:501084 RenderThreadImpl* render_thread_impl = RenderThreadImpl::current();
tasakb95dbb50c2017-02-08 18:07:501085 base::TimeDelta sample = base::TimeTicks::Now() - was_shown_time_;
Takashi SAKAMOTO870f6262017-08-22 04:08:271086 if (render_thread_impl->NeedsToRecordFirstActivePaint(TTFAP_AFTER_PURGED)) {
1087 UMA_HISTOGRAM_TIMES("PurgeAndSuspend.Experimental.TimeToFirstActivePaint",
1088 sample);
1089 }
1090 if (render_thread_impl->NeedsToRecordFirstActivePaint(
1091 TTFAP_5MIN_AFTER_BACKGROUNDED)) {
1092 UMA_HISTOGRAM_TIMES(
1093 "PurgeAndSuspend.Experimental.TimeToFirstActivePaint."
1094 "AfterBackgrounded.5min",
1095 sample);
1096 }
fsamuel78f86e42016-01-20 04:10:231097}
1098
Stephen Chenney9879a5c2018-09-26 14:21:421099void RenderWidget::RecordEndOfFrameMetrics(base::TimeTicks frame_begin_time) {
1100 if (!GetWebWidget())
1101 return;
1102
1103 GetWebWidget()->RecordEndOfFrameMetrics(frame_begin_time);
1104}
1105
fsamuel78f86e42016-01-20 04:10:231106void RenderWidget::WillBeginCompositorFrame() {
1107 TRACE_EVENT0("gpu", "RenderWidget::willBeginCompositorFrame");
1108
Ken Buchananed449bb2018-02-01 21:02:051109 if (!GetWebWidget())
1110 return;
1111
Blink Reformat1c4d759e2017-04-09 16:34:541112 GetWebWidget()->SetSuppressFrameRequestsWorkaroundFor704763Only(true);
alancutter317a8e02017-04-05 10:09:291113
dglazkov06854c5c2016-08-31 00:19:591114 // The UpdateTextInputState can result in further layout and possibly
1115 // enable GPU acceleration so they need to be called before any painting
1116 // is done.
changwan75e3b2072017-01-16 02:55:001117 UpdateTextInputState();
dglazkov06854c5c2016-08-31 00:19:591118 UpdateSelectionBounds();
1119
ericwilligers88e69742016-10-17 19:29:551120 for (auto& observer : render_frame_proxies_)
1121 observer.WillBeginCompositorFrame();
initial.commit09911bf2008-07-26 23:55:291122}
1123
Kent Tamura21d1de62018-12-10 04:45:201124std::unique_ptr<cc::SwapPromise> RenderWidget::RequestCopyOfOutputForWebTest(
Fady Samueldfecb7d2017-07-26 11:41:041125 std::unique_ptr<viz::CopyOutputRequest> request) {
Kent Tamura21d1de62018-12-10 04:45:201126 return RenderThreadImpl::current()->RequestCopyOfOutputForWebTest(
jbroman6ccbc7d472016-07-27 04:45:411127 routing_id_, std::move(request));
1128}
1129
fsamuel72464894f2015-12-15 06:59:311130///////////////////////////////////////////////////////////////////////////////
1131// RenderWidgetInputHandlerDelegate
1132
avid7d6b2e2016-03-04 19:41:171133void RenderWidget::FocusChangeComplete() {
Ehsan Karamad655d7b8a2018-01-12 18:38:381134 blink::WebFrameWidget* frame_widget = GetFrameWidget();
1135 if (!frame_widget)
ekaramad27ca69b12017-04-20 18:34:291136 return;
Ehsan Karamad655d7b8a2018-01-12 18:38:381137
ekaramad27ca69b12017-04-20 18:34:291138 blink::WebLocalFrame* focused =
Ehsan Karamad655d7b8a2018-01-12 18:38:381139 frame_widget->LocalRoot()->View()->FocusedFrame();
1140
ekaramad27ca69b12017-04-20 18:34:291141 if (focused && focused->AutofillClient())
1142 focused->AutofillClient()->DidCompleteFocusChangeInFrame();
avid7d6b2e2016-03-04 19:41:171143}
fsamuel72464894f2015-12-15 06:59:311144
dtapuska1827dd22016-03-11 15:24:591145void RenderWidget::ObserveGestureEventAndResult(
1146 const blink::WebGestureEvent& gesture_event,
1147 const gfx::Vector2dF& unused_delta,
Sandra Sunac5cdd832017-12-11 03:27:131148 const cc::OverscrollBehavior& overscroll_behavior,
dtapuska1827dd22016-03-11 15:24:591149 bool event_processed) {
1150 if (!compositor_deps_->IsElasticOverscrollEnabled())
1151 return;
1152
1153 cc::InputHandlerScrollResult scroll_result;
1154 scroll_result.did_scroll = event_processed;
1155 scroll_result.did_overscroll_root = !unused_delta.IsZero();
1156 scroll_result.unused_scroll_delta = unused_delta;
Sandra Sunac5cdd832017-12-11 03:27:131157 scroll_result.overscroll_behavior = overscroll_behavior;
dtapuska1827dd22016-03-11 15:24:591158
Dave Tapuska04bc5ee92018-04-17 19:03:311159 widget_input_handler_manager_->ObserveGestureEventOnMainThread(gesture_event,
1160 scroll_result);
dtapuska1827dd22016-03-11 15:24:591161}
1162
avid7d6b2e2016-03-04 19:41:171163void RenderWidget::OnDidHandleKeyEvent() {
alexmos56567492016-09-13 00:52:461164 ClearEditCommands();
1165}
1166
1167void RenderWidget::SetEditCommandForNextKeyEvent(const std::string& name,
1168 const std::string& value) {
1169 ClearEditCommands();
1170 edit_commands_.emplace_back(name, value);
1171}
1172
1173void RenderWidget::ClearEditCommands() {
1174 edit_commands_.clear();
avid7d6b2e2016-03-04 19:41:171175}
fsamuel72464894f2015-12-15 06:59:311176
chongza8ba91fc2016-08-16 21:39:171177void RenderWidget::OnDidOverscroll(const ui::DidOverscrollParams& params) {
Dave Tapuska04bc5ee92018-04-17 19:03:311178 if (mojom::WidgetInputHandlerHost* host =
1179 widget_input_handler_manager_->GetWidgetInputHandlerHost()) {
1180 host->DidOverscroll(params);
Dave Tapuska139a72f2017-09-06 21:57:031181 }
fsamuel72464894f2015-12-15 06:59:311182}
1183
fsamuele8326c742016-01-12 00:49:391184void RenderWidget::SetInputHandler(RenderWidgetInputHandler* input_handler) {
1185 // Nothing to do here. RenderWidget created the |input_handler| and will take
1186 // ownership of it. We just verify here that we don't already have an input
1187 // handler.
1188 DCHECK(!input_handler_);
1189}
1190
changwan75e3b2072017-01-16 02:55:001191void RenderWidget::ShowVirtualKeyboard() {
1192 UpdateTextInputStateInternal(true, false);
1193}
1194
changwan62f57292017-02-17 08:28:251195void RenderWidget::ClearTextInputState() {
1196 text_input_info_ = blink::WebTextInputInfo();
1197 text_input_type_ = ui::TextInputType::TEXT_INPUT_TYPE_NONE;
1198 text_input_mode_ = ui::TextInputMode::TEXT_INPUT_MODE_DEFAULT;
1199 can_compose_inline_ = false;
1200 text_input_flags_ = 0;
AJITH KUMAR V041c0b02017-08-08 10:39:201201 next_previous_flags_ = kInvalidNextPreviousFlagsValue;
changwan62f57292017-02-17 08:28:251202}
1203
changwan75e3b2072017-01-16 02:55:001204void RenderWidget::UpdateTextInputState() {
1205 UpdateTextInputStateInternal(false, false);
1206}
1207
1208void RenderWidget::UpdateTextInputStateInternal(bool show_virtual_keyboard,
1209 bool reply_to_request) {
fsamuel72464894f2015-12-15 06:59:311210 TRACE_EVENT0("renderer", "RenderWidget::UpdateTextInputState");
changwan75e3b2072017-01-16 02:55:001211
fsamuel72464894f2015-12-15 06:59:311212 if (ime_event_guard_) {
changwan75e3b2072017-01-16 02:55:001213 DCHECK(!reply_to_request);
1214 // show_virtual_keyboard should still be effective even if it was set inside
1215 // the IME
fsamuel72464894f2015-12-15 06:59:311216 // event guard.
changwan75e3b2072017-01-16 02:55:001217 if (show_virtual_keyboard)
1218 ime_event_guard_->set_show_virtual_keyboard(true);
fsamuel72464894f2015-12-15 06:59:311219 return;
1220 }
1221
dglazkov97b6c2b2016-10-25 17:35:551222 ui::TextInputType new_type = GetTextInputType();
1223 if (IsDateTimeInput(new_type))
1224 return; // Not considered as a text input field in WebKit/Chromium.
fsamuel72464894f2015-12-15 06:59:311225
dglazkov97b6c2b2016-10-25 17:35:551226 blink::WebTextInputInfo new_info;
ekaramad5aff1942017-01-06 01:26:351227 if (auto* controller = GetInputMethodController())
Blink Reformat1c4d759e2017-04-09 16:34:541228 new_info = controller->TextInputInfo();
dtapuskac4dd5be2016-10-25 15:11:101229 const ui::TextInputMode new_mode =
Blink Reformat1c4d759e2017-04-09 16:34:541230 ConvertWebTextInputMode(new_info.input_mode);
fsamuel72464894f2015-12-15 06:59:311231
1232 bool new_can_compose_inline = CanComposeInline();
1233
1234 // Only sends text input params if they are changed or if the ime should be
1235 // shown.
changwan75e3b2072017-01-16 02:55:001236 if (show_virtual_keyboard || reply_to_request ||
changwan38c3eb612016-12-09 01:45:561237 text_input_type_ != new_type || text_input_mode_ != new_mode ||
1238 text_input_info_ != new_info ||
1239 can_compose_inline_ != new_can_compose_inline) {
ekaramad9053e57b2016-04-26 20:00:381240 TextInputState params;
dglazkov97b6c2b2016-10-25 17:35:551241 params.type = new_type;
fsamuel72464894f2015-12-15 06:59:311242 params.mode = new_mode;
1243 params.flags = new_info.flags;
AJITH KUMAR V9e7b5572017-11-14 00:54:211244#if defined(OS_ANDROID)
AJITH KUMAR V041c0b02017-08-08 10:39:201245 if (next_previous_flags_ == kInvalidNextPreviousFlagsValue) {
1246 // Due to a focus change, values will be reset by the frame.
1247 // That case we only need fresh NEXT/PREVIOUS information.
Albert J. Wong3c93c182018-09-27 17:29:431248 // Also we won't send WidgetHostMsg_TextInputStateChanged if next/previous
AJITH KUMAR V041c0b02017-08-08 10:39:201249 // focusable status is changed.
1250 if (auto* controller = GetInputMethodController()) {
1251 next_previous_flags_ =
1252 controller->ComputeWebTextInputNextPreviousFlags();
1253 } else {
1254 // For safety in case GetInputMethodController() is null, because -1 is
1255 // invalid value to send to browser process.
1256 next_previous_flags_ = 0;
1257 }
1258 }
AJITH KUMAR V9e7b5572017-11-14 00:54:211259#else
1260 next_previous_flags_ = 0;
1261#endif
AJITH KUMAR V041c0b02017-08-08 10:39:201262 params.flags |= next_previous_flags_;
Blink Reformat1c4d759e2017-04-09 16:34:541263 params.value = new_info.value.Utf8();
1264 params.selection_start = new_info.selection_start;
1265 params.selection_end = new_info.selection_end;
1266 params.composition_start = new_info.composition_start;
1267 params.composition_end = new_info.composition_end;
fsamuel72464894f2015-12-15 06:59:311268 params.can_compose_inline = new_can_compose_inline;
changwan75e3b2072017-01-16 02:55:001269 // TODO(changwan): change instances of show_ime_if_needed to
1270 // show_virtual_keyboard.
1271 params.show_ime_if_needed = show_virtual_keyboard;
1272 params.reply_to_request = reply_to_request;
Albert J. Wong3c93c182018-09-27 17:29:431273 Send(new WidgetHostMsg_TextInputStateChanged(routing_id(), params));
fsamuel72464894f2015-12-15 06:59:311274
1275 text_input_info_ = new_info;
dglazkov97b6c2b2016-10-25 17:35:551276 text_input_type_ = new_type;
fsamuel72464894f2015-12-15 06:59:311277 text_input_mode_ = new_mode;
1278 can_compose_inline_ = new_can_compose_inline;
1279 text_input_flags_ = new_info.flags;
fsamuel72464894f2015-12-15 06:59:311280 }
1281}
1282
1283bool RenderWidget::WillHandleGestureEvent(const blink::WebGestureEvent& event) {
paulmeyer6ef5a792016-11-08 20:33:581284 possible_drag_event_info_.event_source =
1285 ui::DragDropTypes::DRAG_EVENT_SOURCE_TOUCH;
1286 possible_drag_event_info_.event_location =
Ella Ge1116059d2018-03-21 02:06:131287 gfx::ToFlooredPoint(event.PositionInScreen());
avid7d6b2e2016-03-04 19:41:171288
fsamuel72464894f2015-12-15 06:59:311289 return false;
1290}
1291
1292bool RenderWidget::WillHandleMouseEvent(const blink::WebMouseEvent& event) {
ericwilligers88e69742016-10-17 19:29:551293 for (auto& observer : render_frames_)
1294 observer.RenderWidgetWillHandleMouseEvent();
lfge0c2792ec2016-05-11 18:52:081295
paulmeyer6ef5a792016-11-08 20:33:581296 possible_drag_event_info_.event_source =
1297 ui::DragDropTypes::DRAG_EVENT_SOURCE_MOUSE;
1298 possible_drag_event_info_.event_location =
Blink Reformat1c4d759e2017-04-09 16:34:541299 gfx::Point(event.PositionInScreen().x, event.PositionInScreen().y);
paulmeyer6ef5a792016-11-08 20:33:581300
Albert J. Wong7bbf22d2018-12-20 00:27:271301 if (delegate())
1302 return delegate()->RenderWidgetWillHandleMouseEventForWidget(event);
avid7d6b2e2016-03-04 19:41:171303
fsamuel72464894f2015-12-15 06:59:311304 return false;
[email protected]fd847792013-10-24 17:12:351305}
1306
initial.commit09911bf2008-07-26 23:55:291307///////////////////////////////////////////////////////////////////////////////
mfomitchev2600fd7c2016-02-17 20:53:391308// RenderWidgetScreenMetricsDelegate
1309
1310void RenderWidget::Redraw() {
danakja6c10012018-07-06 14:25:361311 if (layer_tree_view_)
1312 layer_tree_view_->SetNeedsRedrawRect(gfx::Rect(size_));
mfomitchev2600fd7c2016-02-17 20:53:391313}
1314
bokanc63441c2016-04-27 15:49:121315void RenderWidget::ResizeWebWidget() {
danakja2c9d0a92018-07-25 20:01:181316 gfx::Size size = GetSizeForWebWidget();
Albert J. Wong7bbf22d2018-12-20 00:27:271317 if (delegate()) {
1318 delegate()->ResizeWebWidgetForWidget(size, top_controls_height_,
1319 bottom_controls_height_,
1320 browser_controls_shrink_blink_size_);
danakja2c9d0a92018-07-25 20:01:181321 return;
1322 }
1323 GetWebWidget()->Resize(size);
bokanc63441c2016-04-27 15:49:121324}
1325
1326gfx::Size RenderWidget::GetSizeForWebWidget() const {
danakj4b347212018-07-04 17:55:171327 if (compositor_deps_->IsUseZoomForDSFEnabled()) {
Fady Samuela863f152018-03-09 16:10:031328 return gfx::ScaleToCeiledSize(size_,
1329 GetOriginalScreenInfo().device_scale_factor);
1330 }
bokanc63441c2016-04-27 15:49:121331
1332 return size_;
1333}
1334
akaba3483d8f2018-07-10 21:43:091335void RenderWidget::UpdateZoom(double zoom_level) {
akabac6bd1212018-06-25 20:10:481336 blink::WebFrameWidget* frame_widget = GetFrameWidget();
1337 if (!frame_widget)
1338 return;
1339 RenderFrameImpl* render_frame =
1340 RenderFrameImpl::FromWebFrame(frame_widget->LocalRoot());
1341
akaba3483d8f2018-07-10 21:43:091342 // Return early if zoom level is unchanged.
1343 if (render_frame->GetZoomLevel() == zoom_level) {
akabac6bd1212018-06-25 20:10:481344 return;
1345 }
1346
akaba3483d8f2018-07-10 21:43:091347 render_frame->SetZoomLevel(zoom_level);
akabac6bd1212018-06-25 20:10:481348
1349 for (auto& observer : render_frame_proxies_)
akaba3483d8f2018-07-10 21:43:091350 observer.OnZoomLevelChanged(zoom_level);
akabac6bd1212018-06-25 20:10:481351
1352 for (auto& plugin : browser_plugins_)
akaba3483d8f2018-07-10 21:43:091353 plugin.OnZoomLevelChanged(zoom_level);
akabac6bd1212018-06-25 20:10:481354}
1355
Fady Samuel799e72192018-04-25 21:16:571356void RenderWidget::SynchronizeVisualProperties(const VisualProperties& params) {
Christopher Cameronbe7ecea2018-02-27 00:47:191357 // Inform the rendering thread of the color space indicate the presence of HDR
1358 // capabilities.
Miguel Casas5e1018052018-01-09 19:17:361359 RenderThreadImpl* render_thread = RenderThreadImpl::current();
1360 if (render_thread)
Christopher Camerone9a30c12018-03-07 08:23:381361 render_thread->SetRenderingColorSpace(params.screen_info.color_space);
Miguel Casas5e1018052018-01-09 19:17:361362
mfomitchev2600fd7c2016-02-17 20:53:391363 // Ignore this during shutdown.
lfg8ff33912016-09-13 20:59:211364 if (!GetWebWidget())
mfomitchev2600fd7c2016-02-17 20:53:391365 return;
1366
W. James MacLean2a90bff2018-11-05 20:52:471367 // Only propagate the external PSF to non-main-frames.
Albert J. Wong7bbf22d2018-12-20 00:27:271368 if (!delegate())
W. James MacLean2a90bff2018-11-05 20:52:471369 layer_tree_view_->SetExternalPageScaleFactor(params.page_scale_factor);
1370
Fady Samuele62be402018-03-29 03:19:531371 gfx::Size new_compositor_viewport_pixel_size =
1372 params.auto_resize_enabled
1373 ? gfx::ScaleToCeiledSize(size_,
1374 params.screen_info.device_scale_factor)
1375 : params.compositor_viewport_pixel_size;
Fady Samuel84d8a1f72018-11-08 00:26:171376 UpdateSurfaceAndScreenInfo(params.local_surface_id_allocation.value_or(
1377 viz::LocalSurfaceIdAllocation()),
1378 new_compositor_viewport_pixel_size,
1379 params.screen_info);
Vladimir Levin98d76dad2018-04-21 00:21:291380 UpdateCaptureSequenceNumber(params.capture_sequence_number);
danakja6c10012018-07-06 14:25:361381 if (layer_tree_view_) {
1382 layer_tree_view_->SetBrowserControlsHeight(
Sadrul Habib Chowdhury04d5dd4d2017-08-10 01:59:271383 params.top_controls_height, params.bottom_controls_height,
1384 params.browser_controls_shrink_blink_size);
danakja6c10012018-07-06 14:25:361385 layer_tree_view_->SetRasterColorSpace(
Sadrul Habib Chowdhury04d5dd4d2017-08-10 01:59:271386 screen_info_.color_space.GetRasterColorSpace());
ianwene5fc5782016-08-18 04:05:151387 }
mfomitchev2600fd7c2016-02-17 20:53:391388
akaba3483d8f2018-07-10 21:43:091389 UpdateZoom(params.zoom_level);
akabac6bd1212018-06-25 20:10:481390
danakj21604ce2018-12-19 16:01:221391 if (!params.auto_resize_enabled) {
1392 visible_viewport_size_ = params.visible_viewport_size;
Fady Samuele62be402018-03-29 03:19:531393
danakj21604ce2018-12-19 16:01:221394 // NOTE: We may have entered fullscreen mode without changing our size.
1395 bool fullscreen_change =
1396 is_fullscreen_granted_ != params.is_fullscreen_granted;
1397 is_fullscreen_granted_ = params.is_fullscreen_granted;
1398 display_mode_ = params.display_mode;
mfomitchev2600fd7c2016-02-17 20:53:391399
danakj21604ce2018-12-19 16:01:221400 size_ = params.new_size;
mfomitchev2600fd7c2016-02-17 20:53:391401
danakj21604ce2018-12-19 16:01:221402 ResizeWebWidget();
mfomitchev2600fd7c2016-02-17 20:53:391403
danakj21604ce2018-12-19 16:01:221404 WebSize visual_viewport_size;
1405 if (compositor_deps_->IsUseZoomForDSFEnabled()) {
1406 visual_viewport_size =
1407 gfx::ScaleToCeiledSize(params.visible_viewport_size,
1408 GetOriginalScreenInfo().device_scale_factor);
1409 } else {
1410 visual_viewport_size = visible_viewport_size_;
1411 }
1412 GetWebWidget()->ResizeVisualViewport(visual_viewport_size);
bokan71cb5b12016-04-27 03:45:221413
danakj21604ce2018-12-19 16:01:221414 if (fullscreen_change)
1415 DidToggleFullscreen();
mfomitchev2600fd7c2016-02-17 20:53:391416 }
W. James MacLean2a90bff2018-11-05 20:52:471417
Albert J. Wong7bbf22d2018-12-20 00:27:271418 if (!delegate()) {
W. James MacLean2a90bff2018-11-05 20:52:471419 // Make sure that page scale factor changes propagating down from the main
1420 // frame are relayed to nested OOPIFs/non-main-frames.
1421 page_scale_factor_from_mainframe_ = params.page_scale_factor;
1422 for (auto& observer : render_frame_proxies_)
1423 observer.OnPageScaleFactorChanged(params.page_scale_factor);
1424 }
mfomitchev2600fd7c2016-02-17 20:53:391425}
1426
1427void RenderWidget::SetScreenMetricsEmulationParameters(
1428 bool enabled,
1429 const blink::WebDeviceEmulationParams& params) {
Albert J. Wong7bbf22d2018-12-20 00:27:271430 // This is only supported in RenderView, which has an delegate().
1431 DCHECK(delegate());
1432 delegate()->SetScreenMetricsEmulationParametersForWidget(enabled, params);
mfomitchev2600fd7c2016-02-17 20:53:391433}
1434
Albert J. Wong3c93c182018-09-27 17:29:431435void RenderWidget::SetScreenRects(const gfx::Rect& widget_screen_rect,
mfomitchev2600fd7c2016-02-17 20:53:391436 const gfx::Rect& window_screen_rect) {
Albert J. Wong3c93c182018-09-27 17:29:431437 widget_screen_rect_ = widget_screen_rect;
mfomitchev2600fd7c2016-02-17 20:53:391438 window_screen_rect_ = window_screen_rect;
1439}
1440
1441///////////////////////////////////////////////////////////////////////////////
[email protected]f98d7e3c2010-09-13 22:30:461442// WebWidgetClient
initial.commit09911bf2008-07-26 23:55:291443
Chris Harrelsond7ab99b2018-01-24 17:51:361444void RenderWidget::IntrinsicSizingInfoChanged(
1445 const blink::WebIntrinsicSizingInfo& sizing_info) {
Albert J. Wong3c93c182018-09-27 17:29:431446 Send(new WidgetHostMsg_IntrinsicSizingInfoChanged(routing_id_, sizing_info));
Chris Harrelsond7ab99b2018-01-24 17:51:361447}
1448
Blink Reformat1c4d759e2017-04-09 16:34:541449void RenderWidget::DidMeaningfulLayout(blink::WebMeaningfulLayout layout_type) {
1450 if (layout_type == blink::WebMeaningfulLayout::kVisuallyNonEmpty) {
Albert J. Wongfb64e142018-10-16 01:10:451451 QueueMessage(new WidgetHostMsg_DidFirstVisuallyNonEmptyPaint(routing_id_));
dglazkovf0e1d6d2015-10-10 02:13:481452 }
dglazkov79c426102015-08-31 21:22:431453
ericwilligers88e69742016-10-17 19:29:551454 for (auto& observer : render_frames_)
1455 observer.DidMeaningfulLayout(layout_type);
dglazkov79c426102015-08-31 21:22:431456}
1457
[email protected]586871b2014-07-22 17:05:111458// static
dchengcedca5612016-04-09 01:40:151459std::unique_ptr<cc::SwapPromise> RenderWidget::QueueMessageImpl(
[email protected]586871b2014-07-22 17:05:111460 IPC::Message* msg,
[email protected]586871b2014-07-22 17:05:111461 FrameSwapMessageQueue* frame_swap_message_queue,
1462 scoped_refptr<IPC::SyncMessageFilter> sync_message_filter,
[email protected]586871b2014-07-22 17:05:111463 int source_frame_number) {
[email protected]586871b2014-07-22 17:05:111464 bool first_message_for_frame = false;
Sadrul Habib Chowdhury4e0335cf2018-07-31 20:08:491465 frame_swap_message_queue->QueueMessageForFrame(
1466 source_frame_number, base::WrapUnique(msg), &first_message_for_frame);
[email protected]586871b2014-07-22 17:05:111467 if (first_message_for_frame) {
dchengcedca5612016-04-09 01:40:151468 std::unique_ptr<cc::SwapPromise> promise(new QueueMessageSwapPromise(
[email protected]586871b2014-07-22 17:05:111469 sync_message_filter, frame_swap_message_queue, source_frame_number));
dcheng4b6b5ff2014-10-16 00:42:061470 return promise;
[email protected]586871b2014-07-22 17:05:111471 }
dcheng4b6b5ff2014-10-16 00:42:061472 return nullptr;
[email protected]586871b2014-07-22 17:05:111473}
1474
danakj53802692018-07-25 21:46:441475void RenderWidget::SetHandlingInputEvent(bool handling_input_event) {
1476 input_handler_->set_handling_input_event(handling_input_event);
1477}
1478
Sadrul Habib Chowdhury4e0335cf2018-07-31 20:08:491479void RenderWidget::QueueMessage(IPC::Message* msg) {
[email protected]586871b2014-07-22 17:05:111480 // RenderThreadImpl::current() is NULL in some tests.
danakja6c10012018-07-06 14:25:361481 if (!layer_tree_view_ || !RenderThreadImpl::current()) {
[email protected]586871b2014-07-22 17:05:111482 Send(msg);
1483 return;
1484 }
1485
dchengcedca5612016-04-09 01:40:151486 std::unique_ptr<cc::SwapPromise> swap_promise =
Sadrul Habib Chowdhury4e0335cf2018-07-31 20:08:491487 QueueMessageImpl(msg, frame_swap_message_queue_.get(),
[email protected]586871b2014-07-22 17:05:111488 RenderThreadImpl::current()->sync_message_filter(),
danakja6c10012018-07-06 14:25:361489 layer_tree_view_->GetSourceFrameNumber());
[email protected]586871b2014-07-22 17:05:111490
Ken Buchanance710eb2017-08-30 23:46:521491 if (swap_promise)
danakja6c10012018-07-06 14:25:361492 layer_tree_view_->QueueSwapPromise(std::move(swap_promise));
[email protected]586871b2014-07-22 17:05:111493}
1494
Blink Reformat1c4d759e2017-04-09 16:34:541495void RenderWidget::DidChangeCursor(const WebCursorInfo& cursor_info) {
[email protected]7c51b0ee2009-07-08 21:49:301496 // TODO(darin): Eliminate this temporary.
[email protected]9ec87712013-05-24 23:23:521497 WebCursor cursor;
tfarina75a0abf2015-10-06 15:07:181498 InitializeCursorFromWebCursorInfo(&cursor, cursor_info);
initial.commit09911bf2008-07-26 23:55:291499 // Only send a SetCursor message if we need to make a change.
Navid Zolghadr293bb862018-12-12 18:15:411500 if (input_handler_->DidChangeCursor(cursor))
Albert J. Wong3c93c182018-09-27 17:29:431501 Send(new WidgetHostMsg_SetCursor(routing_id_, cursor));
initial.commit09911bf2008-07-26 23:55:291502}
1503
aelias5971e47d2017-06-16 02:39:381504void RenderWidget::AutoscrollStart(const blink::WebFloatPoint& point) {
Albert J. Wong3c93c182018-09-27 17:29:431505 Send(new WidgetHostMsg_AutoscrollStart(routing_id_, point));
aelias5971e47d2017-06-16 02:39:381506}
1507
1508void RenderWidget::AutoscrollFling(const blink::WebFloatSize& velocity) {
Albert J. Wong3c93c182018-09-27 17:29:431509 Send(new WidgetHostMsg_AutoscrollFling(routing_id_, velocity));
aelias5971e47d2017-06-16 02:39:381510}
1511
1512void RenderWidget::AutoscrollEnd() {
Albert J. Wong3c93c182018-09-27 17:29:431513 Send(new WidgetHostMsg_AutoscrollEnd(routing_id_));
aelias5971e47d2017-06-16 02:39:381514}
1515
initial.commit09911bf2008-07-26 23:55:291516// We are supposed to get a single call to Show for a newly created RenderWidget
1517// that was created via RenderWidget::CreateWebView. So, we wait until this
1518// point to dispatch the ShowWidget message.
1519//
1520// This method provides us with the information about how to display the newly
[email protected]5f9de5882011-09-30 23:36:281521// created RenderWidget (i.e., as a blocked popup or as a new tab).
initial.commit09911bf2008-07-26 23:55:291522//
Blink Reformat1c4d759e2017-04-09 16:34:541523void RenderWidget::Show(WebNavigationPolicy policy) {
danakj65f423b72018-10-10 17:21:201524 if (!show_callback_) {
Albert J. Wong7bbf22d2018-12-20 00:27:271525 if (delegate()) {
danakja2c9d0a92018-07-25 20:01:181526 // When SupportsMultipleWindows is disabled, popups are reusing
danakj65f423b72018-10-10 17:21:201527 // the view's RenderWidget. In some scenarios, this makes blink to call
1528 // Show() twice. But otherwise, if it is enabled, we should not visit
1529 // Show() more than once.
Albert J. Wong7bbf22d2018-12-20 00:27:271530 DCHECK(!delegate()->SupportsMultipleWindowsForWidget());
danakj65f423b72018-10-10 17:21:201531 return;
danakja2c9d0a92018-07-25 20:01:181532 } else {
danakj65f423b72018-10-10 17:21:201533 NOTREACHED() << "received extraneous Show call";
danakja2c9d0a92018-07-25 20:01:181534 }
danakja2c9d0a92018-07-25 20:01:181535 }
1536
initial.commit09911bf2008-07-26 23:55:291537 DCHECK(routing_id_ != MSG_ROUTING_NONE);
nickf7b38222016-11-22 21:59:351538
1539 // The opener is responsible for actually showing this widget.
danakjdf1ceb72018-07-18 20:02:251540 std::move(show_callback_).Run(this, policy, initial_rect_);
nickf7b38222016-11-22 21:59:351541
bokanc007c3a2015-02-03 07:15:561542 // NOTE: initial_rect_ may still have its default values at this point, but
[email protected]8de12d942010-11-17 20:42:441543 // that's okay. It'll be ignored if as_popup is false, or the browser
1544 // process will impose a default position otherwise.
bokanc007c3a2015-02-03 07:15:561545 SetPendingWindowRect(initial_rect_);
initial.commit09911bf2008-07-26 23:55:291546}
1547
danakj17216f4d2018-10-16 22:57:321548LayerTreeView* RenderWidget::InitializeLayerTreeView() {
1549 TRACE_EVENT0("blink", "RenderWidget::InitializeLayerTreeView");
danakj17216f4d2018-10-16 22:57:321550
1551 layer_tree_view_ = std::make_unique<LayerTreeView>(
1552 this, compositor_deps_->GetCompositorMainThreadTaskRunner(),
1553 compositor_deps_->GetCompositorImplThreadTaskRunner(),
1554 compositor_deps_->GetTaskGraphRunner(),
1555 compositor_deps_->GetWebMainThreadScheduler());
1556 layer_tree_view_->Initialize(
danakj6dcbc5962018-11-16 16:45:421557 GenerateLayerTreeSettings(compositor_deps_, for_child_local_root_frame_,
danakj17216f4d2018-10-16 22:57:321558 screen_info_.rect.size(),
1559 screen_info_.device_scale_factor),
1560 compositor_deps_->CreateUkmRecorderFactory());
1561
Fady Samuel1c1ad3692018-11-06 23:28:401562 UpdateSurfaceAndScreenInfo(local_surface_id_allocation_from_parent_,
danakj17216f4d2018-10-16 22:57:321563 compositor_viewport_pixel_size_, screen_info_);
1564 layer_tree_view_->SetRasterColorSpace(
1565 screen_info_.color_space.GetRasterColorSpace());
1566 layer_tree_view_->SetContentSourceId(current_content_source_id_);
1567 // For background pages and certain tests, we don't want to trigger
1568 // LayerTreeFrameSink creation.
1569 bool should_generate_frame_sink =
1570 !compositor_never_visible_ && RenderThreadImpl::current();
danakje9be3162018-11-05 21:32:001571 if (!should_generate_frame_sink) {
1572 // Prevents SetVisible() from blink from doing anything.
danakj17216f4d2018-10-16 22:57:321573 layer_tree_view_->SetNeverVisible();
danakj6dcbc5962018-11-16 16:45:421574 } else if (!is_frozen_) {
danakje9be3162018-11-05 21:32:001575 // Begins the compositor's scheduler to start producing frames.
1576 // Don't do this if the RenderWidget is attached to a RenderViewImpl for a
danakjbc6ba9d22018-11-17 04:03:411577 // proxy main frame, as this RenderWidget is frozen then, and won't be
danakje9be3162018-11-05 21:32:001578 // used for compositing until a WebFrameWidget is attached.
1579 StartCompositor();
1580 }
danakj17216f4d2018-10-16 22:57:321581
danakj17216f4d2018-10-16 22:57:321582 DCHECK_NE(MSG_ROUTING_NONE, routing_id_);
1583 layer_tree_view_->SetFrameSinkId(
1584 viz::FrameSinkId(RenderThread::Get()->GetClientId(), routing_id_));
1585
1586 RenderThreadImpl* render_thread = RenderThreadImpl::current();
1587 if (render_thread) {
1588 input_event_queue_ = base::MakeRefCounted<MainThreadEventQueue>(
1589 this, render_thread->GetWebMainThreadScheduler()->InputTaskRunner(),
1590 render_thread->GetWebMainThreadScheduler(), should_generate_frame_sink);
1591 }
1592
1593 return layer_tree_view_.get();
1594}
1595
danakjde8b7482018-11-15 23:45:021596void RenderWidget::StartCompositor() {
1597 if (!is_hidden_)
1598 layer_tree_view_->SetVisible(true);
1599}
1600
1601void RenderWidget::StopCompositor() {
1602 layer_tree_view_->SetVisible(false);
1603 // Drop all gpu resources, this makes SetVisible(true) more expensive/slower
1604 // but we don't expect to use this RenderWidget again until some possible
1605 // future navigation. This brings us a bit closer to emulating deleting the
1606 // RenderWidget instead of just stopping the compositor.
1607 layer_tree_view_->ReleaseLayerTreeFrameSink();
1608}
1609
danakj6dcbc5962018-11-16 16:45:421610void RenderWidget::SetIsFrozen(bool is_frozen) {
1611 DCHECK_NE(is_frozen, is_frozen_);
1612 is_frozen_ = is_frozen;
1613 if (is_frozen)
danakjde8b7482018-11-15 23:45:021614 StopCompositor();
1615 else
1616 StartCompositor();
1617}
1618
danakjf87460792018-12-04 20:14:221619void RenderWidget::WarmupCompositor() {
1620 DCHECK(is_frozen_);
1621
1622 // Tests have no RenderThreadImpl /o\.
1623 if (!RenderThreadImpl::current())
1624 return;
1625
1626 // Keeping things simple. This would cancel any outstanding warmup if we
1627 // happened to have one (this should be basically impossible). This avoids any
1628 // extra book keeping about the outstanding reqeust.
1629 warmup_weak_ptr_factory_.InvalidateWeakPtrs();
1630 // And if we already did a warmup then we're done.
1631 if (warmup_frame_sink_)
1632 return;
1633
1634 // Mark us pending the warmup frame sink *before* calling
1635 // DoRequestNewLayerTreeFrameSink() as it may run the reply callback
1636 // synchronously. So we don't want to change any state after the call
1637 // to DoRequestNewLayerTreeFrameSink() here.
1638 warmup_frame_sink_request_pending_ = true;
1639
1640 auto cb = base::BindOnce(&RenderWidget::OnReplyForWarmupCompositor,
1641 warmup_weak_ptr_factory_.GetWeakPtr());
1642 DoRequestNewLayerTreeFrameSink(std::move(cb));
1643}
1644
1645void RenderWidget::OnReplyForWarmupCompositor(
1646 std::unique_ptr<cc::LayerTreeFrameSink> sink) {
1647 warmup_frame_sink_request_pending_ = false;
1648
1649 if (after_warmup_callback_)
1650 std::move(after_warmup_callback_).Run(std::move(sink));
1651 else
1652 warmup_frame_sink_ = std::move(sink);
1653}
1654
1655void RenderWidget::AbortWarmupCompositor() {
1656 warmup_frame_sink_request_pending_ = false;
1657 // Drop any pending warmup.
1658 warmup_weak_ptr_factory_.InvalidateWeakPtrs();
1659 // And drop any completed one.
1660 warmup_frame_sink_.reset();
1661
1662 // If we had saved a callback to run after warmup, just do so now indicating
1663 // failure.
1664 if (after_warmup_callback_)
1665 std::move(after_warmup_callback_).Run(nullptr);
1666}
1667
[email protected]2533ce12009-05-09 00:02:241668void RenderWidget::DoDeferredClose() {
Albert J. Wong3c93c182018-09-27 17:29:431669 Send(new WidgetHostMsg_Close(routing_id_));
[email protected]2533ce12009-05-09 00:02:241670}
1671
Blink Reformat1c4d759e2017-04-09 16:34:541672void RenderWidget::CloseWidgetSoon() {
skyostiled8969c2015-07-20 16:57:081673 DCHECK(content::RenderThread::Get());
danakj6dcbc5962018-11-16 16:45:421674 if (is_frozen_) {
1675 // This widget is currently not active. The active main frame widget is in a
[email protected]e1c3a552012-05-04 20:51:321676 // different process. Have the browser route the close request to the
1677 // active widget instead, so that the correct unload handlers are run.
Albert J. Wongfb64e142018-10-16 01:10:451678 Send(new WidgetHostMsg_RouteCloseEvent(routing_id_));
[email protected]e1c3a552012-05-04 20:51:321679 return;
1680 }
1681
danakj292b63e2018-11-19 20:47:061682 // Prevent compositor from setting up new IPC channels, since we know a
1683 // WidgetMsg_Close is coming. We do this immediately, not in DoDeferredClose,
1684 // as the caller (eg WebPagePopupImpl) may start tearing down things after
1685 // calling this method, including detaching the frame from this RenderWidget.
1686 // Then trying to make a LayerTreeFrameSink would crash.
1687 // https://crbug.com/906340
1688 host_will_close_this_ = true;
1689
initial.commit09911bf2008-07-26 23:55:291690 // If a page calls window.close() twice, we'll end up here twice, but that's
1691 // OK. It is safe to send multiple Close messages.
danakj292b63e2018-11-19 20:47:061692 //
[email protected]2533ce12009-05-09 00:02:241693 // Ask the RenderWidgetHost to initiate close. We could be called from deep
1694 // in Javascript. If we ask the RendwerWidgetHost to close now, the window
1695 // could be closed before the JS finishes executing. So instead, post a
1696 // message back to the message loop, which won't run until the JS is
1697 // complete, and then the Close message can be sent.
Hajime Hoshi315a61f2018-08-14 17:27:281698 GetCleanupTaskRunner()->PostTask(
1699 FROM_HERE, base::BindOnce(&RenderWidget::DoDeferredClose, this));
initial.commit09911bf2008-07-26 23:55:291700}
1701
1702void RenderWidget::Close() {
danakj6dcbc5962018-11-16 16:45:421703 // This was done immediately in the |for_child_local_root_frame_| case in the
1704 // OnClose() IPC handler.
1705 if (!for_child_local_root_frame_)
danakj5c5269b2018-11-06 00:39:111706 CloseWebWidget();
1707
danakja6c10012018-07-06 14:25:361708 layer_tree_view_.reset();
Albert J. Wong7bbf22d2018-12-20 00:27:271709 if (delegate())
1710 delegate()->DidCloseWidget();
Albert J. Wongd593de532018-09-06 05:42:151711 // Note the ACK is a control message going to the RenderProcessHost.
Albert J. Wong3c93c182018-09-27 17:29:431712 RenderThread::Get()->Send(new WidgetHostMsg_Close_ACK(routing_id()));
Ken Buchananed449bb2018-02-01 21:02:051713}
1714
1715void RenderWidget::CloseWebWidget() {
danakj35314ab2018-10-19 16:37:341716 // If the browser has not sent OnDisableDeviceEmulation, we have an emulator
1717 // hanging out still. Destroying it must happen *after* the IPC route is
1718 // removed so that another IPC does not arrive and re-create the emulator
1719 // during closing.
1720 //
1721 // This destruction is normally part of an IPC and expects objects to be alive
1722 // that would be alive while the IPC route is active such as the
1723 // |layer_tree_view_|. So we ensure that it is the first thing to be
1724 // destroyed here before deleting things from the RenderWidget or the
Albert J. Wong7bbf22d2018-12-20 00:27:271725 // delegate().
danakj35314ab2018-10-19 16:37:341726 //
1727 // TODO(danakj): The emulator could reset to non-emulated values in an
1728 // explicit method call (instead of in the destructor) that occurs when
1729 // emulation is disabled, but does not need to occur during RenderWidget
1730 // closing. Then we would not have to destroy this so carefully.
1731 screen_metrics_emulator_.reset();
1732
danakj5c5269b2018-11-06 00:39:111733 // Informs the WebWidget that compositor is being destroyed, so it can remove
1734 // references to it first.
1735 //
Albert J. Wong7bbf22d2018-12-20 00:27:271736 // When delegate() is present, the RenderWidget is for a main frame,
danakj5c5269b2018-11-06 00:39:111737 // and the GetWebWidget() is not the same as |webwidget_internal_|. However
1738 // that widget is responsible for doing WillCloseLayerTreeView() on the
1739 // |webwidget_internal_|, not us. Otherwise, they are the same and this is
1740 // notifying |webwidget_internal_|.
1741 GetWebWidget()->WillCloseLayerTreeView();
1742
Albert J. Wong7bbf22d2018-12-20 00:27:271743 // While the wrapping WebWidget from an delegate() is responsible for
danakj5c5269b2018-11-06 00:39:111744 // doing WillCloseLayerTreeView() on the |webwidget_internal_|, this class is
1745 // responsible for calling Close() on it. Notably, then, the wrapping
1746 // WebWidget does not.
1747 webwidget_internal_->Close();
1748 webwidget_internal_ = nullptr;
initial.commit09911bf2008-07-26 23:55:291749}
1750
Mitsuru Oshima3c07b332018-02-06 04:56:501751void RenderWidget::UpdateWebViewWithDeviceScaleFactor() {
1752 blink::WebFrameWidget* frame_widget = GetFrameWidget();
1753 blink::WebFrame* current_frame =
1754 frame_widget ? frame_widget->LocalRoot() : nullptr;
1755 blink::WebView* webview = current_frame ? current_frame->View() : nullptr;
1756 if (webview) {
danakj4b347212018-07-04 17:55:171757 if (compositor_deps_->IsUseZoomForDSFEnabled())
Fady Samuela863f152018-03-09 16:10:031758 webview->SetZoomFactorForDeviceScaleFactor(
1759 GetWebScreenInfo().device_scale_factor);
Mitsuru Oshima3c07b332018-02-06 04:56:501760 else
Fady Samuela863f152018-03-09 16:10:031761 webview->SetDeviceScaleFactor(GetWebScreenInfo().device_scale_factor);
Mitsuru Oshima3c07b332018-02-06 04:56:501762
1763 webview->GetSettings()->SetPreferCompositingToLCDTextEnabled(
Christopher Camerone9a30c12018-03-07 08:23:381764 PreferCompositingToLCDText(compositor_deps_,
Fady Samuela863f152018-03-09 16:10:031765 GetWebScreenInfo().device_scale_factor));
Mitsuru Oshima3c07b332018-02-06 04:56:501766 }
1767}
1768
Ehsan Karamad655d7b8a2018-01-12 18:38:381769blink::WebFrameWidget* RenderWidget::GetFrameWidget() const {
danakj89ae4532018-12-12 22:52:551770 // TODO(danakj): Remove this check and don't call this method for non-frames.
1771 if (!for_frame())
1772 return nullptr;
1773 // TODO(danakj): Is this needed? IPCs stop after closing, but code used to
1774 // check for a null WebWidget.
1775 if (closing_)
Ehsan Karamad655d7b8a2018-01-12 18:38:381776 return nullptr;
1777
danakj89ae4532018-12-12 22:52:551778 blink::WebWidget* widget;
Albert J. Wong7bbf22d2018-12-20 00:27:271779 if (delegate()) {
danakj89ae4532018-12-12 22:52:551780 // Main frame WebFrameWidgets are held by the delegate, the internal widget
1781 // points directly to the WebView.
1782 // TODO(ekaramad): We should drop IPCs when |is_frozen_| instead of
1783 // handling them and finding a null here. However there is also the case
1784 // of the frame being detached without the widget being frozen to be
1785 // resolved (https://crbug.com/906340). So for now this can return null.
Albert J. Wong7bbf22d2018-12-20 00:27:271786 widget = delegate()->GetWebWidgetForWidget();
danakj89ae4532018-12-12 22:52:551787 } else {
1788 // Subframes always have a WebFrameWidget themselves.
1789 widget = webwidget_internal_;
Ehsan Karamad655d7b8a2018-01-12 18:38:381790 }
danakj89ae4532018-12-12 22:52:551791 return static_cast<blink::WebFrameWidget*>(widget);
Ehsan Karamad655d7b8a2018-01-12 18:38:381792}
1793
bokan841fdc72016-10-06 00:16:351794void RenderWidget::ScreenRectToEmulatedIfNeeded(WebRect* window_rect) const {
1795 DCHECK(window_rect);
1796 float scale = popup_origin_scale_for_emulation_;
1797 if (!scale)
1798 return;
1799 window_rect->x =
1800 popup_view_origin_for_emulation_.x() +
1801 (window_rect->x - popup_screen_origin_for_emulation_.x()) / scale;
1802 window_rect->y =
1803 popup_view_origin_for_emulation_.y() +
1804 (window_rect->y - popup_screen_origin_for_emulation_.y()) / scale;
1805}
1806
1807void RenderWidget::EmulatedToScreenRectIfNeeded(WebRect* window_rect) const {
1808 DCHECK(window_rect);
1809 float scale = popup_origin_scale_for_emulation_;
1810 if (!scale)
1811 return;
1812 window_rect->x =
1813 popup_screen_origin_for_emulation_.x() +
1814 (window_rect->x - popup_view_origin_for_emulation_.x()) * scale;
1815 window_rect->y =
1816 popup_screen_origin_for_emulation_.y() +
1817 (window_rect->y - popup_view_origin_for_emulation_.y()) * scale;
1818}
1819
Blink Reformat1c4d759e2017-04-09 16:34:541820WebRect RenderWidget::WindowRect() {
bokan841fdc72016-10-06 00:16:351821 WebRect rect;
bokan6b08cd22016-10-05 00:55:211822 if (pending_window_rect_count_) {
1823 // NOTE(mbelshe): If there is a pending_window_rect_, then getting
1824 // the RootWindowRect is probably going to return wrong results since the
1825 // browser may not have processed the Move yet. There isn't really anything
1826 // good to do in this case, and it shouldn't happen - since this size is
1827 // only really needed for windowToScreen, which is only used for Popups.
bokan841fdc72016-10-06 00:16:351828 rect = pending_window_rect_;
1829 } else {
1830 rect = window_screen_rect_;
bokan6b08cd22016-10-05 00:55:211831 }
[email protected]2533ce12009-05-09 00:02:241832
bokan841fdc72016-10-06 00:16:351833 ScreenRectToEmulatedIfNeeded(&rect);
1834 return rect;
bokan6b08cd22016-10-05 00:55:211835}
1836
Blink Reformat1c4d759e2017-04-09 16:34:541837WebRect RenderWidget::ViewRect() {
Albert J. Wong3c93c182018-09-27 17:29:431838 WebRect rect = widget_screen_rect_;
bokan841fdc72016-10-06 00:16:351839 ScreenRectToEmulatedIfNeeded(&rect);
1840 return rect;
initial.commit09911bf2008-07-26 23:55:291841}
1842
Blink Reformat1c4d759e2017-04-09 16:34:541843void RenderWidget::SetToolTipText(const blink::WebString& text,
[email protected]8a9d6ca32011-06-06 20:11:301844 WebTextDirection hint) {
Albert J. Wong3c93c182018-09-27 17:29:431845 Send(new WidgetHostMsg_SetTooltipText(routing_id_, text.Utf16(), hint));
[email protected]8a9d6ca32011-06-06 20:11:301846}
1847
Blink Reformat1c4d759e2017-04-09 16:34:541848void RenderWidget::SetWindowRect(const WebRect& rect_in_screen) {
danakj81d601e62018-10-09 22:15:051849 // This path is for the renderer to change the on-screen position/size of
1850 // the widget by changing its window rect. This is not possible for
1851 // RenderWidgets whose position/size are controlled by layout from another
danakj6dcbc5962018-11-16 16:45:421852 // frame tree (ie. child local root frames), as the window rect can only be
1853 // set by the browser.
1854 if (for_child_local_root_frame_)
danakj81d601e62018-10-09 22:15:051855 return;
1856
oshima33ec97cd2015-12-14 19:40:241857 WebRect window_rect = rect_in_screen;
bokan841fdc72016-10-06 00:16:351858 EmulatedToScreenRectIfNeeded(&window_rect);
[email protected]b2e4c70132013-10-03 02:07:511859
danakj65f423b72018-10-10 17:21:201860 if (resizing_mode_selector_->is_synchronous_mode()) {
Kent Tamura21d1de62018-12-10 04:45:201861 // This is a web-test-only path. At one point, it was planned to be
danakj65f423b72018-10-10 17:21:201862 // removed. See https://crbug.com/309760.
bokanc007c3a2015-02-03 07:15:561863 SetWindowRectSynchronously(window_rect);
danakj65f423b72018-10-10 17:21:201864 return;
1865 }
1866
1867 if (show_callback_) {
1868 // The widget is not shown yet. Delay the |window_rect| being sent to the
1869 // browser until Show() is called so it can be sent with that IPC, once the
1870 // browser is ready for the info.
1871 initial_rect_ = window_rect;
1872 } else {
1873 Send(new WidgetHostMsg_RequestSetBounds(routing_id_, window_rect));
1874 SetPendingWindowRect(window_rect);
initial.commit09911bf2008-07-26 23:55:291875 }
1876}
1877
[email protected]2533ce12009-05-09 00:02:241878void RenderWidget::SetPendingWindowRect(const WebRect& rect) {
1879 pending_window_rect_ = rect;
1880 pending_window_rect_count_++;
[email protected]2533ce12009-05-09 00:02:241881
bokan6b08cd22016-10-05 00:55:211882 // Popups don't get size updates back from the browser so just store the set
1883 // values.
danakj89ae4532018-12-12 22:52:551884 if (!for_frame()) {
Blink Reformat1c4d759e2017-04-09 16:34:541885 window_screen_rect_ = rect;
Albert J. Wong3c93c182018-09-27 17:29:431886 widget_screen_rect_ = rect;
[email protected]2533ce12009-05-09 00:02:241887 }
[email protected]d4547452008-08-28 18:36:371888}
1889
wjmacleanee244e02017-05-26 21:06:071890void RenderWidget::OnShowContextMenu(ui::MenuSourceType source_type,
1891 const gfx::Point& location) {
wjmacleanee244e02017-05-26 21:06:071892 has_host_context_menu_location_ = true;
1893 host_context_menu_location_ = location;
1894 if (GetWebWidget()) {
1895 GetWebWidget()->ShowContextMenu(
1896 static_cast<blink::WebMenuSourceType>(source_type));
1897 }
1898 has_host_context_menu_location_ = false;
1899}
1900
[email protected]fa7b1dc2010-06-23 17:53:041901void RenderWidget::OnImeSetComposition(
[email protected]fcf75d42013-12-03 20:11:261902 const base::string16& text,
Ryan Landay9e42fd742017-08-12 01:59:111903 const std::vector<WebImeTextSpan>& ime_text_spans,
chongz7eb752802016-01-27 21:28:071904 const gfx::Range& replacement_range,
Ryan Landay9e42fd742017-08-12 01:59:111905 int selection_start,
1906 int selection_end) {
ekaramad5aff1942017-01-06 01:26:351907 if (!ShouldHandleImeEvents())
1908 return;
1909
brettw4b461082016-11-19 18:55:161910#if BUILDFLAG(ENABLE_PLUGINS)
EhsanK22d482e2017-08-10 17:29:491911 if (auto* plugin = GetFocusedPepperPluginInsideWidget()) {
Ryan Landay9e42fd742017-08-12 01:59:111912 plugin->render_frame()->OnImeSetComposition(text, ime_text_spans,
EhsanK22d482e2017-08-10 17:29:491913 selection_start, selection_end);
ekaramad2a46d632016-07-19 13:33:091914 return;
1915 }
1916#endif
[email protected]66fca5bc2013-05-23 06:58:291917 ImeEventGuard guard(this);
ekaramad2daaf672016-11-10 20:29:011918 blink::WebInputMethodController* controller = GetInputMethodController();
ekaramad2daaf672016-11-10 20:29:011919 if (!controller ||
Blink Reformat1c4d759e2017-04-09 16:34:541920 !controller->SetComposition(
Ryan Landay9e42fd742017-08-12 01:59:111921 WebString::FromUTF16(text), WebVector<WebImeTextSpan>(ime_text_spans),
ekaramadce32ef9f2017-02-09 17:33:561922 replacement_range.IsValid()
1923 ? WebRange(replacement_range.start(), replacement_range.length())
1924 : WebRange(),
1925 selection_start, selection_end)) {
[email protected]fa7b1dc2010-06-23 17:53:041926 // If we failed to set the composition text, then we need to let the browser
1927 // process to cancel the input method's ongoing composition session, to make
1928 // sure we are in a consistent state.
Dave Tapuska04bc5ee92018-04-17 19:03:311929 if (mojom::WidgetInputHandlerHost* host =
1930 widget_input_handler_manager_->GetWidgetInputHandlerHost()) {
1931 host->ImeCancelComposition();
Dave Tapuska139a72f2017-09-06 21:57:031932 }
[email protected]7f00efa2010-04-15 05:01:261933 }
nonafa291792016-08-10 02:36:181934 UpdateCompositionInfo(false /* not an immediate request */);
[email protected]fa7b1dc2010-06-23 17:53:041935}
1936
rlanday7efe2302017-01-11 00:14:281937void RenderWidget::OnImeCommitText(
1938 const base::string16& text,
Ryan Landay9e42fd742017-08-12 01:59:111939 const std::vector<WebImeTextSpan>& ime_text_spans,
rlanday7efe2302017-01-11 00:14:281940 const gfx::Range& replacement_range,
1941 int relative_cursor_pos) {
ekaramad5aff1942017-01-06 01:26:351942 if (!ShouldHandleImeEvents())
1943 return;
1944
brettw4b461082016-11-19 18:55:161945#if BUILDFLAG(ENABLE_PLUGINS)
EhsanK22d482e2017-08-10 17:29:491946 if (auto* plugin = GetFocusedPepperPluginInsideWidget()) {
1947 plugin->render_frame()->OnImeCommitText(text, replacement_range,
1948 relative_cursor_pos);
ekaramad2a46d632016-07-19 13:33:091949 return;
1950 }
1951#endif
[email protected]66fca5bc2013-05-23 06:58:291952 ImeEventGuard guard(this);
fsamuele8326c742016-01-12 00:49:391953 input_handler_->set_handling_input_event(true);
ekaramadb6483a052017-02-10 02:23:261954 if (auto* controller = GetInputMethodController()) {
Blink Reformat1c4d759e2017-04-09 16:34:541955 controller->CommitText(
Ryan Landay9e42fd742017-08-12 01:59:111956 WebString::FromUTF16(text), WebVector<WebImeTextSpan>(ime_text_spans),
ekaramadce32ef9f2017-02-09 17:33:561957 replacement_range.IsValid()
1958 ? WebRange(replacement_range.start(), replacement_range.length())
1959 : WebRange(),
1960 relative_cursor_pos);
ekaramadb6483a052017-02-10 02:23:261961 }
aelias87b8f7c2016-09-14 03:19:291962 input_handler_->set_handling_input_event(false);
1963 UpdateCompositionInfo(false /* not an immediate request */);
1964}
1965
1966void RenderWidget::OnImeFinishComposingText(bool keep_selection) {
ekaramad5aff1942017-01-06 01:26:351967 if (!ShouldHandleImeEvents())
1968 return;
1969
brettw4b461082016-11-19 18:55:161970#if BUILDFLAG(ENABLE_PLUGINS)
EhsanK22d482e2017-08-10 17:29:491971 if (auto* plugin = GetFocusedPepperPluginInsideWidget()) {
1972 plugin->render_frame()->OnImeFinishComposingText(keep_selection);
aelias87b8f7c2016-09-14 03:19:291973 return;
1974 }
1975#endif
1976
changwan38c3eb612016-12-09 01:45:561977 if (!GetWebWidget())
aelias87b8f7c2016-09-14 03:19:291978 return;
1979 ImeEventGuard guard(this);
1980 input_handler_->set_handling_input_event(true);
ekaramad2daaf672016-11-10 20:29:011981 if (auto* controller = GetInputMethodController()) {
Blink Reformat1c4d759e2017-04-09 16:34:541982 controller->FinishComposingText(
1983 keep_selection ? WebInputMethodController::kKeepSelection
1984 : WebInputMethodController::kDoNotKeepSelection);
ekaramad2daaf672016-11-10 20:29:011985 }
fsamuele8326c742016-01-12 00:49:391986 input_handler_->set_handling_input_event(false);
nonafa291792016-08-10 02:36:181987 UpdateCompositionInfo(false /* not an immediate request */);
initial.commit09911bf2008-07-26 23:55:291988}
1989
Christopher Camerone9a30c12018-03-07 08:23:381990void RenderWidget::UpdateSurfaceAndScreenInfo(
Fady Samuel1c1ad3692018-11-06 23:28:401991 const viz::LocalSurfaceIdAllocation& new_local_surface_id_allocation,
Christopher Cameronaad15a0d2018-03-09 03:47:331992 const gfx::Size& new_compositor_viewport_pixel_size,
Christopher Camerone9a30c12018-03-07 08:23:381993 const ScreenInfo& new_screen_info) {
Christopher Camerone9a30c12018-03-07 08:23:381994 bool orientation_changed =
1995 screen_info_.orientation_angle != new_screen_info.orientation_angle ||
1996 screen_info_.orientation_type != new_screen_info.orientation_type;
1997 bool web_device_scale_factor_changed =
1998 screen_info_.device_scale_factor != new_screen_info.device_scale_factor;
Fady Samuela863f152018-03-09 16:10:031999 ScreenInfo previous_original_screen_info = GetOriginalScreenInfo();
Christopher Cameronbe7ecea2018-02-27 00:47:192000
Fady Samuel1c1ad3692018-11-06 23:28:402001 local_surface_id_allocation_from_parent_ = new_local_surface_id_allocation;
Christopher Cameronaad15a0d2018-03-09 03:47:332002 compositor_viewport_pixel_size_ = new_compositor_viewport_pixel_size;
Christopher Camerone9a30c12018-03-07 08:23:382003 screen_info_ = new_screen_info;
Christopher Cameronbe7ecea2018-02-27 00:47:192004
danakja6c10012018-07-06 14:25:362005 if (layer_tree_view_) {
2006 layer_tree_view_->SetViewportVisibleRect(ViewportVisibleRect());
Christopher Camerone9a30c12018-03-07 08:23:382007 // Note carefully that the DSF specified in |new_screen_info| is not the
2008 // DSF used by the compositor during device emulation!
danakja6c10012018-07-06 14:25:362009 layer_tree_view_->SetViewportSizeAndScale(
Fady Samuela863f152018-03-09 16:10:032010 compositor_viewport_pixel_size_,
Fady Samuel4255c182018-05-24 20:42:362011 GetOriginalScreenInfo().device_scale_factor,
Fady Samuel1c1ad3692018-11-06 23:28:402012 local_surface_id_allocation_from_parent_);
Christopher Camerone9a30c12018-03-07 08:23:382013 }
Christopher Cameronbe7ecea2018-02-27 00:47:192014
Christopher Camerone9a30c12018-03-07 08:23:382015 if (orientation_changed)
2016 OnOrientationChange();
Christopher Cameronbe7ecea2018-02-27 00:47:192017
Fady Samuela863f152018-03-09 16:10:032018 if (previous_original_screen_info != GetOriginalScreenInfo()) {
Christopher Camerone9a30c12018-03-07 08:23:382019 for (auto& observer : render_frame_proxies_)
Fady Samuela863f152018-03-09 16:10:032020 observer.OnScreenInfoChanged(GetOriginalScreenInfo());
Christopher Camerone9a30c12018-03-07 08:23:382021
2022 // Notify all embedded BrowserPlugins of the updated ScreenInfo.
2023 for (auto& observer : browser_plugins_)
Fady Samuela863f152018-03-09 16:10:032024 observer.ScreenInfoChanged(GetOriginalScreenInfo());
Christopher Camerone9a30c12018-03-07 08:23:382025 }
2026
2027 if (web_device_scale_factor_changed)
Christopher Cameronbe7ecea2018-02-27 00:47:192028 UpdateWebViewWithDeviceScaleFactor();
oshimad5279032015-12-16 18:22:332029}
2030
danakja2c9d0a92018-07-25 20:01:182031void RenderWidget::SetWindowRectSynchronously(
2032 const gfx::Rect& new_window_rect) {
2033 VisualProperties visual_properties;
2034 visual_properties.screen_info = screen_info_;
2035 visual_properties.new_size = new_window_rect.size();
2036 visual_properties.compositor_viewport_pixel_size = gfx::ScaleToCeiledSize(
2037 new_window_rect.size(), GetWebScreenInfo().device_scale_factor);
2038 visual_properties.visible_viewport_size = new_window_rect.size();
2039 visual_properties.is_fullscreen_granted = is_fullscreen_granted_;
2040 visual_properties.display_mode = display_mode_;
Fady Samuel84d8a1f72018-11-08 00:26:172041 visual_properties.local_surface_id_allocation =
2042 local_surface_id_allocation_from_parent_;
W. James MacLean2a90bff2018-11-05 20:52:472043 visual_properties.page_scale_factor = page_scale_factor_from_mainframe_;
danakja2c9d0a92018-07-25 20:01:182044 // We are resizing the window from the renderer, so allocate a new
2045 // viz::LocalSurfaceId to avoid surface invariants violations in tests.
2046 if (layer_tree_view_)
2047 layer_tree_view_->RequestNewLocalSurfaceId();
2048 SynchronizeVisualProperties(visual_properties);
2049
Albert J. Wong3c93c182018-09-27 17:29:432050 widget_screen_rect_ = new_window_rect;
danakja2c9d0a92018-07-25 20:01:182051 window_screen_rect_ = new_window_rect;
danakj65f423b72018-10-10 17:21:202052 if (show_callback_) {
2053 // Tests may call here directly to control the window rect. If
2054 // Show() did not happen yet, the rect is stored to be passed to the
2055 // browser when the RenderWidget requests Show().
danakja2c9d0a92018-07-25 20:01:182056 initial_rect_ = new_window_rect;
danakj65f423b72018-10-10 17:21:202057 }
danakja2c9d0a92018-07-25 20:01:182058}
2059
Vladimir Levin98d76dad2018-04-21 00:21:292060void RenderWidget::UpdateCaptureSequenceNumber(
2061 uint32_t capture_sequence_number) {
2062 if (capture_sequence_number == last_capture_sequence_number_)
2063 return;
2064 last_capture_sequence_number_ = capture_sequence_number;
2065
2066 // Notify observers of the new capture sequence number.
2067 for (auto& observer : render_frame_proxies_)
2068 observer.UpdateCaptureSequenceNumber(capture_sequence_number);
2069 for (auto& observer : browser_plugins_)
2070 observer.UpdateCaptureSequenceNumber(capture_sequence_number);
2071}
2072
[email protected]4873c7d2009-07-16 06:36:282073void RenderWidget::OnSetTextDirection(WebTextDirection direction) {
EhsanK955ba582017-11-30 21:14:402074 if (auto* frame = GetFocusedWebLocalFrameInWidget())
2075 frame->SetTextDirection(direction);
[email protected]07f953332009-03-25 04:31:112076}
2077
Albert J. Wong3c93c182018-09-27 17:29:432078void RenderWidget::OnUpdateScreenRects(const gfx::Rect& widget_screen_rect,
[email protected]80ad8622012-11-07 16:33:032079 const gfx::Rect& window_screen_rect) {
[email protected]b2e4c70132013-10-03 02:07:512080 if (screen_metrics_emulator_) {
Albert J. Wong3c93c182018-09-27 17:29:432081 screen_metrics_emulator_->OnUpdateScreenRects(widget_screen_rect,
mfomitchev2600fd7c2016-02-17 20:53:392082 window_screen_rect);
[email protected]b2e4c70132013-10-03 02:07:512083 } else {
Albert J. Wong3c93c182018-09-27 17:29:432084 SetScreenRects(widget_screen_rect, window_screen_rect);
[email protected]b2e4c70132013-10-03 02:07:512085 }
Albert J. Wong3c93c182018-09-27 17:29:432086 Send(new WidgetHostMsg_UpdateScreenRects_ACK(routing_id()));
[email protected]80ad8622012-11-07 16:33:032087}
2088
kenrbea731792017-01-13 15:10:482089void RenderWidget::OnSetViewportIntersection(
Ken Buchananb2c9e262018-03-10 16:53:312090 const gfx::Rect& viewport_intersection,
Stefan Zager54e25832018-08-14 22:15:312091 const gfx::Rect& compositor_visible_rect,
2092 bool occluded_or_obscured) {
Ehsan Karamadf3ca2a82018-01-20 01:05:252093 if (auto* frame_widget = GetFrameWidget()) {
Ken Buchananb2c9e262018-03-10 16:53:312094 compositor_visible_rect_ = compositor_visible_rect;
Stefan Zager54e25832018-08-14 22:15:312095 frame_widget->SetRemoteViewportIntersection(viewport_intersection,
2096 occluded_or_obscured);
danakja6c10012018-07-06 14:25:362097 layer_tree_view_->SetViewportVisibleRect(ViewportVisibleRect());
kenrbea731792017-01-13 15:10:482098 }
2099}
2100
kenrb04323782017-06-23 01:23:322101void RenderWidget::OnSetIsInert(bool inert) {
danakj89ae4532018-12-12 22:52:552102 if (auto* frame_widget = GetFrameWidget())
Ehsan Karamadf3ca2a82018-01-20 01:05:252103 frame_widget->SetIsInert(inert);
kenrb04323782017-06-23 01:23:322104}
2105
sunxd540a9962018-05-24 22:51:062106void RenderWidget::OnSetInheritedEffectiveTouchAction(
2107 cc::TouchAction touch_action) {
danakj89ae4532018-12-12 22:52:552108 if (auto* frame_widget = GetFrameWidget())
sunxd540a9962018-05-24 22:51:062109 frame_widget->SetInheritedEffectiveTouchAction(touch_action);
sunxd540a9962018-05-24 22:51:062110}
2111
Ken Buchanan8a319fb2017-11-15 18:37:122112void RenderWidget::OnUpdateRenderThrottlingStatus(bool is_throttled,
2113 bool subtree_throttled) {
danakj89ae4532018-12-12 22:52:552114 if (auto* frame_widget = GetFrameWidget())
Ehsan Karamadf3ca2a82018-01-20 01:05:252115 frame_widget->UpdateRenderThrottlingStatus(is_throttled, subtree_throttled);
Ken Buchanan8a319fb2017-11-15 18:37:122116}
2117
paulmeyer90f6c31d2016-11-12 00:17:592118void RenderWidget::OnDragTargetDragEnter(
2119 const std::vector<DropData::Metadata>& drop_meta_data,
Ella Ge80a52dce2017-11-15 18:01:522120 const gfx::PointF& client_point,
2121 const gfx::PointF& screen_point,
paulmeyer90f6c31d2016-11-12 00:17:592122 WebDragOperationsMask ops,
2123 int key_modifiers) {
Ehsan Karamadf3ca2a82018-01-20 01:05:252124 blink::WebFrameWidget* frame_widget = GetFrameWidget();
2125 if (!frame_widget)
paulmeyer90f6c31d2016-11-12 00:17:592126 return;
2127
Ehsan Karamadf3ca2a82018-01-20 01:05:252128 WebDragOperation operation = frame_widget->DragTargetDragEnter(
2129 DropMetaDataToWebDragData(drop_meta_data), client_point, screen_point,
2130 ops, key_modifiers);
paulmeyer90f6c31d2016-11-12 00:17:592131
2132 Send(new DragHostMsg_UpdateDragCursor(routing_id(), operation));
2133}
2134
Ella Ge80a52dce2017-11-15 18:01:522135void RenderWidget::OnDragTargetDragOver(const gfx::PointF& client_point,
2136 const gfx::PointF& screen_point,
paulmeyer90f6c31d2016-11-12 00:17:592137 WebDragOperationsMask ops,
2138 int key_modifiers) {
Ehsan Karamadf3ca2a82018-01-20 01:05:252139 blink::WebFrameWidget* frame_widget = GetFrameWidget();
2140 if (!frame_widget)
paulmeyer90f6c31d2016-11-12 00:17:592141 return;
2142
Ehsan Karamadf3ca2a82018-01-20 01:05:252143 WebDragOperation operation = frame_widget->DragTargetDragOver(
2144 ConvertWindowPointToViewport(client_point), screen_point, ops,
2145 key_modifiers);
paulmeyer90f6c31d2016-11-12 00:17:592146
2147 Send(new DragHostMsg_UpdateDragCursor(routing_id(), operation));
2148}
2149
Ella Ge80a52dce2017-11-15 18:01:522150void RenderWidget::OnDragTargetDragLeave(const gfx::PointF& client_point,
2151 const gfx::PointF& screen_point) {
Ehsan Karamadf3ca2a82018-01-20 01:05:252152 blink::WebFrameWidget* frame_widget = GetFrameWidget();
2153 if (!frame_widget)
paulmeyer90f6c31d2016-11-12 00:17:592154 return;
Ehsan Karamadf3ca2a82018-01-20 01:05:252155
2156 frame_widget
Blink Reformat1c4d759e2017-04-09 16:34:542157 ->DragTargetDragLeave(ConvertWindowPointToViewport(client_point),
kenrb07c272802017-02-07 23:48:172158 screen_point);
paulmeyer90f6c31d2016-11-12 00:17:592159}
2160
2161void RenderWidget::OnDragTargetDrop(const DropData& drop_data,
Ella Ge80a52dce2017-11-15 18:01:522162 const gfx::PointF& client_point,
2163 const gfx::PointF& screen_point,
paulmeyer90f6c31d2016-11-12 00:17:592164 int key_modifiers) {
Ehsan Karamadf3ca2a82018-01-20 01:05:252165 blink::WebFrameWidget* frame_widget = GetFrameWidget();
2166 if (!frame_widget)
paulmeyer90f6c31d2016-11-12 00:17:592167 return;
2168
Ehsan Karamadf3ca2a82018-01-20 01:05:252169 frame_widget->DragTargetDrop(DropDataToWebDragData(drop_data),
2170 ConvertWindowPointToViewport(client_point),
2171 screen_point, key_modifiers);
paulmeyer90f6c31d2016-11-12 00:17:592172}
2173
Ella Ge80a52dce2017-11-15 18:01:522174void RenderWidget::OnDragSourceEnded(const gfx::PointF& client_point,
2175 const gfx::PointF& screen_point,
paulmeyer8fc8ea92016-11-15 05:12:212176 WebDragOperation op) {
Ehsan Karamadf3ca2a82018-01-20 01:05:252177 blink::WebFrameWidget* frame_widget = GetFrameWidget();
2178 if (!frame_widget)
paulmeyer8fc8ea92016-11-15 05:12:212179 return;
2180
Ehsan Karamadf3ca2a82018-01-20 01:05:252181 frame_widget->DragSourceEndedAt(ConvertWindowPointToViewport(client_point),
2182 screen_point, op);
paulmeyer8fc8ea92016-11-15 05:12:212183}
2184
2185void RenderWidget::OnDragSourceSystemDragEnded() {
2186 if (!GetWebWidget())
2187 return;
2188
Blink Reformat1c4d759e2017-04-09 16:34:542189 static_cast<WebFrameWidget*>(GetWebWidget())->DragSourceSystemDragEnded();
paulmeyer8fc8ea92016-11-15 05:12:212190}
2191
Blink Reformat1c4d759e2017-04-09 16:34:542192void RenderWidget::ShowVirtualKeyboardOnElementFocus() {
okaa398f50b2017-03-21 06:30:262193#if defined(OS_CHROMEOS)
2194 // On ChromeOS, virtual keyboard is triggered only when users leave the
2195 // mouse button or the finger and a text input element is focused at that
2196 // time. Focus event itself shouldn't trigger virtual keyboard.
2197 UpdateTextInputState();
2198#else
changwan75e3b2072017-01-16 02:55:002199 ShowVirtualKeyboard();
okaa398f50b2017-03-21 06:30:262200#endif
boliu7b2be2f2016-11-04 04:58:312201
2202// TODO(rouslan): Fix ChromeOS and Windows 8 behavior of autofill popup with
2203// virtual keyboard.
2204#if !defined(OS_ANDROID)
2205 FocusChangeComplete();
2206#endif
[email protected]0d1ebed12013-08-05 22:01:132207}
2208
fsamuel72464894f2015-12-15 06:59:312209ui::TextInputType RenderWidget::GetTextInputType() {
brettw4b461082016-11-19 18:55:162210#if BUILDFLAG(ENABLE_PLUGINS)
EhsanK22d482e2017-08-10 17:29:492211 if (auto* plugin = GetFocusedPepperPluginInsideWidget())
2212 return plugin->text_input_type();
ekaramad2a46d632016-07-19 13:33:092213#endif
ekaramad5aff1942017-01-06 01:26:352214 if (auto* controller = GetInputMethodController())
Blink Reformat1c4d759e2017-04-09 16:34:542215 return ConvertWebTextInputType(controller->TextInputType());
fsamuel72464894f2015-12-15 06:59:312216 return ui::TEXT_INPUT_TYPE_NONE;
2217}
2218
nonafa291792016-08-10 02:36:182219void RenderWidget::UpdateCompositionInfo(bool immediate_request) {
2220 if (!monitor_composition_info_ && !immediate_request)
2221 return; // Do not calculate composition info if not requested.
nonadac0c7a2016-08-01 02:30:592222
nonafa291792016-08-10 02:36:182223 TRACE_EVENT0("renderer", "RenderWidget::UpdateCompositionInfo");
2224 gfx::Range range;
2225 std::vector<gfx::Rect> character_bounds;
2226
2227 if (GetTextInputType() == ui::TEXT_INPUT_TYPE_NONE) {
2228 // Composition information is only available on editable node.
2229 range = gfx::Range::InvalidRange();
2230 } else {
2231 GetCompositionRange(&range);
2232 GetCompositionCharacterBounds(&character_bounds);
2233 }
2234
2235 if (!immediate_request &&
2236 !ShouldUpdateCompositionInfo(range, character_bounds)) {
fsamuel72464894f2015-12-15 06:59:312237 return;
nonafa291792016-08-10 02:36:182238 }
fsamuel72464894f2015-12-15 06:59:312239 composition_character_bounds_ = character_bounds;
2240 composition_range_ = range;
Dave Tapuska04bc5ee92018-04-17 19:03:312241 if (mojom::WidgetInputHandlerHost* host =
2242 widget_input_handler_manager_->GetWidgetInputHandlerHost()) {
2243 host->ImeCompositionRangeChanged(composition_range_,
2244 composition_character_bounds_);
Dave Tapuska139a72f2017-09-06 21:57:032245 }
fsamuel72464894f2015-12-15 06:59:312246}
2247
Blink Reformat1c4d759e2017-04-09 16:34:542248void RenderWidget::ConvertViewportToWindow(blink::WebRect* rect) {
danakj4b347212018-07-04 17:55:172249 if (compositor_deps_->IsUseZoomForDSFEnabled()) {
Fady Samuela863f152018-03-09 16:10:032250 float reverse = 1 / GetOriginalScreenInfo().device_scale_factor;
oshimad5279032015-12-16 18:22:332251 // TODO(oshima): We may need to allow pixel precision here as the the
oshimaf866dab2015-12-05 00:41:542252 // anchor element can be placed at half pixel.
lfg15b235a32016-08-25 17:45:462253 gfx::Rect window_rect =
2254 gfx::ScaleToEnclosedRect(gfx::Rect(*rect), reverse);
2255 rect->x = window_rect.x();
2256 rect->y = window_rect.y();
2257 rect->width = window_rect.width();
2258 rect->height = window_rect.height();
oshimaf866dab2015-12-05 00:41:542259 }
2260}
2261
Blink Reformat1c4d759e2017-04-09 16:34:542262void RenderWidget::ConvertWindowToViewport(blink::WebFloatRect* rect) {
danakj4b347212018-07-04 17:55:172263 if (compositor_deps_->IsUseZoomForDSFEnabled()) {
Fady Samuela863f152018-03-09 16:10:032264 rect->x *= GetOriginalScreenInfo().device_scale_factor;
2265 rect->y *= GetOriginalScreenInfo().device_scale_factor;
2266 rect->width *= GetOriginalScreenInfo().device_scale_factor;
2267 rect->height *= GetOriginalScreenInfo().device_scale_factor;
oshimaa6985b62016-01-27 08:58:302268 }
2269}
2270
changwan8c342742016-02-26 00:53:392271void RenderWidget::OnRequestTextInputStateUpdate() {
Dave Tapuska9db80842017-07-24 17:24:262272#if defined(OS_ANDROID)
changwan8c342742016-02-26 00:53:392273 DCHECK(!ime_event_guard_);
2274 UpdateSelectionBounds();
changwan75e3b2072017-01-16 02:55:002275 UpdateTextInputStateInternal(false, true /* reply_to_request */);
[email protected]105dffb42013-02-20 03:46:212276#endif
Dave Tapuska9db80842017-07-24 17:24:262277}
[email protected]105dffb42013-02-20 03:46:212278
ekaramadc9b70a72017-03-23 16:14:232279void RenderWidget::OnRequestCompositionUpdates(bool immediate_request,
2280 bool monitor_updates) {
2281 monitor_composition_info_ = monitor_updates;
nonafa291792016-08-10 02:36:182282 if (!immediate_request)
2283 return;
2284 UpdateCompositionInfo(true /* immediate request */);
2285}
2286
[email protected]fcdc5642014-05-09 14:32:242287void RenderWidget::OnOrientationChange() {
Ehsan Karamad655d7b8a2018-01-12 18:38:382288 if (auto* frame_widget = GetFrameWidget()) {
Alex Moshchuk0b409c42017-09-13 02:09:002289 // LocalRoot() might return null for provisional main frames. In this case,
2290 // the frame hasn't committed a navigation and is not swapped into the tree
2291 // yet, so it doesn't make sense to send orientation change events to it.
2292 //
2293 // TODO(https://crbug.com/578349): This check should be cleaned up
2294 // once provisional frames are gone.
Ehsan Karamad655d7b8a2018-01-12 18:38:382295 if (frame_widget->LocalRoot())
2296 frame_widget->LocalRoot()->SendOrientationChangeEvent();
lfg8d649cc2017-04-28 18:04:302297 }
[email protected]fcdc5642014-05-09 14:32:242298}
2299
[email protected]bee16aab2009-08-26 15:55:032300void RenderWidget::SetHidden(bool hidden) {
2301 if (is_hidden_ == hidden)
2302 return;
2303
jdduke8fac9d102014-12-20 02:40:132304 // The status has changed. Tell the RenderThread about it and ensure
2305 // throttled acks are released in case frame production ceases.
[email protected]bee16aab2009-08-26 15:55:032306 is_hidden_ = hidden;
jdduke8fac9d102014-12-20 02:40:132307
Scott Violet1098538e2017-10-05 19:23:332308#if defined(USE_AURA)
Scott Violete46b3352018-09-22 00:37:072309 if (features::IsMultiProcessMash())
Scott Violet1098538e2017-10-05 19:23:332310 RendererWindowTreeClient::Get(routing_id_)->SetVisible(!hidden);
2311#endif
2312
Saman Sami58ed4392018-01-31 20:54:192313 // RenderThreadImpl::current() could be null in tests.
2314 if (RenderThreadImpl::current()) {
2315 if (is_hidden_) {
2316 RenderThreadImpl::current()->WidgetHidden();
2317 first_update_visual_state_after_hidden_ = true;
2318 } else {
2319 RenderThreadImpl::current()->WidgetRestored();
2320 }
Lei Zhang9b359d32017-11-28 00:57:022321 }
alexclarke7fa93942015-10-21 15:37:112322
2323 if (render_widget_scheduling_state_)
2324 render_widget_scheduling_state_->SetHidden(hidden);
danakj1b35e9d2018-11-07 19:09:282325
danakj57f139f2018-11-19 19:23:462326 // When the RenderWidget is frozen, visibility of the compositor is overridden
2327 // to always be hidden to prevent it from using resources that are not needed.
2328 // Unfortunately the main RenderWidget for a RenderView must be marked visible
2329 // even if the RenderView has a proxy main frame (and the RenderWidget is
2330 // frozen), in order for the RenderView to use the visibility signal from the
2331 // RenderWidget. This is bad. But we don't need to show the compositor to
2332 // satisfy that requirement.
2333 if (!is_frozen_)
2334 layer_tree_view_->SetVisible(!is_hidden_);
[email protected]bee16aab2009-08-26 15:55:032335}
2336
[email protected]2b624c562011-10-27 22:58:262337void RenderWidget::DidToggleFullscreen() {
lfg8ff33912016-09-13 20:59:212338 if (!GetWebWidget())
[email protected]2b624c562011-10-27 22:58:262339 return;
2340
mikhail.pozdnyakovf2c902a2015-04-14 08:09:122341 if (is_fullscreen_granted_) {
Blink Reformat1c4d759e2017-04-09 16:34:542342 GetWebWidget()->DidEnterFullscreen();
[email protected]2b624c562011-10-27 22:58:262343 } else {
Blink Reformat1c4d759e2017-04-09 16:34:542344 GetWebWidget()->DidExitFullscreen();
[email protected]2b624c562011-10-27 22:58:262345 }
[email protected]2b624c562011-10-27 22:58:262346}
2347
changwanf2a707b2015-10-30 08:22:162348void RenderWidget::OnImeEventGuardStart(ImeEventGuard* guard) {
2349 if (!ime_event_guard_)
2350 ime_event_guard_ = guard;
[email protected]66fca5bc2013-05-23 06:58:292351}
2352
changwanf2a707b2015-10-30 08:22:162353void RenderWidget::OnImeEventGuardFinish(ImeEventGuard* guard) {
Jeremy Roman0b261f42018-08-28 15:36:012354 if (ime_event_guard_ != guard)
changwanf2a707b2015-10-30 08:22:162355 return;
changwanf2a707b2015-10-30 08:22:162356 ime_event_guard_ = nullptr;
2357
[email protected]66fca5bc2013-05-23 06:58:292358 // While handling an ime event, text input state and selection bounds updates
2359 // are ignored. These must explicitly be updated once finished handling the
2360 // ime event.
2361 UpdateSelectionBounds();
[email protected]cb9e2632013-06-18 11:26:472362#if defined(OS_ANDROID)
changwan75e3b2072017-01-16 02:55:002363 if (guard->show_virtual_keyboard())
2364 ShowVirtualKeyboard();
2365 else
2366 UpdateTextInputState();
[email protected]cb9e2632013-06-18 11:26:472367#endif
[email protected]66fca5bc2013-05-23 06:58:292368}
2369
[email protected]7c8873e2013-02-05 08:03:012370void RenderWidget::GetSelectionBounds(gfx::Rect* focus, gfx::Rect* anchor) {
brettw4b461082016-11-19 18:55:162371#if BUILDFLAG(ENABLE_PLUGINS)
EhsanK22d482e2017-08-10 17:29:492372 if (auto* plugin = GetFocusedPepperPluginInsideWidget()) {
ekaramad2a46d632016-07-19 13:33:092373 // TODO(kinaba) http://crbug.com/101101
2374 // Current Pepper IME API does not handle selection bounds. So we simply
2375 // use the caret position as an empty range for now. It will be updated
2376 // after Pepper API equips features related to surrounding text retrieval.
EhsanK22d482e2017-08-10 17:29:492377 blink::WebRect caret(plugin->GetCaretBounds());
Blink Reformat1c4d759e2017-04-09 16:34:542378 ConvertViewportToWindow(&caret);
ekaramad2a46d632016-07-19 13:33:092379 *focus = caret;
2380 *anchor = caret;
2381 return;
2382 }
2383#endif
[email protected]7c8873e2013-02-05 08:03:012384 WebRect focus_webrect;
2385 WebRect anchor_webrect;
Blink Reformat1c4d759e2017-04-09 16:34:542386 GetWebWidget()->SelectionBounds(focus_webrect, anchor_webrect);
2387 ConvertViewportToWindow(&focus_webrect);
2388 ConvertViewportToWindow(&anchor_webrect);
oshima33ec97cd2015-12-14 19:40:242389 *focus = focus_webrect;
2390 *anchor = anchor_webrect;
[email protected]73bf95812011-10-12 11:38:322391}
2392
[email protected]e99ef6f2011-10-16 01:13:002393void RenderWidget::UpdateSelectionBounds() {
jdduke1aebad8e2015-07-22 23:25:082394 TRACE_EVENT0("renderer", "RenderWidget::UpdateSelectionBounds");
lfg8ff33912016-09-13 20:59:212395 if (!GetWebWidget())
[email protected]e99ef6f2011-10-16 01:13:002396 return;
changwanf2a707b2015-10-30 08:22:162397 if (ime_event_guard_)
[email protected]66fca5bc2013-05-23 06:58:292398 return;
[email protected]e99ef6f2011-10-16 01:13:002399
mohsenb0eeba72015-08-09 06:20:082400#if defined(USE_AURA)
2401 // TODO(mohsen): For now, always send explicit selection IPC notifications for
2402 // Aura beucause composited selection updates are not working for webview tags
2403 // which regresses IME inside webview. Remove this when composited selection
2404 // updates are fixed for webviews. See, http://crbug.com/510568.
2405 bool send_ipc = true;
2406#else
jddukeacf809e2014-09-23 20:38:382407 // With composited selection updates, the selection bounds will be reported
2408 // directly by the compositor, in which case explicit IPC selection
2409 // notifications should be suppressed.
mohsenb0eeba72015-08-09 06:20:082410 bool send_ipc =
Blink Reformat1c4d759e2017-04-09 16:34:542411 !blink::WebRuntimeFeatures::IsCompositedSelectionUpdateEnabled();
mohsenb0eeba72015-08-09 06:20:082412#endif
2413 if (send_ipc) {
Albert J. Wong3c93c182018-09-27 17:29:432414 WidgetHostMsg_SelectionBounds_Params params;
EhsanK955ba582017-11-30 21:14:402415 params.is_anchor_first = false;
jddukeacf809e2014-09-23 20:38:382416 GetSelectionBounds(&params.anchor_rect, &params.focus_rect);
2417 if (selection_anchor_rect_ != params.anchor_rect ||
2418 selection_focus_rect_ != params.focus_rect) {
2419 selection_anchor_rect_ = params.anchor_rect;
2420 selection_focus_rect_ = params.focus_rect;
EhsanK955ba582017-11-30 21:14:402421 if (auto* focused_frame = GetFocusedWebLocalFrameInWidget()) {
2422 focused_frame->SelectionTextDirection(params.focus_dir,
2423 params.anchor_dir);
2424 params.is_anchor_first = focused_frame->IsSelectionAnchorFirst();
2425 }
Albert J. Wong3c93c182018-09-27 17:29:432426 Send(new WidgetHostMsg_SelectionBoundsChanged(routing_id_, params));
jddukeacf809e2014-09-23 20:38:382427 }
[email protected]58b48a0d2012-06-13 07:01:352428 }
jddukeacf809e2014-09-23 20:38:382429
nonafa291792016-08-10 02:36:182430 UpdateCompositionInfo(false /* not an immediate request */);
[email protected]e99ef6f2011-10-16 01:13:002431}
2432
lfgb00fcad2016-07-14 14:16:332433void RenderWidget::DidAutoResize(const gfx::Size& new_size) {
2434 WebRect new_size_in_window(0, 0, new_size.width(), new_size.height());
Blink Reformat1c4d759e2017-04-09 16:34:542435 ConvertViewportToWindow(&new_size_in_window);
lfgb00fcad2016-07-14 14:16:332436 if (size_.width() != new_size_in_window.width ||
2437 size_.height() != new_size_in_window.height) {
2438 size_ = gfx::Size(new_size_in_window.width, new_size_in_window.height);
2439
2440 if (resizing_mode_selector_->is_synchronous_mode()) {
Blink Reformat1c4d759e2017-04-09 16:34:542441 gfx::Rect new_pos(WindowRect().x, WindowRect().y, size_.width(),
2442 size_.height());
Albert J. Wong3c93c182018-09-27 17:29:432443 widget_screen_rect_ = new_pos;
lfgb00fcad2016-07-14 14:16:332444 window_screen_rect_ = new_pos;
2445 }
2446
Christopher Camerone9a30c12018-03-07 08:23:382447 // TODO(ccameron): Note that this destroys any information differentiating
Christopher Cameronaad15a0d2018-03-09 03:47:332448 // |size_| from |compositor_viewport_pixel_size_|. Also note that the
2449 // calculation of |new_compositor_viewport_pixel_size| does not appear to
2450 // take into account device emulation.
danakja6c10012018-07-06 14:25:362451 if (layer_tree_view_)
2452 layer_tree_view_->RequestNewLocalSurfaceId();
Christopher Cameronaad15a0d2018-03-09 03:47:332453 gfx::Size new_compositor_viewport_pixel_size =
Fady Samuela863f152018-03-09 16:10:032454 gfx::ScaleToCeiledSize(size_, GetWebScreenInfo().device_scale_factor);
Fady Samuel1c1ad3692018-11-06 23:28:402455 UpdateSurfaceAndScreenInfo(local_surface_id_allocation_from_parent_,
2456 new_compositor_viewport_pixel_size,
2457 screen_info_);
lfgb00fcad2016-07-14 14:16:332458 }
2459}
2460
[email protected]58b48a0d2012-06-13 07:01:352461void RenderWidget::GetCompositionCharacterBounds(
2462 std::vector<gfx::Rect>* bounds) {
2463 DCHECK(bounds);
2464 bounds->clear();
ekaramad2a46d632016-07-19 13:33:092465
brettw4b461082016-11-19 18:55:162466#if BUILDFLAG(ENABLE_PLUGINS)
EhsanK22d482e2017-08-10 17:29:492467 if (GetFocusedPepperPluginInsideWidget())
ekaramad2a46d632016-07-19 13:33:092468 return;
2469#endif
2470
Ehsan Karamad4975af6d2018-02-09 16:58:162471 blink::WebInputMethodController* controller = GetInputMethodController();
2472 if (!controller)
ekaramad2a46d632016-07-19 13:33:092473 return;
2474 blink::WebVector<blink::WebRect> bounds_from_blink;
Ehsan Karamad4975af6d2018-02-09 16:58:162475 if (!controller->GetCompositionCharacterBounds(bounds_from_blink))
ekaramad2a46d632016-07-19 13:33:092476 return;
2477
2478 for (size_t i = 0; i < bounds_from_blink.size(); ++i) {
Blink Reformat1c4d759e2017-04-09 16:34:542479 ConvertViewportToWindow(&bounds_from_blink[i]);
ekaramad2a46d632016-07-19 13:33:092480 bounds->push_back(bounds_from_blink[i]);
2481 }
[email protected]58b48a0d2012-06-13 07:01:352482}
2483
[email protected]db4fc1e2013-09-06 20:01:512484void RenderWidget::GetCompositionRange(gfx::Range* range) {
brettw4b461082016-11-19 18:55:162485#if BUILDFLAG(ENABLE_PLUGINS)
EhsanK22d482e2017-08-10 17:29:492486 if (GetFocusedPepperPluginInsideWidget())
ekaramad2a46d632016-07-19 13:33:092487 return;
2488#endif
EhsanKcdee7b72017-11-08 21:30:092489 blink::WebInputMethodController* controller = GetInputMethodController();
2490 WebRange web_range = controller ? controller->CompositionRange() : WebRange();
Blink Reformat1c4d759e2017-04-09 16:34:542491 if (web_range.IsNull()) {
nona2363a3d2016-11-09 03:26:212492 *range = gfx::Range::InvalidRange();
2493 return;
[email protected]88dbe32f2013-06-20 23:31:362494 }
Blink Reformat1c4d759e2017-04-09 16:34:542495 range->set_start(web_range.StartOffset());
2496 range->set_end(web_range.EndOffset());
[email protected]88dbe32f2013-06-20 23:31:362497}
2498
[email protected]501ea13d2013-07-09 17:03:292499bool RenderWidget::ShouldUpdateCompositionInfo(
[email protected]db4fc1e2013-09-06 20:01:512500 const gfx::Range& range,
[email protected]501ea13d2013-07-09 17:03:292501 const std::vector<gfx::Rect>& bounds) {
changwan2418e1b2016-12-12 12:43:082502 if (!range.IsValid())
2503 return false;
[email protected]501ea13d2013-07-09 17:03:292504 if (composition_range_ != range)
2505 return true;
2506 if (bounds.size() != composition_character_bounds_.size())
2507 return true;
2508 for (size_t i = 0; i < bounds.size(); ++i) {
2509 if (bounds[i] != composition_character_bounds_[i])
2510 return true;
2511 }
2512 return false;
2513}
[email protected]501ea13d2013-07-09 17:03:292514
[email protected]ad26ef42011-06-17 07:59:452515bool RenderWidget::CanComposeInline() {
brettw4b461082016-11-19 18:55:162516#if BUILDFLAG(ENABLE_PLUGINS)
EhsanK22d482e2017-08-10 17:29:492517 if (auto* plugin = GetFocusedPepperPluginInsideWidget())
2518 return plugin->IsPluginAcceptingCompositionEvents();
ekaramad2a46d632016-07-19 13:33:092519#endif
[email protected]ad26ef42011-06-17 07:59:452520 return true;
[email protected]56ea1a62011-05-30 07:05:572521}
2522
Blink Reformat1c4d759e2017-04-09 16:34:542523void RenderWidget::DidHandleGestureEvent(const WebGestureEvent& event,
2524 bool event_cancelled) {
danakja2c9d0a92018-07-25 20:01:182525 if (event_cancelled) {
Albert J. Wong7bbf22d2018-12-20 00:27:272526 // The delegate() doesn't need to hear about cancelled events.
[email protected]c68c3e4e2013-01-24 00:36:562527 return;
danakja2c9d0a92018-07-25 20:01:182528 }
2529
2530#if defined(OS_ANDROID) || defined(USE_AURA)
Blink Reformat1c4d759e2017-04-09 16:34:542531 if (event.GetType() == WebInputEvent::kGestureTap) {
changwan75e3b2072017-01-16 02:55:002532 ShowVirtualKeyboard();
Blink Reformat1c4d759e2017-04-09 16:34:542533 } else if (event.GetType() == WebInputEvent::kGestureLongPress) {
lfg8ff33912016-09-13 20:59:212534 DCHECK(GetWebWidget());
ekaramad5aff1942017-01-06 01:26:352535 blink::WebInputMethodController* controller = GetInputMethodController();
Blink Reformat1c4d759e2017-04-09 16:34:542536 if (!controller || controller->TextInputInfo().value.IsEmpty())
changwan75e3b2072017-01-16 02:55:002537 UpdateTextInputState();
[email protected]07c70d22014-08-21 08:33:462538 else
changwan75e3b2072017-01-16 02:55:002539 ShowVirtualKeyboard();
[email protected]c68c3e4e2013-01-24 00:36:562540 }
ekaramada110f642016-12-21 19:47:282541// TODO(ananta): Piggyback off existing IPCs to communicate this information,
2542// crbug/420130.
2543#if defined(OS_WIN)
danakja2c9d0a92018-07-25 20:01:182544 if (event.GetType() == blink::WebGestureEvent::kGestureTap) {
2545 // TODO(estade): hit test the event against focused node to make sure
2546 // the tap actually hit the focused node.
2547 blink::WebInputMethodController* controller = GetInputMethodController();
2548 blink::WebTextInputType text_input_type =
2549 controller ? controller->TextInputType() : blink::kWebTextInputTypeNone;
ekaramada110f642016-12-21 19:47:282550
Albert J. Wong3c93c182018-09-27 17:29:432551 Send(new WidgetHostMsg_FocusedNodeTouched(
danakja2c9d0a92018-07-25 20:01:182552 routing_id_, text_input_type != blink::kWebTextInputTypeNone));
2553 }
ekaramada110f642016-12-21 19:47:282554#endif
[email protected]c68c3e4e2013-01-24 00:36:562555#endif
danakja2c9d0a92018-07-25 20:01:182556
Albert J. Wong7bbf22d2018-12-20 00:27:272557 // The delegate() gets to respond to handling gestures last.
2558 if (delegate())
2559 delegate()->DidHandleGestureEventForWidget(event);
[email protected]c68c3e4e2013-01-24 00:36:562560}
2561
Blink Reformat1c4d759e2017-04-09 16:34:542562void RenderWidget::DidOverscroll(
bokane53a10f2016-04-13 23:48:312563 const blink::WebFloatSize& overscrollDelta,
2564 const blink::WebFloatSize& accumulatedOverscroll,
sataya.m582c9ce2015-06-09 08:03:422565 const blink::WebFloatPoint& position,
sunyunjiabbea8a92017-08-31 11:18:542566 const blink::WebFloatSize& velocity,
danakje819b622018-05-01 20:27:142567 const cc::OverscrollBehavior& behavior) {
bokan731ec382016-04-07 03:16:482568#if defined(OS_MACOSX)
2569 // On OSX the user can disable the elastic overscroll effect. If that's the
2570 // case, don't forward the overscroll notification.
2571 DCHECK(compositor_deps());
2572 if (!compositor_deps()->IsElasticOverscrollEnabled())
2573 return;
2574#endif
bokane53a10f2016-04-13 23:48:312575 input_handler_->DidOverscrollFromBlink(overscrollDelta, accumulatedOverscroll,
sunyunjiabbea8a92017-08-31 11:18:542576 position, velocity, behavior);
sataya.m582c9ce2015-06-09 08:03:422577}
2578
danakj4b347212018-07-04 17:55:172579// static
2580cc::LayerTreeSettings RenderWidget::GenerateLayerTreeSettings(
2581 CompositorDependencies* compositor_deps,
2582 bool is_for_subframe,
2583 const gfx::Size& initial_screen_size,
2584 float initial_device_scale_factor) {
2585 const bool is_threaded =
2586 !!compositor_deps->GetCompositorImplThreadTaskRunner();
2587
2588 const base::CommandLine& cmd = *base::CommandLine::ForCurrentProcess();
2589 cc::LayerTreeSettings settings;
2590
2591 settings.resource_settings.use_r16_texture =
2592 base::FeatureList::IsEnabled(media::kUseR16Texture);
2593
2594 settings.commit_to_active_tree = !is_threaded;
2595 settings.is_layer_tree_for_subframe = is_for_subframe;
2596
2597 // For web contents, layer transforms should scale up the contents of layers
2598 // to keep content always crisp when possible.
2599 settings.layer_transforms_should_scale_layer_contents = true;
2600
2601 settings.main_frame_before_activation_enabled =
2602 cmd.HasSwitch(cc::switches::kEnableMainFrameBeforeActivation);
2603
2604 // Checkerimaging is not supported for synchronous single-threaded mode, which
2605 // is what the renderer uses if its not threaded.
2606 settings.enable_checker_imaging =
2607 !cmd.HasSwitch(cc::switches::kDisableCheckerImaging) && is_threaded;
2608
2609#if defined(OS_ANDROID)
2610 // We can use a more aggressive limit on Android since decodes tend to take
2611 // longer on these devices.
2612 settings.min_image_bytes_to_checker = 512 * 1024; // 512kB
2613
2614 // Re-rasterization of checker-imaged content with software raster can be too
2615 // costly on Android.
2616 settings.only_checker_images_with_gpu_raster = true;
2617#endif
2618
2619 auto switch_value_as_int = [](const base::CommandLine& command_line,
2620 const std::string& switch_string, int min_value,
2621 int max_value, int* result) {
2622 std::string string_value = command_line.GetSwitchValueASCII(switch_string);
2623 int int_value;
2624 if (base::StringToInt(string_value, &int_value) && int_value >= min_value &&
2625 int_value <= max_value) {
2626 *result = int_value;
2627 return true;
2628 } else {
2629 LOG(WARNING) << "Failed to parse switch " << switch_string << ": "
2630 << string_value;
2631 return false;
2632 }
2633 };
2634
2635 int default_tile_size = 256;
2636#if defined(OS_ANDROID)
2637 const gfx::Size screen_size =
2638 gfx::ScaleToFlooredSize(initial_screen_size, initial_device_scale_factor);
2639 int display_width = screen_size.width();
2640 int display_height = screen_size.height();
2641 int numTiles = (display_width * display_height) / (256 * 256);
2642 if (numTiles > 16)
2643 default_tile_size = 384;
2644 if (numTiles >= 40)
2645 default_tile_size = 512;
2646
2647 // Adjust for some resolutions that barely straddle an extra
2648 // tile when in portrait mode. This helps worst case scroll/raster
2649 // by not needing a full extra tile for each row.
2650 constexpr int tolerance = 10; // To avoid rounding errors.
2651 int portrait_width = std::min(display_width, display_height);
2652 if (default_tile_size == 256 && std::abs(portrait_width - 768) < tolerance)
2653 default_tile_size += 32;
2654 if (default_tile_size == 384 && std::abs(portrait_width - 1200) < tolerance)
2655 default_tile_size += 32;
2656#elif defined(OS_CHROMEOS) || defined(OS_MACOSX)
2657 // Use 512 for high DPI (dsf=2.0f) devices.
2658 if (initial_device_scale_factor >= 2.0f)
2659 default_tile_size = 512;
2660#endif
2661
2662 // TODO(danakj): This should not be a setting O_O; it should change when the
2663 // device scale factor on LayerTreeHost changes.
2664 settings.default_tile_size = gfx::Size(default_tile_size, default_tile_size);
2665 if (cmd.HasSwitch(switches::kDefaultTileWidth)) {
2666 int tile_width = 0;
2667 switch_value_as_int(cmd, switches::kDefaultTileWidth, 1,
2668 std::numeric_limits<int>::max(), &tile_width);
2669 settings.default_tile_size.set_width(tile_width);
2670 }
2671 if (cmd.HasSwitch(switches::kDefaultTileHeight)) {
2672 int tile_height = 0;
2673 switch_value_as_int(cmd, switches::kDefaultTileHeight, 1,
2674 std::numeric_limits<int>::max(), &tile_height);
2675 settings.default_tile_size.set_height(tile_height);
2676 }
2677
2678 int max_untiled_layer_width = settings.max_untiled_layer_size.width();
2679 if (cmd.HasSwitch(switches::kMaxUntiledLayerWidth)) {
2680 switch_value_as_int(cmd, switches::kMaxUntiledLayerWidth, 1,
2681 std::numeric_limits<int>::max(),
2682 &max_untiled_layer_width);
2683 }
2684 int max_untiled_layer_height = settings.max_untiled_layer_size.height();
2685 if (cmd.HasSwitch(switches::kMaxUntiledLayerHeight)) {
2686 switch_value_as_int(cmd, switches::kMaxUntiledLayerHeight, 1,
2687 std::numeric_limits<int>::max(),
2688 &max_untiled_layer_height);
2689 }
2690
2691 settings.max_untiled_layer_size =
2692 gfx::Size(max_untiled_layer_width, max_untiled_layer_height);
2693
2694 settings.gpu_rasterization_msaa_sample_count =
2695 compositor_deps->GetGpuRasterizationMSAASampleCount();
2696 settings.gpu_rasterization_forced =
2697 compositor_deps->IsGpuRasterizationForced();
2698
2699 settings.can_use_lcd_text = compositor_deps->IsLcdTextEnabled();
2700 settings.use_zero_copy = compositor_deps->IsZeroCopyEnabled();
2701 settings.use_partial_raster = compositor_deps->IsPartialRasterEnabled();
2702 settings.enable_elastic_overscroll =
2703 compositor_deps->IsElasticOverscrollEnabled();
2704 settings.resource_settings.use_gpu_memory_buffer_resources =
2705 compositor_deps->IsGpuMemoryBufferCompositorResourcesEnabled();
2706 settings.use_painted_device_scale_factor =
2707 compositor_deps->IsUseZoomForDSFEnabled();
2708
2709 // Build LayerTreeSettings from command line args.
2710 if (cmd.HasSwitch(cc::switches::kBrowserControlsShowThreshold)) {
2711 std::string top_threshold_str =
2712 cmd.GetSwitchValueASCII(cc::switches::kBrowserControlsShowThreshold);
2713 double show_threshold;
2714 if (base::StringToDouble(top_threshold_str, &show_threshold) &&
2715 show_threshold >= 0.f && show_threshold <= 1.f)
2716 settings.top_controls_show_threshold = show_threshold;
2717 }
2718
2719 if (cmd.HasSwitch(cc::switches::kBrowserControlsHideThreshold)) {
2720 std::string top_threshold_str =
2721 cmd.GetSwitchValueASCII(cc::switches::kBrowserControlsHideThreshold);
2722 double hide_threshold;
2723 if (base::StringToDouble(top_threshold_str, &hide_threshold) &&
2724 hide_threshold >= 0.f && hide_threshold <= 1.f)
2725 settings.top_controls_hide_threshold = hide_threshold;
2726 }
2727
Philip Rogersc55ab6a2018-12-05 17:29:192728 // Blink sends cc a layer list and property trees when either
2729 // BlinkGenPropertyTrees or CompositeAfterPaint are enabled.
Philip Rogersfc2f1162018-10-17 18:28:312730 settings.use_layer_lists =
2731 blink::WebRuntimeFeatures::IsBlinkGenPropertyTreesEnabled() ||
Philip Rogersc55ab6a2018-12-05 17:29:192732 blink::WebRuntimeFeatures::IsCompositeAfterPaintEnabled();
danakj4b347212018-07-04 17:55:172733
David Bokan163b6a72018-10-25 22:03:492734 // Blink currently doesn't support setting fractional scroll offsets so CC
2735 // must send integer values. We plan to eventually make Blink use fractional
2736 // offsets internally: https://crbug.com/414283.
2737 settings.commit_fractional_scroll_deltas =
2738 blink::WebRuntimeFeatures::IsFractionalScrollOffsetsEnabled();
2739
danakj4b347212018-07-04 17:55:172740 // The means the renderer compositor has 2 possible modes:
2741 // - Threaded compositing with a scheduler.
Kent Tamura21d1de62018-12-10 04:45:202742 // - Single threaded compositing without a scheduler (for web tests only).
2743 // Using the scheduler in web tests introduces additional composite steps
danakj4b347212018-07-04 17:55:172744 // that create flakiness.
2745 settings.single_thread_proxy_scheduler = false;
2746
2747 // These flags should be mirrored by UI versions in ui/compositor/.
2748 if (cmd.HasSwitch(cc::switches::kShowCompositedLayerBorders))
2749 settings.initial_debug_state.show_debug_borders.set();
2750 settings.initial_debug_state.show_layer_animation_bounds_rects =
2751 cmd.HasSwitch(cc::switches::kShowLayerAnimationBounds);
2752 settings.initial_debug_state.show_paint_rects =
2753 cmd.HasSwitch(switches::kShowPaintRects);
2754 settings.initial_debug_state.show_property_changed_rects =
2755 cmd.HasSwitch(cc::switches::kShowPropertyChangedRects);
2756 settings.initial_debug_state.show_surface_damage_rects =
2757 cmd.HasSwitch(cc::switches::kShowSurfaceDamageRects);
2758 settings.initial_debug_state.show_screen_space_rects =
2759 cmd.HasSwitch(cc::switches::kShowScreenSpaceRects);
2760
2761 settings.initial_debug_state.SetRecordRenderingStats(
2762 cmd.HasSwitch(cc::switches::kEnableGpuBenchmarking));
2763 settings.enable_surface_synchronization =
2764 features::IsSurfaceSynchronizationEnabled();
2765 settings.build_hit_test_data = features::IsVizHitTestingSurfaceLayerEnabled();
2766
2767 if (cmd.HasSwitch(cc::switches::kSlowDownRasterScaleFactor)) {
2768 const int kMinSlowDownScaleFactor = 0;
2769 const int kMaxSlowDownScaleFactor = INT_MAX;
2770 switch_value_as_int(
2771 cmd, cc::switches::kSlowDownRasterScaleFactor, kMinSlowDownScaleFactor,
2772 kMaxSlowDownScaleFactor,
2773 &settings.initial_debug_state.slow_down_raster_scale_factor);
2774 }
2775
2776 // This is default overlay scrollbar settings for Android and DevTools mobile
2777 // emulator. Aura Overlay Scrollbar will override below.
2778 settings.scrollbar_animator = cc::LayerTreeSettings::ANDROID_OVERLAY;
2779 settings.solid_color_scrollbar_color = SkColorSetARGB(128, 128, 128, 128);
2780 settings.scrollbar_fade_delay = base::TimeDelta::FromMilliseconds(300);
2781 settings.scrollbar_fade_duration = base::TimeDelta::FromMilliseconds(300);
2782
2783#if defined(OS_ANDROID)
2784 bool using_synchronous_compositor =
2785 compositor_deps->UsingSynchronousCompositing();
2786 bool using_low_memory_policy = base::SysInfo::IsLowEndDevice();
2787
2788 settings.use_stream_video_draw_quad = true;
2789 settings.using_synchronous_renderer_compositor = using_synchronous_compositor;
2790 if (using_synchronous_compositor) {
2791 // Android WebView uses system scrollbars, so make ours invisible.
2792 // http://crbug.com/677348: This can't be done using hide_scrollbars
2793 // setting because supporting -webkit custom scrollbars is still desired
2794 // on sublayers.
2795 settings.scrollbar_animator = cc::LayerTreeSettings::NO_ANIMATOR;
2796 settings.solid_color_scrollbar_color = SK_ColorTRANSPARENT;
2797
2798 settings.enable_early_damage_check =
2799 cmd.HasSwitch(cc::switches::kCheckDamageEarly);
2800 }
2801 // Android WebView handles root layer flings itself.
2802 settings.ignore_root_layer_flings = using_synchronous_compositor;
2803 // Memory policy on Android WebView does not depend on whether device is
2804 // low end, so always use default policy.
2805 if (using_low_memory_policy && !using_synchronous_compositor) {
2806 // On low-end we want to be very carefull about killing other
2807 // apps. So initially we use 50% more memory to avoid flickering
2808 // or raster-on-demand.
2809 settings.max_memory_for_prepaint_percentage = 67;
2810 } else {
2811 // On other devices we have increased memory excessively to avoid
2812 // raster-on-demand already, so now we reserve 50% _only_ to avoid
2813 // raster-on-demand, and use 50% of the memory otherwise.
2814 settings.max_memory_for_prepaint_percentage = 50;
2815 }
2816
2817 // TODO(danakj): Only do this on low end devices.
2818 settings.create_low_res_tiling = true;
2819
2820#else // defined(OS_ANDROID)
2821 bool using_synchronous_compositor = false; // Only for Android WebView.
2822 // On desktop, we never use the low memory policy unless we are simulating
2823 // low-end mode via a switch.
2824 bool using_low_memory_policy =
2825 cmd.HasSwitch(switches::kEnableLowEndDeviceMode);
2826
2827 if (ui::IsOverlayScrollbarEnabled()) {
2828 settings.scrollbar_animator = cc::LayerTreeSettings::AURA_OVERLAY;
2829 settings.scrollbar_fade_delay = ui::kOverlayScrollbarFadeDelay;
2830 settings.scrollbar_fade_duration = ui::kOverlayScrollbarFadeDuration;
2831 settings.scrollbar_thinning_duration =
2832 ui::kOverlayScrollbarThinningDuration;
2833 settings.scrollbar_flash_after_any_scroll_update =
2834 ui::OverlayScrollbarFlashAfterAnyScrollUpdate();
2835 settings.scrollbar_flash_when_mouse_enter =
2836 ui::OverlayScrollbarFlashWhenMouseEnter();
2837 }
2838
2839 // On desktop, if there's over 4GB of memory on the machine, increase the
2840 // working set size to 256MB for both gpu and software.
2841 const int kImageDecodeMemoryThresholdMB = 4 * 1024;
2842 if (base::SysInfo::AmountOfPhysicalMemoryMB() >=
2843 kImageDecodeMemoryThresholdMB) {
2844 settings.decoded_image_working_set_budget_bytes = 256 * 1024 * 1024;
2845 } else {
2846 // This is the default, but recorded here as well.
2847 settings.decoded_image_working_set_budget_bytes = 128 * 1024 * 1024;
2848 }
2849#endif // defined(OS_ANDROID)
2850
2851 if (using_low_memory_policy) {
2852 // RGBA_4444 textures are only enabled:
2853 // - If the user hasn't explicitly disabled them
2854 // - If system ram is <= 512MB (1GB devices are sometimes low-end).
2855 // - If we are not running in a WebView, where 4444 isn't supported.
2856 if (!cmd.HasSwitch(switches::kDisableRGBA4444Textures) &&
2857 base::SysInfo::AmountOfPhysicalMemoryMB() <= 512 &&
2858 !using_synchronous_compositor) {
2859 settings.use_rgba_4444 = viz::RGBA_4444;
2860
2861 // If we are going to unpremultiply and dither these tiles, we need to
2862 // allocate an additional RGBA_8888 intermediate for each tile
2863 // rasterization when rastering to RGBA_4444 to allow for dithering.
2864 // Setting a reasonable sized max tile size allows this intermediate to
2865 // be consistently reused.
2866 if (base::FeatureList::IsEnabled(
2867 kUnpremultiplyAndDitherLowBitDepthTiles)) {
2868 settings.max_gpu_raster_tile_size = gfx::Size(512, 256);
2869 settings.unpremultiply_and_dither_low_bit_depth_tiles = true;
2870 }
2871 }
2872 }
2873
2874 if (cmd.HasSwitch(switches::kEnableLowResTiling))
2875 settings.create_low_res_tiling = true;
2876 if (cmd.HasSwitch(switches::kDisableLowResTiling))
2877 settings.create_low_res_tiling = false;
2878
2879 if (cmd.HasSwitch(switches::kEnableRGBA4444Textures) &&
2880 !cmd.HasSwitch(switches::kDisableRGBA4444Textures)) {
2881 settings.use_rgba_4444 = true;
2882 }
2883
2884 settings.max_staging_buffer_usage_in_bytes = 32 * 1024 * 1024; // 32MB
2885 // Use 1/4th of staging buffers on low-end devices.
2886 if (base::SysInfo::IsLowEndDevice())
2887 settings.max_staging_buffer_usage_in_bytes /= 4;
2888
2889 cc::ManagedMemoryPolicy defaults = settings.memory_policy;
2890 settings.memory_policy = GetGpuMemoryPolicy(defaults, initial_screen_size,
2891 initial_device_scale_factor);
2892
2893 settings.disallow_non_exact_resource_reuse =
2894 cmd.HasSwitch(switches::kDisallowNonExactResourceReuse);
2895#if defined(OS_ANDROID)
2896 // TODO(crbug.com/746931): This feature appears to be causing visual
2897 // corruption on certain android devices. Will investigate and re-enable.
2898 settings.disallow_non_exact_resource_reuse = true;
2899#endif
2900
2901 if (cmd.HasSwitch(switches::kRunAllCompositorStagesBeforeDraw)) {
2902 settings.wait_for_all_pipeline_stages_before_draw = true;
2903 settings.enable_latency_recovery = false;
2904 }
2905
2906 settings.enable_image_animation_resync =
2907 !cmd.HasSwitch(switches::kDisableImageAnimationResync);
2908
2909 settings.always_request_presentation_time =
2910 cmd.HasSwitch(cc::switches::kAlwaysRequestPresentationTime);
Karolina Soltys4d60b402018-10-01 19:28:202911
2912 settings.send_compositor_frame_ack = false;
2913
danakj4b347212018-07-04 17:55:172914 return settings;
2915}
2916
2917// static
2918cc::ManagedMemoryPolicy RenderWidget::GetGpuMemoryPolicy(
2919 const cc::ManagedMemoryPolicy& default_policy,
2920 const gfx::Size& initial_screen_size,
2921 float initial_device_scale_factor) {
2922 cc::ManagedMemoryPolicy actual = default_policy;
2923 actual.bytes_limit_when_visible = 0;
2924
2925 // If the value was overridden on the command line, use the specified value.
2926 static bool client_hard_limit_bytes_overridden =
2927 base::CommandLine::ForCurrentProcess()->HasSwitch(
2928 switches::kForceGpuMemAvailableMb);
2929 if (client_hard_limit_bytes_overridden) {
2930 if (base::StringToSizeT(
2931 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
2932 switches::kForceGpuMemAvailableMb),
2933 &actual.bytes_limit_when_visible))
2934 actual.bytes_limit_when_visible *= 1024 * 1024;
2935 return actual;
2936 }
2937
2938#if defined(OS_ANDROID)
2939 // We can't query available GPU memory from the system on Android.
2940 // Physical memory is also mis-reported sometimes (eg. Nexus 10 reports
2941 // 1262MB when it actually has 2GB, while Razr M has 1GB but only reports
2942 // 128MB java heap size). First we estimate physical memory using both.
2943 size_t dalvik_mb = base::SysInfo::DalvikHeapSizeMB();
2944 size_t physical_mb = base::SysInfo::AmountOfPhysicalMemoryMB();
2945 size_t physical_memory_mb = 0;
2946 if (base::SysInfo::IsLowEndDevice()) {
2947 // TODO(crbug.com/742534): The code below appears to no longer work.
2948 // |dalvik_mb| no longer follows the expected heuristic pattern, causing us
2949 // to over-estimate memory on low-end devices. This entire section probably
2950 // needs to be re-written, but for now we can address the low-end Android
2951 // issues by ignoring |dalvik_mb|.
2952 physical_memory_mb = physical_mb;
2953 } else if (dalvik_mb >= 256) {
2954 physical_memory_mb = dalvik_mb * 4;
2955 } else {
2956 physical_memory_mb = std::max(dalvik_mb * 4, (physical_mb * 4) / 3);
2957 }
2958
2959 // Now we take a default of 1/8th of memory on high-memory devices,
2960 // and gradually scale that back for low-memory devices (to be nicer
2961 // to other apps so they don't get killed). Examples:
2962 // Nexus 4/10(2GB) 256MB (normally 128MB)
2963 // Droid Razr M(1GB) 114MB (normally 57MB)
2964 // Galaxy Nexus(1GB) 100MB (normally 50MB)
2965 // Xoom(1GB) 100MB (normally 50MB)
2966 // Nexus S(low-end) 8MB (normally 8MB)
2967 // Note that the compositor now uses only some of this memory for
2968 // pre-painting and uses the rest only for 'emergencies'.
2969 if (actual.bytes_limit_when_visible == 0) {
2970 // NOTE: Non-low-end devices use only 50% of these limits,
2971 // except during 'emergencies' where 100% can be used.
2972 if (physical_memory_mb >= 1536)
2973 actual.bytes_limit_when_visible = physical_memory_mb / 8; // >192MB
2974 else if (physical_memory_mb >= 1152)
2975 actual.bytes_limit_when_visible = physical_memory_mb / 8; // >144MB
2976 else if (physical_memory_mb >= 768)
2977 actual.bytes_limit_when_visible = physical_memory_mb / 10; // >76MB
2978 else if (physical_memory_mb >= 513)
2979 actual.bytes_limit_when_visible = physical_memory_mb / 12; // <64MB
2980 else
2981 // Devices with this little RAM have very little headroom so we hardcode
2982 // the limit rather than relying on the heuristics above. (They also use
2983 // 4444 textures so we can use a lower limit.)
2984 actual.bytes_limit_when_visible = 8;
2985
2986 actual.bytes_limit_when_visible =
2987 actual.bytes_limit_when_visible * 1024 * 1024;
2988 // Clamp the observed value to a specific range on Android.
2989 actual.bytes_limit_when_visible = std::max(
2990 actual.bytes_limit_when_visible, static_cast<size_t>(8 * 1024 * 1024));
2991 actual.bytes_limit_when_visible =
2992 std::min(actual.bytes_limit_when_visible,
2993 static_cast<size_t>(256 * 1024 * 1024));
2994 }
2995 actual.priority_cutoff_when_visible =
2996 gpu::MemoryAllocation::CUTOFF_ALLOW_EVERYTHING;
2997#else
2998 // Ignore what the system said and give all clients the same maximum
2999 // allocation on desktop platforms.
3000 actual.bytes_limit_when_visible = 512 * 1024 * 1024;
3001 actual.priority_cutoff_when_visible =
3002 gpu::MemoryAllocation::CUTOFF_ALLOW_NICE_TO_HAVE;
3003
3004 // For large monitors (4k), double the tile memory to avoid frequent out of
3005 // memory problems. 4k could mean a screen width of anywhere from 3840 to 4096
3006 // (see https://en.wikipedia.org/wiki/4K_resolution). We use 3500 as a proxy
3007 // for "large enough".
3008 static const int kLargeDisplayThreshold = 3500;
3009 int display_width =
3010 std::round(initial_screen_size.width() * initial_device_scale_factor);
3011 if (display_width >= kLargeDisplayThreshold)
3012 actual.bytes_limit_when_visible *= 2;
3013#endif
3014 return actual;
3015}
3016
Navid Zolghadr8fdb6114e2018-08-29 19:00:543017void RenderWidget::HasPointerRawMoveEventHandlers(bool has_handlers) {
3018 if (input_event_queue_)
3019 input_event_queue_->HasPointerRawMoveEventHandlers(has_handlers);
3020}
3021
Blink Reformat1c4d759e2017-04-09 16:34:543022void RenderWidget::HasTouchEventHandlers(bool has_handlers) {
W. James MacLean662d5382018-03-29 18:33:083023 if (has_touch_handlers_ && *has_touch_handlers_ == has_handlers)
3024 return;
3025
3026 has_touch_handlers_ = has_handlers;
alexclarke7fa93942015-10-21 15:37:113027 if (render_widget_scheduling_state_)
3028 render_widget_scheduling_state_->SetHasTouchHandler(has_handlers);
Albert J. Wongeb28e7e2018-10-12 00:57:433029 Send(new WidgetHostMsg_HasTouchEventHandlers(routing_id_, has_handlers));
[email protected]ce6689f2013-03-29 12:52:553030}
3031
Dave Tapuskadfe486c12017-06-09 16:53:133032void RenderWidget::SetNeedsLowLatencyInput(bool needs_low_latency) {
3033 if (input_event_queue_)
3034 input_event_queue_->SetNeedsLowLatency(needs_low_latency);
3035}
3036
W. James MacLeand973a55b2018-11-29 21:39:133037void RenderWidget::AnimateDoubleTapZoomInMainFrame(
3038 const blink::WebPoint& point,
3039 const blink::WebRect& rect_to_zoom) {
3040 // Only oopif subframes should be sending this message.
Albert J. Wong7bbf22d2018-12-20 00:27:273041 DCHECK(!delegate());
W. James MacLeand973a55b2018-11-29 21:39:133042 Send(new WidgetHostMsg_AnimateDoubleTapZoomInMainFrame(routing_id(), point,
3043 rect_to_zoom));
3044}
3045
W. James MacLean5372eb72018-12-19 12:56:363046void RenderWidget::ZoomToFindInPageRectInMainFrame(
3047 const blink::WebRect& rect_to_zoom) {
3048 // Only oopif subframes should be sending this message.
Albert J. Wong7bbf22d2018-12-20 00:27:273049 DCHECK(!delegate_);
W. James MacLean5372eb72018-12-19 12:56:363050 Send(new WidgetHostMsg_ZoomToFindInPageRectInMainFrame(routing_id(),
3051 rect_to_zoom));
3052}
3053
Dave Tapuskabafc2ba32017-11-28 01:54:373054void RenderWidget::RequestUnbufferedInputEvents() {
3055 if (input_event_queue_)
3056 input_event_queue_->RequestUnbufferedInputEvents();
3057}
3058
xidachenfa0199e72017-05-11 11:34:263059void RenderWidget::SetTouchAction(cc::TouchAction touch_action) {
Dave Tapuskab28a1082017-08-30 15:37:263060 if (!input_handler_->ProcessTouchAction(touch_action))
[email protected]5d0bbdfa92013-12-10 00:35:513061 return;
3062
Dave Tapuska04bc5ee92018-04-17 19:03:313063 widget_input_handler_manager_->ProcessTouchAction(touch_action);
[email protected]5d0bbdfa92013-12-10 00:35:513064}
3065
[email protected]e3244ed2014-06-20 20:04:273066void RenderWidget::RegisterRenderFrameProxy(RenderFrameProxy* proxy) {
3067 render_frame_proxies_.AddObserver(proxy);
W. James MacLean2a90bff2018-11-05 20:52:473068 // During initial page load, the main frame may have received page
3069 // scale factor information before the sub-frame proxies were registered, so
3070 // make sure we pass the page scale factor along here.
3071 proxy->OnPageScaleFactorChanged(page_scale_factor_from_mainframe_);
[email protected]bffc8302014-01-23 20:52:163072}
3073
[email protected]e3244ed2014-06-20 20:04:273074void RenderWidget::UnregisterRenderFrameProxy(RenderFrameProxy* proxy) {
3075 render_frame_proxies_.RemoveObserver(proxy);
[email protected]bffc8302014-01-23 20:52:163076}
3077
[email protected]de3c5d82014-05-28 22:12:593078void RenderWidget::RegisterRenderFrame(RenderFrameImpl* frame) {
3079 render_frames_.AddObserver(frame);
3080}
3081
3082void RenderWidget::UnregisterRenderFrame(RenderFrameImpl* frame) {
3083 render_frames_.RemoveObserver(frame);
3084}
3085
Saman Samid189e5dfd2017-12-20 22:55:313086void RenderWidget::RegisterBrowserPlugin(BrowserPlugin* browser_plugin) {
3087 browser_plugins_.AddObserver(browser_plugin);
Saman Sami50d1e0c2018-03-13 20:03:493088 browser_plugin->ScreenInfoChanged(GetOriginalScreenInfo());
Saman Samid189e5dfd2017-12-20 22:55:313089}
3090
3091void RenderWidget::UnregisterBrowserPlugin(BrowserPlugin* browser_plugin) {
3092 browser_plugins_.RemoveObserver(browser_plugin);
3093}
3094
Albert J. Wong3c93c182018-09-27 17:29:433095void RenderWidget::OnWaitNextFrameForTests(
3096 int main_frame_thread_observer_routing_id) {
danakj53802692018-07-25 21:46:443097 // Sends an ACK to the browser process during the next compositor frame.
Albert J. Wong3c93c182018-09-27 17:29:433098 QueueMessage(new WidgetHostMsg_WaitForNextFrameForTests_ACK(
3099 main_frame_thread_observer_routing_id));
lfg43e08e62016-02-03 18:51:373100}
3101
Fady Samuela863f152018-03-09 16:10:033102const ScreenInfo& RenderWidget::GetWebScreenInfo() const {
3103 return screen_info_;
Christopher Camerone9a30c12018-03-07 08:23:383104}
3105
Fady Samuela863f152018-03-09 16:10:033106const ScreenInfo& RenderWidget::GetOriginalScreenInfo() const {
Christopher Camerone9a30c12018-03-07 08:23:383107 return screen_metrics_emulator_
3108 ? screen_metrics_emulator_->original_screen_info()
Fady Samuela863f152018-03-09 16:10:033109 : screen_info_;
oshima50872a72016-03-04 13:26:183110}
3111
Ella Ge80a52dce2017-11-15 18:01:523112gfx::PointF RenderWidget::ConvertWindowPointToViewport(
3113 const gfx::PointF& point) {
paulmeyer90f6c31d2016-11-12 00:17:593114 blink::WebFloatRect point_in_viewport(point.x(), point.y(), 0, 0);
Blink Reformat1c4d759e2017-04-09 16:34:543115 ConvertWindowToViewport(&point_in_viewport);
Ella Ge80a52dce2017-11-15 18:01:523116 return gfx::PointF(point_in_viewport.x, point_in_viewport.y);
3117}
3118
3119gfx::Point RenderWidget::ConvertWindowPointToViewport(const gfx::Point& point) {
3120 return gfx::ToRoundedPoint(ConvertWindowPointToViewport(gfx::PointF(point)));
paulmeyer90f6c31d2016-11-12 00:17:593121}
3122
Blink Reformat1c4d759e2017-04-09 16:34:543123bool RenderWidget::RequestPointerLock() {
lfgbee1e0a2016-06-08 21:24:213124 return mouse_lock_dispatcher_->LockMouse(webwidget_mouse_lock_target_.get());
3125}
3126
Blink Reformat1c4d759e2017-04-09 16:34:543127void RenderWidget::RequestPointerUnlock() {
lfgbee1e0a2016-06-08 21:24:213128 mouse_lock_dispatcher_->UnlockMouse(webwidget_mouse_lock_target_.get());
3129}
3130
Blink Reformat1c4d759e2017-04-09 16:34:543131bool RenderWidget::IsPointerLocked() {
lfgbee1e0a2016-06-08 21:24:213132 return mouse_lock_dispatcher_->IsMouseLockedTo(
3133 webwidget_mouse_lock_target_.get());
3134}
3135
Richard Li49fe04d2018-10-21 09:07:193136void RenderWidget::StartDragging(network::mojom::ReferrerPolicy policy,
paulmeyer6ef5a792016-11-08 20:33:583137 const WebDragData& data,
3138 WebDragOperationsMask mask,
danakj0c75ad82018-07-10 19:50:123139 const SkBitmap& drag_image,
danakjef1735a2018-11-13 19:34:443140 const gfx::Point& web_image_offset) {
3141 blink::WebRect offset_in_window(web_image_offset.x(), web_image_offset.y(), 0,
3142 0);
Blink Reformat1c4d759e2017-04-09 16:34:543143 ConvertViewportToWindow(&offset_in_window);
paulmeyer6ef5a792016-11-08 20:33:583144 DropData drop_data(DropDataBuilder::Build(data));
3145 drop_data.referrer_policy = policy;
danakj0c75ad82018-07-10 19:50:123146 gfx::Vector2d image_offset(offset_in_window.x, offset_in_window.y);
3147 Send(new DragHostMsg_StartDragging(routing_id(), drop_data, mask, drag_image,
3148 image_offset, possible_drag_event_info_));
paulmeyer6ef5a792016-11-08 20:33:583149}
3150
kenrb5d78b842017-03-06 21:06:013151uint32_t RenderWidget::GetContentSourceId() {
3152 return current_content_source_id_;
3153}
3154
Saman Samif7731342018-01-24 22:18:443155void RenderWidget::DidNavigate() {
Saman Sami26a1fcd2018-04-10 17:12:213156 ++current_content_source_id_;
danakja6c10012018-07-06 14:25:363157 if (!layer_tree_view_)
Saman Samif7731342018-01-24 22:18:443158 return;
danakja6c10012018-07-06 14:25:363159 layer_tree_view_->SetContentSourceId(current_content_source_id_);
3160 layer_tree_view_->ClearCachesOnNextCommit();
kenrb5d78b842017-03-06 21:06:013161}
3162
lfg8ff33912016-09-13 20:59:213163blink::WebWidget* RenderWidget::GetWebWidget() const {
Albert J. Wong7bbf22d2018-12-20 00:27:273164 if (delegate()) {
3165 blink::WebWidget* delegate_widget = delegate()->GetWebWidgetForWidget();
danakja2c9d0a92018-07-25 20:01:183166 if (delegate_widget)
3167 return delegate_widget;
3168 }
lfg8ff33912016-09-13 20:59:213169 return webwidget_internal_;
3170}
3171
ekaramad2daaf672016-11-10 20:29:013172blink::WebInputMethodController* RenderWidget::GetInputMethodController()
3173 const {
Ehsan Karamad655d7b8a2018-01-12 18:38:383174 if (auto* frame_widget = GetFrameWidget())
3175 return frame_widget->GetActiveWebInputMethodController();
3176
3177 return nullptr;
ekaramad2daaf672016-11-10 20:29:013178}
3179
Dave Tapuska139a72f2017-09-06 21:57:033180void RenderWidget::SetupWidgetInputHandler(
3181 mojom::WidgetInputHandlerRequest request,
3182 mojom::WidgetInputHandlerHostPtr host) {
Dave Tapuskab66c28f2017-11-15 17:18:473183 widget_input_handler_manager_->AddInterface(std::move(request),
3184 std::move(host));
Dave Tapuska525eb15e2017-08-17 21:05:503185}
3186
Dave Tapuska485aca92017-08-08 00:47:583187void RenderWidget::SetWidgetBinding(mojom::WidgetRequest request) {
3188 // Close the old binding if there was one.
3189 // A RenderWidgetHost should not need more than one channel.
3190 widget_binding_.Close();
3191 widget_binding_.Bind(std::move(request));
3192}
3193
Ken Buchanan94c0beb62018-06-22 19:56:243194void RenderWidget::SetMouseCapture(bool capture) {
3195 if (mojom::WidgetInputHandlerHost* host =
3196 widget_input_handler_manager_->GetWidgetInputHandlerHost()) {
3197 host->SetMouseCapture(capture);
3198 }
3199}
3200
danakj53802692018-07-25 21:46:443201void RenderWidget::SetWindowScreenRect(const gfx::Rect& window_screen_rect) {
3202 if (screen_metrics_emulator_)
3203 screen_metrics_emulator_->OnUpdateWindowScreenRect(window_screen_rect);
3204 else
3205 window_screen_rect_ = window_screen_rect;
3206}
3207
Fady Samuelca9ecb72018-05-05 05:59:273208bool RenderWidget::IsSurfaceSynchronizationEnabled() const {
danakja6c10012018-07-06 14:25:363209 return layer_tree_view_ &&
3210 layer_tree_view_->IsSurfaceSynchronizationEnabled();
Fady Samuelca9ecb72018-05-05 05:59:273211}
3212
W. James MacLean2a90bff2018-11-05 20:52:473213void RenderWidget::PageScaleFactorChanged(float page_scale_factor) {
3214 // Only the main frame pulls page scale information from the layer tree host.
3215 // Pages scale is shared with non-mainframe widgets via the IPC for
3216 // OnSynchronizeVisualProperties.
Albert J. Wong7bbf22d2018-12-20 00:27:273217 if (!delegate())
W. James MacLean2a90bff2018-11-05 20:52:473218 return;
3219
3220 page_scale_factor_from_mainframe_ = page_scale_factor;
3221 for (auto& observer : render_frame_proxies_)
3222 observer.OnPageScaleFactorChanged(page_scale_factor);
3223}
3224
danakj53802692018-07-25 21:46:443225void RenderWidget::UseSynchronousResizeModeForTesting(bool enable) {
3226 resizing_mode_selector_->set_is_synchronous_mode(enable);
danakja2c9d0a92018-07-25 20:01:183227}
3228
3229void RenderWidget::SetDeviceScaleFactorForTesting(float factor) {
3230 device_scale_factor_for_testing_ = factor;
3231
3232 VisualProperties visual_properties;
3233 visual_properties.screen_info = screen_info_;
3234 visual_properties.screen_info.device_scale_factor = factor;
3235 visual_properties.new_size = size();
3236 visual_properties.visible_viewport_size = visible_viewport_size_;
3237 visual_properties.compositor_viewport_pixel_size =
3238 gfx::ScaleToCeiledSize(size(), factor);
3239 visual_properties.browser_controls_shrink_blink_size = false;
3240 visual_properties.top_controls_height = 0.f;
3241 visual_properties.is_fullscreen_granted = is_fullscreen_granted_;
3242 visual_properties.display_mode = display_mode_;
Fady Samuel84d8a1f72018-11-08 00:26:173243 visual_properties.local_surface_id_allocation =
3244 local_surface_id_allocation_from_parent_;
W. James MacLean2a90bff2018-11-05 20:52:473245 visual_properties.page_scale_factor = page_scale_factor_from_mainframe_;
danakja2c9d0a92018-07-25 20:01:183246 // We are changing the device scale factor from the renderer, so allocate a
3247 // new viz::LocalSurfaceId to avoid surface invariants violations in tests.
3248 if (layer_tree_view_)
3249 layer_tree_view_->RequestNewLocalSurfaceId();
3250
3251 OnSynchronizeVisualProperties(visual_properties);
3252}
3253
3254void RenderWidget::SetDeviceColorSpaceForTesting(
3255 const gfx::ColorSpace& color_space) {
3256 VisualProperties visual_properties;
3257 visual_properties.screen_info = screen_info_;
3258 visual_properties.screen_info.color_space = color_space;
3259 visual_properties.new_size = size();
3260 visual_properties.visible_viewport_size = visible_viewport_size_;
3261 visual_properties.compositor_viewport_pixel_size =
3262 compositor_viewport_pixel_size_;
3263 visual_properties.browser_controls_shrink_blink_size = false;
3264 visual_properties.top_controls_height = 0.f;
3265 visual_properties.is_fullscreen_granted = is_fullscreen_granted_;
3266 visual_properties.display_mode = display_mode_;
Fady Samuel84d8a1f72018-11-08 00:26:173267 visual_properties.local_surface_id_allocation =
3268 local_surface_id_allocation_from_parent_;
W. James MacLean2a90bff2018-11-05 20:52:473269 visual_properties.page_scale_factor = page_scale_factor_from_mainframe_;
danakja2c9d0a92018-07-25 20:01:183270 // We are changing the device color space from the renderer, so allocate a
3271 // new viz::LocalSurfaceId to avoid surface invariants violations in tests.
3272 if (layer_tree_view_)
3273 layer_tree_view_->RequestNewLocalSurfaceId();
3274 OnSynchronizeVisualProperties(visual_properties);
3275}
3276
3277void RenderWidget::SetWindowRectSynchronouslyForTesting(
3278 const gfx::Rect& new_window_rect) {
3279 SetWindowRectSynchronously(new_window_rect);
3280}
3281
3282void RenderWidget::EnableAutoResizeForTesting(const gfx::Size& min_size,
3283 const gfx::Size& max_size) {
3284 VisualProperties visual_properties;
3285 visual_properties.auto_resize_enabled = true;
3286 visual_properties.min_size_for_auto_resize = min_size;
3287 visual_properties.max_size_for_auto_resize = max_size;
Fady Samuel84d8a1f72018-11-08 00:26:173288 visual_properties.local_surface_id_allocation =
3289 base::Optional<viz::LocalSurfaceIdAllocation>(
3290 viz::LocalSurfaceIdAllocation(
3291 viz::LocalSurfaceId(1, 1, base::UnguessableToken::Create()),
3292 base::TimeTicks::Now()));
W. James MacLean2a90bff2018-11-05 20:52:473293 visual_properties.page_scale_factor = page_scale_factor_from_mainframe_;
danakja2c9d0a92018-07-25 20:01:183294 OnSynchronizeVisualProperties(visual_properties);
3295}
3296
3297void RenderWidget::DisableAutoResizeForTesting(const gfx::Size& new_size) {
3298 if (!auto_resize_mode_)
3299 return;
3300
3301 VisualProperties visual_properties;
3302 visual_properties.auto_resize_enabled = false;
3303 visual_properties.screen_info = screen_info_;
3304 visual_properties.new_size = new_size;
3305 visual_properties.compositor_viewport_pixel_size =
3306 compositor_viewport_pixel_size_;
3307 visual_properties.browser_controls_shrink_blink_size =
3308 browser_controls_shrink_blink_size_;
3309 visual_properties.top_controls_height = top_controls_height_;
3310 visual_properties.visible_viewport_size = visible_viewport_size_;
3311 visual_properties.is_fullscreen_granted = is_fullscreen_granted_;
3312 visual_properties.display_mode = display_mode_;
W. James MacLean2a90bff2018-11-05 20:52:473313 visual_properties.page_scale_factor = page_scale_factor_from_mainframe_;
danakja2c9d0a92018-07-25 20:01:183314 OnSynchronizeVisualProperties(visual_properties);
3315}
3316
EhsanK955ba582017-11-30 21:14:403317blink::WebLocalFrame* RenderWidget::GetFocusedWebLocalFrameInWidget() const {
Ehsan Karamad655d7b8a2018-01-12 18:38:383318 if (auto* frame_widget = GetFrameWidget())
3319 return frame_widget->FocusedWebLocalFrameInWidget();
3320 return nullptr;
EhsanK955ba582017-11-30 21:14:403321}
3322
EhsanK22d482e2017-08-10 17:29:493323#if BUILDFLAG(ENABLE_PLUGINS)
3324PepperPluginInstanceImpl* RenderWidget::GetFocusedPepperPluginInsideWidget() {
Ehsan Karamad655d7b8a2018-01-12 18:38:383325 blink::WebFrameWidget* frame_widget = GetFrameWidget();
3326 if (!frame_widget)
EhsanK22d482e2017-08-10 17:29:493327 return nullptr;
3328
3329 // Focused pepper instance might not always be in the focused frame. For
3330 // instance if a pepper instance and its embedder frame are focused an then
3331 // another frame takes focus using javascript, the embedder frame will no
3332 // longer be focused while the pepper instance is (the embedder frame's
3333 // |focused_pepper_plugin_| is not nullptr). Especially, if the pepper plugin
3334 // is fullscreen, clicking into the pepper will not refocus the embedder
3335 // frame. This is why we have to traverse the whole frame tree to find the
3336 // focused plugin.
Ehsan Karamad655d7b8a2018-01-12 18:38:383337 blink::WebFrame* current_frame = frame_widget->LocalRoot();
EhsanK22d482e2017-08-10 17:29:493338 while (current_frame) {
3339 RenderFrameImpl* render_frame =
3340 current_frame->IsWebLocalFrame()
3341 ? RenderFrameImpl::FromWebFrame(current_frame)
3342 : nullptr;
3343 if (render_frame && render_frame->focused_pepper_plugin())
3344 return render_frame->focused_pepper_plugin();
3345 current_frame = current_frame->TraverseNext();
3346 }
3347 return nullptr;
3348}
3349#endif
3350
Ken Buchananb2c9e262018-03-10 16:53:313351gfx::Rect RenderWidget::ViewportVisibleRect() {
danakj6dcbc5962018-11-16 16:45:423352 return for_child_local_root_frame_
3353 ? compositor_visible_rect_
3354 : gfx::Rect(compositor_viewport_pixel_size_);
Ken Buchananb2c9e262018-03-10 16:53:313355}
3356
Hajime Hoshi315a61f2018-08-14 17:27:283357// static
3358scoped_refptr<base::SingleThreadTaskRunner>
3359RenderWidget::GetCleanupTaskRunner() {
3360 return RenderThreadImpl::current_blink_platform_impl()
3361 ->main_thread_scheduler()
3362 ->CleanupTaskRunner();
3363}
3364
Saman Sami50d1e0c2018-03-13 20:03:493365base::WeakPtr<RenderWidget> RenderWidget::AsWeakPtr() {
3366 return weak_ptr_factory_.GetWeakPtr();
3367}
3368
[email protected]e9ff79c2012-10-19 21:31:263369} // namespace content