blob: c86b669d79f8cb46d98b470cde11bf4a4afc937d [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"
Pavel Feldman3c1842b2017-08-02 05:00:1651#include "content/browser/devtools/protocol/page_handler.h"
dgozmanf00c4f72016-10-19 17:45:1552#include "content/browser/devtools/render_frame_devtools_agent_host.h"
Becca Hughes3b5a43482018-07-17 22:31:5553#include "content/browser/display_cutout/display_cutout_host_impl.h"
[email protected]5f2aa722013-08-07 16:59:4154#include "content/browser/dom_storage/dom_storage_context_wrapper.h"
[email protected]1ea3c792012-04-17 01:25:0455#include "content/browser/dom_storage/session_storage_namespace_impl.h"
[email protected]aa4f3972012-03-01 18:12:1256#include "content/browser/download/mhtml_generation_manager.h"
[email protected]a53209b2012-01-20 16:48:1657#include "content/browser/download/save_package.h"
paulmeyerc0b762b2016-04-13 11:55:1758#include "content/browser/find_request_manager.h"
[email protected]bffc8302014-01-23 20:52:1659#include "content/browser/frame_host/cross_process_frame_connector.h"
ananta4b7467a52016-09-23 01:42:3860#include "content/browser/frame_host/frame_tree_node.h"
[email protected]d4a8ca482013-10-30 21:06:4061#include "content/browser/frame_host/interstitial_page_impl.h"
62#include "content/browser/frame_host/navigation_entry_impl.h"
Arthur Hemeryc3380172018-01-22 14:00:1763#include "content/browser/frame_host/navigation_request.h"
[email protected]6ea6bdf2013-12-06 13:35:0164#include "content/browser/frame_host/navigator_impl.h"
[email protected]52913802013-12-10 05:52:1865#include "content/browser/frame_host/render_frame_host_impl.h"
lfgdb5c4ed2016-03-04 23:09:0766#include "content/browser/frame_host/render_frame_proxy_host.h"
Kenneth Russell954ed9ce2018-04-12 23:07:0167#include "content/browser/gpu/gpu_data_manager_impl.h"
mlamouriefdca9d2014-09-16 16:55:4068#include "content/browser/manifest/manifest_manager_host.h"
Max Morin5bc74f52018-05-09 07:00:2169#include "content/browser/media/audio_stream_broker.h"
dalecurtisbc6572e12014-09-12 19:22:3070#include "content/browser/media/audio_stream_monitor.h"
miu50f97892014-09-22 22:49:5271#include "content/browser/media/capture/web_contents_audio_muter.h"
dalecurtis88c240072015-12-09 02:11:1872#include "content/browser/media/media_web_contents_observer.h"
zqzhang1adf3cb2016-11-01 11:26:4573#include "content/browser/media/session/media_session_impl.h"
tommyclieb25b2a2014-11-03 19:45:0974#include "content/browser/plugin_content_origin_whitelist.h"
David Black9cca3592019-11-06 23:02:2275#include "content/browser/renderer_host/frame_token_message_queue.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]86a0a6e2013-01-28 06:33:0388#include "content/browser/webui/web_ui_controller_factory_registry.h"
[email protected]d2353452012-01-19 19:53:5689#include "content/browser/webui/web_ui_impl.h"
[email protected]8eb04562013-03-06 03:41:1490#include "content/common/browser_plugin/browser_plugin_constants.h"
Paul Meyer0c58c3712016-11-17 22:59:5191#include "content/common/drag_messages.h"
[email protected]f114fa42013-12-06 17:06:4492#include "content/common/frame_messages.h"
[email protected]1f3fc1d2014-04-03 14:50:1793#include "content/common/input_messages.h"
lfgdb5c4ed2016-03-04 23:09:0794#include "content/common/page_messages.h"
nasko34634672016-07-29 18:46:0695#include "content/common/page_state_serialization.h"
rockot5c478a72016-09-28 23:14:1896#include "content/common/render_message_filter.mojom.h"
[email protected]2c5569662011-03-22 20:45:0297#include "content/common/view_messages.h"
Albert J. Wong3c93c182018-09-27 17:29:4398#include "content/common/widget_messages.h"
Abigail Kleinabb428332019-09-13 18:26:2199#include "content/public/browser/accessibility_tree_formatter.h"
[email protected]31a71eaf2014-03-13 01:47:36100#include "content/public/browser/ax_event_notification_details.h"
[email protected]ccb797302011-12-15 16:55:11101#include "content/public/browser/browser_context.h"
[email protected]139355f2014-05-11 14:21:28102#include "content/public/browser/browser_plugin_guest_manager.h"
Eric Seckler8652dcd52018-09-20 10:42:28103#include "content/public/browser/browser_task_traits.h"
Gabriel Charette790754c2018-03-16 21:32:59104#include "content/public/browser/browser_thread.h"
[email protected]87f3c082011-10-19 18:07:44105#include "content/public/browser/content_browser_client.h"
Ken Rockotce010f02019-12-12 23:32:32106#include "content/public/browser/device_service.h"
[email protected]e582fdd2011-12-20 16:48:17107#include "content/public/browser/download_manager.h"
Kent Tamura512a27e2018-10-04 00:49:32108#include "content/public/browser/file_select_listener.h"
ekaramada110f642016-12-21 19:47:28109#include "content/public/browser/focused_node_details.h"
[email protected]d9083482012-01-06 00:38:46110#include "content/public/browser/invalidate_type.h"
[email protected]71a88bb2013-02-01 22:05:15111#include "content/public/browser/javascript_dialog_manager.h"
skyf65d9bb2017-03-24 02:26:39112#include "content/public/browser/keyboard_event_processing_result.h"
[email protected]09d31d52012-03-11 22:30:27113#include "content/public/browser/load_notification_details.h"
[email protected]5b96836f2011-12-22 07:39:00114#include "content/public/browser/navigation_details.h"
[email protected]375fa1b2012-05-22 22:05:37115#include "content/public/browser/notification_details.h"
[email protected]be2510c02012-05-28 14:52:14116#include "content/public/browser/notification_service.h"
scottmg011d96e32016-06-29 19:15:08117#include "content/public/browser/notification_types.h"
[email protected]95640212014-07-26 18:14:30118#include "content/public/browser/render_widget_host_iterator.h"
Lukasz Anforowicze1b954d92017-10-30 21:28:06119#include "content/public/browser/restore_type.h"
estark5ea80e52015-06-19 18:43:12120#include "content/public/browser/security_style_explanations.h"
falken52a56e32016-12-08 05:02:40121#include "content/public/browser/ssl_status.h"
[email protected]4c3a23582012-08-18 08:54:34122#include "content/public/browser/storage_partition.h"
[email protected]674bc592011-12-20 23:00:42123#include "content/public/browser/web_contents_delegate.h"
Julie Jeongeun Kim7d0986172019-12-04 03:17:25124#include "content/public/browser/web_contents_receiver_set.h"
Lei Zhangee3b63ad2018-06-06 23:25:31125#include "content/public/browser/web_ui_controller.h"
[email protected]e091df82011-10-11 18:13:21126#include "content/public/common/bindings_policy.h"
alexmosc9e76ec2016-05-16 22:59:37127#include "content/public/common/child_process_host.h"
Hans Wennborg5ffd1392019-10-16 11:00:02128#include "content/public/common/content_client.h"
[email protected]54087fe2011-10-28 22:02:48129#include "content/public/common/content_constants.h"
Francois Doray3f2afbd2018-04-06 19:18:18130#include "content/public/common/content_features.h"
[email protected]64d69de42012-02-06 00:19:54131#include "content/public/common/content_switches.h"
Lukasz Anforowicze1b954d92017-10-30 21:28:06132#include "content/public/common/page_state.h"
Leon Han963dc182018-11-06 05:41:48133#include "content/public/common/referrer_type_converters.h"
[email protected]04cbd3d2013-12-04 04:58:20134#include "content/public/common/result_codes.h"
[email protected]f16cc6782013-12-16 23:42:57135#include "content/public/common/url_utils.h"
danakjffc8baf2019-10-10 23:49:42136#include "content/public/common/use_zoom_for_dsf_policy.h"
[email protected]b3a97b52014-07-09 06:25:05137#include "content/public/common/web_preferences.h"
Max Morinddebb972018-09-20 10:04:14138#include "media/base/user_input_monitor.h"
wjmaclean64951902016-04-29 20:59:12139#include "net/base/url_util.h"
[email protected]8bfc8272013-09-09 20:10:53140#include "net/http/http_cache.h"
141#include "net/http/http_transaction_factory.h"
rhalavati0fbaeef32017-06-01 15:56:30142#include "net/traffic_annotation/network_traffic_annotation.h"
[email protected]8bfc8272013-09-09 20:10:53143#include "net/url_request/url_request_context.h"
[email protected]abe2c032011-03-31 18:49:34144#include "net/url_request/url_request_context_getter.h"
Scott Violet02e38b92018-03-27 23:42:14145#include "ppapi/buildflags/buildflags.h"
Mounir Lamourifd9dcefa2017-07-06 10:26:55146#include "services/metrics/public/cpp/ukm_recorder.h"
John Abd-El-Malekb175a612018-12-03 20:11:58147#include "services/network/public/cpp/features.h"
rockot734fb662016-10-15 16:41:30148#include "services/service_manager/public/cpp/interface_provider.h"
Becca Hughes6daf5662018-06-27 16:50:54149#include "third_party/blink/public/common/associated_interfaces/associated_interface_provider.h"
Blink Reformata30d4232018-04-07 15:31:06150#include "third_party/blink/public/common/frame/sandbox_flags.h"
151#include "third_party/blink/public/common/mime_util/mime_util.h"
danakj938b37a62019-09-24 18:35:54152#include "third_party/blink/public/common/page/page_zoom.h"
Sergio Villar Senin9d9d4f52019-10-15 10:57:57153#include "third_party/blink/public/common/security/security_style.h"
Dave Tapuskaa4189512019-10-15 20:27:34154#include "third_party/blink/public/mojom/frame/fullscreen.mojom.h"
Takuto Ikutaaa3b796c2019-02-06 02:54:56155#include "third_party/blink/public/mojom/loader/pause_subresource_loading_handle.mojom.h"
Antonio Gomes0d42960a2019-06-05 12:35:51156#include "third_party/blink/public/mojom/mediastream/media_stream.mojom-shared.h"
halton.huoca2eabd2015-07-06 08:17:40157#include "third_party/skia/include/core/SkBitmap.h"
dmazzonid95ae842016-04-12 21:17:39158#include "ui/accessibility/ax_tree_combiner.h"
[email protected]a08412b62012-05-29 21:28:56159#include "ui/base/layout.h"
Daniel Cheng90196c82018-04-25 21:49:14160#include "ui/events/base_event_utils.h"
chongz4a9751942016-08-17 17:49:39161#include "ui/events/blink/web_input_event_traits.h"
[email protected]66bd5512012-08-01 02:02:52162#include "ui/gl/gl_switches.h"
[email protected]3c733bde2010-12-21 19:56:31163
ekaramada110f642016-12-21 19:47:28164#if defined(OS_WIN)
165#include "content/browser/renderer_host/dip_util.h"
166#include "ui/gfx/geometry/dip_util.h"
167#endif
168
[email protected]583418cc2013-01-17 14:01:10169#if defined(OS_ANDROID)
mfomitchev2b8b066a2016-01-28 19:23:15170#include "content/browser/android/date_time_chooser_android.h"
sammcf5f1b0f2016-09-20 23:05:11171#include "content/browser/android/java_interfaces_impl.h"
Leon Hanccb985f2019-08-26 02:43:24172#include "content/browser/android/nfc_host.h"
[email protected]155c7f22013-12-09 17:07:18173#include "content/browser/web_contents/web_contents_android.h"
Ke He31d0bb02018-02-24 07:16:24174#include "services/device/public/mojom/nfc.mojom.h"
mcnee336ea2c2017-05-23 22:50:59175#else // !OS_ANDROID
176#include "content/browser/host_zoom_map_impl.h"
mfomitchev2b8b066a2016-01-28 19:23:15177#endif // OS_ANDROID
[email protected]583418cc2013-01-17 14:01:10178
brettw4b461082016-11-19 18:55:16179#if BUILDFLAG(ENABLE_PLUGINS)
zqzhang181047e62016-07-01 13:37:17180#include "content/browser/media/session/pepper_playback_observer.h"
Lei Zhangebcc6302018-01-12 19:46:45181#endif
zqzhang181047e62016-07-01 13:37:17182
[email protected]8ff00d72012-10-23 19:12:21183namespace content {
Abigail Kleinaa898742019-11-01 02:31:25184
185using AccessibilityEventCallback =
186 base::RepeatingCallback<void(const std::string&)>;
187
[email protected]420ae012009-04-24 05:16:32188namespace {
189
[email protected]960b0372014-05-19 18:01:00190const int kMinimumDelayBetweenLoadingUpdatesMS = 100;
[email protected]82114f52012-03-20 22:53:41191const char kDotGoogleDotCom[] = ".google.com";
[email protected]ca406032011-07-19 21:53:05192
Avi Drissmana9114822018-03-23 18:25:55193base::LazyInstance<std::vector<
194 WebContentsImpl::FriendWrapper::CreatedCallback>>::DestructorAtExit
195 g_created_callbacks = LAZY_INSTANCE_INITIALIZER;
[email protected]cbb1ef592013-06-05 19:49:46196
dcheng57e39e22016-01-21 00:25:38197bool HasMatchingProcess(FrameTree* tree, int render_process_id) {
198 for (FrameTreeNode* node : tree->Nodes()) {
199 if (node->current_frame_host()->GetProcess()->GetID() == render_process_id)
200 return true;
alexmos4cf2aa32015-07-15 23:40:43201 }
dcheng57e39e22016-01-21 00:25:38202 return false;
[email protected]95640212014-07-26 18:14:30203}
204
kenrb19221852016-04-29 17:21:40205bool HasMatchingWidgetHost(FrameTree* tree, RenderWidgetHost* host) {
206 // This method scans the frame tree rather than checking whether
207 // host->delegate() == this, which allows it to return false when the host
208 // for a frame that is pending or pending deletion.
209 if (!host)
210 return false;
211
212 for (FrameTreeNode* node : tree->Nodes()) {
213 if (node->current_frame_host()->GetRenderWidgetHost() == host)
214 return true;
215 }
216 return false;
217}
218
dmazzonif8a31042016-12-03 00:52:13219void UpdateAccessibilityModeOnFrame(RenderFrameHost* frame_host) {
220 static_cast<RenderFrameHostImpl*>(frame_host)->UpdateAccessibilityMode();
[email protected]95640212014-07-26 18:14:30221}
222
dtseng0dd3fa12015-07-22 19:00:52223void ResetAccessibility(RenderFrameHost* rfh) {
224 static_cast<RenderFrameHostImpl*>(rfh)->AccessibilityReset();
225}
226
Ian Clelland5cbaaf82017-11-27 22:00:03227RenderFrameHostImpl* FindOpenerRFH(const WebContents::CreateParams& params) {
228 RenderFrameHostImpl* opener_rfh = nullptr;
alexmose201c7cd2015-06-10 17:14:21229 if (params.opener_render_frame_id != MSG_ROUTING_NONE) {
Ian Clelland5cbaaf82017-11-27 22:00:03230 opener_rfh = RenderFrameHostImpl::FromID(params.opener_render_process_id,
231 params.opener_render_frame_id);
alexmose201c7cd2015-06-10 17:14:21232 }
Ian Clelland5cbaaf82017-11-27 22:00:03233 return opener_rfh;
lukasza6f8ac622017-06-06 03:10:20234}
235
Dominick Ng37a15052018-07-17 23:56:35236// Returns |true| if |type| is the kind of user input that should trigger the
237// user interaction observers.
238bool IsUserInteractionInputType(blink::WebInputEvent::Type type) {
239 // Ideally, this list would be based more off of
240 // https://whatwg.org/C/interaction.html#triggered-by-user-activation.
241 return type == blink::WebInputEvent::kMouseDown ||
242 type == blink::WebInputEvent::kGestureScrollBegin ||
243 type == blink::WebInputEvent::kTouchStart ||
244 type == blink::WebInputEvent::kRawKeyDown;
245}
246
Pavel Feldman3c1842b2017-08-02 05:00:16247// Ensures that OnDialogClosed is only called once.
248class CloseDialogCallbackWrapper
249 : public base::RefCountedThreadSafe<CloseDialogCallbackWrapper> {
250 public:
Avi Drissmane04d3992017-10-05 15:11:36251 using CloseCallback =
252 base::OnceCallback<void(bool, bool, const base::string16&)>;
253
254 explicit CloseDialogCallbackWrapper(CloseCallback callback)
255 : callback_(std::move(callback)) {}
Pavel Feldman3c1842b2017-08-02 05:00:16256
257 void Run(bool dialog_was_suppressed,
258 bool success,
259 const base::string16& user_input) {
Avi Drissmane04d3992017-10-05 15:11:36260 if (callback_.is_null())
Pavel Feldman3c1842b2017-08-02 05:00:16261 return;
Avi Drissmane04d3992017-10-05 15:11:36262 std::move(callback_).Run(dialog_was_suppressed, success, user_input);
Pavel Feldman3c1842b2017-08-02 05:00:16263 }
264
265 private:
266 friend class base::RefCountedThreadSafe<CloseDialogCallbackWrapper>;
267 ~CloseDialogCallbackWrapper() {}
Avi Drissmane04d3992017-10-05 15:11:36268
269 CloseCallback callback_;
Pavel Feldman3c1842b2017-08-02 05:00:16270};
271
Arthur Sonzogni929d29f2018-09-17 14:19:44272bool FrameCompareDepth(RenderFrameHostImpl* a, RenderFrameHostImpl* b) {
273 return a->frame_tree_node()->depth() < b->frame_tree_node()->depth();
274}
275
Raymes Khoury58373df2019-08-06 06:29:20276bool AreValidRegisterProtocolHandlerArguments(const std::string& protocol,
277 const GURL& url,
278 const url::Origin& origin) {
279 ChildProcessSecurityPolicyImpl* policy =
280 ChildProcessSecurityPolicyImpl::GetInstance();
281 if (policy->IsPseudoScheme(protocol))
282 return false;
283
284 url::Origin url_origin = url::Origin::Create(url);
285 if (url_origin.opaque())
286 return false;
287
288 if (!url_origin.IsSameOriginWith(origin))
289 return false;
290
291 return true;
292}
293
yilkal796e89e2019-10-02 23:58:28294void RecordMaxFrameCountUMA(size_t max_frame_count) {
295 UMA_HISTOGRAM_COUNTS_10000("Navigation.MainFrame.MaxFrameCount",
296 max_frame_count);
297}
298
lukasza6f8ac622017-06-06 03:10:20299} // namespace
300
Erik Chenbb8e738e2018-04-28 14:10:43301std::unique_ptr<WebContents> WebContents::Create(
302 const WebContents::CreateParams& params) {
Albert J. Wong65fe64d2019-09-20 02:48:14303 return WebContentsImpl::Create(params);
304}
305
306std::unique_ptr<WebContentsImpl> WebContentsImpl::Create(
307 const CreateParams& params) {
308 return CreateWithOpener(params, FindOpenerRFH(params));
[email protected]d1198fd2012-08-13 22:50:19309}
310
erikchenade1fef2018-05-02 22:07:57311std::unique_ptr<WebContents> WebContents::CreateWithSessionStorage(
[email protected]54944cde2012-12-09 09:24:59312 const WebContents::CreateParams& params,
[email protected]fdac6ade2013-07-20 01:06:30313 const SessionStorageNamespaceMap& session_storage_namespace_map) {
erikchenade1fef2018-05-02 22:07:57314 std::unique_ptr<WebContentsImpl> new_contents(
315 new WebContentsImpl(params.browser_context));
Ian Clelland5cbaaf82017-11-27 22:00:03316 RenderFrameHostImpl* opener_rfh = FindOpenerRFH(params);
317 FrameTreeNode* opener = nullptr;
318 if (opener_rfh)
319 opener = opener_rfh->frame_tree_node();
320 new_contents->SetOpenerForNewContents(opener, params.opener_suppressed);
[email protected]fdac6ade2013-07-20 01:06:30321
jdoerrie55ec69d2018-10-08 13:34:46322 for (auto it = session_storage_namespace_map.begin();
323 it != session_storage_namespace_map.end(); ++it) {
[email protected]fdac6ade2013-07-20 01:06:30324 new_contents->GetController()
325 .SetSessionStorageNamespace(it->first, it->second.get());
326 }
327
Sean Toppingbbc4a2bd2019-01-30 02:16:14328 WebContentsImpl* outer_web_contents = nullptr;
Sam McNally69a33772018-06-27 16:54:12329 if (params.guest_delegate) {
330 // This makes |new_contents| act as a guest.
331 // For more info, see comment above class BrowserPluginGuest.
Albert J. Wongc55cc64d2018-10-12 02:50:04332 BrowserPluginGuest::CreateInWebContents(new_contents.get(),
333 params.guest_delegate);
Sean Toppingbbc4a2bd2019-01-30 02:16:14334 outer_web_contents = static_cast<WebContentsImpl*>(
335 params.guest_delegate->GetOwnerWebContents());
Sam McNally69a33772018-06-27 16:54:12336 }
337
[email protected]54944cde2012-12-09 09:24:59338 new_contents->Init(params);
Sean Toppingbbc4a2bd2019-01-30 02:16:14339 if (outer_web_contents)
340 outer_web_contents->InnerWebContentsCreated(new_contents.get());
[email protected]d1198fd2012-08-13 22:50:19341 return new_contents;
[email protected]a81343d232011-12-27 07:39:20342}
[email protected]746d3052012-05-22 15:15:47343
benwells5e7260d2017-03-20 23:36:33344void WebContentsImpl::FriendWrapper::AddCreatedCallbackForTesting(
nickf9acfbe2014-12-23 19:12:37345 const CreatedCallback& callback) {
[email protected]cbb1ef592013-06-05 19:49:46346 g_created_callbacks.Get().push_back(callback);
347}
348
benwells5e7260d2017-03-20 23:36:33349void WebContentsImpl::FriendWrapper::RemoveCreatedCallbackForTesting(
nickf9acfbe2014-12-23 19:12:37350 const CreatedCallback& callback) {
[email protected]cbb1ef592013-06-05 19:49:46351 for (size_t i = 0; i < g_created_callbacks.Get().size(); ++i) {
Robert Liao1db5a352019-06-05 20:57:34352 if (g_created_callbacks.Get().at(i) == callback) {
[email protected]cbb1ef592013-06-05 19:49:46353 g_created_callbacks.Get().erase(g_created_callbacks.Get().begin() + i);
354 return;
355 }
356 }
357}
358
avidf38c952015-10-27 13:45:13359WebContents* WebContents::FromRenderViewHost(RenderViewHost* rvh) {
360 if (!rvh)
361 return nullptr;
[email protected]746d3052012-05-22 15:15:47362 return rvh->GetDelegate()->GetAsWebContents();
363}
364
[email protected]a86c0e962013-12-17 17:10:39365WebContents* WebContents::FromRenderFrameHost(RenderFrameHost* rfh) {
avidf38c952015-10-27 13:45:13366 if (!rfh)
367 return nullptr;
368 return static_cast<RenderFrameHostImpl*>(rfh)->delegate()->GetAsWebContents();
[email protected]a86c0e962013-12-17 17:10:39369}
370
ananta4b7467a52016-09-23 01:42:38371WebContents* WebContents::FromFrameTreeNodeId(int frame_tree_node_id) {
372 FrameTreeNode* frame_tree_node =
373 FrameTreeNode::GloballyFindByID(frame_tree_node_id);
anantab8cfa3b2016-10-14 22:43:19374 if (!frame_tree_node)
375 return nullptr;
jam8c4edd02017-05-06 18:50:33376 return WebContentsImpl::FromFrameTreeNode(frame_tree_node);
ananta4b7467a52016-09-23 01:42:38377}
378
leon.han552e9de2017-02-09 14:37:30379void WebContents::SetScreenOrientationDelegate(
380 ScreenOrientationDelegate* delegate) {
381 ScreenOrientationProvider::SetDelegate(delegate);
382}
383
[email protected]7fff43e2013-05-21 20:21:10384// WebContentsImpl::DestructionObserver ----------------------------------------
385
386class WebContentsImpl::DestructionObserver : public WebContentsObserver {
387 public:
388 DestructionObserver(WebContentsImpl* owner, WebContents* watched_contents)
389 : WebContentsObserver(watched_contents),
390 owner_(owner) {
391 }
392
393 // WebContentsObserver:
dchengc2282aa2014-10-21 12:07:58394 void WebContentsDestroyed() override {
[email protected]12a46832014-05-09 13:35:58395 owner_->OnWebContentsDestroyed(
396 static_cast<WebContentsImpl*>(web_contents()));
[email protected]7fff43e2013-05-21 20:21:10397 }
398
399 private:
400 WebContentsImpl* owner_;
401
402 DISALLOW_COPY_AND_ASSIGN(DestructionObserver);
403};
404
Joel Hockey85b379d2017-12-11 10:42:13405// WebContentsImpl::ColorChooser ----------------------------------------------
Joel Hockey163835a2017-12-20 11:51:57406class WebContentsImpl::ColorChooser : public blink::mojom::ColorChooser {
Joel Hockey85b379d2017-12-11 10:42:13407 public:
408 ColorChooser(content::ColorChooser* chooser,
Julie Jeongeun Kim8e2879e2019-08-06 23:55:20409 mojo::PendingReceiver<blink::mojom::ColorChooser> receiver,
410 mojo::PendingRemote<blink::mojom::ColorChooserClient> client)
Joel Hockey85b379d2017-12-11 10:42:13411 : chooser_(chooser),
Julie Jeongeun Kim8e2879e2019-08-06 23:55:20412 receiver_(this, std::move(receiver)),
Joel Hockey85b379d2017-12-11 10:42:13413 client_(std::move(client)) {
Julie Jeongeun Kim8e2879e2019-08-06 23:55:20414 receiver_.set_disconnect_handler(
Joel Hockey85b379d2017-12-11 10:42:13415 base::BindOnce([](content::ColorChooser* chooser) { chooser->End(); },
416 base::Unretained(chooser)));
417 }
[email protected]8ed16472014-04-11 19:02:48418
Joel Hockey85b379d2017-12-11 10:42:13419 ~ColorChooser() override { chooser_->End(); }
[email protected]8ed16472014-04-11 19:02:48420
Joel Hockey85b379d2017-12-11 10:42:13421 void SetSelectedColor(SkColor color) override {
422 chooser_->SetSelectedColor(color);
423 }
424
425 void DidChooseColorInColorChooser(SkColor color) {
426 client_->DidChooseColor(color);
427 }
428
429 private:
430 // Color chooser that was opened by this tab.
431 std::unique_ptr<content::ColorChooser> chooser_;
432
Julie Jeongeun Kim8e2879e2019-08-06 23:55:20433 // mojo receiver.
434 mojo::Receiver<blink::mojom::ColorChooser> receiver_;
Joel Hockey85b379d2017-12-11 10:42:13435
436 // mojo renderer client.
Julie Jeongeun Kim8e2879e2019-08-06 23:55:20437 mojo::Remote<blink::mojom::ColorChooserClient> client_;
Joel Hockey85b379d2017-12-11 10:42:13438};
nicka0ac8382016-12-15 23:59:23439
lazyboy6ec48b2a2015-06-29 15:18:14440// WebContentsImpl::WebContentsTreeNode ----------------------------------------
lfg601233692017-03-06 22:45:44441WebContentsImpl::WebContentsTreeNode::WebContentsTreeNode(
442 WebContentsImpl* current_web_contents)
443 : current_web_contents_(current_web_contents),
444 outer_web_contents_(nullptr),
lazyboy6ec48b2a2015-06-29 15:18:14445 outer_contents_frame_tree_node_id_(
avallee0206f782016-07-28 18:55:33446 FrameTreeNode::kFrameTreeNodeInvalidId),
lfg601233692017-03-06 22:45:44447 focused_web_contents_(current_web_contents) {}
lazyboy6ec48b2a2015-06-29 15:18:14448
Lucas Furukawa Gadani3d38ec152018-10-26 20:55:18449WebContentsImpl::WebContentsTreeNode::~WebContentsTreeNode() {}
lazyboy6ec48b2a2015-06-29 15:18:14450
Lucas Furukawa Gadani99125822019-01-03 15:41:49451std::unique_ptr<WebContents>
452WebContentsImpl::WebContentsTreeNode::DisconnectFromOuterWebContents() {
453 std::unique_ptr<WebContents> inner_contents =
454 outer_web_contents_->node_.DetachInnerWebContents(current_web_contents_);
455 OuterContentsFrameTreeNode()->RemoveObserver(this);
456 outer_contents_frame_tree_node_id_ = FrameTreeNode::kFrameTreeNodeInvalidId;
457 outer_web_contents_ = nullptr;
458 return inner_contents;
459}
460
paulmeyerfeafc2d2017-04-25 21:46:40461void WebContentsImpl::WebContentsTreeNode::AttachInnerWebContents(
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:50462 std::unique_ptr<WebContents> inner_web_contents,
463 RenderFrameHostImpl* render_frame_host) {
464 WebContentsImpl* inner_web_contents_impl =
465 static_cast<WebContentsImpl*>(inner_web_contents.get());
466 WebContentsTreeNode& inner_web_contents_node = inner_web_contents_impl->node_;
467
468 inner_web_contents_node.focused_web_contents_ = nullptr;
469 inner_web_contents_node.outer_web_contents_ = current_web_contents_;
470 inner_web_contents_node.outer_contents_frame_tree_node_id_ =
471 render_frame_host->frame_tree_node()->frame_tree_node_id();
472
Lucas Furukawa Gadani3d38ec152018-10-26 20:55:18473 inner_web_contents_.push_back(std::move(inner_web_contents));
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:50474
475 render_frame_host->frame_tree_node()->AddObserver(&inner_web_contents_node);
paulmeyerfeafc2d2017-04-25 21:46:40476}
477
Lucas Furukawa Gadani3d38ec152018-10-26 20:55:18478std::unique_ptr<WebContents>
479WebContentsImpl::WebContentsTreeNode::DetachInnerWebContents(
paulmeyerfeafc2d2017-04-25 21:46:40480 WebContentsImpl* inner_web_contents) {
Lucas Furukawa Gadani3d38ec152018-10-26 20:55:18481 std::unique_ptr<WebContents> detached_contents;
482 for (std::unique_ptr<WebContents>& web_contents : inner_web_contents_) {
483 if (web_contents.get() == inner_web_contents) {
484 detached_contents = std::move(web_contents);
485 std::swap(web_contents, inner_web_contents_.back());
486 inner_web_contents_.pop_back();
487 return detached_contents;
488 }
489 }
490
491 NOTREACHED();
492 return nullptr;
paulmeyerfeafc2d2017-04-25 21:46:40493}
494
lfga3e2bdc2017-03-07 20:44:01495FrameTreeNode*
496WebContentsImpl::WebContentsTreeNode::OuterContentsFrameTreeNode() const {
497 return FrameTreeNode::GloballyFindByID(outer_contents_frame_tree_node_id_);
lfg601233692017-03-06 22:45:44498}
499
lfga3e2bdc2017-03-07 20:44:01500void WebContentsImpl::WebContentsTreeNode::OnFrameTreeNodeDestroyed(
501 FrameTreeNode* node) {
502 DCHECK_EQ(outer_contents_frame_tree_node_id_, node->frame_tree_node_id())
503 << "WebContentsTreeNode should only receive notifications for the "
504 "FrameTreeNode in its outer WebContents that hosts it.";
Lucas Furukawa Gadani3d38ec152018-10-26 20:55:18505
506 // Deletes |this| too.
507 outer_web_contents_->node_.DetachInnerWebContents(current_web_contents_);
lazyboy6ec48b2a2015-06-29 15:18:14508}
509
avallee0206f782016-07-28 18:55:33510void WebContentsImpl::WebContentsTreeNode::SetFocusedWebContents(
511 WebContentsImpl* web_contents) {
512 DCHECK(!outer_web_contents())
513 << "Only the outermost WebContents tracks focus.";
514 focused_web_contents_ = web_contents;
515}
516
paulmeyerfeafc2d2017-04-25 21:46:40517WebContentsImpl*
518WebContentsImpl::WebContentsTreeNode::GetInnerWebContentsInFrame(
519 const FrameTreeNode* frame) {
520 auto ftn_id = frame->frame_tree_node_id();
Lucas Furukawa Gadani3d38ec152018-10-26 20:55:18521 for (auto& contents : inner_web_contents_) {
522 WebContentsImpl* impl = static_cast<WebContentsImpl*>(contents.get());
523 if (impl->node_.outer_contents_frame_tree_node_id() == ftn_id) {
524 return impl;
paulmeyerfeafc2d2017-04-25 21:46:40525 }
526 }
527 return nullptr;
528}
529
Lucas Furukawa Gadani3d38ec152018-10-26 20:55:18530std::vector<WebContentsImpl*>
531WebContentsImpl::WebContentsTreeNode::GetInnerWebContents() const {
532 std::vector<WebContentsImpl*> inner_web_contents;
533 for (auto& contents : inner_web_contents_)
534 inner_web_contents.push_back(static_cast<WebContentsImpl*>(contents.get()));
535
536 return inner_web_contents;
paulmeyerfeafc2d2017-04-25 21:46:40537}
538
[email protected]b172aee2012-04-10 17:05:26539// WebContentsImpl -------------------------------------------------------------
[email protected]420ae012009-04-24 05:16:32540
alexmose201c7cd2015-06-10 17:14:21541WebContentsImpl::WebContentsImpl(BrowserContext* browser_context)
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28542 : delegate_(nullptr),
[email protected]69e797f2013-04-30 01:10:22543 controller_(this, browser_context),
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28544 render_view_host_delegate_view_(nullptr),
alexmose201c7cd2015-06-10 17:14:21545 created_with_opener_(false),
Lucas Furukawa Gadani99125822019-01-03 15:41:49546 node_(this),
[email protected]6ea6bdf2013-12-06 13:35:01547 frame_tree_(new NavigatorImpl(&controller_, this),
blundellc57b93f2014-10-29 13:19:57548 this,
549 this,
550 this,
551 this),
[email protected]6dfed692014-05-22 04:18:03552 is_load_to_different_document_(false),
[email protected]443b80e2010-12-14 00:42:23553 crashed_status_(base::TERMINATION_STATUS_STILL_RUNNING),
554 crashed_error_code_(0),
[email protected]d5f942ba2008-09-26 19:30:34555 waiting_for_response_(false),
[email protected]fcf75d42013-12-03 20:11:26556 load_state_(net::LOAD_STATE_IDLE, base::string16()),
[email protected]094e5b22009-09-25 04:23:56557 upload_size_(0),
558 upload_position_(0),
dfalcantarae6b7b46752015-07-10 18:14:16559 is_resume_pending_(false),
creisadca9862017-06-16 19:12:11560 interstitial_page_(nullptr),
[email protected]b4c84012014-04-28 19:51:10561 has_accessed_initial_document_(false),
Collin Bakerc5259962019-11-14 17:51:58562 visible_capturer_count_(0),
563 hidden_capturer_count_(0),
[email protected]fdd61c62009-04-22 19:22:57564 is_being_destroyed_(false),
creis97867aa2017-02-17 18:39:45565 is_notifying_observers_(false),
[email protected]fdd61c62009-04-22 19:22:57566 notify_disconnection_(false),
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28567 dialog_manager_(nullptr),
[email protected]7ab1e7d62009-10-14 23:32:01568 is_showing_before_unload_dialog_(false),
gab0dccfef2015-05-20 18:43:39569 last_active_time_(base::TimeTicks::Now()),
[email protected]ebf40a72010-07-22 01:46:38570 closed_by_user_gesture_(false),
danakj938b37a62019-09-24 18:35:54571 minimum_zoom_percent_(
572 static_cast<int>(blink::kMinimumPageZoomFactor * 100)),
573 maximum_zoom_percent_(
574 static_cast<int>(blink::kMaximumPageZoomFactor * 100)),
w.shackleton49bcd392016-01-06 17:38:22575 zoom_scroll_remainder_(0),
alexmosc9e76ec2016-05-16 22:59:37576 fullscreen_widget_process_id_(ChildProcessHost::kInvalidUniqueID),
[email protected]5dcaf8e2013-12-28 01:31:42577 fullscreen_widget_routing_id_(MSG_ROUTING_NONE),
[email protected]0b431992014-06-24 00:08:03578 fullscreen_widget_had_focus_at_shutdown_(false),
[email protected]34ff1cfc2014-08-20 06:16:05579 force_disable_overscroll_content_(false),
[email protected]95640212014-07-26 18:14:30580 last_dialog_suppressed_(false),
waffles1c12bf402016-02-10 17:45:48581 accessibility_mode_(
Dominic Mazzoni21fb0282019-02-13 18:32:47582 GetContentClient()->browser()->GetAXModeForBrowserContext(
583 browser_context)),
avayvodcc85bbd2015-08-28 19:11:15584 audio_stream_monitor_(this),
Thomas Guilbert882c4cec2019-02-14 05:55:45585 media_web_contents_observer_(
586 std::make_unique<MediaWebContentsObserver>(this)),
mcnee336ea2c2017-05-23 22:50:59587#if !defined(OS_ANDROID)
mcnee432e47d2015-11-09 19:37:46588 page_scale_factor_is_one_(true),
mcnee336ea2c2017-05-23 22:50:59589#endif // !defined(OS_ANDROID)
qinmin72e8bd02016-10-21 19:35:37590 is_overlay_content_(false),
ekaramadf6750aa2017-06-06 18:29:42591 showing_context_menu_(false),
W. James MacLean62198672019-06-04 16:46:10592 text_autosizer_page_info_({0, 0, 1.f}),
Jeremy Roman3bca4bf2019-07-11 03:41:25593 had_inner_webcontents_(false) {
danakjf4b9e942019-11-29 15:43:04594 frame_tree_.SetFrameRemoveListener(base::BindRepeating(
595 &WebContentsImpl::OnFrameRemoved, base::Unretained(this)));
brettw4b461082016-11-19 18:55:16596#if BUILDFLAG(ENABLE_PLUGINS)
zqzhang181047e62016-07-01 13:37:17597 pepper_playback_observer_.reset(new PepperPlaybackObserver(this));
598#endif
blundelle75a8f92017-03-27 08:11:17599
Becca Hughes86334622018-07-09 21:29:38600#if defined(OS_ANDROID)
Elly Fong-Jonescdd7ae82019-02-20 23:26:33601 display_cutout_host_impl_ = std::make_unique<DisplayCutoutHostImpl>(this);
Becca Hughes86334622018-07-09 21:29:38602#endif
[email protected]332af7732009-03-11 13:21:35603}
initial.commit09911bf2008-07-26 23:55:29604
[email protected]b172aee2012-04-10 17:05:26605WebContentsImpl::~WebContentsImpl() {
creisba91ba82017-05-12 22:56:19606 // Imperfect sanity check against double free, given some crashes unexpectedly
607 // observed in the wild.
608 CHECK(!is_being_destroyed_);
609
610 // We generally keep track of is_being_destroyed_ to let other features know
611 // to avoid certain actions during destruction.
[email protected]420ae012009-04-24 05:16:32612 is_being_destroyed_ = true;
613
creis97867aa2017-02-17 18:39:45614 // A WebContents should never be deleted while it is notifying observers,
creisba91ba82017-05-12 22:56:19615 // since this will lead to a use-after-free as it continues to notify later
creis97867aa2017-02-17 18:39:45616 // observers.
617 CHECK(!is_notifying_observers_);
618
kenrb2a565f82015-09-02 20:24:59619 rwh_input_event_router_.reset();
620
Julie Jeongeun Kim7d0986172019-12-04 03:17:25621 for (auto& entry : receiver_sets_)
622 entry.second->CloseAllReceivers();
rockotf62002a2016-09-15 00:08:59623
avallee0206f782016-07-28 18:55:33624 WebContentsImpl* outermost = GetOutermostWebContents();
avallee9993fca2016-11-17 06:16:50625 if (this != outermost && ContainsOrIsFocusedWebContents()) {
avallee0206f782016-07-28 18:55:33626 // If the current WebContents is in focus, unset it.
avallee9993fca2016-11-17 06:16:50627 outermost->SetAsFocusedWebContentsIfNecessary();
avallee0206f782016-07-28 18:55:33628 }
629
Nick Carterd73635b2018-03-13 18:31:41630 if (mouse_lock_widget_)
631 mouse_lock_widget_->RejectMouseLockOrUnlockIfNecessary();
632
dcheng72be33c2016-01-21 04:11:31633 for (FrameTreeNode* node : frame_tree_.Nodes()) {
634 // Delete all RFHs pending shutdown, which will lead the corresponding RVHs
635 // to be shutdown and be deleted as well.
dcheng57e39e22016-01-21 00:25:38636 node->render_manager()->ClearRFHsPendingShutdown();
dcheng57e39e22016-01-21 00:25:38637 node->render_manager()->ClearWebUIInstances();
dcheng72be33c2016-01-21 04:11:31638 }
carlosk35f35af2015-12-01 10:55:40639
dcheng57e39e22016-01-21 00:25:38640 for (RenderWidgetHostImpl* widget : created_widgets_)
641 widget->DetachDelegate();
[email protected]b24b68a2012-09-24 21:57:26642 created_widgets_.clear();
643
[email protected]3ab9cb82011-06-03 18:02:07644 // Clear out any JavaScript state.
avi5d3b8692016-10-12 22:00:46645 if (dialog_manager_) {
avi6879fcf2017-01-21 05:27:53646 dialog_manager_->CancelDialogs(this, /*reset_state=*/true);
avi5d3b8692016-10-12 22:00:46647 }
[email protected]3ab9cb82011-06-03 18:02:07648
Joel Hockey85b379d2017-12-11 10:42:13649 color_chooser_.reset();
Rakina Zata Amni16196122018-11-14 02:14:00650 find_request_manager_.reset();
[email protected]da8543762012-03-20 08:52:20651
[email protected]420ae012009-04-24 05:16:32652 NotifyDisconnected();
[email protected]420ae012009-04-24 05:16:32653
[email protected]ca13a442012-04-17 14:00:12654 // Notify any observer that have a reference on this WebContents.
[email protected]8ff00d72012-10-23 19:12:21655 NotificationService::current()->Notify(
656 NOTIFICATION_WEB_CONTENTS_DESTROYED,
657 Source<WebContents>(this),
658 NotificationService::NoDetails());
[email protected]420ae012009-04-24 05:16:32659
Arthur Sonzogni5a9a88c2018-10-18 13:16:00660 // Destroy all subframes now. This notifies observers.
661 GetMainFrame()->ResetChildren();
[email protected]df0c843f2014-05-23 20:37:23662 GetRenderManager()->ResetProxyHosts();
[email protected]c06c58c2014-03-12 20:31:59663
clamy0e119882015-07-31 16:12:33664 // Manually call the observer methods for the root frame tree node. It is
665 // necessary to manually delete all objects tracking navigations
666 // (NavigationHandle, NavigationRequest) for observers to be properly
667 // notified of these navigations stopping before the WebContents is
668 // destroyed.
[email protected]df0c843f2014-05-23 20:37:23669 RenderFrameHostManager* root = GetRenderManager();
naskob21fe4872015-02-24 14:15:56670
Lowell Manners20250112019-10-18 15:06:15671 GetController().GetBackForwardCache().Shutdown();
672
naskob21fe4872015-02-24 14:15:56673 root->current_frame_host()->SetRenderFrameCreated(false);
clamyfc3850d2018-02-23 14:35:28674 root->current_frame_host()->ResetNavigationRequests();
clamy0e119882015-07-31 16:12:33675
Arthur Hemeryc3380172018-01-22 14:00:17676 // Do not update state as the WebContents is being destroyed.
Arthur Hemery241b9392019-10-24 11:08:41677 frame_tree_.root()->ResetNavigationRequest(true);
Arthur Hemeryc3380172018-01-22 14:00:17678 if (root->speculative_frame_host()) {
danakjf2342432019-05-15 21:46:09679 root->speculative_frame_host()->DeleteRenderFrame(
680 FrameDeleteIntention::kSpeculativeMainFrameForShutdown);
Arthur Hemeryc3380172018-01-22 14:00:17681 root->speculative_frame_host()->SetRenderFrameCreated(false);
clamyfc3850d2018-02-23 14:35:28682 root->speculative_frame_host()->ResetNavigationRequests();
clamy0e119882015-07-31 16:12:33683 }
[email protected]f273ee52013-10-18 16:05:27684
brettw4b461082016-11-19 18:55:16685#if BUILDFLAG(ENABLE_PLUGINS)
zqzhang181047e62016-07-01 13:37:17686 // Call this before WebContentsDestroyed() is broadcasted since
687 // AudioFocusManager will be destroyed after that.
688 pepper_playback_observer_.reset();
689#endif // defined(ENABLED_PLUGINS)
690
Chris Hamiltondf0d72cd2018-05-29 16:23:53691 // If audio is playing then notify external observers of the audio stream
692 // disappearing.
693 if (is_currently_audible_) {
694 is_currently_audible_ = false;
695 for (auto& observer : observers_)
696 observer.OnAudioStateChanged(false);
697
698 if (GetOuterWebContents())
699 GetOuterWebContents()->OnAudioStateChanged();
700 }
701
ericwilligersde386342016-10-19 02:35:09702 for (auto& observer : observers_)
703 observer.FrameDeleted(root->current_frame_host());
naskoa83483fb2015-02-27 16:57:10704
ericwilligersde386342016-10-19 02:35:09705 for (auto& observer : observers_)
706 observer.RenderViewDeleted(root->current_host());
[email protected]f273ee52013-10-18 16:05:27707
Kevin McNee576bb5d82019-05-29 16:47:51708#if defined(OS_ANDROID)
709 // For simplicity, destroy the Java WebContents before we notify of the
710 // destruction of the WebContents.
711 ClearWebContentsAndroid();
712#endif
713
ericwilligersde386342016-10-19 02:35:09714 for (auto& observer : observers_)
715 observer.WebContentsDestroyed();
[email protected]12a46832014-05-09 13:35:58716
Becca Hughesd11d6502018-07-31 17:01:28717 if (display_cutout_host_impl_)
718 display_cutout_host_impl_->WebContentsDestroyed();
719
ericwilligersde386342016-10-19 02:35:09720 for (auto& observer : observers_)
721 observer.ResetWebContents();
[email protected]232a5812011-03-04 22:42:08722
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28723 SetDelegate(nullptr);
[email protected]b5a1d11c2011-02-17 03:09:42724}
725
erikchen50735fd2018-05-05 15:08:33726std::unique_ptr<WebContentsImpl> WebContentsImpl::CreateWithOpener(
[email protected]54944cde2012-12-09 09:24:59727 const WebContents::CreateParams& params,
Ian Clelland5cbaaf82017-11-27 22:00:03728 RenderFrameHostImpl* opener_rfh) {
[email protected]e11f0e92013-06-12 15:12:03729 TRACE_EVENT0("browser", "WebContentsImpl::CreateWithOpener");
Ian Clelland5cbaaf82017-11-27 22:00:03730 FrameTreeNode* opener = nullptr;
731 if (opener_rfh)
732 opener = opener_rfh->frame_tree_node();
erikchen50735fd2018-05-05 15:08:33733 std::unique_ptr<WebContentsImpl> new_contents(
734 new WebContentsImpl(params.browser_context));
lukasza6f8ac622017-06-06 03:10:20735 new_contents->SetOpenerForNewContents(opener, params.opener_suppressed);
alexmose201c7cd2015-06-10 17:14:21736
alexmosaedfc6f2016-01-21 23:57:38737 // If the opener is sandboxed, a new popup must inherit the opener's sandbox
738 // flags, and these flags take effect immediately. An exception is if the
739 // opener's sandbox flags lack the PropagatesToAuxiliaryBrowsingContexts
740 // bit (which is controlled by the "allow-popups-to-escape-sandbox" token).
741 // See https://html.spec.whatwg.org/#attr-iframe-sandbox.
lukasza6f8ac622017-06-06 03:10:20742 FrameTreeNode* new_root = new_contents->GetFrameTree()->root();
alexmosaedfc6f2016-01-21 23:57:38743 if (opener) {
Ian Clelland5cbaaf82017-11-27 22:00:03744 blink::WebSandboxFlags opener_flags = opener_rfh->active_sandbox_flags();
alexmosaedfc6f2016-01-21 23:57:38745 const blink::WebSandboxFlags inherit_flag =
Blink Reformat1c4d759e2017-04-09 16:34:54746 blink::WebSandboxFlags::kPropagatesToAuxiliaryBrowsingContexts;
Ehsan Karamad39407082019-02-19 23:38:19747 bool sandbox_propagates_to_auxilary_context =
748 (opener_flags & inherit_flag) == inherit_flag;
749 if (sandbox_propagates_to_auxilary_context)
Ian Clellandcdc4f312017-10-13 22:24:12750 new_root->SetPendingFramePolicy({opener_flags, {}});
Ehsan Karamad39407082019-02-19 23:38:19751 if (opener_flags == blink::WebSandboxFlags::kNone ||
752 sandbox_propagates_to_auxilary_context) {
753 // TODO(ekaramad, iclelland): Do not propagate feature policies from non-
754 // sandboxed disowned openers (rel=noopener).
755 // If the current page is not sandboxed, or if the sandbox is to propagate
756 // to the popups then opener's feature policy will apply to the new popup
757 // as well.
758 new_root->SetOpenerFeaturePolicyState(
Ehsan Karamad34d10682019-02-26 20:55:04759 opener_rfh->feature_policy()->GetFeatureState());
alexmosaedfc6f2016-01-21 23:57:38760 }
761 }
762
mark a. foltzef394fce2017-10-21 09:11:02763 // Apply starting sandbox flags.
Luna Luc3fdacdf2017-11-08 04:48:53764 blink::FramePolicy frame_policy(new_root->pending_frame_policy());
mark a. foltzef394fce2017-10-21 09:11:02765 frame_policy.sandbox_flags |= params.starting_sandbox_flags;
766 new_root->SetPendingFramePolicy(frame_policy);
Charlie Hu5ffc0152019-12-06 15:59:53767 new_root->CommitFramePolicy(frame_policy);
mark a. foltzef394fce2017-10-21 09:11:02768
alexmose201c7cd2015-06-10 17:14:21769 // This may be true even when opener is null, such as when opening blocked
770 // popups.
alexmos090fae8e2015-05-28 17:09:28771 if (params.created_with_opener)
772 new_contents->created_with_opener_ = true;
773
Sean Toppingbbc4a2bd2019-01-30 02:16:14774 WebContentsImpl* outer_web_contents = nullptr;
[email protected]4858e432014-06-23 18:14:17775 if (params.guest_delegate) {
[email protected]83100cd2014-05-10 11:50:06776 // This makes |new_contents| act as a guest.
777 // For more info, see comment above class BrowserPluginGuest.
Albert J. Wongc55cc64d2018-10-12 02:50:04778 BrowserPluginGuest::CreateInWebContents(new_contents.get(),
779 params.guest_delegate);
Sean Toppingbbc4a2bd2019-01-30 02:16:14780 outer_web_contents = static_cast<WebContentsImpl*>(
781 params.guest_delegate->GetOwnerWebContents());
[email protected]83100cd2014-05-10 11:50:06782 }
mark a. foltzef394fce2017-10-21 09:11:02783
[email protected]54944cde2012-12-09 09:24:59784 new_contents->Init(params);
Sean Toppingbbc4a2bd2019-01-30 02:16:14785 if (outer_web_contents)
786 outer_web_contents->InnerWebContentsCreated(new_contents.get());
[email protected]d1198fd2012-08-13 22:50:19787 return new_contents;
788}
789
[email protected]95640212014-07-26 18:14:30790// static
791std::vector<WebContentsImpl*> WebContentsImpl::GetAllWebContents() {
792 std::vector<WebContentsImpl*> result;
dcheng59716272016-04-09 05:19:08793 std::unique_ptr<RenderWidgetHostIterator> widgets(
[email protected]95640212014-07-26 18:14:30794 RenderWidgetHostImpl::GetRenderWidgetHosts());
[email protected]95640212014-07-26 18:14:30795 while (RenderWidgetHost* rwh = widgets->GetNextHost()) {
[email protected]95640212014-07-26 18:14:30796 RenderViewHost* rvh = RenderViewHost::From(rwh);
797 if (!rvh)
798 continue;
799 WebContents* web_contents = WebContents::FromRenderViewHost(rvh);
800 if (!web_contents)
801 continue;
avidf38c952015-10-27 13:45:13802 if (web_contents->GetRenderViewHost() != rvh)
803 continue;
804 // Because a WebContents can only have one current RVH at a time, there will
805 // be no duplicate WebContents here.
806 result.push_back(static_cast<WebContentsImpl*>(web_contents));
[email protected]95640212014-07-26 18:14:30807 }
808 return result;
809}
810
clamya16aa8172015-05-26 13:07:25811// static
812WebContentsImpl* WebContentsImpl::FromFrameTreeNode(
paulmeyerfeafc2d2017-04-25 21:46:40813 const FrameTreeNode* frame_tree_node) {
clamya16aa8172015-05-26 13:07:25814 return static_cast<WebContentsImpl*>(
815 WebContents::FromRenderFrameHost(frame_tree_node->current_frame_host()));
816}
817
clamy0d32d6d2015-11-24 11:16:26818// static
819WebContents* WebContentsImpl::FromRenderFrameHostID(int render_process_host_id,
820 int render_frame_host_id) {
Min Qina1150112019-02-15 01:47:49821 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) ||
822 !BrowserThread::IsThreadInitialized(BrowserThread::UI));
clamy0d32d6d2015-11-24 11:16:26823 RenderFrameHost* render_frame_host =
824 RenderFrameHost::FromID(render_process_host_id, render_frame_host_id);
825 if (!render_frame_host)
826 return nullptr;
827
828 return WebContents::FromRenderFrameHost(render_frame_host);
829}
830
paulmeyerfeafc2d2017-04-25 21:46:40831// static
832WebContentsImpl* WebContentsImpl::FromOuterFrameTreeNode(
833 const FrameTreeNode* frame_tree_node) {
834 return WebContentsImpl::FromFrameTreeNode(frame_tree_node)
835 ->node_.GetInnerWebContentsInFrame(frame_tree_node);
836}
837
[email protected]b0936d22013-11-28 06:47:36838RenderFrameHostManager* WebContentsImpl::GetRenderManagerForTesting() {
[email protected]fa944cb82013-11-15 17:51:21839 return GetRenderManager();
[email protected]765187182012-01-11 23:59:28840}
841
nicka0ac8382016-12-15 23:59:23842bool WebContentsImpl::OnMessageReceived(RenderViewHostImpl* render_view_host,
[email protected]7bb761892012-07-20 09:32:47843 const IPC::Message& message) {
nicka0ac8382016-12-15 23:59:23844 for (auto& observer : observers_) {
Lukasz Anforowicz89f2c1b2017-08-30 21:36:46845 // TODO(nick, creis): https://crbug.com/758026: Replace all uses of this
846 // variant of OnMessageReceived with the version that takes a
847 // RenderFrameHost, and then delete it.
nicka0ac8382016-12-15 23:59:23848 if (observer.OnMessageReceived(message))
849 return true;
[email protected]64ffefa2014-05-10 12:06:33850 }
[email protected]403415a2011-01-10 18:57:53851
[email protected]724159a2010-12-30 01:11:18852 bool handled = true;
nicka0ac8382016-12-15 23:59:23853 IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(WebContentsImpl, message, render_view_host)
nicka0ac8382016-12-15 23:59:23854 IPC_MESSAGE_HANDLER(ViewHostMsg_PageScaleFactorChanged,
855 OnPageScaleFactorChanged)
W. James MacLean62198672019-06-04 16:46:10856 IPC_MESSAGE_HANDLER(
857 ViewHostMsg_NotifyTextAutosizerPageInfoChangedInLocalMainFrame,
858 OnTextAutosizerPageInfoChanged)
nicka0ac8382016-12-15 23:59:23859#if BUILDFLAG(ENABLE_PLUGINS)
860 IPC_MESSAGE_HANDLER(ViewHostMsg_RequestPpapiBrokerPermission,
861 OnRequestPpapiBrokerPermission)
862#endif
nicka0ac8382016-12-15 23:59:23863 IPC_MESSAGE_UNHANDLED(handled = false)
864 IPC_END_MESSAGE_MAP()
865
866 return handled;
867}
868
869bool WebContentsImpl::OnMessageReceived(RenderFrameHostImpl* render_frame_host,
870 const IPC::Message& message) {
Lukasz Anforowicz02923102017-10-09 18:11:37871 {
872 WebUIImpl* web_ui = render_frame_host->web_ui();
873 if (web_ui && web_ui->OnMessageReceived(message, render_frame_host))
874 return true;
875 }
876
nicka0ac8382016-12-15 23:59:23877 for (auto& observer : observers_) {
878 if (observer.OnMessageReceived(message, render_frame_host))
879 return true;
880 }
881
882 bool handled = true;
883 IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(WebContentsImpl, message, render_frame_host)
[email protected]cfa856d62014-02-22 07:58:40884 IPC_MESSAGE_HANDLER(FrameHostMsg_DomOperationResponse,
885 OnDomOperationResponse)
[email protected]8ed16472014-04-11 19:02:48886 IPC_MESSAGE_HANDLER(FrameHostMsg_DidFinishLoad, OnDidFinishLoad)
avi7830c852015-08-31 23:46:25887 IPC_MESSAGE_HANDLER(FrameHostMsg_DidLoadResourceFromMemoryCache,
[email protected]724159a2010-12-30 01:11:18888 OnDidLoadResourceFromMemoryCache)
avi3a3332622015-09-01 01:18:54889 IPC_MESSAGE_HANDLER(FrameHostMsg_DidRunInsecureContent,
[email protected]724159a2010-12-30 01:11:18890 OnDidRunInsecureContent)
estark910b4572015-12-09 20:55:41891 IPC_MESSAGE_HANDLER(FrameHostMsg_DidDisplayContentWithCertificateErrors,
892 OnDidDisplayContentWithCertificateErrors)
893 IPC_MESSAGE_HANDLER(FrameHostMsg_DidRunContentWithCertificateErrors,
894 OnDidRunContentWithCertificateErrors)
Dave Tapuska549baab2019-03-19 15:52:40895 IPC_MESSAGE_HANDLER(FrameHostMsg_GoToEntryAtOffset, OnGoToEntryAtOffset)
kouhei40f03cb2015-09-24 07:47:01896 IPC_MESSAGE_HANDLER(FrameHostMsg_UpdatePageImportanceSignals,
897 OnUpdatePageImportanceSignals)
lukasza178ff092017-06-07 23:39:50898 IPC_MESSAGE_HANDLER(FrameHostMsg_UpdateFaviconURL, OnUpdateFaviconURL)
brettw4b461082016-11-19 18:55:16899#if BUILDFLAG(ENABLE_PLUGINS)
emaxxe70f5e12015-05-29 11:26:00900 IPC_MESSAGE_HANDLER(FrameHostMsg_PepperInstanceCreated,
901 OnPepperInstanceCreated)
902 IPC_MESSAGE_HANDLER(FrameHostMsg_PepperInstanceDeleted,
903 OnPepperInstanceDeleted)
tommyclie6633ca72014-10-31 00:40:42904 IPC_MESSAGE_HANDLER(FrameHostMsg_PepperPluginHung, OnPepperPluginHung)
zqzhang181047e62016-07-01 13:37:17905 IPC_MESSAGE_HANDLER(FrameHostMsg_PepperStartsPlayback,
906 OnPepperStartsPlayback)
907 IPC_MESSAGE_HANDLER(FrameHostMsg_PepperStopsPlayback,
908 OnPepperStopsPlayback)
tommyclie6633ca72014-10-31 00:40:42909 IPC_MESSAGE_HANDLER(FrameHostMsg_PluginCrashed, OnPluginCrashed)
thestigc4cac8f2014-09-04 21:17:50910#endif
[email protected]724159a2010-12-30 01:11:18911 IPC_MESSAGE_UNHANDLED(handled = false)
[email protected]e44d1342014-05-16 21:29:33912 IPC_END_MESSAGE_MAP()
[email protected]724159a2010-12-30 01:11:18913
[email protected]724159a2010-12-30 01:11:18914 return handled;
915}
916
[email protected]d1198fd2012-08-13 22:50:19917NavigationControllerImpl& WebContentsImpl::GetController() {
[email protected]f5fa20e2011-12-21 22:35:56918 return controller_;
919}
920
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:41921BrowserContext* WebContentsImpl::GetBrowserContext() {
[email protected]a26023822011-12-29 00:23:55922 return controller_.GetBrowserContext();
[email protected]627e0512011-12-21 22:55:30923}
924
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:41925const GURL& WebContentsImpl::GetURL() {
Peter Boström101d7672018-12-15 00:43:59926 return GetVisibleURL();
[email protected]be1f56ab2011-12-22 06:55:31927}
928
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:41929const GURL& WebContentsImpl::GetVisibleURL() {
[email protected]c854a7e2013-05-21 16:42:24930 // We may not have a navigation entry yet.
[email protected]59167c22013-06-03 18:07:32931 NavigationEntry* entry = controller_.GetVisibleEntry();
[email protected]c854a7e2013-05-21 16:42:24932 return entry ? entry->GetVirtualURL() : GURL::EmptyGURL();
933}
934
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:41935const GURL& WebContentsImpl::GetLastCommittedURL() {
[email protected]c854a7e2013-05-21 16:42:24936 // We may not have a navigation entry yet.
937 NavigationEntry* entry = controller_.GetLastCommittedEntry();
938 return entry ? entry->GetVirtualURL() : GURL::EmptyGURL();
939}
940
[email protected]8ff00d72012-10-23 19:12:21941WebContentsDelegate* WebContentsImpl::GetDelegate() {
[email protected]be1f56ab2011-12-22 06:55:31942 return delegate_;
943}
944
[email protected]8ff00d72012-10-23 19:12:21945void WebContentsImpl::SetDelegate(WebContentsDelegate* delegate) {
[email protected]be1f56ab2011-12-22 06:55:31946 // TODO(cbentzel): remove this debugging code?
947 if (delegate == delegate_)
948 return;
949 if (delegate_)
950 delegate_->Detach(this);
951 delegate_ = delegate;
[email protected]a6b73c62013-02-11 23:05:08952 if (delegate_) {
[email protected]be1f56ab2011-12-22 06:55:31953 delegate_->Attach(this);
[email protected]a6b73c62013-02-11 23:05:08954 // Ensure the visible RVH reflects the new delegate's preferences.
[email protected]86f98a22013-03-20 14:35:00955 if (view_)
[email protected]e85165c642014-06-10 14:34:31956 view_->SetOverscrollControllerEnabled(CanOverscrollContent());
Joel Einbinder848001c2017-09-19 21:39:31957 if (GetRenderViewHost())
958 RenderFrameDevToolsAgentHost::WebContentsCreated(this);
[email protected]a6b73c62013-02-11 23:05:08959 }
[email protected]be1f56ab2011-12-22 06:55:31960}
961
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:41962RenderFrameHostImpl* WebContentsImpl::GetMainFrame() {
[email protected]94d0cc12013-12-18 00:07:41963 return frame_tree_.root()->current_frame_host();
[email protected]60eca4eb2013-12-06 00:02:16964}
965
nick53d5cbf2015-04-23 22:50:14966RenderFrameHostImpl* WebContentsImpl::GetFocusedFrame() {
967 FrameTreeNode* focused_node = frame_tree_.GetFocusedFrame();
968 if (!focused_node)
969 return nullptr;
970 return focused_node->current_frame_host();
[email protected]9c9343b2014-03-08 02:56:07971}
972
rob3e2a0732016-01-06 21:22:09973RenderFrameHostImpl* WebContentsImpl::FindFrameByFrameTreeNodeId(
creisf71a2632017-05-04 19:03:50974 int frame_tree_node_id,
975 int process_id) {
976 FrameTreeNode* frame = frame_tree_.FindByID(frame_tree_node_id);
977
978 // Sanity check that this is in the caller's expected process. Otherwise a
979 // recent cross-process navigation may have led to a privilege change that the
980 // caller is not expecting.
981 if (!frame ||
982 frame->current_frame_host()->GetProcess()->GetID() != process_id)
983 return nullptr;
984
985 return frame->current_frame_host();
986}
987
988RenderFrameHostImpl* WebContentsImpl::UnsafeFindFrameByFrameTreeNodeId(
rob3e2a0732016-01-06 21:22:09989 int frame_tree_node_id) {
creisf71a2632017-05-04 19:03:50990 // Beware using this! The RenderFrameHost may have changed since the caller
991 // obtained frame_tree_node_id.
rob3e2a0732016-01-06 21:22:09992 FrameTreeNode* frame = frame_tree_.FindByID(frame_tree_node_id);
993 return frame ? frame->current_frame_host() : nullptr;
994}
995
[email protected]a86c0e962013-12-17 17:10:39996void WebContentsImpl::ForEachFrame(
Lei Zhangebcc6302018-01-12 19:46:45997 const base::RepeatingCallback<void(RenderFrameHost*)>& on_frame) {
dcheng57e39e22016-01-21 00:25:38998 for (FrameTreeNode* node : frame_tree_.Nodes()) {
999 on_frame.Run(node->current_frame_host());
1000 }
[email protected]a86c0e962013-12-17 17:10:391001}
1002
dchengafb53e22016-02-04 08:11:081003std::vector<RenderFrameHost*> WebContentsImpl::GetAllFrames() {
1004 std::vector<RenderFrameHost*> frame_hosts;
1005 for (FrameTreeNode* node : frame_tree_.Nodes())
1006 frame_hosts.push_back(node->current_frame_host());
1007 return frame_hosts;
1008}
1009
lukaszacbdf52e2016-01-15 21:19:511010int WebContentsImpl::SendToAllFrames(IPC::Message* message) {
1011 int number_of_messages = 0;
dchengafb53e22016-02-04 08:11:081012 for (RenderFrameHost* rfh : GetAllFrames()) {
lukasza1a9ab2422016-03-02 18:47:251013 if (!rfh->IsRenderFrameLive())
1014 continue;
1015
dchengafb53e22016-02-04 08:11:081016 ++number_of_messages;
1017 IPC::Message* message_copy = new IPC::Message(*message);
1018 message_copy->set_routing_id(rfh->GetRoutingID());
1019 rfh->Send(message_copy);
1020 }
[email protected]a86c0e962013-12-17 17:10:391021 delete message;
lukaszacbdf52e2016-01-15 21:19:511022 return number_of_messages;
[email protected]a86c0e962013-12-17 17:10:391023}
1024
lfgdb5c4ed2016-03-04 23:09:071025void WebContentsImpl::SendPageMessage(IPC::Message* msg) {
alexmos136fd6e62016-08-15 20:58:411026 frame_tree_.root()->render_manager()->SendPageMessage(msg, nullptr);
lfgdb5c4ed2016-03-04 23:09:071027}
1028
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411029RenderViewHostImpl* WebContentsImpl::GetRenderViewHost() {
[email protected]fa944cb82013-11-15 17:51:211030 return GetRenderManager()->current_host();
[email protected]be1f56ab2011-12-22 06:55:311031}
1032
creis89a0f782015-05-27 16:13:171033void WebContentsImpl::CancelActiveAndPendingDialogs() {
avi5d3b8692016-10-12 22:00:461034 if (dialog_manager_) {
avi6879fcf2017-01-21 05:27:531035 dialog_manager_->CancelDialogs(this, /*reset_state=*/false);
avi5d3b8692016-10-12 22:00:461036 }
creis89a0f782015-05-27 16:13:171037 if (browser_plugin_embedder_)
1038 browser_plugin_embedder_->CancelGuestDialogs();
1039}
1040
naskoc0fceff2015-04-30 15:53:521041void WebContentsImpl::ClosePage() {
1042 GetRenderViewHost()->ClosePage();
1043}
1044
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411045RenderWidgetHostView* WebContentsImpl::GetRenderWidgetHostView() {
[email protected]fa944cb82013-11-15 17:51:211046 return GetRenderManager()->GetRenderWidgetHostView();
[email protected]be1f56ab2011-12-22 06:55:311047}
1048
lfg265a2672016-04-23 03:11:021049RenderWidgetHostView* WebContentsImpl::GetTopLevelRenderWidgetHostView() {
1050 if (GetOuterWebContents())
1051 return GetOuterWebContents()->GetTopLevelRenderWidgetHostView();
1052 return GetRenderManager()->GetRenderWidgetHostView();
1053}
1054
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411055RenderWidgetHostView* WebContentsImpl::GetFullscreenRenderWidgetHostView() {
vmpstr6d9996c82017-02-23 00:43:251056 if (auto* widget_host = GetFullscreenRenderWidgetHost())
ekaramadaeb3c5c2016-11-09 03:22:351057 return widget_host->GetView();
1058 return nullptr;
[email protected]4aebbca2013-09-17 22:26:491059}
1060
[email protected]8ff00d72012-10-23 19:12:211061WebContentsView* WebContentsImpl::GetView() const {
[email protected]be1f56ab2011-12-22 06:55:311062 return view_.get();
1063}
1064
leon.han552e9de2017-02-09 14:37:301065void WebContentsImpl::OnScreenOrientationChange() {
1066 DCHECK(screen_orientation_provider_);
Becca Hughese9e27952018-06-25 17:08:271067 screen_orientation_provider_->OnOrientationChange();
leon.hane4db177a2017-02-07 14:19:161068}
1069
Alan Cutter92d59252019-03-29 02:32:331070base::Optional<SkColor> WebContentsImpl::GetThemeColor() {
Carlos Caballero1215f882019-10-29 15:58:431071 return GetRenderViewHost()->theme_color();
yusufod41c5f92015-03-06 00:14:281072}
1073
Doug Turner63f3c7b2017-07-29 05:10:011074void WebContentsImpl::SetAccessibilityMode(ui::AXMode mode) {
[email protected]95640212014-07-26 18:14:301075 if (mode == accessibility_mode_)
1076 return;
1077
dmazzonif8a31042016-12-03 00:52:131078 // Don't allow accessibility to be enabled for WebContents that are never
1079 // visible, like background pages.
1080 if (IsNeverVisible())
1081 return;
1082
[email protected]95640212014-07-26 18:14:301083 accessibility_mode_ = mode;
dcheng57e39e22016-01-21 00:25:381084
1085 for (FrameTreeNode* node : frame_tree_.Nodes()) {
dmazzonif8a31042016-12-03 00:52:131086 UpdateAccessibilityModeOnFrame(node->current_frame_host());
clamy610c63b32017-12-22 15:05:181087 // Also update accessibility mode on the speculative RenderFrameHost for
1088 // this FrameTreeNode, if one exists.
Alex Moshchuk3d8a1f392017-10-24 19:01:261089 RenderFrameHost* speculative_frame_host =
1090 node->render_manager()->speculative_frame_host();
1091 if (speculative_frame_host)
1092 UpdateAccessibilityModeOnFrame(speculative_frame_host);
dcheng57e39e22016-01-21 00:25:381093 }
[email protected]95640212014-07-26 18:14:301094}
1095
Doug Turner63f3c7b2017-07-29 05:10:011096void WebContentsImpl::AddAccessibilityMode(ui::AXMode mode) {
1097 ui::AXMode new_mode(accessibility_mode_);
dougtcd3dad732017-03-14 03:26:231098 new_mode |= mode;
1099 SetAccessibilityMode(new_mode);
[email protected]95640212014-07-26 18:14:301100}
1101
Avi Drissman1e620f32018-03-16 13:57:291102// Helper class used by WebContentsImpl::RequestAXTreeSnapshot.
1103// Handles the callbacks from parallel snapshot requests to each frame,
1104// and feeds the results to an AXTreeCombiner, which converts them into a
1105// single combined accessibility tree.
1106class WebContentsImpl::AXTreeSnapshotCombiner
1107 : public base::RefCounted<AXTreeSnapshotCombiner> {
1108 public:
1109 explicit AXTreeSnapshotCombiner(AXTreeSnapshotCallback callback)
1110 : callback_(std::move(callback)) {}
1111
1112 AXTreeSnapshotCallback AddFrame(bool is_root) {
1113 // Adds a reference to |this|.
1114 return base::BindOnce(&AXTreeSnapshotCombiner::ReceiveSnapshot, this,
1115 is_root);
1116 }
1117
1118 void ReceiveSnapshot(bool is_root, const ui::AXTreeUpdate& snapshot) {
1119 combiner_.AddTree(snapshot, is_root);
1120 }
1121
1122 private:
1123 friend class base::RefCounted<AXTreeSnapshotCombiner>;
1124
1125 // This is called automatically after the last call to ReceiveSnapshot
1126 // when there are no more references to this object.
1127 ~AXTreeSnapshotCombiner() {
1128 combiner_.Combine();
1129 std::move(callback_).Run(combiner_.combined());
1130 }
1131
1132 ui::AXTreeCombiner combiner_;
1133 AXTreeSnapshotCallback callback_;
1134};
1135
1136void WebContentsImpl::RequestAXTreeSnapshot(AXTreeSnapshotCallback callback,
1137 ui::AXMode ax_mode) {
dmazzonid95ae842016-04-12 21:17:391138 // Send a request to each of the frames in parallel. Each one will return
1139 // an accessibility tree snapshot, and AXTreeSnapshotCombiner will combine
1140 // them into a single tree and call |callback| with that result, then
1141 // delete |combiner|.
Avi Drissman1e620f32018-03-16 13:57:291142 FrameTreeNode* root_node = frame_tree_.root();
tzik58faa742018-08-01 10:54:181143 auto combiner =
1144 base::MakeRefCounted<AXTreeSnapshotCombiner>(std::move(callback));
Avi Drissman1e620f32018-03-16 13:57:291145
tzik58faa742018-08-01 10:54:181146 RecursiveRequestAXTreeSnapshotOnFrame(root_node, combiner.get(), ax_mode);
Avi Drissman1e620f32018-03-16 13:57:291147}
1148
1149void WebContentsImpl::RecursiveRequestAXTreeSnapshotOnFrame(
1150 FrameTreeNode* root_node,
1151 AXTreeSnapshotCombiner* combiner,
1152 ui::AXMode ax_mode) {
dmazzonid95ae842016-04-12 21:17:391153 for (FrameTreeNode* frame_tree_node : frame_tree_.Nodes()) {
Avi Drissman1e620f32018-03-16 13:57:291154 WebContentsImpl* inner_contents =
1155 node_.GetInnerWebContentsInFrame(frame_tree_node);
1156 if (inner_contents) {
1157 inner_contents->RecursiveRequestAXTreeSnapshotOnFrame(root_node, combiner,
1158 ax_mode);
1159 } else {
1160 bool is_root = frame_tree_node == root_node;
1161 frame_tree_node->current_frame_host()->RequestAXTreeSnapshot(
1162 combiner->AddFrame(is_root), ax_mode);
1163 }
dmazzonid95ae842016-04-12 21:17:391164 }
dmazzoni83ba5c82015-04-14 07:11:511165}
1166
Becca Hughes3b5a43482018-07-17 22:31:551167void WebContentsImpl::NotifyViewportFitChanged(
1168 blink::mojom::ViewportFit value) {
1169 for (auto& observer : observers_)
1170 observer.ViewportFitChanged(value);
1171}
1172
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411173FindRequestManager* WebContentsImpl::GetFindRequestManagerForTesting() {
Ehsan Karamad6beb2ea2018-11-25 18:15:131174 return GetFindRequestManager();
1175}
1176
mcnee336ea2c2017-05-23 22:50:591177#if !defined(OS_ANDROID)
akabac6bd1212018-06-25 20:10:481178void WebContentsImpl::UpdateZoom() {
1179 RenderWidgetHostImpl* rwh = GetRenderViewHost()->GetWidget();
1180 if (rwh->GetView())
1181 rwh->SynchronizeVisualProperties();
wjmaclean64951902016-04-29 20:59:121182}
1183
wjmaclean64951902016-04-29 20:59:121184
1185void WebContentsImpl::UpdateZoomIfNecessary(const std::string& scheme,
akabac6bd1212018-06-25 20:10:481186 const std::string& host) {
wjmaclean64951902016-04-29 20:59:121187 NavigationEntry* entry = GetController().GetLastCommittedEntry();
1188 if (!entry)
1189 return;
1190
1191 GURL url = HostZoomMap::GetURLFromEntry(entry);
1192 if (host != net::GetHostOrSpecFromURL(url) ||
1193 (!scheme.empty() && !url.SchemeIs(scheme))) {
1194 return;
1195 }
1196
akabac6bd1212018-06-25 20:10:481197 UpdateZoom();
wjmaclean64951902016-04-29 20:59:121198}
mcnee336ea2c2017-05-23 22:50:591199#endif // !defined(OS_ANDROID)
wjmaclean64951902016-04-29 20:59:121200
Julie Jeongeun Kim7d0986172019-12-04 03:17:251201base::OnceClosure WebContentsImpl::AddReceiverSet(
rockotf62002a2016-09-15 00:08:591202 const std::string& interface_name,
Julie Jeongeun Kim7d0986172019-12-04 03:17:251203 WebContentsReceiverSet* receiver_set) {
rockotf62002a2016-09-15 00:08:591204 auto result =
Julie Jeongeun Kim7d0986172019-12-04 03:17:251205 receiver_sets_.insert(std::make_pair(interface_name, receiver_set));
rockotf62002a2016-09-15 00:08:591206 DCHECK(result.second);
Julie Jeongeun Kim7d0986172019-12-04 03:17:251207 return base::BindOnce(&WebContentsImpl::RemoveReceiverSet,
danakjf4b9e942019-11-29 15:43:041208 weak_factory_.GetWeakPtr(), interface_name);
rockotf62002a2016-09-15 00:08:591209}
1210
Julie Jeongeun Kim7d0986172019-12-04 03:17:251211WebContentsReceiverSet* WebContentsImpl::GetReceiverSet(
rockot8bbad222017-03-22 04:34:051212 const std::string& interface_name) {
Julie Jeongeun Kim7d0986172019-12-04 03:17:251213 auto it = receiver_sets_.find(interface_name);
1214 if (it == receiver_sets_.end())
rockot8bbad222017-03-22 04:34:051215 return nullptr;
1216 return it->second;
1217}
1218
paulmeyerfeafc2d2017-04-25 21:46:401219std::vector<WebContentsImpl*> WebContentsImpl::GetWebContentsAndAllInner() {
1220 std::vector<WebContentsImpl*> all_contents(1, this);
1221
1222 for (size_t i = 0; i != all_contents.size(); ++i) {
1223 for (auto* inner_contents : all_contents[i]->GetInnerWebContents()) {
Lucas Furukawa Gadani2ec00c82018-12-14 15:53:161224 all_contents.push_back(static_cast<WebContentsImpl*>(inner_contents));
paulmeyerfeafc2d2017-04-25 21:46:401225 }
1226 }
1227
1228 return all_contents;
1229}
1230
Sam McNally145fb172017-05-10 00:13:231231void WebContentsImpl::NotifyManifestUrlChanged(
1232 const base::Optional<GURL>& manifest_url) {
1233 for (auto& observer : observers_)
1234 observer.DidUpdateWebManifestURL(manifest_url);
1235}
1236
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411237WebUI* WebContentsImpl::GetWebUI() {
Nasko Oskov2f7d2112019-10-29 03:26:571238 WebUI* committed_web_ui = GetCommittedWebUI();
1239 if (committed_web_ui)
1240 return committed_web_ui;
1241
1242 if (GetRenderManager()->speculative_frame_host())
1243 return GetRenderManager()->speculative_frame_host()->web_ui();
1244
1245 return nullptr;
[email protected]be1f56ab2011-12-22 06:55:311246}
1247
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411248WebUI* WebContentsImpl::GetCommittedWebUI() {
carlosk35f35af2015-12-01 10:55:401249 return frame_tree_.root()->current_frame_host()->web_ui();
[email protected]d5f942ba2008-09-26 19:30:341250}
1251
Changwan Ryuc1134a82018-03-07 02:10:141252void WebContentsImpl::SetUserAgentOverride(const std::string& override,
1253 bool override_in_new_tabs) {
[email protected]bf70edce2012-06-20 22:32:221254 if (GetUserAgentOverride() == override)
1255 return;
1256
Changwan Ryuc1134a82018-03-07 02:10:141257 should_override_user_agent_in_new_tabs_ = override_in_new_tabs;
1258
[email protected]bf70edce2012-06-20 22:32:221259 renderer_preferences_.user_agent_override = override;
1260
Bruce Long1e3e1f542019-10-16 17:56:281261 // Send the new override string to all renderers in the current page.
1262 SyncRendererPrefs();
[email protected]bf70edce2012-06-20 22:32:221263
1264 // Reload the page if a load is currently in progress to avoid having
1265 // different parts of the page loaded using different user agents.
[email protected]6286a372013-10-09 04:03:271266 NavigationEntry* entry = controller_.GetVisibleEntry();
Ivan Kotenkov2c0d2bb32017-11-01 15:41:281267 if (IsLoading() && entry != nullptr && entry->GetIsOverridingUserAgent())
toyoshim6142d96f2016-12-19 09:07:251268 controller_.Reload(ReloadType::BYPASSING_CACHE, true);
[email protected]8d0f3312012-08-18 01:47:531269
ericwilligersde386342016-10-19 02:35:091270 for (auto& observer : observers_)
1271 observer.UserAgentOverrideSet(override);
[email protected]86ef6a392012-05-11 22:03:111272}
1273
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411274const std::string& WebContentsImpl::GetUserAgentOverride() {
[email protected]bf70edce2012-06-20 22:32:221275 return renderer_preferences_.user_agent_override;
[email protected]86ef6a392012-05-11 22:03:111276}
1277
Changwan Ryuc1134a82018-03-07 02:10:141278bool WebContentsImpl::ShouldOverrideUserAgentInNewTabs() {
1279 return should_override_user_agent_in_new_tabs_;
1280}
1281
dmazzonidd3d51a72016-12-14 18:41:011282void WebContentsImpl::EnableWebContentsOnlyAccessibilityMode() {
Dominic Mazzonid9fda172019-03-07 17:12:071283 // If accessibility is already enabled, we'll need to force a reset
1284 // in order to ensure new observers of accessibility events get the
1285 // full accessibility tree from scratch.
1286 bool need_reset = GetAccessibilityMode().has_mode(ui::AXMode::kWebContents);
1287
1288 ui::AXMode desired_mode =
1289 GetContentClient()->browser()->GetAXModeForBrowserContext(
1290 GetBrowserContext());
1291 desired_mode |= ui::kAXModeWebContentsOnly;
1292 AddAccessibilityMode(desired_mode);
1293
1294 if (need_reset) {
dchengafb53e22016-02-04 08:11:081295 for (RenderFrameHost* rfh : GetAllFrames())
1296 ResetAccessibility(rfh);
dchengafb53e22016-02-04 08:11:081297 }
[email protected]95640212014-07-26 18:14:301298}
1299
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411300bool WebContentsImpl::IsWebContentsOnlyAccessibilityModeForTesting() {
Doug Turner63f3c7b2017-07-29 05:10:011301 return accessibility_mode_ == ui::kAXModeWebContentsOnly;
[email protected]95640212014-07-26 18:14:301302}
1303
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411304bool WebContentsImpl::IsFullAccessibilityModeForTesting() {
Doug Turner63f3c7b2017-07-29 05:10:011305 return accessibility_mode_ == ui::kAXModeComplete;
[email protected]95640212014-07-26 18:14:301306}
1307
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411308const PageImportanceSignals& WebContentsImpl::GetPageImportanceSignals() {
kouhei40f03cb2015-09-24 07:47:011309 return page_importance_signals_;
1310}
1311
Becca Hughes6daf5662018-06-27 16:50:541312#if defined(OS_ANDROID)
1313
1314void WebContentsImpl::SetDisplayCutoutSafeArea(gfx::Insets insets) {
Becca Hughes86334622018-07-09 21:29:381315 if (display_cutout_host_impl_)
1316 display_cutout_host_impl_->SetDisplayCutoutSafeArea(insets);
Becca Hughes6daf5662018-06-27 16:50:541317}
1318
1319#endif
1320
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411321const base::string16& WebContentsImpl::GetTitle() {
[email protected]4c6092c5b2009-06-06 00:23:551322 // Transient entries take precedence. They are used for interstitial pages
1323 // that are shown on top of existing pages.
[email protected]10f417c52011-12-28 21:04:231324 NavigationEntry* entry = controller_.GetTransientEntry();
[email protected]45d0ef7f2011-01-05 13:46:231325 if (entry) {
jshin1fb76462016-04-05 22:13:031326 return entry->GetTitleForDisplay();
[email protected]45d0ef7f2011-01-05 13:46:231327 }
carlosk35f35af2015-12-01 10:55:401328
Nasko Oskov2f7d2112019-10-29 03:26:571329 WebUI* our_web_ui =
1330 GetRenderManager()->speculative_frame_host()
1331 ? GetRenderManager()->speculative_frame_host()->web_ui()
1332 : GetRenderManager()->current_frame_host()->web_ui();
[email protected]7ade2732011-02-10 00:13:581333 if (our_web_ui) {
[email protected]96d185d2009-04-24 03:28:541334 // Don't override the title in view source mode.
[email protected]59167c22013-06-03 18:07:321335 entry = controller_.GetVisibleEntry();
[email protected]96d185d2009-04-24 03:28:541336 if (!(entry && entry->IsViewSourceMode())) {
[email protected]e0112912011-02-02 22:54:351337 // Give the Web UI the chance to override our title.
[email protected]fcf75d42013-12-03 20:11:261338 const base::string16& title = our_web_ui->GetOverriddenTitle();
[email protected]96d185d2009-04-24 03:28:541339 if (!title.empty())
1340 return title;
1341 }
1342 }
1343
1344 // We use the title for the last committed entry rather than a pending
1345 // navigation entry. For example, when the user types in a URL, we want to
1346 // keep the old page's title until the new load has committed and we get a new
1347 // title.
[email protected]96d185d2009-04-24 03:28:541348 entry = controller_.GetLastCommittedEntry();
[email protected]59167c22013-06-03 18:07:321349
creis072b61f2015-09-28 20:52:241350 // We make an exception for initial navigations. We only want to use the title
1351 // from the visible entry if:
1352 // 1. The pending entry has been explicitly assigned a title to display.
1353 // 2. The user is doing a history navigation in a new tab (e.g., Ctrl+Back),
1354 // which case there is a pending entry index other than -1.
1355 //
1356 // Otherwise, we want to stick with the last committed entry's title during
1357 // new navigations, which have pending entries at index -1 with no title.
1358 if (controller_.IsInitialNavigation() &&
1359 ((controller_.GetVisibleEntry() &&
1360 !controller_.GetVisibleEntry()->GetTitle().empty()) ||
1361 controller_.GetPendingEntryIndex() != -1)) {
1362 entry = controller_.GetVisibleEntry();
[email protected]9eeafc012013-11-25 23:49:471363 }
[email protected]59167c22013-06-03 18:07:321364
[email protected]45d0ef7f2011-01-05 13:46:231365 if (entry) {
jshin1fb76462016-04-05 22:13:031366 return entry->GetTitleForDisplay();
[email protected]45d0ef7f2011-01-05 13:46:231367 }
[email protected]987fc3a2011-05-26 14:18:091368
1369 // |page_title_when_no_navigation_entry_| is finally used
1370 // if no title cannot be retrieved.
1371 return page_title_when_no_navigation_entry_;
[email protected]96d185d2009-04-24 03:28:541372}
1373
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411374SiteInstanceImpl* WebContentsImpl::GetSiteInstance() {
[email protected]fa944cb82013-11-15 17:51:211375 return GetRenderManager()->current_host()->GetSiteInstance();
[email protected]96d185d2009-04-24 03:28:541376}
1377
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411378bool WebContentsImpl::IsLoading() {
clamy44e84ce2016-02-22 15:38:251379 return frame_tree_.IsLoading() &&
creisadca9862017-06-16 19:12:111380 !(ShowingInterstitialPage() && interstitial_page_->pause_throbber());
[email protected]3c9e1872010-11-18 16:17:491381}
1382
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411383double WebContentsImpl::GetLoadProgress() {
Peter Boström18a40fa2018-10-31 19:03:501384 return frame_tree_.load_progress();
1385}
1386
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411387bool WebContentsImpl::IsLoadingToDifferentDocument() {
clamy44e84ce2016-02-22 15:38:251388 return IsLoading() && is_load_to_different_document_;
[email protected]6dfed692014-05-22 04:18:031389}
1390
Alexander Timindd0fa0c52019-11-06 13:03:371391bool WebContentsImpl::IsDocumentOnLoadCompletedInMainFrame() {
1392 return GetRenderViewHost()->IsDocumentOnLoadCompletedInMainFrame();
1393}
1394
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411395bool WebContentsImpl::IsWaitingForResponse() {
Peter Boströma9a27f6e2018-11-21 13:12:191396 NavigationRequest* ongoing_navigation_request =
1397 frame_tree_.root()->navigation_request();
1398
1399 // An ongoing navigation request means we're waiting for a response.
1400 return ongoing_navigation_request != nullptr;
[email protected]be1f56ab2011-12-22 06:55:311401}
1402
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411403const net::LoadStateWithParam& WebContentsImpl::GetLoadState() {
[email protected]be1f56ab2011-12-22 06:55:311404 return load_state_;
1405}
1406
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411407const base::string16& WebContentsImpl::GetLoadStateHost() {
[email protected]be1f56ab2011-12-22 06:55:311408 return load_state_host_;
1409}
1410
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411411uint64_t WebContentsImpl::GetUploadSize() {
[email protected]be1f56ab2011-12-22 06:55:311412 return upload_size_;
1413}
1414
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411415uint64_t WebContentsImpl::GetUploadPosition() {
[email protected]be1f56ab2011-12-22 06:55:311416 return upload_position_;
1417}
1418
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411419const std::string& WebContentsImpl::GetEncoding() {
Alexander Timine5f20e22019-10-09 23:22:351420 return GetMainFrame()->GetEncoding();
[email protected]be1f56ab2011-12-22 06:55:311421}
1422
Charles Zhaod72f99d2018-09-17 14:18:271423bool WebContentsImpl::WasDiscarded() {
1424 return GetFrameTree()->root()->was_discarded();
1425}
1426
Shubhie Panickerddf2a4e2018-03-06 00:09:061427void WebContentsImpl::SetWasDiscarded(bool was_discarded) {
1428 GetFrameTree()->root()->set_was_discarded();
1429}
1430
Collin Bakerc5259962019-11-14 17:51:581431void WebContentsImpl::IncrementCapturerCount(const gfx::Size& capture_size,
1432 bool stay_hidden) {
[email protected]5a652232013-02-12 06:15:251433 DCHECK(!is_being_destroyed_);
Collin Bakerc5259962019-11-14 17:51:581434 if (stay_hidden)
1435 ++hidden_capturer_count_;
1436 else
1437 ++visible_capturer_count_;
[email protected]222f5822014-02-05 23:40:491438
1439 // Note: This provides a hint to upstream code to size the views optimally
1440 // for quality (e.g., to avoid scaling).
1441 if (!capture_size.IsEmpty() && preferred_size_for_capture_.IsEmpty()) {
1442 preferred_size_for_capture_ = capture_size;
1443 OnPreferredSizeChanged(preferred_size_);
1444 }
ccameron8807c38f2015-01-17 00:29:191445
Collin Bakerc5259962019-11-14 17:51:581446 UpdateVisibilityAndNotifyPageAndView(GetVisibility());
[email protected]54597982013-02-06 01:59:551447}
1448
Collin Bakerc5259962019-11-14 17:51:581449void WebContentsImpl::DecrementCapturerCount(bool stay_hidden) {
1450 if (stay_hidden)
1451 --hidden_capturer_count_;
1452 else
1453 --visible_capturer_count_;
1454 DCHECK_GE(hidden_capturer_count_, 0);
1455 DCHECK_GE(visible_capturer_count_, 0);
[email protected]54597982013-02-06 01:59:551456
[email protected]5a652232013-02-12 06:15:251457 if (is_being_destroyed_)
1458 return;
1459
Francois Doray24fc62c2017-12-11 17:27:331460 if (!IsBeingCaptured()) {
[email protected]222f5822014-02-05 23:40:491461 const gfx::Size old_size = preferred_size_for_capture_;
1462 preferred_size_for_capture_ = gfx::Size();
1463 OnPreferredSizeChanged(old_size);
[email protected]54597982013-02-06 01:59:551464 }
Collin Bakerc5259962019-11-14 17:51:581465
1466 UpdateVisibilityAndNotifyPageAndView(GetVisibility());
[email protected]be1f56ab2011-12-22 06:55:311467}
1468
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411469bool WebContentsImpl::IsBeingCaptured() {
Collin Bakerc5259962019-11-14 17:51:581470 return visible_capturer_count_ + hidden_capturer_count_ > 0;
[email protected]f2bd40812013-07-20 04:30:441471}
1472
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411473bool WebContentsImpl::IsAudioMuted() {
Guido Urdanetaf8372942019-11-15 09:47:211474 return audio_stream_factory_ && audio_stream_factory_->IsMuted();
miu50f97892014-09-22 22:49:521475}
1476
1477void WebContentsImpl::SetAudioMuted(bool mute) {
1478 DVLOG(1) << "SetAudioMuted(mute=" << mute << "), was " << IsAudioMuted()
1479 << " for WebContentsImpl@" << this;
1480
1481 if (mute == IsAudioMuted())
1482 return;
1483
Guido Urdanetaf8372942019-11-15 09:47:211484 GetAudioStreamFactory()->SetMuted(mute);
miu50f97892014-09-22 22:49:521485
ericwilligersde386342016-10-19 02:35:091486 for (auto& observer : observers_)
1487 observer.DidUpdateAudioMutingState(mute);
wjmaclean2f797c782016-01-07 14:52:031488
Jan Rucka442c83f2017-08-08 13:27:541489 // Notification for UI updates in response to the changed muting state.
Collin Bakeraf540cf2019-09-20 20:54:161490 NotifyNavigationStateChanged(INVALIDATE_TYPE_AUDIO);
Jan Rucka442c83f2017-08-08 13:27:541491}
1492
1493bool WebContentsImpl::IsCurrentlyAudible() {
Chris Hamiltondf0d72cd2018-05-29 16:23:531494 return is_currently_audible_;
miu50f97892014-09-22 22:49:521495}
1496
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411497bool WebContentsImpl::IsConnectedToBluetoothDevice() {
ortunodf4d7982016-04-08 02:33:351498 return bluetooth_connected_device_count_ > 0;
ortuno32e7db3c2016-03-29 16:14:201499}
1500
Lucas Furukawa Gadani4b4eed02019-06-04 23:12:041501bool WebContentsImpl::IsConnectedToSerialPort() {
Reilly Grant5e7c79b22019-04-09 17:26:201502 return serial_active_frame_count_ > 0;
1503}
1504
Matt Reynoldse8c6c1f2019-11-02 09:53:531505bool WebContentsImpl::IsConnectedToHidDevice() {
1506 return hid_active_frame_count_ > 0;
1507}
1508
Marijn Kruisselbrink29051042019-08-06 22:56:551509bool WebContentsImpl::HasNativeFileSystemHandles() {
1510 return native_file_system_handle_count_ > 0;
1511}
Marijn Kruisselbrinkf1714aa22019-07-02 03:45:331512bool WebContentsImpl::HasNativeFileSystemDirectoryHandles() {
Marijn Kruisselbrink46fa4bd2019-07-11 18:15:471513 return !native_file_system_directory_handles_.empty();
1514}
1515
1516std::vector<base::FilePath>
1517WebContentsImpl::GetNativeFileSystemDirectoryHandles() {
1518 std::vector<base::FilePath> result;
1519 result.reserve(native_file_system_directory_handles_.size());
1520 for (auto const& entry : native_file_system_directory_handles_)
1521 result.push_back(entry.first);
1522 return result;
Marijn Kruisselbrinkf1714aa22019-07-02 03:45:331523}
1524
1525bool WebContentsImpl::HasWritableNativeFileSystemHandles() {
1526 return native_file_system_writable_handle_count_ > 0;
1527}
1528
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411529bool WebContentsImpl::HasPictureInPictureVideo() {
sawtellea7333a82018-05-31 02:36:361530 return has_picture_in_picture_video_;
1531}
1532
1533void WebContentsImpl::SetHasPictureInPictureVideo(
1534 bool has_picture_in_picture_video) {
1535 // If status of |this| is already accurate, there is no need to update.
1536 if (has_picture_in_picture_video == has_picture_in_picture_video_)
1537 return;
1538 has_picture_in_picture_video_ = has_picture_in_picture_video;
1539 NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB);
François Beaufort058085d2018-08-30 15:29:141540 for (auto& observer : observers_)
1541 observer.MediaPictureInPictureChanged(has_picture_in_picture_video_);
sawtellea7333a82018-05-31 02:36:361542}
1543
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411544bool WebContentsImpl::IsCrashed() {
Dominic Mazzoni73a48322018-05-25 18:14:071545 switch (crashed_status_) {
1546 case base::TERMINATION_STATUS_PROCESS_CRASHED:
1547 case base::TERMINATION_STATUS_ABNORMAL_TERMINATION:
1548 case base::TERMINATION_STATUS_PROCESS_WAS_KILLED:
1549 case base::TERMINATION_STATUS_OOM:
1550 case base::TERMINATION_STATUS_LAUNCH_FAILED:
oshima620225722015-06-04 19:45:271551#if defined(OS_CHROMEOS)
Dominic Mazzoni73a48322018-05-25 18:14:071552 case base::TERMINATION_STATUS_PROCESS_WAS_KILLED_BY_OOM:
oshima620225722015-06-04 19:45:271553#endif
Dominic Mazzoni73a48322018-05-25 18:14:071554#if defined(OS_ANDROID)
1555 case base::TERMINATION_STATUS_OOM_PROTECTED:
1556#endif
Will Harris07925d12019-10-31 03:03:051557#if defined(OS_WIN)
1558 case base::TERMINATION_STATUS_INTEGRITY_FAILURE:
1559#endif
Dominic Mazzoni73a48322018-05-25 18:14:071560 return true;
1561 case base::TERMINATION_STATUS_NORMAL_TERMINATION:
1562 case base::TERMINATION_STATUS_STILL_RUNNING:
1563 return false;
1564 case base::TERMINATION_STATUS_MAX_ENUM:
1565 NOTREACHED();
1566 return false;
1567 }
1568
1569 NOTREACHED();
1570 return false;
[email protected]3c9e1872010-11-18 16:17:491571}
1572
[email protected]b172aee2012-04-10 17:05:261573void WebContentsImpl::SetIsCrashed(base::TerminationStatus status,
1574 int error_code) {
[email protected]443b80e2010-12-14 00:42:231575 if (status == crashed_status_)
[email protected]d5f942ba2008-09-26 19:30:341576 return;
1577
[email protected]443b80e2010-12-14 00:42:231578 crashed_status_ = status;
1579 crashed_error_code_ = error_code;
[email protected]8ff00d72012-10-23 19:12:211580 NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB);
[email protected]d5f942ba2008-09-26 19:30:341581}
1582
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411583base::TerminationStatus WebContentsImpl::GetCrashedStatus() {
[email protected]be1f56ab2011-12-22 06:55:311584 return crashed_status_;
1585}
1586
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411587int WebContentsImpl::GetCrashedErrorCode() {
afakhry98241832016-03-11 19:27:471588 return crashed_error_code_;
1589}
1590
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411591bool WebContentsImpl::IsBeingDestroyed() {
[email protected]be1f56ab2011-12-22 06:55:311592 return is_being_destroyed_;
1593}
1594
[email protected]7f924832014-08-09 05:57:221595void WebContentsImpl::NotifyNavigationStateChanged(
1596 InvalidateTypes changed_flags) {
dalecurtis88c240072015-12-09 02:11:181597 // Notify the media observer of potential audibility changes.
Collin Bakeraf540cf2019-09-20 20:54:161598 if (changed_flags & INVALIDATE_TYPE_AUDIO) {
mlamouri44e4ef42016-01-20 18:42:271599 media_web_contents_observer_->MaybeUpdateAudibleState();
dalecurtisbc6572e12014-09-12 19:22:301600 }
1601
[email protected]d5f942ba2008-09-26 19:30:341602 if (delegate_)
1603 delegate_->NavigationStateChanged(this, changed_flags);
wjmaclean1dabf3e32016-05-13 23:34:441604
1605 if (GetOuterWebContents())
1606 GetOuterWebContents()->NotifyNavigationStateChanged(changed_flags);
[email protected]d5f942ba2008-09-26 19:30:341607}
1608
W. James MacLean6f34b042019-07-12 19:25:301609RenderFrameHostImpl* WebContentsImpl::GetFocusedFrameFromFocusedDelegate() {
1610 FrameTreeNode* focused_node =
1611 GetFocusedWebContents()->frame_tree_.GetFocusedFrame();
1612 return focused_node ? focused_node->current_frame_host() : nullptr;
1613}
1614
David Black9cca3592019-11-06 23:02:221615void WebContentsImpl::OnVerticalScrollDirectionChanged(
1616 viz::VerticalScrollDirection scroll_direction) {
1617 for (auto& observer : observers_)
1618 observer.DidChangeVerticalScrollDirection(scroll_direction);
1619}
1620
Chris Hamiltondf0d72cd2018-05-29 16:23:531621void WebContentsImpl::OnAudioStateChanged() {
1622 // This notification can come from any embedded contents or from this
1623 // WebContents' stream monitor. Aggregate these signals to get the actual
1624 // state.
1625 bool is_currently_audible =
1626 audio_stream_monitor_.IsCurrentlyAudible() ||
1627 (browser_plugin_embedder_ &&
1628 browser_plugin_embedder_->AreAnyGuestsCurrentlyAudible());
1629 if (is_currently_audible == is_currently_audible_)
1630 return;
1631
1632 // Update internal state.
1633 is_currently_audible_ = is_currently_audible;
1634 was_ever_audible_ = was_ever_audible_ || is_currently_audible_;
1635
1636 SendPageMessage(
1637 new PageMsg_AudioStateChanged(MSG_ROUTING_NONE, is_currently_audible_));
altimind8bd26c2016-11-04 11:44:541638
altiminec87fd1f2016-11-17 20:22:491639 // Notification for UI updates in response to the changed audio state.
Collin Bakeraf540cf2019-09-20 20:54:161640 NotifyNavigationStateChanged(INVALIDATE_TYPE_AUDIO);
Jan Rucka442c83f2017-08-08 13:27:541641
Chris Hamiltondf0d72cd2018-05-29 16:23:531642 // Ensure that audio state changes propagate from innermost to outermost
1643 // WebContents.
1644 if (GetOuterWebContents())
1645 GetOuterWebContents()->OnAudioStateChanged();
Tommy Steimel18360512017-11-01 00:38:191646
Chris Hamiltoneb41198f2018-05-24 18:41:261647 for (auto& observer : observers_)
Chris Hamiltondf0d72cd2018-05-29 16:23:531648 observer.OnAudioStateChanged(is_currently_audible_);
altimind8bd26c2016-11-04 11:44:541649}
1650
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411651base::TimeTicks WebContentsImpl::GetLastActiveTime() {
[email protected]9a890452014-02-13 22:21:021652 return last_active_time_;
[email protected]3e324142012-06-25 18:26:331653}
1654
[email protected]9e2e4632012-07-27 16:38:411655void WebContentsImpl::WasShown() {
Collin Baker989e0882019-11-01 01:27:171656 UpdateVisibilityAndNotifyPageAndView(Visibility::VISIBLE);
[email protected]be1f56ab2011-12-22 06:55:311657}
1658
[email protected]b172aee2012-04-10 17:05:261659void WebContentsImpl::WasHidden() {
Collin Baker989e0882019-11-01 01:27:171660 UpdateVisibilityAndNotifyPageAndView(Visibility::HIDDEN);
[email protected]375fa1b2012-05-22 22:05:371661}
1662
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411663bool WebContentsImpl::HasRecentInteractiveInputEvent() {
Daniel Cheng90196c82018-04-25 21:49:141664 static constexpr base::TimeDelta kMaxInterval =
1665 base::TimeDelta::FromSeconds(5);
1666 base::TimeDelta delta =
1667 ui::EventTimeForNow() - last_interactive_input_event_time_;
1668 // Note: the expectation is that the caller is typically expecting an input
1669 // event, e.g. validating that a WebUI message that requires a gesture is
Daniel Chenge81030b32019-07-17 20:20:231670 // actually attached to a gesture.
Daniel Cheng90196c82018-04-25 21:49:141671 return delta <= kMaxInterval;
1672}
1673
Avi Drissman738ea192018-08-29 20:24:161674void WebContentsImpl::SetIgnoreInputEvents(bool ignore_input_events) {
1675 ignore_input_events_ = ignore_input_events;
1676}
1677
Bo Liu168c8642017-08-28 18:26:021678#if defined(OS_ANDROID)
Bo Liu16dd7b8322018-05-02 21:19:501679void WebContentsImpl::SetMainFrameImportance(
1680 ChildProcessImportance importance) {
1681 GetMainFrame()->GetRenderWidgetHost()->SetImportance(importance);
Bo Liu7c6779e92017-08-16 02:02:281682 if (ShowingInterstitialPage()) {
Bo Liu16dd7b8322018-05-02 21:19:501683 static_cast<RenderFrameHostImpl*>(interstitial_page_->GetMainFrame())
1684 ->GetRenderWidgetHost()
1685 ->SetImportance(importance);
Bo Liu7c6779e92017-08-16 02:02:281686 }
Bo Liu7c6779e92017-08-16 02:02:281687}
Bo Liu168c8642017-08-28 18:26:021688#endif
Bo Liu7c6779e92017-08-16 02:02:281689
ccameron8807c38f2015-01-17 00:29:191690void WebContentsImpl::WasOccluded() {
Collin Baker989e0882019-11-01 01:27:171691 UpdateVisibilityAndNotifyPageAndView(Visibility::OCCLUDED);
ccameron8807c38f2015-01-17 00:29:191692}
1693
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411694Visibility WebContentsImpl::GetVisibility() {
Francois Doraye6161152018-03-27 22:05:371695 return visibility_;
Francois Dorayfe4a1772018-02-17 04:17:091696}
1697
Alex Moshchuk5bcd7c12019-11-12 22:55:311698bool WebContentsImpl::NeedToFireBeforeUnloadOrUnload() {
[email protected]be1f56ab2011-12-22 06:55:311699 // TODO(creis): Should we fire even for interstitial pages?
Alex Moshchuk6fcaca752018-07-14 02:13:591700 if (ShowingInterstitialPage())
1701 return false;
1702
1703 if (!WillNotifyDisconnection())
1704 return false;
1705
1706 // Don't fire if the main frame's RenderViewHost indicates that beforeunload
1707 // and unload have already executed (e.g., after receiving a ClosePage ACK)
1708 // or should be ignored.
1709 if (GetRenderViewHost()->SuddenTerminationAllowed())
1710 return false;
1711
Alex Moshchukc78dbc722019-10-31 20:00:501712 // Check whether any frame in the frame tree needs to run beforeunload or
1713 // unload handlers.
1714 for (FrameTreeNode* node : frame_tree_.Nodes()) {
1715 RenderFrameHostImpl* rfh = node->current_frame_host();
1716
1717 // No need to run beforeunload/unload if the RenderFrame isn't live.
1718 if (!rfh->IsRenderFrameLive())
1719 continue;
1720
Dave Tapuska97d22ab2019-12-03 17:56:361721 if (rfh->GetSuddenTerminationDisablerState(
1722 blink::mojom::SuddenTerminationDisablerType::
1723 kBeforeUnloadHandler) ||
1724 rfh->GetSuddenTerminationDisablerState(
1725 blink::mojom::SuddenTerminationDisablerType::kUnloadHandler)) {
Alex Moshchukc78dbc722019-10-31 20:00:501726 return true;
1727 }
Aditya Keerthi34f37e62019-03-08 15:54:531728 }
Alex Moshchuk6fcaca752018-07-14 02:13:591729
Alex Moshchukc78dbc722019-10-31 20:00:501730 return false;
[email protected]be1f56ab2011-12-22 06:55:311731}
1732
Chris Hamilton3b3e315d2018-09-19 13:16:211733void WebContentsImpl::DispatchBeforeUnload(bool auto_cancel) {
1734 auto before_unload_type =
1735 auto_cancel ? RenderFrameHostImpl::BeforeUnloadType::DISCARD
1736 : RenderFrameHostImpl::BeforeUnloadType::TAB_CLOSE;
1737 GetMainFrame()->DispatchBeforeUnload(before_unload_type, false);
[email protected]1c3f80bd2014-04-08 23:02:061738}
1739
W. James MacLean2539adb32019-12-13 00:40:441740bool WebContentsImpl::IsInnerWebContentsForGuest() {
1741 return !!browser_plugin_guest_;
1742}
1743
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:501744void WebContentsImpl::AttachInnerWebContents(
1745 std::unique_ptr<WebContents> inner_web_contents,
W. James MacLean62883982019-11-12 20:39:001746 RenderFrameHost* render_frame_host,
1747 bool is_full_page) {
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:501748 WebContentsImpl* inner_web_contents_impl =
1749 static_cast<WebContentsImpl*>(inner_web_contents.get());
1750 DCHECK(!inner_web_contents_impl->node_.outer_web_contents());
1751 auto* render_frame_host_impl =
1752 static_cast<RenderFrameHostImpl*>(render_frame_host);
1753 DCHECK_EQ(&frame_tree_,
1754 render_frame_host_impl->frame_tree_node()->frame_tree());
Lucas Furukawa Gadani105de1e812018-05-31 19:38:391755
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:501756 RenderFrameHostManager* inner_render_manager =
1757 inner_web_contents_impl->GetRenderManager();
1758 RenderFrameHostImpl* inner_main_frame =
1759 inner_render_manager->current_frame_host();
1760 RenderViewHostImpl* inner_render_view_host =
1761 inner_render_manager->current_host();
1762 auto* outer_render_manager =
1763 render_frame_host_impl->frame_tree_node()->render_manager();
lfg91a79e12016-04-01 23:52:191764
Lucas Furukawa Gadani105de1e812018-05-31 19:38:391765 // When attaching a WebContents as an inner WebContents, we need to replace
1766 // the Webcontents' view with a WebContentsViewChildFrame.
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:501767 inner_web_contents_impl->view_.reset(new WebContentsViewChildFrame(
1768 inner_web_contents_impl,
1769 GetContentClient()->browser()->GetWebContentsViewDelegate(
1770 inner_web_contents_impl),
1771 &inner_web_contents_impl->render_view_host_delegate_view_));
Lucas Furukawa Gadani105de1e812018-05-31 19:38:391772
lfg91a79e12016-04-01 23:52:191773 // When the WebContents being initialized has an opener, the browser side
1774 // Render{View,Frame}Host must be initialized and the RenderWidgetHostView
1775 // created. This is needed because the usual initialization happens during
1776 // the first navigation, but when attaching a new window we don't navigate
1777 // before attaching. If the browser side is already initialized, the calls
1778 // below will just early return.
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:501779 inner_render_manager->InitRenderView(inner_render_view_host, nullptr);
1780 inner_main_frame->Init();
1781 if (!inner_render_manager->GetRenderWidgetHostView()) {
1782 inner_web_contents_impl->CreateRenderWidgetHostViewForRenderManager(
1783 inner_render_view_host);
1784 }
lfg91a79e12016-04-01 23:52:191785
lazyboy6ec48b2a2015-06-29 15:18:141786 // Create a link to our outer WebContents.
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:501787 node_.AttachInnerWebContents(std::move(inner_web_contents),
1788 render_frame_host_impl);
lazyboy6ec48b2a2015-06-29 15:18:141789
lazyboy6ec48b2a2015-06-29 15:18:141790 // Create a proxy in top-level RenderFrameHostManager, pointing to the
1791 // SiteInstance of the outer WebContents. The proxy will be used to send
1792 // postMessage to the inner WebContents.
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:501793 auto* proxy = inner_render_manager->CreateOuterDelegateProxy(
1794 render_frame_host_impl->GetSiteInstance());
lazyboy6ec48b2a2015-06-29 15:18:141795
Lucas Furukawa Gadani99125822019-01-03 15:41:491796 // When attaching a GuestView as an inner WebContents, there should already be
1797 // a live RenderFrame, which has to be swapped. When attaching a portal, there
1798 // will not be a live RenderFrame before creating the proxy.
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:501799 if (render_frame_host_impl->IsRenderFrameLive()) {
1800 inner_render_manager->SwapOuterDelegateFrame(render_frame_host_impl, proxy);
Lucas Furukawa Gadani99125822019-01-03 15:41:491801
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:501802 inner_web_contents_impl->ReattachToOuterWebContentsFrame();
Lucas Furukawa Gadani99125822019-01-03 15:41:491803 }
ekaramadadd882292016-06-08 15:22:561804
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:501805 if (frame_tree_.GetFocusedFrame() ==
1806 render_frame_host_impl->frame_tree_node()) {
1807 inner_web_contents_impl->SetFocusedFrame(
1808 inner_web_contents_impl->frame_tree_.root(),
1809 render_frame_host_impl->GetSiteInstance());
avallee7529b2a2017-04-26 11:37:511810 }
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:501811 outer_render_manager->set_attach_complete();
W. James MacLean62883982019-11-12 20:39:001812
1813 // If the inner WebContents is full frame, give it focus.
1814 if (is_full_page) {
1815 // There should only ever be one inner WebContents when |is_full_page| is
1816 // true, and it is the one we just attached.
1817 DCHECK_EQ(1u, node_.GetInnerWebContents().size());
1818 inner_web_contents_impl->SetAsFocusedWebContentsIfNecessary();
1819 }
lazyboy6ec48b2a2015-06-29 15:18:141820}
1821
Lucas Furukawa Gadani99125822019-01-03 15:41:491822std::unique_ptr<WebContents> WebContentsImpl::DetachFromOuterWebContents() {
1823 DCHECK(node_.outer_web_contents());
1824 if (RenderWidgetHostViewBase* view =
1825 static_cast<RenderWidgetHostViewBase*>(GetMainFrame()->GetView())) {
1826 view->Destroy();
1827 }
1828 GetRenderManager()->DeleteOuterDelegateProxy(
1829 node_.OuterContentsFrameTreeNode()
1830 ->current_frame_host()
1831 ->GetSiteInstance());
1832 view_.reset(CreateWebContentsView(
1833 this, GetContentClient()->browser()->GetWebContentsViewDelegate(this),
1834 &render_view_host_delegate_view_));
danakjfc5184932019-09-12 18:08:321835 view_->CreateView(nullptr);
Lucas Furukawa Gadani99125822019-01-03 15:41:491836 std::unique_ptr<WebContents> web_contents =
1837 node_.DisconnectFromOuterWebContents();
1838 DCHECK_EQ(web_contents.get(), this);
1839 node_.SetFocusedWebContents(this);
1840 CreateRenderWidgetHostViewForRenderManager(GetRenderViewHost());
1841 return web_contents;
1842}
1843
W. James MacLean2dc75ed42019-03-06 14:31:091844void WebContentsImpl::RecursivelyRegisterFrameSinkIds() {
1845 auto* view = static_cast<RenderWidgetHostViewBase*>(
1846 GetRenderManager()->GetRenderWidgetHostView());
1847 DCHECK(view);
1848 if (!view->IsRenderWidgetHostViewChildFrame())
1849 return;
1850 static_cast<RenderWidgetHostViewChildFrame*>(view)->RegisterFrameSinkId();
1851
1852 for (auto* inner_web_contents : node_.GetInnerWebContents()) {
1853 static_cast<WebContentsImpl*>(inner_web_contents)
1854 ->RecursivelyRegisterFrameSinkIds();
1855 }
1856}
1857
Lucas Furukawa Gadaniaed1fed2017-10-13 17:34:141858void WebContentsImpl::ReattachToOuterWebContentsFrame() {
1859 DCHECK(node_.outer_web_contents());
1860 auto* render_manager = GetRenderManager();
1861 auto* parent_frame =
1862 node_.OuterContentsFrameTreeNode()->current_frame_host()->GetParent();
1863 render_manager->SetRWHViewForInnerContents(
1864 render_manager->GetRenderWidgetHostView());
1865
W. James MacLean2dc75ed42019-03-06 14:31:091866 RecursivelyRegisterFrameSinkIds();
Lucas Furukawa Gadaniaed1fed2017-10-13 17:34:141867
1868 // Set up the the guest's AX tree to point back at the embedder's AX tree.
1869 GetMainFrame()->set_browser_plugin_embedder_ax_tree_id(
1870 parent_frame->GetAXTreeID());
1871 GetMainFrame()->UpdateAXTreeData();
1872}
1873
dalecurtis6c58ed02016-10-28 23:02:371874void WebContentsImpl::DidChangeVisibleSecurityState() {
Emily Starkc663b0242019-04-26 19:34:451875 if (delegate_)
dalecurtis6c58ed02016-10-28 23:02:371876 delegate_->VisibleSecurityStateChanged(this);
Emily Starkc663b0242019-04-26 19:34:451877 for (auto& observer : observers_)
1878 observer.DidChangeVisibleSecurityState();
dalecurtis6c58ed02016-10-28 23:02:371879}
1880
Ahmed Fakhryd70bf37b2018-04-04 17:07:241881void WebContentsImpl::NotifyPreferencesChanged() {
1882 std::set<RenderViewHost*> render_view_host_set;
1883 for (FrameTreeNode* node : frame_tree_.Nodes()) {
Giovanni Ortuño Urquidi17a5d7c2018-04-30 06:48:531884 render_view_host_set.insert(
1885 node->current_frame_host()->GetRenderViewHost());
Ahmed Fakhryd70bf37b2018-04-04 17:07:241886 }
Giovanni Ortuño Urquidi17a5d7c2018-04-30 06:48:531887
Ahmed Fakhryd70bf37b2018-04-04 17:07:241888 for (RenderViewHost* render_view_host : render_view_host_set)
1889 render_view_host->OnWebkitPreferencesChanged();
1890}
1891
Bruce Long1e3e1f542019-10-16 17:56:281892void WebContentsImpl::SyncRendererPrefs() {
1893 blink::mojom::RendererPreferences renderer_preferences =
1894 GetRendererPrefs(GetBrowserContext());
1895 RenderViewHostImpl::GetPlatformSpecificPrefs(&renderer_preferences);
1896 SendPageMessage(
1897 new PageMsg_SetRendererPrefs(MSG_ROUTING_NONE, renderer_preferences));
1898}
1899
Josh Karlindaba39322019-07-17 23:24:331900void WebContentsImpl::OnCookiesRead(const GURL& url,
1901 const GURL& first_party_url,
1902 const net::CookieList& cookie_list,
1903 bool blocked_by_policy) {
1904 for (auto& observer : observers_) {
1905 observer.OnCookiesRead(url, first_party_url, cookie_list,
1906 blocked_by_policy);
1907 }
1908}
1909
1910void WebContentsImpl::OnCookieChange(const GURL& url,
1911 const GURL& first_party_url,
1912 const net::CanonicalCookie& cookie,
1913 bool blocked_by_policy) {
1914 for (auto& observer : observers_) {
1915 observer.OnCookieChange(url, first_party_url, cookie, blocked_by_policy);
1916 }
1917}
1918
[email protected]b172aee2012-04-10 17:05:261919void WebContentsImpl::Stop() {
dcheng57e39e22016-01-21 00:25:381920 for (FrameTreeNode* node : frame_tree_.Nodes())
1921 node->StopLoading();
ericwilligersde386342016-10-19 02:35:091922 for (auto& observer : observers_)
1923 observer.NavigationStopped();
[email protected]0bfbf882011-12-22 18:19:271924}
1925
Francois Doray47f759d2018-06-11 18:13:511926void WebContentsImpl::SetPageFrozen(bool frozen) {
1927 // A visible page is never frozen.
1928 DCHECK_NE(Visibility::VISIBLE, GetVisibility());
1929
1930 SendPageMessage(new PageMsg_SetPageFrozen(MSG_ROUTING_NONE, frozen));
Fadi Meawada6573e02018-03-10 00:52:111931}
1932
erikchen6c7df7f7a2018-05-03 18:13:591933std::unique_ptr<WebContents> WebContentsImpl::Clone() {
[email protected]14392a52012-05-02 20:28:441934 // We use our current SiteInstance since the cloned entry will use it anyway.
alexmose201c7cd2015-06-10 17:14:211935 // We pass our own opener so that the cloned page can access it if it was set
[email protected]ed245db2012-12-18 08:00:451936 // before.
[email protected]54944cde2012-12-09 09:24:591937 CreateParams create_params(GetBrowserContext(), GetSiteInstance());
Ian Clelland5cbaaf82017-11-27 22:00:031938 FrameTreeNode* opener = frame_tree_.root()->opener();
1939 RenderFrameHostImpl* opener_rfh = nullptr;
1940 if (opener)
1941 opener_rfh = opener->current_frame_host();
erikchen6c7df7f7a2018-05-03 18:13:591942 std::unique_ptr<WebContentsImpl> tc =
erikchen50735fd2018-05-05 15:08:331943 CreateWithOpener(create_params, opener_rfh);
Lucas Furukawa Gadani5553a152019-01-08 18:55:571944 tc->GetController().CopyStateFrom(&controller_, true);
ericwilligersde386342016-10-19 02:35:091945 for (auto& observer : observers_)
erikchen6c7df7f7a2018-05-03 18:13:591946 observer.DidCloneToNewWebContents(this, tc.get());
[email protected]0bfbf882011-12-22 18:19:271947 return tc;
1948}
1949
[email protected]14392a52012-05-02 20:28:441950void WebContentsImpl::Observe(int type,
[email protected]8ff00d72012-10-23 19:12:211951 const NotificationSource& source,
1952 const NotificationDetails& details) {
[email protected]14392a52012-05-02 20:28:441953 switch (type) {
[email protected]8ff00d72012-10-23 19:12:211954 case NOTIFICATION_RENDER_WIDGET_HOST_DESTROYED: {
1955 RenderWidgetHost* host = Source<RenderWidgetHost>(source).ptr();
[email protected]0b431992014-06-24 00:08:031956 RenderWidgetHostView* view = host->GetView();
1957 if (view == GetFullscreenRenderWidgetHostView()) {
1958 // We cannot just call view_->RestoreFocus() here. On some platforms,
1959 // attempting to focus the currently-invisible WebContentsView will be
1960 // flat-out ignored. Therefore, this boolean is used to track whether
1961 // we will request focus after the fullscreen widget has been
1962 // destroyed.
1963 fullscreen_widget_had_focus_at_shutdown_ = (view && view->HasFocus());
1964 } else {
alexmosc2a8cec2016-05-23 22:19:531965 for (auto i = pending_widget_views_.begin();
[email protected]0b431992014-06-24 00:08:031966 i != pending_widget_views_.end(); ++i) {
1967 if (host->GetView() == i->second) {
1968 pending_widget_views_.erase(i);
1969 break;
1970 }
[email protected]bafe6cd2012-05-23 23:09:501971 }
1972 }
1973 break;
1974 }
[email protected]14392a52012-05-02 20:28:441975 default:
1976 NOTREACHED();
1977 }
1978}
1979
[email protected]ec6c05f2013-10-23 18:41:571980WebContents* WebContentsImpl::GetWebContents() {
1981 return this;
1982}
1983
[email protected]54944cde2012-12-09 09:24:591984void WebContentsImpl::Init(const WebContents::CreateParams& params) {
[email protected]fa944cb82013-11-15 17:51:211985 // This is set before initializing the render manager since
[email protected]b0936d22013-11-28 06:47:361986 // RenderFrameHostManager::Init calls back into us via its delegate to ask if
[email protected]fa944cb82013-11-15 17:51:211987 // it should be hidden.
Francois Doraye6161152018-03-27 22:05:371988 visibility_ =
1989 params.initially_hidden ? Visibility::HIDDEN : Visibility::VISIBLE;
[email protected]d6fa88f2013-10-18 16:00:431990
Sebastien Marchand28cf23182018-06-20 02:39:351991 if (!params.last_active_time.is_null())
1992 last_active_time_ = params.last_active_time;
1993
dcheng3ce04b62015-10-26 23:30:551994 // The routing ids must either all be set or all be unset.
avic2c29d782014-12-12 00:23:541995 DCHECK((params.routing_id == MSG_ROUTING_NONE &&
dcheng3ce04b62015-10-26 23:30:551996 params.main_frame_routing_id == MSG_ROUTING_NONE &&
1997 params.main_frame_widget_routing_id == MSG_ROUTING_NONE) ||
avic2c29d782014-12-12 00:23:541998 (params.routing_id != MSG_ROUTING_NONE &&
dcheng3ce04b62015-10-26 23:30:551999 params.main_frame_routing_id != MSG_ROUTING_NONE &&
2000 params.main_frame_widget_routing_id != MSG_ROUTING_NONE));
2001
2002 scoped_refptr<SiteInstance> site_instance = params.site_instance;
2003 if (!site_instance)
2004 site_instance = SiteInstance::Create(params.browser_context);
Lukasz Anforowicz3caa8372018-06-05 17:22:072005 if (params.desired_renderer_state == CreateParams::kNoRendererProcess) {
2006 static_cast<SiteInstanceImpl*>(site_instance.get())
2007 ->PreventAssociationWithSpareProcess();
2008 }
dcheng3ce04b62015-10-26 23:30:552009
2010 // A main RenderFrameHost always has a RenderWidgetHost, since it is always a
2011 // local root by definition.
2012 // TODO(avi): Once RenderViewHostImpl has-a RenderWidgetHostImpl, it will no
2013 // longer be necessary to eagerly grab a routing ID for the view.
2014 // https://crbug.com/545684
2015 int32_t view_routing_id = params.routing_id;
2016 int32_t main_frame_widget_routing_id = params.main_frame_widget_routing_id;
2017 if (main_frame_widget_routing_id == MSG_ROUTING_NONE) {
Albert J. Wongcb004632018-07-10 22:58:252018 view_routing_id = site_instance->GetProcess()->GetNextRoutingID();
2019 main_frame_widget_routing_id =
dcheng3ce04b62015-10-26 23:30:552020 site_instance->GetProcess()->GetNextRoutingID();
2021 }
2022
Albert J. Wongcb004632018-07-10 22:58:252023 DCHECK_NE(view_routing_id, main_frame_widget_routing_id);
2024
Dave Tapuska485aca92017-08-08 00:47:582025 GetRenderManager()->Init(
2026 site_instance.get(), view_routing_id, params.main_frame_routing_id,
2027 main_frame_widget_routing_id, params.renderer_initiated_creation);
lukasza464d8692016-02-22 19:26:322028
lukasza5140a412016-09-15 21:12:302029 // blink::FrameTree::setName always keeps |unique_name| empty in case of a
2030 // main frame - let's do the same thing here.
2031 std::string unique_name;
lukasza464d8692016-02-22 19:26:322032 frame_tree_.root()->SetFrameName(params.main_frame_name, unique_name);
[email protected]d1198fd2012-08-13 22:50:192033
[email protected]fc2b46b2014-05-03 16:33:452034 WebContentsViewDelegate* delegate =
2035 GetContentClient()->browser()->GetWebContentsViewDelegate(this);
2036
W. James MacLean2539adb32019-12-13 00:40:442037 if (browser_plugin_guest_) {
sky52a39e342016-12-17 17:23:222038 view_.reset(new WebContentsViewChildFrame(
2039 this, delegate, &render_view_host_delegate_view_));
2040 } else {
2041 view_.reset(CreateWebContentsView(this, delegate,
2042 &render_view_host_delegate_view_));
[email protected]d1198fd2012-08-13 22:50:192043 }
[email protected]fc2b46b2014-05-03 16:33:452044 CHECK(render_view_host_delegate_view_);
[email protected]d1198fd2012-08-13 22:50:192045 CHECK(view_.get());
2046
danakjfc5184932019-09-12 18:08:322047 view_->CreateView(params.context);
[email protected]d1198fd2012-08-13 22:50:192048
brettw4b461082016-11-19 18:55:162049#if BUILDFLAG(ENABLE_PLUGINS)
tommyclieb25b2a2014-11-03 19:45:092050 plugin_content_origin_whitelist_.reset(
2051 new PluginContentOriginWhitelist(this));
2052#endif
2053
[email protected]d1198fd2012-08-13 22:50:192054 registrar_.Add(this,
[email protected]8ff00d72012-10-23 19:12:212055 NOTIFICATION_RENDER_WIDGET_HOST_DESTROYED,
2056 NotificationService::AllBrowserContextsAndSources());
[email protected]f28ef9a32014-05-12 16:36:102057
leon.hane4db177a2017-02-07 14:19:162058 screen_orientation_provider_.reset(new ScreenOrientationProvider(this));
[email protected]01f83f92014-06-17 14:41:542059
mlamouriefdca9d2014-09-16 16:55:402060 manifest_manager_host_.reset(new ManifestManagerHost(this));
2061
mfomitchev2b8b066a2016-01-28 19:23:152062#if defined(OS_ANDROID)
Oksana Zhuravlova0b634332019-10-28 23:04:222063 DateTimeChooserAndroid::CreateForWebContents(this);
[email protected]583418cc2013-01-17 14:01:102064#endif
fsamuel7310a4262014-12-05 05:06:442065
2066 // BrowserPluginGuest::Init needs to be called after this WebContents has
W. James MacLean6e78da342019-12-13 15:13:232067 // a RenderWidgetHostViewChildFrame. That is, |view_->CreateView| above.
fsamuel7310a4262014-12-05 05:06:442068 if (browser_plugin_guest_)
2069 browser_plugin_guest_->Init();
nickf9acfbe2014-12-23 19:12:372070
2071 for (size_t i = 0; i < g_created_callbacks.Get().size(); i++)
2072 g_created_callbacks.Get().at(i).Run(this);
naskob21fe4872015-02-24 14:15:562073
2074 // If the WebContents creation was renderer-initiated, it means that the
2075 // corresponding RenderView and main RenderFrame have already been created.
2076 // Ensure observers are notified about this.
2077 if (params.renderer_initiated_creation) {
avi3627ecac2015-10-16 17:40:432078 GetRenderViewHost()->GetWidget()->set_renderer_initialized(true);
Alex Moshchuk27caae82017-09-11 23:11:182079 GetRenderViewHost()->DispatchRenderViewCreated();
naskob21fe4872015-02-24 14:15:562080 GetRenderManager()->current_frame_host()->SetRenderFrameCreated(true);
2081 }
naskof5940b9f2015-03-02 23:04:052082
lof84501da082016-05-23 21:22:542083 // Create the renderer process in advance if requested.
Lukasz Anforowicz3caa8372018-06-05 17:22:072084 if (params.desired_renderer_state ==
2085 CreateParams::kInitializeAndWarmupRendererProcess) {
lof84501da082016-05-23 21:22:542086 if (!GetRenderManager()->current_frame_host()->IsRenderFrameLive()) {
2087 GetRenderManager()->InitRenderView(GetRenderViewHost(), nullptr);
2088 }
2089 }
2090
naskof5940b9f2015-03-02 23:04:052091 // Ensure that observers are notified of the creation of this WebContents's
2092 // main RenderFrameHost. It must be done here for main frames, since the
2093 // NotifySwappedFromRenderManager expects view_ to already be created and that
2094 // happens after RenderFrameHostManager::Init.
2095 NotifySwappedFromRenderManager(
2096 nullptr, GetRenderManager()->current_frame_host(), true);
Albert J. Wonge76bf0b82019-09-27 07:47:062097
2098 // For WebContents that are never shown, do critical initialization here which
2099 // would normally only happen when the WebContents is shown.
2100 if (params.is_never_visible) {
2101 // This has just been created so there can only be one frame. Thus it is
2102 // safe to initialize the root.
2103 GetMainFrame()->Init();
2104 }
[email protected]d1198fd2012-08-13 22:50:192105}
2106
[email protected]7fff43e2013-05-21 20:21:102107void WebContentsImpl::OnWebContentsDestroyed(WebContentsImpl* web_contents) {
2108 RemoveDestructionObserver(web_contents);
2109
[email protected]ceee8cd2013-03-08 04:59:512110 // Clear a pending contents that has been closed before being shown.
alexmosc2a8cec2016-05-23 22:19:532111 for (auto iter = pending_contents_.begin(); iter != pending_contents_.end();
[email protected]ceee8cd2013-03-08 04:59:512112 ++iter) {
erikchenbee5c9622018-04-27 19:30:252113 if (iter->second.get() != web_contents)
[email protected]ceee8cd2013-03-08 04:59:512114 continue;
erikchenbee5c9622018-04-27 19:30:252115
2116 // Someone else has deleted the WebContents. That should never happen!
2117 // TODO(erikchen): Fix semantics here. https://crbug.com/832879.
2118 iter->second.release();
[email protected]ceee8cd2013-03-08 04:59:512119 pending_contents_.erase(iter);
[email protected]ceee8cd2013-03-08 04:59:512120 return;
2121 }
2122 NOTREACHED();
[email protected]14392a52012-05-02 20:28:442123}
2124
[email protected]7fff43e2013-05-21 20:21:102125void WebContentsImpl::AddDestructionObserver(WebContentsImpl* web_contents) {
Jan Wilken Dörrie73c901e2019-06-12 09:02:322126 if (!base::Contains(destruction_observers_, web_contents)) {
[email protected]7fff43e2013-05-21 20:21:102127 destruction_observers_[web_contents] =
Jeremy Roman04f27c372017-10-27 15:20:552128 std::make_unique<DestructionObserver>(this, web_contents);
[email protected]7fff43e2013-05-21 20:21:102129 }
2130}
2131
2132void WebContentsImpl::RemoveDestructionObserver(WebContentsImpl* web_contents) {
avia8cf81982016-08-12 20:28:462133 destruction_observers_.erase(web_contents);
[email protected]7fff43e2013-05-21 20:21:102134}
2135
[email protected]b172aee2012-04-10 17:05:262136void WebContentsImpl::AddObserver(WebContentsObserver* observer) {
[email protected]be1f56ab2011-12-22 06:55:312137 observers_.AddObserver(observer);
2138}
2139
[email protected]b172aee2012-04-10 17:05:262140void WebContentsImpl::RemoveObserver(WebContentsObserver* observer) {
[email protected]be1f56ab2011-12-22 06:55:312141 observers_.RemoveObserver(observer);
2142}
2143
[email protected]948481d2014-06-11 18:32:222144std::set<RenderWidgetHostView*>
2145WebContentsImpl::GetRenderWidgetHostViewsInTree() {
2146 std::set<RenderWidgetHostView*> set;
2147 if (ShowingInterstitialPage()) {
lfgf949e0e2017-05-10 22:18:112148 if (RenderWidgetHostView* rwhv = GetRenderWidgetHostView())
2149 set.insert(rwhv);
[email protected]948481d2014-06-11 18:32:222150 } else {
dchengafb53e22016-02-04 08:11:082151 for (RenderFrameHost* rfh : GetAllFrames()) {
lfgf949e0e2017-05-10 22:18:112152 if (RenderWidgetHostView* rwhv = static_cast<RenderFrameHostImpl*>(rfh)
2153 ->frame_tree_node()
2154 ->render_manager()
2155 ->GetRenderWidgetHostView()) {
2156 set.insert(rwhv);
2157 }
dchengafb53e22016-02-04 08:11:082158 }
[email protected]948481d2014-06-11 18:32:222159 }
[email protected]de3c5d82014-05-28 22:12:592160 return set;
2161}
2162
[email protected]b172aee2012-04-10 17:05:262163void WebContentsImpl::Activate() {
[email protected]d5f942ba2008-09-26 19:30:342164 if (delegate_)
2165 delegate_->ActivateContents(this);
2166}
2167
avi3e4b8512015-11-11 01:55:492168void WebContentsImpl::LostCapture(RenderWidgetHostImpl* render_widget_host) {
2169 if (!RenderViewHostImpl::From(render_widget_host))
2170 return;
2171
[email protected]63954792011-07-11 04:17:482172 if (delegate_)
2173 delegate_->LostCapture();
2174}
2175
Steven Holte38041342018-06-22 22:00:572176ukm::SourceId WebContentsImpl::GetUkmSourceIdForLastCommittedSource() const {
2177 return last_committed_source_id_;
2178}
2179
Yue Ru Sun979c4b162019-11-06 16:11:042180ukm::SourceId
2181WebContentsImpl::GetUkmSourceIdForLastCommittedSourceIncludingSameDocument()
2182 const {
2183 return last_committed_source_id_including_same_document_;
2184}
2185
Ahmed Fakhryfaa32d22018-10-05 15:56:162186void WebContentsImpl::SetTopControlsShownRatio(
2187 RenderWidgetHostImpl* render_widget_host,
2188 float ratio) {
2189 if (!delegate_)
2190 return;
2191
2192 RenderFrameHostImpl* rfh = GetMainFrame();
2193 if (!rfh || render_widget_host != rfh->GetRenderWidgetHost())
2194 return;
2195
2196 delegate_->SetTopControlsShownRatio(this, ratio);
Ahmed Fakhry58e6ef542018-09-04 18:05:382197}
2198
Ahmed Fakhry58e6ef542018-09-04 18:05:382199void WebContentsImpl::SetTopControlsGestureScrollInProgress(bool in_progress) {
2200 if (delegate_)
2201 delegate_->SetTopControlsGestureScrollInProgress(in_progress);
2202}
2203
lazyboy63f5b312015-11-23 20:19:522204void WebContentsImpl::RenderWidgetCreated(
2205 RenderWidgetHostImpl* render_widget_host) {
2206 created_widgets_.insert(render_widget_host);
2207}
2208
[email protected]b24b68a2012-09-24 21:57:262209void WebContentsImpl::RenderWidgetDeleted(
2210 RenderWidgetHostImpl* render_widget_host) {
lazyboy63f5b312015-11-23 20:19:522211 // Note that |is_being_destroyed_| can be true at this point as
2212 // ~WebContentsImpl() calls RFHM::ClearRFHsPendingShutdown(), which might lead
2213 // us here.
2214 created_widgets_.erase(render_widget_host);
[email protected]b24b68a2012-09-24 21:57:262215
lazyboy63f5b312015-11-23 20:19:522216 if (is_being_destroyed_)
2217 return;
[email protected]44470a22013-01-24 01:21:542218
2219 if (render_widget_host &&
alexmosc9e76ec2016-05-16 22:59:372220 render_widget_host->GetRoutingID() == fullscreen_widget_routing_id_ &&
2221 render_widget_host->GetProcess()->GetID() ==
2222 fullscreen_widget_process_id_) {
[email protected]4aebbca2013-09-17 22:26:492223 if (delegate_ && delegate_->EmbedsFullscreenWidget())
mlamouri7a78d6fd2015-01-17 13:23:532224 delegate_->ExitFullscreenModeForTab(this);
ericwilligersde386342016-10-19 02:35:092225 for (auto& observer : observers_)
2226 observer.DidDestroyFullscreenWidget();
alexmosc9e76ec2016-05-16 22:59:372227 fullscreen_widget_process_id_ = ChildProcessHost::kInvalidUniqueID;
[email protected]44470a22013-01-24 01:21:542228 fullscreen_widget_routing_id_ = MSG_ROUTING_NONE;
[email protected]0b431992014-06-24 00:08:032229 if (fullscreen_widget_had_focus_at_shutdown_)
2230 view_->RestoreFocus();
[email protected]44470a22013-01-24 01:21:542231 }
lfgbee1e0a2016-06-08 21:24:212232
lfg7d4caad2017-03-22 09:01:452233 if (render_widget_host == mouse_lock_widget_)
lfgff8dfc8d2016-10-21 17:42:322234 LostMouseLock(mouse_lock_widget_);
Joe Downing192998b22018-03-22 15:51:362235
Joe Downingc1a57efe2018-05-09 18:23:542236 CancelKeyboardLock(render_widget_host);
[email protected]b24b68a2012-09-24 21:57:262237}
2238
miu9e14e492014-10-25 02:39:042239void WebContentsImpl::RenderWidgetGotFocus(
2240 RenderWidgetHostImpl* render_widget_host) {
calamity7fe55ce2015-04-10 03:59:372241 // Notify the observers if an embedded fullscreen widget was focused.
2242 if (delegate_ && render_widget_host && delegate_->EmbedsFullscreenWidget() &&
2243 render_widget_host->GetView() == GetFullscreenRenderWidgetHostView()) {
Michael Thiessen896405db2017-07-20 17:52:322244 NotifyWebContentsFocused(render_widget_host);
calamity7fe55ce2015-04-10 03:59:372245 }
miu9e14e492014-10-25 02:39:042246}
2247
zijiehe3bee08e22017-05-17 04:14:462248void WebContentsImpl::RenderWidgetLostFocus(
2249 RenderWidgetHostImpl* render_widget_host) {
Michael Thiessen896405db2017-07-20 17:52:322250 // Notify the observers if an embedded fullscreen widget lost focus.
2251 if (delegate_ && render_widget_host && delegate_->EmbedsFullscreenWidget() &&
2252 render_widget_host->GetView() == GetFullscreenRenderWidgetHostView()) {
2253 NotifyWebContentsLostFocus(render_widget_host);
2254 }
zijiehe3bee08e22017-05-17 04:14:462255}
2256
estaded0a0c992015-01-21 14:12:062257void WebContentsImpl::RenderWidgetWasResized(
rouslan2f5993f2015-01-29 00:18:312258 RenderWidgetHostImpl* render_widget_host,
2259 bool width_changed) {
creisc014b402015-04-23 16:41:452260 RenderFrameHostImpl* rfh = GetMainFrame();
estadece344ec2015-01-23 00:12:292261 if (!rfh || render_widget_host != rfh->GetRenderWidgetHost())
estaded0a0c992015-01-21 14:12:062262 return;
estaded0a0c992015-01-21 14:12:062263
ericwilligersde386342016-10-19 02:35:092264 for (auto& observer : observers_)
2265 observer.MainFrameWasResized(width_changed);
estaded0a0c992015-01-21 14:12:062266}
2267
skyf65d9bb2017-03-24 02:26:392268KeyboardEventProcessingResult WebContentsImpl::PreHandleKeyboardEvent(
2269 const NativeWebKeyboardEvent& event) {
W. James MacLeane1187272019-11-12 03:42:482270 auto* outermost_contents = GetOutermostWebContents();
2271 // TODO(wjmaclean): Generalize this to forward all key events to the outermost
2272 // delegate's handler.
2273 if (outermost_contents != this && IsFullscreenForCurrentTab() &&
2274 event.windows_key_code == ui::VKEY_ESCAPE) {
2275 // When an inner WebContents has focus and is fullscreen, redirect <esc>
2276 // key events to the outermost WebContents so it can be handled by that
2277 // WebContents' delegate.
2278 if (outermost_contents->PreHandleKeyboardEvent(event) ==
2279 KeyboardEventProcessingResult::HANDLED) {
2280 return KeyboardEventProcessingResult::HANDLED;
2281 }
2282 }
skyf65d9bb2017-03-24 02:26:392283 return delegate_ ? delegate_->PreHandleKeyboardEvent(this, event)
2284 : KeyboardEventProcessingResult::NOT_HANDLED;
[email protected]63954792011-07-11 04:17:482285}
2286
Dave Tapuska344399e2019-09-09 15:18:262287bool WebContentsImpl::HandleMouseEvent(const blink::WebMouseEvent& event) {
2288 // Handle mouse button back/forward in the browser process after the render
2289 // process is done with the event. This ensures all renderer-initiated history
2290 // navigations can be treated consistently.
2291 if (event.GetType() == blink::WebInputEvent::Type::kMouseUp) {
2292 WebContentsImpl* outermost = GetOutermostWebContents();
2293 if (event.button == blink::WebPointerProperties::Button::kBack &&
2294 outermost->controller_.CanGoBack()) {
2295 outermost->controller_.GoBack();
2296 return true;
2297 } else if (event.button == blink::WebPointerProperties::Button::kForward &&
2298 outermost->controller_.CanGoForward()) {
2299 outermost->controller_.GoForward();
2300 return true;
2301 }
2302 }
2303 return false;
2304}
2305
Scott Violetd5caa872018-10-16 22:00:512306bool WebContentsImpl::HandleKeyboardEvent(const NativeWebKeyboardEvent& event) {
[email protected]0b094002014-08-20 18:28:102307 if (browser_plugin_embedder_ &&
2308 browser_plugin_embedder_->HandleKeyboardEvent(event)) {
Scott Violetd5caa872018-10-16 22:00:512309 return true;
[email protected]0b094002014-08-20 18:28:102310 }
Scott Violetd5caa872018-10-16 22:00:512311 return delegate_ && delegate_->HandleKeyboardEvent(this, event);
[email protected]63954792011-07-11 04:17:482312}
2313
[email protected]d92026ef2014-03-03 21:04:132314bool WebContentsImpl::HandleWheelEvent(
[email protected]180ef242013-11-07 06:50:462315 const blink::WebMouseWheelEvent& event) {
[email protected]bccc4472013-04-18 16:37:192316#if !defined(OS_MACOSX)
lanweia93644f2015-01-21 22:00:332317 // On platforms other than Mac, control+mousewheel may change zoom. On Mac,
2318 // this isn't done for two reasons:
[email protected]bccc4472013-04-18 16:37:192319 // -the OS already has a gesture to do this through pinch-zoom
2320 // -if a user starts an inertial scroll, let's go, and presses control
2321 // (i.e. control+tab) then the OS's buffered scroll events will come in
2322 // with control key set which isn't what the user wants
Blink Reformat1c4d759e2017-04-09 16:34:542323 if (delegate_ && event.wheel_ticks_y &&
Ehsan Karamad0f83cb112018-09-06 22:07:362324 event.event_action == blink::WebMouseWheelEvent::EventAction::kPageZoom) {
w.shackleton49bcd392016-01-06 17:38:222325 // Count only integer cumulative scrolls as zoom events; this handles
2326 // smooth scroll and regular scroll device behavior.
Blink Reformat1c4d759e2017-04-09 16:34:542327 zoom_scroll_remainder_ += event.wheel_ticks_y;
w.shackleton49bcd392016-01-06 17:38:222328 int whole_zoom_scroll_remainder_ = std::lround(zoom_scroll_remainder_);
2329 zoom_scroll_remainder_ -= whole_zoom_scroll_remainder_;
2330 if (whole_zoom_scroll_remainder_ != 0) {
2331 delegate_->ContentsZoomChange(whole_zoom_scroll_remainder_ > 0);
2332 }
[email protected]fb3f066e2013-02-12 19:12:522333 return true;
2334 }
[email protected]bccc4472013-04-18 16:37:192335#endif
[email protected]fb3f066e2013-02-12 19:12:522336 return false;
2337}
2338
[email protected]04bce562014-01-30 05:34:542339bool WebContentsImpl::PreHandleGestureEvent(
2340 const blink::WebGestureEvent& event) {
[email protected]28042d32014-02-03 14:10:032341 return delegate_ && delegate_->PreHandleGestureEvent(this, event);
[email protected]04bce562014-01-30 05:34:542342}
2343
kenrb2a565f82015-09-02 20:24:592344RenderWidgetHostInputEventRouter* WebContentsImpl::GetInputEventRouter() {
wjmacleane31234f2015-12-14 17:20:452345 if (!is_being_destroyed_ && GetOuterWebContents())
2346 return GetOuterWebContents()->GetInputEventRouter();
2347
2348 if (!rwh_input_event_router_.get() && !is_being_destroyed_)
kenrb2a565f82015-09-02 20:24:592349 rwh_input_event_router_.reset(new RenderWidgetHostInputEventRouter);
2350 return rwh_input_event_router_.get();
2351}
2352
alexmos3fcd0ca2015-10-23 18:18:332353void WebContentsImpl::ReplicatePageFocus(bool is_focused) {
2354 // Focus loss may occur while this WebContents is being destroyed. Don't
2355 // send the message in this case, as the main frame's RenderFrameHost and
2356 // other state has already been cleared.
2357 if (is_being_destroyed_)
2358 return;
2359
2360 frame_tree_.ReplicatePageFocus(is_focused);
2361}
2362
alexmosc4cbacb2015-11-21 01:29:222363RenderWidgetHostImpl* WebContentsImpl::GetFocusedRenderWidgetHost(
2364 RenderWidgetHostImpl* receiving_widget) {
alexmosc4cbacb2015-11-21 01:29:222365 // Events for widgets other than the main frame (e.g., popup menus) should be
2366 // forwarded directly to the widget they arrived on.
2367 if (receiving_widget != GetMainFrame()->GetRenderWidgetHost())
2368 return receiving_widget;
alexmosc4d183e2015-11-06 00:46:522369
lfg1453e412017-04-11 00:48:502370 WebContentsImpl* focused_contents = GetFocusedWebContents();
2371
2372 // If the focused WebContents is showing an interstitial, return the
2373 // interstitial's widget.
2374 if (focused_contents->ShowingInterstitialPage()) {
2375 return static_cast<RenderFrameHostImpl*>(
creisadca9862017-06-16 19:12:112376 focused_contents->interstitial_page_->GetMainFrame())
lfg1453e412017-04-11 00:48:502377 ->GetRenderWidgetHost();
2378 }
2379
paulmeyerfeafc2d2017-04-25 21:46:402380 // If the focused WebContents is a guest WebContents, then get the focused
2381 // frame in the embedder WebContents instead.
W. James MacLean13d834d2019-12-04 16:06:452382 FrameTreeNode* focused_frame =
2383 GetFocusedWebContents()->frame_tree_.GetFocusedFrame();
lfg1453e412017-04-11 00:48:502384
alexmosc4d183e2015-11-06 00:46:522385 if (!focused_frame)
alexmosc4cbacb2015-11-21 01:29:222386 return receiving_widget;
alexmosc4d183e2015-11-06 00:46:522387
alexmos732ca3a2015-12-08 02:01:022388 // The view may be null if a subframe's renderer process has crashed while
2389 // the subframe has focus. Drop the event in that case. Do not give
2390 // it to the main frame, so that the user doesn't unexpectedly type into the
2391 // wrong frame if a focused subframe renderer crashes while they type.
2392 RenderWidgetHostView* view = focused_frame->current_frame_host()->GetView();
2393 if (!view)
2394 return nullptr;
2395
2396 return RenderWidgetHostImpl::From(view->GetRenderWidgetHost());
alexmosc4d183e2015-11-06 00:46:522397}
2398
avallee9993fca2016-11-17 06:16:502399RenderWidgetHostImpl* WebContentsImpl::GetRenderWidgetHostWithPageFocus() {
lfg1453e412017-04-11 00:48:502400 WebContentsImpl* focused_web_contents = GetFocusedWebContents();
2401
2402 if (focused_web_contents->ShowingInterstitialPage()) {
2403 return static_cast<RenderFrameHostImpl*>(
creisadca9862017-06-16 19:12:112404 focused_web_contents->interstitial_page_->GetMainFrame())
lfg1453e412017-04-11 00:48:502405 ->GetRenderWidgetHost();
2406 }
lfg1453e412017-04-11 00:48:502407
2408 return focused_web_contents->GetMainFrame()->GetRenderWidgetHost();
avallee9993fca2016-11-17 06:16:502409}
2410
Dave Tapuska3ed44192018-05-01 18:53:302411void WebContentsImpl::EnterFullscreenMode(
2412 const GURL& origin,
Dave Tapuskaa4189512019-10-15 20:27:342413 const blink::mojom::FullscreenOptions& options) {
mlamouri7a78d6fd2015-01-17 13:23:532414 // This method is being called to enter renderer-initiated fullscreen mode.
2415 // Make sure any existing fullscreen widget is shut down first.
[email protected]4aebbca2013-09-17 22:26:492416 RenderWidgetHostView* const widget_view = GetFullscreenRenderWidgetHostView();
avi85a4cef2015-12-18 20:13:372417 if (widget_view) {
2418 RenderWidgetHostImpl::From(widget_view->GetRenderWidgetHost())
2419 ->ShutdownAndDestroyWidget(true);
2420 }
[email protected]4aebbca2013-09-17 22:26:492421
Joe Downing13dd76b2018-04-09 18:32:152422 if (delegate_) {
Dave Tapuska3ed44192018-05-01 18:53:302423 delegate_->EnterFullscreenModeForTab(this, origin, options);
mlamouri7a78d6fd2015-01-17 13:23:532424
Joe Downing13dd76b2018-04-09 18:32:152425 if (keyboard_lock_widget_)
2426 delegate_->RequestKeyboardLock(this, esc_key_locked_);
2427 }
2428
ericwilligersde386342016-10-19 02:35:092429 for (auto& observer : observers_)
2430 observer.DidToggleFullscreenModeForTab(IsFullscreenForCurrentTab(), false);
mlamouri7a78d6fd2015-01-17 13:23:532431}
2432
bokanece34a82016-01-28 19:49:462433void WebContentsImpl::ExitFullscreenMode(bool will_cause_resize) {
mlamouri7a78d6fd2015-01-17 13:23:532434 // This method is being called to leave renderer-initiated fullscreen mode.
2435 // Make sure any existing fullscreen widget is shut down first.
2436 RenderWidgetHostView* const widget_view = GetFullscreenRenderWidgetHostView();
avi85a4cef2015-12-18 20:13:372437 if (widget_view) {
2438 RenderWidgetHostImpl::From(widget_view->GetRenderWidgetHost())
2439 ->ShutdownAndDestroyWidget(true);
2440 }
mlamouri7a78d6fd2015-01-17 13:23:532441
Joe Downing13dd76b2018-04-09 18:32:152442 if (delegate_) {
mlamouri7a78d6fd2015-01-17 13:23:532443 delegate_->ExitFullscreenModeForTab(this);
[email protected]657c8e02014-03-19 19:18:372444
Joe Downing13dd76b2018-04-09 18:32:152445 if (keyboard_lock_widget_)
2446 delegate_->CancelKeyboardLockRequest(this);
2447 }
2448
bokanece34a82016-01-28 19:49:462449 // The fullscreen state is communicated to the renderer through a resize
2450 // message. If the change in fullscreen state doesn't cause a view resize
2451 // then we must ensure web contents exit the fullscreen state by explicitly
2452 // sending a resize message. This is required for the situation of the browser
2453 // moving the view into a "browser fullscreen" state and then the contents
2454 // entering "tab fullscreen". Exiting the contents "tab fullscreen" then won't
2455 // have the side effect of the view resizing, hence the explicit call here is
2456 // required.
2457 if (!will_cause_resize) {
2458 if (RenderWidgetHostView* rwhv = GetRenderWidgetHostView()) {
2459 if (RenderWidgetHost* render_widget_host = rwhv->GetRenderWidgetHost())
Fady Samuel0b911822018-04-25 13:22:162460 render_widget_host->SynchronizeVisualProperties();
bokanece34a82016-01-28 19:49:462461 }
scheib3dcb6c932015-02-23 10:55:582462 }
2463
Arthur Sonzogni929d29f2018-09-17 14:19:442464 current_fullscreen_frame_ = nullptr;
Becca Hughese9e27952018-06-25 17:08:272465
ericwilligersde386342016-10-19 02:35:092466 for (auto& observer : observers_) {
2467 observer.DidToggleFullscreenModeForTab(IsFullscreenForCurrentTab(),
2468 will_cause_resize);
2469 }
Becca Hughesd11d6502018-07-31 17:01:282470
2471 if (display_cutout_host_impl_)
2472 display_cutout_host_impl_->DidExitFullscreen();
[email protected]8a5e0ca2011-08-25 06:30:472473}
2474
Becca Hughesfd5d8f892018-06-14 18:23:362475void WebContentsImpl::FullscreenStateChanged(RenderFrameHost* rfh,
2476 bool is_fullscreen) {
Arthur Sonzogni929d29f2018-09-17 14:19:442477 RenderFrameHostImpl* frame = static_cast<RenderFrameHostImpl*>(rfh);
Becca Hughesfd5d8f892018-06-14 18:23:362478
2479 if (is_fullscreen) {
Jan Wilken Dörrie531be7ca2019-06-07 10:05:572480 if (!base::Contains(fullscreen_frames_, frame)) {
Arthur Sonzogni929d29f2018-09-17 14:19:442481 fullscreen_frames_.insert(frame);
2482 FullscreenFrameSetUpdated();
Becca Hughesfd5d8f892018-06-14 18:23:362483 }
Arthur Sonzogni929d29f2018-09-17 14:19:442484 return;
2485 }
Becca Hughesfd5d8f892018-06-14 18:23:362486
Arthur Sonzogni929d29f2018-09-17 14:19:442487 // If |frame| is no longer in fullscreen, remove it and any descendants.
2488 // See https://fullscreen.spec.whatwg.org.
Arthur Sonzogni969a794e2018-10-11 08:56:092489 size_t size_before_deletion = fullscreen_frames_.size();
2490 base::EraseIf(fullscreen_frames_, [&](RenderFrameHostImpl* current) {
2491 return (current == frame || current->IsDescendantOf(frame));
Arthur Sonzogni929d29f2018-09-17 14:19:442492 });
2493
Arthur Sonzogni969a794e2018-10-11 08:56:092494 if (size_before_deletion != fullscreen_frames_.size())
Arthur Sonzogni929d29f2018-09-17 14:19:442495 FullscreenFrameSetUpdated();
2496}
2497
2498void WebContentsImpl::FullscreenFrameSetUpdated() {
2499 if (fullscreen_frames_.empty()) {
2500 current_fullscreen_frame_ = nullptr;
2501 return;
Becca Hughesfd5d8f892018-06-14 18:23:362502 }
2503
Arthur Sonzogni929d29f2018-09-17 14:19:442504 // Find the current fullscreen frame and call the observers.
2505 // If frame A is fullscreen, then frame B goes into inner fullscreen, then B
2506 // exits fullscreen - that will result in A being fullscreen.
2507 RenderFrameHostImpl* new_fullscreen_frame = *std::max_element(
2508 fullscreen_frames_.begin(), fullscreen_frames_.end(), FrameCompareDepth);
Becca Hughesfd5d8f892018-06-14 18:23:362509
Arthur Sonzogni929d29f2018-09-17 14:19:442510 // If we have already notified observers about this frame then we should not
2511 // fire the observers again.
2512 if (new_fullscreen_frame == current_fullscreen_frame_)
2513 return;
2514 current_fullscreen_frame_ = new_fullscreen_frame;
Becca Hughesfd5d8f892018-06-14 18:23:362515
Arthur Sonzogni929d29f2018-09-17 14:19:442516 for (auto& observer : observers_)
2517 observer.DidAcquireFullscreen(new_fullscreen_frame);
Becca Hughesfd5d8f892018-06-14 18:23:362518
Arthur Sonzogni929d29f2018-09-17 14:19:442519 if (display_cutout_host_impl_)
2520 display_cutout_host_impl_->DidAcquireFullscreen(new_fullscreen_frame);
Becca Hughesfd5d8f892018-06-14 18:23:362521}
2522
Collin Baker989e0882019-11-01 01:27:172523void WebContentsImpl::UpdateVisibilityAndNotifyPageAndView(
2524 Visibility new_visibility) {
Collin Baker989e0882019-11-01 01:27:172525 // Only hide the page if there are no entities capturing screenshots
Collin Bakerc5259962019-11-14 17:51:582526 // or video (e.g. mirroring). If there are, apply the correct state of
2527 // kHidden or kHiddenButPainting.
2528 PageVisibilityState page_visibility;
2529 if (new_visibility == Visibility::VISIBLE || visible_capturer_count_ > 0)
2530 page_visibility = PageVisibilityState::kVisible;
2531 else if (hidden_capturer_count_ > 0)
2532 page_visibility = PageVisibilityState::kHiddenButPainting;
2533 else
2534 page_visibility = PageVisibilityState::kHidden;
Collin Baker989e0882019-11-01 01:27:172535 // If there are entities in Picture-in-Picture mode, don't activate
2536 // the "disable rendering" optimization.
Collin Bakerc5259962019-11-14 17:51:582537 const bool view_is_visible =
2538 page_visibility != PageVisibilityState::kHidden ||
2539 HasPictureInPictureVideo();
Collin Baker989e0882019-11-01 01:27:172540
Collin Bakerc5259962019-11-14 17:51:582541 if (page_visibility != PageVisibilityState::kHidden) {
Dana Friedcf3bc8e2019-11-13 20:00:242542 // We cannot show a page or capture video unless there is a valid renderer
2543 // associated with this web contents. The navigation controller for this
2544 // page must be set to active (allowing navigation to complete, a renderer
2545 // and its associated views to be created, etc.) if any of these conditions
2546 // holds.
2547 //
2548 // Previously, it was possible for browser-side code to try to capture video
2549 // from a restored tab (for a variety of reasons, including the browser
2550 // creating preview thumbnails) and the tab would never actually load. By
Collin Bakerc5259962019-11-14 17:51:582551 // keying this behavior off of |page_visibility| instead of just
Dana Friedcf3bc8e2019-11-13 20:00:242552 // |new_visibility| we avoid this case. See crbug.com/1020782 for more
2553 // context.
2554 controller_.SetActive(true);
2555
2556 // This shows the Page before showing the individual RenderWidgets, as
2557 // RenderWidgets will work to produce compositor frames and handle input
2558 // as soon as they are shown. But the Page and other classes do not expect
2559 // to be producing frames when the Page is hidden. So we make sure the Page
2560 // is shown first.
Collin Bakerc5259962019-11-14 17:51:582561 SendPageMessage(
2562 new PageMsg_VisibilityChanged(MSG_ROUTING_NONE, page_visibility));
Collin Baker98457b52019-11-06 21:34:292563 }
Collin Baker989e0882019-11-01 01:27:172564
2565 // |GetRenderWidgetHostView()| can be null if the user middle clicks a link to
2566 // open a tab in the background, then closes the tab before selecting it.
2567 // This is because closing the tab calls WebContentsImpl::Destroy(), which
2568 // removes the |GetRenderViewHost()|; then when we actually destroy the
2569 // window, OnWindowPosChanged() notices and calls WasHidden() (which
2570 // calls us).
2571 if (auto* view = GetRenderWidgetHostView()) {
2572 if (view_is_visible) {
2573 view->Show();
Collin Baker989e0882019-11-01 01:27:172574 } else if (new_visibility == Visibility::HIDDEN) {
2575 view->Hide();
2576 } else {
2577 view->WasOccluded();
2578 }
2579 }
2580
2581 if (!ShowingInterstitialPage())
2582 SetVisibilityForChildViews(view_is_visible);
2583
2584 // Make sure to call SetVisibilityAndNotifyObservers(VISIBLE) before notifying
2585 // the CrossProcessFrameConnector.
2586 if (new_visibility == Visibility::VISIBLE) {
2587 last_active_time_ = base::TimeTicks::Now();
2588 SetVisibilityAndNotifyObservers(new_visibility);
2589 }
2590
Collin Bakerc5259962019-11-14 17:51:582591 if (page_visibility == PageVisibilityState::kHidden) {
Collin Baker989e0882019-11-01 01:27:172592 // Similar to when showing the page, we only hide the page after
2593 // hiding the individual RenderWidgets.
Collin Baker98457b52019-11-06 21:34:292594 SendPageMessage(new PageMsg_VisibilityChanged(
2595 MSG_ROUTING_NONE, PageVisibilityState::kHidden));
Collin Baker989e0882019-11-01 01:27:172596 } else {
2597 for (FrameTreeNode* node : frame_tree_.Nodes()) {
2598 RenderFrameProxyHost* parent = node->render_manager()->GetProxyToParent();
2599 if (!parent)
2600 continue;
2601
2602 parent->cross_process_frame_connector()->DelegateWasShown();
2603 }
2604 }
2605
2606 if (new_visibility != Visibility::VISIBLE)
2607 SetVisibilityAndNotifyObservers(new_visibility);
2608}
2609
Clark DuValla0e42992018-09-17 17:00:112610#if defined(OS_ANDROID)
2611void WebContentsImpl::UpdateUserGestureCarryoverInfo() {
2612 if (delegate_)
2613 delegate_->UpdateUserGestureCarryoverInfo(this);
2614}
2615#endif
2616
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:412617bool WebContentsImpl::IsFullscreenForCurrentTab() {
[email protected]199bba6e2012-04-04 16:19:382618 return delegate_ ? delegate_->IsFullscreenForTabOrPending(this) : false;
[email protected]5d5f7af2011-10-01 01:38:122619}
2620
Malay Keshav87c42c02019-01-15 08:37:472621bool WebContentsImpl::ShouldShowStaleContentOnEviction() {
2622 return GetDelegate() && GetDelegate()->ShouldShowStaleContentOnEviction(this);
2623}
2624
billorr21c005d2016-11-17 03:57:042625bool WebContentsImpl::IsFullscreen() {
2626 return IsFullscreenForCurrentTab();
2627}
2628
Eric Willigers052f0432019-10-04 04:06:572629blink::mojom::DisplayMode WebContentsImpl::GetDisplayMode(
avibf58fa0c2015-11-11 01:45:522630 RenderWidgetHostImpl* render_widget_host) const {
2631 if (!RenderViewHostImpl::From(render_widget_host))
Eric Willigers052f0432019-10-04 04:06:572632 return blink::mojom::DisplayMode::kBrowser;
avibf58fa0c2015-11-11 01:45:522633
mikhail.pozdnyakovc0e251b2015-04-15 06:51:122634 return delegate_ ? delegate_->GetDisplayMode(this)
Eric Willigers052f0432019-10-04 04:06:572635 : blink::mojom::DisplayMode::kBrowser;
mikhail.pozdnyakovc0e251b2015-04-15 06:51:122636}
2637
avic3aa8422015-11-09 20:57:222638void WebContentsImpl::RequestToLockMouse(
2639 RenderWidgetHostImpl* render_widget_host,
2640 bool user_gesture,
lfgad8244352016-07-13 16:55:512641 bool last_unlocked_by_target,
2642 bool privileged) {
lfgf0cd46e2017-01-04 00:05:232643 for (WebContentsImpl* current = this; current;
2644 current = current->GetOuterWebContents()) {
2645 if (current->mouse_lock_widget_) {
2646 render_widget_host->GotResponseToLockMouseRequest(false);
2647 return;
2648 }
[email protected]e9621112011-10-17 05:38:372649 }
avic3aa8422015-11-09 20:57:222650
lfgad8244352016-07-13 16:55:512651 if (privileged) {
lfgf0cd46e2017-01-04 00:05:232652 DCHECK(!GetOuterWebContents());
lfgad8244352016-07-13 16:55:512653 mouse_lock_widget_ = render_widget_host;
2654 render_widget_host->GotResponseToLockMouseRequest(true);
2655 return;
2656 }
2657
lfgbee1e0a2016-06-08 21:24:212658 bool widget_in_frame_tree = false;
2659 for (FrameTreeNode* node : frame_tree_.Nodes()) {
2660 if (node->current_frame_host()->GetRenderWidgetHost() ==
2661 render_widget_host) {
2662 widget_in_frame_tree = true;
2663 break;
2664 }
2665 }
2666
2667 if (widget_in_frame_tree && delegate_) {
lfgf0cd46e2017-01-04 00:05:232668 for (WebContentsImpl* current = this; current;
2669 current = current->GetOuterWebContents()) {
2670 current->mouse_lock_widget_ = render_widget_host;
2671 }
2672
avic3aa8422015-11-09 20:57:222673 delegate_->RequestToLockMouse(this, user_gesture, last_unlocked_by_target);
lfgbee1e0a2016-06-08 21:24:212674 } else {
2675 render_widget_host->GotResponseToLockMouseRequest(false);
2676 }
[email protected]e9621112011-10-17 05:38:372677}
2678
avi3e4b8512015-11-11 01:55:492679void WebContentsImpl::LostMouseLock(RenderWidgetHostImpl* render_widget_host) {
lfgbee1e0a2016-06-08 21:24:212680 CHECK(mouse_lock_widget_);
lfgf0cd46e2017-01-04 00:05:232681
2682 if (mouse_lock_widget_->delegate()->GetAsWebContents() != this)
2683 return mouse_lock_widget_->delegate()->LostMouseLock(render_widget_host);
2684
lfgbee1e0a2016-06-08 21:24:212685 mouse_lock_widget_->SendMouseLockLost();
lfgf0cd46e2017-01-04 00:05:232686 for (WebContentsImpl* current = this; current;
2687 current = current->GetOuterWebContents()) {
2688 current->mouse_lock_widget_ = nullptr;
2689 }
avi3e4b8512015-11-11 01:55:492690
[email protected]e9621112011-10-17 05:38:372691 if (delegate_)
2692 delegate_->LostMouseLock();
2693}
2694
lfgad8244352016-07-13 16:55:512695bool WebContentsImpl::HasMouseLock(RenderWidgetHostImpl* render_widget_host) {
2696 // To verify if the mouse is locked, the mouse_lock_widget_ needs to be
2697 // assigned to the widget that requested the mouse lock, and the top-level
2698 // platform RenderWidgetHostView needs to hold the mouse lock from the OS.
Lucas Gadania0ea0172018-09-20 18:31:372699 auto* widget_host = GetTopLevelRenderWidgetHostView();
2700 return mouse_lock_widget_ == render_widget_host && widget_host &&
2701 widget_host->IsMouseLocked();
lfgad8244352016-07-13 16:55:512702}
2703
lfg84763c92017-02-16 18:55:152704RenderWidgetHostImpl* WebContentsImpl::GetMouseLockWidget() {
Lucas Gadania0ea0172018-09-20 18:31:372705 auto* widget_host = GetTopLevelRenderWidgetHostView();
2706 if ((widget_host && widget_host->IsMouseLocked()) ||
lfge2176fb2017-03-26 00:45:472707 (GetFullscreenRenderWidgetHostView() &&
Lucas Gadania0ea0172018-09-20 18:31:372708 GetFullscreenRenderWidgetHostView()->IsMouseLocked())) {
lfg84763c92017-02-16 18:55:152709 return mouse_lock_widget_;
Lucas Gadania0ea0172018-09-20 18:31:372710 }
lfg84763c92017-02-16 18:55:152711
2712 return nullptr;
2713}
2714
Joe Downing192998b22018-03-22 15:51:362715bool WebContentsImpl::RequestKeyboardLock(
Joe Downing13dd76b2018-04-09 18:32:152716 RenderWidgetHostImpl* render_widget_host,
2717 bool esc_key_locked) {
Joe Downing192998b22018-03-22 15:51:362718 DCHECK(render_widget_host);
Joe Downing192998b22018-03-22 15:51:362719 if (render_widget_host->delegate()->GetAsWebContents() != this) {
2720 NOTREACHED();
2721 return false;
2722 }
2723
2724 // KeyboardLock is only supported when called by the top-level browsing
2725 // context and is not supported in embedded content scenarios.
2726 if (GetOuterWebContents())
2727 return false;
2728
Joe Downing13dd76b2018-04-09 18:32:152729 esc_key_locked_ = esc_key_locked;
Joe Downing192998b22018-03-22 15:51:362730 keyboard_lock_widget_ = render_widget_host;
2731
Joe Downing13dd76b2018-04-09 18:32:152732 if (delegate_)
2733 delegate_->RequestKeyboardLock(this, esc_key_locked_);
Joe Downing192998b22018-03-22 15:51:362734 return true;
2735}
2736
2737void WebContentsImpl::CancelKeyboardLock(
2738 RenderWidgetHostImpl* render_widget_host) {
2739 if (!keyboard_lock_widget_ || render_widget_host != keyboard_lock_widget_)
2740 return;
2741
2742 RenderWidgetHostImpl* old_keyboard_lock_widget = keyboard_lock_widget_;
2743 keyboard_lock_widget_ = nullptr;
2744
Joe Downing13dd76b2018-04-09 18:32:152745 if (delegate_)
2746 delegate_->CancelKeyboardLockRequest(this);
2747
Joe Downing192998b22018-03-22 15:51:362748 old_keyboard_lock_widget->CancelKeyboardLock();
2749}
2750
2751RenderWidgetHostImpl* WebContentsImpl::GetKeyboardLockWidget() {
2752 return keyboard_lock_widget_;
2753}
2754
Dave Tapuskac3344362019-02-20 17:54:282755void WebContentsImpl::OnRenderFrameProxyVisibilityChanged(
2756 blink::mojom::FrameVisibility visibility) {
2757 switch (visibility) {
2758 case blink::mojom::FrameVisibility::kRenderedInViewport:
2759 WasShown();
2760 break;
2761 case blink::mojom::FrameVisibility::kNotRendered:
2762 WasHidden();
2763 break;
2764 case blink::mojom::FrameVisibility::kRenderedOutOfViewport:
2765 WasOccluded();
2766 break;
2767 }
ekaramada4f42692016-02-11 19:48:372768}
2769
Albert J. Wong65fe64d2019-09-20 02:48:142770RenderFrameHostDelegate* WebContentsImpl::CreateNewWindow(
csharrison95f01e922017-04-24 18:52:352771 RenderFrameHost* opener,
rockot5c478a72016-09-28 23:14:182772 const mojom::CreateNewWindowParams& params,
Albert J. Wong65fe64d2019-09-20 02:48:142773 bool is_new_browsing_instance,
Sergey Kuznetsov32257a22019-02-11 20:26:502774 bool has_user_gesture,
[email protected]97714c82012-06-06 10:15:132775 SessionStorageNamespace* session_storage_namespace) {
csharrison95f01e922017-04-24 18:52:352776 DCHECK(opener);
nick5ae4d2d2017-01-06 01:18:352777
csharrison95f01e922017-04-24 18:52:352778 int render_process_id = opener->GetProcess()->GetID();
csharrison95f01e922017-04-24 18:52:352779
Albert J. Wong65fe64d2019-09-20 02:48:142780 SiteInstance* source_site_instance = opener->GetSiteInstance();
nick5ae4d2d2017-01-06 01:18:352781
[email protected]bafe6cd2012-05-23 23:09:502782 // We usually create the new window in the same BrowsingInstance (group of
2783 // script-related windows), by passing in the current SiteInstance. However,
[email protected]c4538072013-03-18 02:17:552784 // if the opener is being suppressed (in a non-guest), we create a new
2785 // SiteInstance in its own BrowsingInstance.
[email protected]a24efc22014-05-26 15:50:252786 bool is_guest = BrowserPluginGuest::IsGuest(this);
[email protected]c4538072013-03-18 02:17:552787
[email protected]bafe6cd2012-05-23 23:09:502788 scoped_refptr<SiteInstance> site_instance =
alexmos4cf2aa32015-07-15 23:40:432789 params.opener_suppressed && !is_guest
2790 ? SiteInstance::CreateForURL(GetBrowserContext(), params.target_url)
2791 : source_site_instance;
[email protected]bafe6cd2012-05-23 23:09:502792
[email protected]d1198fd2012-08-13 22:50:192793 // We must assign the SessionStorageNamespace before calling Init().
[email protected]4c3a23582012-08-18 08:54:342794 //
2795 // http://crbug.com/142685
[email protected]fdac6ade2013-07-20 01:06:302796 const std::string& partition_id =
2797 GetContentClient()->browser()->
2798 GetStoragePartitionIdForSite(GetBrowserContext(),
2799 site_instance->GetSiteURL());
[email protected]fc72bb12013-06-02 21:13:462800 StoragePartition* partition = BrowserContext::GetStoragePartition(
2801 GetBrowserContext(), site_instance.get());
[email protected]5f2aa722013-08-07 16:59:412802 DOMStorageContextWrapper* dom_storage_context =
2803 static_cast<DOMStorageContextWrapper*>(partition->GetDOMStorageContext());
[email protected]d1198fd2012-08-13 22:50:192804 SessionStorageNamespaceImpl* session_storage_namespace_impl =
2805 static_cast<SessionStorageNamespaceImpl*>(session_storage_namespace);
2806 CHECK(session_storage_namespace_impl->IsFromContext(dom_storage_context));
[email protected]dd6730412013-08-14 15:03:372807
Albert J. Wong1ceccef92019-10-08 08:25:202808 if (delegate_ && delegate_->IsWebContentsCreationOverridden(
2809 source_site_instance, params.window_container_type,
2810 opener->GetLastCommittedURL(), params.frame_name,
2811 params.target_url)) {
2812 return static_cast<WebContentsImpl*>(delegate_->CreateCustomWebContents(
2813 opener, source_site_instance, is_new_browsing_instance,
2814 opener->GetLastCommittedURL(), params.frame_name, params.target_url,
2815 partition_id, session_storage_namespace));
[email protected]dd6730412013-08-14 15:03:372816 }
2817
danakj22b4e8b2019-08-01 23:14:392818 bool renderer_started_hidden =
2819 params.disposition == WindowOpenDisposition::NEW_BACKGROUND_TAB;
2820
[email protected]dd6730412013-08-14 15:03:372821 // Create the new web contents. This will automatically create the new
2822 // WebContentsView. In the future, we may want to create the view separately.
[email protected]fc72bb12013-06-02 21:13:462823 CreateParams create_params(GetBrowserContext(), site_instance.get());
nasko48321ca32015-07-02 20:44:122824 create_params.main_frame_name = params.frame_name;
alexmos4cf2aa32015-07-15 23:40:432825 create_params.opener_render_process_id = render_process_id;
csharrison95f01e922017-04-24 18:52:352826 create_params.opener_render_frame_id = opener->GetRoutingID();
[email protected]50d326e2014-05-20 17:59:062827 create_params.opener_suppressed = params.opener_suppressed;
danakj22b4e8b2019-08-01 23:14:392828 create_params.initially_hidden = renderer_started_hidden;
[email protected]50d326e2014-05-20 17:59:062829
Albert J. Wong65fe64d2019-09-20 02:48:142830 // Even though all codepaths leading here are in response to a renderer
2831 // tryng to open a new window, if the new window ends up in a different
2832 // browsing instance, then the RenderViewHost, RenderWidgetHost,
2833 // RenderFrameHost constellation is effectively browser initiated
2834 // the opener's process will not given the routing IDs for the new
2835 // objects.
2836 create_params.renderer_initiated_creation = !is_new_browsing_instance;
Albert J. Wong1ceccef92019-10-08 08:25:202837
2838 // If |is_new_browsing_instance| is true, defer routing_id allocation
2839 // to the WebContentsImpl::Create() call. This is required because with
2840 // a new browsing instance, WebContentsImpl::Create() may elect a different
2841 // SiteInstance from |site_instance| (which happens if |site_instance| is
2842 // nullptr for example).
2843 //
2844 // TODO(ajwong): This routing id allocation should be pushed down further
2845 // into WebContentsImpl::Create().
2846 if (!is_new_browsing_instance) {
2847 create_params.routing_id = opener->GetProcess()->GetNextRoutingID();
2848 create_params.main_frame_routing_id =
2849 opener->GetProcess()->GetNextRoutingID();
2850 create_params.main_frame_widget_routing_id =
2851 opener->GetProcess()->GetNextRoutingID();
2852 }
Albert J. Wong65fe64d2019-09-20 02:48:142853
2854 std::unique_ptr<WebContentsImpl> new_contents;
[email protected]c4538072013-03-18 02:17:552855 if (!is_guest) {
2856 create_params.context = view_->GetNativeView();
Albert J. Wong65fe64d2019-09-20 02:48:142857 new_contents = WebContentsImpl::Create(create_params);
2858 } else {
2859 new_contents = base::WrapUnique(static_cast<WebContentsImpl*>(
2860 GetBrowserPluginGuest()->CreateNewGuestWindow(create_params)));
[email protected]c4538072013-03-18 02:17:552861 }
Albert J. Wong65fe64d2019-09-20 02:48:142862 auto* new_contents_impl = new_contents.get();
danakje6c01622018-10-05 19:39:262863
2864 new_contents_impl->GetController().SetSessionStorageNamespace(
erikchenbee5c9622018-04-27 19:30:252865 partition_id, session_storage_namespace);
[email protected]d1198fd2012-08-13 22:50:192866
alexmos646fec02015-07-25 00:11:492867 // If the new frame has a name, make sure any SiteInstances that can find
2868 // this named frame have proxies for it. Must be called after
2869 // SetSessionStorageNamespace, since this calls CreateRenderView, which uses
2870 // GetSessionStorageNamespace.
2871 if (!params.frame_name.empty())
danakje6c01622018-10-05 19:39:262872 new_contents_impl->GetRenderManager()->CreateProxiesForNewNamedFrame();
alexmos646fec02015-07-25 00:11:492873
[email protected]c4538072013-03-18 02:17:552874 // Save the window for later if we're not suppressing the opener (since it
[email protected]d70bea92013-04-05 04:23:342875 // will be shown immediately).
2876 if (!params.opener_suppressed) {
2877 if (!is_guest) {
danakje6c01622018-10-05 19:39:262878 WebContentsView* new_view = new_contents_impl->view_.get();
[email protected]bafe6cd2012-05-23 23:09:502879
[email protected]d70bea92013-04-05 04:23:342880 // TODO(brettw): It seems bogus that we have to call this function on the
2881 // newly created object and give it one of its own member variables.
danakj22b4e8b2019-08-01 23:14:392882 RenderWidgetHostView* widget_view = new_view->CreateViewForWidget(
W. James MacLean13d834d2019-12-04 16:06:452883 new_contents_impl->GetRenderViewHost()->GetWidget());
danakj22b4e8b2019-08-01 23:14:392884 if (!renderer_started_hidden) {
2885 // RenderWidgets for frames always initialize as hidden. If the renderer
2886 // created this window as visible, then we show it here.
2887 widget_view->Show();
2888 }
[email protected]d70bea92013-04-05 04:23:342889 }
[email protected]bafe6cd2012-05-23 23:09:502890 // Save the created window associated with the route so we can show it
2891 // later.
Albert J. Wongcb004632018-07-10 22:58:252892 //
2893 // TODO(ajwong): This should be keyed off the RenderFrame routing id or the
2894 // FrameTreeNode id instead of the routing id of the Widget for the main
2895 // frame. https://crbug.com/545684
Albert J. Wong1ceccef92019-10-08 08:25:202896 DCHECK_NE(MSG_ROUTING_NONE, create_params.main_frame_routing_id);
2897 GlobalRoutingID id(render_process_id,
2898 create_params.main_frame_widget_routing_id);
Albert J. Wong65fe64d2019-09-20 02:48:142899 pending_contents_[id] = std::move(new_contents);
danakje6c01622018-10-05 19:39:262900 AddDestructionObserver(new_contents_impl);
[email protected]bafe6cd2012-05-23 23:09:502901 }
2902
2903 if (delegate_) {
Bryan McQuaded2b93c02017-07-14 22:26:312904 delegate_->WebContentsCreated(this, render_process_id,
2905 opener->GetRoutingID(), params.frame_name,
danakje6c01622018-10-05 19:39:262906 params.target_url, new_contents_impl);
[email protected]bafe6cd2012-05-23 23:09:502907 }
2908
Charlie Harrisonf60dd1c2018-08-15 21:15:412909 for (auto& observer : observers_) {
danakje6c01622018-10-05 19:39:262910 observer.DidOpenRequestedURL(new_contents_impl, opener, params.target_url,
Leon Han963dc182018-11-06 05:41:482911 params.referrer.To<Referrer>(),
2912 params.disposition, ui::PAGE_TRANSITION_LINK,
Charlie Harrisonf60dd1c2018-08-15 21:15:412913 false, // started_from_context_menu
2914 true); // renderer_initiated
pnolandaae574e2017-03-06 21:04:212915 }
2916
Avi Drissmanba3b1b342017-08-29 18:16:132917 // Any new WebContents opened while this WebContents is in fullscreen can be
2918 // used to confuse the user, so drop fullscreen.
Avi Drissmand18c51972018-08-15 18:32:092919 ForSecurityDropFullscreen();
Avi Drissmanba3b1b342017-08-29 18:16:132920
[email protected]bafe6cd2012-05-23 23:09:502921 if (params.opener_suppressed) {
2922 // When the opener is suppressed, the original renderer cannot access the
2923 // new window. As a result, we need to show and navigate the window here.
[email protected]eda238a12012-09-07 23:44:002924 bool was_blocked = false;
erikchenbee5c9622018-04-27 19:30:252925
[email protected]eda238a12012-09-07 23:44:002926 if (delegate_) {
danakje6c01622018-10-05 19:39:262927 base::WeakPtr<WebContentsImpl> weak_new_contents =
2928 new_contents_impl->weak_factory_.GetWeakPtr();
nick0d558482017-02-13 22:27:222929
danakje6c01622018-10-05 19:39:262930 gfx::Rect initial_rect; // Report an empty initial rect.
Albert J. Wong65fe64d2019-09-20 02:48:142931 delegate_->AddNewContents(this, std::move(new_contents),
erikchenbee5c9622018-04-27 19:30:252932 params.disposition, initial_rect,
Sergey Kuznetsov32257a22019-02-11 20:26:502933 has_user_gesture, &was_blocked);
danakje6c01622018-10-05 19:39:262934 // The delegate may delete |new_contents_impl| during AddNewContents().
nick0d558482017-02-13 22:27:222935 if (!weak_new_contents)
Albert J. Wong65fe64d2019-09-20 02:48:142936 return nullptr;
[email protected]eda238a12012-09-07 23:44:002937 }
erikchenbee5c9622018-04-27 19:30:252938
[email protected]eda238a12012-09-07 23:44:002939 if (!was_blocked) {
Nasko Oskov83a8cf92018-10-19 14:58:562940 std::unique_ptr<NavigationController::LoadURLParams> load_params =
2941 std::make_unique<NavigationController::LoadURLParams>(
2942 params.target_url);
Nasko Oskov93e7c55c2018-12-19 01:59:292943 load_params->initiator_origin = opener->GetLastCommittedOrigin();
Aaron Colwell9dab1652019-12-09 18:29:492944 load_params->source_site_instance = source_site_instance;
Leon Han963dc182018-11-06 05:41:482945 load_params->referrer = params.referrer.To<Referrer>();
Nasko Oskov83a8cf92018-10-19 14:58:562946 load_params->transition_type = ui::PAGE_TRANSITION_LINK;
2947 load_params->is_renderer_initiated = true;
Sergey Kuznetsov32257a22019-02-11 20:26:502948 load_params->has_user_gesture = has_user_gesture;
mariakhomenkoa4971c12015-07-21 19:04:372949
2950 if (delegate_ && !is_guest &&
2951 !delegate_->ShouldResumeRequestsForCreatedWindow()) {
Nasko Oskov83a8cf92018-10-19 14:58:562952 // We are in asynchronous add new contents path, delay navigation.
2953 DCHECK(!new_contents_impl->delayed_open_url_params_);
2954 new_contents_impl->delayed_load_url_params_ = std::move(load_params);
mariakhomenkoa4971c12015-07-21 19:04:372955 } else {
Nasko Oskov83a8cf92018-10-19 14:58:562956 new_contents_impl->controller_.LoadURLWithParams(*load_params.get());
Nasko Oskov28ebd5b2018-12-07 22:29:332957 if (!is_guest)
2958 new_contents_impl->Focus();
mariakhomenkoa4971c12015-07-21 19:04:372959 }
[email protected]eda238a12012-09-07 23:44:002960 }
[email protected]bafe6cd2012-05-23 23:09:502961 }
Albert J. Wong65fe64d2019-09-20 02:48:142962 return new_contents_impl;
[email protected]bafe6cd2012-05-23 23:09:502963}
2964
Erik Chenc71b51fe2019-09-19 21:34:272965void WebContentsImpl::CreateNewWidget(int32_t render_process_id,
2966 int32_t widget_route_id,
2967 mojo::PendingRemote<mojom::Widget> widget,
2968 RenderViewHostImpl* render_view_host) {
Albert J. Wongf6e13ed2018-09-18 15:25:472969 CreateNewWidget(render_process_id, widget_route_id, /*is_fullscreen=*/false,
Erik Chenc71b51fe2019-09-19 21:34:272970 std::move(widget), render_view_host);
[email protected]bafe6cd2012-05-23 23:09:502971}
2972
Julie Jeongeun Kimcaacf9102019-09-12 02:13:052973void WebContentsImpl::CreateNewFullscreenWidget(
2974 int32_t render_process_id,
2975 int32_t widget_route_id,
Erik Chenc71b51fe2019-09-19 21:34:272976 mojo::PendingRemote<mojom::Widget> widget,
2977 RenderViewHostImpl* render_view_host) {
Albert J. Wongf6e13ed2018-09-18 15:25:472978 CreateNewWidget(render_process_id, widget_route_id, /*is_fullscreen=*/true,
Erik Chenc71b51fe2019-09-19 21:34:272979 std::move(widget), render_view_host);
[email protected]bafe6cd2012-05-23 23:09:502980}
2981
Erik Chenc71b51fe2019-09-19 21:34:272982void WebContentsImpl::CreateNewWidget(int32_t render_process_id,
2983 int32_t route_id,
2984 bool is_fullscreen,
2985 mojo::PendingRemote<mojom::Widget> widget,
2986 RenderViewHostImpl* render_view_host) {
kenrba26d15092016-02-10 22:00:252987 RenderProcessHost* process = RenderProcessHost::FromID(render_process_id);
kenrbb46f86a2015-12-07 07:53:232988 // A message to create a new widget can only come from an active process for
[email protected]a8504022013-12-04 20:23:512989 // this WebContentsImpl instance. If any other process sends the request,
2990 // it is invalid and the process must be terminated.
dcheng57e39e22016-01-21 00:25:382991 if (!HasMatchingProcess(&frame_tree_, render_process_id)) {
Lukasz Anforowicz6f746282018-01-04 23:24:512992 ReceivedBadMessage(process, bad_message::WCI_NEW_WIDGET_PROCESS_MISMATCH);
[email protected]a8504022013-12-04 20:23:512993 return;
2994 }
2995
Dave Tapuska485aca92017-08-08 00:47:582996 RenderWidgetHostImpl* widget_host = new RenderWidgetHostImpl(
David Black9cca3592019-11-06 23:02:222997 this, process, route_id, std::move(widget), IsHidden(),
2998 std::make_unique<FrameTokenMessageQueue>());
[email protected]cfd80b02014-05-01 17:46:482999 RenderWidgetHostViewBase* widget_view =
3000 static_cast<RenderWidgetHostViewBase*>(
danakj0b246942018-09-17 21:30:273001 view_->CreateViewForChildWidget(widget_host));
[email protected]83918ec2013-01-10 15:37:193002 if (!widget_view)
3003 return;
danakj0b246942018-09-17 21:30:273004 // Fullscreen child widgets are frames, other child widgets are popups, and
3005 // popups should not get activated.
3006 if (!is_fullscreen)
3007 widget_view->SetWidgetType(WidgetType::kPopup);
[email protected]bafe6cd2012-05-23 23:09:503008 // Save the created widget associated with the route so we can show it later.
Lukasz Anforowicz09060bdf72018-08-23 15:53:173009 pending_widget_views_[GlobalRoutingID(render_process_id, route_id)] =
alexmosc2a8cec2016-05-23 22:19:533010 widget_view;
[email protected]bafe6cd2012-05-23 23:09:503011}
3012
alexmosc2a8cec2016-05-23 22:19:533013void WebContentsImpl::ShowCreatedWindow(int process_id,
nick5ae4d2d2017-01-06 01:18:353014 int main_frame_widget_route_id,
[email protected]bafe6cd2012-05-23 23:09:503015 WindowOpenDisposition disposition,
bokan107a47f2015-02-03 23:23:393016 const gfx::Rect& initial_rect,
[email protected]bafe6cd2012-05-23 23:09:503017 bool user_gesture) {
danakje6c01622018-10-05 19:39:263018 // This method is the renderer requesting an existing top level window to
3019 // show a new top level window that the renderer created. Each top level
3020 // window is associated with a WebContents. In this case it was created
3021 // earlier but showing it was deferred until the renderer requested for it
3022 // to be shown. We find that previously created WebContents here.
3023 // TODO(danakj): Why do we defer this show step until the renderer asks for it
3024 // when it will always do so. What needs to happen in the renderer before we
3025 // reach here?
3026 std::unique_ptr<WebContentsImpl> owned_created =
nick5ae4d2d2017-01-06 01:18:353027 GetCreatedWindow(process_id, main_frame_widget_route_id);
danakje6c01622018-10-05 19:39:263028 WebContentsImpl* created = owned_created.get();
3029 // The browser may have rejected the request to make a new window, or the
3030 // renderer could be sending an invalid route id. Ignore the request then.
3031 if (!created)
3032 return;
mariakhomenko44bdc4732015-04-29 01:55:383033
danakje6c01622018-10-05 19:39:263034 // This uses the delegate for the WebContents where the window was created
3035 // from, to control how to show the newly created window.
3036 WebContentsDelegate* delegate = GetDelegate();
nick5ae4d2d2017-01-06 01:18:353037
danakje6c01622018-10-05 19:39:263038 // The delegate can be null in tests, so we must check for it :(.
3039 if (delegate) {
3040 // Mark the web contents as pending resume, then immediately do
3041 // the resume if the delegate wants it.
3042 created->is_resume_pending_ = true;
3043 if (delegate->ShouldResumeRequestsForCreatedWindow())
3044 created->ResumeLoadingCreatedWebContents();
3045
3046 base::WeakPtr<WebContentsImpl> weak_created =
3047 created->weak_factory_.GetWeakPtr();
3048 delegate->AddNewContents(this, std::move(owned_created), disposition,
3049 initial_rect, user_gesture, nullptr);
3050 // The delegate may delete |created| during AddNewContents().
3051 if (!weak_created)
3052 return;
[email protected]eda238a12012-09-07 23:44:003053 }
danakje6c01622018-10-05 19:39:263054
3055 RenderWidgetHostImpl* rwh = created->GetMainFrame()->GetRenderWidgetHost();
3056 DCHECK_EQ(main_frame_widget_route_id, rwh->GetRoutingID());
3057 rwh->Send(new WidgetMsg_SetBounds_ACK(rwh->GetRoutingID()));
[email protected]bafe6cd2012-05-23 23:09:503058}
3059
alexmosc2a8cec2016-05-23 22:19:533060void WebContentsImpl::ShowCreatedWidget(int process_id,
Albert J. Wongf6e13ed2018-09-18 15:25:473061 int widget_route_id,
bokan107a47f2015-02-03 23:23:393062 const gfx::Rect& initial_rect) {
Albert J. Wongf6e13ed2018-09-18 15:25:473063 ShowCreatedWidget(process_id, widget_route_id, false, initial_rect);
[email protected]bafe6cd2012-05-23 23:09:503064}
3065
alexmosc2a8cec2016-05-23 22:19:533066void WebContentsImpl::ShowCreatedFullscreenWidget(int process_id,
Albert J. Wongf6e13ed2018-09-18 15:25:473067 int widget_route_id) {
3068 ShowCreatedWidget(process_id, widget_route_id, true, gfx::Rect());
[email protected]bafe6cd2012-05-23 23:09:503069}
3070
alexmosc2a8cec2016-05-23 22:19:533071void WebContentsImpl::ShowCreatedWidget(int process_id,
3072 int route_id,
[email protected]bafe6cd2012-05-23 23:09:503073 bool is_fullscreen,
bokan107a47f2015-02-03 23:23:393074 const gfx::Rect& initial_rect) {
[email protected]cfd80b02014-05-01 17:46:483075 RenderWidgetHostViewBase* widget_host_view =
alexmosc2a8cec2016-05-23 22:19:533076 static_cast<RenderWidgetHostViewBase*>(
3077 GetCreatedWidget(process_id, route_id));
[email protected]bafe6cd2012-05-23 23:09:503078 if (!widget_host_view)
3079 return;
[email protected]cfd80b02014-05-01 17:46:483080
W. James MacLeanec4125c2019-09-27 18:56:003081 // GetOutermostWebContents() returns |this| if there are no outer WebContents.
W. James MacLean3c5f50462019-10-28 16:24:213082 auto* outer_web_contents = GetOuterWebContents();
3083 auto* outermost_web_contents = GetOutermostWebContents();
W. James MacLeanec4125c2019-09-27 18:56:003084 RenderWidgetHostView* view =
W. James MacLean3c5f50462019-10-28 16:24:213085 outermost_web_contents->GetRenderWidgetHostView();
3086 // It's not entirely obvious why we need the transform only in the case where
3087 // the outer webcontents is not the same as the outermost webcontents. It may
3088 // be due to the fact that oopifs that are children of the mainframe get
3089 // correct values for their screenrects, but deeper cross-process frames do
3090 // not. Hopefully this can be resolved with https://crbug.com/928825.
3091 // Handling these cases separately is needed for http://crbug.com/1015298.
3092 bool needs_transform = this != outermost_web_contents &&
3093 outermost_web_contents != outer_web_contents;
W. James MacLeanec4125c2019-09-27 18:56:003094
3095 gfx::Rect transformed_rect(initial_rect);
3096 RenderWidgetHostView* this_view = GetRenderWidgetHostView();
W. James MacLean3c5f50462019-10-28 16:24:213097 if (needs_transform) {
W. James MacLeanec4125c2019-09-27 18:56:003098 // We need to transform the coordinates of initial_rect.
3099 gfx::Point origin =
3100 this_view->TransformPointToRootCoordSpace(initial_rect.origin());
3101 gfx::Point bottom_right =
3102 this_view->TransformPointToRootCoordSpace(initial_rect.bottom_right());
3103 transformed_rect =
3104 gfx::Rect(origin.x(), origin.y(), bottom_right.x() - origin.x(),
3105 bottom_right.y() - origin.y());
[email protected]cfd80b02014-05-01 17:46:483106 }
3107
danakj0b246942018-09-17 21:30:273108 // Fullscreen child widgets are frames, other child widgets are popups.
[email protected]4aebbca2013-09-17 22:26:493109 if (is_fullscreen) {
[email protected]d7f80ba2013-10-12 07:42:313110 DCHECK_EQ(MSG_ROUTING_NONE, fullscreen_widget_routing_id_);
[email protected]0b431992014-06-24 00:08:033111 view_->StoreFocus();
alexmosc9e76ec2016-05-16 22:59:373112 fullscreen_widget_process_id_ =
3113 widget_host_view->GetRenderWidgetHost()->GetProcess()->GetID();
[email protected]d7f80ba2013-10-12 07:42:313114 fullscreen_widget_routing_id_ = route_id;
[email protected]4aebbca2013-09-17 22:26:493115 if (delegate_ && delegate_->EmbedsFullscreenWidget()) {
3116 widget_host_view->InitAsChild(GetRenderWidgetHostView()->GetNativeView());
Dave Tapuska3ed44192018-05-01 18:53:303117 delegate_->EnterFullscreenModeForTab(this, GURL(),
Dave Tapuskaa4189512019-10-15 20:27:343118 blink::mojom::FullscreenOptions());
[email protected]4aebbca2013-09-17 22:26:493119 } else {
[email protected]cfd80b02014-05-01 17:46:483120 widget_host_view->InitAsFullscreen(view);
[email protected]4aebbca2013-09-17 22:26:493121 }
ericwilligersde386342016-10-19 02:35:093122 for (auto& observer : observers_)
3123 observer.DidShowFullscreenWidget();
[email protected]4aebbca2013-09-17 22:26:493124 if (!widget_host_view->HasFocus())
3125 widget_host_view->Focus();
3126 } else {
W. James MacLeanec4125c2019-09-27 18:56:003127 widget_host_view->InitAsPopup(view, transformed_rect);
[email protected]4aebbca2013-09-17 22:26:493128 }
[email protected]89054502012-06-03 10:29:243129
yiyixcb1fbc4f2018-03-16 19:54:083130 RenderWidgetHostImpl* render_widget_host_impl = widget_host_view->host();
[email protected]89054502012-06-03 10:29:243131 render_widget_host_impl->Init();
[email protected]d7f80ba2013-10-12 07:42:313132 // Only allow privileged mouse lock for fullscreen render widget, which is
3133 // used to implement Pepper Flash fullscreen.
3134 render_widget_host_impl->set_allow_privileged_mouse_lock(is_fullscreen);
[email protected]bafe6cd2012-05-23 23:09:503135}
3136
danakje6c01622018-10-05 19:39:263137std::unique_ptr<WebContentsImpl> WebContentsImpl::GetCreatedWindow(
nick5ae4d2d2017-01-06 01:18:353138 int process_id,
3139 int main_frame_widget_route_id) {
Lukasz Anforowicz09060bdf72018-08-23 15:53:173140 auto key = GlobalRoutingID(process_id, main_frame_widget_route_id);
nick5ae4d2d2017-01-06 01:18:353141 auto iter = pending_contents_.find(key);
[email protected]bafe6cd2012-05-23 23:09:503142
3143 // Certain systems can block the creation of new windows. If we didn't succeed
3144 // in creating one, just return NULL.
alexmosc2a8cec2016-05-23 22:19:533145 if (iter == pending_contents_.end())
3146 return nullptr;
[email protected]bafe6cd2012-05-23 23:09:503147
danakje6c01622018-10-05 19:39:263148 std::unique_ptr<WebContentsImpl> new_contents = std::move(iter->second);
nick5ae4d2d2017-01-06 01:18:353149 pending_contents_.erase(key);
danakje6c01622018-10-05 19:39:263150 RemoveDestructionObserver(new_contents.get());
[email protected]bafe6cd2012-05-23 23:09:503151
[email protected]d70bea92013-04-05 04:23:343152 // Don't initialize the guest WebContents immediately.
danakje6c01622018-10-05 19:39:263153 if (BrowserPluginGuest::IsGuest(new_contents.get()))
[email protected]d70bea92013-04-05 04:23:343154 return new_contents;
3155
Lukasz Anforowicz5510ed652018-06-06 16:16:193156 if (!new_contents->GetMainFrame()->GetProcess()->IsInitializedAndNotDead() ||
nick5ae4d2d2017-01-06 01:18:353157 !new_contents->GetMainFrame()->GetView()) {
alexmosc2a8cec2016-05-23 22:19:533158 return nullptr;
nick5ae4d2d2017-01-06 01:18:353159 }
[email protected]bafe6cd2012-05-23 23:09:503160
[email protected]bafe6cd2012-05-23 23:09:503161 return new_contents;
3162}
3163
alexmosc2a8cec2016-05-23 22:19:533164RenderWidgetHostView* WebContentsImpl::GetCreatedWidget(int process_id,
3165 int route_id) {
Lukasz Anforowicz09060bdf72018-08-23 15:53:173166 auto iter = pending_widget_views_.find(GlobalRoutingID(process_id, route_id));
[email protected]bafe6cd2012-05-23 23:09:503167 if (iter == pending_widget_views_.end()) {
3168 DCHECK(false);
alexmosc2a8cec2016-05-23 22:19:533169 return nullptr;
[email protected]bafe6cd2012-05-23 23:09:503170 }
3171
3172 RenderWidgetHostView* widget_host_view = iter->second;
Lukasz Anforowicz09060bdf72018-08-23 15:53:173173 pending_widget_views_.erase(GlobalRoutingID(process_id, route_id));
[email protected]bafe6cd2012-05-23 23:09:503174
3175 RenderWidgetHost* widget_host = widget_host_view->GetRenderWidgetHost();
Lukasz Anforowicz5510ed652018-06-06 16:16:193176 if (!widget_host->GetProcess()->IsInitializedAndNotDead()) {
[email protected]bafe6cd2012-05-23 23:09:503177 // The view has gone away or the renderer crashed. Nothing to do.
alexmosc2a8cec2016-05-23 22:19:533178 return nullptr;
[email protected]bafe6cd2012-05-23 23:09:503179 }
3180
3181 return widget_host_view;
3182}
3183
[email protected]f13b4202012-06-12 23:53:233184void WebContentsImpl::RequestMediaAccessPermission(
[email protected]33662e52013-01-07 21:31:093185 const MediaStreamRequest& request,
Mark Pilgrim57499082018-06-12 12:38:303186 MediaResponseCallback callback) {
[email protected]d19b84b2014-03-14 11:52:373187 if (delegate_) {
Mark Pilgrim57499082018-06-12 12:38:303188 delegate_->RequestMediaAccessPermission(this, request, std::move(callback));
[email protected]d19b84b2014-03-14 11:52:373189 } else {
Antonio Gomes0d42960a2019-06-05 12:35:513190 std::move(callback).Run(
3191 blink::MediaStreamDevices(),
3192 blink::mojom::MediaStreamRequestResult::FAILED_DUE_TO_SHUTDOWN,
3193 std::unique_ptr<MediaStreamUI>());
[email protected]d19b84b2014-03-14 11:52:373194 }
[email protected]f13b4202012-06-12 23:53:233195}
3196
Chandan Padhia4b8bcb72017-09-12 16:41:043197bool WebContentsImpl::CheckMediaAccessPermission(
Raymes Khouryad7c24a12018-03-05 23:22:583198 RenderFrameHost* render_frame_host,
Chandan Padhia4b8bcb72017-09-12 16:41:043199 const url::Origin& security_origin,
Antonio Gomesc8b734b2019-06-05 18:22:163200 blink::mojom::MediaStreamType type) {
3201 DCHECK(type == blink::mojom::MediaStreamType::DEVICE_AUDIO_CAPTURE ||
3202 type == blink::mojom::MediaStreamType::DEVICE_VIDEO_CAPTURE);
Chandan Padhia4b8bcb72017-09-12 16:41:043203 return delegate_ && delegate_->CheckMediaAccessPermission(
Raymes Khouryad7c24a12018-03-05 23:22:583204 render_frame_host, security_origin.GetURL(), type);
grunell657d4d82014-09-18 00:09:433205}
3206
Guido Urdaneta73fa6632019-01-14 18:46:263207std::string WebContentsImpl::GetDefaultMediaDeviceID(
Antonio Gomesc8b734b2019-06-05 18:22:163208 blink::mojom::MediaStreamType type) {
guidou8a440b842017-01-30 13:58:433209 if (!delegate_)
3210 return std::string();
3211 return delegate_->GetDefaultMediaDeviceID(this, type);
3212}
3213
[email protected]cc9200432013-07-23 23:02:403214SessionStorageNamespace* WebContentsImpl::GetSessionStorageNamespace(
3215 SiteInstance* instance) {
3216 return controller_.GetSessionStorageNamespace(instance);
3217}
3218
[email protected]6de7fc482014-06-06 10:46:443219SessionStorageNamespaceMap WebContentsImpl::GetSessionStorageNamespaceMap() {
3220 return controller_.GetSessionStorageNamespaceMap();
3221}
3222
[email protected]9b159a52013-10-03 17:24:553223FrameTree* WebContentsImpl::GetFrameTree() {
3224 return &frame_tree_;
3225}
3226
aelias5252baa2016-04-10 01:18:023227bool WebContentsImpl::IsOverridingUserAgent() {
3228 return GetController().GetVisibleEntry() &&
3229 GetController().GetVisibleEntry()->GetIsOverridingUserAgent();
3230}
3231
avi85ee8362016-10-08 02:09:083232bool WebContentsImpl::IsJavaScriptDialogShowing() const {
3233 return is_showing_javascript_dialog_;
3234}
3235
avic031d392017-03-03 03:09:423236bool WebContentsImpl::ShouldIgnoreUnresponsiveRenderer() {
3237 // Ignore unresponsive renderers if the debugger is attached to them since the
3238 // unresponsiveness might be a result of the renderer sitting on a breakpoint.
3239 //
David Bienvenu525f8a02019-02-15 02:02:423240#ifdef OS_WIN
3241 // Check if a windows debugger is attached to the renderer process.
3242 base::ProcessHandle process_handle =
3243 GetMainFrame()->GetProcess()->GetProcess().Handle();
3244 BOOL debugger_present = FALSE;
3245 if (CheckRemoteDebuggerPresent(process_handle, &debugger_present) &&
3246 debugger_present)
3247 return true;
3248#endif // OS_WIN
3249
avic031d392017-03-03 03:09:423250 // TODO(pfeldman): Fix this to only return true if the renderer is *actually*
3251 // sitting on a breakpoint. https://crbug.com/684202
3252 return DevToolsAgentHost::IsDebuggerAttached(this);
3253}
3254
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:413255ui::AXMode WebContentsImpl::GetAccessibilityMode() {
[email protected]95640212014-07-26 18:14:303256 return accessibility_mode_;
3257}
3258
Dominic Mazzonia7b0edb22017-08-09 16:32:513259void WebContentsImpl::AccessibilityEventReceived(
Dominic Mazzoniccbaa9b2018-06-06 07:44:233260 const AXEventNotificationDetails& details) {
ericwilligersde386342016-10-19 02:35:093261 for (auto& observer : observers_)
Dominic Mazzonia7b0edb22017-08-09 16:32:513262 observer.AccessibilityEventReceived(details);
[email protected]31a71eaf2014-03-13 01:47:363263}
3264
dmazzoni2400c462016-08-23 15:07:133265void WebContentsImpl::AccessibilityLocationChangesReceived(
3266 const std::vector<AXLocationChangeNotificationDetails>& details) {
ericwilligersde386342016-10-19 02:35:093267 for (auto& observer : observers_)
Dominic Mazzonia7b0edb22017-08-09 16:32:513268 observer.AccessibilityLocationChangesReceived(details);
dmazzoni2400c462016-08-23 15:07:133269}
3270
Abigail Kleinabb428332019-09-13 18:26:213271base::string16 WebContentsImpl::DumpAccessibilityTree(
3272 bool internal,
3273 std::vector<AccessibilityTreeFormatter::PropertyFilter> property_filters) {
James Wallace-Leeeafc94cb92018-07-23 21:35:093274 auto* ax_mgr = GetOrCreateRootBrowserAccessibilityManager();
3275 DCHECK(ax_mgr);
Lei Zhangc98caa3822019-11-07 19:17:273276 return AccessibilityTreeFormatterBase::DumpAccessibilityTreeFromManager(
Abigail Kleinabb428332019-09-13 18:26:213277 ax_mgr, internal, property_filters);
James Wallace-Leeeafc94cb92018-07-23 21:35:093278}
3279
Abigail Kleinaa898742019-11-01 02:31:253280void WebContentsImpl::RecordAccessibilityEvents(
3281 AccessibilityEventCallback callback,
3282 bool start) {
3283 if (start) {
3284 SetAccessibilityMode(ui::AXMode::kWebContents);
3285 auto* ax_mgr = GetOrCreateRootBrowserAccessibilityManager();
3286 DCHECK(ax_mgr);
3287 base::ProcessId pid = base::Process::Current().Pid();
3288 event_recorder_ = content::AccessibilityEventRecorder::Create(
3289 ax_mgr, pid, base::StringPiece{});
3290 event_recorder_->ListenToEvents(callback);
3291 } else {
3292 DCHECK(event_recorder_);
3293 event_recorder_->FlushAsyncEvents();
3294 event_recorder_ = nullptr;
3295 }
3296}
3297
dmazzonia656928e2014-09-15 20:28:543298RenderFrameHost* WebContentsImpl::GetGuestByInstanceID(
fsamuel833ee7c2015-02-13 23:40:403299 RenderFrameHost* render_frame_host,
dmazzonia656928e2014-09-15 20:28:543300 int browser_plugin_instance_id) {
3301 BrowserPluginGuestManager* guest_manager =
3302 GetBrowserContext()->GetGuestManager();
fsamuel9cbc00a2015-04-17 20:30:203303 if (!guest_manager)
3304 return nullptr;
3305
dmazzonia656928e2014-09-15 20:28:543306 WebContents* guest = guest_manager->GetGuestByInstanceID(
fsamuel2e9413d2015-02-25 01:25:443307 render_frame_host->GetProcess()->GetID(), browser_plugin_instance_id);
dmazzonia656928e2014-09-15 20:28:543308 if (!guest)
fsamuel9cbc00a2015-04-17 20:30:203309 return nullptr;
3310
dmazzonia656928e2014-09-15 20:28:543311 return guest->GetMainFrame();
3312}
3313
Ke He7319dbe2017-11-09 05:54:443314device::mojom::GeolocationContext* WebContentsImpl::GetGeolocationContext() {
Ken Rockotce010f02019-12-12 23:32:323315 if (!geolocation_context_) {
3316 GetDeviceService().BindGeolocationContext(
3317 geolocation_context_.BindNewPipeAndPassReceiver());
3318 }
Conley Owens6894c4f2017-07-10 19:29:133319 return geolocation_context_.get();
blundellc57b93f2014-10-29 13:19:573320}
3321
ke.he98b761e2017-05-09 05:59:173322device::mojom::WakeLockContext* WebContentsImpl::GetWakeLockContext() {
blundelld8cd72b2017-03-28 07:18:383323 if (!wake_lock_context_host_)
3324 wake_lock_context_host_.reset(new WakeLockContextHost(this));
blundelle75a8f92017-03-27 08:11:173325 return wake_lock_context_host_->GetWakeLockContext();
alogvinovf50445a2015-10-30 13:00:123326}
3327
blundellf5316fc2017-05-15 11:49:033328#if defined(OS_ANDROID)
Miyoung Shin59f39ff2019-09-17 07:23:203329void WebContentsImpl::GetNFC(
3330 mojo::PendingReceiver<device::mojom::NFC> receiver) {
Leon Hanccb985f2019-08-26 02:43:243331 NFCHost nfc_host(this);
Miyoung Shin59f39ff2019-09-17 07:23:203332 nfc_host.GetNFC(std::move(receiver));
blundellf5316fc2017-05-15 11:49:033333}
3334#endif
3335
Evan Stade5e750ad02017-08-07 21:59:273336void WebContentsImpl::SetNotWaitingForResponse() {
3337 if (waiting_for_response_ == false)
3338 return;
3339
3340 waiting_for_response_ = false;
3341 if (delegate_)
3342 delegate_->LoadingStateChanged(this, is_load_to_different_document_);
Chris Hamiltonf82f2a702018-02-15 19:37:293343 for (auto& observer : observers_)
3344 observer.DidReceiveResponse();
Evan Stade5e750ad02017-08-07 21:59:273345}
3346
lfgbb9c28a2016-03-01 03:19:493347void WebContentsImpl::SendScreenRects() {
kenrbc52e1b4e2016-03-20 01:03:373348 for (FrameTreeNode* node : frame_tree_.Nodes()) {
kenrb0e8dc202016-05-30 19:51:403349 if (node->current_frame_host()->is_local_root())
kenrbc52e1b4e2016-03-20 01:03:373350 node->current_frame_host()->GetRenderWidgetHost()->SendScreenRects();
3351 }
lfgbb9c28a2016-03-01 03:19:493352
danakj4df8a9d02019-02-05 20:36:593353 // Interstitials have their own |frame_tree_|.
3354 if (interstitial_page_) {
3355 FrameTree* interstitial_frame_tree = interstitial_page_->GetFrameTree();
3356 for (FrameTreeNode* node : interstitial_frame_tree->Nodes()) {
3357 if (node->current_frame_host()->is_local_root())
3358 node->current_frame_host()->GetRenderWidgetHost()->SendScreenRects();
3359 }
lfgdb5c4ed2016-03-04 23:09:073360 }
[email protected]32deec62013-05-15 23:55:043361}
3362
ekaramadadd882292016-06-08 15:22:563363TextInputManager* WebContentsImpl::GetTextInputManager() {
3364 if (GetOuterWebContents())
3365 return GetOuterWebContents()->GetTextInputManager();
3366
Ehsan Karamadc179dc02018-12-15 03:09:293367 if (!text_input_manager_ && !browser_plugin_guest_) {
Darren Shenca53d5f2018-05-15 04:56:013368 text_input_manager_.reset(new TextInputManager(
3369 GetBrowserContext() &&
3370 !GetBrowserContext()->IsOffTheRecord()) /* should_do_learning */);
3371 }
ekaramadadd882292016-06-08 15:22:563372
3373 return text_input_manager_.get();
3374}
3375
paulmeyer7f6f1d4f2016-11-15 00:00:273376bool WebContentsImpl::OnUpdateDragCursor() {
Lei Zhang6410ec1a2019-01-31 21:38:583377 return browser_plugin_embedder_ &&
3378 browser_plugin_embedder_->OnUpdateDragCursor();
paulmeyer7f6f1d4f2016-11-15 00:00:273379}
3380
kenrb11f213a2017-03-24 18:12:063381bool WebContentsImpl::IsWidgetForMainFrame(
3382 RenderWidgetHostImpl* render_widget_host) {
3383 return render_widget_host == GetMainFrame()->GetRenderWidgetHost();
3384}
3385
[email protected]95640212014-07-26 18:14:303386BrowserAccessibilityManager*
3387 WebContentsImpl::GetRootBrowserAccessibilityManager() {
Dominic Mazzoniefb5f9e32017-12-21 01:08:223388 RenderFrameHostImpl* rfh = static_cast<RenderFrameHostImpl*>(
3389 ShowingInterstitialPage() ? GetInterstitialPage()->GetMainFrame()
3390 : GetMainFrame());
creisc014b402015-04-23 16:41:453391 return rfh ? rfh->browser_accessibility_manager() : nullptr;
[email protected]95640212014-07-26 18:14:303392}
3393
3394BrowserAccessibilityManager*
3395 WebContentsImpl::GetOrCreateRootBrowserAccessibilityManager() {
Dominic Mazzoniefb5f9e32017-12-21 01:08:223396 RenderFrameHostImpl* rfh = static_cast<RenderFrameHostImpl*>(
3397 ShowingInterstitialPage() ? GetInterstitialPage()->GetMainFrame()
3398 : GetMainFrame());
creisc014b402015-04-23 16:41:453399 return rfh ? rfh->GetOrCreateBrowserAccessibilityManager() : nullptr;
[email protected]95640212014-07-26 18:14:303400}
3401
Dave Tapuskab336b922017-07-06 19:24:053402void WebContentsImpl::ExecuteEditCommand(
3403 const std::string& command,
3404 const base::Optional<base::string16>& value) {
Dave Tapuskae782bea2019-07-09 16:21:513405 auto* input_handler = GetFocusedFrameInputHandler();
3406 if (!input_handler)
Dave Tapuskab336b922017-07-06 19:24:053407 return;
3408
Dave Tapuskae782bea2019-07-09 16:21:513409 input_handler->ExecuteEditCommand(command, value);
Dave Tapuskab336b922017-07-06 19:24:053410}
3411
mohsen7ab1ec16ec2015-07-02 18:26:233412void WebContentsImpl::MoveRangeSelectionExtent(const gfx::Point& extent) {
Dave Tapuskae782bea2019-07-09 16:21:513413 auto* input_handler = GetFocusedFrameInputHandler();
3414 if (!input_handler)
mohsen7ab1ec16ec2015-07-02 18:26:233415 return;
3416
Dave Tapuskae782bea2019-07-09 16:21:513417 input_handler->MoveRangeSelectionExtent(extent);
mohsen7ab1ec16ec2015-07-02 18:26:233418}
3419
3420void WebContentsImpl::SelectRange(const gfx::Point& base,
3421 const gfx::Point& extent) {
Dave Tapuskae782bea2019-07-09 16:21:513422 auto* input_handler = GetFocusedFrameInputHandler();
3423 if (!input_handler)
mohsen7ab1ec16ec2015-07-02 18:26:233424 return;
3425
Dave Tapuskae782bea2019-07-09 16:21:513426 input_handler->SelectRange(base, extent);
mohsen7ab1ec16ec2015-07-02 18:26:233427}
3428
Dave Tapuskab336b922017-07-06 19:24:053429void WebContentsImpl::MoveCaret(const gfx::Point& extent) {
Dave Tapuskae782bea2019-07-09 16:21:513430 auto* input_handler = GetFocusedFrameInputHandler();
3431 if (!input_handler)
Dave Tapuskab336b922017-07-06 19:24:053432 return;
3433
Dave Tapuskae782bea2019-07-09 16:21:513434 input_handler->MoveCaret(extent);
Dave Tapuskab336b922017-07-06 19:24:053435}
3436
Shimi Zhang37deeb22017-09-28 00:59:013437void WebContentsImpl::AdjustSelectionByCharacterOffset(
3438 int start_adjust,
3439 int end_adjust,
3440 bool show_selection_menu) {
Dave Tapuskae782bea2019-07-09 16:21:513441 auto* input_handler = GetFocusedFrameInputHandler();
3442 if (!input_handler)
aurimasab0319022015-07-10 21:57:383443 return;
3444
Shimi Zhang37deeb22017-09-28 00:59:013445 using blink::mojom::SelectionMenuBehavior;
Dave Tapuskae782bea2019-07-09 16:21:513446 input_handler->AdjustSelectionByCharacterOffset(
Shimi Zhang37deeb22017-09-28 00:59:013447 start_adjust, end_adjust,
3448 show_selection_menu ? SelectionMenuBehavior::kShow
3449 : SelectionMenuBehavior::kHide);
aurimasab0319022015-07-10 21:57:383450}
3451
[email protected]b172aee2012-04-10 17:05:263452void WebContentsImpl::UpdatePreferredSize(const gfx::Size& pref_size) {
[email protected]222f5822014-02-05 23:40:493453 const gfx::Size old_size = GetPreferredSize();
[email protected]bcd2815602012-01-14 18:17:233454 preferred_size_ = pref_size;
[email protected]222f5822014-02-05 23:40:493455 OnPreferredSizeChanged(old_size);
[email protected]0548c5352011-09-07 00:33:333456}
3457
avic3aa8422015-11-09 20:57:223458void WebContentsImpl::ResizeDueToAutoResize(
3459 RenderWidgetHostImpl* render_widget_host,
Fady Samuel97947112018-05-05 16:24:543460 const gfx::Size& new_size) {
avic3aa8422015-11-09 20:57:223461 if (render_widget_host != GetRenderViewHost()->GetWidget())
3462 return;
3463
kenrbe4f91082017-06-02 20:32:083464 auto_resize_size_ = new_size;
3465
3466 // Out-of-process iframe visible viewport sizes usually come from the
3467 // top-level RenderWidgetHostView, but when auto-resize is enabled on the
3468 // top frame then that size is used instead.
3469 for (FrameTreeNode* node : frame_tree_.Nodes()) {
3470 if (node->current_frame_host()->is_local_root()) {
3471 RenderWidgetHostImpl* host =
3472 node->current_frame_host()->GetRenderWidgetHost();
3473 if (host != render_widget_host)
Fady Samuel0b911822018-04-25 13:22:163474 host->SynchronizeVisualProperties();
kenrbe4f91082017-06-02 20:32:083475 }
3476 }
3477
[email protected]61e2b3cc2012-03-02 16:13:343478 if (delegate_)
3479 delegate_->ResizeDueToAutoResize(this, new_size);
3480}
3481
kenrbe4f91082017-06-02 20:32:083482gfx::Size WebContentsImpl::GetAutoResizeSize() {
3483 return auto_resize_size_;
3484}
3485
3486void WebContentsImpl::ResetAutoResizeSize() {
3487 auto_resize_size_ = gfx::Size();
3488}
3489
[email protected]b172aee2012-04-10 17:05:263490WebContents* WebContentsImpl::OpenURL(const OpenURLParams& params) {
Aaron Colwell9dab1652019-12-09 18:29:493491#if DCHECK_IS_ON()
3492 DCHECK(params.Valid());
3493#endif
3494
Bo Liu300c6052018-06-12 04:46:073495 if (!delegate_) {
3496 // Embedder can delay setting a delegate on new WebContents with
3497 // WebContentsDelegate::ShouldResumeRequestsForCreatedWindow. In the mean
3498 // time, navigations, including the initial one, that goes through OpenURL
3499 // should be delayed until embedder is ready to resume loading.
3500 delayed_open_url_params_ = std::make_unique<OpenURLParams>(params);
Nasko Oskov83a8cf92018-10-19 14:58:563501
3502 // If there was a navigation deferred when creating the window through
3503 // CreateNewWindow, drop it in favor of this navigation.
3504 delayed_load_url_params_.reset();
3505
Ivan Kotenkov2c0d2bb32017-11-01 15:41:283506 return nullptr;
Bo Liu300c6052018-06-12 04:46:073507 }
[email protected]00c37fc2011-08-02 00:22:503508
[email protected]e5d549d2011-12-28 01:29:203509 WebContents* new_contents = delegate_->OpenURLFromTab(this, params);
pnoland488944652017-02-22 18:58:543510
3511 RenderFrameHost* source_render_frame_host = RenderFrameHost::FromID(
3512 params.source_render_process_id, params.source_render_frame_id);
3513
3514 if (source_render_frame_host && params.source_site_instance) {
3515 CHECK_EQ(source_render_frame_host->GetSiteInstance(),
3516 params.source_site_instance.get());
3517 }
3518
3519 if (new_contents && source_render_frame_host && new_contents != this) {
3520 for (auto& observer : observers_) {
Lukasz Anforowicz48c9f872018-08-01 00:58:413521 observer.DidOpenRequestedURL(
3522 new_contents, source_render_frame_host, params.url, params.referrer,
3523 params.disposition, params.transition,
3524 params.started_from_context_menu, params.is_renderer_initiated);
pnoland488944652017-02-22 18:58:543525 }
3526 }
3527
[email protected]e5d549d2011-12-28 01:29:203528 return new_contents;
[email protected]d5f942ba2008-09-26 19:30:343529}
3530
[email protected]a86c0e962013-12-17 17:10:393531void WebContentsImpl::RenderFrameForInterstitialPageCreated(
3532 RenderFrameHost* render_frame_host) {
ericwilligersde386342016-10-19 02:35:093533 for (auto& observer : observers_)
3534 observer.RenderFrameForInterstitialPageCreated(render_frame_host);
[email protected]ba45bfd2012-05-22 21:51:443535}
3536
[email protected]20ca0382013-02-28 19:50:073537void WebContentsImpl::AttachInterstitialPage(
3538 InterstitialPageImpl* interstitial_page) {
creisadca9862017-06-16 19:12:113539 DCHECK(!interstitial_page_ && interstitial_page);
3540 interstitial_page_ = interstitial_page;
[email protected]90fb08ed2013-09-24 17:43:293541
3542 // Cancel any visible dialogs so that they don't interfere with the
3543 // interstitial.
creis89a0f782015-05-27 16:13:173544 CancelActiveAndPendingDialogs();
[email protected]90fb08ed2013-09-24 17:43:293545
ericwilligersde386342016-10-19 02:35:093546 for (auto& observer : observers_)
3547 observer.DidAttachInterstitialPage();
clamy44e84ce2016-02-22 15:38:253548
3549 // Stop the throbber if needed while the interstitial page is shown.
3550 if (frame_tree_.IsLoading())
3551 LoadingStateChanged(true, true, nullptr);
lfg1453e412017-04-11 00:48:503552
3553 // Connect to outer WebContents if necessary.
3554 if (node_.OuterContentsFrameTreeNode()) {
3555 if (GetRenderManager()->GetProxyToOuterDelegate()) {
3556 DCHECK(
3557 static_cast<RenderWidgetHostViewBase*>(interstitial_page->GetView())
3558 ->IsRenderWidgetHostViewChildFrame());
3559 RenderWidgetHostViewChildFrame* view =
3560 static_cast<RenderWidgetHostViewChildFrame*>(
3561 interstitial_page->GetView());
3562 GetRenderManager()->SetRWHViewForInnerContents(view);
3563 }
3564 }
Bo Liu7c6779e92017-08-16 02:02:283565
Bo Liu168c8642017-08-28 18:26:023566#if defined(OS_ANDROID)
Bo Liu7c6779e92017-08-16 02:02:283567 // Update importance of the interstitial.
3568 static_cast<RenderFrameHostImpl*>(interstitial_page_->GetMainFrame())
3569 ->GetRenderWidgetHost()
3570 ->SetImportance(GetMainFrame()->GetRenderWidgetHost()->importance());
Bo Liu168c8642017-08-28 18:26:023571#endif
Dominic Mazzoniefb5f9e32017-12-21 01:08:223572
3573 if (accessibility_mode_.has_mode(ui::AXMode::kNativeAPIs)) {
3574 // Make sure that the main page's accessibility tree is hidden and the
3575 // interstitial gets focus.
3576 RenderFrameHostImpl* rfh =
3577 static_cast<RenderFrameHostImpl*>(GetMainFrame());
3578 if (rfh) {
3579 BrowserAccessibilityManager* accessibility_manager =
3580 rfh->browser_accessibility_manager();
3581 if (accessibility_manager)
3582 accessibility_manager->set_hidden_by_interstitial_page(true);
3583 }
3584 rfh = static_cast<RenderFrameHostImpl*>(
3585 GetInterstitialPage()->GetMainFrame());
3586 if (rfh) {
3587 BrowserAccessibilityManager* accessibility_manager =
3588 rfh->GetOrCreateBrowserAccessibilityManager();
3589 if (accessibility_manager)
3590 accessibility_manager->OnWindowFocused();
3591 }
3592 }
clamy44e84ce2016-02-22 15:38:253593}
3594
3595void WebContentsImpl::DidProceedOnInterstitial() {
3596 // The interstitial page should no longer be pausing the throbber.
creisadca9862017-06-16 19:12:113597 DCHECK(!(ShowingInterstitialPage() && interstitial_page_->pause_throbber()));
clamy44e84ce2016-02-22 15:38:253598
3599 // Restart the throbber now that the interstitial page no longer pauses it.
3600 if (ShowingInterstitialPage() && frame_tree_.IsLoading())
3601 LoadingStateChanged(true, true, nullptr);
[email protected]20ca0382013-02-28 19:50:073602}
3603
Shivani Sharma985474f2019-05-23 21:36:473604bool WebContentsImpl::HadInnerWebContents() {
3605 return had_inner_webcontents_;
3606}
3607
Lucas Furukawa Gadani7f1241d2017-07-12 01:45:043608void WebContentsImpl::DetachInterstitialPage(bool has_focus) {
Lucas Furukawa Gadani69b609192017-07-10 17:08:343609 bool interstitial_pausing_throbber =
3610 ShowingInterstitialPage() && interstitial_page_->pause_throbber();
3611 if (ShowingInterstitialPage())
3612 interstitial_page_ = nullptr;
Lucas Furukawa Gadani7f1241d2017-07-12 01:45:043613
Dominic Mazzoniefb5f9e32017-12-21 01:08:223614 // Make sure that the main page's accessibility tree is no longer
3615 // suppressed.
3616 RenderFrameHostImpl* rfh = GetMainFrame();
3617 if (rfh) {
3618 BrowserAccessibilityManager* accessibility_manager =
3619 rfh->browser_accessibility_manager();
3620 if (accessibility_manager)
3621 accessibility_manager->set_hidden_by_interstitial_page(false);
3622 }
3623
Lucas Furukawa Gadani7f1241d2017-07-12 01:45:043624 // If the focus was on the interstitial, let's keep it to the page.
3625 // (Note that in unit-tests the RVH may not have a view).
3626 if (has_focus && GetRenderViewHost()->GetWidget()->GetView())
3627 GetRenderViewHost()->GetWidget()->GetView()->Focus();
3628
Lucas Furukawa Gadani69b609192017-07-10 17:08:343629 for (auto& observer : observers_)
3630 observer.DidDetachInterstitialPage();
3631
3632 // Disconnect from outer WebContents if necessary. This must happen after the
3633 // interstitial page is cleared above, since the call to
3634 // SetRWHViewForInnerContents below may loop over all the
3635 // RenderWidgetHostViews in the tree (otherwise, including the now-deleted
3636 // view for the interstitial).
lfg1453e412017-04-11 00:48:503637 if (node_.OuterContentsFrameTreeNode()) {
3638 if (GetRenderManager()->GetProxyToOuterDelegate()) {
3639 DCHECK(static_cast<RenderWidgetHostViewBase*>(
3640 GetRenderManager()->current_frame_host()->GetView())
3641 ->IsRenderWidgetHostViewChildFrame());
3642 RenderWidgetHostViewChildFrame* view =
3643 static_cast<RenderWidgetHostViewChildFrame*>(
3644 GetRenderManager()->current_frame_host()->GetView());
3645 GetRenderManager()->SetRWHViewForInnerContents(view);
3646 }
3647 }
3648
clamy44e84ce2016-02-22 15:38:253649 // Restart the throbber if needed now that the interstitial page is going
3650 // away.
3651 if (interstitial_pausing_throbber && frame_tree_.IsLoading())
3652 LoadingStateChanged(true, true, nullptr);
[email protected]20ca0382013-02-28 19:50:073653}
3654
avi2b177592014-12-10 02:08:023655void WebContentsImpl::SetHistoryOffsetAndLength(int history_offset,
3656 int history_length) {
alexmos136fd6e62016-08-15 20:58:413657 SendPageMessage(new PageMsg_SetHistoryOffsetAndLength(
3658 MSG_ROUTING_NONE, history_offset, history_length));
avi2b177592014-12-10 02:08:023659}
3660
3661void WebContentsImpl::SetHistoryOffsetAndLengthForView(
3662 RenderViewHost* render_view_host,
3663 int history_offset,
3664 int history_length) {
alexmos136fd6e62016-08-15 20:58:413665 render_view_host->Send(new PageMsg_SetHistoryOffsetAndLength(
avi2b177592014-12-10 02:08:023666 render_view_host->GetRoutingID(), history_offset, history_length));
[email protected]796931a92011-08-10 01:32:143667}
3668
arthursonzogni818c2642019-09-27 12:18:103669void WebContentsImpl::ReloadFocusedFrame() {
[email protected]1f3fc1d2014-04-03 14:50:173670 RenderFrameHost* focused_frame = GetFocusedFrame();
3671 if (!focused_frame)
3672 return;
3673
yilkal921048bd2019-10-09 23:51:043674 focused_frame->Reload();
[email protected]959be4c2014-04-08 15:01:333675}
3676
Miyoung Shin298d5ad2019-09-05 01:52:373677std::vector<mojo::Remote<blink::mojom::PauseSubresourceLoadingHandle>>
Ryan Sturmc65dbe42018-05-31 20:37:043678WebContentsImpl::PauseSubresourceLoading() {
Miyoung Shin298d5ad2019-09-05 01:52:373679 std::vector<mojo::Remote<blink::mojom::PauseSubresourceLoadingHandle>>
3680 handles;
Ryan Sturmc65dbe42018-05-31 20:37:043681 for (RenderFrameHost* rfh : GetAllFrames()) {
3682 if (!rfh->IsRenderFrameLive())
3683 continue;
3684 handles.push_back(rfh->PauseSubresourceLoading());
3685 }
3686 return handles;
3687}
3688
[email protected]959be4c2014-04-08 15:01:333689void WebContentsImpl::Undo() {
Dave Tapuskae782bea2019-07-09 16:21:513690 auto* input_handler = GetFocusedFrameInputHandler();
3691 if (!input_handler)
[email protected]959be4c2014-04-08 15:01:333692 return;
3693
Dave Tapuskae782bea2019-07-09 16:21:513694 input_handler->Undo();
[email protected]959be4c2014-04-08 15:01:333695 RecordAction(base::UserMetricsAction("Undo"));
3696}
3697
3698void WebContentsImpl::Redo() {
Dave Tapuskae782bea2019-07-09 16:21:513699 auto* input_handler = GetFocusedFrameInputHandler();
3700 if (!input_handler)
[email protected]959be4c2014-04-08 15:01:333701 return;
Dave Tapuskae782bea2019-07-09 16:21:513702
3703 input_handler->Redo();
[email protected]959be4c2014-04-08 15:01:333704 RecordAction(base::UserMetricsAction("Redo"));
3705}
3706
3707void WebContentsImpl::Cut() {
Dave Tapuskae782bea2019-07-09 16:21:513708 auto* input_handler = GetFocusedFrameInputHandler();
3709 if (!input_handler)
[email protected]959be4c2014-04-08 15:01:333710 return;
3711
Dave Tapuskae782bea2019-07-09 16:21:513712 input_handler->Cut();
[email protected]959be4c2014-04-08 15:01:333713 RecordAction(base::UserMetricsAction("Cut"));
3714}
3715
3716void WebContentsImpl::Copy() {
Dave Tapuskae782bea2019-07-09 16:21:513717 auto* input_handler = GetFocusedFrameInputHandler();
3718 if (!input_handler)
[email protected]959be4c2014-04-08 15:01:333719 return;
3720
Dave Tapuskae782bea2019-07-09 16:21:513721 input_handler->Copy();
[email protected]959be4c2014-04-08 15:01:333722 RecordAction(base::UserMetricsAction("Copy"));
3723}
3724
3725void WebContentsImpl::CopyToFindPboard() {
3726#if defined(OS_MACOSX)
Dave Tapuskae782bea2019-07-09 16:21:513727 auto* input_handler = GetFocusedFrameInputHandler();
3728 if (!input_handler)
[email protected]959be4c2014-04-08 15:01:333729 return;
3730
3731 // Windows/Linux don't have the concept of a find pasteboard.
Dave Tapuskae782bea2019-07-09 16:21:513732 input_handler->CopyToFindPboard();
[email protected]959be4c2014-04-08 15:01:333733 RecordAction(base::UserMetricsAction("CopyToFindPboard"));
3734#endif
3735}
3736
3737void WebContentsImpl::Paste() {
Dave Tapuskae782bea2019-07-09 16:21:513738 auto* input_handler = GetFocusedFrameInputHandler();
3739 if (!input_handler)
[email protected]959be4c2014-04-08 15:01:333740 return;
3741
Dave Tapuskae782bea2019-07-09 16:21:513742 input_handler->Paste();
Archana Simhaf67090c2019-05-06 23:43:233743 for (auto& observer : observers_)
3744 observer.OnPaste();
[email protected]959be4c2014-04-08 15:01:333745 RecordAction(base::UserMetricsAction("Paste"));
3746}
3747
3748void WebContentsImpl::PasteAndMatchStyle() {
Dave Tapuskae782bea2019-07-09 16:21:513749 auto* input_handler = GetFocusedFrameInputHandler();
3750 if (!input_handler)
[email protected]959be4c2014-04-08 15:01:333751 return;
3752
Dave Tapuskae782bea2019-07-09 16:21:513753 input_handler->PasteAndMatchStyle();
Archana Simhaf67090c2019-05-06 23:43:233754 for (auto& observer : observers_)
3755 observer.OnPaste();
[email protected]959be4c2014-04-08 15:01:333756 RecordAction(base::UserMetricsAction("PasteAndMatchStyle"));
3757}
3758
3759void WebContentsImpl::Delete() {
Dave Tapuskae782bea2019-07-09 16:21:513760 auto* input_handler = GetFocusedFrameInputHandler();
3761 if (!input_handler)
[email protected]959be4c2014-04-08 15:01:333762 return;
3763
Dave Tapuskae782bea2019-07-09 16:21:513764 input_handler->Delete();
[email protected]959be4c2014-04-08 15:01:333765 RecordAction(base::UserMetricsAction("DeleteSelection"));
3766}
3767
3768void WebContentsImpl::SelectAll() {
Dave Tapuskae782bea2019-07-09 16:21:513769 auto* input_handler = GetFocusedFrameInputHandler();
3770 if (!input_handler)
[email protected]959be4c2014-04-08 15:01:333771 return;
3772
Dave Tapuskae782bea2019-07-09 16:21:513773 input_handler->SelectAll();
[email protected]959be4c2014-04-08 15:01:333774 RecordAction(base::UserMetricsAction("SelectAll"));
3775}
3776
yabinh351e7ec2017-03-10 02:43:243777void WebContentsImpl::CollapseSelection() {
Dave Tapuskae782bea2019-07-09 16:21:513778 auto* input_handler = GetFocusedFrameInputHandler();
3779 if (!input_handler)
[email protected]959be4c2014-04-08 15:01:333780 return;
3781
Dave Tapuskae782bea2019-07-09 16:21:513782 input_handler->CollapseSelection();
[email protected]1f3fc1d2014-04-03 14:50:173783}
3784
3785void WebContentsImpl::Replace(const base::string16& word) {
Dave Tapuskae782bea2019-07-09 16:21:513786 auto* input_handler = GetFocusedFrameInputHandler();
3787 if (!input_handler)
[email protected]1f3fc1d2014-04-03 14:50:173788 return;
3789
Dave Tapuskae782bea2019-07-09 16:21:513790 input_handler->Replace(word);
[email protected]1f3fc1d2014-04-03 14:50:173791}
3792
3793void WebContentsImpl::ReplaceMisspelling(const base::string16& word) {
Dave Tapuskae782bea2019-07-09 16:21:513794 auto* input_handler = GetFocusedFrameInputHandler();
3795 if (!input_handler)
[email protected]1f3fc1d2014-04-03 14:50:173796 return;
3797
Dave Tapuskae782bea2019-07-09 16:21:513798 input_handler->ReplaceMisspelling(word);
[email protected]959be4c2014-04-08 15:01:333799}
3800
3801void WebContentsImpl::NotifyContextMenuClosed(
3802 const CustomContextMenuContext& context) {
3803 RenderFrameHost* focused_frame = GetFocusedFrame();
3804 if (!focused_frame)
3805 return;
3806
3807 focused_frame->Send(new FrameMsg_ContextMenuClosed(
3808 focused_frame->GetRoutingID(), context));
3809}
3810
3811void WebContentsImpl::ExecuteCustomContextMenuCommand(
3812 int action, const CustomContextMenuContext& context) {
3813 RenderFrameHost* focused_frame = GetFocusedFrame();
3814 if (!focused_frame)
3815 return;
3816
3817 focused_frame->Send(new FrameMsg_CustomContextMenuAction(
3818 focused_frame->GetRoutingID(), context, action));
[email protected]4fed3702014-04-01 09:08:003819}
3820
[email protected]fc2b46b2014-05-03 16:33:453821gfx::NativeView WebContentsImpl::GetNativeView() {
3822 return view_->GetNativeView();
3823}
3824
3825gfx::NativeView WebContentsImpl::GetContentNativeView() {
3826 return view_->GetContentNativeView();
3827}
3828
3829gfx::NativeWindow WebContentsImpl::GetTopLevelNativeWindow() {
3830 return view_->GetTopLevelNativeWindow();
3831}
3832
3833gfx::Rect WebContentsImpl::GetViewBounds() {
3834 return view_->GetViewBounds();
3835}
3836
3837gfx::Rect WebContentsImpl::GetContainerBounds() {
3838 gfx::Rect rv;
3839 view_->GetContainerBounds(&rv);
3840 return rv;
3841}
3842
3843DropData* WebContentsImpl::GetDropData() {
3844 return view_->GetDropData();
3845}
3846
3847void WebContentsImpl::Focus() {
miu9e14e492014-10-25 02:39:043848 view_->Focus();
[email protected]fc2b46b2014-05-03 16:33:453849}
3850
3851void WebContentsImpl::SetInitialFocus() {
miu9e14e492014-10-25 02:39:043852 view_->SetInitialFocus();
[email protected]fc2b46b2014-05-03 16:33:453853}
3854
3855void WebContentsImpl::StoreFocus() {
miu9e14e492014-10-25 02:39:043856 view_->StoreFocus();
[email protected]fc2b46b2014-05-03 16:33:453857}
3858
3859void WebContentsImpl::RestoreFocus() {
miu9e14e492014-10-25 02:39:043860 view_->RestoreFocus();
[email protected]fc2b46b2014-05-03 16:33:453861}
3862
[email protected]b172aee2012-04-10 17:05:263863void WebContentsImpl::FocusThroughTabTraversal(bool reverse) {
Maxim Podgorodskiya926c4ff2017-11-20 02:06:393864 view_->FocusThroughTabTraversal(reverse);
[email protected]96d185d2009-04-24 03:28:543865}
3866
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:413867bool WebContentsImpl::ShowingInterstitialPage() {
creisadca9862017-06-16 19:12:113868 return interstitial_page_ != nullptr;
[email protected]96d185d2009-04-24 03:28:543869}
3870
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:413871InterstitialPageImpl* WebContentsImpl::GetInterstitialPage() {
creisadca9862017-06-16 19:12:113872 return interstitial_page_;
[email protected]686493142011-07-15 21:47:223873}
3874
[email protected]b172aee2012-04-10 17:05:263875bool WebContentsImpl::IsSavable() {
[email protected]a53209b2012-01-20 16:48:163876 // WebKit creates Document object when MIME type is application/xhtml+xml,
3877 // so we also support this MIME type.
3878 return contents_mime_type_ == "text/html" ||
3879 contents_mime_type_ == "text/xml" ||
3880 contents_mime_type_ == "application/xhtml+xml" ||
3881 contents_mime_type_ == "text/plain" ||
3882 contents_mime_type_ == "text/css" ||
Kinuko Yasuda74702f92017-07-31 03:27:533883 blink::IsSupportedJavascriptMimeType(contents_mime_type_);
[email protected]a53209b2012-01-20 16:48:163884}
3885
[email protected]b172aee2012-04-10 17:05:263886void WebContentsImpl::OnSavePage() {
[email protected]c7dd2f62011-07-18 15:57:593887 // If we can not save the page, try to download it.
[email protected]a53209b2012-01-20 16:48:163888 if (!IsSavable()) {
Min Qinda0ed2062018-02-23 22:00:533889 download::RecordSavePackageEvent(
3890 download::SAVE_PACKAGE_DOWNLOAD_ON_NON_HTML);
nasko89ad7742015-03-05 22:14:193891 SaveFrame(GetLastCommittedURL(), Referrer());
[email protected]27678b2a2012-02-04 22:09:143892 return;
[email protected]c7dd2f62011-07-18 15:57:593893 }
3894
3895 Stop();
3896
3897 // Create the save package and possibly prompt the user for the name to save
3898 // the page as. The user prompt is an asynchronous operation that runs on
3899 // another thread.
3900 save_package_ = new SavePackage(this);
3901 save_package_->GetSaveInfo();
3902}
3903
3904// Used in automated testing to bypass prompting the user for file names.
3905// Instead, the names and paths are hard coded rather than running them through
3906// file name sanitation and extension / mime checking.
[email protected]2dec8ec2013-02-07 19:20:343907bool WebContentsImpl::SavePage(const base::FilePath& main_file,
3908 const base::FilePath& dir_path,
[email protected]8ff00d72012-10-23 19:12:213909 SavePageType save_type) {
[email protected]c7dd2f62011-07-18 15:57:593910 // Stop the page from navigating.
3911 Stop();
3912
3913 save_package_ = new SavePackage(this, save_type, main_file, dir_path);
[email protected]8ff00d72012-10-23 19:12:213914 return save_package_->Init(SavePackageDownloadCreatedCallback());
[email protected]c7dd2f62011-07-18 15:57:593915}
3916
[email protected]3c71576ce2013-07-23 02:00:013917void WebContentsImpl::SaveFrame(const GURL& url,
3918 const Referrer& referrer) {
Xing Liu10329bf2018-03-20 19:22:143919 SaveFrameWithHeaders(url, referrer, std::string(), base::string16());
kundaji6c7f9692015-03-09 18:00:373920}
3921
Xing Liu10329bf2018-03-20 19:22:143922void WebContentsImpl::SaveFrameWithHeaders(
3923 const GURL& url,
3924 const Referrer& referrer,
3925 const std::string& headers,
3926 const base::string16& suggested_filename) {
Lei Zhang13a42e42019-02-19 23:28:113927 // Check and see if the guest can handle this.
3928 if (delegate_) {
3929 WebContents* guest_web_contents = nullptr;
3930 if (browser_plugin_embedder_) {
3931 BrowserPluginGuest* guest = browser_plugin_embedder_->GetFullPageGuest();
3932 if (guest)
3933 guest_web_contents = guest->GetWebContents();
3934 } else if (browser_plugin_guest_) {
3935 guest_web_contents = this;
3936 }
3937
3938 if (guest_web_contents && delegate_->GuestSaveFrame(guest_web_contents))
3939 return;
3940 }
3941
nasko89ad7742015-03-05 22:14:193942 if (!GetLastCommittedURL().is_valid())
[email protected]3c71576ce2013-07-23 02:00:013943 return;
sammc92af61552014-11-19 23:27:403944 if (delegate_ && delegate_->SaveFrame(url, referrer))
3945 return;
3946
nasko89ad7742015-03-05 22:14:193947 // TODO(nasko): This check for main frame is incorrect and should be fixed
brettw760f7442016-05-23 21:19:223948 // by explicitly passing in which frame this method should target. This would
3949 // indicate whether it's the main frame, and also tell us the frame pointer
3950 // to use for routing.
nasko89ad7742015-03-05 22:14:193951 bool is_main_frame = (url == GetLastCommittedURL());
brettw760f7442016-05-23 21:19:223952 RenderFrameHost* frame_host = GetMainFrame();
[email protected]3c71576ce2013-07-23 02:00:013953
avib7348942015-12-25 20:57:103954 int64_t post_id = -1;
[email protected]3c71576ce2013-07-23 02:00:013955 if (is_main_frame) {
Lucas Furukawa Gadani5553a152019-01-08 18:55:573956 NavigationEntry* entry = controller_.GetLastCommittedEntry();
[email protected]3c71576ce2013-07-23 02:00:013957 if (entry)
3958 post_id = entry->GetPostID();
3959 }
Ramin Halavati24e21012017-07-10 12:56:063960 net::NetworkTrafficAnnotationTag traffic_annotation =
3961 net::DefineNetworkTrafficAnnotation("download_web_contents_frame", R"(
3962 semantics {
3963 sender: "Save Page Action"
3964 description:
3965 "Saves the given frame's URL to the local file system."
3966 trigger:
3967 "The user has triggered a save operation on the frame through a "
3968 "context menu or other mechanism."
3969 data: "None."
3970 destination: WEBSITE
3971 }
3972 policy {
Ramin Halavati3b979782017-07-21 11:40:263973 cookies_allowed: YES
Ramin Halavati24e21012017-07-10 12:56:063974 cookies_store: "user"
3975 setting:
3976 "This feature cannot be disabled by settings, but it's is only "
3977 "triggered by user request."
3978 policy_exception_justification: "Not implemented."
3979 })");
Min Qina904f3302018-02-13 23:33:343980 auto params = std::make_unique<download::DownloadUrlParameters>(
brettw760f7442016-05-23 21:19:223981 url, frame_host->GetProcess()->GetID(),
3982 frame_host->GetRenderViewHost()->GetRoutingID(),
Yao Xiao65d20b82019-12-05 22:04:203983 frame_host->GetRoutingID(), traffic_annotation,
3984 frame_host->GetNetworkIsolationKey());
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_) {
W. James MacLean9437b5bf2019-12-06 17:33:444051 browser_plugin_embedder_->DragSourceEndedAt(client_x, client_y, screen_x,
4052 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 (mouse_lock_widget_) {
4144 if (mouse_lock_widget_->delegate()->GetAsWebContents() != this) {
4145 return mouse_lock_widget_->delegate()
4146 ->GetAsWebContents()
4147 ->GotResponseToLockMouseRequest(allowed);
4148 }
lfgad8244352016-07-13 16:55:514149
lfgf0cd46e2017-01-04 00:05:234150 if (mouse_lock_widget_->GotResponseToLockMouseRequest(allowed))
4151 return true;
4152 }
4153
4154 for (WebContentsImpl* current = this; current;
4155 current = current->GetOuterWebContents()) {
4156 current->mouse_lock_widget_ = nullptr;
4157 }
4158
lfgbee1e0a2016-06-08 21:24:214159 return false;
[email protected]0bfbf882011-12-22 18:19:274160}
4161
Joe Downing13dd76b2018-04-09 18:32:154162bool WebContentsImpl::GotResponseToKeyboardLockRequest(bool allowed) {
4163 if (!keyboard_lock_widget_)
4164 return false;
4165
4166 if (keyboard_lock_widget_->delegate()->GetAsWebContents() != this) {
4167 NOTREACHED();
4168 return false;
4169 }
4170
4171 // KeyboardLock is only supported when called by the top-level browsing
4172 // context and is not supported in embedded content scenarios.
4173 if (GetOuterWebContents())
4174 return false;
4175
4176 keyboard_lock_widget_->GotResponseToKeyboardLockRequest(allowed);
4177 return true;
4178}
4179
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:414180bool WebContentsImpl::HasOpener() {
Ivan Kotenkov2c0d2bb32017-11-01 15:41:284181 return GetOpener() != nullptr;
[email protected]a0358d72012-03-09 14:06:504182}
4183
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:414184RenderFrameHostImpl* WebContentsImpl::GetOpener() {
alexmose201c7cd2015-06-10 17:14:214185 FrameTreeNode* opener_ftn = frame_tree_.root()->opener();
lukasza6f8ac622017-06-06 03:10:204186 return opener_ftn ? opener_ftn->current_frame_host() : nullptr;
jochen55ff3502014-12-18 20:52:574187}
4188
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:414189bool WebContentsImpl::HasOriginalOpener() {
Ivan Kotenkov2c0d2bb32017-11-01 15:41:284190 return GetOriginalOpener() != nullptr;
jochen6004a362017-02-04 00:11:404191}
4192
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:414193RenderFrameHostImpl* WebContentsImpl::GetOriginalOpener() {
jochen6004a362017-02-04 00:11:404194 FrameTreeNode* opener_ftn = frame_tree_.root()->original_opener();
lukasza6f8ac622017-06-06 03:10:204195 return opener_ftn ? opener_ftn->current_frame_host() : nullptr;
jochen6004a362017-02-04 00:11:404196}
4197
[email protected]cb805452013-05-22 15:16:214198void WebContentsImpl::DidChooseColorInColorChooser(SkColor color) {
Joel Hockey85b379d2017-12-11 10:42:134199 color_chooser_->DidChooseColorInColorChooser(color);
[email protected]da8543762012-03-20 08:52:204200}
4201
[email protected]cb805452013-05-22 15:16:214202void WebContentsImpl::DidEndColorChooser() {
Joel Hockey85b379d2017-12-11 10:42:134203 color_chooser_.reset();
[email protected]da8543762012-03-20 08:52:204204}
4205
halton.huoca2eabd2015-07-06 08:17:404206int WebContentsImpl::DownloadImage(
4207 const GURL& url,
4208 bool is_favicon,
Fredrik Söderquistb2b39eb92019-11-18 16:16:504209 uint32_t preferred_size,
halton.huoca2eabd2015-07-06 08:17:404210 uint32_t max_bitmap_size,
4211 bool bypass_cache,
Avi Drissmana5a52dd2018-03-27 03:39:024212 WebContents::ImageDownloadCallback callback) {
amistry9f01b772015-07-29 01:54:554213 DCHECK_CURRENTLY_ON(BrowserThread::UI);
halton.huoca2eabd2015-07-06 08:17:404214 static int next_image_download_id = 0;
Julie Jeongeun Kimb1473572019-08-15 05:59:134215 const mojo::Remote<blink::mojom::ImageDownloader>& mojo_image_downloader =
halton.huoca2eabd2015-07-06 08:17:404216 GetMainFrame()->GetMojoImageDownloader();
amistry9f01b772015-07-29 01:54:554217 const int download_id = ++next_image_download_id;
4218 if (!mojo_image_downloader) {
4219 // If the renderer process is dead (i.e. crash, or memory pressure on
4220 // Android), the downloader service will be invalid. Pre-Mojo, this would
Avi Drissmana5a52dd2018-03-27 03:39:024221 // hang the callback indefinitely since the IPC would be dropped. Now,
amistry9f01b772015-07-29 01:54:554222 // respond with a 400 HTTP error code to indicate that something went wrong.
Sami Kyostila8e4d5a92019-08-02 12:45:054223 base::PostTask(
Eric Seckler8652dcd52018-09-20 10:42:284224 FROM_HERE, {BrowserThread::UI},
tzike2aca992017-09-05 08:50:544225 base::BindOnce(&WebContentsImpl::OnDidDownloadImage,
Avi Drissmana5a52dd2018-03-27 03:39:024226 weak_factory_.GetWeakPtr(), std::move(callback),
4227 download_id, url, 400, std::vector<SkBitmap>(),
4228 std::vector<gfx::Size>()));
amistry9f01b772015-07-29 01:54:554229 return download_id;
4230 }
4231
halton.huoca2eabd2015-07-06 08:17:404232 mojo_image_downloader->DownloadImage(
Fredrik Söderquistb2b39eb92019-11-18 16:16:504233 url, is_favicon, preferred_size, max_bitmap_size, bypass_cache,
tzike2aca992017-09-05 08:50:544234 base::BindOnce(&WebContentsImpl::OnDidDownloadImage,
Avi Drissmana5a52dd2018-03-27 03:39:024235 weak_factory_.GetWeakPtr(), std::move(callback),
4236 download_id, url));
amistry9f01b772015-07-29 01:54:554237 return download_id;
[email protected]795c28972012-12-06 06:13:394238}
4239
[email protected]36ec24f2014-01-09 00:32:084240void WebContentsImpl::Find(int request_id,
4241 const base::string16& search_text,
Rakina Zata Amni3f77dff2018-09-08 16:19:434242 blink::mojom::FindOptionsPtr options) {
thestig6057a6b22015-11-12 23:01:334243 // Cowardly refuse to search for no text.
4244 if (search_text.empty()) {
4245 NOTREACHED();
4246 return;
4247 }
4248
Rakina Zata Amni3f77dff2018-09-08 16:19:434249 GetOrCreateFindRequestManager()->Find(request_id, search_text,
4250 std::move(options));
[email protected]36ec24f2014-01-09 00:32:084251}
4252
4253void WebContentsImpl::StopFinding(StopFindAction action) {
paulmeyerfeafc2d2017-04-25 21:46:404254 if (FindRequestManager* manager = GetFindRequestManager())
4255 manager->StopFinding(action);
[email protected]36ec24f2014-01-09 00:32:084256}
4257
Tommy Steimel18360512017-11-01 00:38:194258bool WebContentsImpl::WasEverAudible() {
4259 return was_ever_audible_;
4260}
4261
Avi Drissmana5a52dd2018-03-27 03:39:024262void WebContentsImpl::GetManifest(GetManifestCallback callback) {
4263 manifest_manager_host_->GetManifest(std::move(callback));
mlamouriefdca9d2014-09-16 16:55:404264}
4265
bokanece34a82016-01-28 19:49:464266void WebContentsImpl::ExitFullscreen(bool will_cause_resize) {
mlamouri7a78d6fd2015-01-17 13:23:534267 // Clean up related state and initiate the fullscreen exit.
avi3627ecac2015-10-16 17:40:434268 GetRenderViewHost()->GetWidget()->RejectMouseLockOrUnlockIfNecessary();
bokanece34a82016-01-28 19:49:464269 ExitFullscreenMode(will_cause_resize);
mlamouri7a78d6fd2015-01-17 13:23:534270}
4271
Avi Drissmanced52b62019-08-14 21:25:464272void WebContentsImpl::ForSecurityDropFullscreen() {
4273 // There are two chains of WebContents to kick out of fullscreen.
4274 //
4275 // Chain 1, the inner/outer WebContents chain. If an inner WebContents has
4276 // done something that requires the browser to drop fullscreen, drop
4277 // fullscreen from it and any outer WebContents that may be in fullscreen.
4278 //
4279 // Chain 2, the opener WebContents chain. If a WebContents has done something
4280 // that requires the browser to drop fullscreen, drop fullscreen from any
4281 // WebContents that was involved in the chain of opening it.
4282 //
4283 // Note that these two chains don't interact, as only a top-level WebContents
4284 // can have an opener. This simplifies things.
4285
4286 WebContents* web_contents = this;
4287 while (web_contents) {
4288 if (web_contents->IsFullscreenForCurrentTab())
4289 web_contents->ExitFullscreen(true);
4290
4291 if (web_contents->HasOriginalOpener())
4292 web_contents = FromRenderFrameHost(web_contents->GetOriginalOpener());
4293 else
4294 web_contents = web_contents->GetOuterWebContents();
4295 }
4296}
4297
mariakhomenko44bdc4732015-04-29 01:55:384298void WebContentsImpl::ResumeLoadingCreatedWebContents() {
Nasko Oskov83a8cf92018-10-19 14:58:564299 if (delayed_load_url_params_.get()) {
4300 DCHECK(!delayed_open_url_params_);
4301 controller_.LoadURLWithParams(*delayed_load_url_params_.get());
4302 delayed_load_url_params_.reset(nullptr);
4303 return;
4304 }
4305
mariakhomenkoa4971c12015-07-21 19:04:374306 if (delayed_open_url_params_.get()) {
4307 OpenURL(*delayed_open_url_params_.get());
4308 delayed_open_url_params_.reset(nullptr);
4309 return;
4310 }
4311
mariakhomenko44bdc4732015-04-29 01:55:384312 // Resume blocked requests for both the RenderViewHost and RenderFrameHost.
4313 // TODO(brettw): It seems bogus to reach into here and initialize the host.
dfalcantarae6b7b46752015-07-10 18:14:164314 if (is_resume_pending_) {
4315 is_resume_pending_ = false;
avi0f1bbc9712015-11-17 02:58:134316 GetRenderViewHost()->GetWidget()->Init();
dfalcantarae6b7b46752015-07-10 18:14:164317 GetMainFrame()->Init();
4318 }
mariakhomenko44bdc4732015-04-29 01:55:384319}
4320
[email protected]b172aee2012-04-10 17:05:264321bool WebContentsImpl::FocusLocationBarByDefault() {
Xiyuan Xia2a66b112018-12-06 15:52:164322 if (should_focus_location_bar_by_default_)
[email protected]0c9406632013-02-08 01:13:334323 return true;
Xiyuan Xia2a66b112018-12-06 15:52:164324
[email protected]0c9406632013-02-08 01:13:334325 return delegate_ && delegate_->ShouldFocusLocationBarByDefault(this);
[email protected]0bfbf882011-12-22 18:19:274326}
4327
Tommy C. Li8bb5bf42019-01-18 18:45:514328void WebContentsImpl::SetFocusToLocationBar() {
[email protected]0bfbf882011-12-22 18:19:274329 if (delegate_)
Tommy C. Li8bb5bf42019-01-18 18:45:514330 delegate_->SetFocusToLocationBar();
[email protected]c40d6232011-03-25 00:16:214331}
4332
clamy0e119882015-07-31 16:12:334333void WebContentsImpl::DidStartNavigation(NavigationHandle* navigation_handle) {
Charles Harrison53096ba2017-12-15 15:39:484334 TRACE_EVENT1("navigation", "WebContentsImpl::DidStartNavigation",
4335 "navigation_handle", navigation_handle);
ericwilligersde386342016-10-19 02:35:094336 for (auto& observer : observers_)
4337 observer.DidStartNavigation(navigation_handle);
Becca Hughesd11d6502018-07-31 17:01:284338
4339 if (display_cutout_host_impl_)
4340 display_cutout_host_impl_->DidStartNavigation(navigation_handle);
Xiyuan Xia2a66b112018-12-06 15:52:164341
4342 if (navigation_handle->IsInMainFrame()) {
4343 // When the browser is started with about:blank as the startup URL, focus
4344 // the location bar (which will also select its contents) so people can
4345 // simply begin typing to navigate elsewhere.
4346 //
4347 // We need to be careful not to trigger this for anything other than the
4348 // startup navigation. In particular, if we allow an attacker to open a
4349 // popup to about:blank, then navigate, focusing the Omnibox will cause the
4350 // end of the new URL to be scrolled into view instead of the start,
4351 // allowing the attacker to spoof other URLs. The conditions checked here
4352 // are all aimed at ensuring no such attacker-controlled navigation can
4353 // trigger this.
4354 should_focus_location_bar_by_default_ =
4355 controller_.IsInitialNavigation() &&
4356 !navigation_handle->IsRendererInitiated() &&
4357 navigation_handle->GetURL() == url::kAboutBlankURL;
4358 }
clamy0e119882015-07-31 16:12:334359}
4360
4361void WebContentsImpl::DidRedirectNavigation(
4362 NavigationHandle* navigation_handle) {
Charles Harrison53096ba2017-12-15 15:39:484363 TRACE_EVENT1("navigation", "WebContentsImpl::DidRedirectNavigation",
4364 "navigation_handle", navigation_handle);
ericwilligersde386342016-10-19 02:35:094365 for (auto& observer : observers_)
4366 observer.DidRedirectNavigation(navigation_handle);
jam5f358e52017-02-13 16:37:034367
4368 // Notify accessibility if this is a reload. This has to called on the
4369 // BrowserAccessibilityManager associated with the old RFHI.
4370 if (navigation_handle->GetReloadType() != ReloadType::NONE) {
Mohamed Abdelhalim95794962019-09-20 11:16:494371 NavigationRequest* request = NavigationRequest::From(navigation_handle);
jam5f358e52017-02-13 16:37:034372 BrowserAccessibilityManager* manager =
Mohamed Abdelhalim95794962019-09-20 11:16:494373 request->frame_tree_node()
jam5f358e52017-02-13 16:37:034374 ->current_frame_host()
4375 ->browser_accessibility_manager();
4376 if (manager)
4377 manager->UserIsReloading();
4378 }
clamy0e119882015-07-31 16:12:334379}
4380
clamyefca29e2015-09-17 00:22:114381void WebContentsImpl::ReadyToCommitNavigation(
4382 NavigationHandle* navigation_handle) {
Charles Harrison53096ba2017-12-15 15:39:484383 TRACE_EVENT1("navigation", "WebContentsImpl::ReadyToCommitNavigation",
4384 "navigation_handle", navigation_handle);
ericwilligersde386342016-10-19 02:35:094385 for (auto& observer : observers_)
4386 observer.ReadyToCommitNavigation(navigation_handle);
John Abd-El-Malekd4882642017-12-04 21:45:194387
Kenneth Russell954ed9ce2018-04-12 23:07:014388 // If any domains are blocked from accessing 3D APIs because they may
4389 // have caused the GPU to reset recently, unblock them here if the user
4390 // initiated this navigation. This implies that the user was involved in
4391 // the decision to navigate, so there's no concern about
4392 // denial-of-service issues. Want to do this as early as
4393 // possible to avoid race conditions with pages attempting to access
4394 // WebGL early on.
4395 //
4396 // TODO(crbug.com/617904): currently navigations initiated by the browser
4397 // (reload button, reload menu option, pressing return in the Omnibox)
4398 // return false from HasUserGesture(). If or when that is addressed,
4399 // remove the check for IsRendererInitiated() below.
4400 //
4401 // TODO(crbug.com/832180): HasUserGesture comes from the renderer
4402 // process and isn't validated. Until it is, don't trust it.
4403 if (!navigation_handle->IsRendererInitiated()) {
4404 GpuDataManagerImpl::GetInstance()->UnblockDomainFrom3DAPIs(
4405 navigation_handle->GetURL());
4406 }
4407
John Abd-El-Malekfde08e6da2017-12-05 04:06:244408 if (navigation_handle->IsSameDocument())
4409 return;
4410
Livvie Lin139bf442018-08-10 00:18:544411 // SSLInfo is not needed on subframe navigations since the main-frame
4412 // certificate is the only one that can be inspected (using the info
4413 // bubble) without refreshing the page with DevTools open.
Carlos ILaed1e7b2019-03-12 19:27:214414 // We don't call DidStartResourceResponse on net errors, since that results on
4415 // existing cert exceptions being revoked, which leads to weird behavior with
4416 // committed interstitials or while offline. We only need the error check for
4417 // the main frame case because unlike this method, SubresourceResponseStarted
4418 // does not get called on network errors.
4419 if (navigation_handle->IsInMainFrame() &&
4420 navigation_handle->GetNetErrorCode() == net::OK) {
Livvie Lin139bf442018-08-10 00:18:544421 controller_.ssl_manager()->DidStartResourceResponse(
Lukasz Anforowiczee527cc2019-11-19 02:06:594422 url::Origin::Create(navigation_handle->GetURL()),
Camille Lamy62b826012019-02-26 09:15:474423 navigation_handle->GetSSLInfo().has_value()
4424 ? net::IsCertStatusError(
4425 navigation_handle->GetSSLInfo()->cert_status)
4426 : false);
4427 }
John Abd-El-Malekfde08e6da2017-12-05 04:06:244428
4429 SetNotWaitingForResponse();
clamyefca29e2015-09-17 00:22:114430}
4431
clamy0e119882015-07-31 16:12:334432void WebContentsImpl::DidFinishNavigation(NavigationHandle* navigation_handle) {
Charles Harrison53096ba2017-12-15 15:39:484433 TRACE_EVENT1("navigation", "WebContentsImpl::DidFinishNavigation",
4434 "navigation_handle", navigation_handle);
Steven Holte38041342018-06-22 22:00:574435
ericwilligersde386342016-10-19 02:35:094436 for (auto& observer : observers_)
4437 observer.DidFinishNavigation(navigation_handle);
clamy0e119882015-07-31 16:12:334438
Becca Hughesd11d6502018-07-31 17:01:284439 if (display_cutout_host_impl_)
4440 display_cutout_host_impl_->DidFinishNavigation(navigation_handle);
4441
jam5f358e52017-02-13 16:37:034442 if (navigation_handle->HasCommitted()) {
Yue Ru Sun979c4b162019-11-06 16:11:044443 if (navigation_handle->IsInMainFrame()) {
4444 last_committed_source_id_including_same_document_ =
Steven Holte38041342018-06-22 22:00:574445 ukm::ConvertToSourceId(navigation_handle->GetNavigationId(),
4446 ukm::SourceIdType::NAVIGATION_ID);
Yue Ru Sun979c4b162019-11-06 16:11:044447
4448 if (!navigation_handle->IsSameDocument()) {
4449 was_ever_audible_ = false;
4450 last_committed_source_id_ =
4451 last_committed_source_id_including_same_document_;
4452 }
Tommy Steimel18360512017-11-01 00:38:194453 }
dmazzonibf8cec42015-02-08 08:28:084454 }
Xiyuan Xia2a66b112018-12-06 15:52:164455
4456 // If we didn't end up on about:blank after setting this in DidStartNavigation
4457 // then don't focus the location bar.
4458 if (should_focus_location_bar_by_default_ &&
4459 navigation_handle->GetURL() != url::kAboutBlankURL) {
4460 should_focus_location_bar_by_default_ = false;
4461 }
yilkal796e89e2019-10-02 23:58:284462
yilkal172da772019-11-06 22:49:074463 if (navigation_handle->IsInMainFrame() && first_navigation_completed_)
4464 RecordMaxFrameCountUMA(max_loaded_frame_count_);
yilkal796e89e2019-10-02 23:58:284465
yilkal172da772019-11-06 22:49:074466 // If navigation has successfully finished in the main frame, set
4467 // |first_navigation_completed_| to true so that we will record
4468 // |max_loaded_frame_count_| above when future main frame navigations finish.
4469 if (navigation_handle->IsInMainFrame() && !navigation_handle->IsErrorPage()) {
4470 first_navigation_completed_ = true;
4471
4472 // Navigation has completed in main frame. Reset |max_loaded_frame_count_|.
4473 // |max_loaded_frame_count_| is not necessarily 1 if the navigation was
4474 // served from BackForwardCache.
4475 max_loaded_frame_count_ =
4476 GetMainFrame()->frame_tree_node()->GetFrameTreeSize();
yilkal796e89e2019-10-02 23:58:284477 }
[email protected]400992b2012-06-14 00:03:544478}
4479
[email protected]b80624c2014-02-09 02:46:554480void WebContentsImpl::DidFailLoadWithError(
4481 RenderFrameHostImpl* render_frame_host,
[email protected]b80624c2014-02-09 02:46:554482 const GURL& url,
[email protected]b80624c2014-02-09 02:46:554483 int error_code,
Yutaka Hirano16d3c5d2017-07-31 06:17:304484 const base::string16& error_description) {
ericwilligersde386342016-10-19 02:35:094485 for (auto& observer : observers_) {
Yutaka Hirano16d3c5d2017-07-31 06:17:304486 observer.DidFailLoad(render_frame_host, url, error_code, error_description);
ericwilligersde386342016-10-19 02:35:094487 }
[email protected]b80624c2014-02-09 02:46:554488}
4489
[email protected]52913802013-12-10 05:52:184490void WebContentsImpl::NotifyChangedNavigationState(
4491 InvalidateTypes changed_flags) {
4492 NotifyNavigationStateChanged(changed_flags);
4493}
4494
toyoshim0df1d3a2016-09-09 09:52:484495void WebContentsImpl::DidStartNavigationToPendingEntry(const GURL& url,
4496 ReloadType reload_type) {
[email protected]3691e5cf2014-01-22 10:16:204497 // Notify observers about navigation.
ericwilligersde386342016-10-19 02:35:094498 for (auto& observer : observers_)
4499 observer.DidStartNavigationToPendingEntry(url, reload_type);
[email protected]3691e5cf2014-01-22 10:16:204500}
4501
lukasza1d025732016-09-28 21:36:084502bool WebContentsImpl::ShouldTransferNavigation(bool is_main_frame_navigation) {
creis29460272015-12-16 04:38:224503 if (!delegate_)
4504 return true;
lukasza1d025732016-09-28 21:36:084505 return delegate_->ShouldTransferNavigation(is_main_frame_navigation);
creis29460272015-12-16 04:38:224506}
4507
[email protected]aa62afd2014-04-22 19:22:464508bool WebContentsImpl::ShouldPreserveAbortedURLs() {
4509 if (!delegate_)
4510 return false;
4511 return delegate_->ShouldPreserveAbortedURLs(this);
4512}
4513
[email protected]0d0f4c492014-04-02 06:42:574514void WebContentsImpl::DidNavigateMainFramePreCommit(
[email protected]5cfbddc2014-06-23 23:52:234515 bool navigation_is_within_page) {
[email protected]0d0f4c492014-04-02 06:42:574516 // Ensure fullscreen mode is exited before committing the navigation to a
4517 // different page. The next page will not start out assuming it is in
4518 // fullscreen mode.
[email protected]5cfbddc2014-06-23 23:52:234519 if (navigation_is_within_page) {
[email protected]dfc39cb2014-04-09 22:58:194520 // No page change? Then, the renderer and browser can remain in fullscreen.
[email protected]0d0f4c492014-04-02 06:42:574521 return;
4522 }
alexmos5dd617d2016-06-07 04:21:154523 if (IsFullscreenForCurrentTab())
bokanece34a82016-01-28 19:49:464524 ExitFullscreen(false);
alexmos5dd617d2016-06-07 04:21:154525 DCHECK(!IsFullscreenForCurrentTab());
Joe Downingc1a57efe2018-05-09 18:23:544526
4527 // Clean up keyboard lock state when navigating.
4528 CancelKeyboardLock(keyboard_lock_widget_);
[email protected]0d0f4c492014-04-02 06:42:574529}
4530
[email protected]37567b432014-02-12 01:12:224531void WebContentsImpl::DidNavigateMainFramePostCommit(
sky8b00392d2015-01-10 00:30:284532 RenderFrameHostImpl* render_frame_host,
[email protected]37567b432014-02-12 01:12:224533 const LoadCommittedDetails& details,
4534 const FrameHostMsg_DidCommitProvisionalLoad_Params& params) {
4535 if (details.is_navigation_to_different_page()) {
4536 // Clear the status bubble. This is a workaround for a bug where WebKit
4537 // doesn't let us know that the cursor left an element during a
4538 // transition (this is also why the mouse cursor remains as a hand after
4539 // clicking on a link); see bugs 1184641 and 980803. We don't want to
4540 // clear the bubble when a user navigates to a named anchor in the same
4541 // page.
Lukasz Anforowiczfd707142018-02-07 19:46:134542 ClearTargetURL();
lanwei9d343ad22015-02-11 01:46:004543
4544 RenderWidgetHostViewBase* rwhvb =
4545 static_cast<RenderWidgetHostViewBase*>(GetRenderWidgetHostView());
4546 if (rwhvb)
4547 rwhvb->OnDidNavigateMainFrameToNewPage();
[email protected]37567b432014-02-12 01:12:224548 }
4549
[email protected]e85165c642014-06-10 14:34:314550 if (delegate_)
[email protected]37567b432014-02-12 01:12:224551 delegate_->DidNavigateMainFramePostCommit(this);
[email protected]e85165c642014-06-10 14:34:314552 view_->SetOverscrollControllerEnabled(CanOverscrollContent());
Shivani Sharma985474f2019-05-23 21:36:474553
4554 if (!details.is_same_document && GetInnerWebContents().empty())
4555 had_inner_webcontents_ = false;
Carlos Caballero91fffb22019-10-29 15:24:304556
4557 if (details.is_navigation_to_different_page() &&
4558 GetRenderViewHost()->did_first_visually_non_empty_paint()) {
4559 // This event will not fire again if the page is restored from the
4560 // BackForwardCache. So fire it ourselves if needed.
4561 DidFirstVisuallyNonEmptyPaint(GetRenderViewHost());
4562 }
Carlos Caballero1215f882019-10-29 15:58:434563
4564 if (GetRenderViewHost()->theme_color() != last_sent_theme_color_) {
4565 // This event will not fire again if the page is restored from the
4566 // BackForwardCache. So fire it ourselves if needed.
4567 OnThemeColorChanged(GetRenderViewHost());
4568 }
[email protected]37567b432014-02-12 01:12:224569}
4570
4571void WebContentsImpl::DidNavigateAnyFramePostCommit(
4572 RenderFrameHostImpl* render_frame_host,
4573 const LoadCommittedDetails& details,
4574 const FrameHostMsg_DidCommitProvisionalLoad_Params& params) {
[email protected]b4c84012014-04-28 19:51:104575 // Now that something has committed, we don't need to track whether the
4576 // initial page has been accessed.
4577 has_accessed_initial_document_ = false;
4578
[email protected]37567b432014-02-12 01:12:224579 // If we navigate off the page, close all JavaScript dialogs.
eugenebutee08663a2017-04-27 17:43:124580 if (!details.is_same_document)
creis89a0f782015-05-27 16:13:174581 CancelActiveAndPendingDialogs();
[email protected]37567b432014-02-12 01:12:224582
avid53461d2016-02-25 17:15:044583 // If this is a user-initiated navigation, start allowing JavaScript dialogs
4584 // again.
avi5d3b8692016-10-12 22:00:464585 if (params.gesture == NavigationGestureUser && dialog_manager_) {
avi6879fcf2017-01-21 05:27:534586 dialog_manager_->CancelDialogs(this, /*reset_state=*/true);
avi5d3b8692016-10-12 22:00:464587 }
[email protected]37567b432014-02-12 01:12:224588}
4589
4590void WebContentsImpl::SetMainFrameMimeType(const std::string& mime_type) {
4591 contents_mime_type_ = mime_type;
4592}
4593
[email protected]277857a2014-06-03 10:38:014594bool WebContentsImpl::CanOverscrollContent() const {
[email protected]e85165c642014-06-10 14:34:314595 // Disable overscroll when touch emulation is on. See crbug.com/369938.
[email protected]34ff1cfc2014-08-20 06:16:054596 if (force_disable_overscroll_content_)
[email protected]e85165c642014-06-10 14:34:314597 return false;
4598
[email protected]37567b432014-02-12 01:12:224599 if (delegate_)
4600 return delegate_->CanOverscrollContent();
4601
4602 return false;
4603}
4604
Carlos Caballero1215f882019-10-29 15:58:434605void WebContentsImpl::OnThemeColorChanged(RenderViewHostImpl* source) {
4606 if (source->did_first_visually_non_empty_paint() &&
4607 last_sent_theme_color_ != source->theme_color()) {
ericwilligersde386342016-10-19 02:35:094608 for (auto& observer : observers_)
Carlos Caballero1215f882019-10-29 15:58:434609 observer.DidChangeThemeColor(source->theme_color());
4610 last_sent_theme_color_ = source->theme_color();
peter90afaba2015-06-01 12:05:294611 }
[email protected]e7104762014-06-20 19:17:254612}
4613
[email protected]b172aee2012-04-10 17:05:264614void WebContentsImpl::OnDidLoadResourceFromMemoryCache(
nicka0ac8382016-12-15 23:59:234615 RenderFrameHostImpl* source,
[email protected]724159a2010-12-30 01:11:184616 const GURL& url,
[email protected]70435962011-08-02 20:13:284617 const std::string& http_method,
[email protected]6d6cfb3a2012-05-23 22:53:184618 const std::string& mime_type,
[email protected]6c1e05212014-07-31 00:59:404619 ResourceType resource_type) {
ericwilligersde386342016-10-19 02:35:094620 for (auto& observer : observers_)
4621 observer.DidLoadResourceFromMemoryCache(url, mime_type, resource_type);
[email protected]b0f724c2013-09-05 04:21:134622
Steven Bingler674fb0822018-12-12 18:04:524623 if (url.is_valid() && url.SchemeIsHTTPOrHTTPS()) {
nicka0ac8382016-12-15 23:59:234624 StoragePartition* partition = source->GetProcess()->GetStoragePartition();
John Abd-El-Malekf4fe9d82019-07-31 04:05:594625 partition->GetNetworkContext()->NotifyExternalCacheHit(
Matt Menke2a33dee2019-12-02 17:01:544626 url, http_method, source->GetNetworkIsolationKey());
[email protected]8bfc8272013-09-09 20:10:534627 }
[email protected]724159a2010-12-30 01:11:184628}
4629
carloskd9d97942017-02-16 08:58:094630void WebContentsImpl::DidDisplayInsecureContent() {
estarkc2273502016-10-12 22:03:024631 controller_.ssl_manager()->DidDisplayMixedContent();
[email protected]724159a2010-12-30 01:11:184632}
4633
Dave Tapuskaa657d872019-10-21 20:23:214634void WebContentsImpl::DidContainInsecureFormAction() {
elawrenceb2ac2a232017-03-27 21:46:254635 controller_.ssl_manager()->DidContainInsecureFormAction();
4636}
4637
nicka0ac8382016-12-15 23:59:234638void WebContentsImpl::OnDidRunInsecureContent(RenderFrameHostImpl* source,
4639 const GURL& security_origin,
estark910b4572015-12-09 20:55:414640 const GURL& target_url) {
nicka0ac8382016-12-15 23:59:234641 // TODO(nick, estark): Should we call FilterURL using |source|'s process on
4642 // these parameters? |target_url| seems unused, except for a log message. And
4643 // |security_origin| might be replaceable with the origin of the main frame.
carloskd9d97942017-02-16 08:58:094644 DidRunInsecureContent(security_origin, target_url);
4645}
4646
4647void WebContentsImpl::DidRunInsecureContent(const GURL& security_origin,
4648 const GURL& target_url) {
[email protected]9450c462013-11-23 01:22:584649 LOG(WARNING) << security_origin << " ran insecure content from "
4650 << target_url.possibly_invalid_spec();
[email protected]e6e30ac2014-01-13 21:24:394651 RecordAction(base::UserMetricsAction("SSL.RanInsecureContent"));
estark910b4572015-12-09 20:55:414652 if (base::EndsWith(security_origin.spec(), kDotGoogleDotCom,
brettwa7ff1b292015-07-16 17:49:294653 base::CompareCase::INSENSITIVE_ASCII))
[email protected]e6e30ac2014-01-13 21:24:394654 RecordAction(base::UserMetricsAction("SSL.RanInsecureContentGoogle"));
estarkc2273502016-10-12 22:03:024655 controller_.ssl_manager()->DidRunMixedContent(security_origin);
[email protected]724159a2010-12-30 01:11:184656}
4657
carloskd9d97942017-02-16 08:58:094658void WebContentsImpl::PassiveInsecureContentFound(const GURL& resource_url) {
Sergey Kuznetsov3ebc01b2018-02-01 04:57:364659 if (delegate_) {
4660 delegate_->PassiveInsecureContentFound(resource_url);
4661 }
carloskd9d97942017-02-16 08:58:094662}
4663
4664bool WebContentsImpl::ShouldAllowRunningInsecureContent(
4665 WebContents* web_contents,
4666 bool allowed_per_prefs,
4667 const url::Origin& origin,
4668 const GURL& resource_url) {
Sergey Kuznetsov3ebc01b2018-02-01 04:57:364669 if (delegate_) {
4670 return delegate_->ShouldAllowRunningInsecureContent(
4671 web_contents, allowed_per_prefs, origin, resource_url);
4672 }
4673
4674 return allowed_per_prefs;
carloskd9d97942017-02-16 08:58:094675}
4676
Lukasz Anforowicze1b954d92017-10-30 21:28:064677void WebContentsImpl::ViewSource(RenderFrameHostImpl* frame) {
4678 DCHECK_EQ(this, WebContents::FromRenderFrameHost(frame));
4679
4680 // Don't do anything if there is no |delegate_| that could accept and show the
4681 // new WebContents containing the view-source.
4682 if (!delegate_)
4683 return;
4684
4685 // Use the last committed entry, since the pending entry hasn't loaded yet and
4686 // won't be copied into the cloned tab.
4687 NavigationEntryImpl* last_committed_entry =
4688 static_cast<NavigationEntryImpl*>(frame->frame_tree_node()
4689 ->navigator()
4690 ->GetController()
4691 ->GetLastCommittedEntry());
4692 if (!last_committed_entry)
4693 return;
4694
4695 FrameNavigationEntry* frame_entry =
4696 last_committed_entry->GetFrameEntry(frame->frame_tree_node());
4697 if (!frame_entry)
4698 return;
4699
4700 // Any new WebContents opened while this WebContents is in fullscreen can be
4701 // used to confuse the user, so drop fullscreen.
Avi Drissmand18c51972018-08-15 18:32:094702 ForSecurityDropFullscreen();
Lukasz Anforowicze1b954d92017-10-30 21:28:064703
4704 // We intentionally don't share the SiteInstance with the original frame so
4705 // that view source has a consistent process model and always ends up in a new
4706 // process (https://crbug.com/699493).
4707 scoped_refptr<SiteInstanceImpl> site_instance_for_view_source = nullptr;
Lukasz Anforowicz435bcb582019-07-12 20:50:064708 // Referrer and initiator are not important, because view-source should not
4709 // hit the network, but should be served from the cache instead.
Lukasz Anforowicze1b954d92017-10-30 21:28:064710 Referrer referrer_for_view_source;
Lukasz Anforowicz435bcb582019-07-12 20:50:064711 base::Optional<url::Origin> initiator_for_view_source = base::nullopt;
Lukasz Anforowicze1b954d92017-10-30 21:28:064712 // Do not restore title, derive it from the url.
4713 base::string16 title_for_view_source;
4714 auto navigation_entry = std::make_unique<NavigationEntryImpl>(
4715 site_instance_for_view_source, frame_entry->url(),
Lukasz Anforowicz435bcb582019-07-12 20:50:064716 referrer_for_view_source, initiator_for_view_source,
4717 title_for_view_source, ui::PAGE_TRANSITION_LINK,
Marijn Kruisselbrink7a0d5e182018-05-24 22:55:094718 /* is_renderer_initiated = */ false,
4719 /* blob_url_loader_factory = */ nullptr);
Lukasz Anforowicze1b954d92017-10-30 21:28:064720 navigation_entry->SetVirtualURL(GURL(content::kViewSourceScheme +
4721 std::string(":") +
4722 frame_entry->url().spec()));
4723
Matt Menke2a33dee2019-12-02 17:01:544724 navigation_entry->set_network_isolation_key(frame->GetNetworkIsolationKey());
Yao Xiaof68f57d2019-10-03 04:20:054725
Lukasz Anforowicze1b954d92017-10-30 21:28:064726 // Do not restore scroller position.
4727 // TODO(creis, lukasza, arthursonzogni): Do not reuse the original PageState,
4728 // but start from a new one and only copy the needed data.
4729 const PageState& new_page_state =
4730 frame_entry->page_state().RemoveScrollOffset();
4731
4732 scoped_refptr<FrameNavigationEntry> new_frame_entry =
4733 navigation_entry->root_node()->frame_entry;
4734 new_frame_entry->set_method(frame_entry->method());
4735 new_frame_entry->SetPageState(new_page_state);
4736
4737 // Create a new WebContents, which is used to display the source code.
erikchenbee5c9622018-04-27 19:30:254738 std::unique_ptr<WebContents> view_source_contents =
Erik Chenbb8e738e2018-04-28 14:10:434739 Create(CreateParams(GetBrowserContext()));
Lukasz Anforowicze1b954d92017-10-30 21:28:064740
4741 // Restore the previously created NavigationEntry.
4742 std::vector<std::unique_ptr<NavigationEntry>> navigation_entries;
4743 navigation_entries.push_back(std::move(navigation_entry));
4744 view_source_contents->GetController().Restore(0, RestoreType::CURRENT_SESSION,
4745 &navigation_entries);
4746
4747 // Add |view_source_contents| as a new tab.
4748 gfx::Rect initial_rect;
4749 constexpr bool kUserGesture = true;
4750 bool ignored_was_blocked;
erikchenbee5c9622018-04-27 19:30:254751 delegate_->AddNewContents(this, std::move(view_source_contents),
Lukasz Anforowicze1b954d92017-10-30 21:28:064752 WindowOpenDisposition::NEW_FOREGROUND_TAB,
4753 initial_rect, kUserGesture, &ignored_was_blocked);
4754 // Note that the |delegate_| could have deleted |view_source_contents| during
4755 // AddNewContents method call.
4756}
4757
Lukasz Anforowiczee527cc2019-11-19 02:06:594758void WebContentsImpl::SubresourceResponseStarted(
4759 const url::Origin& origin_of_final_response_url,
4760 net::CertStatus cert_status) {
4761 controller_.ssl_manager()->DidStartResourceResponse(
4762 origin_of_final_response_url, cert_status);
Jay Civellid5904f02018-03-23 19:43:574763 SetNotWaitingForResponse();
4764}
4765
Jay Civelli116683f2018-03-27 19:56:234766void WebContentsImpl::ResourceLoadComplete(
Jay Civelli52a63302018-04-30 22:39:554767 RenderFrameHost* render_frame_host,
Clark DuVall36164bd2018-08-09 22:49:084768 const GlobalRequestID& request_id,
Jay Civelli116683f2018-03-27 19:56:234769 mojom::ResourceLoadInfoPtr resource_load_info) {
John Abd-El-Malek9b8f7fa2017-12-04 23:52:254770 for (auto& observer : observers_) {
Clark DuVall36164bd2018-08-09 22:49:084771 observer.ResourceLoadComplete(render_frame_host, request_id,
4772 *resource_load_info);
John Abd-El-Malek9b8f7fa2017-12-04 23:52:254773 }
John Abd-El-Malekd4882642017-12-04 21:45:194774}
4775
Wei Li5bb659742018-02-14 03:07:584776void WebContentsImpl::PrintCrossProcessSubframe(
4777 const gfx::Rect& rect,
4778 int document_cookie,
4779 RenderFrameHost* subframe_host) {
Wei Li5c0b4d12018-03-22 15:05:224780 auto* outer_contents = GetOuterWebContents();
4781 if (outer_contents) {
4782 // When an extension or app page is printed, the content should be
4783 // composited with outer content, so the outer contents should handle the
4784 // print request.
4785 outer_contents->PrintCrossProcessSubframe(rect, document_cookie,
4786 subframe_host);
4787 return;
4788 }
4789
Wei Li5bb659742018-02-14 03:07:584790 // If there is no delegate such as in tests or during deletion, do nothing.
4791 if (!delegate_)
4792 return;
4793
4794 delegate_->PrintCrossProcessSubframe(this, rect, document_cookie,
4795 subframe_host);
4796}
4797
rob.buis300b0872017-03-10 20:43:584798#if defined(OS_ANDROID)
4799base::android::ScopedJavaLocalRef<jobject>
4800WebContentsImpl::GetJavaRenderFrameHostDelegate() {
4801 return GetJavaWebContents();
4802}
4803#endif
4804
estark910b4572015-12-09 20:55:414805void WebContentsImpl::OnDidDisplayContentWithCertificateErrors(
Zhuoyu Qian4353f7f2018-01-08 05:33:004806 RenderFrameHostImpl* source) {
estarkc2273502016-10-12 22:03:024807 controller_.ssl_manager()->DidDisplayContentWithCertErrors();
estark910b4572015-12-09 20:55:414808}
4809
4810void WebContentsImpl::OnDidRunContentWithCertificateErrors(
Zhuoyu Qian4353f7f2018-01-08 05:33:004811 RenderFrameHostImpl* source) {
nicka0ac8382016-12-15 23:59:234812 // TODO(nick, estark): Do we need to consider |source| here somehow?
estarkcd2e30c2016-08-12 06:51:154813 NavigationEntry* entry = controller_.GetVisibleEntry();
4814 if (!entry)
estarkaeda1422016-05-10 02:31:124815 return;
4816
estarkaeda1422016-05-10 02:31:124817 // TODO(estark): check that this does something reasonable for
4818 // about:blank and sandboxed origins. https://crbug.com/609527
estarkcd2e30c2016-08-12 06:51:154819 controller_.ssl_manager()->DidRunContentWithCertErrors(
4820 entry->GetURL().GetOrigin());
estark910b4572015-12-09 20:55:414821}
4822
Kouhei Ueno97e0990d2019-09-25 02:57:274823void WebContentsImpl::DOMContentLoaded(RenderFrameHost* render_frame_host) {
ericwilligersde386342016-10-19 02:35:094824 for (auto& observer : observers_)
Kouhei Uenof6e96242019-09-27 01:54:354825 observer.DOMContentLoaded(render_frame_host);
[email protected]724159a2010-12-30 01:11:184826}
4827
nicka0ac8382016-12-15 23:59:234828void WebContentsImpl::OnDidFinishLoad(RenderFrameHostImpl* source,
4829 const GURL& url) {
[email protected]8b3af1e2014-01-24 13:29:124830 GURL validated_url(url);
nicka0ac8382016-12-15 23:59:234831 source->GetProcess()->FilterURL(false, &validated_url);
[email protected]028053d42014-03-05 22:20:374832
ericwilligersde386342016-10-19 02:35:094833 for (auto& observer : observers_)
nicka0ac8382016-12-15 23:59:234834 observer.DidFinishLoad(source, validated_url);
yilkal172da772019-11-06 22:49:074835
4836 size_t tree_size = frame_tree_.root()->GetFrameTreeSize();
4837 if (max_loaded_frame_count_ < tree_size)
4838 max_loaded_frame_count_ = tree_size;
4839
4840 if (!source->GetParent())
4841 UMA_HISTOGRAM_COUNTS_1000("Navigation.MainFrame.FrameCount", tree_size);
[email protected]1a55c5be2011-11-29 11:36:314842}
4843
Dave Tapuska549baab2019-03-19 15:52:404844void WebContentsImpl::OnGoToEntryAtOffset(RenderFrameHostImpl* source,
Arthur Sonzognia7d715a2018-09-20 16:11:134845 int offset,
Dave Tapuska043b94a2019-09-10 14:14:354846 bool has_user_gesture) {
Arthur Sonzognia7d715a2018-09-20 16:11:134847 // Non-user initiated navigations coming from the renderer should be ignored
4848 // if there is an ongoing browser-initiated navigation.
4849 // See https://crbug.com/879965.
4850 // TODO(arthursonzogni): See if this should check for ongoing navigations in
4851 // the frame(s) affected by the session history navigation, rather than just
4852 // the main frame.
4853 if (!has_user_gesture) {
4854 NavigationRequest* ongoing_navigation_request =
4855 frame_tree_.root()->navigation_request();
4856 if (ongoing_navigation_request &&
4857 ongoing_navigation_request->browser_initiated()) {
4858 return;
4859 }
4860 }
4861
nicka0ac8382016-12-15 23:59:234862 // All frames are allowed to navigate the global history.
Dave Tapuska8bfd84c2019-03-26 20:47:164863 if (!delegate_ || delegate_->OnGoToEntryOffset(offset)) {
Dave Tapuska043b94a2019-09-10 14:14:354864 if (source->IsSandboxed(blink::WebSandboxFlags::kTopNavigation)) {
Dave Tapuska8bfd84c2019-03-26 20:47:164865 // Keep track of whether this is a session history from a sandboxed iframe
4866 // with top level navigation disallowed.
4867 controller_.GoToOffsetInSandboxedFrame(offset,
4868 source->GetFrameTreeNodeId());
4869 } else {
4870 controller_.GoToOffset(offset);
4871 }
4872 }
[email protected]216813952011-05-19 22:21:264873}
4874
nicka0ac8382016-12-15 23:59:234875void WebContentsImpl::OnPageScaleFactorChanged(RenderViewHostImpl* source,
4876 float page_scale_factor) {
mcnee336ea2c2017-05-23 22:50:594877#if !defined(OS_ANDROID)
4878 // While page scale factor is used on mobile, this PageScaleFactorIsOne logic
4879 // is only needed on desktop.
mcnee432e47d2015-11-09 19:37:464880 bool is_one = page_scale_factor == 1.f;
4881 if (is_one != page_scale_factor_is_one_) {
4882 page_scale_factor_is_one_ = is_one;
4883
4884 HostZoomMapImpl* host_zoom_map =
4885 static_cast<HostZoomMapImpl*>(HostZoomMap::GetForWebContents(this));
4886
nicka0ac8382016-12-15 23:59:234887 if (host_zoom_map) {
mcnee432e47d2015-11-09 19:37:464888 host_zoom_map->SetPageScaleFactorIsOneForView(
nicka0ac8382016-12-15 23:59:234889 source->GetProcess()->GetID(), source->GetRoutingID(),
mcnee432e47d2015-11-09 19:37:464890 page_scale_factor_is_one_);
4891 }
4892 }
mcnee336ea2c2017-05-23 22:50:594893#endif // !defined(OS_ANDROID)
mcnee432e47d2015-11-09 19:37:464894
ericwilligersde386342016-10-19 02:35:094895 for (auto& observer : observers_)
4896 observer.OnPageScaleFactorChanged(page_scale_factor);
mcnee432e47d2015-11-09 19:37:464897}
4898
W. James MacLean62198672019-06-04 16:46:104899void WebContentsImpl::OnTextAutosizerPageInfoChanged(
4900 RenderViewHostImpl* source,
4901 const blink::WebTextAutosizerPageInfo& page_info) {
4902 // Keep a copy of |page_info| in case we create a new RenderView before
4903 // the next update.
4904 text_autosizer_page_info_ = page_info;
4905 frame_tree_.root()->render_manager()->SendPageMessage(
4906 new PageMsg_UpdateTextAutosizerPageInfoForRemoteMainFrames(
4907 MSG_ROUTING_NONE, page_info),
4908 source->GetSiteInstance());
4909}
4910
Kent Tamura8c9e0562018-11-06 07:02:194911void WebContentsImpl::EnumerateDirectory(
4912 RenderFrameHost* render_frame_host,
4913 std::unique_ptr<FileSelectListener> listener,
4914 const base::FilePath& directory_path) {
4915 if (delegate_)
4916 delegate_->EnumerateDirectory(this, std::move(listener), directory_path);
4917 else
4918 listener->FileSelectionCanceled();
[email protected]3a29a6e2011-08-24 18:26:214919}
4920
Dave Tapuskaf9fc3122019-10-18 19:42:554921void WebContentsImpl::RegisterProtocolHandler(RenderFrameHostImpl* source,
4922 const std::string& protocol,
4923 const GURL& url,
4924 const base::string16& title,
4925 bool user_gesture) {
nicka0ac8382016-12-15 23:59:234926 // TODO(nick): Should we consider |source| here or pass it to the delegate?
4927 // TODO(nick): Do we need to apply FilterURL to |url|?
[email protected]e5f2de02012-07-20 22:15:434928 if (!delegate_)
4929 return;
4930
Raymes Khoury58373df2019-08-06 06:29:204931 if (!AreValidRegisterProtocolHandlerArguments(
4932 protocol, url, source->GetLastCommittedOrigin())) {
4933 ReceivedBadMessage(source->GetProcess(),
4934 bad_message::REGISTER_PROTOCOL_HANDLER_INVALID_URL);
[email protected]b9535422012-02-09 01:47:594935 return;
Raymes Khoury58373df2019-08-06 06:29:204936 }
[email protected]8f810632013-06-06 22:33:324937
[email protected]c125e532014-05-10 20:39:064938 delegate_->RegisterProtocolHandler(this, protocol, url, user_gesture);
[email protected]7d189022011-08-25 22:54:204939}
4940
Dave Tapuskaf9fc3122019-10-18 19:42:554941void WebContentsImpl::UnregisterProtocolHandler(RenderFrameHostImpl* source,
4942 const std::string& protocol,
4943 const GURL& url,
4944 bool user_gesture) {
nicka0ac8382016-12-15 23:59:234945 // TODO(nick): Should we consider |source| here or pass it to the delegate?
4946 // TODO(nick): Do we need to apply FilterURL to |url|?
[email protected]f5273e52014-07-14 16:30:204947 if (!delegate_)
4948 return;
4949
Raymes Khoury58373df2019-08-06 06:29:204950 if (!AreValidRegisterProtocolHandlerArguments(
4951 protocol, url, source->GetLastCommittedOrigin())) {
4952 ReceivedBadMessage(source->GetProcess(),
4953 bad_message::REGISTER_PROTOCOL_HANDLER_INVALID_URL);
[email protected]f5273e52014-07-14 16:30:204954 return;
Raymes Khoury58373df2019-08-06 06:29:204955 }
[email protected]f5273e52014-07-14 16:30:204956
4957 delegate_->UnregisterProtocolHandler(this, protocol, url, user_gesture);
4958}
4959
kouhei40f03cb2015-09-24 07:47:014960void WebContentsImpl::OnUpdatePageImportanceSignals(
nicka0ac8382016-12-15 23:59:234961 RenderFrameHostImpl* source,
kouhei40f03cb2015-09-24 07:47:014962 const PageImportanceSignals& signals) {
nicka0ac8382016-12-15 23:59:234963 // TODO(nick, kouhei): Fix this for oopifs; currently all frames' state gets
4964 // written to this one field.
kouhei40f03cb2015-09-24 07:47:014965 page_importance_signals_ = signals;
4966}
4967
nicka0ac8382016-12-15 23:59:234968void WebContentsImpl::OnDomOperationResponse(RenderFrameHostImpl* source,
4969 const std::string& json_string) {
4970 // TODO(nick, lukasza): The notification below should probably be updated to
4971 // include |source|.
avi60bd4902015-09-23 20:39:244972 std::string json = json_string;
4973 NotificationService::current()->Notify(NOTIFICATION_DOM_OPERATION_RESPONSE,
4974 Source<WebContents>(this),
4975 Details<std::string>(&json));
[email protected]cfa856d62014-02-22 07:58:404976}
4977
Marijn Kruisselbrink7f519672019-02-05 19:12:504978void WebContentsImpl::OnAppCacheAccessed(const GURL& manifest_url,
[email protected]b172aee2012-04-10 17:05:264979 bool blocked_by_policy) {
nicka0ac8382016-12-15 23:59:234980 // TODO(nick): Should we consider |source| here? Should we call FilterURL on
4981 // |manifest_url|?
4982
[email protected]7fc4bbb2011-09-08 21:23:104983 // Notify observers about navigation.
ericwilligersde386342016-10-19 02:35:094984 for (auto& observer : observers_)
4985 observer.AppCacheAccessed(manifest_url, blocked_by_policy);
[email protected]7fc4bbb2011-09-08 21:23:104986}
4987
Miyoung Shincb6475a2019-11-09 10:49:454988void WebContentsImpl::OnColorChooserFactoryReceiver(
Julie Jeongeun Kim8e2879e2019-08-06 23:55:204989 mojo::PendingReceiver<blink::mojom::ColorChooserFactory> receiver) {
4990 color_chooser_factory_receivers_.Add(this, std::move(receiver));
Joel Hockey85b379d2017-12-11 10:42:134991}
4992
4993void WebContentsImpl::OpenColorChooser(
Julie Jeongeun Kim8e2879e2019-08-06 23:55:204994 mojo::PendingReceiver<blink::mojom::ColorChooser> chooser_receiver,
4995 mojo::PendingRemote<blink::mojom::ColorChooserClient> client,
[email protected]8ed16472014-04-11 19:02:484996 SkColor color,
Joel Hockey163835a2017-12-20 11:51:574997 std::vector<blink::mojom::ColorSuggestionPtr> suggestions) {
Joel Hockey85b379d2017-12-11 10:42:134998 content::ColorChooser* new_color_chooser =
Ivan Kotenkov2c0d2bb32017-11-01 15:41:284999 delegate_ ? delegate_->OpenColorChooser(this, color, suggestions)
5000 : nullptr;
[email protected]820957a2014-01-14 14:56:305001 if (!new_color_chooser)
[email protected]cb805452013-05-22 15:16:215002 return;
[email protected]8ed16472014-04-11 19:02:485003
Joel Hockey85b379d2017-12-11 10:42:135004 color_chooser_.reset();
5005 color_chooser_ = std::make_unique<ColorChooser>(
Julie Jeongeun Kim8e2879e2019-08-06 23:55:205006 new_color_chooser, std::move(chooser_receiver), std::move(client));
[email protected]da8543762012-03-20 08:52:205007}
5008
brettw4b461082016-11-19 18:55:165009#if BUILDFLAG(ENABLE_PLUGINS)
nicka0ac8382016-12-15 23:59:235010void WebContentsImpl::OnPepperInstanceCreated(RenderFrameHostImpl* source,
5011 int32_t pp_instance) {
ericwilligersde386342016-10-19 02:35:095012 for (auto& observer : observers_)
5013 observer.PepperInstanceCreated();
nicka0ac8382016-12-15 23:59:235014 pepper_playback_observer_->PepperInstanceCreated(source, pp_instance);
emaxxe70f5e12015-05-29 11:26:005015}
5016
nicka0ac8382016-12-15 23:59:235017void WebContentsImpl::OnPepperInstanceDeleted(RenderFrameHostImpl* source,
5018 int32_t pp_instance) {
ericwilligersde386342016-10-19 02:35:095019 for (auto& observer : observers_)
5020 observer.PepperInstanceDeleted();
nicka0ac8382016-12-15 23:59:235021 pepper_playback_observer_->PepperInstanceDeleted(source, pp_instance);
emaxxe70f5e12015-05-29 11:26:005022}
5023
nicka0ac8382016-12-15 23:59:235024void WebContentsImpl::OnPepperPluginHung(RenderFrameHostImpl* source,
5025 int plugin_child_id,
tommyclie6633ca72014-10-31 00:40:425026 const base::FilePath& path,
5027 bool is_hung) {
Steven Holte95922222018-09-14 20:06:235028 UMA_HISTOGRAM_COUNTS_1M("Pepper.PluginHung", 1);
tommyclie6633ca72014-10-31 00:40:425029
ericwilligersde386342016-10-19 02:35:095030 for (auto& observer : observers_)
5031 observer.PluginHungStatusChanged(plugin_child_id, path, is_hung);
tommyclie6633ca72014-10-31 00:40:425032}
5033
nicka0ac8382016-12-15 23:59:235034void WebContentsImpl::OnPepperStartsPlayback(RenderFrameHostImpl* source,
5035 int32_t pp_instance) {
5036 pepper_playback_observer_->PepperStartsPlayback(source, pp_instance);
zqzhang181047e62016-07-01 13:37:175037}
5038
nicka0ac8382016-12-15 23:59:235039void WebContentsImpl::OnPepperStopsPlayback(RenderFrameHostImpl* source,
5040 int32_t pp_instance) {
5041 pepper_playback_observer_->PepperStopsPlayback(source, pp_instance);
zqzhang181047e62016-07-01 13:37:175042}
5043
nicka0ac8382016-12-15 23:59:235044void WebContentsImpl::OnPluginCrashed(RenderFrameHostImpl* source,
5045 const base::FilePath& plugin_path,
tommyclie6633ca72014-10-31 00:40:425046 base::ProcessId plugin_pid) {
nicka0ac8382016-12-15 23:59:235047 // TODO(nick): Eliminate the |plugin_pid| parameter, which can't be trusted,
5048 // and is only used by BlinkTestController.
ericwilligersde386342016-10-19 02:35:095049 for (auto& observer : observers_)
5050 observer.PluginCrashed(plugin_path, plugin_pid);
tommyclie6633ca72014-10-31 00:40:425051}
5052
[email protected]d8415ad92012-08-23 14:40:505053void WebContentsImpl::OnRequestPpapiBrokerPermission(
nicka0ac8382016-12-15 23:59:235054 RenderViewHostImpl* source,
5055 int ppb_broker_route_id,
[email protected]d8415ad92012-08-23 14:40:505056 const GURL& url,
[email protected]2dec8ec2013-02-07 19:20:345057 const base::FilePath& plugin_path) {
Avi Drissman0a2a967d2019-04-01 19:33:525058 base::OnceCallback<void(bool)> permission_result_callback = base::BindOnce(
nicka0ac8382016-12-15 23:59:235059 &WebContentsImpl::SendPpapiBrokerPermissionResult, base::Unretained(this),
5060 source->GetProcess()->GetID(), ppb_broker_route_id);
[email protected]8c8fc292012-11-23 18:57:165061 if (!delegate_) {
Tommy Nyquist4b749d02018-03-20 21:46:295062 std::move(permission_result_callback).Run(false);
[email protected]8c8fc292012-11-23 18:57:165063 return;
[email protected]d8415ad92012-08-23 14:40:505064 }
5065
Avi Drissman0a2a967d2019-04-01 19:33:525066 delegate_->RequestPpapiBrokerPermission(
5067 this, url, plugin_path, std::move(permission_result_callback));
[email protected]d8415ad92012-08-23 14:40:505068}
5069
nicka0ac8382016-12-15 23:59:235070void WebContentsImpl::SendPpapiBrokerPermissionResult(int process_id,
5071 int ppb_broker_route_id,
5072 bool result) {
5073 RenderProcessHost* rph = RenderProcessHost::FromID(process_id);
5074 if (rph) {
5075 // TODO(nick): Convert this from ViewMsg_ to a Ppapi msg, since it
5076 // is not routed to a RenderView.
5077 rph->Send(
5078 new ViewMsg_PpapiBrokerPermissionResult(ppb_broker_route_id, result));
5079 }
[email protected]d8415ad92012-08-23 14:40:505080}
brettw4b461082016-11-19 18:55:165081#endif // BUILDFLAG(ENABLE_PLUGINS)
[email protected]19be7a62012-10-01 23:03:375082
[email protected]795c28972012-12-06 06:13:395083void WebContentsImpl::OnUpdateFaviconURL(
lukasza178ff092017-06-07 23:39:505084 RenderFrameHostImpl* source,
[email protected]795c28972012-12-06 06:13:395085 const std::vector<FaviconURL>& candidates) {
lukasza178ff092017-06-07 23:39:505086 // Ignore favicons for non-main frame.
5087 if (source->GetParent()) {
5088 NOTREACHED();
5089 return;
5090 }
5091
pkotwiczf9bc20592015-04-21 18:57:065092 // We get updated favicon URLs after the page stops loading. If a cross-site
5093 // navigation occurs while a page is still loading, the initial page
5094 // may stop loading and send us updated favicon URLs after the navigation
5095 // for the new page has committed.
lukasza178ff092017-06-07 23:39:505096 if (!source->IsCurrent())
pkotwiczf9bc20592015-04-21 18:57:065097 return;
5098
ericwilligersde386342016-10-19 02:35:095099 for (auto& observer : observers_)
5100 observer.DidUpdateFaviconURL(candidates);
[email protected]795c28972012-12-06 06:13:395101}
5102
qinmin72e8bd02016-10-21 19:35:375103void WebContentsImpl::SetIsOverlayContent(bool is_overlay_content) {
5104 is_overlay_content_ = is_overlay_content;
5105}
5106
Albert J. Wongfb64e142018-10-16 01:10:455107void WebContentsImpl::DidFirstVisuallyNonEmptyPaint(
5108 RenderViewHostImpl* source) {
nicka0ac8382016-12-15 23:59:235109 // TODO(nick): When this is ported to FrameHostMsg_, we should only listen if
5110 // |source| is the main frame.
ericwilligersde386342016-10-19 02:35:095111 for (auto& observer : observers_)
5112 observer.DidFirstVisuallyNonEmptyPaint();
yusufod41c5f92015-03-06 00:14:285113
Carlos Caballero1215f882019-10-29 15:58:435114 if (source->theme_color() != last_sent_theme_color_) {
yusufod41c5f92015-03-06 00:14:285115 // Theme color should have updated by now if there was one.
ericwilligersde386342016-10-19 02:35:095116 for (auto& observer : observers_)
Carlos Caballero1215f882019-10-29 15:58:435117 observer.DidChangeThemeColor(source->theme_color());
5118 last_sent_theme_color_ = source->theme_color();
yusufod41c5f92015-03-06 00:14:285119 }
[email protected]9f268072013-11-07 00:02:155120}
[email protected]d9030b82013-07-19 08:26:065121
Adithya Srinivasan46b8a792019-02-01 14:47:235122bool WebContentsImpl::IsPortal() const {
5123 return portal();
5124}
5125
[email protected]17e286e2013-03-01 23:29:395126void WebContentsImpl::NotifyBeforeFormRepostWarningShow() {
ericwilligersde386342016-10-19 02:35:095127 for (auto& observer : observers_)
5128 observer.BeforeFormRepostWarningShow();
[email protected]17e286e2013-03-01 23:29:395129}
5130
[email protected]ec6c05f2013-10-23 18:41:575131void WebContentsImpl::ActivateAndShowRepostFormWarningDialog() {
5132 Activate();
5133 if (delegate_)
5134 delegate_->ShowRepostFormWarningDialog(this);
5135}
5136
[email protected]b4c84012014-04-28 19:51:105137bool WebContentsImpl::HasAccessedInitialDocument() {
5138 return has_accessed_initial_document_;
5139}
5140
afakhry6f0c1ec22016-07-14 13:55:135141void WebContentsImpl::UpdateTitleForEntry(NavigationEntry* entry,
[email protected]fcf75d42013-12-03 20:11:265142 const base::string16& title) {
[email protected]fcf75d42013-12-03 20:11:265143 base::string16 final_title;
Avi Drissman93002212017-09-27 03:20:525144 base::TrimWhitespace(title, base::TRIM_ALL, &final_title);
[email protected]420ae012009-04-24 05:16:325145
[email protected]987fc3a2011-05-26 14:18:095146 // If a page is created via window.open and never navigated,
5147 // there will be no navigation entry. In this situation,
[email protected]73eb2602012-02-09 19:50:555148 // |page_title_when_no_navigation_entry_| will be used for page title.
[email protected]987fc3a2011-05-26 14:18:095149 if (entry) {
[email protected]36fc0392011-12-25 03:59:515150 if (final_title == entry->GetTitle())
afakhry6f0c1ec22016-07-14 13:55:135151 return; // Nothing changed, don't bother.
[email protected]420ae012009-04-24 05:16:325152
[email protected]36fc0392011-12-25 03:59:515153 entry->SetTitle(final_title);
Avi Drissman93002212017-09-27 03:20:525154
5155 // The title for display may differ from the title just set; grab it.
5156 final_title = entry->GetTitleForDisplay();
[email protected]987fc3a2011-05-26 14:18:095157 } else {
5158 if (page_title_when_no_navigation_entry_ == final_title)
afakhry6f0c1ec22016-07-14 13:55:135159 return; // Nothing changed, don't bother.
[email protected]987fc3a2011-05-26 14:18:095160
5161 page_title_when_no_navigation_entry_ = final_title;
5162 }
[email protected]420ae012009-04-24 05:16:325163
[email protected]420ae012009-04-24 05:16:325164 // Lastly, set the title for the view.
[email protected]790e2fd2011-09-21 20:28:235165 view_->SetPageTitle(final_title);
[email protected]420ae012009-04-24 05:16:325166
ericwilligersde386342016-10-19 02:35:095167 for (auto& observer : observers_)
Avi Drissman93002212017-09-27 03:20:525168 observer.TitleWasSet(entry);
[email protected]66798902013-10-01 18:40:165169
afakhry6f0c1ec22016-07-14 13:55:135170 // Broadcast notifications when the UI should be updated.
5171 if (entry == controller_.GetEntryAtOffset(0))
5172 NotifyNavigationStateChanged(INVALIDATE_TYPE_TITLE);
[email protected]420ae012009-04-24 05:16:325173}
5174
fdegansa696e5112015-04-17 01:57:595175void WebContentsImpl::SendChangeLoadProgress() {
[email protected]960b0372014-05-19 18:01:005176 loading_last_progress_update_ = base::TimeTicks::Now();
Evan Stade9f328112019-10-23 20:49:255177 for (auto& observer : observers_)
5178 observer.LoadProgressChanged(frame_tree_.load_progress());
[email protected]960b0372014-05-19 18:01:005179}
5180
5181void WebContentsImpl::ResetLoadProgressState() {
fdegans1d16355162015-03-26 11:58:345182 frame_tree_.ResetLoadProgress();
[email protected]960b0372014-05-19 18:01:005183 loading_weak_factory_.InvalidateWeakPtrs();
5184 loading_last_progress_update_ = base::TimeTicks();
5185}
5186
clamy44e84ce2016-02-22 15:38:255187// Notifies the RenderWidgetHost instance about the fact that the page is
5188// loading, or done loading.
5189void WebContentsImpl::LoadingStateChanged(bool to_different_document,
5190 bool due_to_interstitial,
5191 LoadNotificationDetails* details) {
Nate Chapin22ea6592019-03-05 22:29:025192 if (is_being_destroyed_)
5193 return;
clamy44e84ce2016-02-22 15:38:255194 // Do not send notifications about loading changes in the FrameTree while the
5195 // interstitial page is pausing the throbber.
creisadca9862017-06-16 19:12:115196 if (ShowingInterstitialPage() && interstitial_page_->pause_throbber() &&
clamy44e84ce2016-02-22 15:38:255197 !due_to_interstitial) {
5198 return;
5199 }
5200
5201 bool is_loading = IsLoading();
5202
5203 if (!is_loading) {
5204 load_state_ = net::LoadStateWithParam(net::LOAD_STATE_IDLE,
5205 base::string16());
5206 load_state_host_.clear();
5207 upload_size_ = 0;
5208 upload_position_ = 0;
5209 }
5210
5211 GetRenderManager()->SetIsLoading(is_loading);
5212
5213 waiting_for_response_ = is_loading;
5214 is_load_to_different_document_ = to_different_document;
5215
5216 if (delegate_)
5217 delegate_->LoadingStateChanged(this, to_different_document);
5218 NotifyNavigationStateChanged(INVALIDATE_TYPE_LOAD);
5219
5220 std::string url = (details ? details->url.possibly_invalid_spec() : "NULL");
5221 if (is_loading) {
5222 TRACE_EVENT_ASYNC_BEGIN2("browser,navigation", "WebContentsImpl Loading",
5223 this, "URL", url, "Main FrameTreeNode id",
5224 GetFrameTree()->root()->frame_tree_node_id());
ericwilligersde386342016-10-19 02:35:095225 for (auto& observer : observers_)
5226 observer.DidStartLoading();
clamy44e84ce2016-02-22 15:38:255227 } else {
5228 TRACE_EVENT_ASYNC_END1("browser,navigation", "WebContentsImpl Loading",
5229 this, "URL", url);
ericwilligersde386342016-10-19 02:35:095230 for (auto& observer : observers_)
5231 observer.DidStopLoading();
clamy44e84ce2016-02-22 15:38:255232 }
5233
5234 // TODO(avi): Remove. http://crbug.com/170921
5235 int type = is_loading ? NOTIFICATION_LOAD_START : NOTIFICATION_LOAD_STOP;
5236 NotificationDetails det = NotificationService::NoDetails();
5237 if (details)
5238 det = Details<LoadNotificationDetails>(details);
5239 NotificationService::current()->Notify(
5240 type, Source<NavigationController>(&controller_), det);
5241}
5242
[email protected]02d7b6e2014-06-24 21:01:505243void WebContentsImpl::NotifyViewSwapped(RenderViewHost* old_host,
5244 RenderViewHost* new_host) {
[email protected]420ae012009-04-24 05:16:325245 // After sending out a swap notification, we need to send a disconnect
5246 // notification so that clients that pick up a pointer to |this| can NULL the
5247 // pointer. See Bug 1230284.
5248 notify_disconnection_ = true;
ericwilligersde386342016-10-19 02:35:095249 for (auto& observer : observers_)
5250 observer.RenderViewHostChanged(old_host, new_host);
[email protected]da7a7182013-09-06 08:11:115251
Jinsuk Kimdc5284c22018-07-11 02:22:065252 view_->RenderViewHostChanged(old_host, new_host);
5253
Lucas Furukawa Gadanif9ea4fc2018-08-03 23:57:135254 // If this is an inner WebContents that has swapped views, we need to reattach
5255 // it to its outer WebContents.
5256 if (node_.outer_web_contents())
5257 ReattachToOuterWebContentsFrame();
5258
[email protected]7a846df2012-09-20 19:17:395259 // Ensure that the associated embedder gets cleared after a RenderViewHost
5260 // gets swapped, so we don't reuse the same embedder next time a
5261 // RenderViewHost is attached to this WebContents.
5262 RemoveBrowserPluginEmbedder();
[email protected]420ae012009-04-24 05:16:325263}
5264
[email protected]02d7b6e2014-06-24 21:01:505265void WebContentsImpl::NotifyFrameSwapped(RenderFrameHost* old_host,
Jinsuk Kim895b2c52018-07-03 06:49:165266 RenderFrameHost* new_host,
5267 bool is_main_frame) {
Bo Liu168c8642017-08-28 18:26:025268#if defined(OS_ANDROID)
Bo Liu16dd7b8322018-05-02 21:19:505269 // Copy importance from |old_host| if |new_host| is a main frame.
5270 if (old_host && !new_host->GetParent()) {
Bo Liu7c6779e92017-08-16 02:02:285271 static_cast<RenderFrameHostImpl*>(new_host)
5272 ->GetRenderWidgetHost()
Bo Liu16dd7b8322018-05-02 21:19:505273 ->SetImportance(static_cast<RenderFrameHostImpl*>(old_host)
5274 ->GetRenderWidgetHost()
5275 ->importance());
Bo Liu7c6779e92017-08-16 02:02:285276 }
Bo Liu168c8642017-08-28 18:26:025277#endif
ericwilligersde386342016-10-19 02:35:095278 for (auto& observer : observers_)
5279 observer.RenderFrameHostChanged(old_host, new_host);
[email protected]02d7b6e2014-06-24 21:01:505280}
5281
[email protected]da7a7182013-09-06 08:11:115282// TODO(avi): Remove this entire function because this notification is already
5283// covered by two observer functions. http://crbug.com/170921
[email protected]b172aee2012-04-10 17:05:265284void WebContentsImpl::NotifyDisconnected() {
[email protected]420ae012009-04-24 05:16:325285 if (!notify_disconnection_)
5286 return;
5287
5288 notify_disconnection_ = false;
[email protected]8ff00d72012-10-23 19:12:215289 NotificationService::current()->Notify(
5290 NOTIFICATION_WEB_CONTENTS_DISCONNECTED,
5291 Source<WebContents>(this),
5292 NotificationService::NoDetails());
[email protected]420ae012009-04-24 05:16:325293}
5294
[email protected]cbb1ef592013-06-05 19:49:465295void WebContentsImpl::NotifyNavigationEntryCommitted(
5296 const LoadCommittedDetails& load_details) {
ericwilligersde386342016-10-19 02:35:095297 for (auto& observer : observers_)
5298 observer.NavigationEntryCommitted(load_details);
[email protected]cbb1ef592013-06-05 19:49:465299}
5300
Sam McNally5c087a32017-08-25 01:46:145301void WebContentsImpl::NotifyNavigationEntryChanged(
5302 const EntryChangedDetails& change_details) {
5303 for (auto& observer : observers_)
5304 observer.NavigationEntryChanged(change_details);
5305}
5306
5307void WebContentsImpl::NotifyNavigationListPruned(
5308 const PrunedDetails& pruned_details) {
5309 for (auto& observer : observers_)
5310 observer.NavigationListPruned(pruned_details);
5311}
5312
Christian Dullweber1af31e62018-02-22 11:49:485313void WebContentsImpl::NotifyNavigationEntriesDeleted() {
5314 for (auto& observer : observers_)
5315 observer.NavigationEntriesDeleted();
5316}
5317
rockotf62002a2016-09-15 00:08:595318void WebContentsImpl::OnAssociatedInterfaceRequest(
5319 RenderFrameHost* render_frame_host,
5320 const std::string& interface_name,
5321 mojo::ScopedInterfaceEndpointHandle handle) {
Julie Jeongeun Kim7d0986172019-12-04 03:17:255322 auto it = receiver_sets_.find(interface_name);
5323 if (it != receiver_sets_.end())
5324 it->second->OnReceiverForFrame(render_frame_host, std::move(handle));
rockotf62002a2016-09-15 00:08:595325}
5326
Ben Goodger9e24929e2017-07-21 03:13:405327void WebContentsImpl::OnInterfaceRequest(
5328 RenderFrameHost* render_frame_host,
5329 const std::string& interface_name,
5330 mojo::ScopedMessagePipeHandle* interface_pipe) {
5331 for (auto& observer : observers_) {
5332 observer.OnInterfaceRequestFromFrame(render_frame_host, interface_name,
5333 interface_pipe);
5334 if (!interface_pipe->is_valid())
5335 break;
5336 }
5337}
5338
Varun Khaneja5da94302019-06-25 04:48:105339void WebContentsImpl::OnDidBlockNavigation(
5340 const GURL& blocked_url,
5341 const GURL& initiator_url,
5342 blink::NavigationBlockedReason reason) {
Nicolas Dossou-gbetebcab2472017-09-09 12:28:405343 if (delegate_)
Varun Khaneja5da94302019-06-25 04:48:105344 delegate_->OnDidBlockNavigation(this, blocked_url, initiator_url, reason);
Nicolas Dossou-gbetebcab2472017-09-09 12:28:405345}
5346
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:415347const GURL& WebContentsImpl::GetMainFrameLastCommittedURL() {
[email protected]c31a84802014-04-03 15:55:495348 return GetLastCommittedURL();
5349}
5350
[email protected]b849847b2013-12-10 21:57:585351void WebContentsImpl::RenderFrameCreated(RenderFrameHost* render_frame_host) {
ericwilligersde386342016-10-19 02:35:095352 for (auto& observer : observers_)
5353 observer.RenderFrameCreated(render_frame_host);
dmazzonif8a31042016-12-03 00:52:135354 UpdateAccessibilityModeOnFrame(render_frame_host);
clamy91b7abf2016-03-24 15:17:455355
Becca Hughesd11d6502018-07-31 17:01:285356 if (display_cutout_host_impl_)
5357 display_cutout_host_impl_->RenderFrameCreated(render_frame_host);
5358
clamy91b7abf2016-03-24 15:17:455359 if (!render_frame_host->IsRenderFrameLive() || render_frame_host->GetParent())
5360 return;
5361
5362 NavigationEntry* entry = controller_.GetPendingEntry();
5363 if (entry && entry->IsViewSourceMode()) {
5364 // Put the renderer in view source mode.
Dave Tapuska3c05e1c2019-11-27 18:34:185365 static_cast<RenderFrameHostImpl*>(render_frame_host)
5366 ->GetAssociatedLocalFrame()
5367 ->EnableViewSourceMode();
clamy91b7abf2016-03-24 15:17:455368 }
[email protected]b849847b2013-12-10 21:57:585369}
5370
5371void WebContentsImpl::RenderFrameDeleted(RenderFrameHost* render_frame_host) {
yilkal172da772019-11-06 22:49:075372 if (IsBeingDestroyed() && !render_frame_host->GetParent() &&
5373 first_navigation_completed_ &&
Rakina Zata Amni74627df2019-10-15 02:03:175374 !static_cast<RenderFrameHostImpl*>(render_frame_host)
5375 ->is_in_back_forward_cache()) {
yilkal796e89e2019-10-02 23:58:285376 // Main frame has been deleted because WebContents is being destroyed.
Rakina Zata Amni74627df2019-10-15 02:03:175377 // Note that we aren't recording this here when the main frame is in the
5378 // back-forward cache because that means we've actually already navigated
5379 // away from it (and we got to this point because the WebContents is
yilkal172da772019-11-06 22:49:075380 // deleted), which means |max_loaded_frame_count_| is already overwritten.
5381 // The |max_loaded_frame_count_| value will instead be recorded from within
5382 // |WebContentsImpl::DidFinishNavigation()|.
5383 RecordMaxFrameCountUMA(max_loaded_frame_count_);
yilkal796e89e2019-10-02 23:58:285384 }
5385
creis3638897452017-05-09 22:07:505386 is_notifying_observers_ = true;
ericwilligersde386342016-10-19 02:35:095387 for (auto& observer : observers_)
5388 observer.RenderFrameDeleted(render_frame_host);
creis3638897452017-05-09 22:07:505389 is_notifying_observers_ = false;
zqzhang7b6f4f02016-12-07 22:10:225390#if BUILDFLAG(ENABLE_PLUGINS)
5391 pepper_playback_observer_->RenderFrameDeleted(render_frame_host);
5392#endif
Becca Hughesfd5d8f892018-06-14 18:23:365393
Becca Hughesd11d6502018-07-31 17:01:285394 if (display_cutout_host_impl_)
5395 display_cutout_host_impl_->RenderFrameDeleted(render_frame_host);
5396
Becca Hughesfd5d8f892018-06-14 18:23:365397 // Remove any fullscreen state that the frame has stored.
5398 FullscreenStateChanged(render_frame_host, false /* is_fullscreen */);
[email protected]b849847b2013-12-10 21:57:585399}
5400
[email protected]a09d53ce2014-01-31 00:46:425401void WebContentsImpl::ShowContextMenu(RenderFrameHost* render_frame_host,
5402 const ContextMenuParams& params) {
avie9fd8722017-04-10 16:17:585403 // If a renderer fires off a second command to show a context menu before the
5404 // first context menu is closed, just ignore it. https://crbug.com/707534
ekaramadf6750aa2017-06-06 18:29:425405 if (showing_context_menu_)
avie9fd8722017-04-10 16:17:585406 return;
5407
[email protected]077e704b2014-05-23 19:24:105408 ContextMenuParams context_menu_params(params);
[email protected]a09d53ce2014-01-31 00:46:425409 // Allow WebContentsDelegates to handle the context menu operation first.
Joel Klinghed0ea22ea2019-02-20 22:04:225410 if (delegate_ &&
5411 delegate_->HandleContextMenu(render_frame_host, context_menu_params))
[email protected]a09d53ce2014-01-31 00:46:425412 return;
5413
[email protected]077e704b2014-05-23 19:24:105414 render_view_host_delegate_view_->ShowContextMenu(render_frame_host,
5415 context_menu_params);
[email protected]a09d53ce2014-01-31 00:46:425416}
5417
Avi Drissman4891b852018-09-25 18:12:585418namespace {
5419// Normalizes the line endings: \r\n -> \n, lone \r -> \n.
5420base::string16 NormalizeLineBreaks(const base::string16& source) {
5421 static const base::NoDestructor<base::string16> kReturnNewline(
5422 base::ASCIIToUTF16("\r\n"));
5423 static const base::NoDestructor<base::string16> kReturn(
5424 base::ASCIIToUTF16("\r"));
5425 static const base::NoDestructor<base::string16> kNewline(
5426 base::ASCIIToUTF16("\n"));
5427
5428 std::vector<base::StringPiece16> pieces;
5429
5430 for (const auto& rn_line : base::SplitStringPieceUsingSubstr(
5431 source, *kReturnNewline, base::KEEP_WHITESPACE,
5432 base::SPLIT_WANT_ALL)) {
5433 auto r_lines = base::SplitStringPieceUsingSubstr(
5434 rn_line, *kReturn, base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL);
5435 std::move(std::begin(r_lines), std::end(r_lines),
5436 std::back_inserter(pieces));
5437 }
5438
5439 return base::JoinString(pieces, *kNewline);
5440}
5441} // namespace
5442
avi777ff452017-02-09 19:04:485443void WebContentsImpl::RunJavaScriptDialog(RenderFrameHost* render_frame_host,
5444 const base::string16& message,
5445 const base::string16& default_prompt,
avi777ff452017-02-09 19:04:485446 JavaScriptDialogType dialog_type,
5447 IPC::Message* reply_msg) {
Avi Drissman2ccbb402018-03-20 21:11:365448 // Ensure that if showing a dialog is the first thing that a page does, that
5449 // the contents of the previous page aren't shown behind it. This is required
5450 // because showing a dialog freezes the renderer, so no frames will be coming
5451 // from it. https://crbug.com/823353
5452 auto* render_widget_host_impl =
5453 static_cast<RenderFrameHostImpl*>(render_frame_host)
5454 ->GetRenderWidgetHost();
5455 if (render_widget_host_impl)
5456 render_widget_host_impl->ForceFirstFrameAfterNavigationTimeout();
5457
avi0720b02e2017-06-13 03:22:135458 // Running a dialog causes an exit to webpage-initiated fullscreen.
5459 // http://crbug.com/728276
Avi Drissmand18c51972018-08-15 18:32:095460 ForSecurityDropFullscreen();
avi0720b02e2017-06-13 03:22:135461
Avi Drissmane04d3992017-10-05 15:11:365462 auto callback =
5463 base::BindOnce(&WebContentsImpl::OnDialogClosed, base::Unretained(this),
5464 render_frame_host->GetProcess()->GetID(),
5465 render_frame_host->GetRoutingID(), reply_msg);
Pavel Feldman3c1842b2017-08-02 05:00:165466
Pavel Feldman3c1842b2017-08-02 05:00:165467 std::vector<protocol::PageHandler*> page_handlers =
5468 protocol::PageHandler::EnabledForWebContents(this);
5469
Pavel Feldmanfab3cb62017-10-05 01:02:015470 if (delegate_)
5471 dialog_manager_ = delegate_->GetJavaScriptDialogManager(this);
5472
Charlie Reis96077362019-07-04 00:34:045473 // While a JS message dialog is showing, defer commits in this WebContents.
5474 javascript_dialog_navigation_deferrer_ =
5475 std::make_unique<JavaScriptDialogNavigationDeferrer>();
5476
Pavel Feldmanfab3cb62017-10-05 01:02:015477 // Suppress JavaScript dialogs when requested. Also suppress messages when
5478 // showing an interstitial as it's shown over the previous page and we don't
5479 // want the hidden page's dialogs to interfere with the interstitial.
5480 bool should_suppress = ShowingInterstitialPage() ||
5481 (delegate_ && delegate_->ShouldSuppressDialogs(this));
Pavel Feldman1c587bc52018-04-04 21:16:055482 bool has_non_devtools_handlers = delegate_ && dialog_manager_;
5483 bool has_handlers = page_handlers.size() || has_non_devtools_handlers;
Pavel Feldmanfab3cb62017-10-05 01:02:015484 bool suppress_this_message = should_suppress || !has_handlers;
5485
5486 if (suppress_this_message) {
Avi Drissmane04d3992017-10-05 15:11:365487 std::move(callback).Run(true, false, base::string16());
Pavel Feldman3c1842b2017-08-02 05:00:165488 return;
5489 }
5490
5491 scoped_refptr<CloseDialogCallbackWrapper> wrapper =
Avi Drissmane04d3992017-10-05 15:11:365492 new CloseDialogCallbackWrapper(std::move(callback));
Pavel Feldman3c1842b2017-08-02 05:00:165493
5494 is_showing_javascript_dialog_ = true;
5495
Avi Drissman4891b852018-09-25 18:12:585496 base::string16 normalized_message = NormalizeLineBreaks(message);
5497
Pavel Feldman3c1842b2017-08-02 05:00:165498 for (auto* handler : page_handlers) {
Avi Drissmane04d3992017-10-05 15:11:365499 handler->DidRunJavaScriptDialog(
Avi Drissman4891b852018-09-25 18:12:585500 render_frame_host->GetLastCommittedURL(), normalized_message,
5501 default_prompt, dialog_type, has_non_devtools_handlers,
Avi Drissmane04d3992017-10-05 15:11:365502 base::BindOnce(&CloseDialogCallbackWrapper::Run, wrapper, false));
Pavel Feldman3c1842b2017-08-02 05:00:165503 }
5504
5505 if (dialog_manager_) {
[email protected]87de04b02014-04-08 22:14:495506 dialog_manager_->RunJavaScriptDialog(
Avi Drissman4891b852018-09-25 18:12:585507 this, render_frame_host, dialog_type, normalized_message,
5508 default_prompt,
Avi Drissmane04d3992017-10-05 15:11:365509 base::BindOnce(&CloseDialogCallbackWrapper::Run, wrapper, false),
5510 &suppress_this_message);
[email protected]87de04b02014-04-08 22:14:495511 }
5512
5513 if (suppress_this_message) {
5514 // If we are suppressing messages, just reply as if the user immediately
5515 // pressed "Cancel", passing true to |dialog_was_suppressed|.
Avi Drissmane04d3992017-10-05 15:11:365516 wrapper->Run(true, false, base::string16());
[email protected]87de04b02014-04-08 22:14:495517 }
[email protected]87de04b02014-04-08 22:14:495518}
5519
5520void WebContentsImpl::RunBeforeUnloadConfirm(
[email protected]39f2f9252014-05-03 00:54:015521 RenderFrameHost* render_frame_host,
[email protected]87de04b02014-04-08 22:14:495522 bool is_reload,
5523 IPC::Message* reply_msg) {
Avi Drissman2ccbb402018-03-20 21:11:365524 // Ensure that if showing a dialog is the first thing that a page does, that
5525 // the contents of the previous page aren't shown behind it. This is required
5526 // because showing a dialog freezes the renderer, so no frames will be coming
5527 // from it. https://crbug.com/823353
5528 auto* render_widget_host_impl =
5529 static_cast<RenderFrameHostImpl*>(render_frame_host)
5530 ->GetRenderWidgetHost();
5531 if (render_widget_host_impl)
5532 render_widget_host_impl->ForceFirstFrameAfterNavigationTimeout();
5533
avi0720b02e2017-06-13 03:22:135534 // Running a dialog causes an exit to webpage-initiated fullscreen.
5535 // http://crbug.com/728276
Avi Drissmand18c51972018-08-15 18:32:095536 ForSecurityDropFullscreen();
avi0720b02e2017-06-13 03:22:135537
[email protected]39f2f9252014-05-03 00:54:015538 RenderFrameHostImpl* rfhi =
5539 static_cast<RenderFrameHostImpl*>(render_frame_host);
[email protected]87de04b02014-04-08 22:14:495540 if (delegate_)
5541 delegate_->WillRunBeforeUnloadConfirm();
5542
Avi Drissmane04d3992017-10-05 15:11:365543 auto callback =
5544 base::BindOnce(&WebContentsImpl::OnDialogClosed, base::Unretained(this),
5545 render_frame_host->GetProcess()->GetID(),
5546 render_frame_host->GetRoutingID(), reply_msg);
Pavel Feldman3c1842b2017-08-02 05:00:165547
Pavel Feldman3c1842b2017-08-02 05:00:165548 std::vector<protocol::PageHandler*> page_handlers =
5549 protocol::PageHandler::EnabledForWebContents(this);
5550
Pavel Feldmanfab3cb62017-10-05 01:02:015551 if (delegate_)
5552 dialog_manager_ = delegate_->GetJavaScriptDialogManager(this);
5553
Charlie Reis96077362019-07-04 00:34:045554 // While a JS beforeunload dialog is showing, defer commits in this
5555 // WebContents.
5556 javascript_dialog_navigation_deferrer_ =
5557 std::make_unique<JavaScriptDialogNavigationDeferrer>();
5558
Pavel Feldmanfab3cb62017-10-05 01:02:015559 bool should_suppress = ShowingInterstitialPage() || !rfhi->is_active() ||
5560 (delegate_ && delegate_->ShouldSuppressDialogs(this));
Pavel Feldman1c587bc52018-04-04 21:16:055561 bool has_non_devtools_handlers = delegate_ && dialog_manager_;
5562 bool has_handlers = page_handlers.size() || has_non_devtools_handlers;
Pavel Feldmanfab3cb62017-10-05 01:02:015563 if (should_suppress || !has_handlers) {
Avi Drissmane04d3992017-10-05 15:11:365564 std::move(callback).Run(false, true, base::string16());
[email protected]87de04b02014-04-08 22:14:495565 return;
5566 }
5567
5568 is_showing_before_unload_dialog_ = true;
Pavel Feldman3c1842b2017-08-02 05:00:165569
5570 scoped_refptr<CloseDialogCallbackWrapper> wrapper =
Avi Drissmane04d3992017-10-05 15:11:365571 new CloseDialogCallbackWrapper(std::move(callback));
Pavel Feldman3c1842b2017-08-02 05:00:165572
5573 GURL frame_url = rfhi->GetLastCommittedURL();
5574 for (auto* handler : page_handlers) {
Avi Drissmane04d3992017-10-05 15:11:365575 handler->DidRunBeforeUnloadConfirm(
Pavel Feldman1c587bc52018-04-04 21:16:055576 frame_url, has_non_devtools_handlers,
Avi Drissmane04d3992017-10-05 15:11:365577 base::BindOnce(&CloseDialogCallbackWrapper::Run, wrapper, false));
Pavel Feldman3c1842b2017-08-02 05:00:165578 }
5579
5580 if (dialog_manager_) {
Avi Drissmane04d3992017-10-05 15:11:365581 dialog_manager_->RunBeforeUnloadDialog(
Avi Drissmanff9ed752017-11-28 21:18:125582 this, render_frame_host, is_reload,
Avi Drissmane04d3992017-10-05 15:11:365583 base::BindOnce(&CloseDialogCallbackWrapper::Run, wrapper, false));
Pavel Feldman3c1842b2017-08-02 05:00:165584 }
[email protected]87de04b02014-04-08 22:14:495585}
5586
Kent Tamurac260c9a2018-09-14 09:10:565587void WebContentsImpl::RunFileChooser(
5588 RenderFrameHost* render_frame_host,
Kent Tamura512a27e2018-10-04 00:49:325589 std::unique_ptr<content::FileSelectListener> listener,
Kent Tamurac260c9a2018-09-14 09:10:565590 const blink::mojom::FileChooserParams& params) {
Avi Drissman3d41e7712018-08-27 21:18:085591 // Any explicit focusing of another window while this WebContents is in
5592 // fullscreen can be used to confuse the user, so drop fullscreen.
5593 ForSecurityDropFullscreen();
5594
naskoada75b22016-06-11 16:09:465595 if (delegate_)
Kent Tamura512a27e2018-10-04 00:49:325596 delegate_->RunFileChooser(render_frame_host, std::move(listener), params);
5597 else
5598 listener->FileSelectionCanceled();
naskoada75b22016-06-11 16:09:465599}
5600
[email protected]a86c0e962013-12-17 17:10:395601WebContents* WebContentsImpl::GetAsWebContents() {
5602 return this;
5603}
5604
mcnee336ea2c2017-05-23 22:50:595605#if !defined(OS_ANDROID)
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:415606double WebContentsImpl::GetPendingPageZoomLevel() {
wjmaclean64951902016-04-29 20:59:125607 NavigationEntry* pending_entry = GetController().GetPendingEntry();
5608 if (!pending_entry)
5609 return HostZoomMap::GetZoomLevel(this);
5610
5611 GURL url = pending_entry->GetURL();
5612 return HostZoomMap::GetForWebContents(this)->GetZoomLevelForHostAndScheme(
5613 url.scheme(), net::GetHostOrSpecFromURL(url));
5614}
mcnee336ea2c2017-05-23 22:50:595615#endif // !defined(OS_ANDROID)
wjmaclean64951902016-04-29 20:59:125616
qinmin72e8bd02016-10-21 19:35:375617bool WebContentsImpl::HideDownloadUI() const {
5618 return is_overlay_content_;
5619}
5620
mlamouri5cd9ae82017-02-18 11:05:095621bool WebContentsImpl::HasPersistentVideo() const {
5622 return has_persistent_video_;
5623}
5624
Ian Vollick5d5c37f2019-06-19 22:50:575625bool WebContentsImpl::IsSpatialNavigationDisabled() const {
5626 return is_spatial_navigation_disabled_;
5627}
5628
Albert J. Wong65fe64d2019-09-20 02:48:145629RenderFrameHostImpl* WebContentsImpl::GetPendingMainFrame() {
clamy610c63b32017-12-22 15:05:185630 return GetRenderManager()->speculative_frame_host();
Alex Moshchuk27caae82017-09-11 23:11:185631}
5632
mlamourie52c1b542017-02-23 14:57:505633bool WebContentsImpl::HasActiveEffectivelyFullscreenVideo() const {
5634 return media_web_contents_observer_->HasActiveEffectivelyFullscreenVideo();
5635}
5636
François Beaufortad6c5232018-02-26 11:00:445637bool WebContentsImpl::IsPictureInPictureAllowedForFullscreenVideo() const {
5638 return media_web_contents_observer_
5639 ->IsPictureInPictureAllowedForFullscreenVideo();
5640}
5641
ekaramada110f642016-12-21 19:47:285642bool WebContentsImpl::IsFocusedElementEditable() {
5643 RenderFrameHostImpl* frame = GetFocusedFrame();
5644 return frame && frame->has_focused_editable_element();
5645}
5646
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:415647bool WebContentsImpl::IsShowingContextMenu() {
ekaramadf6750aa2017-06-06 18:29:425648 return showing_context_menu_;
5649}
5650
5651void WebContentsImpl::SetShowingContextMenu(bool showing) {
5652 DCHECK_NE(showing_context_menu_, showing);
5653 showing_context_menu_ = showing;
5654
5655 if (auto* view = GetRenderWidgetHostView()) {
5656 // Notify the main frame's RWHV to run the platform-specific code, if any.
5657 static_cast<RenderWidgetHostViewBase*>(view)->SetShowingContextMenu(
5658 showing);
5659 }
5660}
5661
ekaramada110f642016-12-21 19:47:285662void WebContentsImpl::ClearFocusedElement() {
5663 if (auto* frame = GetFocusedFrame())
5664 frame->ClearFocusedElement();
5665}
5666
[email protected]7912e822014-04-16 02:37:035667bool WebContentsImpl::IsNeverVisible() {
5668 if (!delegate_)
5669 return false;
5670 return delegate_->IsNeverVisible(this);
5671}
5672
[email protected]5a3bdf52012-05-24 15:12:575673RenderViewHostDelegateView* WebContentsImpl::GetDelegateView() {
5674 return render_view_host_delegate_view_;
[email protected]420ae012009-04-24 05:16:325675}
5676
Leon Hanc819dc62019-01-28 04:30:195677blink::mojom::RendererPreferences WebContentsImpl::GetRendererPrefs(
[email protected]8ff00d72012-10-23 19:12:215678 BrowserContext* browser_context) const {
[email protected]840b1512009-07-21 16:53:465679 return renderer_preferences_;
[email protected]80d96fa2009-06-10 22:34:515680}
5681
Lucas Furukawa Gadani2ec00c82018-12-14 15:53:165682RenderFrameHostImpl* WebContentsImpl::GetOuterWebContentsFrame() {
5683 if (GetOuterDelegateFrameTreeNodeId() ==
5684 FrameTreeNode::kFrameTreeNodeInvalidId) {
5685 return nullptr;
5686 }
5687
5688 FrameTreeNode* outer_node =
5689 FrameTreeNode::GloballyFindByID(GetOuterDelegateFrameTreeNodeId());
5690 // The outer node should be in the outer WebContents.
5691 DCHECK_EQ(outer_node->frame_tree(), GetOuterWebContents()->GetFrameTree());
5692 return outer_node->parent()->current_frame_host();
5693}
5694
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:415695WebContentsImpl* WebContentsImpl::GetOuterWebContents() {
Lucas Furukawa Gadani105de1e812018-05-31 19:38:395696 return node_.outer_web_contents();
avallee0206f782016-07-28 18:55:335697}
5698
Lucas Furukawa Gadani2ec00c82018-12-14 15:53:165699std::vector<WebContents*> WebContentsImpl::GetInnerWebContents() {
5700 std::vector<WebContents*> all_inner_contents;
Lucas Furukawa Gadani2ec00c82018-12-14 15:53:165701 const auto& inner_contents = node_.GetInnerWebContents();
5702 all_inner_contents.insert(all_inner_contents.end(), inner_contents.begin(),
5703 inner_contents.end());
5704 return all_inner_contents;
5705}
5706
avallee0206f782016-07-28 18:55:335707WebContentsImpl* WebContentsImpl::GetFocusedWebContents() {
lfg601233692017-03-06 22:45:445708 return GetOutermostWebContents()->node_.focused_web_contents();
avallee0206f782016-07-28 18:55:335709}
5710
avallee9993fca2016-11-17 06:16:505711bool WebContentsImpl::ContainsOrIsFocusedWebContents() {
5712 for (WebContentsImpl* focused_contents = GetFocusedWebContents();
5713 focused_contents;
5714 focused_contents = focused_contents->GetOuterWebContents()) {
5715 if (focused_contents == this)
5716 return true;
5717 }
5718
5719 return false;
5720}
5721
W. James MacLean9437b5bf2019-12-06 17:33:445722void WebContentsImpl::RemoveBrowserPluginEmbedder() {
5723 browser_plugin_embedder_.reset();
5724}
5725
avallee0206f782016-07-28 18:55:335726WebContentsImpl* WebContentsImpl::GetOutermostWebContents() {
5727 WebContentsImpl* root = this;
5728 while (root->GetOuterWebContents())
5729 root = root->GetOuterWebContents();
5730 return root;
5731}
5732
avalleed68cdd12017-06-21 22:07:275733void WebContentsImpl::FocusOuterAttachmentFrameChain() {
5734 WebContentsImpl* outer_contents = GetOuterWebContents();
5735 if (!outer_contents)
5736 return;
5737
5738 FrameTreeNode* outer_node =
5739 FrameTreeNode::GloballyFindByID(GetOuterDelegateFrameTreeNodeId());
5740 outer_contents->frame_tree_.SetFocusedFrame(outer_node, nullptr);
5741
5742 // For a browser initiated focus change, let embedding renderer know of the
5743 // change. Otherwise, if the currently focused element is just across a
5744 // process boundary in focus order, it will not be possible to move across
5745 // that boundary. This is because the target element will already be focused
5746 // (that renderer was not notified) and drop the event.
5747 if (GetRenderManager()->GetProxyToOuterDelegate())
5748 GetRenderManager()->GetProxyToOuterDelegate()->SetFocusedFrame();
5749
5750 outer_contents->FocusOuterAttachmentFrameChain();
5751}
5752
Sean Toppingbbc4a2bd2019-01-30 02:16:145753void WebContentsImpl::InnerWebContentsCreated(WebContents* inner_web_contents) {
Shivani Sharma985474f2019-05-23 21:36:475754 had_inner_webcontents_ = true;
Sean Toppingbbc4a2bd2019-01-30 02:16:145755 for (auto& observer : observers_)
5756 observer.InnerWebContentsCreated(inner_web_contents);
5757}
5758
[email protected]b172aee2012-04-10 17:05:265759void WebContentsImpl::RenderViewCreated(RenderViewHost* render_view_host) {
[email protected]86f98a22013-03-20 14:35:005760 if (delegate_)
[email protected]e85165c642014-06-10 14:34:315761 view_->SetOverscrollControllerEnabled(CanOverscrollContent());
[email protected]a6b73c62013-02-11 23:05:085762
[email protected]8ff00d72012-10-23 19:12:215763 NotificationService::current()->Notify(
5764 NOTIFICATION_WEB_CONTENTS_RENDER_VIEW_HOST_CREATED,
5765 Source<WebContents>(this),
5766 Details<RenderViewHost>(render_view_host));
[email protected]420ae012009-04-24 05:16:325767
[email protected]60780f412013-02-25 16:34:105768 view_->RenderViewCreated(render_view_host);
[email protected]11f764bb2011-05-25 19:45:265769
ericwilligersde386342016-10-19 02:35:095770 for (auto& observer : observers_)
5771 observer.RenderViewCreated(render_view_host);
Joel Einbinder848001c2017-09-19 21:39:315772 if (delegate_)
5773 RenderFrameDevToolsAgentHost::WebContentsCreated(this);
[email protected]420ae012009-04-24 05:16:325774}
5775
[email protected]b172aee2012-04-10 17:05:265776void WebContentsImpl::RenderViewReady(RenderViewHost* rvh) {
[email protected]151a63d2011-12-20 22:32:525777 if (rvh != GetRenderViewHost()) {
[email protected]420ae012009-04-24 05:16:325778 // Don't notify the world, since this came from a renderer in the
5779 // background.
5780 return;
5781 }
5782
dmazzoni62111452017-04-28 23:46:555783 RenderWidgetHostViewBase* rwhv =
5784 static_cast<RenderWidgetHostViewBase*>(GetRenderWidgetHostView());
5785 if (rwhv)
5786 rwhv->SetMainFrameAXTreeID(GetMainFrame()->GetAXTreeID());
5787
[email protected]da7a7182013-09-06 08:11:115788 notify_disconnection_ = true;
[email protected]da7a7182013-09-06 08:11:115789
[email protected]be1f56ab2011-12-22 06:55:315790 bool was_crashed = IsCrashed();
[email protected]443b80e2010-12-14 00:42:235791 SetIsCrashed(base::TERMINATION_STATUS_STILL_RUNNING, 0);
[email protected]9d8a4642009-07-29 17:25:305792
5793 // Restore the focus to the tab (otherwise the focus will be on the top
5794 // window).
[email protected]484ae5912010-09-29 19:16:145795 if (was_crashed && !FocusLocationBarByDefault() &&
5796 (!delegate_ || delegate_->ShouldFocusPageAfterCrash())) {
[email protected]f3615f02013-02-26 06:09:065797 view_->Focus();
[email protected]484ae5912010-09-29 19:16:145798 }
[email protected]32ded2212011-11-10 18:51:435799
ericwilligersde386342016-10-19 02:35:095800 for (auto& observer : observers_)
5801 observer.RenderViewReady();
Jinsuk Kim895b2c52018-07-03 06:49:165802
5803 view_->RenderViewReady();
[email protected]420ae012009-04-24 05:16:325804}
5805
[email protected]ec6a7eb2013-04-22 17:34:225806void WebContentsImpl::RenderViewTerminated(RenderViewHost* rvh,
5807 base::TerminationStatus status,
5808 int error_code) {
[email protected]151a63d2011-12-20 22:32:525809 if (rvh != GetRenderViewHost()) {
[email protected]420ae012009-04-24 05:16:325810 // The pending page's RenderViewHost is gone.
5811 return;
5812 }
5813
[email protected]2fcdcc32014-03-05 02:14:075814 // Ensure fullscreen mode is exited in the |delegate_| since a crashed
5815 // renderer may not have made a clean exit.
alexmos5dd617d2016-06-07 04:21:155816 if (IsFullscreenForCurrentTab())
bokanece34a82016-01-28 19:49:465817 ExitFullscreenMode(false);
[email protected]2fcdcc32014-03-05 02:14:075818
Jennifer Apacibleb4b972a2018-06-07 04:41:025819 // Ensure any video in Picture-in-Picture is exited in the |delegate_| since
5820 // a crashed renderer may not have made a clean exit.
5821 if (HasPictureInPictureVideo())
5822 ExitPictureInPicture();
5823
[email protected]698191dc2014-02-25 01:06:135824 // Cancel any visible dialogs so they are not left dangling over the sad tab.
creis89a0f782015-05-27 16:13:175825 CancelActiveAndPendingDialogs();
[email protected]698191dc2014-02-25 01:06:135826
dalecurtis3d1e1302017-02-20 22:33:255827 audio_stream_monitor_.RenderProcessGone(rvh->GetProcess()->GetID());
5828
[email protected]960b0372014-05-19 18:01:005829 // Reset the loading progress. TODO(avi): What does it mean to have a
5830 // "renderer crash" when there is more than one renderer process serving a
5831 // webpage? Once this function is called at a more granular frame level, we
5832 // probably will need to more granularly reset the state here.
5833 ResetLoadProgressState();
clamy44e84ce2016-02-22 15:38:255834 NotifyDisconnected();
5835 SetIsCrashed(status, error_code);
[email protected]960b0372014-05-19 18:01:005836
ericwilligersde386342016-10-19 02:35:095837 for (auto& observer : observers_)
5838 observer.RenderProcessGone(GetCrashedStatus());
[email protected]420ae012009-04-24 05:16:325839}
5840
[email protected]b172aee2012-04-10 17:05:265841void WebContentsImpl::RenderViewDeleted(RenderViewHost* rvh) {
ericwilligersde386342016-10-19 02:35:095842 for (auto& observer : observers_)
5843 observer.RenderViewDeleted(rvh);
[email protected]2e4633c2009-07-09 16:58:065844}
5845
sky8b00392d2015-01-10 00:30:285846void WebContentsImpl::UpdateTargetURL(RenderViewHost* render_view_host,
5847 const GURL& url) {
5848 if (fullscreen_widget_routing_id_ != MSG_ROUTING_NONE) {
Lukasz Anforowiczfd707142018-02-07 19:46:135849 // If we're in flash fullscreen (i.e. Pepper plugin fullscreen) only update
5850 // the url if it's from the fullscreen renderer.
sky8b00392d2015-01-10 00:30:285851 RenderWidgetHostView* fs = GetFullscreenRenderWidgetHostView();
avif9ab5d942015-10-15 14:05:445852 if (fs && fs->GetRenderWidgetHost() != render_view_host->GetWidget())
sky8b00392d2015-01-10 00:30:285853 return;
5854 }
Lukasz Anforowiczfd707142018-02-07 19:46:135855
5856 // In case of racey updates from multiple RenderViewHosts, the last URL should
5857 // be shown - see also some discussion in https://crbug.com/807776.
5858 if (!url.is_valid() && render_view_host != view_that_set_last_target_url_)
5859 return;
5860 view_that_set_last_target_url_ = url.is_valid() ? render_view_host : nullptr;
5861
[email protected]6934a702011-12-20 00:04:515862 if (delegate_)
avi450ce00e2014-09-24 01:58:525863 delegate_->UpdateTargetURL(this, url);
[email protected]420ae012009-04-24 05:16:325864}
5865
Lukasz Anforowiczfd707142018-02-07 19:46:135866void WebContentsImpl::ClearTargetURL() {
5867 view_that_set_last_target_url_ = nullptr;
5868 if (delegate_)
5869 delegate_->UpdateTargetURL(this, GURL());
5870}
5871
[email protected]b172aee2012-04-10 17:05:265872void WebContentsImpl::Close(RenderViewHost* rvh) {
[email protected]60780f412013-02-25 16:34:105873#if defined(OS_MACOSX)
[email protected]07707302009-11-06 00:50:295874 // The UI may be in an event-tracking loop, such as between the
5875 // mouse-down and mouse-up in text selection or a button click.
5876 // Defer the close until after tracking is complete, so that we
5877 // don't free objects out from under the UI.
[email protected]07707302009-11-06 00:50:295878 // TODO(shess): This could get more fine-grained. For instance,
5879 // closing a tab in another window while selecting text in the
5880 // current window's Omnibox should be just fine.
Christopher Cameron9ae31e0d2019-03-01 20:01:195881 if (view_->CloseTabAfterEventTrackingIfNeeded())
[email protected]07707302009-11-06 00:50:295882 return;
[email protected]60780f412013-02-25 16:34:105883#endif
[email protected]07707302009-11-06 00:50:295884
[email protected]420ae012009-04-24 05:16:325885 // Ignore this if it comes from a RenderViewHost that we aren't showing.
[email protected]151a63d2011-12-20 22:32:525886 if (delegate_ && rvh == GetRenderViewHost())
[email protected]6934a702011-12-20 00:04:515887 delegate_->CloseContents(this);
[email protected]420ae012009-04-24 05:16:325888}
5889
Avi Drissman014dec72018-06-07 02:34:015890void WebContentsImpl::RequestSetBounds(const gfx::Rect& new_bounds) {
5891 if (delegate_)
5892 delegate_->SetContentsBounds(this, new_bounds);
[email protected]420ae012009-04-24 05:16:325893}
5894
fdegansa696e5112015-04-17 01:57:595895void WebContentsImpl::DidStartLoading(FrameTreeNode* frame_tree_node,
[email protected]e3b10d12014-03-28 16:06:095896 bool to_different_document) {
Evan Stadec3b26022017-07-14 19:50:245897 LoadingStateChanged(frame_tree_node->IsMainFrame() && to_different_document,
5898 false, nullptr);
dmazzonibf8cec42015-02-08 08:28:085899
Xiyuan Xia2a66b112018-12-06 15:52:165900 // Reset the focus state from DidStartNavigation to false if a new load starts
5901 // afterward, in case loading logic triggers a FocusLocationBarByDefault call.
5902 should_focus_location_bar_by_default_ = false;
[email protected]420ae012009-04-24 05:16:325903}
5904
fdegansc73eec22015-03-19 14:47:545905void WebContentsImpl::DidStopLoading() {
dcheng59716272016-04-09 05:19:085906 std::unique_ptr<LoadNotificationDetails> details;
[email protected]420ae012009-04-24 05:16:325907
[email protected]9595fd82013-04-19 21:28:495908 // Use the last committed entry rather than the active one, in case a
5909 // pending entry has been created.
5910 NavigationEntry* entry = controller_.GetLastCommittedEntry();
[email protected]3691e5cf2014-01-22 10:16:205911 Navigator* navigator = frame_tree_.root()->navigator();
[email protected]9595fd82013-04-19 21:28:495912
[email protected]420ae012009-04-24 05:16:325913 // An entry may not exist for a stop when loading an initial blank page or
5914 // if an iframe injected by script into a blank page finishes loading.
5915 if (entry) {
[email protected]3691e5cf2014-01-22 10:16:205916 base::TimeDelta elapsed =
5917 base::TimeTicks::Now() - navigator->GetCurrentLoadStart();
[email protected]420ae012009-04-24 05:16:325918
5919 details.reset(new LoadNotificationDetails(
[email protected]36fc0392011-12-25 03:59:515920 entry->GetVirtualURL(),
[email protected]420ae012009-04-24 05:16:325921 elapsed,
5922 &controller_,
5923 controller_.GetCurrentEntryIndex()));
5924 }
5925
clamy44e84ce2016-02-22 15:38:255926 LoadingStateChanged(true, false, details.get());
[email protected]420ae012009-04-24 05:16:325927}
5928
fdegansa696e5112015-04-17 01:57:595929void WebContentsImpl::DidChangeLoadProgress() {
Nate Chapin22ea6592019-03-05 22:29:025930 if (is_being_destroyed_)
5931 return;
fdegansa696e5112015-04-17 01:57:595932 double load_progress = frame_tree_.load_progress();
5933
5934 // The delegate is notified immediately for the first and last updates. Also,
5935 // since the message loop may be pretty busy when a page is loaded, it might
5936 // not execute a posted task in a timely manner so the progress report is sent
5937 // immediately if enough time has passed.
5938 base::TimeDelta min_delay =
5939 base::TimeDelta::FromMilliseconds(kMinimumDelayBetweenLoadingUpdatesMS);
5940 bool delay_elapsed = loading_last_progress_update_.is_null() ||
5941 base::TimeTicks::Now() - loading_last_progress_update_ > min_delay;
5942
5943 if (load_progress == 0.0 || load_progress == 1.0 || delay_elapsed) {
5944 // If there is a pending task to send progress, it is now obsolete.
5945 loading_weak_factory_.InvalidateWeakPtrs();
5946
5947 // Notify the load progress change.
5948 SendChangeLoadProgress();
5949
5950 // Clean-up the states if needed.
5951 if (load_progress == 1.0)
5952 ResetLoadProgressState();
5953 return;
5954 }
5955
5956 if (loading_weak_factory_.HasWeakPtrs())
5957 return;
5958
skyostil95082a62015-06-05 19:53:075959 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
tzike2aca992017-09-05 08:50:545960 FROM_HERE,
5961 base::BindOnce(&WebContentsImpl::SendChangeLoadProgress,
5962 loading_weak_factory_.GetWeakPtr()),
fdegansa696e5112015-04-17 01:57:595963 min_delay);
5964}
5965
avid6d88b912017-01-13 00:16:005966std::vector<std::unique_ptr<NavigationThrottle>>
5967WebContentsImpl::CreateThrottlesForNavigation(
clamycbf524d42016-09-27 12:48:355968 NavigationHandle* navigation_handle) {
Charlie Reis96077362019-07-04 00:34:045969 auto throttles = GetContentClient()->browser()->CreateThrottlesForNavigation(
clamycbf524d42016-09-27 12:48:355970 navigation_handle);
Charlie Reis96077362019-07-04 00:34:045971
5972 // This is not a normal place to be adding a throttle. However, in the case
5973 // javascript dialogs, related logic is present in the web_contents/ layer,
5974 // and the purpose of the throttle is to ensure that navigation commits are
5975 // deferred for the entire WebContents. Most throttles are either added by
5976 // the embederrer outside of content/, or are per-frame and added by
5977 // NavigationThrottleRunner.
5978 std::unique_ptr<content::NavigationThrottle> dialog_throttle =
5979 JavaScriptDialogNavigationThrottle::MaybeCreateThrottleFor(
5980 navigation_handle);
5981 if (dialog_throttle)
5982 throttles.push_back(std::move(dialog_throttle));
5983
5984 return throttles;
clamycbf524d42016-09-27 12:48:355985}
5986
clamy1e5574e2016-09-29 16:48:445987std::unique_ptr<NavigationUIData> WebContentsImpl::GetNavigationUIData(
5988 NavigationHandle* navigation_handle) {
clamy1e5574e2016-09-29 16:48:445989 return GetContentClient()->browser()->GetNavigationUIData(navigation_handle);
5990}
5991
pdr15f5b5b2017-06-20 00:58:005992void WebContentsImpl::DidCancelLoading() {
5993 controller_.DiscardNonCommittedEntries();
5994
5995 // Update the URL display.
5996 NotifyNavigationStateChanged(INVALIDATE_TYPE_URL);
5997}
5998
[email protected]59167c22013-06-03 18:07:325999void WebContentsImpl::DidAccessInitialDocument() {
[email protected]b4c84012014-04-28 19:51:106000 has_accessed_initial_document_ = true;
6001
[email protected]d1371bf8e2014-04-19 03:26:246002 // We may have left a failed browser-initiated navigation in the address bar
6003 // to let the user edit it and try again. Clear it now that content might
6004 // show up underneath it.
6005 if (!IsLoading() && controller_.GetPendingEntry())
avi45a72532015-04-07 21:01:456006 controller_.DiscardPendingEntry(false);
[email protected]d1371bf8e2014-04-19 03:26:246007
[email protected]59167c22013-06-03 18:07:326008 // Update the URL display.
thestigc4cac8f2014-09-04 21:17:506009 NotifyNavigationStateChanged(INVALIDATE_TYPE_URL);
[email protected]59167c22013-06-03 18:07:326010}
6011
alexmosbe2f4c32015-03-10 02:30:236012void WebContentsImpl::DidChangeName(RenderFrameHost* render_frame_host,
6013 const std::string& name) {
ericwilligersde386342016-10-19 02:35:096014 for (auto& observer : observers_)
6015 observer.FrameNameChanged(render_frame_host, name);
alexmosbe2f4c32015-03-10 02:30:236016}
6017
John Delaneyedd8d6c2019-01-25 00:23:576018void WebContentsImpl::DidReceiveFirstUserActivation(
6019 RenderFrameHost* render_frame_host) {
6020 for (auto& observer : observers_)
6021 observer.FrameReceivedFirstUserActivation(render_frame_host);
6022}
6023
John Delaney0146a742019-01-25 19:25:286024void WebContentsImpl::DidChangeDisplayState(RenderFrameHost* render_frame_host,
6025 bool is_display_none) {
6026 for (auto& observer : observers_)
6027 observer.FrameDisplayStateChanged(render_frame_host, is_display_none);
6028}
6029
6030void WebContentsImpl::FrameSizeChanged(RenderFrameHost* render_frame_host,
6031 const gfx::Size& frame_size) {
6032 for (auto& observer : observers_)
6033 observer.FrameSizeChanged(render_frame_host, frame_size);
6034}
6035
[email protected]39f2f9252014-05-03 00:54:016036void WebContentsImpl::DocumentOnLoadCompleted(
[email protected]50279492014-05-05 23:24:296037 RenderFrameHost* render_frame_host) {
falken52a56e32016-12-08 05:02:406038 ShowInsecureLocalhostWarningIfNeeded();
6039
Alexander Timindd0fa0c52019-11-06 13:03:376040 GetRenderViewHost()->DocumentOnLoadCompletedInMainFrame();
6041
creis97867aa2017-02-17 18:39:456042 is_notifying_observers_ = true;
ericwilligersde386342016-10-19 02:35:096043 for (auto& observer : observers_)
6044 observer.DocumentOnLoadCompletedInMainFrame();
creis97867aa2017-02-17 18:39:456045 is_notifying_observers_ = false;
[email protected]6578fef2013-10-14 02:35:096046
6047 // TODO(avi): Remove. http://crbug.com/170921
[email protected]8ff00d72012-10-23 19:12:216048 NotificationService::current()->Notify(
6049 NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
6050 Source<WebContents>(this),
[email protected]50279492014-05-05 23:24:296051 NotificationService::NoDetails());
[email protected]25497492010-09-11 15:15:086052}
6053
creis8eb8ef22015-11-04 22:51:386054void WebContentsImpl::UpdateStateForFrame(RenderFrameHost* render_frame_host,
6055 const PageState& page_state) {
creis8eb8ef22015-11-04 22:51:386056 // The state update affects the last NavigationEntry associated with the given
6057 // |render_frame_host|. This may not be the last committed NavigationEntry (as
6058 // in the case of an UpdateState from a frame being swapped out). We track
6059 // which entry this is in the RenderFrameHost's nav_entry_id.
6060 RenderFrameHostImpl* rfhi =
6061 static_cast<RenderFrameHostImpl*>(render_frame_host);
6062 NavigationEntryImpl* entry =
6063 controller_.GetEntryWithUniqueID(rfhi->nav_entry_id());
6064 if (!entry)
6065 return;
6066
6067 FrameNavigationEntry* frame_entry =
6068 entry->GetFrameEntry(rfhi->frame_tree_node());
6069 if (!frame_entry)
6070 return;
6071
creisd06a9422015-11-11 03:08:456072 // The SiteInstance might not match if we do a cross-process navigation with
6073 // replacement (e.g., auto-subframe), in which case the swap out of the old
6074 // RenderFrameHost runs in the background after the old FrameNavigationEntry
6075 // has already been replaced and destroyed.
6076 if (frame_entry->site_instance() != rfhi->GetSiteInstance())
6077 return;
6078
creis8eb8ef22015-11-04 22:51:386079 if (page_state == frame_entry->page_state())
6080 return; // Nothing to update.
6081
creis0cade2e2017-02-28 06:37:476082 DCHECK(page_state.IsValid()) << "Shouldn't set an empty PageState.";
nasko34634672016-07-29 18:46:066083
6084 // The document_sequence_number and item_sequence_number recorded in the
6085 // FrameNavigationEntry should not differ from the one coming with the update,
6086 // since it must come from the same document. Do not update it if a difference
6087 // is detected, as this indicates that |frame_entry| is not the correct one.
6088 ExplodedPageState exploded_state;
6089 if (!DecodePageState(page_state.ToEncodedData(), &exploded_state))
6090 return;
6091
6092 if (exploded_state.top.document_sequence_number !=
6093 frame_entry->document_sequence_number() ||
6094 exploded_state.top.item_sequence_number !=
6095 frame_entry->item_sequence_number()) {
6096 return;
6097 }
6098
nasko04a322f25a2016-09-01 00:00:346099 frame_entry->SetPageState(page_state);
creis8eb8ef22015-11-04 22:51:386100 controller_.NotifyEntryChanged(entry);
6101}
6102
[email protected]ef3adfc2014-05-11 00:04:546103void WebContentsImpl::UpdateTitle(RenderFrameHost* render_frame_host,
[email protected]ef3adfc2014-05-11 00:04:546104 const base::string16& title,
6105 base::i18n::TextDirection title_direction) {
[email protected]ef3adfc2014-05-11 00:04:546106 // Try to find the navigation entry, which might not be the current one.
avi286019ce2015-07-29 01:26:586107 // For example, it might be from a recently swapped out RFH.
avi1edc8552016-10-13 21:37:166108 NavigationEntryImpl* entry = controller_.GetEntryWithUniqueID(
creisad8c31a2016-05-24 17:07:046109 static_cast<RenderFrameHostImpl*>(render_frame_host)->nav_entry_id());
[email protected]ef3adfc2014-05-11 00:04:546110
6111 // We can handle title updates when we don't have an entry in
6112 // UpdateTitleForEntry, but only if the update is from the current RVH.
6113 // TODO(avi): Change to make decisions based on the RenderFrameHost.
avi286019ce2015-07-29 01:26:586114 if (!entry && render_frame_host != GetMainFrame())
[email protected]ef3adfc2014-05-11 00:04:546115 return;
6116
6117 // TODO(evan): make use of title_direction.
6118 // http://code.google.com/p/chromium/issues/detail?id=27094
afakhry6f0c1ec22016-07-14 13:55:136119 UpdateTitleForEntry(entry, title);
[email protected]ef3adfc2014-05-11 00:04:546120}
6121
[email protected]39f2f9252014-05-03 00:54:016122void WebContentsImpl::DocumentAvailableInMainFrame(
6123 RenderViewHost* render_view_host) {
ericwilligersde386342016-10-19 02:35:096124 for (auto& observer : observers_)
6125 observer.DocumentAvailableInMainFrame();
[email protected]39f2f9252014-05-03 00:54:016126}
naskoc0fceff2015-04-30 15:53:526127
[email protected]e1c3a552012-05-04 20:51:326128void WebContentsImpl::RouteCloseEvent(RenderViewHost* rvh) {
6129 // Tell the active RenderViewHost to run unload handlers and close, as long
6130 // as the request came from a RenderViewHost in the same BrowsingInstance.
6131 // In most cases, we receive this from a swapped out RenderViewHost.
6132 // It is possible to receive it from one that has just been swapped in,
6133 // in which case we might as well deliver the message anyway.
6134 if (rvh->GetSiteInstance()->IsRelatedSiteInstance(GetSiteInstance()))
naskoc0fceff2015-04-30 15:53:526135 ClosePage();
[email protected]e1c3a552012-05-04 20:51:326136}
6137
alexmose7da5a12015-04-09 02:22:166138bool WebContentsImpl::ShouldRouteMessageEvent(
6139 RenderFrameHost* target_rfh,
6140 SiteInstance* source_site_instance) const {
6141 // Allow the message if this WebContents is dedicated to a browser plugin
6142 // guest.
[email protected]0eba810b2012-10-18 03:19:366143 // Note: This check means that an embedder could theoretically receive a
6144 // postMessage from anyone (not just its own guests). However, this is
6145 // probably not a risk for apps since other pages won't have references
6146 // to App windows.
alexmose7da5a12015-04-09 02:22:166147 return GetBrowserPluginGuest() || GetBrowserPluginEmbedder();
6148}
[email protected]f546640b2012-05-15 00:03:496149
alexmos58729042015-06-18 23:20:006150void WebContentsImpl::EnsureOpenerProxiesExist(RenderFrameHost* source_rfh) {
alexmose7da5a12015-04-09 02:22:166151 WebContentsImpl* source_web_contents = static_cast<WebContentsImpl*>(
6152 WebContents::FromRenderFrameHost(source_rfh));
[email protected]f546640b2012-05-15 00:03:496153
alexmose7da5a12015-04-09 02:22:166154 if (source_web_contents) {
lazyboy6ec48b2a2015-06-29 15:18:146155 // If this message is going to outer WebContents from inner WebContents,
6156 // then we should not create a RenderView. AttachToOuterWebContentsFrame()
6157 // already created a RenderFrameProxyHost for that purpose.
6158 if (GetBrowserPluginEmbedder() &&
W. James MacLean2539adb32019-12-13 00:40:446159 source_web_contents->browser_plugin_guest_) {
lazyboy6ec48b2a2015-06-29 15:18:146160 return;
W. James MacLean2539adb32019-12-13 00:40:446161 }
lazyboy6ec48b2a2015-06-29 15:18:146162
lfg812874d2016-02-09 22:26:136163 if (this != source_web_contents && GetBrowserPluginGuest()) {
naskob3041b98a42016-03-12 04:43:066164 // We create a RenderFrameProxyHost for the embedder in the guest's render
6165 // process but we intentionally do not expose the embedder's opener chain
6166 // to it.
6167 source_web_contents->GetRenderManager()->CreateRenderFrameProxy(
6168 GetSiteInstance());
[email protected]f546640b2012-05-15 00:03:496169 } else {
alexmos58729042015-06-18 23:20:006170 RenderFrameHostImpl* source_rfhi =
6171 static_cast<RenderFrameHostImpl*>(source_rfh);
6172 source_rfhi->frame_tree_node()->render_manager()->CreateOpenerProxies(
alexmos90325cf2015-09-02 17:18:396173 GetSiteInstance(), nullptr);
[email protected]f546640b2012-05-15 00:03:496174 }
6175 }
[email protected]f546640b2012-05-15 00:03:496176}
6177
avallee9993fca2016-11-17 06:16:506178void WebContentsImpl::SetAsFocusedWebContentsIfNecessary() {
6179 // Only change focus if we are not currently focused.
6180 WebContentsImpl* old_contents = GetFocusedWebContents();
6181 if (old_contents == this)
6182 return;
6183
paulmeyerfeafc2d2017-04-25 21:46:406184 GetOutermostWebContents()->node_.SetFocusedWebContents(this);
6185
avallee9993fca2016-11-17 06:16:506186 // Send a page level blur to the old contents so that it displays inactive UI
6187 // and focus this contents to activate it.
6188 if (old_contents)
6189 old_contents->GetMainFrame()->GetRenderWidgetHost()->SetPageFocus(false);
avalleec2ff1ef2017-01-21 00:12:306190
6191 // Make sure the outer web contents knows our frame is focused. Otherwise, the
6192 // outer renderer could have the element before or after the frame element
6193 // focused which would return early without actually advancing focus.
avalleed68cdd12017-06-21 22:07:276194 FocusOuterAttachmentFrameChain();
avalleec2ff1ef2017-01-21 00:12:306195
lfg1453e412017-04-11 00:48:506196 if (ShowingInterstitialPage()) {
creisadca9862017-06-16 19:12:116197 static_cast<RenderFrameHostImpl*>(interstitial_page_->GetMainFrame())
lfg1453e412017-04-11 00:48:506198 ->GetRenderWidgetHost()
6199 ->SetPageFocus(true);
6200 } else {
6201 GetMainFrame()->GetRenderWidgetHost()->SetPageFocus(true);
6202 }
avallee9993fca2016-11-17 06:16:506203}
6204
avallee0206f782016-07-28 18:55:336205void WebContentsImpl::SetFocusedFrame(FrameTreeNode* node,
6206 SiteInstance* source) {
avallee0206f782016-07-28 18:55:336207 frame_tree_.SetFocusedFrame(node, source);
avalleed68cdd12017-06-21 22:07:276208
Lucas Furukawa Gadaniabbfa342017-08-29 19:37:576209 if (auto* inner_contents = node_.GetInnerWebContentsInFrame(node)) {
6210 // |this| is an outer WebContents and |node| represents an inner
6211 // WebContents. Transfer the focus to the inner contents if |this| is
6212 // focused.
6213 if (GetFocusedWebContents() == this)
6214 inner_contents->SetAsFocusedWebContentsIfNecessary();
6215 } else if (node_.OuterContentsFrameTreeNode() &&
6216 node_.OuterContentsFrameTreeNode()
6217 ->current_frame_host()
6218 ->GetSiteInstance() == source) {
6219 // |this| is an inner WebContents, |node| is its main FrameTreeNode and
6220 // the outer WebContents FrameTreeNode is at |source|'s SiteInstance.
6221 // Transfer the focus to the inner WebContents if the outer WebContents is
6222 // focused. This branch is used when an inner WebContents is focused through
Lowell Mannersa9530382019-08-01 09:46:576223 // its RenderFrameProxyHost (via FrameFocused mojo call, used to
Lucas Furukawa Gadaniabbfa342017-08-29 19:37:576224 // implement the window.focus() API).
6225 if (GetFocusedWebContents() == GetOuterWebContents())
6226 SetAsFocusedWebContentsIfNecessary();
6227 } else if (!GetOuterWebContents()) {
6228 // This is an outermost WebContents.
6229 SetAsFocusedWebContentsIfNecessary();
Lucas Furukawa Gadaniabbfa342017-08-29 19:37:576230 }
avalleed68cdd12017-06-21 22:07:276231}
6232
Avi Drissman36f801f2018-02-01 20:06:046233void WebContentsImpl::DidCallFocus() {
6234 // Any explicit focusing of another window while this WebContents is in
6235 // fullscreen can be used to confuse the user, so drop fullscreen.
Avi Drissmand18c51972018-08-15 18:32:096236 ForSecurityDropFullscreen();
Avi Drissman36f801f2018-02-01 20:06:046237}
6238
avalleed68cdd12017-06-21 22:07:276239RenderFrameHost* WebContentsImpl::GetFocusedFrameIncludingInnerWebContents() {
6240 WebContentsImpl* contents = this;
6241 FrameTreeNode* focused_node = contents->frame_tree_.GetFocusedFrame();
6242
6243 // If there is no focused frame in the outer WebContents, we need to return
6244 // null.
6245 if (!focused_node)
6246 return nullptr;
6247
6248 // If the focused frame is embedding an inner WebContents, we must descend
6249 // into that contents. If the current WebContents does not have a focused
6250 // frame, return the main frame of this contents instead of the focused empty
6251 // frame embedding this contents.
6252 while (true) {
6253 contents = contents->node_.GetInnerWebContentsInFrame(focused_node);
6254 if (!contents)
6255 return focused_node->current_frame_host();
6256
6257 focused_node = contents->frame_tree_.GetFocusedFrame();
6258 if (!focused_node)
6259 return contents->GetMainFrame();
6260 }
avallee0206f782016-07-28 18:55:336261}
6262
Lucas Furukawa Gadaniabbfa342017-08-29 19:37:576263void WebContentsImpl::OnAdvanceFocus(RenderFrameHostImpl* source_rfh) {
6264 // When a RenderFrame needs to advance focus to a RenderFrameProxy (by hitting
6265 // TAB), the RenderFrameProxy sends an IPC to RenderFrameProxyHost. When this
6266 // RenderFrameProxyHost represents an inner WebContents, the outer WebContents
6267 // needs to focus the inner WebContents.
6268 if (GetOuterWebContents() &&
6269 GetOuterWebContents() == source_rfh->delegate()->GetAsWebContents() &&
6270 GetFocusedWebContents() == GetOuterWebContents()) {
6271 SetAsFocusedWebContentsIfNecessary();
6272 }
6273}
6274
ekaramada110f642016-12-21 19:47:286275void WebContentsImpl::OnFocusedElementChangedInFrame(
6276 RenderFrameHostImpl* frame,
6277 const gfx::Rect& bounds_in_root_view) {
6278 RenderWidgetHostViewBase* root_view =
6279 static_cast<RenderWidgetHostViewBase*>(GetRenderWidgetHostView());
6280 if (!root_view || !frame->GetView())
6281 return;
6282
6283 // Converting to screen coordinates.
6284 gfx::Point origin = bounds_in_root_view.origin();
6285 origin += root_view->GetViewBounds().OffsetFromOrigin();
6286 gfx::Rect bounds_in_screen(origin, bounds_in_root_view.size());
6287
6288 root_view->FocusedNodeChanged(frame->has_focused_editable_element(),
6289 bounds_in_screen);
6290
6291 FocusedNodeDetails details = {frame->has_focused_editable_element(),
6292 bounds_in_screen};
6293
6294 // TODO(ekaramad): We should replace this with an observer notification
6295 // (https://crbug.com/675975).
6296 NotificationService::current()->Notify(
6297 NOTIFICATION_FOCUS_CHANGED_IN_PAGE,
6298 Source<RenderViewHost>(GetRenderViewHost()),
6299 Details<FocusedNodeDetails>(&details));
Jeff Fisher709b4c52019-07-29 20:07:346300
6301 for (auto& observer : observers_)
6302 observer.OnFocusChangedInPage(&details);
ekaramada110f642016-12-21 19:47:286303}
6304
Lowell Manners1de5242e2019-04-25 10:18:466305bool WebContentsImpl::DidAddMessageToConsole(
6306 blink::mojom::ConsoleMessageLevel log_level,
6307 const base::string16& message,
6308 int32_t line_no,
6309 const base::string16& source_id) {
[email protected]a796f202012-05-30 14:14:256310 if (!delegate_)
6311 return false;
Lowell Manners1de5242e2019-04-25 10:18:466312 return delegate_->DidAddMessageToConsole(this, log_level, message, line_no,
avia90ae4e2016-11-11 20:49:336313 source_id);
[email protected]a796f202012-05-30 14:14:256314}
6315
Daniel Cheng90196c82018-04-25 21:49:146316void WebContentsImpl::DidReceiveInputEvent(
dominickn9c7ceaa32016-02-27 01:52:536317 RenderWidgetHostImpl* render_widget_host,
6318 const blink::WebInputEvent::Type type) {
Dominick Ng37a15052018-07-17 23:56:356319 if (!IsUserInteractionInputType(type))
Daniel Cheng90196c82018-04-25 21:49:146320 return;
6321
kenrb19221852016-04-29 17:21:406322 // Ignore unless the widget is currently in the frame tree.
6323 if (!HasMatchingWidgetHost(&frame_tree_, render_widget_host))
avic3aa8422015-11-09 20:57:226324 return;
6325
Daniel Cheng90196c82018-04-25 21:49:146326 if (type != blink::WebInputEvent::kGestureScrollBegin)
6327 last_interactive_input_event_time_ = ui::EventTimeForNow();
dominickn9c7ceaa32016-02-27 01:52:536328
Dominick Ng37a15052018-07-17 23:56:356329 for (auto& observer : observers_)
6330 observer.DidGetUserInteraction(type);
dominickn2dd142dd2015-10-29 05:30:506331}
6332
Avi Drissman738ea192018-08-29 20:24:166333bool WebContentsImpl::ShouldIgnoreInputEvents() {
6334 WebContentsImpl* web_contents = this;
6335 while (web_contents) {
6336 if (web_contents->ignore_input_events_)
6337 return true;
6338 web_contents = web_contents->GetOuterWebContents();
6339 }
6340
6341 return false;
6342}
6343
avallee9993fca2016-11-17 06:16:506344void WebContentsImpl::FocusOwningWebContents(
6345 RenderWidgetHostImpl* render_widget_host) {
Alex Moshchukc4f0a722019-11-19 22:41:286346 RenderWidgetHostImpl* main_frame_widget_host =
6347 GetMainFrame()->GetRenderWidgetHost();
avallee9993fca2016-11-17 06:16:506348 RenderWidgetHostImpl* focused_widget =
Alex Moshchukc4f0a722019-11-19 22:41:286349 GetFocusedRenderWidgetHost(main_frame_widget_host);
avallee9993fca2016-11-17 06:16:506350
6351 if (focused_widget != render_widget_host &&
avallee8e881592016-11-19 00:39:306352 (!focused_widget ||
6353 focused_widget->delegate() != render_widget_host->delegate())) {
avallee9993fca2016-11-17 06:16:506354 SetAsFocusedWebContentsIfNecessary();
6355 }
6356}
6357
[email protected]b172aee2012-04-10 17:05:266358void WebContentsImpl::OnIgnoredUIEvent() {
[email protected]31145092011-09-30 01:49:446359 // Notify observers.
ericwilligersde386342016-10-19 02:35:096360 for (auto& observer : observers_)
6361 observer.DidGetIgnoredUIEvent();
[email protected]fa1cf0b82010-01-15 21:49:446362}
6363
avic3aa8422015-11-09 20:57:226364void WebContentsImpl::RendererUnresponsive(
Lukasz Anforowicz52b93722018-06-20 16:11:396365 RenderWidgetHostImpl* render_widget_host,
6366 base::RepeatingClosure hang_monitor_restarter) {
avic031d392017-03-03 03:09:426367 if (ShouldIgnoreUnresponsiveRenderer())
[email protected]e5fc1632011-08-08 07:51:536368 return;
6369
Lukasz Anforowicz4f0593d2018-09-21 18:33:546370 // Do not report hangs (to task manager, to hang renderer dialog, etc.) for
6371 // invisible tabs (like extension background page, background tabs). See
6372 // https://crbug.com/881812 for rationale and for choosing the visibility
6373 // (rather than process priority) as the signal here.
6374 if (GetVisibility() != Visibility::VISIBLE)
6375 return;
6376
Avi Drissman8920def2018-01-31 19:53:366377 if (!render_widget_host->renderer_initialized())
[email protected]55452902011-06-01 21:57:476378 return;
6379
Lukasz Anforowicz4f0593d2018-09-21 18:33:546380 for (auto& observer : observers_)
6381 observer.OnRendererUnresponsive(render_widget_host->GetProcess());
6382
Dave Tapuskac6146c52017-12-20 22:48:156383 if (delegate_)
Lukasz Anforowicz52b93722018-06-20 16:11:396384 delegate_->RendererUnresponsive(this, render_widget_host,
6385 std::move(hang_monitor_restarter));
[email protected]420ae012009-04-24 05:16:326386}
6387
avic3aa8422015-11-09 20:57:226388void WebContentsImpl::RendererResponsive(
6389 RenderWidgetHostImpl* render_widget_host) {
[email protected]6934a702011-12-20 00:04:516390 if (delegate_)
Avi Drissman1572e8c3c2018-02-02 19:06:366391 delegate_->RendererResponsive(this, render_widget_host);
[email protected]420ae012009-04-24 05:16:326392}
6393
Alex Moshchuk7b4f0652019-05-30 18:54:416394void WebContentsImpl::SubframeCrashed(
6395 blink::mojom::FrameVisibility visibility) {
6396 // If a subframe crashed on a hidden tab, mark the tab for reload to avoid
6397 // showing a sad frame to the user if they ever switch back to that tab. Do
6398 // this for subframes that are either visible in viewport or visible but
6399 // scrolled out of view, but skip subframes that are not rendered (e.g., via
6400 // "display:none"), since in that case the user wouldn't see a sad frame
6401 // anyway.
6402 bool did_mark_for_reload = false;
6403 if (IsHidden() && visibility != blink::mojom::FrameVisibility::kNotRendered &&
6404 base::FeatureList::IsEnabled(
6405 features::kReloadHiddenTabsWithCrashedSubframes)) {
6406 controller_.SetNeedsReload(
6407 NavigationControllerImpl::NeedsReloadType::kCrashedSubframe);
6408 did_mark_for_reload = true;
6409 UMA_HISTOGRAM_ENUMERATION(
6410 "Stability.ChildFrameCrash.TabMarkedForReload.Visibility", visibility);
6411 }
6412
6413 UMA_HISTOGRAM_BOOLEAN("Stability.ChildFrameCrash.TabMarkedForReload",
6414 did_mark_for_reload);
6415}
6416
[email protected]b172aee2012-04-10 17:05:266417void WebContentsImpl::BeforeUnloadFiredFromRenderManager(
[email protected]feaded02012-09-25 19:12:556418 bool proceed, const base::TimeTicks& proceed_time,
[email protected]420ae012009-04-24 05:16:326419 bool* proceed_to_fire_unload) {
ericwilligersde386342016-10-19 02:35:096420 for (auto& observer : observers_)
Chris Hamilton3b3e315d2018-09-19 13:16:216421 observer.BeforeUnloadFired(proceed, proceed_time);
[email protected]6934a702011-12-20 00:04:516422 if (delegate_)
6423 delegate_->BeforeUnloadFired(this, proceed, proceed_to_fire_unload);
[email protected]e80af492013-06-24 21:52:096424 // Note: |this| might be deleted at this point.
[email protected]420ae012009-04-24 05:16:326425}
6426
[email protected]58d5cfe2013-07-10 02:40:526427void WebContentsImpl::RenderProcessGoneFromRenderManager(
[email protected]3a3d47472010-07-15 21:03:546428 RenderViewHost* render_view_host) {
[email protected]443b80e2010-12-14 00:42:236429 DCHECK(crashed_status_ != base::TERMINATION_STATUS_STILL_RUNNING);
[email protected]ec6a7eb2013-04-22 17:34:226430 RenderViewTerminated(render_view_host, crashed_status_, crashed_error_code_);
[email protected]3a3d47472010-07-15 21:03:546431}
6432
Jianzhou Fengd8720c72018-01-18 03:06:306433void WebContentsImpl::UpdateRenderViewSizeForRenderManager(bool is_main_frame) {
[email protected]8643e6d2012-01-18 20:26:106434 // TODO(brettw) this is a hack. See WebContentsView::SizeContents.
Jianzhou Fengd8720c72018-01-18 03:06:306435 gfx::Size size = GetSizeForNewRenderView(is_main_frame);
[email protected]0323ee42010-02-17 22:03:266436 // 0x0 isn't a valid window size (minimal window size is 1x1) but it may be
6437 // here during container initialization and normal window size will be set
6438 // later. In case of tab duplication this resizing to 0x0 prevents setting
6439 // normal size later so just ignore it.
6440 if (!size.IsEmpty())
6441 view_->SizeContents(size);
[email protected]420ae012009-04-24 05:16:326442}
6443
[email protected]af905902013-10-01 21:38:516444void WebContentsImpl::CancelModalDialogsForRenderManager() {
6445 // We need to cancel modal dialogs when doing a process swap, since the load
avi2460c762015-04-17 15:21:546446 // deferrer would prevent us from swapping out. We also clear the state
6447 // because this is a cross-process navigation, which means that it's a new
6448 // site that should not have to pay for the sins of its predecessor.
creis89a0f782015-05-27 16:13:176449 //
Lei Zhang6410ec1a2019-01-31 21:38:586450 // Note that we don't bother telling |browser_plugin_embedder_| because the
creis89a0f782015-05-27 16:13:176451 // cross-process navigation will either destroy the browser plugins or not
6452 // require their dialogs to close.
avi5d3b8692016-10-12 22:00:466453 if (dialog_manager_) {
avi6879fcf2017-01-21 05:27:536454 dialog_manager_->CancelDialogs(this, /*reset_state=*/true);
avi5d3b8692016-10-12 22:00:466455 }
[email protected]af905902013-10-01 21:38:516456}
6457
[email protected]02d7b6e2014-06-24 21:01:506458void WebContentsImpl::NotifySwappedFromRenderManager(RenderFrameHost* old_host,
6459 RenderFrameHost* new_host,
6460 bool is_main_frame) {
6461 if (is_main_frame) {
arthursonzognif97bca512019-10-10 11:32:586462 NotifyViewSwapped(old_host ? old_host->GetRenderViewHost() : nullptr,
6463 new_host->GetRenderViewHost());
[email protected]a6b73c62013-02-11 23:05:086464
[email protected]02d7b6e2014-06-24 21:01:506465 // Make sure the visible RVH reflects the new delegate's preferences.
6466 if (delegate_)
6467 view_->SetOverscrollControllerEnabled(CanOverscrollContent());
[email protected]d56a49e2013-02-23 00:47:396468
dmazzoni62111452017-04-28 23:46:556469 RenderWidgetHostViewBase* rwhv =
6470 static_cast<RenderWidgetHostViewBase*>(GetRenderWidgetHostView());
6471 if (rwhv)
6472 rwhv->SetMainFrameAXTreeID(GetMainFrame()->GetAXTreeID());
[email protected]02d7b6e2014-06-24 21:01:506473 }
6474
Jinsuk Kim895b2c52018-07-03 06:49:166475 NotifyFrameSwapped(old_host, new_host, is_main_frame);
[email protected]3a3d47472010-07-15 21:03:546476}
6477
naskof5940b9f2015-03-02 23:04:056478void WebContentsImpl::NotifyMainFrameSwappedFromRenderManager(
Jinsuk Kim895b2c52018-07-03 06:49:166479 RenderFrameHost* old_host,
6480 RenderFrameHost* new_host) {
6481 NotifyViewSwapped(old_host ? old_host->GetRenderViewHost() : nullptr,
6482 new_host->GetRenderViewHost());
naskof5940b9f2015-03-02 23:04:056483}
6484
[email protected]b172aee2012-04-10 17:05:266485NavigationControllerImpl& WebContentsImpl::GetControllerForRenderManager() {
[email protected]d1198fd2012-08-13 22:50:196486 return GetController();
[email protected]3a3d47472010-07-15 21:03:546487}
6488
dcheng59716272016-04-09 05:19:086489std::unique_ptr<WebUIImpl> WebContentsImpl::CreateWebUIForRenderFrameHost(
carlosk37f793792014-11-27 15:08:296490 const GURL& url) {
Dan Beam055fe8f2017-10-04 23:23:366491 return CreateWebUI(url);
[email protected]420ae012009-04-24 05:16:326492}
6493
creisadca9862017-06-16 19:12:116494InterstitialPageImpl* WebContentsImpl::GetInterstitialForRenderManager() {
6495 return interstitial_page_;
6496}
6497
lfgbede6392015-09-11 21:54:066498void WebContentsImpl::CreateRenderWidgetHostViewForRenderManager(
6499 RenderViewHost* render_view_host) {
lfgc740d4b22016-04-15 16:45:336500 RenderWidgetHostViewBase* rwh_view =
W. James MacLean13d834d2019-12-04 16:06:456501 view_->CreateViewForWidget(render_view_host->GetWidget());
danakjf616f2252019-09-18 21:44:096502 rwh_view->SetSize(GetSizeForNewRenderView(true));
lfgbede6392015-09-11 21:54:066503}
6504
6505bool WebContentsImpl::CreateRenderViewForRenderManager(
6506 RenderViewHost* render_view_host,
6507 int opener_frame_routing_id,
6508 int proxy_routing_id,
Pavel Feldman25234722017-10-11 02:49:066509 const base::UnguessableToken& devtools_frame_token,
lfgbede6392015-09-11 21:54:066510 const FrameReplicationState& replicated_frame_state) {
6511 TRACE_EVENT0("browser,navigation",
6512 "WebContentsImpl::CreateRenderViewForRenderManager");
6513
piman00cf9402017-04-03 17:09:206514 if (proxy_routing_id == MSG_ROUTING_NONE)
lfg9431efd2016-12-14 14:45:266515 CreateRenderWidgetHostViewForRenderManager(render_view_host);
lfg9431efd2016-12-14 14:45:266516
piman00cf9402017-04-03 17:09:206517 if (!static_cast<RenderViewHostImpl*>(render_view_host)
6518 ->CreateRenderView(opener_frame_routing_id, proxy_routing_id,
Pavel Feldman25234722017-10-11 02:49:066519 devtools_frame_token, replicated_frame_state,
6520 created_with_opener_)) {
[email protected]a4127722011-04-27 23:13:526521 return false;
[email protected]14392a52012-05-02 20:28:446522 }
W. James MacLean62198672019-06-04 16:46:106523 // Set the TextAutosizer state from the main frame's renderer on the new view,
6524 // but only if it's not for the main frame. Main frame renderers should create
6525 // this state themselves from up-to-date values, so we shouldn't override it
6526 // with the cached values.
6527 if (!render_view_host->GetMainFrame()) {
6528 render_view_host->Send(
6529 new PageMsg_UpdateTextAutosizerPageInfoForRemoteMainFrames(
6530 render_view_host->GetRoutingID(), text_autosizer_page_info_));
6531 }
[email protected]a4127722011-04-27 23:13:526532
Lucas Furukawa Gadaniaed1fed2017-10-13 17:34:146533 if (proxy_routing_id == MSG_ROUTING_NONE && node_.outer_web_contents())
6534 ReattachToOuterWebContentsFrame();
6535
avi2b177592014-12-10 02:08:026536 SetHistoryOffsetAndLengthForView(render_view_host,
6537 controller_.GetLastCommittedEntryIndex(),
6538 controller_.GetEntryCount());
6539
[email protected]a220b5932013-09-21 03:47:446540#if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID)
[email protected]f8233cc2011-05-31 20:24:506541 // Force a ViewMsg_Resize to be sent, needed to make plugins show up on
6542 // linux. See crbug.com/83941.
avif9ab5d942015-10-15 14:05:446543 RenderWidgetHostView* rwh_view = render_view_host->GetWidget()->GetView();
[email protected]245f7d52011-11-28 15:36:446544 if (rwh_view) {
6545 if (RenderWidgetHost* render_widget_host = rwh_view->GetRenderWidgetHost())
Fady Samuel0b911822018-04-25 13:22:166546 render_widget_host->SynchronizeVisualProperties();
[email protected]245f7d52011-11-28 15:36:446547 }
[email protected]f8233cc2011-05-31 20:24:506548#endif
6549
[email protected]420ae012009-04-24 05:16:326550 return true;
6551}
6552
[email protected]82307f6b2014-08-07 03:30:126553bool WebContentsImpl::CreateRenderFrameForRenderManager(
6554 RenderFrameHost* render_frame_host,
arthursonzogni1efc7b962019-03-14 09:36:336555 int previous_routing_id,
alexmosa181efc2015-09-03 00:39:046556 int opener_routing_id,
japhete6adf142014-10-31 00:01:496557 int parent_routing_id,
alexmosa181efc2015-09-03 00:39:046558 int previous_sibling_routing_id) {
naskob8744d22014-08-28 17:07:436559 TRACE_EVENT0("browser,navigation",
6560 "WebContentsImpl::CreateRenderFrameForRenderManager");
[email protected]82307f6b2014-08-07 03:30:126561
6562 RenderFrameHostImpl* rfh =
6563 static_cast<RenderFrameHostImpl*>(render_frame_host);
arthursonzogni1efc7b962019-03-14 09:36:336564 if (!rfh->CreateRenderFrame(previous_routing_id, opener_routing_id,
alexmosa181efc2015-09-03 00:39:046565 parent_routing_id, previous_sibling_routing_id))
[email protected]82307f6b2014-08-07 03:30:126566 return false;
6567
6568 // TODO(nasko): When RenderWidgetHost is owned by RenderFrameHost, the passed
6569 // RenderFrameHost will have to be associated with the appropriate
6570 // RenderWidgetHostView or a new one should be created here.
6571
6572 return true;
6573}
6574
mfomitchev2b8b066a2016-01-28 19:23:156575#if defined(OS_ANDROID)
[email protected]fc2b46b2014-05-03 16:33:456576
[email protected]155c7f22013-12-09 17:07:186577base::android::ScopedJavaLocalRef<jobject>
6578WebContentsImpl::GetJavaWebContents() {
mostynb042582e2015-03-16 22:13:406579 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]0e813a52014-08-13 10:34:566580 return GetWebContentsAndroid()->GetJavaObject();
6581}
[email protected]155c7f22013-12-09 17:07:186582
[email protected]0e813a52014-08-13 10:34:566583WebContentsAndroid* WebContentsImpl::GetWebContentsAndroid() {
Kevin McNee576bb5d82019-05-29 16:47:516584 if (!web_contents_android_) {
6585 web_contents_android_ = std::make_unique<WebContentsAndroid>(this);
[email protected]155c7f22013-12-09 17:07:186586 }
Kevin McNee576bb5d82019-05-29 16:47:516587 return web_contents_android_.get();
6588}
6589
6590void WebContentsImpl::ClearWebContentsAndroid() {
6591 DCHECK_CURRENTLY_ON(BrowserThread::UI);
6592 web_contents_android_.reset();
[email protected]155c7f22013-12-09 17:07:186593}
6594
paulmeyerc0b762b2016-04-13 11:55:176595void WebContentsImpl::ActivateNearestFindResult(float x,
6596 float y) {
6597 GetOrCreateFindRequestManager()->ActivateNearestFindResult(x, y);
6598}
6599
6600void WebContentsImpl::RequestFindMatchRects(int current_version) {
6601 GetOrCreateFindRequestManager()->RequestFindMatchRects(current_version);
6602}
6603
[email protected]7d244f12013-08-22 21:41:516604bool WebContentsImpl::CreateRenderViewForInitialEmptyDocument() {
nasko4c0feb62015-06-05 18:37:066605 return CreateRenderViewForRenderManager(
6606 GetRenderViewHost(), MSG_ROUTING_NONE, MSG_ROUTING_NONE,
Pavel Feldman25234722017-10-11 02:49:066607 frame_tree_.root()->devtools_frame_token(),
lfgbede6392015-09-11 21:54:066608 frame_tree_.root()->current_replication_state());
[email protected]7d244f12013-08-22 21:41:516609}
[email protected]fc2b46b2014-05-03 16:33:456610
rockot400ea35b2016-10-15 19:15:326611service_manager::InterfaceProvider* WebContentsImpl::GetJavaInterfaces() {
sammcf5f1b0f2016-09-20 23:05:116612 if (!java_interfaces_) {
Gyuyoung Kim6c9ce9022019-11-26 05:40:086613 mojo::PendingRemote<service_manager::mojom::InterfaceProvider> provider;
6614 BindInterfaceRegistryForWebContents(
6615 provider.InitWithNewPipeAndPassReceiver(), this);
rockot400ea35b2016-10-15 19:15:326616 java_interfaces_.reset(new service_manager::InterfaceProvider);
sammcf5f1b0f2016-09-20 23:05:116617 java_interfaces_->Bind(std::move(provider));
6618 }
6619 return java_interfaces_.get();
6620}
6621
Francois Doray6c26f3342018-08-24 17:17:226622#endif
6623
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:416624bool WebContentsImpl::CompletedFirstVisuallyNonEmptyPaint() {
Carlos Caballero91fffb22019-10-29 15:24:306625 return GetRenderViewHost()->did_first_visually_non_empty_paint();
spqchane72c3cb2017-06-30 23:02:106626}
6627
pkotwicz75ca8ffd2016-02-16 23:10:196628void WebContentsImpl::OnDidDownloadImage(
Avi Drissmana5a52dd2018-03-27 03:39:026629 ImageDownloadCallback callback,
pkotwicz75ca8ffd2016-02-16 23:10:196630 int id,
6631 const GURL& image_url,
leon.hancbc4bc3c2016-02-26 07:08:526632 int32_t http_status_code,
leon.hanc2228532016-08-16 05:59:186633 const std::vector<SkBitmap>& images,
6634 const std::vector<gfx::Size>& original_image_sizes) {
Avi Drissmana5a52dd2018-03-27 03:39:026635 std::move(callback).Run(id, http_status_code, image_url, images,
6636 original_image_sizes);
pkotwicz75ca8ffd2016-02-16 23:10:196637}
6638
[email protected]6fba26d2014-04-29 09:38:286639void WebContentsImpl::OnDialogClosed(int render_process_id,
6640 int render_frame_id,
[email protected]b172aee2012-04-10 17:05:266641 IPC::Message* reply_msg,
[email protected]87de04b02014-04-08 22:14:496642 bool dialog_was_suppressed,
[email protected]b172aee2012-04-10 17:05:266643 bool success,
[email protected]fcf75d42013-12-03 20:11:266644 const base::string16& user_input) {
[email protected]6fba26d2014-04-29 09:38:286645 RenderFrameHostImpl* rfh = RenderFrameHostImpl::FromID(render_process_id,
6646 render_frame_id);
[email protected]87de04b02014-04-08 22:14:496647 last_dialog_suppressed_ = dialog_was_suppressed;
6648
Charlie Reis96077362019-07-04 00:34:046649 javascript_dialog_navigation_deferrer_.reset();
6650
[email protected]beb440c2009-11-06 04:08:546651 if (is_showing_before_unload_dialog_ && !success) {
clamybc6f4152016-04-01 20:14:236652 // It is possible for the current RenderFrameHost to have changed in the
6653 // meantime. Do not reset the navigation state in that case.
6654 if (rfh && rfh == rfh->frame_tree_node()->current_frame_host()) {
clamy44e84ce2016-02-22 15:38:256655 rfh->frame_tree_node()->BeforeUnloadCanceled();
clamybc6f4152016-04-01 20:14:236656 controller_.DiscardNonCommittedEntries();
6657 }
[email protected]ec8e8b02013-06-19 04:57:106658
Charles Reis15d60b132017-11-03 17:43:476659 // Update the URL display either way, to avoid showing a stale URL.
6660 NotifyNavigationStateChanged(INVALIDATE_TYPE_URL);
6661
ericwilligersde386342016-10-19 02:35:096662 for (auto& observer : observers_)
6663 observer.BeforeUnloadDialogCancelled();
[email protected]beb440c2009-11-06 04:08:546664 }
[email protected]87de04b02014-04-08 22:14:496665
[email protected]6fba26d2014-04-29 09:38:286666 if (rfh) {
avi44acfaa82017-05-03 22:55:356667 rfh->JavaScriptDialogClosed(reply_msg, success, user_input);
Pavel Feldman3c1842b2017-08-02 05:00:166668
6669 std::vector<protocol::PageHandler*> page_handlers =
6670 protocol::PageHandler::EnabledForWebContents(this);
6671 for (auto* handler : page_handlers)
6672 handler->DidCloseJavaScriptDialog(success, user_input);
6673
[email protected]6fba26d2014-04-29 09:38:286674 } else {
6675 // Don't leak the sync IPC reply if the RFH or process is gone.
6676 delete reply_msg;
6677 }
avi1c51fb62017-02-27 20:43:036678
6679 is_showing_javascript_dialog_ = false;
6680 is_showing_before_unload_dialog_ = false;
[email protected]beb440c2009-11-06 04:08:546681}
6682
[email protected]1ac10dca2013-08-20 20:47:046683bool WebContentsImpl::IsHidden() {
Francois Doraye6161152018-03-27 22:05:376684 return !IsBeingCaptured() && visibility_ != Visibility::VISIBLE;
[email protected]1ac10dca2013-08-20 20:47:046685}
6686
rob97250742015-12-10 17:45:156687int WebContentsImpl::GetOuterDelegateFrameTreeNodeId() {
lfg601233692017-03-06 22:45:446688 return node_.outer_contents_frame_tree_node_id();
lazyboy6ec48b2a2015-06-29 15:18:146689}
6690
ekaramadaeb3c5c2016-11-09 03:22:356691RenderWidgetHostImpl* WebContentsImpl::GetFullscreenRenderWidgetHost() const {
6692 return RenderWidgetHostImpl::FromID(fullscreen_widget_process_id_,
6693 fullscreen_widget_routing_id_);
6694}
6695
[email protected]b0936d22013-11-28 06:47:366696RenderFrameHostManager* WebContentsImpl::GetRenderManager() const {
[email protected]fa944cb82013-11-15 17:51:216697 return frame_tree_.root()->render_manager();
6698}
6699
[email protected]f8497342013-02-05 22:15:026700BrowserPluginGuest* WebContentsImpl::GetBrowserPluginGuest() const {
[email protected]7a846df2012-09-20 19:17:396701 return browser_plugin_guest_.get();
6702}
6703
Albert J. Wongc55cc64d2018-10-12 02:50:046704void WebContentsImpl::SetBrowserPluginGuest(
6705 std::unique_ptr<BrowserPluginGuest> guest) {
6706 DCHECK(!browser_plugin_guest_);
6707 DCHECK(guest);
6708 browser_plugin_guest_ = std::move(guest);
[email protected]738f57a2013-06-29 21:06:546709}
6710
Aidan Wolter52bcc12a2018-11-14 03:27:296711base::UnguessableToken WebContentsImpl::GetAudioGroupId() {
Aidan Wolter52bcc12a2018-11-14 03:27:296712 return GetAudioStreamFactory()->group_id();
6713}
6714
Sofiya Semenova5e9d9122019-07-11 02:46:416715ukm::SourceId WebContentsImpl::GetLastCommittedSourceId() {
6716 return last_committed_source_id_;
6717}
6718
[email protected]f8497342013-02-05 22:15:026719BrowserPluginEmbedder* WebContentsImpl::GetBrowserPluginEmbedder() const {
[email protected]7a846df2012-09-20 19:17:396720 return browser_plugin_embedder_.get();
6721}
[email protected]8ff00d72012-10-23 19:12:216722
fsamuel60b42282015-03-10 03:29:146723void WebContentsImpl::CreateBrowserPluginEmbedderIfNecessary() {
6724 if (browser_plugin_embedder_)
6725 return;
6726 browser_plugin_embedder_.reset(BrowserPluginEmbedder::Create(this));
6727}
6728
Jianzhou Fengd8720c72018-01-18 03:06:306729gfx::Size WebContentsImpl::GetSizeForNewRenderView(bool is_main_frame) {
[email protected]dc0fd432013-08-27 15:29:216730 gfx::Size size;
Jianzhou Fengd8720c72018-01-18 03:06:306731 if (is_main_frame)
6732 size = device_emulation_size_;
6733 if (size.IsEmpty() && delegate_)
[email protected]dc0fd432013-08-27 15:29:216734 size = delegate_->GetSizeForNewRenderView(this);
6735 if (size.IsEmpty())
[email protected]fc2b46b2014-05-03 16:33:456736 size = GetContainerBounds().size();
[email protected]dc0fd432013-08-27 15:29:216737 return size;
6738}
6739
[email protected]ae7eeda2014-07-04 01:53:266740void WebContentsImpl::OnFrameRemoved(RenderFrameHost* render_frame_host) {
ericwilligersde386342016-10-19 02:35:096741 for (auto& observer : observers_)
6742 observer.FrameDeleted(render_frame_host);
[email protected]9b159a52013-10-03 17:24:556743}
6744
[email protected]222f5822014-02-05 23:40:496745void WebContentsImpl::OnPreferredSizeChanged(const gfx::Size& old_size) {
6746 if (!delegate_)
6747 return;
6748 const gfx::Size new_size = GetPreferredSize();
6749 if (new_size != old_size)
6750 delegate_->UpdatePreferredSize(this, new_size);
6751}
6752
Dan Beam055fe8f2017-10-04 23:23:366753std::unique_ptr<WebUIImpl> WebContentsImpl::CreateWebUI(const GURL& url) {
Jeremy Roman04f27c372017-10-27 15:20:556754 std::unique_ptr<WebUIImpl> web_ui = std::make_unique<WebUIImpl>(this);
Lei Zhangee3b63ad2018-06-06 23:25:316755 std::unique_ptr<WebUIController> controller(
avie865b1d2016-10-24 19:42:596756 WebUIControllerFactoryRegistry::GetInstance()
Lei Zhangee3b63ad2018-06-06 23:25:316757 ->CreateWebUIControllerForURL(web_ui.get(), url));
dbeama1b926a2015-08-31 23:17:516758 if (controller) {
Lei Zhangee3b63ad2018-06-06 23:25:316759 web_ui->SetController(std::move(controller));
dbeama1b926a2015-08-31 23:17:516760 return web_ui;
6761 }
6762
avie865b1d2016-10-24 19:42:596763 return nullptr;
dbeama1b926a2015-08-31 23:17:516764}
6765
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:416766FindRequestManager* WebContentsImpl::GetFindRequestManager() {
6767 for (auto* contents = this; contents;
paulmeyerfeafc2d2017-04-25 21:46:406768 contents = contents->GetOuterWebContents()) {
6769 if (contents->find_request_manager_)
6770 return contents->find_request_manager_.get();
6771 }
6772
6773 return nullptr;
6774}
6775
paulmeyerc0b762b2016-04-13 11:55:176776FindRequestManager* WebContentsImpl::GetOrCreateFindRequestManager() {
paulmeyerfeafc2d2017-04-25 21:46:406777 if (FindRequestManager* manager = GetFindRequestManager())
6778 return manager;
6779
Ehsan Karamad6beb2ea2018-11-25 18:15:136780 DCHECK(!browser_plugin_guest_ || GetOuterWebContents());
6781
paulmeyerfeafc2d2017-04-25 21:46:406782 // No existing FindRequestManager found, so one must be created.
6783 find_request_manager_.reset(new FindRequestManager(this));
6784
6785 // Concurrent find sessions must not overlap, so destroy any existing
6786 // FindRequestManagers in any inner WebContentses.
Lucas Furukawa Gadani2ec00c82018-12-14 15:53:166787 for (WebContents* contents : GetWebContentsAndAllInner()) {
6788 auto* web_contents_impl = static_cast<WebContentsImpl*>(contents);
6789 if (web_contents_impl == this)
paulmeyerfeafc2d2017-04-25 21:46:406790 continue;
Lucas Furukawa Gadani2ec00c82018-12-14 15:53:166791 if (web_contents_impl->find_request_manager_) {
6792 web_contents_impl->find_request_manager_->StopFinding(
6793 STOP_FIND_ACTION_CLEAR_SELECTION);
6794 web_contents_impl->find_request_manager_.release();
paulmeyerfeafc2d2017-04-25 21:46:406795 }
6796 }
paulmeyerc0b762b2016-04-13 11:55:176797
6798 return find_request_manager_.get();
6799}
6800
6801void WebContentsImpl::NotifyFindReply(int request_id,
6802 int number_of_matches,
6803 const gfx::Rect& selection_rect,
6804 int active_match_ordinal,
6805 bool final_update) {
Sebastien Marchandcf97ed52017-07-24 23:50:346806 if (delegate_ && !is_being_destroyed_ &&
Lukasz Anforowicze9ae3722017-09-29 17:37:386807 !GetMainFrame()->GetProcess()->FastShutdownStarted()) {
paulmeyerc0b762b2016-04-13 11:55:176808 delegate_->FindReply(this, request_id, number_of_matches, selection_rect,
6809 active_match_ordinal, final_update);
6810 }
6811}
6812
ortuno467e5792016-06-10 04:32:396813void WebContentsImpl::IncrementBluetoothConnectedDeviceCount() {
6814 // Trying to invalidate the tab state while being destroyed could result in a
6815 // use after free.
6816 if (IsBeingDestroyed()) {
6817 return;
6818 }
6819 // Notify for UI updates if the state changes.
6820 bluetooth_connected_device_count_++;
6821 if (bluetooth_connected_device_count_ == 1) {
6822 NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB);
6823 }
6824}
6825
6826void WebContentsImpl::DecrementBluetoothConnectedDeviceCount() {
6827 // Trying to invalidate the tab state while being destroyed could result in a
6828 // use after free.
6829 if (IsBeingDestroyed()) {
6830 return;
6831 }
6832 // Notify for UI updates if the state changes.
Lukasz Anforowicze9ae3722017-09-29 17:37:386833 DCHECK_NE(bluetooth_connected_device_count_, 0u);
ortuno467e5792016-06-10 04:32:396834 bluetooth_connected_device_count_--;
6835 if (bluetooth_connected_device_count_ == 0) {
6836 NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB);
6837 }
6838}
6839
Reilly Grant5e7c79b22019-04-09 17:26:206840void WebContentsImpl::IncrementSerialActiveFrameCount() {
6841 // Trying to invalidate the tab state while being destroyed could result in a
6842 // use after free.
6843 if (IsBeingDestroyed())
6844 return;
6845
6846 // Notify for UI updates if the state changes.
6847 serial_active_frame_count_++;
6848 if (serial_active_frame_count_ == 1)
6849 NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB);
6850}
6851
6852void WebContentsImpl::DecrementSerialActiveFrameCount() {
6853 // Trying to invalidate the tab state while being destroyed could result in a
6854 // use after free.
6855 if (IsBeingDestroyed())
6856 return;
6857
6858 // Notify for UI updates if the state changes.
6859 DCHECK_NE(0u, serial_active_frame_count_);
6860 serial_active_frame_count_--;
6861 if (serial_active_frame_count_ == 0)
6862 NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB);
6863}
6864
Matt Reynoldse8c6c1f2019-11-02 09:53:536865void WebContentsImpl::IncrementHidActiveFrameCount() {
6866 // Trying to invalidate the tab state while being destroyed could result in a
6867 // use after free.
6868 if (IsBeingDestroyed())
6869 return;
6870
6871 // Notify for UI updates if the active frame count transitions from zero to
6872 // non-zero.
6873 hid_active_frame_count_++;
6874 if (hid_active_frame_count_ == 1)
6875 NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB);
6876}
6877
6878void WebContentsImpl::DecrementHidActiveFrameCount() {
6879 // Trying to invalidate the tab state while being destroyed could result in a
6880 // use after free.
6881 if (IsBeingDestroyed())
6882 return;
6883
6884 // Notify for UI updates if the active frame count transitions from non-zero
6885 // to zero.
6886 DCHECK_NE(0u, hid_active_frame_count_);
6887 hid_active_frame_count_--;
6888 if (hid_active_frame_count_ == 0)
6889 NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB);
6890}
6891
Marijn Kruisselbrink29051042019-08-06 22:56:556892void WebContentsImpl::IncrementNativeFileSystemHandleCount() {
6893 // Trying to invalidate the tab state while being destroyed could result in a
6894 // use after free.
6895 if (IsBeingDestroyed())
6896 return;
6897
6898 // Notify for UI updates if the state changes. Need both TYPE_TAB and TYPE_URL
6899 // to update both the tab-level usage indicator and the usage indicator in the
6900 // omnibox.
6901 native_file_system_handle_count_++;
6902 if (native_file_system_handle_count_ == 1) {
6903 NotifyNavigationStateChanged(static_cast<content::InvalidateTypes>(
6904 INVALIDATE_TYPE_TAB | INVALIDATE_TYPE_URL));
6905 }
6906}
6907
6908void WebContentsImpl::DecrementNativeFileSystemHandleCount() {
6909 // Trying to invalidate the tab state while being destroyed could result in a
6910 // use after free.
6911 if (IsBeingDestroyed())
6912 return;
6913
6914 // Notify for UI updates if the state changes. Need both TYPE_TAB and TYPE_URL
6915 // to update both the tab-level usage indicator and the usage indicator in the
6916 // omnibox.
6917 DCHECK_NE(0u, native_file_system_handle_count_);
6918 native_file_system_handle_count_--;
6919 if (native_file_system_handle_count_ == 0) {
6920 NotifyNavigationStateChanged(static_cast<content::InvalidateTypes>(
6921 INVALIDATE_TYPE_TAB | INVALIDATE_TYPE_URL));
6922 }
6923}
6924
Marijn Kruisselbrink46fa4bd2019-07-11 18:15:476925void WebContentsImpl::AddNativeFileSystemDirectoryHandle(
6926 const base::FilePath& path) {
Marijn Kruisselbrinkf1714aa22019-07-02 03:45:336927 // Trying to invalidate the tab state while being destroyed could result in a
6928 // use after free.
6929 if (IsBeingDestroyed())
6930 return;
6931
6932 // Notify for UI updates if the state changes. Need both TYPE_TAB and TYPE_URL
6933 // to update both the tab-level usage indicator and the usage indicator in the
6934 // omnibox.
Marijn Kruisselbrink46fa4bd2019-07-11 18:15:476935 const bool was_empty = native_file_system_directory_handles_.empty();
6936 native_file_system_directory_handles_[path]++;
6937 if (was_empty) {
Marijn Kruisselbrinkf1714aa22019-07-02 03:45:336938 NotifyNavigationStateChanged(static_cast<content::InvalidateTypes>(
6939 INVALIDATE_TYPE_TAB | INVALIDATE_TYPE_URL));
6940 }
6941}
6942
Marijn Kruisselbrink46fa4bd2019-07-11 18:15:476943void WebContentsImpl::RemoveNativeFileSystemDirectoryHandle(
6944 const base::FilePath& path) {
Marijn Kruisselbrinkf1714aa22019-07-02 03:45:336945 // Trying to invalidate the tab state while being destroyed could result in a
6946 // use after free.
6947 if (IsBeingDestroyed())
6948 return;
6949
6950 // Notify for UI updates if the state changes. Need both TYPE_TAB and TYPE_URL
6951 // to update both the tab-level usage indicator and the usage indicator in the
6952 // omnibox.
Marijn Kruisselbrink46fa4bd2019-07-11 18:15:476953 auto it = native_file_system_directory_handles_.find(path);
6954 DCHECK(it != native_file_system_directory_handles_.end());
6955 DCHECK_NE(0u, it->second);
6956 it->second--;
6957 if (it->second == 0)
6958 native_file_system_directory_handles_.erase(it);
6959 if (native_file_system_directory_handles_.empty()) {
Marijn Kruisselbrinkf1714aa22019-07-02 03:45:336960 NotifyNavigationStateChanged(static_cast<content::InvalidateTypes>(
6961 INVALIDATE_TYPE_TAB | INVALIDATE_TYPE_URL));
6962 }
6963}
6964
6965void WebContentsImpl::IncrementWritableNativeFileSystemHandleCount() {
6966 // Trying to invalidate the tab state while being destroyed could result in a
6967 // use after free.
6968 if (IsBeingDestroyed())
6969 return;
6970
6971 // Notify for UI updates if the state changes. Need both TYPE_TAB and TYPE_URL
6972 // to update both the tab-level usage indicator and the usage indicator in the
6973 // omnibox.
6974 native_file_system_writable_handle_count_++;
6975 if (native_file_system_writable_handle_count_ == 1) {
6976 NotifyNavigationStateChanged(static_cast<content::InvalidateTypes>(
6977 INVALIDATE_TYPE_TAB | INVALIDATE_TYPE_URL));
6978 }
6979}
6980
6981void WebContentsImpl::DecrementWritableNativeFileSystemHandleCount() {
6982 // Trying to invalidate the tab state while being destroyed could result in a
6983 // use after free.
6984 if (IsBeingDestroyed())
6985 return;
6986
6987 // Notify for UI updates if the state changes. Need both TYPE_TAB and TYPE_URL
6988 // to update both the tab-level usage indicator and the usage indicator in the
6989 // omnibox.
6990 DCHECK_NE(0u, native_file_system_writable_handle_count_);
6991 native_file_system_writable_handle_count_--;
6992 if (native_file_system_writable_handle_count_ == 0) {
6993 NotifyNavigationStateChanged(static_cast<content::InvalidateTypes>(
6994 INVALIDATE_TYPE_TAB | INVALIDATE_TYPE_URL));
6995 }
6996}
6997
zqzhang8ac49002017-03-16 21:51:356998void WebContentsImpl::SetHasPersistentVideo(bool has_persistent_video) {
6999 if (has_persistent_video_ == has_persistent_video)
mlamouri5cd9ae82017-02-18 11:05:097000 return;
7001
zqzhang8ac49002017-03-16 21:51:357002 has_persistent_video_ = has_persistent_video;
mlamouri5cd9ae82017-02-18 11:05:097003 NotifyPreferencesChanged();
zqzhang8ac49002017-03-16 21:51:357004 media_web_contents_observer()->RequestPersistentVideo(has_persistent_video);
mlamouri5cd9ae82017-02-18 11:05:097005}
7006
Ian Vollick5d5c37f2019-06-19 22:50:577007void WebContentsImpl::SetSpatialNavigationDisabled(bool disabled) {
7008 if (is_spatial_navigation_disabled_ == disabled)
7009 return;
7010
7011 is_spatial_navigation_disabled_ = disabled;
7012 NotifyPreferencesChanged();
7013}
7014
paulmeyerfeafc2d2017-04-25 21:46:407015void WebContentsImpl::BrowserPluginGuestWillDetach() {
7016 WebContentsImpl* outermost = GetOutermostWebContents();
7017 if (this != outermost && ContainsOrIsFocusedWebContents())
7018 outermost->SetAsFocusedWebContentsIfNecessary();
7019}
7020
Becca Hughes112832e2019-06-11 17:19:027021PictureInPictureResult WebContentsImpl::EnterPictureInPicture(
Mounir Lamouri11e9ef42018-05-22 03:10:167022 const viz::SurfaceId& surface_id,
7023 const gfx::Size& natural_size) {
François Beaufortf09e4462019-01-18 14:34:517024 return delegate_
7025 ? delegate_->EnterPictureInPicture(this, surface_id, natural_size)
Becca Hughes112832e2019-06-11 17:19:027026 : PictureInPictureResult::kNotSupported;
Mounir Lamouri11e9ef42018-05-22 03:10:167027}
7028
Mounir Lamouri6d759e12018-05-16 20:01:307029void WebContentsImpl::ExitPictureInPicture() {
7030 if (delegate_)
7031 delegate_->ExitPictureInPicture();
7032}
7033
paulmeyerc0b762b2016-04-13 11:55:177034#if defined(OS_ANDROID)
7035void WebContentsImpl::NotifyFindMatchRectsReply(
7036 int version,
7037 const std::vector<gfx::RectF>& rects,
7038 const gfx::RectF& active_rect) {
7039 if (delegate_)
7040 delegate_->FindMatchRectsReply(this, version, rects, active_rect);
7041}
7042#endif
7043
[email protected]34ff1cfc2014-08-20 06:16:057044void WebContentsImpl::SetForceDisableOverscrollContent(bool force_disable) {
7045 force_disable_overscroll_content_ = force_disable;
7046 if (view_)
7047 view_->SetOverscrollControllerEnabled(CanOverscrollContent());
7048}
7049
Jianzhou Fengd8720c72018-01-18 03:06:307050bool WebContentsImpl::SetDeviceEmulationSize(const gfx::Size& new_size) {
7051 device_emulation_size_ = new_size;
7052 RenderWidgetHostView* rwhv = GetMainFrame()->GetView();
7053
7054 const gfx::Size current_size = rwhv->GetViewBounds().size();
7055 if (view_size_before_emulation_.IsEmpty())
7056 view_size_before_emulation_ = current_size;
7057
7058 if (current_size != new_size)
7059 rwhv->SetSize(new_size);
7060
7061 return current_size != new_size;
7062}
7063
7064void WebContentsImpl::ClearDeviceEmulationSize() {
7065 RenderWidgetHostView* rwhv = GetMainFrame()->GetView();
7066 // WebContentsView could get resized during emulation, which also resizes
7067 // RWHV. If it happens, assume user would like to keep using the size after
7068 // emulation.
7069 // TODO(jzfeng): Prohibit resizing RWHV through any other means (at least when
7070 // WebContentsView size changes).
Dmitry Gozmanc63707182018-03-06 00:09:397071 if (!view_size_before_emulation_.IsEmpty() && rwhv &&
7072 rwhv->GetViewBounds().size() == device_emulation_size_) {
Jianzhou Fengd8720c72018-01-18 03:06:307073 rwhv->SetSize(view_size_before_emulation_);
Dmitry Gozmanc63707182018-03-06 00:09:397074 }
Jianzhou Fengd8720c72018-01-18 03:06:307075 device_emulation_size_ = gfx::Size();
7076 view_size_before_emulation_ = gfx::Size();
7077}
7078
Max Morin5bc74f52018-05-09 07:00:217079ForwardingAudioStreamFactory* WebContentsImpl::GetAudioStreamFactory() {
7080 if (!audio_stream_factory_) {
7081 audio_stream_factory_.emplace(
7082 this,
Max Morinddebb972018-09-20 10:04:147083 // BrowserMainLoop::GetInstance() may be null in unit tests.
7084 BrowserMainLoop::GetInstance()
7085 ? static_cast<media::UserInputMonitorBase*>(
7086 BrowserMainLoop::GetInstance()->user_input_monitor())
7087 : nullptr,
Ken Rockot72964402019-12-06 10:40:517088 AudioStreamBrokerFactory::CreateImpl());
Max Morin5bc74f52018-05-09 07:00:217089 }
7090
7091 return &*audio_stream_factory_;
7092}
7093
dalecurtis88c240072015-12-09 02:11:187094void WebContentsImpl::MediaStartedPlaying(
billorr21c005d2016-11-17 03:57:047095 const WebContentsObserver::MediaPlayerInfo& media_info,
Mounir Lamouri0f5bdf52019-04-04 16:32:357096 const MediaPlayerId& id) {
billorr21c005d2016-11-17 03:57:047097 if (media_info.has_video)
7098 currently_playing_video_count_++;
7099
ericwilligersde386342016-10-19 02:35:097100 for (auto& observer : observers_)
billorr21c005d2016-11-17 03:57:047101 observer.MediaStartedPlaying(media_info, id);
dalecurtis88c240072015-12-09 02:11:187102}
7103
7104void WebContentsImpl::MediaStoppedPlaying(
billorr21c005d2016-11-17 03:57:047105 const WebContentsObserver::MediaPlayerInfo& media_info,
Mounir Lamouri0f5bdf52019-04-04 16:32:357106 const MediaPlayerId& id,
Becca Hughes220bfc102017-11-14 18:24:077107 WebContentsObserver::MediaStoppedReason reason) {
billorr21c005d2016-11-17 03:57:047108 if (media_info.has_video)
7109 currently_playing_video_count_--;
7110
ericwilligersde386342016-10-19 02:35:097111 for (auto& observer : observers_)
Becca Hughes220bfc102017-11-14 18:24:077112 observer.MediaStoppedPlaying(media_info, id, reason);
billorr21c005d2016-11-17 03:57:047113}
7114
Mounir Lamouri0f5bdf52019-04-04 16:32:357115void WebContentsImpl::MediaResized(const gfx::Size& size,
7116 const MediaPlayerId& id) {
peconn257951522017-06-09 18:24:597117 cached_video_sizes_[id] = size;
7118
7119 for (auto& observer : observers_)
7120 observer.MediaResized(size, id);
7121}
7122
Peter E Conneccb34c22017-09-08 09:37:587123void WebContentsImpl::MediaEffectivelyFullscreenChanged(bool is_fullscreen) {
7124 for (auto& observer : observers_)
7125 observer.MediaEffectivelyFullscreenChanged(is_fullscreen);
7126}
7127
Peter Conn31c726a2017-08-17 11:07:347128base::Optional<gfx::Size> WebContentsImpl::GetFullscreenVideoSize() {
Mounir Lamouri0f5bdf52019-04-04 16:32:357129 base::Optional<MediaPlayerId> id =
Peter Conn31c726a2017-08-17 11:07:347130 media_web_contents_observer_->GetFullscreenVideoMediaPlayerId();
Jan Wilken Dörrie531be7ca2019-06-07 10:05:577131 if (id && base::Contains(cached_video_sizes_, id.value()))
Peter Conn31c726a2017-08-17 11:07:347132 return base::Optional<gfx::Size>(cached_video_sizes_[id.value()]);
Lei Zhang6410ec1a2019-01-31 21:38:587133 return base::nullopt;
peconn257951522017-06-09 18:24:597134}
7135
billorr21c005d2016-11-17 03:57:047136int WebContentsImpl::GetCurrentlyPlayingVideoCount() {
7137 return currently_playing_video_count_;
dalecurtis88c240072015-12-09 02:11:187138}
7139
Raymond Toye413650c2018-08-31 00:12:477140void WebContentsImpl::AudioContextPlaybackStarted(RenderFrameHost* host,
7141 int context_id) {
7142 WebContentsObserver::AudioContextId audio_context_id(host, context_id);
7143 for (auto& observer : observers_)
7144 observer.AudioContextPlaybackStarted(audio_context_id);
7145}
7146
7147void WebContentsImpl::AudioContextPlaybackStopped(RenderFrameHost* host,
7148 int context_id) {
7149 WebContentsObserver::AudioContextId audio_context_id(host, context_id);
7150 for (auto& observer : observers_)
7151 observer.AudioContextPlaybackStopped(audio_context_id);
7152}
7153
evliufc233aa2019-10-31 19:53:037154void WebContentsImpl::MediaWatchTimeChanged(
7155 const content::MediaPlayerWatchTime& watch_time) {
7156 for (auto& observer : observers_)
7157 observer.MediaWatchTimeChanged(watch_time);
7158}
7159
evliu7f736f02019-11-07 17:43:507160media::MediaMetricsProvider::RecordAggregateWatchTimeCallback
7161WebContentsImpl::GetRecordAggregateWatchTimeCallback() {
7162 return base::BindRepeating(
7163 [](base::WeakPtr<RenderFrameHostDelegate> delegate,
7164 GURL last_committed_url, base::TimeDelta total_watch_time,
evliua283c9e2019-11-08 22:57:227165 base::TimeDelta time_stamp, bool has_video, bool has_audio) {
7166 content::MediaPlayerWatchTime watch_time(
7167 last_committed_url, last_committed_url.GetOrigin(),
7168 total_watch_time, time_stamp, has_video, has_audio);
evliu7f736f02019-11-07 17:43:507169
7170 // Save the watch time if the delegate is still alive.
7171 if (delegate)
7172 delegate->MediaWatchTimeChanged(watch_time);
7173 },
7174 weak_factory_.GetWeakPtr(), GetMainFrameLastCommittedURL());
7175}
7176
Ehsan Karamad1c631762019-05-13 23:04:017177RenderFrameHostImpl* WebContentsImpl::GetMainFrameForInnerDelegate(
7178 FrameTreeNode* frame_tree_node) {
7179 if (auto* web_contents = node_.GetInnerWebContentsInFrame(frame_tree_node))
7180 return web_contents->GetMainFrame();
7181 return nullptr;
7182}
7183
John Budorickee1c9512019-10-17 15:32:197184bool WebContentsImpl::IsFrameLowPriority(
7185 const RenderFrameHost* render_frame_host) {
7186 if (!delegate_)
7187 return false;
7188 return delegate_->IsFrameLowPriority(this, render_frame_host);
7189}
7190
Francois Doraye6161152018-03-27 22:05:377191void WebContentsImpl::UpdateWebContentsVisibility(Visibility visibility) {
Francois Doray3f2afbd2018-04-06 19:18:187192 // Occlusion is disabled when |features::kWebContentsOcclusion| is disabled
7193 // (for power and speed impact assessment) or when
7194 // |switches::kDisableBackgroundingOccludedWindowsForTesting| is specified on
7195 // the command line (to avoid flakiness in browser tests).
7196 const bool occlusion_is_disabled =
7197 !base::FeatureList::IsEnabled(features::kWebContentsOcclusion) ||
Francois Doraye6161152018-03-27 22:05:377198 base::CommandLine::ForCurrentProcess()->HasSwitch(
7199 switches::kDisableBackgroundingOccludedWindowsForTesting);
7200 if (occlusion_is_disabled && visibility == Visibility::OCCLUDED)
7201 visibility = Visibility::VISIBLE;
7202
tapted65ff2ea72016-03-01 23:39:007203 if (!did_first_set_visible_) {
Francois Doraye6161152018-03-27 22:05:377204 if (visibility == Visibility::VISIBLE) {
7205 // A WebContents created with CreateParams::initially_hidden = false
7206 // starts with GetVisibility() == Visibility::VISIBLE even though it is
7207 // not really visible. Call WasShown() when it becomes visible for real as
7208 // the page load mechanism and some WebContentsObserver rely on that.
tapted65ff2ea72016-03-01 23:39:007209 WasShown();
Francois Dorayfe4a1772018-02-17 04:17:097210 did_first_set_visible_ = true;
tapted65ff2ea72016-03-01 23:39:007211 }
Francois Doraye6161152018-03-27 22:05:377212
7213 // Trust the initial visibility of the WebContents and do not switch it to
7214 // HIDDEN or OCCLUDED before it becomes VISIBLE for real. Doing so would
7215 // result in destroying resources that would immediately be recreated (e.g.
7216 // UpdateWebContents(HIDDEN) can be called when a WebContents is added to a
7217 // hidden window that is about to be shown).
7218
tapted65ff2ea72016-03-01 23:39:007219 return;
7220 }
Francois Doraye6161152018-03-27 22:05:377221
7222 if (visibility == visibility_)
tapted65ff2ea72016-03-01 23:39:007223 return;
7224
Collin Baker989e0882019-11-01 01:27:177225 UpdateVisibilityAndNotifyPageAndView(visibility);
tapted65ff2ea72016-03-01 23:39:007226}
7227
aelias5252baa2016-04-10 01:18:027228void WebContentsImpl::UpdateOverridingUserAgent() {
mlamouri5cd9ae82017-02-18 11:05:097229 NotifyPreferencesChanged();
aelias5252baa2016-04-10 01:18:027230}
7231
avid53461d2016-02-25 17:15:047232void WebContentsImpl::SetJavaScriptDialogManagerForTesting(
7233 JavaScriptDialogManager* dialog_manager) {
7234 dialog_manager_ = dialog_manager;
7235}
7236
Julie Jeongeun Kim7d0986172019-12-04 03:17:257237void WebContentsImpl::RemoveReceiverSet(const std::string& interface_name) {
7238 auto it = receiver_sets_.find(interface_name);
7239 if (it != receiver_sets_.end())
7240 receiver_sets_.erase(it);
rockotf62002a2016-09-15 00:08:597241}
7242
nzolghadrd87a308d2016-12-07 15:45:567243bool WebContentsImpl::AddDomainInfoToRapporSample(rappor::Sample* sample) {
nzolghadrbbffc6c2017-05-25 17:56:397244 // Here we associate this metric to the main frame URL regardless of what
7245 // caused it.
nzolghadrd87a308d2016-12-07 15:45:567246 sample->SetStringField("Domain", ::rappor::GetDomainAndRegistrySampleFromGURL(
7247 GetLastCommittedURL()));
nzolghadrd87a308d2016-12-07 15:45:567248 return true;
7249}
7250
falken52a56e32016-12-08 05:02:407251void WebContentsImpl::ShowInsecureLocalhostWarningIfNeeded() {
7252 bool allow_localhost = base::CommandLine::ForCurrentProcess()->HasSwitch(
7253 switches::kAllowInsecureLocalhost);
7254 if (!allow_localhost)
7255 return;
7256
7257 content::NavigationEntry* entry = GetController().GetLastCommittedEntry();
Rob Wuf79b3ba2018-01-14 01:54:317258 if (!entry || !net::IsLocalhost(entry->GetURL()))
falken52a56e32016-12-08 05:02:407259 return;
7260
7261 content::SSLStatus ssl_status = entry->GetSSL();
Matt Mueller936511442019-09-03 18:15:127262 if (!net::IsCertStatusError(ssl_status.cert_status))
falken52a56e32016-12-08 05:02:407263 return;
7264
7265 GetMainFrame()->AddMessageToConsole(
Abhijeet Kandalkara5928a0a2019-03-13 05:04:427266 blink::mojom::ConsoleMessageLevel::kWarning,
falken52a56e32016-12-08 05:02:407267 base::StringPrintf("This site does not have a valid SSL "
7268 "certificate! Without SSL, your site's and "
7269 "visitors' data is vulnerable to theft and "
7270 "tampering. Get a valid SSL certificate before"
7271 " releasing your website to the public."));
7272}
7273
ekaramadf6750aa2017-06-06 18:29:427274bool WebContentsImpl::IsShowingContextMenuOnPage() const {
7275 return showing_context_menu_;
7276}
7277
Min Qina904f3302018-02-13 23:33:347278download::DownloadUrlParameters::RequestHeadersType
7279WebContentsImpl::ParseDownloadHeaders(const std::string& headers) {
7280 download::DownloadUrlParameters::RequestHeadersType request_headers;
Megan Jablonski2f6a4c52017-07-10 23:01:257281 for (const base::StringPiece& key_value : base::SplitStringPiece(
7282 headers, "\r\n", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY)) {
7283 std::vector<std::string> pair = base::SplitString(
7284 key_value, ":", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
7285 if (2ul == pair.size())
7286 request_headers.push_back(make_pair(pair[0], pair[1]));
7287 }
7288 return request_headers;
7289}
7290
lukasza6f8ac622017-06-06 03:10:207291void WebContentsImpl::SetOpenerForNewContents(FrameTreeNode* opener,
7292 bool opener_suppressed) {
7293 if (opener) {
7294 FrameTreeNode* new_root = GetFrameTree()->root();
7295
7296 // For the "original opener", track the opener's main frame instead, because
7297 // if the opener is a subframe, the opener tracking could be easily bypassed
7298 // by spawning from a subframe and deleting the subframe.
7299 // https://crbug.com/705316
7300 new_root->SetOriginalOpener(opener->frame_tree()->root());
7301
7302 if (!opener_suppressed) {
7303 new_root->SetOpener(opener);
7304 created_with_opener_ = true;
7305 }
7306 }
7307}
7308
Mounir Lamouri0f5bdf52019-04-04 16:32:357309void WebContentsImpl::MediaMutedStatusChanged(const MediaPlayerId& id,
7310 bool muted) {
Becca Hughes9f6fd4b82017-06-15 10:01:407311 for (auto& observer : observers_)
7312 observer.MediaMutedStatusChanged(id, muted);
7313}
7314
EhsanK2075c7e2017-08-21 02:42:397315void WebContentsImpl::SetVisibilityForChildViews(bool visible) {
7316 GetMainFrame()->SetVisibilityForChildViews(visible);
7317}
7318
Dave Tapuskae782bea2019-07-09 16:21:517319mojom::FrameInputHandler* WebContentsImpl::GetFocusedFrameInputHandler() {
7320 auto* focused_frame = GetFocusedFrame();
7321 if (!focused_frame)
7322 return nullptr;
7323 return focused_frame->GetFrameInputHandler();
7324}
7325
[email protected]8ff00d72012-10-23 19:12:217326} // namespace content