blob: 13550b725258eed90b286c514afd5bdb973a5a6d [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
Avi Drissman33972942020-06-19 14:16:019#include <algorithm>
w.shackleton49bcd392016-01-06 17:38:2210#include <cmath>
[email protected]2bb171882012-03-07 02:09:4611#include <utility>
rkaplowe56a52032017-02-07 00:14:5412#include <vector>
[email protected]b75b8292010-10-01 07:28:2513
Sebastien Marchandf8cbfab2019-01-25 16:02:3014#include "base/bind.h"
[email protected]36fb2c7c2011-04-04 15:49:0815#include "base/command_line.h"
Avi Drissman33972942020-06-19 14:16:0116#include "base/containers/flat_set.h"
creisfafed882016-07-29 00:03:0917#include "base/debug/dump_without_crashing.h"
zqzhang181047e62016-07-01 13:37:1718#include "base/feature_list.h"
Marijn Kruisselbrink46fa4bd2019-07-11 18:15:4719#include "base/files/file_path.h"
[email protected]cbb1ef592013-06-05 19:49:4620#include "base/lazy_instance.h"
skyostil95082a62015-06-05 19:53:0721#include "base/location.h"
[email protected]6d636e62013-07-31 03:15:5622#include "base/logging.h"
avib7348942015-12-25 20:57:1023#include "base/macros.h"
fdorayba121422016-12-23 19:51:4824#include "base/memory/ptr_util.h"
dmazzonid95ae842016-04-12 21:17:3925#include "base/memory/ref_counted.h"
Rakina Zata Amni4029b6d2020-07-28 02:36:2026#include "base/metrics/field_trial.h"
asvitkine8d51e9d2016-09-02 23:55:4327#include "base/metrics/histogram_macros.h"
bratell0a7406f2017-03-28 07:46:3728#include "base/metrics/user_metrics.h"
Avi Drissman4891b852018-09-25 18:12:5829#include "base/no_destructor.h"
Carlos Caballero1215f882019-10-29 15:58:4330#include "base/optional.h"
[email protected]04cbd3d2013-12-04 04:58:2031#include "base/process/process.h"
skyostil95082a62015-06-05 19:53:0732#include "base/single_thread_task_runner.h"
Arthur Sonzogni929d29f2018-09-17 14:19:4433#include "base/stl_util.h"
[email protected]348fbaac2013-06-11 06:31:5134#include "base/strings/string16.h"
35#include "base/strings/string_number_conversions.h"
Avi Drissman4891b852018-09-25 18:12:5836#include "base/strings/string_split.h"
[email protected]348fbaac2013-06-11 06:31:5137#include "base/strings/string_util.h"
[email protected]74ebfb12013-06-07 20:48:0038#include "base/strings/utf_string_conversions.h"
Rakina Zata Amni4029b6d2020-07-28 02:36:2039#include "base/system/sys_info.h"
gab30f26df2016-05-11 19:37:5540#include "base/threading/thread_task_runner_handle.h"
[email protected]a43858f2013-06-28 15:18:3741#include "base/time/time.h"
ssid3e765612015-01-28 04:03:4242#include "base/trace_event/trace_event.h"
avib7348942015-12-25 20:57:1043#include "build/build_config.h"
Min Qinda0ed2062018-02-23 22:00:5344#include "components/download/public/common/download_stats.h"
nzolghadrd87a308d2016-12-07 15:45:5645#include "components/rappor/public/rappor_utils.h"
rsleevi24f64dc22015-08-07 21:39:2146#include "components/url_formatter/url_formatter.h"
Abigail Kleinaa898742019-11-01 02:31:2547#include "content/browser/accessibility/accessibility_event_recorder.h"
James Wallace-Leeeafc94cb92018-07-23 21:35:0948#include "content/browser/accessibility/accessibility_tree_formatter_blink.h"
jamescookda2505812015-03-20 18:01:1849#include "content/browser/bad_message.h"
Max Morinddebb972018-09-20 10:04:1450#include "content/browser/browser_main_loop.h"
[email protected]7a846df2012-09-20 19:17:3951#include "content/browser/browser_plugin/browser_plugin_embedder.h"
52#include "content/browser/browser_plugin/browser_plugin_guest.h"
[email protected]b9535422012-02-09 01:47:5953#include "content/browser/child_process_security_policy_impl.h"
John Delaney732721e92020-02-07 23:11:2754#include "content/browser/conversions/conversion_host.h"
Pavel Feldman3c1842b2017-08-02 05:00:1655#include "content/browser/devtools/protocol/page_handler.h"
dgozmanf00c4f72016-10-19 17:45:1556#include "content/browser/devtools/render_frame_devtools_agent_host.h"
Becca Hughes3b5a43482018-07-17 22:31:5557#include "content/browser/display_cutout/display_cutout_host_impl.h"
[email protected]5f2aa722013-08-07 16:59:4158#include "content/browser/dom_storage/dom_storage_context_wrapper.h"
[email protected]1ea3c792012-04-17 01:25:0459#include "content/browser/dom_storage/session_storage_namespace_impl.h"
[email protected]aa4f3972012-03-01 18:12:1260#include "content/browser/download/mhtml_generation_manager.h"
[email protected]a53209b2012-01-20 16:48:1661#include "content/browser/download/save_package.h"
paulmeyerc0b762b2016-04-13 11:55:1762#include "content/browser/find_request_manager.h"
[email protected]bffc8302014-01-23 20:52:1663#include "content/browser/frame_host/cross_process_frame_connector.h"
ananta4b7467a52016-09-23 01:42:3864#include "content/browser/frame_host/frame_tree_node.h"
[email protected]d4a8ca482013-10-30 21:06:4065#include "content/browser/frame_host/navigation_entry_impl.h"
Arthur Hemeryc3380172018-01-22 14:00:1766#include "content/browser/frame_host/navigation_request.h"
Hiroki Nakagawa080f86c2020-05-13 10:56:4267#include "content/browser/frame_host/navigator.h"
[email protected]52913802013-12-10 05:52:1868#include "content/browser/frame_host/render_frame_host_impl.h"
lfgdb5c4ed2016-03-04 23:09:0769#include "content/browser/frame_host/render_frame_proxy_host.h"
Kenneth Russell954ed9ce2018-04-12 23:07:0170#include "content/browser/gpu/gpu_data_manager_impl.h"
mlamouriefdca9d2014-09-16 16:55:4071#include "content/browser/manifest/manifest_manager_host.h"
Max Morin5bc74f52018-05-09 07:00:2172#include "content/browser/media/audio_stream_broker.h"
dalecurtisbc6572e12014-09-12 19:22:3073#include "content/browser/media/audio_stream_monitor.h"
miu50f97892014-09-22 22:49:5274#include "content/browser/media/capture/web_contents_audio_muter.h"
dalecurtis88c240072015-12-09 02:11:1875#include "content/browser/media/media_web_contents_observer.h"
zqzhang1adf3cb2016-11-01 11:26:4576#include "content/browser/media/session/media_session_impl.h"
Yuzu Saijoa11069a2020-04-22 10:56:5577#include "content/browser/plugin_content_origin_allowlist.h"
Kevin McNee94ea52f52020-06-23 17:42:0678#include "content/browser/portal/portal.h"
David Black9cca3592019-11-06 23:02:2279#include "content/browser/renderer_host/frame_token_message_queue.h"
[email protected]f3b1a082011-11-18 00:34:3080#include "content/browser/renderer_host/render_process_host_impl.h"
[email protected]21161032014-05-05 16:56:5081#include "content/browser/renderer_host/render_view_host_delegate_view.h"
[email protected]b3c41c0b2012-03-06 15:48:3282#include "content/browser/renderer_host/render_view_host_impl.h"
[email protected]bafe6cd2012-05-23 23:09:5083#include "content/browser/renderer_host/render_widget_host_impl.h"
kenrb2a565f82015-09-02 20:24:5984#include "content/browser/renderer_host/render_widget_host_input_event_router.h"
[email protected]cfd80b02014-05-01 17:46:4885#include "content/browser/renderer_host/render_widget_host_view_base.h"
Ken Buchanandaef006b2017-08-17 18:32:1586#include "content/browser/renderer_host/render_widget_host_view_child_frame.h"
ekaramadadd882292016-06-08 15:22:5687#include "content/browser/renderer_host/text_input_manager.h"
Mike Wassermanb213b9352020-04-16 00:32:3488#include "content/browser/screen_enumeration/screen_change_monitor.h"
leon.han552e9de2017-02-09 14:37:3089#include "content/browser/screen_orientation/screen_orientation_provider.h"
[email protected]b6583592012-01-25 19:52:3390#include "content/browser/site_instance_impl.h"
Charlie Reis96077362019-07-04 00:34:0491#include "content/browser/web_contents/javascript_dialog_navigation_deferrer.h"
lfgc740d4b22016-04-15 16:45:3392#include "content/browser/web_contents/web_contents_view_child_frame.h"
Tsuyoshi Horob0d8d902020-03-12 00:09:3993#include "content/browser/web_package/save_as_web_bundle_job.h"
[email protected]86a0a6e2013-01-28 06:33:0394#include "content/browser/webui/web_ui_controller_factory_registry.h"
[email protected]d2353452012-01-19 19:53:5695#include "content/browser/webui/web_ui_impl.h"
[email protected]8eb04562013-03-06 03:41:1496#include "content/common/browser_plugin/browser_plugin_constants.h"
Rakina Zata Amni4029b6d2020-07-28 02:36:2097#include "content/common/content_switches_internal.h"
Paul Meyer0c58c3712016-11-17 22:59:5198#include "content/common/drag_messages.h"
[email protected]f114fa42013-12-06 17:06:4499#include "content/common/frame_messages.h"
[email protected]1f3fc1d2014-04-03 14:50:17100#include "content/common/input_messages.h"
lfgdb5c4ed2016-03-04 23:09:07101#include "content/common/page_messages.h"
nasko34634672016-07-29 18:46:06102#include "content/common/page_state_serialization.h"
rockot5c478a72016-09-28 23:14:18103#include "content/common/render_message_filter.mojom.h"
[email protected]2c5569662011-03-22 20:45:02104#include "content/common/view_messages.h"
Albert J. Wong3c93c182018-09-27 17:29:43105#include "content/common/widget_messages.h"
Abigail Kleinabb428332019-09-13 18:26:21106#include "content/public/browser/accessibility_tree_formatter.h"
[email protected]31a71eaf2014-03-13 01:47:36107#include "content/public/browser/ax_event_notification_details.h"
[email protected]ccb797302011-12-15 16:55:11108#include "content/public/browser/browser_context.h"
[email protected]139355f2014-05-11 14:21:28109#include "content/public/browser/browser_plugin_guest_manager.h"
Eric Seckler8652dcd52018-09-20 10:42:28110#include "content/public/browser/browser_task_traits.h"
Gabriel Charette790754c2018-03-16 21:32:59111#include "content/public/browser/browser_thread.h"
[email protected]87f3c082011-10-19 18:07:44112#include "content/public/browser/content_browser_client.h"
Lukasz Anforowicz8c4446dc2020-03-11 22:16:42113#include "content/public/browser/context_menu_params.h"
Ken Rockotce010f02019-12-12 23:32:32114#include "content/public/browser/device_service.h"
[email protected]e582fdd2011-12-20 16:48:17115#include "content/public/browser/download_manager.h"
Kent Tamura512a27e2018-10-04 00:49:32116#include "content/public/browser/file_select_listener.h"
ekaramada110f642016-12-21 19:47:28117#include "content/public/browser/focused_node_details.h"
Danyao Wanga78f3dd22020-03-05 05:31:27118#include "content/public/browser/global_routing_id.h"
[email protected]d9083482012-01-06 00:38:46119#include "content/public/browser/invalidate_type.h"
[email protected]71a88bb2013-02-01 22:05:15120#include "content/public/browser/javascript_dialog_manager.h"
skyf65d9bb2017-03-24 02:26:39121#include "content/public/browser/keyboard_event_processing_result.h"
[email protected]09d31d52012-03-11 22:30:27122#include "content/public/browser/load_notification_details.h"
[email protected]5b96836f2011-12-22 07:39:00123#include "content/public/browser/navigation_details.h"
[email protected]375fa1b2012-05-22 22:05:37124#include "content/public/browser/notification_details.h"
[email protected]be2510c02012-05-28 14:52:14125#include "content/public/browser/notification_service.h"
scottmg011d96e32016-06-29 19:15:08126#include "content/public/browser/notification_types.h"
[email protected]95640212014-07-26 18:14:30127#include "content/public/browser/render_widget_host_iterator.h"
Lukasz Anforowicze1b954d92017-10-30 21:28:06128#include "content/public/browser/restore_type.h"
estark5ea80e52015-06-19 18:43:12129#include "content/public/browser/security_style_explanations.h"
falken52a56e32016-12-08 05:02:40130#include "content/public/browser/ssl_status.h"
[email protected]4c3a23582012-08-18 08:54:34131#include "content/public/browser/storage_partition.h"
[email protected]674bc592011-12-20 23:00:42132#include "content/public/browser/web_contents_delegate.h"
Julie Jeongeun Kim7d0986172019-12-04 03:17:25133#include "content/public/browser/web_contents_receiver_set.h"
Lei Zhangee3b63ad2018-06-06 23:25:31134#include "content/public/browser/web_ui_controller.h"
[email protected]e091df82011-10-11 18:13:21135#include "content/public/common/bindings_policy.h"
alexmosc9e76ec2016-05-16 22:59:37136#include "content/public/common/child_process_host.h"
Hans Wennborg5ffd1392019-10-16 11:00:02137#include "content/public/common/content_client.h"
[email protected]54087fe2011-10-28 22:02:48138#include "content/public/common/content_constants.h"
Francois Doray3f2afbd2018-04-06 19:18:18139#include "content/public/common/content_features.h"
[email protected]64d69de42012-02-06 00:19:54140#include "content/public/common/content_switches.h"
Lukasz Anforowicze1b954d92017-10-30 21:28:06141#include "content/public/common/page_state.h"
Leon Han963dc182018-11-06 05:41:48142#include "content/public/common/referrer_type_converters.h"
[email protected]04cbd3d2013-12-04 04:58:20143#include "content/public/common/result_codes.h"
[email protected]f16cc6782013-12-16 23:42:57144#include "content/public/common/url_utils.h"
danakjffc8baf2019-10-10 23:49:42145#include "content/public/common/use_zoom_for_dsf_policy.h"
[email protected]b3a97b52014-07-09 06:25:05146#include "content/public/common/web_preferences.h"
Rakina Zata Amni4029b6d2020-07-28 02:36:20147#include "media/base/media_switches.h"
Max Morinddebb972018-09-20 10:04:14148#include "media/base/user_input_monitor.h"
wjmaclean64951902016-04-29 20:59:12149#include "net/base/url_util.h"
[email protected]8bfc8272013-09-09 20:10:53150#include "net/http/http_cache.h"
151#include "net/http/http_transaction_factory.h"
David Van Clevef99881632020-07-16 19:41:13152#include "net/http/http_util.h"
rhalavati0fbaeef32017-06-01 15:56:30153#include "net/traffic_annotation/network_traffic_annotation.h"
Scott Violet02e38b92018-03-27 23:42:14154#include "ppapi/buildflags/buildflags.h"
Mounir Lamourifd9dcefa2017-07-06 10:26:55155#include "services/metrics/public/cpp/ukm_recorder.h"
John Abd-El-Malekb175a612018-12-03 20:11:58156#include "services/network/public/cpp/features.h"
arthursonzognib93a4472020-04-10 07:38:00157#include "services/network/public/cpp/web_sandbox_flags.h"
158#include "services/network/public/mojom/web_sandbox_flags.mojom.h"
rockot734fb662016-10-15 16:41:30159#include "services/service_manager/public/cpp/interface_provider.h"
Becca Hughes6daf5662018-06-27 16:50:54160#include "third_party/blink/public/common/associated_interfaces/associated_interface_provider.h"
Blink Reformata30d4232018-04-07 15:31:06161#include "third_party/blink/public/common/mime_util/mime_util.h"
danakj938b37a62019-09-24 18:35:54162#include "third_party/blink/public/common/page/page_zoom.h"
Sergio Villar Senin9d9d4f52019-10-15 10:57:57163#include "third_party/blink/public/common/security/security_style.h"
Abhijeet Kandalkarde7348e2020-01-13 06:06:54164#include "third_party/blink/public/mojom/frame/frame.mojom.h"
Dave Tapuskaa4189512019-10-15 20:27:34165#include "third_party/blink/public/mojom/frame/fullscreen.mojom.h"
Takuto Ikutaaa3b796c2019-02-06 02:54:56166#include "third_party/blink/public/mojom/loader/pause_subresource_loading_handle.mojom.h"
Antonio Gomes0d42960a2019-06-05 12:35:51167#include "third_party/blink/public/mojom/mediastream/media_stream.mojom-shared.h"
halton.huoca2eabd2015-07-06 08:17:40168#include "third_party/skia/include/core/SkBitmap.h"
dmazzonid95ae842016-04-12 21:17:39169#include "ui/accessibility/ax_tree_combiner.h"
Rakina Zata Amni4029b6d2020-07-28 02:36:20170#include "ui/base/device_form_factor.h"
Mugdha Lakhani0a0d7862020-07-29 09:58:45171#if defined(USE_AURA)
172#include "ui/aura/window.h"
173#endif
[email protected]a08412b62012-05-29 21:28:56174#include "ui/base/layout.h"
Rakina Zata Amni4029b6d2020-07-28 02:36:20175#include "ui/base/ui_base_features.h"
176#include "ui/base/ui_base_switches.h"
177#include "ui/display/screen.h"
Daniel Cheng90196c82018-04-25 21:49:14178#include "ui/events/base_event_utils.h"
chongz4a9751942016-08-17 17:49:39179#include "ui/events/blink/web_input_event_traits.h"
Rakina Zata Amni4029b6d2020-07-28 02:36:20180#include "ui/gfx/animation/animation.h"
[email protected]66bd5512012-08-01 02:02:52181#include "ui/gl/gl_switches.h"
[email protected]3c733bde2010-12-21 19:56:31182
ekaramada110f642016-12-21 19:47:28183#if defined(OS_WIN)
184#include "content/browser/renderer_host/dip_util.h"
185#include "ui/gfx/geometry/dip_util.h"
186#endif
187
[email protected]583418cc2013-01-17 14:01:10188#if defined(OS_ANDROID)
mfomitchev2b8b066a2016-01-28 19:23:15189#include "content/browser/android/date_time_chooser_android.h"
sammcf5f1b0f2016-09-20 23:05:11190#include "content/browser/android/java_interfaces_impl.h"
Leon Hanccb985f2019-08-26 02:43:24191#include "content/browser/android/nfc_host.h"
[email protected]155c7f22013-12-09 17:07:18192#include "content/browser/web_contents/web_contents_android.h"
Ke He31d0bb02018-02-24 07:16:24193#include "services/device/public/mojom/nfc.mojom.h"
Mugdha Lakhani0a0d7862020-07-29 09:58:45194#include "ui/android/view_android.h"
mcnee336ea2c2017-05-23 22:50:59195#else // !OS_ANDROID
196#include "content/browser/host_zoom_map_impl.h"
mfomitchev2b8b066a2016-01-28 19:23:15197#endif // OS_ANDROID
[email protected]583418cc2013-01-17 14:01:10198
brettw4b461082016-11-19 18:55:16199#if BUILDFLAG(ENABLE_PLUGINS)
zqzhang181047e62016-07-01 13:37:17200#include "content/browser/media/session/pepper_playback_observer.h"
Lei Zhangebcc6302018-01-12 19:46:45201#endif
zqzhang181047e62016-07-01 13:37:17202
[email protected]8ff00d72012-10-23 19:12:21203namespace content {
Abigail Kleinaa898742019-11-01 02:31:25204
205using AccessibilityEventCallback =
206 base::RepeatingCallback<void(const std::string&)>;
207
[email protected]420ae012009-04-24 05:16:32208namespace {
209
[email protected]960b0372014-05-19 18:01:00210const int kMinimumDelayBetweenLoadingUpdatesMS = 100;
[email protected]82114f52012-03-20 22:53:41211const char kDotGoogleDotCom[] = ".google.com";
[email protected]ca406032011-07-19 21:53:05212
Sreeja Kamishettybe0ccd6b2020-06-09 11:52:50213using LifecycleState = RenderFrameHostImpl::LifecycleState;
214
Alexander Timin33bd754c02020-03-10 22:50:18215// TODO(crbug.com/1059903): Clean up after the initial investigation.
216constexpr base::Feature kCheckWebContentsAccessFromNonCurrentFrame{
217 "CheckWebContentsAccessFromNonCurrentFrame",
218 base::FEATURE_DISABLED_BY_DEFAULT};
219
Avi Drissmana9114822018-03-23 18:25:55220base::LazyInstance<std::vector<
221 WebContentsImpl::FriendWrapper::CreatedCallback>>::DestructorAtExit
222 g_created_callbacks = LAZY_INSTANCE_INITIALIZER;
[email protected]cbb1ef592013-06-05 19:49:46223
dcheng57e39e22016-01-21 00:25:38224bool HasMatchingProcess(FrameTree* tree, int render_process_id) {
225 for (FrameTreeNode* node : tree->Nodes()) {
226 if (node->current_frame_host()->GetProcess()->GetID() == render_process_id)
227 return true;
alexmos4cf2aa32015-07-15 23:40:43228 }
dcheng57e39e22016-01-21 00:25:38229 return false;
[email protected]95640212014-07-26 18:14:30230}
231
kenrb19221852016-04-29 17:21:40232bool HasMatchingWidgetHost(FrameTree* tree, RenderWidgetHost* host) {
233 // This method scans the frame tree rather than checking whether
234 // host->delegate() == this, which allows it to return false when the host
235 // for a frame that is pending or pending deletion.
236 if (!host)
237 return false;
238
239 for (FrameTreeNode* node : tree->Nodes()) {
240 if (node->current_frame_host()->GetRenderWidgetHost() == host)
241 return true;
242 }
243 return false;
244}
245
dmazzonif8a31042016-12-03 00:52:13246void UpdateAccessibilityModeOnFrame(RenderFrameHost* frame_host) {
247 static_cast<RenderFrameHostImpl*>(frame_host)->UpdateAccessibilityMode();
[email protected]95640212014-07-26 18:14:30248}
249
dtseng0dd3fa12015-07-22 19:00:52250void ResetAccessibility(RenderFrameHost* rfh) {
251 static_cast<RenderFrameHostImpl*>(rfh)->AccessibilityReset();
252}
253
Ian Clelland5cbaaf82017-11-27 22:00:03254RenderFrameHostImpl* FindOpenerRFH(const WebContents::CreateParams& params) {
255 RenderFrameHostImpl* opener_rfh = nullptr;
alexmose201c7cd2015-06-10 17:14:21256 if (params.opener_render_frame_id != MSG_ROUTING_NONE) {
Ian Clelland5cbaaf82017-11-27 22:00:03257 opener_rfh = RenderFrameHostImpl::FromID(params.opener_render_process_id,
258 params.opener_render_frame_id);
alexmose201c7cd2015-06-10 17:14:21259 }
Ian Clelland5cbaaf82017-11-27 22:00:03260 return opener_rfh;
lukasza6f8ac622017-06-06 03:10:20261}
262
Dominick Ng37a15052018-07-17 23:56:35263// Returns |true| if |type| is the kind of user input that should trigger the
264// user interaction observers.
265bool IsUserInteractionInputType(blink::WebInputEvent::Type type) {
266 // Ideally, this list would be based more off of
267 // https://whatwg.org/C/interaction.html#triggered-by-user-activation.
Dave Tapuska347d60a2020-04-21 23:55:47268 return type == blink::WebInputEvent::Type::kMouseDown ||
269 type == blink::WebInputEvent::Type::kGestureScrollBegin ||
270 type == blink::WebInputEvent::Type::kTouchStart ||
271 type == blink::WebInputEvent::Type::kRawKeyDown;
Dominick Ng37a15052018-07-17 23:56:35272}
273
Pavel Feldman3c1842b2017-08-02 05:00:16274// Ensures that OnDialogClosed is only called once.
275class CloseDialogCallbackWrapper
276 : public base::RefCountedThreadSafe<CloseDialogCallbackWrapper> {
277 public:
Avi Drissmane04d3992017-10-05 15:11:36278 using CloseCallback =
279 base::OnceCallback<void(bool, bool, const base::string16&)>;
280
281 explicit CloseDialogCallbackWrapper(CloseCallback callback)
282 : callback_(std::move(callback)) {}
Pavel Feldman3c1842b2017-08-02 05:00:16283
284 void Run(bool dialog_was_suppressed,
285 bool success,
286 const base::string16& user_input) {
Avi Drissmane04d3992017-10-05 15:11:36287 if (callback_.is_null())
Pavel Feldman3c1842b2017-08-02 05:00:16288 return;
Avi Drissmane04d3992017-10-05 15:11:36289 std::move(callback_).Run(dialog_was_suppressed, success, user_input);
Pavel Feldman3c1842b2017-08-02 05:00:16290 }
291
292 private:
293 friend class base::RefCountedThreadSafe<CloseDialogCallbackWrapper>;
294 ~CloseDialogCallbackWrapper() {}
Avi Drissmane04d3992017-10-05 15:11:36295
296 CloseCallback callback_;
Pavel Feldman3c1842b2017-08-02 05:00:16297};
298
Arthur Sonzogni929d29f2018-09-17 14:19:44299bool FrameCompareDepth(RenderFrameHostImpl* a, RenderFrameHostImpl* b) {
300 return a->frame_tree_node()->depth() < b->frame_tree_node()->depth();
301}
302
Raymes Khoury58373df2019-08-06 06:29:20303bool AreValidRegisterProtocolHandlerArguments(const std::string& protocol,
304 const GURL& url,
305 const url::Origin& origin) {
306 ChildProcessSecurityPolicyImpl* policy =
307 ChildProcessSecurityPolicyImpl::GetInstance();
308 if (policy->IsPseudoScheme(protocol))
309 return false;
310
311 url::Origin url_origin = url::Origin::Create(url);
312 if (url_origin.opaque())
313 return false;
314
315 if (!url_origin.IsSameOriginWith(origin))
316 return false;
317
318 return true;
319}
320
yilkal796e89e2019-10-02 23:58:28321void RecordMaxFrameCountUMA(size_t max_frame_count) {
322 UMA_HISTOGRAM_COUNTS_10000("Navigation.MainFrame.MaxFrameCount",
323 max_frame_count);
324}
325
Jeremy Roman03ce13ce2020-05-29 22:16:57326// Returns whether the condition provided applies to any inner contents.
327// This check is not recursive (however, the predicate provided may itself
328// recurse each contents' own inner contents).
329//
330// For example, if this is used to aggregate state from inner contents to outer
331// contents, then that propagation will gather transitive descendants without
332// need for this helper to do so. In fact, in such cases recursing on inner
333// contents here would make that operation quadratic rather than linear.
334template <typename Functor>
335bool AnyInnerWebContents(WebContents* web_contents, const Functor& f) {
336 const auto& inner_contents = web_contents->GetInnerWebContents();
337 return std::any_of(inner_contents.begin(), inner_contents.end(), f);
338}
339
Xuehui Xieb2300ae42020-06-09 20:25:05340std::vector<RenderFrameHost*> GetAllFramesImpl(FrameTree& frame_tree,
341 bool include_pending) {
342 std::vector<RenderFrameHost*> frame_hosts;
343 for (FrameTreeNode* node : frame_tree.Nodes()) {
344 frame_hosts.push_back(node->current_frame_host());
345 if (include_pending) {
346 RenderFrameHostImpl* pending_frame_host =
347 node->render_manager()->speculative_frame_host();
348 if (pending_frame_host)
349 frame_hosts.push_back(pending_frame_host);
350 }
351 }
352 return frame_hosts;
353}
354
355int SendToAllFramesImpl(FrameTree& frame_tree,
356 bool include_pending,
357 IPC::Message* message) {
358 int number_of_messages = 0;
359 std::vector<RenderFrameHost*> frame_hosts =
360 GetAllFramesImpl(frame_tree, include_pending);
361 for (RenderFrameHost* rfh : frame_hosts) {
362 if (!rfh->IsRenderFrameLive())
363 continue;
364
365 ++number_of_messages;
366 IPC::Message* message_copy = new IPC::Message(*message);
367 message_copy->set_routing_id(rfh->GetRoutingID());
368 rfh->Send(message_copy);
369 }
370 delete message;
371 return number_of_messages;
372}
373
Avi Drissman33972942020-06-19 14:16:01374// Returns the set of all WebContentses that are reachable from |web_contents|
375// by applying some combination of WebContents::GetOriginalOpener() and
376// WebContents::GetOuterWebContents(). The |web_contents| parameter will be
377// included in the returned set.
378base::flat_set<WebContentsImpl*> GetAllOpeningWebContents(
379 WebContentsImpl* web_contents) {
380 base::flat_set<WebContentsImpl*> result;
381 base::flat_set<WebContentsImpl*> current;
382
383 current.insert(web_contents);
384
385 while (!current.empty()) {
386 WebContentsImpl* current_contents = *current.begin();
387 current.erase(current.begin());
388 auto insert_result = result.insert(current_contents);
389
390 if (insert_result.second) {
391 RenderFrameHostImpl* opener_rfh = current_contents->GetOriginalOpener();
392 if (opener_rfh) {
393 current.insert(static_cast<WebContentsImpl*>(
394 WebContents::FromRenderFrameHost(opener_rfh)));
395 }
396
397 WebContentsImpl* outer_contents = current_contents->GetOuterWebContents();
398 if (outer_contents)
399 current.insert(outer_contents);
400 }
401 }
402
403 return result;
404}
405
Rakina Zata Amni4029b6d2020-07-28 02:36:20406#if defined(OS_ANDROID)
407float GetDeviceScaleAdjustment(int min_width) {
408 static const float kMinFSM = 1.05f;
409 static const int kWidthForMinFSM = 320;
410 static const float kMaxFSM = 1.3f;
411 static const int kWidthForMaxFSM = 800;
412
413 if (min_width <= kWidthForMinFSM)
414 return kMinFSM;
415 if (min_width >= kWidthForMaxFSM)
416 return kMaxFSM;
417
418 // The font scale multiplier varies linearly between kMinFSM and kMaxFSM.
419 float ratio = static_cast<float>(min_width - kWidthForMinFSM) /
420 (kWidthForMaxFSM - kWidthForMinFSM);
421 return ratio * (kMaxFSM - kMinFSM) + kMinFSM;
422}
423#endif
424
Avi Drissman33972942020-06-19 14:16:01425// Used to attach the "set of fullscreen contents" to a browser context. Storing
426// sets of WebContents on their browser context is done for two reasons. One,
427// related WebContentses must necessarily share a browser context, so this saves
428// lookup time by restricting to one specific browser context. Two, separating
429// by browser context is preemptive paranoia about keeping things separate.
430class FullscreenContentsHolder : public base::SupportsUserData::Data {
431 public:
432 FullscreenContentsHolder() = default;
433 ~FullscreenContentsHolder() override = default;
434
435 FullscreenContentsHolder(const FullscreenContentsHolder&) = delete;
436 FullscreenContentsHolder& operator=(const FullscreenContentsHolder&) = delete;
437
438 base::flat_set<WebContentsImpl*>* set() { return &set_; }
439
440 private:
441 base::flat_set<WebContentsImpl*> set_;
442};
443
444const char kFullscreenContentsSet[] = "fullscreen-contents";
445
446base::flat_set<WebContentsImpl*>* FullscreenContentsSet(
447 BrowserContext* browser_context) {
448 auto* set_holder = static_cast<FullscreenContentsHolder*>(
449 browser_context->GetUserData(kFullscreenContentsSet));
450 if (!set_holder) {
451 auto new_holder = std::make_unique<FullscreenContentsHolder>();
452 set_holder = new_holder.get();
453 browser_context->SetUserData(kFullscreenContentsSet, std::move(new_holder));
454 }
455
456 return set_holder->set();
457}
458
lukasza6f8ac622017-06-06 03:10:20459} // namespace
460
Joel Hockey8c2011b22020-04-30 05:07:19461CreatedWindow::CreatedWindow() = default;
462CreatedWindow::CreatedWindow(std::unique_ptr<WebContentsImpl> contents,
463 GURL target_url)
464 : contents(std::move(contents)), target_url(std::move(target_url)) {}
465CreatedWindow::~CreatedWindow() = default;
466CreatedWindow::CreatedWindow(CreatedWindow&&) = default;
467CreatedWindow& CreatedWindow::operator=(CreatedWindow&&) = default;
468
Erik Chenbb8e738e2018-04-28 14:10:43469std::unique_ptr<WebContents> WebContents::Create(
470 const WebContents::CreateParams& params) {
Albert J. Wong65fe64d2019-09-20 02:48:14471 return WebContentsImpl::Create(params);
472}
473
474std::unique_ptr<WebContentsImpl> WebContentsImpl::Create(
475 const CreateParams& params) {
476 return CreateWithOpener(params, FindOpenerRFH(params));
[email protected]d1198fd2012-08-13 22:50:19477}
478
erikchenade1fef2018-05-02 22:07:57479std::unique_ptr<WebContents> WebContents::CreateWithSessionStorage(
[email protected]54944cde2012-12-09 09:24:59480 const WebContents::CreateParams& params,
[email protected]fdac6ade2013-07-20 01:06:30481 const SessionStorageNamespaceMap& session_storage_namespace_map) {
erikchenade1fef2018-05-02 22:07:57482 std::unique_ptr<WebContentsImpl> new_contents(
483 new WebContentsImpl(params.browser_context));
Ian Clelland5cbaaf82017-11-27 22:00:03484 RenderFrameHostImpl* opener_rfh = FindOpenerRFH(params);
485 FrameTreeNode* opener = nullptr;
486 if (opener_rfh)
487 opener = opener_rfh->frame_tree_node();
488 new_contents->SetOpenerForNewContents(opener, params.opener_suppressed);
[email protected]fdac6ade2013-07-20 01:06:30489
jdoerrie55ec69d2018-10-08 13:34:46490 for (auto it = session_storage_namespace_map.begin();
491 it != session_storage_namespace_map.end(); ++it) {
Fergal Daly09d6c762020-05-29 02:05:18492 new_contents->GetController().SetSessionStorageNamespace(it->first,
493 it->second.get());
[email protected]fdac6ade2013-07-20 01:06:30494 }
495
Sean Toppingbbc4a2bd2019-01-30 02:16:14496 WebContentsImpl* outer_web_contents = nullptr;
Sam McNally69a33772018-06-27 16:54:12497 if (params.guest_delegate) {
498 // This makes |new_contents| act as a guest.
499 // For more info, see comment above class BrowserPluginGuest.
Albert J. Wongc55cc64d2018-10-12 02:50:04500 BrowserPluginGuest::CreateInWebContents(new_contents.get(),
501 params.guest_delegate);
Sean Toppingbbc4a2bd2019-01-30 02:16:14502 outer_web_contents = static_cast<WebContentsImpl*>(
503 params.guest_delegate->GetOwnerWebContents());
Sam McNally69a33772018-06-27 16:54:12504 }
505
[email protected]54944cde2012-12-09 09:24:59506 new_contents->Init(params);
Sean Toppingbbc4a2bd2019-01-30 02:16:14507 if (outer_web_contents)
508 outer_web_contents->InnerWebContentsCreated(new_contents.get());
[email protected]d1198fd2012-08-13 22:50:19509 return new_contents;
[email protected]a81343d232011-12-27 07:39:20510}
[email protected]746d3052012-05-22 15:15:47511
benwells5e7260d2017-03-20 23:36:33512void WebContentsImpl::FriendWrapper::AddCreatedCallbackForTesting(
nickf9acfbe2014-12-23 19:12:37513 const CreatedCallback& callback) {
[email protected]cbb1ef592013-06-05 19:49:46514 g_created_callbacks.Get().push_back(callback);
515}
516
benwells5e7260d2017-03-20 23:36:33517void WebContentsImpl::FriendWrapper::RemoveCreatedCallbackForTesting(
nickf9acfbe2014-12-23 19:12:37518 const CreatedCallback& callback) {
[email protected]cbb1ef592013-06-05 19:49:46519 for (size_t i = 0; i < g_created_callbacks.Get().size(); ++i) {
Robert Liao1db5a352019-06-05 20:57:34520 if (g_created_callbacks.Get().at(i) == callback) {
[email protected]cbb1ef592013-06-05 19:49:46521 g_created_callbacks.Get().erase(g_created_callbacks.Get().begin() + i);
522 return;
523 }
524 }
525}
526
avidf38c952015-10-27 13:45:13527WebContents* WebContents::FromRenderViewHost(RenderViewHost* rvh) {
528 if (!rvh)
529 return nullptr;
[email protected]746d3052012-05-22 15:15:47530 return rvh->GetDelegate()->GetAsWebContents();
531}
532
[email protected]a86c0e962013-12-17 17:10:39533WebContents* WebContents::FromRenderFrameHost(RenderFrameHost* rfh) {
avidf38c952015-10-27 13:45:13534 if (!rfh)
535 return nullptr;
Alexander Timin33bd754c02020-03-10 22:50:18536 if (!rfh->IsCurrent() && base::FeatureList::IsEnabled(
537 kCheckWebContentsAccessFromNonCurrentFrame)) {
538 // TODO(crbug.com/1059903): return nullptr here eventually.
539 base::debug::DumpWithoutCrashing();
540 }
avidf38c952015-10-27 13:45:13541 return static_cast<RenderFrameHostImpl*>(rfh)->delegate()->GetAsWebContents();
[email protected]a86c0e962013-12-17 17:10:39542}
543
ananta4b7467a52016-09-23 01:42:38544WebContents* WebContents::FromFrameTreeNodeId(int frame_tree_node_id) {
545 FrameTreeNode* frame_tree_node =
546 FrameTreeNode::GloballyFindByID(frame_tree_node_id);
anantab8cfa3b2016-10-14 22:43:19547 if (!frame_tree_node)
548 return nullptr;
jam8c4edd02017-05-06 18:50:33549 return WebContentsImpl::FromFrameTreeNode(frame_tree_node);
ananta4b7467a52016-09-23 01:42:38550}
551
leon.han552e9de2017-02-09 14:37:30552void WebContents::SetScreenOrientationDelegate(
553 ScreenOrientationDelegate* delegate) {
554 ScreenOrientationProvider::SetDelegate(delegate);
555}
556
[email protected]7fff43e2013-05-21 20:21:10557// WebContentsImpl::DestructionObserver ----------------------------------------
558
559class WebContentsImpl::DestructionObserver : public WebContentsObserver {
560 public:
561 DestructionObserver(WebContentsImpl* owner, WebContents* watched_contents)
Fergal Daly09d6c762020-05-29 02:05:18562 : WebContentsObserver(watched_contents), owner_(owner) {}
[email protected]7fff43e2013-05-21 20:21:10563
564 // WebContentsObserver:
dchengc2282aa2014-10-21 12:07:58565 void WebContentsDestroyed() override {
[email protected]12a46832014-05-09 13:35:58566 owner_->OnWebContentsDestroyed(
567 static_cast<WebContentsImpl*>(web_contents()));
[email protected]7fff43e2013-05-21 20:21:10568 }
569
570 private:
571 WebContentsImpl* owner_;
572
573 DISALLOW_COPY_AND_ASSIGN(DestructionObserver);
574};
575
Sreeja Kamishettybe0ccd6b2020-06-09 11:52:50576// TODO(sreejakshetty): Make |WebContentsImpl::ColorChooser| per-frame instead
577// of WebContents-owned.
Joel Hockey85b379d2017-12-11 10:42:13578// WebContentsImpl::ColorChooser ----------------------------------------------
Joel Hockey163835a2017-12-20 11:51:57579class WebContentsImpl::ColorChooser : public blink::mojom::ColorChooser {
Joel Hockey85b379d2017-12-11 10:42:13580 public:
581 ColorChooser(content::ColorChooser* chooser,
Julie Jeongeun Kim8e2879e2019-08-06 23:55:20582 mojo::PendingReceiver<blink::mojom::ColorChooser> receiver,
583 mojo::PendingRemote<blink::mojom::ColorChooserClient> client)
Joel Hockey85b379d2017-12-11 10:42:13584 : chooser_(chooser),
Julie Jeongeun Kim8e2879e2019-08-06 23:55:20585 receiver_(this, std::move(receiver)),
Joel Hockey85b379d2017-12-11 10:42:13586 client_(std::move(client)) {
Julie Jeongeun Kim8e2879e2019-08-06 23:55:20587 receiver_.set_disconnect_handler(
Joel Hockey85b379d2017-12-11 10:42:13588 base::BindOnce([](content::ColorChooser* chooser) { chooser->End(); },
589 base::Unretained(chooser)));
590 }
[email protected]8ed16472014-04-11 19:02:48591
Joel Hockey85b379d2017-12-11 10:42:13592 ~ColorChooser() override { chooser_->End(); }
[email protected]8ed16472014-04-11 19:02:48593
Joel Hockey85b379d2017-12-11 10:42:13594 void SetSelectedColor(SkColor color) override {
595 chooser_->SetSelectedColor(color);
596 }
597
598 void DidChooseColorInColorChooser(SkColor color) {
599 client_->DidChooseColor(color);
600 }
601
602 private:
603 // Color chooser that was opened by this tab.
604 std::unique_ptr<content::ColorChooser> chooser_;
605
Julie Jeongeun Kim8e2879e2019-08-06 23:55:20606 // mojo receiver.
607 mojo::Receiver<blink::mojom::ColorChooser> receiver_;
Joel Hockey85b379d2017-12-11 10:42:13608
609 // mojo renderer client.
Julie Jeongeun Kim8e2879e2019-08-06 23:55:20610 mojo::Remote<blink::mojom::ColorChooserClient> client_;
Joel Hockey85b379d2017-12-11 10:42:13611};
nicka0ac8382016-12-15 23:59:23612
lazyboy6ec48b2a2015-06-29 15:18:14613// WebContentsImpl::WebContentsTreeNode ----------------------------------------
lfg601233692017-03-06 22:45:44614WebContentsImpl::WebContentsTreeNode::WebContentsTreeNode(
615 WebContentsImpl* current_web_contents)
616 : current_web_contents_(current_web_contents),
617 outer_web_contents_(nullptr),
lazyboy6ec48b2a2015-06-29 15:18:14618 outer_contents_frame_tree_node_id_(
avallee0206f782016-07-28 18:55:33619 FrameTreeNode::kFrameTreeNodeInvalidId),
lfg601233692017-03-06 22:45:44620 focused_web_contents_(current_web_contents) {}
lazyboy6ec48b2a2015-06-29 15:18:14621
Lucas Furukawa Gadani3d38ec152018-10-26 20:55:18622WebContentsImpl::WebContentsTreeNode::~WebContentsTreeNode() {}
lazyboy6ec48b2a2015-06-29 15:18:14623
Lucas Furukawa Gadani99125822019-01-03 15:41:49624std::unique_ptr<WebContents>
625WebContentsImpl::WebContentsTreeNode::DisconnectFromOuterWebContents() {
626 std::unique_ptr<WebContents> inner_contents =
627 outer_web_contents_->node_.DetachInnerWebContents(current_web_contents_);
628 OuterContentsFrameTreeNode()->RemoveObserver(this);
629 outer_contents_frame_tree_node_id_ = FrameTreeNode::kFrameTreeNodeInvalidId;
630 outer_web_contents_ = nullptr;
631 return inner_contents;
632}
633
paulmeyerfeafc2d2017-04-25 21:46:40634void WebContentsImpl::WebContentsTreeNode::AttachInnerWebContents(
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:50635 std::unique_ptr<WebContents> inner_web_contents,
636 RenderFrameHostImpl* render_frame_host) {
637 WebContentsImpl* inner_web_contents_impl =
638 static_cast<WebContentsImpl*>(inner_web_contents.get());
639 WebContentsTreeNode& inner_web_contents_node = inner_web_contents_impl->node_;
640
641 inner_web_contents_node.focused_web_contents_ = nullptr;
642 inner_web_contents_node.outer_web_contents_ = current_web_contents_;
643 inner_web_contents_node.outer_contents_frame_tree_node_id_ =
644 render_frame_host->frame_tree_node()->frame_tree_node_id();
645
Lucas Furukawa Gadani3d38ec152018-10-26 20:55:18646 inner_web_contents_.push_back(std::move(inner_web_contents));
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:50647
648 render_frame_host->frame_tree_node()->AddObserver(&inner_web_contents_node);
Jeremy Roman03ce13ce2020-05-29 22:16:57649 current_web_contents_->InnerWebContentsAttached(inner_web_contents_impl);
paulmeyerfeafc2d2017-04-25 21:46:40650}
651
Lucas Furukawa Gadani3d38ec152018-10-26 20:55:18652std::unique_ptr<WebContents>
653WebContentsImpl::WebContentsTreeNode::DetachInnerWebContents(
paulmeyerfeafc2d2017-04-25 21:46:40654 WebContentsImpl* inner_web_contents) {
Lucas Furukawa Gadani3d38ec152018-10-26 20:55:18655 std::unique_ptr<WebContents> detached_contents;
656 for (std::unique_ptr<WebContents>& web_contents : inner_web_contents_) {
657 if (web_contents.get() == inner_web_contents) {
658 detached_contents = std::move(web_contents);
659 std::swap(web_contents, inner_web_contents_.back());
660 inner_web_contents_.pop_back();
Jeremy Roman03ce13ce2020-05-29 22:16:57661 current_web_contents_->InnerWebContentsDetached(inner_web_contents);
Lucas Furukawa Gadani3d38ec152018-10-26 20:55:18662 return detached_contents;
663 }
664 }
665
666 NOTREACHED();
667 return nullptr;
paulmeyerfeafc2d2017-04-25 21:46:40668}
669
lfga3e2bdc2017-03-07 20:44:01670FrameTreeNode*
671WebContentsImpl::WebContentsTreeNode::OuterContentsFrameTreeNode() const {
672 return FrameTreeNode::GloballyFindByID(outer_contents_frame_tree_node_id_);
lfg601233692017-03-06 22:45:44673}
674
lfga3e2bdc2017-03-07 20:44:01675void WebContentsImpl::WebContentsTreeNode::OnFrameTreeNodeDestroyed(
676 FrameTreeNode* node) {
677 DCHECK_EQ(outer_contents_frame_tree_node_id_, node->frame_tree_node_id())
678 << "WebContentsTreeNode should only receive notifications for the "
679 "FrameTreeNode in its outer WebContents that hosts it.";
Lucas Furukawa Gadani3d38ec152018-10-26 20:55:18680
681 // Deletes |this| too.
682 outer_web_contents_->node_.DetachInnerWebContents(current_web_contents_);
lazyboy6ec48b2a2015-06-29 15:18:14683}
684
avallee0206f782016-07-28 18:55:33685void WebContentsImpl::WebContentsTreeNode::SetFocusedWebContents(
686 WebContentsImpl* web_contents) {
687 DCHECK(!outer_web_contents())
688 << "Only the outermost WebContents tracks focus.";
689 focused_web_contents_ = web_contents;
690}
691
paulmeyerfeafc2d2017-04-25 21:46:40692WebContentsImpl*
693WebContentsImpl::WebContentsTreeNode::GetInnerWebContentsInFrame(
694 const FrameTreeNode* frame) {
695 auto ftn_id = frame->frame_tree_node_id();
Lucas Furukawa Gadani3d38ec152018-10-26 20:55:18696 for (auto& contents : inner_web_contents_) {
697 WebContentsImpl* impl = static_cast<WebContentsImpl*>(contents.get());
698 if (impl->node_.outer_contents_frame_tree_node_id() == ftn_id) {
699 return impl;
paulmeyerfeafc2d2017-04-25 21:46:40700 }
701 }
702 return nullptr;
703}
704
Lucas Furukawa Gadani3d38ec152018-10-26 20:55:18705std::vector<WebContentsImpl*>
706WebContentsImpl::WebContentsTreeNode::GetInnerWebContents() const {
707 std::vector<WebContentsImpl*> inner_web_contents;
708 for (auto& contents : inner_web_contents_)
709 inner_web_contents.push_back(static_cast<WebContentsImpl*>(contents.get()));
710
711 return inner_web_contents;
paulmeyerfeafc2d2017-04-25 21:46:40712}
713
[email protected]b172aee2012-04-10 17:05:26714// WebContentsImpl -------------------------------------------------------------
[email protected]420ae012009-04-24 05:16:32715
alexmose201c7cd2015-06-10 17:14:21716WebContentsImpl::WebContentsImpl(BrowserContext* browser_context)
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28717 : delegate_(nullptr),
[email protected]69e797f2013-04-30 01:10:22718 controller_(this, browser_context),
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28719 render_view_host_delegate_view_(nullptr),
alexmose201c7cd2015-06-10 17:14:21720 created_with_opener_(false),
Lucas Furukawa Gadani99125822019-01-03 15:41:49721 node_(this),
Fergal Daly09d6c762020-05-29 02:05:18722 frame_tree_(&controller_, this, this, this, this, this),
[email protected]6dfed692014-05-22 04:18:03723 is_load_to_different_document_(false),
[email protected]443b80e2010-12-14 00:42:23724 crashed_status_(base::TERMINATION_STATUS_STILL_RUNNING),
725 crashed_error_code_(0),
[email protected]d5f942ba2008-09-26 19:30:34726 waiting_for_response_(false),
[email protected]fcf75d42013-12-03 20:11:26727 load_state_(net::LOAD_STATE_IDLE, base::string16()),
[email protected]094e5b22009-09-25 04:23:56728 upload_size_(0),
729 upload_position_(0),
dfalcantarae6b7b46752015-07-10 18:14:16730 is_resume_pending_(false),
[email protected]b4c84012014-04-28 19:51:10731 has_accessed_initial_document_(false),
Collin Bakerc5259962019-11-14 17:51:58732 visible_capturer_count_(0),
733 hidden_capturer_count_(0),
[email protected]fdd61c62009-04-22 19:22:57734 is_being_destroyed_(false),
creis97867aa2017-02-17 18:39:45735 is_notifying_observers_(false),
[email protected]fdd61c62009-04-22 19:22:57736 notify_disconnection_(false),
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28737 dialog_manager_(nullptr),
[email protected]7ab1e7d62009-10-14 23:32:01738 is_showing_before_unload_dialog_(false),
gab0dccfef2015-05-20 18:43:39739 last_active_time_(base::TimeTicks::Now()),
[email protected]ebf40a72010-07-22 01:46:38740 closed_by_user_gesture_(false),
danakj938b37a62019-09-24 18:35:54741 minimum_zoom_percent_(
742 static_cast<int>(blink::kMinimumPageZoomFactor * 100)),
743 maximum_zoom_percent_(
744 static_cast<int>(blink::kMaximumPageZoomFactor * 100)),
w.shackleton49bcd392016-01-06 17:38:22745 zoom_scroll_remainder_(0),
alexmosc9e76ec2016-05-16 22:59:37746 fullscreen_widget_process_id_(ChildProcessHost::kInvalidUniqueID),
[email protected]5dcaf8e2013-12-28 01:31:42747 fullscreen_widget_routing_id_(MSG_ROUTING_NONE),
[email protected]0b431992014-06-24 00:08:03748 fullscreen_widget_had_focus_at_shutdown_(false),
[email protected]34ff1cfc2014-08-20 06:16:05749 force_disable_overscroll_content_(false),
[email protected]95640212014-07-26 18:14:30750 last_dialog_suppressed_(false),
waffles1c12bf402016-02-10 17:45:48751 accessibility_mode_(
Dominic Mazzoni21fb0282019-02-13 18:32:47752 GetContentClient()->browser()->GetAXModeForBrowserContext(
753 browser_context)),
avayvodcc85bbd2015-08-28 19:11:15754 audio_stream_monitor_(this),
Thomas Guilbert882c4cec2019-02-14 05:55:45755 media_web_contents_observer_(
756 std::make_unique<MediaWebContentsObserver>(this)),
mcnee336ea2c2017-05-23 22:50:59757#if !defined(OS_ANDROID)
mcnee432e47d2015-11-09 19:37:46758 page_scale_factor_is_one_(true),
mcnee336ea2c2017-05-23 22:50:59759#endif // !defined(OS_ANDROID)
qinmin72e8bd02016-10-21 19:35:37760 is_overlay_content_(false),
ekaramadf6750aa2017-06-06 18:29:42761 showing_context_menu_(false),
W. James MacLean62198672019-06-04 16:46:10762 text_autosizer_page_info_({0, 0, 1.f}),
shivanigithubcceeacf2020-03-06 20:00:27763 native_theme_observer_(this) {
danakjf4b9e942019-11-29 15:43:04764 frame_tree_.SetFrameRemoveListener(base::BindRepeating(
765 &WebContentsImpl::OnFrameRemoved, base::Unretained(this)));
brettw4b461082016-11-19 18:55:16766#if BUILDFLAG(ENABLE_PLUGINS)
zqzhang181047e62016-07-01 13:37:17767 pepper_playback_observer_.reset(new PepperPlaybackObserver(this));
768#endif
blundelle75a8f92017-03-27 08:11:17769
Becca Hughes86334622018-07-09 21:29:38770#if defined(OS_ANDROID)
Elly Fong-Jonescdd7ae82019-02-20 23:26:33771 display_cutout_host_impl_ = std::make_unique<DisplayCutoutHostImpl>(this);
Becca Hughes86334622018-07-09 21:29:38772#endif
John Delaney732721e92020-02-07 23:11:27773
Alison Maher7f366dc62020-03-03 19:46:00774 ui::NativeTheme* native_theme = ui::NativeTheme::GetInstanceForWeb();
775 native_theme_observer_.Add(native_theme);
776 using_dark_colors_ = native_theme->ShouldUseDarkColors();
777 preferred_color_scheme_ = native_theme->GetPreferredColorScheme();
778
Mike Wassermanb213b9352020-04-16 00:32:34779 screen_change_monitor_ =
780 std::make_unique<ScreenChangeMonitor>(base::BindRepeating(
781 &WebContentsImpl::OnScreensChange, base::Unretained(this)));
782
John Delaney732721e92020-02-07 23:11:27783 // ConversionHost takes a weak ref on |this|, so it must be created outside of
784 // the initializer list.
Charlie Harrison405f6212020-04-14 21:57:32785 if (base::FeatureList::IsEnabled(features::kConversionMeasurement)) {
786 conversion_host_ = std::make_unique<ConversionHost>(this);
787 }
[email protected]332af7732009-03-11 13:21:35788}
initial.commit09911bf2008-07-26 23:55:29789
[email protected]b172aee2012-04-10 17:05:26790WebContentsImpl::~WebContentsImpl() {
creisba91ba82017-05-12 22:56:19791 // Imperfect sanity check against double free, given some crashes unexpectedly
792 // observed in the wild.
793 CHECK(!is_being_destroyed_);
794
795 // We generally keep track of is_being_destroyed_ to let other features know
796 // to avoid certain actions during destruction.
[email protected]420ae012009-04-24 05:16:32797 is_being_destroyed_ = true;
798
creis97867aa2017-02-17 18:39:45799 // A WebContents should never be deleted while it is notifying observers,
creisba91ba82017-05-12 22:56:19800 // since this will lead to a use-after-free as it continues to notify later
creis97867aa2017-02-17 18:39:45801 // observers.
802 CHECK(!is_notifying_observers_);
803
Avi Drissman33972942020-06-19 14:16:01804 FullscreenContentsSet(GetBrowserContext())->erase(this);
805
kenrb2a565f82015-09-02 20:24:59806 rwh_input_event_router_.reset();
807
Julie Jeongeun Kim7d0986172019-12-04 03:17:25808 for (auto& entry : receiver_sets_)
809 entry.second->CloseAllReceivers();
rockotf62002a2016-09-15 00:08:59810
avallee0206f782016-07-28 18:55:33811 WebContentsImpl* outermost = GetOutermostWebContents();
avallee9993fca2016-11-17 06:16:50812 if (this != outermost && ContainsOrIsFocusedWebContents()) {
avallee0206f782016-07-28 18:55:33813 // If the current WebContents is in focus, unset it.
avallee9993fca2016-11-17 06:16:50814 outermost->SetAsFocusedWebContentsIfNecessary();
avallee0206f782016-07-28 18:55:33815 }
816
Nick Carterd73635b2018-03-13 18:31:41817 if (mouse_lock_widget_)
James Hollyerd5c9de462020-03-10 19:02:45818 mouse_lock_widget_->RejectMouseLockOrUnlockIfNecessary(
819 blink::mojom::PointerLockResult::kElementDestroyed);
Nick Carterd73635b2018-03-13 18:31:41820
dcheng72be33c2016-01-21 04:11:31821 for (FrameTreeNode* node : frame_tree_.Nodes()) {
822 // Delete all RFHs pending shutdown, which will lead the corresponding RVHs
823 // to be shutdown and be deleted as well.
dcheng57e39e22016-01-21 00:25:38824 node->render_manager()->ClearRFHsPendingShutdown();
dcheng57e39e22016-01-21 00:25:38825 node->render_manager()->ClearWebUIInstances();
dcheng72be33c2016-01-21 04:11:31826 }
carlosk35f35af2015-12-01 10:55:40827
dcheng57e39e22016-01-21 00:25:38828 for (RenderWidgetHostImpl* widget : created_widgets_)
829 widget->DetachDelegate();
[email protected]b24b68a2012-09-24 21:57:26830 created_widgets_.clear();
831
[email protected]3ab9cb82011-06-03 18:02:07832 // Clear out any JavaScript state.
avi5d3b8692016-10-12 22:00:46833 if (dialog_manager_) {
avi6879fcf2017-01-21 05:27:53834 dialog_manager_->CancelDialogs(this, /*reset_state=*/true);
avi5d3b8692016-10-12 22:00:46835 }
[email protected]3ab9cb82011-06-03 18:02:07836
Joel Hockey85b379d2017-12-11 10:42:13837 color_chooser_.reset();
Rakina Zata Amni16196122018-11-14 02:14:00838 find_request_manager_.reset();
[email protected]da8543762012-03-20 08:52:20839
[email protected]420ae012009-04-24 05:16:32840 NotifyDisconnected();
[email protected]420ae012009-04-24 05:16:32841
[email protected]ca13a442012-04-17 14:00:12842 // Notify any observer that have a reference on this WebContents.
[email protected]8ff00d72012-10-23 19:12:21843 NotificationService::current()->Notify(
844 NOTIFICATION_WEB_CONTENTS_DESTROYED,
845 Source<WebContents>(this),
846 NotificationService::NoDetails());
[email protected]420ae012009-04-24 05:16:32847
Arthur Sonzogni5a9a88c2018-10-18 13:16:00848 // Destroy all subframes now. This notifies observers.
849 GetMainFrame()->ResetChildren();
[email protected]df0c843f2014-05-23 20:37:23850 GetRenderManager()->ResetProxyHosts();
[email protected]c06c58c2014-03-12 20:31:59851
clamy0e119882015-07-31 16:12:33852 // Manually call the observer methods for the root frame tree node. It is
853 // necessary to manually delete all objects tracking navigations
854 // (NavigationHandle, NavigationRequest) for observers to be properly
855 // notified of these navigations stopping before the WebContents is
856 // destroyed.
[email protected]df0c843f2014-05-23 20:37:23857 RenderFrameHostManager* root = GetRenderManager();
naskob21fe4872015-02-24 14:15:56858
Lowell Manners20250112019-10-18 15:06:15859 GetController().GetBackForwardCache().Shutdown();
860
naskob21fe4872015-02-24 14:15:56861 root->current_frame_host()->SetRenderFrameCreated(false);
clamyfc3850d2018-02-23 14:35:28862 root->current_frame_host()->ResetNavigationRequests();
clamy0e119882015-07-31 16:12:33863
Arthur Hemeryc3380172018-01-22 14:00:17864 // Do not update state as the WebContents is being destroyed.
Arthur Hemery241b9392019-10-24 11:08:41865 frame_tree_.root()->ResetNavigationRequest(true);
Arthur Hemeryc3380172018-01-22 14:00:17866 if (root->speculative_frame_host()) {
danakjf2342432019-05-15 21:46:09867 root->speculative_frame_host()->DeleteRenderFrame(
868 FrameDeleteIntention::kSpeculativeMainFrameForShutdown);
Arthur Hemeryc3380172018-01-22 14:00:17869 root->speculative_frame_host()->SetRenderFrameCreated(false);
clamyfc3850d2018-02-23 14:35:28870 root->speculative_frame_host()->ResetNavigationRequests();
clamy0e119882015-07-31 16:12:33871 }
[email protected]f273ee52013-10-18 16:05:27872
brettw4b461082016-11-19 18:55:16873#if BUILDFLAG(ENABLE_PLUGINS)
zqzhang181047e62016-07-01 13:37:17874 // Call this before WebContentsDestroyed() is broadcasted since
875 // AudioFocusManager will be destroyed after that.
876 pepper_playback_observer_.reset();
877#endif // defined(ENABLED_PLUGINS)
878
Chris Hamiltondf0d72cd2018-05-29 16:23:53879 // If audio is playing then notify external observers of the audio stream
880 // disappearing.
881 if (is_currently_audible_) {
882 is_currently_audible_ = false;
883 for (auto& observer : observers_)
884 observer.OnAudioStateChanged(false);
885
886 if (GetOuterWebContents())
887 GetOuterWebContents()->OnAudioStateChanged();
888 }
889
ericwilligersde386342016-10-19 02:35:09890 for (auto& observer : observers_)
891 observer.FrameDeleted(root->current_frame_host());
naskoa83483fb2015-02-27 16:57:10892
ericwilligersde386342016-10-19 02:35:09893 for (auto& observer : observers_)
894 observer.RenderViewDeleted(root->current_host());
[email protected]f273ee52013-10-18 16:05:27895
Kevin McNee576bb5d82019-05-29 16:47:51896#if defined(OS_ANDROID)
897 // For simplicity, destroy the Java WebContents before we notify of the
898 // destruction of the WebContents.
899 ClearWebContentsAndroid();
900#endif
901
ericwilligersde386342016-10-19 02:35:09902 for (auto& observer : observers_)
903 observer.WebContentsDestroyed();
[email protected]12a46832014-05-09 13:35:58904
Becca Hughesd11d6502018-07-31 17:01:28905 if (display_cutout_host_impl_)
906 display_cutout_host_impl_->WebContentsDestroyed();
907
ericwilligersde386342016-10-19 02:35:09908 for (auto& observer : observers_)
909 observer.ResetWebContents();
[email protected]232a5812011-03-04 22:42:08910
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28911 SetDelegate(nullptr);
[email protected]b5a1d11c2011-02-17 03:09:42912}
913
erikchen50735fd2018-05-05 15:08:33914std::unique_ptr<WebContentsImpl> WebContentsImpl::CreateWithOpener(
[email protected]54944cde2012-12-09 09:24:59915 const WebContents::CreateParams& params,
Ian Clelland5cbaaf82017-11-27 22:00:03916 RenderFrameHostImpl* opener_rfh) {
[email protected]e11f0e92013-06-12 15:12:03917 TRACE_EVENT0("browser", "WebContentsImpl::CreateWithOpener");
Ian Clelland5cbaaf82017-11-27 22:00:03918 FrameTreeNode* opener = nullptr;
919 if (opener_rfh)
920 opener = opener_rfh->frame_tree_node();
erikchen50735fd2018-05-05 15:08:33921 std::unique_ptr<WebContentsImpl> new_contents(
922 new WebContentsImpl(params.browser_context));
lukasza6f8ac622017-06-06 03:10:20923 new_contents->SetOpenerForNewContents(opener, params.opener_suppressed);
alexmose201c7cd2015-06-10 17:14:21924
alexmosaedfc6f2016-01-21 23:57:38925 // If the opener is sandboxed, a new popup must inherit the opener's sandbox
926 // flags, and these flags take effect immediately. An exception is if the
927 // opener's sandbox flags lack the PropagatesToAuxiliaryBrowsingContexts
928 // bit (which is controlled by the "allow-popups-to-escape-sandbox" token).
929 // See https://html.spec.whatwg.org/#attr-iframe-sandbox.
lukasza6f8ac622017-06-06 03:10:20930 FrameTreeNode* new_root = new_contents->GetFrameTree()->root();
alexmosaedfc6f2016-01-21 23:57:38931 if (opener) {
arthursonzognib93a4472020-04-10 07:38:00932 network::mojom::WebSandboxFlags opener_flags =
Antonio Gomes7096ac02020-02-14 14:29:54933 opener_rfh->active_sandbox_flags();
arthursonzognib93a4472020-04-10 07:38:00934 const network::mojom::WebSandboxFlags inherit_flag =
935 network::mojom::WebSandboxFlags::kPropagatesToAuxiliaryBrowsingContexts;
Ehsan Karamad39407082019-02-19 23:38:19936 bool sandbox_propagates_to_auxilary_context =
937 (opener_flags & inherit_flag) == inherit_flag;
938 if (sandbox_propagates_to_auxilary_context)
Charlie Hue1b77ac2019-12-13 21:30:17939 new_root->SetPendingFramePolicy({opener_flags,
940 {} /* container_policy */,
941 {} /* required_document_policy */});
arthursonzognib93a4472020-04-10 07:38:00942 if (opener_flags == network::mojom::WebSandboxFlags::kNone ||
Ehsan Karamad39407082019-02-19 23:38:19943 sandbox_propagates_to_auxilary_context) {
944 // TODO(ekaramad, iclelland): Do not propagate feature policies from non-
945 // sandboxed disowned openers (rel=noopener).
946 // If the current page is not sandboxed, or if the sandbox is to propagate
947 // to the popups then opener's feature policy will apply to the new popup
948 // as well.
949 new_root->SetOpenerFeaturePolicyState(
Ehsan Karamad34d10682019-02-26 20:55:04950 opener_rfh->feature_policy()->GetFeatureState());
alexmosaedfc6f2016-01-21 23:57:38951 }
952 }
953
mark a. foltzef394fce2017-10-21 09:11:02954 // Apply starting sandbox flags.
Luna Luc3fdacdf2017-11-08 04:48:53955 blink::FramePolicy frame_policy(new_root->pending_frame_policy());
mark a. foltzef394fce2017-10-21 09:11:02956 frame_policy.sandbox_flags |= params.starting_sandbox_flags;
957 new_root->SetPendingFramePolicy(frame_policy);
Charlie Hu5ffc0152019-12-06 15:59:53958 new_root->CommitFramePolicy(frame_policy);
mark a. foltzef394fce2017-10-21 09:11:02959
alexmose201c7cd2015-06-10 17:14:21960 // This may be true even when opener is null, such as when opening blocked
961 // popups.
alexmos090fae8e2015-05-28 17:09:28962 if (params.created_with_opener)
963 new_contents->created_with_opener_ = true;
964
Sean Toppingbbc4a2bd2019-01-30 02:16:14965 WebContentsImpl* outer_web_contents = nullptr;
[email protected]4858e432014-06-23 18:14:17966 if (params.guest_delegate) {
[email protected]83100cd2014-05-10 11:50:06967 // This makes |new_contents| act as a guest.
968 // For more info, see comment above class BrowserPluginGuest.
Albert J. Wongc55cc64d2018-10-12 02:50:04969 BrowserPluginGuest::CreateInWebContents(new_contents.get(),
970 params.guest_delegate);
Sean Toppingbbc4a2bd2019-01-30 02:16:14971 outer_web_contents = static_cast<WebContentsImpl*>(
972 params.guest_delegate->GetOwnerWebContents());
[email protected]83100cd2014-05-10 11:50:06973 }
mark a. foltzef394fce2017-10-21 09:11:02974
[email protected]54944cde2012-12-09 09:24:59975 new_contents->Init(params);
Sean Toppingbbc4a2bd2019-01-30 02:16:14976 if (outer_web_contents)
977 outer_web_contents->InnerWebContentsCreated(new_contents.get());
[email protected]d1198fd2012-08-13 22:50:19978 return new_contents;
979}
980
[email protected]95640212014-07-26 18:14:30981// static
982std::vector<WebContentsImpl*> WebContentsImpl::GetAllWebContents() {
983 std::vector<WebContentsImpl*> result;
dcheng59716272016-04-09 05:19:08984 std::unique_ptr<RenderWidgetHostIterator> widgets(
[email protected]95640212014-07-26 18:14:30985 RenderWidgetHostImpl::GetRenderWidgetHosts());
[email protected]95640212014-07-26 18:14:30986 while (RenderWidgetHost* rwh = widgets->GetNextHost()) {
[email protected]95640212014-07-26 18:14:30987 RenderViewHost* rvh = RenderViewHost::From(rwh);
988 if (!rvh)
989 continue;
990 WebContents* web_contents = WebContents::FromRenderViewHost(rvh);
991 if (!web_contents)
992 continue;
avidf38c952015-10-27 13:45:13993 if (web_contents->GetRenderViewHost() != rvh)
994 continue;
995 // Because a WebContents can only have one current RVH at a time, there will
996 // be no duplicate WebContents here.
997 result.push_back(static_cast<WebContentsImpl*>(web_contents));
[email protected]95640212014-07-26 18:14:30998 }
999 return result;
1000}
1001
clamya16aa8172015-05-26 13:07:251002// static
1003WebContentsImpl* WebContentsImpl::FromFrameTreeNode(
paulmeyerfeafc2d2017-04-25 21:46:401004 const FrameTreeNode* frame_tree_node) {
clamya16aa8172015-05-26 13:07:251005 return static_cast<WebContentsImpl*>(
1006 WebContents::FromRenderFrameHost(frame_tree_node->current_frame_host()));
1007}
1008
clamy0d32d6d2015-11-24 11:16:261009// static
Patrick Monette275a6f92b2020-01-09 21:05:041010WebContents* WebContentsImpl::FromRenderFrameHostID(
1011 GlobalFrameRoutingId render_frame_host_id) {
1012 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) ||
1013 !BrowserThread::IsThreadInitialized(BrowserThread::UI));
Patrick Monette7364e6972020-01-09 22:56:021014 RenderFrameHost* render_frame_host =
1015 RenderFrameHost::FromID(render_frame_host_id);
1016 if (!render_frame_host)
1017 return nullptr;
1018
1019 return WebContents::FromRenderFrameHost(render_frame_host);
Patrick Monette275a6f92b2020-01-09 21:05:041020}
1021
1022// static
clamy0d32d6d2015-11-24 11:16:261023WebContents* WebContentsImpl::FromRenderFrameHostID(int render_process_host_id,
1024 int render_frame_host_id) {
Patrick Monette7364e6972020-01-09 22:56:021025 return FromRenderFrameHostID(
1026 GlobalFrameRoutingId(render_process_host_id, render_frame_host_id));
clamy0d32d6d2015-11-24 11:16:261027}
1028
paulmeyerfeafc2d2017-04-25 21:46:401029// static
1030WebContentsImpl* WebContentsImpl::FromOuterFrameTreeNode(
1031 const FrameTreeNode* frame_tree_node) {
1032 return WebContentsImpl::FromFrameTreeNode(frame_tree_node)
1033 ->node_.GetInnerWebContentsInFrame(frame_tree_node);
1034}
1035
[email protected]b0936d22013-11-28 06:47:361036RenderFrameHostManager* WebContentsImpl::GetRenderManagerForTesting() {
[email protected]fa944cb82013-11-15 17:51:211037 return GetRenderManager();
[email protected]765187182012-01-11 23:59:281038}
1039
nicka0ac8382016-12-15 23:59:231040bool WebContentsImpl::OnMessageReceived(RenderViewHostImpl* render_view_host,
[email protected]7bb761892012-07-20 09:32:471041 const IPC::Message& message) {
nicka0ac8382016-12-15 23:59:231042 for (auto& observer : observers_) {
Lukasz Anforowicz89f2c1b2017-08-30 21:36:461043 // TODO(nick, creis): https://crbug.com/758026: Replace all uses of this
1044 // variant of OnMessageReceived with the version that takes a
1045 // RenderFrameHost, and then delete it.
nicka0ac8382016-12-15 23:59:231046 if (observer.OnMessageReceived(message))
1047 return true;
[email protected]64ffefa2014-05-10 12:06:331048 }
[email protected]403415a2011-01-10 18:57:531049
Henrique Ferreiroba535472020-04-21 11:38:361050#if BUILDFLAG(ENABLE_PLUGINS)
[email protected]724159a2010-12-30 01:11:181051 bool handled = true;
nicka0ac8382016-12-15 23:59:231052 IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(WebContentsImpl, message, render_view_host)
nicka0ac8382016-12-15 23:59:231053 IPC_MESSAGE_HANDLER(ViewHostMsg_RequestPpapiBrokerPermission,
1054 OnRequestPpapiBrokerPermission)
nicka0ac8382016-12-15 23:59:231055 IPC_MESSAGE_UNHANDLED(handled = false)
1056 IPC_END_MESSAGE_MAP()
nicka0ac8382016-12-15 23:59:231057 return handled;
Henrique Ferreiroba535472020-04-21 11:38:361058#else
1059 return false;
1060#endif
nicka0ac8382016-12-15 23:59:231061}
1062
1063bool WebContentsImpl::OnMessageReceived(RenderFrameHostImpl* render_frame_host,
1064 const IPC::Message& message) {
1065 for (auto& observer : observers_) {
1066 if (observer.OnMessageReceived(message, render_frame_host))
1067 return true;
1068 }
1069
1070 bool handled = true;
brettw4b461082016-11-19 18:55:161071#if BUILDFLAG(ENABLE_PLUGINS)
Hiroki Nakagawa761b9b02020-07-13 11:12:021072 IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(WebContentsImpl, message, render_frame_host)
emaxxe70f5e12015-05-29 11:26:001073 IPC_MESSAGE_HANDLER(FrameHostMsg_PepperInstanceCreated,
1074 OnPepperInstanceCreated)
1075 IPC_MESSAGE_HANDLER(FrameHostMsg_PepperInstanceDeleted,
1076 OnPepperInstanceDeleted)
tommyclie6633ca72014-10-31 00:40:421077 IPC_MESSAGE_HANDLER(FrameHostMsg_PepperPluginHung, OnPepperPluginHung)
zqzhang181047e62016-07-01 13:37:171078 IPC_MESSAGE_HANDLER(FrameHostMsg_PepperStartsPlayback,
1079 OnPepperStartsPlayback)
1080 IPC_MESSAGE_HANDLER(FrameHostMsg_PepperStopsPlayback,
1081 OnPepperStopsPlayback)
tommyclie6633ca72014-10-31 00:40:421082 IPC_MESSAGE_HANDLER(FrameHostMsg_PluginCrashed, OnPluginCrashed)
[email protected]724159a2010-12-30 01:11:181083 IPC_MESSAGE_UNHANDLED(handled = false)
[email protected]e44d1342014-05-16 21:29:331084 IPC_END_MESSAGE_MAP()
Hiroki Nakagawa761b9b02020-07-13 11:12:021085#else
1086 handled = false;
1087#endif
[email protected]724159a2010-12-30 01:11:181088
[email protected]724159a2010-12-30 01:11:181089 return handled;
1090}
1091
[email protected]d1198fd2012-08-13 22:50:191092NavigationControllerImpl& WebContentsImpl::GetController() {
[email protected]f5fa20e2011-12-21 22:35:561093 return controller_;
1094}
1095
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411096BrowserContext* WebContentsImpl::GetBrowserContext() {
[email protected]a26023822011-12-29 00:23:551097 return controller_.GetBrowserContext();
[email protected]627e0512011-12-21 22:55:301098}
1099
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411100const GURL& WebContentsImpl::GetURL() {
Peter Boström101d7672018-12-15 00:43:591101 return GetVisibleURL();
[email protected]be1f56ab2011-12-22 06:55:311102}
1103
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411104const GURL& WebContentsImpl::GetVisibleURL() {
[email protected]c854a7e2013-05-21 16:42:241105 // We may not have a navigation entry yet.
[email protected]59167c22013-06-03 18:07:321106 NavigationEntry* entry = controller_.GetVisibleEntry();
[email protected]c854a7e2013-05-21 16:42:241107 return entry ? entry->GetVirtualURL() : GURL::EmptyGURL();
1108}
1109
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411110const GURL& WebContentsImpl::GetLastCommittedURL() {
[email protected]c854a7e2013-05-21 16:42:241111 // We may not have a navigation entry yet.
1112 NavigationEntry* entry = controller_.GetLastCommittedEntry();
1113 return entry ? entry->GetVirtualURL() : GURL::EmptyGURL();
1114}
1115
[email protected]8ff00d72012-10-23 19:12:211116WebContentsDelegate* WebContentsImpl::GetDelegate() {
[email protected]be1f56ab2011-12-22 06:55:311117 return delegate_;
1118}
1119
[email protected]8ff00d72012-10-23 19:12:211120void WebContentsImpl::SetDelegate(WebContentsDelegate* delegate) {
[email protected]be1f56ab2011-12-22 06:55:311121 // TODO(cbentzel): remove this debugging code?
1122 if (delegate == delegate_)
1123 return;
1124 if (delegate_)
1125 delegate_->Detach(this);
1126 delegate_ = delegate;
[email protected]a6b73c62013-02-11 23:05:081127 if (delegate_) {
[email protected]be1f56ab2011-12-22 06:55:311128 delegate_->Attach(this);
[email protected]a6b73c62013-02-11 23:05:081129 // Ensure the visible RVH reflects the new delegate's preferences.
[email protected]86f98a22013-03-20 14:35:001130 if (view_)
[email protected]e85165c642014-06-10 14:34:311131 view_->SetOverscrollControllerEnabled(CanOverscrollContent());
Joel Einbinder848001c2017-09-19 21:39:311132 if (GetRenderViewHost())
1133 RenderFrameDevToolsAgentHost::WebContentsCreated(this);
[email protected]a6b73c62013-02-11 23:05:081134 }
[email protected]be1f56ab2011-12-22 06:55:311135}
1136
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411137RenderFrameHostImpl* WebContentsImpl::GetMainFrame() {
[email protected]94d0cc12013-12-18 00:07:411138 return frame_tree_.root()->current_frame_host();
[email protected]60eca4eb2013-12-06 00:02:161139}
1140
nick53d5cbf2015-04-23 22:50:141141RenderFrameHostImpl* WebContentsImpl::GetFocusedFrame() {
1142 FrameTreeNode* focused_node = frame_tree_.GetFocusedFrame();
1143 if (!focused_node)
1144 return nullptr;
1145 return focused_node->current_frame_host();
[email protected]9c9343b2014-03-08 02:56:071146}
1147
rob3e2a0732016-01-06 21:22:091148RenderFrameHostImpl* WebContentsImpl::FindFrameByFrameTreeNodeId(
creisf71a2632017-05-04 19:03:501149 int frame_tree_node_id,
1150 int process_id) {
1151 FrameTreeNode* frame = frame_tree_.FindByID(frame_tree_node_id);
1152
1153 // Sanity check that this is in the caller's expected process. Otherwise a
1154 // recent cross-process navigation may have led to a privilege change that the
1155 // caller is not expecting.
1156 if (!frame ||
1157 frame->current_frame_host()->GetProcess()->GetID() != process_id)
1158 return nullptr;
1159
1160 return frame->current_frame_host();
1161}
1162
1163RenderFrameHostImpl* WebContentsImpl::UnsafeFindFrameByFrameTreeNodeId(
rob3e2a0732016-01-06 21:22:091164 int frame_tree_node_id) {
creisf71a2632017-05-04 19:03:501165 // Beware using this! The RenderFrameHost may have changed since the caller
1166 // obtained frame_tree_node_id.
rob3e2a0732016-01-06 21:22:091167 FrameTreeNode* frame = frame_tree_.FindByID(frame_tree_node_id);
1168 return frame ? frame->current_frame_host() : nullptr;
1169}
1170
[email protected]a86c0e962013-12-17 17:10:391171void WebContentsImpl::ForEachFrame(
Lei Zhangebcc6302018-01-12 19:46:451172 const base::RepeatingCallback<void(RenderFrameHost*)>& on_frame) {
dcheng57e39e22016-01-21 00:25:381173 for (FrameTreeNode* node : frame_tree_.Nodes()) {
1174 on_frame.Run(node->current_frame_host());
1175 }
[email protected]a86c0e962013-12-17 17:10:391176}
1177
dchengafb53e22016-02-04 08:11:081178std::vector<RenderFrameHost*> WebContentsImpl::GetAllFrames() {
Xuehui Xieb2300ae42020-06-09 20:25:051179 return GetAllFramesImpl(frame_tree_, /*include_pending=*/false);
1180}
1181
1182std::vector<RenderFrameHost*> WebContentsImpl::GetAllFramesIncludingPending() {
1183 return GetAllFramesImpl(frame_tree_, /*include_pending=*/true);
dchengafb53e22016-02-04 08:11:081184}
1185
lukaszacbdf52e2016-01-15 21:19:511186int WebContentsImpl::SendToAllFrames(IPC::Message* message) {
Xuehui Xieb2300ae42020-06-09 20:25:051187 return SendToAllFramesImpl(frame_tree_, /*include_pending=*/false, message);
1188}
lukasza1a9ab2422016-03-02 18:47:251189
Xuehui Xieb2300ae42020-06-09 20:25:051190int WebContentsImpl::SendToAllFramesIncludingPending(IPC::Message* message) {
1191 return SendToAllFramesImpl(frame_tree_, /*include_pending=*/true, message);
[email protected]a86c0e962013-12-17 17:10:391192}
1193
lfgdb5c4ed2016-03-04 23:09:071194void WebContentsImpl::SendPageMessage(IPC::Message* msg) {
alexmos136fd6e62016-08-15 20:58:411195 frame_tree_.root()->render_manager()->SendPageMessage(msg, nullptr);
lfgdb5c4ed2016-03-04 23:09:071196}
1197
Gyuyoung Kim877e86d2020-07-21 04:01:021198void WebContentsImpl::ExecutePageBroadcastMethod(
1199 PageBroadcastMethodCallback callback) {
1200 frame_tree_.root()->render_manager()->ExecutePageBroadcastMethod(callback);
1201}
1202
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411203RenderViewHostImpl* WebContentsImpl::GetRenderViewHost() {
[email protected]fa944cb82013-11-15 17:51:211204 return GetRenderManager()->current_host();
[email protected]be1f56ab2011-12-22 06:55:311205}
1206
creis89a0f782015-05-27 16:13:171207void WebContentsImpl::CancelActiveAndPendingDialogs() {
Evan Stade753fc202020-07-13 18:09:541208 if (dialog_manager_) {
avi6879fcf2017-01-21 05:27:531209 dialog_manager_->CancelDialogs(this, /*reset_state=*/false);
Evan Stade753fc202020-07-13 18:09:541210 }
creis89a0f782015-05-27 16:13:171211 if (browser_plugin_embedder_)
1212 browser_plugin_embedder_->CancelGuestDialogs();
1213}
1214
naskoc0fceff2015-04-30 15:53:521215void WebContentsImpl::ClosePage() {
1216 GetRenderViewHost()->ClosePage();
1217}
1218
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411219RenderWidgetHostView* WebContentsImpl::GetRenderWidgetHostView() {
[email protected]fa944cb82013-11-15 17:51:211220 return GetRenderManager()->GetRenderWidgetHostView();
[email protected]be1f56ab2011-12-22 06:55:311221}
1222
lfg265a2672016-04-23 03:11:021223RenderWidgetHostView* WebContentsImpl::GetTopLevelRenderWidgetHostView() {
1224 if (GetOuterWebContents())
1225 return GetOuterWebContents()->GetTopLevelRenderWidgetHostView();
1226 return GetRenderManager()->GetRenderWidgetHostView();
1227}
1228
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411229RenderWidgetHostView* WebContentsImpl::GetFullscreenRenderWidgetHostView() {
vmpstr6d9996c82017-02-23 00:43:251230 if (auto* widget_host = GetFullscreenRenderWidgetHost())
ekaramadaeb3c5c2016-11-09 03:22:351231 return widget_host->GetView();
1232 return nullptr;
[email protected]4aebbca2013-09-17 22:26:491233}
1234
[email protected]8ff00d72012-10-23 19:12:211235WebContentsView* WebContentsImpl::GetView() const {
[email protected]be1f56ab2011-12-22 06:55:311236 return view_.get();
1237}
1238
Mike Wassermanb213b9352020-04-16 00:32:341239void WebContentsImpl::OnScreensChange() {
1240 for (FrameTreeNode* node : frame_tree_.Nodes()) {
1241 RenderFrameHostImpl* rfh = node->current_frame_host();
1242 rfh->GetAssociatedLocalFrame()->OnScreensChange();
1243 }
1244}
1245
leon.han552e9de2017-02-09 14:37:301246void WebContentsImpl::OnScreenOrientationChange() {
1247 DCHECK(screen_orientation_provider_);
Lan Weif81c6e7c2020-02-12 16:46:091248 DidChangeScreenOrientation();
Becca Hughese9e27952018-06-25 17:08:271249 screen_orientation_provider_->OnOrientationChange();
leon.hane4db177a2017-02-07 14:19:161250}
1251
Alan Cutter92d59252019-03-29 02:32:331252base::Optional<SkColor> WebContentsImpl::GetThemeColor() {
Carlos Caballero1215f882019-10-29 15:58:431253 return GetRenderViewHost()->theme_color();
yusufod41c5f92015-03-06 00:14:281254}
1255
Doug Turner63f3c7b2017-07-29 05:10:011256void WebContentsImpl::SetAccessibilityMode(ui::AXMode mode) {
[email protected]95640212014-07-26 18:14:301257 if (mode == accessibility_mode_)
1258 return;
1259
dmazzonif8a31042016-12-03 00:52:131260 // Don't allow accessibility to be enabled for WebContents that are never
danakj77eb7e82020-01-09 19:38:461261 // user-visible, like background pages.
1262 if (IsNeverComposited())
dmazzonif8a31042016-12-03 00:52:131263 return;
1264
[email protected]95640212014-07-26 18:14:301265 accessibility_mode_ = mode;
dcheng57e39e22016-01-21 00:25:381266
1267 for (FrameTreeNode* node : frame_tree_.Nodes()) {
dmazzonif8a31042016-12-03 00:52:131268 UpdateAccessibilityModeOnFrame(node->current_frame_host());
clamy610c63b32017-12-22 15:05:181269 // Also update accessibility mode on the speculative RenderFrameHost for
1270 // this FrameTreeNode, if one exists.
Alex Moshchuk3d8a1f392017-10-24 19:01:261271 RenderFrameHost* speculative_frame_host =
1272 node->render_manager()->speculative_frame_host();
1273 if (speculative_frame_host)
1274 UpdateAccessibilityModeOnFrame(speculative_frame_host);
dcheng57e39e22016-01-21 00:25:381275 }
[email protected]95640212014-07-26 18:14:301276}
1277
Doug Turner63f3c7b2017-07-29 05:10:011278void WebContentsImpl::AddAccessibilityMode(ui::AXMode mode) {
1279 ui::AXMode new_mode(accessibility_mode_);
dougtcd3dad732017-03-14 03:26:231280 new_mode |= mode;
1281 SetAccessibilityMode(new_mode);
[email protected]95640212014-07-26 18:14:301282}
1283
Avi Drissman1e620f32018-03-16 13:57:291284// Helper class used by WebContentsImpl::RequestAXTreeSnapshot.
1285// Handles the callbacks from parallel snapshot requests to each frame,
1286// and feeds the results to an AXTreeCombiner, which converts them into a
1287// single combined accessibility tree.
1288class WebContentsImpl::AXTreeSnapshotCombiner
1289 : public base::RefCounted<AXTreeSnapshotCombiner> {
1290 public:
1291 explicit AXTreeSnapshotCombiner(AXTreeSnapshotCallback callback)
1292 : callback_(std::move(callback)) {}
1293
1294 AXTreeSnapshotCallback AddFrame(bool is_root) {
1295 // Adds a reference to |this|.
1296 return base::BindOnce(&AXTreeSnapshotCombiner::ReceiveSnapshot, this,
1297 is_root);
1298 }
1299
1300 void ReceiveSnapshot(bool is_root, const ui::AXTreeUpdate& snapshot) {
1301 combiner_.AddTree(snapshot, is_root);
1302 }
1303
1304 private:
1305 friend class base::RefCounted<AXTreeSnapshotCombiner>;
1306
1307 // This is called automatically after the last call to ReceiveSnapshot
1308 // when there are no more references to this object.
1309 ~AXTreeSnapshotCombiner() {
1310 combiner_.Combine();
1311 std::move(callback_).Run(combiner_.combined());
1312 }
1313
1314 ui::AXTreeCombiner combiner_;
1315 AXTreeSnapshotCallback callback_;
1316};
1317
1318void WebContentsImpl::RequestAXTreeSnapshot(AXTreeSnapshotCallback callback,
1319 ui::AXMode ax_mode) {
dmazzonid95ae842016-04-12 21:17:391320 // Send a request to each of the frames in parallel. Each one will return
1321 // an accessibility tree snapshot, and AXTreeSnapshotCombiner will combine
1322 // them into a single tree and call |callback| with that result, then
1323 // delete |combiner|.
Avi Drissman1e620f32018-03-16 13:57:291324 FrameTreeNode* root_node = frame_tree_.root();
tzik58faa742018-08-01 10:54:181325 auto combiner =
1326 base::MakeRefCounted<AXTreeSnapshotCombiner>(std::move(callback));
Avi Drissman1e620f32018-03-16 13:57:291327
tzik58faa742018-08-01 10:54:181328 RecursiveRequestAXTreeSnapshotOnFrame(root_node, combiner.get(), ax_mode);
Avi Drissman1e620f32018-03-16 13:57:291329}
1330
1331void WebContentsImpl::RecursiveRequestAXTreeSnapshotOnFrame(
1332 FrameTreeNode* root_node,
1333 AXTreeSnapshotCombiner* combiner,
1334 ui::AXMode ax_mode) {
dmazzonid95ae842016-04-12 21:17:391335 for (FrameTreeNode* frame_tree_node : frame_tree_.Nodes()) {
Avi Drissman1e620f32018-03-16 13:57:291336 WebContentsImpl* inner_contents =
1337 node_.GetInnerWebContentsInFrame(frame_tree_node);
1338 if (inner_contents) {
1339 inner_contents->RecursiveRequestAXTreeSnapshotOnFrame(root_node, combiner,
1340 ax_mode);
1341 } else {
1342 bool is_root = frame_tree_node == root_node;
1343 frame_tree_node->current_frame_host()->RequestAXTreeSnapshot(
1344 combiner->AddFrame(is_root), ax_mode);
1345 }
dmazzonid95ae842016-04-12 21:17:391346 }
dmazzoni83ba5c82015-04-14 07:11:511347}
1348
Becca Hughes3b5a43482018-07-17 22:31:551349void WebContentsImpl::NotifyViewportFitChanged(
1350 blink::mojom::ViewportFit value) {
1351 for (auto& observer : observers_)
1352 observer.ViewportFitChanged(value);
1353}
1354
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411355FindRequestManager* WebContentsImpl::GetFindRequestManagerForTesting() {
Ehsan Karamad6beb2ea2018-11-25 18:15:131356 return GetFindRequestManager();
1357}
1358
mcnee336ea2c2017-05-23 22:50:591359#if !defined(OS_ANDROID)
akabac6bd1212018-06-25 20:10:481360void WebContentsImpl::UpdateZoom() {
1361 RenderWidgetHostImpl* rwh = GetRenderViewHost()->GetWidget();
1362 if (rwh->GetView())
1363 rwh->SynchronizeVisualProperties();
wjmaclean64951902016-04-29 20:59:121364}
1365
wjmaclean64951902016-04-29 20:59:121366
1367void WebContentsImpl::UpdateZoomIfNecessary(const std::string& scheme,
akabac6bd1212018-06-25 20:10:481368 const std::string& host) {
wjmaclean64951902016-04-29 20:59:121369 NavigationEntry* entry = GetController().GetLastCommittedEntry();
1370 if (!entry)
1371 return;
1372
1373 GURL url = HostZoomMap::GetURLFromEntry(entry);
1374 if (host != net::GetHostOrSpecFromURL(url) ||
1375 (!scheme.empty() && !url.SchemeIs(scheme))) {
1376 return;
1377 }
1378
akabac6bd1212018-06-25 20:10:481379 UpdateZoom();
wjmaclean64951902016-04-29 20:59:121380}
mcnee336ea2c2017-05-23 22:50:591381#endif // !defined(OS_ANDROID)
wjmaclean64951902016-04-29 20:59:121382
Julie Jeongeun Kim7d0986172019-12-04 03:17:251383base::OnceClosure WebContentsImpl::AddReceiverSet(
rockotf62002a2016-09-15 00:08:591384 const std::string& interface_name,
Julie Jeongeun Kim7d0986172019-12-04 03:17:251385 WebContentsReceiverSet* receiver_set) {
rockotf62002a2016-09-15 00:08:591386 auto result =
Julie Jeongeun Kim7d0986172019-12-04 03:17:251387 receiver_sets_.insert(std::make_pair(interface_name, receiver_set));
rockotf62002a2016-09-15 00:08:591388 DCHECK(result.second);
Julie Jeongeun Kim7d0986172019-12-04 03:17:251389 return base::BindOnce(&WebContentsImpl::RemoveReceiverSet,
danakjf4b9e942019-11-29 15:43:041390 weak_factory_.GetWeakPtr(), interface_name);
rockotf62002a2016-09-15 00:08:591391}
1392
Julie Jeongeun Kim7d0986172019-12-04 03:17:251393WebContentsReceiverSet* WebContentsImpl::GetReceiverSet(
rockot8bbad222017-03-22 04:34:051394 const std::string& interface_name) {
Julie Jeongeun Kim7d0986172019-12-04 03:17:251395 auto it = receiver_sets_.find(interface_name);
1396 if (it == receiver_sets_.end())
rockot8bbad222017-03-22 04:34:051397 return nullptr;
1398 return it->second;
1399}
1400
John Delaney732721e92020-02-07 23:11:271401void WebContentsImpl::RemoveReceiverSetForTesting(
1402 const std::string& interface_name) {
1403 RemoveReceiverSet(interface_name);
1404}
1405
paulmeyerfeafc2d2017-04-25 21:46:401406std::vector<WebContentsImpl*> WebContentsImpl::GetWebContentsAndAllInner() {
1407 std::vector<WebContentsImpl*> all_contents(1, this);
1408
1409 for (size_t i = 0; i != all_contents.size(); ++i) {
1410 for (auto* inner_contents : all_contents[i]->GetInnerWebContents()) {
Lucas Furukawa Gadani2ec00c82018-12-14 15:53:161411 all_contents.push_back(static_cast<WebContentsImpl*>(inner_contents));
paulmeyerfeafc2d2017-04-25 21:46:401412 }
1413 }
1414
1415 return all_contents;
1416}
1417
Sam McNally145fb172017-05-10 00:13:231418void WebContentsImpl::NotifyManifestUrlChanged(
Yuzu Saijo0c263c702020-06-08 03:41:241419 RenderFrameHost* rfh,
Sam McNally145fb172017-05-10 00:13:231420 const base::Optional<GURL>& manifest_url) {
1421 for (auto& observer : observers_)
Yuzu Saijo0c263c702020-06-08 03:41:241422 observer.DidUpdateWebManifestURL(rfh, manifest_url);
Sam McNally145fb172017-05-10 00:13:231423}
1424
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411425WebUI* WebContentsImpl::GetWebUI() {
Nasko Oskov2f7d2112019-10-29 03:26:571426 WebUI* committed_web_ui = GetCommittedWebUI();
1427 if (committed_web_ui)
1428 return committed_web_ui;
1429
1430 if (GetRenderManager()->speculative_frame_host())
1431 return GetRenderManager()->speculative_frame_host()->web_ui();
1432
1433 return nullptr;
[email protected]be1f56ab2011-12-22 06:55:311434}
1435
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411436WebUI* WebContentsImpl::GetCommittedWebUI() {
carlosk35f35af2015-12-01 10:55:401437 return frame_tree_.root()->current_frame_host()->web_ui();
[email protected]d5f942ba2008-09-26 19:30:341438}
1439
Maks Orlovich73f374d2020-04-02 12:46:131440void WebContentsImpl::SetUserAgentOverride(
1441 const blink::UserAgentOverride& ua_override,
1442 bool override_in_new_tabs) {
1443 DCHECK(!ua_override.ua_metadata_override.has_value() ||
1444 !ua_override.ua_string_override.empty());
1445
1446 if (GetUserAgentOverride() == ua_override)
[email protected]bf70edce2012-06-20 22:32:221447 return;
1448
Changwan Ryuc1134a82018-03-07 02:10:141449 should_override_user_agent_in_new_tabs_ = override_in_new_tabs;
1450
Maks Orlovich73f374d2020-04-02 12:46:131451 renderer_preferences_.user_agent_override = ua_override;
[email protected]bf70edce2012-06-20 22:32:221452
Bruce Long1e3e1f542019-10-16 17:56:281453 // Send the new override string to all renderers in the current page.
1454 SyncRendererPrefs();
[email protected]bf70edce2012-06-20 22:32:221455
1456 // Reload the page if a load is currently in progress to avoid having
1457 // different parts of the page loaded using different user agents.
Scott Violete1d58cc2020-04-28 23:07:171458 // No need to reload if the current entry matches that of the
1459 // NavigationRequest supplied to DidStartNavigation() as NavigationRequest
1460 // handles it.
[email protected]6286a372013-10-09 04:03:271461 NavigationEntry* entry = controller_.GetVisibleEntry();
Scott Violete1d58cc2020-04-28 23:07:171462 if (IsLoading() && entry != nullptr && entry->GetIsOverridingUserAgent() &&
1463 (!frame_tree_.root()->navigation_request() ||
1464 frame_tree_.root()->navigation_request()->ua_change_requires_reload())) {
toyoshim6142d96f2016-12-19 09:07:251465 controller_.Reload(ReloadType::BYPASSING_CACHE, true);
Scott Violete1d58cc2020-04-28 23:07:171466 }
[email protected]8d0f3312012-08-18 01:47:531467
ericwilligersde386342016-10-19 02:35:091468 for (auto& observer : observers_)
Maks Orlovich73f374d2020-04-02 12:46:131469 observer.UserAgentOverrideSet(ua_override);
[email protected]86ef6a392012-05-11 22:03:111470}
1471
Scott Violet2a6c5bff2020-04-29 23:59:061472void WebContentsImpl::SetRendererInitiatedUserAgentOverrideOption(
1473 NavigationController::UserAgentOverrideOption option) {
1474 renderer_initiated_user_agent_override_option_ = option;
1475}
1476
Maks Orlovich73f374d2020-04-02 12:46:131477const blink::UserAgentOverride& WebContentsImpl::GetUserAgentOverride() {
[email protected]bf70edce2012-06-20 22:32:221478 return renderer_preferences_.user_agent_override;
[email protected]86ef6a392012-05-11 22:03:111479}
1480
Scott Violet2a6c5bff2020-04-29 23:59:061481bool WebContentsImpl::ShouldOverrideUserAgentForRendererInitiatedNavigation() {
1482 NavigationEntryImpl* current_entry = controller_.GetLastCommittedEntry();
1483 if (!current_entry)
1484 return should_override_user_agent_in_new_tabs_;
1485
1486 switch (renderer_initiated_user_agent_override_option_) {
1487 case NavigationController::UA_OVERRIDE_INHERIT:
1488 return current_entry->GetIsOverridingUserAgent();
1489 case NavigationController::UA_OVERRIDE_TRUE:
1490 return true;
1491 case NavigationController::UA_OVERRIDE_FALSE:
1492 return false;
1493 default:
1494 break;
1495 }
1496 return false;
Changwan Ryuc1134a82018-03-07 02:10:141497}
1498
dmazzonidd3d51a72016-12-14 18:41:011499void WebContentsImpl::EnableWebContentsOnlyAccessibilityMode() {
Dominic Mazzonid9fda172019-03-07 17:12:071500 // If accessibility is already enabled, we'll need to force a reset
1501 // in order to ensure new observers of accessibility events get the
1502 // full accessibility tree from scratch.
1503 bool need_reset = GetAccessibilityMode().has_mode(ui::AXMode::kWebContents);
1504
1505 ui::AXMode desired_mode =
1506 GetContentClient()->browser()->GetAXModeForBrowserContext(
1507 GetBrowserContext());
1508 desired_mode |= ui::kAXModeWebContentsOnly;
1509 AddAccessibilityMode(desired_mode);
1510
1511 if (need_reset) {
dchengafb53e22016-02-04 08:11:081512 for (RenderFrameHost* rfh : GetAllFrames())
1513 ResetAccessibility(rfh);
dchengafb53e22016-02-04 08:11:081514 }
[email protected]95640212014-07-26 18:14:301515}
1516
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411517bool WebContentsImpl::IsWebContentsOnlyAccessibilityModeForTesting() {
Doug Turner63f3c7b2017-07-29 05:10:011518 return accessibility_mode_ == ui::kAXModeWebContentsOnly;
[email protected]95640212014-07-26 18:14:301519}
1520
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411521bool WebContentsImpl::IsFullAccessibilityModeForTesting() {
Doug Turner63f3c7b2017-07-29 05:10:011522 return accessibility_mode_ == ui::kAXModeComplete;
[email protected]95640212014-07-26 18:14:301523}
1524
Becca Hughes6daf5662018-06-27 16:50:541525#if defined(OS_ANDROID)
1526
1527void WebContentsImpl::SetDisplayCutoutSafeArea(gfx::Insets insets) {
Becca Hughes86334622018-07-09 21:29:381528 if (display_cutout_host_impl_)
1529 display_cutout_host_impl_->SetDisplayCutoutSafeArea(insets);
Becca Hughes6daf5662018-06-27 16:50:541530}
1531
1532#endif
1533
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411534const base::string16& WebContentsImpl::GetTitle() {
Nasko Oskov2f7d2112019-10-29 03:26:571535 WebUI* our_web_ui =
1536 GetRenderManager()->speculative_frame_host()
1537 ? GetRenderManager()->speculative_frame_host()->web_ui()
1538 : GetRenderManager()->current_frame_host()->web_ui();
[email protected]7ade2732011-02-10 00:13:581539 if (our_web_ui) {
[email protected]96d185d2009-04-24 03:28:541540 // Don't override the title in view source mode.
Carlos IL4dea8902020-05-26 15:14:291541 NavigationEntry* entry = controller_.GetVisibleEntry();
[email protected]96d185d2009-04-24 03:28:541542 if (!(entry && entry->IsViewSourceMode())) {
[email protected]e0112912011-02-02 22:54:351543 // Give the Web UI the chance to override our title.
[email protected]fcf75d42013-12-03 20:11:261544 const base::string16& title = our_web_ui->GetOverriddenTitle();
[email protected]96d185d2009-04-24 03:28:541545 if (!title.empty())
1546 return title;
1547 }
1548 }
1549
1550 // We use the title for the last committed entry rather than a pending
1551 // navigation entry. For example, when the user types in a URL, we want to
1552 // keep the old page's title until the new load has committed and we get a new
1553 // title.
Carlos IL4dea8902020-05-26 15:14:291554 NavigationEntry* entry = controller_.GetLastCommittedEntry();
[email protected]59167c22013-06-03 18:07:321555
creis072b61f2015-09-28 20:52:241556 // We make an exception for initial navigations. We only want to use the title
1557 // from the visible entry if:
1558 // 1. The pending entry has been explicitly assigned a title to display.
1559 // 2. The user is doing a history navigation in a new tab (e.g., Ctrl+Back),
1560 // which case there is a pending entry index other than -1.
1561 //
1562 // Otherwise, we want to stick with the last committed entry's title during
1563 // new navigations, which have pending entries at index -1 with no title.
1564 if (controller_.IsInitialNavigation() &&
1565 ((controller_.GetVisibleEntry() &&
1566 !controller_.GetVisibleEntry()->GetTitle().empty()) ||
1567 controller_.GetPendingEntryIndex() != -1)) {
1568 entry = controller_.GetVisibleEntry();
[email protected]9eeafc012013-11-25 23:49:471569 }
[email protected]59167c22013-06-03 18:07:321570
[email protected]45d0ef7f2011-01-05 13:46:231571 if (entry) {
jshin1fb76462016-04-05 22:13:031572 return entry->GetTitleForDisplay();
[email protected]45d0ef7f2011-01-05 13:46:231573 }
[email protected]987fc3a2011-05-26 14:18:091574
1575 // |page_title_when_no_navigation_entry_| is finally used
1576 // if no title cannot be retrieved.
1577 return page_title_when_no_navigation_entry_;
[email protected]96d185d2009-04-24 03:28:541578}
1579
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411580SiteInstanceImpl* WebContentsImpl::GetSiteInstance() {
[email protected]fa944cb82013-11-15 17:51:211581 return GetRenderManager()->current_host()->GetSiteInstance();
[email protected]96d185d2009-04-24 03:28:541582}
1583
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411584bool WebContentsImpl::IsLoading() {
Carlos ILd51e7702020-05-07 18:51:391585 return frame_tree_.IsLoading();
[email protected]3c9e1872010-11-18 16:17:491586}
1587
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411588double WebContentsImpl::GetLoadProgress() {
Peter Boström18a40fa2018-10-31 19:03:501589 return frame_tree_.load_progress();
1590}
1591
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411592bool WebContentsImpl::IsLoadingToDifferentDocument() {
clamy44e84ce2016-02-22 15:38:251593 return IsLoading() && is_load_to_different_document_;
[email protected]6dfed692014-05-22 04:18:031594}
1595
Alexander Timindd0fa0c52019-11-06 13:03:371596bool WebContentsImpl::IsDocumentOnLoadCompletedInMainFrame() {
1597 return GetRenderViewHost()->IsDocumentOnLoadCompletedInMainFrame();
1598}
1599
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411600bool WebContentsImpl::IsWaitingForResponse() {
Peter Boströma9a27f6e2018-11-21 13:12:191601 NavigationRequest* ongoing_navigation_request =
1602 frame_tree_.root()->navigation_request();
1603
1604 // An ongoing navigation request means we're waiting for a response.
1605 return ongoing_navigation_request != nullptr;
[email protected]be1f56ab2011-12-22 06:55:311606}
1607
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411608const net::LoadStateWithParam& WebContentsImpl::GetLoadState() {
[email protected]be1f56ab2011-12-22 06:55:311609 return load_state_;
1610}
1611
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411612const base::string16& WebContentsImpl::GetLoadStateHost() {
[email protected]be1f56ab2011-12-22 06:55:311613 return load_state_host_;
1614}
1615
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411616uint64_t WebContentsImpl::GetUploadSize() {
[email protected]be1f56ab2011-12-22 06:55:311617 return upload_size_;
1618}
1619
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411620uint64_t WebContentsImpl::GetUploadPosition() {
[email protected]be1f56ab2011-12-22 06:55:311621 return upload_position_;
1622}
1623
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411624const std::string& WebContentsImpl::GetEncoding() {
Alexander Timine5f20e22019-10-09 23:22:351625 return GetMainFrame()->GetEncoding();
[email protected]be1f56ab2011-12-22 06:55:311626}
1627
Charles Zhaod72f99d2018-09-17 14:18:271628bool WebContentsImpl::WasDiscarded() {
1629 return GetFrameTree()->root()->was_discarded();
1630}
1631
Shubhie Panickerddf2a4e2018-03-06 00:09:061632void WebContentsImpl::SetWasDiscarded(bool was_discarded) {
1633 GetFrameTree()->root()->set_was_discarded();
1634}
1635
Collin Bakerc5259962019-11-14 17:51:581636void WebContentsImpl::IncrementCapturerCount(const gfx::Size& capture_size,
1637 bool stay_hidden) {
[email protected]5a652232013-02-12 06:15:251638 DCHECK(!is_being_destroyed_);
Collin Bakerc5259962019-11-14 17:51:581639 if (stay_hidden)
1640 ++hidden_capturer_count_;
1641 else
1642 ++visible_capturer_count_;
[email protected]222f5822014-02-05 23:40:491643
1644 // Note: This provides a hint to upstream code to size the views optimally
1645 // for quality (e.g., to avoid scaling).
1646 if (!capture_size.IsEmpty() && preferred_size_for_capture_.IsEmpty()) {
1647 preferred_size_for_capture_ = capture_size;
1648 OnPreferredSizeChanged(preferred_size_);
1649 }
ccameron8807c38f2015-01-17 00:29:191650
Collin Bakerc5259962019-11-14 17:51:581651 UpdateVisibilityAndNotifyPageAndView(GetVisibility());
[email protected]54597982013-02-06 01:59:551652}
1653
Collin Bakerc5259962019-11-14 17:51:581654void WebContentsImpl::DecrementCapturerCount(bool stay_hidden) {
1655 if (stay_hidden)
1656 --hidden_capturer_count_;
1657 else
1658 --visible_capturer_count_;
1659 DCHECK_GE(hidden_capturer_count_, 0);
1660 DCHECK_GE(visible_capturer_count_, 0);
[email protected]54597982013-02-06 01:59:551661
[email protected]5a652232013-02-12 06:15:251662 if (is_being_destroyed_)
1663 return;
1664
Francois Doray24fc62c2017-12-11 17:27:331665 if (!IsBeingCaptured()) {
[email protected]222f5822014-02-05 23:40:491666 const gfx::Size old_size = preferred_size_for_capture_;
1667 preferred_size_for_capture_ = gfx::Size();
1668 OnPreferredSizeChanged(old_size);
[email protected]54597982013-02-06 01:59:551669 }
Collin Bakerc5259962019-11-14 17:51:581670
1671 UpdateVisibilityAndNotifyPageAndView(GetVisibility());
[email protected]be1f56ab2011-12-22 06:55:311672}
1673
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411674bool WebContentsImpl::IsBeingCaptured() {
Collin Bakerc5259962019-11-14 17:51:581675 return visible_capturer_count_ + hidden_capturer_count_ > 0;
[email protected]f2bd40812013-07-20 04:30:441676}
1677
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411678bool WebContentsImpl::IsAudioMuted() {
Guido Urdanetaf8372942019-11-15 09:47:211679 return audio_stream_factory_ && audio_stream_factory_->IsMuted();
miu50f97892014-09-22 22:49:521680}
1681
1682void WebContentsImpl::SetAudioMuted(bool mute) {
1683 DVLOG(1) << "SetAudioMuted(mute=" << mute << "), was " << IsAudioMuted()
1684 << " for WebContentsImpl@" << this;
1685
1686 if (mute == IsAudioMuted())
1687 return;
1688
Guido Urdanetaf8372942019-11-15 09:47:211689 GetAudioStreamFactory()->SetMuted(mute);
miu50f97892014-09-22 22:49:521690
ericwilligersde386342016-10-19 02:35:091691 for (auto& observer : observers_)
1692 observer.DidUpdateAudioMutingState(mute);
wjmaclean2f797c782016-01-07 14:52:031693
Jan Rucka442c83f2017-08-08 13:27:541694 // Notification for UI updates in response to the changed muting state.
Collin Bakeraf540cf2019-09-20 20:54:161695 NotifyNavigationStateChanged(INVALIDATE_TYPE_AUDIO);
Jan Rucka442c83f2017-08-08 13:27:541696}
1697
1698bool WebContentsImpl::IsCurrentlyAudible() {
Chris Hamiltondf0d72cd2018-05-29 16:23:531699 return is_currently_audible_;
miu50f97892014-09-22 22:49:521700}
1701
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411702bool WebContentsImpl::IsConnectedToBluetoothDevice() {
ortunodf4d7982016-04-08 02:33:351703 return bluetooth_connected_device_count_ > 0;
ortuno32e7db3c2016-03-29 16:14:201704}
1705
Ovidio Henriquez76696f62020-07-08 03:06:591706bool WebContentsImpl::IsScanningForBluetoothDevices() {
1707 return bluetooth_scanning_sessions_count_ > 0;
1708}
1709
Lucas Furukawa Gadani4b4eed02019-06-04 23:12:041710bool WebContentsImpl::IsConnectedToSerialPort() {
Reilly Grant5e7c79b22019-04-09 17:26:201711 return serial_active_frame_count_ > 0;
1712}
1713
Matt Reynoldse8c6c1f2019-11-02 09:53:531714bool WebContentsImpl::IsConnectedToHidDevice() {
1715 return hid_active_frame_count_ > 0;
1716}
1717
Marijn Kruisselbrink29051042019-08-06 22:56:551718bool WebContentsImpl::HasNativeFileSystemHandles() {
1719 return native_file_system_handle_count_ > 0;
1720}
Marijn Kruisselbrinkf1714aa22019-07-02 03:45:331721
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411722bool WebContentsImpl::HasPictureInPictureVideo() {
sawtellea7333a82018-05-31 02:36:361723 return has_picture_in_picture_video_;
1724}
1725
1726void WebContentsImpl::SetHasPictureInPictureVideo(
1727 bool has_picture_in_picture_video) {
1728 // If status of |this| is already accurate, there is no need to update.
1729 if (has_picture_in_picture_video == has_picture_in_picture_video_)
1730 return;
1731 has_picture_in_picture_video_ = has_picture_in_picture_video;
1732 NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB);
François Beaufort058085d2018-08-30 15:29:141733 for (auto& observer : observers_)
1734 observer.MediaPictureInPictureChanged(has_picture_in_picture_video_);
sawtellea7333a82018-05-31 02:36:361735}
1736
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411737bool WebContentsImpl::IsCrashed() {
Dominic Mazzoni73a48322018-05-25 18:14:071738 switch (crashed_status_) {
1739 case base::TERMINATION_STATUS_PROCESS_CRASHED:
1740 case base::TERMINATION_STATUS_ABNORMAL_TERMINATION:
1741 case base::TERMINATION_STATUS_PROCESS_WAS_KILLED:
1742 case base::TERMINATION_STATUS_OOM:
1743 case base::TERMINATION_STATUS_LAUNCH_FAILED:
oshima620225722015-06-04 19:45:271744#if defined(OS_CHROMEOS)
Dominic Mazzoni73a48322018-05-25 18:14:071745 case base::TERMINATION_STATUS_PROCESS_WAS_KILLED_BY_OOM:
oshima620225722015-06-04 19:45:271746#endif
Dominic Mazzoni73a48322018-05-25 18:14:071747#if defined(OS_ANDROID)
1748 case base::TERMINATION_STATUS_OOM_PROTECTED:
1749#endif
Will Harris07925d12019-10-31 03:03:051750#if defined(OS_WIN)
1751 case base::TERMINATION_STATUS_INTEGRITY_FAILURE:
1752#endif
Dominic Mazzoni73a48322018-05-25 18:14:071753 return true;
1754 case base::TERMINATION_STATUS_NORMAL_TERMINATION:
1755 case base::TERMINATION_STATUS_STILL_RUNNING:
1756 return false;
1757 case base::TERMINATION_STATUS_MAX_ENUM:
1758 NOTREACHED();
1759 return false;
1760 }
1761
1762 NOTREACHED();
1763 return false;
[email protected]3c9e1872010-11-18 16:17:491764}
1765
[email protected]b172aee2012-04-10 17:05:261766void WebContentsImpl::SetIsCrashed(base::TerminationStatus status,
1767 int error_code) {
[email protected]443b80e2010-12-14 00:42:231768 if (status == crashed_status_)
[email protected]d5f942ba2008-09-26 19:30:341769 return;
1770
[email protected]443b80e2010-12-14 00:42:231771 crashed_status_ = status;
1772 crashed_error_code_ = error_code;
[email protected]8ff00d72012-10-23 19:12:211773 NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB);
[email protected]d5f942ba2008-09-26 19:30:341774}
1775
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411776base::TerminationStatus WebContentsImpl::GetCrashedStatus() {
[email protected]be1f56ab2011-12-22 06:55:311777 return crashed_status_;
1778}
1779
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411780int WebContentsImpl::GetCrashedErrorCode() {
afakhry98241832016-03-11 19:27:471781 return crashed_error_code_;
1782}
1783
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411784bool WebContentsImpl::IsBeingDestroyed() {
[email protected]be1f56ab2011-12-22 06:55:311785 return is_being_destroyed_;
1786}
1787
[email protected]7f924832014-08-09 05:57:221788void WebContentsImpl::NotifyNavigationStateChanged(
1789 InvalidateTypes changed_flags) {
dalecurtis88c240072015-12-09 02:11:181790 // Notify the media observer of potential audibility changes.
Collin Bakeraf540cf2019-09-20 20:54:161791 if (changed_flags & INVALIDATE_TYPE_AUDIO) {
mlamouri44e4ef42016-01-20 18:42:271792 media_web_contents_observer_->MaybeUpdateAudibleState();
dalecurtisbc6572e12014-09-12 19:22:301793 }
1794
[email protected]d5f942ba2008-09-26 19:30:341795 if (delegate_)
1796 delegate_->NavigationStateChanged(this, changed_flags);
wjmaclean1dabf3e32016-05-13 23:34:441797
1798 if (GetOuterWebContents())
1799 GetOuterWebContents()->NotifyNavigationStateChanged(changed_flags);
[email protected]d5f942ba2008-09-26 19:30:341800}
1801
W. James MacLean6f34b042019-07-12 19:25:301802RenderFrameHostImpl* WebContentsImpl::GetFocusedFrameFromFocusedDelegate() {
1803 FrameTreeNode* focused_node =
1804 GetFocusedWebContents()->frame_tree_.GetFocusedFrame();
1805 return focused_node ? focused_node->current_frame_host() : nullptr;
1806}
1807
David Black9cca3592019-11-06 23:02:221808void WebContentsImpl::OnVerticalScrollDirectionChanged(
1809 viz::VerticalScrollDirection scroll_direction) {
1810 for (auto& observer : observers_)
1811 observer.DidChangeVerticalScrollDirection(scroll_direction);
1812}
1813
Chris Hamiltondf0d72cd2018-05-29 16:23:531814void WebContentsImpl::OnAudioStateChanged() {
1815 // This notification can come from any embedded contents or from this
1816 // WebContents' stream monitor. Aggregate these signals to get the actual
1817 // state.
Jeremy Roman03ce13ce2020-05-29 22:16:571818 //
1819 // Note that guests may not be attached as inner contents, and so may need to
1820 // be checked separately.
Chris Hamiltondf0d72cd2018-05-29 16:23:531821 bool is_currently_audible =
1822 audio_stream_monitor_.IsCurrentlyAudible() ||
1823 (browser_plugin_embedder_ &&
Jeremy Roman03ce13ce2020-05-29 22:16:571824 browser_plugin_embedder_->AreAnyGuestsCurrentlyAudible()) ||
1825 AnyInnerWebContents(this, [](WebContents* inner_contents) {
1826 return inner_contents->IsCurrentlyAudible();
1827 });
Chris Hamiltondf0d72cd2018-05-29 16:23:531828 if (is_currently_audible == is_currently_audible_)
1829 return;
1830
1831 // Update internal state.
1832 is_currently_audible_ = is_currently_audible;
1833 was_ever_audible_ = was_ever_audible_ || is_currently_audible_;
1834
Gyuyoung Kim877e86d2020-07-21 04:01:021835 ExecutePageBroadcastMethod(base::BindRepeating(
1836 [](bool is_currently_audible, RenderViewHostImpl* rvh) {
1837 if (auto& broadcast = rvh->GetAssociatedPageBroadcast())
1838 broadcast->AudioStateChanged(is_currently_audible);
1839 },
1840 is_currently_audible_));
altimind8bd26c2016-11-04 11:44:541841
altiminec87fd1f2016-11-17 20:22:491842 // Notification for UI updates in response to the changed audio state.
Collin Bakeraf540cf2019-09-20 20:54:161843 NotifyNavigationStateChanged(INVALIDATE_TYPE_AUDIO);
Jan Rucka442c83f2017-08-08 13:27:541844
Chris Hamiltondf0d72cd2018-05-29 16:23:531845 // Ensure that audio state changes propagate from innermost to outermost
1846 // WebContents.
1847 if (GetOuterWebContents())
1848 GetOuterWebContents()->OnAudioStateChanged();
Tommy Steimel18360512017-11-01 00:38:191849
Chris Hamiltoneb41198f2018-05-24 18:41:261850 for (auto& observer : observers_)
Chris Hamiltondf0d72cd2018-05-29 16:23:531851 observer.OnAudioStateChanged(is_currently_audible_);
altimind8bd26c2016-11-04 11:44:541852}
1853
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411854base::TimeTicks WebContentsImpl::GetLastActiveTime() {
[email protected]9a890452014-02-13 22:21:021855 return last_active_time_;
[email protected]3e324142012-06-25 18:26:331856}
1857
[email protected]9e2e4632012-07-27 16:38:411858void WebContentsImpl::WasShown() {
Collin Baker989e0882019-11-01 01:27:171859 UpdateVisibilityAndNotifyPageAndView(Visibility::VISIBLE);
[email protected]be1f56ab2011-12-22 06:55:311860}
1861
[email protected]b172aee2012-04-10 17:05:261862void WebContentsImpl::WasHidden() {
Collin Baker989e0882019-11-01 01:27:171863 UpdateVisibilityAndNotifyPageAndView(Visibility::HIDDEN);
[email protected]375fa1b2012-05-22 22:05:371864}
1865
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411866bool WebContentsImpl::HasRecentInteractiveInputEvent() {
Daniel Cheng90196c82018-04-25 21:49:141867 static constexpr base::TimeDelta kMaxInterval =
1868 base::TimeDelta::FromSeconds(5);
1869 base::TimeDelta delta =
1870 ui::EventTimeForNow() - last_interactive_input_event_time_;
1871 // Note: the expectation is that the caller is typically expecting an input
1872 // event, e.g. validating that a WebUI message that requires a gesture is
Daniel Chenge81030b32019-07-17 20:20:231873 // actually attached to a gesture.
Daniel Cheng90196c82018-04-25 21:49:141874 return delta <= kMaxInterval;
1875}
1876
Avi Drissman738ea192018-08-29 20:24:161877void WebContentsImpl::SetIgnoreInputEvents(bool ignore_input_events) {
1878 ignore_input_events_ = ignore_input_events;
1879}
1880
Bo Liu168c8642017-08-28 18:26:021881#if defined(OS_ANDROID)
Bo Liu16dd7b8322018-05-02 21:19:501882void WebContentsImpl::SetMainFrameImportance(
1883 ChildProcessImportance importance) {
1884 GetMainFrame()->GetRenderWidgetHost()->SetImportance(importance);
Bo Liu7c6779e92017-08-16 02:02:281885}
Bo Liu168c8642017-08-28 18:26:021886#endif
Bo Liu7c6779e92017-08-16 02:02:281887
ccameron8807c38f2015-01-17 00:29:191888void WebContentsImpl::WasOccluded() {
Collin Baker989e0882019-11-01 01:27:171889 UpdateVisibilityAndNotifyPageAndView(Visibility::OCCLUDED);
ccameron8807c38f2015-01-17 00:29:191890}
1891
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411892Visibility WebContentsImpl::GetVisibility() {
Francois Doraye6161152018-03-27 22:05:371893 return visibility_;
Francois Dorayfe4a1772018-02-17 04:17:091894}
1895
Alex Moshchuk5bcd7c12019-11-12 22:55:311896bool WebContentsImpl::NeedToFireBeforeUnloadOrUnload() {
Alexander Timinc7bee322020-05-19 17:54:341897 if (!notify_disconnection_)
Alex Moshchuk6fcaca752018-07-14 02:13:591898 return false;
1899
1900 // Don't fire if the main frame's RenderViewHost indicates that beforeunload
1901 // and unload have already executed (e.g., after receiving a ClosePage ACK)
1902 // or should be ignored.
1903 if (GetRenderViewHost()->SuddenTerminationAllowed())
1904 return false;
1905
Alex Moshchukc78dbc722019-10-31 20:00:501906 // Check whether any frame in the frame tree needs to run beforeunload or
1907 // unload handlers.
1908 for (FrameTreeNode* node : frame_tree_.Nodes()) {
1909 RenderFrameHostImpl* rfh = node->current_frame_host();
1910
1911 // No need to run beforeunload/unload if the RenderFrame isn't live.
1912 if (!rfh->IsRenderFrameLive())
1913 continue;
1914
Dave Tapuska97d22ab2019-12-03 17:56:361915 if (rfh->GetSuddenTerminationDisablerState(
1916 blink::mojom::SuddenTerminationDisablerType::
1917 kBeforeUnloadHandler) ||
1918 rfh->GetSuddenTerminationDisablerState(
1919 blink::mojom::SuddenTerminationDisablerType::kUnloadHandler)) {
Alex Moshchukc78dbc722019-10-31 20:00:501920 return true;
1921 }
Aditya Keerthi34f37e62019-03-08 15:54:531922 }
Alex Moshchuk6fcaca752018-07-14 02:13:591923
Alex Moshchukc78dbc722019-10-31 20:00:501924 return false;
[email protected]be1f56ab2011-12-22 06:55:311925}
1926
Chris Hamilton3b3e315d2018-09-19 13:16:211927void WebContentsImpl::DispatchBeforeUnload(bool auto_cancel) {
1928 auto before_unload_type =
1929 auto_cancel ? RenderFrameHostImpl::BeforeUnloadType::DISCARD
1930 : RenderFrameHostImpl::BeforeUnloadType::TAB_CLOSE;
1931 GetMainFrame()->DispatchBeforeUnload(before_unload_type, false);
[email protected]1c3f80bd2014-04-08 23:02:061932}
1933
W. James MacLean2539adb32019-12-13 00:40:441934bool WebContentsImpl::IsInnerWebContentsForGuest() {
1935 return !!browser_plugin_guest_;
1936}
1937
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:501938void WebContentsImpl::AttachInnerWebContents(
1939 std::unique_ptr<WebContents> inner_web_contents,
W. James MacLean62883982019-11-12 20:39:001940 RenderFrameHost* render_frame_host,
1941 bool is_full_page) {
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:501942 WebContentsImpl* inner_web_contents_impl =
1943 static_cast<WebContentsImpl*>(inner_web_contents.get());
1944 DCHECK(!inner_web_contents_impl->node_.outer_web_contents());
1945 auto* render_frame_host_impl =
1946 static_cast<RenderFrameHostImpl*>(render_frame_host);
Fergal Daly445af932020-06-02 06:37:231947 DCHECK_EQ(&frame_tree_, render_frame_host_impl->frame_tree());
Lucas Furukawa Gadani105de1e812018-05-31 19:38:391948
Yuzu Saijo3f86fb22020-06-11 03:51:101949 // Mark |render_frame_host_impl| as outer delegate frame.
1950 render_frame_host_impl->SetIsOuterDelegateFrame(true);
1951
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:501952 RenderFrameHostManager* inner_render_manager =
1953 inner_web_contents_impl->GetRenderManager();
1954 RenderFrameHostImpl* inner_main_frame =
1955 inner_render_manager->current_frame_host();
1956 RenderViewHostImpl* inner_render_view_host =
1957 inner_render_manager->current_host();
1958 auto* outer_render_manager =
1959 render_frame_host_impl->frame_tree_node()->render_manager();
lfg91a79e12016-04-01 23:52:191960
Lucas Furukawa Gadani105de1e812018-05-31 19:38:391961 // When attaching a WebContents as an inner WebContents, we need to replace
1962 // the Webcontents' view with a WebContentsViewChildFrame.
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:501963 inner_web_contents_impl->view_.reset(new WebContentsViewChildFrame(
1964 inner_web_contents_impl,
1965 GetContentClient()->browser()->GetWebContentsViewDelegate(
1966 inner_web_contents_impl),
1967 &inner_web_contents_impl->render_view_host_delegate_view_));
Lucas Furukawa Gadani105de1e812018-05-31 19:38:391968
lfg91a79e12016-04-01 23:52:191969 // When the WebContents being initialized has an opener, the browser side
1970 // Render{View,Frame}Host must be initialized and the RenderWidgetHostView
1971 // created. This is needed because the usual initialization happens during
1972 // the first navigation, but when attaching a new window we don't navigate
1973 // before attaching. If the browser side is already initialized, the calls
1974 // below will just early return.
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:501975 inner_render_manager->InitRenderView(inner_render_view_host, nullptr);
1976 inner_main_frame->Init();
1977 if (!inner_render_manager->GetRenderWidgetHostView()) {
1978 inner_web_contents_impl->CreateRenderWidgetHostViewForRenderManager(
1979 inner_render_view_host);
1980 }
lfg91a79e12016-04-01 23:52:191981
Adithya Srinivasane6e7f842019-12-16 18:41:361982 inner_web_contents_impl->RecursivelyUnregisterFrameSinkIds();
1983
lazyboy6ec48b2a2015-06-29 15:18:141984 // Create a link to our outer WebContents.
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:501985 node_.AttachInnerWebContents(std::move(inner_web_contents),
1986 render_frame_host_impl);
lazyboy6ec48b2a2015-06-29 15:18:141987
lazyboy6ec48b2a2015-06-29 15:18:141988 // Create a proxy in top-level RenderFrameHostManager, pointing to the
1989 // SiteInstance of the outer WebContents. The proxy will be used to send
1990 // postMessage to the inner WebContents.
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:501991 auto* proxy = inner_render_manager->CreateOuterDelegateProxy(
1992 render_frame_host_impl->GetSiteInstance());
lazyboy6ec48b2a2015-06-29 15:18:141993
Lucas Furukawa Gadani99125822019-01-03 15:41:491994 // When attaching a GuestView as an inner WebContents, there should already be
1995 // a live RenderFrame, which has to be swapped. When attaching a portal, there
1996 // will not be a live RenderFrame before creating the proxy.
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:501997 if (render_frame_host_impl->IsRenderFrameLive()) {
1998 inner_render_manager->SwapOuterDelegateFrame(render_frame_host_impl, proxy);
Lucas Furukawa Gadani99125822019-01-03 15:41:491999
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:502000 inner_web_contents_impl->ReattachToOuterWebContentsFrame();
Lucas Furukawa Gadani99125822019-01-03 15:41:492001 }
ekaramadadd882292016-06-08 15:22:562002
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:502003 if (frame_tree_.GetFocusedFrame() ==
2004 render_frame_host_impl->frame_tree_node()) {
2005 inner_web_contents_impl->SetFocusedFrame(
2006 inner_web_contents_impl->frame_tree_.root(),
2007 render_frame_host_impl->GetSiteInstance());
avallee7529b2a2017-04-26 11:37:512008 }
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:502009 outer_render_manager->set_attach_complete();
W. James MacLean62883982019-11-12 20:39:002010
2011 // If the inner WebContents is full frame, give it focus.
2012 if (is_full_page) {
2013 // There should only ever be one inner WebContents when |is_full_page| is
2014 // true, and it is the one we just attached.
2015 DCHECK_EQ(1u, node_.GetInnerWebContents().size());
2016 inner_web_contents_impl->SetAsFocusedWebContentsIfNecessary();
2017 }
Chris Hamilton16b5d1d2020-06-04 00:08:122018
2019 for (auto& observer : observers_) {
2020 observer.InnerWebContentsAttached(inner_web_contents_impl,
2021 render_frame_host, is_full_page);
2022 }
lazyboy6ec48b2a2015-06-29 15:18:142023}
2024
Lucas Furukawa Gadani99125822019-01-03 15:41:492025std::unique_ptr<WebContents> WebContentsImpl::DetachFromOuterWebContents() {
Chris Hamilton16b5d1d2020-06-04 00:08:122026 auto* outer_web_contents = GetOuterWebContents();
2027 DCHECK(outer_web_contents);
Yuzu Saijo3f86fb22020-06-11 03:51:102028 GetMainFrame()->ParentOrOuterDelegateFrame()->SetIsOuterDelegateFrame(false);
Chris Hamilton16b5d1d2020-06-04 00:08:122029
Adithya Srinivasane6e7f842019-12-16 18:41:362030 RecursivelyUnregisterFrameSinkIds();
Lucas Gadanid29952582020-06-09 21:24:002031
2032 // Each RenderViewHost has a RenderWidgetHost which can have a
2033 // RenderWidgetHostView, and it needs to be re-created with the appropriate
2034 // platform view. It is important to re-create all child views, not only the
2035 // current one, since the view can be swapped due to a cross-origin
2036 // navigation.
2037 std::set<RenderViewHostImpl*> render_view_hosts;
2038 for (auto& render_view_host : GetFrameTree()->render_view_hosts()) {
2039 if (render_view_host.second->GetWidget() &&
2040 render_view_host.second->GetWidget()->GetView()) {
2041 DCHECK(render_view_host.second->GetWidget()
2042 ->GetView()
2043 ->IsRenderWidgetHostViewChildFrame());
2044 render_view_hosts.insert(render_view_host.second);
Kevin McNee6471a702020-01-27 18:23:072045 }
2046 }
Lucas Gadanid29952582020-06-09 21:24:002047
2048 for (auto* render_view_host : render_view_hosts)
2049 render_view_host->GetWidget()->GetView()->Destroy();
2050
Lucas Furukawa Gadani99125822019-01-03 15:41:492051 GetRenderManager()->DeleteOuterDelegateProxy(
2052 node_.OuterContentsFrameTreeNode()
2053 ->current_frame_host()
2054 ->GetSiteInstance());
2055 view_.reset(CreateWebContentsView(
2056 this, GetContentClient()->browser()->GetWebContentsViewDelegate(this),
2057 &render_view_host_delegate_view_));
danakjfc5184932019-09-12 18:08:322058 view_->CreateView(nullptr);
Lucas Furukawa Gadani99125822019-01-03 15:41:492059 std::unique_ptr<WebContents> web_contents =
2060 node_.DisconnectFromOuterWebContents();
2061 DCHECK_EQ(web_contents.get(), this);
2062 node_.SetFocusedWebContents(this);
Lucas Gadanid29952582020-06-09 21:24:002063
2064 for (auto* render_view_host : render_view_hosts)
2065 CreateRenderWidgetHostViewForRenderManager(render_view_host);
2066
Adithya Srinivasane6e7f842019-12-16 18:41:362067 RecursivelyRegisterFrameSinkIds();
Adithya Srinivasan6f0f67b2019-12-19 01:26:462068 // TODO(adithyas): |browser_plugin_embedder_ax_tree_id| should either not be
2069 // used for portals, or it should get a different name.
2070 GetMainFrame()->set_browser_plugin_embedder_ax_tree_id(ui::AXTreeIDUnknown());
2071 GetMainFrame()->UpdateAXTreeData();
Chris Hamilton16b5d1d2020-06-04 00:08:122072
2073 // Invoke on the *outer* web contents observers for symmetry.
2074 for (auto& observer : outer_web_contents->observers_)
2075 observer.InnerWebContentsDetached(this);
2076
Lucas Furukawa Gadani99125822019-01-03 15:41:492077 return web_contents;
2078}
2079
W. James MacLean2dc75ed42019-03-06 14:31:092080void WebContentsImpl::RecursivelyRegisterFrameSinkIds() {
Adithya Srinivasane6e7f842019-12-16 18:41:362081 for (auto* view : GetRenderWidgetHostViewsInWebContentsTree()) {
2082 auto* rwhvb = static_cast<RenderWidgetHostViewBase*>(view);
2083 if (rwhvb->IsRenderWidgetHostViewChildFrame())
2084 static_cast<RenderWidgetHostViewChildFrame*>(view)->RegisterFrameSinkId();
2085 }
2086}
W. James MacLean2dc75ed42019-03-06 14:31:092087
Adithya Srinivasane6e7f842019-12-16 18:41:362088void WebContentsImpl::RecursivelyUnregisterFrameSinkIds() {
2089 for (auto* view : GetRenderWidgetHostViewsInWebContentsTree()) {
2090 auto* rwhvb = static_cast<RenderWidgetHostViewBase*>(view);
2091 if (rwhvb->IsRenderWidgetHostViewChildFrame()) {
2092 static_cast<RenderWidgetHostViewChildFrame*>(view)
2093 ->UnregisterFrameSinkId();
2094 }
W. James MacLean2dc75ed42019-03-06 14:31:092095 }
2096}
2097
Lucas Furukawa Gadaniaed1fed2017-10-13 17:34:142098void WebContentsImpl::ReattachToOuterWebContentsFrame() {
2099 DCHECK(node_.outer_web_contents());
2100 auto* render_manager = GetRenderManager();
2101 auto* parent_frame =
2102 node_.OuterContentsFrameTreeNode()->current_frame_host()->GetParent();
2103 render_manager->SetRWHViewForInnerContents(
2104 render_manager->GetRenderWidgetHostView());
2105
W. James MacLean2dc75ed42019-03-06 14:31:092106 RecursivelyRegisterFrameSinkIds();
Lucas Furukawa Gadaniaed1fed2017-10-13 17:34:142107
2108 // Set up the the guest's AX tree to point back at the embedder's AX tree.
2109 GetMainFrame()->set_browser_plugin_embedder_ax_tree_id(
2110 parent_frame->GetAXTreeID());
2111 GetMainFrame()->UpdateAXTreeData();
2112}
2113
Lucas Gadani972985622020-05-28 20:52:512114void WebContentsImpl::DidActivatePortal(
2115 WebContentsImpl* predecessor_web_contents,
2116 base::TimeTicks activation_time) {
2117 DCHECK(predecessor_web_contents);
Kevin McNeef8eb64c2020-04-21 21:36:062118 NotifyInsidePortal(false);
Kevin McNeef8eb64c2020-04-21 21:36:062119 for (auto& observer : observers_)
Lucas Gadani972985622020-05-28 20:52:512120 observer.DidActivatePortal(predecessor_web_contents, activation_time);
2121
Lucas Gadani972985622020-05-28 20:52:512122 GetDelegate()->WebContentsBecamePortal(predecessor_web_contents);
Kevin McNeef8eb64c2020-04-21 21:36:062123}
2124
Adithya Srinivasan11af2c52019-12-16 22:52:552125void WebContentsImpl::NotifyInsidePortal(bool inside_portal) {
Gyuyoung Kim1c029712020-07-22 09:21:062126 ExecutePageBroadcastMethod(base::BindRepeating(
2127 [](bool inside_portal, RenderViewHostImpl* rvh) {
2128 if (auto& broadcast = rvh->GetAssociatedPageBroadcast())
2129 broadcast->SetInsidePortal(inside_portal);
2130 },
2131 inside_portal));
Adithya Srinivasan11af2c52019-12-16 22:52:552132}
2133
dalecurtis6c58ed02016-10-28 23:02:372134void WebContentsImpl::DidChangeVisibleSecurityState() {
Emily Starkc663b0242019-04-26 19:34:452135 if (delegate_)
dalecurtis6c58ed02016-10-28 23:02:372136 delegate_->VisibleSecurityStateChanged(this);
Emily Starkc663b0242019-04-26 19:34:452137 for (auto& observer : observers_)
2138 observer.DidChangeVisibleSecurityState();
dalecurtis6c58ed02016-10-28 23:02:372139}
2140
Rakina Zata Amni4029b6d2020-07-28 02:36:202141const WebPreferences WebContentsImpl::ComputeWebPreferences() {
2142 TRACE_EVENT0("browser", "WebContentsImpl::ComputeWebPreferences");
2143 WebPreferences prefs;
2144
2145 const base::CommandLine& command_line =
2146 *base::CommandLine::ForCurrentProcess();
2147
2148 SetSlowWebPreferences(command_line, &prefs);
2149
2150 prefs.web_security_enabled =
2151 !command_line.HasSwitch(switches::kDisableWebSecurity);
2152
2153 prefs.remote_fonts_enabled =
2154 !command_line.HasSwitch(switches::kDisableRemoteFonts);
2155 prefs.application_cache_enabled =
2156 base::FeatureList::IsEnabled(blink::features::kAppCache);
2157 prefs.local_storage_enabled =
2158 !command_line.HasSwitch(switches::kDisableLocalStorage);
2159 prefs.databases_enabled =
2160 !command_line.HasSwitch(switches::kDisableDatabases);
2161
2162 prefs.webgl1_enabled = !command_line.HasSwitch(switches::kDisable3DAPIs) &&
2163 !command_line.HasSwitch(switches::kDisableWebGL);
2164 prefs.webgl2_enabled = !command_line.HasSwitch(switches::kDisable3DAPIs) &&
2165 !command_line.HasSwitch(switches::kDisableWebGL) &&
2166 !command_line.HasSwitch(switches::kDisableWebGL2);
2167
2168 prefs.pepper_3d_enabled = !command_line.HasSwitch(switches::kDisablePepper3d);
2169
2170 prefs.flash_3d_enabled = !command_line.HasSwitch(switches::kDisableFlash3d);
2171 prefs.flash_stage3d_enabled =
2172 !command_line.HasSwitch(switches::kDisableFlashStage3d);
2173 prefs.flash_stage3d_baseline_enabled =
2174 !command_line.HasSwitch(switches::kDisableFlashStage3d);
2175
2176 prefs.allow_file_access_from_file_urls =
2177 command_line.HasSwitch(switches::kAllowFileAccessFromFiles);
2178
2179 prefs.accelerated_2d_canvas_enabled =
2180 !command_line.HasSwitch(switches::kDisableAccelerated2dCanvas);
2181 prefs.new_canvas_2d_api_enabled =
2182 command_line.HasSwitch(switches::kEnableNewCanvas2DAPI);
2183 prefs.antialiased_2d_canvas_disabled =
2184 command_line.HasSwitch(switches::kDisable2dCanvasAntialiasing);
2185 prefs.antialiased_clips_2d_canvas_enabled =
2186 !command_line.HasSwitch(switches::kDisable2dCanvasClipAntialiasing);
2187
2188 prefs.disable_ipc_flooding_protection =
2189 command_line.HasSwitch(switches::kDisableIpcFloodingProtection) ||
2190 command_line.HasSwitch(switches::kDisablePushStateThrottle);
2191
2192 prefs.accelerated_video_decode_enabled =
2193 !command_line.HasSwitch(switches::kDisableAcceleratedVideoDecode);
2194
2195 std::string autoplay_policy = media::GetEffectiveAutoplayPolicy(command_line);
2196 if (autoplay_policy == switches::autoplay::kNoUserGestureRequiredPolicy) {
2197 prefs.autoplay_policy = AutoplayPolicy::kNoUserGestureRequired;
2198 } else if (autoplay_policy ==
2199 switches::autoplay::kUserGestureRequiredPolicy) {
2200 prefs.autoplay_policy = AutoplayPolicy::kUserGestureRequired;
2201 } else if (autoplay_policy ==
2202 switches::autoplay::kDocumentUserActivationRequiredPolicy) {
2203 prefs.autoplay_policy = AutoplayPolicy::kDocumentUserActivationRequired;
2204 } else {
2205 NOTREACHED();
2206 }
2207
2208 prefs.dont_send_key_events_to_javascript =
2209 base::FeatureList::IsEnabled(features::kDontSendKeyEventsToJavascript);
2210
2211// TODO(dtapuska): Enable barrel button selection drag support on Android.
2212// crbug.com/758042
2213#if defined(OS_WIN)
2214 prefs.barrel_button_for_drag_enabled =
2215 base::FeatureList::IsEnabled(features::kDirectManipulationStylus);
2216#endif // defined(OS_WIN)
2217
2218 prefs.touch_adjustment_enabled =
2219 !command_line.HasSwitch(switches::kDisableTouchAdjustment);
2220
2221 prefs.enable_scroll_animator =
2222 command_line.HasSwitch(switches::kEnableSmoothScrolling) ||
2223 (!command_line.HasSwitch(switches::kDisableSmoothScrolling) &&
2224 gfx::Animation::ScrollAnimationsEnabledBySystem());
2225
2226 prefs.prefers_reduced_motion = gfx::Animation::PrefersReducedMotion();
2227
2228 if (ChildProcessSecurityPolicyImpl::GetInstance()->HasWebUIBindings(
2229 GetRenderViewHost()->GetProcess()->GetID())) {
2230 prefs.loads_images_automatically = true;
2231 prefs.javascript_enabled = true;
2232 }
2233
2234 prefs.viewport_enabled = command_line.HasSwitch(switches::kEnableViewport);
2235
2236 if (IsOverridingUserAgent())
2237 prefs.viewport_meta_enabled = false;
2238
2239 prefs.main_frame_resizes_are_orientation_changes =
2240 command_line.HasSwitch(switches::kMainFrameResizesAreOrientationChanges);
2241
2242 prefs.spatial_navigation_enabled =
2243 command_line.HasSwitch(switches::kEnableSpatialNavigation);
2244
2245 if (IsSpatialNavigationDisabled())
2246 prefs.spatial_navigation_enabled = false;
2247
2248 prefs.caret_browsing_enabled =
2249 command_line.HasSwitch(switches::kEnableCaretBrowsing);
2250
2251 prefs.disable_reading_from_canvas =
2252 command_line.HasSwitch(switches::kDisableReadingFromCanvas);
2253
2254 prefs.strict_mixed_content_checking =
2255 command_line.HasSwitch(switches::kEnableStrictMixedContentChecking);
2256
2257 prefs.strict_powerful_feature_restrictions = command_line.HasSwitch(
2258 switches::kEnableStrictPowerfulFeatureRestrictions);
2259
2260 const std::string blockable_mixed_content_group =
2261 base::FieldTrialList::FindFullName("BlockableMixedContent");
2262 prefs.strictly_block_blockable_mixed_content =
2263 blockable_mixed_content_group == "StrictlyBlockBlockableMixedContent";
2264
2265 const std::string plugin_mixed_content_status =
2266 base::FieldTrialList::FindFullName("PluginMixedContentStatus");
2267 prefs.block_mixed_plugin_content =
2268 plugin_mixed_content_status == "BlockableMixedContent";
2269
2270 prefs.v8_cache_options = GetV8CacheOptions();
2271
2272 prefs.user_gesture_required_for_presentation = !command_line.HasSwitch(
2273 switches::kDisableGestureRequirementForPresentation);
2274
2275 if (HideDownloadUI())
2276 prefs.hide_download_ui = true;
2277
2278 // `media_controls_enabled` is `true` by default.
2279 if (HasPersistentVideo())
2280 prefs.media_controls_enabled = false;
2281
2282#if defined(OS_ANDROID)
2283 display::Display display = display::Screen::GetScreen()->GetPrimaryDisplay();
2284 gfx::Size size = display.GetSizeInPixel();
2285 int min_width = size.width() < size.height() ? size.width() : size.height();
2286 prefs.device_scale_adjustment = GetDeviceScaleAdjustment(
2287 static_cast<int>(min_width / display.device_scale_factor()));
2288#endif // OS_ANDROID
2289
2290 GetContentClient()->browser()->OverrideWebkitPrefs(GetRenderViewHost(),
2291 &prefs);
2292 return prefs;
2293}
2294
2295void WebContentsImpl::SetSlowWebPreferences(
2296 const base::CommandLine& command_line,
2297 WebPreferences* prefs) {
2298 if (web_preferences_.get()) {
2299#define SET_FROM_CACHE(prefs, field) prefs->field = web_preferences_->field
2300
2301 SET_FROM_CACHE(prefs, touch_event_feature_detection_enabled);
2302 SET_FROM_CACHE(prefs, available_pointer_types);
2303 SET_FROM_CACHE(prefs, available_hover_types);
2304 SET_FROM_CACHE(prefs, primary_pointer_type);
2305 SET_FROM_CACHE(prefs, primary_hover_type);
2306 SET_FROM_CACHE(prefs, pointer_events_max_touch_points);
2307 SET_FROM_CACHE(prefs, number_of_cpu_cores);
2308
2309#if defined(OS_ANDROID)
2310 SET_FROM_CACHE(prefs, video_fullscreen_orientation_lock_enabled);
2311 SET_FROM_CACHE(prefs, video_rotate_to_fullscreen_enabled);
2312#endif
2313
2314#undef SET_FROM_CACHE
2315 } else {
2316 // Every prefs->field modified below should have a SET_FROM_CACHE entry
2317 // above.
2318
2319 // On Android, Touch event feature detection is enabled by default,
2320 // Otherwise default is disabled.
2321 std::string touch_enabled_default_switch =
2322 switches::kTouchEventFeatureDetectionDisabled;
2323#if defined(OS_ANDROID)
2324 touch_enabled_default_switch = switches::kTouchEventFeatureDetectionEnabled;
2325#endif // defined(OS_ANDROID)
2326 const std::string touch_enabled_switch =
2327 command_line.HasSwitch(switches::kTouchEventFeatureDetection)
2328 ? command_line.GetSwitchValueASCII(
2329 switches::kTouchEventFeatureDetection)
2330 : touch_enabled_default_switch;
2331
2332 prefs->touch_event_feature_detection_enabled =
2333 (touch_enabled_switch == switches::kTouchEventFeatureDetectionAuto)
2334 ? (ui::GetTouchScreensAvailability() ==
2335 ui::TouchScreensAvailability::ENABLED)
2336 : (touch_enabled_switch.empty() ||
2337 touch_enabled_switch ==
2338 switches::kTouchEventFeatureDetectionEnabled);
2339
2340 std::tie(prefs->available_pointer_types, prefs->available_hover_types) =
2341 ui::GetAvailablePointerAndHoverTypes();
2342 prefs->primary_pointer_type =
2343 ui::GetPrimaryPointerType(prefs->available_pointer_types);
2344 prefs->primary_hover_type =
2345 ui::GetPrimaryHoverType(prefs->available_hover_types);
2346
2347 prefs->pointer_events_max_touch_points = ui::MaxTouchPoints();
2348
2349 prefs->number_of_cpu_cores = base::SysInfo::NumberOfProcessors();
2350
2351#if defined(OS_ANDROID)
2352 const bool device_is_phone =
2353 ui::GetDeviceFormFactor() == ui::DEVICE_FORM_FACTOR_PHONE;
2354 prefs->video_fullscreen_orientation_lock_enabled = device_is_phone;
2355 prefs->video_rotate_to_fullscreen_enabled = device_is_phone;
2356#endif
2357 }
2358}
2359
2360void WebContentsImpl::OnWebPreferencesChanged() {
2361 // This is defensive code to avoid infinite loops due to code run inside
2362 // SetWebPreferences() accidentally updating more preferences and thus
2363 // calling back into this code. See crbug.com/398751 for one past example.
2364 if (updating_web_preferences_)
2365 return;
2366 updating_web_preferences_ = true;
2367 SetWebPreferences(ComputeWebPreferences());
2368#if defined(OS_ANDROID)
2369 for (FrameTreeNode* node : frame_tree_.Nodes()) {
2370 RenderFrameHostImpl* rfh = node->current_frame_host();
2371 if (rfh->is_local_root()) {
2372 if (auto* rwh = rfh->GetRenderWidgetHost())
2373 rwh->SetForceEnableZoom(web_preferences_->force_enable_zoom);
2374 }
2375 }
2376#endif
2377 updating_web_preferences_ = false;
2378}
2379
Ahmed Fakhryd70bf37b2018-04-04 17:07:242380void WebContentsImpl::NotifyPreferencesChanged() {
Rakina Zata Amni347b70902020-07-22 10:49:042381 // Recompute the WebPreferences based on the current state of the WebContents,
Rakina Zata Amni4029b6d2020-07-28 02:36:202382 // etc. Note that OnWebPreferencesChanged will also call SetWebPreferences and
2383 // send the updated WebPreferences to all RenderViews for this WebContents.
2384 OnWebPreferencesChanged();
Ahmed Fakhryd70bf37b2018-04-04 17:07:242385}
2386
Bruce Long1e3e1f542019-10-16 17:56:282387void WebContentsImpl::SyncRendererPrefs() {
2388 blink::mojom::RendererPreferences renderer_preferences =
2389 GetRendererPrefs(GetBrowserContext());
2390 RenderViewHostImpl::GetPlatformSpecificPrefs(&renderer_preferences);
2391 SendPageMessage(
2392 new PageMsg_SetRendererPrefs(MSG_ROUTING_NONE, renderer_preferences));
2393}
2394
Alexander Timin1cc31f42020-05-12 16:26:012395void WebContentsImpl::OnCookiesAccessed(NavigationHandle* navigation,
2396 const CookieAccessDetails& details) {
Josh Karlindaba39322019-07-17 23:24:332397 for (auto& observer : observers_) {
Alexander Timin1cc31f42020-05-12 16:26:012398 observer.OnCookiesAccessed(navigation, details);
2399 }
2400}
2401
2402void WebContentsImpl::OnCookiesAccessed(RenderFrameHostImpl* rfh,
2403 const CookieAccessDetails& details) {
2404 for (auto& observer : observers_) {
2405 observer.OnCookiesAccessed(rfh, details);
Josh Karlindaba39322019-07-17 23:24:332406 }
2407}
2408
[email protected]b172aee2012-04-10 17:05:262409void WebContentsImpl::Stop() {
dcheng57e39e22016-01-21 00:25:382410 for (FrameTreeNode* node : frame_tree_.Nodes())
2411 node->StopLoading();
ericwilligersde386342016-10-19 02:35:092412 for (auto& observer : observers_)
2413 observer.NavigationStopped();
[email protected]0bfbf882011-12-22 18:19:272414}
2415
Francois Doray47f759d2018-06-11 18:13:512416void WebContentsImpl::SetPageFrozen(bool frozen) {
2417 // A visible page is never frozen.
2418 DCHECK_NE(Visibility::VISIBLE, GetVisibility());
2419
Yuzu Saijo4fa1e9a2020-05-20 02:53:262420 for (auto& entry : frame_tree_.render_view_hosts()) {
2421 entry.second->SetIsFrozen(frozen);
2422 }
Fadi Meawada6573e02018-03-10 00:52:112423}
2424
erikchen6c7df7f7a2018-05-03 18:13:592425std::unique_ptr<WebContents> WebContentsImpl::Clone() {
[email protected]14392a52012-05-02 20:28:442426 // We use our current SiteInstance since the cloned entry will use it anyway.
alexmose201c7cd2015-06-10 17:14:212427 // We pass our own opener so that the cloned page can access it if it was set
[email protected]ed245db2012-12-18 08:00:452428 // before.
[email protected]54944cde2012-12-09 09:24:592429 CreateParams create_params(GetBrowserContext(), GetSiteInstance());
Ian Clelland5cbaaf82017-11-27 22:00:032430 FrameTreeNode* opener = frame_tree_.root()->opener();
2431 RenderFrameHostImpl* opener_rfh = nullptr;
2432 if (opener)
2433 opener_rfh = opener->current_frame_host();
erikchen6c7df7f7a2018-05-03 18:13:592434 std::unique_ptr<WebContentsImpl> tc =
erikchen50735fd2018-05-05 15:08:332435 CreateWithOpener(create_params, opener_rfh);
Lucas Furukawa Gadani5553a152019-01-08 18:55:572436 tc->GetController().CopyStateFrom(&controller_, true);
ericwilligersde386342016-10-19 02:35:092437 for (auto& observer : observers_)
erikchen6c7df7f7a2018-05-03 18:13:592438 observer.DidCloneToNewWebContents(this, tc.get());
[email protected]0bfbf882011-12-22 18:19:272439 return tc;
2440}
2441
[email protected]14392a52012-05-02 20:28:442442void WebContentsImpl::Observe(int type,
[email protected]8ff00d72012-10-23 19:12:212443 const NotificationSource& source,
2444 const NotificationDetails& details) {
[email protected]14392a52012-05-02 20:28:442445 switch (type) {
[email protected]8ff00d72012-10-23 19:12:212446 case NOTIFICATION_RENDER_WIDGET_HOST_DESTROYED: {
2447 RenderWidgetHost* host = Source<RenderWidgetHost>(source).ptr();
[email protected]0b431992014-06-24 00:08:032448 RenderWidgetHostView* view = host->GetView();
2449 if (view == GetFullscreenRenderWidgetHostView()) {
2450 // We cannot just call view_->RestoreFocus() here. On some platforms,
2451 // attempting to focus the currently-invisible WebContentsView will be
2452 // flat-out ignored. Therefore, this boolean is used to track whether
2453 // we will request focus after the fullscreen widget has been
2454 // destroyed.
2455 fullscreen_widget_had_focus_at_shutdown_ = (view && view->HasFocus());
2456 } else {
alexmosc2a8cec2016-05-23 22:19:532457 for (auto i = pending_widget_views_.begin();
[email protected]0b431992014-06-24 00:08:032458 i != pending_widget_views_.end(); ++i) {
2459 if (host->GetView() == i->second) {
2460 pending_widget_views_.erase(i);
2461 break;
2462 }
[email protected]bafe6cd2012-05-23 23:09:502463 }
2464 }
2465 break;
2466 }
[email protected]14392a52012-05-02 20:28:442467 default:
2468 NOTREACHED();
2469 }
2470}
2471
[email protected]ec6c05f2013-10-23 18:41:572472WebContents* WebContentsImpl::GetWebContents() {
2473 return this;
2474}
2475
[email protected]54944cde2012-12-09 09:24:592476void WebContentsImpl::Init(const WebContents::CreateParams& params) {
[email protected]fa944cb82013-11-15 17:51:212477 // This is set before initializing the render manager since
[email protected]b0936d22013-11-28 06:47:362478 // RenderFrameHostManager::Init calls back into us via its delegate to ask if
[email protected]fa944cb82013-11-15 17:51:212479 // it should be hidden.
Francois Doraye6161152018-03-27 22:05:372480 visibility_ =
2481 params.initially_hidden ? Visibility::HIDDEN : Visibility::VISIBLE;
[email protected]d6fa88f2013-10-18 16:00:432482
Sebastien Marchand28cf23182018-06-20 02:39:352483 if (!params.last_active_time.is_null())
2484 last_active_time_ = params.last_active_time;
2485
dcheng3ce04b62015-10-26 23:30:552486 scoped_refptr<SiteInstance> site_instance = params.site_instance;
2487 if (!site_instance)
2488 site_instance = SiteInstance::Create(params.browser_context);
Lukasz Anforowicz3caa8372018-06-05 17:22:072489 if (params.desired_renderer_state == CreateParams::kNoRendererProcess) {
2490 static_cast<SiteInstanceImpl*>(site_instance.get())
2491 ->PreventAssociationWithSpareProcess();
2492 }
dcheng3ce04b62015-10-26 23:30:552493
Fergal Dalycb0c1fdf2020-03-16 07:13:492494 GetRenderManager()->InitRoot(site_instance.get(),
2495 params.renderer_initiated_creation);
lukasza464d8692016-02-22 19:26:322496
lukasza5140a412016-09-15 21:12:302497 // blink::FrameTree::setName always keeps |unique_name| empty in case of a
2498 // main frame - let's do the same thing here.
2499 std::string unique_name;
lukasza464d8692016-02-22 19:26:322500 frame_tree_.root()->SetFrameName(params.main_frame_name, unique_name);
[email protected]d1198fd2012-08-13 22:50:192501
[email protected]fc2b46b2014-05-03 16:33:452502 WebContentsViewDelegate* delegate =
2503 GetContentClient()->browser()->GetWebContentsViewDelegate(this);
2504
W. James MacLean2539adb32019-12-13 00:40:442505 if (browser_plugin_guest_) {
sky52a39e342016-12-17 17:23:222506 view_.reset(new WebContentsViewChildFrame(
2507 this, delegate, &render_view_host_delegate_view_));
2508 } else {
2509 view_.reset(CreateWebContentsView(this, delegate,
2510 &render_view_host_delegate_view_));
[email protected]d1198fd2012-08-13 22:50:192511 }
[email protected]fc2b46b2014-05-03 16:33:452512 CHECK(render_view_host_delegate_view_);
[email protected]d1198fd2012-08-13 22:50:192513 CHECK(view_.get());
2514
danakjfc5184932019-09-12 18:08:322515 view_->CreateView(params.context);
[email protected]d1198fd2012-08-13 22:50:192516
brettw4b461082016-11-19 18:55:162517#if BUILDFLAG(ENABLE_PLUGINS)
Yuzu Saijoa11069a2020-04-22 10:56:552518 plugin_content_origin_allowlist_.reset(
2519 new PluginContentOriginAllowlist(this));
tommyclieb25b2a2014-11-03 19:45:092520#endif
2521
[email protected]d1198fd2012-08-13 22:50:192522 registrar_.Add(this,
[email protected]8ff00d72012-10-23 19:12:212523 NOTIFICATION_RENDER_WIDGET_HOST_DESTROYED,
2524 NotificationService::AllBrowserContextsAndSources());
[email protected]f28ef9a32014-05-12 16:36:102525
leon.hane4db177a2017-02-07 14:19:162526 screen_orientation_provider_.reset(new ScreenOrientationProvider(this));
[email protected]01f83f92014-06-17 14:41:542527
mfomitchev2b8b066a2016-01-28 19:23:152528#if defined(OS_ANDROID)
Oksana Zhuravlova0b634332019-10-28 23:04:222529 DateTimeChooserAndroid::CreateForWebContents(this);
[email protected]583418cc2013-01-17 14:01:102530#endif
fsamuel7310a4262014-12-05 05:06:442531
2532 // BrowserPluginGuest::Init needs to be called after this WebContents has
W. James MacLean6e78da342019-12-13 15:13:232533 // a RenderWidgetHostViewChildFrame. That is, |view_->CreateView| above.
fsamuel7310a4262014-12-05 05:06:442534 if (browser_plugin_guest_)
2535 browser_plugin_guest_->Init();
nickf9acfbe2014-12-23 19:12:372536
2537 for (size_t i = 0; i < g_created_callbacks.Get().size(); i++)
2538 g_created_callbacks.Get().at(i).Run(this);
naskob21fe4872015-02-24 14:15:562539
2540 // If the WebContents creation was renderer-initiated, it means that the
2541 // corresponding RenderView and main RenderFrame have already been created.
2542 // Ensure observers are notified about this.
2543 if (params.renderer_initiated_creation) {
Fergal Dalybb2d6592020-06-02 05:02:332544 GetRenderViewHost()->GetWidget()->set_renderer_initialized(true);
Alex Moshchuk27caae82017-09-11 23:11:182545 GetRenderViewHost()->DispatchRenderViewCreated();
naskob21fe4872015-02-24 14:15:562546 GetRenderManager()->current_frame_host()->SetRenderFrameCreated(true);
2547 }
naskof5940b9f2015-03-02 23:04:052548
lof84501da082016-05-23 21:22:542549 // Create the renderer process in advance if requested.
Lukasz Anforowicz3caa8372018-06-05 17:22:072550 if (params.desired_renderer_state ==
2551 CreateParams::kInitializeAndWarmupRendererProcess) {
lof84501da082016-05-23 21:22:542552 if (!GetRenderManager()->current_frame_host()->IsRenderFrameLive()) {
2553 GetRenderManager()->InitRenderView(GetRenderViewHost(), nullptr);
2554 }
2555 }
2556
naskof5940b9f2015-03-02 23:04:052557 // Ensure that observers are notified of the creation of this WebContents's
2558 // main RenderFrameHost. It must be done here for main frames, since the
2559 // NotifySwappedFromRenderManager expects view_ to already be created and that
2560 // happens after RenderFrameHostManager::Init.
2561 NotifySwappedFromRenderManager(
2562 nullptr, GetRenderManager()->current_frame_host(), true);
Albert J. Wonge76bf0b82019-09-27 07:47:062563
2564 // For WebContents that are never shown, do critical initialization here which
2565 // would normally only happen when the WebContents is shown.
2566 if (params.is_never_visible) {
2567 // This has just been created so there can only be one frame. Thus it is
2568 // safe to initialize the root.
2569 GetMainFrame()->Init();
2570 }
[email protected]d1198fd2012-08-13 22:50:192571}
2572
[email protected]7fff43e2013-05-21 20:21:102573void WebContentsImpl::OnWebContentsDestroyed(WebContentsImpl* web_contents) {
2574 RemoveDestructionObserver(web_contents);
2575
[email protected]ceee8cd2013-03-08 04:59:512576 // Clear a pending contents that has been closed before being shown.
alexmosc2a8cec2016-05-23 22:19:532577 for (auto iter = pending_contents_.begin(); iter != pending_contents_.end();
[email protected]ceee8cd2013-03-08 04:59:512578 ++iter) {
Joel Hockey8c2011b22020-04-30 05:07:192579 if (iter->second.contents.get() != web_contents)
[email protected]ceee8cd2013-03-08 04:59:512580 continue;
erikchenbee5c9622018-04-27 19:30:252581
2582 // Someone else has deleted the WebContents. That should never happen!
2583 // TODO(erikchen): Fix semantics here. https://crbug.com/832879.
Joel Hockey8c2011b22020-04-30 05:07:192584 iter->second.contents.release();
[email protected]ceee8cd2013-03-08 04:59:512585 pending_contents_.erase(iter);
[email protected]ceee8cd2013-03-08 04:59:512586 return;
2587 }
2588 NOTREACHED();
[email protected]14392a52012-05-02 20:28:442589}
2590
[email protected]7fff43e2013-05-21 20:21:102591void WebContentsImpl::AddDestructionObserver(WebContentsImpl* web_contents) {
Jan Wilken Dörrie73c901e2019-06-12 09:02:322592 if (!base::Contains(destruction_observers_, web_contents)) {
[email protected]7fff43e2013-05-21 20:21:102593 destruction_observers_[web_contents] =
Jeremy Roman04f27c372017-10-27 15:20:552594 std::make_unique<DestructionObserver>(this, web_contents);
[email protected]7fff43e2013-05-21 20:21:102595 }
2596}
2597
2598void WebContentsImpl::RemoveDestructionObserver(WebContentsImpl* web_contents) {
avia8cf81982016-08-12 20:28:462599 destruction_observers_.erase(web_contents);
[email protected]7fff43e2013-05-21 20:21:102600}
2601
[email protected]b172aee2012-04-10 17:05:262602void WebContentsImpl::AddObserver(WebContentsObserver* observer) {
[email protected]be1f56ab2011-12-22 06:55:312603 observers_.AddObserver(observer);
2604}
2605
[email protected]b172aee2012-04-10 17:05:262606void WebContentsImpl::RemoveObserver(WebContentsObserver* observer) {
[email protected]be1f56ab2011-12-22 06:55:312607 observers_.RemoveObserver(observer);
2608}
2609
[email protected]948481d2014-06-11 18:32:222610std::set<RenderWidgetHostView*>
2611WebContentsImpl::GetRenderWidgetHostViewsInTree() {
2612 std::set<RenderWidgetHostView*> set;
Carlos IL07f282f2020-05-15 22:58:292613 for (RenderFrameHost* rfh : GetAllFrames()) {
2614 if (RenderWidgetHostView* rwhv = static_cast<RenderFrameHostImpl*>(rfh)
2615 ->frame_tree_node()
2616 ->render_manager()
2617 ->GetRenderWidgetHostView()) {
lfgf949e0e2017-05-10 22:18:112618 set.insert(rwhv);
dchengafb53e22016-02-04 08:11:082619 }
[email protected]948481d2014-06-11 18:32:222620 }
[email protected]de3c5d82014-05-28 22:12:592621 return set;
2622}
2623
Adithya Srinivasane6e7f842019-12-16 18:41:362624std::set<RenderWidgetHostView*>
2625WebContentsImpl::GetRenderWidgetHostViewsInWebContentsTree() {
2626 std::set<RenderWidgetHostView*> result;
2627 GetRenderWidgetHostViewsInWebContentsTree(result);
2628 return result;
2629}
2630
2631void WebContentsImpl::GetRenderWidgetHostViewsInWebContentsTree(
2632 std::set<RenderWidgetHostView*>& result) {
2633 std::set<RenderWidgetHostView*> views = GetRenderWidgetHostViewsInTree();
2634 result.insert(views.begin(), views.end());
2635 for (auto* inner_web_contents : GetInnerWebContents()) {
2636 static_cast<WebContentsImpl*>(inner_web_contents)
2637 ->GetRenderWidgetHostViewsInWebContentsTree(result);
2638 }
2639}
2640
[email protected]b172aee2012-04-10 17:05:262641void WebContentsImpl::Activate() {
[email protected]d5f942ba2008-09-26 19:30:342642 if (delegate_)
2643 delegate_->ActivateContents(this);
2644}
2645
avi3e4b8512015-11-11 01:55:492646void WebContentsImpl::LostCapture(RenderWidgetHostImpl* render_widget_host) {
2647 if (!RenderViewHostImpl::From(render_widget_host))
2648 return;
2649
[email protected]63954792011-07-11 04:17:482650 if (delegate_)
2651 delegate_->LostCapture();
2652}
2653
Yue Ru Sun979c4b162019-11-06 16:11:042654ukm::SourceId
2655WebContentsImpl::GetUkmSourceIdForLastCommittedSourceIncludingSameDocument()
2656 const {
2657 return last_committed_source_id_including_same_document_;
2658}
2659
Ahmed Fakhryfaa32d22018-10-05 15:56:162660void WebContentsImpl::SetTopControlsShownRatio(
2661 RenderWidgetHostImpl* render_widget_host,
2662 float ratio) {
2663 if (!delegate_)
2664 return;
2665
2666 RenderFrameHostImpl* rfh = GetMainFrame();
2667 if (!rfh || render_widget_host != rfh->GetRenderWidgetHost())
2668 return;
2669
2670 delegate_->SetTopControlsShownRatio(this, ratio);
Ahmed Fakhry58e6ef542018-09-04 18:05:382671}
2672
Ahmed Fakhry58e6ef542018-09-04 18:05:382673void WebContentsImpl::SetTopControlsGestureScrollInProgress(bool in_progress) {
2674 if (delegate_)
2675 delegate_->SetTopControlsGestureScrollInProgress(in_progress);
2676}
2677
lazyboy63f5b312015-11-23 20:19:522678void WebContentsImpl::RenderWidgetCreated(
2679 RenderWidgetHostImpl* render_widget_host) {
2680 created_widgets_.insert(render_widget_host);
2681}
2682
[email protected]b24b68a2012-09-24 21:57:262683void WebContentsImpl::RenderWidgetDeleted(
2684 RenderWidgetHostImpl* render_widget_host) {
lazyboy63f5b312015-11-23 20:19:522685 // Note that |is_being_destroyed_| can be true at this point as
2686 // ~WebContentsImpl() calls RFHM::ClearRFHsPendingShutdown(), which might lead
2687 // us here.
2688 created_widgets_.erase(render_widget_host);
[email protected]b24b68a2012-09-24 21:57:262689
lazyboy63f5b312015-11-23 20:19:522690 if (is_being_destroyed_)
2691 return;
[email protected]44470a22013-01-24 01:21:542692
2693 if (render_widget_host &&
alexmosc9e76ec2016-05-16 22:59:372694 render_widget_host->GetRoutingID() == fullscreen_widget_routing_id_ &&
2695 render_widget_host->GetProcess()->GetID() ==
2696 fullscreen_widget_process_id_) {
[email protected]4aebbca2013-09-17 22:26:492697 if (delegate_ && delegate_->EmbedsFullscreenWidget())
mlamouri7a78d6fd2015-01-17 13:23:532698 delegate_->ExitFullscreenModeForTab(this);
ericwilligersde386342016-10-19 02:35:092699 for (auto& observer : observers_)
2700 observer.DidDestroyFullscreenWidget();
alexmosc9e76ec2016-05-16 22:59:372701 fullscreen_widget_process_id_ = ChildProcessHost::kInvalidUniqueID;
[email protected]44470a22013-01-24 01:21:542702 fullscreen_widget_routing_id_ = MSG_ROUTING_NONE;
[email protected]0b431992014-06-24 00:08:032703 if (fullscreen_widget_had_focus_at_shutdown_)
2704 view_->RestoreFocus();
[email protected]44470a22013-01-24 01:21:542705 }
lfgbee1e0a2016-06-08 21:24:212706
lfg7d4caad2017-03-22 09:01:452707 if (render_widget_host == mouse_lock_widget_)
lfgff8dfc8d2016-10-21 17:42:322708 LostMouseLock(mouse_lock_widget_);
Joe Downing192998b22018-03-22 15:51:362709
Joe Downingc1a57efe2018-05-09 18:23:542710 CancelKeyboardLock(render_widget_host);
[email protected]b24b68a2012-09-24 21:57:262711}
2712
miu9e14e492014-10-25 02:39:042713void WebContentsImpl::RenderWidgetGotFocus(
2714 RenderWidgetHostImpl* render_widget_host) {
calamity7fe55ce2015-04-10 03:59:372715 // Notify the observers if an embedded fullscreen widget was focused.
2716 if (delegate_ && render_widget_host && delegate_->EmbedsFullscreenWidget() &&
2717 render_widget_host->GetView() == GetFullscreenRenderWidgetHostView()) {
Michael Thiessen896405db2017-07-20 17:52:322718 NotifyWebContentsFocused(render_widget_host);
calamity7fe55ce2015-04-10 03:59:372719 }
miu9e14e492014-10-25 02:39:042720}
2721
zijiehe3bee08e22017-05-17 04:14:462722void WebContentsImpl::RenderWidgetLostFocus(
2723 RenderWidgetHostImpl* render_widget_host) {
Michael Thiessen896405db2017-07-20 17:52:322724 // Notify the observers if an embedded fullscreen widget lost focus.
2725 if (delegate_ && render_widget_host && delegate_->EmbedsFullscreenWidget() &&
2726 render_widget_host->GetView() == GetFullscreenRenderWidgetHostView()) {
2727 NotifyWebContentsLostFocus(render_widget_host);
2728 }
zijiehe3bee08e22017-05-17 04:14:462729}
2730
estaded0a0c992015-01-21 14:12:062731void WebContentsImpl::RenderWidgetWasResized(
rouslan2f5993f2015-01-29 00:18:312732 RenderWidgetHostImpl* render_widget_host,
2733 bool width_changed) {
creisc014b402015-04-23 16:41:452734 RenderFrameHostImpl* rfh = GetMainFrame();
estadece344ec2015-01-23 00:12:292735 if (!rfh || render_widget_host != rfh->GetRenderWidgetHost())
estaded0a0c992015-01-21 14:12:062736 return;
estaded0a0c992015-01-21 14:12:062737
ericwilligersde386342016-10-19 02:35:092738 for (auto& observer : observers_)
2739 observer.MainFrameWasResized(width_changed);
estaded0a0c992015-01-21 14:12:062740}
2741
skyf65d9bb2017-03-24 02:26:392742KeyboardEventProcessingResult WebContentsImpl::PreHandleKeyboardEvent(
2743 const NativeWebKeyboardEvent& event) {
W. James MacLeane1187272019-11-12 03:42:482744 auto* outermost_contents = GetOutermostWebContents();
2745 // TODO(wjmaclean): Generalize this to forward all key events to the outermost
2746 // delegate's handler.
Avi Drissman97aef042020-06-30 21:04:482747 if (outermost_contents != this && IsFullscreen() &&
W. James MacLeane1187272019-11-12 03:42:482748 event.windows_key_code == ui::VKEY_ESCAPE) {
2749 // When an inner WebContents has focus and is fullscreen, redirect <esc>
2750 // key events to the outermost WebContents so it can be handled by that
2751 // WebContents' delegate.
2752 if (outermost_contents->PreHandleKeyboardEvent(event) ==
2753 KeyboardEventProcessingResult::HANDLED) {
2754 return KeyboardEventProcessingResult::HANDLED;
2755 }
2756 }
skyf65d9bb2017-03-24 02:26:392757 return delegate_ ? delegate_->PreHandleKeyboardEvent(this, event)
2758 : KeyboardEventProcessingResult::NOT_HANDLED;
[email protected]63954792011-07-11 04:17:482759}
2760
Dave Tapuska344399e2019-09-09 15:18:262761bool WebContentsImpl::HandleMouseEvent(const blink::WebMouseEvent& event) {
2762 // Handle mouse button back/forward in the browser process after the render
2763 // process is done with the event. This ensures all renderer-initiated history
2764 // navigations can be treated consistently.
2765 if (event.GetType() == blink::WebInputEvent::Type::kMouseUp) {
2766 WebContentsImpl* outermost = GetOutermostWebContents();
2767 if (event.button == blink::WebPointerProperties::Button::kBack &&
2768 outermost->controller_.CanGoBack()) {
2769 outermost->controller_.GoBack();
2770 return true;
2771 } else if (event.button == blink::WebPointerProperties::Button::kForward &&
2772 outermost->controller_.CanGoForward()) {
2773 outermost->controller_.GoForward();
2774 return true;
2775 }
2776 }
2777 return false;
2778}
2779
Scott Violetd5caa872018-10-16 22:00:512780bool WebContentsImpl::HandleKeyboardEvent(const NativeWebKeyboardEvent& event) {
[email protected]0b094002014-08-20 18:28:102781 if (browser_plugin_embedder_ &&
2782 browser_plugin_embedder_->HandleKeyboardEvent(event)) {
Scott Violetd5caa872018-10-16 22:00:512783 return true;
[email protected]0b094002014-08-20 18:28:102784 }
Scott Violetd5caa872018-10-16 22:00:512785 return delegate_ && delegate_->HandleKeyboardEvent(this, event);
[email protected]63954792011-07-11 04:17:482786}
2787
[email protected]d92026ef2014-03-03 21:04:132788bool WebContentsImpl::HandleWheelEvent(
[email protected]180ef242013-11-07 06:50:462789 const blink::WebMouseWheelEvent& event) {
[email protected]bccc4472013-04-18 16:37:192790#if !defined(OS_MACOSX)
lanweia93644f2015-01-21 22:00:332791 // On platforms other than Mac, control+mousewheel may change zoom. On Mac,
2792 // this isn't done for two reasons:
[email protected]bccc4472013-04-18 16:37:192793 // -the OS already has a gesture to do this through pinch-zoom
2794 // -if a user starts an inertial scroll, let's go, and presses control
2795 // (i.e. control+tab) then the OS's buffered scroll events will come in
2796 // with control key set which isn't what the user wants
Blink Reformat1c4d759e2017-04-09 16:34:542797 if (delegate_ && event.wheel_ticks_y &&
Ehsan Karamad0f83cb112018-09-06 22:07:362798 event.event_action == blink::WebMouseWheelEvent::EventAction::kPageZoom) {
w.shackleton49bcd392016-01-06 17:38:222799 // Count only integer cumulative scrolls as zoom events; this handles
2800 // smooth scroll and regular scroll device behavior.
Blink Reformat1c4d759e2017-04-09 16:34:542801 zoom_scroll_remainder_ += event.wheel_ticks_y;
w.shackleton49bcd392016-01-06 17:38:222802 int whole_zoom_scroll_remainder_ = std::lround(zoom_scroll_remainder_);
2803 zoom_scroll_remainder_ -= whole_zoom_scroll_remainder_;
2804 if (whole_zoom_scroll_remainder_ != 0) {
2805 delegate_->ContentsZoomChange(whole_zoom_scroll_remainder_ > 0);
2806 }
[email protected]fb3f066e2013-02-12 19:12:522807 return true;
2808 }
[email protected]bccc4472013-04-18 16:37:192809#endif
[email protected]fb3f066e2013-02-12 19:12:522810 return false;
2811}
2812
[email protected]04bce562014-01-30 05:34:542813bool WebContentsImpl::PreHandleGestureEvent(
2814 const blink::WebGestureEvent& event) {
[email protected]28042d32014-02-03 14:10:032815 return delegate_ && delegate_->PreHandleGestureEvent(this, event);
[email protected]04bce562014-01-30 05:34:542816}
2817
kenrb2a565f82015-09-02 20:24:592818RenderWidgetHostInputEventRouter* WebContentsImpl::GetInputEventRouter() {
wjmacleane31234f2015-12-14 17:20:452819 if (!is_being_destroyed_ && GetOuterWebContents())
2820 return GetOuterWebContents()->GetInputEventRouter();
2821
2822 if (!rwh_input_event_router_.get() && !is_being_destroyed_)
kenrb2a565f82015-09-02 20:24:592823 rwh_input_event_router_.reset(new RenderWidgetHostInputEventRouter);
2824 return rwh_input_event_router_.get();
2825}
2826
alexmos3fcd0ca2015-10-23 18:18:332827void WebContentsImpl::ReplicatePageFocus(bool is_focused) {
2828 // Focus loss may occur while this WebContents is being destroyed. Don't
2829 // send the message in this case, as the main frame's RenderFrameHost and
2830 // other state has already been cleared.
2831 if (is_being_destroyed_)
2832 return;
2833
2834 frame_tree_.ReplicatePageFocus(is_focused);
2835}
2836
alexmosc4cbacb2015-11-21 01:29:222837RenderWidgetHostImpl* WebContentsImpl::GetFocusedRenderWidgetHost(
2838 RenderWidgetHostImpl* receiving_widget) {
alexmosc4cbacb2015-11-21 01:29:222839 // Events for widgets other than the main frame (e.g., popup menus) should be
2840 // forwarded directly to the widget they arrived on.
2841 if (receiving_widget != GetMainFrame()->GetRenderWidgetHost())
2842 return receiving_widget;
alexmosc4d183e2015-11-06 00:46:522843
paulmeyerfeafc2d2017-04-25 21:46:402844 // If the focused WebContents is a guest WebContents, then get the focused
2845 // frame in the embedder WebContents instead.
W. James MacLean13d834d2019-12-04 16:06:452846 FrameTreeNode* focused_frame =
2847 GetFocusedWebContents()->frame_tree_.GetFocusedFrame();
lfg1453e412017-04-11 00:48:502848
alexmosc4d183e2015-11-06 00:46:522849 if (!focused_frame)
alexmosc4cbacb2015-11-21 01:29:222850 return receiving_widget;
alexmosc4d183e2015-11-06 00:46:522851
alexmos732ca3a2015-12-08 02:01:022852 // The view may be null if a subframe's renderer process has crashed while
2853 // the subframe has focus. Drop the event in that case. Do not give
2854 // it to the main frame, so that the user doesn't unexpectedly type into the
2855 // wrong frame if a focused subframe renderer crashes while they type.
2856 RenderWidgetHostView* view = focused_frame->current_frame_host()->GetView();
2857 if (!view)
2858 return nullptr;
2859
2860 return RenderWidgetHostImpl::From(view->GetRenderWidgetHost());
alexmosc4d183e2015-11-06 00:46:522861}
2862
avallee9993fca2016-11-17 06:16:502863RenderWidgetHostImpl* WebContentsImpl::GetRenderWidgetHostWithPageFocus() {
lfg1453e412017-04-11 00:48:502864 WebContentsImpl* focused_web_contents = GetFocusedWebContents();
2865
lfg1453e412017-04-11 00:48:502866 return focused_web_contents->GetMainFrame()->GetRenderWidgetHost();
avallee9993fca2016-11-17 06:16:502867}
2868
Avi Drissmand1d25652020-03-06 21:28:582869bool WebContentsImpl::CanEnterFullscreenMode() {
Avi Drissman33972942020-06-19 14:16:012870 // It's possible that this WebContents was spawned while blocking UI was on
2871 // the screen, or that it was downstream from a WebContents when UI was
2872 // blocked. Therefore, disqualify it from fullscreen if it or any upstream
2873 // WebContents has an active blocker.
2874 auto openers = GetAllOpeningWebContents(this);
2875 return std::all_of(openers.begin(), openers.end(), [](auto* opener) {
2876 return opener->fullscreen_blocker_count_ == 0;
2877 });
Avi Drissmand1d25652020-03-06 21:28:582878}
2879
Dave Tapuska3ed44192018-05-01 18:53:302880void WebContentsImpl::EnterFullscreenMode(
Mike Wasserman4ca09792020-05-29 17:44:432881 RenderFrameHost* requesting_frame,
Dave Tapuskaa4189512019-10-15 20:27:342882 const blink::mojom::FullscreenOptions& options) {
Avi Drissmand1d25652020-03-06 21:28:582883 DCHECK(CanEnterFullscreenMode());
2884
mlamouri7a78d6fd2015-01-17 13:23:532885 // This method is being called to enter renderer-initiated fullscreen mode.
2886 // Make sure any existing fullscreen widget is shut down first.
[email protected]4aebbca2013-09-17 22:26:492887 RenderWidgetHostView* const widget_view = GetFullscreenRenderWidgetHostView();
avi85a4cef2015-12-18 20:13:372888 if (widget_view) {
2889 RenderWidgetHostImpl::From(widget_view->GetRenderWidgetHost())
2890 ->ShutdownAndDestroyWidget(true);
2891 }
[email protected]4aebbca2013-09-17 22:26:492892
Joe Downing13dd76b2018-04-09 18:32:152893 if (delegate_) {
Mike Wasserman4ca09792020-05-29 17:44:432894 delegate_->EnterFullscreenModeForTab(requesting_frame, options);
mlamouri7a78d6fd2015-01-17 13:23:532895
Joe Downing13dd76b2018-04-09 18:32:152896 if (keyboard_lock_widget_)
2897 delegate_->RequestKeyboardLock(this, esc_key_locked_);
2898 }
2899
ericwilligersde386342016-10-19 02:35:092900 for (auto& observer : observers_)
Avi Drissman97aef042020-06-30 21:04:482901 observer.DidToggleFullscreenModeForTab(IsFullscreen(), false);
Avi Drissman33972942020-06-19 14:16:012902
2903 FullscreenContentsSet(GetBrowserContext())->insert(this);
mlamouri7a78d6fd2015-01-17 13:23:532904}
2905
bokanece34a82016-01-28 19:49:462906void WebContentsImpl::ExitFullscreenMode(bool will_cause_resize) {
mlamouri7a78d6fd2015-01-17 13:23:532907 // This method is being called to leave renderer-initiated fullscreen mode.
2908 // Make sure any existing fullscreen widget is shut down first.
2909 RenderWidgetHostView* const widget_view = GetFullscreenRenderWidgetHostView();
avi85a4cef2015-12-18 20:13:372910 if (widget_view) {
2911 RenderWidgetHostImpl::From(widget_view->GetRenderWidgetHost())
2912 ->ShutdownAndDestroyWidget(true);
2913 }
mlamouri7a78d6fd2015-01-17 13:23:532914
Joe Downing13dd76b2018-04-09 18:32:152915 if (delegate_) {
mlamouri7a78d6fd2015-01-17 13:23:532916 delegate_->ExitFullscreenModeForTab(this);
[email protected]657c8e02014-03-19 19:18:372917
Joe Downing13dd76b2018-04-09 18:32:152918 if (keyboard_lock_widget_)
2919 delegate_->CancelKeyboardLockRequest(this);
2920 }
2921
bokanece34a82016-01-28 19:49:462922 // The fullscreen state is communicated to the renderer through a resize
2923 // message. If the change in fullscreen state doesn't cause a view resize
2924 // then we must ensure web contents exit the fullscreen state by explicitly
2925 // sending a resize message. This is required for the situation of the browser
2926 // moving the view into a "browser fullscreen" state and then the contents
2927 // entering "tab fullscreen". Exiting the contents "tab fullscreen" then won't
2928 // have the side effect of the view resizing, hence the explicit call here is
2929 // required.
2930 if (!will_cause_resize) {
2931 if (RenderWidgetHostView* rwhv = GetRenderWidgetHostView()) {
2932 if (RenderWidgetHost* render_widget_host = rwhv->GetRenderWidgetHost())
Fady Samuel0b911822018-04-25 13:22:162933 render_widget_host->SynchronizeVisualProperties();
bokanece34a82016-01-28 19:49:462934 }
scheib3dcb6c932015-02-23 10:55:582935 }
2936
Arthur Sonzogni929d29f2018-09-17 14:19:442937 current_fullscreen_frame_ = nullptr;
Becca Hughese9e27952018-06-25 17:08:272938
ericwilligersde386342016-10-19 02:35:092939 for (auto& observer : observers_) {
Avi Drissman97aef042020-06-30 21:04:482940 observer.DidToggleFullscreenModeForTab(IsFullscreen(), will_cause_resize);
ericwilligersde386342016-10-19 02:35:092941 }
Becca Hughesd11d6502018-07-31 17:01:282942
2943 if (display_cutout_host_impl_)
2944 display_cutout_host_impl_->DidExitFullscreen();
Avi Drissman33972942020-06-19 14:16:012945
2946 FullscreenContentsSet(GetBrowserContext())->erase(this);
[email protected]8a5e0ca2011-08-25 06:30:472947}
2948
Becca Hughesfd5d8f892018-06-14 18:23:362949void WebContentsImpl::FullscreenStateChanged(RenderFrameHost* rfh,
2950 bool is_fullscreen) {
Arthur Sonzogni929d29f2018-09-17 14:19:442951 RenderFrameHostImpl* frame = static_cast<RenderFrameHostImpl*>(rfh);
Becca Hughesfd5d8f892018-06-14 18:23:362952
2953 if (is_fullscreen) {
Jan Wilken Dörrie531be7ca2019-06-07 10:05:572954 if (!base::Contains(fullscreen_frames_, frame)) {
Arthur Sonzogni929d29f2018-09-17 14:19:442955 fullscreen_frames_.insert(frame);
2956 FullscreenFrameSetUpdated();
Becca Hughesfd5d8f892018-06-14 18:23:362957 }
Arthur Sonzogni929d29f2018-09-17 14:19:442958 return;
2959 }
Becca Hughesfd5d8f892018-06-14 18:23:362960
Arthur Sonzogni929d29f2018-09-17 14:19:442961 // If |frame| is no longer in fullscreen, remove it and any descendants.
2962 // See https://fullscreen.spec.whatwg.org.
Arthur Sonzogni969a794e2018-10-11 08:56:092963 size_t size_before_deletion = fullscreen_frames_.size();
2964 base::EraseIf(fullscreen_frames_, [&](RenderFrameHostImpl* current) {
2965 return (current == frame || current->IsDescendantOf(frame));
Arthur Sonzogni929d29f2018-09-17 14:19:442966 });
2967
Arthur Sonzogni969a794e2018-10-11 08:56:092968 if (size_before_deletion != fullscreen_frames_.size())
Arthur Sonzogni929d29f2018-09-17 14:19:442969 FullscreenFrameSetUpdated();
2970}
2971
2972void WebContentsImpl::FullscreenFrameSetUpdated() {
2973 if (fullscreen_frames_.empty()) {
2974 current_fullscreen_frame_ = nullptr;
2975 return;
Becca Hughesfd5d8f892018-06-14 18:23:362976 }
2977
Arthur Sonzogni929d29f2018-09-17 14:19:442978 // Find the current fullscreen frame and call the observers.
2979 // If frame A is fullscreen, then frame B goes into inner fullscreen, then B
2980 // exits fullscreen - that will result in A being fullscreen.
2981 RenderFrameHostImpl* new_fullscreen_frame = *std::max_element(
2982 fullscreen_frames_.begin(), fullscreen_frames_.end(), FrameCompareDepth);
Becca Hughesfd5d8f892018-06-14 18:23:362983
Arthur Sonzogni929d29f2018-09-17 14:19:442984 // If we have already notified observers about this frame then we should not
2985 // fire the observers again.
2986 if (new_fullscreen_frame == current_fullscreen_frame_)
2987 return;
2988 current_fullscreen_frame_ = new_fullscreen_frame;
Becca Hughesfd5d8f892018-06-14 18:23:362989
Arthur Sonzogni929d29f2018-09-17 14:19:442990 for (auto& observer : observers_)
2991 observer.DidAcquireFullscreen(new_fullscreen_frame);
Becca Hughesfd5d8f892018-06-14 18:23:362992
Arthur Sonzogni929d29f2018-09-17 14:19:442993 if (display_cutout_host_impl_)
2994 display_cutout_host_impl_->DidAcquireFullscreen(new_fullscreen_frame);
Becca Hughesfd5d8f892018-06-14 18:23:362995}
2996
Collin Baker989e0882019-11-01 01:27:172997void WebContentsImpl::UpdateVisibilityAndNotifyPageAndView(
2998 Visibility new_visibility) {
Collin Baker989e0882019-11-01 01:27:172999 // Only hide the page if there are no entities capturing screenshots
Collin Bakerc5259962019-11-14 17:51:583000 // or video (e.g. mirroring). If there are, apply the correct state of
3001 // kHidden or kHiddenButPainting.
3002 PageVisibilityState page_visibility;
3003 if (new_visibility == Visibility::VISIBLE || visible_capturer_count_ > 0)
3004 page_visibility = PageVisibilityState::kVisible;
3005 else if (hidden_capturer_count_ > 0)
3006 page_visibility = PageVisibilityState::kHiddenButPainting;
3007 else
3008 page_visibility = PageVisibilityState::kHidden;
Collin Baker989e0882019-11-01 01:27:173009 // If there are entities in Picture-in-Picture mode, don't activate
3010 // the "disable rendering" optimization.
Collin Bakerc5259962019-11-14 17:51:583011 const bool view_is_visible =
3012 page_visibility != PageVisibilityState::kHidden ||
3013 HasPictureInPictureVideo();
Collin Baker989e0882019-11-01 01:27:173014
Collin Bakerc5259962019-11-14 17:51:583015 if (page_visibility != PageVisibilityState::kHidden) {
Dana Friedcf3bc8e2019-11-13 20:00:243016 // We cannot show a page or capture video unless there is a valid renderer
3017 // associated with this web contents. The navigation controller for this
3018 // page must be set to active (allowing navigation to complete, a renderer
3019 // and its associated views to be created, etc.) if any of these conditions
3020 // holds.
3021 //
3022 // Previously, it was possible for browser-side code to try to capture video
3023 // from a restored tab (for a variety of reasons, including the browser
3024 // creating preview thumbnails) and the tab would never actually load. By
Collin Bakerc5259962019-11-14 17:51:583025 // keying this behavior off of |page_visibility| instead of just
Dana Friedcf3bc8e2019-11-13 20:00:243026 // |new_visibility| we avoid this case. See crbug.com/1020782 for more
3027 // context.
3028 controller_.SetActive(true);
3029
3030 // This shows the Page before showing the individual RenderWidgets, as
3031 // RenderWidgets will work to produce compositor frames and handle input
3032 // as soon as they are shown. But the Page and other classes do not expect
3033 // to be producing frames when the Page is hidden. So we make sure the Page
3034 // is shown first.
Yuzu Saijo232076f2020-05-29 07:14:023035 for (auto* rvh : GetRenderViewHostsIncludingBackForwardCached()) {
3036 rvh->SetVisibility(page_visibility);
Yuzu Saijobe024af02020-05-11 07:35:563037 }
Collin Baker98457b52019-11-06 21:34:293038 }
Collin Baker989e0882019-11-01 01:27:173039
3040 // |GetRenderWidgetHostView()| can be null if the user middle clicks a link to
3041 // open a tab in the background, then closes the tab before selecting it.
3042 // This is because closing the tab calls WebContentsImpl::Destroy(), which
3043 // removes the |GetRenderViewHost()|; then when we actually destroy the
3044 // window, OnWindowPosChanged() notices and calls WasHidden() (which
3045 // calls us).
3046 if (auto* view = GetRenderWidgetHostView()) {
3047 if (view_is_visible) {
3048 view->Show();
Collin Baker989e0882019-11-01 01:27:173049 } else if (new_visibility == Visibility::HIDDEN) {
3050 view->Hide();
3051 } else {
3052 view->WasOccluded();
3053 }
3054 }
3055
Carlos IL07f282f2020-05-15 22:58:293056 SetVisibilityForChildViews(view_is_visible);
Collin Baker989e0882019-11-01 01:27:173057
3058 // Make sure to call SetVisibilityAndNotifyObservers(VISIBLE) before notifying
3059 // the CrossProcessFrameConnector.
3060 if (new_visibility == Visibility::VISIBLE) {
3061 last_active_time_ = base::TimeTicks::Now();
3062 SetVisibilityAndNotifyObservers(new_visibility);
3063 }
3064
Collin Bakerc5259962019-11-14 17:51:583065 if (page_visibility == PageVisibilityState::kHidden) {
Collin Baker989e0882019-11-01 01:27:173066 // Similar to when showing the page, we only hide the page after
3067 // hiding the individual RenderWidgets.
Yuzu Saijo232076f2020-05-29 07:14:023068 for (auto* rvh : GetRenderViewHostsIncludingBackForwardCached()) {
3069 rvh->SetVisibility(page_visibility);
Yuzu Saijobe024af02020-05-11 07:35:563070 }
3071
Collin Baker989e0882019-11-01 01:27:173072 } else {
3073 for (FrameTreeNode* node : frame_tree_.Nodes()) {
3074 RenderFrameProxyHost* parent = node->render_manager()->GetProxyToParent();
3075 if (!parent)
3076 continue;
3077
3078 parent->cross_process_frame_connector()->DelegateWasShown();
3079 }
3080 }
3081
3082 if (new_visibility != Visibility::VISIBLE)
3083 SetVisibilityAndNotifyObservers(new_visibility);
3084}
3085
Clark DuValla0e42992018-09-17 17:00:113086#if defined(OS_ANDROID)
3087void WebContentsImpl::UpdateUserGestureCarryoverInfo() {
3088 if (delegate_)
3089 delegate_->UpdateUserGestureCarryoverInfo(this);
3090}
3091#endif
3092
Avi Drissman97aef042020-06-30 21:04:483093bool WebContentsImpl::IsFullscreen() {
[email protected]199bba6e2012-04-04 16:19:383094 return delegate_ ? delegate_->IsFullscreenForTabOrPending(this) : false;
[email protected]5d5f7af2011-10-01 01:38:123095}
3096
Malay Keshav87c42c02019-01-15 08:37:473097bool WebContentsImpl::ShouldShowStaleContentOnEviction() {
3098 return GetDelegate() && GetDelegate()->ShouldShowStaleContentOnEviction(this);
3099}
3100
danakj45d42452020-04-08 17:24:183101blink::mojom::DisplayMode WebContentsImpl::GetDisplayMode() const {
mikhail.pozdnyakovc0e251b2015-04-15 06:51:123102 return delegate_ ? delegate_->GetDisplayMode(this)
Eric Willigers052f0432019-10-04 04:06:573103 : blink::mojom::DisplayMode::kBrowser;
mikhail.pozdnyakovc0e251b2015-04-15 06:51:123104}
3105
avic3aa8422015-11-09 20:57:223106void WebContentsImpl::RequestToLockMouse(
3107 RenderWidgetHostImpl* render_widget_host,
3108 bool user_gesture,
lfgad8244352016-07-13 16:55:513109 bool last_unlocked_by_target,
3110 bool privileged) {
lfgf0cd46e2017-01-04 00:05:233111 for (WebContentsImpl* current = this; current;
3112 current = current->GetOuterWebContents()) {
3113 if (current->mouse_lock_widget_) {
James Hollyerd5c9de462020-03-10 19:02:453114 render_widget_host->GotResponseToLockMouseRequest(
3115 blink::mojom::PointerLockResult::kAlreadyLocked);
lfgf0cd46e2017-01-04 00:05:233116 return;
3117 }
[email protected]e9621112011-10-17 05:38:373118 }
avic3aa8422015-11-09 20:57:223119
lfgad8244352016-07-13 16:55:513120 if (privileged) {
lfgf0cd46e2017-01-04 00:05:233121 DCHECK(!GetOuterWebContents());
lfgad8244352016-07-13 16:55:513122 mouse_lock_widget_ = render_widget_host;
James Hollyerd5c9de462020-03-10 19:02:453123 render_widget_host->GotResponseToLockMouseRequest(
3124 blink::mojom::PointerLockResult::kSuccess);
lfgad8244352016-07-13 16:55:513125 return;
3126 }
3127
lfgbee1e0a2016-06-08 21:24:213128 bool widget_in_frame_tree = false;
3129 for (FrameTreeNode* node : frame_tree_.Nodes()) {
3130 if (node->current_frame_host()->GetRenderWidgetHost() ==
3131 render_widget_host) {
3132 widget_in_frame_tree = true;
3133 break;
3134 }
3135 }
3136
3137 if (widget_in_frame_tree && delegate_) {
lfgf0cd46e2017-01-04 00:05:233138 for (WebContentsImpl* current = this; current;
3139 current = current->GetOuterWebContents()) {
3140 current->mouse_lock_widget_ = render_widget_host;
3141 }
3142
avic3aa8422015-11-09 20:57:223143 delegate_->RequestToLockMouse(this, user_gesture, last_unlocked_by_target);
lfgbee1e0a2016-06-08 21:24:213144 } else {
James Hollyerd5c9de462020-03-10 19:02:453145 render_widget_host->GotResponseToLockMouseRequest(
3146 blink::mojom::PointerLockResult::kWrongDocument);
lfgbee1e0a2016-06-08 21:24:213147 }
[email protected]e9621112011-10-17 05:38:373148}
3149
avi3e4b8512015-11-11 01:55:493150void WebContentsImpl::LostMouseLock(RenderWidgetHostImpl* render_widget_host) {
lfgbee1e0a2016-06-08 21:24:213151 CHECK(mouse_lock_widget_);
lfgf0cd46e2017-01-04 00:05:233152
3153 if (mouse_lock_widget_->delegate()->GetAsWebContents() != this)
3154 return mouse_lock_widget_->delegate()->LostMouseLock(render_widget_host);
3155
lfgbee1e0a2016-06-08 21:24:213156 mouse_lock_widget_->SendMouseLockLost();
lfgf0cd46e2017-01-04 00:05:233157 for (WebContentsImpl* current = this; current;
3158 current = current->GetOuterWebContents()) {
3159 current->mouse_lock_widget_ = nullptr;
3160 }
avi3e4b8512015-11-11 01:55:493161
[email protected]e9621112011-10-17 05:38:373162 if (delegate_)
3163 delegate_->LostMouseLock();
3164}
3165
lfgad8244352016-07-13 16:55:513166bool WebContentsImpl::HasMouseLock(RenderWidgetHostImpl* render_widget_host) {
3167 // To verify if the mouse is locked, the mouse_lock_widget_ needs to be
3168 // assigned to the widget that requested the mouse lock, and the top-level
3169 // platform RenderWidgetHostView needs to hold the mouse lock from the OS.
Lucas Gadania0ea0172018-09-20 18:31:373170 auto* widget_host = GetTopLevelRenderWidgetHostView();
3171 return mouse_lock_widget_ == render_widget_host && widget_host &&
3172 widget_host->IsMouseLocked();
lfgad8244352016-07-13 16:55:513173}
3174
lfg84763c92017-02-16 18:55:153175RenderWidgetHostImpl* WebContentsImpl::GetMouseLockWidget() {
Lucas Gadania0ea0172018-09-20 18:31:373176 auto* widget_host = GetTopLevelRenderWidgetHostView();
3177 if ((widget_host && widget_host->IsMouseLocked()) ||
lfge2176fb2017-03-26 00:45:473178 (GetFullscreenRenderWidgetHostView() &&
Lucas Gadania0ea0172018-09-20 18:31:373179 GetFullscreenRenderWidgetHostView()->IsMouseLocked())) {
lfg84763c92017-02-16 18:55:153180 return mouse_lock_widget_;
Lucas Gadania0ea0172018-09-20 18:31:373181 }
lfg84763c92017-02-16 18:55:153182
3183 return nullptr;
3184}
3185
Joe Downing192998b22018-03-22 15:51:363186bool WebContentsImpl::RequestKeyboardLock(
Joe Downing13dd76b2018-04-09 18:32:153187 RenderWidgetHostImpl* render_widget_host,
3188 bool esc_key_locked) {
Joe Downing192998b22018-03-22 15:51:363189 DCHECK(render_widget_host);
Joe Downing192998b22018-03-22 15:51:363190 if (render_widget_host->delegate()->GetAsWebContents() != this) {
3191 NOTREACHED();
3192 return false;
3193 }
3194
3195 // KeyboardLock is only supported when called by the top-level browsing
3196 // context and is not supported in embedded content scenarios.
3197 if (GetOuterWebContents())
3198 return false;
3199
Joe Downing13dd76b2018-04-09 18:32:153200 esc_key_locked_ = esc_key_locked;
Joe Downing192998b22018-03-22 15:51:363201 keyboard_lock_widget_ = render_widget_host;
3202
Joe Downing13dd76b2018-04-09 18:32:153203 if (delegate_)
3204 delegate_->RequestKeyboardLock(this, esc_key_locked_);
Joe Downing192998b22018-03-22 15:51:363205 return true;
3206}
3207
3208void WebContentsImpl::CancelKeyboardLock(
3209 RenderWidgetHostImpl* render_widget_host) {
3210 if (!keyboard_lock_widget_ || render_widget_host != keyboard_lock_widget_)
3211 return;
3212
3213 RenderWidgetHostImpl* old_keyboard_lock_widget = keyboard_lock_widget_;
3214 keyboard_lock_widget_ = nullptr;
3215
Joe Downing13dd76b2018-04-09 18:32:153216 if (delegate_)
3217 delegate_->CancelKeyboardLockRequest(this);
3218
Joe Downing192998b22018-03-22 15:51:363219 old_keyboard_lock_widget->CancelKeyboardLock();
3220}
3221
3222RenderWidgetHostImpl* WebContentsImpl::GetKeyboardLockWidget() {
3223 return keyboard_lock_widget_;
3224}
3225
Dave Tapuskac3344362019-02-20 17:54:283226void WebContentsImpl::OnRenderFrameProxyVisibilityChanged(
3227 blink::mojom::FrameVisibility visibility) {
3228 switch (visibility) {
3229 case blink::mojom::FrameVisibility::kRenderedInViewport:
3230 WasShown();
3231 break;
3232 case blink::mojom::FrameVisibility::kNotRendered:
3233 WasHidden();
3234 break;
3235 case blink::mojom::FrameVisibility::kRenderedOutOfViewport:
3236 WasOccluded();
3237 break;
3238 }
ekaramada4f42692016-02-11 19:48:373239}
3240
Albert J. Wong65fe64d2019-09-20 02:48:143241RenderFrameHostDelegate* WebContentsImpl::CreateNewWindow(
csharrison95f01e922017-04-24 18:52:353242 RenderFrameHost* opener,
rockot5c478a72016-09-28 23:14:183243 const mojom::CreateNewWindowParams& params,
Albert J. Wong65fe64d2019-09-20 02:48:143244 bool is_new_browsing_instance,
Sergey Kuznetsov32257a22019-02-11 20:26:503245 bool has_user_gesture,
[email protected]97714c82012-06-06 10:15:133246 SessionStorageNamespace* session_storage_namespace) {
csharrison95f01e922017-04-24 18:52:353247 DCHECK(opener);
nick5ae4d2d2017-01-06 01:18:353248
csharrison95f01e922017-04-24 18:52:353249 int render_process_id = opener->GetProcess()->GetID();
csharrison95f01e922017-04-24 18:52:353250
Albert J. Wong65fe64d2019-09-20 02:48:143251 SiteInstance* source_site_instance = opener->GetSiteInstance();
nick5ae4d2d2017-01-06 01:18:353252
Aaron Colwelleb219982020-01-16 02:17:273253 const GURL source_site_url = source_site_instance->GetSiteURL();
[email protected]fdac6ade2013-07-20 01:06:303254 const std::string& partition_id =
Aaron Colwelleb219982020-01-16 02:17:273255 GetContentClient()->browser()->GetStoragePartitionIdForSite(
3256 GetBrowserContext(), source_site_url);
3257 {
3258 StoragePartition* partition = BrowserContext::GetStoragePartitionForSite(
3259 GetBrowserContext(), source_site_url);
3260 DOMStorageContextWrapper* dom_storage_context =
3261 static_cast<DOMStorageContextWrapper*>(
3262 partition->GetDOMStorageContext());
3263 SessionStorageNamespaceImpl* session_storage_namespace_impl =
3264 static_cast<SessionStorageNamespaceImpl*>(session_storage_namespace);
3265 CHECK(session_storage_namespace_impl->IsFromContext(dom_storage_context));
3266 }
[email protected]dd6730412013-08-14 15:03:373267
Albert J. Wong1ceccef92019-10-08 08:25:203268 if (delegate_ && delegate_->IsWebContentsCreationOverridden(
3269 source_site_instance, params.window_container_type,
3270 opener->GetLastCommittedURL(), params.frame_name,
3271 params.target_url)) {
3272 return static_cast<WebContentsImpl*>(delegate_->CreateCustomWebContents(
3273 opener, source_site_instance, is_new_browsing_instance,
3274 opener->GetLastCommittedURL(), params.frame_name, params.target_url,
3275 partition_id, session_storage_namespace));
[email protected]dd6730412013-08-14 15:03:373276 }
3277
danakj22b4e8b2019-08-01 23:14:393278 bool renderer_started_hidden =
3279 params.disposition == WindowOpenDisposition::NEW_BACKGROUND_TAB;
3280
Aaron Colwelleb219982020-01-16 02:17:273281 // We usually create the new window in the same BrowsingInstance (group of
3282 // script-related windows), by passing in the current SiteInstance. However,
3283 // if the opener is being suppressed (in a non-guest), we do not provide
3284 // a SiteInstance which causes a new one to get created in its own
3285 // BrowsingInstance.
3286 bool is_guest = BrowserPluginGuest::IsGuest(this);
3287 scoped_refptr<SiteInstance> site_instance =
3288 params.opener_suppressed && !is_guest ? nullptr : source_site_instance;
3289
[email protected]dd6730412013-08-14 15:03:373290 // Create the new web contents. This will automatically create the new
3291 // WebContentsView. In the future, we may want to create the view separately.
[email protected]fc72bb12013-06-02 21:13:463292 CreateParams create_params(GetBrowserContext(), site_instance.get());
nasko48321ca32015-07-02 20:44:123293 create_params.main_frame_name = params.frame_name;
alexmos4cf2aa32015-07-15 23:40:433294 create_params.opener_render_process_id = render_process_id;
csharrison95f01e922017-04-24 18:52:353295 create_params.opener_render_frame_id = opener->GetRoutingID();
[email protected]50d326e2014-05-20 17:59:063296 create_params.opener_suppressed = params.opener_suppressed;
danakj22b4e8b2019-08-01 23:14:393297 create_params.initially_hidden = renderer_started_hidden;
[email protected]50d326e2014-05-20 17:59:063298
Albert J. Wong65fe64d2019-09-20 02:48:143299 // Even though all codepaths leading here are in response to a renderer
3300 // tryng to open a new window, if the new window ends up in a different
3301 // browsing instance, then the RenderViewHost, RenderWidgetHost,
3302 // RenderFrameHost constellation is effectively browser initiated
3303 // the opener's process will not given the routing IDs for the new
3304 // objects.
3305 create_params.renderer_initiated_creation = !is_new_browsing_instance;
Albert J. Wong1ceccef92019-10-08 08:25:203306
Albert J. Wong65fe64d2019-09-20 02:48:143307 std::unique_ptr<WebContentsImpl> new_contents;
[email protected]c4538072013-03-18 02:17:553308 if (!is_guest) {
3309 create_params.context = view_->GetNativeView();
Albert J. Wong65fe64d2019-09-20 02:48:143310 new_contents = WebContentsImpl::Create(create_params);
3311 } else {
3312 new_contents = base::WrapUnique(static_cast<WebContentsImpl*>(
3313 GetBrowserPluginGuest()->CreateNewGuestWindow(create_params)));
[email protected]c4538072013-03-18 02:17:553314 }
Albert J. Wong65fe64d2019-09-20 02:48:143315 auto* new_contents_impl = new_contents.get();
danakje6c01622018-10-05 19:39:263316
3317 new_contents_impl->GetController().SetSessionStorageNamespace(
erikchenbee5c9622018-04-27 19:30:253318 partition_id, session_storage_namespace);
[email protected]d1198fd2012-08-13 22:50:193319
alexmos646fec02015-07-25 00:11:493320 // If the new frame has a name, make sure any SiteInstances that can find
3321 // this named frame have proxies for it. Must be called after
3322 // SetSessionStorageNamespace, since this calls CreateRenderView, which uses
3323 // GetSessionStorageNamespace.
3324 if (!params.frame_name.empty())
danakje6c01622018-10-05 19:39:263325 new_contents_impl->GetRenderManager()->CreateProxiesForNewNamedFrame();
alexmos646fec02015-07-25 00:11:493326
[email protected]c4538072013-03-18 02:17:553327 // Save the window for later if we're not suppressing the opener (since it
[email protected]d70bea92013-04-05 04:23:343328 // will be shown immediately).
3329 if (!params.opener_suppressed) {
3330 if (!is_guest) {
danakje6c01622018-10-05 19:39:263331 WebContentsView* new_view = new_contents_impl->view_.get();
[email protected]bafe6cd2012-05-23 23:09:503332
[email protected]d70bea92013-04-05 04:23:343333 // TODO(brettw): It seems bogus that we have to call this function on the
3334 // newly created object and give it one of its own member variables.
danakj22b4e8b2019-08-01 23:14:393335 RenderWidgetHostView* widget_view = new_view->CreateViewForWidget(
W. James MacLean13d834d2019-12-04 16:06:453336 new_contents_impl->GetRenderViewHost()->GetWidget());
danakj22b4e8b2019-08-01 23:14:393337 if (!renderer_started_hidden) {
3338 // RenderWidgets for frames always initialize as hidden. If the renderer
3339 // created this window as visible, then we show it here.
3340 widget_view->Show();
3341 }
[email protected]d70bea92013-04-05 04:23:343342 }
[email protected]bafe6cd2012-05-23 23:09:503343 // Save the created window associated with the route so we can show it
3344 // later.
Albert J. Wongcb004632018-07-10 22:58:253345 //
3346 // TODO(ajwong): This should be keyed off the RenderFrame routing id or the
3347 // FrameTreeNode id instead of the routing id of the Widget for the main
3348 // frame. https://crbug.com/545684
Fergal Daly052873a82020-03-11 14:45:493349 int32_t main_frame_routing_id = new_contents_impl->GetMainFrame()
3350 ->GetRenderWidgetHost()
3351 ->GetRoutingID();
3352 GlobalRoutingID id(render_process_id, main_frame_routing_id);
Joel Hockey8c2011b22020-04-30 05:07:193353 pending_contents_[id] =
3354 CreatedWindow(std::move(new_contents), params.target_url);
danakje6c01622018-10-05 19:39:263355 AddDestructionObserver(new_contents_impl);
[email protected]bafe6cd2012-05-23 23:09:503356 }
3357
3358 if (delegate_) {
Bryan McQuaded2b93c02017-07-14 22:26:313359 delegate_->WebContentsCreated(this, render_process_id,
3360 opener->GetRoutingID(), params.frame_name,
danakje6c01622018-10-05 19:39:263361 params.target_url, new_contents_impl);
[email protected]bafe6cd2012-05-23 23:09:503362 }
3363
Charlie Harrisonf60dd1c2018-08-15 21:15:413364 for (auto& observer : observers_) {
danakje6c01622018-10-05 19:39:263365 observer.DidOpenRequestedURL(new_contents_impl, opener, params.target_url,
Leon Han963dc182018-11-06 05:41:483366 params.referrer.To<Referrer>(),
3367 params.disposition, ui::PAGE_TRANSITION_LINK,
Charlie Harrisonf60dd1c2018-08-15 21:15:413368 false, // started_from_context_menu
3369 true); // renderer_initiated
pnolandaae574e2017-03-06 21:04:213370 }
3371
Avi Drissmanba3b1b342017-08-29 18:16:133372 // Any new WebContents opened while this WebContents is in fullscreen can be
3373 // used to confuse the user, so drop fullscreen.
Avi Drissman1a55a9d62020-03-10 18:56:453374 base::ScopedClosureRunner fullscreen_block = ForSecurityDropFullscreen();
3375 // The new contents will be independent of this contents, so release the
3376 // fullscreen block.
3377 fullscreen_block.RunAndReset();
Avi Drissmanba3b1b342017-08-29 18:16:133378
[email protected]bafe6cd2012-05-23 23:09:503379 if (params.opener_suppressed) {
3380 // When the opener is suppressed, the original renderer cannot access the
3381 // new window. As a result, we need to show and navigate the window here.
[email protected]eda238a12012-09-07 23:44:003382 bool was_blocked = false;
erikchenbee5c9622018-04-27 19:30:253383
[email protected]eda238a12012-09-07 23:44:003384 if (delegate_) {
danakje6c01622018-10-05 19:39:263385 base::WeakPtr<WebContentsImpl> weak_new_contents =
3386 new_contents_impl->weak_factory_.GetWeakPtr();
nick0d558482017-02-13 22:27:223387
danakje6c01622018-10-05 19:39:263388 gfx::Rect initial_rect; // Report an empty initial rect.
Albert J. Wong65fe64d2019-09-20 02:48:143389 delegate_->AddNewContents(this, std::move(new_contents),
Joel Hockey891e88062020-04-30 05:38:443390 params.target_url, params.disposition,
3391 initial_rect, has_user_gesture, &was_blocked);
danakje6c01622018-10-05 19:39:263392 // The delegate may delete |new_contents_impl| during AddNewContents().
nick0d558482017-02-13 22:27:223393 if (!weak_new_contents)
Albert J. Wong65fe64d2019-09-20 02:48:143394 return nullptr;
[email protected]eda238a12012-09-07 23:44:003395 }
erikchenbee5c9622018-04-27 19:30:253396
[email protected]eda238a12012-09-07 23:44:003397 if (!was_blocked) {
Nasko Oskov83a8cf92018-10-19 14:58:563398 std::unique_ptr<NavigationController::LoadURLParams> load_params =
3399 std::make_unique<NavigationController::LoadURLParams>(
3400 params.target_url);
Nasko Oskov93e7c55c2018-12-19 01:59:293401 load_params->initiator_origin = opener->GetLastCommittedOrigin();
Aaron Colwell9dab1652019-12-09 18:29:493402 load_params->source_site_instance = source_site_instance;
Leon Han963dc182018-11-06 05:41:483403 load_params->referrer = params.referrer.To<Referrer>();
Nasko Oskov83a8cf92018-10-19 14:58:563404 load_params->transition_type = ui::PAGE_TRANSITION_LINK;
3405 load_params->is_renderer_initiated = true;
Sergey Kuznetsov32257a22019-02-11 20:26:503406 load_params->has_user_gesture = has_user_gesture;
mariakhomenkoa4971c12015-07-21 19:04:373407
3408 if (delegate_ && !is_guest &&
3409 !delegate_->ShouldResumeRequestsForCreatedWindow()) {
Nasko Oskov83a8cf92018-10-19 14:58:563410 // We are in asynchronous add new contents path, delay navigation.
3411 DCHECK(!new_contents_impl->delayed_open_url_params_);
3412 new_contents_impl->delayed_load_url_params_ = std::move(load_params);
mariakhomenkoa4971c12015-07-21 19:04:373413 } else {
Nasko Oskov83a8cf92018-10-19 14:58:563414 new_contents_impl->controller_.LoadURLWithParams(*load_params.get());
Nasko Oskov28ebd5b2018-12-07 22:29:333415 if (!is_guest)
3416 new_contents_impl->Focus();
mariakhomenkoa4971c12015-07-21 19:04:373417 }
[email protected]eda238a12012-09-07 23:44:003418 }
[email protected]bafe6cd2012-05-23 23:09:503419 }
Albert J. Wong65fe64d2019-09-20 02:48:143420 return new_contents_impl;
[email protected]bafe6cd2012-05-23 23:09:503421}
3422
Dave Tapuskad9835622020-03-10 17:32:313423void WebContentsImpl::CreateNewWidget(
3424 int32_t render_process_id,
3425 int32_t widget_route_id,
Dave Tapuska8499eec2020-03-16 17:54:303426 mojo::PendingAssociatedReceiver<blink::mojom::WidgetHost> blink_widget_host,
3427 mojo::PendingAssociatedRemote<blink::mojom::Widget> blink_widget) {
Albert J. Wongf6e13ed2018-09-18 15:25:473428 CreateNewWidget(render_process_id, widget_route_id, /*is_fullscreen=*/false,
Dave Tapuskab686c1b2020-06-09 00:40:233429 std::move(blink_widget_host), std::move(blink_widget));
[email protected]bafe6cd2012-05-23 23:09:503430}
3431
Julie Jeongeun Kimcaacf9102019-09-12 02:13:053432void WebContentsImpl::CreateNewFullscreenWidget(
3433 int32_t render_process_id,
3434 int32_t widget_route_id,
Dave Tapuska8499eec2020-03-16 17:54:303435 mojo::PendingAssociatedReceiver<blink::mojom::WidgetHost> blink_widget_host,
3436 mojo::PendingAssociatedRemote<blink::mojom::Widget> blink_widget) {
Albert J. Wongf6e13ed2018-09-18 15:25:473437 CreateNewWidget(render_process_id, widget_route_id, /*is_fullscreen=*/true,
Dave Tapuskab686c1b2020-06-09 00:40:233438 std::move(blink_widget_host), std::move(blink_widget));
[email protected]bafe6cd2012-05-23 23:09:503439}
3440
Dave Tapuskad9835622020-03-10 17:32:313441void WebContentsImpl::CreateNewWidget(
3442 int32_t render_process_id,
3443 int32_t route_id,
3444 bool is_fullscreen,
Dave Tapuska8499eec2020-03-16 17:54:303445 mojo::PendingAssociatedReceiver<blink::mojom::WidgetHost> blink_widget_host,
3446 mojo::PendingAssociatedRemote<blink::mojom::Widget> blink_widget) {
kenrba26d15092016-02-10 22:00:253447 RenderProcessHost* process = RenderProcessHost::FromID(render_process_id);
kenrbb46f86a2015-12-07 07:53:233448 // A message to create a new widget can only come from an active process for
[email protected]a8504022013-12-04 20:23:513449 // this WebContentsImpl instance. If any other process sends the request,
3450 // it is invalid and the process must be terminated.
dcheng57e39e22016-01-21 00:25:383451 if (!HasMatchingProcess(&frame_tree_, render_process_id)) {
Lukasz Anforowicz6f746282018-01-04 23:24:513452 ReceivedBadMessage(process, bad_message::WCI_NEW_WIDGET_PROCESS_MISMATCH);
[email protected]a8504022013-12-04 20:23:513453 return;
3454 }
3455
Dave Tapuskab686c1b2020-06-09 00:40:233456 RenderWidgetHostImpl* widget_host =
3457 new RenderWidgetHostImpl(this, process, route_id, IsHidden(),
3458 std::make_unique<FrameTokenMessageQueue>());
Dave Tapuska8499eec2020-03-16 17:54:303459
3460 widget_host->BindWidgetInterfaces(std::move(blink_widget_host),
3461 std::move(blink_widget));
[email protected]cfd80b02014-05-01 17:46:483462 RenderWidgetHostViewBase* widget_view =
3463 static_cast<RenderWidgetHostViewBase*>(
danakj0b246942018-09-17 21:30:273464 view_->CreateViewForChildWidget(widget_host));
[email protected]83918ec2013-01-10 15:37:193465 if (!widget_view)
3466 return;
danakj0b246942018-09-17 21:30:273467 // Fullscreen child widgets are frames, other child widgets are popups, and
3468 // popups should not get activated.
3469 if (!is_fullscreen)
3470 widget_view->SetWidgetType(WidgetType::kPopup);
[email protected]bafe6cd2012-05-23 23:09:503471 // Save the created widget associated with the route so we can show it later.
Lukasz Anforowicz09060bdf72018-08-23 15:53:173472 pending_widget_views_[GlobalRoutingID(render_process_id, route_id)] =
alexmosc2a8cec2016-05-23 22:19:533473 widget_view;
[email protected]bafe6cd2012-05-23 23:09:503474}
3475
alexmosc2a8cec2016-05-23 22:19:533476void WebContentsImpl::ShowCreatedWindow(int process_id,
nick5ae4d2d2017-01-06 01:18:353477 int main_frame_widget_route_id,
[email protected]bafe6cd2012-05-23 23:09:503478 WindowOpenDisposition disposition,
bokan107a47f2015-02-03 23:23:393479 const gfx::Rect& initial_rect,
[email protected]bafe6cd2012-05-23 23:09:503480 bool user_gesture) {
danakje6c01622018-10-05 19:39:263481 // This method is the renderer requesting an existing top level window to
3482 // show a new top level window that the renderer created. Each top level
3483 // window is associated with a WebContents. In this case it was created
3484 // earlier but showing it was deferred until the renderer requested for it
3485 // to be shown. We find that previously created WebContents here.
3486 // TODO(danakj): Why do we defer this show step until the renderer asks for it
3487 // when it will always do so. What needs to happen in the renderer before we
3488 // reach here?
Joel Hockey8c2011b22020-04-30 05:07:193489 base::Optional<CreatedWindow> owned_created =
nick5ae4d2d2017-01-06 01:18:353490 GetCreatedWindow(process_id, main_frame_widget_route_id);
Joel Hockey8c2011b22020-04-30 05:07:193491
danakje6c01622018-10-05 19:39:263492 // The browser may have rejected the request to make a new window, or the
3493 // renderer could be sending an invalid route id. Ignore the request then.
Joel Hockey8c2011b22020-04-30 05:07:193494 if (!owned_created || !owned_created->contents)
danakje6c01622018-10-05 19:39:263495 return;
mariakhomenko44bdc4732015-04-29 01:55:383496
Joel Hockey8c2011b22020-04-30 05:07:193497 WebContentsImpl* created = owned_created->contents.get();
3498
danakje6c01622018-10-05 19:39:263499 // This uses the delegate for the WebContents where the window was created
3500 // from, to control how to show the newly created window.
3501 WebContentsDelegate* delegate = GetDelegate();
nick5ae4d2d2017-01-06 01:18:353502
danakje6c01622018-10-05 19:39:263503 // The delegate can be null in tests, so we must check for it :(.
3504 if (delegate) {
3505 // Mark the web contents as pending resume, then immediately do
3506 // the resume if the delegate wants it.
3507 created->is_resume_pending_ = true;
3508 if (delegate->ShouldResumeRequestsForCreatedWindow())
3509 created->ResumeLoadingCreatedWebContents();
3510
3511 base::WeakPtr<WebContentsImpl> weak_created =
3512 created->weak_factory_.GetWeakPtr();
Joel Hockey8c2011b22020-04-30 05:07:193513 delegate->AddNewContents(this, std::move(owned_created->contents),
Joel Hockey891e88062020-04-30 05:38:443514 std::move(owned_created->target_url), disposition,
3515 initial_rect, user_gesture, nullptr);
danakje6c01622018-10-05 19:39:263516 // The delegate may delete |created| during AddNewContents().
3517 if (!weak_created)
3518 return;
[email protected]eda238a12012-09-07 23:44:003519 }
danakje6c01622018-10-05 19:39:263520
3521 RenderWidgetHostImpl* rwh = created->GetMainFrame()->GetRenderWidgetHost();
3522 DCHECK_EQ(main_frame_widget_route_id, rwh->GetRoutingID());
3523 rwh->Send(new WidgetMsg_SetBounds_ACK(rwh->GetRoutingID()));
[email protected]bafe6cd2012-05-23 23:09:503524}
3525
alexmosc2a8cec2016-05-23 22:19:533526void WebContentsImpl::ShowCreatedWidget(int process_id,
Albert J. Wongf6e13ed2018-09-18 15:25:473527 int widget_route_id,
bokan107a47f2015-02-03 23:23:393528 const gfx::Rect& initial_rect) {
Albert J. Wongf6e13ed2018-09-18 15:25:473529 ShowCreatedWidget(process_id, widget_route_id, false, initial_rect);
[email protected]bafe6cd2012-05-23 23:09:503530}
3531
alexmosc2a8cec2016-05-23 22:19:533532void WebContentsImpl::ShowCreatedFullscreenWidget(int process_id,
Albert J. Wongf6e13ed2018-09-18 15:25:473533 int widget_route_id) {
3534 ShowCreatedWidget(process_id, widget_route_id, true, gfx::Rect());
[email protected]bafe6cd2012-05-23 23:09:503535}
3536
alexmosc2a8cec2016-05-23 22:19:533537void WebContentsImpl::ShowCreatedWidget(int process_id,
3538 int route_id,
[email protected]bafe6cd2012-05-23 23:09:503539 bool is_fullscreen,
bokan107a47f2015-02-03 23:23:393540 const gfx::Rect& initial_rect) {
[email protected]cfd80b02014-05-01 17:46:483541 RenderWidgetHostViewBase* widget_host_view =
alexmosc2a8cec2016-05-23 22:19:533542 static_cast<RenderWidgetHostViewBase*>(
3543 GetCreatedWidget(process_id, route_id));
[email protected]bafe6cd2012-05-23 23:09:503544 if (!widget_host_view)
3545 return;
[email protected]cfd80b02014-05-01 17:46:483546
W. James MacLeanec4125c2019-09-27 18:56:003547 // GetOutermostWebContents() returns |this| if there are no outer WebContents.
W. James MacLean3c5f50462019-10-28 16:24:213548 auto* outer_web_contents = GetOuterWebContents();
3549 auto* outermost_web_contents = GetOutermostWebContents();
W. James MacLeanec4125c2019-09-27 18:56:003550 RenderWidgetHostView* view =
W. James MacLean3c5f50462019-10-28 16:24:213551 outermost_web_contents->GetRenderWidgetHostView();
3552 // It's not entirely obvious why we need the transform only in the case where
3553 // the outer webcontents is not the same as the outermost webcontents. It may
3554 // be due to the fact that oopifs that are children of the mainframe get
3555 // correct values for their screenrects, but deeper cross-process frames do
3556 // not. Hopefully this can be resolved with https://crbug.com/928825.
3557 // Handling these cases separately is needed for http://crbug.com/1015298.
3558 bool needs_transform = this != outermost_web_contents &&
3559 outermost_web_contents != outer_web_contents;
W. James MacLeanec4125c2019-09-27 18:56:003560
3561 gfx::Rect transformed_rect(initial_rect);
3562 RenderWidgetHostView* this_view = GetRenderWidgetHostView();
W. James MacLean3c5f50462019-10-28 16:24:213563 if (needs_transform) {
W. James MacLeanec4125c2019-09-27 18:56:003564 // We need to transform the coordinates of initial_rect.
3565 gfx::Point origin =
3566 this_view->TransformPointToRootCoordSpace(initial_rect.origin());
3567 gfx::Point bottom_right =
3568 this_view->TransformPointToRootCoordSpace(initial_rect.bottom_right());
3569 transformed_rect =
3570 gfx::Rect(origin.x(), origin.y(), bottom_right.x() - origin.x(),
3571 bottom_right.y() - origin.y());
[email protected]cfd80b02014-05-01 17:46:483572 }
3573
danakj0b246942018-09-17 21:30:273574 // Fullscreen child widgets are frames, other child widgets are popups.
[email protected]4aebbca2013-09-17 22:26:493575 if (is_fullscreen) {
[email protected]d7f80ba2013-10-12 07:42:313576 DCHECK_EQ(MSG_ROUTING_NONE, fullscreen_widget_routing_id_);
[email protected]0b431992014-06-24 00:08:033577 view_->StoreFocus();
alexmosc9e76ec2016-05-16 22:59:373578 fullscreen_widget_process_id_ =
3579 widget_host_view->GetRenderWidgetHost()->GetProcess()->GetID();
[email protected]d7f80ba2013-10-12 07:42:313580 fullscreen_widget_routing_id_ = route_id;
[email protected]4aebbca2013-09-17 22:26:493581 if (delegate_ && delegate_->EmbedsFullscreenWidget()) {
3582 widget_host_view->InitAsChild(GetRenderWidgetHostView()->GetNativeView());
Mike Wasserman4ca09792020-05-29 17:44:433583 delegate_->EnterFullscreenModeForTab(GetMainFrame(),
Dave Tapuskaa4189512019-10-15 20:27:343584 blink::mojom::FullscreenOptions());
[email protected]4aebbca2013-09-17 22:26:493585 } else {
[email protected]cfd80b02014-05-01 17:46:483586 widget_host_view->InitAsFullscreen(view);
[email protected]4aebbca2013-09-17 22:26:493587 }
ericwilligersde386342016-10-19 02:35:093588 for (auto& observer : observers_)
3589 observer.DidShowFullscreenWidget();
[email protected]4aebbca2013-09-17 22:26:493590 if (!widget_host_view->HasFocus())
3591 widget_host_view->Focus();
3592 } else {
W. James MacLeanec4125c2019-09-27 18:56:003593 widget_host_view->InitAsPopup(view, transformed_rect);
[email protected]4aebbca2013-09-17 22:26:493594 }
[email protected]89054502012-06-03 10:29:243595
yiyixcb1fbc4f2018-03-16 19:54:083596 RenderWidgetHostImpl* render_widget_host_impl = widget_host_view->host();
[email protected]89054502012-06-03 10:29:243597 render_widget_host_impl->Init();
[email protected]d7f80ba2013-10-12 07:42:313598 // Only allow privileged mouse lock for fullscreen render widget, which is
3599 // used to implement Pepper Flash fullscreen.
3600 render_widget_host_impl->set_allow_privileged_mouse_lock(is_fullscreen);
[email protected]bafe6cd2012-05-23 23:09:503601}
3602
Joel Hockey8c2011b22020-04-30 05:07:193603base::Optional<CreatedWindow> WebContentsImpl::GetCreatedWindow(
nick5ae4d2d2017-01-06 01:18:353604 int process_id,
3605 int main_frame_widget_route_id) {
Lukasz Anforowicz09060bdf72018-08-23 15:53:173606 auto key = GlobalRoutingID(process_id, main_frame_widget_route_id);
nick5ae4d2d2017-01-06 01:18:353607 auto iter = pending_contents_.find(key);
[email protected]bafe6cd2012-05-23 23:09:503608
3609 // Certain systems can block the creation of new windows. If we didn't succeed
3610 // in creating one, just return NULL.
alexmosc2a8cec2016-05-23 22:19:533611 if (iter == pending_contents_.end())
Joel Hockey8c2011b22020-04-30 05:07:193612 return base::nullopt;
[email protected]bafe6cd2012-05-23 23:09:503613
Joel Hockey8c2011b22020-04-30 05:07:193614 CreatedWindow result = std::move(iter->second);
3615 WebContentsImpl* new_contents = result.contents.get();
nick5ae4d2d2017-01-06 01:18:353616 pending_contents_.erase(key);
Joel Hockey8c2011b22020-04-30 05:07:193617 RemoveDestructionObserver(new_contents);
[email protected]bafe6cd2012-05-23 23:09:503618
[email protected]d70bea92013-04-05 04:23:343619 // Don't initialize the guest WebContents immediately.
Joel Hockey8c2011b22020-04-30 05:07:193620 if (BrowserPluginGuest::IsGuest(new_contents))
3621 return result;
[email protected]d70bea92013-04-05 04:23:343622
Lukasz Anforowicz5510ed652018-06-06 16:16:193623 if (!new_contents->GetMainFrame()->GetProcess()->IsInitializedAndNotDead() ||
nick5ae4d2d2017-01-06 01:18:353624 !new_contents->GetMainFrame()->GetView()) {
Joel Hockey8c2011b22020-04-30 05:07:193625 return base::nullopt;
nick5ae4d2d2017-01-06 01:18:353626 }
[email protected]bafe6cd2012-05-23 23:09:503627
Joel Hockey8c2011b22020-04-30 05:07:193628 return result;
[email protected]bafe6cd2012-05-23 23:09:503629}
3630
alexmosc2a8cec2016-05-23 22:19:533631RenderWidgetHostView* WebContentsImpl::GetCreatedWidget(int process_id,
3632 int route_id) {
Lukasz Anforowicz09060bdf72018-08-23 15:53:173633 auto iter = pending_widget_views_.find(GlobalRoutingID(process_id, route_id));
[email protected]bafe6cd2012-05-23 23:09:503634 if (iter == pending_widget_views_.end()) {
3635 DCHECK(false);
alexmosc2a8cec2016-05-23 22:19:533636 return nullptr;
[email protected]bafe6cd2012-05-23 23:09:503637 }
3638
3639 RenderWidgetHostView* widget_host_view = iter->second;
Lukasz Anforowicz09060bdf72018-08-23 15:53:173640 pending_widget_views_.erase(GlobalRoutingID(process_id, route_id));
[email protected]bafe6cd2012-05-23 23:09:503641
3642 RenderWidgetHost* widget_host = widget_host_view->GetRenderWidgetHost();
Lukasz Anforowicz5510ed652018-06-06 16:16:193643 if (!widget_host->GetProcess()->IsInitializedAndNotDead()) {
[email protected]bafe6cd2012-05-23 23:09:503644 // The view has gone away or the renderer crashed. Nothing to do.
alexmosc2a8cec2016-05-23 22:19:533645 return nullptr;
[email protected]bafe6cd2012-05-23 23:09:503646 }
3647
3648 return widget_host_view;
3649}
3650
[email protected]f13b4202012-06-12 23:53:233651void WebContentsImpl::RequestMediaAccessPermission(
[email protected]33662e52013-01-07 21:31:093652 const MediaStreamRequest& request,
Mark Pilgrim57499082018-06-12 12:38:303653 MediaResponseCallback callback) {
[email protected]d19b84b2014-03-14 11:52:373654 if (delegate_) {
Mark Pilgrim57499082018-06-12 12:38:303655 delegate_->RequestMediaAccessPermission(this, request, std::move(callback));
[email protected]d19b84b2014-03-14 11:52:373656 } else {
Antonio Gomes0d42960a2019-06-05 12:35:513657 std::move(callback).Run(
3658 blink::MediaStreamDevices(),
3659 blink::mojom::MediaStreamRequestResult::FAILED_DUE_TO_SHUTDOWN,
3660 std::unique_ptr<MediaStreamUI>());
[email protected]d19b84b2014-03-14 11:52:373661 }
[email protected]f13b4202012-06-12 23:53:233662}
3663
Chandan Padhia4b8bcb72017-09-12 16:41:043664bool WebContentsImpl::CheckMediaAccessPermission(
Raymes Khouryad7c24a12018-03-05 23:22:583665 RenderFrameHost* render_frame_host,
Chandan Padhia4b8bcb72017-09-12 16:41:043666 const url::Origin& security_origin,
Antonio Gomesc8b734b2019-06-05 18:22:163667 blink::mojom::MediaStreamType type) {
3668 DCHECK(type == blink::mojom::MediaStreamType::DEVICE_AUDIO_CAPTURE ||
3669 type == blink::mojom::MediaStreamType::DEVICE_VIDEO_CAPTURE);
Chandan Padhia4b8bcb72017-09-12 16:41:043670 return delegate_ && delegate_->CheckMediaAccessPermission(
Raymes Khouryad7c24a12018-03-05 23:22:583671 render_frame_host, security_origin.GetURL(), type);
grunell657d4d82014-09-18 00:09:433672}
3673
Guido Urdaneta73fa6632019-01-14 18:46:263674std::string WebContentsImpl::GetDefaultMediaDeviceID(
Antonio Gomesc8b734b2019-06-05 18:22:163675 blink::mojom::MediaStreamType type) {
guidou8a440b842017-01-30 13:58:433676 if (!delegate_)
3677 return std::string();
3678 return delegate_->GetDefaultMediaDeviceID(this, type);
3679}
3680
[email protected]cc9200432013-07-23 23:02:403681SessionStorageNamespace* WebContentsImpl::GetSessionStorageNamespace(
3682 SiteInstance* instance) {
3683 return controller_.GetSessionStorageNamespace(instance);
3684}
3685
[email protected]6de7fc482014-06-06 10:46:443686SessionStorageNamespaceMap WebContentsImpl::GetSessionStorageNamespaceMap() {
3687 return controller_.GetSessionStorageNamespaceMap();
3688}
3689
[email protected]9b159a52013-10-03 17:24:553690FrameTree* WebContentsImpl::GetFrameTree() {
3691 return &frame_tree_;
3692}
3693
aelias5252baa2016-04-10 01:18:023694bool WebContentsImpl::IsOverridingUserAgent() {
3695 return GetController().GetVisibleEntry() &&
3696 GetController().GetVisibleEntry()->GetIsOverridingUserAgent();
3697}
3698
avi85ee8362016-10-08 02:09:083699bool WebContentsImpl::IsJavaScriptDialogShowing() const {
3700 return is_showing_javascript_dialog_;
3701}
3702
avic031d392017-03-03 03:09:423703bool WebContentsImpl::ShouldIgnoreUnresponsiveRenderer() {
Dominique Fauteux-Chapleau99244382020-07-08 20:37:003704 if (suppress_unresponsive_renderer_count_ > 0)
3705 return true;
3706
avic031d392017-03-03 03:09:423707 // Ignore unresponsive renderers if the debugger is attached to them since the
3708 // unresponsiveness might be a result of the renderer sitting on a breakpoint.
3709 //
David Bienvenu525f8a02019-02-15 02:02:423710#ifdef OS_WIN
3711 // Check if a windows debugger is attached to the renderer process.
3712 base::ProcessHandle process_handle =
3713 GetMainFrame()->GetProcess()->GetProcess().Handle();
3714 BOOL debugger_present = FALSE;
3715 if (CheckRemoteDebuggerPresent(process_handle, &debugger_present) &&
3716 debugger_present)
3717 return true;
3718#endif // OS_WIN
3719
avic031d392017-03-03 03:09:423720 // TODO(pfeldman): Fix this to only return true if the renderer is *actually*
3721 // sitting on a breakpoint. https://crbug.com/684202
3722 return DevToolsAgentHost::IsDebuggerAttached(this);
3723}
3724
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:413725ui::AXMode WebContentsImpl::GetAccessibilityMode() {
[email protected]95640212014-07-26 18:14:303726 return accessibility_mode_;
3727}
3728
Mario Sanchez Prada5d7f1ac2020-07-16 17:18:403729void WebContentsImpl::AXTreeIDForMainFrameHasChanged() {
3730 RenderWidgetHostViewBase* rwhv =
3731 static_cast<RenderWidgetHostViewBase*>(GetRenderWidgetHostView());
3732 if (rwhv)
3733 rwhv->SetMainFrameAXTreeID(GetMainFrame()->GetAXTreeID());
3734
3735 for (auto& observer : observers_)
3736 observer.AXTreeIDForMainFrameHasChanged();
3737}
3738
Dominic Mazzonia7b0edb22017-08-09 16:32:513739void WebContentsImpl::AccessibilityEventReceived(
Dominic Mazzoniccbaa9b2018-06-06 07:44:233740 const AXEventNotificationDetails& details) {
ericwilligersde386342016-10-19 02:35:093741 for (auto& observer : observers_)
Dominic Mazzonia7b0edb22017-08-09 16:32:513742 observer.AccessibilityEventReceived(details);
[email protected]31a71eaf2014-03-13 01:47:363743}
3744
dmazzoni2400c462016-08-23 15:07:133745void WebContentsImpl::AccessibilityLocationChangesReceived(
3746 const std::vector<AXLocationChangeNotificationDetails>& details) {
ericwilligersde386342016-10-19 02:35:093747 for (auto& observer : observers_)
Dominic Mazzonia7b0edb22017-08-09 16:32:513748 observer.AccessibilityLocationChangesReceived(details);
dmazzoni2400c462016-08-23 15:07:133749}
3750
Abigail Kleinabb428332019-09-13 18:26:213751base::string16 WebContentsImpl::DumpAccessibilityTree(
3752 bool internal,
3753 std::vector<AccessibilityTreeFormatter::PropertyFilter> property_filters) {
James Wallace-Leeeafc94cb92018-07-23 21:35:093754 auto* ax_mgr = GetOrCreateRootBrowserAccessibilityManager();
3755 DCHECK(ax_mgr);
Lei Zhangc98caa3822019-11-07 19:17:273756 return AccessibilityTreeFormatterBase::DumpAccessibilityTreeFromManager(
Abigail Kleinabb428332019-09-13 18:26:213757 ax_mgr, internal, property_filters);
James Wallace-Leeeafc94cb92018-07-23 21:35:093758}
3759
Abigail Kleinaa898742019-11-01 02:31:253760void WebContentsImpl::RecordAccessibilityEvents(
Abigail Kleinbb8304bd2020-05-18 21:44:183761 bool start_recording,
3762 base::Optional<AccessibilityEventCallback> callback) {
3763 // Only pass a callback to RecordAccessibilityEvents when starting to record.
3764 DCHECK_EQ(start_recording, callback.has_value());
3765 if (start_recording) {
Abigail Kleinaa898742019-11-01 02:31:253766 SetAccessibilityMode(ui::AXMode::kWebContents);
3767 auto* ax_mgr = GetOrCreateRootBrowserAccessibilityManager();
3768 DCHECK(ax_mgr);
3769 base::ProcessId pid = base::Process::Current().Pid();
3770 event_recorder_ = content::AccessibilityEventRecorder::Create(
3771 ax_mgr, pid, base::StringPiece{});
Abigail Kleinbb8304bd2020-05-18 21:44:183772 event_recorder_->ListenToEvents(*callback);
Abigail Kleinaa898742019-11-01 02:31:253773 } else {
Abigail Kleinbb8304bd2020-05-18 21:44:183774 if (event_recorder_) {
3775 event_recorder_->FlushAsyncEvents();
3776 event_recorder_.reset(nullptr);
3777 }
Abigail Kleinaa898742019-11-01 02:31:253778 }
3779}
3780
dmazzonia656928e2014-09-15 20:28:543781RenderFrameHost* WebContentsImpl::GetGuestByInstanceID(
fsamuel833ee7c2015-02-13 23:40:403782 RenderFrameHost* render_frame_host,
dmazzonia656928e2014-09-15 20:28:543783 int browser_plugin_instance_id) {
3784 BrowserPluginGuestManager* guest_manager =
3785 GetBrowserContext()->GetGuestManager();
fsamuel9cbc00a2015-04-17 20:30:203786 if (!guest_manager)
3787 return nullptr;
3788
dmazzonia656928e2014-09-15 20:28:543789 WebContents* guest = guest_manager->GetGuestByInstanceID(
fsamuel2e9413d2015-02-25 01:25:443790 render_frame_host->GetProcess()->GetID(), browser_plugin_instance_id);
dmazzonia656928e2014-09-15 20:28:543791 if (!guest)
fsamuel9cbc00a2015-04-17 20:30:203792 return nullptr;
3793
dmazzonia656928e2014-09-15 20:28:543794 return guest->GetMainFrame();
3795}
3796
Ke He7319dbe2017-11-09 05:54:443797device::mojom::GeolocationContext* WebContentsImpl::GetGeolocationContext() {
Ella Ge3e375b702020-06-05 15:57:163798 if (delegate_) {
3799 auto* installed_webapp_context =
3800 delegate_->GetInstalledWebappGeolocationContext();
3801 if (installed_webapp_context)
3802 return installed_webapp_context;
3803 }
3804
Ken Rockotce010f02019-12-12 23:32:323805 if (!geolocation_context_) {
3806 GetDeviceService().BindGeolocationContext(
3807 geolocation_context_.BindNewPipeAndPassReceiver());
3808 }
Conley Owens6894c4f2017-07-10 19:29:133809 return geolocation_context_.get();
blundellc57b93f2014-10-29 13:19:573810}
3811
ke.he98b761e2017-05-09 05:59:173812device::mojom::WakeLockContext* WebContentsImpl::GetWakeLockContext() {
blundelld8cd72b2017-03-28 07:18:383813 if (!wake_lock_context_host_)
3814 wake_lock_context_host_.reset(new WakeLockContextHost(this));
blundelle75a8f92017-03-27 08:11:173815 return wake_lock_context_host_->GetWakeLockContext();
alogvinovf50445a2015-10-30 13:00:123816}
3817
blundellf5316fc2017-05-15 11:49:033818#if defined(OS_ANDROID)
Miyoung Shin59f39ff2019-09-17 07:23:203819void WebContentsImpl::GetNFC(
Reilly Grantafbe2242020-02-20 21:14:553820 RenderFrameHost* render_frame_host,
Miyoung Shin59f39ff2019-09-17 07:23:203821 mojo::PendingReceiver<device::mojom::NFC> receiver) {
Reilly Grantafbe2242020-02-20 21:14:553822 if (!nfc_host_)
3823 nfc_host_ = std::make_unique<NFCHost>(this);
3824 nfc_host_->GetNFC(render_frame_host, std::move(receiver));
blundellf5316fc2017-05-15 11:49:033825}
3826#endif
3827
Evan Stade5e750ad02017-08-07 21:59:273828void WebContentsImpl::SetNotWaitingForResponse() {
3829 if (waiting_for_response_ == false)
3830 return;
3831
3832 waiting_for_response_ = false;
3833 if (delegate_)
3834 delegate_->LoadingStateChanged(this, is_load_to_different_document_);
Chris Hamiltonf82f2a702018-02-15 19:37:293835 for (auto& observer : observers_)
3836 observer.DidReceiveResponse();
Evan Stade5e750ad02017-08-07 21:59:273837}
3838
lfgbb9c28a2016-03-01 03:19:493839void WebContentsImpl::SendScreenRects() {
kenrbc52e1b4e2016-03-20 01:03:373840 for (FrameTreeNode* node : frame_tree_.Nodes()) {
kenrb0e8dc202016-05-30 19:51:403841 if (node->current_frame_host()->is_local_root())
kenrbc52e1b4e2016-03-20 01:03:373842 node->current_frame_host()->GetRenderWidgetHost()->SendScreenRects();
3843 }
[email protected]32deec62013-05-15 23:55:043844}
3845
ekaramadadd882292016-06-08 15:22:563846TextInputManager* WebContentsImpl::GetTextInputManager() {
3847 if (GetOuterWebContents())
3848 return GetOuterWebContents()->GetTextInputManager();
3849
Ehsan Karamadc179dc02018-12-15 03:09:293850 if (!text_input_manager_ && !browser_plugin_guest_) {
Darren Shenca53d5f2018-05-15 04:56:013851 text_input_manager_.reset(new TextInputManager(
3852 GetBrowserContext() &&
3853 !GetBrowserContext()->IsOffTheRecord()) /* should_do_learning */);
3854 }
ekaramadadd882292016-06-08 15:22:563855
3856 return text_input_manager_.get();
3857}
3858
paulmeyer7f6f1d4f2016-11-15 00:00:273859bool WebContentsImpl::OnUpdateDragCursor() {
Lei Zhang6410ec1a2019-01-31 21:38:583860 return browser_plugin_embedder_ &&
3861 browser_plugin_embedder_->OnUpdateDragCursor();
paulmeyer7f6f1d4f2016-11-15 00:00:273862}
3863
kenrb11f213a2017-03-24 18:12:063864bool WebContentsImpl::IsWidgetForMainFrame(
3865 RenderWidgetHostImpl* render_widget_host) {
3866 return render_widget_host == GetMainFrame()->GetRenderWidgetHost();
3867}
3868
[email protected]95640212014-07-26 18:14:303869BrowserAccessibilityManager*
3870 WebContentsImpl::GetRootBrowserAccessibilityManager() {
Carlos ILd51e7702020-05-07 18:51:393871 RenderFrameHostImpl* rfh = static_cast<RenderFrameHostImpl*>(GetMainFrame());
creisc014b402015-04-23 16:41:453872 return rfh ? rfh->browser_accessibility_manager() : nullptr;
[email protected]95640212014-07-26 18:14:303873}
3874
3875BrowserAccessibilityManager*
3876 WebContentsImpl::GetOrCreateRootBrowserAccessibilityManager() {
Carlos ILd51e7702020-05-07 18:51:393877 RenderFrameHostImpl* rfh = static_cast<RenderFrameHostImpl*>(GetMainFrame());
creisc014b402015-04-23 16:41:453878 return rfh ? rfh->GetOrCreateBrowserAccessibilityManager() : nullptr;
[email protected]95640212014-07-26 18:14:303879}
3880
Dave Tapuskab336b922017-07-06 19:24:053881void WebContentsImpl::ExecuteEditCommand(
3882 const std::string& command,
3883 const base::Optional<base::string16>& value) {
Dave Tapuska58a099e2020-06-08 21:48:403884 auto* input_handler = GetFocusedFrameWidgetInputHandler();
Dave Tapuskae782bea2019-07-09 16:21:513885 if (!input_handler)
Dave Tapuskab336b922017-07-06 19:24:053886 return;
3887
Dave Tapuskae782bea2019-07-09 16:21:513888 input_handler->ExecuteEditCommand(command, value);
Dave Tapuskab336b922017-07-06 19:24:053889}
3890
mohsen7ab1ec16ec2015-07-02 18:26:233891void WebContentsImpl::MoveRangeSelectionExtent(const gfx::Point& extent) {
Dave Tapuska58a099e2020-06-08 21:48:403892 auto* input_handler = GetFocusedFrameWidgetInputHandler();
Dave Tapuskae782bea2019-07-09 16:21:513893 if (!input_handler)
mohsen7ab1ec16ec2015-07-02 18:26:233894 return;
3895
Dave Tapuskae782bea2019-07-09 16:21:513896 input_handler->MoveRangeSelectionExtent(extent);
mohsen7ab1ec16ec2015-07-02 18:26:233897}
3898
3899void WebContentsImpl::SelectRange(const gfx::Point& base,
3900 const gfx::Point& extent) {
Dave Tapuska58a099e2020-06-08 21:48:403901 auto* input_handler = GetFocusedFrameWidgetInputHandler();
Dave Tapuskae782bea2019-07-09 16:21:513902 if (!input_handler)
mohsen7ab1ec16ec2015-07-02 18:26:233903 return;
3904
Dave Tapuskae782bea2019-07-09 16:21:513905 input_handler->SelectRange(base, extent);
mohsen7ab1ec16ec2015-07-02 18:26:233906}
3907
Dave Tapuskab336b922017-07-06 19:24:053908void WebContentsImpl::MoveCaret(const gfx::Point& extent) {
Dave Tapuska58a099e2020-06-08 21:48:403909 auto* input_handler = GetFocusedFrameWidgetInputHandler();
Dave Tapuskae782bea2019-07-09 16:21:513910 if (!input_handler)
Dave Tapuskab336b922017-07-06 19:24:053911 return;
3912
Dave Tapuskae782bea2019-07-09 16:21:513913 input_handler->MoveCaret(extent);
Dave Tapuskab336b922017-07-06 19:24:053914}
3915
Shimi Zhang37deeb22017-09-28 00:59:013916void WebContentsImpl::AdjustSelectionByCharacterOffset(
3917 int start_adjust,
3918 int end_adjust,
3919 bool show_selection_menu) {
Dave Tapuska58a099e2020-06-08 21:48:403920 auto* input_handler = GetFocusedFrameWidgetInputHandler();
Dave Tapuskae782bea2019-07-09 16:21:513921 if (!input_handler)
aurimasab0319022015-07-10 21:57:383922 return;
3923
Shimi Zhang37deeb22017-09-28 00:59:013924 using blink::mojom::SelectionMenuBehavior;
Dave Tapuskae782bea2019-07-09 16:21:513925 input_handler->AdjustSelectionByCharacterOffset(
Shimi Zhang37deeb22017-09-28 00:59:013926 start_adjust, end_adjust,
3927 show_selection_menu ? SelectionMenuBehavior::kShow
3928 : SelectionMenuBehavior::kHide);
aurimasab0319022015-07-10 21:57:383929}
3930
[email protected]b172aee2012-04-10 17:05:263931void WebContentsImpl::UpdatePreferredSize(const gfx::Size& pref_size) {
[email protected]222f5822014-02-05 23:40:493932 const gfx::Size old_size = GetPreferredSize();
[email protected]bcd2815602012-01-14 18:17:233933 preferred_size_ = pref_size;
[email protected]222f5822014-02-05 23:40:493934 OnPreferredSizeChanged(old_size);
[email protected]0548c5352011-09-07 00:33:333935}
3936
avic3aa8422015-11-09 20:57:223937void WebContentsImpl::ResizeDueToAutoResize(
3938 RenderWidgetHostImpl* render_widget_host,
Fady Samuel97947112018-05-05 16:24:543939 const gfx::Size& new_size) {
avic3aa8422015-11-09 20:57:223940 if (render_widget_host != GetRenderViewHost()->GetWidget())
3941 return;
3942
[email protected]61e2b3cc2012-03-02 16:13:343943 if (delegate_)
3944 delegate_->ResizeDueToAutoResize(this, new_size);
3945}
3946
[email protected]b172aee2012-04-10 17:05:263947WebContents* WebContentsImpl::OpenURL(const OpenURLParams& params) {
Aaron Colwell9dab1652019-12-09 18:29:493948#if DCHECK_IS_ON()
3949 DCHECK(params.Valid());
3950#endif
3951
Bo Liu300c6052018-06-12 04:46:073952 if (!delegate_) {
3953 // Embedder can delay setting a delegate on new WebContents with
3954 // WebContentsDelegate::ShouldResumeRequestsForCreatedWindow. In the mean
3955 // time, navigations, including the initial one, that goes through OpenURL
3956 // should be delayed until embedder is ready to resume loading.
3957 delayed_open_url_params_ = std::make_unique<OpenURLParams>(params);
Nasko Oskov83a8cf92018-10-19 14:58:563958
3959 // If there was a navigation deferred when creating the window through
3960 // CreateNewWindow, drop it in favor of this navigation.
3961 delayed_load_url_params_.reset();
3962
Ivan Kotenkov2c0d2bb32017-11-01 15:41:283963 return nullptr;
Bo Liu300c6052018-06-12 04:46:073964 }
[email protected]00c37fc2011-08-02 00:22:503965
[email protected]e5d549d2011-12-28 01:29:203966 WebContents* new_contents = delegate_->OpenURLFromTab(this, params);
pnoland488944652017-02-22 18:58:543967
3968 RenderFrameHost* source_render_frame_host = RenderFrameHost::FromID(
3969 params.source_render_process_id, params.source_render_frame_id);
3970
3971 if (source_render_frame_host && params.source_site_instance) {
3972 CHECK_EQ(source_render_frame_host->GetSiteInstance(),
3973 params.source_site_instance.get());
3974 }
3975
3976 if (new_contents && source_render_frame_host && new_contents != this) {
3977 for (auto& observer : observers_) {
Lukasz Anforowicz48c9f872018-08-01 00:58:413978 observer.DidOpenRequestedURL(
3979 new_contents, source_render_frame_host, params.url, params.referrer,
3980 params.disposition, params.transition,
3981 params.started_from_context_menu, params.is_renderer_initiated);
pnoland488944652017-02-22 18:58:543982 }
3983 }
3984
[email protected]e5d549d2011-12-28 01:29:203985 return new_contents;
[email protected]d5f942ba2008-09-26 19:30:343986}
3987
avi2b177592014-12-10 02:08:023988void WebContentsImpl::SetHistoryOffsetAndLength(int history_offset,
3989 int history_length) {
alexmos136fd6e62016-08-15 20:58:413990 SendPageMessage(new PageMsg_SetHistoryOffsetAndLength(
3991 MSG_ROUTING_NONE, history_offset, history_length));
avi2b177592014-12-10 02:08:023992}
3993
3994void WebContentsImpl::SetHistoryOffsetAndLengthForView(
3995 RenderViewHost* render_view_host,
3996 int history_offset,
3997 int history_length) {
alexmos136fd6e62016-08-15 20:58:413998 render_view_host->Send(new PageMsg_SetHistoryOffsetAndLength(
avi2b177592014-12-10 02:08:023999 render_view_host->GetRoutingID(), history_offset, history_length));
[email protected]796931a92011-08-10 01:32:144000}
4001
arthursonzogni818c2642019-09-27 12:18:104002void WebContentsImpl::ReloadFocusedFrame() {
[email protected]1f3fc1d2014-04-03 14:50:174003 RenderFrameHost* focused_frame = GetFocusedFrame();
4004 if (!focused_frame)
4005 return;
4006
yilkal921048bd2019-10-09 23:51:044007 focused_frame->Reload();
[email protected]959be4c2014-04-08 15:01:334008}
4009
4010void WebContentsImpl::Undo() {
Dave Tapuska58a099e2020-06-08 21:48:404011 auto* input_handler = GetFocusedFrameWidgetInputHandler();
Dave Tapuskae782bea2019-07-09 16:21:514012 if (!input_handler)
[email protected]959be4c2014-04-08 15:01:334013 return;
4014
Dave Tapuskae782bea2019-07-09 16:21:514015 input_handler->Undo();
[email protected]959be4c2014-04-08 15:01:334016 RecordAction(base::UserMetricsAction("Undo"));
4017}
4018
4019void WebContentsImpl::Redo() {
Dave Tapuska58a099e2020-06-08 21:48:404020 auto* input_handler = GetFocusedFrameWidgetInputHandler();
Dave Tapuskae782bea2019-07-09 16:21:514021 if (!input_handler)
[email protected]959be4c2014-04-08 15:01:334022 return;
Dave Tapuskae782bea2019-07-09 16:21:514023
4024 input_handler->Redo();
[email protected]959be4c2014-04-08 15:01:334025 RecordAction(base::UserMetricsAction("Redo"));
4026}
4027
4028void WebContentsImpl::Cut() {
Dave Tapuska58a099e2020-06-08 21:48:404029 auto* input_handler = GetFocusedFrameWidgetInputHandler();
Dave Tapuskae782bea2019-07-09 16:21:514030 if (!input_handler)
[email protected]959be4c2014-04-08 15:01:334031 return;
4032
Dave Tapuskae782bea2019-07-09 16:21:514033 input_handler->Cut();
[email protected]959be4c2014-04-08 15:01:334034 RecordAction(base::UserMetricsAction("Cut"));
4035}
4036
4037void WebContentsImpl::Copy() {
Dave Tapuska58a099e2020-06-08 21:48:404038 auto* input_handler = GetFocusedFrameWidgetInputHandler();
Dave Tapuskae782bea2019-07-09 16:21:514039 if (!input_handler)
[email protected]959be4c2014-04-08 15:01:334040 return;
4041
Dave Tapuskae782bea2019-07-09 16:21:514042 input_handler->Copy();
[email protected]959be4c2014-04-08 15:01:334043 RecordAction(base::UserMetricsAction("Copy"));
4044}
4045
4046void WebContentsImpl::CopyToFindPboard() {
4047#if defined(OS_MACOSX)
Dave Tapuska58a099e2020-06-08 21:48:404048 auto* input_handler = GetFocusedFrameWidgetInputHandler();
Dave Tapuskae782bea2019-07-09 16:21:514049 if (!input_handler)
[email protected]959be4c2014-04-08 15:01:334050 return;
4051
4052 // Windows/Linux don't have the concept of a find pasteboard.
Dave Tapuskae782bea2019-07-09 16:21:514053 input_handler->CopyToFindPboard();
[email protected]959be4c2014-04-08 15:01:334054 RecordAction(base::UserMetricsAction("CopyToFindPboard"));
4055#endif
4056}
4057
4058void WebContentsImpl::Paste() {
Dave Tapuska58a099e2020-06-08 21:48:404059 auto* input_handler = GetFocusedFrameWidgetInputHandler();
Dave Tapuskae782bea2019-07-09 16:21:514060 if (!input_handler)
[email protected]959be4c2014-04-08 15:01:334061 return;
4062
Dave Tapuskae782bea2019-07-09 16:21:514063 input_handler->Paste();
Archana Simhaf67090c2019-05-06 23:43:234064 for (auto& observer : observers_)
4065 observer.OnPaste();
[email protected]959be4c2014-04-08 15:01:334066 RecordAction(base::UserMetricsAction("Paste"));
4067}
4068
4069void WebContentsImpl::PasteAndMatchStyle() {
Dave Tapuska58a099e2020-06-08 21:48:404070 auto* input_handler = GetFocusedFrameWidgetInputHandler();
Dave Tapuskae782bea2019-07-09 16:21:514071 if (!input_handler)
[email protected]959be4c2014-04-08 15:01:334072 return;
4073
Dave Tapuskae782bea2019-07-09 16:21:514074 input_handler->PasteAndMatchStyle();
Archana Simhaf67090c2019-05-06 23:43:234075 for (auto& observer : observers_)
4076 observer.OnPaste();
[email protected]959be4c2014-04-08 15:01:334077 RecordAction(base::UserMetricsAction("PasteAndMatchStyle"));
4078}
4079
4080void WebContentsImpl::Delete() {
Dave Tapuska58a099e2020-06-08 21:48:404081 auto* input_handler = GetFocusedFrameWidgetInputHandler();
Dave Tapuskae782bea2019-07-09 16:21:514082 if (!input_handler)
[email protected]959be4c2014-04-08 15:01:334083 return;
4084
Dave Tapuskae782bea2019-07-09 16:21:514085 input_handler->Delete();
[email protected]959be4c2014-04-08 15:01:334086 RecordAction(base::UserMetricsAction("DeleteSelection"));
4087}
4088
4089void WebContentsImpl::SelectAll() {
Dave Tapuska58a099e2020-06-08 21:48:404090 auto* input_handler = GetFocusedFrameWidgetInputHandler();
Dave Tapuskae782bea2019-07-09 16:21:514091 if (!input_handler)
[email protected]959be4c2014-04-08 15:01:334092 return;
4093
Dave Tapuskae782bea2019-07-09 16:21:514094 input_handler->SelectAll();
[email protected]959be4c2014-04-08 15:01:334095 RecordAction(base::UserMetricsAction("SelectAll"));
4096}
4097
yabinh351e7ec2017-03-10 02:43:244098void WebContentsImpl::CollapseSelection() {
Dave Tapuska58a099e2020-06-08 21:48:404099 auto* input_handler = GetFocusedFrameWidgetInputHandler();
Dave Tapuskae782bea2019-07-09 16:21:514100 if (!input_handler)
[email protected]959be4c2014-04-08 15:01:334101 return;
4102
Dave Tapuskae782bea2019-07-09 16:21:514103 input_handler->CollapseSelection();
[email protected]1f3fc1d2014-04-03 14:50:174104}
4105
4106void WebContentsImpl::Replace(const base::string16& word) {
Dave Tapuska58a099e2020-06-08 21:48:404107 auto* input_handler = GetFocusedFrameWidgetInputHandler();
Dave Tapuskae782bea2019-07-09 16:21:514108 if (!input_handler)
[email protected]1f3fc1d2014-04-03 14:50:174109 return;
4110
Dave Tapuskae782bea2019-07-09 16:21:514111 input_handler->Replace(word);
[email protected]1f3fc1d2014-04-03 14:50:174112}
4113
4114void WebContentsImpl::ReplaceMisspelling(const base::string16& word) {
Dave Tapuska58a099e2020-06-08 21:48:404115 auto* input_handler = GetFocusedFrameWidgetInputHandler();
Dave Tapuskae782bea2019-07-09 16:21:514116 if (!input_handler)
[email protected]1f3fc1d2014-04-03 14:50:174117 return;
4118
Dave Tapuskae782bea2019-07-09 16:21:514119 input_handler->ReplaceMisspelling(word);
[email protected]959be4c2014-04-08 15:01:334120}
4121
4122void WebContentsImpl::NotifyContextMenuClosed(
4123 const CustomContextMenuContext& context) {
4124 RenderFrameHost* focused_frame = GetFocusedFrame();
4125 if (!focused_frame)
4126 return;
4127
4128 focused_frame->Send(new FrameMsg_ContextMenuClosed(
4129 focused_frame->GetRoutingID(), context));
4130}
4131
4132void WebContentsImpl::ExecuteCustomContextMenuCommand(
4133 int action, const CustomContextMenuContext& context) {
4134 RenderFrameHost* focused_frame = GetFocusedFrame();
4135 if (!focused_frame)
4136 return;
4137
4138 focused_frame->Send(new FrameMsg_CustomContextMenuAction(
4139 focused_frame->GetRoutingID(), context, action));
[email protected]4fed3702014-04-01 09:08:004140}
4141
[email protected]fc2b46b2014-05-03 16:33:454142gfx::NativeView WebContentsImpl::GetNativeView() {
4143 return view_->GetNativeView();
4144}
4145
4146gfx::NativeView WebContentsImpl::GetContentNativeView() {
4147 return view_->GetContentNativeView();
4148}
4149
4150gfx::NativeWindow WebContentsImpl::GetTopLevelNativeWindow() {
4151 return view_->GetTopLevelNativeWindow();
4152}
4153
4154gfx::Rect WebContentsImpl::GetViewBounds() {
4155 return view_->GetViewBounds();
4156}
4157
4158gfx::Rect WebContentsImpl::GetContainerBounds() {
Jeremy Roman352239c2020-05-21 16:31:324159 return view_->GetContainerBounds();
[email protected]fc2b46b2014-05-03 16:33:454160}
4161
4162DropData* WebContentsImpl::GetDropData() {
4163 return view_->GetDropData();
4164}
4165
4166void WebContentsImpl::Focus() {
miu9e14e492014-10-25 02:39:044167 view_->Focus();
[email protected]fc2b46b2014-05-03 16:33:454168}
4169
4170void WebContentsImpl::SetInitialFocus() {
miu9e14e492014-10-25 02:39:044171 view_->SetInitialFocus();
[email protected]fc2b46b2014-05-03 16:33:454172}
4173
4174void WebContentsImpl::StoreFocus() {
miu9e14e492014-10-25 02:39:044175 view_->StoreFocus();
[email protected]fc2b46b2014-05-03 16:33:454176}
4177
4178void WebContentsImpl::RestoreFocus() {
miu9e14e492014-10-25 02:39:044179 view_->RestoreFocus();
[email protected]fc2b46b2014-05-03 16:33:454180}
4181
[email protected]b172aee2012-04-10 17:05:264182void WebContentsImpl::FocusThroughTabTraversal(bool reverse) {
Maxim Podgorodskiya926c4ff2017-11-20 02:06:394183 view_->FocusThroughTabTraversal(reverse);
[email protected]96d185d2009-04-24 03:28:544184}
4185
[email protected]b172aee2012-04-10 17:05:264186bool WebContentsImpl::IsSavable() {
[email protected]a53209b2012-01-20 16:48:164187 // WebKit creates Document object when MIME type is application/xhtml+xml,
4188 // so we also support this MIME type.
Yuzu Saijoc23e9b72020-06-25 07:21:274189 std::string mime_type = GetContentsMimeType();
4190 return mime_type == "text/html" || mime_type == "text/xml" ||
4191 mime_type == "application/xhtml+xml" || mime_type == "text/plain" ||
4192 mime_type == "text/css" ||
4193 blink::IsSupportedJavascriptMimeType(mime_type);
[email protected]a53209b2012-01-20 16:48:164194}
4195
[email protected]b172aee2012-04-10 17:05:264196void WebContentsImpl::OnSavePage() {
[email protected]c7dd2f62011-07-18 15:57:594197 // If we can not save the page, try to download it.
[email protected]a53209b2012-01-20 16:48:164198 if (!IsSavable()) {
Min Qinda0ed2062018-02-23 22:00:534199 download::RecordSavePackageEvent(
4200 download::SAVE_PACKAGE_DOWNLOAD_ON_NON_HTML);
nasko89ad7742015-03-05 22:14:194201 SaveFrame(GetLastCommittedURL(), Referrer());
[email protected]27678b2a2012-02-04 22:09:144202 return;
[email protected]c7dd2f62011-07-18 15:57:594203 }
4204
4205 Stop();
4206
4207 // Create the save package and possibly prompt the user for the name to save
4208 // the page as. The user prompt is an asynchronous operation that runs on
4209 // another thread.
4210 save_package_ = new SavePackage(this);
4211 save_package_->GetSaveInfo();
4212}
4213
4214// Used in automated testing to bypass prompting the user for file names.
4215// Instead, the names and paths are hard coded rather than running them through
4216// file name sanitation and extension / mime checking.
[email protected]2dec8ec2013-02-07 19:20:344217bool WebContentsImpl::SavePage(const base::FilePath& main_file,
4218 const base::FilePath& dir_path,
[email protected]8ff00d72012-10-23 19:12:214219 SavePageType save_type) {
[email protected]c7dd2f62011-07-18 15:57:594220 // Stop the page from navigating.
4221 Stop();
4222
4223 save_package_ = new SavePackage(this, save_type, main_file, dir_path);
[email protected]8ff00d72012-10-23 19:12:214224 return save_package_->Init(SavePackageDownloadCreatedCallback());
[email protected]c7dd2f62011-07-18 15:57:594225}
4226
[email protected]3c71576ce2013-07-23 02:00:014227void WebContentsImpl::SaveFrame(const GURL& url,
4228 const Referrer& referrer) {
Xing Liu10329bf2018-03-20 19:22:144229 SaveFrameWithHeaders(url, referrer, std::string(), base::string16());
kundaji6c7f9692015-03-09 18:00:374230}
4231
Xing Liu10329bf2018-03-20 19:22:144232void WebContentsImpl::SaveFrameWithHeaders(
4233 const GURL& url,
4234 const Referrer& referrer,
4235 const std::string& headers,
4236 const base::string16& suggested_filename) {
Lei Zhang13a42e42019-02-19 23:28:114237 // Check and see if the guest can handle this.
4238 if (delegate_) {
4239 WebContents* guest_web_contents = nullptr;
4240 if (browser_plugin_embedder_) {
4241 BrowserPluginGuest* guest = browser_plugin_embedder_->GetFullPageGuest();
4242 if (guest)
4243 guest_web_contents = guest->GetWebContents();
4244 } else if (browser_plugin_guest_) {
4245 guest_web_contents = this;
4246 }
4247
4248 if (guest_web_contents && delegate_->GuestSaveFrame(guest_web_contents))
4249 return;
4250 }
4251
nasko89ad7742015-03-05 22:14:194252 if (!GetLastCommittedURL().is_valid())
[email protected]3c71576ce2013-07-23 02:00:014253 return;
sammc92af61552014-11-19 23:27:404254 if (delegate_ && delegate_->SaveFrame(url, referrer))
4255 return;
4256
nasko89ad7742015-03-05 22:14:194257 // TODO(nasko): This check for main frame is incorrect and should be fixed
brettw760f7442016-05-23 21:19:224258 // by explicitly passing in which frame this method should target. This would
4259 // indicate whether it's the main frame, and also tell us the frame pointer
4260 // to use for routing.
nasko89ad7742015-03-05 22:14:194261 bool is_main_frame = (url == GetLastCommittedURL());
brettw760f7442016-05-23 21:19:224262 RenderFrameHost* frame_host = GetMainFrame();
[email protected]3c71576ce2013-07-23 02:00:014263
avib7348942015-12-25 20:57:104264 int64_t post_id = -1;
[email protected]3c71576ce2013-07-23 02:00:014265 if (is_main_frame) {
Lucas Furukawa Gadani5553a152019-01-08 18:55:574266 NavigationEntry* entry = controller_.GetLastCommittedEntry();
[email protected]3c71576ce2013-07-23 02:00:014267 if (entry)
4268 post_id = entry->GetPostID();
4269 }
Ramin Halavati24e21012017-07-10 12:56:064270 net::NetworkTrafficAnnotationTag traffic_annotation =
4271 net::DefineNetworkTrafficAnnotation("download_web_contents_frame", R"(
4272 semantics {
4273 sender: "Save Page Action"
4274 description:
4275 "Saves the given frame's URL to the local file system."
4276 trigger:
4277 "The user has triggered a save operation on the frame through a "
4278 "context menu or other mechanism."
4279 data: "None."
4280 destination: WEBSITE
4281 }
4282 policy {
Ramin Halavati3b979782017-07-21 11:40:264283 cookies_allowed: YES
Ramin Halavati24e21012017-07-10 12:56:064284 cookies_store: "user"
4285 setting:
4286 "This feature cannot be disabled by settings, but it's is only "
4287 "triggered by user request."
4288 policy_exception_justification: "Not implemented."
4289 })");
Min Qina904f3302018-02-13 23:33:344290 auto params = std::make_unique<download::DownloadUrlParameters>(
brettw760f7442016-05-23 21:19:224291 url, frame_host->GetProcess()->GetID(),
4292 frame_host->GetRenderViewHost()->GetRoutingID(),
Matt Menke0dcaebc32020-03-06 17:00:104293 frame_host->GetRoutingID(), traffic_annotation);
Min Qina904f3302018-02-13 23:33:344294 params->set_referrer(referrer.url);
4295 params->set_referrer_policy(
4296 Referrer::ReferrerPolicyForUrlRequest(referrer.policy));
[email protected]3c71576ce2013-07-23 02:00:014297 params->set_post_id(post_id);
[email protected]3c71576ce2013-07-23 02:00:014298 if (post_id >= 0)
4299 params->set_method("POST");
4300 params->set_prompt(true);
kundaji6c7f9692015-03-09 18:00:374301
4302 if (headers.empty()) {
4303 params->set_prefer_cache(true);
4304 } else {
Min Qina904f3302018-02-13 23:33:344305 for (download::DownloadUrlParameters::RequestHeadersNameValuePair
4306 key_value : ParseDownloadHeaders(headers)) {
Megan Jablonski2f6a4c52017-07-10 23:01:254307 params->add_request_header(key_value.first, key_value.second);
kundaji6c7f9692015-03-09 18:00:374308 }
4309 }
Xing Liu10329bf2018-03-20 19:22:144310 params->set_suggested_name(suggested_filename);
Min Qin0ca8e1ee2018-01-31 00:49:354311 params->set_download_source(download::DownloadSource::WEB_CONTENTS_API);
thestig859c78892017-05-15 21:17:064312 BrowserContext::GetDownloadManager(GetBrowserContext())
Ramin Halavati03efa732017-06-12 09:28:204313 ->DownloadUrl(std::move(params));
[email protected]3c71576ce2013-07-23 02:00:014314}
4315
[email protected]b172aee2012-04-10 17:05:264316void WebContentsImpl::GenerateMHTML(
dewittj6dc5747a2016-05-17 01:48:474317 const MHTMLGenerationParams& params,
Avi Drissman149b7832018-03-23 14:31:494318 base::OnceCallback<void(int64_t)> callback) {
Angel Alvarez902496682019-08-27 22:58:424319 base::OnceCallback<void(const MHTMLGenerationResult&)> wrapper_callback =
4320 base::BindOnce(
4321 [](base::OnceCallback<void(int64_t)> size_callback,
4322 const MHTMLGenerationResult& result) {
4323 std::move(size_callback).Run(result.file_size);
4324 },
4325 std::move(callback));
4326 MHTMLGenerationManager::GetInstance()->SaveMHTML(this, params,
4327 std::move(wrapper_callback));
4328}
4329
4330void WebContentsImpl::GenerateMHTMLWithResult(
4331 const MHTMLGenerationParams& params,
4332 MHTMLGenerationResult::GenerateMHTMLCallback callback) {
Avi Drissman149b7832018-03-23 14:31:494333 MHTMLGenerationManager::GetInstance()->SaveMHTML(this, params,
4334 std::move(callback));
[email protected]aa4f3972012-03-01 18:12:124335}
4336
Tsuyoshi Horob0d8d902020-03-12 00:09:394337void WebContentsImpl::GenerateWebBundle(
4338 const base::FilePath& file_path,
4339 base::OnceCallback<void(uint64_t /* file_size */,
4340 data_decoder::mojom::WebBundlerError)> callback) {
4341 SaveAsWebBundleJob::Start(this, file_path, std::move(callback));
4342}
4343
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:414344const std::string& WebContentsImpl::GetContentsMimeType() {
Yuzu Saijoc23e9b72020-06-25 07:21:274345 return GetRenderViewHost()->contents_mime_type();
[email protected]0bfbf882011-12-22 18:19:274346}
4347
Leon Hanc819dc62019-01-28 04:30:194348blink::mojom::RendererPreferences* WebContentsImpl::GetMutableRendererPrefs() {
[email protected]0bfbf882011-12-22 18:19:274349 return &renderer_preferences_;
4350}
4351
[email protected]e35ccd52012-05-23 16:22:474352void WebContentsImpl::Close() {
4353 Close(GetRenderViewHost());
4354}
4355
Ella Ge80a52dce2017-11-15 18:01:524356void WebContentsImpl::DragSourceEndedAt(float client_x,
4357 float client_y,
4358 float screen_x,
4359 float screen_y,
Paul Meyer0c58c3712016-11-17 22:59:514360 blink::WebDragOperation operation,
4361 RenderWidgetHost* source_rwh) {
Lei Zhang6410ec1a2019-01-31 21:38:584362 if (browser_plugin_embedder_) {
W. James MacLean9437b5bf2019-12-06 17:33:444363 browser_plugin_embedder_->DragSourceEndedAt(client_x, client_y, screen_x,
4364 screen_y, operation);
Lei Zhang6410ec1a2019-01-31 21:38:584365 }
Paul Meyer0c58c3712016-11-17 22:59:514366 if (source_rwh) {
Ella Ge80a52dce2017-11-15 18:01:524367 source_rwh->DragSourceEndedAt(gfx::PointF(client_x, client_y),
4368 gfx::PointF(screen_x, screen_y), operation);
Paul Meyer0c58c3712016-11-17 22:59:514369 }
[email protected]cf200a562013-05-03 16:24:294370}
4371
jam73f892642016-09-11 06:04:064372void WebContentsImpl::LoadStateChanged(
Charles Harrison911fa062018-03-06 21:01:464373 const std::string& host,
jam73f892642016-09-11 06:04:064374 const net::LoadStateWithParam& load_state,
4375 uint64_t upload_position,
4376 uint64_t upload_size) {
Charles Harrison911fa062018-03-06 21:01:464377 base::string16 host16 = url_formatter::IDNToUnicode(host);
Elly Fong-Jonescad9c3d12018-01-31 17:26:104378 // Drop no-op updates.
4379 if (load_state_.state == load_state.state &&
4380 load_state_.param == load_state.param &&
4381 upload_position_ == upload_position && upload_size_ == upload_size &&
Charles Harrison911fa062018-03-06 21:01:464382 load_state_host_ == host16) {
Elly Fong-Jonescad9c3d12018-01-31 17:26:104383 return;
4384 }
jam73f892642016-09-11 06:04:064385 load_state_ = load_state;
4386 upload_position_ = upload_position;
4387 upload_size_ = upload_size;
Charles Harrison911fa062018-03-06 21:01:464388 load_state_host_ = host16;
jam73f892642016-09-11 06:04:064389 if (load_state_.state == net::LOAD_STATE_READING_RESPONSE)
4390 SetNotWaitingForResponse();
4391 if (IsLoading()) {
4392 NotifyNavigationStateChanged(static_cast<InvalidateTypes>(
4393 INVALIDATE_TYPE_LOAD | INVALIDATE_TYPE_TAB));
4394 }
4395}
4396
Collin Baker989e0882019-11-01 01:27:174397void WebContentsImpl::SetVisibilityAndNotifyObservers(Visibility visibility) {
Francois Doraye6161152018-03-27 22:05:374398 const Visibility previous_visibility = visibility_;
4399 visibility_ = visibility;
4400
Francois Dorayfe4a1772018-02-17 04:17:094401 // Notify observers if the visibility changed or if WasShown() is being called
4402 // for the first time.
Francois Dorayfe4a1772018-02-17 04:17:094403 if (visibility != previous_visibility ||
4404 (visibility == Visibility::VISIBLE && !did_first_set_visible_)) {
4405 for (auto& observer : observers_)
4406 observer.OnVisibilityChanged(visibility);
4407 }
4408}
4409
Michael Thiessen896405db2017-07-20 17:52:324410void WebContentsImpl::NotifyWebContentsFocused(
4411 RenderWidgetHost* render_widget_host) {
ericwilligersde386342016-10-19 02:35:094412 for (auto& observer : observers_)
Michael Thiessen896405db2017-07-20 17:52:324413 observer.OnWebContentsFocused(render_widget_host);
calamity7fe55ce2015-04-10 03:59:374414}
4415
Michael Thiessen896405db2017-07-20 17:52:324416void WebContentsImpl::NotifyWebContentsLostFocus(
4417 RenderWidgetHost* render_widget_host) {
zijiehe3bee08e22017-05-17 04:14:464418 for (auto& observer : observers_)
Michael Thiessen896405db2017-07-20 17:52:324419 observer.OnWebContentsLostFocus(render_widget_host);
zijiehe3bee08e22017-05-17 04:14:464420}
4421
Paul Meyer0c58c3712016-11-17 22:59:514422void WebContentsImpl::SystemDragEnded(RenderWidgetHost* source_rwh) {
4423 if (source_rwh)
4424 source_rwh->DragSourceSystemDragEnded();
Lei Zhang6410ec1a2019-01-31 21:38:584425 if (browser_plugin_embedder_)
[email protected]cf200a562013-05-03 16:24:294426 browser_plugin_embedder_->SystemDragEnded();
[email protected]7813bd72011-02-05 02:19:344427}
4428
[email protected]b172aee2012-04-10 17:05:264429void WebContentsImpl::SetClosedByUserGesture(bool value) {
[email protected]0bfbf882011-12-22 18:19:274430 closed_by_user_gesture_ = value;
4431}
4432
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:414433bool WebContentsImpl::GetClosedByUserGesture() {
[email protected]0bfbf882011-12-22 18:19:274434 return closed_by_user_gesture_;
4435}
4436
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:414437int WebContentsImpl::GetMinimumZoomPercent() {
[email protected]0bfbf882011-12-22 18:19:274438 return minimum_zoom_percent_;
4439}
4440
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:414441int WebContentsImpl::GetMaximumZoomPercent() {
[email protected]0bfbf882011-12-22 18:19:274442 return maximum_zoom_percent_;
4443}
4444
Dave Tapuska8f7c3872020-02-07 01:16:384445void WebContentsImpl::SetPageScale(float scale_factor) {
4446 GetMainFrame()->GetAssociatedLocalMainFrame()->SetScaleFactor(scale_factor);
ccameronb7c1d6c2015-03-09 17:08:244447}
4448
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:414449gfx::Size WebContentsImpl::GetPreferredSize() {
Francois Doray24fc62c2017-12-11 17:27:334450 return IsBeingCaptured() ? preferred_size_for_capture_ : preferred_size_;
[email protected]bcd2815602012-01-14 18:17:234451}
4452
James Hollyerd5c9de462020-03-10 19:02:454453bool WebContentsImpl::GotResponseToLockMouseRequest(
4454 blink::mojom::PointerLockResult result) {
lfgf0cd46e2017-01-04 00:05:234455 if (mouse_lock_widget_) {
4456 if (mouse_lock_widget_->delegate()->GetAsWebContents() != this) {
4457 return mouse_lock_widget_->delegate()
4458 ->GetAsWebContents()
James Hollyerd5c9de462020-03-10 19:02:454459 ->GotResponseToLockMouseRequest(result);
lfgf0cd46e2017-01-04 00:05:234460 }
lfgad8244352016-07-13 16:55:514461
James Hollyerd5c9de462020-03-10 19:02:454462 if (mouse_lock_widget_->GotResponseToLockMouseRequest(result))
lfgf0cd46e2017-01-04 00:05:234463 return true;
4464 }
4465
4466 for (WebContentsImpl* current = this; current;
4467 current = current->GetOuterWebContents()) {
4468 current->mouse_lock_widget_ = nullptr;
4469 }
4470
lfgbee1e0a2016-06-08 21:24:214471 return false;
[email protected]0bfbf882011-12-22 18:19:274472}
4473
James Hollyerd5c9de462020-03-10 19:02:454474void WebContentsImpl::GotLockMousePermissionResponse(bool allowed) {
4475 GotResponseToLockMouseRequest(
4476 allowed ? blink::mojom::PointerLockResult::kSuccess
4477 : blink::mojom::PointerLockResult::kPermissionDenied);
4478}
4479
Joe Downing13dd76b2018-04-09 18:32:154480bool WebContentsImpl::GotResponseToKeyboardLockRequest(bool allowed) {
4481 if (!keyboard_lock_widget_)
4482 return false;
4483
4484 if (keyboard_lock_widget_->delegate()->GetAsWebContents() != this) {
4485 NOTREACHED();
4486 return false;
4487 }
4488
4489 // KeyboardLock is only supported when called by the top-level browsing
4490 // context and is not supported in embedded content scenarios.
4491 if (GetOuterWebContents())
4492 return false;
4493
4494 keyboard_lock_widget_->GotResponseToKeyboardLockRequest(allowed);
4495 return true;
4496}
4497
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:414498bool WebContentsImpl::HasOpener() {
Ivan Kotenkov2c0d2bb32017-11-01 15:41:284499 return GetOpener() != nullptr;
[email protected]a0358d72012-03-09 14:06:504500}
4501
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:414502RenderFrameHostImpl* WebContentsImpl::GetOpener() {
alexmose201c7cd2015-06-10 17:14:214503 FrameTreeNode* opener_ftn = frame_tree_.root()->opener();
lukasza6f8ac622017-06-06 03:10:204504 return opener_ftn ? opener_ftn->current_frame_host() : nullptr;
jochen55ff3502014-12-18 20:52:574505}
4506
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:414507bool WebContentsImpl::HasOriginalOpener() {
Ivan Kotenkov2c0d2bb32017-11-01 15:41:284508 return GetOriginalOpener() != nullptr;
jochen6004a362017-02-04 00:11:404509}
4510
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:414511RenderFrameHostImpl* WebContentsImpl::GetOriginalOpener() {
jochen6004a362017-02-04 00:11:404512 FrameTreeNode* opener_ftn = frame_tree_.root()->original_opener();
lukasza6f8ac622017-06-06 03:10:204513 return opener_ftn ? opener_ftn->current_frame_host() : nullptr;
jochen6004a362017-02-04 00:11:404514}
4515
[email protected]cb805452013-05-22 15:16:214516void WebContentsImpl::DidChooseColorInColorChooser(SkColor color) {
Joel Hockey85b379d2017-12-11 10:42:134517 color_chooser_->DidChooseColorInColorChooser(color);
[email protected]da8543762012-03-20 08:52:204518}
4519
[email protected]cb805452013-05-22 15:16:214520void WebContentsImpl::DidEndColorChooser() {
Joel Hockey85b379d2017-12-11 10:42:134521 color_chooser_.reset();
[email protected]da8543762012-03-20 08:52:204522}
4523
halton.huoca2eabd2015-07-06 08:17:404524int WebContentsImpl::DownloadImage(
4525 const GURL& url,
4526 bool is_favicon,
Fredrik Söderquistb2b39eb92019-11-18 16:16:504527 uint32_t preferred_size,
halton.huoca2eabd2015-07-06 08:17:404528 uint32_t max_bitmap_size,
4529 bool bypass_cache,
Avi Drissmana5a52dd2018-03-27 03:39:024530 WebContents::ImageDownloadCallback callback) {
Danyao Wanga78f3dd22020-03-05 05:31:274531 return DownloadImageInFrame(GlobalFrameRoutingId(), url, is_favicon,
4532 preferred_size, max_bitmap_size, bypass_cache,
4533 std::move(callback));
4534}
4535
4536int WebContentsImpl::DownloadImageInFrame(
4537 const GlobalFrameRoutingId& initiator_frame_routing_id,
4538 const GURL& url,
4539 bool is_favicon,
4540 uint32_t preferred_size,
4541 uint32_t max_bitmap_size,
4542 bool bypass_cache,
4543 WebContents::ImageDownloadCallback callback) {
amistry9f01b772015-07-29 01:54:554544 DCHECK_CURRENTLY_ON(BrowserThread::UI);
halton.huoca2eabd2015-07-06 08:17:404545 static int next_image_download_id = 0;
Danyao Wanga78f3dd22020-03-05 05:31:274546
4547 RenderFrameHostImpl* initiator_frame =
4548 initiator_frame_routing_id.child_id
4549 ? RenderFrameHostImpl::FromID(initiator_frame_routing_id)
4550 : GetMainFrame();
Julie Jeongeun Kimb1473572019-08-15 05:59:134551 const mojo::Remote<blink::mojom::ImageDownloader>& mojo_image_downloader =
Danyao Wanga78f3dd22020-03-05 05:31:274552 initiator_frame->GetMojoImageDownloader();
amistry9f01b772015-07-29 01:54:554553 const int download_id = ++next_image_download_id;
4554 if (!mojo_image_downloader) {
4555 // If the renderer process is dead (i.e. crash, or memory pressure on
4556 // Android), the downloader service will be invalid. Pre-Mojo, this would
Avi Drissmana5a52dd2018-03-27 03:39:024557 // hang the callback indefinitely since the IPC would be dropped. Now,
amistry9f01b772015-07-29 01:54:554558 // respond with a 400 HTTP error code to indicate that something went wrong.
Gabriel Charettee7cdc5cd2020-05-27 23:35:054559 GetUIThreadTaskRunner({})->PostTask(
4560 FROM_HERE,
tzike2aca992017-09-05 08:50:544561 base::BindOnce(&WebContentsImpl::OnDidDownloadImage,
Avi Drissmana5a52dd2018-03-27 03:39:024562 weak_factory_.GetWeakPtr(), std::move(callback),
4563 download_id, url, 400, std::vector<SkBitmap>(),
4564 std::vector<gfx::Size>()));
amistry9f01b772015-07-29 01:54:554565 return download_id;
4566 }
4567
halton.huoca2eabd2015-07-06 08:17:404568 mojo_image_downloader->DownloadImage(
Fredrik Söderquistb2b39eb92019-11-18 16:16:504569 url, is_favicon, preferred_size, max_bitmap_size, bypass_cache,
tzike2aca992017-09-05 08:50:544570 base::BindOnce(&WebContentsImpl::OnDidDownloadImage,
Avi Drissmana5a52dd2018-03-27 03:39:024571 weak_factory_.GetWeakPtr(), std::move(callback),
4572 download_id, url));
amistry9f01b772015-07-29 01:54:554573 return download_id;
[email protected]795c28972012-12-06 06:13:394574}
4575
[email protected]36ec24f2014-01-09 00:32:084576void WebContentsImpl::Find(int request_id,
4577 const base::string16& search_text,
Rakina Zata Amni3f77dff2018-09-08 16:19:434578 blink::mojom::FindOptionsPtr options) {
thestig6057a6b22015-11-12 23:01:334579 // Cowardly refuse to search for no text.
4580 if (search_text.empty()) {
4581 NOTREACHED();
4582 return;
4583 }
4584
Rakina Zata Amni3f77dff2018-09-08 16:19:434585 GetOrCreateFindRequestManager()->Find(request_id, search_text,
4586 std::move(options));
[email protected]36ec24f2014-01-09 00:32:084587}
4588
4589void WebContentsImpl::StopFinding(StopFindAction action) {
paulmeyerfeafc2d2017-04-25 21:46:404590 if (FindRequestManager* manager = GetFindRequestManager())
4591 manager->StopFinding(action);
[email protected]36ec24f2014-01-09 00:32:084592}
4593
Tommy Steimel18360512017-11-01 00:38:194594bool WebContentsImpl::WasEverAudible() {
4595 return was_ever_audible_;
4596}
4597
Avi Drissmana5a52dd2018-03-27 03:39:024598void WebContentsImpl::GetManifest(GetManifestCallback callback) {
Yuzu Saijofce18322020-05-14 05:02:094599 // TODO(yuzus, 1061899): Move this function to RenderFrameHostImpl.
4600 ManifestManagerHost* manifest_manager_host =
4601 ManifestManagerHost::GetOrCreateForCurrentDocument(GetMainFrame());
4602 manifest_manager_host->GetManifest(std::move(callback));
mlamouriefdca9d2014-09-16 16:55:404603}
4604
bokanece34a82016-01-28 19:49:464605void WebContentsImpl::ExitFullscreen(bool will_cause_resize) {
mlamouri7a78d6fd2015-01-17 13:23:534606 // Clean up related state and initiate the fullscreen exit.
James Hollyerd5c9de462020-03-10 19:02:454607 GetRenderViewHost()->GetWidget()->RejectMouseLockOrUnlockIfNecessary(
4608 blink::mojom::PointerLockResult::kUserRejected);
bokanece34a82016-01-28 19:49:464609 ExitFullscreenMode(will_cause_resize);
mlamouri7a78d6fd2015-01-17 13:23:534610}
4611
Avi Drissman1a55a9d62020-03-10 18:56:454612base::ScopedClosureRunner WebContentsImpl::ForSecurityDropFullscreen() {
Avi Drissman33972942020-06-19 14:16:014613 // Kick WebContentses that are "related" to this WebContents out of
4614 // fullscreen. This needs to be done with two passes, because it is simple to
4615 // walk _up_ the chain of openers and outer contents, but it not simple to
4616 // walk _down_ the chain.
4617
4618 // First, determine if any WebContents that is in fullscreen has this
4619 // WebContents as an upstream contents. Drop that WebContents out of
4620 // fullscreen if it does. This is theoretically quadratic-ish (fullscreen
4621 // contentses x each one's opener length) but neither of those is expected to
4622 // ever be a large number.
4623
4624 auto fullscreen_set_copy = *FullscreenContentsSet(GetBrowserContext());
4625 for (auto* fullscreen_contents : fullscreen_set_copy) {
Avi Drissman97aef042020-06-30 21:04:484626 // Checking IsFullscreen() for tabs in the fullscreen set may seem
4627 // redundant, but teeeeechnically fullscreen is run by the delegate, and
4628 // it's possible that the delegate's notion of fullscreen may have changed
4629 // outside of WebContents's notice.
4630 if (fullscreen_contents->IsFullscreen()) {
Avi Drissman33972942020-06-19 14:16:014631 auto opener_contentses = GetAllOpeningWebContents(fullscreen_contents);
4632 if (opener_contentses.count(this))
4633 fullscreen_contents->ExitFullscreen(true);
4634 }
4635 }
4636
4637 // Second, walk upstream from this WebContents, and drop the fullscreen of
4638 // all WebContentses that are in fullscreen. Block all the WebContentses in
4639 // the chain from entering fullscreen while the returned closure runner is
4640 // alive. It's OK that this set doesn't contain downstream WebContentses, as
4641 // any request to enter fullscreen will have the upstream of the WebContents
4642 // checked. (See CanEnterFullscreenMode().)
Avi Drissmanced52b62019-08-14 21:25:464643
Avi Drissman1a55a9d62020-03-10 18:56:454644 std::vector<base::WeakPtr<WebContentsImpl>> blocked_contentses;
4645
Avi Drissman33972942020-06-19 14:16:014646 for (auto* opener : GetAllOpeningWebContents(this)) {
Avi Drissman1a55a9d62020-03-10 18:56:454647 // Drop fullscreen if the WebContents is in it, and...
Avi Drissman97aef042020-06-30 21:04:484648 if (opener->IsFullscreen())
Avi Drissman33972942020-06-19 14:16:014649 opener->ExitFullscreen(true);
Avi Drissmanced52b62019-08-14 21:25:464650
Avi Drissman1a55a9d62020-03-10 18:56:454651 // ...block the WebContents from entering fullscreen until further notice.
Avi Drissman33972942020-06-19 14:16:014652 ++opener->fullscreen_blocker_count_;
4653 blocked_contentses.push_back(opener->weak_factory_.GetWeakPtr());
Avi Drissmanced52b62019-08-14 21:25:464654 }
Avi Drissman1a55a9d62020-03-10 18:56:454655
4656 return base::ScopedClosureRunner(base::BindOnce(
4657 [](std::vector<base::WeakPtr<WebContentsImpl>> blocked_contentses) {
4658 for (base::WeakPtr<WebContentsImpl>& web_contents :
4659 blocked_contentses) {
4660 if (web_contents) {
4661 DCHECK_GT(web_contents->fullscreen_blocker_count_, 0);
4662 --web_contents->fullscreen_blocker_count_;
4663 }
4664 }
4665 },
4666 std::move(blocked_contentses)));
Avi Drissmanced52b62019-08-14 21:25:464667}
4668
mariakhomenko44bdc4732015-04-29 01:55:384669void WebContentsImpl::ResumeLoadingCreatedWebContents() {
Nasko Oskov83a8cf92018-10-19 14:58:564670 if (delayed_load_url_params_.get()) {
4671 DCHECK(!delayed_open_url_params_);
4672 controller_.LoadURLWithParams(*delayed_load_url_params_.get());
4673 delayed_load_url_params_.reset(nullptr);
4674 return;
4675 }
4676
mariakhomenkoa4971c12015-07-21 19:04:374677 if (delayed_open_url_params_.get()) {
4678 OpenURL(*delayed_open_url_params_.get());
4679 delayed_open_url_params_.reset(nullptr);
4680 return;
4681 }
4682
mariakhomenko44bdc4732015-04-29 01:55:384683 // Resume blocked requests for both the RenderViewHost and RenderFrameHost.
4684 // TODO(brettw): It seems bogus to reach into here and initialize the host.
dfalcantarae6b7b46752015-07-10 18:14:164685 if (is_resume_pending_) {
4686 is_resume_pending_ = false;
avi0f1bbc9712015-11-17 02:58:134687 GetRenderViewHost()->GetWidget()->Init();
dfalcantarae6b7b46752015-07-10 18:14:164688 GetMainFrame()->Init();
4689 }
mariakhomenko44bdc4732015-04-29 01:55:384690}
4691
[email protected]b172aee2012-04-10 17:05:264692bool WebContentsImpl::FocusLocationBarByDefault() {
Xiyuan Xia2a66b112018-12-06 15:52:164693 if (should_focus_location_bar_by_default_)
[email protected]0c9406632013-02-08 01:13:334694 return true;
Xiyuan Xia2a66b112018-12-06 15:52:164695
[email protected]0c9406632013-02-08 01:13:334696 return delegate_ && delegate_->ShouldFocusLocationBarByDefault(this);
[email protected]0bfbf882011-12-22 18:19:274697}
4698
Tommy C. Li8bb5bf42019-01-18 18:45:514699void WebContentsImpl::SetFocusToLocationBar() {
[email protected]0bfbf882011-12-22 18:19:274700 if (delegate_)
Tommy C. Li8bb5bf42019-01-18 18:45:514701 delegate_->SetFocusToLocationBar();
[email protected]c40d6232011-03-25 00:16:214702}
4703
clamy0e119882015-07-31 16:12:334704void WebContentsImpl::DidStartNavigation(NavigationHandle* navigation_handle) {
Charles Harrison53096ba2017-12-15 15:39:484705 TRACE_EVENT1("navigation", "WebContentsImpl::DidStartNavigation",
4706 "navigation_handle", navigation_handle);
Alan Cutterab48fc02020-02-24 12:26:424707 if (navigation_handle->IsInMainFrame())
4708 favicon_urls_.clear();
Mounir Lamouri7b4a1f232019-12-23 16:32:104709
ericwilligersde386342016-10-19 02:35:094710 for (auto& observer : observers_)
4711 observer.DidStartNavigation(navigation_handle);
Becca Hughesd11d6502018-07-31 17:01:284712
Xiyuan Xia2a66b112018-12-06 15:52:164713 if (navigation_handle->IsInMainFrame()) {
4714 // When the browser is started with about:blank as the startup URL, focus
4715 // the location bar (which will also select its contents) so people can
4716 // simply begin typing to navigate elsewhere.
4717 //
4718 // We need to be careful not to trigger this for anything other than the
4719 // startup navigation. In particular, if we allow an attacker to open a
4720 // popup to about:blank, then navigate, focusing the Omnibox will cause the
4721 // end of the new URL to be scrolled into view instead of the start,
4722 // allowing the attacker to spoof other URLs. The conditions checked here
4723 // are all aimed at ensuring no such attacker-controlled navigation can
4724 // trigger this.
4725 should_focus_location_bar_by_default_ =
4726 controller_.IsInitialNavigation() &&
4727 !navigation_handle->IsRendererInitiated() &&
4728 navigation_handle->GetURL() == url::kAboutBlankURL;
4729 }
clamy0e119882015-07-31 16:12:334730}
4731
4732void WebContentsImpl::DidRedirectNavigation(
4733 NavigationHandle* navigation_handle) {
Charles Harrison53096ba2017-12-15 15:39:484734 TRACE_EVENT1("navigation", "WebContentsImpl::DidRedirectNavigation",
4735 "navigation_handle", navigation_handle);
ericwilligersde386342016-10-19 02:35:094736 for (auto& observer : observers_)
4737 observer.DidRedirectNavigation(navigation_handle);
jam5f358e52017-02-13 16:37:034738
4739 // Notify accessibility if this is a reload. This has to called on the
4740 // BrowserAccessibilityManager associated with the old RFHI.
4741 if (navigation_handle->GetReloadType() != ReloadType::NONE) {
Mohamed Abdelhalim95794962019-09-20 11:16:494742 NavigationRequest* request = NavigationRequest::From(navigation_handle);
jam5f358e52017-02-13 16:37:034743 BrowserAccessibilityManager* manager =
Mohamed Abdelhalim95794962019-09-20 11:16:494744 request->frame_tree_node()
jam5f358e52017-02-13 16:37:034745 ->current_frame_host()
4746 ->browser_accessibility_manager();
4747 if (manager)
4748 manager->UserIsReloading();
4749 }
clamy0e119882015-07-31 16:12:334750}
4751
clamyefca29e2015-09-17 00:22:114752void WebContentsImpl::ReadyToCommitNavigation(
4753 NavigationHandle* navigation_handle) {
Charles Harrison53096ba2017-12-15 15:39:484754 TRACE_EVENT1("navigation", "WebContentsImpl::ReadyToCommitNavigation",
4755 "navigation_handle", navigation_handle);
ericwilligersde386342016-10-19 02:35:094756 for (auto& observer : observers_)
4757 observer.ReadyToCommitNavigation(navigation_handle);
John Abd-El-Malekd4882642017-12-04 21:45:194758
Kenneth Russell954ed9ce2018-04-12 23:07:014759 // If any domains are blocked from accessing 3D APIs because they may
4760 // have caused the GPU to reset recently, unblock them here if the user
4761 // initiated this navigation. This implies that the user was involved in
4762 // the decision to navigate, so there's no concern about
4763 // denial-of-service issues. Want to do this as early as
4764 // possible to avoid race conditions with pages attempting to access
4765 // WebGL early on.
4766 //
4767 // TODO(crbug.com/617904): currently navigations initiated by the browser
4768 // (reload button, reload menu option, pressing return in the Omnibox)
4769 // return false from HasUserGesture(). If or when that is addressed,
4770 // remove the check for IsRendererInitiated() below.
4771 //
4772 // TODO(crbug.com/832180): HasUserGesture comes from the renderer
4773 // process and isn't validated. Until it is, don't trust it.
4774 if (!navigation_handle->IsRendererInitiated()) {
4775 GpuDataManagerImpl::GetInstance()->UnblockDomainFrom3DAPIs(
4776 navigation_handle->GetURL());
4777 }
4778
John Abd-El-Malekfde08e6da2017-12-05 04:06:244779 if (navigation_handle->IsSameDocument())
4780 return;
4781
Livvie Lin139bf442018-08-10 00:18:544782 // SSLInfo is not needed on subframe navigations since the main-frame
4783 // certificate is the only one that can be inspected (using the info
4784 // bubble) without refreshing the page with DevTools open.
Carlos ILaed1e7b2019-03-12 19:27:214785 // We don't call DidStartResourceResponse on net errors, since that results on
4786 // existing cert exceptions being revoked, which leads to weird behavior with
4787 // committed interstitials or while offline. We only need the error check for
4788 // the main frame case because unlike this method, SubresourceResponseStarted
4789 // does not get called on network errors.
4790 if (navigation_handle->IsInMainFrame() &&
4791 navigation_handle->GetNetErrorCode() == net::OK) {
Livvie Lin139bf442018-08-10 00:18:544792 controller_.ssl_manager()->DidStartResourceResponse(
Lukasz Anforowiczd334bba2020-02-10 23:32:554793 navigation_handle->GetURL(),
Camille Lamy62b826012019-02-26 09:15:474794 navigation_handle->GetSSLInfo().has_value()
4795 ? net::IsCertStatusError(
4796 navigation_handle->GetSSLInfo()->cert_status)
4797 : false);
4798 }
John Abd-El-Malekfde08e6da2017-12-05 04:06:244799
4800 SetNotWaitingForResponse();
clamyefca29e2015-09-17 00:22:114801}
4802
clamy0e119882015-07-31 16:12:334803void WebContentsImpl::DidFinishNavigation(NavigationHandle* navigation_handle) {
Charles Harrison53096ba2017-12-15 15:39:484804 TRACE_EVENT1("navigation", "WebContentsImpl::DidFinishNavigation",
4805 "navigation_handle", navigation_handle);
Steven Holte38041342018-06-22 22:00:574806
ericwilligersde386342016-10-19 02:35:094807 for (auto& observer : observers_)
4808 observer.DidFinishNavigation(navigation_handle);
clamy0e119882015-07-31 16:12:334809
Becca Hughesd11d6502018-07-31 17:01:284810 if (display_cutout_host_impl_)
4811 display_cutout_host_impl_->DidFinishNavigation(navigation_handle);
4812
jam5f358e52017-02-13 16:37:034813 if (navigation_handle->HasCommitted()) {
Ian Prestcdd843d2020-01-28 18:59:584814 // TODO(domfarolino, dmazzoni): Do this using WebContentsObserver. See
4815 // https://crbug.com/981271.
4816 BrowserAccessibilityManager* manager =
4817 static_cast<RenderFrameHostImpl*>(
4818 navigation_handle->GetRenderFrameHost())
4819 ->browser_accessibility_manager();
4820 if (manager) {
4821 if (navigation_handle->IsErrorPage()) {
4822 manager->NavigationFailed();
4823 } else {
4824 manager->NavigationSucceeded();
4825 }
4826 }
4827
Yue Ru Sun979c4b162019-11-06 16:11:044828 if (navigation_handle->IsInMainFrame()) {
4829 last_committed_source_id_including_same_document_ =
Steven Holte38041342018-06-22 22:00:574830 ukm::ConvertToSourceId(navigation_handle->GetNavigationId(),
4831 ukm::SourceIdType::NAVIGATION_ID);
Yue Ru Sun979c4b162019-11-06 16:11:044832
4833 if (!navigation_handle->IsSameDocument()) {
4834 was_ever_audible_ = false;
Yue Ru Sun979c4b162019-11-06 16:11:044835 }
Tommy Steimel18360512017-11-01 00:38:194836 }
Lan Weif81c6e7c2020-02-12 16:46:094837
4838 if (!navigation_handle->IsSameDocument())
4839 last_screen_orientation_change_time_ = base::TimeTicks();
dmazzonibf8cec42015-02-08 08:28:084840 }
Xiyuan Xia2a66b112018-12-06 15:52:164841
4842 // If we didn't end up on about:blank after setting this in DidStartNavigation
4843 // then don't focus the location bar.
4844 if (should_focus_location_bar_by_default_ &&
4845 navigation_handle->GetURL() != url::kAboutBlankURL) {
4846 should_focus_location_bar_by_default_ = false;
4847 }
yilkal796e89e2019-10-02 23:58:284848
yilkal172da772019-11-06 22:49:074849 if (navigation_handle->IsInMainFrame() && first_navigation_completed_)
4850 RecordMaxFrameCountUMA(max_loaded_frame_count_);
yilkal796e89e2019-10-02 23:58:284851
yilkal172da772019-11-06 22:49:074852 // If navigation has successfully finished in the main frame, set
4853 // |first_navigation_completed_| to true so that we will record
4854 // |max_loaded_frame_count_| above when future main frame navigations finish.
4855 if (navigation_handle->IsInMainFrame() && !navigation_handle->IsErrorPage()) {
4856 first_navigation_completed_ = true;
4857
4858 // Navigation has completed in main frame. Reset |max_loaded_frame_count_|.
4859 // |max_loaded_frame_count_| is not necessarily 1 if the navigation was
4860 // served from BackForwardCache.
4861 max_loaded_frame_count_ =
4862 GetMainFrame()->frame_tree_node()->GetFrameTreeSize();
yilkal796e89e2019-10-02 23:58:284863 }
Rakina Zata Amni347b70902020-07-22 10:49:044864
4865 if (web_preferences_) {
4866 // Update the WebPreferences for this WebContents that depends on changes
4867 // that might occur during navigation. This will only update the preferences
4868 // that needs to be updated (and won't cause an update/overwrite preferences
4869 // that needs to stay the same after navigations).
4870 bool value_changed_due_to_override =
4871 GetContentClient()->browser()->OverrideWebPreferencesAfterNavigation(
4872 this, web_preferences_.get());
4873 // We need to update the WebPreferences value on the renderer if the value
4874 // is changed due to the override above, or if the navigation is served from
4875 // the back-forward cache, because the WebPreferences value stored in the
4876 // renderer might be stale (because we don't send WebPreferences updates to
4877 // bfcached renderers).
4878 // TODO(rakina): Maybe handle the back-forward cache case in
4879 // ReadyToCommitNavigation instead?
4880 if (value_changed_due_to_override ||
4881 NavigationRequest::From(navigation_handle)
4882 ->IsServedFromBackForwardCache()) {
4883 SetWebPreferences(*web_preferences_.get());
4884 }
4885 }
[email protected]400992b2012-06-14 00:03:544886}
4887
[email protected]b80624c2014-02-09 02:46:554888void WebContentsImpl::DidFailLoadWithError(
4889 RenderFrameHostImpl* render_frame_host,
[email protected]b80624c2014-02-09 02:46:554890 const GURL& url,
Dave Tapuska924ef3c2020-01-22 18:20:594891 int error_code) {
ericwilligersde386342016-10-19 02:35:094892 for (auto& observer : observers_) {
Dave Tapuska924ef3c2020-01-22 18:20:594893 observer.DidFailLoad(render_frame_host, url, error_code);
ericwilligersde386342016-10-19 02:35:094894 }
[email protected]b80624c2014-02-09 02:46:554895}
4896
[email protected]52913802013-12-10 05:52:184897void WebContentsImpl::NotifyChangedNavigationState(
4898 InvalidateTypes changed_flags) {
4899 NotifyNavigationStateChanged(changed_flags);
4900}
4901
lukasza1d025732016-09-28 21:36:084902bool WebContentsImpl::ShouldTransferNavigation(bool is_main_frame_navigation) {
creis29460272015-12-16 04:38:224903 if (!delegate_)
4904 return true;
lukasza1d025732016-09-28 21:36:084905 return delegate_->ShouldTransferNavigation(is_main_frame_navigation);
creis29460272015-12-16 04:38:224906}
4907
[email protected]aa62afd2014-04-22 19:22:464908bool WebContentsImpl::ShouldPreserveAbortedURLs() {
4909 if (!delegate_)
4910 return false;
4911 return delegate_->ShouldPreserveAbortedURLs(this);
4912}
4913
[email protected]0d0f4c492014-04-02 06:42:574914void WebContentsImpl::DidNavigateMainFramePreCommit(
[email protected]5cfbddc2014-06-23 23:52:234915 bool navigation_is_within_page) {
[email protected]0d0f4c492014-04-02 06:42:574916 // Ensure fullscreen mode is exited before committing the navigation to a
4917 // different page. The next page will not start out assuming it is in
4918 // fullscreen mode.
[email protected]5cfbddc2014-06-23 23:52:234919 if (navigation_is_within_page) {
[email protected]dfc39cb2014-04-09 22:58:194920 // No page change? Then, the renderer and browser can remain in fullscreen.
[email protected]0d0f4c492014-04-02 06:42:574921 return;
4922 }
Avi Drissman97aef042020-06-30 21:04:484923 if (IsFullscreen())
bokanece34a82016-01-28 19:49:464924 ExitFullscreen(false);
Avi Drissman97aef042020-06-30 21:04:484925 DCHECK(!IsFullscreen());
Joe Downingc1a57efe2018-05-09 18:23:544926
4927 // Clean up keyboard lock state when navigating.
4928 CancelKeyboardLock(keyboard_lock_widget_);
[email protected]0d0f4c492014-04-02 06:42:574929}
4930
[email protected]37567b432014-02-12 01:12:224931void WebContentsImpl::DidNavigateMainFramePostCommit(
sky8b00392d2015-01-10 00:30:284932 RenderFrameHostImpl* render_frame_host,
[email protected]37567b432014-02-12 01:12:224933 const LoadCommittedDetails& details,
4934 const FrameHostMsg_DidCommitProvisionalLoad_Params& params) {
4935 if (details.is_navigation_to_different_page()) {
4936 // Clear the status bubble. This is a workaround for a bug where WebKit
4937 // doesn't let us know that the cursor left an element during a
4938 // transition (this is also why the mouse cursor remains as a hand after
4939 // clicking on a link); see bugs 1184641 and 980803. We don't want to
4940 // clear the bubble when a user navigates to a named anchor in the same
4941 // page.
Lukasz Anforowiczfd707142018-02-07 19:46:134942 ClearTargetURL();
lanwei9d343ad22015-02-11 01:46:004943
4944 RenderWidgetHostViewBase* rwhvb =
4945 static_cast<RenderWidgetHostViewBase*>(GetRenderWidgetHostView());
4946 if (rwhvb)
4947 rwhvb->OnDidNavigateMainFrameToNewPage();
[email protected]37567b432014-02-12 01:12:224948 }
4949
[email protected]e85165c642014-06-10 14:34:314950 if (delegate_)
[email protected]37567b432014-02-12 01:12:224951 delegate_->DidNavigateMainFramePostCommit(this);
[email protected]e85165c642014-06-10 14:34:314952 view_->SetOverscrollControllerEnabled(CanOverscrollContent());
Shivani Sharma985474f2019-05-23 21:36:474953
Carlos Caballero91fffb22019-10-29 15:24:304954 if (details.is_navigation_to_different_page() &&
4955 GetRenderViewHost()->did_first_visually_non_empty_paint()) {
4956 // This event will not fire again if the page is restored from the
4957 // BackForwardCache. So fire it ourselves if needed.
4958 DidFirstVisuallyNonEmptyPaint(GetRenderViewHost());
4959 }
Carlos Caballero1215f882019-10-29 15:58:434960
4961 if (GetRenderViewHost()->theme_color() != last_sent_theme_color_) {
4962 // This event will not fire again if the page is restored from the
4963 // BackForwardCache. So fire it ourselves if needed.
4964 OnThemeColorChanged(GetRenderViewHost());
4965 }
[email protected]37567b432014-02-12 01:12:224966}
4967
4968void WebContentsImpl::DidNavigateAnyFramePostCommit(
4969 RenderFrameHostImpl* render_frame_host,
4970 const LoadCommittedDetails& details,
4971 const FrameHostMsg_DidCommitProvisionalLoad_Params& params) {
[email protected]b4c84012014-04-28 19:51:104972 // Now that something has committed, we don't need to track whether the
4973 // initial page has been accessed.
4974 has_accessed_initial_document_ = false;
4975
[email protected]37567b432014-02-12 01:12:224976 // If we navigate off the page, close all JavaScript dialogs.
eugenebutee08663a2017-04-27 17:43:124977 if (!details.is_same_document)
creis89a0f782015-05-27 16:13:174978 CancelActiveAndPendingDialogs();
[email protected]37567b432014-02-12 01:12:224979
avid53461d2016-02-25 17:15:044980 // If this is a user-initiated navigation, start allowing JavaScript dialogs
4981 // again.
avi5d3b8692016-10-12 22:00:464982 if (params.gesture == NavigationGestureUser && dialog_manager_) {
avi6879fcf2017-01-21 05:27:534983 dialog_manager_->CancelDialogs(this, /*reset_state=*/true);
avi5d3b8692016-10-12 22:00:464984 }
[email protected]37567b432014-02-12 01:12:224985}
4986
[email protected]277857a2014-06-03 10:38:014987bool WebContentsImpl::CanOverscrollContent() const {
[email protected]e85165c642014-06-10 14:34:314988 // Disable overscroll when touch emulation is on. See crbug.com/369938.
[email protected]34ff1cfc2014-08-20 06:16:054989 if (force_disable_overscroll_content_)
[email protected]e85165c642014-06-10 14:34:314990 return false;
4991
[email protected]37567b432014-02-12 01:12:224992 if (delegate_)
4993 return delegate_->CanOverscrollContent();
4994
4995 return false;
4996}
4997
Carlos Caballero1215f882019-10-29 15:58:434998void WebContentsImpl::OnThemeColorChanged(RenderViewHostImpl* source) {
4999 if (source->did_first_visually_non_empty_paint() &&
5000 last_sent_theme_color_ != source->theme_color()) {
ericwilligersde386342016-10-19 02:35:095001 for (auto& observer : observers_)
Peter Kastingb14e5782020-01-06 23:55:045002 observer.DidChangeThemeColor();
Carlos Caballero1215f882019-10-29 15:58:435003 last_sent_theme_color_ = source->theme_color();
peter90afaba2015-06-01 12:05:295004 }
[email protected]e7104762014-06-20 19:17:255005}
5006
Gyuyoung Kim1905f7c2020-05-02 00:00:245007void WebContentsImpl::DidLoadResourceFromMemoryCache(
nicka0ac8382016-12-15 23:59:235008 RenderFrameHostImpl* source,
[email protected]724159a2010-12-30 01:11:185009 const GURL& url,
[email protected]70435962011-08-02 20:13:285010 const std::string& http_method,
[email protected]6d6cfb3a2012-05-23 22:53:185011 const std::string& mime_type,
Kinuko Yasuda433f710a2020-02-14 02:11:585012 network::mojom::RequestDestination request_destination) {
ericwilligersde386342016-10-19 02:35:095013 for (auto& observer : observers_)
Kinuko Yasuda433f710a2020-02-14 02:11:585014 observer.DidLoadResourceFromMemoryCache(url, mime_type,
5015 request_destination);
[email protected]b0f724c2013-09-05 04:21:135016
Steven Bingler674fb0822018-12-12 18:04:525017 if (url.is_valid() && url.SchemeIsHTTPOrHTTPS()) {
nicka0ac8382016-12-15 23:59:235018 StoragePartition* partition = source->GetProcess()->GetStoragePartition();
John Abd-El-Malekf4fe9d82019-07-31 04:05:595019 partition->GetNetworkContext()->NotifyExternalCacheHit(
Matt Menke2a33dee2019-12-02 17:01:545020 url, http_method, source->GetNetworkIsolationKey());
[email protected]8bfc8272013-09-09 20:10:535021 }
[email protected]724159a2010-12-30 01:11:185022}
5023
carloskd9d97942017-02-16 08:58:095024void WebContentsImpl::DidDisplayInsecureContent() {
estarkc2273502016-10-12 22:03:025025 controller_.ssl_manager()->DidDisplayMixedContent();
[email protected]724159a2010-12-30 01:11:185026}
5027
Dave Tapuskaa657d872019-10-21 20:23:215028void WebContentsImpl::DidContainInsecureFormAction() {
elawrenceb2ac2a232017-03-27 21:46:255029 controller_.ssl_manager()->DidContainInsecureFormAction();
5030}
5031
Julie Jeongeun Kim0b103492020-01-29 05:30:515032void WebContentsImpl::DocumentAvailableInMainFrame() {
5033 for (auto& observer : observers_)
5034 observer.DocumentAvailableInMainFrame();
5035}
5036
nicka0ac8382016-12-15 23:59:235037void WebContentsImpl::OnDidRunInsecureContent(RenderFrameHostImpl* source,
5038 const GURL& security_origin,
estark910b4572015-12-09 20:55:415039 const GURL& target_url) {
nicka0ac8382016-12-15 23:59:235040 // TODO(nick, estark): Should we call FilterURL using |source|'s process on
5041 // these parameters? |target_url| seems unused, except for a log message. And
5042 // |security_origin| might be replaceable with the origin of the main frame.
carloskd9d97942017-02-16 08:58:095043 DidRunInsecureContent(security_origin, target_url);
5044}
5045
5046void WebContentsImpl::DidRunInsecureContent(const GURL& security_origin,
5047 const GURL& target_url) {
[email protected]9450c462013-11-23 01:22:585048 LOG(WARNING) << security_origin << " ran insecure content from "
5049 << target_url.possibly_invalid_spec();
[email protected]e6e30ac2014-01-13 21:24:395050 RecordAction(base::UserMetricsAction("SSL.RanInsecureContent"));
estark910b4572015-12-09 20:55:415051 if (base::EndsWith(security_origin.spec(), kDotGoogleDotCom,
brettwa7ff1b292015-07-16 17:49:295052 base::CompareCase::INSENSITIVE_ASCII))
[email protected]e6e30ac2014-01-13 21:24:395053 RecordAction(base::UserMetricsAction("SSL.RanInsecureContentGoogle"));
estarkc2273502016-10-12 22:03:025054 controller_.ssl_manager()->DidRunMixedContent(security_origin);
[email protected]724159a2010-12-30 01:11:185055}
5056
carloskd9d97942017-02-16 08:58:095057void WebContentsImpl::PassiveInsecureContentFound(const GURL& resource_url) {
Sergey Kuznetsov3ebc01b2018-02-01 04:57:365058 if (delegate_) {
5059 delegate_->PassiveInsecureContentFound(resource_url);
5060 }
carloskd9d97942017-02-16 08:58:095061}
5062
5063bool WebContentsImpl::ShouldAllowRunningInsecureContent(
carloskd9d97942017-02-16 08:58:095064 bool allowed_per_prefs,
5065 const url::Origin& origin,
5066 const GURL& resource_url) {
Sergey Kuznetsov3ebc01b2018-02-01 04:57:365067 if (delegate_) {
Carlos IL29294a62020-05-15 00:21:165068 return delegate_->ShouldAllowRunningInsecureContent(this, allowed_per_prefs,
5069 origin, resource_url);
Sergey Kuznetsov3ebc01b2018-02-01 04:57:365070 }
5071
5072 return allowed_per_prefs;
carloskd9d97942017-02-16 08:58:095073}
5074
Lukasz Anforowicze1b954d92017-10-30 21:28:065075void WebContentsImpl::ViewSource(RenderFrameHostImpl* frame) {
5076 DCHECK_EQ(this, WebContents::FromRenderFrameHost(frame));
5077
5078 // Don't do anything if there is no |delegate_| that could accept and show the
5079 // new WebContents containing the view-source.
5080 if (!delegate_)
5081 return;
5082
5083 // Use the last committed entry, since the pending entry hasn't loaded yet and
5084 // won't be copied into the cloned tab.
5085 NavigationEntryImpl* last_committed_entry =
Fergal Daly09d6c762020-05-29 02:05:185086 GetController().GetLastCommittedEntry();
Lukasz Anforowicze1b954d92017-10-30 21:28:065087 if (!last_committed_entry)
5088 return;
5089
5090 FrameNavigationEntry* frame_entry =
5091 last_committed_entry->GetFrameEntry(frame->frame_tree_node());
5092 if (!frame_entry)
5093 return;
5094
5095 // Any new WebContents opened while this WebContents is in fullscreen can be
5096 // used to confuse the user, so drop fullscreen.
Avi Drissman1a55a9d62020-03-10 18:56:455097 base::ScopedClosureRunner fullscreen_block = ForSecurityDropFullscreen();
5098 // The new view source contents will be independent of this contents, so
5099 // release the fullscreen block.
5100 fullscreen_block.RunAndReset();
Lukasz Anforowicze1b954d92017-10-30 21:28:065101
5102 // We intentionally don't share the SiteInstance with the original frame so
5103 // that view source has a consistent process model and always ends up in a new
5104 // process (https://crbug.com/699493).
5105 scoped_refptr<SiteInstanceImpl> site_instance_for_view_source = nullptr;
Lukasz Anforowicz435bcb582019-07-12 20:50:065106 // Referrer and initiator are not important, because view-source should not
5107 // hit the network, but should be served from the cache instead.
Lukasz Anforowicze1b954d92017-10-30 21:28:065108 Referrer referrer_for_view_source;
Lukasz Anforowicz435bcb582019-07-12 20:50:065109 base::Optional<url::Origin> initiator_for_view_source = base::nullopt;
Lukasz Anforowicze1b954d92017-10-30 21:28:065110 // Do not restore title, derive it from the url.
5111 base::string16 title_for_view_source;
5112 auto navigation_entry = std::make_unique<NavigationEntryImpl>(
5113 site_instance_for_view_source, frame_entry->url(),
Lukasz Anforowicz435bcb582019-07-12 20:50:065114 referrer_for_view_source, initiator_for_view_source,
5115 title_for_view_source, ui::PAGE_TRANSITION_LINK,
Marijn Kruisselbrink7a0d5e182018-05-24 22:55:095116 /* is_renderer_initiated = */ false,
5117 /* blob_url_loader_factory = */ nullptr);
Joel Hockey891e88062020-04-30 05:38:445118 const GURL url(content::kViewSourceScheme + std::string(":") +
5119 frame_entry->url().spec());
5120 navigation_entry->SetVirtualURL(url);
Matt Menkea56c5992020-04-10 20:53:565121 navigation_entry->set_isolation_info(
5122 frame->GetIsolationInfoForSubresources());
Yao Xiaof68f57d2019-10-03 04:20:055123
Lukasz Anforowicze1b954d92017-10-30 21:28:065124 // Do not restore scroller position.
5125 // TODO(creis, lukasza, arthursonzogni): Do not reuse the original PageState,
5126 // but start from a new one and only copy the needed data.
5127 const PageState& new_page_state =
5128 frame_entry->page_state().RemoveScrollOffset();
5129
5130 scoped_refptr<FrameNavigationEntry> new_frame_entry =
5131 navigation_entry->root_node()->frame_entry;
5132 new_frame_entry->set_method(frame_entry->method());
5133 new_frame_entry->SetPageState(new_page_state);
5134
5135 // Create a new WebContents, which is used to display the source code.
erikchenbee5c9622018-04-27 19:30:255136 std::unique_ptr<WebContents> view_source_contents =
Erik Chenbb8e738e2018-04-28 14:10:435137 Create(CreateParams(GetBrowserContext()));
Lukasz Anforowicze1b954d92017-10-30 21:28:065138
5139 // Restore the previously created NavigationEntry.
5140 std::vector<std::unique_ptr<NavigationEntry>> navigation_entries;
5141 navigation_entries.push_back(std::move(navigation_entry));
5142 view_source_contents->GetController().Restore(0, RestoreType::CURRENT_SESSION,
5143 &navigation_entries);
5144
5145 // Add |view_source_contents| as a new tab.
5146 gfx::Rect initial_rect;
5147 constexpr bool kUserGesture = true;
5148 bool ignored_was_blocked;
Joel Hockey891e88062020-04-30 05:38:445149 delegate_->AddNewContents(this, std::move(view_source_contents), url,
Lukasz Anforowicze1b954d92017-10-30 21:28:065150 WindowOpenDisposition::NEW_FOREGROUND_TAB,
5151 initial_rect, kUserGesture, &ignored_was_blocked);
5152 // Note that the |delegate_| could have deleted |view_source_contents| during
5153 // AddNewContents method call.
5154}
5155
Lukasz Anforowiczd334bba2020-02-10 23:32:555156void WebContentsImpl::SubresourceResponseStarted(const GURL& url,
5157 net::CertStatus cert_status) {
5158 controller_.ssl_manager()->DidStartResourceResponse(url, cert_status);
Jay Civellid5904f02018-03-23 19:43:575159 SetNotWaitingForResponse();
5160}
5161
Jay Civelli116683f2018-03-27 19:56:235162void WebContentsImpl::ResourceLoadComplete(
Jay Civelli52a63302018-04-30 22:39:555163 RenderFrameHost* render_frame_host,
Clark DuVall36164bd2018-08-09 22:49:085164 const GlobalRequestID& request_id,
Minggang Wangee5af392020-02-05 02:55:285165 blink::mojom::ResourceLoadInfoPtr resource_load_info) {
John Abd-El-Malek9b8f7fa2017-12-04 23:52:255166 for (auto& observer : observers_) {
Clark DuVall36164bd2018-08-09 22:49:085167 observer.ResourceLoadComplete(render_frame_host, request_id,
5168 *resource_load_info);
John Abd-El-Malek9b8f7fa2017-12-04 23:52:255169 }
John Abd-El-Malekd4882642017-12-04 21:45:195170}
5171
Rakina Zata Amni347b70902020-07-22 10:49:045172const WebPreferences& WebContentsImpl::GetOrCreateWebPreferences() {
5173 // Compute WebPreferences based on the current state if it's null.
5174 if (!web_preferences_)
Rakina Zata Amni4029b6d2020-07-28 02:36:205175 OnWebPreferencesChanged();
Rakina Zata Amni347b70902020-07-22 10:49:045176 return *web_preferences_.get();
5177}
5178
5179bool WebContentsImpl::IsWebPreferencesSet() const {
5180 return web_preferences_.get();
5181}
5182
5183void WebContentsImpl::SetWebPreferences(const WebPreferences& prefs) {
5184 web_preferences_ = std::make_unique<WebPreferences>(prefs);
5185 // Get all the RenderViewHosts (except the ones for currently back-forward
5186 // cached pages), and make them send the current WebPreferences
5187 // to the renderer. WebPreferences updates for back-forward cached pages will
5188 // be sent when we restore those pages from the back-forward cache.
5189 for (auto& rvh : frame_tree_.render_view_hosts()) {
5190 rvh.second->SendWebPreferencesToRenderer();
5191 }
5192}
5193
5194void WebContentsImpl::RecomputeWebPreferencesSlow() {
Rakina Zata Amni4029b6d2020-07-28 02:36:205195 // OnWebPreferencesChanged is a no-op when this is true.
5196 if (updating_web_preferences_)
5197 return;
Rakina Zata Amni347b70902020-07-22 10:49:045198 // Resets |web_preferences_| so that we won't have any cached value for slow
5199 // attributes (which won't get recomputed if we have pre-existing values for
5200 // them).
5201 web_preferences_.reset();
Rakina Zata Amni4029b6d2020-07-28 02:36:205202 OnWebPreferencesChanged();
Rakina Zata Amni347b70902020-07-22 10:49:045203}
5204
Wei Li5bb659742018-02-14 03:07:585205void WebContentsImpl::PrintCrossProcessSubframe(
5206 const gfx::Rect& rect,
5207 int document_cookie,
5208 RenderFrameHost* subframe_host) {
Wei Li5c0b4d12018-03-22 15:05:225209 auto* outer_contents = GetOuterWebContents();
5210 if (outer_contents) {
5211 // When an extension or app page is printed, the content should be
5212 // composited with outer content, so the outer contents should handle the
5213 // print request.
5214 outer_contents->PrintCrossProcessSubframe(rect, document_cookie,
5215 subframe_host);
5216 return;
5217 }
5218
Wei Li5bb659742018-02-14 03:07:585219 // If there is no delegate such as in tests or during deletion, do nothing.
5220 if (!delegate_)
5221 return;
5222
5223 delegate_->PrintCrossProcessSubframe(this, rect, document_cookie,
5224 subframe_host);
5225}
5226
ckitagawa8695799b2020-02-05 16:08:495227void WebContentsImpl::CapturePaintPreviewOfCrossProcessSubframe(
5228 const gfx::Rect& rect,
5229 const base::UnguessableToken& guid,
5230 RenderFrameHost* render_frame_host) {
5231 if (!delegate_)
5232 return;
ckitagawac43ac982020-07-07 17:41:175233 delegate_->CapturePaintPreviewOfSubframe(this, rect, guid, render_frame_host);
ckitagawa8695799b2020-02-05 16:08:495234}
5235
rob.buis300b0872017-03-10 20:43:585236#if defined(OS_ANDROID)
5237base::android::ScopedJavaLocalRef<jobject>
5238WebContentsImpl::GetJavaRenderFrameHostDelegate() {
5239 return GetJavaWebContents();
5240}
5241#endif
5242
Hiroki Nakagawa39d1bbe2020-07-14 16:15:525243void WebContentsImpl::OnDidDisplayContentWithCertificateErrors() {
estarkc2273502016-10-12 22:03:025244 controller_.ssl_manager()->DidDisplayContentWithCertErrors();
estark910b4572015-12-09 20:55:415245}
5246
5247void WebContentsImpl::OnDidRunContentWithCertificateErrors(
Zhuoyu Qian4353f7f2018-01-08 05:33:005248 RenderFrameHostImpl* source) {
Emily Starkabbdd662020-06-05 02:40:135249 // For RenderFrameHosts that are inactive and going to be discarded, we can
5250 // disregard this message; there's no need to update the UI if the UI will
5251 // never be shown again.
5252 //
5253 // We still process this message for speculative RenderFrameHosts. This can
5254 // happen when a subframe's main resource has a certificate error. The
5255 // currently committed navigation entry will get marked as having run insecure
5256 // content and that will carry over to the navigation entry for the
5257 // speculative RFH when it commits.
5258 if (source->lifecycle_state() !=
5259 RenderFrameHostImpl::LifecycleState::kSpeculative &&
5260 source->IsInactiveAndDisallowReactivation()) {
estarkaeda1422016-05-10 02:31:125261 return;
Emily Starkabbdd662020-06-05 02:40:135262 }
estarkcd2e30c2016-08-12 06:51:155263 controller_.ssl_manager()->DidRunContentWithCertErrors(
Emily Starkabbdd662020-06-05 02:40:135264 source->GetMainFrame()->GetLastCommittedOrigin().GetURL());
estark910b4572015-12-09 20:55:415265}
5266
Kouhei Ueno97e0990d2019-09-25 02:57:275267void WebContentsImpl::DOMContentLoaded(RenderFrameHost* render_frame_host) {
ericwilligersde386342016-10-19 02:35:095268 for (auto& observer : observers_)
Kouhei Uenof6e96242019-09-27 01:54:355269 observer.DOMContentLoaded(render_frame_host);
[email protected]724159a2010-12-30 01:11:185270}
5271
Gyuyoung Kim525e0c5d2020-03-09 15:04:195272void WebContentsImpl::OnDidFinishLoad(RenderFrameHost* render_frame_host,
nicka0ac8382016-12-15 23:59:235273 const GURL& url) {
[email protected]8b3af1e2014-01-24 13:29:125274 GURL validated_url(url);
Gyuyoung Kim525e0c5d2020-03-09 15:04:195275 render_frame_host->GetProcess()->FilterURL(false, &validated_url);
[email protected]028053d42014-03-05 22:20:375276
ericwilligersde386342016-10-19 02:35:095277 for (auto& observer : observers_)
Gyuyoung Kim525e0c5d2020-03-09 15:04:195278 observer.DidFinishLoad(render_frame_host, validated_url);
yilkal172da772019-11-06 22:49:075279
5280 size_t tree_size = frame_tree_.root()->GetFrameTreeSize();
5281 if (max_loaded_frame_count_ < tree_size)
5282 max_loaded_frame_count_ = tree_size;
5283
Gyuyoung Kim525e0c5d2020-03-09 15:04:195284 if (!render_frame_host->GetParent())
yilkal172da772019-11-06 22:49:075285 UMA_HISTOGRAM_COUNTS_1000("Navigation.MainFrame.FrameCount", tree_size);
[email protected]1a55c5be2011-11-29 11:36:315286}
5287
Dave Tapuska549baab2019-03-19 15:52:405288void WebContentsImpl::OnGoToEntryAtOffset(RenderFrameHostImpl* source,
Julie Jeongeun Kim144f8192020-01-18 06:59:035289 int32_t offset,
Dave Tapuska043b94a2019-09-10 14:14:355290 bool has_user_gesture) {
Arthur Sonzognia7d715a2018-09-20 16:11:135291 // Non-user initiated navigations coming from the renderer should be ignored
5292 // if there is an ongoing browser-initiated navigation.
5293 // See https://crbug.com/879965.
5294 // TODO(arthursonzogni): See if this should check for ongoing navigations in
5295 // the frame(s) affected by the session history navigation, rather than just
5296 // the main frame.
Kevin McNee96851e12020-07-16 03:43:225297 if (Navigator::ShouldIgnoreIncomingRendererRequest(
5298 frame_tree_.root()->navigation_request(), has_user_gesture))
5299 return;
Arthur Sonzognia7d715a2018-09-20 16:11:135300
nicka0ac8382016-12-15 23:59:235301 // All frames are allowed to navigate the global history.
Dave Tapuska8bfd84c2019-03-26 20:47:165302 if (!delegate_ || delegate_->OnGoToEntryOffset(offset)) {
arthursonzognib93a4472020-04-10 07:38:005303 if (source->IsSandboxed(network::mojom::WebSandboxFlags::kTopNavigation)) {
Dave Tapuska8bfd84c2019-03-26 20:47:165304 // Keep track of whether this is a session history from a sandboxed iframe
5305 // with top level navigation disallowed.
5306 controller_.GoToOffsetInSandboxedFrame(offset,
5307 source->GetFrameTreeNodeId());
5308 } else {
5309 controller_.GoToOffset(offset);
5310 }
5311 }
[email protected]216813952011-05-19 22:21:265312}
5313
Dave Tapuska4c02911b2020-02-14 22:52:075314void WebContentsImpl::OnPageScaleFactorChanged(RenderFrameHostImpl* source,
nicka0ac8382016-12-15 23:59:235315 float page_scale_factor) {
mcnee336ea2c2017-05-23 22:50:595316#if !defined(OS_ANDROID)
5317 // While page scale factor is used on mobile, this PageScaleFactorIsOne logic
5318 // is only needed on desktop.
mcnee432e47d2015-11-09 19:37:465319 bool is_one = page_scale_factor == 1.f;
5320 if (is_one != page_scale_factor_is_one_) {
5321 page_scale_factor_is_one_ = is_one;
5322
5323 HostZoomMapImpl* host_zoom_map =
5324 static_cast<HostZoomMapImpl*>(HostZoomMap::GetForWebContents(this));
5325
nicka0ac8382016-12-15 23:59:235326 if (host_zoom_map) {
mcnee432e47d2015-11-09 19:37:465327 host_zoom_map->SetPageScaleFactorIsOneForView(
Dave Tapuska4c02911b2020-02-14 22:52:075328 source->GetProcess()->GetID(),
5329 source->GetRenderViewHost()->GetRoutingID(),
mcnee432e47d2015-11-09 19:37:465330 page_scale_factor_is_one_);
5331 }
5332 }
mcnee336ea2c2017-05-23 22:50:595333#endif // !defined(OS_ANDROID)
mcnee432e47d2015-11-09 19:37:465334
ericwilligersde386342016-10-19 02:35:095335 for (auto& observer : observers_)
5336 observer.OnPageScaleFactorChanged(page_scale_factor);
mcnee432e47d2015-11-09 19:37:465337}
5338
W. James MacLean62198672019-06-04 16:46:105339void WebContentsImpl::OnTextAutosizerPageInfoChanged(
Henrique Ferreiroba535472020-04-21 11:38:365340 RenderFrameHostImpl* source,
5341 blink::mojom::TextAutosizerPageInfoPtr page_info) {
W. James MacLean62198672019-06-04 16:46:105342 // Keep a copy of |page_info| in case we create a new RenderView before
5343 // the next update.
Henrique Ferreiroba535472020-04-21 11:38:365344 text_autosizer_page_info_.main_frame_width = page_info->main_frame_width;
5345 text_autosizer_page_info_.main_frame_layout_width =
5346 page_info->main_frame_layout_width;
5347 text_autosizer_page_info_.device_scale_adjustment =
5348 page_info->device_scale_adjustment;
W. James MacLean62198672019-06-04 16:46:105349 frame_tree_.root()->render_manager()->SendPageMessage(
5350 new PageMsg_UpdateTextAutosizerPageInfoForRemoteMainFrames(
Henrique Ferreiroba535472020-04-21 11:38:365351 MSG_ROUTING_NONE, text_autosizer_page_info_),
W. James MacLean62198672019-06-04 16:46:105352 source->GetSiteInstance());
5353}
5354
Kent Tamura8c9e0562018-11-06 07:02:195355void WebContentsImpl::EnumerateDirectory(
5356 RenderFrameHost* render_frame_host,
Kent Tamura3abb32d2020-07-02 00:23:015357 scoped_refptr<FileChooserImpl::FileSelectListenerImpl> listener,
Kent Tamura8c9e0562018-11-06 07:02:195358 const base::FilePath& directory_path) {
Avi Drissman1a55a9d62020-03-10 18:56:455359 // Any explicit focusing of another window while this WebContents is in
5360 // fullscreen can be used to confuse the user, so drop fullscreen.
5361 base::ScopedClosureRunner fullscreen_block = ForSecurityDropFullscreen();
5362 listener->SetFullscreenBlock(std::move(fullscreen_block));
5363
Kent Tamura8c9e0562018-11-06 07:02:195364 if (delegate_)
5365 delegate_->EnumerateDirectory(this, std::move(listener), directory_path);
5366 else
5367 listener->FileSelectionCanceled();
[email protected]3a29a6e2011-08-24 18:26:215368}
5369
Dave Tapuskaf9fc3122019-10-18 19:42:555370void WebContentsImpl::RegisterProtocolHandler(RenderFrameHostImpl* source,
5371 const std::string& protocol,
5372 const GURL& url,
5373 const base::string16& title,
5374 bool user_gesture) {
nicka0ac8382016-12-15 23:59:235375 // TODO(nick): Do we need to apply FilterURL to |url|?
[email protected]e5f2de02012-07-20 22:15:435376 if (!delegate_)
5377 return;
5378
Raymes Khoury58373df2019-08-06 06:29:205379 if (!AreValidRegisterProtocolHandlerArguments(
5380 protocol, url, source->GetLastCommittedOrigin())) {
5381 ReceivedBadMessage(source->GetProcess(),
5382 bad_message::REGISTER_PROTOCOL_HANDLER_INVALID_URL);
[email protected]b9535422012-02-09 01:47:595383 return;
Raymes Khoury58373df2019-08-06 06:29:205384 }
[email protected]8f810632013-06-06 22:33:325385
Carlos Caballero8a6c6b72020-07-20 16:56:225386 delegate_->RegisterProtocolHandler(source, protocol, url, user_gesture);
[email protected]7d189022011-08-25 22:54:205387}
5388
Dave Tapuskaf9fc3122019-10-18 19:42:555389void WebContentsImpl::UnregisterProtocolHandler(RenderFrameHostImpl* source,
5390 const std::string& protocol,
5391 const GURL& url,
5392 bool user_gesture) {
nicka0ac8382016-12-15 23:59:235393 // TODO(nick): Do we need to apply FilterURL to |url|?
[email protected]f5273e52014-07-14 16:30:205394 if (!delegate_)
5395 return;
5396
Raymes Khoury58373df2019-08-06 06:29:205397 if (!AreValidRegisterProtocolHandlerArguments(
5398 protocol, url, source->GetLastCommittedOrigin())) {
5399 ReceivedBadMessage(source->GetProcess(),
5400 bad_message::REGISTER_PROTOCOL_HANDLER_INVALID_URL);
[email protected]f5273e52014-07-14 16:30:205401 return;
Raymes Khoury58373df2019-08-06 06:29:205402 }
[email protected]f5273e52014-07-14 16:30:205403
Carlos Caballero8a6c6b72020-07-20 16:56:225404 delegate_->UnregisterProtocolHandler(source, protocol, url, user_gesture);
[email protected]f5273e52014-07-14 16:30:205405}
5406
Marijn Kruisselbrink7f519672019-02-05 19:12:505407void WebContentsImpl::OnAppCacheAccessed(const GURL& manifest_url,
[email protected]b172aee2012-04-10 17:05:265408 bool blocked_by_policy) {
nicka0ac8382016-12-15 23:59:235409 // TODO(nick): Should we consider |source| here? Should we call FilterURL on
5410 // |manifest_url|?
5411
[email protected]7fc4bbb2011-09-08 21:23:105412 // Notify observers about navigation.
ericwilligersde386342016-10-19 02:35:095413 for (auto& observer : observers_)
5414 observer.AppCacheAccessed(manifest_url, blocked_by_policy);
[email protected]7fc4bbb2011-09-08 21:23:105415}
5416
Gyuyoung Kimc5f4ac12020-05-08 10:59:395417void WebContentsImpl::DomOperationResponse(const std::string& json_string) {
5418 // TODO(lukasza): The notification below should probably indicate which
5419 // RenderFrameHostImpl the message is coming from. This can enable tests to
5420 // talk to 2 frames at the same time, without being confused which frame a
5421 // given response comes from. See also a corresponding TODO in the
5422 // ExecuteScriptHelper in //content/public/test/browser_test_utils.cc
5423 NotificationService::current()->Notify(
5424 NOTIFICATION_DOM_OPERATION_RESPONSE, Source<WebContents>(this),
5425 Details<const std::string>(&json_string));
5426}
5427
Gyuyoung Kim4c50517842020-06-04 17:50:445428void WebContentsImpl::SavableResourceLinksResponse(
5429 RenderFrameHostImpl* source,
5430 const std::vector<GURL>& resources_list,
5431 blink::mojom::ReferrerPtr referrer,
5432 const std::vector<blink::mojom::SavableSubframePtr>& subframes) {
5433 save_package_->SavableResourceLinksResponse(source, resources_list,
5434 std::move(referrer), subframes);
5435}
5436
5437void WebContentsImpl::SavableResourceLinksError(RenderFrameHostImpl* source) {
5438 save_package_->SavableResourceLinksError(source);
5439}
5440
Alexander Timin17edc742020-04-23 18:22:185441void WebContentsImpl::OnServiceWorkerAccessed(
5442 RenderFrameHost* render_frame_host,
5443 const GURL& scope,
5444 AllowServiceWorkerResult allowed) {
5445 for (auto& observer : observers_) {
5446 observer.OnServiceWorkerAccessed(render_frame_host, scope, allowed);
5447 }
5448}
5449
5450void WebContentsImpl::OnServiceWorkerAccessed(
5451 NavigationHandle* navigation,
5452 const GURL& scope,
5453 AllowServiceWorkerResult allowed) {
5454 for (auto& observer : observers_) {
5455 observer.OnServiceWorkerAccessed(navigation, scope, allowed);
5456 }
5457}
5458
Miyoung Shincb6475a2019-11-09 10:49:455459void WebContentsImpl::OnColorChooserFactoryReceiver(
Julie Jeongeun Kim8e2879e2019-08-06 23:55:205460 mojo::PendingReceiver<blink::mojom::ColorChooserFactory> receiver) {
5461 color_chooser_factory_receivers_.Add(this, std::move(receiver));
Joel Hockey85b379d2017-12-11 10:42:135462}
5463
5464void WebContentsImpl::OpenColorChooser(
Julie Jeongeun Kim8e2879e2019-08-06 23:55:205465 mojo::PendingReceiver<blink::mojom::ColorChooser> chooser_receiver,
5466 mojo::PendingRemote<blink::mojom::ColorChooserClient> client,
[email protected]8ed16472014-04-11 19:02:485467 SkColor color,
Joel Hockey163835a2017-12-20 11:51:575468 std::vector<blink::mojom::ColorSuggestionPtr> suggestions) {
Joel Hockey85b379d2017-12-11 10:42:135469 content::ColorChooser* new_color_chooser =
Ivan Kotenkov2c0d2bb32017-11-01 15:41:285470 delegate_ ? delegate_->OpenColorChooser(this, color, suggestions)
5471 : nullptr;
[email protected]820957a2014-01-14 14:56:305472 if (!new_color_chooser)
[email protected]cb805452013-05-22 15:16:215473 return;
[email protected]8ed16472014-04-11 19:02:485474
Joel Hockey85b379d2017-12-11 10:42:135475 color_chooser_.reset();
5476 color_chooser_ = std::make_unique<ColorChooser>(
Julie Jeongeun Kim8e2879e2019-08-06 23:55:205477 new_color_chooser, std::move(chooser_receiver), std::move(client));
[email protected]da8543762012-03-20 08:52:205478}
5479
brettw4b461082016-11-19 18:55:165480#if BUILDFLAG(ENABLE_PLUGINS)
nicka0ac8382016-12-15 23:59:235481void WebContentsImpl::OnPepperInstanceCreated(RenderFrameHostImpl* source,
5482 int32_t pp_instance) {
ericwilligersde386342016-10-19 02:35:095483 for (auto& observer : observers_)
5484 observer.PepperInstanceCreated();
nicka0ac8382016-12-15 23:59:235485 pepper_playback_observer_->PepperInstanceCreated(source, pp_instance);
emaxxe70f5e12015-05-29 11:26:005486}
5487
nicka0ac8382016-12-15 23:59:235488void WebContentsImpl::OnPepperInstanceDeleted(RenderFrameHostImpl* source,
5489 int32_t pp_instance) {
ericwilligersde386342016-10-19 02:35:095490 for (auto& observer : observers_)
5491 observer.PepperInstanceDeleted();
nicka0ac8382016-12-15 23:59:235492 pepper_playback_observer_->PepperInstanceDeleted(source, pp_instance);
emaxxe70f5e12015-05-29 11:26:005493}
5494
nicka0ac8382016-12-15 23:59:235495void WebContentsImpl::OnPepperPluginHung(RenderFrameHostImpl* source,
5496 int plugin_child_id,
tommyclie6633ca72014-10-31 00:40:425497 const base::FilePath& path,
5498 bool is_hung) {
Steven Holte95922222018-09-14 20:06:235499 UMA_HISTOGRAM_COUNTS_1M("Pepper.PluginHung", 1);
tommyclie6633ca72014-10-31 00:40:425500
ericwilligersde386342016-10-19 02:35:095501 for (auto& observer : observers_)
5502 observer.PluginHungStatusChanged(plugin_child_id, path, is_hung);
tommyclie6633ca72014-10-31 00:40:425503}
5504
nicka0ac8382016-12-15 23:59:235505void WebContentsImpl::OnPepperStartsPlayback(RenderFrameHostImpl* source,
5506 int32_t pp_instance) {
5507 pepper_playback_observer_->PepperStartsPlayback(source, pp_instance);
zqzhang181047e62016-07-01 13:37:175508}
5509
nicka0ac8382016-12-15 23:59:235510void WebContentsImpl::OnPepperStopsPlayback(RenderFrameHostImpl* source,
5511 int32_t pp_instance) {
5512 pepper_playback_observer_->PepperStopsPlayback(source, pp_instance);
zqzhang181047e62016-07-01 13:37:175513}
5514
nicka0ac8382016-12-15 23:59:235515void WebContentsImpl::OnPluginCrashed(RenderFrameHostImpl* source,
5516 const base::FilePath& plugin_path,
tommyclie6633ca72014-10-31 00:40:425517 base::ProcessId plugin_pid) {
nicka0ac8382016-12-15 23:59:235518 // TODO(nick): Eliminate the |plugin_pid| parameter, which can't be trusted,
Gyuyoung Kim26c7bc92020-04-29 00:53:005519 // and is only used by WebTestControlHost.
ericwilligersde386342016-10-19 02:35:095520 for (auto& observer : observers_)
5521 observer.PluginCrashed(plugin_path, plugin_pid);
tommyclie6633ca72014-10-31 00:40:425522}
5523
[email protected]d8415ad92012-08-23 14:40:505524void WebContentsImpl::OnRequestPpapiBrokerPermission(
nicka0ac8382016-12-15 23:59:235525 RenderViewHostImpl* source,
5526 int ppb_broker_route_id,
[email protected]d8415ad92012-08-23 14:40:505527 const GURL& url,
[email protected]2dec8ec2013-02-07 19:20:345528 const base::FilePath& plugin_path) {
Avi Drissman0a2a967d2019-04-01 19:33:525529 base::OnceCallback<void(bool)> permission_result_callback = base::BindOnce(
nicka0ac8382016-12-15 23:59:235530 &WebContentsImpl::SendPpapiBrokerPermissionResult, base::Unretained(this),
5531 source->GetProcess()->GetID(), ppb_broker_route_id);
[email protected]8c8fc292012-11-23 18:57:165532 if (!delegate_) {
Tommy Nyquist4b749d02018-03-20 21:46:295533 std::move(permission_result_callback).Run(false);
[email protected]8c8fc292012-11-23 18:57:165534 return;
[email protected]d8415ad92012-08-23 14:40:505535 }
5536
Avi Drissman0a2a967d2019-04-01 19:33:525537 delegate_->RequestPpapiBrokerPermission(
5538 this, url, plugin_path, std::move(permission_result_callback));
[email protected]d8415ad92012-08-23 14:40:505539}
5540
nicka0ac8382016-12-15 23:59:235541void WebContentsImpl::SendPpapiBrokerPermissionResult(int process_id,
5542 int ppb_broker_route_id,
5543 bool result) {
5544 RenderProcessHost* rph = RenderProcessHost::FromID(process_id);
5545 if (rph) {
5546 // TODO(nick): Convert this from ViewMsg_ to a Ppapi msg, since it
5547 // is not routed to a RenderView.
5548 rph->Send(
5549 new ViewMsg_PpapiBrokerPermissionResult(ppb_broker_route_id, result));
5550 }
[email protected]d8415ad92012-08-23 14:40:505551}
brettw4b461082016-11-19 18:55:165552#endif // BUILDFLAG(ENABLE_PLUGINS)
[email protected]19be7a62012-10-01 23:03:375553
Miyoung Shin5709ab02020-02-19 06:50:415554void WebContentsImpl::UpdateFaviconURL(
5555 RenderFrameHost* source,
5556 std::vector<blink::mojom::FaviconURLPtr> candidates) {
lukasza178ff092017-06-07 23:39:505557 // Ignore favicons for non-main frame.
5558 if (source->GetParent()) {
5559 NOTREACHED();
5560 return;
5561 }
5562
pkotwiczf9bc20592015-04-21 18:57:065563 // We get updated favicon URLs after the page stops loading. If a cross-site
5564 // navigation occurs while a page is still loading, the initial page
5565 // may stop loading and send us updated favicon URLs after the navigation
5566 // for the new page has committed.
lukasza178ff092017-06-07 23:39:505567 if (!source->IsCurrent())
pkotwiczf9bc20592015-04-21 18:57:065568 return;
5569
Miyoung Shin5709ab02020-02-19 06:50:415570 favicon_urls_ = std::move(candidates);
Mounir Lamouri7b4a1f232019-12-23 16:32:105571
ericwilligersde386342016-10-19 02:35:095572 for (auto& observer : observers_)
Yuzu Saijo0c263c702020-06-08 03:41:245573 observer.DidUpdateFaviconURL(source, favicon_urls_);
[email protected]795c28972012-12-06 06:13:395574}
5575
qinmin72e8bd02016-10-21 19:35:375576void WebContentsImpl::SetIsOverlayContent(bool is_overlay_content) {
5577 is_overlay_content_ = is_overlay_content;
5578}
5579
Albert J. Wongfb64e142018-10-16 01:10:455580void WebContentsImpl::DidFirstVisuallyNonEmptyPaint(
5581 RenderViewHostImpl* source) {
nicka0ac8382016-12-15 23:59:235582 // TODO(nick): When this is ported to FrameHostMsg_, we should only listen if
5583 // |source| is the main frame.
ericwilligersde386342016-10-19 02:35:095584 for (auto& observer : observers_)
5585 observer.DidFirstVisuallyNonEmptyPaint();
yusufod41c5f92015-03-06 00:14:285586
Carlos Caballero1215f882019-10-29 15:58:435587 if (source->theme_color() != last_sent_theme_color_) {
yusufod41c5f92015-03-06 00:14:285588 // Theme color should have updated by now if there was one.
ericwilligersde386342016-10-19 02:35:095589 for (auto& observer : observers_)
Peter Kastingb14e5782020-01-06 23:55:045590 observer.DidChangeThemeColor();
Carlos Caballero1215f882019-10-29 15:58:435591 last_sent_theme_color_ = source->theme_color();
yusufod41c5f92015-03-06 00:14:285592 }
[email protected]9f268072013-11-07 00:02:155593}
[email protected]d9030b82013-07-19 08:26:065594
Kevin McNee068c3eb2020-04-03 18:24:195595bool WebContentsImpl::IsPortal() {
Adithya Srinivasan46b8a792019-02-01 14:47:235596 return portal();
5597}
5598
Kevin McNee94ea52f52020-06-23 17:42:065599WebContentsImpl* WebContentsImpl::GetPortalHostWebContents() {
5600 return portal() ? portal()->GetPortalHostContents() : nullptr;
5601}
5602
[email protected]17e286e2013-03-01 23:29:395603void WebContentsImpl::NotifyBeforeFormRepostWarningShow() {
ericwilligersde386342016-10-19 02:35:095604 for (auto& observer : observers_)
5605 observer.BeforeFormRepostWarningShow();
[email protected]17e286e2013-03-01 23:29:395606}
5607
[email protected]ec6c05f2013-10-23 18:41:575608void WebContentsImpl::ActivateAndShowRepostFormWarningDialog() {
5609 Activate();
5610 if (delegate_)
5611 delegate_->ShowRepostFormWarningDialog(this);
5612}
5613
[email protected]b4c84012014-04-28 19:51:105614bool WebContentsImpl::HasAccessedInitialDocument() {
5615 return has_accessed_initial_document_;
5616}
5617
afakhry6f0c1ec22016-07-14 13:55:135618void WebContentsImpl::UpdateTitleForEntry(NavigationEntry* entry,
[email protected]fcf75d42013-12-03 20:11:265619 const base::string16& title) {
[email protected]fcf75d42013-12-03 20:11:265620 base::string16 final_title;
Avi Drissman93002212017-09-27 03:20:525621 base::TrimWhitespace(title, base::TRIM_ALL, &final_title);
[email protected]420ae012009-04-24 05:16:325622
[email protected]987fc3a2011-05-26 14:18:095623 // If a page is created via window.open and never navigated,
5624 // there will be no navigation entry. In this situation,
[email protected]73eb2602012-02-09 19:50:555625 // |page_title_when_no_navigation_entry_| will be used for page title.
[email protected]987fc3a2011-05-26 14:18:095626 if (entry) {
[email protected]36fc0392011-12-25 03:59:515627 if (final_title == entry->GetTitle())
afakhry6f0c1ec22016-07-14 13:55:135628 return; // Nothing changed, don't bother.
[email protected]420ae012009-04-24 05:16:325629
[email protected]36fc0392011-12-25 03:59:515630 entry->SetTitle(final_title);
Avi Drissman93002212017-09-27 03:20:525631
5632 // The title for display may differ from the title just set; grab it.
5633 final_title = entry->GetTitleForDisplay();
[email protected]987fc3a2011-05-26 14:18:095634 } else {
5635 if (page_title_when_no_navigation_entry_ == final_title)
afakhry6f0c1ec22016-07-14 13:55:135636 return; // Nothing changed, don't bother.
[email protected]987fc3a2011-05-26 14:18:095637
5638 page_title_when_no_navigation_entry_ = final_title;
5639 }
[email protected]420ae012009-04-24 05:16:325640
[email protected]420ae012009-04-24 05:16:325641 // Lastly, set the title for the view.
[email protected]790e2fd2011-09-21 20:28:235642 view_->SetPageTitle(final_title);
[email protected]420ae012009-04-24 05:16:325643
ericwilligersde386342016-10-19 02:35:095644 for (auto& observer : observers_)
Avi Drissman93002212017-09-27 03:20:525645 observer.TitleWasSet(entry);
[email protected]66798902013-10-01 18:40:165646
afakhry6f0c1ec22016-07-14 13:55:135647 // Broadcast notifications when the UI should be updated.
5648 if (entry == controller_.GetEntryAtOffset(0))
5649 NotifyNavigationStateChanged(INVALIDATE_TYPE_TITLE);
[email protected]420ae012009-04-24 05:16:325650}
5651
fdegansa696e5112015-04-17 01:57:595652void WebContentsImpl::SendChangeLoadProgress() {
[email protected]960b0372014-05-19 18:01:005653 loading_last_progress_update_ = base::TimeTicks::Now();
Evan Stade9f328112019-10-23 20:49:255654 for (auto& observer : observers_)
5655 observer.LoadProgressChanged(frame_tree_.load_progress());
[email protected]960b0372014-05-19 18:01:005656}
5657
5658void WebContentsImpl::ResetLoadProgressState() {
fdegans1d16355162015-03-26 11:58:345659 frame_tree_.ResetLoadProgress();
[email protected]960b0372014-05-19 18:01:005660 loading_weak_factory_.InvalidateWeakPtrs();
5661 loading_last_progress_update_ = base::TimeTicks();
5662}
5663
clamy44e84ce2016-02-22 15:38:255664// Notifies the RenderWidgetHost instance about the fact that the page is
5665// loading, or done loading.
5666void WebContentsImpl::LoadingStateChanged(bool to_different_document,
clamy44e84ce2016-02-22 15:38:255667 LoadNotificationDetails* details) {
Nate Chapin22ea6592019-03-05 22:29:025668 if (is_being_destroyed_)
5669 return;
clamy44e84ce2016-02-22 15:38:255670
5671 bool is_loading = IsLoading();
5672
5673 if (!is_loading) {
5674 load_state_ = net::LoadStateWithParam(net::LOAD_STATE_IDLE,
5675 base::string16());
5676 load_state_host_.clear();
5677 upload_size_ = 0;
5678 upload_position_ = 0;
5679 }
5680
5681 GetRenderManager()->SetIsLoading(is_loading);
5682
5683 waiting_for_response_ = is_loading;
5684 is_load_to_different_document_ = to_different_document;
5685
5686 if (delegate_)
5687 delegate_->LoadingStateChanged(this, to_different_document);
5688 NotifyNavigationStateChanged(INVALIDATE_TYPE_LOAD);
5689
5690 std::string url = (details ? details->url.possibly_invalid_spec() : "NULL");
5691 if (is_loading) {
5692 TRACE_EVENT_ASYNC_BEGIN2("browser,navigation", "WebContentsImpl Loading",
5693 this, "URL", url, "Main FrameTreeNode id",
5694 GetFrameTree()->root()->frame_tree_node_id());
ericwilligersde386342016-10-19 02:35:095695 for (auto& observer : observers_)
5696 observer.DidStartLoading();
clamy44e84ce2016-02-22 15:38:255697 } else {
5698 TRACE_EVENT_ASYNC_END1("browser,navigation", "WebContentsImpl Loading",
5699 this, "URL", url);
ericwilligersde386342016-10-19 02:35:095700 for (auto& observer : observers_)
5701 observer.DidStopLoading();
clamy44e84ce2016-02-22 15:38:255702 }
5703
5704 // TODO(avi): Remove. http://crbug.com/170921
5705 int type = is_loading ? NOTIFICATION_LOAD_START : NOTIFICATION_LOAD_STOP;
5706 NotificationDetails det = NotificationService::NoDetails();
5707 if (details)
5708 det = Details<LoadNotificationDetails>(details);
5709 NotificationService::current()->Notify(
5710 type, Source<NavigationController>(&controller_), det);
5711}
5712
danakja0bce4c952020-05-12 20:40:285713void WebContentsImpl::NotifyViewSwapped(RenderViewHost* old_view,
5714 RenderViewHost* new_view) {
5715 DCHECK_NE(old_view, new_view);
[email protected]420ae012009-04-24 05:16:325716 // After sending out a swap notification, we need to send a disconnect
5717 // notification so that clients that pick up a pointer to |this| can NULL the
5718 // pointer. See Bug 1230284.
5719 notify_disconnection_ = true;
ericwilligersde386342016-10-19 02:35:095720 for (auto& observer : observers_)
danakja0bce4c952020-05-12 20:40:285721 observer.RenderViewHostChanged(old_view, new_view);
[email protected]da7a7182013-09-06 08:11:115722
danakja0bce4c952020-05-12 20:40:285723 view_->RenderViewHostChanged(old_view, new_view);
Jinsuk Kimdc5284c22018-07-11 02:22:065724
Lucas Furukawa Gadanif9ea4fc2018-08-03 23:57:135725 // If this is an inner WebContents that has swapped views, we need to reattach
5726 // it to its outer WebContents.
5727 if (node_.outer_web_contents())
5728 ReattachToOuterWebContentsFrame();
5729
[email protected]7a846df2012-09-20 19:17:395730 // Ensure that the associated embedder gets cleared after a RenderViewHost
5731 // gets swapped, so we don't reuse the same embedder next time a
5732 // RenderViewHost is attached to this WebContents.
5733 RemoveBrowserPluginEmbedder();
[email protected]420ae012009-04-24 05:16:325734}
5735
danakja0bce4c952020-05-12 20:40:285736void WebContentsImpl::NotifyFrameSwapped(RenderFrameHost* old_frame,
5737 RenderFrameHost* new_frame,
Jinsuk Kim895b2c52018-07-03 06:49:165738 bool is_main_frame) {
Bo Liu168c8642017-08-28 18:26:025739#if defined(OS_ANDROID)
danakja0bce4c952020-05-12 20:40:285740 // Copy importance from |old_frame| if |new_frame| is a main frame.
5741 if (old_frame && !new_frame->GetParent()) {
5742 RenderWidgetHostImpl* old_widget =
5743 static_cast<RenderFrameHostImpl*>(old_frame)->GetRenderWidgetHost();
5744 RenderWidgetHostImpl* new_widget =
5745 static_cast<RenderFrameHostImpl*>(new_frame)->GetRenderWidgetHost();
5746 new_widget->SetImportance(old_widget->importance());
Bo Liu7c6779e92017-08-16 02:02:285747 }
Bo Liu168c8642017-08-28 18:26:025748#endif
ericwilligersde386342016-10-19 02:35:095749 for (auto& observer : observers_)
danakja0bce4c952020-05-12 20:40:285750 observer.RenderFrameHostChanged(old_frame, new_frame);
[email protected]02d7b6e2014-06-24 21:01:505751}
5752
[email protected]da7a7182013-09-06 08:11:115753// TODO(avi): Remove this entire function because this notification is already
5754// covered by two observer functions. http://crbug.com/170921
[email protected]b172aee2012-04-10 17:05:265755void WebContentsImpl::NotifyDisconnected() {
[email protected]420ae012009-04-24 05:16:325756 if (!notify_disconnection_)
5757 return;
5758
5759 notify_disconnection_ = false;
[email protected]8ff00d72012-10-23 19:12:215760 NotificationService::current()->Notify(
5761 NOTIFICATION_WEB_CONTENTS_DISCONNECTED,
5762 Source<WebContents>(this),
5763 NotificationService::NoDetails());
[email protected]420ae012009-04-24 05:16:325764}
5765
[email protected]cbb1ef592013-06-05 19:49:465766void WebContentsImpl::NotifyNavigationEntryCommitted(
5767 const LoadCommittedDetails& load_details) {
ericwilligersde386342016-10-19 02:35:095768 for (auto& observer : observers_)
5769 observer.NavigationEntryCommitted(load_details);
[email protected]cbb1ef592013-06-05 19:49:465770}
5771
Sam McNally5c087a32017-08-25 01:46:145772void WebContentsImpl::NotifyNavigationEntryChanged(
5773 const EntryChangedDetails& change_details) {
5774 for (auto& observer : observers_)
5775 observer.NavigationEntryChanged(change_details);
5776}
5777
5778void WebContentsImpl::NotifyNavigationListPruned(
5779 const PrunedDetails& pruned_details) {
5780 for (auto& observer : observers_)
5781 observer.NavigationListPruned(pruned_details);
5782}
5783
Christian Dullweber1af31e62018-02-22 11:49:485784void WebContentsImpl::NotifyNavigationEntriesDeleted() {
5785 for (auto& observer : observers_)
5786 observer.NavigationEntriesDeleted();
5787}
5788
rockotf62002a2016-09-15 00:08:595789void WebContentsImpl::OnAssociatedInterfaceRequest(
5790 RenderFrameHost* render_frame_host,
5791 const std::string& interface_name,
5792 mojo::ScopedInterfaceEndpointHandle handle) {
Julie Jeongeun Kim7d0986172019-12-04 03:17:255793 auto it = receiver_sets_.find(interface_name);
5794 if (it != receiver_sets_.end())
5795 it->second->OnReceiverForFrame(render_frame_host, std::move(handle));
rockotf62002a2016-09-15 00:08:595796}
5797
Ben Goodger9e24929e2017-07-21 03:13:405798void WebContentsImpl::OnInterfaceRequest(
5799 RenderFrameHost* render_frame_host,
5800 const std::string& interface_name,
5801 mojo::ScopedMessagePipeHandle* interface_pipe) {
5802 for (auto& observer : observers_) {
5803 observer.OnInterfaceRequestFromFrame(render_frame_host, interface_name,
5804 interface_pipe);
5805 if (!interface_pipe->is_valid())
5806 break;
5807 }
5808}
5809
Varun Khaneja5da94302019-06-25 04:48:105810void WebContentsImpl::OnDidBlockNavigation(
5811 const GURL& blocked_url,
5812 const GURL& initiator_url,
Abhijeet Kandalkarde7348e2020-01-13 06:06:545813 blink::mojom::NavigationBlockedReason reason) {
Nicolas Dossou-gbetebcab2472017-09-09 12:28:405814 if (delegate_)
Varun Khaneja5da94302019-06-25 04:48:105815 delegate_->OnDidBlockNavigation(this, blocked_url, initiator_url, reason);
Nicolas Dossou-gbetebcab2472017-09-09 12:28:405816}
5817
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:415818const GURL& WebContentsImpl::GetMainFrameLastCommittedURL() {
[email protected]c31a84802014-04-03 15:55:495819 return GetLastCommittedURL();
5820}
5821
[email protected]b849847b2013-12-10 21:57:585822void WebContentsImpl::RenderFrameCreated(RenderFrameHost* render_frame_host) {
ericwilligersde386342016-10-19 02:35:095823 for (auto& observer : observers_)
5824 observer.RenderFrameCreated(render_frame_host);
dmazzonif8a31042016-12-03 00:52:135825 UpdateAccessibilityModeOnFrame(render_frame_host);
clamy91b7abf2016-03-24 15:17:455826
Becca Hughesd11d6502018-07-31 17:01:285827 if (display_cutout_host_impl_)
5828 display_cutout_host_impl_->RenderFrameCreated(render_frame_host);
5829
clamy91b7abf2016-03-24 15:17:455830 if (!render_frame_host->IsRenderFrameLive() || render_frame_host->GetParent())
5831 return;
5832
5833 NavigationEntry* entry = controller_.GetPendingEntry();
5834 if (entry && entry->IsViewSourceMode()) {
5835 // Put the renderer in view source mode.
Dave Tapuska3c05e1c2019-11-27 18:34:185836 static_cast<RenderFrameHostImpl*>(render_frame_host)
5837 ->GetAssociatedLocalFrame()
5838 ->EnableViewSourceMode();
clamy91b7abf2016-03-24 15:17:455839 }
[email protected]b849847b2013-12-10 21:57:585840}
5841
5842void WebContentsImpl::RenderFrameDeleted(RenderFrameHost* render_frame_host) {
yilkal172da772019-11-06 22:49:075843 if (IsBeingDestroyed() && !render_frame_host->GetParent() &&
5844 first_navigation_completed_ &&
Hajime Hoshibbc509c2020-04-06 08:55:085845 !render_frame_host->IsInBackForwardCache()) {
yilkal796e89e2019-10-02 23:58:285846 // Main frame has been deleted because WebContents is being destroyed.
Rakina Zata Amni74627df2019-10-15 02:03:175847 // Note that we aren't recording this here when the main frame is in the
5848 // back-forward cache because that means we've actually already navigated
5849 // away from it (and we got to this point because the WebContents is
yilkal172da772019-11-06 22:49:075850 // deleted), which means |max_loaded_frame_count_| is already overwritten.
5851 // The |max_loaded_frame_count_| value will instead be recorded from within
5852 // |WebContentsImpl::DidFinishNavigation()|.
5853 RecordMaxFrameCountUMA(max_loaded_frame_count_);
yilkal796e89e2019-10-02 23:58:285854 }
5855
creis3638897452017-05-09 22:07:505856 is_notifying_observers_ = true;
ericwilligersde386342016-10-19 02:35:095857 for (auto& observer : observers_)
5858 observer.RenderFrameDeleted(render_frame_host);
creis3638897452017-05-09 22:07:505859 is_notifying_observers_ = false;
zqzhang7b6f4f02016-12-07 22:10:225860#if BUILDFLAG(ENABLE_PLUGINS)
5861 pepper_playback_observer_->RenderFrameDeleted(render_frame_host);
5862#endif
Becca Hughesfd5d8f892018-06-14 18:23:365863
Becca Hughesd11d6502018-07-31 17:01:285864 if (display_cutout_host_impl_)
5865 display_cutout_host_impl_->RenderFrameDeleted(render_frame_host);
5866
Becca Hughesfd5d8f892018-06-14 18:23:365867 // Remove any fullscreen state that the frame has stored.
5868 FullscreenStateChanged(render_frame_host, false /* is_fullscreen */);
[email protected]b849847b2013-12-10 21:57:585869}
5870
[email protected]a09d53ce2014-01-31 00:46:425871void WebContentsImpl::ShowContextMenu(RenderFrameHost* render_frame_host,
5872 const ContextMenuParams& params) {
avie9fd8722017-04-10 16:17:585873 // If a renderer fires off a second command to show a context menu before the
5874 // first context menu is closed, just ignore it. https://crbug.com/707534
ekaramadf6750aa2017-06-06 18:29:425875 if (showing_context_menu_)
avie9fd8722017-04-10 16:17:585876 return;
5877
[email protected]077e704b2014-05-23 19:24:105878 ContextMenuParams context_menu_params(params);
[email protected]a09d53ce2014-01-31 00:46:425879 // Allow WebContentsDelegates to handle the context menu operation first.
Joel Klinghed0ea22ea2019-02-20 22:04:225880 if (delegate_ &&
5881 delegate_->HandleContextMenu(render_frame_host, context_menu_params))
[email protected]a09d53ce2014-01-31 00:46:425882 return;
5883
[email protected]077e704b2014-05-23 19:24:105884 render_view_host_delegate_view_->ShowContextMenu(render_frame_host,
5885 context_menu_params);
[email protected]a09d53ce2014-01-31 00:46:425886}
5887
Avi Drissman4891b852018-09-25 18:12:585888namespace {
5889// Normalizes the line endings: \r\n -> \n, lone \r -> \n.
5890base::string16 NormalizeLineBreaks(const base::string16& source) {
5891 static const base::NoDestructor<base::string16> kReturnNewline(
5892 base::ASCIIToUTF16("\r\n"));
5893 static const base::NoDestructor<base::string16> kReturn(
5894 base::ASCIIToUTF16("\r"));
5895 static const base::NoDestructor<base::string16> kNewline(
5896 base::ASCIIToUTF16("\n"));
5897
5898 std::vector<base::StringPiece16> pieces;
5899
5900 for (const auto& rn_line : base::SplitStringPieceUsingSubstr(
5901 source, *kReturnNewline, base::KEEP_WHITESPACE,
5902 base::SPLIT_WANT_ALL)) {
5903 auto r_lines = base::SplitStringPieceUsingSubstr(
5904 rn_line, *kReturn, base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL);
5905 std::move(std::begin(r_lines), std::end(r_lines),
5906 std::back_inserter(pieces));
5907 }
5908
5909 return base::JoinString(pieces, *kNewline);
5910}
5911} // namespace
5912
Dave Tapuskacdf545cc2020-01-23 18:38:525913void WebContentsImpl::RunJavaScriptDialog(
5914 RenderFrameHost* render_frame_host,
5915 const base::string16& message,
5916 const base::string16& default_prompt,
5917 JavaScriptDialogType dialog_type,
5918 JavaScriptDialogCallback response_callback) {
Avi Drissman2ccbb402018-03-20 21:11:365919 // Ensure that if showing a dialog is the first thing that a page does, that
5920 // the contents of the previous page aren't shown behind it. This is required
5921 // because showing a dialog freezes the renderer, so no frames will be coming
5922 // from it. https://crbug.com/823353
5923 auto* render_widget_host_impl =
5924 static_cast<RenderFrameHostImpl*>(render_frame_host)
5925 ->GetRenderWidgetHost();
5926 if (render_widget_host_impl)
5927 render_widget_host_impl->ForceFirstFrameAfterNavigationTimeout();
5928
avi0720b02e2017-06-13 03:22:135929 // Running a dialog causes an exit to webpage-initiated fullscreen.
5930 // http://crbug.com/728276
Avi Drissman1a55a9d62020-03-10 18:56:455931 base::ScopedClosureRunner fullscreen_block = ForSecurityDropFullscreen();
avi0720b02e2017-06-13 03:22:135932
Avi Drissman1a55a9d62020-03-10 18:56:455933 auto callback =
5934 base::BindOnce(&WebContentsImpl::OnDialogClosed, base::Unretained(this),
5935 render_frame_host->GetProcess()->GetID(),
5936 render_frame_host->GetRoutingID(),
5937 std::move(response_callback), std::move(fullscreen_block));
Pavel Feldman3c1842b2017-08-02 05:00:165938
Pavel Feldman3c1842b2017-08-02 05:00:165939 std::vector<protocol::PageHandler*> page_handlers =
5940 protocol::PageHandler::EnabledForWebContents(this);
5941
Pavel Feldmanfab3cb62017-10-05 01:02:015942 if (delegate_)
5943 dialog_manager_ = delegate_->GetJavaScriptDialogManager(this);
5944
Charlie Reis96077362019-07-04 00:34:045945 // While a JS message dialog is showing, defer commits in this WebContents.
5946 javascript_dialog_navigation_deferrer_ =
5947 std::make_unique<JavaScriptDialogNavigationDeferrer>();
5948
Carlos IL07f282f2020-05-15 22:58:295949 // Suppress JavaScript dialogs when requested.
5950 bool should_suppress = delegate_ && delegate_->ShouldSuppressDialogs(this);
Pavel Feldman1c587bc52018-04-04 21:16:055951 bool has_non_devtools_handlers = delegate_ && dialog_manager_;
5952 bool has_handlers = page_handlers.size() || has_non_devtools_handlers;
Pavel Feldmanfab3cb62017-10-05 01:02:015953 bool suppress_this_message = should_suppress || !has_handlers;
5954
5955 if (suppress_this_message) {
Avi Drissmane04d3992017-10-05 15:11:365956 std::move(callback).Run(true, false, base::string16());
Pavel Feldman3c1842b2017-08-02 05:00:165957 return;
5958 }
5959
5960 scoped_refptr<CloseDialogCallbackWrapper> wrapper =
Avi Drissmane04d3992017-10-05 15:11:365961 new CloseDialogCallbackWrapper(std::move(callback));
Pavel Feldman3c1842b2017-08-02 05:00:165962
5963 is_showing_javascript_dialog_ = true;
5964
Avi Drissman4891b852018-09-25 18:12:585965 base::string16 normalized_message = NormalizeLineBreaks(message);
5966
Pavel Feldman3c1842b2017-08-02 05:00:165967 for (auto* handler : page_handlers) {
Avi Drissmane04d3992017-10-05 15:11:365968 handler->DidRunJavaScriptDialog(
Avi Drissman4891b852018-09-25 18:12:585969 render_frame_host->GetLastCommittedURL(), normalized_message,
5970 default_prompt, dialog_type, has_non_devtools_handlers,
Avi Drissmane04d3992017-10-05 15:11:365971 base::BindOnce(&CloseDialogCallbackWrapper::Run, wrapper, false));
Pavel Feldman3c1842b2017-08-02 05:00:165972 }
5973
5974 if (dialog_manager_) {
[email protected]87de04b02014-04-08 22:14:495975 dialog_manager_->RunJavaScriptDialog(
Avi Drissman4891b852018-09-25 18:12:585976 this, render_frame_host, dialog_type, normalized_message,
5977 default_prompt,
Avi Drissmane04d3992017-10-05 15:11:365978 base::BindOnce(&CloseDialogCallbackWrapper::Run, wrapper, false),
5979 &suppress_this_message);
[email protected]87de04b02014-04-08 22:14:495980 }
5981
5982 if (suppress_this_message) {
5983 // If we are suppressing messages, just reply as if the user immediately
5984 // pressed "Cancel", passing true to |dialog_was_suppressed|.
Avi Drissmane04d3992017-10-05 15:11:365985 wrapper->Run(true, false, base::string16());
[email protected]87de04b02014-04-08 22:14:495986 }
[email protected]87de04b02014-04-08 22:14:495987}
5988
5989void WebContentsImpl::RunBeforeUnloadConfirm(
[email protected]39f2f9252014-05-03 00:54:015990 RenderFrameHost* render_frame_host,
[email protected]87de04b02014-04-08 22:14:495991 bool is_reload,
Dave Tapuskacdf545cc2020-01-23 18:38:525992 JavaScriptDialogCallback response_callback) {
Avi Drissman2ccbb402018-03-20 21:11:365993 // Ensure that if showing a dialog is the first thing that a page does, that
5994 // the contents of the previous page aren't shown behind it. This is required
5995 // because showing a dialog freezes the renderer, so no frames will be coming
5996 // from it. https://crbug.com/823353
5997 auto* render_widget_host_impl =
5998 static_cast<RenderFrameHostImpl*>(render_frame_host)
5999 ->GetRenderWidgetHost();
6000 if (render_widget_host_impl)
6001 render_widget_host_impl->ForceFirstFrameAfterNavigationTimeout();
6002
avi0720b02e2017-06-13 03:22:136003 // Running a dialog causes an exit to webpage-initiated fullscreen.
6004 // http://crbug.com/728276
Avi Drissman1a55a9d62020-03-10 18:56:456005 base::ScopedClosureRunner fullscreen_block = ForSecurityDropFullscreen();
avi0720b02e2017-06-13 03:22:136006
[email protected]39f2f9252014-05-03 00:54:016007 RenderFrameHostImpl* rfhi =
6008 static_cast<RenderFrameHostImpl*>(render_frame_host);
[email protected]87de04b02014-04-08 22:14:496009 if (delegate_)
6010 delegate_->WillRunBeforeUnloadConfirm();
6011
Avi Drissman1a55a9d62020-03-10 18:56:456012 auto callback =
6013 base::BindOnce(&WebContentsImpl::OnDialogClosed, base::Unretained(this),
6014 render_frame_host->GetProcess()->GetID(),
6015 render_frame_host->GetRoutingID(),
6016 std::move(response_callback), std::move(fullscreen_block));
Pavel Feldman3c1842b2017-08-02 05:00:166017
Pavel Feldman3c1842b2017-08-02 05:00:166018 std::vector<protocol::PageHandler*> page_handlers =
6019 protocol::PageHandler::EnabledForWebContents(this);
6020
Pavel Feldmanfab3cb62017-10-05 01:02:016021 if (delegate_)
6022 dialog_manager_ = delegate_->GetJavaScriptDialogManager(this);
6023
Charlie Reis96077362019-07-04 00:34:046024 // While a JS beforeunload dialog is showing, defer commits in this
6025 // WebContents.
6026 javascript_dialog_navigation_deferrer_ =
6027 std::make_unique<JavaScriptDialogNavigationDeferrer>();
6028
Sreeja Kamishettyf352e302020-06-10 13:21:246029 bool should_suppress = !rfhi->IsCurrent() ||
Tarun Bansalc209b742019-12-24 06:53:256030 (delegate_ && delegate_->ShouldSuppressDialogs(this));
Pavel Feldman1c587bc52018-04-04 21:16:056031 bool has_non_devtools_handlers = delegate_ && dialog_manager_;
6032 bool has_handlers = page_handlers.size() || has_non_devtools_handlers;
Pavel Feldmanfab3cb62017-10-05 01:02:016033 if (should_suppress || !has_handlers) {
Avi Drissmane04d3992017-10-05 15:11:366034 std::move(callback).Run(false, true, base::string16());
[email protected]87de04b02014-04-08 22:14:496035 return;
6036 }
6037
6038 is_showing_before_unload_dialog_ = true;
Pavel Feldman3c1842b2017-08-02 05:00:166039
6040 scoped_refptr<CloseDialogCallbackWrapper> wrapper =
Avi Drissmane04d3992017-10-05 15:11:366041 new CloseDialogCallbackWrapper(std::move(callback));
Pavel Feldman3c1842b2017-08-02 05:00:166042
6043 GURL frame_url = rfhi->GetLastCommittedURL();
6044 for (auto* handler : page_handlers) {
Avi Drissmane04d3992017-10-05 15:11:366045 handler->DidRunBeforeUnloadConfirm(
Pavel Feldman1c587bc52018-04-04 21:16:056046 frame_url, has_non_devtools_handlers,
Avi Drissmane04d3992017-10-05 15:11:366047 base::BindOnce(&CloseDialogCallbackWrapper::Run, wrapper, false));
Pavel Feldman3c1842b2017-08-02 05:00:166048 }
6049
6050 if (dialog_manager_) {
Avi Drissmane04d3992017-10-05 15:11:366051 dialog_manager_->RunBeforeUnloadDialog(
Avi Drissmanff9ed752017-11-28 21:18:126052 this, render_frame_host, is_reload,
Avi Drissmane04d3992017-10-05 15:11:366053 base::BindOnce(&CloseDialogCallbackWrapper::Run, wrapper, false));
Pavel Feldman3c1842b2017-08-02 05:00:166054 }
[email protected]87de04b02014-04-08 22:14:496055}
6056
Kent Tamurac260c9a2018-09-14 09:10:566057void WebContentsImpl::RunFileChooser(
6058 RenderFrameHost* render_frame_host,
Kent Tamura3abb32d2020-07-02 00:23:016059 scoped_refptr<FileChooserImpl::FileSelectListenerImpl> listener,
Kent Tamurac260c9a2018-09-14 09:10:566060 const blink::mojom::FileChooserParams& params) {
Avi Drissman3d41e7712018-08-27 21:18:086061 // Any explicit focusing of another window while this WebContents is in
6062 // fullscreen can be used to confuse the user, so drop fullscreen.
Avi Drissman1a55a9d62020-03-10 18:56:456063 base::ScopedClosureRunner fullscreen_block = ForSecurityDropFullscreen();
6064 listener->SetFullscreenBlock(std::move(fullscreen_block));
Avi Drissman3d41e7712018-08-27 21:18:086065
naskoada75b22016-06-11 16:09:466066 if (delegate_)
Kent Tamura512a27e2018-10-04 00:49:326067 delegate_->RunFileChooser(render_frame_host, std::move(listener), params);
6068 else
6069 listener->FileSelectionCanceled();
naskoada75b22016-06-11 16:09:466070}
6071
[email protected]a86c0e962013-12-17 17:10:396072WebContents* WebContentsImpl::GetAsWebContents() {
6073 return this;
6074}
6075
mcnee336ea2c2017-05-23 22:50:596076#if !defined(OS_ANDROID)
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:416077double WebContentsImpl::GetPendingPageZoomLevel() {
wjmaclean64951902016-04-29 20:59:126078 NavigationEntry* pending_entry = GetController().GetPendingEntry();
6079 if (!pending_entry)
6080 return HostZoomMap::GetZoomLevel(this);
6081
6082 GURL url = pending_entry->GetURL();
6083 return HostZoomMap::GetForWebContents(this)->GetZoomLevelForHostAndScheme(
6084 url.scheme(), net::GetHostOrSpecFromURL(url));
6085}
mcnee336ea2c2017-05-23 22:50:596086#endif // !defined(OS_ANDROID)
wjmaclean64951902016-04-29 20:59:126087
qinmin72e8bd02016-10-21 19:35:376088bool WebContentsImpl::HideDownloadUI() const {
6089 return is_overlay_content_;
6090}
6091
mlamouri5cd9ae82017-02-18 11:05:096092bool WebContentsImpl::HasPersistentVideo() const {
6093 return has_persistent_video_;
6094}
6095
Ian Vollick5d5c37f2019-06-19 22:50:576096bool WebContentsImpl::IsSpatialNavigationDisabled() const {
6097 return is_spatial_navigation_disabled_;
6098}
6099
Albert J. Wong65fe64d2019-09-20 02:48:146100RenderFrameHostImpl* WebContentsImpl::GetPendingMainFrame() {
clamy610c63b32017-12-22 15:05:186101 return GetRenderManager()->speculative_frame_host();
Alex Moshchuk27caae82017-09-11 23:11:186102}
6103
mlamourie52c1b542017-02-23 14:57:506104bool WebContentsImpl::HasActiveEffectivelyFullscreenVideo() const {
6105 return media_web_contents_observer_->HasActiveEffectivelyFullscreenVideo();
6106}
6107
François Beaufortad6c5232018-02-26 11:00:446108bool WebContentsImpl::IsPictureInPictureAllowedForFullscreenVideo() const {
6109 return media_web_contents_observer_
6110 ->IsPictureInPictureAllowedForFullscreenVideo();
6111}
6112
ekaramada110f642016-12-21 19:47:286113bool WebContentsImpl::IsFocusedElementEditable() {
6114 RenderFrameHostImpl* frame = GetFocusedFrame();
6115 return frame && frame->has_focused_editable_element();
6116}
6117
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:416118bool WebContentsImpl::IsShowingContextMenu() {
ekaramadf6750aa2017-06-06 18:29:426119 return showing_context_menu_;
6120}
6121
6122void WebContentsImpl::SetShowingContextMenu(bool showing) {
6123 DCHECK_NE(showing_context_menu_, showing);
6124 showing_context_menu_ = showing;
6125
6126 if (auto* view = GetRenderWidgetHostView()) {
6127 // Notify the main frame's RWHV to run the platform-specific code, if any.
6128 static_cast<RenderWidgetHostViewBase*>(view)->SetShowingContextMenu(
6129 showing);
6130 }
6131}
6132
ekaramada110f642016-12-21 19:47:286133void WebContentsImpl::ClearFocusedElement() {
6134 if (auto* frame = GetFocusedFrame())
6135 frame->ClearFocusedElement();
6136}
6137
danakj77eb7e82020-01-09 19:38:466138bool WebContentsImpl::IsNeverComposited() {
[email protected]7912e822014-04-16 02:37:036139 if (!delegate_)
6140 return false;
danakj77eb7e82020-01-09 19:38:466141 return delegate_->IsNeverComposited(this);
[email protected]7912e822014-04-16 02:37:036142}
6143
[email protected]5a3bdf52012-05-24 15:12:576144RenderViewHostDelegateView* WebContentsImpl::GetDelegateView() {
6145 return render_view_host_delegate_view_;
[email protected]420ae012009-04-24 05:16:326146}
6147
Leon Hanc819dc62019-01-28 04:30:196148blink::mojom::RendererPreferences WebContentsImpl::GetRendererPrefs(
[email protected]8ff00d72012-10-23 19:12:216149 BrowserContext* browser_context) const {
[email protected]840b1512009-07-21 16:53:466150 return renderer_preferences_;
[email protected]80d96fa2009-06-10 22:34:516151}
6152
Lucas Furukawa Gadani2ec00c82018-12-14 15:53:166153RenderFrameHostImpl* WebContentsImpl::GetOuterWebContentsFrame() {
6154 if (GetOuterDelegateFrameTreeNodeId() ==
6155 FrameTreeNode::kFrameTreeNodeInvalidId) {
6156 return nullptr;
6157 }
6158
6159 FrameTreeNode* outer_node =
6160 FrameTreeNode::GloballyFindByID(GetOuterDelegateFrameTreeNodeId());
6161 // The outer node should be in the outer WebContents.
6162 DCHECK_EQ(outer_node->frame_tree(), GetOuterWebContents()->GetFrameTree());
Alexander Timin381e7e182020-04-28 19:04:036163 return outer_node->parent();
Lucas Furukawa Gadani2ec00c82018-12-14 15:53:166164}
6165
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:416166WebContentsImpl* WebContentsImpl::GetOuterWebContents() {
Lucas Furukawa Gadani105de1e812018-05-31 19:38:396167 return node_.outer_web_contents();
avallee0206f782016-07-28 18:55:336168}
6169
Lucas Furukawa Gadani2ec00c82018-12-14 15:53:166170std::vector<WebContents*> WebContentsImpl::GetInnerWebContents() {
6171 std::vector<WebContents*> all_inner_contents;
Lucas Furukawa Gadani2ec00c82018-12-14 15:53:166172 const auto& inner_contents = node_.GetInnerWebContents();
6173 all_inner_contents.insert(all_inner_contents.end(), inner_contents.begin(),
6174 inner_contents.end());
6175 return all_inner_contents;
6176}
6177
Jeremy Roman7e70bf92020-01-07 23:23:586178WebContentsImpl* WebContentsImpl::GetResponsibleWebContents() {
6179 // Iteratively ask delegates which other contents is responsible until a fixed
6180 // point is found.
6181 WebContentsImpl* contents = this;
6182 while (WebContentsDelegate* delegate = contents->GetDelegate()) {
6183 auto* responsible_contents = static_cast<WebContentsImpl*>(
6184 delegate->GetResponsibleWebContents(contents));
6185 if (responsible_contents == contents)
6186 break;
6187 contents = responsible_contents;
6188 }
6189 return contents;
6190}
6191
avallee0206f782016-07-28 18:55:336192WebContentsImpl* WebContentsImpl::GetFocusedWebContents() {
lfg601233692017-03-06 22:45:446193 return GetOutermostWebContents()->node_.focused_web_contents();
avallee0206f782016-07-28 18:55:336194}
6195
avallee9993fca2016-11-17 06:16:506196bool WebContentsImpl::ContainsOrIsFocusedWebContents() {
6197 for (WebContentsImpl* focused_contents = GetFocusedWebContents();
6198 focused_contents;
6199 focused_contents = focused_contents->GetOuterWebContents()) {
6200 if (focused_contents == this)
6201 return true;
6202 }
6203
6204 return false;
6205}
6206
W. James MacLean9437b5bf2019-12-06 17:33:446207void WebContentsImpl::RemoveBrowserPluginEmbedder() {
6208 browser_plugin_embedder_.reset();
6209}
6210
avallee0206f782016-07-28 18:55:336211WebContentsImpl* WebContentsImpl::GetOutermostWebContents() {
6212 WebContentsImpl* root = this;
6213 while (root->GetOuterWebContents())
6214 root = root->GetOuterWebContents();
6215 return root;
6216}
6217
avalleed68cdd12017-06-21 22:07:276218void WebContentsImpl::FocusOuterAttachmentFrameChain() {
6219 WebContentsImpl* outer_contents = GetOuterWebContents();
6220 if (!outer_contents)
6221 return;
6222
6223 FrameTreeNode* outer_node =
6224 FrameTreeNode::GloballyFindByID(GetOuterDelegateFrameTreeNodeId());
6225 outer_contents->frame_tree_.SetFocusedFrame(outer_node, nullptr);
6226
6227 // For a browser initiated focus change, let embedding renderer know of the
6228 // change. Otherwise, if the currently focused element is just across a
6229 // process boundary in focus order, it will not be possible to move across
6230 // that boundary. This is because the target element will already be focused
6231 // (that renderer was not notified) and drop the event.
6232 if (GetRenderManager()->GetProxyToOuterDelegate())
6233 GetRenderManager()->GetProxyToOuterDelegate()->SetFocusedFrame();
6234
6235 outer_contents->FocusOuterAttachmentFrameChain();
6236}
6237
Sean Toppingbbc4a2bd2019-01-30 02:16:146238void WebContentsImpl::InnerWebContentsCreated(WebContents* inner_web_contents) {
6239 for (auto& observer : observers_)
6240 observer.InnerWebContentsCreated(inner_web_contents);
6241}
6242
Jeremy Roman03ce13ce2020-05-29 22:16:576243void WebContentsImpl::InnerWebContentsAttached(
6244 WebContents* inner_web_contents) {
6245 if (inner_web_contents->IsCurrentlyAudible())
6246 OnAudioStateChanged();
6247}
6248
6249void WebContentsImpl::InnerWebContentsDetached(
6250 WebContents* inner_web_contents) {
6251 if (!is_being_destroyed_)
6252 OnAudioStateChanged();
6253}
6254
[email protected]b172aee2012-04-10 17:05:266255void WebContentsImpl::RenderViewCreated(RenderViewHost* render_view_host) {
[email protected]86f98a22013-03-20 14:35:006256 if (delegate_)
[email protected]e85165c642014-06-10 14:34:316257 view_->SetOverscrollControllerEnabled(CanOverscrollContent());
[email protected]a6b73c62013-02-11 23:05:086258
[email protected]8ff00d72012-10-23 19:12:216259 NotificationService::current()->Notify(
6260 NOTIFICATION_WEB_CONTENTS_RENDER_VIEW_HOST_CREATED,
6261 Source<WebContents>(this),
6262 Details<RenderViewHost>(render_view_host));
[email protected]420ae012009-04-24 05:16:326263
ericwilligersde386342016-10-19 02:35:096264 for (auto& observer : observers_)
6265 observer.RenderViewCreated(render_view_host);
Joel Einbinder848001c2017-09-19 21:39:316266 if (delegate_)
6267 RenderFrameDevToolsAgentHost::WebContentsCreated(this);
[email protected]420ae012009-04-24 05:16:326268}
6269
[email protected]b172aee2012-04-10 17:05:266270void WebContentsImpl::RenderViewReady(RenderViewHost* rvh) {
[email protected]151a63d2011-12-20 22:32:526271 if (rvh != GetRenderViewHost()) {
[email protected]420ae012009-04-24 05:16:326272 // Don't notify the world, since this came from a renderer in the
6273 // background.
6274 return;
6275 }
6276
dmazzoni62111452017-04-28 23:46:556277 RenderWidgetHostViewBase* rwhv =
6278 static_cast<RenderWidgetHostViewBase*>(GetRenderWidgetHostView());
6279 if (rwhv)
6280 rwhv->SetMainFrameAXTreeID(GetMainFrame()->GetAXTreeID());
6281
[email protected]da7a7182013-09-06 08:11:116282 notify_disconnection_ = true;
[email protected]da7a7182013-09-06 08:11:116283
[email protected]be1f56ab2011-12-22 06:55:316284 bool was_crashed = IsCrashed();
[email protected]443b80e2010-12-14 00:42:236285 SetIsCrashed(base::TERMINATION_STATUS_STILL_RUNNING, 0);
[email protected]9d8a4642009-07-29 17:25:306286
6287 // Restore the focus to the tab (otherwise the focus will be on the top
6288 // window).
[email protected]484ae5912010-09-29 19:16:146289 if (was_crashed && !FocusLocationBarByDefault() &&
6290 (!delegate_ || delegate_->ShouldFocusPageAfterCrash())) {
[email protected]f3615f02013-02-26 06:09:066291 view_->Focus();
[email protected]484ae5912010-09-29 19:16:146292 }
[email protected]32ded2212011-11-10 18:51:436293
ericwilligersde386342016-10-19 02:35:096294 for (auto& observer : observers_)
6295 observer.RenderViewReady();
Jinsuk Kim895b2c52018-07-03 06:49:166296
6297 view_->RenderViewReady();
[email protected]420ae012009-04-24 05:16:326298}
6299
[email protected]ec6a7eb2013-04-22 17:34:226300void WebContentsImpl::RenderViewTerminated(RenderViewHost* rvh,
6301 base::TerminationStatus status,
6302 int error_code) {
[email protected]151a63d2011-12-20 22:32:526303 if (rvh != GetRenderViewHost()) {
[email protected]420ae012009-04-24 05:16:326304 // The pending page's RenderViewHost is gone.
6305 return;
6306 }
6307
[email protected]2fcdcc32014-03-05 02:14:076308 // Ensure fullscreen mode is exited in the |delegate_| since a crashed
6309 // renderer may not have made a clean exit.
Avi Drissman97aef042020-06-30 21:04:486310 if (IsFullscreen())
bokanece34a82016-01-28 19:49:466311 ExitFullscreenMode(false);
[email protected]2fcdcc32014-03-05 02:14:076312
Jennifer Apacibleb4b972a2018-06-07 04:41:026313 // Ensure any video in Picture-in-Picture is exited in the |delegate_| since
6314 // a crashed renderer may not have made a clean exit.
6315 if (HasPictureInPictureVideo())
6316 ExitPictureInPicture();
6317
[email protected]698191dc2014-02-25 01:06:136318 // Cancel any visible dialogs so they are not left dangling over the sad tab.
creis89a0f782015-05-27 16:13:176319 CancelActiveAndPendingDialogs();
[email protected]698191dc2014-02-25 01:06:136320
dalecurtis3d1e1302017-02-20 22:33:256321 audio_stream_monitor_.RenderProcessGone(rvh->GetProcess()->GetID());
6322
[email protected]960b0372014-05-19 18:01:006323 // Reset the loading progress. TODO(avi): What does it mean to have a
6324 // "renderer crash" when there is more than one renderer process serving a
6325 // webpage? Once this function is called at a more granular frame level, we
6326 // probably will need to more granularly reset the state here.
6327 ResetLoadProgressState();
clamy44e84ce2016-02-22 15:38:256328 NotifyDisconnected();
6329 SetIsCrashed(status, error_code);
[email protected]960b0372014-05-19 18:01:006330
ericwilligersde386342016-10-19 02:35:096331 for (auto& observer : observers_)
6332 observer.RenderProcessGone(GetCrashedStatus());
[email protected]420ae012009-04-24 05:16:326333}
6334
[email protected]b172aee2012-04-10 17:05:266335void WebContentsImpl::RenderViewDeleted(RenderViewHost* rvh) {
ericwilligersde386342016-10-19 02:35:096336 for (auto& observer : observers_)
6337 observer.RenderViewDeleted(rvh);
[email protected]2e4633c2009-07-09 16:58:066338}
6339
sky8b00392d2015-01-10 00:30:286340void WebContentsImpl::UpdateTargetURL(RenderViewHost* render_view_host,
6341 const GURL& url) {
6342 if (fullscreen_widget_routing_id_ != MSG_ROUTING_NONE) {
Lukasz Anforowiczfd707142018-02-07 19:46:136343 // If we're in flash fullscreen (i.e. Pepper plugin fullscreen) only update
6344 // the url if it's from the fullscreen renderer.
sky8b00392d2015-01-10 00:30:286345 RenderWidgetHostView* fs = GetFullscreenRenderWidgetHostView();
avif9ab5d942015-10-15 14:05:446346 if (fs && fs->GetRenderWidgetHost() != render_view_host->GetWidget())
sky8b00392d2015-01-10 00:30:286347 return;
6348 }
Lukasz Anforowiczfd707142018-02-07 19:46:136349
6350 // In case of racey updates from multiple RenderViewHosts, the last URL should
6351 // be shown - see also some discussion in https://crbug.com/807776.
6352 if (!url.is_valid() && render_view_host != view_that_set_last_target_url_)
6353 return;
6354 view_that_set_last_target_url_ = url.is_valid() ? render_view_host : nullptr;
6355
[email protected]6934a702011-12-20 00:04:516356 if (delegate_)
avi450ce00e2014-09-24 01:58:526357 delegate_->UpdateTargetURL(this, url);
[email protected]420ae012009-04-24 05:16:326358}
6359
Lukasz Anforowiczfd707142018-02-07 19:46:136360void WebContentsImpl::ClearTargetURL() {
6361 view_that_set_last_target_url_ = nullptr;
6362 if (delegate_)
6363 delegate_->UpdateTargetURL(this, GURL());
6364}
6365
[email protected]b172aee2012-04-10 17:05:266366void WebContentsImpl::Close(RenderViewHost* rvh) {
[email protected]60780f412013-02-25 16:34:106367#if defined(OS_MACOSX)
[email protected]07707302009-11-06 00:50:296368 // The UI may be in an event-tracking loop, such as between the
6369 // mouse-down and mouse-up in text selection or a button click.
6370 // Defer the close until after tracking is complete, so that we
6371 // don't free objects out from under the UI.
[email protected]07707302009-11-06 00:50:296372 // TODO(shess): This could get more fine-grained. For instance,
6373 // closing a tab in another window while selecting text in the
6374 // current window's Omnibox should be just fine.
Christopher Cameron9ae31e0d2019-03-01 20:01:196375 if (view_->CloseTabAfterEventTrackingIfNeeded())
[email protected]07707302009-11-06 00:50:296376 return;
[email protected]60780f412013-02-25 16:34:106377#endif
[email protected]07707302009-11-06 00:50:296378
[email protected]420ae012009-04-24 05:16:326379 // Ignore this if it comes from a RenderViewHost that we aren't showing.
[email protected]151a63d2011-12-20 22:32:526380 if (delegate_ && rvh == GetRenderViewHost())
[email protected]6934a702011-12-20 00:04:516381 delegate_->CloseContents(this);
[email protected]420ae012009-04-24 05:16:326382}
6383
Avi Drissman014dec72018-06-07 02:34:016384void WebContentsImpl::RequestSetBounds(const gfx::Rect& new_bounds) {
6385 if (delegate_)
6386 delegate_->SetContentsBounds(this, new_bounds);
[email protected]420ae012009-04-24 05:16:326387}
6388
fdegansa696e5112015-04-17 01:57:596389void WebContentsImpl::DidStartLoading(FrameTreeNode* frame_tree_node,
[email protected]e3b10d12014-03-28 16:06:096390 bool to_different_document) {
Evan Stadec3b26022017-07-14 19:50:246391 LoadingStateChanged(frame_tree_node->IsMainFrame() && to_different_document,
Carlos IL07f282f2020-05-15 22:58:296392 nullptr);
dmazzonibf8cec42015-02-08 08:28:086393
Xiyuan Xia2a66b112018-12-06 15:52:166394 // Reset the focus state from DidStartNavigation to false if a new load starts
6395 // afterward, in case loading logic triggers a FocusLocationBarByDefault call.
6396 should_focus_location_bar_by_default_ = false;
Ian Prestcdd843d2020-01-28 18:59:586397
6398 // Notify accessibility that the user is navigating away from the
6399 // current document.
6400 // TODO(domfarolino, dmazzoni): Do this using WebContentsObserver. See
6401 // https://crbug.com/981271.
6402 BrowserAccessibilityManager* manager =
6403 frame_tree_node->current_frame_host()->browser_accessibility_manager();
6404 if (manager)
6405 manager->UserIsNavigatingAway();
[email protected]420ae012009-04-24 05:16:326406}
6407
fdegansc73eec22015-03-19 14:47:546408void WebContentsImpl::DidStopLoading() {
dcheng59716272016-04-09 05:19:086409 std::unique_ptr<LoadNotificationDetails> details;
[email protected]420ae012009-04-24 05:16:326410
[email protected]9595fd82013-04-19 21:28:496411 // Use the last committed entry rather than the active one, in case a
6412 // pending entry has been created.
6413 NavigationEntry* entry = controller_.GetLastCommittedEntry();
6414
[email protected]420ae012009-04-24 05:16:326415 // An entry may not exist for a stop when loading an initial blank page or
6416 // if an iframe injected by script into a blank page finishes loading.
6417 if (entry) {
[email protected]420ae012009-04-24 05:16:326418 details.reset(new LoadNotificationDetails(
[email protected]36fc0392011-12-25 03:59:516419 entry->GetVirtualURL(),
[email protected]420ae012009-04-24 05:16:326420 &controller_,
6421 controller_.GetCurrentEntryIndex()));
6422 }
6423
Carlos IL07f282f2020-05-15 22:58:296424 LoadingStateChanged(true, details.get());
[email protected]420ae012009-04-24 05:16:326425}
6426
fdegansa696e5112015-04-17 01:57:596427void WebContentsImpl::DidChangeLoadProgress() {
Nate Chapin22ea6592019-03-05 22:29:026428 if (is_being_destroyed_)
6429 return;
fdegansa696e5112015-04-17 01:57:596430 double load_progress = frame_tree_.load_progress();
6431
6432 // The delegate is notified immediately for the first and last updates. Also,
6433 // since the message loop may be pretty busy when a page is loaded, it might
6434 // not execute a posted task in a timely manner so the progress report is sent
6435 // immediately if enough time has passed.
6436 base::TimeDelta min_delay =
6437 base::TimeDelta::FromMilliseconds(kMinimumDelayBetweenLoadingUpdatesMS);
6438 bool delay_elapsed = loading_last_progress_update_.is_null() ||
6439 base::TimeTicks::Now() - loading_last_progress_update_ > min_delay;
6440
6441 if (load_progress == 0.0 || load_progress == 1.0 || delay_elapsed) {
6442 // If there is a pending task to send progress, it is now obsolete.
6443 loading_weak_factory_.InvalidateWeakPtrs();
6444
6445 // Notify the load progress change.
6446 SendChangeLoadProgress();
6447
6448 // Clean-up the states if needed.
6449 if (load_progress == 1.0)
6450 ResetLoadProgressState();
6451 return;
6452 }
6453
6454 if (loading_weak_factory_.HasWeakPtrs())
6455 return;
6456
skyostil95082a62015-06-05 19:53:076457 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
tzike2aca992017-09-05 08:50:546458 FROM_HERE,
6459 base::BindOnce(&WebContentsImpl::SendChangeLoadProgress,
6460 loading_weak_factory_.GetWeakPtr()),
fdegansa696e5112015-04-17 01:57:596461 min_delay);
6462}
6463
avid6d88b912017-01-13 00:16:006464std::vector<std::unique_ptr<NavigationThrottle>>
6465WebContentsImpl::CreateThrottlesForNavigation(
clamycbf524d42016-09-27 12:48:356466 NavigationHandle* navigation_handle) {
Charlie Reis96077362019-07-04 00:34:046467 auto throttles = GetContentClient()->browser()->CreateThrottlesForNavigation(
clamycbf524d42016-09-27 12:48:356468 navigation_handle);
Charlie Reis96077362019-07-04 00:34:046469
6470 // This is not a normal place to be adding a throttle. However, in the case
6471 // javascript dialogs, related logic is present in the web_contents/ layer,
6472 // and the purpose of the throttle is to ensure that navigation commits are
6473 // deferred for the entire WebContents. Most throttles are either added by
6474 // the embederrer outside of content/, or are per-frame and added by
6475 // NavigationThrottleRunner.
6476 std::unique_ptr<content::NavigationThrottle> dialog_throttle =
6477 JavaScriptDialogNavigationThrottle::MaybeCreateThrottleFor(
6478 navigation_handle);
6479 if (dialog_throttle)
6480 throttles.push_back(std::move(dialog_throttle));
6481
6482 return throttles;
clamycbf524d42016-09-27 12:48:356483}
6484
clamy1e5574e2016-09-29 16:48:446485std::unique_ptr<NavigationUIData> WebContentsImpl::GetNavigationUIData(
6486 NavigationHandle* navigation_handle) {
clamy1e5574e2016-09-29 16:48:446487 return GetContentClient()->browser()->GetNavigationUIData(navigation_handle);
6488}
6489
W. James MacLean1c40862c2020-04-27 21:05:576490void WebContentsImpl::RegisterExistingOriginToPreventOptInIsolation(
W. James MacLeanb70fab82020-05-01 18:51:146491 const url::Origin& origin,
6492 NavigationRequest* navigation_request_to_exclude) {
W. James MacLean1c40862c2020-04-27 21:05:576493 // Note: This function can be made static if we ever need call it without
6494 // a WebContentsImpl instance, in which case we can use a wrapper to
6495 // implement the override from NavigatorDelegate.
6496 for (WebContentsImpl* web_contents : GetAllWebContents()) {
6497 web_contents->controller_.RegisterExistingOriginToPreventOptInIsolation(
6498 origin);
W. James MacLeanb70fab82020-05-01 18:51:146499 // Walk the frame tree to pick up any frames without FrameNavigationEntries.
6500 // * Some frames won't have FrameNavigationEntries (Issues 524208, 608402).
6501 // * Some pending navigations won't have NavigationEntries.
6502 web_contents->GetFrameTree()->RegisterExistingOriginToPreventOptInIsolation(
6503 origin, navigation_request_to_exclude);
W. James MacLean1c40862c2020-04-27 21:05:576504 }
6505}
6506
pdr15f5b5b2017-06-20 00:58:006507void WebContentsImpl::DidCancelLoading() {
6508 controller_.DiscardNonCommittedEntries();
6509
6510 // Update the URL display.
6511 NotifyNavigationStateChanged(INVALIDATE_TYPE_URL);
6512}
6513
[email protected]59167c22013-06-03 18:07:326514void WebContentsImpl::DidAccessInitialDocument() {
[email protected]b4c84012014-04-28 19:51:106515 has_accessed_initial_document_ = true;
6516
[email protected]d1371bf8e2014-04-19 03:26:246517 // We may have left a failed browser-initiated navigation in the address bar
6518 // to let the user edit it and try again. Clear it now that content might
6519 // show up underneath it.
6520 if (!IsLoading() && controller_.GetPendingEntry())
avi45a72532015-04-07 21:01:456521 controller_.DiscardPendingEntry(false);
[email protected]d1371bf8e2014-04-19 03:26:246522
[email protected]59167c22013-06-03 18:07:326523 // Update the URL display.
thestigc4cac8f2014-09-04 21:17:506524 NotifyNavigationStateChanged(INVALIDATE_TYPE_URL);
[email protected]59167c22013-06-03 18:07:326525}
6526
alexmosbe2f4c32015-03-10 02:30:236527void WebContentsImpl::DidChangeName(RenderFrameHost* render_frame_host,
6528 const std::string& name) {
ericwilligersde386342016-10-19 02:35:096529 for (auto& observer : observers_)
6530 observer.FrameNameChanged(render_frame_host, name);
alexmosbe2f4c32015-03-10 02:30:236531}
6532
John Delaneyedd8d6c2019-01-25 00:23:576533void WebContentsImpl::DidReceiveFirstUserActivation(
6534 RenderFrameHost* render_frame_host) {
6535 for (auto& observer : observers_)
6536 observer.FrameReceivedFirstUserActivation(render_frame_host);
6537}
6538
John Delaney0146a742019-01-25 19:25:286539void WebContentsImpl::DidChangeDisplayState(RenderFrameHost* render_frame_host,
6540 bool is_display_none) {
6541 for (auto& observer : observers_)
6542 observer.FrameDisplayStateChanged(render_frame_host, is_display_none);
6543}
6544
6545void WebContentsImpl::FrameSizeChanged(RenderFrameHost* render_frame_host,
6546 const gfx::Size& frame_size) {
6547 for (auto& observer : observers_)
6548 observer.FrameSizeChanged(render_frame_host, frame_size);
6549}
6550
[email protected]39f2f9252014-05-03 00:54:016551void WebContentsImpl::DocumentOnLoadCompleted(
[email protected]50279492014-05-05 23:24:296552 RenderFrameHost* render_frame_host) {
falken52a56e32016-12-08 05:02:406553 ShowInsecureLocalhostWarningIfNeeded();
6554
Alexander Timindd0fa0c52019-11-06 13:03:376555 GetRenderViewHost()->DocumentOnLoadCompletedInMainFrame();
6556
creis97867aa2017-02-17 18:39:456557 is_notifying_observers_ = true;
ericwilligersde386342016-10-19 02:35:096558 for (auto& observer : observers_)
6559 observer.DocumentOnLoadCompletedInMainFrame();
creis97867aa2017-02-17 18:39:456560 is_notifying_observers_ = false;
[email protected]6578fef2013-10-14 02:35:096561
6562 // TODO(avi): Remove. http://crbug.com/170921
[email protected]8ff00d72012-10-23 19:12:216563 NotificationService::current()->Notify(
6564 NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
6565 Source<WebContents>(this),
[email protected]50279492014-05-05 23:24:296566 NotificationService::NoDetails());
[email protected]25497492010-09-11 15:15:086567}
6568
creis8eb8ef22015-11-04 22:51:386569void WebContentsImpl::UpdateStateForFrame(RenderFrameHost* render_frame_host,
6570 const PageState& page_state) {
creis8eb8ef22015-11-04 22:51:386571 // The state update affects the last NavigationEntry associated with the given
6572 // |render_frame_host|. This may not be the last committed NavigationEntry (as
6573 // in the case of an UpdateState from a frame being swapped out). We track
6574 // which entry this is in the RenderFrameHost's nav_entry_id.
6575 RenderFrameHostImpl* rfhi =
6576 static_cast<RenderFrameHostImpl*>(render_frame_host);
6577 NavigationEntryImpl* entry =
6578 controller_.GetEntryWithUniqueID(rfhi->nav_entry_id());
6579 if (!entry)
6580 return;
6581
6582 FrameNavigationEntry* frame_entry =
6583 entry->GetFrameEntry(rfhi->frame_tree_node());
6584 if (!frame_entry)
6585 return;
6586
creisd06a9422015-11-11 03:08:456587 // The SiteInstance might not match if we do a cross-process navigation with
6588 // replacement (e.g., auto-subframe), in which case the swap out of the old
6589 // RenderFrameHost runs in the background after the old FrameNavigationEntry
6590 // has already been replaced and destroyed.
6591 if (frame_entry->site_instance() != rfhi->GetSiteInstance())
6592 return;
6593
creis8eb8ef22015-11-04 22:51:386594 if (page_state == frame_entry->page_state())
6595 return; // Nothing to update.
6596
creis0cade2e2017-02-28 06:37:476597 DCHECK(page_state.IsValid()) << "Shouldn't set an empty PageState.";
nasko34634672016-07-29 18:46:066598
6599 // The document_sequence_number and item_sequence_number recorded in the
6600 // FrameNavigationEntry should not differ from the one coming with the update,
6601 // since it must come from the same document. Do not update it if a difference
6602 // is detected, as this indicates that |frame_entry| is not the correct one.
6603 ExplodedPageState exploded_state;
6604 if (!DecodePageState(page_state.ToEncodedData(), &exploded_state))
6605 return;
6606
6607 if (exploded_state.top.document_sequence_number !=
6608 frame_entry->document_sequence_number() ||
6609 exploded_state.top.item_sequence_number !=
6610 frame_entry->item_sequence_number()) {
6611 return;
6612 }
6613
nasko04a322f25a2016-09-01 00:00:346614 frame_entry->SetPageState(page_state);
creis8eb8ef22015-11-04 22:51:386615 controller_.NotifyEntryChanged(entry);
6616}
6617
[email protected]ef3adfc2014-05-11 00:04:546618void WebContentsImpl::UpdateTitle(RenderFrameHost* render_frame_host,
[email protected]ef3adfc2014-05-11 00:04:546619 const base::string16& title,
6620 base::i18n::TextDirection title_direction) {
[email protected]ef3adfc2014-05-11 00:04:546621 // Try to find the navigation entry, which might not be the current one.
avi286019ce2015-07-29 01:26:586622 // For example, it might be from a recently swapped out RFH.
avi1edc8552016-10-13 21:37:166623 NavigationEntryImpl* entry = controller_.GetEntryWithUniqueID(
creisad8c31a2016-05-24 17:07:046624 static_cast<RenderFrameHostImpl*>(render_frame_host)->nav_entry_id());
[email protected]ef3adfc2014-05-11 00:04:546625
6626 // We can handle title updates when we don't have an entry in
6627 // UpdateTitleForEntry, but only if the update is from the current RVH.
6628 // TODO(avi): Change to make decisions based on the RenderFrameHost.
avi286019ce2015-07-29 01:26:586629 if (!entry && render_frame_host != GetMainFrame())
[email protected]ef3adfc2014-05-11 00:04:546630 return;
6631
6632 // TODO(evan): make use of title_direction.
6633 // http://code.google.com/p/chromium/issues/detail?id=27094
afakhry6f0c1ec22016-07-14 13:55:136634 UpdateTitleForEntry(entry, title);
[email protected]ef3adfc2014-05-11 00:04:546635}
6636
alexmose7da5a12015-04-09 02:22:166637bool WebContentsImpl::ShouldRouteMessageEvent(
6638 RenderFrameHost* target_rfh,
6639 SiteInstance* source_site_instance) const {
6640 // Allow the message if this WebContents is dedicated to a browser plugin
6641 // guest.
[email protected]0eba810b2012-10-18 03:19:366642 // Note: This check means that an embedder could theoretically receive a
6643 // postMessage from anyone (not just its own guests). However, this is
6644 // probably not a risk for apps since other pages won't have references
6645 // to App windows.
alexmose7da5a12015-04-09 02:22:166646 return GetBrowserPluginGuest() || GetBrowserPluginEmbedder();
6647}
[email protected]f546640b2012-05-15 00:03:496648
alexmos58729042015-06-18 23:20:006649void WebContentsImpl::EnsureOpenerProxiesExist(RenderFrameHost* source_rfh) {
alexmose7da5a12015-04-09 02:22:166650 WebContentsImpl* source_web_contents = static_cast<WebContentsImpl*>(
6651 WebContents::FromRenderFrameHost(source_rfh));
[email protected]f546640b2012-05-15 00:03:496652
alexmose7da5a12015-04-09 02:22:166653 if (source_web_contents) {
lazyboy6ec48b2a2015-06-29 15:18:146654 // If this message is going to outer WebContents from inner WebContents,
6655 // then we should not create a RenderView. AttachToOuterWebContentsFrame()
6656 // already created a RenderFrameProxyHost for that purpose.
6657 if (GetBrowserPluginEmbedder() &&
W. James MacLean2539adb32019-12-13 00:40:446658 source_web_contents->browser_plugin_guest_) {
lazyboy6ec48b2a2015-06-29 15:18:146659 return;
W. James MacLean2539adb32019-12-13 00:40:446660 }
lazyboy6ec48b2a2015-06-29 15:18:146661
lfg812874d2016-02-09 22:26:136662 if (this != source_web_contents && GetBrowserPluginGuest()) {
naskob3041b98a42016-03-12 04:43:066663 // We create a RenderFrameProxyHost for the embedder in the guest's render
6664 // process but we intentionally do not expose the embedder's opener chain
6665 // to it.
6666 source_web_contents->GetRenderManager()->CreateRenderFrameProxy(
6667 GetSiteInstance());
[email protected]f546640b2012-05-15 00:03:496668 } else {
alexmos58729042015-06-18 23:20:006669 RenderFrameHostImpl* source_rfhi =
6670 static_cast<RenderFrameHostImpl*>(source_rfh);
6671 source_rfhi->frame_tree_node()->render_manager()->CreateOpenerProxies(
alexmos90325cf2015-09-02 17:18:396672 GetSiteInstance(), nullptr);
[email protected]f546640b2012-05-15 00:03:496673 }
6674 }
[email protected]f546640b2012-05-15 00:03:496675}
6676
avallee9993fca2016-11-17 06:16:506677void WebContentsImpl::SetAsFocusedWebContentsIfNecessary() {
Adithya Srinivasan11af2c52019-12-16 22:52:556678 DCHECK(!portal());
avallee9993fca2016-11-17 06:16:506679 // Only change focus if we are not currently focused.
6680 WebContentsImpl* old_contents = GetFocusedWebContents();
6681 if (old_contents == this)
6682 return;
6683
paulmeyerfeafc2d2017-04-25 21:46:406684 GetOutermostWebContents()->node_.SetFocusedWebContents(this);
6685
avallee9993fca2016-11-17 06:16:506686 // Send a page level blur to the old contents so that it displays inactive UI
6687 // and focus this contents to activate it.
6688 if (old_contents)
6689 old_contents->GetMainFrame()->GetRenderWidgetHost()->SetPageFocus(false);
avalleec2ff1ef2017-01-21 00:12:306690
6691 // Make sure the outer web contents knows our frame is focused. Otherwise, the
6692 // outer renderer could have the element before or after the frame element
6693 // focused which would return early without actually advancing focus.
avalleed68cdd12017-06-21 22:07:276694 FocusOuterAttachmentFrameChain();
avalleec2ff1ef2017-01-21 00:12:306695
Carlos ILd51e7702020-05-07 18:51:396696 GetMainFrame()->GetRenderWidgetHost()->SetPageFocus(true);
avallee9993fca2016-11-17 06:16:506697}
6698
avallee0206f782016-07-28 18:55:336699void WebContentsImpl::SetFocusedFrame(FrameTreeNode* node,
6700 SiteInstance* source) {
Adithya Srinivasan11af2c52019-12-16 22:52:556701 DCHECK(!portal());
avallee0206f782016-07-28 18:55:336702 frame_tree_.SetFocusedFrame(node, source);
avalleed68cdd12017-06-21 22:07:276703
Lucas Furukawa Gadaniabbfa342017-08-29 19:37:576704 if (auto* inner_contents = node_.GetInnerWebContentsInFrame(node)) {
6705 // |this| is an outer WebContents and |node| represents an inner
6706 // WebContents. Transfer the focus to the inner contents if |this| is
6707 // focused.
6708 if (GetFocusedWebContents() == this)
6709 inner_contents->SetAsFocusedWebContentsIfNecessary();
6710 } else if (node_.OuterContentsFrameTreeNode() &&
6711 node_.OuterContentsFrameTreeNode()
6712 ->current_frame_host()
6713 ->GetSiteInstance() == source) {
6714 // |this| is an inner WebContents, |node| is its main FrameTreeNode and
6715 // the outer WebContents FrameTreeNode is at |source|'s SiteInstance.
6716 // Transfer the focus to the inner WebContents if the outer WebContents is
6717 // focused. This branch is used when an inner WebContents is focused through
Lowell Mannersa9530382019-08-01 09:46:576718 // its RenderFrameProxyHost (via FrameFocused mojo call, used to
Lucas Furukawa Gadaniabbfa342017-08-29 19:37:576719 // implement the window.focus() API).
6720 if (GetFocusedWebContents() == GetOuterWebContents())
6721 SetAsFocusedWebContentsIfNecessary();
6722 } else if (!GetOuterWebContents()) {
6723 // This is an outermost WebContents.
6724 SetAsFocusedWebContentsIfNecessary();
Lucas Furukawa Gadaniabbfa342017-08-29 19:37:576725 }
avalleed68cdd12017-06-21 22:07:276726}
6727
Avi Drissman36f801f2018-02-01 20:06:046728void WebContentsImpl::DidCallFocus() {
6729 // Any explicit focusing of another window while this WebContents is in
6730 // fullscreen can be used to confuse the user, so drop fullscreen.
Avi Drissman1a55a9d62020-03-10 18:56:456731 base::ScopedClosureRunner fullscreen_block = ForSecurityDropFullscreen();
6732 // The other contents is independent of this contents, so release the
6733 // fullscreen block.
6734 fullscreen_block.RunAndReset();
Avi Drissman36f801f2018-02-01 20:06:046735}
6736
avalleed68cdd12017-06-21 22:07:276737RenderFrameHost* WebContentsImpl::GetFocusedFrameIncludingInnerWebContents() {
6738 WebContentsImpl* contents = this;
6739 FrameTreeNode* focused_node = contents->frame_tree_.GetFocusedFrame();
6740
6741 // If there is no focused frame in the outer WebContents, we need to return
6742 // null.
6743 if (!focused_node)
6744 return nullptr;
6745
6746 // If the focused frame is embedding an inner WebContents, we must descend
6747 // into that contents. If the current WebContents does not have a focused
6748 // frame, return the main frame of this contents instead of the focused empty
6749 // frame embedding this contents.
6750 while (true) {
6751 contents = contents->node_.GetInnerWebContentsInFrame(focused_node);
6752 if (!contents)
6753 return focused_node->current_frame_host();
6754
6755 focused_node = contents->frame_tree_.GetFocusedFrame();
6756 if (!focused_node)
6757 return contents->GetMainFrame();
6758 }
avallee0206f782016-07-28 18:55:336759}
6760
Lucas Furukawa Gadaniabbfa342017-08-29 19:37:576761void WebContentsImpl::OnAdvanceFocus(RenderFrameHostImpl* source_rfh) {
6762 // When a RenderFrame needs to advance focus to a RenderFrameProxy (by hitting
6763 // TAB), the RenderFrameProxy sends an IPC to RenderFrameProxyHost. When this
6764 // RenderFrameProxyHost represents an inner WebContents, the outer WebContents
6765 // needs to focus the inner WebContents.
6766 if (GetOuterWebContents() &&
6767 GetOuterWebContents() == source_rfh->delegate()->GetAsWebContents() &&
6768 GetFocusedWebContents() == GetOuterWebContents()) {
6769 SetAsFocusedWebContentsIfNecessary();
6770 }
6771}
6772
ekaramada110f642016-12-21 19:47:286773void WebContentsImpl::OnFocusedElementChangedInFrame(
6774 RenderFrameHostImpl* frame,
6775 const gfx::Rect& bounds_in_root_view) {
6776 RenderWidgetHostViewBase* root_view =
6777 static_cast<RenderWidgetHostViewBase*>(GetRenderWidgetHostView());
6778 if (!root_view || !frame->GetView())
6779 return;
Katie De0131372020-07-09 23:03:106780 // Convert to screen coordinates from window coordinates by adding the
6781 // window's origin.
ekaramada110f642016-12-21 19:47:286782 gfx::Point origin = bounds_in_root_view.origin();
6783 origin += root_view->GetViewBounds().OffsetFromOrigin();
6784 gfx::Rect bounds_in_screen(origin, bounds_in_root_view.size());
6785
6786 root_view->FocusedNodeChanged(frame->has_focused_editable_element(),
6787 bounds_in_screen);
6788
6789 FocusedNodeDetails details = {frame->has_focused_editable_element(),
6790 bounds_in_screen};
6791
6792 // TODO(ekaramad): We should replace this with an observer notification
6793 // (https://crbug.com/675975).
6794 NotificationService::current()->Notify(
6795 NOTIFICATION_FOCUS_CHANGED_IN_PAGE,
6796 Source<RenderViewHost>(GetRenderViewHost()),
6797 Details<FocusedNodeDetails>(&details));
Jeff Fisher709b4c52019-07-29 20:07:346798
6799 for (auto& observer : observers_)
6800 observer.OnFocusChangedInPage(&details);
ekaramada110f642016-12-21 19:47:286801}
6802
Lowell Manners1de5242e2019-04-25 10:18:466803bool WebContentsImpl::DidAddMessageToConsole(
6804 blink::mojom::ConsoleMessageLevel log_level,
6805 const base::string16& message,
6806 int32_t line_no,
6807 const base::string16& source_id) {
Devlin Cronin5ea668f32019-12-18 21:57:586808 for (auto& observer : observers_)
6809 observer.OnDidAddMessageToConsole(log_level, message, line_no, source_id);
6810
[email protected]a796f202012-05-30 14:14:256811 if (!delegate_)
6812 return false;
Lowell Manners1de5242e2019-04-25 10:18:466813 return delegate_->DidAddMessageToConsole(this, log_level, message, line_no,
avia90ae4e2016-11-11 20:49:336814 source_id);
[email protected]a796f202012-05-30 14:14:256815}
6816
Daniel Cheng90196c82018-04-25 21:49:146817void WebContentsImpl::DidReceiveInputEvent(
dominickn9c7ceaa32016-02-27 01:52:536818 RenderWidgetHostImpl* render_widget_host,
Emily Starkc7bd40c42020-07-21 19:12:556819 const blink::WebInputEvent& event) {
6820 if (!IsUserInteractionInputType(event.GetType()))
Daniel Cheng90196c82018-04-25 21:49:146821 return;
6822
kenrb19221852016-04-29 17:21:406823 // Ignore unless the widget is currently in the frame tree.
6824 if (!HasMatchingWidgetHost(&frame_tree_, render_widget_host))
avic3aa8422015-11-09 20:57:226825 return;
6826
Emily Starkc7bd40c42020-07-21 19:12:556827 if (event.GetType() != blink::WebInputEvent::Type::kGestureScrollBegin)
Daniel Cheng90196c82018-04-25 21:49:146828 last_interactive_input_event_time_ = ui::EventTimeForNow();
dominickn9c7ceaa32016-02-27 01:52:536829
Dominick Ng37a15052018-07-17 23:56:356830 for (auto& observer : observers_)
Emily Starkc7bd40c42020-07-21 19:12:556831 observer.DidGetUserInteraction(event);
dominickn2dd142dd2015-10-29 05:30:506832}
6833
Avi Drissman738ea192018-08-29 20:24:166834bool WebContentsImpl::ShouldIgnoreInputEvents() {
6835 WebContentsImpl* web_contents = this;
6836 while (web_contents) {
6837 if (web_contents->ignore_input_events_)
6838 return true;
6839 web_contents = web_contents->GetOuterWebContents();
6840 }
6841
6842 return false;
6843}
6844
avallee9993fca2016-11-17 06:16:506845void WebContentsImpl::FocusOwningWebContents(
6846 RenderWidgetHostImpl* render_widget_host) {
Alex Moshchukc4f0a722019-11-19 22:41:286847 RenderWidgetHostImpl* main_frame_widget_host =
6848 GetMainFrame()->GetRenderWidgetHost();
avallee9993fca2016-11-17 06:16:506849 RenderWidgetHostImpl* focused_widget =
Alex Moshchukc4f0a722019-11-19 22:41:286850 GetFocusedRenderWidgetHost(main_frame_widget_host);
avallee9993fca2016-11-17 06:16:506851
6852 if (focused_widget != render_widget_host &&
avallee8e881592016-11-19 00:39:306853 (!focused_widget ||
6854 focused_widget->delegate() != render_widget_host->delegate())) {
avallee9993fca2016-11-17 06:16:506855 SetAsFocusedWebContentsIfNecessary();
6856 }
6857}
6858
[email protected]b172aee2012-04-10 17:05:266859void WebContentsImpl::OnIgnoredUIEvent() {
[email protected]31145092011-09-30 01:49:446860 // Notify observers.
ericwilligersde386342016-10-19 02:35:096861 for (auto& observer : observers_)
6862 observer.DidGetIgnoredUIEvent();
[email protected]fa1cf0b82010-01-15 21:49:446863}
6864
avic3aa8422015-11-09 20:57:226865void WebContentsImpl::RendererUnresponsive(
Lukasz Anforowicz52b93722018-06-20 16:11:396866 RenderWidgetHostImpl* render_widget_host,
6867 base::RepeatingClosure hang_monitor_restarter) {
avic031d392017-03-03 03:09:426868 if (ShouldIgnoreUnresponsiveRenderer())
[email protected]e5fc1632011-08-08 07:51:536869 return;
6870
Lukasz Anforowicz4f0593d2018-09-21 18:33:546871 // Do not report hangs (to task manager, to hang renderer dialog, etc.) for
6872 // invisible tabs (like extension background page, background tabs). See
6873 // https://crbug.com/881812 for rationale and for choosing the visibility
6874 // (rather than process priority) as the signal here.
6875 if (GetVisibility() != Visibility::VISIBLE)
6876 return;
6877
Avi Drissman8920def2018-01-31 19:53:366878 if (!render_widget_host->renderer_initialized())
[email protected]55452902011-06-01 21:57:476879 return;
6880
Lukasz Anforowicz4f0593d2018-09-21 18:33:546881 for (auto& observer : observers_)
6882 observer.OnRendererUnresponsive(render_widget_host->GetProcess());
6883
Dave Tapuskac6146c52017-12-20 22:48:156884 if (delegate_)
Lukasz Anforowicz52b93722018-06-20 16:11:396885 delegate_->RendererUnresponsive(this, render_widget_host,
6886 std::move(hang_monitor_restarter));
[email protected]420ae012009-04-24 05:16:326887}
6888
avic3aa8422015-11-09 20:57:226889void WebContentsImpl::RendererResponsive(
6890 RenderWidgetHostImpl* render_widget_host) {
Scott Violet48202e62020-05-26 17:19:336891 for (auto& observer : observers_)
6892 observer.OnRendererResponsive(render_widget_host->GetProcess());
6893
[email protected]6934a702011-12-20 00:04:516894 if (delegate_)
Avi Drissman1572e8c3c2018-02-02 19:06:366895 delegate_->RendererResponsive(this, render_widget_host);
[email protected]420ae012009-04-24 05:16:326896}
6897
Alex Moshchuk7b4f0652019-05-30 18:54:416898void WebContentsImpl::SubframeCrashed(
6899 blink::mojom::FrameVisibility visibility) {
6900 // If a subframe crashed on a hidden tab, mark the tab for reload to avoid
6901 // showing a sad frame to the user if they ever switch back to that tab. Do
6902 // this for subframes that are either visible in viewport or visible but
6903 // scrolled out of view, but skip subframes that are not rendered (e.g., via
6904 // "display:none"), since in that case the user wouldn't see a sad frame
6905 // anyway.
6906 bool did_mark_for_reload = false;
6907 if (IsHidden() && visibility != blink::mojom::FrameVisibility::kNotRendered &&
6908 base::FeatureList::IsEnabled(
6909 features::kReloadHiddenTabsWithCrashedSubframes)) {
6910 controller_.SetNeedsReload(
6911 NavigationControllerImpl::NeedsReloadType::kCrashedSubframe);
6912 did_mark_for_reload = true;
6913 UMA_HISTOGRAM_ENUMERATION(
6914 "Stability.ChildFrameCrash.TabMarkedForReload.Visibility", visibility);
6915 }
6916
6917 UMA_HISTOGRAM_BOOLEAN("Stability.ChildFrameCrash.TabMarkedForReload",
6918 did_mark_for_reload);
6919}
6920
[email protected]b172aee2012-04-10 17:05:266921void WebContentsImpl::BeforeUnloadFiredFromRenderManager(
[email protected]feaded02012-09-25 19:12:556922 bool proceed, const base::TimeTicks& proceed_time,
[email protected]420ae012009-04-24 05:16:326923 bool* proceed_to_fire_unload) {
ericwilligersde386342016-10-19 02:35:096924 for (auto& observer : observers_)
Chris Hamilton3b3e315d2018-09-19 13:16:216925 observer.BeforeUnloadFired(proceed, proceed_time);
[email protected]6934a702011-12-20 00:04:516926 if (delegate_)
6927 delegate_->BeforeUnloadFired(this, proceed, proceed_to_fire_unload);
[email protected]e80af492013-06-24 21:52:096928 // Note: |this| might be deleted at this point.
[email protected]420ae012009-04-24 05:16:326929}
6930
[email protected]58d5cfe2013-07-10 02:40:526931void WebContentsImpl::RenderProcessGoneFromRenderManager(
[email protected]3a3d47472010-07-15 21:03:546932 RenderViewHost* render_view_host) {
[email protected]443b80e2010-12-14 00:42:236933 DCHECK(crashed_status_ != base::TERMINATION_STATUS_STILL_RUNNING);
[email protected]ec6a7eb2013-04-22 17:34:226934 RenderViewTerminated(render_view_host, crashed_status_, crashed_error_code_);
[email protected]3a3d47472010-07-15 21:03:546935}
6936
[email protected]af905902013-10-01 21:38:516937void WebContentsImpl::CancelModalDialogsForRenderManager() {
6938 // We need to cancel modal dialogs when doing a process swap, since the load
avi2460c762015-04-17 15:21:546939 // deferrer would prevent us from swapping out. We also clear the state
6940 // because this is a cross-process navigation, which means that it's a new
6941 // site that should not have to pay for the sins of its predecessor.
creis89a0f782015-05-27 16:13:176942 //
Lei Zhang6410ec1a2019-01-31 21:38:586943 // Note that we don't bother telling |browser_plugin_embedder_| because the
creis89a0f782015-05-27 16:13:176944 // cross-process navigation will either destroy the browser plugins or not
6945 // require their dialogs to close.
avi5d3b8692016-10-12 22:00:466946 if (dialog_manager_) {
avi6879fcf2017-01-21 05:27:536947 dialog_manager_->CancelDialogs(this, /*reset_state=*/true);
avi5d3b8692016-10-12 22:00:466948 }
[email protected]af905902013-10-01 21:38:516949}
6950
danakja0bce4c952020-05-12 20:40:286951void WebContentsImpl::NotifySwappedFromRenderManager(RenderFrameHost* old_frame,
6952 RenderFrameHost* new_frame,
[email protected]02d7b6e2014-06-24 21:01:506953 bool is_main_frame) {
6954 if (is_main_frame) {
danakja0bce4c952020-05-12 20:40:286955 // The |new_frame| and its various compadres are already swapped into place
6956 // for the WebContentsImpl when this method is called.
6957 DCHECK(GetMainFrame() == new_frame);
6958 DCHECK(GetRenderViewHost() == new_frame->GetRenderViewHost());
6959 DCHECK(GetRenderWidgetHostView() == new_frame->GetView());
6960
Fergal Daly6767b2e2020-02-04 03:15:166961 RenderViewHost* old_rvh =
danakja0bce4c952020-05-12 20:40:286962 old_frame ? old_frame->GetRenderViewHost() : nullptr;
6963 RenderViewHost* new_rvh = new_frame->GetRenderViewHost();
Fergal Daly6767b2e2020-02-04 03:15:166964 // |old_rvh| and |new_rvh| might be equal when navigating from a crashed
6965 // RenderFrameHost to a new same-site one. With RenderDocument, this will
6966 // happen for every same-site navigation.
6967 if (old_rvh != new_rvh)
6968 NotifyViewSwapped(old_rvh, new_rvh);
[email protected]a6b73c62013-02-11 23:05:086969
[email protected]02d7b6e2014-06-24 21:01:506970 // Make sure the visible RVH reflects the new delegate's preferences.
6971 if (delegate_)
6972 view_->SetOverscrollControllerEnabled(CanOverscrollContent());
[email protected]d56a49e2013-02-23 00:47:396973
danakja0bce4c952020-05-12 20:40:286974 auto* rwhv = static_cast<RenderWidgetHostViewBase*>(new_frame->GetView());
6975 if (rwhv) {
6976 rwhv->SetMainFrameAXTreeID(new_frame->GetAXTreeID());
6977
6978 // The RenderWidgetHostView for the speculative RenderFrameHost is not
6979 // resized with the current RenderFrameHost while a navigation is
6980 // pending. So when we swap in the main frame, we need to update the
6981 // RenderWidgetHostView's size.
6982 //
6983 // Historically, this was done to fix b/1079768 for interstitials.
6984 rwhv->SetSize(GetSizeForMainFrame());
6985 }
[email protected]02d7b6e2014-06-24 21:01:506986 }
6987
danakja0bce4c952020-05-12 20:40:286988 NotifyFrameSwapped(old_frame, new_frame, is_main_frame);
[email protected]3a3d47472010-07-15 21:03:546989}
6990
naskof5940b9f2015-03-02 23:04:056991void WebContentsImpl::NotifyMainFrameSwappedFromRenderManager(
danakja0bce4c952020-05-12 20:40:286992 RenderFrameHost* old_frame,
6993 RenderFrameHost* new_frame) {
6994 NotifyViewSwapped(old_frame ? old_frame->GetRenderViewHost() : nullptr,
6995 new_frame->GetRenderViewHost());
naskof5940b9f2015-03-02 23:04:056996}
6997
[email protected]b172aee2012-04-10 17:05:266998NavigationControllerImpl& WebContentsImpl::GetControllerForRenderManager() {
[email protected]d1198fd2012-08-13 22:50:196999 return GetController();
[email protected]3a3d47472010-07-15 21:03:547000}
7001
dcheng59716272016-04-09 05:19:087002std::unique_ptr<WebUIImpl> WebContentsImpl::CreateWebUIForRenderFrameHost(
Miyoung Shin072d4052020-07-13 07:52:447003 RenderFrameHost* frame_host,
Denis Kuznetsov [CET]2c6f0159f2020-05-18 14:18:167004 const GURL& url) {
Miyoung Shin072d4052020-07-13 07:52:447005 return CreateWebUI(frame_host, url);
[email protected]420ae012009-04-24 05:16:327006}
7007
lfgbede6392015-09-11 21:54:067008void WebContentsImpl::CreateRenderWidgetHostViewForRenderManager(
7009 RenderViewHost* render_view_host) {
lfgc740d4b22016-04-15 16:45:337010 RenderWidgetHostViewBase* rwh_view =
W. James MacLean13d834d2019-12-04 16:06:457011 view_->CreateViewForWidget(render_view_host->GetWidget());
danakja0bce4c952020-05-12 20:40:287012 rwh_view->SetSize(GetSizeForMainFrame());
lfgbede6392015-09-11 21:54:067013}
7014
7015bool WebContentsImpl::CreateRenderViewForRenderManager(
7016 RenderViewHost* render_view_host,
Antonio Gomesb3dd5fc2020-05-25 22:53:277017 const base::Optional<base::UnguessableToken>& opener_frame_token,
Daniel Chengabfdb002020-06-04 06:02:307018 int proxy_routing_id) {
lfgbede6392015-09-11 21:54:067019 TRACE_EVENT0("browser,navigation",
7020 "WebContentsImpl::CreateRenderViewForRenderManager");
7021
piman00cf9402017-04-03 17:09:207022 if (proxy_routing_id == MSG_ROUTING_NONE)
lfg9431efd2016-12-14 14:45:267023 CreateRenderWidgetHostViewForRenderManager(render_view_host);
lfg9431efd2016-12-14 14:45:267024
piman00cf9402017-04-03 17:09:207025 if (!static_cast<RenderViewHostImpl*>(render_view_host)
Daniel Chengabfdb002020-06-04 06:02:307026 ->CreateRenderView(opener_frame_token, proxy_routing_id,
Antonio Gomesb3dd5fc2020-05-25 22:53:277027 created_with_opener_)) {
[email protected]a4127722011-04-27 23:13:527028 return false;
[email protected]14392a52012-05-02 20:28:447029 }
W. James MacLean62198672019-06-04 16:46:107030 // Set the TextAutosizer state from the main frame's renderer on the new view,
7031 // but only if it's not for the main frame. Main frame renderers should create
7032 // this state themselves from up-to-date values, so we shouldn't override it
7033 // with the cached values.
7034 if (!render_view_host->GetMainFrame()) {
7035 render_view_host->Send(
7036 new PageMsg_UpdateTextAutosizerPageInfoForRemoteMainFrames(
7037 render_view_host->GetRoutingID(), text_autosizer_page_info_));
7038 }
[email protected]a4127722011-04-27 23:13:527039
Lucas Furukawa Gadaniaed1fed2017-10-13 17:34:147040 if (proxy_routing_id == MSG_ROUTING_NONE && node_.outer_web_contents())
7041 ReattachToOuterWebContentsFrame();
7042
avi2b177592014-12-10 02:08:027043 SetHistoryOffsetAndLengthForView(render_view_host,
7044 controller_.GetLastCommittedEntryIndex(),
7045 controller_.GetEntryCount());
7046
[email protected]a220b5932013-09-21 03:47:447047#if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID)
[email protected]f8233cc2011-05-31 20:24:507048 // Force a ViewMsg_Resize to be sent, needed to make plugins show up on
7049 // linux. See crbug.com/83941.
avif9ab5d942015-10-15 14:05:447050 RenderWidgetHostView* rwh_view = render_view_host->GetWidget()->GetView();
[email protected]245f7d52011-11-28 15:36:447051 if (rwh_view) {
7052 if (RenderWidgetHost* render_widget_host = rwh_view->GetRenderWidgetHost())
Fady Samuel0b911822018-04-25 13:22:167053 render_widget_host->SynchronizeVisualProperties();
[email protected]245f7d52011-11-28 15:36:447054 }
[email protected]f8233cc2011-05-31 20:24:507055#endif
7056
[email protected]420ae012009-04-24 05:16:327057 return true;
7058}
7059
[email protected]82307f6b2014-08-07 03:30:127060bool WebContentsImpl::CreateRenderFrameForRenderManager(
7061 RenderFrameHost* render_frame_host,
arthursonzogni1efc7b962019-03-14 09:36:337062 int previous_routing_id,
Antonio Gomes9d150542020-05-25 18:32:117063 const base::Optional<base::UnguessableToken>& opener_frame_token,
japhete6adf142014-10-31 00:01:497064 int parent_routing_id,
alexmosa181efc2015-09-03 00:39:047065 int previous_sibling_routing_id) {
naskob8744d22014-08-28 17:07:437066 TRACE_EVENT0("browser,navigation",
7067 "WebContentsImpl::CreateRenderFrameForRenderManager");
[email protected]82307f6b2014-08-07 03:30:127068
7069 RenderFrameHostImpl* rfh =
7070 static_cast<RenderFrameHostImpl*>(render_frame_host);
Antonio Gomes9d150542020-05-25 18:32:117071 if (!rfh->CreateRenderFrame(previous_routing_id, opener_frame_token,
7072 parent_routing_id, previous_sibling_routing_id)) {
[email protected]82307f6b2014-08-07 03:30:127073 return false;
Antonio Gomes9d150542020-05-25 18:32:117074 }
[email protected]82307f6b2014-08-07 03:30:127075
7076 // TODO(nasko): When RenderWidgetHost is owned by RenderFrameHost, the passed
7077 // RenderFrameHost will have to be associated with the appropriate
7078 // RenderWidgetHostView or a new one should be created here.
7079
7080 return true;
7081}
7082
mfomitchev2b8b066a2016-01-28 19:23:157083#if defined(OS_ANDROID)
[email protected]fc2b46b2014-05-03 16:33:457084
[email protected]155c7f22013-12-09 17:07:187085base::android::ScopedJavaLocalRef<jobject>
7086WebContentsImpl::GetJavaWebContents() {
mostynb042582e2015-03-16 22:13:407087 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]0e813a52014-08-13 10:34:567088 return GetWebContentsAndroid()->GetJavaObject();
7089}
[email protected]155c7f22013-12-09 17:07:187090
[email protected]0e813a52014-08-13 10:34:567091WebContentsAndroid* WebContentsImpl::GetWebContentsAndroid() {
Kevin McNee576bb5d82019-05-29 16:47:517092 if (!web_contents_android_) {
7093 web_contents_android_ = std::make_unique<WebContentsAndroid>(this);
[email protected]155c7f22013-12-09 17:07:187094 }
Kevin McNee576bb5d82019-05-29 16:47:517095 return web_contents_android_.get();
7096}
7097
7098void WebContentsImpl::ClearWebContentsAndroid() {
7099 DCHECK_CURRENTLY_ON(BrowserThread::UI);
7100 web_contents_android_.reset();
[email protected]155c7f22013-12-09 17:07:187101}
7102
paulmeyerc0b762b2016-04-13 11:55:177103void WebContentsImpl::ActivateNearestFindResult(float x,
7104 float y) {
7105 GetOrCreateFindRequestManager()->ActivateNearestFindResult(x, y);
7106}
7107
7108void WebContentsImpl::RequestFindMatchRects(int current_version) {
7109 GetOrCreateFindRequestManager()->RequestFindMatchRects(current_version);
7110}
7111
rockot400ea35b2016-10-15 19:15:327112service_manager::InterfaceProvider* WebContentsImpl::GetJavaInterfaces() {
sammcf5f1b0f2016-09-20 23:05:117113 if (!java_interfaces_) {
Gyuyoung Kim6c9ce9022019-11-26 05:40:087114 mojo::PendingRemote<service_manager::mojom::InterfaceProvider> provider;
7115 BindInterfaceRegistryForWebContents(
7116 provider.InitWithNewPipeAndPassReceiver(), this);
rockot400ea35b2016-10-15 19:15:327117 java_interfaces_.reset(new service_manager::InterfaceProvider);
sammcf5f1b0f2016-09-20 23:05:117118 java_interfaces_->Bind(std::move(provider));
7119 }
7120 return java_interfaces_.get();
7121}
7122
Francois Doray6c26f3342018-08-24 17:17:227123#endif
7124
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:417125bool WebContentsImpl::CompletedFirstVisuallyNonEmptyPaint() {
Carlos Caballero91fffb22019-10-29 15:24:307126 return GetRenderViewHost()->did_first_visually_non_empty_paint();
spqchane72c3cb2017-06-30 23:02:107127}
7128
pkotwicz75ca8ffd2016-02-16 23:10:197129void WebContentsImpl::OnDidDownloadImage(
Avi Drissmana5a52dd2018-03-27 03:39:027130 ImageDownloadCallback callback,
pkotwicz75ca8ffd2016-02-16 23:10:197131 int id,
7132 const GURL& image_url,
leon.hancbc4bc3c2016-02-26 07:08:527133 int32_t http_status_code,
leon.hanc2228532016-08-16 05:59:187134 const std::vector<SkBitmap>& images,
7135 const std::vector<gfx::Size>& original_image_sizes) {
Avi Drissmana5a52dd2018-03-27 03:39:027136 std::move(callback).Run(id, http_status_code, image_url, images,
7137 original_image_sizes);
pkotwicz75ca8ffd2016-02-16 23:10:197138}
7139
[email protected]6fba26d2014-04-29 09:38:287140void WebContentsImpl::OnDialogClosed(int render_process_id,
7141 int render_frame_id,
Dave Tapuskacdf545cc2020-01-23 18:38:527142 JavaScriptDialogCallback response_callback,
Avi Drissman1a55a9d62020-03-10 18:56:457143 base::ScopedClosureRunner fullscreen_block,
[email protected]87de04b02014-04-08 22:14:497144 bool dialog_was_suppressed,
[email protected]b172aee2012-04-10 17:05:267145 bool success,
[email protected]fcf75d42013-12-03 20:11:267146 const base::string16& user_input) {
[email protected]6fba26d2014-04-29 09:38:287147 RenderFrameHostImpl* rfh = RenderFrameHostImpl::FromID(render_process_id,
7148 render_frame_id);
[email protected]87de04b02014-04-08 22:14:497149 last_dialog_suppressed_ = dialog_was_suppressed;
Avi Drissman1a55a9d62020-03-10 18:56:457150 fullscreen_block.RunAndReset();
[email protected]87de04b02014-04-08 22:14:497151
Charlie Reis96077362019-07-04 00:34:047152 javascript_dialog_navigation_deferrer_.reset();
7153
[email protected]beb440c2009-11-06 04:08:547154 if (is_showing_before_unload_dialog_ && !success) {
clamybc6f4152016-04-01 20:14:237155 // It is possible for the current RenderFrameHost to have changed in the
7156 // meantime. Do not reset the navigation state in that case.
7157 if (rfh && rfh == rfh->frame_tree_node()->current_frame_host()) {
clamy44e84ce2016-02-22 15:38:257158 rfh->frame_tree_node()->BeforeUnloadCanceled();
clamybc6f4152016-04-01 20:14:237159 controller_.DiscardNonCommittedEntries();
7160 }
[email protected]ec8e8b02013-06-19 04:57:107161
Charles Reis15d60b132017-11-03 17:43:477162 // Update the URL display either way, to avoid showing a stale URL.
7163 NotifyNavigationStateChanged(INVALIDATE_TYPE_URL);
7164
ericwilligersde386342016-10-19 02:35:097165 for (auto& observer : observers_)
7166 observer.BeforeUnloadDialogCancelled();
[email protected]beb440c2009-11-06 04:08:547167 }
[email protected]87de04b02014-04-08 22:14:497168
Dave Tapuskacdf545cc2020-01-23 18:38:527169 std::move(response_callback).Run(success, user_input);
Pavel Feldman3c1842b2017-08-02 05:00:167170
Dave Tapuskacdf545cc2020-01-23 18:38:527171 std::vector<protocol::PageHandler*> page_handlers =
7172 protocol::PageHandler::EnabledForWebContents(this);
7173 for (auto* handler : page_handlers)
7174 handler->DidCloseJavaScriptDialog(success, user_input);
avi1c51fb62017-02-27 20:43:037175
7176 is_showing_javascript_dialog_ = false;
7177 is_showing_before_unload_dialog_ = false;
[email protected]beb440c2009-11-06 04:08:547178}
7179
[email protected]1ac10dca2013-08-20 20:47:047180bool WebContentsImpl::IsHidden() {
Francois Doraye6161152018-03-27 22:05:377181 return !IsBeingCaptured() && visibility_ != Visibility::VISIBLE;
[email protected]1ac10dca2013-08-20 20:47:047182}
7183
rob97250742015-12-10 17:45:157184int WebContentsImpl::GetOuterDelegateFrameTreeNodeId() {
lfg601233692017-03-06 22:45:447185 return node_.outer_contents_frame_tree_node_id();
lazyboy6ec48b2a2015-06-29 15:18:147186}
7187
ekaramadaeb3c5c2016-11-09 03:22:357188RenderWidgetHostImpl* WebContentsImpl::GetFullscreenRenderWidgetHost() const {
7189 return RenderWidgetHostImpl::FromID(fullscreen_widget_process_id_,
7190 fullscreen_widget_routing_id_);
7191}
7192
[email protected]b0936d22013-11-28 06:47:367193RenderFrameHostManager* WebContentsImpl::GetRenderManager() const {
[email protected]fa944cb82013-11-15 17:51:217194 return frame_tree_.root()->render_manager();
7195}
7196
[email protected]f8497342013-02-05 22:15:027197BrowserPluginGuest* WebContentsImpl::GetBrowserPluginGuest() const {
[email protected]7a846df2012-09-20 19:17:397198 return browser_plugin_guest_.get();
7199}
7200
Albert J. Wongc55cc64d2018-10-12 02:50:047201void WebContentsImpl::SetBrowserPluginGuest(
7202 std::unique_ptr<BrowserPluginGuest> guest) {
7203 DCHECK(!browser_plugin_guest_);
7204 DCHECK(guest);
7205 browser_plugin_guest_ = std::move(guest);
[email protected]738f57a2013-06-29 21:06:547206}
7207
Aidan Wolter52bcc12a2018-11-14 03:27:297208base::UnguessableToken WebContentsImpl::GetAudioGroupId() {
Aidan Wolter52bcc12a2018-11-14 03:27:297209 return GetAudioStreamFactory()->group_id();
7210}
7211
Miyoung Shin5709ab02020-02-19 06:50:417212const std::vector<blink::mojom::FaviconURLPtr>&
7213WebContentsImpl::GetFaviconURLs() {
Mounir Lamouri7b4a1f232019-12-23 16:32:107214 return favicon_urls_;
7215}
7216
Mugdha Lakhani0a0d7862020-07-29 09:58:457217// The Mac implementation of the next two methods is in
7218// web_contents_impl_mac.mm
7219#if !defined(OS_MACOSX)
7220
7221void WebContentsImpl::Resize(const gfx::Rect& new_bounds) {
7222#if defined(USE_AURA)
7223 aura::Window* window = GetNativeView();
7224 window->SetBounds(gfx::Rect(window->bounds().origin(), new_bounds.size()));
7225#elif defined(OS_ANDROID)
7226 content::RenderWidgetHostView* view = GetRenderWidgetHostView();
7227 if (view)
7228 view->SetBounds(new_bounds);
7229#endif
7230}
7231
7232gfx::Size WebContentsImpl::GetSize() {
7233#if defined(USE_AURA)
7234 aura::Window* window = GetNativeView();
7235 return window->bounds().size();
7236#elif defined(OS_ANDROID)
7237 ui::ViewAndroid* view_android = GetNativeView();
7238 return view_android->bounds().size();
7239#endif
7240}
7241
7242#endif // !defined(OS_MACOSX)
7243
[email protected]f8497342013-02-05 22:15:027244BrowserPluginEmbedder* WebContentsImpl::GetBrowserPluginEmbedder() const {
[email protected]7a846df2012-09-20 19:17:397245 return browser_plugin_embedder_.get();
7246}
[email protected]8ff00d72012-10-23 19:12:217247
fsamuel60b42282015-03-10 03:29:147248void WebContentsImpl::CreateBrowserPluginEmbedderIfNecessary() {
7249 if (browser_plugin_embedder_)
7250 return;
7251 browser_plugin_embedder_.reset(BrowserPluginEmbedder::Create(this));
7252}
7253
danakja0bce4c952020-05-12 20:40:287254gfx::Size WebContentsImpl::GetSizeForMainFrame() {
7255 if (delegate_) {
7256 // The delegate has a chance to specify a size independent of the UI.
7257 gfx::Size delegate_size = delegate_->GetSizeForNewRenderView(this);
7258 if (!delegate_size.IsEmpty())
7259 return delegate_size;
7260 }
7261
7262 // Device emulation, when enabled, can specify a size independent of the UI.
7263 if (!device_emulation_size_.IsEmpty())
7264 return device_emulation_size_;
7265
7266 return GetContainerBounds().size();
[email protected]dc0fd432013-08-27 15:29:217267}
7268
[email protected]ae7eeda2014-07-04 01:53:267269void WebContentsImpl::OnFrameRemoved(RenderFrameHost* render_frame_host) {
ericwilligersde386342016-10-19 02:35:097270 for (auto& observer : observers_)
7271 observer.FrameDeleted(render_frame_host);
[email protected]9b159a52013-10-03 17:24:557272}
7273
[email protected]222f5822014-02-05 23:40:497274void WebContentsImpl::OnPreferredSizeChanged(const gfx::Size& old_size) {
7275 if (!delegate_)
7276 return;
7277 const gfx::Size new_size = GetPreferredSize();
7278 if (new_size != old_size)
7279 delegate_->UpdatePreferredSize(this, new_size);
7280}
7281
Miyoung Shin072d4052020-07-13 07:52:447282std::unique_ptr<WebUIImpl> WebContentsImpl::CreateWebUI(
7283 RenderFrameHost* frame_host,
7284 const GURL& url) {
7285 std::unique_ptr<WebUIImpl> web_ui =
7286 std::make_unique<WebUIImpl>(this, frame_host);
Lei Zhangee3b63ad2018-06-06 23:25:317287 std::unique_ptr<WebUIController> controller(
avie865b1d2016-10-24 19:42:597288 WebUIControllerFactoryRegistry::GetInstance()
Lei Zhangee3b63ad2018-06-06 23:25:317289 ->CreateWebUIControllerForURL(web_ui.get(), url));
dbeama1b926a2015-08-31 23:17:517290 if (controller) {
Lei Zhangee3b63ad2018-06-06 23:25:317291 web_ui->SetController(std::move(controller));
dbeama1b926a2015-08-31 23:17:517292 return web_ui;
7293 }
7294
avie865b1d2016-10-24 19:42:597295 return nullptr;
dbeama1b926a2015-08-31 23:17:517296}
7297
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:417298FindRequestManager* WebContentsImpl::GetFindRequestManager() {
7299 for (auto* contents = this; contents;
paulmeyerfeafc2d2017-04-25 21:46:407300 contents = contents->GetOuterWebContents()) {
7301 if (contents->find_request_manager_)
7302 return contents->find_request_manager_.get();
7303 }
7304
7305 return nullptr;
7306}
7307
paulmeyerc0b762b2016-04-13 11:55:177308FindRequestManager* WebContentsImpl::GetOrCreateFindRequestManager() {
paulmeyerfeafc2d2017-04-25 21:46:407309 if (FindRequestManager* manager = GetFindRequestManager())
7310 return manager;
7311
Ehsan Karamad6beb2ea2018-11-25 18:15:137312 DCHECK(!browser_plugin_guest_ || GetOuterWebContents());
7313
paulmeyerfeafc2d2017-04-25 21:46:407314 // No existing FindRequestManager found, so one must be created.
7315 find_request_manager_.reset(new FindRequestManager(this));
7316
7317 // Concurrent find sessions must not overlap, so destroy any existing
7318 // FindRequestManagers in any inner WebContentses.
Lucas Furukawa Gadani2ec00c82018-12-14 15:53:167319 for (WebContents* contents : GetWebContentsAndAllInner()) {
7320 auto* web_contents_impl = static_cast<WebContentsImpl*>(contents);
7321 if (web_contents_impl == this)
paulmeyerfeafc2d2017-04-25 21:46:407322 continue;
Lucas Furukawa Gadani2ec00c82018-12-14 15:53:167323 if (web_contents_impl->find_request_manager_) {
7324 web_contents_impl->find_request_manager_->StopFinding(
7325 STOP_FIND_ACTION_CLEAR_SELECTION);
7326 web_contents_impl->find_request_manager_.release();
paulmeyerfeafc2d2017-04-25 21:46:407327 }
7328 }
paulmeyerc0b762b2016-04-13 11:55:177329
7330 return find_request_manager_.get();
7331}
7332
7333void WebContentsImpl::NotifyFindReply(int request_id,
7334 int number_of_matches,
7335 const gfx::Rect& selection_rect,
7336 int active_match_ordinal,
7337 bool final_update) {
Sebastien Marchandcf97ed52017-07-24 23:50:347338 if (delegate_ && !is_being_destroyed_ &&
Lukasz Anforowicze9ae3722017-09-29 17:37:387339 !GetMainFrame()->GetProcess()->FastShutdownStarted()) {
paulmeyerc0b762b2016-04-13 11:55:177340 delegate_->FindReply(this, request_id, number_of_matches, selection_rect,
7341 active_match_ordinal, final_update);
7342 }
7343}
7344
ortuno467e5792016-06-10 04:32:397345void WebContentsImpl::IncrementBluetoothConnectedDeviceCount() {
7346 // Trying to invalidate the tab state while being destroyed could result in a
7347 // use after free.
7348 if (IsBeingDestroyed()) {
7349 return;
7350 }
7351 // Notify for UI updates if the state changes.
7352 bluetooth_connected_device_count_++;
7353 if (bluetooth_connected_device_count_ == 1) {
Francois Doray66766a572019-12-17 20:56:497354 OnIsConnectedToBluetoothDeviceChanged(true);
ortuno467e5792016-06-10 04:32:397355 }
7356}
7357
7358void WebContentsImpl::DecrementBluetoothConnectedDeviceCount() {
7359 // Trying to invalidate the tab state while being destroyed could result in a
7360 // use after free.
7361 if (IsBeingDestroyed()) {
7362 return;
7363 }
7364 // Notify for UI updates if the state changes.
Lukasz Anforowicze9ae3722017-09-29 17:37:387365 DCHECK_NE(bluetooth_connected_device_count_, 0u);
ortuno467e5792016-06-10 04:32:397366 bluetooth_connected_device_count_--;
7367 if (bluetooth_connected_device_count_ == 0) {
Francois Doray66766a572019-12-17 20:56:497368 OnIsConnectedToBluetoothDeviceChanged(false);
7369 }
7370}
7371
7372void WebContentsImpl::OnIsConnectedToBluetoothDeviceChanged(
7373 bool is_connected_to_bluetooth_device) {
7374 NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB);
7375 for (auto& observer : observers_) {
7376 observer.OnIsConnectedToBluetoothDeviceChanged(
7377 is_connected_to_bluetooth_device);
ortuno467e5792016-06-10 04:32:397378 }
7379}
7380
Ovidio Henriquez76696f62020-07-08 03:06:597381void WebContentsImpl::IncrementBluetoothScanningSessionsCount() {
7382 // Trying to invalidate the tab state while being destroyed could result in a
7383 // use after free.
7384 if (IsBeingDestroyed())
7385 return;
7386
7387 // Notify for UI updates if the state changes.
7388 bluetooth_scanning_sessions_count_++;
7389 if (bluetooth_scanning_sessions_count_ == 1)
7390 NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB);
7391}
7392
7393void WebContentsImpl::DecrementBluetoothScanningSessionsCount() {
7394 // Trying to invalidate the tab state while being destroyed could result in a
7395 // use after free.
7396 if (IsBeingDestroyed())
7397 return;
7398
7399 DCHECK_NE(0u, bluetooth_scanning_sessions_count_);
7400 bluetooth_scanning_sessions_count_--;
7401 if (bluetooth_scanning_sessions_count_ == 0)
7402 NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB);
7403}
7404
Reilly Grant5e7c79b22019-04-09 17:26:207405void WebContentsImpl::IncrementSerialActiveFrameCount() {
7406 // Trying to invalidate the tab state while being destroyed could result in a
7407 // use after free.
7408 if (IsBeingDestroyed())
7409 return;
7410
7411 // Notify for UI updates if the state changes.
7412 serial_active_frame_count_++;
7413 if (serial_active_frame_count_ == 1)
7414 NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB);
7415}
7416
7417void WebContentsImpl::DecrementSerialActiveFrameCount() {
7418 // Trying to invalidate the tab state while being destroyed could result in a
7419 // use after free.
7420 if (IsBeingDestroyed())
7421 return;
7422
7423 // Notify for UI updates if the state changes.
7424 DCHECK_NE(0u, serial_active_frame_count_);
7425 serial_active_frame_count_--;
7426 if (serial_active_frame_count_ == 0)
7427 NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB);
7428}
7429
Matt Reynoldse8c6c1f2019-11-02 09:53:537430void WebContentsImpl::IncrementHidActiveFrameCount() {
7431 // Trying to invalidate the tab state while being destroyed could result in a
7432 // use after free.
7433 if (IsBeingDestroyed())
7434 return;
7435
7436 // Notify for UI updates if the active frame count transitions from zero to
7437 // non-zero.
7438 hid_active_frame_count_++;
7439 if (hid_active_frame_count_ == 1)
7440 NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB);
7441}
7442
7443void WebContentsImpl::DecrementHidActiveFrameCount() {
7444 // Trying to invalidate the tab state while being destroyed could result in a
7445 // use after free.
7446 if (IsBeingDestroyed())
7447 return;
7448
7449 // Notify for UI updates if the active frame count transitions from non-zero
7450 // to zero.
7451 DCHECK_NE(0u, hid_active_frame_count_);
7452 hid_active_frame_count_--;
7453 if (hid_active_frame_count_ == 0)
7454 NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB);
7455}
7456
Marijn Kruisselbrink29051042019-08-06 22:56:557457void WebContentsImpl::IncrementNativeFileSystemHandleCount() {
7458 // Trying to invalidate the tab state while being destroyed could result in a
7459 // use after free.
7460 if (IsBeingDestroyed())
7461 return;
7462
7463 // Notify for UI updates if the state changes. Need both TYPE_TAB and TYPE_URL
7464 // to update both the tab-level usage indicator and the usage indicator in the
7465 // omnibox.
7466 native_file_system_handle_count_++;
7467 if (native_file_system_handle_count_ == 1) {
7468 NotifyNavigationStateChanged(static_cast<content::InvalidateTypes>(
7469 INVALIDATE_TYPE_TAB | INVALIDATE_TYPE_URL));
7470 }
7471}
7472
7473void WebContentsImpl::DecrementNativeFileSystemHandleCount() {
7474 // Trying to invalidate the tab state while being destroyed could result in a
7475 // use after free.
7476 if (IsBeingDestroyed())
7477 return;
7478
7479 // Notify for UI updates if the state changes. Need both TYPE_TAB and TYPE_URL
7480 // to update both the tab-level usage indicator and the usage indicator in the
7481 // omnibox.
7482 DCHECK_NE(0u, native_file_system_handle_count_);
7483 native_file_system_handle_count_--;
7484 if (native_file_system_handle_count_ == 0) {
7485 NotifyNavigationStateChanged(static_cast<content::InvalidateTypes>(
7486 INVALIDATE_TYPE_TAB | INVALIDATE_TYPE_URL));
7487 }
7488}
7489
zqzhang8ac49002017-03-16 21:51:357490void WebContentsImpl::SetHasPersistentVideo(bool has_persistent_video) {
7491 if (has_persistent_video_ == has_persistent_video)
mlamouri5cd9ae82017-02-18 11:05:097492 return;
7493
zqzhang8ac49002017-03-16 21:51:357494 has_persistent_video_ = has_persistent_video;
mlamouri5cd9ae82017-02-18 11:05:097495 NotifyPreferencesChanged();
zqzhang8ac49002017-03-16 21:51:357496 media_web_contents_observer()->RequestPersistentVideo(has_persistent_video);
mlamouri5cd9ae82017-02-18 11:05:097497}
7498
Ian Vollick5d5c37f2019-06-19 22:50:577499void WebContentsImpl::SetSpatialNavigationDisabled(bool disabled) {
7500 if (is_spatial_navigation_disabled_ == disabled)
7501 return;
7502
7503 is_spatial_navigation_disabled_ = disabled;
7504 NotifyPreferencesChanged();
7505}
7506
paulmeyerfeafc2d2017-04-25 21:46:407507void WebContentsImpl::BrowserPluginGuestWillDetach() {
7508 WebContentsImpl* outermost = GetOutermostWebContents();
7509 if (this != outermost && ContainsOrIsFocusedWebContents())
7510 outermost->SetAsFocusedWebContentsIfNecessary();
7511}
7512
Becca Hughes112832e2019-06-11 17:19:027513PictureInPictureResult WebContentsImpl::EnterPictureInPicture(
Mounir Lamouri11e9ef42018-05-22 03:10:167514 const viz::SurfaceId& surface_id,
7515 const gfx::Size& natural_size) {
François Beaufortf09e4462019-01-18 14:34:517516 return delegate_
7517 ? delegate_->EnterPictureInPicture(this, surface_id, natural_size)
Becca Hughes112832e2019-06-11 17:19:027518 : PictureInPictureResult::kNotSupported;
Mounir Lamouri11e9ef42018-05-22 03:10:167519}
7520
Mounir Lamouri6d759e12018-05-16 20:01:307521void WebContentsImpl::ExitPictureInPicture() {
7522 if (delegate_)
7523 delegate_->ExitPictureInPicture();
7524}
7525
paulmeyerc0b762b2016-04-13 11:55:177526#if defined(OS_ANDROID)
7527void WebContentsImpl::NotifyFindMatchRectsReply(
7528 int version,
7529 const std::vector<gfx::RectF>& rects,
7530 const gfx::RectF& active_rect) {
7531 if (delegate_)
7532 delegate_->FindMatchRectsReply(this, version, rects, active_rect);
7533}
7534#endif
7535
[email protected]34ff1cfc2014-08-20 06:16:057536void WebContentsImpl::SetForceDisableOverscrollContent(bool force_disable) {
7537 force_disable_overscroll_content_ = force_disable;
7538 if (view_)
7539 view_->SetOverscrollControllerEnabled(CanOverscrollContent());
7540}
7541
Jianzhou Fengd8720c72018-01-18 03:06:307542bool WebContentsImpl::SetDeviceEmulationSize(const gfx::Size& new_size) {
7543 device_emulation_size_ = new_size;
7544 RenderWidgetHostView* rwhv = GetMainFrame()->GetView();
7545
7546 const gfx::Size current_size = rwhv->GetViewBounds().size();
7547 if (view_size_before_emulation_.IsEmpty())
7548 view_size_before_emulation_ = current_size;
7549
7550 if (current_size != new_size)
7551 rwhv->SetSize(new_size);
7552
7553 return current_size != new_size;
7554}
7555
7556void WebContentsImpl::ClearDeviceEmulationSize() {
7557 RenderWidgetHostView* rwhv = GetMainFrame()->GetView();
7558 // WebContentsView could get resized during emulation, which also resizes
7559 // RWHV. If it happens, assume user would like to keep using the size after
7560 // emulation.
7561 // TODO(jzfeng): Prohibit resizing RWHV through any other means (at least when
7562 // WebContentsView size changes).
Dmitry Gozmanc63707182018-03-06 00:09:397563 if (!view_size_before_emulation_.IsEmpty() && rwhv &&
7564 rwhv->GetViewBounds().size() == device_emulation_size_) {
Jianzhou Fengd8720c72018-01-18 03:06:307565 rwhv->SetSize(view_size_before_emulation_);
Dmitry Gozmanc63707182018-03-06 00:09:397566 }
Jianzhou Fengd8720c72018-01-18 03:06:307567 device_emulation_size_ = gfx::Size();
7568 view_size_before_emulation_ = gfx::Size();
7569}
7570
Max Morin5bc74f52018-05-09 07:00:217571ForwardingAudioStreamFactory* WebContentsImpl::GetAudioStreamFactory() {
7572 if (!audio_stream_factory_) {
7573 audio_stream_factory_.emplace(
7574 this,
Max Morinddebb972018-09-20 10:04:147575 // BrowserMainLoop::GetInstance() may be null in unit tests.
7576 BrowserMainLoop::GetInstance()
7577 ? static_cast<media::UserInputMonitorBase*>(
7578 BrowserMainLoop::GetInstance()->user_input_monitor())
7579 : nullptr,
Ken Rockot72964402019-12-06 10:40:517580 AudioStreamBrokerFactory::CreateImpl());
Max Morin5bc74f52018-05-09 07:00:217581 }
7582
7583 return &*audio_stream_factory_;
7584}
7585
dalecurtis88c240072015-12-09 02:11:187586void WebContentsImpl::MediaStartedPlaying(
billorr21c005d2016-11-17 03:57:047587 const WebContentsObserver::MediaPlayerInfo& media_info,
Mounir Lamouri0f5bdf52019-04-04 16:32:357588 const MediaPlayerId& id) {
billorr21c005d2016-11-17 03:57:047589 if (media_info.has_video)
7590 currently_playing_video_count_++;
7591
ericwilligersde386342016-10-19 02:35:097592 for (auto& observer : observers_)
billorr21c005d2016-11-17 03:57:047593 observer.MediaStartedPlaying(media_info, id);
dalecurtis88c240072015-12-09 02:11:187594}
7595
7596void WebContentsImpl::MediaStoppedPlaying(
billorr21c005d2016-11-17 03:57:047597 const WebContentsObserver::MediaPlayerInfo& media_info,
Mounir Lamouri0f5bdf52019-04-04 16:32:357598 const MediaPlayerId& id,
Becca Hughes220bfc102017-11-14 18:24:077599 WebContentsObserver::MediaStoppedReason reason) {
billorr21c005d2016-11-17 03:57:047600 if (media_info.has_video)
7601 currently_playing_video_count_--;
7602
ericwilligersde386342016-10-19 02:35:097603 for (auto& observer : observers_)
Becca Hughes220bfc102017-11-14 18:24:077604 observer.MediaStoppedPlaying(media_info, id, reason);
billorr21c005d2016-11-17 03:57:047605}
7606
Mounir Lamouri0f5bdf52019-04-04 16:32:357607void WebContentsImpl::MediaResized(const gfx::Size& size,
7608 const MediaPlayerId& id) {
peconn257951522017-06-09 18:24:597609 cached_video_sizes_[id] = size;
7610
7611 for (auto& observer : observers_)
7612 observer.MediaResized(size, id);
7613}
7614
Peter E Conneccb34c22017-09-08 09:37:587615void WebContentsImpl::MediaEffectivelyFullscreenChanged(bool is_fullscreen) {
7616 for (auto& observer : observers_)
7617 observer.MediaEffectivelyFullscreenChanged(is_fullscreen);
7618}
7619
Peter Conn31c726a2017-08-17 11:07:347620base::Optional<gfx::Size> WebContentsImpl::GetFullscreenVideoSize() {
Mounir Lamouri0f5bdf52019-04-04 16:32:357621 base::Optional<MediaPlayerId> id =
Peter Conn31c726a2017-08-17 11:07:347622 media_web_contents_observer_->GetFullscreenVideoMediaPlayerId();
Jan Wilken Dörrie531be7ca2019-06-07 10:05:577623 if (id && base::Contains(cached_video_sizes_, id.value()))
Peter Conn31c726a2017-08-17 11:07:347624 return base::Optional<gfx::Size>(cached_video_sizes_[id.value()]);
Lei Zhang6410ec1a2019-01-31 21:38:587625 return base::nullopt;
peconn257951522017-06-09 18:24:597626}
7627
billorr21c005d2016-11-17 03:57:047628int WebContentsImpl::GetCurrentlyPlayingVideoCount() {
7629 return currently_playing_video_count_;
dalecurtis88c240072015-12-09 02:11:187630}
7631
Raymond Toye413650c2018-08-31 00:12:477632void WebContentsImpl::AudioContextPlaybackStarted(RenderFrameHost* host,
7633 int context_id) {
7634 WebContentsObserver::AudioContextId audio_context_id(host, context_id);
7635 for (auto& observer : observers_)
7636 observer.AudioContextPlaybackStarted(audio_context_id);
7637}
7638
7639void WebContentsImpl::AudioContextPlaybackStopped(RenderFrameHost* host,
7640 int context_id) {
7641 WebContentsObserver::AudioContextId audio_context_id(host, context_id);
7642 for (auto& observer : observers_)
7643 observer.AudioContextPlaybackStopped(audio_context_id);
7644}
7645
Chris Hamilton16ca64a2020-05-14 23:13:177646void WebContentsImpl::OnFrameAudioStateChanged(RenderFrameHost* host,
7647 bool is_audible) {
7648 for (auto& observer : observers_)
7649 observer.OnFrameAudioStateChanged(host, is_audible);
7650}
7651
evliu7f736f02019-11-07 17:43:507652media::MediaMetricsProvider::RecordAggregateWatchTimeCallback
7653WebContentsImpl::GetRecordAggregateWatchTimeCallback() {
Becca Hughes53652442020-03-17 15:35:427654 if (!delegate_ || !delegate_->GetDelegateWeakPtr())
7655 return base::DoNothing();
7656
evliu7f736f02019-11-07 17:43:507657 return base::BindRepeating(
Becca Hughes53652442020-03-17 15:35:427658 [](base::WeakPtr<WebContentsDelegate> delegate, GURL last_committed_url,
7659 base::TimeDelta total_watch_time, base::TimeDelta time_stamp,
7660 bool has_video, bool has_audio) {
evliua283c9e2019-11-08 22:57:227661 content::MediaPlayerWatchTime watch_time(
7662 last_committed_url, last_committed_url.GetOrigin(),
7663 total_watch_time, time_stamp, has_video, has_audio);
evliu7f736f02019-11-07 17:43:507664
7665 // Save the watch time if the delegate is still alive.
7666 if (delegate)
7667 delegate->MediaWatchTimeChanged(watch_time);
7668 },
Becca Hughes53652442020-03-17 15:35:427669 delegate_->GetDelegateWeakPtr(), GetMainFrameLastCommittedURL());
evliu7f736f02019-11-07 17:43:507670}
7671
Ehsan Karamad1c631762019-05-13 23:04:017672RenderFrameHostImpl* WebContentsImpl::GetMainFrameForInnerDelegate(
7673 FrameTreeNode* frame_tree_node) {
7674 if (auto* web_contents = node_.GetInnerWebContentsInFrame(frame_tree_node))
7675 return web_contents->GetMainFrame();
7676 return nullptr;
7677}
7678
John Budorickee1c9512019-10-17 15:32:197679bool WebContentsImpl::IsFrameLowPriority(
7680 const RenderFrameHost* render_frame_host) {
7681 if (!delegate_)
7682 return false;
7683 return delegate_->IsFrameLowPriority(this, render_frame_host);
7684}
7685
Roger Tawa03ffdbd2020-01-16 18:12:547686void WebContentsImpl::IsClipboardPasteAllowed(
7687 const GURL& url,
7688 const ui::ClipboardFormatType& data_type,
7689 const std::string& data,
7690 IsClipboardPasteAllowedCallback callback) {
Dominique Fauteux-Chapleau99244382020-07-08 20:37:007691 ++suppress_unresponsive_renderer_count_;
Roger Tawa03ffdbd2020-01-16 18:12:547692 GetContentClient()->browser()->IsClipboardPasteAllowed(
Dominique Fauteux-Chapleau99244382020-07-08 20:37:007693 this, url, data_type, data,
7694 base::BindOnce(&WebContentsImpl::IsClipboardPasteAllowedWrapperCallback,
7695 weak_factory_.GetWeakPtr(), std::move(callback)));
7696}
7697
7698void WebContentsImpl::IsClipboardPasteAllowedWrapperCallback(
7699 IsClipboardPasteAllowedCallback callback,
7700 ClipboardPasteAllowed allowed) {
7701 std::move(callback).Run(allowed);
7702 --suppress_unresponsive_renderer_count_;
Roger Tawa03ffdbd2020-01-16 18:12:547703}
7704
Lan Weif81c6e7c2020-02-12 16:46:097705bool WebContentsImpl::HasSeenRecentScreenOrientationChange() {
7706 static constexpr base::TimeDelta kMaxInterval =
7707 base::TimeDelta::FromSeconds(1);
7708 base::TimeDelta delta =
7709 ui::EventTimeForNow() - last_screen_orientation_change_time_;
7710 // Return whether there is a screen orientation change happened in the recent
7711 // 1 second.
7712 return delta <= kMaxInterval;
7713}
7714
7715void WebContentsImpl::DidChangeScreenOrientation() {
7716 last_screen_orientation_change_time_ = ui::EventTimeForNow();
7717}
7718
Miyoung Shin8f2e1832020-04-27 09:00:027719bool WebContentsImpl::ShowPopupMenu(
Miyoung Shin4d947d22020-04-24 04:48:337720 RenderFrameHostImpl* render_frame_host,
Miyoung Shin8f2e1832020-04-27 09:00:027721 mojo::PendingRemote<blink::mojom::PopupMenuClient>* popup_client,
Miyoung Shin4d947d22020-04-24 04:48:337722 const gfx::Rect& bounds,
7723 int32_t item_height,
7724 double font_size,
7725 int32_t selected_item,
7726 std::vector<blink::mojom::MenuItemPtr>* menu_items,
7727 bool right_aligned,
7728 bool allow_multiple_selection) {
7729 for (auto& observer : observers_) {
Miyoung Shin8f2e1832020-04-27 09:00:027730 if (observer.ShowPopupMenu(render_frame_host, popup_client, bounds,
7731 item_height, font_size, selected_item,
7732 menu_items, right_aligned,
7733 allow_multiple_selection)) {
Miyoung Shin4d947d22020-04-24 04:48:337734 return true;
7735 }
7736 }
7737 return false;
7738}
7739
Francois Doraye6161152018-03-27 22:05:377740void WebContentsImpl::UpdateWebContentsVisibility(Visibility visibility) {
Francois Doray3f2afbd2018-04-06 19:18:187741 // Occlusion is disabled when |features::kWebContentsOcclusion| is disabled
7742 // (for power and speed impact assessment) or when
7743 // |switches::kDisableBackgroundingOccludedWindowsForTesting| is specified on
7744 // the command line (to avoid flakiness in browser tests).
7745 const bool occlusion_is_disabled =
7746 !base::FeatureList::IsEnabled(features::kWebContentsOcclusion) ||
Francois Doraye6161152018-03-27 22:05:377747 base::CommandLine::ForCurrentProcess()->HasSwitch(
7748 switches::kDisableBackgroundingOccludedWindowsForTesting);
7749 if (occlusion_is_disabled && visibility == Visibility::OCCLUDED)
7750 visibility = Visibility::VISIBLE;
7751
tapted65ff2ea72016-03-01 23:39:007752 if (!did_first_set_visible_) {
Francois Doraye6161152018-03-27 22:05:377753 if (visibility == Visibility::VISIBLE) {
7754 // A WebContents created with CreateParams::initially_hidden = false
7755 // starts with GetVisibility() == Visibility::VISIBLE even though it is
7756 // not really visible. Call WasShown() when it becomes visible for real as
7757 // the page load mechanism and some WebContentsObserver rely on that.
tapted65ff2ea72016-03-01 23:39:007758 WasShown();
Francois Dorayfe4a1772018-02-17 04:17:097759 did_first_set_visible_ = true;
tapted65ff2ea72016-03-01 23:39:007760 }
Francois Doraye6161152018-03-27 22:05:377761
7762 // Trust the initial visibility of the WebContents and do not switch it to
7763 // HIDDEN or OCCLUDED before it becomes VISIBLE for real. Doing so would
7764 // result in destroying resources that would immediately be recreated (e.g.
7765 // UpdateWebContents(HIDDEN) can be called when a WebContents is added to a
7766 // hidden window that is about to be shown).
7767
tapted65ff2ea72016-03-01 23:39:007768 return;
7769 }
Francois Doraye6161152018-03-27 22:05:377770
7771 if (visibility == visibility_)
tapted65ff2ea72016-03-01 23:39:007772 return;
7773
Collin Baker989e0882019-11-01 01:27:177774 UpdateVisibilityAndNotifyPageAndView(visibility);
tapted65ff2ea72016-03-01 23:39:007775}
7776
aelias5252baa2016-04-10 01:18:027777void WebContentsImpl::UpdateOverridingUserAgent() {
mlamouri5cd9ae82017-02-18 11:05:097778 NotifyPreferencesChanged();
aelias5252baa2016-04-10 01:18:027779}
7780
avid53461d2016-02-25 17:15:047781void WebContentsImpl::SetJavaScriptDialogManagerForTesting(
7782 JavaScriptDialogManager* dialog_manager) {
7783 dialog_manager_ = dialog_manager;
7784}
7785
Julie Jeongeun Kim7d0986172019-12-04 03:17:257786void WebContentsImpl::RemoveReceiverSet(const std::string& interface_name) {
7787 auto it = receiver_sets_.find(interface_name);
7788 if (it != receiver_sets_.end())
7789 receiver_sets_.erase(it);
rockotf62002a2016-09-15 00:08:597790}
7791
nzolghadrd87a308d2016-12-07 15:45:567792bool WebContentsImpl::AddDomainInfoToRapporSample(rappor::Sample* sample) {
nzolghadrbbffc6c2017-05-25 17:56:397793 // Here we associate this metric to the main frame URL regardless of what
7794 // caused it.
nzolghadrd87a308d2016-12-07 15:45:567795 sample->SetStringField("Domain", ::rappor::GetDomainAndRegistrySampleFromGURL(
7796 GetLastCommittedURL()));
nzolghadrd87a308d2016-12-07 15:45:567797 return true;
7798}
7799
falken52a56e32016-12-08 05:02:407800void WebContentsImpl::ShowInsecureLocalhostWarningIfNeeded() {
7801 bool allow_localhost = base::CommandLine::ForCurrentProcess()->HasSwitch(
7802 switches::kAllowInsecureLocalhost);
7803 if (!allow_localhost)
7804 return;
7805
7806 content::NavigationEntry* entry = GetController().GetLastCommittedEntry();
Rob Wuf79b3ba2018-01-14 01:54:317807 if (!entry || !net::IsLocalhost(entry->GetURL()))
falken52a56e32016-12-08 05:02:407808 return;
7809
7810 content::SSLStatus ssl_status = entry->GetSSL();
Matt Mueller936511442019-09-03 18:15:127811 if (!net::IsCertStatusError(ssl_status.cert_status))
falken52a56e32016-12-08 05:02:407812 return;
7813
7814 GetMainFrame()->AddMessageToConsole(
Abhijeet Kandalkara5928a0a2019-03-13 05:04:427815 blink::mojom::ConsoleMessageLevel::kWarning,
falken52a56e32016-12-08 05:02:407816 base::StringPrintf("This site does not have a valid SSL "
7817 "certificate! Without SSL, your site's and "
7818 "visitors' data is vulnerable to theft and "
7819 "tampering. Get a valid SSL certificate before"
7820 " releasing your website to the public."));
7821}
7822
ekaramadf6750aa2017-06-06 18:29:427823bool WebContentsImpl::IsShowingContextMenuOnPage() const {
7824 return showing_context_menu_;
7825}
7826
Min Qina904f3302018-02-13 23:33:347827download::DownloadUrlParameters::RequestHeadersType
7828WebContentsImpl::ParseDownloadHeaders(const std::string& headers) {
7829 download::DownloadUrlParameters::RequestHeadersType request_headers;
Megan Jablonski2f6a4c52017-07-10 23:01:257830 for (const base::StringPiece& key_value : base::SplitStringPiece(
7831 headers, "\r\n", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY)) {
7832 std::vector<std::string> pair = base::SplitString(
7833 key_value, ":", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
7834 if (2ul == pair.size())
7835 request_headers.push_back(make_pair(pair[0], pair[1]));
7836 }
7837 return request_headers;
7838}
7839
lukasza6f8ac622017-06-06 03:10:207840void WebContentsImpl::SetOpenerForNewContents(FrameTreeNode* opener,
7841 bool opener_suppressed) {
7842 if (opener) {
7843 FrameTreeNode* new_root = GetFrameTree()->root();
7844
7845 // For the "original opener", track the opener's main frame instead, because
7846 // if the opener is a subframe, the opener tracking could be easily bypassed
7847 // by spawning from a subframe and deleting the subframe.
7848 // https://crbug.com/705316
7849 new_root->SetOriginalOpener(opener->frame_tree()->root());
7850
7851 if (!opener_suppressed) {
7852 new_root->SetOpener(opener);
7853 created_with_opener_ = true;
7854 }
7855 }
7856}
7857
Mounir Lamouri0f5bdf52019-04-04 16:32:357858void WebContentsImpl::MediaMutedStatusChanged(const MediaPlayerId& id,
7859 bool muted) {
Becca Hughes9f6fd4b82017-06-15 10:01:407860 for (auto& observer : observers_)
7861 observer.MediaMutedStatusChanged(id, muted);
7862}
7863
EhsanK2075c7e2017-08-21 02:42:397864void WebContentsImpl::SetVisibilityForChildViews(bool visible) {
7865 GetMainFrame()->SetVisibilityForChildViews(visible);
7866}
7867
Alison Maher7f366dc62020-03-03 19:46:007868void WebContentsImpl::OnNativeThemeUpdated(ui::NativeTheme* observed_theme) {
7869 DCHECK(native_theme_observer_.IsObserving(observed_theme));
7870
7871 bool using_dark_colors = observed_theme->ShouldUseDarkColors();
7872 ui::NativeTheme::PreferredColorScheme preferred_color_scheme =
7873 observed_theme->GetPreferredColorScheme();
7874 bool preferences_changed = false;
7875
7876 if (using_dark_colors_ != using_dark_colors) {
7877 using_dark_colors_ = using_dark_colors;
7878 preferences_changed = true;
7879 }
7880 if (preferred_color_scheme_ != preferred_color_scheme) {
7881 preferred_color_scheme_ = preferred_color_scheme;
7882 preferences_changed = true;
7883 }
7884
7885 if (preferences_changed)
7886 NotifyPreferencesChanged();
7887}
7888
Dave Tapuska58a099e2020-06-08 21:48:407889blink::mojom::FrameWidgetInputHandler*
7890WebContentsImpl::GetFocusedFrameWidgetInputHandler() {
7891 auto* focused_render_widget_host =
7892 GetFocusedRenderWidgetHost(GetMainFrame()->GetRenderWidgetHost());
7893 if (!focused_render_widget_host)
Dave Tapuskae782bea2019-07-09 16:21:517894 return nullptr;
Dave Tapuska58a099e2020-06-08 21:48:407895 return focused_render_widget_host->GetFrameWidgetInputHandler();
Dave Tapuskae782bea2019-07-09 16:21:517896}
7897
Alexander Timine3ec4192020-04-20 16:39:407898ukm::SourceId WebContentsImpl::GetCurrentPageUkmSourceId() {
7899 return GetMainFrame()->GetPageUkmSourceId();
7900}
7901
Yuzu Saijo232076f2020-05-29 07:14:027902std::set<RenderViewHostImpl*>
7903WebContentsImpl::GetRenderViewHostsIncludingBackForwardCached() {
7904 std::set<RenderViewHostImpl*> render_view_hosts;
7905
7906 // Add RenderViewHostImpls outside of BackForwardCache.
7907 for (auto& render_view_host : frame_tree_.render_view_hosts()) {
7908 render_view_hosts.insert(render_view_host.second);
7909 }
7910
7911 // Add RenderViewHostImpls in BackForwardCache.
7912 const auto& entries = GetController().GetBackForwardCache().GetEntries();
7913 for (const auto& entry : entries) {
7914 std::set<RenderViewHostImpl*> bfcached_hosts = entry->render_view_hosts;
7915 render_view_hosts.insert(bfcached_hosts.begin(), bfcached_hosts.end());
7916 }
7917
7918 return render_view_hosts;
7919}
7920
Sreeja Kamishettybe0ccd6b2020-06-09 11:52:507921void WebContentsImpl::RenderFrameHostStateChanged(
7922 RenderFrameHost* render_frame_host,
7923 LifecycleState old_state,
7924 LifecycleState new_state) {
7925 if (render_frame_host->GetParent())
7926 return;
7927
7928 if (old_state == LifecycleState::kActive &&
7929 new_state != LifecycleState::kActive) {
7930 // TODO(sreejakshetty): Remove this reset when ColorChooser becomes
7931 // per-frame.
7932 // Close the color chooser popup when RenderFrameHost changes state from
7933 // kActive.
7934 color_chooser_.reset();
7935 }
7936}
7937
[email protected]8ff00d72012-10-23 19:12:217938} // namespace content