Avi Drissman | 4e1b7bc3 | 2022-09-15 14:03:50 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | 93ddb3c | 2012-04-11 21:44:29 | [diff] [blame] | 5 | #include "content/browser/web_contents/web_contents_impl.h" |
[email protected] | 3952656 | 2011-02-05 03:41:51 | [diff] [blame] | 6 | |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | |
w.shackleton | 49bcd39 | 2016-01-06 17:38:22 | [diff] [blame] | 9 | #include <cmath> |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 10 | #include <memory> |
Jan Wilken Dörrie | ad587c3 | 2021-03-11 14:09:27 | [diff] [blame] | 11 | #include <string> |
[email protected] | 2bb17188 | 2012-03-07 02:09:46 | [diff] [blame] | 12 | #include <utility> |
rkaplow | e56a5203 | 2017-02-07 00:14:54 | [diff] [blame] | 13 | #include <vector> |
[email protected] | b75b829 | 2010-10-01 07:28:25 | [diff] [blame] | 14 | |
Bartek Nowierski | d5d1984a | 2022-12-18 12:13:15 | [diff] [blame] | 15 | #include "base/allocator/partition_allocator/partition_alloc_buildflags.h" |
K. Moon | 97def7d | 2022-02-01 15:56:02 | [diff] [blame] | 16 | #include "base/check_op.h" |
[email protected] | 36fb2c7c | 2011-04-04 15:49:08 | [diff] [blame] | 17 | #include "base/command_line.h" |
Lei Zhang | d4f2c7ad | 2021-05-13 20:10:12 | [diff] [blame] | 18 | #include "base/containers/contains.h" |
Lei Zhang | 7375aec | 2021-06-30 16:54:53 | [diff] [blame] | 19 | #include "base/containers/cxx20_erase.h" |
Avi Drissman | 3397294 | 2020-06-19 14:16:01 | [diff] [blame] | 20 | #include "base/containers/flat_set.h" |
zqzhang | 181047e6 | 2016-07-01 13:37:17 | [diff] [blame] | 21 | #include "base/feature_list.h" |
Marijn Kruisselbrink | 46fa4bd | 2019-07-11 18:15:47 | [diff] [blame] | 22 | #include "base/files/file_path.h" |
Avi Drissman | adac2199 | 2023-01-11 23:46:39 | [diff] [blame] | 23 | #include "base/functional/bind.h" |
[email protected] | cbb1ef59 | 2013-06-05 19:49:46 | [diff] [blame] | 24 | #include "base/lazy_instance.h" |
skyostil | 95082a6 | 2015-06-05 19:53:07 | [diff] [blame] | 25 | #include "base/location.h" |
[email protected] | 6d636e6 | 2013-07-31 03:15:56 | [diff] [blame] | 26 | #include "base/logging.h" |
fdoray | ba12142 | 2016-12-23 19:51:48 | [diff] [blame] | 27 | #include "base/memory/ptr_util.h" |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 28 | #include "base/memory/raw_ptr.h" |
dmazzoni | d95ae84 | 2016-04-12 21:17:39 | [diff] [blame] | 29 | #include "base/memory/ref_counted.h" |
Rakina Zata Amni | 4029b6d | 2020-07-28 02:36:20 | [diff] [blame] | 30 | #include "base/metrics/field_trial.h" |
Joe Mason | 7dabc6f | 2021-12-06 17:29:03 | [diff] [blame] | 31 | #include "base/metrics/histogram_functions.h" |
asvitkine | 8d51e9d | 2016-09-02 23:55:43 | [diff] [blame] | 32 | #include "base/metrics/histogram_macros.h" |
bratell | 0a7406f | 2017-03-28 07:46:37 | [diff] [blame] | 33 | #include "base/metrics/user_metrics.h" |
Avi Drissman | 4891b85 | 2018-09-25 18:12:58 | [diff] [blame] | 34 | #include "base/no_destructor.h" |
David Sanders | d4bf5eb | 2022-03-17 07:12:05 | [diff] [blame] | 35 | #include "base/observer_list.h" |
[email protected] | 04cbd3d | 2013-12-04 04:58:20 | [diff] [blame] | 36 | #include "base/process/process.h" |
Peter Kasting | d568594 | 2022-09-02 17:52:17 | [diff] [blame] | 37 | #include "base/ranges/algorithm.h" |
Avi Drissman | 4891b85 | 2018-09-25 18:12:58 | [diff] [blame] | 38 | #include "base/strings/string_split.h" |
[email protected] | 348fbaac | 2013-06-11 06:31:51 | [diff] [blame] | 39 | #include "base/strings/string_util.h" |
Lei Zhang | e02299a | 2021-04-26 23:12:24 | [diff] [blame] | 40 | #include "base/strings/stringprintf.h" |
Rakina Zata Amni | 4029b6d | 2020-07-28 02:36:20 | [diff] [blame] | 41 | #include "base/system/sys_info.h" |
Patrick Monette | 643cdf6 | 2021-10-15 19:13:42 | [diff] [blame] | 42 | #include "base/task/single_thread_task_runner.h" |
[email protected] | a43858f | 2013-06-28 15:18:37 | [diff] [blame] | 43 | #include "base/time/time.h" |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 44 | #include "base/trace_event/optional_trace_event.h" |
ssid | 3e76561 | 2015-01-28 04:03:42 | [diff] [blame] | 45 | #include "base/trace_event/trace_event.h" |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 46 | #include "build/build_config.h" |
Hidehiko Abe | 7c68f58 | 2020-09-03 15:47:25 | [diff] [blame] | 47 | #include "build/chromeos_buildflags.h" |
Min Qin | da0ed206 | 2018-02-23 22:00:53 | [diff] [blame] | 48 | #include "components/download/public/common/download_stats.h" |
Eric Seckler | ab20ea2 | 2021-02-08 11:39:26 | [diff] [blame] | 49 | #include "components/power_scheduler/power_mode.h" |
| 50 | #include "components/power_scheduler/power_mode_arbiter.h" |
| 51 | #include "components/power_scheduler/power_mode_voter.h" |
rsleevi | 24f64dc2 | 2015-08-07 21:39:21 | [diff] [blame] | 52 | #include "components/url_formatter/url_formatter.h" |
Alexander Surkov | fb6cb38 | 2020-12-01 14:46:23 | [diff] [blame] | 53 | #include "content/browser/accessibility/browser_accessibility.h" |
Steve Kobes | 6615df9 | 2021-03-15 18:02:20 | [diff] [blame] | 54 | #include "content/browser/accessibility/browser_accessibility_state_impl.h" |
Andrew Paseltiner | 7cbb617 | 2021-10-06 21:22:51 | [diff] [blame] | 55 | #include "content/browser/attribution_reporting/attribution_host.h" |
jamescook | da250581 | 2015-03-20 18:01:18 | [diff] [blame] | 56 | #include "content/browser/bad_message.h" |
Max Morin | ddebb97 | 2018-09-20 10:04:14 | [diff] [blame] | 57 | #include "content/browser/browser_main_loop.h" |
[email protected] | 7a846df | 2012-09-20 19:17:39 | [diff] [blame] | 58 | #include "content/browser/browser_plugin/browser_plugin_embedder.h" |
| 59 | #include "content/browser/browser_plugin/browser_plugin_guest.h" |
[email protected] | b953542 | 2012-02-09 01:47:59 | [diff] [blame] | 60 | #include "content/browser/child_process_security_policy_impl.h" |
Pavel Feldman | 3c1842b | 2017-08-02 05:00:16 | [diff] [blame] | 61 | #include "content/browser/devtools/protocol/page_handler.h" |
dgozman | f00c4f7 | 2016-10-19 17:45:15 | [diff] [blame] | 62 | #include "content/browser/devtools/render_frame_devtools_agent_host.h" |
Becca Hughes | 3b5a4348 | 2018-07-17 22:31:55 | [diff] [blame] | 63 | #include "content/browser/display_cutout/display_cutout_host_impl.h" |
[email protected] | 5f2aa72 | 2013-08-07 16:59:41 | [diff] [blame] | 64 | #include "content/browser/dom_storage/dom_storage_context_wrapper.h" |
[email protected] | 1ea3c79 | 2012-04-17 01:25:04 | [diff] [blame] | 65 | #include "content/browser/dom_storage/session_storage_namespace_impl.h" |
[email protected] | aa4f397 | 2012-03-01 18:12:12 | [diff] [blame] | 66 | #include "content/browser/download/mhtml_generation_manager.h" |
[email protected] | a53209b | 2012-01-20 16:48:16 | [diff] [blame] | 67 | #include "content/browser/download/save_package.h" |
Dave Tapuska | 54c76a03 | 2021-10-27 22:10:42 | [diff] [blame] | 68 | #include "content/browser/fenced_frame/fenced_frame.h" |
paulmeyer | c0b762b | 2016-04-13 11:55:17 | [diff] [blame] | 69 | #include "content/browser/find_request_manager.h" |
Kenneth Russell | 954ed9ce | 2018-04-12 23:07:01 | [diff] [blame] | 70 | #include "content/browser/gpu/gpu_data_manager_impl.h" |
Mark Schillaci | 1363e4a | 2021-10-04 19:10:09 | [diff] [blame] | 71 | #include "content/browser/host_zoom_map_impl.h" |
dalecurtis | bc6572e1 | 2014-09-12 19:22:30 | [diff] [blame] | 72 | #include "content/browser/media/audio_stream_monitor.h" |
dalecurtis | 88c24007 | 2015-12-09 02:11:18 | [diff] [blame] | 73 | #include "content/browser/media/media_web_contents_observer.h" |
Mike Wasserman | dabad61 | 2020-08-13 04:26:27 | [diff] [blame] | 74 | #include "content/browser/permissions/permission_controller_impl.h" |
Illia Klimov | b648dbd | 2021-07-08 08:08:49 | [diff] [blame] | 75 | #include "content/browser/permissions/permission_util.h" |
Kevin McNee | 94ea52f5 | 2020-06-23 17:42:06 | [diff] [blame] | 76 | #include "content/browser/portal/portal.h" |
Yoshiki Tanioka | 49b4cfb | 2022-10-20 09:25:31 | [diff] [blame] | 77 | #include "content/browser/preloading/prerender/prerender_final_status.h" |
Sreeja Kamishetty | c227f7a | 2022-07-08 22:33:15 | [diff] [blame] | 78 | #include "content/browser/preloading/prerender/prerender_host_registry.h" |
Lingqi Chi | 8e192ba | 2022-11-22 04:34:25 | [diff] [blame] | 79 | #include "content/browser/preloading/prerender/prerender_metrics.h" |
Hiroki Nakagawa | 3e252704 | 2022-11-24 06:46:42 | [diff] [blame] | 80 | #include "content/browser/preloading/prerender/prerender_new_tab_handle.h" |
Tal Pressman | 5dcd238 | 2020-08-26 07:13:05 | [diff] [blame] | 81 | #include "content/browser/renderer_host/agent_scheduling_group_host.h" |
danakj | e34636e | 2020-09-15 22:15:00 | [diff] [blame] | 82 | #include "content/browser/renderer_host/cross_process_frame_connector.h" |
David Black | 9cca359 | 2019-11-06 23:02:22 | [diff] [blame] | 83 | #include "content/browser/renderer_host/frame_token_message_queue.h" |
danakj | e34636e | 2020-09-15 22:15:00 | [diff] [blame] | 84 | #include "content/browser/renderer_host/frame_tree_node.h" |
| 85 | #include "content/browser/renderer_host/navigation_entry_impl.h" |
| 86 | #include "content/browser/renderer_host/navigation_request.h" |
Julie Jeongeun Kim | 9e20451 | 2021-06-24 07:28:54 | [diff] [blame] | 87 | #include "content/browser/renderer_host/page_impl.h" |
danakj | e34636e | 2020-09-15 22:15:00 | [diff] [blame] | 88 | #include "content/browser/renderer_host/render_frame_host_impl.h" |
| 89 | #include "content/browser/renderer_host/render_frame_proxy_host.h" |
[email protected] | 2116103 | 2014-05-05 16:56:50 | [diff] [blame] | 90 | #include "content/browser/renderer_host/render_view_host_delegate_view.h" |
[email protected] | b3c41c0b | 2012-03-06 15:48:32 | [diff] [blame] | 91 | #include "content/browser/renderer_host/render_view_host_impl.h" |
[email protected] | bafe6cd | 2012-05-23 23:09:50 | [diff] [blame] | 92 | #include "content/browser/renderer_host/render_widget_host_impl.h" |
kenrb | 2a565f8 | 2015-09-02 20:24:59 | [diff] [blame] | 93 | #include "content/browser/renderer_host/render_widget_host_input_event_router.h" |
[email protected] | cfd80b0 | 2014-05-01 17:46:48 | [diff] [blame] | 94 | #include "content/browser/renderer_host/render_widget_host_view_base.h" |
Ken Buchanan | daef006b | 2017-08-17 18:32:15 | [diff] [blame] | 95 | #include "content/browser/renderer_host/render_widget_host_view_child_frame.h" |
ekaramad | add88229 | 2016-06-08 15:22:56 | [diff] [blame] | 96 | #include "content/browser/renderer_host/text_input_manager.h" |
Joe Mason | 7ae0e2b | 2021-11-01 12:30:36 | [diff] [blame] | 97 | #include "content/browser/renderer_host/visible_time_request_trigger.h" |
Mike Wasserman | 8edbf2c | 2023-01-05 19:53:06 | [diff] [blame] | 98 | #include "content/browser/screen_details/screen_change_monitor.h" |
leon.han | 552e9de | 2017-02-09 14:37:30 | [diff] [blame] | 99 | #include "content/browser/screen_orientation/screen_orientation_provider.h" |
Yao Xiao | 1ac702d | 2022-06-08 17:20:49 | [diff] [blame] | 100 | #include "content/browser/shared_storage/shared_storage_budget_charger.h" |
[email protected] | b658359 | 2012-01-25 19:52:33 | [diff] [blame] | 101 | #include "content/browser/site_instance_impl.h" |
Lei Zhang | 2764bca | 2021-07-15 02:00:07 | [diff] [blame] | 102 | #include "content/browser/wake_lock/wake_lock_context_host.h" |
David Bokan | 1bdb3701f | 2021-04-30 22:02:35 | [diff] [blame] | 103 | #include "content/browser/web_contents/java_script_dialog_commit_deferring_condition.h" |
Avi Drissman | 0d0908ded | 2022-04-28 05:39:39 | [diff] [blame] | 104 | #include "content/browser/web_contents/web_contents_view.h" |
lfg | c740d4b2 | 2016-04-15 16:45:33 | [diff] [blame] | 105 | #include "content/browser/web_contents/web_contents_view_child_frame.h" |
[email protected] | 86a0a6e | 2013-01-28 06:33:03 | [diff] [blame] | 106 | #include "content/browser/webui/web_ui_controller_factory_registry.h" |
[email protected] | d235345 | 2012-01-19 19:53:56 | [diff] [blame] | 107 | #include "content/browser/webui/web_ui_impl.h" |
David Bienvenu | 1cdcf09 | 2020-11-04 18:47:56 | [diff] [blame] | 108 | #include "content/browser/xr/service/xr_runtime_manager_impl.h" |
Rakina Zata Amni | 4029b6d | 2020-07-28 02:36:20 | [diff] [blame] | 109 | #include "content/common/content_switches_internal.h" |
Alexander Surkov | 3b8d225 | 2020-11-30 21:13:14 | [diff] [blame] | 110 | #include "content/public/browser/ax_inspect_factory.h" |
[email protected] | ccb79730 | 2011-12-15 16:55:11 | [diff] [blame] | 111 | #include "content/public/browser/browser_context.h" |
Kevin McNee | 5f59438 | 2021-05-06 23:18:23 | [diff] [blame] | 112 | #include "content/public/browser/browser_plugin_guest_manager.h" |
Gabriel Charette | 790754c | 2018-03-16 21:32:59 | [diff] [blame] | 113 | #include "content/public/browser/browser_thread.h" |
Lei Zhang | 2764bca | 2021-07-15 02:00:07 | [diff] [blame] | 114 | #include "content/public/browser/color_chooser.h" |
[email protected] | 87f3c08 | 2011-10-19 18:07:44 | [diff] [blame] | 115 | #include "content/public/browser/content_browser_client.h" |
Lukasz Anforowicz | 8c4446dc | 2020-03-11 22:16:42 | [diff] [blame] | 116 | #include "content/public/browser/context_menu_params.h" |
Ken Rockot | ce010f0 | 2019-12-12 23:32:32 | [diff] [blame] | 117 | #include "content/public/browser/device_service.h" |
Fergal Daly | 1336ac64 | 2021-09-14 15:13:11 | [diff] [blame] | 118 | #include "content/public/browser/disallow_activation_reason.h" |
[email protected] | e582fdd | 2011-12-20 16:48:17 | [diff] [blame] | 119 | #include "content/public/browser/download_manager.h" |
Kent Tamura | 512a27e | 2018-10-04 00:49:32 | [diff] [blame] | 120 | #include "content/public/browser/file_select_listener.h" |
ekaramad | a110f64 | 2016-12-21 19:47:28 | [diff] [blame] | 121 | #include "content/public/browser/focused_node_details.h" |
Sreeja Kamishetty | 15f9944a2 | 2022-03-10 10:16:08 | [diff] [blame] | 122 | #include "content/public/browser/frame_type.h" |
Danyao Wang | a78f3dd2 | 2020-03-05 05:31:27 | [diff] [blame] | 123 | #include "content/public/browser/global_routing_id.h" |
[email protected] | d908348 | 2012-01-06 00:38:46 | [diff] [blame] | 124 | #include "content/public/browser/invalidate_type.h" |
[email protected] | 71a88bb | 2013-02-01 22:05:15 | [diff] [blame] | 125 | #include "content/public/browser/javascript_dialog_manager.h" |
sky | f65d9bb | 2017-03-24 02:26:39 | [diff] [blame] | 126 | #include "content/public/browser/keyboard_event_processing_result.h" |
[email protected] | 09d31d5 | 2012-03-11 22:30:27 | [diff] [blame] | 127 | #include "content/public/browser/load_notification_details.h" |
[email protected] | 5b96836f | 2011-12-22 07:39:00 | [diff] [blame] | 128 | #include "content/public/browser/navigation_details.h" |
[email protected] | 375fa1b | 2012-05-22 22:05:37 | [diff] [blame] | 129 | #include "content/public/browser/notification_details.h" |
[email protected] | be2510c0 | 2012-05-28 14:52:14 | [diff] [blame] | 130 | #include "content/public/browser/notification_service.h" |
scottmg | 011d96e3 | 2016-06-29 19:15:08 | [diff] [blame] | 131 | #include "content/public/browser/notification_types.h" |
[email protected] | 9564021 | 2014-07-26 18:14:30 | [diff] [blame] | 132 | #include "content/public/browser/render_widget_host_iterator.h" |
Avi Drissman | 783c123 | 2021-02-05 01:27:05 | [diff] [blame] | 133 | #include "content/public/browser/render_widget_host_observer.h" |
Lukasz Anforowicz | e1b954d9 | 2017-10-30 21:28:06 | [diff] [blame] | 134 | #include "content/public/browser/restore_type.h" |
Alex Moshchuk | 134690e | 2022-05-21 01:50:09 | [diff] [blame] | 135 | #include "content/public/browser/site_isolation_policy.h" |
falken | 52a56e3 | 2016-12-08 05:02:40 | [diff] [blame] | 136 | #include "content/public/browser/ssl_status.h" |
[email protected] | 4c3a2358 | 2012-08-18 08:54:34 | [diff] [blame] | 137 | #include "content/public/browser/storage_partition.h" |
[email protected] | 674bc59 | 2011-12-20 23:00:42 | [diff] [blame] | 138 | #include "content/public/browser/web_contents_delegate.h" |
Avi Drissman | 0d0908ded | 2022-04-28 05:39:39 | [diff] [blame] | 139 | #include "content/public/browser/web_contents_view_delegate.h" |
Lei Zhang | ee3b63ad | 2018-06-06 23:25:31 | [diff] [blame] | 140 | #include "content/public/browser/web_ui_controller.h" |
Hans Wennborg | 5ffd139 | 2019-10-16 11:00:02 | [diff] [blame] | 141 | #include "content/public/common/content_client.h" |
Francois Doray | 3f2afbd | 2018-04-06 19:18:18 | [diff] [blame] | 142 | #include "content/public/common/content_features.h" |
[email protected] | 64d69de4 | 2012-02-06 00:19:54 | [diff] [blame] | 143 | #include "content/public/common/content_switches.h" |
Leon Han | 963dc18 | 2018-11-06 05:41:48 | [diff] [blame] | 144 | #include "content/public/common/referrer_type_converters.h" |
David Sanders | 7d4623d | 2022-04-21 07:00:18 | [diff] [blame] | 145 | #include "content/public/common/url_constants.h" |
Rakina Zata Amni | 4029b6d | 2020-07-28 02:36:20 | [diff] [blame] | 146 | #include "media/base/media_switches.h" |
Max Morin | ddebb97 | 2018-09-20 10:04:14 | [diff] [blame] | 147 | #include "media/base/user_input_monitor.h" |
wjmaclean | 6495190 | 2016-04-29 20:59:12 | [diff] [blame] | 148 | #include "net/base/url_util.h" |
David Van Cleve | f9988163 | 2020-07-16 19:41:13 | [diff] [blame] | 149 | #include "net/http/http_util.h" |
rhalavati | 0fbaeef3 | 2017-06-01 15:56:30 | [diff] [blame] | 150 | #include "net/traffic_annotation/network_traffic_annotation.h" |
Scott Violet | 02e38b9 | 2018-03-27 23:42:14 | [diff] [blame] | 151 | #include "ppapi/buildflags/buildflags.h" |
Dale Curtis | c496a776 | 2021-02-24 01:15:44 | [diff] [blame] | 152 | #include "services/device/public/mojom/wake_lock.mojom.h" |
arthursonzogni | b93a447 | 2020-04-10 07:38:00 | [diff] [blame] | 153 | #include "services/network/public/cpp/web_sandbox_flags.h" |
Lei Zhang | 2764bca | 2021-07-15 02:00:07 | [diff] [blame] | 154 | #include "services/network/public/mojom/network_context.mojom.h" |
Anton Bikineev | f62d1bf | 2021-05-15 17:56:07 | [diff] [blame] | 155 | #include "third_party/abseil-cpp/absl/types/optional.h" |
Javier Fernández GarcÃa-Boente | 0bda5d2 | 2022-03-17 23:25:27 | [diff] [blame] | 156 | #include "third_party/blink/public/common/custom_handlers/protocol_handler_utils.h" |
Takashi Toyoshima | 58a78001 | 2021-04-08 03:19:37 | [diff] [blame] | 157 | #include "third_party/blink/public/common/features.h" |
shivanigithub | c688ced | 2020-11-04 19:46:15 | [diff] [blame] | 158 | #include "third_party/blink/public/common/loader/resource_type_util.h" |
Blink Reformat | a30d423 | 2018-04-07 15:31:06 | [diff] [blame] | 159 | #include "third_party/blink/public/common/mime_util/mime_util.h" |
danakj | 938b37a6 | 2019-09-24 18:35:54 | [diff] [blame] | 160 | #include "third_party/blink/public/common/page/page_zoom.h" |
Miyoung Shin | 5d77f7207 | 2020-10-09 15:14:20 | [diff] [blame] | 161 | #include "third_party/blink/public/common/page_state/page_state.h" |
Andy Paicu | a6d6d85 | 2022-04-28 18:08:36 | [diff] [blame] | 162 | #include "third_party/blink/public/common/permissions/permission_utils.h" |
Frédéric Wang | b79fece | 2020-10-28 18:13:07 | [diff] [blame] | 163 | #include "third_party/blink/public/common/security/protocol_handler_security_level.h" |
David Bokan | 7950a3f | 2020-10-27 21:53:31 | [diff] [blame] | 164 | #include "third_party/blink/public/common/switches.h" |
Gyuyoung Kim | 1ac4ca78 | 2020-09-11 03:32:51 | [diff] [blame] | 165 | #include "third_party/blink/public/common/web_preferences/web_preferences.h" |
Abhijeet Kandalkar | de7348e | 2020-01-13 06:06:54 | [diff] [blame] | 166 | #include "third_party/blink/public/mojom/frame/frame.mojom.h" |
Dave Tapuska | a418951 | 2019-10-15 20:27:34 | [diff] [blame] | 167 | #include "third_party/blink/public/mojom/frame/fullscreen.mojom.h" |
Mikel Astiz | 034ba14e | 2021-04-30 07:23:49 | [diff] [blame] | 168 | #include "third_party/blink/public/mojom/image_downloader/image_downloader.mojom.h" |
Antonio Gomes | 0d42960a | 2019-06-05 12:35:51 | [diff] [blame] | 169 | #include "third_party/blink/public/mojom/mediastream/media_stream.mojom-shared.h" |
Simon Hangl | 574cecf | 2022-05-05 06:19:46 | [diff] [blame] | 170 | #include "third_party/blink/public/mojom/mediastream/media_stream.mojom.h" |
Brad Triebwasser | 767c27a | 2022-08-25 22:56:05 | [diff] [blame] | 171 | #include "third_party/blink/public/mojom/window_features/window_features.mojom.h" |
halton.huo | ca2eabd | 2015-07-06 08:17:40 | [diff] [blame] | 172 | #include "third_party/skia/include/core/SkBitmap.h" |
dmazzoni | d95ae84 | 2016-04-12 21:17:39 | [diff] [blame] | 173 | #include "ui/accessibility/ax_tree_combiner.h" |
David Bokan | 7ec5ac92 | 2022-11-29 03:31:31 | [diff] [blame] | 174 | #include "ui/base/ime/mojom/virtual_keyboard_types.mojom.h" |
Gyuyoung Kim | c527ec3 | 2020-11-21 03:31:43 | [diff] [blame] | 175 | #include "ui/base/pointer/pointer_device.h" |
Lei Zhang | 2764bca | 2021-07-15 02:00:07 | [diff] [blame] | 176 | #include "ui/base/window_open_disposition.h" |
tom | 855c4bf3 | 2021-09-30 00:27:26 | [diff] [blame] | 177 | #include "ui/color/color_provider_manager.h" |
Rakina Zata Amni | 4029b6d | 2020-07-28 02:36:20 | [diff] [blame] | 178 | #include "ui/display/screen.h" |
Brad Triebwasser | 9d34d25 | 2022-07-26 20:44:42 | [diff] [blame] | 179 | #include "ui/display/types/display_constants.h" |
Daniel Cheng | 90196c8 | 2018-04-25 21:49:14 | [diff] [blame] | 180 | #include "ui/events/base_event_utils.h" |
Rakina Zata Amni | 4029b6d | 2020-07-28 02:36:20 | [diff] [blame] | 181 | #include "ui/gfx/animation/animation.h" |
[email protected] | 3c733bde | 2010-12-21 19:56:31 | [diff] [blame] | 182 | |
Xiaohan Wang | 24ec934 | 2022-01-15 17:34:22 | [diff] [blame] | 183 | #if BUILDFLAG(IS_WIN) |
Peter Kvitek | 965f3cd | 2022-01-06 22:33:41 | [diff] [blame] | 184 | #include "base/threading/thread_restrictions.h" |
ekaramad | a110f64 | 2016-12-21 19:47:28 | [diff] [blame] | 185 | #include "content/browser/renderer_host/dip_util.h" |
| 186 | #include "ui/gfx/geometry/dip_util.h" |
| 187 | #endif |
| 188 | |
Xiaohan Wang | 24ec934 | 2022-01-15 17:34:22 | [diff] [blame] | 189 | #if BUILDFLAG(IS_ANDROID) |
mfomitchev | 2b8b066a | 2016-01-28 19:23:15 | [diff] [blame] | 190 | #include "content/browser/android/date_time_chooser_android.h" |
sammc | f5f1b0f | 2016-09-20 23:05:11 | [diff] [blame] | 191 | #include "content/browser/android/java_interfaces_impl.h" |
Leon Han | ccb985f | 2019-08-26 02:43:24 | [diff] [blame] | 192 | #include "content/browser/android/nfc_host.h" |
[email protected] | 155c7f2 | 2013-12-09 17:07:18 | [diff] [blame] | 193 | #include "content/browser/web_contents/web_contents_android.h" |
Ke He | 31d0bb0 | 2018-02-24 07:16:24 | [diff] [blame] | 194 | #include "services/device/public/mojom/nfc.mojom.h" |
Lei Zhang | 2764bca | 2021-07-15 02:00:07 | [diff] [blame] | 195 | #include "services/service_manager/public/cpp/interface_provider.h" |
Mugdha Lakhani | 0a0d786 | 2020-07-29 09:58:45 | [diff] [blame] | 196 | #include "ui/android/view_android.h" |
Lei Zhang | 2764bca | 2021-07-15 02:00:07 | [diff] [blame] | 197 | #include "ui/base/device_form_factor.h" |
Xiaohan Wang | 24ec934 | 2022-01-15 17:34:22 | [diff] [blame] | 198 | #endif // BUILDFLAG(IS_ANDROID) |
[email protected] | 583418cc | 2013-01-17 14:01:10 | [diff] [blame] | 199 | |
K. Moon | 9ae6ef2c | 2022-08-18 01:30:06 | [diff] [blame] | 200 | #if BUILDFLAG(ENABLE_PPAPI) |
zqzhang | 181047e6 | 2016-07-01 13:37:17 | [diff] [blame] | 201 | #include "content/browser/media/session/pepper_playback_observer.h" |
Lei Zhang | ebcc630 | 2018-01-12 19:46:45 | [diff] [blame] | 202 | #endif |
zqzhang | 181047e6 | 2016-07-01 13:37:17 | [diff] [blame] | 203 | |
Lei Zhang | 2764bca | 2021-07-15 02:00:07 | [diff] [blame] | 204 | #if defined(USE_AURA) |
| 205 | #include "ui/aura/window.h" |
| 206 | #endif |
| 207 | |
Bartek Nowierski | bc01b711 | 2023-02-01 14:11:42 | [diff] [blame] | 208 | #if BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC) && BUILDFLAG(USE_STARSCAN) |
| 209 | #include "base/allocator/partition_allocator/starscan/pcscan.h" |
| 210 | #include "content/browser/starscan_load_observer.h" |
| 211 | #endif |
| 212 | |
[email protected] | 8ff00d7 | 2012-10-23 19:12:21 | [diff] [blame] | 213 | namespace content { |
Abigail Klein | aa89874 | 2019-11-01 02:31:25 | [diff] [blame] | 214 | |
[email protected] | 420ae01 | 2009-04-24 05:16:32 | [diff] [blame] | 215 | namespace { |
| 216 | |
Dave Tapuska | e1a08aaf | 2021-03-05 18:31:59 | [diff] [blame] | 217 | // The window which we dobounce load info updates in. |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 218 | constexpr auto kUpdateLoadStatesInterval = base::Milliseconds(250); |
Dave Tapuska | e1a08aaf | 2021-03-05 18:31:59 | [diff] [blame] | 219 | |
Sreeja Kamishetty | 2771b7d | 2021-04-07 06:34:18 | [diff] [blame] | 220 | using LifecycleState = RenderFrameHost::LifecycleState; |
Takashi Toyoshima | daf538e | 2021-04-13 05:47:29 | [diff] [blame] | 221 | using LifecycleStateImpl = RenderFrameHostImpl::LifecycleStateImpl; |
Sreeja Kamishetty | be0ccd6b | 2020-06-09 11:52:50 | [diff] [blame] | 222 | |
Elly Fong-Jones | 564de626 | 2022-07-28 21:11:16 | [diff] [blame] | 223 | base::LazyInstance<base::RepeatingCallbackList<void(WebContents*)>>:: |
| 224 | DestructorAtExit g_created_callbacks = LAZY_INSTANCE_INITIALIZER; |
[email protected] | cbb1ef59 | 2013-06-05 19:49:46 | [diff] [blame] | 225 | |
Miyoung Shin | 7d8077a | 2022-03-25 07:13:14 | [diff] [blame] | 226 | bool HasMatchingWidgetHost(FrameTree* tree, RenderWidgetHostImpl* host) { |
kenrb | 1922185 | 2016-04-29 17:21:40 | [diff] [blame] | 227 | // This method scans the frame tree rather than checking whether |
| 228 | // host->delegate() == this, which allows it to return false when the host |
| 229 | // for a frame that is pending or pending deletion. |
| 230 | if (!host) |
| 231 | return false; |
| 232 | |
Miyoung Shin | 7d8077a | 2022-03-25 07:13:14 | [diff] [blame] | 233 | for (FrameTreeNode* node : tree->NodesIncludingInnerTreeNodes()) { |
| 234 | // We might cross a WebContents boundary here, but it's fine as we are only |
| 235 | // comparing the RWHI with the given `host`, which is always guaranteed to |
| 236 | // belong to the same WebContents as `tree`. |
| 237 | if (node->current_frame_host()->GetRenderWidgetHost() == host) { |
| 238 | DCHECK_EQ(WebContentsImpl::FromFrameTreeNode(node), |
| 239 | WebContentsImpl::FromRenderWidgetHostImpl(host)); |
kenrb | 1922185 | 2016-04-29 17:21:40 | [diff] [blame] | 240 | return true; |
Miyoung Shin | 7d8077a | 2022-03-25 07:13:14 | [diff] [blame] | 241 | } |
kenrb | 1922185 | 2016-04-29 17:21:40 | [diff] [blame] | 242 | } |
| 243 | return false; |
| 244 | } |
| 245 | |
Ian Clelland | 5cbaaf8 | 2017-11-27 22:00:03 | [diff] [blame] | 246 | RenderFrameHostImpl* FindOpenerRFH(const WebContents::CreateParams& params) { |
| 247 | RenderFrameHostImpl* opener_rfh = nullptr; |
alexmos | e201c7cd | 2015-06-10 17:14:21 | [diff] [blame] | 248 | if (params.opener_render_frame_id != MSG_ROUTING_NONE) { |
Ian Clelland | 5cbaaf8 | 2017-11-27 22:00:03 | [diff] [blame] | 249 | opener_rfh = RenderFrameHostImpl::FromID(params.opener_render_process_id, |
| 250 | params.opener_render_frame_id); |
alexmos | e201c7cd | 2015-06-10 17:14:21 | [diff] [blame] | 251 | } |
Ian Clelland | 5cbaaf8 | 2017-11-27 22:00:03 | [diff] [blame] | 252 | return opener_rfh; |
lukasza | 6f8ac62 | 2017-06-06 03:10:20 | [diff] [blame] | 253 | } |
| 254 | |
Dominick Ng | 37a1505 | 2018-07-17 23:56:35 | [diff] [blame] | 255 | // Returns |true| if |type| is the kind of user input that should trigger the |
| 256 | // user interaction observers. |
| 257 | bool IsUserInteractionInputType(blink::WebInputEvent::Type type) { |
Mustaq Ahmed | 3a40f9f2 | 2022-10-11 19:16:17 | [diff] [blame] | 258 | // TODO(mustaq): This list should be based off the HTML spec: |
| 259 | // https://html.spec.whatwg.org/multipage/interaction.html#tracking-user-activation, |
| 260 | // and kGestureScrollBegin is a clear outlier. |
Dave Tapuska | 347d60a | 2020-04-21 23:55:47 | [diff] [blame] | 261 | return type == blink::WebInputEvent::Type::kMouseDown || |
| 262 | type == blink::WebInputEvent::Type::kGestureScrollBegin || |
| 263 | type == blink::WebInputEvent::Type::kTouchStart || |
| 264 | type == blink::WebInputEvent::Type::kRawKeyDown; |
Dominick Ng | 37a1505 | 2018-07-17 23:56:35 | [diff] [blame] | 265 | } |
| 266 | |
Pavel Feldman | 3c1842b | 2017-08-02 05:00:16 | [diff] [blame] | 267 | // Ensures that OnDialogClosed is only called once. |
| 268 | class CloseDialogCallbackWrapper |
| 269 | : public base::RefCountedThreadSafe<CloseDialogCallbackWrapper> { |
| 270 | public: |
Avi Drissman | e04d399 | 2017-10-05 15:11:36 | [diff] [blame] | 271 | using CloseCallback = |
Jan Wilken Dörrie | aace0cfef | 2021-03-11 22:01:58 | [diff] [blame] | 272 | base::OnceCallback<void(bool, bool, const std::u16string&)>; |
Avi Drissman | e04d399 | 2017-10-05 15:11:36 | [diff] [blame] | 273 | |
| 274 | explicit CloseDialogCallbackWrapper(CloseCallback callback) |
| 275 | : callback_(std::move(callback)) {} |
Pavel Feldman | 3c1842b | 2017-08-02 05:00:16 | [diff] [blame] | 276 | |
| 277 | void Run(bool dialog_was_suppressed, |
| 278 | bool success, |
Jan Wilken Dörrie | aace0cfef | 2021-03-11 22:01:58 | [diff] [blame] | 279 | const std::u16string& user_input) { |
Avi Drissman | e04d399 | 2017-10-05 15:11:36 | [diff] [blame] | 280 | if (callback_.is_null()) |
Pavel Feldman | 3c1842b | 2017-08-02 05:00:16 | [diff] [blame] | 281 | return; |
Avi Drissman | e04d399 | 2017-10-05 15:11:36 | [diff] [blame] | 282 | std::move(callback_).Run(dialog_was_suppressed, success, user_input); |
Pavel Feldman | 3c1842b | 2017-08-02 05:00:16 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | private: |
| 286 | friend class base::RefCountedThreadSafe<CloseDialogCallbackWrapper>; |
Fergal Daly | 55b6d72 | 2020-09-11 07:56:33 | [diff] [blame] | 287 | ~CloseDialogCallbackWrapper() = default; |
Avi Drissman | e04d399 | 2017-10-05 15:11:36 | [diff] [blame] | 288 | |
| 289 | CloseCallback callback_; |
Pavel Feldman | 3c1842b | 2017-08-02 05:00:16 | [diff] [blame] | 290 | }; |
| 291 | |
Arthur Sonzogni | 929d29f | 2018-09-17 14:19:44 | [diff] [blame] | 292 | bool FrameCompareDepth(RenderFrameHostImpl* a, RenderFrameHostImpl* b) { |
Harkiran Bolaria | 2e3c0b77 | 2021-09-01 16:01:40 | [diff] [blame] | 293 | return a->GetFrameDepth() < b->GetFrameDepth(); |
Arthur Sonzogni | 929d29f | 2018-09-17 14:19:44 | [diff] [blame] | 294 | } |
| 295 | |
Frédéric Wang | b79fece | 2020-10-28 18:13:07 | [diff] [blame] | 296 | bool AreValidRegisterProtocolHandlerArguments( |
| 297 | const std::string& protocol, |
| 298 | const GURL& url, |
| 299 | const url::Origin& origin, |
| 300 | blink::ProtocolHandlerSecurityLevel security_level) { |
Raymes Khoury | 58373df | 2019-08-06 06:29:20 | [diff] [blame] | 301 | ChildProcessSecurityPolicyImpl* policy = |
| 302 | ChildProcessSecurityPolicyImpl::GetInstance(); |
| 303 | if (policy->IsPseudoScheme(protocol)) |
| 304 | return false; |
| 305 | |
Javier Fernández GarcÃa-Boente | 0bda5d2 | 2022-03-17 23:25:27 | [diff] [blame] | 306 | // Implementation of the protocol handler arguments normalization steps defined |
| 307 | // in the spec. |
| 308 | // https://html.spec.whatwg.org/multipage/system-state.html#normalize-protocol-handler-parameters |
| 309 | // |
| 310 | // Verify custom handler schemes for errors as described in steps 1 and 2 |
| 311 | if (!blink::IsValidCustomHandlerScheme(protocol, security_level)) |
| 312 | return false; |
| 313 | |
Javier Fernández GarcÃa-Boente | f8a578f | 2022-06-29 23:36:23 | [diff] [blame] | 314 | blink::URLSyntaxErrorCode code = |
| 315 | blink::IsValidCustomHandlerURLSyntax(url, url.spec()); |
| 316 | if (code != blink::URLSyntaxErrorCode::kNoError) |
| 317 | return false; |
Javier Fernández GarcÃa-Boente | 0bda5d2 | 2022-03-17 23:25:27 | [diff] [blame] | 318 | |
| 319 | // Verify custom handler URL security as described in steps 6 and 7 |
| 320 | if (!blink::IsAllowedCustomHandlerURL(url, security_level)) |
| 321 | return false; |
Raymes Khoury | 58373df | 2019-08-06 06:29:20 | [diff] [blame] | 322 | url::Origin url_origin = url::Origin::Create(url); |
| 323 | if (url_origin.opaque()) |
| 324 | return false; |
Frédéric Wang | b79fece | 2020-10-28 18:13:07 | [diff] [blame] | 325 | if (security_level < blink::ProtocolHandlerSecurityLevel::kUntrustedOrigins && |
Lukasz Anforowicz | d00c1ed | 2022-01-13 05:25:10 | [diff] [blame] | 326 | !origin.IsSameOriginWith(url)) |
Raymes Khoury | 58373df | 2019-08-06 06:29:20 | [diff] [blame] | 327 | return false; |
| 328 | |
| 329 | return true; |
| 330 | } |
| 331 | |
yilkal | 796e89e | 2019-10-02 23:58:28 | [diff] [blame] | 332 | void RecordMaxFrameCountUMA(size_t max_frame_count) { |
| 333 | UMA_HISTOGRAM_COUNTS_10000("Navigation.MainFrame.MaxFrameCount", |
| 334 | max_frame_count); |
| 335 | } |
| 336 | |
Avi Drissman | 3397294 | 2020-06-19 14:16:01 | [diff] [blame] | 337 | // Returns the set of all WebContentses that are reachable from |web_contents| |
Rakina Zata Amni | 3a48ae4 | 2022-05-05 03:39:56 | [diff] [blame] | 338 | // by applying some combination of |
| 339 | // WebContents::GetFirstWebContentsInLiveOriginalOpenerChain() and |
Avi Drissman | 3397294 | 2020-06-19 14:16:01 | [diff] [blame] | 340 | // WebContents::GetOuterWebContents(). The |web_contents| parameter will be |
| 341 | // included in the returned set. |
| 342 | base::flat_set<WebContentsImpl*> GetAllOpeningWebContents( |
| 343 | WebContentsImpl* web_contents) { |
| 344 | base::flat_set<WebContentsImpl*> result; |
| 345 | base::flat_set<WebContentsImpl*> current; |
| 346 | |
| 347 | current.insert(web_contents); |
| 348 | |
| 349 | while (!current.empty()) { |
| 350 | WebContentsImpl* current_contents = *current.begin(); |
| 351 | current.erase(current.begin()); |
| 352 | auto insert_result = result.insert(current_contents); |
| 353 | |
| 354 | if (insert_result.second) { |
Rakina Zata Amni | 3a48ae4 | 2022-05-05 03:39:56 | [diff] [blame] | 355 | if (WebContents* opener_contents = |
| 356 | current_contents |
| 357 | ->GetFirstWebContentsInLiveOriginalOpenerChain()) { |
| 358 | current.insert(static_cast<WebContentsImpl*>(opener_contents)); |
Avi Drissman | 3397294 | 2020-06-19 14:16:01 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | WebContentsImpl* outer_contents = current_contents->GetOuterWebContents(); |
| 362 | if (outer_contents) |
| 363 | current.insert(outer_contents); |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | return result; |
| 368 | } |
| 369 | |
Xiaohan Wang | 24ec934 | 2022-01-15 17:34:22 | [diff] [blame] | 370 | #if BUILDFLAG(IS_ANDROID) |
Rakina Zata Amni | 4029b6d | 2020-07-28 02:36:20 | [diff] [blame] | 371 | float GetDeviceScaleAdjustment(int min_width) { |
| 372 | static const float kMinFSM = 1.05f; |
| 373 | static const int kWidthForMinFSM = 320; |
| 374 | static const float kMaxFSM = 1.3f; |
| 375 | static const int kWidthForMaxFSM = 800; |
| 376 | |
| 377 | if (min_width <= kWidthForMinFSM) |
| 378 | return kMinFSM; |
| 379 | if (min_width >= kWidthForMaxFSM) |
| 380 | return kMaxFSM; |
| 381 | |
| 382 | // The font scale multiplier varies linearly between kMinFSM and kMaxFSM. |
| 383 | float ratio = static_cast<float>(min_width - kWidthForMinFSM) / |
| 384 | (kWidthForMaxFSM - kWidthForMinFSM); |
| 385 | return ratio * (kMaxFSM - kMinFSM) + kMinFSM; |
| 386 | } |
| 387 | #endif |
| 388 | |
Avi Drissman | 3397294 | 2020-06-19 14:16:01 | [diff] [blame] | 389 | // Used to attach the "set of fullscreen contents" to a browser context. Storing |
| 390 | // sets of WebContents on their browser context is done for two reasons. One, |
| 391 | // related WebContentses must necessarily share a browser context, so this saves |
| 392 | // lookup time by restricting to one specific browser context. Two, separating |
| 393 | // by browser context is preemptive paranoia about keeping things separate. |
| 394 | class FullscreenContentsHolder : public base::SupportsUserData::Data { |
| 395 | public: |
| 396 | FullscreenContentsHolder() = default; |
| 397 | ~FullscreenContentsHolder() override = default; |
| 398 | |
| 399 | FullscreenContentsHolder(const FullscreenContentsHolder&) = delete; |
| 400 | FullscreenContentsHolder& operator=(const FullscreenContentsHolder&) = delete; |
| 401 | |
| 402 | base::flat_set<WebContentsImpl*>* set() { return &set_; } |
| 403 | |
| 404 | private: |
| 405 | base::flat_set<WebContentsImpl*> set_; |
| 406 | }; |
| 407 | |
| 408 | const char kFullscreenContentsSet[] = "fullscreen-contents"; |
| 409 | |
| 410 | base::flat_set<WebContentsImpl*>* FullscreenContentsSet( |
| 411 | BrowserContext* browser_context) { |
| 412 | auto* set_holder = static_cast<FullscreenContentsHolder*>( |
| 413 | browser_context->GetUserData(kFullscreenContentsSet)); |
| 414 | if (!set_holder) { |
| 415 | auto new_holder = std::make_unique<FullscreenContentsHolder>(); |
| 416 | set_holder = new_holder.get(); |
| 417 | browser_context->SetUserData(kFullscreenContentsSet, std::move(new_holder)); |
| 418 | } |
| 419 | |
| 420 | return set_holder->set(); |
| 421 | } |
| 422 | |
Brad Triebwasser | 5eb94a9 | 2022-10-13 22:34:28 | [diff] [blame] | 423 | // Returns true if `host` has the Window Management permission granted. |
| 424 | bool IsWindowManagementGranted(RenderFrameHost* host) { |
Illia Klimov | 4a2cb19 | 2022-03-15 06:31:35 | [diff] [blame] | 425 | content::PermissionController* permission_controller = |
| 426 | host->GetBrowserContext()->GetPermissionController(); |
| 427 | DCHECK(permission_controller); |
Mike Wasserman | 909aa2c | 2021-06-02 02:36:59 | [diff] [blame] | 428 | |
Illia Klimov | 4a2cb19 | 2022-03-15 06:31:35 | [diff] [blame] | 429 | return permission_controller->GetPermissionStatusForCurrentDocument( |
Brad Triebwasser | 334d00e | 2022-10-17 18:47:57 | [diff] [blame] | 430 | blink::PermissionType::WINDOW_MANAGEMENT, host) == |
Illia Klimov | 4a2cb19 | 2022-03-15 06:31:35 | [diff] [blame] | 431 | blink::mojom::PermissionStatus::GRANTED; |
Mike Wasserman | 5b7d3bf | 2020-08-21 21:52:26 | [diff] [blame] | 432 | } |
| 433 | |
Mike Wasserman | b6bc015 | 2020-08-25 22:46:20 | [diff] [blame] | 434 | // Adjust the requested |bounds| for opening or placing a window and return the |
| 435 | // id of the display where the window will be placed. The bounds may not extend |
| 436 | // outside a single screen's work area, and the |host| requires permission to |
| 437 | // specify bounds on a screen other than its current screen. |
Mike Wasserman | dabad61 | 2020-08-13 04:26:27 | [diff] [blame] | 438 | // TODO(crbug.com/897300): These adjustments are inaccurate for window.open(), |
| 439 | // which specifies the inner content size, and for window.moveTo, resizeTo, etc. |
| 440 | // calls on newly created windows, which may pass empty sizes or positions to |
| 441 | // indicate uninitialized placement information in the renderer. Constraints |
| 442 | // enforced later should resolve most inaccuracies, but this early enforcement |
| 443 | // is needed to ensure bounds indicate the appropriate display. |
Mike Wasserman | b6bc015 | 2020-08-25 22:46:20 | [diff] [blame] | 444 | int64_t AdjustRequestedWindowBounds(gfx::Rect* bounds, RenderFrameHost* host) { |
Mike Wasserman | dabad61 | 2020-08-13 04:26:27 | [diff] [blame] | 445 | auto* screen = display::Screen::GetScreen(); |
Mike Wasserman | b6bc015 | 2020-08-25 22:46:20 | [diff] [blame] | 446 | auto display = screen->GetDisplayMatching(*bounds); |
Mike Wasserman | dabad61 | 2020-08-13 04:26:27 | [diff] [blame] | 447 | |
| 448 | // Check, but do not prompt, for permission to place windows on other screens. |
| 449 | // Sites generally need permission to get such bounds in the first place. |
| 450 | // Also clamp offscreen bounds to the window's current screen. |
Brad Triebwasser | 5eb94a9 | 2022-10-13 22:34:28 | [diff] [blame] | 451 | if (!bounds->Intersects(display.bounds()) || !IsWindowManagementGranted(host)) |
Mike Wasserman | dabad61 | 2020-08-13 04:26:27 | [diff] [blame] | 452 | display = screen->GetDisplayNearestView(host->GetNativeView()); |
Mike Wasserman | dabad61 | 2020-08-13 04:26:27 | [diff] [blame] | 453 | |
Mike Wasserman | b6bc015 | 2020-08-25 22:46:20 | [diff] [blame] | 454 | bounds->AdjustToFit(display.work_area()); |
| 455 | return display.id(); |
Mike Wasserman | dabad61 | 2020-08-13 04:26:27 | [diff] [blame] | 456 | } |
| 457 | |
tom | 083fe899 | 2021-10-21 17:48:45 | [diff] [blame] | 458 | // A ColorProviderSource used when one has not been explicitly set. This source |
| 459 | // only reflects theme information present in the web NativeTheme singleton. |
| 460 | // Keep this an implementation detail of WebContentsImpl as we should not be |
| 461 | // exposing a default ColorProviderSource generally. |
| 462 | class DefaultColorProviderSource : public ui::ColorProviderSource, |
| 463 | public ui::NativeThemeObserver { |
| 464 | public: |
| 465 | DefaultColorProviderSource() { |
| 466 | native_theme_observation_.Observe(ui::NativeTheme::GetInstanceForWeb()); |
| 467 | } |
| 468 | DefaultColorProviderSource(const DefaultColorProviderSource&) = delete; |
| 469 | DefaultColorProviderSource& operator=(const DefaultColorProviderSource&) = |
| 470 | delete; |
| 471 | ~DefaultColorProviderSource() override = default; |
| 472 | |
| 473 | static DefaultColorProviderSource* GetInstance() { |
| 474 | static base::NoDestructor<DefaultColorProviderSource> instance; |
| 475 | return instance.get(); |
| 476 | } |
| 477 | |
| 478 | // ui::ColorProviderSource: |
| 479 | const ui::ColorProvider* GetColorProvider() const override { |
| 480 | return ui::ColorProviderManager::Get().GetColorProviderFor( |
Allen Bauer | ec3e8e15 | 2022-03-17 17:47:38 | [diff] [blame] | 481 | GetColorProviderKey()); |
tom | 083fe899 | 2021-10-21 17:48:45 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | // ui::NativeThemeObserver: |
| 485 | void OnNativeThemeUpdated(ui::NativeTheme* observed_theme) override { |
| 486 | DCHECK(native_theme_observation_.IsObservingSource(observed_theme)); |
| 487 | NotifyColorProviderChanged(); |
| 488 | } |
| 489 | |
Allen Bauer | ec3e8e15 | 2022-03-17 17:47:38 | [diff] [blame] | 490 | protected: |
| 491 | // ui::ColorProviderSource: |
| 492 | ui::ColorProviderManager::Key GetColorProviderKey() const override { |
| 493 | return ui::NativeTheme::GetInstanceForWeb()->GetColorProviderKey(nullptr); |
| 494 | } |
| 495 | |
tom | 083fe899 | 2021-10-21 17:48:45 | [diff] [blame] | 496 | private: |
| 497 | base::ScopedObservation<ui::NativeTheme, ui::NativeThemeObserver> |
| 498 | native_theme_observation_{this}; |
| 499 | }; |
tom | 855c4bf3 | 2021-09-30 00:27:26 | [diff] [blame] | 500 | |
Dave Tapuska | 21e9da0 | 2021-12-15 20:58:20 | [diff] [blame] | 501 | size_t GetFrameTreeSize(FrameTree* frame_tree) { |
| 502 | size_t tree_size = 0; |
| 503 | FrameTree::NodeRange node_range = frame_tree->NodesIncludingInnerTreeNodes(); |
| 504 | FrameTree::NodeIterator node_iter = node_range.begin(); |
| 505 | while (node_iter != node_range.end()) { |
| 506 | // Skip over collapsed frame trees. |
| 507 | if ((*node_iter)->is_collapsed()) { |
| 508 | node_iter.AdvanceSkippingChildren(); |
| 509 | } else { |
| 510 | ++tree_size; |
| 511 | ++node_iter; |
| 512 | } |
| 513 | } |
| 514 | return tree_size; |
| 515 | } |
| 516 | |
lukasza | 6f8ac62 | 2017-06-06 03:10:20 | [diff] [blame] | 517 | } // namespace |
| 518 | |
Bruno Pitrus | 2b0fb8b | 2022-10-17 15:56:10 | [diff] [blame] | 519 | // This is a small helper class created while a JavaScript dialog is showing |
| 520 | // and destroyed when it's dismissed. Clients can register callbacks to receive |
| 521 | // a notification when the dialog is dismissed. |
| 522 | class JavaScriptDialogDismissNotifier { |
| 523 | public: |
| 524 | JavaScriptDialogDismissNotifier() = default; |
| 525 | |
| 526 | JavaScriptDialogDismissNotifier(const JavaScriptDialogDismissNotifier&) = |
| 527 | delete; |
| 528 | JavaScriptDialogDismissNotifier& operator=( |
| 529 | const JavaScriptDialogDismissNotifier&) = delete; |
| 530 | |
| 531 | ~JavaScriptDialogDismissNotifier() { |
| 532 | for (auto& callback : callbacks_) { |
| 533 | std::move(callback).Run(); |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | void NotifyOnDismiss(base::OnceClosure callback) { |
| 538 | callbacks_.push_back(std::move(callback)); |
| 539 | } |
| 540 | |
| 541 | private: |
| 542 | std::vector<base::OnceClosure> callbacks_; |
| 543 | }; |
| 544 | |
Joel Hockey | 8c2011b2 | 2020-04-30 05:07:19 | [diff] [blame] | 545 | CreatedWindow::CreatedWindow() = default; |
| 546 | CreatedWindow::CreatedWindow(std::unique_ptr<WebContentsImpl> contents, |
| 547 | GURL target_url) |
| 548 | : contents(std::move(contents)), target_url(std::move(target_url)) {} |
| 549 | CreatedWindow::~CreatedWindow() = default; |
| 550 | CreatedWindow::CreatedWindow(CreatedWindow&&) = default; |
| 551 | CreatedWindow& CreatedWindow::operator=(CreatedWindow&&) = default; |
| 552 | |
Erik Chen | bb8e738e | 2018-04-28 14:10:43 | [diff] [blame] | 553 | std::unique_ptr<WebContents> WebContents::Create( |
| 554 | const WebContents::CreateParams& params) { |
Albert J. Wong | 65fe64d | 2019-09-20 02:48:14 | [diff] [blame] | 555 | return WebContentsImpl::Create(params); |
| 556 | } |
| 557 | |
| 558 | std::unique_ptr<WebContentsImpl> WebContentsImpl::Create( |
| 559 | const CreateParams& params) { |
| 560 | return CreateWithOpener(params, FindOpenerRFH(params)); |
[email protected] | d1198fd | 2012-08-13 22:50:19 | [diff] [blame] | 561 | } |
| 562 | |
erikchen | ade1fef | 2018-05-02 22:07:57 | [diff] [blame] | 563 | std::unique_ptr<WebContents> WebContents::CreateWithSessionStorage( |
[email protected] | 54944cde | 2012-12-09 09:24:59 | [diff] [blame] | 564 | const WebContents::CreateParams& params, |
[email protected] | fdac6ade | 2013-07-20 01:06:30 | [diff] [blame] | 565 | const SessionStorageNamespaceMap& session_storage_namespace_map) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 566 | OPTIONAL_TRACE_EVENT0("content", "WebContents::CreateWithSessionStorage"); |
erikchen | ade1fef | 2018-05-02 22:07:57 | [diff] [blame] | 567 | std::unique_ptr<WebContentsImpl> new_contents( |
| 568 | new WebContentsImpl(params.browser_context)); |
Ian Clelland | 5cbaaf8 | 2017-11-27 22:00:03 | [diff] [blame] | 569 | RenderFrameHostImpl* opener_rfh = FindOpenerRFH(params); |
| 570 | FrameTreeNode* opener = nullptr; |
| 571 | if (opener_rfh) |
| 572 | opener = opener_rfh->frame_tree_node(); |
| 573 | new_contents->SetOpenerForNewContents(opener, params.opener_suppressed); |
[email protected] | fdac6ade | 2013-07-20 01:06:30 | [diff] [blame] | 574 | |
Fergal Daly | 55b6d72 | 2020-09-11 07:56:33 | [diff] [blame] | 575 | for (const auto& it : session_storage_namespace_map) { |
| 576 | new_contents->GetController().SetSessionStorageNamespace(it.first, |
| 577 | it.second.get()); |
[email protected] | fdac6ade | 2013-07-20 01:06:30 | [diff] [blame] | 578 | } |
| 579 | |
Sean Topping | bbc4a2bd | 2019-01-30 02:16:14 | [diff] [blame] | 580 | WebContentsImpl* outer_web_contents = nullptr; |
Sam McNally | 69a3377 | 2018-06-27 16:54:12 | [diff] [blame] | 581 | if (params.guest_delegate) { |
| 582 | // This makes |new_contents| act as a guest. |
| 583 | // For more info, see comment above class BrowserPluginGuest. |
Albert J. Wong | c55cc64d | 2018-10-12 02:50:04 | [diff] [blame] | 584 | BrowserPluginGuest::CreateInWebContents(new_contents.get(), |
| 585 | params.guest_delegate); |
Sean Topping | bbc4a2bd | 2019-01-30 02:16:14 | [diff] [blame] | 586 | outer_web_contents = static_cast<WebContentsImpl*>( |
| 587 | params.guest_delegate->GetOwnerWebContents()); |
Sam McNally | 69a3377 | 2018-06-27 16:54:12 | [diff] [blame] | 588 | } |
| 589 | |
Harkiran Bolaria | 5ce2763 | 2022-01-20 15:05:05 | [diff] [blame] | 590 | new_contents->Init(params, blink::FramePolicy()); |
Sean Topping | bbc4a2bd | 2019-01-30 02:16:14 | [diff] [blame] | 591 | if (outer_web_contents) |
| 592 | outer_web_contents->InnerWebContentsCreated(new_contents.get()); |
[email protected] | d1198fd | 2012-08-13 22:50:19 | [diff] [blame] | 593 | return new_contents; |
[email protected] | a81343d23 | 2011-12-27 07:39:20 | [diff] [blame] | 594 | } |
[email protected] | 746d305 | 2012-05-22 15:15:47 | [diff] [blame] | 595 | |
Elly Fong-Jones | 564de626 | 2022-07-28 21:11:16 | [diff] [blame] | 596 | base::CallbackListSubscription |
| 597 | WebContentsImpl::FriendWrapper::AddCreatedCallbackForTesting( |
nick | f9acfbe | 2014-12-23 19:12:37 | [diff] [blame] | 598 | const CreatedCallback& callback) { |
Elly Fong-Jones | 564de626 | 2022-07-28 21:11:16 | [diff] [blame] | 599 | return g_created_callbacks.Get().Add(callback); |
[email protected] | cbb1ef59 | 2013-06-05 19:49:46 | [diff] [blame] | 600 | } |
| 601 | |
avi | df38c95 | 2015-10-27 13:45:13 | [diff] [blame] | 602 | WebContents* WebContents::FromRenderViewHost(RenderViewHost* rvh) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 603 | OPTIONAL_TRACE_EVENT1(TRACE_DISABLED_BY_DEFAULT("content.verbose"), |
| 604 | "WebContents::FromRenderViewHost", "render_view_host", |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 605 | rvh); |
avi | df38c95 | 2015-10-27 13:45:13 | [diff] [blame] | 606 | if (!rvh) |
| 607 | return nullptr; |
Kevin McNee | 2c3ceca | 2021-09-17 22:52:48 | [diff] [blame] | 608 | return static_cast<WebContentsImpl*>( |
| 609 | static_cast<RenderViewHostImpl*>(rvh)->GetDelegate()); |
[email protected] | 746d305 | 2012-05-22 15:15:47 | [diff] [blame] | 610 | } |
| 611 | |
[email protected] | a86c0e96 | 2013-12-17 17:10:39 | [diff] [blame] | 612 | WebContents* WebContents::FromRenderFrameHost(RenderFrameHost* rfh) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 613 | OPTIONAL_TRACE_EVENT1(TRACE_DISABLED_BY_DEFAULT("content.verbose"), |
| 614 | "WebContents::FromRenderFrameHost", "render_frame_host", |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 615 | rfh); |
Fergal Daly | 5b64d47 | 2021-10-14 04:31:29 | [diff] [blame] | 616 | return WebContentsImpl::FromRenderFrameHostImpl( |
| 617 | static_cast<RenderFrameHostImpl*>(rfh)); |
| 618 | } |
| 619 | |
| 620 | WebContentsImpl* WebContentsImpl::FromRenderFrameHostImpl( |
| 621 | RenderFrameHostImpl* rfh) { |
| 622 | OPTIONAL_TRACE_EVENT1(TRACE_DISABLED_BY_DEFAULT("content.verbose"), |
| 623 | "WebContentsImpl::FromRenderFrameHostImpl", |
| 624 | "render_frame_host", rfh); |
avi | df38c95 | 2015-10-27 13:45:13 | [diff] [blame] | 625 | if (!rfh) |
| 626 | return nullptr; |
Fergal Daly | 5b64d47 | 2021-10-14 04:31:29 | [diff] [blame] | 627 | return static_cast<WebContentsImpl*>(rfh->delegate()); |
[email protected] | a86c0e96 | 2013-12-17 17:10:39 | [diff] [blame] | 628 | } |
| 629 | |
ananta | 4b7467a5 | 2016-09-23 01:42:38 | [diff] [blame] | 630 | WebContents* WebContents::FromFrameTreeNodeId(int frame_tree_node_id) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 631 | OPTIONAL_TRACE_EVENT1(TRACE_DISABLED_BY_DEFAULT("content.verbose"), |
| 632 | "WebContents::FromFrameTreeNodeId", |
| 633 | "frame_tree_node_id", frame_tree_node_id); |
ananta | 4b7467a5 | 2016-09-23 01:42:38 | [diff] [blame] | 634 | FrameTreeNode* frame_tree_node = |
| 635 | FrameTreeNode::GloballyFindByID(frame_tree_node_id); |
ananta | b8cfa3b | 2016-10-14 22:43:19 | [diff] [blame] | 636 | if (!frame_tree_node) |
| 637 | return nullptr; |
jam | 8c4edd0 | 2017-05-06 18:50:33 | [diff] [blame] | 638 | return WebContentsImpl::FromFrameTreeNode(frame_tree_node); |
ananta | 4b7467a5 | 2016-09-23 01:42:38 | [diff] [blame] | 639 | } |
| 640 | |
Matt Falkenhagen | 888db59 | 2021-09-07 20:20:25 | [diff] [blame] | 641 | WebContentsImpl* WebContentsImpl::FromRenderWidgetHostImpl( |
| 642 | RenderWidgetHostImpl* rwh) { |
| 643 | if (!rwh) |
| 644 | return nullptr; |
| 645 | return static_cast<WebContentsImpl*>(rwh->delegate()); |
| 646 | } |
| 647 | |
leon.han | 552e9de | 2017-02-09 14:37:30 | [diff] [blame] | 648 | void WebContents::SetScreenOrientationDelegate( |
| 649 | ScreenOrientationDelegate* delegate) { |
| 650 | ScreenOrientationProvider::SetDelegate(delegate); |
| 651 | } |
| 652 | |
Avi Drissman | 783c123 | 2021-02-05 01:27:05 | [diff] [blame] | 653 | // WebContentsImpl::RenderWidgetHostDestructionObserver ----------------------- |
[email protected] | 7fff43e | 2013-05-21 20:21:10 | [diff] [blame] | 654 | |
Avi Drissman | 783c123 | 2021-02-05 01:27:05 | [diff] [blame] | 655 | class WebContentsImpl::RenderWidgetHostDestructionObserver |
| 656 | : public RenderWidgetHostObserver { |
[email protected] | 7fff43e | 2013-05-21 20:21:10 | [diff] [blame] | 657 | public: |
Avi Drissman | 783c123 | 2021-02-05 01:27:05 | [diff] [blame] | 658 | RenderWidgetHostDestructionObserver(WebContentsImpl* owner, |
| 659 | RenderWidgetHost* watched_host) |
| 660 | : owner_(owner), watched_host_(watched_host) { |
| 661 | watched_host_->AddObserver(this); |
| 662 | } |
| 663 | |
Peter Boström | 828b902 | 2021-09-21 02:28:43 | [diff] [blame] | 664 | RenderWidgetHostDestructionObserver( |
| 665 | const RenderWidgetHostDestructionObserver&) = delete; |
| 666 | RenderWidgetHostDestructionObserver& operator=( |
| 667 | const RenderWidgetHostDestructionObserver&) = delete; |
| 668 | |
Avi Drissman | 783c123 | 2021-02-05 01:27:05 | [diff] [blame] | 669 | ~RenderWidgetHostDestructionObserver() override { |
| 670 | watched_host_->RemoveObserver(this); |
| 671 | } |
| 672 | |
| 673 | // RenderWidgetHostObserver: |
| 674 | void RenderWidgetHostDestroyed(RenderWidgetHost* widget_host) override { |
| 675 | owner_->OnRenderWidgetHostDestroyed(widget_host); |
| 676 | } |
| 677 | |
| 678 | private: |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 679 | raw_ptr<WebContentsImpl> owner_; |
| 680 | raw_ptr<RenderWidgetHost> watched_host_; |
Avi Drissman | 783c123 | 2021-02-05 01:27:05 | [diff] [blame] | 681 | }; |
| 682 | |
| 683 | // WebContentsImpl::WebContentsDestructionObserver ---------------------------- |
| 684 | |
| 685 | class WebContentsImpl::WebContentsDestructionObserver |
| 686 | : public WebContentsObserver { |
| 687 | public: |
| 688 | WebContentsDestructionObserver(WebContentsImpl* owner, |
| 689 | WebContents* watched_contents) |
Fergal Daly | 09d6c76 | 2020-05-29 02:05:18 | [diff] [blame] | 690 | : WebContentsObserver(watched_contents), owner_(owner) {} |
[email protected] | 7fff43e | 2013-05-21 20:21:10 | [diff] [blame] | 691 | |
Peter Boström | 9b03653 | 2021-10-28 23:37:28 | [diff] [blame] | 692 | WebContentsDestructionObserver(const WebContentsDestructionObserver&) = |
| 693 | delete; |
| 694 | WebContentsDestructionObserver& operator=( |
| 695 | const WebContentsDestructionObserver&) = delete; |
| 696 | |
[email protected] | 7fff43e | 2013-05-21 20:21:10 | [diff] [blame] | 697 | // WebContentsObserver: |
dcheng | c2282aa | 2014-10-21 12:07:58 | [diff] [blame] | 698 | void WebContentsDestroyed() override { |
[email protected] | 12a4683 | 2014-05-09 13:35:58 | [diff] [blame] | 699 | owner_->OnWebContentsDestroyed( |
| 700 | static_cast<WebContentsImpl*>(web_contents())); |
[email protected] | 7fff43e | 2013-05-21 20:21:10 | [diff] [blame] | 701 | } |
| 702 | |
| 703 | private: |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 704 | raw_ptr<WebContentsImpl> owner_; |
[email protected] | 7fff43e | 2013-05-21 20:21:10 | [diff] [blame] | 705 | }; |
| 706 | |
Tom Burgin | 732656d3 | 2022-02-07 18:33:26 | [diff] [blame] | 707 | #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_MAC) |
Bo Liu | 2bceebc | 2021-06-03 15:06:33 | [diff] [blame] | 708 | // TODO(sreejakshetty): Make |WebContentsImpl::ColorChooserHolder| per-frame |
| 709 | // instead of WebContents-owned. |
| 710 | // WebContentsImpl::ColorChooserHolder ----------------------------------------- |
| 711 | class WebContentsImpl::ColorChooserHolder : public blink::mojom::ColorChooser { |
Joel Hockey | 85b379d | 2017-12-11 10:42:13 | [diff] [blame] | 712 | public: |
Bo Liu | 2bceebc | 2021-06-03 15:06:33 | [diff] [blame] | 713 | ColorChooserHolder( |
| 714 | mojo::PendingReceiver<blink::mojom::ColorChooser> receiver, |
| 715 | mojo::PendingRemote<blink::mojom::ColorChooserClient> client) |
Bo Liu | ab8f997 | 2021-04-08 22:44:36 | [diff] [blame] | 716 | : receiver_(this, std::move(receiver)), client_(std::move(client)) {} |
| 717 | |
Bo Liu | 2bceebc | 2021-06-03 15:06:33 | [diff] [blame] | 718 | ~ColorChooserHolder() override { |
Bo Liu | ab8f997 | 2021-04-08 22:44:36 | [diff] [blame] | 719 | if (chooser_) |
| 720 | chooser_->End(); |
Joel Hockey | 85b379d | 2017-12-11 10:42:13 | [diff] [blame] | 721 | } |
[email protected] | 8ed1647 | 2014-04-11 19:02:48 | [diff] [blame] | 722 | |
Bo Liu | ab8f997 | 2021-04-08 22:44:36 | [diff] [blame] | 723 | void SetChooser(std::unique_ptr<content::ColorChooser> chooser) { |
| 724 | chooser_ = std::move(chooser); |
| 725 | if (chooser_) { |
| 726 | receiver_.set_disconnect_handler( |
| 727 | base::BindOnce([](content::ColorChooser* chooser) { chooser->End(); }, |
| 728 | base::Unretained(chooser_.get()))); |
| 729 | } |
| 730 | } |
[email protected] | 8ed1647 | 2014-04-11 19:02:48 | [diff] [blame] | 731 | |
Joel Hockey | 85b379d | 2017-12-11 10:42:13 | [diff] [blame] | 732 | void SetSelectedColor(SkColor color) override { |
Bo Liu | 2bceebc | 2021-06-03 15:06:33 | [diff] [blame] | 733 | OPTIONAL_TRACE_EVENT0( |
| 734 | "content", "WebContentsImpl::ColorChooserHolder::SetSelectedColor"); |
Bo Liu | ab8f997 | 2021-04-08 22:44:36 | [diff] [blame] | 735 | if (chooser_) |
| 736 | chooser_->SetSelectedColor(color); |
Joel Hockey | 85b379d | 2017-12-11 10:42:13 | [diff] [blame] | 737 | } |
| 738 | |
| 739 | void DidChooseColorInColorChooser(SkColor color) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 740 | OPTIONAL_TRACE_EVENT0( |
| 741 | "content", |
Bo Liu | 2bceebc | 2021-06-03 15:06:33 | [diff] [blame] | 742 | "WebContentsImpl::ColorChooserHolder::DidChooseColorInColorChooser"); |
Joel Hockey | 85b379d | 2017-12-11 10:42:13 | [diff] [blame] | 743 | client_->DidChooseColor(color); |
| 744 | } |
| 745 | |
| 746 | private: |
| 747 | // Color chooser that was opened by this tab. |
| 748 | std::unique_ptr<content::ColorChooser> chooser_; |
| 749 | |
Julie Jeongeun Kim | 8e2879e | 2019-08-06 23:55:20 | [diff] [blame] | 750 | // mojo receiver. |
| 751 | mojo::Receiver<blink::mojom::ColorChooser> receiver_; |
Joel Hockey | 85b379d | 2017-12-11 10:42:13 | [diff] [blame] | 752 | |
| 753 | // mojo renderer client. |
Julie Jeongeun Kim | 8e2879e | 2019-08-06 23:55:20 | [diff] [blame] | 754 | mojo::Remote<blink::mojom::ColorChooserClient> client_; |
Joel Hockey | 85b379d | 2017-12-11 10:42:13 | [diff] [blame] | 755 | }; |
Tom Burgin | 732656d3 | 2022-02-07 18:33:26 | [diff] [blame] | 756 | #endif // BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_MAC) |
nick | a0ac838 | 2016-12-15 23:59:23 | [diff] [blame] | 757 | |
lazyboy | 6ec48b2a | 2015-06-29 15:18:14 | [diff] [blame] | 758 | // WebContentsImpl::WebContentsTreeNode ---------------------------------------- |
lfg | 60123369 | 2017-03-06 22:45:44 | [diff] [blame] | 759 | WebContentsImpl::WebContentsTreeNode::WebContentsTreeNode( |
| 760 | WebContentsImpl* current_web_contents) |
| 761 | : current_web_contents_(current_web_contents), |
| 762 | outer_web_contents_(nullptr), |
lazyboy | 6ec48b2a | 2015-06-29 15:18:14 | [diff] [blame] | 763 | outer_contents_frame_tree_node_id_( |
avallee | 0206f78 | 2016-07-28 18:55:33 | [diff] [blame] | 764 | FrameTreeNode::kFrameTreeNodeInvalidId), |
Carlos Caballero | 6a99dac | 2021-11-03 10:41:17 | [diff] [blame] | 765 | focused_frame_tree_( |
| 766 | current_web_contents->GetPrimaryFrameTree().GetSafeRef()) {} |
lazyboy | 6ec48b2a | 2015-06-29 15:18:14 | [diff] [blame] | 767 | |
Fergal Daly | 55b6d72 | 2020-09-11 07:56:33 | [diff] [blame] | 768 | WebContentsImpl::WebContentsTreeNode::~WebContentsTreeNode() = default; |
lazyboy | 6ec48b2a | 2015-06-29 15:18:14 | [diff] [blame] | 769 | |
Lucas Furukawa Gadani | 9912582 | 2019-01-03 15:41:49 | [diff] [blame] | 770 | std::unique_ptr<WebContents> |
| 771 | WebContentsImpl::WebContentsTreeNode::DisconnectFromOuterWebContents() { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 772 | OPTIONAL_TRACE_EVENT0("content", |
| 773 | "WebContentsTreeNode::DisconnectFromOuterWebContents"); |
Lucas Furukawa Gadani | 9912582 | 2019-01-03 15:41:49 | [diff] [blame] | 774 | std::unique_ptr<WebContents> inner_contents = |
| 775 | outer_web_contents_->node_.DetachInnerWebContents(current_web_contents_); |
| 776 | OuterContentsFrameTreeNode()->RemoveObserver(this); |
| 777 | outer_contents_frame_tree_node_id_ = FrameTreeNode::kFrameTreeNodeInvalidId; |
| 778 | outer_web_contents_ = nullptr; |
| 779 | return inner_contents; |
| 780 | } |
| 781 | |
paulmeyer | feafc2d | 2017-04-25 21:46:40 | [diff] [blame] | 782 | void WebContentsImpl::WebContentsTreeNode::AttachInnerWebContents( |
Lucas Furukawa Gadani | 6e5b4f3 | 2019-03-02 04:18:50 | [diff] [blame] | 783 | std::unique_ptr<WebContents> inner_web_contents, |
| 784 | RenderFrameHostImpl* render_frame_host) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 785 | OPTIONAL_TRACE_EVENT0("content", |
| 786 | "WebContentsTreeNode::AttachInnerWebContents"); |
Lucas Furukawa Gadani | 6e5b4f3 | 2019-03-02 04:18:50 | [diff] [blame] | 787 | WebContentsImpl* inner_web_contents_impl = |
| 788 | static_cast<WebContentsImpl*>(inner_web_contents.get()); |
| 789 | WebContentsTreeNode& inner_web_contents_node = inner_web_contents_impl->node_; |
| 790 | |
Lucas Furukawa Gadani | 6e5b4f3 | 2019-03-02 04:18:50 | [diff] [blame] | 791 | inner_web_contents_node.outer_web_contents_ = current_web_contents_; |
| 792 | inner_web_contents_node.outer_contents_frame_tree_node_id_ = |
| 793 | render_frame_host->frame_tree_node()->frame_tree_node_id(); |
| 794 | |
Lucas Furukawa Gadani | 3d38ec15 | 2018-10-26 20:55:18 | [diff] [blame] | 795 | inner_web_contents_.push_back(std::move(inner_web_contents)); |
Lucas Furukawa Gadani | 6e5b4f3 | 2019-03-02 04:18:50 | [diff] [blame] | 796 | |
| 797 | render_frame_host->frame_tree_node()->AddObserver(&inner_web_contents_node); |
Jeremy Roman | 03ce13ce | 2020-05-29 22:16:57 | [diff] [blame] | 798 | current_web_contents_->InnerWebContentsAttached(inner_web_contents_impl); |
paulmeyer | feafc2d | 2017-04-25 21:46:40 | [diff] [blame] | 799 | } |
| 800 | |
Lucas Furukawa Gadani | 3d38ec15 | 2018-10-26 20:55:18 | [diff] [blame] | 801 | std::unique_ptr<WebContents> |
| 802 | WebContentsImpl::WebContentsTreeNode::DetachInnerWebContents( |
paulmeyer | feafc2d | 2017-04-25 21:46:40 | [diff] [blame] | 803 | WebContentsImpl* inner_web_contents) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 804 | OPTIONAL_TRACE_EVENT0( |
| 805 | "content", |
| 806 | "WebContentsImpl::WebContentsTreeNode::DetachInnerWebContents"); |
Lucas Furukawa Gadani | 3d38ec15 | 2018-10-26 20:55:18 | [diff] [blame] | 807 | std::unique_ptr<WebContents> detached_contents; |
| 808 | for (std::unique_ptr<WebContents>& web_contents : inner_web_contents_) { |
| 809 | if (web_contents.get() == inner_web_contents) { |
| 810 | detached_contents = std::move(web_contents); |
| 811 | std::swap(web_contents, inner_web_contents_.back()); |
| 812 | inner_web_contents_.pop_back(); |
Jeremy Roman | 03ce13ce | 2020-05-29 22:16:57 | [diff] [blame] | 813 | current_web_contents_->InnerWebContentsDetached(inner_web_contents); |
Lucas Furukawa Gadani | 3d38ec15 | 2018-10-26 20:55:18 | [diff] [blame] | 814 | return detached_contents; |
| 815 | } |
| 816 | } |
| 817 | |
| 818 | NOTREACHED(); |
| 819 | return nullptr; |
paulmeyer | feafc2d | 2017-04-25 21:46:40 | [diff] [blame] | 820 | } |
| 821 | |
lfg | a3e2bdc | 2017-03-07 20:44:01 | [diff] [blame] | 822 | FrameTreeNode* |
| 823 | WebContentsImpl::WebContentsTreeNode::OuterContentsFrameTreeNode() const { |
| 824 | return FrameTreeNode::GloballyFindByID(outer_contents_frame_tree_node_id_); |
lfg | 60123369 | 2017-03-06 22:45:44 | [diff] [blame] | 825 | } |
| 826 | |
lfg | a3e2bdc | 2017-03-07 20:44:01 | [diff] [blame] | 827 | void WebContentsImpl::WebContentsTreeNode::OnFrameTreeNodeDestroyed( |
| 828 | FrameTreeNode* node) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 829 | OPTIONAL_TRACE_EVENT0("content", |
| 830 | "WebContentsTreeNode::OnFrameTreeNodeDestroyed"); |
lfg | a3e2bdc | 2017-03-07 20:44:01 | [diff] [blame] | 831 | DCHECK_EQ(outer_contents_frame_tree_node_id_, node->frame_tree_node_id()) |
| 832 | << "WebContentsTreeNode should only receive notifications for the " |
| 833 | "FrameTreeNode in its outer WebContents that hosts it."; |
Lucas Furukawa Gadani | 3d38ec15 | 2018-10-26 20:55:18 | [diff] [blame] | 834 | |
Paul Semel | a6d42372 | 2022-09-28 18:17:28 | [diff] [blame] | 835 | node->RemoveObserver(this); |
Lucas Furukawa Gadani | 3d38ec15 | 2018-10-26 20:55:18 | [diff] [blame] | 836 | // Deletes |this| too. |
| 837 | outer_web_contents_->node_.DetachInnerWebContents(current_web_contents_); |
lazyboy | 6ec48b2a | 2015-06-29 15:18:14 | [diff] [blame] | 838 | } |
| 839 | |
Dave Tapuska | d8b0530f | 2021-10-19 15:12:31 | [diff] [blame] | 840 | void WebContentsImpl::WebContentsTreeNode::SetFocusedFrameTree( |
| 841 | FrameTree* frame_tree) { |
| 842 | OPTIONAL_TRACE_EVENT0("content", "WebContentsTreeNode::SetFocusedFrameTree"); |
avallee | 0206f78 | 2016-07-28 18:55:33 | [diff] [blame] | 843 | DCHECK(!outer_web_contents()) |
| 844 | << "Only the outermost WebContents tracks focus."; |
Dave Tapuska | d8b0530f | 2021-10-19 15:12:31 | [diff] [blame] | 845 | focused_frame_tree_ = frame_tree->GetSafeRef(); |
avallee | 0206f78 | 2016-07-28 18:55:33 | [diff] [blame] | 846 | } |
| 847 | |
paulmeyer | feafc2d | 2017-04-25 21:46:40 | [diff] [blame] | 848 | WebContentsImpl* |
| 849 | WebContentsImpl::WebContentsTreeNode::GetInnerWebContentsInFrame( |
| 850 | const FrameTreeNode* frame) { |
| 851 | auto ftn_id = frame->frame_tree_node_id(); |
Lucas Furukawa Gadani | 3d38ec15 | 2018-10-26 20:55:18 | [diff] [blame] | 852 | for (auto& contents : inner_web_contents_) { |
| 853 | WebContentsImpl* impl = static_cast<WebContentsImpl*>(contents.get()); |
| 854 | if (impl->node_.outer_contents_frame_tree_node_id() == ftn_id) { |
| 855 | return impl; |
paulmeyer | feafc2d | 2017-04-25 21:46:40 | [diff] [blame] | 856 | } |
| 857 | } |
| 858 | return nullptr; |
| 859 | } |
| 860 | |
Lucas Furukawa Gadani | 3d38ec15 | 2018-10-26 20:55:18 | [diff] [blame] | 861 | std::vector<WebContentsImpl*> |
| 862 | WebContentsImpl::WebContentsTreeNode::GetInnerWebContents() const { |
| 863 | std::vector<WebContentsImpl*> inner_web_contents; |
| 864 | for (auto& contents : inner_web_contents_) |
| 865 | inner_web_contents.push_back(static_cast<WebContentsImpl*>(contents.get())); |
| 866 | |
| 867 | return inner_web_contents; |
paulmeyer | feafc2d | 2017-04-25 21:46:40 | [diff] [blame] | 868 | } |
| 869 | |
Alexander Timin | 90c20c3 | 2020-08-12 15:28:32 | [diff] [blame] | 870 | // WebContentsObserverList ----------------------------------------------------- |
| 871 | WebContentsImpl::WebContentsObserverList::WebContentsObserverList() = default; |
| 872 | WebContentsImpl::WebContentsObserverList::~WebContentsObserverList() = default; |
| 873 | |
| 874 | void WebContentsImpl::WebContentsObserverList::AddObserver( |
| 875 | WebContentsObserver* observer) { |
| 876 | observers_.AddObserver(observer); |
| 877 | } |
| 878 | |
| 879 | void WebContentsImpl::WebContentsObserverList::RemoveObserver( |
| 880 | WebContentsObserver* observer) { |
| 881 | observers_.RemoveObserver(observer); |
| 882 | } |
| 883 | |
[email protected] | b172aee | 2012-04-10 17:05:26 | [diff] [blame] | 884 | // WebContentsImpl ------------------------------------------------------------- |
[email protected] | 420ae01 | 2009-04-24 05:16:32 | [diff] [blame] | 885 | |
Lukasz Anforowicz | 7a0381c | 2022-12-07 20:23:08 | [diff] [blame] | 886 | namespace { |
| 887 | |
| 888 | // A helper for ensuring that WebContents are closed (or otherwise destroyed) |
| 889 | // *before* their BrowserContext is destroyed. |
| 890 | class WebContentsOfBrowserContext : public base::SupportsUserData::Data { |
| 891 | public: |
| 892 | static void Attach(WebContentsImpl& web_contents) { |
| 893 | WebContentsOfBrowserContext* self = |
| 894 | GetOrCreate(*web_contents.GetBrowserContext()); |
| 895 | self->web_contents_set_.insert(&web_contents); |
| 896 | } |
| 897 | |
| 898 | static void Detach(WebContentsImpl& web_contents) { |
| 899 | WebContentsOfBrowserContext* self = |
| 900 | GetOrCreate(*web_contents.GetBrowserContext()); |
| 901 | self->web_contents_set_.erase(&web_contents); |
| 902 | } |
| 903 | |
| 904 | ~WebContentsOfBrowserContext() override { |
| 905 | // The ~WebContentsOfBrowserContext destructor is called when the |
| 906 | // BrowserContext (and its UserData) gets destroyed. At this point |
| 907 | // in time all the WebContents of this BrowserContext should have been |
| 908 | // already closed (i.e. we expect the `web_contents_set_` set to be empty at |
| 909 | // this point - emptied by calls to the `Detach` method above). |
| 910 | if (web_contents_set_.empty()) |
| 911 | return; // Everything is okay - nothing to warn about. |
| 912 | |
Nicolas Ouellet-Payeur | 177a6c4 | 2023-01-25 20:25:07 | [diff] [blame] | 913 | #if BUILDFLAG(IS_ANDROID) |
| 914 | JNIEnv* env = base::android::AttachCurrentThread(); |
| 915 | #endif // BUILDFLAG(IS_ANDROID) |
| 916 | |
Lukasz Anforowicz | 7a0381c | 2022-12-07 20:23:08 | [diff] [blame] | 917 | // Any remaining WebContents contain dangling pointers to the |
| 918 | // BrowserContext being destroyed. Such WebContents (and their |
| 919 | // RenderFrameHosts, SiteInstances, etc.) risk causing |
| 920 | // use-after-free bugs. For more discussion about managing the |
| 921 | // lifetime of WebContents please see https://crbug.com/1376879#c44. |
| 922 | for (WebContents* web_contents_with_dangling_ptr_to_browser_context : |
| 923 | web_contents_set_) { |
| 924 | std::string creator = web_contents_with_dangling_ptr_to_browser_context |
| 925 | ->GetCreatorLocation() |
| 926 | .ToString(); |
| 927 | SCOPED_CRASH_KEY_STRING256("shutdown", "web_contents/creator", creator); |
Nicolas Ouellet-Payeur | 177a6c4 | 2023-01-25 20:25:07 | [diff] [blame] | 928 | |
| 929 | #if BUILDFLAG(IS_ANDROID) |
| 930 | // On Android, also report the Java stack trace from WebContents's |
| 931 | // creation. |
| 932 | WebContentsAndroid::ReportDanglingPtrToBrowserContext( |
| 933 | env, web_contents_with_dangling_ptr_to_browser_context); |
| 934 | #endif // BUILDFLAG(IS_ANDROID) |
| 935 | |
Lukasz Anforowicz | 7a0381c | 2022-12-07 20:23:08 | [diff] [blame] | 936 | NOTREACHED() |
| 937 | << "BrowserContext is getting destroyed without first closing all " |
Lukasz Anforowicz | 1746d87 | 2023-01-03 23:53:26 | [diff] [blame] | 938 | << "WebContents (for more info see https://crbug.com/1376879#c44); " |
| 939 | << "creator = " << creator; |
Lukasz Anforowicz | 7a0381c | 2022-12-07 20:23:08 | [diff] [blame] | 940 | base::debug::DumpWithoutCrashing(); |
| 941 | } |
| 942 | } |
| 943 | |
| 944 | std::unique_ptr<Data> Clone() override { |
| 945 | // Indicate to `SupportsUserData` / `BrowserContext` that cloning is not |
| 946 | // supported. |
| 947 | return nullptr; |
| 948 | } |
| 949 | |
| 950 | private: |
| 951 | WebContentsOfBrowserContext() = default; |
| 952 | |
| 953 | // Gets WebContentsOfBrowserContext associated with the given |
| 954 | // `browser_context` (creating a new WebContentsOfBrowserContext if |
| 955 | // necessary - if one hasn't been created yet). |
| 956 | static WebContentsOfBrowserContext* GetOrCreate( |
| 957 | BrowserContext& browser_context) { |
| 958 | static const char* kUserDataKey = "WebContentsOfBrowserContext/UserDataKey"; |
| 959 | WebContentsOfBrowserContext* result = |
| 960 | static_cast<WebContentsOfBrowserContext*>( |
| 961 | browser_context.GetUserData(kUserDataKey)); |
| 962 | if (!result) { |
| 963 | result = new WebContentsOfBrowserContext(); |
| 964 | browser_context.SetUserData(kUserDataKey, base::WrapUnique(result)); |
| 965 | } |
| 966 | return result; |
| 967 | } |
| 968 | |
| 969 | // Set of all `WebContents` within the tracked `BrowserContext`. |
| 970 | // |
| 971 | // Usage of `raw_ptr` below is okay (i.e. it shouldn't dangle), because |
| 972 | // when `WebContentsImpl`'s destructor runs, then it removes the set entry |
| 973 | // (by calling `Detach`). |
| 974 | std::set<raw_ptr<WebContents>> web_contents_set_; |
| 975 | }; |
| 976 | |
| 977 | } // namespace |
| 978 | |
alexmos | e201c7cd | 2015-06-10 17:14:21 | [diff] [blame] | 979 | WebContentsImpl::WebContentsImpl(BrowserContext* browser_context) |
Ivan Kotenkov | 2c0d2bb3 | 2017-11-01 15:41:28 | [diff] [blame] | 980 | : delegate_(nullptr), |
Ivan Kotenkov | 2c0d2bb3 | 2017-11-01 15:41:28 | [diff] [blame] | 981 | render_view_host_delegate_view_(nullptr), |
Rakina Zata Amni | c7ffea88 | 2021-08-16 10:04:28 | [diff] [blame] | 982 | opened_by_another_window_(false), |
Carlos Caballero | b65b6e3a | 2021-11-15 10:09:00 | [diff] [blame] | 983 | primary_frame_tree_(browser_context, |
| 984 | this, |
| 985 | this, |
| 986 | this, |
| 987 | this, |
| 988 | this, |
| 989 | this, |
| 990 | this, |
| 991 | this, |
Danil Somsikov | 259aa65f | 2022-11-11 20:49:44 | [diff] [blame] | 992 | FrameTree::Type::kPrimary), |
Dave Tapuska | d8b0530f | 2021-10-19 15:12:31 | [diff] [blame] | 993 | node_(this), |
Khushal | c5eaf22 | 2021-06-30 20:15:48 | [diff] [blame] | 994 | primary_main_frame_process_status_( |
| 995 | base::TERMINATION_STATUS_STILL_RUNNING), |
| 996 | primary_main_frame_process_error_code_(0), |
Jan Wilken Dörrie | aace0cfef | 2021-03-11 22:01:58 | [diff] [blame] | 997 | load_state_(net::LOAD_STATE_IDLE, std::u16string()), |
[email protected] | 094e5b2 | 2009-09-25 04:23:56 | [diff] [blame] | 998 | upload_size_(0), |
| 999 | upload_position_(0), |
dfalcantara | e6b7b4675 | 2015-07-10 18:14:16 | [diff] [blame] | 1000 | is_resume_pending_(false), |
[email protected] | fdd61c6 | 2009-04-22 19:22:57 | [diff] [blame] | 1001 | notify_disconnection_(false), |
Ivan Kotenkov | 2c0d2bb3 | 2017-11-01 15:41:28 | [diff] [blame] | 1002 | dialog_manager_(nullptr), |
[email protected] | 7ab1e7d6 | 2009-10-14 23:32:01 | [diff] [blame] | 1003 | is_showing_before_unload_dialog_(false), |
gab | 0dccfef | 2015-05-20 18:43:39 | [diff] [blame] | 1004 | last_active_time_(base::TimeTicks::Now()), |
[email protected] | ebf40a7 | 2010-07-22 01:46:38 | [diff] [blame] | 1005 | closed_by_user_gesture_(false), |
danakj | 938b37a6 | 2019-09-24 18:35:54 | [diff] [blame] | 1006 | minimum_zoom_percent_( |
| 1007 | static_cast<int>(blink::kMinimumPageZoomFactor * 100)), |
| 1008 | maximum_zoom_percent_( |
| 1009 | static_cast<int>(blink::kMaximumPageZoomFactor * 100)), |
w.shackleton | 49bcd39 | 2016-01-06 17:38:22 | [diff] [blame] | 1010 | zoom_scroll_remainder_(0), |
[email protected] | 34ff1cfc | 2014-08-20 06:16:05 | [diff] [blame] | 1011 | force_disable_overscroll_content_(false), |
[email protected] | 9564021 | 2014-07-26 18:14:30 | [diff] [blame] | 1012 | last_dialog_suppressed_(false), |
waffles | 1c12bf40 | 2016-02-10 17:45:48 | [diff] [blame] | 1013 | accessibility_mode_( |
Dominic Mazzoni | 21fb028 | 2019-02-13 18:32:47 | [diff] [blame] | 1014 | GetContentClient()->browser()->GetAXModeForBrowserContext( |
| 1015 | browser_context)), |
avayvod | cc85bbd | 2015-08-28 19:11:15 | [diff] [blame] | 1016 | audio_stream_monitor_(this), |
Thomas Guilbert | 882c4cec | 2019-02-14 05:55:45 | [diff] [blame] | 1017 | media_web_contents_observer_( |
| 1018 | std::make_unique<MediaWebContentsObserver>(this)), |
qinmin | 72e8bd0 | 2016-10-21 19:35:37 | [diff] [blame] | 1019 | is_overlay_content_(false), |
ekaramad | f6750aa | 2017-06-06 18:29:42 | [diff] [blame] | 1020 | showing_context_menu_(false), |
Johann | 344e1c7 | 2022-11-22 07:22:30 | [diff] [blame] | 1021 | prerender_host_registry_(std::make_unique<PrerenderHostRegistry>(*this)), |
Eric Seckler | ab20ea2 | 2021-02-08 11:39:26 | [diff] [blame] | 1022 | audible_power_mode_voter_( |
| 1023 | power_scheduler::PowerModeArbiter::GetInstance()->NewVoter( |
| 1024 | "PowerModeVoter.Audible")) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 1025 | TRACE_EVENT0("content", "WebContentsImpl::WebContentsImpl"); |
Lukasz Anforowicz | 7a0381c | 2022-12-07 20:23:08 | [diff] [blame] | 1026 | WebContentsOfBrowserContext::Attach(*this); |
K. Moon | 9ae6ef2c | 2022-08-18 01:30:06 | [diff] [blame] | 1027 | #if BUILDFLAG(ENABLE_PPAPI) |
Fergal Daly | 55b6d72 | 2020-09-11 07:56:33 | [diff] [blame] | 1028 | pepper_playback_observer_ = std::make_unique<PepperPlaybackObserver>(this); |
zqzhang | 181047e6 | 2016-07-01 13:37:17 | [diff] [blame] | 1029 | #endif |
blundell | e75a8f9 | 2017-03-27 08:11:17 | [diff] [blame] | 1030 | |
Xiaohan Wang | 24ec934 | 2022-01-15 17:34:22 | [diff] [blame] | 1031 | #if BUILDFLAG(IS_ANDROID) |
Elly Fong-Jones | cdd7ae8 | 2019-02-20 23:26:33 | [diff] [blame] | 1032 | display_cutout_host_impl_ = std::make_unique<DisplayCutoutHostImpl>(this); |
Becca Hughes | 8633462 | 2018-07-09 21:29:38 | [diff] [blame] | 1033 | #endif |
John Delaney | 732721e9 | 2020-02-07 23:11:27 | [diff] [blame] | 1034 | |
tom | 083fe899 | 2021-10-21 17:48:45 | [diff] [blame] | 1035 | // WebContents can exist independently of a ColorProviderSource and is still |
| 1036 | // expected to be able to render its content and be inserted into a |
| 1037 | // gfx::NativeView hierarchy. Whilst most WebContents clients should be |
| 1038 | // setting a ColorProviderSource we must accommodate for cases where this is |
| 1039 | // not currently being done. For these cases fallback to the default source. |
| 1040 | SetColorProviderSource(DefaultColorProviderSource::GetInstance()); |
| 1041 | |
Alison Maher | 7f366dc6 | 2020-03-03 19:46:00 | [diff] [blame] | 1042 | ui::NativeTheme* native_theme = ui::NativeTheme::GetInstanceForWeb(); |
Sigurdur Asgeirsson | 103e2fb | 2020-11-10 00:50:16 | [diff] [blame] | 1043 | native_theme_observation_.Observe(native_theme); |
Alison Maher | 7f366dc6 | 2020-03-03 19:46:00 | [diff] [blame] | 1044 | using_dark_colors_ = native_theme->ShouldUseDarkColors(); |
| 1045 | preferred_color_scheme_ = native_theme->GetPreferredColorScheme(); |
Alison Maher | 3b11e94 | 2020-10-27 17:17:50 | [diff] [blame] | 1046 | preferred_contrast_ = native_theme->GetPreferredContrast(); |
Alison Maher | 7f366dc6 | 2020-03-03 19:46:00 | [diff] [blame] | 1047 | |
Mike Wasserman | b213b935 | 2020-04-16 00:32:34 | [diff] [blame] | 1048 | screen_change_monitor_ = |
| 1049 | std::make_unique<ScreenChangeMonitor>(base::BindRepeating( |
| 1050 | &WebContentsImpl::OnScreensChange, base::Unretained(this))); |
| 1051 | |
Yao Xiao | 1ac702d | 2022-06-08 17:20:49 | [diff] [blame] | 1052 | if (base::FeatureList::IsEnabled(blink::features::kSharedStorageAPI)) { |
| 1053 | SharedStorageBudgetCharger::CreateForWebContents(this); |
| 1054 | } |
| 1055 | |
Bartek Nowierski | bc01b711 | 2023-02-01 14:11:42 | [diff] [blame] | 1056 | #if BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC) && BUILDFLAG(USE_STARSCAN) |
Anton Bikineev | 99a0052 | 2021-08-25 08:57:59 | [diff] [blame] | 1057 | // TODO(1231679): Remove or move to another place after finishing the PCScan |
| 1058 | // experiment. |
Kalvin Lee | 9833bf9 | 2022-06-30 02:12:05 | [diff] [blame] | 1059 | if (partition_alloc::internal::PCScan::IsInitialized()) { |
Anton Bikineev | 99a0052 | 2021-08-25 08:57:59 | [diff] [blame] | 1060 | star_scan_load_observer_ = std::make_unique<StarScanLoadObserver>(this); |
| 1061 | } |
Bartek Nowierski | bc01b711 | 2023-02-01 14:11:42 | [diff] [blame] | 1062 | #endif // BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC) && BUILDFLAG(USE_STARSCAN) |
[email protected] | 332af773 | 2009-03-11 13:21:35 | [diff] [blame] | 1063 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1064 | |
[email protected] | b172aee | 2012-04-10 17:05:26 | [diff] [blame] | 1065 | WebContentsImpl::~WebContentsImpl() { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 1066 | TRACE_EVENT0("content", "WebContentsImpl::~WebContentsImpl"); |
Lukasz Anforowicz | 7a0381c | 2022-12-07 20:23:08 | [diff] [blame] | 1067 | WebContentsOfBrowserContext::Detach(*this); |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 1068 | |
creis | ba91ba8 | 2017-05-12 22:56:19 | [diff] [blame] | 1069 | // Imperfect sanity check against double free, given some crashes unexpectedly |
| 1070 | // observed in the wild. |
Takashi Toyoshima | ea534ef2 | 2021-07-21 03:27:59 | [diff] [blame] | 1071 | CHECK(!IsBeingDestroyed()); |
creis | ba91ba8 | 2017-05-12 22:56:19 | [diff] [blame] | 1072 | |
| 1073 | // We generally keep track of is_being_destroyed_ to let other features know |
| 1074 | // to avoid certain actions during destruction. |
[email protected] | 420ae01 | 2009-04-24 05:16:32 | [diff] [blame] | 1075 | is_being_destroyed_ = true; |
| 1076 | |
creis | 97867aa | 2017-02-17 18:39:45 | [diff] [blame] | 1077 | // A WebContents should never be deleted while it is notifying observers, |
creis | ba91ba8 | 2017-05-12 22:56:19 | [diff] [blame] | 1078 | // since this will lead to a use-after-free as it continues to notify later |
creis | 97867aa | 2017-02-17 18:39:45 | [diff] [blame] | 1079 | // observers. |
Alexander Timin | 90c20c3 | 2020-08-12 15:28:32 | [diff] [blame] | 1080 | CHECK(!observers_.is_notifying_observers()); |
danakj | e6aa9bb | 2021-03-02 20:28:07 | [diff] [blame] | 1081 | // This is used to watch for one-off observers (i.e. non-WebContentsObservers) |
| 1082 | // destroying WebContents, which they should not do. |
| 1083 | CHECK(!prevent_destruction_); |
creis | 97867aa | 2017-02-17 18:39:45 | [diff] [blame] | 1084 | |
Takashi Toyoshima | 5e351c9 | 2021-04-27 06:29:02 | [diff] [blame] | 1085 | // We usually record `max_loaded_frame_count_` in `DidFinishNavigation()` |
| 1086 | // for pages the user navigated away from other than the last one. We record |
| 1087 | // it for the last page here. |
Khushal | 97d31e8 | 2021-06-23 21:07:54 | [diff] [blame] | 1088 | if (first_primary_navigation_completed_) |
Takashi Toyoshima | 5e351c9 | 2021-04-27 06:29:02 | [diff] [blame] | 1089 | RecordMaxFrameCountUMA(max_loaded_frame_count_); |
| 1090 | |
Avi Drissman | 3397294 | 2020-06-19 14:16:01 | [diff] [blame] | 1091 | FullscreenContentsSet(GetBrowserContext())->erase(this); |
| 1092 | |
kenrb | 2a565f8 | 2015-09-02 20:24:59 | [diff] [blame] | 1093 | rwh_input_event_router_.reset(); |
| 1094 | |
avallee | 0206f78 | 2016-07-28 18:55:33 | [diff] [blame] | 1095 | WebContentsImpl* outermost = GetOutermostWebContents(); |
avallee | 9993fca | 2016-11-17 06:16:50 | [diff] [blame] | 1096 | if (this != outermost && ContainsOrIsFocusedWebContents()) { |
avallee | 0206f78 | 2016-07-28 18:55:33 | [diff] [blame] | 1097 | // If the current WebContents is in focus, unset it. |
avallee | 9993fca | 2016-11-17 06:16:50 | [diff] [blame] | 1098 | outermost->SetAsFocusedWebContentsIfNecessary(); |
avallee | 0206f78 | 2016-07-28 18:55:33 | [diff] [blame] | 1099 | } |
| 1100 | |
Alex Moshchuk | 8380553 | 2022-08-12 15:15:23 | [diff] [blame] | 1101 | if (mouse_lock_widget_) { |
James Hollyer | d5c9de46 | 2020-03-10 19:02:45 | [diff] [blame] | 1102 | mouse_lock_widget_->RejectMouseLockOrUnlockIfNecessary( |
| 1103 | blink::mojom::PointerLockResult::kElementDestroyed); |
Nick Carter | d73635b | 2018-03-13 18:31:41 | [diff] [blame] | 1104 | |
Alex Moshchuk | 8380553 | 2022-08-12 15:15:23 | [diff] [blame] | 1105 | // Normally, the call above clears mouse_lock_widget_ pointers on the |
| 1106 | // entire WebContents chain, since it results in calling LostMouseLock() |
| 1107 | // when the mouse lock is already active. However, this doesn't work for |
| 1108 | // <webview> guests if the mouse lock request is still pending while the |
| 1109 | // <webview> is destroyed. Hence, ensure that all mouse lock widget |
| 1110 | // pointers are cleared. See https://crbug.com/1346245. |
| 1111 | for (WebContentsImpl* current = this; current; |
| 1112 | current = current->GetOuterWebContents()) { |
| 1113 | current->mouse_lock_widget_ = nullptr; |
| 1114 | } |
| 1115 | } |
| 1116 | |
dcheng | 57e39e2 | 2016-01-21 00:25:38 | [diff] [blame] | 1117 | for (RenderWidgetHostImpl* widget : created_widgets_) |
| 1118 | widget->DetachDelegate(); |
[email protected] | b24b68a | 2012-09-24 21:57:26 | [diff] [blame] | 1119 | created_widgets_.clear(); |
| 1120 | |
[email protected] | 3ab9cb8 | 2011-06-03 18:02:07 | [diff] [blame] | 1121 | // Clear out any JavaScript state. |
avi | 5d3b869 | 2016-10-12 22:00:46 | [diff] [blame] | 1122 | if (dialog_manager_) { |
avi | 6879fcf | 2017-01-21 05:27:53 | [diff] [blame] | 1123 | dialog_manager_->CancelDialogs(this, /*reset_state=*/true); |
avi | 5d3b869 | 2016-10-12 22:00:46 | [diff] [blame] | 1124 | } |
[email protected] | 3ab9cb8 | 2011-06-03 18:02:07 | [diff] [blame] | 1125 | |
Tom Burgin | 732656d3 | 2022-02-07 18:33:26 | [diff] [blame] | 1126 | #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_MAC) |
Bo Liu | 2bceebc | 2021-06-03 15:06:33 | [diff] [blame] | 1127 | color_chooser_holder_.reset(); |
Tom Burgin | 732656d3 | 2022-02-07 18:33:26 | [diff] [blame] | 1128 | #endif // BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_MAC) |
Rakina Zata Amni | 1619612 | 2018-11-14 02:14:00 | [diff] [blame] | 1129 | find_request_manager_.reset(); |
[email protected] | da854376 | 2012-03-20 08:52:20 | [diff] [blame] | 1130 | |
Takashi Toyoshima | 58a78001 | 2021-04-08 03:19:37 | [diff] [blame] | 1131 | // Shutdown the primary FrameTree. |
Carlos Caballero | b65b6e3a | 2021-11-15 10:09:00 | [diff] [blame] | 1132 | primary_frame_tree_.Shutdown(); |
[email protected] | f273ee5 | 2013-10-18 16:05:27 | [diff] [blame] | 1133 | |
Hiroki Nakagawa | 9bc09f2 | 2021-04-23 09:48:55 | [diff] [blame] | 1134 | // Shutdown the non-primary FrameTrees. |
| 1135 | // |
Takashi Toyoshima | 58a78001 | 2021-04-08 03:19:37 | [diff] [blame] | 1136 | // Do this here rather than relying on the owner of the FrameTree to shutdown |
| 1137 | // on WebContentsDestroyed(), so that all the FrameTrees are shutdown at the |
| 1138 | // same time for consistency. Also, destroying a FrameTree results in other |
| 1139 | // observer functions like RenderFrameDeleted() being called, which are not |
| 1140 | // expected to be called after WebContentsDestroyed(). |
Hiroki Nakagawa | 9bc09f2 | 2021-04-23 09:48:55 | [diff] [blame] | 1141 | // |
| 1142 | // Currently the only instances of the non-primary FrameTrees are for |
| 1143 | // prerendering. Shutdown them by destructing PrerenderHostRegistry. |
Hiroki Nakagawa | 5b82ee44 | 2021-09-20 17:17:20 | [diff] [blame] | 1144 | prerender_host_registry_.reset(); |
Takashi Toyoshima | 58a78001 | 2021-04-08 03:19:37 | [diff] [blame] | 1145 | |
K. Moon | 9ae6ef2c | 2022-08-18 01:30:06 | [diff] [blame] | 1146 | #if BUILDFLAG(ENABLE_PPAPI) |
zqzhang | 181047e6 | 2016-07-01 13:37:17 | [diff] [blame] | 1147 | // Call this before WebContentsDestroyed() is broadcasted since |
| 1148 | // AudioFocusManager will be destroyed after that. |
| 1149 | pepper_playback_observer_.reset(); |
| 1150 | #endif // defined(ENABLED_PLUGINS) |
| 1151 | |
Chris Hamilton | df0d72cd | 2018-05-29 16:23:53 | [diff] [blame] | 1152 | // If audio is playing then notify external observers of the audio stream |
| 1153 | // disappearing. |
| 1154 | if (is_currently_audible_) { |
| 1155 | is_currently_audible_ = false; |
Andrew Grieve | e19cd93e | 2021-01-22 05:43:06 | [diff] [blame] | 1156 | observers_.NotifyObservers(&WebContentsObserver::OnAudioStateChanged, |
| 1157 | false); |
Chris Hamilton | df0d72cd | 2018-05-29 16:23:53 | [diff] [blame] | 1158 | if (GetOuterWebContents()) |
| 1159 | GetOuterWebContents()->OnAudioStateChanged(); |
| 1160 | } |
| 1161 | |
Xiaohan Wang | 24ec934 | 2022-01-15 17:34:22 | [diff] [blame] | 1162 | #if BUILDFLAG(IS_ANDROID) |
Kevin McNee | 576bb5d8 | 2019-05-29 16:47:51 | [diff] [blame] | 1163 | // For simplicity, destroy the Java WebContents before we notify of the |
| 1164 | // destruction of the WebContents. |
| 1165 | ClearWebContentsAndroid(); |
| 1166 | #endif |
| 1167 | |
Dave Tapuska | 765a2f6 | 2021-07-07 20:52:23 | [diff] [blame] | 1168 | // |save_package_| is refcounted so make sure we clear the page before |
| 1169 | // we toss out our reference. |
| 1170 | if (save_package_) |
| 1171 | save_package_->ClearPage(); |
| 1172 | |
Andrew Grieve | e19cd93e | 2021-01-22 05:43:06 | [diff] [blame] | 1173 | observers_.NotifyObservers(&WebContentsObserver::WebContentsDestroyed); |
Andrew Grieve | e19cd93e | 2021-01-22 05:43:06 | [diff] [blame] | 1174 | observers_.NotifyObservers(&WebContentsObserver::ResetWebContents); |
Ivan Kotenkov | 2c0d2bb3 | 2017-11-01 15:41:28 | [diff] [blame] | 1175 | SetDelegate(nullptr); |
[email protected] | b5a1d11c | 2011-02-17 03:09:42 | [diff] [blame] | 1176 | } |
| 1177 | |
erikchen | 50735fd | 2018-05-05 15:08:33 | [diff] [blame] | 1178 | std::unique_ptr<WebContentsImpl> WebContentsImpl::CreateWithOpener( |
[email protected] | 54944cde | 2012-12-09 09:24:59 | [diff] [blame] | 1179 | const WebContents::CreateParams& params, |
Ian Clelland | 5cbaaf8 | 2017-11-27 22:00:03 | [diff] [blame] | 1180 | RenderFrameHostImpl* opener_rfh) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 1181 | OPTIONAL_TRACE_EVENT1("browser", "WebContentsImpl::CreateWithOpener", |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 1182 | "opener", opener_rfh); |
Ian Clelland | 5cbaaf8 | 2017-11-27 22:00:03 | [diff] [blame] | 1183 | FrameTreeNode* opener = nullptr; |
| 1184 | if (opener_rfh) |
| 1185 | opener = opener_rfh->frame_tree_node(); |
erikchen | 50735fd | 2018-05-05 15:08:33 | [diff] [blame] | 1186 | std::unique_ptr<WebContentsImpl> new_contents( |
| 1187 | new WebContentsImpl(params.browser_context)); |
lukasza | 6f8ac62 | 2017-06-06 03:10:20 | [diff] [blame] | 1188 | new_contents->SetOpenerForNewContents(opener, params.opener_suppressed); |
alexmos | e201c7cd | 2015-06-10 17:14:21 | [diff] [blame] | 1189 | |
alexmos | aedfc6f | 2016-01-21 23:57:38 | [diff] [blame] | 1190 | // If the opener is sandboxed, a new popup must inherit the opener's sandbox |
| 1191 | // flags, and these flags take effect immediately. An exception is if the |
| 1192 | // opener's sandbox flags lack the PropagatesToAuxiliaryBrowsingContexts |
| 1193 | // bit (which is controlled by the "allow-popups-to-escape-sandbox" token). |
Arthur Sonzogni | 73a3980 | 2022-01-31 14:50:22 | [diff] [blame] | 1194 | // See https://html.spec.whatwg.org/C/#attr-iframe-sandbox. |
Carlos Caballero | 6a99dac | 2021-11-03 10:41:17 | [diff] [blame] | 1195 | FrameTreeNode* new_root = new_contents->GetPrimaryFrameTree().root(); |
alexmos | aedfc6f | 2016-01-21 23:57:38 | [diff] [blame] | 1196 | if (opener) { |
arthursonzogni | b93a447 | 2020-04-10 07:38:00 | [diff] [blame] | 1197 | network::mojom::WebSandboxFlags opener_flags = |
Antonio Gomes | 7096ac0 | 2020-02-14 14:29:54 | [diff] [blame] | 1198 | opener_rfh->active_sandbox_flags(); |
Ian Clelland | 942f73a | 2020-12-07 18:20:26 | [diff] [blame] | 1199 | if (opener_rfh->IsSandboxed(network::mojom::WebSandboxFlags:: |
| 1200 | kPropagatesToAuxiliaryBrowsingContexts)) { |
Charlie Hu | e1b77ac | 2019-12-13 21:30:17 | [diff] [blame] | 1201 | new_root->SetPendingFramePolicy({opener_flags, |
| 1202 | {} /* container_policy */, |
| 1203 | {} /* required_document_policy */}); |
alexmos | aedfc6f | 2016-01-21 23:57:38 | [diff] [blame] | 1204 | } |
arthursonzogni | 034bb9c | 2020-10-01 08:29:56 | [diff] [blame] | 1205 | new_root->SetInitialPopupURL(params.initial_popup_url); |
| 1206 | new_root->SetPopupCreatorOrigin(opener_rfh->GetLastCommittedOrigin()); |
alexmos | aedfc6f | 2016-01-21 23:57:38 | [diff] [blame] | 1207 | } |
| 1208 | |
mark a. foltz | ef394fce | 2017-10-21 09:11:02 | [diff] [blame] | 1209 | // Apply starting sandbox flags. |
Luna Lu | c3fdacdf | 2017-11-08 04:48:53 | [diff] [blame] | 1210 | blink::FramePolicy frame_policy(new_root->pending_frame_policy()); |
mark a. foltz | ef394fce | 2017-10-21 09:11:02 | [diff] [blame] | 1211 | frame_policy.sandbox_flags |= params.starting_sandbox_flags; |
| 1212 | new_root->SetPendingFramePolicy(frame_policy); |
mark a. foltz | ef394fce | 2017-10-21 09:11:02 | [diff] [blame] | 1213 | |
alexmos | e201c7cd | 2015-06-10 17:14:21 | [diff] [blame] | 1214 | // This may be true even when opener is null, such as when opening blocked |
| 1215 | // popups. |
Rakina Zata Amni | c7ffea88 | 2021-08-16 10:04:28 | [diff] [blame] | 1216 | if (params.opened_by_another_window) |
| 1217 | new_contents->opened_by_another_window_ = true; |
alexmos | 090fae8e | 2015-05-28 17:09:28 | [diff] [blame] | 1218 | |
Sean Topping | bbc4a2bd | 2019-01-30 02:16:14 | [diff] [blame] | 1219 | WebContentsImpl* outer_web_contents = nullptr; |
[email protected] | 4858e43 | 2014-06-23 18:14:17 | [diff] [blame] | 1220 | if (params.guest_delegate) { |
[email protected] | 83100cd | 2014-05-10 11:50:06 | [diff] [blame] | 1221 | // This makes |new_contents| act as a guest. |
| 1222 | // For more info, see comment above class BrowserPluginGuest. |
Albert J. Wong | c55cc64d | 2018-10-12 02:50:04 | [diff] [blame] | 1223 | BrowserPluginGuest::CreateInWebContents(new_contents.get(), |
| 1224 | params.guest_delegate); |
Sean Topping | bbc4a2bd | 2019-01-30 02:16:14 | [diff] [blame] | 1225 | outer_web_contents = static_cast<WebContentsImpl*>( |
| 1226 | params.guest_delegate->GetOwnerWebContents()); |
[email protected] | 83100cd | 2014-05-10 11:50:06 | [diff] [blame] | 1227 | } |
mark a. foltz | ef394fce | 2017-10-21 09:11:02 | [diff] [blame] | 1228 | |
Harkiran Bolaria | 5ce2763 | 2022-01-20 15:05:05 | [diff] [blame] | 1229 | new_contents->Init(params, frame_policy); |
Sean Topping | bbc4a2bd | 2019-01-30 02:16:14 | [diff] [blame] | 1230 | if (outer_web_contents) |
| 1231 | outer_web_contents->InnerWebContentsCreated(new_contents.get()); |
[email protected] | d1198fd | 2012-08-13 22:50:19 | [diff] [blame] | 1232 | return new_contents; |
| 1233 | } |
| 1234 | |
[email protected] | 9564021 | 2014-07-26 18:14:30 | [diff] [blame] | 1235 | // static |
| 1236 | std::vector<WebContentsImpl*> WebContentsImpl::GetAllWebContents() { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 1237 | OPTIONAL_TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("content.verbose"), |
| 1238 | "WebContentsImpl::GetAllWebContents"); |
[email protected] | 9564021 | 2014-07-26 18:14:30 | [diff] [blame] | 1239 | std::vector<WebContentsImpl*> result; |
dcheng | 5971627 | 2016-04-09 05:19:08 | [diff] [blame] | 1240 | std::unique_ptr<RenderWidgetHostIterator> widgets( |
[email protected] | 9564021 | 2014-07-26 18:14:30 | [diff] [blame] | 1241 | RenderWidgetHostImpl::GetRenderWidgetHosts()); |
[email protected] | 9564021 | 2014-07-26 18:14:30 | [diff] [blame] | 1242 | while (RenderWidgetHost* rwh = widgets->GetNextHost()) { |
[email protected] | 9564021 | 2014-07-26 18:14:30 | [diff] [blame] | 1243 | RenderViewHost* rvh = RenderViewHost::From(rwh); |
| 1244 | if (!rvh) |
| 1245 | continue; |
| 1246 | WebContents* web_contents = WebContents::FromRenderViewHost(rvh); |
| 1247 | if (!web_contents) |
| 1248 | continue; |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 1249 | if (web_contents->GetPrimaryMainFrame()->GetRenderViewHost() != rvh) |
avi | df38c95 | 2015-10-27 13:45:13 | [diff] [blame] | 1250 | continue; |
| 1251 | // Because a WebContents can only have one current RVH at a time, there will |
| 1252 | // be no duplicate WebContents here. |
| 1253 | result.push_back(static_cast<WebContentsImpl*>(web_contents)); |
[email protected] | 9564021 | 2014-07-26 18:14:30 | [diff] [blame] | 1254 | } |
| 1255 | return result; |
| 1256 | } |
| 1257 | |
clamy | a16aa817 | 2015-05-26 13:07:25 | [diff] [blame] | 1258 | // static |
| 1259 | WebContentsImpl* WebContentsImpl::FromFrameTreeNode( |
paulmeyer | feafc2d | 2017-04-25 21:46:40 | [diff] [blame] | 1260 | const FrameTreeNode* frame_tree_node) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 1261 | OPTIONAL_TRACE_EVENT1(TRACE_DISABLED_BY_DEFAULT("content.verbose"), |
| 1262 | "WebContentsImpl::FromFrameTreeNode", "frame_tree_node", |
Alexander Timin | fb70a2d | 2021-02-08 03:19:07 | [diff] [blame] | 1263 | static_cast<const void*>(frame_tree_node)); |
clamy | a16aa817 | 2015-05-26 13:07:25 | [diff] [blame] | 1264 | return static_cast<WebContentsImpl*>( |
| 1265 | WebContents::FromRenderFrameHost(frame_tree_node->current_frame_host())); |
| 1266 | } |
| 1267 | |
clamy | 0d32d6d | 2015-11-24 11:16:26 | [diff] [blame] | 1268 | // static |
Patrick Monette | 275a6f92b | 2020-01-09 21:05:04 | [diff] [blame] | 1269 | WebContents* WebContentsImpl::FromRenderFrameHostID( |
Alexander Timin | 8690530c | 2021-06-19 00:34:32 | [diff] [blame] | 1270 | GlobalRenderFrameHostId render_frame_host_id) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 1271 | OPTIONAL_TRACE_EVENT2(TRACE_DISABLED_BY_DEFAULT("content.verbose"), |
| 1272 | "WebContentsImpl::FromRenderFrameHostID", "process_id", |
| 1273 | render_frame_host_id.child_id, "frame_id", |
| 1274 | render_frame_host_id.frame_routing_id); |
Patrick Monette | 275a6f92b | 2020-01-09 21:05:04 | [diff] [blame] | 1275 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) || |
| 1276 | !BrowserThread::IsThreadInitialized(BrowserThread::UI)); |
Patrick Monette | 7364e697 | 2020-01-09 22:56:02 | [diff] [blame] | 1277 | RenderFrameHost* render_frame_host = |
| 1278 | RenderFrameHost::FromID(render_frame_host_id); |
| 1279 | if (!render_frame_host) |
| 1280 | return nullptr; |
| 1281 | |
| 1282 | return WebContents::FromRenderFrameHost(render_frame_host); |
Patrick Monette | 275a6f92b | 2020-01-09 21:05:04 | [diff] [blame] | 1283 | } |
| 1284 | |
| 1285 | // static |
clamy | 0d32d6d | 2015-11-24 11:16:26 | [diff] [blame] | 1286 | WebContents* WebContentsImpl::FromRenderFrameHostID(int render_process_host_id, |
| 1287 | int render_frame_host_id) { |
Patrick Monette | 7364e697 | 2020-01-09 22:56:02 | [diff] [blame] | 1288 | return FromRenderFrameHostID( |
Alexander Timin | 8690530c | 2021-06-19 00:34:32 | [diff] [blame] | 1289 | GlobalRenderFrameHostId(render_process_host_id, render_frame_host_id)); |
clamy | 0d32d6d | 2015-11-24 11:16:26 | [diff] [blame] | 1290 | } |
| 1291 | |
paulmeyer | feafc2d | 2017-04-25 21:46:40 | [diff] [blame] | 1292 | // static |
| 1293 | WebContentsImpl* WebContentsImpl::FromOuterFrameTreeNode( |
| 1294 | const FrameTreeNode* frame_tree_node) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 1295 | OPTIONAL_TRACE_EVENT1(TRACE_DISABLED_BY_DEFAULT("content.verbose"), |
| 1296 | "WebContentsImpl::FromOuterFrameTreeNode", |
Alexander Timin | fb70a2d | 2021-02-08 03:19:07 | [diff] [blame] | 1297 | "frame_tree_node", |
| 1298 | static_cast<const void*>(frame_tree_node)); |
paulmeyer | feafc2d | 2017-04-25 21:46:40 | [diff] [blame] | 1299 | return WebContentsImpl::FromFrameTreeNode(frame_tree_node) |
| 1300 | ->node_.GetInnerWebContentsInFrame(frame_tree_node); |
| 1301 | } |
| 1302 | |
nick | a0ac838 | 2016-12-15 23:59:23 | [diff] [blame] | 1303 | bool WebContentsImpl::OnMessageReceived(RenderFrameHostImpl* render_frame_host, |
| 1304 | const IPC::Message& message) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 1305 | OPTIONAL_TRACE_EVENT1("content", "WebContentsImpl::OnMessageReceived", |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 1306 | "render_frame_host", render_frame_host); |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 1307 | |
Alexander Timin | 90c20c3 | 2020-08-12 15:28:32 | [diff] [blame] | 1308 | for (auto& observer : observers_.observer_list()) { |
nick | a0ac838 | 2016-12-15 23:59:23 | [diff] [blame] | 1309 | if (observer.OnMessageReceived(message, render_frame_host)) |
| 1310 | return true; |
| 1311 | } |
| 1312 | |
Dave Tapuska | ef8be161 | 2021-01-11 17:53:14 | [diff] [blame] | 1313 | return false; |
[email protected] | 724159a | 2010-12-30 01:11:18 | [diff] [blame] | 1314 | } |
| 1315 | |
Kuznetsov Alexey | 1a516bd | 2021-08-04 23:49:05 | [diff] [blame] | 1316 | std::string WebContentsImpl::GetTitleForMediaControls() { |
| 1317 | OPTIONAL_TRACE_EVENT0("content", "WebContentsImpl::GetTitleForMediaControls"); |
| 1318 | |
| 1319 | if (!delegate_) |
| 1320 | return std::string(); |
| 1321 | return delegate_->GetTitleForMediaControls(this); |
| 1322 | } |
| 1323 | |
Brad Triebwasser | 9d34d25 | 2022-07-26 20:44:42 | [diff] [blame] | 1324 | bool WebContentsImpl::IsFullscreenOnDisplay(int64_t display_id) const { |
| 1325 | if (!delegate_) |
| 1326 | return false; |
| 1327 | DCHECK_NE(display_id, display::kInvalidDisplayId); |
| 1328 | int64_t fullscreen_display = display::kInvalidDisplayId; |
| 1329 | if (!delegate_->IsFullscreenForTabOrPending(this, &fullscreen_display)) |
| 1330 | return false; |
| 1331 | return fullscreen_display == display_id; |
| 1332 | } |
| 1333 | |
Carlos Caballero | 40b0efd | 2021-01-26 11:55:00 | [diff] [blame] | 1334 | // Returns the NavigationController for the primary FrameTree, i.e. the one |
| 1335 | // whose URL is shown in the omnibox. With MPArch we can have multiple |
| 1336 | // FrameTrees in one WebContents and each has its own NavigationController. |
| 1337 | // TODO(https://crbug.com/1170273): Make sure callers are aware of this. |
[email protected] | d1198fd | 2012-08-13 22:50:19 | [diff] [blame] | 1338 | NavigationControllerImpl& WebContentsImpl::GetController() { |
Carlos Caballero | b65b6e3a | 2021-11-15 10:09:00 | [diff] [blame] | 1339 | return primary_frame_tree_.controller(); |
[email protected] | f5fa20e | 2011-12-21 22:35:56 | [diff] [blame] | 1340 | } |
| 1341 | |
Lucas Furukawa Gadani | e1c5dfda | 2018-11-29 17:57:41 | [diff] [blame] | 1342 | BrowserContext* WebContentsImpl::GetBrowserContext() { |
Carlos Caballero | 40b0efd | 2021-01-26 11:55:00 | [diff] [blame] | 1343 | return GetController().GetBrowserContext(); |
[email protected] | 627e051 | 2011-12-21 22:55:30 | [diff] [blame] | 1344 | } |
| 1345 | |
Claudio DeSouza | 2be02d4 | 2021-08-12 23:23:40 | [diff] [blame] | 1346 | base::WeakPtr<WebContents> WebContentsImpl::GetWeakPtr() { |
| 1347 | return weak_factory_.GetWeakPtr(); |
| 1348 | } |
| 1349 | |
Lucas Furukawa Gadani | e1c5dfda | 2018-11-29 17:57:41 | [diff] [blame] | 1350 | const GURL& WebContentsImpl::GetURL() { |
Peter Boström | 101d767 | 2018-12-15 00:43:59 | [diff] [blame] | 1351 | return GetVisibleURL(); |
[email protected] | be1f56ab | 2011-12-22 06:55:31 | [diff] [blame] | 1352 | } |
| 1353 | |
Lucas Furukawa Gadani | e1c5dfda | 2018-11-29 17:57:41 | [diff] [blame] | 1354 | const GURL& WebContentsImpl::GetVisibleURL() { |
[email protected] | c854a7e | 2013-05-21 16:42:24 | [diff] [blame] | 1355 | // We may not have a navigation entry yet. |
Carlos Caballero | 40b0efd | 2021-01-26 11:55:00 | [diff] [blame] | 1356 | NavigationEntry* entry = GetController().GetVisibleEntry(); |
[email protected] | c854a7e | 2013-05-21 16:42:24 | [diff] [blame] | 1357 | return entry ? entry->GetVirtualURL() : GURL::EmptyGURL(); |
| 1358 | } |
| 1359 | |
Lucas Furukawa Gadani | e1c5dfda | 2018-11-29 17:57:41 | [diff] [blame] | 1360 | const GURL& WebContentsImpl::GetLastCommittedURL() { |
[email protected] | c854a7e | 2013-05-21 16:42:24 | [diff] [blame] | 1361 | // We may not have a navigation entry yet. |
Carlos Caballero | 40b0efd | 2021-01-26 11:55:00 | [diff] [blame] | 1362 | NavigationEntry* entry = GetController().GetLastCommittedEntry(); |
[email protected] | c854a7e | 2013-05-21 16:42:24 | [diff] [blame] | 1363 | return entry ? entry->GetVirtualURL() : GURL::EmptyGURL(); |
| 1364 | } |
| 1365 | |
[email protected] | 8ff00d7 | 2012-10-23 19:12:21 | [diff] [blame] | 1366 | WebContentsDelegate* WebContentsImpl::GetDelegate() { |
[email protected] | be1f56ab | 2011-12-22 06:55:31 | [diff] [blame] | 1367 | return delegate_; |
| 1368 | } |
| 1369 | |
[email protected] | 8ff00d7 | 2012-10-23 19:12:21 | [diff] [blame] | 1370 | void WebContentsImpl::SetDelegate(WebContentsDelegate* delegate) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 1371 | OPTIONAL_TRACE_EVENT1("content", "WebContentsImpl::SetDelegate", "delegate", |
Alexander Timin | fb70a2d | 2021-02-08 03:19:07 | [diff] [blame] | 1372 | static_cast<void*>(delegate)); |
[email protected] | be1f56ab | 2011-12-22 06:55:31 | [diff] [blame] | 1373 | // TODO(cbentzel): remove this debugging code? |
| 1374 | if (delegate == delegate_) |
| 1375 | return; |
| 1376 | if (delegate_) |
| 1377 | delegate_->Detach(this); |
| 1378 | delegate_ = delegate; |
[email protected] | a6b73c6 | 2013-02-11 23:05:08 | [diff] [blame] | 1379 | if (delegate_) { |
[email protected] | be1f56ab | 2011-12-22 06:55:31 | [diff] [blame] | 1380 | delegate_->Attach(this); |
Wolfgang Beyer | a21c8a0d | 2021-05-26 07:22:05 | [diff] [blame] | 1381 | // RenderFrameDevToolsAgentHost should not be told about the WebContents |
| 1382 | // until there is a `delegate_`. |
| 1383 | RenderFrameDevToolsAgentHost::AttachToWebContents(this); |
[email protected] | a6b73c6 | 2013-02-11 23:05:08 | [diff] [blame] | 1384 | } |
danakj | 5f22bcc | 2021-02-12 18:09:01 | [diff] [blame] | 1385 | |
| 1386 | // Re-read values from the new delegate and apply them. |
| 1387 | if (view_) |
| 1388 | view_->SetOverscrollControllerEnabled(CanOverscrollContent()); |
[email protected] | be1f56ab | 2011-12-22 06:55:31 | [diff] [blame] | 1389 | } |
| 1390 | |
Dave Tapuska | 1d5e577 | 2022-06-02 17:26:01 | [diff] [blame] | 1391 | RenderFrameHostImpl* WebContentsImpl::GetPrimaryMainFrame() { |
Carlos Caballero | b65b6e3a | 2021-11-15 10:09:00 | [diff] [blame] | 1392 | return primary_frame_tree_.root()->current_frame_host(); |
[email protected] | 60eca4eb | 2013-12-06 00:02:16 | [diff] [blame] | 1393 | } |
| 1394 | |
Sreeja Kamishetty | 3dde631 | 2021-06-22 14:05:29 | [diff] [blame] | 1395 | PageImpl& WebContentsImpl::GetPrimaryPage() { |
| 1396 | // We should not be accessing Page during the destruction of this WebContents, |
| 1397 | // as the Page has already been cleared. |
| 1398 | // |
| 1399 | // Please note that IsBeingDestroyed() should be checked to ensure that we |
| 1400 | // don't access Page related data that is going to be destroyed. |
Carlos Caballero | b65b6e3a | 2021-11-15 10:09:00 | [diff] [blame] | 1401 | CHECK(primary_frame_tree_.root()->current_frame_host()); |
| 1402 | return primary_frame_tree_.root()->current_frame_host()->GetPage(); |
Sreeja Kamishetty | 3dde631 | 2021-06-22 14:05:29 | [diff] [blame] | 1403 | } |
| 1404 | |
nick | 53d5cbf | 2015-04-23 22:50:14 | [diff] [blame] | 1405 | RenderFrameHostImpl* WebContentsImpl::GetFocusedFrame() { |
Kevin McNee | 61927b3 | 2022-07-05 16:35:38 | [diff] [blame] | 1406 | // If this method is called on an inner WebContents, don't return frames from |
| 1407 | // outside of the inner WebContents's subtree. |
| 1408 | if (GetOuterWebContents() && !ContainsOrIsFocusedWebContents()) { |
nick | 53d5cbf | 2015-04-23 22:50:14 | [diff] [blame] | 1409 | return nullptr; |
Kevin McNee | 61927b3 | 2022-07-05 16:35:38 | [diff] [blame] | 1410 | } |
| 1411 | |
| 1412 | FrameTreeNode* focused_node = GetFocusedFrameTree()->GetFocusedFrame(); |
| 1413 | if (!focused_node) { |
| 1414 | return nullptr; |
| 1415 | } |
| 1416 | |
| 1417 | // If an inner frame tree has focus, we should return a RenderFrameHost from |
| 1418 | // the inner frame tree and not the placeholder RenderFrameHost. |
| 1419 | DCHECK_EQ( |
| 1420 | focused_node->current_frame_host()->inner_tree_main_frame_tree_node_id(), |
| 1421 | FrameTreeNode::kFrameTreeNodeInvalidId); |
| 1422 | |
nick | 53d5cbf | 2015-04-23 22:50:14 | [diff] [blame] | 1423 | return focused_node->current_frame_host(); |
[email protected] | 9c9343b | 2014-03-08 02:56:07 | [diff] [blame] | 1424 | } |
| 1425 | |
Kevin McNee | 07c67ec9 | 2021-08-11 16:18:33 | [diff] [blame] | 1426 | bool WebContentsImpl::IsPrerenderedFrame(int frame_tree_node_id) { |
Kevin McNee | 07c67ec9 | 2021-08-11 16:18:33 | [diff] [blame] | 1427 | if (frame_tree_node_id == RenderFrameHost::kNoFrameTreeNodeId) |
| 1428 | return false; |
| 1429 | |
| 1430 | FrameTreeNode* frame_tree_node = |
| 1431 | FrameTreeNode::GloballyFindByID(frame_tree_node_id); |
| 1432 | if (!frame_tree_node) |
| 1433 | return false; |
| 1434 | |
Kevin McNee | ab98c4a5 | 2022-01-28 16:53:06 | [diff] [blame] | 1435 | // In the case of inner frame trees in a prerender, the inner frame tree's |
| 1436 | // type, would not be FrameTree::Type::kPrerender. So if this is not the |
| 1437 | // outermost frame, we use the lifecycle state of the document that owns this. |
| 1438 | // TODO(1196715, 1232528): This relies on the LifecycleState being correct |
| 1439 | // in the case of inner frame trees. |
| 1440 | if (frame_tree_node->GetParentOrOuterDocumentOrEmbedder()) { |
| 1441 | return frame_tree_node->GetParentOrOuterDocumentOrEmbedder() |
| 1442 | ->lifecycle_state() == |
| 1443 | RenderFrameHostImpl::LifecycleStateImpl::kPrerendering; |
| 1444 | } |
Abhijeet Kandalkar | 497904e2 | 2023-01-12 07:59:30 | [diff] [blame] | 1445 | return frame_tree_node->GetFrameType() == FrameType::kPrerenderMainFrame; |
Kevin McNee | 07c67ec9 | 2021-08-11 16:18:33 | [diff] [blame] | 1446 | } |
| 1447 | |
creis | f71a263 | 2017-05-04 19:03:50 | [diff] [blame] | 1448 | RenderFrameHostImpl* WebContentsImpl::UnsafeFindFrameByFrameTreeNodeId( |
rob | 3e2a073 | 2016-01-06 21:22:09 | [diff] [blame] | 1449 | int frame_tree_node_id) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 1450 | OPTIONAL_TRACE_EVENT1("content", |
| 1451 | "WebContentsImpl::UnsafeFindFrameByFrameTreeNodeId", |
| 1452 | "frame_tree_node_id", frame_tree_node_id); |
creis | f71a263 | 2017-05-04 19:03:50 | [diff] [blame] | 1453 | // Beware using this! The RenderFrameHost may have changed since the caller |
| 1454 | // obtained frame_tree_node_id. |
Kunihiko Sakamoto | 1c5a09e | 2021-08-10 02:40:39 | [diff] [blame] | 1455 | FrameTreeNode* ftn = FrameTreeNode::GloballyFindByID(frame_tree_node_id); |
| 1456 | if (!ftn) |
| 1457 | return nullptr; |
| 1458 | if (this != WebContents::FromRenderFrameHost(ftn->current_frame_host())) |
| 1459 | return nullptr; |
| 1460 | return ftn->current_frame_host(); |
rob | 3e2a073 | 2016-01-06 21:22:09 | [diff] [blame] | 1461 | } |
| 1462 | |
Daniel Cheng | 982f2b2 | 2022-08-25 23:46:16 | [diff] [blame] | 1463 | void WebContentsImpl::ForEachRenderFrameHostWithAction( |
| 1464 | base::FunctionRef<FrameIterationAction(RenderFrameHost*)> on_frame) { |
| 1465 | ForEachRenderFrameHostWithAction( |
| 1466 | [on_frame](RenderFrameHostImpl* rfh) { return on_frame(rfh); }); |
Kevin McNee | c9d0fda | 2021-05-19 15:55:17 | [diff] [blame] | 1467 | } |
| 1468 | |
| 1469 | void WebContentsImpl::ForEachRenderFrameHost( |
Daniel Cheng | 982f2b2 | 2022-08-25 23:46:16 | [diff] [blame] | 1470 | base::FunctionRef<void(RenderFrameHost*)> on_frame) { |
| 1471 | ForEachRenderFrameHost( |
| 1472 | [on_frame](RenderFrameHostImpl* rfh) { on_frame(rfh); }); |
Kevin McNee | c9d0fda | 2021-05-19 15:55:17 | [diff] [blame] | 1473 | } |
| 1474 | |
Daniel Cheng | 982f2b2 | 2022-08-25 23:46:16 | [diff] [blame] | 1475 | void WebContentsImpl::ForEachRenderFrameHostWithAction( |
| 1476 | base::FunctionRef<FrameIterationAction(RenderFrameHostImpl*)> on_frame) { |
Kevin McNee | c9d0fda | 2021-05-19 15:55:17 | [diff] [blame] | 1477 | ForEachRenderFrameHostImpl(on_frame, /* include_speculative */ false); |
| 1478 | } |
| 1479 | |
| 1480 | void WebContentsImpl::ForEachRenderFrameHost( |
Daniel Cheng | 982f2b2 | 2022-08-25 23:46:16 | [diff] [blame] | 1481 | base::FunctionRef<void(RenderFrameHostImpl*)> on_frame) { |
| 1482 | ForEachRenderFrameHostWithAction([on_frame](RenderFrameHostImpl* rfh) { |
| 1483 | on_frame(rfh); |
| 1484 | return FrameIterationAction::kContinue; |
| 1485 | }); |
Kevin McNee | c9d0fda | 2021-05-19 15:55:17 | [diff] [blame] | 1486 | } |
| 1487 | |
Daniel Cheng | 982f2b2 | 2022-08-25 23:46:16 | [diff] [blame] | 1488 | void WebContentsImpl::ForEachRenderFrameHostIncludingSpeculativeWithAction( |
| 1489 | base::FunctionRef<FrameIterationAction(RenderFrameHostImpl*)> on_frame) { |
Kevin McNee | c9d0fda | 2021-05-19 15:55:17 | [diff] [blame] | 1490 | ForEachRenderFrameHostImpl(on_frame, /* include_speculative */ true); |
| 1491 | } |
| 1492 | |
| 1493 | void WebContentsImpl::ForEachRenderFrameHostIncludingSpeculative( |
Daniel Cheng | 982f2b2 | 2022-08-25 23:46:16 | [diff] [blame] | 1494 | base::FunctionRef<void(RenderFrameHostImpl*)> on_frame) { |
| 1495 | ForEachRenderFrameHostIncludingSpeculativeWithAction( |
| 1496 | [on_frame](RenderFrameHostImpl* rfh) { |
| 1497 | on_frame(rfh); |
| 1498 | return FrameIterationAction::kContinue; |
| 1499 | }); |
Kevin McNee | c9d0fda | 2021-05-19 15:55:17 | [diff] [blame] | 1500 | } |
| 1501 | |
| 1502 | void WebContentsImpl::ForEachRenderFrameHostImpl( |
Daniel Cheng | 982f2b2 | 2022-08-25 23:46:16 | [diff] [blame] | 1503 | base::FunctionRef<FrameIterationAction(RenderFrameHostImpl*)> on_frame, |
Kevin McNee | c9d0fda | 2021-05-19 15:55:17 | [diff] [blame] | 1504 | bool include_speculative) { |
| 1505 | // Since |RenderFrameHostImpl::ForEachRenderFrameHost| will reach the |
| 1506 | // RenderFrameHosts descending from a specified root, it is enough to start |
| 1507 | // iteration from each of the outermost main frames to reach everything in |
| 1508 | // this WebContents. However, if iteration stops early in |
| 1509 | // |RenderFrameHostImpl::ForEachRenderFrameHost|, we also need to stop early |
| 1510 | // by not iterating over additional outermost main frames. |
| 1511 | bool iteration_stopped = false; |
Daniel Cheng | 982f2b2 | 2022-08-25 23:46:16 | [diff] [blame] | 1512 | auto on_frame_with_termination = |
| 1513 | [on_frame, &iteration_stopped](RenderFrameHostImpl* rfh) { |
| 1514 | const auto action = on_frame(rfh); |
| 1515 | if (action == FrameIterationAction::kStop) { |
| 1516 | iteration_stopped = true; |
| 1517 | } |
| 1518 | return action; |
| 1519 | }; |
Kevin McNee | c9d0fda | 2021-05-19 15:55:17 | [diff] [blame] | 1520 | |
| 1521 | for (auto* rfh : GetOutermostMainFrames()) { |
| 1522 | if (include_speculative) { |
Daniel Cheng | 982f2b2 | 2022-08-25 23:46:16 | [diff] [blame] | 1523 | rfh->ForEachRenderFrameHostIncludingSpeculativeWithAction( |
Kevin McNee | c9d0fda | 2021-05-19 15:55:17 | [diff] [blame] | 1524 | on_frame_with_termination); |
| 1525 | } else { |
Daniel Cheng | 982f2b2 | 2022-08-25 23:46:16 | [diff] [blame] | 1526 | rfh->ForEachRenderFrameHostWithAction(on_frame_with_termination); |
Kevin McNee | c9d0fda | 2021-05-19 15:55:17 | [diff] [blame] | 1527 | } |
| 1528 | |
| 1529 | if (iteration_stopped) { |
| 1530 | return; |
| 1531 | } |
| 1532 | } |
| 1533 | } |
| 1534 | |
Takashi Toyoshima | dc3f747 | 2021-07-21 08:02:32 | [diff] [blame] | 1535 | void WebContentsImpl::ForEachFrameTree( |
| 1536 | FrameTreeIterationCallback on_frame_tree) { |
Kevin McNee | 53f0b2d | 2021-11-02 18:00:45 | [diff] [blame] | 1537 | // Consider all outermost frame trees, and for each, iterate through their |
| 1538 | // FrameTreeNodes to find any inner frame trees. |
| 1539 | for (FrameTree* outermost_frame_tree : GetOutermostFrameTrees()) { |
| 1540 | FrameTree::NodeRange node_range = |
| 1541 | outermost_frame_tree->NodesIncludingInnerTreeNodes(); |
| 1542 | FrameTree::NodeIterator node_iter = node_range.begin(); |
| 1543 | while (node_iter != node_range.end()) { |
| 1544 | FrameTreeNode* node = *node_iter; |
| 1545 | if (FromFrameTreeNode(node) != this) { |
| 1546 | // Exclude any inner frame trees based on multi-WebContents |
| 1547 | // architecture. |
| 1548 | node_iter.AdvanceSkippingChildren(); |
| 1549 | } else { |
| 1550 | if (node->IsMainFrame()) { |
| 1551 | on_frame_tree.Run(node->frame_tree()); |
| 1552 | } |
| 1553 | ++node_iter; |
| 1554 | } |
| 1555 | } |
| 1556 | } |
| 1557 | } |
Takashi Toyoshima | dc3f747 | 2021-07-21 08:02:32 | [diff] [blame] | 1558 | |
Kevin McNee | 53f0b2d | 2021-11-02 18:00:45 | [diff] [blame] | 1559 | std::vector<FrameTree*> WebContentsImpl::GetOutermostFrameTrees() { |
Sarah Murphy | 3bccae6 | 2022-02-15 20:22:45 | [diff] [blame] | 1560 | // If the WebContentsImpl is being destroyed, then we should not |
| 1561 | // perform the tree traversal. |
| 1562 | if (IsBeingDestroyed()) |
| 1563 | return {}; |
| 1564 | |
Kevin McNee | 53f0b2d | 2021-11-02 18:00:45 | [diff] [blame] | 1565 | std::vector<FrameTree*> result; |
Carlos Caballero | 1a261f8 | 2021-11-04 15:54:03 | [diff] [blame] | 1566 | result.push_back(&GetPrimaryFrameTree()); |
Kevin McNee | 53f0b2d | 2021-11-02 18:00:45 | [diff] [blame] | 1567 | |
Johann | 344e1c7 | 2022-11-22 07:22:30 | [diff] [blame] | 1568 | const std::vector<FrameTree*> prerender_frame_trees = |
| 1569 | GetPrerenderHostRegistry()->GetPrerenderFrameTrees(); |
| 1570 | result.insert(result.end(), prerender_frame_trees.begin(), |
| 1571 | prerender_frame_trees.end()); |
Fergal Daly | 80da1249 | 2022-02-10 06:10:51 | [diff] [blame] | 1572 | |
Kevin McNee | 53f0b2d | 2021-11-02 18:00:45 | [diff] [blame] | 1573 | return result; |
Takashi Toyoshima | dc3f747 | 2021-07-21 08:02:32 | [diff] [blame] | 1574 | } |
| 1575 | |
Kevin McNee | c9d0fda | 2021-05-19 15:55:17 | [diff] [blame] | 1576 | std::vector<RenderFrameHostImpl*> WebContentsImpl::GetOutermostMainFrames() { |
Kevin McNee | d7fc6d05 | 2022-02-08 21:21:50 | [diff] [blame] | 1577 | // Do nothing if the WebContents is currently being initialized or destroyed. |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 1578 | if (!GetPrimaryMainFrame()) |
Kevin McNee | d7fc6d05 | 2022-02-08 21:21:50 | [diff] [blame] | 1579 | return {}; |
| 1580 | |
Kevin McNee | c9d0fda | 2021-05-19 15:55:17 | [diff] [blame] | 1581 | std::vector<RenderFrameHostImpl*> result; |
Kevin McNee | 53f0b2d | 2021-11-02 18:00:45 | [diff] [blame] | 1582 | |
| 1583 | for (FrameTree* outermost_frame_tree : GetOutermostFrameTrees()) { |
Kevin McNee | d7fc6d05 | 2022-02-08 21:21:50 | [diff] [blame] | 1584 | DCHECK(outermost_frame_tree->GetMainFrame()); |
Kevin McNee | 53f0b2d | 2021-11-02 18:00:45 | [diff] [blame] | 1585 | result.push_back(outermost_frame_tree->GetMainFrame()); |
| 1586 | } |
Kevin McNee | c9d0fda | 2021-05-19 15:55:17 | [diff] [blame] | 1587 | |
| 1588 | for (const auto& entry : GetController().GetBackForwardCache().GetEntries()) { |
Yuzu Saijo | 68390b99 | 2021-07-27 06:17:20 | [diff] [blame] | 1589 | result.push_back(entry->render_frame_host()); |
Kevin McNee | c9d0fda | 2021-05-19 15:55:17 | [diff] [blame] | 1590 | } |
| 1591 | |
Kevin McNee | c9d0fda | 2021-05-19 15:55:17 | [diff] [blame] | 1592 | // In the case of inner WebContents, we still allow this method to be called, |
| 1593 | // but the semantics of the values being returned are "outermost |
| 1594 | // within this WebContents" as opposed to truly outermost. We would not expect |
| 1595 | // any other outermost pages besides the primary page in the case of inner |
| 1596 | // WebContents. |
| 1597 | DCHECK(!GetOuterWebContents() || (result.size() == 1)); |
| 1598 | |
| 1599 | return result; |
| 1600 | } |
| 1601 | |
Gyuyoung Kim | 877e86d | 2020-07-21 04:01:02 | [diff] [blame] | 1602 | void WebContentsImpl::ExecutePageBroadcastMethod( |
| 1603 | PageBroadcastMethodCallback callback) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 1604 | OPTIONAL_TRACE_EVENT0("content", |
| 1605 | "WebContentsImpl::ExecutePageBroadcastMethod"); |
Carlos Caballero | b65b6e3a | 2021-11-15 10:09:00 | [diff] [blame] | 1606 | primary_frame_tree_.root()->render_manager()->ExecutePageBroadcastMethod( |
| 1607 | callback); |
Gyuyoung Kim | 877e86d | 2020-07-21 04:01:02 | [diff] [blame] | 1608 | } |
| 1609 | |
Takashi Toyoshima | df3f4e31 | 2021-11-19 03:51:48 | [diff] [blame] | 1610 | void WebContentsImpl::ExecutePageBroadcastMethodForAllPages( |
| 1611 | PageBroadcastMethodCallback callback) { |
| 1612 | OPTIONAL_TRACE_EVENT0( |
| 1613 | "content", "WebContentsImpl::ExecutePageBroadcastMethodForAllPages"); |
| 1614 | ForEachFrameTree(base::BindRepeating( |
Arthur Sonzogni | f6785ec | 2022-12-05 10:11:50 | [diff] [blame] | 1615 | [](PageBroadcastMethodCallback* callback, FrameTree& frame_tree) { |
| 1616 | frame_tree.root()->render_manager()->ExecutePageBroadcastMethod( |
Takashi Toyoshima | df3f4e31 | 2021-11-19 03:51:48 | [diff] [blame] | 1617 | *callback); |
| 1618 | }, |
| 1619 | &callback)); |
| 1620 | } |
| 1621 | |
Lucas Furukawa Gadani | e1c5dfda | 2018-11-29 17:57:41 | [diff] [blame] | 1622 | RenderViewHostImpl* WebContentsImpl::GetRenderViewHost() { |
Alex Moshchuk | 2e470ea | 2021-02-03 06:46:34 | [diff] [blame] | 1623 | return GetRenderManager()->current_frame_host()->render_view_host(); |
[email protected] | be1f56ab | 2011-12-22 06:55:31 | [diff] [blame] | 1624 | } |
| 1625 | |
creis | 89a0f78 | 2015-05-27 16:13:17 | [diff] [blame] | 1626 | void WebContentsImpl::CancelActiveAndPendingDialogs() { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 1627 | OPTIONAL_TRACE_EVENT0("content", |
| 1628 | "WebContentsImpl::CancelActiveAndPendingDialogs"); |
Evan Stade | 753fc20 | 2020-07-13 18:09:54 | [diff] [blame] | 1629 | if (dialog_manager_) { |
avi | 6879fcf | 2017-01-21 05:27:53 | [diff] [blame] | 1630 | dialog_manager_->CancelDialogs(this, /*reset_state=*/false); |
Evan Stade | 753fc20 | 2020-07-13 18:09:54 | [diff] [blame] | 1631 | } |
creis | 89a0f78 | 2015-05-27 16:13:17 | [diff] [blame] | 1632 | if (browser_plugin_embedder_) |
| 1633 | browser_plugin_embedder_->CancelGuestDialogs(); |
| 1634 | } |
| 1635 | |
nasko | c0fceff | 2015-04-30 15:53:52 | [diff] [blame] | 1636 | void WebContentsImpl::ClosePage() { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 1637 | OPTIONAL_TRACE_EVENT0("content", "WebContentsImpl::ClosePage"); |
Alex Moshchuk | e558aba | 2023-01-13 07:42:02 | [diff] [blame] | 1638 | GetPrimaryMainFrame()->ClosePage(); |
nasko | c0fceff | 2015-04-30 15:53:52 | [diff] [blame] | 1639 | } |
| 1640 | |
Lucas Furukawa Gadani | e1c5dfda | 2018-11-29 17:57:41 | [diff] [blame] | 1641 | RenderWidgetHostView* WebContentsImpl::GetRenderWidgetHostView() { |
[email protected] | fa944cb8 | 2013-11-15 17:51:21 | [diff] [blame] | 1642 | return GetRenderManager()->GetRenderWidgetHostView(); |
[email protected] | be1f56ab | 2011-12-22 06:55:31 | [diff] [blame] | 1643 | } |
| 1644 | |
lfg | 265a267 | 2016-04-23 03:11:02 | [diff] [blame] | 1645 | RenderWidgetHostView* WebContentsImpl::GetTopLevelRenderWidgetHostView() { |
| 1646 | if (GetOuterWebContents()) |
| 1647 | return GetOuterWebContents()->GetTopLevelRenderWidgetHostView(); |
| 1648 | return GetRenderManager()->GetRenderWidgetHostView(); |
| 1649 | } |
| 1650 | |
[email protected] | 8ff00d7 | 2012-10-23 19:12:21 | [diff] [blame] | 1651 | WebContentsView* WebContentsImpl::GetView() const { |
[email protected] | be1f56ab | 2011-12-22 06:55:31 | [diff] [blame] | 1652 | return view_.get(); |
| 1653 | } |
| 1654 | |
Mike Wasserman | 5a3cc61d | 2020-08-27 21:12:03 | [diff] [blame] | 1655 | void WebContentsImpl::OnScreensChange(bool is_multi_screen_changed) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 1656 | OPTIONAL_TRACE_EVENT1("content", "WebContentsImpl::OnScreensChange", |
| 1657 | "is_multi_screen_changed", is_multi_screen_changed); |
Mike Wasserman | db8f4f0 | 2020-10-06 01:18:37 | [diff] [blame] | 1658 | // Allow fullscreen requests shortly after user-generated screens changes. |
Mike Wasserman | 11554ff9 | 2021-05-16 00:46:26 | [diff] [blame] | 1659 | // TODO(crbug.com/1169291): Mac should not activate this on local process |
| 1660 | // display::Screen signals, but via RenderWidgetHostViewMac screen updates. |
Mike Wasserman | db8f4f0 | 2020-10-06 01:18:37 | [diff] [blame] | 1661 | transient_allow_fullscreen_.Activate(); |
Mike Wasserman | 11554ff9 | 2021-05-16 00:46:26 | [diff] [blame] | 1662 | |
| 1663 | // Mac display info may originate from a remote process hosting the NSWindow; |
| 1664 | // this local process display::Screen signal should not trigger updates. |
| 1665 | // TODO(crbug.com/1169291): Unify screen info plumbing, caching, etc. |
Xiaohan Wang | 24ec934 | 2022-01-15 17:34:22 | [diff] [blame] | 1666 | #if !BUILDFLAG(IS_MAC) |
Mike Wasserman | 9545ebc | 2021-01-28 03:32:14 | [diff] [blame] | 1667 | // This updates Screen attributes and fires Screen.change events as needed, |
| 1668 | // propagating to all widgets through the VisualProperties update waterfall. |
| 1669 | // This is triggered by system changes, not renderer IPC, so explicitly check |
| 1670 | // that the RenderWidgetHostView is valid before sending an update. |
| 1671 | if (RenderWidgetHostViewBase* view = |
| 1672 | GetRenderViewHost()->GetWidget()->GetView()) { |
Adrienne Walker | 39dcdcf | 2021-10-06 19:30:35 | [diff] [blame] | 1673 | // Only update top-level views, as child frames will have their ScreenInfos |
| 1674 | // updated by the visual property flow. |
| 1675 | if (!view->IsRenderWidgetHostViewChildFrame()) |
| 1676 | view->UpdateScreenInfo(); |
Mike Wasserman | 9545ebc | 2021-01-28 03:32:14 | [diff] [blame] | 1677 | } |
Xiaohan Wang | 24ec934 | 2022-01-15 17:34:22 | [diff] [blame] | 1678 | #endif // !BUILDFLAG(IS_MAC) |
Mike Wasserman | b213b935 | 2020-04-16 00:32:34 | [diff] [blame] | 1679 | } |
| 1680 | |
leon.han | 552e9de | 2017-02-09 14:37:30 | [diff] [blame] | 1681 | void WebContentsImpl::OnScreenOrientationChange() { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 1682 | OPTIONAL_TRACE_EVENT0("content", |
| 1683 | "WebContentsImpl::OnScreenOrientationChange"); |
leon.han | 552e9de | 2017-02-09 14:37:30 | [diff] [blame] | 1684 | DCHECK(screen_orientation_provider_); |
Lan Wei | f81c6e7c | 2020-02-12 16:46:09 | [diff] [blame] | 1685 | DidChangeScreenOrientation(); |
Becca Hughes | e9e2795 | 2018-06-25 17:08:27 | [diff] [blame] | 1686 | screen_orientation_provider_->OnOrientationChange(); |
leon.han | e4db177a | 2017-02-07 14:19:16 | [diff] [blame] | 1687 | } |
| 1688 | |
Anton Bikineev | f62d1bf | 2021-05-15 17:56:07 | [diff] [blame] | 1689 | absl::optional<SkColor> WebContentsImpl::GetThemeColor() { |
Jeremy Roman | 2d8dfe13 | 2021-07-06 20:51:26 | [diff] [blame] | 1690 | return GetPrimaryPage().theme_color(); |
yusufo | d41c5f9 | 2015-03-06 00:14:28 | [diff] [blame] | 1691 | } |
| 1692 | |
Anton Bikineev | f62d1bf | 2021-05-15 17:56:07 | [diff] [blame] | 1693 | absl::optional<SkColor> WebContentsImpl::GetBackgroundColor() { |
Jeremy Roman | 2d8dfe13 | 2021-07-06 20:51:26 | [diff] [blame] | 1694 | return GetPrimaryPage().background_color(); |
Alan Cutter | d73a15d9 | 2020-08-21 07:12:45 | [diff] [blame] | 1695 | } |
| 1696 | |
Bo Liu | b5e79d9 | 2021-06-12 01:40:41 | [diff] [blame] | 1697 | void WebContentsImpl::SetPageBaseBackgroundColor( |
| 1698 | absl::optional<SkColor> color) { |
| 1699 | if (page_base_background_color_ == color) |
| 1700 | return; |
| 1701 | page_base_background_color_ = color; |
| 1702 | ExecutePageBroadcastMethod(base::BindRepeating( |
| 1703 | [](absl::optional<SkColor> color, RenderViewHostImpl* rvh) { |
| 1704 | // Null `broadcast` can happen before view is created on the renderer |
| 1705 | // side, in which case this color will be sent in CreateView. |
| 1706 | if (auto& broadcast = rvh->GetAssociatedPageBroadcast()) |
| 1707 | broadcast->SetPageBaseBackgroundColor(color); |
| 1708 | }, |
| 1709 | page_base_background_color_)); |
| 1710 | } |
| 1711 | |
tom | 855c4bf3 | 2021-09-30 00:27:26 | [diff] [blame] | 1712 | void WebContentsImpl::SetColorProviderSource(ui::ColorProviderSource* source) { |
| 1713 | ColorProviderSourceObserver::Observe(source); |
| 1714 | } |
| 1715 | |
Doug Turner | 63f3c7b | 2017-07-29 05:10:01 | [diff] [blame] | 1716 | void WebContentsImpl::SetAccessibilityMode(ui::AXMode mode) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 1717 | OPTIONAL_TRACE_EVENT2("content", "WebContentsImpl::SetAccessibilityMode", |
| 1718 | "mode", mode.ToString(), "previous_mode", |
| 1719 | accessibility_mode_.ToString()); |
| 1720 | |
[email protected] | 9564021 | 2014-07-26 18:14:30 | [diff] [blame] | 1721 | if (mode == accessibility_mode_) |
| 1722 | return; |
| 1723 | |
dmazzoni | f8a3104 | 2016-12-03 00:52:13 | [diff] [blame] | 1724 | // Don't allow accessibility to be enabled for WebContents that are never |
danakj | 77eb7e8 | 2020-01-09 19:38:46 | [diff] [blame] | 1725 | // user-visible, like background pages. |
| 1726 | if (IsNeverComposited()) |
dmazzoni | f8a3104 | 2016-12-03 00:52:13 | [diff] [blame] | 1727 | return; |
| 1728 | |
[email protected] | 9564021 | 2014-07-26 18:14:30 | [diff] [blame] | 1729 | accessibility_mode_ = mode; |
Ian Vollick | ffdc054 | 2021-05-14 02:12:30 | [diff] [blame] | 1730 | // Update state for all frames in this tree and inner trees. Should also |
| 1731 | // include speculative frame hosts. |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 1732 | GetPrimaryMainFrame()->ForEachRenderFrameHostIncludingSpeculative( |
Daniel Cheng | 982f2b2 | 2022-08-25 23:46:16 | [diff] [blame] | 1733 | &RenderFrameHostImpl::UpdateAccessibilityMode); |
[email protected] | 9564021 | 2014-07-26 18:14:30 | [diff] [blame] | 1734 | } |
| 1735 | |
Doug Turner | 63f3c7b | 2017-07-29 05:10:01 | [diff] [blame] | 1736 | void WebContentsImpl::AddAccessibilityMode(ui::AXMode mode) { |
| 1737 | ui::AXMode new_mode(accessibility_mode_); |
dougt | cd3dad73 | 2017-03-14 03:26:23 | [diff] [blame] | 1738 | new_mode |= mode; |
| 1739 | SetAccessibilityMode(new_mode); |
[email protected] | 9564021 | 2014-07-26 18:14:30 | [diff] [blame] | 1740 | } |
| 1741 | |
Avi Drissman | 1e620f3 | 2018-03-16 13:57:29 | [diff] [blame] | 1742 | // Helper class used by WebContentsImpl::RequestAXTreeSnapshot. |
| 1743 | // Handles the callbacks from parallel snapshot requests to each frame, |
| 1744 | // and feeds the results to an AXTreeCombiner, which converts them into a |
| 1745 | // single combined accessibility tree. |
Dave Tapuska | c05b017 | 2021-09-09 20:27:19 | [diff] [blame] | 1746 | class AXTreeSnapshotCombiner : public base::RefCounted<AXTreeSnapshotCombiner> { |
Avi Drissman | 1e620f3 | 2018-03-16 13:57:29 | [diff] [blame] | 1747 | public: |
Dave Tapuska | c05b017 | 2021-09-09 20:27:19 | [diff] [blame] | 1748 | AXTreeSnapshotCombiner(WebContents::AXTreeSnapshotCallback callback, |
| 1749 | mojom::SnapshotAccessibilityTreeParamsPtr params) |
| 1750 | : callback_(std::move(callback)), params_(std::move(params)) {} |
Avi Drissman | 1e620f3 | 2018-03-16 13:57:29 | [diff] [blame] | 1751 | |
Dave Tapuska | c05b017 | 2021-09-09 20:27:19 | [diff] [blame] | 1752 | WebContents::AXTreeSnapshotCallback AddFrame(bool is_root) { |
Avi Drissman | 1e620f3 | 2018-03-16 13:57:29 | [diff] [blame] | 1753 | // Adds a reference to |this|. |
| 1754 | return base::BindOnce(&AXTreeSnapshotCombiner::ReceiveSnapshot, this, |
| 1755 | is_root); |
| 1756 | } |
| 1757 | |
| 1758 | void ReceiveSnapshot(bool is_root, const ui::AXTreeUpdate& snapshot) { |
| 1759 | combiner_.AddTree(snapshot, is_root); |
| 1760 | } |
| 1761 | |
Dave Tapuska | c05b017 | 2021-09-09 20:27:19 | [diff] [blame] | 1762 | void AXTreeSnapshotOnFrame(RenderFrameHostImpl* rfhi) { |
| 1763 | OPTIONAL_TRACE_EVENT0("content", |
| 1764 | "AXTreeSnapshotCombiner::AXTreeSnapshotOnFrame"); |
Dave Tapuska | 5dd5f982 | 2021-09-10 20:57:15 | [diff] [blame] | 1765 | bool is_root = !rfhi->GetParentOrOuterDocumentOrEmbedder(); |
Dave Tapuska | c05b017 | 2021-09-09 20:27:19 | [diff] [blame] | 1766 | rfhi->RequestAXTreeSnapshot(AddFrame(is_root), params_.Clone()); |
| 1767 | } |
| 1768 | |
Avi Drissman | 1e620f3 | 2018-03-16 13:57:29 | [diff] [blame] | 1769 | private: |
| 1770 | friend class base::RefCounted<AXTreeSnapshotCombiner>; |
| 1771 | |
| 1772 | // This is called automatically after the last call to ReceiveSnapshot |
| 1773 | // when there are no more references to this object. |
| 1774 | ~AXTreeSnapshotCombiner() { |
| 1775 | combiner_.Combine(); |
| 1776 | std::move(callback_).Run(combiner_.combined()); |
| 1777 | } |
| 1778 | |
| 1779 | ui::AXTreeCombiner combiner_; |
Dave Tapuska | c05b017 | 2021-09-09 20:27:19 | [diff] [blame] | 1780 | WebContents::AXTreeSnapshotCallback callback_; |
| 1781 | mojom::SnapshotAccessibilityTreeParamsPtr params_; |
Avi Drissman | 1e620f3 | 2018-03-16 13:57:29 | [diff] [blame] | 1782 | }; |
| 1783 | |
| 1784 | void WebContentsImpl::RequestAXTreeSnapshot(AXTreeSnapshotCallback callback, |
Dominic Mazzoni | 77d0aba | 2021-01-14 09:42:16 | [diff] [blame] | 1785 | ui::AXMode ax_mode, |
| 1786 | bool exclude_offscreen, |
| 1787 | size_t max_nodes, |
| 1788 | base::TimeDelta timeout) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 1789 | OPTIONAL_TRACE_EVENT1("content", "WebContentsImpl::RequestAXTreeSnapshot", |
| 1790 | "mode", ax_mode.ToString()); |
dmazzoni | d95ae84 | 2016-04-12 21:17:39 | [diff] [blame] | 1791 | // Send a request to each of the frames in parallel. Each one will return |
| 1792 | // an accessibility tree snapshot, and AXTreeSnapshotCombiner will combine |
| 1793 | // them into a single tree and call |callback| with that result, then |
| 1794 | // delete |combiner|. |
Dominic Mazzoni | 77d0aba | 2021-01-14 09:42:16 | [diff] [blame] | 1795 | auto params = mojom::SnapshotAccessibilityTreeParams::New(); |
Amanda Lin Dietz | e54912e | 2022-11-22 20:25:57 | [diff] [blame] | 1796 | params->ax_mode = ax_mode.flags(); |
Dominic Mazzoni | 77d0aba | 2021-01-14 09:42:16 | [diff] [blame] | 1797 | params->exclude_offscreen = exclude_offscreen; |
| 1798 | params->max_nodes = max_nodes; |
| 1799 | params->timeout = timeout; |
Avi Drissman | 1e620f3 | 2018-03-16 13:57:29 | [diff] [blame] | 1800 | |
Dave Tapuska | c05b017 | 2021-09-09 20:27:19 | [diff] [blame] | 1801 | auto combiner = base::MakeRefCounted<AXTreeSnapshotCombiner>( |
| 1802 | std::move(callback), std::move(params)); |
Daniel Cheng | 982f2b2 | 2022-08-25 23:46:16 | [diff] [blame] | 1803 | GetPrimaryMainFrame()->ForEachRenderFrameHost( |
| 1804 | [&combiner](RenderFrameHostImpl* rfh) { |
| 1805 | combiner->AXTreeSnapshotOnFrame(rfh); |
| 1806 | }); |
dmazzoni | 83ba5c8 | 2015-04-14 07:11:51 | [diff] [blame] | 1807 | } |
| 1808 | |
Becca Hughes | 3b5a4348 | 2018-07-17 22:31:55 | [diff] [blame] | 1809 | void WebContentsImpl::NotifyViewportFitChanged( |
| 1810 | blink::mojom::ViewportFit value) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 1811 | OPTIONAL_TRACE_EVENT1("content", "WebContentsImpl::NotifyViewportFitChanged", |
| 1812 | "value", static_cast<int>(value)); |
Andrew Grieve | e19cd93e | 2021-01-22 05:43:06 | [diff] [blame] | 1813 | observers_.NotifyObservers(&WebContentsObserver::ViewportFitChanged, value); |
Becca Hughes | 3b5a4348 | 2018-07-17 22:31:55 | [diff] [blame] | 1814 | } |
| 1815 | |
Lucas Furukawa Gadani | e1c5dfda | 2018-11-29 17:57:41 | [diff] [blame] | 1816 | FindRequestManager* WebContentsImpl::GetFindRequestManagerForTesting() { |
Miyoung Shin | bf4c40c | 2021-10-21 11:00:23 | [diff] [blame] | 1817 | return GetOrCreateFindRequestManager(); |
Ehsan Karamad | 6beb2ea | 2018-11-25 18:15:13 | [diff] [blame] | 1818 | } |
| 1819 | |
akaba | c6bd121 | 2018-06-25 20:10:48 | [diff] [blame] | 1820 | void WebContentsImpl::UpdateZoom() { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 1821 | OPTIONAL_TRACE_EVENT0("content", "WebContentsImpl::UpdateZoom"); |
akaba | c6bd121 | 2018-06-25 20:10:48 | [diff] [blame] | 1822 | RenderWidgetHostImpl* rwh = GetRenderViewHost()->GetWidget(); |
| 1823 | if (rwh->GetView()) |
| 1824 | rwh->SynchronizeVisualProperties(); |
wjmaclean | 6495190 | 2016-04-29 20:59:12 | [diff] [blame] | 1825 | } |
| 1826 | |
wjmaclean | 6495190 | 2016-04-29 20:59:12 | [diff] [blame] | 1827 | void WebContentsImpl::UpdateZoomIfNecessary(const std::string& scheme, |
akaba | c6bd121 | 2018-06-25 20:10:48 | [diff] [blame] | 1828 | const std::string& host) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 1829 | OPTIONAL_TRACE_EVENT2("content", "WebContentsImpl::UpdateZoomIfNecessary", |
| 1830 | "scheme", scheme, "host", host); |
wjmaclean | 6495190 | 2016-04-29 20:59:12 | [diff] [blame] | 1831 | NavigationEntry* entry = GetController().GetLastCommittedEntry(); |
| 1832 | if (!entry) |
| 1833 | return; |
| 1834 | |
| 1835 | GURL url = HostZoomMap::GetURLFromEntry(entry); |
| 1836 | if (host != net::GetHostOrSpecFromURL(url) || |
| 1837 | (!scheme.empty() && !url.SchemeIs(scheme))) { |
| 1838 | return; |
| 1839 | } |
| 1840 | |
akaba | c6bd121 | 2018-06-25 20:10:48 | [diff] [blame] | 1841 | UpdateZoom(); |
wjmaclean | 6495190 | 2016-04-29 20:59:12 | [diff] [blame] | 1842 | } |
| 1843 | |
paulmeyer | feafc2d | 2017-04-25 21:46:40 | [diff] [blame] | 1844 | std::vector<WebContentsImpl*> WebContentsImpl::GetWebContentsAndAllInner() { |
| 1845 | std::vector<WebContentsImpl*> all_contents(1, this); |
| 1846 | |
| 1847 | for (size_t i = 0; i != all_contents.size(); ++i) { |
| 1848 | for (auto* inner_contents : all_contents[i]->GetInnerWebContents()) { |
Lucas Furukawa Gadani | 2ec00c8 | 2018-12-14 15:53:16 | [diff] [blame] | 1849 | all_contents.push_back(static_cast<WebContentsImpl*>(inner_contents)); |
paulmeyer | feafc2d | 2017-04-25 21:46:40 | [diff] [blame] | 1850 | } |
| 1851 | } |
| 1852 | |
| 1853 | return all_contents; |
| 1854 | } |
| 1855 | |
Sreeja Kamishetty | cfb4b86 | 2022-03-29 16:59:39 | [diff] [blame] | 1856 | void WebContentsImpl::OnManifestUrlChanged(PageImpl& page) { |
Julie Jeongeun Kim | 9e20451 | 2021-06-24 07:28:54 | [diff] [blame] | 1857 | absl::optional<GURL> manifest_url = page.GetManifestUrl(); |
Julie Jeongeun Kim | d74c1e7f | 2021-06-18 04:06:18 | [diff] [blame] | 1858 | if (!manifest_url.has_value()) |
| 1859 | return; |
| 1860 | |
Sreeja Kamishetty | cfb4b86 | 2022-03-29 16:59:39 | [diff] [blame] | 1861 | if (!page.IsPrimary()) |
| 1862 | return; |
| 1863 | |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 1864 | OPTIONAL_TRACE_EVENT2("content", "WebContentsImpl::NotifyManifestUrlChanged", |
Sreeja Kamishetty | 1b5c143 | 2021-06-25 11:32:59 | [diff] [blame] | 1865 | "render_frame_host", &page.GetMainDocument(), |
Julie Jeongeun Kim | 9e20451 | 2021-06-24 07:28:54 | [diff] [blame] | 1866 | "manifest_url", manifest_url); |
| 1867 | observers_.NotifyObservers(&WebContentsObserver::DidUpdateWebManifestURL, |
Sreeja Kamishetty | 1b5c143 | 2021-06-25 11:32:59 | [diff] [blame] | 1868 | &page.GetMainDocument(), *manifest_url); |
Sam McNally | 145fb17 | 2017-05-10 00:13:23 | [diff] [blame] | 1869 | } |
| 1870 | |
Lucas Furukawa Gadani | e1c5dfda | 2018-11-29 17:57:41 | [diff] [blame] | 1871 | WebUI* WebContentsImpl::GetWebUI() { |
Carlos Caballero | b65b6e3a | 2021-11-15 10:09:00 | [diff] [blame] | 1872 | return primary_frame_tree_.root()->current_frame_host()->web_ui(); |
[email protected] | d5f942ba | 2008-09-26 19:30:34 | [diff] [blame] | 1873 | } |
| 1874 | |
Maks Orlovich | 73f374d | 2020-04-02 12:46:13 | [diff] [blame] | 1875 | void WebContentsImpl::SetUserAgentOverride( |
| 1876 | const blink::UserAgentOverride& ua_override, |
| 1877 | bool override_in_new_tabs) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 1878 | OPTIONAL_TRACE_EVENT2("content", "WebContentsImpl::SetUserAgentOverride", |
| 1879 | "ua_override", ua_override.ua_string_override, |
| 1880 | "override_in_new_tabs", override_in_new_tabs); |
Maks Orlovich | 73f374d | 2020-04-02 12:46:13 | [diff] [blame] | 1881 | DCHECK(!ua_override.ua_metadata_override.has_value() || |
| 1882 | !ua_override.ua_string_override.empty()); |
| 1883 | |
| 1884 | if (GetUserAgentOverride() == ua_override) |
[email protected] | bf70edce | 2012-06-20 22:32:22 | [diff] [blame] | 1885 | return; |
| 1886 | |
David Van Cleve | de398a5 | 2020-12-28 21:33:03 | [diff] [blame] | 1887 | if (!ua_override.ua_string_override.empty() && |
| 1888 | !net::HttpUtil::IsValidHeaderValue(ua_override.ua_string_override)) { |
| 1889 | return; |
| 1890 | } |
| 1891 | |
Changwan Ryu | c1134a8 | 2018-03-07 02:10:14 | [diff] [blame] | 1892 | should_override_user_agent_in_new_tabs_ = override_in_new_tabs; |
| 1893 | |
Maks Orlovich | 73f374d | 2020-04-02 12:46:13 | [diff] [blame] | 1894 | renderer_preferences_.user_agent_override = ua_override; |
[email protected] | bf70edce | 2012-06-20 22:32:22 | [diff] [blame] | 1895 | |
Bruce Long | 1e3e1f54 | 2019-10-16 17:56:28 | [diff] [blame] | 1896 | // Send the new override string to all renderers in the current page. |
| 1897 | SyncRendererPrefs(); |
[email protected] | bf70edce | 2012-06-20 22:32:22 | [diff] [blame] | 1898 | |
| 1899 | // Reload the page if a load is currently in progress to avoid having |
| 1900 | // different parts of the page loaded using different user agents. |
Scott Violet | e1d58cc | 2020-04-28 23:07:17 | [diff] [blame] | 1901 | // No need to reload if the current entry matches that of the |
| 1902 | // NavigationRequest supplied to DidStartNavigation() as NavigationRequest |
| 1903 | // handles it. |
Arthur Sonzogni | f6785ec | 2022-12-05 10:11:50 | [diff] [blame] | 1904 | ForEachFrameTree(base::BindRepeating([](FrameTree& frame_tree) { |
Takashi Toyoshima | c8d2634 | 2021-08-06 07:22:28 | [diff] [blame] | 1905 | // For prerendering, we don't want to activate a prerendered page loaded |
| 1906 | // with a stale UA and will handle it even if it finishes loading. |
Arthur Sonzogni | f6785ec | 2022-12-05 10:11:50 | [diff] [blame] | 1907 | if (!frame_tree.IsLoadingIncludingInnerFrameTrees() && |
| 1908 | !frame_tree.is_prerendering()) { |
Takashi Toyoshima | fa9a910e | 2021-08-05 04:03:00 | [diff] [blame] | 1909 | return; |
Yu Gao | c8c1855 | 2022-06-22 14:38:45 | [diff] [blame] | 1910 | } |
Takashi Toyoshima | c8d2634 | 2021-08-06 07:22:28 | [diff] [blame] | 1911 | |
Arthur Sonzogni | f6785ec | 2022-12-05 10:11:50 | [diff] [blame] | 1912 | NavigationEntry* entry = frame_tree.controller().GetVisibleEntry(); |
Takashi Toyoshima | fa9a910e | 2021-08-05 04:03:00 | [diff] [blame] | 1913 | if (!entry || !entry->GetIsOverridingUserAgent()) |
| 1914 | return; |
Arthur Sonzogni | f6785ec | 2022-12-05 10:11:50 | [diff] [blame] | 1915 | if (frame_tree.root()->navigation_request() && |
| 1916 | !frame_tree.root()->navigation_request()->ua_change_requires_reload()) { |
Takashi Toyoshima | fa9a910e | 2021-08-05 04:03:00 | [diff] [blame] | 1917 | return; |
| 1918 | } |
Arthur Sonzogni | f6785ec | 2022-12-05 10:11:50 | [diff] [blame] | 1919 | if (frame_tree.is_prerendering()) { |
Takashi Toyoshima | fa9a910e | 2021-08-05 04:03:00 | [diff] [blame] | 1920 | // Just cancel if the FrameTree is for prerendering, as prerendered |
| 1921 | // page may not allow another navigation including a reload, depending |
| 1922 | // on conditions. |
Arthur Sonzogni | f6785ec | 2022-12-05 10:11:50 | [diff] [blame] | 1923 | frame_tree.GetMainFrame()->CancelPrerendering(PrerenderCancellationReason( |
| 1924 | PrerenderFinalStatus::kUaChangeRequiresReload)); |
Takashi Toyoshima | fa9a910e | 2021-08-05 04:03:00 | [diff] [blame] | 1925 | } else { |
Arthur Sonzogni | f6785ec | 2022-12-05 10:11:50 | [diff] [blame] | 1926 | frame_tree.controller().Reload(ReloadType::BYPASSING_CACHE, true); |
Takashi Toyoshima | fa9a910e | 2021-08-05 04:03:00 | [diff] [blame] | 1927 | } |
| 1928 | })); |
[email protected] | 8d0f331 | 2012-08-18 01:47:53 | [diff] [blame] | 1929 | |
Andrew Grieve | e19cd93e | 2021-01-22 05:43:06 | [diff] [blame] | 1930 | observers_.NotifyObservers(&WebContentsObserver::UserAgentOverrideSet, |
| 1931 | ua_override); |
[email protected] | 86ef6a39 | 2012-05-11 22:03:11 | [diff] [blame] | 1932 | } |
| 1933 | |
Scott Violet | 2a6c5bff | 2020-04-29 23:59:06 | [diff] [blame] | 1934 | void WebContentsImpl::SetRendererInitiatedUserAgentOverrideOption( |
| 1935 | NavigationController::UserAgentOverrideOption option) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 1936 | OPTIONAL_TRACE_EVENT0( |
| 1937 | "content", |
| 1938 | "WebContentsImpl::SetRendererInitiatedUserAgentOverrideOption"); |
Scott Violet | 2a6c5bff | 2020-04-29 23:59:06 | [diff] [blame] | 1939 | renderer_initiated_user_agent_override_option_ = option; |
| 1940 | } |
| 1941 | |
Maks Orlovich | 73f374d | 2020-04-02 12:46:13 | [diff] [blame] | 1942 | const blink::UserAgentOverride& WebContentsImpl::GetUserAgentOverride() { |
[email protected] | bf70edce | 2012-06-20 22:32:22 | [diff] [blame] | 1943 | return renderer_preferences_.user_agent_override; |
[email protected] | 86ef6a39 | 2012-05-11 22:03:11 | [diff] [blame] | 1944 | } |
| 1945 | |
Scott Violet | 2a6c5bff | 2020-04-29 23:59:06 | [diff] [blame] | 1946 | bool WebContentsImpl::ShouldOverrideUserAgentForRendererInitiatedNavigation() { |
Carlos Caballero | 40b0efd | 2021-01-26 11:55:00 | [diff] [blame] | 1947 | NavigationEntryImpl* current_entry = GetController().GetLastCommittedEntry(); |
Rakina Zata Amni | 2322f4f8 | 2022-01-24 13:24:24 | [diff] [blame] | 1948 | if (!current_entry || current_entry->IsInitialEntry()) |
Scott Violet | 2a6c5bff | 2020-04-29 23:59:06 | [diff] [blame] | 1949 | return should_override_user_agent_in_new_tabs_; |
| 1950 | |
| 1951 | switch (renderer_initiated_user_agent_override_option_) { |
| 1952 | case NavigationController::UA_OVERRIDE_INHERIT: |
| 1953 | return current_entry->GetIsOverridingUserAgent(); |
| 1954 | case NavigationController::UA_OVERRIDE_TRUE: |
| 1955 | return true; |
| 1956 | case NavigationController::UA_OVERRIDE_FALSE: |
| 1957 | return false; |
| 1958 | default: |
| 1959 | break; |
| 1960 | } |
| 1961 | return false; |
Changwan Ryu | c1134a8 | 2018-03-07 02:10:14 | [diff] [blame] | 1962 | } |
| 1963 | |
dmazzoni | dd3d51a7 | 2016-12-14 18:41:01 | [diff] [blame] | 1964 | void WebContentsImpl::EnableWebContentsOnlyAccessibilityMode() { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 1965 | OPTIONAL_TRACE_EVENT0( |
| 1966 | "content", "WebContentsImpl::EnableWebContentsOnlyAccessibilityMode"); |
Dominic Mazzoni | d9fda17 | 2019-03-07 17:12:07 | [diff] [blame] | 1967 | // If accessibility is already enabled, we'll need to force a reset |
| 1968 | // in order to ensure new observers of accessibility events get the |
| 1969 | // full accessibility tree from scratch. |
| 1970 | bool need_reset = GetAccessibilityMode().has_mode(ui::AXMode::kWebContents); |
| 1971 | |
| 1972 | ui::AXMode desired_mode = |
| 1973 | GetContentClient()->browser()->GetAXModeForBrowserContext( |
| 1974 | GetBrowserContext()); |
| 1975 | desired_mode |= ui::kAXModeWebContentsOnly; |
| 1976 | AddAccessibilityMode(desired_mode); |
| 1977 | |
Ian Vollick | ffdc054 | 2021-05-14 02:12:30 | [diff] [blame] | 1978 | // Accessibility mode updates include speculative RFH's as well as any inner |
| 1979 | // trees. Iterate across these as we do for SetAccessibilityMode (which is |
| 1980 | // called indirectly above via AddAccessibilityMode). |
Dominic Mazzoni | d9fda17 | 2019-03-07 17:12:07 | [diff] [blame] | 1981 | if (need_reset) { |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 1982 | GetPrimaryMainFrame()->ForEachRenderFrameHostIncludingSpeculative( |
Daniel Cheng | 982f2b2 | 2022-08-25 23:46:16 | [diff] [blame] | 1983 | &RenderFrameHostImpl::AccessibilityReset); |
dcheng | afb53e2 | 2016-02-04 08:11:08 | [diff] [blame] | 1984 | } |
[email protected] | 9564021 | 2014-07-26 18:14:30 | [diff] [blame] | 1985 | } |
| 1986 | |
Lucas Furukawa Gadani | e1c5dfda | 2018-11-29 17:57:41 | [diff] [blame] | 1987 | bool WebContentsImpl::IsWebContentsOnlyAccessibilityModeForTesting() { |
Doug Turner | 63f3c7b | 2017-07-29 05:10:01 | [diff] [blame] | 1988 | return accessibility_mode_ == ui::kAXModeWebContentsOnly; |
[email protected] | 9564021 | 2014-07-26 18:14:30 | [diff] [blame] | 1989 | } |
| 1990 | |
Lucas Furukawa Gadani | e1c5dfda | 2018-11-29 17:57:41 | [diff] [blame] | 1991 | bool WebContentsImpl::IsFullAccessibilityModeForTesting() { |
Doug Turner | 63f3c7b | 2017-07-29 05:10:01 | [diff] [blame] | 1992 | return accessibility_mode_ == ui::kAXModeComplete; |
[email protected] | 9564021 | 2014-07-26 18:14:30 | [diff] [blame] | 1993 | } |
| 1994 | |
Xiaohan Wang | 24ec934 | 2022-01-15 17:34:22 | [diff] [blame] | 1995 | #if BUILDFLAG(IS_ANDROID) |
Becca Hughes | 6daf566 | 2018-06-27 16:50:54 | [diff] [blame] | 1996 | |
| 1997 | void WebContentsImpl::SetDisplayCutoutSafeArea(gfx::Insets insets) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 1998 | OPTIONAL_TRACE_EVENT0("content", "WebContentsImpl::SetDisplayCutoutSafeArea"); |
Becca Hughes | 8633462 | 2018-07-09 21:29:38 | [diff] [blame] | 1999 | if (display_cutout_host_impl_) |
| 2000 | display_cutout_host_impl_->SetDisplayCutoutSafeArea(insets); |
Becca Hughes | 6daf566 | 2018-06-27 16:50:54 | [diff] [blame] | 2001 | } |
| 2002 | |
| 2003 | #endif |
| 2004 | |
Jan Wilken Dörrie | aace0cfef | 2021-03-11 22:01:58 | [diff] [blame] | 2005 | const std::u16string& WebContentsImpl::GetTitle() { |
Nasko Oskov | 2f7d211 | 2019-10-29 03:26:57 | [diff] [blame] | 2006 | WebUI* our_web_ui = |
| 2007 | GetRenderManager()->speculative_frame_host() |
| 2008 | ? GetRenderManager()->speculative_frame_host()->web_ui() |
| 2009 | : GetRenderManager()->current_frame_host()->web_ui(); |
[email protected] | 7ade273 | 2011-02-10 00:13:58 | [diff] [blame] | 2010 | if (our_web_ui) { |
[email protected] | 96d185d | 2009-04-24 03:28:54 | [diff] [blame] | 2011 | // Don't override the title in view source mode. |
Carlos Caballero | 40b0efd | 2021-01-26 11:55:00 | [diff] [blame] | 2012 | NavigationEntry* entry = GetController().GetVisibleEntry(); |
[email protected] | 96d185d | 2009-04-24 03:28:54 | [diff] [blame] | 2013 | if (!(entry && entry->IsViewSourceMode())) { |
[email protected] | e011291 | 2011-02-02 22:54:35 | [diff] [blame] | 2014 | // Give the Web UI the chance to override our title. |
Jan Wilken Dörrie | aace0cfef | 2021-03-11 22:01:58 | [diff] [blame] | 2015 | const std::u16string& title = our_web_ui->GetOverriddenTitle(); |
[email protected] | 96d185d | 2009-04-24 03:28:54 | [diff] [blame] | 2016 | if (!title.empty()) |
| 2017 | return title; |
| 2018 | } |
| 2019 | } |
| 2020 | |
Rakina Zata Amni | 46087a1 | 2022-11-11 08:28:38 | [diff] [blame] | 2021 | return GetNavigationEntryForTitle()->GetTitleForDisplay(); |
[email protected] | 96d185d | 2009-04-24 03:28:54 | [diff] [blame] | 2022 | } |
| 2023 | |
Lucas Furukawa Gadani | e1c5dfda | 2018-11-29 17:57:41 | [diff] [blame] | 2024 | SiteInstanceImpl* WebContentsImpl::GetSiteInstance() { |
Aaron Colwell | c4bd7d6 | 2021-01-29 04:23:13 | [diff] [blame] | 2025 | return GetRenderManager()->current_frame_host()->GetSiteInstance(); |
[email protected] | 96d185d | 2009-04-24 03:28:54 | [diff] [blame] | 2026 | } |
| 2027 | |
Lucas Furukawa Gadani | e1c5dfda | 2018-11-29 17:57:41 | [diff] [blame] | 2028 | bool WebContentsImpl::IsLoading() { |
Yu Gao | c8c1855 | 2022-06-22 14:38:45 | [diff] [blame] | 2029 | return primary_frame_tree_.IsLoadingIncludingInnerFrameTrees(); |
[email protected] | 3c9e187 | 2010-11-18 16:17:49 | [diff] [blame] | 2030 | } |
| 2031 | |
Lucas Furukawa Gadani | e1c5dfda | 2018-11-29 17:57:41 | [diff] [blame] | 2032 | double WebContentsImpl::GetLoadProgress() { |
Sreeja Kamishetty | 0be3b1b | 2021-08-12 17:04:15 | [diff] [blame] | 2033 | // TODO(crbug.com/1199682): Make this MPArch friendly considering primary |
| 2034 | // frame tree and its descendants. |
Carlos Caballero | b65b6e3a | 2021-11-15 10:09:00 | [diff] [blame] | 2035 | return primary_frame_tree_.GetLoadProgress(); |
Peter Boström | 18a40fa | 2018-10-31 19:03:50 | [diff] [blame] | 2036 | } |
| 2037 | |
Nate Chapin | 9aabf5f | 2021-11-12 00:31:19 | [diff] [blame] | 2038 | bool WebContentsImpl::ShouldShowLoadingUI() { |
| 2039 | return IsLoading() && should_show_loading_ui_; |
[email protected] | 6dfed69 | 2014-05-22 04:18:03 | [diff] [blame] | 2040 | } |
| 2041 | |
Sreeja Kamishetty | 8134758 | 2022-01-06 12:46:33 | [diff] [blame] | 2042 | bool WebContentsImpl::IsDocumentOnLoadCompletedInPrimaryMainFrame() { |
Jeremy Roman | 1ed2346b | 2021-06-25 15:26:35 | [diff] [blame] | 2043 | // TODO(mparch): This should be moved to Page, and callers should use it |
| 2044 | // directly. |
| 2045 | return GetPrimaryPage().is_on_load_completed_in_main_document(); |
Alexander Timin | dd0fa0c5 | 2019-11-06 13:03:37 | [diff] [blame] | 2046 | } |
| 2047 | |
Lucas Furukawa Gadani | e1c5dfda | 2018-11-29 17:57:41 | [diff] [blame] | 2048 | bool WebContentsImpl::IsWaitingForResponse() { |
Peter Boström | a9a27f6e | 2018-11-21 13:12:19 | [diff] [blame] | 2049 | NavigationRequest* ongoing_navigation_request = |
Carlos Caballero | b65b6e3a | 2021-11-15 10:09:00 | [diff] [blame] | 2050 | primary_frame_tree_.root()->navigation_request(); |
Peter Boström | a9a27f6e | 2018-11-21 13:12:19 | [diff] [blame] | 2051 | |
| 2052 | // An ongoing navigation request means we're waiting for a response. |
| 2053 | return ongoing_navigation_request != nullptr; |
[email protected] | be1f56ab | 2011-12-22 06:55:31 | [diff] [blame] | 2054 | } |
| 2055 | |
Lucas Furukawa Gadani | e1c5dfda | 2018-11-29 17:57:41 | [diff] [blame] | 2056 | const net::LoadStateWithParam& WebContentsImpl::GetLoadState() { |
[email protected] | be1f56ab | 2011-12-22 06:55:31 | [diff] [blame] | 2057 | return load_state_; |
| 2058 | } |
| 2059 | |
Jan Wilken Dörrie | aace0cfef | 2021-03-11 22:01:58 | [diff] [blame] | 2060 | const std::u16string& WebContentsImpl::GetLoadStateHost() { |
[email protected] | be1f56ab | 2011-12-22 06:55:31 | [diff] [blame] | 2061 | return load_state_host_; |
| 2062 | } |
| 2063 | |
Lucas Furukawa Gadani | e1c5dfda | 2018-11-29 17:57:41 | [diff] [blame] | 2064 | uint64_t WebContentsImpl::GetUploadSize() { |
[email protected] | be1f56ab | 2011-12-22 06:55:31 | [diff] [blame] | 2065 | return upload_size_; |
| 2066 | } |
| 2067 | |
Lucas Furukawa Gadani | e1c5dfda | 2018-11-29 17:57:41 | [diff] [blame] | 2068 | uint64_t WebContentsImpl::GetUploadPosition() { |
[email protected] | be1f56ab | 2011-12-22 06:55:31 | [diff] [blame] | 2069 | return upload_position_; |
| 2070 | } |
| 2071 | |
Lucas Furukawa Gadani | e1c5dfda | 2018-11-29 17:57:41 | [diff] [blame] | 2072 | const std::string& WebContentsImpl::GetEncoding() { |
Dominic Farolino | 5c606c1 | 2021-12-18 09:40:14 | [diff] [blame] | 2073 | return GetPrimaryPage().GetEncoding(); |
[email protected] | be1f56ab | 2011-12-22 06:55:31 | [diff] [blame] | 2074 | } |
| 2075 | |
Charles Zhao | d72f99d | 2018-09-17 14:18:27 | [diff] [blame] | 2076 | bool WebContentsImpl::WasDiscarded() { |
Carlos Caballero | 6a99dac | 2021-11-03 10:41:17 | [diff] [blame] | 2077 | return GetPrimaryFrameTree().root()->was_discarded(); |
Charles Zhao | d72f99d | 2018-09-17 14:18:27 | [diff] [blame] | 2078 | } |
| 2079 | |
Shubhie Panicker | ddf2a4e | 2018-03-06 00:09:06 | [diff] [blame] | 2080 | void WebContentsImpl::SetWasDiscarded(bool was_discarded) { |
Julie Jeongeun Kim | c2e892f | 2023-01-11 10:22:09 | [diff] [blame] | 2081 | // It's set based on a tab and the setting value is started from a primary |
| 2082 | // tree and propagated to all children nodes including a fenced frame node. |
Carlos Caballero | 6a99dac | 2021-11-03 10:41:17 | [diff] [blame] | 2083 | GetPrimaryFrameTree().root()->set_was_discarded(); |
Shubhie Panicker | ddf2a4e | 2018-03-06 00:09:06 | [diff] [blame] | 2084 | } |
| 2085 | |
Dale Curtis | 7030f25 | 2021-04-07 14:05:31 | [diff] [blame] | 2086 | base::ScopedClosureRunner WebContentsImpl::IncrementCapturerCount( |
| 2087 | const gfx::Size& capture_size, |
| 2088 | bool stay_hidden, |
Elad Alon | 0feb660 | 2021-10-14 09:26:36 | [diff] [blame] | 2089 | bool stay_awake, |
| 2090 | bool is_activity) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 2091 | OPTIONAL_TRACE_EVENT0("content", "WebContentsImpl::IncrementCapturerCount"); |
Takashi Toyoshima | ea534ef2 | 2021-07-21 03:27:59 | [diff] [blame] | 2092 | DCHECK(!IsBeingDestroyed()); |
Xianzhu Wang | 724e08f | 2020-09-01 21:35:07 | [diff] [blame] | 2093 | if (stay_hidden) { |
| 2094 | // A hidden capture should not have side effect on the web contents, so it |
| 2095 | // should not pass a non-empty |capture_size| which will cause side effect. |
| 2096 | DCHECK(capture_size.IsEmpty()); |
Collin Baker | c525996 | 2019-11-14 17:51:58 | [diff] [blame] | 2097 | ++hidden_capturer_count_; |
Xianzhu Wang | 724e08f | 2020-09-01 21:35:07 | [diff] [blame] | 2098 | } else { |
Collin Baker | c525996 | 2019-11-14 17:51:58 | [diff] [blame] | 2099 | ++visible_capturer_count_; |
Xianzhu Wang | 724e08f | 2020-09-01 21:35:07 | [diff] [blame] | 2100 | } |
[email protected] | 222f582 | 2014-02-05 23:40:49 | [diff] [blame] | 2101 | |
Dale Curtis | 7030f25 | 2021-04-07 14:05:31 | [diff] [blame] | 2102 | if (stay_awake) |
| 2103 | ++stay_awake_capturer_count_; |
| 2104 | |
Scott Violet | 7f0b5c3 | 2021-11-04 03:06:22 | [diff] [blame] | 2105 | view_->OnCapturerCountChanged(); |
| 2106 | |
[email protected] | 222f582 | 2014-02-05 23:40:49 | [diff] [blame] | 2107 | // Note: This provides a hint to upstream code to size the views optimally |
| 2108 | // for quality (e.g., to avoid scaling). |
| 2109 | if (!capture_size.IsEmpty() && preferred_size_for_capture_.IsEmpty()) { |
| 2110 | preferred_size_for_capture_ = capture_size; |
| 2111 | OnPreferredSizeChanged(preferred_size_); |
| 2112 | } |
ccameron | 8807c38f | 2015-01-17 00:29:19 | [diff] [blame] | 2113 | |
Dale Curtis | 7030f25 | 2021-04-07 14:05:31 | [diff] [blame] | 2114 | if (!capture_wake_lock_ && stay_awake_capturer_count_) { |
Dale Curtis | c496a776 | 2021-02-24 01:15:44 | [diff] [blame] | 2115 | if (auto* wake_lock_context = GetWakeLockContext()) { |
| 2116 | auto receiver = capture_wake_lock_.BindNewPipeAndPassReceiver(); |
| 2117 | wake_lock_context->GetWakeLock( |
| 2118 | device::mojom::WakeLockType::kPreventDisplaySleepAllowDimming, |
| 2119 | device::mojom::WakeLockReason::kOther, "Capturing", |
| 2120 | std::move(receiver)); |
| 2121 | } |
| 2122 | } |
| 2123 | |
| 2124 | if (capture_wake_lock_) |
| 2125 | capture_wake_lock_->RequestWakeLock(); |
| 2126 | |
Elad Alon | 0feb660 | 2021-10-14 09:26:36 | [diff] [blame] | 2127 | UpdateVisibilityAndNotifyPageAndView(GetVisibility(), is_activity); |
[email protected] | 5459798 | 2013-02-06 01:59:55 | [diff] [blame] | 2128 | |
Elad Alon | 0feb660 | 2021-10-14 09:26:36 | [diff] [blame] | 2129 | return base::ScopedClosureRunner(base::BindOnce( |
| 2130 | &WebContentsImpl::DecrementCapturerCount, weak_factory_.GetWeakPtr(), |
| 2131 | stay_hidden, stay_awake, is_activity)); |
[email protected] | be1f56ab | 2011-12-22 06:55:31 | [diff] [blame] | 2132 | } |
| 2133 | |
Elad Alon | f156eb6 | 2021-05-17 22:02:37 | [diff] [blame] | 2134 | const blink::mojom::CaptureHandleConfig& |
| 2135 | WebContentsImpl::GetCaptureHandleConfig() { |
| 2136 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 2137 | return capture_handle_config_; |
| 2138 | } |
| 2139 | |
Lucas Furukawa Gadani | e1c5dfda | 2018-11-29 17:57:41 | [diff] [blame] | 2140 | bool WebContentsImpl::IsBeingCaptured() { |
Collin Baker | c525996 | 2019-11-14 17:51:58 | [diff] [blame] | 2141 | return visible_capturer_count_ + hidden_capturer_count_ > 0; |
[email protected] | f2bd4081 | 2013-07-20 04:30:44 | [diff] [blame] | 2142 | } |
| 2143 | |
Xianzhu Wang | c61434c | 2020-08-21 19:17:30 | [diff] [blame] | 2144 | bool WebContentsImpl::IsBeingVisiblyCaptured() { |
| 2145 | return visible_capturer_count_ > 0; |
| 2146 | } |
| 2147 | |
Lucas Furukawa Gadani | e1c5dfda | 2018-11-29 17:57:41 | [diff] [blame] | 2148 | bool WebContentsImpl::IsAudioMuted() { |
Guido Urdaneta | f837294 | 2019-11-15 09:47:21 | [diff] [blame] | 2149 | return audio_stream_factory_ && audio_stream_factory_->IsMuted(); |
miu | 50f9789 | 2014-09-22 22:49:52 | [diff] [blame] | 2150 | } |
| 2151 | |
| 2152 | void WebContentsImpl::SetAudioMuted(bool mute) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 2153 | OPTIONAL_TRACE_EVENT1("content", "WebContentsImpl::SetAudioMuted", "mute", |
| 2154 | mute); |
miu | 50f9789 | 2014-09-22 22:49:52 | [diff] [blame] | 2155 | DVLOG(1) << "SetAudioMuted(mute=" << mute << "), was " << IsAudioMuted() |
| 2156 | << " for WebContentsImpl@" << this; |
| 2157 | |
| 2158 | if (mute == IsAudioMuted()) |
| 2159 | return; |
| 2160 | |
Guido Urdaneta | f837294 | 2019-11-15 09:47:21 | [diff] [blame] | 2161 | GetAudioStreamFactory()->SetMuted(mute); |
miu | 50f9789 | 2014-09-22 22:49:52 | [diff] [blame] | 2162 | |
Andrew Grieve | e19cd93e | 2021-01-22 05:43:06 | [diff] [blame] | 2163 | observers_.NotifyObservers(&WebContentsObserver::DidUpdateAudioMutingState, |
| 2164 | mute); |
Jan Rucka | 442c83f | 2017-08-08 13:27:54 | [diff] [blame] | 2165 | // Notification for UI updates in response to the changed muting state. |
Collin Baker | af540cf | 2019-09-20 20:54:16 | [diff] [blame] | 2166 | NotifyNavigationStateChanged(INVALIDATE_TYPE_AUDIO); |
Jan Rucka | 442c83f | 2017-08-08 13:27:54 | [diff] [blame] | 2167 | } |
| 2168 | |
| 2169 | bool WebContentsImpl::IsCurrentlyAudible() { |
Chris Hamilton | df0d72cd | 2018-05-29 16:23:53 | [diff] [blame] | 2170 | return is_currently_audible_; |
miu | 50f9789 | 2014-09-22 22:49:52 | [diff] [blame] | 2171 | } |
| 2172 | |
Lucas Furukawa Gadani | e1c5dfda | 2018-11-29 17:57:41 | [diff] [blame] | 2173 | bool WebContentsImpl::IsConnectedToBluetoothDevice() { |
ortuno | df4d798 | 2016-04-08 02:33:35 | [diff] [blame] | 2174 | return bluetooth_connected_device_count_ > 0; |
ortuno | 32e7db3c | 2016-03-29 16:14:20 | [diff] [blame] | 2175 | } |
| 2176 | |
Ovidio Henriquez | 76696f6 | 2020-07-08 03:06:59 | [diff] [blame] | 2177 | bool WebContentsImpl::IsScanningForBluetoothDevices() { |
| 2178 | return bluetooth_scanning_sessions_count_ > 0; |
| 2179 | } |
| 2180 | |
Lucas Furukawa Gadani | 4b4eed0 | 2019-06-04 23:12:04 | [diff] [blame] | 2181 | bool WebContentsImpl::IsConnectedToSerialPort() { |
Reilly Grant | 5e7c79b2 | 2019-04-09 17:26:20 | [diff] [blame] | 2182 | return serial_active_frame_count_ > 0; |
| 2183 | } |
| 2184 | |
Matt Reynolds | e8c6c1f | 2019-11-02 09:53:53 | [diff] [blame] | 2185 | bool WebContentsImpl::IsConnectedToHidDevice() { |
| 2186 | return hid_active_frame_count_ > 0; |
| 2187 | } |
| 2188 | |
Matt Reynolds | ed00ca7e7 | 2022-08-18 20:56:20 | [diff] [blame] | 2189 | bool WebContentsImpl::IsConnectedToUsbDevice() { |
| 2190 | return usb_active_frame_count_ > 0; |
| 2191 | } |
| 2192 | |
Austin Sullivan | afefb72 | 2021-01-14 01:26:39 | [diff] [blame] | 2193 | bool WebContentsImpl::HasFileSystemAccessHandles() { |
| 2194 | return file_system_access_handle_count_ > 0; |
Marijn Kruisselbrink | 2905104 | 2019-08-06 22:56:55 | [diff] [blame] | 2195 | } |
Marijn Kruisselbrink | f1714aa2 | 2019-07-02 03:45:33 | [diff] [blame] | 2196 | |
Lucas Furukawa Gadani | e1c5dfda | 2018-11-29 17:57:41 | [diff] [blame] | 2197 | bool WebContentsImpl::HasPictureInPictureVideo() { |
sawtelle | a7333a8 | 2018-05-31 02:36:36 | [diff] [blame] | 2198 | return has_picture_in_picture_video_; |
| 2199 | } |
| 2200 | |
Klaus Weidner | d821943 | 2022-02-08 21:50:59 | [diff] [blame] | 2201 | bool WebContentsImpl::HasPictureInPictureDocument() { |
| 2202 | return has_picture_in_picture_document_; |
| 2203 | } |
| 2204 | |
| 2205 | void WebContentsImpl::SetHasPictureInPictureCommon( |
| 2206 | bool has_picture_in_picture) { |
| 2207 | NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB); |
| 2208 | observers_.NotifyObservers(&WebContentsObserver::MediaPictureInPictureChanged, |
| 2209 | has_picture_in_picture); |
Fred Shih | eb7d2ff | 2023-01-18 02:31:46 | [diff] [blame] | 2210 | if (has_picture_in_picture) { |
| 2211 | pip_capture_handle_ = IncrementCapturerCount({}, true, false); |
| 2212 | } else { |
| 2213 | pip_capture_handle_.RunAndReset(); |
| 2214 | } |
Klaus Weidner | d821943 | 2022-02-08 21:50:59 | [diff] [blame] | 2215 | |
| 2216 | // Picture-in-picture state can affect how we notify visibility for non- |
| 2217 | // visible pages. |
| 2218 | if (visibility_ != Visibility::VISIBLE) |
| 2219 | UpdateVisibilityAndNotifyPageAndView(visibility_); |
| 2220 | } |
| 2221 | |
sawtelle | a7333a8 | 2018-05-31 02:36:36 | [diff] [blame] | 2222 | void WebContentsImpl::SetHasPictureInPictureVideo( |
| 2223 | bool has_picture_in_picture_video) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 2224 | OPTIONAL_TRACE_EVENT1("content", |
| 2225 | "WebContentsImpl::SetHasPictureInPictureVideo", |
| 2226 | "has_pip_video", has_picture_in_picture_video); |
sawtelle | a7333a8 | 2018-05-31 02:36:36 | [diff] [blame] | 2227 | // If status of |this| is already accurate, there is no need to update. |
| 2228 | if (has_picture_in_picture_video == has_picture_in_picture_video_) |
| 2229 | return; |
| 2230 | has_picture_in_picture_video_ = has_picture_in_picture_video; |
Klaus Weidner | d821943 | 2022-02-08 21:50:59 | [diff] [blame] | 2231 | SetHasPictureInPictureCommon(has_picture_in_picture_video); |
| 2232 | } |
Frank Liberato | e5ae714 | 2021-10-06 15:49:40 | [diff] [blame] | 2233 | |
Klaus Weidner | d821943 | 2022-02-08 21:50:59 | [diff] [blame] | 2234 | void WebContentsImpl::SetHasPictureInPictureDocument( |
| 2235 | bool has_picture_in_picture_document) { |
| 2236 | OPTIONAL_TRACE_EVENT1("content", |
| 2237 | "WebContentsImpl::SetHasPictureInPictureDocument", |
| 2238 | "has_pip_document", has_picture_in_picture_document); |
| 2239 | // If status of |this| is already accurate, there is no need to update. |
| 2240 | if (has_picture_in_picture_document == has_picture_in_picture_document_) |
| 2241 | return; |
| 2242 | has_picture_in_picture_document_ = has_picture_in_picture_document; |
| 2243 | SetHasPictureInPictureCommon(has_picture_in_picture_document); |
sawtelle | a7333a8 | 2018-05-31 02:36:36 | [diff] [blame] | 2244 | } |
| 2245 | |
Lucas Furukawa Gadani | e1c5dfda | 2018-11-29 17:57:41 | [diff] [blame] | 2246 | bool WebContentsImpl::IsCrashed() { |
Khushal | c5eaf22 | 2021-06-30 20:15:48 | [diff] [blame] | 2247 | switch (primary_main_frame_process_status_) { |
Dominic Mazzoni | 73a4832 | 2018-05-25 18:14:07 | [diff] [blame] | 2248 | case base::TERMINATION_STATUS_PROCESS_CRASHED: |
| 2249 | case base::TERMINATION_STATUS_ABNORMAL_TERMINATION: |
| 2250 | case base::TERMINATION_STATUS_PROCESS_WAS_KILLED: |
| 2251 | case base::TERMINATION_STATUS_OOM: |
| 2252 | case base::TERMINATION_STATUS_LAUNCH_FAILED: |
Xiaohan Wang | 24ec934 | 2022-01-15 17:34:22 | [diff] [blame] | 2253 | #if BUILDFLAG(IS_CHROMEOS) |
Dominic Mazzoni | 73a4832 | 2018-05-25 18:14:07 | [diff] [blame] | 2254 | case base::TERMINATION_STATUS_PROCESS_WAS_KILLED_BY_OOM: |
oshima | 62022572 | 2015-06-04 19:45:27 | [diff] [blame] | 2255 | #endif |
Xiaohan Wang | 24ec934 | 2022-01-15 17:34:22 | [diff] [blame] | 2256 | #if BUILDFLAG(IS_ANDROID) |
Dominic Mazzoni | 73a4832 | 2018-05-25 18:14:07 | [diff] [blame] | 2257 | case base::TERMINATION_STATUS_OOM_PROTECTED: |
| 2258 | #endif |
Xiaohan Wang | 24ec934 | 2022-01-15 17:34:22 | [diff] [blame] | 2259 | #if BUILDFLAG(IS_WIN) |
Will Harris | 07925d1 | 2019-10-31 03:03:05 | [diff] [blame] | 2260 | case base::TERMINATION_STATUS_INTEGRITY_FAILURE: |
| 2261 | #endif |
Dominic Mazzoni | 73a4832 | 2018-05-25 18:14:07 | [diff] [blame] | 2262 | return true; |
| 2263 | case base::TERMINATION_STATUS_NORMAL_TERMINATION: |
| 2264 | case base::TERMINATION_STATUS_STILL_RUNNING: |
| 2265 | return false; |
| 2266 | case base::TERMINATION_STATUS_MAX_ENUM: |
| 2267 | NOTREACHED(); |
| 2268 | return false; |
| 2269 | } |
| 2270 | |
| 2271 | NOTREACHED(); |
| 2272 | return false; |
[email protected] | 3c9e187 | 2010-11-18 16:17:49 | [diff] [blame] | 2273 | } |
| 2274 | |
Khushal | c5eaf22 | 2021-06-30 20:15:48 | [diff] [blame] | 2275 | void WebContentsImpl::SetPrimaryMainFrameProcessStatus( |
| 2276 | base::TerminationStatus status, |
| 2277 | int error_code) { |
| 2278 | OPTIONAL_TRACE_EVENT2("content", |
| 2279 | "WebContentsImpl::SetPrimaryMainFrameProcessStatus", |
danakj | cdab6ed5 | 2021-02-10 23:44:13 | [diff] [blame] | 2280 | "status", static_cast<int>(status), "old_status", |
Khushal | c5eaf22 | 2021-06-30 20:15:48 | [diff] [blame] | 2281 | static_cast<int>(primary_main_frame_process_status_)); |
| 2282 | if (status == primary_main_frame_process_status_) |
[email protected] | d5f942ba | 2008-09-26 19:30:34 | [diff] [blame] | 2283 | return; |
| 2284 | |
Khushal | c5eaf22 | 2021-06-30 20:15:48 | [diff] [blame] | 2285 | primary_main_frame_process_status_ = status; |
| 2286 | primary_main_frame_process_error_code_ = error_code; |
[email protected] | 8ff00d7 | 2012-10-23 19:12:21 | [diff] [blame] | 2287 | NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB); |
[email protected] | d5f942ba | 2008-09-26 19:30:34 | [diff] [blame] | 2288 | } |
| 2289 | |
Lucas Furukawa Gadani | e1c5dfda | 2018-11-29 17:57:41 | [diff] [blame] | 2290 | base::TerminationStatus WebContentsImpl::GetCrashedStatus() { |
Khushal | c5eaf22 | 2021-06-30 20:15:48 | [diff] [blame] | 2291 | return primary_main_frame_process_status_; |
[email protected] | be1f56ab | 2011-12-22 06:55:31 | [diff] [blame] | 2292 | } |
| 2293 | |
Lucas Furukawa Gadani | e1c5dfda | 2018-11-29 17:57:41 | [diff] [blame] | 2294 | int WebContentsImpl::GetCrashedErrorCode() { |
Khushal | c5eaf22 | 2021-06-30 20:15:48 | [diff] [blame] | 2295 | return primary_main_frame_process_error_code_; |
afakhry | 9824183 | 2016-03-11 19:27:47 | [diff] [blame] | 2296 | } |
| 2297 | |
Lucas Furukawa Gadani | e1c5dfda | 2018-11-29 17:57:41 | [diff] [blame] | 2298 | bool WebContentsImpl::IsBeingDestroyed() { |
[email protected] | be1f56ab | 2011-12-22 06:55:31 | [diff] [blame] | 2299 | return is_being_destroyed_; |
| 2300 | } |
| 2301 | |
[email protected] | 7f92483 | 2014-08-09 05:57:22 | [diff] [blame] | 2302 | void WebContentsImpl::NotifyNavigationStateChanged( |
| 2303 | InvalidateTypes changed_flags) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 2304 | TRACE_EVENT1("content,navigation", |
| 2305 | "WebContentsImpl::NotifyNavigationStateChanged", "changed_flags", |
| 2306 | static_cast<int>(changed_flags)); |
dalecurtis | 88c24007 | 2015-12-09 02:11:18 | [diff] [blame] | 2307 | // Notify the media observer of potential audibility changes. |
Collin Baker | af540cf | 2019-09-20 20:54:16 | [diff] [blame] | 2308 | if (changed_flags & INVALIDATE_TYPE_AUDIO) { |
mlamouri | 44e4ef4 | 2016-01-20 18:42:27 | [diff] [blame] | 2309 | media_web_contents_observer_->MaybeUpdateAudibleState(); |
dalecurtis | bc6572e1 | 2014-09-12 19:22:30 | [diff] [blame] | 2310 | } |
| 2311 | |
[email protected] | d5f942ba | 2008-09-26 19:30:34 | [diff] [blame] | 2312 | if (delegate_) |
| 2313 | delegate_->NavigationStateChanged(this, changed_flags); |
wjmaclean | 1dabf3e3 | 2016-05-13 23:34:44 | [diff] [blame] | 2314 | |
| 2315 | if (GetOuterWebContents()) |
| 2316 | GetOuterWebContents()->NotifyNavigationStateChanged(changed_flags); |
[email protected] | d5f942ba | 2008-09-26 19:30:34 | [diff] [blame] | 2317 | } |
| 2318 | |
David Black | 9cca359 | 2019-11-06 23:02:22 | [diff] [blame] | 2319 | void WebContentsImpl::OnVerticalScrollDirectionChanged( |
| 2320 | viz::VerticalScrollDirection scroll_direction) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 2321 | OPTIONAL_TRACE_EVENT1("content", |
| 2322 | "WebContentsImpl::OnVerticalScrollDirectionChanged", |
| 2323 | "scroll_direction", static_cast<int>(scroll_direction)); |
Andrew Grieve | e19cd93e | 2021-01-22 05:43:06 | [diff] [blame] | 2324 | observers_.NotifyObservers( |
| 2325 | &WebContentsObserver::DidChangeVerticalScrollDirection, scroll_direction); |
David Black | 9cca359 | 2019-11-06 23:02:22 | [diff] [blame] | 2326 | } |
| 2327 | |
David Bokan | 7ec5ac92 | 2022-11-29 03:31:31 | [diff] [blame] | 2328 | int WebContentsImpl::GetVirtualKeyboardResizeHeight() { |
| 2329 | // Only consider a web contents to be insetted by the virtual keyboard if it |
| 2330 | // is in the currently active tab. |
| 2331 | if (GetVisibility() != Visibility::VISIBLE) |
| 2332 | return 0; |
| 2333 | |
| 2334 | // The only mode where the virtual keyboard causes the web contents to be |
| 2335 | // resized is kResizesContent. |
| 2336 | if (GetPrimaryPage().virtual_keyboard_mode() != |
| 2337 | ui::mojom::VirtualKeyboardMode::kResizesContent) { |
| 2338 | return 0; |
| 2339 | } |
| 2340 | |
| 2341 | // The virtual keyboard never resizes content when fullscreened. |
| 2342 | if (IsFullscreen()) |
| 2343 | return 0; |
| 2344 | |
| 2345 | return GetDelegate() ? GetDelegate()->GetVirtualKeyboardHeight(this) : 0; |
| 2346 | } |
| 2347 | |
Chris Hamilton | df0d72cd | 2018-05-29 16:23:53 | [diff] [blame] | 2348 | void WebContentsImpl::OnAudioStateChanged() { |
| 2349 | // This notification can come from any embedded contents or from this |
| 2350 | // WebContents' stream monitor. Aggregate these signals to get the actual |
| 2351 | // state. |
Jeremy Roman | 03ce13ce | 2020-05-29 22:16:57 | [diff] [blame] | 2352 | // |
| 2353 | // Note that guests may not be attached as inner contents, and so may need to |
Peter Kasting | d568594 | 2022-09-02 17:52:17 | [diff] [blame] | 2354 | // be checked separately. This intentionally does not do a recursive search, |
| 2355 | // since the state is aggregated in each inner WebContents and reflects that |
| 2356 | // whole subtree. |
Chris Hamilton | df0d72cd | 2018-05-29 16:23:53 | [diff] [blame] | 2357 | bool is_currently_audible = |
| 2358 | audio_stream_monitor_.IsCurrentlyAudible() || |
| 2359 | (browser_plugin_embedder_ && |
Jeremy Roman | 03ce13ce | 2020-05-29 22:16:57 | [diff] [blame] | 2360 | browser_plugin_embedder_->AreAnyGuestsCurrentlyAudible()) || |
Peter Kasting | d568594 | 2022-09-02 17:52:17 | [diff] [blame] | 2361 | base::ranges::any_of(GetInnerWebContents(), |
| 2362 | [](WebContents* inner_contents) { |
| 2363 | return inner_contents->IsCurrentlyAudible(); |
| 2364 | }); |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 2365 | OPTIONAL_TRACE_EVENT2("content", "WebContentsImpl::OnAudioStateChanged", |
| 2366 | "is_currently_audible", is_currently_audible, |
| 2367 | "was_audible", is_currently_audible_); |
Chris Hamilton | df0d72cd | 2018-05-29 16:23:53 | [diff] [blame] | 2368 | if (is_currently_audible == is_currently_audible_) |
| 2369 | return; |
| 2370 | |
| 2371 | // Update internal state. |
| 2372 | is_currently_audible_ = is_currently_audible; |
| 2373 | was_ever_audible_ = was_ever_audible_ || is_currently_audible_; |
| 2374 | |
Eric Seckler | ab20ea2 | 2021-02-08 11:39:26 | [diff] [blame] | 2375 | audible_power_mode_voter_->VoteFor(is_currently_audible_ |
| 2376 | ? power_scheduler::PowerMode::kAudible |
| 2377 | : power_scheduler::PowerMode::kIdle); |
| 2378 | |
Gyuyoung Kim | 877e86d | 2020-07-21 04:01:02 | [diff] [blame] | 2379 | ExecutePageBroadcastMethod(base::BindRepeating( |
| 2380 | [](bool is_currently_audible, RenderViewHostImpl* rvh) { |
| 2381 | if (auto& broadcast = rvh->GetAssociatedPageBroadcast()) |
| 2382 | broadcast->AudioStateChanged(is_currently_audible); |
| 2383 | }, |
| 2384 | is_currently_audible_)); |
altimin | d8bd26c | 2016-11-04 11:44:54 | [diff] [blame] | 2385 | |
altimin | ec87fd1f | 2016-11-17 20:22:49 | [diff] [blame] | 2386 | // Notification for UI updates in response to the changed audio state. |
Collin Baker | af540cf | 2019-09-20 20:54:16 | [diff] [blame] | 2387 | NotifyNavigationStateChanged(INVALIDATE_TYPE_AUDIO); |
Jan Rucka | 442c83f | 2017-08-08 13:27:54 | [diff] [blame] | 2388 | |
Chris Hamilton | df0d72cd | 2018-05-29 16:23:53 | [diff] [blame] | 2389 | // Ensure that audio state changes propagate from innermost to outermost |
| 2390 | // WebContents. |
| 2391 | if (GetOuterWebContents()) |
| 2392 | GetOuterWebContents()->OnAudioStateChanged(); |
Tommy Steimel | 1836051 | 2017-11-01 00:38:19 | [diff] [blame] | 2393 | |
Andrew Grieve | e19cd93e | 2021-01-22 05:43:06 | [diff] [blame] | 2394 | observers_.NotifyObservers(&WebContentsObserver::OnAudioStateChanged, |
| 2395 | is_currently_audible_); |
altimin | d8bd26c | 2016-11-04 11:44:54 | [diff] [blame] | 2396 | } |
| 2397 | |
Lucas Furukawa Gadani | e1c5dfda | 2018-11-29 17:57:41 | [diff] [blame] | 2398 | base::TimeTicks WebContentsImpl::GetLastActiveTime() { |
[email protected] | 9a89045 | 2014-02-13 22:21:02 | [diff] [blame] | 2399 | return last_active_time_; |
[email protected] | 3e32414 | 2012-06-25 18:26:33 | [diff] [blame] | 2400 | } |
| 2401 | |
[email protected] | 9e2e463 | 2012-07-27 16:38:41 | [diff] [blame] | 2402 | void WebContentsImpl::WasShown() { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 2403 | TRACE_EVENT0("content", "WebContentsImpl::WasShown"); |
Collin Baker | 989e088 | 2019-11-01 01:27:17 | [diff] [blame] | 2404 | UpdateVisibilityAndNotifyPageAndView(Visibility::VISIBLE); |
[email protected] | be1f56ab | 2011-12-22 06:55:31 | [diff] [blame] | 2405 | } |
| 2406 | |
[email protected] | b172aee | 2012-04-10 17:05:26 | [diff] [blame] | 2407 | void WebContentsImpl::WasHidden() { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 2408 | TRACE_EVENT0("content", "WebContentsImpl::WasHidden"); |
Collin Baker | 989e088 | 2019-11-01 01:27:17 | [diff] [blame] | 2409 | UpdateVisibilityAndNotifyPageAndView(Visibility::HIDDEN); |
[email protected] | 375fa1b | 2012-05-22 22:05:37 | [diff] [blame] | 2410 | } |
| 2411 | |
Evan Stade | 6decc97 | 2022-10-24 19:42:18 | [diff] [blame] | 2412 | bool WebContentsImpl::HasRecentInteraction() { |
| 2413 | if (last_interaction_time_.is_null()) |
| 2414 | return false; |
| 2415 | |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 2416 | static constexpr base::TimeDelta kMaxInterval = base::Seconds(5); |
Evan Stade | 6decc97 | 2022-10-24 19:42:18 | [diff] [blame] | 2417 | base::TimeDelta delta = ui::EventTimeForNow() - last_interaction_time_; |
Daniel Cheng | 90196c8 | 2018-04-25 21:49:14 | [diff] [blame] | 2418 | // Note: the expectation is that the caller is typically expecting an input |
| 2419 | // event, e.g. validating that a WebUI message that requires a gesture is |
Daniel Cheng | e81030b3 | 2019-07-17 20:20:23 | [diff] [blame] | 2420 | // actually attached to a gesture. |
Daniel Cheng | 90196c8 | 2018-04-25 21:49:14 | [diff] [blame] | 2421 | return delta <= kMaxInterval; |
| 2422 | } |
| 2423 | |
Avi Drissman | 738ea19 | 2018-08-29 20:24:16 | [diff] [blame] | 2424 | void WebContentsImpl::SetIgnoreInputEvents(bool ignore_input_events) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 2425 | OPTIONAL_TRACE_EVENT1("content", "WebContentsImpl::SetIgnoreInputEvents", |
| 2426 | "ignore_input_events", ignore_input_events); |
Avi Drissman | 738ea19 | 2018-08-29 20:24:16 | [diff] [blame] | 2427 | ignore_input_events_ = ignore_input_events; |
| 2428 | } |
| 2429 | |
Sebastien Marchand | c38e5ae | 2021-02-18 20:28:13 | [diff] [blame] | 2430 | bool WebContentsImpl::HasActiveEffectivelyFullscreenVideo() { |
| 2431 | return IsFullscreen() && |
| 2432 | media_web_contents_observer_->HasActiveEffectivelyFullscreenVideo(); |
| 2433 | } |
| 2434 | |
Alexander Timin | bebb200 | 2021-04-20 15:42:24 | [diff] [blame] | 2435 | void WebContentsImpl::WriteIntoTrace(perfetto::TracedValue context) { |
Alexander Timin | e9f413e8 | 2021-03-12 14:06:44 | [diff] [blame] | 2436 | auto dict = std::move(context).WriteDictionary(); |
Carlos Caballero | b65b6e3a | 2021-11-15 10:09:00 | [diff] [blame] | 2437 | dict.Add("root_frame_tree_node_id", |
| 2438 | primary_frame_tree_.root()->frame_tree_node_id()); |
Alexander Timin | e9f413e8 | 2021-03-12 14:06:44 | [diff] [blame] | 2439 | } |
| 2440 | |
Lukasz Anforowicz | 2c1573a | 2021-09-21 18:58:18 | [diff] [blame] | 2441 | const base::Location& WebContentsImpl::GetCreatorLocation() { |
| 2442 | return creator_location_; |
| 2443 | } |
| 2444 | |
Tommy Steimel | 57eafde | 2023-01-27 17:33:24 | [diff] [blame] | 2445 | const absl::optional<blink::mojom::PictureInPictureWindowOptions>& |
| 2446 | WebContentsImpl::GetPictureInPictureOptions() const { |
| 2447 | return picture_in_picture_options_; |
Tommy Steimel | 991916c4 | 2022-06-24 20:59:57 | [diff] [blame] | 2448 | } |
| 2449 | |
Xiaohan Wang | 24ec934 | 2022-01-15 17:34:22 | [diff] [blame] | 2450 | #if BUILDFLAG(IS_ANDROID) |
Khushal | b8aabcf4 | 2021-06-22 11:55:03 | [diff] [blame] | 2451 | void WebContentsImpl::SetPrimaryMainFrameImportance( |
Bo Liu | 16dd7b832 | 2018-05-02 21:19:50 | [diff] [blame] | 2452 | ChildProcessImportance importance) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 2453 | OPTIONAL_TRACE_EVENT1("content", "WebContentsImpl::SetMainFrameImportance", |
| 2454 | "importance", static_cast<int>(importance)); |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 2455 | GetPrimaryMainFrame()->GetRenderWidgetHost()->SetImportance(importance); |
Bo Liu | 7c6779e9 | 2017-08-16 02:02:28 | [diff] [blame] | 2456 | } |
Bo Liu | 168c864 | 2017-08-28 18:26:02 | [diff] [blame] | 2457 | #endif |
Bo Liu | 7c6779e9 | 2017-08-16 02:02:28 | [diff] [blame] | 2458 | |
ccameron | 8807c38f | 2015-01-17 00:29:19 | [diff] [blame] | 2459 | void WebContentsImpl::WasOccluded() { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 2460 | TRACE_EVENT0("content", "WebContentsImpl::WasOccluded"); |
Collin Baker | 989e088 | 2019-11-01 01:27:17 | [diff] [blame] | 2461 | UpdateVisibilityAndNotifyPageAndView(Visibility::OCCLUDED); |
ccameron | 8807c38f | 2015-01-17 00:29:19 | [diff] [blame] | 2462 | } |
| 2463 | |
Lucas Furukawa Gadani | e1c5dfda | 2018-11-29 17:57:41 | [diff] [blame] | 2464 | Visibility WebContentsImpl::GetVisibility() { |
Francois Doray | e616115 | 2018-03-27 22:05:37 | [diff] [blame] | 2465 | return visibility_; |
Francois Doray | fe4a177 | 2018-02-17 04:17:09 | [diff] [blame] | 2466 | } |
| 2467 | |
Rakina Zata Amni | 26e8d8a | 2020-08-05 06:06:06 | [diff] [blame] | 2468 | bool WebContentsImpl::NeedToFireBeforeUnloadOrUnloadEvents() { |
Alexander Timin | c7bee32 | 2020-05-19 17:54:34 | [diff] [blame] | 2469 | if (!notify_disconnection_) |
Alex Moshchuk | 6fcaca75 | 2018-07-14 02:13:59 | [diff] [blame] | 2470 | return false; |
| 2471 | |
Alex Moshchuk | e558aba | 2023-01-13 07:42:02 | [diff] [blame] | 2472 | // Don't fire if the main frame indicates that beforeunload and unload have |
| 2473 | // already executed (e.g., after receiving a ClosePage ACK) or should be |
| 2474 | // ignored. |
| 2475 | if (GetPrimaryMainFrame()->IsPageReadyToBeClosed()) { |
Alex Moshchuk | 6fcaca75 | 2018-07-14 02:13:59 | [diff] [blame] | 2476 | return false; |
Alex Moshchuk | e558aba | 2023-01-13 07:42:02 | [diff] [blame] | 2477 | } |
Alex Moshchuk | 6fcaca75 | 2018-07-14 02:13:59 | [diff] [blame] | 2478 | |
Alex Moshchuk | c78dbc72 | 2019-10-31 20:00:50 | [diff] [blame] | 2479 | // Check whether any frame in the frame tree needs to run beforeunload or |
Rakina Zata Amni | 26e8d8a | 2020-08-05 06:06:06 | [diff] [blame] | 2480 | // unload-time event handlers. |
Carlos Caballero | b65b6e3a | 2021-11-15 10:09:00 | [diff] [blame] | 2481 | for (FrameTreeNode* node : primary_frame_tree_.Nodes()) { |
Alex Moshchuk | c78dbc72 | 2019-10-31 20:00:50 | [diff] [blame] | 2482 | RenderFrameHostImpl* rfh = node->current_frame_host(); |
| 2483 | |
Rakina Zata Amni | 26e8d8a | 2020-08-05 06:06:06 | [diff] [blame] | 2484 | // No need to run beforeunload/unload-time events if the RenderFrame isn't |
| 2485 | // live. |
Alex Moshchuk | c78dbc72 | 2019-10-31 20:00:50 | [diff] [blame] | 2486 | if (!rfh->IsRenderFrameLive()) |
| 2487 | continue; |
Rakina Zata Amni | 26e8d8a | 2020-08-05 06:06:06 | [diff] [blame] | 2488 | bool should_run_before_unload_handler = |
Dave Tapuska | 97d22ab | 2019-12-03 17:56:36 | [diff] [blame] | 2489 | rfh->GetSuddenTerminationDisablerState( |
Rakina Zata Amni | 26e8d8a | 2020-08-05 06:06:06 | [diff] [blame] | 2490 | blink::mojom::SuddenTerminationDisablerType::kBeforeUnloadHandler); |
| 2491 | bool should_run_unload_handler = rfh->GetSuddenTerminationDisablerState( |
| 2492 | blink::mojom::SuddenTerminationDisablerType::kUnloadHandler); |
| 2493 | bool should_run_page_hide_handler = rfh->GetSuddenTerminationDisablerState( |
| 2494 | blink::mojom::SuddenTerminationDisablerType::kPageHideHandler); |
| 2495 | auto* rvh = static_cast<RenderViewHostImpl*>(rfh->GetRenderViewHost()); |
| 2496 | // If the tab is already hidden, we should not run visibilitychange |
| 2497 | // handlers. |
| 2498 | bool is_page_visible = rvh->GetPageLifecycleStateManager() |
| 2499 | ->CalculatePageLifecycleState() |
| 2500 | ->visibility == PageVisibilityState::kVisible; |
| 2501 | |
| 2502 | bool should_run_visibility_change_handler = |
| 2503 | is_page_visible && rfh->GetSuddenTerminationDisablerState( |
| 2504 | blink::mojom::SuddenTerminationDisablerType:: |
| 2505 | kVisibilityChangeHandler); |
| 2506 | if (should_run_before_unload_handler || should_run_unload_handler || |
| 2507 | should_run_page_hide_handler || should_run_visibility_change_handler) { |
Alex Moshchuk | c78dbc72 | 2019-10-31 20:00:50 | [diff] [blame] | 2508 | return true; |
| 2509 | } |
Aditya Keerthi | 34f37e6 | 2019-03-08 15:54:53 | [diff] [blame] | 2510 | } |
Alex Moshchuk | 6fcaca75 | 2018-07-14 02:13:59 | [diff] [blame] | 2511 | |
Alex Moshchuk | c78dbc72 | 2019-10-31 20:00:50 | [diff] [blame] | 2512 | return false; |
[email protected] | be1f56ab | 2011-12-22 06:55:31 | [diff] [blame] | 2513 | } |
| 2514 | |
Chris Hamilton | 3b3e315d | 2018-09-19 13:16:21 | [diff] [blame] | 2515 | void WebContentsImpl::DispatchBeforeUnload(bool auto_cancel) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 2516 | OPTIONAL_TRACE_EVENT1("content", "WebContentsImpl::DispatchBeforeUnload", |
| 2517 | "auto_cancel", auto_cancel); |
Chris Hamilton | 3b3e315d | 2018-09-19 13:16:21 | [diff] [blame] | 2518 | auto before_unload_type = |
| 2519 | auto_cancel ? RenderFrameHostImpl::BeforeUnloadType::DISCARD |
| 2520 | : RenderFrameHostImpl::BeforeUnloadType::TAB_CLOSE; |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 2521 | GetPrimaryMainFrame()->DispatchBeforeUnload(before_unload_type, false); |
[email protected] | 1c3f80bd | 2014-04-08 23:02:06 | [diff] [blame] | 2522 | } |
| 2523 | |
W. James MacLean | 2539adb3 | 2019-12-13 00:40:44 | [diff] [blame] | 2524 | bool WebContentsImpl::IsInnerWebContentsForGuest() { |
Charlie Reis | 3d18960 | 2021-04-27 21:24:47 | [diff] [blame] | 2525 | return IsGuest(); |
W. James MacLean | 2539adb3 | 2019-12-13 00:40:44 | [diff] [blame] | 2526 | } |
| 2527 | |
Lucas Furukawa Gadani | 6e5b4f3 | 2019-03-02 04:18:50 | [diff] [blame] | 2528 | void WebContentsImpl::AttachInnerWebContents( |
| 2529 | std::unique_ptr<WebContents> inner_web_contents, |
W. James MacLean | 6288398 | 2019-11-12 20:39:00 | [diff] [blame] | 2530 | RenderFrameHost* render_frame_host, |
Dave Tapuska | 82b5401 | 2022-07-15 23:26:10 | [diff] [blame] | 2531 | mojo::PendingAssociatedRemote<blink::mojom::RemoteFrame> remote_frame, |
| 2532 | mojo::PendingAssociatedReceiver<blink::mojom::RemoteFrameHost> |
| 2533 | remote_frame_host_receiver, |
W. James MacLean | 6288398 | 2019-11-12 20:39:00 | [diff] [blame] | 2534 | bool is_full_page) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 2535 | OPTIONAL_TRACE_EVENT2("content", "WebContentsImpl::AttachInnerWebContents", |
Alexander Timin | fb70a2d | 2021-02-08 03:19:07 | [diff] [blame] | 2536 | "inner_web_contents", |
| 2537 | static_cast<void*>(inner_web_contents.get()), |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 2538 | "is_full_page", is_full_page); |
Lucas Furukawa Gadani | 6e5b4f3 | 2019-03-02 04:18:50 | [diff] [blame] | 2539 | WebContentsImpl* inner_web_contents_impl = |
| 2540 | static_cast<WebContentsImpl*>(inner_web_contents.get()); |
| 2541 | DCHECK(!inner_web_contents_impl->node_.outer_web_contents()); |
| 2542 | auto* render_frame_host_impl = |
| 2543 | static_cast<RenderFrameHostImpl*>(render_frame_host); |
Matt Falkenhagen | 7b71b9f1 | 2021-08-12 01:32:43 | [diff] [blame] | 2544 | DCHECK_EQ(this, WebContents::FromRenderFrameHost(render_frame_host_impl)); |
Kevin McNee | 490c04be | 2021-06-04 22:52:55 | [diff] [blame] | 2545 | DCHECK(render_frame_host_impl->GetParent()); |
Lucas Furukawa Gadani | 105de1e81 | 2018-05-31 19:38:39 | [diff] [blame] | 2546 | |
Lucas Furukawa Gadani | 6e5b4f3 | 2019-03-02 04:18:50 | [diff] [blame] | 2547 | RenderFrameHostManager* inner_render_manager = |
| 2548 | inner_web_contents_impl->GetRenderManager(); |
| 2549 | RenderFrameHostImpl* inner_main_frame = |
| 2550 | inner_render_manager->current_frame_host(); |
| 2551 | RenderViewHostImpl* inner_render_view_host = |
Alex Moshchuk | 2e470ea | 2021-02-03 06:46:34 | [diff] [blame] | 2552 | inner_main_frame->render_view_host(); |
Lucas Furukawa Gadani | 6e5b4f3 | 2019-03-02 04:18:50 | [diff] [blame] | 2553 | auto* outer_render_manager = |
| 2554 | render_frame_host_impl->frame_tree_node()->render_manager(); |
lfg | 91a79e1 | 2016-04-01 23:52:19 | [diff] [blame] | 2555 | |
Dominic Farolino | 377edb30 | 2021-07-29 05:57:18 | [diff] [blame] | 2556 | // Make |render_frame_host_impl| an outer delegate frame. |
| 2557 | render_frame_host_impl->set_inner_tree_main_frame_tree_node_id( |
| 2558 | inner_main_frame->frame_tree_node()->frame_tree_node_id()); |
| 2559 | |
Lucas Furukawa Gadani | 105de1e81 | 2018-05-31 19:38:39 | [diff] [blame] | 2560 | // When attaching a WebContents as an inner WebContents, we need to replace |
| 2561 | // the Webcontents' view with a WebContentsViewChildFrame. |
Fergal Daly | 55b6d72 | 2020-09-11 07:56:33 | [diff] [blame] | 2562 | inner_web_contents_impl->view_ = std::make_unique<WebContentsViewChildFrame>( |
Lucas Furukawa Gadani | 6e5b4f3 | 2019-03-02 04:18:50 | [diff] [blame] | 2563 | inner_web_contents_impl, |
| 2564 | GetContentClient()->browser()->GetWebContentsViewDelegate( |
| 2565 | inner_web_contents_impl), |
Fergal Daly | 55b6d72 | 2020-09-11 07:56:33 | [diff] [blame] | 2566 | &inner_web_contents_impl->render_view_host_delegate_view_); |
Kevin McNee | 490c04be | 2021-06-04 22:52:55 | [diff] [blame] | 2567 | // On platforms where destroying the WebContents' view does not also destroy |
| 2568 | // the platform RenderWidgetHostView, we need to destroy it if it exists. |
| 2569 | // TODO(mcnee): Should all platforms' WebContentsView destroy the platform |
| 2570 | // RWHV? |
| 2571 | if (RenderWidgetHostViewBase* prev_rwhv = |
| 2572 | inner_render_manager->GetRenderWidgetHostView()) { |
| 2573 | if (!prev_rwhv->IsRenderWidgetHostViewChildFrame()) { |
| 2574 | prev_rwhv->Destroy(); |
| 2575 | } |
| 2576 | } |
Lucas Furukawa Gadani | 105de1e81 | 2018-05-31 19:38:39 | [diff] [blame] | 2577 | |
lfg | 91a79e1 | 2016-04-01 23:52:19 | [diff] [blame] | 2578 | // When the WebContents being initialized has an opener, the browser side |
| 2579 | // Render{View,Frame}Host must be initialized and the RenderWidgetHostView |
| 2580 | // created. This is needed because the usual initialization happens during |
| 2581 | // the first navigation, but when attaching a new window we don't navigate |
| 2582 | // before attaching. If the browser side is already initialized, the calls |
| 2583 | // below will just early return. |
Sharon Yang | 65e78f2d | 2022-02-24 19:58:36 | [diff] [blame] | 2584 | inner_render_manager->InitRenderView( |
| 2585 | inner_main_frame->GetSiteInstance()->group(), inner_render_view_host, |
| 2586 | nullptr); |
Lucas Furukawa Gadani | 6e5b4f3 | 2019-03-02 04:18:50 | [diff] [blame] | 2587 | if (!inner_render_manager->GetRenderWidgetHostView()) { |
| 2588 | inner_web_contents_impl->CreateRenderWidgetHostViewForRenderManager( |
| 2589 | inner_render_view_host); |
| 2590 | } |
lfg | 91a79e1 | 2016-04-01 23:52:19 | [diff] [blame] | 2591 | |
K. Moon | 97def7d | 2022-02-01 15:56:02 | [diff] [blame] | 2592 | inner_web_contents_impl->RecursivelyUnregisterRenderWidgetHostViews(); |
Adithya Srinivasan | e6e7f84 | 2019-12-16 18:41:36 | [diff] [blame] | 2593 | |
lazyboy | 6ec48b2a | 2015-06-29 15:18:14 | [diff] [blame] | 2594 | // Create a link to our outer WebContents. |
Lucas Furukawa Gadani | 6e5b4f3 | 2019-03-02 04:18:50 | [diff] [blame] | 2595 | node_.AttachInnerWebContents(std::move(inner_web_contents), |
| 2596 | render_frame_host_impl); |
lazyboy | 6ec48b2a | 2015-06-29 15:18:14 | [diff] [blame] | 2597 | |
lazyboy | 6ec48b2a | 2015-06-29 15:18:14 | [diff] [blame] | 2598 | // Create a proxy in top-level RenderFrameHostManager, pointing to the |
| 2599 | // SiteInstance of the outer WebContents. The proxy will be used to send |
| 2600 | // postMessage to the inner WebContents. |
Harkiran Bolaria | e45272d | 2022-04-12 08:05:01 | [diff] [blame] | 2601 | auto* proxy = |
| 2602 | inner_main_frame->browsing_context_state()->CreateOuterDelegateProxy( |
| 2603 | render_frame_host_impl->GetSiteInstance(), |
Dave Tapuska | 14457010 | 2022-08-02 19:28:27 | [diff] [blame] | 2604 | inner_main_frame->frame_tree_node(), blink::RemoteFrameToken()); |
Dave Tapuska | 82b5401 | 2022-07-15 23:26:10 | [diff] [blame] | 2605 | if (remote_frame && remote_frame_host_receiver) { |
| 2606 | proxy->BindRemoteFrameInterfaces(std::move(remote_frame), |
| 2607 | std::move(remote_frame_host_receiver)); |
| 2608 | } |
lazyboy | 6ec48b2a | 2015-06-29 15:18:14 | [diff] [blame] | 2609 | |
Lucas Furukawa Gadani | 9912582 | 2019-01-03 15:41:49 | [diff] [blame] | 2610 | // When attaching a GuestView as an inner WebContents, there should already be |
| 2611 | // a live RenderFrame, which has to be swapped. When attaching a portal, there |
| 2612 | // will not be a live RenderFrame before creating the proxy. |
Lucas Furukawa Gadani | 6e5b4f3 | 2019-03-02 04:18:50 | [diff] [blame] | 2613 | if (render_frame_host_impl->IsRenderFrameLive()) { |
| 2614 | inner_render_manager->SwapOuterDelegateFrame(render_frame_host_impl, proxy); |
Lucas Furukawa Gadani | 9912582 | 2019-01-03 15:41:49 | [diff] [blame] | 2615 | |
Lucas Furukawa Gadani | 6e5b4f3 | 2019-03-02 04:18:50 | [diff] [blame] | 2616 | inner_web_contents_impl->ReattachToOuterWebContentsFrame(); |
Lucas Furukawa Gadani | 9912582 | 2019-01-03 15:41:49 | [diff] [blame] | 2617 | } |
ekaramad | add88229 | 2016-06-08 15:22:56 | [diff] [blame] | 2618 | |
Carlos Caballero | b65b6e3a | 2021-11-15 10:09:00 | [diff] [blame] | 2619 | if (primary_frame_tree_.GetFocusedFrame() == |
Lucas Furukawa Gadani | 6e5b4f3 | 2019-03-02 04:18:50 | [diff] [blame] | 2620 | render_frame_host_impl->frame_tree_node()) { |
| 2621 | inner_web_contents_impl->SetFocusedFrame( |
Carlos Caballero | b65b6e3a | 2021-11-15 10:09:00 | [diff] [blame] | 2622 | inner_web_contents_impl->primary_frame_tree_.root(), |
Sharon Yang | efe5263 | 2022-03-08 23:06:06 | [diff] [blame] | 2623 | render_frame_host_impl->GetSiteInstance()->group()); |
avallee | 7529b2a | 2017-04-26 11:37:51 | [diff] [blame] | 2624 | } |
Lucas Furukawa Gadani | 6e5b4f3 | 2019-03-02 04:18:50 | [diff] [blame] | 2625 | outer_render_manager->set_attach_complete(); |
W. James MacLean | 6288398 | 2019-11-12 20:39:00 | [diff] [blame] | 2626 | |
| 2627 | // If the inner WebContents is full frame, give it focus. |
| 2628 | if (is_full_page) { |
| 2629 | // There should only ever be one inner WebContents when |is_full_page| is |
| 2630 | // true, and it is the one we just attached. |
| 2631 | DCHECK_EQ(1u, node_.GetInnerWebContents().size()); |
| 2632 | inner_web_contents_impl->SetAsFocusedWebContentsIfNecessary(); |
| 2633 | } |
Chris Hamilton | 16b5d1d | 2020-06-04 00:08:12 | [diff] [blame] | 2634 | |
Andrew Grieve | e19cd93e | 2021-01-22 05:43:06 | [diff] [blame] | 2635 | observers_.NotifyObservers(&WebContentsObserver::InnerWebContentsAttached, |
| 2636 | inner_web_contents_impl, render_frame_host, |
| 2637 | is_full_page); |
Mario Sanchez Prada | 989aeb7a | 2020-10-13 09:42:56 | [diff] [blame] | 2638 | |
| 2639 | // Make sure that the inner web contents and its outer delegate get properly |
| 2640 | // linked via the embedding token now that inner web contents are attached. |
| 2641 | inner_main_frame->PropagateEmbeddingTokenToParentFrame(); |
lazyboy | 6ec48b2a | 2015-06-29 15:18:14 | [diff] [blame] | 2642 | } |
| 2643 | |
Lucas Furukawa Gadani | 9912582 | 2019-01-03 15:41:49 | [diff] [blame] | 2644 | std::unique_ptr<WebContents> WebContentsImpl::DetachFromOuterWebContents() { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 2645 | OPTIONAL_TRACE_EVENT0("content", |
| 2646 | "WebContentsImpl::DetachFromOuterWebContents"); |
Chris Hamilton | 16b5d1d | 2020-06-04 00:08:12 | [diff] [blame] | 2647 | auto* outer_web_contents = GetOuterWebContents(); |
| 2648 | DCHECK(outer_web_contents); |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 2649 | GetPrimaryMainFrame() |
Dave Tapuska | 5dd5f982 | 2021-09-10 20:57:15 | [diff] [blame] | 2650 | ->GetParentOrOuterDocumentOrEmbedder() |
Dominic Farolino | 377edb30 | 2021-07-29 05:57:18 | [diff] [blame] | 2651 | ->set_inner_tree_main_frame_tree_node_id( |
| 2652 | FrameTreeNode::kFrameTreeNodeInvalidId); |
Chris Hamilton | 16b5d1d | 2020-06-04 00:08:12 | [diff] [blame] | 2653 | |
K. Moon | 97def7d | 2022-02-01 15:56:02 | [diff] [blame] | 2654 | RecursivelyUnregisterRenderWidgetHostViews(); |
Lucas Gadani | d2995258 | 2020-06-09 21:24:00 | [diff] [blame] | 2655 | |
| 2656 | // Each RenderViewHost has a RenderWidgetHost which can have a |
| 2657 | // RenderWidgetHostView, and it needs to be re-created with the appropriate |
| 2658 | // platform view. It is important to re-create all child views, not only the |
| 2659 | // current one, since the view can be swapped due to a cross-origin |
| 2660 | // navigation. |
| 2661 | std::set<RenderViewHostImpl*> render_view_hosts; |
Sharon Yang | ed88454 | 2023-02-02 17:33:44 | [diff] [blame] | 2662 | primary_frame_tree_.ForEachRenderViewHost([&render_view_hosts]( |
| 2663 | RenderViewHostImpl* rvh) { |
| 2664 | if (rvh->GetWidget() && rvh->GetWidget()->GetView()) { |
| 2665 | DCHECK(rvh->GetWidget()->GetView()->IsRenderWidgetHostViewChildFrame()); |
| 2666 | render_view_hosts.insert(rvh); |
Kevin McNee | 6471a70 | 2020-01-27 18:23:07 | [diff] [blame] | 2667 | } |
Sharon Yang | ed88454 | 2023-02-02 17:33:44 | [diff] [blame] | 2668 | }); |
Lucas Gadani | d2995258 | 2020-06-09 21:24:00 | [diff] [blame] | 2669 | |
| 2670 | for (auto* render_view_host : render_view_hosts) |
| 2671 | render_view_host->GetWidget()->GetView()->Destroy(); |
| 2672 | |
Harkiran Bolaria | e45272d | 2022-04-12 08:05:01 | [diff] [blame] | 2673 | GetRenderManager() |
| 2674 | ->current_frame_host() |
| 2675 | ->browsing_context_state() |
| 2676 | ->DeleteOuterDelegateProxy(node_.OuterContentsFrameTreeNode() |
| 2677 | ->current_frame_host() |
| 2678 | ->GetSiteInstance() |
| 2679 | ->group()); |
Avi Drissman | 0d0908ded | 2022-04-28 05:39:39 | [diff] [blame] | 2680 | view_ = CreateWebContentsView( |
Lucas Furukawa Gadani | 9912582 | 2019-01-03 15:41:49 | [diff] [blame] | 2681 | this, GetContentClient()->browser()->GetWebContentsViewDelegate(this), |
Avi Drissman | 0d0908ded | 2022-04-28 05:39:39 | [diff] [blame] | 2682 | &render_view_host_delegate_view_); |
danakj | fc518493 | 2019-09-12 18:08:32 | [diff] [blame] | 2683 | view_->CreateView(nullptr); |
Lucas Furukawa Gadani | 9912582 | 2019-01-03 15:41:49 | [diff] [blame] | 2684 | std::unique_ptr<WebContents> web_contents = |
| 2685 | node_.DisconnectFromOuterWebContents(); |
| 2686 | DCHECK_EQ(web_contents.get(), this); |
Carlos Caballero | 6a99dac | 2021-11-03 10:41:17 | [diff] [blame] | 2687 | node_.SetFocusedFrameTree(&GetPrimaryFrameTree()); |
Lucas Gadani | d2995258 | 2020-06-09 21:24:00 | [diff] [blame] | 2688 | |
| 2689 | for (auto* render_view_host : render_view_hosts) |
| 2690 | CreateRenderWidgetHostViewForRenderManager(render_view_host); |
| 2691 | |
K. Moon | 97def7d | 2022-02-01 15:56:02 | [diff] [blame] | 2692 | RecursivelyRegisterRenderWidgetHostViews(); |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 2693 | GetPrimaryMainFrame()->UpdateAXTreeData(); |
Chris Hamilton | 16b5d1d | 2020-06-04 00:08:12 | [diff] [blame] | 2694 | |
| 2695 | // Invoke on the *outer* web contents observers for symmetry. |
Andrew Grieve | e19cd93e | 2021-01-22 05:43:06 | [diff] [blame] | 2696 | outer_web_contents->observers_.NotifyObservers( |
| 2697 | &WebContentsObserver::InnerWebContentsDetached, this); |
Chris Hamilton | 16b5d1d | 2020-06-04 00:08:12 | [diff] [blame] | 2698 | |
Lucas Furukawa Gadani | 9912582 | 2019-01-03 15:41:49 | [diff] [blame] | 2699 | return web_contents; |
| 2700 | } |
| 2701 | |
K. Moon | 97def7d | 2022-02-01 15:56:02 | [diff] [blame] | 2702 | void WebContentsImpl::RecursivelyRegisterRenderWidgetHostViews() { |
| 2703 | OPTIONAL_TRACE_EVENT0( |
| 2704 | "content", "WebContentsImpl::RecursivelyRegisterRenderWidgetHostViews"); |
| 2705 | |
| 2706 | auto* text_input_manager = GetTextInputManager(); |
Adithya Srinivasan | e6e7f84 | 2019-12-16 18:41:36 | [diff] [blame] | 2707 | for (auto* view : GetRenderWidgetHostViewsInWebContentsTree()) { |
K. Moon | 97def7d | 2022-02-01 15:56:02 | [diff] [blame] | 2708 | if (text_input_manager) { |
| 2709 | // Use RenderWidgetHostView::GetTextInputManager() for its side effects, |
| 2710 | // rather than registering the view directly; the view caches the |
| 2711 | // TextInputManager. |
| 2712 | // |
| 2713 | // TODO(crbug.com/1292036): This probably could be made more symmetrical |
| 2714 | // with unregistration. Getting rid of lazy registration might also allow |
| 2715 | // eliminating some special cases in TextInputManager. |
| 2716 | auto* text_input_manager_for_view = view->GetTextInputManager(); |
| 2717 | DCHECK_EQ(text_input_manager, text_input_manager_for_view); |
| 2718 | } |
| 2719 | |
Kevin McNee | ef1e936 | 2021-06-08 21:31:52 | [diff] [blame] | 2720 | if (view->IsRenderWidgetHostViewChildFrame()) |
Adithya Srinivasan | e6e7f84 | 2019-12-16 18:41:36 | [diff] [blame] | 2721 | static_cast<RenderWidgetHostViewChildFrame*>(view)->RegisterFrameSinkId(); |
| 2722 | } |
| 2723 | } |
W. James MacLean | 2dc75ed4 | 2019-03-06 14:31:09 | [diff] [blame] | 2724 | |
K. Moon | 97def7d | 2022-02-01 15:56:02 | [diff] [blame] | 2725 | void WebContentsImpl::RecursivelyUnregisterRenderWidgetHostViews() { |
| 2726 | OPTIONAL_TRACE_EVENT0( |
| 2727 | "content", "WebContentsImpl::RecursivelyUnregisterRenderWidgetHostViews"); |
| 2728 | |
| 2729 | auto* text_input_manager = GetTextInputManager(); |
Adithya Srinivasan | e6e7f84 | 2019-12-16 18:41:36 | [diff] [blame] | 2730 | for (auto* view : GetRenderWidgetHostViewsInWebContentsTree()) { |
K. Moon | 97def7d | 2022-02-01 15:56:02 | [diff] [blame] | 2731 | if (text_input_manager) { |
| 2732 | // The RenderWidgetHostView will drop the cached TextInputManager itself. |
| 2733 | text_input_manager->Unregister(view); |
| 2734 | } |
| 2735 | |
Kevin McNee | ef1e936 | 2021-06-08 21:31:52 | [diff] [blame] | 2736 | if (view->IsRenderWidgetHostViewChildFrame()) { |
Adithya Srinivasan | e6e7f84 | 2019-12-16 18:41:36 | [diff] [blame] | 2737 | static_cast<RenderWidgetHostViewChildFrame*>(view) |
| 2738 | ->UnregisterFrameSinkId(); |
| 2739 | } |
W. James MacLean | 2dc75ed4 | 2019-03-06 14:31:09 | [diff] [blame] | 2740 | } |
| 2741 | } |
| 2742 | |
Lucas Furukawa Gadani | aed1fed | 2017-10-13 17:34:14 | [diff] [blame] | 2743 | void WebContentsImpl::ReattachToOuterWebContentsFrame() { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 2744 | OPTIONAL_TRACE_EVENT0("content", |
| 2745 | "WebContentsImpl::ReattachToOuterWebContentsFrame"); |
Lucas Furukawa Gadani | aed1fed | 2017-10-13 17:34:14 | [diff] [blame] | 2746 | DCHECK(node_.outer_web_contents()); |
| 2747 | auto* render_manager = GetRenderManager(); |
Kevin McNee | 490c04be | 2021-06-04 22:52:55 | [diff] [blame] | 2748 | auto* child_rwhv = render_manager->GetRenderWidgetHostView(); |
| 2749 | DCHECK(child_rwhv); |
| 2750 | DCHECK(child_rwhv->IsRenderWidgetHostViewChildFrame()); |
Dave Tapuska | efc4936a | 2021-10-05 20:47:10 | [diff] [blame] | 2751 | render_manager->SetRWHViewForInnerFrameTree( |
Kevin McNee | 490c04be | 2021-06-04 22:52:55 | [diff] [blame] | 2752 | static_cast<RenderWidgetHostViewChildFrame*>(child_rwhv)); |
Lucas Furukawa Gadani | aed1fed | 2017-10-13 17:34:14 | [diff] [blame] | 2753 | |
K. Moon | 97def7d | 2022-02-01 15:56:02 | [diff] [blame] | 2754 | RecursivelyRegisterRenderWidgetHostViews(); |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 2755 | GetPrimaryMainFrame()->UpdateAXTreeData(); |
Lucas Furukawa Gadani | aed1fed | 2017-10-13 17:34:14 | [diff] [blame] | 2756 | } |
| 2757 | |
Lucas Gadani | 97298562 | 2020-05-28 20:52:51 | [diff] [blame] | 2758 | void WebContentsImpl::DidActivatePortal( |
| 2759 | WebContentsImpl* predecessor_web_contents, |
| 2760 | base::TimeTicks activation_time) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 2761 | TRACE_EVENT2("content", "WebContentsImpl::DidActivatePortal", "predecessor", |
Alexander Timin | fb70a2d | 2021-02-08 03:19:07 | [diff] [blame] | 2762 | static_cast<void*>(predecessor_web_contents), "activation_time", |
| 2763 | activation_time); |
Lucas Gadani | 97298562 | 2020-05-28 20:52:51 | [diff] [blame] | 2764 | DCHECK(predecessor_web_contents); |
Kevin McNee | f8eb64c | 2020-04-21 21:36:06 | [diff] [blame] | 2765 | NotifyInsidePortal(false); |
Andrew Grieve | e19cd93e | 2021-01-22 05:43:06 | [diff] [blame] | 2766 | observers_.NotifyObservers(&WebContentsObserver::DidActivatePortal, |
| 2767 | predecessor_web_contents, activation_time); |
Lucas Gadani | 97298562 | 2020-05-28 20:52:51 | [diff] [blame] | 2768 | GetDelegate()->WebContentsBecamePortal(predecessor_web_contents); |
Kevin McNee | f8eb64c | 2020-04-21 21:36:06 | [diff] [blame] | 2769 | } |
| 2770 | |
Adithya Srinivasan | 11af2c5 | 2019-12-16 22:52:55 | [diff] [blame] | 2771 | void WebContentsImpl::NotifyInsidePortal(bool inside_portal) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 2772 | OPTIONAL_TRACE_EVENT1("content", "WebContentsImpl::NotifyInsidePortal", |
| 2773 | "inside_portal", inside_portal); |
Gyuyoung Kim | 1c02971 | 2020-07-22 09:21:06 | [diff] [blame] | 2774 | ExecutePageBroadcastMethod(base::BindRepeating( |
| 2775 | [](bool inside_portal, RenderViewHostImpl* rvh) { |
| 2776 | if (auto& broadcast = rvh->GetAssociatedPageBroadcast()) |
| 2777 | broadcast->SetInsidePortal(inside_portal); |
| 2778 | }, |
| 2779 | inside_portal)); |
Adithya Srinivasan | 11af2c5 | 2019-12-16 22:52:55 | [diff] [blame] | 2780 | } |
| 2781 | |
dalecurtis | 6c58ed0 | 2016-10-28 23:02:37 | [diff] [blame] | 2782 | void WebContentsImpl::DidChangeVisibleSecurityState() { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 2783 | OPTIONAL_TRACE_EVENT0("content", |
| 2784 | "WebContentsImpl::DidChangeVisibleSecurityState"); |
Emily Stark | c663b024 | 2019-04-26 19:34:45 | [diff] [blame] | 2785 | if (delegate_) |
dalecurtis | 6c58ed0 | 2016-10-28 23:02:37 | [diff] [blame] | 2786 | delegate_->VisibleSecurityStateChanged(this); |
Clark DuVall | 22aaff3 | 2021-03-18 17:17:47 | [diff] [blame] | 2787 | |
| 2788 | SCOPED_UMA_HISTOGRAM_TIMER( |
| 2789 | "WebContentsObserver.DidChangeVisibleSecurityState"); |
Andrew Grieve | e19cd93e | 2021-01-22 05:43:06 | [diff] [blame] | 2790 | observers_.NotifyObservers( |
| 2791 | &WebContentsObserver::DidChangeVisibleSecurityState); |
dalecurtis | 6c58ed0 | 2016-10-28 23:02:37 | [diff] [blame] | 2792 | } |
| 2793 | |
Gyuyoung Kim | 1ac4ca78 | 2020-09-11 03:32:51 | [diff] [blame] | 2794 | const blink::web_pref::WebPreferences WebContentsImpl::ComputeWebPreferences() { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 2795 | OPTIONAL_TRACE_EVENT0("browser", "WebContentsImpl::ComputeWebPreferences"); |
| 2796 | |
Gyuyoung Kim | 1ac4ca78 | 2020-09-11 03:32:51 | [diff] [blame] | 2797 | blink::web_pref::WebPreferences prefs; |
Rakina Zata Amni | 4029b6d | 2020-07-28 02:36:20 | [diff] [blame] | 2798 | |
| 2799 | const base::CommandLine& command_line = |
| 2800 | *base::CommandLine::ForCurrentProcess(); |
| 2801 | |
| 2802 | SetSlowWebPreferences(command_line, &prefs); |
| 2803 | |
| 2804 | prefs.web_security_enabled = |
| 2805 | !command_line.HasSwitch(switches::kDisableWebSecurity); |
| 2806 | |
| 2807 | prefs.remote_fonts_enabled = |
| 2808 | !command_line.HasSwitch(switches::kDisableRemoteFonts); |
Rakina Zata Amni | 4029b6d | 2020-07-28 02:36:20 | [diff] [blame] | 2809 | prefs.local_storage_enabled = |
| 2810 | !command_line.HasSwitch(switches::kDisableLocalStorage); |
| 2811 | prefs.databases_enabled = |
| 2812 | !command_line.HasSwitch(switches::kDisableDatabases); |
| 2813 | |
| 2814 | prefs.webgl1_enabled = !command_line.HasSwitch(switches::kDisable3DAPIs) && |
| 2815 | !command_line.HasSwitch(switches::kDisableWebGL); |
| 2816 | prefs.webgl2_enabled = !command_line.HasSwitch(switches::kDisable3DAPIs) && |
| 2817 | !command_line.HasSwitch(switches::kDisableWebGL) && |
| 2818 | !command_line.HasSwitch(switches::kDisableWebGL2); |
| 2819 | |
| 2820 | prefs.pepper_3d_enabled = !command_line.HasSwitch(switches::kDisablePepper3d); |
| 2821 | |
Rakina Zata Amni | 4029b6d | 2020-07-28 02:36:20 | [diff] [blame] | 2822 | prefs.allow_file_access_from_file_urls = |
| 2823 | command_line.HasSwitch(switches::kAllowFileAccessFromFiles); |
| 2824 | |
| 2825 | prefs.accelerated_2d_canvas_enabled = |
| 2826 | !command_line.HasSwitch(switches::kDisableAccelerated2dCanvas); |
Alisa Lin | 7925114 | 2021-06-15 20:22:12 | [diff] [blame] | 2827 | prefs.canvas_2d_layers_enabled = |
| 2828 | command_line.HasSwitch(switches::kEnableCanvas2DLayers) || |
| 2829 | base::FeatureList::IsEnabled(features::kEnableCanvas2DLayers); |
Rakina Zata Amni | 4029b6d | 2020-07-28 02:36:20 | [diff] [blame] | 2830 | prefs.antialiased_2d_canvas_disabled = |
| 2831 | command_line.HasSwitch(switches::kDisable2dCanvasAntialiasing); |
| 2832 | prefs.antialiased_clips_2d_canvas_enabled = |
| 2833 | !command_line.HasSwitch(switches::kDisable2dCanvasClipAntialiasing); |
| 2834 | |
| 2835 | prefs.disable_ipc_flooding_protection = |
| 2836 | command_line.HasSwitch(switches::kDisableIpcFloodingProtection) || |
| 2837 | command_line.HasSwitch(switches::kDisablePushStateThrottle); |
| 2838 | |
| 2839 | prefs.accelerated_video_decode_enabled = |
| 2840 | !command_line.HasSwitch(switches::kDisableAcceleratedVideoDecode); |
| 2841 | |
| 2842 | std::string autoplay_policy = media::GetEffectiveAutoplayPolicy(command_line); |
| 2843 | if (autoplay_policy == switches::autoplay::kNoUserGestureRequiredPolicy) { |
Gyuyoung Kim | 1ac4ca78 | 2020-09-11 03:32:51 | [diff] [blame] | 2844 | prefs.autoplay_policy = |
Gyuyoung Kim | f6c02a26 | 2020-10-13 01:36:13 | [diff] [blame] | 2845 | blink::mojom::AutoplayPolicy::kNoUserGestureRequired; |
Rakina Zata Amni | 4029b6d | 2020-07-28 02:36:20 | [diff] [blame] | 2846 | } else if (autoplay_policy == |
| 2847 | switches::autoplay::kUserGestureRequiredPolicy) { |
Gyuyoung Kim | f6c02a26 | 2020-10-13 01:36:13 | [diff] [blame] | 2848 | prefs.autoplay_policy = blink::mojom::AutoplayPolicy::kUserGestureRequired; |
Rakina Zata Amni | 4029b6d | 2020-07-28 02:36:20 | [diff] [blame] | 2849 | } else if (autoplay_policy == |
| 2850 | switches::autoplay::kDocumentUserActivationRequiredPolicy) { |
Gyuyoung Kim | 1ac4ca78 | 2020-09-11 03:32:51 | [diff] [blame] | 2851 | prefs.autoplay_policy = |
Gyuyoung Kim | f6c02a26 | 2020-10-13 01:36:13 | [diff] [blame] | 2852 | blink::mojom::AutoplayPolicy::kDocumentUserActivationRequired; |
Rakina Zata Amni | 4029b6d | 2020-07-28 02:36:20 | [diff] [blame] | 2853 | } else { |
| 2854 | NOTREACHED(); |
| 2855 | } |
| 2856 | |
| 2857 | prefs.dont_send_key_events_to_javascript = |
| 2858 | base::FeatureList::IsEnabled(features::kDontSendKeyEventsToJavascript); |
| 2859 | |
| 2860 | // TODO(dtapuska): Enable barrel button selection drag support on Android. |
| 2861 | // crbug.com/758042 |
Xiaohan Wang | 24ec934 | 2022-01-15 17:34:22 | [diff] [blame] | 2862 | #if BUILDFLAG(IS_WIN) |
Elly Fong-Jones | 081a4cc3 | 2021-06-02 22:59:19 | [diff] [blame] | 2863 | prefs.barrel_button_for_drag_enabled = true; |
Xiaohan Wang | 24ec934 | 2022-01-15 17:34:22 | [diff] [blame] | 2864 | #endif // BUILDFLAG(IS_WIN) |
Rakina Zata Amni | 4029b6d | 2020-07-28 02:36:20 | [diff] [blame] | 2865 | |
Rakina Zata Amni | 4029b6d | 2020-07-28 02:36:20 | [diff] [blame] | 2866 | prefs.enable_scroll_animator = |
| 2867 | command_line.HasSwitch(switches::kEnableSmoothScrolling) || |
| 2868 | (!command_line.HasSwitch(switches::kDisableSmoothScrolling) && |
| 2869 | gfx::Animation::ScrollAnimationsEnabledBySystem()); |
| 2870 | |
| 2871 | prefs.prefers_reduced_motion = gfx::Animation::PrefersReducedMotion(); |
| 2872 | |
| 2873 | if (ChildProcessSecurityPolicyImpl::GetInstance()->HasWebUIBindings( |
| 2874 | GetRenderViewHost()->GetProcess()->GetID())) { |
| 2875 | prefs.loads_images_automatically = true; |
| 2876 | prefs.javascript_enabled = true; |
| 2877 | } |
| 2878 | |
| 2879 | prefs.viewport_enabled = command_line.HasSwitch(switches::kEnableViewport); |
| 2880 | |
David Bokan | 7950a3f | 2020-10-27 21:53:31 | [diff] [blame] | 2881 | prefs.threaded_scrolling_enabled = |
| 2882 | !command_line.HasSwitch(blink::switches::kDisableThreadedScrolling); |
| 2883 | |
John Abd-El-Malek | 74f0b95c | 2021-04-05 06:24:02 | [diff] [blame] | 2884 | if (GetController().GetVisibleEntry() && |
| 2885 | GetController().GetVisibleEntry()->GetIsOverridingUserAgent()) { |
Xiaohan Wang | 24ec934 | 2022-01-15 17:34:22 | [diff] [blame] | 2886 | #if BUILDFLAG(IS_ANDROID) |
John Abd-El-Malek | 267d9ed1 | 2021-03-24 21:29:13 | [diff] [blame] | 2887 | // Only ignore viewport meta tag when Request Desktop Site is used, but not |
| 2888 | // in other situations where embedder changes to arbitrary mobile UA string. |
| 2889 | if (renderer_preferences_.user_agent_override.ua_metadata_override && |
| 2890 | !renderer_preferences_.user_agent_override.ua_metadata_override->mobile) |
| 2891 | #endif |
| 2892 | prefs.viewport_meta_enabled = false; |
| 2893 | } |
Rakina Zata Amni | 4029b6d | 2020-07-28 02:36:20 | [diff] [blame] | 2894 | |
Rakina Zata Amni | 4029b6d | 2020-07-28 02:36:20 | [diff] [blame] | 2895 | prefs.spatial_navigation_enabled = |
| 2896 | command_line.HasSwitch(switches::kEnableSpatialNavigation); |
| 2897 | |
John Abd-El-Malek | 74f0b95c | 2021-04-05 06:24:02 | [diff] [blame] | 2898 | if (is_spatial_navigation_disabled_) |
Rakina Zata Amni | 4029b6d | 2020-07-28 02:36:20 | [diff] [blame] | 2899 | prefs.spatial_navigation_enabled = false; |
| 2900 | |
Mahesh Machavolu | 6cb8018 | 2022-07-22 08:09:31 | [diff] [blame] | 2901 | prefs.stylus_handwriting_enabled = stylus_handwriting_enabled_; |
| 2902 | |
Rakina Zata Amni | 4029b6d | 2020-07-28 02:36:20 | [diff] [blame] | 2903 | prefs.disable_reading_from_canvas = |
| 2904 | command_line.HasSwitch(switches::kDisableReadingFromCanvas); |
| 2905 | |
| 2906 | prefs.strict_mixed_content_checking = |
| 2907 | command_line.HasSwitch(switches::kEnableStrictMixedContentChecking); |
| 2908 | |
| 2909 | prefs.strict_powerful_feature_restrictions = command_line.HasSwitch( |
| 2910 | switches::kEnableStrictPowerfulFeatureRestrictions); |
| 2911 | |
Justin Novosad | 967ad8f0 | 2021-03-31 20:40:35 | [diff] [blame] | 2912 | prefs.fake_no_alloc_direct_call_for_testing_enabled = |
| 2913 | command_line.HasSwitch(switches::kEnableFakeNoAllocDirectCallForTesting); |
| 2914 | |
Rakina Zata Amni | 4029b6d | 2020-07-28 02:36:20 | [diff] [blame] | 2915 | const std::string blockable_mixed_content_group = |
| 2916 | base::FieldTrialList::FindFullName("BlockableMixedContent"); |
| 2917 | prefs.strictly_block_blockable_mixed_content = |
| 2918 | blockable_mixed_content_group == "StrictlyBlockBlockableMixedContent"; |
| 2919 | |
| 2920 | const std::string plugin_mixed_content_status = |
| 2921 | base::FieldTrialList::FindFullName("PluginMixedContentStatus"); |
| 2922 | prefs.block_mixed_plugin_content = |
| 2923 | plugin_mixed_content_status == "BlockableMixedContent"; |
| 2924 | |
| 2925 | prefs.v8_cache_options = GetV8CacheOptions(); |
| 2926 | |
| 2927 | prefs.user_gesture_required_for_presentation = !command_line.HasSwitch( |
| 2928 | switches::kDisableGestureRequirementForPresentation); |
| 2929 | |
John Abd-El-Malek | 74f0b95c | 2021-04-05 06:24:02 | [diff] [blame] | 2930 | if (is_overlay_content_) |
Rakina Zata Amni | 4029b6d | 2020-07-28 02:36:20 | [diff] [blame] | 2931 | prefs.hide_download_ui = true; |
| 2932 | |
| 2933 | // `media_controls_enabled` is `true` by default. |
John Abd-El-Malek | 74f0b95c | 2021-04-05 06:24:02 | [diff] [blame] | 2934 | if (has_persistent_video_) |
Rakina Zata Amni | 4029b6d | 2020-07-28 02:36:20 | [diff] [blame] | 2935 | prefs.media_controls_enabled = false; |
| 2936 | |
Xiaohan Wang | 24ec934 | 2022-01-15 17:34:22 | [diff] [blame] | 2937 | #if BUILDFLAG(IS_ANDROID) |
Rakina Zata Amni | 4029b6d | 2020-07-28 02:36:20 | [diff] [blame] | 2938 | display::Display display = display::Screen::GetScreen()->GetPrimaryDisplay(); |
| 2939 | gfx::Size size = display.GetSizeInPixel(); |
| 2940 | int min_width = size.width() < size.height() ? size.width() : size.height(); |
| 2941 | prefs.device_scale_adjustment = GetDeviceScaleAdjustment( |
| 2942 | static_cast<int>(min_width / display.device_scale_factor())); |
Xiaohan Wang | 24ec934 | 2022-01-15 17:34:22 | [diff] [blame] | 2943 | #endif // BUILDFLAG(IS_ANDROID) |
Rakina Zata Amni | 4029b6d | 2020-07-28 02:36:20 | [diff] [blame] | 2944 | |
Dave Tapuska | 52678d136 | 2022-07-22 19:37:16 | [diff] [blame] | 2945 | // GuestViews in the same StoragePartition need to find each other's frames. |
| 2946 | prefs.renderer_wide_named_frame_lookup = IsGuest(); |
| 2947 | |
David Bokan | 7ec5ac92 | 2022-11-29 03:31:31 | [diff] [blame] | 2948 | if (command_line.HasSwitch(switches::kHideScrollbars)) |
| 2949 | prefs.hide_scrollbars = true; |
| 2950 | |
Aaron Colwell | bd02c6c | 2021-01-16 00:34:29 | [diff] [blame] | 2951 | GetContentClient()->browser()->OverrideWebkitPrefs(this, &prefs); |
Rakina Zata Amni | 4029b6d | 2020-07-28 02:36:20 | [diff] [blame] | 2952 | return prefs; |
| 2953 | } |
| 2954 | |
| 2955 | void WebContentsImpl::SetSlowWebPreferences( |
| 2956 | const base::CommandLine& command_line, |
Gyuyoung Kim | 1ac4ca78 | 2020-09-11 03:32:51 | [diff] [blame] | 2957 | blink::web_pref::WebPreferences* prefs) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 2958 | OPTIONAL_TRACE_EVENT0("content", "WebContentsImpl::SetSlowWebPreferences"); |
| 2959 | |
Rakina Zata Amni | 4029b6d | 2020-07-28 02:36:20 | [diff] [blame] | 2960 | if (web_preferences_.get()) { |
| 2961 | #define SET_FROM_CACHE(prefs, field) prefs->field = web_preferences_->field |
| 2962 | |
| 2963 | SET_FROM_CACHE(prefs, touch_event_feature_detection_enabled); |
| 2964 | SET_FROM_CACHE(prefs, available_pointer_types); |
| 2965 | SET_FROM_CACHE(prefs, available_hover_types); |
| 2966 | SET_FROM_CACHE(prefs, primary_pointer_type); |
| 2967 | SET_FROM_CACHE(prefs, primary_hover_type); |
| 2968 | SET_FROM_CACHE(prefs, pointer_events_max_touch_points); |
| 2969 | SET_FROM_CACHE(prefs, number_of_cpu_cores); |
| 2970 | |
Xiaohan Wang | 24ec934 | 2022-01-15 17:34:22 | [diff] [blame] | 2971 | #if BUILDFLAG(IS_ANDROID) |
Rakina Zata Amni | 4029b6d | 2020-07-28 02:36:20 | [diff] [blame] | 2972 | SET_FROM_CACHE(prefs, video_fullscreen_orientation_lock_enabled); |
| 2973 | SET_FROM_CACHE(prefs, video_rotate_to_fullscreen_enabled); |
| 2974 | #endif |
| 2975 | |
| 2976 | #undef SET_FROM_CACHE |
| 2977 | } else { |
| 2978 | // Every prefs->field modified below should have a SET_FROM_CACHE entry |
| 2979 | // above. |
| 2980 | |
| 2981 | // On Android, Touch event feature detection is enabled by default, |
| 2982 | // Otherwise default is disabled. |
| 2983 | std::string touch_enabled_default_switch = |
| 2984 | switches::kTouchEventFeatureDetectionDisabled; |
Xiaohan Wang | 24ec934 | 2022-01-15 17:34:22 | [diff] [blame] | 2985 | #if BUILDFLAG(IS_ANDROID) |
Rakina Zata Amni | 4029b6d | 2020-07-28 02:36:20 | [diff] [blame] | 2986 | touch_enabled_default_switch = switches::kTouchEventFeatureDetectionEnabled; |
Xiaohan Wang | 24ec934 | 2022-01-15 17:34:22 | [diff] [blame] | 2987 | #endif // BUILDFLAG(IS_ANDROID) |
Rakina Zata Amni | 4029b6d | 2020-07-28 02:36:20 | [diff] [blame] | 2988 | const std::string touch_enabled_switch = |
| 2989 | command_line.HasSwitch(switches::kTouchEventFeatureDetection) |
| 2990 | ? command_line.GetSwitchValueASCII( |
| 2991 | switches::kTouchEventFeatureDetection) |
| 2992 | : touch_enabled_default_switch; |
| 2993 | |
| 2994 | prefs->touch_event_feature_detection_enabled = |
| 2995 | (touch_enabled_switch == switches::kTouchEventFeatureDetectionAuto) |
| 2996 | ? (ui::GetTouchScreensAvailability() == |
| 2997 | ui::TouchScreensAvailability::ENABLED) |
| 2998 | : (touch_enabled_switch.empty() || |
| 2999 | touch_enabled_switch == |
| 3000 | switches::kTouchEventFeatureDetectionEnabled); |
| 3001 | |
| 3002 | std::tie(prefs->available_pointer_types, prefs->available_hover_types) = |
Peter Kvitek | 965f3cd | 2022-01-06 22:33:41 | [diff] [blame] | 3003 | GetAvailablePointerAndHoverTypes(); |
Gyuyoung Kim | d50ce7f | 2020-11-20 19:16:30 | [diff] [blame] | 3004 | prefs->primary_pointer_type = static_cast<blink::mojom::PointerType>( |
| 3005 | ui::GetPrimaryPointerType(prefs->available_pointer_types)); |
Gyuyoung Kim | c527ec3 | 2020-11-21 03:31:43 | [diff] [blame] | 3006 | prefs->primary_hover_type = static_cast<blink::mojom::HoverType>( |
| 3007 | ui::GetPrimaryHoverType(prefs->available_hover_types)); |
Rakina Zata Amni | 4029b6d | 2020-07-28 02:36:20 | [diff] [blame] | 3008 | |
| 3009 | prefs->pointer_events_max_touch_points = ui::MaxTouchPoints(); |
| 3010 | |
| 3011 | prefs->number_of_cpu_cores = base::SysInfo::NumberOfProcessors(); |
| 3012 | |
Xiaohan Wang | 24ec934 | 2022-01-15 17:34:22 | [diff] [blame] | 3013 | #if BUILDFLAG(IS_ANDROID) |
Rakina Zata Amni | 4029b6d | 2020-07-28 02:36:20 | [diff] [blame] | 3014 | const bool device_is_phone = |
| 3015 | ui::GetDeviceFormFactor() == ui::DEVICE_FORM_FACTOR_PHONE; |
| 3016 | prefs->video_fullscreen_orientation_lock_enabled = device_is_phone; |
| 3017 | prefs->video_rotate_to_fullscreen_enabled = device_is_phone; |
| 3018 | #endif |
| 3019 | } |
| 3020 | } |
| 3021 | |
| 3022 | void WebContentsImpl::OnWebPreferencesChanged() { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 3023 | OPTIONAL_TRACE_EVENT0("content", "WebContentsImpl::OnWebPreferencesChanged"); |
| 3024 | |
Rakina Zata Amni | 4029b6d | 2020-07-28 02:36:20 | [diff] [blame] | 3025 | // This is defensive code to avoid infinite loops due to code run inside |
| 3026 | // SetWebPreferences() accidentally updating more preferences and thus |
| 3027 | // calling back into this code. See crbug.com/398751 for one past example. |
| 3028 | if (updating_web_preferences_) |
| 3029 | return; |
| 3030 | updating_web_preferences_ = true; |
| 3031 | SetWebPreferences(ComputeWebPreferences()); |
Xiaohan Wang | 24ec934 | 2022-01-15 17:34:22 | [diff] [blame] | 3032 | #if BUILDFLAG(IS_ANDROID) |
Carlos Caballero | b65b6e3a | 2021-11-15 10:09:00 | [diff] [blame] | 3033 | for (FrameTreeNode* node : primary_frame_tree_.Nodes()) { |
Rakina Zata Amni | 4029b6d | 2020-07-28 02:36:20 | [diff] [blame] | 3034 | RenderFrameHostImpl* rfh = node->current_frame_host(); |
| 3035 | if (rfh->is_local_root()) { |
| 3036 | if (auto* rwh = rfh->GetRenderWidgetHost()) |
| 3037 | rwh->SetForceEnableZoom(web_preferences_->force_enable_zoom); |
| 3038 | } |
| 3039 | } |
| 3040 | #endif |
| 3041 | updating_web_preferences_ = false; |
| 3042 | } |
| 3043 | |
Ahmed Fakhry | d70bf37b | 2018-04-04 17:07:24 | [diff] [blame] | 3044 | void WebContentsImpl::NotifyPreferencesChanged() { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 3045 | OPTIONAL_TRACE_EVENT0("content", "WebContentsImpl::NotifyPreferencesChanged"); |
| 3046 | |
Rakina Zata Amni | 347b7090 | 2020-07-22 10:49:04 | [diff] [blame] | 3047 | // Recompute the WebPreferences based on the current state of the WebContents, |
Rakina Zata Amni | 4029b6d | 2020-07-28 02:36:20 | [diff] [blame] | 3048 | // etc. Note that OnWebPreferencesChanged will also call SetWebPreferences and |
| 3049 | // send the updated WebPreferences to all RenderViews for this WebContents. |
| 3050 | OnWebPreferencesChanged(); |
Ahmed Fakhry | d70bf37b | 2018-04-04 17:07:24 | [diff] [blame] | 3051 | } |
| 3052 | |
Bruce Long | 1e3e1f54 | 2019-10-16 17:56:28 | [diff] [blame] | 3053 | void WebContentsImpl::SyncRendererPrefs() { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 3054 | OPTIONAL_TRACE_EVENT0("content", "WebContentsImpl::SyncRendererPrefs"); |
| 3055 | |
Mario Sanchez Prada | 0bd8b8c | 2020-10-21 17:49:23 | [diff] [blame] | 3056 | blink::RendererPreferences renderer_preferences = GetRendererPrefs(); |
Bruce Long | 1e3e1f54 | 2019-10-16 17:56:28 | [diff] [blame] | 3057 | RenderViewHostImpl::GetPlatformSpecificPrefs(&renderer_preferences); |
Takashi Toyoshima | df3f4e31 | 2021-11-19 03:51:48 | [diff] [blame] | 3058 | ExecutePageBroadcastMethodForAllPages(base::BindRepeating( |
Mario Sanchez Prada | c3eb9c8 | 2020-10-23 19:12:50 | [diff] [blame] | 3059 | [](const blink::RendererPreferences& preferences, |
| 3060 | RenderViewHostImpl* rvh) { |
| 3061 | rvh->SendRendererPreferencesToRenderer(preferences); |
| 3062 | }, |
| 3063 | renderer_preferences)); |
Bruce Long | 1e3e1f54 | 2019-10-16 17:56:28 | [diff] [blame] | 3064 | } |
| 3065 | |
Alexander Timin | 1cc31f4 | 2020-05-12 16:26:01 | [diff] [blame] | 3066 | void WebContentsImpl::OnCookiesAccessed(NavigationHandle* navigation, |
| 3067 | const CookieAccessDetails& details) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 3068 | OPTIONAL_TRACE_EVENT1("content", "WebContentsImpl::OnCookiesAccessed", |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 3069 | "navigation_handle", navigation); |
Andrew Grieve | e19cd93e | 2021-01-22 05:43:06 | [diff] [blame] | 3070 | // Use a variable to select between overloads. |
| 3071 | void (WebContentsObserver::*func)(NavigationHandle*, |
| 3072 | const CookieAccessDetails&) = |
| 3073 | &WebContentsObserver::OnCookiesAccessed; |
| 3074 | observers_.NotifyObservers(func, navigation, details); |
Alexander Timin | 1cc31f4 | 2020-05-12 16:26:01 | [diff] [blame] | 3075 | } |
| 3076 | |
| 3077 | void WebContentsImpl::OnCookiesAccessed(RenderFrameHostImpl* rfh, |
| 3078 | const CookieAccessDetails& details) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 3079 | OPTIONAL_TRACE_EVENT1("content", "WebContentsImpl::OnCookiesAccessed", |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 3080 | "render_frame_host", rfh); |
Andrew Grieve | e19cd93e | 2021-01-22 05:43:06 | [diff] [blame] | 3081 | // Use a variable to select between overloads. |
| 3082 | void (WebContentsObserver::*func)(RenderFrameHost*, |
| 3083 | const CookieAccessDetails&) = |
| 3084 | &WebContentsObserver::OnCookiesAccessed; |
| 3085 | observers_.NotifyObservers(func, rfh, details); |
Josh Karlin | daba3932 | 2019-07-17 23:24:33 | [diff] [blame] | 3086 | } |
| 3087 | |
[email protected] | b172aee | 2012-04-10 17:05:26 | [diff] [blame] | 3088 | void WebContentsImpl::Stop() { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 3089 | TRACE_EVENT0("content", "WebContentsImpl::Stop"); |
Takashi Toyoshima | dc3f747 | 2021-07-21 08:02:32 | [diff] [blame] | 3090 | ForEachFrameTree(base::BindRepeating( |
Arthur Sonzogni | f6785ec | 2022-12-05 10:11:50 | [diff] [blame] | 3091 | [](FrameTree& frame_tree) { frame_tree.StopLoading(); })); |
Johann | 344e1c7 | 2022-11-22 07:22:30 | [diff] [blame] | 3092 | GetPrerenderHostRegistry()->CancelAllHosts(PrerenderFinalStatus::kStop); |
Andrew Grieve | e19cd93e | 2021-01-22 05:43:06 | [diff] [blame] | 3093 | observers_.NotifyObservers(&WebContentsObserver::NavigationStopped); |
[email protected] | 0bfbf88 | 2011-12-22 18:19:27 | [diff] [blame] | 3094 | } |
| 3095 | |
Francois Doray | 47f759d | 2018-06-11 18:13:51 | [diff] [blame] | 3096 | void WebContentsImpl::SetPageFrozen(bool frozen) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 3097 | TRACE_EVENT1("content", "WebContentsImpl::SetPageFrozen", "frozen", frozen); |
| 3098 | |
Francois Doray | 47f759d | 2018-06-11 18:13:51 | [diff] [blame] | 3099 | // A visible page is never frozen. |
| 3100 | DCHECK_NE(Visibility::VISIBLE, GetVisibility()); |
| 3101 | |
Sharon Yang | ed88454 | 2023-02-02 17:33:44 | [diff] [blame] | 3102 | primary_frame_tree_.ForEachRenderViewHost( |
| 3103 | [&frozen](RenderViewHostImpl* rvh) { rvh->SetIsFrozen(frozen); }); |
Fadi Meawad | a6573e0 | 2018-03-10 00:52:11 | [diff] [blame] | 3104 | } |
| 3105 | |
erikchen | 6c7df7f7a | 2018-05-03 18:13:59 | [diff] [blame] | 3106 | std::unique_ptr<WebContents> WebContentsImpl::Clone() { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 3107 | TRACE_EVENT0("content", "WebContentsImpl::Clone"); |
| 3108 | |
[email protected] | 14392a5 | 2012-05-02 20:28:44 | [diff] [blame] | 3109 | // We use our current SiteInstance since the cloned entry will use it anyway. |
alexmos | e201c7cd | 2015-06-10 17:14:21 | [diff] [blame] | 3110 | // We pass our own opener so that the cloned page can access it if it was set |
[email protected] | ed245db | 2012-12-18 08:00:45 | [diff] [blame] | 3111 | // before. |
[email protected] | 54944cde | 2012-12-09 09:24:59 | [diff] [blame] | 3112 | CreateParams create_params(GetBrowserContext(), GetSiteInstance()); |
Carlos Caballero | b65b6e3a | 2021-11-15 10:09:00 | [diff] [blame] | 3113 | FrameTreeNode* opener = primary_frame_tree_.root()->opener(); |
Ian Clelland | 5cbaaf8 | 2017-11-27 22:00:03 | [diff] [blame] | 3114 | RenderFrameHostImpl* opener_rfh = nullptr; |
| 3115 | if (opener) |
| 3116 | opener_rfh = opener->current_frame_host(); |
erikchen | 6c7df7f7a | 2018-05-03 18:13:59 | [diff] [blame] | 3117 | std::unique_ptr<WebContentsImpl> tc = |
erikchen | 50735fd | 2018-05-05 15:08:33 | [diff] [blame] | 3118 | CreateWithOpener(create_params, opener_rfh); |
Carlos Caballero | b65b6e3a | 2021-11-15 10:09:00 | [diff] [blame] | 3119 | tc->GetController().CopyStateFrom(&primary_frame_tree_.controller(), true); |
Andrew Grieve | e19cd93e | 2021-01-22 05:43:06 | [diff] [blame] | 3120 | observers_.NotifyObservers(&WebContentsObserver::DidCloneToNewWebContents, |
| 3121 | this, tc.get()); |
[email protected] | 0bfbf88 | 2011-12-22 18:19:27 | [diff] [blame] | 3122 | return tc; |
| 3123 | } |
| 3124 | |
Matt Falkenhagen | 548ed156 | 2021-07-06 01:38:26 | [diff] [blame] | 3125 | WebContents* WebContentsImpl::DeprecatedGetWebContents() { |
[email protected] | ec6c05f | 2013-10-23 18:41:57 | [diff] [blame] | 3126 | return this; |
| 3127 | } |
| 3128 | |
Harkiran Bolaria | 5ce2763 | 2022-01-20 15:05:05 | [diff] [blame] | 3129 | void WebContentsImpl::Init(const WebContents::CreateParams& params, |
| 3130 | blink::FramePolicy primary_main_frame_policy) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 3131 | TRACE_EVENT0("content", "WebContentsImpl::Init"); |
| 3132 | |
Lukasz Anforowicz | 2c1573a | 2021-09-21 18:58:18 | [diff] [blame] | 3133 | creator_location_ = params.creator_location; |
Nicolas Ouellet-Payeur | 177a6c4 | 2023-01-25 20:25:07 | [diff] [blame] | 3134 | #if BUILDFLAG(IS_ANDROID) |
| 3135 | java_creator_location_ = params.java_creator_location; |
| 3136 | #endif // BUILDFLAG(IS_ANDROID) |
Lukasz Anforowicz | 2c1573a | 2021-09-21 18:58:18 | [diff] [blame] | 3137 | |
Tommy Steimel | 57eafde | 2023-01-27 17:33:24 | [diff] [blame] | 3138 | if (params.picture_in_picture_options.has_value()) { |
| 3139 | picture_in_picture_options_ = params.picture_in_picture_options; |
| 3140 | } |
Tommy Steimel | 991916c4 | 2022-06-24 20:59:57 | [diff] [blame] | 3141 | |
[email protected] | fa944cb8 | 2013-11-15 17:51:21 | [diff] [blame] | 3142 | // This is set before initializing the render manager since |
[email protected] | b0936d2 | 2013-11-28 06:47:36 | [diff] [blame] | 3143 | // RenderFrameHostManager::Init calls back into us via its delegate to ask if |
[email protected] | fa944cb8 | 2013-11-15 17:51:21 | [diff] [blame] | 3144 | // it should be hidden. |
Francois Doray | e616115 | 2018-03-27 22:05:37 | [diff] [blame] | 3145 | visibility_ = |
| 3146 | params.initially_hidden ? Visibility::HIDDEN : Visibility::VISIBLE; |
[email protected] | d6fa88f | 2013-10-18 16:00:43 | [diff] [blame] | 3147 | |
Thoren Paulson | d344c0a | 2021-10-14 19:20:31 | [diff] [blame] | 3148 | enable_wake_locks_ = params.enable_wake_locks; |
| 3149 | |
Sebastien Marchand | 28cf2318 | 2018-06-20 02:39:35 | [diff] [blame] | 3150 | if (!params.last_active_time.is_null()) |
| 3151 | last_active_time_ = params.last_active_time; |
| 3152 | |
Charlie Reis | 37be268 | 2023-01-10 17:04:47 | [diff] [blame] | 3153 | scoped_refptr<SiteInstanceImpl> site_instance = |
| 3154 | static_cast<SiteInstanceImpl*>(params.site_instance.get()); |
dcheng | 3ce04b6 | 2015-10-26 23:30:55 | [diff] [blame] | 3155 | if (!site_instance) |
Charlie Reis | 37be268 | 2023-01-10 17:04:47 | [diff] [blame] | 3156 | site_instance = SiteInstanceImpl::Create(params.browser_context); |
Lukasz Anforowicz | 3caa837 | 2018-06-05 17:22:07 | [diff] [blame] | 3157 | if (params.desired_renderer_state == CreateParams::kNoRendererProcess) { |
Charlie Reis | 37be268 | 2023-01-10 17:04:47 | [diff] [blame] | 3158 | site_instance->PreventAssociationWithSpareProcess(); |
Lukasz Anforowicz | 3caa837 | 2018-06-05 17:22:07 | [diff] [blame] | 3159 | } |
dcheng | 3ce04b6 | 2015-10-26 23:30:55 | [diff] [blame] | 3160 | |
Rakina Zata Amni | 4eb716e | 2022-04-05 21:32:46 | [diff] [blame] | 3161 | // Iniitalize the primary FrameTree. |
| 3162 | // Note that GetOpener() is used here to get the opener for origin |
| 3163 | // inheritance, instead of other similar functions: |
| 3164 | // - GetOriginalOpener(), which would always return the main frame of the |
| 3165 | // opener, which might be different from the actual opener. |
| 3166 | // - FindOpenerRFH(), which will still return the opener frame if the |
| 3167 | // opener is suppressed (e.g. due to 'noopener'). The opener should not |
| 3168 | // be used for origin inheritance purposes in those cases, so this should |
| 3169 | // not pass the opener for those cases. |
Harkiran Bolaria | 5ce2763 | 2022-01-20 15:05:05 | [diff] [blame] | 3170 | primary_frame_tree_.Init( |
| 3171 | site_instance.get(), params.renderer_initiated_creation, |
Danil Somsikov | 259aa65f | 2022-11-11 20:49:44 | [diff] [blame] | 3172 | params.main_frame_name, GetOpener(), primary_main_frame_policy, |
| 3173 | base::UnguessableToken::Create()); |
[email protected] | d1198fd | 2012-08-13 22:50:19 | [diff] [blame] | 3174 | |
Avi Drissman | 0d0908ded | 2022-04-28 05:39:39 | [diff] [blame] | 3175 | std::unique_ptr<WebContentsViewDelegate> delegate = |
[email protected] | fc2b46b | 2014-05-03 16:33:45 | [diff] [blame] | 3176 | GetContentClient()->browser()->GetWebContentsViewDelegate(this); |
| 3177 | |
W. James MacLean | 2539adb3 | 2019-12-13 00:40:44 | [diff] [blame] | 3178 | if (browser_plugin_guest_) { |
Fergal Daly | 55b6d72 | 2020-09-11 07:56:33 | [diff] [blame] | 3179 | view_ = std::make_unique<WebContentsViewChildFrame>( |
Avi Drissman | 0d0908ded | 2022-04-28 05:39:39 | [diff] [blame] | 3180 | this, std::move(delegate), &render_view_host_delegate_view_); |
sky | 52a39e34 | 2016-12-17 17:23:22 | [diff] [blame] | 3181 | } else { |
Avi Drissman | 0d0908ded | 2022-04-28 05:39:39 | [diff] [blame] | 3182 | view_ = CreateWebContentsView(this, std::move(delegate), |
| 3183 | &render_view_host_delegate_view_); |
[email protected] | d1198fd | 2012-08-13 22:50:19 | [diff] [blame] | 3184 | } |
[email protected] | fc2b46b | 2014-05-03 16:33:45 | [diff] [blame] | 3185 | CHECK(render_view_host_delegate_view_); |
[email protected] | d1198fd | 2012-08-13 22:50:19 | [diff] [blame] | 3186 | CHECK(view_.get()); |
| 3187 | |
danakj | fc518493 | 2019-09-12 18:08:32 | [diff] [blame] | 3188 | view_->CreateView(params.context); |
[email protected] | d1198fd | 2012-08-13 22:50:19 | [diff] [blame] | 3189 | |
Fergal Daly | 55b6d72 | 2020-09-11 07:56:33 | [diff] [blame] | 3190 | screen_orientation_provider_ = |
| 3191 | std::make_unique<ScreenOrientationProvider>(this); |
[email protected] | 01f83f9 | 2014-06-17 14:41:54 | [diff] [blame] | 3192 | |
Xiaohan Wang | 24ec934 | 2022-01-15 17:34:22 | [diff] [blame] | 3193 | #if BUILDFLAG(IS_ANDROID) |
Oksana Zhuravlova | 0b63433 | 2019-10-28 23:04:22 | [diff] [blame] | 3194 | DateTimeChooserAndroid::CreateForWebContents(this); |
[email protected] | 583418cc | 2013-01-17 14:01:10 | [diff] [blame] | 3195 | #endif |
fsamuel | 7310a426 | 2014-12-05 05:06:44 | [diff] [blame] | 3196 | |
Nan Lin | 5a49153 | 2022-11-11 18:03:28 | [diff] [blame] | 3197 | // AttributionHost must be created after `view_->CreateView()` is called as it |
| 3198 | // may invoke `WebContentsAndroid::AddObserver()`. |
| 3199 | if (base::FeatureList::IsEnabled(blink::features::kConversionMeasurement)) { |
| 3200 | AttributionHost::CreateForWebContents(this); |
| 3201 | } |
| 3202 | |
fsamuel | 7310a426 | 2014-12-05 05:06:44 | [diff] [blame] | 3203 | // BrowserPluginGuest::Init needs to be called after this WebContents has |
W. James MacLean | 6e78da34 | 2019-12-13 15:13:23 | [diff] [blame] | 3204 | // a RenderWidgetHostViewChildFrame. That is, |view_->CreateView| above. |
fsamuel | 7310a426 | 2014-12-05 05:06:44 | [diff] [blame] | 3205 | if (browser_plugin_guest_) |
| 3206 | browser_plugin_guest_->Init(); |
nick | f9acfbe | 2014-12-23 19:12:37 | [diff] [blame] | 3207 | |
Elly Fong-Jones | 564de626 | 2022-07-28 21:11:16 | [diff] [blame] | 3208 | g_created_callbacks.Get().Notify(this); |
nasko | b21fe487 | 2015-02-24 14:15:56 | [diff] [blame] | 3209 | |
lof84 | 501da08 | 2016-05-23 21:22:54 | [diff] [blame] | 3210 | // Create the renderer process in advance if requested. |
Lukasz Anforowicz | 3caa837 | 2018-06-05 17:22:07 | [diff] [blame] | 3211 | if (params.desired_renderer_state == |
| 3212 | CreateParams::kInitializeAndWarmupRendererProcess) { |
lof84 | 501da08 | 2016-05-23 21:22:54 | [diff] [blame] | 3213 | if (!GetRenderManager()->current_frame_host()->IsRenderFrameLive()) { |
Charlie Reis | 37be268 | 2023-01-10 17:04:47 | [diff] [blame] | 3214 | GetRenderManager()->InitRenderView(site_instance->group(), |
| 3215 | GetRenderViewHost(), nullptr); |
lof84 | 501da08 | 2016-05-23 21:22:54 | [diff] [blame] | 3216 | } |
| 3217 | } |
| 3218 | |
Lukasz Anforowicz | 1830290 | 2021-11-16 19:45:05 | [diff] [blame] | 3219 | // Let the embedder know about the newly created and initialized WebContents. |
| 3220 | GetContentClient()->browser()->OnWebContentsCreated(this); |
| 3221 | |
nasko | f5940b9f | 2015-03-02 23:04:05 | [diff] [blame] | 3222 | // Ensure that observers are notified of the creation of this WebContents's |
| 3223 | // main RenderFrameHost. It must be done here for main frames, since the |
| 3224 | // NotifySwappedFromRenderManager expects view_ to already be created and that |
| 3225 | // happens after RenderFrameHostManager::Init. |
Dave Tapuska | e45d6fd | 2021-09-29 17:03:59 | [diff] [blame] | 3226 | NotifySwappedFromRenderManager(nullptr, |
| 3227 | GetRenderManager()->current_frame_host()); |
[email protected] | d1198fd | 2012-08-13 22:50:19 | [diff] [blame] | 3228 | } |
| 3229 | |
[email protected] | 7fff43e | 2013-05-21 20:21:10 | [diff] [blame] | 3230 | void WebContentsImpl::OnWebContentsDestroyed(WebContentsImpl* web_contents) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 3231 | OPTIONAL_TRACE_EVENT0("content", "WebContentsImpl::OnWebContentsDestroyed"); |
| 3232 | |
Avi Drissman | 783c123 | 2021-02-05 01:27:05 | [diff] [blame] | 3233 | RemoveWebContentsDestructionObserver(web_contents); |
[email protected] | 7fff43e | 2013-05-21 20:21:10 | [diff] [blame] | 3234 | |
[email protected] | ceee8cd | 2013-03-08 04:59:51 | [diff] [blame] | 3235 | // Clear a pending contents that has been closed before being shown. |
alexmos | c2a8cec | 2016-05-23 22:19:53 | [diff] [blame] | 3236 | for (auto iter = pending_contents_.begin(); iter != pending_contents_.end(); |
[email protected] | ceee8cd | 2013-03-08 04:59:51 | [diff] [blame] | 3237 | ++iter) { |
Joel Hockey | 8c2011b2 | 2020-04-30 05:07:19 | [diff] [blame] | 3238 | if (iter->second.contents.get() != web_contents) |
[email protected] | ceee8cd | 2013-03-08 04:59:51 | [diff] [blame] | 3239 | continue; |
erikchen | bee5c962 | 2018-04-27 19:30:25 | [diff] [blame] | 3240 | |
| 3241 | // Someone else has deleted the WebContents. That should never happen! |
| 3242 | // TODO(erikchen): Fix semantics here. https://crbug.com/832879. |
Joel Hockey | 8c2011b2 | 2020-04-30 05:07:19 | [diff] [blame] | 3243 | iter->second.contents.release(); |
[email protected] | ceee8cd | 2013-03-08 04:59:51 | [diff] [blame] | 3244 | pending_contents_.erase(iter); |
[email protected] | ceee8cd | 2013-03-08 04:59:51 | [diff] [blame] | 3245 | return; |
| 3246 | } |
| 3247 | NOTREACHED(); |
[email protected] | 14392a5 | 2012-05-02 20:28:44 | [diff] [blame] | 3248 | } |
| 3249 | |
Avi Drissman | 783c123 | 2021-02-05 01:27:05 | [diff] [blame] | 3250 | void WebContentsImpl::OnRenderWidgetHostDestroyed( |
| 3251 | RenderWidgetHost* render_widget_host) { |
| 3252 | OPTIONAL_TRACE_EVENT0("content", "WebContentsImpl::OnWebContentsDestroyed"); |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 3253 | |
Avi Drissman | 783c123 | 2021-02-05 01:27:05 | [diff] [blame] | 3254 | RemoveRenderWidgetHostDestructionObserver(render_widget_host); |
| 3255 | |
| 3256 | // Clear a pending widget that has been closed before being shown. |
| 3257 | size_t num_erased = |
| 3258 | base::EraseIf(pending_widgets_, [render_widget_host](const auto& pair) { |
| 3259 | return pair.second == render_widget_host; |
| 3260 | }); |
| 3261 | DCHECK_EQ(1u, num_erased); |
| 3262 | } |
| 3263 | |
| 3264 | void WebContentsImpl::AddWebContentsDestructionObserver( |
| 3265 | WebContentsImpl* web_contents) { |
| 3266 | OPTIONAL_TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("content.verbose"), |
| 3267 | "WebContentsImpl::AddWebContentsDestructionObserver"); |
| 3268 | |
| 3269 | if (!base::Contains(web_contents_destruction_observers_, web_contents)) { |
| 3270 | web_contents_destruction_observers_[web_contents] = |
| 3271 | std::make_unique<WebContentsDestructionObserver>(this, web_contents); |
[email protected] | 7fff43e | 2013-05-21 20:21:10 | [diff] [blame] | 3272 | } |
| 3273 | } |
| 3274 | |
Avi Drissman | 783c123 | 2021-02-05 01:27:05 | [diff] [blame] | 3275 | void WebContentsImpl::RemoveWebContentsDestructionObserver( |
| 3276 | WebContentsImpl* web_contents) { |
| 3277 | OPTIONAL_TRACE_EVENT0( |
| 3278 | TRACE_DISABLED_BY_DEFAULT("content.verbose"), |
| 3279 | "WebContentsImpl::RemoveWebContentsDestructionObserver"); |
| 3280 | web_contents_destruction_observers_.erase(web_contents); |
| 3281 | } |
| 3282 | |
| 3283 | void WebContentsImpl::AddRenderWidgetHostDestructionObserver( |
| 3284 | RenderWidgetHost* render_widget_host) { |
| 3285 | OPTIONAL_TRACE_EVENT0( |
| 3286 | TRACE_DISABLED_BY_DEFAULT("content.verbose"), |
| 3287 | "WebContentsImpl::AddRenderWidgetHostDestructionObserver"); |
| 3288 | |
| 3289 | if (!base::Contains(render_widget_host_destruction_observers_, |
| 3290 | render_widget_host)) { |
| 3291 | render_widget_host_destruction_observers_[render_widget_host] = |
| 3292 | std::make_unique<RenderWidgetHostDestructionObserver>( |
| 3293 | this, render_widget_host); |
| 3294 | } |
| 3295 | } |
| 3296 | |
| 3297 | void WebContentsImpl::RemoveRenderWidgetHostDestructionObserver( |
| 3298 | RenderWidgetHost* render_widget_host) { |
| 3299 | OPTIONAL_TRACE_EVENT0( |
| 3300 | TRACE_DISABLED_BY_DEFAULT("content.verbose"), |
| 3301 | "WebContentsImpl::RemoveRenderWidgetHostDestructionObserver"); |
| 3302 | render_widget_host_destruction_observers_.erase(render_widget_host); |
[email protected] | 7fff43e | 2013-05-21 20:21:10 | [diff] [blame] | 3303 | } |
| 3304 | |
[email protected] | b172aee | 2012-04-10 17:05:26 | [diff] [blame] | 3305 | void WebContentsImpl::AddObserver(WebContentsObserver* observer) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 3306 | OPTIONAL_TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("content.verbose"), |
| 3307 | "WebContentsImpl::AddObserver"); |
[email protected] | be1f56ab | 2011-12-22 06:55:31 | [diff] [blame] | 3308 | observers_.AddObserver(observer); |
| 3309 | } |
| 3310 | |
[email protected] | b172aee | 2012-04-10 17:05:26 | [diff] [blame] | 3311 | void WebContentsImpl::RemoveObserver(WebContentsObserver* observer) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 3312 | OPTIONAL_TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("content.verbose"), |
| 3313 | "WebContentsImpl::RemoveObserver"); |
[email protected] | be1f56ab | 2011-12-22 06:55:31 | [diff] [blame] | 3314 | observers_.RemoveObserver(observer); |
| 3315 | } |
| 3316 | |
Kevin McNee | ef1e936 | 2021-06-08 21:31:52 | [diff] [blame] | 3317 | std::set<RenderWidgetHostViewBase*> |
Adithya Srinivasan | e6e7f84 | 2019-12-16 18:41:36 | [diff] [blame] | 3318 | WebContentsImpl::GetRenderWidgetHostViewsInWebContentsTree() { |
Kevin McNee | ef1e936 | 2021-06-08 21:31:52 | [diff] [blame] | 3319 | std::set<RenderWidgetHostViewBase*> result; |
Daniel Cheng | 982f2b2 | 2022-08-25 23:46:16 | [diff] [blame] | 3320 | GetPrimaryMainFrame()->ForEachRenderFrameHost( |
| 3321 | [&result](RenderFrameHostImpl* rfh) { |
Kevin McNee | ef1e936 | 2021-06-08 21:31:52 | [diff] [blame] | 3322 | if (auto* view = |
| 3323 | static_cast<RenderWidgetHostViewBase*>(rfh->GetView())) { |
| 3324 | result.insert(view); |
| 3325 | } |
Daniel Cheng | 982f2b2 | 2022-08-25 23:46:16 | [diff] [blame] | 3326 | }); |
Adithya Srinivasan | e6e7f84 | 2019-12-16 18:41:36 | [diff] [blame] | 3327 | return result; |
| 3328 | } |
| 3329 | |
[email protected] | b172aee | 2012-04-10 17:05:26 | [diff] [blame] | 3330 | void WebContentsImpl::Activate() { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 3331 | OPTIONAL_TRACE_EVENT0("content", "WebContentsImpl::Activate"); |
[email protected] | d5f942ba | 2008-09-26 19:30:34 | [diff] [blame] | 3332 | if (delegate_) |
| 3333 | delegate_->ActivateContents(this); |
| 3334 | } |
| 3335 | |
Ahmed Fakhry | faa32d2 | 2018-10-05 15:56:16 | [diff] [blame] | 3336 | void WebContentsImpl::SetTopControlsShownRatio( |
| 3337 | RenderWidgetHostImpl* render_widget_host, |
| 3338 | float ratio) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 3339 | OPTIONAL_TRACE_EVENT2("content", "WebContentsImpl::SetTopControlsShownRatio", |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 3340 | "render_widget_host", render_widget_host, "ratio", |
| 3341 | ratio); |
Ahmed Fakhry | faa32d2 | 2018-10-05 15:56:16 | [diff] [blame] | 3342 | if (!delegate_) |
| 3343 | return; |
| 3344 | |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 3345 | RenderFrameHostImpl* rfh = GetPrimaryMainFrame(); |
Ahmed Fakhry | faa32d2 | 2018-10-05 15:56:16 | [diff] [blame] | 3346 | if (!rfh || render_widget_host != rfh->GetRenderWidgetHost()) |
| 3347 | return; |
| 3348 | |
| 3349 | delegate_->SetTopControlsShownRatio(this, ratio); |
Ahmed Fakhry | 58e6ef54 | 2018-09-04 18:05:38 | [diff] [blame] | 3350 | } |
| 3351 | |
Ahmed Fakhry | 58e6ef54 | 2018-09-04 18:05:38 | [diff] [blame] | 3352 | void WebContentsImpl::SetTopControlsGestureScrollInProgress(bool in_progress) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 3353 | OPTIONAL_TRACE_EVENT1( |
| 3354 | "content", "WebContentsImpl::SetTopControlsGestureScrollInProgress", |
| 3355 | "in_progress", in_progress); |
Ahmed Fakhry | 58e6ef54 | 2018-09-04 18:05:38 | [diff] [blame] | 3356 | if (delegate_) |
| 3357 | delegate_->SetTopControlsGestureScrollInProgress(in_progress); |
| 3358 | } |
| 3359 | |
lazyboy | 63f5b31 | 2015-11-23 20:19:52 | [diff] [blame] | 3360 | void WebContentsImpl::RenderWidgetCreated( |
| 3361 | RenderWidgetHostImpl* render_widget_host) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 3362 | OPTIONAL_TRACE_EVENT1("content", "WebContentsImpl::RenderWidgetCreated", |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 3363 | "render_widget_host", render_widget_host); |
lazyboy | 63f5b31 | 2015-11-23 20:19:52 | [diff] [blame] | 3364 | created_widgets_.insert(render_widget_host); |
| 3365 | } |
| 3366 | |
[email protected] | b24b68a | 2012-09-24 21:57:26 | [diff] [blame] | 3367 | void WebContentsImpl::RenderWidgetDeleted( |
| 3368 | RenderWidgetHostImpl* render_widget_host) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 3369 | OPTIONAL_TRACE_EVENT1("content", "WebContentsImpl::RenderWidgetDeleted", |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 3370 | "render_widget_host", render_widget_host); |
Takashi Toyoshima | ea534ef2 | 2021-07-21 03:27:59 | [diff] [blame] | 3371 | // Note that IsBeingDestroyed() can return true at this point as |
lazyboy | 63f5b31 | 2015-11-23 20:19:52 | [diff] [blame] | 3372 | // ~WebContentsImpl() calls RFHM::ClearRFHsPendingShutdown(), which might lead |
| 3373 | // us here. |
| 3374 | created_widgets_.erase(render_widget_host); |
[email protected] | b24b68a | 2012-09-24 21:57:26 | [diff] [blame] | 3375 | |
Takashi Toyoshima | ea534ef2 | 2021-07-21 03:27:59 | [diff] [blame] | 3376 | if (IsBeingDestroyed()) |
lazyboy | 63f5b31 | 2015-11-23 20:19:52 | [diff] [blame] | 3377 | return; |
[email protected] | 44470a2 | 2013-01-24 01:21:54 | [diff] [blame] | 3378 | |
lfg | 7d4caad | 2017-03-22 09:01:45 | [diff] [blame] | 3379 | if (render_widget_host == mouse_lock_widget_) |
lfg | ff8dfc8d | 2016-10-21 17:42:32 | [diff] [blame] | 3380 | LostMouseLock(mouse_lock_widget_); |
Joe Downing | 192998b2 | 2018-03-22 15:51:36 | [diff] [blame] | 3381 | |
Joe Downing | c1a57efe | 2018-05-09 18:23:54 | [diff] [blame] | 3382 | CancelKeyboardLock(render_widget_host); |
[email protected] | b24b68a | 2012-09-24 21:57:26 | [diff] [blame] | 3383 | } |
| 3384 | |
estade | d0a0c99 | 2015-01-21 14:12:06 | [diff] [blame] | 3385 | void WebContentsImpl::RenderWidgetWasResized( |
rouslan | 2f5993f | 2015-01-29 00:18:31 | [diff] [blame] | 3386 | RenderWidgetHostImpl* render_widget_host, |
| 3387 | bool width_changed) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 3388 | OPTIONAL_TRACE_EVENT2("content", "WebContentsImpl::RenderWidgetWasResized", |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 3389 | "render_widget_host", render_widget_host, |
| 3390 | "width_changed", width_changed); |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 3391 | RenderFrameHostImpl* rfh = GetPrimaryMainFrame(); |
estade | ce344ec0 | 2015-01-23 00:12:29 | [diff] [blame] | 3392 | if (!rfh || render_widget_host != rfh->GetRenderWidgetHost()) |
estade | d0a0c99 | 2015-01-21 14:12:06 | [diff] [blame] | 3393 | return; |
estade | d0a0c99 | 2015-01-21 14:12:06 | [diff] [blame] | 3394 | |
Dave Tapuska | 1ca6d5f | 2021-10-13 22:40:03 | [diff] [blame] | 3395 | observers_.NotifyObservers(&WebContentsObserver::PrimaryMainFrameWasResized, |
Andrew Grieve | e19cd93e | 2021-01-22 05:43:06 | [diff] [blame] | 3396 | width_changed); |
estade | d0a0c99 | 2015-01-21 14:12:06 | [diff] [blame] | 3397 | } |
| 3398 | |
sky | f65d9bb | 2017-03-24 02:26:39 | [diff] [blame] | 3399 | KeyboardEventProcessingResult WebContentsImpl::PreHandleKeyboardEvent( |
| 3400 | const NativeWebKeyboardEvent& event) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 3401 | OPTIONAL_TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("content.verbose"), |
| 3402 | "WebContentsImpl::PreHandleKeyboardEvent"); |
W. James MacLean | e118727 | 2019-11-12 03:42:48 | [diff] [blame] | 3403 | auto* outermost_contents = GetOutermostWebContents(); |
| 3404 | // TODO(wjmaclean): Generalize this to forward all key events to the outermost |
| 3405 | // delegate's handler. |
Avi Drissman | 97aef04 | 2020-06-30 21:04:48 | [diff] [blame] | 3406 | if (outermost_contents != this && IsFullscreen() && |
W. James MacLean | e118727 | 2019-11-12 03:42:48 | [diff] [blame] | 3407 | event.windows_key_code == ui::VKEY_ESCAPE) { |
| 3408 | // When an inner WebContents has focus and is fullscreen, redirect <esc> |
| 3409 | // key events to the outermost WebContents so it can be handled by that |
| 3410 | // WebContents' delegate. |
| 3411 | if (outermost_contents->PreHandleKeyboardEvent(event) == |
| 3412 | KeyboardEventProcessingResult::HANDLED) { |
| 3413 | return KeyboardEventProcessingResult::HANDLED; |
| 3414 | } |
| 3415 | } |
sky | f65d9bb | 2017-03-24 02:26:39 | [diff] [blame] | 3416 | return delegate_ ? delegate_->PreHandleKeyboardEvent(this, event) |
| 3417 | : KeyboardEventProcessingResult::NOT_HANDLED; |
[email protected] | 6395479 | 2011-07-11 04:17:48 | [diff] [blame] | 3418 | } |
| 3419 | |
Dave Tapuska | 344399e | 2019-09-09 15:18:26 | [diff] [blame] | 3420 | bool WebContentsImpl::HandleMouseEvent(const blink::WebMouseEvent& event) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 3421 | OPTIONAL_TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("content.verbose"), |
| 3422 | "WebContentsImpl::HandleMouseEvent"); |
Dave Tapuska | 344399e | 2019-09-09 15:18:26 | [diff] [blame] | 3423 | // Handle mouse button back/forward in the browser process after the render |
| 3424 | // process is done with the event. This ensures all renderer-initiated history |
| 3425 | // navigations can be treated consistently. |
| 3426 | if (event.GetType() == blink::WebInputEvent::Type::kMouseUp) { |
| 3427 | WebContentsImpl* outermost = GetOutermostWebContents(); |
| 3428 | if (event.button == blink::WebPointerProperties::Button::kBack && |
Carlos Caballero | 40b0efd | 2021-01-26 11:55:00 | [diff] [blame] | 3429 | outermost->GetController().CanGoBack()) { |
| 3430 | outermost->GetController().GoBack(); |
Dave Tapuska | 344399e | 2019-09-09 15:18:26 | [diff] [blame] | 3431 | return true; |
| 3432 | } else if (event.button == blink::WebPointerProperties::Button::kForward && |
Carlos Caballero | 40b0efd | 2021-01-26 11:55:00 | [diff] [blame] | 3433 | outermost->GetController().CanGoForward()) { |
| 3434 | outermost->GetController().GoForward(); |
Dave Tapuska | 344399e | 2019-09-09 15:18:26 | [diff] [blame] | 3435 | return true; |
| 3436 | } |
| 3437 | } |
| 3438 | return false; |
| 3439 | } |
| 3440 | |
Scott Violet | d5caa87 | 2018-10-16 22:00:51 | [diff] [blame] | 3441 | bool WebContentsImpl::HandleKeyboardEvent(const NativeWebKeyboardEvent& event) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 3442 | OPTIONAL_TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("content.verbose"), |
| 3443 | "WebContentsImpl::HandleKeyboardEvent"); |
[email protected] | 0b09400 | 2014-08-20 18:28:10 | [diff] [blame] | 3444 | if (browser_plugin_embedder_ && |
| 3445 | browser_plugin_embedder_->HandleKeyboardEvent(event)) { |
Scott Violet | d5caa87 | 2018-10-16 22:00:51 | [diff] [blame] | 3446 | return true; |
[email protected] | 0b09400 | 2014-08-20 18:28:10 | [diff] [blame] | 3447 | } |
Scott Violet | d5caa87 | 2018-10-16 22:00:51 | [diff] [blame] | 3448 | return delegate_ && delegate_->HandleKeyboardEvent(this, event); |
[email protected] | 6395479 | 2011-07-11 04:17:48 | [diff] [blame] | 3449 | } |
| 3450 | |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 3451 | bool WebContentsImpl::HandleWheelEvent(const blink::WebMouseWheelEvent& event) { |
| 3452 | OPTIONAL_TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("content.verbose"), |
| 3453 | "WebContentsImpl::HandleWheelEvent"); |
Xiaohan Wang | 24ec934 | 2022-01-15 17:34:22 | [diff] [blame] | 3454 | #if !BUILDFLAG(IS_MAC) |
lanwei | a93644f | 2015-01-21 22:00:33 | [diff] [blame] | 3455 | // On platforms other than Mac, control+mousewheel may change zoom. On Mac, |
| 3456 | // this isn't done for two reasons: |
[email protected] | bccc447 | 2013-04-18 16:37:19 | [diff] [blame] | 3457 | // -the OS already has a gesture to do this through pinch-zoom |
| 3458 | // -if a user starts an inertial scroll, let's go, and presses control |
| 3459 | // (i.e. control+tab) then the OS's buffered scroll events will come in |
| 3460 | // with control key set which isn't what the user wants |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 3461 | if (delegate_ && event.wheel_ticks_y && |
Ehsan Karamad | 0f83cb11 | 2018-09-06 22:07:36 | [diff] [blame] | 3462 | event.event_action == blink::WebMouseWheelEvent::EventAction::kPageZoom) { |
w.shackleton | 49bcd39 | 2016-01-06 17:38:22 | [diff] [blame] | 3463 | // Count only integer cumulative scrolls as zoom events; this handles |
| 3464 | // smooth scroll and regular scroll device behavior. |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 3465 | zoom_scroll_remainder_ += event.wheel_ticks_y; |
w.shackleton | 49bcd39 | 2016-01-06 17:38:22 | [diff] [blame] | 3466 | int whole_zoom_scroll_remainder_ = std::lround(zoom_scroll_remainder_); |
| 3467 | zoom_scroll_remainder_ -= whole_zoom_scroll_remainder_; |
| 3468 | if (whole_zoom_scroll_remainder_ != 0) { |
| 3469 | delegate_->ContentsZoomChange(whole_zoom_scroll_remainder_ > 0); |
| 3470 | } |
[email protected] | fb3f066e | 2013-02-12 19:12:52 | [diff] [blame] | 3471 | return true; |
| 3472 | } |
[email protected] | bccc447 | 2013-04-18 16:37:19 | [diff] [blame] | 3473 | #endif |
[email protected] | fb3f066e | 2013-02-12 19:12:52 | [diff] [blame] | 3474 | return false; |
| 3475 | } |
| 3476 | |
[email protected] | 04bce56 | 2014-01-30 05:34:54 | [diff] [blame] | 3477 | bool WebContentsImpl::PreHandleGestureEvent( |
| 3478 | const blink::WebGestureEvent& event) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 3479 | OPTIONAL_TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("content.verbose"), |
| 3480 | "WebContentsImpl::PreHandleGestureEvent"); |
[email protected] | 28042d3 | 2014-02-03 14:10:03 | [diff] [blame] | 3481 | return delegate_ && delegate_->PreHandleGestureEvent(this, event); |
[email protected] | 04bce56 | 2014-01-30 05:34:54 | [diff] [blame] | 3482 | } |
| 3483 | |
kenrb | 2a565f8 | 2015-09-02 20:24:59 | [diff] [blame] | 3484 | RenderWidgetHostInputEventRouter* WebContentsImpl::GetInputEventRouter() { |
Takashi Toyoshima | ea534ef2 | 2021-07-21 03:27:59 | [diff] [blame] | 3485 | if (!IsBeingDestroyed()) { |
| 3486 | if (GetOuterWebContents()) |
| 3487 | return GetOuterWebContents()->GetInputEventRouter(); |
wjmaclean | e31234f | 2015-12-14 17:20:45 | [diff] [blame] | 3488 | |
Takashi Toyoshima | ea534ef2 | 2021-07-21 03:27:59 | [diff] [blame] | 3489 | if (!rwh_input_event_router_.get()) { |
| 3490 | rwh_input_event_router_ = |
| 3491 | std::make_unique<RenderWidgetHostInputEventRouter>(); |
| 3492 | } |
| 3493 | } |
kenrb | 2a565f8 | 2015-09-02 20:24:59 | [diff] [blame] | 3494 | return rwh_input_event_router_.get(); |
| 3495 | } |
| 3496 | |
alexmos | c4cbacb | 2015-11-21 01:29:22 | [diff] [blame] | 3497 | RenderWidgetHostImpl* WebContentsImpl::GetFocusedRenderWidgetHost( |
| 3498 | RenderWidgetHostImpl* receiving_widget) { |
alexmos | c4cbacb | 2015-11-21 01:29:22 | [diff] [blame] | 3499 | // Events for widgets other than the main frame (e.g., popup menus) should be |
| 3500 | // forwarded directly to the widget they arrived on. |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 3501 | if (receiving_widget != GetPrimaryMainFrame()->GetRenderWidgetHost()) |
alexmos | c4cbacb | 2015-11-21 01:29:22 | [diff] [blame] | 3502 | return receiving_widget; |
alexmos | c4d183e | 2015-11-06 00:46:52 | [diff] [blame] | 3503 | |
paulmeyer | feafc2d | 2017-04-25 21:46:40 | [diff] [blame] | 3504 | // If the focused WebContents is a guest WebContents, then get the focused |
| 3505 | // frame in the embedder WebContents instead. |
Dave Tapuska | d8b0530f | 2021-10-19 15:12:31 | [diff] [blame] | 3506 | FrameTreeNode* focused_frame = GetFocusedFrameTree()->GetFocusedFrame(); |
lfg | 1453e41 | 2017-04-11 00:48:50 | [diff] [blame] | 3507 | |
alexmos | c4d183e | 2015-11-06 00:46:52 | [diff] [blame] | 3508 | if (!focused_frame) |
alexmos | c4cbacb | 2015-11-21 01:29:22 | [diff] [blame] | 3509 | return receiving_widget; |
alexmos | c4d183e | 2015-11-06 00:46:52 | [diff] [blame] | 3510 | |
alexmos | 732ca3a | 2015-12-08 02:01:02 | [diff] [blame] | 3511 | // The view may be null if a subframe's renderer process has crashed while |
| 3512 | // the subframe has focus. Drop the event in that case. Do not give |
| 3513 | // it to the main frame, so that the user doesn't unexpectedly type into the |
| 3514 | // wrong frame if a focused subframe renderer crashes while they type. |
| 3515 | RenderWidgetHostView* view = focused_frame->current_frame_host()->GetView(); |
| 3516 | if (!view) |
| 3517 | return nullptr; |
| 3518 | |
| 3519 | return RenderWidgetHostImpl::From(view->GetRenderWidgetHost()); |
alexmos | c4d183e | 2015-11-06 00:46:52 | [diff] [blame] | 3520 | } |
| 3521 | |
avallee | 9993fca | 2016-11-17 06:16:50 | [diff] [blame] | 3522 | RenderWidgetHostImpl* WebContentsImpl::GetRenderWidgetHostWithPageFocus() { |
Dave Tapuska | 54c76a03 | 2021-10-27 22:10:42 | [diff] [blame] | 3523 | FrameTree* focused_frame_tree = GetFocusedFrameTree(); |
| 3524 | return focused_frame_tree->root() |
| 3525 | ->current_frame_host() |
| 3526 | ->GetRenderWidgetHost(); |
avallee | 9993fca | 2016-11-17 06:16:50 | [diff] [blame] | 3527 | } |
| 3528 | |
Koji Ishii | 640cff72 | 2021-12-03 19:16:55 | [diff] [blame] | 3529 | bool WebContentsImpl::CanEnterFullscreenMode( |
| 3530 | RenderFrameHostImpl* requesting_frame, |
| 3531 | const blink::mojom::FullscreenOptions& options) { |
Avi Drissman | 3397294 | 2020-06-19 14:16:01 | [diff] [blame] | 3532 | // It's possible that this WebContents was spawned while blocking UI was on |
| 3533 | // the screen, or that it was downstream from a WebContents when UI was |
| 3534 | // blocked. Therefore, disqualify it from fullscreen if it or any upstream |
| 3535 | // WebContents has an active blocker. |
Peter Kasting | d568594 | 2022-09-02 17:52:17 | [diff] [blame] | 3536 | return base::ranges::all_of(GetAllOpeningWebContents(this), |
| 3537 | [](auto* opener) { |
| 3538 | return opener->fullscreen_blocker_count_ == 0; |
| 3539 | }) && |
Koji Ishii | 640cff72 | 2021-12-03 19:16:55 | [diff] [blame] | 3540 | delegate_->CanEnterFullscreenModeForTab(requesting_frame, options); |
Avi Drissman | d1d2565 | 2020-03-06 21:28:58 | [diff] [blame] | 3541 | } |
| 3542 | |
Dave Tapuska | 3ed4419 | 2018-05-01 18:53:30 | [diff] [blame] | 3543 | void WebContentsImpl::EnterFullscreenMode( |
Kevin McNee | fb86fcf | 2021-02-26 23:20:57 | [diff] [blame] | 3544 | RenderFrameHostImpl* requesting_frame, |
Dave Tapuska | a418951 | 2019-10-15 20:27:34 | [diff] [blame] | 3545 | const blink::mojom::FullscreenOptions& options) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 3546 | OPTIONAL_TRACE_EVENT0("content", "WebContentsImpl::EnterFullscreenMode"); |
Koji Ishii | 640cff72 | 2021-12-03 19:16:55 | [diff] [blame] | 3547 | DCHECK(CanEnterFullscreenMode(requesting_frame, options)); |
Miyoung Shin | 6db57870 | 2021-09-11 02:38:33 | [diff] [blame] | 3548 | DCHECK(requesting_frame->IsActive()); |
Mike Wasserman | dbf43d9 | 2022-05-20 00:32:53 | [diff] [blame] | 3549 | DCHECK(ContainsOrIsFocusedWebContents()); |
Avi Drissman | d1d2565 | 2020-03-06 21:28:58 | [diff] [blame] | 3550 | |
Jonathan Ross | c1d5cc7 | 2022-11-03 22:09:39 | [diff] [blame] | 3551 | // When WebView is the `delegate_` we can end up with VisualProperties changes |
| 3552 | // synchronously. Notify the view ahead so it can handle the transition. |
| 3553 | if (auto* view = GetRenderWidgetHostView()) |
| 3554 | static_cast<RenderWidgetHostViewBase*>(view)->EnterFullscreenMode(options); |
| 3555 | |
Joe Downing | 13dd76b | 2018-04-09 18:32:15 | [diff] [blame] | 3556 | if (delegate_) { |
Mike Wasserman | 4ca0979 | 2020-05-29 17:44:43 | [diff] [blame] | 3557 | delegate_->EnterFullscreenModeForTab(requesting_frame, options); |
mlamouri | 7a78d6fd | 2015-01-17 13:23:53 | [diff] [blame] | 3558 | |
Joe Downing | 13dd76b | 2018-04-09 18:32:15 | [diff] [blame] | 3559 | if (keyboard_lock_widget_) |
| 3560 | delegate_->RequestKeyboardLock(this, esc_key_locked_); |
| 3561 | } |
| 3562 | |
Andrew Grieve | e19cd93e | 2021-01-22 05:43:06 | [diff] [blame] | 3563 | observers_.NotifyObservers( |
| 3564 | &WebContentsObserver::DidToggleFullscreenModeForTab, IsFullscreen(), |
| 3565 | false); |
Avi Drissman | 3397294 | 2020-06-19 14:16:01 | [diff] [blame] | 3566 | FullscreenContentsSet(GetBrowserContext())->insert(this); |
mlamouri | 7a78d6fd | 2015-01-17 13:23:53 | [diff] [blame] | 3567 | } |
| 3568 | |
bokan | ece34a8 | 2016-01-28 19:49:46 | [diff] [blame] | 3569 | void WebContentsImpl::ExitFullscreenMode(bool will_cause_resize) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 3570 | OPTIONAL_TRACE_EVENT1("content", "WebContentsImpl::ExitFullscreenMode", |
| 3571 | "will_cause_resize", will_cause_resize); |
Jonathan Ross | c1d5cc7 | 2022-11-03 22:09:39 | [diff] [blame] | 3572 | // When WebView is the `delegate_` we can end up with VisualProperties changes |
| 3573 | // synchronously. Notify the view ahead so it can handle the transition. |
| 3574 | if (auto* view = GetRenderWidgetHostView()) |
| 3575 | static_cast<RenderWidgetHostViewBase*>(view)->ExitFullscreenMode(); |
| 3576 | |
Joe Downing | 13dd76b | 2018-04-09 18:32:15 | [diff] [blame] | 3577 | if (delegate_) { |
mlamouri | 7a78d6fd | 2015-01-17 13:23:53 | [diff] [blame] | 3578 | delegate_->ExitFullscreenModeForTab(this); |
[email protected] | 657c8e0 | 2014-03-19 19:18:37 | [diff] [blame] | 3579 | |
Joe Downing | 13dd76b | 2018-04-09 18:32:15 | [diff] [blame] | 3580 | if (keyboard_lock_widget_) |
| 3581 | delegate_->CancelKeyboardLockRequest(this); |
| 3582 | } |
| 3583 | |
bokan | ece34a8 | 2016-01-28 19:49:46 | [diff] [blame] | 3584 | // The fullscreen state is communicated to the renderer through a resize |
| 3585 | // message. If the change in fullscreen state doesn't cause a view resize |
| 3586 | // then we must ensure web contents exit the fullscreen state by explicitly |
| 3587 | // sending a resize message. This is required for the situation of the browser |
| 3588 | // moving the view into a "browser fullscreen" state and then the contents |
| 3589 | // entering "tab fullscreen". Exiting the contents "tab fullscreen" then won't |
| 3590 | // have the side effect of the view resizing, hence the explicit call here is |
| 3591 | // required. |
| 3592 | if (!will_cause_resize) { |
| 3593 | if (RenderWidgetHostView* rwhv = GetRenderWidgetHostView()) { |
Ian Vollick | 6d75ac3 | 2021-05-05 17:45:09 | [diff] [blame] | 3594 | if (RenderWidgetHost* render_widget_host = rwhv->GetRenderWidgetHost()) |
| 3595 | render_widget_host->SynchronizeVisualProperties(); |
bokan | ece34a8 | 2016-01-28 19:49:46 | [diff] [blame] | 3596 | } |
scheib | 3dcb6c93 | 2015-02-23 10:55:58 | [diff] [blame] | 3597 | } |
| 3598 | |
Arthur Sonzogni | 929d29f | 2018-09-17 14:19:44 | [diff] [blame] | 3599 | current_fullscreen_frame_ = nullptr; |
Becca Hughes | e9e2795 | 2018-06-25 17:08:27 | [diff] [blame] | 3600 | |
Andrew Grieve | e19cd93e | 2021-01-22 05:43:06 | [diff] [blame] | 3601 | observers_.NotifyObservers( |
| 3602 | &WebContentsObserver::DidToggleFullscreenModeForTab, IsFullscreen(), |
| 3603 | will_cause_resize); |
Becca Hughes | d11d650 | 2018-07-31 17:01:28 | [diff] [blame] | 3604 | |
| 3605 | if (display_cutout_host_impl_) |
| 3606 | display_cutout_host_impl_->DidExitFullscreen(); |
Avi Drissman | 3397294 | 2020-06-19 14:16:01 | [diff] [blame] | 3607 | |
| 3608 | FullscreenContentsSet(GetBrowserContext())->erase(this); |
[email protected] | 8a5e0ca | 2011-08-25 06:30:47 | [diff] [blame] | 3609 | } |
| 3610 | |
Batalov Vladislav | e9a5f382 | 2020-12-02 07:31:11 | [diff] [blame] | 3611 | void WebContentsImpl::FullscreenStateChanged( |
Kevin McNee | fb86fcf | 2021-02-26 23:20:57 | [diff] [blame] | 3612 | RenderFrameHostImpl* rfh, |
Batalov Vladislav | e9a5f382 | 2020-12-02 07:31:11 | [diff] [blame] | 3613 | bool is_fullscreen, |
| 3614 | blink::mojom::FullscreenOptionsPtr options) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 3615 | OPTIONAL_TRACE_EVENT2("content", "WebContentsImpl::FullscreenStateChanged", |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 3616 | "render_frame_host", rfh, "is_fullscreen", |
| 3617 | is_fullscreen); |
Becca Hughes | fd5d8f89 | 2018-06-14 18:23:36 | [diff] [blame] | 3618 | |
Jinsuk Kim | 726a0452 | 2022-08-25 18:25:37 | [diff] [blame] | 3619 | GetView()->FullscreenStateChanged(is_fullscreen); |
| 3620 | |
Becca Hughes | fd5d8f89 | 2018-06-14 18:23:36 | [diff] [blame] | 3621 | if (is_fullscreen) { |
Batalov Vladislav | e9a5f382 | 2020-12-02 07:31:11 | [diff] [blame] | 3622 | if (options.is_null()) { |
| 3623 | ReceivedBadMessage(rfh->GetProcess(), |
| 3624 | bad_message::WCI_INVALID_FULLSCREEN_OPTIONS); |
| 3625 | return; |
| 3626 | } |
| 3627 | |
| 3628 | if (delegate_) |
Kevin McNee | fb86fcf | 2021-02-26 23:20:57 | [diff] [blame] | 3629 | delegate_->FullscreenStateChangedForTab(rfh, *options); |
Batalov Vladislav | e9a5f382 | 2020-12-02 07:31:11 | [diff] [blame] | 3630 | |
Kevin McNee | fb86fcf | 2021-02-26 23:20:57 | [diff] [blame] | 3631 | if (!base::Contains(fullscreen_frames_, rfh)) { |
| 3632 | fullscreen_frames_.insert(rfh); |
Arthur Sonzogni | 929d29f | 2018-09-17 14:19:44 | [diff] [blame] | 3633 | FullscreenFrameSetUpdated(); |
Becca Hughes | fd5d8f89 | 2018-06-14 18:23:36 | [diff] [blame] | 3634 | } |
Arthur Sonzogni | 929d29f | 2018-09-17 14:19:44 | [diff] [blame] | 3635 | return; |
| 3636 | } |
Becca Hughes | fd5d8f89 | 2018-06-14 18:23:36 | [diff] [blame] | 3637 | |
Kevin McNee | fb86fcf | 2021-02-26 23:20:57 | [diff] [blame] | 3638 | // If |rfh| is no longer in fullscreen, remove it and any descendants. |
Arthur Sonzogni | 929d29f | 2018-09-17 14:19:44 | [diff] [blame] | 3639 | // See https://fullscreen.spec.whatwg.org. |
Arthur Sonzogni | 969a794e | 2018-10-11 08:56:09 | [diff] [blame] | 3640 | size_t size_before_deletion = fullscreen_frames_.size(); |
| 3641 | base::EraseIf(fullscreen_frames_, [&](RenderFrameHostImpl* current) { |
Dave Tapuska | 8c4c8376 | 2021-09-15 21:31:52 | [diff] [blame] | 3642 | while (current) { |
| 3643 | if (current == rfh) |
| 3644 | return true; |
| 3645 | // We only look for direct parents. fencedframes and portals will not |
| 3646 | // enter fullscreen. |
| 3647 | current = current->GetParent(); |
| 3648 | } |
| 3649 | return false; |
Arthur Sonzogni | 929d29f | 2018-09-17 14:19:44 | [diff] [blame] | 3650 | }); |
| 3651 | |
Arthur Sonzogni | 969a794e | 2018-10-11 08:56:09 | [diff] [blame] | 3652 | if (size_before_deletion != fullscreen_frames_.size()) |
Arthur Sonzogni | 929d29f | 2018-09-17 14:19:44 | [diff] [blame] | 3653 | FullscreenFrameSetUpdated(); |
| 3654 | } |
| 3655 | |
| 3656 | void WebContentsImpl::FullscreenFrameSetUpdated() { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 3657 | OPTIONAL_TRACE_EVENT0("content", |
| 3658 | "WebContentsImpl::FullscreenFrameSetUpdated"); |
Arthur Sonzogni | 929d29f | 2018-09-17 14:19:44 | [diff] [blame] | 3659 | if (fullscreen_frames_.empty()) { |
| 3660 | current_fullscreen_frame_ = nullptr; |
| 3661 | return; |
Becca Hughes | fd5d8f89 | 2018-06-14 18:23:36 | [diff] [blame] | 3662 | } |
| 3663 | |
Arthur Sonzogni | 929d29f | 2018-09-17 14:19:44 | [diff] [blame] | 3664 | // Find the current fullscreen frame and call the observers. |
| 3665 | // If frame A is fullscreen, then frame B goes into inner fullscreen, then B |
| 3666 | // exits fullscreen - that will result in A being fullscreen. |
| 3667 | RenderFrameHostImpl* new_fullscreen_frame = *std::max_element( |
| 3668 | fullscreen_frames_.begin(), fullscreen_frames_.end(), FrameCompareDepth); |
Becca Hughes | fd5d8f89 | 2018-06-14 18:23:36 | [diff] [blame] | 3669 | |
Arthur Sonzogni | 929d29f | 2018-09-17 14:19:44 | [diff] [blame] | 3670 | // If we have already notified observers about this frame then we should not |
| 3671 | // fire the observers again. |
| 3672 | if (new_fullscreen_frame == current_fullscreen_frame_) |
| 3673 | return; |
| 3674 | current_fullscreen_frame_ = new_fullscreen_frame; |
Becca Hughes | fd5d8f89 | 2018-06-14 18:23:36 | [diff] [blame] | 3675 | |
Andrew Grieve | e19cd93e | 2021-01-22 05:43:06 | [diff] [blame] | 3676 | observers_.NotifyObservers(&WebContentsObserver::DidAcquireFullscreen, |
| 3677 | new_fullscreen_frame); |
Arthur Sonzogni | 929d29f | 2018-09-17 14:19:44 | [diff] [blame] | 3678 | if (display_cutout_host_impl_) |
| 3679 | display_cutout_host_impl_->DidAcquireFullscreen(new_fullscreen_frame); |
Becca Hughes | fd5d8f89 | 2018-06-14 18:23:36 | [diff] [blame] | 3680 | } |
| 3681 | |
Tsuyoshi Horo | cd82846 | 2021-04-21 04:59:58 | [diff] [blame] | 3682 | PageVisibilityState WebContentsImpl::CalculatePageVisibilityState( |
| 3683 | Visibility visibility) const { |
Collin Baker | 989e088 | 2019-11-01 01:27:17 | [diff] [blame] | 3684 | // Only hide the page if there are no entities capturing screenshots |
David Bienvenu | 1cdcf09 | 2020-11-04 18:47:56 | [diff] [blame] | 3685 | // or video (e.g. mirroring or WebXR). If there are, apply the correct state |
| 3686 | // of kHidden or kHiddenButPainting. |
| 3687 | bool web_contents_visible_in_vr = false; |
| 3688 | #if BUILDFLAG(ENABLE_VR) |
| 3689 | web_contents_visible_in_vr = |
| 3690 | XRRuntimeManagerImpl::GetImmersiveSessionWebContents() == this; |
| 3691 | #endif |
| 3692 | |
Frank Liberato | 9c28010b | 2021-11-05 21:18:32 | [diff] [blame] | 3693 | // If there are entities in Picture-in-Picture mode, don't activate the |
| 3694 | // "disable rendering" optimization, since it still needs to drive the video, |
| 3695 | // and possibly other elements on the page like canvas, to keep the picture in |
| 3696 | // picture window up to date. |
Tsuyoshi Horo | cd82846 | 2021-04-21 04:59:58 | [diff] [blame] | 3697 | if (visibility == Visibility::VISIBLE || visible_capturer_count_ > 0 || |
David Bienvenu | 1cdcf09 | 2020-11-04 18:47:56 | [diff] [blame] | 3698 | web_contents_visible_in_vr) { |
Tsuyoshi Horo | cd82846 | 2021-04-21 04:59:58 | [diff] [blame] | 3699 | return PageVisibilityState::kVisible; |
Fred Shih | eb7d2ff | 2023-01-18 02:31:46 | [diff] [blame] | 3700 | } else if (hidden_capturer_count_ > 0) { |
Tsuyoshi Horo | cd82846 | 2021-04-21 04:59:58 | [diff] [blame] | 3701 | return PageVisibilityState::kHiddenButPainting; |
David Bienvenu | 1cdcf09 | 2020-11-04 18:47:56 | [diff] [blame] | 3702 | } |
Tsuyoshi Horo | cd82846 | 2021-04-21 04:59:58 | [diff] [blame] | 3703 | return PageVisibilityState::kHidden; |
| 3704 | } |
| 3705 | |
| 3706 | PageVisibilityState WebContentsImpl::GetPageVisibilityState() const { |
| 3707 | return CalculatePageVisibilityState(visibility_); |
| 3708 | } |
| 3709 | |
| 3710 | void WebContentsImpl::UpdateVisibilityAndNotifyPageAndView( |
Elad Alon | 0feb660 | 2021-10-14 09:26:36 | [diff] [blame] | 3711 | Visibility new_visibility, |
| 3712 | bool is_activity) { |
Kevin McNee | d7fc6d05 | 2022-02-08 21:21:50 | [diff] [blame] | 3713 | DCHECK(!IsBeingDestroyed()); |
| 3714 | |
Tsuyoshi Horo | cd82846 | 2021-04-21 04:59:58 | [diff] [blame] | 3715 | PageVisibilityState page_visibility = |
| 3716 | CalculatePageVisibilityState(new_visibility); |
| 3717 | |
Frank Liberato | 9c28010b | 2021-11-05 21:18:32 | [diff] [blame] | 3718 | // A crashed frame might be covered by a sad tab. See docs on SadTabHelper |
| 3719 | // exactly when it is or isn't. Either way, don't make it visible. |
David Bienvenu | 1cdcf09 | 2020-11-04 18:47:56 | [diff] [blame] | 3720 | bool view_is_visible = |
Frank Liberato | 9c28010b | 2021-11-05 21:18:32 | [diff] [blame] | 3721 | !IsCrashed() && page_visibility != PageVisibilityState::kHidden; |
Collin Baker | 989e088 | 2019-11-01 01:27:17 | [diff] [blame] | 3722 | |
Dave Tapuska | 05fcfb6 | 2021-10-18 17:09:03 | [diff] [blame] | 3723 | // Prerendering relies on overriding FrameTree::Delegate::IsHidden, |
| 3724 | // while for other frame trees FrameTree::Delegate::IsHidden |
| 3725 | // resolves to WebContents' visibility, so we avoid Prerender RennderViewHosts |
| 3726 | // here. |
| 3727 | ForEachRenderViewHostTypes view_mask = |
| 3728 | static_cast<ForEachRenderViewHostTypes>( |
| 3729 | ForEachRenderViewHostTypes::kBackForwardCacheViews | |
| 3730 | ForEachRenderViewHostTypes::kActiveViews); |
| 3731 | |
| 3732 | RenderViewHostIterationCallback update_frame_tree_visibility = |
| 3733 | base::BindRepeating( |
| 3734 | [](PageVisibilityState page_visibility, |
| 3735 | RenderViewHostImpl* render_view_host) { |
| 3736 | render_view_host->SetFrameTreeVisibility(page_visibility); |
| 3737 | }, |
| 3738 | page_visibility); |
Collin Baker | c525996 | 2019-11-14 17:51:58 | [diff] [blame] | 3739 | if (page_visibility != PageVisibilityState::kHidden) { |
Dana Fried | cf3bc8e | 2019-11-13 20:00:24 | [diff] [blame] | 3740 | // We cannot show a page or capture video unless there is a valid renderer |
| 3741 | // associated with this web contents. The navigation controller for this |
| 3742 | // page must be set to active (allowing navigation to complete, a renderer |
| 3743 | // and its associated views to be created, etc.) if any of these conditions |
| 3744 | // holds. |
| 3745 | // |
| 3746 | // Previously, it was possible for browser-side code to try to capture video |
| 3747 | // from a restored tab (for a variety of reasons, including the browser |
| 3748 | // creating preview thumbnails) and the tab would never actually load. By |
Collin Baker | c525996 | 2019-11-14 17:51:58 | [diff] [blame] | 3749 | // keying this behavior off of |page_visibility| instead of just |
Dana Fried | cf3bc8e | 2019-11-13 20:00:24 | [diff] [blame] | 3750 | // |new_visibility| we avoid this case. See crbug.com/1020782 for more |
| 3751 | // context. |
Carlos Caballero | 40b0efd | 2021-01-26 11:55:00 | [diff] [blame] | 3752 | GetController().SetActive(true); |
Dana Fried | cf3bc8e | 2019-11-13 20:00:24 | [diff] [blame] | 3753 | |
| 3754 | // This shows the Page before showing the individual RenderWidgets, as |
| 3755 | // RenderWidgets will work to produce compositor frames and handle input |
| 3756 | // as soon as they are shown. But the Page and other classes do not expect |
| 3757 | // to be producing frames when the Page is hidden. So we make sure the Page |
| 3758 | // is shown first. |
Dave Tapuska | 05fcfb6 | 2021-10-18 17:09:03 | [diff] [blame] | 3759 | ForEachRenderViewHost(view_mask, update_frame_tree_visibility); |
Collin Baker | 98457b5 | 2019-11-06 21:34:29 | [diff] [blame] | 3760 | } |
Collin Baker | 989e088 | 2019-11-01 01:27:17 | [diff] [blame] | 3761 | |
| 3762 | // |GetRenderWidgetHostView()| can be null if the user middle clicks a link to |
| 3763 | // open a tab in the background, then closes the tab before selecting it. |
| 3764 | // This is because closing the tab calls WebContentsImpl::Destroy(), which |
| 3765 | // removes the |GetRenderViewHost()|; then when we actually destroy the |
| 3766 | // window, OnWindowPosChanged() notices and calls WasHidden() (which |
| 3767 | // calls us). |
| 3768 | if (auto* view = GetRenderWidgetHostView()) { |
| 3769 | if (view_is_visible) { |
David Bienvenu | c40de89 | 2021-01-28 00:12:14 | [diff] [blame] | 3770 | static_cast<RenderWidgetHostViewBase*>(view)->ShowWithVisibility( |
Joe Mason | 3f70f47 | 2021-11-05 15:26:50 | [diff] [blame] | 3771 | page_visibility); |
Collin Baker | 989e088 | 2019-11-01 01:27:17 | [diff] [blame] | 3772 | } else if (new_visibility == Visibility::HIDDEN) { |
| 3773 | view->Hide(); |
| 3774 | } else { |
| 3775 | view->WasOccluded(); |
| 3776 | } |
| 3777 | } |
| 3778 | |
Carlos IL | 07f282f | 2020-05-15 22:58:29 | [diff] [blame] | 3779 | SetVisibilityForChildViews(view_is_visible); |
Collin Baker | 989e088 | 2019-11-01 01:27:17 | [diff] [blame] | 3780 | |
| 3781 | // Make sure to call SetVisibilityAndNotifyObservers(VISIBLE) before notifying |
| 3782 | // the CrossProcessFrameConnector. |
| 3783 | if (new_visibility == Visibility::VISIBLE) { |
Elad Alon | 0feb660 | 2021-10-14 09:26:36 | [diff] [blame] | 3784 | if (is_activity) { |
| 3785 | last_active_time_ = base::TimeTicks::Now(); |
| 3786 | } |
Collin Baker | 989e088 | 2019-11-01 01:27:17 | [diff] [blame] | 3787 | SetVisibilityAndNotifyObservers(new_visibility); |
| 3788 | } |
| 3789 | |
Collin Baker | c525996 | 2019-11-14 17:51:58 | [diff] [blame] | 3790 | if (page_visibility == PageVisibilityState::kHidden) { |
Collin Baker | 989e088 | 2019-11-01 01:27:17 | [diff] [blame] | 3791 | // Similar to when showing the page, we only hide the page after |
| 3792 | // hiding the individual RenderWidgets. |
Dave Tapuska | 05fcfb6 | 2021-10-18 17:09:03 | [diff] [blame] | 3793 | ForEachRenderViewHost(view_mask, update_frame_tree_visibility); |
Collin Baker | 989e088 | 2019-11-01 01:27:17 | [diff] [blame] | 3794 | } else { |
Dave Tapuska | 05fcfb6 | 2021-10-18 17:09:03 | [diff] [blame] | 3795 | for (FrameTreeNode* node : |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 3796 | FrameTree::SubtreeAndInnerTreeNodes(GetPrimaryMainFrame())) { |
Dave Tapuska | 05fcfb6 | 2021-10-18 17:09:03 | [diff] [blame] | 3797 | RenderFrameProxyHost* proxy_to_parent_or_outer_delegate = |
| 3798 | node->render_manager()->GetProxyToParentOrOuterDelegate(); |
| 3799 | if (!proxy_to_parent_or_outer_delegate) |
Collin Baker | 989e088 | 2019-11-01 01:27:17 | [diff] [blame] | 3800 | continue; |
| 3801 | |
Dave Tapuska | 05fcfb6 | 2021-10-18 17:09:03 | [diff] [blame] | 3802 | // DelegateWasShown keeps track of crash metrics. This is safe to |
| 3803 | // call for GuestViews, and inner frame trees. |
| 3804 | proxy_to_parent_or_outer_delegate->cross_process_frame_connector() |
| 3805 | ->DelegateWasShown(); |
Collin Baker | 989e088 | 2019-11-01 01:27:17 | [diff] [blame] | 3806 | } |
| 3807 | } |
| 3808 | |
| 3809 | if (new_visibility != Visibility::VISIBLE) |
| 3810 | SetVisibilityAndNotifyObservers(new_visibility); |
| 3811 | } |
| 3812 | |
Michael Thiessen | e566352 | 2022-05-25 21:23:28 | [diff] [blame] | 3813 | #if BUILDFLAG(IS_ANDROID) |
| 3814 | void WebContentsImpl::UpdateUserGestureCarryoverInfo() { |
| 3815 | OPTIONAL_TRACE_EVENT0("content", |
| 3816 | "WebContentsImpl::UpdateUserGestureCarryoverInfo"); |
| 3817 | if (delegate_) |
| 3818 | delegate_->UpdateUserGestureCarryoverInfo(this); |
| 3819 | } |
| 3820 | #endif |
| 3821 | |
Avi Drissman | 97aef04 | 2020-06-30 21:04:48 | [diff] [blame] | 3822 | bool WebContentsImpl::IsFullscreen() { |
[email protected] | 199bba6e | 2012-04-04 16:19:38 | [diff] [blame] | 3823 | return delegate_ ? delegate_->IsFullscreenForTabOrPending(this) : false; |
[email protected] | 5d5f7af | 2011-10-01 01:38:12 | [diff] [blame] | 3824 | } |
| 3825 | |
Malay Keshav | 87c42c0 | 2019-01-15 08:37:47 | [diff] [blame] | 3826 | bool WebContentsImpl::ShouldShowStaleContentOnEviction() { |
| 3827 | return GetDelegate() && GetDelegate()->ShouldShowStaleContentOnEviction(this); |
| 3828 | } |
| 3829 | |
danakj | 45d4245 | 2020-04-08 17:24:18 | [diff] [blame] | 3830 | blink::mojom::DisplayMode WebContentsImpl::GetDisplayMode() const { |
mikhail.pozdnyakov | c0e251b | 2015-04-15 06:51:12 | [diff] [blame] | 3831 | return delegate_ ? delegate_->GetDisplayMode(this) |
Eric Willigers | 052f043 | 2019-10-04 04:06:57 | [diff] [blame] | 3832 | : blink::mojom::DisplayMode::kBrowser; |
mikhail.pozdnyakov | c0e251b | 2015-04-15 06:51:12 | [diff] [blame] | 3833 | } |
| 3834 | |
avi | c3aa842 | 2015-11-09 20:57:22 | [diff] [blame] | 3835 | void WebContentsImpl::RequestToLockMouse( |
| 3836 | RenderWidgetHostImpl* render_widget_host, |
| 3837 | bool user_gesture, |
lfg | ad824435 | 2016-07-13 16:55:51 | [diff] [blame] | 3838 | bool last_unlocked_by_target, |
| 3839 | bool privileged) { |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 3840 | OPTIONAL_TRACE_EVENT2("content", "WebContentsImpl::RequestToLockMouse", |
| 3841 | "render_widget_host", render_widget_host, "privileged", |
| 3842 | privileged); |
Tsuyoshi Horo | 0c9728f | 2021-12-03 16:50:04 | [diff] [blame] | 3843 | if (render_widget_host->frame_tree()->type() == |
| 3844 | FrameTree::Type::kFencedFrame) { |
| 3845 | // The renderer should have checked and disallowed the request for fenced |
| 3846 | // frames in PointerLockController and dispatched pointerlockerror. Ignore |
| 3847 | // the request and mark it as bad if it didn't happen for some reason. |
| 3848 | ReceivedBadMessage(render_widget_host->GetProcess(), |
| 3849 | bad_message::WCI_REQUEST_LOCK_MOUSE_FENCED_FRAME); |
| 3850 | return; |
| 3851 | } |
lfg | f0cd46e | 2017-01-04 00:05:23 | [diff] [blame] | 3852 | for (WebContentsImpl* current = this; current; |
| 3853 | current = current->GetOuterWebContents()) { |
| 3854 | if (current->mouse_lock_widget_) { |
James Hollyer | d5c9de46 | 2020-03-10 19:02:45 | [diff] [blame] | 3855 | render_widget_host->GotResponseToLockMouseRequest( |
| 3856 | blink::mojom::PointerLockResult::kAlreadyLocked); |
lfg | f0cd46e | 2017-01-04 00:05:23 | [diff] [blame] | 3857 | return; |
| 3858 | } |
[email protected] | e962111 | 2011-10-17 05:38:37 | [diff] [blame] | 3859 | } |
avi | c3aa842 | 2015-11-09 20:57:22 | [diff] [blame] | 3860 | |
lfg | ad824435 | 2016-07-13 16:55:51 | [diff] [blame] | 3861 | if (privileged) { |
lfg | f0cd46e | 2017-01-04 00:05:23 | [diff] [blame] | 3862 | DCHECK(!GetOuterWebContents()); |
lfg | ad824435 | 2016-07-13 16:55:51 | [diff] [blame] | 3863 | mouse_lock_widget_ = render_widget_host; |
James Hollyer | d5c9de46 | 2020-03-10 19:02:45 | [diff] [blame] | 3864 | render_widget_host->GotResponseToLockMouseRequest( |
| 3865 | blink::mojom::PointerLockResult::kSuccess); |
lfg | ad824435 | 2016-07-13 16:55:51 | [diff] [blame] | 3866 | return; |
| 3867 | } |
| 3868 | |
lfg | bee1e0a | 2016-06-08 21:24:21 | [diff] [blame] | 3869 | bool widget_in_frame_tree = false; |
Carlos Caballero | b65b6e3a | 2021-11-15 10:09:00 | [diff] [blame] | 3870 | for (FrameTreeNode* node : primary_frame_tree_.Nodes()) { |
lfg | bee1e0a | 2016-06-08 21:24:21 | [diff] [blame] | 3871 | if (node->current_frame_host()->GetRenderWidgetHost() == |
| 3872 | render_widget_host) { |
| 3873 | widget_in_frame_tree = true; |
| 3874 | break; |
| 3875 | } |
| 3876 | } |
| 3877 | |
| 3878 | if (widget_in_frame_tree && delegate_) { |
lfg | f0cd46e | 2017-01-04 00:05:23 | [diff] [blame] | 3879 | for (WebContentsImpl* current = this; current; |
| 3880 | current = current->GetOuterWebContents()) { |
| 3881 | current->mouse_lock_widget_ = render_widget_host; |
| 3882 | } |
| 3883 | |
avi | c3aa842 | 2015-11-09 20:57:22 | [diff] [blame] | 3884 | delegate_->RequestToLockMouse(this, user_gesture, last_unlocked_by_target); |
lfg | bee1e0a | 2016-06-08 21:24:21 | [diff] [blame] | 3885 | } else { |
James Hollyer | d5c9de46 | 2020-03-10 19:02:45 | [diff] [blame] | 3886 | render_widget_host->GotResponseToLockMouseRequest( |
| 3887 | blink::mojom::PointerLockResult::kWrongDocument); |
lfg | bee1e0a | 2016-06-08 21:24:21 | [diff] [blame] | 3888 | } |
[email protected] | e962111 | 2011-10-17 05:38:37 | [diff] [blame] | 3889 | } |
| 3890 | |
avi | 3e4b851 | 2015-11-11 01:55:49 | [diff] [blame] | 3891 | void WebContentsImpl::LostMouseLock(RenderWidgetHostImpl* render_widget_host) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 3892 | OPTIONAL_TRACE_EVENT1("content", "WebContentsImpl::LostMouseLock", |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 3893 | "render_widget_host", render_widget_host); |
lfg | bee1e0a | 2016-06-08 21:24:21 | [diff] [blame] | 3894 | CHECK(mouse_lock_widget_); |
lfg | f0cd46e | 2017-01-04 00:05:23 | [diff] [blame] | 3895 | |
Matt Falkenhagen | 888db59 | 2021-09-07 20:20:25 | [diff] [blame] | 3896 | if (WebContentsImpl::FromRenderWidgetHostImpl(mouse_lock_widget_) != this) |
lfg | f0cd46e | 2017-01-04 00:05:23 | [diff] [blame] | 3897 | return mouse_lock_widget_->delegate()->LostMouseLock(render_widget_host); |
| 3898 | |
lfg | bee1e0a | 2016-06-08 21:24:21 | [diff] [blame] | 3899 | mouse_lock_widget_->SendMouseLockLost(); |
lfg | f0cd46e | 2017-01-04 00:05:23 | [diff] [blame] | 3900 | for (WebContentsImpl* current = this; current; |
| 3901 | current = current->GetOuterWebContents()) { |
| 3902 | current->mouse_lock_widget_ = nullptr; |
| 3903 | } |
avi | 3e4b851 | 2015-11-11 01:55:49 | [diff] [blame] | 3904 | |
[email protected] | e962111 | 2011-10-17 05:38:37 | [diff] [blame] | 3905 | if (delegate_) |
| 3906 | delegate_->LostMouseLock(); |
| 3907 | } |
| 3908 | |
lfg | ad824435 | 2016-07-13 16:55:51 | [diff] [blame] | 3909 | bool WebContentsImpl::HasMouseLock(RenderWidgetHostImpl* render_widget_host) { |
| 3910 | // To verify if the mouse is locked, the mouse_lock_widget_ needs to be |
| 3911 | // assigned to the widget that requested the mouse lock, and the top-level |
| 3912 | // platform RenderWidgetHostView needs to hold the mouse lock from the OS. |
Lucas Gadani | a0ea017 | 2018-09-20 18:31:37 | [diff] [blame] | 3913 | auto* widget_host = GetTopLevelRenderWidgetHostView(); |
| 3914 | return mouse_lock_widget_ == render_widget_host && widget_host && |
| 3915 | widget_host->IsMouseLocked(); |
lfg | ad824435 | 2016-07-13 16:55:51 | [diff] [blame] | 3916 | } |
| 3917 | |
lfg | 84763c9 | 2017-02-16 18:55:15 | [diff] [blame] | 3918 | RenderWidgetHostImpl* WebContentsImpl::GetMouseLockWidget() { |
Lucas Gadani | a0ea017 | 2018-09-20 18:31:37 | [diff] [blame] | 3919 | auto* widget_host = GetTopLevelRenderWidgetHostView(); |
Dave Tapuska | 406647a | 2020-10-21 00:55:57 | [diff] [blame] | 3920 | if (widget_host && widget_host->IsMouseLocked()) { |
lfg | 84763c9 | 2017-02-16 18:55:15 | [diff] [blame] | 3921 | return mouse_lock_widget_; |
Lucas Gadani | a0ea017 | 2018-09-20 18:31:37 | [diff] [blame] | 3922 | } |
lfg | 84763c9 | 2017-02-16 18:55:15 | [diff] [blame] | 3923 | |
| 3924 | return nullptr; |
| 3925 | } |
| 3926 | |
Joe Downing | 192998b2 | 2018-03-22 15:51:36 | [diff] [blame] | 3927 | bool WebContentsImpl::RequestKeyboardLock( |
Joe Downing | 13dd76b | 2018-04-09 18:32:15 | [diff] [blame] | 3928 | RenderWidgetHostImpl* render_widget_host, |
| 3929 | bool esc_key_locked) { |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 3930 | OPTIONAL_TRACE_EVENT2("content", "WebContentsImpl::RequestKeyboardLock", |
| 3931 | "render_widget_host", render_widget_host, |
| 3932 | "esc_key_locked", esc_key_locked); |
Joe Downing | 192998b2 | 2018-03-22 15:51:36 | [diff] [blame] | 3933 | DCHECK(render_widget_host); |
Matt Falkenhagen | 888db59 | 2021-09-07 20:20:25 | [diff] [blame] | 3934 | if (WebContentsImpl::FromRenderWidgetHostImpl(render_widget_host) != this) { |
Joe Downing | 192998b2 | 2018-03-22 15:51:36 | [diff] [blame] | 3935 | NOTREACHED(); |
| 3936 | return false; |
| 3937 | } |
| 3938 | |
| 3939 | // KeyboardLock is only supported when called by the top-level browsing |
| 3940 | // context and is not supported in embedded content scenarios. |
| 3941 | if (GetOuterWebContents()) |
| 3942 | return false; |
| 3943 | |
Joe Downing | 13dd76b | 2018-04-09 18:32:15 | [diff] [blame] | 3944 | esc_key_locked_ = esc_key_locked; |
Joe Downing | 192998b2 | 2018-03-22 15:51:36 | [diff] [blame] | 3945 | keyboard_lock_widget_ = render_widget_host; |
| 3946 | |
Joe Downing | 13dd76b | 2018-04-09 18:32:15 | [diff] [blame] | 3947 | if (delegate_) |
| 3948 | delegate_->RequestKeyboardLock(this, esc_key_locked_); |
Joe Downing | 192998b2 | 2018-03-22 15:51:36 | [diff] [blame] | 3949 | return true; |
| 3950 | } |
| 3951 | |
| 3952 | void WebContentsImpl::CancelKeyboardLock( |
| 3953 | RenderWidgetHostImpl* render_widget_host) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 3954 | OPTIONAL_TRACE_EVENT1("content", "WebContentsImpl::CancelKeyboardLockRequest", |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 3955 | "render_widget_host", render_widget_host); |
Joe Downing | 192998b2 | 2018-03-22 15:51:36 | [diff] [blame] | 3956 | if (!keyboard_lock_widget_ || render_widget_host != keyboard_lock_widget_) |
| 3957 | return; |
| 3958 | |
| 3959 | RenderWidgetHostImpl* old_keyboard_lock_widget = keyboard_lock_widget_; |
| 3960 | keyboard_lock_widget_ = nullptr; |
| 3961 | |
Joe Downing | 13dd76b | 2018-04-09 18:32:15 | [diff] [blame] | 3962 | if (delegate_) |
| 3963 | delegate_->CancelKeyboardLockRequest(this); |
| 3964 | |
Joe Downing | 192998b2 | 2018-03-22 15:51:36 | [diff] [blame] | 3965 | old_keyboard_lock_widget->CancelKeyboardLock(); |
| 3966 | } |
| 3967 | |
| 3968 | RenderWidgetHostImpl* WebContentsImpl::GetKeyboardLockWidget() { |
| 3969 | return keyboard_lock_widget_; |
| 3970 | } |
| 3971 | |
Dave Tapuska | e1c6295 | 2021-11-02 23:14:06 | [diff] [blame] | 3972 | bool WebContentsImpl::OnRenderFrameProxyVisibilityChanged( |
| 3973 | RenderFrameProxyHost* render_frame_proxy_host, |
Dave Tapuska | c334436 | 2019-02-20 17:54:28 | [diff] [blame] | 3974 | blink::mojom::FrameVisibility visibility) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 3975 | OPTIONAL_TRACE_EVENT1("content", |
| 3976 | "WebContentsImpl::OnRenderFrameProxyVisibilityChanged", |
| 3977 | "visibility", static_cast<int>(visibility)); |
Dave Tapuska | e1c6295 | 2021-11-02 23:14:06 | [diff] [blame] | 3978 | |
| 3979 | // Check that we are responsible for the RenderFrameProxyHost by checking that |
Carlos Caballero | b65b6e3a | 2021-11-15 10:09:00 | [diff] [blame] | 3980 | // its FrameTreeNode matches our `primary_frame_tree_` root. Otherwise this is |
| 3981 | // not a visibility change that affects all frames in an inner WebContents. |
| 3982 | if (render_frame_proxy_host->frame_tree_node() != primary_frame_tree_.root()) |
Dave Tapuska | e1c6295 | 2021-11-02 23:14:06 | [diff] [blame] | 3983 | return false; |
| 3984 | |
| 3985 | DCHECK(GetOuterWebContents()); |
| 3986 | |
Dave Tapuska | c334436 | 2019-02-20 17:54:28 | [diff] [blame] | 3987 | switch (visibility) { |
| 3988 | case blink::mojom::FrameVisibility::kRenderedInViewport: |
| 3989 | WasShown(); |
| 3990 | break; |
| 3991 | case blink::mojom::FrameVisibility::kNotRendered: |
| 3992 | WasHidden(); |
| 3993 | break; |
| 3994 | case blink::mojom::FrameVisibility::kRenderedOutOfViewport: |
| 3995 | WasOccluded(); |
| 3996 | break; |
| 3997 | } |
Dave Tapuska | e1c6295 | 2021-11-02 23:14:06 | [diff] [blame] | 3998 | return true; |
ekaramad | a4f4269 | 2016-02-11 19:48:37 | [diff] [blame] | 3999 | } |
| 4000 | |
Alexander Timin | 9666d50 | 2021-06-19 01:34:50 | [diff] [blame] | 4001 | FrameTree* WebContentsImpl::CreateNewWindow( |
Kevin McNee | fb86fcf | 2021-02-26 23:20:57 | [diff] [blame] | 4002 | RenderFrameHostImpl* opener, |
rockot | 5c478a7 | 2016-09-28 23:14:18 | [diff] [blame] | 4003 | const mojom::CreateNewWindowParams& params, |
Albert J. Wong | 65fe64d | 2019-09-20 02:48:14 | [diff] [blame] | 4004 | bool is_new_browsing_instance, |
Sergey Kuznetsov | 32257a2 | 2019-02-11 20:26:50 | [diff] [blame] | 4005 | bool has_user_gesture, |
[email protected] | 97714c8 | 2012-06-06 10:15:13 | [diff] [blame] | 4006 | SessionStorageNamespace* session_storage_namespace) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 4007 | TRACE_EVENT2("browser,content,navigation", "WebContentsImpl::CreateNewWindow", |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 4008 | "opener", opener, "params", params); |
csharrison | 95f01e92 | 2017-04-24 18:52:35 | [diff] [blame] | 4009 | DCHECK(opener); |
nick | 5ae4d2d | 2017-01-06 01:18:35 | [diff] [blame] | 4010 | |
Georg Neis | 736ad4c | 2022-06-10 05:58:32 | [diff] [blame] | 4011 | // Give the content browser client a chance to intercept the request and open |
| 4012 | // the URL with an external handler. |
| 4013 | if (GetContentClient()->browser()->OpenExternally(opener, params.target_url, |
| 4014 | params.disposition)) |
| 4015 | return nullptr; |
| 4016 | |
csharrison | 95f01e92 | 2017-04-24 18:52:35 | [diff] [blame] | 4017 | int render_process_id = opener->GetProcess()->GetID(); |
Charlie Reis | 37be268 | 2023-01-10 17:04:47 | [diff] [blame] | 4018 | SiteInstanceImpl* source_site_instance = opener->GetSiteInstance(); |
Alex Moshchuk | 8015afcf | 2022-01-31 22:59:25 | [diff] [blame] | 4019 | const auto& partition_config = |
| 4020 | source_site_instance->GetStoragePartitionConfig(); |
Aaron Colwell | 78b4bde | 2021-03-16 16:16:09 | [diff] [blame] | 4021 | |
Aaron Colwell | eb21998 | 2020-01-16 02:17:27 | [diff] [blame] | 4022 | { |
Lukasz Anforowicz | b9a969a | 2021-04-29 15:26:25 | [diff] [blame] | 4023 | StoragePartition* partition = |
| 4024 | GetBrowserContext()->GetStoragePartition(source_site_instance); |
Aaron Colwell | eb21998 | 2020-01-16 02:17:27 | [diff] [blame] | 4025 | DOMStorageContextWrapper* dom_storage_context = |
| 4026 | static_cast<DOMStorageContextWrapper*>( |
| 4027 | partition->GetDOMStorageContext()); |
| 4028 | SessionStorageNamespaceImpl* session_storage_namespace_impl = |
| 4029 | static_cast<SessionStorageNamespaceImpl*>(session_storage_namespace); |
| 4030 | CHECK(session_storage_namespace_impl->IsFromContext(dom_storage_context)); |
| 4031 | } |
[email protected] | dd673041 | 2013-08-14 15:03:37 | [diff] [blame] | 4032 | |
Albert J. Wong | 1ceccef9 | 2019-10-08 08:25:20 | [diff] [blame] | 4033 | if (delegate_ && delegate_->IsWebContentsCreationOverridden( |
| 4034 | source_site_instance, params.window_container_type, |
| 4035 | opener->GetLastCommittedURL(), params.frame_name, |
| 4036 | params.target_url)) { |
Alexander Timin | 9666d50 | 2021-06-19 01:34:50 | [diff] [blame] | 4037 | auto* web_contents_impl = |
| 4038 | static_cast<WebContentsImpl*>(delegate_->CreateCustomWebContents( |
| 4039 | opener, source_site_instance, is_new_browsing_instance, |
| 4040 | opener->GetLastCommittedURL(), params.frame_name, params.target_url, |
Alex Moshchuk | 8015afcf | 2022-01-31 22:59:25 | [diff] [blame] | 4041 | partition_config, session_storage_namespace)); |
Alexander Timin | 9666d50 | 2021-06-19 01:34:50 | [diff] [blame] | 4042 | if (!web_contents_impl) |
| 4043 | return nullptr; |
Carlos Caballero | 6a99dac | 2021-11-03 10:41:17 | [diff] [blame] | 4044 | return &web_contents_impl->GetPrimaryFrameTree(); |
[email protected] | dd673041 | 2013-08-14 15:03:37 | [diff] [blame] | 4045 | } |
| 4046 | |
danakj | 22b4e8b | 2019-08-01 23:14:39 | [diff] [blame] | 4047 | bool renderer_started_hidden = |
| 4048 | params.disposition == WindowOpenDisposition::NEW_BACKGROUND_TAB; |
| 4049 | |
Alex Moshchuk | 134690e | 2022-05-21 01:50:09 | [diff] [blame] | 4050 | bool is_guest = IsGuest(); |
| 4051 | |
Aaron Colwell | eb21998 | 2020-01-16 02:17:27 | [diff] [blame] | 4052 | // We usually create the new window in the same BrowsingInstance (group of |
| 4053 | // script-related windows), by passing in the current SiteInstance. However, |
Alex Moshchuk | 134690e | 2022-05-21 01:50:09 | [diff] [blame] | 4054 | // if the opener is being suppressed, we need to ensure that the new |
| 4055 | // SiteInstance is created in a new BrowsingInstance. |
| 4056 | scoped_refptr<SiteInstance> site_instance; |
| 4057 | if (params.opener_suppressed) { |
| 4058 | if (is_guest) { |
| 4059 | // For site-isolated guests, noopener windows can be created in a new |
| 4060 | // BrowsingInstance as long as they preserve the guest's StoragePartition. |
| 4061 | // For non-isolated guests, we preserve the legacy behavior of keeping the |
| 4062 | // new window in the old SiteInstance and BrowsingInstance. |
| 4063 | site_instance = SiteIsolationPolicy::IsSiteIsolationForGuestsEnabled() |
| 4064 | ? SiteInstance::CreateForGuest(GetBrowserContext(), |
| 4065 | partition_config) |
| 4066 | : source_site_instance; |
| 4067 | } else { |
| 4068 | site_instance = SiteInstance::Create(GetBrowserContext()); |
| 4069 | } |
| 4070 | } else { |
| 4071 | site_instance = source_site_instance; |
| 4072 | } |
Aaron Colwell | eb21998 | 2020-01-16 02:17:27 | [diff] [blame] | 4073 | |
[email protected] | dd673041 | 2013-08-14 15:03:37 | [diff] [blame] | 4074 | // Create the new web contents. This will automatically create the new |
| 4075 | // WebContentsView. In the future, we may want to create the view separately. |
[email protected] | fc72bb1 | 2013-06-02 21:13:46 | [diff] [blame] | 4076 | CreateParams create_params(GetBrowserContext(), site_instance.get()); |
nasko | 48321ca3 | 2015-07-02 20:44:12 | [diff] [blame] | 4077 | create_params.main_frame_name = params.frame_name; |
alexmos | 4cf2aa3 | 2015-07-15 23:40:43 | [diff] [blame] | 4078 | create_params.opener_render_process_id = render_process_id; |
csharrison | 95f01e92 | 2017-04-24 18:52:35 | [diff] [blame] | 4079 | create_params.opener_render_frame_id = opener->GetRoutingID(); |
[email protected] | 50d326e | 2014-05-20 17:59:06 | [diff] [blame] | 4080 | create_params.opener_suppressed = params.opener_suppressed; |
danakj | 22b4e8b | 2019-08-01 23:14:39 | [diff] [blame] | 4081 | create_params.initially_hidden = renderer_started_hidden; |
arthursonzogni | 034bb9c | 2020-10-01 08:29:56 | [diff] [blame] | 4082 | create_params.initial_popup_url = params.target_url; |
[email protected] | 50d326e | 2014-05-20 17:59:06 | [diff] [blame] | 4083 | |
Albert J. Wong | 65fe64d | 2019-09-20 02:48:14 | [diff] [blame] | 4084 | // Even though all codepaths leading here are in response to a renderer |
Arthur Hemery | f6218ed | 2022-10-01 14:39:25 | [diff] [blame] | 4085 | // trying to open a new window, if the new window ends up in a different |
Albert J. Wong | 65fe64d | 2019-09-20 02:48:14 | [diff] [blame] | 4086 | // browsing instance, then the RenderViewHost, RenderWidgetHost, |
| 4087 | // RenderFrameHost constellation is effectively browser initiated |
| 4088 | // the opener's process will not given the routing IDs for the new |
| 4089 | // objects. |
| 4090 | create_params.renderer_initiated_creation = !is_new_browsing_instance; |
Albert J. Wong | 1ceccef9 | 2019-10-08 08:25:20 | [diff] [blame] | 4091 | |
Tommy Steimel | 991916c4 | 2022-06-24 20:59:57 | [diff] [blame] | 4092 | if (params.pip_options) { |
Tommy Steimel | 57eafde | 2023-01-27 17:33:24 | [diff] [blame] | 4093 | create_params.picture_in_picture_options = *(params.pip_options); |
Tommy Steimel | 991916c4 | 2022-06-24 20:59:57 | [diff] [blame] | 4094 | } |
| 4095 | |
Hiroki Nakagawa | 3e252704 | 2022-11-24 06:46:42 | [diff] [blame] | 4096 | // Check whether there is an available prerendered page for this navigation if |
| 4097 | // this is not for guest. If it exists, take WebContents pre-created for |
| 4098 | // hosting the prerendered page instead of creating new WebContents. |
| 4099 | // TODO(crbug.com/1350676): Instead of filtering out the guest case here, |
| 4100 | // check it and drop prerender requests before starting prerendering. |
Albert J. Wong | 65fe64d | 2019-09-20 02:48:14 | [diff] [blame] | 4101 | std::unique_ptr<WebContentsImpl> new_contents; |
Hiroki Nakagawa | 3e252704 | 2022-11-24 06:46:42 | [diff] [blame] | 4102 | if (base::FeatureList::IsEnabled(blink::features::kPrerender2InNewTab) && |
| 4103 | !is_guest) { |
| 4104 | new_contents = |
| 4105 | GetPrerenderHostRegistry()->TakePreCreatedWebContentsForNewTabIfExists( |
| 4106 | params, create_params); |
| 4107 | if (new_contents) { |
| 4108 | // The SiteInstance of the pre-created WebContents should be in a |
| 4109 | // different BrowsingInstance from the source SiteInstance, while they |
| 4110 | // should be in the same StoragePartition. |
| 4111 | SiteInstanceImpl* new_site_instance = new_contents->GetSiteInstance(); |
| 4112 | DCHECK(!new_site_instance->IsRelatedSiteInstance(source_site_instance)); |
| 4113 | DCHECK_EQ(new_site_instance->GetStoragePartitionConfig(), |
| 4114 | source_site_instance->GetStoragePartitionConfig()); |
| 4115 | } |
[email protected] | c453807 | 2013-03-18 02:17:55 | [diff] [blame] | 4116 | } |
danakj | e6c0162 | 2018-10-05 19:39:26 | [diff] [blame] | 4117 | |
Hiroki Nakagawa | 3e252704 | 2022-11-24 06:46:42 | [diff] [blame] | 4118 | if (!new_contents) { |
| 4119 | if (!is_guest) { |
| 4120 | create_params.context = view_->GetNativeView(); |
| 4121 | new_contents = WebContentsImpl::Create(create_params); |
| 4122 | } else { |
| 4123 | new_contents = |
| 4124 | GetBrowserPluginGuest()->CreateNewGuestWindow(create_params); |
| 4125 | } |
| 4126 | new_contents->GetController().SetSessionStorageNamespace( |
| 4127 | partition_config, session_storage_namespace); |
| 4128 | } |
| 4129 | |
| 4130 | auto* new_contents_impl = new_contents.get(); |
[email protected] | d1198fd | 2012-08-13 22:50:19 | [diff] [blame] | 4131 | |
alexmos | 646fec0 | 2015-07-25 00:11:49 | [diff] [blame] | 4132 | // If the new frame has a name, make sure any SiteInstances that can find |
| 4133 | // this named frame have proxies for it. Must be called after |
| 4134 | // SetSessionStorageNamespace, since this calls CreateRenderView, which uses |
| 4135 | // GetSessionStorageNamespace. |
Harkiran Bolaria | 2912a6b3 | 2022-02-22 16:43:45 | [diff] [blame] | 4136 | if (!params.frame_name.empty()) { |
| 4137 | new_contents_impl->GetRenderManager()->CreateProxiesForNewNamedFrame( |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 4138 | new_contents_impl->GetPrimaryMainFrame()->browsing_context_state()); |
Harkiran Bolaria | 2912a6b3 | 2022-02-22 16:43:45 | [diff] [blame] | 4139 | } |
alexmos | 646fec0 | 2015-07-25 00:11:49 | [diff] [blame] | 4140 | |
[email protected] | c453807 | 2013-03-18 02:17:55 | [diff] [blame] | 4141 | // Save the window for later if we're not suppressing the opener (since it |
[email protected] | d70bea9 | 2013-04-05 04:23:34 | [diff] [blame] | 4142 | // will be shown immediately). |
| 4143 | if (!params.opener_suppressed) { |
| 4144 | if (!is_guest) { |
danakj | e6c0162 | 2018-10-05 19:39:26 | [diff] [blame] | 4145 | WebContentsView* new_view = new_contents_impl->view_.get(); |
[email protected] | bafe6cd | 2012-05-23 23:09:50 | [diff] [blame] | 4146 | |
[email protected] | d70bea9 | 2013-04-05 04:23:34 | [diff] [blame] | 4147 | // TODO(brettw): It seems bogus that we have to call this function on the |
| 4148 | // newly created object and give it one of its own member variables. |
danakj | 22b4e8b | 2019-08-01 23:14:39 | [diff] [blame] | 4149 | RenderWidgetHostView* widget_view = new_view->CreateViewForWidget( |
W. James MacLean | 13d834d | 2019-12-04 16:06:45 | [diff] [blame] | 4150 | new_contents_impl->GetRenderViewHost()->GetWidget()); |
danakj | 5f22bcc | 2021-02-12 18:09:01 | [diff] [blame] | 4151 | view_->SetOverscrollControllerEnabled(CanOverscrollContent()); |
danakj | 22b4e8b | 2019-08-01 23:14:39 | [diff] [blame] | 4152 | if (!renderer_started_hidden) { |
| 4153 | // RenderWidgets for frames always initialize as hidden. If the renderer |
| 4154 | // created this window as visible, then we show it here. |
| 4155 | widget_view->Show(); |
| 4156 | } |
[email protected] | d70bea9 | 2013-04-05 04:23:34 | [diff] [blame] | 4157 | } |
[email protected] | bafe6cd | 2012-05-23 23:09:50 | [diff] [blame] | 4158 | // Save the created window associated with the route so we can show it |
| 4159 | // later. |
Albert J. Wong | cb00463 | 2018-07-10 22:58:25 | [diff] [blame] | 4160 | // |
| 4161 | // TODO(ajwong): This should be keyed off the RenderFrame routing id or the |
| 4162 | // FrameTreeNode id instead of the routing id of the Widget for the main |
| 4163 | // frame. https://crbug.com/545684 |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 4164 | int32_t main_frame_routing_id = new_contents_impl->GetPrimaryMainFrame() |
Fergal Daly | 052873a8 | 2020-03-11 14:45:49 | [diff] [blame] | 4165 | ->GetRenderWidgetHost() |
| 4166 | ->GetRoutingID(); |
| 4167 | GlobalRoutingID id(render_process_id, main_frame_routing_id); |
Joel Hockey | 8c2011b2 | 2020-04-30 05:07:19 | [diff] [blame] | 4168 | pending_contents_[id] = |
| 4169 | CreatedWindow(std::move(new_contents), params.target_url); |
Avi Drissman | 783c123 | 2021-02-05 01:27:05 | [diff] [blame] | 4170 | AddWebContentsDestructionObserver(new_contents_impl); |
[email protected] | bafe6cd | 2012-05-23 23:09:50 | [diff] [blame] | 4171 | } |
| 4172 | |
| 4173 | if (delegate_) { |
Bryan McQuade | d2b93c0 | 2017-07-14 22:26:31 | [diff] [blame] | 4174 | delegate_->WebContentsCreated(this, render_process_id, |
| 4175 | opener->GetRoutingID(), params.frame_name, |
danakj | e6c0162 | 2018-10-05 19:39:26 | [diff] [blame] | 4176 | params.target_url, new_contents_impl); |
[email protected] | bafe6cd | 2012-05-23 23:09:50 | [diff] [blame] | 4177 | } |
| 4178 | |
Andrew Grieve | e19cd93e | 2021-01-22 05:43:06 | [diff] [blame] | 4179 | observers_.NotifyObservers(&WebContentsObserver::DidOpenRequestedURL, |
| 4180 | new_contents_impl, opener, params.target_url, |
| 4181 | params.referrer.To<Referrer>(), params.disposition, |
| 4182 | ui::PAGE_TRANSITION_LINK, |
| 4183 | false, // started_from_context_menu |
| 4184 | true); // renderer_initiated |
pnoland | aae574e | 2017-03-06 21:04:21 | [diff] [blame] | 4185 | |
[email protected] | bafe6cd | 2012-05-23 23:09:50 | [diff] [blame] | 4186 | if (params.opener_suppressed) { |
| 4187 | // When the opener is suppressed, the original renderer cannot access the |
| 4188 | // new window. As a result, we need to show and navigate the window here. |
[email protected] | eda238a1 | 2012-09-07 23:44:00 | [diff] [blame] | 4189 | bool was_blocked = false; |
erikchen | bee5c962 | 2018-04-27 19:30:25 | [diff] [blame] | 4190 | |
[email protected] | eda238a1 | 2012-09-07 23:44:00 | [diff] [blame] | 4191 | if (delegate_) { |
danakj | e6c0162 | 2018-10-05 19:39:26 | [diff] [blame] | 4192 | base::WeakPtr<WebContentsImpl> weak_new_contents = |
| 4193 | new_contents_impl->weak_factory_.GetWeakPtr(); |
nick | 0d55848 | 2017-02-13 22:27:22 | [diff] [blame] | 4194 | |
Brad Triebwasser | 767c27a | 2022-08-25 22:56:05 | [diff] [blame] | 4195 | delegate_->AddNewContents( |
| 4196 | this, std::move(new_contents), params.target_url, params.disposition, |
| 4197 | *params.features, has_user_gesture, &was_blocked); |
danakj | e6c0162 | 2018-10-05 19:39:26 | [diff] [blame] | 4198 | // The delegate may delete |new_contents_impl| during AddNewContents(). |
nick | 0d55848 | 2017-02-13 22:27:22 | [diff] [blame] | 4199 | if (!weak_new_contents) |
Albert J. Wong | 65fe64d | 2019-09-20 02:48:14 | [diff] [blame] | 4200 | return nullptr; |
[email protected] | eda238a1 | 2012-09-07 23:44:00 | [diff] [blame] | 4201 | } |
erikchen | bee5c962 | 2018-04-27 19:30:25 | [diff] [blame] | 4202 | |
[email protected] | eda238a1 | 2012-09-07 23:44:00 | [diff] [blame] | 4203 | if (!was_blocked) { |
Nasko Oskov | 83a8cf9 | 2018-10-19 14:58:56 | [diff] [blame] | 4204 | std::unique_ptr<NavigationController::LoadURLParams> load_params = |
| 4205 | std::make_unique<NavigationController::LoadURLParams>( |
| 4206 | params.target_url); |
Nasko Oskov | 93e7c55c | 2018-12-19 01:59:29 | [diff] [blame] | 4207 | load_params->initiator_origin = opener->GetLastCommittedOrigin(); |
John Delaney | 23bf1a24 | 2021-02-26 19:41:29 | [diff] [blame] | 4208 | load_params->initiator_process_id = opener->GetProcess()->GetID(); |
John Delaney | 8623c64 | 2021-01-06 17:37:07 | [diff] [blame] | 4209 | load_params->initiator_frame_token = opener->GetFrameToken(); |
Lukasz Anforowicz | d2629b4 | 2020-08-11 16:07:31 | [diff] [blame] | 4210 | // Avoiding setting |load_params->source_site_instance| when |
| 4211 | // |opener_suppressed| is true, because in that case we do not want to use |
| 4212 | // the old SiteInstance and/or BrowsingInstance. See also the test here: |
| 4213 | // NewPopupCOOP_SameOriginPolicyAndCrossOriginIframeSetsNoopener. |
Leon Han | 963dc18 | 2018-11-06 05:41:48 | [diff] [blame] | 4214 | load_params->referrer = params.referrer.To<Referrer>(); |
Nasko Oskov | 83a8cf9 | 2018-10-19 14:58:56 | [diff] [blame] | 4215 | load_params->transition_type = ui::PAGE_TRANSITION_LINK; |
| 4216 | load_params->is_renderer_initiated = true; |
John Delaney | 8623c64 | 2021-01-06 17:37:07 | [diff] [blame] | 4217 | load_params->was_opener_suppressed = true; |
Sergey Kuznetsov | 32257a2 | 2019-02-11 20:26:50 | [diff] [blame] | 4218 | load_params->has_user_gesture = has_user_gesture; |
jongdeok.kim | 5de823b3 | 2022-06-14 04:37:50 | [diff] [blame] | 4219 | load_params->is_form_submission = params.is_form_submission; |
| 4220 | if (params.form_submission_post_data) { |
| 4221 | load_params->load_type = NavigationController::LOAD_TYPE_HTTP_POST; |
| 4222 | load_params->post_data = params.form_submission_post_data; |
| 4223 | load_params->post_content_type = |
| 4224 | params.form_submission_post_content_type; |
| 4225 | } |
John Delaney | 8623c64 | 2021-01-06 17:37:07 | [diff] [blame] | 4226 | load_params->impression = params.impression; |
Gang Wu | b14b302 | 2021-03-25 22:53:48 | [diff] [blame] | 4227 | load_params->override_user_agent = |
| 4228 | new_contents_impl->should_override_user_agent_in_new_tabs_ |
| 4229 | ? NavigationController::UA_OVERRIDE_TRUE |
| 4230 | : NavigationController::UA_OVERRIDE_FALSE; |
Antonio Sartori | 6984c74 | 2021-08-26 08:03:41 | [diff] [blame] | 4231 | load_params->download_policy = params.download_policy; |
Yao Xiao | 720ef9d6 | 2022-12-09 05:18:29 | [diff] [blame] | 4232 | load_params->initiator_activation_and_ad_status = |
| 4233 | params.initiator_activation_and_ad_status; |
mariakhomenko | a4971c1 | 2015-07-21 19:04:37 | [diff] [blame] | 4234 | |
| 4235 | if (delegate_ && !is_guest && |
| 4236 | !delegate_->ShouldResumeRequestsForCreatedWindow()) { |
Nasko Oskov | 83a8cf9 | 2018-10-19 14:58:56 | [diff] [blame] | 4237 | // We are in asynchronous add new contents path, delay navigation. |
| 4238 | DCHECK(!new_contents_impl->delayed_open_url_params_); |
| 4239 | new_contents_impl->delayed_load_url_params_ = std::move(load_params); |
mariakhomenko | a4971c1 | 2015-07-21 19:04:37 | [diff] [blame] | 4240 | } else { |
Carlos Caballero | 40b0efd | 2021-01-26 11:55:00 | [diff] [blame] | 4241 | new_contents_impl->GetController().LoadURLWithParams( |
| 4242 | *load_params.get()); |
Nasko Oskov | 28ebd5b | 2018-12-07 22:29:33 | [diff] [blame] | 4243 | if (!is_guest) |
| 4244 | new_contents_impl->Focus(); |
mariakhomenko | a4971c1 | 2015-07-21 19:04:37 | [diff] [blame] | 4245 | } |
[email protected] | eda238a1 | 2012-09-07 23:44:00 | [diff] [blame] | 4246 | } |
[email protected] | bafe6cd | 2012-05-23 23:09:50 | [diff] [blame] | 4247 | } |
Carlos Caballero | 6a99dac | 2021-11-03 10:41:17 | [diff] [blame] | 4248 | return &new_contents_impl->GetPrimaryFrameTree(); |
[email protected] | bafe6cd | 2012-05-23 23:09:50 | [diff] [blame] | 4249 | } |
| 4250 | |
Dave Tapuska | 42f9b90 | 2020-10-23 21:57:06 | [diff] [blame] | 4251 | RenderWidgetHostImpl* WebContentsImpl::CreateNewPopupWidget( |
Dave Tapuska | 80fb7252 | 2022-03-18 21:34:23 | [diff] [blame] | 4252 | base::SafeRef<SiteInstanceGroup> site_instance_group, |
Tal Pressman | 5dcd238 | 2020-08-26 07:13:05 | [diff] [blame] | 4253 | int32_t route_id, |
Dave Tapuska | 04ccda06 | 2020-10-21 21:58:12 | [diff] [blame] | 4254 | mojo::PendingAssociatedReceiver<blink::mojom::PopupWidgetHost> |
| 4255 | blink_popup_widget_host, |
Dave Tapuska | 8499eec | 2020-03-16 17:54:30 | [diff] [blame] | 4256 | mojo::PendingAssociatedReceiver<blink::mojom::WidgetHost> blink_widget_host, |
| 4257 | mojo::PendingAssociatedRemote<blink::mojom::Widget> blink_widget) { |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 4258 | OPTIONAL_TRACE_EVENT1("content", "WebContentsImpl::CreateNewPopupWidget", |
| 4259 | "route_id", route_id); |
arthursonzogni | 4fe2e31d | 2020-12-04 15:51:01 | [diff] [blame] | 4260 | RenderWidgetHostImpl* widget_host = RenderWidgetHostImpl::CreateSelfOwned( |
Dave Tapuska | 80fb7252 | 2022-03-18 21:34:23 | [diff] [blame] | 4261 | &primary_frame_tree_, this, site_instance_group, route_id, IsHidden(), |
Tal Pressman | 5dcd238 | 2020-08-26 07:13:05 | [diff] [blame] | 4262 | std::make_unique<FrameTokenMessageQueue>()); |
Dave Tapuska | 8499eec | 2020-03-16 17:54:30 | [diff] [blame] | 4263 | |
| 4264 | widget_host->BindWidgetInterfaces(std::move(blink_widget_host), |
| 4265 | std::move(blink_widget)); |
Dave Tapuska | 04ccda06 | 2020-10-21 21:58:12 | [diff] [blame] | 4266 | widget_host->BindPopupWidgetInterface(std::move(blink_popup_widget_host)); |
[email protected] | cfd80b0 | 2014-05-01 17:46:48 | [diff] [blame] | 4267 | RenderWidgetHostViewBase* widget_view = |
| 4268 | static_cast<RenderWidgetHostViewBase*>( |
danakj | 0b24694 | 2018-09-17 21:30:27 | [diff] [blame] | 4269 | view_->CreateViewForChildWidget(widget_host)); |
[email protected] | 83918ec | 2013-01-10 15:37:19 | [diff] [blame] | 4270 | if (!widget_view) |
Dave Tapuska | 42f9b90 | 2020-10-23 21:57:06 | [diff] [blame] | 4271 | return nullptr; |
Dave Tapuska | 04b305f9 | 2020-10-19 18:20:28 | [diff] [blame] | 4272 | widget_view->SetWidgetType(WidgetType::kPopup); |
Avi Drissman | 783c123 | 2021-02-05 01:27:05 | [diff] [blame] | 4273 | |
[email protected] | bafe6cd | 2012-05-23 23:09:50 | [diff] [blame] | 4274 | // Save the created widget associated with the route so we can show it later. |
Dave Tapuska | 80fb7252 | 2022-03-18 21:34:23 | [diff] [blame] | 4275 | pending_widgets_[GlobalRoutingID(site_instance_group->process()->GetID(), |
| 4276 | route_id)] = widget_host; |
Avi Drissman | 783c123 | 2021-02-05 01:27:05 | [diff] [blame] | 4277 | AddRenderWidgetHostDestructionObserver(widget_host); |
| 4278 | |
Dave Tapuska | 42f9b90 | 2020-10-23 21:57:06 | [diff] [blame] | 4279 | return widget_host; |
[email protected] | bafe6cd | 2012-05-23 23:09:50 | [diff] [blame] | 4280 | } |
| 4281 | |
Brad Triebwasser | 767c27a | 2022-08-25 22:56:05 | [diff] [blame] | 4282 | void WebContentsImpl::ShowCreatedWindow( |
| 4283 | RenderFrameHostImpl* opener, |
| 4284 | int main_frame_widget_route_id, |
| 4285 | WindowOpenDisposition disposition, |
| 4286 | const blink::mojom::WindowFeatures& window_features, |
| 4287 | bool user_gesture) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 4288 | OPTIONAL_TRACE_EVENT2("content", "WebContentsImpl::ShowCreatedWindow", |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 4289 | "opener", opener, "main_frame_widget_route_id", |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 4290 | main_frame_widget_route_id); |
danakj | e6c0162 | 2018-10-05 19:39:26 | [diff] [blame] | 4291 | // This method is the renderer requesting an existing top level window to |
| 4292 | // show a new top level window that the renderer created. Each top level |
| 4293 | // window is associated with a WebContents. In this case it was created |
| 4294 | // earlier but showing it was deferred until the renderer requested for it |
| 4295 | // to be shown. We find that previously created WebContents here. |
| 4296 | // TODO(danakj): Why do we defer this show step until the renderer asks for it |
| 4297 | // when it will always do so. What needs to happen in the renderer before we |
| 4298 | // reach here? |
Anton Bikineev | f62d1bf | 2021-05-15 17:56:07 | [diff] [blame] | 4299 | absl::optional<CreatedWindow> owned_created = GetCreatedWindow( |
Mike Wasserman | dabad61 | 2020-08-13 04:26:27 | [diff] [blame] | 4300 | opener->GetProcess()->GetID(), main_frame_widget_route_id); |
Joel Hockey | 8c2011b2 | 2020-04-30 05:07:19 | [diff] [blame] | 4301 | |
danakj | e6c0162 | 2018-10-05 19:39:26 | [diff] [blame] | 4302 | // The browser may have rejected the request to make a new window, or the |
Dave Tapuska | 437e5d9 | 2020-10-26 17:59:36 | [diff] [blame] | 4303 | // renderer could be requesting to show a previously shown window (occurs when |
| 4304 | // mojom::CreateNewWindowStatus::kReuse is used). Ignore the request then. |
Joel Hockey | 8c2011b2 | 2020-04-30 05:07:19 | [diff] [blame] | 4305 | if (!owned_created || !owned_created->contents) |
danakj | e6c0162 | 2018-10-05 19:39:26 | [diff] [blame] | 4306 | return; |
mariakhomenko | 44bdc473 | 2015-04-29 01:55:38 | [diff] [blame] | 4307 | |
Joel Hockey | 8c2011b2 | 2020-04-30 05:07:19 | [diff] [blame] | 4308 | WebContentsImpl* created = owned_created->contents.get(); |
| 4309 | |
danakj | e6c0162 | 2018-10-05 19:39:26 | [diff] [blame] | 4310 | // This uses the delegate for the WebContents where the window was created |
| 4311 | // from, to control how to show the newly created window. |
| 4312 | WebContentsDelegate* delegate = GetDelegate(); |
nick | 5ae4d2d | 2017-01-06 01:18:35 | [diff] [blame] | 4313 | |
Brad Triebwasser | 767c27a | 2022-08-25 22:56:05 | [diff] [blame] | 4314 | // Individual members of |window_features.bounds| may be 0 to indicate that |
| 4315 | // the window.open() feature string did not specify a value. This code does |
| 4316 | // not distinguish between an unspecified value and 0. |
Mike Wasserman | b6bc015 | 2020-08-25 22:46:20 | [diff] [blame] | 4317 | // Assume that if any single value is non-zero, all values should be used. |
Brad Triebwasser | 767c27a | 2022-08-25 22:56:05 | [diff] [blame] | 4318 | // TODO(crbug.com/897300): Utilize window_features.has_x and others. |
| 4319 | blink::mojom::WindowFeatures adjusted_features = window_features; |
Mike Wasserman | b6bc015 | 2020-08-25 22:46:20 | [diff] [blame] | 4320 | int64_t display_id = display::kInvalidDisplayId; |
Brad Triebwasser | 767c27a | 2022-08-25 22:56:05 | [diff] [blame] | 4321 | if (adjusted_features.bounds != gfx::Rect()) |
| 4322 | display_id = AdjustRequestedWindowBounds(&adjusted_features.bounds, opener); |
Mike Wasserman | b6bc015 | 2020-08-25 22:46:20 | [diff] [blame] | 4323 | |
| 4324 | // Drop fullscreen when opening a WebContents to prohibit deceptive behavior. |
| 4325 | // Only drop fullscreen on the specific destination display, if it is known. |
Brad Triebwasser | 5eb94a9 | 2022-10-13 22:34:28 | [diff] [blame] | 4326 | // This supports sites using cross-screen window management capabilities to |
Mike Wasserman | b6bc015 | 2020-08-25 22:46:20 | [diff] [blame] | 4327 | // retain fullscreen and open a window on another screen. |
| 4328 | ForSecurityDropFullscreen(display_id).RunAndReset(); |
| 4329 | |
danakj | e6c0162 | 2018-10-05 19:39:26 | [diff] [blame] | 4330 | // The delegate can be null in tests, so we must check for it :(. |
| 4331 | if (delegate) { |
| 4332 | // Mark the web contents as pending resume, then immediately do |
| 4333 | // the resume if the delegate wants it. |
| 4334 | created->is_resume_pending_ = true; |
| 4335 | if (delegate->ShouldResumeRequestsForCreatedWindow()) |
| 4336 | created->ResumeLoadingCreatedWebContents(); |
| 4337 | |
Joel Hockey | 8c2011b2 | 2020-04-30 05:07:19 | [diff] [blame] | 4338 | delegate->AddNewContents(this, std::move(owned_created->contents), |
Joel Hockey | 891e8806 | 2020-04-30 05:38:44 | [diff] [blame] | 4339 | std::move(owned_created->target_url), disposition, |
Brad Triebwasser | 767c27a | 2022-08-25 22:56:05 | [diff] [blame] | 4340 | adjusted_features, user_gesture, nullptr); |
[email protected] | eda238a1 | 2012-09-07 23:44:00 | [diff] [blame] | 4341 | } |
[email protected] | bafe6cd | 2012-05-23 23:09:50 | [diff] [blame] | 4342 | } |
| 4343 | |
alexmos | c2a8cec | 2016-05-23 22:19:53 | [diff] [blame] | 4344 | void WebContentsImpl::ShowCreatedWidget(int process_id, |
Albert J. Wong | f6e13ed | 2018-09-18 15:25:47 | [diff] [blame] | 4345 | int widget_route_id, |
Maksim Sisov | 113ea34 | 2021-08-26 16:19:31 | [diff] [blame] | 4346 | const gfx::Rect& initial_rect, |
| 4347 | const gfx::Rect& initial_anchor_rect) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 4348 | OPTIONAL_TRACE_EVENT2("content", "WebContentsImpl::ShowCreatedWidget", |
| 4349 | "process_id", process_id, "widget_route_id", |
| 4350 | widget_route_id); |
[email protected] | cfd80b0 | 2014-05-01 17:46:48 | [diff] [blame] | 4351 | RenderWidgetHostViewBase* widget_host_view = |
alexmos | c2a8cec | 2016-05-23 22:19:53 | [diff] [blame] | 4352 | static_cast<RenderWidgetHostViewBase*>( |
Dave Tapuska | 22cafa3 | 2020-10-06 11:56:05 | [diff] [blame] | 4353 | GetCreatedWidget(process_id, widget_route_id)); |
[email protected] | bafe6cd | 2012-05-23 23:09:50 | [diff] [blame] | 4354 | if (!widget_host_view) |
| 4355 | return; |
[email protected] | cfd80b0 | 2014-05-01 17:46:48 | [diff] [blame] | 4356 | |
W. James MacLean | ec4125c | 2019-09-27 18:56:00 | [diff] [blame] | 4357 | // GetOutermostWebContents() returns |this| if there are no outer WebContents. |
W. James MacLean | 3c5f5046 | 2019-10-28 16:24:21 | [diff] [blame] | 4358 | auto* outer_web_contents = GetOuterWebContents(); |
| 4359 | auto* outermost_web_contents = GetOutermostWebContents(); |
W. James MacLean | ec4125c | 2019-09-27 18:56:00 | [diff] [blame] | 4360 | RenderWidgetHostView* view = |
W. James MacLean | 3c5f5046 | 2019-10-28 16:24:21 | [diff] [blame] | 4361 | outermost_web_contents->GetRenderWidgetHostView(); |
| 4362 | // It's not entirely obvious why we need the transform only in the case where |
| 4363 | // the outer webcontents is not the same as the outermost webcontents. It may |
| 4364 | // be due to the fact that oopifs that are children of the mainframe get |
| 4365 | // correct values for their screenrects, but deeper cross-process frames do |
| 4366 | // not. Hopefully this can be resolved with https://crbug.com/928825. |
| 4367 | // Handling these cases separately is needed for http://crbug.com/1015298. |
| 4368 | bool needs_transform = this != outermost_web_contents && |
| 4369 | outermost_web_contents != outer_web_contents; |
W. James MacLean | ec4125c | 2019-09-27 18:56:00 | [diff] [blame] | 4370 | |
| 4371 | gfx::Rect transformed_rect(initial_rect); |
Maksim Sisov | 113ea34 | 2021-08-26 16:19:31 | [diff] [blame] | 4372 | gfx::Rect transformed_anchor_rect(initial_anchor_rect); |
W. James MacLean | ec4125c | 2019-09-27 18:56:00 | [diff] [blame] | 4373 | RenderWidgetHostView* this_view = GetRenderWidgetHostView(); |
W. James MacLean | 3c5f5046 | 2019-10-28 16:24:21 | [diff] [blame] | 4374 | if (needs_transform) { |
W. James MacLean | ec4125c | 2019-09-27 18:56:00 | [diff] [blame] | 4375 | // We need to transform the coordinates of initial_rect. |
| 4376 | gfx::Point origin = |
| 4377 | this_view->TransformPointToRootCoordSpace(initial_rect.origin()); |
| 4378 | gfx::Point bottom_right = |
| 4379 | this_view->TransformPointToRootCoordSpace(initial_rect.bottom_right()); |
| 4380 | transformed_rect = |
| 4381 | gfx::Rect(origin.x(), origin.y(), bottom_right.x() - origin.x(), |
| 4382 | bottom_right.y() - origin.y()); |
Maksim Sisov | 113ea34 | 2021-08-26 16:19:31 | [diff] [blame] | 4383 | |
| 4384 | origin = |
| 4385 | this_view->TransformPointToRootCoordSpace(initial_anchor_rect.origin()); |
| 4386 | bottom_right = this_view->TransformPointToRootCoordSpace( |
| 4387 | initial_anchor_rect.bottom_right()); |
| 4388 | transformed_anchor_rect = |
| 4389 | gfx::Rect(origin.x(), origin.y(), bottom_right.x() - origin.x(), |
| 4390 | bottom_right.y() - origin.y()); |
[email protected] | cfd80b0 | 2014-05-01 17:46:48 | [diff] [blame] | 4391 | } |
| 4392 | |
Maksim Sisov | 113ea34 | 2021-08-26 16:19:31 | [diff] [blame] | 4393 | widget_host_view->InitAsPopup(view, transformed_rect, |
| 4394 | transformed_anchor_rect); |
[email protected] | 8905450 | 2012-06-03 10:29:24 | [diff] [blame] | 4395 | |
yiyix | cb1fbc4f | 2018-03-16 19:54:08 | [diff] [blame] | 4396 | RenderWidgetHostImpl* render_widget_host_impl = widget_host_view->host(); |
danakj | 08eb51d | 2020-12-30 20:15:23 | [diff] [blame] | 4397 | // Renderer-owned popup widgets wait for the renderer to request for them |
| 4398 | // to be shown. We signal that this condition is satisfied by calling Init(). |
[email protected] | 8905450 | 2012-06-03 10:29:24 | [diff] [blame] | 4399 | render_widget_host_impl->Init(); |
[email protected] | bafe6cd | 2012-05-23 23:09:50 | [diff] [blame] | 4400 | } |
| 4401 | |
Anton Bikineev | f62d1bf | 2021-05-15 17:56:07 | [diff] [blame] | 4402 | absl::optional<CreatedWindow> WebContentsImpl::GetCreatedWindow( |
nick | 5ae4d2d | 2017-01-06 01:18:35 | [diff] [blame] | 4403 | int process_id, |
| 4404 | int main_frame_widget_route_id) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 4405 | OPTIONAL_TRACE_EVENT2("content", "WebContentsImpl::GetCreatedWindow", |
| 4406 | "process_id", process_id, "main_frame_widget_route_id", |
| 4407 | main_frame_widget_route_id); |
| 4408 | |
Lukasz Anforowicz | 09060bdf7 | 2018-08-23 15:53:17 | [diff] [blame] | 4409 | auto key = GlobalRoutingID(process_id, main_frame_widget_route_id); |
nick | 5ae4d2d | 2017-01-06 01:18:35 | [diff] [blame] | 4410 | auto iter = pending_contents_.find(key); |
[email protected] | bafe6cd | 2012-05-23 23:09:50 | [diff] [blame] | 4411 | |
| 4412 | // Certain systems can block the creation of new windows. If we didn't succeed |
| 4413 | // in creating one, just return NULL. |
alexmos | c2a8cec | 2016-05-23 22:19:53 | [diff] [blame] | 4414 | if (iter == pending_contents_.end()) |
Anton Bikineev | f62d1bf | 2021-05-15 17:56:07 | [diff] [blame] | 4415 | return absl::nullopt; |
[email protected] | bafe6cd | 2012-05-23 23:09:50 | [diff] [blame] | 4416 | |
Joel Hockey | 8c2011b2 | 2020-04-30 05:07:19 | [diff] [blame] | 4417 | CreatedWindow result = std::move(iter->second); |
| 4418 | WebContentsImpl* new_contents = result.contents.get(); |
nick | 5ae4d2d | 2017-01-06 01:18:35 | [diff] [blame] | 4419 | pending_contents_.erase(key); |
Avi Drissman | 783c123 | 2021-02-05 01:27:05 | [diff] [blame] | 4420 | RemoveWebContentsDestructionObserver(new_contents); |
[email protected] | bafe6cd | 2012-05-23 23:09:50 | [diff] [blame] | 4421 | |
[email protected] | d70bea9 | 2013-04-05 04:23:34 | [diff] [blame] | 4422 | // Don't initialize the guest WebContents immediately. |
Charlie Reis | 3d18960 | 2021-04-27 21:24:47 | [diff] [blame] | 4423 | if (new_contents->IsGuest()) |
Joel Hockey | 8c2011b2 | 2020-04-30 05:07:19 | [diff] [blame] | 4424 | return result; |
[email protected] | d70bea9 | 2013-04-05 04:23:34 | [diff] [blame] | 4425 | |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 4426 | if (!new_contents->GetPrimaryMainFrame() |
| 4427 | ->GetProcess() |
| 4428 | ->IsInitializedAndNotDead() || |
| 4429 | !new_contents->GetPrimaryMainFrame()->GetView()) { |
Anton Bikineev | f62d1bf | 2021-05-15 17:56:07 | [diff] [blame] | 4430 | return absl::nullopt; |
nick | 5ae4d2d | 2017-01-06 01:18:35 | [diff] [blame] | 4431 | } |
[email protected] | bafe6cd | 2012-05-23 23:09:50 | [diff] [blame] | 4432 | |
Joel Hockey | 8c2011b2 | 2020-04-30 05:07:19 | [diff] [blame] | 4433 | return result; |
[email protected] | bafe6cd | 2012-05-23 23:09:50 | [diff] [blame] | 4434 | } |
| 4435 | |
alexmos | c2a8cec | 2016-05-23 22:19:53 | [diff] [blame] | 4436 | RenderWidgetHostView* WebContentsImpl::GetCreatedWidget(int process_id, |
| 4437 | int route_id) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 4438 | OPTIONAL_TRACE_EVENT2("content", "WebContentsImpl::GetCreatedWidget", |
| 4439 | "process_id", process_id, "route_id", route_id); |
| 4440 | |
Avi Drissman | 783c123 | 2021-02-05 01:27:05 | [diff] [blame] | 4441 | auto iter = pending_widgets_.find(GlobalRoutingID(process_id, route_id)); |
| 4442 | if (iter == pending_widgets_.end()) { |
[email protected] | bafe6cd | 2012-05-23 23:09:50 | [diff] [blame] | 4443 | DCHECK(false); |
alexmos | c2a8cec | 2016-05-23 22:19:53 | [diff] [blame] | 4444 | return nullptr; |
[email protected] | bafe6cd | 2012-05-23 23:09:50 | [diff] [blame] | 4445 | } |
| 4446 | |
Avi Drissman | 783c123 | 2021-02-05 01:27:05 | [diff] [blame] | 4447 | RenderWidgetHost* widget_host = iter->second; |
| 4448 | pending_widgets_.erase(GlobalRoutingID(process_id, route_id)); |
| 4449 | RemoveRenderWidgetHostDestructionObserver(widget_host); |
[email protected] | bafe6cd | 2012-05-23 23:09:50 | [diff] [blame] | 4450 | |
Lukasz Anforowicz | 5510ed65 | 2018-06-06 16:16:19 | [diff] [blame] | 4451 | if (!widget_host->GetProcess()->IsInitializedAndNotDead()) { |
[email protected] | bafe6cd | 2012-05-23 23:09:50 | [diff] [blame] | 4452 | // The view has gone away or the renderer crashed. Nothing to do. |
alexmos | c2a8cec | 2016-05-23 22:19:53 | [diff] [blame] | 4453 | return nullptr; |
[email protected] | bafe6cd | 2012-05-23 23:09:50 | [diff] [blame] | 4454 | } |
| 4455 | |
Avi Drissman | 783c123 | 2021-02-05 01:27:05 | [diff] [blame] | 4456 | return widget_host->GetView(); |
[email protected] | bafe6cd | 2012-05-23 23:09:50 | [diff] [blame] | 4457 | } |
| 4458 | |
Mario Sanchez Prada | f68d9827 | 2020-12-09 18:47:52 | [diff] [blame] | 4459 | void WebContentsImpl::CreateMediaPlayerHostForRenderFrameHost( |
Kevin McNee | fb86fcf | 2021-02-26 23:20:57 | [diff] [blame] | 4460 | RenderFrameHostImpl* frame_host, |
Miyoung Shin | 040d834 | 2021-02-23 15:12:31 | [diff] [blame] | 4461 | mojo::PendingAssociatedReceiver<media::mojom::MediaPlayerHost> receiver) { |
Alexander Timin | 8690530c | 2021-06-19 00:34:32 | [diff] [blame] | 4462 | media_web_contents_observer()->BindMediaPlayerHost(frame_host->GetGlobalId(), |
| 4463 | std::move(receiver)); |
Mario Sanchez Prada | f68d9827 | 2020-12-09 18:47:52 | [diff] [blame] | 4464 | } |
| 4465 | |
[email protected] | f13b420 | 2012-06-12 23:53:23 | [diff] [blame] | 4466 | void WebContentsImpl::RequestMediaAccessPermission( |
[email protected] | 33662e5 | 2013-01-07 21:31:09 | [diff] [blame] | 4467 | const MediaStreamRequest& request, |
Mark Pilgrim | 5749908 | 2018-06-12 12:38:30 | [diff] [blame] | 4468 | MediaResponseCallback callback) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 4469 | OPTIONAL_TRACE_EVENT2("content", |
| 4470 | "WebContentsImpl::RequestMediaAccessPermission", |
| 4471 | "render_process_id", request.render_process_id, |
| 4472 | "render_frame_id", request.render_frame_id); |
| 4473 | |
[email protected] | d19b84b | 2014-03-14 11:52:37 | [diff] [blame] | 4474 | if (delegate_) { |
Mark Pilgrim | 5749908 | 2018-06-12 12:38:30 | [diff] [blame] | 4475 | delegate_->RequestMediaAccessPermission(this, request, std::move(callback)); |
[email protected] | d19b84b | 2014-03-14 11:52:37 | [diff] [blame] | 4476 | } else { |
Antonio Gomes | 0d42960a | 2019-06-05 12:35:51 | [diff] [blame] | 4477 | std::move(callback).Run( |
Simon Hangl | fd537997 | 2022-06-09 09:36:54 | [diff] [blame] | 4478 | blink::mojom::StreamDevicesSet(), |
Antonio Gomes | 0d42960a | 2019-06-05 12:35:51 | [diff] [blame] | 4479 | blink::mojom::MediaStreamRequestResult::FAILED_DUE_TO_SHUTDOWN, |
| 4480 | std::unique_ptr<MediaStreamUI>()); |
[email protected] | d19b84b | 2014-03-14 11:52:37 | [diff] [blame] | 4481 | } |
[email protected] | f13b420 | 2012-06-12 23:53:23 | [diff] [blame] | 4482 | } |
| 4483 | |
Chandan Padhi | a4b8bcb7 | 2017-09-12 16:41:04 | [diff] [blame] | 4484 | bool WebContentsImpl::CheckMediaAccessPermission( |
Kevin McNee | fb86fcf | 2021-02-26 23:20:57 | [diff] [blame] | 4485 | RenderFrameHostImpl* render_frame_host, |
Chandan Padhi | a4b8bcb7 | 2017-09-12 16:41:04 | [diff] [blame] | 4486 | const url::Origin& security_origin, |
Antonio Gomes | c8b734b | 2019-06-05 18:22:16 | [diff] [blame] | 4487 | blink::mojom::MediaStreamType type) { |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 4488 | OPTIONAL_TRACE_EVENT2("content", |
| 4489 | "WebContentsImpl::CheckMediaAccessPermission", |
| 4490 | "render_frame_host", render_frame_host, |
| 4491 | "security_origin", security_origin); |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 4492 | |
Antonio Gomes | c8b734b | 2019-06-05 18:22:16 | [diff] [blame] | 4493 | DCHECK(type == blink::mojom::MediaStreamType::DEVICE_AUDIO_CAPTURE || |
| 4494 | type == blink::mojom::MediaStreamType::DEVICE_VIDEO_CAPTURE); |
Chandan Padhi | a4b8bcb7 | 2017-09-12 16:41:04 | [diff] [blame] | 4495 | return delegate_ && delegate_->CheckMediaAccessPermission( |
Raymes Khoury | ad7c24a1 | 2018-03-05 23:22:58 | [diff] [blame] | 4496 | render_frame_host, security_origin.GetURL(), type); |
grunell | 657d4d8 | 2014-09-18 00:09:43 | [diff] [blame] | 4497 | } |
| 4498 | |
Guido Urdaneta | 73fa663 | 2019-01-14 18:46:26 | [diff] [blame] | 4499 | std::string WebContentsImpl::GetDefaultMediaDeviceID( |
Antonio Gomes | c8b734b | 2019-06-05 18:22:16 | [diff] [blame] | 4500 | blink::mojom::MediaStreamType type) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 4501 | OPTIONAL_TRACE_EVENT1("content", "WebContentsImpl::GetDefaultMediaDeviceID", |
| 4502 | "type", static_cast<int>(type)); |
| 4503 | |
guidou | 8a440b84 | 2017-01-30 13:58:43 | [diff] [blame] | 4504 | if (!delegate_) |
| 4505 | return std::string(); |
| 4506 | return delegate_->GetDefaultMediaDeviceID(this, type); |
| 4507 | } |
| 4508 | |
Elad Alon | f156eb6 | 2021-05-17 22:02:37 | [diff] [blame] | 4509 | void WebContentsImpl::SetCaptureHandleConfig( |
| 4510 | blink::mojom::CaptureHandleConfigPtr config) { |
| 4511 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 4512 | |
| 4513 | if (capture_handle_config_ == *config) { |
| 4514 | return; // Avoid unnecessary notifications. |
| 4515 | } |
| 4516 | |
| 4517 | capture_handle_config_ = std::move(*config); |
| 4518 | |
| 4519 | // Propagates the capture-handle-config inside of the browser process. |
| 4520 | // Only render processes which are eligible based on |permittedOrigins| |
| 4521 | // will get this. |
| 4522 | observers_.NotifyObservers(&WebContentsObserver::OnCaptureHandleConfigUpdate, |
| 4523 | capture_handle_config_); |
| 4524 | } |
| 4525 | |
avi | 85ee836 | 2016-10-08 02:09:08 | [diff] [blame] | 4526 | bool WebContentsImpl::IsJavaScriptDialogShowing() const { |
| 4527 | return is_showing_javascript_dialog_; |
| 4528 | } |
| 4529 | |
avi | c031d39 | 2017-03-03 03:09:42 | [diff] [blame] | 4530 | bool WebContentsImpl::ShouldIgnoreUnresponsiveRenderer() { |
Minoru Chikamune | 562598cd | 2020-12-28 01:42:31 | [diff] [blame] | 4531 | // Suppress unresponsive renderers if the command line asks for it. |
| 4532 | if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 4533 | switches::kDisableHangMonitor)) |
| 4534 | return true; |
| 4535 | |
Kevin McNee | d7fc6d05 | 2022-02-08 21:21:50 | [diff] [blame] | 4536 | if (IsBeingDestroyed()) |
| 4537 | return true; |
| 4538 | |
Dominique Fauteux-Chapleau | 9924438 | 2020-07-08 20:37:00 | [diff] [blame] | 4539 | if (suppress_unresponsive_renderer_count_ > 0) |
| 4540 | return true; |
| 4541 | |
avi | c031d39 | 2017-03-03 03:09:42 | [diff] [blame] | 4542 | // Ignore unresponsive renderers if the debugger is attached to them since the |
| 4543 | // unresponsiveness might be a result of the renderer sitting on a breakpoint. |
| 4544 | // |
Xiaohan Wang | 00d05a0 | 2022-01-21 22:47:18 | [diff] [blame] | 4545 | #if BUILDFLAG(IS_WIN) |
David Bienvenu | 525f8a0 | 2019-02-15 02:02:42 | [diff] [blame] | 4546 | // Check if a windows debugger is attached to the renderer process. |
| 4547 | base::ProcessHandle process_handle = |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 4548 | GetPrimaryMainFrame()->GetProcess()->GetProcess().Handle(); |
David Bienvenu | 525f8a0 | 2019-02-15 02:02:42 | [diff] [blame] | 4549 | BOOL debugger_present = FALSE; |
| 4550 | if (CheckRemoteDebuggerPresent(process_handle, &debugger_present) && |
| 4551 | debugger_present) |
| 4552 | return true; |
Xiaohan Wang | 24ec934 | 2022-01-15 17:34:22 | [diff] [blame] | 4553 | #endif // BUILDFLAG(IS_WIN) |
David Bienvenu | 525f8a0 | 2019-02-15 02:02:42 | [diff] [blame] | 4554 | |
avi | c031d39 | 2017-03-03 03:09:42 | [diff] [blame] | 4555 | // TODO(pfeldman): Fix this to only return true if the renderer is *actually* |
| 4556 | // sitting on a breakpoint. https://crbug.com/684202 |
| 4557 | return DevToolsAgentHost::IsDebuggerAttached(this); |
| 4558 | } |
| 4559 | |
Lucas Furukawa Gadani | e1c5dfda | 2018-11-29 17:57:41 | [diff] [blame] | 4560 | ui::AXMode WebContentsImpl::GetAccessibilityMode() { |
[email protected] | 9564021 | 2014-07-26 18:14:30 | [diff] [blame] | 4561 | return accessibility_mode_; |
| 4562 | } |
| 4563 | |
Mario Sanchez Prada | 5d7f1ac | 2020-07-16 17:18:40 | [diff] [blame] | 4564 | void WebContentsImpl::AXTreeIDForMainFrameHasChanged() { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 4565 | OPTIONAL_TRACE_EVENT0("content", |
| 4566 | "WebContentsImpl::AXTreeIDForMainFrameHasChanged"); |
| 4567 | |
Mario Sanchez Prada | 5d7f1ac | 2020-07-16 17:18:40 | [diff] [blame] | 4568 | RenderWidgetHostViewBase* rwhv = |
| 4569 | static_cast<RenderWidgetHostViewBase*>(GetRenderWidgetHostView()); |
| 4570 | if (rwhv) |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 4571 | rwhv->SetMainFrameAXTreeID(GetPrimaryMainFrame()->GetAXTreeID()); |
Mario Sanchez Prada | 5d7f1ac | 2020-07-16 17:18:40 | [diff] [blame] | 4572 | |
Andrew Grieve | e19cd93e | 2021-01-22 05:43:06 | [diff] [blame] | 4573 | observers_.NotifyObservers( |
| 4574 | &WebContentsObserver::AXTreeIDForMainFrameHasChanged); |
Mario Sanchez Prada | 5d7f1ac | 2020-07-16 17:18:40 | [diff] [blame] | 4575 | } |
| 4576 | |
Dominic Mazzoni | a7b0edb2 | 2017-08-09 16:32:51 | [diff] [blame] | 4577 | void WebContentsImpl::AccessibilityEventReceived( |
Dominic Mazzoni | ccbaa9b | 2018-06-06 07:44:23 | [diff] [blame] | 4578 | const AXEventNotificationDetails& details) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 4579 | OPTIONAL_TRACE_EVENT0("content", |
| 4580 | "WebContentsImpl::AccessibilityEventReceived"); |
Andrew Grieve | e19cd93e | 2021-01-22 05:43:06 | [diff] [blame] | 4581 | observers_.NotifyObservers(&WebContentsObserver::AccessibilityEventReceived, |
| 4582 | details); |
[email protected] | 31a71eaf | 2014-03-13 01:47:36 | [diff] [blame] | 4583 | } |
| 4584 | |
dmazzoni | 2400c46 | 2016-08-23 15:07:13 | [diff] [blame] | 4585 | void WebContentsImpl::AccessibilityLocationChangesReceived( |
| 4586 | const std::vector<AXLocationChangeNotificationDetails>& details) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 4587 | OPTIONAL_TRACE_EVENT0( |
| 4588 | "content", "WebContentsImpl::AccessibilityLocationChangesReceived"); |
Andrew Grieve | e19cd93e | 2021-01-22 05:43:06 | [diff] [blame] | 4589 | observers_.NotifyObservers( |
| 4590 | &WebContentsObserver::AccessibilityLocationChangesReceived, details); |
dmazzoni | 2400c46 | 2016-08-23 15:07:13 | [diff] [blame] | 4591 | } |
| 4592 | |
Alexander Surkov | 2ab5162 | 2020-09-02 12:01:42 | [diff] [blame] | 4593 | std::string WebContentsImpl::DumpAccessibilityTree( |
Abigail Klein | abb42833 | 2019-09-13 18:26:21 | [diff] [blame] | 4594 | bool internal, |
Alexander Surkov | 9048288 | 2020-10-12 16:30:38 | [diff] [blame] | 4595 | std::vector<ui::AXPropertyFilter> property_filters) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 4596 | OPTIONAL_TRACE_EVENT0("content", "WebContentsImpl::DumpAccessibilityTree"); |
James Wallace-Lee | eafc94cb9 | 2018-07-23 21:35:09 | [diff] [blame] | 4597 | auto* ax_mgr = GetOrCreateRootBrowserAccessibilityManager(); |
Javier Contreras Tenorio | aa5f70c5 | 2023-02-06 12:34:04 | [diff] [blame] | 4598 | // Since for Web Content we get the AXTree updates through the renderer at a |
| 4599 | // point after the manager is created, there are cases where at this point in |
| 4600 | // the lifecycle the AXTree associated with `ax_mgr` does not have a valid |
| 4601 | // tree ID. As such, if this is the case we return an empty string early. If |
| 4602 | // we don't have this check, there will be a scenario where we then try to get |
| 4603 | // the manager using the ID (which at this point is invalid) which leads to a |
| 4604 | // crash. See https://crbug.com/1405036. |
Javier Contreras Tenorio | 4908f71 | 2023-02-08 21:42:04 | [diff] [blame] | 4605 | if (!ax_mgr || !ax_mgr->HasValidTreeID()) |
Javier Contreras Tenorio | aa5f70c5 | 2023-02-06 12:34:04 | [diff] [blame] | 4606 | return "-"; |
| 4607 | |
Javier Contreras Tenorio | 4908f71 | 2023-02-08 21:42:04 | [diff] [blame] | 4608 | // Developer mode: crash immediately on any accessibility fatal error. |
| 4609 | // This only runs during integration tests, or if a developer is |
| 4610 | // using an inspection tool, e.g. chrome://accessibility. |
| 4611 | BrowserAccessibilityManager::AlwaysFailFast(); |
| 4612 | |
Alexander Surkov | fb6cb38 | 2020-12-01 14:46:23 | [diff] [blame] | 4613 | std::unique_ptr<ui::AXTreeFormatter> formatter = |
| 4614 | internal ? AXInspectFactory::CreateBlinkFormatter() |
| 4615 | : AXInspectFactory::CreatePlatformFormatter(); |
| 4616 | |
| 4617 | formatter->SetPropertyFilters(property_filters); |
Javier Contreras | a0039cbf | 2022-08-31 19:35:02 | [diff] [blame] | 4618 | return formatter->Format(ax_mgr->GetBrowserAccessibilityRoot()); |
James Wallace-Lee | eafc94cb9 | 2018-07-23 21:35:09 | [diff] [blame] | 4619 | } |
| 4620 | |
Abigail Klein | aa89874 | 2019-11-01 02:31:25 | [diff] [blame] | 4621 | void WebContentsImpl::RecordAccessibilityEvents( |
Abigail Klein | bb8304bd | 2020-05-18 21:44:18 | [diff] [blame] | 4622 | bool start_recording, |
Anton Bikineev | f62d1bf | 2021-05-15 17:56:07 | [diff] [blame] | 4623 | absl::optional<ui::AXEventCallback> callback) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 4624 | OPTIONAL_TRACE_EVENT0("content", |
| 4625 | "WebContentsImpl::RecordAccessibilityEvents"); |
Aaron Leventhal | 96df51a9 | 2021-12-02 23:39:01 | [diff] [blame] | 4626 | |
Abigail Klein | bb8304bd | 2020-05-18 21:44:18 | [diff] [blame] | 4627 | // Only pass a callback to RecordAccessibilityEvents when starting to record. |
| 4628 | DCHECK_EQ(start_recording, callback.has_value()); |
| 4629 | if (start_recording) { |
Aaron Leventhal | dadb9bf | 2022-09-14 22:08:18 | [diff] [blame] | 4630 | BrowserAccessibilityStateImpl::GetInstance()->AddAccessibilityModeFlags( |
Amanda Lin Dietz | e54912e | 2022-11-22 20:25:57 | [diff] [blame] | 4631 | ui::kAXModeBasic.flags()); |
Abigail Klein | aa89874 | 2019-11-01 02:31:25 | [diff] [blame] | 4632 | auto* ax_mgr = GetOrCreateRootBrowserAccessibilityManager(); |
Aaron Leventhal | dadb9bf | 2022-09-14 22:08:18 | [diff] [blame] | 4633 | CHECK(ax_mgr); |
Abigail Klein | aa89874 | 2019-11-01 02:31:25 | [diff] [blame] | 4634 | base::ProcessId pid = base::Process::Current().Pid(); |
Alexander Surkov | f83fa6cd | 2022-02-11 14:01:51 | [diff] [blame] | 4635 | gfx::AcceleratedWidget widget = |
Javier Contreras | a0039cbf | 2022-08-31 19:35:02 | [diff] [blame] | 4636 | ax_mgr->GetBrowserAccessibilityRoot() |
| 4637 | ->GetTargetForNativeAccessibilityEvent(); |
Alexander Surkov | f83fa6cd | 2022-02-11 14:01:51 | [diff] [blame] | 4638 | event_recorder_ = content::AXInspectFactory::CreatePlatformRecorder( |
Alexander Surkov | 34080bf | 2022-08-23 21:26:23 | [diff] [blame] | 4639 | ax_mgr, pid, ui::AXTreeSelector(widget)); |
Abigail Klein | bb8304bd | 2020-05-18 21:44:18 | [diff] [blame] | 4640 | event_recorder_->ListenToEvents(*callback); |
Abigail Klein | aa89874 | 2019-11-01 02:31:25 | [diff] [blame] | 4641 | } else { |
Abigail Klein | bb8304bd | 2020-05-18 21:44:18 | [diff] [blame] | 4642 | if (event_recorder_) { |
Adam Ettenberger | e1a5a18 | 2021-06-04 17:49:42 | [diff] [blame] | 4643 | event_recorder_->WaitForDoneRecording(); |
Abigail Klein | bb8304bd | 2020-05-18 21:44:18 | [diff] [blame] | 4644 | event_recorder_.reset(nullptr); |
| 4645 | } |
Abigail Klein | aa89874 | 2019-11-01 02:31:25 | [diff] [blame] | 4646 | } |
| 4647 | } |
| 4648 | |
Ke He | 7319dbe | 2017-11-09 05:54:44 | [diff] [blame] | 4649 | device::mojom::GeolocationContext* WebContentsImpl::GetGeolocationContext() { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 4650 | OPTIONAL_TRACE_EVENT0("content", "WebContentsImpl::GetGeolocationContext"); |
Ella Ge | 3e375b70 | 2020-06-05 15:57:16 | [diff] [blame] | 4651 | if (delegate_) { |
| 4652 | auto* installed_webapp_context = |
| 4653 | delegate_->GetInstalledWebappGeolocationContext(); |
| 4654 | if (installed_webapp_context) |
| 4655 | return installed_webapp_context; |
| 4656 | } |
| 4657 | |
Ken Rockot | ce010f0 | 2019-12-12 23:32:32 | [diff] [blame] | 4658 | if (!geolocation_context_) { |
| 4659 | GetDeviceService().BindGeolocationContext( |
| 4660 | geolocation_context_.BindNewPipeAndPassReceiver()); |
| 4661 | } |
Conley Owens | 6894c4f | 2017-07-10 19:29:13 | [diff] [blame] | 4662 | return geolocation_context_.get(); |
blundell | c57b93f | 2014-10-29 13:19:57 | [diff] [blame] | 4663 | } |
| 4664 | |
ke.he | 98b761e | 2017-05-09 05:59:17 | [diff] [blame] | 4665 | device::mojom::WakeLockContext* WebContentsImpl::GetWakeLockContext() { |
Thoren Paulson | d344c0a | 2021-10-14 19:20:31 | [diff] [blame] | 4666 | if (!enable_wake_locks_) |
| 4667 | return nullptr; |
blundell | d8cd72b | 2017-03-28 07:18:38 | [diff] [blame] | 4668 | if (!wake_lock_context_host_) |
Fergal Daly | 55b6d72 | 2020-09-11 07:56:33 | [diff] [blame] | 4669 | wake_lock_context_host_ = std::make_unique<WakeLockContextHost>(this); |
blundell | e75a8f9 | 2017-03-27 08:11:17 | [diff] [blame] | 4670 | return wake_lock_context_host_->GetWakeLockContext(); |
alogvinov | f50445a | 2015-10-30 13:00:12 | [diff] [blame] | 4671 | } |
| 4672 | |
Xiaohan Wang | 24ec934 | 2022-01-15 17:34:22 | [diff] [blame] | 4673 | #if BUILDFLAG(IS_ANDROID) |
Miyoung Shin | 59f39ff | 2019-09-17 07:23:20 | [diff] [blame] | 4674 | void WebContentsImpl::GetNFC( |
Reilly Grant | afbe224 | 2020-02-20 21:14:55 | [diff] [blame] | 4675 | RenderFrameHost* render_frame_host, |
Miyoung Shin | 59f39ff | 2019-09-17 07:23:20 | [diff] [blame] | 4676 | mojo::PendingReceiver<device::mojom::NFC> receiver) { |
Reilly Grant | afbe224 | 2020-02-20 21:14:55 | [diff] [blame] | 4677 | if (!nfc_host_) |
| 4678 | nfc_host_ = std::make_unique<NFCHost>(this); |
| 4679 | nfc_host_->GetNFC(render_frame_host, std::move(receiver)); |
blundell | f5316fc | 2017-05-15 11:49:03 | [diff] [blame] | 4680 | } |
| 4681 | #endif |
| 4682 | |
lfg | bb9c28a | 2016-03-01 03:19:49 | [diff] [blame] | 4683 | void WebContentsImpl::SendScreenRects() { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 4684 | OPTIONAL_TRACE_EVENT0("content", "WebContentsImpl::SendScreenRects"); |
Kevin McNee | d7fc6d05 | 2022-02-08 21:21:50 | [diff] [blame] | 4685 | |
| 4686 | DCHECK(!IsBeingDestroyed()); |
| 4687 | |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 4688 | GetPrimaryMainFrame()->ForEachRenderFrameHost( |
Daniel Cheng | 982f2b2 | 2022-08-25 23:46:16 | [diff] [blame] | 4689 | [](RenderFrameHostImpl* render_frame_host) { |
Dave Tapuska | 41fbb6e | 2021-12-07 01:11:41 | [diff] [blame] | 4690 | if (render_frame_host->is_local_root()) { |
| 4691 | render_frame_host->GetRenderWidgetHost()->SendScreenRects(); |
| 4692 | } |
Daniel Cheng | 982f2b2 | 2022-08-25 23:46:16 | [diff] [blame] | 4693 | }); |
[email protected] | 32deec6 | 2013-05-15 23:55:04 | [diff] [blame] | 4694 | } |
| 4695 | |
Dave Tapuska | 3450d0b | 2022-04-08 21:27:53 | [diff] [blame] | 4696 | void WebContentsImpl::SendActiveState(bool active) { |
| 4697 | DCHECK(!IsBeingDestroyed()); |
| 4698 | |
| 4699 | // Replicate the active state to all LocalRoots. |
Daniel Cheng | 982f2b2 | 2022-08-25 23:46:16 | [diff] [blame] | 4700 | GetPrimaryMainFrame()->ForEachRenderFrameHost( |
| 4701 | [active](RenderFrameHostImpl* render_frame_host) { |
Dave Tapuska | 3450d0b | 2022-04-08 21:27:53 | [diff] [blame] | 4702 | if (render_frame_host->is_local_root()) { |
| 4703 | render_frame_host->GetRenderWidgetHost()->SetActive(active); |
| 4704 | } |
Daniel Cheng | 982f2b2 | 2022-08-25 23:46:16 | [diff] [blame] | 4705 | }); |
Dave Tapuska | 3450d0b | 2022-04-08 21:27:53 | [diff] [blame] | 4706 | } |
| 4707 | |
ekaramad | add88229 | 2016-06-08 15:22:56 | [diff] [blame] | 4708 | TextInputManager* WebContentsImpl::GetTextInputManager() { |
David Bokan | e86aa03 | 2022-05-08 18:21:24 | [diff] [blame] | 4709 | if (suppress_ime_events_for_testing_) |
| 4710 | return nullptr; |
| 4711 | |
ekaramad | add88229 | 2016-06-08 15:22:56 | [diff] [blame] | 4712 | if (GetOuterWebContents()) |
| 4713 | return GetOuterWebContents()->GetTextInputManager(); |
| 4714 | |
Ehsan Karamad | c179dc0 | 2018-12-15 03:09:29 | [diff] [blame] | 4715 | if (!text_input_manager_ && !browser_plugin_guest_) { |
Fergal Daly | 55b6d72 | 2020-09-11 07:56:33 | [diff] [blame] | 4716 | text_input_manager_ = std::make_unique<TextInputManager>( |
Darren Shen | ca53d5f | 2018-05-15 04:56:01 | [diff] [blame] | 4717 | GetBrowserContext() && |
Fergal Daly | 55b6d72 | 2020-09-11 07:56:33 | [diff] [blame] | 4718 | !GetBrowserContext()->IsOffTheRecord() /* should_do_learning */); |
Darren Shen | ca53d5f | 2018-05-15 04:56:01 | [diff] [blame] | 4719 | } |
ekaramad | add88229 | 2016-06-08 15:22:56 | [diff] [blame] | 4720 | |
| 4721 | return text_input_manager_.get(); |
| 4722 | } |
| 4723 | |
Dave Tapuska | 1d72e33 | 2021-10-13 19:24:38 | [diff] [blame] | 4724 | bool WebContentsImpl::IsWidgetForPrimaryMainFrame( |
kenrb | 11f213a | 2017-03-24 18:12:06 | [diff] [blame] | 4725 | RenderWidgetHostImpl* render_widget_host) { |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 4726 | return render_widget_host == GetPrimaryMainFrame()->GetRenderWidgetHost(); |
kenrb | 11f213a | 2017-03-24 18:12:06 | [diff] [blame] | 4727 | } |
| 4728 | |
[email protected] | 9564021 | 2014-07-26 18:14:30 | [diff] [blame] | 4729 | BrowserAccessibilityManager* |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 4730 | WebContentsImpl::GetRootBrowserAccessibilityManager() { |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 4731 | RenderFrameHostImpl* rfh = |
| 4732 | static_cast<RenderFrameHostImpl*>(GetPrimaryMainFrame()); |
creis | c014b40 | 2015-04-23 16:41:45 | [diff] [blame] | 4733 | return rfh ? rfh->browser_accessibility_manager() : nullptr; |
[email protected] | 9564021 | 2014-07-26 18:14:30 | [diff] [blame] | 4734 | } |
| 4735 | |
| 4736 | BrowserAccessibilityManager* |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 4737 | WebContentsImpl::GetOrCreateRootBrowserAccessibilityManager() { |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 4738 | RenderFrameHostImpl* rfh = |
| 4739 | static_cast<RenderFrameHostImpl*>(GetPrimaryMainFrame()); |
creis | c014b40 | 2015-04-23 16:41:45 | [diff] [blame] | 4740 | return rfh ? rfh->GetOrCreateBrowserAccessibilityManager() : nullptr; |
[email protected] | 9564021 | 2014-07-26 18:14:30 | [diff] [blame] | 4741 | } |
| 4742 | |
Dave Tapuska | b336b92 | 2017-07-06 19:24:05 | [diff] [blame] | 4743 | void WebContentsImpl::ExecuteEditCommand( |
| 4744 | const std::string& command, |
Anton Bikineev | f62d1bf | 2021-05-15 17:56:07 | [diff] [blame] | 4745 | const absl::optional<std::u16string>& value) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 4746 | OPTIONAL_TRACE_EVENT0("content", "WebContentsImpl::ExecuteEditCommand"); |
Dave Tapuska | 58a099e | 2020-06-08 21:48:40 | [diff] [blame] | 4747 | auto* input_handler = GetFocusedFrameWidgetInputHandler(); |
Dave Tapuska | e782bea | 2019-07-09 16:21:51 | [diff] [blame] | 4748 | if (!input_handler) |
Dave Tapuska | b336b92 | 2017-07-06 19:24:05 | [diff] [blame] | 4749 | return; |
| 4750 | |
Dave Tapuska | e782bea | 2019-07-09 16:21:51 | [diff] [blame] | 4751 | input_handler->ExecuteEditCommand(command, value); |
Dave Tapuska | b336b92 | 2017-07-06 19:24:05 | [diff] [blame] | 4752 | } |
| 4753 | |
mohsen | 7ab1ec16ec | 2015-07-02 18:26:23 | [diff] [blame] | 4754 | void WebContentsImpl::MoveRangeSelectionExtent(const gfx::Point& extent) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 4755 | OPTIONAL_TRACE_EVENT0("content", "WebContentsImpl::MoveRangeSelectionExtent"); |
Dave Tapuska | 58a099e | 2020-06-08 21:48:40 | [diff] [blame] | 4756 | auto* input_handler = GetFocusedFrameWidgetInputHandler(); |
Dave Tapuska | e782bea | 2019-07-09 16:21:51 | [diff] [blame] | 4757 | if (!input_handler) |
mohsen | 7ab1ec16ec | 2015-07-02 18:26:23 | [diff] [blame] | 4758 | return; |
| 4759 | |
Dave Tapuska | e782bea | 2019-07-09 16:21:51 | [diff] [blame] | 4760 | input_handler->MoveRangeSelectionExtent(extent); |
mohsen | 7ab1ec16ec | 2015-07-02 18:26:23 | [diff] [blame] | 4761 | } |
| 4762 | |
| 4763 | void WebContentsImpl::SelectRange(const gfx::Point& base, |
| 4764 | const gfx::Point& extent) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 4765 | OPTIONAL_TRACE_EVENT0("content", "WebContentsImpl::SelectRange"); |
Dave Tapuska | 58a099e | 2020-06-08 21:48:40 | [diff] [blame] | 4766 | auto* input_handler = GetFocusedFrameWidgetInputHandler(); |
Dave Tapuska | e782bea | 2019-07-09 16:21:51 | [diff] [blame] | 4767 | if (!input_handler) |
mohsen | 7ab1ec16ec | 2015-07-02 18:26:23 | [diff] [blame] | 4768 | return; |
| 4769 | |
Dave Tapuska | e782bea | 2019-07-09 16:21:51 | [diff] [blame] | 4770 | input_handler->SelectRange(base, extent); |
mohsen | 7ab1ec16ec | 2015-07-02 18:26:23 | [diff] [blame] | 4771 | } |
| 4772 | |
Dave Tapuska | b336b92 | 2017-07-06 19:24:05 | [diff] [blame] | 4773 | void WebContentsImpl::MoveCaret(const gfx::Point& extent) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 4774 | OPTIONAL_TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("content.verbose"), |
| 4775 | "WebContentsImpl::MoveCaret"); |
Dave Tapuska | 58a099e | 2020-06-08 21:48:40 | [diff] [blame] | 4776 | auto* input_handler = GetFocusedFrameWidgetInputHandler(); |
Dave Tapuska | e782bea | 2019-07-09 16:21:51 | [diff] [blame] | 4777 | if (!input_handler) |
Dave Tapuska | b336b92 | 2017-07-06 19:24:05 | [diff] [blame] | 4778 | return; |
| 4779 | |
Dave Tapuska | e782bea | 2019-07-09 16:21:51 | [diff] [blame] | 4780 | input_handler->MoveCaret(extent); |
Dave Tapuska | b336b92 | 2017-07-06 19:24:05 | [diff] [blame] | 4781 | } |
| 4782 | |
Shimi Zhang | 37deeb2 | 2017-09-28 00:59:01 | [diff] [blame] | 4783 | void WebContentsImpl::AdjustSelectionByCharacterOffset( |
| 4784 | int start_adjust, |
| 4785 | int end_adjust, |
| 4786 | bool show_selection_menu) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 4787 | OPTIONAL_TRACE_EVENT0("content", |
| 4788 | "WebContentsImpl::AdjustSelectionByCharacterOffset"); |
Dave Tapuska | 58a099e | 2020-06-08 21:48:40 | [diff] [blame] | 4789 | auto* input_handler = GetFocusedFrameWidgetInputHandler(); |
Dave Tapuska | e782bea | 2019-07-09 16:21:51 | [diff] [blame] | 4790 | if (!input_handler) |
aurimas | ab031902 | 2015-07-10 21:57:38 | [diff] [blame] | 4791 | return; |
| 4792 | |
Shimi Zhang | 37deeb2 | 2017-09-28 00:59:01 | [diff] [blame] | 4793 | using blink::mojom::SelectionMenuBehavior; |
Dave Tapuska | e782bea | 2019-07-09 16:21:51 | [diff] [blame] | 4794 | input_handler->AdjustSelectionByCharacterOffset( |
Shimi Zhang | 37deeb2 | 2017-09-28 00:59:01 | [diff] [blame] | 4795 | start_adjust, end_adjust, |
| 4796 | show_selection_menu ? SelectionMenuBehavior::kShow |
| 4797 | : SelectionMenuBehavior::kHide); |
aurimas | ab031902 | 2015-07-10 21:57:38 | [diff] [blame] | 4798 | } |
| 4799 | |
avi | c3aa842 | 2015-11-09 20:57:22 | [diff] [blame] | 4800 | void WebContentsImpl::ResizeDueToAutoResize( |
| 4801 | RenderWidgetHostImpl* render_widget_host, |
Fady Samuel | 9794711 | 2018-05-05 16:24:54 | [diff] [blame] | 4802 | const gfx::Size& new_size) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 4803 | OPTIONAL_TRACE_EVENT1("content", "WebContentsImpl::ResizeDueToAutoResize", |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 4804 | "render_widget_host", render_widget_host); |
avi | c3aa842 | 2015-11-09 20:57:22 | [diff] [blame] | 4805 | if (render_widget_host != GetRenderViewHost()->GetWidget()) |
| 4806 | return; |
| 4807 | |
[email protected] | 61e2b3cc | 2012-03-02 16:13:34 | [diff] [blame] | 4808 | if (delegate_) |
| 4809 | delegate_->ResizeDueToAutoResize(this, new_size); |
| 4810 | } |
| 4811 | |
[email protected] | b172aee | 2012-04-10 17:05:26 | [diff] [blame] | 4812 | WebContents* WebContentsImpl::OpenURL(const OpenURLParams& params) { |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 4813 | TRACE_EVENT1("content", "WebContentsImpl::OpenURL", "url", params.url); |
Aaron Colwell | 9dab165 | 2019-12-09 18:29:49 | [diff] [blame] | 4814 | #if DCHECK_IS_ON() |
| 4815 | DCHECK(params.Valid()); |
| 4816 | #endif |
| 4817 | |
Bo Liu | 300c605 | 2018-06-12 04:46:07 | [diff] [blame] | 4818 | if (!delegate_) { |
| 4819 | // Embedder can delay setting a delegate on new WebContents with |
| 4820 | // WebContentsDelegate::ShouldResumeRequestsForCreatedWindow. In the mean |
| 4821 | // time, navigations, including the initial one, that goes through OpenURL |
| 4822 | // should be delayed until embedder is ready to resume loading. |
| 4823 | delayed_open_url_params_ = std::make_unique<OpenURLParams>(params); |
Nasko Oskov | 83a8cf9 | 2018-10-19 14:58:56 | [diff] [blame] | 4824 | |
| 4825 | // If there was a navigation deferred when creating the window through |
| 4826 | // CreateNewWindow, drop it in favor of this navigation. |
| 4827 | delayed_load_url_params_.reset(); |
| 4828 | |
Ivan Kotenkov | 2c0d2bb3 | 2017-11-01 15:41:28 | [diff] [blame] | 4829 | return nullptr; |
Bo Liu | 300c605 | 2018-06-12 04:46:07 | [diff] [blame] | 4830 | } |
[email protected] | 00c37fc | 2011-08-02 00:22:50 | [diff] [blame] | 4831 | |
pnoland | 48894465 | 2017-02-22 18:58:54 | [diff] [blame] | 4832 | RenderFrameHost* source_render_frame_host = RenderFrameHost::FromID( |
| 4833 | params.source_render_process_id, params.source_render_frame_id); |
| 4834 | |
David Bokan | 8cb28a52 | 2021-05-26 18:50:59 | [diff] [blame] | 4835 | // Prevent frames that are not active (e.g. a prerendering page) from opening |
| 4836 | // new windows, tabs, popups, etc. |
Ian Vollick | f6725a2e | 2021-05-20 14:21:30 | [diff] [blame] | 4837 | if (params.disposition != WindowOpenDisposition::CURRENT_TAB && |
Sreeja Kamishetty | e49854f8 | 2021-06-02 00:52:03 | [diff] [blame] | 4838 | source_render_frame_host && !source_render_frame_host->IsActive()) { |
Ian Vollick | f6725a2e | 2021-05-20 14:21:30 | [diff] [blame] | 4839 | return nullptr; |
| 4840 | } |
| 4841 | |
David Bokan | 534f1f4 | 2021-05-21 18:55:41 | [diff] [blame] | 4842 | if (params.frame_tree_node_id != FrameTreeNode::kFrameTreeNodeInvalidId) { |
| 4843 | if (auto* frame_tree_node = |
| 4844 | FrameTreeNode::GloballyFindByID(params.frame_tree_node_id)) { |
| 4845 | // If a frame tree node ID is specified and it exists, ensure it is for a |
| 4846 | // node within this WebContents. Note: this WebContents could be hosting |
| 4847 | // multiple frame trees (e.g. prerendering) so it's not enough to check |
Carlos Caballero | b65b6e3a | 2021-11-15 10:09:00 | [diff] [blame] | 4848 | // against this->primary_frame_tree_. Check against page_delegate(), which |
| 4849 | // is always a WebContentsImpl, while delegate() may be implemented by |
Matt Falkenhagen | 7b37cf49 | 2021-09-30 08:32:16 | [diff] [blame] | 4850 | // something else such as for prerendered frame trees. |
Arthur Sonzogni | f6785ec | 2022-12-05 10:11:50 | [diff] [blame] | 4851 | FrameTree& frame_tree = frame_tree_node->frame_tree(); |
| 4852 | CHECK_EQ(frame_tree.page_delegate(), this); |
David Bokan | 8cb28a52 | 2021-05-26 18:50:59 | [diff] [blame] | 4853 | |
Dave Tapuska | 42dc0310e | 2022-03-16 20:40:57 | [diff] [blame] | 4854 | // Prerendering and fenced frame navigations are hidden from embedders. |
| 4855 | // If the navigation is targeting a frame in a prerendering or fenced |
| 4856 | // frame tree, we shouldn't run that navigation through the embedder |
| 4857 | // delegate. Embedder implementations of |
| 4858 | // `WebContentsDelegate::OpenURLFromTab` assume that the primary |
| 4859 | // frame tree Navigation controller should be used for navigating. |
| 4860 | // Instead, we just navigate directly on the relevant frame |
| 4861 | // tree. |
Arthur Sonzogni | f6785ec | 2022-12-05 10:11:50 | [diff] [blame] | 4862 | if (frame_tree.type() == FrameTree::Type::kPrerender || |
Abhijeet Kandalkar | 749b8bf1 | 2022-12-14 16:41:48 | [diff] [blame] | 4863 | frame_tree_node->IsInFencedFrameTree()) { |
Hiroki Nakagawa | 5b82ee44 | 2021-09-20 17:17:20 | [diff] [blame] | 4864 | DCHECK_EQ(params.disposition, WindowOpenDisposition::CURRENT_TAB); |
Arthur Sonzogni | f6785ec | 2022-12-05 10:11:50 | [diff] [blame] | 4865 | frame_tree.controller().LoadURLWithParams( |
Hiroki Nakagawa | 5b82ee44 | 2021-09-20 17:17:20 | [diff] [blame] | 4866 | NavigationController::LoadURLParams(params)); |
| 4867 | return this; |
David Bokan | 8cb28a52 | 2021-05-26 18:50:59 | [diff] [blame] | 4868 | } |
David Bokan | 534f1f4 | 2021-05-21 18:55:41 | [diff] [blame] | 4869 | } else { |
| 4870 | // If the node doesn't exist it was probably removed from its frame tree. |
| 4871 | // In that case, abort since continuing would navigate the root frame. |
| 4872 | return nullptr; |
| 4873 | } |
| 4874 | } |
| 4875 | |
Ian Vollick | f6725a2e | 2021-05-20 14:21:30 | [diff] [blame] | 4876 | WebContents* new_contents = delegate_->OpenURLFromTab(this, params); |
| 4877 | |
pnoland | 48894465 | 2017-02-22 18:58:54 | [diff] [blame] | 4878 | if (source_render_frame_host && params.source_site_instance) { |
| 4879 | CHECK_EQ(source_render_frame_host->GetSiteInstance(), |
| 4880 | params.source_site_instance.get()); |
| 4881 | } |
| 4882 | |
| 4883 | if (new_contents && source_render_frame_host && new_contents != this) { |
Andrew Grieve | e19cd93e | 2021-01-22 05:43:06 | [diff] [blame] | 4884 | observers_.NotifyObservers( |
| 4885 | &WebContentsObserver::DidOpenRequestedURL, new_contents, |
| 4886 | source_render_frame_host, params.url, params.referrer, |
| 4887 | params.disposition, params.transition, params.started_from_context_menu, |
| 4888 | params.is_renderer_initiated); |
pnoland | 48894465 | 2017-02-22 18:58:54 | [diff] [blame] | 4889 | } |
| 4890 | |
[email protected] | e5d549d | 2011-12-28 01:29:20 | [diff] [blame] | 4891 | return new_contents; |
[email protected] | d5f942ba | 2008-09-26 19:30:34 | [diff] [blame] | 4892 | } |
| 4893 | |
avi | 2b17759 | 2014-12-10 02:08:02 | [diff] [blame] | 4894 | void WebContentsImpl::SetHistoryOffsetAndLengthForView( |
| 4895 | RenderViewHost* render_view_host, |
| 4896 | int history_offset, |
| 4897 | int history_length) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 4898 | OPTIONAL_TRACE_EVENT2( |
| 4899 | "content", "WebContentsImpl::SetHistoryOffsetAndLengthForView", |
| 4900 | "history_offset", history_offset, "history_length", history_length); |
Dave Tapuska | c088829 | 2020-10-30 22:50:10 | [diff] [blame] | 4901 | if (auto& broadcast = static_cast<RenderViewHostImpl*>(render_view_host) |
| 4902 | ->GetAssociatedPageBroadcast()) |
| 4903 | broadcast->SetHistoryOffsetAndLength(history_offset, history_length); |
[email protected] | 796931a9 | 2011-08-10 01:32:14 | [diff] [blame] | 4904 | } |
| 4905 | |
arthursonzogni | 818c264 | 2019-09-27 12:18:10 | [diff] [blame] | 4906 | void WebContentsImpl::ReloadFocusedFrame() { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 4907 | OPTIONAL_TRACE_EVENT0("content", "WebContentsImpl::ReloadFocusedFrame"); |
[email protected] | 1f3fc1d | 2014-04-03 14:50:17 | [diff] [blame] | 4908 | RenderFrameHost* focused_frame = GetFocusedFrame(); |
| 4909 | if (!focused_frame) |
| 4910 | return; |
| 4911 | |
yilkal | 921048bd | 2019-10-09 23:51:04 | [diff] [blame] | 4912 | focused_frame->Reload(); |
[email protected] | 959be4c | 2014-04-08 15:01:33 | [diff] [blame] | 4913 | } |
| 4914 | |
| 4915 | void WebContentsImpl::Undo() { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 4916 | OPTIONAL_TRACE_EVENT0("content", "WebContentsImpl::Undo"); |
Dave Tapuska | 58a099e | 2020-06-08 21:48:40 | [diff] [blame] | 4917 | auto* input_handler = GetFocusedFrameWidgetInputHandler(); |
Dave Tapuska | e782bea | 2019-07-09 16:21:51 | [diff] [blame] | 4918 | if (!input_handler) |
[email protected] | 959be4c | 2014-04-08 15:01:33 | [diff] [blame] | 4919 | return; |
| 4920 | |
Evan Stade | 6decc97 | 2022-10-24 19:42:18 | [diff] [blame] | 4921 | last_interaction_time_ = ui::EventTimeForNow(); |
Dave Tapuska | e782bea | 2019-07-09 16:21:51 | [diff] [blame] | 4922 | input_handler->Undo(); |
[email protected] | 959be4c | 2014-04-08 15:01:33 | [diff] [blame] | 4923 | RecordAction(base::UserMetricsAction("Undo")); |
| 4924 | } |
| 4925 | |
| 4926 | void WebContentsImpl::Redo() { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 4927 | OPTIONAL_TRACE_EVENT0("content", "WebContentsImpl::Redo"); |
Dave Tapuska | 58a099e | 2020-06-08 21:48:40 | [diff] [blame] | 4928 | auto* input_handler = GetFocusedFrameWidgetInputHandler(); |
Dave Tapuska | e782bea | 2019-07-09 16:21:51 | [diff] [blame] | 4929 | if (!input_handler) |
[email protected] | 959be4c | 2014-04-08 15:01:33 | [diff] [blame] | 4930 | return; |
Dave Tapuska | e782bea | 2019-07-09 16:21:51 | [diff] [blame] | 4931 | |
Evan Stade | 6decc97 | 2022-10-24 19:42:18 | [diff] [blame] | 4932 | last_interaction_time_ = ui::EventTimeForNow(); |
Dave Tapuska | e782bea | 2019-07-09 16:21:51 | [diff] [blame] | 4933 | input_handler->Redo(); |
[email protected] | 959be4c | 2014-04-08 15:01:33 | [diff] [blame] | 4934 | RecordAction(base::UserMetricsAction("Redo")); |
| 4935 | } |
| 4936 | |
| 4937 | void WebContentsImpl::Cut() { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 4938 | OPTIONAL_TRACE_EVENT0("content", "WebContentsImpl::Cut"); |
Dave Tapuska | 58a099e | 2020-06-08 21:48:40 | [diff] [blame] | 4939 | auto* input_handler = GetFocusedFrameWidgetInputHandler(); |
Dave Tapuska | e782bea | 2019-07-09 16:21:51 | [diff] [blame] | 4940 | if (!input_handler) |
[email protected] | 959be4c | 2014-04-08 15:01:33 | [diff] [blame] | 4941 | return; |
| 4942 | |
Evan Stade | 6decc97 | 2022-10-24 19:42:18 | [diff] [blame] | 4943 | last_interaction_time_ = ui::EventTimeForNow(); |
Dave Tapuska | e782bea | 2019-07-09 16:21:51 | [diff] [blame] | 4944 | input_handler->Cut(); |
[email protected] | 959be4c | 2014-04-08 15:01:33 | [diff] [blame] | 4945 | RecordAction(base::UserMetricsAction("Cut")); |
| 4946 | } |
| 4947 | |
| 4948 | void WebContentsImpl::Copy() { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 4949 | OPTIONAL_TRACE_EVENT0("content", "WebContentsImpl::Copy"); |
Dave Tapuska | 58a099e | 2020-06-08 21:48:40 | [diff] [blame] | 4950 | auto* input_handler = GetFocusedFrameWidgetInputHandler(); |
Dave Tapuska | e782bea | 2019-07-09 16:21:51 | [diff] [blame] | 4951 | if (!input_handler) |
[email protected] | 959be4c | 2014-04-08 15:01:33 | [diff] [blame] | 4952 | return; |
| 4953 | |
Evan Stade | 6decc97 | 2022-10-24 19:42:18 | [diff] [blame] | 4954 | last_interaction_time_ = ui::EventTimeForNow(); |
Dave Tapuska | e782bea | 2019-07-09 16:21:51 | [diff] [blame] | 4955 | input_handler->Copy(); |
[email protected] | 959be4c | 2014-04-08 15:01:33 | [diff] [blame] | 4956 | RecordAction(base::UserMetricsAction("Copy")); |
| 4957 | } |
| 4958 | |
| 4959 | void WebContentsImpl::CopyToFindPboard() { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 4960 | OPTIONAL_TRACE_EVENT0("content", "WebContentsImpl::CopyToFindPboard"); |
Xiaohan Wang | 24ec934 | 2022-01-15 17:34:22 | [diff] [blame] | 4961 | #if BUILDFLAG(IS_MAC) |
Dave Tapuska | 58a099e | 2020-06-08 21:48:40 | [diff] [blame] | 4962 | auto* input_handler = GetFocusedFrameWidgetInputHandler(); |
Dave Tapuska | e782bea | 2019-07-09 16:21:51 | [diff] [blame] | 4963 | if (!input_handler) |
[email protected] | 959be4c | 2014-04-08 15:01:33 | [diff] [blame] | 4964 | return; |
| 4965 | |
Evan Stade | 6decc97 | 2022-10-24 19:42:18 | [diff] [blame] | 4966 | last_interaction_time_ = ui::EventTimeForNow(); |
[email protected] | 959be4c | 2014-04-08 15:01:33 | [diff] [blame] | 4967 | // Windows/Linux don't have the concept of a find pasteboard. |
Dave Tapuska | e782bea | 2019-07-09 16:21:51 | [diff] [blame] | 4968 | input_handler->CopyToFindPboard(); |
[email protected] | 959be4c | 2014-04-08 15:01:33 | [diff] [blame] | 4969 | RecordAction(base::UserMetricsAction("CopyToFindPboard")); |
| 4970 | #endif |
| 4971 | } |
| 4972 | |
| 4973 | void WebContentsImpl::Paste() { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 4974 | OPTIONAL_TRACE_EVENT0("content", "WebContentsImpl::Paste"); |
Dave Tapuska | 58a099e | 2020-06-08 21:48:40 | [diff] [blame] | 4975 | auto* input_handler = GetFocusedFrameWidgetInputHandler(); |
Dave Tapuska | e782bea | 2019-07-09 16:21:51 | [diff] [blame] | 4976 | if (!input_handler) |
[email protected] | 959be4c | 2014-04-08 15:01:33 | [diff] [blame] | 4977 | return; |
| 4978 | |
Evan Stade | 6decc97 | 2022-10-24 19:42:18 | [diff] [blame] | 4979 | last_interaction_time_ = ui::EventTimeForNow(); |
Dave Tapuska | e782bea | 2019-07-09 16:21:51 | [diff] [blame] | 4980 | input_handler->Paste(); |
Andrew Grieve | e19cd93e | 2021-01-22 05:43:06 | [diff] [blame] | 4981 | observers_.NotifyObservers(&WebContentsObserver::OnPaste); |
[email protected] | 959be4c | 2014-04-08 15:01:33 | [diff] [blame] | 4982 | RecordAction(base::UserMetricsAction("Paste")); |
| 4983 | } |
| 4984 | |
| 4985 | void WebContentsImpl::PasteAndMatchStyle() { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 4986 | OPTIONAL_TRACE_EVENT0("content", "WebContentsImpl::PasteAndMatchStyle"); |
Dave Tapuska | 58a099e | 2020-06-08 21:48:40 | [diff] [blame] | 4987 | auto* input_handler = GetFocusedFrameWidgetInputHandler(); |
Dave Tapuska | e782bea | 2019-07-09 16:21:51 | [diff] [blame] | 4988 | if (!input_handler) |
[email protected] | 959be4c | 2014-04-08 15:01:33 | [diff] [blame] | 4989 | return; |
| 4990 | |
Evan Stade | 6decc97 | 2022-10-24 19:42:18 | [diff] [blame] | 4991 | last_interaction_time_ = ui::EventTimeForNow(); |
Dave Tapuska | e782bea | 2019-07-09 16:21:51 | [diff] [blame] | 4992 | input_handler->PasteAndMatchStyle(); |
Andrew Grieve | e19cd93e | 2021-01-22 05:43:06 | [diff] [blame] | 4993 | observers_.NotifyObservers(&WebContentsObserver::OnPaste); |
[email protected] | 959be4c | 2014-04-08 15:01:33 | [diff] [blame] | 4994 | RecordAction(base::UserMetricsAction("PasteAndMatchStyle")); |
| 4995 | } |
| 4996 | |
| 4997 | void WebContentsImpl::Delete() { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 4998 | OPTIONAL_TRACE_EVENT0("content", "WebContentsImpl::Delete"); |
Dave Tapuska | 58a099e | 2020-06-08 21:48:40 | [diff] [blame] | 4999 | auto* input_handler = GetFocusedFrameWidgetInputHandler(); |
Dave Tapuska | e782bea | 2019-07-09 16:21:51 | [diff] [blame] | 5000 | if (!input_handler) |
[email protected] | 959be4c | 2014-04-08 15:01:33 | [diff] [blame] | 5001 | return; |
| 5002 | |
Evan Stade | 6decc97 | 2022-10-24 19:42:18 | [diff] [blame] | 5003 | last_interaction_time_ = ui::EventTimeForNow(); |
Dave Tapuska | e782bea | 2019-07-09 16:21:51 | [diff] [blame] | 5004 | input_handler->Delete(); |
[email protected] | 959be4c | 2014-04-08 15:01:33 | [diff] [blame] | 5005 | RecordAction(base::UserMetricsAction("DeleteSelection")); |
| 5006 | } |
| 5007 | |
| 5008 | void WebContentsImpl::SelectAll() { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 5009 | OPTIONAL_TRACE_EVENT0("content", "WebContentsImpl::SelectAll"); |
Dave Tapuska | 58a099e | 2020-06-08 21:48:40 | [diff] [blame] | 5010 | auto* input_handler = GetFocusedFrameWidgetInputHandler(); |
Dave Tapuska | e782bea | 2019-07-09 16:21:51 | [diff] [blame] | 5011 | if (!input_handler) |
[email protected] | 959be4c | 2014-04-08 15:01:33 | [diff] [blame] | 5012 | return; |
| 5013 | |
Evan Stade | 6decc97 | 2022-10-24 19:42:18 | [diff] [blame] | 5014 | last_interaction_time_ = ui::EventTimeForNow(); |
Dave Tapuska | e782bea | 2019-07-09 16:21:51 | [diff] [blame] | 5015 | input_handler->SelectAll(); |
[email protected] | 959be4c | 2014-04-08 15:01:33 | [diff] [blame] | 5016 | RecordAction(base::UserMetricsAction("SelectAll")); |
| 5017 | } |
| 5018 | |
yabinh | 351e7ec | 2017-03-10 02:43:24 | [diff] [blame] | 5019 | void WebContentsImpl::CollapseSelection() { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 5020 | OPTIONAL_TRACE_EVENT0("content", "WebContentsImpl::CollapseSelection"); |
Dave Tapuska | 58a099e | 2020-06-08 21:48:40 | [diff] [blame] | 5021 | auto* input_handler = GetFocusedFrameWidgetInputHandler(); |
Dave Tapuska | e782bea | 2019-07-09 16:21:51 | [diff] [blame] | 5022 | if (!input_handler) |
[email protected] | 959be4c | 2014-04-08 15:01:33 | [diff] [blame] | 5023 | return; |
| 5024 | |
Evan Stade | 6decc97 | 2022-10-24 19:42:18 | [diff] [blame] | 5025 | last_interaction_time_ = ui::EventTimeForNow(); |
Dave Tapuska | e782bea | 2019-07-09 16:21:51 | [diff] [blame] | 5026 | input_handler->CollapseSelection(); |
[email protected] | 1f3fc1d | 2014-04-03 14:50:17 | [diff] [blame] | 5027 | } |
| 5028 | |
Leonard Grey | f378e7d | 2021-03-25 20:53:50 | [diff] [blame] | 5029 | void WebContentsImpl::ScrollToTopOfDocument() { |
Anton Bikineev | f62d1bf | 2021-05-15 17:56:07 | [diff] [blame] | 5030 | ExecuteEditCommand("ScrollToBeginningOfDocument", absl::nullopt); |
Leonard Grey | f378e7d | 2021-03-25 20:53:50 | [diff] [blame] | 5031 | } |
| 5032 | |
| 5033 | void WebContentsImpl::ScrollToBottomOfDocument() { |
Anton Bikineev | f62d1bf | 2021-05-15 17:56:07 | [diff] [blame] | 5034 | ExecuteEditCommand("ScrollToEndOfDocument", absl::nullopt); |
Leonard Grey | f378e7d | 2021-03-25 20:53:50 | [diff] [blame] | 5035 | } |
| 5036 | |
Jan Wilken Dörrie | aace0cfef | 2021-03-11 22:01:58 | [diff] [blame] | 5037 | void WebContentsImpl::Replace(const std::u16string& word) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 5038 | OPTIONAL_TRACE_EVENT0("content", "WebContentsImpl::Replace"); |
Dave Tapuska | 58a099e | 2020-06-08 21:48:40 | [diff] [blame] | 5039 | auto* input_handler = GetFocusedFrameWidgetInputHandler(); |
Dave Tapuska | e782bea | 2019-07-09 16:21:51 | [diff] [blame] | 5040 | if (!input_handler) |
[email protected] | 1f3fc1d | 2014-04-03 14:50:17 | [diff] [blame] | 5041 | return; |
| 5042 | |
Evan Stade | 6decc97 | 2022-10-24 19:42:18 | [diff] [blame] | 5043 | last_interaction_time_ = ui::EventTimeForNow(); |
Dave Tapuska | e782bea | 2019-07-09 16:21:51 | [diff] [blame] | 5044 | input_handler->Replace(word); |
[email protected] | 1f3fc1d | 2014-04-03 14:50:17 | [diff] [blame] | 5045 | } |
| 5046 | |
Jan Wilken Dörrie | aace0cfef | 2021-03-11 22:01:58 | [diff] [blame] | 5047 | void WebContentsImpl::ReplaceMisspelling(const std::u16string& word) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 5048 | OPTIONAL_TRACE_EVENT0("content", "WebContentsImpl::ReplaceMisspelling"); |
Dave Tapuska | 58a099e | 2020-06-08 21:48:40 | [diff] [blame] | 5049 | auto* input_handler = GetFocusedFrameWidgetInputHandler(); |
Dave Tapuska | e782bea | 2019-07-09 16:21:51 | [diff] [blame] | 5050 | if (!input_handler) |
[email protected] | 1f3fc1d | 2014-04-03 14:50:17 | [diff] [blame] | 5051 | return; |
| 5052 | |
Evan Stade | 6decc97 | 2022-10-24 19:42:18 | [diff] [blame] | 5053 | last_interaction_time_ = ui::EventTimeForNow(); |
Dave Tapuska | e782bea | 2019-07-09 16:21:51 | [diff] [blame] | 5054 | input_handler->ReplaceMisspelling(word); |
[email protected] | 959be4c | 2014-04-08 15:01:33 | [diff] [blame] | 5055 | } |
| 5056 | |
Gyuyoung Kim | 1bc1ba8 | 2021-02-08 23:32:44 | [diff] [blame] | 5057 | void WebContentsImpl::NotifyContextMenuClosed(const GURL& link_followed) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 5058 | OPTIONAL_TRACE_EVENT0("content", "WebContentsImpl::NotifyContextMenuClosed"); |
[email protected] | 959be4c | 2014-04-08 15:01:33 | [diff] [blame] | 5059 | RenderFrameHost* focused_frame = GetFocusedFrame(); |
| 5060 | if (!focused_frame) |
| 5061 | return; |
| 5062 | |
Gyuyoung Kim | 1bc1ba8 | 2021-02-08 23:32:44 | [diff] [blame] | 5063 | if (context_menu_client_) |
| 5064 | context_menu_client_->ContextMenuClosed(link_followed); |
| 5065 | |
| 5066 | context_menu_client_.reset(); |
[email protected] | 959be4c | 2014-04-08 15:01:33 | [diff] [blame] | 5067 | } |
| 5068 | |
| 5069 | void WebContentsImpl::ExecuteCustomContextMenuCommand( |
Gyuyoung Kim | 49b81f9 | 2021-01-23 10:20:58 | [diff] [blame] | 5070 | int action, |
Gyuyoung Kim | 1bc1ba8 | 2021-02-08 23:32:44 | [diff] [blame] | 5071 | const GURL& link_followed) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 5072 | OPTIONAL_TRACE_EVENT1("content", |
| 5073 | "WebContentsImpl::ExecuteCustomContextMenuCommand", |
| 5074 | "action", action); |
[email protected] | 959be4c | 2014-04-08 15:01:33 | [diff] [blame] | 5075 | RenderFrameHost* focused_frame = GetFocusedFrame(); |
| 5076 | if (!focused_frame) |
| 5077 | return; |
| 5078 | |
Gyuyoung Kim | 1bc1ba8 | 2021-02-08 23:32:44 | [diff] [blame] | 5079 | if (context_menu_client_) |
| 5080 | context_menu_client_->CustomContextMenuAction(action); |
[email protected] | 4fed370 | 2014-04-01 09:08:00 | [diff] [blame] | 5081 | } |
| 5082 | |
[email protected] | fc2b46b | 2014-05-03 16:33:45 | [diff] [blame] | 5083 | gfx::NativeView WebContentsImpl::GetNativeView() { |
| 5084 | return view_->GetNativeView(); |
| 5085 | } |
| 5086 | |
| 5087 | gfx::NativeView WebContentsImpl::GetContentNativeView() { |
| 5088 | return view_->GetContentNativeView(); |
| 5089 | } |
| 5090 | |
| 5091 | gfx::NativeWindow WebContentsImpl::GetTopLevelNativeWindow() { |
| 5092 | return view_->GetTopLevelNativeWindow(); |
| 5093 | } |
| 5094 | |
| 5095 | gfx::Rect WebContentsImpl::GetViewBounds() { |
| 5096 | return view_->GetViewBounds(); |
| 5097 | } |
| 5098 | |
| 5099 | gfx::Rect WebContentsImpl::GetContainerBounds() { |
Jeremy Roman | 352239c | 2020-05-21 16:31:32 | [diff] [blame] | 5100 | return view_->GetContainerBounds(); |
[email protected] | fc2b46b | 2014-05-03 16:33:45 | [diff] [blame] | 5101 | } |
| 5102 | |
| 5103 | DropData* WebContentsImpl::GetDropData() { |
| 5104 | return view_->GetDropData(); |
| 5105 | } |
| 5106 | |
| 5107 | void WebContentsImpl::Focus() { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 5108 | OPTIONAL_TRACE_EVENT0("content", "WebContentsImpl::Focus"); |
miu | 9e14e49 | 2014-10-25 02:39:04 | [diff] [blame] | 5109 | view_->Focus(); |
[email protected] | fc2b46b | 2014-05-03 16:33:45 | [diff] [blame] | 5110 | } |
| 5111 | |
| 5112 | void WebContentsImpl::SetInitialFocus() { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 5113 | OPTIONAL_TRACE_EVENT0("content", "WebContentsImpl::SetInitialFocus"); |
miu | 9e14e49 | 2014-10-25 02:39:04 | [diff] [blame] | 5114 | view_->SetInitialFocus(); |
[email protected] | fc2b46b | 2014-05-03 16:33:45 | [diff] [blame] | 5115 | } |
| 5116 | |
| 5117 | void WebContentsImpl::StoreFocus() { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 5118 | OPTIONAL_TRACE_EVENT0("content", "WebContentsImpl::StoreFocus"); |
miu | 9e14e49 | 2014-10-25 02:39:04 | [diff] [blame] | 5119 | view_->StoreFocus(); |
[email protected] | fc2b46b | 2014-05-03 16:33:45 | [diff] [blame] | 5120 | } |
| 5121 | |
| 5122 | void WebContentsImpl::RestoreFocus() { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 5123 | OPTIONAL_TRACE_EVENT0("content", "WebContentsImpl::RestoreFocus"); |
miu | 9e14e49 | 2014-10-25 02:39:04 | [diff] [blame] | 5124 | view_->RestoreFocus(); |
[email protected] | fc2b46b | 2014-05-03 16:33:45 | [diff] [blame] | 5125 | } |
| 5126 | |
[email protected] | b172aee | 2012-04-10 17:05:26 | [diff] [blame] | 5127 | void WebContentsImpl::FocusThroughTabTraversal(bool reverse) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 5128 | OPTIONAL_TRACE_EVENT1("content", "WebContentsImpl::FocusThroughTabTraversal", |
| 5129 | "reverse", reverse); |
Maxim Podgorodskiy | a926c4ff | 2017-11-20 02:06:39 | [diff] [blame] | 5130 | view_->FocusThroughTabTraversal(reverse); |
[email protected] | 96d185d | 2009-04-24 03:28:54 | [diff] [blame] | 5131 | } |
| 5132 | |
[email protected] | b172aee | 2012-04-10 17:05:26 | [diff] [blame] | 5133 | bool WebContentsImpl::IsSavable() { |
[email protected] | a53209b | 2012-01-20 16:48:16 | [diff] [blame] | 5134 | // WebKit creates Document object when MIME type is application/xhtml+xml, |
| 5135 | // so we also support this MIME type. |
Yuzu Saijo | c23e9b7 | 2020-06-25 07:21:27 | [diff] [blame] | 5136 | std::string mime_type = GetContentsMimeType(); |
| 5137 | return mime_type == "text/html" || mime_type == "text/xml" || |
| 5138 | mime_type == "application/xhtml+xml" || mime_type == "text/plain" || |
| 5139 | mime_type == "text/css" || |
| 5140 | blink::IsSupportedJavascriptMimeType(mime_type); |
[email protected] | a53209b | 2012-01-20 16:48:16 | [diff] [blame] | 5141 | } |
| 5142 | |
[email protected] | b172aee | 2012-04-10 17:05:26 | [diff] [blame] | 5143 | void WebContentsImpl::OnSavePage() { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 5144 | OPTIONAL_TRACE_EVENT0("content", "WebContentsImpl::OnSavePage"); |
[email protected] | c7dd2f6 | 2011-07-18 15:57:59 | [diff] [blame] | 5145 | // If we can not save the page, try to download it. |
[email protected] | a53209b | 2012-01-20 16:48:16 | [diff] [blame] | 5146 | if (!IsSavable()) { |
Min Qin | da0ed206 | 2018-02-23 22:00:53 | [diff] [blame] | 5147 | download::RecordSavePackageEvent( |
| 5148 | download::SAVE_PACKAGE_DOWNLOAD_ON_NON_HTML); |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 5149 | SaveFrame(GetLastCommittedURL(), Referrer(), GetPrimaryMainFrame()); |
[email protected] | 27678b2a | 2012-02-04 22:09:14 | [diff] [blame] | 5150 | return; |
[email protected] | c7dd2f6 | 2011-07-18 15:57:59 | [diff] [blame] | 5151 | } |
| 5152 | |
| 5153 | Stop(); |
| 5154 | |
| 5155 | // Create the save package and possibly prompt the user for the name to save |
| 5156 | // the page as. The user prompt is an asynchronous operation that runs on |
| 5157 | // another thread. |
Dave Tapuska | 765a2f6 | 2021-07-07 20:52:23 | [diff] [blame] | 5158 | save_package_ = new SavePackage(GetPrimaryPage()); |
[email protected] | c7dd2f6 | 2011-07-18 15:57:59 | [diff] [blame] | 5159 | save_package_->GetSaveInfo(); |
| 5160 | } |
| 5161 | |
| 5162 | // Used in automated testing to bypass prompting the user for file names. |
| 5163 | // Instead, the names and paths are hard coded rather than running them through |
| 5164 | // file name sanitation and extension / mime checking. |
[email protected] | 2dec8ec | 2013-02-07 19:20:34 | [diff] [blame] | 5165 | bool WebContentsImpl::SavePage(const base::FilePath& main_file, |
| 5166 | const base::FilePath& dir_path, |
[email protected] | 8ff00d7 | 2012-10-23 19:12:21 | [diff] [blame] | 5167 | SavePageType save_type) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 5168 | OPTIONAL_TRACE_EVENT2("content", "WebContentsImpl::SavePage", "main_file", |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 5169 | main_file, "dir_path", dir_path); |
[email protected] | c7dd2f6 | 2011-07-18 15:57:59 | [diff] [blame] | 5170 | // Stop the page from navigating. |
| 5171 | Stop(); |
| 5172 | |
Dave Tapuska | 765a2f6 | 2021-07-07 20:52:23 | [diff] [blame] | 5173 | save_package_ = |
| 5174 | new SavePackage(GetPrimaryPage(), save_type, main_file, dir_path); |
[email protected] | 8ff00d7 | 2012-10-23 19:12:21 | [diff] [blame] | 5175 | return save_package_->Init(SavePackageDownloadCreatedCallback()); |
[email protected] | c7dd2f6 | 2011-07-18 15:57:59 | [diff] [blame] | 5176 | } |
| 5177 | |
Yao Xiao | e500704 | 2021-02-26 02:32:09 | [diff] [blame] | 5178 | void WebContentsImpl::SaveFrame(const GURL& url, |
| 5179 | const Referrer& referrer, |
| 5180 | RenderFrameHost* rfh) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 5181 | OPTIONAL_TRACE_EVENT0("content", "WebContentsImpl::SaveFrame"); |
Jan Wilken Dörrie | aace0cfef | 2021-03-11 22:01:58 | [diff] [blame] | 5182 | SaveFrameWithHeaders(url, referrer, std::string(), std::u16string(), rfh); |
kundaji | 6c7f969 | 2015-03-09 18:00:37 | [diff] [blame] | 5183 | } |
| 5184 | |
Xing Liu | 10329bf | 2018-03-20 19:22:14 | [diff] [blame] | 5185 | void WebContentsImpl::SaveFrameWithHeaders( |
| 5186 | const GURL& url, |
| 5187 | const Referrer& referrer, |
| 5188 | const std::string& headers, |
Jan Wilken Dörrie | aace0cfef | 2021-03-11 22:01:58 | [diff] [blame] | 5189 | const std::u16string& suggested_filename, |
Yao Xiao | e500704 | 2021-02-26 02:32:09 | [diff] [blame] | 5190 | RenderFrameHost* rfh) { |
| 5191 | DCHECK(rfh); |
Jeremy Roman | cfa82c65 | 2021-07-06 21:43:27 | [diff] [blame] | 5192 | auto& rfhi = *static_cast<RenderFrameHostImpl*>(rfh); |
Yao Xiao | e500704 | 2021-02-26 02:32:09 | [diff] [blame] | 5193 | |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 5194 | OPTIONAL_TRACE_EVENT2("content", "WebContentsImpl::SaveFrameWithHeaders", |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 5195 | "url", url, "headers", headers); |
Lei Zhang | 13a42e4 | 2019-02-19 23:28:11 | [diff] [blame] | 5196 | // Check and see if the guest can handle this. |
| 5197 | if (delegate_) { |
| 5198 | WebContents* guest_web_contents = nullptr; |
| 5199 | if (browser_plugin_embedder_) { |
| 5200 | BrowserPluginGuest* guest = browser_plugin_embedder_->GetFullPageGuest(); |
| 5201 | if (guest) |
| 5202 | guest_web_contents = guest->GetWebContents(); |
| 5203 | } else if (browser_plugin_guest_) { |
| 5204 | guest_web_contents = this; |
| 5205 | } |
| 5206 | |
| 5207 | if (guest_web_contents && delegate_->GuestSaveFrame(guest_web_contents)) |
| 5208 | return; |
| 5209 | } |
| 5210 | |
nasko | 89ad774 | 2015-03-05 22:14:19 | [diff] [blame] | 5211 | if (!GetLastCommittedURL().is_valid()) |
[email protected] | 3c71576ce | 2013-07-23 02:00:01 | [diff] [blame] | 5212 | return; |
Yao Xiao | e500704 | 2021-02-26 02:32:09 | [diff] [blame] | 5213 | if (delegate_ && delegate_->SaveFrame(url, referrer, rfh)) |
sammc | 92af6155 | 2014-11-19 23:27:40 | [diff] [blame] | 5214 | return; |
| 5215 | |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 5216 | int64_t post_id = -1; |
Jeremy Roman | cfa82c65 | 2021-07-06 21:43:27 | [diff] [blame] | 5217 | if (rfhi.is_main_frame()) { |
| 5218 | NavigationEntry* entry = |
| 5219 | rfhi.frame_tree()->controller().GetLastCommittedEntry(); |
[email protected] | 3c71576ce | 2013-07-23 02:00:01 | [diff] [blame] | 5220 | if (entry) |
| 5221 | post_id = entry->GetPostID(); |
| 5222 | } |
Ramin Halavati | 24e2101 | 2017-07-10 12:56:06 | [diff] [blame] | 5223 | net::NetworkTrafficAnnotationTag traffic_annotation = |
| 5224 | net::DefineNetworkTrafficAnnotation("download_web_contents_frame", R"( |
| 5225 | semantics { |
| 5226 | sender: "Save Page Action" |
| 5227 | description: |
| 5228 | "Saves the given frame's URL to the local file system." |
| 5229 | trigger: |
| 5230 | "The user has triggered a save operation on the frame through a " |
| 5231 | "context menu or other mechanism." |
| 5232 | data: "None." |
| 5233 | destination: WEBSITE |
| 5234 | } |
| 5235 | policy { |
Ramin Halavati | 3b97978 | 2017-07-21 11:40:26 | [diff] [blame] | 5236 | cookies_allowed: YES |
Ramin Halavati | 24e2101 | 2017-07-10 12:56:06 | [diff] [blame] | 5237 | cookies_store: "user" |
| 5238 | setting: |
| 5239 | "This feature cannot be disabled by settings, but it's is only " |
| 5240 | "triggered by user request." |
| 5241 | policy_exception_justification: "Not implemented." |
| 5242 | })"); |
Min Qin | a904f330 | 2018-02-13 23:33:34 | [diff] [blame] | 5243 | auto params = std::make_unique<download::DownloadUrlParameters>( |
Jeremy Roman | cfa82c65 | 2021-07-06 21:43:27 | [diff] [blame] | 5244 | url, rfh->GetProcess()->GetID(), rfh->GetRoutingID(), traffic_annotation); |
Min Qin | a904f330 | 2018-02-13 23:33:34 | [diff] [blame] | 5245 | params->set_referrer(referrer.url); |
| 5246 | params->set_referrer_policy( |
| 5247 | Referrer::ReferrerPolicyForUrlRequest(referrer.policy)); |
[email protected] | 3c71576ce | 2013-07-23 02:00:01 | [diff] [blame] | 5248 | params->set_post_id(post_id); |
[email protected] | 3c71576ce | 2013-07-23 02:00:01 | [diff] [blame] | 5249 | if (post_id >= 0) |
| 5250 | params->set_method("POST"); |
| 5251 | params->set_prompt(true); |
kundaji | 6c7f969 | 2015-03-09 18:00:37 | [diff] [blame] | 5252 | |
Min Qin | 7e82cc7 | 2021-12-28 19:20:49 | [diff] [blame] | 5253 | if (!headers.empty()) { |
Min Qin | a904f330 | 2018-02-13 23:33:34 | [diff] [blame] | 5254 | for (download::DownloadUrlParameters::RequestHeadersNameValuePair |
| 5255 | key_value : ParseDownloadHeaders(headers)) { |
Megan Jablonski | 2f6a4c5 | 2017-07-10 23:01:25 | [diff] [blame] | 5256 | params->add_request_header(key_value.first, key_value.second); |
kundaji | 6c7f969 | 2015-03-09 18:00:37 | [diff] [blame] | 5257 | } |
| 5258 | } |
Min Qin | 7e82cc7 | 2021-12-28 19:20:49 | [diff] [blame] | 5259 | params->set_prefer_cache(true); |
Xing Liu | 10329bf | 2018-03-20 19:22:14 | [diff] [blame] | 5260 | params->set_suggested_name(suggested_filename); |
Min Qin | 0ca8e1ee | 2018-01-31 00:49:35 | [diff] [blame] | 5261 | params->set_download_source(download::DownloadSource::WEB_CONTENTS_API); |
Jeremy Roman | cfa82c65 | 2021-07-06 21:43:27 | [diff] [blame] | 5262 | params->set_isolation_info(rfhi.ComputeIsolationInfoForNavigation(url)); |
Yao Xiao | e500704 | 2021-02-26 02:32:09 | [diff] [blame] | 5263 | |
Lukasz Anforowicz | 48d8345 | 2021-05-12 02:58:20 | [diff] [blame] | 5264 | GetBrowserContext()->GetDownloadManager()->DownloadUrl(std::move(params)); |
[email protected] | 3c71576ce | 2013-07-23 02:00:01 | [diff] [blame] | 5265 | } |
| 5266 | |
[email protected] | b172aee | 2012-04-10 17:05:26 | [diff] [blame] | 5267 | void WebContentsImpl::GenerateMHTML( |
dewittj | 6dc5747a | 2016-05-17 01:48:47 | [diff] [blame] | 5268 | const MHTMLGenerationParams& params, |
Avi Drissman | 149b783 | 2018-03-23 14:31:49 | [diff] [blame] | 5269 | base::OnceCallback<void(int64_t)> callback) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 5270 | OPTIONAL_TRACE_EVENT0("content", "WebContentsImpl::GenerateMHTML"); |
Angel Alvarez | 90249668 | 2019-08-27 22:58:42 | [diff] [blame] | 5271 | base::OnceCallback<void(const MHTMLGenerationResult&)> wrapper_callback = |
| 5272 | base::BindOnce( |
| 5273 | [](base::OnceCallback<void(int64_t)> size_callback, |
| 5274 | const MHTMLGenerationResult& result) { |
| 5275 | std::move(size_callback).Run(result.file_size); |
| 5276 | }, |
| 5277 | std::move(callback)); |
| 5278 | MHTMLGenerationManager::GetInstance()->SaveMHTML(this, params, |
| 5279 | std::move(wrapper_callback)); |
| 5280 | } |
| 5281 | |
| 5282 | void WebContentsImpl::GenerateMHTMLWithResult( |
| 5283 | const MHTMLGenerationParams& params, |
| 5284 | MHTMLGenerationResult::GenerateMHTMLCallback callback) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 5285 | OPTIONAL_TRACE_EVENT0("content", "WebContentsImpl::GenerateMHTMLWithResult"); |
Avi Drissman | 149b783 | 2018-03-23 14:31:49 | [diff] [blame] | 5286 | MHTMLGenerationManager::GetInstance()->SaveMHTML(this, params, |
| 5287 | std::move(callback)); |
[email protected] | aa4f397 | 2012-03-01 18:12:12 | [diff] [blame] | 5288 | } |
| 5289 | |
Lucas Furukawa Gadani | e1c5dfda | 2018-11-29 17:57:41 | [diff] [blame] | 5290 | const std::string& WebContentsImpl::GetContentsMimeType() { |
Jeremy Roman | 2d8dfe13 | 2021-07-06 20:51:26 | [diff] [blame] | 5291 | return GetPrimaryPage().contents_mime_type(); |
[email protected] | 0bfbf88 | 2011-12-22 18:19:27 | [diff] [blame] | 5292 | } |
| 5293 | |
Mario Sanchez Prada | 0bd8b8c | 2020-10-21 17:49:23 | [diff] [blame] | 5294 | blink::RendererPreferences* WebContentsImpl::GetMutableRendererPrefs() { |
[email protected] | 0bfbf88 | 2011-12-22 18:19:27 | [diff] [blame] | 5295 | return &renderer_preferences_; |
| 5296 | } |
| 5297 | |
Ella Ge | 80a52dce | 2017-11-15 18:01:52 | [diff] [blame] | 5298 | void WebContentsImpl::DragSourceEndedAt(float client_x, |
| 5299 | float client_y, |
| 5300 | float screen_x, |
| 5301 | float screen_y, |
Henrique Ferreiro | 299008ec | 2020-12-29 14:36:28 | [diff] [blame] | 5302 | ui::mojom::DragOperation operation, |
Paul Meyer | 0c58c371 | 2016-11-17 22:59:51 | [diff] [blame] | 5303 | RenderWidgetHost* source_rwh) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 5304 | OPTIONAL_TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("content.verbose"), |
| 5305 | "WebContentsImpl::DragSourceEndedAt"); |
Paul Meyer | 0c58c371 | 2016-11-17 22:59:51 | [diff] [blame] | 5306 | if (source_rwh) { |
Ella Ge | 80a52dce | 2017-11-15 18:01:52 | [diff] [blame] | 5307 | source_rwh->DragSourceEndedAt(gfx::PointF(client_x, client_y), |
Joel Einbinder | 64dd1244 | 2021-04-06 06:09:27 | [diff] [blame] | 5308 | gfx::PointF(screen_x, screen_y), operation, |
| 5309 | base::DoNothing()); |
Paul Meyer | 0c58c371 | 2016-11-17 22:59:51 | [diff] [blame] | 5310 | } |
[email protected] | cf200a56 | 2013-05-03 16:24:29 | [diff] [blame] | 5311 | } |
| 5312 | |
Dave Tapuska | e1a08aaf | 2021-03-05 18:31:59 | [diff] [blame] | 5313 | void WebContentsImpl::LoadStateChanged(network::mojom::LoadInfoPtr load_info) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 5314 | OPTIONAL_TRACE_EVENT2("content", "WebContentsImpl::LoadStateChanged", "host", |
Dave Tapuska | e1a08aaf | 2021-03-05 18:31:59 | [diff] [blame] | 5315 | load_info->host, "load_state", load_info->load_state); |
| 5316 | |
| 5317 | // If the new load state isn't progressed as far as the current loading state |
| 5318 | // or both are sending an upload and the upload is smaller, return early |
| 5319 | // discarding the new load state. |
| 5320 | if (load_info_timestamp_ + kUpdateLoadStatesInterval > load_info->timestamp && |
| 5321 | (load_state_.state > load_info->load_state || |
| 5322 | (load_state_.state == load_info->load_state && |
| 5323 | load_state_.state == net::LOAD_STATE_SENDING_REQUEST && |
| 5324 | upload_size_ > load_info->upload_size))) { |
Elly Fong-Jones | cad9c3d1 | 2018-01-31 17:26:10 | [diff] [blame] | 5325 | return; |
| 5326 | } |
Dave Tapuska | e1a08aaf | 2021-03-05 18:31:59 | [diff] [blame] | 5327 | |
| 5328 | load_info_timestamp_ = load_info->timestamp; |
Jan Wilken Dörrie | aace0cfef | 2021-03-11 22:01:58 | [diff] [blame] | 5329 | std::u16string host16 = url_formatter::IDNToUnicode(load_info->host); |
Dave Tapuska | e1a08aaf | 2021-03-05 18:31:59 | [diff] [blame] | 5330 | // Drop no-op updates. |
| 5331 | if (load_state_.state == load_info->load_state && |
| 5332 | load_state_.param == load_info->state_param && |
| 5333 | upload_position_ == load_info->upload_position && |
| 5334 | upload_size_ == load_info->upload_size && load_state_host_ == host16) { |
| 5335 | return; |
| 5336 | } |
| 5337 | load_state_ = net::LoadStateWithParam( |
| 5338 | static_cast<net::LoadState>(load_info->load_state), |
| 5339 | load_info->state_param); |
Charles Harrison | 911fa06 | 2018-03-06 21:01:46 | [diff] [blame] | 5340 | load_state_host_ = host16; |
jam | 73f89264 | 2016-09-11 06:04:06 | [diff] [blame] | 5341 | } |
| 5342 | |
Collin Baker | 989e088 | 2019-11-01 01:27:17 | [diff] [blame] | 5343 | void WebContentsImpl::SetVisibilityAndNotifyObservers(Visibility visibility) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 5344 | OPTIONAL_TRACE_EVENT1("content", |
| 5345 | "WebContentsImpl::SetVisibilityAndNotifyObservers", |
| 5346 | "visibility", static_cast<int>(visibility)); |
Francois Doray | e616115 | 2018-03-27 22:05:37 | [diff] [blame] | 5347 | const Visibility previous_visibility = visibility_; |
| 5348 | visibility_ = visibility; |
| 5349 | |
Francois Doray | fe4a177 | 2018-02-17 04:17:09 | [diff] [blame] | 5350 | // Notify observers if the visibility changed or if WasShown() is being called |
| 5351 | // for the first time. |
Francois Doray | fe4a177 | 2018-02-17 04:17:09 | [diff] [blame] | 5352 | if (visibility != previous_visibility || |
| 5353 | (visibility == Visibility::VISIBLE && !did_first_set_visible_)) { |
Clark DuVall | 22aaff3 | 2021-03-18 17:17:47 | [diff] [blame] | 5354 | SCOPED_UMA_HISTOGRAM_TIMER("WebContentsObserver.OnVisibilityChanged"); |
Andrew Grieve | e19cd93e | 2021-01-22 05:43:06 | [diff] [blame] | 5355 | observers_.NotifyObservers(&WebContentsObserver::OnVisibilityChanged, |
| 5356 | visibility); |
Francois Doray | fe4a177 | 2018-02-17 04:17:09 | [diff] [blame] | 5357 | } |
| 5358 | } |
| 5359 | |
Michael Thiessen | 896405db | 2017-07-20 17:52:32 | [diff] [blame] | 5360 | void WebContentsImpl::NotifyWebContentsFocused( |
| 5361 | RenderWidgetHost* render_widget_host) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 5362 | OPTIONAL_TRACE_EVENT1("content", "WebContentsImpl::NotifyWebContentsFocused", |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 5363 | "render_widget_host", render_widget_host); |
Andrew Grieve | e19cd93e | 2021-01-22 05:43:06 | [diff] [blame] | 5364 | observers_.NotifyObservers(&WebContentsObserver::OnWebContentsFocused, |
| 5365 | render_widget_host); |
calamity | 7fe55ce | 2015-04-10 03:59:37 | [diff] [blame] | 5366 | } |
| 5367 | |
Michael Thiessen | 896405db | 2017-07-20 17:52:32 | [diff] [blame] | 5368 | void WebContentsImpl::NotifyWebContentsLostFocus( |
| 5369 | RenderWidgetHost* render_widget_host) { |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 5370 | OPTIONAL_TRACE_EVENT1("content", |
| 5371 | "WebContentsImpl::NotifyWebContentsLostFocus", |
| 5372 | "render_widget_host", render_widget_host); |
Andrew Grieve | e19cd93e | 2021-01-22 05:43:06 | [diff] [blame] | 5373 | observers_.NotifyObservers(&WebContentsObserver::OnWebContentsLostFocus, |
| 5374 | render_widget_host); |
zijiehe | 3bee08e2 | 2017-05-17 04:14:46 | [diff] [blame] | 5375 | } |
| 5376 | |
Paul Meyer | 0c58c371 | 2016-11-17 22:59:51 | [diff] [blame] | 5377 | void WebContentsImpl::SystemDragEnded(RenderWidgetHost* source_rwh) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 5378 | OPTIONAL_TRACE_EVENT1("content", "WebContentsImpl::SystemDragEnded", |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 5379 | "render_widget_host", source_rwh); |
Paul Meyer | 0c58c371 | 2016-11-17 22:59:51 | [diff] [blame] | 5380 | if (source_rwh) |
| 5381 | source_rwh->DragSourceSystemDragEnded(); |
[email protected] | 7813bd7 | 2011-02-05 02:19:34 | [diff] [blame] | 5382 | } |
| 5383 | |
[email protected] | b172aee | 2012-04-10 17:05:26 | [diff] [blame] | 5384 | void WebContentsImpl::SetClosedByUserGesture(bool value) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 5385 | OPTIONAL_TRACE_EVENT1("content", "WebContentsImpl::SetClosedByUserGesture", |
| 5386 | "value", value); |
[email protected] | 0bfbf88 | 2011-12-22 18:19:27 | [diff] [blame] | 5387 | closed_by_user_gesture_ = value; |
| 5388 | } |
| 5389 | |
Lucas Furukawa Gadani | e1c5dfda | 2018-11-29 17:57:41 | [diff] [blame] | 5390 | bool WebContentsImpl::GetClosedByUserGesture() { |
[email protected] | 0bfbf88 | 2011-12-22 18:19:27 | [diff] [blame] | 5391 | return closed_by_user_gesture_; |
| 5392 | } |
| 5393 | |
Lucas Furukawa Gadani | e1c5dfda | 2018-11-29 17:57:41 | [diff] [blame] | 5394 | int WebContentsImpl::GetMinimumZoomPercent() { |
[email protected] | 0bfbf88 | 2011-12-22 18:19:27 | [diff] [blame] | 5395 | return minimum_zoom_percent_; |
| 5396 | } |
| 5397 | |
Lucas Furukawa Gadani | e1c5dfda | 2018-11-29 17:57:41 | [diff] [blame] | 5398 | int WebContentsImpl::GetMaximumZoomPercent() { |
[email protected] | 0bfbf88 | 2011-12-22 18:19:27 | [diff] [blame] | 5399 | return maximum_zoom_percent_; |
| 5400 | } |
| 5401 | |
Dave Tapuska | 8f7c387 | 2020-02-07 01:16:38 | [diff] [blame] | 5402 | void WebContentsImpl::SetPageScale(float scale_factor) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 5403 | OPTIONAL_TRACE_EVENT1("content", "WebContentsImpl::SetPageScale", |
| 5404 | "scale_factor", scale_factor); |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 5405 | GetPrimaryMainFrame()->GetAssociatedLocalMainFrame()->SetScaleFactor( |
| 5406 | scale_factor); |
ccameron | b7c1d6c | 2015-03-09 17:08:24 | [diff] [blame] | 5407 | } |
| 5408 | |
Lucas Furukawa Gadani | e1c5dfda | 2018-11-29 17:57:41 | [diff] [blame] | 5409 | gfx::Size WebContentsImpl::GetPreferredSize() { |
Francois Doray | 24fc62c | 2017-12-11 17:27:33 | [diff] [blame] | 5410 | return IsBeingCaptured() ? preferred_size_for_capture_ : preferred_size_; |
[email protected] | bcd281560 | 2012-01-14 18:17:23 | [diff] [blame] | 5411 | } |
| 5412 | |
James Hollyer | d5c9de46 | 2020-03-10 19:02:45 | [diff] [blame] | 5413 | bool WebContentsImpl::GotResponseToLockMouseRequest( |
| 5414 | blink::mojom::PointerLockResult result) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 5415 | OPTIONAL_TRACE_EVENT0("content", |
| 5416 | "WebContentsImpl::GotResponseToLockMouseRequest"); |
lfg | f0cd46e | 2017-01-04 00:05:23 | [diff] [blame] | 5417 | if (mouse_lock_widget_) { |
Matt Falkenhagen | 888db59 | 2021-09-07 20:20:25 | [diff] [blame] | 5418 | auto* web_contents = |
| 5419 | WebContentsImpl::FromRenderWidgetHostImpl(mouse_lock_widget_); |
| 5420 | if (web_contents != this) |
| 5421 | return web_contents->GotResponseToLockMouseRequest(result); |
lfg | ad824435 | 2016-07-13 16:55:51 | [diff] [blame] | 5422 | |
James Hollyer | d5c9de46 | 2020-03-10 19:02:45 | [diff] [blame] | 5423 | if (mouse_lock_widget_->GotResponseToLockMouseRequest(result)) |
lfg | f0cd46e | 2017-01-04 00:05:23 | [diff] [blame] | 5424 | return true; |
| 5425 | } |
| 5426 | |
| 5427 | for (WebContentsImpl* current = this; current; |
| 5428 | current = current->GetOuterWebContents()) { |
| 5429 | current->mouse_lock_widget_ = nullptr; |
| 5430 | } |
| 5431 | |
lfg | bee1e0a | 2016-06-08 21:24:21 | [diff] [blame] | 5432 | return false; |
[email protected] | 0bfbf88 | 2011-12-22 18:19:27 | [diff] [blame] | 5433 | } |
| 5434 | |
James Hollyer | d5c9de46 | 2020-03-10 19:02:45 | [diff] [blame] | 5435 | void WebContentsImpl::GotLockMousePermissionResponse(bool allowed) { |
| 5436 | GotResponseToLockMouseRequest( |
| 5437 | allowed ? blink::mojom::PointerLockResult::kSuccess |
| 5438 | : blink::mojom::PointerLockResult::kPermissionDenied); |
| 5439 | } |
| 5440 | |
Dave Tapuska | b499878 | 2020-10-08 17:22:47 | [diff] [blame] | 5441 | void WebContentsImpl::DropMouseLockForTesting() { |
| 5442 | if (mouse_lock_widget_) { |
| 5443 | mouse_lock_widget_->RejectMouseLockOrUnlockIfNecessary( |
| 5444 | blink::mojom::PointerLockResult::kUnknownError); |
| 5445 | for (WebContentsImpl* current = this; current; |
| 5446 | current = current->GetOuterWebContents()) { |
| 5447 | current->mouse_lock_widget_ = nullptr; |
| 5448 | } |
| 5449 | } |
| 5450 | } |
| 5451 | |
Joe Downing | 13dd76b | 2018-04-09 18:32:15 | [diff] [blame] | 5452 | bool WebContentsImpl::GotResponseToKeyboardLockRequest(bool allowed) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 5453 | OPTIONAL_TRACE_EVENT1("content", |
| 5454 | "WebContentsImpl::GotResponseToKeyboardLockRequest", |
| 5455 | "allowed", allowed); |
| 5456 | |
Joe Downing | 13dd76b | 2018-04-09 18:32:15 | [diff] [blame] | 5457 | if (!keyboard_lock_widget_) |
| 5458 | return false; |
| 5459 | |
Matt Falkenhagen | 888db59 | 2021-09-07 20:20:25 | [diff] [blame] | 5460 | if (WebContentsImpl::FromRenderWidgetHostImpl(keyboard_lock_widget_) != |
| 5461 | this) { |
Joe Downing | 13dd76b | 2018-04-09 18:32:15 | [diff] [blame] | 5462 | NOTREACHED(); |
| 5463 | return false; |
| 5464 | } |
| 5465 | |
| 5466 | // KeyboardLock is only supported when called by the top-level browsing |
| 5467 | // context and is not supported in embedded content scenarios. |
| 5468 | if (GetOuterWebContents()) |
| 5469 | return false; |
| 5470 | |
| 5471 | keyboard_lock_widget_->GotResponseToKeyboardLockRequest(allowed); |
| 5472 | return true; |
| 5473 | } |
| 5474 | |
Lucas Furukawa Gadani | e1c5dfda | 2018-11-29 17:57:41 | [diff] [blame] | 5475 | bool WebContentsImpl::HasOpener() { |
Ivan Kotenkov | 2c0d2bb3 | 2017-11-01 15:41:28 | [diff] [blame] | 5476 | return GetOpener() != nullptr; |
[email protected] | a0358d7 | 2012-03-09 14:06:50 | [diff] [blame] | 5477 | } |
| 5478 | |
Lucas Furukawa Gadani | e1c5dfda | 2018-11-29 17:57:41 | [diff] [blame] | 5479 | RenderFrameHostImpl* WebContentsImpl::GetOpener() { |
Carlos Caballero | b65b6e3a | 2021-11-15 10:09:00 | [diff] [blame] | 5480 | FrameTreeNode* opener_ftn = primary_frame_tree_.root()->opener(); |
lukasza | 6f8ac62 | 2017-06-06 03:10:20 | [diff] [blame] | 5481 | return opener_ftn ? opener_ftn->current_frame_host() : nullptr; |
jochen | 55ff350 | 2014-12-18 20:52:57 | [diff] [blame] | 5482 | } |
| 5483 | |
Rakina Zata Amni | 3a48ae4 | 2022-05-05 03:39:56 | [diff] [blame] | 5484 | bool WebContentsImpl::HasLiveOriginalOpenerChain() { |
| 5485 | return GetFirstWebContentsInLiveOriginalOpenerChain() != nullptr; |
jochen | 6004a36 | 2017-02-04 00:11:40 | [diff] [blame] | 5486 | } |
| 5487 | |
Rakina Zata Amni | 3a48ae4 | 2022-05-05 03:39:56 | [diff] [blame] | 5488 | WebContents* WebContentsImpl::GetFirstWebContentsInLiveOriginalOpenerChain() { |
| 5489 | FrameTreeNode* opener_ftn = |
| 5490 | primary_frame_tree_.root() |
| 5491 | ->first_live_main_frame_in_original_opener_chain(); |
| 5492 | return opener_ftn ? WebContents::FromRenderFrameHost( |
| 5493 | opener_ftn->current_frame_host()) |
| 5494 | : nullptr; |
jochen | 6004a36 | 2017-02-04 00:11:40 | [diff] [blame] | 5495 | } |
| 5496 | |
Tom Burgin | 732656d3 | 2022-02-07 18:33:26 | [diff] [blame] | 5497 | #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_MAC) |
[email protected] | cb80545 | 2013-05-22 15:16:21 | [diff] [blame] | 5498 | void WebContentsImpl::DidChooseColorInColorChooser(SkColor color) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 5499 | OPTIONAL_TRACE_EVENT1("content", |
| 5500 | "WebContentsImpl::DidChooseColorInColorChooser", |
| 5501 | "color", color); |
Bo Liu | 2bceebc | 2021-06-03 15:06:33 | [diff] [blame] | 5502 | if (color_chooser_holder_) |
| 5503 | color_chooser_holder_->DidChooseColorInColorChooser(color); |
[email protected] | da854376 | 2012-03-20 08:52:20 | [diff] [blame] | 5504 | } |
| 5505 | |
[email protected] | cb80545 | 2013-05-22 15:16:21 | [diff] [blame] | 5506 | void WebContentsImpl::DidEndColorChooser() { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 5507 | OPTIONAL_TRACE_EVENT0("content", "WebContentsImpl::DidEndColorChooser"); |
Bo Liu | 2bceebc | 2021-06-03 15:06:33 | [diff] [blame] | 5508 | color_chooser_holder_.reset(); |
[email protected] | da854376 | 2012-03-20 08:52:20 | [diff] [blame] | 5509 | } |
Tom Burgin | 732656d3 | 2022-02-07 18:33:26 | [diff] [blame] | 5510 | #endif // BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_MAC) |
[email protected] | da854376 | 2012-03-20 08:52:20 | [diff] [blame] | 5511 | |
halton.huo | ca2eabd | 2015-07-06 08:17:40 | [diff] [blame] | 5512 | int WebContentsImpl::DownloadImage( |
| 5513 | const GURL& url, |
| 5514 | bool is_favicon, |
Rouslan Solomakhin | db19343e | 2021-09-15 15:34:55 | [diff] [blame] | 5515 | const gfx::Size& preferred_size, |
halton.huo | ca2eabd | 2015-07-06 08:17:40 | [diff] [blame] | 5516 | uint32_t max_bitmap_size, |
| 5517 | bool bypass_cache, |
Avi Drissman | a5a52dd | 2018-03-27 03:39:02 | [diff] [blame] | 5518 | WebContents::ImageDownloadCallback callback) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 5519 | OPTIONAL_TRACE_EVENT1("content", "WebContentsImpl::DownloadImage", "url", |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 5520 | url); |
Alexander Timin | 8690530c | 2021-06-19 00:34:32 | [diff] [blame] | 5521 | return DownloadImageInFrame(GlobalRenderFrameHostId(), url, is_favicon, |
Danyao Wang | a78f3dd2 | 2020-03-05 05:31:27 | [diff] [blame] | 5522 | preferred_size, max_bitmap_size, bypass_cache, |
| 5523 | std::move(callback)); |
| 5524 | } |
| 5525 | |
| 5526 | int WebContentsImpl::DownloadImageInFrame( |
Alexander Timin | 8690530c | 2021-06-19 00:34:32 | [diff] [blame] | 5527 | const GlobalRenderFrameHostId& initiator_frame_routing_id, |
Danyao Wang | a78f3dd2 | 2020-03-05 05:31:27 | [diff] [blame] | 5528 | const GURL& url, |
| 5529 | bool is_favicon, |
Rouslan Solomakhin | db19343e | 2021-09-15 15:34:55 | [diff] [blame] | 5530 | const gfx::Size& preferred_size, |
Danyao Wang | a78f3dd2 | 2020-03-05 05:31:27 | [diff] [blame] | 5531 | uint32_t max_bitmap_size, |
| 5532 | bool bypass_cache, |
| 5533 | WebContents::ImageDownloadCallback callback) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 5534 | OPTIONAL_TRACE_EVENT0("content", "WebContentsImpl::DownloadImageInFrame"); |
amistry | 9f01b77 | 2015-07-29 01:54:55 | [diff] [blame] | 5535 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
halton.huo | ca2eabd | 2015-07-06 08:17:40 | [diff] [blame] | 5536 | static int next_image_download_id = 0; |
danakj | 34c8ac0 | 2021-02-25 19:16:09 | [diff] [blame] | 5537 | const int download_id = ++next_image_download_id; |
Danyao Wang | a78f3dd2 | 2020-03-05 05:31:27 | [diff] [blame] | 5538 | |
| 5539 | RenderFrameHostImpl* initiator_frame = |
| 5540 | initiator_frame_routing_id.child_id |
| 5541 | ? RenderFrameHostImpl::FromID(initiator_frame_routing_id) |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 5542 | : GetPrimaryMainFrame(); |
danakj | 34c8ac0 | 2021-02-25 19:16:09 | [diff] [blame] | 5543 | if (!initiator_frame->IsRenderFrameLive()) { |
amistry | 9f01b77 | 2015-07-29 01:54:55 | [diff] [blame] | 5544 | // If the renderer process is dead (i.e. crash, or memory pressure on |
| 5545 | // Android), the downloader service will be invalid. Pre-Mojo, this would |
Avi Drissman | a5a52dd | 2018-03-27 03:39:02 | [diff] [blame] | 5546 | // hang the callback indefinitely since the IPC would be dropped. Now, |
amistry | 9f01b77 | 2015-07-29 01:54:55 | [diff] [blame] | 5547 | // respond with a 400 HTTP error code to indicate that something went wrong. |
Gabriel Charette | e7cdc5cd | 2020-05-27 23:35:05 | [diff] [blame] | 5548 | GetUIThreadTaskRunner({})->PostTask( |
| 5549 | FROM_HERE, |
Mikel Astiz | 034ba14e | 2021-04-30 07:23:49 | [diff] [blame] | 5550 | base::BindOnce( |
| 5551 | &WebContentsImpl::OnDidDownloadImage, weak_factory_.GetWeakPtr(), |
| 5552 | initiator_frame->GetWeakPtr(), std::move(callback), download_id, |
| 5553 | url, 400, std::vector<SkBitmap>(), std::vector<gfx::Size>())); |
amistry | 9f01b77 | 2015-07-29 01:54:55 | [diff] [blame] | 5554 | return download_id; |
| 5555 | } |
| 5556 | |
danakj | 34c8ac0 | 2021-02-25 19:16:09 | [diff] [blame] | 5557 | initiator_frame->GetMojoImageDownloader()->DownloadImage( |
Fredrik Söderquist | b2b39eb9 | 2019-11-18 16:16:50 | [diff] [blame] | 5558 | url, is_favicon, preferred_size, max_bitmap_size, bypass_cache, |
tzik | e2aca99 | 2017-09-05 08:50:54 | [diff] [blame] | 5559 | base::BindOnce(&WebContentsImpl::OnDidDownloadImage, |
Mikel Astiz | 034ba14e | 2021-04-30 07:23:49 | [diff] [blame] | 5560 | weak_factory_.GetWeakPtr(), initiator_frame->GetWeakPtr(), |
| 5561 | std::move(callback), download_id, url)); |
amistry | 9f01b77 | 2015-07-29 01:54:55 | [diff] [blame] | 5562 | return download_id; |
[email protected] | 795c2897 | 2012-12-06 06:13:39 | [diff] [blame] | 5563 | } |
| 5564 | |
[email protected] | 36ec24f | 2014-01-09 00:32:08 | [diff] [blame] | 5565 | void WebContentsImpl::Find(int request_id, |
Jan Wilken Dörrie | aace0cfef | 2021-03-11 22:01:58 | [diff] [blame] | 5566 | const std::u16string& search_text, |
Joey Arhar | cac45bf | 2022-01-07 21:59:31 | [diff] [blame] | 5567 | blink::mojom::FindOptionsPtr options, |
| 5568 | bool skip_delay) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 5569 | OPTIONAL_TRACE_EVENT0("content", "WebContentsImpl::Find"); |
thestig | 6057a6b2 | 2015-11-12 23:01:33 | [diff] [blame] | 5570 | // Cowardly refuse to search for no text. |
| 5571 | if (search_text.empty()) { |
| 5572 | NOTREACHED(); |
| 5573 | return; |
| 5574 | } |
| 5575 | |
Rakina Zata Amni | 3f77dff | 2018-09-08 16:19:43 | [diff] [blame] | 5576 | GetOrCreateFindRequestManager()->Find(request_id, search_text, |
Joey Arhar | cac45bf | 2022-01-07 21:59:31 | [diff] [blame] | 5577 | std::move(options), skip_delay); |
[email protected] | 36ec24f | 2014-01-09 00:32:08 | [diff] [blame] | 5578 | } |
| 5579 | |
| 5580 | void WebContentsImpl::StopFinding(StopFindAction action) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 5581 | OPTIONAL_TRACE_EVENT0("content", "WebContentsImpl::StopFinding"); |
paulmeyer | feafc2d | 2017-04-25 21:46:40 | [diff] [blame] | 5582 | if (FindRequestManager* manager = GetFindRequestManager()) |
| 5583 | manager->StopFinding(action); |
[email protected] | 36ec24f | 2014-01-09 00:32:08 | [diff] [blame] | 5584 | } |
| 5585 | |
Tommy Steimel | 1836051 | 2017-11-01 00:38:19 | [diff] [blame] | 5586 | bool WebContentsImpl::WasEverAudible() { |
| 5587 | return was_ever_audible_; |
| 5588 | } |
| 5589 | |
bokan | ece34a8 | 2016-01-28 19:49:46 | [diff] [blame] | 5590 | void WebContentsImpl::ExitFullscreen(bool will_cause_resize) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 5591 | OPTIONAL_TRACE_EVENT0("content", "WebContentsImpl::ExitFullscreen"); |
mlamouri | 7a78d6fd | 2015-01-17 13:23:53 | [diff] [blame] | 5592 | // Clean up related state and initiate the fullscreen exit. |
James Hollyer | d5c9de46 | 2020-03-10 19:02:45 | [diff] [blame] | 5593 | GetRenderViewHost()->GetWidget()->RejectMouseLockOrUnlockIfNecessary( |
| 5594 | blink::mojom::PointerLockResult::kUserRejected); |
bokan | ece34a8 | 2016-01-28 19:49:46 | [diff] [blame] | 5595 | ExitFullscreenMode(will_cause_resize); |
mlamouri | 7a78d6fd | 2015-01-17 13:23:53 | [diff] [blame] | 5596 | } |
| 5597 | |
Mike Wasserman | b6bc015 | 2020-08-25 22:46:20 | [diff] [blame] | 5598 | base::ScopedClosureRunner WebContentsImpl::ForSecurityDropFullscreen( |
| 5599 | int64_t display_id) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 5600 | OPTIONAL_TRACE_EVENT1("content", "WebContentsImpl::ForSecurityDropFullscreen", |
| 5601 | "display_id", display_id); |
Avi Drissman | 3397294 | 2020-06-19 14:16:01 | [diff] [blame] | 5602 | // Kick WebContentses that are "related" to this WebContents out of |
| 5603 | // fullscreen. This needs to be done with two passes, because it is simple to |
| 5604 | // walk _up_ the chain of openers and outer contents, but it not simple to |
| 5605 | // walk _down_ the chain. |
| 5606 | |
| 5607 | // First, determine if any WebContents that is in fullscreen has this |
| 5608 | // WebContents as an upstream contents. Drop that WebContents out of |
| 5609 | // fullscreen if it does. This is theoretically quadratic-ish (fullscreen |
| 5610 | // contentses x each one's opener length) but neither of those is expected to |
| 5611 | // ever be a large number. |
Avi Drissman | 3397294 | 2020-06-19 14:16:01 | [diff] [blame] | 5612 | auto fullscreen_set_copy = *FullscreenContentsSet(GetBrowserContext()); |
| 5613 | for (auto* fullscreen_contents : fullscreen_set_copy) { |
Avi Drissman | 97aef04 | 2020-06-30 21:04:48 | [diff] [blame] | 5614 | // Checking IsFullscreen() for tabs in the fullscreen set may seem |
| 5615 | // redundant, but teeeeechnically fullscreen is run by the delegate, and |
| 5616 | // it's possible that the delegate's notion of fullscreen may have changed |
| 5617 | // outside of WebContents's notice. |
Mike Wasserman | b6bc015 | 2020-08-25 22:46:20 | [diff] [blame] | 5618 | if (fullscreen_contents->IsFullscreen() && |
Brad Triebwasser | 9d34d25 | 2022-07-26 20:44:42 | [diff] [blame] | 5619 | (display_id == display::kInvalidDisplayId || |
| 5620 | fullscreen_contents->IsFullscreenOnDisplay(display_id))) { |
Avi Drissman | 3397294 | 2020-06-19 14:16:01 | [diff] [blame] | 5621 | auto opener_contentses = GetAllOpeningWebContents(fullscreen_contents); |
| 5622 | if (opener_contentses.count(this)) |
| 5623 | fullscreen_contents->ExitFullscreen(true); |
| 5624 | } |
| 5625 | } |
| 5626 | |
| 5627 | // Second, walk upstream from this WebContents, and drop the fullscreen of |
| 5628 | // all WebContentses that are in fullscreen. Block all the WebContentses in |
| 5629 | // the chain from entering fullscreen while the returned closure runner is |
| 5630 | // alive. It's OK that this set doesn't contain downstream WebContentses, as |
| 5631 | // any request to enter fullscreen will have the upstream of the WebContents |
| 5632 | // checked. (See CanEnterFullscreenMode().) |
Avi Drissman | ced52b6 | 2019-08-14 21:25:46 | [diff] [blame] | 5633 | |
Avi Drissman | 1a55a9d6 | 2020-03-10 18:56:45 | [diff] [blame] | 5634 | std::vector<base::WeakPtr<WebContentsImpl>> blocked_contentses; |
| 5635 | |
Avi Drissman | 3397294 | 2020-06-19 14:16:01 | [diff] [blame] | 5636 | for (auto* opener : GetAllOpeningWebContents(this)) { |
Avi Drissman | 1a55a9d6 | 2020-03-10 18:56:45 | [diff] [blame] | 5637 | // Drop fullscreen if the WebContents is in it, and... |
Brad Triebwasser | 9d34d25 | 2022-07-26 20:44:42 | [diff] [blame] | 5638 | if (opener->IsFullscreen() && (display_id == display::kInvalidDisplayId || |
| 5639 | opener->IsFullscreenOnDisplay(display_id))) |
Avi Drissman | 3397294 | 2020-06-19 14:16:01 | [diff] [blame] | 5640 | opener->ExitFullscreen(true); |
Avi Drissman | ced52b6 | 2019-08-14 21:25:46 | [diff] [blame] | 5641 | |
Avi Drissman | 1a55a9d6 | 2020-03-10 18:56:45 | [diff] [blame] | 5642 | // ...block the WebContents from entering fullscreen until further notice. |
Avi Drissman | 3397294 | 2020-06-19 14:16:01 | [diff] [blame] | 5643 | ++opener->fullscreen_blocker_count_; |
| 5644 | blocked_contentses.push_back(opener->weak_factory_.GetWeakPtr()); |
Avi Drissman | ced52b6 | 2019-08-14 21:25:46 | [diff] [blame] | 5645 | } |
Avi Drissman | 1a55a9d6 | 2020-03-10 18:56:45 | [diff] [blame] | 5646 | |
| 5647 | return base::ScopedClosureRunner(base::BindOnce( |
| 5648 | [](std::vector<base::WeakPtr<WebContentsImpl>> blocked_contentses) { |
| 5649 | for (base::WeakPtr<WebContentsImpl>& web_contents : |
| 5650 | blocked_contentses) { |
| 5651 | if (web_contents) { |
| 5652 | DCHECK_GT(web_contents->fullscreen_blocker_count_, 0); |
| 5653 | --web_contents->fullscreen_blocker_count_; |
| 5654 | } |
| 5655 | } |
| 5656 | }, |
| 5657 | std::move(blocked_contentses))); |
Avi Drissman | ced52b6 | 2019-08-14 21:25:46 | [diff] [blame] | 5658 | } |
| 5659 | |
mariakhomenko | 44bdc473 | 2015-04-29 01:55:38 | [diff] [blame] | 5660 | void WebContentsImpl::ResumeLoadingCreatedWebContents() { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 5661 | OPTIONAL_TRACE_EVENT0("content", |
| 5662 | "WebContentsImpl::ResumeLoadingCreatedWebContents"); |
Nasko Oskov | 83a8cf9 | 2018-10-19 14:58:56 | [diff] [blame] | 5663 | if (delayed_load_url_params_.get()) { |
| 5664 | DCHECK(!delayed_open_url_params_); |
Carlos Caballero | 40b0efd | 2021-01-26 11:55:00 | [diff] [blame] | 5665 | GetController().LoadURLWithParams(*delayed_load_url_params_.get()); |
Nasko Oskov | 83a8cf9 | 2018-10-19 14:58:56 | [diff] [blame] | 5666 | delayed_load_url_params_.reset(nullptr); |
| 5667 | return; |
| 5668 | } |
| 5669 | |
mariakhomenko | a4971c1 | 2015-07-21 19:04:37 | [diff] [blame] | 5670 | if (delayed_open_url_params_.get()) { |
| 5671 | OpenURL(*delayed_open_url_params_.get()); |
| 5672 | delayed_open_url_params_.reset(nullptr); |
| 5673 | return; |
| 5674 | } |
| 5675 | |
danakj | 08eb51d | 2020-12-30 20:15:23 | [diff] [blame] | 5676 | // Renderer-created main frames wait for the renderer to request for them to |
| 5677 | // perform navigations and to be shown. We signal that this condition is |
| 5678 | // satisfied by calling Init(). |
dfalcantara | e6b7b4675 | 2015-07-10 18:14:16 | [diff] [blame] | 5679 | if (is_resume_pending_) { |
| 5680 | is_resume_pending_ = false; |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 5681 | GetPrimaryMainFrame()->Init(); |
dfalcantara | e6b7b4675 | 2015-07-10 18:14:16 | [diff] [blame] | 5682 | } |
mariakhomenko | 44bdc473 | 2015-04-29 01:55:38 | [diff] [blame] | 5683 | } |
| 5684 | |
[email protected] | b172aee | 2012-04-10 17:05:26 | [diff] [blame] | 5685 | bool WebContentsImpl::FocusLocationBarByDefault() { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 5686 | OPTIONAL_TRACE_EVENT0("content", |
| 5687 | "WebContentsImpl::FocusLocationBarByDefault"); |
Xiyuan Xia | 2a66b11 | 2018-12-06 15:52:16 | [diff] [blame] | 5688 | if (should_focus_location_bar_by_default_) |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 5689 | return true; |
Xiyuan Xia | 2a66b11 | 2018-12-06 15:52:16 | [diff] [blame] | 5690 | |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 5691 | return delegate_ && delegate_->ShouldFocusLocationBarByDefault(this); |
[email protected] | 0bfbf88 | 2011-12-22 18:19:27 | [diff] [blame] | 5692 | } |
| 5693 | |
clamy | 0e11988 | 2015-07-31 16:12:33 | [diff] [blame] | 5694 | void WebContentsImpl::DidStartNavigation(NavigationHandle* navigation_handle) { |
Charles Harrison | 53096ba | 2017-12-15 15:39:48 | [diff] [blame] | 5695 | TRACE_EVENT1("navigation", "WebContentsImpl::DidStartNavigation", |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 5696 | "navigation_handle", navigation_handle); |
Clark DuVall | 22aaff3 | 2021-03-18 17:17:47 | [diff] [blame] | 5697 | { |
| 5698 | SCOPED_UMA_HISTOGRAM_TIMER("WebContentsObserver.DidStartNavigation"); |
| 5699 | observers_.NotifyObservers(&WebContentsObserver::DidStartNavigation, |
| 5700 | navigation_handle); |
| 5701 | } |
Khushal | 7548ad10 | 2021-06-25 23:23:37 | [diff] [blame] | 5702 | if (navigation_handle->IsInPrimaryMainFrame()) { |
Xiyuan Xia | 2a66b11 | 2018-12-06 15:52:16 | [diff] [blame] | 5703 | // When the browser is started with about:blank as the startup URL, focus |
| 5704 | // the location bar (which will also select its contents) so people can |
| 5705 | // simply begin typing to navigate elsewhere. |
| 5706 | // |
| 5707 | // We need to be careful not to trigger this for anything other than the |
| 5708 | // startup navigation. In particular, if we allow an attacker to open a |
| 5709 | // popup to about:blank, then navigate, focusing the Omnibox will cause the |
| 5710 | // end of the new URL to be scrolled into view instead of the start, |
| 5711 | // allowing the attacker to spoof other URLs. The conditions checked here |
| 5712 | // are all aimed at ensuring no such attacker-controlled navigation can |
| 5713 | // trigger this. |
| 5714 | should_focus_location_bar_by_default_ = |
Carlos Caballero | 40b0efd | 2021-01-26 11:55:00 | [diff] [blame] | 5715 | GetController().IsInitialNavigation() && |
Xiyuan Xia | 2a66b11 | 2018-12-06 15:52:16 | [diff] [blame] | 5716 | !navigation_handle->IsRendererInitiated() && |
| 5717 | navigation_handle->GetURL() == url::kAboutBlankURL; |
| 5718 | } |
clamy | 0e11988 | 2015-07-31 16:12:33 | [diff] [blame] | 5719 | } |
| 5720 | |
| 5721 | void WebContentsImpl::DidRedirectNavigation( |
| 5722 | NavigationHandle* navigation_handle) { |
Charles Harrison | 53096ba | 2017-12-15 15:39:48 | [diff] [blame] | 5723 | TRACE_EVENT1("navigation", "WebContentsImpl::DidRedirectNavigation", |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 5724 | "navigation_handle", navigation_handle); |
Clark DuVall | 22aaff3 | 2021-03-18 17:17:47 | [diff] [blame] | 5725 | { |
| 5726 | SCOPED_UMA_HISTOGRAM_TIMER("WebContentsObserver.DidRedirectNavigation"); |
| 5727 | observers_.NotifyObservers(&WebContentsObserver::DidRedirectNavigation, |
| 5728 | navigation_handle); |
| 5729 | } |
jam | 5f358e5 | 2017-02-13 16:37:03 | [diff] [blame] | 5730 | // Notify accessibility if this is a reload. This has to called on the |
| 5731 | // BrowserAccessibilityManager associated with the old RFHI. |
| 5732 | if (navigation_handle->GetReloadType() != ReloadType::NONE) { |
Mohamed Abdelhalim | 9579496 | 2019-09-20 11:16:49 | [diff] [blame] | 5733 | NavigationRequest* request = NavigationRequest::From(navigation_handle); |
jam | 5f358e5 | 2017-02-13 16:37:03 | [diff] [blame] | 5734 | BrowserAccessibilityManager* manager = |
Mohamed Abdelhalim | 9579496 | 2019-09-20 11:16:49 | [diff] [blame] | 5735 | request->frame_tree_node() |
jam | 5f358e5 | 2017-02-13 16:37:03 | [diff] [blame] | 5736 | ->current_frame_host() |
| 5737 | ->browser_accessibility_manager(); |
| 5738 | if (manager) |
| 5739 | manager->UserIsReloading(); |
| 5740 | } |
clamy | 0e11988 | 2015-07-31 16:12:33 | [diff] [blame] | 5741 | } |
| 5742 | |
clamy | efca29e | 2015-09-17 00:22:11 | [diff] [blame] | 5743 | void WebContentsImpl::ReadyToCommitNavigation( |
| 5744 | NavigationHandle* navigation_handle) { |
Charles Harrison | 53096ba | 2017-12-15 15:39:48 | [diff] [blame] | 5745 | TRACE_EVENT1("navigation", "WebContentsImpl::ReadyToCommitNavigation", |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 5746 | "navigation_handle", navigation_handle); |
Elad Alon | f156eb6 | 2021-05-17 22:02:37 | [diff] [blame] | 5747 | |
| 5748 | // Cross-document navigation of the top-level frame resets the capture |
Khushal | 918195b | 2021-08-20 19:28:41 | [diff] [blame] | 5749 | // handle config. Using IsInPrimaryMainFrame is valid here since the browser |
| 5750 | // caches this state for the active main frame only. |
Elad Alon | f156eb6 | 2021-05-17 22:02:37 | [diff] [blame] | 5751 | if (!navigation_handle->IsSameDocument() && |
Kevin McNee | 19b0d0f3 | 2021-06-30 17:35:52 | [diff] [blame] | 5752 | navigation_handle->IsInPrimaryMainFrame()) { |
Elad Alon | f156eb6 | 2021-05-17 22:02:37 | [diff] [blame] | 5753 | SetCaptureHandleConfig(blink::mojom::CaptureHandleConfig::New()); |
| 5754 | } |
| 5755 | |
Andrew Grieve | e19cd93e | 2021-01-22 05:43:06 | [diff] [blame] | 5756 | observers_.NotifyObservers(&WebContentsObserver::ReadyToCommitNavigation, |
| 5757 | navigation_handle); |
Elad Alon | f156eb6 | 2021-05-17 22:02:37 | [diff] [blame] | 5758 | |
Kenneth Russell | 954ed9ce | 2018-04-12 23:07:01 | [diff] [blame] | 5759 | // If any domains are blocked from accessing 3D APIs because they may |
| 5760 | // have caused the GPU to reset recently, unblock them here if the user |
| 5761 | // initiated this navigation. This implies that the user was involved in |
| 5762 | // the decision to navigate, so there's no concern about |
| 5763 | // denial-of-service issues. Want to do this as early as |
| 5764 | // possible to avoid race conditions with pages attempting to access |
| 5765 | // WebGL early on. |
| 5766 | // |
| 5767 | // TODO(crbug.com/617904): currently navigations initiated by the browser |
| 5768 | // (reload button, reload menu option, pressing return in the Omnibox) |
| 5769 | // return false from HasUserGesture(). If or when that is addressed, |
| 5770 | // remove the check for IsRendererInitiated() below. |
| 5771 | // |
| 5772 | // TODO(crbug.com/832180): HasUserGesture comes from the renderer |
| 5773 | // process and isn't validated. Until it is, don't trust it. |
| 5774 | if (!navigation_handle->IsRendererInitiated()) { |
| 5775 | GpuDataManagerImpl::GetInstance()->UnblockDomainFrom3DAPIs( |
| 5776 | navigation_handle->GetURL()); |
| 5777 | } |
| 5778 | |
John Abd-El-Malek | fde08e6da | 2017-12-05 04:06:24 | [diff] [blame] | 5779 | if (navigation_handle->IsSameDocument()) |
| 5780 | return; |
| 5781 | |
Livvie Lin | 139bf44 | 2018-08-10 00:18:54 | [diff] [blame] | 5782 | // SSLInfo is not needed on subframe navigations since the main-frame |
| 5783 | // certificate is the only one that can be inspected (using the info |
| 5784 | // bubble) without refreshing the page with DevTools open. |
Carlos IL | aed1e7b | 2019-03-12 19:27:21 | [diff] [blame] | 5785 | // We don't call DidStartResourceResponse on net errors, since that results on |
| 5786 | // existing cert exceptions being revoked, which leads to weird behavior with |
| 5787 | // committed interstitials or while offline. We only need the error check for |
Julie Jeongeun Kim | f6732dc41 | 2021-11-16 07:33:06 | [diff] [blame] | 5788 | // the main frame case. |
Carlos IL | aed1e7b | 2019-03-12 19:27:21 | [diff] [blame] | 5789 | if (navigation_handle->IsInMainFrame() && |
| 5790 | navigation_handle->GetNetErrorCode() == net::OK) { |
Carlos Caballero | ede6f8c | 2021-01-28 11:01:50 | [diff] [blame] | 5791 | static_cast<NavigationRequest*>(navigation_handle) |
| 5792 | ->frame_tree_node() |
| 5793 | ->frame_tree() |
Arthur Sonzogni | f6785ec | 2022-12-05 10:11:50 | [diff] [blame] | 5794 | .controller() |
Carlos Caballero | ede6f8c | 2021-01-28 11:01:50 | [diff] [blame] | 5795 | .ssl_manager() |
| 5796 | ->DidStartResourceResponse( |
Yoshisato Yanagisawa | 66845bf | 2022-05-12 04:04:02 | [diff] [blame] | 5797 | url::SchemeHostPort(navigation_handle->GetURL()), |
Carlos Caballero | ede6f8c | 2021-01-28 11:01:50 | [diff] [blame] | 5798 | navigation_handle->GetSSLInfo().has_value() |
| 5799 | ? net::IsCertStatusError( |
| 5800 | navigation_handle->GetSSLInfo()->cert_status) |
| 5801 | : false); |
Camille Lamy | 62b82601 | 2019-02-26 09:15:47 | [diff] [blame] | 5802 | } |
clamy | efca29e | 2015-09-17 00:22:11 | [diff] [blame] | 5803 | } |
| 5804 | |
clamy | 0e11988 | 2015-07-31 16:12:33 | [diff] [blame] | 5805 | void WebContentsImpl::DidFinishNavigation(NavigationHandle* navigation_handle) { |
Charles Harrison | 53096ba | 2017-12-15 15:39:48 | [diff] [blame] | 5806 | TRACE_EVENT1("navigation", "WebContentsImpl::DidFinishNavigation", |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 5807 | "navigation_handle", navigation_handle); |
Steven Holte | 3804134 | 2018-06-22 22:00:57 | [diff] [blame] | 5808 | |
Clark DuVall | 22aaff3 | 2021-03-18 17:17:47 | [diff] [blame] | 5809 | { |
| 5810 | SCOPED_UMA_HISTOGRAM_TIMER("WebContentsObserver.DidFinishNavigation"); |
| 5811 | observers_.NotifyObservers(&WebContentsObserver::DidFinishNavigation, |
| 5812 | navigation_handle); |
| 5813 | } |
Becca Hughes | d11d650 | 2018-07-31 17:01:28 | [diff] [blame] | 5814 | if (display_cutout_host_impl_) |
| 5815 | display_cutout_host_impl_->DidFinishNavigation(navigation_handle); |
| 5816 | |
jam | 5f358e5 | 2017-02-13 16:37:03 | [diff] [blame] | 5817 | if (navigation_handle->HasCommitted()) { |
Ian Prest | cdd843d | 2020-01-28 18:59:58 | [diff] [blame] | 5818 | // TODO(domfarolino, dmazzoni): Do this using WebContentsObserver. See |
| 5819 | // https://crbug.com/981271. |
| 5820 | BrowserAccessibilityManager* manager = |
| 5821 | static_cast<RenderFrameHostImpl*>( |
| 5822 | navigation_handle->GetRenderFrameHost()) |
| 5823 | ->browser_accessibility_manager(); |
| 5824 | if (manager) { |
| 5825 | if (navigation_handle->IsErrorPage()) { |
| 5826 | manager->NavigationFailed(); |
| 5827 | } else { |
| 5828 | manager->NavigationSucceeded(); |
| 5829 | } |
| 5830 | } |
| 5831 | |
Khushal | afff1d2 | 2021-06-25 16:00:34 | [diff] [blame] | 5832 | // TODO(crbug.com/1223113) : Move this tracking to PageImpl. |
| 5833 | if (navigation_handle->IsInPrimaryMainFrame() && |
Khushal | 6fca5376 | 2021-06-25 04:42:18 | [diff] [blame] | 5834 | !navigation_handle->IsSameDocument()) { |
| 5835 | was_ever_audible_ = false; |
Tommy Steimel | 1836051 | 2017-11-01 00:38:19 | [diff] [blame] | 5836 | } |
Lan Wei | f81c6e7c | 2020-02-12 16:46:09 | [diff] [blame] | 5837 | |
Robert Lin | a662a47 | 2022-09-01 01:00:25 | [diff] [blame] | 5838 | // Clear the stored prerender activation result if this is not a prerender |
| 5839 | // activation. If this is another prerender activation, it will override |
| 5840 | // the old result in DevTools. |
| 5841 | if (!navigation_handle->IsPrerenderedPageActivation() && |
| 5842 | !navigation_handle->IsSameDocument() && |
| 5843 | navigation_handle->IsInPrimaryMainFrame()) { |
| 5844 | last_navigation_was_prerender_activation_for_devtools_ = false; |
| 5845 | } |
| 5846 | |
Lan Wei | f81c6e7c | 2020-02-12 16:46:09 | [diff] [blame] | 5847 | if (!navigation_handle->IsSameDocument()) |
| 5848 | last_screen_orientation_change_time_ = base::TimeTicks(); |
dmazzoni | bf8cec4 | 2015-02-08 08:28:08 | [diff] [blame] | 5849 | } |
Xiyuan Xia | 2a66b11 | 2018-12-06 15:52:16 | [diff] [blame] | 5850 | |
| 5851 | // If we didn't end up on about:blank after setting this in DidStartNavigation |
| 5852 | // then don't focus the location bar. |
| 5853 | if (should_focus_location_bar_by_default_ && |
| 5854 | navigation_handle->GetURL() != url::kAboutBlankURL) { |
| 5855 | should_focus_location_bar_by_default_ = false; |
| 5856 | } |
yilkal | 796e89e | 2019-10-02 23:58:28 | [diff] [blame] | 5857 | |
Khushal | 97d31e8 | 2021-06-23 21:07:54 | [diff] [blame] | 5858 | if (navigation_handle->IsInPrimaryMainFrame() && |
| 5859 | first_primary_navigation_completed_) { |
yilkal | 172da77 | 2019-11-06 22:49:07 | [diff] [blame] | 5860 | RecordMaxFrameCountUMA(max_loaded_frame_count_); |
Khushal | 97d31e8 | 2021-06-23 21:07:54 | [diff] [blame] | 5861 | } |
yilkal | 796e89e | 2019-10-02 23:58:28 | [diff] [blame] | 5862 | |
yilkal | 172da77 | 2019-11-06 22:49:07 | [diff] [blame] | 5863 | // If navigation has successfully finished in the main frame, set |
Khushal | 97d31e8 | 2021-06-23 21:07:54 | [diff] [blame] | 5864 | // |first_primary_navigation_completed_| to true so that we will record |
yilkal | 172da77 | 2019-11-06 22:49:07 | [diff] [blame] | 5865 | // |max_loaded_frame_count_| above when future main frame navigations finish. |
Khushal | 97d31e8 | 2021-06-23 21:07:54 | [diff] [blame] | 5866 | if (navigation_handle->IsInPrimaryMainFrame() && |
| 5867 | !navigation_handle->IsErrorPage()) { |
| 5868 | first_primary_navigation_completed_ = true; |
yilkal | 172da77 | 2019-11-06 22:49:07 | [diff] [blame] | 5869 | |
| 5870 | // Navigation has completed in main frame. Reset |max_loaded_frame_count_|. |
| 5871 | // |max_loaded_frame_count_| is not necessarily 1 if the navigation was |
| 5872 | // served from BackForwardCache. |
Dave Tapuska | 21e9da0 | 2021-12-15 20:58:20 | [diff] [blame] | 5873 | max_loaded_frame_count_ = GetFrameTreeSize(&primary_frame_tree_); |
yilkal | 796e89e | 2019-10-02 23:58:28 | [diff] [blame] | 5874 | } |
Rakina Zata Amni | 347b7090 | 2020-07-22 10:49:04 | [diff] [blame] | 5875 | |
| 5876 | if (web_preferences_) { |
| 5877 | // Update the WebPreferences for this WebContents that depends on changes |
| 5878 | // that might occur during navigation. This will only update the preferences |
| 5879 | // that needs to be updated (and won't cause an update/overwrite preferences |
| 5880 | // that needs to stay the same after navigations). |
| 5881 | bool value_changed_due_to_override = |
| 5882 | GetContentClient()->browser()->OverrideWebPreferencesAfterNavigation( |
| 5883 | this, web_preferences_.get()); |
| 5884 | // We need to update the WebPreferences value on the renderer if the value |
| 5885 | // is changed due to the override above, or if the navigation is served from |
| 5886 | // the back-forward cache, because the WebPreferences value stored in the |
| 5887 | // renderer might be stale (because we don't send WebPreferences updates to |
Takashi Toyoshima | 9c01ce44 | 2021-06-11 04:55:57 | [diff] [blame] | 5888 | // bfcached renderers). Same for prerendering. |
Rakina Zata Amni | 347b7090 | 2020-07-22 10:49:04 | [diff] [blame] | 5889 | // TODO(rakina): Maybe handle the back-forward cache case in |
| 5890 | // ReadyToCommitNavigation instead? |
Rakina Zata Amni | 2c46e88b | 2021-04-02 05:22:33 | [diff] [blame] | 5891 | // TODO(https://crbug.com/1194880): Maybe sync RendererPreferences as well? |
Rakina Zata Amni | 347b7090 | 2020-07-22 10:49:04 | [diff] [blame] | 5892 | if (value_changed_due_to_override || |
Takashi Toyoshima | 1c1f02cd | 2021-06-18 21:39:16 | [diff] [blame] | 5893 | NavigationRequest::From(navigation_handle)->IsPageActivation()) { |
Rakina Zata Amni | 347b7090 | 2020-07-22 10:49:04 | [diff] [blame] | 5894 | SetWebPreferences(*web_preferences_.get()); |
| 5895 | } |
| 5896 | } |
Ian Vollick | 6d75ac3 | 2021-05-05 17:45:09 | [diff] [blame] | 5897 | |
| 5898 | if (navigation_handle->HasCommitted() && |
| 5899 | navigation_handle->IsPrerenderedPageActivation()) { |
Julie Jeongeun Kim | d74c1e7f | 2021-06-18 04:06:18 | [diff] [blame] | 5900 | // We defer favicon and manifest URL updates while prerendering. Upon |
| 5901 | // activation, we must inform interested parties about our candidate favicon |
| 5902 | // URLs and the manifest URL. |
Julie Jeongeun Kim | 0a594bb | 2021-10-21 05:30:53 | [diff] [blame] | 5903 | DCHECK(navigation_handle->IsInPrimaryMainFrame()); |
Ian Vollick | 6d75ac3 | 2021-05-05 17:45:09 | [diff] [blame] | 5904 | auto* rfhi = static_cast<RenderFrameHostImpl*>( |
| 5905 | navigation_handle->GetRenderFrameHost()); |
Julie Jeongeun Kim | 0a594bb | 2021-10-21 05:30:53 | [diff] [blame] | 5906 | UpdateFaviconURL(rfhi, rfhi->FaviconURLs()); |
| 5907 | OnManifestUrlChanged(rfhi->GetPage()); |
Adithya Srinivasan | 9b0c99c | 2021-08-10 15:19:45 | [diff] [blame] | 5908 | |
| 5909 | // The page might have set its title while prerendering, and if it was, we |
| 5910 | // skipped notifying observers then, and we need to notify them now after |
| 5911 | // the page is activated. |
| 5912 | DCHECK(navigation_handle->IsInPrimaryMainFrame()); |
| 5913 | NavigationEntryImpl* entry = GetController().GetLastCommittedEntry(); |
| 5914 | DCHECK(entry); |
| 5915 | if (!entry->GetTitle().empty()) { |
| 5916 | NotifyTitleUpdateForEntry(entry); |
| 5917 | } |
Ian Vollick | 6d75ac3 | 2021-05-05 17:45:09 | [diff] [blame] | 5918 | } |
[email protected] | 400992b | 2012-06-14 00:03:54 | [diff] [blame] | 5919 | } |
| 5920 | |
[email protected] | b80624c | 2014-02-09 02:46:55 | [diff] [blame] | 5921 | void WebContentsImpl::DidFailLoadWithError( |
| 5922 | RenderFrameHostImpl* render_frame_host, |
[email protected] | b80624c | 2014-02-09 02:46:55 | [diff] [blame] | 5923 | const GURL& url, |
Dave Tapuska | 924ef3c | 2020-01-22 18:20:59 | [diff] [blame] | 5924 | int error_code) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 5925 | TRACE_EVENT2("content,navigation", "WebContentsImpl::DidFailLoadWithError", |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 5926 | "render_frame_host", render_frame_host, "url", url); |
Andrew Grieve | e19cd93e | 2021-01-22 05:43:06 | [diff] [blame] | 5927 | observers_.NotifyObservers(&WebContentsObserver::DidFailLoad, |
| 5928 | render_frame_host, url, error_code); |
[email protected] | b80624c | 2014-02-09 02:46:55 | [diff] [blame] | 5929 | } |
| 5930 | |
[email protected] | 5291380 | 2013-12-10 05:52:18 | [diff] [blame] | 5931 | void WebContentsImpl::NotifyChangedNavigationState( |
| 5932 | InvalidateTypes changed_flags) { |
| 5933 | NotifyNavigationStateChanged(changed_flags); |
| 5934 | } |
| 5935 | |
Alexander Timin | a0ef6df | 2021-06-24 13:34:46 | [diff] [blame] | 5936 | bool WebContentsImpl::ShouldAllowRendererInitiatedCrossProcessNavigation( |
Ian Vollick | 1c6dd3e | 2022-04-13 02:06:26 | [diff] [blame] | 5937 | bool is_outermost_main_frame_navigation) { |
Alexander Timin | a0ef6df | 2021-06-24 13:34:46 | [diff] [blame] | 5938 | OPTIONAL_TRACE_EVENT1( |
| 5939 | "content", |
| 5940 | "WebContentsImpl::ShouldAllowRendererInitiatedCrossProcessNavigation", |
Ian Vollick | 1c6dd3e | 2022-04-13 02:06:26 | [diff] [blame] | 5941 | "is_outermost_main_frame_navigation", is_outermost_main_frame_navigation); |
creis | 2946027 | 2015-12-16 04:38:22 | [diff] [blame] | 5942 | if (!delegate_) |
| 5943 | return true; |
Alexander Timin | a0ef6df | 2021-06-24 13:34:46 | [diff] [blame] | 5944 | return delegate_->ShouldAllowRendererInitiatedCrossProcessNavigation( |
Ian Vollick | 1c6dd3e | 2022-04-13 02:06:26 | [diff] [blame] | 5945 | is_outermost_main_frame_navigation); |
creis | 2946027 | 2015-12-16 04:38:22 | [diff] [blame] | 5946 | } |
| 5947 | |
[email protected] | aa62afd | 2014-04-22 19:22:46 | [diff] [blame] | 5948 | bool WebContentsImpl::ShouldPreserveAbortedURLs() { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 5949 | OPTIONAL_TRACE_EVENT0("content", |
| 5950 | "WebContentsImpl::ShouldPreserveAbortedURLs"); |
[email protected] | aa62afd | 2014-04-22 19:22:46 | [diff] [blame] | 5951 | if (!delegate_) |
| 5952 | return false; |
| 5953 | return delegate_->ShouldPreserveAbortedURLs(this); |
| 5954 | } |
| 5955 | |
Abhijeet Kandalkar | 3dc6e60 | 2022-11-09 05:08:37 | [diff] [blame] | 5956 | void WebContentsImpl::NotifyNavigationStateChangedFromController( |
| 5957 | InvalidateTypes changed_flags) { |
| 5958 | NotifyNavigationStateChanged(changed_flags); |
| 5959 | } |
| 5960 | |
[email protected] | 0d0f4c49 | 2014-04-02 06:42:57 | [diff] [blame] | 5961 | void WebContentsImpl::DidNavigateMainFramePreCommit( |
Liviu Tinta | 23558196 | 2021-07-14 20:37:07 | [diff] [blame] | 5962 | FrameTreeNode* frame_tree_node, |
[email protected] | 5cfbddc | 2014-06-23 23:52:23 | [diff] [blame] | 5963 | bool navigation_is_within_page) { |
Kevin McNee | 9f3ef7c | 2021-09-29 18:43:41 | [diff] [blame] | 5964 | // The `frame_tree_node` is always a main frame. |
Liviu Tinta | 23558196 | 2021-07-14 20:37:07 | [diff] [blame] | 5965 | DCHECK(frame_tree_node->IsMainFrame()); |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 5966 | TRACE_EVENT1("content,navigation", |
| 5967 | "WebContentsImpl::DidNavigateMainFramePreCommit", |
| 5968 | "navigation_is_within_page", navigation_is_within_page); |
Abhijeet Kandalkar | c2e075e | 2022-12-16 12:55:52 | [diff] [blame] | 5969 | const bool is_primary_mainframe = |
| 5970 | frame_tree_node->GetFrameType() == FrameType::kPrimaryMainFrame; |
Liviu Tinta | 23558196 | 2021-07-14 20:37:07 | [diff] [blame] | 5971 | // If running for a non-primary main frame, early out. |
Abhijeet Kandalkar | c2e075e | 2022-12-16 12:55:52 | [diff] [blame] | 5972 | if (!is_primary_mainframe) |
Liviu Tinta | 23558196 | 2021-07-14 20:37:07 | [diff] [blame] | 5973 | return; |
| 5974 | |
[email protected] | 0d0f4c49 | 2014-04-02 06:42:57 | [diff] [blame] | 5975 | // Ensure fullscreen mode is exited before committing the navigation to a |
| 5976 | // different page. The next page will not start out assuming it is in |
| 5977 | // fullscreen mode. |
[email protected] | 5cfbddc | 2014-06-23 23:52:23 | [diff] [blame] | 5978 | if (navigation_is_within_page) { |
[email protected] | dfc39cb | 2014-04-09 22:58:19 | [diff] [blame] | 5979 | // No page change? Then, the renderer and browser can remain in fullscreen. |
[email protected] | 0d0f4c49 | 2014-04-02 06:42:57 | [diff] [blame] | 5980 | return; |
| 5981 | } |
Liviu Tinta | 23558196 | 2021-07-14 20:37:07 | [diff] [blame] | 5982 | |
Avi Drissman | 97aef04 | 2020-06-30 21:04:48 | [diff] [blame] | 5983 | if (IsFullscreen()) |
bokan | ece34a8 | 2016-01-28 19:49:46 | [diff] [blame] | 5984 | ExitFullscreen(false); |
Avi Drissman | 97aef04 | 2020-06-30 21:04:48 | [diff] [blame] | 5985 | DCHECK(!IsFullscreen()); |
Joe Downing | c1a57efe | 2018-05-09 18:23:54 | [diff] [blame] | 5986 | |
| 5987 | // Clean up keyboard lock state when navigating. |
| 5988 | CancelKeyboardLock(keyboard_lock_widget_); |
[email protected] | 0d0f4c49 | 2014-04-02 06:42:57 | [diff] [blame] | 5989 | } |
| 5990 | |
[email protected] | 37567b43 | 2014-02-12 01:12:22 | [diff] [blame] | 5991 | void WebContentsImpl::DidNavigateMainFramePostCommit( |
sky | 8b00392d | 2015-01-10 00:30:28 | [diff] [blame] | 5992 | RenderFrameHostImpl* render_frame_host, |
Rakina Zata Amni | ff10975d | 2021-10-08 06:04:24 | [diff] [blame] | 5993 | const LoadCommittedDetails& details) { |
Liviu Tinta | 4090405 | 2021-07-20 15:12:52 | [diff] [blame] | 5994 | // The render_frame_host is always a main frame. |
| 5995 | DCHECK(render_frame_host->is_main_frame()); |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 5996 | OPTIONAL_TRACE_EVENT1("content,navigation", |
| 5997 | "WebContentsImpl::DidNavigateMainFramePostCommit", |
| 5998 | "render_frame_host", render_frame_host); |
Kevin McNee | 9f3ef7c | 2021-09-29 18:43:41 | [diff] [blame] | 5999 | const bool is_primary_main_frame = render_frame_host->IsInPrimaryMainFrame(); |
Liviu Tinta | 4090405 | 2021-07-20 15:12:52 | [diff] [blame] | 6000 | |
[email protected] | 37567b43 | 2014-02-12 01:12:22 | [diff] [blame] | 6001 | if (details.is_navigation_to_different_page()) { |
Liviu Tinta | 4090405 | 2021-07-20 15:12:52 | [diff] [blame] | 6002 | if (is_primary_main_frame) { |
| 6003 | // Clear the status bubble. This is a workaround for a bug where WebKit |
| 6004 | // doesn't let us know that the cursor left an element during a |
| 6005 | // transition (this is also why the mouse cursor remains as a hand after |
| 6006 | // clicking on a link); see bugs 1184641 and 980803. We don't want to |
| 6007 | // clear the bubble when a user navigates to a named anchor in the same |
| 6008 | // page. |
| 6009 | ClearTargetURL(); |
| 6010 | } |
lanwei | 9d343ad2 | 2015-02-11 01:46:00 | [diff] [blame] | 6011 | |
Carlos Caballero | 179e199 | 2021-04-08 14:31:08 | [diff] [blame] | 6012 | RenderWidgetHostViewBase* rwhvb = static_cast<RenderWidgetHostViewBase*>( |
| 6013 | render_frame_host->GetMainFrame()->GetView()); |
lanwei | 9d343ad2 | 2015-02-11 01:46:00 | [diff] [blame] | 6014 | if (rwhvb) |
| 6015 | rwhvb->OnDidNavigateMainFrameToNewPage(); |
[email protected] | 37567b43 | 2014-02-12 01:12:22 | [diff] [blame] | 6016 | } |
| 6017 | |
Jeremy Roman | 2d8dfe13 | 2021-07-06 20:51:26 | [diff] [blame] | 6018 | PageImpl& page = render_frame_host->GetPage(); |
| 6019 | if (page.IsPrimary()) { |
| 6020 | // The following events will not fire again if the this is a back-forward |
| 6021 | // cache restore or prerendering activation. Fire them ourselves if needed. |
| 6022 | if (details.is_navigation_to_different_page() && |
| 6023 | page.did_first_visually_non_empty_paint()) { |
| 6024 | OnFirstVisuallyNonEmptyPaint(page); |
| 6025 | } |
| 6026 | OnThemeColorChanged(page); |
| 6027 | OnBackgroundColorChanged(page); |
Michael Bai | 19f17a30 | 2021-12-08 04:08:33 | [diff] [blame] | 6028 | DidInferColorScheme(page); |
Tal Pressman | cbca7e2 | 2021-08-06 01:28:49 | [diff] [blame] | 6029 | AXTreeIDForMainFrameHasChanged(); |
Carlos Caballero | 91fffb2 | 2019-10-29 15:24:30 | [diff] [blame] | 6030 | } |
[email protected] | 37567b43 | 2014-02-12 01:12:22 | [diff] [blame] | 6031 | } |
| 6032 | |
| 6033 | void WebContentsImpl::DidNavigateAnyFramePostCommit( |
| 6034 | RenderFrameHostImpl* render_frame_host, |
Rakina Zata Amni | ff10975d | 2021-10-08 06:04:24 | [diff] [blame] | 6035 | const LoadCommittedDetails& details) { |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 6036 | OPTIONAL_TRACE_EVENT1("content,navigation", |
| 6037 | "WebContentsImpl::DidNavigateAnyFramePostCommit", |
| 6038 | "render_frame_host", render_frame_host); |
[email protected] | b4c8401 | 2014-04-28 19:51:10 | [diff] [blame] | 6039 | |
Yoshisato Yanagisawa | 8414148 | 2021-11-24 23:46:42 | [diff] [blame] | 6040 | // This function can be called by prerendered frames or other inactive frames, |
| 6041 | // we want to guard the dialog cancellations below. |
| 6042 | const bool is_active = render_frame_host->IsActive(); |
| 6043 | |
Yoshisato Yanagisawa | 4289e8d | 2022-03-01 05:55:38 | [diff] [blame] | 6044 | // If we navigate off the active non-fenced frame page, close all JavaScript |
| 6045 | // dialogs. We do not cancel the dialogs for fenced frames because it can be |
| 6046 | // used as a communication channel. Please see also: |
| 6047 | // RenderFrameHostManager::UnloadOldFrame in |
| 6048 | // content/browser/renderer_host/render_frame_host_manager.cc |
| 6049 | // |
| 6050 | // TODO(crbug.com/1299379): Note that fenced frames cannot open modal dialogs |
| 6051 | // so this only affects dialogs outside the fenced frame tree. If this is ever |
| 6052 | // changed then the navigation should be deferred till the dialog from within |
| 6053 | // the fenced frame is open. |
| 6054 | if (is_active && !render_frame_host->IsNestedWithinFencedFrame() && |
| 6055 | !details.is_same_document) { |
creis | 89a0f78 | 2015-05-27 16:13:17 | [diff] [blame] | 6056 | CancelActiveAndPendingDialogs(); |
Yoshisato Yanagisawa | 4289e8d | 2022-03-01 05:55:38 | [diff] [blame] | 6057 | } |
[email protected] | 37567b43 | 2014-02-12 01:12:22 | [diff] [blame] | 6058 | |
avi | d53461d | 2016-02-25 17:15:04 | [diff] [blame] | 6059 | // If this is a user-initiated navigation, start allowing JavaScript dialogs |
| 6060 | // again. |
Yao Xiao | d553b70 | 2022-12-08 02:20:47 | [diff] [blame] | 6061 | // |
| 6062 | // TODO(https://crbug.com/1399296): Consider using the actual value of |
| 6063 | // "whether a navigation started with user activation or not" instead of |
| 6064 | // has_user_gesture, which might get filtered out when navigating from |
| 6065 | // proxies. If so, we can remove tracking of |
| 6066 | // `last_committed_common_params_has_user_gesture` entirely. |
| 6067 | if (render_frame_host->last_committed_common_params_has_user_gesture() && |
Rakina Zata Amni | ff10975d | 2021-10-08 06:04:24 | [diff] [blame] | 6068 | dialog_manager_) { |
Yoshisato Yanagisawa | 8414148 | 2021-11-24 23:46:42 | [diff] [blame] | 6069 | DCHECK(is_active); |
avi | 6879fcf | 2017-01-21 05:27:53 | [diff] [blame] | 6070 | dialog_manager_->CancelDialogs(this, /*reset_state=*/true); |
avi | 5d3b869 | 2016-10-12 22:00:46 | [diff] [blame] | 6071 | } |
[email protected] | 37567b43 | 2014-02-12 01:12:22 | [diff] [blame] | 6072 | } |
| 6073 | |
[email protected] | 277857a | 2014-06-03 10:38:01 | [diff] [blame] | 6074 | bool WebContentsImpl::CanOverscrollContent() const { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 6075 | OPTIONAL_TRACE_EVENT0("content", "WebContentsImpl::CanOverscrollContent"); |
[email protected] | e85165c64 | 2014-06-10 14:34:31 | [diff] [blame] | 6076 | // Disable overscroll when touch emulation is on. See crbug.com/369938. |
[email protected] | 34ff1cfc | 2014-08-20 06:16:05 | [diff] [blame] | 6077 | if (force_disable_overscroll_content_) |
[email protected] | e85165c64 | 2014-06-10 14:34:31 | [diff] [blame] | 6078 | return false; |
danakj | 5f22bcc | 2021-02-12 18:09:01 | [diff] [blame] | 6079 | return delegate_ && delegate_->CanOverscrollContent(); |
[email protected] | 37567b43 | 2014-02-12 01:12:22 | [diff] [blame] | 6080 | } |
| 6081 | |
Jeremy Roman | 2d8dfe13 | 2021-07-06 20:51:26 | [diff] [blame] | 6082 | void WebContentsImpl::OnThemeColorChanged(PageImpl& page) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 6083 | OPTIONAL_TRACE_EVENT1("content", "WebContentsImpl::OnThemeColorChanged", |
Jeremy Roman | 2d8dfe13 | 2021-07-06 20:51:26 | [diff] [blame] | 6084 | "page", page); |
| 6085 | if (!page.IsPrimary()) |
| 6086 | return; |
| 6087 | |
| 6088 | if (page.did_first_visually_non_empty_paint() && |
| 6089 | last_sent_theme_color_ != page.theme_color()) { |
Andrew Grieve | e19cd93e | 2021-01-22 05:43:06 | [diff] [blame] | 6090 | observers_.NotifyObservers(&WebContentsObserver::DidChangeThemeColor); |
Jeremy Roman | 2d8dfe13 | 2021-07-06 20:51:26 | [diff] [blame] | 6091 | last_sent_theme_color_ = page.theme_color(); |
peter | 90afaba | 2015-06-01 12:05:29 | [diff] [blame] | 6092 | } |
[email protected] | e710476 | 2014-06-20 19:17:25 | [diff] [blame] | 6093 | } |
| 6094 | |
Jeremy Roman | 2d8dfe13 | 2021-07-06 20:51:26 | [diff] [blame] | 6095 | void WebContentsImpl::OnBackgroundColorChanged(PageImpl& page) { |
| 6096 | if (!page.IsPrimary()) |
| 6097 | return; |
| 6098 | |
| 6099 | if (page.did_first_visually_non_empty_paint() && |
| 6100 | last_sent_background_color_ != page.background_color()) { |
Andrew Grieve | e19cd93e | 2021-01-22 05:43:06 | [diff] [blame] | 6101 | observers_.NotifyObservers(&WebContentsObserver::OnBackgroundColorChanged); |
Jeremy Roman | 2d8dfe13 | 2021-07-06 20:51:26 | [diff] [blame] | 6102 | last_sent_background_color_ = page.background_color(); |
Hassan Talat | f8f4844 | 2021-06-11 00:46:56 | [diff] [blame] | 6103 | return; |
| 6104 | } |
| 6105 | |
Jeremy Roman | 2d8dfe13 | 2021-07-06 20:51:26 | [diff] [blame] | 6106 | if (page.background_color().has_value()) { |
Hassan Talat | f8f4844 | 2021-06-11 00:46:56 | [diff] [blame] | 6107 | if (auto* view = GetRenderWidgetHostView()) { |
| 6108 | static_cast<RenderWidgetHostViewBase*>(view)->SetContentBackgroundColor( |
Jeremy Roman | 2d8dfe13 | 2021-07-06 20:51:26 | [diff] [blame] | 6109 | page.background_color().value()); |
Hassan Talat | f8f4844 | 2021-06-11 00:46:56 | [diff] [blame] | 6110 | } |
Alan Cutter | d73a15d9 | 2020-08-21 07:12:45 | [diff] [blame] | 6111 | } |
| 6112 | } |
| 6113 | |
Michael Bai | 19f17a30 | 2021-12-08 04:08:33 | [diff] [blame] | 6114 | void WebContentsImpl::DidInferColorScheme(PageImpl& page) { |
| 6115 | // If the page is primary, notify embedders that the current inferred color |
| 6116 | // scheme for this WebContents has changed. |
| 6117 | if (page.IsPrimary()) { |
| 6118 | observers_.NotifyObservers(&WebContentsObserver::InferredColorSchemeUpdated, |
| 6119 | page.inferred_color_scheme()); |
Michael Bai | 93d372bd | 2022-03-28 23:05:54 | [diff] [blame] | 6120 | if (page.inferred_color_scheme().has_value()) { |
| 6121 | bool dark = page.inferred_color_scheme().value() == |
| 6122 | blink::mojom::PreferredColorScheme::kDark; |
| 6123 | base::UmaHistogramBoolean("Power.DarkMode.InferredDarkPageColorScheme", |
| 6124 | dark); |
Michael Bai | e0fc428f | 2022-04-01 02:48:05 | [diff] [blame] | 6125 | if (web_preferences_->preferred_color_scheme == |
| 6126 | blink::mojom::PreferredColorScheme::kDark) { |
Michael Bai | 93d372bd | 2022-03-28 23:05:54 | [diff] [blame] | 6127 | base::UmaHistogramBoolean( |
| 6128 | "Power.DarkMode.DarkColorScheme.InferredDarkPageColorScheme", dark); |
| 6129 | } |
| 6130 | } |
Michael Bai | 19f17a30 | 2021-12-08 04:08:33 | [diff] [blame] | 6131 | } |
| 6132 | } |
| 6133 | |
David Bokan | d6e44055b | 2022-09-21 03:58:08 | [diff] [blame] | 6134 | void WebContentsImpl::OnVirtualKeyboardModeChanged(PageImpl& page) { |
| 6135 | if (!page.IsPrimary()) |
| 6136 | return; |
| 6137 | |
| 6138 | observers_.NotifyObservers(&WebContentsObserver::VirtualKeyboardModeChanged, |
| 6139 | page.virtual_keyboard_mode()); |
| 6140 | } |
| 6141 | |
Gyuyoung Kim | 1905f7c | 2020-05-02 00:00:24 | [diff] [blame] | 6142 | void WebContentsImpl::DidLoadResourceFromMemoryCache( |
nick | a0ac838 | 2016-12-15 23:59:23 | [diff] [blame] | 6143 | RenderFrameHostImpl* source, |
[email protected] | 724159a | 2010-12-30 01:11:18 | [diff] [blame] | 6144 | const GURL& url, |
[email protected] | 7043596 | 2011-08-02 20:13:28 | [diff] [blame] | 6145 | const std::string& http_method, |
[email protected] | 6d6cfb3a | 2012-05-23 22:53:18 | [diff] [blame] | 6146 | const std::string& mime_type, |
Arthur Sonzogni | 54013f2 | 2021-09-03 14:57:12 | [diff] [blame] | 6147 | network::mojom::RequestDestination request_destination, |
| 6148 | bool include_credentials) { |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 6149 | OPTIONAL_TRACE_EVENT2("content", |
| 6150 | "WebContentsImpl::DidLoadResourceFromMemoryCache", |
| 6151 | "render_frame_host", source, "url", url); |
Andrew Grieve | e19cd93e | 2021-01-22 05:43:06 | [diff] [blame] | 6152 | observers_.NotifyObservers( |
Lucas Furukawa Gadani | 07b45e15 | 2021-04-14 00:56:44 | [diff] [blame] | 6153 | &WebContentsObserver::DidLoadResourceFromMemoryCache, source, url, |
| 6154 | mime_type, request_destination); |
[email protected] | b0f724c | 2013-09-05 04:21:13 | [diff] [blame] | 6155 | |
Arthur Sonzogni | 54013f2 | 2021-09-03 14:57:12 | [diff] [blame] | 6156 | if (!url.is_valid() || !url.SchemeIsHTTPOrHTTPS()) |
| 6157 | return; |
shivanigithub | c688ced | 2020-11-04 19:46:15 | [diff] [blame] | 6158 | |
Arthur Sonzogni | 54013f2 | 2021-09-03 14:57:12 | [diff] [blame] | 6159 | StoragePartition* partition = source->GetProcess()->GetStoragePartition(); |
| 6160 | |
| 6161 | DCHECK(!blink::IsRequestDestinationFrame(request_destination)); |
| 6162 | partition->GetNetworkContext()->NotifyExternalCacheHit( |
| 6163 | url, http_method, source->GetNetworkIsolationKey(), |
| 6164 | /*is_subframe_document_resource=*/false, |
| 6165 | /*include_credentials=*/include_credentials); |
[email protected] | 724159a | 2010-12-30 01:11:18 | [diff] [blame] | 6166 | } |
| 6167 | |
Sreeja Kamishetty | 4978330 | 2022-01-28 17:52:25 | [diff] [blame] | 6168 | void WebContentsImpl::PrimaryMainDocumentElementAvailable() { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 6169 | OPTIONAL_TRACE_EVENT0("content", |
Sreeja Kamishetty | 4978330 | 2022-01-28 17:52:25 | [diff] [blame] | 6170 | "WebContentsImpl::PrimaryMainDocumentElementAvailable"); |
Clark DuVall | 22aaff3 | 2021-03-18 17:17:47 | [diff] [blame] | 6171 | SCOPED_UMA_HISTOGRAM_TIMER( |
Sreeja Kamishetty | 4978330 | 2022-01-28 17:52:25 | [diff] [blame] | 6172 | "WebContentsObserver.PrimaryMainDocumentElementAvailable"); |
| 6173 | observers_.NotifyObservers( |
| 6174 | &WebContentsObserver::PrimaryMainDocumentElementAvailable); |
Julie Jeongeun Kim | 0b10349 | 2020-01-29 05:30:51 | [diff] [blame] | 6175 | } |
| 6176 | |
carlosk | d9d9794 | 2017-02-16 08:58:09 | [diff] [blame] | 6177 | void WebContentsImpl::PassiveInsecureContentFound(const GURL& resource_url) { |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 6178 | OPTIONAL_TRACE_EVENT1("content", |
| 6179 | "WebContentsImpl::PassiveInsecureContentFound", |
| 6180 | "resource_url", resource_url); |
Sergey Kuznetsov | 3ebc01b | 2018-02-01 04:57:36 | [diff] [blame] | 6181 | if (delegate_) { |
| 6182 | delegate_->PassiveInsecureContentFound(resource_url); |
| 6183 | } |
carlosk | d9d9794 | 2017-02-16 08:58:09 | [diff] [blame] | 6184 | } |
| 6185 | |
| 6186 | bool WebContentsImpl::ShouldAllowRunningInsecureContent( |
carlosk | d9d9794 | 2017-02-16 08:58:09 | [diff] [blame] | 6187 | bool allowed_per_prefs, |
| 6188 | const url::Origin& origin, |
| 6189 | const GURL& resource_url) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 6190 | OPTIONAL_TRACE_EVENT0("content", |
| 6191 | "WebContentsImpl::ShouldAllowRunningInsecureContent"); |
Sergey Kuznetsov | 3ebc01b | 2018-02-01 04:57:36 | [diff] [blame] | 6192 | if (delegate_) { |
Carlos IL | 29294a6 | 2020-05-15 00:21:16 | [diff] [blame] | 6193 | return delegate_->ShouldAllowRunningInsecureContent(this, allowed_per_prefs, |
| 6194 | origin, resource_url); |
Sergey Kuznetsov | 3ebc01b | 2018-02-01 04:57:36 | [diff] [blame] | 6195 | } |
| 6196 | |
| 6197 | return allowed_per_prefs; |
carlosk | d9d9794 | 2017-02-16 08:58:09 | [diff] [blame] | 6198 | } |
| 6199 | |
Lukasz Anforowicz | e1b954d9 | 2017-10-30 21:28:06 | [diff] [blame] | 6200 | void WebContentsImpl::ViewSource(RenderFrameHostImpl* frame) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 6201 | OPTIONAL_TRACE_EVENT1("content", "WebContentsImpl::ViewSource", |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 6202 | "render_frame_host", frame); |
Lukasz Anforowicz | e1b954d9 | 2017-10-30 21:28:06 | [diff] [blame] | 6203 | DCHECK_EQ(this, WebContents::FromRenderFrameHost(frame)); |
| 6204 | |
| 6205 | // Don't do anything if there is no |delegate_| that could accept and show the |
| 6206 | // new WebContents containing the view-source. |
| 6207 | if (!delegate_) |
| 6208 | return; |
| 6209 | |
| 6210 | // Use the last committed entry, since the pending entry hasn't loaded yet and |
| 6211 | // won't be copied into the cloned tab. |
| 6212 | NavigationEntryImpl* last_committed_entry = |
Jeremy Roman | b26f7d1 | 2021-06-16 22:12:05 | [diff] [blame] | 6213 | frame->frame_tree()->controller().GetLastCommittedEntry(); |
Lukasz Anforowicz | e1b954d9 | 2017-10-30 21:28:06 | [diff] [blame] | 6214 | if (!last_committed_entry) |
| 6215 | return; |
| 6216 | |
| 6217 | FrameNavigationEntry* frame_entry = |
| 6218 | last_committed_entry->GetFrameEntry(frame->frame_tree_node()); |
| 6219 | if (!frame_entry) |
| 6220 | return; |
| 6221 | |
| 6222 | // Any new WebContents opened while this WebContents is in fullscreen can be |
| 6223 | // used to confuse the user, so drop fullscreen. |
Avi Drissman | 1a55a9d6 | 2020-03-10 18:56:45 | [diff] [blame] | 6224 | base::ScopedClosureRunner fullscreen_block = ForSecurityDropFullscreen(); |
| 6225 | // The new view source contents will be independent of this contents, so |
| 6226 | // release the fullscreen block. |
| 6227 | fullscreen_block.RunAndReset(); |
Lukasz Anforowicz | e1b954d9 | 2017-10-30 21:28:06 | [diff] [blame] | 6228 | |
| 6229 | // We intentionally don't share the SiteInstance with the original frame so |
| 6230 | // that view source has a consistent process model and always ends up in a new |
| 6231 | // process (https://crbug.com/699493). |
Lei Zhang | d40a2747 | 2021-04-15 19:04:42 | [diff] [blame] | 6232 | scoped_refptr<SiteInstanceImpl> site_instance_for_view_source; |
Lukasz Anforowicz | 435bcb58 | 2019-07-12 20:50:06 | [diff] [blame] | 6233 | // Referrer and initiator are not important, because view-source should not |
| 6234 | // hit the network, but should be served from the cache instead. |
Lukasz Anforowicz | e1b954d9 | 2017-10-30 21:28:06 | [diff] [blame] | 6235 | Referrer referrer_for_view_source; |
Anton Bikineev | f62d1bf | 2021-05-15 17:56:07 | [diff] [blame] | 6236 | absl::optional<url::Origin> initiator_for_view_source = absl::nullopt; |
W. James MacLean | 23e90a1 | 2022-12-21 04:38:21 | [diff] [blame] | 6237 | absl::optional<GURL> initiator_base_url_for_view_source = absl::nullopt; |
Lukasz Anforowicz | e1b954d9 | 2017-10-30 21:28:06 | [diff] [blame] | 6238 | // Do not restore title, derive it from the url. |
Jan Wilken Dörrie | aace0cfef | 2021-03-11 22:01:58 | [diff] [blame] | 6239 | std::u16string title_for_view_source; |
Lukasz Anforowicz | e1b954d9 | 2017-10-30 21:28:06 | [diff] [blame] | 6240 | auto navigation_entry = std::make_unique<NavigationEntryImpl>( |
| 6241 | site_instance_for_view_source, frame_entry->url(), |
Lukasz Anforowicz | 435bcb58 | 2019-07-12 20:50:06 | [diff] [blame] | 6242 | referrer_for_view_source, initiator_for_view_source, |
W. James MacLean | 23e90a1 | 2022-12-21 04:38:21 | [diff] [blame] | 6243 | initiator_base_url_for_view_source, title_for_view_source, |
| 6244 | ui::PAGE_TRANSITION_LINK, |
Marijn Kruisselbrink | 7a0d5e18 | 2018-05-24 22:55:09 | [diff] [blame] | 6245 | /* is_renderer_initiated = */ false, |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 6246 | /* blob_url_loader_factory = */ nullptr, /* is_initial_entry = */ false); |
Joel Hockey | 891e8806 | 2020-04-30 05:38:44 | [diff] [blame] | 6247 | const GURL url(content::kViewSourceScheme + std::string(":") + |
| 6248 | frame_entry->url().spec()); |
| 6249 | navigation_entry->SetVirtualURL(url); |
shivanigithub | c688ced | 2020-11-04 19:46:15 | [diff] [blame] | 6250 | |
Matt Menke | f5ee978 | 2021-02-09 05:17:48 | [diff] [blame] | 6251 | // View source opens the URL in a new tab as a top-level navigation. A |
| 6252 | // top-level navigation may have a different IsolationInfo than the source |
| 6253 | // iframe, so preserve the IsolationInfo from the origin frame, to use the |
| 6254 | // same network shard and increase chances of a cache hit. |
| 6255 | navigation_entry->set_isolation_info( |
| 6256 | frame->ComputeIsolationInfoForNavigation(navigation_entry->GetURL())); |
Yao Xiao | f68f57d | 2019-10-03 04:20:05 | [diff] [blame] | 6257 | |
Lukasz Anforowicz | e1b954d9 | 2017-10-30 21:28:06 | [diff] [blame] | 6258 | // Do not restore scroller position. |
| 6259 | // TODO(creis, lukasza, arthursonzogni): Do not reuse the original PageState, |
| 6260 | // but start from a new one and only copy the needed data. |
Miyoung Shin | 5d77f7207 | 2020-10-09 15:14:20 | [diff] [blame] | 6261 | const blink::PageState& new_page_state = |
Lukasz Anforowicz | e1b954d9 | 2017-10-30 21:28:06 | [diff] [blame] | 6262 | frame_entry->page_state().RemoveScrollOffset(); |
| 6263 | |
| 6264 | scoped_refptr<FrameNavigationEntry> new_frame_entry = |
| 6265 | navigation_entry->root_node()->frame_entry; |
| 6266 | new_frame_entry->set_method(frame_entry->method()); |
| 6267 | new_frame_entry->SetPageState(new_page_state); |
| 6268 | |
| 6269 | // Create a new WebContents, which is used to display the source code. |
erikchen | bee5c962 | 2018-04-27 19:30:25 | [diff] [blame] | 6270 | std::unique_ptr<WebContents> view_source_contents = |
Erik Chen | bb8e738e | 2018-04-28 14:10:43 | [diff] [blame] | 6271 | Create(CreateParams(GetBrowserContext())); |
Lukasz Anforowicz | e1b954d9 | 2017-10-30 21:28:06 | [diff] [blame] | 6272 | |
| 6273 | // Restore the previously created NavigationEntry. |
| 6274 | std::vector<std::unique_ptr<NavigationEntry>> navigation_entries; |
| 6275 | navigation_entries.push_back(std::move(navigation_entry)); |
Lukasz Anforowicz | 0de0f45 | 2020-12-02 19:57:15 | [diff] [blame] | 6276 | view_source_contents->GetController().Restore(0, RestoreType::kRestored, |
Lukasz Anforowicz | e1b954d9 | 2017-10-30 21:28:06 | [diff] [blame] | 6277 | &navigation_entries); |
| 6278 | |
| 6279 | // Add |view_source_contents| as a new tab. |
Lukasz Anforowicz | e1b954d9 | 2017-10-30 21:28:06 | [diff] [blame] | 6280 | constexpr bool kUserGesture = true; |
| 6281 | bool ignored_was_blocked; |
Joel Hockey | 891e8806 | 2020-04-30 05:38:44 | [diff] [blame] | 6282 | delegate_->AddNewContents(this, std::move(view_source_contents), url, |
Lukasz Anforowicz | e1b954d9 | 2017-10-30 21:28:06 | [diff] [blame] | 6283 | WindowOpenDisposition::NEW_FOREGROUND_TAB, |
Brad Triebwasser | 767c27a | 2022-08-25 22:56:05 | [diff] [blame] | 6284 | blink::mojom::WindowFeatures(), kUserGesture, |
| 6285 | &ignored_was_blocked); |
Lukasz Anforowicz | e1b954d9 | 2017-10-30 21:28:06 | [diff] [blame] | 6286 | // Note that the |delegate_| could have deleted |view_source_contents| during |
| 6287 | // AddNewContents method call. |
| 6288 | } |
| 6289 | |
Jay Civelli | 116683f | 2018-03-27 19:56:23 | [diff] [blame] | 6290 | void WebContentsImpl::ResourceLoadComplete( |
Kevin McNee | fb86fcf | 2021-02-26 23:20:57 | [diff] [blame] | 6291 | RenderFrameHostImpl* render_frame_host, |
Clark DuVall | 36164bd | 2018-08-09 22:49:08 | [diff] [blame] | 6292 | const GlobalRequestID& request_id, |
Minggang Wang | ee5af39 | 2020-02-05 02:55:28 | [diff] [blame] | 6293 | blink::mojom::ResourceLoadInfoPtr resource_load_info) { |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 6294 | OPTIONAL_TRACE_EVENT2("content", "WebContentsImpl::ResourceLoadComplete", |
| 6295 | "render_frame_host", render_frame_host, "request_id", |
| 6296 | request_id); |
Andrew Grieve | e19cd93e | 2021-01-22 05:43:06 | [diff] [blame] | 6297 | const blink::mojom::ResourceLoadInfo& resource_load_info_ref = |
| 6298 | *resource_load_info; |
| 6299 | observers_.NotifyObservers(&WebContentsObserver::ResourceLoadComplete, |
| 6300 | render_frame_host, request_id, |
| 6301 | resource_load_info_ref); |
John Abd-El-Malek | d488264 | 2017-12-04 21:45:19 | [diff] [blame] | 6302 | } |
| 6303 | |
Gyuyoung Kim | 1ac4ca78 | 2020-09-11 03:32:51 | [diff] [blame] | 6304 | const blink::web_pref::WebPreferences& |
| 6305 | WebContentsImpl::GetOrCreateWebPreferences() { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 6306 | OPTIONAL_TRACE_EVENT0("content", |
| 6307 | "WebContentsImpl::GetOrCreateWebPreferences"); |
Rakina Zata Amni | 347b7090 | 2020-07-22 10:49:04 | [diff] [blame] | 6308 | // Compute WebPreferences based on the current state if it's null. |
| 6309 | if (!web_preferences_) |
Rakina Zata Amni | 4029b6d | 2020-07-28 02:36:20 | [diff] [blame] | 6310 | OnWebPreferencesChanged(); |
Rakina Zata Amni | 347b7090 | 2020-07-22 10:49:04 | [diff] [blame] | 6311 | return *web_preferences_.get(); |
| 6312 | } |
| 6313 | |
Gyuyoung Kim | 1ac4ca78 | 2020-09-11 03:32:51 | [diff] [blame] | 6314 | void WebContentsImpl::SetWebPreferences( |
| 6315 | const blink::web_pref::WebPreferences& prefs) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 6316 | OPTIONAL_TRACE_EVENT0("content", "WebContentsImpl::SetWebPreferences"); |
Gyuyoung Kim | 1ac4ca78 | 2020-09-11 03:32:51 | [diff] [blame] | 6317 | web_preferences_ = std::make_unique<blink::web_pref::WebPreferences>(prefs); |
Rakina Zata Amni | 347b7090 | 2020-07-22 10:49:04 | [diff] [blame] | 6318 | // Get all the RenderViewHosts (except the ones for currently back-forward |
| 6319 | // cached pages), and make them send the current WebPreferences |
| 6320 | // to the renderer. WebPreferences updates for back-forward cached pages will |
| 6321 | // be sent when we restore those pages from the back-forward cache. |
Sharon Yang | ed88454 | 2023-02-02 17:33:44 | [diff] [blame] | 6322 | primary_frame_tree_.ForEachRenderViewHost( |
| 6323 | [](RenderViewHostImpl* rvh) { rvh->SendWebPreferencesToRenderer(); }); |
Rakina Zata Amni | 347b7090 | 2020-07-22 10:49:04 | [diff] [blame] | 6324 | } |
| 6325 | |
| 6326 | void WebContentsImpl::RecomputeWebPreferencesSlow() { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 6327 | OPTIONAL_TRACE_EVENT0("content", |
| 6328 | "WebContentsImpl::RecomputeWebPreferencesSlow"); |
Rakina Zata Amni | 4029b6d | 2020-07-28 02:36:20 | [diff] [blame] | 6329 | // OnWebPreferencesChanged is a no-op when this is true. |
| 6330 | if (updating_web_preferences_) |
| 6331 | return; |
Rakina Zata Amni | 347b7090 | 2020-07-22 10:49:04 | [diff] [blame] | 6332 | // Resets |web_preferences_| so that we won't have any cached value for slow |
| 6333 | // attributes (which won't get recomputed if we have pre-existing values for |
| 6334 | // them). |
| 6335 | web_preferences_.reset(); |
Rakina Zata Amni | 4029b6d | 2020-07-28 02:36:20 | [diff] [blame] | 6336 | OnWebPreferencesChanged(); |
Rakina Zata Amni | 347b7090 | 2020-07-22 10:49:04 | [diff] [blame] | 6337 | } |
| 6338 | |
Bo Liu | b5e79d9 | 2021-06-12 01:40:41 | [diff] [blame] | 6339 | absl::optional<SkColor> WebContentsImpl::GetBaseBackgroundColor() { |
| 6340 | return page_base_background_color_; |
| 6341 | } |
| 6342 | |
Wei Li | 5bb65974 | 2018-02-14 03:07:58 | [diff] [blame] | 6343 | void WebContentsImpl::PrintCrossProcessSubframe( |
| 6344 | const gfx::Rect& rect, |
| 6345 | int document_cookie, |
Kevin McNee | fb86fcf | 2021-02-26 23:20:57 | [diff] [blame] | 6346 | RenderFrameHostImpl* subframe_host) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 6347 | OPTIONAL_TRACE_EVENT1("content", "WebContentsImpl::PrintCrossProcessSubframe", |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 6348 | "subframe", subframe_host); |
Wei Li | 5c0b4d1 | 2018-03-22 15:05:22 | [diff] [blame] | 6349 | auto* outer_contents = GetOuterWebContents(); |
| 6350 | if (outer_contents) { |
| 6351 | // When an extension or app page is printed, the content should be |
| 6352 | // composited with outer content, so the outer contents should handle the |
| 6353 | // print request. |
| 6354 | outer_contents->PrintCrossProcessSubframe(rect, document_cookie, |
| 6355 | subframe_host); |
| 6356 | return; |
| 6357 | } |
| 6358 | |
Wei Li | 5bb65974 | 2018-02-14 03:07:58 | [diff] [blame] | 6359 | // If there is no delegate such as in tests or during deletion, do nothing. |
| 6360 | if (!delegate_) |
| 6361 | return; |
| 6362 | |
| 6363 | delegate_->PrintCrossProcessSubframe(this, rect, document_cookie, |
| 6364 | subframe_host); |
| 6365 | } |
| 6366 | |
ckitagawa | 8695799b | 2020-02-05 16:08:49 | [diff] [blame] | 6367 | void WebContentsImpl::CapturePaintPreviewOfCrossProcessSubframe( |
| 6368 | const gfx::Rect& rect, |
| 6369 | const base::UnguessableToken& guid, |
Kevin McNee | fb86fcf | 2021-02-26 23:20:57 | [diff] [blame] | 6370 | RenderFrameHostImpl* render_frame_host) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 6371 | OPTIONAL_TRACE_EVENT1( |
| 6372 | "content", "WebContentsImpl::CapturePaintPreviewOfCrossProcessSubframe", |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 6373 | "render_frame_host", render_frame_host); |
ckitagawa | 8695799b | 2020-02-05 16:08:49 | [diff] [blame] | 6374 | if (!delegate_) |
| 6375 | return; |
ckitagawa | c43ac98 | 2020-07-07 17:41:17 | [diff] [blame] | 6376 | delegate_->CapturePaintPreviewOfSubframe(this, rect, guid, render_frame_host); |
ckitagawa | 8695799b | 2020-02-05 16:08:49 | [diff] [blame] | 6377 | } |
| 6378 | |
Xiaohan Wang | 24ec934 | 2022-01-15 17:34:22 | [diff] [blame] | 6379 | #if BUILDFLAG(IS_ANDROID) |
rob.buis | 300b087 | 2017-03-10 20:43:58 | [diff] [blame] | 6380 | base::android::ScopedJavaLocalRef<jobject> |
| 6381 | WebContentsImpl::GetJavaRenderFrameHostDelegate() { |
| 6382 | return GetJavaWebContents(); |
| 6383 | } |
| 6384 | #endif |
| 6385 | |
Kevin McNee | fb86fcf | 2021-02-26 23:20:57 | [diff] [blame] | 6386 | void WebContentsImpl::DOMContentLoaded(RenderFrameHostImpl* render_frame_host) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 6387 | OPTIONAL_TRACE_EVENT1("content", "WebContentsImpl::DOMContentLoaded", |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 6388 | "render_frame_host", render_frame_host); |
Clark DuVall | 22aaff3 | 2021-03-18 17:17:47 | [diff] [blame] | 6389 | SCOPED_UMA_HISTOGRAM_TIMER("WebContentsObserver.DOMContentLoaded"); |
Andrew Grieve | e19cd93e | 2021-01-22 05:43:06 | [diff] [blame] | 6390 | observers_.NotifyObservers(&WebContentsObserver::DOMContentLoaded, |
| 6391 | render_frame_host); |
[email protected] | 724159a | 2010-12-30 01:11:18 | [diff] [blame] | 6392 | } |
| 6393 | |
Kevin McNee | fb86fcf | 2021-02-26 23:20:57 | [diff] [blame] | 6394 | void WebContentsImpl::OnDidFinishLoad(RenderFrameHostImpl* render_frame_host, |
nick | a0ac838 | 2016-12-15 23:59:23 | [diff] [blame] | 6395 | const GURL& url) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 6396 | OPTIONAL_TRACE_EVENT2("content", "WebContentsImpl::OnDidFinishLoad", |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 6397 | "render_frame_host", render_frame_host, "url", url); |
[email protected] | 8b3af1e | 2014-01-24 13:29:12 | [diff] [blame] | 6398 | GURL validated_url(url); |
Gyuyoung Kim | 525e0c5d | 2020-03-09 15:04:19 | [diff] [blame] | 6399 | render_frame_host->GetProcess()->FilterURL(false, &validated_url); |
[email protected] | 028053d4 | 2014-03-05 22:20:37 | [diff] [blame] | 6400 | |
Clark DuVall | 22aaff3 | 2021-03-18 17:17:47 | [diff] [blame] | 6401 | { |
| 6402 | SCOPED_UMA_HISTOGRAM_TIMER("WebContentsObserver.DidFinishLoad"); |
| 6403 | observers_.NotifyObservers(&WebContentsObserver::DidFinishLoad, |
| 6404 | render_frame_host, validated_url); |
| 6405 | } |
Dave Tapuska | 21e9da0 | 2021-12-15 20:58:20 | [diff] [blame] | 6406 | size_t tree_size = GetFrameTreeSize(&primary_frame_tree_); |
yilkal | 172da77 | 2019-11-06 22:49:07 | [diff] [blame] | 6407 | if (max_loaded_frame_count_ < tree_size) |
| 6408 | max_loaded_frame_count_ = tree_size; |
| 6409 | |
Dave Tapuska | 21e9da0 | 2021-12-15 20:58:20 | [diff] [blame] | 6410 | if (!render_frame_host->GetParentOrOuterDocument()) |
yilkal | 172da77 | 2019-11-06 22:49:07 | [diff] [blame] | 6411 | UMA_HISTOGRAM_COUNTS_1000("Navigation.MainFrame.FrameCount", tree_size); |
[email protected] | 1a55c5be | 2011-11-29 11:36:31 | [diff] [blame] | 6412 | } |
| 6413 | |
Carlos Caballero | ede6f8c | 2021-01-28 11:01:50 | [diff] [blame] | 6414 | bool WebContentsImpl::IsAllowedToGoToEntryAtOffset(int32_t offset) { |
| 6415 | // TODO(https://crbug.com/1170277): This should probably be renamed to |
| 6416 | // WebContentsDelegate::IsAllowedToGoToEntryAtOffset or |
| 6417 | // ShouldGoToEntryAtOffset |
| 6418 | return !delegate_ || delegate_->OnGoToEntryOffset(offset); |
[email protected] | 21681395 | 2011-05-19 22:21:26 | [diff] [blame] | 6419 | } |
| 6420 | |
Kevin McNee | 3183a779 | 2021-11-09 21:03:36 | [diff] [blame] | 6421 | void WebContentsImpl::OnPageScaleFactorChanged(PageImpl& source) { |
| 6422 | OPTIONAL_TRACE_EVENT1("content", "WebContentsImpl::OnPageScaleFactorChanged", |
| 6423 | "source", source); |
mcnee | 432e47d | 2015-11-09 19:37:46 | [diff] [blame] | 6424 | |
Kevin McNee | 3183a779 | 2021-11-09 21:03:36 | [diff] [blame] | 6425 | if (source.IsPrimary()) { |
| 6426 | observers_.NotifyObservers(&WebContentsObserver::OnPageScaleFactorChanged, |
Kevin McNee | c4325ba | 2022-04-08 23:18:23 | [diff] [blame] | 6427 | source.GetPageScaleFactor()); |
mcnee | 432e47d | 2015-11-09 19:37:46 | [diff] [blame] | 6428 | } |
mcnee | 432e47d | 2015-11-09 19:37:46 | [diff] [blame] | 6429 | } |
| 6430 | |
Kent Tamura | 8c9e056 | 2018-11-06 07:02:19 | [diff] [blame] | 6431 | void WebContentsImpl::EnumerateDirectory( |
| 6432 | RenderFrameHost* render_frame_host, |
Kent Tamura | 3abb32d | 2020-07-02 00:23:01 | [diff] [blame] | 6433 | scoped_refptr<FileChooserImpl::FileSelectListenerImpl> listener, |
Kent Tamura | 8c9e056 | 2018-11-06 07:02:19 | [diff] [blame] | 6434 | const base::FilePath& directory_path) { |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 6435 | OPTIONAL_TRACE_EVENT2("content", "WebContentsImpl::EnumerateDirectory", |
| 6436 | "render_frame_host", render_frame_host, |
| 6437 | "directory_path", directory_path); |
Avi Drissman | 1a55a9d6 | 2020-03-10 18:56:45 | [diff] [blame] | 6438 | // Any explicit focusing of another window while this WebContents is in |
| 6439 | // fullscreen can be used to confuse the user, so drop fullscreen. |
| 6440 | base::ScopedClosureRunner fullscreen_block = ForSecurityDropFullscreen(); |
| 6441 | listener->SetFullscreenBlock(std::move(fullscreen_block)); |
| 6442 | |
Kent Tamura | 8c9e056 | 2018-11-06 07:02:19 | [diff] [blame] | 6443 | if (delegate_) |
| 6444 | delegate_->EnumerateDirectory(this, std::move(listener), directory_path); |
| 6445 | else |
| 6446 | listener->FileSelectionCanceled(); |
[email protected] | 3a29a6e | 2011-08-24 18:26:21 | [diff] [blame] | 6447 | } |
| 6448 | |
Dave Tapuska | f9fc312 | 2019-10-18 19:42:55 | [diff] [blame] | 6449 | void WebContentsImpl::RegisterProtocolHandler(RenderFrameHostImpl* source, |
| 6450 | const std::string& protocol, |
| 6451 | const GURL& url, |
Dave Tapuska | f9fc312 | 2019-10-18 19:42:55 | [diff] [blame] | 6452 | bool user_gesture) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 6453 | OPTIONAL_TRACE_EVENT2("content", "WebContentsImpl::RegisterProtocolHandler", |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 6454 | "render_frame_host", source, "protocol", protocol); |
nick | a0ac838 | 2016-12-15 23:59:23 | [diff] [blame] | 6455 | // TODO(nick): Do we need to apply FilterURL to |url|? |
[email protected] | e5f2de0 | 2012-07-20 22:15:43 | [diff] [blame] | 6456 | if (!delegate_) |
| 6457 | return; |
| 6458 | |
Frédéric Wang | b79fece | 2020-10-28 18:13:07 | [diff] [blame] | 6459 | blink::ProtocolHandlerSecurityLevel security_level = |
| 6460 | delegate_->GetProtocolHandlerSecurityLevel(source); |
| 6461 | |
Javier Fernández GarcÃa-Boente | 0bda5d2 | 2022-03-17 23:25:27 | [diff] [blame] | 6462 | // Run the protocol handler arguments normalization process defined in the |
| 6463 | // spec. |
| 6464 | // https://html.spec.whatwg.org/multipage/system-state.html#normalize-protocol-handler-parameters |
Raymes Khoury | 58373df | 2019-08-06 06:29:20 | [diff] [blame] | 6465 | if (!AreValidRegisterProtocolHandlerArguments( |
Frédéric Wang | b79fece | 2020-10-28 18:13:07 | [diff] [blame] | 6466 | protocol, url, source->GetLastCommittedOrigin(), security_level)) { |
Raymes Khoury | 58373df | 2019-08-06 06:29:20 | [diff] [blame] | 6467 | ReceivedBadMessage(source->GetProcess(), |
| 6468 | bad_message::REGISTER_PROTOCOL_HANDLER_INVALID_URL); |
[email protected] | b953542 | 2012-02-09 01:47:59 | [diff] [blame] | 6469 | return; |
Raymes Khoury | 58373df | 2019-08-06 06:29:20 | [diff] [blame] | 6470 | } |
[email protected] | 8f81063 | 2013-06-06 22:33:32 | [diff] [blame] | 6471 | |
Carlos Caballero | 8a6c6b7 | 2020-07-20 16:56:22 | [diff] [blame] | 6472 | delegate_->RegisterProtocolHandler(source, protocol, url, user_gesture); |
[email protected] | 7d18902 | 2011-08-25 22:54:20 | [diff] [blame] | 6473 | } |
| 6474 | |
Dave Tapuska | f9fc312 | 2019-10-18 19:42:55 | [diff] [blame] | 6475 | void WebContentsImpl::UnregisterProtocolHandler(RenderFrameHostImpl* source, |
| 6476 | const std::string& protocol, |
| 6477 | const GURL& url, |
| 6478 | bool user_gesture) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 6479 | OPTIONAL_TRACE_EVENT2("content", "WebContentsImpl::UnregisterProtocolHandler", |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 6480 | "render_frame_host", source, "protocol", protocol); |
nick | a0ac838 | 2016-12-15 23:59:23 | [diff] [blame] | 6481 | // TODO(nick): Do we need to apply FilterURL to |url|? |
[email protected] | f5273e5 | 2014-07-14 16:30:20 | [diff] [blame] | 6482 | if (!delegate_) |
| 6483 | return; |
| 6484 | |
Frédéric Wang | b79fece | 2020-10-28 18:13:07 | [diff] [blame] | 6485 | blink::ProtocolHandlerSecurityLevel security_level = |
| 6486 | delegate_->GetProtocolHandlerSecurityLevel(source); |
| 6487 | |
Raymes Khoury | 58373df | 2019-08-06 06:29:20 | [diff] [blame] | 6488 | if (!AreValidRegisterProtocolHandlerArguments( |
Frédéric Wang | b79fece | 2020-10-28 18:13:07 | [diff] [blame] | 6489 | protocol, url, source->GetLastCommittedOrigin(), security_level)) { |
Raymes Khoury | 58373df | 2019-08-06 06:29:20 | [diff] [blame] | 6490 | ReceivedBadMessage(source->GetProcess(), |
| 6491 | bad_message::REGISTER_PROTOCOL_HANDLER_INVALID_URL); |
[email protected] | f5273e5 | 2014-07-14 16:30:20 | [diff] [blame] | 6492 | return; |
Raymes Khoury | 58373df | 2019-08-06 06:29:20 | [diff] [blame] | 6493 | } |
[email protected] | f5273e5 | 2014-07-14 16:30:20 | [diff] [blame] | 6494 | |
Carlos Caballero | 8a6c6b7 | 2020-07-20 16:56:22 | [diff] [blame] | 6495 | delegate_->UnregisterProtocolHandler(source, protocol, url, user_gesture); |
[email protected] | f5273e5 | 2014-07-14 16:30:20 | [diff] [blame] | 6496 | } |
| 6497 | |
Ewelina Baran | ed2df8ad | 2021-12-22 10:05:37 | [diff] [blame] | 6498 | void WebContentsImpl::DomOperationResponse(RenderFrameHost* render_frame_host, |
| 6499 | const std::string& json_string) { |
| 6500 | OPTIONAL_TRACE_EVENT2("content", "WebContentsImpl::DomOperationResponse", |
| 6501 | "render_frame_host", render_frame_host, "json_string", |
| 6502 | json_string); |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 6503 | |
Ewelina Baran | ed2df8ad | 2021-12-22 10:05:37 | [diff] [blame] | 6504 | observers_.NotifyObservers(&WebContentsObserver::DomOperationResponse, |
| 6505 | render_frame_host, json_string); |
Gyuyoung Kim | c5f4ac1 | 2020-05-08 10:59:39 | [diff] [blame] | 6506 | } |
| 6507 | |
Gyuyoung Kim | 4c5051784 | 2020-06-04 17:50:44 | [diff] [blame] | 6508 | void WebContentsImpl::SavableResourceLinksResponse( |
| 6509 | RenderFrameHostImpl* source, |
| 6510 | const std::vector<GURL>& resources_list, |
| 6511 | blink::mojom::ReferrerPtr referrer, |
| 6512 | const std::vector<blink::mojom::SavableSubframePtr>& subframes) { |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 6513 | OPTIONAL_TRACE_EVENT1("content", |
| 6514 | "WebContentsImpl::SavableResourceLinksResponse", |
| 6515 | "render_frame_host", source); |
Dave Tapuska | a7cad8f49 | 2021-08-04 16:55:30 | [diff] [blame] | 6516 | if (save_package_) { |
| 6517 | save_package_->SavableResourceLinksResponse(source, resources_list, |
| 6518 | std::move(referrer), subframes); |
| 6519 | } |
Gyuyoung Kim | 4c5051784 | 2020-06-04 17:50:44 | [diff] [blame] | 6520 | } |
| 6521 | |
| 6522 | void WebContentsImpl::SavableResourceLinksError(RenderFrameHostImpl* source) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 6523 | OPTIONAL_TRACE_EVENT1("content", "WebContentsImpl::SavableResourceLinksError", |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 6524 | "render_frame_host", source); |
Dave Tapuska | a7cad8f49 | 2021-08-04 16:55:30 | [diff] [blame] | 6525 | if (save_package_) { |
| 6526 | save_package_->SavableResourceLinksError(source); |
| 6527 | } |
Gyuyoung Kim | 4c5051784 | 2020-06-04 17:50:44 | [diff] [blame] | 6528 | } |
| 6529 | |
Alexander Timin | 17edc74 | 2020-04-23 18:22:18 | [diff] [blame] | 6530 | void WebContentsImpl::OnServiceWorkerAccessed( |
| 6531 | RenderFrameHost* render_frame_host, |
| 6532 | const GURL& scope, |
| 6533 | AllowServiceWorkerResult allowed) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 6534 | OPTIONAL_TRACE_EVENT2("content", "WebContentsImpl::OnServiceWorkerAccessed", |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 6535 | "render_frame_host", render_frame_host, "scope", scope); |
Andrew Grieve | e19cd93e | 2021-01-22 05:43:06 | [diff] [blame] | 6536 | // Use a variable to select between overloads. |
| 6537 | void (WebContentsObserver::*func)(RenderFrameHost*, const GURL&, |
| 6538 | AllowServiceWorkerResult) = |
| 6539 | &WebContentsObserver::OnServiceWorkerAccessed; |
| 6540 | observers_.NotifyObservers(func, render_frame_host, scope, allowed); |
Alexander Timin | 17edc74 | 2020-04-23 18:22:18 | [diff] [blame] | 6541 | } |
| 6542 | |
| 6543 | void WebContentsImpl::OnServiceWorkerAccessed( |
| 6544 | NavigationHandle* navigation, |
| 6545 | const GURL& scope, |
| 6546 | AllowServiceWorkerResult allowed) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 6547 | OPTIONAL_TRACE_EVENT2("content", "WebContentsImpl::OnServiceWorkerAccessed", |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 6548 | "navigation_handle", navigation, "scope", scope); |
Andrew Grieve | e19cd93e | 2021-01-22 05:43:06 | [diff] [blame] | 6549 | // Use a variable to select between overloads. |
| 6550 | void (WebContentsObserver::*func)(NavigationHandle*, const GURL&, |
| 6551 | AllowServiceWorkerResult) = |
| 6552 | &WebContentsObserver::OnServiceWorkerAccessed; |
| 6553 | observers_.NotifyObservers(func, navigation, scope, allowed); |
Alexander Timin | 17edc74 | 2020-04-23 18:22:18 | [diff] [blame] | 6554 | } |
| 6555 | |
Miyoung Shin | cb6475a | 2019-11-09 10:49:45 | [diff] [blame] | 6556 | void WebContentsImpl::OnColorChooserFactoryReceiver( |
Julie Jeongeun Kim | 8e2879e | 2019-08-06 23:55:20 | [diff] [blame] | 6557 | mojo::PendingReceiver<blink::mojom::ColorChooserFactory> receiver) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 6558 | OPTIONAL_TRACE_EVENT0("content", |
| 6559 | "WebContentsImpl::OnColorChooserFactoryReceiver"); |
Julie Jeongeun Kim | 8e2879e | 2019-08-06 23:55:20 | [diff] [blame] | 6560 | color_chooser_factory_receivers_.Add(this, std::move(receiver)); |
Joel Hockey | 85b379d | 2017-12-11 10:42:13 | [diff] [blame] | 6561 | } |
| 6562 | |
Tom Burgin | 732656d3 | 2022-02-07 18:33:26 | [diff] [blame] | 6563 | #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_MAC) |
Joel Hockey | 85b379d | 2017-12-11 10:42:13 | [diff] [blame] | 6564 | void WebContentsImpl::OpenColorChooser( |
Julie Jeongeun Kim | 8e2879e | 2019-08-06 23:55:20 | [diff] [blame] | 6565 | mojo::PendingReceiver<blink::mojom::ColorChooser> chooser_receiver, |
| 6566 | mojo::PendingRemote<blink::mojom::ColorChooserClient> client, |
[email protected] | 8ed1647 | 2014-04-11 19:02:48 | [diff] [blame] | 6567 | SkColor color, |
Joel Hockey | 163835a | 2017-12-20 11:51:57 | [diff] [blame] | 6568 | std::vector<blink::mojom::ColorSuggestionPtr> suggestions) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 6569 | OPTIONAL_TRACE_EVENT0("content", "WebContentsImpl::OpenColorChooser"); |
Bo Liu | 2bceebc | 2021-06-03 15:06:33 | [diff] [blame] | 6570 | // Create `color_chooser_holder_` before calling OpenColorChooser since |
Bo Liu | ab8f997 | 2021-04-08 22:44:36 | [diff] [blame] | 6571 | // OpenColorChooser may callback with results. |
Bo Liu | 2bceebc | 2021-06-03 15:06:33 | [diff] [blame] | 6572 | color_chooser_holder_.reset(); |
| 6573 | color_chooser_holder_ = std::make_unique<ColorChooserHolder>( |
| 6574 | std::move(chooser_receiver), std::move(client)); |
Elly Fong-Jones | 77f78d4 | 2021-01-29 23:56:55 | [diff] [blame] | 6575 | |
Bo Liu | 2bceebc | 2021-06-03 15:06:33 | [diff] [blame] | 6576 | auto new_color_chooser = |
Ivan Kotenkov | 2c0d2bb3 | 2017-11-01 15:41:28 | [diff] [blame] | 6577 | delegate_ ? delegate_->OpenColorChooser(this, color, suggestions) |
Bo Liu | 2bceebc | 2021-06-03 15:06:33 | [diff] [blame] | 6578 | : nullptr; |
| 6579 | if (color_chooser_holder_ && new_color_chooser) { |
| 6580 | color_chooser_holder_->SetChooser(std::move(new_color_chooser)); |
Bo Liu | 5f4bf41 | 2021-06-01 20:09:33 | [diff] [blame] | 6581 | } else if (new_color_chooser) { |
| 6582 | // OpenColorChooser synchronously called back to DidEndColorChooser. |
Bo Liu | 2bceebc | 2021-06-03 15:06:33 | [diff] [blame] | 6583 | DCHECK(!color_chooser_holder_); |
Bo Liu | 5f4bf41 | 2021-06-01 20:09:33 | [diff] [blame] | 6584 | new_color_chooser->End(); |
Bo Liu | 2bceebc | 2021-06-03 15:06:33 | [diff] [blame] | 6585 | } else if (color_chooser_holder_) { |
Bo Liu | 5f4bf41 | 2021-06-01 20:09:33 | [diff] [blame] | 6586 | DCHECK(!new_color_chooser); |
Bo Liu | 2bceebc | 2021-06-03 15:06:33 | [diff] [blame] | 6587 | color_chooser_holder_.reset(); |
Bo Liu | 5f4bf41 | 2021-06-01 20:09:33 | [diff] [blame] | 6588 | } |
[email protected] | da854376 | 2012-03-20 08:52:20 | [diff] [blame] | 6589 | } |
Tom Burgin | 732656d3 | 2022-02-07 18:33:26 | [diff] [blame] | 6590 | #endif // BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_MAC) |
[email protected] | da854376 | 2012-03-20 08:52:20 | [diff] [blame] | 6591 | |
K. Moon | 9ae6ef2c | 2022-08-18 01:30:06 | [diff] [blame] | 6592 | #if BUILDFLAG(ENABLE_PPAPI) |
nick | a0ac838 | 2016-12-15 23:59:23 | [diff] [blame] | 6593 | void WebContentsImpl::OnPepperInstanceCreated(RenderFrameHostImpl* source, |
| 6594 | int32_t pp_instance) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 6595 | OPTIONAL_TRACE_EVENT1("content", "WebContentsImpl::OnPepperInstanceCreated", |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 6596 | "render_frame_host", source); |
Andrew Grieve | e19cd93e | 2021-01-22 05:43:06 | [diff] [blame] | 6597 | observers_.NotifyObservers(&WebContentsObserver::PepperInstanceCreated); |
nick | a0ac838 | 2016-12-15 23:59:23 | [diff] [blame] | 6598 | pepper_playback_observer_->PepperInstanceCreated(source, pp_instance); |
emaxx | e70f5e1 | 2015-05-29 11:26:00 | [diff] [blame] | 6599 | } |
| 6600 | |
nick | a0ac838 | 2016-12-15 23:59:23 | [diff] [blame] | 6601 | void WebContentsImpl::OnPepperInstanceDeleted(RenderFrameHostImpl* source, |
| 6602 | int32_t pp_instance) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 6603 | OPTIONAL_TRACE_EVENT1("content", "WebContentsImpl::OnPepperInstanceDeleted", |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 6604 | "render_frame_host", source); |
Andrew Grieve | e19cd93e | 2021-01-22 05:43:06 | [diff] [blame] | 6605 | observers_.NotifyObservers(&WebContentsObserver::PepperInstanceDeleted); |
nick | a0ac838 | 2016-12-15 23:59:23 | [diff] [blame] | 6606 | pepper_playback_observer_->PepperInstanceDeleted(source, pp_instance); |
emaxx | e70f5e1 | 2015-05-29 11:26:00 | [diff] [blame] | 6607 | } |
| 6608 | |
nick | a0ac838 | 2016-12-15 23:59:23 | [diff] [blame] | 6609 | void WebContentsImpl::OnPepperPluginHung(RenderFrameHostImpl* source, |
| 6610 | int plugin_child_id, |
tommycli | e6633ca7 | 2014-10-31 00:40:42 | [diff] [blame] | 6611 | const base::FilePath& path, |
| 6612 | bool is_hung) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 6613 | OPTIONAL_TRACE_EVENT1("content", "WebContentsImpl::OnPepperPluginHung", |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 6614 | "render_frame_host", source); |
Steven Holte | 9592222 | 2018-09-14 20:06:23 | [diff] [blame] | 6615 | UMA_HISTOGRAM_COUNTS_1M("Pepper.PluginHung", 1); |
tommycli | e6633ca7 | 2014-10-31 00:40:42 | [diff] [blame] | 6616 | |
Andrew Grieve | e19cd93e | 2021-01-22 05:43:06 | [diff] [blame] | 6617 | observers_.NotifyObservers(&WebContentsObserver::PluginHungStatusChanged, |
| 6618 | plugin_child_id, path, is_hung); |
tommycli | e6633ca7 | 2014-10-31 00:40:42 | [diff] [blame] | 6619 | } |
| 6620 | |
nick | a0ac838 | 2016-12-15 23:59:23 | [diff] [blame] | 6621 | void WebContentsImpl::OnPepperStartsPlayback(RenderFrameHostImpl* source, |
| 6622 | int32_t pp_instance) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 6623 | OPTIONAL_TRACE_EVENT1("content", "WebContentsImpl::OnPepperStartsPlayback", |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 6624 | "render_frame_host", source); |
nick | a0ac838 | 2016-12-15 23:59:23 | [diff] [blame] | 6625 | pepper_playback_observer_->PepperStartsPlayback(source, pp_instance); |
zqzhang | 181047e6 | 2016-07-01 13:37:17 | [diff] [blame] | 6626 | } |
| 6627 | |
nick | a0ac838 | 2016-12-15 23:59:23 | [diff] [blame] | 6628 | void WebContentsImpl::OnPepperStopsPlayback(RenderFrameHostImpl* source, |
| 6629 | int32_t pp_instance) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 6630 | OPTIONAL_TRACE_EVENT1("content", "WebContentsImpl::OnPepperStopsPlayback", |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 6631 | "render_frame_host", source); |
nick | a0ac838 | 2016-12-15 23:59:23 | [diff] [blame] | 6632 | pepper_playback_observer_->PepperStopsPlayback(source, pp_instance); |
zqzhang | 181047e6 | 2016-07-01 13:37:17 | [diff] [blame] | 6633 | } |
| 6634 | |
Dave Tapuska | 993dae35 | 2021-01-08 17:40:59 | [diff] [blame] | 6635 | void WebContentsImpl::OnPepperPluginCrashed(RenderFrameHostImpl* source, |
| 6636 | const base::FilePath& plugin_path, |
| 6637 | base::ProcessId plugin_pid) { |
| 6638 | OPTIONAL_TRACE_EVENT1("content", "WebContentsImpl::OnPepperPluginCrashed", |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 6639 | "render_frame_host", source); |
nick | a0ac838 | 2016-12-15 23:59:23 | [diff] [blame] | 6640 | // TODO(nick): Eliminate the |plugin_pid| parameter, which can't be trusted, |
Gyuyoung Kim | 26c7bc9 | 2020-04-29 00:53:00 | [diff] [blame] | 6641 | // and is only used by WebTestControlHost. |
Andrew Grieve | e19cd93e | 2021-01-22 05:43:06 | [diff] [blame] | 6642 | observers_.NotifyObservers(&WebContentsObserver::PluginCrashed, plugin_path, |
| 6643 | plugin_pid); |
tommycli | e6633ca7 | 2014-10-31 00:40:42 | [diff] [blame] | 6644 | } |
| 6645 | |
K. Moon | 9ae6ef2c | 2022-08-18 01:30:06 | [diff] [blame] | 6646 | #endif // BUILDFLAG(ENABLE_PPAPI) |
[email protected] | 19be7a6 | 2012-10-01 23:03:37 | [diff] [blame] | 6647 | |
Miyoung Shin | 5709ab0 | 2020-02-19 06:50:41 | [diff] [blame] | 6648 | void WebContentsImpl::UpdateFaviconURL( |
Kevin McNee | fb86fcf | 2021-02-26 23:20:57 | [diff] [blame] | 6649 | RenderFrameHostImpl* source, |
Ian Vollick | 6d75ac3 | 2021-05-05 17:45:09 | [diff] [blame] | 6650 | const std::vector<blink::mojom::FaviconURLPtr>& candidates) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 6651 | OPTIONAL_TRACE_EVENT1("content", "WebContentsImpl::UpdateFaviconURL", |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 6652 | "render_frame_host", source); |
pkotwicz | f9bc2059 | 2015-04-21 18:57:06 | [diff] [blame] | 6653 | // We get updated favicon URLs after the page stops loading. If a cross-site |
| 6654 | // navigation occurs while a page is still loading, the initial page |
| 6655 | // may stop loading and send us updated favicon URLs after the navigation |
| 6656 | // for the new page has committed. |
Julie Jeongeun Kim | 0a594bb | 2021-10-21 05:30:53 | [diff] [blame] | 6657 | if (!source->IsInPrimaryMainFrame()) |
pkotwicz | f9bc2059 | 2015-04-21 18:57:06 | [diff] [blame] | 6658 | return; |
| 6659 | |
Andrew Grieve | e19cd93e | 2021-01-22 05:43:06 | [diff] [blame] | 6660 | observers_.NotifyObservers(&WebContentsObserver::DidUpdateFaviconURL, source, |
Ian Vollick | 6d75ac3 | 2021-05-05 17:45:09 | [diff] [blame] | 6661 | candidates); |
[email protected] | 795c2897 | 2012-12-06 06:13:39 | [diff] [blame] | 6662 | } |
| 6663 | |
qinmin | 72e8bd0 | 2016-10-21 19:35:37 | [diff] [blame] | 6664 | void WebContentsImpl::SetIsOverlayContent(bool is_overlay_content) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 6665 | OPTIONAL_TRACE_EVENT1("content", "WebContentsImpl::SetIsOverlayContent", |
| 6666 | "is_overlay_content", is_overlay_content); |
qinmin | 72e8bd0 | 2016-10-21 19:35:37 | [diff] [blame] | 6667 | is_overlay_content_ = is_overlay_content; |
| 6668 | } |
| 6669 | |
Jeremy Roman | 2d8dfe13 | 2021-07-06 20:51:26 | [diff] [blame] | 6670 | void WebContentsImpl::OnFirstVisuallyNonEmptyPaint(PageImpl& page) { |
| 6671 | OPTIONAL_TRACE_EVENT1( |
| 6672 | "content", "WebContentsImpl::OnFirstVisuallyNonEmptyPaint", "page", page); |
| 6673 | if (!page.IsPrimary()) |
| 6674 | return; |
| 6675 | |
Clark DuVall | 22aaff3 | 2021-03-18 17:17:47 | [diff] [blame] | 6676 | { |
| 6677 | SCOPED_UMA_HISTOGRAM_TIMER( |
| 6678 | "WebContentsObserver.DidFirstVisuallyNonEmptyPaint"); |
| 6679 | observers_.NotifyObservers( |
| 6680 | &WebContentsObserver::DidFirstVisuallyNonEmptyPaint); |
| 6681 | } |
Jeremy Roman | 2d8dfe13 | 2021-07-06 20:51:26 | [diff] [blame] | 6682 | if (page.theme_color() != last_sent_theme_color_) { |
yusufo | d41c5f9 | 2015-03-06 00:14:28 | [diff] [blame] | 6683 | // Theme color should have updated by now if there was one. |
Andrew Grieve | e19cd93e | 2021-01-22 05:43:06 | [diff] [blame] | 6684 | observers_.NotifyObservers(&WebContentsObserver::DidChangeThemeColor); |
Jeremy Roman | 2d8dfe13 | 2021-07-06 20:51:26 | [diff] [blame] | 6685 | last_sent_theme_color_ = GetPrimaryPage().theme_color(); |
yusufo | d41c5f9 | 2015-03-06 00:14:28 | [diff] [blame] | 6686 | } |
Alan Cutter | d73a15d9 | 2020-08-21 07:12:45 | [diff] [blame] | 6687 | |
Jeremy Roman | 2d8dfe13 | 2021-07-06 20:51:26 | [diff] [blame] | 6688 | if (page.background_color() != last_sent_background_color_) { |
Alan Cutter | d73a15d9 | 2020-08-21 07:12:45 | [diff] [blame] | 6689 | // Background color should have updated by now if there was one. |
Andrew Grieve | e19cd93e | 2021-01-22 05:43:06 | [diff] [blame] | 6690 | observers_.NotifyObservers(&WebContentsObserver::OnBackgroundColorChanged); |
Jeremy Roman | 2d8dfe13 | 2021-07-06 20:51:26 | [diff] [blame] | 6691 | last_sent_background_color_ = page.background_color(); |
Alan Cutter | d73a15d9 | 2020-08-21 07:12:45 | [diff] [blame] | 6692 | } |
[email protected] | 9f26807 | 2013-11-07 00:02:15 | [diff] [blame] | 6693 | } |
[email protected] | d9030b8 | 2013-07-19 08:26:06 | [diff] [blame] | 6694 | |
Charlie Reis | 3d18960 | 2021-04-27 21:24:47 | [diff] [blame] | 6695 | bool WebContentsImpl::IsGuest() { |
| 6696 | return !!browser_plugin_guest_; |
| 6697 | } |
| 6698 | |
Kevin McNee | 068c3eb | 2020-04-03 18:24:19 | [diff] [blame] | 6699 | bool WebContentsImpl::IsPortal() { |
Adithya Srinivasan | 46b8a79 | 2019-02-01 14:47:23 | [diff] [blame] | 6700 | return portal(); |
| 6701 | } |
| 6702 | |
Kevin McNee | 94ea52f5 | 2020-06-23 17:42:06 | [diff] [blame] | 6703 | WebContentsImpl* WebContentsImpl::GetPortalHostWebContents() { |
| 6704 | return portal() ? portal()->GetPortalHostContents() : nullptr; |
| 6705 | } |
| 6706 | |
[email protected] | 17e286e | 2013-03-01 23:29:39 | [diff] [blame] | 6707 | void WebContentsImpl::NotifyBeforeFormRepostWarningShow() { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 6708 | OPTIONAL_TRACE_EVENT0("content", |
| 6709 | "WebContentsImpl::NotifyBeforeFormRepostWarningShow"); |
Andrew Grieve | e19cd93e | 2021-01-22 05:43:06 | [diff] [blame] | 6710 | observers_.NotifyObservers(&WebContentsObserver::BeforeFormRepostWarningShow); |
[email protected] | 17e286e | 2013-03-01 23:29:39 | [diff] [blame] | 6711 | } |
| 6712 | |
[email protected] | ec6c05f | 2013-10-23 18:41:57 | [diff] [blame] | 6713 | void WebContentsImpl::ActivateAndShowRepostFormWarningDialog() { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 6714 | OPTIONAL_TRACE_EVENT0( |
| 6715 | "content", "WebContentsImpl::ActivateAndShowRepostFormWarningDialog"); |
[email protected] | ec6c05f | 2013-10-23 18:41:57 | [diff] [blame] | 6716 | Activate(); |
| 6717 | if (delegate_) |
| 6718 | delegate_->ShowRepostFormWarningDialog(this); |
| 6719 | } |
| 6720 | |
[email protected] | b4c8401 | 2014-04-28 19:51:10 | [diff] [blame] | 6721 | bool WebContentsImpl::HasAccessedInitialDocument() { |
Carlos Caballero | 6a99dac | 2021-11-03 10:41:17 | [diff] [blame] | 6722 | return GetPrimaryFrameTree().has_accessed_initial_main_document(); |
[email protected] | b4c8401 | 2014-04-28 19:51:10 | [diff] [blame] | 6723 | } |
| 6724 | |
afakhry | 6f0c1ec2 | 2016-07-14 13:55:13 | [diff] [blame] | 6725 | void WebContentsImpl::UpdateTitleForEntry(NavigationEntry* entry, |
Jan Wilken Dörrie | aace0cfef | 2021-03-11 22:01:58 | [diff] [blame] | 6726 | const std::u16string& title) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 6727 | OPTIONAL_TRACE_EVENT1("content", "WebContentsImpl::UpdateTitleForEntry", |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 6728 | "title", title); |
Adithya Srinivasan | 9b0c99c | 2021-08-10 15:19:45 | [diff] [blame] | 6729 | NavigationEntryImpl* entry_impl = |
| 6730 | NavigationEntryImpl::FromNavigationEntry(entry); |
| 6731 | bool title_changed = UpdateTitleForEntryImpl(entry_impl, title); |
| 6732 | if (title_changed) |
| 6733 | NotifyTitleUpdateForEntry(entry_impl); |
| 6734 | } |
| 6735 | |
| 6736 | bool WebContentsImpl::UpdateTitleForEntryImpl(NavigationEntryImpl* entry, |
| 6737 | const std::u16string& title) { |
Rakina Zata Amni | 46087a1 | 2022-11-11 08:28:38 | [diff] [blame] | 6738 | DCHECK(entry); |
Jan Wilken Dörrie | aace0cfef | 2021-03-11 22:01:58 | [diff] [blame] | 6739 | std::u16string final_title; |
Avi Drissman | 9300221 | 2017-09-27 03:20:52 | [diff] [blame] | 6740 | base::TrimWhitespace(title, base::TRIM_ALL, &final_title); |
[email protected] | 420ae01 | 2009-04-24 05:16:32 | [diff] [blame] | 6741 | |
Rakina Zata Amni | 46087a1 | 2022-11-11 08:28:38 | [diff] [blame] | 6742 | if (final_title == entry->GetTitle()) |
| 6743 | return false; // Nothing changed, don't bother. |
[email protected] | 420ae01 | 2009-04-24 05:16:32 | [diff] [blame] | 6744 | |
Rakina Zata Amni | 46087a1 | 2022-11-11 08:28:38 | [diff] [blame] | 6745 | entry->SetTitle(final_title); |
| 6746 | // The title for display may differ from the title just set; grab it. |
| 6747 | final_title = entry->GetTitleForDisplay(); |
Avi Drissman | 9300221 | 2017-09-27 03:20:52 | [diff] [blame] | 6748 | |
Adithya Srinivasan | 9b0c99c | 2021-08-10 15:19:45 | [diff] [blame] | 6749 | return true; |
| 6750 | } |
[email protected] | 420ae01 | 2009-04-24 05:16:32 | [diff] [blame] | 6751 | |
Adithya Srinivasan | 9b0c99c | 2021-08-10 15:19:45 | [diff] [blame] | 6752 | void WebContentsImpl::NotifyTitleUpdateForEntry(NavigationEntryImpl* entry) { |
Rakina Zata Amni | 46087a1 | 2022-11-11 08:28:38 | [diff] [blame] | 6753 | // |entry| must belong to the primary frame tree's NavigationController. |
| 6754 | DCHECK(GetController().GetEntryWithUniqueIDIncludingPending( |
| 6755 | entry->GetUniqueID())); |
| 6756 | std::u16string final_title = entry->GetTitleForDisplay(); |
Adithya Srinivasan | 9b0c99c | 2021-08-10 15:19:45 | [diff] [blame] | 6757 | bool did_web_contents_title_change = entry == GetNavigationEntryForTitle(); |
| 6758 | if (did_web_contents_title_change) |
| 6759 | view_->SetPageTitle(final_title); |
[email protected] | 420ae01 | 2009-04-24 05:16:32 | [diff] [blame] | 6760 | |
Clark DuVall | 22aaff3 | 2021-03-18 17:17:47 | [diff] [blame] | 6761 | { |
| 6762 | SCOPED_UMA_HISTOGRAM_TIMER("WebContentsObserver.TitleWasSet"); |
| 6763 | observers_.NotifyObservers(&WebContentsObserver::TitleWasSet, entry); |
| 6764 | } |
Adithya Srinivasan | 9b0c99c | 2021-08-10 15:19:45 | [diff] [blame] | 6765 | |
| 6766 | if (did_web_contents_title_change) |
afakhry | 6f0c1ec2 | 2016-07-14 13:55:13 | [diff] [blame] | 6767 | NotifyNavigationStateChanged(INVALIDATE_TYPE_TITLE); |
[email protected] | 420ae01 | 2009-04-24 05:16:32 | [diff] [blame] | 6768 | } |
| 6769 | |
Adithya Srinivasan | 9b0c99c | 2021-08-10 15:19:45 | [diff] [blame] | 6770 | NavigationEntry* WebContentsImpl::GetNavigationEntryForTitle() { |
| 6771 | // We use the title for the last committed entry rather than a pending |
| 6772 | // navigation entry. For example, when the user types in a URL, we want to |
| 6773 | // keep the old page's title until the new load has committed and we get a new |
| 6774 | // title. |
| 6775 | NavigationEntry* entry = GetController().GetLastCommittedEntry(); |
| 6776 | |
| 6777 | // We make an exception for initial navigations. We only want to use the title |
| 6778 | // from the visible entry if: |
| 6779 | // 1. The pending entry has been explicitly assigned a title to display. |
| 6780 | // 2. The user is doing a history navigation in a new tab (e.g., Ctrl+Back), |
| 6781 | // which case there is a pending entry index other than -1. |
| 6782 | // |
| 6783 | // Otherwise, we want to stick with the last committed entry's title during |
| 6784 | // new navigations, which have pending entries at index -1 with no title. |
| 6785 | if (GetController().IsInitialNavigation() && |
| 6786 | ((GetController().GetVisibleEntry() && |
| 6787 | !GetController().GetVisibleEntry()->GetTitle().empty()) || |
| 6788 | GetController().GetPendingEntryIndex() != -1)) { |
| 6789 | entry = GetController().GetVisibleEntry(); |
| 6790 | } |
| 6791 | |
| 6792 | return entry; |
| 6793 | } |
| 6794 | |
fdegans | a696e511 | 2015-04-17 01:57:59 | [diff] [blame] | 6795 | void WebContentsImpl::SendChangeLoadProgress() { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 6796 | OPTIONAL_TRACE_EVENT1("content", "WebContentsImpl::SendChangeLoadProgress", |
Sreeja Kamishetty | 0be3b1b | 2021-08-12 17:04:15 | [diff] [blame] | 6797 | "load_progress", GetLoadProgress()); |
[email protected] | 960b037 | 2014-05-19 18:01:00 | [diff] [blame] | 6798 | loading_last_progress_update_ = base::TimeTicks::Now(); |
Clark DuVall | 22aaff3 | 2021-03-18 17:17:47 | [diff] [blame] | 6799 | |
| 6800 | SCOPED_UMA_HISTOGRAM_TIMER("WebContentsObserver.LoadProgressChanged"); |
Andrew Grieve | e19cd93e | 2021-01-22 05:43:06 | [diff] [blame] | 6801 | observers_.NotifyObservers(&WebContentsObserver::LoadProgressChanged, |
Sreeja Kamishetty | 0be3b1b | 2021-08-12 17:04:15 | [diff] [blame] | 6802 | GetLoadProgress()); |
[email protected] | 960b037 | 2014-05-19 18:01:00 | [diff] [blame] | 6803 | } |
| 6804 | |
| 6805 | void WebContentsImpl::ResetLoadProgressState() { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 6806 | OPTIONAL_TRACE_EVENT0("content", "WebContentsImpl::ResetLoadProgressState"); |
Sreeja Kamishetty | 0be3b1b | 2021-08-12 17:04:15 | [diff] [blame] | 6807 | GetPrimaryPage().set_load_progress(0.0); |
[email protected] | 960b037 | 2014-05-19 18:01:00 | [diff] [blame] | 6808 | loading_weak_factory_.InvalidateWeakPtrs(); |
| 6809 | loading_last_progress_update_ = base::TimeTicks(); |
| 6810 | } |
| 6811 | |
clamy | 44e84ce | 2016-02-22 15:38:25 | [diff] [blame] | 6812 | // Notifies the RenderWidgetHost instance about the fact that the page is |
| 6813 | // loading, or done loading. |
Nate Chapin | 9aabf5f | 2021-11-12 00:31:19 | [diff] [blame] | 6814 | void WebContentsImpl::LoadingStateChanged(bool should_show_loading_ui, |
clamy | 44e84ce | 2016-02-22 15:38:25 | [diff] [blame] | 6815 | LoadNotificationDetails* details) { |
Takashi Toyoshima | ea534ef2 | 2021-07-21 03:27:59 | [diff] [blame] | 6816 | if (IsBeingDestroyed()) |
Nate Chapin | 22ea659 | 2019-03-05 22:29:02 | [diff] [blame] | 6817 | return; |
clamy | 44e84ce | 2016-02-22 15:38:25 | [diff] [blame] | 6818 | |
| 6819 | bool is_loading = IsLoading(); |
| 6820 | |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 6821 | OPTIONAL_TRACE_EVENT1("content", "WebContentsImpl::LoadingStateChanged", |
| 6822 | "is_loading", is_loading); |
| 6823 | |
clamy | 44e84ce | 2016-02-22 15:38:25 | [diff] [blame] | 6824 | if (!is_loading) { |
Jan Wilken Dörrie | aace0cfef | 2021-03-11 22:01:58 | [diff] [blame] | 6825 | load_state_ = |
| 6826 | net::LoadStateWithParam(net::LOAD_STATE_IDLE, std::u16string()); |
clamy | 44e84ce | 2016-02-22 15:38:25 | [diff] [blame] | 6827 | load_state_host_.clear(); |
| 6828 | upload_size_ = 0; |
| 6829 | upload_position_ = 0; |
| 6830 | } |
| 6831 | |
Nate Chapin | 9aabf5f | 2021-11-12 00:31:19 | [diff] [blame] | 6832 | should_show_loading_ui_ = should_show_loading_ui; |
clamy | 44e84ce | 2016-02-22 15:38:25 | [diff] [blame] | 6833 | |
| 6834 | if (delegate_) |
Nate Chapin | 9aabf5f | 2021-11-12 00:31:19 | [diff] [blame] | 6835 | delegate_->LoadingStateChanged(this, should_show_loading_ui_); |
clamy | 44e84ce | 2016-02-22 15:38:25 | [diff] [blame] | 6836 | NotifyNavigationStateChanged(INVALIDATE_TYPE_LOAD); |
| 6837 | |
| 6838 | std::string url = (details ? details->url.possibly_invalid_spec() : "NULL"); |
| 6839 | if (is_loading) { |
Dan Elphick | 3fb6ca1 | 2020-10-26 18:15:48 | [diff] [blame] | 6840 | TRACE_EVENT_NESTABLE_ASYNC_BEGIN2( |
| 6841 | "browser,navigation", "WebContentsImpl Loading", this, "URL", url, |
Carlos Caballero | 6a99dac | 2021-11-03 10:41:17 | [diff] [blame] | 6842 | "Primary Main FrameTreeNode id", |
| 6843 | GetPrimaryFrameTree().root()->frame_tree_node_id()); |
Clark DuVall | 22aaff3 | 2021-03-18 17:17:47 | [diff] [blame] | 6844 | SCOPED_UMA_HISTOGRAM_TIMER("WebContentsObserver.DidStartLoading"); |
Andrew Grieve | e19cd93e | 2021-01-22 05:43:06 | [diff] [blame] | 6845 | observers_.NotifyObservers(&WebContentsObserver::DidStartLoading); |
clamy | 44e84ce | 2016-02-22 15:38:25 | [diff] [blame] | 6846 | } else { |
Dan Elphick | 3fb6ca1 | 2020-10-26 18:15:48 | [diff] [blame] | 6847 | TRACE_EVENT_NESTABLE_ASYNC_END1( |
| 6848 | "browser,navigation", "WebContentsImpl Loading", this, "URL", url); |
Clark DuVall | 22aaff3 | 2021-03-18 17:17:47 | [diff] [blame] | 6849 | SCOPED_UMA_HISTOGRAM_TIMER("WebContentsObserver.DidStopLoading"); |
Andrew Grieve | e19cd93e | 2021-01-22 05:43:06 | [diff] [blame] | 6850 | observers_.NotifyObservers(&WebContentsObserver::DidStopLoading); |
clamy | 44e84ce | 2016-02-22 15:38:25 | [diff] [blame] | 6851 | } |
| 6852 | |
| 6853 | // TODO(avi): Remove. http://crbug.com/170921 |
| 6854 | int type = is_loading ? NOTIFICATION_LOAD_START : NOTIFICATION_LOAD_STOP; |
| 6855 | NotificationDetails det = NotificationService::NoDetails(); |
| 6856 | if (details) |
Ian Vollick | 6d75ac3 | 2021-05-05 17:45:09 | [diff] [blame] | 6857 | det = Details<LoadNotificationDetails>(details); |
clamy | 44e84ce | 2016-02-22 15:38:25 | [diff] [blame] | 6858 | NotificationService::current()->Notify( |
Carlos Caballero | 40b0efd | 2021-01-26 11:55:00 | [diff] [blame] | 6859 | type, Source<NavigationController>(&GetController()), det); |
clamy | 44e84ce | 2016-02-22 15:38:25 | [diff] [blame] | 6860 | } |
| 6861 | |
danakj | a0bce4c95 | 2020-05-12 20:40:28 | [diff] [blame] | 6862 | void WebContentsImpl::NotifyViewSwapped(RenderViewHost* old_view, |
| 6863 | RenderViewHost* new_view) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 6864 | OPTIONAL_TRACE_EVENT2("content", "WebContentsImpl::NotifyViewSwapped", |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 6865 | "old_view", old_view, "new_view", new_view); |
danakj | a0bce4c95 | 2020-05-12 20:40:28 | [diff] [blame] | 6866 | DCHECK_NE(old_view, new_view); |
[email protected] | 420ae01 | 2009-04-24 05:16:32 | [diff] [blame] | 6867 | // After sending out a swap notification, we need to send a disconnect |
| 6868 | // notification so that clients that pick up a pointer to |this| can NULL the |
| 6869 | // pointer. See Bug 1230284. |
| 6870 | notify_disconnection_ = true; |
Nasko Oskov | 82dbf01 | 2023-01-10 22:40:01 | [diff] [blame] | 6871 | observers_.NotifyObservers(&WebContentsObserver::RenderViewHostChanged, |
| 6872 | old_view, new_view); |
danakj | a0bce4c95 | 2020-05-12 20:40:28 | [diff] [blame] | 6873 | view_->RenderViewHostChanged(old_view, new_view); |
Jinsuk Kim | dc5284c2 | 2018-07-11 02:22:06 | [diff] [blame] | 6874 | |
Lucas Furukawa Gadani | f9ea4fc | 2018-08-03 23:57:13 | [diff] [blame] | 6875 | // If this is an inner WebContents that has swapped views, we need to reattach |
| 6876 | // it to its outer WebContents. |
| 6877 | if (node_.outer_web_contents()) |
| 6878 | ReattachToOuterWebContentsFrame(); |
| 6879 | |
[email protected] | 7a846df | 2012-09-20 19:17:39 | [diff] [blame] | 6880 | // Ensure that the associated embedder gets cleared after a RenderViewHost |
| 6881 | // gets swapped, so we don't reuse the same embedder next time a |
| 6882 | // RenderViewHost is attached to this WebContents. |
| 6883 | RemoveBrowserPluginEmbedder(); |
[email protected] | 420ae01 | 2009-04-24 05:16:32 | [diff] [blame] | 6884 | } |
| 6885 | |
Kevin McNee | fb86fcf | 2021-02-26 23:20:57 | [diff] [blame] | 6886 | void WebContentsImpl::NotifyFrameSwapped(RenderFrameHostImpl* old_frame, |
Dave Tapuska | e45d6fd | 2021-09-29 17:03:59 | [diff] [blame] | 6887 | RenderFrameHostImpl* new_frame) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 6888 | TRACE_EVENT2("content", "WebContentsImpl::NotifyFrameSwapped", "old_frame", |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 6889 | old_frame, "new_frame", new_frame); |
Xiaohan Wang | 24ec934 | 2022-01-15 17:34:22 | [diff] [blame] | 6890 | #if BUILDFLAG(IS_ANDROID) |
danakj | a0bce4c95 | 2020-05-12 20:40:28 | [diff] [blame] | 6891 | // Copy importance from |old_frame| if |new_frame| is a main frame. |
| 6892 | if (old_frame && !new_frame->GetParent()) { |
Kevin McNee | fb86fcf | 2021-02-26 23:20:57 | [diff] [blame] | 6893 | RenderWidgetHostImpl* old_widget = old_frame->GetRenderWidgetHost(); |
| 6894 | RenderWidgetHostImpl* new_widget = new_frame->GetRenderWidgetHost(); |
danakj | a0bce4c95 | 2020-05-12 20:40:28 | [diff] [blame] | 6895 | new_widget->SetImportance(old_widget->importance()); |
Bo Liu | 7c6779e9 | 2017-08-16 02:02:28 | [diff] [blame] | 6896 | } |
Bo Liu | 168c864 | 2017-08-28 18:26:02 | [diff] [blame] | 6897 | #endif |
Andrew Grieve | e19cd93e | 2021-01-22 05:43:06 | [diff] [blame] | 6898 | observers_.NotifyObservers(&WebContentsObserver::RenderFrameHostChanged, |
| 6899 | old_frame, new_frame); |
[email protected] | 02d7b6e | 2014-06-24 21:01:50 | [diff] [blame] | 6900 | } |
| 6901 | |
[email protected] | cbb1ef59 | 2013-06-05 19:49:46 | [diff] [blame] | 6902 | void WebContentsImpl::NotifyNavigationEntryCommitted( |
| 6903 | const LoadCommittedDetails& load_details) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 6904 | OPTIONAL_TRACE_EVENT0("content", |
| 6905 | "WebContentsImpl::NotifyNavigationEntryCommitted"); |
Clark DuVall | 22aaff3 | 2021-03-18 17:17:47 | [diff] [blame] | 6906 | SCOPED_UMA_HISTOGRAM_TIMER("WebContentsObserver.NavigationEntryCommitted"); |
Andrew Grieve | e19cd93e | 2021-01-22 05:43:06 | [diff] [blame] | 6907 | observers_.NotifyObservers(&WebContentsObserver::NavigationEntryCommitted, |
| 6908 | load_details); |
[email protected] | cbb1ef59 | 2013-06-05 19:49:46 | [diff] [blame] | 6909 | } |
| 6910 | |
Sam McNally | 5c087a3 | 2017-08-25 01:46:14 | [diff] [blame] | 6911 | void WebContentsImpl::NotifyNavigationEntryChanged( |
| 6912 | const EntryChangedDetails& change_details) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 6913 | OPTIONAL_TRACE_EVENT0("content", |
| 6914 | "WebContentsImpl::NotifyNavigationEntryChanged"); |
Clark DuVall | 22aaff3 | 2021-03-18 17:17:47 | [diff] [blame] | 6915 | SCOPED_UMA_HISTOGRAM_TIMER("WebContentsObserver.NavigationEntryChanged"); |
Andrew Grieve | e19cd93e | 2021-01-22 05:43:06 | [diff] [blame] | 6916 | observers_.NotifyObservers(&WebContentsObserver::NavigationEntryChanged, |
| 6917 | change_details); |
Sam McNally | 5c087a3 | 2017-08-25 01:46:14 | [diff] [blame] | 6918 | } |
| 6919 | |
| 6920 | void WebContentsImpl::NotifyNavigationListPruned( |
| 6921 | const PrunedDetails& pruned_details) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 6922 | OPTIONAL_TRACE_EVENT0("content", |
| 6923 | "WebContentsImpl::NotifyNavigationListPruned"); |
Andrew Grieve | e19cd93e | 2021-01-22 05:43:06 | [diff] [blame] | 6924 | observers_.NotifyObservers(&WebContentsObserver::NavigationListPruned, |
| 6925 | pruned_details); |
Sam McNally | 5c087a3 | 2017-08-25 01:46:14 | [diff] [blame] | 6926 | } |
| 6927 | |
Christian Dullweber | 1af31e6 | 2018-02-22 11:49:48 | [diff] [blame] | 6928 | void WebContentsImpl::NotifyNavigationEntriesDeleted() { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 6929 | OPTIONAL_TRACE_EVENT0("content", |
| 6930 | "WebContentsImpl::NotifyNavigationEntriesDeleted"); |
Clark DuVall | 22aaff3 | 2021-03-18 17:17:47 | [diff] [blame] | 6931 | SCOPED_UMA_HISTOGRAM_TIMER("WebContentsObserver.NavigationEntryDeleted"); |
Andrew Grieve | e19cd93e | 2021-01-22 05:43:06 | [diff] [blame] | 6932 | observers_.NotifyObservers(&WebContentsObserver::NavigationEntriesDeleted); |
Christian Dullweber | 1af31e6 | 2018-02-22 11:49:48 | [diff] [blame] | 6933 | } |
| 6934 | |
Varun Khaneja | 5da9430 | 2019-06-25 04:48:10 | [diff] [blame] | 6935 | void WebContentsImpl::OnDidBlockNavigation( |
| 6936 | const GURL& blocked_url, |
| 6937 | const GURL& initiator_url, |
Abhijeet Kandalkar | de7348e | 2020-01-13 06:06:54 | [diff] [blame] | 6938 | blink::mojom::NavigationBlockedReason reason) { |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 6939 | OPTIONAL_TRACE_EVENT1("content", "WebContentsImpl::OnDidBlockNavigation", |
| 6940 | "details", [&](perfetto::TracedValue context) { |
| 6941 | // TODO(crbug.com/1183371): Replace this with passing |
| 6942 | // more parameters to TRACE_EVENT directly when |
| 6943 | // available. |
| 6944 | auto dict = std::move(context).WriteDictionary(); |
| 6945 | dict.Add("blocked_url", blocked_url); |
| 6946 | dict.Add("initiator_url", initiator_url); |
| 6947 | dict.Add("reason", reason); |
| 6948 | }); |
Nicolas Dossou-gbete | bcab247 | 2017-09-09 12:28:40 | [diff] [blame] | 6949 | if (delegate_) |
Varun Khaneja | 5da9430 | 2019-06-25 04:48:10 | [diff] [blame] | 6950 | delegate_->OnDidBlockNavigation(this, blocked_url, initiator_url, reason); |
Nicolas Dossou-gbete | bcab247 | 2017-09-09 12:28:40 | [diff] [blame] | 6951 | } |
| 6952 | |
Kevin McNee | fb86fcf | 2021-02-26 23:20:57 | [diff] [blame] | 6953 | void WebContentsImpl::RenderFrameCreated( |
| 6954 | RenderFrameHostImpl* render_frame_host) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 6955 | TRACE_EVENT1("content", "WebContentsImpl::RenderFrameCreated", |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 6956 | "render_frame_host", render_frame_host); |
danakj | cdab6ed5 | 2021-02-10 23:44:13 | [diff] [blame] | 6957 | |
Kevin McNee | 9f3ef7c | 2021-09-29 18:43:41 | [diff] [blame] | 6958 | if (render_frame_host->IsInPrimaryMainFrame()) { |
Khushal | c5eaf22 | 2021-06-30 20:15:48 | [diff] [blame] | 6959 | NotifyPrimaryMainFrameProcessIsAlive(); |
danakj | cdab6ed5 | 2021-02-10 23:44:13 | [diff] [blame] | 6960 | } |
| 6961 | |
Clark DuVall | 22aaff3 | 2021-03-18 17:17:47 | [diff] [blame] | 6962 | { |
| 6963 | SCOPED_UMA_HISTOGRAM_TIMER("WebContentsObserver.RenderFrameCreated"); |
| 6964 | observers_.NotifyObservers(&WebContentsObserver::RenderFrameCreated, |
| 6965 | render_frame_host); |
| 6966 | } |
Kevin McNee | fb86fcf | 2021-02-26 23:20:57 | [diff] [blame] | 6967 | render_frame_host->UpdateAccessibilityMode(); |
clamy | 91b7abf | 2016-03-24 15:17:45 | [diff] [blame] | 6968 | |
Becca Hughes | d11d650 | 2018-07-31 17:01:28 | [diff] [blame] | 6969 | if (display_cutout_host_impl_) |
| 6970 | display_cutout_host_impl_->RenderFrameCreated(render_frame_host); |
[email protected] | b849847b | 2013-12-10 21:57:58 | [diff] [blame] | 6971 | } |
| 6972 | |
Kevin McNee | fb86fcf | 2021-02-26 23:20:57 | [diff] [blame] | 6973 | void WebContentsImpl::RenderFrameDeleted( |
| 6974 | RenderFrameHostImpl* render_frame_host) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 6975 | TRACE_EVENT1("content", "WebContentsImpl::RenderFrameDeleted", |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 6976 | "render_frame_host", render_frame_host); |
Clark DuVall | 22aaff3 | 2021-03-18 17:17:47 | [diff] [blame] | 6977 | { |
| 6978 | SCOPED_UMA_HISTOGRAM_TIMER("WebContentsObserver.RenderFrameDeleted"); |
| 6979 | observers_.NotifyObservers(&WebContentsObserver::RenderFrameDeleted, |
| 6980 | render_frame_host); |
| 6981 | } |
K. Moon | 9ae6ef2c | 2022-08-18 01:30:06 | [diff] [blame] | 6982 | #if BUILDFLAG(ENABLE_PPAPI) |
zqzhang | 7b6f4f0 | 2016-12-07 22:10:22 | [diff] [blame] | 6983 | pepper_playback_observer_->RenderFrameDeleted(render_frame_host); |
| 6984 | #endif |
Becca Hughes | fd5d8f89 | 2018-06-14 18:23:36 | [diff] [blame] | 6985 | |
Becca Hughes | d11d650 | 2018-07-31 17:01:28 | [diff] [blame] | 6986 | if (display_cutout_host_impl_) |
| 6987 | display_cutout_host_impl_->RenderFrameDeleted(render_frame_host); |
| 6988 | |
Becca Hughes | fd5d8f89 | 2018-06-14 18:23:36 | [diff] [blame] | 6989 | // Remove any fullscreen state that the frame has stored. |
Batalov Vladislav | e9a5f382 | 2020-12-02 07:31:11 | [diff] [blame] | 6990 | FullscreenStateChanged(render_frame_host, false /* is_fullscreen */, |
| 6991 | blink::mojom::FullscreenOptionsPtr()); |
[email protected] | b849847b | 2013-12-10 21:57:58 | [diff] [blame] | 6992 | } |
| 6993 | |
Gyuyoung Kim | 1bc1ba8 | 2021-02-08 23:32:44 | [diff] [blame] | 6994 | void WebContentsImpl::ShowContextMenu( |
Lukasz Anforowicz | 49e30ea0 | 2021-10-28 20:31:56 | [diff] [blame] | 6995 | RenderFrameHost& render_frame_host, |
Gyuyoung Kim | 1bc1ba8 | 2021-02-08 23:32:44 | [diff] [blame] | 6996 | mojo::PendingAssociatedRemote<blink::mojom::ContextMenuClient> |
| 6997 | context_menu_client, |
| 6998 | const ContextMenuParams& params) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 6999 | OPTIONAL_TRACE_EVENT1("content", "WebContentsImpl::ShowContextMenu", |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 7000 | "render_frame_host", render_frame_host); |
avi | e9fd872 | 2017-04-10 16:17:58 | [diff] [blame] | 7001 | // If a renderer fires off a second command to show a context menu before the |
| 7002 | // first context menu is closed, just ignore it. https://crbug.com/707534 |
ekaramad | f6750aa | 2017-06-06 18:29:42 | [diff] [blame] | 7003 | if (showing_context_menu_) |
avi | e9fd872 | 2017-04-10 16:17:58 | [diff] [blame] | 7004 | return; |
| 7005 | |
Gyuyoung Kim | 1bc1ba8 | 2021-02-08 23:32:44 | [diff] [blame] | 7006 | if (context_menu_client) { |
| 7007 | context_menu_client_.reset(); |
| 7008 | context_menu_client_.Bind(std::move(context_menu_client)); |
| 7009 | } |
| 7010 | |
[email protected] | 077e704b | 2014-05-23 19:24:10 | [diff] [blame] | 7011 | ContextMenuParams context_menu_params(params); |
[email protected] | a09d53ce | 2014-01-31 00:46:42 | [diff] [blame] | 7012 | // Allow WebContentsDelegates to handle the context menu operation first. |
Joel Klinghed | 0ea22ea | 2019-02-20 22:04:22 | [diff] [blame] | 7013 | if (delegate_ && |
| 7014 | delegate_->HandleContextMenu(render_frame_host, context_menu_params)) |
[email protected] | a09d53ce | 2014-01-31 00:46:42 | [diff] [blame] | 7015 | return; |
| 7016 | |
[email protected] | 077e704b | 2014-05-23 19:24:10 | [diff] [blame] | 7017 | render_view_host_delegate_view_->ShowContextMenu(render_frame_host, |
| 7018 | context_menu_params); |
[email protected] | a09d53ce | 2014-01-31 00:46:42 | [diff] [blame] | 7019 | } |
| 7020 | |
Avi Drissman | 4891b85 | 2018-09-25 18:12:58 | [diff] [blame] | 7021 | namespace { |
| 7022 | // Normalizes the line endings: \r\n -> \n, lone \r -> \n. |
Jan Wilken Dörrie | aace0cfef | 2021-03-11 22:01:58 | [diff] [blame] | 7023 | std::u16string NormalizeLineBreaks(const std::u16string& source) { |
Jan Wilken Dörrie | 8aeb574 | 2021-03-23 19:27:02 | [diff] [blame] | 7024 | static const base::NoDestructor<std::u16string> kReturnNewline(u"\r\n"); |
| 7025 | static const base::NoDestructor<std::u16string> kReturn(u"\r"); |
| 7026 | static const base::NoDestructor<std::u16string> kNewline(u"\n"); |
Avi Drissman | 4891b85 | 2018-09-25 18:12:58 | [diff] [blame] | 7027 | |
| 7028 | std::vector<base::StringPiece16> pieces; |
| 7029 | |
| 7030 | for (const auto& rn_line : base::SplitStringPieceUsingSubstr( |
| 7031 | source, *kReturnNewline, base::KEEP_WHITESPACE, |
| 7032 | base::SPLIT_WANT_ALL)) { |
| 7033 | auto r_lines = base::SplitStringPieceUsingSubstr( |
| 7034 | rn_line, *kReturn, base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL); |
| 7035 | std::move(std::begin(r_lines), std::end(r_lines), |
| 7036 | std::back_inserter(pieces)); |
| 7037 | } |
| 7038 | |
| 7039 | return base::JoinString(pieces, *kNewline); |
| 7040 | } |
| 7041 | } // namespace |
| 7042 | |
Dave Tapuska | cdf545cc | 2020-01-23 18:38:52 | [diff] [blame] | 7043 | void WebContentsImpl::RunJavaScriptDialog( |
Kevin McNee | fb86fcf | 2021-02-26 23:20:57 | [diff] [blame] | 7044 | RenderFrameHostImpl* render_frame_host, |
Jan Wilken Dörrie | aace0cfef | 2021-03-11 22:01:58 | [diff] [blame] | 7045 | const std::u16string& message, |
| 7046 | const std::u16string& default_prompt, |
Dave Tapuska | cdf545cc | 2020-01-23 18:38:52 | [diff] [blame] | 7047 | JavaScriptDialogType dialog_type, |
Carlos IL | 5c669bc | 2021-05-26 21:16:24 | [diff] [blame] | 7048 | bool disable_third_party_subframe_suppresion, |
Dave Tapuska | cdf545cc | 2020-01-23 18:38:52 | [diff] [blame] | 7049 | JavaScriptDialogCallback response_callback) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 7050 | OPTIONAL_TRACE_EVENT1("content", "WebContentsImpl::RunJavaScriptDialog", |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 7051 | "render_frame_host", render_frame_host); |
Adithya Srinivasan | 8c177fe8 | 2022-07-28 15:26:27 | [diff] [blame] | 7052 | DCHECK(render_frame_host->GetPage().IsPrimary() && !IsPortal()); |
Tal Pressman | f1cdc75 | 2021-10-25 05:03:49 | [diff] [blame] | 7053 | |
Avi Drissman | 2ccbb40 | 2018-03-20 21:11:36 | [diff] [blame] | 7054 | // Ensure that if showing a dialog is the first thing that a page does, that |
| 7055 | // the contents of the previous page aren't shown behind it. This is required |
| 7056 | // because showing a dialog freezes the renderer, so no frames will be coming |
| 7057 | // from it. https://crbug.com/823353 |
Kevin McNee | fb86fcf | 2021-02-26 23:20:57 | [diff] [blame] | 7058 | auto* render_widget_host_impl = render_frame_host->GetRenderWidgetHost(); |
Avi Drissman | 2ccbb40 | 2018-03-20 21:11:36 | [diff] [blame] | 7059 | if (render_widget_host_impl) |
| 7060 | render_widget_host_impl->ForceFirstFrameAfterNavigationTimeout(); |
| 7061 | |
avi | 0720b02e | 2017-06-13 03:22:13 | [diff] [blame] | 7062 | // Running a dialog causes an exit to webpage-initiated fullscreen. |
| 7063 | // http://crbug.com/728276 |
Avi Drissman | 1a55a9d6 | 2020-03-10 18:56:45 | [diff] [blame] | 7064 | base::ScopedClosureRunner fullscreen_block = ForSecurityDropFullscreen(); |
avi | 0720b02e | 2017-06-13 03:22:13 | [diff] [blame] | 7065 | |
Avi Drissman | 1a55a9d6 | 2020-03-10 18:56:45 | [diff] [blame] | 7066 | auto callback = |
| 7067 | base::BindOnce(&WebContentsImpl::OnDialogClosed, base::Unretained(this), |
| 7068 | render_frame_host->GetProcess()->GetID(), |
| 7069 | render_frame_host->GetRoutingID(), |
| 7070 | std::move(response_callback), std::move(fullscreen_block)); |
Pavel Feldman | 3c1842b | 2017-08-02 05:00:16 | [diff] [blame] | 7071 | |
Pavel Feldman | 3c1842b | 2017-08-02 05:00:16 | [diff] [blame] | 7072 | std::vector<protocol::PageHandler*> page_handlers = |
| 7073 | protocol::PageHandler::EnabledForWebContents(this); |
| 7074 | |
Pavel Feldman | fab3cb6 | 2017-10-05 01:02:01 | [diff] [blame] | 7075 | if (delegate_) |
| 7076 | dialog_manager_ = delegate_->GetJavaScriptDialogManager(this); |
| 7077 | |
Charlie Reis | 9607736 | 2019-07-04 00:34:04 | [diff] [blame] | 7078 | // While a JS message dialog is showing, defer commits in this WebContents. |
David Bokan | 1bdb3701f | 2021-04-30 22:02:35 | [diff] [blame] | 7079 | javascript_dialog_dismiss_notifier_ = |
| 7080 | std::make_unique<JavaScriptDialogDismissNotifier>(); |
Charlie Reis | 9607736 | 2019-07-04 00:34:04 | [diff] [blame] | 7081 | |
Carlos IL | 07f282f | 2020-05-15 22:58:29 | [diff] [blame] | 7082 | // Suppress JavaScript dialogs when requested. |
| 7083 | bool should_suppress = delegate_ && delegate_->ShouldSuppressDialogs(this); |
Pavel Feldman | 1c587bc5 | 2018-04-04 21:16:05 | [diff] [blame] | 7084 | bool has_non_devtools_handlers = delegate_ && dialog_manager_; |
| 7085 | bool has_handlers = page_handlers.size() || has_non_devtools_handlers; |
Pavel Feldman | fab3cb6 | 2017-10-05 01:02:01 | [diff] [blame] | 7086 | bool suppress_this_message = should_suppress || !has_handlers; |
| 7087 | |
Carlos IL | 5c669bc | 2021-05-26 21:16:24 | [diff] [blame] | 7088 | if (!disable_third_party_subframe_suppresion && |
| 7089 | GetContentClient()->browser()->SuppressDifferentOriginSubframeJSDialogs( |
Carlos IL | 0806d68 | 2021-02-25 19:47:26 | [diff] [blame] | 7090 | GetBrowserContext())) { |
Carlos IL | 6a2cd96 | 2021-03-23 19:13:46 | [diff] [blame] | 7091 | // We can't check for opaque origin cases, default to allowing them to |
| 7092 | // trigger dialogs. |
| 7093 | // TODO(carlosil): The main use case for opaque use cases are tests, |
| 7094 | // investigate if there are uses in the wild, otherwise adapt tests that |
| 7095 | // require dialogs so they commit an origin first, and remove this |
| 7096 | // conditional. |
| 7097 | if (!render_frame_host->GetLastCommittedOrigin().opaque()) { |
| 7098 | bool is_different_origin_subframe = |
| 7099 | render_frame_host->GetLastCommittedOrigin() != |
Lukasz Anforowicz | d00c1ed | 2022-01-13 05:25:10 | [diff] [blame] | 7100 | render_frame_host->GetOutermostMainFrame()->GetLastCommittedOrigin(); |
Carlos IL | 6a2cd96 | 2021-03-23 19:13:46 | [diff] [blame] | 7101 | suppress_this_message |= is_different_origin_subframe; |
Carlos IL | 7971a079 | 2021-04-07 21:43:35 | [diff] [blame] | 7102 | if (is_different_origin_subframe) { |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 7103 | GetPrimaryMainFrame()->AddMessageToConsole( |
Carlos IL | 7971a079 | 2021-04-07 21:43:35 | [diff] [blame] | 7104 | blink::mojom::ConsoleMessageLevel::kWarning, |
| 7105 | base::StringPrintf( |
| 7106 | "A different origin subframe tried to create a JavaScript " |
| 7107 | "dialog. This is no longer allowed and was blocked. See " |
| 7108 | "https://www.chromestatus.com/feature/5148698084376576 for " |
| 7109 | "more details.")); |
| 7110 | } |
Carlos IL | 6a2cd96 | 2021-03-23 19:13:46 | [diff] [blame] | 7111 | } |
Carlos IL | 0d9b759 | 2021-02-08 22:23:16 | [diff] [blame] | 7112 | } |
| 7113 | |
Pavel Feldman | fab3cb6 | 2017-10-05 01:02:01 | [diff] [blame] | 7114 | if (suppress_this_message) { |
Jan Wilken Dörrie | aace0cfef | 2021-03-11 22:01:58 | [diff] [blame] | 7115 | std::move(callback).Run(true, false, std::u16string()); |
Pavel Feldman | 3c1842b | 2017-08-02 05:00:16 | [diff] [blame] | 7116 | return; |
| 7117 | } |
| 7118 | |
| 7119 | scoped_refptr<CloseDialogCallbackWrapper> wrapper = |
Avi Drissman | e04d399 | 2017-10-05 15:11:36 | [diff] [blame] | 7120 | new CloseDialogCallbackWrapper(std::move(callback)); |
Pavel Feldman | 3c1842b | 2017-08-02 05:00:16 | [diff] [blame] | 7121 | |
| 7122 | is_showing_javascript_dialog_ = true; |
| 7123 | |
Jan Wilken Dörrie | aace0cfef | 2021-03-11 22:01:58 | [diff] [blame] | 7124 | std::u16string normalized_message = NormalizeLineBreaks(message); |
Avi Drissman | 4891b85 | 2018-09-25 18:12:58 | [diff] [blame] | 7125 | |
Pavel Feldman | 3c1842b | 2017-08-02 05:00:16 | [diff] [blame] | 7126 | for (auto* handler : page_handlers) { |
Avi Drissman | e04d399 | 2017-10-05 15:11:36 | [diff] [blame] | 7127 | handler->DidRunJavaScriptDialog( |
Avi Drissman | 4891b85 | 2018-09-25 18:12:58 | [diff] [blame] | 7128 | render_frame_host->GetLastCommittedURL(), normalized_message, |
| 7129 | default_prompt, dialog_type, has_non_devtools_handlers, |
Avi Drissman | e04d399 | 2017-10-05 15:11:36 | [diff] [blame] | 7130 | base::BindOnce(&CloseDialogCallbackWrapper::Run, wrapper, false)); |
Pavel Feldman | 3c1842b | 2017-08-02 05:00:16 | [diff] [blame] | 7131 | } |
| 7132 | |
| 7133 | if (dialog_manager_) { |
[email protected] | 87de04b0 | 2014-04-08 22:14:49 | [diff] [blame] | 7134 | dialog_manager_->RunJavaScriptDialog( |
Avi Drissman | 4891b85 | 2018-09-25 18:12:58 | [diff] [blame] | 7135 | this, render_frame_host, dialog_type, normalized_message, |
| 7136 | default_prompt, |
Avi Drissman | e04d399 | 2017-10-05 15:11:36 | [diff] [blame] | 7137 | base::BindOnce(&CloseDialogCallbackWrapper::Run, wrapper, false), |
| 7138 | &suppress_this_message); |
[email protected] | 87de04b0 | 2014-04-08 22:14:49 | [diff] [blame] | 7139 | } |
| 7140 | |
| 7141 | if (suppress_this_message) { |
| 7142 | // If we are suppressing messages, just reply as if the user immediately |
| 7143 | // pressed "Cancel", passing true to |dialog_was_suppressed|. |
Jan Wilken Dörrie | aace0cfef | 2021-03-11 22:01:58 | [diff] [blame] | 7144 | wrapper->Run(true, false, std::u16string()); |
[email protected] | 87de04b0 | 2014-04-08 22:14:49 | [diff] [blame] | 7145 | } |
[email protected] | 87de04b0 | 2014-04-08 22:14:49 | [diff] [blame] | 7146 | } |
| 7147 | |
David Bokan | 1bdb3701f | 2021-04-30 22:02:35 | [diff] [blame] | 7148 | void WebContentsImpl::NotifyOnJavaScriptDialogDismiss( |
| 7149 | base::OnceClosure callback) { |
| 7150 | DCHECK(javascript_dialog_dismiss_notifier_); |
| 7151 | javascript_dialog_dismiss_notifier_->NotifyOnDismiss(std::move(callback)); |
| 7152 | } |
| 7153 | |
[email protected] | 87de04b0 | 2014-04-08 22:14:49 | [diff] [blame] | 7154 | void WebContentsImpl::RunBeforeUnloadConfirm( |
Kevin McNee | fb86fcf | 2021-02-26 23:20:57 | [diff] [blame] | 7155 | RenderFrameHostImpl* render_frame_host, |
[email protected] | 87de04b0 | 2014-04-08 22:14:49 | [diff] [blame] | 7156 | bool is_reload, |
Dave Tapuska | cdf545cc | 2020-01-23 18:38:52 | [diff] [blame] | 7157 | JavaScriptDialogCallback response_callback) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 7158 | OPTIONAL_TRACE_EVENT2("content", "WebContentsImpl::RunBeforeUnloadConfirm", |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 7159 | "render_frame_host", render_frame_host, "is_reload", |
| 7160 | is_reload); |
Adithya Srinivasan | 8c177fe8 | 2022-07-28 15:26:27 | [diff] [blame] | 7161 | DCHECK(render_frame_host->GetPage().IsPrimary() && !IsPortal()); |
Tal Pressman | f1cdc75 | 2021-10-25 05:03:49 | [diff] [blame] | 7162 | |
Avi Drissman | 2ccbb40 | 2018-03-20 21:11:36 | [diff] [blame] | 7163 | // Ensure that if showing a dialog is the first thing that a page does, that |
| 7164 | // the contents of the previous page aren't shown behind it. This is required |
| 7165 | // because showing a dialog freezes the renderer, so no frames will be coming |
| 7166 | // from it. https://crbug.com/823353 |
Kevin McNee | fb86fcf | 2021-02-26 23:20:57 | [diff] [blame] | 7167 | auto* render_widget_host_impl = render_frame_host->GetRenderWidgetHost(); |
Avi Drissman | 2ccbb40 | 2018-03-20 21:11:36 | [diff] [blame] | 7168 | if (render_widget_host_impl) |
| 7169 | render_widget_host_impl->ForceFirstFrameAfterNavigationTimeout(); |
| 7170 | |
avi | 0720b02e | 2017-06-13 03:22:13 | [diff] [blame] | 7171 | // Running a dialog causes an exit to webpage-initiated fullscreen. |
| 7172 | // http://crbug.com/728276 |
Avi Drissman | 1a55a9d6 | 2020-03-10 18:56:45 | [diff] [blame] | 7173 | base::ScopedClosureRunner fullscreen_block = ForSecurityDropFullscreen(); |
avi | 0720b02e | 2017-06-13 03:22:13 | [diff] [blame] | 7174 | |
Avi Drissman | 1a55a9d6 | 2020-03-10 18:56:45 | [diff] [blame] | 7175 | auto callback = |
| 7176 | base::BindOnce(&WebContentsImpl::OnDialogClosed, base::Unretained(this), |
| 7177 | render_frame_host->GetProcess()->GetID(), |
| 7178 | render_frame_host->GetRoutingID(), |
| 7179 | std::move(response_callback), std::move(fullscreen_block)); |
Pavel Feldman | 3c1842b | 2017-08-02 05:00:16 | [diff] [blame] | 7180 | |
Pavel Feldman | 3c1842b | 2017-08-02 05:00:16 | [diff] [blame] | 7181 | std::vector<protocol::PageHandler*> page_handlers = |
| 7182 | protocol::PageHandler::EnabledForWebContents(this); |
| 7183 | |
Pavel Feldman | fab3cb6 | 2017-10-05 01:02:01 | [diff] [blame] | 7184 | if (delegate_) |
| 7185 | dialog_manager_ = delegate_->GetJavaScriptDialogManager(this); |
| 7186 | |
Charlie Reis | 9607736 | 2019-07-04 00:34:04 | [diff] [blame] | 7187 | // While a JS beforeunload dialog is showing, defer commits in this |
| 7188 | // WebContents. |
David Bokan | 1bdb3701f | 2021-04-30 22:02:35 | [diff] [blame] | 7189 | javascript_dialog_dismiss_notifier_ = |
| 7190 | std::make_unique<JavaScriptDialogDismissNotifier>(); |
Charlie Reis | 9607736 | 2019-07-04 00:34:04 | [diff] [blame] | 7191 | |
Dave Tapuska | 19578e9 | 2021-08-09 17:14:02 | [diff] [blame] | 7192 | bool should_suppress = delegate_ && delegate_->ShouldSuppressDialogs(this); |
Pavel Feldman | 1c587bc5 | 2018-04-04 21:16:05 | [diff] [blame] | 7193 | bool has_non_devtools_handlers = delegate_ && dialog_manager_; |
| 7194 | bool has_handlers = page_handlers.size() || has_non_devtools_handlers; |
Pavel Feldman | fab3cb6 | 2017-10-05 01:02:01 | [diff] [blame] | 7195 | if (should_suppress || !has_handlers) { |
Jan Wilken Dörrie | aace0cfef | 2021-03-11 22:01:58 | [diff] [blame] | 7196 | std::move(callback).Run(false, true, std::u16string()); |
[email protected] | 87de04b0 | 2014-04-08 22:14:49 | [diff] [blame] | 7197 | return; |
| 7198 | } |
| 7199 | |
| 7200 | is_showing_before_unload_dialog_ = true; |
Pavel Feldman | 3c1842b | 2017-08-02 05:00:16 | [diff] [blame] | 7201 | |
| 7202 | scoped_refptr<CloseDialogCallbackWrapper> wrapper = |
Avi Drissman | e04d399 | 2017-10-05 15:11:36 | [diff] [blame] | 7203 | new CloseDialogCallbackWrapper(std::move(callback)); |
Pavel Feldman | 3c1842b | 2017-08-02 05:00:16 | [diff] [blame] | 7204 | |
Kevin McNee | fb86fcf | 2021-02-26 23:20:57 | [diff] [blame] | 7205 | GURL frame_url = render_frame_host->GetLastCommittedURL(); |
Pavel Feldman | 3c1842b | 2017-08-02 05:00:16 | [diff] [blame] | 7206 | for (auto* handler : page_handlers) { |
Avi Drissman | e04d399 | 2017-10-05 15:11:36 | [diff] [blame] | 7207 | handler->DidRunBeforeUnloadConfirm( |
Pavel Feldman | 1c587bc5 | 2018-04-04 21:16:05 | [diff] [blame] | 7208 | frame_url, has_non_devtools_handlers, |
Avi Drissman | e04d399 | 2017-10-05 15:11:36 | [diff] [blame] | 7209 | base::BindOnce(&CloseDialogCallbackWrapper::Run, wrapper, false)); |
Pavel Feldman | 3c1842b | 2017-08-02 05:00:16 | [diff] [blame] | 7210 | } |
| 7211 | |
| 7212 | if (dialog_manager_) { |
Avi Drissman | e04d399 | 2017-10-05 15:11:36 | [diff] [blame] | 7213 | dialog_manager_->RunBeforeUnloadDialog( |
Avi Drissman | ff9ed75 | 2017-11-28 21:18:12 | [diff] [blame] | 7214 | this, render_frame_host, is_reload, |
Avi Drissman | e04d399 | 2017-10-05 15:11:36 | [diff] [blame] | 7215 | base::BindOnce(&CloseDialogCallbackWrapper::Run, wrapper, false)); |
Pavel Feldman | 3c1842b | 2017-08-02 05:00:16 | [diff] [blame] | 7216 | } |
[email protected] | 87de04b0 | 2014-04-08 22:14:49 | [diff] [blame] | 7217 | } |
| 7218 | |
Kent Tamura | c260c9a | 2018-09-14 09:10:56 | [diff] [blame] | 7219 | void WebContentsImpl::RunFileChooser( |
| 7220 | RenderFrameHost* render_frame_host, |
Kent Tamura | 3abb32d | 2020-07-02 00:23:01 | [diff] [blame] | 7221 | scoped_refptr<FileChooserImpl::FileSelectListenerImpl> listener, |
Kent Tamura | c260c9a | 2018-09-14 09:10:56 | [diff] [blame] | 7222 | const blink::mojom::FileChooserParams& params) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 7223 | OPTIONAL_TRACE_EVENT1("content", "WebContentsImpl::RunFileChooser", |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 7224 | "render_frame_host", render_frame_host); |
Avi Drissman | 3d41e771 | 2018-08-27 21:18:08 | [diff] [blame] | 7225 | // Any explicit focusing of another window while this WebContents is in |
| 7226 | // fullscreen can be used to confuse the user, so drop fullscreen. |
Avi Drissman | 1a55a9d6 | 2020-03-10 18:56:45 | [diff] [blame] | 7227 | base::ScopedClosureRunner fullscreen_block = ForSecurityDropFullscreen(); |
| 7228 | listener->SetFullscreenBlock(std::move(fullscreen_block)); |
Avi Drissman | 3d41e771 | 2018-08-27 21:18:08 | [diff] [blame] | 7229 | |
nasko | ada75b2 | 2016-06-11 16:09:46 | [diff] [blame] | 7230 | if (delegate_) |
Kent Tamura | 512a27e | 2018-10-04 00:49:32 | [diff] [blame] | 7231 | delegate_->RunFileChooser(render_frame_host, std::move(listener), params); |
| 7232 | else |
| 7233 | listener->FileSelectionCanceled(); |
nasko | ada75b2 | 2016-06-11 16:09:46 | [diff] [blame] | 7234 | } |
| 7235 | |
Lucas Furukawa Gadani | e1c5dfda | 2018-11-29 17:57:41 | [diff] [blame] | 7236 | double WebContentsImpl::GetPendingPageZoomLevel() { |
Xiaohan Wang | 24ec934 | 2022-01-15 17:34:22 | [diff] [blame] | 7237 | #if BUILDFLAG(IS_ANDROID) |
Mark Schillaci | 1363e4a | 2021-10-04 19:10:09 | [diff] [blame] | 7238 | // On Android, use the default page zoom level when the AccessibilityPageZoom |
Aishwarya Rajesh | 39ef1c6 | 2022-11-09 06:25:37 | [diff] [blame] | 7239 | // and RequestDesktopSiteZoom features are not enabled. |
| 7240 | if (!base::FeatureList::IsEnabled(features::kAccessibilityPageZoom) && |
| 7241 | !base::FeatureList::IsEnabled(features::kRequestDesktopSiteZoom)) { |
Mark Schillaci | 1363e4a | 2021-10-04 19:10:09 | [diff] [blame] | 7242 | return 0.0; |
| 7243 | } |
| 7244 | #endif |
wjmaclean | 6495190 | 2016-04-29 20:59:12 | [diff] [blame] | 7245 | NavigationEntry* pending_entry = GetController().GetPendingEntry(); |
| 7246 | if (!pending_entry) |
| 7247 | return HostZoomMap::GetZoomLevel(this); |
| 7248 | |
| 7249 | GURL url = pending_entry->GetURL(); |
| 7250 | return HostZoomMap::GetForWebContents(this)->GetZoomLevelForHostAndScheme( |
| 7251 | url.scheme(), net::GetHostOrSpecFromURL(url)); |
| 7252 | } |
| 7253 | |
François Beaufort | ad6c523 | 2018-02-26 11:00:44 | [diff] [blame] | 7254 | bool WebContentsImpl::IsPictureInPictureAllowedForFullscreenVideo() const { |
| 7255 | return media_web_contents_observer_ |
| 7256 | ->IsPictureInPictureAllowedForFullscreenVideo(); |
| 7257 | } |
| 7258 | |
ekaramad | a110f64 | 2016-12-21 19:47:28 | [diff] [blame] | 7259 | bool WebContentsImpl::IsFocusedElementEditable() { |
| 7260 | RenderFrameHostImpl* frame = GetFocusedFrame(); |
| 7261 | return frame && frame->has_focused_editable_element(); |
| 7262 | } |
| 7263 | |
Lucas Furukawa Gadani | e1c5dfda | 2018-11-29 17:57:41 | [diff] [blame] | 7264 | bool WebContentsImpl::IsShowingContextMenu() { |
ekaramad | f6750aa | 2017-06-06 18:29:42 | [diff] [blame] | 7265 | return showing_context_menu_; |
| 7266 | } |
| 7267 | |
| 7268 | void WebContentsImpl::SetShowingContextMenu(bool showing) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 7269 | OPTIONAL_TRACE_EVENT1("content", "WebContentsImpl::SetShowingContextMenu", |
| 7270 | "showing", showing); |
| 7271 | |
ekaramad | f6750aa | 2017-06-06 18:29:42 | [diff] [blame] | 7272 | DCHECK_NE(showing_context_menu_, showing); |
| 7273 | showing_context_menu_ = showing; |
| 7274 | |
| 7275 | if (auto* view = GetRenderWidgetHostView()) { |
| 7276 | // Notify the main frame's RWHV to run the platform-specific code, if any. |
| 7277 | static_cast<RenderWidgetHostViewBase*>(view)->SetShowingContextMenu( |
| 7278 | showing); |
| 7279 | } |
| 7280 | } |
| 7281 | |
ekaramad | a110f64 | 2016-12-21 19:47:28 | [diff] [blame] | 7282 | void WebContentsImpl::ClearFocusedElement() { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 7283 | OPTIONAL_TRACE_EVENT0("content", "WebContentsImpl::ClearFocusedElement"); |
ekaramad | a110f64 | 2016-12-21 19:47:28 | [diff] [blame] | 7284 | if (auto* frame = GetFocusedFrame()) |
| 7285 | frame->ClearFocusedElement(); |
| 7286 | } |
| 7287 | |
danakj | 77eb7e8 | 2020-01-09 19:38:46 | [diff] [blame] | 7288 | bool WebContentsImpl::IsNeverComposited() { |
[email protected] | 7912e82 | 2014-04-16 02:37:03 | [diff] [blame] | 7289 | if (!delegate_) |
| 7290 | return false; |
danakj | 77eb7e8 | 2020-01-09 19:38:46 | [diff] [blame] | 7291 | return delegate_->IsNeverComposited(this); |
[email protected] | 7912e82 | 2014-04-16 02:37:03 | [diff] [blame] | 7292 | } |
| 7293 | |
[email protected] | 5a3bdf5 | 2012-05-24 15:12:57 | [diff] [blame] | 7294 | RenderViewHostDelegateView* WebContentsImpl::GetDelegateView() { |
| 7295 | return render_view_host_delegate_view_; |
[email protected] | 420ae01 | 2009-04-24 05:16:32 | [diff] [blame] | 7296 | } |
| 7297 | |
Mario Sanchez Prada | 0bd8b8c | 2020-10-21 17:49:23 | [diff] [blame] | 7298 | const blink::RendererPreferences& WebContentsImpl::GetRendererPrefs() const { |
[email protected] | 840b151 | 2009-07-21 16:53:46 | [diff] [blame] | 7299 | return renderer_preferences_; |
[email protected] | 80d96fa | 2009-06-10 22:34:51 | [diff] [blame] | 7300 | } |
| 7301 | |
Lucas Furukawa Gadani | 2ec00c8 | 2018-12-14 15:53:16 | [diff] [blame] | 7302 | RenderFrameHostImpl* WebContentsImpl::GetOuterWebContentsFrame() { |
| 7303 | if (GetOuterDelegateFrameTreeNodeId() == |
| 7304 | FrameTreeNode::kFrameTreeNodeInvalidId) { |
| 7305 | return nullptr; |
| 7306 | } |
| 7307 | |
| 7308 | FrameTreeNode* outer_node = |
| 7309 | FrameTreeNode::GloballyFindByID(GetOuterDelegateFrameTreeNodeId()); |
| 7310 | // The outer node should be in the outer WebContents. |
Arthur Sonzogni | f6785ec | 2022-12-05 10:11:50 | [diff] [blame] | 7311 | DCHECK_EQ(&outer_node->frame_tree(), |
Carlos Caballero | 6a99dac | 2021-11-03 10:41:17 | [diff] [blame] | 7312 | &GetOuterWebContents()->GetPrimaryFrameTree()); |
Alexander Timin | 381e7e18 | 2020-04-28 19:04:03 | [diff] [blame] | 7313 | return outer_node->parent(); |
Lucas Furukawa Gadani | 2ec00c8 | 2018-12-14 15:53:16 | [diff] [blame] | 7314 | } |
| 7315 | |
Lucas Furukawa Gadani | e1c5dfda | 2018-11-29 17:57:41 | [diff] [blame] | 7316 | WebContentsImpl* WebContentsImpl::GetOuterWebContents() { |
Lucas Furukawa Gadani | 105de1e81 | 2018-05-31 19:38:39 | [diff] [blame] | 7317 | return node_.outer_web_contents(); |
avallee | 0206f78 | 2016-07-28 18:55:33 | [diff] [blame] | 7318 | } |
| 7319 | |
Lucas Furukawa Gadani | 2ec00c8 | 2018-12-14 15:53:16 | [diff] [blame] | 7320 | std::vector<WebContents*> WebContentsImpl::GetInnerWebContents() { |
| 7321 | std::vector<WebContents*> all_inner_contents; |
Lucas Furukawa Gadani | 2ec00c8 | 2018-12-14 15:53:16 | [diff] [blame] | 7322 | const auto& inner_contents = node_.GetInnerWebContents(); |
| 7323 | all_inner_contents.insert(all_inner_contents.end(), inner_contents.begin(), |
| 7324 | inner_contents.end()); |
| 7325 | return all_inner_contents; |
| 7326 | } |
| 7327 | |
Jeremy Roman | 7e70bf95 | 2020-01-07 23:23:58 | [diff] [blame] | 7328 | WebContentsImpl* WebContentsImpl::GetResponsibleWebContents() { |
| 7329 | // Iteratively ask delegates which other contents is responsible until a fixed |
| 7330 | // point is found. |
| 7331 | WebContentsImpl* contents = this; |
| 7332 | while (WebContentsDelegate* delegate = contents->GetDelegate()) { |
| 7333 | auto* responsible_contents = static_cast<WebContentsImpl*>( |
| 7334 | delegate->GetResponsibleWebContents(contents)); |
| 7335 | if (responsible_contents == contents) |
| 7336 | break; |
| 7337 | contents = responsible_contents; |
| 7338 | } |
| 7339 | return contents; |
| 7340 | } |
| 7341 | |
avallee | 0206f78 | 2016-07-28 18:55:33 | [diff] [blame] | 7342 | WebContentsImpl* WebContentsImpl::GetFocusedWebContents() { |
Dave Tapuska | d8b0530f | 2021-10-19 15:12:31 | [diff] [blame] | 7343 | return WebContentsImpl::FromFrameTreeNode(GetFocusedFrameTree()->root()); |
| 7344 | } |
| 7345 | |
| 7346 | FrameTree* WebContentsImpl::GetFocusedFrameTree() { |
| 7347 | return GetOutermostWebContents()->node_.focused_frame_tree(); |
avallee | 0206f78 | 2016-07-28 18:55:33 | [diff] [blame] | 7348 | } |
| 7349 | |
Lukasz Anforowicz | 2cd231bf | 2020-10-02 19:13:20 | [diff] [blame] | 7350 | void WebContentsImpl::SetFocusToLocationBar() { |
| 7351 | OPTIONAL_TRACE_EVENT0("content", "WebContentsImpl::SetFocusToLocationBar"); |
| 7352 | if (delegate_) |
| 7353 | delegate_->SetFocusToLocationBar(); |
| 7354 | } |
| 7355 | |
avallee | 9993fca | 2016-11-17 06:16:50 | [diff] [blame] | 7356 | bool WebContentsImpl::ContainsOrIsFocusedWebContents() { |
| 7357 | for (WebContentsImpl* focused_contents = GetFocusedWebContents(); |
| 7358 | focused_contents; |
| 7359 | focused_contents = focused_contents->GetOuterWebContents()) { |
| 7360 | if (focused_contents == this) |
| 7361 | return true; |
| 7362 | } |
| 7363 | |
| 7364 | return false; |
| 7365 | } |
| 7366 | |
W. James MacLean | 9437b5bf | 2019-12-06 17:33:44 | [diff] [blame] | 7367 | void WebContentsImpl::RemoveBrowserPluginEmbedder() { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 7368 | OPTIONAL_TRACE_EVENT0("content", |
| 7369 | "WebContentsImpl::RemoveBrowserPluginEmbedder"); |
W. James MacLean | 9437b5bf | 2019-12-06 17:33:44 | [diff] [blame] | 7370 | browser_plugin_embedder_.reset(); |
| 7371 | } |
| 7372 | |
avallee | 0206f78 | 2016-07-28 18:55:33 | [diff] [blame] | 7373 | WebContentsImpl* WebContentsImpl::GetOutermostWebContents() { |
| 7374 | WebContentsImpl* root = this; |
| 7375 | while (root->GetOuterWebContents()) |
| 7376 | root = root->GetOuterWebContents(); |
| 7377 | return root; |
| 7378 | } |
| 7379 | |
Sean Topping | bbc4a2bd | 2019-01-30 02:16:14 | [diff] [blame] | 7380 | void WebContentsImpl::InnerWebContentsCreated(WebContents* inner_web_contents) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 7381 | OPTIONAL_TRACE_EVENT0("content", "WebContentsImpl::InnerWebContentsCreated"); |
Andrew Grieve | e19cd93e | 2021-01-22 05:43:06 | [diff] [blame] | 7382 | observers_.NotifyObservers(&WebContentsObserver::InnerWebContentsCreated, |
| 7383 | inner_web_contents); |
Sean Topping | bbc4a2bd | 2019-01-30 02:16:14 | [diff] [blame] | 7384 | } |
| 7385 | |
Jeremy Roman | 03ce13ce | 2020-05-29 22:16:57 | [diff] [blame] | 7386 | void WebContentsImpl::InnerWebContentsAttached( |
| 7387 | WebContents* inner_web_contents) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 7388 | OPTIONAL_TRACE_EVENT0("content", "WebContentsImpl::InnerWebContentsDetached"); |
Jeremy Roman | 03ce13ce | 2020-05-29 22:16:57 | [diff] [blame] | 7389 | if (inner_web_contents->IsCurrentlyAudible()) |
| 7390 | OnAudioStateChanged(); |
| 7391 | } |
| 7392 | |
| 7393 | void WebContentsImpl::InnerWebContentsDetached( |
| 7394 | WebContents* inner_web_contents) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 7395 | OPTIONAL_TRACE_EVENT0("content", "WebContentsImpl::InnerWebContentsCreated"); |
Takashi Toyoshima | ea534ef2 | 2021-07-21 03:27:59 | [diff] [blame] | 7396 | if (!IsBeingDestroyed()) |
Jeremy Roman | 03ce13ce | 2020-05-29 22:16:57 | [diff] [blame] | 7397 | OnAudioStateChanged(); |
| 7398 | } |
| 7399 | |
[email protected] | b172aee | 2012-04-10 17:05:26 | [diff] [blame] | 7400 | void WebContentsImpl::RenderViewReady(RenderViewHost* rvh) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 7401 | OPTIONAL_TRACE_EVENT1("content", "WebContentsImpl::RenderViewReady", |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 7402 | "render_view_host", rvh); |
[email protected] | 151a63d | 2011-12-20 22:32:52 | [diff] [blame] | 7403 | if (rvh != GetRenderViewHost()) { |
[email protected] | 420ae01 | 2009-04-24 05:16:32 | [diff] [blame] | 7404 | // Don't notify the world, since this came from a renderer in the |
| 7405 | // background. |
| 7406 | return; |
| 7407 | } |
| 7408 | |
dmazzoni | 6211145 | 2017-04-28 23:46:55 | [diff] [blame] | 7409 | RenderWidgetHostViewBase* rwhv = |
| 7410 | static_cast<RenderWidgetHostViewBase*>(GetRenderWidgetHostView()); |
| 7411 | if (rwhv) |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 7412 | rwhv->SetMainFrameAXTreeID(GetPrimaryMainFrame()->GetAXTreeID()); |
dmazzoni | 6211145 | 2017-04-28 23:46:55 | [diff] [blame] | 7413 | |
[email protected] | da7a718 | 2013-09-06 08:11:11 | [diff] [blame] | 7414 | notify_disconnection_ = true; |
[email protected] | da7a718 | 2013-09-06 08:11:11 | [diff] [blame] | 7415 | |
Clark DuVall | 22aaff3 | 2021-03-18 17:17:47 | [diff] [blame] | 7416 | { |
| 7417 | SCOPED_UMA_HISTOGRAM_TIMER("WebContentsObserver.RenderViewReady"); |
| 7418 | observers_.NotifyObservers(&WebContentsObserver::RenderViewReady); |
| 7419 | } |
Jinsuk Kim | 895b2c5 | 2018-07-03 06:49:16 | [diff] [blame] | 7420 | view_->RenderViewReady(); |
[email protected] | 420ae01 | 2009-04-24 05:16:32 | [diff] [blame] | 7421 | } |
| 7422 | |
[email protected] | ec6a7eb | 2013-04-22 17:34:22 | [diff] [blame] | 7423 | void WebContentsImpl::RenderViewTerminated(RenderViewHost* rvh, |
| 7424 | base::TerminationStatus status, |
| 7425 | int error_code) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 7426 | OPTIONAL_TRACE_EVENT2("content", "WebContentsImpl::RenderViewTerminated", |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 7427 | "render_view_host", rvh, "status", |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 7428 | static_cast<int>(status)); |
Alex Moshchuk | 2e470ea | 2021-02-03 06:46:34 | [diff] [blame] | 7429 | |
| 7430 | // It is possible to get here while the WebContentsImpl is being destroyed, |
| 7431 | // in particular when the destruction of the main frame's RenderFrameHost and |
| 7432 | // RenderViewHost triggers cleanup of the main frame's process, which in turn |
| 7433 | // dispatches RenderProcessExited observers, one of which calls in here. In |
| 7434 | // this state, we cannot check GetRenderViewHost() below, since |
| 7435 | // current_frame_host() for the root FrameTreeNode has already been cleared. |
| 7436 | // Since the WebContents is going away, none of the work here is needed, so |
| 7437 | // just return early. |
Takashi Toyoshima | ea534ef2 | 2021-07-21 03:27:59 | [diff] [blame] | 7438 | if (IsBeingDestroyed()) |
Alex Moshchuk | 2e470ea | 2021-02-03 06:46:34 | [diff] [blame] | 7439 | return; |
| 7440 | |
[email protected] | 151a63d | 2011-12-20 22:32:52 | [diff] [blame] | 7441 | if (rvh != GetRenderViewHost()) { |
[email protected] | 420ae01 | 2009-04-24 05:16:32 | [diff] [blame] | 7442 | // The pending page's RenderViewHost is gone. |
| 7443 | return; |
| 7444 | } |
Lingqi Chi | 641aeee | 2021-09-08 00:32:58 | [diff] [blame] | 7445 | |
| 7446 | auto* rvh_impl = static_cast<RenderViewHostImpl*>(rvh); |
Kevin McNee | 9f3ef7c | 2021-09-29 18:43:41 | [diff] [blame] | 7447 | DCHECK(rvh_impl->GetMainRenderFrameHost()->IsInPrimaryMainFrame()) |
Khushal | c5eaf22 | 2021-06-30 20:15:48 | [diff] [blame] | 7448 | << "GetRenderViewHost() must belong to the primary frame tree"; |
| 7449 | |
[email protected] | 2fcdcc3 | 2014-03-05 02:14:07 | [diff] [blame] | 7450 | // Ensure fullscreen mode is exited in the |delegate_| since a crashed |
| 7451 | // renderer may not have made a clean exit. |
Avi Drissman | 97aef04 | 2020-06-30 21:04:48 | [diff] [blame] | 7452 | if (IsFullscreen()) |
bokan | ece34a8 | 2016-01-28 19:49:46 | [diff] [blame] | 7453 | ExitFullscreenMode(false); |
[email protected] | 2fcdcc3 | 2014-03-05 02:14:07 | [diff] [blame] | 7454 | |
Klaus Weidner | d821943 | 2022-02-08 21:50:59 | [diff] [blame] | 7455 | // Ensure any video or document in Picture-in-Picture is exited in the |
| 7456 | // |delegate_| since a crashed renderer may not have made a clean exit. |
| 7457 | if (HasPictureInPictureVideo() || HasPictureInPictureDocument()) |
Jennifer Apacible | b4b972a | 2018-06-07 04:41:02 | [diff] [blame] | 7458 | ExitPictureInPicture(); |
| 7459 | |
[email protected] | 698191dc | 2014-02-25 01:06:13 | [diff] [blame] | 7460 | // Cancel any visible dialogs so they are not left dangling over the sad tab. |
creis | 89a0f78 | 2015-05-27 16:13:17 | [diff] [blame] | 7461 | CancelActiveAndPendingDialogs(); |
[email protected] | 698191dc | 2014-02-25 01:06:13 | [diff] [blame] | 7462 | |
Lingqi Chi | 641aeee | 2021-09-08 00:32:58 | [diff] [blame] | 7463 | audio_stream_monitor_.RenderProcessGone(rvh_impl->GetProcess()->GetID()); |
dalecurtis | 3d1e130 | 2017-02-20 22:33:25 | [diff] [blame] | 7464 | |
[email protected] | 960b037 | 2014-05-19 18:01:00 | [diff] [blame] | 7465 | // Reset the loading progress. TODO(avi): What does it mean to have a |
| 7466 | // "renderer crash" when there is more than one renderer process serving a |
| 7467 | // webpage? Once this function is called at a more granular frame level, we |
| 7468 | // probably will need to more granularly reset the state here. |
| 7469 | ResetLoadProgressState(); |
Khushal | c5eaf22 | 2021-06-30 20:15:48 | [diff] [blame] | 7470 | |
| 7471 | SetPrimaryMainFrameProcessStatus(status, error_code); |
[email protected] | 960b037 | 2014-05-19 18:01:00 | [diff] [blame] | 7472 | |
Dave Tapuska | 7052b7c5 | 2021-10-18 17:30:53 | [diff] [blame] | 7473 | TRACE_EVENT0( |
| 7474 | "content", |
| 7475 | "Dispatching WebContentsObserver::PrimaryMainFrameRenderProcessGone"); |
| 7476 | // Some observers might destroy WebContents in |
| 7477 | // PrimaryMainFrameRenderProcessGone. |
Alexander Timin | 90c20c3 | 2020-08-12 15:28:32 | [diff] [blame] | 7478 | base::WeakPtr<WebContentsImpl> weak_ptr = weak_factory_.GetWeakPtr(); |
Alexander Timin | 90c20c3 | 2020-08-12 15:28:32 | [diff] [blame] | 7479 | for (auto& observer : observers_.observer_list()) { |
Dave Tapuska | 7052b7c5 | 2021-10-18 17:30:53 | [diff] [blame] | 7480 | observer.PrimaryMainFrameRenderProcessGone(status); |
Alexander Timin | 90c20c3 | 2020-08-12 15:28:32 | [diff] [blame] | 7481 | if (!weak_ptr) |
| 7482 | return; |
| 7483 | } |
| 7484 | |
| 7485 | // |this| might have been deleted. Do not add code here. |
[email protected] | 420ae01 | 2009-04-24 05:16:32 | [diff] [blame] | 7486 | } |
| 7487 | |
[email protected] | b172aee | 2012-04-10 17:05:26 | [diff] [blame] | 7488 | void WebContentsImpl::RenderViewDeleted(RenderViewHost* rvh) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 7489 | OPTIONAL_TRACE_EVENT1("content", "WebContentsImpl::RenderViewDeleted", |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 7490 | "render_view_host", rvh); |
Andrew Grieve | e19cd93e | 2021-01-22 05:43:06 | [diff] [blame] | 7491 | observers_.NotifyObservers(&WebContentsObserver::RenderViewDeleted, rvh); |
[email protected] | 2e4633c | 2009-07-09 16:58:06 | [diff] [blame] | 7492 | } |
| 7493 | |
Lukasz Anforowicz | fd70714 | 2018-02-07 19:46:13 | [diff] [blame] | 7494 | void WebContentsImpl::ClearTargetURL() { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 7495 | OPTIONAL_TRACE_EVENT0("content", "WebContentsImpl::ClearTargetURL"); |
Mario Sanchez Prada | dffbe66e | 2020-09-07 12:01:08 | [diff] [blame] | 7496 | frame_that_set_last_target_url_ = nullptr; |
Lukasz Anforowicz | fd70714 | 2018-02-07 19:46:13 | [diff] [blame] | 7497 | if (delegate_) |
| 7498 | delegate_->UpdateTargetURL(this, GURL()); |
| 7499 | } |
| 7500 | |
Alex Moshchuk | e558aba | 2023-01-13 07:42:02 | [diff] [blame] | 7501 | void WebContentsImpl::Close() { |
| 7502 | OPTIONAL_TRACE_EVENT1("content", "WebContentsImpl::Close", |
| 7503 | "render_frame_host", GetPrimaryMainFrame()); |
Xiaohan Wang | 24ec934 | 2022-01-15 17:34:22 | [diff] [blame] | 7504 | #if BUILDFLAG(IS_MAC) |
[email protected] | 0770730 | 2009-11-06 00:50:29 | [diff] [blame] | 7505 | // The UI may be in an event-tracking loop, such as between the |
| 7506 | // mouse-down and mouse-up in text selection or a button click. |
| 7507 | // Defer the close until after tracking is complete, so that we |
| 7508 | // don't free objects out from under the UI. |
[email protected] | 0770730 | 2009-11-06 00:50:29 | [diff] [blame] | 7509 | // TODO(shess): This could get more fine-grained. For instance, |
| 7510 | // closing a tab in another window while selecting text in the |
| 7511 | // current window's Omnibox should be just fine. |
Christopher Cameron | 9ae31e0d | 2019-03-01 20:01:19 | [diff] [blame] | 7512 | if (view_->CloseTabAfterEventTrackingIfNeeded()) |
[email protected] | 0770730 | 2009-11-06 00:50:29 | [diff] [blame] | 7513 | return; |
[email protected] | 60780f41 | 2013-02-25 16:34:10 | [diff] [blame] | 7514 | #endif |
[email protected] | 0770730 | 2009-11-06 00:50:29 | [diff] [blame] | 7515 | |
Alex Moshchuk | e558aba | 2023-01-13 07:42:02 | [diff] [blame] | 7516 | if (delegate_) { |
[email protected] | 6934a70 | 2011-12-20 00:04:51 | [diff] [blame] | 7517 | delegate_->CloseContents(this); |
Alex Moshchuk | e558aba | 2023-01-13 07:42:02 | [diff] [blame] | 7518 | } |
[email protected] | 420ae01 | 2009-04-24 05:16:32 | [diff] [blame] | 7519 | } |
| 7520 | |
Dave Tapuska | 3f201fa | 2020-10-28 14:17:10 | [diff] [blame] | 7521 | void WebContentsImpl::SetWindowRect(const gfx::Rect& new_bounds) { |
| 7522 | OPTIONAL_TRACE_EVENT0("content", "WebContentsImpl::SetWindowRect"); |
Mike Wasserman | dabad61 | 2020-08-13 04:26:27 | [diff] [blame] | 7523 | if (!delegate_) |
| 7524 | return; |
| 7525 | |
| 7526 | // Members of |new_bounds| may be 0 to indicate uninitialized values for newly |
| 7527 | // opened windows, even if the |GetContainerBounds()| inner rect is correct. |
| 7528 | // TODO(crbug.com/897300): Plumb values as specified; fallback on outer rect. |
| 7529 | auto bounds = new_bounds; |
| 7530 | if (bounds.IsEmpty()) |
| 7531 | bounds.set_size(GetContainerBounds().size()); |
| 7532 | |
| 7533 | // Only requests from the main frame, not subframes, should reach this code. |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 7534 | int64_t display_id = |
| 7535 | AdjustRequestedWindowBounds(&bounds, GetPrimaryMainFrame()); |
Mike Wasserman | b6bc015 | 2020-08-25 22:46:20 | [diff] [blame] | 7536 | |
| 7537 | // Drop fullscreen when placing a WebContents to prohibit deceptive behavior. |
| 7538 | // Only drop fullscreen on the specific destination display, which is known. |
Brad Triebwasser | 5eb94a9 | 2022-10-13 22:34:28 | [diff] [blame] | 7539 | // This supports sites using cross-screen window management capabilities to |
Mike Wasserman | b6bc015 | 2020-08-25 22:46:20 | [diff] [blame] | 7540 | // retain fullscreen and place a window on another screen. |
| 7541 | ForSecurityDropFullscreen(display_id).RunAndReset(); |
| 7542 | |
Mike Wasserman | dabad61 | 2020-08-13 04:26:27 | [diff] [blame] | 7543 | delegate_->SetContentsBounds(this, bounds); |
[email protected] | 420ae01 | 2009-04-24 05:16:32 | [diff] [blame] | 7544 | } |
| 7545 | |
Lingqi Chi | b2f44200 | 2021-07-29 01:53:54 | [diff] [blame] | 7546 | void WebContentsImpl::UpdateWindowPreferredSize(const gfx::Size& pref_size) { |
| 7547 | OPTIONAL_TRACE_EVENT0("content", "WebContentsImpl::UpdatePreferredSize"); |
| 7548 | const gfx::Size old_size = GetPreferredSize(); |
| 7549 | preferred_size_ = pref_size; |
| 7550 | OnPreferredSizeChanged(old_size); |
| 7551 | } |
| 7552 | |
Camille Lamy | 8766bd3e | 2020-11-26 11:36:32 | [diff] [blame] | 7553 | std::vector<RenderFrameHostImpl*> |
| 7554 | WebContentsImpl::GetActiveTopLevelDocumentsInBrowsingContextGroup( |
| 7555 | RenderFrameHostImpl* render_frame_host) { |
| 7556 | std::vector<RenderFrameHostImpl*> out; |
| 7557 | for (WebContentsImpl* web_contents : GetAllWebContents()) { |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 7558 | RenderFrameHostImpl* other_render_frame_host = |
| 7559 | web_contents->GetPrimaryMainFrame(); |
Camille Lamy | 8766bd3e | 2020-11-26 11:36:32 | [diff] [blame] | 7560 | |
| 7561 | // Filters out inactive documents. |
| 7562 | if (other_render_frame_host->lifecycle_state() != |
Sreeja Kamishetty | 299329ad | 2021-03-25 14:06:01 | [diff] [blame] | 7563 | RenderFrameHostImpl::LifecycleStateImpl::kActive) { |
Camille Lamy | 8766bd3e | 2020-11-26 11:36:32 | [diff] [blame] | 7564 | continue; |
| 7565 | } |
| 7566 | |
| 7567 | // Filters out documents from a different browsing context group. |
| 7568 | if (!render_frame_host->GetSiteInstance()->IsRelatedSiteInstance( |
| 7569 | other_render_frame_host->GetSiteInstance())) { |
| 7570 | continue; |
| 7571 | } |
| 7572 | |
| 7573 | out.push_back(other_render_frame_host); |
| 7574 | } |
| 7575 | return out; |
| 7576 | } |
| 7577 | |
Hiroki Nakagawa | 1cb1455 | 2021-04-23 03:37:34 | [diff] [blame] | 7578 | PrerenderHostRegistry* WebContentsImpl::GetPrerenderHostRegistry() { |
Hiroki Nakagawa | 1cb1455 | 2021-04-23 03:37:34 | [diff] [blame] | 7579 | DCHECK(prerender_host_registry_); |
| 7580 | return prerender_host_registry_.get(); |
| 7581 | } |
| 7582 | |
fdegans | a696e511 | 2015-04-17 01:57:59 | [diff] [blame] | 7583 | void WebContentsImpl::DidStartLoading(FrameTreeNode* frame_tree_node, |
Nate Chapin | 9aabf5f | 2021-11-12 00:31:19 | [diff] [blame] | 7584 | bool should_show_loading_ui) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 7585 | OPTIONAL_TRACE_EVENT1("content", "WebContentsImpl::DidStartLoading", |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 7586 | "frame_tree_node", frame_tree_node); |
Sreeja Kamishetty | 15f9944a2 | 2022-03-10 10:16:08 | [diff] [blame] | 7587 | LoadingStateChanged( |
| 7588 | frame_tree_node->GetFrameType() == FrameType::kPrimaryMainFrame && |
| 7589 | should_show_loading_ui, |
| 7590 | nullptr); |
dmazzoni | bf8cec4 | 2015-02-08 08:28:08 | [diff] [blame] | 7591 | |
Xiyuan Xia | 2a66b11 | 2018-12-06 15:52:16 | [diff] [blame] | 7592 | // Reset the focus state from DidStartNavigation to false if a new load starts |
| 7593 | // afterward, in case loading logic triggers a FocusLocationBarByDefault call. |
| 7594 | should_focus_location_bar_by_default_ = false; |
Ian Prest | cdd843d | 2020-01-28 18:59:58 | [diff] [blame] | 7595 | |
| 7596 | // Notify accessibility that the user is navigating away from the |
| 7597 | // current document. |
| 7598 | // TODO(domfarolino, dmazzoni): Do this using WebContentsObserver. See |
| 7599 | // https://crbug.com/981271. |
| 7600 | BrowserAccessibilityManager* manager = |
| 7601 | frame_tree_node->current_frame_host()->browser_accessibility_manager(); |
| 7602 | if (manager) |
| 7603 | manager->UserIsNavigatingAway(); |
[email protected] | 420ae01 | 2009-04-24 05:16:32 | [diff] [blame] | 7604 | } |
| 7605 | |
fdegans | c73eec2 | 2015-03-19 14:47:54 | [diff] [blame] | 7606 | void WebContentsImpl::DidStopLoading() { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 7607 | OPTIONAL_TRACE_EVENT0("content", "WebContentsImpl::DidStopLoading"); |
dcheng | 5971627 | 2016-04-09 05:19:08 | [diff] [blame] | 7608 | std::unique_ptr<LoadNotificationDetails> details; |
[email protected] | 420ae01 | 2009-04-24 05:16:32 | [diff] [blame] | 7609 | |
[email protected] | 9595fd8 | 2013-04-19 21:28:49 | [diff] [blame] | 7610 | // Use the last committed entry rather than the active one, in case a |
| 7611 | // pending entry has been created. |
Carlos Caballero | 40b0efd | 2021-01-26 11:55:00 | [diff] [blame] | 7612 | NavigationEntry* entry = GetController().GetLastCommittedEntry(); |
[email protected] | 9595fd8 | 2013-04-19 21:28:49 | [diff] [blame] | 7613 | |
[email protected] | 420ae01 | 2009-04-24 05:16:32 | [diff] [blame] | 7614 | // An entry may not exist for a stop when loading an initial blank page or |
| 7615 | // if an iframe injected by script into a blank page finishes loading. |
| 7616 | if (entry) { |
Fergal Daly | 55b6d72 | 2020-09-11 07:56:33 | [diff] [blame] | 7617 | details = std::make_unique<LoadNotificationDetails>( |
Carlos Caballero | 40b0efd | 2021-01-26 11:55:00 | [diff] [blame] | 7618 | entry->GetVirtualURL(), &GetController(), |
| 7619 | GetController().GetCurrentEntryIndex()); |
[email protected] | 420ae01 | 2009-04-24 05:16:32 | [diff] [blame] | 7620 | } |
| 7621 | |
Nate Chapin | 9aabf5f | 2021-11-12 00:31:19 | [diff] [blame] | 7622 | LoadingStateChanged(true /* should_show_loading_ui */, details.get()); |
[email protected] | 420ae01 | 2009-04-24 05:16:32 | [diff] [blame] | 7623 | } |
| 7624 | |
Yeunjoo Choi | 8a64db30 | 2022-08-25 02:29:28 | [diff] [blame] | 7625 | void WebContentsImpl::DidChangeLoadProgressForPrimaryMainFrame() { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 7626 | OPTIONAL_TRACE_EVENT0("content", "WebContentsImpl::DidChangeLoadProgress"); |
Takashi Toyoshima | ea534ef2 | 2021-07-21 03:27:59 | [diff] [blame] | 7627 | if (IsBeingDestroyed()) |
Nate Chapin | 22ea659 | 2019-03-05 22:29:02 | [diff] [blame] | 7628 | return; |
Sreeja Kamishetty | 0be3b1b | 2021-08-12 17:04:15 | [diff] [blame] | 7629 | double load_progress = GetLoadProgress(); |
fdegans | a696e511 | 2015-04-17 01:57:59 | [diff] [blame] | 7630 | |
| 7631 | // The delegate is notified immediately for the first and last updates. Also, |
| 7632 | // since the message loop may be pretty busy when a page is loaded, it might |
| 7633 | // not execute a posted task in a timely manner so the progress report is sent |
| 7634 | // immediately if enough time has passed. |
Sreeja Kamishetty | 0be3b1b | 2021-08-12 17:04:15 | [diff] [blame] | 7635 | base::TimeDelta min_delay = minimum_delay_between_loading_updates_ms_; |
Ian Vollick | 6d75ac3 | 2021-05-05 17:45:09 | [diff] [blame] | 7636 | bool delay_elapsed = |
| 7637 | loading_last_progress_update_.is_null() || |
fdegans | a696e511 | 2015-04-17 01:57:59 | [diff] [blame] | 7638 | base::TimeTicks::Now() - loading_last_progress_update_ > min_delay; |
| 7639 | |
| 7640 | if (load_progress == 0.0 || load_progress == 1.0 || delay_elapsed) { |
| 7641 | // If there is a pending task to send progress, it is now obsolete. |
| 7642 | loading_weak_factory_.InvalidateWeakPtrs(); |
| 7643 | |
| 7644 | // Notify the load progress change. |
| 7645 | SendChangeLoadProgress(); |
| 7646 | |
| 7647 | // Clean-up the states if needed. |
| 7648 | if (load_progress == 1.0) |
| 7649 | ResetLoadProgressState(); |
| 7650 | return; |
| 7651 | } |
| 7652 | |
| 7653 | if (loading_weak_factory_.HasWeakPtrs()) |
| 7654 | return; |
| 7655 | |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 7656 | if (min_delay == base::Milliseconds(0)) { |
Sreeja Kamishetty | 0be3b1b | 2021-08-12 17:04:15 | [diff] [blame] | 7657 | SendChangeLoadProgress(); |
| 7658 | } else { |
Sean Maher | 5b9af51f | 2022-11-21 15:32:47 | [diff] [blame] | 7659 | base::SingleThreadTaskRunner::GetCurrentDefault()->PostDelayedTask( |
Sreeja Kamishetty | 0be3b1b | 2021-08-12 17:04:15 | [diff] [blame] | 7660 | FROM_HERE, |
| 7661 | base::BindOnce(&WebContentsImpl::SendChangeLoadProgress, |
| 7662 | loading_weak_factory_.GetWeakPtr()), |
| 7663 | min_delay); |
| 7664 | } |
fdegans | a696e511 | 2015-04-17 01:57:59 | [diff] [blame] | 7665 | } |
| 7666 | |
Tsuyoshi Horo | cd82846 | 2021-04-21 04:59:58 | [diff] [blame] | 7667 | bool WebContentsImpl::IsHidden() { |
| 7668 | return GetPageVisibilityState() == PageVisibilityState::kHidden; |
| 7669 | } |
| 7670 | |
avi | d6d88b91 | 2017-01-13 00:16:00 | [diff] [blame] | 7671 | std::vector<std::unique_ptr<NavigationThrottle>> |
| 7672 | WebContentsImpl::CreateThrottlesForNavigation( |
clamy | cbf524d4 | 2016-09-27 12:48:35 | [diff] [blame] | 7673 | NavigationHandle* navigation_handle) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 7674 | OPTIONAL_TRACE_EVENT1("content", |
| 7675 | "WebContentsImpl::CreateThrottlesForNavigation", |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 7676 | "navigation", navigation_handle); |
Charlie Reis | 9607736 | 2019-07-04 00:34:04 | [diff] [blame] | 7677 | auto throttles = GetContentClient()->browser()->CreateThrottlesForNavigation( |
clamy | cbf524d4 | 2016-09-27 12:48:35 | [diff] [blame] | 7678 | navigation_handle); |
Charlie Reis | 9607736 | 2019-07-04 00:34:04 | [diff] [blame] | 7679 | |
Charlie Reis | 9607736 | 2019-07-04 00:34:04 | [diff] [blame] | 7680 | return throttles; |
clamy | cbf524d4 | 2016-09-27 12:48:35 | [diff] [blame] | 7681 | } |
| 7682 | |
David Bokan | 1bdb3701f | 2021-04-30 22:02:35 | [diff] [blame] | 7683 | std::vector<std::unique_ptr<CommitDeferringCondition>> |
| 7684 | WebContentsImpl::CreateDeferringConditionsForNavigationCommit( |
David Bokan | 51a6df83 | 2022-02-17 18:31:19 | [diff] [blame] | 7685 | NavigationHandle& navigation_handle, |
| 7686 | CommitDeferringCondition::NavigationType type) { |
| 7687 | OPTIONAL_TRACE_EVENT2( |
| 7688 | "content", "WebContentsImpl::CreateDeferringConditionsForNavigation", |
| 7689 | "navigation", navigation_handle, "NavigationType", type); |
| 7690 | std::vector<std::unique_ptr<CommitDeferringCondition>> conditions = |
| 7691 | GetContentClient() |
| 7692 | ->browser() |
| 7693 | ->CreateCommitDeferringConditionsForNavigation(&navigation_handle, |
| 7694 | type); |
| 7695 | |
David Bokan | 1bdb3701f | 2021-04-30 22:02:35 | [diff] [blame] | 7696 | if (auto condition = JavaScriptDialogCommitDeferringCondition::MaybeCreate( |
| 7697 | static_cast<NavigationRequest&>(navigation_handle))) |
| 7698 | conditions.push_back(std::move(condition)); |
| 7699 | return conditions; |
| 7700 | } |
| 7701 | |
clamy | 1e5574e9 | 2016-09-29 16:48:44 | [diff] [blame] | 7702 | std::unique_ptr<NavigationUIData> WebContentsImpl::GetNavigationUIData( |
| 7703 | NavigationHandle* navigation_handle) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 7704 | OPTIONAL_TRACE_EVENT0("content", "WebContentsImpl::GetNavigationUIData"); |
clamy | 1e5574e9 | 2016-09-29 16:48:44 | [diff] [blame] | 7705 | return GetContentClient()->browser()->GetNavigationUIData(navigation_handle); |
| 7706 | } |
| 7707 | |
W. James MacLean | c07dc41b | 2022-07-25 18:52:16 | [diff] [blame] | 7708 | void WebContentsImpl::RegisterExistingOriginAsHavingDefaultIsolation( |
W. James MacLean | b70fab8 | 2020-05-01 18:51:14 | [diff] [blame] | 7709 | const url::Origin& origin, |
| 7710 | NavigationRequest* navigation_request_to_exclude) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 7711 | OPTIONAL_TRACE_EVENT2( |
| 7712 | "content", |
W. James MacLean | c07dc41b | 2022-07-25 18:52:16 | [diff] [blame] | 7713 | "WebContentsImpl::RegisterExistingOriginAsHavingDefaultIsolation", |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 7714 | "origin", origin, "navigation_request_to_exclude", |
| 7715 | navigation_request_to_exclude); |
W. James MacLean | 1c40862c | 2020-04-27 21:05:57 | [diff] [blame] | 7716 | // Note: This function can be made static if we ever need call it without |
| 7717 | // a WebContentsImpl instance, in which case we can use a wrapper to |
| 7718 | // implement the override from NavigatorDelegate. |
| 7719 | for (WebContentsImpl* web_contents : GetAllWebContents()) { |
W. James MacLean | 17cc71f | 2020-11-12 21:20:12 | [diff] [blame] | 7720 | // We only need to search entries in the same BrowserContext as us. |
| 7721 | if (web_contents->GetBrowserContext() != GetBrowserContext()) |
| 7722 | continue; |
Carlos Caballero | 40b0efd | 2021-01-26 11:55:00 | [diff] [blame] | 7723 | |
W. James MacLean | c6dc86c | 2021-08-12 13:58:37 | [diff] [blame] | 7724 | // Walk the frame trees to notify each one's NavigationController about the |
W. James MacLean | c07dc41b | 2022-07-25 18:52:16 | [diff] [blame] | 7725 | // opting-in or opting-out `origin`, and also pick up any frames without |
W. James MacLean | c6dc86c | 2021-08-12 13:58:37 | [diff] [blame] | 7726 | // FrameNavigationEntries. |
W. James MacLean | b70fab8 | 2020-05-01 18:51:14 | [diff] [blame] | 7727 | // * Some frames won't have FrameNavigationEntries (Issues 524208, 608402). |
| 7728 | // * Some pending navigations won't have NavigationEntries. |
W. James MacLean | c6dc86c | 2021-08-12 13:58:37 | [diff] [blame] | 7729 | web_contents->ForEachFrameTree(base::BindRepeating( |
| 7730 | [](const url::Origin& origin, |
| 7731 | NavigationRequest* navigation_request_to_exclude, |
Arthur Sonzogni | f6785ec | 2022-12-05 10:11:50 | [diff] [blame] | 7732 | FrameTree& frame_tree) { |
| 7733 | frame_tree.RegisterExistingOriginAsHavingDefaultIsolation( |
W. James MacLean | c6dc86c | 2021-08-12 13:58:37 | [diff] [blame] | 7734 | origin, navigation_request_to_exclude); |
| 7735 | }, |
| 7736 | origin, navigation_request_to_exclude)); |
W. James MacLean | 1c40862c | 2020-04-27 21:05:57 | [diff] [blame] | 7737 | } |
| 7738 | } |
| 7739 | |
Kevin McNee | fb86fcf | 2021-02-26 23:20:57 | [diff] [blame] | 7740 | void WebContentsImpl::DidChangeName(RenderFrameHostImpl* render_frame_host, |
alexmos | be2f4c3 | 2015-03-10 02:30:23 | [diff] [blame] | 7741 | const std::string& name) { |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 7742 | OPTIONAL_TRACE_EVENT2("content", "WebContentsImpl::DidChangeName", |
| 7743 | "render_frame_host", render_frame_host, "name", name); |
Andrew Grieve | e19cd93e | 2021-01-22 05:43:06 | [diff] [blame] | 7744 | observers_.NotifyObservers(&WebContentsObserver::FrameNameChanged, |
| 7745 | render_frame_host, name); |
alexmos | be2f4c3 | 2015-03-10 02:30:23 | [diff] [blame] | 7746 | } |
| 7747 | |
John Delaney | b625dca9 | 2021-04-14 17:00:34 | [diff] [blame] | 7748 | void WebContentsImpl::DidReceiveUserActivation( |
Kevin McNee | fb86fcf | 2021-02-26 23:20:57 | [diff] [blame] | 7749 | RenderFrameHostImpl* render_frame_host) { |
John Delaney | b625dca9 | 2021-04-14 17:00:34 | [diff] [blame] | 7750 | OPTIONAL_TRACE_EVENT1("content", "WebContentsImpl::DidReceiveUserActivation", |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 7751 | "render_frame_host", render_frame_host); |
John Delaney | b625dca9 | 2021-04-14 17:00:34 | [diff] [blame] | 7752 | observers_.NotifyObservers(&WebContentsObserver::FrameReceivedUserActivation, |
| 7753 | render_frame_host); |
John Delaney | edd8d6c | 2019-01-25 00:23:57 | [diff] [blame] | 7754 | } |
| 7755 | |
Robert Sesek | c4eeccbd | 2021-07-23 21:20:19 | [diff] [blame] | 7756 | void WebContentsImpl::BindDisplayCutoutHost( |
| 7757 | RenderFrameHostImpl* render_frame_host, |
| 7758 | mojo::PendingAssociatedReceiver<blink::mojom::DisplayCutoutHost> receiver) { |
| 7759 | if (display_cutout_host_impl_) { |
| 7760 | display_cutout_host_impl_->BindReceiver(std::move(receiver), |
| 7761 | render_frame_host); |
| 7762 | } |
| 7763 | } |
| 7764 | |
Kevin McNee | fb86fcf | 2021-02-26 23:20:57 | [diff] [blame] | 7765 | void WebContentsImpl::DidChangeDisplayState( |
| 7766 | RenderFrameHostImpl* render_frame_host, |
| 7767 | bool is_display_none) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 7768 | OPTIONAL_TRACE_EVENT2("content", "WebContentsImpl::DidChangeDisplayState", |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 7769 | "render_frame_host", render_frame_host, |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 7770 | "is_display_none", is_display_none); |
Andrew Grieve | e19cd93e | 2021-01-22 05:43:06 | [diff] [blame] | 7771 | observers_.NotifyObservers(&WebContentsObserver::FrameDisplayStateChanged, |
| 7772 | render_frame_host, is_display_none); |
John Delaney | 0146a74 | 2019-01-25 19:25:28 | [diff] [blame] | 7773 | } |
| 7774 | |
Kevin McNee | fb86fcf | 2021-02-26 23:20:57 | [diff] [blame] | 7775 | void WebContentsImpl::FrameSizeChanged(RenderFrameHostImpl* render_frame_host, |
John Delaney | 0146a74 | 2019-01-25 19:25:28 | [diff] [blame] | 7776 | const gfx::Size& frame_size) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 7777 | OPTIONAL_TRACE_EVENT1("content", "WebContentsImpl::FrameSizeChanged", |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 7778 | "render_frame_host", render_frame_host); |
Andrew Grieve | e19cd93e | 2021-01-22 05:43:06 | [diff] [blame] | 7779 | observers_.NotifyObservers(&WebContentsObserver::FrameSizeChanged, |
| 7780 | render_frame_host, frame_size); |
John Delaney | 0146a74 | 2019-01-25 19:25:28 | [diff] [blame] | 7781 | } |
| 7782 | |
[email protected] | 39f2f925 | 2014-05-03 00:54:01 | [diff] [blame] | 7783 | void WebContentsImpl::DocumentOnLoadCompleted( |
Kevin McNee | fb86fcf | 2021-02-26 23:20:57 | [diff] [blame] | 7784 | RenderFrameHostImpl* render_frame_host) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 7785 | OPTIONAL_TRACE_EVENT1("content", "WebContentsImpl::DocumentOnLoadCompleted", |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 7786 | "render_frame_host", render_frame_host); |
Sreeja Kamishetty | 6b55ecc | 2021-12-10 11:28:17 | [diff] [blame] | 7787 | DCHECK(render_frame_host->IsInPrimaryMainFrame()); |
Jeremy Roman | d597d337 | 2021-07-09 23:36:56 | [diff] [blame] | 7788 | ShowInsecureLocalhostWarningIfNeeded(render_frame_host->GetPage()); |
falken | 52a56e3 | 2016-12-08 05:02:40 | [diff] [blame] | 7789 | |
Andrew Grieve | e19cd93e | 2021-01-22 05:43:06 | [diff] [blame] | 7790 | observers_.NotifyObservers( |
Sreeja Kamishetty | 8134758 | 2022-01-06 12:46:33 | [diff] [blame] | 7791 | &WebContentsObserver::DocumentOnLoadCompletedInPrimaryMainFrame); |
[email protected] | 2549749 | 2010-09-11 15:15:08 | [diff] [blame] | 7792 | } |
| 7793 | |
Kevin McNee | fb86fcf | 2021-02-26 23:20:57 | [diff] [blame] | 7794 | void WebContentsImpl::UpdateTitle(RenderFrameHostImpl* render_frame_host, |
Jan Wilken Dörrie | aace0cfef | 2021-03-11 22:01:58 | [diff] [blame] | 7795 | const std::u16string& title, |
[email protected] | ef3adfc | 2014-05-11 00:04:54 | [diff] [blame] | 7796 | base::i18n::TextDirection title_direction) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 7797 | OPTIONAL_TRACE_EVENT2("content", "WebContentsImpl::UpdateTitle", |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 7798 | "render_frame_host", render_frame_host, "title", title); |
[email protected] | ef3adfc | 2014-05-11 00:04:54 | [diff] [blame] | 7799 | // Try to find the navigation entry, which might not be the current one. |
Adithya Srinivasan | 9b0c99c | 2021-08-10 15:19:45 | [diff] [blame] | 7800 | // For example, it might be from a pending deletion RFH, or it might be from a |
| 7801 | // non-primary frame tree. |
Carlos Caballero | ede6f8c | 2021-01-28 11:01:50 | [diff] [blame] | 7802 | NavigationEntryImpl* entry = |
Kevin McNee | fb86fcf | 2021-02-26 23:20:57 | [diff] [blame] | 7803 | render_frame_host->frame_tree()->controller().GetEntryWithUniqueID( |
| 7804 | render_frame_host->nav_entry_id()); |
[email protected] | ef3adfc | 2014-05-11 00:04:54 | [diff] [blame] | 7805 | |
Rakina Zata Amni | 46087a1 | 2022-11-11 08:28:38 | [diff] [blame] | 7806 | if (!entry) { |
| 7807 | // We can handle title updates with no matching NavigationEntry, but only if |
| 7808 | // the update is for the initial NavigationEntry. In that case the initial |
| 7809 | // empty document RFH wouldn't have a `nav_entry_id` set because it hasn't |
| 7810 | // committed any navigation. Note that if the title update came from the |
| 7811 | // initial empty document but the WebContents is doing a session restore, |
| 7812 | // we will ignore the title update (because GetLastCommittedEntry() would |
| 7813 | // return the non-initial restored entry), which avoids accidental |
| 7814 | // overwriting of the restored entry's title. |
| 7815 | if (render_frame_host->GetParent() || !render_frame_host->frame_tree() |
| 7816 | ->controller() |
| 7817 | .GetLastCommittedEntry() |
| 7818 | ->IsInitialEntry()) { |
Rakina Zata Amni | 2322f4f8 | 2022-01-24 13:24:24 | [diff] [blame] | 7819 | return; |
Rakina Zata Amni | 46087a1 | 2022-11-11 08:28:38 | [diff] [blame] | 7820 | } |
| 7821 | entry = |
| 7822 | render_frame_host->frame_tree()->controller().GetLastCommittedEntry(); |
Rakina Zata Amni | afd3c658 | 2021-11-30 06:19:17 | [diff] [blame] | 7823 | } |
[email protected] | ef3adfc | 2014-05-11 00:04:54 | [diff] [blame] | 7824 | |
| 7825 | // TODO(evan): make use of title_direction. |
| 7826 | // http://code.google.com/p/chromium/issues/detail?id=27094 |
Adithya Srinivasan | 9b0c99c | 2021-08-10 15:19:45 | [diff] [blame] | 7827 | bool title_changed = UpdateTitleForEntryImpl(entry, title); |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 7828 | if (title_changed && render_frame_host == GetPrimaryMainFrame()) { |
Adithya Srinivasan | 9b0c99c | 2021-08-10 15:19:45 | [diff] [blame] | 7829 | NotifyTitleUpdateForEntry(entry); |
| 7830 | } |
[email protected] | ef3adfc | 2014-05-11 00:04:54 | [diff] [blame] | 7831 | } |
| 7832 | |
Kevin McNee | fb86fcf | 2021-02-26 23:20:57 | [diff] [blame] | 7833 | void WebContentsImpl::UpdateTargetURL(RenderFrameHostImpl* render_frame_host, |
Mario Sanchez Prada | dffbe66e | 2020-09-07 12:01:08 | [diff] [blame] | 7834 | const GURL& url) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 7835 | OPTIONAL_TRACE_EVENT2("content", "WebContentsImpl::UpdateTargetURL", |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 7836 | "render_frame_host", render_frame_host, "url", url); |
Mario Sanchez Prada | dffbe66e | 2020-09-07 12:01:08 | [diff] [blame] | 7837 | // In case of racey updates from multiple RenderViewHosts, the last URL should |
| 7838 | // be shown - see also some discussion in https://crbug.com/807776. |
| 7839 | if (!url.is_valid() && render_frame_host != frame_that_set_last_target_url_) |
| 7840 | return; |
| 7841 | frame_that_set_last_target_url_ = |
| 7842 | url.is_valid() ? render_frame_host : nullptr; |
| 7843 | |
| 7844 | if (delegate_) |
| 7845 | delegate_->UpdateTargetURL(this, url); |
| 7846 | } |
| 7847 | |
alexmos | e7da5a1 | 2015-04-09 02:22:16 | [diff] [blame] | 7848 | bool WebContentsImpl::ShouldRouteMessageEvent( |
Sharon Yang | 7298ae80 | 2022-02-25 03:50:15 | [diff] [blame] | 7849 | RenderFrameHostImpl* target_rfh) const { |
| 7850 | OPTIONAL_TRACE_EVENT1("content", "WebContentsImpl::ShouldRouteMessageEvent", |
| 7851 | "render_frame_host", target_rfh); |
alexmos | e7da5a1 | 2015-04-09 02:22:16 | [diff] [blame] | 7852 | // Allow the message if this WebContents is dedicated to a browser plugin |
| 7853 | // guest. |
[email protected] | 0eba810b | 2012-10-18 03:19:36 | [diff] [blame] | 7854 | // Note: This check means that an embedder could theoretically receive a |
| 7855 | // postMessage from anyone (not just its own guests). However, this is |
| 7856 | // probably not a risk for apps since other pages won't have references |
| 7857 | // to App windows. |
alexmos | e7da5a1 | 2015-04-09 02:22:16 | [diff] [blame] | 7858 | return GetBrowserPluginGuest() || GetBrowserPluginEmbedder(); |
| 7859 | } |
[email protected] | f546640b | 2012-05-15 00:03:49 | [diff] [blame] | 7860 | |
Kevin McNee | fb86fcf | 2021-02-26 23:20:57 | [diff] [blame] | 7861 | void WebContentsImpl::EnsureOpenerProxiesExist( |
| 7862 | RenderFrameHostImpl* source_rfh) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 7863 | OPTIONAL_TRACE_EVENT1("content", "WebContentsImpl::EnsureOpenerProxiesExist", |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 7864 | "render_frame_host", source_rfh); |
alexmos | e7da5a1 | 2015-04-09 02:22:16 | [diff] [blame] | 7865 | WebContentsImpl* source_web_contents = static_cast<WebContentsImpl*>( |
| 7866 | WebContents::FromRenderFrameHost(source_rfh)); |
[email protected] | f546640b | 2012-05-15 00:03:49 | [diff] [blame] | 7867 | |
alexmos | e7da5a1 | 2015-04-09 02:22:16 | [diff] [blame] | 7868 | if (source_web_contents) { |
lazyboy | 6ec48b2a | 2015-06-29 15:18:14 | [diff] [blame] | 7869 | // If this message is going to outer WebContents from inner WebContents, |
| 7870 | // then we should not create a RenderView. AttachToOuterWebContentsFrame() |
| 7871 | // already created a RenderFrameProxyHost for that purpose. |
| 7872 | if (GetBrowserPluginEmbedder() && |
W. James MacLean | 2539adb3 | 2019-12-13 00:40:44 | [diff] [blame] | 7873 | source_web_contents->browser_plugin_guest_) { |
lazyboy | 6ec48b2a | 2015-06-29 15:18:14 | [diff] [blame] | 7874 | return; |
W. James MacLean | 2539adb3 | 2019-12-13 00:40:44 | [diff] [blame] | 7875 | } |
lazyboy | 6ec48b2a | 2015-06-29 15:18:14 | [diff] [blame] | 7876 | |
lfg | 812874d | 2016-02-09 22:26:13 | [diff] [blame] | 7877 | if (this != source_web_contents && GetBrowserPluginGuest()) { |
nasko | b3041b98a4 | 2016-03-12 04:43:06 | [diff] [blame] | 7878 | // We create a RenderFrameProxyHost for the embedder in the guest's render |
| 7879 | // process but we intentionally do not expose the embedder's opener chain |
| 7880 | // to it. |
| 7881 | source_web_contents->GetRenderManager()->CreateRenderFrameProxy( |
Harkiran Bolaria | 2912a6b3 | 2022-02-22 16:43:45 | [diff] [blame] | 7882 | GetSiteInstance(), |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 7883 | source_web_contents->GetPrimaryMainFrame()->browsing_context_state()); |
[email protected] | f546640b | 2012-05-15 00:03:49 | [diff] [blame] | 7884 | } else { |
Kevin McNee | fb86fcf | 2021-02-26 23:20:57 | [diff] [blame] | 7885 | source_rfh->frame_tree_node()->render_manager()->CreateOpenerProxies( |
Harkiran Bolaria | 2912a6b3 | 2022-02-22 16:43:45 | [diff] [blame] | 7886 | GetSiteInstance(), nullptr, source_rfh->browsing_context_state()); |
[email protected] | f546640b | 2012-05-15 00:03:49 | [diff] [blame] | 7887 | } |
| 7888 | } |
[email protected] | f546640b | 2012-05-15 00:03:49 | [diff] [blame] | 7889 | } |
| 7890 | |
avallee | 9993fca | 2016-11-17 06:16:50 | [diff] [blame] | 7891 | void WebContentsImpl::SetAsFocusedWebContentsIfNecessary() { |
Carlos Caballero | 1a261f8 | 2021-11-04 15:54:03 | [diff] [blame] | 7892 | SetFocusedFrameTree(&GetPrimaryFrameTree()); |
Dave Tapuska | 54c76a03 | 2021-10-27 22:10:42 | [diff] [blame] | 7893 | } |
| 7894 | |
| 7895 | void WebContentsImpl::SetFocusedFrameTree(FrameTree* frame_tree_to_focus) { |
| 7896 | OPTIONAL_TRACE_EVENT0("content", "WebContentsImpl::SetFocusedFrameTree"); |
Adithya Srinivasan | 11c3d6ba0 | 2021-02-12 20:25:10 | [diff] [blame] | 7897 | DCHECK(!GetOuterWebContents() || !IsPortal()); |
Dave Tapuska | 54c76a03 | 2021-10-27 22:10:42 | [diff] [blame] | 7898 | DCHECK(frame_tree_to_focus); |
| 7899 | |
avallee | 9993fca | 2016-11-17 06:16:50 | [diff] [blame] | 7900 | // Only change focus if we are not currently focused. |
Dave Tapuska | 54c76a03 | 2021-10-27 22:10:42 | [diff] [blame] | 7901 | FrameTree* old_focused_frame_tree = GetFocusedFrameTree(); |
| 7902 | if (old_focused_frame_tree == frame_tree_to_focus) |
avallee | 9993fca | 2016-11-17 06:16:50 | [diff] [blame] | 7903 | return; |
| 7904 | |
Dave Tapuska | 54c76a03 | 2021-10-27 22:10:42 | [diff] [blame] | 7905 | GetOutermostWebContents()->node_.SetFocusedFrameTree(frame_tree_to_focus); |
paulmeyer | feafc2d | 2017-04-25 21:46:40 | [diff] [blame] | 7906 | |
Dave Tapuska | 54c76a03 | 2021-10-27 22:10:42 | [diff] [blame] | 7907 | // Send a page level blur to the `old_focused_frame_tree` so that it displays |
| 7908 | // inactive UI and focus `frame_tree_to_focus` to activate it. |
| 7909 | if (old_focused_frame_tree) { |
| 7910 | old_focused_frame_tree->root() |
| 7911 | ->current_frame_host() |
| 7912 | ->GetRenderWidgetHost() |
| 7913 | ->SetPageFocus(false); |
| 7914 | } |
avallee | c2ff1ef | 2017-01-21 00:12:30 | [diff] [blame] | 7915 | |
Dave Tapuska | 54c76a03 | 2021-10-27 22:10:42 | [diff] [blame] | 7916 | // Make sure the outer frame trees knows our frame is focused. Otherwise, the |
avallee | c2ff1ef | 2017-01-21 00:12:30 | [diff] [blame] | 7917 | // outer renderer could have the element before or after the frame element |
| 7918 | // focused which would return early without actually advancing focus. |
Kevin McNee | 088a1b5 | 2022-07-13 23:15:37 | [diff] [blame] | 7919 | RenderFrameHostImpl::UpdateAXFocusDeferScope focus_defer_scope( |
| 7920 | *frame_tree_to_focus->root() |
| 7921 | ->current_frame_host() |
| 7922 | ->GetOutermostMainFrameOrEmbedder()); |
Dave Tapuska | 54c76a03 | 2021-10-27 22:10:42 | [diff] [blame] | 7923 | frame_tree_to_focus->FocusOuterFrameTrees(); |
avallee | c2ff1ef | 2017-01-21 00:12:30 | [diff] [blame] | 7924 | |
Dave Tapuska | 54c76a03 | 2021-10-27 22:10:42 | [diff] [blame] | 7925 | frame_tree_to_focus->root() |
| 7926 | ->current_frame_host() |
| 7927 | ->GetRenderWidgetHost() |
| 7928 | ->SetPageFocus(true); |
avallee | 9993fca | 2016-11-17 06:16:50 | [diff] [blame] | 7929 | } |
| 7930 | |
avallee | 0206f78 | 2016-07-28 18:55:33 | [diff] [blame] | 7931 | void WebContentsImpl::SetFocusedFrame(FrameTreeNode* node, |
Sharon Yang | efe5263 | 2022-03-08 23:06:06 | [diff] [blame] | 7932 | SiteInstanceGroup* source) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 7933 | OPTIONAL_TRACE_EVENT2("content", "WebContentsImpl::SetFocusedFrame", |
Sharon Yang | efe5263 | 2022-03-08 23:06:06 | [diff] [blame] | 7934 | "frame_tree_node", node, "source_site_instance_group", |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 7935 | source); |
Kevin McNee | 088a1b5 | 2022-07-13 23:15:37 | [diff] [blame] | 7936 | |
| 7937 | // Focus will not be in a consistent state until the focused frame tree is |
| 7938 | // also updated (if necessary). |
| 7939 | RenderFrameHostImpl::UpdateAXFocusDeferScope focus_defer_scope( |
| 7940 | *node->current_frame_host()->GetOutermostMainFrameOrEmbedder()); |
| 7941 | |
Arthur Sonzogni | f6785ec | 2022-12-05 10:11:50 | [diff] [blame] | 7942 | node->frame_tree().SetFocusedFrame(node, source); |
avallee | d68cdd1 | 2017-06-21 22:07:27 | [diff] [blame] | 7943 | |
Dave Tapuska | 54c76a03 | 2021-10-27 22:10:42 | [diff] [blame] | 7944 | auto* inner_contents = node_.GetInnerWebContentsInFrame(node); |
| 7945 | |
| 7946 | // We currently do not need to descend into inner frame trees that |
| 7947 | // are not an inner WebContents for focus. If `inner_contents` is null, |
| 7948 | // then the only supported inner frame tree type would be fenced frames. |
| 7949 | // An embedding frame focusing a fenced frame is not allowed since that would |
| 7950 | // be an information leak. If a renderer attempts to do that, that should be |
| 7951 | // blocked by `RenderFrameProxyHost::DidFocusFrame()`. |
| 7952 | DCHECK(inner_contents || |
| 7953 | node->current_frame_host()->inner_tree_main_frame_tree_node_id() == |
| 7954 | FrameTreeNode::kFrameTreeNodeInvalidId); |
| 7955 | |
| 7956 | if (inner_contents) { |
| 7957 | // An inner WebContents is not created from Fenced Frames so we |
| 7958 | // shouldn't end up in this branch. |
Abhijeet Kandalkar | 749b8bf1 | 2022-12-14 16:41:48 | [diff] [blame] | 7959 | DCHECK(!node->IsInFencedFrameTree()); |
Dave Tapuska | 54c76a03 | 2021-10-27 22:10:42 | [diff] [blame] | 7960 | |
Lucas Furukawa Gadani | abbfa34 | 2017-08-29 19:37:57 | [diff] [blame] | 7961 | // |this| is an outer WebContents and |node| represents an inner |
| 7962 | // WebContents. Transfer the focus to the inner contents if |this| is |
| 7963 | // focused. |
Adithya Srinivasan | 11c3d6ba0 | 2021-02-12 20:25:10 | [diff] [blame] | 7964 | DCHECK(!inner_contents->IsPortal()); |
Lucas Furukawa Gadani | abbfa34 | 2017-08-29 19:37:57 | [diff] [blame] | 7965 | if (GetFocusedWebContents() == this) |
| 7966 | inner_contents->SetAsFocusedWebContentsIfNecessary(); |
| 7967 | } else if (node_.OuterContentsFrameTreeNode() && |
| 7968 | node_.OuterContentsFrameTreeNode() |
| 7969 | ->current_frame_host() |
Sharon Yang | efe5263 | 2022-03-08 23:06:06 | [diff] [blame] | 7970 | ->GetSiteInstance() |
| 7971 | ->group() == source) { |
Dave Tapuska | 54c76a03 | 2021-10-27 22:10:42 | [diff] [blame] | 7972 | // A GuestView embedding a fenced frame will have an |
| 7973 | // OuterContentsFrameTreeNode. However, it will not have the same site |
| 7974 | // instance because a FencedFrame creates a new BrowsingInstance. |
Abhijeet Kandalkar | 749b8bf1 | 2022-12-14 16:41:48 | [diff] [blame] | 7975 | DCHECK(!node->IsInFencedFrameTree()); |
Dave Tapuska | 54c76a03 | 2021-10-27 22:10:42 | [diff] [blame] | 7976 | |
Lucas Furukawa Gadani | abbfa34 | 2017-08-29 19:37:57 | [diff] [blame] | 7977 | // |this| is an inner WebContents, |node| is its main FrameTreeNode and |
| 7978 | // the outer WebContents FrameTreeNode is at |source|'s SiteInstance. |
| 7979 | // Transfer the focus to the inner WebContents if the outer WebContents is |
| 7980 | // focused. This branch is used when an inner WebContents is focused through |
Lowell Manners | a953038 | 2019-08-01 09:46:57 | [diff] [blame] | 7981 | // its RenderFrameProxyHost (via FrameFocused mojo call, used to |
Lucas Furukawa Gadani | abbfa34 | 2017-08-29 19:37:57 | [diff] [blame] | 7982 | // implement the window.focus() API). |
| 7983 | if (GetFocusedWebContents() == GetOuterWebContents()) |
Arthur Sonzogni | f6785ec | 2022-12-05 10:11:50 | [diff] [blame] | 7984 | SetFocusedFrameTree(&node->frame_tree()); |
Dave Tapuska | 54c76a03 | 2021-10-27 22:10:42 | [diff] [blame] | 7985 | } else if (!GetOuterWebContents() || GetFocusedWebContents() == this) { |
| 7986 | // This is an outermost WebContents or we are currently focused so allow |
| 7987 | // the requested node's frame tree to be focused. The |
| 7988 | // (GetFocusedWebContents() == this) is needed when there are multiple |
| 7989 | // frame trees within an inner WebContents (ie. a GuestView with fenced |
| 7990 | // frames). |
Arthur Sonzogni | f6785ec | 2022-12-05 10:11:50 | [diff] [blame] | 7991 | SetFocusedFrameTree(&node->frame_tree()); |
Lucas Furukawa Gadani | abbfa34 | 2017-08-29 19:37:57 | [diff] [blame] | 7992 | } |
avallee | d68cdd1 | 2017-06-21 22:07:27 | [diff] [blame] | 7993 | } |
| 7994 | |
Avi Drissman | 36f801f | 2018-02-01 20:06:04 | [diff] [blame] | 7995 | void WebContentsImpl::DidCallFocus() { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 7996 | OPTIONAL_TRACE_EVENT0("content", "WebContentsImpl::DidCallFocus"); |
Avi Drissman | 36f801f | 2018-02-01 20:06:04 | [diff] [blame] | 7997 | // Any explicit focusing of another window while this WebContents is in |
| 7998 | // fullscreen can be used to confuse the user, so drop fullscreen. |
Avi Drissman | 1a55a9d6 | 2020-03-10 18:56:45 | [diff] [blame] | 7999 | base::ScopedClosureRunner fullscreen_block = ForSecurityDropFullscreen(); |
| 8000 | // The other contents is independent of this contents, so release the |
| 8001 | // fullscreen block. |
| 8002 | fullscreen_block.RunAndReset(); |
Avi Drissman | 36f801f | 2018-02-01 20:06:04 | [diff] [blame] | 8003 | } |
| 8004 | |
Lucas Furukawa Gadani | abbfa34 | 2017-08-29 19:37:57 | [diff] [blame] | 8005 | void WebContentsImpl::OnAdvanceFocus(RenderFrameHostImpl* source_rfh) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 8006 | OPTIONAL_TRACE_EVENT1("content", "WebContentsImpl::OnAdvanceFocus", |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 8007 | "render_frame_host", source_rfh); |
Dave Tapuska | 2402595f | 2022-08-03 16:24:21 | [diff] [blame] | 8008 | // When a RenderFrame needs to advance focus to a `blink::RemoteFrame` (by |
| 8009 | // hitting TAB), the `blink::RemoteFrame` sends an IPC to |
| 8010 | // RenderFrameProxyHost. When this RenderFrameProxyHost represents an inner |
| 8011 | // WebContents, the outer WebContents needs to focus the inner WebContents. |
Lucas Furukawa Gadani | abbfa34 | 2017-08-29 19:37:57 | [diff] [blame] | 8012 | if (GetOuterWebContents() && |
Matt Falkenhagen | 7b71b9f1 | 2021-08-12 01:32:43 | [diff] [blame] | 8013 | GetOuterWebContents() == WebContents::FromRenderFrameHost(source_rfh) && |
Lucas Furukawa Gadani | abbfa34 | 2017-08-29 19:37:57 | [diff] [blame] | 8014 | GetFocusedWebContents() == GetOuterWebContents()) { |
| 8015 | SetAsFocusedWebContentsIfNecessary(); |
| 8016 | } |
| 8017 | } |
| 8018 | |
ekaramad | a110f64 | 2016-12-21 19:47:28 | [diff] [blame] | 8019 | void WebContentsImpl::OnFocusedElementChangedInFrame( |
| 8020 | RenderFrameHostImpl* frame, |
Emily Stark | 023dbacc | 2020-08-27 22:31:40 | [diff] [blame] | 8021 | const gfx::Rect& bounds_in_root_view, |
| 8022 | blink::mojom::FocusType focus_type) { |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 8023 | OPTIONAL_TRACE_EVENT1("content", |
| 8024 | "WebContentsImpl::OnFocusedElementChangedInFrame", |
| 8025 | "render_frame_host", frame); |
ekaramad | a110f64 | 2016-12-21 19:47:28 | [diff] [blame] | 8026 | RenderWidgetHostViewBase* root_view = |
| 8027 | static_cast<RenderWidgetHostViewBase*>(GetRenderWidgetHostView()); |
| 8028 | if (!root_view || !frame->GetView()) |
| 8029 | return; |
Katie D | e013137a | 2020-07-09 23:03:10 | [diff] [blame] | 8030 | // Convert to screen coordinates from window coordinates by adding the |
| 8031 | // window's origin. |
ekaramad | a110f64 | 2016-12-21 19:47:28 | [diff] [blame] | 8032 | gfx::Point origin = bounds_in_root_view.origin(); |
| 8033 | origin += root_view->GetViewBounds().OffsetFromOrigin(); |
| 8034 | gfx::Rect bounds_in_screen(origin, bounds_in_root_view.size()); |
| 8035 | |
| 8036 | root_view->FocusedNodeChanged(frame->has_focused_editable_element(), |
| 8037 | bounds_in_screen); |
| 8038 | |
| 8039 | FocusedNodeDetails details = {frame->has_focused_editable_element(), |
Emily Stark | 023dbacc | 2020-08-27 22:31:40 | [diff] [blame] | 8040 | bounds_in_screen, focus_type}; |
Steve Kobes | 6615df9 | 2021-03-15 18:02:20 | [diff] [blame] | 8041 | BrowserAccessibilityStateImpl::GetInstance()->OnFocusChangedInPage(details); |
Andrew Grieve | e19cd93e | 2021-01-22 05:43:06 | [diff] [blame] | 8042 | observers_.NotifyObservers(&WebContentsObserver::OnFocusChangedInPage, |
| 8043 | &details); |
ekaramad | a110f64 | 2016-12-21 19:47:28 | [diff] [blame] | 8044 | } |
| 8045 | |
Lowell Manners | 1de5242e | 2019-04-25 10:18:46 | [diff] [blame] | 8046 | bool WebContentsImpl::DidAddMessageToConsole( |
Kevin McNee | fb86fcf | 2021-02-26 23:20:57 | [diff] [blame] | 8047 | RenderFrameHostImpl* source_frame, |
Lowell Manners | 1de5242e | 2019-04-25 10:18:46 | [diff] [blame] | 8048 | blink::mojom::ConsoleMessageLevel log_level, |
Jan Wilken Dörrie | aace0cfef | 2021-03-11 22:01:58 | [diff] [blame] | 8049 | const std::u16string& message, |
Lowell Manners | 1de5242e | 2019-04-25 10:18:46 | [diff] [blame] | 8050 | int32_t line_no, |
Jan Wilken Dörrie | aace0cfef | 2021-03-11 22:01:58 | [diff] [blame] | 8051 | const std::u16string& source_id, |
Anton Bikineev | f62d1bf | 2021-05-15 17:56:07 | [diff] [blame] | 8052 | const absl::optional<std::u16string>& untrusted_stack_trace) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 8053 | OPTIONAL_TRACE_EVENT1("content", "WebContentsImpl::DidAddMessageToConsole", |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 8054 | "message", message); |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 8055 | |
Andrew Grieve | e19cd93e | 2021-01-22 05:43:06 | [diff] [blame] | 8056 | observers_.NotifyObservers(&WebContentsObserver::OnDidAddMessageToConsole, |
| 8057 | source_frame, log_level, message, line_no, |
| 8058 | source_id, untrusted_stack_trace); |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 8059 | |
[email protected] | a796f20 | 2012-05-30 14:14:25 | [diff] [blame] | 8060 | if (!delegate_) |
| 8061 | return false; |
Lowell Manners | 1de5242e | 2019-04-25 10:18:46 | [diff] [blame] | 8062 | return delegate_->DidAddMessageToConsole(this, log_level, message, line_no, |
avi | a90ae4e | 2016-11-11 20:49:33 | [diff] [blame] | 8063 | source_id); |
[email protected] | a796f20 | 2012-05-30 14:14:25 | [diff] [blame] | 8064 | } |
| 8065 | |
Daniel Cheng | 90196c8 | 2018-04-25 21:49:14 | [diff] [blame] | 8066 | void WebContentsImpl::DidReceiveInputEvent( |
dominickn | 9c7ceaa3 | 2016-02-27 01:52:53 | [diff] [blame] | 8067 | RenderWidgetHostImpl* render_widget_host, |
Emily Stark | c7bd40c4 | 2020-07-21 19:12:55 | [diff] [blame] | 8068 | const blink::WebInputEvent& event) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 8069 | OPTIONAL_TRACE_EVENT1("content", "WebContentsImpl::DidReceiveInputEvent", |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 8070 | "render_widget_host", render_widget_host); |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 8071 | |
Emily Stark | c7bd40c4 | 2020-07-21 19:12:55 | [diff] [blame] | 8072 | if (!IsUserInteractionInputType(event.GetType())) |
Daniel Cheng | 90196c8 | 2018-04-25 21:49:14 | [diff] [blame] | 8073 | return; |
| 8074 | |
kenrb | 1922185 | 2016-04-29 17:21:40 | [diff] [blame] | 8075 | // Ignore unless the widget is currently in the frame tree. |
Carlos Caballero | b65b6e3a | 2021-11-15 10:09:00 | [diff] [blame] | 8076 | if (!HasMatchingWidgetHost(&primary_frame_tree_, render_widget_host)) |
avi | c3aa842 | 2015-11-09 20:57:22 | [diff] [blame] | 8077 | return; |
| 8078 | |
Emily Stark | c7bd40c4 | 2020-07-21 19:12:55 | [diff] [blame] | 8079 | if (event.GetType() != blink::WebInputEvent::Type::kGestureScrollBegin) |
Evan Stade | 6decc97 | 2022-10-24 19:42:18 | [diff] [blame] | 8080 | last_interaction_time_ = ui::EventTimeForNow(); |
dominickn | 9c7ceaa3 | 2016-02-27 01:52:53 | [diff] [blame] | 8081 | |
Andrew Grieve | e19cd93e | 2021-01-22 05:43:06 | [diff] [blame] | 8082 | observers_.NotifyObservers(&WebContentsObserver::DidGetUserInteraction, |
| 8083 | event); |
dominickn | 2dd142dd | 2015-10-29 05:30:50 | [diff] [blame] | 8084 | } |
| 8085 | |
Avi Drissman | 738ea19 | 2018-08-29 20:24:16 | [diff] [blame] | 8086 | bool WebContentsImpl::ShouldIgnoreInputEvents() { |
| 8087 | WebContentsImpl* web_contents = this; |
| 8088 | while (web_contents) { |
| 8089 | if (web_contents->ignore_input_events_) |
| 8090 | return true; |
| 8091 | web_contents = web_contents->GetOuterWebContents(); |
| 8092 | } |
| 8093 | |
| 8094 | return false; |
| 8095 | } |
| 8096 | |
avallee | 9993fca | 2016-11-17 06:16:50 | [diff] [blame] | 8097 | void WebContentsImpl::FocusOwningWebContents( |
| 8098 | RenderWidgetHostImpl* render_widget_host) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 8099 | OPTIONAL_TRACE_EVENT1("content", "WebContentsImpl::FocusOwningWebContents", |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 8100 | "render_widget_host", render_widget_host); |
Alex Moshchuk | c4f0a72 | 2019-11-19 22:41:28 | [diff] [blame] | 8101 | RenderWidgetHostImpl* main_frame_widget_host = |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 8102 | GetPrimaryMainFrame()->GetRenderWidgetHost(); |
avallee | 9993fca | 2016-11-17 06:16:50 | [diff] [blame] | 8103 | RenderWidgetHostImpl* focused_widget = |
Alex Moshchuk | c4f0a72 | 2019-11-19 22:41:28 | [diff] [blame] | 8104 | GetFocusedRenderWidgetHost(main_frame_widget_host); |
avallee | 9993fca | 2016-11-17 06:16:50 | [diff] [blame] | 8105 | |
| 8106 | if (focused_widget != render_widget_host && |
avallee | 8e88159 | 2016-11-19 00:39:30 | [diff] [blame] | 8107 | (!focused_widget || |
| 8108 | focused_widget->delegate() != render_widget_host->delegate())) { |
avallee | 9993fca | 2016-11-17 06:16:50 | [diff] [blame] | 8109 | SetAsFocusedWebContentsIfNecessary(); |
| 8110 | } |
| 8111 | } |
| 8112 | |
[email protected] | b172aee | 2012-04-10 17:05:26 | [diff] [blame] | 8113 | void WebContentsImpl::OnIgnoredUIEvent() { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 8114 | OPTIONAL_TRACE_EVENT0("content", "WebContentsImpl::OnIgnoredUIEvent"); |
[email protected] | 3114509 | 2011-09-30 01:49:44 | [diff] [blame] | 8115 | // Notify observers. |
Andrew Grieve | e19cd93e | 2021-01-22 05:43:06 | [diff] [blame] | 8116 | observers_.NotifyObservers(&WebContentsObserver::DidGetIgnoredUIEvent); |
[email protected] | fa1cf0b8 | 2010-01-15 21:49:44 | [diff] [blame] | 8117 | } |
| 8118 | |
avi | c3aa842 | 2015-11-09 20:57:22 | [diff] [blame] | 8119 | void WebContentsImpl::RendererUnresponsive( |
Lukasz Anforowicz | 52b9372 | 2018-06-20 16:11:39 | [diff] [blame] | 8120 | RenderWidgetHostImpl* render_widget_host, |
| 8121 | base::RepeatingClosure hang_monitor_restarter) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 8122 | OPTIONAL_TRACE_EVENT1("content", "WebContentsImpl::RendererUnresponsive", |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 8123 | "render_widget_host", render_widget_host); |
avi | c031d39 | 2017-03-03 03:09:42 | [diff] [blame] | 8124 | if (ShouldIgnoreUnresponsiveRenderer()) |
[email protected] | e5fc163 | 2011-08-08 07:51:53 | [diff] [blame] | 8125 | return; |
| 8126 | |
Lukasz Anforowicz | 4f0593d | 2018-09-21 18:33:54 | [diff] [blame] | 8127 | // Do not report hangs (to task manager, to hang renderer dialog, etc.) for |
| 8128 | // invisible tabs (like extension background page, background tabs). See |
| 8129 | // https://crbug.com/881812 for rationale and for choosing the visibility |
| 8130 | // (rather than process priority) as the signal here. |
| 8131 | if (GetVisibility() != Visibility::VISIBLE) |
| 8132 | return; |
| 8133 | |
Avi Drissman | 8920def | 2018-01-31 19:53:36 | [diff] [blame] | 8134 | if (!render_widget_host->renderer_initialized()) |
[email protected] | 5545290 | 2011-06-01 21:57:47 | [diff] [blame] | 8135 | return; |
| 8136 | |
Andrew Grieve | e19cd93e | 2021-01-22 05:43:06 | [diff] [blame] | 8137 | observers_.NotifyObservers(&WebContentsObserver::OnRendererUnresponsive, |
| 8138 | render_widget_host->GetProcess()); |
Dave Tapuska | c6146c5 | 2017-12-20 22:48:15 | [diff] [blame] | 8139 | if (delegate_) |
Lukasz Anforowicz | 52b9372 | 2018-06-20 16:11:39 | [diff] [blame] | 8140 | delegate_->RendererUnresponsive(this, render_widget_host, |
| 8141 | std::move(hang_monitor_restarter)); |
[email protected] | 420ae01 | 2009-04-24 05:16:32 | [diff] [blame] | 8142 | } |
| 8143 | |
avi | c3aa842 | 2015-11-09 20:57:22 | [diff] [blame] | 8144 | void WebContentsImpl::RendererResponsive( |
| 8145 | RenderWidgetHostImpl* render_widget_host) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 8146 | OPTIONAL_TRACE_EVENT1("content", "WebContentsImpl::RenderResponsive", |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 8147 | "render_widget_host", render_widget_host); |
Andrew Grieve | e19cd93e | 2021-01-22 05:43:06 | [diff] [blame] | 8148 | observers_.NotifyObservers(&WebContentsObserver::OnRendererResponsive, |
| 8149 | render_widget_host->GetProcess()); |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 8150 | |
[email protected] | 6934a70 | 2011-12-20 00:04:51 | [diff] [blame] | 8151 | if (delegate_) |
Avi Drissman | 1572e8c3c | 2018-02-02 19:06:36 | [diff] [blame] | 8152 | delegate_->RendererResponsive(this, render_widget_host); |
[email protected] | 420ae01 | 2009-04-24 05:16:32 | [diff] [blame] | 8153 | } |
| 8154 | |
[email protected] | b172aee | 2012-04-10 17:05:26 | [diff] [blame] | 8155 | void WebContentsImpl::BeforeUnloadFiredFromRenderManager( |
Ian Vollick | 6d75ac3 | 2021-05-05 17:45:09 | [diff] [blame] | 8156 | bool proceed, |
[email protected] | 420ae01 | 2009-04-24 05:16:32 | [diff] [blame] | 8157 | bool* proceed_to_fire_unload) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 8158 | OPTIONAL_TRACE_EVENT0("content", |
| 8159 | "WebContentsImpl::BeforeUnloadFiredFromRenderManager"); |
Arthur Sonzogni | 72be0c2 | 2023-02-10 10:19:04 | [diff] [blame^] | 8160 | observers_.NotifyObservers(&WebContentsObserver::BeforeUnloadFired, proceed); |
[email protected] | 6934a70 | 2011-12-20 00:04:51 | [diff] [blame] | 8161 | if (delegate_) |
| 8162 | delegate_->BeforeUnloadFired(this, proceed, proceed_to_fire_unload); |
[email protected] | e80af49 | 2013-06-24 21:52:09 | [diff] [blame] | 8163 | // Note: |this| might be deleted at this point. |
[email protected] | 420ae01 | 2009-04-24 05:16:32 | [diff] [blame] | 8164 | } |
| 8165 | |
[email protected] | af90590 | 2013-10-01 21:38:51 | [diff] [blame] | 8166 | void WebContentsImpl::CancelModalDialogsForRenderManager() { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 8167 | OPTIONAL_TRACE_EVENT0("content", |
| 8168 | "WebContentsImpl::CancelModalDialogsForRenderManager"); |
[email protected] | af90590 | 2013-10-01 21:38:51 | [diff] [blame] | 8169 | // We need to cancel modal dialogs when doing a process swap, since the load |
avi | 2460c76 | 2015-04-17 15:21:54 | [diff] [blame] | 8170 | // deferrer would prevent us from swapping out. We also clear the state |
| 8171 | // because this is a cross-process navigation, which means that it's a new |
| 8172 | // site that should not have to pay for the sins of its predecessor. |
creis | 89a0f78 | 2015-05-27 16:13:17 | [diff] [blame] | 8173 | // |
Lei Zhang | 6410ec1a | 2019-01-31 21:38:58 | [diff] [blame] | 8174 | // Note that we don't bother telling |browser_plugin_embedder_| because the |
creis | 89a0f78 | 2015-05-27 16:13:17 | [diff] [blame] | 8175 | // cross-process navigation will either destroy the browser plugins or not |
| 8176 | // require their dialogs to close. |
avi | 5d3b869 | 2016-10-12 22:00:46 | [diff] [blame] | 8177 | if (dialog_manager_) { |
avi | 6879fcf | 2017-01-21 05:27:53 | [diff] [blame] | 8178 | dialog_manager_->CancelDialogs(this, /*reset_state=*/true); |
avi | 5d3b869 | 2016-10-12 22:00:46 | [diff] [blame] | 8179 | } |
[email protected] | af90590 | 2013-10-01 21:38:51 | [diff] [blame] | 8180 | } |
| 8181 | |
Kevin McNee | fb86fcf | 2021-02-26 23:20:57 | [diff] [blame] | 8182 | void WebContentsImpl::NotifySwappedFromRenderManager( |
| 8183 | RenderFrameHostImpl* old_frame, |
Dave Tapuska | e45d6fd | 2021-09-29 17:03:59 | [diff] [blame] | 8184 | RenderFrameHostImpl* new_frame) { |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 8185 | TRACE_EVENT2("content", "WebContentsImpl::NotifySwappedFromRenderManager", |
| 8186 | "old_render_frame_host", old_frame, "new_render_frame_host", |
| 8187 | new_frame); |
Khushal | c5eaf22 | 2021-06-30 20:15:48 | [diff] [blame] | 8188 | DCHECK_NE(new_frame->lifecycle_state(), |
| 8189 | RenderFrameHostImpl::LifecycleStateImpl::kSpeculative); |
Carlos Caballero | d364451 | 2021-02-11 15:59:53 | [diff] [blame] | 8190 | |
Nasko Oskov | 82dbf01 | 2023-01-10 22:40:01 | [diff] [blame] | 8191 | // Only fire RenderViewHostChanged if it is related to our FrameTree, as |
| 8192 | // observers can not deal with events coming from non-primary FrameTree. |
| 8193 | // TODO(https://crbug.com/1168562): Update observers to deal with the events, |
| 8194 | // and fire events for all frame trees. |
Kevin McNee | 9f3ef7c | 2021-09-29 18:43:41 | [diff] [blame] | 8195 | if (new_frame->IsInPrimaryMainFrame()) { |
danakj | a0bce4c95 | 2020-05-12 20:40:28 | [diff] [blame] | 8196 | // The |new_frame| and its various compadres are already swapped into place |
| 8197 | // for the WebContentsImpl when this method is called. |
Carlos Caballero | b65b6e3a | 2021-11-15 10:09:00 | [diff] [blame] | 8198 | DCHECK_EQ( |
| 8199 | primary_frame_tree_.root()->render_manager()->GetRenderWidgetHostView(), |
| 8200 | new_frame->GetView()); |
danakj | a0bce4c95 | 2020-05-12 20:40:28 | [diff] [blame] | 8201 | |
Fergal Daly | 6767b2e | 2020-02-04 03:15:16 | [diff] [blame] | 8202 | RenderViewHost* old_rvh = |
danakj | a0bce4c95 | 2020-05-12 20:40:28 | [diff] [blame] | 8203 | old_frame ? old_frame->GetRenderViewHost() : nullptr; |
| 8204 | RenderViewHost* new_rvh = new_frame->GetRenderViewHost(); |
Fergal Daly | 6767b2e | 2020-02-04 03:15:16 | [diff] [blame] | 8205 | // |old_rvh| and |new_rvh| might be equal when navigating from a crashed |
| 8206 | // RenderFrameHost to a new same-site one. With RenderDocument, this will |
| 8207 | // happen for every same-site navigation. |
| 8208 | if (old_rvh != new_rvh) |
| 8209 | NotifyViewSwapped(old_rvh, new_rvh); |
[email protected] | a6b73c6 | 2013-02-11 23:05:08 | [diff] [blame] | 8210 | |
danakj | a0bce4c95 | 2020-05-12 20:40:28 | [diff] [blame] | 8211 | auto* rwhv = static_cast<RenderWidgetHostViewBase*>(new_frame->GetView()); |
| 8212 | if (rwhv) { |
| 8213 | rwhv->SetMainFrameAXTreeID(new_frame->GetAXTreeID()); |
| 8214 | |
| 8215 | // The RenderWidgetHostView for the speculative RenderFrameHost is not |
| 8216 | // resized with the current RenderFrameHost while a navigation is |
| 8217 | // pending. So when we swap in the main frame, we need to update the |
| 8218 | // RenderWidgetHostView's size. |
| 8219 | // |
| 8220 | // Historically, this was done to fix b/1079768 for interstitials. |
| 8221 | rwhv->SetSize(GetSizeForMainFrame()); |
| 8222 | } |
Khushal | c5eaf22 | 2021-06-30 20:15:48 | [diff] [blame] | 8223 | |
| 8224 | NotifyPrimaryMainFrameProcessIsAlive(); |
[email protected] | 02d7b6e | 2014-06-24 21:01:50 | [diff] [blame] | 8225 | } |
| 8226 | |
Dave Tapuska | e45d6fd | 2021-09-29 17:03:59 | [diff] [blame] | 8227 | NotifyFrameSwapped(old_frame, new_frame); |
[email protected] | 3a3d4747 | 2010-07-15 21:03:54 | [diff] [blame] | 8228 | } |
| 8229 | |
Jonathan Ross | 61ec6534 | 2021-12-14 23:01:40 | [diff] [blame] | 8230 | void WebContentsImpl::NotifySwappedFromRenderManagerWithoutFallbackContent( |
| 8231 | RenderFrameHostImpl* new_frame) { |
| 8232 | auto* rwhv = static_cast<RenderWidgetHostViewBase*>(new_frame->GetView()); |
| 8233 | if (rwhv) |
| 8234 | rwhv->ClearFallbackSurfaceForCommitPending(); |
| 8235 | } |
| 8236 | |
nasko | f5940b9f | 2015-03-02 23:04:05 | [diff] [blame] | 8237 | void WebContentsImpl::NotifyMainFrameSwappedFromRenderManager( |
Kevin McNee | fb86fcf | 2021-02-26 23:20:57 | [diff] [blame] | 8238 | RenderFrameHostImpl* old_frame, |
| 8239 | RenderFrameHostImpl* new_frame) { |
Nasko Oskov | 82dbf01 | 2023-01-10 22:40:01 | [diff] [blame] | 8240 | // Only fire RenderViewHostChanged if it is |
| 8241 | // related to our FrameTree, as observers cannot deal with events coming |
| 8242 | // from non-primary FrameTree. |
| 8243 | // TODO(https://crbug.com/1168562): Update observers to deal with the events, |
| 8244 | // and fire events for all frame trees. |
Kevin McNee | 9f3ef7c | 2021-09-29 18:43:41 | [diff] [blame] | 8245 | if (!new_frame->IsInPrimaryMainFrame()) { |
Carlos Caballero | d364451 | 2021-02-11 15:59:53 | [diff] [blame] | 8246 | return; |
| 8247 | } |
danakj | a0bce4c95 | 2020-05-12 20:40:28 | [diff] [blame] | 8248 | NotifyViewSwapped(old_frame ? old_frame->GetRenderViewHost() : nullptr, |
| 8249 | new_frame->GetRenderViewHost()); |
nasko | f5940b9f | 2015-03-02 23:04:05 | [diff] [blame] | 8250 | } |
| 8251 | |
dcheng | 5971627 | 2016-04-09 05:19:08 | [diff] [blame] | 8252 | std::unique_ptr<WebUIImpl> WebContentsImpl::CreateWebUIForRenderFrameHost( |
Ian Barkley-Yeung | bda77fb | 2020-12-08 04:26:46 | [diff] [blame] | 8253 | RenderFrameHostImpl* frame_host, |
Denis Kuznetsov [CET] | 2c6f0159f | 2020-05-18 14:18:16 | [diff] [blame] | 8254 | const GURL& url) { |
Miyoung Shin | 072d405 | 2020-07-13 07:52:44 | [diff] [blame] | 8255 | return CreateWebUI(frame_host, url); |
[email protected] | 420ae01 | 2009-04-24 05:16:32 | [diff] [blame] | 8256 | } |
| 8257 | |
lfg | bede639 | 2015-09-11 21:54:06 | [diff] [blame] | 8258 | void WebContentsImpl::CreateRenderWidgetHostViewForRenderManager( |
| 8259 | RenderViewHost* render_view_host) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 8260 | OPTIONAL_TRACE_EVENT1( |
| 8261 | "content", "WebContentsImpl::CreateRenderWidgetHostViewForRenderManager", |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 8262 | "render_view_host", render_view_host); |
Dave Tapuska | efc4936a | 2021-10-05 20:47:10 | [diff] [blame] | 8263 | // TODO(crbug.com/1254770): Fix this for portals. |
| 8264 | bool is_inner_frame_tree = static_cast<RenderViewHostImpl*>(render_view_host) |
| 8265 | ->frame_tree() |
| 8266 | ->type() == FrameTree::Type::kFencedFrame; |
| 8267 | if (is_inner_frame_tree) { |
| 8268 | WebContentsViewChildFrame::CreateRenderWidgetHostViewForInnerFrameTree( |
| 8269 | this, render_view_host->GetWidget()); |
| 8270 | } else { |
| 8271 | RenderWidgetHostViewBase* rwh_view = |
| 8272 | view_->CreateViewForWidget(render_view_host->GetWidget()); |
| 8273 | view_->SetOverscrollControllerEnabled(CanOverscrollContent()); |
| 8274 | rwh_view->SetSize(GetSizeForMainFrame()); |
| 8275 | } |
lfg | bede639 | 2015-09-11 21:54:06 | [diff] [blame] | 8276 | } |
| 8277 | |
Fergal Daly | 6de62f5 | 2020-10-14 01:56:44 | [diff] [blame] | 8278 | void WebContentsImpl::ReattachOuterDelegateIfNeeded() { |
| 8279 | if (node_.outer_web_contents()) |
| 8280 | ReattachToOuterWebContentsFrame(); |
| 8281 | } |
| 8282 | |
lfg | bede639 | 2015-09-11 21:54:06 | [diff] [blame] | 8283 | bool WebContentsImpl::CreateRenderViewForRenderManager( |
| 8284 | RenderViewHost* render_view_host, |
Anton Bikineev | f62d1bf | 2021-05-15 17:56:07 | [diff] [blame] | 8285 | const absl::optional<blink::FrameToken>& opener_frame_token, |
Aaron Colwell | 6fdff5e | 2021-01-27 22:42:01 | [diff] [blame] | 8286 | RenderFrameProxyHost* proxy_host) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 8287 | TRACE_EVENT1("browser,navigation", |
| 8288 | "WebContentsImpl::CreateRenderViewForRenderManager", |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 8289 | "render_view_host", render_view_host); |
Carlos Caballero | 40b0efd | 2021-01-26 11:55:00 | [diff] [blame] | 8290 | auto* rvh_impl = static_cast<RenderViewHostImpl*>(render_view_host); |
danakj | e6aa9bb | 2021-03-02 20:28:07 | [diff] [blame] | 8291 | // Observers should not destroy the WebContents here or we will crash as the |
| 8292 | // stack unwinds. See crbug.com/1181043. |
| 8293 | base::AutoReset<bool> scope(&prevent_destruction_, true); |
Carlos Caballero | 40b0efd | 2021-01-26 11:55:00 | [diff] [blame] | 8294 | |
Aaron Colwell | 6fdff5e | 2021-01-27 22:42:01 | [diff] [blame] | 8295 | if (!proxy_host) |
lfg | 9431efd | 2016-12-14 14:45:26 | [diff] [blame] | 8296 | CreateRenderWidgetHostViewForRenderManager(render_view_host); |
lfg | 9431efd | 2016-12-14 14:45:26 | [diff] [blame] | 8297 | |
Aaron Colwell | 6fdff5e | 2021-01-27 22:42:01 | [diff] [blame] | 8298 | const auto proxy_routing_id = |
| 8299 | proxy_host ? proxy_host->GetRoutingID() : MSG_ROUTING_NONE; |
Carlos Caballero | ede6f8c | 2021-01-28 11:01:50 | [diff] [blame] | 8300 | // TODO(https://crbug.com/1171646): Given MPArch, should we pass |
Rakina Zata Amni | c7ffea88 | 2021-08-16 10:04:28 | [diff] [blame] | 8301 | // opened_by_another_window_ for non primary FrameTrees? |
Carlos Caballero | 40b0efd | 2021-01-26 11:55:00 | [diff] [blame] | 8302 | if (!rvh_impl->CreateRenderView(opener_frame_token, proxy_routing_id, |
Rakina Zata Amni | c7ffea88 | 2021-08-16 10:04:28 | [diff] [blame] | 8303 | opened_by_another_window_)) { |
[email protected] | a412772 | 2011-04-27 23:13:52 | [diff] [blame] | 8304 | return false; |
[email protected] | 14392a5 | 2012-05-02 20:28:44 | [diff] [blame] | 8305 | } |
Lingqi Chi | dcf72244 | 2021-09-02 01:47:19 | [diff] [blame] | 8306 | |
W. James MacLean | 6219867 | 2019-06-04 16:46:10 | [diff] [blame] | 8307 | // Set the TextAutosizer state from the main frame's renderer on the new view, |
| 8308 | // but only if it's not for the main frame. Main frame renderers should create |
| 8309 | // this state themselves from up-to-date values, so we shouldn't override it |
| 8310 | // with the cached values. |
Lingqi Chi | 2f0e7123 | 2021-09-08 02:36:19 | [diff] [blame] | 8311 | if (!rvh_impl->GetMainRenderFrameHost() && proxy_host) { |
Antonio Gomes | da7e03d | 2020-08-05 15:03:08 | [diff] [blame] | 8312 | proxy_host->GetAssociatedRemoteMainFrame()->UpdateTextAutosizerPageInfo( |
Lingqi Chi | dcf72244 | 2021-09-02 01:47:19 | [diff] [blame] | 8313 | proxy_host->frame_tree_node() |
| 8314 | ->current_frame_host() |
| 8315 | ->GetPage() |
| 8316 | .text_autosizer_page_info() |
| 8317 | .Clone()); |
W. James MacLean | 6219867 | 2019-06-04 16:46:10 | [diff] [blame] | 8318 | } |
[email protected] | a412772 | 2011-04-27 23:13:52 | [diff] [blame] | 8319 | |
Alex Moshchuk | 7142d51a | 2022-04-01 16:30:26 | [diff] [blame] | 8320 | // If `render_view_host` is for an inner WebContents, ensure that its |
| 8321 | // RenderWidgetHostView is properly reattached to the outer WebContents. Note |
| 8322 | // that this should only be done when `render_view_host` is already the |
| 8323 | // current RenderViewHost in this WebContents. If it isn't, the reattachment |
| 8324 | // will happen later when `render_view_host` becomes current as part of |
| 8325 | // committing the speculative RenderFrameHost it's associated with. |
| 8326 | if (!proxy_host && render_view_host == GetRenderViewHost()) |
Fergal Daly | 6de62f5 | 2020-10-14 01:56:44 | [diff] [blame] | 8327 | ReattachOuterDelegateIfNeeded(); |
Lucas Furukawa Gadani | aed1fed | 2017-10-13 17:34:14 | [diff] [blame] | 8328 | |
Carlos Caballero | ede6f8c | 2021-01-28 11:01:50 | [diff] [blame] | 8329 | SetHistoryOffsetAndLengthForView( |
| 8330 | render_view_host, |
| 8331 | rvh_impl->frame_tree()->controller().GetLastCommittedEntryIndex(), |
| 8332 | rvh_impl->frame_tree()->controller().GetEntryCount()); |
avi | 2b17759 | 2014-12-10 02:08:02 | [diff] [blame] | 8333 | |
Xiaohan Wang | 24ec934 | 2022-01-15 17:34:22 | [diff] [blame] | 8334 | #if BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_MAC) && !BUILDFLAG(IS_ANDROID) |
[email protected] | f8233cc | 2011-05-31 20:24:50 | [diff] [blame] | 8335 | // Force a ViewMsg_Resize to be sent, needed to make plugins show up on |
| 8336 | // linux. See crbug.com/83941. |
avi | f9ab5d94 | 2015-10-15 14:05:44 | [diff] [blame] | 8337 | RenderWidgetHostView* rwh_view = render_view_host->GetWidget()->GetView(); |
[email protected] | 245f7d5 | 2011-11-28 15:36:44 | [diff] [blame] | 8338 | if (rwh_view) { |
| 8339 | if (RenderWidgetHost* render_widget_host = rwh_view->GetRenderWidgetHost()) |
Fady Samuel | 0b91182 | 2018-04-25 13:22:16 | [diff] [blame] | 8340 | render_widget_host->SynchronizeVisualProperties(); |
[email protected] | 245f7d5 | 2011-11-28 15:36:44 | [diff] [blame] | 8341 | } |
[email protected] | f8233cc | 2011-05-31 20:24:50 | [diff] [blame] | 8342 | #endif |
| 8343 | |
[email protected] | 420ae01 | 2009-04-24 05:16:32 | [diff] [blame] | 8344 | return true; |
| 8345 | } |
| 8346 | |
Xiaohan Wang | 24ec934 | 2022-01-15 17:34:22 | [diff] [blame] | 8347 | #if BUILDFLAG(IS_ANDROID) |
[email protected] | fc2b46b | 2014-05-03 16:33:45 | [diff] [blame] | 8348 | |
[email protected] | 155c7f2 | 2013-12-09 17:07:18 | [diff] [blame] | 8349 | base::android::ScopedJavaLocalRef<jobject> |
| 8350 | WebContentsImpl::GetJavaWebContents() { |
mostynb | 042582e | 2015-03-16 22:13:40 | [diff] [blame] | 8351 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 0e813a5 | 2014-08-13 10:34:56 | [diff] [blame] | 8352 | return GetWebContentsAndroid()->GetJavaObject(); |
| 8353 | } |
[email protected] | 155c7f2 | 2013-12-09 17:07:18 | [diff] [blame] | 8354 | |
Nicolas Ouellet-Payeur | 177a6c4 | 2023-01-25 20:25:07 | [diff] [blame] | 8355 | base::android::ScopedJavaLocalRef<jthrowable> |
| 8356 | WebContentsImpl::GetJavaCreatorLocation() { |
| 8357 | return base::android::ScopedJavaLocalRef<jthrowable>(java_creator_location_); |
| 8358 | } |
| 8359 | |
[email protected] | 0e813a5 | 2014-08-13 10:34:56 | [diff] [blame] | 8360 | WebContentsAndroid* WebContentsImpl::GetWebContentsAndroid() { |
Kevin McNee | 576bb5d8 | 2019-05-29 16:47:51 | [diff] [blame] | 8361 | if (!web_contents_android_) { |
| 8362 | web_contents_android_ = std::make_unique<WebContentsAndroid>(this); |
[email protected] | 155c7f2 | 2013-12-09 17:07:18 | [diff] [blame] | 8363 | } |
Kevin McNee | 576bb5d8 | 2019-05-29 16:47:51 | [diff] [blame] | 8364 | return web_contents_android_.get(); |
| 8365 | } |
| 8366 | |
| 8367 | void WebContentsImpl::ClearWebContentsAndroid() { |
| 8368 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 8369 | web_contents_android_.reset(); |
[email protected] | 155c7f2 | 2013-12-09 17:07:18 | [diff] [blame] | 8370 | } |
| 8371 | |
Ian Vollick | 6d75ac3 | 2021-05-05 17:45:09 | [diff] [blame] | 8372 | void WebContentsImpl::ActivateNearestFindResult(float x, float y) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 8373 | OPTIONAL_TRACE_EVENT0("content", |
| 8374 | "WebContentsImpl::ActivateNearestFindResult"); |
paulmeyer | c0b762b | 2016-04-13 11:55:17 | [diff] [blame] | 8375 | GetOrCreateFindRequestManager()->ActivateNearestFindResult(x, y); |
| 8376 | } |
| 8377 | |
| 8378 | void WebContentsImpl::RequestFindMatchRects(int current_version) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 8379 | OPTIONAL_TRACE_EVENT0("content", "WebContentsImpl::RequestFindMatchRects"); |
paulmeyer | c0b762b | 2016-04-13 11:55:17 | [diff] [blame] | 8380 | GetOrCreateFindRequestManager()->RequestFindMatchRects(current_version); |
| 8381 | } |
| 8382 | |
rockot | 400ea35b | 2016-10-15 19:15:32 | [diff] [blame] | 8383 | service_manager::InterfaceProvider* WebContentsImpl::GetJavaInterfaces() { |
sammc | f5f1b0f | 2016-09-20 23:05:11 | [diff] [blame] | 8384 | if (!java_interfaces_) { |
Gyuyoung Kim | 6c9ce902 | 2019-11-26 05:40:08 | [diff] [blame] | 8385 | mojo::PendingRemote<service_manager::mojom::InterfaceProvider> provider; |
| 8386 | BindInterfaceRegistryForWebContents( |
| 8387 | provider.InitWithNewPipeAndPassReceiver(), this); |
Peter Boström | 08e7ed8 | 2021-04-19 17:49:59 | [diff] [blame] | 8388 | java_interfaces_ = std::make_unique<service_manager::InterfaceProvider>( |
Sean Maher | 5b9af51f | 2022-11-21 15:32:47 | [diff] [blame] | 8389 | base::SingleThreadTaskRunner::GetCurrentDefault()); |
sammc | f5f1b0f | 2016-09-20 23:05:11 | [diff] [blame] | 8390 | java_interfaces_->Bind(std::move(provider)); |
| 8391 | } |
| 8392 | return java_interfaces_.get(); |
| 8393 | } |
| 8394 | |
Francois Doray | 6c26f334 | 2018-08-24 17:17:22 | [diff] [blame] | 8395 | #endif |
| 8396 | |
Lucas Furukawa Gadani | e1c5dfda | 2018-11-29 17:57:41 | [diff] [blame] | 8397 | bool WebContentsImpl::CompletedFirstVisuallyNonEmptyPaint() { |
Jeremy Roman | 2d8dfe13 | 2021-07-06 20:51:26 | [diff] [blame] | 8398 | return GetPrimaryPage().did_first_visually_non_empty_paint(); |
spqchan | e72c3cb | 2017-06-30 23:02:10 | [diff] [blame] | 8399 | } |
| 8400 | |
pkotwicz | 75ca8ffd | 2016-02-16 23:10:19 | [diff] [blame] | 8401 | void WebContentsImpl::OnDidDownloadImage( |
Mikel Astiz | 034ba14e | 2021-04-30 07:23:49 | [diff] [blame] | 8402 | base::WeakPtr<RenderFrameHostImpl> rfh, |
Avi Drissman | a5a52dd | 2018-03-27 03:39:02 | [diff] [blame] | 8403 | ImageDownloadCallback callback, |
pkotwicz | 75ca8ffd | 2016-02-16 23:10:19 | [diff] [blame] | 8404 | int id, |
| 8405 | const GURL& image_url, |
leon.han | cbc4bc3c | 2016-02-26 07:08:52 | [diff] [blame] | 8406 | int32_t http_status_code, |
danakj | cd882fe | 2020-12-09 19:32:19 | [diff] [blame] | 8407 | const std::vector<SkBitmap>& images, |
leon.han | c222853 | 2016-08-16 05:59:18 | [diff] [blame] | 8408 | const std::vector<gfx::Size>& original_image_sizes) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 8409 | OPTIONAL_TRACE_EVENT1("content", "WebContentsImpl::OnDidDownloadImage", |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 8410 | "image_url", image_url); |
Mikel Astiz | 034ba14e | 2021-04-30 07:23:49 | [diff] [blame] | 8411 | |
| 8412 | // Guard against buggy or compromised renderers that could violate the API |
| 8413 | // contract that |images| and |original_image_sizes| must have the same |
| 8414 | // length. |
| 8415 | if (images.size() != original_image_sizes.size()) { |
| 8416 | if (rfh) { |
| 8417 | ReceivedBadMessage(rfh->GetProcess(), |
| 8418 | bad_message::WCI_INVALID_DOWNLOAD_IMAGE_RESULT); |
| 8419 | } |
| 8420 | // Respond with a 400 to indicate that something went wrong. |
| 8421 | std::move(callback).Run(id, 400, image_url, std::vector<SkBitmap>(), |
| 8422 | std::vector<gfx::Size>()); |
| 8423 | return; |
| 8424 | } |
| 8425 | |
Avi Drissman | a5a52dd | 2018-03-27 03:39:02 | [diff] [blame] | 8426 | std::move(callback).Run(id, http_status_code, image_url, images, |
| 8427 | original_image_sizes); |
pkotwicz | 75ca8ffd | 2016-02-16 23:10:19 | [diff] [blame] | 8428 | } |
| 8429 | |
[email protected] | 6fba26d | 2014-04-29 09:38:28 | [diff] [blame] | 8430 | void WebContentsImpl::OnDialogClosed(int render_process_id, |
| 8431 | int render_frame_id, |
Dave Tapuska | cdf545cc | 2020-01-23 18:38:52 | [diff] [blame] | 8432 | JavaScriptDialogCallback response_callback, |
Avi Drissman | 1a55a9d6 | 2020-03-10 18:56:45 | [diff] [blame] | 8433 | base::ScopedClosureRunner fullscreen_block, |
[email protected] | 87de04b0 | 2014-04-08 22:14:49 | [diff] [blame] | 8434 | bool dialog_was_suppressed, |
[email protected] | b172aee | 2012-04-10 17:05:26 | [diff] [blame] | 8435 | bool success, |
Jan Wilken Dörrie | aace0cfef | 2021-03-11 22:01:58 | [diff] [blame] | 8436 | const std::u16string& user_input) { |
Ian Vollick | 6d75ac3 | 2021-05-05 17:45:09 | [diff] [blame] | 8437 | RenderFrameHostImpl* rfh = |
| 8438 | RenderFrameHostImpl::FromID(render_process_id, render_frame_id); |
Adithya Srinivasan | 2d948b2 | 2022-09-15 13:22:20 | [diff] [blame] | 8439 | DCHECK((!rfh || rfh->GetPage().IsPrimary()) && !IsPortal()); |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 8440 | OPTIONAL_TRACE_EVENT1("content", "WebContentsImpl::OnDialogClosed", |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 8441 | "render_frame_host", rfh); |
[email protected] | 87de04b0 | 2014-04-08 22:14:49 | [diff] [blame] | 8442 | last_dialog_suppressed_ = dialog_was_suppressed; |
Avi Drissman | 1a55a9d6 | 2020-03-10 18:56:45 | [diff] [blame] | 8443 | fullscreen_block.RunAndReset(); |
[email protected] | 87de04b0 | 2014-04-08 22:14:49 | [diff] [blame] | 8444 | |
David Bokan | 1bdb3701f | 2021-04-30 22:02:35 | [diff] [blame] | 8445 | javascript_dialog_dismiss_notifier_.reset(); |
Charlie Reis | 9607736 | 2019-07-04 00:34:04 | [diff] [blame] | 8446 | |
[email protected] | beb440c | 2009-11-06 04:08:54 | [diff] [blame] | 8447 | if (is_showing_before_unload_dialog_ && !success) { |
clamy | bc6f415 | 2016-04-01 20:14:23 | [diff] [blame] | 8448 | // It is possible for the current RenderFrameHost to have changed in the |
| 8449 | // meantime. Do not reset the navigation state in that case. |
| 8450 | if (rfh && rfh == rfh->frame_tree_node()->current_frame_host()) { |
clamy | 44e84ce | 2016-02-22 15:38:25 | [diff] [blame] | 8451 | rfh->frame_tree_node()->BeforeUnloadCanceled(); |
Carlos Caballero | ede6f8c | 2021-01-28 11:01:50 | [diff] [blame] | 8452 | rfh->frame_tree()->controller().DiscardNonCommittedEntries(); |
clamy | bc6f415 | 2016-04-01 20:14:23 | [diff] [blame] | 8453 | } |
[email protected] | ec8e8b0 | 2013-06-19 04:57:10 | [diff] [blame] | 8454 | |
Charles Reis | 15d60b13 | 2017-11-03 17:43:47 | [diff] [blame] | 8455 | // Update the URL display either way, to avoid showing a stale URL. |
| 8456 | NotifyNavigationStateChanged(INVALIDATE_TYPE_URL); |
| 8457 | |
Andrew Grieve | e19cd93e | 2021-01-22 05:43:06 | [diff] [blame] | 8458 | observers_.NotifyObservers( |
| 8459 | &WebContentsObserver::BeforeUnloadDialogCancelled); |
[email protected] | beb440c | 2009-11-06 04:08:54 | [diff] [blame] | 8460 | } |
[email protected] | 87de04b0 | 2014-04-08 22:14:49 | [diff] [blame] | 8461 | |
Dave Tapuska | cdf545cc | 2020-01-23 18:38:52 | [diff] [blame] | 8462 | std::move(response_callback).Run(success, user_input); |
Pavel Feldman | 3c1842b | 2017-08-02 05:00:16 | [diff] [blame] | 8463 | |
Dave Tapuska | cdf545cc | 2020-01-23 18:38:52 | [diff] [blame] | 8464 | std::vector<protocol::PageHandler*> page_handlers = |
| 8465 | protocol::PageHandler::EnabledForWebContents(this); |
| 8466 | for (auto* handler : page_handlers) |
| 8467 | handler->DidCloseJavaScriptDialog(success, user_input); |
avi | 1c51fb6 | 2017-02-27 20:43:03 | [diff] [blame] | 8468 | |
| 8469 | is_showing_javascript_dialog_ = false; |
| 8470 | is_showing_before_unload_dialog_ = false; |
[email protected] | beb440c | 2009-11-06 04:08:54 | [diff] [blame] | 8471 | } |
| 8472 | |
Paul Semel | 3e24104 | 2022-10-11 12:57:31 | [diff] [blame] | 8473 | RenderFrameHostManager* WebContentsImpl::GetRenderManager() { |
Carlos Caballero | b65b6e3a | 2021-11-15 10:09:00 | [diff] [blame] | 8474 | return primary_frame_tree_.root()->render_manager(); |
[email protected] | fa944cb8 | 2013-11-15 17:51:21 | [diff] [blame] | 8475 | } |
| 8476 | |
[email protected] | f849734 | 2013-02-05 22:15:02 | [diff] [blame] | 8477 | BrowserPluginGuest* WebContentsImpl::GetBrowserPluginGuest() const { |
[email protected] | 7a846df | 2012-09-20 19:17:39 | [diff] [blame] | 8478 | return browser_plugin_guest_.get(); |
| 8479 | } |
| 8480 | |
Albert J. Wong | c55cc64d | 2018-10-12 02:50:04 | [diff] [blame] | 8481 | void WebContentsImpl::SetBrowserPluginGuest( |
| 8482 | std::unique_ptr<BrowserPluginGuest> guest) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 8483 | OPTIONAL_TRACE_EVENT0("content", "WebContentsImpl::SetBrowserPluginGuest"); |
Albert J. Wong | c55cc64d | 2018-10-12 02:50:04 | [diff] [blame] | 8484 | DCHECK(!browser_plugin_guest_); |
| 8485 | DCHECK(guest); |
| 8486 | browser_plugin_guest_ = std::move(guest); |
[email protected] | 738f57a | 2013-06-29 21:06:54 | [diff] [blame] | 8487 | } |
| 8488 | |
Aidan Wolter | 52bcc12a | 2018-11-14 03:27:29 | [diff] [blame] | 8489 | base::UnguessableToken WebContentsImpl::GetAudioGroupId() { |
Aidan Wolter | 52bcc12a | 2018-11-14 03:27:29 | [diff] [blame] | 8490 | return GetAudioStreamFactory()->group_id(); |
| 8491 | } |
| 8492 | |
Miyoung Shin | 5709ab0 | 2020-02-19 06:50:41 | [diff] [blame] | 8493 | const std::vector<blink::mojom::FaviconURLPtr>& |
| 8494 | WebContentsImpl::GetFaviconURLs() { |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 8495 | return GetPrimaryMainFrame()->FaviconURLs(); |
Mounir Lamouri | 7b4a1f23 | 2019-12-23 16:32:10 | [diff] [blame] | 8496 | } |
| 8497 | |
Mugdha Lakhani | 0a0d786 | 2020-07-29 09:58:45 | [diff] [blame] | 8498 | // The Mac implementation of the next two methods is in |
| 8499 | // web_contents_impl_mac.mm |
Xiaohan Wang | 24ec934 | 2022-01-15 17:34:22 | [diff] [blame] | 8500 | #if !BUILDFLAG(IS_MAC) |
Mugdha Lakhani | 0a0d786 | 2020-07-29 09:58:45 | [diff] [blame] | 8501 | |
| 8502 | void WebContentsImpl::Resize(const gfx::Rect& new_bounds) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 8503 | OPTIONAL_TRACE_EVENT0("content", "WebContentsImpl::Resize"); |
Mugdha Lakhani | 0a0d786 | 2020-07-29 09:58:45 | [diff] [blame] | 8504 | #if defined(USE_AURA) |
| 8505 | aura::Window* window = GetNativeView(); |
| 8506 | window->SetBounds(gfx::Rect(window->bounds().origin(), new_bounds.size())); |
Xiaohan Wang | 24ec934 | 2022-01-15 17:34:22 | [diff] [blame] | 8507 | #elif BUILDFLAG(IS_ANDROID) |
Mugdha Lakhani | 0a0d786 | 2020-07-29 09:58:45 | [diff] [blame] | 8508 | content::RenderWidgetHostView* view = GetRenderWidgetHostView(); |
| 8509 | if (view) |
| 8510 | view->SetBounds(new_bounds); |
| 8511 | #endif |
| 8512 | } |
| 8513 | |
| 8514 | gfx::Size WebContentsImpl::GetSize() { |
| 8515 | #if defined(USE_AURA) |
| 8516 | aura::Window* window = GetNativeView(); |
| 8517 | return window->bounds().size(); |
Xiaohan Wang | 24ec934 | 2022-01-15 17:34:22 | [diff] [blame] | 8518 | #elif BUILDFLAG(IS_ANDROID) |
Mugdha Lakhani | 0a0d786 | 2020-07-29 09:58:45 | [diff] [blame] | 8519 | ui::ViewAndroid* view_android = GetNativeView(); |
| 8520 | return view_android->bounds().size(); |
Dave Tapuska | a1cc370 | 2023-02-03 20:43:24 | [diff] [blame] | 8521 | #elif BUILDFLAG(IS_IOS) |
| 8522 | // TODO(crbug.com/1411704): Implement me. |
| 8523 | NOTREACHED(); |
| 8524 | return gfx::Size(); |
Mugdha Lakhani | 0a0d786 | 2020-07-29 09:58:45 | [diff] [blame] | 8525 | #endif |
| 8526 | } |
| 8527 | |
Xiaohan Wang | 24ec934 | 2022-01-15 17:34:22 | [diff] [blame] | 8528 | #endif // !BUILDFLAG(IS_MAC) |
Mugdha Lakhani | 0a0d786 | 2020-07-29 09:58:45 | [diff] [blame] | 8529 | |
Mike Jackson | f116a2e | 2021-06-09 16:48:30 | [diff] [blame] | 8530 | gfx::Rect WebContentsImpl::GetWindowsControlsOverlayRect() const { |
| 8531 | return window_controls_overlay_rect_; |
| 8532 | } |
| 8533 | |
Hassan Talat | d96dbc0 | 2021-01-20 08:02:04 | [diff] [blame] | 8534 | void WebContentsImpl::UpdateWindowControlsOverlay( |
Amanda Baker | 228ec48 | 2021-03-24 19:50:08 | [diff] [blame] | 8535 | const gfx::Rect& bounding_rect) { |
Mike Jackson | f116a2e | 2021-06-09 16:48:30 | [diff] [blame] | 8536 | if (window_controls_overlay_rect_ == bounding_rect) |
| 8537 | return; |
| 8538 | |
| 8539 | window_controls_overlay_rect_ = bounding_rect; |
| 8540 | |
| 8541 | // Updates to the |window_controls_overlay_rect_| are sent via |
| 8542 | // the VisualProperties message. |
| 8543 | if (RenderWidgetHost* render_widget_host = |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 8544 | GetPrimaryMainFrame()->GetRenderWidgetHost()) |
Mike Jackson | f116a2e | 2021-06-09 16:48:30 | [diff] [blame] | 8545 | render_widget_host->SynchronizeVisualProperties(); |
Marijn Kruisselbrink | 61cdd4d | 2023-01-26 02:47:48 | [diff] [blame] | 8546 | |
| 8547 | view_->UpdateWindowControlsOverlay(bounding_rect); |
Hassan Talat | d96dbc0 | 2021-01-20 08:02:04 | [diff] [blame] | 8548 | } |
| 8549 | |
[email protected] | f849734 | 2013-02-05 22:15:02 | [diff] [blame] | 8550 | BrowserPluginEmbedder* WebContentsImpl::GetBrowserPluginEmbedder() const { |
[email protected] | 7a846df | 2012-09-20 19:17:39 | [diff] [blame] | 8551 | return browser_plugin_embedder_.get(); |
| 8552 | } |
[email protected] | 8ff00d7 | 2012-10-23 19:12:21 | [diff] [blame] | 8553 | |
fsamuel | 60b4228 | 2015-03-10 03:29:14 | [diff] [blame] | 8554 | void WebContentsImpl::CreateBrowserPluginEmbedderIfNecessary() { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 8555 | OPTIONAL_TRACE_EVENT0( |
| 8556 | "content", "WebContentsImpl::CreateBrowserPluginEmbedderIfNecessary"); |
fsamuel | 60b4228 | 2015-03-10 03:29:14 | [diff] [blame] | 8557 | if (browser_plugin_embedder_) |
| 8558 | return; |
| 8559 | browser_plugin_embedder_.reset(BrowserPluginEmbedder::Create(this)); |
| 8560 | } |
| 8561 | |
danakj | a0bce4c95 | 2020-05-12 20:40:28 | [diff] [blame] | 8562 | gfx::Size WebContentsImpl::GetSizeForMainFrame() { |
| 8563 | if (delegate_) { |
| 8564 | // The delegate has a chance to specify a size independent of the UI. |
| 8565 | gfx::Size delegate_size = delegate_->GetSizeForNewRenderView(this); |
| 8566 | if (!delegate_size.IsEmpty()) |
| 8567 | return delegate_size; |
| 8568 | } |
| 8569 | |
| 8570 | // Device emulation, when enabled, can specify a size independent of the UI. |
| 8571 | if (!device_emulation_size_.IsEmpty()) |
| 8572 | return device_emulation_size_; |
| 8573 | |
| 8574 | return GetContainerBounds().size(); |
[email protected] | dc0fd43 | 2013-08-27 15:29:21 | [diff] [blame] | 8575 | } |
| 8576 | |
Carlos Caballero | 6ff6ace | 2021-02-05 16:53:00 | [diff] [blame] | 8577 | void WebContentsImpl::OnFrameTreeNodeDestroyed(FrameTreeNode* node) { |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 8578 | OPTIONAL_TRACE_EVENT1("content", "WebContentsImpl::OnFrameTreeNodeDestroyed", |
Carlos Caballero | bf362496 | 2021-04-02 01:26:02 | [diff] [blame] | 8579 | "node", node); |
Dave Tapuska | 54c76a03 | 2021-10-27 22:10:42 | [diff] [blame] | 8580 | // If we are the focused frame tree and are destroying the root node |
| 8581 | // reassign the focused frame tree node. |
Arthur Sonzogni | f6785ec | 2022-12-05 10:11:50 | [diff] [blame] | 8582 | if (!node->parent() && GetFocusedFrameTree() == &node->frame_tree() && |
| 8583 | &node->frame_tree() != &primary_frame_tree_) { |
Dave Tapuska | 54c76a03 | 2021-10-27 22:10:42 | [diff] [blame] | 8584 | FrameTreeNode* frame_in_embedder = |
| 8585 | node->render_manager()->GetOuterDelegateNode(); |
Arthur Sonzogni | f6785ec | 2022-12-05 10:11:50 | [diff] [blame] | 8586 | SetFocusedFrameTree(frame_in_embedder ? &frame_in_embedder->frame_tree() |
Carlos Caballero | b65b6e3a | 2021-11-15 10:09:00 | [diff] [blame] | 8587 | : &primary_frame_tree_); |
Dave Tapuska | 54c76a03 | 2021-10-27 22:10:42 | [diff] [blame] | 8588 | } |
| 8589 | |
Andrew Grieve | e19cd93e | 2021-01-22 05:43:06 | [diff] [blame] | 8590 | observers_.NotifyObservers(&WebContentsObserver::FrameDeleted, |
Carlos Caballero | bf362496 | 2021-04-02 01:26:02 | [diff] [blame] | 8591 | node->frame_tree_node_id()); |
[email protected] | 9b159a5 | 2013-10-03 17:24:55 | [diff] [blame] | 8592 | } |
| 8593 | |
[email protected] | 222f582 | 2014-02-05 23:40:49 | [diff] [blame] | 8594 | void WebContentsImpl::OnPreferredSizeChanged(const gfx::Size& old_size) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 8595 | OPTIONAL_TRACE_EVENT0("content", "WebContentsImpl::OnPreferredSizeChanged"); |
[email protected] | 222f582 | 2014-02-05 23:40:49 | [diff] [blame] | 8596 | if (!delegate_) |
| 8597 | return; |
| 8598 | const gfx::Size new_size = GetPreferredSize(); |
| 8599 | if (new_size != old_size) |
| 8600 | delegate_->UpdatePreferredSize(this, new_size); |
| 8601 | } |
| 8602 | |
Miyoung Shin | 072d405 | 2020-07-13 07:52:44 | [diff] [blame] | 8603 | std::unique_ptr<WebUIImpl> WebContentsImpl::CreateWebUI( |
Ian Barkley-Yeung | bda77fb | 2020-12-08 04:26:46 | [diff] [blame] | 8604 | RenderFrameHostImpl* frame_host, |
Miyoung Shin | 072d405 | 2020-07-13 07:52:44 | [diff] [blame] | 8605 | const GURL& url) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 8606 | TRACE_EVENT2("content", "WebContentsImpl::CreateWebUI", "frame_host", |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 8607 | frame_host, "url", url); |
Miyoung Shin | 072d405 | 2020-07-13 07:52:44 | [diff] [blame] | 8608 | std::unique_ptr<WebUIImpl> web_ui = |
| 8609 | std::make_unique<WebUIImpl>(this, frame_host); |
Lei Zhang | ee3b63ad | 2018-06-06 23:25:31 | [diff] [blame] | 8610 | std::unique_ptr<WebUIController> controller( |
avi | e865b1d | 2016-10-24 19:42:59 | [diff] [blame] | 8611 | WebUIControllerFactoryRegistry::GetInstance() |
Lei Zhang | ee3b63ad | 2018-06-06 23:25:31 | [diff] [blame] | 8612 | ->CreateWebUIControllerForURL(web_ui.get(), url)); |
dbeam | a1b926a | 2015-08-31 23:17:51 | [diff] [blame] | 8613 | if (controller) { |
Lei Zhang | ee3b63ad | 2018-06-06 23:25:31 | [diff] [blame] | 8614 | web_ui->SetController(std::move(controller)); |
dbeam | a1b926a | 2015-08-31 23:17:51 | [diff] [blame] | 8615 | return web_ui; |
| 8616 | } |
| 8617 | |
avi | e865b1d | 2016-10-24 19:42:59 | [diff] [blame] | 8618 | return nullptr; |
dbeam | a1b926a | 2015-08-31 23:17:51 | [diff] [blame] | 8619 | } |
| 8620 | |
Lucas Furukawa Gadani | e1c5dfda | 2018-11-29 17:57:41 | [diff] [blame] | 8621 | FindRequestManager* WebContentsImpl::GetFindRequestManager() { |
| 8622 | for (auto* contents = this; contents; |
paulmeyer | feafc2d | 2017-04-25 21:46:40 | [diff] [blame] | 8623 | contents = contents->GetOuterWebContents()) { |
| 8624 | if (contents->find_request_manager_) |
| 8625 | return contents->find_request_manager_.get(); |
| 8626 | } |
| 8627 | |
| 8628 | return nullptr; |
| 8629 | } |
| 8630 | |
paulmeyer | c0b762b | 2016-04-13 11:55:17 | [diff] [blame] | 8631 | FindRequestManager* WebContentsImpl::GetOrCreateFindRequestManager() { |
paulmeyer | feafc2d | 2017-04-25 21:46:40 | [diff] [blame] | 8632 | if (FindRequestManager* manager = GetFindRequestManager()) |
| 8633 | return manager; |
| 8634 | |
Ehsan Karamad | 6beb2ea | 2018-11-25 18:15:13 | [diff] [blame] | 8635 | DCHECK(!browser_plugin_guest_ || GetOuterWebContents()); |
| 8636 | |
paulmeyer | feafc2d | 2017-04-25 21:46:40 | [diff] [blame] | 8637 | // No existing FindRequestManager found, so one must be created. |
Fergal Daly | 55b6d72 | 2020-09-11 07:56:33 | [diff] [blame] | 8638 | find_request_manager_ = std::make_unique<FindRequestManager>(this); |
paulmeyer | feafc2d | 2017-04-25 21:46:40 | [diff] [blame] | 8639 | |
| 8640 | // Concurrent find sessions must not overlap, so destroy any existing |
| 8641 | // FindRequestManagers in any inner WebContentses. |
Lucas Furukawa Gadani | 2ec00c8 | 2018-12-14 15:53:16 | [diff] [blame] | 8642 | for (WebContents* contents : GetWebContentsAndAllInner()) { |
| 8643 | auto* web_contents_impl = static_cast<WebContentsImpl*>(contents); |
| 8644 | if (web_contents_impl == this) |
paulmeyer | feafc2d | 2017-04-25 21:46:40 | [diff] [blame] | 8645 | continue; |
Lucas Furukawa Gadani | 2ec00c8 | 2018-12-14 15:53:16 | [diff] [blame] | 8646 | if (web_contents_impl->find_request_manager_) { |
| 8647 | web_contents_impl->find_request_manager_->StopFinding( |
| 8648 | STOP_FIND_ACTION_CLEAR_SELECTION); |
| 8649 | web_contents_impl->find_request_manager_.release(); |
paulmeyer | feafc2d | 2017-04-25 21:46:40 | [diff] [blame] | 8650 | } |
| 8651 | } |
paulmeyer | c0b762b | 2016-04-13 11:55:17 | [diff] [blame] | 8652 | |
| 8653 | return find_request_manager_.get(); |
| 8654 | } |
| 8655 | |
| 8656 | void WebContentsImpl::NotifyFindReply(int request_id, |
| 8657 | int number_of_matches, |
| 8658 | const gfx::Rect& selection_rect, |
| 8659 | int active_match_ordinal, |
| 8660 | bool final_update) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 8661 | OPTIONAL_TRACE_EVENT0("content", "WebContentsImpl::NotifyFindReply"); |
Takashi Toyoshima | ea534ef2 | 2021-07-21 03:27:59 | [diff] [blame] | 8662 | if (delegate_ && !IsBeingDestroyed() && |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 8663 | !GetPrimaryMainFrame()->GetProcess()->FastShutdownStarted()) { |
paulmeyer | c0b762b | 2016-04-13 11:55:17 | [diff] [blame] | 8664 | delegate_->FindReply(this, request_id, number_of_matches, selection_rect, |
| 8665 | active_match_ordinal, final_update); |
| 8666 | } |
| 8667 | } |
| 8668 | |
ortuno | 467e579 | 2016-06-10 04:32:39 | [diff] [blame] | 8669 | void WebContentsImpl::IncrementBluetoothConnectedDeviceCount() { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 8670 | OPTIONAL_TRACE_EVENT0( |
| 8671 | "content", "WebContentsImpl::IncrementBluetoothConnectedDeviceCount"); |
ortuno | 467e579 | 2016-06-10 04:32:39 | [diff] [blame] | 8672 | // Trying to invalidate the tab state while being destroyed could result in a |
| 8673 | // use after free. |
| 8674 | if (IsBeingDestroyed()) { |
| 8675 | return; |
| 8676 | } |
| 8677 | // Notify for UI updates if the state changes. |
| 8678 | bluetooth_connected_device_count_++; |
| 8679 | if (bluetooth_connected_device_count_ == 1) { |
Francois Doray | 66766a57 | 2019-12-17 20:56:49 | [diff] [blame] | 8680 | OnIsConnectedToBluetoothDeviceChanged(true); |
ortuno | 467e579 | 2016-06-10 04:32:39 | [diff] [blame] | 8681 | } |
| 8682 | } |
| 8683 | |
| 8684 | void WebContentsImpl::DecrementBluetoothConnectedDeviceCount() { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 8685 | OPTIONAL_TRACE_EVENT0( |
| 8686 | "content", "WebContentsImpl::DecrementBluetoothConnectedDeviceCount"); |
ortuno | 467e579 | 2016-06-10 04:32:39 | [diff] [blame] | 8687 | // Trying to invalidate the tab state while being destroyed could result in a |
| 8688 | // use after free. |
| 8689 | if (IsBeingDestroyed()) { |
| 8690 | return; |
| 8691 | } |
| 8692 | // Notify for UI updates if the state changes. |
Lukasz Anforowicz | e9ae372 | 2017-09-29 17:37:38 | [diff] [blame] | 8693 | DCHECK_NE(bluetooth_connected_device_count_, 0u); |
ortuno | 467e579 | 2016-06-10 04:32:39 | [diff] [blame] | 8694 | bluetooth_connected_device_count_--; |
| 8695 | if (bluetooth_connected_device_count_ == 0) { |
Francois Doray | 66766a57 | 2019-12-17 20:56:49 | [diff] [blame] | 8696 | OnIsConnectedToBluetoothDeviceChanged(false); |
| 8697 | } |
| 8698 | } |
| 8699 | |
| 8700 | void WebContentsImpl::OnIsConnectedToBluetoothDeviceChanged( |
| 8701 | bool is_connected_to_bluetooth_device) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 8702 | OPTIONAL_TRACE_EVENT0( |
| 8703 | "content", "WebContentsImpl::OnIsConnectedToBluetoothDeviceChanged"); |
Francois Doray | 66766a57 | 2019-12-17 20:56:49 | [diff] [blame] | 8704 | NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB); |
Andrew Grieve | e19cd93e | 2021-01-22 05:43:06 | [diff] [blame] | 8705 | observers_.NotifyObservers( |
| 8706 | &WebContentsObserver::OnIsConnectedToBluetoothDeviceChanged, |
| 8707 | is_connected_to_bluetooth_device); |
ortuno | 467e579 | 2016-06-10 04:32:39 | [diff] [blame] | 8708 | } |
| 8709 | |
Ovidio Henriquez | 76696f6 | 2020-07-08 03:06:59 | [diff] [blame] | 8710 | void WebContentsImpl::IncrementBluetoothScanningSessionsCount() { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 8711 | OPTIONAL_TRACE_EVENT0( |
| 8712 | "content", "WebContentsImpl::IncrementBluetoothScanningSessionsCount"); |
Ovidio Henriquez | 76696f6 | 2020-07-08 03:06:59 | [diff] [blame] | 8713 | // Trying to invalidate the tab state while being destroyed could result in a |
| 8714 | // use after free. |
| 8715 | if (IsBeingDestroyed()) |
| 8716 | return; |
| 8717 | |
| 8718 | // Notify for UI updates if the state changes. |
| 8719 | bluetooth_scanning_sessions_count_++; |
| 8720 | if (bluetooth_scanning_sessions_count_ == 1) |
| 8721 | NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB); |
| 8722 | } |
| 8723 | |
| 8724 | void WebContentsImpl::DecrementBluetoothScanningSessionsCount() { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 8725 | OPTIONAL_TRACE_EVENT0( |
| 8726 | "content", "WebContentsImpl::DecrementBluetoothScanningSessionsCount"); |
Ovidio Henriquez | 76696f6 | 2020-07-08 03:06:59 | [diff] [blame] | 8727 | // Trying to invalidate the tab state while being destroyed could result in a |
| 8728 | // use after free. |
| 8729 | if (IsBeingDestroyed()) |
| 8730 | return; |
| 8731 | |
| 8732 | DCHECK_NE(0u, bluetooth_scanning_sessions_count_); |
| 8733 | bluetooth_scanning_sessions_count_--; |
| 8734 | if (bluetooth_scanning_sessions_count_ == 0) |
| 8735 | NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB); |
| 8736 | } |
| 8737 | |
Reilly Grant | 5e7c79b2 | 2019-04-09 17:26:20 | [diff] [blame] | 8738 | void WebContentsImpl::IncrementSerialActiveFrameCount() { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 8739 | OPTIONAL_TRACE_EVENT0("content", |
| 8740 | "WebContentsImpl::IncrementSerialActiveFrameCount"); |
Reilly Grant | 5e7c79b2 | 2019-04-09 17:26:20 | [diff] [blame] | 8741 | // Trying to invalidate the tab state while being destroyed could result in a |
| 8742 | // use after free. |
| 8743 | if (IsBeingDestroyed()) |
| 8744 | return; |
| 8745 | |
| 8746 | // Notify for UI updates if the state changes. |
| 8747 | serial_active_frame_count_++; |
| 8748 | if (serial_active_frame_count_ == 1) |
| 8749 | NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB); |
| 8750 | } |
| 8751 | |
| 8752 | void WebContentsImpl::DecrementSerialActiveFrameCount() { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 8753 | OPTIONAL_TRACE_EVENT0("content", |
| 8754 | "WebContentsImpl::DecrementSerialActiveFrameCount"); |
Reilly Grant | 5e7c79b2 | 2019-04-09 17:26:20 | [diff] [blame] | 8755 | // Trying to invalidate the tab state while being destroyed could result in a |
| 8756 | // use after free. |
| 8757 | if (IsBeingDestroyed()) |
| 8758 | return; |
| 8759 | |
| 8760 | // Notify for UI updates if the state changes. |
| 8761 | DCHECK_NE(0u, serial_active_frame_count_); |
| 8762 | serial_active_frame_count_--; |
| 8763 | if (serial_active_frame_count_ == 0) |
| 8764 | NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB); |
| 8765 | } |
| 8766 | |
Matt Reynolds | e8c6c1f | 2019-11-02 09:53:53 | [diff] [blame] | 8767 | void WebContentsImpl::IncrementHidActiveFrameCount() { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 8768 | OPTIONAL_TRACE_EVENT0("content", |
| 8769 | "WebContentsImpl::IncrementHidActiveFrameCount"); |
Matt Reynolds | e8c6c1f | 2019-11-02 09:53:53 | [diff] [blame] | 8770 | // Trying to invalidate the tab state while being destroyed could result in a |
| 8771 | // use after free. |
| 8772 | if (IsBeingDestroyed()) |
| 8773 | return; |
| 8774 | |
| 8775 | // Notify for UI updates if the active frame count transitions from zero to |
| 8776 | // non-zero. |
| 8777 | hid_active_frame_count_++; |
| 8778 | if (hid_active_frame_count_ == 1) |
| 8779 | NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB); |
| 8780 | } |
| 8781 | |
| 8782 | void WebContentsImpl::DecrementHidActiveFrameCount() { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 8783 | OPTIONAL_TRACE_EVENT0("content", |
| 8784 | "WebContentsImpl::DecrementHidActiveFrameCount"); |
Matt Reynolds | e8c6c1f | 2019-11-02 09:53:53 | [diff] [blame] | 8785 | // Trying to invalidate the tab state while being destroyed could result in a |
| 8786 | // use after free. |
| 8787 | if (IsBeingDestroyed()) |
| 8788 | return; |
| 8789 | |
| 8790 | // Notify for UI updates if the active frame count transitions from non-zero |
| 8791 | // to zero. |
| 8792 | DCHECK_NE(0u, hid_active_frame_count_); |
| 8793 | hid_active_frame_count_--; |
| 8794 | if (hid_active_frame_count_ == 0) |
| 8795 | NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB); |
| 8796 | } |
| 8797 | |
Matt Reynolds | ed00ca7e7 | 2022-08-18 20:56:20 | [diff] [blame] | 8798 | void WebContentsImpl::OnIsConnectedToUsbDeviceChanged( |
| 8799 | bool is_connected_to_usb_device) { |
| 8800 | OPTIONAL_TRACE_EVENT0("content", |
| 8801 | "WebContentsImpl::OnIsConnectedToUsbDeviceChanged"); |
| 8802 | NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB); |
| 8803 | observers_.NotifyObservers( |
| 8804 | &WebContentsObserver::OnIsConnectedToUsbDeviceChanged, |
| 8805 | is_connected_to_usb_device); |
| 8806 | } |
| 8807 | |
| 8808 | void WebContentsImpl::IncrementUsbActiveFrameCount() { |
| 8809 | OPTIONAL_TRACE_EVENT0("content", |
| 8810 | "WebContentsImpl::IncrementUsbActiveFrameCount"); |
| 8811 | // Trying to invalidate the tab state while being destroyed could result in a |
| 8812 | // use after free. |
| 8813 | if (IsBeingDestroyed()) |
| 8814 | return; |
| 8815 | |
| 8816 | // Notify for UI updates if the active frame count transitions from zero to |
| 8817 | // non-zero. |
| 8818 | usb_active_frame_count_++; |
| 8819 | if (usb_active_frame_count_ == 1) |
| 8820 | OnIsConnectedToUsbDeviceChanged(true); |
| 8821 | } |
| 8822 | |
| 8823 | void WebContentsImpl::DecrementUsbActiveFrameCount() { |
| 8824 | OPTIONAL_TRACE_EVENT0("content", |
| 8825 | "WebContentsImpl::DecrementUsbActiveFrameCount"); |
| 8826 | // Trying to invalidate the tab state while being destroyed could result in a |
| 8827 | // use after free. |
| 8828 | if (IsBeingDestroyed()) |
| 8829 | return; |
| 8830 | |
| 8831 | // Notify for UI updates if the active frame count transitions from non-zero |
| 8832 | // to zero. |
| 8833 | DCHECK_NE(0u, usb_active_frame_count_); |
| 8834 | usb_active_frame_count_--; |
| 8835 | if (usb_active_frame_count_ == 0) |
| 8836 | OnIsConnectedToUsbDeviceChanged(false); |
| 8837 | } |
| 8838 | |
Austin Sullivan | afefb72 | 2021-01-14 01:26:39 | [diff] [blame] | 8839 | void WebContentsImpl::IncrementFileSystemAccessHandleCount() { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 8840 | OPTIONAL_TRACE_EVENT0( |
Austin Sullivan | afefb72 | 2021-01-14 01:26:39 | [diff] [blame] | 8841 | "content", "WebContentsImpl::IncrementFileSystemAccessHandleCount"); |
Marijn Kruisselbrink | 2905104 | 2019-08-06 22:56:55 | [diff] [blame] | 8842 | // Trying to invalidate the tab state while being destroyed could result in a |
| 8843 | // use after free. |
| 8844 | if (IsBeingDestroyed()) |
| 8845 | return; |
| 8846 | |
| 8847 | // Notify for UI updates if the state changes. Need both TYPE_TAB and TYPE_URL |
| 8848 | // to update both the tab-level usage indicator and the usage indicator in the |
| 8849 | // omnibox. |
Austin Sullivan | afefb72 | 2021-01-14 01:26:39 | [diff] [blame] | 8850 | file_system_access_handle_count_++; |
| 8851 | if (file_system_access_handle_count_ == 1) { |
Marijn Kruisselbrink | 2905104 | 2019-08-06 22:56:55 | [diff] [blame] | 8852 | NotifyNavigationStateChanged(static_cast<content::InvalidateTypes>( |
| 8853 | INVALIDATE_TYPE_TAB | INVALIDATE_TYPE_URL)); |
| 8854 | } |
| 8855 | } |
| 8856 | |
Austin Sullivan | afefb72 | 2021-01-14 01:26:39 | [diff] [blame] | 8857 | void WebContentsImpl::DecrementFileSystemAccessHandleCount() { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 8858 | OPTIONAL_TRACE_EVENT0( |
Austin Sullivan | afefb72 | 2021-01-14 01:26:39 | [diff] [blame] | 8859 | "content", "WebContentsImpl::DecrementFileSystemAccessHandleCount"); |
Marijn Kruisselbrink | 2905104 | 2019-08-06 22:56:55 | [diff] [blame] | 8860 | // Trying to invalidate the tab state while being destroyed could result in a |
| 8861 | // use after free. |
| 8862 | if (IsBeingDestroyed()) |
| 8863 | return; |
| 8864 | |
| 8865 | // Notify for UI updates if the state changes. Need both TYPE_TAB and TYPE_URL |
| 8866 | // to update both the tab-level usage indicator and the usage indicator in the |
| 8867 | // omnibox. |
Austin Sullivan | afefb72 | 2021-01-14 01:26:39 | [diff] [blame] | 8868 | DCHECK_NE(0u, file_system_access_handle_count_); |
| 8869 | file_system_access_handle_count_--; |
| 8870 | if (file_system_access_handle_count_ == 0) { |
Marijn Kruisselbrink | 2905104 | 2019-08-06 22:56:55 | [diff] [blame] | 8871 | NotifyNavigationStateChanged(static_cast<content::InvalidateTypes>( |
| 8872 | INVALIDATE_TYPE_TAB | INVALIDATE_TYPE_URL)); |
| 8873 | } |
| 8874 | } |
| 8875 | |
zqzhang | 8ac4900 | 2017-03-16 21:51:35 | [diff] [blame] | 8876 | void WebContentsImpl::SetHasPersistentVideo(bool has_persistent_video) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 8877 | OPTIONAL_TRACE_EVENT2("content", "WebContentsImpl::SetHasPersistentVideo", |
| 8878 | "has_persistent_video", has_persistent_video, |
| 8879 | "had_persistent_value", has_persistent_video_); |
zqzhang | 8ac4900 | 2017-03-16 21:51:35 | [diff] [blame] | 8880 | if (has_persistent_video_ == has_persistent_video) |
mlamouri | 5cd9ae8 | 2017-02-18 11:05:09 | [diff] [blame] | 8881 | return; |
| 8882 | |
zqzhang | 8ac4900 | 2017-03-16 21:51:35 | [diff] [blame] | 8883 | has_persistent_video_ = has_persistent_video; |
mlamouri | 5cd9ae8 | 2017-02-18 11:05:09 | [diff] [blame] | 8884 | NotifyPreferencesChanged(); |
zqzhang | 8ac4900 | 2017-03-16 21:51:35 | [diff] [blame] | 8885 | media_web_contents_observer()->RequestPersistentVideo(has_persistent_video); |
Jonathan Ross | c1d5cc7 | 2022-11-03 22:09:39 | [diff] [blame] | 8886 | |
| 8887 | // This is Picture-in-Picture on Android S+. |
| 8888 | if (auto* view = GetRenderWidgetHostView()) { |
| 8889 | static_cast<RenderWidgetHostViewBase*>(view)->SetHasPersistentVideo( |
| 8890 | has_persistent_video); |
| 8891 | } |
mlamouri | 5cd9ae8 | 2017-02-18 11:05:09 | [diff] [blame] | 8892 | } |
| 8893 | |
Ian Vollick | 5d5c37f | 2019-06-19 22:50:57 | [diff] [blame] | 8894 | void WebContentsImpl::SetSpatialNavigationDisabled(bool disabled) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 8895 | OPTIONAL_TRACE_EVENT2( |
| 8896 | "content", "WebContentsImpl::SetSpatialNavigationDisabled", "disabled", |
| 8897 | disabled, "was_disabled", is_spatial_navigation_disabled_); |
Ian Vollick | 5d5c37f | 2019-06-19 22:50:57 | [diff] [blame] | 8898 | if (is_spatial_navigation_disabled_ == disabled) |
| 8899 | return; |
| 8900 | |
| 8901 | is_spatial_navigation_disabled_ = disabled; |
| 8902 | NotifyPreferencesChanged(); |
| 8903 | } |
| 8904 | |
Mahesh Machavolu | 6cb8018 | 2022-07-22 08:09:31 | [diff] [blame] | 8905 | void WebContentsImpl::SetStylusHandwritingEnabled(bool enabled) { |
| 8906 | if (stylus_handwriting_enabled_ == enabled) |
| 8907 | return; |
| 8908 | stylus_handwriting_enabled_ = enabled; |
| 8909 | NotifyPreferencesChanged(); |
| 8910 | } |
| 8911 | |
François Beaufort | 1388f289 | 2022-01-29 08:22:47 | [diff] [blame] | 8912 | PictureInPictureResult WebContentsImpl::EnterPictureInPicture() { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 8913 | OPTIONAL_TRACE_EVENT0("content", "WebContentsImpl::EnterPictureInPicture"); |
François Beaufort | 1388f289 | 2022-01-29 08:22:47 | [diff] [blame] | 8914 | return delegate_ ? delegate_->EnterPictureInPicture(this) |
| 8915 | : PictureInPictureResult::kNotSupported; |
Mounir Lamouri | 11e9ef43 | 2018-05-22 03:10:16 | [diff] [blame] | 8916 | } |
| 8917 | |
Mounir Lamouri | 6d759e1 | 2018-05-16 20:01:30 | [diff] [blame] | 8918 | void WebContentsImpl::ExitPictureInPicture() { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 8919 | OPTIONAL_TRACE_EVENT0("content", "WebContentsImpl::ExitPictureInPicture"); |
Mounir Lamouri | 6d759e1 | 2018-05-16 20:01:30 | [diff] [blame] | 8920 | if (delegate_) |
| 8921 | delegate_->ExitPictureInPicture(); |
| 8922 | } |
| 8923 | |
Alexander Cooper | 987d046cc | 2021-04-14 00:07:21 | [diff] [blame] | 8924 | void WebContentsImpl::OnXrHasRenderTarget( |
| 8925 | const viz::FrameSinkId& frame_sink_id) { |
| 8926 | xr_render_target_ = frame_sink_id; |
| 8927 | observers_.NotifyObservers(&WebContentsObserver::CaptureTargetChanged); |
| 8928 | } |
| 8929 | |
| 8930 | viz::FrameSinkId WebContentsImpl::GetCaptureFrameSinkId() { |
| 8931 | if (xr_render_target_.is_valid()) |
| 8932 | return xr_render_target_; |
| 8933 | |
| 8934 | RenderWidgetHostView* host_view = GetRenderWidgetHostView(); |
| 8935 | if (!host_view) { |
| 8936 | return {}; |
| 8937 | } |
| 8938 | |
| 8939 | RenderWidgetHostViewBase* base_view = |
| 8940 | static_cast<RenderWidgetHostViewBase*>(host_view); |
| 8941 | return base_view->GetFrameSinkId(); |
| 8942 | } |
| 8943 | |
Xiaohan Wang | 24ec934 | 2022-01-15 17:34:22 | [diff] [blame] | 8944 | #if BUILDFLAG(IS_ANDROID) |
paulmeyer | c0b762b | 2016-04-13 11:55:17 | [diff] [blame] | 8945 | void WebContentsImpl::NotifyFindMatchRectsReply( |
| 8946 | int version, |
| 8947 | const std::vector<gfx::RectF>& rects, |
| 8948 | const gfx::RectF& active_rect) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 8949 | OPTIONAL_TRACE_EVENT0("content", |
| 8950 | "WebContentsImpl::NotifyFindMatchRectsReply"); |
paulmeyer | c0b762b | 2016-04-13 11:55:17 | [diff] [blame] | 8951 | if (delegate_) |
| 8952 | delegate_->FindMatchRectsReply(this, version, rects, active_rect); |
| 8953 | } |
| 8954 | #endif |
| 8955 | |
[email protected] | 34ff1cfc | 2014-08-20 06:16:05 | [diff] [blame] | 8956 | void WebContentsImpl::SetForceDisableOverscrollContent(bool force_disable) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 8957 | OPTIONAL_TRACE_EVENT1("content", |
| 8958 | "WebContentsImpl::SetForceDisableOverscrollContent", |
| 8959 | "force_disable", force_disable); |
[email protected] | 34ff1cfc | 2014-08-20 06:16:05 | [diff] [blame] | 8960 | force_disable_overscroll_content_ = force_disable; |
| 8961 | if (view_) |
| 8962 | view_->SetOverscrollControllerEnabled(CanOverscrollContent()); |
| 8963 | } |
| 8964 | |
Jianzhou Feng | d8720c7 | 2018-01-18 03:06:30 | [diff] [blame] | 8965 | bool WebContentsImpl::SetDeviceEmulationSize(const gfx::Size& new_size) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 8966 | OPTIONAL_TRACE_EVENT0("content", "WebContentsImpl::SetDeviceEmulationSize"); |
Jianzhou Feng | d8720c7 | 2018-01-18 03:06:30 | [diff] [blame] | 8967 | device_emulation_size_ = new_size; |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 8968 | RenderWidgetHostView* rwhv = GetPrimaryMainFrame()->GetView(); |
Jianzhou Feng | d8720c7 | 2018-01-18 03:06:30 | [diff] [blame] | 8969 | |
| 8970 | const gfx::Size current_size = rwhv->GetViewBounds().size(); |
| 8971 | if (view_size_before_emulation_.IsEmpty()) |
| 8972 | view_size_before_emulation_ = current_size; |
| 8973 | |
| 8974 | if (current_size != new_size) |
| 8975 | rwhv->SetSize(new_size); |
| 8976 | |
| 8977 | return current_size != new_size; |
| 8978 | } |
| 8979 | |
| 8980 | void WebContentsImpl::ClearDeviceEmulationSize() { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 8981 | OPTIONAL_TRACE_EVENT0("content", "WebContentsImpl::ClearDeviceEmulationSize"); |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 8982 | RenderWidgetHostView* rwhv = GetPrimaryMainFrame()->GetView(); |
Jianzhou Feng | d8720c7 | 2018-01-18 03:06:30 | [diff] [blame] | 8983 | // WebContentsView could get resized during emulation, which also resizes |
| 8984 | // RWHV. If it happens, assume user would like to keep using the size after |
| 8985 | // emulation. |
| 8986 | // TODO(jzfeng): Prohibit resizing RWHV through any other means (at least when |
| 8987 | // WebContentsView size changes). |
Dmitry Gozman | c6370718 | 2018-03-06 00:09:39 | [diff] [blame] | 8988 | if (!view_size_before_emulation_.IsEmpty() && rwhv && |
| 8989 | rwhv->GetViewBounds().size() == device_emulation_size_) { |
Jianzhou Feng | d8720c7 | 2018-01-18 03:06:30 | [diff] [blame] | 8990 | rwhv->SetSize(view_size_before_emulation_); |
Dmitry Gozman | c6370718 | 2018-03-06 00:09:39 | [diff] [blame] | 8991 | } |
Jianzhou Feng | d8720c7 | 2018-01-18 03:06:30 | [diff] [blame] | 8992 | device_emulation_size_ = gfx::Size(); |
| 8993 | view_size_before_emulation_ = gfx::Size(); |
| 8994 | } |
| 8995 | |
Max Morin | 5bc74f5 | 2018-05-09 07:00:21 | [diff] [blame] | 8996 | ForwardingAudioStreamFactory* WebContentsImpl::GetAudioStreamFactory() { |
| 8997 | if (!audio_stream_factory_) { |
Sergey Ulanov | 52d396d | 2022-09-06 23:46:43 | [diff] [blame] | 8998 | std::unique_ptr<AudioStreamBrokerFactory> broker_factory; |
| 8999 | if (delegate_) |
| 9000 | broker_factory = delegate_->CreateAudioStreamBrokerFactory(this); |
| 9001 | if (!broker_factory) |
| 9002 | broker_factory = AudioStreamBrokerFactory::CreateImpl(); |
Max Morin | 5bc74f5 | 2018-05-09 07:00:21 | [diff] [blame] | 9003 | audio_stream_factory_.emplace( |
| 9004 | this, |
Max Morin | ddebb97 | 2018-09-20 10:04:14 | [diff] [blame] | 9005 | // BrowserMainLoop::GetInstance() may be null in unit tests. |
| 9006 | BrowserMainLoop::GetInstance() |
| 9007 | ? static_cast<media::UserInputMonitorBase*>( |
| 9008 | BrowserMainLoop::GetInstance()->user_input_monitor()) |
| 9009 | : nullptr, |
Sergey Ulanov | 52d396d | 2022-09-06 23:46:43 | [diff] [blame] | 9010 | std::move(broker_factory)); |
Max Morin | 5bc74f5 | 2018-05-09 07:00:21 | [diff] [blame] | 9011 | } |
| 9012 | |
| 9013 | return &*audio_stream_factory_; |
| 9014 | } |
| 9015 | |
dalecurtis | 88c24007 | 2015-12-09 02:11:18 | [diff] [blame] | 9016 | void WebContentsImpl::MediaStartedPlaying( |
billorr | 21c005d | 2016-11-17 03:57:04 | [diff] [blame] | 9017 | const WebContentsObserver::MediaPlayerInfo& media_info, |
Mounir Lamouri | 0f5bdf5 | 2019-04-04 16:32:35 | [diff] [blame] | 9018 | const MediaPlayerId& id) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 9019 | OPTIONAL_TRACE_EVENT0("content", "WebContentsImpl::MediaStartedPlaying"); |
billorr | 21c005d | 2016-11-17 03:57:04 | [diff] [blame] | 9020 | if (media_info.has_video) |
| 9021 | currently_playing_video_count_++; |
| 9022 | |
Andrew Grieve | e19cd93e | 2021-01-22 05:43:06 | [diff] [blame] | 9023 | observers_.NotifyObservers(&WebContentsObserver::MediaStartedPlaying, |
| 9024 | media_info, id); |
dalecurtis | 88c24007 | 2015-12-09 02:11:18 | [diff] [blame] | 9025 | } |
| 9026 | |
| 9027 | void WebContentsImpl::MediaStoppedPlaying( |
billorr | 21c005d | 2016-11-17 03:57:04 | [diff] [blame] | 9028 | const WebContentsObserver::MediaPlayerInfo& media_info, |
Mounir Lamouri | 0f5bdf5 | 2019-04-04 16:32:35 | [diff] [blame] | 9029 | const MediaPlayerId& id, |
Becca Hughes | 220bfc10 | 2017-11-14 18:24:07 | [diff] [blame] | 9030 | WebContentsObserver::MediaStoppedReason reason) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 9031 | OPTIONAL_TRACE_EVENT0("content", "WebContentsImpl::MediaStoppedPlaying"); |
billorr | 21c005d | 2016-11-17 03:57:04 | [diff] [blame] | 9032 | if (media_info.has_video) |
| 9033 | currently_playing_video_count_--; |
| 9034 | |
Andrew Grieve | e19cd93e | 2021-01-22 05:43:06 | [diff] [blame] | 9035 | observers_.NotifyObservers(&WebContentsObserver::MediaStoppedPlaying, |
| 9036 | media_info, id, reason); |
billorr | 21c005d | 2016-11-17 03:57:04 | [diff] [blame] | 9037 | } |
| 9038 | |
Mounir Lamouri | 0f5bdf5 | 2019-04-04 16:32:35 | [diff] [blame] | 9039 | void WebContentsImpl::MediaResized(const gfx::Size& size, |
| 9040 | const MediaPlayerId& id) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 9041 | OPTIONAL_TRACE_EVENT0("content", "WebContentsImpl::MediaResized"); |
peconn | 25795152 | 2017-06-09 18:24:59 | [diff] [blame] | 9042 | cached_video_sizes_[id] = size; |
| 9043 | |
Andrew Grieve | e19cd93e | 2021-01-22 05:43:06 | [diff] [blame] | 9044 | observers_.NotifyObservers(&WebContentsObserver::MediaResized, size, id); |
peconn | 25795152 | 2017-06-09 18:24:59 | [diff] [blame] | 9045 | } |
| 9046 | |
Chris Hamilton | c347e10 | 2020-11-18 23:10:21 | [diff] [blame] | 9047 | void WebContentsImpl::MediaEffectivelyFullscreenChanged(bool is_fullscreen) { |
| 9048 | OPTIONAL_TRACE_EVENT1("content", |
| 9049 | "WebContentsImpl::MediaEffectivelyFullscreenChanged", |
| 9050 | "is_fullscreen", is_fullscreen); |
Andrew Grieve | e19cd93e | 2021-01-22 05:43:06 | [diff] [blame] | 9051 | observers_.NotifyObservers( |
| 9052 | &WebContentsObserver::MediaEffectivelyFullscreenChanged, is_fullscreen); |
Chris Hamilton | c347e10 | 2020-11-18 23:10:21 | [diff] [blame] | 9053 | } |
| 9054 | |
Chris Hamilton | c347e10 | 2020-11-18 23:10:21 | [diff] [blame] | 9055 | void WebContentsImpl::MediaDestroyed(const MediaPlayerId& id) { |
| 9056 | OPTIONAL_TRACE_EVENT0("content", "WebContentsImpl::MediaDestroyed"); |
Andrew Grieve | e19cd93e | 2021-01-22 05:43:06 | [diff] [blame] | 9057 | observers_.NotifyObservers(&WebContentsObserver::MediaDestroyed, id); |
Chris Hamilton | c347e10 | 2020-11-18 23:10:21 | [diff] [blame] | 9058 | } |
| 9059 | |
| 9060 | int WebContentsImpl::GetCurrentlyPlayingVideoCount() { |
| 9061 | return currently_playing_video_count_; |
Peter E Conn | eccb34c2 | 2017-09-08 09:37:58 | [diff] [blame] | 9062 | } |
| 9063 | |
Anton Bikineev | f62d1bf | 2021-05-15 17:56:07 | [diff] [blame] | 9064 | absl::optional<gfx::Size> WebContentsImpl::GetFullscreenVideoSize() { |
| 9065 | absl::optional<MediaPlayerId> id = |
Peter Conn | 31c726a | 2017-08-17 11:07:34 | [diff] [blame] | 9066 | media_web_contents_observer_->GetFullscreenVideoMediaPlayerId(); |
Jan Wilken Dörrie | 531be7ca | 2019-06-07 10:05:57 | [diff] [blame] | 9067 | if (id && base::Contains(cached_video_sizes_, id.value())) |
Anton Bikineev | f62d1bf | 2021-05-15 17:56:07 | [diff] [blame] | 9068 | return absl::optional<gfx::Size>(cached_video_sizes_[id.value()]); |
| 9069 | return absl::nullopt; |
peconn | 25795152 | 2017-06-09 18:24:59 | [diff] [blame] | 9070 | } |
| 9071 | |
Kevin McNee | fb86fcf | 2021-02-26 23:20:57 | [diff] [blame] | 9072 | void WebContentsImpl::AudioContextPlaybackStarted(RenderFrameHostImpl* host, |
Raymond Toy | e413650c | 2018-08-31 00:12:47 | [diff] [blame] | 9073 | int context_id) { |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 9074 | OPTIONAL_TRACE_EVENT1("content", |
| 9075 | "WebContentsImpl::AudioContextPlaybackStarted", |
| 9076 | "render_frame_host", host); |
Raymond Toy | e413650c | 2018-08-31 00:12:47 | [diff] [blame] | 9077 | WebContentsObserver::AudioContextId audio_context_id(host, context_id); |
Andrew Grieve | e19cd93e | 2021-01-22 05:43:06 | [diff] [blame] | 9078 | observers_.NotifyObservers(&WebContentsObserver::AudioContextPlaybackStarted, |
| 9079 | audio_context_id); |
Raymond Toy | e413650c | 2018-08-31 00:12:47 | [diff] [blame] | 9080 | } |
| 9081 | |
Kevin McNee | fb86fcf | 2021-02-26 23:20:57 | [diff] [blame] | 9082 | void WebContentsImpl::AudioContextPlaybackStopped(RenderFrameHostImpl* host, |
Raymond Toy | e413650c | 2018-08-31 00:12:47 | [diff] [blame] | 9083 | int context_id) { |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 9084 | OPTIONAL_TRACE_EVENT1("content", |
| 9085 | "WebContentsImpl::AudioContextPlaybackStopped", |
| 9086 | "render_frame_host", host); |
Raymond Toy | e413650c | 2018-08-31 00:12:47 | [diff] [blame] | 9087 | WebContentsObserver::AudioContextId audio_context_id(host, context_id); |
Andrew Grieve | e19cd93e | 2021-01-22 05:43:06 | [diff] [blame] | 9088 | observers_.NotifyObservers(&WebContentsObserver::AudioContextPlaybackStopped, |
| 9089 | audio_context_id); |
Raymond Toy | e413650c | 2018-08-31 00:12:47 | [diff] [blame] | 9090 | } |
| 9091 | |
Kevin McNee | fb86fcf | 2021-02-26 23:20:57 | [diff] [blame] | 9092 | void WebContentsImpl::OnFrameAudioStateChanged(RenderFrameHostImpl* host, |
Chris Hamilton | 16ca64a | 2020-05-14 23:13:17 | [diff] [blame] | 9093 | bool is_audible) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 9094 | OPTIONAL_TRACE_EVENT2("content", "WebContentsImpl::OnFrameAudioStateChanged", |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 9095 | "render_frame_host", host, "is_audible", is_audible); |
Andrew Grieve | e19cd93e | 2021-01-22 05:43:06 | [diff] [blame] | 9096 | observers_.NotifyObservers(&WebContentsObserver::OnFrameAudioStateChanged, |
| 9097 | host, is_audible); |
Chris Hamilton | 16ca64a | 2020-05-14 23:13:17 | [diff] [blame] | 9098 | } |
| 9099 | |
evliu | 7f736f0 | 2019-11-07 17:43:50 | [diff] [blame] | 9100 | media::MediaMetricsProvider::RecordAggregateWatchTimeCallback |
Minoru Chikamune | a602642f | 2021-08-20 04:39:13 | [diff] [blame] | 9101 | WebContentsImpl::GetRecordAggregateWatchTimeCallback( |
| 9102 | const GURL& page_main_frame_last_committed_url) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 9103 | OPTIONAL_TRACE_EVENT0("content", |
| 9104 | "WebContentsImpl::RecordAggregateWatchTimeCallback"); |
Becca Hughes | 5365244 | 2020-03-17 15:35:42 | [diff] [blame] | 9105 | if (!delegate_ || !delegate_->GetDelegateWeakPtr()) |
| 9106 | return base::DoNothing(); |
| 9107 | |
evliu | 7f736f0 | 2019-11-07 17:43:50 | [diff] [blame] | 9108 | return base::BindRepeating( |
Minoru Chikamune | a602642f | 2021-08-20 04:39:13 | [diff] [blame] | 9109 | [](base::WeakPtr<WebContentsDelegate> delegate, |
| 9110 | GURL page_main_frame_last_committed_url, |
Becca Hughes | 5365244 | 2020-03-17 15:35:42 | [diff] [blame] | 9111 | base::TimeDelta total_watch_time, base::TimeDelta time_stamp, |
| 9112 | bool has_video, bool has_audio) { |
evliu | a283c9e | 2019-11-08 22:57:22 | [diff] [blame] | 9113 | content::MediaPlayerWatchTime watch_time( |
Minoru Chikamune | a602642f | 2021-08-20 04:39:13 | [diff] [blame] | 9114 | page_main_frame_last_committed_url, |
Mike West | 800532c | 2021-10-14 09:26:52 | [diff] [blame] | 9115 | page_main_frame_last_committed_url.DeprecatedGetOriginAsURL(), |
| 9116 | total_watch_time, time_stamp, has_video, has_audio); |
evliu | 7f736f0 | 2019-11-07 17:43:50 | [diff] [blame] | 9117 | |
| 9118 | // Save the watch time if the delegate is still alive. |
| 9119 | if (delegate) |
| 9120 | delegate->MediaWatchTimeChanged(watch_time); |
| 9121 | }, |
Minoru Chikamune | a602642f | 2021-08-20 04:39:13 | [diff] [blame] | 9122 | delegate_->GetDelegateWeakPtr(), page_main_frame_last_committed_url); |
evliu | 7f736f0 | 2019-11-07 17:43:50 | [diff] [blame] | 9123 | } |
| 9124 | |
Kevin McNee | 5f59438 | 2021-05-06 23:18:23 | [diff] [blame] | 9125 | std::vector<FrameTreeNode*> WebContentsImpl::GetUnattachedOwnedNodes( |
| 9126 | RenderFrameHostImpl* owner) { |
| 9127 | std::vector<FrameTreeNode*> unattached_owned_nodes; |
| 9128 | BrowserPluginGuestManager* guest_manager = |
| 9129 | GetBrowserContext()->GetGuestManager(); |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 9130 | if (owner == GetPrimaryMainFrame() && guest_manager) { |
Kevin McNee | 5f59438 | 2021-05-06 23:18:23 | [diff] [blame] | 9131 | guest_manager->ForEachUnattachedGuest( |
| 9132 | this, base::BindRepeating( |
| 9133 | [](std::vector<FrameTreeNode*>& unattached_owned_nodes, |
| 9134 | WebContents* guest_contents) { |
| 9135 | unattached_owned_nodes.push_back( |
| 9136 | static_cast<WebContentsImpl*>(guest_contents) |
Carlos Caballero | b65b6e3a | 2021-11-15 10:09:00 | [diff] [blame] | 9137 | ->primary_frame_tree_.root()); |
Kevin McNee | 5f59438 | 2021-05-06 23:18:23 | [diff] [blame] | 9138 | }, |
| 9139 | std::ref(unattached_owned_nodes))); |
| 9140 | } |
| 9141 | // TODO(mcnee): Also include orphaned portals here. |
| 9142 | // See https://crbug.com/1196715 |
| 9143 | return unattached_owned_nodes; |
| 9144 | } |
| 9145 | |
Darwin Huang | 6195d04 | 2021-02-12 22:36:00 | [diff] [blame] | 9146 | void WebContentsImpl::IsClipboardPasteContentAllowed( |
Roger Tawa | 03ffdbd | 2020-01-16 18:12:54 | [diff] [blame] | 9147 | const GURL& url, |
| 9148 | const ui::ClipboardFormatType& data_type, |
| 9149 | const std::string& data, |
Darwin Huang | 6195d04 | 2021-02-12 22:36:00 | [diff] [blame] | 9150 | IsClipboardPasteContentAllowedCallback callback) { |
Dominique Fauteux-Chapleau | 9924438 | 2020-07-08 20:37:00 | [diff] [blame] | 9151 | ++suppress_unresponsive_renderer_count_; |
Darwin Huang | 6195d04 | 2021-02-12 22:36:00 | [diff] [blame] | 9152 | GetContentClient()->browser()->IsClipboardPasteContentAllowed( |
Dominique Fauteux-Chapleau | 9924438 | 2020-07-08 20:37:00 | [diff] [blame] | 9153 | this, url, data_type, data, |
Darwin Huang | 6195d04 | 2021-02-12 22:36:00 | [diff] [blame] | 9154 | base::BindOnce( |
| 9155 | &WebContentsImpl::IsClipboardPasteContentAllowedWrapperCallback, |
| 9156 | weak_factory_.GetWeakPtr(), std::move(callback))); |
Dominique Fauteux-Chapleau | 9924438 | 2020-07-08 20:37:00 | [diff] [blame] | 9157 | } |
| 9158 | |
Darwin Huang | 6195d04 | 2021-02-12 22:36:00 | [diff] [blame] | 9159 | void WebContentsImpl::IsClipboardPasteContentAllowedWrapperCallback( |
| 9160 | IsClipboardPasteContentAllowedCallback callback, |
Roger Tawa | c617026 | 2022-10-28 23:48:52 | [diff] [blame] | 9161 | const absl::optional<std::string>& data) { |
| 9162 | std::move(callback).Run(data); |
Dominique Fauteux-Chapleau | 9924438 | 2020-07-08 20:37:00 | [diff] [blame] | 9163 | --suppress_unresponsive_renderer_count_; |
Roger Tawa | 03ffdbd | 2020-01-16 18:12:54 | [diff] [blame] | 9164 | } |
| 9165 | |
Robert Sesek | 5047c41 | 2021-08-09 17:36:18 | [diff] [blame] | 9166 | void WebContentsImpl::BindScreenOrientation( |
| 9167 | RenderFrameHost* rfh, |
| 9168 | mojo::PendingAssociatedReceiver<device::mojom::ScreenOrientation> |
| 9169 | receiver) { |
| 9170 | screen_orientation_provider_->BindScreenOrientation(rfh, std::move(receiver)); |
| 9171 | } |
| 9172 | |
Lan Wei | f81c6e7c | 2020-02-12 16:46:09 | [diff] [blame] | 9173 | bool WebContentsImpl::HasSeenRecentScreenOrientationChange() { |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 9174 | static constexpr base::TimeDelta kMaxInterval = base::Seconds(1); |
Lan Wei | f81c6e7c | 2020-02-12 16:46:09 | [diff] [blame] | 9175 | base::TimeDelta delta = |
| 9176 | ui::EventTimeForNow() - last_screen_orientation_change_time_; |
Mike Wasserman | db8f4f0 | 2020-10-06 01:18:37 | [diff] [blame] | 9177 | // Return whether a screen orientation change happened in the last 1 second. |
Lan Wei | f81c6e7c | 2020-02-12 16:46:09 | [diff] [blame] | 9178 | return delta <= kMaxInterval; |
| 9179 | } |
| 9180 | |
Mike Wasserman | db8f4f0 | 2020-10-06 01:18:37 | [diff] [blame] | 9181 | bool WebContentsImpl::IsTransientAllowFullscreenActive() const { |
| 9182 | return transient_allow_fullscreen_.IsActive(); |
| 9183 | } |
| 9184 | |
Dave Tapuska | dfff738 | 2021-04-23 19:46:41 | [diff] [blame] | 9185 | bool WebContentsImpl::IsBackForwardCacheSupported() { |
| 9186 | if (!GetDelegate()) |
| 9187 | return false; |
| 9188 | return GetDelegate()->IsBackForwardCacheSupported(); |
| 9189 | } |
| 9190 | |
Sreeja Kamishetty | d64b993d | 2022-02-14 12:04:42 | [diff] [blame] | 9191 | FrameTree* WebContentsImpl::LoadingTree() { |
| 9192 | return &GetPrimaryFrameTree(); |
| 9193 | } |
| 9194 | |
Lan Wei | f81c6e7c | 2020-02-12 16:46:09 | [diff] [blame] | 9195 | void WebContentsImpl::DidChangeScreenOrientation() { |
| 9196 | last_screen_orientation_change_time_ = ui::EventTimeForNow(); |
| 9197 | } |
| 9198 | |
William Liu | cc35b315f | 2022-11-17 20:42:04 | [diff] [blame] | 9199 | bool WebContentsImpl::ShowPopupMenu(RenderFrameHostImpl* render_frame_host, |
| 9200 | const gfx::Rect& bounds) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 9201 | OPTIONAL_TRACE_EVENT1("content", "WebContentsImpl::ShowPopupMenu", |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 9202 | "render_frame_host", render_frame_host); |
Lingqi Chi | f8d423e | 2021-08-12 08:00:18 | [diff] [blame] | 9203 | DCHECK(render_frame_host->IsActive()); |
John Abd-El-Malek | 5b1d725 | 2021-03-05 21:03:08 | [diff] [blame] | 9204 | if (show_poup_menu_callback_) { |
| 9205 | std::move(show_poup_menu_callback_).Run(bounds); |
| 9206 | return true; |
Miyoung Shin | 4d947d2 | 2020-04-24 04:48:33 | [diff] [blame] | 9207 | } |
| 9208 | return false; |
| 9209 | } |
| 9210 | |
Francois Doray | e616115 | 2018-03-27 22:05:37 | [diff] [blame] | 9211 | void WebContentsImpl::UpdateWebContentsVisibility(Visibility visibility) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 9212 | OPTIONAL_TRACE_EVENT1("content", |
| 9213 | "WebContentsImpl::UpdateWebContentsVisibility", |
| 9214 | "visibility", visibility); |
Francois Doray | 1d3372a | 2020-12-17 19:00:11 | [diff] [blame] | 9215 | // Occlusion is disabled when |
Francois Doray | 3f2afbd | 2018-04-06 19:18:18 | [diff] [blame] | 9216 | // |switches::kDisableBackgroundingOccludedWindowsForTesting| is specified on |
| 9217 | // the command line (to avoid flakiness in browser tests). |
| 9218 | const bool occlusion_is_disabled = |
Francois Doray | e616115 | 2018-03-27 22:05:37 | [diff] [blame] | 9219 | base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 9220 | switches::kDisableBackgroundingOccludedWindowsForTesting); |
| 9221 | if (occlusion_is_disabled && visibility == Visibility::OCCLUDED) |
| 9222 | visibility = Visibility::VISIBLE; |
| 9223 | |
tapted | 65ff2ea7 | 2016-03-01 23:39:00 | [diff] [blame] | 9224 | if (!did_first_set_visible_) { |
Francois Doray | e616115 | 2018-03-27 22:05:37 | [diff] [blame] | 9225 | if (visibility == Visibility::VISIBLE) { |
| 9226 | // A WebContents created with CreateParams::initially_hidden = false |
| 9227 | // starts with GetVisibility() == Visibility::VISIBLE even though it is |
| 9228 | // not really visible. Call WasShown() when it becomes visible for real as |
| 9229 | // the page load mechanism and some WebContentsObserver rely on that. |
tapted | 65ff2ea7 | 2016-03-01 23:39:00 | [diff] [blame] | 9230 | WasShown(); |
Francois Doray | fe4a177 | 2018-02-17 04:17:09 | [diff] [blame] | 9231 | did_first_set_visible_ = true; |
tapted | 65ff2ea7 | 2016-03-01 23:39:00 | [diff] [blame] | 9232 | } |
Francois Doray | e616115 | 2018-03-27 22:05:37 | [diff] [blame] | 9233 | |
| 9234 | // Trust the initial visibility of the WebContents and do not switch it to |
| 9235 | // HIDDEN or OCCLUDED before it becomes VISIBLE for real. Doing so would |
| 9236 | // result in destroying resources that would immediately be recreated (e.g. |
| 9237 | // UpdateWebContents(HIDDEN) can be called when a WebContents is added to a |
| 9238 | // hidden window that is about to be shown). |
| 9239 | |
tapted | 65ff2ea7 | 2016-03-01 23:39:00 | [diff] [blame] | 9240 | return; |
| 9241 | } |
Francois Doray | e616115 | 2018-03-27 22:05:37 | [diff] [blame] | 9242 | |
| 9243 | if (visibility == visibility_) |
tapted | 65ff2ea7 | 2016-03-01 23:39:00 | [diff] [blame] | 9244 | return; |
| 9245 | |
Collin Baker | 989e088 | 2019-11-01 01:27:17 | [diff] [blame] | 9246 | UpdateVisibilityAndNotifyPageAndView(visibility); |
tapted | 65ff2ea7 | 2016-03-01 23:39:00 | [diff] [blame] | 9247 | } |
| 9248 | |
aelias | 5252baa | 2016-04-10 01:18:02 | [diff] [blame] | 9249 | void WebContentsImpl::UpdateOverridingUserAgent() { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 9250 | OPTIONAL_TRACE_EVENT0("content", |
| 9251 | "WebContentsImpl::UpdateOverridingUserAgent"); |
mlamouri | 5cd9ae8 | 2017-02-18 11:05:09 | [diff] [blame] | 9252 | NotifyPreferencesChanged(); |
aelias | 5252baa | 2016-04-10 01:18:02 | [diff] [blame] | 9253 | } |
| 9254 | |
avi | d53461d | 2016-02-25 17:15:04 | [diff] [blame] | 9255 | void WebContentsImpl::SetJavaScriptDialogManagerForTesting( |
| 9256 | JavaScriptDialogManager* dialog_manager) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 9257 | OPTIONAL_TRACE_EVENT0( |
| 9258 | "content", "WebContentsImpl::SetJavaScriptDialogManagerForTesting"); |
avi | d53461d | 2016-02-25 17:15:04 | [diff] [blame] | 9259 | dialog_manager_ = dialog_manager; |
| 9260 | } |
| 9261 | |
Jeremy Roman | d597d337 | 2021-07-09 23:36:56 | [diff] [blame] | 9262 | void WebContentsImpl::ShowInsecureLocalhostWarningIfNeeded(PageImpl& page) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 9263 | OPTIONAL_TRACE_EVENT0( |
| 9264 | "content", "WebContentsImpl::ShowInsecureLocalhostWarningIfNeeded"); |
Jeremy Roman | d597d337 | 2021-07-09 23:36:56 | [diff] [blame] | 9265 | |
falken | 52a56e3 | 2016-12-08 05:02:40 | [diff] [blame] | 9266 | bool allow_localhost = base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 9267 | switches::kAllowInsecureLocalhost); |
| 9268 | if (!allow_localhost) |
| 9269 | return; |
| 9270 | |
Jeremy Roman | d597d337 | 2021-07-09 23:36:56 | [diff] [blame] | 9271 | RenderFrameHostImpl& frame = page.GetMainDocument(); |
| 9272 | NavigationEntry* entry = |
| 9273 | frame.frame_tree()->controller().GetLastCommittedEntry(); |
Rob Wu | f79b3ba | 2018-01-14 01:54:31 | [diff] [blame] | 9274 | if (!entry || !net::IsLocalhost(entry->GetURL())) |
falken | 52a56e3 | 2016-12-08 05:02:40 | [diff] [blame] | 9275 | return; |
| 9276 | |
Jeremy Roman | d597d337 | 2021-07-09 23:36:56 | [diff] [blame] | 9277 | SSLStatus ssl_status = entry->GetSSL(); |
Matt Mueller | 93651144 | 2019-09-03 18:15:12 | [diff] [blame] | 9278 | if (!net::IsCertStatusError(ssl_status.cert_status)) |
falken | 52a56e3 | 2016-12-08 05:02:40 | [diff] [blame] | 9279 | return; |
| 9280 | |
Jeremy Roman | d597d337 | 2021-07-09 23:36:56 | [diff] [blame] | 9281 | frame.AddMessageToConsole(blink::mojom::ConsoleMessageLevel::kWarning, |
| 9282 | "This site does not have a valid SSL " |
| 9283 | "certificate! Without SSL, your site's and " |
| 9284 | "visitors' data is vulnerable to theft and " |
| 9285 | "tampering. Get a valid SSL certificate before " |
| 9286 | " releasing your website to the public."); |
falken | 52a56e3 | 2016-12-08 05:02:40 | [diff] [blame] | 9287 | } |
| 9288 | |
ekaramad | f6750aa | 2017-06-06 18:29:42 | [diff] [blame] | 9289 | bool WebContentsImpl::IsShowingContextMenuOnPage() const { |
| 9290 | return showing_context_menu_; |
| 9291 | } |
| 9292 | |
Min Qin | a904f330 | 2018-02-13 23:33:34 | [diff] [blame] | 9293 | download::DownloadUrlParameters::RequestHeadersType |
| 9294 | WebContentsImpl::ParseDownloadHeaders(const std::string& headers) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 9295 | OPTIONAL_TRACE_EVENT1("content", "WebContentsImpl::ParseDownloadHeaders", |
| 9296 | "headers", headers); |
Min Qin | a904f330 | 2018-02-13 23:33:34 | [diff] [blame] | 9297 | download::DownloadUrlParameters::RequestHeadersType request_headers; |
Megan Jablonski | 2f6a4c5 | 2017-07-10 23:01:25 | [diff] [blame] | 9298 | for (const base::StringPiece& key_value : base::SplitStringPiece( |
| 9299 | headers, "\r\n", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY)) { |
| 9300 | std::vector<std::string> pair = base::SplitString( |
| 9301 | key_value, ":", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
| 9302 | if (2ul == pair.size()) |
| 9303 | request_headers.push_back(make_pair(pair[0], pair[1])); |
| 9304 | } |
| 9305 | return request_headers; |
| 9306 | } |
| 9307 | |
lukasza | 6f8ac62 | 2017-06-06 03:10:20 | [diff] [blame] | 9308 | void WebContentsImpl::SetOpenerForNewContents(FrameTreeNode* opener, |
| 9309 | bool opener_suppressed) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 9310 | OPTIONAL_TRACE_EVENT0("content", "WebContentsImpl::SetOpenerForNewContents"); |
lukasza | 6f8ac62 | 2017-06-06 03:10:20 | [diff] [blame] | 9311 | if (opener) { |
Carlos Caballero | 6a99dac | 2021-11-03 10:41:17 | [diff] [blame] | 9312 | FrameTreeNode* new_root = GetPrimaryFrameTree().root(); |
lukasza | 6f8ac62 | 2017-06-06 03:10:20 | [diff] [blame] | 9313 | |
| 9314 | // For the "original opener", track the opener's main frame instead, because |
| 9315 | // if the opener is a subframe, the opener tracking could be easily bypassed |
| 9316 | // by spawning from a subframe and deleting the subframe. |
| 9317 | // https://crbug.com/705316 |
Arthur Sonzogni | f6785ec | 2022-12-05 10:11:50 | [diff] [blame] | 9318 | new_root->SetOriginalOpener(opener->frame_tree().root()); |
Danil Somsikov | 259aa65f | 2022-11-11 20:49:44 | [diff] [blame] | 9319 | new_root->SetOpenerDevtoolsFrameToken( |
| 9320 | opener->current_frame_host()->devtools_frame_token()); |
Rakina Zata Amni | c7ffea88 | 2021-08-16 10:04:28 | [diff] [blame] | 9321 | opened_by_another_window_ = true; |
lukasza | 6f8ac62 | 2017-06-06 03:10:20 | [diff] [blame] | 9322 | |
| 9323 | if (!opener_suppressed) { |
| 9324 | new_root->SetOpener(opener); |
lukasza | 6f8ac62 | 2017-06-06 03:10:20 | [diff] [blame] | 9325 | } |
| 9326 | } |
| 9327 | } |
| 9328 | |
Mounir Lamouri | 0f5bdf5 | 2019-04-04 16:32:35 | [diff] [blame] | 9329 | void WebContentsImpl::MediaMutedStatusChanged(const MediaPlayerId& id, |
| 9330 | bool muted) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 9331 | OPTIONAL_TRACE_EVENT1("content", "WebContentsImpl::MediaMutedStatusChanged", |
| 9332 | "muted", muted); |
Andrew Grieve | e19cd93e | 2021-01-22 05:43:06 | [diff] [blame] | 9333 | observers_.NotifyObservers(&WebContentsObserver::MediaMutedStatusChanged, id, |
| 9334 | muted); |
Becca Hughes | 9f6fd4b8 | 2017-06-15 10:01:40 | [diff] [blame] | 9335 | } |
| 9336 | |
EhsanK | 2075c7e | 2017-08-21 02:42:39 | [diff] [blame] | 9337 | void WebContentsImpl::SetVisibilityForChildViews(bool visible) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 9338 | OPTIONAL_TRACE_EVENT1("content", |
| 9339 | "WebContentsImpl::SetVisibilityForChildViews", |
| 9340 | "visible", visible); |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 9341 | GetPrimaryMainFrame()->SetVisibilityForChildViews(visible); |
EhsanK | 2075c7e | 2017-08-21 02:42:39 | [diff] [blame] | 9342 | } |
| 9343 | |
Alison Maher | 7f366dc6 | 2020-03-03 19:46:00 | [diff] [blame] | 9344 | void WebContentsImpl::OnNativeThemeUpdated(ui::NativeTheme* observed_theme) { |
Alexander Timin | 5fdeff2 | 2020-09-08 09:20:37 | [diff] [blame] | 9345 | OPTIONAL_TRACE_EVENT0("content", "WebContentsImpl::OnNativeThemeUpdated"); |
Sigurdur Asgeirsson | 103e2fb | 2020-11-10 00:50:16 | [diff] [blame] | 9346 | DCHECK(native_theme_observation_.IsObservingSource(observed_theme)); |
Alison Maher | 7f366dc6 | 2020-03-03 19:46:00 | [diff] [blame] | 9347 | |
| 9348 | bool using_dark_colors = observed_theme->ShouldUseDarkColors(); |
| 9349 | ui::NativeTheme::PreferredColorScheme preferred_color_scheme = |
| 9350 | observed_theme->GetPreferredColorScheme(); |
Alison Maher | 3b11e94 | 2020-10-27 17:17:50 | [diff] [blame] | 9351 | ui::NativeTheme::PreferredContrast preferred_contrast = |
| 9352 | observed_theme->GetPreferredContrast(); |
Alison Maher | 7f366dc6 | 2020-03-03 19:46:00 | [diff] [blame] | 9353 | bool preferences_changed = false; |
| 9354 | |
| 9355 | if (using_dark_colors_ != using_dark_colors) { |
| 9356 | using_dark_colors_ = using_dark_colors; |
| 9357 | preferences_changed = true; |
| 9358 | } |
| 9359 | if (preferred_color_scheme_ != preferred_color_scheme) { |
| 9360 | preferred_color_scheme_ = preferred_color_scheme; |
| 9361 | preferences_changed = true; |
| 9362 | } |
Alison Maher | 3b11e94 | 2020-10-27 17:17:50 | [diff] [blame] | 9363 | if (preferred_contrast_ != preferred_contrast) { |
| 9364 | preferred_contrast_ = preferred_contrast; |
| 9365 | preferences_changed = true; |
| 9366 | } |
Alison Maher | 7f366dc6 | 2020-03-03 19:46:00 | [diff] [blame] | 9367 | |
| 9368 | if (preferences_changed) |
| 9369 | NotifyPreferencesChanged(); |
| 9370 | } |
| 9371 | |
Abigail Klein | 08cfe3f | 2021-04-01 02:09:31 | [diff] [blame] | 9372 | void WebContentsImpl::OnCaptionStyleUpdated() { |
| 9373 | NotifyPreferencesChanged(); |
| 9374 | } |
| 9375 | |
tom | 855c4bf3 | 2021-09-30 00:27:26 | [diff] [blame] | 9376 | void WebContentsImpl::OnColorProviderChanged() { |
tom | 083fe899 | 2021-10-21 17:48:45 | [diff] [blame] | 9377 | // TODO(tluk): Currently changes to theme related web prefs are being |
| 9378 | // propagated in OnNativeThemeUpdated(). This logic should be moved here since |
| 9379 | // there are instances where the system theme may not change but clients |
| 9380 | // manually change the ColorProviderSource currently being observed by this |
| 9381 | // WebContents. |
| 9382 | |
| 9383 | // OnColorProviderChanged() might have been triggered as the result of the |
| 9384 | // observed source being reset. If this is the case fallback to the default |
| 9385 | // source. |
| 9386 | if (!GetColorProviderSource()) { |
| 9387 | SetColorProviderSource(DefaultColorProviderSource::GetInstance()); |
| 9388 | return; |
| 9389 | } |
Tom Lukaszewicz | 22b784a4 | 2021-11-29 22:56:49 | [diff] [blame] | 9390 | observers_.NotifyObservers(&WebContentsObserver::OnColorProviderChanged); |
tom | 855c4bf3 | 2021-09-30 00:27:26 | [diff] [blame] | 9391 | } |
| 9392 | |
Tom Lukaszewicz | 63b3da5 | 2022-01-26 01:37:57 | [diff] [blame] | 9393 | const ui::ColorProvider& WebContentsImpl::GetColorProvider() const { |
tom | 083fe899 | 2021-10-21 17:48:45 | [diff] [blame] | 9394 | auto* source = GetColorProviderSource(); |
| 9395 | DCHECK(source); |
Tom Lukaszewicz | 63b3da5 | 2022-01-26 01:37:57 | [diff] [blame] | 9396 | auto* color_provider = source->GetColorProvider(); |
| 9397 | DCHECK(color_provider); |
| 9398 | return *color_provider; |
tom | 855c4bf3 | 2021-09-30 00:27:26 | [diff] [blame] | 9399 | } |
| 9400 | |
Dave Tapuska | 58a099e | 2020-06-08 21:48:40 | [diff] [blame] | 9401 | blink::mojom::FrameWidgetInputHandler* |
| 9402 | WebContentsImpl::GetFocusedFrameWidgetInputHandler() { |
| 9403 | auto* focused_render_widget_host = |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 9404 | GetFocusedRenderWidgetHost(GetPrimaryMainFrame()->GetRenderWidgetHost()); |
Dave Tapuska | 58a099e | 2020-06-08 21:48:40 | [diff] [blame] | 9405 | if (!focused_render_widget_host) |
Dave Tapuska | e782bea | 2019-07-09 16:21:51 | [diff] [blame] | 9406 | return nullptr; |
Dave Tapuska | 58a099e | 2020-06-08 21:48:40 | [diff] [blame] | 9407 | return focused_render_widget_host->GetFrameWidgetInputHandler(); |
Dave Tapuska | e782bea | 2019-07-09 16:21:51 | [diff] [blame] | 9408 | } |
| 9409 | |
Alexander Timin | e3ec419 | 2020-04-20 16:39:40 | [diff] [blame] | 9410 | ukm::SourceId WebContentsImpl::GetCurrentPageUkmSourceId() { |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 9411 | return GetPrimaryMainFrame()->GetPageUkmSourceId(); |
Alexander Timin | e3ec419 | 2020-04-20 16:39:40 | [diff] [blame] | 9412 | } |
| 9413 | |
Dave Tapuska | 05fcfb6 | 2021-10-18 17:09:03 | [diff] [blame] | 9414 | void WebContentsImpl::ForEachRenderViewHost( |
| 9415 | ForEachRenderViewHostTypes view_mask, |
| 9416 | RenderViewHostIterationCallback on_render_view_host) { |
Yuzu Saijo | 232076f | 2020-05-29 07:14:02 | [diff] [blame] | 9417 | std::set<RenderViewHostImpl*> render_view_hosts; |
| 9418 | |
Dave Tapuska | 05fcfb6 | 2021-10-18 17:09:03 | [diff] [blame] | 9419 | if ((view_mask & (ForEachRenderViewHostTypes::kPrerenderViews | |
| 9420 | ForEachRenderViewHostTypes::kActiveViews)) != 0) { |
| 9421 | ForEachFrameTree(base::BindRepeating( |
| 9422 | [](ForEachRenderViewHostTypes view_mask, |
| 9423 | std::set<RenderViewHostImpl*>& render_view_hosts, |
Arthur Sonzogni | f6785ec | 2022-12-05 10:11:50 | [diff] [blame] | 9424 | FrameTree& frame_tree) { |
Dave Tapuska | 05fcfb6 | 2021-10-18 17:09:03 | [diff] [blame] | 9425 | // Check the view masks. |
Arthur Sonzogni | f6785ec | 2022-12-05 10:11:50 | [diff] [blame] | 9426 | if (frame_tree.is_prerendering()) { |
Dave Tapuska | 05fcfb6 | 2021-10-18 17:09:03 | [diff] [blame] | 9427 | // We are in a prerendering page. |
| 9428 | if ((view_mask & ForEachRenderViewHostTypes::kPrerenderViews) == 0) |
| 9429 | return; |
| 9430 | } else { |
| 9431 | // We are in an active page. |
| 9432 | if ((view_mask & ForEachRenderViewHostTypes::kActiveViews) == 0) |
| 9433 | return; |
| 9434 | } |
Sharon Yang | ed88454 | 2023-02-02 17:33:44 | [diff] [blame] | 9435 | frame_tree.ForEachRenderViewHost( |
| 9436 | [&render_view_hosts](RenderViewHostImpl* rvh) { |
| 9437 | render_view_hosts.insert(rvh); |
| 9438 | }); |
Dave Tapuska | 05fcfb6 | 2021-10-18 17:09:03 | [diff] [blame] | 9439 | }, |
| 9440 | view_mask, std::ref(render_view_hosts))); |
Yuzu Saijo | 232076f | 2020-05-29 07:14:02 | [diff] [blame] | 9441 | } |
| 9442 | |
Dave Tapuska | 05fcfb6 | 2021-10-18 17:09:03 | [diff] [blame] | 9443 | if ((view_mask & ForEachRenderViewHostTypes::kBackForwardCacheViews) != 0) { |
| 9444 | // Add RenderViewHostImpls in BackForwardCache. |
| 9445 | const auto& entries = GetController().GetBackForwardCache().GetEntries(); |
| 9446 | for (const auto& entry : entries) { |
Dave Tapuska | c3e5835 | 2022-09-28 19:05:27 | [diff] [blame] | 9447 | for (const auto& render_view : entry->render_view_hosts()) { |
| 9448 | render_view_hosts.insert(&*render_view); |
| 9449 | } |
Dave Tapuska | 05fcfb6 | 2021-10-18 17:09:03 | [diff] [blame] | 9450 | } |
Yuzu Saijo | 232076f | 2020-05-29 07:14:02 | [diff] [blame] | 9451 | } |
| 9452 | |
Dave Tapuska | 05fcfb6 | 2021-10-18 17:09:03 | [diff] [blame] | 9453 | for (auto* render_view_host : render_view_hosts) |
| 9454 | on_render_view_host.Run(render_view_host); |
Yuzu Saijo | 232076f | 2020-05-29 07:14:02 | [diff] [blame] | 9455 | } |
| 9456 | |
Julie Jeongeun Kim | d4597df1 | 2022-11-11 02:44:51 | [diff] [blame] | 9457 | void WebContentsImpl::NotifyPageBecamePrimary(PageImpl& page) { |
Sreeja Kamishetty | a21b4f6 | 2021-06-25 07:48:25 | [diff] [blame] | 9458 | OPTIONAL_TRACE_EVENT0("content", "WebContentsImpl::PrimaryPageChanged"); |
| 9459 | |
Sreeja Kamishetty | 5346897 | 2021-07-13 11:53:32 | [diff] [blame] | 9460 | DCHECK_EQ(&page, &GetPrimaryPage()); |
| 9461 | |
Dave Tapuska | 765a2f6 | 2021-07-07 20:52:23 | [diff] [blame] | 9462 | // Clear |save_package_| since the primary page changed. |
| 9463 | if (save_package_) { |
| 9464 | save_package_->ClearPage(); |
| 9465 | save_package_.reset(); |
| 9466 | } |
Sreeja Kamishetty | 5346897 | 2021-07-13 11:53:32 | [diff] [blame] | 9467 | observers_.NotifyObservers(&WebContentsObserver::PrimaryPageChanged, page); |
Sreeja Kamishetty | a21b4f6 | 2021-06-25 07:48:25 | [diff] [blame] | 9468 | } |
| 9469 | |
Dominic Farolino | edf44ee | 2021-07-20 23:50:59 | [diff] [blame] | 9470 | int WebContentsImpl::GetOuterDelegateFrameTreeNodeId() { |
| 9471 | return node_.outer_contents_frame_tree_node_id(); |
| 9472 | } |
| 9473 | |
Sreeja Kamishetty | be0ccd6b | 2020-06-09 11:52:50 | [diff] [blame] | 9474 | void WebContentsImpl::RenderFrameHostStateChanged( |
Sreeja Kamishetty | 2771b7d | 2021-04-07 06:34:18 | [diff] [blame] | 9475 | RenderFrameHost* render_frame_host, |
| 9476 | LifecycleState old_state, |
| 9477 | LifecycleState new_state) { |
| 9478 | DCHECK_NE(old_state, new_state); |
Alexander Timin | f785f34 | 2021-03-18 00:00:56 | [diff] [blame] | 9479 | OPTIONAL_TRACE_EVENT2("content", |
| 9480 | "WebContentsImpl::RenderFrameHostStateChanged", |
| 9481 | "render_frame_host", render_frame_host, "states", |
| 9482 | [&](perfetto::TracedValue context) { |
| 9483 | // TODO(crbug.com/1183371): Replace this with passing |
| 9484 | // more parameters to TRACE_EVENT directly when |
| 9485 | // available. |
| 9486 | auto dict = std::move(context).WriteDictionary(); |
| 9487 | dict.Add("old", old_state); |
| 9488 | dict.Add("new", new_state); |
| 9489 | }); |
Sreeja Kamishetty | be0ccd6b | 2020-06-09 11:52:50 | [diff] [blame] | 9490 | |
Tom Burgin | 732656d3 | 2022-02-07 18:33:26 | [diff] [blame] | 9491 | #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_MAC) |
Kevin McNee | 0c88f7af | 2021-04-30 15:17:24 | [diff] [blame] | 9492 | if (old_state == LifecycleState::kActive && !render_frame_host->GetParent()) { |
Bo Liu | 2bceebc | 2021-06-03 15:06:33 | [diff] [blame] | 9493 | // TODO(sreejakshetty): Remove this reset when ColorChooserHolder becomes |
Sreeja Kamishetty | be0ccd6b | 2020-06-09 11:52:50 | [diff] [blame] | 9494 | // per-frame. |
| 9495 | // Close the color chooser popup when RenderFrameHost changes state from |
| 9496 | // kActive. |
Bo Liu | 2bceebc | 2021-06-03 15:06:33 | [diff] [blame] | 9497 | color_chooser_holder_.reset(); |
Sreeja Kamishetty | be0ccd6b | 2020-06-09 11:52:50 | [diff] [blame] | 9498 | } |
Tom Burgin | 732656d3 | 2022-02-07 18:33:26 | [diff] [blame] | 9499 | #endif // BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_MAC) |
Sreeja Kamishetty | 2771b7d | 2021-04-07 06:34:18 | [diff] [blame] | 9500 | |
| 9501 | observers_.NotifyObservers(&WebContentsObserver::RenderFrameHostStateChanged, |
| 9502 | render_frame_host, old_state, new_state); |
Sreeja Kamishetty | be0ccd6b | 2020-06-09 11:52:50 | [diff] [blame] | 9503 | } |
| 9504 | |
Dale Curtis | 7030f25 | 2021-04-07 14:05:31 | [diff] [blame] | 9505 | void WebContentsImpl::DecrementCapturerCount(bool stay_hidden, |
Elad Alon | 0feb660 | 2021-10-14 09:26:36 | [diff] [blame] | 9506 | bool stay_awake, |
| 9507 | bool is_activity) { |
Dale Curtis | 7030f25 | 2021-04-07 14:05:31 | [diff] [blame] | 9508 | OPTIONAL_TRACE_EVENT0("content", "WebContentsImpl::DecrementCapturerCount"); |
| 9509 | if (stay_hidden) |
| 9510 | --hidden_capturer_count_; |
| 9511 | else |
| 9512 | --visible_capturer_count_; |
| 9513 | if (stay_awake) |
| 9514 | --stay_awake_capturer_count_; |
| 9515 | DCHECK_GE(hidden_capturer_count_, 0); |
| 9516 | DCHECK_GE(visible_capturer_count_, 0); |
| 9517 | DCHECK_GE(stay_awake_capturer_count_, 0); |
| 9518 | |
Takashi Toyoshima | ea534ef2 | 2021-07-21 03:27:59 | [diff] [blame] | 9519 | if (IsBeingDestroyed()) |
Dale Curtis | 7030f25 | 2021-04-07 14:05:31 | [diff] [blame] | 9520 | return; |
| 9521 | |
Scott Violet | 7f0b5c3 | 2021-11-04 03:06:22 | [diff] [blame] | 9522 | view_->OnCapturerCountChanged(); |
| 9523 | |
Dale Curtis | 7030f25 | 2021-04-07 14:05:31 | [diff] [blame] | 9524 | const bool is_being_captured = IsBeingCaptured(); |
| 9525 | if (!is_being_captured) { |
| 9526 | const gfx::Size old_size = preferred_size_for_capture_; |
| 9527 | preferred_size_for_capture_ = gfx::Size(); |
| 9528 | OnPreferredSizeChanged(old_size); |
| 9529 | } |
| 9530 | |
| 9531 | if (capture_wake_lock_ && (!is_being_captured || !stay_awake_capturer_count_)) |
| 9532 | capture_wake_lock_->CancelWakeLock(); |
| 9533 | |
Elad Alon | 0feb660 | 2021-10-14 09:26:36 | [diff] [blame] | 9534 | UpdateVisibilityAndNotifyPageAndView(GetVisibility(), is_activity); |
Dale Curtis | 7030f25 | 2021-04-07 14:05:31 | [diff] [blame] | 9535 | } |
| 9536 | |
Khushal | c5eaf22 | 2021-06-30 20:15:48 | [diff] [blame] | 9537 | void WebContentsImpl::NotifyPrimaryMainFrameProcessIsAlive() { |
| 9538 | // The WebContents tracks the process state for the primary main frame's |
| 9539 | // renderer. |
Mahesh Machavolu | 3c4b311 | 2021-10-06 16:20:13 | [diff] [blame] | 9540 | // Consider renderer as terminated when exited with any termination status. |
| 9541 | bool was_renderer_terminated = primary_main_frame_process_status_ != |
| 9542 | base::TERMINATION_STATUS_STILL_RUNNING; |
Khushal | c5eaf22 | 2021-06-30 20:15:48 | [diff] [blame] | 9543 | SetPrimaryMainFrameProcessStatus(base::TERMINATION_STATUS_STILL_RUNNING, 0); |
| 9544 | // Restore the focus to the tab (otherwise the focus will be on the top |
| 9545 | // window). |
Mahesh Machavolu | 3c4b311 | 2021-10-06 16:20:13 | [diff] [blame] | 9546 | if (was_renderer_terminated && !FocusLocationBarByDefault()) { |
Khushal | c5eaf22 | 2021-06-30 20:15:48 | [diff] [blame] | 9547 | if (!delegate_ || delegate_->ShouldFocusPageAfterCrash()) { |
| 9548 | view_->Focus(); |
| 9549 | } |
| 9550 | } |
| 9551 | } |
| 9552 | |
Yoshisato Yanagisawa | 607a7cca | 2021-10-14 02:45:36 | [diff] [blame] | 9553 | void WebContentsImpl::UpdateBrowserControlsState( |
| 9554 | cc::BrowserControlsState constraints, |
| 9555 | cc::BrowserControlsState current, |
| 9556 | bool animate) { |
| 9557 | // Browser controls should be synchronised with the scroll state. Therefore, |
| 9558 | // they are controlled from the renderer by the main RenderFrame(Host). |
Yoshisato Yanagisawa | d016d62d3 | 2021-10-15 04:38:55 | [diff] [blame] | 9559 | GetPrimaryPage().UpdateBrowserControlsState(constraints, current, animate); |
Yoshisato Yanagisawa | 607a7cca | 2021-10-14 02:45:36 | [diff] [blame] | 9560 | } |
| 9561 | |
Joe Mason | 7ae0e2b | 2021-11-01 12:30:36 | [diff] [blame] | 9562 | void WebContentsImpl::SetTabSwitchStartTime(base::TimeTicks start_time, |
| 9563 | bool destination_is_loaded) { |
Joe Mason | 4dc51d4 | 2022-12-17 00:00:14 | [diff] [blame] | 9564 | GetVisibleTimeRequestTrigger().UpdateRequest( |
| 9565 | start_time, destination_is_loaded, |
| 9566 | /*show_reason_tab_switching=*/true, |
| 9567 | /*show_reason_bfcache_restore=*/false); |
Joe Mason | 7ae0e2b | 2021-11-01 12:30:36 | [diff] [blame] | 9568 | } |
| 9569 | |
Joe Mason | 4dc51d4 | 2022-12-17 00:00:14 | [diff] [blame] | 9570 | VisibleTimeRequestTrigger& WebContentsImpl::GetVisibleTimeRequestTrigger() { |
| 9571 | return visible_time_request_trigger_; |
Joe Mason | 7ae0e2b | 2021-11-01 12:30:36 | [diff] [blame] | 9572 | } |
| 9573 | |
Robert Lin | c37fb58 | 2021-11-11 03:18:47 | [diff] [blame] | 9574 | std::unique_ptr<PrerenderHandle> WebContentsImpl::StartPrerendering( |
Asami Doi | cf0f164 | 2021-11-25 05:00:26 | [diff] [blame] | 9575 | const GURL& prerendering_url, |
| 9576 | PrerenderTriggerType trigger_type, |
Robert Lin | 125720c04 | 2022-01-21 01:14:58 | [diff] [blame] | 9577 | const std::string& embedder_histogram_suffix, |
Lingqi Chi | 21d9feb | 2022-02-02 09:42:18 | [diff] [blame] | 9578 | ui::PageTransition page_transition, |
Sreeja Kamishetty | ac12140e | 2022-07-14 22:16:51 | [diff] [blame] | 9579 | PreloadingAttempt* preloading_attempt, |
Lingqi Chi | 21d9feb | 2022-02-02 09:42:18 | [diff] [blame] | 9580 | absl::optional<base::RepeatingCallback<bool(const GURL&)>> |
| 9581 | url_match_predicate) { |
Robert Lin | c37fb58 | 2021-11-11 03:18:47 | [diff] [blame] | 9582 | PrerenderAttributes attributes( |
Asami Doi | cf0f164 | 2021-11-25 05:00:26 | [diff] [blame] | 9583 | prerendering_url, trigger_type, embedder_histogram_suffix, |
| 9584 | content::Referrer(), /*initiator_origin=*/absl::nullopt, prerendering_url, |
Hiroki Nakagawa | b930733e | 2023-01-04 16:40:52 | [diff] [blame] | 9585 | content::ChildProcessHost::kInvalidUniqueID, GetWeakPtr(), |
Robert Lin | 286e2eb9 | 2022-05-10 23:41:53 | [diff] [blame] | 9586 | /*initiator_frame_token=*/absl::nullopt, |
| 9587 | /*initiator_frame_tree_node_id=*/RenderFrameHost::kNoFrameTreeNodeId, |
| 9588 | ukm::kInvalidSourceId, page_transition, url_match_predicate); |
Sreeja Kamishetty | ac12140e | 2022-07-14 22:16:51 | [diff] [blame] | 9589 | int frame_tree_node_id = GetPrerenderHostRegistry()->CreateAndStartHost( |
Hiroki Nakagawa | a000bb8 | 2022-11-29 06:01:02 | [diff] [blame] | 9590 | attributes, preloading_attempt); |
Robert Lin | c37fb58 | 2021-11-11 03:18:47 | [diff] [blame] | 9591 | |
| 9592 | if (frame_tree_node_id != FrameTreeNode::kFrameTreeNodeInvalidId) { |
| 9593 | return std::make_unique<PrerenderHandleImpl>( |
Robert Lin | 624a0fe | 2021-12-08 22:58:30 | [diff] [blame] | 9594 | GetPrerenderHostRegistry()->GetWeakPtr(), frame_tree_node_id, |
| 9595 | prerendering_url); |
Robert Lin | c37fb58 | 2021-11-11 03:18:47 | [diff] [blame] | 9596 | } |
| 9597 | return nullptr; |
| 9598 | } |
| 9599 | |
Anthony Vallee-Dubois | aa02652 | 2022-12-07 17:24:28 | [diff] [blame] | 9600 | void WebContentsImpl::AboutToBeDiscarded(WebContents* new_contents) { |
| 9601 | observers_.NotifyObservers(&WebContentsObserver::AboutToBeDiscarded, |
| 9602 | new_contents); |
| 9603 | } |
| 9604 | |
Yoshiki Tanioka | 49b4cfb | 2022-10-20 09:25:31 | [diff] [blame] | 9605 | bool WebContentsImpl::CancelPrerendering(FrameTreeNode* frame_tree_node, |
| 9606 | PrerenderFinalStatus final_status) { |
Kevin McNee | ab98c4a5 | 2022-01-28 16:53:06 | [diff] [blame] | 9607 | if (!frame_tree_node) |
| 9608 | return false; |
| 9609 | |
| 9610 | DCHECK_EQ(this, FromFrameTreeNode(frame_tree_node)); |
| 9611 | |
| 9612 | // A prerendered page is identified by its root FrameTreeNode id, so if the |
| 9613 | // given `frame_tree_node` is in any way embedded, we need to iterate up to |
| 9614 | // the prerender root. |
| 9615 | if (frame_tree_node->GetParentOrOuterDocumentOrEmbedder()) { |
| 9616 | return frame_tree_node->GetParentOrOuterDocumentOrEmbedder() |
Lingqi Chi | 8e192ba | 2022-11-22 04:34:25 | [diff] [blame] | 9617 | ->CancelPrerendering(PrerenderCancellationReason(final_status)); |
Kevin McNee | ab98c4a5 | 2022-01-28 16:53:06 | [diff] [blame] | 9618 | } |
| 9619 | return GetPrerenderHostRegistry()->CancelHost( |
| 9620 | frame_tree_node->frame_tree_node_id(), final_status); |
| 9621 | } |
| 9622 | |
David Bokan | d6e44055b | 2022-09-21 03:58:08 | [diff] [blame] | 9623 | ui::mojom::VirtualKeyboardMode WebContentsImpl::GetVirtualKeyboardMode() const { |
| 9624 | return primary_frame_tree_.root() |
| 9625 | ->current_frame_host() |
| 9626 | ->GetPage() |
| 9627 | .virtual_keyboard_mode(); |
| 9628 | } |
| 9629 | |
Peter Kvitek | 965f3cd | 2022-01-06 22:33:41 | [diff] [blame] | 9630 | // static |
| 9631 | std::pair<int, int> WebContentsImpl::GetAvailablePointerAndHoverTypes() { |
| 9632 | // On Windows we have to temporarily allow blocking calls since |
| 9633 | // ui::GetAvailablePointerAndHoverTypes needs to call some in order to |
| 9634 | // figure out tablet device details in base::win::IsDeviceUsedAsATablet, |
| 9635 | // see https://crbug.com/1262162. |
Xiaohan Wang | 24ec934 | 2022-01-15 17:34:22 | [diff] [blame] | 9636 | #if BUILDFLAG(IS_WIN) |
Peter Kvitek | 965f3cd | 2022-01-06 22:33:41 | [diff] [blame] | 9637 | base::ScopedAllowBlocking scoped_allow_blocking; |
| 9638 | #endif |
| 9639 | return ui::GetAvailablePointerAndHoverTypes(); |
| 9640 | } |
| 9641 | |
[email protected] | 8ff00d7 | 2012-10-23 19:12:21 | [diff] [blame] | 9642 | } // namespace content |