blob: 2f09301bc07434db8c82bdc23b71c9c0f0df6769 [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() {
Peter Marshall2dc74e92020-07-29 20:02:302388 blink::mojom::RendererPreferences renderer_preferences = GetRendererPrefs();
Bruce Long1e3e1f542019-10-16 17:56:282389 RenderViewHostImpl::GetPlatformSpecificPrefs(&renderer_preferences);
2390 SendPageMessage(
2391 new PageMsg_SetRendererPrefs(MSG_ROUTING_NONE, renderer_preferences));
2392}
2393
Alexander Timin1cc31f42020-05-12 16:26:012394void WebContentsImpl::OnCookiesAccessed(NavigationHandle* navigation,
2395 const CookieAccessDetails& details) {
Josh Karlindaba39322019-07-17 23:24:332396 for (auto& observer : observers_) {
Alexander Timin1cc31f42020-05-12 16:26:012397 observer.OnCookiesAccessed(navigation, details);
2398 }
2399}
2400
2401void WebContentsImpl::OnCookiesAccessed(RenderFrameHostImpl* rfh,
2402 const CookieAccessDetails& details) {
2403 for (auto& observer : observers_) {
2404 observer.OnCookiesAccessed(rfh, details);
Josh Karlindaba39322019-07-17 23:24:332405 }
2406}
2407
[email protected]b172aee2012-04-10 17:05:262408void WebContentsImpl::Stop() {
dcheng57e39e22016-01-21 00:25:382409 for (FrameTreeNode* node : frame_tree_.Nodes())
2410 node->StopLoading();
ericwilligersde386342016-10-19 02:35:092411 for (auto& observer : observers_)
2412 observer.NavigationStopped();
[email protected]0bfbf882011-12-22 18:19:272413}
2414
Francois Doray47f759d2018-06-11 18:13:512415void WebContentsImpl::SetPageFrozen(bool frozen) {
2416 // A visible page is never frozen.
2417 DCHECK_NE(Visibility::VISIBLE, GetVisibility());
2418
Yuzu Saijo4fa1e9a2020-05-20 02:53:262419 for (auto& entry : frame_tree_.render_view_hosts()) {
2420 entry.second->SetIsFrozen(frozen);
2421 }
Fadi Meawada6573e02018-03-10 00:52:112422}
2423
erikchen6c7df7f7a2018-05-03 18:13:592424std::unique_ptr<WebContents> WebContentsImpl::Clone() {
[email protected]14392a52012-05-02 20:28:442425 // We use our current SiteInstance since the cloned entry will use it anyway.
alexmose201c7cd2015-06-10 17:14:212426 // We pass our own opener so that the cloned page can access it if it was set
[email protected]ed245db2012-12-18 08:00:452427 // before.
[email protected]54944cde2012-12-09 09:24:592428 CreateParams create_params(GetBrowserContext(), GetSiteInstance());
Ian Clelland5cbaaf82017-11-27 22:00:032429 FrameTreeNode* opener = frame_tree_.root()->opener();
2430 RenderFrameHostImpl* opener_rfh = nullptr;
2431 if (opener)
2432 opener_rfh = opener->current_frame_host();
erikchen6c7df7f7a2018-05-03 18:13:592433 std::unique_ptr<WebContentsImpl> tc =
erikchen50735fd2018-05-05 15:08:332434 CreateWithOpener(create_params, opener_rfh);
Lucas Furukawa Gadani5553a152019-01-08 18:55:572435 tc->GetController().CopyStateFrom(&controller_, true);
ericwilligersde386342016-10-19 02:35:092436 for (auto& observer : observers_)
erikchen6c7df7f7a2018-05-03 18:13:592437 observer.DidCloneToNewWebContents(this, tc.get());
[email protected]0bfbf882011-12-22 18:19:272438 return tc;
2439}
2440
[email protected]14392a52012-05-02 20:28:442441void WebContentsImpl::Observe(int type,
[email protected]8ff00d72012-10-23 19:12:212442 const NotificationSource& source,
2443 const NotificationDetails& details) {
[email protected]14392a52012-05-02 20:28:442444 switch (type) {
[email protected]8ff00d72012-10-23 19:12:212445 case NOTIFICATION_RENDER_WIDGET_HOST_DESTROYED: {
2446 RenderWidgetHost* host = Source<RenderWidgetHost>(source).ptr();
[email protected]0b431992014-06-24 00:08:032447 RenderWidgetHostView* view = host->GetView();
2448 if (view == GetFullscreenRenderWidgetHostView()) {
2449 // We cannot just call view_->RestoreFocus() here. On some platforms,
2450 // attempting to focus the currently-invisible WebContentsView will be
2451 // flat-out ignored. Therefore, this boolean is used to track whether
2452 // we will request focus after the fullscreen widget has been
2453 // destroyed.
2454 fullscreen_widget_had_focus_at_shutdown_ = (view && view->HasFocus());
2455 } else {
alexmosc2a8cec2016-05-23 22:19:532456 for (auto i = pending_widget_views_.begin();
[email protected]0b431992014-06-24 00:08:032457 i != pending_widget_views_.end(); ++i) {
2458 if (host->GetView() == i->second) {
2459 pending_widget_views_.erase(i);
2460 break;
2461 }
[email protected]bafe6cd2012-05-23 23:09:502462 }
2463 }
2464 break;
2465 }
[email protected]14392a52012-05-02 20:28:442466 default:
2467 NOTREACHED();
2468 }
2469}
2470
[email protected]ec6c05f2013-10-23 18:41:572471WebContents* WebContentsImpl::GetWebContents() {
2472 return this;
2473}
2474
[email protected]54944cde2012-12-09 09:24:592475void WebContentsImpl::Init(const WebContents::CreateParams& params) {
[email protected]fa944cb82013-11-15 17:51:212476 // This is set before initializing the render manager since
[email protected]b0936d22013-11-28 06:47:362477 // RenderFrameHostManager::Init calls back into us via its delegate to ask if
[email protected]fa944cb82013-11-15 17:51:212478 // it should be hidden.
Francois Doraye6161152018-03-27 22:05:372479 visibility_ =
2480 params.initially_hidden ? Visibility::HIDDEN : Visibility::VISIBLE;
[email protected]d6fa88f2013-10-18 16:00:432481
Sebastien Marchand28cf23182018-06-20 02:39:352482 if (!params.last_active_time.is_null())
2483 last_active_time_ = params.last_active_time;
2484
dcheng3ce04b62015-10-26 23:30:552485 scoped_refptr<SiteInstance> site_instance = params.site_instance;
2486 if (!site_instance)
2487 site_instance = SiteInstance::Create(params.browser_context);
Lukasz Anforowicz3caa8372018-06-05 17:22:072488 if (params.desired_renderer_state == CreateParams::kNoRendererProcess) {
2489 static_cast<SiteInstanceImpl*>(site_instance.get())
2490 ->PreventAssociationWithSpareProcess();
2491 }
dcheng3ce04b62015-10-26 23:30:552492
Fergal Dalycb0c1fdf2020-03-16 07:13:492493 GetRenderManager()->InitRoot(site_instance.get(),
2494 params.renderer_initiated_creation);
lukasza464d8692016-02-22 19:26:322495
lukasza5140a412016-09-15 21:12:302496 // blink::FrameTree::setName always keeps |unique_name| empty in case of a
2497 // main frame - let's do the same thing here.
2498 std::string unique_name;
lukasza464d8692016-02-22 19:26:322499 frame_tree_.root()->SetFrameName(params.main_frame_name, unique_name);
[email protected]d1198fd2012-08-13 22:50:192500
[email protected]fc2b46b2014-05-03 16:33:452501 WebContentsViewDelegate* delegate =
2502 GetContentClient()->browser()->GetWebContentsViewDelegate(this);
2503
W. James MacLean2539adb32019-12-13 00:40:442504 if (browser_plugin_guest_) {
sky52a39e342016-12-17 17:23:222505 view_.reset(new WebContentsViewChildFrame(
2506 this, delegate, &render_view_host_delegate_view_));
2507 } else {
2508 view_.reset(CreateWebContentsView(this, delegate,
2509 &render_view_host_delegate_view_));
[email protected]d1198fd2012-08-13 22:50:192510 }
[email protected]fc2b46b2014-05-03 16:33:452511 CHECK(render_view_host_delegate_view_);
[email protected]d1198fd2012-08-13 22:50:192512 CHECK(view_.get());
2513
danakjfc5184932019-09-12 18:08:322514 view_->CreateView(params.context);
[email protected]d1198fd2012-08-13 22:50:192515
brettw4b461082016-11-19 18:55:162516#if BUILDFLAG(ENABLE_PLUGINS)
Yuzu Saijoa11069a2020-04-22 10:56:552517 plugin_content_origin_allowlist_.reset(
2518 new PluginContentOriginAllowlist(this));
tommyclieb25b2a2014-11-03 19:45:092519#endif
2520
[email protected]d1198fd2012-08-13 22:50:192521 registrar_.Add(this,
[email protected]8ff00d72012-10-23 19:12:212522 NOTIFICATION_RENDER_WIDGET_HOST_DESTROYED,
2523 NotificationService::AllBrowserContextsAndSources());
[email protected]f28ef9a32014-05-12 16:36:102524
leon.hane4db177a2017-02-07 14:19:162525 screen_orientation_provider_.reset(new ScreenOrientationProvider(this));
[email protected]01f83f92014-06-17 14:41:542526
mfomitchev2b8b066a2016-01-28 19:23:152527#if defined(OS_ANDROID)
Oksana Zhuravlova0b634332019-10-28 23:04:222528 DateTimeChooserAndroid::CreateForWebContents(this);
[email protected]583418cc2013-01-17 14:01:102529#endif
fsamuel7310a4262014-12-05 05:06:442530
2531 // BrowserPluginGuest::Init needs to be called after this WebContents has
W. James MacLean6e78da342019-12-13 15:13:232532 // a RenderWidgetHostViewChildFrame. That is, |view_->CreateView| above.
fsamuel7310a4262014-12-05 05:06:442533 if (browser_plugin_guest_)
2534 browser_plugin_guest_->Init();
nickf9acfbe2014-12-23 19:12:372535
2536 for (size_t i = 0; i < g_created_callbacks.Get().size(); i++)
2537 g_created_callbacks.Get().at(i).Run(this);
naskob21fe4872015-02-24 14:15:562538
2539 // If the WebContents creation was renderer-initiated, it means that the
2540 // corresponding RenderView and main RenderFrame have already been created.
2541 // Ensure observers are notified about this.
2542 if (params.renderer_initiated_creation) {
Fergal Dalybb2d6592020-06-02 05:02:332543 GetRenderViewHost()->GetWidget()->set_renderer_initialized(true);
Alex Moshchuk27caae82017-09-11 23:11:182544 GetRenderViewHost()->DispatchRenderViewCreated();
naskob21fe4872015-02-24 14:15:562545 GetRenderManager()->current_frame_host()->SetRenderFrameCreated(true);
2546 }
naskof5940b9f2015-03-02 23:04:052547
lof84501da082016-05-23 21:22:542548 // Create the renderer process in advance if requested.
Lukasz Anforowicz3caa8372018-06-05 17:22:072549 if (params.desired_renderer_state ==
2550 CreateParams::kInitializeAndWarmupRendererProcess) {
lof84501da082016-05-23 21:22:542551 if (!GetRenderManager()->current_frame_host()->IsRenderFrameLive()) {
2552 GetRenderManager()->InitRenderView(GetRenderViewHost(), nullptr);
2553 }
2554 }
2555
naskof5940b9f2015-03-02 23:04:052556 // Ensure that observers are notified of the creation of this WebContents's
2557 // main RenderFrameHost. It must be done here for main frames, since the
2558 // NotifySwappedFromRenderManager expects view_ to already be created and that
2559 // happens after RenderFrameHostManager::Init.
2560 NotifySwappedFromRenderManager(
2561 nullptr, GetRenderManager()->current_frame_host(), true);
Albert J. Wonge76bf0b82019-09-27 07:47:062562
2563 // For WebContents that are never shown, do critical initialization here which
2564 // would normally only happen when the WebContents is shown.
2565 if (params.is_never_visible) {
2566 // This has just been created so there can only be one frame. Thus it is
2567 // safe to initialize the root.
2568 GetMainFrame()->Init();
2569 }
[email protected]d1198fd2012-08-13 22:50:192570}
2571
[email protected]7fff43e2013-05-21 20:21:102572void WebContentsImpl::OnWebContentsDestroyed(WebContentsImpl* web_contents) {
2573 RemoveDestructionObserver(web_contents);
2574
[email protected]ceee8cd2013-03-08 04:59:512575 // Clear a pending contents that has been closed before being shown.
alexmosc2a8cec2016-05-23 22:19:532576 for (auto iter = pending_contents_.begin(); iter != pending_contents_.end();
[email protected]ceee8cd2013-03-08 04:59:512577 ++iter) {
Joel Hockey8c2011b22020-04-30 05:07:192578 if (iter->second.contents.get() != web_contents)
[email protected]ceee8cd2013-03-08 04:59:512579 continue;
erikchenbee5c9622018-04-27 19:30:252580
2581 // Someone else has deleted the WebContents. That should never happen!
2582 // TODO(erikchen): Fix semantics here. https://crbug.com/832879.
Joel Hockey8c2011b22020-04-30 05:07:192583 iter->second.contents.release();
[email protected]ceee8cd2013-03-08 04:59:512584 pending_contents_.erase(iter);
[email protected]ceee8cd2013-03-08 04:59:512585 return;
2586 }
2587 NOTREACHED();
[email protected]14392a52012-05-02 20:28:442588}
2589
[email protected]7fff43e2013-05-21 20:21:102590void WebContentsImpl::AddDestructionObserver(WebContentsImpl* web_contents) {
Jan Wilken Dörrie73c901e2019-06-12 09:02:322591 if (!base::Contains(destruction_observers_, web_contents)) {
[email protected]7fff43e2013-05-21 20:21:102592 destruction_observers_[web_contents] =
Jeremy Roman04f27c372017-10-27 15:20:552593 std::make_unique<DestructionObserver>(this, web_contents);
[email protected]7fff43e2013-05-21 20:21:102594 }
2595}
2596
2597void WebContentsImpl::RemoveDestructionObserver(WebContentsImpl* web_contents) {
avia8cf81982016-08-12 20:28:462598 destruction_observers_.erase(web_contents);
[email protected]7fff43e2013-05-21 20:21:102599}
2600
[email protected]b172aee2012-04-10 17:05:262601void WebContentsImpl::AddObserver(WebContentsObserver* observer) {
[email protected]be1f56ab2011-12-22 06:55:312602 observers_.AddObserver(observer);
2603}
2604
[email protected]b172aee2012-04-10 17:05:262605void WebContentsImpl::RemoveObserver(WebContentsObserver* observer) {
[email protected]be1f56ab2011-12-22 06:55:312606 observers_.RemoveObserver(observer);
2607}
2608
[email protected]948481d2014-06-11 18:32:222609std::set<RenderWidgetHostView*>
2610WebContentsImpl::GetRenderWidgetHostViewsInTree() {
2611 std::set<RenderWidgetHostView*> set;
Carlos IL07f282f2020-05-15 22:58:292612 for (RenderFrameHost* rfh : GetAllFrames()) {
2613 if (RenderWidgetHostView* rwhv = static_cast<RenderFrameHostImpl*>(rfh)
2614 ->frame_tree_node()
2615 ->render_manager()
2616 ->GetRenderWidgetHostView()) {
lfgf949e0e2017-05-10 22:18:112617 set.insert(rwhv);
dchengafb53e22016-02-04 08:11:082618 }
[email protected]948481d2014-06-11 18:32:222619 }
[email protected]de3c5d82014-05-28 22:12:592620 return set;
2621}
2622
Adithya Srinivasane6e7f842019-12-16 18:41:362623std::set<RenderWidgetHostView*>
2624WebContentsImpl::GetRenderWidgetHostViewsInWebContentsTree() {
2625 std::set<RenderWidgetHostView*> result;
2626 GetRenderWidgetHostViewsInWebContentsTree(result);
2627 return result;
2628}
2629
2630void WebContentsImpl::GetRenderWidgetHostViewsInWebContentsTree(
2631 std::set<RenderWidgetHostView*>& result) {
2632 std::set<RenderWidgetHostView*> views = GetRenderWidgetHostViewsInTree();
2633 result.insert(views.begin(), views.end());
2634 for (auto* inner_web_contents : GetInnerWebContents()) {
2635 static_cast<WebContentsImpl*>(inner_web_contents)
2636 ->GetRenderWidgetHostViewsInWebContentsTree(result);
2637 }
2638}
2639
[email protected]b172aee2012-04-10 17:05:262640void WebContentsImpl::Activate() {
[email protected]d5f942ba2008-09-26 19:30:342641 if (delegate_)
2642 delegate_->ActivateContents(this);
2643}
2644
avi3e4b8512015-11-11 01:55:492645void WebContentsImpl::LostCapture(RenderWidgetHostImpl* render_widget_host) {
2646 if (!RenderViewHostImpl::From(render_widget_host))
2647 return;
2648
[email protected]63954792011-07-11 04:17:482649 if (delegate_)
2650 delegate_->LostCapture();
2651}
2652
Yue Ru Sun979c4b162019-11-06 16:11:042653ukm::SourceId
2654WebContentsImpl::GetUkmSourceIdForLastCommittedSourceIncludingSameDocument()
2655 const {
2656 return last_committed_source_id_including_same_document_;
2657}
2658
Ahmed Fakhryfaa32d22018-10-05 15:56:162659void WebContentsImpl::SetTopControlsShownRatio(
2660 RenderWidgetHostImpl* render_widget_host,
2661 float ratio) {
2662 if (!delegate_)
2663 return;
2664
2665 RenderFrameHostImpl* rfh = GetMainFrame();
2666 if (!rfh || render_widget_host != rfh->GetRenderWidgetHost())
2667 return;
2668
2669 delegate_->SetTopControlsShownRatio(this, ratio);
Ahmed Fakhry58e6ef542018-09-04 18:05:382670}
2671
Ahmed Fakhry58e6ef542018-09-04 18:05:382672void WebContentsImpl::SetTopControlsGestureScrollInProgress(bool in_progress) {
2673 if (delegate_)
2674 delegate_->SetTopControlsGestureScrollInProgress(in_progress);
2675}
2676
lazyboy63f5b312015-11-23 20:19:522677void WebContentsImpl::RenderWidgetCreated(
2678 RenderWidgetHostImpl* render_widget_host) {
2679 created_widgets_.insert(render_widget_host);
2680}
2681
[email protected]b24b68a2012-09-24 21:57:262682void WebContentsImpl::RenderWidgetDeleted(
2683 RenderWidgetHostImpl* render_widget_host) {
lazyboy63f5b312015-11-23 20:19:522684 // Note that |is_being_destroyed_| can be true at this point as
2685 // ~WebContentsImpl() calls RFHM::ClearRFHsPendingShutdown(), which might lead
2686 // us here.
2687 created_widgets_.erase(render_widget_host);
[email protected]b24b68a2012-09-24 21:57:262688
lazyboy63f5b312015-11-23 20:19:522689 if (is_being_destroyed_)
2690 return;
[email protected]44470a22013-01-24 01:21:542691
2692 if (render_widget_host &&
alexmosc9e76ec2016-05-16 22:59:372693 render_widget_host->GetRoutingID() == fullscreen_widget_routing_id_ &&
2694 render_widget_host->GetProcess()->GetID() ==
2695 fullscreen_widget_process_id_) {
[email protected]4aebbca2013-09-17 22:26:492696 if (delegate_ && delegate_->EmbedsFullscreenWidget())
mlamouri7a78d6fd2015-01-17 13:23:532697 delegate_->ExitFullscreenModeForTab(this);
ericwilligersde386342016-10-19 02:35:092698 for (auto& observer : observers_)
2699 observer.DidDestroyFullscreenWidget();
alexmosc9e76ec2016-05-16 22:59:372700 fullscreen_widget_process_id_ = ChildProcessHost::kInvalidUniqueID;
[email protected]44470a22013-01-24 01:21:542701 fullscreen_widget_routing_id_ = MSG_ROUTING_NONE;
[email protected]0b431992014-06-24 00:08:032702 if (fullscreen_widget_had_focus_at_shutdown_)
2703 view_->RestoreFocus();
[email protected]44470a22013-01-24 01:21:542704 }
lfgbee1e0a2016-06-08 21:24:212705
lfg7d4caad2017-03-22 09:01:452706 if (render_widget_host == mouse_lock_widget_)
lfgff8dfc8d2016-10-21 17:42:322707 LostMouseLock(mouse_lock_widget_);
Joe Downing192998b22018-03-22 15:51:362708
Joe Downingc1a57efe2018-05-09 18:23:542709 CancelKeyboardLock(render_widget_host);
[email protected]b24b68a2012-09-24 21:57:262710}
2711
miu9e14e492014-10-25 02:39:042712void WebContentsImpl::RenderWidgetGotFocus(
2713 RenderWidgetHostImpl* render_widget_host) {
calamity7fe55ce2015-04-10 03:59:372714 // Notify the observers if an embedded fullscreen widget was focused.
2715 if (delegate_ && render_widget_host && delegate_->EmbedsFullscreenWidget() &&
2716 render_widget_host->GetView() == GetFullscreenRenderWidgetHostView()) {
Michael Thiessen896405db2017-07-20 17:52:322717 NotifyWebContentsFocused(render_widget_host);
calamity7fe55ce2015-04-10 03:59:372718 }
miu9e14e492014-10-25 02:39:042719}
2720
zijiehe3bee08e22017-05-17 04:14:462721void WebContentsImpl::RenderWidgetLostFocus(
2722 RenderWidgetHostImpl* render_widget_host) {
Michael Thiessen896405db2017-07-20 17:52:322723 // Notify the observers if an embedded fullscreen widget lost focus.
2724 if (delegate_ && render_widget_host && delegate_->EmbedsFullscreenWidget() &&
2725 render_widget_host->GetView() == GetFullscreenRenderWidgetHostView()) {
2726 NotifyWebContentsLostFocus(render_widget_host);
2727 }
zijiehe3bee08e22017-05-17 04:14:462728}
2729
estaded0a0c992015-01-21 14:12:062730void WebContentsImpl::RenderWidgetWasResized(
rouslan2f5993f2015-01-29 00:18:312731 RenderWidgetHostImpl* render_widget_host,
2732 bool width_changed) {
creisc014b402015-04-23 16:41:452733 RenderFrameHostImpl* rfh = GetMainFrame();
estadece344ec2015-01-23 00:12:292734 if (!rfh || render_widget_host != rfh->GetRenderWidgetHost())
estaded0a0c992015-01-21 14:12:062735 return;
estaded0a0c992015-01-21 14:12:062736
ericwilligersde386342016-10-19 02:35:092737 for (auto& observer : observers_)
2738 observer.MainFrameWasResized(width_changed);
estaded0a0c992015-01-21 14:12:062739}
2740
skyf65d9bb2017-03-24 02:26:392741KeyboardEventProcessingResult WebContentsImpl::PreHandleKeyboardEvent(
2742 const NativeWebKeyboardEvent& event) {
W. James MacLeane1187272019-11-12 03:42:482743 auto* outermost_contents = GetOutermostWebContents();
2744 // TODO(wjmaclean): Generalize this to forward all key events to the outermost
2745 // delegate's handler.
Avi Drissman97aef042020-06-30 21:04:482746 if (outermost_contents != this && IsFullscreen() &&
W. James MacLeane1187272019-11-12 03:42:482747 event.windows_key_code == ui::VKEY_ESCAPE) {
2748 // When an inner WebContents has focus and is fullscreen, redirect <esc>
2749 // key events to the outermost WebContents so it can be handled by that
2750 // WebContents' delegate.
2751 if (outermost_contents->PreHandleKeyboardEvent(event) ==
2752 KeyboardEventProcessingResult::HANDLED) {
2753 return KeyboardEventProcessingResult::HANDLED;
2754 }
2755 }
skyf65d9bb2017-03-24 02:26:392756 return delegate_ ? delegate_->PreHandleKeyboardEvent(this, event)
2757 : KeyboardEventProcessingResult::NOT_HANDLED;
[email protected]63954792011-07-11 04:17:482758}
2759
Dave Tapuska344399e2019-09-09 15:18:262760bool WebContentsImpl::HandleMouseEvent(const blink::WebMouseEvent& event) {
2761 // Handle mouse button back/forward in the browser process after the render
2762 // process is done with the event. This ensures all renderer-initiated history
2763 // navigations can be treated consistently.
2764 if (event.GetType() == blink::WebInputEvent::Type::kMouseUp) {
2765 WebContentsImpl* outermost = GetOutermostWebContents();
2766 if (event.button == blink::WebPointerProperties::Button::kBack &&
2767 outermost->controller_.CanGoBack()) {
2768 outermost->controller_.GoBack();
2769 return true;
2770 } else if (event.button == blink::WebPointerProperties::Button::kForward &&
2771 outermost->controller_.CanGoForward()) {
2772 outermost->controller_.GoForward();
2773 return true;
2774 }
2775 }
2776 return false;
2777}
2778
Scott Violetd5caa872018-10-16 22:00:512779bool WebContentsImpl::HandleKeyboardEvent(const NativeWebKeyboardEvent& event) {
[email protected]0b094002014-08-20 18:28:102780 if (browser_plugin_embedder_ &&
2781 browser_plugin_embedder_->HandleKeyboardEvent(event)) {
Scott Violetd5caa872018-10-16 22:00:512782 return true;
[email protected]0b094002014-08-20 18:28:102783 }
Scott Violetd5caa872018-10-16 22:00:512784 return delegate_ && delegate_->HandleKeyboardEvent(this, event);
[email protected]63954792011-07-11 04:17:482785}
2786
[email protected]d92026ef2014-03-03 21:04:132787bool WebContentsImpl::HandleWheelEvent(
[email protected]180ef242013-11-07 06:50:462788 const blink::WebMouseWheelEvent& event) {
Avi Drissman7c57be72020-07-29 20:09:462789#if !defined(OS_MAC)
lanweia93644f2015-01-21 22:00:332790 // On platforms other than Mac, control+mousewheel may change zoom. On Mac,
2791 // this isn't done for two reasons:
[email protected]bccc4472013-04-18 16:37:192792 // -the OS already has a gesture to do this through pinch-zoom
2793 // -if a user starts an inertial scroll, let's go, and presses control
2794 // (i.e. control+tab) then the OS's buffered scroll events will come in
2795 // with control key set which isn't what the user wants
Blink Reformat1c4d759e2017-04-09 16:34:542796 if (delegate_ && event.wheel_ticks_y &&
Ehsan Karamad0f83cb112018-09-06 22:07:362797 event.event_action == blink::WebMouseWheelEvent::EventAction::kPageZoom) {
w.shackleton49bcd392016-01-06 17:38:222798 // Count only integer cumulative scrolls as zoom events; this handles
2799 // smooth scroll and regular scroll device behavior.
Blink Reformat1c4d759e2017-04-09 16:34:542800 zoom_scroll_remainder_ += event.wheel_ticks_y;
w.shackleton49bcd392016-01-06 17:38:222801 int whole_zoom_scroll_remainder_ = std::lround(zoom_scroll_remainder_);
2802 zoom_scroll_remainder_ -= whole_zoom_scroll_remainder_;
2803 if (whole_zoom_scroll_remainder_ != 0) {
2804 delegate_->ContentsZoomChange(whole_zoom_scroll_remainder_ > 0);
2805 }
[email protected]fb3f066e2013-02-12 19:12:522806 return true;
2807 }
[email protected]bccc4472013-04-18 16:37:192808#endif
[email protected]fb3f066e2013-02-12 19:12:522809 return false;
2810}
2811
[email protected]04bce562014-01-30 05:34:542812bool WebContentsImpl::PreHandleGestureEvent(
2813 const blink::WebGestureEvent& event) {
[email protected]28042d32014-02-03 14:10:032814 return delegate_ && delegate_->PreHandleGestureEvent(this, event);
[email protected]04bce562014-01-30 05:34:542815}
2816
kenrb2a565f82015-09-02 20:24:592817RenderWidgetHostInputEventRouter* WebContentsImpl::GetInputEventRouter() {
wjmacleane31234f2015-12-14 17:20:452818 if (!is_being_destroyed_ && GetOuterWebContents())
2819 return GetOuterWebContents()->GetInputEventRouter();
2820
2821 if (!rwh_input_event_router_.get() && !is_being_destroyed_)
kenrb2a565f82015-09-02 20:24:592822 rwh_input_event_router_.reset(new RenderWidgetHostInputEventRouter);
2823 return rwh_input_event_router_.get();
2824}
2825
alexmos3fcd0ca2015-10-23 18:18:332826void WebContentsImpl::ReplicatePageFocus(bool is_focused) {
2827 // Focus loss may occur while this WebContents is being destroyed. Don't
2828 // send the message in this case, as the main frame's RenderFrameHost and
2829 // other state has already been cleared.
2830 if (is_being_destroyed_)
2831 return;
2832
2833 frame_tree_.ReplicatePageFocus(is_focused);
2834}
2835
alexmosc4cbacb2015-11-21 01:29:222836RenderWidgetHostImpl* WebContentsImpl::GetFocusedRenderWidgetHost(
2837 RenderWidgetHostImpl* receiving_widget) {
alexmosc4cbacb2015-11-21 01:29:222838 // Events for widgets other than the main frame (e.g., popup menus) should be
2839 // forwarded directly to the widget they arrived on.
2840 if (receiving_widget != GetMainFrame()->GetRenderWidgetHost())
2841 return receiving_widget;
alexmosc4d183e2015-11-06 00:46:522842
paulmeyerfeafc2d2017-04-25 21:46:402843 // If the focused WebContents is a guest WebContents, then get the focused
2844 // frame in the embedder WebContents instead.
W. James MacLean13d834d2019-12-04 16:06:452845 FrameTreeNode* focused_frame =
2846 GetFocusedWebContents()->frame_tree_.GetFocusedFrame();
lfg1453e412017-04-11 00:48:502847
alexmosc4d183e2015-11-06 00:46:522848 if (!focused_frame)
alexmosc4cbacb2015-11-21 01:29:222849 return receiving_widget;
alexmosc4d183e2015-11-06 00:46:522850
alexmos732ca3a2015-12-08 02:01:022851 // The view may be null if a subframe's renderer process has crashed while
2852 // the subframe has focus. Drop the event in that case. Do not give
2853 // it to the main frame, so that the user doesn't unexpectedly type into the
2854 // wrong frame if a focused subframe renderer crashes while they type.
2855 RenderWidgetHostView* view = focused_frame->current_frame_host()->GetView();
2856 if (!view)
2857 return nullptr;
2858
2859 return RenderWidgetHostImpl::From(view->GetRenderWidgetHost());
alexmosc4d183e2015-11-06 00:46:522860}
2861
avallee9993fca2016-11-17 06:16:502862RenderWidgetHostImpl* WebContentsImpl::GetRenderWidgetHostWithPageFocus() {
lfg1453e412017-04-11 00:48:502863 WebContentsImpl* focused_web_contents = GetFocusedWebContents();
2864
lfg1453e412017-04-11 00:48:502865 return focused_web_contents->GetMainFrame()->GetRenderWidgetHost();
avallee9993fca2016-11-17 06:16:502866}
2867
Avi Drissmand1d25652020-03-06 21:28:582868bool WebContentsImpl::CanEnterFullscreenMode() {
Avi Drissman33972942020-06-19 14:16:012869 // It's possible that this WebContents was spawned while blocking UI was on
2870 // the screen, or that it was downstream from a WebContents when UI was
2871 // blocked. Therefore, disqualify it from fullscreen if it or any upstream
2872 // WebContents has an active blocker.
2873 auto openers = GetAllOpeningWebContents(this);
2874 return std::all_of(openers.begin(), openers.end(), [](auto* opener) {
2875 return opener->fullscreen_blocker_count_ == 0;
2876 });
Avi Drissmand1d25652020-03-06 21:28:582877}
2878
Dave Tapuska3ed44192018-05-01 18:53:302879void WebContentsImpl::EnterFullscreenMode(
Mike Wasserman4ca09792020-05-29 17:44:432880 RenderFrameHost* requesting_frame,
Dave Tapuskaa4189512019-10-15 20:27:342881 const blink::mojom::FullscreenOptions& options) {
Avi Drissmand1d25652020-03-06 21:28:582882 DCHECK(CanEnterFullscreenMode());
2883
mlamouri7a78d6fd2015-01-17 13:23:532884 // This method is being called to enter renderer-initiated fullscreen mode.
2885 // Make sure any existing fullscreen widget is shut down first.
[email protected]4aebbca2013-09-17 22:26:492886 RenderWidgetHostView* const widget_view = GetFullscreenRenderWidgetHostView();
avi85a4cef2015-12-18 20:13:372887 if (widget_view) {
2888 RenderWidgetHostImpl::From(widget_view->GetRenderWidgetHost())
2889 ->ShutdownAndDestroyWidget(true);
2890 }
[email protected]4aebbca2013-09-17 22:26:492891
Joe Downing13dd76b2018-04-09 18:32:152892 if (delegate_) {
Mike Wasserman4ca09792020-05-29 17:44:432893 delegate_->EnterFullscreenModeForTab(requesting_frame, options);
mlamouri7a78d6fd2015-01-17 13:23:532894
Joe Downing13dd76b2018-04-09 18:32:152895 if (keyboard_lock_widget_)
2896 delegate_->RequestKeyboardLock(this, esc_key_locked_);
2897 }
2898
ericwilligersde386342016-10-19 02:35:092899 for (auto& observer : observers_)
Avi Drissman97aef042020-06-30 21:04:482900 observer.DidToggleFullscreenModeForTab(IsFullscreen(), false);
Avi Drissman33972942020-06-19 14:16:012901
2902 FullscreenContentsSet(GetBrowserContext())->insert(this);
mlamouri7a78d6fd2015-01-17 13:23:532903}
2904
bokanece34a82016-01-28 19:49:462905void WebContentsImpl::ExitFullscreenMode(bool will_cause_resize) {
mlamouri7a78d6fd2015-01-17 13:23:532906 // This method is being called to leave renderer-initiated fullscreen mode.
2907 // Make sure any existing fullscreen widget is shut down first.
2908 RenderWidgetHostView* const widget_view = GetFullscreenRenderWidgetHostView();
avi85a4cef2015-12-18 20:13:372909 if (widget_view) {
2910 RenderWidgetHostImpl::From(widget_view->GetRenderWidgetHost())
2911 ->ShutdownAndDestroyWidget(true);
2912 }
mlamouri7a78d6fd2015-01-17 13:23:532913
Joe Downing13dd76b2018-04-09 18:32:152914 if (delegate_) {
mlamouri7a78d6fd2015-01-17 13:23:532915 delegate_->ExitFullscreenModeForTab(this);
[email protected]657c8e02014-03-19 19:18:372916
Joe Downing13dd76b2018-04-09 18:32:152917 if (keyboard_lock_widget_)
2918 delegate_->CancelKeyboardLockRequest(this);
2919 }
2920
bokanece34a82016-01-28 19:49:462921 // The fullscreen state is communicated to the renderer through a resize
2922 // message. If the change in fullscreen state doesn't cause a view resize
2923 // then we must ensure web contents exit the fullscreen state by explicitly
2924 // sending a resize message. This is required for the situation of the browser
2925 // moving the view into a "browser fullscreen" state and then the contents
2926 // entering "tab fullscreen". Exiting the contents "tab fullscreen" then won't
2927 // have the side effect of the view resizing, hence the explicit call here is
2928 // required.
2929 if (!will_cause_resize) {
2930 if (RenderWidgetHostView* rwhv = GetRenderWidgetHostView()) {
2931 if (RenderWidgetHost* render_widget_host = rwhv->GetRenderWidgetHost())
Fady Samuel0b911822018-04-25 13:22:162932 render_widget_host->SynchronizeVisualProperties();
bokanece34a82016-01-28 19:49:462933 }
scheib3dcb6c932015-02-23 10:55:582934 }
2935
Arthur Sonzogni929d29f2018-09-17 14:19:442936 current_fullscreen_frame_ = nullptr;
Becca Hughese9e27952018-06-25 17:08:272937
ericwilligersde386342016-10-19 02:35:092938 for (auto& observer : observers_) {
Avi Drissman97aef042020-06-30 21:04:482939 observer.DidToggleFullscreenModeForTab(IsFullscreen(), will_cause_resize);
ericwilligersde386342016-10-19 02:35:092940 }
Becca Hughesd11d6502018-07-31 17:01:282941
2942 if (display_cutout_host_impl_)
2943 display_cutout_host_impl_->DidExitFullscreen();
Avi Drissman33972942020-06-19 14:16:012944
2945 FullscreenContentsSet(GetBrowserContext())->erase(this);
[email protected]8a5e0ca2011-08-25 06:30:472946}
2947
Becca Hughesfd5d8f892018-06-14 18:23:362948void WebContentsImpl::FullscreenStateChanged(RenderFrameHost* rfh,
2949 bool is_fullscreen) {
Arthur Sonzogni929d29f2018-09-17 14:19:442950 RenderFrameHostImpl* frame = static_cast<RenderFrameHostImpl*>(rfh);
Becca Hughesfd5d8f892018-06-14 18:23:362951
2952 if (is_fullscreen) {
Jan Wilken Dörrie531be7ca2019-06-07 10:05:572953 if (!base::Contains(fullscreen_frames_, frame)) {
Arthur Sonzogni929d29f2018-09-17 14:19:442954 fullscreen_frames_.insert(frame);
2955 FullscreenFrameSetUpdated();
Becca Hughesfd5d8f892018-06-14 18:23:362956 }
Arthur Sonzogni929d29f2018-09-17 14:19:442957 return;
2958 }
Becca Hughesfd5d8f892018-06-14 18:23:362959
Arthur Sonzogni929d29f2018-09-17 14:19:442960 // If |frame| is no longer in fullscreen, remove it and any descendants.
2961 // See https://fullscreen.spec.whatwg.org.
Arthur Sonzogni969a794e2018-10-11 08:56:092962 size_t size_before_deletion = fullscreen_frames_.size();
2963 base::EraseIf(fullscreen_frames_, [&](RenderFrameHostImpl* current) {
2964 return (current == frame || current->IsDescendantOf(frame));
Arthur Sonzogni929d29f2018-09-17 14:19:442965 });
2966
Arthur Sonzogni969a794e2018-10-11 08:56:092967 if (size_before_deletion != fullscreen_frames_.size())
Arthur Sonzogni929d29f2018-09-17 14:19:442968 FullscreenFrameSetUpdated();
2969}
2970
2971void WebContentsImpl::FullscreenFrameSetUpdated() {
2972 if (fullscreen_frames_.empty()) {
2973 current_fullscreen_frame_ = nullptr;
2974 return;
Becca Hughesfd5d8f892018-06-14 18:23:362975 }
2976
Arthur Sonzogni929d29f2018-09-17 14:19:442977 // Find the current fullscreen frame and call the observers.
2978 // If frame A is fullscreen, then frame B goes into inner fullscreen, then B
2979 // exits fullscreen - that will result in A being fullscreen.
2980 RenderFrameHostImpl* new_fullscreen_frame = *std::max_element(
2981 fullscreen_frames_.begin(), fullscreen_frames_.end(), FrameCompareDepth);
Becca Hughesfd5d8f892018-06-14 18:23:362982
Arthur Sonzogni929d29f2018-09-17 14:19:442983 // If we have already notified observers about this frame then we should not
2984 // fire the observers again.
2985 if (new_fullscreen_frame == current_fullscreen_frame_)
2986 return;
2987 current_fullscreen_frame_ = new_fullscreen_frame;
Becca Hughesfd5d8f892018-06-14 18:23:362988
Arthur Sonzogni929d29f2018-09-17 14:19:442989 for (auto& observer : observers_)
2990 observer.DidAcquireFullscreen(new_fullscreen_frame);
Becca Hughesfd5d8f892018-06-14 18:23:362991
Arthur Sonzogni929d29f2018-09-17 14:19:442992 if (display_cutout_host_impl_)
2993 display_cutout_host_impl_->DidAcquireFullscreen(new_fullscreen_frame);
Becca Hughesfd5d8f892018-06-14 18:23:362994}
2995
Collin Baker989e0882019-11-01 01:27:172996void WebContentsImpl::UpdateVisibilityAndNotifyPageAndView(
2997 Visibility new_visibility) {
Collin Baker989e0882019-11-01 01:27:172998 // Only hide the page if there are no entities capturing screenshots
Collin Bakerc5259962019-11-14 17:51:582999 // or video (e.g. mirroring). If there are, apply the correct state of
3000 // kHidden or kHiddenButPainting.
3001 PageVisibilityState page_visibility;
3002 if (new_visibility == Visibility::VISIBLE || visible_capturer_count_ > 0)
3003 page_visibility = PageVisibilityState::kVisible;
3004 else if (hidden_capturer_count_ > 0)
3005 page_visibility = PageVisibilityState::kHiddenButPainting;
3006 else
3007 page_visibility = PageVisibilityState::kHidden;
Collin Baker989e0882019-11-01 01:27:173008 // If there are entities in Picture-in-Picture mode, don't activate
3009 // the "disable rendering" optimization.
Collin Bakerc5259962019-11-14 17:51:583010 const bool view_is_visible =
3011 page_visibility != PageVisibilityState::kHidden ||
3012 HasPictureInPictureVideo();
Collin Baker989e0882019-11-01 01:27:173013
Collin Bakerc5259962019-11-14 17:51:583014 if (page_visibility != PageVisibilityState::kHidden) {
Dana Friedcf3bc8e2019-11-13 20:00:243015 // We cannot show a page or capture video unless there is a valid renderer
3016 // associated with this web contents. The navigation controller for this
3017 // page must be set to active (allowing navigation to complete, a renderer
3018 // and its associated views to be created, etc.) if any of these conditions
3019 // holds.
3020 //
3021 // Previously, it was possible for browser-side code to try to capture video
3022 // from a restored tab (for a variety of reasons, including the browser
3023 // creating preview thumbnails) and the tab would never actually load. By
Collin Bakerc5259962019-11-14 17:51:583024 // keying this behavior off of |page_visibility| instead of just
Dana Friedcf3bc8e2019-11-13 20:00:243025 // |new_visibility| we avoid this case. See crbug.com/1020782 for more
3026 // context.
3027 controller_.SetActive(true);
3028
3029 // This shows the Page before showing the individual RenderWidgets, as
3030 // RenderWidgets will work to produce compositor frames and handle input
3031 // as soon as they are shown. But the Page and other classes do not expect
3032 // to be producing frames when the Page is hidden. So we make sure the Page
3033 // is shown first.
Yuzu Saijo232076f2020-05-29 07:14:023034 for (auto* rvh : GetRenderViewHostsIncludingBackForwardCached()) {
3035 rvh->SetVisibility(page_visibility);
Yuzu Saijobe024af02020-05-11 07:35:563036 }
Collin Baker98457b52019-11-06 21:34:293037 }
Collin Baker989e0882019-11-01 01:27:173038
3039 // |GetRenderWidgetHostView()| can be null if the user middle clicks a link to
3040 // open a tab in the background, then closes the tab before selecting it.
3041 // This is because closing the tab calls WebContentsImpl::Destroy(), which
3042 // removes the |GetRenderViewHost()|; then when we actually destroy the
3043 // window, OnWindowPosChanged() notices and calls WasHidden() (which
3044 // calls us).
3045 if (auto* view = GetRenderWidgetHostView()) {
3046 if (view_is_visible) {
3047 view->Show();
Collin Baker989e0882019-11-01 01:27:173048 } else if (new_visibility == Visibility::HIDDEN) {
3049 view->Hide();
3050 } else {
3051 view->WasOccluded();
3052 }
3053 }
3054
Carlos IL07f282f2020-05-15 22:58:293055 SetVisibilityForChildViews(view_is_visible);
Collin Baker989e0882019-11-01 01:27:173056
3057 // Make sure to call SetVisibilityAndNotifyObservers(VISIBLE) before notifying
3058 // the CrossProcessFrameConnector.
3059 if (new_visibility == Visibility::VISIBLE) {
3060 last_active_time_ = base::TimeTicks::Now();
3061 SetVisibilityAndNotifyObservers(new_visibility);
3062 }
3063
Collin Bakerc5259962019-11-14 17:51:583064 if (page_visibility == PageVisibilityState::kHidden) {
Collin Baker989e0882019-11-01 01:27:173065 // Similar to when showing the page, we only hide the page after
3066 // hiding the individual RenderWidgets.
Yuzu Saijo232076f2020-05-29 07:14:023067 for (auto* rvh : GetRenderViewHostsIncludingBackForwardCached()) {
3068 rvh->SetVisibility(page_visibility);
Yuzu Saijobe024af02020-05-11 07:35:563069 }
3070
Collin Baker989e0882019-11-01 01:27:173071 } else {
3072 for (FrameTreeNode* node : frame_tree_.Nodes()) {
3073 RenderFrameProxyHost* parent = node->render_manager()->GetProxyToParent();
3074 if (!parent)
3075 continue;
3076
3077 parent->cross_process_frame_connector()->DelegateWasShown();
3078 }
3079 }
3080
3081 if (new_visibility != Visibility::VISIBLE)
3082 SetVisibilityAndNotifyObservers(new_visibility);
3083}
3084
Clark DuValla0e42992018-09-17 17:00:113085#if defined(OS_ANDROID)
3086void WebContentsImpl::UpdateUserGestureCarryoverInfo() {
3087 if (delegate_)
3088 delegate_->UpdateUserGestureCarryoverInfo(this);
3089}
3090#endif
3091
Avi Drissman97aef042020-06-30 21:04:483092bool WebContentsImpl::IsFullscreen() {
[email protected]199bba6e2012-04-04 16:19:383093 return delegate_ ? delegate_->IsFullscreenForTabOrPending(this) : false;
[email protected]5d5f7af2011-10-01 01:38:123094}
3095
Malay Keshav87c42c02019-01-15 08:37:473096bool WebContentsImpl::ShouldShowStaleContentOnEviction() {
3097 return GetDelegate() && GetDelegate()->ShouldShowStaleContentOnEviction(this);
3098}
3099
danakj45d42452020-04-08 17:24:183100blink::mojom::DisplayMode WebContentsImpl::GetDisplayMode() const {
mikhail.pozdnyakovc0e251b2015-04-15 06:51:123101 return delegate_ ? delegate_->GetDisplayMode(this)
Eric Willigers052f0432019-10-04 04:06:573102 : blink::mojom::DisplayMode::kBrowser;
mikhail.pozdnyakovc0e251b2015-04-15 06:51:123103}
3104
avic3aa8422015-11-09 20:57:223105void WebContentsImpl::RequestToLockMouse(
3106 RenderWidgetHostImpl* render_widget_host,
3107 bool user_gesture,
lfgad8244352016-07-13 16:55:513108 bool last_unlocked_by_target,
3109 bool privileged) {
lfgf0cd46e2017-01-04 00:05:233110 for (WebContentsImpl* current = this; current;
3111 current = current->GetOuterWebContents()) {
3112 if (current->mouse_lock_widget_) {
James Hollyerd5c9de462020-03-10 19:02:453113 render_widget_host->GotResponseToLockMouseRequest(
3114 blink::mojom::PointerLockResult::kAlreadyLocked);
lfgf0cd46e2017-01-04 00:05:233115 return;
3116 }
[email protected]e9621112011-10-17 05:38:373117 }
avic3aa8422015-11-09 20:57:223118
lfgad8244352016-07-13 16:55:513119 if (privileged) {
lfgf0cd46e2017-01-04 00:05:233120 DCHECK(!GetOuterWebContents());
lfgad8244352016-07-13 16:55:513121 mouse_lock_widget_ = render_widget_host;
James Hollyerd5c9de462020-03-10 19:02:453122 render_widget_host->GotResponseToLockMouseRequest(
3123 blink::mojom::PointerLockResult::kSuccess);
lfgad8244352016-07-13 16:55:513124 return;
3125 }
3126
lfgbee1e0a2016-06-08 21:24:213127 bool widget_in_frame_tree = false;
3128 for (FrameTreeNode* node : frame_tree_.Nodes()) {
3129 if (node->current_frame_host()->GetRenderWidgetHost() ==
3130 render_widget_host) {
3131 widget_in_frame_tree = true;
3132 break;
3133 }
3134 }
3135
3136 if (widget_in_frame_tree && delegate_) {
lfgf0cd46e2017-01-04 00:05:233137 for (WebContentsImpl* current = this; current;
3138 current = current->GetOuterWebContents()) {
3139 current->mouse_lock_widget_ = render_widget_host;
3140 }
3141
avic3aa8422015-11-09 20:57:223142 delegate_->RequestToLockMouse(this, user_gesture, last_unlocked_by_target);
lfgbee1e0a2016-06-08 21:24:213143 } else {
James Hollyerd5c9de462020-03-10 19:02:453144 render_widget_host->GotResponseToLockMouseRequest(
3145 blink::mojom::PointerLockResult::kWrongDocument);
lfgbee1e0a2016-06-08 21:24:213146 }
[email protected]e9621112011-10-17 05:38:373147}
3148
avi3e4b8512015-11-11 01:55:493149void WebContentsImpl::LostMouseLock(RenderWidgetHostImpl* render_widget_host) {
lfgbee1e0a2016-06-08 21:24:213150 CHECK(mouse_lock_widget_);
lfgf0cd46e2017-01-04 00:05:233151
3152 if (mouse_lock_widget_->delegate()->GetAsWebContents() != this)
3153 return mouse_lock_widget_->delegate()->LostMouseLock(render_widget_host);
3154
lfgbee1e0a2016-06-08 21:24:213155 mouse_lock_widget_->SendMouseLockLost();
lfgf0cd46e2017-01-04 00:05:233156 for (WebContentsImpl* current = this; current;
3157 current = current->GetOuterWebContents()) {
3158 current->mouse_lock_widget_ = nullptr;
3159 }
avi3e4b8512015-11-11 01:55:493160
[email protected]e9621112011-10-17 05:38:373161 if (delegate_)
3162 delegate_->LostMouseLock();
3163}
3164
lfgad8244352016-07-13 16:55:513165bool WebContentsImpl::HasMouseLock(RenderWidgetHostImpl* render_widget_host) {
3166 // To verify if the mouse is locked, the mouse_lock_widget_ needs to be
3167 // assigned to the widget that requested the mouse lock, and the top-level
3168 // platform RenderWidgetHostView needs to hold the mouse lock from the OS.
Lucas Gadania0ea0172018-09-20 18:31:373169 auto* widget_host = GetTopLevelRenderWidgetHostView();
3170 return mouse_lock_widget_ == render_widget_host && widget_host &&
3171 widget_host->IsMouseLocked();
lfgad8244352016-07-13 16:55:513172}
3173
lfg84763c92017-02-16 18:55:153174RenderWidgetHostImpl* WebContentsImpl::GetMouseLockWidget() {
Lucas Gadania0ea0172018-09-20 18:31:373175 auto* widget_host = GetTopLevelRenderWidgetHostView();
3176 if ((widget_host && widget_host->IsMouseLocked()) ||
lfge2176fb2017-03-26 00:45:473177 (GetFullscreenRenderWidgetHostView() &&
Lucas Gadania0ea0172018-09-20 18:31:373178 GetFullscreenRenderWidgetHostView()->IsMouseLocked())) {
lfg84763c92017-02-16 18:55:153179 return mouse_lock_widget_;
Lucas Gadania0ea0172018-09-20 18:31:373180 }
lfg84763c92017-02-16 18:55:153181
3182 return nullptr;
3183}
3184
Joe Downing192998b22018-03-22 15:51:363185bool WebContentsImpl::RequestKeyboardLock(
Joe Downing13dd76b2018-04-09 18:32:153186 RenderWidgetHostImpl* render_widget_host,
3187 bool esc_key_locked) {
Joe Downing192998b22018-03-22 15:51:363188 DCHECK(render_widget_host);
Joe Downing192998b22018-03-22 15:51:363189 if (render_widget_host->delegate()->GetAsWebContents() != this) {
3190 NOTREACHED();
3191 return false;
3192 }
3193
3194 // KeyboardLock is only supported when called by the top-level browsing
3195 // context and is not supported in embedded content scenarios.
3196 if (GetOuterWebContents())
3197 return false;
3198
Joe Downing13dd76b2018-04-09 18:32:153199 esc_key_locked_ = esc_key_locked;
Joe Downing192998b22018-03-22 15:51:363200 keyboard_lock_widget_ = render_widget_host;
3201
Joe Downing13dd76b2018-04-09 18:32:153202 if (delegate_)
3203 delegate_->RequestKeyboardLock(this, esc_key_locked_);
Joe Downing192998b22018-03-22 15:51:363204 return true;
3205}
3206
3207void WebContentsImpl::CancelKeyboardLock(
3208 RenderWidgetHostImpl* render_widget_host) {
3209 if (!keyboard_lock_widget_ || render_widget_host != keyboard_lock_widget_)
3210 return;
3211
3212 RenderWidgetHostImpl* old_keyboard_lock_widget = keyboard_lock_widget_;
3213 keyboard_lock_widget_ = nullptr;
3214
Joe Downing13dd76b2018-04-09 18:32:153215 if (delegate_)
3216 delegate_->CancelKeyboardLockRequest(this);
3217
Joe Downing192998b22018-03-22 15:51:363218 old_keyboard_lock_widget->CancelKeyboardLock();
3219}
3220
3221RenderWidgetHostImpl* WebContentsImpl::GetKeyboardLockWidget() {
3222 return keyboard_lock_widget_;
3223}
3224
Dave Tapuskac3344362019-02-20 17:54:283225void WebContentsImpl::OnRenderFrameProxyVisibilityChanged(
3226 blink::mojom::FrameVisibility visibility) {
3227 switch (visibility) {
3228 case blink::mojom::FrameVisibility::kRenderedInViewport:
3229 WasShown();
3230 break;
3231 case blink::mojom::FrameVisibility::kNotRendered:
3232 WasHidden();
3233 break;
3234 case blink::mojom::FrameVisibility::kRenderedOutOfViewport:
3235 WasOccluded();
3236 break;
3237 }
ekaramada4f42692016-02-11 19:48:373238}
3239
Albert J. Wong65fe64d2019-09-20 02:48:143240RenderFrameHostDelegate* WebContentsImpl::CreateNewWindow(
csharrison95f01e922017-04-24 18:52:353241 RenderFrameHost* opener,
rockot5c478a72016-09-28 23:14:183242 const mojom::CreateNewWindowParams& params,
Albert J. Wong65fe64d2019-09-20 02:48:143243 bool is_new_browsing_instance,
Sergey Kuznetsov32257a22019-02-11 20:26:503244 bool has_user_gesture,
[email protected]97714c82012-06-06 10:15:133245 SessionStorageNamespace* session_storage_namespace) {
csharrison95f01e922017-04-24 18:52:353246 DCHECK(opener);
nick5ae4d2d2017-01-06 01:18:353247
csharrison95f01e922017-04-24 18:52:353248 int render_process_id = opener->GetProcess()->GetID();
csharrison95f01e922017-04-24 18:52:353249
Albert J. Wong65fe64d2019-09-20 02:48:143250 SiteInstance* source_site_instance = opener->GetSiteInstance();
nick5ae4d2d2017-01-06 01:18:353251
Aaron Colwelleb219982020-01-16 02:17:273252 const GURL source_site_url = source_site_instance->GetSiteURL();
[email protected]fdac6ade2013-07-20 01:06:303253 const std::string& partition_id =
Aaron Colwelleb219982020-01-16 02:17:273254 GetContentClient()->browser()->GetStoragePartitionIdForSite(
3255 GetBrowserContext(), source_site_url);
3256 {
3257 StoragePartition* partition = BrowserContext::GetStoragePartitionForSite(
3258 GetBrowserContext(), source_site_url);
3259 DOMStorageContextWrapper* dom_storage_context =
3260 static_cast<DOMStorageContextWrapper*>(
3261 partition->GetDOMStorageContext());
3262 SessionStorageNamespaceImpl* session_storage_namespace_impl =
3263 static_cast<SessionStorageNamespaceImpl*>(session_storage_namespace);
3264 CHECK(session_storage_namespace_impl->IsFromContext(dom_storage_context));
3265 }
[email protected]dd6730412013-08-14 15:03:373266
Albert J. Wong1ceccef92019-10-08 08:25:203267 if (delegate_ && delegate_->IsWebContentsCreationOverridden(
3268 source_site_instance, params.window_container_type,
3269 opener->GetLastCommittedURL(), params.frame_name,
3270 params.target_url)) {
3271 return static_cast<WebContentsImpl*>(delegate_->CreateCustomWebContents(
3272 opener, source_site_instance, is_new_browsing_instance,
3273 opener->GetLastCommittedURL(), params.frame_name, params.target_url,
3274 partition_id, session_storage_namespace));
[email protected]dd6730412013-08-14 15:03:373275 }
3276
danakj22b4e8b2019-08-01 23:14:393277 bool renderer_started_hidden =
3278 params.disposition == WindowOpenDisposition::NEW_BACKGROUND_TAB;
3279
Aaron Colwelleb219982020-01-16 02:17:273280 // We usually create the new window in the same BrowsingInstance (group of
3281 // script-related windows), by passing in the current SiteInstance. However,
3282 // if the opener is being suppressed (in a non-guest), we do not provide
3283 // a SiteInstance which causes a new one to get created in its own
3284 // BrowsingInstance.
3285 bool is_guest = BrowserPluginGuest::IsGuest(this);
3286 scoped_refptr<SiteInstance> site_instance =
3287 params.opener_suppressed && !is_guest ? nullptr : source_site_instance;
3288
[email protected]dd6730412013-08-14 15:03:373289 // Create the new web contents. This will automatically create the new
3290 // WebContentsView. In the future, we may want to create the view separately.
[email protected]fc72bb12013-06-02 21:13:463291 CreateParams create_params(GetBrowserContext(), site_instance.get());
nasko48321ca32015-07-02 20:44:123292 create_params.main_frame_name = params.frame_name;
alexmos4cf2aa32015-07-15 23:40:433293 create_params.opener_render_process_id = render_process_id;
csharrison95f01e922017-04-24 18:52:353294 create_params.opener_render_frame_id = opener->GetRoutingID();
[email protected]50d326e2014-05-20 17:59:063295 create_params.opener_suppressed = params.opener_suppressed;
danakj22b4e8b2019-08-01 23:14:393296 create_params.initially_hidden = renderer_started_hidden;
[email protected]50d326e2014-05-20 17:59:063297
Albert J. Wong65fe64d2019-09-20 02:48:143298 // Even though all codepaths leading here are in response to a renderer
3299 // tryng to open a new window, if the new window ends up in a different
3300 // browsing instance, then the RenderViewHost, RenderWidgetHost,
3301 // RenderFrameHost constellation is effectively browser initiated
3302 // the opener's process will not given the routing IDs for the new
3303 // objects.
3304 create_params.renderer_initiated_creation = !is_new_browsing_instance;
Albert J. Wong1ceccef92019-10-08 08:25:203305
Albert J. Wong65fe64d2019-09-20 02:48:143306 std::unique_ptr<WebContentsImpl> new_contents;
[email protected]c4538072013-03-18 02:17:553307 if (!is_guest) {
3308 create_params.context = view_->GetNativeView();
Albert J. Wong65fe64d2019-09-20 02:48:143309 new_contents = WebContentsImpl::Create(create_params);
3310 } else {
3311 new_contents = base::WrapUnique(static_cast<WebContentsImpl*>(
3312 GetBrowserPluginGuest()->CreateNewGuestWindow(create_params)));
[email protected]c4538072013-03-18 02:17:553313 }
Albert J. Wong65fe64d2019-09-20 02:48:143314 auto* new_contents_impl = new_contents.get();
danakje6c01622018-10-05 19:39:263315
3316 new_contents_impl->GetController().SetSessionStorageNamespace(
erikchenbee5c9622018-04-27 19:30:253317 partition_id, session_storage_namespace);
[email protected]d1198fd2012-08-13 22:50:193318
alexmos646fec02015-07-25 00:11:493319 // If the new frame has a name, make sure any SiteInstances that can find
3320 // this named frame have proxies for it. Must be called after
3321 // SetSessionStorageNamespace, since this calls CreateRenderView, which uses
3322 // GetSessionStorageNamespace.
3323 if (!params.frame_name.empty())
danakje6c01622018-10-05 19:39:263324 new_contents_impl->GetRenderManager()->CreateProxiesForNewNamedFrame();
alexmos646fec02015-07-25 00:11:493325
[email protected]c4538072013-03-18 02:17:553326 // Save the window for later if we're not suppressing the opener (since it
[email protected]d70bea92013-04-05 04:23:343327 // will be shown immediately).
3328 if (!params.opener_suppressed) {
3329 if (!is_guest) {
danakje6c01622018-10-05 19:39:263330 WebContentsView* new_view = new_contents_impl->view_.get();
[email protected]bafe6cd2012-05-23 23:09:503331
[email protected]d70bea92013-04-05 04:23:343332 // TODO(brettw): It seems bogus that we have to call this function on the
3333 // newly created object and give it one of its own member variables.
danakj22b4e8b2019-08-01 23:14:393334 RenderWidgetHostView* widget_view = new_view->CreateViewForWidget(
W. James MacLean13d834d2019-12-04 16:06:453335 new_contents_impl->GetRenderViewHost()->GetWidget());
danakj22b4e8b2019-08-01 23:14:393336 if (!renderer_started_hidden) {
3337 // RenderWidgets for frames always initialize as hidden. If the renderer
3338 // created this window as visible, then we show it here.
3339 widget_view->Show();
3340 }
[email protected]d70bea92013-04-05 04:23:343341 }
[email protected]bafe6cd2012-05-23 23:09:503342 // Save the created window associated with the route so we can show it
3343 // later.
Albert J. Wongcb004632018-07-10 22:58:253344 //
3345 // TODO(ajwong): This should be keyed off the RenderFrame routing id or the
3346 // FrameTreeNode id instead of the routing id of the Widget for the main
3347 // frame. https://crbug.com/545684
Fergal Daly052873a82020-03-11 14:45:493348 int32_t main_frame_routing_id = new_contents_impl->GetMainFrame()
3349 ->GetRenderWidgetHost()
3350 ->GetRoutingID();
3351 GlobalRoutingID id(render_process_id, main_frame_routing_id);
Joel Hockey8c2011b22020-04-30 05:07:193352 pending_contents_[id] =
3353 CreatedWindow(std::move(new_contents), params.target_url);
danakje6c01622018-10-05 19:39:263354 AddDestructionObserver(new_contents_impl);
[email protected]bafe6cd2012-05-23 23:09:503355 }
3356
3357 if (delegate_) {
Bryan McQuaded2b93c02017-07-14 22:26:313358 delegate_->WebContentsCreated(this, render_process_id,
3359 opener->GetRoutingID(), params.frame_name,
danakje6c01622018-10-05 19:39:263360 params.target_url, new_contents_impl);
[email protected]bafe6cd2012-05-23 23:09:503361 }
3362
Charlie Harrisonf60dd1c2018-08-15 21:15:413363 for (auto& observer : observers_) {
danakje6c01622018-10-05 19:39:263364 observer.DidOpenRequestedURL(new_contents_impl, opener, params.target_url,
Leon Han963dc182018-11-06 05:41:483365 params.referrer.To<Referrer>(),
3366 params.disposition, ui::PAGE_TRANSITION_LINK,
Charlie Harrisonf60dd1c2018-08-15 21:15:413367 false, // started_from_context_menu
3368 true); // renderer_initiated
pnolandaae574e2017-03-06 21:04:213369 }
3370
Avi Drissmanba3b1b342017-08-29 18:16:133371 // Any new WebContents opened while this WebContents is in fullscreen can be
3372 // used to confuse the user, so drop fullscreen.
Avi Drissman1a55a9d62020-03-10 18:56:453373 base::ScopedClosureRunner fullscreen_block = ForSecurityDropFullscreen();
3374 // The new contents will be independent of this contents, so release the
3375 // fullscreen block.
3376 fullscreen_block.RunAndReset();
Avi Drissmanba3b1b342017-08-29 18:16:133377
[email protected]bafe6cd2012-05-23 23:09:503378 if (params.opener_suppressed) {
3379 // When the opener is suppressed, the original renderer cannot access the
3380 // new window. As a result, we need to show and navigate the window here.
[email protected]eda238a12012-09-07 23:44:003381 bool was_blocked = false;
erikchenbee5c9622018-04-27 19:30:253382
[email protected]eda238a12012-09-07 23:44:003383 if (delegate_) {
danakje6c01622018-10-05 19:39:263384 base::WeakPtr<WebContentsImpl> weak_new_contents =
3385 new_contents_impl->weak_factory_.GetWeakPtr();
nick0d558482017-02-13 22:27:223386
danakje6c01622018-10-05 19:39:263387 gfx::Rect initial_rect; // Report an empty initial rect.
Albert J. Wong65fe64d2019-09-20 02:48:143388 delegate_->AddNewContents(this, std::move(new_contents),
Joel Hockey891e88062020-04-30 05:38:443389 params.target_url, params.disposition,
3390 initial_rect, has_user_gesture, &was_blocked);
danakje6c01622018-10-05 19:39:263391 // The delegate may delete |new_contents_impl| during AddNewContents().
nick0d558482017-02-13 22:27:223392 if (!weak_new_contents)
Albert J. Wong65fe64d2019-09-20 02:48:143393 return nullptr;
[email protected]eda238a12012-09-07 23:44:003394 }
erikchenbee5c9622018-04-27 19:30:253395
[email protected]eda238a12012-09-07 23:44:003396 if (!was_blocked) {
Nasko Oskov83a8cf92018-10-19 14:58:563397 std::unique_ptr<NavigationController::LoadURLParams> load_params =
3398 std::make_unique<NavigationController::LoadURLParams>(
3399 params.target_url);
Nasko Oskov93e7c55c2018-12-19 01:59:293400 load_params->initiator_origin = opener->GetLastCommittedOrigin();
Aaron Colwell9dab1652019-12-09 18:29:493401 load_params->source_site_instance = source_site_instance;
Leon Han963dc182018-11-06 05:41:483402 load_params->referrer = params.referrer.To<Referrer>();
Nasko Oskov83a8cf92018-10-19 14:58:563403 load_params->transition_type = ui::PAGE_TRANSITION_LINK;
3404 load_params->is_renderer_initiated = true;
Sergey Kuznetsov32257a22019-02-11 20:26:503405 load_params->has_user_gesture = has_user_gesture;
mariakhomenkoa4971c12015-07-21 19:04:373406
3407 if (delegate_ && !is_guest &&
3408 !delegate_->ShouldResumeRequestsForCreatedWindow()) {
Nasko Oskov83a8cf92018-10-19 14:58:563409 // We are in asynchronous add new contents path, delay navigation.
3410 DCHECK(!new_contents_impl->delayed_open_url_params_);
3411 new_contents_impl->delayed_load_url_params_ = std::move(load_params);
mariakhomenkoa4971c12015-07-21 19:04:373412 } else {
Nasko Oskov83a8cf92018-10-19 14:58:563413 new_contents_impl->controller_.LoadURLWithParams(*load_params.get());
Nasko Oskov28ebd5b2018-12-07 22:29:333414 if (!is_guest)
3415 new_contents_impl->Focus();
mariakhomenkoa4971c12015-07-21 19:04:373416 }
[email protected]eda238a12012-09-07 23:44:003417 }
[email protected]bafe6cd2012-05-23 23:09:503418 }
Albert J. Wong65fe64d2019-09-20 02:48:143419 return new_contents_impl;
[email protected]bafe6cd2012-05-23 23:09:503420}
3421
Dave Tapuskad9835622020-03-10 17:32:313422void WebContentsImpl::CreateNewWidget(
3423 int32_t render_process_id,
3424 int32_t widget_route_id,
Dave Tapuska8499eec2020-03-16 17:54:303425 mojo::PendingAssociatedReceiver<blink::mojom::WidgetHost> blink_widget_host,
3426 mojo::PendingAssociatedRemote<blink::mojom::Widget> blink_widget) {
Albert J. Wongf6e13ed2018-09-18 15:25:473427 CreateNewWidget(render_process_id, widget_route_id, /*is_fullscreen=*/false,
Dave Tapuskab686c1b2020-06-09 00:40:233428 std::move(blink_widget_host), std::move(blink_widget));
[email protected]bafe6cd2012-05-23 23:09:503429}
3430
Julie Jeongeun Kimcaacf9102019-09-12 02:13:053431void WebContentsImpl::CreateNewFullscreenWidget(
3432 int32_t render_process_id,
3433 int32_t widget_route_id,
Dave Tapuska8499eec2020-03-16 17:54:303434 mojo::PendingAssociatedReceiver<blink::mojom::WidgetHost> blink_widget_host,
3435 mojo::PendingAssociatedRemote<blink::mojom::Widget> blink_widget) {
Albert J. Wongf6e13ed2018-09-18 15:25:473436 CreateNewWidget(render_process_id, widget_route_id, /*is_fullscreen=*/true,
Dave Tapuskab686c1b2020-06-09 00:40:233437 std::move(blink_widget_host), std::move(blink_widget));
[email protected]bafe6cd2012-05-23 23:09:503438}
3439
Dave Tapuskad9835622020-03-10 17:32:313440void WebContentsImpl::CreateNewWidget(
3441 int32_t render_process_id,
3442 int32_t route_id,
3443 bool is_fullscreen,
Dave Tapuska8499eec2020-03-16 17:54:303444 mojo::PendingAssociatedReceiver<blink::mojom::WidgetHost> blink_widget_host,
3445 mojo::PendingAssociatedRemote<blink::mojom::Widget> blink_widget) {
kenrba26d15092016-02-10 22:00:253446 RenderProcessHost* process = RenderProcessHost::FromID(render_process_id);
kenrbb46f86a2015-12-07 07:53:233447 // A message to create a new widget can only come from an active process for
[email protected]a8504022013-12-04 20:23:513448 // this WebContentsImpl instance. If any other process sends the request,
3449 // it is invalid and the process must be terminated.
dcheng57e39e22016-01-21 00:25:383450 if (!HasMatchingProcess(&frame_tree_, render_process_id)) {
Lukasz Anforowicz6f746282018-01-04 23:24:513451 ReceivedBadMessage(process, bad_message::WCI_NEW_WIDGET_PROCESS_MISMATCH);
[email protected]a8504022013-12-04 20:23:513452 return;
3453 }
3454
Dave Tapuskab686c1b2020-06-09 00:40:233455 RenderWidgetHostImpl* widget_host =
3456 new RenderWidgetHostImpl(this, process, route_id, IsHidden(),
3457 std::make_unique<FrameTokenMessageQueue>());
Dave Tapuska8499eec2020-03-16 17:54:303458
3459 widget_host->BindWidgetInterfaces(std::move(blink_widget_host),
3460 std::move(blink_widget));
[email protected]cfd80b02014-05-01 17:46:483461 RenderWidgetHostViewBase* widget_view =
3462 static_cast<RenderWidgetHostViewBase*>(
danakj0b246942018-09-17 21:30:273463 view_->CreateViewForChildWidget(widget_host));
[email protected]83918ec2013-01-10 15:37:193464 if (!widget_view)
3465 return;
danakj0b246942018-09-17 21:30:273466 // Fullscreen child widgets are frames, other child widgets are popups, and
3467 // popups should not get activated.
3468 if (!is_fullscreen)
3469 widget_view->SetWidgetType(WidgetType::kPopup);
[email protected]bafe6cd2012-05-23 23:09:503470 // Save the created widget associated with the route so we can show it later.
Lukasz Anforowicz09060bdf72018-08-23 15:53:173471 pending_widget_views_[GlobalRoutingID(render_process_id, route_id)] =
alexmosc2a8cec2016-05-23 22:19:533472 widget_view;
[email protected]bafe6cd2012-05-23 23:09:503473}
3474
alexmosc2a8cec2016-05-23 22:19:533475void WebContentsImpl::ShowCreatedWindow(int process_id,
nick5ae4d2d2017-01-06 01:18:353476 int main_frame_widget_route_id,
[email protected]bafe6cd2012-05-23 23:09:503477 WindowOpenDisposition disposition,
bokan107a47f2015-02-03 23:23:393478 const gfx::Rect& initial_rect,
[email protected]bafe6cd2012-05-23 23:09:503479 bool user_gesture) {
danakje6c01622018-10-05 19:39:263480 // This method is the renderer requesting an existing top level window to
3481 // show a new top level window that the renderer created. Each top level
3482 // window is associated with a WebContents. In this case it was created
3483 // earlier but showing it was deferred until the renderer requested for it
3484 // to be shown. We find that previously created WebContents here.
3485 // TODO(danakj): Why do we defer this show step until the renderer asks for it
3486 // when it will always do so. What needs to happen in the renderer before we
3487 // reach here?
Joel Hockey8c2011b22020-04-30 05:07:193488 base::Optional<CreatedWindow> owned_created =
nick5ae4d2d2017-01-06 01:18:353489 GetCreatedWindow(process_id, main_frame_widget_route_id);
Joel Hockey8c2011b22020-04-30 05:07:193490
danakje6c01622018-10-05 19:39:263491 // The browser may have rejected the request to make a new window, or the
3492 // renderer could be sending an invalid route id. Ignore the request then.
Joel Hockey8c2011b22020-04-30 05:07:193493 if (!owned_created || !owned_created->contents)
danakje6c01622018-10-05 19:39:263494 return;
mariakhomenko44bdc4732015-04-29 01:55:383495
Joel Hockey8c2011b22020-04-30 05:07:193496 WebContentsImpl* created = owned_created->contents.get();
3497
danakje6c01622018-10-05 19:39:263498 // This uses the delegate for the WebContents where the window was created
3499 // from, to control how to show the newly created window.
3500 WebContentsDelegate* delegate = GetDelegate();
nick5ae4d2d2017-01-06 01:18:353501
danakje6c01622018-10-05 19:39:263502 // The delegate can be null in tests, so we must check for it :(.
3503 if (delegate) {
3504 // Mark the web contents as pending resume, then immediately do
3505 // the resume if the delegate wants it.
3506 created->is_resume_pending_ = true;
3507 if (delegate->ShouldResumeRequestsForCreatedWindow())
3508 created->ResumeLoadingCreatedWebContents();
3509
3510 base::WeakPtr<WebContentsImpl> weak_created =
3511 created->weak_factory_.GetWeakPtr();
Joel Hockey8c2011b22020-04-30 05:07:193512 delegate->AddNewContents(this, std::move(owned_created->contents),
Joel Hockey891e88062020-04-30 05:38:443513 std::move(owned_created->target_url), disposition,
3514 initial_rect, user_gesture, nullptr);
danakje6c01622018-10-05 19:39:263515 // The delegate may delete |created| during AddNewContents().
3516 if (!weak_created)
3517 return;
[email protected]eda238a12012-09-07 23:44:003518 }
danakje6c01622018-10-05 19:39:263519
3520 RenderWidgetHostImpl* rwh = created->GetMainFrame()->GetRenderWidgetHost();
3521 DCHECK_EQ(main_frame_widget_route_id, rwh->GetRoutingID());
3522 rwh->Send(new WidgetMsg_SetBounds_ACK(rwh->GetRoutingID()));
[email protected]bafe6cd2012-05-23 23:09:503523}
3524
alexmosc2a8cec2016-05-23 22:19:533525void WebContentsImpl::ShowCreatedWidget(int process_id,
Albert J. Wongf6e13ed2018-09-18 15:25:473526 int widget_route_id,
bokan107a47f2015-02-03 23:23:393527 const gfx::Rect& initial_rect) {
Albert J. Wongf6e13ed2018-09-18 15:25:473528 ShowCreatedWidget(process_id, widget_route_id, false, initial_rect);
[email protected]bafe6cd2012-05-23 23:09:503529}
3530
alexmosc2a8cec2016-05-23 22:19:533531void WebContentsImpl::ShowCreatedFullscreenWidget(int process_id,
Albert J. Wongf6e13ed2018-09-18 15:25:473532 int widget_route_id) {
3533 ShowCreatedWidget(process_id, widget_route_id, true, gfx::Rect());
[email protected]bafe6cd2012-05-23 23:09:503534}
3535
alexmosc2a8cec2016-05-23 22:19:533536void WebContentsImpl::ShowCreatedWidget(int process_id,
3537 int route_id,
[email protected]bafe6cd2012-05-23 23:09:503538 bool is_fullscreen,
bokan107a47f2015-02-03 23:23:393539 const gfx::Rect& initial_rect) {
[email protected]cfd80b02014-05-01 17:46:483540 RenderWidgetHostViewBase* widget_host_view =
alexmosc2a8cec2016-05-23 22:19:533541 static_cast<RenderWidgetHostViewBase*>(
3542 GetCreatedWidget(process_id, route_id));
[email protected]bafe6cd2012-05-23 23:09:503543 if (!widget_host_view)
3544 return;
[email protected]cfd80b02014-05-01 17:46:483545
W. James MacLeanec4125c2019-09-27 18:56:003546 // GetOutermostWebContents() returns |this| if there are no outer WebContents.
W. James MacLean3c5f50462019-10-28 16:24:213547 auto* outer_web_contents = GetOuterWebContents();
3548 auto* outermost_web_contents = GetOutermostWebContents();
W. James MacLeanec4125c2019-09-27 18:56:003549 RenderWidgetHostView* view =
W. James MacLean3c5f50462019-10-28 16:24:213550 outermost_web_contents->GetRenderWidgetHostView();
3551 // It's not entirely obvious why we need the transform only in the case where
3552 // the outer webcontents is not the same as the outermost webcontents. It may
3553 // be due to the fact that oopifs that are children of the mainframe get
3554 // correct values for their screenrects, but deeper cross-process frames do
3555 // not. Hopefully this can be resolved with https://crbug.com/928825.
3556 // Handling these cases separately is needed for http://crbug.com/1015298.
3557 bool needs_transform = this != outermost_web_contents &&
3558 outermost_web_contents != outer_web_contents;
W. James MacLeanec4125c2019-09-27 18:56:003559
3560 gfx::Rect transformed_rect(initial_rect);
3561 RenderWidgetHostView* this_view = GetRenderWidgetHostView();
W. James MacLean3c5f50462019-10-28 16:24:213562 if (needs_transform) {
W. James MacLeanec4125c2019-09-27 18:56:003563 // We need to transform the coordinates of initial_rect.
3564 gfx::Point origin =
3565 this_view->TransformPointToRootCoordSpace(initial_rect.origin());
3566 gfx::Point bottom_right =
3567 this_view->TransformPointToRootCoordSpace(initial_rect.bottom_right());
3568 transformed_rect =
3569 gfx::Rect(origin.x(), origin.y(), bottom_right.x() - origin.x(),
3570 bottom_right.y() - origin.y());
[email protected]cfd80b02014-05-01 17:46:483571 }
3572
danakj0b246942018-09-17 21:30:273573 // Fullscreen child widgets are frames, other child widgets are popups.
[email protected]4aebbca2013-09-17 22:26:493574 if (is_fullscreen) {
[email protected]d7f80ba2013-10-12 07:42:313575 DCHECK_EQ(MSG_ROUTING_NONE, fullscreen_widget_routing_id_);
[email protected]0b431992014-06-24 00:08:033576 view_->StoreFocus();
alexmosc9e76ec2016-05-16 22:59:373577 fullscreen_widget_process_id_ =
3578 widget_host_view->GetRenderWidgetHost()->GetProcess()->GetID();
[email protected]d7f80ba2013-10-12 07:42:313579 fullscreen_widget_routing_id_ = route_id;
[email protected]4aebbca2013-09-17 22:26:493580 if (delegate_ && delegate_->EmbedsFullscreenWidget()) {
3581 widget_host_view->InitAsChild(GetRenderWidgetHostView()->GetNativeView());
Mike Wasserman4ca09792020-05-29 17:44:433582 delegate_->EnterFullscreenModeForTab(GetMainFrame(),
Dave Tapuskaa4189512019-10-15 20:27:343583 blink::mojom::FullscreenOptions());
[email protected]4aebbca2013-09-17 22:26:493584 } else {
[email protected]cfd80b02014-05-01 17:46:483585 widget_host_view->InitAsFullscreen(view);
[email protected]4aebbca2013-09-17 22:26:493586 }
ericwilligersde386342016-10-19 02:35:093587 for (auto& observer : observers_)
3588 observer.DidShowFullscreenWidget();
[email protected]4aebbca2013-09-17 22:26:493589 if (!widget_host_view->HasFocus())
3590 widget_host_view->Focus();
3591 } else {
W. James MacLeanec4125c2019-09-27 18:56:003592 widget_host_view->InitAsPopup(view, transformed_rect);
[email protected]4aebbca2013-09-17 22:26:493593 }
[email protected]89054502012-06-03 10:29:243594
yiyixcb1fbc4f2018-03-16 19:54:083595 RenderWidgetHostImpl* render_widget_host_impl = widget_host_view->host();
[email protected]89054502012-06-03 10:29:243596 render_widget_host_impl->Init();
[email protected]d7f80ba2013-10-12 07:42:313597 // Only allow privileged mouse lock for fullscreen render widget, which is
3598 // used to implement Pepper Flash fullscreen.
3599 render_widget_host_impl->set_allow_privileged_mouse_lock(is_fullscreen);
[email protected]bafe6cd2012-05-23 23:09:503600}
3601
Joel Hockey8c2011b22020-04-30 05:07:193602base::Optional<CreatedWindow> WebContentsImpl::GetCreatedWindow(
nick5ae4d2d2017-01-06 01:18:353603 int process_id,
3604 int main_frame_widget_route_id) {
Lukasz Anforowicz09060bdf72018-08-23 15:53:173605 auto key = GlobalRoutingID(process_id, main_frame_widget_route_id);
nick5ae4d2d2017-01-06 01:18:353606 auto iter = pending_contents_.find(key);
[email protected]bafe6cd2012-05-23 23:09:503607
3608 // Certain systems can block the creation of new windows. If we didn't succeed
3609 // in creating one, just return NULL.
alexmosc2a8cec2016-05-23 22:19:533610 if (iter == pending_contents_.end())
Joel Hockey8c2011b22020-04-30 05:07:193611 return base::nullopt;
[email protected]bafe6cd2012-05-23 23:09:503612
Joel Hockey8c2011b22020-04-30 05:07:193613 CreatedWindow result = std::move(iter->second);
3614 WebContentsImpl* new_contents = result.contents.get();
nick5ae4d2d2017-01-06 01:18:353615 pending_contents_.erase(key);
Joel Hockey8c2011b22020-04-30 05:07:193616 RemoveDestructionObserver(new_contents);
[email protected]bafe6cd2012-05-23 23:09:503617
[email protected]d70bea92013-04-05 04:23:343618 // Don't initialize the guest WebContents immediately.
Joel Hockey8c2011b22020-04-30 05:07:193619 if (BrowserPluginGuest::IsGuest(new_contents))
3620 return result;
[email protected]d70bea92013-04-05 04:23:343621
Lukasz Anforowicz5510ed652018-06-06 16:16:193622 if (!new_contents->GetMainFrame()->GetProcess()->IsInitializedAndNotDead() ||
nick5ae4d2d2017-01-06 01:18:353623 !new_contents->GetMainFrame()->GetView()) {
Joel Hockey8c2011b22020-04-30 05:07:193624 return base::nullopt;
nick5ae4d2d2017-01-06 01:18:353625 }
[email protected]bafe6cd2012-05-23 23:09:503626
Joel Hockey8c2011b22020-04-30 05:07:193627 return result;
[email protected]bafe6cd2012-05-23 23:09:503628}
3629
alexmosc2a8cec2016-05-23 22:19:533630RenderWidgetHostView* WebContentsImpl::GetCreatedWidget(int process_id,
3631 int route_id) {
Lukasz Anforowicz09060bdf72018-08-23 15:53:173632 auto iter = pending_widget_views_.find(GlobalRoutingID(process_id, route_id));
[email protected]bafe6cd2012-05-23 23:09:503633 if (iter == pending_widget_views_.end()) {
3634 DCHECK(false);
alexmosc2a8cec2016-05-23 22:19:533635 return nullptr;
[email protected]bafe6cd2012-05-23 23:09:503636 }
3637
3638 RenderWidgetHostView* widget_host_view = iter->second;
Lukasz Anforowicz09060bdf72018-08-23 15:53:173639 pending_widget_views_.erase(GlobalRoutingID(process_id, route_id));
[email protected]bafe6cd2012-05-23 23:09:503640
3641 RenderWidgetHost* widget_host = widget_host_view->GetRenderWidgetHost();
Lukasz Anforowicz5510ed652018-06-06 16:16:193642 if (!widget_host->GetProcess()->IsInitializedAndNotDead()) {
[email protected]bafe6cd2012-05-23 23:09:503643 // The view has gone away or the renderer crashed. Nothing to do.
alexmosc2a8cec2016-05-23 22:19:533644 return nullptr;
[email protected]bafe6cd2012-05-23 23:09:503645 }
3646
3647 return widget_host_view;
3648}
3649
[email protected]f13b4202012-06-12 23:53:233650void WebContentsImpl::RequestMediaAccessPermission(
[email protected]33662e52013-01-07 21:31:093651 const MediaStreamRequest& request,
Mark Pilgrim57499082018-06-12 12:38:303652 MediaResponseCallback callback) {
[email protected]d19b84b2014-03-14 11:52:373653 if (delegate_) {
Mark Pilgrim57499082018-06-12 12:38:303654 delegate_->RequestMediaAccessPermission(this, request, std::move(callback));
[email protected]d19b84b2014-03-14 11:52:373655 } else {
Antonio Gomes0d42960a2019-06-05 12:35:513656 std::move(callback).Run(
3657 blink::MediaStreamDevices(),
3658 blink::mojom::MediaStreamRequestResult::FAILED_DUE_TO_SHUTDOWN,
3659 std::unique_ptr<MediaStreamUI>());
[email protected]d19b84b2014-03-14 11:52:373660 }
[email protected]f13b4202012-06-12 23:53:233661}
3662
Chandan Padhia4b8bcb72017-09-12 16:41:043663bool WebContentsImpl::CheckMediaAccessPermission(
Raymes Khouryad7c24a12018-03-05 23:22:583664 RenderFrameHost* render_frame_host,
Chandan Padhia4b8bcb72017-09-12 16:41:043665 const url::Origin& security_origin,
Antonio Gomesc8b734b2019-06-05 18:22:163666 blink::mojom::MediaStreamType type) {
3667 DCHECK(type == blink::mojom::MediaStreamType::DEVICE_AUDIO_CAPTURE ||
3668 type == blink::mojom::MediaStreamType::DEVICE_VIDEO_CAPTURE);
Chandan Padhia4b8bcb72017-09-12 16:41:043669 return delegate_ && delegate_->CheckMediaAccessPermission(
Raymes Khouryad7c24a12018-03-05 23:22:583670 render_frame_host, security_origin.GetURL(), type);
grunell657d4d82014-09-18 00:09:433671}
3672
Guido Urdaneta73fa6632019-01-14 18:46:263673std::string WebContentsImpl::GetDefaultMediaDeviceID(
Antonio Gomesc8b734b2019-06-05 18:22:163674 blink::mojom::MediaStreamType type) {
guidou8a440b842017-01-30 13:58:433675 if (!delegate_)
3676 return std::string();
3677 return delegate_->GetDefaultMediaDeviceID(this, type);
3678}
3679
[email protected]cc9200432013-07-23 23:02:403680SessionStorageNamespace* WebContentsImpl::GetSessionStorageNamespace(
3681 SiteInstance* instance) {
3682 return controller_.GetSessionStorageNamespace(instance);
3683}
3684
[email protected]6de7fc482014-06-06 10:46:443685SessionStorageNamespaceMap WebContentsImpl::GetSessionStorageNamespaceMap() {
3686 return controller_.GetSessionStorageNamespaceMap();
3687}
3688
[email protected]9b159a52013-10-03 17:24:553689FrameTree* WebContentsImpl::GetFrameTree() {
3690 return &frame_tree_;
3691}
3692
aelias5252baa2016-04-10 01:18:023693bool WebContentsImpl::IsOverridingUserAgent() {
3694 return GetController().GetVisibleEntry() &&
3695 GetController().GetVisibleEntry()->GetIsOverridingUserAgent();
3696}
3697
avi85ee8362016-10-08 02:09:083698bool WebContentsImpl::IsJavaScriptDialogShowing() const {
3699 return is_showing_javascript_dialog_;
3700}
3701
avic031d392017-03-03 03:09:423702bool WebContentsImpl::ShouldIgnoreUnresponsiveRenderer() {
Dominique Fauteux-Chapleau99244382020-07-08 20:37:003703 if (suppress_unresponsive_renderer_count_ > 0)
3704 return true;
3705
avic031d392017-03-03 03:09:423706 // Ignore unresponsive renderers if the debugger is attached to them since the
3707 // unresponsiveness might be a result of the renderer sitting on a breakpoint.
3708 //
David Bienvenu525f8a02019-02-15 02:02:423709#ifdef OS_WIN
3710 // Check if a windows debugger is attached to the renderer process.
3711 base::ProcessHandle process_handle =
3712 GetMainFrame()->GetProcess()->GetProcess().Handle();
3713 BOOL debugger_present = FALSE;
3714 if (CheckRemoteDebuggerPresent(process_handle, &debugger_present) &&
3715 debugger_present)
3716 return true;
3717#endif // OS_WIN
3718
avic031d392017-03-03 03:09:423719 // TODO(pfeldman): Fix this to only return true if the renderer is *actually*
3720 // sitting on a breakpoint. https://crbug.com/684202
3721 return DevToolsAgentHost::IsDebuggerAttached(this);
3722}
3723
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:413724ui::AXMode WebContentsImpl::GetAccessibilityMode() {
[email protected]95640212014-07-26 18:14:303725 return accessibility_mode_;
3726}
3727
Mario Sanchez Prada5d7f1ac2020-07-16 17:18:403728void WebContentsImpl::AXTreeIDForMainFrameHasChanged() {
3729 RenderWidgetHostViewBase* rwhv =
3730 static_cast<RenderWidgetHostViewBase*>(GetRenderWidgetHostView());
3731 if (rwhv)
3732 rwhv->SetMainFrameAXTreeID(GetMainFrame()->GetAXTreeID());
3733
3734 for (auto& observer : observers_)
3735 observer.AXTreeIDForMainFrameHasChanged();
3736}
3737
Dominic Mazzonia7b0edb22017-08-09 16:32:513738void WebContentsImpl::AccessibilityEventReceived(
Dominic Mazzoniccbaa9b2018-06-06 07:44:233739 const AXEventNotificationDetails& details) {
ericwilligersde386342016-10-19 02:35:093740 for (auto& observer : observers_)
Dominic Mazzonia7b0edb22017-08-09 16:32:513741 observer.AccessibilityEventReceived(details);
[email protected]31a71eaf2014-03-13 01:47:363742}
3743
dmazzoni2400c462016-08-23 15:07:133744void WebContentsImpl::AccessibilityLocationChangesReceived(
3745 const std::vector<AXLocationChangeNotificationDetails>& details) {
ericwilligersde386342016-10-19 02:35:093746 for (auto& observer : observers_)
Dominic Mazzonia7b0edb22017-08-09 16:32:513747 observer.AccessibilityLocationChangesReceived(details);
dmazzoni2400c462016-08-23 15:07:133748}
3749
Abigail Kleinabb428332019-09-13 18:26:213750base::string16 WebContentsImpl::DumpAccessibilityTree(
3751 bool internal,
3752 std::vector<AccessibilityTreeFormatter::PropertyFilter> property_filters) {
James Wallace-Leeeafc94cb92018-07-23 21:35:093753 auto* ax_mgr = GetOrCreateRootBrowserAccessibilityManager();
3754 DCHECK(ax_mgr);
Lei Zhangc98caa3822019-11-07 19:17:273755 return AccessibilityTreeFormatterBase::DumpAccessibilityTreeFromManager(
Abigail Kleinabb428332019-09-13 18:26:213756 ax_mgr, internal, property_filters);
James Wallace-Leeeafc94cb92018-07-23 21:35:093757}
3758
Abigail Kleinaa898742019-11-01 02:31:253759void WebContentsImpl::RecordAccessibilityEvents(
Abigail Kleinbb8304bd2020-05-18 21:44:183760 bool start_recording,
3761 base::Optional<AccessibilityEventCallback> callback) {
3762 // Only pass a callback to RecordAccessibilityEvents when starting to record.
3763 DCHECK_EQ(start_recording, callback.has_value());
3764 if (start_recording) {
Abigail Kleinaa898742019-11-01 02:31:253765 SetAccessibilityMode(ui::AXMode::kWebContents);
3766 auto* ax_mgr = GetOrCreateRootBrowserAccessibilityManager();
3767 DCHECK(ax_mgr);
3768 base::ProcessId pid = base::Process::Current().Pid();
3769 event_recorder_ = content::AccessibilityEventRecorder::Create(
3770 ax_mgr, pid, base::StringPiece{});
Abigail Kleinbb8304bd2020-05-18 21:44:183771 event_recorder_->ListenToEvents(*callback);
Abigail Kleinaa898742019-11-01 02:31:253772 } else {
Abigail Kleinbb8304bd2020-05-18 21:44:183773 if (event_recorder_) {
3774 event_recorder_->FlushAsyncEvents();
3775 event_recorder_.reset(nullptr);
3776 }
Abigail Kleinaa898742019-11-01 02:31:253777 }
3778}
3779
dmazzonia656928e2014-09-15 20:28:543780RenderFrameHost* WebContentsImpl::GetGuestByInstanceID(
fsamuel833ee7c2015-02-13 23:40:403781 RenderFrameHost* render_frame_host,
dmazzonia656928e2014-09-15 20:28:543782 int browser_plugin_instance_id) {
3783 BrowserPluginGuestManager* guest_manager =
3784 GetBrowserContext()->GetGuestManager();
fsamuel9cbc00a2015-04-17 20:30:203785 if (!guest_manager)
3786 return nullptr;
3787
dmazzonia656928e2014-09-15 20:28:543788 WebContents* guest = guest_manager->GetGuestByInstanceID(
fsamuel2e9413d2015-02-25 01:25:443789 render_frame_host->GetProcess()->GetID(), browser_plugin_instance_id);
dmazzonia656928e2014-09-15 20:28:543790 if (!guest)
fsamuel9cbc00a2015-04-17 20:30:203791 return nullptr;
3792
dmazzonia656928e2014-09-15 20:28:543793 return guest->GetMainFrame();
3794}
3795
Ke He7319dbe2017-11-09 05:54:443796device::mojom::GeolocationContext* WebContentsImpl::GetGeolocationContext() {
Ella Ge3e375b702020-06-05 15:57:163797 if (delegate_) {
3798 auto* installed_webapp_context =
3799 delegate_->GetInstalledWebappGeolocationContext();
3800 if (installed_webapp_context)
3801 return installed_webapp_context;
3802 }
3803
Ken Rockotce010f02019-12-12 23:32:323804 if (!geolocation_context_) {
3805 GetDeviceService().BindGeolocationContext(
3806 geolocation_context_.BindNewPipeAndPassReceiver());
3807 }
Conley Owens6894c4f2017-07-10 19:29:133808 return geolocation_context_.get();
blundellc57b93f2014-10-29 13:19:573809}
3810
ke.he98b761e2017-05-09 05:59:173811device::mojom::WakeLockContext* WebContentsImpl::GetWakeLockContext() {
blundelld8cd72b2017-03-28 07:18:383812 if (!wake_lock_context_host_)
3813 wake_lock_context_host_.reset(new WakeLockContextHost(this));
blundelle75a8f92017-03-27 08:11:173814 return wake_lock_context_host_->GetWakeLockContext();
alogvinovf50445a2015-10-30 13:00:123815}
3816
blundellf5316fc2017-05-15 11:49:033817#if defined(OS_ANDROID)
Miyoung Shin59f39ff2019-09-17 07:23:203818void WebContentsImpl::GetNFC(
Reilly Grantafbe2242020-02-20 21:14:553819 RenderFrameHost* render_frame_host,
Miyoung Shin59f39ff2019-09-17 07:23:203820 mojo::PendingReceiver<device::mojom::NFC> receiver) {
Reilly Grantafbe2242020-02-20 21:14:553821 if (!nfc_host_)
3822 nfc_host_ = std::make_unique<NFCHost>(this);
3823 nfc_host_->GetNFC(render_frame_host, std::move(receiver));
blundellf5316fc2017-05-15 11:49:033824}
3825#endif
3826
Evan Stade5e750ad02017-08-07 21:59:273827void WebContentsImpl::SetNotWaitingForResponse() {
3828 if (waiting_for_response_ == false)
3829 return;
3830
3831 waiting_for_response_ = false;
3832 if (delegate_)
3833 delegate_->LoadingStateChanged(this, is_load_to_different_document_);
Chris Hamiltonf82f2a702018-02-15 19:37:293834 for (auto& observer : observers_)
3835 observer.DidReceiveResponse();
Evan Stade5e750ad02017-08-07 21:59:273836}
3837
lfgbb9c28a2016-03-01 03:19:493838void WebContentsImpl::SendScreenRects() {
kenrbc52e1b4e2016-03-20 01:03:373839 for (FrameTreeNode* node : frame_tree_.Nodes()) {
kenrb0e8dc202016-05-30 19:51:403840 if (node->current_frame_host()->is_local_root())
kenrbc52e1b4e2016-03-20 01:03:373841 node->current_frame_host()->GetRenderWidgetHost()->SendScreenRects();
3842 }
[email protected]32deec62013-05-15 23:55:043843}
3844
ekaramadadd882292016-06-08 15:22:563845TextInputManager* WebContentsImpl::GetTextInputManager() {
3846 if (GetOuterWebContents())
3847 return GetOuterWebContents()->GetTextInputManager();
3848
Ehsan Karamadc179dc02018-12-15 03:09:293849 if (!text_input_manager_ && !browser_plugin_guest_) {
Darren Shenca53d5f2018-05-15 04:56:013850 text_input_manager_.reset(new TextInputManager(
3851 GetBrowserContext() &&
3852 !GetBrowserContext()->IsOffTheRecord()) /* should_do_learning */);
3853 }
ekaramadadd882292016-06-08 15:22:563854
3855 return text_input_manager_.get();
3856}
3857
paulmeyer7f6f1d4f2016-11-15 00:00:273858bool WebContentsImpl::OnUpdateDragCursor() {
Lei Zhang6410ec1a2019-01-31 21:38:583859 return browser_plugin_embedder_ &&
3860 browser_plugin_embedder_->OnUpdateDragCursor();
paulmeyer7f6f1d4f2016-11-15 00:00:273861}
3862
kenrb11f213a2017-03-24 18:12:063863bool WebContentsImpl::IsWidgetForMainFrame(
3864 RenderWidgetHostImpl* render_widget_host) {
3865 return render_widget_host == GetMainFrame()->GetRenderWidgetHost();
3866}
3867
[email protected]95640212014-07-26 18:14:303868BrowserAccessibilityManager*
3869 WebContentsImpl::GetRootBrowserAccessibilityManager() {
Carlos ILd51e7702020-05-07 18:51:393870 RenderFrameHostImpl* rfh = static_cast<RenderFrameHostImpl*>(GetMainFrame());
creisc014b402015-04-23 16:41:453871 return rfh ? rfh->browser_accessibility_manager() : nullptr;
[email protected]95640212014-07-26 18:14:303872}
3873
3874BrowserAccessibilityManager*
3875 WebContentsImpl::GetOrCreateRootBrowserAccessibilityManager() {
Carlos ILd51e7702020-05-07 18:51:393876 RenderFrameHostImpl* rfh = static_cast<RenderFrameHostImpl*>(GetMainFrame());
creisc014b402015-04-23 16:41:453877 return rfh ? rfh->GetOrCreateBrowserAccessibilityManager() : nullptr;
[email protected]95640212014-07-26 18:14:303878}
3879
Dave Tapuskab336b922017-07-06 19:24:053880void WebContentsImpl::ExecuteEditCommand(
3881 const std::string& command,
3882 const base::Optional<base::string16>& value) {
Dave Tapuska58a099e2020-06-08 21:48:403883 auto* input_handler = GetFocusedFrameWidgetInputHandler();
Dave Tapuskae782bea2019-07-09 16:21:513884 if (!input_handler)
Dave Tapuskab336b922017-07-06 19:24:053885 return;
3886
Dave Tapuskae782bea2019-07-09 16:21:513887 input_handler->ExecuteEditCommand(command, value);
Dave Tapuskab336b922017-07-06 19:24:053888}
3889
mohsen7ab1ec16ec2015-07-02 18:26:233890void WebContentsImpl::MoveRangeSelectionExtent(const gfx::Point& extent) {
Dave Tapuska58a099e2020-06-08 21:48:403891 auto* input_handler = GetFocusedFrameWidgetInputHandler();
Dave Tapuskae782bea2019-07-09 16:21:513892 if (!input_handler)
mohsen7ab1ec16ec2015-07-02 18:26:233893 return;
3894
Dave Tapuskae782bea2019-07-09 16:21:513895 input_handler->MoveRangeSelectionExtent(extent);
mohsen7ab1ec16ec2015-07-02 18:26:233896}
3897
3898void WebContentsImpl::SelectRange(const gfx::Point& base,
3899 const gfx::Point& extent) {
Dave Tapuska58a099e2020-06-08 21:48:403900 auto* input_handler = GetFocusedFrameWidgetInputHandler();
Dave Tapuskae782bea2019-07-09 16:21:513901 if (!input_handler)
mohsen7ab1ec16ec2015-07-02 18:26:233902 return;
3903
Dave Tapuskae782bea2019-07-09 16:21:513904 input_handler->SelectRange(base, extent);
mohsen7ab1ec16ec2015-07-02 18:26:233905}
3906
Dave Tapuskab336b922017-07-06 19:24:053907void WebContentsImpl::MoveCaret(const gfx::Point& extent) {
Dave Tapuska58a099e2020-06-08 21:48:403908 auto* input_handler = GetFocusedFrameWidgetInputHandler();
Dave Tapuskae782bea2019-07-09 16:21:513909 if (!input_handler)
Dave Tapuskab336b922017-07-06 19:24:053910 return;
3911
Dave Tapuskae782bea2019-07-09 16:21:513912 input_handler->MoveCaret(extent);
Dave Tapuskab336b922017-07-06 19:24:053913}
3914
Shimi Zhang37deeb22017-09-28 00:59:013915void WebContentsImpl::AdjustSelectionByCharacterOffset(
3916 int start_adjust,
3917 int end_adjust,
3918 bool show_selection_menu) {
Dave Tapuska58a099e2020-06-08 21:48:403919 auto* input_handler = GetFocusedFrameWidgetInputHandler();
Dave Tapuskae782bea2019-07-09 16:21:513920 if (!input_handler)
aurimasab0319022015-07-10 21:57:383921 return;
3922
Shimi Zhang37deeb22017-09-28 00:59:013923 using blink::mojom::SelectionMenuBehavior;
Dave Tapuskae782bea2019-07-09 16:21:513924 input_handler->AdjustSelectionByCharacterOffset(
Shimi Zhang37deeb22017-09-28 00:59:013925 start_adjust, end_adjust,
3926 show_selection_menu ? SelectionMenuBehavior::kShow
3927 : SelectionMenuBehavior::kHide);
aurimasab0319022015-07-10 21:57:383928}
3929
[email protected]b172aee2012-04-10 17:05:263930void WebContentsImpl::UpdatePreferredSize(const gfx::Size& pref_size) {
[email protected]222f5822014-02-05 23:40:493931 const gfx::Size old_size = GetPreferredSize();
[email protected]bcd2815602012-01-14 18:17:233932 preferred_size_ = pref_size;
[email protected]222f5822014-02-05 23:40:493933 OnPreferredSizeChanged(old_size);
[email protected]0548c5352011-09-07 00:33:333934}
3935
avic3aa8422015-11-09 20:57:223936void WebContentsImpl::ResizeDueToAutoResize(
3937 RenderWidgetHostImpl* render_widget_host,
Fady Samuel97947112018-05-05 16:24:543938 const gfx::Size& new_size) {
avic3aa8422015-11-09 20:57:223939 if (render_widget_host != GetRenderViewHost()->GetWidget())
3940 return;
3941
[email protected]61e2b3cc2012-03-02 16:13:343942 if (delegate_)
3943 delegate_->ResizeDueToAutoResize(this, new_size);
3944}
3945
[email protected]b172aee2012-04-10 17:05:263946WebContents* WebContentsImpl::OpenURL(const OpenURLParams& params) {
Aaron Colwell9dab1652019-12-09 18:29:493947#if DCHECK_IS_ON()
3948 DCHECK(params.Valid());
3949#endif
3950
Bo Liu300c6052018-06-12 04:46:073951 if (!delegate_) {
3952 // Embedder can delay setting a delegate on new WebContents with
3953 // WebContentsDelegate::ShouldResumeRequestsForCreatedWindow. In the mean
3954 // time, navigations, including the initial one, that goes through OpenURL
3955 // should be delayed until embedder is ready to resume loading.
3956 delayed_open_url_params_ = std::make_unique<OpenURLParams>(params);
Nasko Oskov83a8cf92018-10-19 14:58:563957
3958 // If there was a navigation deferred when creating the window through
3959 // CreateNewWindow, drop it in favor of this navigation.
3960 delayed_load_url_params_.reset();
3961
Ivan Kotenkov2c0d2bb32017-11-01 15:41:283962 return nullptr;
Bo Liu300c6052018-06-12 04:46:073963 }
[email protected]00c37fc2011-08-02 00:22:503964
[email protected]e5d549d2011-12-28 01:29:203965 WebContents* new_contents = delegate_->OpenURLFromTab(this, params);
pnoland488944652017-02-22 18:58:543966
3967 RenderFrameHost* source_render_frame_host = RenderFrameHost::FromID(
3968 params.source_render_process_id, params.source_render_frame_id);
3969
3970 if (source_render_frame_host && params.source_site_instance) {
3971 CHECK_EQ(source_render_frame_host->GetSiteInstance(),
3972 params.source_site_instance.get());
3973 }
3974
3975 if (new_contents && source_render_frame_host && new_contents != this) {
3976 for (auto& observer : observers_) {
Lukasz Anforowicz48c9f872018-08-01 00:58:413977 observer.DidOpenRequestedURL(
3978 new_contents, source_render_frame_host, params.url, params.referrer,
3979 params.disposition, params.transition,
3980 params.started_from_context_menu, params.is_renderer_initiated);
pnoland488944652017-02-22 18:58:543981 }
3982 }
3983
[email protected]e5d549d2011-12-28 01:29:203984 return new_contents;
[email protected]d5f942ba2008-09-26 19:30:343985}
3986
avi2b177592014-12-10 02:08:023987void WebContentsImpl::SetHistoryOffsetAndLength(int history_offset,
3988 int history_length) {
alexmos136fd6e62016-08-15 20:58:413989 SendPageMessage(new PageMsg_SetHistoryOffsetAndLength(
3990 MSG_ROUTING_NONE, history_offset, history_length));
avi2b177592014-12-10 02:08:023991}
3992
3993void WebContentsImpl::SetHistoryOffsetAndLengthForView(
3994 RenderViewHost* render_view_host,
3995 int history_offset,
3996 int history_length) {
alexmos136fd6e62016-08-15 20:58:413997 render_view_host->Send(new PageMsg_SetHistoryOffsetAndLength(
avi2b177592014-12-10 02:08:023998 render_view_host->GetRoutingID(), history_offset, history_length));
[email protected]796931a92011-08-10 01:32:143999}
4000
arthursonzogni818c2642019-09-27 12:18:104001void WebContentsImpl::ReloadFocusedFrame() {
[email protected]1f3fc1d2014-04-03 14:50:174002 RenderFrameHost* focused_frame = GetFocusedFrame();
4003 if (!focused_frame)
4004 return;
4005
yilkal921048bd2019-10-09 23:51:044006 focused_frame->Reload();
[email protected]959be4c2014-04-08 15:01:334007}
4008
4009void WebContentsImpl::Undo() {
Dave Tapuska58a099e2020-06-08 21:48:404010 auto* input_handler = GetFocusedFrameWidgetInputHandler();
Dave Tapuskae782bea2019-07-09 16:21:514011 if (!input_handler)
[email protected]959be4c2014-04-08 15:01:334012 return;
4013
Dave Tapuskae782bea2019-07-09 16:21:514014 input_handler->Undo();
[email protected]959be4c2014-04-08 15:01:334015 RecordAction(base::UserMetricsAction("Undo"));
4016}
4017
4018void WebContentsImpl::Redo() {
Dave Tapuska58a099e2020-06-08 21:48:404019 auto* input_handler = GetFocusedFrameWidgetInputHandler();
Dave Tapuskae782bea2019-07-09 16:21:514020 if (!input_handler)
[email protected]959be4c2014-04-08 15:01:334021 return;
Dave Tapuskae782bea2019-07-09 16:21:514022
4023 input_handler->Redo();
[email protected]959be4c2014-04-08 15:01:334024 RecordAction(base::UserMetricsAction("Redo"));
4025}
4026
4027void WebContentsImpl::Cut() {
Dave Tapuska58a099e2020-06-08 21:48:404028 auto* input_handler = GetFocusedFrameWidgetInputHandler();
Dave Tapuskae782bea2019-07-09 16:21:514029 if (!input_handler)
[email protected]959be4c2014-04-08 15:01:334030 return;
4031
Dave Tapuskae782bea2019-07-09 16:21:514032 input_handler->Cut();
[email protected]959be4c2014-04-08 15:01:334033 RecordAction(base::UserMetricsAction("Cut"));
4034}
4035
4036void WebContentsImpl::Copy() {
Dave Tapuska58a099e2020-06-08 21:48:404037 auto* input_handler = GetFocusedFrameWidgetInputHandler();
Dave Tapuskae782bea2019-07-09 16:21:514038 if (!input_handler)
[email protected]959be4c2014-04-08 15:01:334039 return;
4040
Dave Tapuskae782bea2019-07-09 16:21:514041 input_handler->Copy();
[email protected]959be4c2014-04-08 15:01:334042 RecordAction(base::UserMetricsAction("Copy"));
4043}
4044
4045void WebContentsImpl::CopyToFindPboard() {
Avi Drissman7c57be72020-07-29 20:09:464046#if defined(OS_MAC)
Dave Tapuska58a099e2020-06-08 21:48:404047 auto* input_handler = GetFocusedFrameWidgetInputHandler();
Dave Tapuskae782bea2019-07-09 16:21:514048 if (!input_handler)
[email protected]959be4c2014-04-08 15:01:334049 return;
4050
4051 // Windows/Linux don't have the concept of a find pasteboard.
Dave Tapuskae782bea2019-07-09 16:21:514052 input_handler->CopyToFindPboard();
[email protected]959be4c2014-04-08 15:01:334053 RecordAction(base::UserMetricsAction("CopyToFindPboard"));
4054#endif
4055}
4056
4057void WebContentsImpl::Paste() {
Dave Tapuska58a099e2020-06-08 21:48:404058 auto* input_handler = GetFocusedFrameWidgetInputHandler();
Dave Tapuskae782bea2019-07-09 16:21:514059 if (!input_handler)
[email protected]959be4c2014-04-08 15:01:334060 return;
4061
Dave Tapuskae782bea2019-07-09 16:21:514062 input_handler->Paste();
Archana Simhaf67090c2019-05-06 23:43:234063 for (auto& observer : observers_)
4064 observer.OnPaste();
[email protected]959be4c2014-04-08 15:01:334065 RecordAction(base::UserMetricsAction("Paste"));
4066}
4067
4068void WebContentsImpl::PasteAndMatchStyle() {
Dave Tapuska58a099e2020-06-08 21:48:404069 auto* input_handler = GetFocusedFrameWidgetInputHandler();
Dave Tapuskae782bea2019-07-09 16:21:514070 if (!input_handler)
[email protected]959be4c2014-04-08 15:01:334071 return;
4072
Dave Tapuskae782bea2019-07-09 16:21:514073 input_handler->PasteAndMatchStyle();
Archana Simhaf67090c2019-05-06 23:43:234074 for (auto& observer : observers_)
4075 observer.OnPaste();
[email protected]959be4c2014-04-08 15:01:334076 RecordAction(base::UserMetricsAction("PasteAndMatchStyle"));
4077}
4078
4079void WebContentsImpl::Delete() {
Dave Tapuska58a099e2020-06-08 21:48:404080 auto* input_handler = GetFocusedFrameWidgetInputHandler();
Dave Tapuskae782bea2019-07-09 16:21:514081 if (!input_handler)
[email protected]959be4c2014-04-08 15:01:334082 return;
4083
Dave Tapuskae782bea2019-07-09 16:21:514084 input_handler->Delete();
[email protected]959be4c2014-04-08 15:01:334085 RecordAction(base::UserMetricsAction("DeleteSelection"));
4086}
4087
4088void WebContentsImpl::SelectAll() {
Dave Tapuska58a099e2020-06-08 21:48:404089 auto* input_handler = GetFocusedFrameWidgetInputHandler();
Dave Tapuskae782bea2019-07-09 16:21:514090 if (!input_handler)
[email protected]959be4c2014-04-08 15:01:334091 return;
4092
Dave Tapuskae782bea2019-07-09 16:21:514093 input_handler->SelectAll();
[email protected]959be4c2014-04-08 15:01:334094 RecordAction(base::UserMetricsAction("SelectAll"));
4095}
4096
yabinh351e7ec2017-03-10 02:43:244097void WebContentsImpl::CollapseSelection() {
Dave Tapuska58a099e2020-06-08 21:48:404098 auto* input_handler = GetFocusedFrameWidgetInputHandler();
Dave Tapuskae782bea2019-07-09 16:21:514099 if (!input_handler)
[email protected]959be4c2014-04-08 15:01:334100 return;
4101
Dave Tapuskae782bea2019-07-09 16:21:514102 input_handler->CollapseSelection();
[email protected]1f3fc1d2014-04-03 14:50:174103}
4104
4105void WebContentsImpl::Replace(const base::string16& word) {
Dave Tapuska58a099e2020-06-08 21:48:404106 auto* input_handler = GetFocusedFrameWidgetInputHandler();
Dave Tapuskae782bea2019-07-09 16:21:514107 if (!input_handler)
[email protected]1f3fc1d2014-04-03 14:50:174108 return;
4109
Dave Tapuskae782bea2019-07-09 16:21:514110 input_handler->Replace(word);
[email protected]1f3fc1d2014-04-03 14:50:174111}
4112
4113void WebContentsImpl::ReplaceMisspelling(const base::string16& word) {
Dave Tapuska58a099e2020-06-08 21:48:404114 auto* input_handler = GetFocusedFrameWidgetInputHandler();
Dave Tapuskae782bea2019-07-09 16:21:514115 if (!input_handler)
[email protected]1f3fc1d2014-04-03 14:50:174116 return;
4117
Dave Tapuskae782bea2019-07-09 16:21:514118 input_handler->ReplaceMisspelling(word);
[email protected]959be4c2014-04-08 15:01:334119}
4120
4121void WebContentsImpl::NotifyContextMenuClosed(
4122 const CustomContextMenuContext& context) {
4123 RenderFrameHost* focused_frame = GetFocusedFrame();
4124 if (!focused_frame)
4125 return;
4126
4127 focused_frame->Send(new FrameMsg_ContextMenuClosed(
4128 focused_frame->GetRoutingID(), context));
4129}
4130
4131void WebContentsImpl::ExecuteCustomContextMenuCommand(
4132 int action, const CustomContextMenuContext& context) {
4133 RenderFrameHost* focused_frame = GetFocusedFrame();
4134 if (!focused_frame)
4135 return;
4136
4137 focused_frame->Send(new FrameMsg_CustomContextMenuAction(
4138 focused_frame->GetRoutingID(), context, action));
[email protected]4fed3702014-04-01 09:08:004139}
4140
[email protected]fc2b46b2014-05-03 16:33:454141gfx::NativeView WebContentsImpl::GetNativeView() {
4142 return view_->GetNativeView();
4143}
4144
4145gfx::NativeView WebContentsImpl::GetContentNativeView() {
4146 return view_->GetContentNativeView();
4147}
4148
4149gfx::NativeWindow WebContentsImpl::GetTopLevelNativeWindow() {
4150 return view_->GetTopLevelNativeWindow();
4151}
4152
4153gfx::Rect WebContentsImpl::GetViewBounds() {
4154 return view_->GetViewBounds();
4155}
4156
4157gfx::Rect WebContentsImpl::GetContainerBounds() {
Jeremy Roman352239c2020-05-21 16:31:324158 return view_->GetContainerBounds();
[email protected]fc2b46b2014-05-03 16:33:454159}
4160
4161DropData* WebContentsImpl::GetDropData() {
4162 return view_->GetDropData();
4163}
4164
4165void WebContentsImpl::Focus() {
miu9e14e492014-10-25 02:39:044166 view_->Focus();
[email protected]fc2b46b2014-05-03 16:33:454167}
4168
4169void WebContentsImpl::SetInitialFocus() {
miu9e14e492014-10-25 02:39:044170 view_->SetInitialFocus();
[email protected]fc2b46b2014-05-03 16:33:454171}
4172
4173void WebContentsImpl::StoreFocus() {
miu9e14e492014-10-25 02:39:044174 view_->StoreFocus();
[email protected]fc2b46b2014-05-03 16:33:454175}
4176
4177void WebContentsImpl::RestoreFocus() {
miu9e14e492014-10-25 02:39:044178 view_->RestoreFocus();
[email protected]fc2b46b2014-05-03 16:33:454179}
4180
[email protected]b172aee2012-04-10 17:05:264181void WebContentsImpl::FocusThroughTabTraversal(bool reverse) {
Maxim Podgorodskiya926c4ff2017-11-20 02:06:394182 view_->FocusThroughTabTraversal(reverse);
[email protected]96d185d2009-04-24 03:28:544183}
4184
[email protected]b172aee2012-04-10 17:05:264185bool WebContentsImpl::IsSavable() {
[email protected]a53209b2012-01-20 16:48:164186 // WebKit creates Document object when MIME type is application/xhtml+xml,
4187 // so we also support this MIME type.
Yuzu Saijoc23e9b72020-06-25 07:21:274188 std::string mime_type = GetContentsMimeType();
4189 return mime_type == "text/html" || mime_type == "text/xml" ||
4190 mime_type == "application/xhtml+xml" || mime_type == "text/plain" ||
4191 mime_type == "text/css" ||
4192 blink::IsSupportedJavascriptMimeType(mime_type);
[email protected]a53209b2012-01-20 16:48:164193}
4194
[email protected]b172aee2012-04-10 17:05:264195void WebContentsImpl::OnSavePage() {
[email protected]c7dd2f62011-07-18 15:57:594196 // If we can not save the page, try to download it.
[email protected]a53209b2012-01-20 16:48:164197 if (!IsSavable()) {
Min Qinda0ed2062018-02-23 22:00:534198 download::RecordSavePackageEvent(
4199 download::SAVE_PACKAGE_DOWNLOAD_ON_NON_HTML);
nasko89ad7742015-03-05 22:14:194200 SaveFrame(GetLastCommittedURL(), Referrer());
[email protected]27678b2a2012-02-04 22:09:144201 return;
[email protected]c7dd2f62011-07-18 15:57:594202 }
4203
4204 Stop();
4205
4206 // Create the save package and possibly prompt the user for the name to save
4207 // the page as. The user prompt is an asynchronous operation that runs on
4208 // another thread.
4209 save_package_ = new SavePackage(this);
4210 save_package_->GetSaveInfo();
4211}
4212
4213// Used in automated testing to bypass prompting the user for file names.
4214// Instead, the names and paths are hard coded rather than running them through
4215// file name sanitation and extension / mime checking.
[email protected]2dec8ec2013-02-07 19:20:344216bool WebContentsImpl::SavePage(const base::FilePath& main_file,
4217 const base::FilePath& dir_path,
[email protected]8ff00d72012-10-23 19:12:214218 SavePageType save_type) {
[email protected]c7dd2f62011-07-18 15:57:594219 // Stop the page from navigating.
4220 Stop();
4221
4222 save_package_ = new SavePackage(this, save_type, main_file, dir_path);
[email protected]8ff00d72012-10-23 19:12:214223 return save_package_->Init(SavePackageDownloadCreatedCallback());
[email protected]c7dd2f62011-07-18 15:57:594224}
4225
[email protected]3c71576ce2013-07-23 02:00:014226void WebContentsImpl::SaveFrame(const GURL& url,
4227 const Referrer& referrer) {
Xing Liu10329bf2018-03-20 19:22:144228 SaveFrameWithHeaders(url, referrer, std::string(), base::string16());
kundaji6c7f9692015-03-09 18:00:374229}
4230
Xing Liu10329bf2018-03-20 19:22:144231void WebContentsImpl::SaveFrameWithHeaders(
4232 const GURL& url,
4233 const Referrer& referrer,
4234 const std::string& headers,
4235 const base::string16& suggested_filename) {
Lei Zhang13a42e42019-02-19 23:28:114236 // Check and see if the guest can handle this.
4237 if (delegate_) {
4238 WebContents* guest_web_contents = nullptr;
4239 if (browser_plugin_embedder_) {
4240 BrowserPluginGuest* guest = browser_plugin_embedder_->GetFullPageGuest();
4241 if (guest)
4242 guest_web_contents = guest->GetWebContents();
4243 } else if (browser_plugin_guest_) {
4244 guest_web_contents = this;
4245 }
4246
4247 if (guest_web_contents && delegate_->GuestSaveFrame(guest_web_contents))
4248 return;
4249 }
4250
nasko89ad7742015-03-05 22:14:194251 if (!GetLastCommittedURL().is_valid())
[email protected]3c71576ce2013-07-23 02:00:014252 return;
sammc92af61552014-11-19 23:27:404253 if (delegate_ && delegate_->SaveFrame(url, referrer))
4254 return;
4255
nasko89ad7742015-03-05 22:14:194256 // TODO(nasko): This check for main frame is incorrect and should be fixed
brettw760f7442016-05-23 21:19:224257 // by explicitly passing in which frame this method should target. This would
4258 // indicate whether it's the main frame, and also tell us the frame pointer
4259 // to use for routing.
nasko89ad7742015-03-05 22:14:194260 bool is_main_frame = (url == GetLastCommittedURL());
brettw760f7442016-05-23 21:19:224261 RenderFrameHost* frame_host = GetMainFrame();
[email protected]3c71576ce2013-07-23 02:00:014262
avib7348942015-12-25 20:57:104263 int64_t post_id = -1;
[email protected]3c71576ce2013-07-23 02:00:014264 if (is_main_frame) {
Lucas Furukawa Gadani5553a152019-01-08 18:55:574265 NavigationEntry* entry = controller_.GetLastCommittedEntry();
[email protected]3c71576ce2013-07-23 02:00:014266 if (entry)
4267 post_id = entry->GetPostID();
4268 }
Ramin Halavati24e21012017-07-10 12:56:064269 net::NetworkTrafficAnnotationTag traffic_annotation =
4270 net::DefineNetworkTrafficAnnotation("download_web_contents_frame", R"(
4271 semantics {
4272 sender: "Save Page Action"
4273 description:
4274 "Saves the given frame's URL to the local file system."
4275 trigger:
4276 "The user has triggered a save operation on the frame through a "
4277 "context menu or other mechanism."
4278 data: "None."
4279 destination: WEBSITE
4280 }
4281 policy {
Ramin Halavati3b979782017-07-21 11:40:264282 cookies_allowed: YES
Ramin Halavati24e21012017-07-10 12:56:064283 cookies_store: "user"
4284 setting:
4285 "This feature cannot be disabled by settings, but it's is only "
4286 "triggered by user request."
4287 policy_exception_justification: "Not implemented."
4288 })");
Min Qina904f3302018-02-13 23:33:344289 auto params = std::make_unique<download::DownloadUrlParameters>(
brettw760f7442016-05-23 21:19:224290 url, frame_host->GetProcess()->GetID(),
4291 frame_host->GetRenderViewHost()->GetRoutingID(),
Matt Menke0dcaebc32020-03-06 17:00:104292 frame_host->GetRoutingID(), traffic_annotation);
Min Qina904f3302018-02-13 23:33:344293 params->set_referrer(referrer.url);
4294 params->set_referrer_policy(
4295 Referrer::ReferrerPolicyForUrlRequest(referrer.policy));
[email protected]3c71576ce2013-07-23 02:00:014296 params->set_post_id(post_id);
[email protected]3c71576ce2013-07-23 02:00:014297 if (post_id >= 0)
4298 params->set_method("POST");
4299 params->set_prompt(true);
kundaji6c7f9692015-03-09 18:00:374300
4301 if (headers.empty()) {
4302 params->set_prefer_cache(true);
4303 } else {
Min Qina904f3302018-02-13 23:33:344304 for (download::DownloadUrlParameters::RequestHeadersNameValuePair
4305 key_value : ParseDownloadHeaders(headers)) {
Megan Jablonski2f6a4c52017-07-10 23:01:254306 params->add_request_header(key_value.first, key_value.second);
kundaji6c7f9692015-03-09 18:00:374307 }
4308 }
Xing Liu10329bf2018-03-20 19:22:144309 params->set_suggested_name(suggested_filename);
Min Qin0ca8e1ee2018-01-31 00:49:354310 params->set_download_source(download::DownloadSource::WEB_CONTENTS_API);
thestig859c78892017-05-15 21:17:064311 BrowserContext::GetDownloadManager(GetBrowserContext())
Ramin Halavati03efa732017-06-12 09:28:204312 ->DownloadUrl(std::move(params));
[email protected]3c71576ce2013-07-23 02:00:014313}
4314
[email protected]b172aee2012-04-10 17:05:264315void WebContentsImpl::GenerateMHTML(
dewittj6dc5747a2016-05-17 01:48:474316 const MHTMLGenerationParams& params,
Avi Drissman149b7832018-03-23 14:31:494317 base::OnceCallback<void(int64_t)> callback) {
Angel Alvarez902496682019-08-27 22:58:424318 base::OnceCallback<void(const MHTMLGenerationResult&)> wrapper_callback =
4319 base::BindOnce(
4320 [](base::OnceCallback<void(int64_t)> size_callback,
4321 const MHTMLGenerationResult& result) {
4322 std::move(size_callback).Run(result.file_size);
4323 },
4324 std::move(callback));
4325 MHTMLGenerationManager::GetInstance()->SaveMHTML(this, params,
4326 std::move(wrapper_callback));
4327}
4328
4329void WebContentsImpl::GenerateMHTMLWithResult(
4330 const MHTMLGenerationParams& params,
4331 MHTMLGenerationResult::GenerateMHTMLCallback callback) {
Avi Drissman149b7832018-03-23 14:31:494332 MHTMLGenerationManager::GetInstance()->SaveMHTML(this, params,
4333 std::move(callback));
[email protected]aa4f3972012-03-01 18:12:124334}
4335
Tsuyoshi Horob0d8d902020-03-12 00:09:394336void WebContentsImpl::GenerateWebBundle(
4337 const base::FilePath& file_path,
4338 base::OnceCallback<void(uint64_t /* file_size */,
4339 data_decoder::mojom::WebBundlerError)> callback) {
4340 SaveAsWebBundleJob::Start(this, file_path, std::move(callback));
4341}
4342
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:414343const std::string& WebContentsImpl::GetContentsMimeType() {
Yuzu Saijoc23e9b72020-06-25 07:21:274344 return GetRenderViewHost()->contents_mime_type();
[email protected]0bfbf882011-12-22 18:19:274345}
4346
Leon Hanc819dc62019-01-28 04:30:194347blink::mojom::RendererPreferences* WebContentsImpl::GetMutableRendererPrefs() {
[email protected]0bfbf882011-12-22 18:19:274348 return &renderer_preferences_;
4349}
4350
[email protected]e35ccd52012-05-23 16:22:474351void WebContentsImpl::Close() {
4352 Close(GetRenderViewHost());
4353}
4354
Ella Ge80a52dce2017-11-15 18:01:524355void WebContentsImpl::DragSourceEndedAt(float client_x,
4356 float client_y,
4357 float screen_x,
4358 float screen_y,
Paul Meyer0c58c3712016-11-17 22:59:514359 blink::WebDragOperation operation,
4360 RenderWidgetHost* source_rwh) {
Lei Zhang6410ec1a2019-01-31 21:38:584361 if (browser_plugin_embedder_) {
W. James MacLean9437b5bf2019-12-06 17:33:444362 browser_plugin_embedder_->DragSourceEndedAt(client_x, client_y, screen_x,
4363 screen_y, operation);
Lei Zhang6410ec1a2019-01-31 21:38:584364 }
Paul Meyer0c58c3712016-11-17 22:59:514365 if (source_rwh) {
Ella Ge80a52dce2017-11-15 18:01:524366 source_rwh->DragSourceEndedAt(gfx::PointF(client_x, client_y),
4367 gfx::PointF(screen_x, screen_y), operation);
Paul Meyer0c58c3712016-11-17 22:59:514368 }
[email protected]cf200a562013-05-03 16:24:294369}
4370
jam73f892642016-09-11 06:04:064371void WebContentsImpl::LoadStateChanged(
Charles Harrison911fa062018-03-06 21:01:464372 const std::string& host,
jam73f892642016-09-11 06:04:064373 const net::LoadStateWithParam& load_state,
4374 uint64_t upload_position,
4375 uint64_t upload_size) {
Charles Harrison911fa062018-03-06 21:01:464376 base::string16 host16 = url_formatter::IDNToUnicode(host);
Elly Fong-Jonescad9c3d12018-01-31 17:26:104377 // Drop no-op updates.
4378 if (load_state_.state == load_state.state &&
4379 load_state_.param == load_state.param &&
4380 upload_position_ == upload_position && upload_size_ == upload_size &&
Charles Harrison911fa062018-03-06 21:01:464381 load_state_host_ == host16) {
Elly Fong-Jonescad9c3d12018-01-31 17:26:104382 return;
4383 }
jam73f892642016-09-11 06:04:064384 load_state_ = load_state;
4385 upload_position_ = upload_position;
4386 upload_size_ = upload_size;
Charles Harrison911fa062018-03-06 21:01:464387 load_state_host_ = host16;
jam73f892642016-09-11 06:04:064388 if (load_state_.state == net::LOAD_STATE_READING_RESPONSE)
4389 SetNotWaitingForResponse();
4390 if (IsLoading()) {
4391 NotifyNavigationStateChanged(static_cast<InvalidateTypes>(
4392 INVALIDATE_TYPE_LOAD | INVALIDATE_TYPE_TAB));
4393 }
4394}
4395
Collin Baker989e0882019-11-01 01:27:174396void WebContentsImpl::SetVisibilityAndNotifyObservers(Visibility visibility) {
Francois Doraye6161152018-03-27 22:05:374397 const Visibility previous_visibility = visibility_;
4398 visibility_ = visibility;
4399
Francois Dorayfe4a1772018-02-17 04:17:094400 // Notify observers if the visibility changed or if WasShown() is being called
4401 // for the first time.
Francois Dorayfe4a1772018-02-17 04:17:094402 if (visibility != previous_visibility ||
4403 (visibility == Visibility::VISIBLE && !did_first_set_visible_)) {
4404 for (auto& observer : observers_)
4405 observer.OnVisibilityChanged(visibility);
4406 }
4407}
4408
Michael Thiessen896405db2017-07-20 17:52:324409void WebContentsImpl::NotifyWebContentsFocused(
4410 RenderWidgetHost* render_widget_host) {
ericwilligersde386342016-10-19 02:35:094411 for (auto& observer : observers_)
Michael Thiessen896405db2017-07-20 17:52:324412 observer.OnWebContentsFocused(render_widget_host);
calamity7fe55ce2015-04-10 03:59:374413}
4414
Michael Thiessen896405db2017-07-20 17:52:324415void WebContentsImpl::NotifyWebContentsLostFocus(
4416 RenderWidgetHost* render_widget_host) {
zijiehe3bee08e22017-05-17 04:14:464417 for (auto& observer : observers_)
Michael Thiessen896405db2017-07-20 17:52:324418 observer.OnWebContentsLostFocus(render_widget_host);
zijiehe3bee08e22017-05-17 04:14:464419}
4420
Paul Meyer0c58c3712016-11-17 22:59:514421void WebContentsImpl::SystemDragEnded(RenderWidgetHost* source_rwh) {
4422 if (source_rwh)
4423 source_rwh->DragSourceSystemDragEnded();
Lei Zhang6410ec1a2019-01-31 21:38:584424 if (browser_plugin_embedder_)
[email protected]cf200a562013-05-03 16:24:294425 browser_plugin_embedder_->SystemDragEnded();
[email protected]7813bd72011-02-05 02:19:344426}
4427
[email protected]b172aee2012-04-10 17:05:264428void WebContentsImpl::SetClosedByUserGesture(bool value) {
[email protected]0bfbf882011-12-22 18:19:274429 closed_by_user_gesture_ = value;
4430}
4431
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:414432bool WebContentsImpl::GetClosedByUserGesture() {
[email protected]0bfbf882011-12-22 18:19:274433 return closed_by_user_gesture_;
4434}
4435
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:414436int WebContentsImpl::GetMinimumZoomPercent() {
[email protected]0bfbf882011-12-22 18:19:274437 return minimum_zoom_percent_;
4438}
4439
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:414440int WebContentsImpl::GetMaximumZoomPercent() {
[email protected]0bfbf882011-12-22 18:19:274441 return maximum_zoom_percent_;
4442}
4443
Dave Tapuska8f7c3872020-02-07 01:16:384444void WebContentsImpl::SetPageScale(float scale_factor) {
4445 GetMainFrame()->GetAssociatedLocalMainFrame()->SetScaleFactor(scale_factor);
ccameronb7c1d6c2015-03-09 17:08:244446}
4447
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:414448gfx::Size WebContentsImpl::GetPreferredSize() {
Francois Doray24fc62c2017-12-11 17:27:334449 return IsBeingCaptured() ? preferred_size_for_capture_ : preferred_size_;
[email protected]bcd2815602012-01-14 18:17:234450}
4451
James Hollyerd5c9de462020-03-10 19:02:454452bool WebContentsImpl::GotResponseToLockMouseRequest(
4453 blink::mojom::PointerLockResult result) {
lfgf0cd46e2017-01-04 00:05:234454 if (mouse_lock_widget_) {
4455 if (mouse_lock_widget_->delegate()->GetAsWebContents() != this) {
4456 return mouse_lock_widget_->delegate()
4457 ->GetAsWebContents()
James Hollyerd5c9de462020-03-10 19:02:454458 ->GotResponseToLockMouseRequest(result);
lfgf0cd46e2017-01-04 00:05:234459 }
lfgad8244352016-07-13 16:55:514460
James Hollyerd5c9de462020-03-10 19:02:454461 if (mouse_lock_widget_->GotResponseToLockMouseRequest(result))
lfgf0cd46e2017-01-04 00:05:234462 return true;
4463 }
4464
4465 for (WebContentsImpl* current = this; current;
4466 current = current->GetOuterWebContents()) {
4467 current->mouse_lock_widget_ = nullptr;
4468 }
4469
lfgbee1e0a2016-06-08 21:24:214470 return false;
[email protected]0bfbf882011-12-22 18:19:274471}
4472
James Hollyerd5c9de462020-03-10 19:02:454473void WebContentsImpl::GotLockMousePermissionResponse(bool allowed) {
4474 GotResponseToLockMouseRequest(
4475 allowed ? blink::mojom::PointerLockResult::kSuccess
4476 : blink::mojom::PointerLockResult::kPermissionDenied);
4477}
4478
Joe Downing13dd76b2018-04-09 18:32:154479bool WebContentsImpl::GotResponseToKeyboardLockRequest(bool allowed) {
4480 if (!keyboard_lock_widget_)
4481 return false;
4482
4483 if (keyboard_lock_widget_->delegate()->GetAsWebContents() != this) {
4484 NOTREACHED();
4485 return false;
4486 }
4487
4488 // KeyboardLock is only supported when called by the top-level browsing
4489 // context and is not supported in embedded content scenarios.
4490 if (GetOuterWebContents())
4491 return false;
4492
4493 keyboard_lock_widget_->GotResponseToKeyboardLockRequest(allowed);
4494 return true;
4495}
4496
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:414497bool WebContentsImpl::HasOpener() {
Ivan Kotenkov2c0d2bb32017-11-01 15:41:284498 return GetOpener() != nullptr;
[email protected]a0358d72012-03-09 14:06:504499}
4500
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:414501RenderFrameHostImpl* WebContentsImpl::GetOpener() {
alexmose201c7cd2015-06-10 17:14:214502 FrameTreeNode* opener_ftn = frame_tree_.root()->opener();
lukasza6f8ac622017-06-06 03:10:204503 return opener_ftn ? opener_ftn->current_frame_host() : nullptr;
jochen55ff3502014-12-18 20:52:574504}
4505
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:414506bool WebContentsImpl::HasOriginalOpener() {
Ivan Kotenkov2c0d2bb32017-11-01 15:41:284507 return GetOriginalOpener() != nullptr;
jochen6004a362017-02-04 00:11:404508}
4509
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:414510RenderFrameHostImpl* WebContentsImpl::GetOriginalOpener() {
jochen6004a362017-02-04 00:11:404511 FrameTreeNode* opener_ftn = frame_tree_.root()->original_opener();
lukasza6f8ac622017-06-06 03:10:204512 return opener_ftn ? opener_ftn->current_frame_host() : nullptr;
jochen6004a362017-02-04 00:11:404513}
4514
[email protected]cb805452013-05-22 15:16:214515void WebContentsImpl::DidChooseColorInColorChooser(SkColor color) {
Joel Hockey85b379d2017-12-11 10:42:134516 color_chooser_->DidChooseColorInColorChooser(color);
[email protected]da8543762012-03-20 08:52:204517}
4518
[email protected]cb805452013-05-22 15:16:214519void WebContentsImpl::DidEndColorChooser() {
Joel Hockey85b379d2017-12-11 10:42:134520 color_chooser_.reset();
[email protected]da8543762012-03-20 08:52:204521}
4522
halton.huoca2eabd2015-07-06 08:17:404523int WebContentsImpl::DownloadImage(
4524 const GURL& url,
4525 bool is_favicon,
Fredrik Söderquistb2b39eb92019-11-18 16:16:504526 uint32_t preferred_size,
halton.huoca2eabd2015-07-06 08:17:404527 uint32_t max_bitmap_size,
4528 bool bypass_cache,
Avi Drissmana5a52dd2018-03-27 03:39:024529 WebContents::ImageDownloadCallback callback) {
Danyao Wanga78f3dd22020-03-05 05:31:274530 return DownloadImageInFrame(GlobalFrameRoutingId(), url, is_favicon,
4531 preferred_size, max_bitmap_size, bypass_cache,
4532 std::move(callback));
4533}
4534
4535int WebContentsImpl::DownloadImageInFrame(
4536 const GlobalFrameRoutingId& initiator_frame_routing_id,
4537 const GURL& url,
4538 bool is_favicon,
4539 uint32_t preferred_size,
4540 uint32_t max_bitmap_size,
4541 bool bypass_cache,
4542 WebContents::ImageDownloadCallback callback) {
amistry9f01b772015-07-29 01:54:554543 DCHECK_CURRENTLY_ON(BrowserThread::UI);
halton.huoca2eabd2015-07-06 08:17:404544 static int next_image_download_id = 0;
Danyao Wanga78f3dd22020-03-05 05:31:274545
4546 RenderFrameHostImpl* initiator_frame =
4547 initiator_frame_routing_id.child_id
4548 ? RenderFrameHostImpl::FromID(initiator_frame_routing_id)
4549 : GetMainFrame();
Julie Jeongeun Kimb1473572019-08-15 05:59:134550 const mojo::Remote<blink::mojom::ImageDownloader>& mojo_image_downloader =
Danyao Wanga78f3dd22020-03-05 05:31:274551 initiator_frame->GetMojoImageDownloader();
amistry9f01b772015-07-29 01:54:554552 const int download_id = ++next_image_download_id;
4553 if (!mojo_image_downloader) {
4554 // If the renderer process is dead (i.e. crash, or memory pressure on
4555 // Android), the downloader service will be invalid. Pre-Mojo, this would
Avi Drissmana5a52dd2018-03-27 03:39:024556 // hang the callback indefinitely since the IPC would be dropped. Now,
amistry9f01b772015-07-29 01:54:554557 // respond with a 400 HTTP error code to indicate that something went wrong.
Gabriel Charettee7cdc5cd2020-05-27 23:35:054558 GetUIThreadTaskRunner({})->PostTask(
4559 FROM_HERE,
tzike2aca992017-09-05 08:50:544560 base::BindOnce(&WebContentsImpl::OnDidDownloadImage,
Avi Drissmana5a52dd2018-03-27 03:39:024561 weak_factory_.GetWeakPtr(), std::move(callback),
4562 download_id, url, 400, std::vector<SkBitmap>(),
4563 std::vector<gfx::Size>()));
amistry9f01b772015-07-29 01:54:554564 return download_id;
4565 }
4566
halton.huoca2eabd2015-07-06 08:17:404567 mojo_image_downloader->DownloadImage(
Fredrik Söderquistb2b39eb92019-11-18 16:16:504568 url, is_favicon, preferred_size, max_bitmap_size, bypass_cache,
tzike2aca992017-09-05 08:50:544569 base::BindOnce(&WebContentsImpl::OnDidDownloadImage,
Avi Drissmana5a52dd2018-03-27 03:39:024570 weak_factory_.GetWeakPtr(), std::move(callback),
4571 download_id, url));
amistry9f01b772015-07-29 01:54:554572 return download_id;
[email protected]795c28972012-12-06 06:13:394573}
4574
[email protected]36ec24f2014-01-09 00:32:084575void WebContentsImpl::Find(int request_id,
4576 const base::string16& search_text,
Rakina Zata Amni3f77dff2018-09-08 16:19:434577 blink::mojom::FindOptionsPtr options) {
thestig6057a6b22015-11-12 23:01:334578 // Cowardly refuse to search for no text.
4579 if (search_text.empty()) {
4580 NOTREACHED();
4581 return;
4582 }
4583
Rakina Zata Amni3f77dff2018-09-08 16:19:434584 GetOrCreateFindRequestManager()->Find(request_id, search_text,
4585 std::move(options));
[email protected]36ec24f2014-01-09 00:32:084586}
4587
4588void WebContentsImpl::StopFinding(StopFindAction action) {
paulmeyerfeafc2d2017-04-25 21:46:404589 if (FindRequestManager* manager = GetFindRequestManager())
4590 manager->StopFinding(action);
[email protected]36ec24f2014-01-09 00:32:084591}
4592
Tommy Steimel18360512017-11-01 00:38:194593bool WebContentsImpl::WasEverAudible() {
4594 return was_ever_audible_;
4595}
4596
Avi Drissmana5a52dd2018-03-27 03:39:024597void WebContentsImpl::GetManifest(GetManifestCallback callback) {
Yuzu Saijofce18322020-05-14 05:02:094598 // TODO(yuzus, 1061899): Move this function to RenderFrameHostImpl.
4599 ManifestManagerHost* manifest_manager_host =
4600 ManifestManagerHost::GetOrCreateForCurrentDocument(GetMainFrame());
4601 manifest_manager_host->GetManifest(std::move(callback));
mlamouriefdca9d2014-09-16 16:55:404602}
4603
bokanece34a82016-01-28 19:49:464604void WebContentsImpl::ExitFullscreen(bool will_cause_resize) {
mlamouri7a78d6fd2015-01-17 13:23:534605 // Clean up related state and initiate the fullscreen exit.
James Hollyerd5c9de462020-03-10 19:02:454606 GetRenderViewHost()->GetWidget()->RejectMouseLockOrUnlockIfNecessary(
4607 blink::mojom::PointerLockResult::kUserRejected);
bokanece34a82016-01-28 19:49:464608 ExitFullscreenMode(will_cause_resize);
mlamouri7a78d6fd2015-01-17 13:23:534609}
4610
Avi Drissman1a55a9d62020-03-10 18:56:454611base::ScopedClosureRunner WebContentsImpl::ForSecurityDropFullscreen() {
Avi Drissman33972942020-06-19 14:16:014612 // Kick WebContentses that are "related" to this WebContents out of
4613 // fullscreen. This needs to be done with two passes, because it is simple to
4614 // walk _up_ the chain of openers and outer contents, but it not simple to
4615 // walk _down_ the chain.
4616
4617 // First, determine if any WebContents that is in fullscreen has this
4618 // WebContents as an upstream contents. Drop that WebContents out of
4619 // fullscreen if it does. This is theoretically quadratic-ish (fullscreen
4620 // contentses x each one's opener length) but neither of those is expected to
4621 // ever be a large number.
4622
4623 auto fullscreen_set_copy = *FullscreenContentsSet(GetBrowserContext());
4624 for (auto* fullscreen_contents : fullscreen_set_copy) {
Avi Drissman97aef042020-06-30 21:04:484625 // Checking IsFullscreen() for tabs in the fullscreen set may seem
4626 // redundant, but teeeeechnically fullscreen is run by the delegate, and
4627 // it's possible that the delegate's notion of fullscreen may have changed
4628 // outside of WebContents's notice.
4629 if (fullscreen_contents->IsFullscreen()) {
Avi Drissman33972942020-06-19 14:16:014630 auto opener_contentses = GetAllOpeningWebContents(fullscreen_contents);
4631 if (opener_contentses.count(this))
4632 fullscreen_contents->ExitFullscreen(true);
4633 }
4634 }
4635
4636 // Second, walk upstream from this WebContents, and drop the fullscreen of
4637 // all WebContentses that are in fullscreen. Block all the WebContentses in
4638 // the chain from entering fullscreen while the returned closure runner is
4639 // alive. It's OK that this set doesn't contain downstream WebContentses, as
4640 // any request to enter fullscreen will have the upstream of the WebContents
4641 // checked. (See CanEnterFullscreenMode().)
Avi Drissmanced52b62019-08-14 21:25:464642
Avi Drissman1a55a9d62020-03-10 18:56:454643 std::vector<base::WeakPtr<WebContentsImpl>> blocked_contentses;
4644
Avi Drissman33972942020-06-19 14:16:014645 for (auto* opener : GetAllOpeningWebContents(this)) {
Avi Drissman1a55a9d62020-03-10 18:56:454646 // Drop fullscreen if the WebContents is in it, and...
Avi Drissman97aef042020-06-30 21:04:484647 if (opener->IsFullscreen())
Avi Drissman33972942020-06-19 14:16:014648 opener->ExitFullscreen(true);
Avi Drissmanced52b62019-08-14 21:25:464649
Avi Drissman1a55a9d62020-03-10 18:56:454650 // ...block the WebContents from entering fullscreen until further notice.
Avi Drissman33972942020-06-19 14:16:014651 ++opener->fullscreen_blocker_count_;
4652 blocked_contentses.push_back(opener->weak_factory_.GetWeakPtr());
Avi Drissmanced52b62019-08-14 21:25:464653 }
Avi Drissman1a55a9d62020-03-10 18:56:454654
4655 return base::ScopedClosureRunner(base::BindOnce(
4656 [](std::vector<base::WeakPtr<WebContentsImpl>> blocked_contentses) {
4657 for (base::WeakPtr<WebContentsImpl>& web_contents :
4658 blocked_contentses) {
4659 if (web_contents) {
4660 DCHECK_GT(web_contents->fullscreen_blocker_count_, 0);
4661 --web_contents->fullscreen_blocker_count_;
4662 }
4663 }
4664 },
4665 std::move(blocked_contentses)));
Avi Drissmanced52b62019-08-14 21:25:464666}
4667
mariakhomenko44bdc4732015-04-29 01:55:384668void WebContentsImpl::ResumeLoadingCreatedWebContents() {
Nasko Oskov83a8cf92018-10-19 14:58:564669 if (delayed_load_url_params_.get()) {
4670 DCHECK(!delayed_open_url_params_);
4671 controller_.LoadURLWithParams(*delayed_load_url_params_.get());
4672 delayed_load_url_params_.reset(nullptr);
4673 return;
4674 }
4675
mariakhomenkoa4971c12015-07-21 19:04:374676 if (delayed_open_url_params_.get()) {
4677 OpenURL(*delayed_open_url_params_.get());
4678 delayed_open_url_params_.reset(nullptr);
4679 return;
4680 }
4681
mariakhomenko44bdc4732015-04-29 01:55:384682 // Resume blocked requests for both the RenderViewHost and RenderFrameHost.
4683 // TODO(brettw): It seems bogus to reach into here and initialize the host.
dfalcantarae6b7b46752015-07-10 18:14:164684 if (is_resume_pending_) {
4685 is_resume_pending_ = false;
avi0f1bbc9712015-11-17 02:58:134686 GetRenderViewHost()->GetWidget()->Init();
dfalcantarae6b7b46752015-07-10 18:14:164687 GetMainFrame()->Init();
4688 }
mariakhomenko44bdc4732015-04-29 01:55:384689}
4690
[email protected]b172aee2012-04-10 17:05:264691bool WebContentsImpl::FocusLocationBarByDefault() {
Xiyuan Xia2a66b112018-12-06 15:52:164692 if (should_focus_location_bar_by_default_)
[email protected]0c9406632013-02-08 01:13:334693 return true;
Xiyuan Xia2a66b112018-12-06 15:52:164694
[email protected]0c9406632013-02-08 01:13:334695 return delegate_ && delegate_->ShouldFocusLocationBarByDefault(this);
[email protected]0bfbf882011-12-22 18:19:274696}
4697
Tommy C. Li8bb5bf42019-01-18 18:45:514698void WebContentsImpl::SetFocusToLocationBar() {
[email protected]0bfbf882011-12-22 18:19:274699 if (delegate_)
Tommy C. Li8bb5bf42019-01-18 18:45:514700 delegate_->SetFocusToLocationBar();
[email protected]c40d6232011-03-25 00:16:214701}
4702
clamy0e119882015-07-31 16:12:334703void WebContentsImpl::DidStartNavigation(NavigationHandle* navigation_handle) {
Charles Harrison53096ba2017-12-15 15:39:484704 TRACE_EVENT1("navigation", "WebContentsImpl::DidStartNavigation",
4705 "navigation_handle", navigation_handle);
Alan Cutterab48fc02020-02-24 12:26:424706 if (navigation_handle->IsInMainFrame())
4707 favicon_urls_.clear();
Mounir Lamouri7b4a1f232019-12-23 16:32:104708
ericwilligersde386342016-10-19 02:35:094709 for (auto& observer : observers_)
4710 observer.DidStartNavigation(navigation_handle);
Becca Hughesd11d6502018-07-31 17:01:284711
Xiyuan Xia2a66b112018-12-06 15:52:164712 if (navigation_handle->IsInMainFrame()) {
4713 // When the browser is started with about:blank as the startup URL, focus
4714 // the location bar (which will also select its contents) so people can
4715 // simply begin typing to navigate elsewhere.
4716 //
4717 // We need to be careful not to trigger this for anything other than the
4718 // startup navigation. In particular, if we allow an attacker to open a
4719 // popup to about:blank, then navigate, focusing the Omnibox will cause the
4720 // end of the new URL to be scrolled into view instead of the start,
4721 // allowing the attacker to spoof other URLs. The conditions checked here
4722 // are all aimed at ensuring no such attacker-controlled navigation can
4723 // trigger this.
4724 should_focus_location_bar_by_default_ =
4725 controller_.IsInitialNavigation() &&
4726 !navigation_handle->IsRendererInitiated() &&
4727 navigation_handle->GetURL() == url::kAboutBlankURL;
4728 }
clamy0e119882015-07-31 16:12:334729}
4730
4731void WebContentsImpl::DidRedirectNavigation(
4732 NavigationHandle* navigation_handle) {
Charles Harrison53096ba2017-12-15 15:39:484733 TRACE_EVENT1("navigation", "WebContentsImpl::DidRedirectNavigation",
4734 "navigation_handle", navigation_handle);
ericwilligersde386342016-10-19 02:35:094735 for (auto& observer : observers_)
4736 observer.DidRedirectNavigation(navigation_handle);
jam5f358e52017-02-13 16:37:034737
4738 // Notify accessibility if this is a reload. This has to called on the
4739 // BrowserAccessibilityManager associated with the old RFHI.
4740 if (navigation_handle->GetReloadType() != ReloadType::NONE) {
Mohamed Abdelhalim95794962019-09-20 11:16:494741 NavigationRequest* request = NavigationRequest::From(navigation_handle);
jam5f358e52017-02-13 16:37:034742 BrowserAccessibilityManager* manager =
Mohamed Abdelhalim95794962019-09-20 11:16:494743 request->frame_tree_node()
jam5f358e52017-02-13 16:37:034744 ->current_frame_host()
4745 ->browser_accessibility_manager();
4746 if (manager)
4747 manager->UserIsReloading();
4748 }
clamy0e119882015-07-31 16:12:334749}
4750
clamyefca29e2015-09-17 00:22:114751void WebContentsImpl::ReadyToCommitNavigation(
4752 NavigationHandle* navigation_handle) {
Charles Harrison53096ba2017-12-15 15:39:484753 TRACE_EVENT1("navigation", "WebContentsImpl::ReadyToCommitNavigation",
4754 "navigation_handle", navigation_handle);
ericwilligersde386342016-10-19 02:35:094755 for (auto& observer : observers_)
4756 observer.ReadyToCommitNavigation(navigation_handle);
John Abd-El-Malekd4882642017-12-04 21:45:194757
Kenneth Russell954ed9ce2018-04-12 23:07:014758 // If any domains are blocked from accessing 3D APIs because they may
4759 // have caused the GPU to reset recently, unblock them here if the user
4760 // initiated this navigation. This implies that the user was involved in
4761 // the decision to navigate, so there's no concern about
4762 // denial-of-service issues. Want to do this as early as
4763 // possible to avoid race conditions with pages attempting to access
4764 // WebGL early on.
4765 //
4766 // TODO(crbug.com/617904): currently navigations initiated by the browser
4767 // (reload button, reload menu option, pressing return in the Omnibox)
4768 // return false from HasUserGesture(). If or when that is addressed,
4769 // remove the check for IsRendererInitiated() below.
4770 //
4771 // TODO(crbug.com/832180): HasUserGesture comes from the renderer
4772 // process and isn't validated. Until it is, don't trust it.
4773 if (!navigation_handle->IsRendererInitiated()) {
4774 GpuDataManagerImpl::GetInstance()->UnblockDomainFrom3DAPIs(
4775 navigation_handle->GetURL());
4776 }
4777
John Abd-El-Malekfde08e6da2017-12-05 04:06:244778 if (navigation_handle->IsSameDocument())
4779 return;
4780
Livvie Lin139bf442018-08-10 00:18:544781 // SSLInfo is not needed on subframe navigations since the main-frame
4782 // certificate is the only one that can be inspected (using the info
4783 // bubble) without refreshing the page with DevTools open.
Carlos ILaed1e7b2019-03-12 19:27:214784 // We don't call DidStartResourceResponse on net errors, since that results on
4785 // existing cert exceptions being revoked, which leads to weird behavior with
4786 // committed interstitials or while offline. We only need the error check for
4787 // the main frame case because unlike this method, SubresourceResponseStarted
4788 // does not get called on network errors.
4789 if (navigation_handle->IsInMainFrame() &&
4790 navigation_handle->GetNetErrorCode() == net::OK) {
Livvie Lin139bf442018-08-10 00:18:544791 controller_.ssl_manager()->DidStartResourceResponse(
Lukasz Anforowiczd334bba2020-02-10 23:32:554792 navigation_handle->GetURL(),
Camille Lamy62b826012019-02-26 09:15:474793 navigation_handle->GetSSLInfo().has_value()
4794 ? net::IsCertStatusError(
4795 navigation_handle->GetSSLInfo()->cert_status)
4796 : false);
4797 }
John Abd-El-Malekfde08e6da2017-12-05 04:06:244798
4799 SetNotWaitingForResponse();
clamyefca29e2015-09-17 00:22:114800}
4801
clamy0e119882015-07-31 16:12:334802void WebContentsImpl::DidFinishNavigation(NavigationHandle* navigation_handle) {
Charles Harrison53096ba2017-12-15 15:39:484803 TRACE_EVENT1("navigation", "WebContentsImpl::DidFinishNavigation",
4804 "navigation_handle", navigation_handle);
Steven Holte38041342018-06-22 22:00:574805
ericwilligersde386342016-10-19 02:35:094806 for (auto& observer : observers_)
4807 observer.DidFinishNavigation(navigation_handle);
clamy0e119882015-07-31 16:12:334808
Becca Hughesd11d6502018-07-31 17:01:284809 if (display_cutout_host_impl_)
4810 display_cutout_host_impl_->DidFinishNavigation(navigation_handle);
4811
jam5f358e52017-02-13 16:37:034812 if (navigation_handle->HasCommitted()) {
Ian Prestcdd843d2020-01-28 18:59:584813 // TODO(domfarolino, dmazzoni): Do this using WebContentsObserver. See
4814 // https://crbug.com/981271.
4815 BrowserAccessibilityManager* manager =
4816 static_cast<RenderFrameHostImpl*>(
4817 navigation_handle->GetRenderFrameHost())
4818 ->browser_accessibility_manager();
4819 if (manager) {
4820 if (navigation_handle->IsErrorPage()) {
4821 manager->NavigationFailed();
4822 } else {
4823 manager->NavigationSucceeded();
4824 }
4825 }
4826
Yue Ru Sun979c4b162019-11-06 16:11:044827 if (navigation_handle->IsInMainFrame()) {
4828 last_committed_source_id_including_same_document_ =
Steven Holte38041342018-06-22 22:00:574829 ukm::ConvertToSourceId(navigation_handle->GetNavigationId(),
4830 ukm::SourceIdType::NAVIGATION_ID);
Yue Ru Sun979c4b162019-11-06 16:11:044831
4832 if (!navigation_handle->IsSameDocument()) {
4833 was_ever_audible_ = false;
Yue Ru Sun979c4b162019-11-06 16:11:044834 }
Tommy Steimel18360512017-11-01 00:38:194835 }
Lan Weif81c6e7c2020-02-12 16:46:094836
4837 if (!navigation_handle->IsSameDocument())
4838 last_screen_orientation_change_time_ = base::TimeTicks();
dmazzonibf8cec42015-02-08 08:28:084839 }
Xiyuan Xia2a66b112018-12-06 15:52:164840
4841 // If we didn't end up on about:blank after setting this in DidStartNavigation
4842 // then don't focus the location bar.
4843 if (should_focus_location_bar_by_default_ &&
4844 navigation_handle->GetURL() != url::kAboutBlankURL) {
4845 should_focus_location_bar_by_default_ = false;
4846 }
yilkal796e89e2019-10-02 23:58:284847
yilkal172da772019-11-06 22:49:074848 if (navigation_handle->IsInMainFrame() && first_navigation_completed_)
4849 RecordMaxFrameCountUMA(max_loaded_frame_count_);
yilkal796e89e2019-10-02 23:58:284850
yilkal172da772019-11-06 22:49:074851 // If navigation has successfully finished in the main frame, set
4852 // |first_navigation_completed_| to true so that we will record
4853 // |max_loaded_frame_count_| above when future main frame navigations finish.
4854 if (navigation_handle->IsInMainFrame() && !navigation_handle->IsErrorPage()) {
4855 first_navigation_completed_ = true;
4856
4857 // Navigation has completed in main frame. Reset |max_loaded_frame_count_|.
4858 // |max_loaded_frame_count_| is not necessarily 1 if the navigation was
4859 // served from BackForwardCache.
4860 max_loaded_frame_count_ =
4861 GetMainFrame()->frame_tree_node()->GetFrameTreeSize();
yilkal796e89e2019-10-02 23:58:284862 }
Rakina Zata Amni347b70902020-07-22 10:49:044863
4864 if (web_preferences_) {
4865 // Update the WebPreferences for this WebContents that depends on changes
4866 // that might occur during navigation. This will only update the preferences
4867 // that needs to be updated (and won't cause an update/overwrite preferences
4868 // that needs to stay the same after navigations).
4869 bool value_changed_due_to_override =
4870 GetContentClient()->browser()->OverrideWebPreferencesAfterNavigation(
4871 this, web_preferences_.get());
4872 // We need to update the WebPreferences value on the renderer if the value
4873 // is changed due to the override above, or if the navigation is served from
4874 // the back-forward cache, because the WebPreferences value stored in the
4875 // renderer might be stale (because we don't send WebPreferences updates to
4876 // bfcached renderers).
4877 // TODO(rakina): Maybe handle the back-forward cache case in
4878 // ReadyToCommitNavigation instead?
4879 if (value_changed_due_to_override ||
4880 NavigationRequest::From(navigation_handle)
4881 ->IsServedFromBackForwardCache()) {
4882 SetWebPreferences(*web_preferences_.get());
4883 }
4884 }
[email protected]400992b2012-06-14 00:03:544885}
4886
[email protected]b80624c2014-02-09 02:46:554887void WebContentsImpl::DidFailLoadWithError(
4888 RenderFrameHostImpl* render_frame_host,
[email protected]b80624c2014-02-09 02:46:554889 const GURL& url,
Dave Tapuska924ef3c2020-01-22 18:20:594890 int error_code) {
ericwilligersde386342016-10-19 02:35:094891 for (auto& observer : observers_) {
Dave Tapuska924ef3c2020-01-22 18:20:594892 observer.DidFailLoad(render_frame_host, url, error_code);
ericwilligersde386342016-10-19 02:35:094893 }
[email protected]b80624c2014-02-09 02:46:554894}
4895
[email protected]52913802013-12-10 05:52:184896void WebContentsImpl::NotifyChangedNavigationState(
4897 InvalidateTypes changed_flags) {
4898 NotifyNavigationStateChanged(changed_flags);
4899}
4900
lukasza1d025732016-09-28 21:36:084901bool WebContentsImpl::ShouldTransferNavigation(bool is_main_frame_navigation) {
creis29460272015-12-16 04:38:224902 if (!delegate_)
4903 return true;
lukasza1d025732016-09-28 21:36:084904 return delegate_->ShouldTransferNavigation(is_main_frame_navigation);
creis29460272015-12-16 04:38:224905}
4906
[email protected]aa62afd2014-04-22 19:22:464907bool WebContentsImpl::ShouldPreserveAbortedURLs() {
4908 if (!delegate_)
4909 return false;
4910 return delegate_->ShouldPreserveAbortedURLs(this);
4911}
4912
[email protected]0d0f4c492014-04-02 06:42:574913void WebContentsImpl::DidNavigateMainFramePreCommit(
[email protected]5cfbddc2014-06-23 23:52:234914 bool navigation_is_within_page) {
[email protected]0d0f4c492014-04-02 06:42:574915 // Ensure fullscreen mode is exited before committing the navigation to a
4916 // different page. The next page will not start out assuming it is in
4917 // fullscreen mode.
[email protected]5cfbddc2014-06-23 23:52:234918 if (navigation_is_within_page) {
[email protected]dfc39cb2014-04-09 22:58:194919 // No page change? Then, the renderer and browser can remain in fullscreen.
[email protected]0d0f4c492014-04-02 06:42:574920 return;
4921 }
Avi Drissman97aef042020-06-30 21:04:484922 if (IsFullscreen())
bokanece34a82016-01-28 19:49:464923 ExitFullscreen(false);
Avi Drissman97aef042020-06-30 21:04:484924 DCHECK(!IsFullscreen());
Joe Downingc1a57efe2018-05-09 18:23:544925
4926 // Clean up keyboard lock state when navigating.
4927 CancelKeyboardLock(keyboard_lock_widget_);
[email protected]0d0f4c492014-04-02 06:42:574928}
4929
[email protected]37567b432014-02-12 01:12:224930void WebContentsImpl::DidNavigateMainFramePostCommit(
sky8b00392d2015-01-10 00:30:284931 RenderFrameHostImpl* render_frame_host,
[email protected]37567b432014-02-12 01:12:224932 const LoadCommittedDetails& details,
4933 const FrameHostMsg_DidCommitProvisionalLoad_Params& params) {
4934 if (details.is_navigation_to_different_page()) {
4935 // Clear the status bubble. This is a workaround for a bug where WebKit
4936 // doesn't let us know that the cursor left an element during a
4937 // transition (this is also why the mouse cursor remains as a hand after
4938 // clicking on a link); see bugs 1184641 and 980803. We don't want to
4939 // clear the bubble when a user navigates to a named anchor in the same
4940 // page.
Lukasz Anforowiczfd707142018-02-07 19:46:134941 ClearTargetURL();
lanwei9d343ad22015-02-11 01:46:004942
4943 RenderWidgetHostViewBase* rwhvb =
4944 static_cast<RenderWidgetHostViewBase*>(GetRenderWidgetHostView());
4945 if (rwhvb)
4946 rwhvb->OnDidNavigateMainFrameToNewPage();
[email protected]37567b432014-02-12 01:12:224947 }
4948
[email protected]e85165c642014-06-10 14:34:314949 if (delegate_)
[email protected]37567b432014-02-12 01:12:224950 delegate_->DidNavigateMainFramePostCommit(this);
[email protected]e85165c642014-06-10 14:34:314951 view_->SetOverscrollControllerEnabled(CanOverscrollContent());
Shivani Sharma985474f2019-05-23 21:36:474952
Carlos Caballero91fffb22019-10-29 15:24:304953 if (details.is_navigation_to_different_page() &&
4954 GetRenderViewHost()->did_first_visually_non_empty_paint()) {
4955 // This event will not fire again if the page is restored from the
4956 // BackForwardCache. So fire it ourselves if needed.
4957 DidFirstVisuallyNonEmptyPaint(GetRenderViewHost());
4958 }
Carlos Caballero1215f882019-10-29 15:58:434959
4960 if (GetRenderViewHost()->theme_color() != last_sent_theme_color_) {
4961 // This event will not fire again if the page is restored from the
4962 // BackForwardCache. So fire it ourselves if needed.
4963 OnThemeColorChanged(GetRenderViewHost());
4964 }
[email protected]37567b432014-02-12 01:12:224965}
4966
4967void WebContentsImpl::DidNavigateAnyFramePostCommit(
4968 RenderFrameHostImpl* render_frame_host,
4969 const LoadCommittedDetails& details,
4970 const FrameHostMsg_DidCommitProvisionalLoad_Params& params) {
[email protected]b4c84012014-04-28 19:51:104971 // Now that something has committed, we don't need to track whether the
4972 // initial page has been accessed.
4973 has_accessed_initial_document_ = false;
4974
[email protected]37567b432014-02-12 01:12:224975 // If we navigate off the page, close all JavaScript dialogs.
eugenebutee08663a2017-04-27 17:43:124976 if (!details.is_same_document)
creis89a0f782015-05-27 16:13:174977 CancelActiveAndPendingDialogs();
[email protected]37567b432014-02-12 01:12:224978
avid53461d2016-02-25 17:15:044979 // If this is a user-initiated navigation, start allowing JavaScript dialogs
4980 // again.
avi5d3b8692016-10-12 22:00:464981 if (params.gesture == NavigationGestureUser && dialog_manager_) {
avi6879fcf2017-01-21 05:27:534982 dialog_manager_->CancelDialogs(this, /*reset_state=*/true);
avi5d3b8692016-10-12 22:00:464983 }
[email protected]37567b432014-02-12 01:12:224984}
4985
[email protected]277857a2014-06-03 10:38:014986bool WebContentsImpl::CanOverscrollContent() const {
[email protected]e85165c642014-06-10 14:34:314987 // Disable overscroll when touch emulation is on. See crbug.com/369938.
[email protected]34ff1cfc2014-08-20 06:16:054988 if (force_disable_overscroll_content_)
[email protected]e85165c642014-06-10 14:34:314989 return false;
4990
[email protected]37567b432014-02-12 01:12:224991 if (delegate_)
4992 return delegate_->CanOverscrollContent();
4993
4994 return false;
4995}
4996
Carlos Caballero1215f882019-10-29 15:58:434997void WebContentsImpl::OnThemeColorChanged(RenderViewHostImpl* source) {
4998 if (source->did_first_visually_non_empty_paint() &&
4999 last_sent_theme_color_ != source->theme_color()) {
ericwilligersde386342016-10-19 02:35:095000 for (auto& observer : observers_)
Peter Kastingb14e5782020-01-06 23:55:045001 observer.DidChangeThemeColor();
Carlos Caballero1215f882019-10-29 15:58:435002 last_sent_theme_color_ = source->theme_color();
peter90afaba2015-06-01 12:05:295003 }
[email protected]e7104762014-06-20 19:17:255004}
5005
Gyuyoung Kim1905f7c2020-05-02 00:00:245006void WebContentsImpl::DidLoadResourceFromMemoryCache(
nicka0ac8382016-12-15 23:59:235007 RenderFrameHostImpl* source,
[email protected]724159a2010-12-30 01:11:185008 const GURL& url,
[email protected]70435962011-08-02 20:13:285009 const std::string& http_method,
[email protected]6d6cfb3a2012-05-23 22:53:185010 const std::string& mime_type,
Kinuko Yasuda433f710a2020-02-14 02:11:585011 network::mojom::RequestDestination request_destination) {
ericwilligersde386342016-10-19 02:35:095012 for (auto& observer : observers_)
Kinuko Yasuda433f710a2020-02-14 02:11:585013 observer.DidLoadResourceFromMemoryCache(url, mime_type,
5014 request_destination);
[email protected]b0f724c2013-09-05 04:21:135015
Steven Bingler674fb0822018-12-12 18:04:525016 if (url.is_valid() && url.SchemeIsHTTPOrHTTPS()) {
nicka0ac8382016-12-15 23:59:235017 StoragePartition* partition = source->GetProcess()->GetStoragePartition();
John Abd-El-Malekf4fe9d82019-07-31 04:05:595018 partition->GetNetworkContext()->NotifyExternalCacheHit(
Matt Menke2a33dee2019-12-02 17:01:545019 url, http_method, source->GetNetworkIsolationKey());
[email protected]8bfc8272013-09-09 20:10:535020 }
[email protected]724159a2010-12-30 01:11:185021}
5022
carloskd9d97942017-02-16 08:58:095023void WebContentsImpl::DidDisplayInsecureContent() {
estarkc2273502016-10-12 22:03:025024 controller_.ssl_manager()->DidDisplayMixedContent();
[email protected]724159a2010-12-30 01:11:185025}
5026
Dave Tapuskaa657d872019-10-21 20:23:215027void WebContentsImpl::DidContainInsecureFormAction() {
elawrenceb2ac2a232017-03-27 21:46:255028 controller_.ssl_manager()->DidContainInsecureFormAction();
5029}
5030
Julie Jeongeun Kim0b103492020-01-29 05:30:515031void WebContentsImpl::DocumentAvailableInMainFrame() {
5032 for (auto& observer : observers_)
5033 observer.DocumentAvailableInMainFrame();
5034}
5035
nicka0ac8382016-12-15 23:59:235036void WebContentsImpl::OnDidRunInsecureContent(RenderFrameHostImpl* source,
5037 const GURL& security_origin,
estark910b4572015-12-09 20:55:415038 const GURL& target_url) {
nicka0ac8382016-12-15 23:59:235039 // TODO(nick, estark): Should we call FilterURL using |source|'s process on
5040 // these parameters? |target_url| seems unused, except for a log message. And
5041 // |security_origin| might be replaceable with the origin of the main frame.
carloskd9d97942017-02-16 08:58:095042 DidRunInsecureContent(security_origin, target_url);
5043}
5044
5045void WebContentsImpl::DidRunInsecureContent(const GURL& security_origin,
5046 const GURL& target_url) {
[email protected]9450c462013-11-23 01:22:585047 LOG(WARNING) << security_origin << " ran insecure content from "
5048 << target_url.possibly_invalid_spec();
[email protected]e6e30ac2014-01-13 21:24:395049 RecordAction(base::UserMetricsAction("SSL.RanInsecureContent"));
estark910b4572015-12-09 20:55:415050 if (base::EndsWith(security_origin.spec(), kDotGoogleDotCom,
brettwa7ff1b292015-07-16 17:49:295051 base::CompareCase::INSENSITIVE_ASCII))
[email protected]e6e30ac2014-01-13 21:24:395052 RecordAction(base::UserMetricsAction("SSL.RanInsecureContentGoogle"));
estarkc2273502016-10-12 22:03:025053 controller_.ssl_manager()->DidRunMixedContent(security_origin);
[email protected]724159a2010-12-30 01:11:185054}
5055
carloskd9d97942017-02-16 08:58:095056void WebContentsImpl::PassiveInsecureContentFound(const GURL& resource_url) {
Sergey Kuznetsov3ebc01b2018-02-01 04:57:365057 if (delegate_) {
5058 delegate_->PassiveInsecureContentFound(resource_url);
5059 }
carloskd9d97942017-02-16 08:58:095060}
5061
5062bool WebContentsImpl::ShouldAllowRunningInsecureContent(
carloskd9d97942017-02-16 08:58:095063 bool allowed_per_prefs,
5064 const url::Origin& origin,
5065 const GURL& resource_url) {
Sergey Kuznetsov3ebc01b2018-02-01 04:57:365066 if (delegate_) {
Carlos IL29294a62020-05-15 00:21:165067 return delegate_->ShouldAllowRunningInsecureContent(this, allowed_per_prefs,
5068 origin, resource_url);
Sergey Kuznetsov3ebc01b2018-02-01 04:57:365069 }
5070
5071 return allowed_per_prefs;
carloskd9d97942017-02-16 08:58:095072}
5073
Lukasz Anforowicze1b954d92017-10-30 21:28:065074void WebContentsImpl::ViewSource(RenderFrameHostImpl* frame) {
5075 DCHECK_EQ(this, WebContents::FromRenderFrameHost(frame));
5076
5077 // Don't do anything if there is no |delegate_| that could accept and show the
5078 // new WebContents containing the view-source.
5079 if (!delegate_)
5080 return;
5081
5082 // Use the last committed entry, since the pending entry hasn't loaded yet and
5083 // won't be copied into the cloned tab.
5084 NavigationEntryImpl* last_committed_entry =
Fergal Daly09d6c762020-05-29 02:05:185085 GetController().GetLastCommittedEntry();
Lukasz Anforowicze1b954d92017-10-30 21:28:065086 if (!last_committed_entry)
5087 return;
5088
5089 FrameNavigationEntry* frame_entry =
5090 last_committed_entry->GetFrameEntry(frame->frame_tree_node());
5091 if (!frame_entry)
5092 return;
5093
5094 // Any new WebContents opened while this WebContents is in fullscreen can be
5095 // used to confuse the user, so drop fullscreen.
Avi Drissman1a55a9d62020-03-10 18:56:455096 base::ScopedClosureRunner fullscreen_block = ForSecurityDropFullscreen();
5097 // The new view source contents will be independent of this contents, so
5098 // release the fullscreen block.
5099 fullscreen_block.RunAndReset();
Lukasz Anforowicze1b954d92017-10-30 21:28:065100
5101 // We intentionally don't share the SiteInstance with the original frame so
5102 // that view source has a consistent process model and always ends up in a new
5103 // process (https://crbug.com/699493).
5104 scoped_refptr<SiteInstanceImpl> site_instance_for_view_source = nullptr;
Lukasz Anforowicz435bcb582019-07-12 20:50:065105 // Referrer and initiator are not important, because view-source should not
5106 // hit the network, but should be served from the cache instead.
Lukasz Anforowicze1b954d92017-10-30 21:28:065107 Referrer referrer_for_view_source;
Lukasz Anforowicz435bcb582019-07-12 20:50:065108 base::Optional<url::Origin> initiator_for_view_source = base::nullopt;
Lukasz Anforowicze1b954d92017-10-30 21:28:065109 // Do not restore title, derive it from the url.
5110 base::string16 title_for_view_source;
5111 auto navigation_entry = std::make_unique<NavigationEntryImpl>(
5112 site_instance_for_view_source, frame_entry->url(),
Lukasz Anforowicz435bcb582019-07-12 20:50:065113 referrer_for_view_source, initiator_for_view_source,
5114 title_for_view_source, ui::PAGE_TRANSITION_LINK,
Marijn Kruisselbrink7a0d5e182018-05-24 22:55:095115 /* is_renderer_initiated = */ false,
5116 /* blob_url_loader_factory = */ nullptr);
Joel Hockey891e88062020-04-30 05:38:445117 const GURL url(content::kViewSourceScheme + std::string(":") +
5118 frame_entry->url().spec());
5119 navigation_entry->SetVirtualURL(url);
Matt Menkea56c5992020-04-10 20:53:565120 navigation_entry->set_isolation_info(
5121 frame->GetIsolationInfoForSubresources());
Yao Xiaof68f57d2019-10-03 04:20:055122
Lukasz Anforowicze1b954d92017-10-30 21:28:065123 // Do not restore scroller position.
5124 // TODO(creis, lukasza, arthursonzogni): Do not reuse the original PageState,
5125 // but start from a new one and only copy the needed data.
5126 const PageState& new_page_state =
5127 frame_entry->page_state().RemoveScrollOffset();
5128
5129 scoped_refptr<FrameNavigationEntry> new_frame_entry =
5130 navigation_entry->root_node()->frame_entry;
5131 new_frame_entry->set_method(frame_entry->method());
5132 new_frame_entry->SetPageState(new_page_state);
5133
5134 // Create a new WebContents, which is used to display the source code.
erikchenbee5c9622018-04-27 19:30:255135 std::unique_ptr<WebContents> view_source_contents =
Erik Chenbb8e738e2018-04-28 14:10:435136 Create(CreateParams(GetBrowserContext()));
Lukasz Anforowicze1b954d92017-10-30 21:28:065137
5138 // Restore the previously created NavigationEntry.
5139 std::vector<std::unique_ptr<NavigationEntry>> navigation_entries;
5140 navigation_entries.push_back(std::move(navigation_entry));
5141 view_source_contents->GetController().Restore(0, RestoreType::CURRENT_SESSION,
5142 &navigation_entries);
5143
5144 // Add |view_source_contents| as a new tab.
5145 gfx::Rect initial_rect;
5146 constexpr bool kUserGesture = true;
5147 bool ignored_was_blocked;
Joel Hockey891e88062020-04-30 05:38:445148 delegate_->AddNewContents(this, std::move(view_source_contents), url,
Lukasz Anforowicze1b954d92017-10-30 21:28:065149 WindowOpenDisposition::NEW_FOREGROUND_TAB,
5150 initial_rect, kUserGesture, &ignored_was_blocked);
5151 // Note that the |delegate_| could have deleted |view_source_contents| during
5152 // AddNewContents method call.
5153}
5154
Lukasz Anforowiczd334bba2020-02-10 23:32:555155void WebContentsImpl::SubresourceResponseStarted(const GURL& url,
5156 net::CertStatus cert_status) {
5157 controller_.ssl_manager()->DidStartResourceResponse(url, cert_status);
Jay Civellid5904f02018-03-23 19:43:575158 SetNotWaitingForResponse();
5159}
5160
Jay Civelli116683f2018-03-27 19:56:235161void WebContentsImpl::ResourceLoadComplete(
Jay Civelli52a63302018-04-30 22:39:555162 RenderFrameHost* render_frame_host,
Clark DuVall36164bd2018-08-09 22:49:085163 const GlobalRequestID& request_id,
Minggang Wangee5af392020-02-05 02:55:285164 blink::mojom::ResourceLoadInfoPtr resource_load_info) {
John Abd-El-Malek9b8f7fa2017-12-04 23:52:255165 for (auto& observer : observers_) {
Clark DuVall36164bd2018-08-09 22:49:085166 observer.ResourceLoadComplete(render_frame_host, request_id,
5167 *resource_load_info);
John Abd-El-Malek9b8f7fa2017-12-04 23:52:255168 }
John Abd-El-Malekd4882642017-12-04 21:45:195169}
5170
Rakina Zata Amni347b70902020-07-22 10:49:045171const WebPreferences& WebContentsImpl::GetOrCreateWebPreferences() {
5172 // Compute WebPreferences based on the current state if it's null.
5173 if (!web_preferences_)
Rakina Zata Amni4029b6d2020-07-28 02:36:205174 OnWebPreferencesChanged();
Rakina Zata Amni347b70902020-07-22 10:49:045175 return *web_preferences_.get();
5176}
5177
5178bool WebContentsImpl::IsWebPreferencesSet() const {
5179 return web_preferences_.get();
5180}
5181
5182void WebContentsImpl::SetWebPreferences(const WebPreferences& prefs) {
5183 web_preferences_ = std::make_unique<WebPreferences>(prefs);
5184 // Get all the RenderViewHosts (except the ones for currently back-forward
5185 // cached pages), and make them send the current WebPreferences
5186 // to the renderer. WebPreferences updates for back-forward cached pages will
5187 // be sent when we restore those pages from the back-forward cache.
5188 for (auto& rvh : frame_tree_.render_view_hosts()) {
5189 rvh.second->SendWebPreferencesToRenderer();
5190 }
5191}
5192
5193void WebContentsImpl::RecomputeWebPreferencesSlow() {
Rakina Zata Amni4029b6d2020-07-28 02:36:205194 // OnWebPreferencesChanged is a no-op when this is true.
5195 if (updating_web_preferences_)
5196 return;
Rakina Zata Amni347b70902020-07-22 10:49:045197 // Resets |web_preferences_| so that we won't have any cached value for slow
5198 // attributes (which won't get recomputed if we have pre-existing values for
5199 // them).
5200 web_preferences_.reset();
Rakina Zata Amni4029b6d2020-07-28 02:36:205201 OnWebPreferencesChanged();
Rakina Zata Amni347b70902020-07-22 10:49:045202}
5203
Wei Li5bb659742018-02-14 03:07:585204void WebContentsImpl::PrintCrossProcessSubframe(
5205 const gfx::Rect& rect,
5206 int document_cookie,
5207 RenderFrameHost* subframe_host) {
Wei Li5c0b4d12018-03-22 15:05:225208 auto* outer_contents = GetOuterWebContents();
5209 if (outer_contents) {
5210 // When an extension or app page is printed, the content should be
5211 // composited with outer content, so the outer contents should handle the
5212 // print request.
5213 outer_contents->PrintCrossProcessSubframe(rect, document_cookie,
5214 subframe_host);
5215 return;
5216 }
5217
Wei Li5bb659742018-02-14 03:07:585218 // If there is no delegate such as in tests or during deletion, do nothing.
5219 if (!delegate_)
5220 return;
5221
5222 delegate_->PrintCrossProcessSubframe(this, rect, document_cookie,
5223 subframe_host);
5224}
5225
ckitagawa8695799b2020-02-05 16:08:495226void WebContentsImpl::CapturePaintPreviewOfCrossProcessSubframe(
5227 const gfx::Rect& rect,
5228 const base::UnguessableToken& guid,
5229 RenderFrameHost* render_frame_host) {
5230 if (!delegate_)
5231 return;
ckitagawac43ac982020-07-07 17:41:175232 delegate_->CapturePaintPreviewOfSubframe(this, rect, guid, render_frame_host);
ckitagawa8695799b2020-02-05 16:08:495233}
5234
rob.buis300b0872017-03-10 20:43:585235#if defined(OS_ANDROID)
5236base::android::ScopedJavaLocalRef<jobject>
5237WebContentsImpl::GetJavaRenderFrameHostDelegate() {
5238 return GetJavaWebContents();
5239}
5240#endif
5241
Hiroki Nakagawa39d1bbe2020-07-14 16:15:525242void WebContentsImpl::OnDidDisplayContentWithCertificateErrors() {
estarkc2273502016-10-12 22:03:025243 controller_.ssl_manager()->DidDisplayContentWithCertErrors();
estark910b4572015-12-09 20:55:415244}
5245
5246void WebContentsImpl::OnDidRunContentWithCertificateErrors(
Zhuoyu Qian4353f7f2018-01-08 05:33:005247 RenderFrameHostImpl* source) {
Emily Starkabbdd662020-06-05 02:40:135248 // For RenderFrameHosts that are inactive and going to be discarded, we can
5249 // disregard this message; there's no need to update the UI if the UI will
5250 // never be shown again.
5251 //
5252 // We still process this message for speculative RenderFrameHosts. This can
5253 // happen when a subframe's main resource has a certificate error. The
5254 // currently committed navigation entry will get marked as having run insecure
5255 // content and that will carry over to the navigation entry for the
5256 // speculative RFH when it commits.
5257 if (source->lifecycle_state() !=
5258 RenderFrameHostImpl::LifecycleState::kSpeculative &&
5259 source->IsInactiveAndDisallowReactivation()) {
estarkaeda1422016-05-10 02:31:125260 return;
Emily Starkabbdd662020-06-05 02:40:135261 }
estarkcd2e30c2016-08-12 06:51:155262 controller_.ssl_manager()->DidRunContentWithCertErrors(
Emily Starkabbdd662020-06-05 02:40:135263 source->GetMainFrame()->GetLastCommittedOrigin().GetURL());
estark910b4572015-12-09 20:55:415264}
5265
Kouhei Ueno97e0990d2019-09-25 02:57:275266void WebContentsImpl::DOMContentLoaded(RenderFrameHost* render_frame_host) {
ericwilligersde386342016-10-19 02:35:095267 for (auto& observer : observers_)
Kouhei Uenof6e96242019-09-27 01:54:355268 observer.DOMContentLoaded(render_frame_host);
[email protected]724159a2010-12-30 01:11:185269}
5270
Gyuyoung Kim525e0c5d2020-03-09 15:04:195271void WebContentsImpl::OnDidFinishLoad(RenderFrameHost* render_frame_host,
nicka0ac8382016-12-15 23:59:235272 const GURL& url) {
[email protected]8b3af1e2014-01-24 13:29:125273 GURL validated_url(url);
Gyuyoung Kim525e0c5d2020-03-09 15:04:195274 render_frame_host->GetProcess()->FilterURL(false, &validated_url);
[email protected]028053d42014-03-05 22:20:375275
ericwilligersde386342016-10-19 02:35:095276 for (auto& observer : observers_)
Gyuyoung Kim525e0c5d2020-03-09 15:04:195277 observer.DidFinishLoad(render_frame_host, validated_url);
yilkal172da772019-11-06 22:49:075278
5279 size_t tree_size = frame_tree_.root()->GetFrameTreeSize();
5280 if (max_loaded_frame_count_ < tree_size)
5281 max_loaded_frame_count_ = tree_size;
5282
Gyuyoung Kim525e0c5d2020-03-09 15:04:195283 if (!render_frame_host->GetParent())
yilkal172da772019-11-06 22:49:075284 UMA_HISTOGRAM_COUNTS_1000("Navigation.MainFrame.FrameCount", tree_size);
[email protected]1a55c5be2011-11-29 11:36:315285}
5286
Dave Tapuska549baab2019-03-19 15:52:405287void WebContentsImpl::OnGoToEntryAtOffset(RenderFrameHostImpl* source,
Julie Jeongeun Kim144f8192020-01-18 06:59:035288 int32_t offset,
Dave Tapuska043b94a2019-09-10 14:14:355289 bool has_user_gesture) {
Arthur Sonzognia7d715a2018-09-20 16:11:135290 // Non-user initiated navigations coming from the renderer should be ignored
5291 // if there is an ongoing browser-initiated navigation.
5292 // See https://crbug.com/879965.
5293 // TODO(arthursonzogni): See if this should check for ongoing navigations in
5294 // the frame(s) affected by the session history navigation, rather than just
5295 // the main frame.
Kevin McNee96851e12020-07-16 03:43:225296 if (Navigator::ShouldIgnoreIncomingRendererRequest(
5297 frame_tree_.root()->navigation_request(), has_user_gesture))
5298 return;
Arthur Sonzognia7d715a2018-09-20 16:11:135299
nicka0ac8382016-12-15 23:59:235300 // All frames are allowed to navigate the global history.
Dave Tapuska8bfd84c2019-03-26 20:47:165301 if (!delegate_ || delegate_->OnGoToEntryOffset(offset)) {
arthursonzognib93a4472020-04-10 07:38:005302 if (source->IsSandboxed(network::mojom::WebSandboxFlags::kTopNavigation)) {
Dave Tapuska8bfd84c2019-03-26 20:47:165303 // Keep track of whether this is a session history from a sandboxed iframe
5304 // with top level navigation disallowed.
5305 controller_.GoToOffsetInSandboxedFrame(offset,
5306 source->GetFrameTreeNodeId());
5307 } else {
5308 controller_.GoToOffset(offset);
5309 }
5310 }
[email protected]216813952011-05-19 22:21:265311}
5312
Dave Tapuska4c02911b2020-02-14 22:52:075313void WebContentsImpl::OnPageScaleFactorChanged(RenderFrameHostImpl* source,
nicka0ac8382016-12-15 23:59:235314 float page_scale_factor) {
mcnee336ea2c2017-05-23 22:50:595315#if !defined(OS_ANDROID)
5316 // While page scale factor is used on mobile, this PageScaleFactorIsOne logic
5317 // is only needed on desktop.
mcnee432e47d2015-11-09 19:37:465318 bool is_one = page_scale_factor == 1.f;
5319 if (is_one != page_scale_factor_is_one_) {
5320 page_scale_factor_is_one_ = is_one;
5321
5322 HostZoomMapImpl* host_zoom_map =
5323 static_cast<HostZoomMapImpl*>(HostZoomMap::GetForWebContents(this));
5324
nicka0ac8382016-12-15 23:59:235325 if (host_zoom_map) {
mcnee432e47d2015-11-09 19:37:465326 host_zoom_map->SetPageScaleFactorIsOneForView(
Dave Tapuska4c02911b2020-02-14 22:52:075327 source->GetProcess()->GetID(),
5328 source->GetRenderViewHost()->GetRoutingID(),
mcnee432e47d2015-11-09 19:37:465329 page_scale_factor_is_one_);
5330 }
5331 }
mcnee336ea2c2017-05-23 22:50:595332#endif // !defined(OS_ANDROID)
mcnee432e47d2015-11-09 19:37:465333
ericwilligersde386342016-10-19 02:35:095334 for (auto& observer : observers_)
5335 observer.OnPageScaleFactorChanged(page_scale_factor);
mcnee432e47d2015-11-09 19:37:465336}
5337
W. James MacLean62198672019-06-04 16:46:105338void WebContentsImpl::OnTextAutosizerPageInfoChanged(
Henrique Ferreiroba535472020-04-21 11:38:365339 RenderFrameHostImpl* source,
5340 blink::mojom::TextAutosizerPageInfoPtr page_info) {
W. James MacLean62198672019-06-04 16:46:105341 // Keep a copy of |page_info| in case we create a new RenderView before
5342 // the next update.
Henrique Ferreiroba535472020-04-21 11:38:365343 text_autosizer_page_info_.main_frame_width = page_info->main_frame_width;
5344 text_autosizer_page_info_.main_frame_layout_width =
5345 page_info->main_frame_layout_width;
5346 text_autosizer_page_info_.device_scale_adjustment =
5347 page_info->device_scale_adjustment;
W. James MacLean62198672019-06-04 16:46:105348 frame_tree_.root()->render_manager()->SendPageMessage(
5349 new PageMsg_UpdateTextAutosizerPageInfoForRemoteMainFrames(
Henrique Ferreiroba535472020-04-21 11:38:365350 MSG_ROUTING_NONE, text_autosizer_page_info_),
W. James MacLean62198672019-06-04 16:46:105351 source->GetSiteInstance());
5352}
5353
Kent Tamura8c9e0562018-11-06 07:02:195354void WebContentsImpl::EnumerateDirectory(
5355 RenderFrameHost* render_frame_host,
Kent Tamura3abb32d2020-07-02 00:23:015356 scoped_refptr<FileChooserImpl::FileSelectListenerImpl> listener,
Kent Tamura8c9e0562018-11-06 07:02:195357 const base::FilePath& directory_path) {
Avi Drissman1a55a9d62020-03-10 18:56:455358 // Any explicit focusing of another window while this WebContents is in
5359 // fullscreen can be used to confuse the user, so drop fullscreen.
5360 base::ScopedClosureRunner fullscreen_block = ForSecurityDropFullscreen();
5361 listener->SetFullscreenBlock(std::move(fullscreen_block));
5362
Kent Tamura8c9e0562018-11-06 07:02:195363 if (delegate_)
5364 delegate_->EnumerateDirectory(this, std::move(listener), directory_path);
5365 else
5366 listener->FileSelectionCanceled();
[email protected]3a29a6e2011-08-24 18:26:215367}
5368
Dave Tapuskaf9fc3122019-10-18 19:42:555369void WebContentsImpl::RegisterProtocolHandler(RenderFrameHostImpl* source,
5370 const std::string& protocol,
5371 const GURL& url,
5372 const base::string16& title,
5373 bool user_gesture) {
nicka0ac8382016-12-15 23:59:235374 // TODO(nick): Do we need to apply FilterURL to |url|?
[email protected]e5f2de02012-07-20 22:15:435375 if (!delegate_)
5376 return;
5377
Raymes Khoury58373df2019-08-06 06:29:205378 if (!AreValidRegisterProtocolHandlerArguments(
5379 protocol, url, source->GetLastCommittedOrigin())) {
5380 ReceivedBadMessage(source->GetProcess(),
5381 bad_message::REGISTER_PROTOCOL_HANDLER_INVALID_URL);
[email protected]b9535422012-02-09 01:47:595382 return;
Raymes Khoury58373df2019-08-06 06:29:205383 }
[email protected]8f810632013-06-06 22:33:325384
Carlos Caballero8a6c6b72020-07-20 16:56:225385 delegate_->RegisterProtocolHandler(source, protocol, url, user_gesture);
[email protected]7d189022011-08-25 22:54:205386}
5387
Dave Tapuskaf9fc3122019-10-18 19:42:555388void WebContentsImpl::UnregisterProtocolHandler(RenderFrameHostImpl* source,
5389 const std::string& protocol,
5390 const GURL& url,
5391 bool user_gesture) {
nicka0ac8382016-12-15 23:59:235392 // TODO(nick): Do we need to apply FilterURL to |url|?
[email protected]f5273e52014-07-14 16:30:205393 if (!delegate_)
5394 return;
5395
Raymes Khoury58373df2019-08-06 06:29:205396 if (!AreValidRegisterProtocolHandlerArguments(
5397 protocol, url, source->GetLastCommittedOrigin())) {
5398 ReceivedBadMessage(source->GetProcess(),
5399 bad_message::REGISTER_PROTOCOL_HANDLER_INVALID_URL);
[email protected]f5273e52014-07-14 16:30:205400 return;
Raymes Khoury58373df2019-08-06 06:29:205401 }
[email protected]f5273e52014-07-14 16:30:205402
Carlos Caballero8a6c6b72020-07-20 16:56:225403 delegate_->UnregisterProtocolHandler(source, protocol, url, user_gesture);
[email protected]f5273e52014-07-14 16:30:205404}
5405
Marijn Kruisselbrink7f519672019-02-05 19:12:505406void WebContentsImpl::OnAppCacheAccessed(const GURL& manifest_url,
[email protected]b172aee2012-04-10 17:05:265407 bool blocked_by_policy) {
nicka0ac8382016-12-15 23:59:235408 // TODO(nick): Should we consider |source| here? Should we call FilterURL on
5409 // |manifest_url|?
5410
[email protected]7fc4bbb2011-09-08 21:23:105411 // Notify observers about navigation.
ericwilligersde386342016-10-19 02:35:095412 for (auto& observer : observers_)
5413 observer.AppCacheAccessed(manifest_url, blocked_by_policy);
[email protected]7fc4bbb2011-09-08 21:23:105414}
5415
Gyuyoung Kimc5f4ac12020-05-08 10:59:395416void WebContentsImpl::DomOperationResponse(const std::string& json_string) {
5417 // TODO(lukasza): The notification below should probably indicate which
5418 // RenderFrameHostImpl the message is coming from. This can enable tests to
5419 // talk to 2 frames at the same time, without being confused which frame a
5420 // given response comes from. See also a corresponding TODO in the
5421 // ExecuteScriptHelper in //content/public/test/browser_test_utils.cc
5422 NotificationService::current()->Notify(
5423 NOTIFICATION_DOM_OPERATION_RESPONSE, Source<WebContents>(this),
5424 Details<const std::string>(&json_string));
5425}
5426
Gyuyoung Kim4c50517842020-06-04 17:50:445427void WebContentsImpl::SavableResourceLinksResponse(
5428 RenderFrameHostImpl* source,
5429 const std::vector<GURL>& resources_list,
5430 blink::mojom::ReferrerPtr referrer,
5431 const std::vector<blink::mojom::SavableSubframePtr>& subframes) {
5432 save_package_->SavableResourceLinksResponse(source, resources_list,
5433 std::move(referrer), subframes);
5434}
5435
5436void WebContentsImpl::SavableResourceLinksError(RenderFrameHostImpl* source) {
5437 save_package_->SavableResourceLinksError(source);
5438}
5439
Alexander Timin17edc742020-04-23 18:22:185440void WebContentsImpl::OnServiceWorkerAccessed(
5441 RenderFrameHost* render_frame_host,
5442 const GURL& scope,
5443 AllowServiceWorkerResult allowed) {
5444 for (auto& observer : observers_) {
5445 observer.OnServiceWorkerAccessed(render_frame_host, scope, allowed);
5446 }
5447}
5448
5449void WebContentsImpl::OnServiceWorkerAccessed(
5450 NavigationHandle* navigation,
5451 const GURL& scope,
5452 AllowServiceWorkerResult allowed) {
5453 for (auto& observer : observers_) {
5454 observer.OnServiceWorkerAccessed(navigation, scope, allowed);
5455 }
5456}
5457
Miyoung Shincb6475a2019-11-09 10:49:455458void WebContentsImpl::OnColorChooserFactoryReceiver(
Julie Jeongeun Kim8e2879e2019-08-06 23:55:205459 mojo::PendingReceiver<blink::mojom::ColorChooserFactory> receiver) {
5460 color_chooser_factory_receivers_.Add(this, std::move(receiver));
Joel Hockey85b379d2017-12-11 10:42:135461}
5462
5463void WebContentsImpl::OpenColorChooser(
Julie Jeongeun Kim8e2879e2019-08-06 23:55:205464 mojo::PendingReceiver<blink::mojom::ColorChooser> chooser_receiver,
5465 mojo::PendingRemote<blink::mojom::ColorChooserClient> client,
[email protected]8ed16472014-04-11 19:02:485466 SkColor color,
Joel Hockey163835a2017-12-20 11:51:575467 std::vector<blink::mojom::ColorSuggestionPtr> suggestions) {
Joel Hockey85b379d2017-12-11 10:42:135468 content::ColorChooser* new_color_chooser =
Ivan Kotenkov2c0d2bb32017-11-01 15:41:285469 delegate_ ? delegate_->OpenColorChooser(this, color, suggestions)
5470 : nullptr;
[email protected]820957a2014-01-14 14:56:305471 if (!new_color_chooser)
[email protected]cb805452013-05-22 15:16:215472 return;
[email protected]8ed16472014-04-11 19:02:485473
Joel Hockey85b379d2017-12-11 10:42:135474 color_chooser_.reset();
5475 color_chooser_ = std::make_unique<ColorChooser>(
Julie Jeongeun Kim8e2879e2019-08-06 23:55:205476 new_color_chooser, std::move(chooser_receiver), std::move(client));
[email protected]da8543762012-03-20 08:52:205477}
5478
brettw4b461082016-11-19 18:55:165479#if BUILDFLAG(ENABLE_PLUGINS)
nicka0ac8382016-12-15 23:59:235480void WebContentsImpl::OnPepperInstanceCreated(RenderFrameHostImpl* source,
5481 int32_t pp_instance) {
ericwilligersde386342016-10-19 02:35:095482 for (auto& observer : observers_)
5483 observer.PepperInstanceCreated();
nicka0ac8382016-12-15 23:59:235484 pepper_playback_observer_->PepperInstanceCreated(source, pp_instance);
emaxxe70f5e12015-05-29 11:26:005485}
5486
nicka0ac8382016-12-15 23:59:235487void WebContentsImpl::OnPepperInstanceDeleted(RenderFrameHostImpl* source,
5488 int32_t pp_instance) {
ericwilligersde386342016-10-19 02:35:095489 for (auto& observer : observers_)
5490 observer.PepperInstanceDeleted();
nicka0ac8382016-12-15 23:59:235491 pepper_playback_observer_->PepperInstanceDeleted(source, pp_instance);
emaxxe70f5e12015-05-29 11:26:005492}
5493
nicka0ac8382016-12-15 23:59:235494void WebContentsImpl::OnPepperPluginHung(RenderFrameHostImpl* source,
5495 int plugin_child_id,
tommyclie6633ca72014-10-31 00:40:425496 const base::FilePath& path,
5497 bool is_hung) {
Steven Holte95922222018-09-14 20:06:235498 UMA_HISTOGRAM_COUNTS_1M("Pepper.PluginHung", 1);
tommyclie6633ca72014-10-31 00:40:425499
ericwilligersde386342016-10-19 02:35:095500 for (auto& observer : observers_)
5501 observer.PluginHungStatusChanged(plugin_child_id, path, is_hung);
tommyclie6633ca72014-10-31 00:40:425502}
5503
nicka0ac8382016-12-15 23:59:235504void WebContentsImpl::OnPepperStartsPlayback(RenderFrameHostImpl* source,
5505 int32_t pp_instance) {
5506 pepper_playback_observer_->PepperStartsPlayback(source, pp_instance);
zqzhang181047e62016-07-01 13:37:175507}
5508
nicka0ac8382016-12-15 23:59:235509void WebContentsImpl::OnPepperStopsPlayback(RenderFrameHostImpl* source,
5510 int32_t pp_instance) {
5511 pepper_playback_observer_->PepperStopsPlayback(source, pp_instance);
zqzhang181047e62016-07-01 13:37:175512}
5513
nicka0ac8382016-12-15 23:59:235514void WebContentsImpl::OnPluginCrashed(RenderFrameHostImpl* source,
5515 const base::FilePath& plugin_path,
tommyclie6633ca72014-10-31 00:40:425516 base::ProcessId plugin_pid) {
nicka0ac8382016-12-15 23:59:235517 // TODO(nick): Eliminate the |plugin_pid| parameter, which can't be trusted,
Gyuyoung Kim26c7bc92020-04-29 00:53:005518 // and is only used by WebTestControlHost.
ericwilligersde386342016-10-19 02:35:095519 for (auto& observer : observers_)
5520 observer.PluginCrashed(plugin_path, plugin_pid);
tommyclie6633ca72014-10-31 00:40:425521}
5522
[email protected]d8415ad92012-08-23 14:40:505523void WebContentsImpl::OnRequestPpapiBrokerPermission(
nicka0ac8382016-12-15 23:59:235524 RenderViewHostImpl* source,
5525 int ppb_broker_route_id,
[email protected]d8415ad92012-08-23 14:40:505526 const GURL& url,
[email protected]2dec8ec2013-02-07 19:20:345527 const base::FilePath& plugin_path) {
Avi Drissman0a2a967d2019-04-01 19:33:525528 base::OnceCallback<void(bool)> permission_result_callback = base::BindOnce(
nicka0ac8382016-12-15 23:59:235529 &WebContentsImpl::SendPpapiBrokerPermissionResult, base::Unretained(this),
5530 source->GetProcess()->GetID(), ppb_broker_route_id);
[email protected]8c8fc292012-11-23 18:57:165531 if (!delegate_) {
Tommy Nyquist4b749d02018-03-20 21:46:295532 std::move(permission_result_callback).Run(false);
[email protected]8c8fc292012-11-23 18:57:165533 return;
[email protected]d8415ad92012-08-23 14:40:505534 }
5535
Avi Drissman0a2a967d2019-04-01 19:33:525536 delegate_->RequestPpapiBrokerPermission(
5537 this, url, plugin_path, std::move(permission_result_callback));
[email protected]d8415ad92012-08-23 14:40:505538}
5539
nicka0ac8382016-12-15 23:59:235540void WebContentsImpl::SendPpapiBrokerPermissionResult(int process_id,
5541 int ppb_broker_route_id,
5542 bool result) {
5543 RenderProcessHost* rph = RenderProcessHost::FromID(process_id);
5544 if (rph) {
5545 // TODO(nick): Convert this from ViewMsg_ to a Ppapi msg, since it
5546 // is not routed to a RenderView.
5547 rph->Send(
5548 new ViewMsg_PpapiBrokerPermissionResult(ppb_broker_route_id, result));
5549 }
[email protected]d8415ad92012-08-23 14:40:505550}
brettw4b461082016-11-19 18:55:165551#endif // BUILDFLAG(ENABLE_PLUGINS)
[email protected]19be7a62012-10-01 23:03:375552
Miyoung Shin5709ab02020-02-19 06:50:415553void WebContentsImpl::UpdateFaviconURL(
5554 RenderFrameHost* source,
5555 std::vector<blink::mojom::FaviconURLPtr> candidates) {
lukasza178ff092017-06-07 23:39:505556 // Ignore favicons for non-main frame.
5557 if (source->GetParent()) {
5558 NOTREACHED();
5559 return;
5560 }
5561
pkotwiczf9bc20592015-04-21 18:57:065562 // We get updated favicon URLs after the page stops loading. If a cross-site
5563 // navigation occurs while a page is still loading, the initial page
5564 // may stop loading and send us updated favicon URLs after the navigation
5565 // for the new page has committed.
lukasza178ff092017-06-07 23:39:505566 if (!source->IsCurrent())
pkotwiczf9bc20592015-04-21 18:57:065567 return;
5568
Miyoung Shin5709ab02020-02-19 06:50:415569 favicon_urls_ = std::move(candidates);
Mounir Lamouri7b4a1f232019-12-23 16:32:105570
ericwilligersde386342016-10-19 02:35:095571 for (auto& observer : observers_)
Yuzu Saijo0c263c702020-06-08 03:41:245572 observer.DidUpdateFaviconURL(source, favicon_urls_);
[email protected]795c28972012-12-06 06:13:395573}
5574
qinmin72e8bd02016-10-21 19:35:375575void WebContentsImpl::SetIsOverlayContent(bool is_overlay_content) {
5576 is_overlay_content_ = is_overlay_content;
5577}
5578
Albert J. Wongfb64e142018-10-16 01:10:455579void WebContentsImpl::DidFirstVisuallyNonEmptyPaint(
5580 RenderViewHostImpl* source) {
nicka0ac8382016-12-15 23:59:235581 // TODO(nick): When this is ported to FrameHostMsg_, we should only listen if
5582 // |source| is the main frame.
ericwilligersde386342016-10-19 02:35:095583 for (auto& observer : observers_)
5584 observer.DidFirstVisuallyNonEmptyPaint();
yusufod41c5f92015-03-06 00:14:285585
Carlos Caballero1215f882019-10-29 15:58:435586 if (source->theme_color() != last_sent_theme_color_) {
yusufod41c5f92015-03-06 00:14:285587 // Theme color should have updated by now if there was one.
ericwilligersde386342016-10-19 02:35:095588 for (auto& observer : observers_)
Peter Kastingb14e5782020-01-06 23:55:045589 observer.DidChangeThemeColor();
Carlos Caballero1215f882019-10-29 15:58:435590 last_sent_theme_color_ = source->theme_color();
yusufod41c5f92015-03-06 00:14:285591 }
[email protected]9f268072013-11-07 00:02:155592}
[email protected]d9030b82013-07-19 08:26:065593
Kevin McNee068c3eb2020-04-03 18:24:195594bool WebContentsImpl::IsPortal() {
Adithya Srinivasan46b8a792019-02-01 14:47:235595 return portal();
5596}
5597
Kevin McNee94ea52f52020-06-23 17:42:065598WebContentsImpl* WebContentsImpl::GetPortalHostWebContents() {
5599 return portal() ? portal()->GetPortalHostContents() : nullptr;
5600}
5601
[email protected]17e286e2013-03-01 23:29:395602void WebContentsImpl::NotifyBeforeFormRepostWarningShow() {
ericwilligersde386342016-10-19 02:35:095603 for (auto& observer : observers_)
5604 observer.BeforeFormRepostWarningShow();
[email protected]17e286e2013-03-01 23:29:395605}
5606
[email protected]ec6c05f2013-10-23 18:41:575607void WebContentsImpl::ActivateAndShowRepostFormWarningDialog() {
5608 Activate();
5609 if (delegate_)
5610 delegate_->ShowRepostFormWarningDialog(this);
5611}
5612
[email protected]b4c84012014-04-28 19:51:105613bool WebContentsImpl::HasAccessedInitialDocument() {
5614 return has_accessed_initial_document_;
5615}
5616
afakhry6f0c1ec22016-07-14 13:55:135617void WebContentsImpl::UpdateTitleForEntry(NavigationEntry* entry,
[email protected]fcf75d42013-12-03 20:11:265618 const base::string16& title) {
[email protected]fcf75d42013-12-03 20:11:265619 base::string16 final_title;
Avi Drissman93002212017-09-27 03:20:525620 base::TrimWhitespace(title, base::TRIM_ALL, &final_title);
[email protected]420ae012009-04-24 05:16:325621
[email protected]987fc3a2011-05-26 14:18:095622 // If a page is created via window.open and never navigated,
5623 // there will be no navigation entry. In this situation,
[email protected]73eb2602012-02-09 19:50:555624 // |page_title_when_no_navigation_entry_| will be used for page title.
[email protected]987fc3a2011-05-26 14:18:095625 if (entry) {
[email protected]36fc0392011-12-25 03:59:515626 if (final_title == entry->GetTitle())
afakhry6f0c1ec22016-07-14 13:55:135627 return; // Nothing changed, don't bother.
[email protected]420ae012009-04-24 05:16:325628
[email protected]36fc0392011-12-25 03:59:515629 entry->SetTitle(final_title);
Avi Drissman93002212017-09-27 03:20:525630
5631 // The title for display may differ from the title just set; grab it.
5632 final_title = entry->GetTitleForDisplay();
[email protected]987fc3a2011-05-26 14:18:095633 } else {
5634 if (page_title_when_no_navigation_entry_ == final_title)
afakhry6f0c1ec22016-07-14 13:55:135635 return; // Nothing changed, don't bother.
[email protected]987fc3a2011-05-26 14:18:095636
5637 page_title_when_no_navigation_entry_ = final_title;
5638 }
[email protected]420ae012009-04-24 05:16:325639
[email protected]420ae012009-04-24 05:16:325640 // Lastly, set the title for the view.
[email protected]790e2fd2011-09-21 20:28:235641 view_->SetPageTitle(final_title);
[email protected]420ae012009-04-24 05:16:325642
ericwilligersde386342016-10-19 02:35:095643 for (auto& observer : observers_)
Avi Drissman93002212017-09-27 03:20:525644 observer.TitleWasSet(entry);
[email protected]66798902013-10-01 18:40:165645
afakhry6f0c1ec22016-07-14 13:55:135646 // Broadcast notifications when the UI should be updated.
5647 if (entry == controller_.GetEntryAtOffset(0))
5648 NotifyNavigationStateChanged(INVALIDATE_TYPE_TITLE);
[email protected]420ae012009-04-24 05:16:325649}
5650
fdegansa696e5112015-04-17 01:57:595651void WebContentsImpl::SendChangeLoadProgress() {
[email protected]960b0372014-05-19 18:01:005652 loading_last_progress_update_ = base::TimeTicks::Now();
Evan Stade9f328112019-10-23 20:49:255653 for (auto& observer : observers_)
5654 observer.LoadProgressChanged(frame_tree_.load_progress());
[email protected]960b0372014-05-19 18:01:005655}
5656
5657void WebContentsImpl::ResetLoadProgressState() {
fdegans1d16355162015-03-26 11:58:345658 frame_tree_.ResetLoadProgress();
[email protected]960b0372014-05-19 18:01:005659 loading_weak_factory_.InvalidateWeakPtrs();
5660 loading_last_progress_update_ = base::TimeTicks();
5661}
5662
clamy44e84ce2016-02-22 15:38:255663// Notifies the RenderWidgetHost instance about the fact that the page is
5664// loading, or done loading.
5665void WebContentsImpl::LoadingStateChanged(bool to_different_document,
clamy44e84ce2016-02-22 15:38:255666 LoadNotificationDetails* details) {
Nate Chapin22ea6592019-03-05 22:29:025667 if (is_being_destroyed_)
5668 return;
clamy44e84ce2016-02-22 15:38:255669
5670 bool is_loading = IsLoading();
5671
5672 if (!is_loading) {
5673 load_state_ = net::LoadStateWithParam(net::LOAD_STATE_IDLE,
5674 base::string16());
5675 load_state_host_.clear();
5676 upload_size_ = 0;
5677 upload_position_ = 0;
5678 }
5679
5680 GetRenderManager()->SetIsLoading(is_loading);
5681
5682 waiting_for_response_ = is_loading;
5683 is_load_to_different_document_ = to_different_document;
5684
5685 if (delegate_)
5686 delegate_->LoadingStateChanged(this, to_different_document);
5687 NotifyNavigationStateChanged(INVALIDATE_TYPE_LOAD);
5688
5689 std::string url = (details ? details->url.possibly_invalid_spec() : "NULL");
5690 if (is_loading) {
5691 TRACE_EVENT_ASYNC_BEGIN2("browser,navigation", "WebContentsImpl Loading",
5692 this, "URL", url, "Main FrameTreeNode id",
5693 GetFrameTree()->root()->frame_tree_node_id());
ericwilligersde386342016-10-19 02:35:095694 for (auto& observer : observers_)
5695 observer.DidStartLoading();
clamy44e84ce2016-02-22 15:38:255696 } else {
5697 TRACE_EVENT_ASYNC_END1("browser,navigation", "WebContentsImpl Loading",
5698 this, "URL", url);
ericwilligersde386342016-10-19 02:35:095699 for (auto& observer : observers_)
5700 observer.DidStopLoading();
clamy44e84ce2016-02-22 15:38:255701 }
5702
5703 // TODO(avi): Remove. http://crbug.com/170921
5704 int type = is_loading ? NOTIFICATION_LOAD_START : NOTIFICATION_LOAD_STOP;
5705 NotificationDetails det = NotificationService::NoDetails();
5706 if (details)
5707 det = Details<LoadNotificationDetails>(details);
5708 NotificationService::current()->Notify(
5709 type, Source<NavigationController>(&controller_), det);
5710}
5711
danakja0bce4c952020-05-12 20:40:285712void WebContentsImpl::NotifyViewSwapped(RenderViewHost* old_view,
5713 RenderViewHost* new_view) {
5714 DCHECK_NE(old_view, new_view);
[email protected]420ae012009-04-24 05:16:325715 // After sending out a swap notification, we need to send a disconnect
5716 // notification so that clients that pick up a pointer to |this| can NULL the
5717 // pointer. See Bug 1230284.
5718 notify_disconnection_ = true;
ericwilligersde386342016-10-19 02:35:095719 for (auto& observer : observers_)
danakja0bce4c952020-05-12 20:40:285720 observer.RenderViewHostChanged(old_view, new_view);
[email protected]da7a7182013-09-06 08:11:115721
danakja0bce4c952020-05-12 20:40:285722 view_->RenderViewHostChanged(old_view, new_view);
Jinsuk Kimdc5284c22018-07-11 02:22:065723
Lucas Furukawa Gadanif9ea4fc2018-08-03 23:57:135724 // If this is an inner WebContents that has swapped views, we need to reattach
5725 // it to its outer WebContents.
5726 if (node_.outer_web_contents())
5727 ReattachToOuterWebContentsFrame();
5728
[email protected]7a846df2012-09-20 19:17:395729 // Ensure that the associated embedder gets cleared after a RenderViewHost
5730 // gets swapped, so we don't reuse the same embedder next time a
5731 // RenderViewHost is attached to this WebContents.
5732 RemoveBrowserPluginEmbedder();
[email protected]420ae012009-04-24 05:16:325733}
5734
danakja0bce4c952020-05-12 20:40:285735void WebContentsImpl::NotifyFrameSwapped(RenderFrameHost* old_frame,
5736 RenderFrameHost* new_frame,
Jinsuk Kim895b2c52018-07-03 06:49:165737 bool is_main_frame) {
Bo Liu168c8642017-08-28 18:26:025738#if defined(OS_ANDROID)
danakja0bce4c952020-05-12 20:40:285739 // Copy importance from |old_frame| if |new_frame| is a main frame.
5740 if (old_frame && !new_frame->GetParent()) {
5741 RenderWidgetHostImpl* old_widget =
5742 static_cast<RenderFrameHostImpl*>(old_frame)->GetRenderWidgetHost();
5743 RenderWidgetHostImpl* new_widget =
5744 static_cast<RenderFrameHostImpl*>(new_frame)->GetRenderWidgetHost();
5745 new_widget->SetImportance(old_widget->importance());
Bo Liu7c6779e92017-08-16 02:02:285746 }
Bo Liu168c8642017-08-28 18:26:025747#endif
ericwilligersde386342016-10-19 02:35:095748 for (auto& observer : observers_)
danakja0bce4c952020-05-12 20:40:285749 observer.RenderFrameHostChanged(old_frame, new_frame);
[email protected]02d7b6e2014-06-24 21:01:505750}
5751
[email protected]da7a7182013-09-06 08:11:115752// TODO(avi): Remove this entire function because this notification is already
5753// covered by two observer functions. http://crbug.com/170921
[email protected]b172aee2012-04-10 17:05:265754void WebContentsImpl::NotifyDisconnected() {
[email protected]420ae012009-04-24 05:16:325755 if (!notify_disconnection_)
5756 return;
5757
5758 notify_disconnection_ = false;
[email protected]8ff00d72012-10-23 19:12:215759 NotificationService::current()->Notify(
5760 NOTIFICATION_WEB_CONTENTS_DISCONNECTED,
5761 Source<WebContents>(this),
5762 NotificationService::NoDetails());
[email protected]420ae012009-04-24 05:16:325763}
5764
[email protected]cbb1ef592013-06-05 19:49:465765void WebContentsImpl::NotifyNavigationEntryCommitted(
5766 const LoadCommittedDetails& load_details) {
ericwilligersde386342016-10-19 02:35:095767 for (auto& observer : observers_)
5768 observer.NavigationEntryCommitted(load_details);
[email protected]cbb1ef592013-06-05 19:49:465769}
5770
Sam McNally5c087a32017-08-25 01:46:145771void WebContentsImpl::NotifyNavigationEntryChanged(
5772 const EntryChangedDetails& change_details) {
5773 for (auto& observer : observers_)
5774 observer.NavigationEntryChanged(change_details);
5775}
5776
5777void WebContentsImpl::NotifyNavigationListPruned(
5778 const PrunedDetails& pruned_details) {
5779 for (auto& observer : observers_)
5780 observer.NavigationListPruned(pruned_details);
5781}
5782
Christian Dullweber1af31e62018-02-22 11:49:485783void WebContentsImpl::NotifyNavigationEntriesDeleted() {
5784 for (auto& observer : observers_)
5785 observer.NavigationEntriesDeleted();
5786}
5787
rockotf62002a2016-09-15 00:08:595788void WebContentsImpl::OnAssociatedInterfaceRequest(
5789 RenderFrameHost* render_frame_host,
5790 const std::string& interface_name,
5791 mojo::ScopedInterfaceEndpointHandle handle) {
Julie Jeongeun Kim7d0986172019-12-04 03:17:255792 auto it = receiver_sets_.find(interface_name);
5793 if (it != receiver_sets_.end())
5794 it->second->OnReceiverForFrame(render_frame_host, std::move(handle));
rockotf62002a2016-09-15 00:08:595795}
5796
Ben Goodger9e24929e2017-07-21 03:13:405797void WebContentsImpl::OnInterfaceRequest(
5798 RenderFrameHost* render_frame_host,
5799 const std::string& interface_name,
5800 mojo::ScopedMessagePipeHandle* interface_pipe) {
5801 for (auto& observer : observers_) {
5802 observer.OnInterfaceRequestFromFrame(render_frame_host, interface_name,
5803 interface_pipe);
5804 if (!interface_pipe->is_valid())
5805 break;
5806 }
5807}
5808
Varun Khaneja5da94302019-06-25 04:48:105809void WebContentsImpl::OnDidBlockNavigation(
5810 const GURL& blocked_url,
5811 const GURL& initiator_url,
Abhijeet Kandalkarde7348e2020-01-13 06:06:545812 blink::mojom::NavigationBlockedReason reason) {
Nicolas Dossou-gbetebcab2472017-09-09 12:28:405813 if (delegate_)
Varun Khaneja5da94302019-06-25 04:48:105814 delegate_->OnDidBlockNavigation(this, blocked_url, initiator_url, reason);
Nicolas Dossou-gbetebcab2472017-09-09 12:28:405815}
5816
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:415817const GURL& WebContentsImpl::GetMainFrameLastCommittedURL() {
[email protected]c31a84802014-04-03 15:55:495818 return GetLastCommittedURL();
5819}
5820
[email protected]b849847b2013-12-10 21:57:585821void WebContentsImpl::RenderFrameCreated(RenderFrameHost* render_frame_host) {
ericwilligersde386342016-10-19 02:35:095822 for (auto& observer : observers_)
5823 observer.RenderFrameCreated(render_frame_host);
dmazzonif8a31042016-12-03 00:52:135824 UpdateAccessibilityModeOnFrame(render_frame_host);
clamy91b7abf2016-03-24 15:17:455825
Becca Hughesd11d6502018-07-31 17:01:285826 if (display_cutout_host_impl_)
5827 display_cutout_host_impl_->RenderFrameCreated(render_frame_host);
5828
clamy91b7abf2016-03-24 15:17:455829 if (!render_frame_host->IsRenderFrameLive() || render_frame_host->GetParent())
5830 return;
5831
5832 NavigationEntry* entry = controller_.GetPendingEntry();
5833 if (entry && entry->IsViewSourceMode()) {
5834 // Put the renderer in view source mode.
Dave Tapuska3c05e1c2019-11-27 18:34:185835 static_cast<RenderFrameHostImpl*>(render_frame_host)
5836 ->GetAssociatedLocalFrame()
5837 ->EnableViewSourceMode();
clamy91b7abf2016-03-24 15:17:455838 }
[email protected]b849847b2013-12-10 21:57:585839}
5840
5841void WebContentsImpl::RenderFrameDeleted(RenderFrameHost* render_frame_host) {
yilkal172da772019-11-06 22:49:075842 if (IsBeingDestroyed() && !render_frame_host->GetParent() &&
5843 first_navigation_completed_ &&
Hajime Hoshibbc509c2020-04-06 08:55:085844 !render_frame_host->IsInBackForwardCache()) {
yilkal796e89e2019-10-02 23:58:285845 // Main frame has been deleted because WebContents is being destroyed.
Rakina Zata Amni74627df2019-10-15 02:03:175846 // Note that we aren't recording this here when the main frame is in the
5847 // back-forward cache because that means we've actually already navigated
5848 // away from it (and we got to this point because the WebContents is
yilkal172da772019-11-06 22:49:075849 // deleted), which means |max_loaded_frame_count_| is already overwritten.
5850 // The |max_loaded_frame_count_| value will instead be recorded from within
5851 // |WebContentsImpl::DidFinishNavigation()|.
5852 RecordMaxFrameCountUMA(max_loaded_frame_count_);
yilkal796e89e2019-10-02 23:58:285853 }
5854
creis3638897452017-05-09 22:07:505855 is_notifying_observers_ = true;
ericwilligersde386342016-10-19 02:35:095856 for (auto& observer : observers_)
5857 observer.RenderFrameDeleted(render_frame_host);
creis3638897452017-05-09 22:07:505858 is_notifying_observers_ = false;
zqzhang7b6f4f02016-12-07 22:10:225859#if BUILDFLAG(ENABLE_PLUGINS)
5860 pepper_playback_observer_->RenderFrameDeleted(render_frame_host);
5861#endif
Becca Hughesfd5d8f892018-06-14 18:23:365862
Becca Hughesd11d6502018-07-31 17:01:285863 if (display_cutout_host_impl_)
5864 display_cutout_host_impl_->RenderFrameDeleted(render_frame_host);
5865
Becca Hughesfd5d8f892018-06-14 18:23:365866 // Remove any fullscreen state that the frame has stored.
5867 FullscreenStateChanged(render_frame_host, false /* is_fullscreen */);
[email protected]b849847b2013-12-10 21:57:585868}
5869
[email protected]a09d53ce2014-01-31 00:46:425870void WebContentsImpl::ShowContextMenu(RenderFrameHost* render_frame_host,
5871 const ContextMenuParams& params) {
avie9fd8722017-04-10 16:17:585872 // If a renderer fires off a second command to show a context menu before the
5873 // first context menu is closed, just ignore it. https://crbug.com/707534
ekaramadf6750aa2017-06-06 18:29:425874 if (showing_context_menu_)
avie9fd8722017-04-10 16:17:585875 return;
5876
[email protected]077e704b2014-05-23 19:24:105877 ContextMenuParams context_menu_params(params);
[email protected]a09d53ce2014-01-31 00:46:425878 // Allow WebContentsDelegates to handle the context menu operation first.
Joel Klinghed0ea22ea2019-02-20 22:04:225879 if (delegate_ &&
5880 delegate_->HandleContextMenu(render_frame_host, context_menu_params))
[email protected]a09d53ce2014-01-31 00:46:425881 return;
5882
[email protected]077e704b2014-05-23 19:24:105883 render_view_host_delegate_view_->ShowContextMenu(render_frame_host,
5884 context_menu_params);
[email protected]a09d53ce2014-01-31 00:46:425885}
5886
Avi Drissman4891b852018-09-25 18:12:585887namespace {
5888// Normalizes the line endings: \r\n -> \n, lone \r -> \n.
5889base::string16 NormalizeLineBreaks(const base::string16& source) {
5890 static const base::NoDestructor<base::string16> kReturnNewline(
5891 base::ASCIIToUTF16("\r\n"));
5892 static const base::NoDestructor<base::string16> kReturn(
5893 base::ASCIIToUTF16("\r"));
5894 static const base::NoDestructor<base::string16> kNewline(
5895 base::ASCIIToUTF16("\n"));
5896
5897 std::vector<base::StringPiece16> pieces;
5898
5899 for (const auto& rn_line : base::SplitStringPieceUsingSubstr(
5900 source, *kReturnNewline, base::KEEP_WHITESPACE,
5901 base::SPLIT_WANT_ALL)) {
5902 auto r_lines = base::SplitStringPieceUsingSubstr(
5903 rn_line, *kReturn, base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL);
5904 std::move(std::begin(r_lines), std::end(r_lines),
5905 std::back_inserter(pieces));
5906 }
5907
5908 return base::JoinString(pieces, *kNewline);
5909}
5910} // namespace
5911
Dave Tapuskacdf545cc2020-01-23 18:38:525912void WebContentsImpl::RunJavaScriptDialog(
5913 RenderFrameHost* render_frame_host,
5914 const base::string16& message,
5915 const base::string16& default_prompt,
5916 JavaScriptDialogType dialog_type,
5917 JavaScriptDialogCallback response_callback) {
Avi Drissman2ccbb402018-03-20 21:11:365918 // Ensure that if showing a dialog is the first thing that a page does, that
5919 // the contents of the previous page aren't shown behind it. This is required
5920 // because showing a dialog freezes the renderer, so no frames will be coming
5921 // from it. https://crbug.com/823353
5922 auto* render_widget_host_impl =
5923 static_cast<RenderFrameHostImpl*>(render_frame_host)
5924 ->GetRenderWidgetHost();
5925 if (render_widget_host_impl)
5926 render_widget_host_impl->ForceFirstFrameAfterNavigationTimeout();
5927
avi0720b02e2017-06-13 03:22:135928 // Running a dialog causes an exit to webpage-initiated fullscreen.
5929 // http://crbug.com/728276
Avi Drissman1a55a9d62020-03-10 18:56:455930 base::ScopedClosureRunner fullscreen_block = ForSecurityDropFullscreen();
avi0720b02e2017-06-13 03:22:135931
Avi Drissman1a55a9d62020-03-10 18:56:455932 auto callback =
5933 base::BindOnce(&WebContentsImpl::OnDialogClosed, base::Unretained(this),
5934 render_frame_host->GetProcess()->GetID(),
5935 render_frame_host->GetRoutingID(),
5936 std::move(response_callback), std::move(fullscreen_block));
Pavel Feldman3c1842b2017-08-02 05:00:165937
Pavel Feldman3c1842b2017-08-02 05:00:165938 std::vector<protocol::PageHandler*> page_handlers =
5939 protocol::PageHandler::EnabledForWebContents(this);
5940
Pavel Feldmanfab3cb62017-10-05 01:02:015941 if (delegate_)
5942 dialog_manager_ = delegate_->GetJavaScriptDialogManager(this);
5943
Charlie Reis96077362019-07-04 00:34:045944 // While a JS message dialog is showing, defer commits in this WebContents.
5945 javascript_dialog_navigation_deferrer_ =
5946 std::make_unique<JavaScriptDialogNavigationDeferrer>();
5947
Carlos IL07f282f2020-05-15 22:58:295948 // Suppress JavaScript dialogs when requested.
5949 bool should_suppress = delegate_ && delegate_->ShouldSuppressDialogs(this);
Pavel Feldman1c587bc52018-04-04 21:16:055950 bool has_non_devtools_handlers = delegate_ && dialog_manager_;
5951 bool has_handlers = page_handlers.size() || has_non_devtools_handlers;
Pavel Feldmanfab3cb62017-10-05 01:02:015952 bool suppress_this_message = should_suppress || !has_handlers;
5953
5954 if (suppress_this_message) {
Avi Drissmane04d3992017-10-05 15:11:365955 std::move(callback).Run(true, false, base::string16());
Pavel Feldman3c1842b2017-08-02 05:00:165956 return;
5957 }
5958
5959 scoped_refptr<CloseDialogCallbackWrapper> wrapper =
Avi Drissmane04d3992017-10-05 15:11:365960 new CloseDialogCallbackWrapper(std::move(callback));
Pavel Feldman3c1842b2017-08-02 05:00:165961
5962 is_showing_javascript_dialog_ = true;
5963
Avi Drissman4891b852018-09-25 18:12:585964 base::string16 normalized_message = NormalizeLineBreaks(message);
5965
Pavel Feldman3c1842b2017-08-02 05:00:165966 for (auto* handler : page_handlers) {
Avi Drissmane04d3992017-10-05 15:11:365967 handler->DidRunJavaScriptDialog(
Avi Drissman4891b852018-09-25 18:12:585968 render_frame_host->GetLastCommittedURL(), normalized_message,
5969 default_prompt, dialog_type, has_non_devtools_handlers,
Avi Drissmane04d3992017-10-05 15:11:365970 base::BindOnce(&CloseDialogCallbackWrapper::Run, wrapper, false));
Pavel Feldman3c1842b2017-08-02 05:00:165971 }
5972
5973 if (dialog_manager_) {
[email protected]87de04b02014-04-08 22:14:495974 dialog_manager_->RunJavaScriptDialog(
Avi Drissman4891b852018-09-25 18:12:585975 this, render_frame_host, dialog_type, normalized_message,
5976 default_prompt,
Avi Drissmane04d3992017-10-05 15:11:365977 base::BindOnce(&CloseDialogCallbackWrapper::Run, wrapper, false),
5978 &suppress_this_message);
[email protected]87de04b02014-04-08 22:14:495979 }
5980
5981 if (suppress_this_message) {
5982 // If we are suppressing messages, just reply as if the user immediately
5983 // pressed "Cancel", passing true to |dialog_was_suppressed|.
Avi Drissmane04d3992017-10-05 15:11:365984 wrapper->Run(true, false, base::string16());
[email protected]87de04b02014-04-08 22:14:495985 }
[email protected]87de04b02014-04-08 22:14:495986}
5987
5988void WebContentsImpl::RunBeforeUnloadConfirm(
[email protected]39f2f9252014-05-03 00:54:015989 RenderFrameHost* render_frame_host,
[email protected]87de04b02014-04-08 22:14:495990 bool is_reload,
Dave Tapuskacdf545cc2020-01-23 18:38:525991 JavaScriptDialogCallback response_callback) {
Avi Drissman2ccbb402018-03-20 21:11:365992 // Ensure that if showing a dialog is the first thing that a page does, that
5993 // the contents of the previous page aren't shown behind it. This is required
5994 // because showing a dialog freezes the renderer, so no frames will be coming
5995 // from it. https://crbug.com/823353
5996 auto* render_widget_host_impl =
5997 static_cast<RenderFrameHostImpl*>(render_frame_host)
5998 ->GetRenderWidgetHost();
5999 if (render_widget_host_impl)
6000 render_widget_host_impl->ForceFirstFrameAfterNavigationTimeout();
6001
avi0720b02e2017-06-13 03:22:136002 // Running a dialog causes an exit to webpage-initiated fullscreen.
6003 // http://crbug.com/728276
Avi Drissman1a55a9d62020-03-10 18:56:456004 base::ScopedClosureRunner fullscreen_block = ForSecurityDropFullscreen();
avi0720b02e2017-06-13 03:22:136005
[email protected]39f2f9252014-05-03 00:54:016006 RenderFrameHostImpl* rfhi =
6007 static_cast<RenderFrameHostImpl*>(render_frame_host);
[email protected]87de04b02014-04-08 22:14:496008 if (delegate_)
6009 delegate_->WillRunBeforeUnloadConfirm();
6010
Avi Drissman1a55a9d62020-03-10 18:56:456011 auto callback =
6012 base::BindOnce(&WebContentsImpl::OnDialogClosed, base::Unretained(this),
6013 render_frame_host->GetProcess()->GetID(),
6014 render_frame_host->GetRoutingID(),
6015 std::move(response_callback), std::move(fullscreen_block));
Pavel Feldman3c1842b2017-08-02 05:00:166016
Pavel Feldman3c1842b2017-08-02 05:00:166017 std::vector<protocol::PageHandler*> page_handlers =
6018 protocol::PageHandler::EnabledForWebContents(this);
6019
Pavel Feldmanfab3cb62017-10-05 01:02:016020 if (delegate_)
6021 dialog_manager_ = delegate_->GetJavaScriptDialogManager(this);
6022
Charlie Reis96077362019-07-04 00:34:046023 // While a JS beforeunload dialog is showing, defer commits in this
6024 // WebContents.
6025 javascript_dialog_navigation_deferrer_ =
6026 std::make_unique<JavaScriptDialogNavigationDeferrer>();
6027
Sreeja Kamishettyf352e302020-06-10 13:21:246028 bool should_suppress = !rfhi->IsCurrent() ||
Tarun Bansalc209b742019-12-24 06:53:256029 (delegate_ && delegate_->ShouldSuppressDialogs(this));
Pavel Feldman1c587bc52018-04-04 21:16:056030 bool has_non_devtools_handlers = delegate_ && dialog_manager_;
6031 bool has_handlers = page_handlers.size() || has_non_devtools_handlers;
Pavel Feldmanfab3cb62017-10-05 01:02:016032 if (should_suppress || !has_handlers) {
Avi Drissmane04d3992017-10-05 15:11:366033 std::move(callback).Run(false, true, base::string16());
[email protected]87de04b02014-04-08 22:14:496034 return;
6035 }
6036
6037 is_showing_before_unload_dialog_ = true;
Pavel Feldman3c1842b2017-08-02 05:00:166038
6039 scoped_refptr<CloseDialogCallbackWrapper> wrapper =
Avi Drissmane04d3992017-10-05 15:11:366040 new CloseDialogCallbackWrapper(std::move(callback));
Pavel Feldman3c1842b2017-08-02 05:00:166041
6042 GURL frame_url = rfhi->GetLastCommittedURL();
6043 for (auto* handler : page_handlers) {
Avi Drissmane04d3992017-10-05 15:11:366044 handler->DidRunBeforeUnloadConfirm(
Pavel Feldman1c587bc52018-04-04 21:16:056045 frame_url, has_non_devtools_handlers,
Avi Drissmane04d3992017-10-05 15:11:366046 base::BindOnce(&CloseDialogCallbackWrapper::Run, wrapper, false));
Pavel Feldman3c1842b2017-08-02 05:00:166047 }
6048
6049 if (dialog_manager_) {
Avi Drissmane04d3992017-10-05 15:11:366050 dialog_manager_->RunBeforeUnloadDialog(
Avi Drissmanff9ed752017-11-28 21:18:126051 this, render_frame_host, is_reload,
Avi Drissmane04d3992017-10-05 15:11:366052 base::BindOnce(&CloseDialogCallbackWrapper::Run, wrapper, false));
Pavel Feldman3c1842b2017-08-02 05:00:166053 }
[email protected]87de04b02014-04-08 22:14:496054}
6055
Kent Tamurac260c9a2018-09-14 09:10:566056void WebContentsImpl::RunFileChooser(
6057 RenderFrameHost* render_frame_host,
Kent Tamura3abb32d2020-07-02 00:23:016058 scoped_refptr<FileChooserImpl::FileSelectListenerImpl> listener,
Kent Tamurac260c9a2018-09-14 09:10:566059 const blink::mojom::FileChooserParams& params) {
Avi Drissman3d41e7712018-08-27 21:18:086060 // Any explicit focusing of another window while this WebContents is in
6061 // fullscreen can be used to confuse the user, so drop fullscreen.
Avi Drissman1a55a9d62020-03-10 18:56:456062 base::ScopedClosureRunner fullscreen_block = ForSecurityDropFullscreen();
6063 listener->SetFullscreenBlock(std::move(fullscreen_block));
Avi Drissman3d41e7712018-08-27 21:18:086064
naskoada75b22016-06-11 16:09:466065 if (delegate_)
Kent Tamura512a27e2018-10-04 00:49:326066 delegate_->RunFileChooser(render_frame_host, std::move(listener), params);
6067 else
6068 listener->FileSelectionCanceled();
naskoada75b22016-06-11 16:09:466069}
6070
[email protected]a86c0e962013-12-17 17:10:396071WebContents* WebContentsImpl::GetAsWebContents() {
6072 return this;
6073}
6074
mcnee336ea2c2017-05-23 22:50:596075#if !defined(OS_ANDROID)
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:416076double WebContentsImpl::GetPendingPageZoomLevel() {
wjmaclean64951902016-04-29 20:59:126077 NavigationEntry* pending_entry = GetController().GetPendingEntry();
6078 if (!pending_entry)
6079 return HostZoomMap::GetZoomLevel(this);
6080
6081 GURL url = pending_entry->GetURL();
6082 return HostZoomMap::GetForWebContents(this)->GetZoomLevelForHostAndScheme(
6083 url.scheme(), net::GetHostOrSpecFromURL(url));
6084}
mcnee336ea2c2017-05-23 22:50:596085#endif // !defined(OS_ANDROID)
wjmaclean64951902016-04-29 20:59:126086
qinmin72e8bd02016-10-21 19:35:376087bool WebContentsImpl::HideDownloadUI() const {
6088 return is_overlay_content_;
6089}
6090
mlamouri5cd9ae82017-02-18 11:05:096091bool WebContentsImpl::HasPersistentVideo() const {
6092 return has_persistent_video_;
6093}
6094
Ian Vollick5d5c37f2019-06-19 22:50:576095bool WebContentsImpl::IsSpatialNavigationDisabled() const {
6096 return is_spatial_navigation_disabled_;
6097}
6098
Albert J. Wong65fe64d2019-09-20 02:48:146099RenderFrameHostImpl* WebContentsImpl::GetPendingMainFrame() {
clamy610c63b32017-12-22 15:05:186100 return GetRenderManager()->speculative_frame_host();
Alex Moshchuk27caae82017-09-11 23:11:186101}
6102
mlamourie52c1b542017-02-23 14:57:506103bool WebContentsImpl::HasActiveEffectivelyFullscreenVideo() const {
6104 return media_web_contents_observer_->HasActiveEffectivelyFullscreenVideo();
6105}
6106
François Beaufortad6c5232018-02-26 11:00:446107bool WebContentsImpl::IsPictureInPictureAllowedForFullscreenVideo() const {
6108 return media_web_contents_observer_
6109 ->IsPictureInPictureAllowedForFullscreenVideo();
6110}
6111
ekaramada110f642016-12-21 19:47:286112bool WebContentsImpl::IsFocusedElementEditable() {
6113 RenderFrameHostImpl* frame = GetFocusedFrame();
6114 return frame && frame->has_focused_editable_element();
6115}
6116
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:416117bool WebContentsImpl::IsShowingContextMenu() {
ekaramadf6750aa2017-06-06 18:29:426118 return showing_context_menu_;
6119}
6120
6121void WebContentsImpl::SetShowingContextMenu(bool showing) {
6122 DCHECK_NE(showing_context_menu_, showing);
6123 showing_context_menu_ = showing;
6124
6125 if (auto* view = GetRenderWidgetHostView()) {
6126 // Notify the main frame's RWHV to run the platform-specific code, if any.
6127 static_cast<RenderWidgetHostViewBase*>(view)->SetShowingContextMenu(
6128 showing);
6129 }
6130}
6131
ekaramada110f642016-12-21 19:47:286132void WebContentsImpl::ClearFocusedElement() {
6133 if (auto* frame = GetFocusedFrame())
6134 frame->ClearFocusedElement();
6135}
6136
danakj77eb7e82020-01-09 19:38:466137bool WebContentsImpl::IsNeverComposited() {
[email protected]7912e822014-04-16 02:37:036138 if (!delegate_)
6139 return false;
danakj77eb7e82020-01-09 19:38:466140 return delegate_->IsNeverComposited(this);
[email protected]7912e822014-04-16 02:37:036141}
6142
[email protected]5a3bdf52012-05-24 15:12:576143RenderViewHostDelegateView* WebContentsImpl::GetDelegateView() {
6144 return render_view_host_delegate_view_;
[email protected]420ae012009-04-24 05:16:326145}
6146
Peter Marshall2dc74e92020-07-29 20:02:306147blink::mojom::RendererPreferences WebContentsImpl::GetRendererPrefs() const {
[email protected]840b1512009-07-21 16:53:466148 return renderer_preferences_;
[email protected]80d96fa2009-06-10 22:34:516149}
6150
Lucas Furukawa Gadani2ec00c82018-12-14 15:53:166151RenderFrameHostImpl* WebContentsImpl::GetOuterWebContentsFrame() {
6152 if (GetOuterDelegateFrameTreeNodeId() ==
6153 FrameTreeNode::kFrameTreeNodeInvalidId) {
6154 return nullptr;
6155 }
6156
6157 FrameTreeNode* outer_node =
6158 FrameTreeNode::GloballyFindByID(GetOuterDelegateFrameTreeNodeId());
6159 // The outer node should be in the outer WebContents.
6160 DCHECK_EQ(outer_node->frame_tree(), GetOuterWebContents()->GetFrameTree());
Alexander Timin381e7e182020-04-28 19:04:036161 return outer_node->parent();
Lucas Furukawa Gadani2ec00c82018-12-14 15:53:166162}
6163
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:416164WebContentsImpl* WebContentsImpl::GetOuterWebContents() {
Lucas Furukawa Gadani105de1e812018-05-31 19:38:396165 return node_.outer_web_contents();
avallee0206f782016-07-28 18:55:336166}
6167
Lucas Furukawa Gadani2ec00c82018-12-14 15:53:166168std::vector<WebContents*> WebContentsImpl::GetInnerWebContents() {
6169 std::vector<WebContents*> all_inner_contents;
Lucas Furukawa Gadani2ec00c82018-12-14 15:53:166170 const auto& inner_contents = node_.GetInnerWebContents();
6171 all_inner_contents.insert(all_inner_contents.end(), inner_contents.begin(),
6172 inner_contents.end());
6173 return all_inner_contents;
6174}
6175
Jeremy Roman7e70bf92020-01-07 23:23:586176WebContentsImpl* WebContentsImpl::GetResponsibleWebContents() {
6177 // Iteratively ask delegates which other contents is responsible until a fixed
6178 // point is found.
6179 WebContentsImpl* contents = this;
6180 while (WebContentsDelegate* delegate = contents->GetDelegate()) {
6181 auto* responsible_contents = static_cast<WebContentsImpl*>(
6182 delegate->GetResponsibleWebContents(contents));
6183 if (responsible_contents == contents)
6184 break;
6185 contents = responsible_contents;
6186 }
6187 return contents;
6188}
6189
avallee0206f782016-07-28 18:55:336190WebContentsImpl* WebContentsImpl::GetFocusedWebContents() {
lfg601233692017-03-06 22:45:446191 return GetOutermostWebContents()->node_.focused_web_contents();
avallee0206f782016-07-28 18:55:336192}
6193
avallee9993fca2016-11-17 06:16:506194bool WebContentsImpl::ContainsOrIsFocusedWebContents() {
6195 for (WebContentsImpl* focused_contents = GetFocusedWebContents();
6196 focused_contents;
6197 focused_contents = focused_contents->GetOuterWebContents()) {
6198 if (focused_contents == this)
6199 return true;
6200 }
6201
6202 return false;
6203}
6204
W. James MacLean9437b5bf2019-12-06 17:33:446205void WebContentsImpl::RemoveBrowserPluginEmbedder() {
6206 browser_plugin_embedder_.reset();
6207}
6208
avallee0206f782016-07-28 18:55:336209WebContentsImpl* WebContentsImpl::GetOutermostWebContents() {
6210 WebContentsImpl* root = this;
6211 while (root->GetOuterWebContents())
6212 root = root->GetOuterWebContents();
6213 return root;
6214}
6215
avalleed68cdd12017-06-21 22:07:276216void WebContentsImpl::FocusOuterAttachmentFrameChain() {
6217 WebContentsImpl* outer_contents = GetOuterWebContents();
6218 if (!outer_contents)
6219 return;
6220
6221 FrameTreeNode* outer_node =
6222 FrameTreeNode::GloballyFindByID(GetOuterDelegateFrameTreeNodeId());
6223 outer_contents->frame_tree_.SetFocusedFrame(outer_node, nullptr);
6224
6225 // For a browser initiated focus change, let embedding renderer know of the
6226 // change. Otherwise, if the currently focused element is just across a
6227 // process boundary in focus order, it will not be possible to move across
6228 // that boundary. This is because the target element will already be focused
6229 // (that renderer was not notified) and drop the event.
6230 if (GetRenderManager()->GetProxyToOuterDelegate())
6231 GetRenderManager()->GetProxyToOuterDelegate()->SetFocusedFrame();
6232
6233 outer_contents->FocusOuterAttachmentFrameChain();
6234}
6235
Sean Toppingbbc4a2bd2019-01-30 02:16:146236void WebContentsImpl::InnerWebContentsCreated(WebContents* inner_web_contents) {
6237 for (auto& observer : observers_)
6238 observer.InnerWebContentsCreated(inner_web_contents);
6239}
6240
Jeremy Roman03ce13ce2020-05-29 22:16:576241void WebContentsImpl::InnerWebContentsAttached(
6242 WebContents* inner_web_contents) {
6243 if (inner_web_contents->IsCurrentlyAudible())
6244 OnAudioStateChanged();
6245}
6246
6247void WebContentsImpl::InnerWebContentsDetached(
6248 WebContents* inner_web_contents) {
6249 if (!is_being_destroyed_)
6250 OnAudioStateChanged();
6251}
6252
[email protected]b172aee2012-04-10 17:05:266253void WebContentsImpl::RenderViewCreated(RenderViewHost* render_view_host) {
[email protected]86f98a22013-03-20 14:35:006254 if (delegate_)
[email protected]e85165c642014-06-10 14:34:316255 view_->SetOverscrollControllerEnabled(CanOverscrollContent());
[email protected]a6b73c62013-02-11 23:05:086256
[email protected]8ff00d72012-10-23 19:12:216257 NotificationService::current()->Notify(
6258 NOTIFICATION_WEB_CONTENTS_RENDER_VIEW_HOST_CREATED,
6259 Source<WebContents>(this),
6260 Details<RenderViewHost>(render_view_host));
[email protected]420ae012009-04-24 05:16:326261
ericwilligersde386342016-10-19 02:35:096262 for (auto& observer : observers_)
6263 observer.RenderViewCreated(render_view_host);
Joel Einbinder848001c2017-09-19 21:39:316264 if (delegate_)
6265 RenderFrameDevToolsAgentHost::WebContentsCreated(this);
[email protected]420ae012009-04-24 05:16:326266}
6267
[email protected]b172aee2012-04-10 17:05:266268void WebContentsImpl::RenderViewReady(RenderViewHost* rvh) {
[email protected]151a63d2011-12-20 22:32:526269 if (rvh != GetRenderViewHost()) {
[email protected]420ae012009-04-24 05:16:326270 // Don't notify the world, since this came from a renderer in the
6271 // background.
6272 return;
6273 }
6274
dmazzoni62111452017-04-28 23:46:556275 RenderWidgetHostViewBase* rwhv =
6276 static_cast<RenderWidgetHostViewBase*>(GetRenderWidgetHostView());
6277 if (rwhv)
6278 rwhv->SetMainFrameAXTreeID(GetMainFrame()->GetAXTreeID());
6279
[email protected]da7a7182013-09-06 08:11:116280 notify_disconnection_ = true;
[email protected]da7a7182013-09-06 08:11:116281
[email protected]be1f56ab2011-12-22 06:55:316282 bool was_crashed = IsCrashed();
[email protected]443b80e2010-12-14 00:42:236283 SetIsCrashed(base::TERMINATION_STATUS_STILL_RUNNING, 0);
[email protected]9d8a4642009-07-29 17:25:306284
6285 // Restore the focus to the tab (otherwise the focus will be on the top
6286 // window).
[email protected]484ae5912010-09-29 19:16:146287 if (was_crashed && !FocusLocationBarByDefault() &&
6288 (!delegate_ || delegate_->ShouldFocusPageAfterCrash())) {
[email protected]f3615f02013-02-26 06:09:066289 view_->Focus();
[email protected]484ae5912010-09-29 19:16:146290 }
[email protected]32ded2212011-11-10 18:51:436291
ericwilligersde386342016-10-19 02:35:096292 for (auto& observer : observers_)
6293 observer.RenderViewReady();
Jinsuk Kim895b2c52018-07-03 06:49:166294
6295 view_->RenderViewReady();
[email protected]420ae012009-04-24 05:16:326296}
6297
[email protected]ec6a7eb2013-04-22 17:34:226298void WebContentsImpl::RenderViewTerminated(RenderViewHost* rvh,
6299 base::TerminationStatus status,
6300 int error_code) {
[email protected]151a63d2011-12-20 22:32:526301 if (rvh != GetRenderViewHost()) {
[email protected]420ae012009-04-24 05:16:326302 // The pending page's RenderViewHost is gone.
6303 return;
6304 }
6305
[email protected]2fcdcc32014-03-05 02:14:076306 // Ensure fullscreen mode is exited in the |delegate_| since a crashed
6307 // renderer may not have made a clean exit.
Avi Drissman97aef042020-06-30 21:04:486308 if (IsFullscreen())
bokanece34a82016-01-28 19:49:466309 ExitFullscreenMode(false);
[email protected]2fcdcc32014-03-05 02:14:076310
Jennifer Apacibleb4b972a2018-06-07 04:41:026311 // Ensure any video in Picture-in-Picture is exited in the |delegate_| since
6312 // a crashed renderer may not have made a clean exit.
6313 if (HasPictureInPictureVideo())
6314 ExitPictureInPicture();
6315
[email protected]698191dc2014-02-25 01:06:136316 // Cancel any visible dialogs so they are not left dangling over the sad tab.
creis89a0f782015-05-27 16:13:176317 CancelActiveAndPendingDialogs();
[email protected]698191dc2014-02-25 01:06:136318
dalecurtis3d1e1302017-02-20 22:33:256319 audio_stream_monitor_.RenderProcessGone(rvh->GetProcess()->GetID());
6320
[email protected]960b0372014-05-19 18:01:006321 // Reset the loading progress. TODO(avi): What does it mean to have a
6322 // "renderer crash" when there is more than one renderer process serving a
6323 // webpage? Once this function is called at a more granular frame level, we
6324 // probably will need to more granularly reset the state here.
6325 ResetLoadProgressState();
clamy44e84ce2016-02-22 15:38:256326 NotifyDisconnected();
6327 SetIsCrashed(status, error_code);
[email protected]960b0372014-05-19 18:01:006328
ericwilligersde386342016-10-19 02:35:096329 for (auto& observer : observers_)
6330 observer.RenderProcessGone(GetCrashedStatus());
[email protected]420ae012009-04-24 05:16:326331}
6332
[email protected]b172aee2012-04-10 17:05:266333void WebContentsImpl::RenderViewDeleted(RenderViewHost* rvh) {
ericwilligersde386342016-10-19 02:35:096334 for (auto& observer : observers_)
6335 observer.RenderViewDeleted(rvh);
[email protected]2e4633c2009-07-09 16:58:066336}
6337
sky8b00392d2015-01-10 00:30:286338void WebContentsImpl::UpdateTargetURL(RenderViewHost* render_view_host,
6339 const GURL& url) {
6340 if (fullscreen_widget_routing_id_ != MSG_ROUTING_NONE) {
Lukasz Anforowiczfd707142018-02-07 19:46:136341 // If we're in flash fullscreen (i.e. Pepper plugin fullscreen) only update
6342 // the url if it's from the fullscreen renderer.
sky8b00392d2015-01-10 00:30:286343 RenderWidgetHostView* fs = GetFullscreenRenderWidgetHostView();
avif9ab5d942015-10-15 14:05:446344 if (fs && fs->GetRenderWidgetHost() != render_view_host->GetWidget())
sky8b00392d2015-01-10 00:30:286345 return;
6346 }
Lukasz Anforowiczfd707142018-02-07 19:46:136347
6348 // In case of racey updates from multiple RenderViewHosts, the last URL should
6349 // be shown - see also some discussion in https://crbug.com/807776.
6350 if (!url.is_valid() && render_view_host != view_that_set_last_target_url_)
6351 return;
6352 view_that_set_last_target_url_ = url.is_valid() ? render_view_host : nullptr;
6353
[email protected]6934a702011-12-20 00:04:516354 if (delegate_)
avi450ce00e2014-09-24 01:58:526355 delegate_->UpdateTargetURL(this, url);
[email protected]420ae012009-04-24 05:16:326356}
6357
Lukasz Anforowiczfd707142018-02-07 19:46:136358void WebContentsImpl::ClearTargetURL() {
6359 view_that_set_last_target_url_ = nullptr;
6360 if (delegate_)
6361 delegate_->UpdateTargetURL(this, GURL());
6362}
6363
[email protected]b172aee2012-04-10 17:05:266364void WebContentsImpl::Close(RenderViewHost* rvh) {
Avi Drissman7c57be72020-07-29 20:09:466365#if defined(OS_MAC)
[email protected]07707302009-11-06 00:50:296366 // The UI may be in an event-tracking loop, such as between the
6367 // mouse-down and mouse-up in text selection or a button click.
6368 // Defer the close until after tracking is complete, so that we
6369 // don't free objects out from under the UI.
[email protected]07707302009-11-06 00:50:296370 // TODO(shess): This could get more fine-grained. For instance,
6371 // closing a tab in another window while selecting text in the
6372 // current window's Omnibox should be just fine.
Christopher Cameron9ae31e0d2019-03-01 20:01:196373 if (view_->CloseTabAfterEventTrackingIfNeeded())
[email protected]07707302009-11-06 00:50:296374 return;
[email protected]60780f412013-02-25 16:34:106375#endif
[email protected]07707302009-11-06 00:50:296376
[email protected]420ae012009-04-24 05:16:326377 // Ignore this if it comes from a RenderViewHost that we aren't showing.
[email protected]151a63d2011-12-20 22:32:526378 if (delegate_ && rvh == GetRenderViewHost())
[email protected]6934a702011-12-20 00:04:516379 delegate_->CloseContents(this);
[email protected]420ae012009-04-24 05:16:326380}
6381
Avi Drissman014dec72018-06-07 02:34:016382void WebContentsImpl::RequestSetBounds(const gfx::Rect& new_bounds) {
6383 if (delegate_)
6384 delegate_->SetContentsBounds(this, new_bounds);
[email protected]420ae012009-04-24 05:16:326385}
6386
fdegansa696e5112015-04-17 01:57:596387void WebContentsImpl::DidStartLoading(FrameTreeNode* frame_tree_node,
[email protected]e3b10d12014-03-28 16:06:096388 bool to_different_document) {
Evan Stadec3b26022017-07-14 19:50:246389 LoadingStateChanged(frame_tree_node->IsMainFrame() && to_different_document,
Carlos IL07f282f2020-05-15 22:58:296390 nullptr);
dmazzonibf8cec42015-02-08 08:28:086391
Xiyuan Xia2a66b112018-12-06 15:52:166392 // Reset the focus state from DidStartNavigation to false if a new load starts
6393 // afterward, in case loading logic triggers a FocusLocationBarByDefault call.
6394 should_focus_location_bar_by_default_ = false;
Ian Prestcdd843d2020-01-28 18:59:586395
6396 // Notify accessibility that the user is navigating away from the
6397 // current document.
6398 // TODO(domfarolino, dmazzoni): Do this using WebContentsObserver. See
6399 // https://crbug.com/981271.
6400 BrowserAccessibilityManager* manager =
6401 frame_tree_node->current_frame_host()->browser_accessibility_manager();
6402 if (manager)
6403 manager->UserIsNavigatingAway();
[email protected]420ae012009-04-24 05:16:326404}
6405
fdegansc73eec22015-03-19 14:47:546406void WebContentsImpl::DidStopLoading() {
dcheng59716272016-04-09 05:19:086407 std::unique_ptr<LoadNotificationDetails> details;
[email protected]420ae012009-04-24 05:16:326408
[email protected]9595fd82013-04-19 21:28:496409 // Use the last committed entry rather than the active one, in case a
6410 // pending entry has been created.
6411 NavigationEntry* entry = controller_.GetLastCommittedEntry();
6412
[email protected]420ae012009-04-24 05:16:326413 // An entry may not exist for a stop when loading an initial blank page or
6414 // if an iframe injected by script into a blank page finishes loading.
6415 if (entry) {
[email protected]420ae012009-04-24 05:16:326416 details.reset(new LoadNotificationDetails(
[email protected]36fc0392011-12-25 03:59:516417 entry->GetVirtualURL(),
[email protected]420ae012009-04-24 05:16:326418 &controller_,
6419 controller_.GetCurrentEntryIndex()));
6420 }
6421
Carlos IL07f282f2020-05-15 22:58:296422 LoadingStateChanged(true, details.get());
[email protected]420ae012009-04-24 05:16:326423}
6424
fdegansa696e5112015-04-17 01:57:596425void WebContentsImpl::DidChangeLoadProgress() {
Nate Chapin22ea6592019-03-05 22:29:026426 if (is_being_destroyed_)
6427 return;
fdegansa696e5112015-04-17 01:57:596428 double load_progress = frame_tree_.load_progress();
6429
6430 // The delegate is notified immediately for the first and last updates. Also,
6431 // since the message loop may be pretty busy when a page is loaded, it might
6432 // not execute a posted task in a timely manner so the progress report is sent
6433 // immediately if enough time has passed.
6434 base::TimeDelta min_delay =
6435 base::TimeDelta::FromMilliseconds(kMinimumDelayBetweenLoadingUpdatesMS);
6436 bool delay_elapsed = loading_last_progress_update_.is_null() ||
6437 base::TimeTicks::Now() - loading_last_progress_update_ > min_delay;
6438
6439 if (load_progress == 0.0 || load_progress == 1.0 || delay_elapsed) {
6440 // If there is a pending task to send progress, it is now obsolete.
6441 loading_weak_factory_.InvalidateWeakPtrs();
6442
6443 // Notify the load progress change.
6444 SendChangeLoadProgress();
6445
6446 // Clean-up the states if needed.
6447 if (load_progress == 1.0)
6448 ResetLoadProgressState();
6449 return;
6450 }
6451
6452 if (loading_weak_factory_.HasWeakPtrs())
6453 return;
6454
skyostil95082a62015-06-05 19:53:076455 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
tzike2aca992017-09-05 08:50:546456 FROM_HERE,
6457 base::BindOnce(&WebContentsImpl::SendChangeLoadProgress,
6458 loading_weak_factory_.GetWeakPtr()),
fdegansa696e5112015-04-17 01:57:596459 min_delay);
6460}
6461
avid6d88b912017-01-13 00:16:006462std::vector<std::unique_ptr<NavigationThrottle>>
6463WebContentsImpl::CreateThrottlesForNavigation(
clamycbf524d42016-09-27 12:48:356464 NavigationHandle* navigation_handle) {
Charlie Reis96077362019-07-04 00:34:046465 auto throttles = GetContentClient()->browser()->CreateThrottlesForNavigation(
clamycbf524d42016-09-27 12:48:356466 navigation_handle);
Charlie Reis96077362019-07-04 00:34:046467
6468 // This is not a normal place to be adding a throttle. However, in the case
6469 // javascript dialogs, related logic is present in the web_contents/ layer,
6470 // and the purpose of the throttle is to ensure that navigation commits are
6471 // deferred for the entire WebContents. Most throttles are either added by
6472 // the embederrer outside of content/, or are per-frame and added by
6473 // NavigationThrottleRunner.
6474 std::unique_ptr<content::NavigationThrottle> dialog_throttle =
6475 JavaScriptDialogNavigationThrottle::MaybeCreateThrottleFor(
6476 navigation_handle);
6477 if (dialog_throttle)
6478 throttles.push_back(std::move(dialog_throttle));
6479
6480 return throttles;
clamycbf524d42016-09-27 12:48:356481}
6482
clamy1e5574e2016-09-29 16:48:446483std::unique_ptr<NavigationUIData> WebContentsImpl::GetNavigationUIData(
6484 NavigationHandle* navigation_handle) {
clamy1e5574e2016-09-29 16:48:446485 return GetContentClient()->browser()->GetNavigationUIData(navigation_handle);
6486}
6487
W. James MacLean1c40862c2020-04-27 21:05:576488void WebContentsImpl::RegisterExistingOriginToPreventOptInIsolation(
W. James MacLeanb70fab82020-05-01 18:51:146489 const url::Origin& origin,
6490 NavigationRequest* navigation_request_to_exclude) {
W. James MacLean1c40862c2020-04-27 21:05:576491 // Note: This function can be made static if we ever need call it without
6492 // a WebContentsImpl instance, in which case we can use a wrapper to
6493 // implement the override from NavigatorDelegate.
6494 for (WebContentsImpl* web_contents : GetAllWebContents()) {
6495 web_contents->controller_.RegisterExistingOriginToPreventOptInIsolation(
6496 origin);
W. James MacLeanb70fab82020-05-01 18:51:146497 // Walk the frame tree to pick up any frames without FrameNavigationEntries.
6498 // * Some frames won't have FrameNavigationEntries (Issues 524208, 608402).
6499 // * Some pending navigations won't have NavigationEntries.
6500 web_contents->GetFrameTree()->RegisterExistingOriginToPreventOptInIsolation(
6501 origin, navigation_request_to_exclude);
W. James MacLean1c40862c2020-04-27 21:05:576502 }
6503}
6504
pdr15f5b5b2017-06-20 00:58:006505void WebContentsImpl::DidCancelLoading() {
6506 controller_.DiscardNonCommittedEntries();
6507
6508 // Update the URL display.
6509 NotifyNavigationStateChanged(INVALIDATE_TYPE_URL);
6510}
6511
[email protected]59167c22013-06-03 18:07:326512void WebContentsImpl::DidAccessInitialDocument() {
[email protected]b4c84012014-04-28 19:51:106513 has_accessed_initial_document_ = true;
6514
[email protected]d1371bf8e2014-04-19 03:26:246515 // We may have left a failed browser-initiated navigation in the address bar
6516 // to let the user edit it and try again. Clear it now that content might
6517 // show up underneath it.
6518 if (!IsLoading() && controller_.GetPendingEntry())
avi45a72532015-04-07 21:01:456519 controller_.DiscardPendingEntry(false);
[email protected]d1371bf8e2014-04-19 03:26:246520
[email protected]59167c22013-06-03 18:07:326521 // Update the URL display.
thestigc4cac8f2014-09-04 21:17:506522 NotifyNavigationStateChanged(INVALIDATE_TYPE_URL);
[email protected]59167c22013-06-03 18:07:326523}
6524
alexmosbe2f4c32015-03-10 02:30:236525void WebContentsImpl::DidChangeName(RenderFrameHost* render_frame_host,
6526 const std::string& name) {
ericwilligersde386342016-10-19 02:35:096527 for (auto& observer : observers_)
6528 observer.FrameNameChanged(render_frame_host, name);
alexmosbe2f4c32015-03-10 02:30:236529}
6530
John Delaneyedd8d6c2019-01-25 00:23:576531void WebContentsImpl::DidReceiveFirstUserActivation(
6532 RenderFrameHost* render_frame_host) {
6533 for (auto& observer : observers_)
6534 observer.FrameReceivedFirstUserActivation(render_frame_host);
6535}
6536
John Delaney0146a742019-01-25 19:25:286537void WebContentsImpl::DidChangeDisplayState(RenderFrameHost* render_frame_host,
6538 bool is_display_none) {
6539 for (auto& observer : observers_)
6540 observer.FrameDisplayStateChanged(render_frame_host, is_display_none);
6541}
6542
6543void WebContentsImpl::FrameSizeChanged(RenderFrameHost* render_frame_host,
6544 const gfx::Size& frame_size) {
6545 for (auto& observer : observers_)
6546 observer.FrameSizeChanged(render_frame_host, frame_size);
6547}
6548
[email protected]39f2f9252014-05-03 00:54:016549void WebContentsImpl::DocumentOnLoadCompleted(
[email protected]50279492014-05-05 23:24:296550 RenderFrameHost* render_frame_host) {
falken52a56e32016-12-08 05:02:406551 ShowInsecureLocalhostWarningIfNeeded();
6552
Alexander Timindd0fa0c52019-11-06 13:03:376553 GetRenderViewHost()->DocumentOnLoadCompletedInMainFrame();
6554
creis97867aa2017-02-17 18:39:456555 is_notifying_observers_ = true;
ericwilligersde386342016-10-19 02:35:096556 for (auto& observer : observers_)
6557 observer.DocumentOnLoadCompletedInMainFrame();
creis97867aa2017-02-17 18:39:456558 is_notifying_observers_ = false;
[email protected]6578fef2013-10-14 02:35:096559
6560 // TODO(avi): Remove. http://crbug.com/170921
[email protected]8ff00d72012-10-23 19:12:216561 NotificationService::current()->Notify(
6562 NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
6563 Source<WebContents>(this),
[email protected]50279492014-05-05 23:24:296564 NotificationService::NoDetails());
[email protected]25497492010-09-11 15:15:086565}
6566
creis8eb8ef22015-11-04 22:51:386567void WebContentsImpl::UpdateStateForFrame(RenderFrameHost* render_frame_host,
6568 const PageState& page_state) {
creis8eb8ef22015-11-04 22:51:386569 // The state update affects the last NavigationEntry associated with the given
6570 // |render_frame_host|. This may not be the last committed NavigationEntry (as
6571 // in the case of an UpdateState from a frame being swapped out). We track
6572 // which entry this is in the RenderFrameHost's nav_entry_id.
6573 RenderFrameHostImpl* rfhi =
6574 static_cast<RenderFrameHostImpl*>(render_frame_host);
6575 NavigationEntryImpl* entry =
6576 controller_.GetEntryWithUniqueID(rfhi->nav_entry_id());
6577 if (!entry)
6578 return;
6579
6580 FrameNavigationEntry* frame_entry =
6581 entry->GetFrameEntry(rfhi->frame_tree_node());
6582 if (!frame_entry)
6583 return;
6584
creisd06a9422015-11-11 03:08:456585 // The SiteInstance might not match if we do a cross-process navigation with
6586 // replacement (e.g., auto-subframe), in which case the swap out of the old
6587 // RenderFrameHost runs in the background after the old FrameNavigationEntry
6588 // has already been replaced and destroyed.
6589 if (frame_entry->site_instance() != rfhi->GetSiteInstance())
6590 return;
6591
creis8eb8ef22015-11-04 22:51:386592 if (page_state == frame_entry->page_state())
6593 return; // Nothing to update.
6594
creis0cade2e2017-02-28 06:37:476595 DCHECK(page_state.IsValid()) << "Shouldn't set an empty PageState.";
nasko34634672016-07-29 18:46:066596
6597 // The document_sequence_number and item_sequence_number recorded in the
6598 // FrameNavigationEntry should not differ from the one coming with the update,
6599 // since it must come from the same document. Do not update it if a difference
6600 // is detected, as this indicates that |frame_entry| is not the correct one.
6601 ExplodedPageState exploded_state;
6602 if (!DecodePageState(page_state.ToEncodedData(), &exploded_state))
6603 return;
6604
6605 if (exploded_state.top.document_sequence_number !=
6606 frame_entry->document_sequence_number() ||
6607 exploded_state.top.item_sequence_number !=
6608 frame_entry->item_sequence_number()) {
6609 return;
6610 }
6611
nasko04a322f25a2016-09-01 00:00:346612 frame_entry->SetPageState(page_state);
creis8eb8ef22015-11-04 22:51:386613 controller_.NotifyEntryChanged(entry);
6614}
6615
[email protected]ef3adfc2014-05-11 00:04:546616void WebContentsImpl::UpdateTitle(RenderFrameHost* render_frame_host,
[email protected]ef3adfc2014-05-11 00:04:546617 const base::string16& title,
6618 base::i18n::TextDirection title_direction) {
[email protected]ef3adfc2014-05-11 00:04:546619 // Try to find the navigation entry, which might not be the current one.
avi286019ce2015-07-29 01:26:586620 // For example, it might be from a recently swapped out RFH.
avi1edc8552016-10-13 21:37:166621 NavigationEntryImpl* entry = controller_.GetEntryWithUniqueID(
creisad8c31a2016-05-24 17:07:046622 static_cast<RenderFrameHostImpl*>(render_frame_host)->nav_entry_id());
[email protected]ef3adfc2014-05-11 00:04:546623
6624 // We can handle title updates when we don't have an entry in
6625 // UpdateTitleForEntry, but only if the update is from the current RVH.
6626 // TODO(avi): Change to make decisions based on the RenderFrameHost.
avi286019ce2015-07-29 01:26:586627 if (!entry && render_frame_host != GetMainFrame())
[email protected]ef3adfc2014-05-11 00:04:546628 return;
6629
6630 // TODO(evan): make use of title_direction.
6631 // http://code.google.com/p/chromium/issues/detail?id=27094
afakhry6f0c1ec22016-07-14 13:55:136632 UpdateTitleForEntry(entry, title);
[email protected]ef3adfc2014-05-11 00:04:546633}
6634
alexmose7da5a12015-04-09 02:22:166635bool WebContentsImpl::ShouldRouteMessageEvent(
6636 RenderFrameHost* target_rfh,
6637 SiteInstance* source_site_instance) const {
6638 // Allow the message if this WebContents is dedicated to a browser plugin
6639 // guest.
[email protected]0eba810b2012-10-18 03:19:366640 // Note: This check means that an embedder could theoretically receive a
6641 // postMessage from anyone (not just its own guests). However, this is
6642 // probably not a risk for apps since other pages won't have references
6643 // to App windows.
alexmose7da5a12015-04-09 02:22:166644 return GetBrowserPluginGuest() || GetBrowserPluginEmbedder();
6645}
[email protected]f546640b2012-05-15 00:03:496646
alexmos58729042015-06-18 23:20:006647void WebContentsImpl::EnsureOpenerProxiesExist(RenderFrameHost* source_rfh) {
alexmose7da5a12015-04-09 02:22:166648 WebContentsImpl* source_web_contents = static_cast<WebContentsImpl*>(
6649 WebContents::FromRenderFrameHost(source_rfh));
[email protected]f546640b2012-05-15 00:03:496650
alexmose7da5a12015-04-09 02:22:166651 if (source_web_contents) {
lazyboy6ec48b2a2015-06-29 15:18:146652 // If this message is going to outer WebContents from inner WebContents,
6653 // then we should not create a RenderView. AttachToOuterWebContentsFrame()
6654 // already created a RenderFrameProxyHost for that purpose.
6655 if (GetBrowserPluginEmbedder() &&
W. James MacLean2539adb32019-12-13 00:40:446656 source_web_contents->browser_plugin_guest_) {
lazyboy6ec48b2a2015-06-29 15:18:146657 return;
W. James MacLean2539adb32019-12-13 00:40:446658 }
lazyboy6ec48b2a2015-06-29 15:18:146659
lfg812874d2016-02-09 22:26:136660 if (this != source_web_contents && GetBrowserPluginGuest()) {
naskob3041b98a42016-03-12 04:43:066661 // We create a RenderFrameProxyHost for the embedder in the guest's render
6662 // process but we intentionally do not expose the embedder's opener chain
6663 // to it.
6664 source_web_contents->GetRenderManager()->CreateRenderFrameProxy(
6665 GetSiteInstance());
[email protected]f546640b2012-05-15 00:03:496666 } else {
alexmos58729042015-06-18 23:20:006667 RenderFrameHostImpl* source_rfhi =
6668 static_cast<RenderFrameHostImpl*>(source_rfh);
6669 source_rfhi->frame_tree_node()->render_manager()->CreateOpenerProxies(
alexmos90325cf2015-09-02 17:18:396670 GetSiteInstance(), nullptr);
[email protected]f546640b2012-05-15 00:03:496671 }
6672 }
[email protected]f546640b2012-05-15 00:03:496673}
6674
avallee9993fca2016-11-17 06:16:506675void WebContentsImpl::SetAsFocusedWebContentsIfNecessary() {
Adithya Srinivasan11af2c52019-12-16 22:52:556676 DCHECK(!portal());
avallee9993fca2016-11-17 06:16:506677 // Only change focus if we are not currently focused.
6678 WebContentsImpl* old_contents = GetFocusedWebContents();
6679 if (old_contents == this)
6680 return;
6681
paulmeyerfeafc2d2017-04-25 21:46:406682 GetOutermostWebContents()->node_.SetFocusedWebContents(this);
6683
avallee9993fca2016-11-17 06:16:506684 // Send a page level blur to the old contents so that it displays inactive UI
6685 // and focus this contents to activate it.
6686 if (old_contents)
6687 old_contents->GetMainFrame()->GetRenderWidgetHost()->SetPageFocus(false);
avalleec2ff1ef2017-01-21 00:12:306688
6689 // Make sure the outer web contents knows our frame is focused. Otherwise, the
6690 // outer renderer could have the element before or after the frame element
6691 // focused which would return early without actually advancing focus.
avalleed68cdd12017-06-21 22:07:276692 FocusOuterAttachmentFrameChain();
avalleec2ff1ef2017-01-21 00:12:306693
Carlos ILd51e7702020-05-07 18:51:396694 GetMainFrame()->GetRenderWidgetHost()->SetPageFocus(true);
avallee9993fca2016-11-17 06:16:506695}
6696
avallee0206f782016-07-28 18:55:336697void WebContentsImpl::SetFocusedFrame(FrameTreeNode* node,
6698 SiteInstance* source) {
Adithya Srinivasan11af2c52019-12-16 22:52:556699 DCHECK(!portal());
avallee0206f782016-07-28 18:55:336700 frame_tree_.SetFocusedFrame(node, source);
avalleed68cdd12017-06-21 22:07:276701
Lucas Furukawa Gadaniabbfa342017-08-29 19:37:576702 if (auto* inner_contents = node_.GetInnerWebContentsInFrame(node)) {
6703 // |this| is an outer WebContents and |node| represents an inner
6704 // WebContents. Transfer the focus to the inner contents if |this| is
6705 // focused.
6706 if (GetFocusedWebContents() == this)
6707 inner_contents->SetAsFocusedWebContentsIfNecessary();
6708 } else if (node_.OuterContentsFrameTreeNode() &&
6709 node_.OuterContentsFrameTreeNode()
6710 ->current_frame_host()
6711 ->GetSiteInstance() == source) {
6712 // |this| is an inner WebContents, |node| is its main FrameTreeNode and
6713 // the outer WebContents FrameTreeNode is at |source|'s SiteInstance.
6714 // Transfer the focus to the inner WebContents if the outer WebContents is
6715 // focused. This branch is used when an inner WebContents is focused through
Lowell Mannersa9530382019-08-01 09:46:576716 // its RenderFrameProxyHost (via FrameFocused mojo call, used to
Lucas Furukawa Gadaniabbfa342017-08-29 19:37:576717 // implement the window.focus() API).
6718 if (GetFocusedWebContents() == GetOuterWebContents())
6719 SetAsFocusedWebContentsIfNecessary();
6720 } else if (!GetOuterWebContents()) {
6721 // This is an outermost WebContents.
6722 SetAsFocusedWebContentsIfNecessary();
Lucas Furukawa Gadaniabbfa342017-08-29 19:37:576723 }
avalleed68cdd12017-06-21 22:07:276724}
6725
Avi Drissman36f801f2018-02-01 20:06:046726void WebContentsImpl::DidCallFocus() {
6727 // Any explicit focusing of another window while this WebContents is in
6728 // fullscreen can be used to confuse the user, so drop fullscreen.
Avi Drissman1a55a9d62020-03-10 18:56:456729 base::ScopedClosureRunner fullscreen_block = ForSecurityDropFullscreen();
6730 // The other contents is independent of this contents, so release the
6731 // fullscreen block.
6732 fullscreen_block.RunAndReset();
Avi Drissman36f801f2018-02-01 20:06:046733}
6734
avalleed68cdd12017-06-21 22:07:276735RenderFrameHost* WebContentsImpl::GetFocusedFrameIncludingInnerWebContents() {
6736 WebContentsImpl* contents = this;
6737 FrameTreeNode* focused_node = contents->frame_tree_.GetFocusedFrame();
6738
6739 // If there is no focused frame in the outer WebContents, we need to return
6740 // null.
6741 if (!focused_node)
6742 return nullptr;
6743
6744 // If the focused frame is embedding an inner WebContents, we must descend
6745 // into that contents. If the current WebContents does not have a focused
6746 // frame, return the main frame of this contents instead of the focused empty
6747 // frame embedding this contents.
6748 while (true) {
6749 contents = contents->node_.GetInnerWebContentsInFrame(focused_node);
6750 if (!contents)
6751 return focused_node->current_frame_host();
6752
6753 focused_node = contents->frame_tree_.GetFocusedFrame();
6754 if (!focused_node)
6755 return contents->GetMainFrame();
6756 }
avallee0206f782016-07-28 18:55:336757}
6758
Lucas Furukawa Gadaniabbfa342017-08-29 19:37:576759void WebContentsImpl::OnAdvanceFocus(RenderFrameHostImpl* source_rfh) {
6760 // When a RenderFrame needs to advance focus to a RenderFrameProxy (by hitting
6761 // TAB), the RenderFrameProxy sends an IPC to RenderFrameProxyHost. When this
6762 // RenderFrameProxyHost represents an inner WebContents, the outer WebContents
6763 // needs to focus the inner WebContents.
6764 if (GetOuterWebContents() &&
6765 GetOuterWebContents() == source_rfh->delegate()->GetAsWebContents() &&
6766 GetFocusedWebContents() == GetOuterWebContents()) {
6767 SetAsFocusedWebContentsIfNecessary();
6768 }
6769}
6770
ekaramada110f642016-12-21 19:47:286771void WebContentsImpl::OnFocusedElementChangedInFrame(
6772 RenderFrameHostImpl* frame,
6773 const gfx::Rect& bounds_in_root_view) {
6774 RenderWidgetHostViewBase* root_view =
6775 static_cast<RenderWidgetHostViewBase*>(GetRenderWidgetHostView());
6776 if (!root_view || !frame->GetView())
6777 return;
Katie De0131372020-07-09 23:03:106778 // Convert to screen coordinates from window coordinates by adding the
6779 // window's origin.
ekaramada110f642016-12-21 19:47:286780 gfx::Point origin = bounds_in_root_view.origin();
6781 origin += root_view->GetViewBounds().OffsetFromOrigin();
6782 gfx::Rect bounds_in_screen(origin, bounds_in_root_view.size());
6783
6784 root_view->FocusedNodeChanged(frame->has_focused_editable_element(),
6785 bounds_in_screen);
6786
6787 FocusedNodeDetails details = {frame->has_focused_editable_element(),
6788 bounds_in_screen};
6789
6790 // TODO(ekaramad): We should replace this with an observer notification
6791 // (https://crbug.com/675975).
6792 NotificationService::current()->Notify(
6793 NOTIFICATION_FOCUS_CHANGED_IN_PAGE,
6794 Source<RenderViewHost>(GetRenderViewHost()),
6795 Details<FocusedNodeDetails>(&details));
Jeff Fisher709b4c52019-07-29 20:07:346796
6797 for (auto& observer : observers_)
6798 observer.OnFocusChangedInPage(&details);
ekaramada110f642016-12-21 19:47:286799}
6800
Lowell Manners1de5242e2019-04-25 10:18:466801bool WebContentsImpl::DidAddMessageToConsole(
6802 blink::mojom::ConsoleMessageLevel log_level,
6803 const base::string16& message,
6804 int32_t line_no,
6805 const base::string16& source_id) {
Devlin Cronin5ea668f32019-12-18 21:57:586806 for (auto& observer : observers_)
6807 observer.OnDidAddMessageToConsole(log_level, message, line_no, source_id);
6808
[email protected]a796f202012-05-30 14:14:256809 if (!delegate_)
6810 return false;
Lowell Manners1de5242e2019-04-25 10:18:466811 return delegate_->DidAddMessageToConsole(this, log_level, message, line_no,
avia90ae4e2016-11-11 20:49:336812 source_id);
[email protected]a796f202012-05-30 14:14:256813}
6814
Daniel Cheng90196c82018-04-25 21:49:146815void WebContentsImpl::DidReceiveInputEvent(
dominickn9c7ceaa32016-02-27 01:52:536816 RenderWidgetHostImpl* render_widget_host,
Emily Starkc7bd40c42020-07-21 19:12:556817 const blink::WebInputEvent& event) {
6818 if (!IsUserInteractionInputType(event.GetType()))
Daniel Cheng90196c82018-04-25 21:49:146819 return;
6820
kenrb19221852016-04-29 17:21:406821 // Ignore unless the widget is currently in the frame tree.
6822 if (!HasMatchingWidgetHost(&frame_tree_, render_widget_host))
avic3aa8422015-11-09 20:57:226823 return;
6824
Emily Starkc7bd40c42020-07-21 19:12:556825 if (event.GetType() != blink::WebInputEvent::Type::kGestureScrollBegin)
Daniel Cheng90196c82018-04-25 21:49:146826 last_interactive_input_event_time_ = ui::EventTimeForNow();
dominickn9c7ceaa32016-02-27 01:52:536827
Dominick Ng37a15052018-07-17 23:56:356828 for (auto& observer : observers_)
Emily Starkc7bd40c42020-07-21 19:12:556829 observer.DidGetUserInteraction(event);
dominickn2dd142dd2015-10-29 05:30:506830}
6831
Avi Drissman738ea192018-08-29 20:24:166832bool WebContentsImpl::ShouldIgnoreInputEvents() {
6833 WebContentsImpl* web_contents = this;
6834 while (web_contents) {
6835 if (web_contents->ignore_input_events_)
6836 return true;
6837 web_contents = web_contents->GetOuterWebContents();
6838 }
6839
6840 return false;
6841}
6842
avallee9993fca2016-11-17 06:16:506843void WebContentsImpl::FocusOwningWebContents(
6844 RenderWidgetHostImpl* render_widget_host) {
Alex Moshchukc4f0a722019-11-19 22:41:286845 RenderWidgetHostImpl* main_frame_widget_host =
6846 GetMainFrame()->GetRenderWidgetHost();
avallee9993fca2016-11-17 06:16:506847 RenderWidgetHostImpl* focused_widget =
Alex Moshchukc4f0a722019-11-19 22:41:286848 GetFocusedRenderWidgetHost(main_frame_widget_host);
avallee9993fca2016-11-17 06:16:506849
6850 if (focused_widget != render_widget_host &&
avallee8e881592016-11-19 00:39:306851 (!focused_widget ||
6852 focused_widget->delegate() != render_widget_host->delegate())) {
avallee9993fca2016-11-17 06:16:506853 SetAsFocusedWebContentsIfNecessary();
6854 }
6855}
6856
[email protected]b172aee2012-04-10 17:05:266857void WebContentsImpl::OnIgnoredUIEvent() {
[email protected]31145092011-09-30 01:49:446858 // Notify observers.
ericwilligersde386342016-10-19 02:35:096859 for (auto& observer : observers_)
6860 observer.DidGetIgnoredUIEvent();
[email protected]fa1cf0b82010-01-15 21:49:446861}
6862
avic3aa8422015-11-09 20:57:226863void WebContentsImpl::RendererUnresponsive(
Lukasz Anforowicz52b93722018-06-20 16:11:396864 RenderWidgetHostImpl* render_widget_host,
6865 base::RepeatingClosure hang_monitor_restarter) {
avic031d392017-03-03 03:09:426866 if (ShouldIgnoreUnresponsiveRenderer())
[email protected]e5fc1632011-08-08 07:51:536867 return;
6868
Lukasz Anforowicz4f0593d2018-09-21 18:33:546869 // Do not report hangs (to task manager, to hang renderer dialog, etc.) for
6870 // invisible tabs (like extension background page, background tabs). See
6871 // https://crbug.com/881812 for rationale and for choosing the visibility
6872 // (rather than process priority) as the signal here.
6873 if (GetVisibility() != Visibility::VISIBLE)
6874 return;
6875
Avi Drissman8920def2018-01-31 19:53:366876 if (!render_widget_host->renderer_initialized())
[email protected]55452902011-06-01 21:57:476877 return;
6878
Lukasz Anforowicz4f0593d2018-09-21 18:33:546879 for (auto& observer : observers_)
6880 observer.OnRendererUnresponsive(render_widget_host->GetProcess());
6881
Dave Tapuskac6146c52017-12-20 22:48:156882 if (delegate_)
Lukasz Anforowicz52b93722018-06-20 16:11:396883 delegate_->RendererUnresponsive(this, render_widget_host,
6884 std::move(hang_monitor_restarter));
[email protected]420ae012009-04-24 05:16:326885}
6886
avic3aa8422015-11-09 20:57:226887void WebContentsImpl::RendererResponsive(
6888 RenderWidgetHostImpl* render_widget_host) {
Scott Violet48202e62020-05-26 17:19:336889 for (auto& observer : observers_)
6890 observer.OnRendererResponsive(render_widget_host->GetProcess());
6891
[email protected]6934a702011-12-20 00:04:516892 if (delegate_)
Avi Drissman1572e8c3c2018-02-02 19:06:366893 delegate_->RendererResponsive(this, render_widget_host);
[email protected]420ae012009-04-24 05:16:326894}
6895
Alex Moshchuk7b4f0652019-05-30 18:54:416896void WebContentsImpl::SubframeCrashed(
6897 blink::mojom::FrameVisibility visibility) {
6898 // If a subframe crashed on a hidden tab, mark the tab for reload to avoid
6899 // showing a sad frame to the user if they ever switch back to that tab. Do
6900 // this for subframes that are either visible in viewport or visible but
6901 // scrolled out of view, but skip subframes that are not rendered (e.g., via
6902 // "display:none"), since in that case the user wouldn't see a sad frame
6903 // anyway.
6904 bool did_mark_for_reload = false;
6905 if (IsHidden() && visibility != blink::mojom::FrameVisibility::kNotRendered &&
6906 base::FeatureList::IsEnabled(
6907 features::kReloadHiddenTabsWithCrashedSubframes)) {
6908 controller_.SetNeedsReload(
6909 NavigationControllerImpl::NeedsReloadType::kCrashedSubframe);
6910 did_mark_for_reload = true;
6911 UMA_HISTOGRAM_ENUMERATION(
6912 "Stability.ChildFrameCrash.TabMarkedForReload.Visibility", visibility);
6913 }
6914
6915 UMA_HISTOGRAM_BOOLEAN("Stability.ChildFrameCrash.TabMarkedForReload",
6916 did_mark_for_reload);
6917}
6918
[email protected]b172aee2012-04-10 17:05:266919void WebContentsImpl::BeforeUnloadFiredFromRenderManager(
[email protected]feaded02012-09-25 19:12:556920 bool proceed, const base::TimeTicks& proceed_time,
[email protected]420ae012009-04-24 05:16:326921 bool* proceed_to_fire_unload) {
ericwilligersde386342016-10-19 02:35:096922 for (auto& observer : observers_)
Chris Hamilton3b3e315d2018-09-19 13:16:216923 observer.BeforeUnloadFired(proceed, proceed_time);
[email protected]6934a702011-12-20 00:04:516924 if (delegate_)
6925 delegate_->BeforeUnloadFired(this, proceed, proceed_to_fire_unload);
[email protected]e80af492013-06-24 21:52:096926 // Note: |this| might be deleted at this point.
[email protected]420ae012009-04-24 05:16:326927}
6928
[email protected]58d5cfe2013-07-10 02:40:526929void WebContentsImpl::RenderProcessGoneFromRenderManager(
[email protected]3a3d47472010-07-15 21:03:546930 RenderViewHost* render_view_host) {
[email protected]443b80e2010-12-14 00:42:236931 DCHECK(crashed_status_ != base::TERMINATION_STATUS_STILL_RUNNING);
[email protected]ec6a7eb2013-04-22 17:34:226932 RenderViewTerminated(render_view_host, crashed_status_, crashed_error_code_);
[email protected]3a3d47472010-07-15 21:03:546933}
6934
[email protected]af905902013-10-01 21:38:516935void WebContentsImpl::CancelModalDialogsForRenderManager() {
6936 // We need to cancel modal dialogs when doing a process swap, since the load
avi2460c762015-04-17 15:21:546937 // deferrer would prevent us from swapping out. We also clear the state
6938 // because this is a cross-process navigation, which means that it's a new
6939 // site that should not have to pay for the sins of its predecessor.
creis89a0f782015-05-27 16:13:176940 //
Lei Zhang6410ec1a2019-01-31 21:38:586941 // Note that we don't bother telling |browser_plugin_embedder_| because the
creis89a0f782015-05-27 16:13:176942 // cross-process navigation will either destroy the browser plugins or not
6943 // require their dialogs to close.
avi5d3b8692016-10-12 22:00:466944 if (dialog_manager_) {
avi6879fcf2017-01-21 05:27:536945 dialog_manager_->CancelDialogs(this, /*reset_state=*/true);
avi5d3b8692016-10-12 22:00:466946 }
[email protected]af905902013-10-01 21:38:516947}
6948
danakja0bce4c952020-05-12 20:40:286949void WebContentsImpl::NotifySwappedFromRenderManager(RenderFrameHost* old_frame,
6950 RenderFrameHost* new_frame,
[email protected]02d7b6e2014-06-24 21:01:506951 bool is_main_frame) {
6952 if (is_main_frame) {
danakja0bce4c952020-05-12 20:40:286953 // The |new_frame| and its various compadres are already swapped into place
6954 // for the WebContentsImpl when this method is called.
6955 DCHECK(GetMainFrame() == new_frame);
6956 DCHECK(GetRenderViewHost() == new_frame->GetRenderViewHost());
6957 DCHECK(GetRenderWidgetHostView() == new_frame->GetView());
6958
Fergal Daly6767b2e2020-02-04 03:15:166959 RenderViewHost* old_rvh =
danakja0bce4c952020-05-12 20:40:286960 old_frame ? old_frame->GetRenderViewHost() : nullptr;
6961 RenderViewHost* new_rvh = new_frame->GetRenderViewHost();
Fergal Daly6767b2e2020-02-04 03:15:166962 // |old_rvh| and |new_rvh| might be equal when navigating from a crashed
6963 // RenderFrameHost to a new same-site one. With RenderDocument, this will
6964 // happen for every same-site navigation.
6965 if (old_rvh != new_rvh)
6966 NotifyViewSwapped(old_rvh, new_rvh);
[email protected]a6b73c62013-02-11 23:05:086967
[email protected]02d7b6e2014-06-24 21:01:506968 // Make sure the visible RVH reflects the new delegate's preferences.
6969 if (delegate_)
6970 view_->SetOverscrollControllerEnabled(CanOverscrollContent());
[email protected]d56a49e2013-02-23 00:47:396971
danakja0bce4c952020-05-12 20:40:286972 auto* rwhv = static_cast<RenderWidgetHostViewBase*>(new_frame->GetView());
6973 if (rwhv) {
6974 rwhv->SetMainFrameAXTreeID(new_frame->GetAXTreeID());
6975
6976 // The RenderWidgetHostView for the speculative RenderFrameHost is not
6977 // resized with the current RenderFrameHost while a navigation is
6978 // pending. So when we swap in the main frame, we need to update the
6979 // RenderWidgetHostView's size.
6980 //
6981 // Historically, this was done to fix b/1079768 for interstitials.
6982 rwhv->SetSize(GetSizeForMainFrame());
6983 }
[email protected]02d7b6e2014-06-24 21:01:506984 }
6985
danakja0bce4c952020-05-12 20:40:286986 NotifyFrameSwapped(old_frame, new_frame, is_main_frame);
[email protected]3a3d47472010-07-15 21:03:546987}
6988
naskof5940b9f2015-03-02 23:04:056989void WebContentsImpl::NotifyMainFrameSwappedFromRenderManager(
danakja0bce4c952020-05-12 20:40:286990 RenderFrameHost* old_frame,
6991 RenderFrameHost* new_frame) {
6992 NotifyViewSwapped(old_frame ? old_frame->GetRenderViewHost() : nullptr,
6993 new_frame->GetRenderViewHost());
naskof5940b9f2015-03-02 23:04:056994}
6995
[email protected]b172aee2012-04-10 17:05:266996NavigationControllerImpl& WebContentsImpl::GetControllerForRenderManager() {
[email protected]d1198fd2012-08-13 22:50:196997 return GetController();
[email protected]3a3d47472010-07-15 21:03:546998}
6999
dcheng59716272016-04-09 05:19:087000std::unique_ptr<WebUIImpl> WebContentsImpl::CreateWebUIForRenderFrameHost(
Miyoung Shin072d4052020-07-13 07:52:447001 RenderFrameHost* frame_host,
Denis Kuznetsov [CET]2c6f0159f2020-05-18 14:18:167002 const GURL& url) {
Miyoung Shin072d4052020-07-13 07:52:447003 return CreateWebUI(frame_host, url);
[email protected]420ae012009-04-24 05:16:327004}
7005
lfgbede6392015-09-11 21:54:067006void WebContentsImpl::CreateRenderWidgetHostViewForRenderManager(
7007 RenderViewHost* render_view_host) {
lfgc740d4b22016-04-15 16:45:337008 RenderWidgetHostViewBase* rwh_view =
W. James MacLean13d834d2019-12-04 16:06:457009 view_->CreateViewForWidget(render_view_host->GetWidget());
danakja0bce4c952020-05-12 20:40:287010 rwh_view->SetSize(GetSizeForMainFrame());
lfgbede6392015-09-11 21:54:067011}
7012
7013bool WebContentsImpl::CreateRenderViewForRenderManager(
7014 RenderViewHost* render_view_host,
Antonio Gomesb3dd5fc2020-05-25 22:53:277015 const base::Optional<base::UnguessableToken>& opener_frame_token,
Daniel Chengabfdb002020-06-04 06:02:307016 int proxy_routing_id) {
lfgbede6392015-09-11 21:54:067017 TRACE_EVENT0("browser,navigation",
7018 "WebContentsImpl::CreateRenderViewForRenderManager");
7019
piman00cf9402017-04-03 17:09:207020 if (proxy_routing_id == MSG_ROUTING_NONE)
lfg9431efd2016-12-14 14:45:267021 CreateRenderWidgetHostViewForRenderManager(render_view_host);
lfg9431efd2016-12-14 14:45:267022
piman00cf9402017-04-03 17:09:207023 if (!static_cast<RenderViewHostImpl*>(render_view_host)
Daniel Chengabfdb002020-06-04 06:02:307024 ->CreateRenderView(opener_frame_token, proxy_routing_id,
Antonio Gomesb3dd5fc2020-05-25 22:53:277025 created_with_opener_)) {
[email protected]a4127722011-04-27 23:13:527026 return false;
[email protected]14392a52012-05-02 20:28:447027 }
W. James MacLean62198672019-06-04 16:46:107028 // Set the TextAutosizer state from the main frame's renderer on the new view,
7029 // but only if it's not for the main frame. Main frame renderers should create
7030 // this state themselves from up-to-date values, so we shouldn't override it
7031 // with the cached values.
7032 if (!render_view_host->GetMainFrame()) {
7033 render_view_host->Send(
7034 new PageMsg_UpdateTextAutosizerPageInfoForRemoteMainFrames(
7035 render_view_host->GetRoutingID(), text_autosizer_page_info_));
7036 }
[email protected]a4127722011-04-27 23:13:527037
Lucas Furukawa Gadaniaed1fed2017-10-13 17:34:147038 if (proxy_routing_id == MSG_ROUTING_NONE && node_.outer_web_contents())
7039 ReattachToOuterWebContentsFrame();
7040
avi2b177592014-12-10 02:08:027041 SetHistoryOffsetAndLengthForView(render_view_host,
7042 controller_.GetLastCommittedEntryIndex(),
7043 controller_.GetEntryCount());
7044
Avi Drissman7c57be72020-07-29 20:09:467045#if defined(OS_POSIX) && !defined(OS_MAC) && !defined(OS_ANDROID)
[email protected]f8233cc2011-05-31 20:24:507046 // Force a ViewMsg_Resize to be sent, needed to make plugins show up on
7047 // linux. See crbug.com/83941.
avif9ab5d942015-10-15 14:05:447048 RenderWidgetHostView* rwh_view = render_view_host->GetWidget()->GetView();
[email protected]245f7d52011-11-28 15:36:447049 if (rwh_view) {
7050 if (RenderWidgetHost* render_widget_host = rwh_view->GetRenderWidgetHost())
Fady Samuel0b911822018-04-25 13:22:167051 render_widget_host->SynchronizeVisualProperties();
[email protected]245f7d52011-11-28 15:36:447052 }
[email protected]f8233cc2011-05-31 20:24:507053#endif
7054
[email protected]420ae012009-04-24 05:16:327055 return true;
7056}
7057
[email protected]82307f6b2014-08-07 03:30:127058bool WebContentsImpl::CreateRenderFrameForRenderManager(
7059 RenderFrameHost* render_frame_host,
arthursonzogni1efc7b962019-03-14 09:36:337060 int previous_routing_id,
Antonio Gomes9d150542020-05-25 18:32:117061 const base::Optional<base::UnguessableToken>& opener_frame_token,
japhete6adf142014-10-31 00:01:497062 int parent_routing_id,
alexmosa181efc2015-09-03 00:39:047063 int previous_sibling_routing_id) {
naskob8744d22014-08-28 17:07:437064 TRACE_EVENT0("browser,navigation",
7065 "WebContentsImpl::CreateRenderFrameForRenderManager");
[email protected]82307f6b2014-08-07 03:30:127066
7067 RenderFrameHostImpl* rfh =
7068 static_cast<RenderFrameHostImpl*>(render_frame_host);
Antonio Gomes9d150542020-05-25 18:32:117069 if (!rfh->CreateRenderFrame(previous_routing_id, opener_frame_token,
7070 parent_routing_id, previous_sibling_routing_id)) {
[email protected]82307f6b2014-08-07 03:30:127071 return false;
Antonio Gomes9d150542020-05-25 18:32:117072 }
[email protected]82307f6b2014-08-07 03:30:127073
7074 // TODO(nasko): When RenderWidgetHost is owned by RenderFrameHost, the passed
7075 // RenderFrameHost will have to be associated with the appropriate
7076 // RenderWidgetHostView or a new one should be created here.
7077
7078 return true;
7079}
7080
mfomitchev2b8b066a2016-01-28 19:23:157081#if defined(OS_ANDROID)
[email protected]fc2b46b2014-05-03 16:33:457082
[email protected]155c7f22013-12-09 17:07:187083base::android::ScopedJavaLocalRef<jobject>
7084WebContentsImpl::GetJavaWebContents() {
mostynb042582e2015-03-16 22:13:407085 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]0e813a52014-08-13 10:34:567086 return GetWebContentsAndroid()->GetJavaObject();
7087}
[email protected]155c7f22013-12-09 17:07:187088
[email protected]0e813a52014-08-13 10:34:567089WebContentsAndroid* WebContentsImpl::GetWebContentsAndroid() {
Kevin McNee576bb5d82019-05-29 16:47:517090 if (!web_contents_android_) {
7091 web_contents_android_ = std::make_unique<WebContentsAndroid>(this);
[email protected]155c7f22013-12-09 17:07:187092 }
Kevin McNee576bb5d82019-05-29 16:47:517093 return web_contents_android_.get();
7094}
7095
7096void WebContentsImpl::ClearWebContentsAndroid() {
7097 DCHECK_CURRENTLY_ON(BrowserThread::UI);
7098 web_contents_android_.reset();
[email protected]155c7f22013-12-09 17:07:187099}
7100
paulmeyerc0b762b2016-04-13 11:55:177101void WebContentsImpl::ActivateNearestFindResult(float x,
7102 float y) {
7103 GetOrCreateFindRequestManager()->ActivateNearestFindResult(x, y);
7104}
7105
7106void WebContentsImpl::RequestFindMatchRects(int current_version) {
7107 GetOrCreateFindRequestManager()->RequestFindMatchRects(current_version);
7108}
7109
rockot400ea35b2016-10-15 19:15:327110service_manager::InterfaceProvider* WebContentsImpl::GetJavaInterfaces() {
sammcf5f1b0f2016-09-20 23:05:117111 if (!java_interfaces_) {
Gyuyoung Kim6c9ce9022019-11-26 05:40:087112 mojo::PendingRemote<service_manager::mojom::InterfaceProvider> provider;
7113 BindInterfaceRegistryForWebContents(
7114 provider.InitWithNewPipeAndPassReceiver(), this);
rockot400ea35b2016-10-15 19:15:327115 java_interfaces_.reset(new service_manager::InterfaceProvider);
sammcf5f1b0f2016-09-20 23:05:117116 java_interfaces_->Bind(std::move(provider));
7117 }
7118 return java_interfaces_.get();
7119}
7120
Francois Doray6c26f3342018-08-24 17:17:227121#endif
7122
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:417123bool WebContentsImpl::CompletedFirstVisuallyNonEmptyPaint() {
Carlos Caballero91fffb22019-10-29 15:24:307124 return GetRenderViewHost()->did_first_visually_non_empty_paint();
spqchane72c3cb2017-06-30 23:02:107125}
7126
pkotwicz75ca8ffd2016-02-16 23:10:197127void WebContentsImpl::OnDidDownloadImage(
Avi Drissmana5a52dd2018-03-27 03:39:027128 ImageDownloadCallback callback,
pkotwicz75ca8ffd2016-02-16 23:10:197129 int id,
7130 const GURL& image_url,
leon.hancbc4bc3c2016-02-26 07:08:527131 int32_t http_status_code,
leon.hanc2228532016-08-16 05:59:187132 const std::vector<SkBitmap>& images,
7133 const std::vector<gfx::Size>& original_image_sizes) {
Avi Drissmana5a52dd2018-03-27 03:39:027134 std::move(callback).Run(id, http_status_code, image_url, images,
7135 original_image_sizes);
pkotwicz75ca8ffd2016-02-16 23:10:197136}
7137
[email protected]6fba26d2014-04-29 09:38:287138void WebContentsImpl::OnDialogClosed(int render_process_id,
7139 int render_frame_id,
Dave Tapuskacdf545cc2020-01-23 18:38:527140 JavaScriptDialogCallback response_callback,
Avi Drissman1a55a9d62020-03-10 18:56:457141 base::ScopedClosureRunner fullscreen_block,
[email protected]87de04b02014-04-08 22:14:497142 bool dialog_was_suppressed,
[email protected]b172aee2012-04-10 17:05:267143 bool success,
[email protected]fcf75d42013-12-03 20:11:267144 const base::string16& user_input) {
[email protected]6fba26d2014-04-29 09:38:287145 RenderFrameHostImpl* rfh = RenderFrameHostImpl::FromID(render_process_id,
7146 render_frame_id);
[email protected]87de04b02014-04-08 22:14:497147 last_dialog_suppressed_ = dialog_was_suppressed;
Avi Drissman1a55a9d62020-03-10 18:56:457148 fullscreen_block.RunAndReset();
[email protected]87de04b02014-04-08 22:14:497149
Charlie Reis96077362019-07-04 00:34:047150 javascript_dialog_navigation_deferrer_.reset();
7151
[email protected]beb440c2009-11-06 04:08:547152 if (is_showing_before_unload_dialog_ && !success) {
clamybc6f4152016-04-01 20:14:237153 // It is possible for the current RenderFrameHost to have changed in the
7154 // meantime. Do not reset the navigation state in that case.
7155 if (rfh && rfh == rfh->frame_tree_node()->current_frame_host()) {
clamy44e84ce2016-02-22 15:38:257156 rfh->frame_tree_node()->BeforeUnloadCanceled();
clamybc6f4152016-04-01 20:14:237157 controller_.DiscardNonCommittedEntries();
7158 }
[email protected]ec8e8b02013-06-19 04:57:107159
Charles Reis15d60b132017-11-03 17:43:477160 // Update the URL display either way, to avoid showing a stale URL.
7161 NotifyNavigationStateChanged(INVALIDATE_TYPE_URL);
7162
ericwilligersde386342016-10-19 02:35:097163 for (auto& observer : observers_)
7164 observer.BeforeUnloadDialogCancelled();
[email protected]beb440c2009-11-06 04:08:547165 }
[email protected]87de04b02014-04-08 22:14:497166
Dave Tapuskacdf545cc2020-01-23 18:38:527167 std::move(response_callback).Run(success, user_input);
Pavel Feldman3c1842b2017-08-02 05:00:167168
Dave Tapuskacdf545cc2020-01-23 18:38:527169 std::vector<protocol::PageHandler*> page_handlers =
7170 protocol::PageHandler::EnabledForWebContents(this);
7171 for (auto* handler : page_handlers)
7172 handler->DidCloseJavaScriptDialog(success, user_input);
avi1c51fb62017-02-27 20:43:037173
7174 is_showing_javascript_dialog_ = false;
7175 is_showing_before_unload_dialog_ = false;
[email protected]beb440c2009-11-06 04:08:547176}
7177
[email protected]1ac10dca2013-08-20 20:47:047178bool WebContentsImpl::IsHidden() {
Francois Doraye6161152018-03-27 22:05:377179 return !IsBeingCaptured() && visibility_ != Visibility::VISIBLE;
[email protected]1ac10dca2013-08-20 20:47:047180}
7181
rob97250742015-12-10 17:45:157182int WebContentsImpl::GetOuterDelegateFrameTreeNodeId() {
lfg601233692017-03-06 22:45:447183 return node_.outer_contents_frame_tree_node_id();
lazyboy6ec48b2a2015-06-29 15:18:147184}
7185
ekaramadaeb3c5c2016-11-09 03:22:357186RenderWidgetHostImpl* WebContentsImpl::GetFullscreenRenderWidgetHost() const {
7187 return RenderWidgetHostImpl::FromID(fullscreen_widget_process_id_,
7188 fullscreen_widget_routing_id_);
7189}
7190
[email protected]b0936d22013-11-28 06:47:367191RenderFrameHostManager* WebContentsImpl::GetRenderManager() const {
[email protected]fa944cb82013-11-15 17:51:217192 return frame_tree_.root()->render_manager();
7193}
7194
[email protected]f8497342013-02-05 22:15:027195BrowserPluginGuest* WebContentsImpl::GetBrowserPluginGuest() const {
[email protected]7a846df2012-09-20 19:17:397196 return browser_plugin_guest_.get();
7197}
7198
Albert J. Wongc55cc64d2018-10-12 02:50:047199void WebContentsImpl::SetBrowserPluginGuest(
7200 std::unique_ptr<BrowserPluginGuest> guest) {
7201 DCHECK(!browser_plugin_guest_);
7202 DCHECK(guest);
7203 browser_plugin_guest_ = std::move(guest);
[email protected]738f57a2013-06-29 21:06:547204}
7205
Aidan Wolter52bcc12a2018-11-14 03:27:297206base::UnguessableToken WebContentsImpl::GetAudioGroupId() {
Aidan Wolter52bcc12a2018-11-14 03:27:297207 return GetAudioStreamFactory()->group_id();
7208}
7209
Miyoung Shin5709ab02020-02-19 06:50:417210const std::vector<blink::mojom::FaviconURLPtr>&
7211WebContentsImpl::GetFaviconURLs() {
Mounir Lamouri7b4a1f232019-12-23 16:32:107212 return favicon_urls_;
7213}
7214
Mugdha Lakhani0a0d7862020-07-29 09:58:457215// The Mac implementation of the next two methods is in
7216// web_contents_impl_mac.mm
7217#if !defined(OS_MACOSX)
7218
7219void WebContentsImpl::Resize(const gfx::Rect& new_bounds) {
7220#if defined(USE_AURA)
7221 aura::Window* window = GetNativeView();
7222 window->SetBounds(gfx::Rect(window->bounds().origin(), new_bounds.size()));
7223#elif defined(OS_ANDROID)
7224 content::RenderWidgetHostView* view = GetRenderWidgetHostView();
7225 if (view)
7226 view->SetBounds(new_bounds);
7227#endif
7228}
7229
7230gfx::Size WebContentsImpl::GetSize() {
7231#if defined(USE_AURA)
7232 aura::Window* window = GetNativeView();
7233 return window->bounds().size();
7234#elif defined(OS_ANDROID)
7235 ui::ViewAndroid* view_android = GetNativeView();
7236 return view_android->bounds().size();
7237#endif
7238}
7239
7240#endif // !defined(OS_MACOSX)
7241
[email protected]f8497342013-02-05 22:15:027242BrowserPluginEmbedder* WebContentsImpl::GetBrowserPluginEmbedder() const {
[email protected]7a846df2012-09-20 19:17:397243 return browser_plugin_embedder_.get();
7244}
[email protected]8ff00d72012-10-23 19:12:217245
fsamuel60b42282015-03-10 03:29:147246void WebContentsImpl::CreateBrowserPluginEmbedderIfNecessary() {
7247 if (browser_plugin_embedder_)
7248 return;
7249 browser_plugin_embedder_.reset(BrowserPluginEmbedder::Create(this));
7250}
7251
danakja0bce4c952020-05-12 20:40:287252gfx::Size WebContentsImpl::GetSizeForMainFrame() {
7253 if (delegate_) {
7254 // The delegate has a chance to specify a size independent of the UI.
7255 gfx::Size delegate_size = delegate_->GetSizeForNewRenderView(this);
7256 if (!delegate_size.IsEmpty())
7257 return delegate_size;
7258 }
7259
7260 // Device emulation, when enabled, can specify a size independent of the UI.
7261 if (!device_emulation_size_.IsEmpty())
7262 return device_emulation_size_;
7263
7264 return GetContainerBounds().size();
[email protected]dc0fd432013-08-27 15:29:217265}
7266
[email protected]ae7eeda2014-07-04 01:53:267267void WebContentsImpl::OnFrameRemoved(RenderFrameHost* render_frame_host) {
ericwilligersde386342016-10-19 02:35:097268 for (auto& observer : observers_)
7269 observer.FrameDeleted(render_frame_host);
[email protected]9b159a52013-10-03 17:24:557270}
7271
[email protected]222f5822014-02-05 23:40:497272void WebContentsImpl::OnPreferredSizeChanged(const gfx::Size& old_size) {
7273 if (!delegate_)
7274 return;
7275 const gfx::Size new_size = GetPreferredSize();
7276 if (new_size != old_size)
7277 delegate_->UpdatePreferredSize(this, new_size);
7278}
7279
Miyoung Shin072d4052020-07-13 07:52:447280std::unique_ptr<WebUIImpl> WebContentsImpl::CreateWebUI(
7281 RenderFrameHost* frame_host,
7282 const GURL& url) {
7283 std::unique_ptr<WebUIImpl> web_ui =
7284 std::make_unique<WebUIImpl>(this, frame_host);
Lei Zhangee3b63ad2018-06-06 23:25:317285 std::unique_ptr<WebUIController> controller(
avie865b1d2016-10-24 19:42:597286 WebUIControllerFactoryRegistry::GetInstance()
Lei Zhangee3b63ad2018-06-06 23:25:317287 ->CreateWebUIControllerForURL(web_ui.get(), url));
dbeama1b926a2015-08-31 23:17:517288 if (controller) {
Lei Zhangee3b63ad2018-06-06 23:25:317289 web_ui->SetController(std::move(controller));
dbeama1b926a2015-08-31 23:17:517290 return web_ui;
7291 }
7292
avie865b1d2016-10-24 19:42:597293 return nullptr;
dbeama1b926a2015-08-31 23:17:517294}
7295
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:417296FindRequestManager* WebContentsImpl::GetFindRequestManager() {
7297 for (auto* contents = this; contents;
paulmeyerfeafc2d2017-04-25 21:46:407298 contents = contents->GetOuterWebContents()) {
7299 if (contents->find_request_manager_)
7300 return contents->find_request_manager_.get();
7301 }
7302
7303 return nullptr;
7304}
7305
paulmeyerc0b762b2016-04-13 11:55:177306FindRequestManager* WebContentsImpl::GetOrCreateFindRequestManager() {
paulmeyerfeafc2d2017-04-25 21:46:407307 if (FindRequestManager* manager = GetFindRequestManager())
7308 return manager;
7309
Ehsan Karamad6beb2ea2018-11-25 18:15:137310 DCHECK(!browser_plugin_guest_ || GetOuterWebContents());
7311
paulmeyerfeafc2d2017-04-25 21:46:407312 // No existing FindRequestManager found, so one must be created.
7313 find_request_manager_.reset(new FindRequestManager(this));
7314
7315 // Concurrent find sessions must not overlap, so destroy any existing
7316 // FindRequestManagers in any inner WebContentses.
Lucas Furukawa Gadani2ec00c82018-12-14 15:53:167317 for (WebContents* contents : GetWebContentsAndAllInner()) {
7318 auto* web_contents_impl = static_cast<WebContentsImpl*>(contents);
7319 if (web_contents_impl == this)
paulmeyerfeafc2d2017-04-25 21:46:407320 continue;
Lucas Furukawa Gadani2ec00c82018-12-14 15:53:167321 if (web_contents_impl->find_request_manager_) {
7322 web_contents_impl->find_request_manager_->StopFinding(
7323 STOP_FIND_ACTION_CLEAR_SELECTION);
7324 web_contents_impl->find_request_manager_.release();
paulmeyerfeafc2d2017-04-25 21:46:407325 }
7326 }
paulmeyerc0b762b2016-04-13 11:55:177327
7328 return find_request_manager_.get();
7329}
7330
7331void WebContentsImpl::NotifyFindReply(int request_id,
7332 int number_of_matches,
7333 const gfx::Rect& selection_rect,
7334 int active_match_ordinal,
7335 bool final_update) {
Sebastien Marchandcf97ed52017-07-24 23:50:347336 if (delegate_ && !is_being_destroyed_ &&
Lukasz Anforowicze9ae3722017-09-29 17:37:387337 !GetMainFrame()->GetProcess()->FastShutdownStarted()) {
paulmeyerc0b762b2016-04-13 11:55:177338 delegate_->FindReply(this, request_id, number_of_matches, selection_rect,
7339 active_match_ordinal, final_update);
7340 }
7341}
7342
ortuno467e5792016-06-10 04:32:397343void WebContentsImpl::IncrementBluetoothConnectedDeviceCount() {
7344 // Trying to invalidate the tab state while being destroyed could result in a
7345 // use after free.
7346 if (IsBeingDestroyed()) {
7347 return;
7348 }
7349 // Notify for UI updates if the state changes.
7350 bluetooth_connected_device_count_++;
7351 if (bluetooth_connected_device_count_ == 1) {
Francois Doray66766a572019-12-17 20:56:497352 OnIsConnectedToBluetoothDeviceChanged(true);
ortuno467e5792016-06-10 04:32:397353 }
7354}
7355
7356void WebContentsImpl::DecrementBluetoothConnectedDeviceCount() {
7357 // Trying to invalidate the tab state while being destroyed could result in a
7358 // use after free.
7359 if (IsBeingDestroyed()) {
7360 return;
7361 }
7362 // Notify for UI updates if the state changes.
Lukasz Anforowicze9ae3722017-09-29 17:37:387363 DCHECK_NE(bluetooth_connected_device_count_, 0u);
ortuno467e5792016-06-10 04:32:397364 bluetooth_connected_device_count_--;
7365 if (bluetooth_connected_device_count_ == 0) {
Francois Doray66766a572019-12-17 20:56:497366 OnIsConnectedToBluetoothDeviceChanged(false);
7367 }
7368}
7369
7370void WebContentsImpl::OnIsConnectedToBluetoothDeviceChanged(
7371 bool is_connected_to_bluetooth_device) {
7372 NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB);
7373 for (auto& observer : observers_) {
7374 observer.OnIsConnectedToBluetoothDeviceChanged(
7375 is_connected_to_bluetooth_device);
ortuno467e5792016-06-10 04:32:397376 }
7377}
7378
Ovidio Henriquez76696f62020-07-08 03:06:597379void WebContentsImpl::IncrementBluetoothScanningSessionsCount() {
7380 // Trying to invalidate the tab state while being destroyed could result in a
7381 // use after free.
7382 if (IsBeingDestroyed())
7383 return;
7384
7385 // Notify for UI updates if the state changes.
7386 bluetooth_scanning_sessions_count_++;
7387 if (bluetooth_scanning_sessions_count_ == 1)
7388 NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB);
7389}
7390
7391void WebContentsImpl::DecrementBluetoothScanningSessionsCount() {
7392 // Trying to invalidate the tab state while being destroyed could result in a
7393 // use after free.
7394 if (IsBeingDestroyed())
7395 return;
7396
7397 DCHECK_NE(0u, bluetooth_scanning_sessions_count_);
7398 bluetooth_scanning_sessions_count_--;
7399 if (bluetooth_scanning_sessions_count_ == 0)
7400 NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB);
7401}
7402
Reilly Grant5e7c79b22019-04-09 17:26:207403void WebContentsImpl::IncrementSerialActiveFrameCount() {
7404 // Trying to invalidate the tab state while being destroyed could result in a
7405 // use after free.
7406 if (IsBeingDestroyed())
7407 return;
7408
7409 // Notify for UI updates if the state changes.
7410 serial_active_frame_count_++;
7411 if (serial_active_frame_count_ == 1)
7412 NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB);
7413}
7414
7415void WebContentsImpl::DecrementSerialActiveFrameCount() {
7416 // Trying to invalidate the tab state while being destroyed could result in a
7417 // use after free.
7418 if (IsBeingDestroyed())
7419 return;
7420
7421 // Notify for UI updates if the state changes.
7422 DCHECK_NE(0u, serial_active_frame_count_);
7423 serial_active_frame_count_--;
7424 if (serial_active_frame_count_ == 0)
7425 NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB);
7426}
7427
Matt Reynoldse8c6c1f2019-11-02 09:53:537428void WebContentsImpl::IncrementHidActiveFrameCount() {
7429 // Trying to invalidate the tab state while being destroyed could result in a
7430 // use after free.
7431 if (IsBeingDestroyed())
7432 return;
7433
7434 // Notify for UI updates if the active frame count transitions from zero to
7435 // non-zero.
7436 hid_active_frame_count_++;
7437 if (hid_active_frame_count_ == 1)
7438 NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB);
7439}
7440
7441void WebContentsImpl::DecrementHidActiveFrameCount() {
7442 // Trying to invalidate the tab state while being destroyed could result in a
7443 // use after free.
7444 if (IsBeingDestroyed())
7445 return;
7446
7447 // Notify for UI updates if the active frame count transitions from non-zero
7448 // to zero.
7449 DCHECK_NE(0u, hid_active_frame_count_);
7450 hid_active_frame_count_--;
7451 if (hid_active_frame_count_ == 0)
7452 NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB);
7453}
7454
Marijn Kruisselbrink29051042019-08-06 22:56:557455void WebContentsImpl::IncrementNativeFileSystemHandleCount() {
7456 // Trying to invalidate the tab state while being destroyed could result in a
7457 // use after free.
7458 if (IsBeingDestroyed())
7459 return;
7460
7461 // Notify for UI updates if the state changes. Need both TYPE_TAB and TYPE_URL
7462 // to update both the tab-level usage indicator and the usage indicator in the
7463 // omnibox.
7464 native_file_system_handle_count_++;
7465 if (native_file_system_handle_count_ == 1) {
7466 NotifyNavigationStateChanged(static_cast<content::InvalidateTypes>(
7467 INVALIDATE_TYPE_TAB | INVALIDATE_TYPE_URL));
7468 }
7469}
7470
7471void WebContentsImpl::DecrementNativeFileSystemHandleCount() {
7472 // Trying to invalidate the tab state while being destroyed could result in a
7473 // use after free.
7474 if (IsBeingDestroyed())
7475 return;
7476
7477 // Notify for UI updates if the state changes. Need both TYPE_TAB and TYPE_URL
7478 // to update both the tab-level usage indicator and the usage indicator in the
7479 // omnibox.
7480 DCHECK_NE(0u, native_file_system_handle_count_);
7481 native_file_system_handle_count_--;
7482 if (native_file_system_handle_count_ == 0) {
7483 NotifyNavigationStateChanged(static_cast<content::InvalidateTypes>(
7484 INVALIDATE_TYPE_TAB | INVALIDATE_TYPE_URL));
7485 }
7486}
7487
zqzhang8ac49002017-03-16 21:51:357488void WebContentsImpl::SetHasPersistentVideo(bool has_persistent_video) {
7489 if (has_persistent_video_ == has_persistent_video)
mlamouri5cd9ae82017-02-18 11:05:097490 return;
7491
zqzhang8ac49002017-03-16 21:51:357492 has_persistent_video_ = has_persistent_video;
mlamouri5cd9ae82017-02-18 11:05:097493 NotifyPreferencesChanged();
zqzhang8ac49002017-03-16 21:51:357494 media_web_contents_observer()->RequestPersistentVideo(has_persistent_video);
mlamouri5cd9ae82017-02-18 11:05:097495}
7496
Ian Vollick5d5c37f2019-06-19 22:50:577497void WebContentsImpl::SetSpatialNavigationDisabled(bool disabled) {
7498 if (is_spatial_navigation_disabled_ == disabled)
7499 return;
7500
7501 is_spatial_navigation_disabled_ = disabled;
7502 NotifyPreferencesChanged();
7503}
7504
paulmeyerfeafc2d2017-04-25 21:46:407505void WebContentsImpl::BrowserPluginGuestWillDetach() {
7506 WebContentsImpl* outermost = GetOutermostWebContents();
7507 if (this != outermost && ContainsOrIsFocusedWebContents())
7508 outermost->SetAsFocusedWebContentsIfNecessary();
7509}
7510
Becca Hughes112832e2019-06-11 17:19:027511PictureInPictureResult WebContentsImpl::EnterPictureInPicture(
Mounir Lamouri11e9ef42018-05-22 03:10:167512 const viz::SurfaceId& surface_id,
7513 const gfx::Size& natural_size) {
François Beaufortf09e4462019-01-18 14:34:517514 return delegate_
7515 ? delegate_->EnterPictureInPicture(this, surface_id, natural_size)
Becca Hughes112832e2019-06-11 17:19:027516 : PictureInPictureResult::kNotSupported;
Mounir Lamouri11e9ef42018-05-22 03:10:167517}
7518
Mounir Lamouri6d759e12018-05-16 20:01:307519void WebContentsImpl::ExitPictureInPicture() {
7520 if (delegate_)
7521 delegate_->ExitPictureInPicture();
7522}
7523
paulmeyerc0b762b2016-04-13 11:55:177524#if defined(OS_ANDROID)
7525void WebContentsImpl::NotifyFindMatchRectsReply(
7526 int version,
7527 const std::vector<gfx::RectF>& rects,
7528 const gfx::RectF& active_rect) {
7529 if (delegate_)
7530 delegate_->FindMatchRectsReply(this, version, rects, active_rect);
7531}
7532#endif
7533
[email protected]34ff1cfc2014-08-20 06:16:057534void WebContentsImpl::SetForceDisableOverscrollContent(bool force_disable) {
7535 force_disable_overscroll_content_ = force_disable;
7536 if (view_)
7537 view_->SetOverscrollControllerEnabled(CanOverscrollContent());
7538}
7539
Jianzhou Fengd8720c72018-01-18 03:06:307540bool WebContentsImpl::SetDeviceEmulationSize(const gfx::Size& new_size) {
7541 device_emulation_size_ = new_size;
7542 RenderWidgetHostView* rwhv = GetMainFrame()->GetView();
7543
7544 const gfx::Size current_size = rwhv->GetViewBounds().size();
7545 if (view_size_before_emulation_.IsEmpty())
7546 view_size_before_emulation_ = current_size;
7547
7548 if (current_size != new_size)
7549 rwhv->SetSize(new_size);
7550
7551 return current_size != new_size;
7552}
7553
7554void WebContentsImpl::ClearDeviceEmulationSize() {
7555 RenderWidgetHostView* rwhv = GetMainFrame()->GetView();
7556 // WebContentsView could get resized during emulation, which also resizes
7557 // RWHV. If it happens, assume user would like to keep using the size after
7558 // emulation.
7559 // TODO(jzfeng): Prohibit resizing RWHV through any other means (at least when
7560 // WebContentsView size changes).
Dmitry Gozmanc63707182018-03-06 00:09:397561 if (!view_size_before_emulation_.IsEmpty() && rwhv &&
7562 rwhv->GetViewBounds().size() == device_emulation_size_) {
Jianzhou Fengd8720c72018-01-18 03:06:307563 rwhv->SetSize(view_size_before_emulation_);
Dmitry Gozmanc63707182018-03-06 00:09:397564 }
Jianzhou Fengd8720c72018-01-18 03:06:307565 device_emulation_size_ = gfx::Size();
7566 view_size_before_emulation_ = gfx::Size();
7567}
7568
Max Morin5bc74f52018-05-09 07:00:217569ForwardingAudioStreamFactory* WebContentsImpl::GetAudioStreamFactory() {
7570 if (!audio_stream_factory_) {
7571 audio_stream_factory_.emplace(
7572 this,
Max Morinddebb972018-09-20 10:04:147573 // BrowserMainLoop::GetInstance() may be null in unit tests.
7574 BrowserMainLoop::GetInstance()
7575 ? static_cast<media::UserInputMonitorBase*>(
7576 BrowserMainLoop::GetInstance()->user_input_monitor())
7577 : nullptr,
Ken Rockot72964402019-12-06 10:40:517578 AudioStreamBrokerFactory::CreateImpl());
Max Morin5bc74f52018-05-09 07:00:217579 }
7580
7581 return &*audio_stream_factory_;
7582}
7583
dalecurtis88c240072015-12-09 02:11:187584void WebContentsImpl::MediaStartedPlaying(
billorr21c005d2016-11-17 03:57:047585 const WebContentsObserver::MediaPlayerInfo& media_info,
Mounir Lamouri0f5bdf52019-04-04 16:32:357586 const MediaPlayerId& id) {
billorr21c005d2016-11-17 03:57:047587 if (media_info.has_video)
7588 currently_playing_video_count_++;
7589
ericwilligersde386342016-10-19 02:35:097590 for (auto& observer : observers_)
billorr21c005d2016-11-17 03:57:047591 observer.MediaStartedPlaying(media_info, id);
dalecurtis88c240072015-12-09 02:11:187592}
7593
7594void WebContentsImpl::MediaStoppedPlaying(
billorr21c005d2016-11-17 03:57:047595 const WebContentsObserver::MediaPlayerInfo& media_info,
Mounir Lamouri0f5bdf52019-04-04 16:32:357596 const MediaPlayerId& id,
Becca Hughes220bfc102017-11-14 18:24:077597 WebContentsObserver::MediaStoppedReason reason) {
billorr21c005d2016-11-17 03:57:047598 if (media_info.has_video)
7599 currently_playing_video_count_--;
7600
ericwilligersde386342016-10-19 02:35:097601 for (auto& observer : observers_)
Becca Hughes220bfc102017-11-14 18:24:077602 observer.MediaStoppedPlaying(media_info, id, reason);
billorr21c005d2016-11-17 03:57:047603}
7604
Mounir Lamouri0f5bdf52019-04-04 16:32:357605void WebContentsImpl::MediaResized(const gfx::Size& size,
7606 const MediaPlayerId& id) {
peconn257951522017-06-09 18:24:597607 cached_video_sizes_[id] = size;
7608
7609 for (auto& observer : observers_)
7610 observer.MediaResized(size, id);
7611}
7612
Michael Crouse57fd8da2020-07-30 17:15:487613void WebContentsImpl::MediaBufferUnderflow(const MediaPlayerId& id) {
7614 for (auto& observer : observers_)
7615 observer.MediaBufferUnderflow(id);
7616}
7617
Peter E Conneccb34c22017-09-08 09:37:587618void WebContentsImpl::MediaEffectivelyFullscreenChanged(bool is_fullscreen) {
7619 for (auto& observer : observers_)
7620 observer.MediaEffectivelyFullscreenChanged(is_fullscreen);
7621}
7622
Peter Conn31c726a2017-08-17 11:07:347623base::Optional<gfx::Size> WebContentsImpl::GetFullscreenVideoSize() {
Mounir Lamouri0f5bdf52019-04-04 16:32:357624 base::Optional<MediaPlayerId> id =
Peter Conn31c726a2017-08-17 11:07:347625 media_web_contents_observer_->GetFullscreenVideoMediaPlayerId();
Jan Wilken Dörrie531be7ca2019-06-07 10:05:577626 if (id && base::Contains(cached_video_sizes_, id.value()))
Peter Conn31c726a2017-08-17 11:07:347627 return base::Optional<gfx::Size>(cached_video_sizes_[id.value()]);
Lei Zhang6410ec1a2019-01-31 21:38:587628 return base::nullopt;
peconn257951522017-06-09 18:24:597629}
7630
billorr21c005d2016-11-17 03:57:047631int WebContentsImpl::GetCurrentlyPlayingVideoCount() {
7632 return currently_playing_video_count_;
dalecurtis88c240072015-12-09 02:11:187633}
7634
Raymond Toye413650c2018-08-31 00:12:477635void WebContentsImpl::AudioContextPlaybackStarted(RenderFrameHost* host,
7636 int context_id) {
7637 WebContentsObserver::AudioContextId audio_context_id(host, context_id);
7638 for (auto& observer : observers_)
7639 observer.AudioContextPlaybackStarted(audio_context_id);
7640}
7641
7642void WebContentsImpl::AudioContextPlaybackStopped(RenderFrameHost* host,
7643 int context_id) {
7644 WebContentsObserver::AudioContextId audio_context_id(host, context_id);
7645 for (auto& observer : observers_)
7646 observer.AudioContextPlaybackStopped(audio_context_id);
7647}
7648
Chris Hamilton16ca64a2020-05-14 23:13:177649void WebContentsImpl::OnFrameAudioStateChanged(RenderFrameHost* host,
7650 bool is_audible) {
7651 for (auto& observer : observers_)
7652 observer.OnFrameAudioStateChanged(host, is_audible);
7653}
7654
evliu7f736f02019-11-07 17:43:507655media::MediaMetricsProvider::RecordAggregateWatchTimeCallback
7656WebContentsImpl::GetRecordAggregateWatchTimeCallback() {
Becca Hughes53652442020-03-17 15:35:427657 if (!delegate_ || !delegate_->GetDelegateWeakPtr())
7658 return base::DoNothing();
7659
evliu7f736f02019-11-07 17:43:507660 return base::BindRepeating(
Becca Hughes53652442020-03-17 15:35:427661 [](base::WeakPtr<WebContentsDelegate> delegate, GURL last_committed_url,
7662 base::TimeDelta total_watch_time, base::TimeDelta time_stamp,
7663 bool has_video, bool has_audio) {
evliua283c9e2019-11-08 22:57:227664 content::MediaPlayerWatchTime watch_time(
7665 last_committed_url, last_committed_url.GetOrigin(),
7666 total_watch_time, time_stamp, has_video, has_audio);
evliu7f736f02019-11-07 17:43:507667
7668 // Save the watch time if the delegate is still alive.
7669 if (delegate)
7670 delegate->MediaWatchTimeChanged(watch_time);
7671 },
Becca Hughes53652442020-03-17 15:35:427672 delegate_->GetDelegateWeakPtr(), GetMainFrameLastCommittedURL());
evliu7f736f02019-11-07 17:43:507673}
7674
Ehsan Karamad1c631762019-05-13 23:04:017675RenderFrameHostImpl* WebContentsImpl::GetMainFrameForInnerDelegate(
7676 FrameTreeNode* frame_tree_node) {
7677 if (auto* web_contents = node_.GetInnerWebContentsInFrame(frame_tree_node))
7678 return web_contents->GetMainFrame();
7679 return nullptr;
7680}
7681
John Budorickee1c9512019-10-17 15:32:197682bool WebContentsImpl::IsFrameLowPriority(
7683 const RenderFrameHost* render_frame_host) {
7684 if (!delegate_)
7685 return false;
7686 return delegate_->IsFrameLowPriority(this, render_frame_host);
7687}
7688
Roger Tawa03ffdbd2020-01-16 18:12:547689void WebContentsImpl::IsClipboardPasteAllowed(
7690 const GURL& url,
7691 const ui::ClipboardFormatType& data_type,
7692 const std::string& data,
7693 IsClipboardPasteAllowedCallback callback) {
Dominique Fauteux-Chapleau99244382020-07-08 20:37:007694 ++suppress_unresponsive_renderer_count_;
Roger Tawa03ffdbd2020-01-16 18:12:547695 GetContentClient()->browser()->IsClipboardPasteAllowed(
Dominique Fauteux-Chapleau99244382020-07-08 20:37:007696 this, url, data_type, data,
7697 base::BindOnce(&WebContentsImpl::IsClipboardPasteAllowedWrapperCallback,
7698 weak_factory_.GetWeakPtr(), std::move(callback)));
7699}
7700
7701void WebContentsImpl::IsClipboardPasteAllowedWrapperCallback(
7702 IsClipboardPasteAllowedCallback callback,
7703 ClipboardPasteAllowed allowed) {
7704 std::move(callback).Run(allowed);
7705 --suppress_unresponsive_renderer_count_;
Roger Tawa03ffdbd2020-01-16 18:12:547706}
7707
Lan Weif81c6e7c2020-02-12 16:46:097708bool WebContentsImpl::HasSeenRecentScreenOrientationChange() {
7709 static constexpr base::TimeDelta kMaxInterval =
7710 base::TimeDelta::FromSeconds(1);
7711 base::TimeDelta delta =
7712 ui::EventTimeForNow() - last_screen_orientation_change_time_;
7713 // Return whether there is a screen orientation change happened in the recent
7714 // 1 second.
7715 return delta <= kMaxInterval;
7716}
7717
7718void WebContentsImpl::DidChangeScreenOrientation() {
7719 last_screen_orientation_change_time_ = ui::EventTimeForNow();
7720}
7721
Miyoung Shin8f2e1832020-04-27 09:00:027722bool WebContentsImpl::ShowPopupMenu(
Miyoung Shin4d947d22020-04-24 04:48:337723 RenderFrameHostImpl* render_frame_host,
Miyoung Shin8f2e1832020-04-27 09:00:027724 mojo::PendingRemote<blink::mojom::PopupMenuClient>* popup_client,
Miyoung Shin4d947d22020-04-24 04:48:337725 const gfx::Rect& bounds,
7726 int32_t item_height,
7727 double font_size,
7728 int32_t selected_item,
7729 std::vector<blink::mojom::MenuItemPtr>* menu_items,
7730 bool right_aligned,
7731 bool allow_multiple_selection) {
7732 for (auto& observer : observers_) {
Miyoung Shin8f2e1832020-04-27 09:00:027733 if (observer.ShowPopupMenu(render_frame_host, popup_client, bounds,
7734 item_height, font_size, selected_item,
7735 menu_items, right_aligned,
7736 allow_multiple_selection)) {
Miyoung Shin4d947d22020-04-24 04:48:337737 return true;
7738 }
7739 }
7740 return false;
7741}
7742
Francois Doraye6161152018-03-27 22:05:377743void WebContentsImpl::UpdateWebContentsVisibility(Visibility visibility) {
Francois Doray3f2afbd2018-04-06 19:18:187744 // Occlusion is disabled when |features::kWebContentsOcclusion| is disabled
7745 // (for power and speed impact assessment) or when
7746 // |switches::kDisableBackgroundingOccludedWindowsForTesting| is specified on
7747 // the command line (to avoid flakiness in browser tests).
7748 const bool occlusion_is_disabled =
7749 !base::FeatureList::IsEnabled(features::kWebContentsOcclusion) ||
Francois Doraye6161152018-03-27 22:05:377750 base::CommandLine::ForCurrentProcess()->HasSwitch(
7751 switches::kDisableBackgroundingOccludedWindowsForTesting);
7752 if (occlusion_is_disabled && visibility == Visibility::OCCLUDED)
7753 visibility = Visibility::VISIBLE;
7754
tapted65ff2ea72016-03-01 23:39:007755 if (!did_first_set_visible_) {
Francois Doraye6161152018-03-27 22:05:377756 if (visibility == Visibility::VISIBLE) {
7757 // A WebContents created with CreateParams::initially_hidden = false
7758 // starts with GetVisibility() == Visibility::VISIBLE even though it is
7759 // not really visible. Call WasShown() when it becomes visible for real as
7760 // the page load mechanism and some WebContentsObserver rely on that.
tapted65ff2ea72016-03-01 23:39:007761 WasShown();
Francois Dorayfe4a1772018-02-17 04:17:097762 did_first_set_visible_ = true;
tapted65ff2ea72016-03-01 23:39:007763 }
Francois Doraye6161152018-03-27 22:05:377764
7765 // Trust the initial visibility of the WebContents and do not switch it to
7766 // HIDDEN or OCCLUDED before it becomes VISIBLE for real. Doing so would
7767 // result in destroying resources that would immediately be recreated (e.g.
7768 // UpdateWebContents(HIDDEN) can be called when a WebContents is added to a
7769 // hidden window that is about to be shown).
7770
tapted65ff2ea72016-03-01 23:39:007771 return;
7772 }
Francois Doraye6161152018-03-27 22:05:377773
7774 if (visibility == visibility_)
tapted65ff2ea72016-03-01 23:39:007775 return;
7776
Collin Baker989e0882019-11-01 01:27:177777 UpdateVisibilityAndNotifyPageAndView(visibility);
tapted65ff2ea72016-03-01 23:39:007778}
7779
aelias5252baa2016-04-10 01:18:027780void WebContentsImpl::UpdateOverridingUserAgent() {
mlamouri5cd9ae82017-02-18 11:05:097781 NotifyPreferencesChanged();
aelias5252baa2016-04-10 01:18:027782}
7783
avid53461d2016-02-25 17:15:047784void WebContentsImpl::SetJavaScriptDialogManagerForTesting(
7785 JavaScriptDialogManager* dialog_manager) {
7786 dialog_manager_ = dialog_manager;
7787}
7788
Julie Jeongeun Kim7d0986172019-12-04 03:17:257789void WebContentsImpl::RemoveReceiverSet(const std::string& interface_name) {
7790 auto it = receiver_sets_.find(interface_name);
7791 if (it != receiver_sets_.end())
7792 receiver_sets_.erase(it);
rockotf62002a2016-09-15 00:08:597793}
7794
nzolghadrd87a308d2016-12-07 15:45:567795bool WebContentsImpl::AddDomainInfoToRapporSample(rappor::Sample* sample) {
nzolghadrbbffc6c2017-05-25 17:56:397796 // Here we associate this metric to the main frame URL regardless of what
7797 // caused it.
nzolghadrd87a308d2016-12-07 15:45:567798 sample->SetStringField("Domain", ::rappor::GetDomainAndRegistrySampleFromGURL(
7799 GetLastCommittedURL()));
nzolghadrd87a308d2016-12-07 15:45:567800 return true;
7801}
7802
falken52a56e32016-12-08 05:02:407803void WebContentsImpl::ShowInsecureLocalhostWarningIfNeeded() {
7804 bool allow_localhost = base::CommandLine::ForCurrentProcess()->HasSwitch(
7805 switches::kAllowInsecureLocalhost);
7806 if (!allow_localhost)
7807 return;
7808
7809 content::NavigationEntry* entry = GetController().GetLastCommittedEntry();
Rob Wuf79b3ba2018-01-14 01:54:317810 if (!entry || !net::IsLocalhost(entry->GetURL()))
falken52a56e32016-12-08 05:02:407811 return;
7812
7813 content::SSLStatus ssl_status = entry->GetSSL();
Matt Mueller936511442019-09-03 18:15:127814 if (!net::IsCertStatusError(ssl_status.cert_status))
falken52a56e32016-12-08 05:02:407815 return;
7816
7817 GetMainFrame()->AddMessageToConsole(
Abhijeet Kandalkara5928a0a2019-03-13 05:04:427818 blink::mojom::ConsoleMessageLevel::kWarning,
falken52a56e32016-12-08 05:02:407819 base::StringPrintf("This site does not have a valid SSL "
7820 "certificate! Without SSL, your site's and "
7821 "visitors' data is vulnerable to theft and "
7822 "tampering. Get a valid SSL certificate before"
7823 " releasing your website to the public."));
7824}
7825
ekaramadf6750aa2017-06-06 18:29:427826bool WebContentsImpl::IsShowingContextMenuOnPage() const {
7827 return showing_context_menu_;
7828}
7829
Min Qina904f3302018-02-13 23:33:347830download::DownloadUrlParameters::RequestHeadersType
7831WebContentsImpl::ParseDownloadHeaders(const std::string& headers) {
7832 download::DownloadUrlParameters::RequestHeadersType request_headers;
Megan Jablonski2f6a4c52017-07-10 23:01:257833 for (const base::StringPiece& key_value : base::SplitStringPiece(
7834 headers, "\r\n", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY)) {
7835 std::vector<std::string> pair = base::SplitString(
7836 key_value, ":", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
7837 if (2ul == pair.size())
7838 request_headers.push_back(make_pair(pair[0], pair[1]));
7839 }
7840 return request_headers;
7841}
7842
lukasza6f8ac622017-06-06 03:10:207843void WebContentsImpl::SetOpenerForNewContents(FrameTreeNode* opener,
7844 bool opener_suppressed) {
7845 if (opener) {
7846 FrameTreeNode* new_root = GetFrameTree()->root();
7847
7848 // For the "original opener", track the opener's main frame instead, because
7849 // if the opener is a subframe, the opener tracking could be easily bypassed
7850 // by spawning from a subframe and deleting the subframe.
7851 // https://crbug.com/705316
7852 new_root->SetOriginalOpener(opener->frame_tree()->root());
7853
7854 if (!opener_suppressed) {
7855 new_root->SetOpener(opener);
7856 created_with_opener_ = true;
7857 }
7858 }
7859}
7860
Mounir Lamouri0f5bdf52019-04-04 16:32:357861void WebContentsImpl::MediaMutedStatusChanged(const MediaPlayerId& id,
7862 bool muted) {
Becca Hughes9f6fd4b82017-06-15 10:01:407863 for (auto& observer : observers_)
7864 observer.MediaMutedStatusChanged(id, muted);
7865}
7866
EhsanK2075c7e2017-08-21 02:42:397867void WebContentsImpl::SetVisibilityForChildViews(bool visible) {
7868 GetMainFrame()->SetVisibilityForChildViews(visible);
7869}
7870
Alison Maher7f366dc62020-03-03 19:46:007871void WebContentsImpl::OnNativeThemeUpdated(ui::NativeTheme* observed_theme) {
7872 DCHECK(native_theme_observer_.IsObserving(observed_theme));
7873
7874 bool using_dark_colors = observed_theme->ShouldUseDarkColors();
7875 ui::NativeTheme::PreferredColorScheme preferred_color_scheme =
7876 observed_theme->GetPreferredColorScheme();
7877 bool preferences_changed = false;
7878
7879 if (using_dark_colors_ != using_dark_colors) {
7880 using_dark_colors_ = using_dark_colors;
7881 preferences_changed = true;
7882 }
7883 if (preferred_color_scheme_ != preferred_color_scheme) {
7884 preferred_color_scheme_ = preferred_color_scheme;
7885 preferences_changed = true;
7886 }
7887
7888 if (preferences_changed)
7889 NotifyPreferencesChanged();
7890}
7891
Dave Tapuska58a099e2020-06-08 21:48:407892blink::mojom::FrameWidgetInputHandler*
7893WebContentsImpl::GetFocusedFrameWidgetInputHandler() {
7894 auto* focused_render_widget_host =
7895 GetFocusedRenderWidgetHost(GetMainFrame()->GetRenderWidgetHost());
7896 if (!focused_render_widget_host)
Dave Tapuskae782bea2019-07-09 16:21:517897 return nullptr;
Dave Tapuska58a099e2020-06-08 21:48:407898 return focused_render_widget_host->GetFrameWidgetInputHandler();
Dave Tapuskae782bea2019-07-09 16:21:517899}
7900
Alexander Timine3ec4192020-04-20 16:39:407901ukm::SourceId WebContentsImpl::GetCurrentPageUkmSourceId() {
7902 return GetMainFrame()->GetPageUkmSourceId();
7903}
7904
Yuzu Saijo232076f2020-05-29 07:14:027905std::set<RenderViewHostImpl*>
7906WebContentsImpl::GetRenderViewHostsIncludingBackForwardCached() {
7907 std::set<RenderViewHostImpl*> render_view_hosts;
7908
7909 // Add RenderViewHostImpls outside of BackForwardCache.
7910 for (auto& render_view_host : frame_tree_.render_view_hosts()) {
7911 render_view_hosts.insert(render_view_host.second);
7912 }
7913
7914 // Add RenderViewHostImpls in BackForwardCache.
7915 const auto& entries = GetController().GetBackForwardCache().GetEntries();
7916 for (const auto& entry : entries) {
7917 std::set<RenderViewHostImpl*> bfcached_hosts = entry->render_view_hosts;
7918 render_view_hosts.insert(bfcached_hosts.begin(), bfcached_hosts.end());
7919 }
7920
7921 return render_view_hosts;
7922}
7923
Sreeja Kamishettybe0ccd6b2020-06-09 11:52:507924void WebContentsImpl::RenderFrameHostStateChanged(
7925 RenderFrameHost* render_frame_host,
7926 LifecycleState old_state,
7927 LifecycleState new_state) {
7928 if (render_frame_host->GetParent())
7929 return;
7930
7931 if (old_state == LifecycleState::kActive &&
7932 new_state != LifecycleState::kActive) {
7933 // TODO(sreejakshetty): Remove this reset when ColorChooser becomes
7934 // per-frame.
7935 // Close the color chooser popup when RenderFrameHost changes state from
7936 // kActive.
7937 color_chooser_.reset();
7938 }
7939}
7940
[email protected]8ff00d72012-10-23 19:12:217941} // namespace content