blob: 43e0ea45292be24b64bf0938d0af91e0466f3920 [file] [log] [blame]
[email protected]91854cd2012-01-10 19:43:571// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]93ddb3c2012-04-11 21:44:295#include "content/browser/web_contents/web_contents_impl.h"
[email protected]39526562011-02-05 03:41:516
avib7348942015-12-25 20:57:107#include <stddef.h>
8
w.shackleton49bcd392016-01-06 17:38:229#include <cmath>
[email protected]2bb171882012-03-07 02:09:4610#include <utility>
rkaplowe56a52032017-02-07 00:14:5411#include <vector>
[email protected]b75b8292010-10-01 07:28:2512
Sebastien Marchandf8cbfab2019-01-25 16:02:3013#include "base/bind.h"
[email protected]36fb2c7c2011-04-04 15:49:0814#include "base/command_line.h"
creisfafed882016-07-29 00:03:0915#include "base/debug/dump_without_crashing.h"
zqzhang181047e62016-07-01 13:37:1716#include "base/feature_list.h"
Marijn Kruisselbrink46fa4bd2019-07-11 18:15:4717#include "base/files/file_path.h"
[email protected]cbb1ef592013-06-05 19:49:4618#include "base/lazy_instance.h"
skyostil95082a62015-06-05 19:53:0719#include "base/location.h"
[email protected]6d636e62013-07-31 03:15:5620#include "base/logging.h"
avib7348942015-12-25 20:57:1021#include "base/macros.h"
fdorayba121422016-12-23 19:51:4822#include "base/memory/ptr_util.h"
dmazzonid95ae842016-04-12 21:17:3923#include "base/memory/ref_counted.h"
asvitkine8d51e9d2016-09-02 23:55:4324#include "base/metrics/histogram_macros.h"
bratell0a7406f2017-03-28 07:46:3725#include "base/metrics/user_metrics.h"
Avi Drissman4891b852018-09-25 18:12:5826#include "base/no_destructor.h"
Carlos Caballero1215f882019-10-29 15:58:4327#include "base/optional.h"
[email protected]04cbd3d2013-12-04 04:58:2028#include "base/process/process.h"
skyostil95082a62015-06-05 19:53:0729#include "base/single_thread_task_runner.h"
Arthur Sonzogni929d29f2018-09-17 14:19:4430#include "base/stl_util.h"
[email protected]348fbaac2013-06-11 06:31:5131#include "base/strings/string16.h"
32#include "base/strings/string_number_conversions.h"
Avi Drissman4891b852018-09-25 18:12:5833#include "base/strings/string_split.h"
[email protected]348fbaac2013-06-11 06:31:5134#include "base/strings/string_util.h"
[email protected]74ebfb12013-06-07 20:48:0035#include "base/strings/utf_string_conversions.h"
gab30f26df2016-05-11 19:37:5536#include "base/threading/thread_task_runner_handle.h"
[email protected]a43858f2013-06-28 15:18:3737#include "base/time/time.h"
ssid3e765612015-01-28 04:03:4238#include "base/trace_event/trace_event.h"
avib7348942015-12-25 20:57:1039#include "build/build_config.h"
Min Qinda0ed2062018-02-23 22:00:5340#include "components/download/public/common/download_stats.h"
nzolghadrd87a308d2016-12-07 15:45:5641#include "components/rappor/public/rappor_utils.h"
rsleevi24f64dc22015-08-07 21:39:2142#include "components/url_formatter/url_formatter.h"
Abigail Kleinaa898742019-11-01 02:31:2543#include "content/browser/accessibility/accessibility_event_recorder.h"
James Wallace-Leeeafc94cb92018-07-23 21:35:0944#include "content/browser/accessibility/accessibility_tree_formatter_blink.h"
jamescookda2505812015-03-20 18:01:1845#include "content/browser/bad_message.h"
Max Morinddebb972018-09-20 10:04:1446#include "content/browser/browser_main_loop.h"
[email protected]7a846df2012-09-20 19:17:3947#include "content/browser/browser_plugin/browser_plugin_embedder.h"
48#include "content/browser/browser_plugin/browser_plugin_guest.h"
[email protected]b9535422012-02-09 01:47:5949#include "content/browser/child_process_security_policy_impl.h"
John Delaney732721e92020-02-07 23:11:2750#include "content/browser/conversions/conversion_host.h"
Pavel Feldman3c1842b2017-08-02 05:00:1651#include "content/browser/devtools/protocol/page_handler.h"
dgozmanf00c4f72016-10-19 17:45:1552#include "content/browser/devtools/render_frame_devtools_agent_host.h"
Becca Hughes3b5a43482018-07-17 22:31:5553#include "content/browser/display_cutout/display_cutout_host_impl.h"
[email protected]5f2aa722013-08-07 16:59:4154#include "content/browser/dom_storage/dom_storage_context_wrapper.h"
[email protected]1ea3c792012-04-17 01:25:0455#include "content/browser/dom_storage/session_storage_namespace_impl.h"
[email protected]aa4f3972012-03-01 18:12:1256#include "content/browser/download/mhtml_generation_manager.h"
[email protected]a53209b2012-01-20 16:48:1657#include "content/browser/download/save_package.h"
paulmeyerc0b762b2016-04-13 11:55:1758#include "content/browser/find_request_manager.h"
[email protected]bffc8302014-01-23 20:52:1659#include "content/browser/frame_host/cross_process_frame_connector.h"
ananta4b7467a52016-09-23 01:42:3860#include "content/browser/frame_host/frame_tree_node.h"
[email protected]d4a8ca482013-10-30 21:06:4061#include "content/browser/frame_host/navigation_entry_impl.h"
Arthur Hemeryc3380172018-01-22 14:00:1762#include "content/browser/frame_host/navigation_request.h"
Hiroki Nakagawa080f86c2020-05-13 10:56:4263#include "content/browser/frame_host/navigator.h"
[email protected]52913802013-12-10 05:52:1864#include "content/browser/frame_host/render_frame_host_impl.h"
lfgdb5c4ed2016-03-04 23:09:0765#include "content/browser/frame_host/render_frame_proxy_host.h"
Kenneth Russell954ed9ce2018-04-12 23:07:0166#include "content/browser/gpu/gpu_data_manager_impl.h"
mlamouriefdca9d2014-09-16 16:55:4067#include "content/browser/manifest/manifest_manager_host.h"
Max Morin5bc74f52018-05-09 07:00:2168#include "content/browser/media/audio_stream_broker.h"
dalecurtisbc6572e12014-09-12 19:22:3069#include "content/browser/media/audio_stream_monitor.h"
miu50f97892014-09-22 22:49:5270#include "content/browser/media/capture/web_contents_audio_muter.h"
dalecurtis88c240072015-12-09 02:11:1871#include "content/browser/media/media_web_contents_observer.h"
zqzhang1adf3cb2016-11-01 11:26:4572#include "content/browser/media/session/media_session_impl.h"
Yuzu Saijoa11069a2020-04-22 10:56:5573#include "content/browser/plugin_content_origin_allowlist.h"
David Black9cca3592019-11-06 23:02:2274#include "content/browser/renderer_host/frame_token_message_queue.h"
[email protected]f3b1a082011-11-18 00:34:3075#include "content/browser/renderer_host/render_process_host_impl.h"
[email protected]21161032014-05-05 16:56:5076#include "content/browser/renderer_host/render_view_host_delegate_view.h"
[email protected]b3c41c0b2012-03-06 15:48:3277#include "content/browser/renderer_host/render_view_host_impl.h"
[email protected]bafe6cd2012-05-23 23:09:5078#include "content/browser/renderer_host/render_widget_host_impl.h"
kenrb2a565f82015-09-02 20:24:5979#include "content/browser/renderer_host/render_widget_host_input_event_router.h"
[email protected]cfd80b02014-05-01 17:46:4880#include "content/browser/renderer_host/render_widget_host_view_base.h"
Ken Buchanandaef006b2017-08-17 18:32:1581#include "content/browser/renderer_host/render_widget_host_view_child_frame.h"
ekaramadadd882292016-06-08 15:22:5682#include "content/browser/renderer_host/text_input_manager.h"
Mike Wassermanb213b9352020-04-16 00:32:3483#include "content/browser/screen_enumeration/screen_change_monitor.h"
leon.han552e9de2017-02-09 14:37:3084#include "content/browser/screen_orientation/screen_orientation_provider.h"
[email protected]b6583592012-01-25 19:52:3385#include "content/browser/site_instance_impl.h"
Charlie Reis96077362019-07-04 00:34:0486#include "content/browser/web_contents/javascript_dialog_navigation_deferrer.h"
lfgc740d4b22016-04-15 16:45:3387#include "content/browser/web_contents/web_contents_view_child_frame.h"
Tsuyoshi Horob0d8d902020-03-12 00:09:3988#include "content/browser/web_package/save_as_web_bundle_job.h"
[email protected]86a0a6e2013-01-28 06:33:0389#include "content/browser/webui/web_ui_controller_factory_registry.h"
[email protected]d2353452012-01-19 19:53:5690#include "content/browser/webui/web_ui_impl.h"
[email protected]8eb04562013-03-06 03:41:1491#include "content/common/browser_plugin/browser_plugin_constants.h"
Paul Meyer0c58c3712016-11-17 22:59:5192#include "content/common/drag_messages.h"
[email protected]f114fa42013-12-06 17:06:4493#include "content/common/frame_messages.h"
[email protected]1f3fc1d2014-04-03 14:50:1794#include "content/common/input_messages.h"
lfgdb5c4ed2016-03-04 23:09:0795#include "content/common/page_messages.h"
nasko34634672016-07-29 18:46:0696#include "content/common/page_state_serialization.h"
rockot5c478a72016-09-28 23:14:1897#include "content/common/render_message_filter.mojom.h"
[email protected]2c5569662011-03-22 20:45:0298#include "content/common/view_messages.h"
Albert J. Wong3c93c182018-09-27 17:29:4399#include "content/common/widget_messages.h"
Abigail Kleinabb428332019-09-13 18:26:21100#include "content/public/browser/accessibility_tree_formatter.h"
[email protected]31a71eaf2014-03-13 01:47:36101#include "content/public/browser/ax_event_notification_details.h"
[email protected]ccb797302011-12-15 16:55:11102#include "content/public/browser/browser_context.h"
[email protected]139355f2014-05-11 14:21:28103#include "content/public/browser/browser_plugin_guest_manager.h"
Eric Seckler8652dcd52018-09-20 10:42:28104#include "content/public/browser/browser_task_traits.h"
Gabriel Charette790754c2018-03-16 21:32:59105#include "content/public/browser/browser_thread.h"
[email protected]87f3c082011-10-19 18:07:44106#include "content/public/browser/content_browser_client.h"
Lukasz Anforowicz8c4446dc2020-03-11 22:16:42107#include "content/public/browser/context_menu_params.h"
Ken Rockotce010f02019-12-12 23:32:32108#include "content/public/browser/device_service.h"
[email protected]e582fdd2011-12-20 16:48:17109#include "content/public/browser/download_manager.h"
Kent Tamura512a27e2018-10-04 00:49:32110#include "content/public/browser/file_select_listener.h"
ekaramada110f642016-12-21 19:47:28111#include "content/public/browser/focused_node_details.h"
Danyao Wanga78f3dd22020-03-05 05:31:27112#include "content/public/browser/global_routing_id.h"
[email protected]d9083482012-01-06 00:38:46113#include "content/public/browser/invalidate_type.h"
[email protected]71a88bb2013-02-01 22:05:15114#include "content/public/browser/javascript_dialog_manager.h"
skyf65d9bb2017-03-24 02:26:39115#include "content/public/browser/keyboard_event_processing_result.h"
[email protected]09d31d52012-03-11 22:30:27116#include "content/public/browser/load_notification_details.h"
[email protected]5b96836f2011-12-22 07:39:00117#include "content/public/browser/navigation_details.h"
[email protected]375fa1b2012-05-22 22:05:37118#include "content/public/browser/notification_details.h"
[email protected]be2510c02012-05-28 14:52:14119#include "content/public/browser/notification_service.h"
scottmg011d96e32016-06-29 19:15:08120#include "content/public/browser/notification_types.h"
[email protected]95640212014-07-26 18:14:30121#include "content/public/browser/render_widget_host_iterator.h"
Lukasz Anforowicze1b954d92017-10-30 21:28:06122#include "content/public/browser/restore_type.h"
estark5ea80e52015-06-19 18:43:12123#include "content/public/browser/security_style_explanations.h"
falken52a56e32016-12-08 05:02:40124#include "content/public/browser/ssl_status.h"
[email protected]4c3a23582012-08-18 08:54:34125#include "content/public/browser/storage_partition.h"
[email protected]674bc592011-12-20 23:00:42126#include "content/public/browser/web_contents_delegate.h"
Julie Jeongeun Kim7d0986172019-12-04 03:17:25127#include "content/public/browser/web_contents_receiver_set.h"
Lei Zhangee3b63ad2018-06-06 23:25:31128#include "content/public/browser/web_ui_controller.h"
[email protected]e091df82011-10-11 18:13:21129#include "content/public/common/bindings_policy.h"
alexmosc9e76ec2016-05-16 22:59:37130#include "content/public/common/child_process_host.h"
Hans Wennborg5ffd1392019-10-16 11:00:02131#include "content/public/common/content_client.h"
[email protected]54087fe2011-10-28 22:02:48132#include "content/public/common/content_constants.h"
Francois Doray3f2afbd2018-04-06 19:18:18133#include "content/public/common/content_features.h"
[email protected]64d69de42012-02-06 00:19:54134#include "content/public/common/content_switches.h"
Lukasz Anforowicze1b954d92017-10-30 21:28:06135#include "content/public/common/page_state.h"
Leon Han963dc182018-11-06 05:41:48136#include "content/public/common/referrer_type_converters.h"
[email protected]04cbd3d2013-12-04 04:58:20137#include "content/public/common/result_codes.h"
[email protected]f16cc6782013-12-16 23:42:57138#include "content/public/common/url_utils.h"
danakjffc8baf2019-10-10 23:49:42139#include "content/public/common/use_zoom_for_dsf_policy.h"
[email protected]b3a97b52014-07-09 06:25:05140#include "content/public/common/web_preferences.h"
Max Morinddebb972018-09-20 10:04:14141#include "media/base/user_input_monitor.h"
wjmaclean64951902016-04-29 20:59:12142#include "net/base/url_util.h"
[email protected]8bfc8272013-09-09 20:10:53143#include "net/http/http_cache.h"
144#include "net/http/http_transaction_factory.h"
rhalavati0fbaeef32017-06-01 15:56:30145#include "net/traffic_annotation/network_traffic_annotation.h"
[email protected]8bfc8272013-09-09 20:10:53146#include "net/url_request/url_request_context.h"
[email protected]abe2c032011-03-31 18:49:34147#include "net/url_request/url_request_context_getter.h"
Scott Violet02e38b92018-03-27 23:42:14148#include "ppapi/buildflags/buildflags.h"
Mounir Lamourifd9dcefa2017-07-06 10:26:55149#include "services/metrics/public/cpp/ukm_recorder.h"
John Abd-El-Malekb175a612018-12-03 20:11:58150#include "services/network/public/cpp/features.h"
arthursonzognib93a4472020-04-10 07:38:00151#include "services/network/public/cpp/web_sandbox_flags.h"
152#include "services/network/public/mojom/web_sandbox_flags.mojom.h"
rockot734fb662016-10-15 16:41:30153#include "services/service_manager/public/cpp/interface_provider.h"
Becca Hughes6daf5662018-06-27 16:50:54154#include "third_party/blink/public/common/associated_interfaces/associated_interface_provider.h"
Blink Reformata30d4232018-04-07 15:31:06155#include "third_party/blink/public/common/mime_util/mime_util.h"
danakj938b37a62019-09-24 18:35:54156#include "third_party/blink/public/common/page/page_zoom.h"
Sergio Villar Senin9d9d4f52019-10-15 10:57:57157#include "third_party/blink/public/common/security/security_style.h"
Abhijeet Kandalkarde7348e2020-01-13 06:06:54158#include "third_party/blink/public/mojom/frame/frame.mojom.h"
Dave Tapuskaa4189512019-10-15 20:27:34159#include "third_party/blink/public/mojom/frame/fullscreen.mojom.h"
Takuto Ikutaaa3b796c2019-02-06 02:54:56160#include "third_party/blink/public/mojom/loader/pause_subresource_loading_handle.mojom.h"
Antonio Gomes0d42960a2019-06-05 12:35:51161#include "third_party/blink/public/mojom/mediastream/media_stream.mojom-shared.h"
halton.huoca2eabd2015-07-06 08:17:40162#include "third_party/skia/include/core/SkBitmap.h"
dmazzonid95ae842016-04-12 21:17:39163#include "ui/accessibility/ax_tree_combiner.h"
[email protected]a08412b62012-05-29 21:28:56164#include "ui/base/layout.h"
Daniel Cheng90196c82018-04-25 21:49:14165#include "ui/events/base_event_utils.h"
chongz4a9751942016-08-17 17:49:39166#include "ui/events/blink/web_input_event_traits.h"
[email protected]66bd5512012-08-01 02:02:52167#include "ui/gl/gl_switches.h"
[email protected]3c733bde2010-12-21 19:56:31168
ekaramada110f642016-12-21 19:47:28169#if defined(OS_WIN)
170#include "content/browser/renderer_host/dip_util.h"
171#include "ui/gfx/geometry/dip_util.h"
172#endif
173
[email protected]583418cc2013-01-17 14:01:10174#if defined(OS_ANDROID)
mfomitchev2b8b066a2016-01-28 19:23:15175#include "content/browser/android/date_time_chooser_android.h"
sammcf5f1b0f2016-09-20 23:05:11176#include "content/browser/android/java_interfaces_impl.h"
Leon Hanccb985f2019-08-26 02:43:24177#include "content/browser/android/nfc_host.h"
[email protected]155c7f22013-12-09 17:07:18178#include "content/browser/web_contents/web_contents_android.h"
Ke He31d0bb02018-02-24 07:16:24179#include "services/device/public/mojom/nfc.mojom.h"
mcnee336ea2c2017-05-23 22:50:59180#else // !OS_ANDROID
181#include "content/browser/host_zoom_map_impl.h"
mfomitchev2b8b066a2016-01-28 19:23:15182#endif // OS_ANDROID
[email protected]583418cc2013-01-17 14:01:10183
brettw4b461082016-11-19 18:55:16184#if BUILDFLAG(ENABLE_PLUGINS)
zqzhang181047e62016-07-01 13:37:17185#include "content/browser/media/session/pepper_playback_observer.h"
Lei Zhangebcc6302018-01-12 19:46:45186#endif
zqzhang181047e62016-07-01 13:37:17187
[email protected]8ff00d72012-10-23 19:12:21188namespace content {
Abigail Kleinaa898742019-11-01 02:31:25189
190using AccessibilityEventCallback =
191 base::RepeatingCallback<void(const std::string&)>;
192
[email protected]420ae012009-04-24 05:16:32193namespace {
194
[email protected]960b0372014-05-19 18:01:00195const int kMinimumDelayBetweenLoadingUpdatesMS = 100;
[email protected]82114f52012-03-20 22:53:41196const char kDotGoogleDotCom[] = ".google.com";
[email protected]ca406032011-07-19 21:53:05197
Sreeja Kamishettybe0ccd6b2020-06-09 11:52:50198using LifecycleState = RenderFrameHostImpl::LifecycleState;
199
Alexander Timin33bd754c02020-03-10 22:50:18200// TODO(crbug.com/1059903): Clean up after the initial investigation.
201constexpr base::Feature kCheckWebContentsAccessFromNonCurrentFrame{
202 "CheckWebContentsAccessFromNonCurrentFrame",
203 base::FEATURE_DISABLED_BY_DEFAULT};
204
Avi Drissmana9114822018-03-23 18:25:55205base::LazyInstance<std::vector<
206 WebContentsImpl::FriendWrapper::CreatedCallback>>::DestructorAtExit
207 g_created_callbacks = LAZY_INSTANCE_INITIALIZER;
[email protected]cbb1ef592013-06-05 19:49:46208
dcheng57e39e22016-01-21 00:25:38209bool HasMatchingProcess(FrameTree* tree, int render_process_id) {
210 for (FrameTreeNode* node : tree->Nodes()) {
211 if (node->current_frame_host()->GetProcess()->GetID() == render_process_id)
212 return true;
alexmos4cf2aa32015-07-15 23:40:43213 }
dcheng57e39e22016-01-21 00:25:38214 return false;
[email protected]95640212014-07-26 18:14:30215}
216
kenrb19221852016-04-29 17:21:40217bool HasMatchingWidgetHost(FrameTree* tree, RenderWidgetHost* host) {
218 // This method scans the frame tree rather than checking whether
219 // host->delegate() == this, which allows it to return false when the host
220 // for a frame that is pending or pending deletion.
221 if (!host)
222 return false;
223
224 for (FrameTreeNode* node : tree->Nodes()) {
225 if (node->current_frame_host()->GetRenderWidgetHost() == host)
226 return true;
227 }
228 return false;
229}
230
dmazzonif8a31042016-12-03 00:52:13231void UpdateAccessibilityModeOnFrame(RenderFrameHost* frame_host) {
232 static_cast<RenderFrameHostImpl*>(frame_host)->UpdateAccessibilityMode();
[email protected]95640212014-07-26 18:14:30233}
234
dtseng0dd3fa12015-07-22 19:00:52235void ResetAccessibility(RenderFrameHost* rfh) {
236 static_cast<RenderFrameHostImpl*>(rfh)->AccessibilityReset();
237}
238
Ian Clelland5cbaaf82017-11-27 22:00:03239RenderFrameHostImpl* FindOpenerRFH(const WebContents::CreateParams& params) {
240 RenderFrameHostImpl* opener_rfh = nullptr;
alexmose201c7cd2015-06-10 17:14:21241 if (params.opener_render_frame_id != MSG_ROUTING_NONE) {
Ian Clelland5cbaaf82017-11-27 22:00:03242 opener_rfh = RenderFrameHostImpl::FromID(params.opener_render_process_id,
243 params.opener_render_frame_id);
alexmose201c7cd2015-06-10 17:14:21244 }
Ian Clelland5cbaaf82017-11-27 22:00:03245 return opener_rfh;
lukasza6f8ac622017-06-06 03:10:20246}
247
Dominick Ng37a15052018-07-17 23:56:35248// Returns |true| if |type| is the kind of user input that should trigger the
249// user interaction observers.
250bool IsUserInteractionInputType(blink::WebInputEvent::Type type) {
251 // Ideally, this list would be based more off of
252 // https://whatwg.org/C/interaction.html#triggered-by-user-activation.
Dave Tapuska347d60a2020-04-21 23:55:47253 return type == blink::WebInputEvent::Type::kMouseDown ||
254 type == blink::WebInputEvent::Type::kGestureScrollBegin ||
255 type == blink::WebInputEvent::Type::kTouchStart ||
256 type == blink::WebInputEvent::Type::kRawKeyDown;
Dominick Ng37a15052018-07-17 23:56:35257}
258
Pavel Feldman3c1842b2017-08-02 05:00:16259// Ensures that OnDialogClosed is only called once.
260class CloseDialogCallbackWrapper
261 : public base::RefCountedThreadSafe<CloseDialogCallbackWrapper> {
262 public:
Avi Drissmane04d3992017-10-05 15:11:36263 using CloseCallback =
264 base::OnceCallback<void(bool, bool, const base::string16&)>;
265
266 explicit CloseDialogCallbackWrapper(CloseCallback callback)
267 : callback_(std::move(callback)) {}
Pavel Feldman3c1842b2017-08-02 05:00:16268
269 void Run(bool dialog_was_suppressed,
270 bool success,
271 const base::string16& user_input) {
Avi Drissmane04d3992017-10-05 15:11:36272 if (callback_.is_null())
Pavel Feldman3c1842b2017-08-02 05:00:16273 return;
Avi Drissmane04d3992017-10-05 15:11:36274 std::move(callback_).Run(dialog_was_suppressed, success, user_input);
Pavel Feldman3c1842b2017-08-02 05:00:16275 }
276
277 private:
278 friend class base::RefCountedThreadSafe<CloseDialogCallbackWrapper>;
279 ~CloseDialogCallbackWrapper() {}
Avi Drissmane04d3992017-10-05 15:11:36280
281 CloseCallback callback_;
Pavel Feldman3c1842b2017-08-02 05:00:16282};
283
Arthur Sonzogni929d29f2018-09-17 14:19:44284bool FrameCompareDepth(RenderFrameHostImpl* a, RenderFrameHostImpl* b) {
285 return a->frame_tree_node()->depth() < b->frame_tree_node()->depth();
286}
287
Raymes Khoury58373df2019-08-06 06:29:20288bool AreValidRegisterProtocolHandlerArguments(const std::string& protocol,
289 const GURL& url,
290 const url::Origin& origin) {
291 ChildProcessSecurityPolicyImpl* policy =
292 ChildProcessSecurityPolicyImpl::GetInstance();
293 if (policy->IsPseudoScheme(protocol))
294 return false;
295
296 url::Origin url_origin = url::Origin::Create(url);
297 if (url_origin.opaque())
298 return false;
299
300 if (!url_origin.IsSameOriginWith(origin))
301 return false;
302
303 return true;
304}
305
yilkal796e89e2019-10-02 23:58:28306void RecordMaxFrameCountUMA(size_t max_frame_count) {
307 UMA_HISTOGRAM_COUNTS_10000("Navigation.MainFrame.MaxFrameCount",
308 max_frame_count);
309}
310
Jeremy Roman03ce13ce2020-05-29 22:16:57311// Returns whether the condition provided applies to any inner contents.
312// This check is not recursive (however, the predicate provided may itself
313// recurse each contents' own inner contents).
314//
315// For example, if this is used to aggregate state from inner contents to outer
316// contents, then that propagation will gather transitive descendants without
317// need for this helper to do so. In fact, in such cases recursing on inner
318// contents here would make that operation quadratic rather than linear.
319template <typename Functor>
320bool AnyInnerWebContents(WebContents* web_contents, const Functor& f) {
321 const auto& inner_contents = web_contents->GetInnerWebContents();
322 return std::any_of(inner_contents.begin(), inner_contents.end(), f);
323}
324
Xuehui Xieb2300ae42020-06-09 20:25:05325std::vector<RenderFrameHost*> GetAllFramesImpl(FrameTree& frame_tree,
326 bool include_pending) {
327 std::vector<RenderFrameHost*> frame_hosts;
328 for (FrameTreeNode* node : frame_tree.Nodes()) {
329 frame_hosts.push_back(node->current_frame_host());
330 if (include_pending) {
331 RenderFrameHostImpl* pending_frame_host =
332 node->render_manager()->speculative_frame_host();
333 if (pending_frame_host)
334 frame_hosts.push_back(pending_frame_host);
335 }
336 }
337 return frame_hosts;
338}
339
340int SendToAllFramesImpl(FrameTree& frame_tree,
341 bool include_pending,
342 IPC::Message* message) {
343 int number_of_messages = 0;
344 std::vector<RenderFrameHost*> frame_hosts =
345 GetAllFramesImpl(frame_tree, include_pending);
346 for (RenderFrameHost* rfh : frame_hosts) {
347 if (!rfh->IsRenderFrameLive())
348 continue;
349
350 ++number_of_messages;
351 IPC::Message* message_copy = new IPC::Message(*message);
352 message_copy->set_routing_id(rfh->GetRoutingID());
353 rfh->Send(message_copy);
354 }
355 delete message;
356 return number_of_messages;
357}
358
lukasza6f8ac622017-06-06 03:10:20359} // namespace
360
Joel Hockey8c2011b22020-04-30 05:07:19361CreatedWindow::CreatedWindow() = default;
362CreatedWindow::CreatedWindow(std::unique_ptr<WebContentsImpl> contents,
363 GURL target_url)
364 : contents(std::move(contents)), target_url(std::move(target_url)) {}
365CreatedWindow::~CreatedWindow() = default;
366CreatedWindow::CreatedWindow(CreatedWindow&&) = default;
367CreatedWindow& CreatedWindow::operator=(CreatedWindow&&) = default;
368
Erik Chenbb8e738e2018-04-28 14:10:43369std::unique_ptr<WebContents> WebContents::Create(
370 const WebContents::CreateParams& params) {
Albert J. Wong65fe64d2019-09-20 02:48:14371 return WebContentsImpl::Create(params);
372}
373
374std::unique_ptr<WebContentsImpl> WebContentsImpl::Create(
375 const CreateParams& params) {
376 return CreateWithOpener(params, FindOpenerRFH(params));
[email protected]d1198fd2012-08-13 22:50:19377}
378
erikchenade1fef2018-05-02 22:07:57379std::unique_ptr<WebContents> WebContents::CreateWithSessionStorage(
[email protected]54944cde2012-12-09 09:24:59380 const WebContents::CreateParams& params,
[email protected]fdac6ade2013-07-20 01:06:30381 const SessionStorageNamespaceMap& session_storage_namespace_map) {
erikchenade1fef2018-05-02 22:07:57382 std::unique_ptr<WebContentsImpl> new_contents(
383 new WebContentsImpl(params.browser_context));
Ian Clelland5cbaaf82017-11-27 22:00:03384 RenderFrameHostImpl* opener_rfh = FindOpenerRFH(params);
385 FrameTreeNode* opener = nullptr;
386 if (opener_rfh)
387 opener = opener_rfh->frame_tree_node();
388 new_contents->SetOpenerForNewContents(opener, params.opener_suppressed);
[email protected]fdac6ade2013-07-20 01:06:30389
jdoerrie55ec69d2018-10-08 13:34:46390 for (auto it = session_storage_namespace_map.begin();
391 it != session_storage_namespace_map.end(); ++it) {
Fergal Daly09d6c762020-05-29 02:05:18392 new_contents->GetController().SetSessionStorageNamespace(it->first,
393 it->second.get());
[email protected]fdac6ade2013-07-20 01:06:30394 }
395
Sean Toppingbbc4a2bd2019-01-30 02:16:14396 WebContentsImpl* outer_web_contents = nullptr;
Sam McNally69a33772018-06-27 16:54:12397 if (params.guest_delegate) {
398 // This makes |new_contents| act as a guest.
399 // For more info, see comment above class BrowserPluginGuest.
Albert J. Wongc55cc64d2018-10-12 02:50:04400 BrowserPluginGuest::CreateInWebContents(new_contents.get(),
401 params.guest_delegate);
Sean Toppingbbc4a2bd2019-01-30 02:16:14402 outer_web_contents = static_cast<WebContentsImpl*>(
403 params.guest_delegate->GetOwnerWebContents());
Sam McNally69a33772018-06-27 16:54:12404 }
405
[email protected]54944cde2012-12-09 09:24:59406 new_contents->Init(params);
Sean Toppingbbc4a2bd2019-01-30 02:16:14407 if (outer_web_contents)
408 outer_web_contents->InnerWebContentsCreated(new_contents.get());
[email protected]d1198fd2012-08-13 22:50:19409 return new_contents;
[email protected]a81343d232011-12-27 07:39:20410}
[email protected]746d3052012-05-22 15:15:47411
benwells5e7260d2017-03-20 23:36:33412void WebContentsImpl::FriendWrapper::AddCreatedCallbackForTesting(
nickf9acfbe2014-12-23 19:12:37413 const CreatedCallback& callback) {
[email protected]cbb1ef592013-06-05 19:49:46414 g_created_callbacks.Get().push_back(callback);
415}
416
benwells5e7260d2017-03-20 23:36:33417void WebContentsImpl::FriendWrapper::RemoveCreatedCallbackForTesting(
nickf9acfbe2014-12-23 19:12:37418 const CreatedCallback& callback) {
[email protected]cbb1ef592013-06-05 19:49:46419 for (size_t i = 0; i < g_created_callbacks.Get().size(); ++i) {
Robert Liao1db5a352019-06-05 20:57:34420 if (g_created_callbacks.Get().at(i) == callback) {
[email protected]cbb1ef592013-06-05 19:49:46421 g_created_callbacks.Get().erase(g_created_callbacks.Get().begin() + i);
422 return;
423 }
424 }
425}
426
avidf38c952015-10-27 13:45:13427WebContents* WebContents::FromRenderViewHost(RenderViewHost* rvh) {
428 if (!rvh)
429 return nullptr;
[email protected]746d3052012-05-22 15:15:47430 return rvh->GetDelegate()->GetAsWebContents();
431}
432
[email protected]a86c0e962013-12-17 17:10:39433WebContents* WebContents::FromRenderFrameHost(RenderFrameHost* rfh) {
avidf38c952015-10-27 13:45:13434 if (!rfh)
435 return nullptr;
Alexander Timin33bd754c02020-03-10 22:50:18436 if (!rfh->IsCurrent() && base::FeatureList::IsEnabled(
437 kCheckWebContentsAccessFromNonCurrentFrame)) {
438 // TODO(crbug.com/1059903): return nullptr here eventually.
439 base::debug::DumpWithoutCrashing();
440 }
avidf38c952015-10-27 13:45:13441 return static_cast<RenderFrameHostImpl*>(rfh)->delegate()->GetAsWebContents();
[email protected]a86c0e962013-12-17 17:10:39442}
443
ananta4b7467a52016-09-23 01:42:38444WebContents* WebContents::FromFrameTreeNodeId(int frame_tree_node_id) {
445 FrameTreeNode* frame_tree_node =
446 FrameTreeNode::GloballyFindByID(frame_tree_node_id);
anantab8cfa3b2016-10-14 22:43:19447 if (!frame_tree_node)
448 return nullptr;
jam8c4edd02017-05-06 18:50:33449 return WebContentsImpl::FromFrameTreeNode(frame_tree_node);
ananta4b7467a52016-09-23 01:42:38450}
451
leon.han552e9de2017-02-09 14:37:30452void WebContents::SetScreenOrientationDelegate(
453 ScreenOrientationDelegate* delegate) {
454 ScreenOrientationProvider::SetDelegate(delegate);
455}
456
[email protected]7fff43e2013-05-21 20:21:10457// WebContentsImpl::DestructionObserver ----------------------------------------
458
459class WebContentsImpl::DestructionObserver : public WebContentsObserver {
460 public:
461 DestructionObserver(WebContentsImpl* owner, WebContents* watched_contents)
Fergal Daly09d6c762020-05-29 02:05:18462 : WebContentsObserver(watched_contents), owner_(owner) {}
[email protected]7fff43e2013-05-21 20:21:10463
464 // WebContentsObserver:
dchengc2282aa2014-10-21 12:07:58465 void WebContentsDestroyed() override {
[email protected]12a46832014-05-09 13:35:58466 owner_->OnWebContentsDestroyed(
467 static_cast<WebContentsImpl*>(web_contents()));
[email protected]7fff43e2013-05-21 20:21:10468 }
469
470 private:
471 WebContentsImpl* owner_;
472
473 DISALLOW_COPY_AND_ASSIGN(DestructionObserver);
474};
475
Sreeja Kamishettybe0ccd6b2020-06-09 11:52:50476// TODO(sreejakshetty): Make |WebContentsImpl::ColorChooser| per-frame instead
477// of WebContents-owned.
Joel Hockey85b379d2017-12-11 10:42:13478// WebContentsImpl::ColorChooser ----------------------------------------------
Joel Hockey163835a2017-12-20 11:51:57479class WebContentsImpl::ColorChooser : public blink::mojom::ColorChooser {
Joel Hockey85b379d2017-12-11 10:42:13480 public:
481 ColorChooser(content::ColorChooser* chooser,
Julie Jeongeun Kim8e2879e2019-08-06 23:55:20482 mojo::PendingReceiver<blink::mojom::ColorChooser> receiver,
483 mojo::PendingRemote<blink::mojom::ColorChooserClient> client)
Joel Hockey85b379d2017-12-11 10:42:13484 : chooser_(chooser),
Julie Jeongeun Kim8e2879e2019-08-06 23:55:20485 receiver_(this, std::move(receiver)),
Joel Hockey85b379d2017-12-11 10:42:13486 client_(std::move(client)) {
Julie Jeongeun Kim8e2879e2019-08-06 23:55:20487 receiver_.set_disconnect_handler(
Joel Hockey85b379d2017-12-11 10:42:13488 base::BindOnce([](content::ColorChooser* chooser) { chooser->End(); },
489 base::Unretained(chooser)));
490 }
[email protected]8ed16472014-04-11 19:02:48491
Joel Hockey85b379d2017-12-11 10:42:13492 ~ColorChooser() override { chooser_->End(); }
[email protected]8ed16472014-04-11 19:02:48493
Joel Hockey85b379d2017-12-11 10:42:13494 void SetSelectedColor(SkColor color) override {
495 chooser_->SetSelectedColor(color);
496 }
497
498 void DidChooseColorInColorChooser(SkColor color) {
499 client_->DidChooseColor(color);
500 }
501
502 private:
503 // Color chooser that was opened by this tab.
504 std::unique_ptr<content::ColorChooser> chooser_;
505
Julie Jeongeun Kim8e2879e2019-08-06 23:55:20506 // mojo receiver.
507 mojo::Receiver<blink::mojom::ColorChooser> receiver_;
Joel Hockey85b379d2017-12-11 10:42:13508
509 // mojo renderer client.
Julie Jeongeun Kim8e2879e2019-08-06 23:55:20510 mojo::Remote<blink::mojom::ColorChooserClient> client_;
Joel Hockey85b379d2017-12-11 10:42:13511};
nicka0ac8382016-12-15 23:59:23512
lazyboy6ec48b2a2015-06-29 15:18:14513// WebContentsImpl::WebContentsTreeNode ----------------------------------------
lfg601233692017-03-06 22:45:44514WebContentsImpl::WebContentsTreeNode::WebContentsTreeNode(
515 WebContentsImpl* current_web_contents)
516 : current_web_contents_(current_web_contents),
517 outer_web_contents_(nullptr),
lazyboy6ec48b2a2015-06-29 15:18:14518 outer_contents_frame_tree_node_id_(
avallee0206f782016-07-28 18:55:33519 FrameTreeNode::kFrameTreeNodeInvalidId),
lfg601233692017-03-06 22:45:44520 focused_web_contents_(current_web_contents) {}
lazyboy6ec48b2a2015-06-29 15:18:14521
Lucas Furukawa Gadani3d38ec152018-10-26 20:55:18522WebContentsImpl::WebContentsTreeNode::~WebContentsTreeNode() {}
lazyboy6ec48b2a2015-06-29 15:18:14523
Lucas Furukawa Gadani99125822019-01-03 15:41:49524std::unique_ptr<WebContents>
525WebContentsImpl::WebContentsTreeNode::DisconnectFromOuterWebContents() {
526 std::unique_ptr<WebContents> inner_contents =
527 outer_web_contents_->node_.DetachInnerWebContents(current_web_contents_);
528 OuterContentsFrameTreeNode()->RemoveObserver(this);
529 outer_contents_frame_tree_node_id_ = FrameTreeNode::kFrameTreeNodeInvalidId;
530 outer_web_contents_ = nullptr;
531 return inner_contents;
532}
533
paulmeyerfeafc2d2017-04-25 21:46:40534void WebContentsImpl::WebContentsTreeNode::AttachInnerWebContents(
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:50535 std::unique_ptr<WebContents> inner_web_contents,
536 RenderFrameHostImpl* render_frame_host) {
537 WebContentsImpl* inner_web_contents_impl =
538 static_cast<WebContentsImpl*>(inner_web_contents.get());
539 WebContentsTreeNode& inner_web_contents_node = inner_web_contents_impl->node_;
540
541 inner_web_contents_node.focused_web_contents_ = nullptr;
542 inner_web_contents_node.outer_web_contents_ = current_web_contents_;
543 inner_web_contents_node.outer_contents_frame_tree_node_id_ =
544 render_frame_host->frame_tree_node()->frame_tree_node_id();
545
Lucas Furukawa Gadani3d38ec152018-10-26 20:55:18546 inner_web_contents_.push_back(std::move(inner_web_contents));
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:50547
548 render_frame_host->frame_tree_node()->AddObserver(&inner_web_contents_node);
Jeremy Roman03ce13ce2020-05-29 22:16:57549 current_web_contents_->InnerWebContentsAttached(inner_web_contents_impl);
paulmeyerfeafc2d2017-04-25 21:46:40550}
551
Lucas Furukawa Gadani3d38ec152018-10-26 20:55:18552std::unique_ptr<WebContents>
553WebContentsImpl::WebContentsTreeNode::DetachInnerWebContents(
paulmeyerfeafc2d2017-04-25 21:46:40554 WebContentsImpl* inner_web_contents) {
Lucas Furukawa Gadani3d38ec152018-10-26 20:55:18555 std::unique_ptr<WebContents> detached_contents;
556 for (std::unique_ptr<WebContents>& web_contents : inner_web_contents_) {
557 if (web_contents.get() == inner_web_contents) {
558 detached_contents = std::move(web_contents);
559 std::swap(web_contents, inner_web_contents_.back());
560 inner_web_contents_.pop_back();
Jeremy Roman03ce13ce2020-05-29 22:16:57561 current_web_contents_->InnerWebContentsDetached(inner_web_contents);
Lucas Furukawa Gadani3d38ec152018-10-26 20:55:18562 return detached_contents;
563 }
564 }
565
566 NOTREACHED();
567 return nullptr;
paulmeyerfeafc2d2017-04-25 21:46:40568}
569
lfga3e2bdc2017-03-07 20:44:01570FrameTreeNode*
571WebContentsImpl::WebContentsTreeNode::OuterContentsFrameTreeNode() const {
572 return FrameTreeNode::GloballyFindByID(outer_contents_frame_tree_node_id_);
lfg601233692017-03-06 22:45:44573}
574
lfga3e2bdc2017-03-07 20:44:01575void WebContentsImpl::WebContentsTreeNode::OnFrameTreeNodeDestroyed(
576 FrameTreeNode* node) {
577 DCHECK_EQ(outer_contents_frame_tree_node_id_, node->frame_tree_node_id())
578 << "WebContentsTreeNode should only receive notifications for the "
579 "FrameTreeNode in its outer WebContents that hosts it.";
Lucas Furukawa Gadani3d38ec152018-10-26 20:55:18580
581 // Deletes |this| too.
582 outer_web_contents_->node_.DetachInnerWebContents(current_web_contents_);
lazyboy6ec48b2a2015-06-29 15:18:14583}
584
avallee0206f782016-07-28 18:55:33585void WebContentsImpl::WebContentsTreeNode::SetFocusedWebContents(
586 WebContentsImpl* web_contents) {
587 DCHECK(!outer_web_contents())
588 << "Only the outermost WebContents tracks focus.";
589 focused_web_contents_ = web_contents;
590}
591
paulmeyerfeafc2d2017-04-25 21:46:40592WebContentsImpl*
593WebContentsImpl::WebContentsTreeNode::GetInnerWebContentsInFrame(
594 const FrameTreeNode* frame) {
595 auto ftn_id = frame->frame_tree_node_id();
Lucas Furukawa Gadani3d38ec152018-10-26 20:55:18596 for (auto& contents : inner_web_contents_) {
597 WebContentsImpl* impl = static_cast<WebContentsImpl*>(contents.get());
598 if (impl->node_.outer_contents_frame_tree_node_id() == ftn_id) {
599 return impl;
paulmeyerfeafc2d2017-04-25 21:46:40600 }
601 }
602 return nullptr;
603}
604
Lucas Furukawa Gadani3d38ec152018-10-26 20:55:18605std::vector<WebContentsImpl*>
606WebContentsImpl::WebContentsTreeNode::GetInnerWebContents() const {
607 std::vector<WebContentsImpl*> inner_web_contents;
608 for (auto& contents : inner_web_contents_)
609 inner_web_contents.push_back(static_cast<WebContentsImpl*>(contents.get()));
610
611 return inner_web_contents;
paulmeyerfeafc2d2017-04-25 21:46:40612}
613
[email protected]b172aee2012-04-10 17:05:26614// WebContentsImpl -------------------------------------------------------------
[email protected]420ae012009-04-24 05:16:32615
alexmose201c7cd2015-06-10 17:14:21616WebContentsImpl::WebContentsImpl(BrowserContext* browser_context)
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28617 : delegate_(nullptr),
[email protected]69e797f2013-04-30 01:10:22618 controller_(this, browser_context),
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28619 render_view_host_delegate_view_(nullptr),
alexmose201c7cd2015-06-10 17:14:21620 created_with_opener_(false),
Lucas Furukawa Gadani99125822019-01-03 15:41:49621 node_(this),
Fergal Daly09d6c762020-05-29 02:05:18622 frame_tree_(&controller_, this, this, this, this, this),
[email protected]6dfed692014-05-22 04:18:03623 is_load_to_different_document_(false),
[email protected]443b80e2010-12-14 00:42:23624 crashed_status_(base::TERMINATION_STATUS_STILL_RUNNING),
625 crashed_error_code_(0),
[email protected]d5f942ba2008-09-26 19:30:34626 waiting_for_response_(false),
[email protected]fcf75d42013-12-03 20:11:26627 load_state_(net::LOAD_STATE_IDLE, base::string16()),
[email protected]094e5b22009-09-25 04:23:56628 upload_size_(0),
629 upload_position_(0),
dfalcantarae6b7b46752015-07-10 18:14:16630 is_resume_pending_(false),
[email protected]b4c84012014-04-28 19:51:10631 has_accessed_initial_document_(false),
Collin Bakerc5259962019-11-14 17:51:58632 visible_capturer_count_(0),
633 hidden_capturer_count_(0),
[email protected]fdd61c62009-04-22 19:22:57634 is_being_destroyed_(false),
creis97867aa2017-02-17 18:39:45635 is_notifying_observers_(false),
[email protected]fdd61c62009-04-22 19:22:57636 notify_disconnection_(false),
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28637 dialog_manager_(nullptr),
[email protected]7ab1e7d62009-10-14 23:32:01638 is_showing_before_unload_dialog_(false),
gab0dccfef2015-05-20 18:43:39639 last_active_time_(base::TimeTicks::Now()),
[email protected]ebf40a72010-07-22 01:46:38640 closed_by_user_gesture_(false),
danakj938b37a62019-09-24 18:35:54641 minimum_zoom_percent_(
642 static_cast<int>(blink::kMinimumPageZoomFactor * 100)),
643 maximum_zoom_percent_(
644 static_cast<int>(blink::kMaximumPageZoomFactor * 100)),
w.shackleton49bcd392016-01-06 17:38:22645 zoom_scroll_remainder_(0),
alexmosc9e76ec2016-05-16 22:59:37646 fullscreen_widget_process_id_(ChildProcessHost::kInvalidUniqueID),
[email protected]5dcaf8e2013-12-28 01:31:42647 fullscreen_widget_routing_id_(MSG_ROUTING_NONE),
[email protected]0b431992014-06-24 00:08:03648 fullscreen_widget_had_focus_at_shutdown_(false),
[email protected]34ff1cfc2014-08-20 06:16:05649 force_disable_overscroll_content_(false),
[email protected]95640212014-07-26 18:14:30650 last_dialog_suppressed_(false),
waffles1c12bf402016-02-10 17:45:48651 accessibility_mode_(
Dominic Mazzoni21fb0282019-02-13 18:32:47652 GetContentClient()->browser()->GetAXModeForBrowserContext(
653 browser_context)),
avayvodcc85bbd2015-08-28 19:11:15654 audio_stream_monitor_(this),
Thomas Guilbert882c4cec2019-02-14 05:55:45655 media_web_contents_observer_(
656 std::make_unique<MediaWebContentsObserver>(this)),
mcnee336ea2c2017-05-23 22:50:59657#if !defined(OS_ANDROID)
mcnee432e47d2015-11-09 19:37:46658 page_scale_factor_is_one_(true),
mcnee336ea2c2017-05-23 22:50:59659#endif // !defined(OS_ANDROID)
qinmin72e8bd02016-10-21 19:35:37660 is_overlay_content_(false),
ekaramadf6750aa2017-06-06 18:29:42661 showing_context_menu_(false),
W. James MacLean62198672019-06-04 16:46:10662 text_autosizer_page_info_({0, 0, 1.f}),
shivanigithubcceeacf2020-03-06 20:00:27663 native_theme_observer_(this) {
danakjf4b9e942019-11-29 15:43:04664 frame_tree_.SetFrameRemoveListener(base::BindRepeating(
665 &WebContentsImpl::OnFrameRemoved, base::Unretained(this)));
brettw4b461082016-11-19 18:55:16666#if BUILDFLAG(ENABLE_PLUGINS)
zqzhang181047e62016-07-01 13:37:17667 pepper_playback_observer_.reset(new PepperPlaybackObserver(this));
668#endif
blundelle75a8f92017-03-27 08:11:17669
Becca Hughes86334622018-07-09 21:29:38670#if defined(OS_ANDROID)
Elly Fong-Jonescdd7ae82019-02-20 23:26:33671 display_cutout_host_impl_ = std::make_unique<DisplayCutoutHostImpl>(this);
Becca Hughes86334622018-07-09 21:29:38672#endif
John Delaney732721e92020-02-07 23:11:27673
Alison Maher7f366dc62020-03-03 19:46:00674 ui::NativeTheme* native_theme = ui::NativeTheme::GetInstanceForWeb();
675 native_theme_observer_.Add(native_theme);
676 using_dark_colors_ = native_theme->ShouldUseDarkColors();
677 preferred_color_scheme_ = native_theme->GetPreferredColorScheme();
678
Mike Wassermanb213b9352020-04-16 00:32:34679 screen_change_monitor_ =
680 std::make_unique<ScreenChangeMonitor>(base::BindRepeating(
681 &WebContentsImpl::OnScreensChange, base::Unretained(this)));
682
John Delaney732721e92020-02-07 23:11:27683 // ConversionHost takes a weak ref on |this|, so it must be created outside of
684 // the initializer list.
Charlie Harrison405f6212020-04-14 21:57:32685 if (base::FeatureList::IsEnabled(features::kConversionMeasurement)) {
686 conversion_host_ = std::make_unique<ConversionHost>(this);
687 }
[email protected]332af7732009-03-11 13:21:35688}
initial.commit09911bf2008-07-26 23:55:29689
[email protected]b172aee2012-04-10 17:05:26690WebContentsImpl::~WebContentsImpl() {
creisba91ba82017-05-12 22:56:19691 // Imperfect sanity check against double free, given some crashes unexpectedly
692 // observed in the wild.
693 CHECK(!is_being_destroyed_);
694
695 // We generally keep track of is_being_destroyed_ to let other features know
696 // to avoid certain actions during destruction.
[email protected]420ae012009-04-24 05:16:32697 is_being_destroyed_ = true;
698
creis97867aa2017-02-17 18:39:45699 // A WebContents should never be deleted while it is notifying observers,
creisba91ba82017-05-12 22:56:19700 // since this will lead to a use-after-free as it continues to notify later
creis97867aa2017-02-17 18:39:45701 // observers.
702 CHECK(!is_notifying_observers_);
703
kenrb2a565f82015-09-02 20:24:59704 rwh_input_event_router_.reset();
705
Julie Jeongeun Kim7d0986172019-12-04 03:17:25706 for (auto& entry : receiver_sets_)
707 entry.second->CloseAllReceivers();
rockotf62002a2016-09-15 00:08:59708
avallee0206f782016-07-28 18:55:33709 WebContentsImpl* outermost = GetOutermostWebContents();
avallee9993fca2016-11-17 06:16:50710 if (this != outermost && ContainsOrIsFocusedWebContents()) {
avallee0206f782016-07-28 18:55:33711 // If the current WebContents is in focus, unset it.
avallee9993fca2016-11-17 06:16:50712 outermost->SetAsFocusedWebContentsIfNecessary();
avallee0206f782016-07-28 18:55:33713 }
714
Nick Carterd73635b2018-03-13 18:31:41715 if (mouse_lock_widget_)
James Hollyerd5c9de462020-03-10 19:02:45716 mouse_lock_widget_->RejectMouseLockOrUnlockIfNecessary(
717 blink::mojom::PointerLockResult::kElementDestroyed);
Nick Carterd73635b2018-03-13 18:31:41718
dcheng72be33c2016-01-21 04:11:31719 for (FrameTreeNode* node : frame_tree_.Nodes()) {
720 // Delete all RFHs pending shutdown, which will lead the corresponding RVHs
721 // to be shutdown and be deleted as well.
dcheng57e39e22016-01-21 00:25:38722 node->render_manager()->ClearRFHsPendingShutdown();
dcheng57e39e22016-01-21 00:25:38723 node->render_manager()->ClearWebUIInstances();
dcheng72be33c2016-01-21 04:11:31724 }
carlosk35f35af2015-12-01 10:55:40725
dcheng57e39e22016-01-21 00:25:38726 for (RenderWidgetHostImpl* widget : created_widgets_)
727 widget->DetachDelegate();
[email protected]b24b68a2012-09-24 21:57:26728 created_widgets_.clear();
729
[email protected]3ab9cb82011-06-03 18:02:07730 // Clear out any JavaScript state.
avi5d3b8692016-10-12 22:00:46731 if (dialog_manager_) {
avi6879fcf2017-01-21 05:27:53732 dialog_manager_->CancelDialogs(this, /*reset_state=*/true);
avi5d3b8692016-10-12 22:00:46733 }
[email protected]3ab9cb82011-06-03 18:02:07734
Joel Hockey85b379d2017-12-11 10:42:13735 color_chooser_.reset();
Rakina Zata Amni16196122018-11-14 02:14:00736 find_request_manager_.reset();
[email protected]da8543762012-03-20 08:52:20737
[email protected]420ae012009-04-24 05:16:32738 NotifyDisconnected();
[email protected]420ae012009-04-24 05:16:32739
[email protected]ca13a442012-04-17 14:00:12740 // Notify any observer that have a reference on this WebContents.
[email protected]8ff00d72012-10-23 19:12:21741 NotificationService::current()->Notify(
742 NOTIFICATION_WEB_CONTENTS_DESTROYED,
743 Source<WebContents>(this),
744 NotificationService::NoDetails());
[email protected]420ae012009-04-24 05:16:32745
Arthur Sonzogni5a9a88c2018-10-18 13:16:00746 // Destroy all subframes now. This notifies observers.
747 GetMainFrame()->ResetChildren();
[email protected]df0c843f2014-05-23 20:37:23748 GetRenderManager()->ResetProxyHosts();
[email protected]c06c58c2014-03-12 20:31:59749
clamy0e119882015-07-31 16:12:33750 // Manually call the observer methods for the root frame tree node. It is
751 // necessary to manually delete all objects tracking navigations
752 // (NavigationHandle, NavigationRequest) for observers to be properly
753 // notified of these navigations stopping before the WebContents is
754 // destroyed.
[email protected]df0c843f2014-05-23 20:37:23755 RenderFrameHostManager* root = GetRenderManager();
naskob21fe4872015-02-24 14:15:56756
Lowell Manners20250112019-10-18 15:06:15757 GetController().GetBackForwardCache().Shutdown();
758
naskob21fe4872015-02-24 14:15:56759 root->current_frame_host()->SetRenderFrameCreated(false);
clamyfc3850d2018-02-23 14:35:28760 root->current_frame_host()->ResetNavigationRequests();
clamy0e119882015-07-31 16:12:33761
Arthur Hemeryc3380172018-01-22 14:00:17762 // Do not update state as the WebContents is being destroyed.
Arthur Hemery241b9392019-10-24 11:08:41763 frame_tree_.root()->ResetNavigationRequest(true);
Arthur Hemeryc3380172018-01-22 14:00:17764 if (root->speculative_frame_host()) {
danakjf2342432019-05-15 21:46:09765 root->speculative_frame_host()->DeleteRenderFrame(
766 FrameDeleteIntention::kSpeculativeMainFrameForShutdown);
Arthur Hemeryc3380172018-01-22 14:00:17767 root->speculative_frame_host()->SetRenderFrameCreated(false);
clamyfc3850d2018-02-23 14:35:28768 root->speculative_frame_host()->ResetNavigationRequests();
clamy0e119882015-07-31 16:12:33769 }
[email protected]f273ee52013-10-18 16:05:27770
brettw4b461082016-11-19 18:55:16771#if BUILDFLAG(ENABLE_PLUGINS)
zqzhang181047e62016-07-01 13:37:17772 // Call this before WebContentsDestroyed() is broadcasted since
773 // AudioFocusManager will be destroyed after that.
774 pepper_playback_observer_.reset();
775#endif // defined(ENABLED_PLUGINS)
776
Chris Hamiltondf0d72cd2018-05-29 16:23:53777 // If audio is playing then notify external observers of the audio stream
778 // disappearing.
779 if (is_currently_audible_) {
780 is_currently_audible_ = false;
781 for (auto& observer : observers_)
782 observer.OnAudioStateChanged(false);
783
784 if (GetOuterWebContents())
785 GetOuterWebContents()->OnAudioStateChanged();
786 }
787
ericwilligersde386342016-10-19 02:35:09788 for (auto& observer : observers_)
789 observer.FrameDeleted(root->current_frame_host());
naskoa83483fb2015-02-27 16:57:10790
ericwilligersde386342016-10-19 02:35:09791 for (auto& observer : observers_)
792 observer.RenderViewDeleted(root->current_host());
[email protected]f273ee52013-10-18 16:05:27793
Kevin McNee576bb5d82019-05-29 16:47:51794#if defined(OS_ANDROID)
795 // For simplicity, destroy the Java WebContents before we notify of the
796 // destruction of the WebContents.
797 ClearWebContentsAndroid();
798#endif
799
ericwilligersde386342016-10-19 02:35:09800 for (auto& observer : observers_)
801 observer.WebContentsDestroyed();
[email protected]12a46832014-05-09 13:35:58802
Becca Hughesd11d6502018-07-31 17:01:28803 if (display_cutout_host_impl_)
804 display_cutout_host_impl_->WebContentsDestroyed();
805
ericwilligersde386342016-10-19 02:35:09806 for (auto& observer : observers_)
807 observer.ResetWebContents();
[email protected]232a5812011-03-04 22:42:08808
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28809 SetDelegate(nullptr);
[email protected]b5a1d11c2011-02-17 03:09:42810}
811
erikchen50735fd2018-05-05 15:08:33812std::unique_ptr<WebContentsImpl> WebContentsImpl::CreateWithOpener(
[email protected]54944cde2012-12-09 09:24:59813 const WebContents::CreateParams& params,
Ian Clelland5cbaaf82017-11-27 22:00:03814 RenderFrameHostImpl* opener_rfh) {
[email protected]e11f0e92013-06-12 15:12:03815 TRACE_EVENT0("browser", "WebContentsImpl::CreateWithOpener");
Ian Clelland5cbaaf82017-11-27 22:00:03816 FrameTreeNode* opener = nullptr;
817 if (opener_rfh)
818 opener = opener_rfh->frame_tree_node();
erikchen50735fd2018-05-05 15:08:33819 std::unique_ptr<WebContentsImpl> new_contents(
820 new WebContentsImpl(params.browser_context));
lukasza6f8ac622017-06-06 03:10:20821 new_contents->SetOpenerForNewContents(opener, params.opener_suppressed);
alexmose201c7cd2015-06-10 17:14:21822
alexmosaedfc6f2016-01-21 23:57:38823 // If the opener is sandboxed, a new popup must inherit the opener's sandbox
824 // flags, and these flags take effect immediately. An exception is if the
825 // opener's sandbox flags lack the PropagatesToAuxiliaryBrowsingContexts
826 // bit (which is controlled by the "allow-popups-to-escape-sandbox" token).
827 // See https://html.spec.whatwg.org/#attr-iframe-sandbox.
lukasza6f8ac622017-06-06 03:10:20828 FrameTreeNode* new_root = new_contents->GetFrameTree()->root();
alexmosaedfc6f2016-01-21 23:57:38829 if (opener) {
arthursonzognib93a4472020-04-10 07:38:00830 network::mojom::WebSandboxFlags opener_flags =
Antonio Gomes7096ac02020-02-14 14:29:54831 opener_rfh->active_sandbox_flags();
arthursonzognib93a4472020-04-10 07:38:00832 const network::mojom::WebSandboxFlags inherit_flag =
833 network::mojom::WebSandboxFlags::kPropagatesToAuxiliaryBrowsingContexts;
Ehsan Karamad39407082019-02-19 23:38:19834 bool sandbox_propagates_to_auxilary_context =
835 (opener_flags & inherit_flag) == inherit_flag;
836 if (sandbox_propagates_to_auxilary_context)
Charlie Hue1b77ac2019-12-13 21:30:17837 new_root->SetPendingFramePolicy({opener_flags,
838 {} /* container_policy */,
839 {} /* required_document_policy */});
arthursonzognib93a4472020-04-10 07:38:00840 if (opener_flags == network::mojom::WebSandboxFlags::kNone ||
Ehsan Karamad39407082019-02-19 23:38:19841 sandbox_propagates_to_auxilary_context) {
842 // TODO(ekaramad, iclelland): Do not propagate feature policies from non-
843 // sandboxed disowned openers (rel=noopener).
844 // If the current page is not sandboxed, or if the sandbox is to propagate
845 // to the popups then opener's feature policy will apply to the new popup
846 // as well.
847 new_root->SetOpenerFeaturePolicyState(
Ehsan Karamad34d10682019-02-26 20:55:04848 opener_rfh->feature_policy()->GetFeatureState());
alexmosaedfc6f2016-01-21 23:57:38849 }
850 }
851
mark a. foltzef394fce2017-10-21 09:11:02852 // Apply starting sandbox flags.
Luna Luc3fdacdf2017-11-08 04:48:53853 blink::FramePolicy frame_policy(new_root->pending_frame_policy());
mark a. foltzef394fce2017-10-21 09:11:02854 frame_policy.sandbox_flags |= params.starting_sandbox_flags;
855 new_root->SetPendingFramePolicy(frame_policy);
Charlie Hu5ffc0152019-12-06 15:59:53856 new_root->CommitFramePolicy(frame_policy);
mark a. foltzef394fce2017-10-21 09:11:02857
alexmose201c7cd2015-06-10 17:14:21858 // This may be true even when opener is null, such as when opening blocked
859 // popups.
alexmos090fae8e2015-05-28 17:09:28860 if (params.created_with_opener)
861 new_contents->created_with_opener_ = true;
862
Sean Toppingbbc4a2bd2019-01-30 02:16:14863 WebContentsImpl* outer_web_contents = nullptr;
[email protected]4858e432014-06-23 18:14:17864 if (params.guest_delegate) {
[email protected]83100cd2014-05-10 11:50:06865 // This makes |new_contents| act as a guest.
866 // For more info, see comment above class BrowserPluginGuest.
Albert J. Wongc55cc64d2018-10-12 02:50:04867 BrowserPluginGuest::CreateInWebContents(new_contents.get(),
868 params.guest_delegate);
Sean Toppingbbc4a2bd2019-01-30 02:16:14869 outer_web_contents = static_cast<WebContentsImpl*>(
870 params.guest_delegate->GetOwnerWebContents());
[email protected]83100cd2014-05-10 11:50:06871 }
mark a. foltzef394fce2017-10-21 09:11:02872
[email protected]54944cde2012-12-09 09:24:59873 new_contents->Init(params);
Sean Toppingbbc4a2bd2019-01-30 02:16:14874 if (outer_web_contents)
875 outer_web_contents->InnerWebContentsCreated(new_contents.get());
[email protected]d1198fd2012-08-13 22:50:19876 return new_contents;
877}
878
[email protected]95640212014-07-26 18:14:30879// static
880std::vector<WebContentsImpl*> WebContentsImpl::GetAllWebContents() {
881 std::vector<WebContentsImpl*> result;
dcheng59716272016-04-09 05:19:08882 std::unique_ptr<RenderWidgetHostIterator> widgets(
[email protected]95640212014-07-26 18:14:30883 RenderWidgetHostImpl::GetRenderWidgetHosts());
[email protected]95640212014-07-26 18:14:30884 while (RenderWidgetHost* rwh = widgets->GetNextHost()) {
[email protected]95640212014-07-26 18:14:30885 RenderViewHost* rvh = RenderViewHost::From(rwh);
886 if (!rvh)
887 continue;
888 WebContents* web_contents = WebContents::FromRenderViewHost(rvh);
889 if (!web_contents)
890 continue;
avidf38c952015-10-27 13:45:13891 if (web_contents->GetRenderViewHost() != rvh)
892 continue;
893 // Because a WebContents can only have one current RVH at a time, there will
894 // be no duplicate WebContents here.
895 result.push_back(static_cast<WebContentsImpl*>(web_contents));
[email protected]95640212014-07-26 18:14:30896 }
897 return result;
898}
899
clamya16aa8172015-05-26 13:07:25900// static
901WebContentsImpl* WebContentsImpl::FromFrameTreeNode(
paulmeyerfeafc2d2017-04-25 21:46:40902 const FrameTreeNode* frame_tree_node) {
clamya16aa8172015-05-26 13:07:25903 return static_cast<WebContentsImpl*>(
904 WebContents::FromRenderFrameHost(frame_tree_node->current_frame_host()));
905}
906
clamy0d32d6d2015-11-24 11:16:26907// static
Patrick Monette275a6f92b2020-01-09 21:05:04908WebContents* WebContentsImpl::FromRenderFrameHostID(
909 GlobalFrameRoutingId render_frame_host_id) {
910 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) ||
911 !BrowserThread::IsThreadInitialized(BrowserThread::UI));
Patrick Monette7364e6972020-01-09 22:56:02912 RenderFrameHost* render_frame_host =
913 RenderFrameHost::FromID(render_frame_host_id);
914 if (!render_frame_host)
915 return nullptr;
916
917 return WebContents::FromRenderFrameHost(render_frame_host);
Patrick Monette275a6f92b2020-01-09 21:05:04918}
919
920// static
clamy0d32d6d2015-11-24 11:16:26921WebContents* WebContentsImpl::FromRenderFrameHostID(int render_process_host_id,
922 int render_frame_host_id) {
Patrick Monette7364e6972020-01-09 22:56:02923 return FromRenderFrameHostID(
924 GlobalFrameRoutingId(render_process_host_id, render_frame_host_id));
clamy0d32d6d2015-11-24 11:16:26925}
926
paulmeyerfeafc2d2017-04-25 21:46:40927// static
928WebContentsImpl* WebContentsImpl::FromOuterFrameTreeNode(
929 const FrameTreeNode* frame_tree_node) {
930 return WebContentsImpl::FromFrameTreeNode(frame_tree_node)
931 ->node_.GetInnerWebContentsInFrame(frame_tree_node);
932}
933
[email protected]b0936d22013-11-28 06:47:36934RenderFrameHostManager* WebContentsImpl::GetRenderManagerForTesting() {
[email protected]fa944cb82013-11-15 17:51:21935 return GetRenderManager();
[email protected]765187182012-01-11 23:59:28936}
937
nicka0ac8382016-12-15 23:59:23938bool WebContentsImpl::OnMessageReceived(RenderViewHostImpl* render_view_host,
[email protected]7bb761892012-07-20 09:32:47939 const IPC::Message& message) {
nicka0ac8382016-12-15 23:59:23940 for (auto& observer : observers_) {
Lukasz Anforowicz89f2c1b2017-08-30 21:36:46941 // TODO(nick, creis): https://crbug.com/758026: Replace all uses of this
942 // variant of OnMessageReceived with the version that takes a
943 // RenderFrameHost, and then delete it.
nicka0ac8382016-12-15 23:59:23944 if (observer.OnMessageReceived(message))
945 return true;
[email protected]64ffefa2014-05-10 12:06:33946 }
[email protected]403415a2011-01-10 18:57:53947
Henrique Ferreiroba535472020-04-21 11:38:36948#if BUILDFLAG(ENABLE_PLUGINS)
[email protected]724159a2010-12-30 01:11:18949 bool handled = true;
nicka0ac8382016-12-15 23:59:23950 IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(WebContentsImpl, message, render_view_host)
nicka0ac8382016-12-15 23:59:23951 IPC_MESSAGE_HANDLER(ViewHostMsg_RequestPpapiBrokerPermission,
952 OnRequestPpapiBrokerPermission)
nicka0ac8382016-12-15 23:59:23953 IPC_MESSAGE_UNHANDLED(handled = false)
954 IPC_END_MESSAGE_MAP()
nicka0ac8382016-12-15 23:59:23955 return handled;
Henrique Ferreiroba535472020-04-21 11:38:36956#else
957 return false;
958#endif
nicka0ac8382016-12-15 23:59:23959}
960
961bool WebContentsImpl::OnMessageReceived(RenderFrameHostImpl* render_frame_host,
962 const IPC::Message& message) {
Denis Kuznetsov [CET]2c6f0159f2020-05-18 14:18:16963 {
964 WebUIImpl* web_ui = render_frame_host->web_ui();
965 if (web_ui && web_ui->OnMessageReceived(message, render_frame_host))
966 return true;
967 }
968
nicka0ac8382016-12-15 23:59:23969 for (auto& observer : observers_) {
970 if (observer.OnMessageReceived(message, render_frame_host))
971 return true;
972 }
973
974 bool handled = true;
975 IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(WebContentsImpl, message, render_frame_host)
avi3a3332622015-09-01 01:18:54976 IPC_MESSAGE_HANDLER(FrameHostMsg_DidRunInsecureContent,
[email protected]724159a2010-12-30 01:11:18977 OnDidRunInsecureContent)
estark910b4572015-12-09 20:55:41978 IPC_MESSAGE_HANDLER(FrameHostMsg_DidDisplayContentWithCertificateErrors,
979 OnDidDisplayContentWithCertificateErrors)
980 IPC_MESSAGE_HANDLER(FrameHostMsg_DidRunContentWithCertificateErrors,
981 OnDidRunContentWithCertificateErrors)
brettw4b461082016-11-19 18:55:16982#if BUILDFLAG(ENABLE_PLUGINS)
emaxxe70f5e12015-05-29 11:26:00983 IPC_MESSAGE_HANDLER(FrameHostMsg_PepperInstanceCreated,
984 OnPepperInstanceCreated)
985 IPC_MESSAGE_HANDLER(FrameHostMsg_PepperInstanceDeleted,
986 OnPepperInstanceDeleted)
tommyclie6633ca72014-10-31 00:40:42987 IPC_MESSAGE_HANDLER(FrameHostMsg_PepperPluginHung, OnPepperPluginHung)
zqzhang181047e62016-07-01 13:37:17988 IPC_MESSAGE_HANDLER(FrameHostMsg_PepperStartsPlayback,
989 OnPepperStartsPlayback)
990 IPC_MESSAGE_HANDLER(FrameHostMsg_PepperStopsPlayback,
991 OnPepperStopsPlayback)
tommyclie6633ca72014-10-31 00:40:42992 IPC_MESSAGE_HANDLER(FrameHostMsg_PluginCrashed, OnPluginCrashed)
thestigc4cac8f2014-09-04 21:17:50993#endif
[email protected]724159a2010-12-30 01:11:18994 IPC_MESSAGE_UNHANDLED(handled = false)
[email protected]e44d1342014-05-16 21:29:33995 IPC_END_MESSAGE_MAP()
[email protected]724159a2010-12-30 01:11:18996
[email protected]724159a2010-12-30 01:11:18997 return handled;
998}
999
[email protected]d1198fd2012-08-13 22:50:191000NavigationControllerImpl& WebContentsImpl::GetController() {
[email protected]f5fa20e2011-12-21 22:35:561001 return controller_;
1002}
1003
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411004BrowserContext* WebContentsImpl::GetBrowserContext() {
[email protected]a26023822011-12-29 00:23:551005 return controller_.GetBrowserContext();
[email protected]627e0512011-12-21 22:55:301006}
1007
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411008const GURL& WebContentsImpl::GetURL() {
Peter Boström101d7672018-12-15 00:43:591009 return GetVisibleURL();
[email protected]be1f56ab2011-12-22 06:55:311010}
1011
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411012const GURL& WebContentsImpl::GetVisibleURL() {
[email protected]c854a7e2013-05-21 16:42:241013 // We may not have a navigation entry yet.
[email protected]59167c22013-06-03 18:07:321014 NavigationEntry* entry = controller_.GetVisibleEntry();
[email protected]c854a7e2013-05-21 16:42:241015 return entry ? entry->GetVirtualURL() : GURL::EmptyGURL();
1016}
1017
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411018const GURL& WebContentsImpl::GetLastCommittedURL() {
[email protected]c854a7e2013-05-21 16:42:241019 // We may not have a navigation entry yet.
1020 NavigationEntry* entry = controller_.GetLastCommittedEntry();
1021 return entry ? entry->GetVirtualURL() : GURL::EmptyGURL();
1022}
1023
[email protected]8ff00d72012-10-23 19:12:211024WebContentsDelegate* WebContentsImpl::GetDelegate() {
[email protected]be1f56ab2011-12-22 06:55:311025 return delegate_;
1026}
1027
[email protected]8ff00d72012-10-23 19:12:211028void WebContentsImpl::SetDelegate(WebContentsDelegate* delegate) {
[email protected]be1f56ab2011-12-22 06:55:311029 // TODO(cbentzel): remove this debugging code?
1030 if (delegate == delegate_)
1031 return;
1032 if (delegate_)
1033 delegate_->Detach(this);
1034 delegate_ = delegate;
[email protected]a6b73c62013-02-11 23:05:081035 if (delegate_) {
[email protected]be1f56ab2011-12-22 06:55:311036 delegate_->Attach(this);
[email protected]a6b73c62013-02-11 23:05:081037 // Ensure the visible RVH reflects the new delegate's preferences.
[email protected]86f98a22013-03-20 14:35:001038 if (view_)
[email protected]e85165c642014-06-10 14:34:311039 view_->SetOverscrollControllerEnabled(CanOverscrollContent());
Joel Einbinder848001c2017-09-19 21:39:311040 if (GetRenderViewHost())
1041 RenderFrameDevToolsAgentHost::WebContentsCreated(this);
[email protected]a6b73c62013-02-11 23:05:081042 }
[email protected]be1f56ab2011-12-22 06:55:311043}
1044
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411045RenderFrameHostImpl* WebContentsImpl::GetMainFrame() {
[email protected]94d0cc12013-12-18 00:07:411046 return frame_tree_.root()->current_frame_host();
[email protected]60eca4eb2013-12-06 00:02:161047}
1048
nick53d5cbf2015-04-23 22:50:141049RenderFrameHostImpl* WebContentsImpl::GetFocusedFrame() {
1050 FrameTreeNode* focused_node = frame_tree_.GetFocusedFrame();
1051 if (!focused_node)
1052 return nullptr;
1053 return focused_node->current_frame_host();
[email protected]9c9343b2014-03-08 02:56:071054}
1055
rob3e2a0732016-01-06 21:22:091056RenderFrameHostImpl* WebContentsImpl::FindFrameByFrameTreeNodeId(
creisf71a2632017-05-04 19:03:501057 int frame_tree_node_id,
1058 int process_id) {
1059 FrameTreeNode* frame = frame_tree_.FindByID(frame_tree_node_id);
1060
1061 // Sanity check that this is in the caller's expected process. Otherwise a
1062 // recent cross-process navigation may have led to a privilege change that the
1063 // caller is not expecting.
1064 if (!frame ||
1065 frame->current_frame_host()->GetProcess()->GetID() != process_id)
1066 return nullptr;
1067
1068 return frame->current_frame_host();
1069}
1070
1071RenderFrameHostImpl* WebContentsImpl::UnsafeFindFrameByFrameTreeNodeId(
rob3e2a0732016-01-06 21:22:091072 int frame_tree_node_id) {
creisf71a2632017-05-04 19:03:501073 // Beware using this! The RenderFrameHost may have changed since the caller
1074 // obtained frame_tree_node_id.
rob3e2a0732016-01-06 21:22:091075 FrameTreeNode* frame = frame_tree_.FindByID(frame_tree_node_id);
1076 return frame ? frame->current_frame_host() : nullptr;
1077}
1078
[email protected]a86c0e962013-12-17 17:10:391079void WebContentsImpl::ForEachFrame(
Lei Zhangebcc6302018-01-12 19:46:451080 const base::RepeatingCallback<void(RenderFrameHost*)>& on_frame) {
dcheng57e39e22016-01-21 00:25:381081 for (FrameTreeNode* node : frame_tree_.Nodes()) {
1082 on_frame.Run(node->current_frame_host());
1083 }
[email protected]a86c0e962013-12-17 17:10:391084}
1085
dchengafb53e22016-02-04 08:11:081086std::vector<RenderFrameHost*> WebContentsImpl::GetAllFrames() {
Xuehui Xieb2300ae42020-06-09 20:25:051087 return GetAllFramesImpl(frame_tree_, /*include_pending=*/false);
1088}
1089
1090std::vector<RenderFrameHost*> WebContentsImpl::GetAllFramesIncludingPending() {
1091 return GetAllFramesImpl(frame_tree_, /*include_pending=*/true);
dchengafb53e22016-02-04 08:11:081092}
1093
lukaszacbdf52e2016-01-15 21:19:511094int WebContentsImpl::SendToAllFrames(IPC::Message* message) {
Xuehui Xieb2300ae42020-06-09 20:25:051095 return SendToAllFramesImpl(frame_tree_, /*include_pending=*/false, message);
1096}
lukasza1a9ab2422016-03-02 18:47:251097
Xuehui Xieb2300ae42020-06-09 20:25:051098int WebContentsImpl::SendToAllFramesIncludingPending(IPC::Message* message) {
1099 return SendToAllFramesImpl(frame_tree_, /*include_pending=*/true, message);
[email protected]a86c0e962013-12-17 17:10:391100}
1101
lfgdb5c4ed2016-03-04 23:09:071102void WebContentsImpl::SendPageMessage(IPC::Message* msg) {
alexmos136fd6e62016-08-15 20:58:411103 frame_tree_.root()->render_manager()->SendPageMessage(msg, nullptr);
lfgdb5c4ed2016-03-04 23:09:071104}
1105
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411106RenderViewHostImpl* WebContentsImpl::GetRenderViewHost() {
[email protected]fa944cb82013-11-15 17:51:211107 return GetRenderManager()->current_host();
[email protected]be1f56ab2011-12-22 06:55:311108}
1109
creis89a0f782015-05-27 16:13:171110void WebContentsImpl::CancelActiveAndPendingDialogs() {
avi5d3b8692016-10-12 22:00:461111 if (dialog_manager_) {
avi6879fcf2017-01-21 05:27:531112 dialog_manager_->CancelDialogs(this, /*reset_state=*/false);
avi5d3b8692016-10-12 22:00:461113 }
creis89a0f782015-05-27 16:13:171114 if (browser_plugin_embedder_)
1115 browser_plugin_embedder_->CancelGuestDialogs();
1116}
1117
naskoc0fceff2015-04-30 15:53:521118void WebContentsImpl::ClosePage() {
1119 GetRenderViewHost()->ClosePage();
1120}
1121
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411122RenderWidgetHostView* WebContentsImpl::GetRenderWidgetHostView() {
[email protected]fa944cb82013-11-15 17:51:211123 return GetRenderManager()->GetRenderWidgetHostView();
[email protected]be1f56ab2011-12-22 06:55:311124}
1125
lfg265a2672016-04-23 03:11:021126RenderWidgetHostView* WebContentsImpl::GetTopLevelRenderWidgetHostView() {
1127 if (GetOuterWebContents())
1128 return GetOuterWebContents()->GetTopLevelRenderWidgetHostView();
1129 return GetRenderManager()->GetRenderWidgetHostView();
1130}
1131
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411132RenderWidgetHostView* WebContentsImpl::GetFullscreenRenderWidgetHostView() {
vmpstr6d9996c82017-02-23 00:43:251133 if (auto* widget_host = GetFullscreenRenderWidgetHost())
ekaramadaeb3c5c2016-11-09 03:22:351134 return widget_host->GetView();
1135 return nullptr;
[email protected]4aebbca2013-09-17 22:26:491136}
1137
[email protected]8ff00d72012-10-23 19:12:211138WebContentsView* WebContentsImpl::GetView() const {
[email protected]be1f56ab2011-12-22 06:55:311139 return view_.get();
1140}
1141
Mike Wassermanb213b9352020-04-16 00:32:341142void WebContentsImpl::OnScreensChange() {
1143 for (FrameTreeNode* node : frame_tree_.Nodes()) {
1144 RenderFrameHostImpl* rfh = node->current_frame_host();
1145 rfh->GetAssociatedLocalFrame()->OnScreensChange();
1146 }
1147}
1148
leon.han552e9de2017-02-09 14:37:301149void WebContentsImpl::OnScreenOrientationChange() {
1150 DCHECK(screen_orientation_provider_);
Lan Weif81c6e7c2020-02-12 16:46:091151 DidChangeScreenOrientation();
Becca Hughese9e27952018-06-25 17:08:271152 screen_orientation_provider_->OnOrientationChange();
leon.hane4db177a2017-02-07 14:19:161153}
1154
Alan Cutter92d59252019-03-29 02:32:331155base::Optional<SkColor> WebContentsImpl::GetThemeColor() {
Carlos Caballero1215f882019-10-29 15:58:431156 return GetRenderViewHost()->theme_color();
yusufod41c5f92015-03-06 00:14:281157}
1158
Doug Turner63f3c7b2017-07-29 05:10:011159void WebContentsImpl::SetAccessibilityMode(ui::AXMode mode) {
[email protected]95640212014-07-26 18:14:301160 if (mode == accessibility_mode_)
1161 return;
1162
dmazzonif8a31042016-12-03 00:52:131163 // Don't allow accessibility to be enabled for WebContents that are never
danakj77eb7e82020-01-09 19:38:461164 // user-visible, like background pages.
1165 if (IsNeverComposited())
dmazzonif8a31042016-12-03 00:52:131166 return;
1167
[email protected]95640212014-07-26 18:14:301168 accessibility_mode_ = mode;
dcheng57e39e22016-01-21 00:25:381169
1170 for (FrameTreeNode* node : frame_tree_.Nodes()) {
dmazzonif8a31042016-12-03 00:52:131171 UpdateAccessibilityModeOnFrame(node->current_frame_host());
clamy610c63b32017-12-22 15:05:181172 // Also update accessibility mode on the speculative RenderFrameHost for
1173 // this FrameTreeNode, if one exists.
Alex Moshchuk3d8a1f392017-10-24 19:01:261174 RenderFrameHost* speculative_frame_host =
1175 node->render_manager()->speculative_frame_host();
1176 if (speculative_frame_host)
1177 UpdateAccessibilityModeOnFrame(speculative_frame_host);
dcheng57e39e22016-01-21 00:25:381178 }
[email protected]95640212014-07-26 18:14:301179}
1180
Doug Turner63f3c7b2017-07-29 05:10:011181void WebContentsImpl::AddAccessibilityMode(ui::AXMode mode) {
1182 ui::AXMode new_mode(accessibility_mode_);
dougtcd3dad732017-03-14 03:26:231183 new_mode |= mode;
1184 SetAccessibilityMode(new_mode);
[email protected]95640212014-07-26 18:14:301185}
1186
Avi Drissman1e620f32018-03-16 13:57:291187// Helper class used by WebContentsImpl::RequestAXTreeSnapshot.
1188// Handles the callbacks from parallel snapshot requests to each frame,
1189// and feeds the results to an AXTreeCombiner, which converts them into a
1190// single combined accessibility tree.
1191class WebContentsImpl::AXTreeSnapshotCombiner
1192 : public base::RefCounted<AXTreeSnapshotCombiner> {
1193 public:
1194 explicit AXTreeSnapshotCombiner(AXTreeSnapshotCallback callback)
1195 : callback_(std::move(callback)) {}
1196
1197 AXTreeSnapshotCallback AddFrame(bool is_root) {
1198 // Adds a reference to |this|.
1199 return base::BindOnce(&AXTreeSnapshotCombiner::ReceiveSnapshot, this,
1200 is_root);
1201 }
1202
1203 void ReceiveSnapshot(bool is_root, const ui::AXTreeUpdate& snapshot) {
1204 combiner_.AddTree(snapshot, is_root);
1205 }
1206
1207 private:
1208 friend class base::RefCounted<AXTreeSnapshotCombiner>;
1209
1210 // This is called automatically after the last call to ReceiveSnapshot
1211 // when there are no more references to this object.
1212 ~AXTreeSnapshotCombiner() {
1213 combiner_.Combine();
1214 std::move(callback_).Run(combiner_.combined());
1215 }
1216
1217 ui::AXTreeCombiner combiner_;
1218 AXTreeSnapshotCallback callback_;
1219};
1220
1221void WebContentsImpl::RequestAXTreeSnapshot(AXTreeSnapshotCallback callback,
1222 ui::AXMode ax_mode) {
dmazzonid95ae842016-04-12 21:17:391223 // Send a request to each of the frames in parallel. Each one will return
1224 // an accessibility tree snapshot, and AXTreeSnapshotCombiner will combine
1225 // them into a single tree and call |callback| with that result, then
1226 // delete |combiner|.
Avi Drissman1e620f32018-03-16 13:57:291227 FrameTreeNode* root_node = frame_tree_.root();
tzik58faa742018-08-01 10:54:181228 auto combiner =
1229 base::MakeRefCounted<AXTreeSnapshotCombiner>(std::move(callback));
Avi Drissman1e620f32018-03-16 13:57:291230
tzik58faa742018-08-01 10:54:181231 RecursiveRequestAXTreeSnapshotOnFrame(root_node, combiner.get(), ax_mode);
Avi Drissman1e620f32018-03-16 13:57:291232}
1233
1234void WebContentsImpl::RecursiveRequestAXTreeSnapshotOnFrame(
1235 FrameTreeNode* root_node,
1236 AXTreeSnapshotCombiner* combiner,
1237 ui::AXMode ax_mode) {
dmazzonid95ae842016-04-12 21:17:391238 for (FrameTreeNode* frame_tree_node : frame_tree_.Nodes()) {
Avi Drissman1e620f32018-03-16 13:57:291239 WebContentsImpl* inner_contents =
1240 node_.GetInnerWebContentsInFrame(frame_tree_node);
1241 if (inner_contents) {
1242 inner_contents->RecursiveRequestAXTreeSnapshotOnFrame(root_node, combiner,
1243 ax_mode);
1244 } else {
1245 bool is_root = frame_tree_node == root_node;
1246 frame_tree_node->current_frame_host()->RequestAXTreeSnapshot(
1247 combiner->AddFrame(is_root), ax_mode);
1248 }
dmazzonid95ae842016-04-12 21:17:391249 }
dmazzoni83ba5c82015-04-14 07:11:511250}
1251
Becca Hughes3b5a43482018-07-17 22:31:551252void WebContentsImpl::NotifyViewportFitChanged(
1253 blink::mojom::ViewportFit value) {
1254 for (auto& observer : observers_)
1255 observer.ViewportFitChanged(value);
1256}
1257
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411258FindRequestManager* WebContentsImpl::GetFindRequestManagerForTesting() {
Ehsan Karamad6beb2ea2018-11-25 18:15:131259 return GetFindRequestManager();
1260}
1261
mcnee336ea2c2017-05-23 22:50:591262#if !defined(OS_ANDROID)
akabac6bd1212018-06-25 20:10:481263void WebContentsImpl::UpdateZoom() {
1264 RenderWidgetHostImpl* rwh = GetRenderViewHost()->GetWidget();
1265 if (rwh->GetView())
1266 rwh->SynchronizeVisualProperties();
wjmaclean64951902016-04-29 20:59:121267}
1268
wjmaclean64951902016-04-29 20:59:121269
1270void WebContentsImpl::UpdateZoomIfNecessary(const std::string& scheme,
akabac6bd1212018-06-25 20:10:481271 const std::string& host) {
wjmaclean64951902016-04-29 20:59:121272 NavigationEntry* entry = GetController().GetLastCommittedEntry();
1273 if (!entry)
1274 return;
1275
1276 GURL url = HostZoomMap::GetURLFromEntry(entry);
1277 if (host != net::GetHostOrSpecFromURL(url) ||
1278 (!scheme.empty() && !url.SchemeIs(scheme))) {
1279 return;
1280 }
1281
akabac6bd1212018-06-25 20:10:481282 UpdateZoom();
wjmaclean64951902016-04-29 20:59:121283}
mcnee336ea2c2017-05-23 22:50:591284#endif // !defined(OS_ANDROID)
wjmaclean64951902016-04-29 20:59:121285
Julie Jeongeun Kim7d0986172019-12-04 03:17:251286base::OnceClosure WebContentsImpl::AddReceiverSet(
rockotf62002a2016-09-15 00:08:591287 const std::string& interface_name,
Julie Jeongeun Kim7d0986172019-12-04 03:17:251288 WebContentsReceiverSet* receiver_set) {
rockotf62002a2016-09-15 00:08:591289 auto result =
Julie Jeongeun Kim7d0986172019-12-04 03:17:251290 receiver_sets_.insert(std::make_pair(interface_name, receiver_set));
rockotf62002a2016-09-15 00:08:591291 DCHECK(result.second);
Julie Jeongeun Kim7d0986172019-12-04 03:17:251292 return base::BindOnce(&WebContentsImpl::RemoveReceiverSet,
danakjf4b9e942019-11-29 15:43:041293 weak_factory_.GetWeakPtr(), interface_name);
rockotf62002a2016-09-15 00:08:591294}
1295
Julie Jeongeun Kim7d0986172019-12-04 03:17:251296WebContentsReceiverSet* WebContentsImpl::GetReceiverSet(
rockot8bbad222017-03-22 04:34:051297 const std::string& interface_name) {
Julie Jeongeun Kim7d0986172019-12-04 03:17:251298 auto it = receiver_sets_.find(interface_name);
1299 if (it == receiver_sets_.end())
rockot8bbad222017-03-22 04:34:051300 return nullptr;
1301 return it->second;
1302}
1303
John Delaney732721e92020-02-07 23:11:271304void WebContentsImpl::RemoveReceiverSetForTesting(
1305 const std::string& interface_name) {
1306 RemoveReceiverSet(interface_name);
1307}
1308
paulmeyerfeafc2d2017-04-25 21:46:401309std::vector<WebContentsImpl*> WebContentsImpl::GetWebContentsAndAllInner() {
1310 std::vector<WebContentsImpl*> all_contents(1, this);
1311
1312 for (size_t i = 0; i != all_contents.size(); ++i) {
1313 for (auto* inner_contents : all_contents[i]->GetInnerWebContents()) {
Lucas Furukawa Gadani2ec00c82018-12-14 15:53:161314 all_contents.push_back(static_cast<WebContentsImpl*>(inner_contents));
paulmeyerfeafc2d2017-04-25 21:46:401315 }
1316 }
1317
1318 return all_contents;
1319}
1320
Sam McNally145fb172017-05-10 00:13:231321void WebContentsImpl::NotifyManifestUrlChanged(
Yuzu Saijo0c263c702020-06-08 03:41:241322 RenderFrameHost* rfh,
Sam McNally145fb172017-05-10 00:13:231323 const base::Optional<GURL>& manifest_url) {
1324 for (auto& observer : observers_)
Yuzu Saijo0c263c702020-06-08 03:41:241325 observer.DidUpdateWebManifestURL(rfh, manifest_url);
Sam McNally145fb172017-05-10 00:13:231326}
1327
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411328WebUI* WebContentsImpl::GetWebUI() {
Nasko Oskov2f7d2112019-10-29 03:26:571329 WebUI* committed_web_ui = GetCommittedWebUI();
1330 if (committed_web_ui)
1331 return committed_web_ui;
1332
1333 if (GetRenderManager()->speculative_frame_host())
1334 return GetRenderManager()->speculative_frame_host()->web_ui();
1335
1336 return nullptr;
[email protected]be1f56ab2011-12-22 06:55:311337}
1338
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411339WebUI* WebContentsImpl::GetCommittedWebUI() {
carlosk35f35af2015-12-01 10:55:401340 return frame_tree_.root()->current_frame_host()->web_ui();
[email protected]d5f942ba2008-09-26 19:30:341341}
1342
Maks Orlovich73f374d2020-04-02 12:46:131343void WebContentsImpl::SetUserAgentOverride(
1344 const blink::UserAgentOverride& ua_override,
1345 bool override_in_new_tabs) {
1346 DCHECK(!ua_override.ua_metadata_override.has_value() ||
1347 !ua_override.ua_string_override.empty());
1348
1349 if (GetUserAgentOverride() == ua_override)
[email protected]bf70edce2012-06-20 22:32:221350 return;
1351
Changwan Ryuc1134a82018-03-07 02:10:141352 should_override_user_agent_in_new_tabs_ = override_in_new_tabs;
1353
Maks Orlovich73f374d2020-04-02 12:46:131354 renderer_preferences_.user_agent_override = ua_override;
[email protected]bf70edce2012-06-20 22:32:221355
Bruce Long1e3e1f542019-10-16 17:56:281356 // Send the new override string to all renderers in the current page.
1357 SyncRendererPrefs();
[email protected]bf70edce2012-06-20 22:32:221358
1359 // Reload the page if a load is currently in progress to avoid having
1360 // different parts of the page loaded using different user agents.
Scott Violete1d58cc2020-04-28 23:07:171361 // No need to reload if the current entry matches that of the
1362 // NavigationRequest supplied to DidStartNavigation() as NavigationRequest
1363 // handles it.
[email protected]6286a372013-10-09 04:03:271364 NavigationEntry* entry = controller_.GetVisibleEntry();
Scott Violete1d58cc2020-04-28 23:07:171365 if (IsLoading() && entry != nullptr && entry->GetIsOverridingUserAgent() &&
1366 (!frame_tree_.root()->navigation_request() ||
1367 frame_tree_.root()->navigation_request()->ua_change_requires_reload())) {
toyoshim6142d96f2016-12-19 09:07:251368 controller_.Reload(ReloadType::BYPASSING_CACHE, true);
Scott Violete1d58cc2020-04-28 23:07:171369 }
[email protected]8d0f3312012-08-18 01:47:531370
ericwilligersde386342016-10-19 02:35:091371 for (auto& observer : observers_)
Maks Orlovich73f374d2020-04-02 12:46:131372 observer.UserAgentOverrideSet(ua_override);
[email protected]86ef6a392012-05-11 22:03:111373}
1374
Scott Violet2a6c5bff2020-04-29 23:59:061375void WebContentsImpl::SetRendererInitiatedUserAgentOverrideOption(
1376 NavigationController::UserAgentOverrideOption option) {
1377 renderer_initiated_user_agent_override_option_ = option;
1378}
1379
Maks Orlovich73f374d2020-04-02 12:46:131380const blink::UserAgentOverride& WebContentsImpl::GetUserAgentOverride() {
[email protected]bf70edce2012-06-20 22:32:221381 return renderer_preferences_.user_agent_override;
[email protected]86ef6a392012-05-11 22:03:111382}
1383
Scott Violet2a6c5bff2020-04-29 23:59:061384bool WebContentsImpl::ShouldOverrideUserAgentForRendererInitiatedNavigation() {
1385 NavigationEntryImpl* current_entry = controller_.GetLastCommittedEntry();
1386 if (!current_entry)
1387 return should_override_user_agent_in_new_tabs_;
1388
1389 switch (renderer_initiated_user_agent_override_option_) {
1390 case NavigationController::UA_OVERRIDE_INHERIT:
1391 return current_entry->GetIsOverridingUserAgent();
1392 case NavigationController::UA_OVERRIDE_TRUE:
1393 return true;
1394 case NavigationController::UA_OVERRIDE_FALSE:
1395 return false;
1396 default:
1397 break;
1398 }
1399 return false;
Changwan Ryuc1134a82018-03-07 02:10:141400}
1401
dmazzonidd3d51a72016-12-14 18:41:011402void WebContentsImpl::EnableWebContentsOnlyAccessibilityMode() {
Dominic Mazzonid9fda172019-03-07 17:12:071403 // If accessibility is already enabled, we'll need to force a reset
1404 // in order to ensure new observers of accessibility events get the
1405 // full accessibility tree from scratch.
1406 bool need_reset = GetAccessibilityMode().has_mode(ui::AXMode::kWebContents);
1407
1408 ui::AXMode desired_mode =
1409 GetContentClient()->browser()->GetAXModeForBrowserContext(
1410 GetBrowserContext());
1411 desired_mode |= ui::kAXModeWebContentsOnly;
1412 AddAccessibilityMode(desired_mode);
1413
1414 if (need_reset) {
dchengafb53e22016-02-04 08:11:081415 for (RenderFrameHost* rfh : GetAllFrames())
1416 ResetAccessibility(rfh);
dchengafb53e22016-02-04 08:11:081417 }
[email protected]95640212014-07-26 18:14:301418}
1419
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411420bool WebContentsImpl::IsWebContentsOnlyAccessibilityModeForTesting() {
Doug Turner63f3c7b2017-07-29 05:10:011421 return accessibility_mode_ == ui::kAXModeWebContentsOnly;
[email protected]95640212014-07-26 18:14:301422}
1423
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411424bool WebContentsImpl::IsFullAccessibilityModeForTesting() {
Doug Turner63f3c7b2017-07-29 05:10:011425 return accessibility_mode_ == ui::kAXModeComplete;
[email protected]95640212014-07-26 18:14:301426}
1427
Becca Hughes6daf5662018-06-27 16:50:541428#if defined(OS_ANDROID)
1429
1430void WebContentsImpl::SetDisplayCutoutSafeArea(gfx::Insets insets) {
Becca Hughes86334622018-07-09 21:29:381431 if (display_cutout_host_impl_)
1432 display_cutout_host_impl_->SetDisplayCutoutSafeArea(insets);
Becca Hughes6daf5662018-06-27 16:50:541433}
1434
1435#endif
1436
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411437const base::string16& WebContentsImpl::GetTitle() {
Nasko Oskov2f7d2112019-10-29 03:26:571438 WebUI* our_web_ui =
1439 GetRenderManager()->speculative_frame_host()
1440 ? GetRenderManager()->speculative_frame_host()->web_ui()
1441 : GetRenderManager()->current_frame_host()->web_ui();
[email protected]7ade2732011-02-10 00:13:581442 if (our_web_ui) {
[email protected]96d185d2009-04-24 03:28:541443 // Don't override the title in view source mode.
Carlos IL4dea8902020-05-26 15:14:291444 NavigationEntry* entry = controller_.GetVisibleEntry();
[email protected]96d185d2009-04-24 03:28:541445 if (!(entry && entry->IsViewSourceMode())) {
[email protected]e0112912011-02-02 22:54:351446 // Give the Web UI the chance to override our title.
[email protected]fcf75d42013-12-03 20:11:261447 const base::string16& title = our_web_ui->GetOverriddenTitle();
[email protected]96d185d2009-04-24 03:28:541448 if (!title.empty())
1449 return title;
1450 }
1451 }
1452
1453 // We use the title for the last committed entry rather than a pending
1454 // navigation entry. For example, when the user types in a URL, we want to
1455 // keep the old page's title until the new load has committed and we get a new
1456 // title.
Carlos IL4dea8902020-05-26 15:14:291457 NavigationEntry* entry = controller_.GetLastCommittedEntry();
[email protected]59167c22013-06-03 18:07:321458
creis072b61f2015-09-28 20:52:241459 // We make an exception for initial navigations. We only want to use the title
1460 // from the visible entry if:
1461 // 1. The pending entry has been explicitly assigned a title to display.
1462 // 2. The user is doing a history navigation in a new tab (e.g., Ctrl+Back),
1463 // which case there is a pending entry index other than -1.
1464 //
1465 // Otherwise, we want to stick with the last committed entry's title during
1466 // new navigations, which have pending entries at index -1 with no title.
1467 if (controller_.IsInitialNavigation() &&
1468 ((controller_.GetVisibleEntry() &&
1469 !controller_.GetVisibleEntry()->GetTitle().empty()) ||
1470 controller_.GetPendingEntryIndex() != -1)) {
1471 entry = controller_.GetVisibleEntry();
[email protected]9eeafc012013-11-25 23:49:471472 }
[email protected]59167c22013-06-03 18:07:321473
[email protected]45d0ef7f2011-01-05 13:46:231474 if (entry) {
jshin1fb76462016-04-05 22:13:031475 return entry->GetTitleForDisplay();
[email protected]45d0ef7f2011-01-05 13:46:231476 }
[email protected]987fc3a2011-05-26 14:18:091477
1478 // |page_title_when_no_navigation_entry_| is finally used
1479 // if no title cannot be retrieved.
1480 return page_title_when_no_navigation_entry_;
[email protected]96d185d2009-04-24 03:28:541481}
1482
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411483SiteInstanceImpl* WebContentsImpl::GetSiteInstance() {
[email protected]fa944cb82013-11-15 17:51:211484 return GetRenderManager()->current_host()->GetSiteInstance();
[email protected]96d185d2009-04-24 03:28:541485}
1486
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411487bool WebContentsImpl::IsLoading() {
Carlos ILd51e7702020-05-07 18:51:391488 return frame_tree_.IsLoading();
[email protected]3c9e1872010-11-18 16:17:491489}
1490
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411491double WebContentsImpl::GetLoadProgress() {
Peter Boström18a40fa2018-10-31 19:03:501492 return frame_tree_.load_progress();
1493}
1494
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411495bool WebContentsImpl::IsLoadingToDifferentDocument() {
clamy44e84ce2016-02-22 15:38:251496 return IsLoading() && is_load_to_different_document_;
[email protected]6dfed692014-05-22 04:18:031497}
1498
Alexander Timindd0fa0c52019-11-06 13:03:371499bool WebContentsImpl::IsDocumentOnLoadCompletedInMainFrame() {
1500 return GetRenderViewHost()->IsDocumentOnLoadCompletedInMainFrame();
1501}
1502
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411503bool WebContentsImpl::IsWaitingForResponse() {
Peter Boströma9a27f6e2018-11-21 13:12:191504 NavigationRequest* ongoing_navigation_request =
1505 frame_tree_.root()->navigation_request();
1506
1507 // An ongoing navigation request means we're waiting for a response.
1508 return ongoing_navigation_request != nullptr;
[email protected]be1f56ab2011-12-22 06:55:311509}
1510
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411511const net::LoadStateWithParam& WebContentsImpl::GetLoadState() {
[email protected]be1f56ab2011-12-22 06:55:311512 return load_state_;
1513}
1514
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411515const base::string16& WebContentsImpl::GetLoadStateHost() {
[email protected]be1f56ab2011-12-22 06:55:311516 return load_state_host_;
1517}
1518
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411519uint64_t WebContentsImpl::GetUploadSize() {
[email protected]be1f56ab2011-12-22 06:55:311520 return upload_size_;
1521}
1522
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411523uint64_t WebContentsImpl::GetUploadPosition() {
[email protected]be1f56ab2011-12-22 06:55:311524 return upload_position_;
1525}
1526
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411527const std::string& WebContentsImpl::GetEncoding() {
Alexander Timine5f20e22019-10-09 23:22:351528 return GetMainFrame()->GetEncoding();
[email protected]be1f56ab2011-12-22 06:55:311529}
1530
Charles Zhaod72f99d2018-09-17 14:18:271531bool WebContentsImpl::WasDiscarded() {
1532 return GetFrameTree()->root()->was_discarded();
1533}
1534
Shubhie Panickerddf2a4e2018-03-06 00:09:061535void WebContentsImpl::SetWasDiscarded(bool was_discarded) {
1536 GetFrameTree()->root()->set_was_discarded();
1537}
1538
Collin Bakerc5259962019-11-14 17:51:581539void WebContentsImpl::IncrementCapturerCount(const gfx::Size& capture_size,
1540 bool stay_hidden) {
[email protected]5a652232013-02-12 06:15:251541 DCHECK(!is_being_destroyed_);
Collin Bakerc5259962019-11-14 17:51:581542 if (stay_hidden)
1543 ++hidden_capturer_count_;
1544 else
1545 ++visible_capturer_count_;
[email protected]222f5822014-02-05 23:40:491546
1547 // Note: This provides a hint to upstream code to size the views optimally
1548 // for quality (e.g., to avoid scaling).
1549 if (!capture_size.IsEmpty() && preferred_size_for_capture_.IsEmpty()) {
1550 preferred_size_for_capture_ = capture_size;
1551 OnPreferredSizeChanged(preferred_size_);
1552 }
ccameron8807c38f2015-01-17 00:29:191553
Collin Bakerc5259962019-11-14 17:51:581554 UpdateVisibilityAndNotifyPageAndView(GetVisibility());
[email protected]54597982013-02-06 01:59:551555}
1556
Collin Bakerc5259962019-11-14 17:51:581557void WebContentsImpl::DecrementCapturerCount(bool stay_hidden) {
1558 if (stay_hidden)
1559 --hidden_capturer_count_;
1560 else
1561 --visible_capturer_count_;
1562 DCHECK_GE(hidden_capturer_count_, 0);
1563 DCHECK_GE(visible_capturer_count_, 0);
[email protected]54597982013-02-06 01:59:551564
[email protected]5a652232013-02-12 06:15:251565 if (is_being_destroyed_)
1566 return;
1567
Francois Doray24fc62c2017-12-11 17:27:331568 if (!IsBeingCaptured()) {
[email protected]222f5822014-02-05 23:40:491569 const gfx::Size old_size = preferred_size_for_capture_;
1570 preferred_size_for_capture_ = gfx::Size();
1571 OnPreferredSizeChanged(old_size);
[email protected]54597982013-02-06 01:59:551572 }
Collin Bakerc5259962019-11-14 17:51:581573
1574 UpdateVisibilityAndNotifyPageAndView(GetVisibility());
[email protected]be1f56ab2011-12-22 06:55:311575}
1576
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411577bool WebContentsImpl::IsBeingCaptured() {
Collin Bakerc5259962019-11-14 17:51:581578 return visible_capturer_count_ + hidden_capturer_count_ > 0;
[email protected]f2bd40812013-07-20 04:30:441579}
1580
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411581bool WebContentsImpl::IsAudioMuted() {
Guido Urdanetaf8372942019-11-15 09:47:211582 return audio_stream_factory_ && audio_stream_factory_->IsMuted();
miu50f97892014-09-22 22:49:521583}
1584
1585void WebContentsImpl::SetAudioMuted(bool mute) {
1586 DVLOG(1) << "SetAudioMuted(mute=" << mute << "), was " << IsAudioMuted()
1587 << " for WebContentsImpl@" << this;
1588
1589 if (mute == IsAudioMuted())
1590 return;
1591
Guido Urdanetaf8372942019-11-15 09:47:211592 GetAudioStreamFactory()->SetMuted(mute);
miu50f97892014-09-22 22:49:521593
ericwilligersde386342016-10-19 02:35:091594 for (auto& observer : observers_)
1595 observer.DidUpdateAudioMutingState(mute);
wjmaclean2f797c782016-01-07 14:52:031596
Jan Rucka442c83f2017-08-08 13:27:541597 // Notification for UI updates in response to the changed muting state.
Collin Bakeraf540cf2019-09-20 20:54:161598 NotifyNavigationStateChanged(INVALIDATE_TYPE_AUDIO);
Jan Rucka442c83f2017-08-08 13:27:541599}
1600
1601bool WebContentsImpl::IsCurrentlyAudible() {
Chris Hamiltondf0d72cd2018-05-29 16:23:531602 return is_currently_audible_;
miu50f97892014-09-22 22:49:521603}
1604
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411605bool WebContentsImpl::IsConnectedToBluetoothDevice() {
ortunodf4d7982016-04-08 02:33:351606 return bluetooth_connected_device_count_ > 0;
ortuno32e7db3c2016-03-29 16:14:201607}
1608
Lucas Furukawa Gadani4b4eed02019-06-04 23:12:041609bool WebContentsImpl::IsConnectedToSerialPort() {
Reilly Grant5e7c79b22019-04-09 17:26:201610 return serial_active_frame_count_ > 0;
1611}
1612
Matt Reynoldse8c6c1f2019-11-02 09:53:531613bool WebContentsImpl::IsConnectedToHidDevice() {
1614 return hid_active_frame_count_ > 0;
1615}
1616
Marijn Kruisselbrink29051042019-08-06 22:56:551617bool WebContentsImpl::HasNativeFileSystemHandles() {
1618 return native_file_system_handle_count_ > 0;
1619}
Marijn Kruisselbrinkf1714aa22019-07-02 03:45:331620bool WebContentsImpl::HasNativeFileSystemDirectoryHandles() {
Marijn Kruisselbrink46fa4bd2019-07-11 18:15:471621 return !native_file_system_directory_handles_.empty();
1622}
1623
1624std::vector<base::FilePath>
1625WebContentsImpl::GetNativeFileSystemDirectoryHandles() {
1626 std::vector<base::FilePath> result;
1627 result.reserve(native_file_system_directory_handles_.size());
1628 for (auto const& entry : native_file_system_directory_handles_)
1629 result.push_back(entry.first);
1630 return result;
Marijn Kruisselbrinkf1714aa22019-07-02 03:45:331631}
1632
1633bool WebContentsImpl::HasWritableNativeFileSystemHandles() {
1634 return native_file_system_writable_handle_count_ > 0;
1635}
1636
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411637bool WebContentsImpl::HasPictureInPictureVideo() {
sawtellea7333a82018-05-31 02:36:361638 return has_picture_in_picture_video_;
1639}
1640
1641void WebContentsImpl::SetHasPictureInPictureVideo(
1642 bool has_picture_in_picture_video) {
1643 // If status of |this| is already accurate, there is no need to update.
1644 if (has_picture_in_picture_video == has_picture_in_picture_video_)
1645 return;
1646 has_picture_in_picture_video_ = has_picture_in_picture_video;
1647 NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB);
François Beaufort058085d2018-08-30 15:29:141648 for (auto& observer : observers_)
1649 observer.MediaPictureInPictureChanged(has_picture_in_picture_video_);
sawtellea7333a82018-05-31 02:36:361650}
1651
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411652bool WebContentsImpl::IsCrashed() {
Dominic Mazzoni73a48322018-05-25 18:14:071653 switch (crashed_status_) {
1654 case base::TERMINATION_STATUS_PROCESS_CRASHED:
1655 case base::TERMINATION_STATUS_ABNORMAL_TERMINATION:
1656 case base::TERMINATION_STATUS_PROCESS_WAS_KILLED:
1657 case base::TERMINATION_STATUS_OOM:
1658 case base::TERMINATION_STATUS_LAUNCH_FAILED:
oshima620225722015-06-04 19:45:271659#if defined(OS_CHROMEOS)
Dominic Mazzoni73a48322018-05-25 18:14:071660 case base::TERMINATION_STATUS_PROCESS_WAS_KILLED_BY_OOM:
oshima620225722015-06-04 19:45:271661#endif
Dominic Mazzoni73a48322018-05-25 18:14:071662#if defined(OS_ANDROID)
1663 case base::TERMINATION_STATUS_OOM_PROTECTED:
1664#endif
Will Harris07925d12019-10-31 03:03:051665#if defined(OS_WIN)
1666 case base::TERMINATION_STATUS_INTEGRITY_FAILURE:
1667#endif
Dominic Mazzoni73a48322018-05-25 18:14:071668 return true;
1669 case base::TERMINATION_STATUS_NORMAL_TERMINATION:
1670 case base::TERMINATION_STATUS_STILL_RUNNING:
1671 return false;
1672 case base::TERMINATION_STATUS_MAX_ENUM:
1673 NOTREACHED();
1674 return false;
1675 }
1676
1677 NOTREACHED();
1678 return false;
[email protected]3c9e1872010-11-18 16:17:491679}
1680
[email protected]b172aee2012-04-10 17:05:261681void WebContentsImpl::SetIsCrashed(base::TerminationStatus status,
1682 int error_code) {
[email protected]443b80e2010-12-14 00:42:231683 if (status == crashed_status_)
[email protected]d5f942ba2008-09-26 19:30:341684 return;
1685
[email protected]443b80e2010-12-14 00:42:231686 crashed_status_ = status;
1687 crashed_error_code_ = error_code;
[email protected]8ff00d72012-10-23 19:12:211688 NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB);
[email protected]d5f942ba2008-09-26 19:30:341689}
1690
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411691base::TerminationStatus WebContentsImpl::GetCrashedStatus() {
[email protected]be1f56ab2011-12-22 06:55:311692 return crashed_status_;
1693}
1694
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411695int WebContentsImpl::GetCrashedErrorCode() {
afakhry98241832016-03-11 19:27:471696 return crashed_error_code_;
1697}
1698
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411699bool WebContentsImpl::IsBeingDestroyed() {
[email protected]be1f56ab2011-12-22 06:55:311700 return is_being_destroyed_;
1701}
1702
[email protected]7f924832014-08-09 05:57:221703void WebContentsImpl::NotifyNavigationStateChanged(
1704 InvalidateTypes changed_flags) {
dalecurtis88c240072015-12-09 02:11:181705 // Notify the media observer of potential audibility changes.
Collin Bakeraf540cf2019-09-20 20:54:161706 if (changed_flags & INVALIDATE_TYPE_AUDIO) {
mlamouri44e4ef42016-01-20 18:42:271707 media_web_contents_observer_->MaybeUpdateAudibleState();
dalecurtisbc6572e12014-09-12 19:22:301708 }
1709
[email protected]d5f942ba2008-09-26 19:30:341710 if (delegate_)
1711 delegate_->NavigationStateChanged(this, changed_flags);
wjmaclean1dabf3e32016-05-13 23:34:441712
1713 if (GetOuterWebContents())
1714 GetOuterWebContents()->NotifyNavigationStateChanged(changed_flags);
[email protected]d5f942ba2008-09-26 19:30:341715}
1716
W. James MacLean6f34b042019-07-12 19:25:301717RenderFrameHostImpl* WebContentsImpl::GetFocusedFrameFromFocusedDelegate() {
1718 FrameTreeNode* focused_node =
1719 GetFocusedWebContents()->frame_tree_.GetFocusedFrame();
1720 return focused_node ? focused_node->current_frame_host() : nullptr;
1721}
1722
David Black9cca3592019-11-06 23:02:221723void WebContentsImpl::OnVerticalScrollDirectionChanged(
1724 viz::VerticalScrollDirection scroll_direction) {
1725 for (auto& observer : observers_)
1726 observer.DidChangeVerticalScrollDirection(scroll_direction);
1727}
1728
Chris Hamiltondf0d72cd2018-05-29 16:23:531729void WebContentsImpl::OnAudioStateChanged() {
1730 // This notification can come from any embedded contents or from this
1731 // WebContents' stream monitor. Aggregate these signals to get the actual
1732 // state.
Jeremy Roman03ce13ce2020-05-29 22:16:571733 //
1734 // Note that guests may not be attached as inner contents, and so may need to
1735 // be checked separately.
Chris Hamiltondf0d72cd2018-05-29 16:23:531736 bool is_currently_audible =
1737 audio_stream_monitor_.IsCurrentlyAudible() ||
1738 (browser_plugin_embedder_ &&
Jeremy Roman03ce13ce2020-05-29 22:16:571739 browser_plugin_embedder_->AreAnyGuestsCurrentlyAudible()) ||
1740 AnyInnerWebContents(this, [](WebContents* inner_contents) {
1741 return inner_contents->IsCurrentlyAudible();
1742 });
Chris Hamiltondf0d72cd2018-05-29 16:23:531743 if (is_currently_audible == is_currently_audible_)
1744 return;
1745
1746 // Update internal state.
1747 is_currently_audible_ = is_currently_audible;
1748 was_ever_audible_ = was_ever_audible_ || is_currently_audible_;
1749
1750 SendPageMessage(
1751 new PageMsg_AudioStateChanged(MSG_ROUTING_NONE, is_currently_audible_));
altimind8bd26c2016-11-04 11:44:541752
altiminec87fd1f2016-11-17 20:22:491753 // Notification for UI updates in response to the changed audio state.
Collin Bakeraf540cf2019-09-20 20:54:161754 NotifyNavigationStateChanged(INVALIDATE_TYPE_AUDIO);
Jan Rucka442c83f2017-08-08 13:27:541755
Chris Hamiltondf0d72cd2018-05-29 16:23:531756 // Ensure that audio state changes propagate from innermost to outermost
1757 // WebContents.
1758 if (GetOuterWebContents())
1759 GetOuterWebContents()->OnAudioStateChanged();
Tommy Steimel18360512017-11-01 00:38:191760
Chris Hamiltoneb41198f2018-05-24 18:41:261761 for (auto& observer : observers_)
Chris Hamiltondf0d72cd2018-05-29 16:23:531762 observer.OnAudioStateChanged(is_currently_audible_);
altimind8bd26c2016-11-04 11:44:541763}
1764
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411765base::TimeTicks WebContentsImpl::GetLastActiveTime() {
[email protected]9a890452014-02-13 22:21:021766 return last_active_time_;
[email protected]3e324142012-06-25 18:26:331767}
1768
[email protected]9e2e4632012-07-27 16:38:411769void WebContentsImpl::WasShown() {
Collin Baker989e0882019-11-01 01:27:171770 UpdateVisibilityAndNotifyPageAndView(Visibility::VISIBLE);
[email protected]be1f56ab2011-12-22 06:55:311771}
1772
[email protected]b172aee2012-04-10 17:05:261773void WebContentsImpl::WasHidden() {
Collin Baker989e0882019-11-01 01:27:171774 UpdateVisibilityAndNotifyPageAndView(Visibility::HIDDEN);
[email protected]375fa1b2012-05-22 22:05:371775}
1776
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411777bool WebContentsImpl::HasRecentInteractiveInputEvent() {
Daniel Cheng90196c82018-04-25 21:49:141778 static constexpr base::TimeDelta kMaxInterval =
1779 base::TimeDelta::FromSeconds(5);
1780 base::TimeDelta delta =
1781 ui::EventTimeForNow() - last_interactive_input_event_time_;
1782 // Note: the expectation is that the caller is typically expecting an input
1783 // event, e.g. validating that a WebUI message that requires a gesture is
Daniel Chenge81030b32019-07-17 20:20:231784 // actually attached to a gesture.
Daniel Cheng90196c82018-04-25 21:49:141785 return delta <= kMaxInterval;
1786}
1787
Avi Drissman738ea192018-08-29 20:24:161788void WebContentsImpl::SetIgnoreInputEvents(bool ignore_input_events) {
1789 ignore_input_events_ = ignore_input_events;
1790}
1791
Bo Liu168c8642017-08-28 18:26:021792#if defined(OS_ANDROID)
Bo Liu16dd7b8322018-05-02 21:19:501793void WebContentsImpl::SetMainFrameImportance(
1794 ChildProcessImportance importance) {
1795 GetMainFrame()->GetRenderWidgetHost()->SetImportance(importance);
Bo Liu7c6779e92017-08-16 02:02:281796}
Bo Liu168c8642017-08-28 18:26:021797#endif
Bo Liu7c6779e92017-08-16 02:02:281798
ccameron8807c38f2015-01-17 00:29:191799void WebContentsImpl::WasOccluded() {
Collin Baker989e0882019-11-01 01:27:171800 UpdateVisibilityAndNotifyPageAndView(Visibility::OCCLUDED);
ccameron8807c38f2015-01-17 00:29:191801}
1802
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411803Visibility WebContentsImpl::GetVisibility() {
Francois Doraye6161152018-03-27 22:05:371804 return visibility_;
Francois Dorayfe4a1772018-02-17 04:17:091805}
1806
Alex Moshchuk5bcd7c12019-11-12 22:55:311807bool WebContentsImpl::NeedToFireBeforeUnloadOrUnload() {
Alexander Timinc7bee322020-05-19 17:54:341808 if (!notify_disconnection_)
Alex Moshchuk6fcaca752018-07-14 02:13:591809 return false;
1810
1811 // Don't fire if the main frame's RenderViewHost indicates that beforeunload
1812 // and unload have already executed (e.g., after receiving a ClosePage ACK)
1813 // or should be ignored.
1814 if (GetRenderViewHost()->SuddenTerminationAllowed())
1815 return false;
1816
Alex Moshchukc78dbc722019-10-31 20:00:501817 // Check whether any frame in the frame tree needs to run beforeunload or
1818 // unload handlers.
1819 for (FrameTreeNode* node : frame_tree_.Nodes()) {
1820 RenderFrameHostImpl* rfh = node->current_frame_host();
1821
1822 // No need to run beforeunload/unload if the RenderFrame isn't live.
1823 if (!rfh->IsRenderFrameLive())
1824 continue;
1825
Dave Tapuska97d22ab2019-12-03 17:56:361826 if (rfh->GetSuddenTerminationDisablerState(
1827 blink::mojom::SuddenTerminationDisablerType::
1828 kBeforeUnloadHandler) ||
1829 rfh->GetSuddenTerminationDisablerState(
1830 blink::mojom::SuddenTerminationDisablerType::kUnloadHandler)) {
Alex Moshchukc78dbc722019-10-31 20:00:501831 return true;
1832 }
Aditya Keerthi34f37e62019-03-08 15:54:531833 }
Alex Moshchuk6fcaca752018-07-14 02:13:591834
Alex Moshchukc78dbc722019-10-31 20:00:501835 return false;
[email protected]be1f56ab2011-12-22 06:55:311836}
1837
Chris Hamilton3b3e315d2018-09-19 13:16:211838void WebContentsImpl::DispatchBeforeUnload(bool auto_cancel) {
1839 auto before_unload_type =
1840 auto_cancel ? RenderFrameHostImpl::BeforeUnloadType::DISCARD
1841 : RenderFrameHostImpl::BeforeUnloadType::TAB_CLOSE;
1842 GetMainFrame()->DispatchBeforeUnload(before_unload_type, false);
[email protected]1c3f80bd2014-04-08 23:02:061843}
1844
W. James MacLean2539adb32019-12-13 00:40:441845bool WebContentsImpl::IsInnerWebContentsForGuest() {
1846 return !!browser_plugin_guest_;
1847}
1848
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:501849void WebContentsImpl::AttachInnerWebContents(
1850 std::unique_ptr<WebContents> inner_web_contents,
W. James MacLean62883982019-11-12 20:39:001851 RenderFrameHost* render_frame_host,
1852 bool is_full_page) {
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:501853 WebContentsImpl* inner_web_contents_impl =
1854 static_cast<WebContentsImpl*>(inner_web_contents.get());
1855 DCHECK(!inner_web_contents_impl->node_.outer_web_contents());
1856 auto* render_frame_host_impl =
1857 static_cast<RenderFrameHostImpl*>(render_frame_host);
Fergal Daly445af932020-06-02 06:37:231858 DCHECK_EQ(&frame_tree_, render_frame_host_impl->frame_tree());
Lucas Furukawa Gadani105de1e812018-05-31 19:38:391859
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:501860 RenderFrameHostManager* inner_render_manager =
1861 inner_web_contents_impl->GetRenderManager();
1862 RenderFrameHostImpl* inner_main_frame =
1863 inner_render_manager->current_frame_host();
1864 RenderViewHostImpl* inner_render_view_host =
1865 inner_render_manager->current_host();
1866 auto* outer_render_manager =
1867 render_frame_host_impl->frame_tree_node()->render_manager();
lfg91a79e12016-04-01 23:52:191868
Lucas Furukawa Gadani105de1e812018-05-31 19:38:391869 // When attaching a WebContents as an inner WebContents, we need to replace
1870 // the Webcontents' view with a WebContentsViewChildFrame.
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:501871 inner_web_contents_impl->view_.reset(new WebContentsViewChildFrame(
1872 inner_web_contents_impl,
1873 GetContentClient()->browser()->GetWebContentsViewDelegate(
1874 inner_web_contents_impl),
1875 &inner_web_contents_impl->render_view_host_delegate_view_));
Lucas Furukawa Gadani105de1e812018-05-31 19:38:391876
lfg91a79e12016-04-01 23:52:191877 // When the WebContents being initialized has an opener, the browser side
1878 // Render{View,Frame}Host must be initialized and the RenderWidgetHostView
1879 // created. This is needed because the usual initialization happens during
1880 // the first navigation, but when attaching a new window we don't navigate
1881 // before attaching. If the browser side is already initialized, the calls
1882 // below will just early return.
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:501883 inner_render_manager->InitRenderView(inner_render_view_host, nullptr);
1884 inner_main_frame->Init();
1885 if (!inner_render_manager->GetRenderWidgetHostView()) {
1886 inner_web_contents_impl->CreateRenderWidgetHostViewForRenderManager(
1887 inner_render_view_host);
1888 }
lfg91a79e12016-04-01 23:52:191889
Adithya Srinivasane6e7f842019-12-16 18:41:361890 inner_web_contents_impl->RecursivelyUnregisterFrameSinkIds();
1891
lazyboy6ec48b2a2015-06-29 15:18:141892 // Create a link to our outer WebContents.
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:501893 node_.AttachInnerWebContents(std::move(inner_web_contents),
1894 render_frame_host_impl);
lazyboy6ec48b2a2015-06-29 15:18:141895
lazyboy6ec48b2a2015-06-29 15:18:141896 // Create a proxy in top-level RenderFrameHostManager, pointing to the
1897 // SiteInstance of the outer WebContents. The proxy will be used to send
1898 // postMessage to the inner WebContents.
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:501899 auto* proxy = inner_render_manager->CreateOuterDelegateProxy(
1900 render_frame_host_impl->GetSiteInstance());
lazyboy6ec48b2a2015-06-29 15:18:141901
Lucas Furukawa Gadani99125822019-01-03 15:41:491902 // When attaching a GuestView as an inner WebContents, there should already be
1903 // a live RenderFrame, which has to be swapped. When attaching a portal, there
1904 // will not be a live RenderFrame before creating the proxy.
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:501905 if (render_frame_host_impl->IsRenderFrameLive()) {
1906 inner_render_manager->SwapOuterDelegateFrame(render_frame_host_impl, proxy);
Lucas Furukawa Gadani99125822019-01-03 15:41:491907
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:501908 inner_web_contents_impl->ReattachToOuterWebContentsFrame();
Lucas Furukawa Gadani99125822019-01-03 15:41:491909 }
ekaramadadd882292016-06-08 15:22:561910
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:501911 if (frame_tree_.GetFocusedFrame() ==
1912 render_frame_host_impl->frame_tree_node()) {
1913 inner_web_contents_impl->SetFocusedFrame(
1914 inner_web_contents_impl->frame_tree_.root(),
1915 render_frame_host_impl->GetSiteInstance());
avallee7529b2a2017-04-26 11:37:511916 }
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:501917 outer_render_manager->set_attach_complete();
W. James MacLean62883982019-11-12 20:39:001918
1919 // If the inner WebContents is full frame, give it focus.
1920 if (is_full_page) {
1921 // There should only ever be one inner WebContents when |is_full_page| is
1922 // true, and it is the one we just attached.
1923 DCHECK_EQ(1u, node_.GetInnerWebContents().size());
1924 inner_web_contents_impl->SetAsFocusedWebContentsIfNecessary();
1925 }
Chris Hamilton16b5d1d2020-06-04 00:08:121926
1927 for (auto& observer : observers_) {
1928 observer.InnerWebContentsAttached(inner_web_contents_impl,
1929 render_frame_host, is_full_page);
1930 }
lazyboy6ec48b2a2015-06-29 15:18:141931}
1932
Lucas Furukawa Gadani99125822019-01-03 15:41:491933std::unique_ptr<WebContents> WebContentsImpl::DetachFromOuterWebContents() {
Chris Hamilton16b5d1d2020-06-04 00:08:121934 auto* outer_web_contents = GetOuterWebContents();
1935 DCHECK(outer_web_contents);
1936
Adithya Srinivasane6e7f842019-12-16 18:41:361937 RecursivelyUnregisterFrameSinkIds();
Lucas Furukawa Gadani99125822019-01-03 15:41:491938 if (RenderWidgetHostViewBase* view =
1939 static_cast<RenderWidgetHostViewBase*>(GetMainFrame()->GetView())) {
Kevin McNee6471a702020-01-27 18:23:071940 DCHECK(view->IsRenderWidgetHostViewChildFrame());
Lucas Furukawa Gadani99125822019-01-03 15:41:491941 view->Destroy();
1942 }
Kevin McNee6471a702020-01-27 18:23:071943 if (GetPendingMainFrame()) {
1944 if (RenderWidgetHostViewBase* view = static_cast<RenderWidgetHostViewBase*>(
1945 GetPendingMainFrame()->GetView())) {
1946 DCHECK(view->IsRenderWidgetHostViewChildFrame());
1947 view->Destroy();
1948 }
1949 }
Lucas Furukawa Gadani99125822019-01-03 15:41:491950 GetRenderManager()->DeleteOuterDelegateProxy(
1951 node_.OuterContentsFrameTreeNode()
1952 ->current_frame_host()
1953 ->GetSiteInstance());
1954 view_.reset(CreateWebContentsView(
1955 this, GetContentClient()->browser()->GetWebContentsViewDelegate(this),
1956 &render_view_host_delegate_view_));
danakjfc5184932019-09-12 18:08:321957 view_->CreateView(nullptr);
Lucas Furukawa Gadani99125822019-01-03 15:41:491958 std::unique_ptr<WebContents> web_contents =
1959 node_.DisconnectFromOuterWebContents();
1960 DCHECK_EQ(web_contents.get(), this);
1961 node_.SetFocusedWebContents(this);
1962 CreateRenderWidgetHostViewForRenderManager(GetRenderViewHost());
Kevin McNee6471a702020-01-27 18:23:071963 if (GetPendingMainFrame()) {
1964 CreateRenderWidgetHostViewForRenderManager(
1965 GetPendingMainFrame()->render_view_host());
1966 }
Adithya Srinivasane6e7f842019-12-16 18:41:361967 RecursivelyRegisterFrameSinkIds();
Adithya Srinivasan6f0f67b2019-12-19 01:26:461968 // TODO(adithyas): |browser_plugin_embedder_ax_tree_id| should either not be
1969 // used for portals, or it should get a different name.
1970 GetMainFrame()->set_browser_plugin_embedder_ax_tree_id(ui::AXTreeIDUnknown());
1971 GetMainFrame()->UpdateAXTreeData();
Chris Hamilton16b5d1d2020-06-04 00:08:121972
1973 // Invoke on the *outer* web contents observers for symmetry.
1974 for (auto& observer : outer_web_contents->observers_)
1975 observer.InnerWebContentsDetached(this);
1976
Lucas Furukawa Gadani99125822019-01-03 15:41:491977 return web_contents;
1978}
1979
W. James MacLean2dc75ed42019-03-06 14:31:091980void WebContentsImpl::RecursivelyRegisterFrameSinkIds() {
Adithya Srinivasane6e7f842019-12-16 18:41:361981 for (auto* view : GetRenderWidgetHostViewsInWebContentsTree()) {
1982 auto* rwhvb = static_cast<RenderWidgetHostViewBase*>(view);
1983 if (rwhvb->IsRenderWidgetHostViewChildFrame())
1984 static_cast<RenderWidgetHostViewChildFrame*>(view)->RegisterFrameSinkId();
1985 }
1986}
W. James MacLean2dc75ed42019-03-06 14:31:091987
Adithya Srinivasane6e7f842019-12-16 18:41:361988void WebContentsImpl::RecursivelyUnregisterFrameSinkIds() {
1989 for (auto* view : GetRenderWidgetHostViewsInWebContentsTree()) {
1990 auto* rwhvb = static_cast<RenderWidgetHostViewBase*>(view);
1991 if (rwhvb->IsRenderWidgetHostViewChildFrame()) {
1992 static_cast<RenderWidgetHostViewChildFrame*>(view)
1993 ->UnregisterFrameSinkId();
1994 }
W. James MacLean2dc75ed42019-03-06 14:31:091995 }
1996}
1997
Lucas Furukawa Gadaniaed1fed2017-10-13 17:34:141998void WebContentsImpl::ReattachToOuterWebContentsFrame() {
1999 DCHECK(node_.outer_web_contents());
2000 auto* render_manager = GetRenderManager();
2001 auto* parent_frame =
2002 node_.OuterContentsFrameTreeNode()->current_frame_host()->GetParent();
2003 render_manager->SetRWHViewForInnerContents(
2004 render_manager->GetRenderWidgetHostView());
2005
W. James MacLean2dc75ed42019-03-06 14:31:092006 RecursivelyRegisterFrameSinkIds();
Lucas Furukawa Gadaniaed1fed2017-10-13 17:34:142007
2008 // Set up the the guest's AX tree to point back at the embedder's AX tree.
2009 GetMainFrame()->set_browser_plugin_embedder_ax_tree_id(
2010 parent_frame->GetAXTreeID());
2011 GetMainFrame()->UpdateAXTreeData();
2012}
2013
Lucas Gadani972985622020-05-28 20:52:512014void WebContentsImpl::DidActivatePortal(
2015 WebContentsImpl* predecessor_web_contents,
2016 base::TimeTicks activation_time) {
2017 DCHECK(predecessor_web_contents);
Kevin McNeef8eb64c2020-04-21 21:36:062018 NotifyInsidePortal(false);
Kevin McNeef8eb64c2020-04-21 21:36:062019 for (auto& observer : observers_)
Lucas Gadani972985622020-05-28 20:52:512020 observer.DidActivatePortal(predecessor_web_contents, activation_time);
2021
Lucas Gadani972985622020-05-28 20:52:512022 GetDelegate()->WebContentsBecamePortal(predecessor_web_contents);
Kevin McNeef8eb64c2020-04-21 21:36:062023}
2024
Adithya Srinivasan11af2c52019-12-16 22:52:552025void WebContentsImpl::NotifyInsidePortal(bool inside_portal) {
2026 SendPageMessage(new PageMsg_SetInsidePortal(MSG_ROUTING_NONE, inside_portal));
2027}
2028
dalecurtis6c58ed02016-10-28 23:02:372029void WebContentsImpl::DidChangeVisibleSecurityState() {
Emily Starkc663b0242019-04-26 19:34:452030 if (delegate_)
dalecurtis6c58ed02016-10-28 23:02:372031 delegate_->VisibleSecurityStateChanged(this);
Emily Starkc663b0242019-04-26 19:34:452032 for (auto& observer : observers_)
2033 observer.DidChangeVisibleSecurityState();
dalecurtis6c58ed02016-10-28 23:02:372034}
2035
Ahmed Fakhryd70bf37b2018-04-04 17:07:242036void WebContentsImpl::NotifyPreferencesChanged() {
2037 std::set<RenderViewHost*> render_view_host_set;
2038 for (FrameTreeNode* node : frame_tree_.Nodes()) {
Giovanni Ortuño Urquidi17a5d7c2018-04-30 06:48:532039 render_view_host_set.insert(
2040 node->current_frame_host()->GetRenderViewHost());
Ahmed Fakhryd70bf37b2018-04-04 17:07:242041 }
Giovanni Ortuño Urquidi17a5d7c2018-04-30 06:48:532042
Ahmed Fakhryd70bf37b2018-04-04 17:07:242043 for (RenderViewHost* render_view_host : render_view_host_set)
2044 render_view_host->OnWebkitPreferencesChanged();
2045}
2046
Bruce Long1e3e1f542019-10-16 17:56:282047void WebContentsImpl::SyncRendererPrefs() {
2048 blink::mojom::RendererPreferences renderer_preferences =
2049 GetRendererPrefs(GetBrowserContext());
2050 RenderViewHostImpl::GetPlatformSpecificPrefs(&renderer_preferences);
2051 SendPageMessage(
2052 new PageMsg_SetRendererPrefs(MSG_ROUTING_NONE, renderer_preferences));
2053}
2054
Alexander Timin1cc31f42020-05-12 16:26:012055void WebContentsImpl::OnCookiesAccessed(NavigationHandle* navigation,
2056 const CookieAccessDetails& details) {
Josh Karlindaba39322019-07-17 23:24:332057 for (auto& observer : observers_) {
Alexander Timin1cc31f42020-05-12 16:26:012058 observer.OnCookiesAccessed(navigation, details);
2059 }
2060}
2061
2062void WebContentsImpl::OnCookiesAccessed(RenderFrameHostImpl* rfh,
2063 const CookieAccessDetails& details) {
2064 for (auto& observer : observers_) {
2065 observer.OnCookiesAccessed(rfh, details);
Josh Karlindaba39322019-07-17 23:24:332066 }
2067}
2068
[email protected]b172aee2012-04-10 17:05:262069void WebContentsImpl::Stop() {
dcheng57e39e22016-01-21 00:25:382070 for (FrameTreeNode* node : frame_tree_.Nodes())
2071 node->StopLoading();
ericwilligersde386342016-10-19 02:35:092072 for (auto& observer : observers_)
2073 observer.NavigationStopped();
[email protected]0bfbf882011-12-22 18:19:272074}
2075
Francois Doray47f759d2018-06-11 18:13:512076void WebContentsImpl::SetPageFrozen(bool frozen) {
2077 // A visible page is never frozen.
2078 DCHECK_NE(Visibility::VISIBLE, GetVisibility());
2079
Yuzu Saijo4fa1e9a2020-05-20 02:53:262080 for (auto& entry : frame_tree_.render_view_hosts()) {
2081 entry.second->SetIsFrozen(frozen);
2082 }
Fadi Meawada6573e02018-03-10 00:52:112083}
2084
erikchen6c7df7f7a2018-05-03 18:13:592085std::unique_ptr<WebContents> WebContentsImpl::Clone() {
[email protected]14392a52012-05-02 20:28:442086 // We use our current SiteInstance since the cloned entry will use it anyway.
alexmose201c7cd2015-06-10 17:14:212087 // We pass our own opener so that the cloned page can access it if it was set
[email protected]ed245db2012-12-18 08:00:452088 // before.
[email protected]54944cde2012-12-09 09:24:592089 CreateParams create_params(GetBrowserContext(), GetSiteInstance());
Ian Clelland5cbaaf82017-11-27 22:00:032090 FrameTreeNode* opener = frame_tree_.root()->opener();
2091 RenderFrameHostImpl* opener_rfh = nullptr;
2092 if (opener)
2093 opener_rfh = opener->current_frame_host();
erikchen6c7df7f7a2018-05-03 18:13:592094 std::unique_ptr<WebContentsImpl> tc =
erikchen50735fd2018-05-05 15:08:332095 CreateWithOpener(create_params, opener_rfh);
Lucas Furukawa Gadani5553a152019-01-08 18:55:572096 tc->GetController().CopyStateFrom(&controller_, true);
ericwilligersde386342016-10-19 02:35:092097 for (auto& observer : observers_)
erikchen6c7df7f7a2018-05-03 18:13:592098 observer.DidCloneToNewWebContents(this, tc.get());
[email protected]0bfbf882011-12-22 18:19:272099 return tc;
2100}
2101
[email protected]14392a52012-05-02 20:28:442102void WebContentsImpl::Observe(int type,
[email protected]8ff00d72012-10-23 19:12:212103 const NotificationSource& source,
2104 const NotificationDetails& details) {
[email protected]14392a52012-05-02 20:28:442105 switch (type) {
[email protected]8ff00d72012-10-23 19:12:212106 case NOTIFICATION_RENDER_WIDGET_HOST_DESTROYED: {
2107 RenderWidgetHost* host = Source<RenderWidgetHost>(source).ptr();
[email protected]0b431992014-06-24 00:08:032108 RenderWidgetHostView* view = host->GetView();
2109 if (view == GetFullscreenRenderWidgetHostView()) {
2110 // We cannot just call view_->RestoreFocus() here. On some platforms,
2111 // attempting to focus the currently-invisible WebContentsView will be
2112 // flat-out ignored. Therefore, this boolean is used to track whether
2113 // we will request focus after the fullscreen widget has been
2114 // destroyed.
2115 fullscreen_widget_had_focus_at_shutdown_ = (view && view->HasFocus());
2116 } else {
alexmosc2a8cec2016-05-23 22:19:532117 for (auto i = pending_widget_views_.begin();
[email protected]0b431992014-06-24 00:08:032118 i != pending_widget_views_.end(); ++i) {
2119 if (host->GetView() == i->second) {
2120 pending_widget_views_.erase(i);
2121 break;
2122 }
[email protected]bafe6cd2012-05-23 23:09:502123 }
2124 }
2125 break;
2126 }
[email protected]14392a52012-05-02 20:28:442127 default:
2128 NOTREACHED();
2129 }
2130}
2131
[email protected]ec6c05f2013-10-23 18:41:572132WebContents* WebContentsImpl::GetWebContents() {
2133 return this;
2134}
2135
[email protected]54944cde2012-12-09 09:24:592136void WebContentsImpl::Init(const WebContents::CreateParams& params) {
[email protected]fa944cb82013-11-15 17:51:212137 // This is set before initializing the render manager since
[email protected]b0936d22013-11-28 06:47:362138 // RenderFrameHostManager::Init calls back into us via its delegate to ask if
[email protected]fa944cb82013-11-15 17:51:212139 // it should be hidden.
Francois Doraye6161152018-03-27 22:05:372140 visibility_ =
2141 params.initially_hidden ? Visibility::HIDDEN : Visibility::VISIBLE;
[email protected]d6fa88f2013-10-18 16:00:432142
Sebastien Marchand28cf23182018-06-20 02:39:352143 if (!params.last_active_time.is_null())
2144 last_active_time_ = params.last_active_time;
2145
dcheng3ce04b62015-10-26 23:30:552146 scoped_refptr<SiteInstance> site_instance = params.site_instance;
2147 if (!site_instance)
2148 site_instance = SiteInstance::Create(params.browser_context);
Lukasz Anforowicz3caa8372018-06-05 17:22:072149 if (params.desired_renderer_state == CreateParams::kNoRendererProcess) {
2150 static_cast<SiteInstanceImpl*>(site_instance.get())
2151 ->PreventAssociationWithSpareProcess();
2152 }
dcheng3ce04b62015-10-26 23:30:552153
Fergal Dalycb0c1fdf2020-03-16 07:13:492154 GetRenderManager()->InitRoot(site_instance.get(),
2155 params.renderer_initiated_creation);
lukasza464d8692016-02-22 19:26:322156
lukasza5140a412016-09-15 21:12:302157 // blink::FrameTree::setName always keeps |unique_name| empty in case of a
2158 // main frame - let's do the same thing here.
2159 std::string unique_name;
lukasza464d8692016-02-22 19:26:322160 frame_tree_.root()->SetFrameName(params.main_frame_name, unique_name);
[email protected]d1198fd2012-08-13 22:50:192161
[email protected]fc2b46b2014-05-03 16:33:452162 WebContentsViewDelegate* delegate =
2163 GetContentClient()->browser()->GetWebContentsViewDelegate(this);
2164
W. James MacLean2539adb32019-12-13 00:40:442165 if (browser_plugin_guest_) {
sky52a39e342016-12-17 17:23:222166 view_.reset(new WebContentsViewChildFrame(
2167 this, delegate, &render_view_host_delegate_view_));
2168 } else {
2169 view_.reset(CreateWebContentsView(this, delegate,
2170 &render_view_host_delegate_view_));
[email protected]d1198fd2012-08-13 22:50:192171 }
[email protected]fc2b46b2014-05-03 16:33:452172 CHECK(render_view_host_delegate_view_);
[email protected]d1198fd2012-08-13 22:50:192173 CHECK(view_.get());
2174
danakjfc5184932019-09-12 18:08:322175 view_->CreateView(params.context);
[email protected]d1198fd2012-08-13 22:50:192176
brettw4b461082016-11-19 18:55:162177#if BUILDFLAG(ENABLE_PLUGINS)
Yuzu Saijoa11069a2020-04-22 10:56:552178 plugin_content_origin_allowlist_.reset(
2179 new PluginContentOriginAllowlist(this));
tommyclieb25b2a2014-11-03 19:45:092180#endif
2181
[email protected]d1198fd2012-08-13 22:50:192182 registrar_.Add(this,
[email protected]8ff00d72012-10-23 19:12:212183 NOTIFICATION_RENDER_WIDGET_HOST_DESTROYED,
2184 NotificationService::AllBrowserContextsAndSources());
[email protected]f28ef9a32014-05-12 16:36:102185
leon.hane4db177a2017-02-07 14:19:162186 screen_orientation_provider_.reset(new ScreenOrientationProvider(this));
[email protected]01f83f92014-06-17 14:41:542187
mfomitchev2b8b066a2016-01-28 19:23:152188#if defined(OS_ANDROID)
Oksana Zhuravlova0b634332019-10-28 23:04:222189 DateTimeChooserAndroid::CreateForWebContents(this);
[email protected]583418cc2013-01-17 14:01:102190#endif
fsamuel7310a4262014-12-05 05:06:442191
2192 // BrowserPluginGuest::Init needs to be called after this WebContents has
W. James MacLean6e78da342019-12-13 15:13:232193 // a RenderWidgetHostViewChildFrame. That is, |view_->CreateView| above.
fsamuel7310a4262014-12-05 05:06:442194 if (browser_plugin_guest_)
2195 browser_plugin_guest_->Init();
nickf9acfbe2014-12-23 19:12:372196
2197 for (size_t i = 0; i < g_created_callbacks.Get().size(); i++)
2198 g_created_callbacks.Get().at(i).Run(this);
naskob21fe4872015-02-24 14:15:562199
2200 // If the WebContents creation was renderer-initiated, it means that the
2201 // corresponding RenderView and main RenderFrame have already been created.
2202 // Ensure observers are notified about this.
2203 if (params.renderer_initiated_creation) {
Fergal Dalybb2d6592020-06-02 05:02:332204 GetRenderViewHost()->GetWidget()->set_renderer_initialized(true);
Alex Moshchuk27caae82017-09-11 23:11:182205 GetRenderViewHost()->DispatchRenderViewCreated();
naskob21fe4872015-02-24 14:15:562206 GetRenderManager()->current_frame_host()->SetRenderFrameCreated(true);
2207 }
naskof5940b9f2015-03-02 23:04:052208
lof84501da082016-05-23 21:22:542209 // Create the renderer process in advance if requested.
Lukasz Anforowicz3caa8372018-06-05 17:22:072210 if (params.desired_renderer_state ==
2211 CreateParams::kInitializeAndWarmupRendererProcess) {
lof84501da082016-05-23 21:22:542212 if (!GetRenderManager()->current_frame_host()->IsRenderFrameLive()) {
2213 GetRenderManager()->InitRenderView(GetRenderViewHost(), nullptr);
2214 }
2215 }
2216
naskof5940b9f2015-03-02 23:04:052217 // Ensure that observers are notified of the creation of this WebContents's
2218 // main RenderFrameHost. It must be done here for main frames, since the
2219 // NotifySwappedFromRenderManager expects view_ to already be created and that
2220 // happens after RenderFrameHostManager::Init.
2221 NotifySwappedFromRenderManager(
2222 nullptr, GetRenderManager()->current_frame_host(), true);
Albert J. Wonge76bf0b82019-09-27 07:47:062223
2224 // For WebContents that are never shown, do critical initialization here which
2225 // would normally only happen when the WebContents is shown.
2226 if (params.is_never_visible) {
2227 // This has just been created so there can only be one frame. Thus it is
2228 // safe to initialize the root.
2229 GetMainFrame()->Init();
2230 }
[email protected]d1198fd2012-08-13 22:50:192231}
2232
[email protected]7fff43e2013-05-21 20:21:102233void WebContentsImpl::OnWebContentsDestroyed(WebContentsImpl* web_contents) {
2234 RemoveDestructionObserver(web_contents);
2235
[email protected]ceee8cd2013-03-08 04:59:512236 // Clear a pending contents that has been closed before being shown.
alexmosc2a8cec2016-05-23 22:19:532237 for (auto iter = pending_contents_.begin(); iter != pending_contents_.end();
[email protected]ceee8cd2013-03-08 04:59:512238 ++iter) {
Joel Hockey8c2011b22020-04-30 05:07:192239 if (iter->second.contents.get() != web_contents)
[email protected]ceee8cd2013-03-08 04:59:512240 continue;
erikchenbee5c9622018-04-27 19:30:252241
2242 // Someone else has deleted the WebContents. That should never happen!
2243 // TODO(erikchen): Fix semantics here. https://crbug.com/832879.
Joel Hockey8c2011b22020-04-30 05:07:192244 iter->second.contents.release();
[email protected]ceee8cd2013-03-08 04:59:512245 pending_contents_.erase(iter);
[email protected]ceee8cd2013-03-08 04:59:512246 return;
2247 }
2248 NOTREACHED();
[email protected]14392a52012-05-02 20:28:442249}
2250
[email protected]7fff43e2013-05-21 20:21:102251void WebContentsImpl::AddDestructionObserver(WebContentsImpl* web_contents) {
Jan Wilken Dörrie73c901e2019-06-12 09:02:322252 if (!base::Contains(destruction_observers_, web_contents)) {
[email protected]7fff43e2013-05-21 20:21:102253 destruction_observers_[web_contents] =
Jeremy Roman04f27c372017-10-27 15:20:552254 std::make_unique<DestructionObserver>(this, web_contents);
[email protected]7fff43e2013-05-21 20:21:102255 }
2256}
2257
2258void WebContentsImpl::RemoveDestructionObserver(WebContentsImpl* web_contents) {
avia8cf81982016-08-12 20:28:462259 destruction_observers_.erase(web_contents);
[email protected]7fff43e2013-05-21 20:21:102260}
2261
[email protected]b172aee2012-04-10 17:05:262262void WebContentsImpl::AddObserver(WebContentsObserver* observer) {
[email protected]be1f56ab2011-12-22 06:55:312263 observers_.AddObserver(observer);
2264}
2265
[email protected]b172aee2012-04-10 17:05:262266void WebContentsImpl::RemoveObserver(WebContentsObserver* observer) {
[email protected]be1f56ab2011-12-22 06:55:312267 observers_.RemoveObserver(observer);
2268}
2269
[email protected]948481d2014-06-11 18:32:222270std::set<RenderWidgetHostView*>
2271WebContentsImpl::GetRenderWidgetHostViewsInTree() {
2272 std::set<RenderWidgetHostView*> set;
Carlos IL07f282f2020-05-15 22:58:292273 for (RenderFrameHost* rfh : GetAllFrames()) {
2274 if (RenderWidgetHostView* rwhv = static_cast<RenderFrameHostImpl*>(rfh)
2275 ->frame_tree_node()
2276 ->render_manager()
2277 ->GetRenderWidgetHostView()) {
lfgf949e0e2017-05-10 22:18:112278 set.insert(rwhv);
dchengafb53e22016-02-04 08:11:082279 }
[email protected]948481d2014-06-11 18:32:222280 }
[email protected]de3c5d82014-05-28 22:12:592281 return set;
2282}
2283
Adithya Srinivasane6e7f842019-12-16 18:41:362284std::set<RenderWidgetHostView*>
2285WebContentsImpl::GetRenderWidgetHostViewsInWebContentsTree() {
2286 std::set<RenderWidgetHostView*> result;
2287 GetRenderWidgetHostViewsInWebContentsTree(result);
2288 return result;
2289}
2290
2291void WebContentsImpl::GetRenderWidgetHostViewsInWebContentsTree(
2292 std::set<RenderWidgetHostView*>& result) {
2293 std::set<RenderWidgetHostView*> views = GetRenderWidgetHostViewsInTree();
2294 result.insert(views.begin(), views.end());
2295 for (auto* inner_web_contents : GetInnerWebContents()) {
2296 static_cast<WebContentsImpl*>(inner_web_contents)
2297 ->GetRenderWidgetHostViewsInWebContentsTree(result);
2298 }
2299}
2300
[email protected]b172aee2012-04-10 17:05:262301void WebContentsImpl::Activate() {
[email protected]d5f942ba2008-09-26 19:30:342302 if (delegate_)
2303 delegate_->ActivateContents(this);
2304}
2305
avi3e4b8512015-11-11 01:55:492306void WebContentsImpl::LostCapture(RenderWidgetHostImpl* render_widget_host) {
2307 if (!RenderViewHostImpl::From(render_widget_host))
2308 return;
2309
[email protected]63954792011-07-11 04:17:482310 if (delegate_)
2311 delegate_->LostCapture();
2312}
2313
Yue Ru Sun979c4b162019-11-06 16:11:042314ukm::SourceId
2315WebContentsImpl::GetUkmSourceIdForLastCommittedSourceIncludingSameDocument()
2316 const {
2317 return last_committed_source_id_including_same_document_;
2318}
2319
Ahmed Fakhryfaa32d22018-10-05 15:56:162320void WebContentsImpl::SetTopControlsShownRatio(
2321 RenderWidgetHostImpl* render_widget_host,
2322 float ratio) {
2323 if (!delegate_)
2324 return;
2325
2326 RenderFrameHostImpl* rfh = GetMainFrame();
2327 if (!rfh || render_widget_host != rfh->GetRenderWidgetHost())
2328 return;
2329
2330 delegate_->SetTopControlsShownRatio(this, ratio);
Ahmed Fakhry58e6ef542018-09-04 18:05:382331}
2332
Ahmed Fakhry58e6ef542018-09-04 18:05:382333void WebContentsImpl::SetTopControlsGestureScrollInProgress(bool in_progress) {
2334 if (delegate_)
2335 delegate_->SetTopControlsGestureScrollInProgress(in_progress);
2336}
2337
lazyboy63f5b312015-11-23 20:19:522338void WebContentsImpl::RenderWidgetCreated(
2339 RenderWidgetHostImpl* render_widget_host) {
2340 created_widgets_.insert(render_widget_host);
2341}
2342
[email protected]b24b68a2012-09-24 21:57:262343void WebContentsImpl::RenderWidgetDeleted(
2344 RenderWidgetHostImpl* render_widget_host) {
lazyboy63f5b312015-11-23 20:19:522345 // Note that |is_being_destroyed_| can be true at this point as
2346 // ~WebContentsImpl() calls RFHM::ClearRFHsPendingShutdown(), which might lead
2347 // us here.
2348 created_widgets_.erase(render_widget_host);
[email protected]b24b68a2012-09-24 21:57:262349
lazyboy63f5b312015-11-23 20:19:522350 if (is_being_destroyed_)
2351 return;
[email protected]44470a22013-01-24 01:21:542352
2353 if (render_widget_host &&
alexmosc9e76ec2016-05-16 22:59:372354 render_widget_host->GetRoutingID() == fullscreen_widget_routing_id_ &&
2355 render_widget_host->GetProcess()->GetID() ==
2356 fullscreen_widget_process_id_) {
[email protected]4aebbca2013-09-17 22:26:492357 if (delegate_ && delegate_->EmbedsFullscreenWidget())
mlamouri7a78d6fd2015-01-17 13:23:532358 delegate_->ExitFullscreenModeForTab(this);
ericwilligersde386342016-10-19 02:35:092359 for (auto& observer : observers_)
2360 observer.DidDestroyFullscreenWidget();
alexmosc9e76ec2016-05-16 22:59:372361 fullscreen_widget_process_id_ = ChildProcessHost::kInvalidUniqueID;
[email protected]44470a22013-01-24 01:21:542362 fullscreen_widget_routing_id_ = MSG_ROUTING_NONE;
[email protected]0b431992014-06-24 00:08:032363 if (fullscreen_widget_had_focus_at_shutdown_)
2364 view_->RestoreFocus();
[email protected]44470a22013-01-24 01:21:542365 }
lfgbee1e0a2016-06-08 21:24:212366
lfg7d4caad2017-03-22 09:01:452367 if (render_widget_host == mouse_lock_widget_)
lfgff8dfc8d2016-10-21 17:42:322368 LostMouseLock(mouse_lock_widget_);
Joe Downing192998b22018-03-22 15:51:362369
Joe Downingc1a57efe2018-05-09 18:23:542370 CancelKeyboardLock(render_widget_host);
[email protected]b24b68a2012-09-24 21:57:262371}
2372
miu9e14e492014-10-25 02:39:042373void WebContentsImpl::RenderWidgetGotFocus(
2374 RenderWidgetHostImpl* render_widget_host) {
calamity7fe55ce2015-04-10 03:59:372375 // Notify the observers if an embedded fullscreen widget was focused.
2376 if (delegate_ && render_widget_host && delegate_->EmbedsFullscreenWidget() &&
2377 render_widget_host->GetView() == GetFullscreenRenderWidgetHostView()) {
Michael Thiessen896405db2017-07-20 17:52:322378 NotifyWebContentsFocused(render_widget_host);
calamity7fe55ce2015-04-10 03:59:372379 }
miu9e14e492014-10-25 02:39:042380}
2381
zijiehe3bee08e22017-05-17 04:14:462382void WebContentsImpl::RenderWidgetLostFocus(
2383 RenderWidgetHostImpl* render_widget_host) {
Michael Thiessen896405db2017-07-20 17:52:322384 // Notify the observers if an embedded fullscreen widget lost focus.
2385 if (delegate_ && render_widget_host && delegate_->EmbedsFullscreenWidget() &&
2386 render_widget_host->GetView() == GetFullscreenRenderWidgetHostView()) {
2387 NotifyWebContentsLostFocus(render_widget_host);
2388 }
zijiehe3bee08e22017-05-17 04:14:462389}
2390
estaded0a0c992015-01-21 14:12:062391void WebContentsImpl::RenderWidgetWasResized(
rouslan2f5993f2015-01-29 00:18:312392 RenderWidgetHostImpl* render_widget_host,
2393 bool width_changed) {
creisc014b402015-04-23 16:41:452394 RenderFrameHostImpl* rfh = GetMainFrame();
estadece344ec2015-01-23 00:12:292395 if (!rfh || render_widget_host != rfh->GetRenderWidgetHost())
estaded0a0c992015-01-21 14:12:062396 return;
estaded0a0c992015-01-21 14:12:062397
ericwilligersde386342016-10-19 02:35:092398 for (auto& observer : observers_)
2399 observer.MainFrameWasResized(width_changed);
estaded0a0c992015-01-21 14:12:062400}
2401
skyf65d9bb2017-03-24 02:26:392402KeyboardEventProcessingResult WebContentsImpl::PreHandleKeyboardEvent(
2403 const NativeWebKeyboardEvent& event) {
W. James MacLeane1187272019-11-12 03:42:482404 auto* outermost_contents = GetOutermostWebContents();
2405 // TODO(wjmaclean): Generalize this to forward all key events to the outermost
2406 // delegate's handler.
2407 if (outermost_contents != this && IsFullscreenForCurrentTab() &&
2408 event.windows_key_code == ui::VKEY_ESCAPE) {
2409 // When an inner WebContents has focus and is fullscreen, redirect <esc>
2410 // key events to the outermost WebContents so it can be handled by that
2411 // WebContents' delegate.
2412 if (outermost_contents->PreHandleKeyboardEvent(event) ==
2413 KeyboardEventProcessingResult::HANDLED) {
2414 return KeyboardEventProcessingResult::HANDLED;
2415 }
2416 }
skyf65d9bb2017-03-24 02:26:392417 return delegate_ ? delegate_->PreHandleKeyboardEvent(this, event)
2418 : KeyboardEventProcessingResult::NOT_HANDLED;
[email protected]63954792011-07-11 04:17:482419}
2420
Dave Tapuska344399e2019-09-09 15:18:262421bool WebContentsImpl::HandleMouseEvent(const blink::WebMouseEvent& event) {
2422 // Handle mouse button back/forward in the browser process after the render
2423 // process is done with the event. This ensures all renderer-initiated history
2424 // navigations can be treated consistently.
2425 if (event.GetType() == blink::WebInputEvent::Type::kMouseUp) {
2426 WebContentsImpl* outermost = GetOutermostWebContents();
2427 if (event.button == blink::WebPointerProperties::Button::kBack &&
2428 outermost->controller_.CanGoBack()) {
2429 outermost->controller_.GoBack();
2430 return true;
2431 } else if (event.button == blink::WebPointerProperties::Button::kForward &&
2432 outermost->controller_.CanGoForward()) {
2433 outermost->controller_.GoForward();
2434 return true;
2435 }
2436 }
2437 return false;
2438}
2439
Scott Violetd5caa872018-10-16 22:00:512440bool WebContentsImpl::HandleKeyboardEvent(const NativeWebKeyboardEvent& event) {
[email protected]0b094002014-08-20 18:28:102441 if (browser_plugin_embedder_ &&
2442 browser_plugin_embedder_->HandleKeyboardEvent(event)) {
Scott Violetd5caa872018-10-16 22:00:512443 return true;
[email protected]0b094002014-08-20 18:28:102444 }
Scott Violetd5caa872018-10-16 22:00:512445 return delegate_ && delegate_->HandleKeyboardEvent(this, event);
[email protected]63954792011-07-11 04:17:482446}
2447
[email protected]d92026ef2014-03-03 21:04:132448bool WebContentsImpl::HandleWheelEvent(
[email protected]180ef242013-11-07 06:50:462449 const blink::WebMouseWheelEvent& event) {
[email protected]bccc4472013-04-18 16:37:192450#if !defined(OS_MACOSX)
lanweia93644f2015-01-21 22:00:332451 // On platforms other than Mac, control+mousewheel may change zoom. On Mac,
2452 // this isn't done for two reasons:
[email protected]bccc4472013-04-18 16:37:192453 // -the OS already has a gesture to do this through pinch-zoom
2454 // -if a user starts an inertial scroll, let's go, and presses control
2455 // (i.e. control+tab) then the OS's buffered scroll events will come in
2456 // with control key set which isn't what the user wants
Blink Reformat1c4d759e2017-04-09 16:34:542457 if (delegate_ && event.wheel_ticks_y &&
Ehsan Karamad0f83cb112018-09-06 22:07:362458 event.event_action == blink::WebMouseWheelEvent::EventAction::kPageZoom) {
w.shackleton49bcd392016-01-06 17:38:222459 // Count only integer cumulative scrolls as zoom events; this handles
2460 // smooth scroll and regular scroll device behavior.
Blink Reformat1c4d759e2017-04-09 16:34:542461 zoom_scroll_remainder_ += event.wheel_ticks_y;
w.shackleton49bcd392016-01-06 17:38:222462 int whole_zoom_scroll_remainder_ = std::lround(zoom_scroll_remainder_);
2463 zoom_scroll_remainder_ -= whole_zoom_scroll_remainder_;
2464 if (whole_zoom_scroll_remainder_ != 0) {
2465 delegate_->ContentsZoomChange(whole_zoom_scroll_remainder_ > 0);
2466 }
[email protected]fb3f066e2013-02-12 19:12:522467 return true;
2468 }
[email protected]bccc4472013-04-18 16:37:192469#endif
[email protected]fb3f066e2013-02-12 19:12:522470 return false;
2471}
2472
[email protected]04bce562014-01-30 05:34:542473bool WebContentsImpl::PreHandleGestureEvent(
2474 const blink::WebGestureEvent& event) {
[email protected]28042d32014-02-03 14:10:032475 return delegate_ && delegate_->PreHandleGestureEvent(this, event);
[email protected]04bce562014-01-30 05:34:542476}
2477
kenrb2a565f82015-09-02 20:24:592478RenderWidgetHostInputEventRouter* WebContentsImpl::GetInputEventRouter() {
wjmacleane31234f2015-12-14 17:20:452479 if (!is_being_destroyed_ && GetOuterWebContents())
2480 return GetOuterWebContents()->GetInputEventRouter();
2481
2482 if (!rwh_input_event_router_.get() && !is_being_destroyed_)
kenrb2a565f82015-09-02 20:24:592483 rwh_input_event_router_.reset(new RenderWidgetHostInputEventRouter);
2484 return rwh_input_event_router_.get();
2485}
2486
alexmos3fcd0ca2015-10-23 18:18:332487void WebContentsImpl::ReplicatePageFocus(bool is_focused) {
2488 // Focus loss may occur while this WebContents is being destroyed. Don't
2489 // send the message in this case, as the main frame's RenderFrameHost and
2490 // other state has already been cleared.
2491 if (is_being_destroyed_)
2492 return;
2493
2494 frame_tree_.ReplicatePageFocus(is_focused);
2495}
2496
alexmosc4cbacb2015-11-21 01:29:222497RenderWidgetHostImpl* WebContentsImpl::GetFocusedRenderWidgetHost(
2498 RenderWidgetHostImpl* receiving_widget) {
alexmosc4cbacb2015-11-21 01:29:222499 // Events for widgets other than the main frame (e.g., popup menus) should be
2500 // forwarded directly to the widget they arrived on.
2501 if (receiving_widget != GetMainFrame()->GetRenderWidgetHost())
2502 return receiving_widget;
alexmosc4d183e2015-11-06 00:46:522503
paulmeyerfeafc2d2017-04-25 21:46:402504 // If the focused WebContents is a guest WebContents, then get the focused
2505 // frame in the embedder WebContents instead.
W. James MacLean13d834d2019-12-04 16:06:452506 FrameTreeNode* focused_frame =
2507 GetFocusedWebContents()->frame_tree_.GetFocusedFrame();
lfg1453e412017-04-11 00:48:502508
alexmosc4d183e2015-11-06 00:46:522509 if (!focused_frame)
alexmosc4cbacb2015-11-21 01:29:222510 return receiving_widget;
alexmosc4d183e2015-11-06 00:46:522511
alexmos732ca3a2015-12-08 02:01:022512 // The view may be null if a subframe's renderer process has crashed while
2513 // the subframe has focus. Drop the event in that case. Do not give
2514 // it to the main frame, so that the user doesn't unexpectedly type into the
2515 // wrong frame if a focused subframe renderer crashes while they type.
2516 RenderWidgetHostView* view = focused_frame->current_frame_host()->GetView();
2517 if (!view)
2518 return nullptr;
2519
2520 return RenderWidgetHostImpl::From(view->GetRenderWidgetHost());
alexmosc4d183e2015-11-06 00:46:522521}
2522
avallee9993fca2016-11-17 06:16:502523RenderWidgetHostImpl* WebContentsImpl::GetRenderWidgetHostWithPageFocus() {
lfg1453e412017-04-11 00:48:502524 WebContentsImpl* focused_web_contents = GetFocusedWebContents();
2525
lfg1453e412017-04-11 00:48:502526 return focused_web_contents->GetMainFrame()->GetRenderWidgetHost();
avallee9993fca2016-11-17 06:16:502527}
2528
Avi Drissmand1d25652020-03-06 21:28:582529bool WebContentsImpl::CanEnterFullscreenMode() {
Avi Drissman1a55a9d62020-03-10 18:56:452530 return fullscreen_blocker_count_ == 0;
Avi Drissmand1d25652020-03-06 21:28:582531}
2532
Dave Tapuska3ed44192018-05-01 18:53:302533void WebContentsImpl::EnterFullscreenMode(
Mike Wasserman4ca09792020-05-29 17:44:432534 RenderFrameHost* requesting_frame,
Dave Tapuskaa4189512019-10-15 20:27:342535 const blink::mojom::FullscreenOptions& options) {
Avi Drissmand1d25652020-03-06 21:28:582536 DCHECK(CanEnterFullscreenMode());
2537
mlamouri7a78d6fd2015-01-17 13:23:532538 // This method is being called to enter renderer-initiated fullscreen mode.
2539 // Make sure any existing fullscreen widget is shut down first.
[email protected]4aebbca2013-09-17 22:26:492540 RenderWidgetHostView* const widget_view = GetFullscreenRenderWidgetHostView();
avi85a4cef2015-12-18 20:13:372541 if (widget_view) {
2542 RenderWidgetHostImpl::From(widget_view->GetRenderWidgetHost())
2543 ->ShutdownAndDestroyWidget(true);
2544 }
[email protected]4aebbca2013-09-17 22:26:492545
Joe Downing13dd76b2018-04-09 18:32:152546 if (delegate_) {
Mike Wasserman4ca09792020-05-29 17:44:432547 delegate_->EnterFullscreenModeForTab(requesting_frame, options);
mlamouri7a78d6fd2015-01-17 13:23:532548
Joe Downing13dd76b2018-04-09 18:32:152549 if (keyboard_lock_widget_)
2550 delegate_->RequestKeyboardLock(this, esc_key_locked_);
2551 }
2552
ericwilligersde386342016-10-19 02:35:092553 for (auto& observer : observers_)
2554 observer.DidToggleFullscreenModeForTab(IsFullscreenForCurrentTab(), false);
mlamouri7a78d6fd2015-01-17 13:23:532555}
2556
bokanece34a82016-01-28 19:49:462557void WebContentsImpl::ExitFullscreenMode(bool will_cause_resize) {
mlamouri7a78d6fd2015-01-17 13:23:532558 // This method is being called to leave renderer-initiated fullscreen mode.
2559 // Make sure any existing fullscreen widget is shut down first.
2560 RenderWidgetHostView* const widget_view = GetFullscreenRenderWidgetHostView();
avi85a4cef2015-12-18 20:13:372561 if (widget_view) {
2562 RenderWidgetHostImpl::From(widget_view->GetRenderWidgetHost())
2563 ->ShutdownAndDestroyWidget(true);
2564 }
mlamouri7a78d6fd2015-01-17 13:23:532565
Joe Downing13dd76b2018-04-09 18:32:152566 if (delegate_) {
mlamouri7a78d6fd2015-01-17 13:23:532567 delegate_->ExitFullscreenModeForTab(this);
[email protected]657c8e02014-03-19 19:18:372568
Joe Downing13dd76b2018-04-09 18:32:152569 if (keyboard_lock_widget_)
2570 delegate_->CancelKeyboardLockRequest(this);
2571 }
2572
bokanece34a82016-01-28 19:49:462573 // The fullscreen state is communicated to the renderer through a resize
2574 // message. If the change in fullscreen state doesn't cause a view resize
2575 // then we must ensure web contents exit the fullscreen state by explicitly
2576 // sending a resize message. This is required for the situation of the browser
2577 // moving the view into a "browser fullscreen" state and then the contents
2578 // entering "tab fullscreen". Exiting the contents "tab fullscreen" then won't
2579 // have the side effect of the view resizing, hence the explicit call here is
2580 // required.
2581 if (!will_cause_resize) {
2582 if (RenderWidgetHostView* rwhv = GetRenderWidgetHostView()) {
2583 if (RenderWidgetHost* render_widget_host = rwhv->GetRenderWidgetHost())
Fady Samuel0b911822018-04-25 13:22:162584 render_widget_host->SynchronizeVisualProperties();
bokanece34a82016-01-28 19:49:462585 }
scheib3dcb6c932015-02-23 10:55:582586 }
2587
Arthur Sonzogni929d29f2018-09-17 14:19:442588 current_fullscreen_frame_ = nullptr;
Becca Hughese9e27952018-06-25 17:08:272589
ericwilligersde386342016-10-19 02:35:092590 for (auto& observer : observers_) {
2591 observer.DidToggleFullscreenModeForTab(IsFullscreenForCurrentTab(),
2592 will_cause_resize);
2593 }
Becca Hughesd11d6502018-07-31 17:01:282594
2595 if (display_cutout_host_impl_)
2596 display_cutout_host_impl_->DidExitFullscreen();
[email protected]8a5e0ca2011-08-25 06:30:472597}
2598
Becca Hughesfd5d8f892018-06-14 18:23:362599void WebContentsImpl::FullscreenStateChanged(RenderFrameHost* rfh,
2600 bool is_fullscreen) {
Arthur Sonzogni929d29f2018-09-17 14:19:442601 RenderFrameHostImpl* frame = static_cast<RenderFrameHostImpl*>(rfh);
Becca Hughesfd5d8f892018-06-14 18:23:362602
2603 if (is_fullscreen) {
Jan Wilken Dörrie531be7ca2019-06-07 10:05:572604 if (!base::Contains(fullscreen_frames_, frame)) {
Arthur Sonzogni929d29f2018-09-17 14:19:442605 fullscreen_frames_.insert(frame);
2606 FullscreenFrameSetUpdated();
Becca Hughesfd5d8f892018-06-14 18:23:362607 }
Arthur Sonzogni929d29f2018-09-17 14:19:442608 return;
2609 }
Becca Hughesfd5d8f892018-06-14 18:23:362610
Arthur Sonzogni929d29f2018-09-17 14:19:442611 // If |frame| is no longer in fullscreen, remove it and any descendants.
2612 // See https://fullscreen.spec.whatwg.org.
Arthur Sonzogni969a794e2018-10-11 08:56:092613 size_t size_before_deletion = fullscreen_frames_.size();
2614 base::EraseIf(fullscreen_frames_, [&](RenderFrameHostImpl* current) {
2615 return (current == frame || current->IsDescendantOf(frame));
Arthur Sonzogni929d29f2018-09-17 14:19:442616 });
2617
Arthur Sonzogni969a794e2018-10-11 08:56:092618 if (size_before_deletion != fullscreen_frames_.size())
Arthur Sonzogni929d29f2018-09-17 14:19:442619 FullscreenFrameSetUpdated();
2620}
2621
2622void WebContentsImpl::FullscreenFrameSetUpdated() {
2623 if (fullscreen_frames_.empty()) {
2624 current_fullscreen_frame_ = nullptr;
2625 return;
Becca Hughesfd5d8f892018-06-14 18:23:362626 }
2627
Arthur Sonzogni929d29f2018-09-17 14:19:442628 // Find the current fullscreen frame and call the observers.
2629 // If frame A is fullscreen, then frame B goes into inner fullscreen, then B
2630 // exits fullscreen - that will result in A being fullscreen.
2631 RenderFrameHostImpl* new_fullscreen_frame = *std::max_element(
2632 fullscreen_frames_.begin(), fullscreen_frames_.end(), FrameCompareDepth);
Becca Hughesfd5d8f892018-06-14 18:23:362633
Arthur Sonzogni929d29f2018-09-17 14:19:442634 // If we have already notified observers about this frame then we should not
2635 // fire the observers again.
2636 if (new_fullscreen_frame == current_fullscreen_frame_)
2637 return;
2638 current_fullscreen_frame_ = new_fullscreen_frame;
Becca Hughesfd5d8f892018-06-14 18:23:362639
Arthur Sonzogni929d29f2018-09-17 14:19:442640 for (auto& observer : observers_)
2641 observer.DidAcquireFullscreen(new_fullscreen_frame);
Becca Hughesfd5d8f892018-06-14 18:23:362642
Arthur Sonzogni929d29f2018-09-17 14:19:442643 if (display_cutout_host_impl_)
2644 display_cutout_host_impl_->DidAcquireFullscreen(new_fullscreen_frame);
Becca Hughesfd5d8f892018-06-14 18:23:362645}
2646
Collin Baker989e0882019-11-01 01:27:172647void WebContentsImpl::UpdateVisibilityAndNotifyPageAndView(
2648 Visibility new_visibility) {
Collin Baker989e0882019-11-01 01:27:172649 // Only hide the page if there are no entities capturing screenshots
Collin Bakerc5259962019-11-14 17:51:582650 // or video (e.g. mirroring). If there are, apply the correct state of
2651 // kHidden or kHiddenButPainting.
2652 PageVisibilityState page_visibility;
2653 if (new_visibility == Visibility::VISIBLE || visible_capturer_count_ > 0)
2654 page_visibility = PageVisibilityState::kVisible;
2655 else if (hidden_capturer_count_ > 0)
2656 page_visibility = PageVisibilityState::kHiddenButPainting;
2657 else
2658 page_visibility = PageVisibilityState::kHidden;
Collin Baker989e0882019-11-01 01:27:172659 // If there are entities in Picture-in-Picture mode, don't activate
2660 // the "disable rendering" optimization.
Collin Bakerc5259962019-11-14 17:51:582661 const bool view_is_visible =
2662 page_visibility != PageVisibilityState::kHidden ||
2663 HasPictureInPictureVideo();
Collin Baker989e0882019-11-01 01:27:172664
Collin Bakerc5259962019-11-14 17:51:582665 if (page_visibility != PageVisibilityState::kHidden) {
Dana Friedcf3bc8e2019-11-13 20:00:242666 // We cannot show a page or capture video unless there is a valid renderer
2667 // associated with this web contents. The navigation controller for this
2668 // page must be set to active (allowing navigation to complete, a renderer
2669 // and its associated views to be created, etc.) if any of these conditions
2670 // holds.
2671 //
2672 // Previously, it was possible for browser-side code to try to capture video
2673 // from a restored tab (for a variety of reasons, including the browser
2674 // creating preview thumbnails) and the tab would never actually load. By
Collin Bakerc5259962019-11-14 17:51:582675 // keying this behavior off of |page_visibility| instead of just
Dana Friedcf3bc8e2019-11-13 20:00:242676 // |new_visibility| we avoid this case. See crbug.com/1020782 for more
2677 // context.
2678 controller_.SetActive(true);
2679
2680 // This shows the Page before showing the individual RenderWidgets, as
2681 // RenderWidgets will work to produce compositor frames and handle input
2682 // as soon as they are shown. But the Page and other classes do not expect
2683 // to be producing frames when the Page is hidden. So we make sure the Page
2684 // is shown first.
Yuzu Saijo232076f2020-05-29 07:14:022685 for (auto* rvh : GetRenderViewHostsIncludingBackForwardCached()) {
2686 rvh->SetVisibility(page_visibility);
Yuzu Saijobe024af02020-05-11 07:35:562687 }
Collin Baker98457b52019-11-06 21:34:292688 }
Collin Baker989e0882019-11-01 01:27:172689
2690 // |GetRenderWidgetHostView()| can be null if the user middle clicks a link to
2691 // open a tab in the background, then closes the tab before selecting it.
2692 // This is because closing the tab calls WebContentsImpl::Destroy(), which
2693 // removes the |GetRenderViewHost()|; then when we actually destroy the
2694 // window, OnWindowPosChanged() notices and calls WasHidden() (which
2695 // calls us).
2696 if (auto* view = GetRenderWidgetHostView()) {
2697 if (view_is_visible) {
2698 view->Show();
Collin Baker989e0882019-11-01 01:27:172699 } else if (new_visibility == Visibility::HIDDEN) {
2700 view->Hide();
2701 } else {
2702 view->WasOccluded();
2703 }
2704 }
2705
Carlos IL07f282f2020-05-15 22:58:292706 SetVisibilityForChildViews(view_is_visible);
Collin Baker989e0882019-11-01 01:27:172707
2708 // Make sure to call SetVisibilityAndNotifyObservers(VISIBLE) before notifying
2709 // the CrossProcessFrameConnector.
2710 if (new_visibility == Visibility::VISIBLE) {
2711 last_active_time_ = base::TimeTicks::Now();
2712 SetVisibilityAndNotifyObservers(new_visibility);
2713 }
2714
Collin Bakerc5259962019-11-14 17:51:582715 if (page_visibility == PageVisibilityState::kHidden) {
Collin Baker989e0882019-11-01 01:27:172716 // Similar to when showing the page, we only hide the page after
2717 // hiding the individual RenderWidgets.
Yuzu Saijo232076f2020-05-29 07:14:022718 for (auto* rvh : GetRenderViewHostsIncludingBackForwardCached()) {
2719 rvh->SetVisibility(page_visibility);
Yuzu Saijobe024af02020-05-11 07:35:562720 }
2721
Collin Baker989e0882019-11-01 01:27:172722 } else {
2723 for (FrameTreeNode* node : frame_tree_.Nodes()) {
2724 RenderFrameProxyHost* parent = node->render_manager()->GetProxyToParent();
2725 if (!parent)
2726 continue;
2727
2728 parent->cross_process_frame_connector()->DelegateWasShown();
2729 }
2730 }
2731
2732 if (new_visibility != Visibility::VISIBLE)
2733 SetVisibilityAndNotifyObservers(new_visibility);
2734}
2735
Clark DuValla0e42992018-09-17 17:00:112736#if defined(OS_ANDROID)
2737void WebContentsImpl::UpdateUserGestureCarryoverInfo() {
2738 if (delegate_)
2739 delegate_->UpdateUserGestureCarryoverInfo(this);
2740}
2741#endif
2742
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:412743bool WebContentsImpl::IsFullscreenForCurrentTab() {
[email protected]199bba6e2012-04-04 16:19:382744 return delegate_ ? delegate_->IsFullscreenForTabOrPending(this) : false;
[email protected]5d5f7af2011-10-01 01:38:122745}
2746
Malay Keshav87c42c02019-01-15 08:37:472747bool WebContentsImpl::ShouldShowStaleContentOnEviction() {
2748 return GetDelegate() && GetDelegate()->ShouldShowStaleContentOnEviction(this);
2749}
2750
billorr21c005d2016-11-17 03:57:042751bool WebContentsImpl::IsFullscreen() {
2752 return IsFullscreenForCurrentTab();
2753}
2754
danakj45d42452020-04-08 17:24:182755blink::mojom::DisplayMode WebContentsImpl::GetDisplayMode() const {
mikhail.pozdnyakovc0e251b2015-04-15 06:51:122756 return delegate_ ? delegate_->GetDisplayMode(this)
Eric Willigers052f0432019-10-04 04:06:572757 : blink::mojom::DisplayMode::kBrowser;
mikhail.pozdnyakovc0e251b2015-04-15 06:51:122758}
2759
avic3aa8422015-11-09 20:57:222760void WebContentsImpl::RequestToLockMouse(
2761 RenderWidgetHostImpl* render_widget_host,
2762 bool user_gesture,
lfgad8244352016-07-13 16:55:512763 bool last_unlocked_by_target,
2764 bool privileged) {
lfgf0cd46e2017-01-04 00:05:232765 for (WebContentsImpl* current = this; current;
2766 current = current->GetOuterWebContents()) {
2767 if (current->mouse_lock_widget_) {
James Hollyerd5c9de462020-03-10 19:02:452768 render_widget_host->GotResponseToLockMouseRequest(
2769 blink::mojom::PointerLockResult::kAlreadyLocked);
lfgf0cd46e2017-01-04 00:05:232770 return;
2771 }
[email protected]e9621112011-10-17 05:38:372772 }
avic3aa8422015-11-09 20:57:222773
lfgad8244352016-07-13 16:55:512774 if (privileged) {
lfgf0cd46e2017-01-04 00:05:232775 DCHECK(!GetOuterWebContents());
lfgad8244352016-07-13 16:55:512776 mouse_lock_widget_ = render_widget_host;
James Hollyerd5c9de462020-03-10 19:02:452777 render_widget_host->GotResponseToLockMouseRequest(
2778 blink::mojom::PointerLockResult::kSuccess);
lfgad8244352016-07-13 16:55:512779 return;
2780 }
2781
lfgbee1e0a2016-06-08 21:24:212782 bool widget_in_frame_tree = false;
2783 for (FrameTreeNode* node : frame_tree_.Nodes()) {
2784 if (node->current_frame_host()->GetRenderWidgetHost() ==
2785 render_widget_host) {
2786 widget_in_frame_tree = true;
2787 break;
2788 }
2789 }
2790
2791 if (widget_in_frame_tree && delegate_) {
lfgf0cd46e2017-01-04 00:05:232792 for (WebContentsImpl* current = this; current;
2793 current = current->GetOuterWebContents()) {
2794 current->mouse_lock_widget_ = render_widget_host;
2795 }
2796
avic3aa8422015-11-09 20:57:222797 delegate_->RequestToLockMouse(this, user_gesture, last_unlocked_by_target);
lfgbee1e0a2016-06-08 21:24:212798 } else {
James Hollyerd5c9de462020-03-10 19:02:452799 render_widget_host->GotResponseToLockMouseRequest(
2800 blink::mojom::PointerLockResult::kWrongDocument);
lfgbee1e0a2016-06-08 21:24:212801 }
[email protected]e9621112011-10-17 05:38:372802}
2803
avi3e4b8512015-11-11 01:55:492804void WebContentsImpl::LostMouseLock(RenderWidgetHostImpl* render_widget_host) {
lfgbee1e0a2016-06-08 21:24:212805 CHECK(mouse_lock_widget_);
lfgf0cd46e2017-01-04 00:05:232806
2807 if (mouse_lock_widget_->delegate()->GetAsWebContents() != this)
2808 return mouse_lock_widget_->delegate()->LostMouseLock(render_widget_host);
2809
lfgbee1e0a2016-06-08 21:24:212810 mouse_lock_widget_->SendMouseLockLost();
lfgf0cd46e2017-01-04 00:05:232811 for (WebContentsImpl* current = this; current;
2812 current = current->GetOuterWebContents()) {
2813 current->mouse_lock_widget_ = nullptr;
2814 }
avi3e4b8512015-11-11 01:55:492815
[email protected]e9621112011-10-17 05:38:372816 if (delegate_)
2817 delegate_->LostMouseLock();
2818}
2819
lfgad8244352016-07-13 16:55:512820bool WebContentsImpl::HasMouseLock(RenderWidgetHostImpl* render_widget_host) {
2821 // To verify if the mouse is locked, the mouse_lock_widget_ needs to be
2822 // assigned to the widget that requested the mouse lock, and the top-level
2823 // platform RenderWidgetHostView needs to hold the mouse lock from the OS.
Lucas Gadania0ea0172018-09-20 18:31:372824 auto* widget_host = GetTopLevelRenderWidgetHostView();
2825 return mouse_lock_widget_ == render_widget_host && widget_host &&
2826 widget_host->IsMouseLocked();
lfgad8244352016-07-13 16:55:512827}
2828
lfg84763c92017-02-16 18:55:152829RenderWidgetHostImpl* WebContentsImpl::GetMouseLockWidget() {
Lucas Gadania0ea0172018-09-20 18:31:372830 auto* widget_host = GetTopLevelRenderWidgetHostView();
2831 if ((widget_host && widget_host->IsMouseLocked()) ||
lfge2176fb2017-03-26 00:45:472832 (GetFullscreenRenderWidgetHostView() &&
Lucas Gadania0ea0172018-09-20 18:31:372833 GetFullscreenRenderWidgetHostView()->IsMouseLocked())) {
lfg84763c92017-02-16 18:55:152834 return mouse_lock_widget_;
Lucas Gadania0ea0172018-09-20 18:31:372835 }
lfg84763c92017-02-16 18:55:152836
2837 return nullptr;
2838}
2839
Joe Downing192998b22018-03-22 15:51:362840bool WebContentsImpl::RequestKeyboardLock(
Joe Downing13dd76b2018-04-09 18:32:152841 RenderWidgetHostImpl* render_widget_host,
2842 bool esc_key_locked) {
Joe Downing192998b22018-03-22 15:51:362843 DCHECK(render_widget_host);
Joe Downing192998b22018-03-22 15:51:362844 if (render_widget_host->delegate()->GetAsWebContents() != this) {
2845 NOTREACHED();
2846 return false;
2847 }
2848
2849 // KeyboardLock is only supported when called by the top-level browsing
2850 // context and is not supported in embedded content scenarios.
2851 if (GetOuterWebContents())
2852 return false;
2853
Joe Downing13dd76b2018-04-09 18:32:152854 esc_key_locked_ = esc_key_locked;
Joe Downing192998b22018-03-22 15:51:362855 keyboard_lock_widget_ = render_widget_host;
2856
Joe Downing13dd76b2018-04-09 18:32:152857 if (delegate_)
2858 delegate_->RequestKeyboardLock(this, esc_key_locked_);
Joe Downing192998b22018-03-22 15:51:362859 return true;
2860}
2861
2862void WebContentsImpl::CancelKeyboardLock(
2863 RenderWidgetHostImpl* render_widget_host) {
2864 if (!keyboard_lock_widget_ || render_widget_host != keyboard_lock_widget_)
2865 return;
2866
2867 RenderWidgetHostImpl* old_keyboard_lock_widget = keyboard_lock_widget_;
2868 keyboard_lock_widget_ = nullptr;
2869
Joe Downing13dd76b2018-04-09 18:32:152870 if (delegate_)
2871 delegate_->CancelKeyboardLockRequest(this);
2872
Joe Downing192998b22018-03-22 15:51:362873 old_keyboard_lock_widget->CancelKeyboardLock();
2874}
2875
2876RenderWidgetHostImpl* WebContentsImpl::GetKeyboardLockWidget() {
2877 return keyboard_lock_widget_;
2878}
2879
Dave Tapuskac3344362019-02-20 17:54:282880void WebContentsImpl::OnRenderFrameProxyVisibilityChanged(
2881 blink::mojom::FrameVisibility visibility) {
2882 switch (visibility) {
2883 case blink::mojom::FrameVisibility::kRenderedInViewport:
2884 WasShown();
2885 break;
2886 case blink::mojom::FrameVisibility::kNotRendered:
2887 WasHidden();
2888 break;
2889 case blink::mojom::FrameVisibility::kRenderedOutOfViewport:
2890 WasOccluded();
2891 break;
2892 }
ekaramada4f42692016-02-11 19:48:372893}
2894
Albert J. Wong65fe64d2019-09-20 02:48:142895RenderFrameHostDelegate* WebContentsImpl::CreateNewWindow(
csharrison95f01e922017-04-24 18:52:352896 RenderFrameHost* opener,
rockot5c478a72016-09-28 23:14:182897 const mojom::CreateNewWindowParams& params,
Albert J. Wong65fe64d2019-09-20 02:48:142898 bool is_new_browsing_instance,
Sergey Kuznetsov32257a22019-02-11 20:26:502899 bool has_user_gesture,
[email protected]97714c82012-06-06 10:15:132900 SessionStorageNamespace* session_storage_namespace) {
csharrison95f01e922017-04-24 18:52:352901 DCHECK(opener);
nick5ae4d2d2017-01-06 01:18:352902
csharrison95f01e922017-04-24 18:52:352903 int render_process_id = opener->GetProcess()->GetID();
csharrison95f01e922017-04-24 18:52:352904
Albert J. Wong65fe64d2019-09-20 02:48:142905 SiteInstance* source_site_instance = opener->GetSiteInstance();
nick5ae4d2d2017-01-06 01:18:352906
Aaron Colwelleb219982020-01-16 02:17:272907 const GURL source_site_url = source_site_instance->GetSiteURL();
[email protected]fdac6ade2013-07-20 01:06:302908 const std::string& partition_id =
Aaron Colwelleb219982020-01-16 02:17:272909 GetContentClient()->browser()->GetStoragePartitionIdForSite(
2910 GetBrowserContext(), source_site_url);
2911 {
2912 StoragePartition* partition = BrowserContext::GetStoragePartitionForSite(
2913 GetBrowserContext(), source_site_url);
2914 DOMStorageContextWrapper* dom_storage_context =
2915 static_cast<DOMStorageContextWrapper*>(
2916 partition->GetDOMStorageContext());
2917 SessionStorageNamespaceImpl* session_storage_namespace_impl =
2918 static_cast<SessionStorageNamespaceImpl*>(session_storage_namespace);
2919 CHECK(session_storage_namespace_impl->IsFromContext(dom_storage_context));
2920 }
[email protected]dd6730412013-08-14 15:03:372921
Albert J. Wong1ceccef92019-10-08 08:25:202922 if (delegate_ && delegate_->IsWebContentsCreationOverridden(
2923 source_site_instance, params.window_container_type,
2924 opener->GetLastCommittedURL(), params.frame_name,
2925 params.target_url)) {
2926 return static_cast<WebContentsImpl*>(delegate_->CreateCustomWebContents(
2927 opener, source_site_instance, is_new_browsing_instance,
2928 opener->GetLastCommittedURL(), params.frame_name, params.target_url,
2929 partition_id, session_storage_namespace));
[email protected]dd6730412013-08-14 15:03:372930 }
2931
danakj22b4e8b2019-08-01 23:14:392932 bool renderer_started_hidden =
2933 params.disposition == WindowOpenDisposition::NEW_BACKGROUND_TAB;
2934
Aaron Colwelleb219982020-01-16 02:17:272935 // We usually create the new window in the same BrowsingInstance (group of
2936 // script-related windows), by passing in the current SiteInstance. However,
2937 // if the opener is being suppressed (in a non-guest), we do not provide
2938 // a SiteInstance which causes a new one to get created in its own
2939 // BrowsingInstance.
2940 bool is_guest = BrowserPluginGuest::IsGuest(this);
2941 scoped_refptr<SiteInstance> site_instance =
2942 params.opener_suppressed && !is_guest ? nullptr : source_site_instance;
2943
[email protected]dd6730412013-08-14 15:03:372944 // Create the new web contents. This will automatically create the new
2945 // WebContentsView. In the future, we may want to create the view separately.
[email protected]fc72bb12013-06-02 21:13:462946 CreateParams create_params(GetBrowserContext(), site_instance.get());
nasko48321ca32015-07-02 20:44:122947 create_params.main_frame_name = params.frame_name;
alexmos4cf2aa32015-07-15 23:40:432948 create_params.opener_render_process_id = render_process_id;
csharrison95f01e922017-04-24 18:52:352949 create_params.opener_render_frame_id = opener->GetRoutingID();
[email protected]50d326e2014-05-20 17:59:062950 create_params.opener_suppressed = params.opener_suppressed;
danakj22b4e8b2019-08-01 23:14:392951 create_params.initially_hidden = renderer_started_hidden;
[email protected]50d326e2014-05-20 17:59:062952
Albert J. Wong65fe64d2019-09-20 02:48:142953 // Even though all codepaths leading here are in response to a renderer
2954 // tryng to open a new window, if the new window ends up in a different
2955 // browsing instance, then the RenderViewHost, RenderWidgetHost,
2956 // RenderFrameHost constellation is effectively browser initiated
2957 // the opener's process will not given the routing IDs for the new
2958 // objects.
2959 create_params.renderer_initiated_creation = !is_new_browsing_instance;
Albert J. Wong1ceccef92019-10-08 08:25:202960
Albert J. Wong65fe64d2019-09-20 02:48:142961 std::unique_ptr<WebContentsImpl> new_contents;
[email protected]c4538072013-03-18 02:17:552962 if (!is_guest) {
2963 create_params.context = view_->GetNativeView();
Albert J. Wong65fe64d2019-09-20 02:48:142964 new_contents = WebContentsImpl::Create(create_params);
2965 } else {
2966 new_contents = base::WrapUnique(static_cast<WebContentsImpl*>(
2967 GetBrowserPluginGuest()->CreateNewGuestWindow(create_params)));
[email protected]c4538072013-03-18 02:17:552968 }
Albert J. Wong65fe64d2019-09-20 02:48:142969 auto* new_contents_impl = new_contents.get();
danakje6c01622018-10-05 19:39:262970
2971 new_contents_impl->GetController().SetSessionStorageNamespace(
erikchenbee5c9622018-04-27 19:30:252972 partition_id, session_storage_namespace);
[email protected]d1198fd2012-08-13 22:50:192973
alexmos646fec02015-07-25 00:11:492974 // If the new frame has a name, make sure any SiteInstances that can find
2975 // this named frame have proxies for it. Must be called after
2976 // SetSessionStorageNamespace, since this calls CreateRenderView, which uses
2977 // GetSessionStorageNamespace.
2978 if (!params.frame_name.empty())
danakje6c01622018-10-05 19:39:262979 new_contents_impl->GetRenderManager()->CreateProxiesForNewNamedFrame();
alexmos646fec02015-07-25 00:11:492980
[email protected]c4538072013-03-18 02:17:552981 // Save the window for later if we're not suppressing the opener (since it
[email protected]d70bea92013-04-05 04:23:342982 // will be shown immediately).
2983 if (!params.opener_suppressed) {
2984 if (!is_guest) {
danakje6c01622018-10-05 19:39:262985 WebContentsView* new_view = new_contents_impl->view_.get();
[email protected]bafe6cd2012-05-23 23:09:502986
[email protected]d70bea92013-04-05 04:23:342987 // TODO(brettw): It seems bogus that we have to call this function on the
2988 // newly created object and give it one of its own member variables.
danakj22b4e8b2019-08-01 23:14:392989 RenderWidgetHostView* widget_view = new_view->CreateViewForWidget(
W. James MacLean13d834d2019-12-04 16:06:452990 new_contents_impl->GetRenderViewHost()->GetWidget());
danakj22b4e8b2019-08-01 23:14:392991 if (!renderer_started_hidden) {
2992 // RenderWidgets for frames always initialize as hidden. If the renderer
2993 // created this window as visible, then we show it here.
2994 widget_view->Show();
2995 }
[email protected]d70bea92013-04-05 04:23:342996 }
[email protected]bafe6cd2012-05-23 23:09:502997 // Save the created window associated with the route so we can show it
2998 // later.
Albert J. Wongcb004632018-07-10 22:58:252999 //
3000 // TODO(ajwong): This should be keyed off the RenderFrame routing id or the
3001 // FrameTreeNode id instead of the routing id of the Widget for the main
3002 // frame. https://crbug.com/545684
Fergal Daly052873a82020-03-11 14:45:493003 int32_t main_frame_routing_id = new_contents_impl->GetMainFrame()
3004 ->GetRenderWidgetHost()
3005 ->GetRoutingID();
3006 GlobalRoutingID id(render_process_id, main_frame_routing_id);
Joel Hockey8c2011b22020-04-30 05:07:193007 pending_contents_[id] =
3008 CreatedWindow(std::move(new_contents), params.target_url);
danakje6c01622018-10-05 19:39:263009 AddDestructionObserver(new_contents_impl);
[email protected]bafe6cd2012-05-23 23:09:503010 }
3011
3012 if (delegate_) {
Bryan McQuaded2b93c02017-07-14 22:26:313013 delegate_->WebContentsCreated(this, render_process_id,
3014 opener->GetRoutingID(), params.frame_name,
danakje6c01622018-10-05 19:39:263015 params.target_url, new_contents_impl);
[email protected]bafe6cd2012-05-23 23:09:503016 }
3017
Charlie Harrisonf60dd1c2018-08-15 21:15:413018 for (auto& observer : observers_) {
danakje6c01622018-10-05 19:39:263019 observer.DidOpenRequestedURL(new_contents_impl, opener, params.target_url,
Leon Han963dc182018-11-06 05:41:483020 params.referrer.To<Referrer>(),
3021 params.disposition, ui::PAGE_TRANSITION_LINK,
Charlie Harrisonf60dd1c2018-08-15 21:15:413022 false, // started_from_context_menu
3023 true); // renderer_initiated
pnolandaae574e2017-03-06 21:04:213024 }
3025
Avi Drissmanba3b1b342017-08-29 18:16:133026 // Any new WebContents opened while this WebContents is in fullscreen can be
3027 // used to confuse the user, so drop fullscreen.
Avi Drissman1a55a9d62020-03-10 18:56:453028 base::ScopedClosureRunner fullscreen_block = ForSecurityDropFullscreen();
3029 // The new contents will be independent of this contents, so release the
3030 // fullscreen block.
3031 fullscreen_block.RunAndReset();
Avi Drissmanba3b1b342017-08-29 18:16:133032
[email protected]bafe6cd2012-05-23 23:09:503033 if (params.opener_suppressed) {
3034 // When the opener is suppressed, the original renderer cannot access the
3035 // new window. As a result, we need to show and navigate the window here.
[email protected]eda238a12012-09-07 23:44:003036 bool was_blocked = false;
erikchenbee5c9622018-04-27 19:30:253037
[email protected]eda238a12012-09-07 23:44:003038 if (delegate_) {
danakje6c01622018-10-05 19:39:263039 base::WeakPtr<WebContentsImpl> weak_new_contents =
3040 new_contents_impl->weak_factory_.GetWeakPtr();
nick0d558482017-02-13 22:27:223041
danakje6c01622018-10-05 19:39:263042 gfx::Rect initial_rect; // Report an empty initial rect.
Albert J. Wong65fe64d2019-09-20 02:48:143043 delegate_->AddNewContents(this, std::move(new_contents),
Joel Hockey891e88062020-04-30 05:38:443044 params.target_url, params.disposition,
3045 initial_rect, has_user_gesture, &was_blocked);
danakje6c01622018-10-05 19:39:263046 // The delegate may delete |new_contents_impl| during AddNewContents().
nick0d558482017-02-13 22:27:223047 if (!weak_new_contents)
Albert J. Wong65fe64d2019-09-20 02:48:143048 return nullptr;
[email protected]eda238a12012-09-07 23:44:003049 }
erikchenbee5c9622018-04-27 19:30:253050
[email protected]eda238a12012-09-07 23:44:003051 if (!was_blocked) {
Nasko Oskov83a8cf92018-10-19 14:58:563052 std::unique_ptr<NavigationController::LoadURLParams> load_params =
3053 std::make_unique<NavigationController::LoadURLParams>(
3054 params.target_url);
Nasko Oskov93e7c55c2018-12-19 01:59:293055 load_params->initiator_origin = opener->GetLastCommittedOrigin();
Aaron Colwell9dab1652019-12-09 18:29:493056 load_params->source_site_instance = source_site_instance;
Leon Han963dc182018-11-06 05:41:483057 load_params->referrer = params.referrer.To<Referrer>();
Nasko Oskov83a8cf92018-10-19 14:58:563058 load_params->transition_type = ui::PAGE_TRANSITION_LINK;
3059 load_params->is_renderer_initiated = true;
Sergey Kuznetsov32257a22019-02-11 20:26:503060 load_params->has_user_gesture = has_user_gesture;
mariakhomenkoa4971c12015-07-21 19:04:373061
3062 if (delegate_ && !is_guest &&
3063 !delegate_->ShouldResumeRequestsForCreatedWindow()) {
Nasko Oskov83a8cf92018-10-19 14:58:563064 // We are in asynchronous add new contents path, delay navigation.
3065 DCHECK(!new_contents_impl->delayed_open_url_params_);
3066 new_contents_impl->delayed_load_url_params_ = std::move(load_params);
mariakhomenkoa4971c12015-07-21 19:04:373067 } else {
Nasko Oskov83a8cf92018-10-19 14:58:563068 new_contents_impl->controller_.LoadURLWithParams(*load_params.get());
Nasko Oskov28ebd5b2018-12-07 22:29:333069 if (!is_guest)
3070 new_contents_impl->Focus();
mariakhomenkoa4971c12015-07-21 19:04:373071 }
[email protected]eda238a12012-09-07 23:44:003072 }
[email protected]bafe6cd2012-05-23 23:09:503073 }
Albert J. Wong65fe64d2019-09-20 02:48:143074 return new_contents_impl;
[email protected]bafe6cd2012-05-23 23:09:503075}
3076
Dave Tapuskad9835622020-03-10 17:32:313077void WebContentsImpl::CreateNewWidget(
3078 int32_t render_process_id,
3079 int32_t widget_route_id,
Dave Tapuska8499eec2020-03-16 17:54:303080 mojo::PendingAssociatedReceiver<blink::mojom::WidgetHost> blink_widget_host,
3081 mojo::PendingAssociatedRemote<blink::mojom::Widget> blink_widget) {
Albert J. Wongf6e13ed2018-09-18 15:25:473082 CreateNewWidget(render_process_id, widget_route_id, /*is_fullscreen=*/false,
Dave Tapuskab686c1b2020-06-09 00:40:233083 std::move(blink_widget_host), std::move(blink_widget));
[email protected]bafe6cd2012-05-23 23:09:503084}
3085
Julie Jeongeun Kimcaacf9102019-09-12 02:13:053086void WebContentsImpl::CreateNewFullscreenWidget(
3087 int32_t render_process_id,
3088 int32_t widget_route_id,
Dave Tapuska8499eec2020-03-16 17:54:303089 mojo::PendingAssociatedReceiver<blink::mojom::WidgetHost> blink_widget_host,
3090 mojo::PendingAssociatedRemote<blink::mojom::Widget> blink_widget) {
Albert J. Wongf6e13ed2018-09-18 15:25:473091 CreateNewWidget(render_process_id, widget_route_id, /*is_fullscreen=*/true,
Dave Tapuskab686c1b2020-06-09 00:40:233092 std::move(blink_widget_host), std::move(blink_widget));
[email protected]bafe6cd2012-05-23 23:09:503093}
3094
Dave Tapuskad9835622020-03-10 17:32:313095void WebContentsImpl::CreateNewWidget(
3096 int32_t render_process_id,
3097 int32_t route_id,
3098 bool is_fullscreen,
Dave Tapuska8499eec2020-03-16 17:54:303099 mojo::PendingAssociatedReceiver<blink::mojom::WidgetHost> blink_widget_host,
3100 mojo::PendingAssociatedRemote<blink::mojom::Widget> blink_widget) {
kenrba26d15092016-02-10 22:00:253101 RenderProcessHost* process = RenderProcessHost::FromID(render_process_id);
kenrbb46f86a2015-12-07 07:53:233102 // A message to create a new widget can only come from an active process for
[email protected]a8504022013-12-04 20:23:513103 // this WebContentsImpl instance. If any other process sends the request,
3104 // it is invalid and the process must be terminated.
dcheng57e39e22016-01-21 00:25:383105 if (!HasMatchingProcess(&frame_tree_, render_process_id)) {
Lukasz Anforowicz6f746282018-01-04 23:24:513106 ReceivedBadMessage(process, bad_message::WCI_NEW_WIDGET_PROCESS_MISMATCH);
[email protected]a8504022013-12-04 20:23:513107 return;
3108 }
3109
Dave Tapuskab686c1b2020-06-09 00:40:233110 RenderWidgetHostImpl* widget_host =
3111 new RenderWidgetHostImpl(this, process, route_id, IsHidden(),
3112 std::make_unique<FrameTokenMessageQueue>());
Dave Tapuska8499eec2020-03-16 17:54:303113
3114 widget_host->BindWidgetInterfaces(std::move(blink_widget_host),
3115 std::move(blink_widget));
[email protected]cfd80b02014-05-01 17:46:483116 RenderWidgetHostViewBase* widget_view =
3117 static_cast<RenderWidgetHostViewBase*>(
danakj0b246942018-09-17 21:30:273118 view_->CreateViewForChildWidget(widget_host));
[email protected]83918ec2013-01-10 15:37:193119 if (!widget_view)
3120 return;
danakj0b246942018-09-17 21:30:273121 // Fullscreen child widgets are frames, other child widgets are popups, and
3122 // popups should not get activated.
3123 if (!is_fullscreen)
3124 widget_view->SetWidgetType(WidgetType::kPopup);
[email protected]bafe6cd2012-05-23 23:09:503125 // Save the created widget associated with the route so we can show it later.
Lukasz Anforowicz09060bdf72018-08-23 15:53:173126 pending_widget_views_[GlobalRoutingID(render_process_id, route_id)] =
alexmosc2a8cec2016-05-23 22:19:533127 widget_view;
[email protected]bafe6cd2012-05-23 23:09:503128}
3129
alexmosc2a8cec2016-05-23 22:19:533130void WebContentsImpl::ShowCreatedWindow(int process_id,
nick5ae4d2d2017-01-06 01:18:353131 int main_frame_widget_route_id,
[email protected]bafe6cd2012-05-23 23:09:503132 WindowOpenDisposition disposition,
bokan107a47f2015-02-03 23:23:393133 const gfx::Rect& initial_rect,
[email protected]bafe6cd2012-05-23 23:09:503134 bool user_gesture) {
danakje6c01622018-10-05 19:39:263135 // This method is the renderer requesting an existing top level window to
3136 // show a new top level window that the renderer created. Each top level
3137 // window is associated with a WebContents. In this case it was created
3138 // earlier but showing it was deferred until the renderer requested for it
3139 // to be shown. We find that previously created WebContents here.
3140 // TODO(danakj): Why do we defer this show step until the renderer asks for it
3141 // when it will always do so. What needs to happen in the renderer before we
3142 // reach here?
Joel Hockey8c2011b22020-04-30 05:07:193143 base::Optional<CreatedWindow> owned_created =
nick5ae4d2d2017-01-06 01:18:353144 GetCreatedWindow(process_id, main_frame_widget_route_id);
Joel Hockey8c2011b22020-04-30 05:07:193145
danakje6c01622018-10-05 19:39:263146 // The browser may have rejected the request to make a new window, or the
3147 // renderer could be sending an invalid route id. Ignore the request then.
Joel Hockey8c2011b22020-04-30 05:07:193148 if (!owned_created || !owned_created->contents)
danakje6c01622018-10-05 19:39:263149 return;
mariakhomenko44bdc4732015-04-29 01:55:383150
Joel Hockey8c2011b22020-04-30 05:07:193151 WebContentsImpl* created = owned_created->contents.get();
3152
danakje6c01622018-10-05 19:39:263153 // This uses the delegate for the WebContents where the window was created
3154 // from, to control how to show the newly created window.
3155 WebContentsDelegate* delegate = GetDelegate();
nick5ae4d2d2017-01-06 01:18:353156
danakje6c01622018-10-05 19:39:263157 // The delegate can be null in tests, so we must check for it :(.
3158 if (delegate) {
3159 // Mark the web contents as pending resume, then immediately do
3160 // the resume if the delegate wants it.
3161 created->is_resume_pending_ = true;
3162 if (delegate->ShouldResumeRequestsForCreatedWindow())
3163 created->ResumeLoadingCreatedWebContents();
3164
3165 base::WeakPtr<WebContentsImpl> weak_created =
3166 created->weak_factory_.GetWeakPtr();
Joel Hockey8c2011b22020-04-30 05:07:193167 delegate->AddNewContents(this, std::move(owned_created->contents),
Joel Hockey891e88062020-04-30 05:38:443168 std::move(owned_created->target_url), disposition,
3169 initial_rect, user_gesture, nullptr);
danakje6c01622018-10-05 19:39:263170 // The delegate may delete |created| during AddNewContents().
3171 if (!weak_created)
3172 return;
[email protected]eda238a12012-09-07 23:44:003173 }
danakje6c01622018-10-05 19:39:263174
3175 RenderWidgetHostImpl* rwh = created->GetMainFrame()->GetRenderWidgetHost();
3176 DCHECK_EQ(main_frame_widget_route_id, rwh->GetRoutingID());
3177 rwh->Send(new WidgetMsg_SetBounds_ACK(rwh->GetRoutingID()));
[email protected]bafe6cd2012-05-23 23:09:503178}
3179
alexmosc2a8cec2016-05-23 22:19:533180void WebContentsImpl::ShowCreatedWidget(int process_id,
Albert J. Wongf6e13ed2018-09-18 15:25:473181 int widget_route_id,
bokan107a47f2015-02-03 23:23:393182 const gfx::Rect& initial_rect) {
Albert J. Wongf6e13ed2018-09-18 15:25:473183 ShowCreatedWidget(process_id, widget_route_id, false, initial_rect);
[email protected]bafe6cd2012-05-23 23:09:503184}
3185
alexmosc2a8cec2016-05-23 22:19:533186void WebContentsImpl::ShowCreatedFullscreenWidget(int process_id,
Albert J. Wongf6e13ed2018-09-18 15:25:473187 int widget_route_id) {
3188 ShowCreatedWidget(process_id, widget_route_id, true, gfx::Rect());
[email protected]bafe6cd2012-05-23 23:09:503189}
3190
alexmosc2a8cec2016-05-23 22:19:533191void WebContentsImpl::ShowCreatedWidget(int process_id,
3192 int route_id,
[email protected]bafe6cd2012-05-23 23:09:503193 bool is_fullscreen,
bokan107a47f2015-02-03 23:23:393194 const gfx::Rect& initial_rect) {
[email protected]cfd80b02014-05-01 17:46:483195 RenderWidgetHostViewBase* widget_host_view =
alexmosc2a8cec2016-05-23 22:19:533196 static_cast<RenderWidgetHostViewBase*>(
3197 GetCreatedWidget(process_id, route_id));
[email protected]bafe6cd2012-05-23 23:09:503198 if (!widget_host_view)
3199 return;
[email protected]cfd80b02014-05-01 17:46:483200
W. James MacLeanec4125c2019-09-27 18:56:003201 // GetOutermostWebContents() returns |this| if there are no outer WebContents.
W. James MacLean3c5f50462019-10-28 16:24:213202 auto* outer_web_contents = GetOuterWebContents();
3203 auto* outermost_web_contents = GetOutermostWebContents();
W. James MacLeanec4125c2019-09-27 18:56:003204 RenderWidgetHostView* view =
W. James MacLean3c5f50462019-10-28 16:24:213205 outermost_web_contents->GetRenderWidgetHostView();
3206 // It's not entirely obvious why we need the transform only in the case where
3207 // the outer webcontents is not the same as the outermost webcontents. It may
3208 // be due to the fact that oopifs that are children of the mainframe get
3209 // correct values for their screenrects, but deeper cross-process frames do
3210 // not. Hopefully this can be resolved with https://crbug.com/928825.
3211 // Handling these cases separately is needed for http://crbug.com/1015298.
3212 bool needs_transform = this != outermost_web_contents &&
3213 outermost_web_contents != outer_web_contents;
W. James MacLeanec4125c2019-09-27 18:56:003214
3215 gfx::Rect transformed_rect(initial_rect);
3216 RenderWidgetHostView* this_view = GetRenderWidgetHostView();
W. James MacLean3c5f50462019-10-28 16:24:213217 if (needs_transform) {
W. James MacLeanec4125c2019-09-27 18:56:003218 // We need to transform the coordinates of initial_rect.
3219 gfx::Point origin =
3220 this_view->TransformPointToRootCoordSpace(initial_rect.origin());
3221 gfx::Point bottom_right =
3222 this_view->TransformPointToRootCoordSpace(initial_rect.bottom_right());
3223 transformed_rect =
3224 gfx::Rect(origin.x(), origin.y(), bottom_right.x() - origin.x(),
3225 bottom_right.y() - origin.y());
[email protected]cfd80b02014-05-01 17:46:483226 }
3227
danakj0b246942018-09-17 21:30:273228 // Fullscreen child widgets are frames, other child widgets are popups.
[email protected]4aebbca2013-09-17 22:26:493229 if (is_fullscreen) {
[email protected]d7f80ba2013-10-12 07:42:313230 DCHECK_EQ(MSG_ROUTING_NONE, fullscreen_widget_routing_id_);
[email protected]0b431992014-06-24 00:08:033231 view_->StoreFocus();
alexmosc9e76ec2016-05-16 22:59:373232 fullscreen_widget_process_id_ =
3233 widget_host_view->GetRenderWidgetHost()->GetProcess()->GetID();
[email protected]d7f80ba2013-10-12 07:42:313234 fullscreen_widget_routing_id_ = route_id;
[email protected]4aebbca2013-09-17 22:26:493235 if (delegate_ && delegate_->EmbedsFullscreenWidget()) {
3236 widget_host_view->InitAsChild(GetRenderWidgetHostView()->GetNativeView());
Mike Wasserman4ca09792020-05-29 17:44:433237 delegate_->EnterFullscreenModeForTab(GetMainFrame(),
Dave Tapuskaa4189512019-10-15 20:27:343238 blink::mojom::FullscreenOptions());
[email protected]4aebbca2013-09-17 22:26:493239 } else {
[email protected]cfd80b02014-05-01 17:46:483240 widget_host_view->InitAsFullscreen(view);
[email protected]4aebbca2013-09-17 22:26:493241 }
ericwilligersde386342016-10-19 02:35:093242 for (auto& observer : observers_)
3243 observer.DidShowFullscreenWidget();
[email protected]4aebbca2013-09-17 22:26:493244 if (!widget_host_view->HasFocus())
3245 widget_host_view->Focus();
3246 } else {
W. James MacLeanec4125c2019-09-27 18:56:003247 widget_host_view->InitAsPopup(view, transformed_rect);
[email protected]4aebbca2013-09-17 22:26:493248 }
[email protected]89054502012-06-03 10:29:243249
yiyixcb1fbc4f2018-03-16 19:54:083250 RenderWidgetHostImpl* render_widget_host_impl = widget_host_view->host();
[email protected]89054502012-06-03 10:29:243251 render_widget_host_impl->Init();
[email protected]d7f80ba2013-10-12 07:42:313252 // Only allow privileged mouse lock for fullscreen render widget, which is
3253 // used to implement Pepper Flash fullscreen.
3254 render_widget_host_impl->set_allow_privileged_mouse_lock(is_fullscreen);
[email protected]bafe6cd2012-05-23 23:09:503255}
3256
Joel Hockey8c2011b22020-04-30 05:07:193257base::Optional<CreatedWindow> WebContentsImpl::GetCreatedWindow(
nick5ae4d2d2017-01-06 01:18:353258 int process_id,
3259 int main_frame_widget_route_id) {
Lukasz Anforowicz09060bdf72018-08-23 15:53:173260 auto key = GlobalRoutingID(process_id, main_frame_widget_route_id);
nick5ae4d2d2017-01-06 01:18:353261 auto iter = pending_contents_.find(key);
[email protected]bafe6cd2012-05-23 23:09:503262
3263 // Certain systems can block the creation of new windows. If we didn't succeed
3264 // in creating one, just return NULL.
alexmosc2a8cec2016-05-23 22:19:533265 if (iter == pending_contents_.end())
Joel Hockey8c2011b22020-04-30 05:07:193266 return base::nullopt;
[email protected]bafe6cd2012-05-23 23:09:503267
Joel Hockey8c2011b22020-04-30 05:07:193268 CreatedWindow result = std::move(iter->second);
3269 WebContentsImpl* new_contents = result.contents.get();
nick5ae4d2d2017-01-06 01:18:353270 pending_contents_.erase(key);
Joel Hockey8c2011b22020-04-30 05:07:193271 RemoveDestructionObserver(new_contents);
[email protected]bafe6cd2012-05-23 23:09:503272
[email protected]d70bea92013-04-05 04:23:343273 // Don't initialize the guest WebContents immediately.
Joel Hockey8c2011b22020-04-30 05:07:193274 if (BrowserPluginGuest::IsGuest(new_contents))
3275 return result;
[email protected]d70bea92013-04-05 04:23:343276
Lukasz Anforowicz5510ed652018-06-06 16:16:193277 if (!new_contents->GetMainFrame()->GetProcess()->IsInitializedAndNotDead() ||
nick5ae4d2d2017-01-06 01:18:353278 !new_contents->GetMainFrame()->GetView()) {
Joel Hockey8c2011b22020-04-30 05:07:193279 return base::nullopt;
nick5ae4d2d2017-01-06 01:18:353280 }
[email protected]bafe6cd2012-05-23 23:09:503281
Joel Hockey8c2011b22020-04-30 05:07:193282 return result;
[email protected]bafe6cd2012-05-23 23:09:503283}
3284
alexmosc2a8cec2016-05-23 22:19:533285RenderWidgetHostView* WebContentsImpl::GetCreatedWidget(int process_id,
3286 int route_id) {
Lukasz Anforowicz09060bdf72018-08-23 15:53:173287 auto iter = pending_widget_views_.find(GlobalRoutingID(process_id, route_id));
[email protected]bafe6cd2012-05-23 23:09:503288 if (iter == pending_widget_views_.end()) {
3289 DCHECK(false);
alexmosc2a8cec2016-05-23 22:19:533290 return nullptr;
[email protected]bafe6cd2012-05-23 23:09:503291 }
3292
3293 RenderWidgetHostView* widget_host_view = iter->second;
Lukasz Anforowicz09060bdf72018-08-23 15:53:173294 pending_widget_views_.erase(GlobalRoutingID(process_id, route_id));
[email protected]bafe6cd2012-05-23 23:09:503295
3296 RenderWidgetHost* widget_host = widget_host_view->GetRenderWidgetHost();
Lukasz Anforowicz5510ed652018-06-06 16:16:193297 if (!widget_host->GetProcess()->IsInitializedAndNotDead()) {
[email protected]bafe6cd2012-05-23 23:09:503298 // The view has gone away or the renderer crashed. Nothing to do.
alexmosc2a8cec2016-05-23 22:19:533299 return nullptr;
[email protected]bafe6cd2012-05-23 23:09:503300 }
3301
3302 return widget_host_view;
3303}
3304
[email protected]f13b4202012-06-12 23:53:233305void WebContentsImpl::RequestMediaAccessPermission(
[email protected]33662e52013-01-07 21:31:093306 const MediaStreamRequest& request,
Mark Pilgrim57499082018-06-12 12:38:303307 MediaResponseCallback callback) {
[email protected]d19b84b2014-03-14 11:52:373308 if (delegate_) {
Mark Pilgrim57499082018-06-12 12:38:303309 delegate_->RequestMediaAccessPermission(this, request, std::move(callback));
[email protected]d19b84b2014-03-14 11:52:373310 } else {
Antonio Gomes0d42960a2019-06-05 12:35:513311 std::move(callback).Run(
3312 blink::MediaStreamDevices(),
3313 blink::mojom::MediaStreamRequestResult::FAILED_DUE_TO_SHUTDOWN,
3314 std::unique_ptr<MediaStreamUI>());
[email protected]d19b84b2014-03-14 11:52:373315 }
[email protected]f13b4202012-06-12 23:53:233316}
3317
Chandan Padhia4b8bcb72017-09-12 16:41:043318bool WebContentsImpl::CheckMediaAccessPermission(
Raymes Khouryad7c24a12018-03-05 23:22:583319 RenderFrameHost* render_frame_host,
Chandan Padhia4b8bcb72017-09-12 16:41:043320 const url::Origin& security_origin,
Antonio Gomesc8b734b2019-06-05 18:22:163321 blink::mojom::MediaStreamType type) {
3322 DCHECK(type == blink::mojom::MediaStreamType::DEVICE_AUDIO_CAPTURE ||
3323 type == blink::mojom::MediaStreamType::DEVICE_VIDEO_CAPTURE);
Chandan Padhia4b8bcb72017-09-12 16:41:043324 return delegate_ && delegate_->CheckMediaAccessPermission(
Raymes Khouryad7c24a12018-03-05 23:22:583325 render_frame_host, security_origin.GetURL(), type);
grunell657d4d82014-09-18 00:09:433326}
3327
Guido Urdaneta73fa6632019-01-14 18:46:263328std::string WebContentsImpl::GetDefaultMediaDeviceID(
Antonio Gomesc8b734b2019-06-05 18:22:163329 blink::mojom::MediaStreamType type) {
guidou8a440b842017-01-30 13:58:433330 if (!delegate_)
3331 return std::string();
3332 return delegate_->GetDefaultMediaDeviceID(this, type);
3333}
3334
[email protected]cc9200432013-07-23 23:02:403335SessionStorageNamespace* WebContentsImpl::GetSessionStorageNamespace(
3336 SiteInstance* instance) {
3337 return controller_.GetSessionStorageNamespace(instance);
3338}
3339
[email protected]6de7fc482014-06-06 10:46:443340SessionStorageNamespaceMap WebContentsImpl::GetSessionStorageNamespaceMap() {
3341 return controller_.GetSessionStorageNamespaceMap();
3342}
3343
[email protected]9b159a52013-10-03 17:24:553344FrameTree* WebContentsImpl::GetFrameTree() {
3345 return &frame_tree_;
3346}
3347
aelias5252baa2016-04-10 01:18:023348bool WebContentsImpl::IsOverridingUserAgent() {
3349 return GetController().GetVisibleEntry() &&
3350 GetController().GetVisibleEntry()->GetIsOverridingUserAgent();
3351}
3352
avi85ee8362016-10-08 02:09:083353bool WebContentsImpl::IsJavaScriptDialogShowing() const {
3354 return is_showing_javascript_dialog_;
3355}
3356
avic031d392017-03-03 03:09:423357bool WebContentsImpl::ShouldIgnoreUnresponsiveRenderer() {
3358 // Ignore unresponsive renderers if the debugger is attached to them since the
3359 // unresponsiveness might be a result of the renderer sitting on a breakpoint.
3360 //
David Bienvenu525f8a02019-02-15 02:02:423361#ifdef OS_WIN
3362 // Check if a windows debugger is attached to the renderer process.
3363 base::ProcessHandle process_handle =
3364 GetMainFrame()->GetProcess()->GetProcess().Handle();
3365 BOOL debugger_present = FALSE;
3366 if (CheckRemoteDebuggerPresent(process_handle, &debugger_present) &&
3367 debugger_present)
3368 return true;
3369#endif // OS_WIN
3370
avic031d392017-03-03 03:09:423371 // TODO(pfeldman): Fix this to only return true if the renderer is *actually*
3372 // sitting on a breakpoint. https://crbug.com/684202
3373 return DevToolsAgentHost::IsDebuggerAttached(this);
3374}
3375
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:413376ui::AXMode WebContentsImpl::GetAccessibilityMode() {
[email protected]95640212014-07-26 18:14:303377 return accessibility_mode_;
3378}
3379
Dominic Mazzonia7b0edb22017-08-09 16:32:513380void WebContentsImpl::AccessibilityEventReceived(
Dominic Mazzoniccbaa9b2018-06-06 07:44:233381 const AXEventNotificationDetails& details) {
ericwilligersde386342016-10-19 02:35:093382 for (auto& observer : observers_)
Dominic Mazzonia7b0edb22017-08-09 16:32:513383 observer.AccessibilityEventReceived(details);
[email protected]31a71eaf2014-03-13 01:47:363384}
3385
dmazzoni2400c462016-08-23 15:07:133386void WebContentsImpl::AccessibilityLocationChangesReceived(
3387 const std::vector<AXLocationChangeNotificationDetails>& details) {
ericwilligersde386342016-10-19 02:35:093388 for (auto& observer : observers_)
Dominic Mazzonia7b0edb22017-08-09 16:32:513389 observer.AccessibilityLocationChangesReceived(details);
dmazzoni2400c462016-08-23 15:07:133390}
3391
Abigail Kleinabb428332019-09-13 18:26:213392base::string16 WebContentsImpl::DumpAccessibilityTree(
3393 bool internal,
3394 std::vector<AccessibilityTreeFormatter::PropertyFilter> property_filters) {
James Wallace-Leeeafc94cb92018-07-23 21:35:093395 auto* ax_mgr = GetOrCreateRootBrowserAccessibilityManager();
3396 DCHECK(ax_mgr);
Lei Zhangc98caa3822019-11-07 19:17:273397 return AccessibilityTreeFormatterBase::DumpAccessibilityTreeFromManager(
Abigail Kleinabb428332019-09-13 18:26:213398 ax_mgr, internal, property_filters);
James Wallace-Leeeafc94cb92018-07-23 21:35:093399}
3400
Abigail Kleinaa898742019-11-01 02:31:253401void WebContentsImpl::RecordAccessibilityEvents(
Abigail Kleinbb8304bd2020-05-18 21:44:183402 bool start_recording,
3403 base::Optional<AccessibilityEventCallback> callback) {
3404 // Only pass a callback to RecordAccessibilityEvents when starting to record.
3405 DCHECK_EQ(start_recording, callback.has_value());
3406 if (start_recording) {
Abigail Kleinaa898742019-11-01 02:31:253407 SetAccessibilityMode(ui::AXMode::kWebContents);
3408 auto* ax_mgr = GetOrCreateRootBrowserAccessibilityManager();
3409 DCHECK(ax_mgr);
3410 base::ProcessId pid = base::Process::Current().Pid();
3411 event_recorder_ = content::AccessibilityEventRecorder::Create(
3412 ax_mgr, pid, base::StringPiece{});
Abigail Kleinbb8304bd2020-05-18 21:44:183413 event_recorder_->ListenToEvents(*callback);
Abigail Kleinaa898742019-11-01 02:31:253414 } else {
Abigail Kleinbb8304bd2020-05-18 21:44:183415 if (event_recorder_) {
3416 event_recorder_->FlushAsyncEvents();
3417 event_recorder_.reset(nullptr);
3418 }
Abigail Kleinaa898742019-11-01 02:31:253419 }
3420}
3421
dmazzonia656928e2014-09-15 20:28:543422RenderFrameHost* WebContentsImpl::GetGuestByInstanceID(
fsamuel833ee7c2015-02-13 23:40:403423 RenderFrameHost* render_frame_host,
dmazzonia656928e2014-09-15 20:28:543424 int browser_plugin_instance_id) {
3425 BrowserPluginGuestManager* guest_manager =
3426 GetBrowserContext()->GetGuestManager();
fsamuel9cbc00a2015-04-17 20:30:203427 if (!guest_manager)
3428 return nullptr;
3429
dmazzonia656928e2014-09-15 20:28:543430 WebContents* guest = guest_manager->GetGuestByInstanceID(
fsamuel2e9413d2015-02-25 01:25:443431 render_frame_host->GetProcess()->GetID(), browser_plugin_instance_id);
dmazzonia656928e2014-09-15 20:28:543432 if (!guest)
fsamuel9cbc00a2015-04-17 20:30:203433 return nullptr;
3434
dmazzonia656928e2014-09-15 20:28:543435 return guest->GetMainFrame();
3436}
3437
Ke He7319dbe2017-11-09 05:54:443438device::mojom::GeolocationContext* WebContentsImpl::GetGeolocationContext() {
Ella Ge3e375b702020-06-05 15:57:163439 if (delegate_) {
3440 auto* installed_webapp_context =
3441 delegate_->GetInstalledWebappGeolocationContext();
3442 if (installed_webapp_context)
3443 return installed_webapp_context;
3444 }
3445
Ken Rockotce010f02019-12-12 23:32:323446 if (!geolocation_context_) {
3447 GetDeviceService().BindGeolocationContext(
3448 geolocation_context_.BindNewPipeAndPassReceiver());
3449 }
Conley Owens6894c4f2017-07-10 19:29:133450 return geolocation_context_.get();
blundellc57b93f2014-10-29 13:19:573451}
3452
ke.he98b761e2017-05-09 05:59:173453device::mojom::WakeLockContext* WebContentsImpl::GetWakeLockContext() {
blundelld8cd72b2017-03-28 07:18:383454 if (!wake_lock_context_host_)
3455 wake_lock_context_host_.reset(new WakeLockContextHost(this));
blundelle75a8f92017-03-27 08:11:173456 return wake_lock_context_host_->GetWakeLockContext();
alogvinovf50445a2015-10-30 13:00:123457}
3458
blundellf5316fc2017-05-15 11:49:033459#if defined(OS_ANDROID)
Miyoung Shin59f39ff2019-09-17 07:23:203460void WebContentsImpl::GetNFC(
Reilly Grantafbe2242020-02-20 21:14:553461 RenderFrameHost* render_frame_host,
Miyoung Shin59f39ff2019-09-17 07:23:203462 mojo::PendingReceiver<device::mojom::NFC> receiver) {
Reilly Grantafbe2242020-02-20 21:14:553463 if (!nfc_host_)
3464 nfc_host_ = std::make_unique<NFCHost>(this);
3465 nfc_host_->GetNFC(render_frame_host, std::move(receiver));
blundellf5316fc2017-05-15 11:49:033466}
3467#endif
3468
Evan Stade5e750ad02017-08-07 21:59:273469void WebContentsImpl::SetNotWaitingForResponse() {
3470 if (waiting_for_response_ == false)
3471 return;
3472
3473 waiting_for_response_ = false;
3474 if (delegate_)
3475 delegate_->LoadingStateChanged(this, is_load_to_different_document_);
Chris Hamiltonf82f2a702018-02-15 19:37:293476 for (auto& observer : observers_)
3477 observer.DidReceiveResponse();
Evan Stade5e750ad02017-08-07 21:59:273478}
3479
lfgbb9c28a2016-03-01 03:19:493480void WebContentsImpl::SendScreenRects() {
kenrbc52e1b4e2016-03-20 01:03:373481 for (FrameTreeNode* node : frame_tree_.Nodes()) {
kenrb0e8dc202016-05-30 19:51:403482 if (node->current_frame_host()->is_local_root())
kenrbc52e1b4e2016-03-20 01:03:373483 node->current_frame_host()->GetRenderWidgetHost()->SendScreenRects();
3484 }
[email protected]32deec62013-05-15 23:55:043485}
3486
ekaramadadd882292016-06-08 15:22:563487TextInputManager* WebContentsImpl::GetTextInputManager() {
3488 if (GetOuterWebContents())
3489 return GetOuterWebContents()->GetTextInputManager();
3490
Ehsan Karamadc179dc02018-12-15 03:09:293491 if (!text_input_manager_ && !browser_plugin_guest_) {
Darren Shenca53d5f2018-05-15 04:56:013492 text_input_manager_.reset(new TextInputManager(
3493 GetBrowserContext() &&
3494 !GetBrowserContext()->IsOffTheRecord()) /* should_do_learning */);
3495 }
ekaramadadd882292016-06-08 15:22:563496
3497 return text_input_manager_.get();
3498}
3499
paulmeyer7f6f1d4f2016-11-15 00:00:273500bool WebContentsImpl::OnUpdateDragCursor() {
Lei Zhang6410ec1a2019-01-31 21:38:583501 return browser_plugin_embedder_ &&
3502 browser_plugin_embedder_->OnUpdateDragCursor();
paulmeyer7f6f1d4f2016-11-15 00:00:273503}
3504
kenrb11f213a2017-03-24 18:12:063505bool WebContentsImpl::IsWidgetForMainFrame(
3506 RenderWidgetHostImpl* render_widget_host) {
3507 return render_widget_host == GetMainFrame()->GetRenderWidgetHost();
3508}
3509
[email protected]95640212014-07-26 18:14:303510BrowserAccessibilityManager*
3511 WebContentsImpl::GetRootBrowserAccessibilityManager() {
Carlos ILd51e7702020-05-07 18:51:393512 RenderFrameHostImpl* rfh = static_cast<RenderFrameHostImpl*>(GetMainFrame());
creisc014b402015-04-23 16:41:453513 return rfh ? rfh->browser_accessibility_manager() : nullptr;
[email protected]95640212014-07-26 18:14:303514}
3515
3516BrowserAccessibilityManager*
3517 WebContentsImpl::GetOrCreateRootBrowserAccessibilityManager() {
Carlos ILd51e7702020-05-07 18:51:393518 RenderFrameHostImpl* rfh = static_cast<RenderFrameHostImpl*>(GetMainFrame());
creisc014b402015-04-23 16:41:453519 return rfh ? rfh->GetOrCreateBrowserAccessibilityManager() : nullptr;
[email protected]95640212014-07-26 18:14:303520}
3521
Dave Tapuskab336b922017-07-06 19:24:053522void WebContentsImpl::ExecuteEditCommand(
3523 const std::string& command,
3524 const base::Optional<base::string16>& value) {
Dave Tapuska58a099e2020-06-08 21:48:403525 auto* input_handler = GetFocusedFrameWidgetInputHandler();
Dave Tapuskae782bea2019-07-09 16:21:513526 if (!input_handler)
Dave Tapuskab336b922017-07-06 19:24:053527 return;
3528
Dave Tapuskae782bea2019-07-09 16:21:513529 input_handler->ExecuteEditCommand(command, value);
Dave Tapuskab336b922017-07-06 19:24:053530}
3531
mohsen7ab1ec16ec2015-07-02 18:26:233532void WebContentsImpl::MoveRangeSelectionExtent(const gfx::Point& extent) {
Dave Tapuska58a099e2020-06-08 21:48:403533 auto* input_handler = GetFocusedFrameWidgetInputHandler();
Dave Tapuskae782bea2019-07-09 16:21:513534 if (!input_handler)
mohsen7ab1ec16ec2015-07-02 18:26:233535 return;
3536
Dave Tapuskae782bea2019-07-09 16:21:513537 input_handler->MoveRangeSelectionExtent(extent);
mohsen7ab1ec16ec2015-07-02 18:26:233538}
3539
3540void WebContentsImpl::SelectRange(const gfx::Point& base,
3541 const gfx::Point& extent) {
Dave Tapuska58a099e2020-06-08 21:48:403542 auto* input_handler = GetFocusedFrameWidgetInputHandler();
Dave Tapuskae782bea2019-07-09 16:21:513543 if (!input_handler)
mohsen7ab1ec16ec2015-07-02 18:26:233544 return;
3545
Dave Tapuskae782bea2019-07-09 16:21:513546 input_handler->SelectRange(base, extent);
mohsen7ab1ec16ec2015-07-02 18:26:233547}
3548
Dave Tapuskab336b922017-07-06 19:24:053549void WebContentsImpl::MoveCaret(const gfx::Point& extent) {
Dave Tapuska58a099e2020-06-08 21:48:403550 auto* input_handler = GetFocusedFrameWidgetInputHandler();
Dave Tapuskae782bea2019-07-09 16:21:513551 if (!input_handler)
Dave Tapuskab336b922017-07-06 19:24:053552 return;
3553
Dave Tapuskae782bea2019-07-09 16:21:513554 input_handler->MoveCaret(extent);
Dave Tapuskab336b922017-07-06 19:24:053555}
3556
Shimi Zhang37deeb22017-09-28 00:59:013557void WebContentsImpl::AdjustSelectionByCharacterOffset(
3558 int start_adjust,
3559 int end_adjust,
3560 bool show_selection_menu) {
Dave Tapuska58a099e2020-06-08 21:48:403561 auto* input_handler = GetFocusedFrameWidgetInputHandler();
Dave Tapuskae782bea2019-07-09 16:21:513562 if (!input_handler)
aurimasab0319022015-07-10 21:57:383563 return;
3564
Shimi Zhang37deeb22017-09-28 00:59:013565 using blink::mojom::SelectionMenuBehavior;
Dave Tapuskae782bea2019-07-09 16:21:513566 input_handler->AdjustSelectionByCharacterOffset(
Shimi Zhang37deeb22017-09-28 00:59:013567 start_adjust, end_adjust,
3568 show_selection_menu ? SelectionMenuBehavior::kShow
3569 : SelectionMenuBehavior::kHide);
aurimasab0319022015-07-10 21:57:383570}
3571
[email protected]b172aee2012-04-10 17:05:263572void WebContentsImpl::UpdatePreferredSize(const gfx::Size& pref_size) {
[email protected]222f5822014-02-05 23:40:493573 const gfx::Size old_size = GetPreferredSize();
[email protected]bcd2815602012-01-14 18:17:233574 preferred_size_ = pref_size;
[email protected]222f5822014-02-05 23:40:493575 OnPreferredSizeChanged(old_size);
[email protected]0548c5352011-09-07 00:33:333576}
3577
avic3aa8422015-11-09 20:57:223578void WebContentsImpl::ResizeDueToAutoResize(
3579 RenderWidgetHostImpl* render_widget_host,
Fady Samuel97947112018-05-05 16:24:543580 const gfx::Size& new_size) {
avic3aa8422015-11-09 20:57:223581 if (render_widget_host != GetRenderViewHost()->GetWidget())
3582 return;
3583
[email protected]61e2b3cc2012-03-02 16:13:343584 if (delegate_)
3585 delegate_->ResizeDueToAutoResize(this, new_size);
3586}
3587
[email protected]b172aee2012-04-10 17:05:263588WebContents* WebContentsImpl::OpenURL(const OpenURLParams& params) {
Aaron Colwell9dab1652019-12-09 18:29:493589#if DCHECK_IS_ON()
3590 DCHECK(params.Valid());
3591#endif
3592
Bo Liu300c6052018-06-12 04:46:073593 if (!delegate_) {
3594 // Embedder can delay setting a delegate on new WebContents with
3595 // WebContentsDelegate::ShouldResumeRequestsForCreatedWindow. In the mean
3596 // time, navigations, including the initial one, that goes through OpenURL
3597 // should be delayed until embedder is ready to resume loading.
3598 delayed_open_url_params_ = std::make_unique<OpenURLParams>(params);
Nasko Oskov83a8cf92018-10-19 14:58:563599
3600 // If there was a navigation deferred when creating the window through
3601 // CreateNewWindow, drop it in favor of this navigation.
3602 delayed_load_url_params_.reset();
3603
Ivan Kotenkov2c0d2bb32017-11-01 15:41:283604 return nullptr;
Bo Liu300c6052018-06-12 04:46:073605 }
[email protected]00c37fc2011-08-02 00:22:503606
[email protected]e5d549d2011-12-28 01:29:203607 WebContents* new_contents = delegate_->OpenURLFromTab(this, params);
pnoland488944652017-02-22 18:58:543608
3609 RenderFrameHost* source_render_frame_host = RenderFrameHost::FromID(
3610 params.source_render_process_id, params.source_render_frame_id);
3611
3612 if (source_render_frame_host && params.source_site_instance) {
3613 CHECK_EQ(source_render_frame_host->GetSiteInstance(),
3614 params.source_site_instance.get());
3615 }
3616
3617 if (new_contents && source_render_frame_host && new_contents != this) {
3618 for (auto& observer : observers_) {
Lukasz Anforowicz48c9f872018-08-01 00:58:413619 observer.DidOpenRequestedURL(
3620 new_contents, source_render_frame_host, params.url, params.referrer,
3621 params.disposition, params.transition,
3622 params.started_from_context_menu, params.is_renderer_initiated);
pnoland488944652017-02-22 18:58:543623 }
3624 }
3625
[email protected]e5d549d2011-12-28 01:29:203626 return new_contents;
[email protected]d5f942ba2008-09-26 19:30:343627}
3628
avi2b177592014-12-10 02:08:023629void WebContentsImpl::SetHistoryOffsetAndLength(int history_offset,
3630 int history_length) {
alexmos136fd6e62016-08-15 20:58:413631 SendPageMessage(new PageMsg_SetHistoryOffsetAndLength(
3632 MSG_ROUTING_NONE, history_offset, history_length));
avi2b177592014-12-10 02:08:023633}
3634
3635void WebContentsImpl::SetHistoryOffsetAndLengthForView(
3636 RenderViewHost* render_view_host,
3637 int history_offset,
3638 int history_length) {
alexmos136fd6e62016-08-15 20:58:413639 render_view_host->Send(new PageMsg_SetHistoryOffsetAndLength(
avi2b177592014-12-10 02:08:023640 render_view_host->GetRoutingID(), history_offset, history_length));
[email protected]796931a92011-08-10 01:32:143641}
3642
arthursonzogni818c2642019-09-27 12:18:103643void WebContentsImpl::ReloadFocusedFrame() {
[email protected]1f3fc1d2014-04-03 14:50:173644 RenderFrameHost* focused_frame = GetFocusedFrame();
3645 if (!focused_frame)
3646 return;
3647
yilkal921048bd2019-10-09 23:51:043648 focused_frame->Reload();
[email protected]959be4c2014-04-08 15:01:333649}
3650
[email protected]959be4c2014-04-08 15:01:333651void WebContentsImpl::Undo() {
Dave Tapuska58a099e2020-06-08 21:48:403652 auto* input_handler = GetFocusedFrameWidgetInputHandler();
Dave Tapuskae782bea2019-07-09 16:21:513653 if (!input_handler)
[email protected]959be4c2014-04-08 15:01:333654 return;
3655
Dave Tapuskae782bea2019-07-09 16:21:513656 input_handler->Undo();
[email protected]959be4c2014-04-08 15:01:333657 RecordAction(base::UserMetricsAction("Undo"));
3658}
3659
3660void WebContentsImpl::Redo() {
Dave Tapuska58a099e2020-06-08 21:48:403661 auto* input_handler = GetFocusedFrameWidgetInputHandler();
Dave Tapuskae782bea2019-07-09 16:21:513662 if (!input_handler)
[email protected]959be4c2014-04-08 15:01:333663 return;
Dave Tapuskae782bea2019-07-09 16:21:513664
3665 input_handler->Redo();
[email protected]959be4c2014-04-08 15:01:333666 RecordAction(base::UserMetricsAction("Redo"));
3667}
3668
3669void WebContentsImpl::Cut() {
Dave Tapuska58a099e2020-06-08 21:48:403670 auto* input_handler = GetFocusedFrameWidgetInputHandler();
Dave Tapuskae782bea2019-07-09 16:21:513671 if (!input_handler)
[email protected]959be4c2014-04-08 15:01:333672 return;
3673
Dave Tapuskae782bea2019-07-09 16:21:513674 input_handler->Cut();
[email protected]959be4c2014-04-08 15:01:333675 RecordAction(base::UserMetricsAction("Cut"));
3676}
3677
3678void WebContentsImpl::Copy() {
Dave Tapuska58a099e2020-06-08 21:48:403679 auto* input_handler = GetFocusedFrameWidgetInputHandler();
Dave Tapuskae782bea2019-07-09 16:21:513680 if (!input_handler)
[email protected]959be4c2014-04-08 15:01:333681 return;
3682
Dave Tapuskae782bea2019-07-09 16:21:513683 input_handler->Copy();
[email protected]959be4c2014-04-08 15:01:333684 RecordAction(base::UserMetricsAction("Copy"));
3685}
3686
3687void WebContentsImpl::CopyToFindPboard() {
3688#if defined(OS_MACOSX)
Dave Tapuska58a099e2020-06-08 21:48:403689 auto* input_handler = GetFocusedFrameWidgetInputHandler();
Dave Tapuskae782bea2019-07-09 16:21:513690 if (!input_handler)
[email protected]959be4c2014-04-08 15:01:333691 return;
3692
3693 // Windows/Linux don't have the concept of a find pasteboard.
Dave Tapuskae782bea2019-07-09 16:21:513694 input_handler->CopyToFindPboard();
[email protected]959be4c2014-04-08 15:01:333695 RecordAction(base::UserMetricsAction("CopyToFindPboard"));
3696#endif
3697}
3698
3699void WebContentsImpl::Paste() {
Dave Tapuska58a099e2020-06-08 21:48:403700 auto* input_handler = GetFocusedFrameWidgetInputHandler();
Dave Tapuskae782bea2019-07-09 16:21:513701 if (!input_handler)
[email protected]959be4c2014-04-08 15:01:333702 return;
3703
Dave Tapuskae782bea2019-07-09 16:21:513704 input_handler->Paste();
Archana Simhaf67090c2019-05-06 23:43:233705 for (auto& observer : observers_)
3706 observer.OnPaste();
[email protected]959be4c2014-04-08 15:01:333707 RecordAction(base::UserMetricsAction("Paste"));
3708}
3709
3710void WebContentsImpl::PasteAndMatchStyle() {
Dave Tapuska58a099e2020-06-08 21:48:403711 auto* input_handler = GetFocusedFrameWidgetInputHandler();
Dave Tapuskae782bea2019-07-09 16:21:513712 if (!input_handler)
[email protected]959be4c2014-04-08 15:01:333713 return;
3714
Dave Tapuskae782bea2019-07-09 16:21:513715 input_handler->PasteAndMatchStyle();
Archana Simhaf67090c2019-05-06 23:43:233716 for (auto& observer : observers_)
3717 observer.OnPaste();
[email protected]959be4c2014-04-08 15:01:333718 RecordAction(base::UserMetricsAction("PasteAndMatchStyle"));
3719}
3720
3721void WebContentsImpl::Delete() {
Dave Tapuska58a099e2020-06-08 21:48:403722 auto* input_handler = GetFocusedFrameWidgetInputHandler();
Dave Tapuskae782bea2019-07-09 16:21:513723 if (!input_handler)
[email protected]959be4c2014-04-08 15:01:333724 return;
3725
Dave Tapuskae782bea2019-07-09 16:21:513726 input_handler->Delete();
[email protected]959be4c2014-04-08 15:01:333727 RecordAction(base::UserMetricsAction("DeleteSelection"));
3728}
3729
3730void WebContentsImpl::SelectAll() {
Dave Tapuska58a099e2020-06-08 21:48:403731 auto* input_handler = GetFocusedFrameWidgetInputHandler();
Dave Tapuskae782bea2019-07-09 16:21:513732 if (!input_handler)
[email protected]959be4c2014-04-08 15:01:333733 return;
3734
Dave Tapuskae782bea2019-07-09 16:21:513735 input_handler->SelectAll();
[email protected]959be4c2014-04-08 15:01:333736 RecordAction(base::UserMetricsAction("SelectAll"));
3737}
3738
yabinh351e7ec2017-03-10 02:43:243739void WebContentsImpl::CollapseSelection() {
Dave Tapuska58a099e2020-06-08 21:48:403740 auto* input_handler = GetFocusedFrameWidgetInputHandler();
Dave Tapuskae782bea2019-07-09 16:21:513741 if (!input_handler)
[email protected]959be4c2014-04-08 15:01:333742 return;
3743
Dave Tapuskae782bea2019-07-09 16:21:513744 input_handler->CollapseSelection();
[email protected]1f3fc1d2014-04-03 14:50:173745}
3746
3747void WebContentsImpl::Replace(const base::string16& word) {
Dave Tapuska58a099e2020-06-08 21:48:403748 auto* input_handler = GetFocusedFrameWidgetInputHandler();
Dave Tapuskae782bea2019-07-09 16:21:513749 if (!input_handler)
[email protected]1f3fc1d2014-04-03 14:50:173750 return;
3751
Dave Tapuskae782bea2019-07-09 16:21:513752 input_handler->Replace(word);
[email protected]1f3fc1d2014-04-03 14:50:173753}
3754
3755void WebContentsImpl::ReplaceMisspelling(const base::string16& word) {
Dave Tapuska58a099e2020-06-08 21:48:403756 auto* input_handler = GetFocusedFrameWidgetInputHandler();
Dave Tapuskae782bea2019-07-09 16:21:513757 if (!input_handler)
[email protected]1f3fc1d2014-04-03 14:50:173758 return;
3759
Dave Tapuskae782bea2019-07-09 16:21:513760 input_handler->ReplaceMisspelling(word);
[email protected]959be4c2014-04-08 15:01:333761}
3762
3763void WebContentsImpl::NotifyContextMenuClosed(
3764 const CustomContextMenuContext& context) {
3765 RenderFrameHost* focused_frame = GetFocusedFrame();
3766 if (!focused_frame)
3767 return;
3768
3769 focused_frame->Send(new FrameMsg_ContextMenuClosed(
3770 focused_frame->GetRoutingID(), context));
3771}
3772
3773void WebContentsImpl::ExecuteCustomContextMenuCommand(
3774 int action, const CustomContextMenuContext& context) {
3775 RenderFrameHost* focused_frame = GetFocusedFrame();
3776 if (!focused_frame)
3777 return;
3778
3779 focused_frame->Send(new FrameMsg_CustomContextMenuAction(
3780 focused_frame->GetRoutingID(), context, action));
[email protected]4fed3702014-04-01 09:08:003781}
3782
[email protected]fc2b46b2014-05-03 16:33:453783gfx::NativeView WebContentsImpl::GetNativeView() {
3784 return view_->GetNativeView();
3785}
3786
3787gfx::NativeView WebContentsImpl::GetContentNativeView() {
3788 return view_->GetContentNativeView();
3789}
3790
3791gfx::NativeWindow WebContentsImpl::GetTopLevelNativeWindow() {
3792 return view_->GetTopLevelNativeWindow();
3793}
3794
3795gfx::Rect WebContentsImpl::GetViewBounds() {
3796 return view_->GetViewBounds();
3797}
3798
3799gfx::Rect WebContentsImpl::GetContainerBounds() {
Jeremy Roman352239c2020-05-21 16:31:323800 return view_->GetContainerBounds();
[email protected]fc2b46b2014-05-03 16:33:453801}
3802
3803DropData* WebContentsImpl::GetDropData() {
3804 return view_->GetDropData();
3805}
3806
3807void WebContentsImpl::Focus() {
miu9e14e492014-10-25 02:39:043808 view_->Focus();
[email protected]fc2b46b2014-05-03 16:33:453809}
3810
3811void WebContentsImpl::SetInitialFocus() {
miu9e14e492014-10-25 02:39:043812 view_->SetInitialFocus();
[email protected]fc2b46b2014-05-03 16:33:453813}
3814
3815void WebContentsImpl::StoreFocus() {
miu9e14e492014-10-25 02:39:043816 view_->StoreFocus();
[email protected]fc2b46b2014-05-03 16:33:453817}
3818
3819void WebContentsImpl::RestoreFocus() {
miu9e14e492014-10-25 02:39:043820 view_->RestoreFocus();
[email protected]fc2b46b2014-05-03 16:33:453821}
3822
[email protected]b172aee2012-04-10 17:05:263823void WebContentsImpl::FocusThroughTabTraversal(bool reverse) {
Maxim Podgorodskiya926c4ff2017-11-20 02:06:393824 view_->FocusThroughTabTraversal(reverse);
[email protected]96d185d2009-04-24 03:28:543825}
3826
[email protected]b172aee2012-04-10 17:05:263827bool WebContentsImpl::IsSavable() {
[email protected]a53209b2012-01-20 16:48:163828 // WebKit creates Document object when MIME type is application/xhtml+xml,
3829 // so we also support this MIME type.
3830 return contents_mime_type_ == "text/html" ||
3831 contents_mime_type_ == "text/xml" ||
3832 contents_mime_type_ == "application/xhtml+xml" ||
3833 contents_mime_type_ == "text/plain" ||
3834 contents_mime_type_ == "text/css" ||
Kinuko Yasuda74702f92017-07-31 03:27:533835 blink::IsSupportedJavascriptMimeType(contents_mime_type_);
[email protected]a53209b2012-01-20 16:48:163836}
3837
[email protected]b172aee2012-04-10 17:05:263838void WebContentsImpl::OnSavePage() {
[email protected]c7dd2f62011-07-18 15:57:593839 // If we can not save the page, try to download it.
[email protected]a53209b2012-01-20 16:48:163840 if (!IsSavable()) {
Min Qinda0ed2062018-02-23 22:00:533841 download::RecordSavePackageEvent(
3842 download::SAVE_PACKAGE_DOWNLOAD_ON_NON_HTML);
nasko89ad7742015-03-05 22:14:193843 SaveFrame(GetLastCommittedURL(), Referrer());
[email protected]27678b2a2012-02-04 22:09:143844 return;
[email protected]c7dd2f62011-07-18 15:57:593845 }
3846
3847 Stop();
3848
3849 // Create the save package and possibly prompt the user for the name to save
3850 // the page as. The user prompt is an asynchronous operation that runs on
3851 // another thread.
3852 save_package_ = new SavePackage(this);
3853 save_package_->GetSaveInfo();
3854}
3855
3856// Used in automated testing to bypass prompting the user for file names.
3857// Instead, the names and paths are hard coded rather than running them through
3858// file name sanitation and extension / mime checking.
[email protected]2dec8ec2013-02-07 19:20:343859bool WebContentsImpl::SavePage(const base::FilePath& main_file,
3860 const base::FilePath& dir_path,
[email protected]8ff00d72012-10-23 19:12:213861 SavePageType save_type) {
[email protected]c7dd2f62011-07-18 15:57:593862 // Stop the page from navigating.
3863 Stop();
3864
3865 save_package_ = new SavePackage(this, save_type, main_file, dir_path);
[email protected]8ff00d72012-10-23 19:12:213866 return save_package_->Init(SavePackageDownloadCreatedCallback());
[email protected]c7dd2f62011-07-18 15:57:593867}
3868
[email protected]3c71576ce2013-07-23 02:00:013869void WebContentsImpl::SaveFrame(const GURL& url,
3870 const Referrer& referrer) {
Xing Liu10329bf2018-03-20 19:22:143871 SaveFrameWithHeaders(url, referrer, std::string(), base::string16());
kundaji6c7f9692015-03-09 18:00:373872}
3873
Xing Liu10329bf2018-03-20 19:22:143874void WebContentsImpl::SaveFrameWithHeaders(
3875 const GURL& url,
3876 const Referrer& referrer,
3877 const std::string& headers,
3878 const base::string16& suggested_filename) {
Lei Zhang13a42e42019-02-19 23:28:113879 // Check and see if the guest can handle this.
3880 if (delegate_) {
3881 WebContents* guest_web_contents = nullptr;
3882 if (browser_plugin_embedder_) {
3883 BrowserPluginGuest* guest = browser_plugin_embedder_->GetFullPageGuest();
3884 if (guest)
3885 guest_web_contents = guest->GetWebContents();
3886 } else if (browser_plugin_guest_) {
3887 guest_web_contents = this;
3888 }
3889
3890 if (guest_web_contents && delegate_->GuestSaveFrame(guest_web_contents))
3891 return;
3892 }
3893
nasko89ad7742015-03-05 22:14:193894 if (!GetLastCommittedURL().is_valid())
[email protected]3c71576ce2013-07-23 02:00:013895 return;
sammc92af61552014-11-19 23:27:403896 if (delegate_ && delegate_->SaveFrame(url, referrer))
3897 return;
3898
nasko89ad7742015-03-05 22:14:193899 // TODO(nasko): This check for main frame is incorrect and should be fixed
brettw760f7442016-05-23 21:19:223900 // by explicitly passing in which frame this method should target. This would
3901 // indicate whether it's the main frame, and also tell us the frame pointer
3902 // to use for routing.
nasko89ad7742015-03-05 22:14:193903 bool is_main_frame = (url == GetLastCommittedURL());
brettw760f7442016-05-23 21:19:223904 RenderFrameHost* frame_host = GetMainFrame();
[email protected]3c71576ce2013-07-23 02:00:013905
avib7348942015-12-25 20:57:103906 int64_t post_id = -1;
[email protected]3c71576ce2013-07-23 02:00:013907 if (is_main_frame) {
Lucas Furukawa Gadani5553a152019-01-08 18:55:573908 NavigationEntry* entry = controller_.GetLastCommittedEntry();
[email protected]3c71576ce2013-07-23 02:00:013909 if (entry)
3910 post_id = entry->GetPostID();
3911 }
Ramin Halavati24e21012017-07-10 12:56:063912 net::NetworkTrafficAnnotationTag traffic_annotation =
3913 net::DefineNetworkTrafficAnnotation("download_web_contents_frame", R"(
3914 semantics {
3915 sender: "Save Page Action"
3916 description:
3917 "Saves the given frame's URL to the local file system."
3918 trigger:
3919 "The user has triggered a save operation on the frame through a "
3920 "context menu or other mechanism."
3921 data: "None."
3922 destination: WEBSITE
3923 }
3924 policy {
Ramin Halavati3b979782017-07-21 11:40:263925 cookies_allowed: YES
Ramin Halavati24e21012017-07-10 12:56:063926 cookies_store: "user"
3927 setting:
3928 "This feature cannot be disabled by settings, but it's is only "
3929 "triggered by user request."
3930 policy_exception_justification: "Not implemented."
3931 })");
Min Qina904f3302018-02-13 23:33:343932 auto params = std::make_unique<download::DownloadUrlParameters>(
brettw760f7442016-05-23 21:19:223933 url, frame_host->GetProcess()->GetID(),
3934 frame_host->GetRenderViewHost()->GetRoutingID(),
Matt Menke0dcaebc32020-03-06 17:00:103935 frame_host->GetRoutingID(), traffic_annotation);
Min Qina904f3302018-02-13 23:33:343936 params->set_referrer(referrer.url);
3937 params->set_referrer_policy(
3938 Referrer::ReferrerPolicyForUrlRequest(referrer.policy));
[email protected]3c71576ce2013-07-23 02:00:013939 params->set_post_id(post_id);
[email protected]3c71576ce2013-07-23 02:00:013940 if (post_id >= 0)
3941 params->set_method("POST");
3942 params->set_prompt(true);
kundaji6c7f9692015-03-09 18:00:373943
3944 if (headers.empty()) {
3945 params->set_prefer_cache(true);
3946 } else {
Min Qina904f3302018-02-13 23:33:343947 for (download::DownloadUrlParameters::RequestHeadersNameValuePair
3948 key_value : ParseDownloadHeaders(headers)) {
Megan Jablonski2f6a4c52017-07-10 23:01:253949 params->add_request_header(key_value.first, key_value.second);
kundaji6c7f9692015-03-09 18:00:373950 }
3951 }
Xing Liu10329bf2018-03-20 19:22:143952 params->set_suggested_name(suggested_filename);
Min Qin0ca8e1ee2018-01-31 00:49:353953 params->set_download_source(download::DownloadSource::WEB_CONTENTS_API);
thestig859c78892017-05-15 21:17:063954 BrowserContext::GetDownloadManager(GetBrowserContext())
Ramin Halavati03efa732017-06-12 09:28:203955 ->DownloadUrl(std::move(params));
[email protected]3c71576ce2013-07-23 02:00:013956}
3957
[email protected]b172aee2012-04-10 17:05:263958void WebContentsImpl::GenerateMHTML(
dewittj6dc5747a2016-05-17 01:48:473959 const MHTMLGenerationParams& params,
Avi Drissman149b7832018-03-23 14:31:493960 base::OnceCallback<void(int64_t)> callback) {
Angel Alvarez902496682019-08-27 22:58:423961 base::OnceCallback<void(const MHTMLGenerationResult&)> wrapper_callback =
3962 base::BindOnce(
3963 [](base::OnceCallback<void(int64_t)> size_callback,
3964 const MHTMLGenerationResult& result) {
3965 std::move(size_callback).Run(result.file_size);
3966 },
3967 std::move(callback));
3968 MHTMLGenerationManager::GetInstance()->SaveMHTML(this, params,
3969 std::move(wrapper_callback));
3970}
3971
3972void WebContentsImpl::GenerateMHTMLWithResult(
3973 const MHTMLGenerationParams& params,
3974 MHTMLGenerationResult::GenerateMHTMLCallback callback) {
Avi Drissman149b7832018-03-23 14:31:493975 MHTMLGenerationManager::GetInstance()->SaveMHTML(this, params,
3976 std::move(callback));
[email protected]aa4f3972012-03-01 18:12:123977}
3978
Tsuyoshi Horob0d8d902020-03-12 00:09:393979void WebContentsImpl::GenerateWebBundle(
3980 const base::FilePath& file_path,
3981 base::OnceCallback<void(uint64_t /* file_size */,
3982 data_decoder::mojom::WebBundlerError)> callback) {
3983 SaveAsWebBundleJob::Start(this, file_path, std::move(callback));
3984}
3985
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:413986const std::string& WebContentsImpl::GetContentsMimeType() {
[email protected]0bfbf882011-12-22 18:19:273987 return contents_mime_type_;
3988}
3989
Leon Hanc819dc62019-01-28 04:30:193990blink::mojom::RendererPreferences* WebContentsImpl::GetMutableRendererPrefs() {
[email protected]0bfbf882011-12-22 18:19:273991 return &renderer_preferences_;
3992}
3993
[email protected]e35ccd52012-05-23 16:22:473994void WebContentsImpl::Close() {
3995 Close(GetRenderViewHost());
3996}
3997
Ella Ge80a52dce2017-11-15 18:01:523998void WebContentsImpl::DragSourceEndedAt(float client_x,
3999 float client_y,
4000 float screen_x,
4001 float screen_y,
Paul Meyer0c58c3712016-11-17 22:59:514002 blink::WebDragOperation operation,
4003 RenderWidgetHost* source_rwh) {
Lei Zhang6410ec1a2019-01-31 21:38:584004 if (browser_plugin_embedder_) {
W. James MacLean9437b5bf2019-12-06 17:33:444005 browser_plugin_embedder_->DragSourceEndedAt(client_x, client_y, screen_x,
4006 screen_y, operation);
Lei Zhang6410ec1a2019-01-31 21:38:584007 }
Paul Meyer0c58c3712016-11-17 22:59:514008 if (source_rwh) {
Ella Ge80a52dce2017-11-15 18:01:524009 source_rwh->DragSourceEndedAt(gfx::PointF(client_x, client_y),
4010 gfx::PointF(screen_x, screen_y), operation);
Paul Meyer0c58c3712016-11-17 22:59:514011 }
[email protected]cf200a562013-05-03 16:24:294012}
4013
jam73f892642016-09-11 06:04:064014void WebContentsImpl::LoadStateChanged(
Charles Harrison911fa062018-03-06 21:01:464015 const std::string& host,
jam73f892642016-09-11 06:04:064016 const net::LoadStateWithParam& load_state,
4017 uint64_t upload_position,
4018 uint64_t upload_size) {
Charles Harrison911fa062018-03-06 21:01:464019 base::string16 host16 = url_formatter::IDNToUnicode(host);
Elly Fong-Jonescad9c3d12018-01-31 17:26:104020 // Drop no-op updates.
4021 if (load_state_.state == load_state.state &&
4022 load_state_.param == load_state.param &&
4023 upload_position_ == upload_position && upload_size_ == upload_size &&
Charles Harrison911fa062018-03-06 21:01:464024 load_state_host_ == host16) {
Elly Fong-Jonescad9c3d12018-01-31 17:26:104025 return;
4026 }
jam73f892642016-09-11 06:04:064027 load_state_ = load_state;
4028 upload_position_ = upload_position;
4029 upload_size_ = upload_size;
Charles Harrison911fa062018-03-06 21:01:464030 load_state_host_ = host16;
jam73f892642016-09-11 06:04:064031 if (load_state_.state == net::LOAD_STATE_READING_RESPONSE)
4032 SetNotWaitingForResponse();
4033 if (IsLoading()) {
4034 NotifyNavigationStateChanged(static_cast<InvalidateTypes>(
4035 INVALIDATE_TYPE_LOAD | INVALIDATE_TYPE_TAB));
4036 }
4037}
4038
Collin Baker989e0882019-11-01 01:27:174039void WebContentsImpl::SetVisibilityAndNotifyObservers(Visibility visibility) {
Francois Doraye6161152018-03-27 22:05:374040 const Visibility previous_visibility = visibility_;
4041 visibility_ = visibility;
4042
Francois Dorayfe4a1772018-02-17 04:17:094043 // Notify observers if the visibility changed or if WasShown() is being called
4044 // for the first time.
Francois Dorayfe4a1772018-02-17 04:17:094045 if (visibility != previous_visibility ||
4046 (visibility == Visibility::VISIBLE && !did_first_set_visible_)) {
4047 for (auto& observer : observers_)
4048 observer.OnVisibilityChanged(visibility);
4049 }
4050}
4051
Michael Thiessen896405db2017-07-20 17:52:324052void WebContentsImpl::NotifyWebContentsFocused(
4053 RenderWidgetHost* render_widget_host) {
ericwilligersde386342016-10-19 02:35:094054 for (auto& observer : observers_)
Michael Thiessen896405db2017-07-20 17:52:324055 observer.OnWebContentsFocused(render_widget_host);
calamity7fe55ce2015-04-10 03:59:374056}
4057
Michael Thiessen896405db2017-07-20 17:52:324058void WebContentsImpl::NotifyWebContentsLostFocus(
4059 RenderWidgetHost* render_widget_host) {
zijiehe3bee08e22017-05-17 04:14:464060 for (auto& observer : observers_)
Michael Thiessen896405db2017-07-20 17:52:324061 observer.OnWebContentsLostFocus(render_widget_host);
zijiehe3bee08e22017-05-17 04:14:464062}
4063
Paul Meyer0c58c3712016-11-17 22:59:514064void WebContentsImpl::SystemDragEnded(RenderWidgetHost* source_rwh) {
4065 if (source_rwh)
4066 source_rwh->DragSourceSystemDragEnded();
Lei Zhang6410ec1a2019-01-31 21:38:584067 if (browser_plugin_embedder_)
[email protected]cf200a562013-05-03 16:24:294068 browser_plugin_embedder_->SystemDragEnded();
[email protected]7813bd72011-02-05 02:19:344069}
4070
[email protected]b172aee2012-04-10 17:05:264071void WebContentsImpl::SetClosedByUserGesture(bool value) {
[email protected]0bfbf882011-12-22 18:19:274072 closed_by_user_gesture_ = value;
4073}
4074
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:414075bool WebContentsImpl::GetClosedByUserGesture() {
[email protected]0bfbf882011-12-22 18:19:274076 return closed_by_user_gesture_;
4077}
4078
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:414079int WebContentsImpl::GetMinimumZoomPercent() {
[email protected]0bfbf882011-12-22 18:19:274080 return minimum_zoom_percent_;
4081}
4082
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:414083int WebContentsImpl::GetMaximumZoomPercent() {
[email protected]0bfbf882011-12-22 18:19:274084 return maximum_zoom_percent_;
4085}
4086
Dave Tapuska8f7c3872020-02-07 01:16:384087void WebContentsImpl::SetPageScale(float scale_factor) {
4088 GetMainFrame()->GetAssociatedLocalMainFrame()->SetScaleFactor(scale_factor);
ccameronb7c1d6c2015-03-09 17:08:244089}
4090
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:414091gfx::Size WebContentsImpl::GetPreferredSize() {
Francois Doray24fc62c2017-12-11 17:27:334092 return IsBeingCaptured() ? preferred_size_for_capture_ : preferred_size_;
[email protected]bcd2815602012-01-14 18:17:234093}
4094
James Hollyerd5c9de462020-03-10 19:02:454095bool WebContentsImpl::GotResponseToLockMouseRequest(
4096 blink::mojom::PointerLockResult result) {
lfgf0cd46e2017-01-04 00:05:234097 if (mouse_lock_widget_) {
4098 if (mouse_lock_widget_->delegate()->GetAsWebContents() != this) {
4099 return mouse_lock_widget_->delegate()
4100 ->GetAsWebContents()
James Hollyerd5c9de462020-03-10 19:02:454101 ->GotResponseToLockMouseRequest(result);
lfgf0cd46e2017-01-04 00:05:234102 }
lfgad8244352016-07-13 16:55:514103
James Hollyerd5c9de462020-03-10 19:02:454104 if (mouse_lock_widget_->GotResponseToLockMouseRequest(result))
lfgf0cd46e2017-01-04 00:05:234105 return true;
4106 }
4107
4108 for (WebContentsImpl* current = this; current;
4109 current = current->GetOuterWebContents()) {
4110 current->mouse_lock_widget_ = nullptr;
4111 }
4112
lfgbee1e0a2016-06-08 21:24:214113 return false;
[email protected]0bfbf882011-12-22 18:19:274114}
4115
James Hollyerd5c9de462020-03-10 19:02:454116void WebContentsImpl::GotLockMousePermissionResponse(bool allowed) {
4117 GotResponseToLockMouseRequest(
4118 allowed ? blink::mojom::PointerLockResult::kSuccess
4119 : blink::mojom::PointerLockResult::kPermissionDenied);
4120}
4121
Joe Downing13dd76b2018-04-09 18:32:154122bool WebContentsImpl::GotResponseToKeyboardLockRequest(bool allowed) {
4123 if (!keyboard_lock_widget_)
4124 return false;
4125
4126 if (keyboard_lock_widget_->delegate()->GetAsWebContents() != this) {
4127 NOTREACHED();
4128 return false;
4129 }
4130
4131 // KeyboardLock is only supported when called by the top-level browsing
4132 // context and is not supported in embedded content scenarios.
4133 if (GetOuterWebContents())
4134 return false;
4135
4136 keyboard_lock_widget_->GotResponseToKeyboardLockRequest(allowed);
4137 return true;
4138}
4139
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:414140bool WebContentsImpl::HasOpener() {
Ivan Kotenkov2c0d2bb32017-11-01 15:41:284141 return GetOpener() != nullptr;
[email protected]a0358d72012-03-09 14:06:504142}
4143
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:414144RenderFrameHostImpl* WebContentsImpl::GetOpener() {
alexmose201c7cd2015-06-10 17:14:214145 FrameTreeNode* opener_ftn = frame_tree_.root()->opener();
lukasza6f8ac622017-06-06 03:10:204146 return opener_ftn ? opener_ftn->current_frame_host() : nullptr;
jochen55ff3502014-12-18 20:52:574147}
4148
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:414149bool WebContentsImpl::HasOriginalOpener() {
Ivan Kotenkov2c0d2bb32017-11-01 15:41:284150 return GetOriginalOpener() != nullptr;
jochen6004a362017-02-04 00:11:404151}
4152
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:414153RenderFrameHostImpl* WebContentsImpl::GetOriginalOpener() {
jochen6004a362017-02-04 00:11:404154 FrameTreeNode* opener_ftn = frame_tree_.root()->original_opener();
lukasza6f8ac622017-06-06 03:10:204155 return opener_ftn ? opener_ftn->current_frame_host() : nullptr;
jochen6004a362017-02-04 00:11:404156}
4157
[email protected]cb805452013-05-22 15:16:214158void WebContentsImpl::DidChooseColorInColorChooser(SkColor color) {
Joel Hockey85b379d2017-12-11 10:42:134159 color_chooser_->DidChooseColorInColorChooser(color);
[email protected]da8543762012-03-20 08:52:204160}
4161
[email protected]cb805452013-05-22 15:16:214162void WebContentsImpl::DidEndColorChooser() {
Joel Hockey85b379d2017-12-11 10:42:134163 color_chooser_.reset();
[email protected]da8543762012-03-20 08:52:204164}
4165
halton.huoca2eabd2015-07-06 08:17:404166int WebContentsImpl::DownloadImage(
4167 const GURL& url,
4168 bool is_favicon,
Fredrik Söderquistb2b39eb92019-11-18 16:16:504169 uint32_t preferred_size,
halton.huoca2eabd2015-07-06 08:17:404170 uint32_t max_bitmap_size,
4171 bool bypass_cache,
Avi Drissmana5a52dd2018-03-27 03:39:024172 WebContents::ImageDownloadCallback callback) {
Danyao Wanga78f3dd22020-03-05 05:31:274173 return DownloadImageInFrame(GlobalFrameRoutingId(), url, is_favicon,
4174 preferred_size, max_bitmap_size, bypass_cache,
4175 std::move(callback));
4176}
4177
4178int WebContentsImpl::DownloadImageInFrame(
4179 const GlobalFrameRoutingId& initiator_frame_routing_id,
4180 const GURL& url,
4181 bool is_favicon,
4182 uint32_t preferred_size,
4183 uint32_t max_bitmap_size,
4184 bool bypass_cache,
4185 WebContents::ImageDownloadCallback callback) {
amistry9f01b772015-07-29 01:54:554186 DCHECK_CURRENTLY_ON(BrowserThread::UI);
halton.huoca2eabd2015-07-06 08:17:404187 static int next_image_download_id = 0;
Danyao Wanga78f3dd22020-03-05 05:31:274188
4189 RenderFrameHostImpl* initiator_frame =
4190 initiator_frame_routing_id.child_id
4191 ? RenderFrameHostImpl::FromID(initiator_frame_routing_id)
4192 : GetMainFrame();
Julie Jeongeun Kimb1473572019-08-15 05:59:134193 const mojo::Remote<blink::mojom::ImageDownloader>& mojo_image_downloader =
Danyao Wanga78f3dd22020-03-05 05:31:274194 initiator_frame->GetMojoImageDownloader();
amistry9f01b772015-07-29 01:54:554195 const int download_id = ++next_image_download_id;
4196 if (!mojo_image_downloader) {
4197 // If the renderer process is dead (i.e. crash, or memory pressure on
4198 // Android), the downloader service will be invalid. Pre-Mojo, this would
Avi Drissmana5a52dd2018-03-27 03:39:024199 // hang the callback indefinitely since the IPC would be dropped. Now,
amistry9f01b772015-07-29 01:54:554200 // respond with a 400 HTTP error code to indicate that something went wrong.
Gabriel Charettee7cdc5cd2020-05-27 23:35:054201 GetUIThreadTaskRunner({})->PostTask(
4202 FROM_HERE,
tzike2aca992017-09-05 08:50:544203 base::BindOnce(&WebContentsImpl::OnDidDownloadImage,
Avi Drissmana5a52dd2018-03-27 03:39:024204 weak_factory_.GetWeakPtr(), std::move(callback),
4205 download_id, url, 400, std::vector<SkBitmap>(),
4206 std::vector<gfx::Size>()));
amistry9f01b772015-07-29 01:54:554207 return download_id;
4208 }
4209
halton.huoca2eabd2015-07-06 08:17:404210 mojo_image_downloader->DownloadImage(
Fredrik Söderquistb2b39eb92019-11-18 16:16:504211 url, is_favicon, preferred_size, max_bitmap_size, bypass_cache,
tzike2aca992017-09-05 08:50:544212 base::BindOnce(&WebContentsImpl::OnDidDownloadImage,
Avi Drissmana5a52dd2018-03-27 03:39:024213 weak_factory_.GetWeakPtr(), std::move(callback),
4214 download_id, url));
amistry9f01b772015-07-29 01:54:554215 return download_id;
[email protected]795c28972012-12-06 06:13:394216}
4217
[email protected]36ec24f2014-01-09 00:32:084218void WebContentsImpl::Find(int request_id,
4219 const base::string16& search_text,
Rakina Zata Amni3f77dff2018-09-08 16:19:434220 blink::mojom::FindOptionsPtr options) {
thestig6057a6b22015-11-12 23:01:334221 // Cowardly refuse to search for no text.
4222 if (search_text.empty()) {
4223 NOTREACHED();
4224 return;
4225 }
4226
Rakina Zata Amni3f77dff2018-09-08 16:19:434227 GetOrCreateFindRequestManager()->Find(request_id, search_text,
4228 std::move(options));
[email protected]36ec24f2014-01-09 00:32:084229}
4230
4231void WebContentsImpl::StopFinding(StopFindAction action) {
paulmeyerfeafc2d2017-04-25 21:46:404232 if (FindRequestManager* manager = GetFindRequestManager())
4233 manager->StopFinding(action);
[email protected]36ec24f2014-01-09 00:32:084234}
4235
Tommy Steimel18360512017-11-01 00:38:194236bool WebContentsImpl::WasEverAudible() {
4237 return was_ever_audible_;
4238}
4239
Avi Drissmana5a52dd2018-03-27 03:39:024240void WebContentsImpl::GetManifest(GetManifestCallback callback) {
Yuzu Saijofce18322020-05-14 05:02:094241 // TODO(yuzus, 1061899): Move this function to RenderFrameHostImpl.
4242 ManifestManagerHost* manifest_manager_host =
4243 ManifestManagerHost::GetOrCreateForCurrentDocument(GetMainFrame());
4244 manifest_manager_host->GetManifest(std::move(callback));
mlamouriefdca9d2014-09-16 16:55:404245}
4246
bokanece34a82016-01-28 19:49:464247void WebContentsImpl::ExitFullscreen(bool will_cause_resize) {
mlamouri7a78d6fd2015-01-17 13:23:534248 // Clean up related state and initiate the fullscreen exit.
James Hollyerd5c9de462020-03-10 19:02:454249 GetRenderViewHost()->GetWidget()->RejectMouseLockOrUnlockIfNecessary(
4250 blink::mojom::PointerLockResult::kUserRejected);
bokanece34a82016-01-28 19:49:464251 ExitFullscreenMode(will_cause_resize);
mlamouri7a78d6fd2015-01-17 13:23:534252}
4253
Avi Drissman1a55a9d62020-03-10 18:56:454254base::ScopedClosureRunner WebContentsImpl::ForSecurityDropFullscreen() {
Avi Drissmanced52b62019-08-14 21:25:464255 // There are two chains of WebContents to kick out of fullscreen.
4256 //
4257 // Chain 1, the inner/outer WebContents chain. If an inner WebContents has
4258 // done something that requires the browser to drop fullscreen, drop
4259 // fullscreen from it and any outer WebContents that may be in fullscreen.
4260 //
4261 // Chain 2, the opener WebContents chain. If a WebContents has done something
4262 // that requires the browser to drop fullscreen, drop fullscreen from any
4263 // WebContents that was involved in the chain of opening it.
4264 //
4265 // Note that these two chains don't interact, as only a top-level WebContents
4266 // can have an opener. This simplifies things.
4267
Avi Drissman1a55a9d62020-03-10 18:56:454268 std::vector<base::WeakPtr<WebContentsImpl>> blocked_contentses;
4269
4270 WebContentsImpl* web_contents = this;
Avi Drissmanced52b62019-08-14 21:25:464271 while (web_contents) {
Avi Drissman1a55a9d62020-03-10 18:56:454272 // Drop fullscreen if the WebContents is in it, and...
Avi Drissmanced52b62019-08-14 21:25:464273 if (web_contents->IsFullscreenForCurrentTab())
4274 web_contents->ExitFullscreen(true);
4275
Avi Drissman1a55a9d62020-03-10 18:56:454276 // ...block the WebContents from entering fullscreen until further notice.
4277 ++web_contents->fullscreen_blocker_count_;
4278 blocked_contentses.push_back(web_contents->weak_factory_.GetWeakPtr());
4279
4280 if (web_contents->HasOriginalOpener()) {
4281 web_contents = static_cast<WebContentsImpl*>(
4282 FromRenderFrameHost(web_contents->GetOriginalOpener()));
4283 } else {
Avi Drissmanced52b62019-08-14 21:25:464284 web_contents = web_contents->GetOuterWebContents();
Avi Drissman1a55a9d62020-03-10 18:56:454285 }
Avi Drissmanced52b62019-08-14 21:25:464286 }
Avi Drissman1a55a9d62020-03-10 18:56:454287
4288 return base::ScopedClosureRunner(base::BindOnce(
4289 [](std::vector<base::WeakPtr<WebContentsImpl>> blocked_contentses) {
4290 for (base::WeakPtr<WebContentsImpl>& web_contents :
4291 blocked_contentses) {
4292 if (web_contents) {
4293 DCHECK_GT(web_contents->fullscreen_blocker_count_, 0);
4294 --web_contents->fullscreen_blocker_count_;
4295 }
4296 }
4297 },
4298 std::move(blocked_contentses)));
Avi Drissmanced52b62019-08-14 21:25:464299}
4300
mariakhomenko44bdc4732015-04-29 01:55:384301void WebContentsImpl::ResumeLoadingCreatedWebContents() {
Nasko Oskov83a8cf92018-10-19 14:58:564302 if (delayed_load_url_params_.get()) {
4303 DCHECK(!delayed_open_url_params_);
4304 controller_.LoadURLWithParams(*delayed_load_url_params_.get());
4305 delayed_load_url_params_.reset(nullptr);
4306 return;
4307 }
4308
mariakhomenkoa4971c12015-07-21 19:04:374309 if (delayed_open_url_params_.get()) {
4310 OpenURL(*delayed_open_url_params_.get());
4311 delayed_open_url_params_.reset(nullptr);
4312 return;
4313 }
4314
mariakhomenko44bdc4732015-04-29 01:55:384315 // Resume blocked requests for both the RenderViewHost and RenderFrameHost.
4316 // TODO(brettw): It seems bogus to reach into here and initialize the host.
dfalcantarae6b7b46752015-07-10 18:14:164317 if (is_resume_pending_) {
4318 is_resume_pending_ = false;
avi0f1bbc9712015-11-17 02:58:134319 GetRenderViewHost()->GetWidget()->Init();
dfalcantarae6b7b46752015-07-10 18:14:164320 GetMainFrame()->Init();
4321 }
mariakhomenko44bdc4732015-04-29 01:55:384322}
4323
[email protected]b172aee2012-04-10 17:05:264324bool WebContentsImpl::FocusLocationBarByDefault() {
Xiyuan Xia2a66b112018-12-06 15:52:164325 if (should_focus_location_bar_by_default_)
[email protected]0c9406632013-02-08 01:13:334326 return true;
Xiyuan Xia2a66b112018-12-06 15:52:164327
[email protected]0c9406632013-02-08 01:13:334328 return delegate_ && delegate_->ShouldFocusLocationBarByDefault(this);
[email protected]0bfbf882011-12-22 18:19:274329}
4330
Tommy C. Li8bb5bf42019-01-18 18:45:514331void WebContentsImpl::SetFocusToLocationBar() {
[email protected]0bfbf882011-12-22 18:19:274332 if (delegate_)
Tommy C. Li8bb5bf42019-01-18 18:45:514333 delegate_->SetFocusToLocationBar();
[email protected]c40d6232011-03-25 00:16:214334}
4335
clamy0e119882015-07-31 16:12:334336void WebContentsImpl::DidStartNavigation(NavigationHandle* navigation_handle) {
Charles Harrison53096ba2017-12-15 15:39:484337 TRACE_EVENT1("navigation", "WebContentsImpl::DidStartNavigation",
4338 "navigation_handle", navigation_handle);
Alan Cutterab48fc02020-02-24 12:26:424339 if (navigation_handle->IsInMainFrame())
4340 favicon_urls_.clear();
Mounir Lamouri7b4a1f232019-12-23 16:32:104341
ericwilligersde386342016-10-19 02:35:094342 for (auto& observer : observers_)
4343 observer.DidStartNavigation(navigation_handle);
Becca Hughesd11d6502018-07-31 17:01:284344
Xiyuan Xia2a66b112018-12-06 15:52:164345 if (navigation_handle->IsInMainFrame()) {
4346 // When the browser is started with about:blank as the startup URL, focus
4347 // the location bar (which will also select its contents) so people can
4348 // simply begin typing to navigate elsewhere.
4349 //
4350 // We need to be careful not to trigger this for anything other than the
4351 // startup navigation. In particular, if we allow an attacker to open a
4352 // popup to about:blank, then navigate, focusing the Omnibox will cause the
4353 // end of the new URL to be scrolled into view instead of the start,
4354 // allowing the attacker to spoof other URLs. The conditions checked here
4355 // are all aimed at ensuring no such attacker-controlled navigation can
4356 // trigger this.
4357 should_focus_location_bar_by_default_ =
4358 controller_.IsInitialNavigation() &&
4359 !navigation_handle->IsRendererInitiated() &&
4360 navigation_handle->GetURL() == url::kAboutBlankURL;
4361 }
clamy0e119882015-07-31 16:12:334362}
4363
4364void WebContentsImpl::DidRedirectNavigation(
4365 NavigationHandle* navigation_handle) {
Charles Harrison53096ba2017-12-15 15:39:484366 TRACE_EVENT1("navigation", "WebContentsImpl::DidRedirectNavigation",
4367 "navigation_handle", navigation_handle);
ericwilligersde386342016-10-19 02:35:094368 for (auto& observer : observers_)
4369 observer.DidRedirectNavigation(navigation_handle);
jam5f358e52017-02-13 16:37:034370
4371 // Notify accessibility if this is a reload. This has to called on the
4372 // BrowserAccessibilityManager associated with the old RFHI.
4373 if (navigation_handle->GetReloadType() != ReloadType::NONE) {
Mohamed Abdelhalim95794962019-09-20 11:16:494374 NavigationRequest* request = NavigationRequest::From(navigation_handle);
jam5f358e52017-02-13 16:37:034375 BrowserAccessibilityManager* manager =
Mohamed Abdelhalim95794962019-09-20 11:16:494376 request->frame_tree_node()
jam5f358e52017-02-13 16:37:034377 ->current_frame_host()
4378 ->browser_accessibility_manager();
4379 if (manager)
4380 manager->UserIsReloading();
4381 }
clamy0e119882015-07-31 16:12:334382}
4383
clamyefca29e2015-09-17 00:22:114384void WebContentsImpl::ReadyToCommitNavigation(
4385 NavigationHandle* navigation_handle) {
Charles Harrison53096ba2017-12-15 15:39:484386 TRACE_EVENT1("navigation", "WebContentsImpl::ReadyToCommitNavigation",
4387 "navigation_handle", navigation_handle);
ericwilligersde386342016-10-19 02:35:094388 for (auto& observer : observers_)
4389 observer.ReadyToCommitNavigation(navigation_handle);
John Abd-El-Malekd4882642017-12-04 21:45:194390
Kenneth Russell954ed9ce2018-04-12 23:07:014391 // If any domains are blocked from accessing 3D APIs because they may
4392 // have caused the GPU to reset recently, unblock them here if the user
4393 // initiated this navigation. This implies that the user was involved in
4394 // the decision to navigate, so there's no concern about
4395 // denial-of-service issues. Want to do this as early as
4396 // possible to avoid race conditions with pages attempting to access
4397 // WebGL early on.
4398 //
4399 // TODO(crbug.com/617904): currently navigations initiated by the browser
4400 // (reload button, reload menu option, pressing return in the Omnibox)
4401 // return false from HasUserGesture(). If or when that is addressed,
4402 // remove the check for IsRendererInitiated() below.
4403 //
4404 // TODO(crbug.com/832180): HasUserGesture comes from the renderer
4405 // process and isn't validated. Until it is, don't trust it.
4406 if (!navigation_handle->IsRendererInitiated()) {
4407 GpuDataManagerImpl::GetInstance()->UnblockDomainFrom3DAPIs(
4408 navigation_handle->GetURL());
4409 }
4410
John Abd-El-Malekfde08e6da2017-12-05 04:06:244411 if (navigation_handle->IsSameDocument())
4412 return;
4413
Livvie Lin139bf442018-08-10 00:18:544414 // SSLInfo is not needed on subframe navigations since the main-frame
4415 // certificate is the only one that can be inspected (using the info
4416 // bubble) without refreshing the page with DevTools open.
Carlos ILaed1e7b2019-03-12 19:27:214417 // We don't call DidStartResourceResponse on net errors, since that results on
4418 // existing cert exceptions being revoked, which leads to weird behavior with
4419 // committed interstitials or while offline. We only need the error check for
4420 // the main frame case because unlike this method, SubresourceResponseStarted
4421 // does not get called on network errors.
4422 if (navigation_handle->IsInMainFrame() &&
4423 navigation_handle->GetNetErrorCode() == net::OK) {
Livvie Lin139bf442018-08-10 00:18:544424 controller_.ssl_manager()->DidStartResourceResponse(
Lukasz Anforowiczd334bba2020-02-10 23:32:554425 navigation_handle->GetURL(),
Camille Lamy62b826012019-02-26 09:15:474426 navigation_handle->GetSSLInfo().has_value()
4427 ? net::IsCertStatusError(
4428 navigation_handle->GetSSLInfo()->cert_status)
4429 : false);
4430 }
John Abd-El-Malekfde08e6da2017-12-05 04:06:244431
4432 SetNotWaitingForResponse();
clamyefca29e2015-09-17 00:22:114433}
4434
clamy0e119882015-07-31 16:12:334435void WebContentsImpl::DidFinishNavigation(NavigationHandle* navigation_handle) {
Charles Harrison53096ba2017-12-15 15:39:484436 TRACE_EVENT1("navigation", "WebContentsImpl::DidFinishNavigation",
4437 "navigation_handle", navigation_handle);
Steven Holte38041342018-06-22 22:00:574438
ericwilligersde386342016-10-19 02:35:094439 for (auto& observer : observers_)
4440 observer.DidFinishNavigation(navigation_handle);
clamy0e119882015-07-31 16:12:334441
Becca Hughesd11d6502018-07-31 17:01:284442 if (display_cutout_host_impl_)
4443 display_cutout_host_impl_->DidFinishNavigation(navigation_handle);
4444
jam5f358e52017-02-13 16:37:034445 if (navigation_handle->HasCommitted()) {
Ian Prestcdd843d2020-01-28 18:59:584446 // TODO(domfarolino, dmazzoni): Do this using WebContentsObserver. See
4447 // https://crbug.com/981271.
4448 BrowserAccessibilityManager* manager =
4449 static_cast<RenderFrameHostImpl*>(
4450 navigation_handle->GetRenderFrameHost())
4451 ->browser_accessibility_manager();
4452 if (manager) {
4453 if (navigation_handle->IsErrorPage()) {
4454 manager->NavigationFailed();
4455 } else {
4456 manager->NavigationSucceeded();
4457 }
4458 }
4459
Yue Ru Sun979c4b162019-11-06 16:11:044460 if (navigation_handle->IsInMainFrame()) {
4461 last_committed_source_id_including_same_document_ =
Steven Holte38041342018-06-22 22:00:574462 ukm::ConvertToSourceId(navigation_handle->GetNavigationId(),
4463 ukm::SourceIdType::NAVIGATION_ID);
Yue Ru Sun979c4b162019-11-06 16:11:044464
4465 if (!navigation_handle->IsSameDocument()) {
4466 was_ever_audible_ = false;
Yue Ru Sun979c4b162019-11-06 16:11:044467 }
Tommy Steimel18360512017-11-01 00:38:194468 }
Lan Weif81c6e7c2020-02-12 16:46:094469
4470 if (!navigation_handle->IsSameDocument())
4471 last_screen_orientation_change_time_ = base::TimeTicks();
dmazzonibf8cec42015-02-08 08:28:084472 }
Xiyuan Xia2a66b112018-12-06 15:52:164473
4474 // If we didn't end up on about:blank after setting this in DidStartNavigation
4475 // then don't focus the location bar.
4476 if (should_focus_location_bar_by_default_ &&
4477 navigation_handle->GetURL() != url::kAboutBlankURL) {
4478 should_focus_location_bar_by_default_ = false;
4479 }
yilkal796e89e2019-10-02 23:58:284480
yilkal172da772019-11-06 22:49:074481 if (navigation_handle->IsInMainFrame() && first_navigation_completed_)
4482 RecordMaxFrameCountUMA(max_loaded_frame_count_);
yilkal796e89e2019-10-02 23:58:284483
yilkal172da772019-11-06 22:49:074484 // If navigation has successfully finished in the main frame, set
4485 // |first_navigation_completed_| to true so that we will record
4486 // |max_loaded_frame_count_| above when future main frame navigations finish.
4487 if (navigation_handle->IsInMainFrame() && !navigation_handle->IsErrorPage()) {
4488 first_navigation_completed_ = true;
4489
4490 // Navigation has completed in main frame. Reset |max_loaded_frame_count_|.
4491 // |max_loaded_frame_count_| is not necessarily 1 if the navigation was
4492 // served from BackForwardCache.
4493 max_loaded_frame_count_ =
4494 GetMainFrame()->frame_tree_node()->GetFrameTreeSize();
yilkal796e89e2019-10-02 23:58:284495 }
[email protected]400992b2012-06-14 00:03:544496}
4497
[email protected]b80624c2014-02-09 02:46:554498void WebContentsImpl::DidFailLoadWithError(
4499 RenderFrameHostImpl* render_frame_host,
[email protected]b80624c2014-02-09 02:46:554500 const GURL& url,
Dave Tapuska924ef3c2020-01-22 18:20:594501 int error_code) {
ericwilligersde386342016-10-19 02:35:094502 for (auto& observer : observers_) {
Dave Tapuska924ef3c2020-01-22 18:20:594503 observer.DidFailLoad(render_frame_host, url, error_code);
ericwilligersde386342016-10-19 02:35:094504 }
[email protected]b80624c2014-02-09 02:46:554505}
4506
[email protected]52913802013-12-10 05:52:184507void WebContentsImpl::NotifyChangedNavigationState(
4508 InvalidateTypes changed_flags) {
4509 NotifyNavigationStateChanged(changed_flags);
4510}
4511
lukasza1d025732016-09-28 21:36:084512bool WebContentsImpl::ShouldTransferNavigation(bool is_main_frame_navigation) {
creis29460272015-12-16 04:38:224513 if (!delegate_)
4514 return true;
lukasza1d025732016-09-28 21:36:084515 return delegate_->ShouldTransferNavigation(is_main_frame_navigation);
creis29460272015-12-16 04:38:224516}
4517
[email protected]aa62afd2014-04-22 19:22:464518bool WebContentsImpl::ShouldPreserveAbortedURLs() {
4519 if (!delegate_)
4520 return false;
4521 return delegate_->ShouldPreserveAbortedURLs(this);
4522}
4523
[email protected]0d0f4c492014-04-02 06:42:574524void WebContentsImpl::DidNavigateMainFramePreCommit(
[email protected]5cfbddc2014-06-23 23:52:234525 bool navigation_is_within_page) {
[email protected]0d0f4c492014-04-02 06:42:574526 // Ensure fullscreen mode is exited before committing the navigation to a
4527 // different page. The next page will not start out assuming it is in
4528 // fullscreen mode.
[email protected]5cfbddc2014-06-23 23:52:234529 if (navigation_is_within_page) {
[email protected]dfc39cb2014-04-09 22:58:194530 // No page change? Then, the renderer and browser can remain in fullscreen.
[email protected]0d0f4c492014-04-02 06:42:574531 return;
4532 }
alexmos5dd617d2016-06-07 04:21:154533 if (IsFullscreenForCurrentTab())
bokanece34a82016-01-28 19:49:464534 ExitFullscreen(false);
alexmos5dd617d2016-06-07 04:21:154535 DCHECK(!IsFullscreenForCurrentTab());
Joe Downingc1a57efe2018-05-09 18:23:544536
4537 // Clean up keyboard lock state when navigating.
4538 CancelKeyboardLock(keyboard_lock_widget_);
[email protected]0d0f4c492014-04-02 06:42:574539}
4540
[email protected]37567b432014-02-12 01:12:224541void WebContentsImpl::DidNavigateMainFramePostCommit(
sky8b00392d2015-01-10 00:30:284542 RenderFrameHostImpl* render_frame_host,
[email protected]37567b432014-02-12 01:12:224543 const LoadCommittedDetails& details,
4544 const FrameHostMsg_DidCommitProvisionalLoad_Params& params) {
4545 if (details.is_navigation_to_different_page()) {
4546 // Clear the status bubble. This is a workaround for a bug where WebKit
4547 // doesn't let us know that the cursor left an element during a
4548 // transition (this is also why the mouse cursor remains as a hand after
4549 // clicking on a link); see bugs 1184641 and 980803. We don't want to
4550 // clear the bubble when a user navigates to a named anchor in the same
4551 // page.
Lukasz Anforowiczfd707142018-02-07 19:46:134552 ClearTargetURL();
lanwei9d343ad22015-02-11 01:46:004553
4554 RenderWidgetHostViewBase* rwhvb =
4555 static_cast<RenderWidgetHostViewBase*>(GetRenderWidgetHostView());
4556 if (rwhvb)
4557 rwhvb->OnDidNavigateMainFrameToNewPage();
[email protected]37567b432014-02-12 01:12:224558 }
4559
[email protected]e85165c642014-06-10 14:34:314560 if (delegate_)
[email protected]37567b432014-02-12 01:12:224561 delegate_->DidNavigateMainFramePostCommit(this);
[email protected]e85165c642014-06-10 14:34:314562 view_->SetOverscrollControllerEnabled(CanOverscrollContent());
Shivani Sharma985474f2019-05-23 21:36:474563
Carlos Caballero91fffb22019-10-29 15:24:304564 if (details.is_navigation_to_different_page() &&
4565 GetRenderViewHost()->did_first_visually_non_empty_paint()) {
4566 // This event will not fire again if the page is restored from the
4567 // BackForwardCache. So fire it ourselves if needed.
4568 DidFirstVisuallyNonEmptyPaint(GetRenderViewHost());
4569 }
Carlos Caballero1215f882019-10-29 15:58:434570
4571 if (GetRenderViewHost()->theme_color() != last_sent_theme_color_) {
4572 // This event will not fire again if the page is restored from the
4573 // BackForwardCache. So fire it ourselves if needed.
4574 OnThemeColorChanged(GetRenderViewHost());
4575 }
[email protected]37567b432014-02-12 01:12:224576}
4577
4578void WebContentsImpl::DidNavigateAnyFramePostCommit(
4579 RenderFrameHostImpl* render_frame_host,
4580 const LoadCommittedDetails& details,
4581 const FrameHostMsg_DidCommitProvisionalLoad_Params& params) {
[email protected]b4c84012014-04-28 19:51:104582 // Now that something has committed, we don't need to track whether the
4583 // initial page has been accessed.
4584 has_accessed_initial_document_ = false;
4585
[email protected]37567b432014-02-12 01:12:224586 // If we navigate off the page, close all JavaScript dialogs.
eugenebutee08663a2017-04-27 17:43:124587 if (!details.is_same_document)
creis89a0f782015-05-27 16:13:174588 CancelActiveAndPendingDialogs();
[email protected]37567b432014-02-12 01:12:224589
avid53461d2016-02-25 17:15:044590 // If this is a user-initiated navigation, start allowing JavaScript dialogs
4591 // again.
avi5d3b8692016-10-12 22:00:464592 if (params.gesture == NavigationGestureUser && dialog_manager_) {
avi6879fcf2017-01-21 05:27:534593 dialog_manager_->CancelDialogs(this, /*reset_state=*/true);
avi5d3b8692016-10-12 22:00:464594 }
[email protected]37567b432014-02-12 01:12:224595}
4596
4597void WebContentsImpl::SetMainFrameMimeType(const std::string& mime_type) {
4598 contents_mime_type_ = mime_type;
4599}
4600
[email protected]277857a2014-06-03 10:38:014601bool WebContentsImpl::CanOverscrollContent() const {
[email protected]e85165c642014-06-10 14:34:314602 // Disable overscroll when touch emulation is on. See crbug.com/369938.
[email protected]34ff1cfc2014-08-20 06:16:054603 if (force_disable_overscroll_content_)
[email protected]e85165c642014-06-10 14:34:314604 return false;
4605
[email protected]37567b432014-02-12 01:12:224606 if (delegate_)
4607 return delegate_->CanOverscrollContent();
4608
4609 return false;
4610}
4611
Carlos Caballero1215f882019-10-29 15:58:434612void WebContentsImpl::OnThemeColorChanged(RenderViewHostImpl* source) {
4613 if (source->did_first_visually_non_empty_paint() &&
4614 last_sent_theme_color_ != source->theme_color()) {
ericwilligersde386342016-10-19 02:35:094615 for (auto& observer : observers_)
Peter Kastingb14e5782020-01-06 23:55:044616 observer.DidChangeThemeColor();
Carlos Caballero1215f882019-10-29 15:58:434617 last_sent_theme_color_ = source->theme_color();
peter90afaba2015-06-01 12:05:294618 }
[email protected]e7104762014-06-20 19:17:254619}
4620
Gyuyoung Kim1905f7c2020-05-02 00:00:244621void WebContentsImpl::DidLoadResourceFromMemoryCache(
nicka0ac8382016-12-15 23:59:234622 RenderFrameHostImpl* source,
[email protected]724159a2010-12-30 01:11:184623 const GURL& url,
[email protected]70435962011-08-02 20:13:284624 const std::string& http_method,
[email protected]6d6cfb3a2012-05-23 22:53:184625 const std::string& mime_type,
Kinuko Yasuda433f710a2020-02-14 02:11:584626 network::mojom::RequestDestination request_destination) {
ericwilligersde386342016-10-19 02:35:094627 for (auto& observer : observers_)
Kinuko Yasuda433f710a2020-02-14 02:11:584628 observer.DidLoadResourceFromMemoryCache(url, mime_type,
4629 request_destination);
[email protected]b0f724c2013-09-05 04:21:134630
Steven Bingler674fb0822018-12-12 18:04:524631 if (url.is_valid() && url.SchemeIsHTTPOrHTTPS()) {
nicka0ac8382016-12-15 23:59:234632 StoragePartition* partition = source->GetProcess()->GetStoragePartition();
John Abd-El-Malekf4fe9d82019-07-31 04:05:594633 partition->GetNetworkContext()->NotifyExternalCacheHit(
Matt Menke2a33dee2019-12-02 17:01:544634 url, http_method, source->GetNetworkIsolationKey());
[email protected]8bfc8272013-09-09 20:10:534635 }
[email protected]724159a2010-12-30 01:11:184636}
4637
carloskd9d97942017-02-16 08:58:094638void WebContentsImpl::DidDisplayInsecureContent() {
estarkc2273502016-10-12 22:03:024639 controller_.ssl_manager()->DidDisplayMixedContent();
[email protected]724159a2010-12-30 01:11:184640}
4641
Dave Tapuskaa657d872019-10-21 20:23:214642void WebContentsImpl::DidContainInsecureFormAction() {
elawrenceb2ac2a232017-03-27 21:46:254643 controller_.ssl_manager()->DidContainInsecureFormAction();
4644}
4645
Julie Jeongeun Kim0b103492020-01-29 05:30:514646void WebContentsImpl::DocumentAvailableInMainFrame() {
4647 for (auto& observer : observers_)
4648 observer.DocumentAvailableInMainFrame();
4649}
4650
nicka0ac8382016-12-15 23:59:234651void WebContentsImpl::OnDidRunInsecureContent(RenderFrameHostImpl* source,
4652 const GURL& security_origin,
estark910b4572015-12-09 20:55:414653 const GURL& target_url) {
nicka0ac8382016-12-15 23:59:234654 // TODO(nick, estark): Should we call FilterURL using |source|'s process on
4655 // these parameters? |target_url| seems unused, except for a log message. And
4656 // |security_origin| might be replaceable with the origin of the main frame.
carloskd9d97942017-02-16 08:58:094657 DidRunInsecureContent(security_origin, target_url);
4658}
4659
4660void WebContentsImpl::DidRunInsecureContent(const GURL& security_origin,
4661 const GURL& target_url) {
[email protected]9450c462013-11-23 01:22:584662 LOG(WARNING) << security_origin << " ran insecure content from "
4663 << target_url.possibly_invalid_spec();
[email protected]e6e30ac2014-01-13 21:24:394664 RecordAction(base::UserMetricsAction("SSL.RanInsecureContent"));
estark910b4572015-12-09 20:55:414665 if (base::EndsWith(security_origin.spec(), kDotGoogleDotCom,
brettwa7ff1b292015-07-16 17:49:294666 base::CompareCase::INSENSITIVE_ASCII))
[email protected]e6e30ac2014-01-13 21:24:394667 RecordAction(base::UserMetricsAction("SSL.RanInsecureContentGoogle"));
estarkc2273502016-10-12 22:03:024668 controller_.ssl_manager()->DidRunMixedContent(security_origin);
[email protected]724159a2010-12-30 01:11:184669}
4670
carloskd9d97942017-02-16 08:58:094671void WebContentsImpl::PassiveInsecureContentFound(const GURL& resource_url) {
Sergey Kuznetsov3ebc01b2018-02-01 04:57:364672 if (delegate_) {
4673 delegate_->PassiveInsecureContentFound(resource_url);
4674 }
carloskd9d97942017-02-16 08:58:094675}
4676
4677bool WebContentsImpl::ShouldAllowRunningInsecureContent(
carloskd9d97942017-02-16 08:58:094678 bool allowed_per_prefs,
4679 const url::Origin& origin,
4680 const GURL& resource_url) {
Sergey Kuznetsov3ebc01b2018-02-01 04:57:364681 if (delegate_) {
Carlos IL29294a62020-05-15 00:21:164682 return delegate_->ShouldAllowRunningInsecureContent(this, allowed_per_prefs,
4683 origin, resource_url);
Sergey Kuznetsov3ebc01b2018-02-01 04:57:364684 }
4685
4686 return allowed_per_prefs;
carloskd9d97942017-02-16 08:58:094687}
4688
Lukasz Anforowicze1b954d92017-10-30 21:28:064689void WebContentsImpl::ViewSource(RenderFrameHostImpl* frame) {
4690 DCHECK_EQ(this, WebContents::FromRenderFrameHost(frame));
4691
4692 // Don't do anything if there is no |delegate_| that could accept and show the
4693 // new WebContents containing the view-source.
4694 if (!delegate_)
4695 return;
4696
4697 // Use the last committed entry, since the pending entry hasn't loaded yet and
4698 // won't be copied into the cloned tab.
4699 NavigationEntryImpl* last_committed_entry =
Fergal Daly09d6c762020-05-29 02:05:184700 GetController().GetLastCommittedEntry();
Lukasz Anforowicze1b954d92017-10-30 21:28:064701 if (!last_committed_entry)
4702 return;
4703
4704 FrameNavigationEntry* frame_entry =
4705 last_committed_entry->GetFrameEntry(frame->frame_tree_node());
4706 if (!frame_entry)
4707 return;
4708
4709 // Any new WebContents opened while this WebContents is in fullscreen can be
4710 // used to confuse the user, so drop fullscreen.
Avi Drissman1a55a9d62020-03-10 18:56:454711 base::ScopedClosureRunner fullscreen_block = ForSecurityDropFullscreen();
4712 // The new view source contents will be independent of this contents, so
4713 // release the fullscreen block.
4714 fullscreen_block.RunAndReset();
Lukasz Anforowicze1b954d92017-10-30 21:28:064715
4716 // We intentionally don't share the SiteInstance with the original frame so
4717 // that view source has a consistent process model and always ends up in a new
4718 // process (https://crbug.com/699493).
4719 scoped_refptr<SiteInstanceImpl> site_instance_for_view_source = nullptr;
Lukasz Anforowicz435bcb582019-07-12 20:50:064720 // Referrer and initiator are not important, because view-source should not
4721 // hit the network, but should be served from the cache instead.
Lukasz Anforowicze1b954d92017-10-30 21:28:064722 Referrer referrer_for_view_source;
Lukasz Anforowicz435bcb582019-07-12 20:50:064723 base::Optional<url::Origin> initiator_for_view_source = base::nullopt;
Lukasz Anforowicze1b954d92017-10-30 21:28:064724 // Do not restore title, derive it from the url.
4725 base::string16 title_for_view_source;
4726 auto navigation_entry = std::make_unique<NavigationEntryImpl>(
4727 site_instance_for_view_source, frame_entry->url(),
Lukasz Anforowicz435bcb582019-07-12 20:50:064728 referrer_for_view_source, initiator_for_view_source,
4729 title_for_view_source, ui::PAGE_TRANSITION_LINK,
Marijn Kruisselbrink7a0d5e182018-05-24 22:55:094730 /* is_renderer_initiated = */ false,
4731 /* blob_url_loader_factory = */ nullptr);
Joel Hockey891e88062020-04-30 05:38:444732 const GURL url(content::kViewSourceScheme + std::string(":") +
4733 frame_entry->url().spec());
4734 navigation_entry->SetVirtualURL(url);
Matt Menkea56c5992020-04-10 20:53:564735 navigation_entry->set_isolation_info(
4736 frame->GetIsolationInfoForSubresources());
Yao Xiaof68f57d2019-10-03 04:20:054737
Lukasz Anforowicze1b954d92017-10-30 21:28:064738 // Do not restore scroller position.
4739 // TODO(creis, lukasza, arthursonzogni): Do not reuse the original PageState,
4740 // but start from a new one and only copy the needed data.
4741 const PageState& new_page_state =
4742 frame_entry->page_state().RemoveScrollOffset();
4743
4744 scoped_refptr<FrameNavigationEntry> new_frame_entry =
4745 navigation_entry->root_node()->frame_entry;
4746 new_frame_entry->set_method(frame_entry->method());
4747 new_frame_entry->SetPageState(new_page_state);
4748
4749 // Create a new WebContents, which is used to display the source code.
erikchenbee5c9622018-04-27 19:30:254750 std::unique_ptr<WebContents> view_source_contents =
Erik Chenbb8e738e2018-04-28 14:10:434751 Create(CreateParams(GetBrowserContext()));
Lukasz Anforowicze1b954d92017-10-30 21:28:064752
4753 // Restore the previously created NavigationEntry.
4754 std::vector<std::unique_ptr<NavigationEntry>> navigation_entries;
4755 navigation_entries.push_back(std::move(navigation_entry));
4756 view_source_contents->GetController().Restore(0, RestoreType::CURRENT_SESSION,
4757 &navigation_entries);
4758
4759 // Add |view_source_contents| as a new tab.
4760 gfx::Rect initial_rect;
4761 constexpr bool kUserGesture = true;
4762 bool ignored_was_blocked;
Joel Hockey891e88062020-04-30 05:38:444763 delegate_->AddNewContents(this, std::move(view_source_contents), url,
Lukasz Anforowicze1b954d92017-10-30 21:28:064764 WindowOpenDisposition::NEW_FOREGROUND_TAB,
4765 initial_rect, kUserGesture, &ignored_was_blocked);
4766 // Note that the |delegate_| could have deleted |view_source_contents| during
4767 // AddNewContents method call.
4768}
4769
Lukasz Anforowiczd334bba2020-02-10 23:32:554770void WebContentsImpl::SubresourceResponseStarted(const GURL& url,
4771 net::CertStatus cert_status) {
4772 controller_.ssl_manager()->DidStartResourceResponse(url, cert_status);
Jay Civellid5904f02018-03-23 19:43:574773 SetNotWaitingForResponse();
4774}
4775
Jay Civelli116683f2018-03-27 19:56:234776void WebContentsImpl::ResourceLoadComplete(
Jay Civelli52a63302018-04-30 22:39:554777 RenderFrameHost* render_frame_host,
Clark DuVall36164bd2018-08-09 22:49:084778 const GlobalRequestID& request_id,
Minggang Wangee5af392020-02-05 02:55:284779 blink::mojom::ResourceLoadInfoPtr resource_load_info) {
John Abd-El-Malek9b8f7fa2017-12-04 23:52:254780 for (auto& observer : observers_) {
Clark DuVall36164bd2018-08-09 22:49:084781 observer.ResourceLoadComplete(render_frame_host, request_id,
4782 *resource_load_info);
John Abd-El-Malek9b8f7fa2017-12-04 23:52:254783 }
John Abd-El-Malekd4882642017-12-04 21:45:194784}
4785
Wei Li5bb659742018-02-14 03:07:584786void WebContentsImpl::PrintCrossProcessSubframe(
4787 const gfx::Rect& rect,
4788 int document_cookie,
4789 RenderFrameHost* subframe_host) {
Wei Li5c0b4d12018-03-22 15:05:224790 auto* outer_contents = GetOuterWebContents();
4791 if (outer_contents) {
4792 // When an extension or app page is printed, the content should be
4793 // composited with outer content, so the outer contents should handle the
4794 // print request.
4795 outer_contents->PrintCrossProcessSubframe(rect, document_cookie,
4796 subframe_host);
4797 return;
4798 }
4799
Wei Li5bb659742018-02-14 03:07:584800 // If there is no delegate such as in tests or during deletion, do nothing.
4801 if (!delegate_)
4802 return;
4803
4804 delegate_->PrintCrossProcessSubframe(this, rect, document_cookie,
4805 subframe_host);
4806}
4807
ckitagawa8695799b2020-02-05 16:08:494808void WebContentsImpl::CapturePaintPreviewOfCrossProcessSubframe(
4809 const gfx::Rect& rect,
4810 const base::UnguessableToken& guid,
4811 RenderFrameHost* render_frame_host) {
4812 if (!delegate_)
4813 return;
4814 delegate_->CapturePaintPreviewOfCrossProcessSubframe(this, rect, guid,
4815 render_frame_host);
4816}
4817
rob.buis300b0872017-03-10 20:43:584818#if defined(OS_ANDROID)
4819base::android::ScopedJavaLocalRef<jobject>
4820WebContentsImpl::GetJavaRenderFrameHostDelegate() {
4821 return GetJavaWebContents();
4822}
4823#endif
4824
estark910b4572015-12-09 20:55:414825void WebContentsImpl::OnDidDisplayContentWithCertificateErrors(
Zhuoyu Qian4353f7f2018-01-08 05:33:004826 RenderFrameHostImpl* source) {
estarkc2273502016-10-12 22:03:024827 controller_.ssl_manager()->DidDisplayContentWithCertErrors();
estark910b4572015-12-09 20:55:414828}
4829
4830void WebContentsImpl::OnDidRunContentWithCertificateErrors(
Zhuoyu Qian4353f7f2018-01-08 05:33:004831 RenderFrameHostImpl* source) {
Emily Starkabbdd662020-06-05 02:40:134832 // For RenderFrameHosts that are inactive and going to be discarded, we can
4833 // disregard this message; there's no need to update the UI if the UI will
4834 // never be shown again.
4835 //
4836 // We still process this message for speculative RenderFrameHosts. This can
4837 // happen when a subframe's main resource has a certificate error. The
4838 // currently committed navigation entry will get marked as having run insecure
4839 // content and that will carry over to the navigation entry for the
4840 // speculative RFH when it commits.
4841 if (source->lifecycle_state() !=
4842 RenderFrameHostImpl::LifecycleState::kSpeculative &&
4843 source->IsInactiveAndDisallowReactivation()) {
estarkaeda1422016-05-10 02:31:124844 return;
Emily Starkabbdd662020-06-05 02:40:134845 }
estarkcd2e30c2016-08-12 06:51:154846 controller_.ssl_manager()->DidRunContentWithCertErrors(
Emily Starkabbdd662020-06-05 02:40:134847 source->GetMainFrame()->GetLastCommittedOrigin().GetURL());
estark910b4572015-12-09 20:55:414848}
4849
Kouhei Ueno97e0990d2019-09-25 02:57:274850void WebContentsImpl::DOMContentLoaded(RenderFrameHost* render_frame_host) {
ericwilligersde386342016-10-19 02:35:094851 for (auto& observer : observers_)
Kouhei Uenof6e96242019-09-27 01:54:354852 observer.DOMContentLoaded(render_frame_host);
[email protected]724159a2010-12-30 01:11:184853}
4854
Gyuyoung Kim525e0c5d2020-03-09 15:04:194855void WebContentsImpl::OnDidFinishLoad(RenderFrameHost* render_frame_host,
nicka0ac8382016-12-15 23:59:234856 const GURL& url) {
[email protected]8b3af1e2014-01-24 13:29:124857 GURL validated_url(url);
Gyuyoung Kim525e0c5d2020-03-09 15:04:194858 render_frame_host->GetProcess()->FilterURL(false, &validated_url);
[email protected]028053d42014-03-05 22:20:374859
ericwilligersde386342016-10-19 02:35:094860 for (auto& observer : observers_)
Gyuyoung Kim525e0c5d2020-03-09 15:04:194861 observer.DidFinishLoad(render_frame_host, validated_url);
yilkal172da772019-11-06 22:49:074862
4863 size_t tree_size = frame_tree_.root()->GetFrameTreeSize();
4864 if (max_loaded_frame_count_ < tree_size)
4865 max_loaded_frame_count_ = tree_size;
4866
Gyuyoung Kim525e0c5d2020-03-09 15:04:194867 if (!render_frame_host->GetParent())
yilkal172da772019-11-06 22:49:074868 UMA_HISTOGRAM_COUNTS_1000("Navigation.MainFrame.FrameCount", tree_size);
[email protected]1a55c5be2011-11-29 11:36:314869}
4870
Dave Tapuska549baab2019-03-19 15:52:404871void WebContentsImpl::OnGoToEntryAtOffset(RenderFrameHostImpl* source,
Julie Jeongeun Kim144f8192020-01-18 06:59:034872 int32_t offset,
Dave Tapuska043b94a2019-09-10 14:14:354873 bool has_user_gesture) {
Arthur Sonzognia7d715a2018-09-20 16:11:134874 // Non-user initiated navigations coming from the renderer should be ignored
4875 // if there is an ongoing browser-initiated navigation.
4876 // See https://crbug.com/879965.
4877 // TODO(arthursonzogni): See if this should check for ongoing navigations in
4878 // the frame(s) affected by the session history navigation, rather than just
4879 // the main frame.
4880 if (!has_user_gesture) {
4881 NavigationRequest* ongoing_navigation_request =
4882 frame_tree_.root()->navigation_request();
4883 if (ongoing_navigation_request &&
4884 ongoing_navigation_request->browser_initiated()) {
4885 return;
4886 }
4887 }
4888
nicka0ac8382016-12-15 23:59:234889 // All frames are allowed to navigate the global history.
Dave Tapuska8bfd84c2019-03-26 20:47:164890 if (!delegate_ || delegate_->OnGoToEntryOffset(offset)) {
arthursonzognib93a4472020-04-10 07:38:004891 if (source->IsSandboxed(network::mojom::WebSandboxFlags::kTopNavigation)) {
Dave Tapuska8bfd84c2019-03-26 20:47:164892 // Keep track of whether this is a session history from a sandboxed iframe
4893 // with top level navigation disallowed.
4894 controller_.GoToOffsetInSandboxedFrame(offset,
4895 source->GetFrameTreeNodeId());
4896 } else {
4897 controller_.GoToOffset(offset);
4898 }
4899 }
[email protected]216813952011-05-19 22:21:264900}
4901
Dave Tapuska4c02911b2020-02-14 22:52:074902void WebContentsImpl::OnPageScaleFactorChanged(RenderFrameHostImpl* source,
nicka0ac8382016-12-15 23:59:234903 float page_scale_factor) {
mcnee336ea2c2017-05-23 22:50:594904#if !defined(OS_ANDROID)
4905 // While page scale factor is used on mobile, this PageScaleFactorIsOne logic
4906 // is only needed on desktop.
mcnee432e47d2015-11-09 19:37:464907 bool is_one = page_scale_factor == 1.f;
4908 if (is_one != page_scale_factor_is_one_) {
4909 page_scale_factor_is_one_ = is_one;
4910
4911 HostZoomMapImpl* host_zoom_map =
4912 static_cast<HostZoomMapImpl*>(HostZoomMap::GetForWebContents(this));
4913
nicka0ac8382016-12-15 23:59:234914 if (host_zoom_map) {
mcnee432e47d2015-11-09 19:37:464915 host_zoom_map->SetPageScaleFactorIsOneForView(
Dave Tapuska4c02911b2020-02-14 22:52:074916 source->GetProcess()->GetID(),
4917 source->GetRenderViewHost()->GetRoutingID(),
mcnee432e47d2015-11-09 19:37:464918 page_scale_factor_is_one_);
4919 }
4920 }
mcnee336ea2c2017-05-23 22:50:594921#endif // !defined(OS_ANDROID)
mcnee432e47d2015-11-09 19:37:464922
ericwilligersde386342016-10-19 02:35:094923 for (auto& observer : observers_)
4924 observer.OnPageScaleFactorChanged(page_scale_factor);
mcnee432e47d2015-11-09 19:37:464925}
4926
W. James MacLean62198672019-06-04 16:46:104927void WebContentsImpl::OnTextAutosizerPageInfoChanged(
Henrique Ferreiroba535472020-04-21 11:38:364928 RenderFrameHostImpl* source,
4929 blink::mojom::TextAutosizerPageInfoPtr page_info) {
W. James MacLean62198672019-06-04 16:46:104930 // Keep a copy of |page_info| in case we create a new RenderView before
4931 // the next update.
Henrique Ferreiroba535472020-04-21 11:38:364932 text_autosizer_page_info_.main_frame_width = page_info->main_frame_width;
4933 text_autosizer_page_info_.main_frame_layout_width =
4934 page_info->main_frame_layout_width;
4935 text_autosizer_page_info_.device_scale_adjustment =
4936 page_info->device_scale_adjustment;
W. James MacLean62198672019-06-04 16:46:104937 frame_tree_.root()->render_manager()->SendPageMessage(
4938 new PageMsg_UpdateTextAutosizerPageInfoForRemoteMainFrames(
Henrique Ferreiroba535472020-04-21 11:38:364939 MSG_ROUTING_NONE, text_autosizer_page_info_),
W. James MacLean62198672019-06-04 16:46:104940 source->GetSiteInstance());
4941}
4942
Kent Tamura8c9e0562018-11-06 07:02:194943void WebContentsImpl::EnumerateDirectory(
4944 RenderFrameHost* render_frame_host,
Avi Drissman1a55a9d62020-03-10 18:56:454945 std::unique_ptr<FileChooserImpl::FileSelectListenerImpl> listener,
Kent Tamura8c9e0562018-11-06 07:02:194946 const base::FilePath& directory_path) {
Avi Drissman1a55a9d62020-03-10 18:56:454947 // Any explicit focusing of another window while this WebContents is in
4948 // fullscreen can be used to confuse the user, so drop fullscreen.
4949 base::ScopedClosureRunner fullscreen_block = ForSecurityDropFullscreen();
4950 listener->SetFullscreenBlock(std::move(fullscreen_block));
4951
Kent Tamura8c9e0562018-11-06 07:02:194952 if (delegate_)
4953 delegate_->EnumerateDirectory(this, std::move(listener), directory_path);
4954 else
4955 listener->FileSelectionCanceled();
[email protected]3a29a6e2011-08-24 18:26:214956}
4957
Dave Tapuskaf9fc3122019-10-18 19:42:554958void WebContentsImpl::RegisterProtocolHandler(RenderFrameHostImpl* source,
4959 const std::string& protocol,
4960 const GURL& url,
4961 const base::string16& title,
4962 bool user_gesture) {
nicka0ac8382016-12-15 23:59:234963 // TODO(nick): Should we consider |source| here or pass it to the delegate?
4964 // TODO(nick): Do we need to apply FilterURL to |url|?
[email protected]e5f2de02012-07-20 22:15:434965 if (!delegate_)
4966 return;
4967
Raymes Khoury58373df2019-08-06 06:29:204968 if (!AreValidRegisterProtocolHandlerArguments(
4969 protocol, url, source->GetLastCommittedOrigin())) {
4970 ReceivedBadMessage(source->GetProcess(),
4971 bad_message::REGISTER_PROTOCOL_HANDLER_INVALID_URL);
[email protected]b9535422012-02-09 01:47:594972 return;
Raymes Khoury58373df2019-08-06 06:29:204973 }
[email protected]8f810632013-06-06 22:33:324974
[email protected]c125e532014-05-10 20:39:064975 delegate_->RegisterProtocolHandler(this, protocol, url, user_gesture);
[email protected]7d189022011-08-25 22:54:204976}
4977
Dave Tapuskaf9fc3122019-10-18 19:42:554978void WebContentsImpl::UnregisterProtocolHandler(RenderFrameHostImpl* source,
4979 const std::string& protocol,
4980 const GURL& url,
4981 bool user_gesture) {
nicka0ac8382016-12-15 23:59:234982 // TODO(nick): Should we consider |source| here or pass it to the delegate?
4983 // TODO(nick): Do we need to apply FilterURL to |url|?
[email protected]f5273e52014-07-14 16:30:204984 if (!delegate_)
4985 return;
4986
Raymes Khoury58373df2019-08-06 06:29:204987 if (!AreValidRegisterProtocolHandlerArguments(
4988 protocol, url, source->GetLastCommittedOrigin())) {
4989 ReceivedBadMessage(source->GetProcess(),
4990 bad_message::REGISTER_PROTOCOL_HANDLER_INVALID_URL);
[email protected]f5273e52014-07-14 16:30:204991 return;
Raymes Khoury58373df2019-08-06 06:29:204992 }
[email protected]f5273e52014-07-14 16:30:204993
4994 delegate_->UnregisterProtocolHandler(this, protocol, url, user_gesture);
4995}
4996
Marijn Kruisselbrink7f519672019-02-05 19:12:504997void WebContentsImpl::OnAppCacheAccessed(const GURL& manifest_url,
[email protected]b172aee2012-04-10 17:05:264998 bool blocked_by_policy) {
nicka0ac8382016-12-15 23:59:234999 // TODO(nick): Should we consider |source| here? Should we call FilterURL on
5000 // |manifest_url|?
5001
[email protected]7fc4bbb2011-09-08 21:23:105002 // Notify observers about navigation.
ericwilligersde386342016-10-19 02:35:095003 for (auto& observer : observers_)
5004 observer.AppCacheAccessed(manifest_url, blocked_by_policy);
[email protected]7fc4bbb2011-09-08 21:23:105005}
5006
Gyuyoung Kimc5f4ac12020-05-08 10:59:395007void WebContentsImpl::DomOperationResponse(const std::string& json_string) {
5008 // TODO(lukasza): The notification below should probably indicate which
5009 // RenderFrameHostImpl the message is coming from. This can enable tests to
5010 // talk to 2 frames at the same time, without being confused which frame a
5011 // given response comes from. See also a corresponding TODO in the
5012 // ExecuteScriptHelper in //content/public/test/browser_test_utils.cc
5013 NotificationService::current()->Notify(
5014 NOTIFICATION_DOM_OPERATION_RESPONSE, Source<WebContents>(this),
5015 Details<const std::string>(&json_string));
5016}
5017
Gyuyoung Kim4c50517842020-06-04 17:50:445018void WebContentsImpl::SavableResourceLinksResponse(
5019 RenderFrameHostImpl* source,
5020 const std::vector<GURL>& resources_list,
5021 blink::mojom::ReferrerPtr referrer,
5022 const std::vector<blink::mojom::SavableSubframePtr>& subframes) {
5023 save_package_->SavableResourceLinksResponse(source, resources_list,
5024 std::move(referrer), subframes);
5025}
5026
5027void WebContentsImpl::SavableResourceLinksError(RenderFrameHostImpl* source) {
5028 save_package_->SavableResourceLinksError(source);
5029}
5030
Alexander Timin17edc742020-04-23 18:22:185031void WebContentsImpl::OnServiceWorkerAccessed(
5032 RenderFrameHost* render_frame_host,
5033 const GURL& scope,
5034 AllowServiceWorkerResult allowed) {
5035 for (auto& observer : observers_) {
5036 observer.OnServiceWorkerAccessed(render_frame_host, scope, allowed);
5037 }
5038}
5039
5040void WebContentsImpl::OnServiceWorkerAccessed(
5041 NavigationHandle* navigation,
5042 const GURL& scope,
5043 AllowServiceWorkerResult allowed) {
5044 for (auto& observer : observers_) {
5045 observer.OnServiceWorkerAccessed(navigation, scope, allowed);
5046 }
5047}
5048
Miyoung Shincb6475a2019-11-09 10:49:455049void WebContentsImpl::OnColorChooserFactoryReceiver(
Julie Jeongeun Kim8e2879e2019-08-06 23:55:205050 mojo::PendingReceiver<blink::mojom::ColorChooserFactory> receiver) {
5051 color_chooser_factory_receivers_.Add(this, std::move(receiver));
Joel Hockey85b379d2017-12-11 10:42:135052}
5053
5054void WebContentsImpl::OpenColorChooser(
Julie Jeongeun Kim8e2879e2019-08-06 23:55:205055 mojo::PendingReceiver<blink::mojom::ColorChooser> chooser_receiver,
5056 mojo::PendingRemote<blink::mojom::ColorChooserClient> client,
[email protected]8ed16472014-04-11 19:02:485057 SkColor color,
Joel Hockey163835a2017-12-20 11:51:575058 std::vector<blink::mojom::ColorSuggestionPtr> suggestions) {
Joel Hockey85b379d2017-12-11 10:42:135059 content::ColorChooser* new_color_chooser =
Ivan Kotenkov2c0d2bb32017-11-01 15:41:285060 delegate_ ? delegate_->OpenColorChooser(this, color, suggestions)
5061 : nullptr;
[email protected]820957a2014-01-14 14:56:305062 if (!new_color_chooser)
[email protected]cb805452013-05-22 15:16:215063 return;
[email protected]8ed16472014-04-11 19:02:485064
Joel Hockey85b379d2017-12-11 10:42:135065 color_chooser_.reset();
5066 color_chooser_ = std::make_unique<ColorChooser>(
Julie Jeongeun Kim8e2879e2019-08-06 23:55:205067 new_color_chooser, std::move(chooser_receiver), std::move(client));
[email protected]da8543762012-03-20 08:52:205068}
5069
brettw4b461082016-11-19 18:55:165070#if BUILDFLAG(ENABLE_PLUGINS)
nicka0ac8382016-12-15 23:59:235071void WebContentsImpl::OnPepperInstanceCreated(RenderFrameHostImpl* source,
5072 int32_t pp_instance) {
ericwilligersde386342016-10-19 02:35:095073 for (auto& observer : observers_)
5074 observer.PepperInstanceCreated();
nicka0ac8382016-12-15 23:59:235075 pepper_playback_observer_->PepperInstanceCreated(source, pp_instance);
emaxxe70f5e12015-05-29 11:26:005076}
5077
nicka0ac8382016-12-15 23:59:235078void WebContentsImpl::OnPepperInstanceDeleted(RenderFrameHostImpl* source,
5079 int32_t pp_instance) {
ericwilligersde386342016-10-19 02:35:095080 for (auto& observer : observers_)
5081 observer.PepperInstanceDeleted();
nicka0ac8382016-12-15 23:59:235082 pepper_playback_observer_->PepperInstanceDeleted(source, pp_instance);
emaxxe70f5e12015-05-29 11:26:005083}
5084
nicka0ac8382016-12-15 23:59:235085void WebContentsImpl::OnPepperPluginHung(RenderFrameHostImpl* source,
5086 int plugin_child_id,
tommyclie6633ca72014-10-31 00:40:425087 const base::FilePath& path,
5088 bool is_hung) {
Steven Holte95922222018-09-14 20:06:235089 UMA_HISTOGRAM_COUNTS_1M("Pepper.PluginHung", 1);
tommyclie6633ca72014-10-31 00:40:425090
ericwilligersde386342016-10-19 02:35:095091 for (auto& observer : observers_)
5092 observer.PluginHungStatusChanged(plugin_child_id, path, is_hung);
tommyclie6633ca72014-10-31 00:40:425093}
5094
nicka0ac8382016-12-15 23:59:235095void WebContentsImpl::OnPepperStartsPlayback(RenderFrameHostImpl* source,
5096 int32_t pp_instance) {
5097 pepper_playback_observer_->PepperStartsPlayback(source, pp_instance);
zqzhang181047e62016-07-01 13:37:175098}
5099
nicka0ac8382016-12-15 23:59:235100void WebContentsImpl::OnPepperStopsPlayback(RenderFrameHostImpl* source,
5101 int32_t pp_instance) {
5102 pepper_playback_observer_->PepperStopsPlayback(source, pp_instance);
zqzhang181047e62016-07-01 13:37:175103}
5104
nicka0ac8382016-12-15 23:59:235105void WebContentsImpl::OnPluginCrashed(RenderFrameHostImpl* source,
5106 const base::FilePath& plugin_path,
tommyclie6633ca72014-10-31 00:40:425107 base::ProcessId plugin_pid) {
nicka0ac8382016-12-15 23:59:235108 // TODO(nick): Eliminate the |plugin_pid| parameter, which can't be trusted,
Gyuyoung Kim26c7bc92020-04-29 00:53:005109 // and is only used by WebTestControlHost.
ericwilligersde386342016-10-19 02:35:095110 for (auto& observer : observers_)
5111 observer.PluginCrashed(plugin_path, plugin_pid);
tommyclie6633ca72014-10-31 00:40:425112}
5113
[email protected]d8415ad92012-08-23 14:40:505114void WebContentsImpl::OnRequestPpapiBrokerPermission(
nicka0ac8382016-12-15 23:59:235115 RenderViewHostImpl* source,
5116 int ppb_broker_route_id,
[email protected]d8415ad92012-08-23 14:40:505117 const GURL& url,
[email protected]2dec8ec2013-02-07 19:20:345118 const base::FilePath& plugin_path) {
Avi Drissman0a2a967d2019-04-01 19:33:525119 base::OnceCallback<void(bool)> permission_result_callback = base::BindOnce(
nicka0ac8382016-12-15 23:59:235120 &WebContentsImpl::SendPpapiBrokerPermissionResult, base::Unretained(this),
5121 source->GetProcess()->GetID(), ppb_broker_route_id);
[email protected]8c8fc292012-11-23 18:57:165122 if (!delegate_) {
Tommy Nyquist4b749d02018-03-20 21:46:295123 std::move(permission_result_callback).Run(false);
[email protected]8c8fc292012-11-23 18:57:165124 return;
[email protected]d8415ad92012-08-23 14:40:505125 }
5126
Avi Drissman0a2a967d2019-04-01 19:33:525127 delegate_->RequestPpapiBrokerPermission(
5128 this, url, plugin_path, std::move(permission_result_callback));
[email protected]d8415ad92012-08-23 14:40:505129}
5130
nicka0ac8382016-12-15 23:59:235131void WebContentsImpl::SendPpapiBrokerPermissionResult(int process_id,
5132 int ppb_broker_route_id,
5133 bool result) {
5134 RenderProcessHost* rph = RenderProcessHost::FromID(process_id);
5135 if (rph) {
5136 // TODO(nick): Convert this from ViewMsg_ to a Ppapi msg, since it
5137 // is not routed to a RenderView.
5138 rph->Send(
5139 new ViewMsg_PpapiBrokerPermissionResult(ppb_broker_route_id, result));
5140 }
[email protected]d8415ad92012-08-23 14:40:505141}
brettw4b461082016-11-19 18:55:165142#endif // BUILDFLAG(ENABLE_PLUGINS)
[email protected]19be7a62012-10-01 23:03:375143
Miyoung Shin5709ab02020-02-19 06:50:415144void WebContentsImpl::UpdateFaviconURL(
5145 RenderFrameHost* source,
5146 std::vector<blink::mojom::FaviconURLPtr> candidates) {
lukasza178ff092017-06-07 23:39:505147 // Ignore favicons for non-main frame.
5148 if (source->GetParent()) {
5149 NOTREACHED();
5150 return;
5151 }
5152
pkotwiczf9bc20592015-04-21 18:57:065153 // We get updated favicon URLs after the page stops loading. If a cross-site
5154 // navigation occurs while a page is still loading, the initial page
5155 // may stop loading and send us updated favicon URLs after the navigation
5156 // for the new page has committed.
lukasza178ff092017-06-07 23:39:505157 if (!source->IsCurrent())
pkotwiczf9bc20592015-04-21 18:57:065158 return;
5159
Miyoung Shin5709ab02020-02-19 06:50:415160 favicon_urls_ = std::move(candidates);
Mounir Lamouri7b4a1f232019-12-23 16:32:105161
ericwilligersde386342016-10-19 02:35:095162 for (auto& observer : observers_)
Yuzu Saijo0c263c702020-06-08 03:41:245163 observer.DidUpdateFaviconURL(source, favicon_urls_);
[email protected]795c28972012-12-06 06:13:395164}
5165
qinmin72e8bd02016-10-21 19:35:375166void WebContentsImpl::SetIsOverlayContent(bool is_overlay_content) {
5167 is_overlay_content_ = is_overlay_content;
5168}
5169
Albert J. Wongfb64e142018-10-16 01:10:455170void WebContentsImpl::DidFirstVisuallyNonEmptyPaint(
5171 RenderViewHostImpl* source) {
nicka0ac8382016-12-15 23:59:235172 // TODO(nick): When this is ported to FrameHostMsg_, we should only listen if
5173 // |source| is the main frame.
ericwilligersde386342016-10-19 02:35:095174 for (auto& observer : observers_)
5175 observer.DidFirstVisuallyNonEmptyPaint();
yusufod41c5f92015-03-06 00:14:285176
Carlos Caballero1215f882019-10-29 15:58:435177 if (source->theme_color() != last_sent_theme_color_) {
yusufod41c5f92015-03-06 00:14:285178 // Theme color should have updated by now if there was one.
ericwilligersde386342016-10-19 02:35:095179 for (auto& observer : observers_)
Peter Kastingb14e5782020-01-06 23:55:045180 observer.DidChangeThemeColor();
Carlos Caballero1215f882019-10-29 15:58:435181 last_sent_theme_color_ = source->theme_color();
yusufod41c5f92015-03-06 00:14:285182 }
[email protected]9f268072013-11-07 00:02:155183}
[email protected]d9030b82013-07-19 08:26:065184
Kevin McNee068c3eb2020-04-03 18:24:195185bool WebContentsImpl::IsPortal() {
Adithya Srinivasan46b8a792019-02-01 14:47:235186 return portal();
5187}
5188
[email protected]17e286e2013-03-01 23:29:395189void WebContentsImpl::NotifyBeforeFormRepostWarningShow() {
ericwilligersde386342016-10-19 02:35:095190 for (auto& observer : observers_)
5191 observer.BeforeFormRepostWarningShow();
[email protected]17e286e2013-03-01 23:29:395192}
5193
[email protected]ec6c05f2013-10-23 18:41:575194void WebContentsImpl::ActivateAndShowRepostFormWarningDialog() {
5195 Activate();
5196 if (delegate_)
5197 delegate_->ShowRepostFormWarningDialog(this);
5198}
5199
[email protected]b4c84012014-04-28 19:51:105200bool WebContentsImpl::HasAccessedInitialDocument() {
5201 return has_accessed_initial_document_;
5202}
5203
afakhry6f0c1ec22016-07-14 13:55:135204void WebContentsImpl::UpdateTitleForEntry(NavigationEntry* entry,
[email protected]fcf75d42013-12-03 20:11:265205 const base::string16& title) {
[email protected]fcf75d42013-12-03 20:11:265206 base::string16 final_title;
Avi Drissman93002212017-09-27 03:20:525207 base::TrimWhitespace(title, base::TRIM_ALL, &final_title);
[email protected]420ae012009-04-24 05:16:325208
[email protected]987fc3a2011-05-26 14:18:095209 // If a page is created via window.open and never navigated,
5210 // there will be no navigation entry. In this situation,
[email protected]73eb2602012-02-09 19:50:555211 // |page_title_when_no_navigation_entry_| will be used for page title.
[email protected]987fc3a2011-05-26 14:18:095212 if (entry) {
[email protected]36fc0392011-12-25 03:59:515213 if (final_title == entry->GetTitle())
afakhry6f0c1ec22016-07-14 13:55:135214 return; // Nothing changed, don't bother.
[email protected]420ae012009-04-24 05:16:325215
[email protected]36fc0392011-12-25 03:59:515216 entry->SetTitle(final_title);
Avi Drissman93002212017-09-27 03:20:525217
5218 // The title for display may differ from the title just set; grab it.
5219 final_title = entry->GetTitleForDisplay();
[email protected]987fc3a2011-05-26 14:18:095220 } else {
5221 if (page_title_when_no_navigation_entry_ == final_title)
afakhry6f0c1ec22016-07-14 13:55:135222 return; // Nothing changed, don't bother.
[email protected]987fc3a2011-05-26 14:18:095223
5224 page_title_when_no_navigation_entry_ = final_title;
5225 }
[email protected]420ae012009-04-24 05:16:325226
[email protected]420ae012009-04-24 05:16:325227 // Lastly, set the title for the view.
[email protected]790e2fd2011-09-21 20:28:235228 view_->SetPageTitle(final_title);
[email protected]420ae012009-04-24 05:16:325229
ericwilligersde386342016-10-19 02:35:095230 for (auto& observer : observers_)
Avi Drissman93002212017-09-27 03:20:525231 observer.TitleWasSet(entry);
[email protected]66798902013-10-01 18:40:165232
afakhry6f0c1ec22016-07-14 13:55:135233 // Broadcast notifications when the UI should be updated.
5234 if (entry == controller_.GetEntryAtOffset(0))
5235 NotifyNavigationStateChanged(INVALIDATE_TYPE_TITLE);
[email protected]420ae012009-04-24 05:16:325236}
5237
fdegansa696e5112015-04-17 01:57:595238void WebContentsImpl::SendChangeLoadProgress() {
[email protected]960b0372014-05-19 18:01:005239 loading_last_progress_update_ = base::TimeTicks::Now();
Evan Stade9f328112019-10-23 20:49:255240 for (auto& observer : observers_)
5241 observer.LoadProgressChanged(frame_tree_.load_progress());
[email protected]960b0372014-05-19 18:01:005242}
5243
5244void WebContentsImpl::ResetLoadProgressState() {
fdegans1d16355162015-03-26 11:58:345245 frame_tree_.ResetLoadProgress();
[email protected]960b0372014-05-19 18:01:005246 loading_weak_factory_.InvalidateWeakPtrs();
5247 loading_last_progress_update_ = base::TimeTicks();
5248}
5249
clamy44e84ce2016-02-22 15:38:255250// Notifies the RenderWidgetHost instance about the fact that the page is
5251// loading, or done loading.
5252void WebContentsImpl::LoadingStateChanged(bool to_different_document,
clamy44e84ce2016-02-22 15:38:255253 LoadNotificationDetails* details) {
Nate Chapin22ea6592019-03-05 22:29:025254 if (is_being_destroyed_)
5255 return;
clamy44e84ce2016-02-22 15:38:255256
5257 bool is_loading = IsLoading();
5258
5259 if (!is_loading) {
5260 load_state_ = net::LoadStateWithParam(net::LOAD_STATE_IDLE,
5261 base::string16());
5262 load_state_host_.clear();
5263 upload_size_ = 0;
5264 upload_position_ = 0;
5265 }
5266
5267 GetRenderManager()->SetIsLoading(is_loading);
5268
5269 waiting_for_response_ = is_loading;
5270 is_load_to_different_document_ = to_different_document;
5271
5272 if (delegate_)
5273 delegate_->LoadingStateChanged(this, to_different_document);
5274 NotifyNavigationStateChanged(INVALIDATE_TYPE_LOAD);
5275
5276 std::string url = (details ? details->url.possibly_invalid_spec() : "NULL");
5277 if (is_loading) {
5278 TRACE_EVENT_ASYNC_BEGIN2("browser,navigation", "WebContentsImpl Loading",
5279 this, "URL", url, "Main FrameTreeNode id",
5280 GetFrameTree()->root()->frame_tree_node_id());
ericwilligersde386342016-10-19 02:35:095281 for (auto& observer : observers_)
5282 observer.DidStartLoading();
clamy44e84ce2016-02-22 15:38:255283 } else {
5284 TRACE_EVENT_ASYNC_END1("browser,navigation", "WebContentsImpl Loading",
5285 this, "URL", url);
ericwilligersde386342016-10-19 02:35:095286 for (auto& observer : observers_)
5287 observer.DidStopLoading();
clamy44e84ce2016-02-22 15:38:255288 }
5289
5290 // TODO(avi): Remove. http://crbug.com/170921
5291 int type = is_loading ? NOTIFICATION_LOAD_START : NOTIFICATION_LOAD_STOP;
5292 NotificationDetails det = NotificationService::NoDetails();
5293 if (details)
5294 det = Details<LoadNotificationDetails>(details);
5295 NotificationService::current()->Notify(
5296 type, Source<NavigationController>(&controller_), det);
5297}
5298
danakja0bce4c952020-05-12 20:40:285299void WebContentsImpl::NotifyViewSwapped(RenderViewHost* old_view,
5300 RenderViewHost* new_view) {
5301 DCHECK_NE(old_view, new_view);
[email protected]420ae012009-04-24 05:16:325302 // After sending out a swap notification, we need to send a disconnect
5303 // notification so that clients that pick up a pointer to |this| can NULL the
5304 // pointer. See Bug 1230284.
5305 notify_disconnection_ = true;
ericwilligersde386342016-10-19 02:35:095306 for (auto& observer : observers_)
danakja0bce4c952020-05-12 20:40:285307 observer.RenderViewHostChanged(old_view, new_view);
[email protected]da7a7182013-09-06 08:11:115308
danakja0bce4c952020-05-12 20:40:285309 view_->RenderViewHostChanged(old_view, new_view);
Jinsuk Kimdc5284c22018-07-11 02:22:065310
Lucas Furukawa Gadanif9ea4fc2018-08-03 23:57:135311 // If this is an inner WebContents that has swapped views, we need to reattach
5312 // it to its outer WebContents.
5313 if (node_.outer_web_contents())
5314 ReattachToOuterWebContentsFrame();
5315
[email protected]7a846df2012-09-20 19:17:395316 // Ensure that the associated embedder gets cleared after a RenderViewHost
5317 // gets swapped, so we don't reuse the same embedder next time a
5318 // RenderViewHost is attached to this WebContents.
5319 RemoveBrowserPluginEmbedder();
[email protected]420ae012009-04-24 05:16:325320}
5321
danakja0bce4c952020-05-12 20:40:285322void WebContentsImpl::NotifyFrameSwapped(RenderFrameHost* old_frame,
5323 RenderFrameHost* new_frame,
Jinsuk Kim895b2c52018-07-03 06:49:165324 bool is_main_frame) {
Bo Liu168c8642017-08-28 18:26:025325#if defined(OS_ANDROID)
danakja0bce4c952020-05-12 20:40:285326 // Copy importance from |old_frame| if |new_frame| is a main frame.
5327 if (old_frame && !new_frame->GetParent()) {
5328 RenderWidgetHostImpl* old_widget =
5329 static_cast<RenderFrameHostImpl*>(old_frame)->GetRenderWidgetHost();
5330 RenderWidgetHostImpl* new_widget =
5331 static_cast<RenderFrameHostImpl*>(new_frame)->GetRenderWidgetHost();
5332 new_widget->SetImportance(old_widget->importance());
Bo Liu7c6779e92017-08-16 02:02:285333 }
Bo Liu168c8642017-08-28 18:26:025334#endif
ericwilligersde386342016-10-19 02:35:095335 for (auto& observer : observers_)
danakja0bce4c952020-05-12 20:40:285336 observer.RenderFrameHostChanged(old_frame, new_frame);
[email protected]02d7b6e2014-06-24 21:01:505337}
5338
[email protected]da7a7182013-09-06 08:11:115339// TODO(avi): Remove this entire function because this notification is already
5340// covered by two observer functions. http://crbug.com/170921
[email protected]b172aee2012-04-10 17:05:265341void WebContentsImpl::NotifyDisconnected() {
[email protected]420ae012009-04-24 05:16:325342 if (!notify_disconnection_)
5343 return;
5344
5345 notify_disconnection_ = false;
[email protected]8ff00d72012-10-23 19:12:215346 NotificationService::current()->Notify(
5347 NOTIFICATION_WEB_CONTENTS_DISCONNECTED,
5348 Source<WebContents>(this),
5349 NotificationService::NoDetails());
[email protected]420ae012009-04-24 05:16:325350}
5351
[email protected]cbb1ef592013-06-05 19:49:465352void WebContentsImpl::NotifyNavigationEntryCommitted(
5353 const LoadCommittedDetails& load_details) {
ericwilligersde386342016-10-19 02:35:095354 for (auto& observer : observers_)
5355 observer.NavigationEntryCommitted(load_details);
[email protected]cbb1ef592013-06-05 19:49:465356}
5357
Sam McNally5c087a32017-08-25 01:46:145358void WebContentsImpl::NotifyNavigationEntryChanged(
5359 const EntryChangedDetails& change_details) {
5360 for (auto& observer : observers_)
5361 observer.NavigationEntryChanged(change_details);
5362}
5363
5364void WebContentsImpl::NotifyNavigationListPruned(
5365 const PrunedDetails& pruned_details) {
5366 for (auto& observer : observers_)
5367 observer.NavigationListPruned(pruned_details);
5368}
5369
Christian Dullweber1af31e62018-02-22 11:49:485370void WebContentsImpl::NotifyNavigationEntriesDeleted() {
5371 for (auto& observer : observers_)
5372 observer.NavigationEntriesDeleted();
5373}
5374
rockotf62002a2016-09-15 00:08:595375void WebContentsImpl::OnAssociatedInterfaceRequest(
5376 RenderFrameHost* render_frame_host,
5377 const std::string& interface_name,
5378 mojo::ScopedInterfaceEndpointHandle handle) {
Julie Jeongeun Kim7d0986172019-12-04 03:17:255379 auto it = receiver_sets_.find(interface_name);
5380 if (it != receiver_sets_.end())
5381 it->second->OnReceiverForFrame(render_frame_host, std::move(handle));
rockotf62002a2016-09-15 00:08:595382}
5383
Ben Goodger9e24929e2017-07-21 03:13:405384void WebContentsImpl::OnInterfaceRequest(
5385 RenderFrameHost* render_frame_host,
5386 const std::string& interface_name,
5387 mojo::ScopedMessagePipeHandle* interface_pipe) {
5388 for (auto& observer : observers_) {
5389 observer.OnInterfaceRequestFromFrame(render_frame_host, interface_name,
5390 interface_pipe);
5391 if (!interface_pipe->is_valid())
5392 break;
5393 }
5394}
5395
Varun Khaneja5da94302019-06-25 04:48:105396void WebContentsImpl::OnDidBlockNavigation(
5397 const GURL& blocked_url,
5398 const GURL& initiator_url,
Abhijeet Kandalkarde7348e2020-01-13 06:06:545399 blink::mojom::NavigationBlockedReason reason) {
Nicolas Dossou-gbetebcab2472017-09-09 12:28:405400 if (delegate_)
Varun Khaneja5da94302019-06-25 04:48:105401 delegate_->OnDidBlockNavigation(this, blocked_url, initiator_url, reason);
Nicolas Dossou-gbetebcab2472017-09-09 12:28:405402}
5403
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:415404const GURL& WebContentsImpl::GetMainFrameLastCommittedURL() {
[email protected]c31a84802014-04-03 15:55:495405 return GetLastCommittedURL();
5406}
5407
[email protected]b849847b2013-12-10 21:57:585408void WebContentsImpl::RenderFrameCreated(RenderFrameHost* render_frame_host) {
ericwilligersde386342016-10-19 02:35:095409 for (auto& observer : observers_)
5410 observer.RenderFrameCreated(render_frame_host);
dmazzonif8a31042016-12-03 00:52:135411 UpdateAccessibilityModeOnFrame(render_frame_host);
clamy91b7abf2016-03-24 15:17:455412
Becca Hughesd11d6502018-07-31 17:01:285413 if (display_cutout_host_impl_)
5414 display_cutout_host_impl_->RenderFrameCreated(render_frame_host);
5415
clamy91b7abf2016-03-24 15:17:455416 if (!render_frame_host->IsRenderFrameLive() || render_frame_host->GetParent())
5417 return;
5418
5419 NavigationEntry* entry = controller_.GetPendingEntry();
5420 if (entry && entry->IsViewSourceMode()) {
5421 // Put the renderer in view source mode.
Dave Tapuska3c05e1c2019-11-27 18:34:185422 static_cast<RenderFrameHostImpl*>(render_frame_host)
5423 ->GetAssociatedLocalFrame()
5424 ->EnableViewSourceMode();
clamy91b7abf2016-03-24 15:17:455425 }
[email protected]b849847b2013-12-10 21:57:585426}
5427
5428void WebContentsImpl::RenderFrameDeleted(RenderFrameHost* render_frame_host) {
yilkal172da772019-11-06 22:49:075429 if (IsBeingDestroyed() && !render_frame_host->GetParent() &&
5430 first_navigation_completed_ &&
Hajime Hoshibbc509c2020-04-06 08:55:085431 !render_frame_host->IsInBackForwardCache()) {
yilkal796e89e2019-10-02 23:58:285432 // Main frame has been deleted because WebContents is being destroyed.
Rakina Zata Amni74627df2019-10-15 02:03:175433 // Note that we aren't recording this here when the main frame is in the
5434 // back-forward cache because that means we've actually already navigated
5435 // away from it (and we got to this point because the WebContents is
yilkal172da772019-11-06 22:49:075436 // deleted), which means |max_loaded_frame_count_| is already overwritten.
5437 // The |max_loaded_frame_count_| value will instead be recorded from within
5438 // |WebContentsImpl::DidFinishNavigation()|.
5439 RecordMaxFrameCountUMA(max_loaded_frame_count_);
yilkal796e89e2019-10-02 23:58:285440 }
5441
creis3638897452017-05-09 22:07:505442 is_notifying_observers_ = true;
ericwilligersde386342016-10-19 02:35:095443 for (auto& observer : observers_)
5444 observer.RenderFrameDeleted(render_frame_host);
creis3638897452017-05-09 22:07:505445 is_notifying_observers_ = false;
zqzhang7b6f4f02016-12-07 22:10:225446#if BUILDFLAG(ENABLE_PLUGINS)
5447 pepper_playback_observer_->RenderFrameDeleted(render_frame_host);
5448#endif
Becca Hughesfd5d8f892018-06-14 18:23:365449
Becca Hughesd11d6502018-07-31 17:01:285450 if (display_cutout_host_impl_)
5451 display_cutout_host_impl_->RenderFrameDeleted(render_frame_host);
5452
Becca Hughesfd5d8f892018-06-14 18:23:365453 // Remove any fullscreen state that the frame has stored.
5454 FullscreenStateChanged(render_frame_host, false /* is_fullscreen */);
[email protected]b849847b2013-12-10 21:57:585455}
5456
[email protected]a09d53ce2014-01-31 00:46:425457void WebContentsImpl::ShowContextMenu(RenderFrameHost* render_frame_host,
5458 const ContextMenuParams& params) {
avie9fd8722017-04-10 16:17:585459 // If a renderer fires off a second command to show a context menu before the
5460 // first context menu is closed, just ignore it. https://crbug.com/707534
ekaramadf6750aa2017-06-06 18:29:425461 if (showing_context_menu_)
avie9fd8722017-04-10 16:17:585462 return;
5463
[email protected]077e704b2014-05-23 19:24:105464 ContextMenuParams context_menu_params(params);
[email protected]a09d53ce2014-01-31 00:46:425465 // Allow WebContentsDelegates to handle the context menu operation first.
Joel Klinghed0ea22ea2019-02-20 22:04:225466 if (delegate_ &&
5467 delegate_->HandleContextMenu(render_frame_host, context_menu_params))
[email protected]a09d53ce2014-01-31 00:46:425468 return;
5469
[email protected]077e704b2014-05-23 19:24:105470 render_view_host_delegate_view_->ShowContextMenu(render_frame_host,
5471 context_menu_params);
[email protected]a09d53ce2014-01-31 00:46:425472}
5473
Avi Drissman4891b852018-09-25 18:12:585474namespace {
5475// Normalizes the line endings: \r\n -> \n, lone \r -> \n.
5476base::string16 NormalizeLineBreaks(const base::string16& source) {
5477 static const base::NoDestructor<base::string16> kReturnNewline(
5478 base::ASCIIToUTF16("\r\n"));
5479 static const base::NoDestructor<base::string16> kReturn(
5480 base::ASCIIToUTF16("\r"));
5481 static const base::NoDestructor<base::string16> kNewline(
5482 base::ASCIIToUTF16("\n"));
5483
5484 std::vector<base::StringPiece16> pieces;
5485
5486 for (const auto& rn_line : base::SplitStringPieceUsingSubstr(
5487 source, *kReturnNewline, base::KEEP_WHITESPACE,
5488 base::SPLIT_WANT_ALL)) {
5489 auto r_lines = base::SplitStringPieceUsingSubstr(
5490 rn_line, *kReturn, base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL);
5491 std::move(std::begin(r_lines), std::end(r_lines),
5492 std::back_inserter(pieces));
5493 }
5494
5495 return base::JoinString(pieces, *kNewline);
5496}
5497} // namespace
5498
Dave Tapuskacdf545cc2020-01-23 18:38:525499void WebContentsImpl::RunJavaScriptDialog(
5500 RenderFrameHost* render_frame_host,
5501 const base::string16& message,
5502 const base::string16& default_prompt,
5503 JavaScriptDialogType dialog_type,
5504 JavaScriptDialogCallback response_callback) {
Avi Drissman2ccbb402018-03-20 21:11:365505 // Ensure that if showing a dialog is the first thing that a page does, that
5506 // the contents of the previous page aren't shown behind it. This is required
5507 // because showing a dialog freezes the renderer, so no frames will be coming
5508 // from it. https://crbug.com/823353
5509 auto* render_widget_host_impl =
5510 static_cast<RenderFrameHostImpl*>(render_frame_host)
5511 ->GetRenderWidgetHost();
5512 if (render_widget_host_impl)
5513 render_widget_host_impl->ForceFirstFrameAfterNavigationTimeout();
5514
avi0720b02e2017-06-13 03:22:135515 // Running a dialog causes an exit to webpage-initiated fullscreen.
5516 // http://crbug.com/728276
Avi Drissman1a55a9d62020-03-10 18:56:455517 base::ScopedClosureRunner fullscreen_block = ForSecurityDropFullscreen();
avi0720b02e2017-06-13 03:22:135518
Avi Drissman1a55a9d62020-03-10 18:56:455519 auto callback =
5520 base::BindOnce(&WebContentsImpl::OnDialogClosed, base::Unretained(this),
5521 render_frame_host->GetProcess()->GetID(),
5522 render_frame_host->GetRoutingID(),
5523 std::move(response_callback), std::move(fullscreen_block));
Pavel Feldman3c1842b2017-08-02 05:00:165524
Pavel Feldman3c1842b2017-08-02 05:00:165525 std::vector<protocol::PageHandler*> page_handlers =
5526 protocol::PageHandler::EnabledForWebContents(this);
5527
Pavel Feldmanfab3cb62017-10-05 01:02:015528 if (delegate_)
5529 dialog_manager_ = delegate_->GetJavaScriptDialogManager(this);
5530
Charlie Reis96077362019-07-04 00:34:045531 // While a JS message dialog is showing, defer commits in this WebContents.
5532 javascript_dialog_navigation_deferrer_ =
5533 std::make_unique<JavaScriptDialogNavigationDeferrer>();
5534
Carlos IL07f282f2020-05-15 22:58:295535 // Suppress JavaScript dialogs when requested.
5536 bool should_suppress = delegate_ && delegate_->ShouldSuppressDialogs(this);
Pavel Feldman1c587bc52018-04-04 21:16:055537 bool has_non_devtools_handlers = delegate_ && dialog_manager_;
5538 bool has_handlers = page_handlers.size() || has_non_devtools_handlers;
Pavel Feldmanfab3cb62017-10-05 01:02:015539 bool suppress_this_message = should_suppress || !has_handlers;
5540
5541 if (suppress_this_message) {
Avi Drissmane04d3992017-10-05 15:11:365542 std::move(callback).Run(true, false, base::string16());
Pavel Feldman3c1842b2017-08-02 05:00:165543 return;
5544 }
5545
5546 scoped_refptr<CloseDialogCallbackWrapper> wrapper =
Avi Drissmane04d3992017-10-05 15:11:365547 new CloseDialogCallbackWrapper(std::move(callback));
Pavel Feldman3c1842b2017-08-02 05:00:165548
5549 is_showing_javascript_dialog_ = true;
5550
Avi Drissman4891b852018-09-25 18:12:585551 base::string16 normalized_message = NormalizeLineBreaks(message);
5552
Pavel Feldman3c1842b2017-08-02 05:00:165553 for (auto* handler : page_handlers) {
Avi Drissmane04d3992017-10-05 15:11:365554 handler->DidRunJavaScriptDialog(
Avi Drissman4891b852018-09-25 18:12:585555 render_frame_host->GetLastCommittedURL(), normalized_message,
5556 default_prompt, dialog_type, has_non_devtools_handlers,
Avi Drissmane04d3992017-10-05 15:11:365557 base::BindOnce(&CloseDialogCallbackWrapper::Run, wrapper, false));
Pavel Feldman3c1842b2017-08-02 05:00:165558 }
5559
5560 if (dialog_manager_) {
[email protected]87de04b02014-04-08 22:14:495561 dialog_manager_->RunJavaScriptDialog(
Avi Drissman4891b852018-09-25 18:12:585562 this, render_frame_host, dialog_type, normalized_message,
5563 default_prompt,
Avi Drissmane04d3992017-10-05 15:11:365564 base::BindOnce(&CloseDialogCallbackWrapper::Run, wrapper, false),
5565 &suppress_this_message);
[email protected]87de04b02014-04-08 22:14:495566 }
5567
5568 if (suppress_this_message) {
5569 // If we are suppressing messages, just reply as if the user immediately
5570 // pressed "Cancel", passing true to |dialog_was_suppressed|.
Avi Drissmane04d3992017-10-05 15:11:365571 wrapper->Run(true, false, base::string16());
[email protected]87de04b02014-04-08 22:14:495572 }
[email protected]87de04b02014-04-08 22:14:495573}
5574
5575void WebContentsImpl::RunBeforeUnloadConfirm(
[email protected]39f2f9252014-05-03 00:54:015576 RenderFrameHost* render_frame_host,
[email protected]87de04b02014-04-08 22:14:495577 bool is_reload,
Dave Tapuskacdf545cc2020-01-23 18:38:525578 JavaScriptDialogCallback response_callback) {
Avi Drissman2ccbb402018-03-20 21:11:365579 // Ensure that if showing a dialog is the first thing that a page does, that
5580 // the contents of the previous page aren't shown behind it. This is required
5581 // because showing a dialog freezes the renderer, so no frames will be coming
5582 // from it. https://crbug.com/823353
5583 auto* render_widget_host_impl =
5584 static_cast<RenderFrameHostImpl*>(render_frame_host)
5585 ->GetRenderWidgetHost();
5586 if (render_widget_host_impl)
5587 render_widget_host_impl->ForceFirstFrameAfterNavigationTimeout();
5588
avi0720b02e2017-06-13 03:22:135589 // Running a dialog causes an exit to webpage-initiated fullscreen.
5590 // http://crbug.com/728276
Avi Drissman1a55a9d62020-03-10 18:56:455591 base::ScopedClosureRunner fullscreen_block = ForSecurityDropFullscreen();
avi0720b02e2017-06-13 03:22:135592
[email protected]39f2f9252014-05-03 00:54:015593 RenderFrameHostImpl* rfhi =
5594 static_cast<RenderFrameHostImpl*>(render_frame_host);
[email protected]87de04b02014-04-08 22:14:495595 if (delegate_)
5596 delegate_->WillRunBeforeUnloadConfirm();
5597
Avi Drissman1a55a9d62020-03-10 18:56:455598 auto callback =
5599 base::BindOnce(&WebContentsImpl::OnDialogClosed, base::Unretained(this),
5600 render_frame_host->GetProcess()->GetID(),
5601 render_frame_host->GetRoutingID(),
5602 std::move(response_callback), std::move(fullscreen_block));
Pavel Feldman3c1842b2017-08-02 05:00:165603
Pavel Feldman3c1842b2017-08-02 05:00:165604 std::vector<protocol::PageHandler*> page_handlers =
5605 protocol::PageHandler::EnabledForWebContents(this);
5606
Pavel Feldmanfab3cb62017-10-05 01:02:015607 if (delegate_)
5608 dialog_manager_ = delegate_->GetJavaScriptDialogManager(this);
5609
Charlie Reis96077362019-07-04 00:34:045610 // While a JS beforeunload dialog is showing, defer commits in this
5611 // WebContents.
5612 javascript_dialog_navigation_deferrer_ =
5613 std::make_unique<JavaScriptDialogNavigationDeferrer>();
5614
Carlos IL07f282f2020-05-15 22:58:295615 bool should_suppress = !rfhi->is_active() ||
Tarun Bansalc209b742019-12-24 06:53:255616 (delegate_ && delegate_->ShouldSuppressDialogs(this));
Pavel Feldman1c587bc52018-04-04 21:16:055617 bool has_non_devtools_handlers = delegate_ && dialog_manager_;
5618 bool has_handlers = page_handlers.size() || has_non_devtools_handlers;
Pavel Feldmanfab3cb62017-10-05 01:02:015619 if (should_suppress || !has_handlers) {
Avi Drissmane04d3992017-10-05 15:11:365620 std::move(callback).Run(false, true, base::string16());
[email protected]87de04b02014-04-08 22:14:495621 return;
5622 }
5623
5624 is_showing_before_unload_dialog_ = true;
Pavel Feldman3c1842b2017-08-02 05:00:165625
5626 scoped_refptr<CloseDialogCallbackWrapper> wrapper =
Avi Drissmane04d3992017-10-05 15:11:365627 new CloseDialogCallbackWrapper(std::move(callback));
Pavel Feldman3c1842b2017-08-02 05:00:165628
5629 GURL frame_url = rfhi->GetLastCommittedURL();
5630 for (auto* handler : page_handlers) {
Avi Drissmane04d3992017-10-05 15:11:365631 handler->DidRunBeforeUnloadConfirm(
Pavel Feldman1c587bc52018-04-04 21:16:055632 frame_url, has_non_devtools_handlers,
Avi Drissmane04d3992017-10-05 15:11:365633 base::BindOnce(&CloseDialogCallbackWrapper::Run, wrapper, false));
Pavel Feldman3c1842b2017-08-02 05:00:165634 }
5635
5636 if (dialog_manager_) {
Avi Drissmane04d3992017-10-05 15:11:365637 dialog_manager_->RunBeforeUnloadDialog(
Avi Drissmanff9ed752017-11-28 21:18:125638 this, render_frame_host, is_reload,
Avi Drissmane04d3992017-10-05 15:11:365639 base::BindOnce(&CloseDialogCallbackWrapper::Run, wrapper, false));
Pavel Feldman3c1842b2017-08-02 05:00:165640 }
[email protected]87de04b02014-04-08 22:14:495641}
5642
Kent Tamurac260c9a2018-09-14 09:10:565643void WebContentsImpl::RunFileChooser(
5644 RenderFrameHost* render_frame_host,
Avi Drissman1a55a9d62020-03-10 18:56:455645 std::unique_ptr<FileChooserImpl::FileSelectListenerImpl> listener,
Kent Tamurac260c9a2018-09-14 09:10:565646 const blink::mojom::FileChooserParams& params) {
Avi Drissman3d41e7712018-08-27 21:18:085647 // Any explicit focusing of another window while this WebContents is in
5648 // fullscreen can be used to confuse the user, so drop fullscreen.
Avi Drissman1a55a9d62020-03-10 18:56:455649 base::ScopedClosureRunner fullscreen_block = ForSecurityDropFullscreen();
5650 listener->SetFullscreenBlock(std::move(fullscreen_block));
Avi Drissman3d41e7712018-08-27 21:18:085651
naskoada75b22016-06-11 16:09:465652 if (delegate_)
Kent Tamura512a27e2018-10-04 00:49:325653 delegate_->RunFileChooser(render_frame_host, std::move(listener), params);
5654 else
5655 listener->FileSelectionCanceled();
naskoada75b22016-06-11 16:09:465656}
5657
[email protected]a86c0e962013-12-17 17:10:395658WebContents* WebContentsImpl::GetAsWebContents() {
5659 return this;
5660}
5661
mcnee336ea2c2017-05-23 22:50:595662#if !defined(OS_ANDROID)
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:415663double WebContentsImpl::GetPendingPageZoomLevel() {
wjmaclean64951902016-04-29 20:59:125664 NavigationEntry* pending_entry = GetController().GetPendingEntry();
5665 if (!pending_entry)
5666 return HostZoomMap::GetZoomLevel(this);
5667
5668 GURL url = pending_entry->GetURL();
5669 return HostZoomMap::GetForWebContents(this)->GetZoomLevelForHostAndScheme(
5670 url.scheme(), net::GetHostOrSpecFromURL(url));
5671}
mcnee336ea2c2017-05-23 22:50:595672#endif // !defined(OS_ANDROID)
wjmaclean64951902016-04-29 20:59:125673
qinmin72e8bd02016-10-21 19:35:375674bool WebContentsImpl::HideDownloadUI() const {
5675 return is_overlay_content_;
5676}
5677
mlamouri5cd9ae82017-02-18 11:05:095678bool WebContentsImpl::HasPersistentVideo() const {
5679 return has_persistent_video_;
5680}
5681
Ian Vollick5d5c37f2019-06-19 22:50:575682bool WebContentsImpl::IsSpatialNavigationDisabled() const {
5683 return is_spatial_navigation_disabled_;
5684}
5685
Albert J. Wong65fe64d2019-09-20 02:48:145686RenderFrameHostImpl* WebContentsImpl::GetPendingMainFrame() {
clamy610c63b32017-12-22 15:05:185687 return GetRenderManager()->speculative_frame_host();
Alex Moshchuk27caae82017-09-11 23:11:185688}
5689
mlamourie52c1b542017-02-23 14:57:505690bool WebContentsImpl::HasActiveEffectivelyFullscreenVideo() const {
5691 return media_web_contents_observer_->HasActiveEffectivelyFullscreenVideo();
5692}
5693
François Beaufortad6c5232018-02-26 11:00:445694bool WebContentsImpl::IsPictureInPictureAllowedForFullscreenVideo() const {
5695 return media_web_contents_observer_
5696 ->IsPictureInPictureAllowedForFullscreenVideo();
5697}
5698
ekaramada110f642016-12-21 19:47:285699bool WebContentsImpl::IsFocusedElementEditable() {
5700 RenderFrameHostImpl* frame = GetFocusedFrame();
5701 return frame && frame->has_focused_editable_element();
5702}
5703
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:415704bool WebContentsImpl::IsShowingContextMenu() {
ekaramadf6750aa2017-06-06 18:29:425705 return showing_context_menu_;
5706}
5707
5708void WebContentsImpl::SetShowingContextMenu(bool showing) {
5709 DCHECK_NE(showing_context_menu_, showing);
5710 showing_context_menu_ = showing;
5711
5712 if (auto* view = GetRenderWidgetHostView()) {
5713 // Notify the main frame's RWHV to run the platform-specific code, if any.
5714 static_cast<RenderWidgetHostViewBase*>(view)->SetShowingContextMenu(
5715 showing);
5716 }
5717}
5718
ekaramada110f642016-12-21 19:47:285719void WebContentsImpl::ClearFocusedElement() {
5720 if (auto* frame = GetFocusedFrame())
5721 frame->ClearFocusedElement();
5722}
5723
danakj77eb7e82020-01-09 19:38:465724bool WebContentsImpl::IsNeverComposited() {
[email protected]7912e822014-04-16 02:37:035725 if (!delegate_)
5726 return false;
danakj77eb7e82020-01-09 19:38:465727 return delegate_->IsNeverComposited(this);
[email protected]7912e822014-04-16 02:37:035728}
5729
[email protected]5a3bdf52012-05-24 15:12:575730RenderViewHostDelegateView* WebContentsImpl::GetDelegateView() {
5731 return render_view_host_delegate_view_;
[email protected]420ae012009-04-24 05:16:325732}
5733
Leon Hanc819dc62019-01-28 04:30:195734blink::mojom::RendererPreferences WebContentsImpl::GetRendererPrefs(
[email protected]8ff00d72012-10-23 19:12:215735 BrowserContext* browser_context) const {
[email protected]840b1512009-07-21 16:53:465736 return renderer_preferences_;
[email protected]80d96fa2009-06-10 22:34:515737}
5738
Lucas Furukawa Gadani2ec00c82018-12-14 15:53:165739RenderFrameHostImpl* WebContentsImpl::GetOuterWebContentsFrame() {
5740 if (GetOuterDelegateFrameTreeNodeId() ==
5741 FrameTreeNode::kFrameTreeNodeInvalidId) {
5742 return nullptr;
5743 }
5744
5745 FrameTreeNode* outer_node =
5746 FrameTreeNode::GloballyFindByID(GetOuterDelegateFrameTreeNodeId());
5747 // The outer node should be in the outer WebContents.
5748 DCHECK_EQ(outer_node->frame_tree(), GetOuterWebContents()->GetFrameTree());
Alexander Timin381e7e182020-04-28 19:04:035749 return outer_node->parent();
Lucas Furukawa Gadani2ec00c82018-12-14 15:53:165750}
5751
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:415752WebContentsImpl* WebContentsImpl::GetOuterWebContents() {
Lucas Furukawa Gadani105de1e812018-05-31 19:38:395753 return node_.outer_web_contents();
avallee0206f782016-07-28 18:55:335754}
5755
Lucas Furukawa Gadani2ec00c82018-12-14 15:53:165756std::vector<WebContents*> WebContentsImpl::GetInnerWebContents() {
5757 std::vector<WebContents*> all_inner_contents;
Lucas Furukawa Gadani2ec00c82018-12-14 15:53:165758 const auto& inner_contents = node_.GetInnerWebContents();
5759 all_inner_contents.insert(all_inner_contents.end(), inner_contents.begin(),
5760 inner_contents.end());
5761 return all_inner_contents;
5762}
5763
Jeremy Roman7e70bf92020-01-07 23:23:585764WebContentsImpl* WebContentsImpl::GetResponsibleWebContents() {
5765 // Iteratively ask delegates which other contents is responsible until a fixed
5766 // point is found.
5767 WebContentsImpl* contents = this;
5768 while (WebContentsDelegate* delegate = contents->GetDelegate()) {
5769 auto* responsible_contents = static_cast<WebContentsImpl*>(
5770 delegate->GetResponsibleWebContents(contents));
5771 if (responsible_contents == contents)
5772 break;
5773 contents = responsible_contents;
5774 }
5775 return contents;
5776}
5777
avallee0206f782016-07-28 18:55:335778WebContentsImpl* WebContentsImpl::GetFocusedWebContents() {
lfg601233692017-03-06 22:45:445779 return GetOutermostWebContents()->node_.focused_web_contents();
avallee0206f782016-07-28 18:55:335780}
5781
avallee9993fca2016-11-17 06:16:505782bool WebContentsImpl::ContainsOrIsFocusedWebContents() {
5783 for (WebContentsImpl* focused_contents = GetFocusedWebContents();
5784 focused_contents;
5785 focused_contents = focused_contents->GetOuterWebContents()) {
5786 if (focused_contents == this)
5787 return true;
5788 }
5789
5790 return false;
5791}
5792
W. James MacLean9437b5bf2019-12-06 17:33:445793void WebContentsImpl::RemoveBrowserPluginEmbedder() {
5794 browser_plugin_embedder_.reset();
5795}
5796
avallee0206f782016-07-28 18:55:335797WebContentsImpl* WebContentsImpl::GetOutermostWebContents() {
5798 WebContentsImpl* root = this;
5799 while (root->GetOuterWebContents())
5800 root = root->GetOuterWebContents();
5801 return root;
5802}
5803
avalleed68cdd12017-06-21 22:07:275804void WebContentsImpl::FocusOuterAttachmentFrameChain() {
5805 WebContentsImpl* outer_contents = GetOuterWebContents();
5806 if (!outer_contents)
5807 return;
5808
5809 FrameTreeNode* outer_node =
5810 FrameTreeNode::GloballyFindByID(GetOuterDelegateFrameTreeNodeId());
5811 outer_contents->frame_tree_.SetFocusedFrame(outer_node, nullptr);
5812
5813 // For a browser initiated focus change, let embedding renderer know of the
5814 // change. Otherwise, if the currently focused element is just across a
5815 // process boundary in focus order, it will not be possible to move across
5816 // that boundary. This is because the target element will already be focused
5817 // (that renderer was not notified) and drop the event.
5818 if (GetRenderManager()->GetProxyToOuterDelegate())
5819 GetRenderManager()->GetProxyToOuterDelegate()->SetFocusedFrame();
5820
5821 outer_contents->FocusOuterAttachmentFrameChain();
5822}
5823
Sean Toppingbbc4a2bd2019-01-30 02:16:145824void WebContentsImpl::InnerWebContentsCreated(WebContents* inner_web_contents) {
5825 for (auto& observer : observers_)
5826 observer.InnerWebContentsCreated(inner_web_contents);
5827}
5828
Jeremy Roman03ce13ce2020-05-29 22:16:575829void WebContentsImpl::InnerWebContentsAttached(
5830 WebContents* inner_web_contents) {
5831 if (inner_web_contents->IsCurrentlyAudible())
5832 OnAudioStateChanged();
5833}
5834
5835void WebContentsImpl::InnerWebContentsDetached(
5836 WebContents* inner_web_contents) {
5837 if (!is_being_destroyed_)
5838 OnAudioStateChanged();
5839}
5840
[email protected]b172aee2012-04-10 17:05:265841void WebContentsImpl::RenderViewCreated(RenderViewHost* render_view_host) {
[email protected]86f98a22013-03-20 14:35:005842 if (delegate_)
[email protected]e85165c642014-06-10 14:34:315843 view_->SetOverscrollControllerEnabled(CanOverscrollContent());
[email protected]a6b73c62013-02-11 23:05:085844
[email protected]8ff00d72012-10-23 19:12:215845 NotificationService::current()->Notify(
5846 NOTIFICATION_WEB_CONTENTS_RENDER_VIEW_HOST_CREATED,
5847 Source<WebContents>(this),
5848 Details<RenderViewHost>(render_view_host));
[email protected]420ae012009-04-24 05:16:325849
ericwilligersde386342016-10-19 02:35:095850 for (auto& observer : observers_)
5851 observer.RenderViewCreated(render_view_host);
Joel Einbinder848001c2017-09-19 21:39:315852 if (delegate_)
5853 RenderFrameDevToolsAgentHost::WebContentsCreated(this);
[email protected]420ae012009-04-24 05:16:325854}
5855
[email protected]b172aee2012-04-10 17:05:265856void WebContentsImpl::RenderViewReady(RenderViewHost* rvh) {
[email protected]151a63d2011-12-20 22:32:525857 if (rvh != GetRenderViewHost()) {
[email protected]420ae012009-04-24 05:16:325858 // Don't notify the world, since this came from a renderer in the
5859 // background.
5860 return;
5861 }
5862
dmazzoni62111452017-04-28 23:46:555863 RenderWidgetHostViewBase* rwhv =
5864 static_cast<RenderWidgetHostViewBase*>(GetRenderWidgetHostView());
5865 if (rwhv)
5866 rwhv->SetMainFrameAXTreeID(GetMainFrame()->GetAXTreeID());
5867
[email protected]da7a7182013-09-06 08:11:115868 notify_disconnection_ = true;
[email protected]da7a7182013-09-06 08:11:115869
[email protected]be1f56ab2011-12-22 06:55:315870 bool was_crashed = IsCrashed();
[email protected]443b80e2010-12-14 00:42:235871 SetIsCrashed(base::TERMINATION_STATUS_STILL_RUNNING, 0);
[email protected]9d8a4642009-07-29 17:25:305872
5873 // Restore the focus to the tab (otherwise the focus will be on the top
5874 // window).
[email protected]484ae5912010-09-29 19:16:145875 if (was_crashed && !FocusLocationBarByDefault() &&
5876 (!delegate_ || delegate_->ShouldFocusPageAfterCrash())) {
[email protected]f3615f02013-02-26 06:09:065877 view_->Focus();
[email protected]484ae5912010-09-29 19:16:145878 }
[email protected]32ded2212011-11-10 18:51:435879
ericwilligersde386342016-10-19 02:35:095880 for (auto& observer : observers_)
5881 observer.RenderViewReady();
Jinsuk Kim895b2c52018-07-03 06:49:165882
5883 view_->RenderViewReady();
[email protected]420ae012009-04-24 05:16:325884}
5885
[email protected]ec6a7eb2013-04-22 17:34:225886void WebContentsImpl::RenderViewTerminated(RenderViewHost* rvh,
5887 base::TerminationStatus status,
5888 int error_code) {
[email protected]151a63d2011-12-20 22:32:525889 if (rvh != GetRenderViewHost()) {
[email protected]420ae012009-04-24 05:16:325890 // The pending page's RenderViewHost is gone.
5891 return;
5892 }
5893
[email protected]2fcdcc32014-03-05 02:14:075894 // Ensure fullscreen mode is exited in the |delegate_| since a crashed
5895 // renderer may not have made a clean exit.
alexmos5dd617d2016-06-07 04:21:155896 if (IsFullscreenForCurrentTab())
bokanece34a82016-01-28 19:49:465897 ExitFullscreenMode(false);
[email protected]2fcdcc32014-03-05 02:14:075898
Jennifer Apacibleb4b972a2018-06-07 04:41:025899 // Ensure any video in Picture-in-Picture is exited in the |delegate_| since
5900 // a crashed renderer may not have made a clean exit.
5901 if (HasPictureInPictureVideo())
5902 ExitPictureInPicture();
5903
[email protected]698191dc2014-02-25 01:06:135904 // Cancel any visible dialogs so they are not left dangling over the sad tab.
creis89a0f782015-05-27 16:13:175905 CancelActiveAndPendingDialogs();
[email protected]698191dc2014-02-25 01:06:135906
dalecurtis3d1e1302017-02-20 22:33:255907 audio_stream_monitor_.RenderProcessGone(rvh->GetProcess()->GetID());
5908
[email protected]960b0372014-05-19 18:01:005909 // Reset the loading progress. TODO(avi): What does it mean to have a
5910 // "renderer crash" when there is more than one renderer process serving a
5911 // webpage? Once this function is called at a more granular frame level, we
5912 // probably will need to more granularly reset the state here.
5913 ResetLoadProgressState();
clamy44e84ce2016-02-22 15:38:255914 NotifyDisconnected();
5915 SetIsCrashed(status, error_code);
[email protected]960b0372014-05-19 18:01:005916
ericwilligersde386342016-10-19 02:35:095917 for (auto& observer : observers_)
5918 observer.RenderProcessGone(GetCrashedStatus());
[email protected]420ae012009-04-24 05:16:325919}
5920
[email protected]b172aee2012-04-10 17:05:265921void WebContentsImpl::RenderViewDeleted(RenderViewHost* rvh) {
ericwilligersde386342016-10-19 02:35:095922 for (auto& observer : observers_)
5923 observer.RenderViewDeleted(rvh);
[email protected]2e4633c2009-07-09 16:58:065924}
5925
sky8b00392d2015-01-10 00:30:285926void WebContentsImpl::UpdateTargetURL(RenderViewHost* render_view_host,
5927 const GURL& url) {
5928 if (fullscreen_widget_routing_id_ != MSG_ROUTING_NONE) {
Lukasz Anforowiczfd707142018-02-07 19:46:135929 // If we're in flash fullscreen (i.e. Pepper plugin fullscreen) only update
5930 // the url if it's from the fullscreen renderer.
sky8b00392d2015-01-10 00:30:285931 RenderWidgetHostView* fs = GetFullscreenRenderWidgetHostView();
avif9ab5d942015-10-15 14:05:445932 if (fs && fs->GetRenderWidgetHost() != render_view_host->GetWidget())
sky8b00392d2015-01-10 00:30:285933 return;
5934 }
Lukasz Anforowiczfd707142018-02-07 19:46:135935
5936 // In case of racey updates from multiple RenderViewHosts, the last URL should
5937 // be shown - see also some discussion in https://crbug.com/807776.
5938 if (!url.is_valid() && render_view_host != view_that_set_last_target_url_)
5939 return;
5940 view_that_set_last_target_url_ = url.is_valid() ? render_view_host : nullptr;
5941
[email protected]6934a702011-12-20 00:04:515942 if (delegate_)
avi450ce00e2014-09-24 01:58:525943 delegate_->UpdateTargetURL(this, url);
[email protected]420ae012009-04-24 05:16:325944}
5945
Lukasz Anforowiczfd707142018-02-07 19:46:135946void WebContentsImpl::ClearTargetURL() {
5947 view_that_set_last_target_url_ = nullptr;
5948 if (delegate_)
5949 delegate_->UpdateTargetURL(this, GURL());
5950}
5951
[email protected]b172aee2012-04-10 17:05:265952void WebContentsImpl::Close(RenderViewHost* rvh) {
[email protected]60780f412013-02-25 16:34:105953#if defined(OS_MACOSX)
[email protected]07707302009-11-06 00:50:295954 // The UI may be in an event-tracking loop, such as between the
5955 // mouse-down and mouse-up in text selection or a button click.
5956 // Defer the close until after tracking is complete, so that we
5957 // don't free objects out from under the UI.
[email protected]07707302009-11-06 00:50:295958 // TODO(shess): This could get more fine-grained. For instance,
5959 // closing a tab in another window while selecting text in the
5960 // current window's Omnibox should be just fine.
Christopher Cameron9ae31e0d2019-03-01 20:01:195961 if (view_->CloseTabAfterEventTrackingIfNeeded())
[email protected]07707302009-11-06 00:50:295962 return;
[email protected]60780f412013-02-25 16:34:105963#endif
[email protected]07707302009-11-06 00:50:295964
[email protected]420ae012009-04-24 05:16:325965 // Ignore this if it comes from a RenderViewHost that we aren't showing.
[email protected]151a63d2011-12-20 22:32:525966 if (delegate_ && rvh == GetRenderViewHost())
[email protected]6934a702011-12-20 00:04:515967 delegate_->CloseContents(this);
[email protected]420ae012009-04-24 05:16:325968}
5969
Avi Drissman014dec72018-06-07 02:34:015970void WebContentsImpl::RequestSetBounds(const gfx::Rect& new_bounds) {
5971 if (delegate_)
5972 delegate_->SetContentsBounds(this, new_bounds);
[email protected]420ae012009-04-24 05:16:325973}
5974
fdegansa696e5112015-04-17 01:57:595975void WebContentsImpl::DidStartLoading(FrameTreeNode* frame_tree_node,
[email protected]e3b10d12014-03-28 16:06:095976 bool to_different_document) {
Evan Stadec3b26022017-07-14 19:50:245977 LoadingStateChanged(frame_tree_node->IsMainFrame() && to_different_document,
Carlos IL07f282f2020-05-15 22:58:295978 nullptr);
dmazzonibf8cec42015-02-08 08:28:085979
Xiyuan Xia2a66b112018-12-06 15:52:165980 // Reset the focus state from DidStartNavigation to false if a new load starts
5981 // afterward, in case loading logic triggers a FocusLocationBarByDefault call.
5982 should_focus_location_bar_by_default_ = false;
Ian Prestcdd843d2020-01-28 18:59:585983
5984 // Notify accessibility that the user is navigating away from the
5985 // current document.
5986 // TODO(domfarolino, dmazzoni): Do this using WebContentsObserver. See
5987 // https://crbug.com/981271.
5988 BrowserAccessibilityManager* manager =
5989 frame_tree_node->current_frame_host()->browser_accessibility_manager();
5990 if (manager)
5991 manager->UserIsNavigatingAway();
[email protected]420ae012009-04-24 05:16:325992}
5993
fdegansc73eec22015-03-19 14:47:545994void WebContentsImpl::DidStopLoading() {
dcheng59716272016-04-09 05:19:085995 std::unique_ptr<LoadNotificationDetails> details;
[email protected]420ae012009-04-24 05:16:325996
[email protected]9595fd82013-04-19 21:28:495997 // Use the last committed entry rather than the active one, in case a
5998 // pending entry has been created.
5999 NavigationEntry* entry = controller_.GetLastCommittedEntry();
6000
[email protected]420ae012009-04-24 05:16:326001 // An entry may not exist for a stop when loading an initial blank page or
6002 // if an iframe injected by script into a blank page finishes loading.
6003 if (entry) {
[email protected]420ae012009-04-24 05:16:326004 details.reset(new LoadNotificationDetails(
[email protected]36fc0392011-12-25 03:59:516005 entry->GetVirtualURL(),
[email protected]420ae012009-04-24 05:16:326006 &controller_,
6007 controller_.GetCurrentEntryIndex()));
6008 }
6009
Carlos IL07f282f2020-05-15 22:58:296010 LoadingStateChanged(true, details.get());
[email protected]420ae012009-04-24 05:16:326011}
6012
fdegansa696e5112015-04-17 01:57:596013void WebContentsImpl::DidChangeLoadProgress() {
Nate Chapin22ea6592019-03-05 22:29:026014 if (is_being_destroyed_)
6015 return;
fdegansa696e5112015-04-17 01:57:596016 double load_progress = frame_tree_.load_progress();
6017
6018 // The delegate is notified immediately for the first and last updates. Also,
6019 // since the message loop may be pretty busy when a page is loaded, it might
6020 // not execute a posted task in a timely manner so the progress report is sent
6021 // immediately if enough time has passed.
6022 base::TimeDelta min_delay =
6023 base::TimeDelta::FromMilliseconds(kMinimumDelayBetweenLoadingUpdatesMS);
6024 bool delay_elapsed = loading_last_progress_update_.is_null() ||
6025 base::TimeTicks::Now() - loading_last_progress_update_ > min_delay;
6026
6027 if (load_progress == 0.0 || load_progress == 1.0 || delay_elapsed) {
6028 // If there is a pending task to send progress, it is now obsolete.
6029 loading_weak_factory_.InvalidateWeakPtrs();
6030
6031 // Notify the load progress change.
6032 SendChangeLoadProgress();
6033
6034 // Clean-up the states if needed.
6035 if (load_progress == 1.0)
6036 ResetLoadProgressState();
6037 return;
6038 }
6039
6040 if (loading_weak_factory_.HasWeakPtrs())
6041 return;
6042
skyostil95082a62015-06-05 19:53:076043 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
tzike2aca992017-09-05 08:50:546044 FROM_HERE,
6045 base::BindOnce(&WebContentsImpl::SendChangeLoadProgress,
6046 loading_weak_factory_.GetWeakPtr()),
fdegansa696e5112015-04-17 01:57:596047 min_delay);
6048}
6049
avid6d88b912017-01-13 00:16:006050std::vector<std::unique_ptr<NavigationThrottle>>
6051WebContentsImpl::CreateThrottlesForNavigation(
clamycbf524d42016-09-27 12:48:356052 NavigationHandle* navigation_handle) {
Charlie Reis96077362019-07-04 00:34:046053 auto throttles = GetContentClient()->browser()->CreateThrottlesForNavigation(
clamycbf524d42016-09-27 12:48:356054 navigation_handle);
Charlie Reis96077362019-07-04 00:34:046055
6056 // This is not a normal place to be adding a throttle. However, in the case
6057 // javascript dialogs, related logic is present in the web_contents/ layer,
6058 // and the purpose of the throttle is to ensure that navigation commits are
6059 // deferred for the entire WebContents. Most throttles are either added by
6060 // the embederrer outside of content/, or are per-frame and added by
6061 // NavigationThrottleRunner.
6062 std::unique_ptr<content::NavigationThrottle> dialog_throttle =
6063 JavaScriptDialogNavigationThrottle::MaybeCreateThrottleFor(
6064 navigation_handle);
6065 if (dialog_throttle)
6066 throttles.push_back(std::move(dialog_throttle));
6067
6068 return throttles;
clamycbf524d42016-09-27 12:48:356069}
6070
clamy1e5574e2016-09-29 16:48:446071std::unique_ptr<NavigationUIData> WebContentsImpl::GetNavigationUIData(
6072 NavigationHandle* navigation_handle) {
clamy1e5574e2016-09-29 16:48:446073 return GetContentClient()->browser()->GetNavigationUIData(navigation_handle);
6074}
6075
W. James MacLean1c40862c2020-04-27 21:05:576076void WebContentsImpl::RegisterExistingOriginToPreventOptInIsolation(
W. James MacLeanb70fab82020-05-01 18:51:146077 const url::Origin& origin,
6078 NavigationRequest* navigation_request_to_exclude) {
W. James MacLean1c40862c2020-04-27 21:05:576079 // Note: This function can be made static if we ever need call it without
6080 // a WebContentsImpl instance, in which case we can use a wrapper to
6081 // implement the override from NavigatorDelegate.
6082 for (WebContentsImpl* web_contents : GetAllWebContents()) {
6083 web_contents->controller_.RegisterExistingOriginToPreventOptInIsolation(
6084 origin);
W. James MacLeanb70fab82020-05-01 18:51:146085 // Walk the frame tree to pick up any frames without FrameNavigationEntries.
6086 // * Some frames won't have FrameNavigationEntries (Issues 524208, 608402).
6087 // * Some pending navigations won't have NavigationEntries.
6088 web_contents->GetFrameTree()->RegisterExistingOriginToPreventOptInIsolation(
6089 origin, navigation_request_to_exclude);
W. James MacLean1c40862c2020-04-27 21:05:576090 }
6091}
6092
pdr15f5b5b2017-06-20 00:58:006093void WebContentsImpl::DidCancelLoading() {
6094 controller_.DiscardNonCommittedEntries();
6095
6096 // Update the URL display.
6097 NotifyNavigationStateChanged(INVALIDATE_TYPE_URL);
6098}
6099
[email protected]59167c22013-06-03 18:07:326100void WebContentsImpl::DidAccessInitialDocument() {
[email protected]b4c84012014-04-28 19:51:106101 has_accessed_initial_document_ = true;
6102
[email protected]d1371bf8e2014-04-19 03:26:246103 // We may have left a failed browser-initiated navigation in the address bar
6104 // to let the user edit it and try again. Clear it now that content might
6105 // show up underneath it.
6106 if (!IsLoading() && controller_.GetPendingEntry())
avi45a72532015-04-07 21:01:456107 controller_.DiscardPendingEntry(false);
[email protected]d1371bf8e2014-04-19 03:26:246108
[email protected]59167c22013-06-03 18:07:326109 // Update the URL display.
thestigc4cac8f2014-09-04 21:17:506110 NotifyNavigationStateChanged(INVALIDATE_TYPE_URL);
[email protected]59167c22013-06-03 18:07:326111}
6112
alexmosbe2f4c32015-03-10 02:30:236113void WebContentsImpl::DidChangeName(RenderFrameHost* render_frame_host,
6114 const std::string& name) {
ericwilligersde386342016-10-19 02:35:096115 for (auto& observer : observers_)
6116 observer.FrameNameChanged(render_frame_host, name);
alexmosbe2f4c32015-03-10 02:30:236117}
6118
John Delaneyedd8d6c2019-01-25 00:23:576119void WebContentsImpl::DidReceiveFirstUserActivation(
6120 RenderFrameHost* render_frame_host) {
6121 for (auto& observer : observers_)
6122 observer.FrameReceivedFirstUserActivation(render_frame_host);
6123}
6124
John Delaney0146a742019-01-25 19:25:286125void WebContentsImpl::DidChangeDisplayState(RenderFrameHost* render_frame_host,
6126 bool is_display_none) {
6127 for (auto& observer : observers_)
6128 observer.FrameDisplayStateChanged(render_frame_host, is_display_none);
6129}
6130
6131void WebContentsImpl::FrameSizeChanged(RenderFrameHost* render_frame_host,
6132 const gfx::Size& frame_size) {
6133 for (auto& observer : observers_)
6134 observer.FrameSizeChanged(render_frame_host, frame_size);
6135}
6136
[email protected]39f2f9252014-05-03 00:54:016137void WebContentsImpl::DocumentOnLoadCompleted(
[email protected]50279492014-05-05 23:24:296138 RenderFrameHost* render_frame_host) {
falken52a56e32016-12-08 05:02:406139 ShowInsecureLocalhostWarningIfNeeded();
6140
Alexander Timindd0fa0c52019-11-06 13:03:376141 GetRenderViewHost()->DocumentOnLoadCompletedInMainFrame();
6142
creis97867aa2017-02-17 18:39:456143 is_notifying_observers_ = true;
ericwilligersde386342016-10-19 02:35:096144 for (auto& observer : observers_)
6145 observer.DocumentOnLoadCompletedInMainFrame();
creis97867aa2017-02-17 18:39:456146 is_notifying_observers_ = false;
[email protected]6578fef2013-10-14 02:35:096147
6148 // TODO(avi): Remove. http://crbug.com/170921
[email protected]8ff00d72012-10-23 19:12:216149 NotificationService::current()->Notify(
6150 NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
6151 Source<WebContents>(this),
[email protected]50279492014-05-05 23:24:296152 NotificationService::NoDetails());
[email protected]25497492010-09-11 15:15:086153}
6154
creis8eb8ef22015-11-04 22:51:386155void WebContentsImpl::UpdateStateForFrame(RenderFrameHost* render_frame_host,
6156 const PageState& page_state) {
creis8eb8ef22015-11-04 22:51:386157 // The state update affects the last NavigationEntry associated with the given
6158 // |render_frame_host|. This may not be the last committed NavigationEntry (as
6159 // in the case of an UpdateState from a frame being swapped out). We track
6160 // which entry this is in the RenderFrameHost's nav_entry_id.
6161 RenderFrameHostImpl* rfhi =
6162 static_cast<RenderFrameHostImpl*>(render_frame_host);
6163 NavigationEntryImpl* entry =
6164 controller_.GetEntryWithUniqueID(rfhi->nav_entry_id());
6165 if (!entry)
6166 return;
6167
6168 FrameNavigationEntry* frame_entry =
6169 entry->GetFrameEntry(rfhi->frame_tree_node());
6170 if (!frame_entry)
6171 return;
6172
creisd06a9422015-11-11 03:08:456173 // The SiteInstance might not match if we do a cross-process navigation with
6174 // replacement (e.g., auto-subframe), in which case the swap out of the old
6175 // RenderFrameHost runs in the background after the old FrameNavigationEntry
6176 // has already been replaced and destroyed.
6177 if (frame_entry->site_instance() != rfhi->GetSiteInstance())
6178 return;
6179
creis8eb8ef22015-11-04 22:51:386180 if (page_state == frame_entry->page_state())
6181 return; // Nothing to update.
6182
creis0cade2e2017-02-28 06:37:476183 DCHECK(page_state.IsValid()) << "Shouldn't set an empty PageState.";
nasko34634672016-07-29 18:46:066184
6185 // The document_sequence_number and item_sequence_number recorded in the
6186 // FrameNavigationEntry should not differ from the one coming with the update,
6187 // since it must come from the same document. Do not update it if a difference
6188 // is detected, as this indicates that |frame_entry| is not the correct one.
6189 ExplodedPageState exploded_state;
6190 if (!DecodePageState(page_state.ToEncodedData(), &exploded_state))
6191 return;
6192
6193 if (exploded_state.top.document_sequence_number !=
6194 frame_entry->document_sequence_number() ||
6195 exploded_state.top.item_sequence_number !=
6196 frame_entry->item_sequence_number()) {
6197 return;
6198 }
6199
nasko04a322f25a2016-09-01 00:00:346200 frame_entry->SetPageState(page_state);
creis8eb8ef22015-11-04 22:51:386201 controller_.NotifyEntryChanged(entry);
6202}
6203
[email protected]ef3adfc2014-05-11 00:04:546204void WebContentsImpl::UpdateTitle(RenderFrameHost* render_frame_host,
[email protected]ef3adfc2014-05-11 00:04:546205 const base::string16& title,
6206 base::i18n::TextDirection title_direction) {
[email protected]ef3adfc2014-05-11 00:04:546207 // Try to find the navigation entry, which might not be the current one.
avi286019ce2015-07-29 01:26:586208 // For example, it might be from a recently swapped out RFH.
avi1edc8552016-10-13 21:37:166209 NavigationEntryImpl* entry = controller_.GetEntryWithUniqueID(
creisad8c31a2016-05-24 17:07:046210 static_cast<RenderFrameHostImpl*>(render_frame_host)->nav_entry_id());
[email protected]ef3adfc2014-05-11 00:04:546211
6212 // We can handle title updates when we don't have an entry in
6213 // UpdateTitleForEntry, but only if the update is from the current RVH.
6214 // TODO(avi): Change to make decisions based on the RenderFrameHost.
avi286019ce2015-07-29 01:26:586215 if (!entry && render_frame_host != GetMainFrame())
[email protected]ef3adfc2014-05-11 00:04:546216 return;
6217
6218 // TODO(evan): make use of title_direction.
6219 // http://code.google.com/p/chromium/issues/detail?id=27094
afakhry6f0c1ec22016-07-14 13:55:136220 UpdateTitleForEntry(entry, title);
[email protected]ef3adfc2014-05-11 00:04:546221}
6222
[email protected]e1c3a552012-05-04 20:51:326223void WebContentsImpl::RouteCloseEvent(RenderViewHost* rvh) {
6224 // Tell the active RenderViewHost to run unload handlers and close, as long
6225 // as the request came from a RenderViewHost in the same BrowsingInstance.
6226 // In most cases, we receive this from a swapped out RenderViewHost.
6227 // It is possible to receive it from one that has just been swapped in,
6228 // in which case we might as well deliver the message anyway.
6229 if (rvh->GetSiteInstance()->IsRelatedSiteInstance(GetSiteInstance()))
naskoc0fceff2015-04-30 15:53:526230 ClosePage();
[email protected]e1c3a552012-05-04 20:51:326231}
6232
alexmose7da5a12015-04-09 02:22:166233bool WebContentsImpl::ShouldRouteMessageEvent(
6234 RenderFrameHost* target_rfh,
6235 SiteInstance* source_site_instance) const {
6236 // Allow the message if this WebContents is dedicated to a browser plugin
6237 // guest.
[email protected]0eba810b2012-10-18 03:19:366238 // Note: This check means that an embedder could theoretically receive a
6239 // postMessage from anyone (not just its own guests). However, this is
6240 // probably not a risk for apps since other pages won't have references
6241 // to App windows.
alexmose7da5a12015-04-09 02:22:166242 return GetBrowserPluginGuest() || GetBrowserPluginEmbedder();
6243}
[email protected]f546640b2012-05-15 00:03:496244
alexmos58729042015-06-18 23:20:006245void WebContentsImpl::EnsureOpenerProxiesExist(RenderFrameHost* source_rfh) {
alexmose7da5a12015-04-09 02:22:166246 WebContentsImpl* source_web_contents = static_cast<WebContentsImpl*>(
6247 WebContents::FromRenderFrameHost(source_rfh));
[email protected]f546640b2012-05-15 00:03:496248
alexmose7da5a12015-04-09 02:22:166249 if (source_web_contents) {
lazyboy6ec48b2a2015-06-29 15:18:146250 // If this message is going to outer WebContents from inner WebContents,
6251 // then we should not create a RenderView. AttachToOuterWebContentsFrame()
6252 // already created a RenderFrameProxyHost for that purpose.
6253 if (GetBrowserPluginEmbedder() &&
W. James MacLean2539adb32019-12-13 00:40:446254 source_web_contents->browser_plugin_guest_) {
lazyboy6ec48b2a2015-06-29 15:18:146255 return;
W. James MacLean2539adb32019-12-13 00:40:446256 }
lazyboy6ec48b2a2015-06-29 15:18:146257
lfg812874d2016-02-09 22:26:136258 if (this != source_web_contents && GetBrowserPluginGuest()) {
naskob3041b98a42016-03-12 04:43:066259 // We create a RenderFrameProxyHost for the embedder in the guest's render
6260 // process but we intentionally do not expose the embedder's opener chain
6261 // to it.
6262 source_web_contents->GetRenderManager()->CreateRenderFrameProxy(
6263 GetSiteInstance());
[email protected]f546640b2012-05-15 00:03:496264 } else {
alexmos58729042015-06-18 23:20:006265 RenderFrameHostImpl* source_rfhi =
6266 static_cast<RenderFrameHostImpl*>(source_rfh);
6267 source_rfhi->frame_tree_node()->render_manager()->CreateOpenerProxies(
alexmos90325cf2015-09-02 17:18:396268 GetSiteInstance(), nullptr);
[email protected]f546640b2012-05-15 00:03:496269 }
6270 }
[email protected]f546640b2012-05-15 00:03:496271}
6272
avallee9993fca2016-11-17 06:16:506273void WebContentsImpl::SetAsFocusedWebContentsIfNecessary() {
Adithya Srinivasan11af2c52019-12-16 22:52:556274 DCHECK(!portal());
avallee9993fca2016-11-17 06:16:506275 // Only change focus if we are not currently focused.
6276 WebContentsImpl* old_contents = GetFocusedWebContents();
6277 if (old_contents == this)
6278 return;
6279
paulmeyerfeafc2d2017-04-25 21:46:406280 GetOutermostWebContents()->node_.SetFocusedWebContents(this);
6281
avallee9993fca2016-11-17 06:16:506282 // Send a page level blur to the old contents so that it displays inactive UI
6283 // and focus this contents to activate it.
6284 if (old_contents)
6285 old_contents->GetMainFrame()->GetRenderWidgetHost()->SetPageFocus(false);
avalleec2ff1ef2017-01-21 00:12:306286
6287 // Make sure the outer web contents knows our frame is focused. Otherwise, the
6288 // outer renderer could have the element before or after the frame element
6289 // focused which would return early without actually advancing focus.
avalleed68cdd12017-06-21 22:07:276290 FocusOuterAttachmentFrameChain();
avalleec2ff1ef2017-01-21 00:12:306291
Carlos ILd51e7702020-05-07 18:51:396292 GetMainFrame()->GetRenderWidgetHost()->SetPageFocus(true);
avallee9993fca2016-11-17 06:16:506293}
6294
avallee0206f782016-07-28 18:55:336295void WebContentsImpl::SetFocusedFrame(FrameTreeNode* node,
6296 SiteInstance* source) {
Adithya Srinivasan11af2c52019-12-16 22:52:556297 DCHECK(!portal());
avallee0206f782016-07-28 18:55:336298 frame_tree_.SetFocusedFrame(node, source);
avalleed68cdd12017-06-21 22:07:276299
Lucas Furukawa Gadaniabbfa342017-08-29 19:37:576300 if (auto* inner_contents = node_.GetInnerWebContentsInFrame(node)) {
6301 // |this| is an outer WebContents and |node| represents an inner
6302 // WebContents. Transfer the focus to the inner contents if |this| is
6303 // focused.
6304 if (GetFocusedWebContents() == this)
6305 inner_contents->SetAsFocusedWebContentsIfNecessary();
6306 } else if (node_.OuterContentsFrameTreeNode() &&
6307 node_.OuterContentsFrameTreeNode()
6308 ->current_frame_host()
6309 ->GetSiteInstance() == source) {
6310 // |this| is an inner WebContents, |node| is its main FrameTreeNode and
6311 // the outer WebContents FrameTreeNode is at |source|'s SiteInstance.
6312 // Transfer the focus to the inner WebContents if the outer WebContents is
6313 // focused. This branch is used when an inner WebContents is focused through
Lowell Mannersa9530382019-08-01 09:46:576314 // its RenderFrameProxyHost (via FrameFocused mojo call, used to
Lucas Furukawa Gadaniabbfa342017-08-29 19:37:576315 // implement the window.focus() API).
6316 if (GetFocusedWebContents() == GetOuterWebContents())
6317 SetAsFocusedWebContentsIfNecessary();
6318 } else if (!GetOuterWebContents()) {
6319 // This is an outermost WebContents.
6320 SetAsFocusedWebContentsIfNecessary();
Lucas Furukawa Gadaniabbfa342017-08-29 19:37:576321 }
avalleed68cdd12017-06-21 22:07:276322}
6323
Avi Drissman36f801f2018-02-01 20:06:046324void WebContentsImpl::DidCallFocus() {
6325 // Any explicit focusing of another window while this WebContents is in
6326 // fullscreen can be used to confuse the user, so drop fullscreen.
Avi Drissman1a55a9d62020-03-10 18:56:456327 base::ScopedClosureRunner fullscreen_block = ForSecurityDropFullscreen();
6328 // The other contents is independent of this contents, so release the
6329 // fullscreen block.
6330 fullscreen_block.RunAndReset();
Avi Drissman36f801f2018-02-01 20:06:046331}
6332
avalleed68cdd12017-06-21 22:07:276333RenderFrameHost* WebContentsImpl::GetFocusedFrameIncludingInnerWebContents() {
6334 WebContentsImpl* contents = this;
6335 FrameTreeNode* focused_node = contents->frame_tree_.GetFocusedFrame();
6336
6337 // If there is no focused frame in the outer WebContents, we need to return
6338 // null.
6339 if (!focused_node)
6340 return nullptr;
6341
6342 // If the focused frame is embedding an inner WebContents, we must descend
6343 // into that contents. If the current WebContents does not have a focused
6344 // frame, return the main frame of this contents instead of the focused empty
6345 // frame embedding this contents.
6346 while (true) {
6347 contents = contents->node_.GetInnerWebContentsInFrame(focused_node);
6348 if (!contents)
6349 return focused_node->current_frame_host();
6350
6351 focused_node = contents->frame_tree_.GetFocusedFrame();
6352 if (!focused_node)
6353 return contents->GetMainFrame();
6354 }
avallee0206f782016-07-28 18:55:336355}
6356
Lucas Furukawa Gadaniabbfa342017-08-29 19:37:576357void WebContentsImpl::OnAdvanceFocus(RenderFrameHostImpl* source_rfh) {
6358 // When a RenderFrame needs to advance focus to a RenderFrameProxy (by hitting
6359 // TAB), the RenderFrameProxy sends an IPC to RenderFrameProxyHost. When this
6360 // RenderFrameProxyHost represents an inner WebContents, the outer WebContents
6361 // needs to focus the inner WebContents.
6362 if (GetOuterWebContents() &&
6363 GetOuterWebContents() == source_rfh->delegate()->GetAsWebContents() &&
6364 GetFocusedWebContents() == GetOuterWebContents()) {
6365 SetAsFocusedWebContentsIfNecessary();
6366 }
6367}
6368
ekaramada110f642016-12-21 19:47:286369void WebContentsImpl::OnFocusedElementChangedInFrame(
6370 RenderFrameHostImpl* frame,
6371 const gfx::Rect& bounds_in_root_view) {
6372 RenderWidgetHostViewBase* root_view =
6373 static_cast<RenderWidgetHostViewBase*>(GetRenderWidgetHostView());
6374 if (!root_view || !frame->GetView())
6375 return;
6376
6377 // Converting to screen coordinates.
6378 gfx::Point origin = bounds_in_root_view.origin();
6379 origin += root_view->GetViewBounds().OffsetFromOrigin();
6380 gfx::Rect bounds_in_screen(origin, bounds_in_root_view.size());
6381
6382 root_view->FocusedNodeChanged(frame->has_focused_editable_element(),
6383 bounds_in_screen);
6384
6385 FocusedNodeDetails details = {frame->has_focused_editable_element(),
6386 bounds_in_screen};
6387
6388 // TODO(ekaramad): We should replace this with an observer notification
6389 // (https://crbug.com/675975).
6390 NotificationService::current()->Notify(
6391 NOTIFICATION_FOCUS_CHANGED_IN_PAGE,
6392 Source<RenderViewHost>(GetRenderViewHost()),
6393 Details<FocusedNodeDetails>(&details));
Jeff Fisher709b4c52019-07-29 20:07:346394
6395 for (auto& observer : observers_)
6396 observer.OnFocusChangedInPage(&details);
ekaramada110f642016-12-21 19:47:286397}
6398
Lowell Manners1de5242e2019-04-25 10:18:466399bool WebContentsImpl::DidAddMessageToConsole(
6400 blink::mojom::ConsoleMessageLevel log_level,
6401 const base::string16& message,
6402 int32_t line_no,
6403 const base::string16& source_id) {
Devlin Cronin5ea668f32019-12-18 21:57:586404 for (auto& observer : observers_)
6405 observer.OnDidAddMessageToConsole(log_level, message, line_no, source_id);
6406
[email protected]a796f202012-05-30 14:14:256407 if (!delegate_)
6408 return false;
Lowell Manners1de5242e2019-04-25 10:18:466409 return delegate_->DidAddMessageToConsole(this, log_level, message, line_no,
avia90ae4e2016-11-11 20:49:336410 source_id);
[email protected]a796f202012-05-30 14:14:256411}
6412
Daniel Cheng90196c82018-04-25 21:49:146413void WebContentsImpl::DidReceiveInputEvent(
dominickn9c7ceaa32016-02-27 01:52:536414 RenderWidgetHostImpl* render_widget_host,
6415 const blink::WebInputEvent::Type type) {
Dominick Ng37a15052018-07-17 23:56:356416 if (!IsUserInteractionInputType(type))
Daniel Cheng90196c82018-04-25 21:49:146417 return;
6418
kenrb19221852016-04-29 17:21:406419 // Ignore unless the widget is currently in the frame tree.
6420 if (!HasMatchingWidgetHost(&frame_tree_, render_widget_host))
avic3aa8422015-11-09 20:57:226421 return;
6422
Dave Tapuska347d60a2020-04-21 23:55:476423 if (type != blink::WebInputEvent::Type::kGestureScrollBegin)
Daniel Cheng90196c82018-04-25 21:49:146424 last_interactive_input_event_time_ = ui::EventTimeForNow();
dominickn9c7ceaa32016-02-27 01:52:536425
Dominick Ng37a15052018-07-17 23:56:356426 for (auto& observer : observers_)
6427 observer.DidGetUserInteraction(type);
dominickn2dd142dd2015-10-29 05:30:506428}
6429
Avi Drissman738ea192018-08-29 20:24:166430bool WebContentsImpl::ShouldIgnoreInputEvents() {
6431 WebContentsImpl* web_contents = this;
6432 while (web_contents) {
6433 if (web_contents->ignore_input_events_)
6434 return true;
6435 web_contents = web_contents->GetOuterWebContents();
6436 }
6437
6438 return false;
6439}
6440
avallee9993fca2016-11-17 06:16:506441void WebContentsImpl::FocusOwningWebContents(
6442 RenderWidgetHostImpl* render_widget_host) {
Alex Moshchukc4f0a722019-11-19 22:41:286443 RenderWidgetHostImpl* main_frame_widget_host =
6444 GetMainFrame()->GetRenderWidgetHost();
avallee9993fca2016-11-17 06:16:506445 RenderWidgetHostImpl* focused_widget =
Alex Moshchukc4f0a722019-11-19 22:41:286446 GetFocusedRenderWidgetHost(main_frame_widget_host);
avallee9993fca2016-11-17 06:16:506447
6448 if (focused_widget != render_widget_host &&
avallee8e881592016-11-19 00:39:306449 (!focused_widget ||
6450 focused_widget->delegate() != render_widget_host->delegate())) {
avallee9993fca2016-11-17 06:16:506451 SetAsFocusedWebContentsIfNecessary();
6452 }
6453}
6454
[email protected]b172aee2012-04-10 17:05:266455void WebContentsImpl::OnIgnoredUIEvent() {
[email protected]31145092011-09-30 01:49:446456 // Notify observers.
ericwilligersde386342016-10-19 02:35:096457 for (auto& observer : observers_)
6458 observer.DidGetIgnoredUIEvent();
[email protected]fa1cf0b82010-01-15 21:49:446459}
6460
avic3aa8422015-11-09 20:57:226461void WebContentsImpl::RendererUnresponsive(
Lukasz Anforowicz52b93722018-06-20 16:11:396462 RenderWidgetHostImpl* render_widget_host,
6463 base::RepeatingClosure hang_monitor_restarter) {
avic031d392017-03-03 03:09:426464 if (ShouldIgnoreUnresponsiveRenderer())
[email protected]e5fc1632011-08-08 07:51:536465 return;
6466
Lukasz Anforowicz4f0593d2018-09-21 18:33:546467 // Do not report hangs (to task manager, to hang renderer dialog, etc.) for
6468 // invisible tabs (like extension background page, background tabs). See
6469 // https://crbug.com/881812 for rationale and for choosing the visibility
6470 // (rather than process priority) as the signal here.
6471 if (GetVisibility() != Visibility::VISIBLE)
6472 return;
6473
Avi Drissman8920def2018-01-31 19:53:366474 if (!render_widget_host->renderer_initialized())
[email protected]55452902011-06-01 21:57:476475 return;
6476
Lukasz Anforowicz4f0593d2018-09-21 18:33:546477 for (auto& observer : observers_)
6478 observer.OnRendererUnresponsive(render_widget_host->GetProcess());
6479
Dave Tapuskac6146c52017-12-20 22:48:156480 if (delegate_)
Lukasz Anforowicz52b93722018-06-20 16:11:396481 delegate_->RendererUnresponsive(this, render_widget_host,
6482 std::move(hang_monitor_restarter));
[email protected]420ae012009-04-24 05:16:326483}
6484
avic3aa8422015-11-09 20:57:226485void WebContentsImpl::RendererResponsive(
6486 RenderWidgetHostImpl* render_widget_host) {
Scott Violet48202e62020-05-26 17:19:336487 for (auto& observer : observers_)
6488 observer.OnRendererResponsive(render_widget_host->GetProcess());
6489
[email protected]6934a702011-12-20 00:04:516490 if (delegate_)
Avi Drissman1572e8c3c2018-02-02 19:06:366491 delegate_->RendererResponsive(this, render_widget_host);
[email protected]420ae012009-04-24 05:16:326492}
6493
Alex Moshchuk7b4f0652019-05-30 18:54:416494void WebContentsImpl::SubframeCrashed(
6495 blink::mojom::FrameVisibility visibility) {
6496 // If a subframe crashed on a hidden tab, mark the tab for reload to avoid
6497 // showing a sad frame to the user if they ever switch back to that tab. Do
6498 // this for subframes that are either visible in viewport or visible but
6499 // scrolled out of view, but skip subframes that are not rendered (e.g., via
6500 // "display:none"), since in that case the user wouldn't see a sad frame
6501 // anyway.
6502 bool did_mark_for_reload = false;
6503 if (IsHidden() && visibility != blink::mojom::FrameVisibility::kNotRendered &&
6504 base::FeatureList::IsEnabled(
6505 features::kReloadHiddenTabsWithCrashedSubframes)) {
6506 controller_.SetNeedsReload(
6507 NavigationControllerImpl::NeedsReloadType::kCrashedSubframe);
6508 did_mark_for_reload = true;
6509 UMA_HISTOGRAM_ENUMERATION(
6510 "Stability.ChildFrameCrash.TabMarkedForReload.Visibility", visibility);
6511 }
6512
6513 UMA_HISTOGRAM_BOOLEAN("Stability.ChildFrameCrash.TabMarkedForReload",
6514 did_mark_for_reload);
6515}
6516
[email protected]b172aee2012-04-10 17:05:266517void WebContentsImpl::BeforeUnloadFiredFromRenderManager(
[email protected]feaded02012-09-25 19:12:556518 bool proceed, const base::TimeTicks& proceed_time,
[email protected]420ae012009-04-24 05:16:326519 bool* proceed_to_fire_unload) {
ericwilligersde386342016-10-19 02:35:096520 for (auto& observer : observers_)
Chris Hamilton3b3e315d2018-09-19 13:16:216521 observer.BeforeUnloadFired(proceed, proceed_time);
[email protected]6934a702011-12-20 00:04:516522 if (delegate_)
6523 delegate_->BeforeUnloadFired(this, proceed, proceed_to_fire_unload);
[email protected]e80af492013-06-24 21:52:096524 // Note: |this| might be deleted at this point.
[email protected]420ae012009-04-24 05:16:326525}
6526
[email protected]58d5cfe2013-07-10 02:40:526527void WebContentsImpl::RenderProcessGoneFromRenderManager(
[email protected]3a3d47472010-07-15 21:03:546528 RenderViewHost* render_view_host) {
[email protected]443b80e2010-12-14 00:42:236529 DCHECK(crashed_status_ != base::TERMINATION_STATUS_STILL_RUNNING);
[email protected]ec6a7eb2013-04-22 17:34:226530 RenderViewTerminated(render_view_host, crashed_status_, crashed_error_code_);
[email protected]3a3d47472010-07-15 21:03:546531}
6532
[email protected]af905902013-10-01 21:38:516533void WebContentsImpl::CancelModalDialogsForRenderManager() {
6534 // We need to cancel modal dialogs when doing a process swap, since the load
avi2460c762015-04-17 15:21:546535 // deferrer would prevent us from swapping out. We also clear the state
6536 // because this is a cross-process navigation, which means that it's a new
6537 // site that should not have to pay for the sins of its predecessor.
creis89a0f782015-05-27 16:13:176538 //
Lei Zhang6410ec1a2019-01-31 21:38:586539 // Note that we don't bother telling |browser_plugin_embedder_| because the
creis89a0f782015-05-27 16:13:176540 // cross-process navigation will either destroy the browser plugins or not
6541 // require their dialogs to close.
avi5d3b8692016-10-12 22:00:466542 if (dialog_manager_) {
avi6879fcf2017-01-21 05:27:536543 dialog_manager_->CancelDialogs(this, /*reset_state=*/true);
avi5d3b8692016-10-12 22:00:466544 }
[email protected]af905902013-10-01 21:38:516545}
6546
danakja0bce4c952020-05-12 20:40:286547void WebContentsImpl::NotifySwappedFromRenderManager(RenderFrameHost* old_frame,
6548 RenderFrameHost* new_frame,
[email protected]02d7b6e2014-06-24 21:01:506549 bool is_main_frame) {
6550 if (is_main_frame) {
danakja0bce4c952020-05-12 20:40:286551 // The |new_frame| and its various compadres are already swapped into place
6552 // for the WebContentsImpl when this method is called.
6553 DCHECK(GetMainFrame() == new_frame);
6554 DCHECK(GetRenderViewHost() == new_frame->GetRenderViewHost());
6555 DCHECK(GetRenderWidgetHostView() == new_frame->GetView());
6556
Fergal Daly6767b2e2020-02-04 03:15:166557 RenderViewHost* old_rvh =
danakja0bce4c952020-05-12 20:40:286558 old_frame ? old_frame->GetRenderViewHost() : nullptr;
6559 RenderViewHost* new_rvh = new_frame->GetRenderViewHost();
Fergal Daly6767b2e2020-02-04 03:15:166560 // |old_rvh| and |new_rvh| might be equal when navigating from a crashed
6561 // RenderFrameHost to a new same-site one. With RenderDocument, this will
6562 // happen for every same-site navigation.
6563 if (old_rvh != new_rvh)
6564 NotifyViewSwapped(old_rvh, new_rvh);
[email protected]a6b73c62013-02-11 23:05:086565
[email protected]02d7b6e2014-06-24 21:01:506566 // Make sure the visible RVH reflects the new delegate's preferences.
6567 if (delegate_)
6568 view_->SetOverscrollControllerEnabled(CanOverscrollContent());
[email protected]d56a49e2013-02-23 00:47:396569
danakja0bce4c952020-05-12 20:40:286570 auto* rwhv = static_cast<RenderWidgetHostViewBase*>(new_frame->GetView());
6571 if (rwhv) {
6572 rwhv->SetMainFrameAXTreeID(new_frame->GetAXTreeID());
6573
6574 // The RenderWidgetHostView for the speculative RenderFrameHost is not
6575 // resized with the current RenderFrameHost while a navigation is
6576 // pending. So when we swap in the main frame, we need to update the
6577 // RenderWidgetHostView's size.
6578 //
6579 // Historically, this was done to fix b/1079768 for interstitials.
6580 rwhv->SetSize(GetSizeForMainFrame());
6581 }
[email protected]02d7b6e2014-06-24 21:01:506582 }
6583
danakja0bce4c952020-05-12 20:40:286584 NotifyFrameSwapped(old_frame, new_frame, is_main_frame);
[email protected]3a3d47472010-07-15 21:03:546585}
6586
naskof5940b9f2015-03-02 23:04:056587void WebContentsImpl::NotifyMainFrameSwappedFromRenderManager(
danakja0bce4c952020-05-12 20:40:286588 RenderFrameHost* old_frame,
6589 RenderFrameHost* new_frame) {
6590 NotifyViewSwapped(old_frame ? old_frame->GetRenderViewHost() : nullptr,
6591 new_frame->GetRenderViewHost());
naskof5940b9f2015-03-02 23:04:056592}
6593
[email protected]b172aee2012-04-10 17:05:266594NavigationControllerImpl& WebContentsImpl::GetControllerForRenderManager() {
[email protected]d1198fd2012-08-13 22:50:196595 return GetController();
[email protected]3a3d47472010-07-15 21:03:546596}
6597
dcheng59716272016-04-09 05:19:086598std::unique_ptr<WebUIImpl> WebContentsImpl::CreateWebUIForRenderFrameHost(
Denis Kuznetsov [CET]2c6f0159f2020-05-18 14:18:166599 const GURL& url) {
6600 return CreateWebUI(url);
[email protected]420ae012009-04-24 05:16:326601}
6602
lfgbede6392015-09-11 21:54:066603void WebContentsImpl::CreateRenderWidgetHostViewForRenderManager(
6604 RenderViewHost* render_view_host) {
lfgc740d4b22016-04-15 16:45:336605 RenderWidgetHostViewBase* rwh_view =
W. James MacLean13d834d2019-12-04 16:06:456606 view_->CreateViewForWidget(render_view_host->GetWidget());
danakja0bce4c952020-05-12 20:40:286607 rwh_view->SetSize(GetSizeForMainFrame());
lfgbede6392015-09-11 21:54:066608}
6609
6610bool WebContentsImpl::CreateRenderViewForRenderManager(
6611 RenderViewHost* render_view_host,
Antonio Gomesb3dd5fc2020-05-25 22:53:276612 const base::Optional<base::UnguessableToken>& opener_frame_token,
Daniel Chengabfdb002020-06-04 06:02:306613 int proxy_routing_id) {
lfgbede6392015-09-11 21:54:066614 TRACE_EVENT0("browser,navigation",
6615 "WebContentsImpl::CreateRenderViewForRenderManager");
6616
piman00cf9402017-04-03 17:09:206617 if (proxy_routing_id == MSG_ROUTING_NONE)
lfg9431efd2016-12-14 14:45:266618 CreateRenderWidgetHostViewForRenderManager(render_view_host);
lfg9431efd2016-12-14 14:45:266619
piman00cf9402017-04-03 17:09:206620 if (!static_cast<RenderViewHostImpl*>(render_view_host)
Daniel Chengabfdb002020-06-04 06:02:306621 ->CreateRenderView(opener_frame_token, proxy_routing_id,
Antonio Gomesb3dd5fc2020-05-25 22:53:276622 created_with_opener_)) {
[email protected]a4127722011-04-27 23:13:526623 return false;
[email protected]14392a52012-05-02 20:28:446624 }
W. James MacLean62198672019-06-04 16:46:106625 // Set the TextAutosizer state from the main frame's renderer on the new view,
6626 // but only if it's not for the main frame. Main frame renderers should create
6627 // this state themselves from up-to-date values, so we shouldn't override it
6628 // with the cached values.
6629 if (!render_view_host->GetMainFrame()) {
6630 render_view_host->Send(
6631 new PageMsg_UpdateTextAutosizerPageInfoForRemoteMainFrames(
6632 render_view_host->GetRoutingID(), text_autosizer_page_info_));
6633 }
[email protected]a4127722011-04-27 23:13:526634
Lucas Furukawa Gadaniaed1fed2017-10-13 17:34:146635 if (proxy_routing_id == MSG_ROUTING_NONE && node_.outer_web_contents())
6636 ReattachToOuterWebContentsFrame();
6637
avi2b177592014-12-10 02:08:026638 SetHistoryOffsetAndLengthForView(render_view_host,
6639 controller_.GetLastCommittedEntryIndex(),
6640 controller_.GetEntryCount());
6641
[email protected]a220b5932013-09-21 03:47:446642#if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID)
[email protected]f8233cc2011-05-31 20:24:506643 // Force a ViewMsg_Resize to be sent, needed to make plugins show up on
6644 // linux. See crbug.com/83941.
avif9ab5d942015-10-15 14:05:446645 RenderWidgetHostView* rwh_view = render_view_host->GetWidget()->GetView();
[email protected]245f7d52011-11-28 15:36:446646 if (rwh_view) {
6647 if (RenderWidgetHost* render_widget_host = rwh_view->GetRenderWidgetHost())
Fady Samuel0b911822018-04-25 13:22:166648 render_widget_host->SynchronizeVisualProperties();
[email protected]245f7d52011-11-28 15:36:446649 }
[email protected]f8233cc2011-05-31 20:24:506650#endif
6651
[email protected]420ae012009-04-24 05:16:326652 return true;
6653}
6654
[email protected]82307f6b2014-08-07 03:30:126655bool WebContentsImpl::CreateRenderFrameForRenderManager(
6656 RenderFrameHost* render_frame_host,
arthursonzogni1efc7b962019-03-14 09:36:336657 int previous_routing_id,
Antonio Gomes9d150542020-05-25 18:32:116658 const base::Optional<base::UnguessableToken>& opener_frame_token,
japhete6adf142014-10-31 00:01:496659 int parent_routing_id,
alexmosa181efc2015-09-03 00:39:046660 int previous_sibling_routing_id) {
naskob8744d22014-08-28 17:07:436661 TRACE_EVENT0("browser,navigation",
6662 "WebContentsImpl::CreateRenderFrameForRenderManager");
[email protected]82307f6b2014-08-07 03:30:126663
6664 RenderFrameHostImpl* rfh =
6665 static_cast<RenderFrameHostImpl*>(render_frame_host);
Antonio Gomes9d150542020-05-25 18:32:116666 if (!rfh->CreateRenderFrame(previous_routing_id, opener_frame_token,
6667 parent_routing_id, previous_sibling_routing_id)) {
[email protected]82307f6b2014-08-07 03:30:126668 return false;
Antonio Gomes9d150542020-05-25 18:32:116669 }
[email protected]82307f6b2014-08-07 03:30:126670
6671 // TODO(nasko): When RenderWidgetHost is owned by RenderFrameHost, the passed
6672 // RenderFrameHost will have to be associated with the appropriate
6673 // RenderWidgetHostView or a new one should be created here.
6674
6675 return true;
6676}
6677
mfomitchev2b8b066a2016-01-28 19:23:156678#if defined(OS_ANDROID)
[email protected]fc2b46b2014-05-03 16:33:456679
[email protected]155c7f22013-12-09 17:07:186680base::android::ScopedJavaLocalRef<jobject>
6681WebContentsImpl::GetJavaWebContents() {
mostynb042582e2015-03-16 22:13:406682 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]0e813a52014-08-13 10:34:566683 return GetWebContentsAndroid()->GetJavaObject();
6684}
[email protected]155c7f22013-12-09 17:07:186685
[email protected]0e813a52014-08-13 10:34:566686WebContentsAndroid* WebContentsImpl::GetWebContentsAndroid() {
Kevin McNee576bb5d82019-05-29 16:47:516687 if (!web_contents_android_) {
6688 web_contents_android_ = std::make_unique<WebContentsAndroid>(this);
[email protected]155c7f22013-12-09 17:07:186689 }
Kevin McNee576bb5d82019-05-29 16:47:516690 return web_contents_android_.get();
6691}
6692
6693void WebContentsImpl::ClearWebContentsAndroid() {
6694 DCHECK_CURRENTLY_ON(BrowserThread::UI);
6695 web_contents_android_.reset();
[email protected]155c7f22013-12-09 17:07:186696}
6697
paulmeyerc0b762b2016-04-13 11:55:176698void WebContentsImpl::ActivateNearestFindResult(float x,
6699 float y) {
6700 GetOrCreateFindRequestManager()->ActivateNearestFindResult(x, y);
6701}
6702
6703void WebContentsImpl::RequestFindMatchRects(int current_version) {
6704 GetOrCreateFindRequestManager()->RequestFindMatchRects(current_version);
6705}
6706
rockot400ea35b2016-10-15 19:15:326707service_manager::InterfaceProvider* WebContentsImpl::GetJavaInterfaces() {
sammcf5f1b0f2016-09-20 23:05:116708 if (!java_interfaces_) {
Gyuyoung Kim6c9ce9022019-11-26 05:40:086709 mojo::PendingRemote<service_manager::mojom::InterfaceProvider> provider;
6710 BindInterfaceRegistryForWebContents(
6711 provider.InitWithNewPipeAndPassReceiver(), this);
rockot400ea35b2016-10-15 19:15:326712 java_interfaces_.reset(new service_manager::InterfaceProvider);
sammcf5f1b0f2016-09-20 23:05:116713 java_interfaces_->Bind(std::move(provider));
6714 }
6715 return java_interfaces_.get();
6716}
6717
Francois Doray6c26f3342018-08-24 17:17:226718#endif
6719
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:416720bool WebContentsImpl::CompletedFirstVisuallyNonEmptyPaint() {
Carlos Caballero91fffb22019-10-29 15:24:306721 return GetRenderViewHost()->did_first_visually_non_empty_paint();
spqchane72c3cb2017-06-30 23:02:106722}
6723
pkotwicz75ca8ffd2016-02-16 23:10:196724void WebContentsImpl::OnDidDownloadImage(
Avi Drissmana5a52dd2018-03-27 03:39:026725 ImageDownloadCallback callback,
pkotwicz75ca8ffd2016-02-16 23:10:196726 int id,
6727 const GURL& image_url,
leon.hancbc4bc3c2016-02-26 07:08:526728 int32_t http_status_code,
leon.hanc2228532016-08-16 05:59:186729 const std::vector<SkBitmap>& images,
6730 const std::vector<gfx::Size>& original_image_sizes) {
Avi Drissmana5a52dd2018-03-27 03:39:026731 std::move(callback).Run(id, http_status_code, image_url, images,
6732 original_image_sizes);
pkotwicz75ca8ffd2016-02-16 23:10:196733}
6734
[email protected]6fba26d2014-04-29 09:38:286735void WebContentsImpl::OnDialogClosed(int render_process_id,
6736 int render_frame_id,
Dave Tapuskacdf545cc2020-01-23 18:38:526737 JavaScriptDialogCallback response_callback,
Avi Drissman1a55a9d62020-03-10 18:56:456738 base::ScopedClosureRunner fullscreen_block,
[email protected]87de04b02014-04-08 22:14:496739 bool dialog_was_suppressed,
[email protected]b172aee2012-04-10 17:05:266740 bool success,
[email protected]fcf75d42013-12-03 20:11:266741 const base::string16& user_input) {
[email protected]6fba26d2014-04-29 09:38:286742 RenderFrameHostImpl* rfh = RenderFrameHostImpl::FromID(render_process_id,
6743 render_frame_id);
[email protected]87de04b02014-04-08 22:14:496744 last_dialog_suppressed_ = dialog_was_suppressed;
Avi Drissman1a55a9d62020-03-10 18:56:456745 fullscreen_block.RunAndReset();
[email protected]87de04b02014-04-08 22:14:496746
Charlie Reis96077362019-07-04 00:34:046747 javascript_dialog_navigation_deferrer_.reset();
6748
[email protected]beb440c2009-11-06 04:08:546749 if (is_showing_before_unload_dialog_ && !success) {
clamybc6f4152016-04-01 20:14:236750 // It is possible for the current RenderFrameHost to have changed in the
6751 // meantime. Do not reset the navigation state in that case.
6752 if (rfh && rfh == rfh->frame_tree_node()->current_frame_host()) {
clamy44e84ce2016-02-22 15:38:256753 rfh->frame_tree_node()->BeforeUnloadCanceled();
clamybc6f4152016-04-01 20:14:236754 controller_.DiscardNonCommittedEntries();
6755 }
[email protected]ec8e8b02013-06-19 04:57:106756
Charles Reis15d60b132017-11-03 17:43:476757 // Update the URL display either way, to avoid showing a stale URL.
6758 NotifyNavigationStateChanged(INVALIDATE_TYPE_URL);
6759
ericwilligersde386342016-10-19 02:35:096760 for (auto& observer : observers_)
6761 observer.BeforeUnloadDialogCancelled();
[email protected]beb440c2009-11-06 04:08:546762 }
[email protected]87de04b02014-04-08 22:14:496763
Dave Tapuskacdf545cc2020-01-23 18:38:526764 std::move(response_callback).Run(success, user_input);
Pavel Feldman3c1842b2017-08-02 05:00:166765
Dave Tapuskacdf545cc2020-01-23 18:38:526766 std::vector<protocol::PageHandler*> page_handlers =
6767 protocol::PageHandler::EnabledForWebContents(this);
6768 for (auto* handler : page_handlers)
6769 handler->DidCloseJavaScriptDialog(success, user_input);
avi1c51fb62017-02-27 20:43:036770
6771 is_showing_javascript_dialog_ = false;
6772 is_showing_before_unload_dialog_ = false;
[email protected]beb440c2009-11-06 04:08:546773}
6774
[email protected]1ac10dca2013-08-20 20:47:046775bool WebContentsImpl::IsHidden() {
Francois Doraye6161152018-03-27 22:05:376776 return !IsBeingCaptured() && visibility_ != Visibility::VISIBLE;
[email protected]1ac10dca2013-08-20 20:47:046777}
6778
rob97250742015-12-10 17:45:156779int WebContentsImpl::GetOuterDelegateFrameTreeNodeId() {
lfg601233692017-03-06 22:45:446780 return node_.outer_contents_frame_tree_node_id();
lazyboy6ec48b2a2015-06-29 15:18:146781}
6782
ekaramadaeb3c5c2016-11-09 03:22:356783RenderWidgetHostImpl* WebContentsImpl::GetFullscreenRenderWidgetHost() const {
6784 return RenderWidgetHostImpl::FromID(fullscreen_widget_process_id_,
6785 fullscreen_widget_routing_id_);
6786}
6787
[email protected]b0936d22013-11-28 06:47:366788RenderFrameHostManager* WebContentsImpl::GetRenderManager() const {
[email protected]fa944cb82013-11-15 17:51:216789 return frame_tree_.root()->render_manager();
6790}
6791
[email protected]f8497342013-02-05 22:15:026792BrowserPluginGuest* WebContentsImpl::GetBrowserPluginGuest() const {
[email protected]7a846df2012-09-20 19:17:396793 return browser_plugin_guest_.get();
6794}
6795
Albert J. Wongc55cc64d2018-10-12 02:50:046796void WebContentsImpl::SetBrowserPluginGuest(
6797 std::unique_ptr<BrowserPluginGuest> guest) {
6798 DCHECK(!browser_plugin_guest_);
6799 DCHECK(guest);
6800 browser_plugin_guest_ = std::move(guest);
[email protected]738f57a2013-06-29 21:06:546801}
6802
Aidan Wolter52bcc12a2018-11-14 03:27:296803base::UnguessableToken WebContentsImpl::GetAudioGroupId() {
Aidan Wolter52bcc12a2018-11-14 03:27:296804 return GetAudioStreamFactory()->group_id();
6805}
6806
Miyoung Shin5709ab02020-02-19 06:50:416807const std::vector<blink::mojom::FaviconURLPtr>&
6808WebContentsImpl::GetFaviconURLs() {
Mounir Lamouri7b4a1f232019-12-23 16:32:106809 return favicon_urls_;
6810}
6811
[email protected]f8497342013-02-05 22:15:026812BrowserPluginEmbedder* WebContentsImpl::GetBrowserPluginEmbedder() const {
[email protected]7a846df2012-09-20 19:17:396813 return browser_plugin_embedder_.get();
6814}
[email protected]8ff00d72012-10-23 19:12:216815
fsamuel60b42282015-03-10 03:29:146816void WebContentsImpl::CreateBrowserPluginEmbedderIfNecessary() {
6817 if (browser_plugin_embedder_)
6818 return;
6819 browser_plugin_embedder_.reset(BrowserPluginEmbedder::Create(this));
6820}
6821
danakja0bce4c952020-05-12 20:40:286822gfx::Size WebContentsImpl::GetSizeForMainFrame() {
6823 if (delegate_) {
6824 // The delegate has a chance to specify a size independent of the UI.
6825 gfx::Size delegate_size = delegate_->GetSizeForNewRenderView(this);
6826 if (!delegate_size.IsEmpty())
6827 return delegate_size;
6828 }
6829
6830 // Device emulation, when enabled, can specify a size independent of the UI.
6831 if (!device_emulation_size_.IsEmpty())
6832 return device_emulation_size_;
6833
6834 return GetContainerBounds().size();
[email protected]dc0fd432013-08-27 15:29:216835}
6836
[email protected]ae7eeda2014-07-04 01:53:266837void WebContentsImpl::OnFrameRemoved(RenderFrameHost* render_frame_host) {
ericwilligersde386342016-10-19 02:35:096838 for (auto& observer : observers_)
6839 observer.FrameDeleted(render_frame_host);
[email protected]9b159a52013-10-03 17:24:556840}
6841
[email protected]222f5822014-02-05 23:40:496842void WebContentsImpl::OnPreferredSizeChanged(const gfx::Size& old_size) {
6843 if (!delegate_)
6844 return;
6845 const gfx::Size new_size = GetPreferredSize();
6846 if (new_size != old_size)
6847 delegate_->UpdatePreferredSize(this, new_size);
6848}
6849
Denis Kuznetsov [CET]2c6f0159f2020-05-18 14:18:166850std::unique_ptr<WebUIImpl> WebContentsImpl::CreateWebUI(const GURL& url) {
6851 std::unique_ptr<WebUIImpl> web_ui = std::make_unique<WebUIImpl>(this);
Lei Zhangee3b63ad2018-06-06 23:25:316852 std::unique_ptr<WebUIController> controller(
avie865b1d2016-10-24 19:42:596853 WebUIControllerFactoryRegistry::GetInstance()
Lei Zhangee3b63ad2018-06-06 23:25:316854 ->CreateWebUIControllerForURL(web_ui.get(), url));
dbeama1b926a2015-08-31 23:17:516855 if (controller) {
Lei Zhangee3b63ad2018-06-06 23:25:316856 web_ui->SetController(std::move(controller));
dbeama1b926a2015-08-31 23:17:516857 return web_ui;
6858 }
6859
avie865b1d2016-10-24 19:42:596860 return nullptr;
dbeama1b926a2015-08-31 23:17:516861}
6862
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:416863FindRequestManager* WebContentsImpl::GetFindRequestManager() {
6864 for (auto* contents = this; contents;
paulmeyerfeafc2d2017-04-25 21:46:406865 contents = contents->GetOuterWebContents()) {
6866 if (contents->find_request_manager_)
6867 return contents->find_request_manager_.get();
6868 }
6869
6870 return nullptr;
6871}
6872
paulmeyerc0b762b2016-04-13 11:55:176873FindRequestManager* WebContentsImpl::GetOrCreateFindRequestManager() {
paulmeyerfeafc2d2017-04-25 21:46:406874 if (FindRequestManager* manager = GetFindRequestManager())
6875 return manager;
6876
Ehsan Karamad6beb2ea2018-11-25 18:15:136877 DCHECK(!browser_plugin_guest_ || GetOuterWebContents());
6878
paulmeyerfeafc2d2017-04-25 21:46:406879 // No existing FindRequestManager found, so one must be created.
6880 find_request_manager_.reset(new FindRequestManager(this));
6881
6882 // Concurrent find sessions must not overlap, so destroy any existing
6883 // FindRequestManagers in any inner WebContentses.
Lucas Furukawa Gadani2ec00c82018-12-14 15:53:166884 for (WebContents* contents : GetWebContentsAndAllInner()) {
6885 auto* web_contents_impl = static_cast<WebContentsImpl*>(contents);
6886 if (web_contents_impl == this)
paulmeyerfeafc2d2017-04-25 21:46:406887 continue;
Lucas Furukawa Gadani2ec00c82018-12-14 15:53:166888 if (web_contents_impl->find_request_manager_) {
6889 web_contents_impl->find_request_manager_->StopFinding(
6890 STOP_FIND_ACTION_CLEAR_SELECTION);
6891 web_contents_impl->find_request_manager_.release();
paulmeyerfeafc2d2017-04-25 21:46:406892 }
6893 }
paulmeyerc0b762b2016-04-13 11:55:176894
6895 return find_request_manager_.get();
6896}
6897
6898void WebContentsImpl::NotifyFindReply(int request_id,
6899 int number_of_matches,
6900 const gfx::Rect& selection_rect,
6901 int active_match_ordinal,
6902 bool final_update) {
Sebastien Marchandcf97ed52017-07-24 23:50:346903 if (delegate_ && !is_being_destroyed_ &&
Lukasz Anforowicze9ae3722017-09-29 17:37:386904 !GetMainFrame()->GetProcess()->FastShutdownStarted()) {
paulmeyerc0b762b2016-04-13 11:55:176905 delegate_->FindReply(this, request_id, number_of_matches, selection_rect,
6906 active_match_ordinal, final_update);
6907 }
6908}
6909
ortuno467e5792016-06-10 04:32:396910void WebContentsImpl::IncrementBluetoothConnectedDeviceCount() {
6911 // Trying to invalidate the tab state while being destroyed could result in a
6912 // use after free.
6913 if (IsBeingDestroyed()) {
6914 return;
6915 }
6916 // Notify for UI updates if the state changes.
6917 bluetooth_connected_device_count_++;
6918 if (bluetooth_connected_device_count_ == 1) {
Francois Doray66766a572019-12-17 20:56:496919 OnIsConnectedToBluetoothDeviceChanged(true);
ortuno467e5792016-06-10 04:32:396920 }
6921}
6922
6923void WebContentsImpl::DecrementBluetoothConnectedDeviceCount() {
6924 // Trying to invalidate the tab state while being destroyed could result in a
6925 // use after free.
6926 if (IsBeingDestroyed()) {
6927 return;
6928 }
6929 // Notify for UI updates if the state changes.
Lukasz Anforowicze9ae3722017-09-29 17:37:386930 DCHECK_NE(bluetooth_connected_device_count_, 0u);
ortuno467e5792016-06-10 04:32:396931 bluetooth_connected_device_count_--;
6932 if (bluetooth_connected_device_count_ == 0) {
Francois Doray66766a572019-12-17 20:56:496933 OnIsConnectedToBluetoothDeviceChanged(false);
6934 }
6935}
6936
6937void WebContentsImpl::OnIsConnectedToBluetoothDeviceChanged(
6938 bool is_connected_to_bluetooth_device) {
6939 NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB);
6940 for (auto& observer : observers_) {
6941 observer.OnIsConnectedToBluetoothDeviceChanged(
6942 is_connected_to_bluetooth_device);
ortuno467e5792016-06-10 04:32:396943 }
6944}
6945
Reilly Grant5e7c79b22019-04-09 17:26:206946void WebContentsImpl::IncrementSerialActiveFrameCount() {
6947 // Trying to invalidate the tab state while being destroyed could result in a
6948 // use after free.
6949 if (IsBeingDestroyed())
6950 return;
6951
6952 // Notify for UI updates if the state changes.
6953 serial_active_frame_count_++;
6954 if (serial_active_frame_count_ == 1)
6955 NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB);
6956}
6957
6958void WebContentsImpl::DecrementSerialActiveFrameCount() {
6959 // Trying to invalidate the tab state while being destroyed could result in a
6960 // use after free.
6961 if (IsBeingDestroyed())
6962 return;
6963
6964 // Notify for UI updates if the state changes.
6965 DCHECK_NE(0u, serial_active_frame_count_);
6966 serial_active_frame_count_--;
6967 if (serial_active_frame_count_ == 0)
6968 NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB);
6969}
6970
Matt Reynoldse8c6c1f2019-11-02 09:53:536971void WebContentsImpl::IncrementHidActiveFrameCount() {
6972 // Trying to invalidate the tab state while being destroyed could result in a
6973 // use after free.
6974 if (IsBeingDestroyed())
6975 return;
6976
6977 // Notify for UI updates if the active frame count transitions from zero to
6978 // non-zero.
6979 hid_active_frame_count_++;
6980 if (hid_active_frame_count_ == 1)
6981 NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB);
6982}
6983
6984void WebContentsImpl::DecrementHidActiveFrameCount() {
6985 // Trying to invalidate the tab state while being destroyed could result in a
6986 // use after free.
6987 if (IsBeingDestroyed())
6988 return;
6989
6990 // Notify for UI updates if the active frame count transitions from non-zero
6991 // to zero.
6992 DCHECK_NE(0u, hid_active_frame_count_);
6993 hid_active_frame_count_--;
6994 if (hid_active_frame_count_ == 0)
6995 NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB);
6996}
6997
Marijn Kruisselbrink29051042019-08-06 22:56:556998void WebContentsImpl::IncrementNativeFileSystemHandleCount() {
6999 // Trying to invalidate the tab state while being destroyed could result in a
7000 // use after free.
7001 if (IsBeingDestroyed())
7002 return;
7003
7004 // Notify for UI updates if the state changes. Need both TYPE_TAB and TYPE_URL
7005 // to update both the tab-level usage indicator and the usage indicator in the
7006 // omnibox.
7007 native_file_system_handle_count_++;
7008 if (native_file_system_handle_count_ == 1) {
7009 NotifyNavigationStateChanged(static_cast<content::InvalidateTypes>(
7010 INVALIDATE_TYPE_TAB | INVALIDATE_TYPE_URL));
7011 }
7012}
7013
7014void WebContentsImpl::DecrementNativeFileSystemHandleCount() {
7015 // Trying to invalidate the tab state while being destroyed could result in a
7016 // use after free.
7017 if (IsBeingDestroyed())
7018 return;
7019
7020 // Notify for UI updates if the state changes. Need both TYPE_TAB and TYPE_URL
7021 // to update both the tab-level usage indicator and the usage indicator in the
7022 // omnibox.
7023 DCHECK_NE(0u, native_file_system_handle_count_);
7024 native_file_system_handle_count_--;
7025 if (native_file_system_handle_count_ == 0) {
7026 NotifyNavigationStateChanged(static_cast<content::InvalidateTypes>(
7027 INVALIDATE_TYPE_TAB | INVALIDATE_TYPE_URL));
7028 }
7029}
7030
Marijn Kruisselbrink46fa4bd2019-07-11 18:15:477031void WebContentsImpl::AddNativeFileSystemDirectoryHandle(
7032 const base::FilePath& path) {
Marijn Kruisselbrinkf1714aa22019-07-02 03:45:337033 // Trying to invalidate the tab state while being destroyed could result in a
7034 // use after free.
7035 if (IsBeingDestroyed())
7036 return;
7037
7038 // Notify for UI updates if the state changes. Need both TYPE_TAB and TYPE_URL
7039 // to update both the tab-level usage indicator and the usage indicator in the
7040 // omnibox.
Marijn Kruisselbrink46fa4bd2019-07-11 18:15:477041 const bool was_empty = native_file_system_directory_handles_.empty();
7042 native_file_system_directory_handles_[path]++;
7043 if (was_empty) {
Marijn Kruisselbrinkf1714aa22019-07-02 03:45:337044 NotifyNavigationStateChanged(static_cast<content::InvalidateTypes>(
7045 INVALIDATE_TYPE_TAB | INVALIDATE_TYPE_URL));
7046 }
7047}
7048
Marijn Kruisselbrink46fa4bd2019-07-11 18:15:477049void WebContentsImpl::RemoveNativeFileSystemDirectoryHandle(
7050 const base::FilePath& path) {
Marijn Kruisselbrinkf1714aa22019-07-02 03:45:337051 // Trying to invalidate the tab state while being destroyed could result in a
7052 // use after free.
7053 if (IsBeingDestroyed())
7054 return;
7055
7056 // Notify for UI updates if the state changes. Need both TYPE_TAB and TYPE_URL
7057 // to update both the tab-level usage indicator and the usage indicator in the
7058 // omnibox.
Marijn Kruisselbrink46fa4bd2019-07-11 18:15:477059 auto it = native_file_system_directory_handles_.find(path);
7060 DCHECK(it != native_file_system_directory_handles_.end());
7061 DCHECK_NE(0u, it->second);
7062 it->second--;
7063 if (it->second == 0)
7064 native_file_system_directory_handles_.erase(it);
7065 if (native_file_system_directory_handles_.empty()) {
Marijn Kruisselbrinkf1714aa22019-07-02 03:45:337066 NotifyNavigationStateChanged(static_cast<content::InvalidateTypes>(
7067 INVALIDATE_TYPE_TAB | INVALIDATE_TYPE_URL));
7068 }
7069}
7070
7071void WebContentsImpl::IncrementWritableNativeFileSystemHandleCount() {
7072 // Trying to invalidate the tab state while being destroyed could result in a
7073 // use after free.
7074 if (IsBeingDestroyed())
7075 return;
7076
7077 // Notify for UI updates if the state changes. Need both TYPE_TAB and TYPE_URL
7078 // to update both the tab-level usage indicator and the usage indicator in the
7079 // omnibox.
7080 native_file_system_writable_handle_count_++;
7081 if (native_file_system_writable_handle_count_ == 1) {
7082 NotifyNavigationStateChanged(static_cast<content::InvalidateTypes>(
7083 INVALIDATE_TYPE_TAB | INVALIDATE_TYPE_URL));
7084 }
7085}
7086
7087void WebContentsImpl::DecrementWritableNativeFileSystemHandleCount() {
7088 // Trying to invalidate the tab state while being destroyed could result in a
7089 // use after free.
7090 if (IsBeingDestroyed())
7091 return;
7092
7093 // Notify for UI updates if the state changes. Need both TYPE_TAB and TYPE_URL
7094 // to update both the tab-level usage indicator and the usage indicator in the
7095 // omnibox.
7096 DCHECK_NE(0u, native_file_system_writable_handle_count_);
7097 native_file_system_writable_handle_count_--;
7098 if (native_file_system_writable_handle_count_ == 0) {
7099 NotifyNavigationStateChanged(static_cast<content::InvalidateTypes>(
7100 INVALIDATE_TYPE_TAB | INVALIDATE_TYPE_URL));
7101 }
7102}
7103
zqzhang8ac49002017-03-16 21:51:357104void WebContentsImpl::SetHasPersistentVideo(bool has_persistent_video) {
7105 if (has_persistent_video_ == has_persistent_video)
mlamouri5cd9ae82017-02-18 11:05:097106 return;
7107
zqzhang8ac49002017-03-16 21:51:357108 has_persistent_video_ = has_persistent_video;
mlamouri5cd9ae82017-02-18 11:05:097109 NotifyPreferencesChanged();
zqzhang8ac49002017-03-16 21:51:357110 media_web_contents_observer()->RequestPersistentVideo(has_persistent_video);
mlamouri5cd9ae82017-02-18 11:05:097111}
7112
Ian Vollick5d5c37f2019-06-19 22:50:577113void WebContentsImpl::SetSpatialNavigationDisabled(bool disabled) {
7114 if (is_spatial_navigation_disabled_ == disabled)
7115 return;
7116
7117 is_spatial_navigation_disabled_ = disabled;
7118 NotifyPreferencesChanged();
7119}
7120
paulmeyerfeafc2d2017-04-25 21:46:407121void WebContentsImpl::BrowserPluginGuestWillDetach() {
7122 WebContentsImpl* outermost = GetOutermostWebContents();
7123 if (this != outermost && ContainsOrIsFocusedWebContents())
7124 outermost->SetAsFocusedWebContentsIfNecessary();
7125}
7126
Becca Hughes112832e2019-06-11 17:19:027127PictureInPictureResult WebContentsImpl::EnterPictureInPicture(
Mounir Lamouri11e9ef42018-05-22 03:10:167128 const viz::SurfaceId& surface_id,
7129 const gfx::Size& natural_size) {
François Beaufortf09e4462019-01-18 14:34:517130 return delegate_
7131 ? delegate_->EnterPictureInPicture(this, surface_id, natural_size)
Becca Hughes112832e2019-06-11 17:19:027132 : PictureInPictureResult::kNotSupported;
Mounir Lamouri11e9ef42018-05-22 03:10:167133}
7134
Mounir Lamouri6d759e12018-05-16 20:01:307135void WebContentsImpl::ExitPictureInPicture() {
7136 if (delegate_)
7137 delegate_->ExitPictureInPicture();
7138}
7139
paulmeyerc0b762b2016-04-13 11:55:177140#if defined(OS_ANDROID)
7141void WebContentsImpl::NotifyFindMatchRectsReply(
7142 int version,
7143 const std::vector<gfx::RectF>& rects,
7144 const gfx::RectF& active_rect) {
7145 if (delegate_)
7146 delegate_->FindMatchRectsReply(this, version, rects, active_rect);
7147}
7148#endif
7149
[email protected]34ff1cfc2014-08-20 06:16:057150void WebContentsImpl::SetForceDisableOverscrollContent(bool force_disable) {
7151 force_disable_overscroll_content_ = force_disable;
7152 if (view_)
7153 view_->SetOverscrollControllerEnabled(CanOverscrollContent());
7154}
7155
Jianzhou Fengd8720c72018-01-18 03:06:307156bool WebContentsImpl::SetDeviceEmulationSize(const gfx::Size& new_size) {
7157 device_emulation_size_ = new_size;
7158 RenderWidgetHostView* rwhv = GetMainFrame()->GetView();
7159
7160 const gfx::Size current_size = rwhv->GetViewBounds().size();
7161 if (view_size_before_emulation_.IsEmpty())
7162 view_size_before_emulation_ = current_size;
7163
7164 if (current_size != new_size)
7165 rwhv->SetSize(new_size);
7166
7167 return current_size != new_size;
7168}
7169
7170void WebContentsImpl::ClearDeviceEmulationSize() {
7171 RenderWidgetHostView* rwhv = GetMainFrame()->GetView();
7172 // WebContentsView could get resized during emulation, which also resizes
7173 // RWHV. If it happens, assume user would like to keep using the size after
7174 // emulation.
7175 // TODO(jzfeng): Prohibit resizing RWHV through any other means (at least when
7176 // WebContentsView size changes).
Dmitry Gozmanc63707182018-03-06 00:09:397177 if (!view_size_before_emulation_.IsEmpty() && rwhv &&
7178 rwhv->GetViewBounds().size() == device_emulation_size_) {
Jianzhou Fengd8720c72018-01-18 03:06:307179 rwhv->SetSize(view_size_before_emulation_);
Dmitry Gozmanc63707182018-03-06 00:09:397180 }
Jianzhou Fengd8720c72018-01-18 03:06:307181 device_emulation_size_ = gfx::Size();
7182 view_size_before_emulation_ = gfx::Size();
7183}
7184
Max Morin5bc74f52018-05-09 07:00:217185ForwardingAudioStreamFactory* WebContentsImpl::GetAudioStreamFactory() {
7186 if (!audio_stream_factory_) {
7187 audio_stream_factory_.emplace(
7188 this,
Max Morinddebb972018-09-20 10:04:147189 // BrowserMainLoop::GetInstance() may be null in unit tests.
7190 BrowserMainLoop::GetInstance()
7191 ? static_cast<media::UserInputMonitorBase*>(
7192 BrowserMainLoop::GetInstance()->user_input_monitor())
7193 : nullptr,
Ken Rockot72964402019-12-06 10:40:517194 AudioStreamBrokerFactory::CreateImpl());
Max Morin5bc74f52018-05-09 07:00:217195 }
7196
7197 return &*audio_stream_factory_;
7198}
7199
dalecurtis88c240072015-12-09 02:11:187200void WebContentsImpl::MediaStartedPlaying(
billorr21c005d2016-11-17 03:57:047201 const WebContentsObserver::MediaPlayerInfo& media_info,
Mounir Lamouri0f5bdf52019-04-04 16:32:357202 const MediaPlayerId& id) {
billorr21c005d2016-11-17 03:57:047203 if (media_info.has_video)
7204 currently_playing_video_count_++;
7205
ericwilligersde386342016-10-19 02:35:097206 for (auto& observer : observers_)
billorr21c005d2016-11-17 03:57:047207 observer.MediaStartedPlaying(media_info, id);
dalecurtis88c240072015-12-09 02:11:187208}
7209
7210void WebContentsImpl::MediaStoppedPlaying(
billorr21c005d2016-11-17 03:57:047211 const WebContentsObserver::MediaPlayerInfo& media_info,
Mounir Lamouri0f5bdf52019-04-04 16:32:357212 const MediaPlayerId& id,
Becca Hughes220bfc102017-11-14 18:24:077213 WebContentsObserver::MediaStoppedReason reason) {
billorr21c005d2016-11-17 03:57:047214 if (media_info.has_video)
7215 currently_playing_video_count_--;
7216
ericwilligersde386342016-10-19 02:35:097217 for (auto& observer : observers_)
Becca Hughes220bfc102017-11-14 18:24:077218 observer.MediaStoppedPlaying(media_info, id, reason);
billorr21c005d2016-11-17 03:57:047219}
7220
Mounir Lamouri0f5bdf52019-04-04 16:32:357221void WebContentsImpl::MediaResized(const gfx::Size& size,
7222 const MediaPlayerId& id) {
peconn257951522017-06-09 18:24:597223 cached_video_sizes_[id] = size;
7224
7225 for (auto& observer : observers_)
7226 observer.MediaResized(size, id);
7227}
7228
Peter E Conneccb34c22017-09-08 09:37:587229void WebContentsImpl::MediaEffectivelyFullscreenChanged(bool is_fullscreen) {
7230 for (auto& observer : observers_)
7231 observer.MediaEffectivelyFullscreenChanged(is_fullscreen);
7232}
7233
Peter Conn31c726a2017-08-17 11:07:347234base::Optional<gfx::Size> WebContentsImpl::GetFullscreenVideoSize() {
Mounir Lamouri0f5bdf52019-04-04 16:32:357235 base::Optional<MediaPlayerId> id =
Peter Conn31c726a2017-08-17 11:07:347236 media_web_contents_observer_->GetFullscreenVideoMediaPlayerId();
Jan Wilken Dörrie531be7ca2019-06-07 10:05:577237 if (id && base::Contains(cached_video_sizes_, id.value()))
Peter Conn31c726a2017-08-17 11:07:347238 return base::Optional<gfx::Size>(cached_video_sizes_[id.value()]);
Lei Zhang6410ec1a2019-01-31 21:38:587239 return base::nullopt;
peconn257951522017-06-09 18:24:597240}
7241
billorr21c005d2016-11-17 03:57:047242int WebContentsImpl::GetCurrentlyPlayingVideoCount() {
7243 return currently_playing_video_count_;
dalecurtis88c240072015-12-09 02:11:187244}
7245
Raymond Toye413650c2018-08-31 00:12:477246void WebContentsImpl::AudioContextPlaybackStarted(RenderFrameHost* host,
7247 int context_id) {
7248 WebContentsObserver::AudioContextId audio_context_id(host, context_id);
7249 for (auto& observer : observers_)
7250 observer.AudioContextPlaybackStarted(audio_context_id);
7251}
7252
7253void WebContentsImpl::AudioContextPlaybackStopped(RenderFrameHost* host,
7254 int context_id) {
7255 WebContentsObserver::AudioContextId audio_context_id(host, context_id);
7256 for (auto& observer : observers_)
7257 observer.AudioContextPlaybackStopped(audio_context_id);
7258}
7259
Chris Hamilton16ca64a2020-05-14 23:13:177260void WebContentsImpl::OnFrameAudioStateChanged(RenderFrameHost* host,
7261 bool is_audible) {
7262 for (auto& observer : observers_)
7263 observer.OnFrameAudioStateChanged(host, is_audible);
7264}
7265
evliu7f736f02019-11-07 17:43:507266media::MediaMetricsProvider::RecordAggregateWatchTimeCallback
7267WebContentsImpl::GetRecordAggregateWatchTimeCallback() {
Becca Hughes53652442020-03-17 15:35:427268 if (!delegate_ || !delegate_->GetDelegateWeakPtr())
7269 return base::DoNothing();
7270
evliu7f736f02019-11-07 17:43:507271 return base::BindRepeating(
Becca Hughes53652442020-03-17 15:35:427272 [](base::WeakPtr<WebContentsDelegate> delegate, GURL last_committed_url,
7273 base::TimeDelta total_watch_time, base::TimeDelta time_stamp,
7274 bool has_video, bool has_audio) {
evliua283c9e2019-11-08 22:57:227275 content::MediaPlayerWatchTime watch_time(
7276 last_committed_url, last_committed_url.GetOrigin(),
7277 total_watch_time, time_stamp, has_video, has_audio);
evliu7f736f02019-11-07 17:43:507278
7279 // Save the watch time if the delegate is still alive.
7280 if (delegate)
7281 delegate->MediaWatchTimeChanged(watch_time);
7282 },
Becca Hughes53652442020-03-17 15:35:427283 delegate_->GetDelegateWeakPtr(), GetMainFrameLastCommittedURL());
evliu7f736f02019-11-07 17:43:507284}
7285
Ehsan Karamad1c631762019-05-13 23:04:017286RenderFrameHostImpl* WebContentsImpl::GetMainFrameForInnerDelegate(
7287 FrameTreeNode* frame_tree_node) {
7288 if (auto* web_contents = node_.GetInnerWebContentsInFrame(frame_tree_node))
7289 return web_contents->GetMainFrame();
7290 return nullptr;
7291}
7292
John Budorickee1c9512019-10-17 15:32:197293bool WebContentsImpl::IsFrameLowPriority(
7294 const RenderFrameHost* render_frame_host) {
7295 if (!delegate_)
7296 return false;
7297 return delegate_->IsFrameLowPriority(this, render_frame_host);
7298}
7299
Roger Tawa03ffdbd2020-01-16 18:12:547300void WebContentsImpl::IsClipboardPasteAllowed(
7301 const GURL& url,
7302 const ui::ClipboardFormatType& data_type,
7303 const std::string& data,
7304 IsClipboardPasteAllowedCallback callback) {
7305 GetContentClient()->browser()->IsClipboardPasteAllowed(
7306 this, url, data_type, data, std::move(callback));
7307}
7308
Lan Weif81c6e7c2020-02-12 16:46:097309bool WebContentsImpl::HasSeenRecentScreenOrientationChange() {
7310 static constexpr base::TimeDelta kMaxInterval =
7311 base::TimeDelta::FromSeconds(1);
7312 base::TimeDelta delta =
7313 ui::EventTimeForNow() - last_screen_orientation_change_time_;
7314 // Return whether there is a screen orientation change happened in the recent
7315 // 1 second.
7316 return delta <= kMaxInterval;
7317}
7318
7319void WebContentsImpl::DidChangeScreenOrientation() {
7320 last_screen_orientation_change_time_ = ui::EventTimeForNow();
7321}
7322
Miyoung Shin8f2e1832020-04-27 09:00:027323bool WebContentsImpl::ShowPopupMenu(
Miyoung Shin4d947d22020-04-24 04:48:337324 RenderFrameHostImpl* render_frame_host,
Miyoung Shin8f2e1832020-04-27 09:00:027325 mojo::PendingRemote<blink::mojom::PopupMenuClient>* popup_client,
Miyoung Shin4d947d22020-04-24 04:48:337326 const gfx::Rect& bounds,
7327 int32_t item_height,
7328 double font_size,
7329 int32_t selected_item,
7330 std::vector<blink::mojom::MenuItemPtr>* menu_items,
7331 bool right_aligned,
7332 bool allow_multiple_selection) {
7333 for (auto& observer : observers_) {
Miyoung Shin8f2e1832020-04-27 09:00:027334 if (observer.ShowPopupMenu(render_frame_host, popup_client, bounds,
7335 item_height, font_size, selected_item,
7336 menu_items, right_aligned,
7337 allow_multiple_selection)) {
Miyoung Shin4d947d22020-04-24 04:48:337338 return true;
7339 }
7340 }
7341 return false;
7342}
7343
Francois Doraye6161152018-03-27 22:05:377344void WebContentsImpl::UpdateWebContentsVisibility(Visibility visibility) {
Francois Doray3f2afbd2018-04-06 19:18:187345 // Occlusion is disabled when |features::kWebContentsOcclusion| is disabled
7346 // (for power and speed impact assessment) or when
7347 // |switches::kDisableBackgroundingOccludedWindowsForTesting| is specified on
7348 // the command line (to avoid flakiness in browser tests).
7349 const bool occlusion_is_disabled =
7350 !base::FeatureList::IsEnabled(features::kWebContentsOcclusion) ||
Francois Doraye6161152018-03-27 22:05:377351 base::CommandLine::ForCurrentProcess()->HasSwitch(
7352 switches::kDisableBackgroundingOccludedWindowsForTesting);
7353 if (occlusion_is_disabled && visibility == Visibility::OCCLUDED)
7354 visibility = Visibility::VISIBLE;
7355
tapted65ff2ea72016-03-01 23:39:007356 if (!did_first_set_visible_) {
Francois Doraye6161152018-03-27 22:05:377357 if (visibility == Visibility::VISIBLE) {
7358 // A WebContents created with CreateParams::initially_hidden = false
7359 // starts with GetVisibility() == Visibility::VISIBLE even though it is
7360 // not really visible. Call WasShown() when it becomes visible for real as
7361 // the page load mechanism and some WebContentsObserver rely on that.
tapted65ff2ea72016-03-01 23:39:007362 WasShown();
Francois Dorayfe4a1772018-02-17 04:17:097363 did_first_set_visible_ = true;
tapted65ff2ea72016-03-01 23:39:007364 }
Francois Doraye6161152018-03-27 22:05:377365
7366 // Trust the initial visibility of the WebContents and do not switch it to
7367 // HIDDEN or OCCLUDED before it becomes VISIBLE for real. Doing so would
7368 // result in destroying resources that would immediately be recreated (e.g.
7369 // UpdateWebContents(HIDDEN) can be called when a WebContents is added to a
7370 // hidden window that is about to be shown).
7371
tapted65ff2ea72016-03-01 23:39:007372 return;
7373 }
Francois Doraye6161152018-03-27 22:05:377374
7375 if (visibility == visibility_)
tapted65ff2ea72016-03-01 23:39:007376 return;
7377
Collin Baker989e0882019-11-01 01:27:177378 UpdateVisibilityAndNotifyPageAndView(visibility);
tapted65ff2ea72016-03-01 23:39:007379}
7380
aelias5252baa2016-04-10 01:18:027381void WebContentsImpl::UpdateOverridingUserAgent() {
mlamouri5cd9ae82017-02-18 11:05:097382 NotifyPreferencesChanged();
aelias5252baa2016-04-10 01:18:027383}
7384
avid53461d2016-02-25 17:15:047385void WebContentsImpl::SetJavaScriptDialogManagerForTesting(
7386 JavaScriptDialogManager* dialog_manager) {
7387 dialog_manager_ = dialog_manager;
7388}
7389
Julie Jeongeun Kim7d0986172019-12-04 03:17:257390void WebContentsImpl::RemoveReceiverSet(const std::string& interface_name) {
7391 auto it = receiver_sets_.find(interface_name);
7392 if (it != receiver_sets_.end())
7393 receiver_sets_.erase(it);
rockotf62002a2016-09-15 00:08:597394}
7395
nzolghadrd87a308d2016-12-07 15:45:567396bool WebContentsImpl::AddDomainInfoToRapporSample(rappor::Sample* sample) {
nzolghadrbbffc6c2017-05-25 17:56:397397 // Here we associate this metric to the main frame URL regardless of what
7398 // caused it.
nzolghadrd87a308d2016-12-07 15:45:567399 sample->SetStringField("Domain", ::rappor::GetDomainAndRegistrySampleFromGURL(
7400 GetLastCommittedURL()));
nzolghadrd87a308d2016-12-07 15:45:567401 return true;
7402}
7403
falken52a56e32016-12-08 05:02:407404void WebContentsImpl::ShowInsecureLocalhostWarningIfNeeded() {
7405 bool allow_localhost = base::CommandLine::ForCurrentProcess()->HasSwitch(
7406 switches::kAllowInsecureLocalhost);
7407 if (!allow_localhost)
7408 return;
7409
7410 content::NavigationEntry* entry = GetController().GetLastCommittedEntry();
Rob Wuf79b3ba2018-01-14 01:54:317411 if (!entry || !net::IsLocalhost(entry->GetURL()))
falken52a56e32016-12-08 05:02:407412 return;
7413
7414 content::SSLStatus ssl_status = entry->GetSSL();
Matt Mueller936511442019-09-03 18:15:127415 if (!net::IsCertStatusError(ssl_status.cert_status))
falken52a56e32016-12-08 05:02:407416 return;
7417
7418 GetMainFrame()->AddMessageToConsole(
Abhijeet Kandalkara5928a0a2019-03-13 05:04:427419 blink::mojom::ConsoleMessageLevel::kWarning,
falken52a56e32016-12-08 05:02:407420 base::StringPrintf("This site does not have a valid SSL "
7421 "certificate! Without SSL, your site's and "
7422 "visitors' data is vulnerable to theft and "
7423 "tampering. Get a valid SSL certificate before"
7424 " releasing your website to the public."));
7425}
7426
ekaramadf6750aa2017-06-06 18:29:427427bool WebContentsImpl::IsShowingContextMenuOnPage() const {
7428 return showing_context_menu_;
7429}
7430
Min Qina904f3302018-02-13 23:33:347431download::DownloadUrlParameters::RequestHeadersType
7432WebContentsImpl::ParseDownloadHeaders(const std::string& headers) {
7433 download::DownloadUrlParameters::RequestHeadersType request_headers;
Megan Jablonski2f6a4c52017-07-10 23:01:257434 for (const base::StringPiece& key_value : base::SplitStringPiece(
7435 headers, "\r\n", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY)) {
7436 std::vector<std::string> pair = base::SplitString(
7437 key_value, ":", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
7438 if (2ul == pair.size())
7439 request_headers.push_back(make_pair(pair[0], pair[1]));
7440 }
7441 return request_headers;
7442}
7443
lukasza6f8ac622017-06-06 03:10:207444void WebContentsImpl::SetOpenerForNewContents(FrameTreeNode* opener,
7445 bool opener_suppressed) {
7446 if (opener) {
7447 FrameTreeNode* new_root = GetFrameTree()->root();
7448
7449 // For the "original opener", track the opener's main frame instead, because
7450 // if the opener is a subframe, the opener tracking could be easily bypassed
7451 // by spawning from a subframe and deleting the subframe.
7452 // https://crbug.com/705316
7453 new_root->SetOriginalOpener(opener->frame_tree()->root());
7454
7455 if (!opener_suppressed) {
7456 new_root->SetOpener(opener);
7457 created_with_opener_ = true;
7458 }
7459 }
7460}
7461
Mounir Lamouri0f5bdf52019-04-04 16:32:357462void WebContentsImpl::MediaMutedStatusChanged(const MediaPlayerId& id,
7463 bool muted) {
Becca Hughes9f6fd4b82017-06-15 10:01:407464 for (auto& observer : observers_)
7465 observer.MediaMutedStatusChanged(id, muted);
7466}
7467
EhsanK2075c7e2017-08-21 02:42:397468void WebContentsImpl::SetVisibilityForChildViews(bool visible) {
7469 GetMainFrame()->SetVisibilityForChildViews(visible);
7470}
7471
Alison Maher7f366dc62020-03-03 19:46:007472void WebContentsImpl::OnNativeThemeUpdated(ui::NativeTheme* observed_theme) {
7473 DCHECK(native_theme_observer_.IsObserving(observed_theme));
7474
7475 bool using_dark_colors = observed_theme->ShouldUseDarkColors();
7476 ui::NativeTheme::PreferredColorScheme preferred_color_scheme =
7477 observed_theme->GetPreferredColorScheme();
7478 bool preferences_changed = false;
7479
7480 if (using_dark_colors_ != using_dark_colors) {
7481 using_dark_colors_ = using_dark_colors;
7482 preferences_changed = true;
7483 }
7484 if (preferred_color_scheme_ != preferred_color_scheme) {
7485 preferred_color_scheme_ = preferred_color_scheme;
7486 preferences_changed = true;
7487 }
7488
7489 if (preferences_changed)
7490 NotifyPreferencesChanged();
7491}
7492
Dave Tapuska58a099e2020-06-08 21:48:407493blink::mojom::FrameWidgetInputHandler*
7494WebContentsImpl::GetFocusedFrameWidgetInputHandler() {
7495 auto* focused_render_widget_host =
7496 GetFocusedRenderWidgetHost(GetMainFrame()->GetRenderWidgetHost());
7497 if (!focused_render_widget_host)
Dave Tapuskae782bea2019-07-09 16:21:517498 return nullptr;
Dave Tapuska58a099e2020-06-08 21:48:407499 return focused_render_widget_host->GetFrameWidgetInputHandler();
Dave Tapuskae782bea2019-07-09 16:21:517500}
7501
Alexander Timine3ec4192020-04-20 16:39:407502ukm::SourceId WebContentsImpl::GetCurrentPageUkmSourceId() {
7503 return GetMainFrame()->GetPageUkmSourceId();
7504}
7505
Yuzu Saijo232076f2020-05-29 07:14:027506std::set<RenderViewHostImpl*>
7507WebContentsImpl::GetRenderViewHostsIncludingBackForwardCached() {
7508 std::set<RenderViewHostImpl*> render_view_hosts;
7509
7510 // Add RenderViewHostImpls outside of BackForwardCache.
7511 for (auto& render_view_host : frame_tree_.render_view_hosts()) {
7512 render_view_hosts.insert(render_view_host.second);
7513 }
7514
7515 // Add RenderViewHostImpls in BackForwardCache.
7516 const auto& entries = GetController().GetBackForwardCache().GetEntries();
7517 for (const auto& entry : entries) {
7518 std::set<RenderViewHostImpl*> bfcached_hosts = entry->render_view_hosts;
7519 render_view_hosts.insert(bfcached_hosts.begin(), bfcached_hosts.end());
7520 }
7521
7522 return render_view_hosts;
7523}
7524
Sreeja Kamishettybe0ccd6b2020-06-09 11:52:507525void WebContentsImpl::RenderFrameHostStateChanged(
7526 RenderFrameHost* render_frame_host,
7527 LifecycleState old_state,
7528 LifecycleState new_state) {
7529 if (render_frame_host->GetParent())
7530 return;
7531
7532 if (old_state == LifecycleState::kActive &&
7533 new_state != LifecycleState::kActive) {
7534 // TODO(sreejakshetty): Remove this reset when ColorChooser becomes
7535 // per-frame.
7536 // Close the color chooser popup when RenderFrameHost changes state from
7537 // kActive.
7538 color_chooser_.reset();
7539 }
7540}
7541
[email protected]8ff00d72012-10-23 19:12:217542} // namespace content