blob: 13fd890e671e2055674a539ddb519bf5c180740e [file] [log] [blame]
[email protected]91854cd2012-01-10 19:43:571// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]93ddb3c2012-04-11 21:44:295#include "content/browser/web_contents/web_contents_impl.h"
[email protected]39526562011-02-05 03:41:516
avib7348942015-12-25 20:57:107#include <stddef.h>
8
w.shackleton49bcd392016-01-06 17:38:229#include <cmath>
[email protected]2bb171882012-03-07 02:09:4610#include <utility>
rkaplowe56a52032017-02-07 00:14:5411#include <vector>
[email protected]b75b8292010-10-01 07:28:2512
Sebastien Marchandf8cbfab2019-01-25 16:02:3013#include "base/bind.h"
[email protected]36fb2c7c2011-04-04 15:49:0814#include "base/command_line.h"
creisfafed882016-07-29 00:03:0915#include "base/debug/dump_without_crashing.h"
zqzhang181047e62016-07-01 13:37:1716#include "base/feature_list.h"
Marijn Kruisselbrink46fa4bd2019-07-11 18:15:4717#include "base/files/file_path.h"
[email protected]cbb1ef592013-06-05 19:49:4618#include "base/lazy_instance.h"
skyostil95082a62015-06-05 19:53:0719#include "base/location.h"
[email protected]6d636e62013-07-31 03:15:5620#include "base/logging.h"
avib7348942015-12-25 20:57:1021#include "base/macros.h"
fdorayba121422016-12-23 19:51:4822#include "base/memory/ptr_util.h"
dmazzonid95ae842016-04-12 21:17:3923#include "base/memory/ref_counted.h"
asvitkine8d51e9d2016-09-02 23:55:4324#include "base/metrics/histogram_macros.h"
bratell0a7406f2017-03-28 07:46:3725#include "base/metrics/user_metrics.h"
Avi Drissman4891b852018-09-25 18:12:5826#include "base/no_destructor.h"
Carlos Caballero1215f882019-10-29 15:58:4327#include "base/optional.h"
[email protected]04cbd3d2013-12-04 04:58:2028#include "base/process/process.h"
skyostil95082a62015-06-05 19:53:0729#include "base/single_thread_task_runner.h"
Arthur Sonzogni929d29f2018-09-17 14:19:4430#include "base/stl_util.h"
[email protected]348fbaac2013-06-11 06:31:5131#include "base/strings/string16.h"
32#include "base/strings/string_number_conversions.h"
Avi Drissman4891b852018-09-25 18:12:5833#include "base/strings/string_split.h"
[email protected]348fbaac2013-06-11 06:31:5134#include "base/strings/string_util.h"
[email protected]74ebfb12013-06-07 20:48:0035#include "base/strings/utf_string_conversions.h"
Eric Seckler8652dcd52018-09-20 10:42:2836#include "base/task/post_task.h"
gab30f26df2016-05-11 19:37:5537#include "base/threading/thread_task_runner_handle.h"
[email protected]a43858f2013-06-28 15:18:3738#include "base/time/time.h"
ssid3e765612015-01-28 04:03:4239#include "base/trace_event/trace_event.h"
avib7348942015-12-25 20:57:1040#include "build/build_config.h"
Min Qinda0ed2062018-02-23 22:00:5341#include "components/download/public/common/download_stats.h"
nzolghadrd87a308d2016-12-07 15:45:5642#include "components/rappor/public/rappor_utils.h"
rsleevi24f64dc22015-08-07 21:39:2143#include "components/url_formatter/url_formatter.h"
Abigail Kleinaa898742019-11-01 02:31:2544#include "content/browser/accessibility/accessibility_event_recorder.h"
James Wallace-Leeeafc94cb92018-07-23 21:35:0945#include "content/browser/accessibility/accessibility_tree_formatter_blink.h"
jamescookda2505812015-03-20 18:01:1846#include "content/browser/bad_message.h"
Max Morinddebb972018-09-20 10:04:1447#include "content/browser/browser_main_loop.h"
[email protected]7a846df2012-09-20 19:17:3948#include "content/browser/browser_plugin/browser_plugin_embedder.h"
49#include "content/browser/browser_plugin/browser_plugin_guest.h"
[email protected]b9535422012-02-09 01:47:5950#include "content/browser/child_process_security_policy_impl.h"
Andrey Lushnikov81b992e2019-06-27 23:27:2151#include "content/browser/devtools/devtools_instrumentation.h"
Pavel Feldman3c1842b2017-08-02 05:00:1652#include "content/browser/devtools/protocol/page_handler.h"
dgozmanf00c4f72016-10-19 17:45:1553#include "content/browser/devtools/render_frame_devtools_agent_host.h"
Becca Hughes3b5a43482018-07-17 22:31:5554#include "content/browser/display_cutout/display_cutout_host_impl.h"
[email protected]5f2aa722013-08-07 16:59:4155#include "content/browser/dom_storage/dom_storage_context_wrapper.h"
[email protected]1ea3c792012-04-17 01:25:0456#include "content/browser/dom_storage/session_storage_namespace_impl.h"
[email protected]aa4f3972012-03-01 18:12:1257#include "content/browser/download/mhtml_generation_manager.h"
[email protected]a53209b2012-01-20 16:48:1658#include "content/browser/download/save_package.h"
paulmeyerc0b762b2016-04-13 11:55:1759#include "content/browser/find_request_manager.h"
[email protected]bffc8302014-01-23 20:52:1660#include "content/browser/frame_host/cross_process_frame_connector.h"
ananta4b7467a52016-09-23 01:42:3861#include "content/browser/frame_host/frame_tree_node.h"
[email protected]d4a8ca482013-10-30 21:06:4062#include "content/browser/frame_host/interstitial_page_impl.h"
63#include "content/browser/frame_host/navigation_entry_impl.h"
Arthur Hemeryc3380172018-01-22 14:00:1764#include "content/browser/frame_host/navigation_request.h"
[email protected]6ea6bdf2013-12-06 13:35:0165#include "content/browser/frame_host/navigator_impl.h"
[email protected]52913802013-12-10 05:52:1866#include "content/browser/frame_host/render_frame_host_impl.h"
lfgdb5c4ed2016-03-04 23:09:0767#include "content/browser/frame_host/render_frame_proxy_host.h"
Kenneth Russell954ed9ce2018-04-12 23:07:0168#include "content/browser/gpu/gpu_data_manager_impl.h"
mlamouriefdca9d2014-09-16 16:55:4069#include "content/browser/manifest/manifest_manager_host.h"
Max Morin5bc74f52018-05-09 07:00:2170#include "content/browser/media/audio_stream_broker.h"
dalecurtisbc6572e12014-09-12 19:22:3071#include "content/browser/media/audio_stream_monitor.h"
miu50f97892014-09-22 22:49:5272#include "content/browser/media/capture/web_contents_audio_muter.h"
dalecurtis88c240072015-12-09 02:11:1873#include "content/browser/media/media_web_contents_observer.h"
zqzhang1adf3cb2016-11-01 11:26:4574#include "content/browser/media/session/media_session_impl.h"
tommyclieb25b2a2014-11-03 19:45:0975#include "content/browser/plugin_content_origin_whitelist.h"
[email protected]f3b1a082011-11-18 00:34:3076#include "content/browser/renderer_host/render_process_host_impl.h"
[email protected]21161032014-05-05 16:56:5077#include "content/browser/renderer_host/render_view_host_delegate_view.h"
[email protected]b3c41c0b2012-03-06 15:48:3278#include "content/browser/renderer_host/render_view_host_impl.h"
[email protected]bafe6cd2012-05-23 23:09:5079#include "content/browser/renderer_host/render_widget_host_impl.h"
kenrb2a565f82015-09-02 20:24:5980#include "content/browser/renderer_host/render_widget_host_input_event_router.h"
[email protected]cfd80b02014-05-01 17:46:4881#include "content/browser/renderer_host/render_widget_host_view_base.h"
Ken Buchanandaef006b2017-08-17 18:32:1582#include "content/browser/renderer_host/render_widget_host_view_child_frame.h"
ekaramadadd882292016-06-08 15:22:5683#include "content/browser/renderer_host/text_input_manager.h"
leon.han552e9de2017-02-09 14:37:3084#include "content/browser/screen_orientation/screen_orientation_provider.h"
[email protected]b6583592012-01-25 19:52:3385#include "content/browser/site_instance_impl.h"
Charlie Reis96077362019-07-04 00:34:0486#include "content/browser/web_contents/javascript_dialog_navigation_deferrer.h"
lfgc740d4b22016-04-15 16:45:3387#include "content/browser/web_contents/web_contents_view_child_frame.h"
[email protected]b5a40842012-11-28 15:26:1188#include "content/browser/web_contents/web_contents_view_guest.h"
[email protected]86a0a6e2013-01-28 06:33:0389#include "content/browser/webui/web_ui_controller_factory_registry.h"
[email protected]d2353452012-01-19 19:53:5690#include "content/browser/webui/web_ui_impl.h"
[email protected]8eb04562013-03-06 03:41:1491#include "content/common/browser_plugin/browser_plugin_constants.h"
[email protected]703dd662013-03-05 07:37:4292#include "content/common/browser_plugin/browser_plugin_messages.h"
Paul Meyer0c58c3712016-11-17 22:59:5193#include "content/common/drag_messages.h"
[email protected]f114fa42013-12-06 17:06:4494#include "content/common/frame_messages.h"
[email protected]1f3fc1d2014-04-03 14:50:1795#include "content/common/input_messages.h"
lfgdb5c4ed2016-03-04 23:09:0796#include "content/common/page_messages.h"
nasko34634672016-07-29 18:46:0697#include "content/common/page_state_serialization.h"
rockot5c478a72016-09-28 23:14:1898#include "content/common/render_message_filter.mojom.h"
[email protected]2c5569662011-03-22 20:45:0299#include "content/common/view_messages.h"
Albert J. Wong3c93c182018-09-27 17:29:43100#include "content/common/widget_messages.h"
Abigail Kleinabb428332019-09-13 18:26:21101#include "content/public/browser/accessibility_tree_formatter.h"
[email protected]31a71eaf2014-03-13 01:47:36102#include "content/public/browser/ax_event_notification_details.h"
[email protected]ccb797302011-12-15 16:55:11103#include "content/public/browser/browser_context.h"
[email protected]139355f2014-05-11 14:21:28104#include "content/public/browser/browser_plugin_guest_manager.h"
Eric Seckler8652dcd52018-09-20 10:42:28105#include "content/public/browser/browser_task_traits.h"
Gabriel Charette790754c2018-03-16 21:32:59106#include "content/public/browser/browser_thread.h"
[email protected]87f3c082011-10-19 18:07:44107#include "content/public/browser/content_browser_client.h"
[email protected]e582fdd2011-12-20 16:48:17108#include "content/public/browser/download_manager.h"
Kent Tamura512a27e2018-10-04 00:49:32109#include "content/public/browser/file_select_listener.h"
ekaramada110f642016-12-21 19:47:28110#include "content/public/browser/focused_node_details.h"
ekaramad1beabecc2016-09-23 20:18:17111#include "content/public/browser/guest_mode.h"
[email protected]d9083482012-01-06 00:38:46112#include "content/public/browser/invalidate_type.h"
[email protected]71a88bb2013-02-01 22:05:15113#include "content/public/browser/javascript_dialog_manager.h"
skyf65d9bb2017-03-24 02:26:39114#include "content/public/browser/keyboard_event_processing_result.h"
[email protected]09d31d52012-03-11 22:30:27115#include "content/public/browser/load_notification_details.h"
[email protected]5b96836f2011-12-22 07:39:00116#include "content/public/browser/navigation_details.h"
[email protected]375fa1b2012-05-22 22:05:37117#include "content/public/browser/notification_details.h"
[email protected]be2510c02012-05-28 14:52:14118#include "content/public/browser/notification_service.h"
scottmg011d96e32016-06-29 19:15:08119#include "content/public/browser/notification_types.h"
[email protected]95640212014-07-26 18:14:30120#include "content/public/browser/render_widget_host_iterator.h"
Lukasz Anforowicze1b954d92017-10-30 21:28:06121#include "content/public/browser/restore_type.h"
estark5ea80e52015-06-19 18:43:12122#include "content/public/browser/security_style_explanations.h"
falken52a56e32016-12-08 05:02:40123#include "content/public/browser/ssl_status.h"
[email protected]4c3a23582012-08-18 08:54:34124#include "content/public/browser/storage_partition.h"
Ken Rockotfd03e682019-06-20 21:13:54125#include "content/public/browser/system_connector.h"
rockotf62002a2016-09-15 00:08:59126#include "content/public/browser/web_contents_binding_set.h"
[email protected]674bc592011-12-20 23:00:42127#include "content/public/browser/web_contents_delegate.h"
Lei Zhangee3b63ad2018-06-06 23:25:31128#include "content/public/browser/web_ui_controller.h"
[email protected]e091df82011-10-11 18:13:21129#include "content/public/common/bindings_policy.h"
alexmosc9e76ec2016-05-16 22:59:37130#include "content/public/common/child_process_host.h"
Hans Wennborg5ffd1392019-10-16 11:00:02131#include "content/public/common/content_client.h"
[email protected]54087fe2011-10-28 22:02:48132#include "content/public/common/content_constants.h"
Francois Doray3f2afbd2018-04-06 19:18:18133#include "content/public/common/content_features.h"
[email protected]64d69de42012-02-06 00:19:54134#include "content/public/common/content_switches.h"
Lukasz Anforowicze1b954d92017-10-30 21:28:06135#include "content/public/common/page_state.h"
Leon Han963dc182018-11-06 05:41:48136#include "content/public/common/referrer_type_converters.h"
[email protected]04cbd3d2013-12-04 04:58:20137#include "content/public/common/result_codes.h"
[email protected]f16cc6782013-12-16 23:42:57138#include "content/public/common/url_utils.h"
danakjffc8baf2019-10-10 23:49:42139#include "content/public/common/use_zoom_for_dsf_policy.h"
[email protected]b3a97b52014-07-09 06:25:05140#include "content/public/common/web_preferences.h"
Max Morinddebb972018-09-20 10:04:14141#include "media/base/user_input_monitor.h"
wjmaclean64951902016-04-29 20:59:12142#include "net/base/url_util.h"
[email protected]8bfc8272013-09-09 20:10:53143#include "net/http/http_cache.h"
144#include "net/http/http_transaction_factory.h"
rhalavati0fbaeef32017-06-01 15:56:30145#include "net/traffic_annotation/network_traffic_annotation.h"
[email protected]8bfc8272013-09-09 20:10:53146#include "net/url_request/url_request_context.h"
[email protected]abe2c032011-03-31 18:49:34147#include "net/url_request/url_request_context_getter.h"
Scott Violet02e38b92018-03-27 23:42:14148#include "ppapi/buildflags/buildflags.h"
Ke He31d0bb02018-02-24 07:16:24149#include "services/device/public/mojom/constants.mojom.h"
Mounir Lamourifd9dcefa2017-07-06 10:26:55150#include "services/metrics/public/cpp/ukm_recorder.h"
John Abd-El-Malekb175a612018-12-03 20:11:58151#include "services/network/public/cpp/features.h"
Ke He7319dbe2017-11-09 05:54:44152#include "services/service_manager/public/cpp/connector.h"
rockot734fb662016-10-15 16:41:30153#include "services/service_manager/public/cpp/interface_provider.h"
Becca Hughes6daf5662018-06-27 16:50:54154#include "third_party/blink/public/common/associated_interfaces/associated_interface_provider.h"
Blink Reformata30d4232018-04-07 15:31:06155#include "third_party/blink/public/common/frame/sandbox_flags.h"
156#include "third_party/blink/public/common/mime_util/mime_util.h"
danakj938b37a62019-09-24 18:35:54157#include "third_party/blink/public/common/page/page_zoom.h"
Sergio Villar Senin9d9d4f52019-10-15 10:57:57158#include "third_party/blink/public/common/security/security_style.h"
Dave Tapuskaa4189512019-10-15 20:27:34159#include "third_party/blink/public/mojom/frame/fullscreen.mojom.h"
Takuto Ikutaaa3b796c2019-02-06 02:54:56160#include "third_party/blink/public/mojom/loader/pause_subresource_loading_handle.mojom.h"
Antonio Gomes0d42960a2019-06-05 12:35:51161#include "third_party/blink/public/mojom/mediastream/media_stream.mojom-shared.h"
halton.huoca2eabd2015-07-06 08:17:40162#include "third_party/skia/include/core/SkBitmap.h"
dmazzonid95ae842016-04-12 21:17:39163#include "ui/accessibility/ax_tree_combiner.h"
[email protected]a08412b62012-05-29 21:28:56164#include "ui/base/layout.h"
Daniel Cheng90196c82018-04-25 21:49:14165#include "ui/events/base_event_utils.h"
chongz4a9751942016-08-17 17:49:39166#include "ui/events/blink/web_input_event_traits.h"
[email protected]66bd5512012-08-01 02:02:52167#include "ui/gl/gl_switches.h"
[email protected]3c733bde2010-12-21 19:56:31168
ekaramada110f642016-12-21 19:47:28169#if defined(OS_WIN)
170#include "content/browser/renderer_host/dip_util.h"
171#include "ui/gfx/geometry/dip_util.h"
172#endif
173
[email protected]583418cc2013-01-17 14:01:10174#if defined(OS_ANDROID)
mfomitchev2b8b066a2016-01-28 19:23:15175#include "content/browser/android/date_time_chooser_android.h"
sammcf5f1b0f2016-09-20 23:05:11176#include "content/browser/android/java_interfaces_impl.h"
Leon Hanccb985f2019-08-26 02:43:24177#include "content/browser/android/nfc_host.h"
[email protected]155c7f22013-12-09 17:07:18178#include "content/browser/web_contents/web_contents_android.h"
Ke He31d0bb02018-02-24 07:16:24179#include "services/device/public/mojom/nfc.mojom.h"
mcnee336ea2c2017-05-23 22:50:59180#else // !OS_ANDROID
181#include "content/browser/host_zoom_map_impl.h"
mfomitchev2b8b066a2016-01-28 19:23:15182#endif // OS_ANDROID
[email protected]583418cc2013-01-17 14:01:10183
brettw4b461082016-11-19 18:55:16184#if BUILDFLAG(ENABLE_PLUGINS)
zqzhang181047e62016-07-01 13:37:17185#include "content/browser/media/session/pepper_playback_observer.h"
Lei Zhangebcc6302018-01-12 19:46:45186#endif
zqzhang181047e62016-07-01 13:37:17187
[email protected]8ff00d72012-10-23 19:12:21188namespace content {
Abigail Kleinaa898742019-11-01 02:31:25189
190using AccessibilityEventCallback =
191 base::RepeatingCallback<void(const std::string&)>;
192
[email protected]420ae012009-04-24 05:16:32193namespace {
194
[email protected]960b0372014-05-19 18:01:00195const int kMinimumDelayBetweenLoadingUpdatesMS = 100;
[email protected]82114f52012-03-20 22:53:41196const char kDotGoogleDotCom[] = ".google.com";
[email protected]ca406032011-07-19 21:53:05197
Avi Drissmana9114822018-03-23 18:25:55198base::LazyInstance<std::vector<
199 WebContentsImpl::FriendWrapper::CreatedCallback>>::DestructorAtExit
200 g_created_callbacks = LAZY_INSTANCE_INITIALIZER;
[email protected]cbb1ef592013-06-05 19:49:46201
dcheng57e39e22016-01-21 00:25:38202bool HasMatchingProcess(FrameTree* tree, int render_process_id) {
203 for (FrameTreeNode* node : tree->Nodes()) {
204 if (node->current_frame_host()->GetProcess()->GetID() == render_process_id)
205 return true;
alexmos4cf2aa32015-07-15 23:40:43206 }
dcheng57e39e22016-01-21 00:25:38207 return false;
[email protected]95640212014-07-26 18:14:30208}
209
kenrb19221852016-04-29 17:21:40210bool HasMatchingWidgetHost(FrameTree* tree, RenderWidgetHost* host) {
211 // This method scans the frame tree rather than checking whether
212 // host->delegate() == this, which allows it to return false when the host
213 // for a frame that is pending or pending deletion.
214 if (!host)
215 return false;
216
217 for (FrameTreeNode* node : tree->Nodes()) {
218 if (node->current_frame_host()->GetRenderWidgetHost() == host)
219 return true;
220 }
221 return false;
222}
223
dmazzonif8a31042016-12-03 00:52:13224void UpdateAccessibilityModeOnFrame(RenderFrameHost* frame_host) {
225 static_cast<RenderFrameHostImpl*>(frame_host)->UpdateAccessibilityMode();
[email protected]95640212014-07-26 18:14:30226}
227
dtseng0dd3fa12015-07-22 19:00:52228void ResetAccessibility(RenderFrameHost* rfh) {
229 static_cast<RenderFrameHostImpl*>(rfh)->AccessibilityReset();
230}
231
paulmeyerfeafc2d2017-04-25 21:46:40232// Helper for GetInnerWebContents().
Lucas Furukawa Gadani2ec00c82018-12-14 15:53:16233bool GetInnerWebContentsHelper(std::vector<WebContents*>* all_guest_contents,
234 WebContents* guest_contents) {
Ehsan Karamad6beb2ea2018-11-25 18:15:13235 auto* web_contents_impl = static_cast<WebContentsImpl*>(guest_contents);
236 if (web_contents_impl->GetBrowserPluginGuest()->attached() &&
237 !GuestMode::IsCrossProcessFrameGuest(web_contents_impl)) {
238 all_guest_contents->push_back(web_contents_impl);
239 }
paulmeyerfeafc2d2017-04-25 21:46:40240 return false;
241}
242
Ian Clelland5cbaaf82017-11-27 22:00:03243RenderFrameHostImpl* FindOpenerRFH(const WebContents::CreateParams& params) {
244 RenderFrameHostImpl* opener_rfh = nullptr;
alexmose201c7cd2015-06-10 17:14:21245 if (params.opener_render_frame_id != MSG_ROUTING_NONE) {
Ian Clelland5cbaaf82017-11-27 22:00:03246 opener_rfh = RenderFrameHostImpl::FromID(params.opener_render_process_id,
247 params.opener_render_frame_id);
alexmose201c7cd2015-06-10 17:14:21248 }
Ian Clelland5cbaaf82017-11-27 22:00:03249 return opener_rfh;
lukasza6f8ac622017-06-06 03:10:20250}
251
Dominick Ng37a15052018-07-17 23:56:35252// Returns |true| if |type| is the kind of user input that should trigger the
253// user interaction observers.
254bool IsUserInteractionInputType(blink::WebInputEvent::Type type) {
255 // Ideally, this list would be based more off of
256 // https://whatwg.org/C/interaction.html#triggered-by-user-activation.
257 return type == blink::WebInputEvent::kMouseDown ||
258 type == blink::WebInputEvent::kGestureScrollBegin ||
259 type == blink::WebInputEvent::kTouchStart ||
260 type == blink::WebInputEvent::kRawKeyDown;
261}
262
Pavel Feldman3c1842b2017-08-02 05:00:16263// Ensures that OnDialogClosed is only called once.
264class CloseDialogCallbackWrapper
265 : public base::RefCountedThreadSafe<CloseDialogCallbackWrapper> {
266 public:
Avi Drissmane04d3992017-10-05 15:11:36267 using CloseCallback =
268 base::OnceCallback<void(bool, bool, const base::string16&)>;
269
270 explicit CloseDialogCallbackWrapper(CloseCallback callback)
271 : callback_(std::move(callback)) {}
Pavel Feldman3c1842b2017-08-02 05:00:16272
273 void Run(bool dialog_was_suppressed,
274 bool success,
275 const base::string16& user_input) {
Avi Drissmane04d3992017-10-05 15:11:36276 if (callback_.is_null())
Pavel Feldman3c1842b2017-08-02 05:00:16277 return;
Avi Drissmane04d3992017-10-05 15:11:36278 std::move(callback_).Run(dialog_was_suppressed, success, user_input);
Pavel Feldman3c1842b2017-08-02 05:00:16279 }
280
281 private:
282 friend class base::RefCountedThreadSafe<CloseDialogCallbackWrapper>;
283 ~CloseDialogCallbackWrapper() {}
Avi Drissmane04d3992017-10-05 15:11:36284
285 CloseCallback callback_;
Pavel Feldman3c1842b2017-08-02 05:00:16286};
287
Arthur Sonzogni929d29f2018-09-17 14:19:44288bool FrameCompareDepth(RenderFrameHostImpl* a, RenderFrameHostImpl* b) {
289 return a->frame_tree_node()->depth() < b->frame_tree_node()->depth();
290}
291
Raymes Khoury58373df2019-08-06 06:29:20292bool AreValidRegisterProtocolHandlerArguments(const std::string& protocol,
293 const GURL& url,
294 const url::Origin& origin) {
295 ChildProcessSecurityPolicyImpl* policy =
296 ChildProcessSecurityPolicyImpl::GetInstance();
297 if (policy->IsPseudoScheme(protocol))
298 return false;
299
300 url::Origin url_origin = url::Origin::Create(url);
301 if (url_origin.opaque())
302 return false;
303
304 if (!url_origin.IsSameOriginWith(origin))
305 return false;
306
307 return true;
308}
309
yilkal796e89e2019-10-02 23:58:28310void RecordMaxFrameCountUMA(size_t max_frame_count) {
311 UMA_HISTOGRAM_COUNTS_10000("Navigation.MainFrame.MaxFrameCount",
312 max_frame_count);
313}
314
lukasza6f8ac622017-06-06 03:10:20315} // namespace
316
Erik Chenbb8e738e2018-04-28 14:10:43317std::unique_ptr<WebContents> WebContents::Create(
318 const WebContents::CreateParams& params) {
Albert J. Wong65fe64d2019-09-20 02:48:14319 return WebContentsImpl::Create(params);
320}
321
322std::unique_ptr<WebContentsImpl> WebContentsImpl::Create(
323 const CreateParams& params) {
324 return CreateWithOpener(params, FindOpenerRFH(params));
[email protected]d1198fd2012-08-13 22:50:19325}
326
erikchenade1fef2018-05-02 22:07:57327std::unique_ptr<WebContents> WebContents::CreateWithSessionStorage(
[email protected]54944cde2012-12-09 09:24:59328 const WebContents::CreateParams& params,
[email protected]fdac6ade2013-07-20 01:06:30329 const SessionStorageNamespaceMap& session_storage_namespace_map) {
erikchenade1fef2018-05-02 22:07:57330 std::unique_ptr<WebContentsImpl> new_contents(
331 new WebContentsImpl(params.browser_context));
Ian Clelland5cbaaf82017-11-27 22:00:03332 RenderFrameHostImpl* opener_rfh = FindOpenerRFH(params);
333 FrameTreeNode* opener = nullptr;
334 if (opener_rfh)
335 opener = opener_rfh->frame_tree_node();
336 new_contents->SetOpenerForNewContents(opener, params.opener_suppressed);
[email protected]fdac6ade2013-07-20 01:06:30337
jdoerrie55ec69d2018-10-08 13:34:46338 for (auto it = session_storage_namespace_map.begin();
339 it != session_storage_namespace_map.end(); ++it) {
[email protected]fdac6ade2013-07-20 01:06:30340 new_contents->GetController()
341 .SetSessionStorageNamespace(it->first, it->second.get());
342 }
343
Sean Toppingbbc4a2bd2019-01-30 02:16:14344 WebContentsImpl* outer_web_contents = nullptr;
Sam McNally69a33772018-06-27 16:54:12345 if (params.guest_delegate) {
346 // This makes |new_contents| act as a guest.
347 // For more info, see comment above class BrowserPluginGuest.
Albert J. Wongc55cc64d2018-10-12 02:50:04348 BrowserPluginGuest::CreateInWebContents(new_contents.get(),
349 params.guest_delegate);
Sean Toppingbbc4a2bd2019-01-30 02:16:14350 outer_web_contents = static_cast<WebContentsImpl*>(
351 params.guest_delegate->GetOwnerWebContents());
Sam McNally69a33772018-06-27 16:54:12352 }
353
[email protected]54944cde2012-12-09 09:24:59354 new_contents->Init(params);
Sean Toppingbbc4a2bd2019-01-30 02:16:14355 if (outer_web_contents)
356 outer_web_contents->InnerWebContentsCreated(new_contents.get());
[email protected]d1198fd2012-08-13 22:50:19357 return new_contents;
[email protected]a81343d232011-12-27 07:39:20358}
[email protected]746d3052012-05-22 15:15:47359
benwells5e7260d2017-03-20 23:36:33360void WebContentsImpl::FriendWrapper::AddCreatedCallbackForTesting(
nickf9acfbe2014-12-23 19:12:37361 const CreatedCallback& callback) {
[email protected]cbb1ef592013-06-05 19:49:46362 g_created_callbacks.Get().push_back(callback);
363}
364
benwells5e7260d2017-03-20 23:36:33365void WebContentsImpl::FriendWrapper::RemoveCreatedCallbackForTesting(
nickf9acfbe2014-12-23 19:12:37366 const CreatedCallback& callback) {
[email protected]cbb1ef592013-06-05 19:49:46367 for (size_t i = 0; i < g_created_callbacks.Get().size(); ++i) {
Robert Liao1db5a352019-06-05 20:57:34368 if (g_created_callbacks.Get().at(i) == callback) {
[email protected]cbb1ef592013-06-05 19:49:46369 g_created_callbacks.Get().erase(g_created_callbacks.Get().begin() + i);
370 return;
371 }
372 }
373}
374
avidf38c952015-10-27 13:45:13375WebContents* WebContents::FromRenderViewHost(RenderViewHost* rvh) {
376 if (!rvh)
377 return nullptr;
[email protected]746d3052012-05-22 15:15:47378 return rvh->GetDelegate()->GetAsWebContents();
379}
380
[email protected]a86c0e962013-12-17 17:10:39381WebContents* WebContents::FromRenderFrameHost(RenderFrameHost* rfh) {
avidf38c952015-10-27 13:45:13382 if (!rfh)
383 return nullptr;
384 return static_cast<RenderFrameHostImpl*>(rfh)->delegate()->GetAsWebContents();
[email protected]a86c0e962013-12-17 17:10:39385}
386
ananta4b7467a52016-09-23 01:42:38387WebContents* WebContents::FromFrameTreeNodeId(int frame_tree_node_id) {
388 FrameTreeNode* frame_tree_node =
389 FrameTreeNode::GloballyFindByID(frame_tree_node_id);
anantab8cfa3b2016-10-14 22:43:19390 if (!frame_tree_node)
391 return nullptr;
jam8c4edd02017-05-06 18:50:33392 return WebContentsImpl::FromFrameTreeNode(frame_tree_node);
ananta4b7467a52016-09-23 01:42:38393}
394
leon.han552e9de2017-02-09 14:37:30395void WebContents::SetScreenOrientationDelegate(
396 ScreenOrientationDelegate* delegate) {
397 ScreenOrientationProvider::SetDelegate(delegate);
398}
399
[email protected]7fff43e2013-05-21 20:21:10400// WebContentsImpl::DestructionObserver ----------------------------------------
401
402class WebContentsImpl::DestructionObserver : public WebContentsObserver {
403 public:
404 DestructionObserver(WebContentsImpl* owner, WebContents* watched_contents)
405 : WebContentsObserver(watched_contents),
406 owner_(owner) {
407 }
408
409 // WebContentsObserver:
dchengc2282aa2014-10-21 12:07:58410 void WebContentsDestroyed() override {
[email protected]12a46832014-05-09 13:35:58411 owner_->OnWebContentsDestroyed(
412 static_cast<WebContentsImpl*>(web_contents()));
[email protected]7fff43e2013-05-21 20:21:10413 }
414
415 private:
416 WebContentsImpl* owner_;
417
418 DISALLOW_COPY_AND_ASSIGN(DestructionObserver);
419};
420
Joel Hockey85b379d2017-12-11 10:42:13421// WebContentsImpl::ColorChooser ----------------------------------------------
Joel Hockey163835a2017-12-20 11:51:57422class WebContentsImpl::ColorChooser : public blink::mojom::ColorChooser {
Joel Hockey85b379d2017-12-11 10:42:13423 public:
424 ColorChooser(content::ColorChooser* chooser,
Julie Jeongeun Kim8e2879e2019-08-06 23:55:20425 mojo::PendingReceiver<blink::mojom::ColorChooser> receiver,
426 mojo::PendingRemote<blink::mojom::ColorChooserClient> client)
Joel Hockey85b379d2017-12-11 10:42:13427 : chooser_(chooser),
Julie Jeongeun Kim8e2879e2019-08-06 23:55:20428 receiver_(this, std::move(receiver)),
Joel Hockey85b379d2017-12-11 10:42:13429 client_(std::move(client)) {
Julie Jeongeun Kim8e2879e2019-08-06 23:55:20430 receiver_.set_disconnect_handler(
Joel Hockey85b379d2017-12-11 10:42:13431 base::BindOnce([](content::ColorChooser* chooser) { chooser->End(); },
432 base::Unretained(chooser)));
433 }
[email protected]8ed16472014-04-11 19:02:48434
Joel Hockey85b379d2017-12-11 10:42:13435 ~ColorChooser() override { chooser_->End(); }
[email protected]8ed16472014-04-11 19:02:48436
Joel Hockey85b379d2017-12-11 10:42:13437 void SetSelectedColor(SkColor color) override {
438 chooser_->SetSelectedColor(color);
439 }
440
441 void DidChooseColorInColorChooser(SkColor color) {
442 client_->DidChooseColor(color);
443 }
444
445 private:
446 // Color chooser that was opened by this tab.
447 std::unique_ptr<content::ColorChooser> chooser_;
448
Julie Jeongeun Kim8e2879e2019-08-06 23:55:20449 // mojo receiver.
450 mojo::Receiver<blink::mojom::ColorChooser> receiver_;
Joel Hockey85b379d2017-12-11 10:42:13451
452 // mojo renderer client.
Julie Jeongeun Kim8e2879e2019-08-06 23:55:20453 mojo::Remote<blink::mojom::ColorChooserClient> client_;
Joel Hockey85b379d2017-12-11 10:42:13454};
nicka0ac8382016-12-15 23:59:23455
lazyboy6ec48b2a2015-06-29 15:18:14456// WebContentsImpl::WebContentsTreeNode ----------------------------------------
lfg601233692017-03-06 22:45:44457WebContentsImpl::WebContentsTreeNode::WebContentsTreeNode(
458 WebContentsImpl* current_web_contents)
459 : current_web_contents_(current_web_contents),
460 outer_web_contents_(nullptr),
lazyboy6ec48b2a2015-06-29 15:18:14461 outer_contents_frame_tree_node_id_(
avallee0206f782016-07-28 18:55:33462 FrameTreeNode::kFrameTreeNodeInvalidId),
lfg601233692017-03-06 22:45:44463 focused_web_contents_(current_web_contents) {}
lazyboy6ec48b2a2015-06-29 15:18:14464
Lucas Furukawa Gadani3d38ec152018-10-26 20:55:18465WebContentsImpl::WebContentsTreeNode::~WebContentsTreeNode() {}
lazyboy6ec48b2a2015-06-29 15:18:14466
Lucas Furukawa Gadani99125822019-01-03 15:41:49467std::unique_ptr<WebContents>
468WebContentsImpl::WebContentsTreeNode::DisconnectFromOuterWebContents() {
469 std::unique_ptr<WebContents> inner_contents =
470 outer_web_contents_->node_.DetachInnerWebContents(current_web_contents_);
471 OuterContentsFrameTreeNode()->RemoveObserver(this);
472 outer_contents_frame_tree_node_id_ = FrameTreeNode::kFrameTreeNodeInvalidId;
473 outer_web_contents_ = nullptr;
474 return inner_contents;
475}
476
paulmeyerfeafc2d2017-04-25 21:46:40477void WebContentsImpl::WebContentsTreeNode::AttachInnerWebContents(
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:50478 std::unique_ptr<WebContents> inner_web_contents,
479 RenderFrameHostImpl* render_frame_host) {
480 WebContentsImpl* inner_web_contents_impl =
481 static_cast<WebContentsImpl*>(inner_web_contents.get());
482 WebContentsTreeNode& inner_web_contents_node = inner_web_contents_impl->node_;
483
484 inner_web_contents_node.focused_web_contents_ = nullptr;
485 inner_web_contents_node.outer_web_contents_ = current_web_contents_;
486 inner_web_contents_node.outer_contents_frame_tree_node_id_ =
487 render_frame_host->frame_tree_node()->frame_tree_node_id();
488
Lucas Furukawa Gadani3d38ec152018-10-26 20:55:18489 inner_web_contents_.push_back(std::move(inner_web_contents));
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:50490
491 render_frame_host->frame_tree_node()->AddObserver(&inner_web_contents_node);
paulmeyerfeafc2d2017-04-25 21:46:40492}
493
Lucas Furukawa Gadani3d38ec152018-10-26 20:55:18494std::unique_ptr<WebContents>
495WebContentsImpl::WebContentsTreeNode::DetachInnerWebContents(
paulmeyerfeafc2d2017-04-25 21:46:40496 WebContentsImpl* inner_web_contents) {
Lucas Furukawa Gadani3d38ec152018-10-26 20:55:18497 std::unique_ptr<WebContents> detached_contents;
498 for (std::unique_ptr<WebContents>& web_contents : inner_web_contents_) {
499 if (web_contents.get() == inner_web_contents) {
500 detached_contents = std::move(web_contents);
501 std::swap(web_contents, inner_web_contents_.back());
502 inner_web_contents_.pop_back();
503 return detached_contents;
504 }
505 }
506
507 NOTREACHED();
508 return nullptr;
paulmeyerfeafc2d2017-04-25 21:46:40509}
510
lfga3e2bdc2017-03-07 20:44:01511FrameTreeNode*
512WebContentsImpl::WebContentsTreeNode::OuterContentsFrameTreeNode() const {
513 return FrameTreeNode::GloballyFindByID(outer_contents_frame_tree_node_id_);
lfg601233692017-03-06 22:45:44514}
515
lfga3e2bdc2017-03-07 20:44:01516void WebContentsImpl::WebContentsTreeNode::OnFrameTreeNodeDestroyed(
517 FrameTreeNode* node) {
518 DCHECK_EQ(outer_contents_frame_tree_node_id_, node->frame_tree_node_id())
519 << "WebContentsTreeNode should only receive notifications for the "
520 "FrameTreeNode in its outer WebContents that hosts it.";
Lucas Furukawa Gadani3d38ec152018-10-26 20:55:18521
522 // Deletes |this| too.
523 outer_web_contents_->node_.DetachInnerWebContents(current_web_contents_);
lazyboy6ec48b2a2015-06-29 15:18:14524}
525
avallee0206f782016-07-28 18:55:33526void WebContentsImpl::WebContentsTreeNode::SetFocusedWebContents(
527 WebContentsImpl* web_contents) {
528 DCHECK(!outer_web_contents())
529 << "Only the outermost WebContents tracks focus.";
530 focused_web_contents_ = web_contents;
531}
532
paulmeyerfeafc2d2017-04-25 21:46:40533WebContentsImpl*
534WebContentsImpl::WebContentsTreeNode::GetInnerWebContentsInFrame(
535 const FrameTreeNode* frame) {
536 auto ftn_id = frame->frame_tree_node_id();
Lucas Furukawa Gadani3d38ec152018-10-26 20:55:18537 for (auto& contents : inner_web_contents_) {
538 WebContentsImpl* impl = static_cast<WebContentsImpl*>(contents.get());
539 if (impl->node_.outer_contents_frame_tree_node_id() == ftn_id) {
540 return impl;
paulmeyerfeafc2d2017-04-25 21:46:40541 }
542 }
543 return nullptr;
544}
545
Lucas Furukawa Gadani3d38ec152018-10-26 20:55:18546std::vector<WebContentsImpl*>
547WebContentsImpl::WebContentsTreeNode::GetInnerWebContents() const {
548 std::vector<WebContentsImpl*> inner_web_contents;
549 for (auto& contents : inner_web_contents_)
550 inner_web_contents.push_back(static_cast<WebContentsImpl*>(contents.get()));
551
552 return inner_web_contents;
paulmeyerfeafc2d2017-04-25 21:46:40553}
554
[email protected]b172aee2012-04-10 17:05:26555// WebContentsImpl -------------------------------------------------------------
[email protected]420ae012009-04-24 05:16:32556
alexmose201c7cd2015-06-10 17:14:21557WebContentsImpl::WebContentsImpl(BrowserContext* browser_context)
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28558 : delegate_(nullptr),
[email protected]69e797f2013-04-30 01:10:22559 controller_(this, browser_context),
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28560 render_view_host_delegate_view_(nullptr),
alexmose201c7cd2015-06-10 17:14:21561 created_with_opener_(false),
Lucas Furukawa Gadani99125822019-01-03 15:41:49562 node_(this),
[email protected]6ea6bdf2013-12-06 13:35:01563 frame_tree_(new NavigatorImpl(&controller_, this),
blundellc57b93f2014-10-29 13:19:57564 this,
565 this,
566 this,
567 this),
[email protected]6dfed692014-05-22 04:18:03568 is_load_to_different_document_(false),
[email protected]443b80e2010-12-14 00:42:23569 crashed_status_(base::TERMINATION_STATUS_STILL_RUNNING),
570 crashed_error_code_(0),
[email protected]d5f942ba2008-09-26 19:30:34571 waiting_for_response_(false),
[email protected]fcf75d42013-12-03 20:11:26572 load_state_(net::LOAD_STATE_IDLE, base::string16()),
[email protected]094e5b22009-09-25 04:23:56573 upload_size_(0),
574 upload_position_(0),
dfalcantarae6b7b46752015-07-10 18:14:16575 is_resume_pending_(false),
creisadca9862017-06-16 19:12:11576 interstitial_page_(nullptr),
[email protected]b4c84012014-04-28 19:51:10577 has_accessed_initial_document_(false),
[email protected]54597982013-02-06 01:59:55578 capturer_count_(0),
[email protected]fdd61c62009-04-22 19:22:57579 is_being_destroyed_(false),
creis97867aa2017-02-17 18:39:45580 is_notifying_observers_(false),
[email protected]fdd61c62009-04-22 19:22:57581 notify_disconnection_(false),
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28582 dialog_manager_(nullptr),
[email protected]7ab1e7d62009-10-14 23:32:01583 is_showing_before_unload_dialog_(false),
gab0dccfef2015-05-20 18:43:39584 last_active_time_(base::TimeTicks::Now()),
[email protected]ebf40a72010-07-22 01:46:38585 closed_by_user_gesture_(false),
danakj938b37a62019-09-24 18:35:54586 minimum_zoom_percent_(
587 static_cast<int>(blink::kMinimumPageZoomFactor * 100)),
588 maximum_zoom_percent_(
589 static_cast<int>(blink::kMaximumPageZoomFactor * 100)),
w.shackleton49bcd392016-01-06 17:38:22590 zoom_scroll_remainder_(0),
alexmosc9e76ec2016-05-16 22:59:37591 fullscreen_widget_process_id_(ChildProcessHost::kInvalidUniqueID),
[email protected]5dcaf8e2013-12-28 01:31:42592 fullscreen_widget_routing_id_(MSG_ROUTING_NONE),
[email protected]0b431992014-06-24 00:08:03593 fullscreen_widget_had_focus_at_shutdown_(false),
[email protected]34ff1cfc2014-08-20 06:16:05594 force_disable_overscroll_content_(false),
[email protected]95640212014-07-26 18:14:30595 last_dialog_suppressed_(false),
waffles1c12bf402016-02-10 17:45:48596 accessibility_mode_(
Dominic Mazzoni21fb0282019-02-13 18:32:47597 GetContentClient()->browser()->GetAXModeForBrowserContext(
598 browser_context)),
avayvodcc85bbd2015-08-28 19:11:15599 audio_stream_monitor_(this),
Thomas Guilbert882c4cec2019-02-14 05:55:45600 media_web_contents_observer_(
601 std::make_unique<MediaWebContentsObserver>(this)),
mcnee336ea2c2017-05-23 22:50:59602#if !defined(OS_ANDROID)
mcnee432e47d2015-11-09 19:37:46603 page_scale_factor_is_one_(true),
mcnee336ea2c2017-05-23 22:50:59604#endif // !defined(OS_ANDROID)
qinmin72e8bd02016-10-21 19:35:37605 is_overlay_content_(false),
ekaramadf6750aa2017-06-06 18:29:42606 showing_context_menu_(false),
W. James MacLean62198672019-06-04 16:46:10607 text_autosizer_page_info_({0, 0, 1.f}),
Jeremy Roman3bca4bf2019-07-11 03:41:25608 had_inner_webcontents_(false) {
[email protected]9b159a52013-10-03 17:24:55609 frame_tree_.SetFrameRemoveListener(
610 base::Bind(&WebContentsImpl::OnFrameRemoved,
611 base::Unretained(this)));
brettw4b461082016-11-19 18:55:16612#if BUILDFLAG(ENABLE_PLUGINS)
zqzhang181047e62016-07-01 13:37:17613 pepper_playback_observer_.reset(new PepperPlaybackObserver(this));
614#endif
blundelle75a8f92017-03-27 08:11:17615
Becca Hughes86334622018-07-09 21:29:38616#if defined(OS_ANDROID)
Elly Fong-Jonescdd7ae82019-02-20 23:26:33617 display_cutout_host_impl_ = std::make_unique<DisplayCutoutHostImpl>(this);
Becca Hughes86334622018-07-09 21:29:38618#endif
Becca Hughes6fe54e432018-06-09 04:18:56619
Julie Jeongeun Kim8e2879e2019-08-06 23:55:20620 binders_.Add(base::BindRepeating(
Joel Hockey85b379d2017-12-11 10:42:13621 &WebContentsImpl::OnColorChooserFactoryRequest, base::Unretained(this)));
[email protected]332af7732009-03-11 13:21:35622}
initial.commit09911bf2008-07-26 23:55:29623
[email protected]b172aee2012-04-10 17:05:26624WebContentsImpl::~WebContentsImpl() {
creisba91ba82017-05-12 22:56:19625 // Imperfect sanity check against double free, given some crashes unexpectedly
626 // observed in the wild.
627 CHECK(!is_being_destroyed_);
628
629 // We generally keep track of is_being_destroyed_ to let other features know
630 // to avoid certain actions during destruction.
[email protected]420ae012009-04-24 05:16:32631 is_being_destroyed_ = true;
632
creis97867aa2017-02-17 18:39:45633 // A WebContents should never be deleted while it is notifying observers,
creisba91ba82017-05-12 22:56:19634 // since this will lead to a use-after-free as it continues to notify later
creis97867aa2017-02-17 18:39:45635 // observers.
636 CHECK(!is_notifying_observers_);
637
kenrb2a565f82015-09-02 20:24:59638 rwh_input_event_router_.reset();
639
rockotf62002a2016-09-15 00:08:59640 for (auto& entry : binding_sets_)
641 entry.second->CloseAllBindings();
642
avallee0206f782016-07-28 18:55:33643 WebContentsImpl* outermost = GetOutermostWebContents();
avallee9993fca2016-11-17 06:16:50644 if (this != outermost && ContainsOrIsFocusedWebContents()) {
avallee0206f782016-07-28 18:55:33645 // If the current WebContents is in focus, unset it.
avallee9993fca2016-11-17 06:16:50646 outermost->SetAsFocusedWebContentsIfNecessary();
avallee0206f782016-07-28 18:55:33647 }
648
Nick Carterd73635b2018-03-13 18:31:41649 if (mouse_lock_widget_)
650 mouse_lock_widget_->RejectMouseLockOrUnlockIfNecessary();
651
dcheng72be33c2016-01-21 04:11:31652 for (FrameTreeNode* node : frame_tree_.Nodes()) {
653 // Delete all RFHs pending shutdown, which will lead the corresponding RVHs
654 // to be shutdown and be deleted as well.
dcheng57e39e22016-01-21 00:25:38655 node->render_manager()->ClearRFHsPendingShutdown();
dcheng57e39e22016-01-21 00:25:38656 node->render_manager()->ClearWebUIInstances();
dcheng72be33c2016-01-21 04:11:31657 }
carlosk35f35af2015-12-01 10:55:40658
dcheng57e39e22016-01-21 00:25:38659 for (RenderWidgetHostImpl* widget : created_widgets_)
660 widget->DetachDelegate();
[email protected]b24b68a2012-09-24 21:57:26661 created_widgets_.clear();
662
[email protected]3ab9cb82011-06-03 18:02:07663 // Clear out any JavaScript state.
avi5d3b8692016-10-12 22:00:46664 if (dialog_manager_) {
avi6879fcf2017-01-21 05:27:53665 dialog_manager_->CancelDialogs(this, /*reset_state=*/true);
avi5d3b8692016-10-12 22:00:46666 }
[email protected]3ab9cb82011-06-03 18:02:07667
Joel Hockey85b379d2017-12-11 10:42:13668 color_chooser_.reset();
Rakina Zata Amni16196122018-11-14 02:14:00669 find_request_manager_.reset();
[email protected]da8543762012-03-20 08:52:20670
[email protected]420ae012009-04-24 05:16:32671 NotifyDisconnected();
[email protected]420ae012009-04-24 05:16:32672
[email protected]ca13a442012-04-17 14:00:12673 // Notify any observer that have a reference on this WebContents.
[email protected]8ff00d72012-10-23 19:12:21674 NotificationService::current()->Notify(
675 NOTIFICATION_WEB_CONTENTS_DESTROYED,
676 Source<WebContents>(this),
677 NotificationService::NoDetails());
[email protected]420ae012009-04-24 05:16:32678
Arthur Sonzogni5a9a88c2018-10-18 13:16:00679 // Destroy all subframes now. This notifies observers.
680 GetMainFrame()->ResetChildren();
[email protected]df0c843f2014-05-23 20:37:23681 GetRenderManager()->ResetProxyHosts();
[email protected]c06c58c2014-03-12 20:31:59682
clamy0e119882015-07-31 16:12:33683 // Manually call the observer methods for the root frame tree node. It is
684 // necessary to manually delete all objects tracking navigations
685 // (NavigationHandle, NavigationRequest) for observers to be properly
686 // notified of these navigations stopping before the WebContents is
687 // destroyed.
[email protected]df0c843f2014-05-23 20:37:23688 RenderFrameHostManager* root = GetRenderManager();
naskob21fe4872015-02-24 14:15:56689
Lowell Manners20250112019-10-18 15:06:15690 GetController().GetBackForwardCache().Shutdown();
691
naskob21fe4872015-02-24 14:15:56692 root->current_frame_host()->SetRenderFrameCreated(false);
clamyfc3850d2018-02-23 14:35:28693 root->current_frame_host()->ResetNavigationRequests();
clamy0e119882015-07-31 16:12:33694
Arthur Hemeryc3380172018-01-22 14:00:17695 // Do not update state as the WebContents is being destroyed.
Arthur Hemery241b9392019-10-24 11:08:41696 frame_tree_.root()->ResetNavigationRequest(true);
Arthur Hemeryc3380172018-01-22 14:00:17697 if (root->speculative_frame_host()) {
danakjf2342432019-05-15 21:46:09698 root->speculative_frame_host()->DeleteRenderFrame(
699 FrameDeleteIntention::kSpeculativeMainFrameForShutdown);
Arthur Hemeryc3380172018-01-22 14:00:17700 root->speculative_frame_host()->SetRenderFrameCreated(false);
clamyfc3850d2018-02-23 14:35:28701 root->speculative_frame_host()->ResetNavigationRequests();
clamy0e119882015-07-31 16:12:33702 }
[email protected]f273ee52013-10-18 16:05:27703
brettw4b461082016-11-19 18:55:16704#if BUILDFLAG(ENABLE_PLUGINS)
zqzhang181047e62016-07-01 13:37:17705 // Call this before WebContentsDestroyed() is broadcasted since
706 // AudioFocusManager will be destroyed after that.
707 pepper_playback_observer_.reset();
708#endif // defined(ENABLED_PLUGINS)
709
Chris Hamiltondf0d72cd2018-05-29 16:23:53710 // If audio is playing then notify external observers of the audio stream
711 // disappearing.
712 if (is_currently_audible_) {
713 is_currently_audible_ = false;
714 for (auto& observer : observers_)
715 observer.OnAudioStateChanged(false);
716
717 if (GetOuterWebContents())
718 GetOuterWebContents()->OnAudioStateChanged();
719 }
720
ericwilligersde386342016-10-19 02:35:09721 for (auto& observer : observers_)
722 observer.FrameDeleted(root->current_frame_host());
naskoa83483fb2015-02-27 16:57:10723
ericwilligersde386342016-10-19 02:35:09724 for (auto& observer : observers_)
725 observer.RenderViewDeleted(root->current_host());
[email protected]f273ee52013-10-18 16:05:27726
Kevin McNee576bb5d82019-05-29 16:47:51727#if defined(OS_ANDROID)
728 // For simplicity, destroy the Java WebContents before we notify of the
729 // destruction of the WebContents.
730 ClearWebContentsAndroid();
731#endif
732
ericwilligersde386342016-10-19 02:35:09733 for (auto& observer : observers_)
734 observer.WebContentsDestroyed();
[email protected]12a46832014-05-09 13:35:58735
Becca Hughesd11d6502018-07-31 17:01:28736 if (display_cutout_host_impl_)
737 display_cutout_host_impl_->WebContentsDestroyed();
738
ericwilligersde386342016-10-19 02:35:09739 for (auto& observer : observers_)
740 observer.ResetWebContents();
[email protected]232a5812011-03-04 22:42:08741
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28742 SetDelegate(nullptr);
[email protected]b5a1d11c2011-02-17 03:09:42743}
744
erikchen50735fd2018-05-05 15:08:33745std::unique_ptr<WebContentsImpl> WebContentsImpl::CreateWithOpener(
[email protected]54944cde2012-12-09 09:24:59746 const WebContents::CreateParams& params,
Ian Clelland5cbaaf82017-11-27 22:00:03747 RenderFrameHostImpl* opener_rfh) {
[email protected]e11f0e92013-06-12 15:12:03748 TRACE_EVENT0("browser", "WebContentsImpl::CreateWithOpener");
Ian Clelland5cbaaf82017-11-27 22:00:03749 FrameTreeNode* opener = nullptr;
750 if (opener_rfh)
751 opener = opener_rfh->frame_tree_node();
erikchen50735fd2018-05-05 15:08:33752 std::unique_ptr<WebContentsImpl> new_contents(
753 new WebContentsImpl(params.browser_context));
lukasza6f8ac622017-06-06 03:10:20754 new_contents->SetOpenerForNewContents(opener, params.opener_suppressed);
alexmose201c7cd2015-06-10 17:14:21755
alexmosaedfc6f2016-01-21 23:57:38756 // If the opener is sandboxed, a new popup must inherit the opener's sandbox
757 // flags, and these flags take effect immediately. An exception is if the
758 // opener's sandbox flags lack the PropagatesToAuxiliaryBrowsingContexts
759 // bit (which is controlled by the "allow-popups-to-escape-sandbox" token).
760 // See https://html.spec.whatwg.org/#attr-iframe-sandbox.
lukasza6f8ac622017-06-06 03:10:20761 FrameTreeNode* new_root = new_contents->GetFrameTree()->root();
alexmosaedfc6f2016-01-21 23:57:38762 if (opener) {
Ian Clelland5cbaaf82017-11-27 22:00:03763 blink::WebSandboxFlags opener_flags = opener_rfh->active_sandbox_flags();
alexmosaedfc6f2016-01-21 23:57:38764 const blink::WebSandboxFlags inherit_flag =
Blink Reformat1c4d759e2017-04-09 16:34:54765 blink::WebSandboxFlags::kPropagatesToAuxiliaryBrowsingContexts;
Ehsan Karamad39407082019-02-19 23:38:19766 bool sandbox_propagates_to_auxilary_context =
767 (opener_flags & inherit_flag) == inherit_flag;
768 if (sandbox_propagates_to_auxilary_context)
Ian Clellandcdc4f312017-10-13 22:24:12769 new_root->SetPendingFramePolicy({opener_flags, {}});
Ehsan Karamad39407082019-02-19 23:38:19770 if (opener_flags == blink::WebSandboxFlags::kNone ||
771 sandbox_propagates_to_auxilary_context) {
772 // TODO(ekaramad, iclelland): Do not propagate feature policies from non-
773 // sandboxed disowned openers (rel=noopener).
774 // If the current page is not sandboxed, or if the sandbox is to propagate
775 // to the popups then opener's feature policy will apply to the new popup
776 // as well.
777 new_root->SetOpenerFeaturePolicyState(
Ehsan Karamad34d10682019-02-26 20:55:04778 opener_rfh->feature_policy()->GetFeatureState());
alexmosaedfc6f2016-01-21 23:57:38779 }
780 }
781
mark a. foltzef394fce2017-10-21 09:11:02782 // Apply starting sandbox flags.
Luna Luc3fdacdf2017-11-08 04:48:53783 blink::FramePolicy frame_policy(new_root->pending_frame_policy());
mark a. foltzef394fce2017-10-21 09:11:02784 frame_policy.sandbox_flags |= params.starting_sandbox_flags;
785 new_root->SetPendingFramePolicy(frame_policy);
786 new_root->CommitPendingFramePolicy();
787
alexmose201c7cd2015-06-10 17:14:21788 // This may be true even when opener is null, such as when opening blocked
789 // popups.
alexmos090fae8e2015-05-28 17:09:28790 if (params.created_with_opener)
791 new_contents->created_with_opener_ = true;
792
Sean Toppingbbc4a2bd2019-01-30 02:16:14793 WebContentsImpl* outer_web_contents = nullptr;
[email protected]4858e432014-06-23 18:14:17794 if (params.guest_delegate) {
[email protected]83100cd2014-05-10 11:50:06795 // This makes |new_contents| act as a guest.
796 // For more info, see comment above class BrowserPluginGuest.
Albert J. Wongc55cc64d2018-10-12 02:50:04797 BrowserPluginGuest::CreateInWebContents(new_contents.get(),
798 params.guest_delegate);
Sean Toppingbbc4a2bd2019-01-30 02:16:14799 outer_web_contents = static_cast<WebContentsImpl*>(
800 params.guest_delegate->GetOwnerWebContents());
[email protected]83100cd2014-05-10 11:50:06801 }
mark a. foltzef394fce2017-10-21 09:11:02802
[email protected]54944cde2012-12-09 09:24:59803 new_contents->Init(params);
Sean Toppingbbc4a2bd2019-01-30 02:16:14804 if (outer_web_contents)
805 outer_web_contents->InnerWebContentsCreated(new_contents.get());
[email protected]d1198fd2012-08-13 22:50:19806 return new_contents;
807}
808
[email protected]95640212014-07-26 18:14:30809// static
810std::vector<WebContentsImpl*> WebContentsImpl::GetAllWebContents() {
811 std::vector<WebContentsImpl*> result;
dcheng59716272016-04-09 05:19:08812 std::unique_ptr<RenderWidgetHostIterator> widgets(
[email protected]95640212014-07-26 18:14:30813 RenderWidgetHostImpl::GetRenderWidgetHosts());
[email protected]95640212014-07-26 18:14:30814 while (RenderWidgetHost* rwh = widgets->GetNextHost()) {
[email protected]95640212014-07-26 18:14:30815 RenderViewHost* rvh = RenderViewHost::From(rwh);
816 if (!rvh)
817 continue;
818 WebContents* web_contents = WebContents::FromRenderViewHost(rvh);
819 if (!web_contents)
820 continue;
avidf38c952015-10-27 13:45:13821 if (web_contents->GetRenderViewHost() != rvh)
822 continue;
823 // Because a WebContents can only have one current RVH at a time, there will
824 // be no duplicate WebContents here.
825 result.push_back(static_cast<WebContentsImpl*>(web_contents));
[email protected]95640212014-07-26 18:14:30826 }
827 return result;
828}
829
clamya16aa8172015-05-26 13:07:25830// static
831WebContentsImpl* WebContentsImpl::FromFrameTreeNode(
paulmeyerfeafc2d2017-04-25 21:46:40832 const FrameTreeNode* frame_tree_node) {
clamya16aa8172015-05-26 13:07:25833 return static_cast<WebContentsImpl*>(
834 WebContents::FromRenderFrameHost(frame_tree_node->current_frame_host()));
835}
836
clamy0d32d6d2015-11-24 11:16:26837// static
838WebContents* WebContentsImpl::FromRenderFrameHostID(int render_process_host_id,
839 int render_frame_host_id) {
Min Qina1150112019-02-15 01:47:49840 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) ||
841 !BrowserThread::IsThreadInitialized(BrowserThread::UI));
clamy0d32d6d2015-11-24 11:16:26842 RenderFrameHost* render_frame_host =
843 RenderFrameHost::FromID(render_process_host_id, render_frame_host_id);
844 if (!render_frame_host)
845 return nullptr;
846
847 return WebContents::FromRenderFrameHost(render_frame_host);
848}
849
paulmeyerfeafc2d2017-04-25 21:46:40850// static
851WebContentsImpl* WebContentsImpl::FromOuterFrameTreeNode(
852 const FrameTreeNode* frame_tree_node) {
853 return WebContentsImpl::FromFrameTreeNode(frame_tree_node)
854 ->node_.GetInnerWebContentsInFrame(frame_tree_node);
855}
856
[email protected]b0936d22013-11-28 06:47:36857RenderFrameHostManager* WebContentsImpl::GetRenderManagerForTesting() {
[email protected]fa944cb82013-11-15 17:51:21858 return GetRenderManager();
[email protected]765187182012-01-11 23:59:28859}
860
nicka0ac8382016-12-15 23:59:23861bool WebContentsImpl::OnMessageReceived(RenderViewHostImpl* render_view_host,
[email protected]7bb761892012-07-20 09:32:47862 const IPC::Message& message) {
nicka0ac8382016-12-15 23:59:23863 for (auto& observer : observers_) {
Lukasz Anforowicz89f2c1b2017-08-30 21:36:46864 // TODO(nick, creis): https://crbug.com/758026: Replace all uses of this
865 // variant of OnMessageReceived with the version that takes a
866 // RenderFrameHost, and then delete it.
nicka0ac8382016-12-15 23:59:23867 if (observer.OnMessageReceived(message))
868 return true;
[email protected]64ffefa2014-05-10 12:06:33869 }
[email protected]403415a2011-01-10 18:57:53870
[email protected]724159a2010-12-30 01:11:18871 bool handled = true;
nicka0ac8382016-12-15 23:59:23872 IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(WebContentsImpl, message, render_view_host)
nicka0ac8382016-12-15 23:59:23873 IPC_MESSAGE_HANDLER(ViewHostMsg_PageScaleFactorChanged,
874 OnPageScaleFactorChanged)
W. James MacLean62198672019-06-04 16:46:10875 IPC_MESSAGE_HANDLER(
876 ViewHostMsg_NotifyTextAutosizerPageInfoChangedInLocalMainFrame,
877 OnTextAutosizerPageInfoChanged)
nicka0ac8382016-12-15 23:59:23878#if BUILDFLAG(ENABLE_PLUGINS)
879 IPC_MESSAGE_HANDLER(ViewHostMsg_RequestPpapiBrokerPermission,
880 OnRequestPpapiBrokerPermission)
881#endif
nicka0ac8382016-12-15 23:59:23882 IPC_MESSAGE_UNHANDLED(handled = false)
883 IPC_END_MESSAGE_MAP()
884
885 return handled;
886}
887
888bool WebContentsImpl::OnMessageReceived(RenderFrameHostImpl* render_frame_host,
889 const IPC::Message& message) {
Lukasz Anforowicz02923102017-10-09 18:11:37890 {
891 WebUIImpl* web_ui = render_frame_host->web_ui();
892 if (web_ui && web_ui->OnMessageReceived(message, render_frame_host))
893 return true;
894 }
895
nicka0ac8382016-12-15 23:59:23896 for (auto& observer : observers_) {
897 if (observer.OnMessageReceived(message, render_frame_host))
898 return true;
899 }
900
901 bool handled = true;
902 IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(WebContentsImpl, message, render_frame_host)
[email protected]cfa856d62014-02-22 07:58:40903 IPC_MESSAGE_HANDLER(FrameHostMsg_DomOperationResponse,
904 OnDomOperationResponse)
[email protected]8ed16472014-04-11 19:02:48905 IPC_MESSAGE_HANDLER(FrameHostMsg_DidFinishLoad, OnDidFinishLoad)
avi7830c852015-08-31 23:46:25906 IPC_MESSAGE_HANDLER(FrameHostMsg_DidLoadResourceFromMemoryCache,
[email protected]724159a2010-12-30 01:11:18907 OnDidLoadResourceFromMemoryCache)
avi3a3332622015-09-01 01:18:54908 IPC_MESSAGE_HANDLER(FrameHostMsg_DidRunInsecureContent,
[email protected]724159a2010-12-30 01:11:18909 OnDidRunInsecureContent)
estark910b4572015-12-09 20:55:41910 IPC_MESSAGE_HANDLER(FrameHostMsg_DidDisplayContentWithCertificateErrors,
911 OnDidDisplayContentWithCertificateErrors)
912 IPC_MESSAGE_HANDLER(FrameHostMsg_DidRunContentWithCertificateErrors,
913 OnDidRunContentWithCertificateErrors)
Dave Tapuska549baab2019-03-19 15:52:40914 IPC_MESSAGE_HANDLER(FrameHostMsg_GoToEntryAtOffset, OnGoToEntryAtOffset)
kouhei40f03cb2015-09-24 07:47:01915 IPC_MESSAGE_HANDLER(FrameHostMsg_UpdatePageImportanceSignals,
916 OnUpdatePageImportanceSignals)
lukasza178ff092017-06-07 23:39:50917 IPC_MESSAGE_HANDLER(FrameHostMsg_UpdateFaviconURL, OnUpdateFaviconURL)
brettw4b461082016-11-19 18:55:16918#if BUILDFLAG(ENABLE_PLUGINS)
emaxxe70f5e12015-05-29 11:26:00919 IPC_MESSAGE_HANDLER(FrameHostMsg_PepperInstanceCreated,
920 OnPepperInstanceCreated)
921 IPC_MESSAGE_HANDLER(FrameHostMsg_PepperInstanceDeleted,
922 OnPepperInstanceDeleted)
tommyclie6633ca72014-10-31 00:40:42923 IPC_MESSAGE_HANDLER(FrameHostMsg_PepperPluginHung, OnPepperPluginHung)
zqzhang181047e62016-07-01 13:37:17924 IPC_MESSAGE_HANDLER(FrameHostMsg_PepperStartsPlayback,
925 OnPepperStartsPlayback)
926 IPC_MESSAGE_HANDLER(FrameHostMsg_PepperStopsPlayback,
927 OnPepperStopsPlayback)
tommyclie6633ca72014-10-31 00:40:42928 IPC_MESSAGE_HANDLER(FrameHostMsg_PluginCrashed, OnPluginCrashed)
[email protected]c4538072013-03-18 02:17:55929 IPC_MESSAGE_HANDLER_GENERIC(BrowserPluginHostMsg_Attach,
fsamuel833ee7c2015-02-13 23:40:40930 OnBrowserPluginMessage(render_frame_host,
931 message))
thestigc4cac8f2014-09-04 21:17:50932#endif
[email protected]724159a2010-12-30 01:11:18933 IPC_MESSAGE_UNHANDLED(handled = false)
[email protected]e44d1342014-05-16 21:29:33934 IPC_END_MESSAGE_MAP()
[email protected]724159a2010-12-30 01:11:18935
[email protected]724159a2010-12-30 01:11:18936 return handled;
937}
938
[email protected]d1198fd2012-08-13 22:50:19939NavigationControllerImpl& WebContentsImpl::GetController() {
[email protected]f5fa20e2011-12-21 22:35:56940 return controller_;
941}
942
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:41943BrowserContext* WebContentsImpl::GetBrowserContext() {
[email protected]a26023822011-12-29 00:23:55944 return controller_.GetBrowserContext();
[email protected]627e0512011-12-21 22:55:30945}
946
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:41947const GURL& WebContentsImpl::GetURL() {
Peter Boström101d7672018-12-15 00:43:59948 return GetVisibleURL();
[email protected]be1f56ab2011-12-22 06:55:31949}
950
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:41951const GURL& WebContentsImpl::GetVisibleURL() {
[email protected]c854a7e2013-05-21 16:42:24952 // We may not have a navigation entry yet.
[email protected]59167c22013-06-03 18:07:32953 NavigationEntry* entry = controller_.GetVisibleEntry();
[email protected]c854a7e2013-05-21 16:42:24954 return entry ? entry->GetVirtualURL() : GURL::EmptyGURL();
955}
956
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:41957const GURL& WebContentsImpl::GetLastCommittedURL() {
[email protected]c854a7e2013-05-21 16:42:24958 // We may not have a navigation entry yet.
959 NavigationEntry* entry = controller_.GetLastCommittedEntry();
960 return entry ? entry->GetVirtualURL() : GURL::EmptyGURL();
961}
962
[email protected]8ff00d72012-10-23 19:12:21963WebContentsDelegate* WebContentsImpl::GetDelegate() {
[email protected]be1f56ab2011-12-22 06:55:31964 return delegate_;
965}
966
[email protected]8ff00d72012-10-23 19:12:21967void WebContentsImpl::SetDelegate(WebContentsDelegate* delegate) {
[email protected]be1f56ab2011-12-22 06:55:31968 // TODO(cbentzel): remove this debugging code?
969 if (delegate == delegate_)
970 return;
971 if (delegate_)
972 delegate_->Detach(this);
973 delegate_ = delegate;
[email protected]a6b73c62013-02-11 23:05:08974 if (delegate_) {
[email protected]be1f56ab2011-12-22 06:55:31975 delegate_->Attach(this);
[email protected]a6b73c62013-02-11 23:05:08976 // Ensure the visible RVH reflects the new delegate's preferences.
[email protected]86f98a22013-03-20 14:35:00977 if (view_)
[email protected]e85165c642014-06-10 14:34:31978 view_->SetOverscrollControllerEnabled(CanOverscrollContent());
Joel Einbinder848001c2017-09-19 21:39:31979 if (GetRenderViewHost())
980 RenderFrameDevToolsAgentHost::WebContentsCreated(this);
[email protected]a6b73c62013-02-11 23:05:08981 }
[email protected]be1f56ab2011-12-22 06:55:31982}
983
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:41984RenderFrameHostImpl* WebContentsImpl::GetMainFrame() {
[email protected]94d0cc12013-12-18 00:07:41985 return frame_tree_.root()->current_frame_host();
[email protected]60eca4eb2013-12-06 00:02:16986}
987
nick53d5cbf2015-04-23 22:50:14988RenderFrameHostImpl* WebContentsImpl::GetFocusedFrame() {
989 FrameTreeNode* focused_node = frame_tree_.GetFocusedFrame();
990 if (!focused_node)
991 return nullptr;
992 return focused_node->current_frame_host();
[email protected]9c9343b2014-03-08 02:56:07993}
994
rob3e2a0732016-01-06 21:22:09995RenderFrameHostImpl* WebContentsImpl::FindFrameByFrameTreeNodeId(
creisf71a2632017-05-04 19:03:50996 int frame_tree_node_id,
997 int process_id) {
998 FrameTreeNode* frame = frame_tree_.FindByID(frame_tree_node_id);
999
1000 // Sanity check that this is in the caller's expected process. Otherwise a
1001 // recent cross-process navigation may have led to a privilege change that the
1002 // caller is not expecting.
1003 if (!frame ||
1004 frame->current_frame_host()->GetProcess()->GetID() != process_id)
1005 return nullptr;
1006
1007 return frame->current_frame_host();
1008}
1009
1010RenderFrameHostImpl* WebContentsImpl::UnsafeFindFrameByFrameTreeNodeId(
rob3e2a0732016-01-06 21:22:091011 int frame_tree_node_id) {
creisf71a2632017-05-04 19:03:501012 // Beware using this! The RenderFrameHost may have changed since the caller
1013 // obtained frame_tree_node_id.
rob3e2a0732016-01-06 21:22:091014 FrameTreeNode* frame = frame_tree_.FindByID(frame_tree_node_id);
1015 return frame ? frame->current_frame_host() : nullptr;
1016}
1017
[email protected]a86c0e962013-12-17 17:10:391018void WebContentsImpl::ForEachFrame(
Lei Zhangebcc6302018-01-12 19:46:451019 const base::RepeatingCallback<void(RenderFrameHost*)>& on_frame) {
dcheng57e39e22016-01-21 00:25:381020 for (FrameTreeNode* node : frame_tree_.Nodes()) {
1021 on_frame.Run(node->current_frame_host());
1022 }
[email protected]a86c0e962013-12-17 17:10:391023}
1024
dchengafb53e22016-02-04 08:11:081025std::vector<RenderFrameHost*> WebContentsImpl::GetAllFrames() {
1026 std::vector<RenderFrameHost*> frame_hosts;
1027 for (FrameTreeNode* node : frame_tree_.Nodes())
1028 frame_hosts.push_back(node->current_frame_host());
1029 return frame_hosts;
1030}
1031
lukaszacbdf52e2016-01-15 21:19:511032int WebContentsImpl::SendToAllFrames(IPC::Message* message) {
1033 int number_of_messages = 0;
dchengafb53e22016-02-04 08:11:081034 for (RenderFrameHost* rfh : GetAllFrames()) {
lukasza1a9ab2422016-03-02 18:47:251035 if (!rfh->IsRenderFrameLive())
1036 continue;
1037
dchengafb53e22016-02-04 08:11:081038 ++number_of_messages;
1039 IPC::Message* message_copy = new IPC::Message(*message);
1040 message_copy->set_routing_id(rfh->GetRoutingID());
1041 rfh->Send(message_copy);
1042 }
[email protected]a86c0e962013-12-17 17:10:391043 delete message;
lukaszacbdf52e2016-01-15 21:19:511044 return number_of_messages;
[email protected]a86c0e962013-12-17 17:10:391045}
1046
lfgdb5c4ed2016-03-04 23:09:071047void WebContentsImpl::SendPageMessage(IPC::Message* msg) {
alexmos136fd6e62016-08-15 20:58:411048 frame_tree_.root()->render_manager()->SendPageMessage(msg, nullptr);
lfgdb5c4ed2016-03-04 23:09:071049}
1050
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411051RenderViewHostImpl* WebContentsImpl::GetRenderViewHost() {
[email protected]fa944cb82013-11-15 17:51:211052 return GetRenderManager()->current_host();
[email protected]be1f56ab2011-12-22 06:55:311053}
1054
creis89a0f782015-05-27 16:13:171055void WebContentsImpl::CancelActiveAndPendingDialogs() {
avi5d3b8692016-10-12 22:00:461056 if (dialog_manager_) {
avi6879fcf2017-01-21 05:27:531057 dialog_manager_->CancelDialogs(this, /*reset_state=*/false);
avi5d3b8692016-10-12 22:00:461058 }
creis89a0f782015-05-27 16:13:171059 if (browser_plugin_embedder_)
1060 browser_plugin_embedder_->CancelGuestDialogs();
1061}
1062
naskoc0fceff2015-04-30 15:53:521063void WebContentsImpl::ClosePage() {
1064 GetRenderViewHost()->ClosePage();
1065}
1066
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411067RenderWidgetHostView* WebContentsImpl::GetRenderWidgetHostView() {
[email protected]fa944cb82013-11-15 17:51:211068 return GetRenderManager()->GetRenderWidgetHostView();
[email protected]be1f56ab2011-12-22 06:55:311069}
1070
lfg265a2672016-04-23 03:11:021071RenderWidgetHostView* WebContentsImpl::GetTopLevelRenderWidgetHostView() {
1072 if (GetOuterWebContents())
1073 return GetOuterWebContents()->GetTopLevelRenderWidgetHostView();
1074 return GetRenderManager()->GetRenderWidgetHostView();
1075}
1076
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411077RenderWidgetHostView* WebContentsImpl::GetFullscreenRenderWidgetHostView() {
vmpstr6d9996c82017-02-23 00:43:251078 if (auto* widget_host = GetFullscreenRenderWidgetHost())
ekaramadaeb3c5c2016-11-09 03:22:351079 return widget_host->GetView();
1080 return nullptr;
[email protected]4aebbca2013-09-17 22:26:491081}
1082
[email protected]8ff00d72012-10-23 19:12:211083WebContentsView* WebContentsImpl::GetView() const {
[email protected]be1f56ab2011-12-22 06:55:311084 return view_.get();
1085}
1086
leon.han552e9de2017-02-09 14:37:301087void WebContentsImpl::OnScreenOrientationChange() {
1088 DCHECK(screen_orientation_provider_);
Becca Hughese9e27952018-06-25 17:08:271089 screen_orientation_provider_->OnOrientationChange();
leon.hane4db177a2017-02-07 14:19:161090}
1091
Alan Cutter92d59252019-03-29 02:32:331092base::Optional<SkColor> WebContentsImpl::GetThemeColor() {
Carlos Caballero1215f882019-10-29 15:58:431093 return GetRenderViewHost()->theme_color();
yusufod41c5f92015-03-06 00:14:281094}
1095
Doug Turner63f3c7b2017-07-29 05:10:011096void WebContentsImpl::SetAccessibilityMode(ui::AXMode mode) {
[email protected]95640212014-07-26 18:14:301097 if (mode == accessibility_mode_)
1098 return;
1099
dmazzonif8a31042016-12-03 00:52:131100 // Don't allow accessibility to be enabled for WebContents that are never
1101 // visible, like background pages.
1102 if (IsNeverVisible())
1103 return;
1104
[email protected]95640212014-07-26 18:14:301105 accessibility_mode_ = mode;
dcheng57e39e22016-01-21 00:25:381106
1107 for (FrameTreeNode* node : frame_tree_.Nodes()) {
dmazzonif8a31042016-12-03 00:52:131108 UpdateAccessibilityModeOnFrame(node->current_frame_host());
clamy610c63b32017-12-22 15:05:181109 // Also update accessibility mode on the speculative RenderFrameHost for
1110 // this FrameTreeNode, if one exists.
Alex Moshchuk3d8a1f392017-10-24 19:01:261111 RenderFrameHost* speculative_frame_host =
1112 node->render_manager()->speculative_frame_host();
1113 if (speculative_frame_host)
1114 UpdateAccessibilityModeOnFrame(speculative_frame_host);
dcheng57e39e22016-01-21 00:25:381115 }
[email protected]95640212014-07-26 18:14:301116}
1117
Doug Turner63f3c7b2017-07-29 05:10:011118void WebContentsImpl::AddAccessibilityMode(ui::AXMode mode) {
1119 ui::AXMode new_mode(accessibility_mode_);
dougtcd3dad732017-03-14 03:26:231120 new_mode |= mode;
1121 SetAccessibilityMode(new_mode);
[email protected]95640212014-07-26 18:14:301122}
1123
Avi Drissman1e620f32018-03-16 13:57:291124// Helper class used by WebContentsImpl::RequestAXTreeSnapshot.
1125// Handles the callbacks from parallel snapshot requests to each frame,
1126// and feeds the results to an AXTreeCombiner, which converts them into a
1127// single combined accessibility tree.
1128class WebContentsImpl::AXTreeSnapshotCombiner
1129 : public base::RefCounted<AXTreeSnapshotCombiner> {
1130 public:
1131 explicit AXTreeSnapshotCombiner(AXTreeSnapshotCallback callback)
1132 : callback_(std::move(callback)) {}
1133
1134 AXTreeSnapshotCallback AddFrame(bool is_root) {
1135 // Adds a reference to |this|.
1136 return base::BindOnce(&AXTreeSnapshotCombiner::ReceiveSnapshot, this,
1137 is_root);
1138 }
1139
1140 void ReceiveSnapshot(bool is_root, const ui::AXTreeUpdate& snapshot) {
1141 combiner_.AddTree(snapshot, is_root);
1142 }
1143
1144 private:
1145 friend class base::RefCounted<AXTreeSnapshotCombiner>;
1146
1147 // This is called automatically after the last call to ReceiveSnapshot
1148 // when there are no more references to this object.
1149 ~AXTreeSnapshotCombiner() {
1150 combiner_.Combine();
1151 std::move(callback_).Run(combiner_.combined());
1152 }
1153
1154 ui::AXTreeCombiner combiner_;
1155 AXTreeSnapshotCallback callback_;
1156};
1157
1158void WebContentsImpl::RequestAXTreeSnapshot(AXTreeSnapshotCallback callback,
1159 ui::AXMode ax_mode) {
dmazzonid95ae842016-04-12 21:17:391160 // Send a request to each of the frames in parallel. Each one will return
1161 // an accessibility tree snapshot, and AXTreeSnapshotCombiner will combine
1162 // them into a single tree and call |callback| with that result, then
1163 // delete |combiner|.
Avi Drissman1e620f32018-03-16 13:57:291164 FrameTreeNode* root_node = frame_tree_.root();
tzik58faa742018-08-01 10:54:181165 auto combiner =
1166 base::MakeRefCounted<AXTreeSnapshotCombiner>(std::move(callback));
Avi Drissman1e620f32018-03-16 13:57:291167
tzik58faa742018-08-01 10:54:181168 RecursiveRequestAXTreeSnapshotOnFrame(root_node, combiner.get(), ax_mode);
Avi Drissman1e620f32018-03-16 13:57:291169}
1170
1171void WebContentsImpl::RecursiveRequestAXTreeSnapshotOnFrame(
1172 FrameTreeNode* root_node,
1173 AXTreeSnapshotCombiner* combiner,
1174 ui::AXMode ax_mode) {
dmazzonid95ae842016-04-12 21:17:391175 for (FrameTreeNode* frame_tree_node : frame_tree_.Nodes()) {
Avi Drissman1e620f32018-03-16 13:57:291176 WebContentsImpl* inner_contents =
1177 node_.GetInnerWebContentsInFrame(frame_tree_node);
1178 if (inner_contents) {
1179 inner_contents->RecursiveRequestAXTreeSnapshotOnFrame(root_node, combiner,
1180 ax_mode);
1181 } else {
1182 bool is_root = frame_tree_node == root_node;
1183 frame_tree_node->current_frame_host()->RequestAXTreeSnapshot(
1184 combiner->AddFrame(is_root), ax_mode);
1185 }
dmazzonid95ae842016-04-12 21:17:391186 }
dmazzoni83ba5c82015-04-14 07:11:511187}
1188
Becca Hughes3b5a43482018-07-17 22:31:551189void WebContentsImpl::NotifyViewportFitChanged(
1190 blink::mojom::ViewportFit value) {
1191 for (auto& observer : observers_)
1192 observer.ViewportFitChanged(value);
1193}
1194
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411195FindRequestManager* WebContentsImpl::GetFindRequestManagerForTesting() {
Ehsan Karamad6beb2ea2018-11-25 18:15:131196 return GetFindRequestManager();
1197}
1198
mcnee336ea2c2017-05-23 22:50:591199#if !defined(OS_ANDROID)
akabac6bd1212018-06-25 20:10:481200void WebContentsImpl::UpdateZoom() {
1201 RenderWidgetHostImpl* rwh = GetRenderViewHost()->GetWidget();
1202 if (rwh->GetView())
1203 rwh->SynchronizeVisualProperties();
wjmaclean64951902016-04-29 20:59:121204}
1205
wjmaclean64951902016-04-29 20:59:121206
1207void WebContentsImpl::UpdateZoomIfNecessary(const std::string& scheme,
akabac6bd1212018-06-25 20:10:481208 const std::string& host) {
wjmaclean64951902016-04-29 20:59:121209 NavigationEntry* entry = GetController().GetLastCommittedEntry();
1210 if (!entry)
1211 return;
1212
1213 GURL url = HostZoomMap::GetURLFromEntry(entry);
1214 if (host != net::GetHostOrSpecFromURL(url) ||
1215 (!scheme.empty() && !url.SchemeIs(scheme))) {
1216 return;
1217 }
1218
akabac6bd1212018-06-25 20:10:481219 UpdateZoom();
wjmaclean64951902016-04-29 20:59:121220}
mcnee336ea2c2017-05-23 22:50:591221#endif // !defined(OS_ANDROID)
wjmaclean64951902016-04-29 20:59:121222
rockotf62002a2016-09-15 00:08:591223base::Closure WebContentsImpl::AddBindingSet(
1224 const std::string& interface_name,
1225 WebContentsBindingSet* binding_set) {
1226 auto result =
1227 binding_sets_.insert(std::make_pair(interface_name, binding_set));
1228 DCHECK(result.second);
1229 return base::Bind(&WebContentsImpl::RemoveBindingSet,
1230 weak_factory_.GetWeakPtr(), interface_name);
1231}
1232
rockot8bbad222017-03-22 04:34:051233WebContentsBindingSet* WebContentsImpl::GetBindingSet(
1234 const std::string& interface_name) {
1235 auto it = binding_sets_.find(interface_name);
1236 if (it == binding_sets_.end())
1237 return nullptr;
1238 return it->second;
1239}
1240
paulmeyerfeafc2d2017-04-25 21:46:401241std::vector<WebContentsImpl*> WebContentsImpl::GetWebContentsAndAllInner() {
1242 std::vector<WebContentsImpl*> all_contents(1, this);
1243
1244 for (size_t i = 0; i != all_contents.size(); ++i) {
1245 for (auto* inner_contents : all_contents[i]->GetInnerWebContents()) {
Lucas Furukawa Gadani2ec00c82018-12-14 15:53:161246 all_contents.push_back(static_cast<WebContentsImpl*>(inner_contents));
paulmeyerfeafc2d2017-04-25 21:46:401247 }
1248 }
1249
1250 return all_contents;
1251}
1252
Sam McNally145fb172017-05-10 00:13:231253void WebContentsImpl::NotifyManifestUrlChanged(
1254 const base::Optional<GURL>& manifest_url) {
1255 for (auto& observer : observers_)
1256 observer.DidUpdateWebManifestURL(manifest_url);
1257}
1258
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411259WebUI* WebContentsImpl::GetWebUI() {
Nasko Oskov2f7d2112019-10-29 03:26:571260 WebUI* committed_web_ui = GetCommittedWebUI();
1261 if (committed_web_ui)
1262 return committed_web_ui;
1263
1264 if (GetRenderManager()->speculative_frame_host())
1265 return GetRenderManager()->speculative_frame_host()->web_ui();
1266
1267 return nullptr;
[email protected]be1f56ab2011-12-22 06:55:311268}
1269
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411270WebUI* WebContentsImpl::GetCommittedWebUI() {
carlosk35f35af2015-12-01 10:55:401271 return frame_tree_.root()->current_frame_host()->web_ui();
[email protected]d5f942ba2008-09-26 19:30:341272}
1273
Changwan Ryuc1134a82018-03-07 02:10:141274void WebContentsImpl::SetUserAgentOverride(const std::string& override,
1275 bool override_in_new_tabs) {
[email protected]bf70edce2012-06-20 22:32:221276 if (GetUserAgentOverride() == override)
1277 return;
1278
Changwan Ryuc1134a82018-03-07 02:10:141279 should_override_user_agent_in_new_tabs_ = override_in_new_tabs;
1280
[email protected]bf70edce2012-06-20 22:32:221281 renderer_preferences_.user_agent_override = override;
1282
Bruce Long1e3e1f542019-10-16 17:56:281283 // Send the new override string to all renderers in the current page.
1284 SyncRendererPrefs();
[email protected]bf70edce2012-06-20 22:32:221285
1286 // Reload the page if a load is currently in progress to avoid having
1287 // different parts of the page loaded using different user agents.
[email protected]6286a372013-10-09 04:03:271288 NavigationEntry* entry = controller_.GetVisibleEntry();
Ivan Kotenkov2c0d2bb32017-11-01 15:41:281289 if (IsLoading() && entry != nullptr && entry->GetIsOverridingUserAgent())
toyoshim6142d96f2016-12-19 09:07:251290 controller_.Reload(ReloadType::BYPASSING_CACHE, true);
[email protected]8d0f3312012-08-18 01:47:531291
ericwilligersde386342016-10-19 02:35:091292 for (auto& observer : observers_)
1293 observer.UserAgentOverrideSet(override);
[email protected]86ef6a392012-05-11 22:03:111294}
1295
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411296const std::string& WebContentsImpl::GetUserAgentOverride() {
[email protected]bf70edce2012-06-20 22:32:221297 return renderer_preferences_.user_agent_override;
[email protected]86ef6a392012-05-11 22:03:111298}
1299
Changwan Ryuc1134a82018-03-07 02:10:141300bool WebContentsImpl::ShouldOverrideUserAgentInNewTabs() {
1301 return should_override_user_agent_in_new_tabs_;
1302}
1303
dmazzonidd3d51a72016-12-14 18:41:011304void WebContentsImpl::EnableWebContentsOnlyAccessibilityMode() {
Dominic Mazzonid9fda172019-03-07 17:12:071305 // If accessibility is already enabled, we'll need to force a reset
1306 // in order to ensure new observers of accessibility events get the
1307 // full accessibility tree from scratch.
1308 bool need_reset = GetAccessibilityMode().has_mode(ui::AXMode::kWebContents);
1309
1310 ui::AXMode desired_mode =
1311 GetContentClient()->browser()->GetAXModeForBrowserContext(
1312 GetBrowserContext());
1313 desired_mode |= ui::kAXModeWebContentsOnly;
1314 AddAccessibilityMode(desired_mode);
1315
1316 if (need_reset) {
dchengafb53e22016-02-04 08:11:081317 for (RenderFrameHost* rfh : GetAllFrames())
1318 ResetAccessibility(rfh);
dchengafb53e22016-02-04 08:11:081319 }
[email protected]95640212014-07-26 18:14:301320}
1321
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411322bool WebContentsImpl::IsWebContentsOnlyAccessibilityModeForTesting() {
Doug Turner63f3c7b2017-07-29 05:10:011323 return accessibility_mode_ == ui::kAXModeWebContentsOnly;
[email protected]95640212014-07-26 18:14:301324}
1325
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411326bool WebContentsImpl::IsFullAccessibilityModeForTesting() {
Doug Turner63f3c7b2017-07-29 05:10:011327 return accessibility_mode_ == ui::kAXModeComplete;
[email protected]95640212014-07-26 18:14:301328}
1329
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411330const PageImportanceSignals& WebContentsImpl::GetPageImportanceSignals() {
kouhei40f03cb2015-09-24 07:47:011331 return page_importance_signals_;
1332}
1333
Becca Hughes6daf5662018-06-27 16:50:541334#if defined(OS_ANDROID)
1335
1336void WebContentsImpl::SetDisplayCutoutSafeArea(gfx::Insets insets) {
Becca Hughes86334622018-07-09 21:29:381337 if (display_cutout_host_impl_)
1338 display_cutout_host_impl_->SetDisplayCutoutSafeArea(insets);
Becca Hughes6daf5662018-06-27 16:50:541339}
1340
1341#endif
1342
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411343const base::string16& WebContentsImpl::GetTitle() {
[email protected]4c6092c5b2009-06-06 00:23:551344 // Transient entries take precedence. They are used for interstitial pages
1345 // that are shown on top of existing pages.
[email protected]10f417c52011-12-28 21:04:231346 NavigationEntry* entry = controller_.GetTransientEntry();
[email protected]45d0ef7f2011-01-05 13:46:231347 if (entry) {
jshin1fb76462016-04-05 22:13:031348 return entry->GetTitleForDisplay();
[email protected]45d0ef7f2011-01-05 13:46:231349 }
carlosk35f35af2015-12-01 10:55:401350
Nasko Oskov2f7d2112019-10-29 03:26:571351 WebUI* our_web_ui =
1352 GetRenderManager()->speculative_frame_host()
1353 ? GetRenderManager()->speculative_frame_host()->web_ui()
1354 : GetRenderManager()->current_frame_host()->web_ui();
[email protected]7ade2732011-02-10 00:13:581355 if (our_web_ui) {
[email protected]96d185d2009-04-24 03:28:541356 // Don't override the title in view source mode.
[email protected]59167c22013-06-03 18:07:321357 entry = controller_.GetVisibleEntry();
[email protected]96d185d2009-04-24 03:28:541358 if (!(entry && entry->IsViewSourceMode())) {
[email protected]e0112912011-02-02 22:54:351359 // Give the Web UI the chance to override our title.
[email protected]fcf75d42013-12-03 20:11:261360 const base::string16& title = our_web_ui->GetOverriddenTitle();
[email protected]96d185d2009-04-24 03:28:541361 if (!title.empty())
1362 return title;
1363 }
1364 }
1365
1366 // We use the title for the last committed entry rather than a pending
1367 // navigation entry. For example, when the user types in a URL, we want to
1368 // keep the old page's title until the new load has committed and we get a new
1369 // title.
[email protected]96d185d2009-04-24 03:28:541370 entry = controller_.GetLastCommittedEntry();
[email protected]59167c22013-06-03 18:07:321371
creis072b61f2015-09-28 20:52:241372 // We make an exception for initial navigations. We only want to use the title
1373 // from the visible entry if:
1374 // 1. The pending entry has been explicitly assigned a title to display.
1375 // 2. The user is doing a history navigation in a new tab (e.g., Ctrl+Back),
1376 // which case there is a pending entry index other than -1.
1377 //
1378 // Otherwise, we want to stick with the last committed entry's title during
1379 // new navigations, which have pending entries at index -1 with no title.
1380 if (controller_.IsInitialNavigation() &&
1381 ((controller_.GetVisibleEntry() &&
1382 !controller_.GetVisibleEntry()->GetTitle().empty()) ||
1383 controller_.GetPendingEntryIndex() != -1)) {
1384 entry = controller_.GetVisibleEntry();
[email protected]9eeafc012013-11-25 23:49:471385 }
[email protected]59167c22013-06-03 18:07:321386
[email protected]45d0ef7f2011-01-05 13:46:231387 if (entry) {
jshin1fb76462016-04-05 22:13:031388 return entry->GetTitleForDisplay();
[email protected]45d0ef7f2011-01-05 13:46:231389 }
[email protected]987fc3a2011-05-26 14:18:091390
1391 // |page_title_when_no_navigation_entry_| is finally used
1392 // if no title cannot be retrieved.
1393 return page_title_when_no_navigation_entry_;
[email protected]96d185d2009-04-24 03:28:541394}
1395
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411396SiteInstanceImpl* WebContentsImpl::GetSiteInstance() {
[email protected]fa944cb82013-11-15 17:51:211397 return GetRenderManager()->current_host()->GetSiteInstance();
[email protected]96d185d2009-04-24 03:28:541398}
1399
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411400bool WebContentsImpl::IsLoading() {
clamy44e84ce2016-02-22 15:38:251401 return frame_tree_.IsLoading() &&
creisadca9862017-06-16 19:12:111402 !(ShowingInterstitialPage() && interstitial_page_->pause_throbber());
[email protected]3c9e1872010-11-18 16:17:491403}
1404
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411405double WebContentsImpl::GetLoadProgress() {
Peter Boström18a40fa2018-10-31 19:03:501406 return frame_tree_.load_progress();
1407}
1408
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411409bool WebContentsImpl::IsLoadingToDifferentDocument() {
clamy44e84ce2016-02-22 15:38:251410 return IsLoading() && is_load_to_different_document_;
[email protected]6dfed692014-05-22 04:18:031411}
1412
Alexander Timindd0fa0c52019-11-06 13:03:371413bool WebContentsImpl::IsDocumentOnLoadCompletedInMainFrame() {
1414 return GetRenderViewHost()->IsDocumentOnLoadCompletedInMainFrame();
1415}
1416
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411417bool WebContentsImpl::IsWaitingForResponse() {
Peter Boströma9a27f6e2018-11-21 13:12:191418 NavigationRequest* ongoing_navigation_request =
1419 frame_tree_.root()->navigation_request();
1420
1421 // An ongoing navigation request means we're waiting for a response.
1422 return ongoing_navigation_request != nullptr;
[email protected]be1f56ab2011-12-22 06:55:311423}
1424
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411425const net::LoadStateWithParam& WebContentsImpl::GetLoadState() {
[email protected]be1f56ab2011-12-22 06:55:311426 return load_state_;
1427}
1428
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411429const base::string16& WebContentsImpl::GetLoadStateHost() {
[email protected]be1f56ab2011-12-22 06:55:311430 return load_state_host_;
1431}
1432
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411433uint64_t WebContentsImpl::GetUploadSize() {
[email protected]be1f56ab2011-12-22 06:55:311434 return upload_size_;
1435}
1436
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411437uint64_t WebContentsImpl::GetUploadPosition() {
[email protected]be1f56ab2011-12-22 06:55:311438 return upload_position_;
1439}
1440
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411441const std::string& WebContentsImpl::GetEncoding() {
Alexander Timine5f20e22019-10-09 23:22:351442 return GetMainFrame()->GetEncoding();
[email protected]be1f56ab2011-12-22 06:55:311443}
1444
Charles Zhaod72f99d2018-09-17 14:18:271445bool WebContentsImpl::WasDiscarded() {
1446 return GetFrameTree()->root()->was_discarded();
1447}
1448
Shubhie Panickerddf2a4e2018-03-06 00:09:061449void WebContentsImpl::SetWasDiscarded(bool was_discarded) {
1450 GetFrameTree()->root()->set_was_discarded();
1451}
1452
[email protected]222f5822014-02-05 23:40:491453void WebContentsImpl::IncrementCapturerCount(const gfx::Size& capture_size) {
[email protected]5a652232013-02-12 06:15:251454 DCHECK(!is_being_destroyed_);
Francois Doraye6161152018-03-27 22:05:371455 const bool was_captured = IsBeingCaptured();
[email protected]54597982013-02-06 01:59:551456 ++capturer_count_;
1457 DVLOG(1) << "There are now " << capturer_count_
1458 << " capturing(s) of WebContentsImpl@" << this;
[email protected]222f5822014-02-05 23:40:491459
1460 // Note: This provides a hint to upstream code to size the views optimally
1461 // for quality (e.g., to avoid scaling).
1462 if (!capture_size.IsEmpty() && preferred_size_for_capture_.IsEmpty()) {
1463 preferred_size_for_capture_ = capture_size;
1464 OnPreferredSizeChanged(preferred_size_);
1465 }
ccameron8807c38f2015-01-17 00:29:191466
Collin Baker989e0882019-11-01 01:27:171467 if (!was_captured)
1468 UpdateVisibilityAndNotifyPageAndView(GetVisibility());
[email protected]54597982013-02-06 01:59:551469}
1470
1471void WebContentsImpl::DecrementCapturerCount() {
1472 --capturer_count_;
1473 DVLOG(1) << "There are now " << capturer_count_
1474 << " capturing(s) of WebContentsImpl@" << this;
1475 DCHECK_LE(0, capturer_count_);
1476
[email protected]5a652232013-02-12 06:15:251477 if (is_being_destroyed_)
1478 return;
1479
Francois Doray24fc62c2017-12-11 17:27:331480 if (!IsBeingCaptured()) {
[email protected]222f5822014-02-05 23:40:491481 const gfx::Size old_size = preferred_size_for_capture_;
1482 preferred_size_for_capture_ = gfx::Size();
1483 OnPreferredSizeChanged(old_size);
Collin Baker989e0882019-11-01 01:27:171484 UpdateVisibilityAndNotifyPageAndView(GetVisibility());
[email protected]54597982013-02-06 01:59:551485 }
[email protected]be1f56ab2011-12-22 06:55:311486}
1487
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411488bool WebContentsImpl::IsBeingCaptured() {
Francois Doray24fc62c2017-12-11 17:27:331489 return capturer_count_ > 0;
[email protected]f2bd40812013-07-20 04:30:441490}
1491
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411492bool WebContentsImpl::IsAudioMuted() {
Max Morinb5506f72018-05-16 07:33:101493 if (base::FeatureList::IsEnabled(features::kAudioServiceAudioStreams)) {
1494 return audio_stream_factory_ && audio_stream_factory_->IsMuted();
1495 }
miu50f97892014-09-22 22:49:521496 return audio_muter_.get() && audio_muter_->is_muting();
1497}
1498
1499void WebContentsImpl::SetAudioMuted(bool mute) {
1500 DVLOG(1) << "SetAudioMuted(mute=" << mute << "), was " << IsAudioMuted()
1501 << " for WebContentsImpl@" << this;
1502
1503 if (mute == IsAudioMuted())
1504 return;
1505
Max Morinb5506f72018-05-16 07:33:101506 if (base::FeatureList::IsEnabled(features::kAudioServiceAudioStreams)) {
1507 GetAudioStreamFactory()->SetMuted(mute);
miu50f97892014-09-22 22:49:521508 } else {
Max Morinb5506f72018-05-16 07:33:101509 if (mute) {
1510 if (!audio_muter_)
1511 audio_muter_.reset(new WebContentsAudioMuter(this));
1512 audio_muter_->StartMuting();
1513 } else {
1514 DCHECK(audio_muter_);
1515 audio_muter_->StopMuting();
1516 }
miu50f97892014-09-22 22:49:521517 }
1518
ericwilligersde386342016-10-19 02:35:091519 for (auto& observer : observers_)
1520 observer.DidUpdateAudioMutingState(mute);
wjmaclean2f797c782016-01-07 14:52:031521
Jan Rucka442c83f2017-08-08 13:27:541522 // Notification for UI updates in response to the changed muting state.
Collin Bakeraf540cf2019-09-20 20:54:161523 NotifyNavigationStateChanged(INVALIDATE_TYPE_AUDIO);
Jan Rucka442c83f2017-08-08 13:27:541524}
1525
1526bool WebContentsImpl::IsCurrentlyAudible() {
Chris Hamiltondf0d72cd2018-05-29 16:23:531527 return is_currently_audible_;
miu50f97892014-09-22 22:49:521528}
1529
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411530bool WebContentsImpl::IsConnectedToBluetoothDevice() {
ortunodf4d7982016-04-08 02:33:351531 return bluetooth_connected_device_count_ > 0;
ortuno32e7db3c2016-03-29 16:14:201532}
1533
Lucas Furukawa Gadani4b4eed02019-06-04 23:12:041534bool WebContentsImpl::IsConnectedToSerialPort() {
Reilly Grant5e7c79b22019-04-09 17:26:201535 return serial_active_frame_count_ > 0;
1536}
1537
Matt Reynoldse8c6c1f2019-11-02 09:53:531538bool WebContentsImpl::IsConnectedToHidDevice() {
1539 return hid_active_frame_count_ > 0;
1540}
1541
Marijn Kruisselbrink29051042019-08-06 22:56:551542bool WebContentsImpl::HasNativeFileSystemHandles() {
1543 return native_file_system_handle_count_ > 0;
1544}
Marijn Kruisselbrinkf1714aa22019-07-02 03:45:331545bool WebContentsImpl::HasNativeFileSystemDirectoryHandles() {
Marijn Kruisselbrink46fa4bd2019-07-11 18:15:471546 return !native_file_system_directory_handles_.empty();
1547}
1548
1549std::vector<base::FilePath>
1550WebContentsImpl::GetNativeFileSystemDirectoryHandles() {
1551 std::vector<base::FilePath> result;
1552 result.reserve(native_file_system_directory_handles_.size());
1553 for (auto const& entry : native_file_system_directory_handles_)
1554 result.push_back(entry.first);
1555 return result;
Marijn Kruisselbrinkf1714aa22019-07-02 03:45:331556}
1557
1558bool WebContentsImpl::HasWritableNativeFileSystemHandles() {
1559 return native_file_system_writable_handle_count_ > 0;
1560}
1561
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411562bool WebContentsImpl::HasPictureInPictureVideo() {
sawtellea7333a82018-05-31 02:36:361563 return has_picture_in_picture_video_;
1564}
1565
1566void WebContentsImpl::SetHasPictureInPictureVideo(
1567 bool has_picture_in_picture_video) {
1568 // If status of |this| is already accurate, there is no need to update.
1569 if (has_picture_in_picture_video == has_picture_in_picture_video_)
1570 return;
1571 has_picture_in_picture_video_ = has_picture_in_picture_video;
1572 NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB);
François Beaufort058085d2018-08-30 15:29:141573 for (auto& observer : observers_)
1574 observer.MediaPictureInPictureChanged(has_picture_in_picture_video_);
sawtellea7333a82018-05-31 02:36:361575}
1576
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411577bool WebContentsImpl::IsCrashed() {
Dominic Mazzoni73a48322018-05-25 18:14:071578 switch (crashed_status_) {
1579 case base::TERMINATION_STATUS_PROCESS_CRASHED:
1580 case base::TERMINATION_STATUS_ABNORMAL_TERMINATION:
1581 case base::TERMINATION_STATUS_PROCESS_WAS_KILLED:
1582 case base::TERMINATION_STATUS_OOM:
1583 case base::TERMINATION_STATUS_LAUNCH_FAILED:
oshima620225722015-06-04 19:45:271584#if defined(OS_CHROMEOS)
Dominic Mazzoni73a48322018-05-25 18:14:071585 case base::TERMINATION_STATUS_PROCESS_WAS_KILLED_BY_OOM:
oshima620225722015-06-04 19:45:271586#endif
Dominic Mazzoni73a48322018-05-25 18:14:071587#if defined(OS_ANDROID)
1588 case base::TERMINATION_STATUS_OOM_PROTECTED:
1589#endif
Will Harris07925d12019-10-31 03:03:051590#if defined(OS_WIN)
1591 case base::TERMINATION_STATUS_INTEGRITY_FAILURE:
1592#endif
Dominic Mazzoni73a48322018-05-25 18:14:071593 return true;
1594 case base::TERMINATION_STATUS_NORMAL_TERMINATION:
1595 case base::TERMINATION_STATUS_STILL_RUNNING:
1596 return false;
1597 case base::TERMINATION_STATUS_MAX_ENUM:
1598 NOTREACHED();
1599 return false;
1600 }
1601
1602 NOTREACHED();
1603 return false;
[email protected]3c9e1872010-11-18 16:17:491604}
1605
[email protected]b172aee2012-04-10 17:05:261606void WebContentsImpl::SetIsCrashed(base::TerminationStatus status,
1607 int error_code) {
[email protected]443b80e2010-12-14 00:42:231608 if (status == crashed_status_)
[email protected]d5f942ba2008-09-26 19:30:341609 return;
1610
[email protected]443b80e2010-12-14 00:42:231611 crashed_status_ = status;
1612 crashed_error_code_ = error_code;
[email protected]8ff00d72012-10-23 19:12:211613 NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB);
[email protected]d5f942ba2008-09-26 19:30:341614}
1615
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411616base::TerminationStatus WebContentsImpl::GetCrashedStatus() {
[email protected]be1f56ab2011-12-22 06:55:311617 return crashed_status_;
1618}
1619
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411620int WebContentsImpl::GetCrashedErrorCode() {
afakhry98241832016-03-11 19:27:471621 return crashed_error_code_;
1622}
1623
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411624bool WebContentsImpl::IsBeingDestroyed() {
[email protected]be1f56ab2011-12-22 06:55:311625 return is_being_destroyed_;
1626}
1627
[email protected]7f924832014-08-09 05:57:221628void WebContentsImpl::NotifyNavigationStateChanged(
1629 InvalidateTypes changed_flags) {
dalecurtis88c240072015-12-09 02:11:181630 // Notify the media observer of potential audibility changes.
Collin Bakeraf540cf2019-09-20 20:54:161631 if (changed_flags & INVALIDATE_TYPE_AUDIO) {
mlamouri44e4ef42016-01-20 18:42:271632 media_web_contents_observer_->MaybeUpdateAudibleState();
dalecurtisbc6572e12014-09-12 19:22:301633 }
1634
[email protected]d5f942ba2008-09-26 19:30:341635 if (delegate_)
1636 delegate_->NavigationStateChanged(this, changed_flags);
wjmaclean1dabf3e32016-05-13 23:34:441637
1638 if (GetOuterWebContents())
1639 GetOuterWebContents()->NotifyNavigationStateChanged(changed_flags);
[email protected]d5f942ba2008-09-26 19:30:341640}
1641
W. James MacLean6f34b042019-07-12 19:25:301642RenderFrameHostImpl* WebContentsImpl::GetFocusedFrameFromFocusedDelegate() {
1643 FrameTreeNode* focused_node =
1644 GetFocusedWebContents()->frame_tree_.GetFocusedFrame();
1645 return focused_node ? focused_node->current_frame_host() : nullptr;
1646}
1647
Chris Hamiltondf0d72cd2018-05-29 16:23:531648void WebContentsImpl::OnAudioStateChanged() {
1649 // This notification can come from any embedded contents or from this
1650 // WebContents' stream monitor. Aggregate these signals to get the actual
1651 // state.
1652 bool is_currently_audible =
1653 audio_stream_monitor_.IsCurrentlyAudible() ||
1654 (browser_plugin_embedder_ &&
1655 browser_plugin_embedder_->AreAnyGuestsCurrentlyAudible());
1656 if (is_currently_audible == is_currently_audible_)
1657 return;
1658
1659 // Update internal state.
1660 is_currently_audible_ = is_currently_audible;
1661 was_ever_audible_ = was_ever_audible_ || is_currently_audible_;
1662
1663 SendPageMessage(
1664 new PageMsg_AudioStateChanged(MSG_ROUTING_NONE, is_currently_audible_));
altimind8bd26c2016-11-04 11:44:541665
altiminec87fd1f2016-11-17 20:22:491666 // Notification for UI updates in response to the changed audio state.
Collin Bakeraf540cf2019-09-20 20:54:161667 NotifyNavigationStateChanged(INVALIDATE_TYPE_AUDIO);
Jan Rucka442c83f2017-08-08 13:27:541668
Chris Hamiltondf0d72cd2018-05-29 16:23:531669 // Ensure that audio state changes propagate from innermost to outermost
1670 // WebContents.
1671 if (GetOuterWebContents())
1672 GetOuterWebContents()->OnAudioStateChanged();
Tommy Steimel18360512017-11-01 00:38:191673
Chris Hamiltoneb41198f2018-05-24 18:41:261674 for (auto& observer : observers_)
Chris Hamiltondf0d72cd2018-05-29 16:23:531675 observer.OnAudioStateChanged(is_currently_audible_);
altimind8bd26c2016-11-04 11:44:541676}
1677
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411678base::TimeTicks WebContentsImpl::GetLastActiveTime() {
[email protected]9a890452014-02-13 22:21:021679 return last_active_time_;
[email protected]3e324142012-06-25 18:26:331680}
1681
[email protected]9e2e4632012-07-27 16:38:411682void WebContentsImpl::WasShown() {
Collin Baker989e0882019-11-01 01:27:171683 UpdateVisibilityAndNotifyPageAndView(Visibility::VISIBLE);
[email protected]be1f56ab2011-12-22 06:55:311684}
1685
[email protected]b172aee2012-04-10 17:05:261686void WebContentsImpl::WasHidden() {
Collin Baker989e0882019-11-01 01:27:171687 UpdateVisibilityAndNotifyPageAndView(Visibility::HIDDEN);
[email protected]375fa1b2012-05-22 22:05:371688}
1689
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411690bool WebContentsImpl::HasRecentInteractiveInputEvent() {
Daniel Cheng90196c82018-04-25 21:49:141691 static constexpr base::TimeDelta kMaxInterval =
1692 base::TimeDelta::FromSeconds(5);
1693 base::TimeDelta delta =
1694 ui::EventTimeForNow() - last_interactive_input_event_time_;
1695 // Note: the expectation is that the caller is typically expecting an input
1696 // event, e.g. validating that a WebUI message that requires a gesture is
Daniel Chenge81030b32019-07-17 20:20:231697 // actually attached to a gesture.
Daniel Cheng90196c82018-04-25 21:49:141698 return delta <= kMaxInterval;
1699}
1700
Avi Drissman738ea192018-08-29 20:24:161701void WebContentsImpl::SetIgnoreInputEvents(bool ignore_input_events) {
1702 ignore_input_events_ = ignore_input_events;
1703}
1704
Bo Liu168c8642017-08-28 18:26:021705#if defined(OS_ANDROID)
Bo Liu16dd7b8322018-05-02 21:19:501706void WebContentsImpl::SetMainFrameImportance(
1707 ChildProcessImportance importance) {
1708 GetMainFrame()->GetRenderWidgetHost()->SetImportance(importance);
Bo Liu7c6779e92017-08-16 02:02:281709 if (ShowingInterstitialPage()) {
Bo Liu16dd7b8322018-05-02 21:19:501710 static_cast<RenderFrameHostImpl*>(interstitial_page_->GetMainFrame())
1711 ->GetRenderWidgetHost()
1712 ->SetImportance(importance);
Bo Liu7c6779e92017-08-16 02:02:281713 }
Bo Liu7c6779e92017-08-16 02:02:281714}
Bo Liu168c8642017-08-28 18:26:021715#endif
Bo Liu7c6779e92017-08-16 02:02:281716
ccameron8807c38f2015-01-17 00:29:191717void WebContentsImpl::WasOccluded() {
Collin Baker989e0882019-11-01 01:27:171718 UpdateVisibilityAndNotifyPageAndView(Visibility::OCCLUDED);
ccameron8807c38f2015-01-17 00:29:191719}
1720
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411721Visibility WebContentsImpl::GetVisibility() {
Francois Doraye6161152018-03-27 22:05:371722 return visibility_;
Francois Dorayfe4a1772018-02-17 04:17:091723}
1724
Alex Moshchuk6fcaca752018-07-14 02:13:591725// TODO(alexmos): rename to NeedToFireBeforeUnloadOrUnload().
[email protected]b172aee2012-04-10 17:05:261726bool WebContentsImpl::NeedToFireBeforeUnload() {
[email protected]be1f56ab2011-12-22 06:55:311727 // TODO(creis): Should we fire even for interstitial pages?
Alex Moshchuk6fcaca752018-07-14 02:13:591728 if (ShowingInterstitialPage())
1729 return false;
1730
1731 if (!WillNotifyDisconnection())
1732 return false;
1733
1734 // Don't fire if the main frame's RenderViewHost indicates that beforeunload
1735 // and unload have already executed (e.g., after receiving a ClosePage ACK)
1736 // or should be ignored.
1737 if (GetRenderViewHost()->SuddenTerminationAllowed())
1738 return false;
1739
Alex Moshchukc78dbc722019-10-31 20:00:501740 // Check whether any frame in the frame tree needs to run beforeunload or
1741 // unload handlers.
1742 for (FrameTreeNode* node : frame_tree_.Nodes()) {
1743 RenderFrameHostImpl* rfh = node->current_frame_host();
1744
1745 // No need to run beforeunload/unload if the RenderFrame isn't live.
1746 if (!rfh->IsRenderFrameLive())
1747 continue;
1748
1749 if (rfh->GetSuddenTerminationDisablerState(blink::kBeforeUnloadHandler |
1750 blink::kUnloadHandler)) {
1751 return true;
1752 }
Aditya Keerthi34f37e62019-03-08 15:54:531753 }
Alex Moshchuk6fcaca752018-07-14 02:13:591754
Alex Moshchukc78dbc722019-10-31 20:00:501755 return false;
[email protected]be1f56ab2011-12-22 06:55:311756}
1757
Chris Hamilton3b3e315d2018-09-19 13:16:211758void WebContentsImpl::DispatchBeforeUnload(bool auto_cancel) {
1759 auto before_unload_type =
1760 auto_cancel ? RenderFrameHostImpl::BeforeUnloadType::DISCARD
1761 : RenderFrameHostImpl::BeforeUnloadType::TAB_CLOSE;
1762 GetMainFrame()->DispatchBeforeUnload(before_unload_type, false);
[email protected]1c3f80bd2014-04-08 23:02:061763}
1764
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:501765void WebContentsImpl::AttachInnerWebContents(
1766 std::unique_ptr<WebContents> inner_web_contents,
1767 RenderFrameHost* render_frame_host) {
1768 WebContentsImpl* inner_web_contents_impl =
1769 static_cast<WebContentsImpl*>(inner_web_contents.get());
1770 DCHECK(!inner_web_contents_impl->node_.outer_web_contents());
1771 auto* render_frame_host_impl =
1772 static_cast<RenderFrameHostImpl*>(render_frame_host);
1773 DCHECK_EQ(&frame_tree_,
1774 render_frame_host_impl->frame_tree_node()->frame_tree());
Lucas Furukawa Gadani105de1e812018-05-31 19:38:391775
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:501776 RenderFrameHostManager* inner_render_manager =
1777 inner_web_contents_impl->GetRenderManager();
1778 RenderFrameHostImpl* inner_main_frame =
1779 inner_render_manager->current_frame_host();
1780 RenderViewHostImpl* inner_render_view_host =
1781 inner_render_manager->current_host();
1782 auto* outer_render_manager =
1783 render_frame_host_impl->frame_tree_node()->render_manager();
lfg91a79e12016-04-01 23:52:191784
Lucas Furukawa Gadani105de1e812018-05-31 19:38:391785 // When attaching a WebContents as an inner WebContents, we need to replace
1786 // the Webcontents' view with a WebContentsViewChildFrame.
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:501787 inner_web_contents_impl->view_.reset(new WebContentsViewChildFrame(
1788 inner_web_contents_impl,
1789 GetContentClient()->browser()->GetWebContentsViewDelegate(
1790 inner_web_contents_impl),
1791 &inner_web_contents_impl->render_view_host_delegate_view_));
Lucas Furukawa Gadani105de1e812018-05-31 19:38:391792
lfg91a79e12016-04-01 23:52:191793 // When the WebContents being initialized has an opener, the browser side
1794 // Render{View,Frame}Host must be initialized and the RenderWidgetHostView
1795 // created. This is needed because the usual initialization happens during
1796 // the first navigation, but when attaching a new window we don't navigate
1797 // before attaching. If the browser side is already initialized, the calls
1798 // below will just early return.
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:501799 inner_render_manager->InitRenderView(inner_render_view_host, nullptr);
1800 inner_main_frame->Init();
1801 if (!inner_render_manager->GetRenderWidgetHostView()) {
1802 inner_web_contents_impl->CreateRenderWidgetHostViewForRenderManager(
1803 inner_render_view_host);
1804 }
lfg91a79e12016-04-01 23:52:191805
lazyboy6ec48b2a2015-06-29 15:18:141806 // Create a link to our outer WebContents.
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:501807 node_.AttachInnerWebContents(std::move(inner_web_contents),
1808 render_frame_host_impl);
lazyboy6ec48b2a2015-06-29 15:18:141809
lazyboy6ec48b2a2015-06-29 15:18:141810 // Create a proxy in top-level RenderFrameHostManager, pointing to the
1811 // SiteInstance of the outer WebContents. The proxy will be used to send
1812 // postMessage to the inner WebContents.
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:501813 auto* proxy = inner_render_manager->CreateOuterDelegateProxy(
1814 render_frame_host_impl->GetSiteInstance());
lazyboy6ec48b2a2015-06-29 15:18:141815
Lucas Furukawa Gadani99125822019-01-03 15:41:491816 // When attaching a GuestView as an inner WebContents, there should already be
1817 // a live RenderFrame, which has to be swapped. When attaching a portal, there
1818 // will not be a live RenderFrame before creating the proxy.
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:501819 if (render_frame_host_impl->IsRenderFrameLive()) {
1820 inner_render_manager->SwapOuterDelegateFrame(render_frame_host_impl, proxy);
Lucas Furukawa Gadani99125822019-01-03 15:41:491821
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:501822 inner_web_contents_impl->ReattachToOuterWebContentsFrame();
Lucas Furukawa Gadani99125822019-01-03 15:41:491823 }
ekaramadadd882292016-06-08 15:22:561824
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:501825 if (frame_tree_.GetFocusedFrame() ==
1826 render_frame_host_impl->frame_tree_node()) {
1827 inner_web_contents_impl->SetFocusedFrame(
1828 inner_web_contents_impl->frame_tree_.root(),
1829 render_frame_host_impl->GetSiteInstance());
avallee7529b2a2017-04-26 11:37:511830 }
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:501831 outer_render_manager->set_attach_complete();
lazyboy6ec48b2a2015-06-29 15:18:141832}
1833
Lucas Furukawa Gadani99125822019-01-03 15:41:491834std::unique_ptr<WebContents> WebContentsImpl::DetachFromOuterWebContents() {
1835 DCHECK(node_.outer_web_contents());
1836 if (RenderWidgetHostViewBase* view =
1837 static_cast<RenderWidgetHostViewBase*>(GetMainFrame()->GetView())) {
1838 view->Destroy();
1839 }
1840 GetRenderManager()->DeleteOuterDelegateProxy(
1841 node_.OuterContentsFrameTreeNode()
1842 ->current_frame_host()
1843 ->GetSiteInstance());
1844 view_.reset(CreateWebContentsView(
1845 this, GetContentClient()->browser()->GetWebContentsViewDelegate(this),
1846 &render_view_host_delegate_view_));
danakjfc5184932019-09-12 18:08:321847 view_->CreateView(nullptr);
Lucas Furukawa Gadani99125822019-01-03 15:41:491848 std::unique_ptr<WebContents> web_contents =
1849 node_.DisconnectFromOuterWebContents();
1850 DCHECK_EQ(web_contents.get(), this);
1851 node_.SetFocusedWebContents(this);
1852 CreateRenderWidgetHostViewForRenderManager(GetRenderViewHost());
1853 return web_contents;
1854}
1855
W. James MacLean2dc75ed42019-03-06 14:31:091856void WebContentsImpl::RecursivelyRegisterFrameSinkIds() {
1857 auto* view = static_cast<RenderWidgetHostViewBase*>(
1858 GetRenderManager()->GetRenderWidgetHostView());
1859 DCHECK(view);
1860 if (!view->IsRenderWidgetHostViewChildFrame())
1861 return;
1862 static_cast<RenderWidgetHostViewChildFrame*>(view)->RegisterFrameSinkId();
1863
1864 for (auto* inner_web_contents : node_.GetInnerWebContents()) {
1865 static_cast<WebContentsImpl*>(inner_web_contents)
1866 ->RecursivelyRegisterFrameSinkIds();
1867 }
1868}
1869
Lucas Furukawa Gadaniaed1fed2017-10-13 17:34:141870void WebContentsImpl::ReattachToOuterWebContentsFrame() {
1871 DCHECK(node_.outer_web_contents());
1872 auto* render_manager = GetRenderManager();
1873 auto* parent_frame =
1874 node_.OuterContentsFrameTreeNode()->current_frame_host()->GetParent();
1875 render_manager->SetRWHViewForInnerContents(
1876 render_manager->GetRenderWidgetHostView());
1877
W. James MacLean2dc75ed42019-03-06 14:31:091878 RecursivelyRegisterFrameSinkIds();
Lucas Furukawa Gadaniaed1fed2017-10-13 17:34:141879
1880 // Set up the the guest's AX tree to point back at the embedder's AX tree.
1881 GetMainFrame()->set_browser_plugin_embedder_ax_tree_id(
1882 parent_frame->GetAXTreeID());
1883 GetMainFrame()->UpdateAXTreeData();
1884}
1885
dalecurtis6c58ed02016-10-28 23:02:371886void WebContentsImpl::DidChangeVisibleSecurityState() {
Emily Starkc663b0242019-04-26 19:34:451887 if (delegate_)
dalecurtis6c58ed02016-10-28 23:02:371888 delegate_->VisibleSecurityStateChanged(this);
Emily Starkc663b0242019-04-26 19:34:451889 for (auto& observer : observers_)
1890 observer.DidChangeVisibleSecurityState();
dalecurtis6c58ed02016-10-28 23:02:371891}
1892
Ahmed Fakhryd70bf37b2018-04-04 17:07:241893void WebContentsImpl::NotifyPreferencesChanged() {
1894 std::set<RenderViewHost*> render_view_host_set;
1895 for (FrameTreeNode* node : frame_tree_.Nodes()) {
Giovanni Ortuño Urquidi17a5d7c2018-04-30 06:48:531896 render_view_host_set.insert(
1897 node->current_frame_host()->GetRenderViewHost());
Ahmed Fakhryd70bf37b2018-04-04 17:07:241898 }
Giovanni Ortuño Urquidi17a5d7c2018-04-30 06:48:531899
Ahmed Fakhryd70bf37b2018-04-04 17:07:241900 for (RenderViewHost* render_view_host : render_view_host_set)
1901 render_view_host->OnWebkitPreferencesChanged();
1902}
1903
Bruce Long1e3e1f542019-10-16 17:56:281904void WebContentsImpl::SyncRendererPrefs() {
1905 blink::mojom::RendererPreferences renderer_preferences =
1906 GetRendererPrefs(GetBrowserContext());
1907 RenderViewHostImpl::GetPlatformSpecificPrefs(&renderer_preferences);
1908 SendPageMessage(
1909 new PageMsg_SetRendererPrefs(MSG_ROUTING_NONE, renderer_preferences));
1910}
1911
Josh Karlindaba39322019-07-17 23:24:331912void WebContentsImpl::OnCookiesRead(const GURL& url,
1913 const GURL& first_party_url,
1914 const net::CookieList& cookie_list,
1915 bool blocked_by_policy) {
1916 for (auto& observer : observers_) {
1917 observer.OnCookiesRead(url, first_party_url, cookie_list,
1918 blocked_by_policy);
1919 }
1920}
1921
1922void WebContentsImpl::OnCookieChange(const GURL& url,
1923 const GURL& first_party_url,
1924 const net::CanonicalCookie& cookie,
1925 bool blocked_by_policy) {
1926 for (auto& observer : observers_) {
1927 observer.OnCookieChange(url, first_party_url, cookie, blocked_by_policy);
1928 }
1929}
1930
[email protected]b172aee2012-04-10 17:05:261931void WebContentsImpl::Stop() {
dcheng57e39e22016-01-21 00:25:381932 for (FrameTreeNode* node : frame_tree_.Nodes())
1933 node->StopLoading();
ericwilligersde386342016-10-19 02:35:091934 for (auto& observer : observers_)
1935 observer.NavigationStopped();
[email protected]0bfbf882011-12-22 18:19:271936}
1937
Francois Doray47f759d2018-06-11 18:13:511938void WebContentsImpl::SetPageFrozen(bool frozen) {
1939 // A visible page is never frozen.
1940 DCHECK_NE(Visibility::VISIBLE, GetVisibility());
1941
1942 SendPageMessage(new PageMsg_SetPageFrozen(MSG_ROUTING_NONE, frozen));
Fadi Meawada6573e02018-03-10 00:52:111943}
1944
erikchen6c7df7f7a2018-05-03 18:13:591945std::unique_ptr<WebContents> WebContentsImpl::Clone() {
[email protected]14392a52012-05-02 20:28:441946 // We use our current SiteInstance since the cloned entry will use it anyway.
alexmose201c7cd2015-06-10 17:14:211947 // We pass our own opener so that the cloned page can access it if it was set
[email protected]ed245db2012-12-18 08:00:451948 // before.
[email protected]54944cde2012-12-09 09:24:591949 CreateParams create_params(GetBrowserContext(), GetSiteInstance());
Ian Clelland5cbaaf82017-11-27 22:00:031950 FrameTreeNode* opener = frame_tree_.root()->opener();
1951 RenderFrameHostImpl* opener_rfh = nullptr;
1952 if (opener)
1953 opener_rfh = opener->current_frame_host();
erikchen6c7df7f7a2018-05-03 18:13:591954 std::unique_ptr<WebContentsImpl> tc =
erikchen50735fd2018-05-05 15:08:331955 CreateWithOpener(create_params, opener_rfh);
Lucas Furukawa Gadani5553a152019-01-08 18:55:571956 tc->GetController().CopyStateFrom(&controller_, true);
ericwilligersde386342016-10-19 02:35:091957 for (auto& observer : observers_)
erikchen6c7df7f7a2018-05-03 18:13:591958 observer.DidCloneToNewWebContents(this, tc.get());
[email protected]0bfbf882011-12-22 18:19:271959 return tc;
1960}
1961
[email protected]14392a52012-05-02 20:28:441962void WebContentsImpl::Observe(int type,
[email protected]8ff00d72012-10-23 19:12:211963 const NotificationSource& source,
1964 const NotificationDetails& details) {
[email protected]14392a52012-05-02 20:28:441965 switch (type) {
[email protected]8ff00d72012-10-23 19:12:211966 case NOTIFICATION_RENDER_WIDGET_HOST_DESTROYED: {
1967 RenderWidgetHost* host = Source<RenderWidgetHost>(source).ptr();
[email protected]0b431992014-06-24 00:08:031968 RenderWidgetHostView* view = host->GetView();
1969 if (view == GetFullscreenRenderWidgetHostView()) {
1970 // We cannot just call view_->RestoreFocus() here. On some platforms,
1971 // attempting to focus the currently-invisible WebContentsView will be
1972 // flat-out ignored. Therefore, this boolean is used to track whether
1973 // we will request focus after the fullscreen widget has been
1974 // destroyed.
1975 fullscreen_widget_had_focus_at_shutdown_ = (view && view->HasFocus());
1976 } else {
alexmosc2a8cec2016-05-23 22:19:531977 for (auto i = pending_widget_views_.begin();
[email protected]0b431992014-06-24 00:08:031978 i != pending_widget_views_.end(); ++i) {
1979 if (host->GetView() == i->second) {
1980 pending_widget_views_.erase(i);
1981 break;
1982 }
[email protected]bafe6cd2012-05-23 23:09:501983 }
1984 }
1985 break;
1986 }
[email protected]14392a52012-05-02 20:28:441987 default:
1988 NOTREACHED();
1989 }
1990}
1991
[email protected]ec6c05f2013-10-23 18:41:571992WebContents* WebContentsImpl::GetWebContents() {
1993 return this;
1994}
1995
[email protected]54944cde2012-12-09 09:24:591996void WebContentsImpl::Init(const WebContents::CreateParams& params) {
[email protected]fa944cb82013-11-15 17:51:211997 // This is set before initializing the render manager since
[email protected]b0936d22013-11-28 06:47:361998 // RenderFrameHostManager::Init calls back into us via its delegate to ask if
[email protected]fa944cb82013-11-15 17:51:211999 // it should be hidden.
Francois Doraye6161152018-03-27 22:05:372000 visibility_ =
2001 params.initially_hidden ? Visibility::HIDDEN : Visibility::VISIBLE;
[email protected]d6fa88f2013-10-18 16:00:432002
Sebastien Marchand28cf23182018-06-20 02:39:352003 if (!params.last_active_time.is_null())
2004 last_active_time_ = params.last_active_time;
2005
dcheng3ce04b62015-10-26 23:30:552006 // The routing ids must either all be set or all be unset.
avic2c29d782014-12-12 00:23:542007 DCHECK((params.routing_id == MSG_ROUTING_NONE &&
dcheng3ce04b62015-10-26 23:30:552008 params.main_frame_routing_id == MSG_ROUTING_NONE &&
2009 params.main_frame_widget_routing_id == MSG_ROUTING_NONE) ||
avic2c29d782014-12-12 00:23:542010 (params.routing_id != MSG_ROUTING_NONE &&
dcheng3ce04b62015-10-26 23:30:552011 params.main_frame_routing_id != MSG_ROUTING_NONE &&
2012 params.main_frame_widget_routing_id != MSG_ROUTING_NONE));
2013
2014 scoped_refptr<SiteInstance> site_instance = params.site_instance;
2015 if (!site_instance)
2016 site_instance = SiteInstance::Create(params.browser_context);
Lukasz Anforowicz3caa8372018-06-05 17:22:072017 if (params.desired_renderer_state == CreateParams::kNoRendererProcess) {
2018 static_cast<SiteInstanceImpl*>(site_instance.get())
2019 ->PreventAssociationWithSpareProcess();
2020 }
dcheng3ce04b62015-10-26 23:30:552021
2022 // A main RenderFrameHost always has a RenderWidgetHost, since it is always a
2023 // local root by definition.
2024 // TODO(avi): Once RenderViewHostImpl has-a RenderWidgetHostImpl, it will no
2025 // longer be necessary to eagerly grab a routing ID for the view.
2026 // https://crbug.com/545684
2027 int32_t view_routing_id = params.routing_id;
2028 int32_t main_frame_widget_routing_id = params.main_frame_widget_routing_id;
2029 if (main_frame_widget_routing_id == MSG_ROUTING_NONE) {
Albert J. Wongcb004632018-07-10 22:58:252030 view_routing_id = site_instance->GetProcess()->GetNextRoutingID();
2031 main_frame_widget_routing_id =
dcheng3ce04b62015-10-26 23:30:552032 site_instance->GetProcess()->GetNextRoutingID();
2033 }
2034
Albert J. Wongcb004632018-07-10 22:58:252035 DCHECK_NE(view_routing_id, main_frame_widget_routing_id);
2036
Dave Tapuska485aca92017-08-08 00:47:582037 GetRenderManager()->Init(
2038 site_instance.get(), view_routing_id, params.main_frame_routing_id,
2039 main_frame_widget_routing_id, params.renderer_initiated_creation);
lukasza464d8692016-02-22 19:26:322040
lukasza5140a412016-09-15 21:12:302041 // blink::FrameTree::setName always keeps |unique_name| empty in case of a
2042 // main frame - let's do the same thing here.
2043 std::string unique_name;
lukasza464d8692016-02-22 19:26:322044 frame_tree_.root()->SetFrameName(params.main_frame_name, unique_name);
[email protected]d1198fd2012-08-13 22:50:192045
[email protected]fc2b46b2014-05-03 16:33:452046 WebContentsViewDelegate* delegate =
2047 GetContentClient()->browser()->GetWebContentsViewDelegate(this);
2048
sky52a39e342016-12-17 17:23:222049 if (GuestMode::IsCrossProcessFrameGuest(this)) {
2050 view_.reset(new WebContentsViewChildFrame(
2051 this, delegate, &render_view_host_delegate_view_));
2052 } else {
2053 view_.reset(CreateWebContentsView(this, delegate,
2054 &render_view_host_delegate_view_));
Scott Violetc884c7fd2017-11-03 23:51:012055 if (browser_plugin_guest_) {
2056 view_ = std::make_unique<WebContentsViewGuest>(
2057 this, browser_plugin_guest_.get(), std::move(view_),
2058 &render_view_host_delegate_view_);
2059 }
[email protected]d1198fd2012-08-13 22:50:192060 }
[email protected]fc2b46b2014-05-03 16:33:452061 CHECK(render_view_host_delegate_view_);
[email protected]d1198fd2012-08-13 22:50:192062 CHECK(view_.get());
2063
danakjfc5184932019-09-12 18:08:322064 view_->CreateView(params.context);
[email protected]d1198fd2012-08-13 22:50:192065
brettw4b461082016-11-19 18:55:162066#if BUILDFLAG(ENABLE_PLUGINS)
tommyclieb25b2a2014-11-03 19:45:092067 plugin_content_origin_whitelist_.reset(
2068 new PluginContentOriginWhitelist(this));
2069#endif
2070
[email protected]d1198fd2012-08-13 22:50:192071 registrar_.Add(this,
[email protected]8ff00d72012-10-23 19:12:212072 NOTIFICATION_RENDER_WIDGET_HOST_DESTROYED,
2073 NotificationService::AllBrowserContextsAndSources());
[email protected]f28ef9a32014-05-12 16:36:102074
leon.hane4db177a2017-02-07 14:19:162075 screen_orientation_provider_.reset(new ScreenOrientationProvider(this));
[email protected]01f83f92014-06-17 14:41:542076
mlamouriefdca9d2014-09-16 16:55:402077 manifest_manager_host_.reset(new ManifestManagerHost(this));
2078
mfomitchev2b8b066a2016-01-28 19:23:152079#if defined(OS_ANDROID)
Oksana Zhuravlova0b634332019-10-28 23:04:222080 DateTimeChooserAndroid::CreateForWebContents(this);
[email protected]583418cc2013-01-17 14:01:102081#endif
fsamuel7310a4262014-12-05 05:06:442082
2083 // BrowserPluginGuest::Init needs to be called after this WebContents has
2084 // a RenderWidgetHostViewGuest. That is, |view_->CreateView| above.
2085 if (browser_plugin_guest_)
2086 browser_plugin_guest_->Init();
nickf9acfbe2014-12-23 19:12:372087
2088 for (size_t i = 0; i < g_created_callbacks.Get().size(); i++)
2089 g_created_callbacks.Get().at(i).Run(this);
naskob21fe4872015-02-24 14:15:562090
2091 // If the WebContents creation was renderer-initiated, it means that the
2092 // corresponding RenderView and main RenderFrame have already been created.
2093 // Ensure observers are notified about this.
2094 if (params.renderer_initiated_creation) {
avi3627ecac2015-10-16 17:40:432095 GetRenderViewHost()->GetWidget()->set_renderer_initialized(true);
Alex Moshchuk27caae82017-09-11 23:11:182096 GetRenderViewHost()->DispatchRenderViewCreated();
naskob21fe4872015-02-24 14:15:562097 GetRenderManager()->current_frame_host()->SetRenderFrameCreated(true);
2098 }
naskof5940b9f2015-03-02 23:04:052099
lof84501da082016-05-23 21:22:542100 // Create the renderer process in advance if requested.
Lukasz Anforowicz3caa8372018-06-05 17:22:072101 if (params.desired_renderer_state ==
2102 CreateParams::kInitializeAndWarmupRendererProcess) {
lof84501da082016-05-23 21:22:542103 if (!GetRenderManager()->current_frame_host()->IsRenderFrameLive()) {
2104 GetRenderManager()->InitRenderView(GetRenderViewHost(), nullptr);
2105 }
2106 }
2107
naskof5940b9f2015-03-02 23:04:052108 // Ensure that observers are notified of the creation of this WebContents's
2109 // main RenderFrameHost. It must be done here for main frames, since the
2110 // NotifySwappedFromRenderManager expects view_ to already be created and that
2111 // happens after RenderFrameHostManager::Init.
2112 NotifySwappedFromRenderManager(
2113 nullptr, GetRenderManager()->current_frame_host(), true);
Albert J. Wonge76bf0b82019-09-27 07:47:062114
2115 // For WebContents that are never shown, do critical initialization here which
2116 // would normally only happen when the WebContents is shown.
2117 if (params.is_never_visible) {
2118 // This has just been created so there can only be one frame. Thus it is
2119 // safe to initialize the root.
2120 GetMainFrame()->Init();
2121 }
[email protected]d1198fd2012-08-13 22:50:192122}
2123
[email protected]7fff43e2013-05-21 20:21:102124void WebContentsImpl::OnWebContentsDestroyed(WebContentsImpl* web_contents) {
2125 RemoveDestructionObserver(web_contents);
2126
[email protected]ceee8cd2013-03-08 04:59:512127 // Clear a pending contents that has been closed before being shown.
alexmosc2a8cec2016-05-23 22:19:532128 for (auto iter = pending_contents_.begin(); iter != pending_contents_.end();
[email protected]ceee8cd2013-03-08 04:59:512129 ++iter) {
erikchenbee5c9622018-04-27 19:30:252130 if (iter->second.get() != web_contents)
[email protected]ceee8cd2013-03-08 04:59:512131 continue;
erikchenbee5c9622018-04-27 19:30:252132
2133 // Someone else has deleted the WebContents. That should never happen!
2134 // TODO(erikchen): Fix semantics here. https://crbug.com/832879.
2135 iter->second.release();
[email protected]ceee8cd2013-03-08 04:59:512136 pending_contents_.erase(iter);
[email protected]ceee8cd2013-03-08 04:59:512137 return;
2138 }
2139 NOTREACHED();
[email protected]14392a52012-05-02 20:28:442140}
2141
[email protected]7fff43e2013-05-21 20:21:102142void WebContentsImpl::AddDestructionObserver(WebContentsImpl* web_contents) {
Jan Wilken Dörrie73c901e2019-06-12 09:02:322143 if (!base::Contains(destruction_observers_, web_contents)) {
[email protected]7fff43e2013-05-21 20:21:102144 destruction_observers_[web_contents] =
Jeremy Roman04f27c372017-10-27 15:20:552145 std::make_unique<DestructionObserver>(this, web_contents);
[email protected]7fff43e2013-05-21 20:21:102146 }
2147}
2148
2149void WebContentsImpl::RemoveDestructionObserver(WebContentsImpl* web_contents) {
avia8cf81982016-08-12 20:28:462150 destruction_observers_.erase(web_contents);
[email protected]7fff43e2013-05-21 20:21:102151}
2152
[email protected]b172aee2012-04-10 17:05:262153void WebContentsImpl::AddObserver(WebContentsObserver* observer) {
[email protected]be1f56ab2011-12-22 06:55:312154 observers_.AddObserver(observer);
2155}
2156
[email protected]b172aee2012-04-10 17:05:262157void WebContentsImpl::RemoveObserver(WebContentsObserver* observer) {
[email protected]be1f56ab2011-12-22 06:55:312158 observers_.RemoveObserver(observer);
2159}
2160
[email protected]948481d2014-06-11 18:32:222161std::set<RenderWidgetHostView*>
2162WebContentsImpl::GetRenderWidgetHostViewsInTree() {
2163 std::set<RenderWidgetHostView*> set;
2164 if (ShowingInterstitialPage()) {
lfgf949e0e2017-05-10 22:18:112165 if (RenderWidgetHostView* rwhv = GetRenderWidgetHostView())
2166 set.insert(rwhv);
[email protected]948481d2014-06-11 18:32:222167 } else {
dchengafb53e22016-02-04 08:11:082168 for (RenderFrameHost* rfh : GetAllFrames()) {
lfgf949e0e2017-05-10 22:18:112169 if (RenderWidgetHostView* rwhv = static_cast<RenderFrameHostImpl*>(rfh)
2170 ->frame_tree_node()
2171 ->render_manager()
2172 ->GetRenderWidgetHostView()) {
2173 set.insert(rwhv);
2174 }
dchengafb53e22016-02-04 08:11:082175 }
[email protected]948481d2014-06-11 18:32:222176 }
[email protected]de3c5d82014-05-28 22:12:592177 return set;
2178}
2179
[email protected]b172aee2012-04-10 17:05:262180void WebContentsImpl::Activate() {
[email protected]d5f942ba2008-09-26 19:30:342181 if (delegate_)
2182 delegate_->ActivateContents(this);
2183}
2184
avi3e4b8512015-11-11 01:55:492185void WebContentsImpl::LostCapture(RenderWidgetHostImpl* render_widget_host) {
2186 if (!RenderViewHostImpl::From(render_widget_host))
2187 return;
2188
[email protected]63954792011-07-11 04:17:482189 if (delegate_)
2190 delegate_->LostCapture();
2191}
2192
Steven Holte38041342018-06-22 22:00:572193ukm::SourceId WebContentsImpl::GetUkmSourceIdForLastCommittedSource() const {
2194 return last_committed_source_id_;
2195}
2196
Ahmed Fakhryfaa32d22018-10-05 15:56:162197void WebContentsImpl::SetTopControlsShownRatio(
2198 RenderWidgetHostImpl* render_widget_host,
2199 float ratio) {
2200 if (!delegate_)
2201 return;
2202
2203 RenderFrameHostImpl* rfh = GetMainFrame();
2204 if (!rfh || render_widget_host != rfh->GetRenderWidgetHost())
2205 return;
2206
2207 delegate_->SetTopControlsShownRatio(this, ratio);
Ahmed Fakhry58e6ef542018-09-04 18:05:382208}
2209
Ahmed Fakhry58e6ef542018-09-04 18:05:382210void WebContentsImpl::SetTopControlsGestureScrollInProgress(bool in_progress) {
2211 if (delegate_)
2212 delegate_->SetTopControlsGestureScrollInProgress(in_progress);
2213}
2214
lazyboy63f5b312015-11-23 20:19:522215void WebContentsImpl::RenderWidgetCreated(
2216 RenderWidgetHostImpl* render_widget_host) {
2217 created_widgets_.insert(render_widget_host);
2218}
2219
[email protected]b24b68a2012-09-24 21:57:262220void WebContentsImpl::RenderWidgetDeleted(
2221 RenderWidgetHostImpl* render_widget_host) {
lazyboy63f5b312015-11-23 20:19:522222 // Note that |is_being_destroyed_| can be true at this point as
2223 // ~WebContentsImpl() calls RFHM::ClearRFHsPendingShutdown(), which might lead
2224 // us here.
2225 created_widgets_.erase(render_widget_host);
[email protected]b24b68a2012-09-24 21:57:262226
lazyboy63f5b312015-11-23 20:19:522227 if (is_being_destroyed_)
2228 return;
[email protected]44470a22013-01-24 01:21:542229
2230 if (render_widget_host &&
alexmosc9e76ec2016-05-16 22:59:372231 render_widget_host->GetRoutingID() == fullscreen_widget_routing_id_ &&
2232 render_widget_host->GetProcess()->GetID() ==
2233 fullscreen_widget_process_id_) {
[email protected]4aebbca2013-09-17 22:26:492234 if (delegate_ && delegate_->EmbedsFullscreenWidget())
mlamouri7a78d6fd2015-01-17 13:23:532235 delegate_->ExitFullscreenModeForTab(this);
ericwilligersde386342016-10-19 02:35:092236 for (auto& observer : observers_)
2237 observer.DidDestroyFullscreenWidget();
alexmosc9e76ec2016-05-16 22:59:372238 fullscreen_widget_process_id_ = ChildProcessHost::kInvalidUniqueID;
[email protected]44470a22013-01-24 01:21:542239 fullscreen_widget_routing_id_ = MSG_ROUTING_NONE;
[email protected]0b431992014-06-24 00:08:032240 if (fullscreen_widget_had_focus_at_shutdown_)
2241 view_->RestoreFocus();
[email protected]44470a22013-01-24 01:21:542242 }
lfgbee1e0a2016-06-08 21:24:212243
lfg7d4caad2017-03-22 09:01:452244 if (render_widget_host == mouse_lock_widget_)
lfgff8dfc8d2016-10-21 17:42:322245 LostMouseLock(mouse_lock_widget_);
Joe Downing192998b22018-03-22 15:51:362246
Joe Downingc1a57efe2018-05-09 18:23:542247 CancelKeyboardLock(render_widget_host);
[email protected]b24b68a2012-09-24 21:57:262248}
2249
miu9e14e492014-10-25 02:39:042250void WebContentsImpl::RenderWidgetGotFocus(
2251 RenderWidgetHostImpl* render_widget_host) {
calamity7fe55ce2015-04-10 03:59:372252 // Notify the observers if an embedded fullscreen widget was focused.
2253 if (delegate_ && render_widget_host && delegate_->EmbedsFullscreenWidget() &&
2254 render_widget_host->GetView() == GetFullscreenRenderWidgetHostView()) {
Michael Thiessen896405db2017-07-20 17:52:322255 NotifyWebContentsFocused(render_widget_host);
calamity7fe55ce2015-04-10 03:59:372256 }
miu9e14e492014-10-25 02:39:042257}
2258
zijiehe3bee08e22017-05-17 04:14:462259void WebContentsImpl::RenderWidgetLostFocus(
2260 RenderWidgetHostImpl* render_widget_host) {
Michael Thiessen896405db2017-07-20 17:52:322261 // Notify the observers if an embedded fullscreen widget lost focus.
2262 if (delegate_ && render_widget_host && delegate_->EmbedsFullscreenWidget() &&
2263 render_widget_host->GetView() == GetFullscreenRenderWidgetHostView()) {
2264 NotifyWebContentsLostFocus(render_widget_host);
2265 }
zijiehe3bee08e22017-05-17 04:14:462266}
2267
estaded0a0c992015-01-21 14:12:062268void WebContentsImpl::RenderWidgetWasResized(
rouslan2f5993f2015-01-29 00:18:312269 RenderWidgetHostImpl* render_widget_host,
2270 bool width_changed) {
creisc014b402015-04-23 16:41:452271 RenderFrameHostImpl* rfh = GetMainFrame();
estadece344ec2015-01-23 00:12:292272 if (!rfh || render_widget_host != rfh->GetRenderWidgetHost())
estaded0a0c992015-01-21 14:12:062273 return;
estaded0a0c992015-01-21 14:12:062274
ericwilligersde386342016-10-19 02:35:092275 for (auto& observer : observers_)
2276 observer.MainFrameWasResized(width_changed);
estaded0a0c992015-01-21 14:12:062277}
2278
skyf65d9bb2017-03-24 02:26:392279KeyboardEventProcessingResult WebContentsImpl::PreHandleKeyboardEvent(
2280 const NativeWebKeyboardEvent& event) {
2281 return delegate_ ? delegate_->PreHandleKeyboardEvent(this, event)
2282 : KeyboardEventProcessingResult::NOT_HANDLED;
[email protected]63954792011-07-11 04:17:482283}
2284
Dave Tapuska344399e2019-09-09 15:18:262285bool WebContentsImpl::HandleMouseEvent(const blink::WebMouseEvent& event) {
2286 // Handle mouse button back/forward in the browser process after the render
2287 // process is done with the event. This ensures all renderer-initiated history
2288 // navigations can be treated consistently.
2289 if (event.GetType() == blink::WebInputEvent::Type::kMouseUp) {
2290 WebContentsImpl* outermost = GetOutermostWebContents();
2291 if (event.button == blink::WebPointerProperties::Button::kBack &&
2292 outermost->controller_.CanGoBack()) {
2293 outermost->controller_.GoBack();
2294 return true;
2295 } else if (event.button == blink::WebPointerProperties::Button::kForward &&
2296 outermost->controller_.CanGoForward()) {
2297 outermost->controller_.GoForward();
2298 return true;
2299 }
2300 }
2301 return false;
2302}
2303
Scott Violetd5caa872018-10-16 22:00:512304bool WebContentsImpl::HandleKeyboardEvent(const NativeWebKeyboardEvent& event) {
[email protected]0b094002014-08-20 18:28:102305 if (browser_plugin_embedder_ &&
2306 browser_plugin_embedder_->HandleKeyboardEvent(event)) {
Scott Violetd5caa872018-10-16 22:00:512307 return true;
[email protected]0b094002014-08-20 18:28:102308 }
Scott Violetd5caa872018-10-16 22:00:512309 return delegate_ && delegate_->HandleKeyboardEvent(this, event);
[email protected]63954792011-07-11 04:17:482310}
2311
[email protected]d92026ef2014-03-03 21:04:132312bool WebContentsImpl::HandleWheelEvent(
[email protected]180ef242013-11-07 06:50:462313 const blink::WebMouseWheelEvent& event) {
[email protected]bccc4472013-04-18 16:37:192314#if !defined(OS_MACOSX)
lanweia93644f2015-01-21 22:00:332315 // On platforms other than Mac, control+mousewheel may change zoom. On Mac,
2316 // this isn't done for two reasons:
[email protected]bccc4472013-04-18 16:37:192317 // -the OS already has a gesture to do this through pinch-zoom
2318 // -if a user starts an inertial scroll, let's go, and presses control
2319 // (i.e. control+tab) then the OS's buffered scroll events will come in
2320 // with control key set which isn't what the user wants
Blink Reformat1c4d759e2017-04-09 16:34:542321 if (delegate_ && event.wheel_ticks_y &&
Ehsan Karamad0f83cb112018-09-06 22:07:362322 event.event_action == blink::WebMouseWheelEvent::EventAction::kPageZoom) {
w.shackleton49bcd392016-01-06 17:38:222323 // Count only integer cumulative scrolls as zoom events; this handles
2324 // smooth scroll and regular scroll device behavior.
Blink Reformat1c4d759e2017-04-09 16:34:542325 zoom_scroll_remainder_ += event.wheel_ticks_y;
w.shackleton49bcd392016-01-06 17:38:222326 int whole_zoom_scroll_remainder_ = std::lround(zoom_scroll_remainder_);
2327 zoom_scroll_remainder_ -= whole_zoom_scroll_remainder_;
2328 if (whole_zoom_scroll_remainder_ != 0) {
2329 delegate_->ContentsZoomChange(whole_zoom_scroll_remainder_ > 0);
2330 }
[email protected]fb3f066e2013-02-12 19:12:522331 return true;
2332 }
[email protected]bccc4472013-04-18 16:37:192333#endif
[email protected]fb3f066e2013-02-12 19:12:522334 return false;
2335}
2336
[email protected]04bce562014-01-30 05:34:542337bool WebContentsImpl::PreHandleGestureEvent(
2338 const blink::WebGestureEvent& event) {
[email protected]28042d32014-02-03 14:10:032339 return delegate_ && delegate_->PreHandleGestureEvent(this, event);
[email protected]04bce562014-01-30 05:34:542340}
2341
kenrb2a565f82015-09-02 20:24:592342RenderWidgetHostInputEventRouter* WebContentsImpl::GetInputEventRouter() {
wjmacleane31234f2015-12-14 17:20:452343 if (!is_being_destroyed_ && GetOuterWebContents())
2344 return GetOuterWebContents()->GetInputEventRouter();
2345
2346 if (!rwh_input_event_router_.get() && !is_being_destroyed_)
kenrb2a565f82015-09-02 20:24:592347 rwh_input_event_router_.reset(new RenderWidgetHostInputEventRouter);
2348 return rwh_input_event_router_.get();
2349}
2350
alexmos3fcd0ca2015-10-23 18:18:332351void WebContentsImpl::ReplicatePageFocus(bool is_focused) {
2352 // Focus loss may occur while this WebContents is being destroyed. Don't
2353 // send the message in this case, as the main frame's RenderFrameHost and
2354 // other state has already been cleared.
2355 if (is_being_destroyed_)
2356 return;
2357
2358 frame_tree_.ReplicatePageFocus(is_focused);
2359}
2360
alexmosc4cbacb2015-11-21 01:29:222361RenderWidgetHostImpl* WebContentsImpl::GetFocusedRenderWidgetHost(
2362 RenderWidgetHostImpl* receiving_widget) {
alexmosc4cbacb2015-11-21 01:29:222363 // Events for widgets other than the main frame (e.g., popup menus) should be
2364 // forwarded directly to the widget they arrived on.
2365 if (receiving_widget != GetMainFrame()->GetRenderWidgetHost())
2366 return receiving_widget;
alexmosc4d183e2015-11-06 00:46:522367
lfg1453e412017-04-11 00:48:502368 WebContentsImpl* focused_contents = GetFocusedWebContents();
2369
2370 // If the focused WebContents is showing an interstitial, return the
2371 // interstitial's widget.
2372 if (focused_contents->ShowingInterstitialPage()) {
2373 return static_cast<RenderFrameHostImpl*>(
creisadca9862017-06-16 19:12:112374 focused_contents->interstitial_page_->GetMainFrame())
lfg1453e412017-04-11 00:48:502375 ->GetRenderWidgetHost();
2376 }
2377
paulmeyerfeafc2d2017-04-25 21:46:402378 // If the focused WebContents is a guest WebContents, then get the focused
2379 // frame in the embedder WebContents instead.
2380 FrameTreeNode* focused_frame = nullptr;
2381 if (focused_contents->browser_plugin_guest_ &&
2382 !GuestMode::IsCrossProcessFrameGuest(focused_contents)) {
2383 focused_frame = frame_tree_.GetFocusedFrame();
2384 } else {
2385 focused_frame = GetFocusedWebContents()->frame_tree_.GetFocusedFrame();
2386 }
lfg1453e412017-04-11 00:48:502387
alexmosc4d183e2015-11-06 00:46:522388 if (!focused_frame)
alexmosc4cbacb2015-11-21 01:29:222389 return receiving_widget;
alexmosc4d183e2015-11-06 00:46:522390
alexmos732ca3a2015-12-08 02:01:022391 // The view may be null if a subframe's renderer process has crashed while
2392 // the subframe has focus. Drop the event in that case. Do not give
2393 // it to the main frame, so that the user doesn't unexpectedly type into the
2394 // wrong frame if a focused subframe renderer crashes while they type.
2395 RenderWidgetHostView* view = focused_frame->current_frame_host()->GetView();
2396 if (!view)
2397 return nullptr;
2398
2399 return RenderWidgetHostImpl::From(view->GetRenderWidgetHost());
alexmosc4d183e2015-11-06 00:46:522400}
2401
avallee9993fca2016-11-17 06:16:502402RenderWidgetHostImpl* WebContentsImpl::GetRenderWidgetHostWithPageFocus() {
lfg1453e412017-04-11 00:48:502403 WebContentsImpl* focused_web_contents = GetFocusedWebContents();
2404
2405 if (focused_web_contents->ShowingInterstitialPage()) {
2406 return static_cast<RenderFrameHostImpl*>(
creisadca9862017-06-16 19:12:112407 focused_web_contents->interstitial_page_->GetMainFrame())
lfg1453e412017-04-11 00:48:502408 ->GetRenderWidgetHost();
2409 }
Alex Valléedc80ae2ed2017-09-06 21:55:442410 if (!GuestMode::IsCrossProcessFrameGuest(focused_web_contents) &&
2411 focused_web_contents->browser_plugin_guest_) {
2412 // If this is a guest, we need to be controlled by our embedder.
2413 return focused_web_contents->GetOuterWebContents()
2414 ->GetMainFrame()
2415 ->GetRenderWidgetHost();
2416 }
lfg1453e412017-04-11 00:48:502417
2418 return focused_web_contents->GetMainFrame()->GetRenderWidgetHost();
avallee9993fca2016-11-17 06:16:502419}
2420
Dave Tapuska3ed44192018-05-01 18:53:302421void WebContentsImpl::EnterFullscreenMode(
2422 const GURL& origin,
Dave Tapuskaa4189512019-10-15 20:27:342423 const blink::mojom::FullscreenOptions& options) {
mlamouri7a78d6fd2015-01-17 13:23:532424 // This method is being called to enter renderer-initiated fullscreen mode.
2425 // Make sure any existing fullscreen widget is shut down first.
[email protected]4aebbca2013-09-17 22:26:492426 RenderWidgetHostView* const widget_view = GetFullscreenRenderWidgetHostView();
avi85a4cef2015-12-18 20:13:372427 if (widget_view) {
2428 RenderWidgetHostImpl::From(widget_view->GetRenderWidgetHost())
2429 ->ShutdownAndDestroyWidget(true);
2430 }
[email protected]4aebbca2013-09-17 22:26:492431
Joe Downing13dd76b2018-04-09 18:32:152432 if (delegate_) {
Dave Tapuska3ed44192018-05-01 18:53:302433 delegate_->EnterFullscreenModeForTab(this, origin, options);
mlamouri7a78d6fd2015-01-17 13:23:532434
Joe Downing13dd76b2018-04-09 18:32:152435 if (keyboard_lock_widget_)
2436 delegate_->RequestKeyboardLock(this, esc_key_locked_);
2437 }
2438
ericwilligersde386342016-10-19 02:35:092439 for (auto& observer : observers_)
2440 observer.DidToggleFullscreenModeForTab(IsFullscreenForCurrentTab(), false);
mlamouri7a78d6fd2015-01-17 13:23:532441}
2442
bokanece34a82016-01-28 19:49:462443void WebContentsImpl::ExitFullscreenMode(bool will_cause_resize) {
mlamouri7a78d6fd2015-01-17 13:23:532444 // This method is being called to leave renderer-initiated fullscreen mode.
2445 // Make sure any existing fullscreen widget is shut down first.
2446 RenderWidgetHostView* const widget_view = GetFullscreenRenderWidgetHostView();
avi85a4cef2015-12-18 20:13:372447 if (widget_view) {
2448 RenderWidgetHostImpl::From(widget_view->GetRenderWidgetHost())
2449 ->ShutdownAndDestroyWidget(true);
2450 }
mlamouri7a78d6fd2015-01-17 13:23:532451
Joe Downing13dd76b2018-04-09 18:32:152452 if (delegate_) {
mlamouri7a78d6fd2015-01-17 13:23:532453 delegate_->ExitFullscreenModeForTab(this);
[email protected]657c8e02014-03-19 19:18:372454
Joe Downing13dd76b2018-04-09 18:32:152455 if (keyboard_lock_widget_)
2456 delegate_->CancelKeyboardLockRequest(this);
2457 }
2458
bokanece34a82016-01-28 19:49:462459 // The fullscreen state is communicated to the renderer through a resize
2460 // message. If the change in fullscreen state doesn't cause a view resize
2461 // then we must ensure web contents exit the fullscreen state by explicitly
2462 // sending a resize message. This is required for the situation of the browser
2463 // moving the view into a "browser fullscreen" state and then the contents
2464 // entering "tab fullscreen". Exiting the contents "tab fullscreen" then won't
2465 // have the side effect of the view resizing, hence the explicit call here is
2466 // required.
2467 if (!will_cause_resize) {
2468 if (RenderWidgetHostView* rwhv = GetRenderWidgetHostView()) {
2469 if (RenderWidgetHost* render_widget_host = rwhv->GetRenderWidgetHost())
Fady Samuel0b911822018-04-25 13:22:162470 render_widget_host->SynchronizeVisualProperties();
bokanece34a82016-01-28 19:49:462471 }
scheib3dcb6c932015-02-23 10:55:582472 }
2473
Arthur Sonzogni929d29f2018-09-17 14:19:442474 current_fullscreen_frame_ = nullptr;
Becca Hughese9e27952018-06-25 17:08:272475
ericwilligersde386342016-10-19 02:35:092476 for (auto& observer : observers_) {
2477 observer.DidToggleFullscreenModeForTab(IsFullscreenForCurrentTab(),
2478 will_cause_resize);
2479 }
Becca Hughesd11d6502018-07-31 17:01:282480
2481 if (display_cutout_host_impl_)
2482 display_cutout_host_impl_->DidExitFullscreen();
[email protected]8a5e0ca2011-08-25 06:30:472483}
2484
Becca Hughesfd5d8f892018-06-14 18:23:362485void WebContentsImpl::FullscreenStateChanged(RenderFrameHost* rfh,
2486 bool is_fullscreen) {
Arthur Sonzogni929d29f2018-09-17 14:19:442487 RenderFrameHostImpl* frame = static_cast<RenderFrameHostImpl*>(rfh);
Becca Hughesfd5d8f892018-06-14 18:23:362488
2489 if (is_fullscreen) {
Jan Wilken Dörrie531be7ca2019-06-07 10:05:572490 if (!base::Contains(fullscreen_frames_, frame)) {
Arthur Sonzogni929d29f2018-09-17 14:19:442491 fullscreen_frames_.insert(frame);
2492 FullscreenFrameSetUpdated();
Becca Hughesfd5d8f892018-06-14 18:23:362493 }
Arthur Sonzogni929d29f2018-09-17 14:19:442494 return;
2495 }
Becca Hughesfd5d8f892018-06-14 18:23:362496
Arthur Sonzogni929d29f2018-09-17 14:19:442497 // If |frame| is no longer in fullscreen, remove it and any descendants.
2498 // See https://fullscreen.spec.whatwg.org.
Arthur Sonzogni969a794e2018-10-11 08:56:092499 size_t size_before_deletion = fullscreen_frames_.size();
2500 base::EraseIf(fullscreen_frames_, [&](RenderFrameHostImpl* current) {
2501 return (current == frame || current->IsDescendantOf(frame));
Arthur Sonzogni929d29f2018-09-17 14:19:442502 });
2503
Arthur Sonzogni969a794e2018-10-11 08:56:092504 if (size_before_deletion != fullscreen_frames_.size())
Arthur Sonzogni929d29f2018-09-17 14:19:442505 FullscreenFrameSetUpdated();
2506}
2507
2508void WebContentsImpl::FullscreenFrameSetUpdated() {
2509 if (fullscreen_frames_.empty()) {
2510 current_fullscreen_frame_ = nullptr;
2511 return;
Becca Hughesfd5d8f892018-06-14 18:23:362512 }
2513
Arthur Sonzogni929d29f2018-09-17 14:19:442514 // Find the current fullscreen frame and call the observers.
2515 // If frame A is fullscreen, then frame B goes into inner fullscreen, then B
2516 // exits fullscreen - that will result in A being fullscreen.
2517 RenderFrameHostImpl* new_fullscreen_frame = *std::max_element(
2518 fullscreen_frames_.begin(), fullscreen_frames_.end(), FrameCompareDepth);
Becca Hughesfd5d8f892018-06-14 18:23:362519
Arthur Sonzogni929d29f2018-09-17 14:19:442520 // If we have already notified observers about this frame then we should not
2521 // fire the observers again.
2522 if (new_fullscreen_frame == current_fullscreen_frame_)
2523 return;
2524 current_fullscreen_frame_ = new_fullscreen_frame;
Becca Hughesfd5d8f892018-06-14 18:23:362525
Arthur Sonzogni929d29f2018-09-17 14:19:442526 for (auto& observer : observers_)
2527 observer.DidAcquireFullscreen(new_fullscreen_frame);
Becca Hughesfd5d8f892018-06-14 18:23:362528
Arthur Sonzogni929d29f2018-09-17 14:19:442529 if (display_cutout_host_impl_)
2530 display_cutout_host_impl_->DidAcquireFullscreen(new_fullscreen_frame);
Becca Hughesfd5d8f892018-06-14 18:23:362531}
2532
Collin Baker989e0882019-11-01 01:27:172533void WebContentsImpl::UpdateVisibilityAndNotifyPageAndView(
2534 Visibility new_visibility) {
2535 if (new_visibility == Visibility::VISIBLE)
2536 controller_.SetActive(true);
2537
2538 // Only hide the page if there are no entities capturing screenshots
2539 // or video (e.g. mirroring).
2540 const bool page_is_visible =
2541 new_visibility == Visibility::VISIBLE || IsBeingCaptured();
2542 // If there are entities in Picture-in-Picture mode, don't activate
2543 // the "disable rendering" optimization.
2544 const bool view_is_visible = page_is_visible || HasPictureInPictureVideo();
2545
2546 // This shows the Page before showing the individual RenderWidgets, as
2547 // RenderWidgets will work to produce compositor frames and handle input
2548 // as soon as they are shown. But the Page and other classes do not expect to
2549 // be producing frames when the Page is hidden. So we make sure the Page is
2550 // shown first.
2551 if (page_is_visible)
2552 SendPageMessage(new PageMsg_WasShown(MSG_ROUTING_NONE));
2553
2554 // |GetRenderWidgetHostView()| can be null if the user middle clicks a link to
2555 // open a tab in the background, then closes the tab before selecting it.
2556 // This is because closing the tab calls WebContentsImpl::Destroy(), which
2557 // removes the |GetRenderViewHost()|; then when we actually destroy the
2558 // window, OnWindowPosChanged() notices and calls WasHidden() (which
2559 // calls us).
2560 if (auto* view = GetRenderWidgetHostView()) {
2561 if (view_is_visible) {
2562 view->Show();
2563#if defined(OS_MACOSX)
2564 view->SetActive(true);
2565#endif
2566 } else if (new_visibility == Visibility::HIDDEN) {
2567 view->Hide();
2568 } else {
2569 view->WasOccluded();
2570 }
2571 }
2572
2573 if (!ShowingInterstitialPage())
2574 SetVisibilityForChildViews(view_is_visible);
2575
2576 // Make sure to call SetVisibilityAndNotifyObservers(VISIBLE) before notifying
2577 // the CrossProcessFrameConnector.
2578 if (new_visibility == Visibility::VISIBLE) {
2579 last_active_time_ = base::TimeTicks::Now();
2580 SetVisibilityAndNotifyObservers(new_visibility);
2581 }
2582
2583 if (!page_is_visible) {
2584 // Similar to when showing the page, we only hide the page after
2585 // hiding the individual RenderWidgets.
2586 SendPageMessage(new PageMsg_WasHidden(MSG_ROUTING_NONE));
2587 } else {
2588 for (FrameTreeNode* node : frame_tree_.Nodes()) {
2589 RenderFrameProxyHost* parent = node->render_manager()->GetProxyToParent();
2590 if (!parent)
2591 continue;
2592
2593 parent->cross_process_frame_connector()->DelegateWasShown();
2594 }
2595 }
2596
2597 if (new_visibility != Visibility::VISIBLE)
2598 SetVisibilityAndNotifyObservers(new_visibility);
2599}
2600
Clark DuValla0e42992018-09-17 17:00:112601#if defined(OS_ANDROID)
2602void WebContentsImpl::UpdateUserGestureCarryoverInfo() {
2603 if (delegate_)
2604 delegate_->UpdateUserGestureCarryoverInfo(this);
2605}
2606#endif
2607
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:412608bool WebContentsImpl::IsFullscreenForCurrentTab() {
[email protected]199bba6e2012-04-04 16:19:382609 return delegate_ ? delegate_->IsFullscreenForTabOrPending(this) : false;
[email protected]5d5f7af2011-10-01 01:38:122610}
2611
Malay Keshav87c42c02019-01-15 08:37:472612bool WebContentsImpl::ShouldShowStaleContentOnEviction() {
2613 return GetDelegate() && GetDelegate()->ShouldShowStaleContentOnEviction(this);
2614}
2615
billorr21c005d2016-11-17 03:57:042616bool WebContentsImpl::IsFullscreen() {
2617 return IsFullscreenForCurrentTab();
2618}
2619
Eric Willigers052f0432019-10-04 04:06:572620blink::mojom::DisplayMode WebContentsImpl::GetDisplayMode(
avibf58fa0c2015-11-11 01:45:522621 RenderWidgetHostImpl* render_widget_host) const {
2622 if (!RenderViewHostImpl::From(render_widget_host))
Eric Willigers052f0432019-10-04 04:06:572623 return blink::mojom::DisplayMode::kBrowser;
avibf58fa0c2015-11-11 01:45:522624
mikhail.pozdnyakovc0e251b2015-04-15 06:51:122625 return delegate_ ? delegate_->GetDisplayMode(this)
Eric Willigers052f0432019-10-04 04:06:572626 : blink::mojom::DisplayMode::kBrowser;
mikhail.pozdnyakovc0e251b2015-04-15 06:51:122627}
2628
avic3aa8422015-11-09 20:57:222629void WebContentsImpl::RequestToLockMouse(
2630 RenderWidgetHostImpl* render_widget_host,
2631 bool user_gesture,
lfgad8244352016-07-13 16:55:512632 bool last_unlocked_by_target,
2633 bool privileged) {
lfgf0cd46e2017-01-04 00:05:232634 for (WebContentsImpl* current = this; current;
2635 current = current->GetOuterWebContents()) {
2636 if (current->mouse_lock_widget_) {
2637 render_widget_host->GotResponseToLockMouseRequest(false);
2638 return;
2639 }
[email protected]e9621112011-10-17 05:38:372640 }
avic3aa8422015-11-09 20:57:222641
lfgad8244352016-07-13 16:55:512642 if (privileged) {
lfgf0cd46e2017-01-04 00:05:232643 DCHECK(!GetOuterWebContents());
lfgad8244352016-07-13 16:55:512644 mouse_lock_widget_ = render_widget_host;
2645 render_widget_host->GotResponseToLockMouseRequest(true);
2646 return;
2647 }
2648
lfgbee1e0a2016-06-08 21:24:212649 bool widget_in_frame_tree = false;
2650 for (FrameTreeNode* node : frame_tree_.Nodes()) {
2651 if (node->current_frame_host()->GetRenderWidgetHost() ==
2652 render_widget_host) {
2653 widget_in_frame_tree = true;
2654 break;
2655 }
2656 }
2657
2658 if (widget_in_frame_tree && delegate_) {
lfgf0cd46e2017-01-04 00:05:232659 for (WebContentsImpl* current = this; current;
2660 current = current->GetOuterWebContents()) {
2661 current->mouse_lock_widget_ = render_widget_host;
2662 }
2663
avic3aa8422015-11-09 20:57:222664 delegate_->RequestToLockMouse(this, user_gesture, last_unlocked_by_target);
lfgbee1e0a2016-06-08 21:24:212665 } else {
2666 render_widget_host->GotResponseToLockMouseRequest(false);
2667 }
[email protected]e9621112011-10-17 05:38:372668}
2669
avi3e4b8512015-11-11 01:55:492670void WebContentsImpl::LostMouseLock(RenderWidgetHostImpl* render_widget_host) {
lfgbee1e0a2016-06-08 21:24:212671 CHECK(mouse_lock_widget_);
lfgf0cd46e2017-01-04 00:05:232672
2673 if (mouse_lock_widget_->delegate()->GetAsWebContents() != this)
2674 return mouse_lock_widget_->delegate()->LostMouseLock(render_widget_host);
2675
lfgbee1e0a2016-06-08 21:24:212676 mouse_lock_widget_->SendMouseLockLost();
lfgf0cd46e2017-01-04 00:05:232677 for (WebContentsImpl* current = this; current;
2678 current = current->GetOuterWebContents()) {
2679 current->mouse_lock_widget_ = nullptr;
2680 }
avi3e4b8512015-11-11 01:55:492681
[email protected]e9621112011-10-17 05:38:372682 if (delegate_)
2683 delegate_->LostMouseLock();
2684}
2685
lfgad8244352016-07-13 16:55:512686bool WebContentsImpl::HasMouseLock(RenderWidgetHostImpl* render_widget_host) {
2687 // To verify if the mouse is locked, the mouse_lock_widget_ needs to be
2688 // assigned to the widget that requested the mouse lock, and the top-level
2689 // platform RenderWidgetHostView needs to hold the mouse lock from the OS.
Lucas Gadania0ea0172018-09-20 18:31:372690 auto* widget_host = GetTopLevelRenderWidgetHostView();
2691 return mouse_lock_widget_ == render_widget_host && widget_host &&
2692 widget_host->IsMouseLocked();
lfgad8244352016-07-13 16:55:512693}
2694
lfg84763c92017-02-16 18:55:152695RenderWidgetHostImpl* WebContentsImpl::GetMouseLockWidget() {
Lucas Gadania0ea0172018-09-20 18:31:372696 auto* widget_host = GetTopLevelRenderWidgetHostView();
2697 if ((widget_host && widget_host->IsMouseLocked()) ||
lfge2176fb2017-03-26 00:45:472698 (GetFullscreenRenderWidgetHostView() &&
Lucas Gadania0ea0172018-09-20 18:31:372699 GetFullscreenRenderWidgetHostView()->IsMouseLocked())) {
lfg84763c92017-02-16 18:55:152700 return mouse_lock_widget_;
Lucas Gadania0ea0172018-09-20 18:31:372701 }
lfg84763c92017-02-16 18:55:152702
2703 return nullptr;
2704}
2705
Joe Downing192998b22018-03-22 15:51:362706bool WebContentsImpl::RequestKeyboardLock(
Joe Downing13dd76b2018-04-09 18:32:152707 RenderWidgetHostImpl* render_widget_host,
2708 bool esc_key_locked) {
Joe Downing192998b22018-03-22 15:51:362709 DCHECK(render_widget_host);
Joe Downing192998b22018-03-22 15:51:362710 if (render_widget_host->delegate()->GetAsWebContents() != this) {
2711 NOTREACHED();
2712 return false;
2713 }
2714
2715 // KeyboardLock is only supported when called by the top-level browsing
2716 // context and is not supported in embedded content scenarios.
2717 if (GetOuterWebContents())
2718 return false;
2719
Joe Downing13dd76b2018-04-09 18:32:152720 esc_key_locked_ = esc_key_locked;
Joe Downing192998b22018-03-22 15:51:362721 keyboard_lock_widget_ = render_widget_host;
2722
Joe Downing13dd76b2018-04-09 18:32:152723 if (delegate_)
2724 delegate_->RequestKeyboardLock(this, esc_key_locked_);
Joe Downing192998b22018-03-22 15:51:362725 return true;
2726}
2727
2728void WebContentsImpl::CancelKeyboardLock(
2729 RenderWidgetHostImpl* render_widget_host) {
2730 if (!keyboard_lock_widget_ || render_widget_host != keyboard_lock_widget_)
2731 return;
2732
2733 RenderWidgetHostImpl* old_keyboard_lock_widget = keyboard_lock_widget_;
2734 keyboard_lock_widget_ = nullptr;
2735
Joe Downing13dd76b2018-04-09 18:32:152736 if (delegate_)
2737 delegate_->CancelKeyboardLockRequest(this);
2738
Joe Downing192998b22018-03-22 15:51:362739 old_keyboard_lock_widget->CancelKeyboardLock();
2740}
2741
2742RenderWidgetHostImpl* WebContentsImpl::GetKeyboardLockWidget() {
2743 return keyboard_lock_widget_;
2744}
2745
Dave Tapuskac3344362019-02-20 17:54:282746void WebContentsImpl::OnRenderFrameProxyVisibilityChanged(
2747 blink::mojom::FrameVisibility visibility) {
2748 switch (visibility) {
2749 case blink::mojom::FrameVisibility::kRenderedInViewport:
2750 WasShown();
2751 break;
2752 case blink::mojom::FrameVisibility::kNotRendered:
2753 WasHidden();
2754 break;
2755 case blink::mojom::FrameVisibility::kRenderedOutOfViewport:
2756 WasOccluded();
2757 break;
2758 }
ekaramada4f42692016-02-11 19:48:372759}
2760
Albert J. Wong65fe64d2019-09-20 02:48:142761RenderFrameHostDelegate* WebContentsImpl::CreateNewWindow(
csharrison95f01e922017-04-24 18:52:352762 RenderFrameHost* opener,
rockot5c478a72016-09-28 23:14:182763 const mojom::CreateNewWindowParams& params,
Albert J. Wong65fe64d2019-09-20 02:48:142764 bool is_new_browsing_instance,
Sergey Kuznetsov32257a22019-02-11 20:26:502765 bool has_user_gesture,
[email protected]97714c82012-06-06 10:15:132766 SessionStorageNamespace* session_storage_namespace) {
csharrison95f01e922017-04-24 18:52:352767 DCHECK(opener);
nick5ae4d2d2017-01-06 01:18:352768
csharrison95f01e922017-04-24 18:52:352769 int render_process_id = opener->GetProcess()->GetID();
csharrison95f01e922017-04-24 18:52:352770
Albert J. Wong65fe64d2019-09-20 02:48:142771 SiteInstance* source_site_instance = opener->GetSiteInstance();
nick5ae4d2d2017-01-06 01:18:352772
[email protected]bafe6cd2012-05-23 23:09:502773 // We usually create the new window in the same BrowsingInstance (group of
2774 // script-related windows), by passing in the current SiteInstance. However,
[email protected]c4538072013-03-18 02:17:552775 // if the opener is being suppressed (in a non-guest), we create a new
2776 // SiteInstance in its own BrowsingInstance.
[email protected]a24efc22014-05-26 15:50:252777 bool is_guest = BrowserPluginGuest::IsGuest(this);
[email protected]c4538072013-03-18 02:17:552778
[email protected]bafe6cd2012-05-23 23:09:502779 scoped_refptr<SiteInstance> site_instance =
alexmos4cf2aa32015-07-15 23:40:432780 params.opener_suppressed && !is_guest
2781 ? SiteInstance::CreateForURL(GetBrowserContext(), params.target_url)
2782 : source_site_instance;
[email protected]bafe6cd2012-05-23 23:09:502783
[email protected]d1198fd2012-08-13 22:50:192784 // We must assign the SessionStorageNamespace before calling Init().
[email protected]4c3a23582012-08-18 08:54:342785 //
2786 // http://crbug.com/142685
[email protected]fdac6ade2013-07-20 01:06:302787 const std::string& partition_id =
2788 GetContentClient()->browser()->
2789 GetStoragePartitionIdForSite(GetBrowserContext(),
2790 site_instance->GetSiteURL());
[email protected]fc72bb12013-06-02 21:13:462791 StoragePartition* partition = BrowserContext::GetStoragePartition(
2792 GetBrowserContext(), site_instance.get());
[email protected]5f2aa722013-08-07 16:59:412793 DOMStorageContextWrapper* dom_storage_context =
2794 static_cast<DOMStorageContextWrapper*>(partition->GetDOMStorageContext());
[email protected]d1198fd2012-08-13 22:50:192795 SessionStorageNamespaceImpl* session_storage_namespace_impl =
2796 static_cast<SessionStorageNamespaceImpl*>(session_storage_namespace);
2797 CHECK(session_storage_namespace_impl->IsFromContext(dom_storage_context));
[email protected]dd6730412013-08-14 15:03:372798
Albert J. Wong1ceccef92019-10-08 08:25:202799 if (delegate_ && delegate_->IsWebContentsCreationOverridden(
2800 source_site_instance, params.window_container_type,
2801 opener->GetLastCommittedURL(), params.frame_name,
2802 params.target_url)) {
2803 return static_cast<WebContentsImpl*>(delegate_->CreateCustomWebContents(
2804 opener, source_site_instance, is_new_browsing_instance,
2805 opener->GetLastCommittedURL(), params.frame_name, params.target_url,
2806 partition_id, session_storage_namespace));
[email protected]dd6730412013-08-14 15:03:372807 }
2808
danakj22b4e8b2019-08-01 23:14:392809 bool renderer_started_hidden =
2810 params.disposition == WindowOpenDisposition::NEW_BACKGROUND_TAB;
2811
[email protected]dd6730412013-08-14 15:03:372812 // Create the new web contents. This will automatically create the new
2813 // WebContentsView. In the future, we may want to create the view separately.
[email protected]fc72bb12013-06-02 21:13:462814 CreateParams create_params(GetBrowserContext(), site_instance.get());
nasko48321ca32015-07-02 20:44:122815 create_params.main_frame_name = params.frame_name;
alexmos4cf2aa32015-07-15 23:40:432816 create_params.opener_render_process_id = render_process_id;
csharrison95f01e922017-04-24 18:52:352817 create_params.opener_render_frame_id = opener->GetRoutingID();
[email protected]50d326e2014-05-20 17:59:062818 create_params.opener_suppressed = params.opener_suppressed;
danakj22b4e8b2019-08-01 23:14:392819 create_params.initially_hidden = renderer_started_hidden;
[email protected]50d326e2014-05-20 17:59:062820
Albert J. Wong65fe64d2019-09-20 02:48:142821 // Even though all codepaths leading here are in response to a renderer
2822 // tryng to open a new window, if the new window ends up in a different
2823 // browsing instance, then the RenderViewHost, RenderWidgetHost,
2824 // RenderFrameHost constellation is effectively browser initiated
2825 // the opener's process will not given the routing IDs for the new
2826 // objects.
2827 create_params.renderer_initiated_creation = !is_new_browsing_instance;
Albert J. Wong1ceccef92019-10-08 08:25:202828
2829 // If |is_new_browsing_instance| is true, defer routing_id allocation
2830 // to the WebContentsImpl::Create() call. This is required because with
2831 // a new browsing instance, WebContentsImpl::Create() may elect a different
2832 // SiteInstance from |site_instance| (which happens if |site_instance| is
2833 // nullptr for example).
2834 //
2835 // TODO(ajwong): This routing id allocation should be pushed down further
2836 // into WebContentsImpl::Create().
2837 if (!is_new_browsing_instance) {
2838 create_params.routing_id = opener->GetProcess()->GetNextRoutingID();
2839 create_params.main_frame_routing_id =
2840 opener->GetProcess()->GetNextRoutingID();
2841 create_params.main_frame_widget_routing_id =
2842 opener->GetProcess()->GetNextRoutingID();
2843 }
Albert J. Wong65fe64d2019-09-20 02:48:142844
2845 std::unique_ptr<WebContentsImpl> new_contents;
[email protected]c4538072013-03-18 02:17:552846 if (!is_guest) {
2847 create_params.context = view_->GetNativeView();
Albert J. Wong65fe64d2019-09-20 02:48:142848 new_contents = WebContentsImpl::Create(create_params);
2849 } else {
2850 new_contents = base::WrapUnique(static_cast<WebContentsImpl*>(
2851 GetBrowserPluginGuest()->CreateNewGuestWindow(create_params)));
[email protected]c4538072013-03-18 02:17:552852 }
Albert J. Wong65fe64d2019-09-20 02:48:142853 auto* new_contents_impl = new_contents.get();
danakje6c01622018-10-05 19:39:262854
2855 new_contents_impl->GetController().SetSessionStorageNamespace(
erikchenbee5c9622018-04-27 19:30:252856 partition_id, session_storage_namespace);
[email protected]d1198fd2012-08-13 22:50:192857
alexmos646fec02015-07-25 00:11:492858 // If the new frame has a name, make sure any SiteInstances that can find
2859 // this named frame have proxies for it. Must be called after
2860 // SetSessionStorageNamespace, since this calls CreateRenderView, which uses
2861 // GetSessionStorageNamespace.
2862 if (!params.frame_name.empty())
danakje6c01622018-10-05 19:39:262863 new_contents_impl->GetRenderManager()->CreateProxiesForNewNamedFrame();
alexmos646fec02015-07-25 00:11:492864
[email protected]c4538072013-03-18 02:17:552865 // Save the window for later if we're not suppressing the opener (since it
[email protected]d70bea92013-04-05 04:23:342866 // will be shown immediately).
2867 if (!params.opener_suppressed) {
2868 if (!is_guest) {
danakje6c01622018-10-05 19:39:262869 WebContentsView* new_view = new_contents_impl->view_.get();
[email protected]bafe6cd2012-05-23 23:09:502870
[email protected]d70bea92013-04-05 04:23:342871 // TODO(brettw): It seems bogus that we have to call this function on the
2872 // newly created object and give it one of its own member variables.
danakj22b4e8b2019-08-01 23:14:392873 RenderWidgetHostView* widget_view = new_view->CreateViewForWidget(
danakje6c01622018-10-05 19:39:262874 new_contents_impl->GetRenderViewHost()->GetWidget(), false);
danakj22b4e8b2019-08-01 23:14:392875 if (!renderer_started_hidden) {
2876 // RenderWidgets for frames always initialize as hidden. If the renderer
2877 // created this window as visible, then we show it here.
2878 widget_view->Show();
2879 }
[email protected]d70bea92013-04-05 04:23:342880 }
[email protected]bafe6cd2012-05-23 23:09:502881 // Save the created window associated with the route so we can show it
2882 // later.
Albert J. Wongcb004632018-07-10 22:58:252883 //
2884 // TODO(ajwong): This should be keyed off the RenderFrame routing id or the
2885 // FrameTreeNode id instead of the routing id of the Widget for the main
2886 // frame. https://crbug.com/545684
Albert J. Wong1ceccef92019-10-08 08:25:202887 DCHECK_NE(MSG_ROUTING_NONE, create_params.main_frame_routing_id);
2888 GlobalRoutingID id(render_process_id,
2889 create_params.main_frame_widget_routing_id);
Albert J. Wong65fe64d2019-09-20 02:48:142890 pending_contents_[id] = std::move(new_contents);
danakje6c01622018-10-05 19:39:262891 AddDestructionObserver(new_contents_impl);
[email protected]bafe6cd2012-05-23 23:09:502892 }
2893
2894 if (delegate_) {
Bryan McQuaded2b93c02017-07-14 22:26:312895 delegate_->WebContentsCreated(this, render_process_id,
2896 opener->GetRoutingID(), params.frame_name,
danakje6c01622018-10-05 19:39:262897 params.target_url, new_contents_impl);
[email protected]bafe6cd2012-05-23 23:09:502898 }
2899
Charlie Harrisonf60dd1c2018-08-15 21:15:412900 for (auto& observer : observers_) {
danakje6c01622018-10-05 19:39:262901 observer.DidOpenRequestedURL(new_contents_impl, opener, params.target_url,
Leon Han963dc182018-11-06 05:41:482902 params.referrer.To<Referrer>(),
2903 params.disposition, ui::PAGE_TRANSITION_LINK,
Charlie Harrisonf60dd1c2018-08-15 21:15:412904 false, // started_from_context_menu
2905 true); // renderer_initiated
pnolandaae574e2017-03-06 21:04:212906 }
2907
Avi Drissmanba3b1b342017-08-29 18:16:132908 // Any new WebContents opened while this WebContents is in fullscreen can be
2909 // used to confuse the user, so drop fullscreen.
Avi Drissmand18c51972018-08-15 18:32:092910 ForSecurityDropFullscreen();
Avi Drissmanba3b1b342017-08-29 18:16:132911
[email protected]bafe6cd2012-05-23 23:09:502912 if (params.opener_suppressed) {
2913 // When the opener is suppressed, the original renderer cannot access the
2914 // new window. As a result, we need to show and navigate the window here.
[email protected]eda238a12012-09-07 23:44:002915 bool was_blocked = false;
erikchenbee5c9622018-04-27 19:30:252916
[email protected]eda238a12012-09-07 23:44:002917 if (delegate_) {
danakje6c01622018-10-05 19:39:262918 base::WeakPtr<WebContentsImpl> weak_new_contents =
2919 new_contents_impl->weak_factory_.GetWeakPtr();
nick0d558482017-02-13 22:27:222920
danakje6c01622018-10-05 19:39:262921 gfx::Rect initial_rect; // Report an empty initial rect.
Albert J. Wong65fe64d2019-09-20 02:48:142922 delegate_->AddNewContents(this, std::move(new_contents),
erikchenbee5c9622018-04-27 19:30:252923 params.disposition, initial_rect,
Sergey Kuznetsov32257a22019-02-11 20:26:502924 has_user_gesture, &was_blocked);
danakje6c01622018-10-05 19:39:262925 // The delegate may delete |new_contents_impl| during AddNewContents().
nick0d558482017-02-13 22:27:222926 if (!weak_new_contents)
Albert J. Wong65fe64d2019-09-20 02:48:142927 return nullptr;
[email protected]eda238a12012-09-07 23:44:002928 }
erikchenbee5c9622018-04-27 19:30:252929
[email protected]eda238a12012-09-07 23:44:002930 if (!was_blocked) {
Nasko Oskov83a8cf92018-10-19 14:58:562931 std::unique_ptr<NavigationController::LoadURLParams> load_params =
2932 std::make_unique<NavigationController::LoadURLParams>(
2933 params.target_url);
Nasko Oskov93e7c55c2018-12-19 01:59:292934 load_params->initiator_origin = opener->GetLastCommittedOrigin();
Leon Han963dc182018-11-06 05:41:482935 load_params->referrer = params.referrer.To<Referrer>();
Nasko Oskov83a8cf92018-10-19 14:58:562936 load_params->transition_type = ui::PAGE_TRANSITION_LINK;
2937 load_params->is_renderer_initiated = true;
Sergey Kuznetsov32257a22019-02-11 20:26:502938 load_params->has_user_gesture = has_user_gesture;
mariakhomenkoa4971c12015-07-21 19:04:372939
2940 if (delegate_ && !is_guest &&
2941 !delegate_->ShouldResumeRequestsForCreatedWindow()) {
Nasko Oskov83a8cf92018-10-19 14:58:562942 // We are in asynchronous add new contents path, delay navigation.
2943 DCHECK(!new_contents_impl->delayed_open_url_params_);
2944 new_contents_impl->delayed_load_url_params_ = std::move(load_params);
mariakhomenkoa4971c12015-07-21 19:04:372945 } else {
Nasko Oskov83a8cf92018-10-19 14:58:562946 new_contents_impl->controller_.LoadURLWithParams(*load_params.get());
Nasko Oskov28ebd5b2018-12-07 22:29:332947 if (!is_guest)
2948 new_contents_impl->Focus();
mariakhomenkoa4971c12015-07-21 19:04:372949 }
[email protected]eda238a12012-09-07 23:44:002950 }
[email protected]bafe6cd2012-05-23 23:09:502951 }
Albert J. Wong65fe64d2019-09-20 02:48:142952 return new_contents_impl;
[email protected]bafe6cd2012-05-23 23:09:502953}
2954
Erik Chenc71b51fe2019-09-19 21:34:272955void WebContentsImpl::CreateNewWidget(int32_t render_process_id,
2956 int32_t widget_route_id,
2957 mojo::PendingRemote<mojom::Widget> widget,
2958 RenderViewHostImpl* render_view_host) {
Albert J. Wongf6e13ed2018-09-18 15:25:472959 CreateNewWidget(render_process_id, widget_route_id, /*is_fullscreen=*/false,
Erik Chenc71b51fe2019-09-19 21:34:272960 std::move(widget), render_view_host);
[email protected]bafe6cd2012-05-23 23:09:502961}
2962
Julie Jeongeun Kimcaacf9102019-09-12 02:13:052963void WebContentsImpl::CreateNewFullscreenWidget(
2964 int32_t render_process_id,
2965 int32_t widget_route_id,
Erik Chenc71b51fe2019-09-19 21:34:272966 mojo::PendingRemote<mojom::Widget> widget,
2967 RenderViewHostImpl* render_view_host) {
Albert J. Wongf6e13ed2018-09-18 15:25:472968 CreateNewWidget(render_process_id, widget_route_id, /*is_fullscreen=*/true,
Erik Chenc71b51fe2019-09-19 21:34:272969 std::move(widget), render_view_host);
[email protected]bafe6cd2012-05-23 23:09:502970}
2971
Erik Chenc71b51fe2019-09-19 21:34:272972void WebContentsImpl::CreateNewWidget(int32_t render_process_id,
2973 int32_t route_id,
2974 bool is_fullscreen,
2975 mojo::PendingRemote<mojom::Widget> widget,
2976 RenderViewHostImpl* render_view_host) {
kenrba26d15092016-02-10 22:00:252977 RenderProcessHost* process = RenderProcessHost::FromID(render_process_id);
kenrbb46f86a2015-12-07 07:53:232978 // A message to create a new widget can only come from an active process for
[email protected]a8504022013-12-04 20:23:512979 // this WebContentsImpl instance. If any other process sends the request,
2980 // it is invalid and the process must be terminated.
dcheng57e39e22016-01-21 00:25:382981 if (!HasMatchingProcess(&frame_tree_, render_process_id)) {
Lukasz Anforowicz6f746282018-01-04 23:24:512982 ReceivedBadMessage(process, bad_message::WCI_NEW_WIDGET_PROCESS_MISMATCH);
[email protected]a8504022013-12-04 20:23:512983 return;
2984 }
2985
Dave Tapuska485aca92017-08-08 00:47:582986 RenderWidgetHostImpl* widget_host = new RenderWidgetHostImpl(
2987 this, process, route_id, std::move(widget), IsHidden());
[email protected]cfd80b02014-05-01 17:46:482988 RenderWidgetHostViewBase* widget_view =
2989 static_cast<RenderWidgetHostViewBase*>(
danakj0b246942018-09-17 21:30:272990 view_->CreateViewForChildWidget(widget_host));
[email protected]83918ec2013-01-10 15:37:192991 if (!widget_view)
2992 return;
danakj0b246942018-09-17 21:30:272993 // Fullscreen child widgets are frames, other child widgets are popups, and
2994 // popups should not get activated.
2995 if (!is_fullscreen)
2996 widget_view->SetWidgetType(WidgetType::kPopup);
[email protected]bafe6cd2012-05-23 23:09:502997 // Save the created widget associated with the route so we can show it later.
Lukasz Anforowicz09060bdf72018-08-23 15:53:172998 pending_widget_views_[GlobalRoutingID(render_process_id, route_id)] =
alexmosc2a8cec2016-05-23 22:19:532999 widget_view;
[email protected]bafe6cd2012-05-23 23:09:503000}
3001
alexmosc2a8cec2016-05-23 22:19:533002void WebContentsImpl::ShowCreatedWindow(int process_id,
nick5ae4d2d2017-01-06 01:18:353003 int main_frame_widget_route_id,
[email protected]bafe6cd2012-05-23 23:09:503004 WindowOpenDisposition disposition,
bokan107a47f2015-02-03 23:23:393005 const gfx::Rect& initial_rect,
[email protected]bafe6cd2012-05-23 23:09:503006 bool user_gesture) {
danakje6c01622018-10-05 19:39:263007 // This method is the renderer requesting an existing top level window to
3008 // show a new top level window that the renderer created. Each top level
3009 // window is associated with a WebContents. In this case it was created
3010 // earlier but showing it was deferred until the renderer requested for it
3011 // to be shown. We find that previously created WebContents here.
3012 // TODO(danakj): Why do we defer this show step until the renderer asks for it
3013 // when it will always do so. What needs to happen in the renderer before we
3014 // reach here?
3015 std::unique_ptr<WebContentsImpl> owned_created =
nick5ae4d2d2017-01-06 01:18:353016 GetCreatedWindow(process_id, main_frame_widget_route_id);
danakje6c01622018-10-05 19:39:263017 WebContentsImpl* created = owned_created.get();
3018 // The browser may have rejected the request to make a new window, or the
3019 // renderer could be sending an invalid route id. Ignore the request then.
3020 if (!created)
3021 return;
mariakhomenko44bdc4732015-04-29 01:55:383022
danakje6c01622018-10-05 19:39:263023 // This uses the delegate for the WebContents where the window was created
3024 // from, to control how to show the newly created window.
3025 WebContentsDelegate* delegate = GetDelegate();
nick5ae4d2d2017-01-06 01:18:353026
danakje6c01622018-10-05 19:39:263027 // The delegate can be null in tests, so we must check for it :(.
3028 if (delegate) {
3029 // Mark the web contents as pending resume, then immediately do
3030 // the resume if the delegate wants it.
3031 created->is_resume_pending_ = true;
3032 if (delegate->ShouldResumeRequestsForCreatedWindow())
3033 created->ResumeLoadingCreatedWebContents();
3034
3035 base::WeakPtr<WebContentsImpl> weak_created =
3036 created->weak_factory_.GetWeakPtr();
3037 delegate->AddNewContents(this, std::move(owned_created), disposition,
3038 initial_rect, user_gesture, nullptr);
3039 // The delegate may delete |created| during AddNewContents().
3040 if (!weak_created)
3041 return;
[email protected]eda238a12012-09-07 23:44:003042 }
danakje6c01622018-10-05 19:39:263043
3044 RenderWidgetHostImpl* rwh = created->GetMainFrame()->GetRenderWidgetHost();
3045 DCHECK_EQ(main_frame_widget_route_id, rwh->GetRoutingID());
3046 rwh->Send(new WidgetMsg_SetBounds_ACK(rwh->GetRoutingID()));
[email protected]bafe6cd2012-05-23 23:09:503047}
3048
alexmosc2a8cec2016-05-23 22:19:533049void WebContentsImpl::ShowCreatedWidget(int process_id,
Albert J. Wongf6e13ed2018-09-18 15:25:473050 int widget_route_id,
bokan107a47f2015-02-03 23:23:393051 const gfx::Rect& initial_rect) {
Albert J. Wongf6e13ed2018-09-18 15:25:473052 ShowCreatedWidget(process_id, widget_route_id, false, initial_rect);
[email protected]bafe6cd2012-05-23 23:09:503053}
3054
alexmosc2a8cec2016-05-23 22:19:533055void WebContentsImpl::ShowCreatedFullscreenWidget(int process_id,
Albert J. Wongf6e13ed2018-09-18 15:25:473056 int widget_route_id) {
3057 ShowCreatedWidget(process_id, widget_route_id, true, gfx::Rect());
[email protected]bafe6cd2012-05-23 23:09:503058}
3059
alexmosc2a8cec2016-05-23 22:19:533060void WebContentsImpl::ShowCreatedWidget(int process_id,
3061 int route_id,
[email protected]bafe6cd2012-05-23 23:09:503062 bool is_fullscreen,
bokan107a47f2015-02-03 23:23:393063 const gfx::Rect& initial_rect) {
[email protected]cfd80b02014-05-01 17:46:483064 RenderWidgetHostViewBase* widget_host_view =
alexmosc2a8cec2016-05-23 22:19:533065 static_cast<RenderWidgetHostViewBase*>(
3066 GetCreatedWidget(process_id, route_id));
[email protected]bafe6cd2012-05-23 23:09:503067 if (!widget_host_view)
3068 return;
[email protected]cfd80b02014-05-01 17:46:483069
W. James MacLeanec4125c2019-09-27 18:56:003070 // GetOutermostWebContents() returns |this| if there are no outer WebContents.
W. James MacLean3c5f50462019-10-28 16:24:213071 auto* outer_web_contents = GetOuterWebContents();
3072 auto* outermost_web_contents = GetOutermostWebContents();
W. James MacLeanec4125c2019-09-27 18:56:003073 RenderWidgetHostView* view =
W. James MacLean3c5f50462019-10-28 16:24:213074 outermost_web_contents->GetRenderWidgetHostView();
3075 // It's not entirely obvious why we need the transform only in the case where
3076 // the outer webcontents is not the same as the outermost webcontents. It may
3077 // be due to the fact that oopifs that are children of the mainframe get
3078 // correct values for their screenrects, but deeper cross-process frames do
3079 // not. Hopefully this can be resolved with https://crbug.com/928825.
3080 // Handling these cases separately is needed for http://crbug.com/1015298.
3081 bool needs_transform = this != outermost_web_contents &&
3082 outermost_web_contents != outer_web_contents;
W. James MacLeanec4125c2019-09-27 18:56:003083
3084 gfx::Rect transformed_rect(initial_rect);
3085 RenderWidgetHostView* this_view = GetRenderWidgetHostView();
W. James MacLean3c5f50462019-10-28 16:24:213086 if (needs_transform) {
W. James MacLeanec4125c2019-09-27 18:56:003087 // We need to transform the coordinates of initial_rect.
3088 gfx::Point origin =
3089 this_view->TransformPointToRootCoordSpace(initial_rect.origin());
3090 gfx::Point bottom_right =
3091 this_view->TransformPointToRootCoordSpace(initial_rect.bottom_right());
3092 transformed_rect =
3093 gfx::Rect(origin.x(), origin.y(), bottom_right.x() - origin.x(),
3094 bottom_right.y() - origin.y());
[email protected]cfd80b02014-05-01 17:46:483095 }
3096
danakj0b246942018-09-17 21:30:273097 // Fullscreen child widgets are frames, other child widgets are popups.
[email protected]4aebbca2013-09-17 22:26:493098 if (is_fullscreen) {
[email protected]d7f80ba2013-10-12 07:42:313099 DCHECK_EQ(MSG_ROUTING_NONE, fullscreen_widget_routing_id_);
[email protected]0b431992014-06-24 00:08:033100 view_->StoreFocus();
alexmosc9e76ec2016-05-16 22:59:373101 fullscreen_widget_process_id_ =
3102 widget_host_view->GetRenderWidgetHost()->GetProcess()->GetID();
[email protected]d7f80ba2013-10-12 07:42:313103 fullscreen_widget_routing_id_ = route_id;
[email protected]4aebbca2013-09-17 22:26:493104 if (delegate_ && delegate_->EmbedsFullscreenWidget()) {
3105 widget_host_view->InitAsChild(GetRenderWidgetHostView()->GetNativeView());
Dave Tapuska3ed44192018-05-01 18:53:303106 delegate_->EnterFullscreenModeForTab(this, GURL(),
Dave Tapuskaa4189512019-10-15 20:27:343107 blink::mojom::FullscreenOptions());
[email protected]4aebbca2013-09-17 22:26:493108 } else {
[email protected]cfd80b02014-05-01 17:46:483109 widget_host_view->InitAsFullscreen(view);
[email protected]4aebbca2013-09-17 22:26:493110 }
ericwilligersde386342016-10-19 02:35:093111 for (auto& observer : observers_)
3112 observer.DidShowFullscreenWidget();
[email protected]4aebbca2013-09-17 22:26:493113 if (!widget_host_view->HasFocus())
3114 widget_host_view->Focus();
3115 } else {
W. James MacLeanec4125c2019-09-27 18:56:003116 widget_host_view->InitAsPopup(view, transformed_rect);
[email protected]4aebbca2013-09-17 22:26:493117 }
[email protected]89054502012-06-03 10:29:243118
yiyixcb1fbc4f2018-03-16 19:54:083119 RenderWidgetHostImpl* render_widget_host_impl = widget_host_view->host();
[email protected]89054502012-06-03 10:29:243120 render_widget_host_impl->Init();
[email protected]d7f80ba2013-10-12 07:42:313121 // Only allow privileged mouse lock for fullscreen render widget, which is
3122 // used to implement Pepper Flash fullscreen.
3123 render_widget_host_impl->set_allow_privileged_mouse_lock(is_fullscreen);
[email protected]bafe6cd2012-05-23 23:09:503124}
3125
danakje6c01622018-10-05 19:39:263126std::unique_ptr<WebContentsImpl> WebContentsImpl::GetCreatedWindow(
nick5ae4d2d2017-01-06 01:18:353127 int process_id,
3128 int main_frame_widget_route_id) {
Lukasz Anforowicz09060bdf72018-08-23 15:53:173129 auto key = GlobalRoutingID(process_id, main_frame_widget_route_id);
nick5ae4d2d2017-01-06 01:18:353130 auto iter = pending_contents_.find(key);
[email protected]bafe6cd2012-05-23 23:09:503131
3132 // Certain systems can block the creation of new windows. If we didn't succeed
3133 // in creating one, just return NULL.
alexmosc2a8cec2016-05-23 22:19:533134 if (iter == pending_contents_.end())
3135 return nullptr;
[email protected]bafe6cd2012-05-23 23:09:503136
danakje6c01622018-10-05 19:39:263137 std::unique_ptr<WebContentsImpl> new_contents = std::move(iter->second);
nick5ae4d2d2017-01-06 01:18:353138 pending_contents_.erase(key);
danakje6c01622018-10-05 19:39:263139 RemoveDestructionObserver(new_contents.get());
[email protected]bafe6cd2012-05-23 23:09:503140
[email protected]d70bea92013-04-05 04:23:343141 // Don't initialize the guest WebContents immediately.
danakje6c01622018-10-05 19:39:263142 if (BrowserPluginGuest::IsGuest(new_contents.get()))
[email protected]d70bea92013-04-05 04:23:343143 return new_contents;
3144
Lukasz Anforowicz5510ed652018-06-06 16:16:193145 if (!new_contents->GetMainFrame()->GetProcess()->IsInitializedAndNotDead() ||
nick5ae4d2d2017-01-06 01:18:353146 !new_contents->GetMainFrame()->GetView()) {
alexmosc2a8cec2016-05-23 22:19:533147 return nullptr;
nick5ae4d2d2017-01-06 01:18:353148 }
[email protected]bafe6cd2012-05-23 23:09:503149
[email protected]bafe6cd2012-05-23 23:09:503150 return new_contents;
3151}
3152
alexmosc2a8cec2016-05-23 22:19:533153RenderWidgetHostView* WebContentsImpl::GetCreatedWidget(int process_id,
3154 int route_id) {
Lukasz Anforowicz09060bdf72018-08-23 15:53:173155 auto iter = pending_widget_views_.find(GlobalRoutingID(process_id, route_id));
[email protected]bafe6cd2012-05-23 23:09:503156 if (iter == pending_widget_views_.end()) {
3157 DCHECK(false);
alexmosc2a8cec2016-05-23 22:19:533158 return nullptr;
[email protected]bafe6cd2012-05-23 23:09:503159 }
3160
3161 RenderWidgetHostView* widget_host_view = iter->second;
Lukasz Anforowicz09060bdf72018-08-23 15:53:173162 pending_widget_views_.erase(GlobalRoutingID(process_id, route_id));
[email protected]bafe6cd2012-05-23 23:09:503163
3164 RenderWidgetHost* widget_host = widget_host_view->GetRenderWidgetHost();
Lukasz Anforowicz5510ed652018-06-06 16:16:193165 if (!widget_host->GetProcess()->IsInitializedAndNotDead()) {
[email protected]bafe6cd2012-05-23 23:09:503166 // The view has gone away or the renderer crashed. Nothing to do.
alexmosc2a8cec2016-05-23 22:19:533167 return nullptr;
[email protected]bafe6cd2012-05-23 23:09:503168 }
3169
3170 return widget_host_view;
3171}
3172
[email protected]f13b4202012-06-12 23:53:233173void WebContentsImpl::RequestMediaAccessPermission(
[email protected]33662e52013-01-07 21:31:093174 const MediaStreamRequest& request,
Mark Pilgrim57499082018-06-12 12:38:303175 MediaResponseCallback callback) {
[email protected]d19b84b2014-03-14 11:52:373176 if (delegate_) {
Mark Pilgrim57499082018-06-12 12:38:303177 delegate_->RequestMediaAccessPermission(this, request, std::move(callback));
[email protected]d19b84b2014-03-14 11:52:373178 } else {
Antonio Gomes0d42960a2019-06-05 12:35:513179 std::move(callback).Run(
3180 blink::MediaStreamDevices(),
3181 blink::mojom::MediaStreamRequestResult::FAILED_DUE_TO_SHUTDOWN,
3182 std::unique_ptr<MediaStreamUI>());
[email protected]d19b84b2014-03-14 11:52:373183 }
[email protected]f13b4202012-06-12 23:53:233184}
3185
Chandan Padhia4b8bcb72017-09-12 16:41:043186bool WebContentsImpl::CheckMediaAccessPermission(
Raymes Khouryad7c24a12018-03-05 23:22:583187 RenderFrameHost* render_frame_host,
Chandan Padhia4b8bcb72017-09-12 16:41:043188 const url::Origin& security_origin,
Antonio Gomesc8b734b2019-06-05 18:22:163189 blink::mojom::MediaStreamType type) {
3190 DCHECK(type == blink::mojom::MediaStreamType::DEVICE_AUDIO_CAPTURE ||
3191 type == blink::mojom::MediaStreamType::DEVICE_VIDEO_CAPTURE);
Chandan Padhia4b8bcb72017-09-12 16:41:043192 return delegate_ && delegate_->CheckMediaAccessPermission(
Raymes Khouryad7c24a12018-03-05 23:22:583193 render_frame_host, security_origin.GetURL(), type);
grunell657d4d82014-09-18 00:09:433194}
3195
Guido Urdaneta73fa6632019-01-14 18:46:263196std::string WebContentsImpl::GetDefaultMediaDeviceID(
Antonio Gomesc8b734b2019-06-05 18:22:163197 blink::mojom::MediaStreamType type) {
guidou8a440b842017-01-30 13:58:433198 if (!delegate_)
3199 return std::string();
3200 return delegate_->GetDefaultMediaDeviceID(this, type);
3201}
3202
[email protected]cc9200432013-07-23 23:02:403203SessionStorageNamespace* WebContentsImpl::GetSessionStorageNamespace(
3204 SiteInstance* instance) {
3205 return controller_.GetSessionStorageNamespace(instance);
3206}
3207
[email protected]6de7fc482014-06-06 10:46:443208SessionStorageNamespaceMap WebContentsImpl::GetSessionStorageNamespaceMap() {
3209 return controller_.GetSessionStorageNamespaceMap();
3210}
3211
[email protected]9b159a52013-10-03 17:24:553212FrameTree* WebContentsImpl::GetFrameTree() {
3213 return &frame_tree_;
3214}
3215
aelias5252baa2016-04-10 01:18:023216bool WebContentsImpl::IsOverridingUserAgent() {
3217 return GetController().GetVisibleEntry() &&
3218 GetController().GetVisibleEntry()->GetIsOverridingUserAgent();
3219}
3220
avi85ee8362016-10-08 02:09:083221bool WebContentsImpl::IsJavaScriptDialogShowing() const {
3222 return is_showing_javascript_dialog_;
3223}
3224
avic031d392017-03-03 03:09:423225bool WebContentsImpl::ShouldIgnoreUnresponsiveRenderer() {
3226 // Ignore unresponsive renderers if the debugger is attached to them since the
3227 // unresponsiveness might be a result of the renderer sitting on a breakpoint.
3228 //
David Bienvenu525f8a02019-02-15 02:02:423229#ifdef OS_WIN
3230 // Check if a windows debugger is attached to the renderer process.
3231 base::ProcessHandle process_handle =
3232 GetMainFrame()->GetProcess()->GetProcess().Handle();
3233 BOOL debugger_present = FALSE;
3234 if (CheckRemoteDebuggerPresent(process_handle, &debugger_present) &&
3235 debugger_present)
3236 return true;
3237#endif // OS_WIN
3238
avic031d392017-03-03 03:09:423239 // TODO(pfeldman): Fix this to only return true if the renderer is *actually*
3240 // sitting on a breakpoint. https://crbug.com/684202
3241 return DevToolsAgentHost::IsDebuggerAttached(this);
3242}
3243
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:413244ui::AXMode WebContentsImpl::GetAccessibilityMode() {
[email protected]95640212014-07-26 18:14:303245 return accessibility_mode_;
3246}
3247
Dominic Mazzonia7b0edb22017-08-09 16:32:513248void WebContentsImpl::AccessibilityEventReceived(
Dominic Mazzoniccbaa9b2018-06-06 07:44:233249 const AXEventNotificationDetails& details) {
ericwilligersde386342016-10-19 02:35:093250 for (auto& observer : observers_)
Dominic Mazzonia7b0edb22017-08-09 16:32:513251 observer.AccessibilityEventReceived(details);
[email protected]31a71eaf2014-03-13 01:47:363252}
3253
dmazzoni2400c462016-08-23 15:07:133254void WebContentsImpl::AccessibilityLocationChangesReceived(
3255 const std::vector<AXLocationChangeNotificationDetails>& details) {
ericwilligersde386342016-10-19 02:35:093256 for (auto& observer : observers_)
Dominic Mazzonia7b0edb22017-08-09 16:32:513257 observer.AccessibilityLocationChangesReceived(details);
dmazzoni2400c462016-08-23 15:07:133258}
3259
Abigail Kleinabb428332019-09-13 18:26:213260base::string16 WebContentsImpl::DumpAccessibilityTree(
3261 bool internal,
3262 std::vector<AccessibilityTreeFormatter::PropertyFilter> property_filters) {
James Wallace-Leeeafc94cb92018-07-23 21:35:093263 auto* ax_mgr = GetOrCreateRootBrowserAccessibilityManager();
3264 DCHECK(ax_mgr);
Abigail Kleinabb428332019-09-13 18:26:213265 return AccessibilityTreeFormatter::DumpAccessibilityTreeFromManager(
3266 ax_mgr, internal, property_filters);
James Wallace-Leeeafc94cb92018-07-23 21:35:093267}
3268
Abigail Kleinaa898742019-11-01 02:31:253269void WebContentsImpl::RecordAccessibilityEvents(
3270 AccessibilityEventCallback callback,
3271 bool start) {
3272 if (start) {
3273 SetAccessibilityMode(ui::AXMode::kWebContents);
3274 auto* ax_mgr = GetOrCreateRootBrowserAccessibilityManager();
3275 DCHECK(ax_mgr);
3276 base::ProcessId pid = base::Process::Current().Pid();
3277 event_recorder_ = content::AccessibilityEventRecorder::Create(
3278 ax_mgr, pid, base::StringPiece{});
3279 event_recorder_->ListenToEvents(callback);
3280 } else {
3281 DCHECK(event_recorder_);
3282 event_recorder_->FlushAsyncEvents();
3283 event_recorder_ = nullptr;
3284 }
3285}
3286
dmazzonia656928e2014-09-15 20:28:543287RenderFrameHost* WebContentsImpl::GetGuestByInstanceID(
fsamuel833ee7c2015-02-13 23:40:403288 RenderFrameHost* render_frame_host,
dmazzonia656928e2014-09-15 20:28:543289 int browser_plugin_instance_id) {
3290 BrowserPluginGuestManager* guest_manager =
3291 GetBrowserContext()->GetGuestManager();
fsamuel9cbc00a2015-04-17 20:30:203292 if (!guest_manager)
3293 return nullptr;
3294
dmazzonia656928e2014-09-15 20:28:543295 WebContents* guest = guest_manager->GetGuestByInstanceID(
fsamuel2e9413d2015-02-25 01:25:443296 render_frame_host->GetProcess()->GetID(), browser_plugin_instance_id);
dmazzonia656928e2014-09-15 20:28:543297 if (!guest)
fsamuel9cbc00a2015-04-17 20:30:203298 return nullptr;
3299
dmazzonia656928e2014-09-15 20:28:543300 return guest->GetMainFrame();
3301}
3302
Ke He7319dbe2017-11-09 05:54:443303device::mojom::GeolocationContext* WebContentsImpl::GetGeolocationContext() {
3304 if (geolocation_context_)
3305 return geolocation_context_.get();
3306
Ken Rockotfd03e682019-06-20 21:13:543307 service_manager::Connector* connector = GetSystemConnector();
Miyoung Shin0f4480d2019-09-12 01:15:263308 auto receiver = geolocation_context_.BindNewPipeAndPassReceiver();
Ken Rockotfd03e682019-06-20 21:13:543309 if (connector)
Miyoung Shin0f4480d2019-09-12 01:15:263310 connector->Connect(device::mojom::kServiceName, std::move(receiver));
Conley Owens6894c4f2017-07-10 19:29:133311 return geolocation_context_.get();
blundellc57b93f2014-10-29 13:19:573312}
3313
ke.he98b761e2017-05-09 05:59:173314device::mojom::WakeLockContext* WebContentsImpl::GetWakeLockContext() {
blundelld8cd72b2017-03-28 07:18:383315 if (!wake_lock_context_host_)
3316 wake_lock_context_host_.reset(new WakeLockContextHost(this));
blundelle75a8f92017-03-27 08:11:173317 return wake_lock_context_host_->GetWakeLockContext();
alogvinovf50445a2015-10-30 13:00:123318}
3319
blundellf5316fc2017-05-15 11:49:033320#if defined(OS_ANDROID)
Miyoung Shin59f39ff2019-09-17 07:23:203321void WebContentsImpl::GetNFC(
3322 mojo::PendingReceiver<device::mojom::NFC> receiver) {
Leon Hanccb985f2019-08-26 02:43:243323 NFCHost nfc_host(this);
Miyoung Shin59f39ff2019-09-17 07:23:203324 nfc_host.GetNFC(std::move(receiver));
blundellf5316fc2017-05-15 11:49:033325}
3326#endif
3327
Evan Stade5e750ad02017-08-07 21:59:273328void WebContentsImpl::SetNotWaitingForResponse() {
3329 if (waiting_for_response_ == false)
3330 return;
3331
3332 waiting_for_response_ = false;
3333 if (delegate_)
3334 delegate_->LoadingStateChanged(this, is_load_to_different_document_);
Chris Hamiltonf82f2a702018-02-15 19:37:293335 for (auto& observer : observers_)
3336 observer.DidReceiveResponse();
Evan Stade5e750ad02017-08-07 21:59:273337}
3338
lfgbb9c28a2016-03-01 03:19:493339void WebContentsImpl::SendScreenRects() {
kenrbc52e1b4e2016-03-20 01:03:373340 for (FrameTreeNode* node : frame_tree_.Nodes()) {
kenrb0e8dc202016-05-30 19:51:403341 if (node->current_frame_host()->is_local_root())
kenrbc52e1b4e2016-03-20 01:03:373342 node->current_frame_host()->GetRenderWidgetHost()->SendScreenRects();
3343 }
lfgbb9c28a2016-03-01 03:19:493344
danakj4df8a9d02019-02-05 20:36:593345 // Interstitials have their own |frame_tree_|.
3346 if (interstitial_page_) {
3347 FrameTree* interstitial_frame_tree = interstitial_page_->GetFrameTree();
3348 for (FrameTreeNode* node : interstitial_frame_tree->Nodes()) {
3349 if (node->current_frame_host()->is_local_root())
3350 node->current_frame_host()->GetRenderWidgetHost()->SendScreenRects();
3351 }
lfgdb5c4ed2016-03-04 23:09:073352 }
3353
avallee9f43b012016-11-26 07:02:103354 if (browser_plugin_embedder_ && !is_being_destroyed_)
[email protected]a7568e62013-06-14 07:50:443355 browser_plugin_embedder_->DidSendScreenRects();
[email protected]32deec62013-05-15 23:55:043356}
3357
ekaramadadd882292016-06-08 15:22:563358TextInputManager* WebContentsImpl::GetTextInputManager() {
3359 if (GetOuterWebContents())
3360 return GetOuterWebContents()->GetTextInputManager();
3361
Ehsan Karamadc179dc02018-12-15 03:09:293362 if (!text_input_manager_ && !browser_plugin_guest_) {
Darren Shenca53d5f2018-05-15 04:56:013363 text_input_manager_.reset(new TextInputManager(
3364 GetBrowserContext() &&
3365 !GetBrowserContext()->IsOffTheRecord()) /* should_do_learning */);
3366 }
ekaramadadd882292016-06-08 15:22:563367
3368 return text_input_manager_.get();
3369}
3370
paulmeyer7f6f1d4f2016-11-15 00:00:273371bool WebContentsImpl::OnUpdateDragCursor() {
Lei Zhang6410ec1a2019-01-31 21:38:583372 return browser_plugin_embedder_ &&
3373 browser_plugin_embedder_->OnUpdateDragCursor();
paulmeyer7f6f1d4f2016-11-15 00:00:273374}
3375
kenrb11f213a2017-03-24 18:12:063376bool WebContentsImpl::IsWidgetForMainFrame(
3377 RenderWidgetHostImpl* render_widget_host) {
3378 return render_widget_host == GetMainFrame()->GetRenderWidgetHost();
3379}
3380
[email protected]95640212014-07-26 18:14:303381BrowserAccessibilityManager*
3382 WebContentsImpl::GetRootBrowserAccessibilityManager() {
Dominic Mazzoniefb5f9e32017-12-21 01:08:223383 RenderFrameHostImpl* rfh = static_cast<RenderFrameHostImpl*>(
3384 ShowingInterstitialPage() ? GetInterstitialPage()->GetMainFrame()
3385 : GetMainFrame());
creisc014b402015-04-23 16:41:453386 return rfh ? rfh->browser_accessibility_manager() : nullptr;
[email protected]95640212014-07-26 18:14:303387}
3388
3389BrowserAccessibilityManager*
3390 WebContentsImpl::GetOrCreateRootBrowserAccessibilityManager() {
Dominic Mazzoniefb5f9e32017-12-21 01:08:223391 RenderFrameHostImpl* rfh = static_cast<RenderFrameHostImpl*>(
3392 ShowingInterstitialPage() ? GetInterstitialPage()->GetMainFrame()
3393 : GetMainFrame());
creisc014b402015-04-23 16:41:453394 return rfh ? rfh->GetOrCreateBrowserAccessibilityManager() : nullptr;
[email protected]95640212014-07-26 18:14:303395}
3396
Dave Tapuskab336b922017-07-06 19:24:053397void WebContentsImpl::ExecuteEditCommand(
3398 const std::string& command,
3399 const base::Optional<base::string16>& value) {
Dave Tapuskae782bea2019-07-09 16:21:513400 auto* input_handler = GetFocusedFrameInputHandler();
3401 if (!input_handler)
Dave Tapuskab336b922017-07-06 19:24:053402 return;
3403
Dave Tapuskae782bea2019-07-09 16:21:513404 input_handler->ExecuteEditCommand(command, value);
Dave Tapuskab336b922017-07-06 19:24:053405}
3406
mohsen7ab1ec16ec2015-07-02 18:26:233407void WebContentsImpl::MoveRangeSelectionExtent(const gfx::Point& extent) {
Dave Tapuskae782bea2019-07-09 16:21:513408 auto* input_handler = GetFocusedFrameInputHandler();
3409 if (!input_handler)
mohsen7ab1ec16ec2015-07-02 18:26:233410 return;
3411
Dave Tapuskae782bea2019-07-09 16:21:513412 input_handler->MoveRangeSelectionExtent(extent);
mohsen7ab1ec16ec2015-07-02 18:26:233413}
3414
3415void WebContentsImpl::SelectRange(const gfx::Point& base,
3416 const gfx::Point& extent) {
Dave Tapuskae782bea2019-07-09 16:21:513417 auto* input_handler = GetFocusedFrameInputHandler();
3418 if (!input_handler)
mohsen7ab1ec16ec2015-07-02 18:26:233419 return;
3420
Dave Tapuskae782bea2019-07-09 16:21:513421 input_handler->SelectRange(base, extent);
mohsen7ab1ec16ec2015-07-02 18:26:233422}
3423
Dave Tapuskab336b922017-07-06 19:24:053424void WebContentsImpl::MoveCaret(const gfx::Point& extent) {
Dave Tapuskae782bea2019-07-09 16:21:513425 auto* input_handler = GetFocusedFrameInputHandler();
3426 if (!input_handler)
Dave Tapuskab336b922017-07-06 19:24:053427 return;
3428
Dave Tapuskae782bea2019-07-09 16:21:513429 input_handler->MoveCaret(extent);
Dave Tapuskab336b922017-07-06 19:24:053430}
3431
Shimi Zhang37deeb22017-09-28 00:59:013432void WebContentsImpl::AdjustSelectionByCharacterOffset(
3433 int start_adjust,
3434 int end_adjust,
3435 bool show_selection_menu) {
Dave Tapuskae782bea2019-07-09 16:21:513436 auto* input_handler = GetFocusedFrameInputHandler();
3437 if (!input_handler)
aurimasab0319022015-07-10 21:57:383438 return;
3439
Shimi Zhang37deeb22017-09-28 00:59:013440 using blink::mojom::SelectionMenuBehavior;
Dave Tapuskae782bea2019-07-09 16:21:513441 input_handler->AdjustSelectionByCharacterOffset(
Shimi Zhang37deeb22017-09-28 00:59:013442 start_adjust, end_adjust,
3443 show_selection_menu ? SelectionMenuBehavior::kShow
3444 : SelectionMenuBehavior::kHide);
aurimasab0319022015-07-10 21:57:383445}
3446
[email protected]b172aee2012-04-10 17:05:263447void WebContentsImpl::UpdatePreferredSize(const gfx::Size& pref_size) {
[email protected]222f5822014-02-05 23:40:493448 const gfx::Size old_size = GetPreferredSize();
[email protected]bcd2815602012-01-14 18:17:233449 preferred_size_ = pref_size;
[email protected]222f5822014-02-05 23:40:493450 OnPreferredSizeChanged(old_size);
[email protected]0548c5352011-09-07 00:33:333451}
3452
avic3aa8422015-11-09 20:57:223453void WebContentsImpl::ResizeDueToAutoResize(
3454 RenderWidgetHostImpl* render_widget_host,
Fady Samuel97947112018-05-05 16:24:543455 const gfx::Size& new_size) {
avic3aa8422015-11-09 20:57:223456 if (render_widget_host != GetRenderViewHost()->GetWidget())
3457 return;
3458
kenrbe4f91082017-06-02 20:32:083459 auto_resize_size_ = new_size;
3460
3461 // Out-of-process iframe visible viewport sizes usually come from the
3462 // top-level RenderWidgetHostView, but when auto-resize is enabled on the
3463 // top frame then that size is used instead.
3464 for (FrameTreeNode* node : frame_tree_.Nodes()) {
3465 if (node->current_frame_host()->is_local_root()) {
3466 RenderWidgetHostImpl* host =
3467 node->current_frame_host()->GetRenderWidgetHost();
3468 if (host != render_widget_host)
Fady Samuel0b911822018-04-25 13:22:163469 host->SynchronizeVisualProperties();
kenrbe4f91082017-06-02 20:32:083470 }
3471 }
3472
[email protected]61e2b3cc2012-03-02 16:13:343473 if (delegate_)
3474 delegate_->ResizeDueToAutoResize(this, new_size);
3475}
3476
kenrbe4f91082017-06-02 20:32:083477gfx::Size WebContentsImpl::GetAutoResizeSize() {
3478 return auto_resize_size_;
3479}
3480
3481void WebContentsImpl::ResetAutoResizeSize() {
3482 auto_resize_size_ = gfx::Size();
3483}
3484
Albert J. Wongeb28e7e2018-10-12 00:57:433485InputEventShim* WebContentsImpl::GetInputEventShim() const {
3486 // The only thing that intercepts text input and mouse lock is the
3487 // BrowserPluginGuest. Delegate to the BrowserPluginGuest logic for
3488 // whether or not it needs to shim these events.
3489 if (browser_plugin_guest_) {
3490 return browser_plugin_guest_->GetInputEventShim();
3491 }
3492 return nullptr;
3493}
3494
[email protected]b172aee2012-04-10 17:05:263495WebContents* WebContentsImpl::OpenURL(const OpenURLParams& params) {
Bo Liu300c6052018-06-12 04:46:073496 if (!delegate_) {
3497 // Embedder can delay setting a delegate on new WebContents with
3498 // WebContentsDelegate::ShouldResumeRequestsForCreatedWindow. In the mean
3499 // time, navigations, including the initial one, that goes through OpenURL
3500 // should be delayed until embedder is ready to resume loading.
3501 delayed_open_url_params_ = std::make_unique<OpenURLParams>(params);
Nasko Oskov83a8cf92018-10-19 14:58:563502
3503 // If there was a navigation deferred when creating the window through
3504 // CreateNewWindow, drop it in favor of this navigation.
3505 delayed_load_url_params_.reset();
3506
Ivan Kotenkov2c0d2bb32017-11-01 15:41:283507 return nullptr;
Bo Liu300c6052018-06-12 04:46:073508 }
[email protected]00c37fc2011-08-02 00:22:503509
[email protected]e5d549d2011-12-28 01:29:203510 WebContents* new_contents = delegate_->OpenURLFromTab(this, params);
pnoland488944652017-02-22 18:58:543511
3512 RenderFrameHost* source_render_frame_host = RenderFrameHost::FromID(
3513 params.source_render_process_id, params.source_render_frame_id);
3514
3515 if (source_render_frame_host && params.source_site_instance) {
3516 CHECK_EQ(source_render_frame_host->GetSiteInstance(),
3517 params.source_site_instance.get());
3518 }
3519
3520 if (new_contents && source_render_frame_host && new_contents != this) {
3521 for (auto& observer : observers_) {
Lukasz Anforowicz48c9f872018-08-01 00:58:413522 observer.DidOpenRequestedURL(
3523 new_contents, source_render_frame_host, params.url, params.referrer,
3524 params.disposition, params.transition,
3525 params.started_from_context_menu, params.is_renderer_initiated);
pnoland488944652017-02-22 18:58:543526 }
3527 }
3528
[email protected]e5d549d2011-12-28 01:29:203529 return new_contents;
[email protected]d5f942ba2008-09-26 19:30:343530}
3531
[email protected]a86c0e962013-12-17 17:10:393532void WebContentsImpl::RenderFrameForInterstitialPageCreated(
3533 RenderFrameHost* render_frame_host) {
ericwilligersde386342016-10-19 02:35:093534 for (auto& observer : observers_)
3535 observer.RenderFrameForInterstitialPageCreated(render_frame_host);
[email protected]ba45bfd2012-05-22 21:51:443536}
3537
[email protected]20ca0382013-02-28 19:50:073538void WebContentsImpl::AttachInterstitialPage(
3539 InterstitialPageImpl* interstitial_page) {
creisadca9862017-06-16 19:12:113540 DCHECK(!interstitial_page_ && interstitial_page);
3541 interstitial_page_ = interstitial_page;
[email protected]90fb08ed2013-09-24 17:43:293542
3543 // Cancel any visible dialogs so that they don't interfere with the
3544 // interstitial.
creis89a0f782015-05-27 16:13:173545 CancelActiveAndPendingDialogs();
[email protected]90fb08ed2013-09-24 17:43:293546
ericwilligersde386342016-10-19 02:35:093547 for (auto& observer : observers_)
3548 observer.DidAttachInterstitialPage();
clamy44e84ce2016-02-22 15:38:253549
3550 // Stop the throbber if needed while the interstitial page is shown.
3551 if (frame_tree_.IsLoading())
3552 LoadingStateChanged(true, true, nullptr);
lfg1453e412017-04-11 00:48:503553
3554 // Connect to outer WebContents if necessary.
3555 if (node_.OuterContentsFrameTreeNode()) {
3556 if (GetRenderManager()->GetProxyToOuterDelegate()) {
3557 DCHECK(
3558 static_cast<RenderWidgetHostViewBase*>(interstitial_page->GetView())
3559 ->IsRenderWidgetHostViewChildFrame());
3560 RenderWidgetHostViewChildFrame* view =
3561 static_cast<RenderWidgetHostViewChildFrame*>(
3562 interstitial_page->GetView());
3563 GetRenderManager()->SetRWHViewForInnerContents(view);
3564 }
3565 }
Bo Liu7c6779e92017-08-16 02:02:283566
Bo Liu168c8642017-08-28 18:26:023567#if defined(OS_ANDROID)
Bo Liu7c6779e92017-08-16 02:02:283568 // Update importance of the interstitial.
3569 static_cast<RenderFrameHostImpl*>(interstitial_page_->GetMainFrame())
3570 ->GetRenderWidgetHost()
3571 ->SetImportance(GetMainFrame()->GetRenderWidgetHost()->importance());
Bo Liu168c8642017-08-28 18:26:023572#endif
Dominic Mazzoniefb5f9e32017-12-21 01:08:223573
3574 if (accessibility_mode_.has_mode(ui::AXMode::kNativeAPIs)) {
3575 // Make sure that the main page's accessibility tree is hidden and the
3576 // interstitial gets focus.
3577 RenderFrameHostImpl* rfh =
3578 static_cast<RenderFrameHostImpl*>(GetMainFrame());
3579 if (rfh) {
3580 BrowserAccessibilityManager* accessibility_manager =
3581 rfh->browser_accessibility_manager();
3582 if (accessibility_manager)
3583 accessibility_manager->set_hidden_by_interstitial_page(true);
3584 }
3585 rfh = static_cast<RenderFrameHostImpl*>(
3586 GetInterstitialPage()->GetMainFrame());
3587 if (rfh) {
3588 BrowserAccessibilityManager* accessibility_manager =
3589 rfh->GetOrCreateBrowserAccessibilityManager();
3590 if (accessibility_manager)
3591 accessibility_manager->OnWindowFocused();
3592 }
3593 }
clamy44e84ce2016-02-22 15:38:253594}
3595
3596void WebContentsImpl::DidProceedOnInterstitial() {
3597 // The interstitial page should no longer be pausing the throbber.
creisadca9862017-06-16 19:12:113598 DCHECK(!(ShowingInterstitialPage() && interstitial_page_->pause_throbber()));
clamy44e84ce2016-02-22 15:38:253599
3600 // Restart the throbber now that the interstitial page no longer pauses it.
3601 if (ShowingInterstitialPage() && frame_tree_.IsLoading())
3602 LoadingStateChanged(true, true, nullptr);
[email protected]20ca0382013-02-28 19:50:073603}
3604
Shivani Sharma985474f2019-05-23 21:36:473605bool WebContentsImpl::HadInnerWebContents() {
3606 return had_inner_webcontents_;
3607}
3608
Lucas Furukawa Gadani7f1241d2017-07-12 01:45:043609void WebContentsImpl::DetachInterstitialPage(bool has_focus) {
Lucas Furukawa Gadani69b609192017-07-10 17:08:343610 bool interstitial_pausing_throbber =
3611 ShowingInterstitialPage() && interstitial_page_->pause_throbber();
3612 if (ShowingInterstitialPage())
3613 interstitial_page_ = nullptr;
Lucas Furukawa Gadani7f1241d2017-07-12 01:45:043614
Dominic Mazzoniefb5f9e32017-12-21 01:08:223615 // Make sure that the main page's accessibility tree is no longer
3616 // suppressed.
3617 RenderFrameHostImpl* rfh = GetMainFrame();
3618 if (rfh) {
3619 BrowserAccessibilityManager* accessibility_manager =
3620 rfh->browser_accessibility_manager();
3621 if (accessibility_manager)
3622 accessibility_manager->set_hidden_by_interstitial_page(false);
3623 }
3624
Lucas Furukawa Gadani7f1241d2017-07-12 01:45:043625 // If the focus was on the interstitial, let's keep it to the page.
3626 // (Note that in unit-tests the RVH may not have a view).
3627 if (has_focus && GetRenderViewHost()->GetWidget()->GetView())
3628 GetRenderViewHost()->GetWidget()->GetView()->Focus();
3629
Lucas Furukawa Gadani69b609192017-07-10 17:08:343630 for (auto& observer : observers_)
3631 observer.DidDetachInterstitialPage();
3632
3633 // Disconnect from outer WebContents if necessary. This must happen after the
3634 // interstitial page is cleared above, since the call to
3635 // SetRWHViewForInnerContents below may loop over all the
3636 // RenderWidgetHostViews in the tree (otherwise, including the now-deleted
3637 // view for the interstitial).
lfg1453e412017-04-11 00:48:503638 if (node_.OuterContentsFrameTreeNode()) {
3639 if (GetRenderManager()->GetProxyToOuterDelegate()) {
3640 DCHECK(static_cast<RenderWidgetHostViewBase*>(
3641 GetRenderManager()->current_frame_host()->GetView())
3642 ->IsRenderWidgetHostViewChildFrame());
3643 RenderWidgetHostViewChildFrame* view =
3644 static_cast<RenderWidgetHostViewChildFrame*>(
3645 GetRenderManager()->current_frame_host()->GetView());
3646 GetRenderManager()->SetRWHViewForInnerContents(view);
3647 }
3648 }
3649
clamy44e84ce2016-02-22 15:38:253650 // Restart the throbber if needed now that the interstitial page is going
3651 // away.
3652 if (interstitial_pausing_throbber && frame_tree_.IsLoading())
3653 LoadingStateChanged(true, true, nullptr);
[email protected]20ca0382013-02-28 19:50:073654}
3655
avi2b177592014-12-10 02:08:023656void WebContentsImpl::SetHistoryOffsetAndLength(int history_offset,
3657 int history_length) {
alexmos136fd6e62016-08-15 20:58:413658 SendPageMessage(new PageMsg_SetHistoryOffsetAndLength(
3659 MSG_ROUTING_NONE, history_offset, history_length));
avi2b177592014-12-10 02:08:023660}
3661
3662void WebContentsImpl::SetHistoryOffsetAndLengthForView(
3663 RenderViewHost* render_view_host,
3664 int history_offset,
3665 int history_length) {
alexmos136fd6e62016-08-15 20:58:413666 render_view_host->Send(new PageMsg_SetHistoryOffsetAndLength(
avi2b177592014-12-10 02:08:023667 render_view_host->GetRoutingID(), history_offset, history_length));
[email protected]796931a92011-08-10 01:32:143668}
3669
arthursonzogni818c2642019-09-27 12:18:103670void WebContentsImpl::ReloadFocusedFrame() {
[email protected]1f3fc1d2014-04-03 14:50:173671 RenderFrameHost* focused_frame = GetFocusedFrame();
3672 if (!focused_frame)
3673 return;
3674
yilkal921048bd2019-10-09 23:51:043675 focused_frame->Reload();
[email protected]959be4c2014-04-08 15:01:333676}
3677
Miyoung Shin298d5ad2019-09-05 01:52:373678std::vector<mojo::Remote<blink::mojom::PauseSubresourceLoadingHandle>>
Ryan Sturmc65dbe42018-05-31 20:37:043679WebContentsImpl::PauseSubresourceLoading() {
Miyoung Shin298d5ad2019-09-05 01:52:373680 std::vector<mojo::Remote<blink::mojom::PauseSubresourceLoadingHandle>>
3681 handles;
Ryan Sturmc65dbe42018-05-31 20:37:043682 for (RenderFrameHost* rfh : GetAllFrames()) {
3683 if (!rfh->IsRenderFrameLive())
3684 continue;
3685 handles.push_back(rfh->PauseSubresourceLoading());
3686 }
3687 return handles;
3688}
3689
[email protected]959be4c2014-04-08 15:01:333690void WebContentsImpl::Undo() {
Dave Tapuskae782bea2019-07-09 16:21:513691 auto* input_handler = GetFocusedFrameInputHandler();
3692 if (!input_handler)
[email protected]959be4c2014-04-08 15:01:333693 return;
3694
Dave Tapuskae782bea2019-07-09 16:21:513695 input_handler->Undo();
[email protected]959be4c2014-04-08 15:01:333696 RecordAction(base::UserMetricsAction("Undo"));
3697}
3698
3699void WebContentsImpl::Redo() {
Dave Tapuskae782bea2019-07-09 16:21:513700 auto* input_handler = GetFocusedFrameInputHandler();
3701 if (!input_handler)
[email protected]959be4c2014-04-08 15:01:333702 return;
Dave Tapuskae782bea2019-07-09 16:21:513703
3704 input_handler->Redo();
[email protected]959be4c2014-04-08 15:01:333705 RecordAction(base::UserMetricsAction("Redo"));
3706}
3707
3708void WebContentsImpl::Cut() {
Dave Tapuskae782bea2019-07-09 16:21:513709 auto* input_handler = GetFocusedFrameInputHandler();
3710 if (!input_handler)
[email protected]959be4c2014-04-08 15:01:333711 return;
3712
Dave Tapuskae782bea2019-07-09 16:21:513713 input_handler->Cut();
[email protected]959be4c2014-04-08 15:01:333714 RecordAction(base::UserMetricsAction("Cut"));
3715}
3716
3717void WebContentsImpl::Copy() {
Dave Tapuskae782bea2019-07-09 16:21:513718 auto* input_handler = GetFocusedFrameInputHandler();
3719 if (!input_handler)
[email protected]959be4c2014-04-08 15:01:333720 return;
3721
Dave Tapuskae782bea2019-07-09 16:21:513722 input_handler->Copy();
[email protected]959be4c2014-04-08 15:01:333723 RecordAction(base::UserMetricsAction("Copy"));
3724}
3725
3726void WebContentsImpl::CopyToFindPboard() {
3727#if defined(OS_MACOSX)
Dave Tapuskae782bea2019-07-09 16:21:513728 auto* input_handler = GetFocusedFrameInputHandler();
3729 if (!input_handler)
[email protected]959be4c2014-04-08 15:01:333730 return;
3731
3732 // Windows/Linux don't have the concept of a find pasteboard.
Dave Tapuskae782bea2019-07-09 16:21:513733 input_handler->CopyToFindPboard();
[email protected]959be4c2014-04-08 15:01:333734 RecordAction(base::UserMetricsAction("CopyToFindPboard"));
3735#endif
3736}
3737
3738void WebContentsImpl::Paste() {
Dave Tapuskae782bea2019-07-09 16:21:513739 auto* input_handler = GetFocusedFrameInputHandler();
3740 if (!input_handler)
[email protected]959be4c2014-04-08 15:01:333741 return;
3742
Dave Tapuskae782bea2019-07-09 16:21:513743 input_handler->Paste();
Archana Simhaf67090c2019-05-06 23:43:233744 for (auto& observer : observers_)
3745 observer.OnPaste();
[email protected]959be4c2014-04-08 15:01:333746 RecordAction(base::UserMetricsAction("Paste"));
3747}
3748
3749void WebContentsImpl::PasteAndMatchStyle() {
Dave Tapuskae782bea2019-07-09 16:21:513750 auto* input_handler = GetFocusedFrameInputHandler();
3751 if (!input_handler)
[email protected]959be4c2014-04-08 15:01:333752 return;
3753
Dave Tapuskae782bea2019-07-09 16:21:513754 input_handler->PasteAndMatchStyle();
Archana Simhaf67090c2019-05-06 23:43:233755 for (auto& observer : observers_)
3756 observer.OnPaste();
[email protected]959be4c2014-04-08 15:01:333757 RecordAction(base::UserMetricsAction("PasteAndMatchStyle"));
3758}
3759
3760void WebContentsImpl::Delete() {
Dave Tapuskae782bea2019-07-09 16:21:513761 auto* input_handler = GetFocusedFrameInputHandler();
3762 if (!input_handler)
[email protected]959be4c2014-04-08 15:01:333763 return;
3764
Dave Tapuskae782bea2019-07-09 16:21:513765 input_handler->Delete();
[email protected]959be4c2014-04-08 15:01:333766 RecordAction(base::UserMetricsAction("DeleteSelection"));
3767}
3768
3769void WebContentsImpl::SelectAll() {
Dave Tapuskae782bea2019-07-09 16:21:513770 auto* input_handler = GetFocusedFrameInputHandler();
3771 if (!input_handler)
[email protected]959be4c2014-04-08 15:01:333772 return;
3773
Dave Tapuskae782bea2019-07-09 16:21:513774 input_handler->SelectAll();
[email protected]959be4c2014-04-08 15:01:333775 RecordAction(base::UserMetricsAction("SelectAll"));
3776}
3777
yabinh351e7ec2017-03-10 02:43:243778void WebContentsImpl::CollapseSelection() {
Dave Tapuskae782bea2019-07-09 16:21:513779 auto* input_handler = GetFocusedFrameInputHandler();
3780 if (!input_handler)
[email protected]959be4c2014-04-08 15:01:333781 return;
3782
Dave Tapuskae782bea2019-07-09 16:21:513783 input_handler->CollapseSelection();
[email protected]1f3fc1d2014-04-03 14:50:173784}
3785
3786void WebContentsImpl::Replace(const base::string16& word) {
Dave Tapuskae782bea2019-07-09 16:21:513787 auto* input_handler = GetFocusedFrameInputHandler();
3788 if (!input_handler)
[email protected]1f3fc1d2014-04-03 14:50:173789 return;
3790
Dave Tapuskae782bea2019-07-09 16:21:513791 input_handler->Replace(word);
[email protected]1f3fc1d2014-04-03 14:50:173792}
3793
3794void WebContentsImpl::ReplaceMisspelling(const base::string16& word) {
Dave Tapuskae782bea2019-07-09 16:21:513795 auto* input_handler = GetFocusedFrameInputHandler();
3796 if (!input_handler)
[email protected]1f3fc1d2014-04-03 14:50:173797 return;
3798
Dave Tapuskae782bea2019-07-09 16:21:513799 input_handler->ReplaceMisspelling(word);
[email protected]959be4c2014-04-08 15:01:333800}
3801
3802void WebContentsImpl::NotifyContextMenuClosed(
3803 const CustomContextMenuContext& context) {
3804 RenderFrameHost* focused_frame = GetFocusedFrame();
3805 if (!focused_frame)
3806 return;
3807
3808 focused_frame->Send(new FrameMsg_ContextMenuClosed(
3809 focused_frame->GetRoutingID(), context));
3810}
3811
3812void WebContentsImpl::ExecuteCustomContextMenuCommand(
3813 int action, const CustomContextMenuContext& context) {
3814 RenderFrameHost* focused_frame = GetFocusedFrame();
3815 if (!focused_frame)
3816 return;
3817
3818 focused_frame->Send(new FrameMsg_CustomContextMenuAction(
3819 focused_frame->GetRoutingID(), context, action));
[email protected]4fed3702014-04-01 09:08:003820}
3821
[email protected]fc2b46b2014-05-03 16:33:453822gfx::NativeView WebContentsImpl::GetNativeView() {
3823 return view_->GetNativeView();
3824}
3825
3826gfx::NativeView WebContentsImpl::GetContentNativeView() {
3827 return view_->GetContentNativeView();
3828}
3829
3830gfx::NativeWindow WebContentsImpl::GetTopLevelNativeWindow() {
3831 return view_->GetTopLevelNativeWindow();
3832}
3833
3834gfx::Rect WebContentsImpl::GetViewBounds() {
3835 return view_->GetViewBounds();
3836}
3837
3838gfx::Rect WebContentsImpl::GetContainerBounds() {
3839 gfx::Rect rv;
3840 view_->GetContainerBounds(&rv);
3841 return rv;
3842}
3843
3844DropData* WebContentsImpl::GetDropData() {
3845 return view_->GetDropData();
3846}
3847
3848void WebContentsImpl::Focus() {
miu9e14e492014-10-25 02:39:043849 view_->Focus();
[email protected]fc2b46b2014-05-03 16:33:453850}
3851
3852void WebContentsImpl::SetInitialFocus() {
miu9e14e492014-10-25 02:39:043853 view_->SetInitialFocus();
[email protected]fc2b46b2014-05-03 16:33:453854}
3855
3856void WebContentsImpl::StoreFocus() {
miu9e14e492014-10-25 02:39:043857 view_->StoreFocus();
[email protected]fc2b46b2014-05-03 16:33:453858}
3859
3860void WebContentsImpl::RestoreFocus() {
miu9e14e492014-10-25 02:39:043861 view_->RestoreFocus();
[email protected]fc2b46b2014-05-03 16:33:453862}
3863
[email protected]b172aee2012-04-10 17:05:263864void WebContentsImpl::FocusThroughTabTraversal(bool reverse) {
Maxim Podgorodskiya926c4ff2017-11-20 02:06:393865 view_->FocusThroughTabTraversal(reverse);
[email protected]96d185d2009-04-24 03:28:543866}
3867
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:413868bool WebContentsImpl::ShowingInterstitialPage() {
creisadca9862017-06-16 19:12:113869 return interstitial_page_ != nullptr;
[email protected]96d185d2009-04-24 03:28:543870}
3871
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:413872InterstitialPageImpl* WebContentsImpl::GetInterstitialPage() {
creisadca9862017-06-16 19:12:113873 return interstitial_page_;
[email protected]686493142011-07-15 21:47:223874}
3875
[email protected]b172aee2012-04-10 17:05:263876bool WebContentsImpl::IsSavable() {
[email protected]a53209b2012-01-20 16:48:163877 // WebKit creates Document object when MIME type is application/xhtml+xml,
3878 // so we also support this MIME type.
3879 return contents_mime_type_ == "text/html" ||
3880 contents_mime_type_ == "text/xml" ||
3881 contents_mime_type_ == "application/xhtml+xml" ||
3882 contents_mime_type_ == "text/plain" ||
3883 contents_mime_type_ == "text/css" ||
Kinuko Yasuda74702f92017-07-31 03:27:533884 blink::IsSupportedJavascriptMimeType(contents_mime_type_);
[email protected]a53209b2012-01-20 16:48:163885}
3886
[email protected]b172aee2012-04-10 17:05:263887void WebContentsImpl::OnSavePage() {
[email protected]c7dd2f62011-07-18 15:57:593888 // If we can not save the page, try to download it.
[email protected]a53209b2012-01-20 16:48:163889 if (!IsSavable()) {
Min Qinda0ed2062018-02-23 22:00:533890 download::RecordSavePackageEvent(
3891 download::SAVE_PACKAGE_DOWNLOAD_ON_NON_HTML);
nasko89ad7742015-03-05 22:14:193892 SaveFrame(GetLastCommittedURL(), Referrer());
[email protected]27678b2a2012-02-04 22:09:143893 return;
[email protected]c7dd2f62011-07-18 15:57:593894 }
3895
3896 Stop();
3897
3898 // Create the save package and possibly prompt the user for the name to save
3899 // the page as. The user prompt is an asynchronous operation that runs on
3900 // another thread.
3901 save_package_ = new SavePackage(this);
3902 save_package_->GetSaveInfo();
3903}
3904
3905// Used in automated testing to bypass prompting the user for file names.
3906// Instead, the names and paths are hard coded rather than running them through
3907// file name sanitation and extension / mime checking.
[email protected]2dec8ec2013-02-07 19:20:343908bool WebContentsImpl::SavePage(const base::FilePath& main_file,
3909 const base::FilePath& dir_path,
[email protected]8ff00d72012-10-23 19:12:213910 SavePageType save_type) {
[email protected]c7dd2f62011-07-18 15:57:593911 // Stop the page from navigating.
3912 Stop();
3913
3914 save_package_ = new SavePackage(this, save_type, main_file, dir_path);
[email protected]8ff00d72012-10-23 19:12:213915 return save_package_->Init(SavePackageDownloadCreatedCallback());
[email protected]c7dd2f62011-07-18 15:57:593916}
3917
[email protected]3c71576ce2013-07-23 02:00:013918void WebContentsImpl::SaveFrame(const GURL& url,
3919 const Referrer& referrer) {
Xing Liu10329bf2018-03-20 19:22:143920 SaveFrameWithHeaders(url, referrer, std::string(), base::string16());
kundaji6c7f9692015-03-09 18:00:373921}
3922
Xing Liu10329bf2018-03-20 19:22:143923void WebContentsImpl::SaveFrameWithHeaders(
3924 const GURL& url,
3925 const Referrer& referrer,
3926 const std::string& headers,
3927 const base::string16& suggested_filename) {
Lei Zhang13a42e42019-02-19 23:28:113928 // Check and see if the guest can handle this.
3929 if (delegate_) {
3930 WebContents* guest_web_contents = nullptr;
3931 if (browser_plugin_embedder_) {
3932 BrowserPluginGuest* guest = browser_plugin_embedder_->GetFullPageGuest();
3933 if (guest)
3934 guest_web_contents = guest->GetWebContents();
3935 } else if (browser_plugin_guest_) {
3936 guest_web_contents = this;
3937 }
3938
3939 if (guest_web_contents && delegate_->GuestSaveFrame(guest_web_contents))
3940 return;
3941 }
3942
nasko89ad7742015-03-05 22:14:193943 if (!GetLastCommittedURL().is_valid())
[email protected]3c71576ce2013-07-23 02:00:013944 return;
sammc92af61552014-11-19 23:27:403945 if (delegate_ && delegate_->SaveFrame(url, referrer))
3946 return;
3947
nasko89ad7742015-03-05 22:14:193948 // TODO(nasko): This check for main frame is incorrect and should be fixed
brettw760f7442016-05-23 21:19:223949 // by explicitly passing in which frame this method should target. This would
3950 // indicate whether it's the main frame, and also tell us the frame pointer
3951 // to use for routing.
nasko89ad7742015-03-05 22:14:193952 bool is_main_frame = (url == GetLastCommittedURL());
brettw760f7442016-05-23 21:19:223953 RenderFrameHost* frame_host = GetMainFrame();
[email protected]3c71576ce2013-07-23 02:00:013954
avib7348942015-12-25 20:57:103955 int64_t post_id = -1;
[email protected]3c71576ce2013-07-23 02:00:013956 if (is_main_frame) {
Lucas Furukawa Gadani5553a152019-01-08 18:55:573957 NavigationEntry* entry = controller_.GetLastCommittedEntry();
[email protected]3c71576ce2013-07-23 02:00:013958 if (entry)
3959 post_id = entry->GetPostID();
3960 }
Ramin Halavati24e21012017-07-10 12:56:063961 net::NetworkTrafficAnnotationTag traffic_annotation =
3962 net::DefineNetworkTrafficAnnotation("download_web_contents_frame", R"(
3963 semantics {
3964 sender: "Save Page Action"
3965 description:
3966 "Saves the given frame's URL to the local file system."
3967 trigger:
3968 "The user has triggered a save operation on the frame through a "
3969 "context menu or other mechanism."
3970 data: "None."
3971 destination: WEBSITE
3972 }
3973 policy {
Ramin Halavati3b979782017-07-21 11:40:263974 cookies_allowed: YES
Ramin Halavati24e21012017-07-10 12:56:063975 cookies_store: "user"
3976 setting:
3977 "This feature cannot be disabled by settings, but it's is only "
3978 "triggered by user request."
3979 policy_exception_justification: "Not implemented."
3980 })");
Min Qina904f3302018-02-13 23:33:343981 auto params = std::make_unique<download::DownloadUrlParameters>(
brettw760f7442016-05-23 21:19:223982 url, frame_host->GetProcess()->GetID(),
3983 frame_host->GetRenderViewHost()->GetRoutingID(),
Matt Menke6d93c3c2018-10-10 15:21:503984 frame_host->GetRoutingID(), traffic_annotation);
Min Qina904f3302018-02-13 23:33:343985 params->set_referrer(referrer.url);
3986 params->set_referrer_policy(
3987 Referrer::ReferrerPolicyForUrlRequest(referrer.policy));
[email protected]3c71576ce2013-07-23 02:00:013988 params->set_post_id(post_id);
[email protected]3c71576ce2013-07-23 02:00:013989 if (post_id >= 0)
3990 params->set_method("POST");
3991 params->set_prompt(true);
kundaji6c7f9692015-03-09 18:00:373992
3993 if (headers.empty()) {
3994 params->set_prefer_cache(true);
3995 } else {
Min Qina904f3302018-02-13 23:33:343996 for (download::DownloadUrlParameters::RequestHeadersNameValuePair
3997 key_value : ParseDownloadHeaders(headers)) {
Megan Jablonski2f6a4c52017-07-10 23:01:253998 params->add_request_header(key_value.first, key_value.second);
kundaji6c7f9692015-03-09 18:00:373999 }
4000 }
Xing Liu10329bf2018-03-20 19:22:144001 params->set_suggested_name(suggested_filename);
Min Qin0ca8e1ee2018-01-31 00:49:354002 params->set_download_source(download::DownloadSource::WEB_CONTENTS_API);
thestig859c78892017-05-15 21:17:064003 BrowserContext::GetDownloadManager(GetBrowserContext())
Ramin Halavati03efa732017-06-12 09:28:204004 ->DownloadUrl(std::move(params));
[email protected]3c71576ce2013-07-23 02:00:014005}
4006
[email protected]b172aee2012-04-10 17:05:264007void WebContentsImpl::GenerateMHTML(
dewittj6dc5747a2016-05-17 01:48:474008 const MHTMLGenerationParams& params,
Avi Drissman149b7832018-03-23 14:31:494009 base::OnceCallback<void(int64_t)> callback) {
Angel Alvarez902496682019-08-27 22:58:424010 base::OnceCallback<void(const MHTMLGenerationResult&)> wrapper_callback =
4011 base::BindOnce(
4012 [](base::OnceCallback<void(int64_t)> size_callback,
4013 const MHTMLGenerationResult& result) {
4014 std::move(size_callback).Run(result.file_size);
4015 },
4016 std::move(callback));
4017 MHTMLGenerationManager::GetInstance()->SaveMHTML(this, params,
4018 std::move(wrapper_callback));
4019}
4020
4021void WebContentsImpl::GenerateMHTMLWithResult(
4022 const MHTMLGenerationParams& params,
4023 MHTMLGenerationResult::GenerateMHTMLCallback callback) {
Avi Drissman149b7832018-03-23 14:31:494024 MHTMLGenerationManager::GetInstance()->SaveMHTML(this, params,
4025 std::move(callback));
[email protected]aa4f3972012-03-01 18:12:124026}
4027
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:414028const std::string& WebContentsImpl::GetContentsMimeType() {
[email protected]0bfbf882011-12-22 18:19:274029 return contents_mime_type_;
4030}
4031
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:414032bool WebContentsImpl::WillNotifyDisconnection() {
[email protected]0bfbf882011-12-22 18:19:274033 return notify_disconnection_;
4034}
4035
Leon Hanc819dc62019-01-28 04:30:194036blink::mojom::RendererPreferences* WebContentsImpl::GetMutableRendererPrefs() {
[email protected]0bfbf882011-12-22 18:19:274037 return &renderer_preferences_;
4038}
4039
[email protected]e35ccd52012-05-23 16:22:474040void WebContentsImpl::Close() {
4041 Close(GetRenderViewHost());
4042}
4043
Ella Ge80a52dce2017-11-15 18:01:524044void WebContentsImpl::DragSourceEndedAt(float client_x,
4045 float client_y,
4046 float screen_x,
4047 float screen_y,
Paul Meyer0c58c3712016-11-17 22:59:514048 blink::WebDragOperation operation,
4049 RenderWidgetHost* source_rwh) {
Lei Zhang6410ec1a2019-01-31 21:38:584050 if (browser_plugin_embedder_) {
paulmeyer8fc8ea92016-11-15 05:12:214051 browser_plugin_embedder_->DragSourceEndedAt(
4052 client_x, client_y, screen_x, screen_y, operation);
Lei Zhang6410ec1a2019-01-31 21:38:584053 }
Paul Meyer0c58c3712016-11-17 22:59:514054 if (source_rwh) {
Ella Ge80a52dce2017-11-15 18:01:524055 source_rwh->DragSourceEndedAt(gfx::PointF(client_x, client_y),
4056 gfx::PointF(screen_x, screen_y), operation);
Paul Meyer0c58c3712016-11-17 22:59:514057 }
[email protected]cf200a562013-05-03 16:24:294058}
4059
jam73f892642016-09-11 06:04:064060void WebContentsImpl::LoadStateChanged(
Charles Harrison911fa062018-03-06 21:01:464061 const std::string& host,
jam73f892642016-09-11 06:04:064062 const net::LoadStateWithParam& load_state,
4063 uint64_t upload_position,
4064 uint64_t upload_size) {
Charles Harrison911fa062018-03-06 21:01:464065 base::string16 host16 = url_formatter::IDNToUnicode(host);
Elly Fong-Jonescad9c3d12018-01-31 17:26:104066 // Drop no-op updates.
4067 if (load_state_.state == load_state.state &&
4068 load_state_.param == load_state.param &&
4069 upload_position_ == upload_position && upload_size_ == upload_size &&
Charles Harrison911fa062018-03-06 21:01:464070 load_state_host_ == host16) {
Elly Fong-Jonescad9c3d12018-01-31 17:26:104071 return;
4072 }
jam73f892642016-09-11 06:04:064073 load_state_ = load_state;
4074 upload_position_ = upload_position;
4075 upload_size_ = upload_size;
Charles Harrison911fa062018-03-06 21:01:464076 load_state_host_ = host16;
jam73f892642016-09-11 06:04:064077 if (load_state_.state == net::LOAD_STATE_READING_RESPONSE)
4078 SetNotWaitingForResponse();
4079 if (IsLoading()) {
4080 NotifyNavigationStateChanged(static_cast<InvalidateTypes>(
4081 INVALIDATE_TYPE_LOAD | INVALIDATE_TYPE_TAB));
4082 }
4083}
4084
Collin Baker989e0882019-11-01 01:27:174085void WebContentsImpl::SetVisibilityAndNotifyObservers(Visibility visibility) {
Francois Doraye6161152018-03-27 22:05:374086 const Visibility previous_visibility = visibility_;
4087 visibility_ = visibility;
4088
Francois Dorayfe4a1772018-02-17 04:17:094089 // Notify observers if the visibility changed or if WasShown() is being called
4090 // for the first time.
Francois Dorayfe4a1772018-02-17 04:17:094091 if (visibility != previous_visibility ||
4092 (visibility == Visibility::VISIBLE && !did_first_set_visible_)) {
4093 for (auto& observer : observers_)
4094 observer.OnVisibilityChanged(visibility);
4095 }
4096}
4097
Michael Thiessen896405db2017-07-20 17:52:324098void WebContentsImpl::NotifyWebContentsFocused(
4099 RenderWidgetHost* render_widget_host) {
ericwilligersde386342016-10-19 02:35:094100 for (auto& observer : observers_)
Michael Thiessen896405db2017-07-20 17:52:324101 observer.OnWebContentsFocused(render_widget_host);
calamity7fe55ce2015-04-10 03:59:374102}
4103
Michael Thiessen896405db2017-07-20 17:52:324104void WebContentsImpl::NotifyWebContentsLostFocus(
4105 RenderWidgetHost* render_widget_host) {
zijiehe3bee08e22017-05-17 04:14:464106 for (auto& observer : observers_)
Michael Thiessen896405db2017-07-20 17:52:324107 observer.OnWebContentsLostFocus(render_widget_host);
zijiehe3bee08e22017-05-17 04:14:464108}
4109
Paul Meyer0c58c3712016-11-17 22:59:514110void WebContentsImpl::SystemDragEnded(RenderWidgetHost* source_rwh) {
4111 if (source_rwh)
4112 source_rwh->DragSourceSystemDragEnded();
Lei Zhang6410ec1a2019-01-31 21:38:584113 if (browser_plugin_embedder_)
[email protected]cf200a562013-05-03 16:24:294114 browser_plugin_embedder_->SystemDragEnded();
[email protected]7813bd72011-02-05 02:19:344115}
4116
[email protected]b172aee2012-04-10 17:05:264117void WebContentsImpl::SetClosedByUserGesture(bool value) {
[email protected]0bfbf882011-12-22 18:19:274118 closed_by_user_gesture_ = value;
4119}
4120
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:414121bool WebContentsImpl::GetClosedByUserGesture() {
[email protected]0bfbf882011-12-22 18:19:274122 return closed_by_user_gesture_;
4123}
4124
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:414125int WebContentsImpl::GetMinimumZoomPercent() {
[email protected]0bfbf882011-12-22 18:19:274126 return minimum_zoom_percent_;
4127}
4128
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:414129int WebContentsImpl::GetMaximumZoomPercent() {
[email protected]0bfbf882011-12-22 18:19:274130 return maximum_zoom_percent_;
4131}
4132
mcnee432e47d2015-11-09 19:37:464133void WebContentsImpl::SetPageScale(float page_scale_factor) {
Lukasz Anforowiczf4357ca2017-09-07 01:43:324134 GetRenderViewHost()->Send(new ViewMsg_SetPageScale(
4135 GetRenderViewHost()->GetRoutingID(), page_scale_factor));
ccameronb7c1d6c2015-03-09 17:08:244136}
4137
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:414138gfx::Size WebContentsImpl::GetPreferredSize() {
Francois Doray24fc62c2017-12-11 17:27:334139 return IsBeingCaptured() ? preferred_size_for_capture_ : preferred_size_;
[email protected]bcd2815602012-01-14 18:17:234140}
4141
[email protected]b172aee2012-04-10 17:05:264142bool WebContentsImpl::GotResponseToLockMouseRequest(bool allowed) {
lfgf0cd46e2017-01-04 00:05:234143 if (!GuestMode::IsCrossProcessFrameGuest(GetWebContents()) &&
4144 GetBrowserPluginGuest())
[email protected]660f18e2014-05-15 20:53:054145 return GetBrowserPluginGuest()->LockMouse(allowed);
4146
lfgf0cd46e2017-01-04 00:05:234147 if (mouse_lock_widget_) {
4148 if (mouse_lock_widget_->delegate()->GetAsWebContents() != this) {
4149 return mouse_lock_widget_->delegate()
4150 ->GetAsWebContents()
4151 ->GotResponseToLockMouseRequest(allowed);
4152 }
lfgad8244352016-07-13 16:55:514153
lfgf0cd46e2017-01-04 00:05:234154 if (mouse_lock_widget_->GotResponseToLockMouseRequest(allowed))
4155 return true;
4156 }
4157
4158 for (WebContentsImpl* current = this; current;
4159 current = current->GetOuterWebContents()) {
4160 current->mouse_lock_widget_ = nullptr;
4161 }
4162
lfgbee1e0a2016-06-08 21:24:214163 return false;
[email protected]0bfbf882011-12-22 18:19:274164}
4165
Joe Downing13dd76b2018-04-09 18:32:154166bool WebContentsImpl::GotResponseToKeyboardLockRequest(bool allowed) {
4167 if (!keyboard_lock_widget_)
4168 return false;
4169
4170 if (keyboard_lock_widget_->delegate()->GetAsWebContents() != this) {
4171 NOTREACHED();
4172 return false;
4173 }
4174
4175 // KeyboardLock is only supported when called by the top-level browsing
4176 // context and is not supported in embedded content scenarios.
4177 if (GetOuterWebContents())
4178 return false;
4179
4180 keyboard_lock_widget_->GotResponseToKeyboardLockRequest(allowed);
4181 return true;
4182}
4183
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:414184bool WebContentsImpl::HasOpener() {
Ivan Kotenkov2c0d2bb32017-11-01 15:41:284185 return GetOpener() != nullptr;
[email protected]a0358d72012-03-09 14:06:504186}
4187
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:414188RenderFrameHostImpl* WebContentsImpl::GetOpener() {
alexmose201c7cd2015-06-10 17:14:214189 FrameTreeNode* opener_ftn = frame_tree_.root()->opener();
lukasza6f8ac622017-06-06 03:10:204190 return opener_ftn ? opener_ftn->current_frame_host() : nullptr;
jochen55ff3502014-12-18 20:52:574191}
4192
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:414193bool WebContentsImpl::HasOriginalOpener() {
Ivan Kotenkov2c0d2bb32017-11-01 15:41:284194 return GetOriginalOpener() != nullptr;
jochen6004a362017-02-04 00:11:404195}
4196
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:414197RenderFrameHostImpl* WebContentsImpl::GetOriginalOpener() {
jochen6004a362017-02-04 00:11:404198 FrameTreeNode* opener_ftn = frame_tree_.root()->original_opener();
lukasza6f8ac622017-06-06 03:10:204199 return opener_ftn ? opener_ftn->current_frame_host() : nullptr;
jochen6004a362017-02-04 00:11:404200}
4201
[email protected]cb805452013-05-22 15:16:214202void WebContentsImpl::DidChooseColorInColorChooser(SkColor color) {
Joel Hockey85b379d2017-12-11 10:42:134203 color_chooser_->DidChooseColorInColorChooser(color);
[email protected]da8543762012-03-20 08:52:204204}
4205
[email protected]cb805452013-05-22 15:16:214206void WebContentsImpl::DidEndColorChooser() {
Joel Hockey85b379d2017-12-11 10:42:134207 color_chooser_.reset();
[email protected]da8543762012-03-20 08:52:204208}
4209
halton.huoca2eabd2015-07-06 08:17:404210int WebContentsImpl::DownloadImage(
4211 const GURL& url,
4212 bool is_favicon,
4213 uint32_t max_bitmap_size,
4214 bool bypass_cache,
Avi Drissmana5a52dd2018-03-27 03:39:024215 WebContents::ImageDownloadCallback callback) {
amistry9f01b772015-07-29 01:54:554216 DCHECK_CURRENTLY_ON(BrowserThread::UI);
halton.huoca2eabd2015-07-06 08:17:404217 static int next_image_download_id = 0;
Julie Jeongeun Kimb1473572019-08-15 05:59:134218 const mojo::Remote<blink::mojom::ImageDownloader>& mojo_image_downloader =
halton.huoca2eabd2015-07-06 08:17:404219 GetMainFrame()->GetMojoImageDownloader();
amistry9f01b772015-07-29 01:54:554220 const int download_id = ++next_image_download_id;
4221 if (!mojo_image_downloader) {
4222 // If the renderer process is dead (i.e. crash, or memory pressure on
4223 // Android), the downloader service will be invalid. Pre-Mojo, this would
Avi Drissmana5a52dd2018-03-27 03:39:024224 // hang the callback indefinitely since the IPC would be dropped. Now,
amistry9f01b772015-07-29 01:54:554225 // respond with a 400 HTTP error code to indicate that something went wrong.
Sami Kyostila8e4d5a92019-08-02 12:45:054226 base::PostTask(
Eric Seckler8652dcd52018-09-20 10:42:284227 FROM_HERE, {BrowserThread::UI},
tzike2aca992017-09-05 08:50:544228 base::BindOnce(&WebContentsImpl::OnDidDownloadImage,
Avi Drissmana5a52dd2018-03-27 03:39:024229 weak_factory_.GetWeakPtr(), std::move(callback),
4230 download_id, url, 400, std::vector<SkBitmap>(),
4231 std::vector<gfx::Size>()));
amistry9f01b772015-07-29 01:54:554232 return download_id;
4233 }
4234
halton.huoca2eabd2015-07-06 08:17:404235 mojo_image_downloader->DownloadImage(
sammcf6f4fcf2016-06-21 05:39:394236 url, is_favicon, max_bitmap_size, bypass_cache,
tzike2aca992017-09-05 08:50:544237 base::BindOnce(&WebContentsImpl::OnDidDownloadImage,
Avi Drissmana5a52dd2018-03-27 03:39:024238 weak_factory_.GetWeakPtr(), std::move(callback),
4239 download_id, url));
amistry9f01b772015-07-29 01:54:554240 return download_id;
[email protected]795c28972012-12-06 06:13:394241}
4242
[email protected]36ec24f2014-01-09 00:32:084243void WebContentsImpl::Find(int request_id,
4244 const base::string16& search_text,
Rakina Zata Amni3f77dff2018-09-08 16:19:434245 blink::mojom::FindOptionsPtr options) {
thestig6057a6b22015-11-12 23:01:334246 // Cowardly refuse to search for no text.
4247 if (search_text.empty()) {
4248 NOTREACHED();
4249 return;
4250 }
4251
Rakina Zata Amni3f77dff2018-09-08 16:19:434252 GetOrCreateFindRequestManager()->Find(request_id, search_text,
4253 std::move(options));
[email protected]36ec24f2014-01-09 00:32:084254}
4255
4256void WebContentsImpl::StopFinding(StopFindAction action) {
paulmeyerfeafc2d2017-04-25 21:46:404257 if (FindRequestManager* manager = GetFindRequestManager())
4258 manager->StopFinding(action);
[email protected]36ec24f2014-01-09 00:32:084259}
4260
Tommy Steimel18360512017-11-01 00:38:194261bool WebContentsImpl::WasEverAudible() {
4262 return was_ever_audible_;
4263}
4264
Avi Drissmana5a52dd2018-03-27 03:39:024265void WebContentsImpl::GetManifest(GetManifestCallback callback) {
4266 manifest_manager_host_->GetManifest(std::move(callback));
mlamouriefdca9d2014-09-16 16:55:404267}
4268
bokanece34a82016-01-28 19:49:464269void WebContentsImpl::ExitFullscreen(bool will_cause_resize) {
mlamouri7a78d6fd2015-01-17 13:23:534270 // Clean up related state and initiate the fullscreen exit.
avi3627ecac2015-10-16 17:40:434271 GetRenderViewHost()->GetWidget()->RejectMouseLockOrUnlockIfNecessary();
bokanece34a82016-01-28 19:49:464272 ExitFullscreenMode(will_cause_resize);
mlamouri7a78d6fd2015-01-17 13:23:534273}
4274
Avi Drissmanced52b62019-08-14 21:25:464275void WebContentsImpl::ForSecurityDropFullscreen() {
4276 // There are two chains of WebContents to kick out of fullscreen.
4277 //
4278 // Chain 1, the inner/outer WebContents chain. If an inner WebContents has
4279 // done something that requires the browser to drop fullscreen, drop
4280 // fullscreen from it and any outer WebContents that may be in fullscreen.
4281 //
4282 // Chain 2, the opener WebContents chain. If a WebContents has done something
4283 // that requires the browser to drop fullscreen, drop fullscreen from any
4284 // WebContents that was involved in the chain of opening it.
4285 //
4286 // Note that these two chains don't interact, as only a top-level WebContents
4287 // can have an opener. This simplifies things.
4288
4289 WebContents* web_contents = this;
4290 while (web_contents) {
4291 if (web_contents->IsFullscreenForCurrentTab())
4292 web_contents->ExitFullscreen(true);
4293
4294 if (web_contents->HasOriginalOpener())
4295 web_contents = FromRenderFrameHost(web_contents->GetOriginalOpener());
4296 else
4297 web_contents = web_contents->GetOuterWebContents();
4298 }
4299}
4300
mariakhomenko44bdc4732015-04-29 01:55:384301void WebContentsImpl::ResumeLoadingCreatedWebContents() {
Nasko Oskov83a8cf92018-10-19 14:58:564302 if (delayed_load_url_params_.get()) {
4303 DCHECK(!delayed_open_url_params_);
4304 controller_.LoadURLWithParams(*delayed_load_url_params_.get());
4305 delayed_load_url_params_.reset(nullptr);
4306 return;
4307 }
4308
mariakhomenkoa4971c12015-07-21 19:04:374309 if (delayed_open_url_params_.get()) {
4310 OpenURL(*delayed_open_url_params_.get());
4311 delayed_open_url_params_.reset(nullptr);
4312 return;
4313 }
4314
mariakhomenko44bdc4732015-04-29 01:55:384315 // Resume blocked requests for both the RenderViewHost and RenderFrameHost.
4316 // TODO(brettw): It seems bogus to reach into here and initialize the host.
dfalcantarae6b7b46752015-07-10 18:14:164317 if (is_resume_pending_) {
4318 is_resume_pending_ = false;
avi0f1bbc9712015-11-17 02:58:134319 GetRenderViewHost()->GetWidget()->Init();
dfalcantarae6b7b46752015-07-10 18:14:164320 GetMainFrame()->Init();
4321 }
mariakhomenko44bdc4732015-04-29 01:55:384322}
4323
[email protected]b172aee2012-04-10 17:05:264324bool WebContentsImpl::FocusLocationBarByDefault() {
Xiyuan Xia2a66b112018-12-06 15:52:164325 if (should_focus_location_bar_by_default_)
[email protected]0c9406632013-02-08 01:13:334326 return true;
Xiyuan Xia2a66b112018-12-06 15:52:164327
[email protected]0c9406632013-02-08 01:13:334328 return delegate_ && delegate_->ShouldFocusLocationBarByDefault(this);
[email protected]0bfbf882011-12-22 18:19:274329}
4330
Tommy C. Li8bb5bf42019-01-18 18:45:514331void WebContentsImpl::SetFocusToLocationBar() {
[email protected]0bfbf882011-12-22 18:19:274332 if (delegate_)
Tommy C. Li8bb5bf42019-01-18 18:45:514333 delegate_->SetFocusToLocationBar();
[email protected]c40d6232011-03-25 00:16:214334}
4335
clamy0e119882015-07-31 16:12:334336void WebContentsImpl::DidStartNavigation(NavigationHandle* navigation_handle) {
Charles Harrison53096ba2017-12-15 15:39:484337 TRACE_EVENT1("navigation", "WebContentsImpl::DidStartNavigation",
4338 "navigation_handle", navigation_handle);
ericwilligersde386342016-10-19 02:35:094339 for (auto& observer : observers_)
4340 observer.DidStartNavigation(navigation_handle);
Becca Hughesd11d6502018-07-31 17:01:284341
4342 if (display_cutout_host_impl_)
4343 display_cutout_host_impl_->DidStartNavigation(navigation_handle);
Xiyuan Xia2a66b112018-12-06 15:52:164344
4345 if (navigation_handle->IsInMainFrame()) {
4346 // When the browser is started with about:blank as the startup URL, focus
4347 // the location bar (which will also select its contents) so people can
4348 // simply begin typing to navigate elsewhere.
4349 //
4350 // We need to be careful not to trigger this for anything other than the
4351 // startup navigation. In particular, if we allow an attacker to open a
4352 // popup to about:blank, then navigate, focusing the Omnibox will cause the
4353 // end of the new URL to be scrolled into view instead of the start,
4354 // allowing the attacker to spoof other URLs. The conditions checked here
4355 // are all aimed at ensuring no such attacker-controlled navigation can
4356 // trigger this.
4357 should_focus_location_bar_by_default_ =
4358 controller_.IsInitialNavigation() &&
4359 !navigation_handle->IsRendererInitiated() &&
4360 navigation_handle->GetURL() == url::kAboutBlankURL;
4361 }
clamy0e119882015-07-31 16:12:334362}
4363
4364void WebContentsImpl::DidRedirectNavigation(
4365 NavigationHandle* navigation_handle) {
Charles Harrison53096ba2017-12-15 15:39:484366 TRACE_EVENT1("navigation", "WebContentsImpl::DidRedirectNavigation",
4367 "navigation_handle", navigation_handle);
ericwilligersde386342016-10-19 02:35:094368 for (auto& observer : observers_)
4369 observer.DidRedirectNavigation(navigation_handle);
jam5f358e52017-02-13 16:37:034370
4371 // Notify accessibility if this is a reload. This has to called on the
4372 // BrowserAccessibilityManager associated with the old RFHI.
4373 if (navigation_handle->GetReloadType() != ReloadType::NONE) {
Mohamed Abdelhalim95794962019-09-20 11:16:494374 NavigationRequest* request = NavigationRequest::From(navigation_handle);
jam5f358e52017-02-13 16:37:034375 BrowserAccessibilityManager* manager =
Mohamed Abdelhalim95794962019-09-20 11:16:494376 request->frame_tree_node()
jam5f358e52017-02-13 16:37:034377 ->current_frame_host()
4378 ->browser_accessibility_manager();
4379 if (manager)
4380 manager->UserIsReloading();
4381 }
clamy0e119882015-07-31 16:12:334382}
4383
clamyefca29e2015-09-17 00:22:114384void WebContentsImpl::ReadyToCommitNavigation(
4385 NavigationHandle* navigation_handle) {
Charles Harrison53096ba2017-12-15 15:39:484386 TRACE_EVENT1("navigation", "WebContentsImpl::ReadyToCommitNavigation",
4387 "navigation_handle", navigation_handle);
yilkal796e89e2019-10-02 23:58:284388
4389 if (!navigation_handle->GetParentFrame() &&
4390 record_max_frame_count_when_leaving_current_page_) {
4391 RecordMaxFrameCountUMA(max_frame_count_);
4392 record_max_frame_count_when_leaving_current_page_ = false;
4393 }
4394
ericwilligersde386342016-10-19 02:35:094395 for (auto& observer : observers_)
4396 observer.ReadyToCommitNavigation(navigation_handle);
John Abd-El-Malekd4882642017-12-04 21:45:194397
Kenneth Russell954ed9ce2018-04-12 23:07:014398 // If any domains are blocked from accessing 3D APIs because they may
4399 // have caused the GPU to reset recently, unblock them here if the user
4400 // initiated this navigation. This implies that the user was involved in
4401 // the decision to navigate, so there's no concern about
4402 // denial-of-service issues. Want to do this as early as
4403 // possible to avoid race conditions with pages attempting to access
4404 // WebGL early on.
4405 //
4406 // TODO(crbug.com/617904): currently navigations initiated by the browser
4407 // (reload button, reload menu option, pressing return in the Omnibox)
4408 // return false from HasUserGesture(). If or when that is addressed,
4409 // remove the check for IsRendererInitiated() below.
4410 //
4411 // TODO(crbug.com/832180): HasUserGesture comes from the renderer
4412 // process and isn't validated. Until it is, don't trust it.
4413 if (!navigation_handle->IsRendererInitiated()) {
4414 GpuDataManagerImpl::GetInstance()->UnblockDomainFrom3DAPIs(
4415 navigation_handle->GetURL());
4416 }
4417
John Abd-El-Malekfde08e6da2017-12-05 04:06:244418 if (navigation_handle->IsSameDocument())
4419 return;
4420
Livvie Lin139bf442018-08-10 00:18:544421 // SSLInfo is not needed on subframe navigations since the main-frame
4422 // certificate is the only one that can be inspected (using the info
4423 // bubble) without refreshing the page with DevTools open.
Carlos ILaed1e7b2019-03-12 19:27:214424 // We don't call DidStartResourceResponse on net errors, since that results on
4425 // existing cert exceptions being revoked, which leads to weird behavior with
4426 // committed interstitials or while offline. We only need the error check for
4427 // the main frame case because unlike this method, SubresourceResponseStarted
4428 // does not get called on network errors.
4429 if (navigation_handle->IsInMainFrame() &&
4430 navigation_handle->GetNetErrorCode() == net::OK) {
Livvie Lin139bf442018-08-10 00:18:544431 controller_.ssl_manager()->DidStartResourceResponse(
4432 navigation_handle->GetURL(),
Camille Lamy62b826012019-02-26 09:15:474433 navigation_handle->GetSSLInfo().has_value()
4434 ? net::IsCertStatusError(
4435 navigation_handle->GetSSLInfo()->cert_status)
4436 : false);
4437 }
John Abd-El-Malekfde08e6da2017-12-05 04:06:244438
4439 SetNotWaitingForResponse();
clamyefca29e2015-09-17 00:22:114440}
4441
clamy0e119882015-07-31 16:12:334442void WebContentsImpl::DidFinishNavigation(NavigationHandle* navigation_handle) {
Charles Harrison53096ba2017-12-15 15:39:484443 TRACE_EVENT1("navigation", "WebContentsImpl::DidFinishNavigation",
4444 "navigation_handle", navigation_handle);
Steven Holte38041342018-06-22 22:00:574445
ericwilligersde386342016-10-19 02:35:094446 for (auto& observer : observers_)
4447 observer.DidFinishNavigation(navigation_handle);
clamy0e119882015-07-31 16:12:334448
Becca Hughesd11d6502018-07-31 17:01:284449 if (display_cutout_host_impl_)
4450 display_cutout_host_impl_->DidFinishNavigation(navigation_handle);
4451
jam5f358e52017-02-13 16:37:034452 if (navigation_handle->HasCommitted()) {
Dominic Farolinof87ec2a2019-07-04 09:18:094453 // TODO(domfarolino, dmazzoni): Do this using WebContentsObserver. See
4454 // https://crbug.com/981271.
dmazzonibf8cec42015-02-08 08:28:084455 BrowserAccessibilityManager* manager =
jam5f358e52017-02-13 16:37:034456 static_cast<RenderFrameHostImpl*>(
4457 navigation_handle->GetRenderFrameHost())
4458 ->browser_accessibility_manager();
4459 if (manager) {
4460 if (navigation_handle->IsErrorPage()) {
4461 manager->NavigationFailed();
4462 } else {
4463 manager->NavigationSucceeded();
4464 }
4465 }
Tommy Steimel18360512017-11-01 00:38:194466
4467 if (navigation_handle->IsInMainFrame() &&
4468 !navigation_handle->IsSameDocument()) {
4469 was_ever_audible_ = false;
Steven Holte38041342018-06-22 22:00:574470 last_committed_source_id_ =
4471 ukm::ConvertToSourceId(navigation_handle->GetNavigationId(),
4472 ukm::SourceIdType::NAVIGATION_ID);
Tommy Steimel18360512017-11-01 00:38:194473 }
dmazzonibf8cec42015-02-08 08:28:084474 }
Xiyuan Xia2a66b112018-12-06 15:52:164475
4476 // If we didn't end up on about:blank after setting this in DidStartNavigation
4477 // then don't focus the location bar.
4478 if (should_focus_location_bar_by_default_ &&
4479 navigation_handle->GetURL() != url::kAboutBlankURL) {
4480 should_focus_location_bar_by_default_ = false;
4481 }
yilkal796e89e2019-10-02 23:58:284482
4483 // If navigation has successfully finished in the main page, set
4484 // |record_max_frame_count_when_leaving_current_page_| to true so that when
4485 // the main frame navigates away from this page we know to record this page's
4486 // max frame count.
4487 if (navigation_handle->IsInMainFrame() && !navigation_handle->IsErrorPage()) {
4488 record_max_frame_count_when_leaving_current_page_ = true;
4489
4490 // Navigation has completed in main frame. Reset |max_frame_count_| to 1.
4491 max_frame_count_ = 1;
4492 }
[email protected]400992b2012-06-14 00:03:544493}
4494
[email protected]b80624c2014-02-09 02:46:554495void WebContentsImpl::DidFailLoadWithError(
4496 RenderFrameHostImpl* render_frame_host,
[email protected]b80624c2014-02-09 02:46:554497 const GURL& url,
[email protected]b80624c2014-02-09 02:46:554498 int error_code,
Yutaka Hirano16d3c5d2017-07-31 06:17:304499 const base::string16& error_description) {
ericwilligersde386342016-10-19 02:35:094500 for (auto& observer : observers_) {
Yutaka Hirano16d3c5d2017-07-31 06:17:304501 observer.DidFailLoad(render_frame_host, url, error_code, error_description);
ericwilligersde386342016-10-19 02:35:094502 }
[email protected]b80624c2014-02-09 02:46:554503}
4504
[email protected]52913802013-12-10 05:52:184505void WebContentsImpl::NotifyChangedNavigationState(
4506 InvalidateTypes changed_flags) {
4507 NotifyNavigationStateChanged(changed_flags);
4508}
4509
toyoshim0df1d3a2016-09-09 09:52:484510void WebContentsImpl::DidStartNavigationToPendingEntry(const GURL& url,
4511 ReloadType reload_type) {
[email protected]3691e5cf2014-01-22 10:16:204512 // Notify observers about navigation.
ericwilligersde386342016-10-19 02:35:094513 for (auto& observer : observers_)
4514 observer.DidStartNavigationToPendingEntry(url, reload_type);
[email protected]3691e5cf2014-01-22 10:16:204515}
4516
lukasza1d025732016-09-28 21:36:084517bool WebContentsImpl::ShouldTransferNavigation(bool is_main_frame_navigation) {
creis29460272015-12-16 04:38:224518 if (!delegate_)
4519 return true;
lukasza1d025732016-09-28 21:36:084520 return delegate_->ShouldTransferNavigation(is_main_frame_navigation);
creis29460272015-12-16 04:38:224521}
4522
[email protected]aa62afd2014-04-22 19:22:464523bool WebContentsImpl::ShouldPreserveAbortedURLs() {
4524 if (!delegate_)
4525 return false;
4526 return delegate_->ShouldPreserveAbortedURLs(this);
4527}
4528
[email protected]0d0f4c492014-04-02 06:42:574529void WebContentsImpl::DidNavigateMainFramePreCommit(
[email protected]5cfbddc2014-06-23 23:52:234530 bool navigation_is_within_page) {
[email protected]0d0f4c492014-04-02 06:42:574531 // Ensure fullscreen mode is exited before committing the navigation to a
4532 // different page. The next page will not start out assuming it is in
4533 // fullscreen mode.
[email protected]5cfbddc2014-06-23 23:52:234534 if (navigation_is_within_page) {
[email protected]dfc39cb2014-04-09 22:58:194535 // No page change? Then, the renderer and browser can remain in fullscreen.
[email protected]0d0f4c492014-04-02 06:42:574536 return;
4537 }
alexmos5dd617d2016-06-07 04:21:154538 if (IsFullscreenForCurrentTab())
bokanece34a82016-01-28 19:49:464539 ExitFullscreen(false);
alexmos5dd617d2016-06-07 04:21:154540 DCHECK(!IsFullscreenForCurrentTab());
Joe Downingc1a57efe2018-05-09 18:23:544541
4542 // Clean up keyboard lock state when navigating.
4543 CancelKeyboardLock(keyboard_lock_widget_);
[email protected]0d0f4c492014-04-02 06:42:574544}
4545
[email protected]37567b432014-02-12 01:12:224546void WebContentsImpl::DidNavigateMainFramePostCommit(
sky8b00392d2015-01-10 00:30:284547 RenderFrameHostImpl* render_frame_host,
[email protected]37567b432014-02-12 01:12:224548 const LoadCommittedDetails& details,
4549 const FrameHostMsg_DidCommitProvisionalLoad_Params& params) {
4550 if (details.is_navigation_to_different_page()) {
4551 // Clear the status bubble. This is a workaround for a bug where WebKit
4552 // doesn't let us know that the cursor left an element during a
4553 // transition (this is also why the mouse cursor remains as a hand after
4554 // clicking on a link); see bugs 1184641 and 980803. We don't want to
4555 // clear the bubble when a user navigates to a named anchor in the same
4556 // page.
Lukasz Anforowiczfd707142018-02-07 19:46:134557 ClearTargetURL();
lanwei9d343ad22015-02-11 01:46:004558
4559 RenderWidgetHostViewBase* rwhvb =
4560 static_cast<RenderWidgetHostViewBase*>(GetRenderWidgetHostView());
4561 if (rwhvb)
4562 rwhvb->OnDidNavigateMainFrameToNewPage();
[email protected]37567b432014-02-12 01:12:224563 }
4564
[email protected]e85165c642014-06-10 14:34:314565 if (delegate_)
[email protected]37567b432014-02-12 01:12:224566 delegate_->DidNavigateMainFramePostCommit(this);
[email protected]e85165c642014-06-10 14:34:314567 view_->SetOverscrollControllerEnabled(CanOverscrollContent());
Shivani Sharma985474f2019-05-23 21:36:474568
4569 if (!details.is_same_document && GetInnerWebContents().empty())
4570 had_inner_webcontents_ = false;
Carlos Caballero91fffb22019-10-29 15:24:304571
4572 if (details.is_navigation_to_different_page() &&
4573 GetRenderViewHost()->did_first_visually_non_empty_paint()) {
4574 // This event will not fire again if the page is restored from the
4575 // BackForwardCache. So fire it ourselves if needed.
4576 DidFirstVisuallyNonEmptyPaint(GetRenderViewHost());
4577 }
Carlos Caballero1215f882019-10-29 15:58:434578
4579 if (GetRenderViewHost()->theme_color() != last_sent_theme_color_) {
4580 // This event will not fire again if the page is restored from the
4581 // BackForwardCache. So fire it ourselves if needed.
4582 OnThemeColorChanged(GetRenderViewHost());
4583 }
[email protected]37567b432014-02-12 01:12:224584}
4585
4586void WebContentsImpl::DidNavigateAnyFramePostCommit(
4587 RenderFrameHostImpl* render_frame_host,
4588 const LoadCommittedDetails& details,
4589 const FrameHostMsg_DidCommitProvisionalLoad_Params& params) {
[email protected]b4c84012014-04-28 19:51:104590 // Now that something has committed, we don't need to track whether the
4591 // initial page has been accessed.
4592 has_accessed_initial_document_ = false;
4593
[email protected]37567b432014-02-12 01:12:224594 // If we navigate off the page, close all JavaScript dialogs.
eugenebutee08663a2017-04-27 17:43:124595 if (!details.is_same_document)
creis89a0f782015-05-27 16:13:174596 CancelActiveAndPendingDialogs();
[email protected]37567b432014-02-12 01:12:224597
avid53461d2016-02-25 17:15:044598 // If this is a user-initiated navigation, start allowing JavaScript dialogs
4599 // again.
avi5d3b8692016-10-12 22:00:464600 if (params.gesture == NavigationGestureUser && dialog_manager_) {
avi6879fcf2017-01-21 05:27:534601 dialog_manager_->CancelDialogs(this, /*reset_state=*/true);
avi5d3b8692016-10-12 22:00:464602 }
[email protected]37567b432014-02-12 01:12:224603}
4604
4605void WebContentsImpl::SetMainFrameMimeType(const std::string& mime_type) {
4606 contents_mime_type_ = mime_type;
4607}
4608
[email protected]277857a2014-06-03 10:38:014609bool WebContentsImpl::CanOverscrollContent() const {
[email protected]e85165c642014-06-10 14:34:314610 // Disable overscroll when touch emulation is on. See crbug.com/369938.
[email protected]34ff1cfc2014-08-20 06:16:054611 if (force_disable_overscroll_content_)
[email protected]e85165c642014-06-10 14:34:314612 return false;
4613
[email protected]37567b432014-02-12 01:12:224614 if (delegate_)
4615 return delegate_->CanOverscrollContent();
4616
4617 return false;
4618}
4619
Carlos Caballero1215f882019-10-29 15:58:434620void WebContentsImpl::OnThemeColorChanged(RenderViewHostImpl* source) {
4621 if (source->did_first_visually_non_empty_paint() &&
4622 last_sent_theme_color_ != source->theme_color()) {
ericwilligersde386342016-10-19 02:35:094623 for (auto& observer : observers_)
Carlos Caballero1215f882019-10-29 15:58:434624 observer.DidChangeThemeColor(source->theme_color());
4625 last_sent_theme_color_ = source->theme_color();
peter90afaba2015-06-01 12:05:294626 }
[email protected]e7104762014-06-20 19:17:254627}
4628
[email protected]b172aee2012-04-10 17:05:264629void WebContentsImpl::OnDidLoadResourceFromMemoryCache(
nicka0ac8382016-12-15 23:59:234630 RenderFrameHostImpl* source,
[email protected]724159a2010-12-30 01:11:184631 const GURL& url,
[email protected]70435962011-08-02 20:13:284632 const std::string& http_method,
[email protected]6d6cfb3a2012-05-23 22:53:184633 const std::string& mime_type,
[email protected]6c1e05212014-07-31 00:59:404634 ResourceType resource_type) {
ericwilligersde386342016-10-19 02:35:094635 for (auto& observer : observers_)
4636 observer.DidLoadResourceFromMemoryCache(url, mime_type, resource_type);
[email protected]b0f724c2013-09-05 04:21:134637
Steven Bingler674fb0822018-12-12 18:04:524638 if (url.is_valid() && url.SchemeIsHTTPOrHTTPS()) {
nicka0ac8382016-12-15 23:59:234639 StoragePartition* partition = source->GetProcess()->GetStoragePartition();
John Abd-El-Malekf4fe9d82019-07-31 04:05:594640 partition->GetNetworkContext()->NotifyExternalCacheHit(
Alex Turner48442b542019-08-21 14:01:144641 url, http_method, source->network_isolation_key());
[email protected]8bfc8272013-09-09 20:10:534642 }
[email protected]724159a2010-12-30 01:11:184643}
4644
carloskd9d97942017-02-16 08:58:094645void WebContentsImpl::DidDisplayInsecureContent() {
estarkc2273502016-10-12 22:03:024646 controller_.ssl_manager()->DidDisplayMixedContent();
[email protected]724159a2010-12-30 01:11:184647}
4648
Dave Tapuskaa657d872019-10-21 20:23:214649void WebContentsImpl::DidContainInsecureFormAction() {
elawrenceb2ac2a232017-03-27 21:46:254650 controller_.ssl_manager()->DidContainInsecureFormAction();
4651}
4652
nicka0ac8382016-12-15 23:59:234653void WebContentsImpl::OnDidRunInsecureContent(RenderFrameHostImpl* source,
4654 const GURL& security_origin,
estark910b4572015-12-09 20:55:414655 const GURL& target_url) {
nicka0ac8382016-12-15 23:59:234656 // TODO(nick, estark): Should we call FilterURL using |source|'s process on
4657 // these parameters? |target_url| seems unused, except for a log message. And
4658 // |security_origin| might be replaceable with the origin of the main frame.
carloskd9d97942017-02-16 08:58:094659 DidRunInsecureContent(security_origin, target_url);
4660}
4661
4662void WebContentsImpl::DidRunInsecureContent(const GURL& security_origin,
4663 const GURL& target_url) {
[email protected]9450c462013-11-23 01:22:584664 LOG(WARNING) << security_origin << " ran insecure content from "
4665 << target_url.possibly_invalid_spec();
[email protected]e6e30ac2014-01-13 21:24:394666 RecordAction(base::UserMetricsAction("SSL.RanInsecureContent"));
estark910b4572015-12-09 20:55:414667 if (base::EndsWith(security_origin.spec(), kDotGoogleDotCom,
brettwa7ff1b292015-07-16 17:49:294668 base::CompareCase::INSENSITIVE_ASCII))
[email protected]e6e30ac2014-01-13 21:24:394669 RecordAction(base::UserMetricsAction("SSL.RanInsecureContentGoogle"));
estarkc2273502016-10-12 22:03:024670 controller_.ssl_manager()->DidRunMixedContent(security_origin);
[email protected]724159a2010-12-30 01:11:184671}
4672
carloskd9d97942017-02-16 08:58:094673void WebContentsImpl::PassiveInsecureContentFound(const GURL& resource_url) {
Sergey Kuznetsov3ebc01b2018-02-01 04:57:364674 if (delegate_) {
4675 delegate_->PassiveInsecureContentFound(resource_url);
4676 }
carloskd9d97942017-02-16 08:58:094677}
4678
4679bool WebContentsImpl::ShouldAllowRunningInsecureContent(
4680 WebContents* web_contents,
4681 bool allowed_per_prefs,
4682 const url::Origin& origin,
4683 const GURL& resource_url) {
Sergey Kuznetsov3ebc01b2018-02-01 04:57:364684 if (delegate_) {
4685 return delegate_->ShouldAllowRunningInsecureContent(
4686 web_contents, allowed_per_prefs, origin, resource_url);
4687 }
4688
4689 return allowed_per_prefs;
carloskd9d97942017-02-16 08:58:094690}
4691
Lukasz Anforowicze1b954d92017-10-30 21:28:064692void WebContentsImpl::ViewSource(RenderFrameHostImpl* frame) {
4693 DCHECK_EQ(this, WebContents::FromRenderFrameHost(frame));
4694
4695 // Don't do anything if there is no |delegate_| that could accept and show the
4696 // new WebContents containing the view-source.
4697 if (!delegate_)
4698 return;
4699
4700 // Use the last committed entry, since the pending entry hasn't loaded yet and
4701 // won't be copied into the cloned tab.
4702 NavigationEntryImpl* last_committed_entry =
4703 static_cast<NavigationEntryImpl*>(frame->frame_tree_node()
4704 ->navigator()
4705 ->GetController()
4706 ->GetLastCommittedEntry());
4707 if (!last_committed_entry)
4708 return;
4709
4710 FrameNavigationEntry* frame_entry =
4711 last_committed_entry->GetFrameEntry(frame->frame_tree_node());
4712 if (!frame_entry)
4713 return;
4714
4715 // Any new WebContents opened while this WebContents is in fullscreen can be
4716 // used to confuse the user, so drop fullscreen.
Avi Drissmand18c51972018-08-15 18:32:094717 ForSecurityDropFullscreen();
Lukasz Anforowicze1b954d92017-10-30 21:28:064718
4719 // We intentionally don't share the SiteInstance with the original frame so
4720 // that view source has a consistent process model and always ends up in a new
4721 // process (https://crbug.com/699493).
4722 scoped_refptr<SiteInstanceImpl> site_instance_for_view_source = nullptr;
Lukasz Anforowicz435bcb582019-07-12 20:50:064723 // Referrer and initiator are not important, because view-source should not
4724 // hit the network, but should be served from the cache instead.
Lukasz Anforowicze1b954d92017-10-30 21:28:064725 Referrer referrer_for_view_source;
Lukasz Anforowicz435bcb582019-07-12 20:50:064726 base::Optional<url::Origin> initiator_for_view_source = base::nullopt;
Lukasz Anforowicze1b954d92017-10-30 21:28:064727 // Do not restore title, derive it from the url.
4728 base::string16 title_for_view_source;
4729 auto navigation_entry = std::make_unique<NavigationEntryImpl>(
4730 site_instance_for_view_source, frame_entry->url(),
Lukasz Anforowicz435bcb582019-07-12 20:50:064731 referrer_for_view_source, initiator_for_view_source,
4732 title_for_view_source, ui::PAGE_TRANSITION_LINK,
Marijn Kruisselbrink7a0d5e182018-05-24 22:55:094733 /* is_renderer_initiated = */ false,
4734 /* blob_url_loader_factory = */ nullptr);
Lukasz Anforowicze1b954d92017-10-30 21:28:064735 navigation_entry->SetVirtualURL(GURL(content::kViewSourceScheme +
4736 std::string(":") +
4737 frame_entry->url().spec()));
4738
Yao Xiaof68f57d2019-10-03 04:20:054739 navigation_entry->set_network_isolation_key(
4740 net::NetworkIsolationKey(GetMainFrame()->GetLastCommittedOrigin(),
4741 frame->GetLastCommittedOrigin()));
4742
Lukasz Anforowicze1b954d92017-10-30 21:28:064743 // Do not restore scroller position.
4744 // TODO(creis, lukasza, arthursonzogni): Do not reuse the original PageState,
4745 // but start from a new one and only copy the needed data.
4746 const PageState& new_page_state =
4747 frame_entry->page_state().RemoveScrollOffset();
4748
4749 scoped_refptr<FrameNavigationEntry> new_frame_entry =
4750 navigation_entry->root_node()->frame_entry;
4751 new_frame_entry->set_method(frame_entry->method());
4752 new_frame_entry->SetPageState(new_page_state);
4753
4754 // Create a new WebContents, which is used to display the source code.
erikchenbee5c9622018-04-27 19:30:254755 std::unique_ptr<WebContents> view_source_contents =
Erik Chenbb8e738e2018-04-28 14:10:434756 Create(CreateParams(GetBrowserContext()));
Lukasz Anforowicze1b954d92017-10-30 21:28:064757
4758 // Restore the previously created NavigationEntry.
4759 std::vector<std::unique_ptr<NavigationEntry>> navigation_entries;
4760 navigation_entries.push_back(std::move(navigation_entry));
4761 view_source_contents->GetController().Restore(0, RestoreType::CURRENT_SESSION,
4762 &navigation_entries);
4763
4764 // Add |view_source_contents| as a new tab.
4765 gfx::Rect initial_rect;
4766 constexpr bool kUserGesture = true;
4767 bool ignored_was_blocked;
erikchenbee5c9622018-04-27 19:30:254768 delegate_->AddNewContents(this, std::move(view_source_contents),
Lukasz Anforowicze1b954d92017-10-30 21:28:064769 WindowOpenDisposition::NEW_FOREGROUND_TAB,
4770 initial_rect, kUserGesture, &ignored_was_blocked);
4771 // Note that the |delegate_| could have deleted |view_source_contents| during
4772 // AddNewContents method call.
4773}
4774
Jay Civellid5904f02018-03-23 19:43:574775void WebContentsImpl::SubresourceResponseStarted(const GURL& url,
4776 net::CertStatus cert_status) {
4777 controller_.ssl_manager()->DidStartResourceResponse(url, cert_status);
4778 SetNotWaitingForResponse();
4779}
4780
Jay Civelli116683f2018-03-27 19:56:234781void WebContentsImpl::ResourceLoadComplete(
Jay Civelli52a63302018-04-30 22:39:554782 RenderFrameHost* render_frame_host,
Clark DuVall36164bd2018-08-09 22:49:084783 const GlobalRequestID& request_id,
Jay Civelli116683f2018-03-27 19:56:234784 mojom::ResourceLoadInfoPtr resource_load_info) {
John Abd-El-Malek9b8f7fa2017-12-04 23:52:254785 for (auto& observer : observers_) {
Clark DuVall36164bd2018-08-09 22:49:084786 observer.ResourceLoadComplete(render_frame_host, request_id,
4787 *resource_load_info);
John Abd-El-Malek9b8f7fa2017-12-04 23:52:254788 }
John Abd-El-Malekd4882642017-12-04 21:45:194789}
4790
Wei Li5bb659742018-02-14 03:07:584791void WebContentsImpl::PrintCrossProcessSubframe(
4792 const gfx::Rect& rect,
4793 int document_cookie,
4794 RenderFrameHost* subframe_host) {
Wei Li5c0b4d12018-03-22 15:05:224795 auto* outer_contents = GetOuterWebContents();
4796 if (outer_contents) {
4797 // When an extension or app page is printed, the content should be
4798 // composited with outer content, so the outer contents should handle the
4799 // print request.
4800 outer_contents->PrintCrossProcessSubframe(rect, document_cookie,
4801 subframe_host);
4802 return;
4803 }
4804
Wei Li5bb659742018-02-14 03:07:584805 // If there is no delegate such as in tests or during deletion, do nothing.
4806 if (!delegate_)
4807 return;
4808
4809 delegate_->PrintCrossProcessSubframe(this, rect, document_cookie,
4810 subframe_host);
4811}
4812
rob.buis300b0872017-03-10 20:43:584813#if defined(OS_ANDROID)
4814base::android::ScopedJavaLocalRef<jobject>
4815WebContentsImpl::GetJavaRenderFrameHostDelegate() {
4816 return GetJavaWebContents();
4817}
4818#endif
4819
estark910b4572015-12-09 20:55:414820void WebContentsImpl::OnDidDisplayContentWithCertificateErrors(
Zhuoyu Qian4353f7f2018-01-08 05:33:004821 RenderFrameHostImpl* source) {
estarkc2273502016-10-12 22:03:024822 controller_.ssl_manager()->DidDisplayContentWithCertErrors();
estark910b4572015-12-09 20:55:414823}
4824
4825void WebContentsImpl::OnDidRunContentWithCertificateErrors(
Zhuoyu Qian4353f7f2018-01-08 05:33:004826 RenderFrameHostImpl* source) {
nicka0ac8382016-12-15 23:59:234827 // TODO(nick, estark): Do we need to consider |source| here somehow?
estarkcd2e30c2016-08-12 06:51:154828 NavigationEntry* entry = controller_.GetVisibleEntry();
4829 if (!entry)
estarkaeda1422016-05-10 02:31:124830 return;
4831
estarkaeda1422016-05-10 02:31:124832 // TODO(estark): check that this does something reasonable for
4833 // about:blank and sandboxed origins. https://crbug.com/609527
estarkcd2e30c2016-08-12 06:51:154834 controller_.ssl_manager()->DidRunContentWithCertErrors(
4835 entry->GetURL().GetOrigin());
estark910b4572015-12-09 20:55:414836}
4837
Kouhei Ueno97e0990d2019-09-25 02:57:274838void WebContentsImpl::DOMContentLoaded(RenderFrameHost* render_frame_host) {
ericwilligersde386342016-10-19 02:35:094839 for (auto& observer : observers_)
Kouhei Uenof6e96242019-09-27 01:54:354840 observer.DOMContentLoaded(render_frame_host);
[email protected]724159a2010-12-30 01:11:184841}
4842
nicka0ac8382016-12-15 23:59:234843void WebContentsImpl::OnDidFinishLoad(RenderFrameHostImpl* source,
4844 const GURL& url) {
[email protected]8b3af1e2014-01-24 13:29:124845 GURL validated_url(url);
nicka0ac8382016-12-15 23:59:234846 source->GetProcess()->FilterURL(false, &validated_url);
[email protected]028053d42014-03-05 22:20:374847
yilkal34a3d752019-08-30 18:20:304848 if (!source->GetParent()) {
4849 size_t frame_count = source->frame_tree_node()->GetFrameTreeSize();
4850 UMA_HISTOGRAM_COUNTS_1000("Navigation.MainFrame.FrameCount", frame_count);
4851 }
4852
ericwilligersde386342016-10-19 02:35:094853 for (auto& observer : observers_)
nicka0ac8382016-12-15 23:59:234854 observer.DidFinishLoad(source, validated_url);
[email protected]1a55c5be2011-11-29 11:36:314855}
4856
Dave Tapuska549baab2019-03-19 15:52:404857void WebContentsImpl::OnGoToEntryAtOffset(RenderFrameHostImpl* source,
Arthur Sonzognia7d715a2018-09-20 16:11:134858 int offset,
Dave Tapuska043b94a2019-09-10 14:14:354859 bool has_user_gesture) {
Arthur Sonzognia7d715a2018-09-20 16:11:134860 // Non-user initiated navigations coming from the renderer should be ignored
4861 // if there is an ongoing browser-initiated navigation.
4862 // See https://crbug.com/879965.
4863 // TODO(arthursonzogni): See if this should check for ongoing navigations in
4864 // the frame(s) affected by the session history navigation, rather than just
4865 // the main frame.
4866 if (!has_user_gesture) {
4867 NavigationRequest* ongoing_navigation_request =
4868 frame_tree_.root()->navigation_request();
4869 if (ongoing_navigation_request &&
4870 ongoing_navigation_request->browser_initiated()) {
4871 return;
4872 }
4873 }
4874
nicka0ac8382016-12-15 23:59:234875 // All frames are allowed to navigate the global history.
Dave Tapuska8bfd84c2019-03-26 20:47:164876 if (!delegate_ || delegate_->OnGoToEntryOffset(offset)) {
Dave Tapuska043b94a2019-09-10 14:14:354877 if (source->IsSandboxed(blink::WebSandboxFlags::kTopNavigation)) {
Dave Tapuska8bfd84c2019-03-26 20:47:164878 // Keep track of whether this is a session history from a sandboxed iframe
4879 // with top level navigation disallowed.
4880 controller_.GoToOffsetInSandboxedFrame(offset,
4881 source->GetFrameTreeNodeId());
4882 } else {
4883 controller_.GoToOffset(offset);
4884 }
4885 }
[email protected]216813952011-05-19 22:21:264886}
4887
nicka0ac8382016-12-15 23:59:234888void WebContentsImpl::OnPageScaleFactorChanged(RenderViewHostImpl* source,
4889 float page_scale_factor) {
mcnee336ea2c2017-05-23 22:50:594890#if !defined(OS_ANDROID)
4891 // While page scale factor is used on mobile, this PageScaleFactorIsOne logic
4892 // is only needed on desktop.
mcnee432e47d2015-11-09 19:37:464893 bool is_one = page_scale_factor == 1.f;
4894 if (is_one != page_scale_factor_is_one_) {
4895 page_scale_factor_is_one_ = is_one;
4896
4897 HostZoomMapImpl* host_zoom_map =
4898 static_cast<HostZoomMapImpl*>(HostZoomMap::GetForWebContents(this));
4899
nicka0ac8382016-12-15 23:59:234900 if (host_zoom_map) {
mcnee432e47d2015-11-09 19:37:464901 host_zoom_map->SetPageScaleFactorIsOneForView(
nicka0ac8382016-12-15 23:59:234902 source->GetProcess()->GetID(), source->GetRoutingID(),
mcnee432e47d2015-11-09 19:37:464903 page_scale_factor_is_one_);
4904 }
4905 }
mcnee336ea2c2017-05-23 22:50:594906#endif // !defined(OS_ANDROID)
mcnee432e47d2015-11-09 19:37:464907
ericwilligersde386342016-10-19 02:35:094908 for (auto& observer : observers_)
4909 observer.OnPageScaleFactorChanged(page_scale_factor);
mcnee432e47d2015-11-09 19:37:464910}
4911
W. James MacLean62198672019-06-04 16:46:104912void WebContentsImpl::OnTextAutosizerPageInfoChanged(
4913 RenderViewHostImpl* source,
4914 const blink::WebTextAutosizerPageInfo& page_info) {
4915 // Keep a copy of |page_info| in case we create a new RenderView before
4916 // the next update.
4917 text_autosizer_page_info_ = page_info;
4918 frame_tree_.root()->render_manager()->SendPageMessage(
4919 new PageMsg_UpdateTextAutosizerPageInfoForRemoteMainFrames(
4920 MSG_ROUTING_NONE, page_info),
4921 source->GetSiteInstance());
4922}
4923
Kent Tamura8c9e0562018-11-06 07:02:194924void WebContentsImpl::EnumerateDirectory(
4925 RenderFrameHost* render_frame_host,
4926 std::unique_ptr<FileSelectListener> listener,
4927 const base::FilePath& directory_path) {
4928 if (delegate_)
4929 delegate_->EnumerateDirectory(this, std::move(listener), directory_path);
4930 else
4931 listener->FileSelectionCanceled();
[email protected]3a29a6e2011-08-24 18:26:214932}
4933
Dave Tapuskaf9fc3122019-10-18 19:42:554934void WebContentsImpl::RegisterProtocolHandler(RenderFrameHostImpl* source,
4935 const std::string& protocol,
4936 const GURL& url,
4937 const base::string16& title,
4938 bool user_gesture) {
nicka0ac8382016-12-15 23:59:234939 // TODO(nick): Should we consider |source| here or pass it to the delegate?
4940 // TODO(nick): Do we need to apply FilterURL to |url|?
[email protected]e5f2de02012-07-20 22:15:434941 if (!delegate_)
4942 return;
4943
Raymes Khoury58373df2019-08-06 06:29:204944 if (!AreValidRegisterProtocolHandlerArguments(
4945 protocol, url, source->GetLastCommittedOrigin())) {
4946 ReceivedBadMessage(source->GetProcess(),
4947 bad_message::REGISTER_PROTOCOL_HANDLER_INVALID_URL);
[email protected]b9535422012-02-09 01:47:594948 return;
Raymes Khoury58373df2019-08-06 06:29:204949 }
[email protected]8f810632013-06-06 22:33:324950
[email protected]c125e532014-05-10 20:39:064951 delegate_->RegisterProtocolHandler(this, protocol, url, user_gesture);
[email protected]7d189022011-08-25 22:54:204952}
4953
Dave Tapuskaf9fc3122019-10-18 19:42:554954void WebContentsImpl::UnregisterProtocolHandler(RenderFrameHostImpl* source,
4955 const std::string& protocol,
4956 const GURL& url,
4957 bool user_gesture) {
nicka0ac8382016-12-15 23:59:234958 // TODO(nick): Should we consider |source| here or pass it to the delegate?
4959 // TODO(nick): Do we need to apply FilterURL to |url|?
[email protected]f5273e52014-07-14 16:30:204960 if (!delegate_)
4961 return;
4962
Raymes Khoury58373df2019-08-06 06:29:204963 if (!AreValidRegisterProtocolHandlerArguments(
4964 protocol, url, source->GetLastCommittedOrigin())) {
4965 ReceivedBadMessage(source->GetProcess(),
4966 bad_message::REGISTER_PROTOCOL_HANDLER_INVALID_URL);
[email protected]f5273e52014-07-14 16:30:204967 return;
Raymes Khoury58373df2019-08-06 06:29:204968 }
[email protected]f5273e52014-07-14 16:30:204969
4970 delegate_->UnregisterProtocolHandler(this, protocol, url, user_gesture);
4971}
4972
kouhei40f03cb2015-09-24 07:47:014973void WebContentsImpl::OnUpdatePageImportanceSignals(
nicka0ac8382016-12-15 23:59:234974 RenderFrameHostImpl* source,
kouhei40f03cb2015-09-24 07:47:014975 const PageImportanceSignals& signals) {
nicka0ac8382016-12-15 23:59:234976 // TODO(nick, kouhei): Fix this for oopifs; currently all frames' state gets
4977 // written to this one field.
kouhei40f03cb2015-09-24 07:47:014978 page_importance_signals_ = signals;
4979}
4980
nicka0ac8382016-12-15 23:59:234981void WebContentsImpl::OnDomOperationResponse(RenderFrameHostImpl* source,
4982 const std::string& json_string) {
4983 // TODO(nick, lukasza): The notification below should probably be updated to
4984 // include |source|.
avi60bd4902015-09-23 20:39:244985 std::string json = json_string;
4986 NotificationService::current()->Notify(NOTIFICATION_DOM_OPERATION_RESPONSE,
4987 Source<WebContents>(this),
4988 Details<std::string>(&json));
[email protected]cfa856d62014-02-22 07:58:404989}
4990
Marijn Kruisselbrink7f519672019-02-05 19:12:504991void WebContentsImpl::OnAppCacheAccessed(const GURL& manifest_url,
[email protected]b172aee2012-04-10 17:05:264992 bool blocked_by_policy) {
nicka0ac8382016-12-15 23:59:234993 // TODO(nick): Should we consider |source| here? Should we call FilterURL on
4994 // |manifest_url|?
4995
[email protected]7fc4bbb2011-09-08 21:23:104996 // Notify observers about navigation.
ericwilligersde386342016-10-19 02:35:094997 for (auto& observer : observers_)
4998 observer.AppCacheAccessed(manifest_url, blocked_by_policy);
[email protected]7fc4bbb2011-09-08 21:23:104999}
5000
Joel Hockey85b379d2017-12-11 10:42:135001void WebContentsImpl::OnColorChooserFactoryRequest(
Julie Jeongeun Kim8e2879e2019-08-06 23:55:205002 mojo::PendingReceiver<blink::mojom::ColorChooserFactory> receiver) {
5003 color_chooser_factory_receivers_.Add(this, std::move(receiver));
Joel Hockey85b379d2017-12-11 10:42:135004}
5005
5006void WebContentsImpl::OpenColorChooser(
Julie Jeongeun Kim8e2879e2019-08-06 23:55:205007 mojo::PendingReceiver<blink::mojom::ColorChooser> chooser_receiver,
5008 mojo::PendingRemote<blink::mojom::ColorChooserClient> client,
[email protected]8ed16472014-04-11 19:02:485009 SkColor color,
Joel Hockey163835a2017-12-20 11:51:575010 std::vector<blink::mojom::ColorSuggestionPtr> suggestions) {
Joel Hockey85b379d2017-12-11 10:42:135011 content::ColorChooser* new_color_chooser =
Ivan Kotenkov2c0d2bb32017-11-01 15:41:285012 delegate_ ? delegate_->OpenColorChooser(this, color, suggestions)
5013 : nullptr;
[email protected]820957a2014-01-14 14:56:305014 if (!new_color_chooser)
[email protected]cb805452013-05-22 15:16:215015 return;
[email protected]8ed16472014-04-11 19:02:485016
Joel Hockey85b379d2017-12-11 10:42:135017 color_chooser_.reset();
5018 color_chooser_ = std::make_unique<ColorChooser>(
Julie Jeongeun Kim8e2879e2019-08-06 23:55:205019 new_color_chooser, std::move(chooser_receiver), std::move(client));
[email protected]da8543762012-03-20 08:52:205020}
5021
brettw4b461082016-11-19 18:55:165022#if BUILDFLAG(ENABLE_PLUGINS)
nicka0ac8382016-12-15 23:59:235023void WebContentsImpl::OnPepperInstanceCreated(RenderFrameHostImpl* source,
5024 int32_t pp_instance) {
ericwilligersde386342016-10-19 02:35:095025 for (auto& observer : observers_)
5026 observer.PepperInstanceCreated();
nicka0ac8382016-12-15 23:59:235027 pepper_playback_observer_->PepperInstanceCreated(source, pp_instance);
emaxxe70f5e12015-05-29 11:26:005028}
5029
nicka0ac8382016-12-15 23:59:235030void WebContentsImpl::OnPepperInstanceDeleted(RenderFrameHostImpl* source,
5031 int32_t pp_instance) {
ericwilligersde386342016-10-19 02:35:095032 for (auto& observer : observers_)
5033 observer.PepperInstanceDeleted();
nicka0ac8382016-12-15 23:59:235034 pepper_playback_observer_->PepperInstanceDeleted(source, pp_instance);
emaxxe70f5e12015-05-29 11:26:005035}
5036
nicka0ac8382016-12-15 23:59:235037void WebContentsImpl::OnPepperPluginHung(RenderFrameHostImpl* source,
5038 int plugin_child_id,
tommyclie6633ca72014-10-31 00:40:425039 const base::FilePath& path,
5040 bool is_hung) {
Steven Holte95922222018-09-14 20:06:235041 UMA_HISTOGRAM_COUNTS_1M("Pepper.PluginHung", 1);
tommyclie6633ca72014-10-31 00:40:425042
ericwilligersde386342016-10-19 02:35:095043 for (auto& observer : observers_)
5044 observer.PluginHungStatusChanged(plugin_child_id, path, is_hung);
tommyclie6633ca72014-10-31 00:40:425045}
5046
nicka0ac8382016-12-15 23:59:235047void WebContentsImpl::OnPepperStartsPlayback(RenderFrameHostImpl* source,
5048 int32_t pp_instance) {
5049 pepper_playback_observer_->PepperStartsPlayback(source, pp_instance);
zqzhang181047e62016-07-01 13:37:175050}
5051
nicka0ac8382016-12-15 23:59:235052void WebContentsImpl::OnPepperStopsPlayback(RenderFrameHostImpl* source,
5053 int32_t pp_instance) {
5054 pepper_playback_observer_->PepperStopsPlayback(source, pp_instance);
zqzhang181047e62016-07-01 13:37:175055}
5056
nicka0ac8382016-12-15 23:59:235057void WebContentsImpl::OnPluginCrashed(RenderFrameHostImpl* source,
5058 const base::FilePath& plugin_path,
tommyclie6633ca72014-10-31 00:40:425059 base::ProcessId plugin_pid) {
nicka0ac8382016-12-15 23:59:235060 // TODO(nick): Eliminate the |plugin_pid| parameter, which can't be trusted,
5061 // and is only used by BlinkTestController.
ericwilligersde386342016-10-19 02:35:095062 for (auto& observer : observers_)
5063 observer.PluginCrashed(plugin_path, plugin_pid);
tommyclie6633ca72014-10-31 00:40:425064}
5065
[email protected]d8415ad92012-08-23 14:40:505066void WebContentsImpl::OnRequestPpapiBrokerPermission(
nicka0ac8382016-12-15 23:59:235067 RenderViewHostImpl* source,
5068 int ppb_broker_route_id,
[email protected]d8415ad92012-08-23 14:40:505069 const GURL& url,
[email protected]2dec8ec2013-02-07 19:20:345070 const base::FilePath& plugin_path) {
Avi Drissman0a2a967d2019-04-01 19:33:525071 base::OnceCallback<void(bool)> permission_result_callback = base::BindOnce(
nicka0ac8382016-12-15 23:59:235072 &WebContentsImpl::SendPpapiBrokerPermissionResult, base::Unretained(this),
5073 source->GetProcess()->GetID(), ppb_broker_route_id);
[email protected]8c8fc292012-11-23 18:57:165074 if (!delegate_) {
Tommy Nyquist4b749d02018-03-20 21:46:295075 std::move(permission_result_callback).Run(false);
[email protected]8c8fc292012-11-23 18:57:165076 return;
[email protected]d8415ad92012-08-23 14:40:505077 }
5078
Avi Drissman0a2a967d2019-04-01 19:33:525079 delegate_->RequestPpapiBrokerPermission(
5080 this, url, plugin_path, std::move(permission_result_callback));
[email protected]d8415ad92012-08-23 14:40:505081}
5082
nicka0ac8382016-12-15 23:59:235083void WebContentsImpl::SendPpapiBrokerPermissionResult(int process_id,
5084 int ppb_broker_route_id,
5085 bool result) {
5086 RenderProcessHost* rph = RenderProcessHost::FromID(process_id);
5087 if (rph) {
5088 // TODO(nick): Convert this from ViewMsg_ to a Ppapi msg, since it
5089 // is not routed to a RenderView.
5090 rph->Send(
5091 new ViewMsg_PpapiBrokerPermissionResult(ppb_broker_route_id, result));
5092 }
[email protected]d8415ad92012-08-23 14:40:505093}
5094
fsamuel833ee7c2015-02-13 23:40:405095void WebContentsImpl::OnBrowserPluginMessage(RenderFrameHost* render_frame_host,
5096 const IPC::Message& message) {
Lei Zhang6410ec1a2019-01-31 21:38:585097 CHECK(!browser_plugin_embedder_);
fsamuel60b42282015-03-10 03:29:145098 CreateBrowserPluginEmbedderIfNecessary();
fsamuel833ee7c2015-02-13 23:40:405099 browser_plugin_embedder_->OnMessageReceived(message, render_frame_host);
[email protected]19be7a62012-10-01 23:03:375100}
brettw4b461082016-11-19 18:55:165101#endif // BUILDFLAG(ENABLE_PLUGINS)
[email protected]19be7a62012-10-01 23:03:375102
[email protected]795c28972012-12-06 06:13:395103void WebContentsImpl::OnUpdateFaviconURL(
lukasza178ff092017-06-07 23:39:505104 RenderFrameHostImpl* source,
[email protected]795c28972012-12-06 06:13:395105 const std::vector<FaviconURL>& candidates) {
lukasza178ff092017-06-07 23:39:505106 // Ignore favicons for non-main frame.
5107 if (source->GetParent()) {
5108 NOTREACHED();
5109 return;
5110 }
5111
pkotwiczf9bc20592015-04-21 18:57:065112 // We get updated favicon URLs after the page stops loading. If a cross-site
5113 // navigation occurs while a page is still loading, the initial page
5114 // may stop loading and send us updated favicon URLs after the navigation
5115 // for the new page has committed.
lukasza178ff092017-06-07 23:39:505116 if (!source->IsCurrent())
pkotwiczf9bc20592015-04-21 18:57:065117 return;
5118
ericwilligersde386342016-10-19 02:35:095119 for (auto& observer : observers_)
5120 observer.DidUpdateFaviconURL(candidates);
[email protected]795c28972012-12-06 06:13:395121}
5122
qinmin72e8bd02016-10-21 19:35:375123void WebContentsImpl::SetIsOverlayContent(bool is_overlay_content) {
5124 is_overlay_content_ = is_overlay_content;
5125}
5126
Albert J. Wongfb64e142018-10-16 01:10:455127void WebContentsImpl::DidFirstVisuallyNonEmptyPaint(
5128 RenderViewHostImpl* source) {
nicka0ac8382016-12-15 23:59:235129 // TODO(nick): When this is ported to FrameHostMsg_, we should only listen if
5130 // |source| is the main frame.
ericwilligersde386342016-10-19 02:35:095131 for (auto& observer : observers_)
5132 observer.DidFirstVisuallyNonEmptyPaint();
yusufod41c5f92015-03-06 00:14:285133
Carlos Caballero1215f882019-10-29 15:58:435134 if (source->theme_color() != last_sent_theme_color_) {
yusufod41c5f92015-03-06 00:14:285135 // Theme color should have updated by now if there was one.
ericwilligersde386342016-10-19 02:35:095136 for (auto& observer : observers_)
Carlos Caballero1215f882019-10-29 15:58:435137 observer.DidChangeThemeColor(source->theme_color());
5138 last_sent_theme_color_ = source->theme_color();
yusufod41c5f92015-03-06 00:14:285139 }
[email protected]9f268072013-11-07 00:02:155140}
[email protected]d9030b82013-07-19 08:26:065141
Adithya Srinivasan46b8a792019-02-01 14:47:235142bool WebContentsImpl::IsPortal() const {
5143 return portal();
5144}
5145
[email protected]17e286e2013-03-01 23:29:395146void WebContentsImpl::NotifyBeforeFormRepostWarningShow() {
ericwilligersde386342016-10-19 02:35:095147 for (auto& observer : observers_)
5148 observer.BeforeFormRepostWarningShow();
[email protected]17e286e2013-03-01 23:29:395149}
5150
[email protected]ec6c05f2013-10-23 18:41:575151void WebContentsImpl::ActivateAndShowRepostFormWarningDialog() {
5152 Activate();
5153 if (delegate_)
5154 delegate_->ShowRepostFormWarningDialog(this);
5155}
5156
[email protected]b4c84012014-04-28 19:51:105157bool WebContentsImpl::HasAccessedInitialDocument() {
5158 return has_accessed_initial_document_;
5159}
5160
afakhry6f0c1ec22016-07-14 13:55:135161void WebContentsImpl::UpdateTitleForEntry(NavigationEntry* entry,
[email protected]fcf75d42013-12-03 20:11:265162 const base::string16& title) {
[email protected]fcf75d42013-12-03 20:11:265163 base::string16 final_title;
Avi Drissman93002212017-09-27 03:20:525164 base::TrimWhitespace(title, base::TRIM_ALL, &final_title);
[email protected]420ae012009-04-24 05:16:325165
[email protected]987fc3a2011-05-26 14:18:095166 // If a page is created via window.open and never navigated,
5167 // there will be no navigation entry. In this situation,
[email protected]73eb2602012-02-09 19:50:555168 // |page_title_when_no_navigation_entry_| will be used for page title.
[email protected]987fc3a2011-05-26 14:18:095169 if (entry) {
[email protected]36fc0392011-12-25 03:59:515170 if (final_title == entry->GetTitle())
afakhry6f0c1ec22016-07-14 13:55:135171 return; // Nothing changed, don't bother.
[email protected]420ae012009-04-24 05:16:325172
[email protected]36fc0392011-12-25 03:59:515173 entry->SetTitle(final_title);
Avi Drissman93002212017-09-27 03:20:525174
5175 // The title for display may differ from the title just set; grab it.
5176 final_title = entry->GetTitleForDisplay();
[email protected]987fc3a2011-05-26 14:18:095177 } else {
5178 if (page_title_when_no_navigation_entry_ == final_title)
afakhry6f0c1ec22016-07-14 13:55:135179 return; // Nothing changed, don't bother.
[email protected]987fc3a2011-05-26 14:18:095180
5181 page_title_when_no_navigation_entry_ = final_title;
5182 }
[email protected]420ae012009-04-24 05:16:325183
[email protected]420ae012009-04-24 05:16:325184 // Lastly, set the title for the view.
[email protected]790e2fd2011-09-21 20:28:235185 view_->SetPageTitle(final_title);
[email protected]420ae012009-04-24 05:16:325186
ericwilligersde386342016-10-19 02:35:095187 for (auto& observer : observers_)
Avi Drissman93002212017-09-27 03:20:525188 observer.TitleWasSet(entry);
[email protected]66798902013-10-01 18:40:165189
afakhry6f0c1ec22016-07-14 13:55:135190 // Broadcast notifications when the UI should be updated.
5191 if (entry == controller_.GetEntryAtOffset(0))
5192 NotifyNavigationStateChanged(INVALIDATE_TYPE_TITLE);
[email protected]420ae012009-04-24 05:16:325193}
5194
fdegansa696e5112015-04-17 01:57:595195void WebContentsImpl::SendChangeLoadProgress() {
[email protected]960b0372014-05-19 18:01:005196 loading_last_progress_update_ = base::TimeTicks::Now();
Evan Stade9f328112019-10-23 20:49:255197 for (auto& observer : observers_)
5198 observer.LoadProgressChanged(frame_tree_.load_progress());
[email protected]960b0372014-05-19 18:01:005199}
5200
5201void WebContentsImpl::ResetLoadProgressState() {
fdegans1d16355162015-03-26 11:58:345202 frame_tree_.ResetLoadProgress();
[email protected]960b0372014-05-19 18:01:005203 loading_weak_factory_.InvalidateWeakPtrs();
5204 loading_last_progress_update_ = base::TimeTicks();
5205}
5206
clamy44e84ce2016-02-22 15:38:255207// Notifies the RenderWidgetHost instance about the fact that the page is
5208// loading, or done loading.
5209void WebContentsImpl::LoadingStateChanged(bool to_different_document,
5210 bool due_to_interstitial,
5211 LoadNotificationDetails* details) {
Nate Chapin22ea6592019-03-05 22:29:025212 if (is_being_destroyed_)
5213 return;
clamy44e84ce2016-02-22 15:38:255214 // Do not send notifications about loading changes in the FrameTree while the
5215 // interstitial page is pausing the throbber.
creisadca9862017-06-16 19:12:115216 if (ShowingInterstitialPage() && interstitial_page_->pause_throbber() &&
clamy44e84ce2016-02-22 15:38:255217 !due_to_interstitial) {
5218 return;
5219 }
5220
5221 bool is_loading = IsLoading();
5222
5223 if (!is_loading) {
5224 load_state_ = net::LoadStateWithParam(net::LOAD_STATE_IDLE,
5225 base::string16());
5226 load_state_host_.clear();
5227 upload_size_ = 0;
5228 upload_position_ = 0;
5229 }
5230
5231 GetRenderManager()->SetIsLoading(is_loading);
5232
5233 waiting_for_response_ = is_loading;
5234 is_load_to_different_document_ = to_different_document;
5235
5236 if (delegate_)
5237 delegate_->LoadingStateChanged(this, to_different_document);
5238 NotifyNavigationStateChanged(INVALIDATE_TYPE_LOAD);
5239
5240 std::string url = (details ? details->url.possibly_invalid_spec() : "NULL");
5241 if (is_loading) {
5242 TRACE_EVENT_ASYNC_BEGIN2("browser,navigation", "WebContentsImpl Loading",
5243 this, "URL", url, "Main FrameTreeNode id",
5244 GetFrameTree()->root()->frame_tree_node_id());
ericwilligersde386342016-10-19 02:35:095245 for (auto& observer : observers_)
5246 observer.DidStartLoading();
clamy44e84ce2016-02-22 15:38:255247 } else {
5248 TRACE_EVENT_ASYNC_END1("browser,navigation", "WebContentsImpl Loading",
5249 this, "URL", url);
ericwilligersde386342016-10-19 02:35:095250 for (auto& observer : observers_)
5251 observer.DidStopLoading();
clamy44e84ce2016-02-22 15:38:255252 }
5253
5254 // TODO(avi): Remove. http://crbug.com/170921
5255 int type = is_loading ? NOTIFICATION_LOAD_START : NOTIFICATION_LOAD_STOP;
5256 NotificationDetails det = NotificationService::NoDetails();
5257 if (details)
5258 det = Details<LoadNotificationDetails>(details);
5259 NotificationService::current()->Notify(
5260 type, Source<NavigationController>(&controller_), det);
5261}
5262
[email protected]02d7b6e2014-06-24 21:01:505263void WebContentsImpl::NotifyViewSwapped(RenderViewHost* old_host,
5264 RenderViewHost* new_host) {
[email protected]420ae012009-04-24 05:16:325265 // After sending out a swap notification, we need to send a disconnect
5266 // notification so that clients that pick up a pointer to |this| can NULL the
5267 // pointer. See Bug 1230284.
5268 notify_disconnection_ = true;
ericwilligersde386342016-10-19 02:35:095269 for (auto& observer : observers_)
5270 observer.RenderViewHostChanged(old_host, new_host);
[email protected]da7a7182013-09-06 08:11:115271
Jinsuk Kimdc5284c22018-07-11 02:22:065272 view_->RenderViewHostChanged(old_host, new_host);
5273
Lucas Furukawa Gadanif9ea4fc2018-08-03 23:57:135274 // If this is an inner WebContents that has swapped views, we need to reattach
5275 // it to its outer WebContents.
5276 if (node_.outer_web_contents())
5277 ReattachToOuterWebContentsFrame();
5278
[email protected]7a846df2012-09-20 19:17:395279 // Ensure that the associated embedder gets cleared after a RenderViewHost
5280 // gets swapped, so we don't reuse the same embedder next time a
5281 // RenderViewHost is attached to this WebContents.
5282 RemoveBrowserPluginEmbedder();
[email protected]420ae012009-04-24 05:16:325283}
5284
[email protected]02d7b6e2014-06-24 21:01:505285void WebContentsImpl::NotifyFrameSwapped(RenderFrameHost* old_host,
Jinsuk Kim895b2c52018-07-03 06:49:165286 RenderFrameHost* new_host,
5287 bool is_main_frame) {
yilkal796e89e2019-10-02 23:58:285288 if (!old_host) {
5289 frame_count_++;
5290 if (max_frame_count_ < frame_count_)
5291 max_frame_count_ = frame_count_;
5292 }
5293
Bo Liu168c8642017-08-28 18:26:025294#if defined(OS_ANDROID)
Bo Liu16dd7b8322018-05-02 21:19:505295 // Copy importance from |old_host| if |new_host| is a main frame.
5296 if (old_host && !new_host->GetParent()) {
Bo Liu7c6779e92017-08-16 02:02:285297 static_cast<RenderFrameHostImpl*>(new_host)
5298 ->GetRenderWidgetHost()
Bo Liu16dd7b8322018-05-02 21:19:505299 ->SetImportance(static_cast<RenderFrameHostImpl*>(old_host)
5300 ->GetRenderWidgetHost()
5301 ->importance());
Bo Liu7c6779e92017-08-16 02:02:285302 }
Bo Liu168c8642017-08-28 18:26:025303#endif
ericwilligersde386342016-10-19 02:35:095304 for (auto& observer : observers_)
5305 observer.RenderFrameHostChanged(old_host, new_host);
[email protected]02d7b6e2014-06-24 21:01:505306}
5307
[email protected]da7a7182013-09-06 08:11:115308// TODO(avi): Remove this entire function because this notification is already
5309// covered by two observer functions. http://crbug.com/170921
[email protected]b172aee2012-04-10 17:05:265310void WebContentsImpl::NotifyDisconnected() {
[email protected]420ae012009-04-24 05:16:325311 if (!notify_disconnection_)
5312 return;
5313
5314 notify_disconnection_ = false;
[email protected]8ff00d72012-10-23 19:12:215315 NotificationService::current()->Notify(
5316 NOTIFICATION_WEB_CONTENTS_DISCONNECTED,
5317 Source<WebContents>(this),
5318 NotificationService::NoDetails());
[email protected]420ae012009-04-24 05:16:325319}
5320
[email protected]cbb1ef592013-06-05 19:49:465321void WebContentsImpl::NotifyNavigationEntryCommitted(
5322 const LoadCommittedDetails& load_details) {
ericwilligersde386342016-10-19 02:35:095323 for (auto& observer : observers_)
5324 observer.NavigationEntryCommitted(load_details);
[email protected]cbb1ef592013-06-05 19:49:465325}
5326
Sam McNally5c087a32017-08-25 01:46:145327void WebContentsImpl::NotifyNavigationEntryChanged(
5328 const EntryChangedDetails& change_details) {
5329 for (auto& observer : observers_)
5330 observer.NavigationEntryChanged(change_details);
5331}
5332
5333void WebContentsImpl::NotifyNavigationListPruned(
5334 const PrunedDetails& pruned_details) {
5335 for (auto& observer : observers_)
5336 observer.NavigationListPruned(pruned_details);
5337}
5338
Christian Dullweber1af31e62018-02-22 11:49:485339void WebContentsImpl::NotifyNavigationEntriesDeleted() {
5340 for (auto& observer : observers_)
5341 observer.NavigationEntriesDeleted();
5342}
5343
rockotf62002a2016-09-15 00:08:595344void WebContentsImpl::OnAssociatedInterfaceRequest(
5345 RenderFrameHost* render_frame_host,
5346 const std::string& interface_name,
5347 mojo::ScopedInterfaceEndpointHandle handle) {
5348 auto it = binding_sets_.find(interface_name);
5349 if (it != binding_sets_.end())
5350 it->second->OnRequestForFrame(render_frame_host, std::move(handle));
5351}
5352
Ben Goodger9e24929e2017-07-21 03:13:405353void WebContentsImpl::OnInterfaceRequest(
5354 RenderFrameHost* render_frame_host,
5355 const std::string& interface_name,
5356 mojo::ScopedMessagePipeHandle* interface_pipe) {
Julie Jeongeun Kim8e2879e2019-08-06 23:55:205357 binders_.TryBind(interface_name, interface_pipe);
Ben Goodger9e24929e2017-07-21 03:13:405358 for (auto& observer : observers_) {
5359 observer.OnInterfaceRequestFromFrame(render_frame_host, interface_name,
5360 interface_pipe);
5361 if (!interface_pipe->is_valid())
5362 break;
5363 }
5364}
5365
Varun Khaneja5da94302019-06-25 04:48:105366void WebContentsImpl::OnDidBlockNavigation(
5367 const GURL& blocked_url,
5368 const GURL& initiator_url,
5369 blink::NavigationBlockedReason reason) {
Nicolas Dossou-gbetebcab2472017-09-09 12:28:405370 if (delegate_)
Varun Khaneja5da94302019-06-25 04:48:105371 delegate_->OnDidBlockNavigation(this, blocked_url, initiator_url, reason);
Nicolas Dossou-gbetebcab2472017-09-09 12:28:405372}
5373
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:415374const GURL& WebContentsImpl::GetMainFrameLastCommittedURL() {
[email protected]c31a84802014-04-03 15:55:495375 return GetLastCommittedURL();
5376}
5377
[email protected]b849847b2013-12-10 21:57:585378void WebContentsImpl::RenderFrameCreated(RenderFrameHost* render_frame_host) {
ericwilligersde386342016-10-19 02:35:095379 for (auto& observer : observers_)
5380 observer.RenderFrameCreated(render_frame_host);
dmazzonif8a31042016-12-03 00:52:135381 UpdateAccessibilityModeOnFrame(render_frame_host);
clamy91b7abf2016-03-24 15:17:455382
Becca Hughesd11d6502018-07-31 17:01:285383 if (display_cutout_host_impl_)
5384 display_cutout_host_impl_->RenderFrameCreated(render_frame_host);
5385
clamy91b7abf2016-03-24 15:17:455386 if (!render_frame_host->IsRenderFrameLive() || render_frame_host->GetParent())
5387 return;
5388
5389 NavigationEntry* entry = controller_.GetPendingEntry();
5390 if (entry && entry->IsViewSourceMode()) {
5391 // Put the renderer in view source mode.
5392 render_frame_host->Send(
5393 new FrameMsg_EnableViewSourceMode(render_frame_host->GetRoutingID()));
5394 }
[email protected]b849847b2013-12-10 21:57:585395}
5396
5397void WebContentsImpl::RenderFrameDeleted(RenderFrameHost* render_frame_host) {
yilkal796e89e2019-10-02 23:58:285398 if (!render_frame_host->GetParent() && IsBeingDestroyed() &&
Rakina Zata Amni74627df2019-10-15 02:03:175399 record_max_frame_count_when_leaving_current_page_ &&
5400 !static_cast<RenderFrameHostImpl*>(render_frame_host)
5401 ->is_in_back_forward_cache()) {
yilkal796e89e2019-10-02 23:58:285402 // Main frame has been deleted because WebContents is being destroyed.
Rakina Zata Amni74627df2019-10-15 02:03:175403 // Note that we aren't recording this here when the main frame is in the
5404 // back-forward cache because that means we've actually already navigated
5405 // away from it (and we got to this point because the WebContents is
5406 // deleted), which means |max_frame_count_| is already overwritten.
5407 // The |max_frame_count_| value will instead be recorded from within
5408 // |WebContentsImpl::ReadyToCommitNavigation()|.
yilkal796e89e2019-10-02 23:58:285409 RecordMaxFrameCountUMA(max_frame_count_);
5410 }
5411
creis3638897452017-05-09 22:07:505412 is_notifying_observers_ = true;
ericwilligersde386342016-10-19 02:35:095413 for (auto& observer : observers_)
5414 observer.RenderFrameDeleted(render_frame_host);
creis3638897452017-05-09 22:07:505415 is_notifying_observers_ = false;
zqzhang7b6f4f02016-12-07 22:10:225416#if BUILDFLAG(ENABLE_PLUGINS)
5417 pepper_playback_observer_->RenderFrameDeleted(render_frame_host);
5418#endif
Becca Hughesfd5d8f892018-06-14 18:23:365419
Becca Hughesd11d6502018-07-31 17:01:285420 if (display_cutout_host_impl_)
5421 display_cutout_host_impl_->RenderFrameDeleted(render_frame_host);
5422
Becca Hughesfd5d8f892018-06-14 18:23:365423 // Remove any fullscreen state that the frame has stored.
5424 FullscreenStateChanged(render_frame_host, false /* is_fullscreen */);
[email protected]b849847b2013-12-10 21:57:585425}
5426
[email protected]a09d53ce2014-01-31 00:46:425427void WebContentsImpl::ShowContextMenu(RenderFrameHost* render_frame_host,
5428 const ContextMenuParams& params) {
avie9fd8722017-04-10 16:17:585429 // If a renderer fires off a second command to show a context menu before the
5430 // first context menu is closed, just ignore it. https://crbug.com/707534
ekaramadf6750aa2017-06-06 18:29:425431 if (showing_context_menu_)
avie9fd8722017-04-10 16:17:585432 return;
5433
[email protected]077e704b2014-05-23 19:24:105434 ContextMenuParams context_menu_params(params);
[email protected]a09d53ce2014-01-31 00:46:425435 // Allow WebContentsDelegates to handle the context menu operation first.
Joel Klinghed0ea22ea2019-02-20 22:04:225436 if (delegate_ &&
5437 delegate_->HandleContextMenu(render_frame_host, context_menu_params))
[email protected]a09d53ce2014-01-31 00:46:425438 return;
5439
[email protected]077e704b2014-05-23 19:24:105440 render_view_host_delegate_view_->ShowContextMenu(render_frame_host,
5441 context_menu_params);
[email protected]a09d53ce2014-01-31 00:46:425442}
5443
Avi Drissman4891b852018-09-25 18:12:585444namespace {
5445// Normalizes the line endings: \r\n -> \n, lone \r -> \n.
5446base::string16 NormalizeLineBreaks(const base::string16& source) {
5447 static const base::NoDestructor<base::string16> kReturnNewline(
5448 base::ASCIIToUTF16("\r\n"));
5449 static const base::NoDestructor<base::string16> kReturn(
5450 base::ASCIIToUTF16("\r"));
5451 static const base::NoDestructor<base::string16> kNewline(
5452 base::ASCIIToUTF16("\n"));
5453
5454 std::vector<base::StringPiece16> pieces;
5455
5456 for (const auto& rn_line : base::SplitStringPieceUsingSubstr(
5457 source, *kReturnNewline, base::KEEP_WHITESPACE,
5458 base::SPLIT_WANT_ALL)) {
5459 auto r_lines = base::SplitStringPieceUsingSubstr(
5460 rn_line, *kReturn, base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL);
5461 std::move(std::begin(r_lines), std::end(r_lines),
5462 std::back_inserter(pieces));
5463 }
5464
5465 return base::JoinString(pieces, *kNewline);
5466}
5467} // namespace
5468
avi777ff452017-02-09 19:04:485469void WebContentsImpl::RunJavaScriptDialog(RenderFrameHost* render_frame_host,
5470 const base::string16& message,
5471 const base::string16& default_prompt,
avi777ff452017-02-09 19:04:485472 JavaScriptDialogType dialog_type,
5473 IPC::Message* reply_msg) {
Avi Drissman2ccbb402018-03-20 21:11:365474 // Ensure that if showing a dialog is the first thing that a page does, that
5475 // the contents of the previous page aren't shown behind it. This is required
5476 // because showing a dialog freezes the renderer, so no frames will be coming
5477 // from it. https://crbug.com/823353
5478 auto* render_widget_host_impl =
5479 static_cast<RenderFrameHostImpl*>(render_frame_host)
5480 ->GetRenderWidgetHost();
5481 if (render_widget_host_impl)
5482 render_widget_host_impl->ForceFirstFrameAfterNavigationTimeout();
5483
avi0720b02e2017-06-13 03:22:135484 // Running a dialog causes an exit to webpage-initiated fullscreen.
5485 // http://crbug.com/728276
Avi Drissmand18c51972018-08-15 18:32:095486 ForSecurityDropFullscreen();
avi0720b02e2017-06-13 03:22:135487
Avi Drissmane04d3992017-10-05 15:11:365488 auto callback =
5489 base::BindOnce(&WebContentsImpl::OnDialogClosed, base::Unretained(this),
5490 render_frame_host->GetProcess()->GetID(),
5491 render_frame_host->GetRoutingID(), reply_msg);
Pavel Feldman3c1842b2017-08-02 05:00:165492
Pavel Feldman3c1842b2017-08-02 05:00:165493 std::vector<protocol::PageHandler*> page_handlers =
5494 protocol::PageHandler::EnabledForWebContents(this);
5495
Pavel Feldmanfab3cb62017-10-05 01:02:015496 if (delegate_)
5497 dialog_manager_ = delegate_->GetJavaScriptDialogManager(this);
5498
Charlie Reis96077362019-07-04 00:34:045499 // While a JS message dialog is showing, defer commits in this WebContents.
5500 javascript_dialog_navigation_deferrer_ =
5501 std::make_unique<JavaScriptDialogNavigationDeferrer>();
5502
Pavel Feldmanfab3cb62017-10-05 01:02:015503 // Suppress JavaScript dialogs when requested. Also suppress messages when
5504 // showing an interstitial as it's shown over the previous page and we don't
5505 // want the hidden page's dialogs to interfere with the interstitial.
5506 bool should_suppress = ShowingInterstitialPage() ||
5507 (delegate_ && delegate_->ShouldSuppressDialogs(this));
Pavel Feldman1c587bc52018-04-04 21:16:055508 bool has_non_devtools_handlers = delegate_ && dialog_manager_;
5509 bool has_handlers = page_handlers.size() || has_non_devtools_handlers;
Pavel Feldmanfab3cb62017-10-05 01:02:015510 bool suppress_this_message = should_suppress || !has_handlers;
5511
5512 if (suppress_this_message) {
Avi Drissmane04d3992017-10-05 15:11:365513 std::move(callback).Run(true, false, base::string16());
Pavel Feldman3c1842b2017-08-02 05:00:165514 return;
5515 }
5516
5517 scoped_refptr<CloseDialogCallbackWrapper> wrapper =
Avi Drissmane04d3992017-10-05 15:11:365518 new CloseDialogCallbackWrapper(std::move(callback));
Pavel Feldman3c1842b2017-08-02 05:00:165519
5520 is_showing_javascript_dialog_ = true;
5521
Avi Drissman4891b852018-09-25 18:12:585522 base::string16 normalized_message = NormalizeLineBreaks(message);
5523
Pavel Feldman3c1842b2017-08-02 05:00:165524 for (auto* handler : page_handlers) {
Avi Drissmane04d3992017-10-05 15:11:365525 handler->DidRunJavaScriptDialog(
Avi Drissman4891b852018-09-25 18:12:585526 render_frame_host->GetLastCommittedURL(), normalized_message,
5527 default_prompt, dialog_type, has_non_devtools_handlers,
Avi Drissmane04d3992017-10-05 15:11:365528 base::BindOnce(&CloseDialogCallbackWrapper::Run, wrapper, false));
Pavel Feldman3c1842b2017-08-02 05:00:165529 }
5530
5531 if (dialog_manager_) {
[email protected]87de04b02014-04-08 22:14:495532 dialog_manager_->RunJavaScriptDialog(
Avi Drissman4891b852018-09-25 18:12:585533 this, render_frame_host, dialog_type, normalized_message,
5534 default_prompt,
Avi Drissmane04d3992017-10-05 15:11:365535 base::BindOnce(&CloseDialogCallbackWrapper::Run, wrapper, false),
5536 &suppress_this_message);
[email protected]87de04b02014-04-08 22:14:495537 }
5538
5539 if (suppress_this_message) {
5540 // If we are suppressing messages, just reply as if the user immediately
5541 // pressed "Cancel", passing true to |dialog_was_suppressed|.
Avi Drissmane04d3992017-10-05 15:11:365542 wrapper->Run(true, false, base::string16());
[email protected]87de04b02014-04-08 22:14:495543 }
[email protected]87de04b02014-04-08 22:14:495544}
5545
5546void WebContentsImpl::RunBeforeUnloadConfirm(
[email protected]39f2f9252014-05-03 00:54:015547 RenderFrameHost* render_frame_host,
[email protected]87de04b02014-04-08 22:14:495548 bool is_reload,
5549 IPC::Message* reply_msg) {
Avi Drissman2ccbb402018-03-20 21:11:365550 // Ensure that if showing a dialog is the first thing that a page does, that
5551 // the contents of the previous page aren't shown behind it. This is required
5552 // because showing a dialog freezes the renderer, so no frames will be coming
5553 // from it. https://crbug.com/823353
5554 auto* render_widget_host_impl =
5555 static_cast<RenderFrameHostImpl*>(render_frame_host)
5556 ->GetRenderWidgetHost();
5557 if (render_widget_host_impl)
5558 render_widget_host_impl->ForceFirstFrameAfterNavigationTimeout();
5559
avi0720b02e2017-06-13 03:22:135560 // Running a dialog causes an exit to webpage-initiated fullscreen.
5561 // http://crbug.com/728276
Avi Drissmand18c51972018-08-15 18:32:095562 ForSecurityDropFullscreen();
avi0720b02e2017-06-13 03:22:135563
[email protected]39f2f9252014-05-03 00:54:015564 RenderFrameHostImpl* rfhi =
5565 static_cast<RenderFrameHostImpl*>(render_frame_host);
[email protected]87de04b02014-04-08 22:14:495566 if (delegate_)
5567 delegate_->WillRunBeforeUnloadConfirm();
5568
Avi Drissmane04d3992017-10-05 15:11:365569 auto callback =
5570 base::BindOnce(&WebContentsImpl::OnDialogClosed, base::Unretained(this),
5571 render_frame_host->GetProcess()->GetID(),
5572 render_frame_host->GetRoutingID(), reply_msg);
Pavel Feldman3c1842b2017-08-02 05:00:165573
Pavel Feldman3c1842b2017-08-02 05:00:165574 std::vector<protocol::PageHandler*> page_handlers =
5575 protocol::PageHandler::EnabledForWebContents(this);
5576
Pavel Feldmanfab3cb62017-10-05 01:02:015577 if (delegate_)
5578 dialog_manager_ = delegate_->GetJavaScriptDialogManager(this);
5579
Charlie Reis96077362019-07-04 00:34:045580 // While a JS beforeunload dialog is showing, defer commits in this
5581 // WebContents.
5582 javascript_dialog_navigation_deferrer_ =
5583 std::make_unique<JavaScriptDialogNavigationDeferrer>();
5584
Pavel Feldmanfab3cb62017-10-05 01:02:015585 bool should_suppress = ShowingInterstitialPage() || !rfhi->is_active() ||
5586 (delegate_ && delegate_->ShouldSuppressDialogs(this));
Pavel Feldman1c587bc52018-04-04 21:16:055587 bool has_non_devtools_handlers = delegate_ && dialog_manager_;
5588 bool has_handlers = page_handlers.size() || has_non_devtools_handlers;
Pavel Feldmanfab3cb62017-10-05 01:02:015589 if (should_suppress || !has_handlers) {
Avi Drissmane04d3992017-10-05 15:11:365590 std::move(callback).Run(false, true, base::string16());
[email protected]87de04b02014-04-08 22:14:495591 return;
5592 }
5593
5594 is_showing_before_unload_dialog_ = true;
Pavel Feldman3c1842b2017-08-02 05:00:165595
5596 scoped_refptr<CloseDialogCallbackWrapper> wrapper =
Avi Drissmane04d3992017-10-05 15:11:365597 new CloseDialogCallbackWrapper(std::move(callback));
Pavel Feldman3c1842b2017-08-02 05:00:165598
5599 GURL frame_url = rfhi->GetLastCommittedURL();
5600 for (auto* handler : page_handlers) {
Avi Drissmane04d3992017-10-05 15:11:365601 handler->DidRunBeforeUnloadConfirm(
Pavel Feldman1c587bc52018-04-04 21:16:055602 frame_url, has_non_devtools_handlers,
Avi Drissmane04d3992017-10-05 15:11:365603 base::BindOnce(&CloseDialogCallbackWrapper::Run, wrapper, false));
Pavel Feldman3c1842b2017-08-02 05:00:165604 }
5605
5606 if (dialog_manager_) {
Avi Drissmane04d3992017-10-05 15:11:365607 dialog_manager_->RunBeforeUnloadDialog(
Avi Drissmanff9ed752017-11-28 21:18:125608 this, render_frame_host, is_reload,
Avi Drissmane04d3992017-10-05 15:11:365609 base::BindOnce(&CloseDialogCallbackWrapper::Run, wrapper, false));
Pavel Feldman3c1842b2017-08-02 05:00:165610 }
[email protected]87de04b02014-04-08 22:14:495611}
5612
Kent Tamurac260c9a2018-09-14 09:10:565613void WebContentsImpl::RunFileChooser(
5614 RenderFrameHost* render_frame_host,
Kent Tamura512a27e2018-10-04 00:49:325615 std::unique_ptr<content::FileSelectListener> listener,
Kent Tamurac260c9a2018-09-14 09:10:565616 const blink::mojom::FileChooserParams& params) {
Avi Drissman3d41e7712018-08-27 21:18:085617 // Any explicit focusing of another window while this WebContents is in
5618 // fullscreen can be used to confuse the user, so drop fullscreen.
5619 ForSecurityDropFullscreen();
5620
Andrey Lushnikov81b992e2019-06-27 23:27:215621 RenderFrameHostImpl* rfhi =
5622 static_cast<RenderFrameHostImpl*>(render_frame_host);
5623 if (devtools_instrumentation::InterceptFileChooser(rfhi, &listener, params))
5624 return;
5625
naskoada75b22016-06-11 16:09:465626 if (delegate_)
Kent Tamura512a27e2018-10-04 00:49:325627 delegate_->RunFileChooser(render_frame_host, std::move(listener), params);
5628 else
5629 listener->FileSelectionCanceled();
naskoada75b22016-06-11 16:09:465630}
5631
[email protected]a86c0e962013-12-17 17:10:395632WebContents* WebContentsImpl::GetAsWebContents() {
5633 return this;
5634}
5635
mcnee336ea2c2017-05-23 22:50:595636#if !defined(OS_ANDROID)
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:415637double WebContentsImpl::GetPendingPageZoomLevel() {
wjmaclean64951902016-04-29 20:59:125638 NavigationEntry* pending_entry = GetController().GetPendingEntry();
5639 if (!pending_entry)
5640 return HostZoomMap::GetZoomLevel(this);
5641
5642 GURL url = pending_entry->GetURL();
5643 return HostZoomMap::GetForWebContents(this)->GetZoomLevelForHostAndScheme(
5644 url.scheme(), net::GetHostOrSpecFromURL(url));
5645}
mcnee336ea2c2017-05-23 22:50:595646#endif // !defined(OS_ANDROID)
wjmaclean64951902016-04-29 20:59:125647
qinmin72e8bd02016-10-21 19:35:375648bool WebContentsImpl::HideDownloadUI() const {
5649 return is_overlay_content_;
5650}
5651
mlamouri5cd9ae82017-02-18 11:05:095652bool WebContentsImpl::HasPersistentVideo() const {
5653 return has_persistent_video_;
5654}
5655
Ian Vollick5d5c37f2019-06-19 22:50:575656bool WebContentsImpl::IsSpatialNavigationDisabled() const {
5657 return is_spatial_navigation_disabled_;
5658}
5659
Albert J. Wong65fe64d2019-09-20 02:48:145660RenderFrameHostImpl* WebContentsImpl::GetPendingMainFrame() {
clamy610c63b32017-12-22 15:05:185661 return GetRenderManager()->speculative_frame_host();
Alex Moshchuk27caae82017-09-11 23:11:185662}
5663
mlamourie52c1b542017-02-23 14:57:505664bool WebContentsImpl::HasActiveEffectivelyFullscreenVideo() const {
5665 return media_web_contents_observer_->HasActiveEffectivelyFullscreenVideo();
5666}
5667
François Beaufortad6c5232018-02-26 11:00:445668bool WebContentsImpl::IsPictureInPictureAllowedForFullscreenVideo() const {
5669 return media_web_contents_observer_
5670 ->IsPictureInPictureAllowedForFullscreenVideo();
5671}
5672
ekaramada110f642016-12-21 19:47:285673bool WebContentsImpl::IsFocusedElementEditable() {
5674 RenderFrameHostImpl* frame = GetFocusedFrame();
5675 return frame && frame->has_focused_editable_element();
5676}
5677
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:415678bool WebContentsImpl::IsShowingContextMenu() {
ekaramadf6750aa2017-06-06 18:29:425679 return showing_context_menu_;
5680}
5681
5682void WebContentsImpl::SetShowingContextMenu(bool showing) {
5683 DCHECK_NE(showing_context_menu_, showing);
5684 showing_context_menu_ = showing;
5685
5686 if (auto* view = GetRenderWidgetHostView()) {
5687 // Notify the main frame's RWHV to run the platform-specific code, if any.
5688 static_cast<RenderWidgetHostViewBase*>(view)->SetShowingContextMenu(
5689 showing);
5690 }
5691}
5692
ekaramada110f642016-12-21 19:47:285693void WebContentsImpl::ClearFocusedElement() {
5694 if (auto* frame = GetFocusedFrame())
5695 frame->ClearFocusedElement();
5696}
5697
[email protected]7912e822014-04-16 02:37:035698bool WebContentsImpl::IsNeverVisible() {
5699 if (!delegate_)
5700 return false;
5701 return delegate_->IsNeverVisible(this);
5702}
5703
[email protected]5a3bdf52012-05-24 15:12:575704RenderViewHostDelegateView* WebContentsImpl::GetDelegateView() {
5705 return render_view_host_delegate_view_;
[email protected]420ae012009-04-24 05:16:325706}
5707
Leon Hanc819dc62019-01-28 04:30:195708blink::mojom::RendererPreferences WebContentsImpl::GetRendererPrefs(
[email protected]8ff00d72012-10-23 19:12:215709 BrowserContext* browser_context) const {
[email protected]840b1512009-07-21 16:53:465710 return renderer_preferences_;
[email protected]80d96fa2009-06-10 22:34:515711}
5712
[email protected]7a846df2012-09-20 19:17:395713void WebContentsImpl::RemoveBrowserPluginEmbedder() {
Lei Zhang6410ec1a2019-01-31 21:38:585714 browser_plugin_embedder_.reset();
[email protected]7a846df2012-09-20 19:17:395715}
5716
Lucas Furukawa Gadani2ec00c82018-12-14 15:53:165717RenderFrameHostImpl* WebContentsImpl::GetOuterWebContentsFrame() {
5718 if (GetOuterDelegateFrameTreeNodeId() ==
5719 FrameTreeNode::kFrameTreeNodeInvalidId) {
5720 return nullptr;
5721 }
5722
5723 FrameTreeNode* outer_node =
5724 FrameTreeNode::GloballyFindByID(GetOuterDelegateFrameTreeNodeId());
5725 // The outer node should be in the outer WebContents.
5726 DCHECK_EQ(outer_node->frame_tree(), GetOuterWebContents()->GetFrameTree());
5727 return outer_node->parent()->current_frame_host();
5728}
5729
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:415730WebContentsImpl* WebContentsImpl::GetOuterWebContents() {
lfg601233692017-03-06 22:45:445731 if (GuestMode::IsCrossProcessFrameGuest(this))
5732 return node_.outer_web_contents();
ekaramad1beabecc2016-09-23 20:18:175733
5734 if (browser_plugin_guest_)
5735 return browser_plugin_guest_->embedder_web_contents();
5736
Lucas Furukawa Gadani105de1e812018-05-31 19:38:395737 return node_.outer_web_contents();
avallee0206f782016-07-28 18:55:335738}
5739
Lucas Furukawa Gadani2ec00c82018-12-14 15:53:165740std::vector<WebContents*> WebContentsImpl::GetInnerWebContents() {
5741 std::vector<WebContents*> all_inner_contents;
5742 if (browser_plugin_embedder_) {
Sean Topping7df73952019-05-16 17:21:165743 BrowserPluginGuestManager* guest_manager =
5744 GetBrowserContext()->GetGuestManager();
5745 if (guest_manager) {
5746 guest_manager->ForEachGuest(
5747 this,
5748 base::BindRepeating(&GetInnerWebContentsHelper, &all_inner_contents));
5749 }
Lucas Furukawa Gadani2ec00c82018-12-14 15:53:165750 }
5751 const auto& inner_contents = node_.GetInnerWebContents();
5752 all_inner_contents.insert(all_inner_contents.end(), inner_contents.begin(),
5753 inner_contents.end());
5754 return all_inner_contents;
5755}
5756
avallee0206f782016-07-28 18:55:335757WebContentsImpl* WebContentsImpl::GetFocusedWebContents() {
lfg601233692017-03-06 22:45:445758 return GetOutermostWebContents()->node_.focused_web_contents();
avallee0206f782016-07-28 18:55:335759}
5760
avallee9993fca2016-11-17 06:16:505761bool WebContentsImpl::ContainsOrIsFocusedWebContents() {
5762 for (WebContentsImpl* focused_contents = GetFocusedWebContents();
5763 focused_contents;
5764 focused_contents = focused_contents->GetOuterWebContents()) {
5765 if (focused_contents == this)
5766 return true;
5767 }
5768
5769 return false;
5770}
5771
avallee0206f782016-07-28 18:55:335772WebContentsImpl* WebContentsImpl::GetOutermostWebContents() {
5773 WebContentsImpl* root = this;
5774 while (root->GetOuterWebContents())
5775 root = root->GetOuterWebContents();
5776 return root;
5777}
5778
avalleed68cdd12017-06-21 22:07:275779void WebContentsImpl::FocusOuterAttachmentFrameChain() {
5780 WebContentsImpl* outer_contents = GetOuterWebContents();
5781 if (!outer_contents)
5782 return;
5783
5784 FrameTreeNode* outer_node =
5785 FrameTreeNode::GloballyFindByID(GetOuterDelegateFrameTreeNodeId());
5786 outer_contents->frame_tree_.SetFocusedFrame(outer_node, nullptr);
5787
5788 // For a browser initiated focus change, let embedding renderer know of the
5789 // change. Otherwise, if the currently focused element is just across a
5790 // process boundary in focus order, it will not be possible to move across
5791 // that boundary. This is because the target element will already be focused
5792 // (that renderer was not notified) and drop the event.
5793 if (GetRenderManager()->GetProxyToOuterDelegate())
5794 GetRenderManager()->GetProxyToOuterDelegate()->SetFocusedFrame();
5795
5796 outer_contents->FocusOuterAttachmentFrameChain();
5797}
5798
Sean Toppingbbc4a2bd2019-01-30 02:16:145799void WebContentsImpl::InnerWebContentsCreated(WebContents* inner_web_contents) {
Shivani Sharma985474f2019-05-23 21:36:475800 had_inner_webcontents_ = true;
Sean Toppingbbc4a2bd2019-01-30 02:16:145801 for (auto& observer : observers_)
5802 observer.InnerWebContentsCreated(inner_web_contents);
5803}
5804
[email protected]b172aee2012-04-10 17:05:265805void WebContentsImpl::RenderViewCreated(RenderViewHost* render_view_host) {
[email protected]86f98a22013-03-20 14:35:005806 if (delegate_)
[email protected]e85165c642014-06-10 14:34:315807 view_->SetOverscrollControllerEnabled(CanOverscrollContent());
[email protected]a6b73c62013-02-11 23:05:085808
[email protected]8ff00d72012-10-23 19:12:215809 NotificationService::current()->Notify(
5810 NOTIFICATION_WEB_CONTENTS_RENDER_VIEW_HOST_CREATED,
5811 Source<WebContents>(this),
5812 Details<RenderViewHost>(render_view_host));
[email protected]420ae012009-04-24 05:16:325813
[email protected]60780f412013-02-25 16:34:105814 view_->RenderViewCreated(render_view_host);
[email protected]11f764bb2011-05-25 19:45:265815
ericwilligersde386342016-10-19 02:35:095816 for (auto& observer : observers_)
5817 observer.RenderViewCreated(render_view_host);
Joel Einbinder848001c2017-09-19 21:39:315818 if (delegate_)
5819 RenderFrameDevToolsAgentHost::WebContentsCreated(this);
[email protected]420ae012009-04-24 05:16:325820}
5821
[email protected]b172aee2012-04-10 17:05:265822void WebContentsImpl::RenderViewReady(RenderViewHost* rvh) {
[email protected]151a63d2011-12-20 22:32:525823 if (rvh != GetRenderViewHost()) {
[email protected]420ae012009-04-24 05:16:325824 // Don't notify the world, since this came from a renderer in the
5825 // background.
5826 return;
5827 }
5828
dmazzoni62111452017-04-28 23:46:555829 RenderWidgetHostViewBase* rwhv =
5830 static_cast<RenderWidgetHostViewBase*>(GetRenderWidgetHostView());
5831 if (rwhv)
5832 rwhv->SetMainFrameAXTreeID(GetMainFrame()->GetAXTreeID());
5833
[email protected]da7a7182013-09-06 08:11:115834 notify_disconnection_ = true;
[email protected]da7a7182013-09-06 08:11:115835
[email protected]be1f56ab2011-12-22 06:55:315836 bool was_crashed = IsCrashed();
[email protected]443b80e2010-12-14 00:42:235837 SetIsCrashed(base::TERMINATION_STATUS_STILL_RUNNING, 0);
[email protected]9d8a4642009-07-29 17:25:305838
5839 // Restore the focus to the tab (otherwise the focus will be on the top
5840 // window).
[email protected]484ae5912010-09-29 19:16:145841 if (was_crashed && !FocusLocationBarByDefault() &&
5842 (!delegate_ || delegate_->ShouldFocusPageAfterCrash())) {
[email protected]f3615f02013-02-26 06:09:065843 view_->Focus();
[email protected]484ae5912010-09-29 19:16:145844 }
[email protected]32ded2212011-11-10 18:51:435845
ericwilligersde386342016-10-19 02:35:095846 for (auto& observer : observers_)
5847 observer.RenderViewReady();
Jinsuk Kim895b2c52018-07-03 06:49:165848
5849 view_->RenderViewReady();
[email protected]420ae012009-04-24 05:16:325850}
5851
[email protected]ec6a7eb2013-04-22 17:34:225852void WebContentsImpl::RenderViewTerminated(RenderViewHost* rvh,
5853 base::TerminationStatus status,
5854 int error_code) {
[email protected]151a63d2011-12-20 22:32:525855 if (rvh != GetRenderViewHost()) {
[email protected]420ae012009-04-24 05:16:325856 // The pending page's RenderViewHost is gone.
5857 return;
5858 }
5859
[email protected]2fcdcc32014-03-05 02:14:075860 // Ensure fullscreen mode is exited in the |delegate_| since a crashed
5861 // renderer may not have made a clean exit.
alexmos5dd617d2016-06-07 04:21:155862 if (IsFullscreenForCurrentTab())
bokanece34a82016-01-28 19:49:465863 ExitFullscreenMode(false);
[email protected]2fcdcc32014-03-05 02:14:075864
Jennifer Apacibleb4b972a2018-06-07 04:41:025865 // Ensure any video in Picture-in-Picture is exited in the |delegate_| since
5866 // a crashed renderer may not have made a clean exit.
5867 if (HasPictureInPictureVideo())
5868 ExitPictureInPicture();
5869
[email protected]698191dc2014-02-25 01:06:135870 // Cancel any visible dialogs so they are not left dangling over the sad tab.
creis89a0f782015-05-27 16:13:175871 CancelActiveAndPendingDialogs();
[email protected]698191dc2014-02-25 01:06:135872
dalecurtis3d1e1302017-02-20 22:33:255873 audio_stream_monitor_.RenderProcessGone(rvh->GetProcess()->GetID());
5874
[email protected]960b0372014-05-19 18:01:005875 // Reset the loading progress. TODO(avi): What does it mean to have a
5876 // "renderer crash" when there is more than one renderer process serving a
5877 // webpage? Once this function is called at a more granular frame level, we
5878 // probably will need to more granularly reset the state here.
5879 ResetLoadProgressState();
clamy44e84ce2016-02-22 15:38:255880 NotifyDisconnected();
5881 SetIsCrashed(status, error_code);
[email protected]960b0372014-05-19 18:01:005882
ericwilligersde386342016-10-19 02:35:095883 for (auto& observer : observers_)
5884 observer.RenderProcessGone(GetCrashedStatus());
[email protected]420ae012009-04-24 05:16:325885}
5886
[email protected]b172aee2012-04-10 17:05:265887void WebContentsImpl::RenderViewDeleted(RenderViewHost* rvh) {
ericwilligersde386342016-10-19 02:35:095888 for (auto& observer : observers_)
5889 observer.RenderViewDeleted(rvh);
[email protected]2e4633c2009-07-09 16:58:065890}
5891
sky8b00392d2015-01-10 00:30:285892void WebContentsImpl::UpdateTargetURL(RenderViewHost* render_view_host,
5893 const GURL& url) {
5894 if (fullscreen_widget_routing_id_ != MSG_ROUTING_NONE) {
Lukasz Anforowiczfd707142018-02-07 19:46:135895 // If we're in flash fullscreen (i.e. Pepper plugin fullscreen) only update
5896 // the url if it's from the fullscreen renderer.
sky8b00392d2015-01-10 00:30:285897 RenderWidgetHostView* fs = GetFullscreenRenderWidgetHostView();
avif9ab5d942015-10-15 14:05:445898 if (fs && fs->GetRenderWidgetHost() != render_view_host->GetWidget())
sky8b00392d2015-01-10 00:30:285899 return;
5900 }
Lukasz Anforowiczfd707142018-02-07 19:46:135901
5902 // In case of racey updates from multiple RenderViewHosts, the last URL should
5903 // be shown - see also some discussion in https://crbug.com/807776.
5904 if (!url.is_valid() && render_view_host != view_that_set_last_target_url_)
5905 return;
5906 view_that_set_last_target_url_ = url.is_valid() ? render_view_host : nullptr;
5907
[email protected]6934a702011-12-20 00:04:515908 if (delegate_)
avi450ce00e2014-09-24 01:58:525909 delegate_->UpdateTargetURL(this, url);
[email protected]420ae012009-04-24 05:16:325910}
5911
Lukasz Anforowiczfd707142018-02-07 19:46:135912void WebContentsImpl::ClearTargetURL() {
5913 view_that_set_last_target_url_ = nullptr;
5914 if (delegate_)
5915 delegate_->UpdateTargetURL(this, GURL());
5916}
5917
[email protected]b172aee2012-04-10 17:05:265918void WebContentsImpl::Close(RenderViewHost* rvh) {
[email protected]60780f412013-02-25 16:34:105919#if defined(OS_MACOSX)
[email protected]07707302009-11-06 00:50:295920 // The UI may be in an event-tracking loop, such as between the
5921 // mouse-down and mouse-up in text selection or a button click.
5922 // Defer the close until after tracking is complete, so that we
5923 // don't free objects out from under the UI.
[email protected]07707302009-11-06 00:50:295924 // TODO(shess): This could get more fine-grained. For instance,
5925 // closing a tab in another window while selecting text in the
5926 // current window's Omnibox should be just fine.
Christopher Cameron9ae31e0d2019-03-01 20:01:195927 if (view_->CloseTabAfterEventTrackingIfNeeded())
[email protected]07707302009-11-06 00:50:295928 return;
[email protected]60780f412013-02-25 16:34:105929#endif
[email protected]07707302009-11-06 00:50:295930
[email protected]420ae012009-04-24 05:16:325931 // Ignore this if it comes from a RenderViewHost that we aren't showing.
[email protected]151a63d2011-12-20 22:32:525932 if (delegate_ && rvh == GetRenderViewHost())
[email protected]6934a702011-12-20 00:04:515933 delegate_->CloseContents(this);
[email protected]420ae012009-04-24 05:16:325934}
5935
Avi Drissman014dec72018-06-07 02:34:015936void WebContentsImpl::RequestSetBounds(const gfx::Rect& new_bounds) {
5937 if (delegate_)
5938 delegate_->SetContentsBounds(this, new_bounds);
[email protected]420ae012009-04-24 05:16:325939}
5940
fdegansa696e5112015-04-17 01:57:595941void WebContentsImpl::DidStartLoading(FrameTreeNode* frame_tree_node,
[email protected]e3b10d12014-03-28 16:06:095942 bool to_different_document) {
Evan Stadec3b26022017-07-14 19:50:245943 LoadingStateChanged(frame_tree_node->IsMainFrame() && to_different_document,
5944 false, nullptr);
dmazzonibf8cec42015-02-08 08:28:085945
Xiyuan Xia2a66b112018-12-06 15:52:165946 // Reset the focus state from DidStartNavigation to false if a new load starts
5947 // afterward, in case loading logic triggers a FocusLocationBarByDefault call.
5948 should_focus_location_bar_by_default_ = false;
5949
dmazzonibf8cec42015-02-08 08:28:085950 // Notify accessibility that the user is navigating away from the
5951 // current document.
Dominic Farolinof87ec2a2019-07-04 09:18:095952 // TODO(domfarolino, dmazzoni): Do this using WebContentsObserver. See
5953 // https://crbug.com/981271.
dmazzonibf8cec42015-02-08 08:28:085954 BrowserAccessibilityManager* manager =
fdegansa696e5112015-04-17 01:57:595955 frame_tree_node->current_frame_host()->browser_accessibility_manager();
dmazzonibf8cec42015-02-08 08:28:085956 if (manager)
5957 manager->UserIsNavigatingAway();
[email protected]420ae012009-04-24 05:16:325958}
5959
fdegansc73eec22015-03-19 14:47:545960void WebContentsImpl::DidStopLoading() {
dcheng59716272016-04-09 05:19:085961 std::unique_ptr<LoadNotificationDetails> details;
[email protected]420ae012009-04-24 05:16:325962
[email protected]9595fd82013-04-19 21:28:495963 // Use the last committed entry rather than the active one, in case a
5964 // pending entry has been created.
5965 NavigationEntry* entry = controller_.GetLastCommittedEntry();
[email protected]3691e5cf2014-01-22 10:16:205966 Navigator* navigator = frame_tree_.root()->navigator();
[email protected]9595fd82013-04-19 21:28:495967
[email protected]420ae012009-04-24 05:16:325968 // An entry may not exist for a stop when loading an initial blank page or
5969 // if an iframe injected by script into a blank page finishes loading.
5970 if (entry) {
[email protected]3691e5cf2014-01-22 10:16:205971 base::TimeDelta elapsed =
5972 base::TimeTicks::Now() - navigator->GetCurrentLoadStart();
[email protected]420ae012009-04-24 05:16:325973
5974 details.reset(new LoadNotificationDetails(
[email protected]36fc0392011-12-25 03:59:515975 entry->GetVirtualURL(),
[email protected]420ae012009-04-24 05:16:325976 elapsed,
5977 &controller_,
5978 controller_.GetCurrentEntryIndex()));
5979 }
5980
clamy44e84ce2016-02-22 15:38:255981 LoadingStateChanged(true, false, details.get());
[email protected]420ae012009-04-24 05:16:325982}
5983
fdegansa696e5112015-04-17 01:57:595984void WebContentsImpl::DidChangeLoadProgress() {
Nate Chapin22ea6592019-03-05 22:29:025985 if (is_being_destroyed_)
5986 return;
fdegansa696e5112015-04-17 01:57:595987 double load_progress = frame_tree_.load_progress();
5988
5989 // The delegate is notified immediately for the first and last updates. Also,
5990 // since the message loop may be pretty busy when a page is loaded, it might
5991 // not execute a posted task in a timely manner so the progress report is sent
5992 // immediately if enough time has passed.
5993 base::TimeDelta min_delay =
5994 base::TimeDelta::FromMilliseconds(kMinimumDelayBetweenLoadingUpdatesMS);
5995 bool delay_elapsed = loading_last_progress_update_.is_null() ||
5996 base::TimeTicks::Now() - loading_last_progress_update_ > min_delay;
5997
5998 if (load_progress == 0.0 || load_progress == 1.0 || delay_elapsed) {
5999 // If there is a pending task to send progress, it is now obsolete.
6000 loading_weak_factory_.InvalidateWeakPtrs();
6001
6002 // Notify the load progress change.
6003 SendChangeLoadProgress();
6004
6005 // Clean-up the states if needed.
6006 if (load_progress == 1.0)
6007 ResetLoadProgressState();
6008 return;
6009 }
6010
6011 if (loading_weak_factory_.HasWeakPtrs())
6012 return;
6013
skyostil95082a62015-06-05 19:53:076014 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
tzike2aca992017-09-05 08:50:546015 FROM_HERE,
6016 base::BindOnce(&WebContentsImpl::SendChangeLoadProgress,
6017 loading_weak_factory_.GetWeakPtr()),
fdegansa696e5112015-04-17 01:57:596018 min_delay);
6019}
6020
avid6d88b912017-01-13 00:16:006021std::vector<std::unique_ptr<NavigationThrottle>>
6022WebContentsImpl::CreateThrottlesForNavigation(
clamycbf524d42016-09-27 12:48:356023 NavigationHandle* navigation_handle) {
Charlie Reis96077362019-07-04 00:34:046024 auto throttles = GetContentClient()->browser()->CreateThrottlesForNavigation(
clamycbf524d42016-09-27 12:48:356025 navigation_handle);
Charlie Reis96077362019-07-04 00:34:046026
6027 // This is not a normal place to be adding a throttle. However, in the case
6028 // javascript dialogs, related logic is present in the web_contents/ layer,
6029 // and the purpose of the throttle is to ensure that navigation commits are
6030 // deferred for the entire WebContents. Most throttles are either added by
6031 // the embederrer outside of content/, or are per-frame and added by
6032 // NavigationThrottleRunner.
6033 std::unique_ptr<content::NavigationThrottle> dialog_throttle =
6034 JavaScriptDialogNavigationThrottle::MaybeCreateThrottleFor(
6035 navigation_handle);
6036 if (dialog_throttle)
6037 throttles.push_back(std::move(dialog_throttle));
6038
6039 return throttles;
clamycbf524d42016-09-27 12:48:356040}
6041
clamy1e5574e2016-09-29 16:48:446042std::unique_ptr<NavigationUIData> WebContentsImpl::GetNavigationUIData(
6043 NavigationHandle* navigation_handle) {
clamy1e5574e2016-09-29 16:48:446044 return GetContentClient()->browser()->GetNavigationUIData(navigation_handle);
6045}
6046
pdr15f5b5b2017-06-20 00:58:006047void WebContentsImpl::DidCancelLoading() {
6048 controller_.DiscardNonCommittedEntries();
6049
6050 // Update the URL display.
6051 NotifyNavigationStateChanged(INVALIDATE_TYPE_URL);
6052}
6053
[email protected]59167c22013-06-03 18:07:326054void WebContentsImpl::DidAccessInitialDocument() {
[email protected]b4c84012014-04-28 19:51:106055 has_accessed_initial_document_ = true;
6056
[email protected]d1371bf8e2014-04-19 03:26:246057 // We may have left a failed browser-initiated navigation in the address bar
6058 // to let the user edit it and try again. Clear it now that content might
6059 // show up underneath it.
6060 if (!IsLoading() && controller_.GetPendingEntry())
avi45a72532015-04-07 21:01:456061 controller_.DiscardPendingEntry(false);
[email protected]d1371bf8e2014-04-19 03:26:246062
[email protected]59167c22013-06-03 18:07:326063 // Update the URL display.
thestigc4cac8f2014-09-04 21:17:506064 NotifyNavigationStateChanged(INVALIDATE_TYPE_URL);
[email protected]59167c22013-06-03 18:07:326065}
6066
alexmosbe2f4c32015-03-10 02:30:236067void WebContentsImpl::DidChangeName(RenderFrameHost* render_frame_host,
6068 const std::string& name) {
ericwilligersde386342016-10-19 02:35:096069 for (auto& observer : observers_)
6070 observer.FrameNameChanged(render_frame_host, name);
alexmosbe2f4c32015-03-10 02:30:236071}
6072
John Delaneyedd8d6c2019-01-25 00:23:576073void WebContentsImpl::DidReceiveFirstUserActivation(
6074 RenderFrameHost* render_frame_host) {
6075 for (auto& observer : observers_)
6076 observer.FrameReceivedFirstUserActivation(render_frame_host);
6077}
6078
John Delaney0146a742019-01-25 19:25:286079void WebContentsImpl::DidChangeDisplayState(RenderFrameHost* render_frame_host,
6080 bool is_display_none) {
6081 for (auto& observer : observers_)
6082 observer.FrameDisplayStateChanged(render_frame_host, is_display_none);
6083}
6084
6085void WebContentsImpl::FrameSizeChanged(RenderFrameHost* render_frame_host,
6086 const gfx::Size& frame_size) {
6087 for (auto& observer : observers_)
6088 observer.FrameSizeChanged(render_frame_host, frame_size);
6089}
6090
[email protected]39f2f9252014-05-03 00:54:016091void WebContentsImpl::DocumentOnLoadCompleted(
[email protected]50279492014-05-05 23:24:296092 RenderFrameHost* render_frame_host) {
falken52a56e32016-12-08 05:02:406093 ShowInsecureLocalhostWarningIfNeeded();
6094
Alexander Timindd0fa0c52019-11-06 13:03:376095 GetRenderViewHost()->DocumentOnLoadCompletedInMainFrame();
6096
creis97867aa2017-02-17 18:39:456097 is_notifying_observers_ = true;
ericwilligersde386342016-10-19 02:35:096098 for (auto& observer : observers_)
6099 observer.DocumentOnLoadCompletedInMainFrame();
creis97867aa2017-02-17 18:39:456100 is_notifying_observers_ = false;
[email protected]6578fef2013-10-14 02:35:096101
6102 // TODO(avi): Remove. http://crbug.com/170921
[email protected]8ff00d72012-10-23 19:12:216103 NotificationService::current()->Notify(
6104 NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
6105 Source<WebContents>(this),
[email protected]50279492014-05-05 23:24:296106 NotificationService::NoDetails());
[email protected]25497492010-09-11 15:15:086107}
6108
creis8eb8ef22015-11-04 22:51:386109void WebContentsImpl::UpdateStateForFrame(RenderFrameHost* render_frame_host,
6110 const PageState& page_state) {
creis8eb8ef22015-11-04 22:51:386111 // The state update affects the last NavigationEntry associated with the given
6112 // |render_frame_host|. This may not be the last committed NavigationEntry (as
6113 // in the case of an UpdateState from a frame being swapped out). We track
6114 // which entry this is in the RenderFrameHost's nav_entry_id.
6115 RenderFrameHostImpl* rfhi =
6116 static_cast<RenderFrameHostImpl*>(render_frame_host);
6117 NavigationEntryImpl* entry =
6118 controller_.GetEntryWithUniqueID(rfhi->nav_entry_id());
6119 if (!entry)
6120 return;
6121
6122 FrameNavigationEntry* frame_entry =
6123 entry->GetFrameEntry(rfhi->frame_tree_node());
6124 if (!frame_entry)
6125 return;
6126
creisd06a9422015-11-11 03:08:456127 // The SiteInstance might not match if we do a cross-process navigation with
6128 // replacement (e.g., auto-subframe), in which case the swap out of the old
6129 // RenderFrameHost runs in the background after the old FrameNavigationEntry
6130 // has already been replaced and destroyed.
6131 if (frame_entry->site_instance() != rfhi->GetSiteInstance())
6132 return;
6133
creis8eb8ef22015-11-04 22:51:386134 if (page_state == frame_entry->page_state())
6135 return; // Nothing to update.
6136
creis0cade2e2017-02-28 06:37:476137 DCHECK(page_state.IsValid()) << "Shouldn't set an empty PageState.";
nasko34634672016-07-29 18:46:066138
6139 // The document_sequence_number and item_sequence_number recorded in the
6140 // FrameNavigationEntry should not differ from the one coming with the update,
6141 // since it must come from the same document. Do not update it if a difference
6142 // is detected, as this indicates that |frame_entry| is not the correct one.
6143 ExplodedPageState exploded_state;
6144 if (!DecodePageState(page_state.ToEncodedData(), &exploded_state))
6145 return;
6146
6147 if (exploded_state.top.document_sequence_number !=
6148 frame_entry->document_sequence_number() ||
6149 exploded_state.top.item_sequence_number !=
6150 frame_entry->item_sequence_number()) {
6151 return;
6152 }
6153
nasko04a322f25a2016-09-01 00:00:346154 frame_entry->SetPageState(page_state);
creis8eb8ef22015-11-04 22:51:386155 controller_.NotifyEntryChanged(entry);
6156}
6157
[email protected]ef3adfc2014-05-11 00:04:546158void WebContentsImpl::UpdateTitle(RenderFrameHost* render_frame_host,
[email protected]ef3adfc2014-05-11 00:04:546159 const base::string16& title,
6160 base::i18n::TextDirection title_direction) {
[email protected]ef3adfc2014-05-11 00:04:546161 // Try to find the navigation entry, which might not be the current one.
avi286019ce2015-07-29 01:26:586162 // For example, it might be from a recently swapped out RFH.
avi1edc8552016-10-13 21:37:166163 NavigationEntryImpl* entry = controller_.GetEntryWithUniqueID(
creisad8c31a2016-05-24 17:07:046164 static_cast<RenderFrameHostImpl*>(render_frame_host)->nav_entry_id());
[email protected]ef3adfc2014-05-11 00:04:546165
6166 // We can handle title updates when we don't have an entry in
6167 // UpdateTitleForEntry, but only if the update is from the current RVH.
6168 // TODO(avi): Change to make decisions based on the RenderFrameHost.
avi286019ce2015-07-29 01:26:586169 if (!entry && render_frame_host != GetMainFrame())
[email protected]ef3adfc2014-05-11 00:04:546170 return;
6171
6172 // TODO(evan): make use of title_direction.
6173 // http://code.google.com/p/chromium/issues/detail?id=27094
afakhry6f0c1ec22016-07-14 13:55:136174 UpdateTitleForEntry(entry, title);
[email protected]ef3adfc2014-05-11 00:04:546175}
6176
[email protected]39f2f9252014-05-03 00:54:016177void WebContentsImpl::DocumentAvailableInMainFrame(
6178 RenderViewHost* render_view_host) {
ericwilligersde386342016-10-19 02:35:096179 for (auto& observer : observers_)
6180 observer.DocumentAvailableInMainFrame();
[email protected]39f2f9252014-05-03 00:54:016181}
naskoc0fceff2015-04-30 15:53:526182
[email protected]e1c3a552012-05-04 20:51:326183void WebContentsImpl::RouteCloseEvent(RenderViewHost* rvh) {
6184 // Tell the active RenderViewHost to run unload handlers and close, as long
6185 // as the request came from a RenderViewHost in the same BrowsingInstance.
6186 // In most cases, we receive this from a swapped out RenderViewHost.
6187 // It is possible to receive it from one that has just been swapped in,
6188 // in which case we might as well deliver the message anyway.
6189 if (rvh->GetSiteInstance()->IsRelatedSiteInstance(GetSiteInstance()))
naskoc0fceff2015-04-30 15:53:526190 ClosePage();
[email protected]e1c3a552012-05-04 20:51:326191}
6192
alexmose7da5a12015-04-09 02:22:166193bool WebContentsImpl::ShouldRouteMessageEvent(
6194 RenderFrameHost* target_rfh,
6195 SiteInstance* source_site_instance) const {
6196 // Allow the message if this WebContents is dedicated to a browser plugin
6197 // guest.
[email protected]0eba810b2012-10-18 03:19:366198 // Note: This check means that an embedder could theoretically receive a
6199 // postMessage from anyone (not just its own guests). However, this is
6200 // probably not a risk for apps since other pages won't have references
6201 // to App windows.
alexmose7da5a12015-04-09 02:22:166202 return GetBrowserPluginGuest() || GetBrowserPluginEmbedder();
6203}
[email protected]f546640b2012-05-15 00:03:496204
alexmos58729042015-06-18 23:20:006205void WebContentsImpl::EnsureOpenerProxiesExist(RenderFrameHost* source_rfh) {
alexmose7da5a12015-04-09 02:22:166206 WebContentsImpl* source_web_contents = static_cast<WebContentsImpl*>(
6207 WebContents::FromRenderFrameHost(source_rfh));
[email protected]f546640b2012-05-15 00:03:496208
alexmose7da5a12015-04-09 02:22:166209 if (source_web_contents) {
lazyboy6ec48b2a2015-06-29 15:18:146210 // If this message is going to outer WebContents from inner WebContents,
6211 // then we should not create a RenderView. AttachToOuterWebContentsFrame()
6212 // already created a RenderFrameProxyHost for that purpose.
6213 if (GetBrowserPluginEmbedder() &&
ekaramad1beabecc2016-09-23 20:18:176214 GuestMode::IsCrossProcessFrameGuest(source_web_contents)) {
lazyboy6ec48b2a2015-06-29 15:18:146215 return;
6216 }
6217
lfg812874d2016-02-09 22:26:136218 if (this != source_web_contents && GetBrowserPluginGuest()) {
naskob3041b98a42016-03-12 04:43:066219 // We create a RenderFrameProxyHost for the embedder in the guest's render
6220 // process but we intentionally do not expose the embedder's opener chain
6221 // to it.
6222 source_web_contents->GetRenderManager()->CreateRenderFrameProxy(
6223 GetSiteInstance());
[email protected]f546640b2012-05-15 00:03:496224 } else {
alexmos58729042015-06-18 23:20:006225 RenderFrameHostImpl* source_rfhi =
6226 static_cast<RenderFrameHostImpl*>(source_rfh);
6227 source_rfhi->frame_tree_node()->render_manager()->CreateOpenerProxies(
alexmos90325cf2015-09-02 17:18:396228 GetSiteInstance(), nullptr);
[email protected]f546640b2012-05-15 00:03:496229 }
6230 }
[email protected]f546640b2012-05-15 00:03:496231}
6232
avallee9993fca2016-11-17 06:16:506233void WebContentsImpl::SetAsFocusedWebContentsIfNecessary() {
6234 // Only change focus if we are not currently focused.
6235 WebContentsImpl* old_contents = GetFocusedWebContents();
6236 if (old_contents == this)
6237 return;
6238
paulmeyerfeafc2d2017-04-25 21:46:406239 GetOutermostWebContents()->node_.SetFocusedWebContents(this);
6240
Greg Thompsonfb957602017-09-04 10:25:526241 if (!GuestMode::IsCrossProcessFrameGuest(this) && browser_plugin_guest_)
paulmeyerfeafc2d2017-04-25 21:46:406242 return;
6243
avallee9993fca2016-11-17 06:16:506244 // Send a page level blur to the old contents so that it displays inactive UI
6245 // and focus this contents to activate it.
6246 if (old_contents)
6247 old_contents->GetMainFrame()->GetRenderWidgetHost()->SetPageFocus(false);
avalleec2ff1ef2017-01-21 00:12:306248
6249 // Make sure the outer web contents knows our frame is focused. Otherwise, the
6250 // outer renderer could have the element before or after the frame element
6251 // focused which would return early without actually advancing focus.
avalleed68cdd12017-06-21 22:07:276252 FocusOuterAttachmentFrameChain();
avalleec2ff1ef2017-01-21 00:12:306253
lfg1453e412017-04-11 00:48:506254 if (ShowingInterstitialPage()) {
creisadca9862017-06-16 19:12:116255 static_cast<RenderFrameHostImpl*>(interstitial_page_->GetMainFrame())
lfg1453e412017-04-11 00:48:506256 ->GetRenderWidgetHost()
6257 ->SetPageFocus(true);
6258 } else {
6259 GetMainFrame()->GetRenderWidgetHost()->SetPageFocus(true);
6260 }
avallee9993fca2016-11-17 06:16:506261}
6262
avallee0206f782016-07-28 18:55:336263void WebContentsImpl::SetFocusedFrame(FrameTreeNode* node,
6264 SiteInstance* source) {
avallee0206f782016-07-28 18:55:336265 frame_tree_.SetFocusedFrame(node, source);
avalleed68cdd12017-06-21 22:07:276266
Lucas Furukawa Gadaniabbfa342017-08-29 19:37:576267 if (auto* inner_contents = node_.GetInnerWebContentsInFrame(node)) {
6268 // |this| is an outer WebContents and |node| represents an inner
6269 // WebContents. Transfer the focus to the inner contents if |this| is
6270 // focused.
6271 if (GetFocusedWebContents() == this)
6272 inner_contents->SetAsFocusedWebContentsIfNecessary();
6273 } else if (node_.OuterContentsFrameTreeNode() &&
6274 node_.OuterContentsFrameTreeNode()
6275 ->current_frame_host()
6276 ->GetSiteInstance() == source) {
6277 // |this| is an inner WebContents, |node| is its main FrameTreeNode and
6278 // the outer WebContents FrameTreeNode is at |source|'s SiteInstance.
6279 // Transfer the focus to the inner WebContents if the outer WebContents is
6280 // focused. This branch is used when an inner WebContents is focused through
Lowell Mannersa9530382019-08-01 09:46:576281 // its RenderFrameProxyHost (via FrameFocused mojo call, used to
Lucas Furukawa Gadaniabbfa342017-08-29 19:37:576282 // implement the window.focus() API).
6283 if (GetFocusedWebContents() == GetOuterWebContents())
6284 SetAsFocusedWebContentsIfNecessary();
6285 } else if (!GetOuterWebContents()) {
6286 // This is an outermost WebContents.
6287 SetAsFocusedWebContentsIfNecessary();
6288 } else if (!GuestMode::IsCrossProcessFrameGuest(this) &&
6289 GetOuterWebContents()) {
6290 // TODO(lfg, paulmeyer): Allows BrowserPlugins to set themselves as the
6291 // focused WebContents. This works around a bug in FindRequestManager that
6292 // doesn't support properly traversing BrowserPlugins.
6293 SetAsFocusedWebContentsIfNecessary();
6294 }
avalleed68cdd12017-06-21 22:07:276295}
6296
Avi Drissman36f801f2018-02-01 20:06:046297void WebContentsImpl::DidCallFocus() {
6298 // Any explicit focusing of another window while this WebContents is in
6299 // fullscreen can be used to confuse the user, so drop fullscreen.
Avi Drissmand18c51972018-08-15 18:32:096300 ForSecurityDropFullscreen();
Avi Drissman36f801f2018-02-01 20:06:046301}
6302
avalleed68cdd12017-06-21 22:07:276303RenderFrameHost* WebContentsImpl::GetFocusedFrameIncludingInnerWebContents() {
6304 WebContentsImpl* contents = this;
6305 FrameTreeNode* focused_node = contents->frame_tree_.GetFocusedFrame();
6306
6307 // If there is no focused frame in the outer WebContents, we need to return
6308 // null.
6309 if (!focused_node)
6310 return nullptr;
6311
6312 // If the focused frame is embedding an inner WebContents, we must descend
6313 // into that contents. If the current WebContents does not have a focused
6314 // frame, return the main frame of this contents instead of the focused empty
6315 // frame embedding this contents.
6316 while (true) {
6317 contents = contents->node_.GetInnerWebContentsInFrame(focused_node);
6318 if (!contents)
6319 return focused_node->current_frame_host();
6320
6321 focused_node = contents->frame_tree_.GetFocusedFrame();
6322 if (!focused_node)
6323 return contents->GetMainFrame();
6324 }
avallee0206f782016-07-28 18:55:336325}
6326
Lucas Furukawa Gadaniabbfa342017-08-29 19:37:576327void WebContentsImpl::OnAdvanceFocus(RenderFrameHostImpl* source_rfh) {
6328 // When a RenderFrame needs to advance focus to a RenderFrameProxy (by hitting
6329 // TAB), the RenderFrameProxy sends an IPC to RenderFrameProxyHost. When this
6330 // RenderFrameProxyHost represents an inner WebContents, the outer WebContents
6331 // needs to focus the inner WebContents.
6332 if (GetOuterWebContents() &&
6333 GetOuterWebContents() == source_rfh->delegate()->GetAsWebContents() &&
6334 GetFocusedWebContents() == GetOuterWebContents()) {
6335 SetAsFocusedWebContentsIfNecessary();
6336 }
6337}
6338
ekaramada110f642016-12-21 19:47:286339void WebContentsImpl::OnFocusedElementChangedInFrame(
6340 RenderFrameHostImpl* frame,
6341 const gfx::Rect& bounds_in_root_view) {
6342 RenderWidgetHostViewBase* root_view =
6343 static_cast<RenderWidgetHostViewBase*>(GetRenderWidgetHostView());
6344 if (!root_view || !frame->GetView())
6345 return;
6346
6347 // Converting to screen coordinates.
6348 gfx::Point origin = bounds_in_root_view.origin();
6349 origin += root_view->GetViewBounds().OffsetFromOrigin();
6350 gfx::Rect bounds_in_screen(origin, bounds_in_root_view.size());
6351
6352 root_view->FocusedNodeChanged(frame->has_focused_editable_element(),
6353 bounds_in_screen);
6354
6355 FocusedNodeDetails details = {frame->has_focused_editable_element(),
6356 bounds_in_screen};
6357
6358 // TODO(ekaramad): We should replace this with an observer notification
6359 // (https://crbug.com/675975).
6360 NotificationService::current()->Notify(
6361 NOTIFICATION_FOCUS_CHANGED_IN_PAGE,
6362 Source<RenderViewHost>(GetRenderViewHost()),
6363 Details<FocusedNodeDetails>(&details));
Jeff Fisher709b4c52019-07-29 20:07:346364
6365 for (auto& observer : observers_)
6366 observer.OnFocusChangedInPage(&details);
ekaramada110f642016-12-21 19:47:286367}
6368
Lowell Manners1de5242e2019-04-25 10:18:466369bool WebContentsImpl::DidAddMessageToConsole(
6370 blink::mojom::ConsoleMessageLevel log_level,
6371 const base::string16& message,
6372 int32_t line_no,
6373 const base::string16& source_id) {
[email protected]a796f202012-05-30 14:14:256374 if (!delegate_)
6375 return false;
Lowell Manners1de5242e2019-04-25 10:18:466376 return delegate_->DidAddMessageToConsole(this, log_level, message, line_no,
avia90ae4e2016-11-11 20:49:336377 source_id);
[email protected]a796f202012-05-30 14:14:256378}
6379
Daniel Cheng90196c82018-04-25 21:49:146380void WebContentsImpl::DidReceiveInputEvent(
dominickn9c7ceaa32016-02-27 01:52:536381 RenderWidgetHostImpl* render_widget_host,
6382 const blink::WebInputEvent::Type type) {
Dominick Ng37a15052018-07-17 23:56:356383 if (!IsUserInteractionInputType(type))
Daniel Cheng90196c82018-04-25 21:49:146384 return;
6385
kenrb19221852016-04-29 17:21:406386 // Ignore unless the widget is currently in the frame tree.
6387 if (!HasMatchingWidgetHost(&frame_tree_, render_widget_host))
avic3aa8422015-11-09 20:57:226388 return;
6389
Daniel Cheng90196c82018-04-25 21:49:146390 if (type != blink::WebInputEvent::kGestureScrollBegin)
6391 last_interactive_input_event_time_ = ui::EventTimeForNow();
dominickn9c7ceaa32016-02-27 01:52:536392
Dominick Ng37a15052018-07-17 23:56:356393 for (auto& observer : observers_)
6394 observer.DidGetUserInteraction(type);
dominickn2dd142dd2015-10-29 05:30:506395}
6396
Avi Drissman738ea192018-08-29 20:24:166397bool WebContentsImpl::ShouldIgnoreInputEvents() {
6398 WebContentsImpl* web_contents = this;
6399 while (web_contents) {
6400 if (web_contents->ignore_input_events_)
6401 return true;
6402 web_contents = web_contents->GetOuterWebContents();
6403 }
6404
6405 return false;
6406}
6407
avallee9993fca2016-11-17 06:16:506408void WebContentsImpl::FocusOwningWebContents(
6409 RenderWidgetHostImpl* render_widget_host) {
6410 // The PDF plugin still runs as a BrowserPlugin and must go through the
6411 // input redirection mechanism. It must not become focused direcly.
6412 if (!GuestMode::IsCrossProcessFrameGuest(this) && browser_plugin_guest_)
6413 return;
6414
6415 RenderWidgetHostImpl* focused_widget =
6416 GetFocusedRenderWidgetHost(render_widget_host);
6417
6418 if (focused_widget != render_widget_host &&
avallee8e881592016-11-19 00:39:306419 (!focused_widget ||
6420 focused_widget->delegate() != render_widget_host->delegate())) {
avallee9993fca2016-11-17 06:16:506421 SetAsFocusedWebContentsIfNecessary();
6422 }
6423}
6424
[email protected]b172aee2012-04-10 17:05:266425void WebContentsImpl::OnIgnoredUIEvent() {
[email protected]31145092011-09-30 01:49:446426 // Notify observers.
ericwilligersde386342016-10-19 02:35:096427 for (auto& observer : observers_)
6428 observer.DidGetIgnoredUIEvent();
[email protected]fa1cf0b82010-01-15 21:49:446429}
6430
avic3aa8422015-11-09 20:57:226431void WebContentsImpl::RendererUnresponsive(
Lukasz Anforowicz52b93722018-06-20 16:11:396432 RenderWidgetHostImpl* render_widget_host,
6433 base::RepeatingClosure hang_monitor_restarter) {
avic031d392017-03-03 03:09:426434 if (ShouldIgnoreUnresponsiveRenderer())
[email protected]e5fc1632011-08-08 07:51:536435 return;
6436
Lukasz Anforowicz4f0593d2018-09-21 18:33:546437 // Do not report hangs (to task manager, to hang renderer dialog, etc.) for
6438 // invisible tabs (like extension background page, background tabs). See
6439 // https://crbug.com/881812 for rationale and for choosing the visibility
6440 // (rather than process priority) as the signal here.
6441 if (GetVisibility() != Visibility::VISIBLE)
6442 return;
6443
Avi Drissman8920def2018-01-31 19:53:366444 if (!render_widget_host->renderer_initialized())
[email protected]55452902011-06-01 21:57:476445 return;
6446
Lukasz Anforowicz4f0593d2018-09-21 18:33:546447 for (auto& observer : observers_)
6448 observer.OnRendererUnresponsive(render_widget_host->GetProcess());
6449
Dave Tapuskac6146c52017-12-20 22:48:156450 if (delegate_)
Lukasz Anforowicz52b93722018-06-20 16:11:396451 delegate_->RendererUnresponsive(this, render_widget_host,
6452 std::move(hang_monitor_restarter));
[email protected]420ae012009-04-24 05:16:326453}
6454
avic3aa8422015-11-09 20:57:226455void WebContentsImpl::RendererResponsive(
6456 RenderWidgetHostImpl* render_widget_host) {
[email protected]6934a702011-12-20 00:04:516457 if (delegate_)
Avi Drissman1572e8c3c2018-02-02 19:06:366458 delegate_->RendererResponsive(this, render_widget_host);
[email protected]420ae012009-04-24 05:16:326459}
6460
Alex Moshchuk7b4f0652019-05-30 18:54:416461void WebContentsImpl::SubframeCrashed(
6462 blink::mojom::FrameVisibility visibility) {
6463 // If a subframe crashed on a hidden tab, mark the tab for reload to avoid
6464 // showing a sad frame to the user if they ever switch back to that tab. Do
6465 // this for subframes that are either visible in viewport or visible but
6466 // scrolled out of view, but skip subframes that are not rendered (e.g., via
6467 // "display:none"), since in that case the user wouldn't see a sad frame
6468 // anyway.
6469 bool did_mark_for_reload = false;
6470 if (IsHidden() && visibility != blink::mojom::FrameVisibility::kNotRendered &&
6471 base::FeatureList::IsEnabled(
6472 features::kReloadHiddenTabsWithCrashedSubframes)) {
6473 controller_.SetNeedsReload(
6474 NavigationControllerImpl::NeedsReloadType::kCrashedSubframe);
6475 did_mark_for_reload = true;
6476 UMA_HISTOGRAM_ENUMERATION(
6477 "Stability.ChildFrameCrash.TabMarkedForReload.Visibility", visibility);
6478 }
6479
6480 UMA_HISTOGRAM_BOOLEAN("Stability.ChildFrameCrash.TabMarkedForReload",
6481 did_mark_for_reload);
6482}
6483
[email protected]b172aee2012-04-10 17:05:266484void WebContentsImpl::BeforeUnloadFiredFromRenderManager(
[email protected]feaded02012-09-25 19:12:556485 bool proceed, const base::TimeTicks& proceed_time,
[email protected]420ae012009-04-24 05:16:326486 bool* proceed_to_fire_unload) {
ericwilligersde386342016-10-19 02:35:096487 for (auto& observer : observers_)
Chris Hamilton3b3e315d2018-09-19 13:16:216488 observer.BeforeUnloadFired(proceed, proceed_time);
[email protected]6934a702011-12-20 00:04:516489 if (delegate_)
6490 delegate_->BeforeUnloadFired(this, proceed, proceed_to_fire_unload);
[email protected]e80af492013-06-24 21:52:096491 // Note: |this| might be deleted at this point.
[email protected]420ae012009-04-24 05:16:326492}
6493
[email protected]58d5cfe2013-07-10 02:40:526494void WebContentsImpl::RenderProcessGoneFromRenderManager(
[email protected]3a3d47472010-07-15 21:03:546495 RenderViewHost* render_view_host) {
[email protected]443b80e2010-12-14 00:42:236496 DCHECK(crashed_status_ != base::TERMINATION_STATUS_STILL_RUNNING);
[email protected]ec6a7eb2013-04-22 17:34:226497 RenderViewTerminated(render_view_host, crashed_status_, crashed_error_code_);
[email protected]3a3d47472010-07-15 21:03:546498}
6499
Jianzhou Fengd8720c72018-01-18 03:06:306500void WebContentsImpl::UpdateRenderViewSizeForRenderManager(bool is_main_frame) {
[email protected]8643e6d2012-01-18 20:26:106501 // TODO(brettw) this is a hack. See WebContentsView::SizeContents.
Jianzhou Fengd8720c72018-01-18 03:06:306502 gfx::Size size = GetSizeForNewRenderView(is_main_frame);
[email protected]0323ee42010-02-17 22:03:266503 // 0x0 isn't a valid window size (minimal window size is 1x1) but it may be
6504 // here during container initialization and normal window size will be set
6505 // later. In case of tab duplication this resizing to 0x0 prevents setting
6506 // normal size later so just ignore it.
6507 if (!size.IsEmpty())
6508 view_->SizeContents(size);
[email protected]420ae012009-04-24 05:16:326509}
6510
[email protected]af905902013-10-01 21:38:516511void WebContentsImpl::CancelModalDialogsForRenderManager() {
6512 // We need to cancel modal dialogs when doing a process swap, since the load
avi2460c762015-04-17 15:21:546513 // deferrer would prevent us from swapping out. We also clear the state
6514 // because this is a cross-process navigation, which means that it's a new
6515 // site that should not have to pay for the sins of its predecessor.
creis89a0f782015-05-27 16:13:176516 //
Lei Zhang6410ec1a2019-01-31 21:38:586517 // Note that we don't bother telling |browser_plugin_embedder_| because the
creis89a0f782015-05-27 16:13:176518 // cross-process navigation will either destroy the browser plugins or not
6519 // require their dialogs to close.
avi5d3b8692016-10-12 22:00:466520 if (dialog_manager_) {
avi6879fcf2017-01-21 05:27:536521 dialog_manager_->CancelDialogs(this, /*reset_state=*/true);
avi5d3b8692016-10-12 22:00:466522 }
[email protected]af905902013-10-01 21:38:516523}
6524
[email protected]02d7b6e2014-06-24 21:01:506525void WebContentsImpl::NotifySwappedFromRenderManager(RenderFrameHost* old_host,
6526 RenderFrameHost* new_host,
6527 bool is_main_frame) {
6528 if (is_main_frame) {
arthursonzognif97bca512019-10-10 11:32:586529 NotifyViewSwapped(old_host ? old_host->GetRenderViewHost() : nullptr,
6530 new_host->GetRenderViewHost());
[email protected]a6b73c62013-02-11 23:05:086531
[email protected]02d7b6e2014-06-24 21:01:506532 // Make sure the visible RVH reflects the new delegate's preferences.
6533 if (delegate_)
6534 view_->SetOverscrollControllerEnabled(CanOverscrollContent());
[email protected]d56a49e2013-02-23 00:47:396535
dmazzoni62111452017-04-28 23:46:556536 RenderWidgetHostViewBase* rwhv =
6537 static_cast<RenderWidgetHostViewBase*>(GetRenderWidgetHostView());
6538 if (rwhv)
6539 rwhv->SetMainFrameAXTreeID(GetMainFrame()->GetAXTreeID());
[email protected]02d7b6e2014-06-24 21:01:506540 }
6541
Jinsuk Kim895b2c52018-07-03 06:49:166542 NotifyFrameSwapped(old_host, new_host, is_main_frame);
[email protected]3a3d47472010-07-15 21:03:546543}
6544
naskof5940b9f2015-03-02 23:04:056545void WebContentsImpl::NotifyMainFrameSwappedFromRenderManager(
Jinsuk Kim895b2c52018-07-03 06:49:166546 RenderFrameHost* old_host,
6547 RenderFrameHost* new_host) {
6548 NotifyViewSwapped(old_host ? old_host->GetRenderViewHost() : nullptr,
6549 new_host->GetRenderViewHost());
naskof5940b9f2015-03-02 23:04:056550}
6551
[email protected]b172aee2012-04-10 17:05:266552NavigationControllerImpl& WebContentsImpl::GetControllerForRenderManager() {
[email protected]d1198fd2012-08-13 22:50:196553 return GetController();
[email protected]3a3d47472010-07-15 21:03:546554}
6555
dcheng59716272016-04-09 05:19:086556std::unique_ptr<WebUIImpl> WebContentsImpl::CreateWebUIForRenderFrameHost(
carlosk37f793792014-11-27 15:08:296557 const GURL& url) {
Dan Beam055fe8f2017-10-04 23:23:366558 return CreateWebUI(url);
[email protected]420ae012009-04-24 05:16:326559}
6560
creisadca9862017-06-16 19:12:116561InterstitialPageImpl* WebContentsImpl::GetInterstitialForRenderManager() {
6562 return interstitial_page_;
6563}
6564
lfgbede6392015-09-11 21:54:066565void WebContentsImpl::CreateRenderWidgetHostViewForRenderManager(
6566 RenderViewHost* render_view_host) {
lfgc740d4b22016-04-15 16:45:336567 RenderWidgetHostViewBase* rwh_view =
6568 view_->CreateViewForWidget(render_view_host->GetWidget(), false);
danakjf616f2252019-09-18 21:44:096569 rwh_view->SetSize(GetSizeForNewRenderView(true));
lfgbede6392015-09-11 21:54:066570}
6571
6572bool WebContentsImpl::CreateRenderViewForRenderManager(
6573 RenderViewHost* render_view_host,
6574 int opener_frame_routing_id,
6575 int proxy_routing_id,
Pavel Feldman25234722017-10-11 02:49:066576 const base::UnguessableToken& devtools_frame_token,
lfgbede6392015-09-11 21:54:066577 const FrameReplicationState& replicated_frame_state) {
6578 TRACE_EVENT0("browser,navigation",
6579 "WebContentsImpl::CreateRenderViewForRenderManager");
6580
piman00cf9402017-04-03 17:09:206581 if (proxy_routing_id == MSG_ROUTING_NONE)
lfg9431efd2016-12-14 14:45:266582 CreateRenderWidgetHostViewForRenderManager(render_view_host);
lfg9431efd2016-12-14 14:45:266583
piman00cf9402017-04-03 17:09:206584 if (!static_cast<RenderViewHostImpl*>(render_view_host)
6585 ->CreateRenderView(opener_frame_routing_id, proxy_routing_id,
Pavel Feldman25234722017-10-11 02:49:066586 devtools_frame_token, replicated_frame_state,
6587 created_with_opener_)) {
[email protected]a4127722011-04-27 23:13:526588 return false;
[email protected]14392a52012-05-02 20:28:446589 }
W. James MacLean62198672019-06-04 16:46:106590 // Set the TextAutosizer state from the main frame's renderer on the new view,
6591 // but only if it's not for the main frame. Main frame renderers should create
6592 // this state themselves from up-to-date values, so we shouldn't override it
6593 // with the cached values.
6594 if (!render_view_host->GetMainFrame()) {
6595 render_view_host->Send(
6596 new PageMsg_UpdateTextAutosizerPageInfoForRemoteMainFrames(
6597 render_view_host->GetRoutingID(), text_autosizer_page_info_));
6598 }
[email protected]a4127722011-04-27 23:13:526599
Lucas Furukawa Gadaniaed1fed2017-10-13 17:34:146600 if (proxy_routing_id == MSG_ROUTING_NONE && node_.outer_web_contents())
6601 ReattachToOuterWebContentsFrame();
6602
avi2b177592014-12-10 02:08:026603 SetHistoryOffsetAndLengthForView(render_view_host,
6604 controller_.GetLastCommittedEntryIndex(),
6605 controller_.GetEntryCount());
6606
[email protected]a220b5932013-09-21 03:47:446607#if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID)
[email protected]f8233cc2011-05-31 20:24:506608 // Force a ViewMsg_Resize to be sent, needed to make plugins show up on
6609 // linux. See crbug.com/83941.
avif9ab5d942015-10-15 14:05:446610 RenderWidgetHostView* rwh_view = render_view_host->GetWidget()->GetView();
[email protected]245f7d52011-11-28 15:36:446611 if (rwh_view) {
6612 if (RenderWidgetHost* render_widget_host = rwh_view->GetRenderWidgetHost())
Fady Samuel0b911822018-04-25 13:22:166613 render_widget_host->SynchronizeVisualProperties();
[email protected]245f7d52011-11-28 15:36:446614 }
[email protected]f8233cc2011-05-31 20:24:506615#endif
6616
[email protected]420ae012009-04-24 05:16:326617 return true;
6618}
6619
[email protected]82307f6b2014-08-07 03:30:126620bool WebContentsImpl::CreateRenderFrameForRenderManager(
6621 RenderFrameHost* render_frame_host,
arthursonzogni1efc7b962019-03-14 09:36:336622 int previous_routing_id,
alexmosa181efc2015-09-03 00:39:046623 int opener_routing_id,
japhete6adf142014-10-31 00:01:496624 int parent_routing_id,
alexmosa181efc2015-09-03 00:39:046625 int previous_sibling_routing_id) {
naskob8744d22014-08-28 17:07:436626 TRACE_EVENT0("browser,navigation",
6627 "WebContentsImpl::CreateRenderFrameForRenderManager");
[email protected]82307f6b2014-08-07 03:30:126628
6629 RenderFrameHostImpl* rfh =
6630 static_cast<RenderFrameHostImpl*>(render_frame_host);
arthursonzogni1efc7b962019-03-14 09:36:336631 if (!rfh->CreateRenderFrame(previous_routing_id, opener_routing_id,
alexmosa181efc2015-09-03 00:39:046632 parent_routing_id, previous_sibling_routing_id))
[email protected]82307f6b2014-08-07 03:30:126633 return false;
6634
6635 // TODO(nasko): When RenderWidgetHost is owned by RenderFrameHost, the passed
6636 // RenderFrameHost will have to be associated with the appropriate
6637 // RenderWidgetHostView or a new one should be created here.
6638
6639 return true;
6640}
6641
mfomitchev2b8b066a2016-01-28 19:23:156642#if defined(OS_ANDROID)
[email protected]fc2b46b2014-05-03 16:33:456643
[email protected]155c7f22013-12-09 17:07:186644base::android::ScopedJavaLocalRef<jobject>
6645WebContentsImpl::GetJavaWebContents() {
mostynb042582e2015-03-16 22:13:406646 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]0e813a52014-08-13 10:34:566647 return GetWebContentsAndroid()->GetJavaObject();
6648}
[email protected]155c7f22013-12-09 17:07:186649
[email protected]0e813a52014-08-13 10:34:566650WebContentsAndroid* WebContentsImpl::GetWebContentsAndroid() {
Kevin McNee576bb5d82019-05-29 16:47:516651 if (!web_contents_android_) {
6652 web_contents_android_ = std::make_unique<WebContentsAndroid>(this);
[email protected]155c7f22013-12-09 17:07:186653 }
Kevin McNee576bb5d82019-05-29 16:47:516654 return web_contents_android_.get();
6655}
6656
6657void WebContentsImpl::ClearWebContentsAndroid() {
6658 DCHECK_CURRENTLY_ON(BrowserThread::UI);
6659 web_contents_android_.reset();
[email protected]155c7f22013-12-09 17:07:186660}
6661
paulmeyerc0b762b2016-04-13 11:55:176662void WebContentsImpl::ActivateNearestFindResult(float x,
6663 float y) {
6664 GetOrCreateFindRequestManager()->ActivateNearestFindResult(x, y);
6665}
6666
6667void WebContentsImpl::RequestFindMatchRects(int current_version) {
6668 GetOrCreateFindRequestManager()->RequestFindMatchRects(current_version);
6669}
6670
[email protected]7d244f12013-08-22 21:41:516671bool WebContentsImpl::CreateRenderViewForInitialEmptyDocument() {
nasko4c0feb62015-06-05 18:37:066672 return CreateRenderViewForRenderManager(
6673 GetRenderViewHost(), MSG_ROUTING_NONE, MSG_ROUTING_NONE,
Pavel Feldman25234722017-10-11 02:49:066674 frame_tree_.root()->devtools_frame_token(),
lfgbede6392015-09-11 21:54:066675 frame_tree_.root()->current_replication_state());
[email protected]7d244f12013-08-22 21:41:516676}
[email protected]fc2b46b2014-05-03 16:33:456677
rockot400ea35b2016-10-15 19:15:326678service_manager::InterfaceProvider* WebContentsImpl::GetJavaInterfaces() {
sammcf5f1b0f2016-09-20 23:05:116679 if (!java_interfaces_) {
rockot400ea35b2016-10-15 19:15:326680 service_manager::mojom::InterfaceProviderPtr provider;
blundelle0a9f1582016-12-20 11:23:326681 BindInterfaceRegistryForWebContents(mojo::MakeRequest(&provider), this);
rockot400ea35b2016-10-15 19:15:326682 java_interfaces_.reset(new service_manager::InterfaceProvider);
sammcf5f1b0f2016-09-20 23:05:116683 java_interfaces_->Bind(std::move(provider));
6684 }
6685 return java_interfaces_.get();
6686}
6687
Francois Doray6c26f3342018-08-24 17:17:226688#endif
6689
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:416690bool WebContentsImpl::CompletedFirstVisuallyNonEmptyPaint() {
Carlos Caballero91fffb22019-10-29 15:24:306691 return GetRenderViewHost()->did_first_visually_non_empty_paint();
spqchane72c3cb2017-06-30 23:02:106692}
6693
pkotwicz75ca8ffd2016-02-16 23:10:196694void WebContentsImpl::OnDidDownloadImage(
Avi Drissmana5a52dd2018-03-27 03:39:026695 ImageDownloadCallback callback,
pkotwicz75ca8ffd2016-02-16 23:10:196696 int id,
6697 const GURL& image_url,
leon.hancbc4bc3c2016-02-26 07:08:526698 int32_t http_status_code,
leon.hanc2228532016-08-16 05:59:186699 const std::vector<SkBitmap>& images,
6700 const std::vector<gfx::Size>& original_image_sizes) {
Avi Drissmana5a52dd2018-03-27 03:39:026701 std::move(callback).Run(id, http_status_code, image_url, images,
6702 original_image_sizes);
pkotwicz75ca8ffd2016-02-16 23:10:196703}
6704
[email protected]6fba26d2014-04-29 09:38:286705void WebContentsImpl::OnDialogClosed(int render_process_id,
6706 int render_frame_id,
[email protected]b172aee2012-04-10 17:05:266707 IPC::Message* reply_msg,
[email protected]87de04b02014-04-08 22:14:496708 bool dialog_was_suppressed,
[email protected]b172aee2012-04-10 17:05:266709 bool success,
[email protected]fcf75d42013-12-03 20:11:266710 const base::string16& user_input) {
[email protected]6fba26d2014-04-29 09:38:286711 RenderFrameHostImpl* rfh = RenderFrameHostImpl::FromID(render_process_id,
6712 render_frame_id);
[email protected]87de04b02014-04-08 22:14:496713 last_dialog_suppressed_ = dialog_was_suppressed;
6714
Charlie Reis96077362019-07-04 00:34:046715 javascript_dialog_navigation_deferrer_.reset();
6716
[email protected]beb440c2009-11-06 04:08:546717 if (is_showing_before_unload_dialog_ && !success) {
clamybc6f4152016-04-01 20:14:236718 // It is possible for the current RenderFrameHost to have changed in the
6719 // meantime. Do not reset the navigation state in that case.
6720 if (rfh && rfh == rfh->frame_tree_node()->current_frame_host()) {
clamy44e84ce2016-02-22 15:38:256721 rfh->frame_tree_node()->BeforeUnloadCanceled();
clamybc6f4152016-04-01 20:14:236722 controller_.DiscardNonCommittedEntries();
6723 }
[email protected]ec8e8b02013-06-19 04:57:106724
Charles Reis15d60b132017-11-03 17:43:476725 // Update the URL display either way, to avoid showing a stale URL.
6726 NotifyNavigationStateChanged(INVALIDATE_TYPE_URL);
6727
ericwilligersde386342016-10-19 02:35:096728 for (auto& observer : observers_)
6729 observer.BeforeUnloadDialogCancelled();
[email protected]beb440c2009-11-06 04:08:546730 }
[email protected]87de04b02014-04-08 22:14:496731
[email protected]6fba26d2014-04-29 09:38:286732 if (rfh) {
avi44acfaa82017-05-03 22:55:356733 rfh->JavaScriptDialogClosed(reply_msg, success, user_input);
Pavel Feldman3c1842b2017-08-02 05:00:166734
6735 std::vector<protocol::PageHandler*> page_handlers =
6736 protocol::PageHandler::EnabledForWebContents(this);
6737 for (auto* handler : page_handlers)
6738 handler->DidCloseJavaScriptDialog(success, user_input);
6739
[email protected]6fba26d2014-04-29 09:38:286740 } else {
6741 // Don't leak the sync IPC reply if the RFH or process is gone.
6742 delete reply_msg;
6743 }
avi1c51fb62017-02-27 20:43:036744
6745 is_showing_javascript_dialog_ = false;
6746 is_showing_before_unload_dialog_ = false;
[email protected]beb440c2009-11-06 04:08:546747}
6748
[email protected]1ac10dca2013-08-20 20:47:046749bool WebContentsImpl::IsHidden() {
Francois Doraye6161152018-03-27 22:05:376750 return !IsBeingCaptured() && visibility_ != Visibility::VISIBLE;
[email protected]1ac10dca2013-08-20 20:47:046751}
6752
rob97250742015-12-10 17:45:156753int WebContentsImpl::GetOuterDelegateFrameTreeNodeId() {
lfg601233692017-03-06 22:45:446754 return node_.outer_contents_frame_tree_node_id();
lazyboy6ec48b2a2015-06-29 15:18:146755}
6756
ekaramadaeb3c5c2016-11-09 03:22:356757RenderWidgetHostImpl* WebContentsImpl::GetFullscreenRenderWidgetHost() const {
6758 return RenderWidgetHostImpl::FromID(fullscreen_widget_process_id_,
6759 fullscreen_widget_routing_id_);
6760}
6761
[email protected]b0936d22013-11-28 06:47:366762RenderFrameHostManager* WebContentsImpl::GetRenderManager() const {
[email protected]fa944cb82013-11-15 17:51:216763 return frame_tree_.root()->render_manager();
6764}
6765
[email protected]f8497342013-02-05 22:15:026766BrowserPluginGuest* WebContentsImpl::GetBrowserPluginGuest() const {
[email protected]7a846df2012-09-20 19:17:396767 return browser_plugin_guest_.get();
6768}
6769
Albert J. Wongc55cc64d2018-10-12 02:50:046770void WebContentsImpl::SetBrowserPluginGuest(
6771 std::unique_ptr<BrowserPluginGuest> guest) {
6772 DCHECK(!browser_plugin_guest_);
6773 DCHECK(guest);
6774 browser_plugin_guest_ = std::move(guest);
[email protected]738f57a2013-06-29 21:06:546775}
6776
Aidan Wolter52bcc12a2018-11-14 03:27:296777base::UnguessableToken WebContentsImpl::GetAudioGroupId() {
6778 if (!base::FeatureList::IsEnabled(features::kAudioServiceAudioStreams))
6779 return base::UnguessableToken::Null();
6780
6781 return GetAudioStreamFactory()->group_id();
6782}
6783
Sofiya Semenova5e9d9122019-07-11 02:46:416784ukm::SourceId WebContentsImpl::GetLastCommittedSourceId() {
6785 return last_committed_source_id_;
6786}
6787
[email protected]f8497342013-02-05 22:15:026788BrowserPluginEmbedder* WebContentsImpl::GetBrowserPluginEmbedder() const {
[email protected]7a846df2012-09-20 19:17:396789 return browser_plugin_embedder_.get();
6790}
[email protected]8ff00d72012-10-23 19:12:216791
fsamuel60b42282015-03-10 03:29:146792void WebContentsImpl::CreateBrowserPluginEmbedderIfNecessary() {
6793 if (browser_plugin_embedder_)
6794 return;
6795 browser_plugin_embedder_.reset(BrowserPluginEmbedder::Create(this));
6796}
6797
Jianzhou Fengd8720c72018-01-18 03:06:306798gfx::Size WebContentsImpl::GetSizeForNewRenderView(bool is_main_frame) {
[email protected]dc0fd432013-08-27 15:29:216799 gfx::Size size;
Jianzhou Fengd8720c72018-01-18 03:06:306800 if (is_main_frame)
6801 size = device_emulation_size_;
6802 if (size.IsEmpty() && delegate_)
[email protected]dc0fd432013-08-27 15:29:216803 size = delegate_->GetSizeForNewRenderView(this);
6804 if (size.IsEmpty())
[email protected]fc2b46b2014-05-03 16:33:456805 size = GetContainerBounds().size();
[email protected]dc0fd432013-08-27 15:29:216806 return size;
6807}
6808
[email protected]ae7eeda2014-07-04 01:53:266809void WebContentsImpl::OnFrameRemoved(RenderFrameHost* render_frame_host) {
yilkal796e89e2019-10-02 23:58:286810 frame_count_--;
6811
ericwilligersde386342016-10-19 02:35:096812 for (auto& observer : observers_)
6813 observer.FrameDeleted(render_frame_host);
[email protected]9b159a52013-10-03 17:24:556814}
6815
[email protected]222f5822014-02-05 23:40:496816void WebContentsImpl::OnPreferredSizeChanged(const gfx::Size& old_size) {
6817 if (!delegate_)
6818 return;
6819 const gfx::Size new_size = GetPreferredSize();
6820 if (new_size != old_size)
6821 delegate_->UpdatePreferredSize(this, new_size);
6822}
6823
Dan Beam055fe8f2017-10-04 23:23:366824std::unique_ptr<WebUIImpl> WebContentsImpl::CreateWebUI(const GURL& url) {
Jeremy Roman04f27c372017-10-27 15:20:556825 std::unique_ptr<WebUIImpl> web_ui = std::make_unique<WebUIImpl>(this);
Lei Zhangee3b63ad2018-06-06 23:25:316826 std::unique_ptr<WebUIController> controller(
avie865b1d2016-10-24 19:42:596827 WebUIControllerFactoryRegistry::GetInstance()
Lei Zhangee3b63ad2018-06-06 23:25:316828 ->CreateWebUIControllerForURL(web_ui.get(), url));
dbeama1b926a2015-08-31 23:17:516829 if (controller) {
Lei Zhangee3b63ad2018-06-06 23:25:316830 web_ui->SetController(std::move(controller));
dbeama1b926a2015-08-31 23:17:516831 return web_ui;
6832 }
6833
avie865b1d2016-10-24 19:42:596834 return nullptr;
dbeama1b926a2015-08-31 23:17:516835}
6836
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:416837FindRequestManager* WebContentsImpl::GetFindRequestManager() {
6838 for (auto* contents = this; contents;
paulmeyerfeafc2d2017-04-25 21:46:406839 contents = contents->GetOuterWebContents()) {
6840 if (contents->find_request_manager_)
6841 return contents->find_request_manager_.get();
6842 }
6843
6844 return nullptr;
6845}
6846
paulmeyerc0b762b2016-04-13 11:55:176847FindRequestManager* WebContentsImpl::GetOrCreateFindRequestManager() {
paulmeyerfeafc2d2017-04-25 21:46:406848 if (FindRequestManager* manager = GetFindRequestManager())
6849 return manager;
6850
Ehsan Karamad6beb2ea2018-11-25 18:15:136851 DCHECK(!browser_plugin_guest_ || GetOuterWebContents());
6852
paulmeyerfeafc2d2017-04-25 21:46:406853 // No existing FindRequestManager found, so one must be created.
6854 find_request_manager_.reset(new FindRequestManager(this));
6855
6856 // Concurrent find sessions must not overlap, so destroy any existing
6857 // FindRequestManagers in any inner WebContentses.
Lucas Furukawa Gadani2ec00c82018-12-14 15:53:166858 for (WebContents* contents : GetWebContentsAndAllInner()) {
6859 auto* web_contents_impl = static_cast<WebContentsImpl*>(contents);
6860 if (web_contents_impl == this)
paulmeyerfeafc2d2017-04-25 21:46:406861 continue;
Lucas Furukawa Gadani2ec00c82018-12-14 15:53:166862 if (web_contents_impl->find_request_manager_) {
6863 web_contents_impl->find_request_manager_->StopFinding(
6864 STOP_FIND_ACTION_CLEAR_SELECTION);
6865 web_contents_impl->find_request_manager_.release();
paulmeyerfeafc2d2017-04-25 21:46:406866 }
6867 }
paulmeyerc0b762b2016-04-13 11:55:176868
6869 return find_request_manager_.get();
6870}
6871
6872void WebContentsImpl::NotifyFindReply(int request_id,
6873 int number_of_matches,
6874 const gfx::Rect& selection_rect,
6875 int active_match_ordinal,
6876 bool final_update) {
Sebastien Marchandcf97ed52017-07-24 23:50:346877 if (delegate_ && !is_being_destroyed_ &&
Lukasz Anforowicze9ae3722017-09-29 17:37:386878 !GetMainFrame()->GetProcess()->FastShutdownStarted()) {
paulmeyerc0b762b2016-04-13 11:55:176879 delegate_->FindReply(this, request_id, number_of_matches, selection_rect,
6880 active_match_ordinal, final_update);
6881 }
6882}
6883
ortuno467e5792016-06-10 04:32:396884void WebContentsImpl::IncrementBluetoothConnectedDeviceCount() {
6885 // Trying to invalidate the tab state while being destroyed could result in a
6886 // use after free.
6887 if (IsBeingDestroyed()) {
6888 return;
6889 }
6890 // Notify for UI updates if the state changes.
6891 bluetooth_connected_device_count_++;
6892 if (bluetooth_connected_device_count_ == 1) {
6893 NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB);
6894 }
6895}
6896
6897void WebContentsImpl::DecrementBluetoothConnectedDeviceCount() {
6898 // Trying to invalidate the tab state while being destroyed could result in a
6899 // use after free.
6900 if (IsBeingDestroyed()) {
6901 return;
6902 }
6903 // Notify for UI updates if the state changes.
Lukasz Anforowicze9ae3722017-09-29 17:37:386904 DCHECK_NE(bluetooth_connected_device_count_, 0u);
ortuno467e5792016-06-10 04:32:396905 bluetooth_connected_device_count_--;
6906 if (bluetooth_connected_device_count_ == 0) {
6907 NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB);
6908 }
6909}
6910
Reilly Grant5e7c79b22019-04-09 17:26:206911void WebContentsImpl::IncrementSerialActiveFrameCount() {
6912 // Trying to invalidate the tab state while being destroyed could result in a
6913 // use after free.
6914 if (IsBeingDestroyed())
6915 return;
6916
6917 // Notify for UI updates if the state changes.
6918 serial_active_frame_count_++;
6919 if (serial_active_frame_count_ == 1)
6920 NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB);
6921}
6922
6923void WebContentsImpl::DecrementSerialActiveFrameCount() {
6924 // Trying to invalidate the tab state while being destroyed could result in a
6925 // use after free.
6926 if (IsBeingDestroyed())
6927 return;
6928
6929 // Notify for UI updates if the state changes.
6930 DCHECK_NE(0u, serial_active_frame_count_);
6931 serial_active_frame_count_--;
6932 if (serial_active_frame_count_ == 0)
6933 NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB);
6934}
6935
Matt Reynoldse8c6c1f2019-11-02 09:53:536936void WebContentsImpl::IncrementHidActiveFrameCount() {
6937 // Trying to invalidate the tab state while being destroyed could result in a
6938 // use after free.
6939 if (IsBeingDestroyed())
6940 return;
6941
6942 // Notify for UI updates if the active frame count transitions from zero to
6943 // non-zero.
6944 hid_active_frame_count_++;
6945 if (hid_active_frame_count_ == 1)
6946 NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB);
6947}
6948
6949void WebContentsImpl::DecrementHidActiveFrameCount() {
6950 // Trying to invalidate the tab state while being destroyed could result in a
6951 // use after free.
6952 if (IsBeingDestroyed())
6953 return;
6954
6955 // Notify for UI updates if the active frame count transitions from non-zero
6956 // to zero.
6957 DCHECK_NE(0u, hid_active_frame_count_);
6958 hid_active_frame_count_--;
6959 if (hid_active_frame_count_ == 0)
6960 NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB);
6961}
6962
Marijn Kruisselbrink29051042019-08-06 22:56:556963void WebContentsImpl::IncrementNativeFileSystemHandleCount() {
6964 // Trying to invalidate the tab state while being destroyed could result in a
6965 // use after free.
6966 if (IsBeingDestroyed())
6967 return;
6968
6969 // Notify for UI updates if the state changes. Need both TYPE_TAB and TYPE_URL
6970 // to update both the tab-level usage indicator and the usage indicator in the
6971 // omnibox.
6972 native_file_system_handle_count_++;
6973 if (native_file_system_handle_count_ == 1) {
6974 NotifyNavigationStateChanged(static_cast<content::InvalidateTypes>(
6975 INVALIDATE_TYPE_TAB | INVALIDATE_TYPE_URL));
6976 }
6977}
6978
6979void WebContentsImpl::DecrementNativeFileSystemHandleCount() {
6980 // Trying to invalidate the tab state while being destroyed could result in a
6981 // use after free.
6982 if (IsBeingDestroyed())
6983 return;
6984
6985 // Notify for UI updates if the state changes. Need both TYPE_TAB and TYPE_URL
6986 // to update both the tab-level usage indicator and the usage indicator in the
6987 // omnibox.
6988 DCHECK_NE(0u, native_file_system_handle_count_);
6989 native_file_system_handle_count_--;
6990 if (native_file_system_handle_count_ == 0) {
6991 NotifyNavigationStateChanged(static_cast<content::InvalidateTypes>(
6992 INVALIDATE_TYPE_TAB | INVALIDATE_TYPE_URL));
6993 }
6994}
6995
Marijn Kruisselbrink46fa4bd2019-07-11 18:15:476996void WebContentsImpl::AddNativeFileSystemDirectoryHandle(
6997 const base::FilePath& path) {
Marijn Kruisselbrinkf1714aa22019-07-02 03:45:336998 // Trying to invalidate the tab state while being destroyed could result in a
6999 // use after free.
7000 if (IsBeingDestroyed())
7001 return;
7002
7003 // Notify for UI updates if the state changes. Need both TYPE_TAB and TYPE_URL
7004 // to update both the tab-level usage indicator and the usage indicator in the
7005 // omnibox.
Marijn Kruisselbrink46fa4bd2019-07-11 18:15:477006 const bool was_empty = native_file_system_directory_handles_.empty();
7007 native_file_system_directory_handles_[path]++;
7008 if (was_empty) {
Marijn Kruisselbrinkf1714aa22019-07-02 03:45:337009 NotifyNavigationStateChanged(static_cast<content::InvalidateTypes>(
7010 INVALIDATE_TYPE_TAB | INVALIDATE_TYPE_URL));
7011 }
7012}
7013
Marijn Kruisselbrink46fa4bd2019-07-11 18:15:477014void WebContentsImpl::RemoveNativeFileSystemDirectoryHandle(
7015 const base::FilePath& path) {
Marijn Kruisselbrinkf1714aa22019-07-02 03:45:337016 // Trying to invalidate the tab state while being destroyed could result in a
7017 // use after free.
7018 if (IsBeingDestroyed())
7019 return;
7020
7021 // Notify for UI updates if the state changes. Need both TYPE_TAB and TYPE_URL
7022 // to update both the tab-level usage indicator and the usage indicator in the
7023 // omnibox.
Marijn Kruisselbrink46fa4bd2019-07-11 18:15:477024 auto it = native_file_system_directory_handles_.find(path);
7025 DCHECK(it != native_file_system_directory_handles_.end());
7026 DCHECK_NE(0u, it->second);
7027 it->second--;
7028 if (it->second == 0)
7029 native_file_system_directory_handles_.erase(it);
7030 if (native_file_system_directory_handles_.empty()) {
Marijn Kruisselbrinkf1714aa22019-07-02 03:45:337031 NotifyNavigationStateChanged(static_cast<content::InvalidateTypes>(
7032 INVALIDATE_TYPE_TAB | INVALIDATE_TYPE_URL));
7033 }
7034}
7035
7036void WebContentsImpl::IncrementWritableNativeFileSystemHandleCount() {
7037 // Trying to invalidate the tab state while being destroyed could result in a
7038 // use after free.
7039 if (IsBeingDestroyed())
7040 return;
7041
7042 // Notify for UI updates if the state changes. Need both TYPE_TAB and TYPE_URL
7043 // to update both the tab-level usage indicator and the usage indicator in the
7044 // omnibox.
7045 native_file_system_writable_handle_count_++;
7046 if (native_file_system_writable_handle_count_ == 1) {
7047 NotifyNavigationStateChanged(static_cast<content::InvalidateTypes>(
7048 INVALIDATE_TYPE_TAB | INVALIDATE_TYPE_URL));
7049 }
7050}
7051
7052void WebContentsImpl::DecrementWritableNativeFileSystemHandleCount() {
7053 // Trying to invalidate the tab state while being destroyed could result in a
7054 // use after free.
7055 if (IsBeingDestroyed())
7056 return;
7057
7058 // Notify for UI updates if the state changes. Need both TYPE_TAB and TYPE_URL
7059 // to update both the tab-level usage indicator and the usage indicator in the
7060 // omnibox.
7061 DCHECK_NE(0u, native_file_system_writable_handle_count_);
7062 native_file_system_writable_handle_count_--;
7063 if (native_file_system_writable_handle_count_ == 0) {
7064 NotifyNavigationStateChanged(static_cast<content::InvalidateTypes>(
7065 INVALIDATE_TYPE_TAB | INVALIDATE_TYPE_URL));
7066 }
7067}
7068
zqzhang8ac49002017-03-16 21:51:357069void WebContentsImpl::SetHasPersistentVideo(bool has_persistent_video) {
7070 if (has_persistent_video_ == has_persistent_video)
mlamouri5cd9ae82017-02-18 11:05:097071 return;
7072
zqzhang8ac49002017-03-16 21:51:357073 has_persistent_video_ = has_persistent_video;
mlamouri5cd9ae82017-02-18 11:05:097074 NotifyPreferencesChanged();
zqzhang8ac49002017-03-16 21:51:357075 media_web_contents_observer()->RequestPersistentVideo(has_persistent_video);
mlamouri5cd9ae82017-02-18 11:05:097076}
7077
Ian Vollick5d5c37f2019-06-19 22:50:577078void WebContentsImpl::SetSpatialNavigationDisabled(bool disabled) {
7079 if (is_spatial_navigation_disabled_ == disabled)
7080 return;
7081
7082 is_spatial_navigation_disabled_ = disabled;
7083 NotifyPreferencesChanged();
7084}
7085
paulmeyerfeafc2d2017-04-25 21:46:407086void WebContentsImpl::BrowserPluginGuestWillDetach() {
7087 WebContentsImpl* outermost = GetOutermostWebContents();
7088 if (this != outermost && ContainsOrIsFocusedWebContents())
7089 outermost->SetAsFocusedWebContentsIfNecessary();
7090}
7091
Becca Hughes112832e2019-06-11 17:19:027092PictureInPictureResult WebContentsImpl::EnterPictureInPicture(
Mounir Lamouri11e9ef42018-05-22 03:10:167093 const viz::SurfaceId& surface_id,
7094 const gfx::Size& natural_size) {
François Beaufortf09e4462019-01-18 14:34:517095 return delegate_
7096 ? delegate_->EnterPictureInPicture(this, surface_id, natural_size)
Becca Hughes112832e2019-06-11 17:19:027097 : PictureInPictureResult::kNotSupported;
Mounir Lamouri11e9ef42018-05-22 03:10:167098}
7099
Mounir Lamouri6d759e12018-05-16 20:01:307100void WebContentsImpl::ExitPictureInPicture() {
7101 if (delegate_)
7102 delegate_->ExitPictureInPicture();
7103}
7104
paulmeyerc0b762b2016-04-13 11:55:177105#if defined(OS_ANDROID)
7106void WebContentsImpl::NotifyFindMatchRectsReply(
7107 int version,
7108 const std::vector<gfx::RectF>& rects,
7109 const gfx::RectF& active_rect) {
7110 if (delegate_)
7111 delegate_->FindMatchRectsReply(this, version, rects, active_rect);
7112}
7113#endif
7114
[email protected]34ff1cfc2014-08-20 06:16:057115void WebContentsImpl::SetForceDisableOverscrollContent(bool force_disable) {
7116 force_disable_overscroll_content_ = force_disable;
7117 if (view_)
7118 view_->SetOverscrollControllerEnabled(CanOverscrollContent());
7119}
7120
Jianzhou Fengd8720c72018-01-18 03:06:307121bool WebContentsImpl::SetDeviceEmulationSize(const gfx::Size& new_size) {
7122 device_emulation_size_ = new_size;
7123 RenderWidgetHostView* rwhv = GetMainFrame()->GetView();
7124
7125 const gfx::Size current_size = rwhv->GetViewBounds().size();
7126 if (view_size_before_emulation_.IsEmpty())
7127 view_size_before_emulation_ = current_size;
7128
7129 if (current_size != new_size)
7130 rwhv->SetSize(new_size);
7131
7132 return current_size != new_size;
7133}
7134
7135void WebContentsImpl::ClearDeviceEmulationSize() {
7136 RenderWidgetHostView* rwhv = GetMainFrame()->GetView();
7137 // WebContentsView could get resized during emulation, which also resizes
7138 // RWHV. If it happens, assume user would like to keep using the size after
7139 // emulation.
7140 // TODO(jzfeng): Prohibit resizing RWHV through any other means (at least when
7141 // WebContentsView size changes).
Dmitry Gozmanc63707182018-03-06 00:09:397142 if (!view_size_before_emulation_.IsEmpty() && rwhv &&
7143 rwhv->GetViewBounds().size() == device_emulation_size_) {
Jianzhou Fengd8720c72018-01-18 03:06:307144 rwhv->SetSize(view_size_before_emulation_);
Dmitry Gozmanc63707182018-03-06 00:09:397145 }
Jianzhou Fengd8720c72018-01-18 03:06:307146 device_emulation_size_ = gfx::Size();
7147 view_size_before_emulation_ = gfx::Size();
7148}
7149
Max Morin5bc74f52018-05-09 07:00:217150ForwardingAudioStreamFactory* WebContentsImpl::GetAudioStreamFactory() {
7151 if (!audio_stream_factory_) {
7152 audio_stream_factory_.emplace(
7153 this,
Max Morinddebb972018-09-20 10:04:147154 // BrowserMainLoop::GetInstance() may be null in unit tests.
7155 BrowserMainLoop::GetInstance()
7156 ? static_cast<media::UserInputMonitorBase*>(
7157 BrowserMainLoop::GetInstance()->user_input_monitor())
7158 : nullptr,
Ken Rockotfd03e682019-06-20 21:13:547159 GetSystemConnector()->Clone(), AudioStreamBrokerFactory::CreateImpl());
Max Morin5bc74f52018-05-09 07:00:217160 }
7161
7162 return &*audio_stream_factory_;
7163}
7164
dalecurtis88c240072015-12-09 02:11:187165void WebContentsImpl::MediaStartedPlaying(
billorr21c005d2016-11-17 03:57:047166 const WebContentsObserver::MediaPlayerInfo& media_info,
Mounir Lamouri0f5bdf52019-04-04 16:32:357167 const MediaPlayerId& id) {
billorr21c005d2016-11-17 03:57:047168 if (media_info.has_video)
7169 currently_playing_video_count_++;
7170
ericwilligersde386342016-10-19 02:35:097171 for (auto& observer : observers_)
billorr21c005d2016-11-17 03:57:047172 observer.MediaStartedPlaying(media_info, id);
dalecurtis88c240072015-12-09 02:11:187173}
7174
7175void WebContentsImpl::MediaStoppedPlaying(
billorr21c005d2016-11-17 03:57:047176 const WebContentsObserver::MediaPlayerInfo& media_info,
Mounir Lamouri0f5bdf52019-04-04 16:32:357177 const MediaPlayerId& id,
Becca Hughes220bfc102017-11-14 18:24:077178 WebContentsObserver::MediaStoppedReason reason) {
billorr21c005d2016-11-17 03:57:047179 if (media_info.has_video)
7180 currently_playing_video_count_--;
7181
ericwilligersde386342016-10-19 02:35:097182 for (auto& observer : observers_)
Becca Hughes220bfc102017-11-14 18:24:077183 observer.MediaStoppedPlaying(media_info, id, reason);
billorr21c005d2016-11-17 03:57:047184}
7185
Mounir Lamouri0f5bdf52019-04-04 16:32:357186void WebContentsImpl::MediaResized(const gfx::Size& size,
7187 const MediaPlayerId& id) {
peconn257951522017-06-09 18:24:597188 cached_video_sizes_[id] = size;
7189
7190 for (auto& observer : observers_)
7191 observer.MediaResized(size, id);
7192}
7193
Peter E Conneccb34c22017-09-08 09:37:587194void WebContentsImpl::MediaEffectivelyFullscreenChanged(bool is_fullscreen) {
7195 for (auto& observer : observers_)
7196 observer.MediaEffectivelyFullscreenChanged(is_fullscreen);
7197}
7198
Peter Conn31c726a2017-08-17 11:07:347199base::Optional<gfx::Size> WebContentsImpl::GetFullscreenVideoSize() {
Mounir Lamouri0f5bdf52019-04-04 16:32:357200 base::Optional<MediaPlayerId> id =
Peter Conn31c726a2017-08-17 11:07:347201 media_web_contents_observer_->GetFullscreenVideoMediaPlayerId();
Jan Wilken Dörrie531be7ca2019-06-07 10:05:577202 if (id && base::Contains(cached_video_sizes_, id.value()))
Peter Conn31c726a2017-08-17 11:07:347203 return base::Optional<gfx::Size>(cached_video_sizes_[id.value()]);
Lei Zhang6410ec1a2019-01-31 21:38:587204 return base::nullopt;
peconn257951522017-06-09 18:24:597205}
7206
billorr21c005d2016-11-17 03:57:047207int WebContentsImpl::GetCurrentlyPlayingVideoCount() {
7208 return currently_playing_video_count_;
dalecurtis88c240072015-12-09 02:11:187209}
7210
Raymond Toye413650c2018-08-31 00:12:477211void WebContentsImpl::AudioContextPlaybackStarted(RenderFrameHost* host,
7212 int context_id) {
7213 WebContentsObserver::AudioContextId audio_context_id(host, context_id);
7214 for (auto& observer : observers_)
7215 observer.AudioContextPlaybackStarted(audio_context_id);
7216}
7217
7218void WebContentsImpl::AudioContextPlaybackStopped(RenderFrameHost* host,
7219 int context_id) {
7220 WebContentsObserver::AudioContextId audio_context_id(host, context_id);
7221 for (auto& observer : observers_)
7222 observer.AudioContextPlaybackStopped(audio_context_id);
7223}
7224
evliufc233aa2019-10-31 19:53:037225void WebContentsImpl::MediaWatchTimeChanged(
7226 const content::MediaPlayerWatchTime& watch_time) {
7227 for (auto& observer : observers_)
7228 observer.MediaWatchTimeChanged(watch_time);
7229}
7230
Ehsan Karamad1c631762019-05-13 23:04:017231RenderFrameHostImpl* WebContentsImpl::GetMainFrameForInnerDelegate(
7232 FrameTreeNode* frame_tree_node) {
7233 if (auto* web_contents = node_.GetInnerWebContentsInFrame(frame_tree_node))
7234 return web_contents->GetMainFrame();
7235 return nullptr;
7236}
7237
John Budorickee1c9512019-10-17 15:32:197238bool WebContentsImpl::IsFrameLowPriority(
7239 const RenderFrameHost* render_frame_host) {
7240 if (!delegate_)
7241 return false;
7242 return delegate_->IsFrameLowPriority(this, render_frame_host);
7243}
7244
Francois Doraye6161152018-03-27 22:05:377245void WebContentsImpl::UpdateWebContentsVisibility(Visibility visibility) {
Francois Doray3f2afbd2018-04-06 19:18:187246 // Occlusion is disabled when |features::kWebContentsOcclusion| is disabled
7247 // (for power and speed impact assessment) or when
7248 // |switches::kDisableBackgroundingOccludedWindowsForTesting| is specified on
7249 // the command line (to avoid flakiness in browser tests).
7250 const bool occlusion_is_disabled =
7251 !base::FeatureList::IsEnabled(features::kWebContentsOcclusion) ||
Francois Doraye6161152018-03-27 22:05:377252 base::CommandLine::ForCurrentProcess()->HasSwitch(
7253 switches::kDisableBackgroundingOccludedWindowsForTesting);
7254 if (occlusion_is_disabled && visibility == Visibility::OCCLUDED)
7255 visibility = Visibility::VISIBLE;
7256
tapted65ff2ea72016-03-01 23:39:007257 if (!did_first_set_visible_) {
Francois Doraye6161152018-03-27 22:05:377258 if (visibility == Visibility::VISIBLE) {
7259 // A WebContents created with CreateParams::initially_hidden = false
7260 // starts with GetVisibility() == Visibility::VISIBLE even though it is
7261 // not really visible. Call WasShown() when it becomes visible for real as
7262 // the page load mechanism and some WebContentsObserver rely on that.
tapted65ff2ea72016-03-01 23:39:007263 WasShown();
Francois Dorayfe4a1772018-02-17 04:17:097264 did_first_set_visible_ = true;
tapted65ff2ea72016-03-01 23:39:007265 }
Francois Doraye6161152018-03-27 22:05:377266
7267 // Trust the initial visibility of the WebContents and do not switch it to
7268 // HIDDEN or OCCLUDED before it becomes VISIBLE for real. Doing so would
7269 // result in destroying resources that would immediately be recreated (e.g.
7270 // UpdateWebContents(HIDDEN) can be called when a WebContents is added to a
7271 // hidden window that is about to be shown).
7272
tapted65ff2ea72016-03-01 23:39:007273 return;
7274 }
Francois Doraye6161152018-03-27 22:05:377275
7276 if (visibility == visibility_)
tapted65ff2ea72016-03-01 23:39:007277 return;
7278
Collin Baker989e0882019-11-01 01:27:177279 UpdateVisibilityAndNotifyPageAndView(visibility);
tapted65ff2ea72016-03-01 23:39:007280}
7281
aelias5252baa2016-04-10 01:18:027282void WebContentsImpl::UpdateOverridingUserAgent() {
mlamouri5cd9ae82017-02-18 11:05:097283 NotifyPreferencesChanged();
aelias5252baa2016-04-10 01:18:027284}
7285
avid53461d2016-02-25 17:15:047286void WebContentsImpl::SetJavaScriptDialogManagerForTesting(
7287 JavaScriptDialogManager* dialog_manager) {
7288 dialog_manager_ = dialog_manager;
7289}
7290
rockotf62002a2016-09-15 00:08:597291void WebContentsImpl::RemoveBindingSet(const std::string& interface_name) {
7292 auto it = binding_sets_.find(interface_name);
7293 if (it != binding_sets_.end())
7294 binding_sets_.erase(it);
7295}
7296
nzolghadrd87a308d2016-12-07 15:45:567297bool WebContentsImpl::AddDomainInfoToRapporSample(rappor::Sample* sample) {
nzolghadrbbffc6c2017-05-25 17:56:397298 // Here we associate this metric to the main frame URL regardless of what
7299 // caused it.
nzolghadrd87a308d2016-12-07 15:45:567300 sample->SetStringField("Domain", ::rappor::GetDomainAndRegistrySampleFromGURL(
7301 GetLastCommittedURL()));
nzolghadrd87a308d2016-12-07 15:45:567302 return true;
7303}
7304
ekaramada110f642016-12-21 19:47:287305void WebContentsImpl::FocusedNodeTouched(bool editable) {
7306#if defined(OS_WIN)
ekaramada110f642016-12-21 19:47:287307 RenderWidgetHostView* view = GetRenderWidgetHostView();
7308 if (!view)
7309 return;
Dave Tapuska2315d8f2018-03-06 17:52:557310 view->FocusedNodeTouched(editable);
ekaramada110f642016-12-21 19:47:287311#endif
7312}
7313
falken52a56e32016-12-08 05:02:407314void WebContentsImpl::ShowInsecureLocalhostWarningIfNeeded() {
7315 bool allow_localhost = base::CommandLine::ForCurrentProcess()->HasSwitch(
7316 switches::kAllowInsecureLocalhost);
7317 if (!allow_localhost)
7318 return;
7319
7320 content::NavigationEntry* entry = GetController().GetLastCommittedEntry();
Rob Wuf79b3ba2018-01-14 01:54:317321 if (!entry || !net::IsLocalhost(entry->GetURL()))
falken52a56e32016-12-08 05:02:407322 return;
7323
7324 content::SSLStatus ssl_status = entry->GetSSL();
Matt Mueller936511442019-09-03 18:15:127325 if (!net::IsCertStatusError(ssl_status.cert_status))
falken52a56e32016-12-08 05:02:407326 return;
7327
7328 GetMainFrame()->AddMessageToConsole(
Abhijeet Kandalkara5928a0a2019-03-13 05:04:427329 blink::mojom::ConsoleMessageLevel::kWarning,
falken52a56e32016-12-08 05:02:407330 base::StringPrintf("This site does not have a valid SSL "
7331 "certificate! Without SSL, your site's and "
7332 "visitors' data is vulnerable to theft and "
7333 "tampering. Get a valid SSL certificate before"
7334 " releasing your website to the public."));
7335}
7336
ekaramadf6750aa2017-06-06 18:29:427337bool WebContentsImpl::IsShowingContextMenuOnPage() const {
7338 return showing_context_menu_;
7339}
7340
Min Qina904f3302018-02-13 23:33:347341download::DownloadUrlParameters::RequestHeadersType
7342WebContentsImpl::ParseDownloadHeaders(const std::string& headers) {
7343 download::DownloadUrlParameters::RequestHeadersType request_headers;
Megan Jablonski2f6a4c52017-07-10 23:01:257344 for (const base::StringPiece& key_value : base::SplitStringPiece(
7345 headers, "\r\n", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY)) {
7346 std::vector<std::string> pair = base::SplitString(
7347 key_value, ":", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
7348 if (2ul == pair.size())
7349 request_headers.push_back(make_pair(pair[0], pair[1]));
7350 }
7351 return request_headers;
7352}
7353
lukasza6f8ac622017-06-06 03:10:207354void WebContentsImpl::SetOpenerForNewContents(FrameTreeNode* opener,
7355 bool opener_suppressed) {
7356 if (opener) {
7357 FrameTreeNode* new_root = GetFrameTree()->root();
7358
7359 // For the "original opener", track the opener's main frame instead, because
7360 // if the opener is a subframe, the opener tracking could be easily bypassed
7361 // by spawning from a subframe and deleting the subframe.
7362 // https://crbug.com/705316
7363 new_root->SetOriginalOpener(opener->frame_tree()->root());
7364
7365 if (!opener_suppressed) {
7366 new_root->SetOpener(opener);
7367 created_with_opener_ = true;
7368 }
7369 }
7370}
7371
Mounir Lamouri0f5bdf52019-04-04 16:32:357372void WebContentsImpl::MediaMutedStatusChanged(const MediaPlayerId& id,
7373 bool muted) {
Becca Hughes9f6fd4b82017-06-15 10:01:407374 for (auto& observer : observers_)
7375 observer.MediaMutedStatusChanged(id, muted);
7376}
7377
EhsanK2075c7e2017-08-21 02:42:397378void WebContentsImpl::SetVisibilityForChildViews(bool visible) {
7379 GetMainFrame()->SetVisibilityForChildViews(visible);
7380}
7381
Dave Tapuskae782bea2019-07-09 16:21:517382mojom::FrameInputHandler* WebContentsImpl::GetFocusedFrameInputHandler() {
7383 auto* focused_frame = GetFocusedFrame();
7384 if (!focused_frame)
7385 return nullptr;
7386 return focused_frame->GetFrameInputHandler();
7387}
7388
[email protected]8ff00d72012-10-23 19:12:217389} // namespace content