blob: 8893afd94126b46bbd16909cc529dc6cace9184e [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"
jinsukkim79df88a2017-01-04 06:26:1518#include "base/i18n/character_encoding.h"
[email protected]cbb1ef592013-06-05 19:49:4619#include "base/lazy_instance.h"
skyostil95082a62015-06-05 19:53:0720#include "base/location.h"
[email protected]6d636e62013-07-31 03:15:5621#include "base/logging.h"
avib7348942015-12-25 20:57:1022#include "base/macros.h"
fdorayba121422016-12-23 19:51:4823#include "base/memory/ptr_util.h"
dmazzonid95ae842016-04-12 21:17:3924#include "base/memory/ref_counted.h"
asvitkine8d51e9d2016-09-02 23:55:4325#include "base/metrics/histogram_macros.h"
bratell0a7406f2017-03-28 07:46:3726#include "base/metrics/user_metrics.h"
Avi Drissman4891b852018-09-25 18:12:5827#include "base/no_destructor.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"
James Wallace-Leeeafc94cb92018-07-23 21:35:0944#include "content/browser/accessibility/accessibility_tree_formatter_blink.h"
jamescookda2505812015-03-20 18:01:1845#include "content/browser/bad_message.h"
Max Morinddebb972018-09-20 10:04:1446#include "content/browser/browser_main_loop.h"
[email protected]7a846df2012-09-20 19:17:3947#include "content/browser/browser_plugin/browser_plugin_embedder.h"
48#include "content/browser/browser_plugin/browser_plugin_guest.h"
[email protected]b9535422012-02-09 01:47:5949#include "content/browser/child_process_security_policy_impl.h"
Andrey Lushnikov81b992e2019-06-27 23:27:2150#include "content/browser/devtools/devtools_instrumentation.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"
clamy0e119882015-07-31 16:12:3363#include "content/browser/frame_host/navigation_handle_impl.h"
Arthur Hemeryc3380172018-01-22 14:00:1764#include "content/browser/frame_host/navigation_request.h"
[email protected]6ea6bdf2013-12-06 13:35:0165#include "content/browser/frame_host/navigator_impl.h"
[email protected]52913802013-12-10 05:52:1866#include "content/browser/frame_host/render_frame_host_impl.h"
lfgdb5c4ed2016-03-04 23:09:0767#include "content/browser/frame_host/render_frame_proxy_host.h"
Kenneth Russell954ed9ce2018-04-12 23:07:0168#include "content/browser/gpu/gpu_data_manager_impl.h"
mlamouriefdca9d2014-09-16 16:55:4069#include "content/browser/manifest/manifest_manager_host.h"
Max Morin5bc74f52018-05-09 07:00:2170#include "content/browser/media/audio_stream_broker.h"
dalecurtisbc6572e12014-09-12 19:22:3071#include "content/browser/media/audio_stream_monitor.h"
miu50f97892014-09-22 22:49:5272#include "content/browser/media/capture/web_contents_audio_muter.h"
dalecurtis88c240072015-12-09 02:11:1873#include "content/browser/media/media_web_contents_observer.h"
zqzhang1adf3cb2016-11-01 11:26:4574#include "content/browser/media/session/media_session_impl.h"
tommyclieb25b2a2014-11-03 19:45:0975#include "content/browser/plugin_content_origin_whitelist.h"
[email protected]f3b1a082011-11-18 00:34:3076#include "content/browser/renderer_host/render_process_host_impl.h"
[email protected]21161032014-05-05 16:56:5077#include "content/browser/renderer_host/render_view_host_delegate_view.h"
[email protected]b3c41c0b2012-03-06 15:48:3278#include "content/browser/renderer_host/render_view_host_impl.h"
[email protected]bafe6cd2012-05-23 23:09:5079#include "content/browser/renderer_host/render_widget_host_impl.h"
kenrb2a565f82015-09-02 20:24:5980#include "content/browser/renderer_host/render_widget_host_input_event_router.h"
[email protected]cfd80b02014-05-01 17:46:4881#include "content/browser/renderer_host/render_widget_host_view_base.h"
Ken Buchanandaef006b2017-08-17 18:32:1582#include "content/browser/renderer_host/render_widget_host_view_child_frame.h"
ekaramadadd882292016-06-08 15:22:5683#include "content/browser/renderer_host/text_input_manager.h"
leon.han552e9de2017-02-09 14:37:3084#include "content/browser/screen_orientation/screen_orientation_provider.h"
[email protected]b6583592012-01-25 19:52:3385#include "content/browser/site_instance_impl.h"
Charlie Reis96077362019-07-04 00:34:0486#include "content/browser/web_contents/javascript_dialog_navigation_deferrer.h"
lfgc740d4b22016-04-15 16:45:3387#include "content/browser/web_contents/web_contents_view_child_frame.h"
[email protected]b5a40842012-11-28 15:26:1188#include "content/browser/web_contents/web_contents_view_guest.h"
[email protected]eb2ef212013-01-29 04:27:5889#include "content/browser/webui/generic_handler.h"
[email protected]86a0a6e2013-01-28 06:33:0390#include "content/browser/webui/web_ui_controller_factory_registry.h"
[email protected]d2353452012-01-19 19:53:5691#include "content/browser/webui/web_ui_impl.h"
[email protected]8eb04562013-03-06 03:41:1492#include "content/common/browser_plugin/browser_plugin_constants.h"
[email protected]703dd662013-03-05 07:37:4293#include "content/common/browser_plugin/browser_plugin_messages.h"
Paul Meyer0c58c3712016-11-17 22:59:5194#include "content/common/drag_messages.h"
[email protected]f114fa42013-12-06 17:06:4495#include "content/common/frame_messages.h"
[email protected]1f3fc1d2014-04-03 14:50:1796#include "content/common/input_messages.h"
lfgdb5c4ed2016-03-04 23:09:0797#include "content/common/page_messages.h"
nasko34634672016-07-29 18:46:0698#include "content/common/page_state_serialization.h"
rockot5c478a72016-09-28 23:14:1899#include "content/common/render_message_filter.mojom.h"
[email protected]2c5569662011-03-22 20:45:02100#include "content/common/view_messages.h"
Albert J. Wong3c93c182018-09-27 17:29:43101#include "content/common/widget_messages.h"
[email protected]31a71eaf2014-03-13 01:47:36102#include "content/public/browser/ax_event_notification_details.h"
[email protected]ccb797302011-12-15 16:55:11103#include "content/public/browser/browser_context.h"
[email protected]139355f2014-05-11 14:21:28104#include "content/public/browser/browser_plugin_guest_manager.h"
Eric Seckler8652dcd52018-09-20 10:42:28105#include "content/public/browser/browser_task_traits.h"
Gabriel Charette790754c2018-03-16 21:32:59106#include "content/public/browser/browser_thread.h"
[email protected]87f3c082011-10-19 18:07:44107#include "content/public/browser/content_browser_client.h"
[email protected]e582fdd2011-12-20 16:48:17108#include "content/public/browser/download_manager.h"
Kent Tamura512a27e2018-10-04 00:49:32109#include "content/public/browser/file_select_listener.h"
ekaramada110f642016-12-21 19:47:28110#include "content/public/browser/focused_node_details.h"
ekaramad1beabecc2016-09-23 20:18:17111#include "content/public/browser/guest_mode.h"
[email protected]d9083482012-01-06 00:38:46112#include "content/public/browser/invalidate_type.h"
[email protected]71a88bb2013-02-01 22:05:15113#include "content/public/browser/javascript_dialog_manager.h"
skyf65d9bb2017-03-24 02:26:39114#include "content/public/browser/keyboard_event_processing_result.h"
[email protected]09d31d52012-03-11 22:30:27115#include "content/public/browser/load_notification_details.h"
[email protected]5b96836f2011-12-22 07:39:00116#include "content/public/browser/navigation_details.h"
[email protected]375fa1b2012-05-22 22:05:37117#include "content/public/browser/notification_details.h"
[email protected]be2510c02012-05-28 14:52:14118#include "content/public/browser/notification_service.h"
scottmg011d96e32016-06-29 19:15:08119#include "content/public/browser/notification_types.h"
[email protected]95640212014-07-26 18:14:30120#include "content/public/browser/render_widget_host_iterator.h"
Lukasz Anforowicze1b954d92017-10-30 21:28:06121#include "content/public/browser/restore_type.h"
estark5ea80e52015-06-19 18:43:12122#include "content/public/browser/security_style_explanations.h"
falken52a56e32016-12-08 05:02:40123#include "content/public/browser/ssl_status.h"
[email protected]4c3a23582012-08-18 08:54:34124#include "content/public/browser/storage_partition.h"
Ken Rockotfd03e682019-06-20 21:13:54125#include "content/public/browser/system_connector.h"
rockotf62002a2016-09-15 00:08:59126#include "content/public/browser/web_contents_binding_set.h"
[email protected]674bc592011-12-20 23:00:42127#include "content/public/browser/web_contents_delegate.h"
Lei Zhangee3b63ad2018-06-06 23:25:31128#include "content/public/browser/web_ui_controller.h"
[email protected]e091df82011-10-11 18:13:21129#include "content/public/common/bindings_policy.h"
alexmosc9e76ec2016-05-16 22:59:37130#include "content/public/common/child_process_host.h"
[email protected]54087fe2011-10-28 22:02:48131#include "content/public/common/content_constants.h"
Francois Doray3f2afbd2018-04-06 19:18:18132#include "content/public/common/content_features.h"
[email protected]64d69de42012-02-06 00:19:54133#include "content/public/common/content_switches.h"
Lukasz Anforowicze1b954d92017-10-30 21:28:06134#include "content/public/common/page_state.h"
[email protected]7940b8e2013-07-25 23:08:49135#include "content/public/common/page_zoom.h"
Leon Han963dc182018-11-06 05:41:48136#include "content/public/common/referrer_type_converters.h"
[email protected]04cbd3d2013-12-04 04:58:20137#include "content/public/common/result_codes.h"
[email protected]f16cc6782013-12-16 23:42:57138#include "content/public/common/url_utils.h"
[email protected]b3a97b52014-07-09 06:25:05139#include "content/public/common/web_preferences.h"
Max Morinddebb972018-09-20 10:04:14140#include "media/base/user_input_monitor.h"
wjmaclean64951902016-04-29 20:59:12141#include "net/base/url_util.h"
[email protected]8bfc8272013-09-09 20:10:53142#include "net/http/http_cache.h"
143#include "net/http/http_transaction_factory.h"
rhalavati0fbaeef32017-06-01 15:56:30144#include "net/traffic_annotation/network_traffic_annotation.h"
[email protected]8bfc8272013-09-09 20:10:53145#include "net/url_request/url_request_context.h"
[email protected]abe2c032011-03-31 18:49:34146#include "net/url_request/url_request_context_getter.h"
Scott Violet02e38b92018-03-27 23:42:14147#include "ppapi/buildflags/buildflags.h"
Ke He31d0bb02018-02-24 07:16:24148#include "services/device/public/mojom/constants.mojom.h"
Mounir Lamourifd9dcefa2017-07-06 10:26:55149#include "services/metrics/public/cpp/ukm_recorder.h"
John Abd-El-Malekb175a612018-12-03 20:11:58150#include "services/network/public/cpp/features.h"
Ke He7319dbe2017-11-09 05:54:44151#include "services/service_manager/public/cpp/connector.h"
rockot734fb662016-10-15 16:41:30152#include "services/service_manager/public/cpp/interface_provider.h"
Becca Hughes6daf5662018-06-27 16:50:54153#include "third_party/blink/public/common/associated_interfaces/associated_interface_provider.h"
Blink Reformata30d4232018-04-07 15:31:06154#include "third_party/blink/public/common/frame/sandbox_flags.h"
155#include "third_party/blink/public/common/mime_util/mime_util.h"
Takuto Ikutaaa3b796c2019-02-06 02:54:56156#include "third_party/blink/public/mojom/loader/pause_subresource_loading_handle.mojom.h"
Antonio Gomes0d42960a2019-06-05 12:35:51157#include "third_party/blink/public/mojom/mediastream/media_stream.mojom-shared.h"
Blink Reformata30d4232018-04-07 15:31:06158#include "third_party/blink/public/platform/web_security_style.h"
halton.huoca2eabd2015-07-06 08:17:40159#include "third_party/skia/include/core/SkBitmap.h"
dmazzonid95ae842016-04-12 21:17:39160#include "ui/accessibility/ax_tree_combiner.h"
[email protected]a08412b62012-05-29 21:28:56161#include "ui/base/layout.h"
Daniel Cheng90196c82018-04-25 21:49:14162#include "ui/events/base_event_utils.h"
chongz4a9751942016-08-17 17:49:39163#include "ui/events/blink/web_input_event_traits.h"
[email protected]66bd5512012-08-01 02:02:52164#include "ui/gl/gl_switches.h"
[email protected]3c733bde2010-12-21 19:56:31165
ekaramada110f642016-12-21 19:47:28166#if defined(OS_WIN)
167#include "content/browser/renderer_host/dip_util.h"
168#include "ui/gfx/geometry/dip_util.h"
169#endif
170
[email protected]583418cc2013-01-17 14:01:10171#if defined(OS_ANDROID)
mfomitchev2b8b066a2016-01-28 19:23:15172#include "content/browser/android/date_time_chooser_android.h"
sammcf5f1b0f2016-09-20 23:05:11173#include "content/browser/android/java_interfaces_impl.h"
[email protected]155c7f22013-12-09 17:07:18174#include "content/browser/web_contents/web_contents_android.h"
Ke He31d0bb02018-02-24 07:16:24175#include "services/device/public/mojom/nfc.mojom.h"
mcnee336ea2c2017-05-23 22:50:59176#else // !OS_ANDROID
177#include "content/browser/host_zoom_map_impl.h"
178#include "content/browser/host_zoom_map_observer.h"
mfomitchev2b8b066a2016-01-28 19:23:15179#endif // OS_ANDROID
[email protected]583418cc2013-01-17 14:01:10180
brettw4b461082016-11-19 18:55:16181#if BUILDFLAG(ENABLE_PLUGINS)
zqzhang181047e62016-07-01 13:37:17182#include "content/browser/media/session/pepper_playback_observer.h"
Lei Zhangebcc6302018-01-12 19:46:45183#endif
zqzhang181047e62016-07-01 13:37:17184
[email protected]8ff00d72012-10-23 19:12:21185namespace content {
[email protected]420ae012009-04-24 05:16:32186namespace {
187
[email protected]960b0372014-05-19 18:01:00188const int kMinimumDelayBetweenLoadingUpdatesMS = 100;
[email protected]82114f52012-03-20 22:53:41189const char kDotGoogleDotCom[] = ".google.com";
[email protected]ca406032011-07-19 21:53:05190
Avi Drissmana9114822018-03-23 18:25:55191base::LazyInstance<std::vector<
192 WebContentsImpl::FriendWrapper::CreatedCallback>>::DestructorAtExit
193 g_created_callbacks = LAZY_INSTANCE_INITIALIZER;
[email protected]cbb1ef592013-06-05 19:49:46194
dcheng57e39e22016-01-21 00:25:38195bool HasMatchingProcess(FrameTree* tree, int render_process_id) {
196 for (FrameTreeNode* node : tree->Nodes()) {
197 if (node->current_frame_host()->GetProcess()->GetID() == render_process_id)
198 return true;
alexmos4cf2aa32015-07-15 23:40:43199 }
dcheng57e39e22016-01-21 00:25:38200 return false;
[email protected]95640212014-07-26 18:14:30201}
202
kenrb19221852016-04-29 17:21:40203bool HasMatchingWidgetHost(FrameTree* tree, RenderWidgetHost* host) {
204 // This method scans the frame tree rather than checking whether
205 // host->delegate() == this, which allows it to return false when the host
206 // for a frame that is pending or pending deletion.
207 if (!host)
208 return false;
209
210 for (FrameTreeNode* node : tree->Nodes()) {
211 if (node->current_frame_host()->GetRenderWidgetHost() == host)
212 return true;
213 }
214 return false;
215}
216
dmazzonif8a31042016-12-03 00:52:13217void UpdateAccessibilityModeOnFrame(RenderFrameHost* frame_host) {
218 static_cast<RenderFrameHostImpl*>(frame_host)->UpdateAccessibilityMode();
[email protected]95640212014-07-26 18:14:30219}
220
dtseng0dd3fa12015-07-22 19:00:52221void ResetAccessibility(RenderFrameHost* rfh) {
222 static_cast<RenderFrameHostImpl*>(rfh)->AccessibilityReset();
223}
224
paulmeyerfeafc2d2017-04-25 21:46:40225// Helper for GetInnerWebContents().
Lucas Furukawa Gadani2ec00c82018-12-14 15:53:16226bool GetInnerWebContentsHelper(std::vector<WebContents*>* all_guest_contents,
227 WebContents* guest_contents) {
Ehsan Karamad6beb2ea2018-11-25 18:15:13228 auto* web_contents_impl = static_cast<WebContentsImpl*>(guest_contents);
229 if (web_contents_impl->GetBrowserPluginGuest()->attached() &&
230 !GuestMode::IsCrossProcessFrameGuest(web_contents_impl)) {
231 all_guest_contents->push_back(web_contents_impl);
232 }
paulmeyerfeafc2d2017-04-25 21:46:40233 return false;
234}
235
Ian Clelland5cbaaf82017-11-27 22:00:03236RenderFrameHostImpl* FindOpenerRFH(const WebContents::CreateParams& params) {
237 RenderFrameHostImpl* opener_rfh = nullptr;
alexmose201c7cd2015-06-10 17:14:21238 if (params.opener_render_frame_id != MSG_ROUTING_NONE) {
Ian Clelland5cbaaf82017-11-27 22:00:03239 opener_rfh = RenderFrameHostImpl::FromID(params.opener_render_process_id,
240 params.opener_render_frame_id);
alexmose201c7cd2015-06-10 17:14:21241 }
Ian Clelland5cbaaf82017-11-27 22:00:03242 return opener_rfh;
lukasza6f8ac622017-06-06 03:10:20243}
244
Dominick Ng37a15052018-07-17 23:56:35245// Returns |true| if |type| is the kind of user input that should trigger the
246// user interaction observers.
247bool IsUserInteractionInputType(blink::WebInputEvent::Type type) {
248 // Ideally, this list would be based more off of
249 // https://whatwg.org/C/interaction.html#triggered-by-user-activation.
250 return type == blink::WebInputEvent::kMouseDown ||
251 type == blink::WebInputEvent::kGestureScrollBegin ||
252 type == blink::WebInputEvent::kTouchStart ||
253 type == blink::WebInputEvent::kRawKeyDown;
254}
255
Pavel Feldman3c1842b2017-08-02 05:00:16256// Ensures that OnDialogClosed is only called once.
257class CloseDialogCallbackWrapper
258 : public base::RefCountedThreadSafe<CloseDialogCallbackWrapper> {
259 public:
Avi Drissmane04d3992017-10-05 15:11:36260 using CloseCallback =
261 base::OnceCallback<void(bool, bool, const base::string16&)>;
262
263 explicit CloseDialogCallbackWrapper(CloseCallback callback)
264 : callback_(std::move(callback)) {}
Pavel Feldman3c1842b2017-08-02 05:00:16265
266 void Run(bool dialog_was_suppressed,
267 bool success,
268 const base::string16& user_input) {
Avi Drissmane04d3992017-10-05 15:11:36269 if (callback_.is_null())
Pavel Feldman3c1842b2017-08-02 05:00:16270 return;
Avi Drissmane04d3992017-10-05 15:11:36271 std::move(callback_).Run(dialog_was_suppressed, success, user_input);
Pavel Feldman3c1842b2017-08-02 05:00:16272 }
273
274 private:
275 friend class base::RefCountedThreadSafe<CloseDialogCallbackWrapper>;
276 ~CloseDialogCallbackWrapper() {}
Avi Drissmane04d3992017-10-05 15:11:36277
278 CloseCallback callback_;
Pavel Feldman3c1842b2017-08-02 05:00:16279};
280
Arthur Sonzogni929d29f2018-09-17 14:19:44281bool FrameCompareDepth(RenderFrameHostImpl* a, RenderFrameHostImpl* b) {
282 return a->frame_tree_node()->depth() < b->frame_tree_node()->depth();
283}
284
lukasza6f8ac622017-06-06 03:10:20285} // namespace
286
Erik Chenbb8e738e2018-04-28 14:10:43287std::unique_ptr<WebContents> WebContents::Create(
288 const WebContents::CreateParams& params) {
erikchen50735fd2018-05-05 15:08:33289 return WebContentsImpl::CreateWithOpener(params, FindOpenerRFH(params));
[email protected]d1198fd2012-08-13 22:50:19290}
291
erikchenade1fef2018-05-02 22:07:57292std::unique_ptr<WebContents> WebContents::CreateWithSessionStorage(
[email protected]54944cde2012-12-09 09:24:59293 const WebContents::CreateParams& params,
[email protected]fdac6ade2013-07-20 01:06:30294 const SessionStorageNamespaceMap& session_storage_namespace_map) {
erikchenade1fef2018-05-02 22:07:57295 std::unique_ptr<WebContentsImpl> new_contents(
296 new WebContentsImpl(params.browser_context));
Ian Clelland5cbaaf82017-11-27 22:00:03297 RenderFrameHostImpl* opener_rfh = FindOpenerRFH(params);
298 FrameTreeNode* opener = nullptr;
299 if (opener_rfh)
300 opener = opener_rfh->frame_tree_node();
301 new_contents->SetOpenerForNewContents(opener, params.opener_suppressed);
[email protected]fdac6ade2013-07-20 01:06:30302
jdoerrie55ec69d2018-10-08 13:34:46303 for (auto it = session_storage_namespace_map.begin();
304 it != session_storage_namespace_map.end(); ++it) {
[email protected]fdac6ade2013-07-20 01:06:30305 new_contents->GetController()
306 .SetSessionStorageNamespace(it->first, it->second.get());
307 }
308
Sean Toppingbbc4a2bd2019-01-30 02:16:14309 WebContentsImpl* outer_web_contents = nullptr;
Sam McNally69a33772018-06-27 16:54:12310 if (params.guest_delegate) {
311 // This makes |new_contents| act as a guest.
312 // For more info, see comment above class BrowserPluginGuest.
Albert J. Wongc55cc64d2018-10-12 02:50:04313 BrowserPluginGuest::CreateInWebContents(new_contents.get(),
314 params.guest_delegate);
Sean Toppingbbc4a2bd2019-01-30 02:16:14315 outer_web_contents = static_cast<WebContentsImpl*>(
316 params.guest_delegate->GetOwnerWebContents());
Sam McNally69a33772018-06-27 16:54:12317 }
318
[email protected]54944cde2012-12-09 09:24:59319 new_contents->Init(params);
Sean Toppingbbc4a2bd2019-01-30 02:16:14320 if (outer_web_contents)
321 outer_web_contents->InnerWebContentsCreated(new_contents.get());
[email protected]d1198fd2012-08-13 22:50:19322 return new_contents;
[email protected]a81343d232011-12-27 07:39:20323}
[email protected]746d3052012-05-22 15:15:47324
benwells5e7260d2017-03-20 23:36:33325void WebContentsImpl::FriendWrapper::AddCreatedCallbackForTesting(
nickf9acfbe2014-12-23 19:12:37326 const CreatedCallback& callback) {
[email protected]cbb1ef592013-06-05 19:49:46327 g_created_callbacks.Get().push_back(callback);
328}
329
benwells5e7260d2017-03-20 23:36:33330void WebContentsImpl::FriendWrapper::RemoveCreatedCallbackForTesting(
nickf9acfbe2014-12-23 19:12:37331 const CreatedCallback& callback) {
[email protected]cbb1ef592013-06-05 19:49:46332 for (size_t i = 0; i < g_created_callbacks.Get().size(); ++i) {
Robert Liao1db5a352019-06-05 20:57:34333 if (g_created_callbacks.Get().at(i) == callback) {
[email protected]cbb1ef592013-06-05 19:49:46334 g_created_callbacks.Get().erase(g_created_callbacks.Get().begin() + i);
335 return;
336 }
337 }
338}
339
avidf38c952015-10-27 13:45:13340WebContents* WebContents::FromRenderViewHost(RenderViewHost* rvh) {
341 if (!rvh)
342 return nullptr;
[email protected]746d3052012-05-22 15:15:47343 return rvh->GetDelegate()->GetAsWebContents();
344}
345
[email protected]a86c0e962013-12-17 17:10:39346WebContents* WebContents::FromRenderFrameHost(RenderFrameHost* rfh) {
avidf38c952015-10-27 13:45:13347 if (!rfh)
348 return nullptr;
349 return static_cast<RenderFrameHostImpl*>(rfh)->delegate()->GetAsWebContents();
[email protected]a86c0e962013-12-17 17:10:39350}
351
ananta4b7467a52016-09-23 01:42:38352WebContents* WebContents::FromFrameTreeNodeId(int frame_tree_node_id) {
353 FrameTreeNode* frame_tree_node =
354 FrameTreeNode::GloballyFindByID(frame_tree_node_id);
anantab8cfa3b2016-10-14 22:43:19355 if (!frame_tree_node)
356 return nullptr;
jam8c4edd02017-05-06 18:50:33357 return WebContentsImpl::FromFrameTreeNode(frame_tree_node);
ananta4b7467a52016-09-23 01:42:38358}
359
leon.han552e9de2017-02-09 14:37:30360void WebContents::SetScreenOrientationDelegate(
361 ScreenOrientationDelegate* delegate) {
362 ScreenOrientationProvider::SetDelegate(delegate);
363}
364
[email protected]7fff43e2013-05-21 20:21:10365// WebContentsImpl::DestructionObserver ----------------------------------------
366
367class WebContentsImpl::DestructionObserver : public WebContentsObserver {
368 public:
369 DestructionObserver(WebContentsImpl* owner, WebContents* watched_contents)
370 : WebContentsObserver(watched_contents),
371 owner_(owner) {
372 }
373
374 // WebContentsObserver:
dchengc2282aa2014-10-21 12:07:58375 void WebContentsDestroyed() override {
[email protected]12a46832014-05-09 13:35:58376 owner_->OnWebContentsDestroyed(
377 static_cast<WebContentsImpl*>(web_contents()));
[email protected]7fff43e2013-05-21 20:21:10378 }
379
380 private:
381 WebContentsImpl* owner_;
382
383 DISALLOW_COPY_AND_ASSIGN(DestructionObserver);
384};
385
Joel Hockey85b379d2017-12-11 10:42:13386// WebContentsImpl::ColorChooser ----------------------------------------------
Joel Hockey163835a2017-12-20 11:51:57387class WebContentsImpl::ColorChooser : public blink::mojom::ColorChooser {
Joel Hockey85b379d2017-12-11 10:42:13388 public:
389 ColorChooser(content::ColorChooser* chooser,
Joel Hockey163835a2017-12-20 11:51:57390 blink::mojom::ColorChooserRequest request,
391 blink::mojom::ColorChooserClientPtr client)
Joel Hockey85b379d2017-12-11 10:42:13392 : chooser_(chooser),
393 binding_(this, std::move(request)),
394 client_(std::move(client)) {
395 binding_.set_connection_error_handler(
396 base::BindOnce([](content::ColorChooser* chooser) { chooser->End(); },
397 base::Unretained(chooser)));
398 }
[email protected]8ed16472014-04-11 19:02:48399
Joel Hockey85b379d2017-12-11 10:42:13400 ~ColorChooser() override { chooser_->End(); }
[email protected]8ed16472014-04-11 19:02:48401
Joel Hockey85b379d2017-12-11 10:42:13402 void SetSelectedColor(SkColor color) override {
403 chooser_->SetSelectedColor(color);
404 }
405
406 void DidChooseColorInColorChooser(SkColor color) {
407 client_->DidChooseColor(color);
408 }
409
410 private:
411 // Color chooser that was opened by this tab.
412 std::unique_ptr<content::ColorChooser> chooser_;
413
414 // mojo bindings.
Joel Hockey163835a2017-12-20 11:51:57415 mojo::Binding<blink::mojom::ColorChooser> binding_;
Joel Hockey85b379d2017-12-11 10:42:13416
417 // mojo renderer client.
Joel Hockey163835a2017-12-20 11:51:57418 blink::mojom::ColorChooserClientPtr client_;
Joel Hockey85b379d2017-12-11 10:42:13419};
nicka0ac8382016-12-15 23:59:23420
lazyboy6ec48b2a2015-06-29 15:18:14421// WebContentsImpl::WebContentsTreeNode ----------------------------------------
lfg601233692017-03-06 22:45:44422WebContentsImpl::WebContentsTreeNode::WebContentsTreeNode(
423 WebContentsImpl* current_web_contents)
424 : current_web_contents_(current_web_contents),
425 outer_web_contents_(nullptr),
lazyboy6ec48b2a2015-06-29 15:18:14426 outer_contents_frame_tree_node_id_(
avallee0206f782016-07-28 18:55:33427 FrameTreeNode::kFrameTreeNodeInvalidId),
lfg601233692017-03-06 22:45:44428 focused_web_contents_(current_web_contents) {}
lazyboy6ec48b2a2015-06-29 15:18:14429
Lucas Furukawa Gadani3d38ec152018-10-26 20:55:18430WebContentsImpl::WebContentsTreeNode::~WebContentsTreeNode() {}
lazyboy6ec48b2a2015-06-29 15:18:14431
Lucas Furukawa Gadani99125822019-01-03 15:41:49432std::unique_ptr<WebContents>
433WebContentsImpl::WebContentsTreeNode::DisconnectFromOuterWebContents() {
434 std::unique_ptr<WebContents> inner_contents =
435 outer_web_contents_->node_.DetachInnerWebContents(current_web_contents_);
436 OuterContentsFrameTreeNode()->RemoveObserver(this);
437 outer_contents_frame_tree_node_id_ = FrameTreeNode::kFrameTreeNodeInvalidId;
438 outer_web_contents_ = nullptr;
439 return inner_contents;
440}
441
paulmeyerfeafc2d2017-04-25 21:46:40442void WebContentsImpl::WebContentsTreeNode::AttachInnerWebContents(
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:50443 std::unique_ptr<WebContents> inner_web_contents,
444 RenderFrameHostImpl* render_frame_host) {
445 WebContentsImpl* inner_web_contents_impl =
446 static_cast<WebContentsImpl*>(inner_web_contents.get());
447 WebContentsTreeNode& inner_web_contents_node = inner_web_contents_impl->node_;
448
449 inner_web_contents_node.focused_web_contents_ = nullptr;
450 inner_web_contents_node.outer_web_contents_ = current_web_contents_;
451 inner_web_contents_node.outer_contents_frame_tree_node_id_ =
452 render_frame_host->frame_tree_node()->frame_tree_node_id();
453
Lucas Furukawa Gadani3d38ec152018-10-26 20:55:18454 inner_web_contents_.push_back(std::move(inner_web_contents));
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:50455
456 render_frame_host->frame_tree_node()->AddObserver(&inner_web_contents_node);
paulmeyerfeafc2d2017-04-25 21:46:40457}
458
Lucas Furukawa Gadani3d38ec152018-10-26 20:55:18459std::unique_ptr<WebContents>
460WebContentsImpl::WebContentsTreeNode::DetachInnerWebContents(
paulmeyerfeafc2d2017-04-25 21:46:40461 WebContentsImpl* inner_web_contents) {
Lucas Furukawa Gadani3d38ec152018-10-26 20:55:18462 std::unique_ptr<WebContents> detached_contents;
463 for (std::unique_ptr<WebContents>& web_contents : inner_web_contents_) {
464 if (web_contents.get() == inner_web_contents) {
465 detached_contents = std::move(web_contents);
466 std::swap(web_contents, inner_web_contents_.back());
467 inner_web_contents_.pop_back();
468 return detached_contents;
469 }
470 }
471
472 NOTREACHED();
473 return nullptr;
paulmeyerfeafc2d2017-04-25 21:46:40474}
475
lfga3e2bdc2017-03-07 20:44:01476FrameTreeNode*
477WebContentsImpl::WebContentsTreeNode::OuterContentsFrameTreeNode() const {
478 return FrameTreeNode::GloballyFindByID(outer_contents_frame_tree_node_id_);
lfg601233692017-03-06 22:45:44479}
480
lfga3e2bdc2017-03-07 20:44:01481void WebContentsImpl::WebContentsTreeNode::OnFrameTreeNodeDestroyed(
482 FrameTreeNode* node) {
483 DCHECK_EQ(outer_contents_frame_tree_node_id_, node->frame_tree_node_id())
484 << "WebContentsTreeNode should only receive notifications for the "
485 "FrameTreeNode in its outer WebContents that hosts it.";
Lucas Furukawa Gadani3d38ec152018-10-26 20:55:18486
487 // Deletes |this| too.
488 outer_web_contents_->node_.DetachInnerWebContents(current_web_contents_);
lazyboy6ec48b2a2015-06-29 15:18:14489}
490
avallee0206f782016-07-28 18:55:33491void WebContentsImpl::WebContentsTreeNode::SetFocusedWebContents(
492 WebContentsImpl* web_contents) {
493 DCHECK(!outer_web_contents())
494 << "Only the outermost WebContents tracks focus.";
495 focused_web_contents_ = web_contents;
496}
497
paulmeyerfeafc2d2017-04-25 21:46:40498WebContentsImpl*
499WebContentsImpl::WebContentsTreeNode::GetInnerWebContentsInFrame(
500 const FrameTreeNode* frame) {
501 auto ftn_id = frame->frame_tree_node_id();
Lucas Furukawa Gadani3d38ec152018-10-26 20:55:18502 for (auto& contents : inner_web_contents_) {
503 WebContentsImpl* impl = static_cast<WebContentsImpl*>(contents.get());
504 if (impl->node_.outer_contents_frame_tree_node_id() == ftn_id) {
505 return impl;
paulmeyerfeafc2d2017-04-25 21:46:40506 }
507 }
508 return nullptr;
509}
510
Lucas Furukawa Gadani3d38ec152018-10-26 20:55:18511std::vector<WebContentsImpl*>
512WebContentsImpl::WebContentsTreeNode::GetInnerWebContents() const {
513 std::vector<WebContentsImpl*> inner_web_contents;
514 for (auto& contents : inner_web_contents_)
515 inner_web_contents.push_back(static_cast<WebContentsImpl*>(contents.get()));
516
517 return inner_web_contents;
paulmeyerfeafc2d2017-04-25 21:46:40518}
519
[email protected]b172aee2012-04-10 17:05:26520// WebContentsImpl -------------------------------------------------------------
[email protected]420ae012009-04-24 05:16:32521
alexmose201c7cd2015-06-10 17:14:21522WebContentsImpl::WebContentsImpl(BrowserContext* browser_context)
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28523 : delegate_(nullptr),
[email protected]69e797f2013-04-30 01:10:22524 controller_(this, browser_context),
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28525 render_view_host_delegate_view_(nullptr),
alexmose201c7cd2015-06-10 17:14:21526 created_with_opener_(false),
Lucas Furukawa Gadani99125822019-01-03 15:41:49527 node_(this),
[email protected]6ea6bdf2013-12-06 13:35:01528 frame_tree_(new NavigatorImpl(&controller_, this),
blundellc57b93f2014-10-29 13:19:57529 this,
530 this,
531 this,
532 this),
[email protected]6dfed692014-05-22 04:18:03533 is_load_to_different_document_(false),
[email protected]443b80e2010-12-14 00:42:23534 crashed_status_(base::TERMINATION_STATUS_STILL_RUNNING),
535 crashed_error_code_(0),
[email protected]d5f942ba2008-09-26 19:30:34536 waiting_for_response_(false),
[email protected]fcf75d42013-12-03 20:11:26537 load_state_(net::LOAD_STATE_IDLE, base::string16()),
[email protected]094e5b22009-09-25 04:23:56538 upload_size_(0),
539 upload_position_(0),
dfalcantarae6b7b46752015-07-10 18:14:16540 is_resume_pending_(false),
creisadca9862017-06-16 19:12:11541 interstitial_page_(nullptr),
[email protected]b4c84012014-04-28 19:51:10542 has_accessed_initial_document_(false),
peter90afaba2015-06-01 12:05:29543 did_first_visually_non_empty_paint_(false),
[email protected]54597982013-02-06 01:59:55544 capturer_count_(0),
[email protected]fdd61c62009-04-22 19:22:57545 is_being_destroyed_(false),
creis97867aa2017-02-17 18:39:45546 is_notifying_observers_(false),
[email protected]fdd61c62009-04-22 19:22:57547 notify_disconnection_(false),
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28548 dialog_manager_(nullptr),
[email protected]7ab1e7d62009-10-14 23:32:01549 is_showing_before_unload_dialog_(false),
gab0dccfef2015-05-20 18:43:39550 last_active_time_(base::TimeTicks::Now()),
[email protected]ebf40a72010-07-22 01:46:38551 closed_by_user_gesture_(false),
[email protected]8ff00d72012-10-23 19:12:21552 minimum_zoom_percent_(static_cast<int>(kMinimumZoomFactor * 100)),
553 maximum_zoom_percent_(static_cast<int>(kMaximumZoomFactor * 100)),
w.shackleton49bcd392016-01-06 17:38:22554 zoom_scroll_remainder_(0),
alexmosc9e76ec2016-05-16 22:59:37555 fullscreen_widget_process_id_(ChildProcessHost::kInvalidUniqueID),
[email protected]5dcaf8e2013-12-28 01:31:42556 fullscreen_widget_routing_id_(MSG_ROUTING_NONE),
[email protected]0b431992014-06-24 00:08:03557 fullscreen_widget_had_focus_at_shutdown_(false),
[email protected]34ff1cfc2014-08-20 06:16:05558 force_disable_overscroll_content_(false),
[email protected]95640212014-07-26 18:14:30559 last_dialog_suppressed_(false),
waffles1c12bf402016-02-10 17:45:48560 accessibility_mode_(
Dominic Mazzoni21fb0282019-02-13 18:32:47561 GetContentClient()->browser()->GetAXModeForBrowserContext(
562 browser_context)),
avayvodcc85bbd2015-08-28 19:11:15563 audio_stream_monitor_(this),
Thomas Guilbert882c4cec2019-02-14 05:55:45564 media_web_contents_observer_(
565 std::make_unique<MediaWebContentsObserver>(this)),
Guido Urdanetaf4008002018-05-03 10:43:04566 media_device_group_id_salt_base_(
567 BrowserContext::CreateRandomMediaDeviceIDSalt()),
mcnee336ea2c2017-05-23 22:50:59568#if !defined(OS_ANDROID)
mcnee432e47d2015-11-09 19:37:46569 page_scale_factor_is_one_(true),
mcnee336ea2c2017-05-23 22:50:59570#endif // !defined(OS_ANDROID)
qinmin72e8bd02016-10-21 19:35:37571 is_overlay_content_(false),
ekaramadf6750aa2017-06-06 18:29:42572 showing_context_menu_(false),
W. James MacLean62198672019-06-04 16:46:10573 text_autosizer_page_info_({0, 0, 1.f}),
Alison Maherbe15c072019-07-08 18:12:39574 native_theme_observer_(this),
Jeremy Roman3bca4bf2019-07-11 03:41:25575 had_inner_webcontents_(false) {
[email protected]9b159a52013-10-03 17:24:55576 frame_tree_.SetFrameRemoveListener(
577 base::Bind(&WebContentsImpl::OnFrameRemoved,
578 base::Unretained(this)));
brettw4b461082016-11-19 18:55:16579#if BUILDFLAG(ENABLE_PLUGINS)
zqzhang181047e62016-07-01 13:37:17580 pepper_playback_observer_.reset(new PepperPlaybackObserver(this));
581#endif
blundelle75a8f92017-03-27 08:11:17582
mcnee336ea2c2017-05-23 22:50:59583#if !defined(OS_ANDROID)
scottmg276753cf2016-10-27 18:25:22584 host_zoom_map_observer_.reset(new HostZoomMapObserver(this));
mcnee336ea2c2017-05-23 22:50:59585#endif // !defined(OS_ANDROID)
Joel Hockey85b379d2017-12-11 10:42:13586
Becca Hughes86334622018-07-09 21:29:38587#if defined(OS_ANDROID)
Elly Fong-Jonescdd7ae82019-02-20 23:26:33588 display_cutout_host_impl_ = std::make_unique<DisplayCutoutHostImpl>(this);
Becca Hughes86334622018-07-09 21:29:38589#endif
Becca Hughes6fe54e432018-06-09 04:18:56590
Joel Hockey85b379d2017-12-11 10:42:13591 registry_.AddInterface(base::BindRepeating(
592 &WebContentsImpl::OnColorChooserFactoryRequest, base::Unretained(this)));
Rune Lillesveen5d8c49982019-04-24 19:07:30593
Alison Maherbe15c072019-07-08 18:12:39594 ui::NativeTheme* native_theme = ui::NativeTheme::GetInstanceForWeb();
595 native_theme_observer_.Add(native_theme);
596 in_high_contrast_ = native_theme->UsesHighContrastColors();
597 in_dark_mode_ = native_theme->SystemDarkModeEnabled();
Alison Maher67fd4d9f2019-07-12 18:34:21598 preferred_color_scheme_ = native_theme->GetPreferredColorScheme();
[email protected]332af7732009-03-11 13:21:35599}
initial.commit09911bf2008-07-26 23:55:29600
[email protected]b172aee2012-04-10 17:05:26601WebContentsImpl::~WebContentsImpl() {
creisba91ba82017-05-12 22:56:19602 // Imperfect sanity check against double free, given some crashes unexpectedly
603 // observed in the wild.
604 CHECK(!is_being_destroyed_);
605
606 // We generally keep track of is_being_destroyed_ to let other features know
607 // to avoid certain actions during destruction.
[email protected]420ae012009-04-24 05:16:32608 is_being_destroyed_ = true;
609
creis97867aa2017-02-17 18:39:45610 // A WebContents should never be deleted while it is notifying observers,
creisba91ba82017-05-12 22:56:19611 // since this will lead to a use-after-free as it continues to notify later
creis97867aa2017-02-17 18:39:45612 // observers.
613 CHECK(!is_notifying_observers_);
614
kenrb2a565f82015-09-02 20:24:59615 rwh_input_event_router_.reset();
616
rockotf62002a2016-09-15 00:08:59617 for (auto& entry : binding_sets_)
618 entry.second->CloseAllBindings();
619
avallee0206f782016-07-28 18:55:33620 WebContentsImpl* outermost = GetOutermostWebContents();
avallee9993fca2016-11-17 06:16:50621 if (this != outermost && ContainsOrIsFocusedWebContents()) {
avallee0206f782016-07-28 18:55:33622 // If the current WebContents is in focus, unset it.
avallee9993fca2016-11-17 06:16:50623 outermost->SetAsFocusedWebContentsIfNecessary();
avallee0206f782016-07-28 18:55:33624 }
625
Nick Carterd73635b2018-03-13 18:31:41626 if (mouse_lock_widget_)
627 mouse_lock_widget_->RejectMouseLockOrUnlockIfNecessary();
628
dcheng72be33c2016-01-21 04:11:31629 for (FrameTreeNode* node : frame_tree_.Nodes()) {
630 // Delete all RFHs pending shutdown, which will lead the corresponding RVHs
631 // to be shutdown and be deleted as well.
dcheng57e39e22016-01-21 00:25:38632 node->render_manager()->ClearRFHsPendingShutdown();
dcheng57e39e22016-01-21 00:25:38633 node->render_manager()->ClearWebUIInstances();
dcheng72be33c2016-01-21 04:11:31634 }
carlosk35f35af2015-12-01 10:55:40635
dcheng57e39e22016-01-21 00:25:38636 for (RenderWidgetHostImpl* widget : created_widgets_)
637 widget->DetachDelegate();
[email protected]b24b68a2012-09-24 21:57:26638 created_widgets_.clear();
639
[email protected]3ab9cb82011-06-03 18:02:07640 // Clear out any JavaScript state.
avi5d3b8692016-10-12 22:00:46641 if (dialog_manager_) {
avi6879fcf2017-01-21 05:27:53642 dialog_manager_->CancelDialogs(this, /*reset_state=*/true);
avi5d3b8692016-10-12 22:00:46643 }
[email protected]3ab9cb82011-06-03 18:02:07644
Joel Hockey85b379d2017-12-11 10:42:13645 color_chooser_.reset();
Rakina Zata Amni16196122018-11-14 02:14:00646 find_request_manager_.reset();
[email protected]da8543762012-03-20 08:52:20647
[email protected]420ae012009-04-24 05:16:32648 NotifyDisconnected();
[email protected]420ae012009-04-24 05:16:32649
[email protected]ca13a442012-04-17 14:00:12650 // Notify any observer that have a reference on this WebContents.
[email protected]8ff00d72012-10-23 19:12:21651 NotificationService::current()->Notify(
652 NOTIFICATION_WEB_CONTENTS_DESTROYED,
653 Source<WebContents>(this),
654 NotificationService::NoDetails());
[email protected]420ae012009-04-24 05:16:32655
Arthur Sonzogni5a9a88c2018-10-18 13:16:00656 // Destroy all subframes now. This notifies observers.
657 GetMainFrame()->ResetChildren();
[email protected]df0c843f2014-05-23 20:37:23658 GetRenderManager()->ResetProxyHosts();
[email protected]c06c58c2014-03-12 20:31:59659
clamy0e119882015-07-31 16:12:33660 // Manually call the observer methods for the root frame tree node. It is
661 // necessary to manually delete all objects tracking navigations
662 // (NavigationHandle, NavigationRequest) for observers to be properly
663 // notified of these navigations stopping before the WebContents is
664 // destroyed.
[email protected]df0c843f2014-05-23 20:37:23665 RenderFrameHostManager* root = GetRenderManager();
naskob21fe4872015-02-24 14:15:56666
Arthur Sonzogni620cec62018-12-13 13:08:57667 GetController().back_forward_cache().Flush();
naskob21fe4872015-02-24 14:15:56668 root->current_frame_host()->SetRenderFrameCreated(false);
clamyfc3850d2018-02-23 14:35:28669 root->current_frame_host()->ResetNavigationRequests();
clamy0e119882015-07-31 16:12:33670
Arthur Hemeryc3380172018-01-22 14:00:17671 // Do not update state as the WebContents is being destroyed.
672 frame_tree_.root()->ResetNavigationRequest(true, true);
673 if (root->speculative_frame_host()) {
danakjf2342432019-05-15 21:46:09674 root->speculative_frame_host()->DeleteRenderFrame(
675 FrameDeleteIntention::kSpeculativeMainFrameForShutdown);
Arthur Hemeryc3380172018-01-22 14:00:17676 root->speculative_frame_host()->SetRenderFrameCreated(false);
clamyfc3850d2018-02-23 14:35:28677 root->speculative_frame_host()->ResetNavigationRequests();
clamy0e119882015-07-31 16:12:33678 }
[email protected]f273ee52013-10-18 16:05:27679
brettw4b461082016-11-19 18:55:16680#if BUILDFLAG(ENABLE_PLUGINS)
zqzhang181047e62016-07-01 13:37:17681 // Call this before WebContentsDestroyed() is broadcasted since
682 // AudioFocusManager will be destroyed after that.
683 pepper_playback_observer_.reset();
684#endif // defined(ENABLED_PLUGINS)
685
Chris Hamiltondf0d72cd2018-05-29 16:23:53686 // If audio is playing then notify external observers of the audio stream
687 // disappearing.
688 if (is_currently_audible_) {
689 is_currently_audible_ = false;
690 for (auto& observer : observers_)
691 observer.OnAudioStateChanged(false);
692
693 if (GetOuterWebContents())
694 GetOuterWebContents()->OnAudioStateChanged();
695 }
696
ericwilligersde386342016-10-19 02:35:09697 for (auto& observer : observers_)
698 observer.FrameDeleted(root->current_frame_host());
naskoa83483fb2015-02-27 16:57:10699
ericwilligersde386342016-10-19 02:35:09700 for (auto& observer : observers_)
701 observer.RenderViewDeleted(root->current_host());
[email protected]f273ee52013-10-18 16:05:27702
Kevin McNee576bb5d82019-05-29 16:47:51703#if defined(OS_ANDROID)
704 // For simplicity, destroy the Java WebContents before we notify of the
705 // destruction of the WebContents.
706 ClearWebContentsAndroid();
707#endif
708
ericwilligersde386342016-10-19 02:35:09709 for (auto& observer : observers_)
710 observer.WebContentsDestroyed();
[email protected]12a46832014-05-09 13:35:58711
Becca Hughesd11d6502018-07-31 17:01:28712 if (display_cutout_host_impl_)
713 display_cutout_host_impl_->WebContentsDestroyed();
714
ericwilligersde386342016-10-19 02:35:09715 for (auto& observer : observers_)
716 observer.ResetWebContents();
[email protected]232a5812011-03-04 22:42:08717
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28718 SetDelegate(nullptr);
[email protected]b5a1d11c2011-02-17 03:09:42719}
720
erikchen50735fd2018-05-05 15:08:33721std::unique_ptr<WebContentsImpl> WebContentsImpl::CreateWithOpener(
[email protected]54944cde2012-12-09 09:24:59722 const WebContents::CreateParams& params,
Ian Clelland5cbaaf82017-11-27 22:00:03723 RenderFrameHostImpl* opener_rfh) {
[email protected]e11f0e92013-06-12 15:12:03724 TRACE_EVENT0("browser", "WebContentsImpl::CreateWithOpener");
Ian Clelland5cbaaf82017-11-27 22:00:03725 FrameTreeNode* opener = nullptr;
726 if (opener_rfh)
727 opener = opener_rfh->frame_tree_node();
erikchen50735fd2018-05-05 15:08:33728 std::unique_ptr<WebContentsImpl> new_contents(
729 new WebContentsImpl(params.browser_context));
lukasza6f8ac622017-06-06 03:10:20730 new_contents->SetOpenerForNewContents(opener, params.opener_suppressed);
alexmose201c7cd2015-06-10 17:14:21731
alexmosaedfc6f2016-01-21 23:57:38732 // If the opener is sandboxed, a new popup must inherit the opener's sandbox
733 // flags, and these flags take effect immediately. An exception is if the
734 // opener's sandbox flags lack the PropagatesToAuxiliaryBrowsingContexts
735 // bit (which is controlled by the "allow-popups-to-escape-sandbox" token).
736 // See https://html.spec.whatwg.org/#attr-iframe-sandbox.
lukasza6f8ac622017-06-06 03:10:20737 FrameTreeNode* new_root = new_contents->GetFrameTree()->root();
alexmosaedfc6f2016-01-21 23:57:38738 if (opener) {
Ian Clelland5cbaaf82017-11-27 22:00:03739 blink::WebSandboxFlags opener_flags = opener_rfh->active_sandbox_flags();
alexmosaedfc6f2016-01-21 23:57:38740 const blink::WebSandboxFlags inherit_flag =
Blink Reformat1c4d759e2017-04-09 16:34:54741 blink::WebSandboxFlags::kPropagatesToAuxiliaryBrowsingContexts;
Ehsan Karamad39407082019-02-19 23:38:19742 bool sandbox_propagates_to_auxilary_context =
743 (opener_flags & inherit_flag) == inherit_flag;
744 if (sandbox_propagates_to_auxilary_context)
Ian Clellandcdc4f312017-10-13 22:24:12745 new_root->SetPendingFramePolicy({opener_flags, {}});
Ehsan Karamad39407082019-02-19 23:38:19746 if (opener_flags == blink::WebSandboxFlags::kNone ||
747 sandbox_propagates_to_auxilary_context) {
748 // TODO(ekaramad, iclelland): Do not propagate feature policies from non-
749 // sandboxed disowned openers (rel=noopener).
750 // If the current page is not sandboxed, or if the sandbox is to propagate
751 // to the popups then opener's feature policy will apply to the new popup
752 // as well.
753 new_root->SetOpenerFeaturePolicyState(
Ehsan Karamad34d10682019-02-26 20:55:04754 opener_rfh->feature_policy()->GetFeatureState());
alexmosaedfc6f2016-01-21 23:57:38755 }
756 }
757
mark a. foltzef394fce2017-10-21 09:11:02758 // Apply starting sandbox flags.
Luna Luc3fdacdf2017-11-08 04:48:53759 blink::FramePolicy frame_policy(new_root->pending_frame_policy());
mark a. foltzef394fce2017-10-21 09:11:02760 frame_policy.sandbox_flags |= params.starting_sandbox_flags;
761 new_root->SetPendingFramePolicy(frame_policy);
762 new_root->CommitPendingFramePolicy();
763
alexmose201c7cd2015-06-10 17:14:21764 // This may be true even when opener is null, such as when opening blocked
765 // popups.
alexmos090fae8e2015-05-28 17:09:28766 if (params.created_with_opener)
767 new_contents->created_with_opener_ = true;
768
Sean Toppingbbc4a2bd2019-01-30 02:16:14769 WebContentsImpl* outer_web_contents = nullptr;
[email protected]4858e432014-06-23 18:14:17770 if (params.guest_delegate) {
[email protected]83100cd2014-05-10 11:50:06771 // This makes |new_contents| act as a guest.
772 // For more info, see comment above class BrowserPluginGuest.
Albert J. Wongc55cc64d2018-10-12 02:50:04773 BrowserPluginGuest::CreateInWebContents(new_contents.get(),
774 params.guest_delegate);
Sean Toppingbbc4a2bd2019-01-30 02:16:14775 outer_web_contents = static_cast<WebContentsImpl*>(
776 params.guest_delegate->GetOwnerWebContents());
[email protected]83100cd2014-05-10 11:50:06777 }
mark a. foltzef394fce2017-10-21 09:11:02778
[email protected]54944cde2012-12-09 09:24:59779 new_contents->Init(params);
Sean Toppingbbc4a2bd2019-01-30 02:16:14780 if (outer_web_contents)
781 outer_web_contents->InnerWebContentsCreated(new_contents.get());
[email protected]d1198fd2012-08-13 22:50:19782 return new_contents;
783}
784
[email protected]95640212014-07-26 18:14:30785// static
786std::vector<WebContentsImpl*> WebContentsImpl::GetAllWebContents() {
787 std::vector<WebContentsImpl*> result;
dcheng59716272016-04-09 05:19:08788 std::unique_ptr<RenderWidgetHostIterator> widgets(
[email protected]95640212014-07-26 18:14:30789 RenderWidgetHostImpl::GetRenderWidgetHosts());
[email protected]95640212014-07-26 18:14:30790 while (RenderWidgetHost* rwh = widgets->GetNextHost()) {
[email protected]95640212014-07-26 18:14:30791 RenderViewHost* rvh = RenderViewHost::From(rwh);
792 if (!rvh)
793 continue;
794 WebContents* web_contents = WebContents::FromRenderViewHost(rvh);
795 if (!web_contents)
796 continue;
avidf38c952015-10-27 13:45:13797 if (web_contents->GetRenderViewHost() != rvh)
798 continue;
799 // Because a WebContents can only have one current RVH at a time, there will
800 // be no duplicate WebContents here.
801 result.push_back(static_cast<WebContentsImpl*>(web_contents));
[email protected]95640212014-07-26 18:14:30802 }
803 return result;
804}
805
clamya16aa8172015-05-26 13:07:25806// static
807WebContentsImpl* WebContentsImpl::FromFrameTreeNode(
paulmeyerfeafc2d2017-04-25 21:46:40808 const FrameTreeNode* frame_tree_node) {
clamya16aa8172015-05-26 13:07:25809 return static_cast<WebContentsImpl*>(
810 WebContents::FromRenderFrameHost(frame_tree_node->current_frame_host()));
811}
812
clamy0d32d6d2015-11-24 11:16:26813// static
814WebContents* WebContentsImpl::FromRenderFrameHostID(int render_process_host_id,
815 int render_frame_host_id) {
Min Qina1150112019-02-15 01:47:49816 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) ||
817 !BrowserThread::IsThreadInitialized(BrowserThread::UI));
clamy0d32d6d2015-11-24 11:16:26818 RenderFrameHost* render_frame_host =
819 RenderFrameHost::FromID(render_process_host_id, render_frame_host_id);
820 if (!render_frame_host)
821 return nullptr;
822
823 return WebContents::FromRenderFrameHost(render_frame_host);
824}
825
paulmeyerfeafc2d2017-04-25 21:46:40826// static
827WebContentsImpl* WebContentsImpl::FromOuterFrameTreeNode(
828 const FrameTreeNode* frame_tree_node) {
829 return WebContentsImpl::FromFrameTreeNode(frame_tree_node)
830 ->node_.GetInnerWebContentsInFrame(frame_tree_node);
831}
832
[email protected]b0936d22013-11-28 06:47:36833RenderFrameHostManager* WebContentsImpl::GetRenderManagerForTesting() {
[email protected]fa944cb82013-11-15 17:51:21834 return GetRenderManager();
[email protected]765187182012-01-11 23:59:28835}
836
nicka0ac8382016-12-15 23:59:23837bool WebContentsImpl::OnMessageReceived(RenderViewHostImpl* render_view_host,
[email protected]7bb761892012-07-20 09:32:47838 const IPC::Message& message) {
nicka0ac8382016-12-15 23:59:23839 for (auto& observer : observers_) {
Lukasz Anforowicz89f2c1b2017-08-30 21:36:46840 // TODO(nick, creis): https://crbug.com/758026: Replace all uses of this
841 // variant of OnMessageReceived with the version that takes a
842 // RenderFrameHost, and then delete it.
nicka0ac8382016-12-15 23:59:23843 if (observer.OnMessageReceived(message))
844 return true;
[email protected]64ffefa2014-05-10 12:06:33845 }
[email protected]403415a2011-01-10 18:57:53846
[email protected]724159a2010-12-30 01:11:18847 bool handled = true;
nicka0ac8382016-12-15 23:59:23848 IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(WebContentsImpl, message, render_view_host)
nicka0ac8382016-12-15 23:59:23849 IPC_MESSAGE_HANDLER(ViewHostMsg_UpdateZoomLimits, OnUpdateZoomLimits)
850 IPC_MESSAGE_HANDLER(ViewHostMsg_PageScaleFactorChanged,
851 OnPageScaleFactorChanged)
W. James MacLean62198672019-06-04 16:46:10852 IPC_MESSAGE_HANDLER(
853 ViewHostMsg_NotifyTextAutosizerPageInfoChangedInLocalMainFrame,
854 OnTextAutosizerPageInfoChanged)
nicka0ac8382016-12-15 23:59:23855#if BUILDFLAG(ENABLE_PLUGINS)
856 IPC_MESSAGE_HANDLER(ViewHostMsg_RequestPpapiBrokerPermission,
857 OnRequestPpapiBrokerPermission)
858#endif
nicka0ac8382016-12-15 23:59:23859 IPC_MESSAGE_UNHANDLED(handled = false)
860 IPC_END_MESSAGE_MAP()
861
862 return handled;
863}
864
865bool WebContentsImpl::OnMessageReceived(RenderFrameHostImpl* render_frame_host,
866 const IPC::Message& message) {
Lukasz Anforowicz02923102017-10-09 18:11:37867 {
868 WebUIImpl* web_ui = render_frame_host->web_ui();
869 if (web_ui && web_ui->OnMessageReceived(message, render_frame_host))
870 return true;
871 }
872
nicka0ac8382016-12-15 23:59:23873 for (auto& observer : observers_) {
874 if (observer.OnMessageReceived(message, render_frame_host))
875 return true;
876 }
877
878 bool handled = true;
879 IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(WebContentsImpl, message, render_frame_host)
[email protected]cfa856d62014-02-22 07:58:40880 IPC_MESSAGE_HANDLER(FrameHostMsg_DomOperationResponse,
881 OnDomOperationResponse)
[email protected]37b64c52014-07-11 21:14:05882 IPC_MESSAGE_HANDLER(FrameHostMsg_DidChangeThemeColor,
883 OnThemeColorChanged)
[email protected]8ed16472014-04-11 19:02:48884 IPC_MESSAGE_HANDLER(FrameHostMsg_DidFinishDocumentLoad,
885 OnDocumentLoadedInFrame)
886 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_DidDisplayInsecureContent,
[email protected]724159a2010-12-30 01:11:18890 OnDidDisplayInsecureContent)
elawrenceb2ac2a232017-03-27 21:46:25891 IPC_MESSAGE_HANDLER(FrameHostMsg_DidContainInsecureFormAction,
892 OnDidContainInsecureFormAction)
avi3a3332622015-09-01 01:18:54893 IPC_MESSAGE_HANDLER(FrameHostMsg_DidRunInsecureContent,
[email protected]724159a2010-12-30 01:11:18894 OnDidRunInsecureContent)
estark910b4572015-12-09 20:55:41895 IPC_MESSAGE_HANDLER(FrameHostMsg_DidDisplayContentWithCertificateErrors,
896 OnDidDisplayContentWithCertificateErrors)
897 IPC_MESSAGE_HANDLER(FrameHostMsg_DidRunContentWithCertificateErrors,
898 OnDidRunContentWithCertificateErrors)
Dave Tapuska549baab2019-03-19 15:52:40899 IPC_MESSAGE_HANDLER(FrameHostMsg_GoToEntryAtOffset, OnGoToEntryAtOffset)
avia23e16b2015-07-09 14:18:48900 IPC_MESSAGE_HANDLER(FrameHostMsg_RegisterProtocolHandler,
901 OnRegisterProtocolHandler)
902 IPC_MESSAGE_HANDLER(FrameHostMsg_UnregisterProtocolHandler,
903 OnUnregisterProtocolHandler)
kouhei40f03cb2015-09-24 07:47:01904 IPC_MESSAGE_HANDLER(FrameHostMsg_UpdatePageImportanceSignals,
905 OnUpdatePageImportanceSignals)
lukasza178ff092017-06-07 23:39:50906 IPC_MESSAGE_HANDLER(FrameHostMsg_UpdateFaviconURL, OnUpdateFaviconURL)
brettw4b461082016-11-19 18:55:16907#if BUILDFLAG(ENABLE_PLUGINS)
emaxxe70f5e12015-05-29 11:26:00908 IPC_MESSAGE_HANDLER(FrameHostMsg_PepperInstanceCreated,
909 OnPepperInstanceCreated)
910 IPC_MESSAGE_HANDLER(FrameHostMsg_PepperInstanceDeleted,
911 OnPepperInstanceDeleted)
tommyclie6633ca72014-10-31 00:40:42912 IPC_MESSAGE_HANDLER(FrameHostMsg_PepperPluginHung, OnPepperPluginHung)
zqzhang181047e62016-07-01 13:37:17913 IPC_MESSAGE_HANDLER(FrameHostMsg_PepperStartsPlayback,
914 OnPepperStartsPlayback)
915 IPC_MESSAGE_HANDLER(FrameHostMsg_PepperStopsPlayback,
916 OnPepperStopsPlayback)
tommyclie6633ca72014-10-31 00:40:42917 IPC_MESSAGE_HANDLER(FrameHostMsg_PluginCrashed, OnPluginCrashed)
[email protected]c4538072013-03-18 02:17:55918 IPC_MESSAGE_HANDLER_GENERIC(BrowserPluginHostMsg_Attach,
fsamuel833ee7c2015-02-13 23:40:40919 OnBrowserPluginMessage(render_frame_host,
920 message))
thestigc4cac8f2014-09-04 21:17:50921#endif
[email protected]724159a2010-12-30 01:11:18922 IPC_MESSAGE_UNHANDLED(handled = false)
[email protected]e44d1342014-05-16 21:29:33923 IPC_END_MESSAGE_MAP()
[email protected]724159a2010-12-30 01:11:18924
[email protected]724159a2010-12-30 01:11:18925 return handled;
926}
927
[email protected]d1198fd2012-08-13 22:50:19928NavigationControllerImpl& WebContentsImpl::GetController() {
[email protected]f5fa20e2011-12-21 22:35:56929 return controller_;
930}
931
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:41932BrowserContext* WebContentsImpl::GetBrowserContext() {
[email protected]a26023822011-12-29 00:23:55933 return controller_.GetBrowserContext();
[email protected]627e0512011-12-21 22:55:30934}
935
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:41936const GURL& WebContentsImpl::GetURL() {
Peter Boström101d7672018-12-15 00:43:59937 return GetVisibleURL();
[email protected]be1f56ab2011-12-22 06:55:31938}
939
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:41940const GURL& WebContentsImpl::GetVisibleURL() {
[email protected]c854a7e2013-05-21 16:42:24941 // We may not have a navigation entry yet.
[email protected]59167c22013-06-03 18:07:32942 NavigationEntry* entry = controller_.GetVisibleEntry();
[email protected]c854a7e2013-05-21 16:42:24943 return entry ? entry->GetVirtualURL() : GURL::EmptyGURL();
944}
945
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:41946const GURL& WebContentsImpl::GetLastCommittedURL() {
[email protected]c854a7e2013-05-21 16:42:24947 // We may not have a navigation entry yet.
948 NavigationEntry* entry = controller_.GetLastCommittedEntry();
949 return entry ? entry->GetVirtualURL() : GURL::EmptyGURL();
950}
951
[email protected]8ff00d72012-10-23 19:12:21952WebContentsDelegate* WebContentsImpl::GetDelegate() {
[email protected]be1f56ab2011-12-22 06:55:31953 return delegate_;
954}
955
[email protected]8ff00d72012-10-23 19:12:21956void WebContentsImpl::SetDelegate(WebContentsDelegate* delegate) {
[email protected]be1f56ab2011-12-22 06:55:31957 // TODO(cbentzel): remove this debugging code?
958 if (delegate == delegate_)
959 return;
960 if (delegate_)
961 delegate_->Detach(this);
962 delegate_ = delegate;
[email protected]a6b73c62013-02-11 23:05:08963 if (delegate_) {
[email protected]be1f56ab2011-12-22 06:55:31964 delegate_->Attach(this);
[email protected]a6b73c62013-02-11 23:05:08965 // Ensure the visible RVH reflects the new delegate's preferences.
[email protected]86f98a22013-03-20 14:35:00966 if (view_)
[email protected]e85165c642014-06-10 14:34:31967 view_->SetOverscrollControllerEnabled(CanOverscrollContent());
Joel Einbinder848001c2017-09-19 21:39:31968 if (GetRenderViewHost())
969 RenderFrameDevToolsAgentHost::WebContentsCreated(this);
[email protected]a6b73c62013-02-11 23:05:08970 }
[email protected]be1f56ab2011-12-22 06:55:31971}
972
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:41973RenderFrameHostImpl* WebContentsImpl::GetMainFrame() {
[email protected]94d0cc12013-12-18 00:07:41974 return frame_tree_.root()->current_frame_host();
[email protected]60eca4eb2013-12-06 00:02:16975}
976
nick53d5cbf2015-04-23 22:50:14977RenderFrameHostImpl* WebContentsImpl::GetFocusedFrame() {
978 FrameTreeNode* focused_node = frame_tree_.GetFocusedFrame();
979 if (!focused_node)
980 return nullptr;
981 return focused_node->current_frame_host();
[email protected]9c9343b2014-03-08 02:56:07982}
983
rob3e2a0732016-01-06 21:22:09984RenderFrameHostImpl* WebContentsImpl::FindFrameByFrameTreeNodeId(
creisf71a2632017-05-04 19:03:50985 int frame_tree_node_id,
986 int process_id) {
987 FrameTreeNode* frame = frame_tree_.FindByID(frame_tree_node_id);
988
989 // Sanity check that this is in the caller's expected process. Otherwise a
990 // recent cross-process navigation may have led to a privilege change that the
991 // caller is not expecting.
992 if (!frame ||
993 frame->current_frame_host()->GetProcess()->GetID() != process_id)
994 return nullptr;
995
996 return frame->current_frame_host();
997}
998
999RenderFrameHostImpl* WebContentsImpl::UnsafeFindFrameByFrameTreeNodeId(
rob3e2a0732016-01-06 21:22:091000 int frame_tree_node_id) {
creisf71a2632017-05-04 19:03:501001 // Beware using this! The RenderFrameHost may have changed since the caller
1002 // obtained frame_tree_node_id.
rob3e2a0732016-01-06 21:22:091003 FrameTreeNode* frame = frame_tree_.FindByID(frame_tree_node_id);
1004 return frame ? frame->current_frame_host() : nullptr;
1005}
1006
[email protected]a86c0e962013-12-17 17:10:391007void WebContentsImpl::ForEachFrame(
Lei Zhangebcc6302018-01-12 19:46:451008 const base::RepeatingCallback<void(RenderFrameHost*)>& on_frame) {
dcheng57e39e22016-01-21 00:25:381009 for (FrameTreeNode* node : frame_tree_.Nodes()) {
1010 on_frame.Run(node->current_frame_host());
1011 }
[email protected]a86c0e962013-12-17 17:10:391012}
1013
dchengafb53e22016-02-04 08:11:081014std::vector<RenderFrameHost*> WebContentsImpl::GetAllFrames() {
1015 std::vector<RenderFrameHost*> frame_hosts;
1016 for (FrameTreeNode* node : frame_tree_.Nodes())
1017 frame_hosts.push_back(node->current_frame_host());
1018 return frame_hosts;
1019}
1020
lukaszacbdf52e2016-01-15 21:19:511021int WebContentsImpl::SendToAllFrames(IPC::Message* message) {
1022 int number_of_messages = 0;
dchengafb53e22016-02-04 08:11:081023 for (RenderFrameHost* rfh : GetAllFrames()) {
lukasza1a9ab2422016-03-02 18:47:251024 if (!rfh->IsRenderFrameLive())
1025 continue;
1026
dchengafb53e22016-02-04 08:11:081027 ++number_of_messages;
1028 IPC::Message* message_copy = new IPC::Message(*message);
1029 message_copy->set_routing_id(rfh->GetRoutingID());
1030 rfh->Send(message_copy);
1031 }
[email protected]a86c0e962013-12-17 17:10:391032 delete message;
lukaszacbdf52e2016-01-15 21:19:511033 return number_of_messages;
[email protected]a86c0e962013-12-17 17:10:391034}
1035
lfgdb5c4ed2016-03-04 23:09:071036void WebContentsImpl::SendPageMessage(IPC::Message* msg) {
alexmos136fd6e62016-08-15 20:58:411037 frame_tree_.root()->render_manager()->SendPageMessage(msg, nullptr);
lfgdb5c4ed2016-03-04 23:09:071038}
1039
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411040RenderViewHostImpl* WebContentsImpl::GetRenderViewHost() {
[email protected]fa944cb82013-11-15 17:51:211041 return GetRenderManager()->current_host();
[email protected]be1f56ab2011-12-22 06:55:311042}
1043
creis89a0f782015-05-27 16:13:171044void WebContentsImpl::CancelActiveAndPendingDialogs() {
avi5d3b8692016-10-12 22:00:461045 if (dialog_manager_) {
avi6879fcf2017-01-21 05:27:531046 dialog_manager_->CancelDialogs(this, /*reset_state=*/false);
avi5d3b8692016-10-12 22:00:461047 }
creis89a0f782015-05-27 16:13:171048 if (browser_plugin_embedder_)
1049 browser_plugin_embedder_->CancelGuestDialogs();
1050}
1051
naskoc0fceff2015-04-30 15:53:521052void WebContentsImpl::ClosePage() {
1053 GetRenderViewHost()->ClosePage();
1054}
1055
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411056RenderWidgetHostView* WebContentsImpl::GetRenderWidgetHostView() {
[email protected]fa944cb82013-11-15 17:51:211057 return GetRenderManager()->GetRenderWidgetHostView();
[email protected]be1f56ab2011-12-22 06:55:311058}
1059
lfg265a2672016-04-23 03:11:021060RenderWidgetHostView* WebContentsImpl::GetTopLevelRenderWidgetHostView() {
1061 if (GetOuterWebContents())
1062 return GetOuterWebContents()->GetTopLevelRenderWidgetHostView();
1063 return GetRenderManager()->GetRenderWidgetHostView();
1064}
1065
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411066RenderWidgetHostView* WebContentsImpl::GetFullscreenRenderWidgetHostView() {
vmpstr6d9996c82017-02-23 00:43:251067 if (auto* widget_host = GetFullscreenRenderWidgetHost())
ekaramadaeb3c5c2016-11-09 03:22:351068 return widget_host->GetView();
1069 return nullptr;
[email protected]4aebbca2013-09-17 22:26:491070}
1071
[email protected]8ff00d72012-10-23 19:12:211072WebContentsView* WebContentsImpl::GetView() const {
[email protected]be1f56ab2011-12-22 06:55:311073 return view_.get();
1074}
1075
leon.han552e9de2017-02-09 14:37:301076void WebContentsImpl::OnScreenOrientationChange() {
1077 DCHECK(screen_orientation_provider_);
Becca Hughese9e27952018-06-25 17:08:271078 screen_orientation_provider_->OnOrientationChange();
leon.hane4db177a2017-02-07 14:19:161079}
1080
Alan Cutter92d59252019-03-29 02:32:331081base::Optional<SkColor> WebContentsImpl::GetThemeColor() {
yusufod41c5f92015-03-06 00:14:281082 return theme_color_;
1083}
1084
Doug Turner63f3c7b2017-07-29 05:10:011085void WebContentsImpl::SetAccessibilityMode(ui::AXMode mode) {
[email protected]95640212014-07-26 18:14:301086 if (mode == accessibility_mode_)
1087 return;
1088
dmazzonif8a31042016-12-03 00:52:131089 // Don't allow accessibility to be enabled for WebContents that are never
1090 // visible, like background pages.
1091 if (IsNeverVisible())
1092 return;
1093
[email protected]95640212014-07-26 18:14:301094 accessibility_mode_ = mode;
dcheng57e39e22016-01-21 00:25:381095
1096 for (FrameTreeNode* node : frame_tree_.Nodes()) {
dmazzonif8a31042016-12-03 00:52:131097 UpdateAccessibilityModeOnFrame(node->current_frame_host());
clamy610c63b32017-12-22 15:05:181098 // Also update accessibility mode on the speculative RenderFrameHost for
1099 // this FrameTreeNode, if one exists.
Alex Moshchuk3d8a1f392017-10-24 19:01:261100 RenderFrameHost* speculative_frame_host =
1101 node->render_manager()->speculative_frame_host();
1102 if (speculative_frame_host)
1103 UpdateAccessibilityModeOnFrame(speculative_frame_host);
dcheng57e39e22016-01-21 00:25:381104 }
[email protected]95640212014-07-26 18:14:301105}
1106
Doug Turner63f3c7b2017-07-29 05:10:011107void WebContentsImpl::AddAccessibilityMode(ui::AXMode mode) {
1108 ui::AXMode new_mode(accessibility_mode_);
dougtcd3dad732017-03-14 03:26:231109 new_mode |= mode;
1110 SetAccessibilityMode(new_mode);
[email protected]95640212014-07-26 18:14:301111}
1112
Avi Drissman1e620f32018-03-16 13:57:291113// Helper class used by WebContentsImpl::RequestAXTreeSnapshot.
1114// Handles the callbacks from parallel snapshot requests to each frame,
1115// and feeds the results to an AXTreeCombiner, which converts them into a
1116// single combined accessibility tree.
1117class WebContentsImpl::AXTreeSnapshotCombiner
1118 : public base::RefCounted<AXTreeSnapshotCombiner> {
1119 public:
1120 explicit AXTreeSnapshotCombiner(AXTreeSnapshotCallback callback)
1121 : callback_(std::move(callback)) {}
1122
1123 AXTreeSnapshotCallback AddFrame(bool is_root) {
1124 // Adds a reference to |this|.
1125 return base::BindOnce(&AXTreeSnapshotCombiner::ReceiveSnapshot, this,
1126 is_root);
1127 }
1128
1129 void ReceiveSnapshot(bool is_root, const ui::AXTreeUpdate& snapshot) {
1130 combiner_.AddTree(snapshot, is_root);
1131 }
1132
1133 private:
1134 friend class base::RefCounted<AXTreeSnapshotCombiner>;
1135
1136 // This is called automatically after the last call to ReceiveSnapshot
1137 // when there are no more references to this object.
1138 ~AXTreeSnapshotCombiner() {
1139 combiner_.Combine();
1140 std::move(callback_).Run(combiner_.combined());
1141 }
1142
1143 ui::AXTreeCombiner combiner_;
1144 AXTreeSnapshotCallback callback_;
1145};
1146
1147void WebContentsImpl::RequestAXTreeSnapshot(AXTreeSnapshotCallback callback,
1148 ui::AXMode ax_mode) {
dmazzonid95ae842016-04-12 21:17:391149 // Send a request to each of the frames in parallel. Each one will return
1150 // an accessibility tree snapshot, and AXTreeSnapshotCombiner will combine
1151 // them into a single tree and call |callback| with that result, then
1152 // delete |combiner|.
Avi Drissman1e620f32018-03-16 13:57:291153 FrameTreeNode* root_node = frame_tree_.root();
tzik58faa742018-08-01 10:54:181154 auto combiner =
1155 base::MakeRefCounted<AXTreeSnapshotCombiner>(std::move(callback));
Avi Drissman1e620f32018-03-16 13:57:291156
tzik58faa742018-08-01 10:54:181157 RecursiveRequestAXTreeSnapshotOnFrame(root_node, combiner.get(), ax_mode);
Avi Drissman1e620f32018-03-16 13:57:291158}
1159
1160void WebContentsImpl::RecursiveRequestAXTreeSnapshotOnFrame(
1161 FrameTreeNode* root_node,
1162 AXTreeSnapshotCombiner* combiner,
1163 ui::AXMode ax_mode) {
dmazzonid95ae842016-04-12 21:17:391164 for (FrameTreeNode* frame_tree_node : frame_tree_.Nodes()) {
Avi Drissman1e620f32018-03-16 13:57:291165 WebContentsImpl* inner_contents =
1166 node_.GetInnerWebContentsInFrame(frame_tree_node);
1167 if (inner_contents) {
1168 inner_contents->RecursiveRequestAXTreeSnapshotOnFrame(root_node, combiner,
1169 ax_mode);
1170 } else {
1171 bool is_root = frame_tree_node == root_node;
1172 frame_tree_node->current_frame_host()->RequestAXTreeSnapshot(
1173 combiner->AddFrame(is_root), ax_mode);
1174 }
dmazzonid95ae842016-04-12 21:17:391175 }
dmazzoni83ba5c82015-04-14 07:11:511176}
1177
Becca Hughes3b5a43482018-07-17 22:31:551178void WebContentsImpl::NotifyViewportFitChanged(
1179 blink::mojom::ViewportFit value) {
1180 for (auto& observer : observers_)
1181 observer.ViewportFitChanged(value);
1182}
1183
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411184FindRequestManager* WebContentsImpl::GetFindRequestManagerForTesting() {
Ehsan Karamad6beb2ea2018-11-25 18:15:131185 return GetFindRequestManager();
1186}
1187
mcnee336ea2c2017-05-23 22:50:591188#if !defined(OS_ANDROID)
akabac6bd1212018-06-25 20:10:481189void WebContentsImpl::UpdateZoom() {
1190 RenderWidgetHostImpl* rwh = GetRenderViewHost()->GetWidget();
1191 if (rwh->GetView())
1192 rwh->SynchronizeVisualProperties();
wjmaclean64951902016-04-29 20:59:121193}
1194
wjmaclean64951902016-04-29 20:59:121195
1196void WebContentsImpl::UpdateZoomIfNecessary(const std::string& scheme,
akabac6bd1212018-06-25 20:10:481197 const std::string& host) {
wjmaclean64951902016-04-29 20:59:121198 NavigationEntry* entry = GetController().GetLastCommittedEntry();
1199 if (!entry)
1200 return;
1201
1202 GURL url = HostZoomMap::GetURLFromEntry(entry);
1203 if (host != net::GetHostOrSpecFromURL(url) ||
1204 (!scheme.empty() && !url.SchemeIs(scheme))) {
1205 return;
1206 }
1207
akabac6bd1212018-06-25 20:10:481208 UpdateZoom();
wjmaclean64951902016-04-29 20:59:121209}
mcnee336ea2c2017-05-23 22:50:591210#endif // !defined(OS_ANDROID)
wjmaclean64951902016-04-29 20:59:121211
rockotf62002a2016-09-15 00:08:591212base::Closure WebContentsImpl::AddBindingSet(
1213 const std::string& interface_name,
1214 WebContentsBindingSet* binding_set) {
1215 auto result =
1216 binding_sets_.insert(std::make_pair(interface_name, binding_set));
1217 DCHECK(result.second);
1218 return base::Bind(&WebContentsImpl::RemoveBindingSet,
1219 weak_factory_.GetWeakPtr(), interface_name);
1220}
1221
rockot8bbad222017-03-22 04:34:051222WebContentsBindingSet* WebContentsImpl::GetBindingSet(
1223 const std::string& interface_name) {
1224 auto it = binding_sets_.find(interface_name);
1225 if (it == binding_sets_.end())
1226 return nullptr;
1227 return it->second;
1228}
1229
paulmeyerfeafc2d2017-04-25 21:46:401230std::vector<WebContentsImpl*> WebContentsImpl::GetWebContentsAndAllInner() {
1231 std::vector<WebContentsImpl*> all_contents(1, this);
1232
1233 for (size_t i = 0; i != all_contents.size(); ++i) {
1234 for (auto* inner_contents : all_contents[i]->GetInnerWebContents()) {
Lucas Furukawa Gadani2ec00c82018-12-14 15:53:161235 all_contents.push_back(static_cast<WebContentsImpl*>(inner_contents));
paulmeyerfeafc2d2017-04-25 21:46:401236 }
1237 }
1238
1239 return all_contents;
1240}
1241
Sam McNally145fb172017-05-10 00:13:231242void WebContentsImpl::NotifyManifestUrlChanged(
1243 const base::Optional<GURL>& manifest_url) {
1244 for (auto& observer : observers_)
1245 observer.DidUpdateWebManifestURL(manifest_url);
1246}
1247
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411248WebUI* WebContentsImpl::GetWebUI() {
carlosk35f35af2015-12-01 10:55:401249 WebUI* commited_web_ui = GetCommittedWebUI();
1250 return commited_web_ui ? commited_web_ui
1251 : GetRenderManager()->GetNavigatingWebUI();
[email protected]be1f56ab2011-12-22 06:55:311252}
1253
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411254WebUI* WebContentsImpl::GetCommittedWebUI() {
carlosk35f35af2015-12-01 10:55:401255 return frame_tree_.root()->current_frame_host()->web_ui();
[email protected]d5f942ba2008-09-26 19:30:341256}
1257
Changwan Ryuc1134a82018-03-07 02:10:141258void WebContentsImpl::SetUserAgentOverride(const std::string& override,
1259 bool override_in_new_tabs) {
[email protected]bf70edce2012-06-20 22:32:221260 if (GetUserAgentOverride() == override)
1261 return;
1262
Changwan Ryuc1134a82018-03-07 02:10:141263 should_override_user_agent_in_new_tabs_ = override_in_new_tabs;
1264
[email protected]bf70edce2012-06-20 22:32:221265 renderer_preferences_.user_agent_override = override;
1266
1267 // Send the new override string to the renderer.
1268 RenderViewHost* host = GetRenderViewHost();
1269 if (host)
1270 host->SyncRendererPrefs();
1271
1272 // Reload the page if a load is currently in progress to avoid having
1273 // different parts of the page loaded using different user agents.
[email protected]6286a372013-10-09 04:03:271274 NavigationEntry* entry = controller_.GetVisibleEntry();
Ivan Kotenkov2c0d2bb32017-11-01 15:41:281275 if (IsLoading() && entry != nullptr && entry->GetIsOverridingUserAgent())
toyoshim6142d96f2016-12-19 09:07:251276 controller_.Reload(ReloadType::BYPASSING_CACHE, true);
[email protected]8d0f3312012-08-18 01:47:531277
ericwilligersde386342016-10-19 02:35:091278 for (auto& observer : observers_)
1279 observer.UserAgentOverrideSet(override);
[email protected]86ef6a392012-05-11 22:03:111280}
1281
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411282const std::string& WebContentsImpl::GetUserAgentOverride() {
[email protected]bf70edce2012-06-20 22:32:221283 return renderer_preferences_.user_agent_override;
[email protected]86ef6a392012-05-11 22:03:111284}
1285
Changwan Ryuc1134a82018-03-07 02:10:141286bool WebContentsImpl::ShouldOverrideUserAgentInNewTabs() {
1287 return should_override_user_agent_in_new_tabs_;
1288}
1289
dmazzonidd3d51a72016-12-14 18:41:011290void WebContentsImpl::EnableWebContentsOnlyAccessibilityMode() {
Dominic Mazzonid9fda172019-03-07 17:12:071291 // If accessibility is already enabled, we'll need to force a reset
1292 // in order to ensure new observers of accessibility events get the
1293 // full accessibility tree from scratch.
1294 bool need_reset = GetAccessibilityMode().has_mode(ui::AXMode::kWebContents);
1295
1296 ui::AXMode desired_mode =
1297 GetContentClient()->browser()->GetAXModeForBrowserContext(
1298 GetBrowserContext());
1299 desired_mode |= ui::kAXModeWebContentsOnly;
1300 AddAccessibilityMode(desired_mode);
1301
1302 if (need_reset) {
dchengafb53e22016-02-04 08:11:081303 for (RenderFrameHost* rfh : GetAllFrames())
1304 ResetAccessibility(rfh);
dchengafb53e22016-02-04 08:11:081305 }
[email protected]95640212014-07-26 18:14:301306}
1307
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411308bool WebContentsImpl::IsWebContentsOnlyAccessibilityModeForTesting() {
Doug Turner63f3c7b2017-07-29 05:10:011309 return accessibility_mode_ == ui::kAXModeWebContentsOnly;
[email protected]95640212014-07-26 18:14:301310}
1311
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411312bool WebContentsImpl::IsFullAccessibilityModeForTesting() {
Doug Turner63f3c7b2017-07-29 05:10:011313 return accessibility_mode_ == ui::kAXModeComplete;
[email protected]95640212014-07-26 18:14:301314}
1315
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411316const PageImportanceSignals& WebContentsImpl::GetPageImportanceSignals() {
kouhei40f03cb2015-09-24 07:47:011317 return page_importance_signals_;
1318}
1319
Guido Urdanetaf4008002018-05-03 10:43:041320const std::string& WebContentsImpl::GetMediaDeviceGroupIDSaltBase() const {
1321 return media_device_group_id_salt_base_;
1322}
1323
Becca Hughes6daf5662018-06-27 16:50:541324#if defined(OS_ANDROID)
1325
1326void WebContentsImpl::SetDisplayCutoutSafeArea(gfx::Insets insets) {
Becca Hughes86334622018-07-09 21:29:381327 if (display_cutout_host_impl_)
1328 display_cutout_host_impl_->SetDisplayCutoutSafeArea(insets);
Becca Hughes6daf5662018-06-27 16:50:541329}
1330
1331#endif
1332
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411333const base::string16& WebContentsImpl::GetTitle() {
[email protected]4c6092c5b2009-06-06 00:23:551334 // Transient entries take precedence. They are used for interstitial pages
1335 // that are shown on top of existing pages.
[email protected]10f417c52011-12-28 21:04:231336 NavigationEntry* entry = controller_.GetTransientEntry();
[email protected]45d0ef7f2011-01-05 13:46:231337 if (entry) {
jshin1fb76462016-04-05 22:13:031338 return entry->GetTitleForDisplay();
[email protected]45d0ef7f2011-01-05 13:46:231339 }
carlosk35f35af2015-12-01 10:55:401340
1341 WebUI* navigating_web_ui = GetRenderManager()->GetNavigatingWebUI();
1342 WebUI* our_web_ui = navigating_web_ui
1343 ? navigating_web_ui
1344 : GetRenderManager()->current_frame_host()->web_ui();
1345
[email protected]7ade2732011-02-10 00:13:581346 if (our_web_ui) {
[email protected]96d185d2009-04-24 03:28:541347 // Don't override the title in view source mode.
[email protected]59167c22013-06-03 18:07:321348 entry = controller_.GetVisibleEntry();
[email protected]96d185d2009-04-24 03:28:541349 if (!(entry && entry->IsViewSourceMode())) {
[email protected]e0112912011-02-02 22:54:351350 // Give the Web UI the chance to override our title.
[email protected]fcf75d42013-12-03 20:11:261351 const base::string16& title = our_web_ui->GetOverriddenTitle();
[email protected]96d185d2009-04-24 03:28:541352 if (!title.empty())
1353 return title;
1354 }
1355 }
1356
1357 // We use the title for the last committed entry rather than a pending
1358 // navigation entry. For example, when the user types in a URL, we want to
1359 // keep the old page's title until the new load has committed and we get a new
1360 // title.
[email protected]96d185d2009-04-24 03:28:541361 entry = controller_.GetLastCommittedEntry();
[email protected]59167c22013-06-03 18:07:321362
creis072b61f2015-09-28 20:52:241363 // We make an exception for initial navigations. We only want to use the title
1364 // from the visible entry if:
1365 // 1. The pending entry has been explicitly assigned a title to display.
1366 // 2. The user is doing a history navigation in a new tab (e.g., Ctrl+Back),
1367 // which case there is a pending entry index other than -1.
1368 //
1369 // Otherwise, we want to stick with the last committed entry's title during
1370 // new navigations, which have pending entries at index -1 with no title.
1371 if (controller_.IsInitialNavigation() &&
1372 ((controller_.GetVisibleEntry() &&
1373 !controller_.GetVisibleEntry()->GetTitle().empty()) ||
1374 controller_.GetPendingEntryIndex() != -1)) {
1375 entry = controller_.GetVisibleEntry();
[email protected]9eeafc012013-11-25 23:49:471376 }
[email protected]59167c22013-06-03 18:07:321377
[email protected]45d0ef7f2011-01-05 13:46:231378 if (entry) {
jshin1fb76462016-04-05 22:13:031379 return entry->GetTitleForDisplay();
[email protected]45d0ef7f2011-01-05 13:46:231380 }
[email protected]987fc3a2011-05-26 14:18:091381
1382 // |page_title_when_no_navigation_entry_| is finally used
1383 // if no title cannot be retrieved.
1384 return page_title_when_no_navigation_entry_;
[email protected]96d185d2009-04-24 03:28:541385}
1386
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411387SiteInstanceImpl* WebContentsImpl::GetSiteInstance() {
[email protected]fa944cb82013-11-15 17:51:211388 return GetRenderManager()->current_host()->GetSiteInstance();
[email protected]96d185d2009-04-24 03:28:541389}
1390
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411391bool WebContentsImpl::IsLoading() {
clamy44e84ce2016-02-22 15:38:251392 return frame_tree_.IsLoading() &&
creisadca9862017-06-16 19:12:111393 !(ShowingInterstitialPage() && interstitial_page_->pause_throbber());
[email protected]3c9e1872010-11-18 16:17:491394}
1395
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411396double WebContentsImpl::GetLoadProgress() {
Peter Boström18a40fa2018-10-31 19:03:501397 return frame_tree_.load_progress();
1398}
1399
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411400bool WebContentsImpl::IsLoadingToDifferentDocument() {
clamy44e84ce2016-02-22 15:38:251401 return IsLoading() && is_load_to_different_document_;
[email protected]6dfed692014-05-22 04:18:031402}
1403
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411404bool WebContentsImpl::IsWaitingForResponse() {
Peter Boströma9a27f6e2018-11-21 13:12:191405 NavigationRequest* ongoing_navigation_request =
1406 frame_tree_.root()->navigation_request();
1407
1408 // An ongoing navigation request means we're waiting for a response.
1409 return ongoing_navigation_request != nullptr;
[email protected]be1f56ab2011-12-22 06:55:311410}
1411
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411412const net::LoadStateWithParam& WebContentsImpl::GetLoadState() {
[email protected]be1f56ab2011-12-22 06:55:311413 return load_state_;
1414}
1415
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411416const base::string16& WebContentsImpl::GetLoadStateHost() {
[email protected]be1f56ab2011-12-22 06:55:311417 return load_state_host_;
1418}
1419
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411420uint64_t WebContentsImpl::GetUploadSize() {
[email protected]be1f56ab2011-12-22 06:55:311421 return upload_size_;
1422}
1423
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411424uint64_t WebContentsImpl::GetUploadPosition() {
[email protected]be1f56ab2011-12-22 06:55:311425 return upload_position_;
1426}
1427
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411428const std::string& WebContentsImpl::GetEncoding() {
[email protected]ef3adfc2014-05-11 00:04:541429 return canonical_encoding_;
[email protected]be1f56ab2011-12-22 06:55:311430}
1431
Charles Zhaod72f99d2018-09-17 14:18:271432bool WebContentsImpl::WasDiscarded() {
1433 return GetFrameTree()->root()->was_discarded();
1434}
1435
Shubhie Panickerddf2a4e2018-03-06 00:09:061436void WebContentsImpl::SetWasDiscarded(bool was_discarded) {
1437 GetFrameTree()->root()->set_was_discarded();
1438}
1439
[email protected]222f5822014-02-05 23:40:491440void WebContentsImpl::IncrementCapturerCount(const gfx::Size& capture_size) {
[email protected]5a652232013-02-12 06:15:251441 DCHECK(!is_being_destroyed_);
Francois Doraye6161152018-03-27 22:05:371442 const bool was_captured = IsBeingCaptured();
[email protected]54597982013-02-06 01:59:551443 ++capturer_count_;
1444 DVLOG(1) << "There are now " << capturer_count_
1445 << " capturing(s) of WebContentsImpl@" << this;
[email protected]222f5822014-02-05 23:40:491446
1447 // Note: This provides a hint to upstream code to size the views optimally
1448 // for quality (e.g., to avoid scaling).
1449 if (!capture_size.IsEmpty() && preferred_size_for_capture_.IsEmpty()) {
1450 preferred_size_for_capture_ = capture_size;
1451 OnPreferredSizeChanged(preferred_size_);
1452 }
ccameron8807c38f2015-01-17 00:29:191453
Francois Doraye6161152018-03-27 22:05:371454 if (GetVisibility() != Visibility::VISIBLE && !was_captured) {
1455 // Ensure that all views act as if they were visible before capture begins.
1456 // TODO(fdoray): Replace RenderWidgetHostView::WasUnOccluded() with a method
1457 // to explicitly notify the RenderWidgetHostView that capture began.
1458 // https://crbug.com/668690
Ehsan Karamada03e1582018-12-05 23:07:561459 if (auto* main_view = GetRenderWidgetHostView())
1460 main_view->WasUnOccluded();
1461 if (!ShowingInterstitialPage())
1462 SetVisibilityForChildViews(true);
Francois Doraye6161152018-03-27 22:05:371463 }
[email protected]54597982013-02-06 01:59:551464}
1465
1466void WebContentsImpl::DecrementCapturerCount() {
1467 --capturer_count_;
1468 DVLOG(1) << "There are now " << capturer_count_
1469 << " capturing(s) of WebContentsImpl@" << this;
1470 DCHECK_LE(0, capturer_count_);
1471
[email protected]5a652232013-02-12 06:15:251472 if (is_being_destroyed_)
1473 return;
1474
Francois Doray24fc62c2017-12-11 17:27:331475 if (!IsBeingCaptured()) {
[email protected]222f5822014-02-05 23:40:491476 const gfx::Size old_size = preferred_size_for_capture_;
1477 preferred_size_for_capture_ = gfx::Size();
1478 OnPreferredSizeChanged(old_size);
[email protected]222f5822014-02-05 23:40:491479
Francois Doraye6161152018-03-27 22:05:371480 if (visibility_ == Visibility::HIDDEN) {
Francois Doray18800972017-08-25 22:38:581481 DVLOG(1) << "Executing delayed WasHidden().";
1482 WasHidden();
Francois Doraye6161152018-03-27 22:05:371483 } else if (visibility_ == Visibility::OCCLUDED) {
Mitsuru Oshima04a80242018-03-03 03:06:151484 WasOccluded();
Francois Doraye6161152018-03-27 22:05:371485 }
[email protected]54597982013-02-06 01:59:551486 }
[email protected]be1f56ab2011-12-22 06:55:311487}
1488
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411489bool WebContentsImpl::IsBeingCaptured() {
Francois Doray24fc62c2017-12-11 17:27:331490 return capturer_count_ > 0;
[email protected]f2bd40812013-07-20 04:30:441491}
1492
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411493bool WebContentsImpl::IsAudioMuted() {
Max Morinb5506f72018-05-16 07:33:101494 if (base::FeatureList::IsEnabled(features::kAudioServiceAudioStreams)) {
1495 return audio_stream_factory_ && audio_stream_factory_->IsMuted();
1496 }
miu50f97892014-09-22 22:49:521497 return audio_muter_.get() && audio_muter_->is_muting();
1498}
1499
1500void WebContentsImpl::SetAudioMuted(bool mute) {
1501 DVLOG(1) << "SetAudioMuted(mute=" << mute << "), was " << IsAudioMuted()
1502 << " for WebContentsImpl@" << this;
1503
1504 if (mute == IsAudioMuted())
1505 return;
1506
Max Morinb5506f72018-05-16 07:33:101507 if (base::FeatureList::IsEnabled(features::kAudioServiceAudioStreams)) {
1508 GetAudioStreamFactory()->SetMuted(mute);
miu50f97892014-09-22 22:49:521509 } else {
Max Morinb5506f72018-05-16 07:33:101510 if (mute) {
1511 if (!audio_muter_)
1512 audio_muter_.reset(new WebContentsAudioMuter(this));
1513 audio_muter_->StartMuting();
1514 } else {
1515 DCHECK(audio_muter_);
1516 audio_muter_->StopMuting();
1517 }
miu50f97892014-09-22 22:49:521518 }
1519
ericwilligersde386342016-10-19 02:35:091520 for (auto& observer : observers_)
1521 observer.DidUpdateAudioMutingState(mute);
wjmaclean2f797c782016-01-07 14:52:031522
Jan Rucka442c83f2017-08-08 13:27:541523 // Notification for UI updates in response to the changed muting state.
1524 NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB);
1525}
1526
1527bool WebContentsImpl::IsCurrentlyAudible() {
Chris Hamiltondf0d72cd2018-05-29 16:23:531528 return is_currently_audible_;
miu50f97892014-09-22 22:49:521529}
1530
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411531bool WebContentsImpl::IsConnectedToBluetoothDevice() {
ortunodf4d7982016-04-08 02:33:351532 return bluetooth_connected_device_count_ > 0;
ortuno32e7db3c2016-03-29 16:14:201533}
1534
Lucas Furukawa Gadani4b4eed02019-06-04 23:12:041535bool WebContentsImpl::IsConnectedToSerialPort() {
Reilly Grant5e7c79b22019-04-09 17:26:201536 return serial_active_frame_count_ > 0;
1537}
1538
Marijn Kruisselbrinkf1714aa22019-07-02 03:45:331539bool WebContentsImpl::HasNativeFileSystemDirectoryHandles() {
Marijn Kruisselbrink46fa4bd2019-07-11 18:15:471540 return !native_file_system_directory_handles_.empty();
1541}
1542
1543std::vector<base::FilePath>
1544WebContentsImpl::GetNativeFileSystemDirectoryHandles() {
1545 std::vector<base::FilePath> result;
1546 result.reserve(native_file_system_directory_handles_.size());
1547 for (auto const& entry : native_file_system_directory_handles_)
1548 result.push_back(entry.first);
1549 return result;
Marijn Kruisselbrinkf1714aa22019-07-02 03:45:331550}
1551
1552bool WebContentsImpl::HasWritableNativeFileSystemHandles() {
1553 return native_file_system_writable_handle_count_ > 0;
1554}
1555
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411556bool WebContentsImpl::HasPictureInPictureVideo() {
sawtellea7333a82018-05-31 02:36:361557 return has_picture_in_picture_video_;
1558}
1559
1560void WebContentsImpl::SetHasPictureInPictureVideo(
1561 bool has_picture_in_picture_video) {
1562 // If status of |this| is already accurate, there is no need to update.
1563 if (has_picture_in_picture_video == has_picture_in_picture_video_)
1564 return;
1565 has_picture_in_picture_video_ = has_picture_in_picture_video;
1566 NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB);
François Beaufort058085d2018-08-30 15:29:141567 for (auto& observer : observers_)
1568 observer.MediaPictureInPictureChanged(has_picture_in_picture_video_);
sawtellea7333a82018-05-31 02:36:361569}
1570
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411571bool WebContentsImpl::IsCrashed() {
Dominic Mazzoni73a48322018-05-25 18:14:071572 switch (crashed_status_) {
1573 case base::TERMINATION_STATUS_PROCESS_CRASHED:
1574 case base::TERMINATION_STATUS_ABNORMAL_TERMINATION:
1575 case base::TERMINATION_STATUS_PROCESS_WAS_KILLED:
1576 case base::TERMINATION_STATUS_OOM:
1577 case base::TERMINATION_STATUS_LAUNCH_FAILED:
oshima620225722015-06-04 19:45:271578#if defined(OS_CHROMEOS)
Dominic Mazzoni73a48322018-05-25 18:14:071579 case base::TERMINATION_STATUS_PROCESS_WAS_KILLED_BY_OOM:
oshima620225722015-06-04 19:45:271580#endif
Dominic Mazzoni73a48322018-05-25 18:14:071581#if defined(OS_ANDROID)
1582 case base::TERMINATION_STATUS_OOM_PROTECTED:
1583#endif
1584 return true;
1585 case base::TERMINATION_STATUS_NORMAL_TERMINATION:
1586 case base::TERMINATION_STATUS_STILL_RUNNING:
1587 return false;
1588 case base::TERMINATION_STATUS_MAX_ENUM:
1589 NOTREACHED();
1590 return false;
1591 }
1592
1593 NOTREACHED();
1594 return false;
[email protected]3c9e1872010-11-18 16:17:491595}
1596
[email protected]b172aee2012-04-10 17:05:261597void WebContentsImpl::SetIsCrashed(base::TerminationStatus status,
1598 int error_code) {
[email protected]443b80e2010-12-14 00:42:231599 if (status == crashed_status_)
[email protected]d5f942ba2008-09-26 19:30:341600 return;
1601
[email protected]443b80e2010-12-14 00:42:231602 crashed_status_ = status;
1603 crashed_error_code_ = error_code;
[email protected]8ff00d72012-10-23 19:12:211604 NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB);
[email protected]d5f942ba2008-09-26 19:30:341605}
1606
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411607base::TerminationStatus WebContentsImpl::GetCrashedStatus() {
[email protected]be1f56ab2011-12-22 06:55:311608 return crashed_status_;
1609}
1610
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411611int WebContentsImpl::GetCrashedErrorCode() {
afakhry98241832016-03-11 19:27:471612 return crashed_error_code_;
1613}
1614
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411615bool WebContentsImpl::IsBeingDestroyed() {
[email protected]be1f56ab2011-12-22 06:55:311616 return is_being_destroyed_;
1617}
1618
[email protected]7f924832014-08-09 05:57:221619void WebContentsImpl::NotifyNavigationStateChanged(
1620 InvalidateTypes changed_flags) {
dalecurtis88c240072015-12-09 02:11:181621 // Notify the media observer of potential audibility changes.
1622 if (changed_flags & INVALIDATE_TYPE_TAB) {
mlamouri44e4ef42016-01-20 18:42:271623 media_web_contents_observer_->MaybeUpdateAudibleState();
dalecurtisbc6572e12014-09-12 19:22:301624 }
1625
[email protected]d5f942ba2008-09-26 19:30:341626 if (delegate_)
1627 delegate_->NavigationStateChanged(this, changed_flags);
wjmaclean1dabf3e32016-05-13 23:34:441628
1629 if (GetOuterWebContents())
1630 GetOuterWebContents()->NotifyNavigationStateChanged(changed_flags);
[email protected]d5f942ba2008-09-26 19:30:341631}
1632
W. James MacLean7c8581072019-06-18 18:07:531633void WebContentsImpl::NotifyVisibleViewportSizeChanged(
1634 const gfx::Size& visible_viewport_size) {
1635 SendPageMessage(new PageMsg_UpdatePageVisualProperties(
1636 MSG_ROUTING_NONE, visible_viewport_size));
1637}
1638
W. James MacLean6f34b042019-07-12 19:25:301639RenderFrameHostImpl* WebContentsImpl::GetFocusedFrameFromFocusedDelegate() {
1640 FrameTreeNode* focused_node =
1641 GetFocusedWebContents()->frame_tree_.GetFocusedFrame();
1642 return focused_node ? focused_node->current_frame_host() : nullptr;
1643}
1644
Chris Hamiltondf0d72cd2018-05-29 16:23:531645void WebContentsImpl::OnAudioStateChanged() {
1646 // This notification can come from any embedded contents or from this
1647 // WebContents' stream monitor. Aggregate these signals to get the actual
1648 // state.
1649 bool is_currently_audible =
1650 audio_stream_monitor_.IsCurrentlyAudible() ||
1651 (browser_plugin_embedder_ &&
1652 browser_plugin_embedder_->AreAnyGuestsCurrentlyAudible());
1653 if (is_currently_audible == is_currently_audible_)
1654 return;
1655
1656 // Update internal state.
1657 is_currently_audible_ = is_currently_audible;
1658 was_ever_audible_ = was_ever_audible_ || is_currently_audible_;
1659
1660 SendPageMessage(
1661 new PageMsg_AudioStateChanged(MSG_ROUTING_NONE, is_currently_audible_));
altimind8bd26c2016-11-04 11:44:541662
altiminec87fd1f2016-11-17 20:22:491663 // Notification for UI updates in response to the changed audio state.
altimind8bd26c2016-11-04 11:44:541664 NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB);
Jan Rucka442c83f2017-08-08 13:27:541665
Chris Hamiltondf0d72cd2018-05-29 16:23:531666 // Ensure that audio state changes propagate from innermost to outermost
1667 // WebContents.
1668 if (GetOuterWebContents())
1669 GetOuterWebContents()->OnAudioStateChanged();
Tommy Steimel18360512017-11-01 00:38:191670
Chris Hamiltoneb41198f2018-05-24 18:41:261671 for (auto& observer : observers_)
Chris Hamiltondf0d72cd2018-05-29 16:23:531672 observer.OnAudioStateChanged(is_currently_audible_);
altimind8bd26c2016-11-04 11:44:541673}
1674
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411675base::TimeTicks WebContentsImpl::GetLastActiveTime() {
[email protected]9a890452014-02-13 22:21:021676 return last_active_time_;
[email protected]3e324142012-06-25 18:26:331677}
1678
[email protected]9e2e4632012-07-27 16:38:411679void WebContentsImpl::WasShown() {
[email protected]96d185d2009-04-24 03:28:541680 controller_.SetActive(true);
[email protected]de3c5d82014-05-28 22:12:591681
danakjc1e71e4f2018-12-05 19:34:351682 // This shows the Page before showing the individual RenderWidgets, as
1683 // RenderWidgets will work to produce compositor frames and handle input
1684 // as soon as they are shown. But the Page and other classes do not expect to
1685 // be producing frames when the Page is hidden. So we make sure the Page is
1686 // shown first.
1687 SendPageMessage(new PageMsg_WasShown(MSG_ROUTING_NONE));
1688
EhsanK2075c7e2017-08-21 02:42:391689 if (auto* view = GetRenderWidgetHostView()) {
lfgf949e0e2017-05-10 22:18:111690 view->Show();
[email protected]789e9152009-08-04 21:59:431691#if defined(OS_MACOSX)
lfgf949e0e2017-05-10 22:18:111692 view->SetActive(true);
[email protected]789e9152009-08-04 21:59:431693#endif
1694 }
[email protected]96d185d2009-04-24 03:28:541695
EhsanK2075c7e2017-08-21 02:42:391696 if (!ShowingInterstitialPage())
1697 SetVisibilityForChildViews(true);
1698
gab0dccfef2015-05-20 18:43:391699 last_active_time_ = base::TimeTicks::Now();
Francois Doraye6161152018-03-27 22:05:371700 SetVisibility(Visibility::VISIBLE);
Lukasz Anforowicz873e6082018-05-17 00:12:331701
1702 for (FrameTreeNode* node : frame_tree_.Nodes()) {
1703 RenderFrameProxyHost* parent = node->render_manager()->GetProxyToParent();
1704 if (!parent)
1705 continue;
1706
Alex Moshchuk2b0c91d2018-10-10 18:41:161707 parent->cross_process_frame_connector()->DelegateWasShown();
Lukasz Anforowicz873e6082018-05-17 00:12:331708 }
[email protected]be1f56ab2011-12-22 06:55:311709}
1710
[email protected]b172aee2012-04-10 17:05:261711void WebContentsImpl::WasHidden() {
Jennifer Apacible72c31412018-06-19 16:52:241712 // If there are entities capturing screenshots or video (e.g. mirroring),
François Beaufort0e27cee02019-01-18 13:21:351713 // or in Picture-in-Picture mode, don't activate the "disable rendering"
1714 // optimization.
1715 if (!IsBeingCaptured() && !HasPictureInPictureVideo()) {
danakjc1e71e4f2018-12-05 19:34:351716 // This hides the individual RenderWidgets before hiding the Page, as
1717 // RenderWidgets will work to produce compositor frames and handle input
1718 // until they are hidden. But the Page and other classes do not expect to
1719 // be producing frames when the Page is hidden. So we stop the widgets,
1720 // then hide the Page.
1721 //
[email protected]151a63d2011-12-20 22:32:521722 // |GetRenderViewHost()| can be NULL if the user middle clicks a link to
[email protected]54597982013-02-06 01:59:551723 // open a tab in the background, then closes the tab before selecting it.
[email protected]b172aee2012-04-10 17:05:261724 // This is because closing the tab calls WebContentsImpl::Destroy(), which
[email protected]151a63d2011-12-20 22:32:521725 // removes the |GetRenderViewHost()|; then when we actually destroy the
[email protected]3e324142012-06-25 18:26:331726 // window, OnWindowPosChanged() notices and calls WasHidden() (which
[email protected]151a63d2011-12-20 22:32:521727 // calls us).
EhsanK2075c7e2017-08-21 02:42:391728 if (auto* view = GetRenderWidgetHostView())
lfgf949e0e2017-05-10 22:18:111729 view->Hide();
lfg717154072016-06-30 15:04:161730
EhsanK2075c7e2017-08-21 02:42:391731 if (!ShowingInterstitialPage())
Igor Eremeevef59187a2017-09-29 00:25:421732 SetVisibilityForChildViews(false);
[email protected]96d185d2009-04-24 03:28:541733 }
1734
François Beaufort0e27cee02019-01-18 13:21:351735 // Inform the renderer that the page was hidden if there are no entities
1736 // capturing screenshots or video (e.g. mirroring).
1737 if (!IsBeingCaptured())
1738 SendPageMessage(new PageMsg_WasHidden(MSG_ROUTING_NONE));
1739
Francois Doraye6161152018-03-27 22:05:371740 SetVisibility(Visibility::HIDDEN);
[email protected]375fa1b2012-05-22 22:05:371741}
1742
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411743bool WebContentsImpl::HasRecentInteractiveInputEvent() {
Daniel Cheng90196c82018-04-25 21:49:141744 static constexpr base::TimeDelta kMaxInterval =
1745 base::TimeDelta::FromSeconds(5);
1746 base::TimeDelta delta =
1747 ui::EventTimeForNow() - last_interactive_input_event_time_;
1748 // Note: the expectation is that the caller is typically expecting an input
1749 // event, e.g. validating that a WebUI message that requires a gesture is
Daniel Chenge81030b32019-07-17 20:20:231750 // actually attached to a gesture.
Daniel Cheng90196c82018-04-25 21:49:141751 return delta <= kMaxInterval;
1752}
1753
Avi Drissman738ea192018-08-29 20:24:161754void WebContentsImpl::SetIgnoreInputEvents(bool ignore_input_events) {
1755 ignore_input_events_ = ignore_input_events;
1756}
1757
Bo Liu168c8642017-08-28 18:26:021758#if defined(OS_ANDROID)
Bo Liu16dd7b8322018-05-02 21:19:501759void WebContentsImpl::SetMainFrameImportance(
1760 ChildProcessImportance importance) {
1761 GetMainFrame()->GetRenderWidgetHost()->SetImportance(importance);
Bo Liu7c6779e92017-08-16 02:02:281762 if (ShowingInterstitialPage()) {
Bo Liu16dd7b8322018-05-02 21:19:501763 static_cast<RenderFrameHostImpl*>(interstitial_page_->GetMainFrame())
1764 ->GetRenderWidgetHost()
1765 ->SetImportance(importance);
Bo Liu7c6779e92017-08-16 02:02:281766 }
Bo Liu7c6779e92017-08-16 02:02:281767}
Bo Liu168c8642017-08-28 18:26:021768#endif
Bo Liu7c6779e92017-08-16 02:02:281769
ccameron8807c38f2015-01-17 00:29:191770void WebContentsImpl::WasOccluded() {
François Beaufortfec686f2019-01-18 17:12:491771 // If there are entities capturing screenshots or video (e.g. mirroring),
1772 // or in Picture-in-Picture mode, don't activate the "disable rendering"
1773 // optimization.
1774 if (!IsBeingCaptured() && !HasPictureInPictureVideo()) {
Ehsan Karamada03e1582018-12-05 23:07:561775 if (auto* main_view = GetRenderWidgetHostView())
1776 main_view->WasOccluded();
1777 if (!ShowingInterstitialPage()) {
1778 // Occluding child view is treated as hiding the view
1779 // (https://crbug.com/903455).
1780 SetVisibilityForChildViews(false);
1781 }
Francois Doray18800972017-08-25 22:38:581782 }
François Beaufortfec686f2019-01-18 17:12:491783
1784 // Inform the renderer that the page was hidden if there are no entities
1785 // capturing screenshots or video (e.g. mirroring).
1786 if (!IsBeingCaptured())
1787 SendPageMessage(new PageMsg_WasHidden(MSG_ROUTING_NONE));
1788
Francois Doraye6161152018-03-27 22:05:371789 SetVisibility(Visibility::OCCLUDED);
ccameron8807c38f2015-01-17 00:29:191790}
1791
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411792Visibility WebContentsImpl::GetVisibility() {
Francois Doraye6161152018-03-27 22:05:371793 return visibility_;
Francois Dorayfe4a1772018-02-17 04:17:091794}
1795
Alex Moshchuk6fcaca752018-07-14 02:13:591796// TODO(alexmos): rename to NeedToFireBeforeUnloadOrUnload().
[email protected]b172aee2012-04-10 17:05:261797bool WebContentsImpl::NeedToFireBeforeUnload() {
[email protected]be1f56ab2011-12-22 06:55:311798 // TODO(creis): Should we fire even for interstitial pages?
Alex Moshchuk6fcaca752018-07-14 02:13:591799 if (ShowingInterstitialPage())
1800 return false;
1801
1802 if (!WillNotifyDisconnection())
1803 return false;
1804
1805 // Don't fire if the main frame's RenderViewHost indicates that beforeunload
1806 // and unload have already executed (e.g., after receiving a ClosePage ACK)
1807 // or should be ignored.
1808 if (GetRenderViewHost()->SuddenTerminationAllowed())
1809 return false;
1810
Aditya Keerthi34f37e62019-03-08 15:54:531811 // Check whether the main frame needs to run beforeunload or unload handlers.
1812 if (GetMainFrame()->GetSuddenTerminationDisablerState(
1813 blink::kBeforeUnloadHandler | blink::kUnloadHandler)) {
Alex Moshchuk6fcaca752018-07-14 02:13:591814 return true;
Aditya Keerthi34f37e62019-03-08 15:54:531815 }
Alex Moshchuk6fcaca752018-07-14 02:13:591816
1817 // Check whether any subframes need to run beforeunload handlers.
1818 //
1819 // TODO(alexmos): Also check whether subframes need to run unload handlers in
1820 // addition to beforeunload.
1821 return GetMainFrame()->ShouldDispatchBeforeUnload(
1822 true /* check_subframes_only */);
[email protected]be1f56ab2011-12-22 06:55:311823}
1824
Chris Hamilton3b3e315d2018-09-19 13:16:211825void WebContentsImpl::DispatchBeforeUnload(bool auto_cancel) {
1826 auto before_unload_type =
1827 auto_cancel ? RenderFrameHostImpl::BeforeUnloadType::DISCARD
1828 : RenderFrameHostImpl::BeforeUnloadType::TAB_CLOSE;
1829 GetMainFrame()->DispatchBeforeUnload(before_unload_type, false);
[email protected]1c3f80bd2014-04-08 23:02:061830}
1831
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:501832void WebContentsImpl::AttachInnerWebContents(
1833 std::unique_ptr<WebContents> inner_web_contents,
1834 RenderFrameHost* render_frame_host) {
1835 WebContentsImpl* inner_web_contents_impl =
1836 static_cast<WebContentsImpl*>(inner_web_contents.get());
1837 DCHECK(!inner_web_contents_impl->node_.outer_web_contents());
1838 auto* render_frame_host_impl =
1839 static_cast<RenderFrameHostImpl*>(render_frame_host);
1840 DCHECK_EQ(&frame_tree_,
1841 render_frame_host_impl->frame_tree_node()->frame_tree());
Lucas Furukawa Gadani105de1e812018-05-31 19:38:391842
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:501843 RenderFrameHostManager* inner_render_manager =
1844 inner_web_contents_impl->GetRenderManager();
1845 RenderFrameHostImpl* inner_main_frame =
1846 inner_render_manager->current_frame_host();
1847 RenderViewHostImpl* inner_render_view_host =
1848 inner_render_manager->current_host();
1849 auto* outer_render_manager =
1850 render_frame_host_impl->frame_tree_node()->render_manager();
lfg91a79e12016-04-01 23:52:191851
Lucas Furukawa Gadani105de1e812018-05-31 19:38:391852 // When attaching a WebContents as an inner WebContents, we need to replace
1853 // the Webcontents' view with a WebContentsViewChildFrame.
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:501854 inner_web_contents_impl->view_.reset(new WebContentsViewChildFrame(
1855 inner_web_contents_impl,
1856 GetContentClient()->browser()->GetWebContentsViewDelegate(
1857 inner_web_contents_impl),
1858 &inner_web_contents_impl->render_view_host_delegate_view_));
Lucas Furukawa Gadani105de1e812018-05-31 19:38:391859
lfg91a79e12016-04-01 23:52:191860 // When the WebContents being initialized has an opener, the browser side
1861 // Render{View,Frame}Host must be initialized and the RenderWidgetHostView
1862 // created. This is needed because the usual initialization happens during
1863 // the first navigation, but when attaching a new window we don't navigate
1864 // before attaching. If the browser side is already initialized, the calls
1865 // below will just early return.
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:501866 inner_render_manager->InitRenderView(inner_render_view_host, nullptr);
1867 inner_main_frame->Init();
1868 if (!inner_render_manager->GetRenderWidgetHostView()) {
1869 inner_web_contents_impl->CreateRenderWidgetHostViewForRenderManager(
1870 inner_render_view_host);
1871 }
lfg91a79e12016-04-01 23:52:191872
lazyboy6ec48b2a2015-06-29 15:18:141873 // Create a link to our outer WebContents.
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:501874 node_.AttachInnerWebContents(std::move(inner_web_contents),
1875 render_frame_host_impl);
lazyboy6ec48b2a2015-06-29 15:18:141876
lazyboy6ec48b2a2015-06-29 15:18:141877 // Create a proxy in top-level RenderFrameHostManager, pointing to the
1878 // SiteInstance of the outer WebContents. The proxy will be used to send
1879 // postMessage to the inner WebContents.
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:501880 auto* proxy = inner_render_manager->CreateOuterDelegateProxy(
1881 render_frame_host_impl->GetSiteInstance());
lazyboy6ec48b2a2015-06-29 15:18:141882
Lucas Furukawa Gadani99125822019-01-03 15:41:491883 // When attaching a GuestView as an inner WebContents, there should already be
1884 // a live RenderFrame, which has to be swapped. When attaching a portal, there
1885 // will not be a live RenderFrame before creating the proxy.
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:501886 if (render_frame_host_impl->IsRenderFrameLive()) {
1887 inner_render_manager->SwapOuterDelegateFrame(render_frame_host_impl, proxy);
Lucas Furukawa Gadani99125822019-01-03 15:41:491888
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:501889 inner_web_contents_impl->ReattachToOuterWebContentsFrame();
Lucas Furukawa Gadani99125822019-01-03 15:41:491890 }
ekaramadadd882292016-06-08 15:22:561891
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:501892 if (frame_tree_.GetFocusedFrame() ==
1893 render_frame_host_impl->frame_tree_node()) {
1894 inner_web_contents_impl->SetFocusedFrame(
1895 inner_web_contents_impl->frame_tree_.root(),
1896 render_frame_host_impl->GetSiteInstance());
avallee7529b2a2017-04-26 11:37:511897 }
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:501898 outer_render_manager->set_attach_complete();
lazyboy6ec48b2a2015-06-29 15:18:141899}
1900
Lucas Furukawa Gadani99125822019-01-03 15:41:491901std::unique_ptr<WebContents> WebContentsImpl::DetachFromOuterWebContents() {
1902 DCHECK(node_.outer_web_contents());
1903 if (RenderWidgetHostViewBase* view =
1904 static_cast<RenderWidgetHostViewBase*>(GetMainFrame()->GetView())) {
1905 view->Destroy();
1906 }
1907 GetRenderManager()->DeleteOuterDelegateProxy(
1908 node_.OuterContentsFrameTreeNode()
1909 ->current_frame_host()
1910 ->GetSiteInstance());
1911 view_.reset(CreateWebContentsView(
1912 this, GetContentClient()->browser()->GetWebContentsViewDelegate(this),
1913 &render_view_host_delegate_view_));
1914 view_->CreateView(GetPreferredSize(), nullptr);
1915 std::unique_ptr<WebContents> web_contents =
1916 node_.DisconnectFromOuterWebContents();
1917 DCHECK_EQ(web_contents.get(), this);
1918 node_.SetFocusedWebContents(this);
1919 CreateRenderWidgetHostViewForRenderManager(GetRenderViewHost());
1920 return web_contents;
1921}
1922
W. James MacLean2dc75ed42019-03-06 14:31:091923void WebContentsImpl::RecursivelyRegisterFrameSinkIds() {
1924 auto* view = static_cast<RenderWidgetHostViewBase*>(
1925 GetRenderManager()->GetRenderWidgetHostView());
1926 DCHECK(view);
1927 if (!view->IsRenderWidgetHostViewChildFrame())
1928 return;
1929 static_cast<RenderWidgetHostViewChildFrame*>(view)->RegisterFrameSinkId();
1930
1931 for (auto* inner_web_contents : node_.GetInnerWebContents()) {
1932 static_cast<WebContentsImpl*>(inner_web_contents)
1933 ->RecursivelyRegisterFrameSinkIds();
1934 }
1935}
1936
Lucas Furukawa Gadaniaed1fed2017-10-13 17:34:141937void WebContentsImpl::ReattachToOuterWebContentsFrame() {
1938 DCHECK(node_.outer_web_contents());
1939 auto* render_manager = GetRenderManager();
1940 auto* parent_frame =
1941 node_.OuterContentsFrameTreeNode()->current_frame_host()->GetParent();
1942 render_manager->SetRWHViewForInnerContents(
1943 render_manager->GetRenderWidgetHostView());
1944
W. James MacLean2dc75ed42019-03-06 14:31:091945 RecursivelyRegisterFrameSinkIds();
Lucas Furukawa Gadaniaed1fed2017-10-13 17:34:141946
1947 // Set up the the guest's AX tree to point back at the embedder's AX tree.
1948 GetMainFrame()->set_browser_plugin_embedder_ax_tree_id(
1949 parent_frame->GetAXTreeID());
1950 GetMainFrame()->UpdateAXTreeData();
1951}
1952
dalecurtis6c58ed02016-10-28 23:02:371953void WebContentsImpl::DidChangeVisibleSecurityState() {
Emily Starkc663b0242019-04-26 19:34:451954 if (delegate_)
dalecurtis6c58ed02016-10-28 23:02:371955 delegate_->VisibleSecurityStateChanged(this);
Emily Starkc663b0242019-04-26 19:34:451956 for (auto& observer : observers_)
1957 observer.DidChangeVisibleSecurityState();
dalecurtis6c58ed02016-10-28 23:02:371958}
1959
Ahmed Fakhryd70bf37b2018-04-04 17:07:241960void WebContentsImpl::NotifyPreferencesChanged() {
1961 std::set<RenderViewHost*> render_view_host_set;
1962 for (FrameTreeNode* node : frame_tree_.Nodes()) {
Giovanni Ortuño Urquidi17a5d7c2018-04-30 06:48:531963 render_view_host_set.insert(
1964 node->current_frame_host()->GetRenderViewHost());
Ahmed Fakhryd70bf37b2018-04-04 17:07:241965 }
Giovanni Ortuño Urquidi17a5d7c2018-04-30 06:48:531966
Ahmed Fakhryd70bf37b2018-04-04 17:07:241967 for (RenderViewHost* render_view_host : render_view_host_set)
1968 render_view_host->OnWebkitPreferencesChanged();
1969}
1970
Josh Karlindaba39322019-07-17 23:24:331971void WebContentsImpl::OnCookiesRead(const GURL& url,
1972 const GURL& first_party_url,
1973 const net::CookieList& cookie_list,
1974 bool blocked_by_policy) {
1975 for (auto& observer : observers_) {
1976 observer.OnCookiesRead(url, first_party_url, cookie_list,
1977 blocked_by_policy);
1978 }
1979}
1980
1981void WebContentsImpl::OnCookieChange(const GURL& url,
1982 const GURL& first_party_url,
1983 const net::CanonicalCookie& cookie,
1984 bool blocked_by_policy) {
1985 for (auto& observer : observers_) {
1986 observer.OnCookieChange(url, first_party_url, cookie, blocked_by_policy);
1987 }
1988}
1989
[email protected]b172aee2012-04-10 17:05:261990void WebContentsImpl::Stop() {
dcheng57e39e22016-01-21 00:25:381991 for (FrameTreeNode* node : frame_tree_.Nodes())
1992 node->StopLoading();
ericwilligersde386342016-10-19 02:35:091993 for (auto& observer : observers_)
1994 observer.NavigationStopped();
[email protected]0bfbf882011-12-22 18:19:271995}
1996
Francois Doray47f759d2018-06-11 18:13:511997void WebContentsImpl::SetPageFrozen(bool frozen) {
1998 // A visible page is never frozen.
1999 DCHECK_NE(Visibility::VISIBLE, GetVisibility());
2000
2001 SendPageMessage(new PageMsg_SetPageFrozen(MSG_ROUTING_NONE, frozen));
Fadi Meawada6573e02018-03-10 00:52:112002}
2003
erikchen6c7df7f7a2018-05-03 18:13:592004std::unique_ptr<WebContents> WebContentsImpl::Clone() {
[email protected]14392a52012-05-02 20:28:442005 // We use our current SiteInstance since the cloned entry will use it anyway.
alexmose201c7cd2015-06-10 17:14:212006 // We pass our own opener so that the cloned page can access it if it was set
[email protected]ed245db2012-12-18 08:00:452007 // before.
[email protected]54944cde2012-12-09 09:24:592008 CreateParams create_params(GetBrowserContext(), GetSiteInstance());
[email protected]fc2b46b2014-05-03 16:33:452009 create_params.initial_size = GetContainerBounds().size();
Ian Clelland5cbaaf82017-11-27 22:00:032010 FrameTreeNode* opener = frame_tree_.root()->opener();
2011 RenderFrameHostImpl* opener_rfh = nullptr;
2012 if (opener)
2013 opener_rfh = opener->current_frame_host();
erikchen6c7df7f7a2018-05-03 18:13:592014 std::unique_ptr<WebContentsImpl> tc =
erikchen50735fd2018-05-05 15:08:332015 CreateWithOpener(create_params, opener_rfh);
Lucas Furukawa Gadani5553a152019-01-08 18:55:572016 tc->GetController().CopyStateFrom(&controller_, true);
ericwilligersde386342016-10-19 02:35:092017 for (auto& observer : observers_)
erikchen6c7df7f7a2018-05-03 18:13:592018 observer.DidCloneToNewWebContents(this, tc.get());
[email protected]0bfbf882011-12-22 18:19:272019 return tc;
2020}
2021
[email protected]14392a52012-05-02 20:28:442022void WebContentsImpl::Observe(int type,
[email protected]8ff00d72012-10-23 19:12:212023 const NotificationSource& source,
2024 const NotificationDetails& details) {
[email protected]14392a52012-05-02 20:28:442025 switch (type) {
[email protected]8ff00d72012-10-23 19:12:212026 case NOTIFICATION_RENDER_WIDGET_HOST_DESTROYED: {
2027 RenderWidgetHost* host = Source<RenderWidgetHost>(source).ptr();
[email protected]0b431992014-06-24 00:08:032028 RenderWidgetHostView* view = host->GetView();
2029 if (view == GetFullscreenRenderWidgetHostView()) {
2030 // We cannot just call view_->RestoreFocus() here. On some platforms,
2031 // attempting to focus the currently-invisible WebContentsView will be
2032 // flat-out ignored. Therefore, this boolean is used to track whether
2033 // we will request focus after the fullscreen widget has been
2034 // destroyed.
2035 fullscreen_widget_had_focus_at_shutdown_ = (view && view->HasFocus());
2036 } else {
alexmosc2a8cec2016-05-23 22:19:532037 for (auto i = pending_widget_views_.begin();
[email protected]0b431992014-06-24 00:08:032038 i != pending_widget_views_.end(); ++i) {
2039 if (host->GetView() == i->second) {
2040 pending_widget_views_.erase(i);
2041 break;
2042 }
[email protected]bafe6cd2012-05-23 23:09:502043 }
2044 }
2045 break;
2046 }
[email protected]14392a52012-05-02 20:28:442047 default:
2048 NOTREACHED();
2049 }
2050}
2051
[email protected]ec6c05f2013-10-23 18:41:572052WebContents* WebContentsImpl::GetWebContents() {
2053 return this;
2054}
2055
[email protected]54944cde2012-12-09 09:24:592056void WebContentsImpl::Init(const WebContents::CreateParams& params) {
[email protected]fa944cb82013-11-15 17:51:212057 // This is set before initializing the render manager since
[email protected]b0936d22013-11-28 06:47:362058 // RenderFrameHostManager::Init calls back into us via its delegate to ask if
[email protected]fa944cb82013-11-15 17:51:212059 // it should be hidden.
Francois Doraye6161152018-03-27 22:05:372060 visibility_ =
2061 params.initially_hidden ? Visibility::HIDDEN : Visibility::VISIBLE;
[email protected]d6fa88f2013-10-18 16:00:432062
Sebastien Marchand28cf23182018-06-20 02:39:352063 if (!params.last_active_time.is_null())
2064 last_active_time_ = params.last_active_time;
2065
dcheng3ce04b62015-10-26 23:30:552066 // The routing ids must either all be set or all be unset.
avic2c29d782014-12-12 00:23:542067 DCHECK((params.routing_id == MSG_ROUTING_NONE &&
dcheng3ce04b62015-10-26 23:30:552068 params.main_frame_routing_id == MSG_ROUTING_NONE &&
2069 params.main_frame_widget_routing_id == MSG_ROUTING_NONE) ||
avic2c29d782014-12-12 00:23:542070 (params.routing_id != MSG_ROUTING_NONE &&
dcheng3ce04b62015-10-26 23:30:552071 params.main_frame_routing_id != MSG_ROUTING_NONE &&
2072 params.main_frame_widget_routing_id != MSG_ROUTING_NONE));
2073
2074 scoped_refptr<SiteInstance> site_instance = params.site_instance;
2075 if (!site_instance)
2076 site_instance = SiteInstance::Create(params.browser_context);
Lukasz Anforowicz3caa8372018-06-05 17:22:072077 if (params.desired_renderer_state == CreateParams::kNoRendererProcess) {
2078 static_cast<SiteInstanceImpl*>(site_instance.get())
2079 ->PreventAssociationWithSpareProcess();
2080 }
dcheng3ce04b62015-10-26 23:30:552081
2082 // A main RenderFrameHost always has a RenderWidgetHost, since it is always a
2083 // local root by definition.
2084 // TODO(avi): Once RenderViewHostImpl has-a RenderWidgetHostImpl, it will no
2085 // longer be necessary to eagerly grab a routing ID for the view.
2086 // https://crbug.com/545684
2087 int32_t view_routing_id = params.routing_id;
2088 int32_t main_frame_widget_routing_id = params.main_frame_widget_routing_id;
2089 if (main_frame_widget_routing_id == MSG_ROUTING_NONE) {
Albert J. Wongcb004632018-07-10 22:58:252090 view_routing_id = site_instance->GetProcess()->GetNextRoutingID();
2091 main_frame_widget_routing_id =
dcheng3ce04b62015-10-26 23:30:552092 site_instance->GetProcess()->GetNextRoutingID();
2093 }
2094
Albert J. Wongcb004632018-07-10 22:58:252095 DCHECK_NE(view_routing_id, main_frame_widget_routing_id);
2096
Dave Tapuska485aca92017-08-08 00:47:582097 GetRenderManager()->Init(
2098 site_instance.get(), view_routing_id, params.main_frame_routing_id,
2099 main_frame_widget_routing_id, params.renderer_initiated_creation);
lukasza464d8692016-02-22 19:26:322100
lukasza5140a412016-09-15 21:12:302101 // blink::FrameTree::setName always keeps |unique_name| empty in case of a
2102 // main frame - let's do the same thing here.
2103 std::string unique_name;
lukasza464d8692016-02-22 19:26:322104 frame_tree_.root()->SetFrameName(params.main_frame_name, unique_name);
[email protected]d1198fd2012-08-13 22:50:192105
[email protected]fc2b46b2014-05-03 16:33:452106 WebContentsViewDelegate* delegate =
2107 GetContentClient()->browser()->GetWebContentsViewDelegate(this);
2108
sky52a39e342016-12-17 17:23:222109 if (GuestMode::IsCrossProcessFrameGuest(this)) {
2110 view_.reset(new WebContentsViewChildFrame(
2111 this, delegate, &render_view_host_delegate_view_));
2112 } else {
2113 view_.reset(CreateWebContentsView(this, delegate,
2114 &render_view_host_delegate_view_));
Scott Violetc884c7fd2017-11-03 23:51:012115 if (browser_plugin_guest_) {
2116 view_ = std::make_unique<WebContentsViewGuest>(
2117 this, browser_plugin_guest_.get(), std::move(view_),
2118 &render_view_host_delegate_view_);
2119 }
[email protected]d1198fd2012-08-13 22:50:192120 }
[email protected]fc2b46b2014-05-03 16:33:452121 CHECK(render_view_host_delegate_view_);
[email protected]d1198fd2012-08-13 22:50:192122 CHECK(view_.get());
2123
[email protected]ed245db2012-12-18 08:00:452124 gfx::Size initial_size = params.initial_size;
[email protected]54944cde2012-12-09 09:24:592125 view_->CreateView(initial_size, params.context);
[email protected]d1198fd2012-08-13 22:50:192126
brettw4b461082016-11-19 18:55:162127#if BUILDFLAG(ENABLE_PLUGINS)
tommyclieb25b2a2014-11-03 19:45:092128 plugin_content_origin_whitelist_.reset(
2129 new PluginContentOriginWhitelist(this));
2130#endif
2131
[email protected]d1198fd2012-08-13 22:50:192132 registrar_.Add(this,
[email protected]8ff00d72012-10-23 19:12:212133 NOTIFICATION_RENDER_WIDGET_HOST_DESTROYED,
2134 NotificationService::AllBrowserContextsAndSources());
[email protected]f28ef9a32014-05-12 16:36:102135
leon.hane4db177a2017-02-07 14:19:162136 screen_orientation_provider_.reset(new ScreenOrientationProvider(this));
[email protected]01f83f92014-06-17 14:41:542137
mlamouriefdca9d2014-09-16 16:55:402138 manifest_manager_host_.reset(new ManifestManagerHost(this));
2139
mfomitchev2b8b066a2016-01-28 19:23:152140#if defined(OS_ANDROID)
Gyuyoung Kim1f672aa2019-04-25 02:10:252141 date_time_chooser_.reset(new DateTimeChooserAndroid(this));
[email protected]583418cc2013-01-17 14:01:102142#endif
fsamuel7310a4262014-12-05 05:06:442143
2144 // BrowserPluginGuest::Init needs to be called after this WebContents has
2145 // a RenderWidgetHostViewGuest. That is, |view_->CreateView| above.
2146 if (browser_plugin_guest_)
2147 browser_plugin_guest_->Init();
nickf9acfbe2014-12-23 19:12:372148
2149 for (size_t i = 0; i < g_created_callbacks.Get().size(); i++)
2150 g_created_callbacks.Get().at(i).Run(this);
naskob21fe4872015-02-24 14:15:562151
2152 // If the WebContents creation was renderer-initiated, it means that the
2153 // corresponding RenderView and main RenderFrame have already been created.
2154 // Ensure observers are notified about this.
2155 if (params.renderer_initiated_creation) {
avi3627ecac2015-10-16 17:40:432156 GetRenderViewHost()->GetWidget()->set_renderer_initialized(true);
Alex Moshchuk27caae82017-09-11 23:11:182157 GetRenderViewHost()->DispatchRenderViewCreated();
naskob21fe4872015-02-24 14:15:562158 GetRenderManager()->current_frame_host()->SetRenderFrameCreated(true);
2159 }
naskof5940b9f2015-03-02 23:04:052160
lof84501da082016-05-23 21:22:542161 // Create the renderer process in advance if requested.
Lukasz Anforowicz3caa8372018-06-05 17:22:072162 if (params.desired_renderer_state ==
2163 CreateParams::kInitializeAndWarmupRendererProcess) {
lof84501da082016-05-23 21:22:542164 if (!GetRenderManager()->current_frame_host()->IsRenderFrameLive()) {
2165 GetRenderManager()->InitRenderView(GetRenderViewHost(), nullptr);
2166 }
2167 }
2168
naskof5940b9f2015-03-02 23:04:052169 // Ensure that observers are notified of the creation of this WebContents's
2170 // main RenderFrameHost. It must be done here for main frames, since the
2171 // NotifySwappedFromRenderManager expects view_ to already be created and that
2172 // happens after RenderFrameHostManager::Init.
2173 NotifySwappedFromRenderManager(
2174 nullptr, GetRenderManager()->current_frame_host(), true);
[email protected]d1198fd2012-08-13 22:50:192175}
2176
[email protected]7fff43e2013-05-21 20:21:102177void WebContentsImpl::OnWebContentsDestroyed(WebContentsImpl* web_contents) {
2178 RemoveDestructionObserver(web_contents);
2179
[email protected]ceee8cd2013-03-08 04:59:512180 // Clear a pending contents that has been closed before being shown.
alexmosc2a8cec2016-05-23 22:19:532181 for (auto iter = pending_contents_.begin(); iter != pending_contents_.end();
[email protected]ceee8cd2013-03-08 04:59:512182 ++iter) {
erikchenbee5c9622018-04-27 19:30:252183 if (iter->second.get() != web_contents)
[email protected]ceee8cd2013-03-08 04:59:512184 continue;
erikchenbee5c9622018-04-27 19:30:252185
2186 // Someone else has deleted the WebContents. That should never happen!
2187 // TODO(erikchen): Fix semantics here. https://crbug.com/832879.
2188 iter->second.release();
[email protected]ceee8cd2013-03-08 04:59:512189 pending_contents_.erase(iter);
[email protected]ceee8cd2013-03-08 04:59:512190 return;
2191 }
2192 NOTREACHED();
[email protected]14392a52012-05-02 20:28:442193}
2194
[email protected]7fff43e2013-05-21 20:21:102195void WebContentsImpl::AddDestructionObserver(WebContentsImpl* web_contents) {
Jan Wilken Dörrie73c901e2019-06-12 09:02:322196 if (!base::Contains(destruction_observers_, web_contents)) {
[email protected]7fff43e2013-05-21 20:21:102197 destruction_observers_[web_contents] =
Jeremy Roman04f27c372017-10-27 15:20:552198 std::make_unique<DestructionObserver>(this, web_contents);
[email protected]7fff43e2013-05-21 20:21:102199 }
2200}
2201
2202void WebContentsImpl::RemoveDestructionObserver(WebContentsImpl* web_contents) {
avia8cf81982016-08-12 20:28:462203 destruction_observers_.erase(web_contents);
[email protected]7fff43e2013-05-21 20:21:102204}
2205
[email protected]b172aee2012-04-10 17:05:262206void WebContentsImpl::AddObserver(WebContentsObserver* observer) {
[email protected]be1f56ab2011-12-22 06:55:312207 observers_.AddObserver(observer);
2208}
2209
[email protected]b172aee2012-04-10 17:05:262210void WebContentsImpl::RemoveObserver(WebContentsObserver* observer) {
[email protected]be1f56ab2011-12-22 06:55:312211 observers_.RemoveObserver(observer);
2212}
2213
[email protected]948481d2014-06-11 18:32:222214std::set<RenderWidgetHostView*>
2215WebContentsImpl::GetRenderWidgetHostViewsInTree() {
2216 std::set<RenderWidgetHostView*> set;
2217 if (ShowingInterstitialPage()) {
lfgf949e0e2017-05-10 22:18:112218 if (RenderWidgetHostView* rwhv = GetRenderWidgetHostView())
2219 set.insert(rwhv);
[email protected]948481d2014-06-11 18:32:222220 } else {
dchengafb53e22016-02-04 08:11:082221 for (RenderFrameHost* rfh : GetAllFrames()) {
lfgf949e0e2017-05-10 22:18:112222 if (RenderWidgetHostView* rwhv = static_cast<RenderFrameHostImpl*>(rfh)
2223 ->frame_tree_node()
2224 ->render_manager()
2225 ->GetRenderWidgetHostView()) {
2226 set.insert(rwhv);
2227 }
dchengafb53e22016-02-04 08:11:082228 }
[email protected]948481d2014-06-11 18:32:222229 }
[email protected]de3c5d82014-05-28 22:12:592230 return set;
2231}
2232
[email protected]b172aee2012-04-10 17:05:262233void WebContentsImpl::Activate() {
[email protected]d5f942ba2008-09-26 19:30:342234 if (delegate_)
2235 delegate_->ActivateContents(this);
2236}
2237
avi3e4b8512015-11-11 01:55:492238void WebContentsImpl::LostCapture(RenderWidgetHostImpl* render_widget_host) {
2239 if (!RenderViewHostImpl::From(render_widget_host))
2240 return;
2241
[email protected]63954792011-07-11 04:17:482242 if (delegate_)
2243 delegate_->LostCapture();
2244}
2245
Steven Holte38041342018-06-22 22:00:572246ukm::SourceId WebContentsImpl::GetUkmSourceIdForLastCommittedSource() const {
2247 return last_committed_source_id_;
2248}
2249
Ahmed Fakhryfaa32d22018-10-05 15:56:162250void WebContentsImpl::SetTopControlsShownRatio(
2251 RenderWidgetHostImpl* render_widget_host,
2252 float ratio) {
2253 if (!delegate_)
2254 return;
2255
2256 RenderFrameHostImpl* rfh = GetMainFrame();
2257 if (!rfh || render_widget_host != rfh->GetRenderWidgetHost())
2258 return;
2259
2260 delegate_->SetTopControlsShownRatio(this, ratio);
Ahmed Fakhry58e6ef542018-09-04 18:05:382261}
2262
Ahmed Fakhry58e6ef542018-09-04 18:05:382263void WebContentsImpl::SetTopControlsGestureScrollInProgress(bool in_progress) {
2264 if (delegate_)
2265 delegate_->SetTopControlsGestureScrollInProgress(in_progress);
2266}
2267
lazyboy63f5b312015-11-23 20:19:522268void WebContentsImpl::RenderWidgetCreated(
2269 RenderWidgetHostImpl* render_widget_host) {
2270 created_widgets_.insert(render_widget_host);
2271}
2272
[email protected]b24b68a2012-09-24 21:57:262273void WebContentsImpl::RenderWidgetDeleted(
2274 RenderWidgetHostImpl* render_widget_host) {
lazyboy63f5b312015-11-23 20:19:522275 // Note that |is_being_destroyed_| can be true at this point as
2276 // ~WebContentsImpl() calls RFHM::ClearRFHsPendingShutdown(), which might lead
2277 // us here.
2278 created_widgets_.erase(render_widget_host);
[email protected]b24b68a2012-09-24 21:57:262279
lazyboy63f5b312015-11-23 20:19:522280 if (is_being_destroyed_)
2281 return;
[email protected]44470a22013-01-24 01:21:542282
2283 if (render_widget_host &&
alexmosc9e76ec2016-05-16 22:59:372284 render_widget_host->GetRoutingID() == fullscreen_widget_routing_id_ &&
2285 render_widget_host->GetProcess()->GetID() ==
2286 fullscreen_widget_process_id_) {
[email protected]4aebbca2013-09-17 22:26:492287 if (delegate_ && delegate_->EmbedsFullscreenWidget())
mlamouri7a78d6fd2015-01-17 13:23:532288 delegate_->ExitFullscreenModeForTab(this);
ericwilligersde386342016-10-19 02:35:092289 for (auto& observer : observers_)
2290 observer.DidDestroyFullscreenWidget();
alexmosc9e76ec2016-05-16 22:59:372291 fullscreen_widget_process_id_ = ChildProcessHost::kInvalidUniqueID;
[email protected]44470a22013-01-24 01:21:542292 fullscreen_widget_routing_id_ = MSG_ROUTING_NONE;
[email protected]0b431992014-06-24 00:08:032293 if (fullscreen_widget_had_focus_at_shutdown_)
2294 view_->RestoreFocus();
[email protected]44470a22013-01-24 01:21:542295 }
lfgbee1e0a2016-06-08 21:24:212296
lfg7d4caad2017-03-22 09:01:452297 if (render_widget_host == mouse_lock_widget_)
lfgff8dfc8d2016-10-21 17:42:322298 LostMouseLock(mouse_lock_widget_);
Joe Downing192998b22018-03-22 15:51:362299
Joe Downingc1a57efe2018-05-09 18:23:542300 CancelKeyboardLock(render_widget_host);
[email protected]b24b68a2012-09-24 21:57:262301}
2302
miu9e14e492014-10-25 02:39:042303void WebContentsImpl::RenderWidgetGotFocus(
2304 RenderWidgetHostImpl* render_widget_host) {
calamity7fe55ce2015-04-10 03:59:372305 // Notify the observers if an embedded fullscreen widget was focused.
2306 if (delegate_ && render_widget_host && delegate_->EmbedsFullscreenWidget() &&
2307 render_widget_host->GetView() == GetFullscreenRenderWidgetHostView()) {
Michael Thiessen896405db2017-07-20 17:52:322308 NotifyWebContentsFocused(render_widget_host);
calamity7fe55ce2015-04-10 03:59:372309 }
miu9e14e492014-10-25 02:39:042310}
2311
zijiehe3bee08e22017-05-17 04:14:462312void WebContentsImpl::RenderWidgetLostFocus(
2313 RenderWidgetHostImpl* render_widget_host) {
Michael Thiessen896405db2017-07-20 17:52:322314 // Notify the observers if an embedded fullscreen widget lost focus.
2315 if (delegate_ && render_widget_host && delegate_->EmbedsFullscreenWidget() &&
2316 render_widget_host->GetView() == GetFullscreenRenderWidgetHostView()) {
2317 NotifyWebContentsLostFocus(render_widget_host);
2318 }
zijiehe3bee08e22017-05-17 04:14:462319}
2320
estaded0a0c992015-01-21 14:12:062321void WebContentsImpl::RenderWidgetWasResized(
rouslan2f5993f2015-01-29 00:18:312322 RenderWidgetHostImpl* render_widget_host,
Christopher Cameron07f666b2018-02-13 23:41:192323 const ScreenInfo& screen_info,
rouslan2f5993f2015-01-29 00:18:312324 bool width_changed) {
creisc014b402015-04-23 16:41:452325 RenderFrameHostImpl* rfh = GetMainFrame();
estadece344ec2015-01-23 00:12:292326 if (!rfh || render_widget_host != rfh->GetRenderWidgetHost())
estaded0a0c992015-01-21 14:12:062327 return;
estaded0a0c992015-01-21 14:12:062328
lfg8d649cc2017-04-28 18:04:302329 SendPageMessage(new PageMsg_UpdateScreenInfo(MSG_ROUTING_NONE, screen_info));
2330
ericwilligersde386342016-10-19 02:35:092331 for (auto& observer : observers_)
2332 observer.MainFrameWasResized(width_changed);
estaded0a0c992015-01-21 14:12:062333}
2334
skyf65d9bb2017-03-24 02:26:392335KeyboardEventProcessingResult WebContentsImpl::PreHandleKeyboardEvent(
2336 const NativeWebKeyboardEvent& event) {
2337 return delegate_ ? delegate_->PreHandleKeyboardEvent(this, event)
2338 : KeyboardEventProcessingResult::NOT_HANDLED;
[email protected]63954792011-07-11 04:17:482339}
2340
Scott Violetd5caa872018-10-16 22:00:512341bool WebContentsImpl::HandleKeyboardEvent(const NativeWebKeyboardEvent& event) {
[email protected]0b094002014-08-20 18:28:102342 if (browser_plugin_embedder_ &&
2343 browser_plugin_embedder_->HandleKeyboardEvent(event)) {
Scott Violetd5caa872018-10-16 22:00:512344 return true;
[email protected]0b094002014-08-20 18:28:102345 }
Scott Violetd5caa872018-10-16 22:00:512346 return delegate_ && delegate_->HandleKeyboardEvent(this, event);
[email protected]63954792011-07-11 04:17:482347}
2348
[email protected]d92026ef2014-03-03 21:04:132349bool WebContentsImpl::HandleWheelEvent(
[email protected]180ef242013-11-07 06:50:462350 const blink::WebMouseWheelEvent& event) {
[email protected]bccc4472013-04-18 16:37:192351#if !defined(OS_MACOSX)
lanweia93644f2015-01-21 22:00:332352 // On platforms other than Mac, control+mousewheel may change zoom. On Mac,
2353 // this isn't done for two reasons:
[email protected]bccc4472013-04-18 16:37:192354 // -the OS already has a gesture to do this through pinch-zoom
2355 // -if a user starts an inertial scroll, let's go, and presses control
2356 // (i.e. control+tab) then the OS's buffered scroll events will come in
2357 // with control key set which isn't what the user wants
Blink Reformat1c4d759e2017-04-09 16:34:542358 if (delegate_ && event.wheel_ticks_y &&
Ehsan Karamad0f83cb112018-09-06 22:07:362359 event.event_action == blink::WebMouseWheelEvent::EventAction::kPageZoom) {
w.shackleton49bcd392016-01-06 17:38:222360 // Count only integer cumulative scrolls as zoom events; this handles
2361 // smooth scroll and regular scroll device behavior.
Blink Reformat1c4d759e2017-04-09 16:34:542362 zoom_scroll_remainder_ += event.wheel_ticks_y;
w.shackleton49bcd392016-01-06 17:38:222363 int whole_zoom_scroll_remainder_ = std::lround(zoom_scroll_remainder_);
2364 zoom_scroll_remainder_ -= whole_zoom_scroll_remainder_;
2365 if (whole_zoom_scroll_remainder_ != 0) {
2366 delegate_->ContentsZoomChange(whole_zoom_scroll_remainder_ > 0);
2367 }
[email protected]fb3f066e2013-02-12 19:12:522368 return true;
2369 }
[email protected]bccc4472013-04-18 16:37:192370#endif
[email protected]fb3f066e2013-02-12 19:12:522371 return false;
2372}
2373
[email protected]04bce562014-01-30 05:34:542374bool WebContentsImpl::PreHandleGestureEvent(
2375 const blink::WebGestureEvent& event) {
[email protected]28042d32014-02-03 14:10:032376 return delegate_ && delegate_->PreHandleGestureEvent(this, event);
[email protected]04bce562014-01-30 05:34:542377}
2378
kenrb2a565f82015-09-02 20:24:592379RenderWidgetHostInputEventRouter* WebContentsImpl::GetInputEventRouter() {
wjmacleane31234f2015-12-14 17:20:452380 if (!is_being_destroyed_ && GetOuterWebContents())
2381 return GetOuterWebContents()->GetInputEventRouter();
2382
2383 if (!rwh_input_event_router_.get() && !is_being_destroyed_)
kenrb2a565f82015-09-02 20:24:592384 rwh_input_event_router_.reset(new RenderWidgetHostInputEventRouter);
2385 return rwh_input_event_router_.get();
2386}
2387
alexmos3fcd0ca2015-10-23 18:18:332388void WebContentsImpl::ReplicatePageFocus(bool is_focused) {
2389 // Focus loss may occur while this WebContents is being destroyed. Don't
2390 // send the message in this case, as the main frame's RenderFrameHost and
2391 // other state has already been cleared.
2392 if (is_being_destroyed_)
2393 return;
2394
2395 frame_tree_.ReplicatePageFocus(is_focused);
2396}
2397
alexmosc4cbacb2015-11-21 01:29:222398RenderWidgetHostImpl* WebContentsImpl::GetFocusedRenderWidgetHost(
2399 RenderWidgetHostImpl* receiving_widget) {
alexmosc4cbacb2015-11-21 01:29:222400 // Events for widgets other than the main frame (e.g., popup menus) should be
2401 // forwarded directly to the widget they arrived on.
2402 if (receiving_widget != GetMainFrame()->GetRenderWidgetHost())
2403 return receiving_widget;
alexmosc4d183e2015-11-06 00:46:522404
lfg1453e412017-04-11 00:48:502405 WebContentsImpl* focused_contents = GetFocusedWebContents();
2406
2407 // If the focused WebContents is showing an interstitial, return the
2408 // interstitial's widget.
2409 if (focused_contents->ShowingInterstitialPage()) {
2410 return static_cast<RenderFrameHostImpl*>(
creisadca9862017-06-16 19:12:112411 focused_contents->interstitial_page_->GetMainFrame())
lfg1453e412017-04-11 00:48:502412 ->GetRenderWidgetHost();
2413 }
2414
paulmeyerfeafc2d2017-04-25 21:46:402415 // If the focused WebContents is a guest WebContents, then get the focused
2416 // frame in the embedder WebContents instead.
2417 FrameTreeNode* focused_frame = nullptr;
2418 if (focused_contents->browser_plugin_guest_ &&
2419 !GuestMode::IsCrossProcessFrameGuest(focused_contents)) {
2420 focused_frame = frame_tree_.GetFocusedFrame();
2421 } else {
2422 focused_frame = GetFocusedWebContents()->frame_tree_.GetFocusedFrame();
2423 }
lfg1453e412017-04-11 00:48:502424
alexmosc4d183e2015-11-06 00:46:522425 if (!focused_frame)
alexmosc4cbacb2015-11-21 01:29:222426 return receiving_widget;
alexmosc4d183e2015-11-06 00:46:522427
alexmos732ca3a2015-12-08 02:01:022428 // The view may be null if a subframe's renderer process has crashed while
2429 // the subframe has focus. Drop the event in that case. Do not give
2430 // it to the main frame, so that the user doesn't unexpectedly type into the
2431 // wrong frame if a focused subframe renderer crashes while they type.
2432 RenderWidgetHostView* view = focused_frame->current_frame_host()->GetView();
2433 if (!view)
2434 return nullptr;
2435
2436 return RenderWidgetHostImpl::From(view->GetRenderWidgetHost());
alexmosc4d183e2015-11-06 00:46:522437}
2438
avallee9993fca2016-11-17 06:16:502439RenderWidgetHostImpl* WebContentsImpl::GetRenderWidgetHostWithPageFocus() {
lfg1453e412017-04-11 00:48:502440 WebContentsImpl* focused_web_contents = GetFocusedWebContents();
2441
2442 if (focused_web_contents->ShowingInterstitialPage()) {
2443 return static_cast<RenderFrameHostImpl*>(
creisadca9862017-06-16 19:12:112444 focused_web_contents->interstitial_page_->GetMainFrame())
lfg1453e412017-04-11 00:48:502445 ->GetRenderWidgetHost();
2446 }
Alex Valléedc80ae2ed2017-09-06 21:55:442447 if (!GuestMode::IsCrossProcessFrameGuest(focused_web_contents) &&
2448 focused_web_contents->browser_plugin_guest_) {
2449 // If this is a guest, we need to be controlled by our embedder.
2450 return focused_web_contents->GetOuterWebContents()
2451 ->GetMainFrame()
2452 ->GetRenderWidgetHost();
2453 }
lfg1453e412017-04-11 00:48:502454
2455 return focused_web_contents->GetMainFrame()->GetRenderWidgetHost();
avallee9993fca2016-11-17 06:16:502456}
2457
Dave Tapuska3ed44192018-05-01 18:53:302458void WebContentsImpl::EnterFullscreenMode(
2459 const GURL& origin,
2460 const blink::WebFullscreenOptions& options) {
mlamouri7a78d6fd2015-01-17 13:23:532461 // This method is being called to enter renderer-initiated fullscreen mode.
2462 // Make sure any existing fullscreen widget is shut down first.
[email protected]4aebbca2013-09-17 22:26:492463 RenderWidgetHostView* const widget_view = GetFullscreenRenderWidgetHostView();
avi85a4cef2015-12-18 20:13:372464 if (widget_view) {
2465 RenderWidgetHostImpl::From(widget_view->GetRenderWidgetHost())
2466 ->ShutdownAndDestroyWidget(true);
2467 }
[email protected]4aebbca2013-09-17 22:26:492468
Joe Downing13dd76b2018-04-09 18:32:152469 if (delegate_) {
Dave Tapuska3ed44192018-05-01 18:53:302470 delegate_->EnterFullscreenModeForTab(this, origin, options);
mlamouri7a78d6fd2015-01-17 13:23:532471
Joe Downing13dd76b2018-04-09 18:32:152472 if (keyboard_lock_widget_)
2473 delegate_->RequestKeyboardLock(this, esc_key_locked_);
2474 }
2475
ericwilligersde386342016-10-19 02:35:092476 for (auto& observer : observers_)
2477 observer.DidToggleFullscreenModeForTab(IsFullscreenForCurrentTab(), false);
mlamouri7a78d6fd2015-01-17 13:23:532478}
2479
bokanece34a82016-01-28 19:49:462480void WebContentsImpl::ExitFullscreenMode(bool will_cause_resize) {
mlamouri7a78d6fd2015-01-17 13:23:532481 // This method is being called to leave renderer-initiated fullscreen mode.
2482 // Make sure any existing fullscreen widget is shut down first.
2483 RenderWidgetHostView* const widget_view = GetFullscreenRenderWidgetHostView();
avi85a4cef2015-12-18 20:13:372484 if (widget_view) {
2485 RenderWidgetHostImpl::From(widget_view->GetRenderWidgetHost())
2486 ->ShutdownAndDestroyWidget(true);
2487 }
mlamouri7a78d6fd2015-01-17 13:23:532488
Joe Downing13dd76b2018-04-09 18:32:152489 if (delegate_) {
mlamouri7a78d6fd2015-01-17 13:23:532490 delegate_->ExitFullscreenModeForTab(this);
[email protected]657c8e02014-03-19 19:18:372491
Joe Downing13dd76b2018-04-09 18:32:152492 if (keyboard_lock_widget_)
2493 delegate_->CancelKeyboardLockRequest(this);
2494 }
2495
bokanece34a82016-01-28 19:49:462496 // The fullscreen state is communicated to the renderer through a resize
2497 // message. If the change in fullscreen state doesn't cause a view resize
2498 // then we must ensure web contents exit the fullscreen state by explicitly
2499 // sending a resize message. This is required for the situation of the browser
2500 // moving the view into a "browser fullscreen" state and then the contents
2501 // entering "tab fullscreen". Exiting the contents "tab fullscreen" then won't
2502 // have the side effect of the view resizing, hence the explicit call here is
2503 // required.
2504 if (!will_cause_resize) {
2505 if (RenderWidgetHostView* rwhv = GetRenderWidgetHostView()) {
2506 if (RenderWidgetHost* render_widget_host = rwhv->GetRenderWidgetHost())
Fady Samuel0b911822018-04-25 13:22:162507 render_widget_host->SynchronizeVisualProperties();
bokanece34a82016-01-28 19:49:462508 }
scheib3dcb6c932015-02-23 10:55:582509 }
2510
Arthur Sonzogni929d29f2018-09-17 14:19:442511 current_fullscreen_frame_ = nullptr;
Becca Hughese9e27952018-06-25 17:08:272512
ericwilligersde386342016-10-19 02:35:092513 for (auto& observer : observers_) {
2514 observer.DidToggleFullscreenModeForTab(IsFullscreenForCurrentTab(),
2515 will_cause_resize);
2516 }
Becca Hughesd11d6502018-07-31 17:01:282517
2518 if (display_cutout_host_impl_)
2519 display_cutout_host_impl_->DidExitFullscreen();
[email protected]8a5e0ca2011-08-25 06:30:472520}
2521
Becca Hughesfd5d8f892018-06-14 18:23:362522void WebContentsImpl::FullscreenStateChanged(RenderFrameHost* rfh,
2523 bool is_fullscreen) {
Arthur Sonzogni929d29f2018-09-17 14:19:442524 RenderFrameHostImpl* frame = static_cast<RenderFrameHostImpl*>(rfh);
Becca Hughesfd5d8f892018-06-14 18:23:362525
2526 if (is_fullscreen) {
Jan Wilken Dörrie531be7ca2019-06-07 10:05:572527 if (!base::Contains(fullscreen_frames_, frame)) {
Arthur Sonzogni929d29f2018-09-17 14:19:442528 fullscreen_frames_.insert(frame);
2529 FullscreenFrameSetUpdated();
Becca Hughesfd5d8f892018-06-14 18:23:362530 }
Arthur Sonzogni929d29f2018-09-17 14:19:442531 return;
2532 }
Becca Hughesfd5d8f892018-06-14 18:23:362533
Arthur Sonzogni929d29f2018-09-17 14:19:442534 // If |frame| is no longer in fullscreen, remove it and any descendants.
2535 // See https://fullscreen.spec.whatwg.org.
Arthur Sonzogni969a794e2018-10-11 08:56:092536 size_t size_before_deletion = fullscreen_frames_.size();
2537 base::EraseIf(fullscreen_frames_, [&](RenderFrameHostImpl* current) {
2538 return (current == frame || current->IsDescendantOf(frame));
Arthur Sonzogni929d29f2018-09-17 14:19:442539 });
2540
Arthur Sonzogni969a794e2018-10-11 08:56:092541 if (size_before_deletion != fullscreen_frames_.size())
Arthur Sonzogni929d29f2018-09-17 14:19:442542 FullscreenFrameSetUpdated();
2543}
2544
2545void WebContentsImpl::FullscreenFrameSetUpdated() {
2546 if (fullscreen_frames_.empty()) {
2547 current_fullscreen_frame_ = nullptr;
2548 return;
Becca Hughesfd5d8f892018-06-14 18:23:362549 }
2550
Arthur Sonzogni929d29f2018-09-17 14:19:442551 // Find the current fullscreen frame and call the observers.
2552 // If frame A is fullscreen, then frame B goes into inner fullscreen, then B
2553 // exits fullscreen - that will result in A being fullscreen.
2554 RenderFrameHostImpl* new_fullscreen_frame = *std::max_element(
2555 fullscreen_frames_.begin(), fullscreen_frames_.end(), FrameCompareDepth);
Becca Hughesfd5d8f892018-06-14 18:23:362556
Arthur Sonzogni929d29f2018-09-17 14:19:442557 // If we have already notified observers about this frame then we should not
2558 // fire the observers again.
2559 if (new_fullscreen_frame == current_fullscreen_frame_)
2560 return;
2561 current_fullscreen_frame_ = new_fullscreen_frame;
Becca Hughesfd5d8f892018-06-14 18:23:362562
Arthur Sonzogni929d29f2018-09-17 14:19:442563 for (auto& observer : observers_)
2564 observer.DidAcquireFullscreen(new_fullscreen_frame);
Becca Hughesfd5d8f892018-06-14 18:23:362565
Arthur Sonzogni929d29f2018-09-17 14:19:442566 if (display_cutout_host_impl_)
2567 display_cutout_host_impl_->DidAcquireFullscreen(new_fullscreen_frame);
Becca Hughesfd5d8f892018-06-14 18:23:362568}
2569
Clark DuValla0e42992018-09-17 17:00:112570#if defined(OS_ANDROID)
2571void WebContentsImpl::UpdateUserGestureCarryoverInfo() {
2572 if (delegate_)
2573 delegate_->UpdateUserGestureCarryoverInfo(this);
2574}
2575#endif
2576
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:412577bool WebContentsImpl::IsFullscreenForCurrentTab() {
[email protected]199bba6e2012-04-04 16:19:382578 return delegate_ ? delegate_->IsFullscreenForTabOrPending(this) : false;
[email protected]5d5f7af2011-10-01 01:38:122579}
2580
Malay Keshav87c42c02019-01-15 08:37:472581bool WebContentsImpl::ShouldShowStaleContentOnEviction() {
2582 return GetDelegate() && GetDelegate()->ShouldShowStaleContentOnEviction(this);
2583}
2584
billorr21c005d2016-11-17 03:57:042585bool WebContentsImpl::IsFullscreen() {
2586 return IsFullscreenForCurrentTab();
2587}
2588
avibf58fa0c2015-11-11 01:45:522589blink::WebDisplayMode WebContentsImpl::GetDisplayMode(
2590 RenderWidgetHostImpl* render_widget_host) const {
2591 if (!RenderViewHostImpl::From(render_widget_host))
Blink Reformat1c4d759e2017-04-09 16:34:542592 return blink::kWebDisplayModeBrowser;
avibf58fa0c2015-11-11 01:45:522593
mikhail.pozdnyakovc0e251b2015-04-15 06:51:122594 return delegate_ ? delegate_->GetDisplayMode(this)
Blink Reformat1c4d759e2017-04-09 16:34:542595 : blink::kWebDisplayModeBrowser;
mikhail.pozdnyakovc0e251b2015-04-15 06:51:122596}
2597
avic3aa8422015-11-09 20:57:222598void WebContentsImpl::RequestToLockMouse(
2599 RenderWidgetHostImpl* render_widget_host,
2600 bool user_gesture,
lfgad8244352016-07-13 16:55:512601 bool last_unlocked_by_target,
2602 bool privileged) {
lfgf0cd46e2017-01-04 00:05:232603 for (WebContentsImpl* current = this; current;
2604 current = current->GetOuterWebContents()) {
2605 if (current->mouse_lock_widget_) {
2606 render_widget_host->GotResponseToLockMouseRequest(false);
2607 return;
2608 }
[email protected]e9621112011-10-17 05:38:372609 }
avic3aa8422015-11-09 20:57:222610
lfgad8244352016-07-13 16:55:512611 if (privileged) {
lfgf0cd46e2017-01-04 00:05:232612 DCHECK(!GetOuterWebContents());
lfgad8244352016-07-13 16:55:512613 mouse_lock_widget_ = render_widget_host;
2614 render_widget_host->GotResponseToLockMouseRequest(true);
2615 return;
2616 }
2617
lfgbee1e0a2016-06-08 21:24:212618 bool widget_in_frame_tree = false;
2619 for (FrameTreeNode* node : frame_tree_.Nodes()) {
2620 if (node->current_frame_host()->GetRenderWidgetHost() ==
2621 render_widget_host) {
2622 widget_in_frame_tree = true;
2623 break;
2624 }
2625 }
2626
2627 if (widget_in_frame_tree && delegate_) {
lfgf0cd46e2017-01-04 00:05:232628 for (WebContentsImpl* current = this; current;
2629 current = current->GetOuterWebContents()) {
2630 current->mouse_lock_widget_ = render_widget_host;
2631 }
2632
avic3aa8422015-11-09 20:57:222633 delegate_->RequestToLockMouse(this, user_gesture, last_unlocked_by_target);
lfgbee1e0a2016-06-08 21:24:212634 } else {
2635 render_widget_host->GotResponseToLockMouseRequest(false);
2636 }
[email protected]e9621112011-10-17 05:38:372637}
2638
avi3e4b8512015-11-11 01:55:492639void WebContentsImpl::LostMouseLock(RenderWidgetHostImpl* render_widget_host) {
lfgbee1e0a2016-06-08 21:24:212640 CHECK(mouse_lock_widget_);
lfgf0cd46e2017-01-04 00:05:232641
2642 if (mouse_lock_widget_->delegate()->GetAsWebContents() != this)
2643 return mouse_lock_widget_->delegate()->LostMouseLock(render_widget_host);
2644
lfgbee1e0a2016-06-08 21:24:212645 mouse_lock_widget_->SendMouseLockLost();
lfgf0cd46e2017-01-04 00:05:232646 for (WebContentsImpl* current = this; current;
2647 current = current->GetOuterWebContents()) {
2648 current->mouse_lock_widget_ = nullptr;
2649 }
avi3e4b8512015-11-11 01:55:492650
[email protected]e9621112011-10-17 05:38:372651 if (delegate_)
2652 delegate_->LostMouseLock();
2653}
2654
lfgad8244352016-07-13 16:55:512655bool WebContentsImpl::HasMouseLock(RenderWidgetHostImpl* render_widget_host) {
2656 // To verify if the mouse is locked, the mouse_lock_widget_ needs to be
2657 // assigned to the widget that requested the mouse lock, and the top-level
2658 // platform RenderWidgetHostView needs to hold the mouse lock from the OS.
Lucas Gadania0ea0172018-09-20 18:31:372659 auto* widget_host = GetTopLevelRenderWidgetHostView();
2660 return mouse_lock_widget_ == render_widget_host && widget_host &&
2661 widget_host->IsMouseLocked();
lfgad8244352016-07-13 16:55:512662}
2663
lfg84763c92017-02-16 18:55:152664RenderWidgetHostImpl* WebContentsImpl::GetMouseLockWidget() {
Lucas Gadania0ea0172018-09-20 18:31:372665 auto* widget_host = GetTopLevelRenderWidgetHostView();
2666 if ((widget_host && widget_host->IsMouseLocked()) ||
lfge2176fb2017-03-26 00:45:472667 (GetFullscreenRenderWidgetHostView() &&
Lucas Gadania0ea0172018-09-20 18:31:372668 GetFullscreenRenderWidgetHostView()->IsMouseLocked())) {
lfg84763c92017-02-16 18:55:152669 return mouse_lock_widget_;
Lucas Gadania0ea0172018-09-20 18:31:372670 }
lfg84763c92017-02-16 18:55:152671
2672 return nullptr;
2673}
2674
Joe Downing192998b22018-03-22 15:51:362675bool WebContentsImpl::RequestKeyboardLock(
Joe Downing13dd76b2018-04-09 18:32:152676 RenderWidgetHostImpl* render_widget_host,
2677 bool esc_key_locked) {
Joe Downing192998b22018-03-22 15:51:362678 DCHECK(render_widget_host);
Joe Downing192998b22018-03-22 15:51:362679 if (render_widget_host->delegate()->GetAsWebContents() != this) {
2680 NOTREACHED();
2681 return false;
2682 }
2683
2684 // KeyboardLock is only supported when called by the top-level browsing
2685 // context and is not supported in embedded content scenarios.
2686 if (GetOuterWebContents())
2687 return false;
2688
Joe Downing13dd76b2018-04-09 18:32:152689 esc_key_locked_ = esc_key_locked;
Joe Downing192998b22018-03-22 15:51:362690 keyboard_lock_widget_ = render_widget_host;
2691
Joe Downing13dd76b2018-04-09 18:32:152692 if (delegate_)
2693 delegate_->RequestKeyboardLock(this, esc_key_locked_);
Joe Downing192998b22018-03-22 15:51:362694 return true;
2695}
2696
2697void WebContentsImpl::CancelKeyboardLock(
2698 RenderWidgetHostImpl* render_widget_host) {
2699 if (!keyboard_lock_widget_ || render_widget_host != keyboard_lock_widget_)
2700 return;
2701
2702 RenderWidgetHostImpl* old_keyboard_lock_widget = keyboard_lock_widget_;
2703 keyboard_lock_widget_ = nullptr;
2704
Joe Downing13dd76b2018-04-09 18:32:152705 if (delegate_)
2706 delegate_->CancelKeyboardLockRequest(this);
2707
Joe Downing192998b22018-03-22 15:51:362708 old_keyboard_lock_widget->CancelKeyboardLock();
2709}
2710
2711RenderWidgetHostImpl* WebContentsImpl::GetKeyboardLockWidget() {
2712 return keyboard_lock_widget_;
2713}
2714
Dave Tapuskac3344362019-02-20 17:54:282715void WebContentsImpl::OnRenderFrameProxyVisibilityChanged(
2716 blink::mojom::FrameVisibility visibility) {
2717 switch (visibility) {
2718 case blink::mojom::FrameVisibility::kRenderedInViewport:
2719 WasShown();
2720 break;
2721 case blink::mojom::FrameVisibility::kNotRendered:
2722 WasHidden();
2723 break;
2724 case blink::mojom::FrameVisibility::kRenderedOutOfViewport:
2725 WasOccluded();
2726 break;
2727 }
ekaramada4f42692016-02-11 19:48:372728}
2729
[email protected]bafe6cd2012-05-23 23:09:502730void WebContentsImpl::CreateNewWindow(
csharrison95f01e922017-04-24 18:52:352731 RenderFrameHost* opener,
nick5ae4d2d2017-01-06 01:18:352732 int32_t render_view_route_id,
dcheng3ce04b62015-10-26 23:30:552733 int32_t main_frame_route_id,
2734 int32_t main_frame_widget_route_id,
rockot5c478a72016-09-28 23:14:182735 const mojom::CreateNewWindowParams& params,
Sergey Kuznetsov32257a22019-02-11 20:26:502736 bool has_user_gesture,
[email protected]97714c82012-06-06 10:15:132737 SessionStorageNamespace* session_storage_namespace) {
nick5ae4d2d2017-01-06 01:18:352738 // We should have zero valid routing ids, or three valid routing IDs.
2739 DCHECK_EQ((render_view_route_id == MSG_ROUTING_NONE),
2740 (main_frame_route_id == MSG_ROUTING_NONE));
2741 DCHECK_EQ((render_view_route_id == MSG_ROUTING_NONE),
2742 (main_frame_widget_route_id == MSG_ROUTING_NONE));
csharrison95f01e922017-04-24 18:52:352743 DCHECK(opener);
nick5ae4d2d2017-01-06 01:18:352744
csharrison95f01e922017-04-24 18:52:352745 int render_process_id = opener->GetProcess()->GetID();
2746 SiteInstance* source_site_instance = opener->GetSiteInstance();
2747
2748 // The route IDs passed into this function can be trusted not to already
2749 // be in use; they were allocated by the RenderWidgetHelper by the caller.
nick5ae4d2d2017-01-06 01:18:352750 DCHECK(!RenderFrameHostImpl::FromID(render_process_id, main_frame_route_id));
2751
[email protected]bafe6cd2012-05-23 23:09:502752 // We usually create the new window in the same BrowsingInstance (group of
2753 // script-related windows), by passing in the current SiteInstance. However,
[email protected]c4538072013-03-18 02:17:552754 // if the opener is being suppressed (in a non-guest), we create a new
2755 // SiteInstance in its own BrowsingInstance.
[email protected]a24efc22014-05-26 15:50:252756 bool is_guest = BrowserPluginGuest::IsGuest(this);
[email protected]c4538072013-03-18 02:17:552757
[email protected]04cbd3d2013-12-04 04:58:202758 // If the opener is to be suppressed, the new window can be in any process.
2759 // Since routing ids are process specific, we must not have one passed in
2760 // as argument here.
nick5ae4d2d2017-01-06 01:18:352761 DCHECK(!params.opener_suppressed || render_view_route_id == MSG_ROUTING_NONE);
[email protected]04cbd3d2013-12-04 04:58:202762
[email protected]bafe6cd2012-05-23 23:09:502763 scoped_refptr<SiteInstance> site_instance =
alexmos4cf2aa32015-07-15 23:40:432764 params.opener_suppressed && !is_guest
2765 ? SiteInstance::CreateForURL(GetBrowserContext(), params.target_url)
2766 : source_site_instance;
[email protected]bafe6cd2012-05-23 23:09:502767
[email protected]d1198fd2012-08-13 22:50:192768 // We must assign the SessionStorageNamespace before calling Init().
[email protected]4c3a23582012-08-18 08:54:342769 //
2770 // http://crbug.com/142685
[email protected]fdac6ade2013-07-20 01:06:302771 const std::string& partition_id =
2772 GetContentClient()->browser()->
2773 GetStoragePartitionIdForSite(GetBrowserContext(),
2774 site_instance->GetSiteURL());
[email protected]fc72bb12013-06-02 21:13:462775 StoragePartition* partition = BrowserContext::GetStoragePartition(
2776 GetBrowserContext(), site_instance.get());
[email protected]5f2aa722013-08-07 16:59:412777 DOMStorageContextWrapper* dom_storage_context =
2778 static_cast<DOMStorageContextWrapper*>(partition->GetDOMStorageContext());
[email protected]d1198fd2012-08-13 22:50:192779 SessionStorageNamespaceImpl* session_storage_namespace_impl =
2780 static_cast<SessionStorageNamespaceImpl*>(session_storage_namespace);
2781 CHECK(session_storage_namespace_impl->IsFromContext(dom_storage_context));
[email protected]dd6730412013-08-14 15:03:372782
2783 if (delegate_ &&
dcheng3ce04b62015-10-26 23:30:552784 !delegate_->ShouldCreateWebContents(
lukasza6f8ac622017-06-06 03:10:202785 this, opener, source_site_instance, render_view_route_id,
2786 main_frame_route_id, main_frame_widget_route_id,
2787 params.window_container_type, opener->GetLastCommittedURL(),
2788 params.frame_name, params.target_url, partition_id,
2789 session_storage_namespace)) {
jambaaab3a2016-11-05 00:46:182790 // Note: even though we're not creating a WebContents here, it could have
2791 // been created by the embedder so ensure that the RenderFrameHost is
2792 // properly initialized.
csharrisona2280cd2016-02-03 23:21:152793 // It's safe to only target the frame because the render process will not
2794 // have a chance to create more frames at this point.
jambaaab3a2016-11-05 00:46:182795 RenderFrameHostImpl* rfh =
2796 RenderFrameHostImpl::FromID(render_process_id, main_frame_route_id);
nick5ae4d2d2017-01-06 01:18:352797 if (rfh) {
2798 DCHECK(rfh->IsRenderFrameLive());
jambaaab3a2016-11-05 00:46:182799 rfh->Init();
nick5ae4d2d2017-01-06 01:18:352800 }
[email protected]dd6730412013-08-14 15:03:372801 return;
2802 }
2803
danakj22b4e8b2019-08-01 23:14:392804 bool renderer_started_hidden =
2805 params.disposition == WindowOpenDisposition::NEW_BACKGROUND_TAB;
2806
[email protected]dd6730412013-08-14 15:03:372807 // Create the new web contents. This will automatically create the new
2808 // WebContentsView. In the future, we may want to create the view separately.
[email protected]fc72bb12013-06-02 21:13:462809 CreateParams create_params(GetBrowserContext(), site_instance.get());
nick5ae4d2d2017-01-06 01:18:352810 create_params.routing_id = render_view_route_id;
[email protected]227692c52013-05-31 22:43:042811 create_params.main_frame_routing_id = main_frame_route_id;
dcheng3ce04b62015-10-26 23:30:552812 create_params.main_frame_widget_routing_id = main_frame_widget_route_id;
nasko48321ca32015-07-02 20:44:122813 create_params.main_frame_name = params.frame_name;
alexmos4cf2aa32015-07-15 23:40:432814 create_params.opener_render_process_id = render_process_id;
csharrison95f01e922017-04-24 18:52:352815 create_params.opener_render_frame_id = opener->GetRoutingID();
[email protected]50d326e2014-05-20 17:59:062816 create_params.opener_suppressed = params.opener_suppressed;
danakj22b4e8b2019-08-01 23:14:392817 create_params.initially_hidden = renderer_started_hidden;
lfg269b702f2015-06-08 19:28:192818 create_params.renderer_initiated_creation =
2819 main_frame_route_id != MSG_ROUTING_NONE;
[email protected]50d326e2014-05-20 17:59:062820
erikchenbee5c9622018-04-27 19:30:252821 std::unique_ptr<WebContents> new_contents;
[email protected]c4538072013-03-18 02:17:552822 if (!is_guest) {
2823 create_params.context = view_->GetNativeView();
[email protected]fc2b46b2014-05-03 16:33:452824 create_params.initial_size = GetContainerBounds().size();
Erik Chenbb8e738e2018-04-28 14:10:432825 new_contents = WebContents::Create(create_params);
[email protected]4858e432014-06-23 18:14:172826 } else {
erikchenbee5c9622018-04-27 19:30:252827 new_contents = base::WrapUnique(
2828 GetBrowserPluginGuest()->CreateNewGuestWindow(create_params));
[email protected]c4538072013-03-18 02:17:552829 }
danakje6c01622018-10-05 19:39:262830 auto owning_contents_impl =
2831 base::WrapUnique(static_cast<WebContentsImpl*>(new_contents.release()));
2832 auto* new_contents_impl = owning_contents_impl.get();
2833
2834 new_contents_impl->GetController().SetSessionStorageNamespace(
erikchenbee5c9622018-04-27 19:30:252835 partition_id, session_storage_namespace);
[email protected]d1198fd2012-08-13 22:50:192836
alexmos646fec02015-07-25 00:11:492837 // If the new frame has a name, make sure any SiteInstances that can find
2838 // this named frame have proxies for it. Must be called after
2839 // SetSessionStorageNamespace, since this calls CreateRenderView, which uses
2840 // GetSessionStorageNamespace.
2841 if (!params.frame_name.empty())
danakje6c01622018-10-05 19:39:262842 new_contents_impl->GetRenderManager()->CreateProxiesForNewNamedFrame();
alexmos646fec02015-07-25 00:11:492843
[email protected]c4538072013-03-18 02:17:552844 // Save the window for later if we're not suppressing the opener (since it
[email protected]d70bea92013-04-05 04:23:342845 // will be shown immediately).
2846 if (!params.opener_suppressed) {
2847 if (!is_guest) {
danakje6c01622018-10-05 19:39:262848 WebContentsView* new_view = new_contents_impl->view_.get();
[email protected]bafe6cd2012-05-23 23:09:502849
[email protected]d70bea92013-04-05 04:23:342850 // TODO(brettw): It seems bogus that we have to call this function on the
2851 // newly created object and give it one of its own member variables.
danakj22b4e8b2019-08-01 23:14:392852 RenderWidgetHostView* widget_view = new_view->CreateViewForWidget(
danakje6c01622018-10-05 19:39:262853 new_contents_impl->GetRenderViewHost()->GetWidget(), false);
danakj22b4e8b2019-08-01 23:14:392854 if (!renderer_started_hidden) {
2855 // RenderWidgets for frames always initialize as hidden. If the renderer
2856 // created this window as visible, then we show it here.
2857 widget_view->Show();
2858 }
[email protected]d70bea92013-04-05 04:23:342859 }
[email protected]bafe6cd2012-05-23 23:09:502860 // Save the created window associated with the route so we can show it
2861 // later.
Albert J. Wongcb004632018-07-10 22:58:252862 //
2863 // TODO(ajwong): This should be keyed off the RenderFrame routing id or the
2864 // FrameTreeNode id instead of the routing id of the Widget for the main
2865 // frame. https://crbug.com/545684
nick5ae4d2d2017-01-06 01:18:352866 DCHECK_NE(MSG_ROUTING_NONE, main_frame_widget_route_id);
danakje6c01622018-10-05 19:39:262867 GlobalRoutingID id(render_process_id, main_frame_widget_route_id);
2868 pending_contents_[id] = std::move(owning_contents_impl);
2869 AddDestructionObserver(new_contents_impl);
[email protected]bafe6cd2012-05-23 23:09:502870 }
2871
2872 if (delegate_) {
Bryan McQuaded2b93c02017-07-14 22:26:312873 delegate_->WebContentsCreated(this, render_process_id,
2874 opener->GetRoutingID(), params.frame_name,
danakje6c01622018-10-05 19:39:262875 params.target_url, new_contents_impl);
[email protected]bafe6cd2012-05-23 23:09:502876 }
2877
Charlie Harrisonf60dd1c2018-08-15 21:15:412878 for (auto& observer : observers_) {
danakje6c01622018-10-05 19:39:262879 observer.DidOpenRequestedURL(new_contents_impl, opener, params.target_url,
Leon Han963dc182018-11-06 05:41:482880 params.referrer.To<Referrer>(),
2881 params.disposition, ui::PAGE_TRANSITION_LINK,
Charlie Harrisonf60dd1c2018-08-15 21:15:412882 false, // started_from_context_menu
2883 true); // renderer_initiated
pnolandaae574e2017-03-06 21:04:212884 }
2885
Avi Drissmanba3b1b342017-08-29 18:16:132886 // Any new WebContents opened while this WebContents is in fullscreen can be
2887 // used to confuse the user, so drop fullscreen.
Avi Drissmand18c51972018-08-15 18:32:092888 ForSecurityDropFullscreen();
Avi Drissmanba3b1b342017-08-29 18:16:132889
[email protected]bafe6cd2012-05-23 23:09:502890 if (params.opener_suppressed) {
2891 // When the opener is suppressed, the original renderer cannot access the
2892 // new window. As a result, we need to show and navigate the window here.
[email protected]eda238a12012-09-07 23:44:002893 bool was_blocked = false;
erikchenbee5c9622018-04-27 19:30:252894
[email protected]eda238a12012-09-07 23:44:002895 if (delegate_) {
danakje6c01622018-10-05 19:39:262896 base::WeakPtr<WebContentsImpl> weak_new_contents =
2897 new_contents_impl->weak_factory_.GetWeakPtr();
nick0d558482017-02-13 22:27:222898
danakje6c01622018-10-05 19:39:262899 gfx::Rect initial_rect; // Report an empty initial rect.
2900 delegate_->AddNewContents(this, std::move(owning_contents_impl),
erikchenbee5c9622018-04-27 19:30:252901 params.disposition, initial_rect,
Sergey Kuznetsov32257a22019-02-11 20:26:502902 has_user_gesture, &was_blocked);
danakje6c01622018-10-05 19:39:262903 // The delegate may delete |new_contents_impl| during AddNewContents().
nick0d558482017-02-13 22:27:222904 if (!weak_new_contents)
danakje6c01622018-10-05 19:39:262905 return;
[email protected]eda238a12012-09-07 23:44:002906 }
erikchenbee5c9622018-04-27 19:30:252907
[email protected]eda238a12012-09-07 23:44:002908 if (!was_blocked) {
Nasko Oskov83a8cf92018-10-19 14:58:562909 std::unique_ptr<NavigationController::LoadURLParams> load_params =
2910 std::make_unique<NavigationController::LoadURLParams>(
2911 params.target_url);
Nasko Oskov93e7c55c2018-12-19 01:59:292912 load_params->initiator_origin = opener->GetLastCommittedOrigin();
Leon Han963dc182018-11-06 05:41:482913 load_params->referrer = params.referrer.To<Referrer>();
Nasko Oskov83a8cf92018-10-19 14:58:562914 load_params->transition_type = ui::PAGE_TRANSITION_LINK;
2915 load_params->is_renderer_initiated = true;
Sergey Kuznetsov32257a22019-02-11 20:26:502916 load_params->has_user_gesture = has_user_gesture;
mariakhomenkoa4971c12015-07-21 19:04:372917
2918 if (delegate_ && !is_guest &&
2919 !delegate_->ShouldResumeRequestsForCreatedWindow()) {
Nasko Oskov83a8cf92018-10-19 14:58:562920 // We are in asynchronous add new contents path, delay navigation.
2921 DCHECK(!new_contents_impl->delayed_open_url_params_);
2922 new_contents_impl->delayed_load_url_params_ = std::move(load_params);
mariakhomenkoa4971c12015-07-21 19:04:372923 } else {
Nasko Oskov83a8cf92018-10-19 14:58:562924 new_contents_impl->controller_.LoadURLWithParams(*load_params.get());
Nasko Oskov28ebd5b2018-12-07 22:29:332925 if (!is_guest)
2926 new_contents_impl->Focus();
mariakhomenkoa4971c12015-07-21 19:04:372927 }
[email protected]eda238a12012-09-07 23:44:002928 }
[email protected]bafe6cd2012-05-23 23:09:502929 }
2930}
2931
avib7348942015-12-25 20:57:102932void WebContentsImpl::CreateNewWidget(int32_t render_process_id,
Albert J. Wongf6e13ed2018-09-18 15:25:472933 int32_t widget_route_id,
danakj829cdd142018-09-14 21:13:272934 mojom::WidgetPtr widget) {
Albert J. Wongf6e13ed2018-09-18 15:25:472935 CreateNewWidget(render_process_id, widget_route_id, /*is_fullscreen=*/false,
danakj829cdd142018-09-14 21:13:272936 std::move(widget));
[email protected]bafe6cd2012-05-23 23:09:502937}
2938
avib7348942015-12-25 20:57:102939void WebContentsImpl::CreateNewFullscreenWidget(int32_t render_process_id,
Albert J. Wongf6e13ed2018-09-18 15:25:472940 int32_t widget_route_id,
Dave Tapuska1bdf1832017-07-07 18:07:192941 mojom::WidgetPtr widget) {
Albert J. Wongf6e13ed2018-09-18 15:25:472942 CreateNewWidget(render_process_id, widget_route_id, /*is_fullscreen=*/true,
danakj829cdd142018-09-14 21:13:272943 std::move(widget));
[email protected]bafe6cd2012-05-23 23:09:502944}
2945
avib7348942015-12-25 20:57:102946void WebContentsImpl::CreateNewWidget(int32_t render_process_id,
2947 int32_t route_id,
[email protected]bafe6cd2012-05-23 23:09:502948 bool is_fullscreen,
danakj829cdd142018-09-14 21:13:272949 mojom::WidgetPtr widget) {
kenrba26d15092016-02-10 22:00:252950 RenderProcessHost* process = RenderProcessHost::FromID(render_process_id);
kenrbb46f86a2015-12-07 07:53:232951 // A message to create a new widget can only come from an active process for
[email protected]a8504022013-12-04 20:23:512952 // this WebContentsImpl instance. If any other process sends the request,
2953 // it is invalid and the process must be terminated.
dcheng57e39e22016-01-21 00:25:382954 if (!HasMatchingProcess(&frame_tree_, render_process_id)) {
Lukasz Anforowicz6f746282018-01-04 23:24:512955 ReceivedBadMessage(process, bad_message::WCI_NEW_WIDGET_PROCESS_MISMATCH);
[email protected]a8504022013-12-04 20:23:512956 return;
2957 }
2958
Dave Tapuska485aca92017-08-08 00:47:582959 RenderWidgetHostImpl* widget_host = new RenderWidgetHostImpl(
2960 this, process, route_id, std::move(widget), IsHidden());
[email protected]b24b68a2012-09-24 21:57:262961
[email protected]cfd80b02014-05-01 17:46:482962 RenderWidgetHostViewBase* widget_view =
2963 static_cast<RenderWidgetHostViewBase*>(
danakj0b246942018-09-17 21:30:272964 view_->CreateViewForChildWidget(widget_host));
[email protected]83918ec2013-01-10 15:37:192965 if (!widget_view)
2966 return;
danakj0b246942018-09-17 21:30:272967 // Fullscreen child widgets are frames, other child widgets are popups, and
2968 // popups should not get activated.
2969 if (!is_fullscreen)
2970 widget_view->SetWidgetType(WidgetType::kPopup);
[email protected]bafe6cd2012-05-23 23:09:502971 // Save the created widget associated with the route so we can show it later.
Lukasz Anforowicz09060bdf72018-08-23 15:53:172972 pending_widget_views_[GlobalRoutingID(render_process_id, route_id)] =
alexmosc2a8cec2016-05-23 22:19:532973 widget_view;
[email protected]bafe6cd2012-05-23 23:09:502974}
2975
alexmosc2a8cec2016-05-23 22:19:532976void WebContentsImpl::ShowCreatedWindow(int process_id,
nick5ae4d2d2017-01-06 01:18:352977 int main_frame_widget_route_id,
[email protected]bafe6cd2012-05-23 23:09:502978 WindowOpenDisposition disposition,
bokan107a47f2015-02-03 23:23:392979 const gfx::Rect& initial_rect,
[email protected]bafe6cd2012-05-23 23:09:502980 bool user_gesture) {
danakje6c01622018-10-05 19:39:262981 // This method is the renderer requesting an existing top level window to
2982 // show a new top level window that the renderer created. Each top level
2983 // window is associated with a WebContents. In this case it was created
2984 // earlier but showing it was deferred until the renderer requested for it
2985 // to be shown. We find that previously created WebContents here.
2986 // TODO(danakj): Why do we defer this show step until the renderer asks for it
2987 // when it will always do so. What needs to happen in the renderer before we
2988 // reach here?
2989 std::unique_ptr<WebContentsImpl> owned_created =
nick5ae4d2d2017-01-06 01:18:352990 GetCreatedWindow(process_id, main_frame_widget_route_id);
danakje6c01622018-10-05 19:39:262991 WebContentsImpl* created = owned_created.get();
2992 // The browser may have rejected the request to make a new window, or the
2993 // renderer could be sending an invalid route id. Ignore the request then.
2994 if (!created)
2995 return;
mariakhomenko44bdc4732015-04-29 01:55:382996
danakje6c01622018-10-05 19:39:262997 // This uses the delegate for the WebContents where the window was created
2998 // from, to control how to show the newly created window.
2999 WebContentsDelegate* delegate = GetDelegate();
nick5ae4d2d2017-01-06 01:18:353000
danakje6c01622018-10-05 19:39:263001 // The delegate can be null in tests, so we must check for it :(.
3002 if (delegate) {
3003 // Mark the web contents as pending resume, then immediately do
3004 // the resume if the delegate wants it.
3005 created->is_resume_pending_ = true;
3006 if (delegate->ShouldResumeRequestsForCreatedWindow())
3007 created->ResumeLoadingCreatedWebContents();
3008
3009 base::WeakPtr<WebContentsImpl> weak_created =
3010 created->weak_factory_.GetWeakPtr();
3011 delegate->AddNewContents(this, std::move(owned_created), disposition,
3012 initial_rect, user_gesture, nullptr);
3013 // The delegate may delete |created| during AddNewContents().
3014 if (!weak_created)
3015 return;
[email protected]eda238a12012-09-07 23:44:003016 }
danakje6c01622018-10-05 19:39:263017
3018 RenderWidgetHostImpl* rwh = created->GetMainFrame()->GetRenderWidgetHost();
3019 DCHECK_EQ(main_frame_widget_route_id, rwh->GetRoutingID());
3020 rwh->Send(new WidgetMsg_SetBounds_ACK(rwh->GetRoutingID()));
[email protected]bafe6cd2012-05-23 23:09:503021}
3022
alexmosc2a8cec2016-05-23 22:19:533023void WebContentsImpl::ShowCreatedWidget(int process_id,
Albert J. Wongf6e13ed2018-09-18 15:25:473024 int widget_route_id,
bokan107a47f2015-02-03 23:23:393025 const gfx::Rect& initial_rect) {
Albert J. Wongf6e13ed2018-09-18 15:25:473026 ShowCreatedWidget(process_id, widget_route_id, false, initial_rect);
[email protected]bafe6cd2012-05-23 23:09:503027}
3028
alexmosc2a8cec2016-05-23 22:19:533029void WebContentsImpl::ShowCreatedFullscreenWidget(int process_id,
Albert J. Wongf6e13ed2018-09-18 15:25:473030 int widget_route_id) {
3031 ShowCreatedWidget(process_id, widget_route_id, true, gfx::Rect());
[email protected]bafe6cd2012-05-23 23:09:503032}
3033
alexmosc2a8cec2016-05-23 22:19:533034void WebContentsImpl::ShowCreatedWidget(int process_id,
3035 int route_id,
[email protected]bafe6cd2012-05-23 23:09:503036 bool is_fullscreen,
bokan107a47f2015-02-03 23:23:393037 const gfx::Rect& initial_rect) {
[email protected]cfd80b02014-05-01 17:46:483038 RenderWidgetHostViewBase* widget_host_view =
alexmosc2a8cec2016-05-23 22:19:533039 static_cast<RenderWidgetHostViewBase*>(
3040 GetCreatedWidget(process_id, route_id));
[email protected]bafe6cd2012-05-23 23:09:503041 if (!widget_host_view)
3042 return;
[email protected]cfd80b02014-05-01 17:46:483043
Ivan Kotenkov2c0d2bb32017-11-01 15:41:283044 RenderWidgetHostView* view = nullptr;
lfgc740d4b22016-04-15 16:45:333045 if (GetOuterWebContents()) {
3046 view = GetOuterWebContents()->GetRenderWidgetHostView();
[email protected]cfd80b02014-05-01 17:46:483047 } else {
3048 view = GetRenderWidgetHostView();
3049 }
3050
danakj0b246942018-09-17 21:30:273051 // Fullscreen child widgets are frames, other child widgets are popups.
[email protected]4aebbca2013-09-17 22:26:493052 if (is_fullscreen) {
[email protected]d7f80ba2013-10-12 07:42:313053 DCHECK_EQ(MSG_ROUTING_NONE, fullscreen_widget_routing_id_);
[email protected]0b431992014-06-24 00:08:033054 view_->StoreFocus();
alexmosc9e76ec2016-05-16 22:59:373055 fullscreen_widget_process_id_ =
3056 widget_host_view->GetRenderWidgetHost()->GetProcess()->GetID();
[email protected]d7f80ba2013-10-12 07:42:313057 fullscreen_widget_routing_id_ = route_id;
[email protected]4aebbca2013-09-17 22:26:493058 if (delegate_ && delegate_->EmbedsFullscreenWidget()) {
3059 widget_host_view->InitAsChild(GetRenderWidgetHostView()->GetNativeView());
Dave Tapuska3ed44192018-05-01 18:53:303060 delegate_->EnterFullscreenModeForTab(this, GURL(),
3061 blink::WebFullscreenOptions());
[email protected]4aebbca2013-09-17 22:26:493062 } else {
[email protected]cfd80b02014-05-01 17:46:483063 widget_host_view->InitAsFullscreen(view);
[email protected]4aebbca2013-09-17 22:26:493064 }
ericwilligersde386342016-10-19 02:35:093065 for (auto& observer : observers_)
3066 observer.DidShowFullscreenWidget();
[email protected]4aebbca2013-09-17 22:26:493067 if (!widget_host_view->HasFocus())
3068 widget_host_view->Focus();
3069 } else {
bokan107a47f2015-02-03 23:23:393070 widget_host_view->InitAsPopup(view, initial_rect);
[email protected]4aebbca2013-09-17 22:26:493071 }
[email protected]89054502012-06-03 10:29:243072
yiyixcb1fbc4f2018-03-16 19:54:083073 RenderWidgetHostImpl* render_widget_host_impl = widget_host_view->host();
[email protected]89054502012-06-03 10:29:243074 render_widget_host_impl->Init();
[email protected]d7f80ba2013-10-12 07:42:313075 // Only allow privileged mouse lock for fullscreen render widget, which is
3076 // used to implement Pepper Flash fullscreen.
3077 render_widget_host_impl->set_allow_privileged_mouse_lock(is_fullscreen);
[email protected]bafe6cd2012-05-23 23:09:503078}
3079
danakje6c01622018-10-05 19:39:263080std::unique_ptr<WebContentsImpl> WebContentsImpl::GetCreatedWindow(
nick5ae4d2d2017-01-06 01:18:353081 int process_id,
3082 int main_frame_widget_route_id) {
Lukasz Anforowicz09060bdf72018-08-23 15:53:173083 auto key = GlobalRoutingID(process_id, main_frame_widget_route_id);
nick5ae4d2d2017-01-06 01:18:353084 auto iter = pending_contents_.find(key);
[email protected]bafe6cd2012-05-23 23:09:503085
3086 // Certain systems can block the creation of new windows. If we didn't succeed
3087 // in creating one, just return NULL.
alexmosc2a8cec2016-05-23 22:19:533088 if (iter == pending_contents_.end())
3089 return nullptr;
[email protected]bafe6cd2012-05-23 23:09:503090
danakje6c01622018-10-05 19:39:263091 std::unique_ptr<WebContentsImpl> new_contents = std::move(iter->second);
nick5ae4d2d2017-01-06 01:18:353092 pending_contents_.erase(key);
danakje6c01622018-10-05 19:39:263093 RemoveDestructionObserver(new_contents.get());
[email protected]bafe6cd2012-05-23 23:09:503094
[email protected]d70bea92013-04-05 04:23:343095 // Don't initialize the guest WebContents immediately.
danakje6c01622018-10-05 19:39:263096 if (BrowserPluginGuest::IsGuest(new_contents.get()))
[email protected]d70bea92013-04-05 04:23:343097 return new_contents;
3098
Lukasz Anforowicz5510ed652018-06-06 16:16:193099 if (!new_contents->GetMainFrame()->GetProcess()->IsInitializedAndNotDead() ||
nick5ae4d2d2017-01-06 01:18:353100 !new_contents->GetMainFrame()->GetView()) {
alexmosc2a8cec2016-05-23 22:19:533101 return nullptr;
nick5ae4d2d2017-01-06 01:18:353102 }
[email protected]bafe6cd2012-05-23 23:09:503103
[email protected]bafe6cd2012-05-23 23:09:503104 return new_contents;
3105}
3106
alexmosc2a8cec2016-05-23 22:19:533107RenderWidgetHostView* WebContentsImpl::GetCreatedWidget(int process_id,
3108 int route_id) {
Lukasz Anforowicz09060bdf72018-08-23 15:53:173109 auto iter = pending_widget_views_.find(GlobalRoutingID(process_id, route_id));
[email protected]bafe6cd2012-05-23 23:09:503110 if (iter == pending_widget_views_.end()) {
3111 DCHECK(false);
alexmosc2a8cec2016-05-23 22:19:533112 return nullptr;
[email protected]bafe6cd2012-05-23 23:09:503113 }
3114
3115 RenderWidgetHostView* widget_host_view = iter->second;
Lukasz Anforowicz09060bdf72018-08-23 15:53:173116 pending_widget_views_.erase(GlobalRoutingID(process_id, route_id));
[email protected]bafe6cd2012-05-23 23:09:503117
3118 RenderWidgetHost* widget_host = widget_host_view->GetRenderWidgetHost();
Lukasz Anforowicz5510ed652018-06-06 16:16:193119 if (!widget_host->GetProcess()->IsInitializedAndNotDead()) {
[email protected]bafe6cd2012-05-23 23:09:503120 // The view has gone away or the renderer crashed. Nothing to do.
alexmosc2a8cec2016-05-23 22:19:533121 return nullptr;
[email protected]bafe6cd2012-05-23 23:09:503122 }
3123
3124 return widget_host_view;
3125}
3126
[email protected]f13b4202012-06-12 23:53:233127void WebContentsImpl::RequestMediaAccessPermission(
[email protected]33662e52013-01-07 21:31:093128 const MediaStreamRequest& request,
Mark Pilgrim57499082018-06-12 12:38:303129 MediaResponseCallback callback) {
[email protected]d19b84b2014-03-14 11:52:373130 if (delegate_) {
Mark Pilgrim57499082018-06-12 12:38:303131 delegate_->RequestMediaAccessPermission(this, request, std::move(callback));
[email protected]d19b84b2014-03-14 11:52:373132 } else {
Antonio Gomes0d42960a2019-06-05 12:35:513133 std::move(callback).Run(
3134 blink::MediaStreamDevices(),
3135 blink::mojom::MediaStreamRequestResult::FAILED_DUE_TO_SHUTDOWN,
3136 std::unique_ptr<MediaStreamUI>());
[email protected]d19b84b2014-03-14 11:52:373137 }
[email protected]f13b4202012-06-12 23:53:233138}
3139
Chandan Padhia4b8bcb72017-09-12 16:41:043140bool WebContentsImpl::CheckMediaAccessPermission(
Raymes Khouryad7c24a12018-03-05 23:22:583141 RenderFrameHost* render_frame_host,
Chandan Padhia4b8bcb72017-09-12 16:41:043142 const url::Origin& security_origin,
Antonio Gomesc8b734b2019-06-05 18:22:163143 blink::mojom::MediaStreamType type) {
3144 DCHECK(type == blink::mojom::MediaStreamType::DEVICE_AUDIO_CAPTURE ||
3145 type == blink::mojom::MediaStreamType::DEVICE_VIDEO_CAPTURE);
Chandan Padhia4b8bcb72017-09-12 16:41:043146 return delegate_ && delegate_->CheckMediaAccessPermission(
Raymes Khouryad7c24a12018-03-05 23:22:583147 render_frame_host, security_origin.GetURL(), type);
grunell657d4d82014-09-18 00:09:433148}
3149
Guido Urdaneta73fa6632019-01-14 18:46:263150std::string WebContentsImpl::GetDefaultMediaDeviceID(
Antonio Gomesc8b734b2019-06-05 18:22:163151 blink::mojom::MediaStreamType type) {
guidou8a440b842017-01-30 13:58:433152 if (!delegate_)
3153 return std::string();
3154 return delegate_->GetDefaultMediaDeviceID(this, type);
3155}
3156
[email protected]cc9200432013-07-23 23:02:403157SessionStorageNamespace* WebContentsImpl::GetSessionStorageNamespace(
3158 SiteInstance* instance) {
3159 return controller_.GetSessionStorageNamespace(instance);
3160}
3161
[email protected]6de7fc482014-06-06 10:46:443162SessionStorageNamespaceMap WebContentsImpl::GetSessionStorageNamespaceMap() {
3163 return controller_.GetSessionStorageNamespaceMap();
3164}
3165
[email protected]9b159a52013-10-03 17:24:553166FrameTree* WebContentsImpl::GetFrameTree() {
3167 return &frame_tree_;
3168}
3169
aelias5252baa2016-04-10 01:18:023170bool WebContentsImpl::IsOverridingUserAgent() {
3171 return GetController().GetVisibleEntry() &&
3172 GetController().GetVisibleEntry()->GetIsOverridingUserAgent();
3173}
3174
avi85ee8362016-10-08 02:09:083175bool WebContentsImpl::IsJavaScriptDialogShowing() const {
3176 return is_showing_javascript_dialog_;
3177}
3178
avic031d392017-03-03 03:09:423179bool WebContentsImpl::ShouldIgnoreUnresponsiveRenderer() {
3180 // Ignore unresponsive renderers if the debugger is attached to them since the
3181 // unresponsiveness might be a result of the renderer sitting on a breakpoint.
3182 //
David Bienvenu525f8a02019-02-15 02:02:423183#ifdef OS_WIN
3184 // Check if a windows debugger is attached to the renderer process.
3185 base::ProcessHandle process_handle =
3186 GetMainFrame()->GetProcess()->GetProcess().Handle();
3187 BOOL debugger_present = FALSE;
3188 if (CheckRemoteDebuggerPresent(process_handle, &debugger_present) &&
3189 debugger_present)
3190 return true;
3191#endif // OS_WIN
3192
avic031d392017-03-03 03:09:423193 // TODO(pfeldman): Fix this to only return true if the renderer is *actually*
3194 // sitting on a breakpoint. https://crbug.com/684202
3195 return DevToolsAgentHost::IsDebuggerAttached(this);
3196}
3197
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:413198ui::AXMode WebContentsImpl::GetAccessibilityMode() {
[email protected]95640212014-07-26 18:14:303199 return accessibility_mode_;
3200}
3201
Dominic Mazzonia7b0edb22017-08-09 16:32:513202void WebContentsImpl::AccessibilityEventReceived(
Dominic Mazzoniccbaa9b2018-06-06 07:44:233203 const AXEventNotificationDetails& details) {
ericwilligersde386342016-10-19 02:35:093204 for (auto& observer : observers_)
Dominic Mazzonia7b0edb22017-08-09 16:32:513205 observer.AccessibilityEventReceived(details);
[email protected]31a71eaf2014-03-13 01:47:363206}
3207
dmazzoni2400c462016-08-23 15:07:133208void WebContentsImpl::AccessibilityLocationChangesReceived(
3209 const std::vector<AXLocationChangeNotificationDetails>& details) {
ericwilligersde386342016-10-19 02:35:093210 for (auto& observer : observers_)
Dominic Mazzonia7b0edb22017-08-09 16:32:513211 observer.AccessibilityLocationChangesReceived(details);
dmazzoni2400c462016-08-23 15:07:133212}
3213
James Wallace-Leeeafc94cb92018-07-23 21:35:093214base::string16 WebContentsImpl::DumpAccessibilityTree(bool internal) {
3215 auto* ax_mgr = GetOrCreateRootBrowserAccessibilityManager();
3216 DCHECK(ax_mgr);
3217 return AccessibilityTreeFormatter::DumpAccessibilityTreeFromManager(ax_mgr,
3218 internal);
3219}
3220
dmazzonia656928e2014-09-15 20:28:543221RenderFrameHost* WebContentsImpl::GetGuestByInstanceID(
fsamuel833ee7c2015-02-13 23:40:403222 RenderFrameHost* render_frame_host,
dmazzonia656928e2014-09-15 20:28:543223 int browser_plugin_instance_id) {
3224 BrowserPluginGuestManager* guest_manager =
3225 GetBrowserContext()->GetGuestManager();
fsamuel9cbc00a2015-04-17 20:30:203226 if (!guest_manager)
3227 return nullptr;
3228
dmazzonia656928e2014-09-15 20:28:543229 WebContents* guest = guest_manager->GetGuestByInstanceID(
fsamuel2e9413d2015-02-25 01:25:443230 render_frame_host->GetProcess()->GetID(), browser_plugin_instance_id);
dmazzonia656928e2014-09-15 20:28:543231 if (!guest)
fsamuel9cbc00a2015-04-17 20:30:203232 return nullptr;
3233
dmazzonia656928e2014-09-15 20:28:543234 return guest->GetMainFrame();
3235}
3236
Ke He7319dbe2017-11-09 05:54:443237device::mojom::GeolocationContext* WebContentsImpl::GetGeolocationContext() {
3238 if (geolocation_context_)
3239 return geolocation_context_.get();
3240
3241 auto request = mojo::MakeRequest(&geolocation_context_);
Ken Rockotfd03e682019-06-20 21:13:543242 service_manager::Connector* connector = GetSystemConnector();
3243 if (connector)
3244 connector->BindInterface(device::mojom::kServiceName, std::move(request));
Conley Owens6894c4f2017-07-10 19:29:133245 return geolocation_context_.get();
blundellc57b93f2014-10-29 13:19:573246}
3247
ke.he98b761e2017-05-09 05:59:173248device::mojom::WakeLockContext* WebContentsImpl::GetWakeLockContext() {
blundelld8cd72b2017-03-28 07:18:383249 if (!wake_lock_context_host_)
3250 wake_lock_context_host_.reset(new WakeLockContextHost(this));
blundelle75a8f92017-03-27 08:11:173251 return wake_lock_context_host_->GetWakeLockContext();
alogvinovf50445a2015-10-30 13:00:123252}
3253
blundellf5316fc2017-05-15 11:49:033254#if defined(OS_ANDROID)
Han Leonf6a2e5f2017-05-23 13:40:473255void WebContentsImpl::GetNFC(device::mojom::NFCRequest request) {
blundellf5316fc2017-05-15 11:49:033256 if (!nfc_host_)
3257 nfc_host_.reset(new NFCHost(this));
3258 nfc_host_->GetNFC(std::move(request));
3259}
3260#endif
3261
Evan Stade5e750ad02017-08-07 21:59:273262void WebContentsImpl::SetNotWaitingForResponse() {
3263 if (waiting_for_response_ == false)
3264 return;
3265
3266 waiting_for_response_ = false;
3267 if (delegate_)
3268 delegate_->LoadingStateChanged(this, is_load_to_different_document_);
Chris Hamiltonf82f2a702018-02-15 19:37:293269 for (auto& observer : observers_)
3270 observer.DidReceiveResponse();
Evan Stade5e750ad02017-08-07 21:59:273271}
3272
lfgbb9c28a2016-03-01 03:19:493273void WebContentsImpl::SendScreenRects() {
kenrbc52e1b4e2016-03-20 01:03:373274 for (FrameTreeNode* node : frame_tree_.Nodes()) {
kenrb0e8dc202016-05-30 19:51:403275 if (node->current_frame_host()->is_local_root())
kenrbc52e1b4e2016-03-20 01:03:373276 node->current_frame_host()->GetRenderWidgetHost()->SendScreenRects();
3277 }
lfgbb9c28a2016-03-01 03:19:493278
danakj4df8a9d02019-02-05 20:36:593279 // Interstitials have their own |frame_tree_|.
3280 if (interstitial_page_) {
3281 FrameTree* interstitial_frame_tree = interstitial_page_->GetFrameTree();
3282 for (FrameTreeNode* node : interstitial_frame_tree->Nodes()) {
3283 if (node->current_frame_host()->is_local_root())
3284 node->current_frame_host()->GetRenderWidgetHost()->SendScreenRects();
3285 }
lfgdb5c4ed2016-03-04 23:09:073286 }
3287
avallee9f43b012016-11-26 07:02:103288 if (browser_plugin_embedder_ && !is_being_destroyed_)
[email protected]a7568e62013-06-14 07:50:443289 browser_plugin_embedder_->DidSendScreenRects();
[email protected]32deec62013-05-15 23:55:043290}
3291
ekaramadadd882292016-06-08 15:22:563292TextInputManager* WebContentsImpl::GetTextInputManager() {
3293 if (GetOuterWebContents())
3294 return GetOuterWebContents()->GetTextInputManager();
3295
Ehsan Karamadc179dc02018-12-15 03:09:293296 if (!text_input_manager_ && !browser_plugin_guest_) {
Darren Shenca53d5f2018-05-15 04:56:013297 text_input_manager_.reset(new TextInputManager(
3298 GetBrowserContext() &&
3299 !GetBrowserContext()->IsOffTheRecord()) /* should_do_learning */);
3300 }
ekaramadadd882292016-06-08 15:22:563301
3302 return text_input_manager_.get();
3303}
3304
paulmeyer7f6f1d4f2016-11-15 00:00:273305bool WebContentsImpl::OnUpdateDragCursor() {
Lei Zhang6410ec1a2019-01-31 21:38:583306 return browser_plugin_embedder_ &&
3307 browser_plugin_embedder_->OnUpdateDragCursor();
paulmeyer7f6f1d4f2016-11-15 00:00:273308}
3309
kenrb11f213a2017-03-24 18:12:063310bool WebContentsImpl::IsWidgetForMainFrame(
3311 RenderWidgetHostImpl* render_widget_host) {
3312 return render_widget_host == GetMainFrame()->GetRenderWidgetHost();
3313}
3314
[email protected]95640212014-07-26 18:14:303315BrowserAccessibilityManager*
3316 WebContentsImpl::GetRootBrowserAccessibilityManager() {
Dominic Mazzoniefb5f9e32017-12-21 01:08:223317 RenderFrameHostImpl* rfh = static_cast<RenderFrameHostImpl*>(
3318 ShowingInterstitialPage() ? GetInterstitialPage()->GetMainFrame()
3319 : GetMainFrame());
creisc014b402015-04-23 16:41:453320 return rfh ? rfh->browser_accessibility_manager() : nullptr;
[email protected]95640212014-07-26 18:14:303321}
3322
3323BrowserAccessibilityManager*
3324 WebContentsImpl::GetOrCreateRootBrowserAccessibilityManager() {
Dominic Mazzoniefb5f9e32017-12-21 01:08:223325 RenderFrameHostImpl* rfh = static_cast<RenderFrameHostImpl*>(
3326 ShowingInterstitialPage() ? GetInterstitialPage()->GetMainFrame()
3327 : GetMainFrame());
creisc014b402015-04-23 16:41:453328 return rfh ? rfh->GetOrCreateBrowserAccessibilityManager() : nullptr;
[email protected]95640212014-07-26 18:14:303329}
3330
Dave Tapuskab336b922017-07-06 19:24:053331void WebContentsImpl::ExecuteEditCommand(
3332 const std::string& command,
3333 const base::Optional<base::string16>& value) {
Dave Tapuskae782bea2019-07-09 16:21:513334 auto* input_handler = GetFocusedFrameInputHandler();
3335 if (!input_handler)
Dave Tapuskab336b922017-07-06 19:24:053336 return;
3337
Dave Tapuskae782bea2019-07-09 16:21:513338 input_handler->ExecuteEditCommand(command, value);
Dave Tapuskab336b922017-07-06 19:24:053339}
3340
mohsen7ab1ec16ec2015-07-02 18:26:233341void WebContentsImpl::MoveRangeSelectionExtent(const gfx::Point& extent) {
Dave Tapuskae782bea2019-07-09 16:21:513342 auto* input_handler = GetFocusedFrameInputHandler();
3343 if (!input_handler)
mohsen7ab1ec16ec2015-07-02 18:26:233344 return;
3345
Dave Tapuskae782bea2019-07-09 16:21:513346 input_handler->MoveRangeSelectionExtent(extent);
mohsen7ab1ec16ec2015-07-02 18:26:233347}
3348
3349void WebContentsImpl::SelectRange(const gfx::Point& base,
3350 const gfx::Point& extent) {
Dave Tapuskae782bea2019-07-09 16:21:513351 auto* input_handler = GetFocusedFrameInputHandler();
3352 if (!input_handler)
mohsen7ab1ec16ec2015-07-02 18:26:233353 return;
3354
Dave Tapuskae782bea2019-07-09 16:21:513355 input_handler->SelectRange(base, extent);
mohsen7ab1ec16ec2015-07-02 18:26:233356}
3357
Dave Tapuskab336b922017-07-06 19:24:053358void WebContentsImpl::MoveCaret(const gfx::Point& extent) {
Dave Tapuskae782bea2019-07-09 16:21:513359 auto* input_handler = GetFocusedFrameInputHandler();
3360 if (!input_handler)
Dave Tapuskab336b922017-07-06 19:24:053361 return;
3362
Dave Tapuskae782bea2019-07-09 16:21:513363 input_handler->MoveCaret(extent);
Dave Tapuskab336b922017-07-06 19:24:053364}
3365
Shimi Zhang37deeb22017-09-28 00:59:013366void WebContentsImpl::AdjustSelectionByCharacterOffset(
3367 int start_adjust,
3368 int end_adjust,
3369 bool show_selection_menu) {
Dave Tapuskae782bea2019-07-09 16:21:513370 auto* input_handler = GetFocusedFrameInputHandler();
3371 if (!input_handler)
aurimasab0319022015-07-10 21:57:383372 return;
3373
Shimi Zhang37deeb22017-09-28 00:59:013374 using blink::mojom::SelectionMenuBehavior;
Dave Tapuskae782bea2019-07-09 16:21:513375 input_handler->AdjustSelectionByCharacterOffset(
Shimi Zhang37deeb22017-09-28 00:59:013376 start_adjust, end_adjust,
3377 show_selection_menu ? SelectionMenuBehavior::kShow
3378 : SelectionMenuBehavior::kHide);
aurimasab0319022015-07-10 21:57:383379}
3380
[email protected]b172aee2012-04-10 17:05:263381void WebContentsImpl::UpdatePreferredSize(const gfx::Size& pref_size) {
[email protected]222f5822014-02-05 23:40:493382 const gfx::Size old_size = GetPreferredSize();
[email protected]bcd2815602012-01-14 18:17:233383 preferred_size_ = pref_size;
[email protected]222f5822014-02-05 23:40:493384 OnPreferredSizeChanged(old_size);
[email protected]0548c5352011-09-07 00:33:333385}
3386
avic3aa8422015-11-09 20:57:223387void WebContentsImpl::ResizeDueToAutoResize(
3388 RenderWidgetHostImpl* render_widget_host,
Fady Samuel97947112018-05-05 16:24:543389 const gfx::Size& new_size) {
avic3aa8422015-11-09 20:57:223390 if (render_widget_host != GetRenderViewHost()->GetWidget())
3391 return;
3392
kenrbe4f91082017-06-02 20:32:083393 auto_resize_size_ = new_size;
3394
3395 // Out-of-process iframe visible viewport sizes usually come from the
3396 // top-level RenderWidgetHostView, but when auto-resize is enabled on the
3397 // top frame then that size is used instead.
3398 for (FrameTreeNode* node : frame_tree_.Nodes()) {
3399 if (node->current_frame_host()->is_local_root()) {
3400 RenderWidgetHostImpl* host =
3401 node->current_frame_host()->GetRenderWidgetHost();
3402 if (host != render_widget_host)
Fady Samuel0b911822018-04-25 13:22:163403 host->SynchronizeVisualProperties();
kenrbe4f91082017-06-02 20:32:083404 }
3405 }
3406
[email protected]61e2b3cc2012-03-02 16:13:343407 if (delegate_)
3408 delegate_->ResizeDueToAutoResize(this, new_size);
3409}
3410
kenrbe4f91082017-06-02 20:32:083411gfx::Size WebContentsImpl::GetAutoResizeSize() {
3412 return auto_resize_size_;
3413}
3414
3415void WebContentsImpl::ResetAutoResizeSize() {
3416 auto_resize_size_ = gfx::Size();
3417}
3418
Albert J. Wongeb28e7e2018-10-12 00:57:433419InputEventShim* WebContentsImpl::GetInputEventShim() const {
3420 // The only thing that intercepts text input and mouse lock is the
3421 // BrowserPluginGuest. Delegate to the BrowserPluginGuest logic for
3422 // whether or not it needs to shim these events.
3423 if (browser_plugin_guest_) {
3424 return browser_plugin_guest_->GetInputEventShim();
3425 }
3426 return nullptr;
3427}
3428
[email protected]b172aee2012-04-10 17:05:263429WebContents* WebContentsImpl::OpenURL(const OpenURLParams& params) {
Bo Liu300c6052018-06-12 04:46:073430 if (!delegate_) {
3431 // Embedder can delay setting a delegate on new WebContents with
3432 // WebContentsDelegate::ShouldResumeRequestsForCreatedWindow. In the mean
3433 // time, navigations, including the initial one, that goes through OpenURL
3434 // should be delayed until embedder is ready to resume loading.
3435 delayed_open_url_params_ = std::make_unique<OpenURLParams>(params);
Nasko Oskov83a8cf92018-10-19 14:58:563436
3437 // If there was a navigation deferred when creating the window through
3438 // CreateNewWindow, drop it in favor of this navigation.
3439 delayed_load_url_params_.reset();
3440
Ivan Kotenkov2c0d2bb32017-11-01 15:41:283441 return nullptr;
Bo Liu300c6052018-06-12 04:46:073442 }
[email protected]00c37fc2011-08-02 00:22:503443
[email protected]e5d549d2011-12-28 01:29:203444 WebContents* new_contents = delegate_->OpenURLFromTab(this, params);
pnoland488944652017-02-22 18:58:543445
3446 RenderFrameHost* source_render_frame_host = RenderFrameHost::FromID(
3447 params.source_render_process_id, params.source_render_frame_id);
3448
3449 if (source_render_frame_host && params.source_site_instance) {
3450 CHECK_EQ(source_render_frame_host->GetSiteInstance(),
3451 params.source_site_instance.get());
3452 }
3453
3454 if (new_contents && source_render_frame_host && new_contents != this) {
3455 for (auto& observer : observers_) {
Lukasz Anforowicz48c9f872018-08-01 00:58:413456 observer.DidOpenRequestedURL(
3457 new_contents, source_render_frame_host, params.url, params.referrer,
3458 params.disposition, params.transition,
3459 params.started_from_context_menu, params.is_renderer_initiated);
pnoland488944652017-02-22 18:58:543460 }
3461 }
3462
[email protected]e5d549d2011-12-28 01:29:203463 return new_contents;
[email protected]d5f942ba2008-09-26 19:30:343464}
3465
[email protected]a86c0e962013-12-17 17:10:393466void WebContentsImpl::RenderFrameForInterstitialPageCreated(
3467 RenderFrameHost* render_frame_host) {
ericwilligersde386342016-10-19 02:35:093468 for (auto& observer : observers_)
3469 observer.RenderFrameForInterstitialPageCreated(render_frame_host);
[email protected]ba45bfd2012-05-22 21:51:443470}
3471
[email protected]20ca0382013-02-28 19:50:073472void WebContentsImpl::AttachInterstitialPage(
3473 InterstitialPageImpl* interstitial_page) {
creisadca9862017-06-16 19:12:113474 DCHECK(!interstitial_page_ && interstitial_page);
3475 interstitial_page_ = interstitial_page;
[email protected]90fb08ed2013-09-24 17:43:293476
3477 // Cancel any visible dialogs so that they don't interfere with the
3478 // interstitial.
creis89a0f782015-05-27 16:13:173479 CancelActiveAndPendingDialogs();
[email protected]90fb08ed2013-09-24 17:43:293480
ericwilligersde386342016-10-19 02:35:093481 for (auto& observer : observers_)
3482 observer.DidAttachInterstitialPage();
clamy44e84ce2016-02-22 15:38:253483
3484 // Stop the throbber if needed while the interstitial page is shown.
3485 if (frame_tree_.IsLoading())
3486 LoadingStateChanged(true, true, nullptr);
lfg1453e412017-04-11 00:48:503487
3488 // Connect to outer WebContents if necessary.
3489 if (node_.OuterContentsFrameTreeNode()) {
3490 if (GetRenderManager()->GetProxyToOuterDelegate()) {
3491 DCHECK(
3492 static_cast<RenderWidgetHostViewBase*>(interstitial_page->GetView())
3493 ->IsRenderWidgetHostViewChildFrame());
3494 RenderWidgetHostViewChildFrame* view =
3495 static_cast<RenderWidgetHostViewChildFrame*>(
3496 interstitial_page->GetView());
3497 GetRenderManager()->SetRWHViewForInnerContents(view);
3498 }
3499 }
Bo Liu7c6779e92017-08-16 02:02:283500
Bo Liu168c8642017-08-28 18:26:023501#if defined(OS_ANDROID)
Bo Liu7c6779e92017-08-16 02:02:283502 // Update importance of the interstitial.
3503 static_cast<RenderFrameHostImpl*>(interstitial_page_->GetMainFrame())
3504 ->GetRenderWidgetHost()
3505 ->SetImportance(GetMainFrame()->GetRenderWidgetHost()->importance());
Bo Liu168c8642017-08-28 18:26:023506#endif
Dominic Mazzoniefb5f9e32017-12-21 01:08:223507
3508 if (accessibility_mode_.has_mode(ui::AXMode::kNativeAPIs)) {
3509 // Make sure that the main page's accessibility tree is hidden and the
3510 // interstitial gets focus.
3511 RenderFrameHostImpl* rfh =
3512 static_cast<RenderFrameHostImpl*>(GetMainFrame());
3513 if (rfh) {
3514 BrowserAccessibilityManager* accessibility_manager =
3515 rfh->browser_accessibility_manager();
3516 if (accessibility_manager)
3517 accessibility_manager->set_hidden_by_interstitial_page(true);
3518 }
3519 rfh = static_cast<RenderFrameHostImpl*>(
3520 GetInterstitialPage()->GetMainFrame());
3521 if (rfh) {
3522 BrowserAccessibilityManager* accessibility_manager =
3523 rfh->GetOrCreateBrowserAccessibilityManager();
3524 if (accessibility_manager)
3525 accessibility_manager->OnWindowFocused();
3526 }
3527 }
clamy44e84ce2016-02-22 15:38:253528}
3529
3530void WebContentsImpl::DidProceedOnInterstitial() {
3531 // The interstitial page should no longer be pausing the throbber.
creisadca9862017-06-16 19:12:113532 DCHECK(!(ShowingInterstitialPage() && interstitial_page_->pause_throbber()));
clamy44e84ce2016-02-22 15:38:253533
3534 // Restart the throbber now that the interstitial page no longer pauses it.
3535 if (ShowingInterstitialPage() && frame_tree_.IsLoading())
3536 LoadingStateChanged(true, true, nullptr);
[email protected]20ca0382013-02-28 19:50:073537}
3538
Shivani Sharma985474f2019-05-23 21:36:473539bool WebContentsImpl::HadInnerWebContents() {
3540 return had_inner_webcontents_;
3541}
3542
Lucas Furukawa Gadani7f1241d2017-07-12 01:45:043543void WebContentsImpl::DetachInterstitialPage(bool has_focus) {
Lucas Furukawa Gadani69b609192017-07-10 17:08:343544 bool interstitial_pausing_throbber =
3545 ShowingInterstitialPage() && interstitial_page_->pause_throbber();
3546 if (ShowingInterstitialPage())
3547 interstitial_page_ = nullptr;
Lucas Furukawa Gadani7f1241d2017-07-12 01:45:043548
Dominic Mazzoniefb5f9e32017-12-21 01:08:223549 // Make sure that the main page's accessibility tree is no longer
3550 // suppressed.
3551 RenderFrameHostImpl* rfh = GetMainFrame();
3552 if (rfh) {
3553 BrowserAccessibilityManager* accessibility_manager =
3554 rfh->browser_accessibility_manager();
3555 if (accessibility_manager)
3556 accessibility_manager->set_hidden_by_interstitial_page(false);
3557 }
3558
Lucas Furukawa Gadani7f1241d2017-07-12 01:45:043559 // If the focus was on the interstitial, let's keep it to the page.
3560 // (Note that in unit-tests the RVH may not have a view).
3561 if (has_focus && GetRenderViewHost()->GetWidget()->GetView())
3562 GetRenderViewHost()->GetWidget()->GetView()->Focus();
3563
Lucas Furukawa Gadani69b609192017-07-10 17:08:343564 for (auto& observer : observers_)
3565 observer.DidDetachInterstitialPage();
3566
3567 // Disconnect from outer WebContents if necessary. This must happen after the
3568 // interstitial page is cleared above, since the call to
3569 // SetRWHViewForInnerContents below may loop over all the
3570 // RenderWidgetHostViews in the tree (otherwise, including the now-deleted
3571 // view for the interstitial).
lfg1453e412017-04-11 00:48:503572 if (node_.OuterContentsFrameTreeNode()) {
3573 if (GetRenderManager()->GetProxyToOuterDelegate()) {
3574 DCHECK(static_cast<RenderWidgetHostViewBase*>(
3575 GetRenderManager()->current_frame_host()->GetView())
3576 ->IsRenderWidgetHostViewChildFrame());
3577 RenderWidgetHostViewChildFrame* view =
3578 static_cast<RenderWidgetHostViewChildFrame*>(
3579 GetRenderManager()->current_frame_host()->GetView());
3580 GetRenderManager()->SetRWHViewForInnerContents(view);
3581 }
3582 }
3583
clamy44e84ce2016-02-22 15:38:253584 // Restart the throbber if needed now that the interstitial page is going
3585 // away.
3586 if (interstitial_pausing_throbber && frame_tree_.IsLoading())
3587 LoadingStateChanged(true, true, nullptr);
[email protected]20ca0382013-02-28 19:50:073588}
3589
avi2b177592014-12-10 02:08:023590void WebContentsImpl::SetHistoryOffsetAndLength(int history_offset,
3591 int history_length) {
alexmos136fd6e62016-08-15 20:58:413592 SendPageMessage(new PageMsg_SetHistoryOffsetAndLength(
3593 MSG_ROUTING_NONE, history_offset, history_length));
avi2b177592014-12-10 02:08:023594}
3595
3596void WebContentsImpl::SetHistoryOffsetAndLengthForView(
3597 RenderViewHost* render_view_host,
3598 int history_offset,
3599 int history_length) {
alexmos136fd6e62016-08-15 20:58:413600 render_view_host->Send(new PageMsg_SetHistoryOffsetAndLength(
avi2b177592014-12-10 02:08:023601 render_view_host->GetRoutingID(), history_offset, history_length));
[email protected]796931a92011-08-10 01:32:143602}
3603
toyoshim24a4c7ab2016-04-05 09:24:143604void WebContentsImpl::ReloadFocusedFrame(bool bypass_cache) {
[email protected]1f3fc1d2014-04-03 14:50:173605 RenderFrameHost* focused_frame = GetFocusedFrame();
3606 if (!focused_frame)
3607 return;
3608
[email protected]959be4c2014-04-08 15:01:333609 focused_frame->Send(new FrameMsg_Reload(
toyoshim24a4c7ab2016-04-05 09:24:143610 focused_frame->GetRoutingID(), bypass_cache));
[email protected]959be4c2014-04-08 15:01:333611}
3612
megjablon3f5941072016-02-04 23:27:523613void WebContentsImpl::ReloadLoFiImages() {
3614 SendToAllFrames(new FrameMsg_ReloadLoFiImages(MSG_ROUTING_NONE));
Robert Ogden34e97962018-09-14 23:50:273615 for (auto& observer : observers_)
3616 observer.DidReloadLoFiImages();
megjablon3f5941072016-02-04 23:27:523617}
3618
Ryan Sturmc65dbe42018-05-31 20:37:043619std::vector<blink::mojom::PauseSubresourceLoadingHandlePtr>
3620WebContentsImpl::PauseSubresourceLoading() {
3621 std::vector<blink::mojom::PauseSubresourceLoadingHandlePtr> handles;
3622 for (RenderFrameHost* rfh : GetAllFrames()) {
3623 if (!rfh->IsRenderFrameLive())
3624 continue;
3625 handles.push_back(rfh->PauseSubresourceLoading());
3626 }
3627 return handles;
3628}
3629
[email protected]959be4c2014-04-08 15:01:333630void WebContentsImpl::Undo() {
Dave Tapuskae782bea2019-07-09 16:21:513631 auto* input_handler = GetFocusedFrameInputHandler();
3632 if (!input_handler)
[email protected]959be4c2014-04-08 15:01:333633 return;
3634
Dave Tapuskae782bea2019-07-09 16:21:513635 input_handler->Undo();
[email protected]959be4c2014-04-08 15:01:333636 RecordAction(base::UserMetricsAction("Undo"));
3637}
3638
3639void WebContentsImpl::Redo() {
Dave Tapuskae782bea2019-07-09 16:21:513640 auto* input_handler = GetFocusedFrameInputHandler();
3641 if (!input_handler)
[email protected]959be4c2014-04-08 15:01:333642 return;
Dave Tapuskae782bea2019-07-09 16:21:513643
3644 input_handler->Redo();
[email protected]959be4c2014-04-08 15:01:333645 RecordAction(base::UserMetricsAction("Redo"));
3646}
3647
3648void WebContentsImpl::Cut() {
Dave Tapuskae782bea2019-07-09 16:21:513649 auto* input_handler = GetFocusedFrameInputHandler();
3650 if (!input_handler)
[email protected]959be4c2014-04-08 15:01:333651 return;
3652
Dave Tapuskae782bea2019-07-09 16:21:513653 input_handler->Cut();
[email protected]959be4c2014-04-08 15:01:333654 RecordAction(base::UserMetricsAction("Cut"));
3655}
3656
3657void WebContentsImpl::Copy() {
Dave Tapuskae782bea2019-07-09 16:21:513658 auto* input_handler = GetFocusedFrameInputHandler();
3659 if (!input_handler)
[email protected]959be4c2014-04-08 15:01:333660 return;
3661
Dave Tapuskae782bea2019-07-09 16:21:513662 input_handler->Copy();
[email protected]959be4c2014-04-08 15:01:333663 RecordAction(base::UserMetricsAction("Copy"));
3664}
3665
3666void WebContentsImpl::CopyToFindPboard() {
3667#if defined(OS_MACOSX)
Dave Tapuskae782bea2019-07-09 16:21:513668 auto* input_handler = GetFocusedFrameInputHandler();
3669 if (!input_handler)
[email protected]959be4c2014-04-08 15:01:333670 return;
3671
3672 // Windows/Linux don't have the concept of a find pasteboard.
Dave Tapuskae782bea2019-07-09 16:21:513673 input_handler->CopyToFindPboard();
[email protected]959be4c2014-04-08 15:01:333674 RecordAction(base::UserMetricsAction("CopyToFindPboard"));
3675#endif
3676}
3677
3678void WebContentsImpl::Paste() {
Dave Tapuskae782bea2019-07-09 16:21:513679 auto* input_handler = GetFocusedFrameInputHandler();
3680 if (!input_handler)
[email protected]959be4c2014-04-08 15:01:333681 return;
3682
Dave Tapuskae782bea2019-07-09 16:21:513683 input_handler->Paste();
Archana Simhaf67090c2019-05-06 23:43:233684 for (auto& observer : observers_)
3685 observer.OnPaste();
[email protected]959be4c2014-04-08 15:01:333686 RecordAction(base::UserMetricsAction("Paste"));
3687}
3688
3689void WebContentsImpl::PasteAndMatchStyle() {
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->PasteAndMatchStyle();
Archana Simhaf67090c2019-05-06 23:43:233695 for (auto& observer : observers_)
3696 observer.OnPaste();
[email protected]959be4c2014-04-08 15:01:333697 RecordAction(base::UserMetricsAction("PasteAndMatchStyle"));
3698}
3699
3700void WebContentsImpl::Delete() {
Dave Tapuskae782bea2019-07-09 16:21:513701 auto* input_handler = GetFocusedFrameInputHandler();
3702 if (!input_handler)
[email protected]959be4c2014-04-08 15:01:333703 return;
3704
Dave Tapuskae782bea2019-07-09 16:21:513705 input_handler->Delete();
[email protected]959be4c2014-04-08 15:01:333706 RecordAction(base::UserMetricsAction("DeleteSelection"));
3707}
3708
3709void WebContentsImpl::SelectAll() {
Dave Tapuskae782bea2019-07-09 16:21:513710 auto* input_handler = GetFocusedFrameInputHandler();
3711 if (!input_handler)
[email protected]959be4c2014-04-08 15:01:333712 return;
3713
Dave Tapuskae782bea2019-07-09 16:21:513714 input_handler->SelectAll();
[email protected]959be4c2014-04-08 15:01:333715 RecordAction(base::UserMetricsAction("SelectAll"));
3716}
3717
yabinh351e7ec2017-03-10 02:43:243718void WebContentsImpl::CollapseSelection() {
Dave Tapuskae782bea2019-07-09 16:21:513719 auto* input_handler = GetFocusedFrameInputHandler();
3720 if (!input_handler)
[email protected]959be4c2014-04-08 15:01:333721 return;
3722
Dave Tapuskae782bea2019-07-09 16:21:513723 input_handler->CollapseSelection();
[email protected]1f3fc1d2014-04-03 14:50:173724}
3725
3726void WebContentsImpl::Replace(const base::string16& word) {
Dave Tapuskae782bea2019-07-09 16:21:513727 auto* input_handler = GetFocusedFrameInputHandler();
3728 if (!input_handler)
[email protected]1f3fc1d2014-04-03 14:50:173729 return;
3730
Dave Tapuskae782bea2019-07-09 16:21:513731 input_handler->Replace(word);
[email protected]1f3fc1d2014-04-03 14:50:173732}
3733
3734void WebContentsImpl::ReplaceMisspelling(const base::string16& word) {
Dave Tapuskae782bea2019-07-09 16:21:513735 auto* input_handler = GetFocusedFrameInputHandler();
3736 if (!input_handler)
[email protected]1f3fc1d2014-04-03 14:50:173737 return;
3738
Dave Tapuskae782bea2019-07-09 16:21:513739 input_handler->ReplaceMisspelling(word);
[email protected]959be4c2014-04-08 15:01:333740}
3741
3742void WebContentsImpl::NotifyContextMenuClosed(
3743 const CustomContextMenuContext& context) {
3744 RenderFrameHost* focused_frame = GetFocusedFrame();
3745 if (!focused_frame)
3746 return;
3747
3748 focused_frame->Send(new FrameMsg_ContextMenuClosed(
3749 focused_frame->GetRoutingID(), context));
3750}
3751
3752void WebContentsImpl::ExecuteCustomContextMenuCommand(
3753 int action, const CustomContextMenuContext& context) {
3754 RenderFrameHost* focused_frame = GetFocusedFrame();
3755 if (!focused_frame)
3756 return;
3757
3758 focused_frame->Send(new FrameMsg_CustomContextMenuAction(
3759 focused_frame->GetRoutingID(), context, action));
[email protected]4fed3702014-04-01 09:08:003760}
3761
[email protected]fc2b46b2014-05-03 16:33:453762gfx::NativeView WebContentsImpl::GetNativeView() {
3763 return view_->GetNativeView();
3764}
3765
3766gfx::NativeView WebContentsImpl::GetContentNativeView() {
3767 return view_->GetContentNativeView();
3768}
3769
3770gfx::NativeWindow WebContentsImpl::GetTopLevelNativeWindow() {
3771 return view_->GetTopLevelNativeWindow();
3772}
3773
3774gfx::Rect WebContentsImpl::GetViewBounds() {
3775 return view_->GetViewBounds();
3776}
3777
3778gfx::Rect WebContentsImpl::GetContainerBounds() {
3779 gfx::Rect rv;
3780 view_->GetContainerBounds(&rv);
3781 return rv;
3782}
3783
3784DropData* WebContentsImpl::GetDropData() {
3785 return view_->GetDropData();
3786}
3787
3788void WebContentsImpl::Focus() {
miu9e14e492014-10-25 02:39:043789 view_->Focus();
[email protected]fc2b46b2014-05-03 16:33:453790}
3791
3792void WebContentsImpl::SetInitialFocus() {
miu9e14e492014-10-25 02:39:043793 view_->SetInitialFocus();
[email protected]fc2b46b2014-05-03 16:33:453794}
3795
3796void WebContentsImpl::StoreFocus() {
miu9e14e492014-10-25 02:39:043797 view_->StoreFocus();
[email protected]fc2b46b2014-05-03 16:33:453798}
3799
3800void WebContentsImpl::RestoreFocus() {
miu9e14e492014-10-25 02:39:043801 view_->RestoreFocus();
[email protected]fc2b46b2014-05-03 16:33:453802}
3803
[email protected]b172aee2012-04-10 17:05:263804void WebContentsImpl::FocusThroughTabTraversal(bool reverse) {
Maxim Podgorodskiya926c4ff2017-11-20 02:06:393805 view_->FocusThroughTabTraversal(reverse);
[email protected]96d185d2009-04-24 03:28:543806}
3807
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:413808bool WebContentsImpl::ShowingInterstitialPage() {
creisadca9862017-06-16 19:12:113809 return interstitial_page_ != nullptr;
[email protected]96d185d2009-04-24 03:28:543810}
3811
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:413812InterstitialPageImpl* WebContentsImpl::GetInterstitialPage() {
creisadca9862017-06-16 19:12:113813 return interstitial_page_;
[email protected]686493142011-07-15 21:47:223814}
3815
[email protected]b172aee2012-04-10 17:05:263816bool WebContentsImpl::IsSavable() {
[email protected]a53209b2012-01-20 16:48:163817 // WebKit creates Document object when MIME type is application/xhtml+xml,
3818 // so we also support this MIME type.
3819 return contents_mime_type_ == "text/html" ||
3820 contents_mime_type_ == "text/xml" ||
3821 contents_mime_type_ == "application/xhtml+xml" ||
3822 contents_mime_type_ == "text/plain" ||
3823 contents_mime_type_ == "text/css" ||
Kinuko Yasuda74702f92017-07-31 03:27:533824 blink::IsSupportedJavascriptMimeType(contents_mime_type_);
[email protected]a53209b2012-01-20 16:48:163825}
3826
[email protected]b172aee2012-04-10 17:05:263827void WebContentsImpl::OnSavePage() {
[email protected]c7dd2f62011-07-18 15:57:593828 // If we can not save the page, try to download it.
[email protected]a53209b2012-01-20 16:48:163829 if (!IsSavable()) {
Min Qinda0ed2062018-02-23 22:00:533830 download::RecordSavePackageEvent(
3831 download::SAVE_PACKAGE_DOWNLOAD_ON_NON_HTML);
nasko89ad7742015-03-05 22:14:193832 SaveFrame(GetLastCommittedURL(), Referrer());
[email protected]27678b2a2012-02-04 22:09:143833 return;
[email protected]c7dd2f62011-07-18 15:57:593834 }
3835
3836 Stop();
3837
3838 // Create the save package and possibly prompt the user for the name to save
3839 // the page as. The user prompt is an asynchronous operation that runs on
3840 // another thread.
3841 save_package_ = new SavePackage(this);
3842 save_package_->GetSaveInfo();
3843}
3844
3845// Used in automated testing to bypass prompting the user for file names.
3846// Instead, the names and paths are hard coded rather than running them through
3847// file name sanitation and extension / mime checking.
[email protected]2dec8ec2013-02-07 19:20:343848bool WebContentsImpl::SavePage(const base::FilePath& main_file,
3849 const base::FilePath& dir_path,
[email protected]8ff00d72012-10-23 19:12:213850 SavePageType save_type) {
[email protected]c7dd2f62011-07-18 15:57:593851 // Stop the page from navigating.
3852 Stop();
3853
3854 save_package_ = new SavePackage(this, save_type, main_file, dir_path);
[email protected]8ff00d72012-10-23 19:12:213855 return save_package_->Init(SavePackageDownloadCreatedCallback());
[email protected]c7dd2f62011-07-18 15:57:593856}
3857
[email protected]3c71576ce2013-07-23 02:00:013858void WebContentsImpl::SaveFrame(const GURL& url,
3859 const Referrer& referrer) {
Xing Liu10329bf2018-03-20 19:22:143860 SaveFrameWithHeaders(url, referrer, std::string(), base::string16());
kundaji6c7f9692015-03-09 18:00:373861}
3862
Xing Liu10329bf2018-03-20 19:22:143863void WebContentsImpl::SaveFrameWithHeaders(
3864 const GURL& url,
3865 const Referrer& referrer,
3866 const std::string& headers,
3867 const base::string16& suggested_filename) {
Lei Zhang13a42e42019-02-19 23:28:113868 // Check and see if the guest can handle this.
3869 if (delegate_) {
3870 WebContents* guest_web_contents = nullptr;
3871 if (browser_plugin_embedder_) {
3872 BrowserPluginGuest* guest = browser_plugin_embedder_->GetFullPageGuest();
3873 if (guest)
3874 guest_web_contents = guest->GetWebContents();
3875 } else if (browser_plugin_guest_) {
3876 guest_web_contents = this;
3877 }
3878
3879 if (guest_web_contents && delegate_->GuestSaveFrame(guest_web_contents))
3880 return;
3881 }
3882
nasko89ad7742015-03-05 22:14:193883 if (!GetLastCommittedURL().is_valid())
[email protected]3c71576ce2013-07-23 02:00:013884 return;
sammc92af61552014-11-19 23:27:403885 if (delegate_ && delegate_->SaveFrame(url, referrer))
3886 return;
3887
nasko89ad7742015-03-05 22:14:193888 // TODO(nasko): This check for main frame is incorrect and should be fixed
brettw760f7442016-05-23 21:19:223889 // by explicitly passing in which frame this method should target. This would
3890 // indicate whether it's the main frame, and also tell us the frame pointer
3891 // to use for routing.
nasko89ad7742015-03-05 22:14:193892 bool is_main_frame = (url == GetLastCommittedURL());
brettw760f7442016-05-23 21:19:223893 RenderFrameHost* frame_host = GetMainFrame();
[email protected]3c71576ce2013-07-23 02:00:013894
avib7348942015-12-25 20:57:103895 int64_t post_id = -1;
[email protected]3c71576ce2013-07-23 02:00:013896 if (is_main_frame) {
Lucas Furukawa Gadani5553a152019-01-08 18:55:573897 NavigationEntry* entry = controller_.GetLastCommittedEntry();
[email protected]3c71576ce2013-07-23 02:00:013898 if (entry)
3899 post_id = entry->GetPostID();
3900 }
Ramin Halavati24e21012017-07-10 12:56:063901 net::NetworkTrafficAnnotationTag traffic_annotation =
3902 net::DefineNetworkTrafficAnnotation("download_web_contents_frame", R"(
3903 semantics {
3904 sender: "Save Page Action"
3905 description:
3906 "Saves the given frame's URL to the local file system."
3907 trigger:
3908 "The user has triggered a save operation on the frame through a "
3909 "context menu or other mechanism."
3910 data: "None."
3911 destination: WEBSITE
3912 }
3913 policy {
Ramin Halavati3b979782017-07-21 11:40:263914 cookies_allowed: YES
Ramin Halavati24e21012017-07-10 12:56:063915 cookies_store: "user"
3916 setting:
3917 "This feature cannot be disabled by settings, but it's is only "
3918 "triggered by user request."
3919 policy_exception_justification: "Not implemented."
3920 })");
Min Qina904f3302018-02-13 23:33:343921 auto params = std::make_unique<download::DownloadUrlParameters>(
brettw760f7442016-05-23 21:19:223922 url, frame_host->GetProcess()->GetID(),
3923 frame_host->GetRenderViewHost()->GetRoutingID(),
Matt Menke6d93c3c2018-10-10 15:21:503924 frame_host->GetRoutingID(), traffic_annotation);
Min Qina904f3302018-02-13 23:33:343925 params->set_referrer(referrer.url);
3926 params->set_referrer_policy(
3927 Referrer::ReferrerPolicyForUrlRequest(referrer.policy));
[email protected]3c71576ce2013-07-23 02:00:013928 params->set_post_id(post_id);
[email protected]3c71576ce2013-07-23 02:00:013929 if (post_id >= 0)
3930 params->set_method("POST");
3931 params->set_prompt(true);
kundaji6c7f9692015-03-09 18:00:373932
3933 if (headers.empty()) {
3934 params->set_prefer_cache(true);
3935 } else {
Min Qina904f3302018-02-13 23:33:343936 for (download::DownloadUrlParameters::RequestHeadersNameValuePair
3937 key_value : ParseDownloadHeaders(headers)) {
Megan Jablonski2f6a4c52017-07-10 23:01:253938 params->add_request_header(key_value.first, key_value.second);
kundaji6c7f9692015-03-09 18:00:373939 }
3940 }
Xing Liu10329bf2018-03-20 19:22:143941 params->set_suggested_name(suggested_filename);
Min Qin0ca8e1ee2018-01-31 00:49:353942 params->set_download_source(download::DownloadSource::WEB_CONTENTS_API);
thestig859c78892017-05-15 21:17:063943 BrowserContext::GetDownloadManager(GetBrowserContext())
Ramin Halavati03efa732017-06-12 09:28:203944 ->DownloadUrl(std::move(params));
[email protected]3c71576ce2013-07-23 02:00:013945}
3946
[email protected]b172aee2012-04-10 17:05:263947void WebContentsImpl::GenerateMHTML(
dewittj6dc5747a2016-05-17 01:48:473948 const MHTMLGenerationParams& params,
Avi Drissman149b7832018-03-23 14:31:493949 base::OnceCallback<void(int64_t)> callback) {
3950 MHTMLGenerationManager::GetInstance()->SaveMHTML(this, params,
3951 std::move(callback));
[email protected]aa4f3972012-03-01 18:12:123952}
3953
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:413954const std::string& WebContentsImpl::GetContentsMimeType() {
[email protected]0bfbf882011-12-22 18:19:273955 return contents_mime_type_;
3956}
3957
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:413958bool WebContentsImpl::WillNotifyDisconnection() {
[email protected]0bfbf882011-12-22 18:19:273959 return notify_disconnection_;
3960}
3961
Leon Hanc819dc62019-01-28 04:30:193962blink::mojom::RendererPreferences* WebContentsImpl::GetMutableRendererPrefs() {
[email protected]0bfbf882011-12-22 18:19:273963 return &renderer_preferences_;
3964}
3965
[email protected]e35ccd52012-05-23 16:22:473966void WebContentsImpl::Close() {
3967 Close(GetRenderViewHost());
3968}
3969
Ella Ge80a52dce2017-11-15 18:01:523970void WebContentsImpl::DragSourceEndedAt(float client_x,
3971 float client_y,
3972 float screen_x,
3973 float screen_y,
Paul Meyer0c58c3712016-11-17 22:59:513974 blink::WebDragOperation operation,
3975 RenderWidgetHost* source_rwh) {
Lei Zhang6410ec1a2019-01-31 21:38:583976 if (browser_plugin_embedder_) {
paulmeyer8fc8ea92016-11-15 05:12:213977 browser_plugin_embedder_->DragSourceEndedAt(
3978 client_x, client_y, screen_x, screen_y, operation);
Lei Zhang6410ec1a2019-01-31 21:38:583979 }
Paul Meyer0c58c3712016-11-17 22:59:513980 if (source_rwh) {
Ella Ge80a52dce2017-11-15 18:01:523981 source_rwh->DragSourceEndedAt(gfx::PointF(client_x, client_y),
3982 gfx::PointF(screen_x, screen_y), operation);
Paul Meyer0c58c3712016-11-17 22:59:513983 }
[email protected]cf200a562013-05-03 16:24:293984}
3985
jam73f892642016-09-11 06:04:063986void WebContentsImpl::LoadStateChanged(
Charles Harrison911fa062018-03-06 21:01:463987 const std::string& host,
jam73f892642016-09-11 06:04:063988 const net::LoadStateWithParam& load_state,
3989 uint64_t upload_position,
3990 uint64_t upload_size) {
Charles Harrison911fa062018-03-06 21:01:463991 base::string16 host16 = url_formatter::IDNToUnicode(host);
Elly Fong-Jonescad9c3d12018-01-31 17:26:103992 // Drop no-op updates.
3993 if (load_state_.state == load_state.state &&
3994 load_state_.param == load_state.param &&
3995 upload_position_ == upload_position && upload_size_ == upload_size &&
Charles Harrison911fa062018-03-06 21:01:463996 load_state_host_ == host16) {
Elly Fong-Jonescad9c3d12018-01-31 17:26:103997 return;
3998 }
jam73f892642016-09-11 06:04:063999 load_state_ = load_state;
4000 upload_position_ = upload_position;
4001 upload_size_ = upload_size;
Charles Harrison911fa062018-03-06 21:01:464002 load_state_host_ = host16;
jam73f892642016-09-11 06:04:064003 if (load_state_.state == net::LOAD_STATE_READING_RESPONSE)
4004 SetNotWaitingForResponse();
4005 if (IsLoading()) {
4006 NotifyNavigationStateChanged(static_cast<InvalidateTypes>(
4007 INVALIDATE_TYPE_LOAD | INVALIDATE_TYPE_TAB));
4008 }
4009}
4010
Francois Doraye6161152018-03-27 22:05:374011void WebContentsImpl::SetVisibility(Visibility visibility) {
4012 const Visibility previous_visibility = visibility_;
4013 visibility_ = visibility;
4014
Francois Dorayfe4a1772018-02-17 04:17:094015 // Notify observers if the visibility changed or if WasShown() is being called
4016 // for the first time.
Francois Dorayfe4a1772018-02-17 04:17:094017 if (visibility != previous_visibility ||
4018 (visibility == Visibility::VISIBLE && !did_first_set_visible_)) {
4019 for (auto& observer : observers_)
4020 observer.OnVisibilityChanged(visibility);
4021 }
4022}
4023
Michael Thiessen896405db2017-07-20 17:52:324024void WebContentsImpl::NotifyWebContentsFocused(
4025 RenderWidgetHost* render_widget_host) {
ericwilligersde386342016-10-19 02:35:094026 for (auto& observer : observers_)
Michael Thiessen896405db2017-07-20 17:52:324027 observer.OnWebContentsFocused(render_widget_host);
calamity7fe55ce2015-04-10 03:59:374028}
4029
Michael Thiessen896405db2017-07-20 17:52:324030void WebContentsImpl::NotifyWebContentsLostFocus(
4031 RenderWidgetHost* render_widget_host) {
zijiehe3bee08e22017-05-17 04:14:464032 for (auto& observer : observers_)
Michael Thiessen896405db2017-07-20 17:52:324033 observer.OnWebContentsLostFocus(render_widget_host);
zijiehe3bee08e22017-05-17 04:14:464034}
4035
Paul Meyer0c58c3712016-11-17 22:59:514036void WebContentsImpl::SystemDragEnded(RenderWidgetHost* source_rwh) {
4037 if (source_rwh)
4038 source_rwh->DragSourceSystemDragEnded();
Lei Zhang6410ec1a2019-01-31 21:38:584039 if (browser_plugin_embedder_)
[email protected]cf200a562013-05-03 16:24:294040 browser_plugin_embedder_->SystemDragEnded();
[email protected]7813bd72011-02-05 02:19:344041}
4042
[email protected]b172aee2012-04-10 17:05:264043void WebContentsImpl::SetClosedByUserGesture(bool value) {
[email protected]0bfbf882011-12-22 18:19:274044 closed_by_user_gesture_ = value;
4045}
4046
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:414047bool WebContentsImpl::GetClosedByUserGesture() {
[email protected]0bfbf882011-12-22 18:19:274048 return closed_by_user_gesture_;
4049}
4050
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:414051int WebContentsImpl::GetMinimumZoomPercent() {
[email protected]0bfbf882011-12-22 18:19:274052 return minimum_zoom_percent_;
4053}
4054
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:414055int WebContentsImpl::GetMaximumZoomPercent() {
[email protected]0bfbf882011-12-22 18:19:274056 return maximum_zoom_percent_;
4057}
4058
mcnee432e47d2015-11-09 19:37:464059void WebContentsImpl::SetPageScale(float page_scale_factor) {
Lukasz Anforowiczf4357ca2017-09-07 01:43:324060 GetRenderViewHost()->Send(new ViewMsg_SetPageScale(
4061 GetRenderViewHost()->GetRoutingID(), page_scale_factor));
ccameronb7c1d6c2015-03-09 17:08:244062}
4063
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:414064gfx::Size WebContentsImpl::GetPreferredSize() {
Francois Doray24fc62c2017-12-11 17:27:334065 return IsBeingCaptured() ? preferred_size_for_capture_ : preferred_size_;
[email protected]bcd2815602012-01-14 18:17:234066}
4067
[email protected]b172aee2012-04-10 17:05:264068bool WebContentsImpl::GotResponseToLockMouseRequest(bool allowed) {
lfgf0cd46e2017-01-04 00:05:234069 if (!GuestMode::IsCrossProcessFrameGuest(GetWebContents()) &&
4070 GetBrowserPluginGuest())
[email protected]660f18e2014-05-15 20:53:054071 return GetBrowserPluginGuest()->LockMouse(allowed);
4072
lfgf0cd46e2017-01-04 00:05:234073 if (mouse_lock_widget_) {
4074 if (mouse_lock_widget_->delegate()->GetAsWebContents() != this) {
4075 return mouse_lock_widget_->delegate()
4076 ->GetAsWebContents()
4077 ->GotResponseToLockMouseRequest(allowed);
4078 }
lfgad8244352016-07-13 16:55:514079
lfgf0cd46e2017-01-04 00:05:234080 if (mouse_lock_widget_->GotResponseToLockMouseRequest(allowed))
4081 return true;
4082 }
4083
4084 for (WebContentsImpl* current = this; current;
4085 current = current->GetOuterWebContents()) {
4086 current->mouse_lock_widget_ = nullptr;
4087 }
4088
lfgbee1e0a2016-06-08 21:24:214089 return false;
[email protected]0bfbf882011-12-22 18:19:274090}
4091
Joe Downing13dd76b2018-04-09 18:32:154092bool WebContentsImpl::GotResponseToKeyboardLockRequest(bool allowed) {
4093 if (!keyboard_lock_widget_)
4094 return false;
4095
4096 if (keyboard_lock_widget_->delegate()->GetAsWebContents() != this) {
4097 NOTREACHED();
4098 return false;
4099 }
4100
4101 // KeyboardLock is only supported when called by the top-level browsing
4102 // context and is not supported in embedded content scenarios.
4103 if (GetOuterWebContents())
4104 return false;
4105
4106 keyboard_lock_widget_->GotResponseToKeyboardLockRequest(allowed);
4107 return true;
4108}
4109
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:414110bool WebContentsImpl::HasOpener() {
Ivan Kotenkov2c0d2bb32017-11-01 15:41:284111 return GetOpener() != nullptr;
[email protected]a0358d72012-03-09 14:06:504112}
4113
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:414114RenderFrameHostImpl* WebContentsImpl::GetOpener() {
alexmose201c7cd2015-06-10 17:14:214115 FrameTreeNode* opener_ftn = frame_tree_.root()->opener();
lukasza6f8ac622017-06-06 03:10:204116 return opener_ftn ? opener_ftn->current_frame_host() : nullptr;
jochen55ff3502014-12-18 20:52:574117}
4118
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:414119bool WebContentsImpl::HasOriginalOpener() {
Ivan Kotenkov2c0d2bb32017-11-01 15:41:284120 return GetOriginalOpener() != nullptr;
jochen6004a362017-02-04 00:11:404121}
4122
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:414123RenderFrameHostImpl* WebContentsImpl::GetOriginalOpener() {
jochen6004a362017-02-04 00:11:404124 FrameTreeNode* opener_ftn = frame_tree_.root()->original_opener();
lukasza6f8ac622017-06-06 03:10:204125 return opener_ftn ? opener_ftn->current_frame_host() : nullptr;
jochen6004a362017-02-04 00:11:404126}
4127
[email protected]cb805452013-05-22 15:16:214128void WebContentsImpl::DidChooseColorInColorChooser(SkColor color) {
Joel Hockey85b379d2017-12-11 10:42:134129 color_chooser_->DidChooseColorInColorChooser(color);
[email protected]da8543762012-03-20 08:52:204130}
4131
[email protected]cb805452013-05-22 15:16:214132void WebContentsImpl::DidEndColorChooser() {
Joel Hockey85b379d2017-12-11 10:42:134133 color_chooser_.reset();
[email protected]da8543762012-03-20 08:52:204134}
4135
halton.huoca2eabd2015-07-06 08:17:404136int WebContentsImpl::DownloadImage(
4137 const GURL& url,
4138 bool is_favicon,
4139 uint32_t max_bitmap_size,
4140 bool bypass_cache,
Avi Drissmana5a52dd2018-03-27 03:39:024141 WebContents::ImageDownloadCallback callback) {
amistry9f01b772015-07-29 01:54:554142 DCHECK_CURRENTLY_ON(BrowserThread::UI);
halton.huoca2eabd2015-07-06 08:17:404143 static int next_image_download_id = 0;
Gyuyoung Kime370bdb2019-06-28 00:31:524144 const blink::mojom::ImageDownloaderPtr& mojo_image_downloader =
halton.huoca2eabd2015-07-06 08:17:404145 GetMainFrame()->GetMojoImageDownloader();
amistry9f01b772015-07-29 01:54:554146 const int download_id = ++next_image_download_id;
4147 if (!mojo_image_downloader) {
4148 // If the renderer process is dead (i.e. crash, or memory pressure on
4149 // Android), the downloader service will be invalid. Pre-Mojo, this would
Avi Drissmana5a52dd2018-03-27 03:39:024150 // hang the callback indefinitely since the IPC would be dropped. Now,
amistry9f01b772015-07-29 01:54:554151 // respond with a 400 HTTP error code to indicate that something went wrong.
Sami Kyostila8e4d5a92019-08-02 12:45:054152 base::PostTask(
Eric Seckler8652dcd52018-09-20 10:42:284153 FROM_HERE, {BrowserThread::UI},
tzike2aca992017-09-05 08:50:544154 base::BindOnce(&WebContentsImpl::OnDidDownloadImage,
Avi Drissmana5a52dd2018-03-27 03:39:024155 weak_factory_.GetWeakPtr(), std::move(callback),
4156 download_id, url, 400, std::vector<SkBitmap>(),
4157 std::vector<gfx::Size>()));
amistry9f01b772015-07-29 01:54:554158 return download_id;
4159 }
4160
halton.huoca2eabd2015-07-06 08:17:404161 mojo_image_downloader->DownloadImage(
sammcf6f4fcf2016-06-21 05:39:394162 url, is_favicon, max_bitmap_size, bypass_cache,
tzike2aca992017-09-05 08:50:544163 base::BindOnce(&WebContentsImpl::OnDidDownloadImage,
Avi Drissmana5a52dd2018-03-27 03:39:024164 weak_factory_.GetWeakPtr(), std::move(callback),
4165 download_id, url));
amistry9f01b772015-07-29 01:54:554166 return download_id;
[email protected]795c28972012-12-06 06:13:394167}
4168
[email protected]36ec24f2014-01-09 00:32:084169void WebContentsImpl::Find(int request_id,
4170 const base::string16& search_text,
Rakina Zata Amni3f77dff2018-09-08 16:19:434171 blink::mojom::FindOptionsPtr options) {
thestig6057a6b22015-11-12 23:01:334172 // Cowardly refuse to search for no text.
4173 if (search_text.empty()) {
4174 NOTREACHED();
4175 return;
4176 }
4177
Rakina Zata Amni3f77dff2018-09-08 16:19:434178 GetOrCreateFindRequestManager()->Find(request_id, search_text,
4179 std::move(options));
[email protected]36ec24f2014-01-09 00:32:084180}
4181
4182void WebContentsImpl::StopFinding(StopFindAction action) {
paulmeyerfeafc2d2017-04-25 21:46:404183 if (FindRequestManager* manager = GetFindRequestManager())
4184 manager->StopFinding(action);
[email protected]36ec24f2014-01-09 00:32:084185}
4186
Tommy Steimel18360512017-11-01 00:38:194187bool WebContentsImpl::WasEverAudible() {
4188 return was_ever_audible_;
4189}
4190
Avi Drissmana5a52dd2018-03-27 03:39:024191void WebContentsImpl::GetManifest(GetManifestCallback callback) {
4192 manifest_manager_host_->GetManifest(std::move(callback));
mlamouriefdca9d2014-09-16 16:55:404193}
4194
bokanece34a82016-01-28 19:49:464195void WebContentsImpl::ExitFullscreen(bool will_cause_resize) {
mlamouri7a78d6fd2015-01-17 13:23:534196 // Clean up related state and initiate the fullscreen exit.
avi3627ecac2015-10-16 17:40:434197 GetRenderViewHost()->GetWidget()->RejectMouseLockOrUnlockIfNecessary();
bokanece34a82016-01-28 19:49:464198 ExitFullscreenMode(will_cause_resize);
mlamouri7a78d6fd2015-01-17 13:23:534199}
4200
mariakhomenko44bdc4732015-04-29 01:55:384201void WebContentsImpl::ResumeLoadingCreatedWebContents() {
Nasko Oskov83a8cf92018-10-19 14:58:564202 if (delayed_load_url_params_.get()) {
4203 DCHECK(!delayed_open_url_params_);
4204 controller_.LoadURLWithParams(*delayed_load_url_params_.get());
4205 delayed_load_url_params_.reset(nullptr);
4206 return;
4207 }
4208
mariakhomenkoa4971c12015-07-21 19:04:374209 if (delayed_open_url_params_.get()) {
4210 OpenURL(*delayed_open_url_params_.get());
4211 delayed_open_url_params_.reset(nullptr);
4212 return;
4213 }
4214
mariakhomenko44bdc4732015-04-29 01:55:384215 // Resume blocked requests for both the RenderViewHost and RenderFrameHost.
4216 // TODO(brettw): It seems bogus to reach into here and initialize the host.
dfalcantarae6b7b46752015-07-10 18:14:164217 if (is_resume_pending_) {
4218 is_resume_pending_ = false;
avi0f1bbc9712015-11-17 02:58:134219 GetRenderViewHost()->GetWidget()->Init();
dfalcantarae6b7b46752015-07-10 18:14:164220 GetMainFrame()->Init();
4221 }
mariakhomenko44bdc4732015-04-29 01:55:384222}
4223
[email protected]b172aee2012-04-10 17:05:264224bool WebContentsImpl::FocusLocationBarByDefault() {
Xiyuan Xia2a66b112018-12-06 15:52:164225 if (should_focus_location_bar_by_default_)
[email protected]0c9406632013-02-08 01:13:334226 return true;
Xiyuan Xia2a66b112018-12-06 15:52:164227
[email protected]0c9406632013-02-08 01:13:334228 return delegate_ && delegate_->ShouldFocusLocationBarByDefault(this);
[email protected]0bfbf882011-12-22 18:19:274229}
4230
Tommy C. Li8bb5bf42019-01-18 18:45:514231void WebContentsImpl::SetFocusToLocationBar() {
[email protected]0bfbf882011-12-22 18:19:274232 if (delegate_)
Tommy C. Li8bb5bf42019-01-18 18:45:514233 delegate_->SetFocusToLocationBar();
[email protected]c40d6232011-03-25 00:16:214234}
4235
clamy0e119882015-07-31 16:12:334236void WebContentsImpl::DidStartNavigation(NavigationHandle* navigation_handle) {
Charles Harrison53096ba2017-12-15 15:39:484237 TRACE_EVENT1("navigation", "WebContentsImpl::DidStartNavigation",
4238 "navigation_handle", navigation_handle);
ericwilligersde386342016-10-19 02:35:094239 for (auto& observer : observers_)
4240 observer.DidStartNavigation(navigation_handle);
Becca Hughesd11d6502018-07-31 17:01:284241
4242 if (display_cutout_host_impl_)
4243 display_cutout_host_impl_->DidStartNavigation(navigation_handle);
Xiyuan Xia2a66b112018-12-06 15:52:164244
4245 if (navigation_handle->IsInMainFrame()) {
4246 // When the browser is started with about:blank as the startup URL, focus
4247 // the location bar (which will also select its contents) so people can
4248 // simply begin typing to navigate elsewhere.
4249 //
4250 // We need to be careful not to trigger this for anything other than the
4251 // startup navigation. In particular, if we allow an attacker to open a
4252 // popup to about:blank, then navigate, focusing the Omnibox will cause the
4253 // end of the new URL to be scrolled into view instead of the start,
4254 // allowing the attacker to spoof other URLs. The conditions checked here
4255 // are all aimed at ensuring no such attacker-controlled navigation can
4256 // trigger this.
4257 should_focus_location_bar_by_default_ =
4258 controller_.IsInitialNavigation() &&
4259 !navigation_handle->IsRendererInitiated() &&
4260 navigation_handle->GetURL() == url::kAboutBlankURL;
4261 }
clamy0e119882015-07-31 16:12:334262}
4263
4264void WebContentsImpl::DidRedirectNavigation(
4265 NavigationHandle* navigation_handle) {
Charles Harrison53096ba2017-12-15 15:39:484266 TRACE_EVENT1("navigation", "WebContentsImpl::DidRedirectNavigation",
4267 "navigation_handle", navigation_handle);
ericwilligersde386342016-10-19 02:35:094268 for (auto& observer : observers_)
4269 observer.DidRedirectNavigation(navigation_handle);
jam5f358e52017-02-13 16:37:034270
4271 // Notify accessibility if this is a reload. This has to called on the
4272 // BrowserAccessibilityManager associated with the old RFHI.
4273 if (navigation_handle->GetReloadType() != ReloadType::NONE) {
4274 NavigationHandleImpl* nhi =
4275 static_cast<NavigationHandleImpl*>(navigation_handle);
4276 BrowserAccessibilityManager* manager =
4277 nhi->frame_tree_node()
4278 ->current_frame_host()
4279 ->browser_accessibility_manager();
4280 if (manager)
4281 manager->UserIsReloading();
4282 }
clamy0e119882015-07-31 16:12:334283}
4284
clamyefca29e2015-09-17 00:22:114285void WebContentsImpl::ReadyToCommitNavigation(
4286 NavigationHandle* navigation_handle) {
Charles Harrison53096ba2017-12-15 15:39:484287 TRACE_EVENT1("navigation", "WebContentsImpl::ReadyToCommitNavigation",
4288 "navigation_handle", navigation_handle);
ericwilligersde386342016-10-19 02:35:094289 for (auto& observer : observers_)
4290 observer.ReadyToCommitNavigation(navigation_handle);
John Abd-El-Malekd4882642017-12-04 21:45:194291
Kenneth Russell954ed9ce2018-04-12 23:07:014292 // If any domains are blocked from accessing 3D APIs because they may
4293 // have caused the GPU to reset recently, unblock them here if the user
4294 // initiated this navigation. This implies that the user was involved in
4295 // the decision to navigate, so there's no concern about
4296 // denial-of-service issues. Want to do this as early as
4297 // possible to avoid race conditions with pages attempting to access
4298 // WebGL early on.
4299 //
4300 // TODO(crbug.com/617904): currently navigations initiated by the browser
4301 // (reload button, reload menu option, pressing return in the Omnibox)
4302 // return false from HasUserGesture(). If or when that is addressed,
4303 // remove the check for IsRendererInitiated() below.
4304 //
4305 // TODO(crbug.com/832180): HasUserGesture comes from the renderer
4306 // process and isn't validated. Until it is, don't trust it.
4307 if (!navigation_handle->IsRendererInitiated()) {
4308 GpuDataManagerImpl::GetInstance()->UnblockDomainFrom3DAPIs(
4309 navigation_handle->GetURL());
4310 }
4311
John Abd-El-Malekfde08e6da2017-12-05 04:06:244312 if (navigation_handle->IsSameDocument())
4313 return;
4314
Livvie Lin139bf442018-08-10 00:18:544315 // SSLInfo is not needed on subframe navigations since the main-frame
4316 // certificate is the only one that can be inspected (using the info
4317 // bubble) without refreshing the page with DevTools open.
Carlos ILaed1e7b2019-03-12 19:27:214318 // We don't call DidStartResourceResponse on net errors, since that results on
4319 // existing cert exceptions being revoked, which leads to weird behavior with
4320 // committed interstitials or while offline. We only need the error check for
4321 // the main frame case because unlike this method, SubresourceResponseStarted
4322 // does not get called on network errors.
4323 if (navigation_handle->IsInMainFrame() &&
4324 navigation_handle->GetNetErrorCode() == net::OK) {
Livvie Lin139bf442018-08-10 00:18:544325 controller_.ssl_manager()->DidStartResourceResponse(
4326 navigation_handle->GetURL(),
Camille Lamy62b826012019-02-26 09:15:474327 navigation_handle->GetSSLInfo().has_value()
4328 ? net::IsCertStatusError(
4329 navigation_handle->GetSSLInfo()->cert_status)
4330 : false);
4331 }
John Abd-El-Malekfde08e6da2017-12-05 04:06:244332
4333 SetNotWaitingForResponse();
clamyefca29e2015-09-17 00:22:114334}
4335
clamy0e119882015-07-31 16:12:334336void WebContentsImpl::DidFinishNavigation(NavigationHandle* navigation_handle) {
Charles Harrison53096ba2017-12-15 15:39:484337 TRACE_EVENT1("navigation", "WebContentsImpl::DidFinishNavigation",
4338 "navigation_handle", navigation_handle);
Steven Holte38041342018-06-22 22:00:574339
ericwilligersde386342016-10-19 02:35:094340 for (auto& observer : observers_)
4341 observer.DidFinishNavigation(navigation_handle);
clamy0e119882015-07-31 16:12:334342
Becca Hughesd11d6502018-07-31 17:01:284343 if (display_cutout_host_impl_)
4344 display_cutout_host_impl_->DidFinishNavigation(navigation_handle);
4345
jam5f358e52017-02-13 16:37:034346 if (navigation_handle->HasCommitted()) {
Dominic Farolinof87ec2a2019-07-04 09:18:094347 // TODO(domfarolino, dmazzoni): Do this using WebContentsObserver. See
4348 // https://crbug.com/981271.
dmazzonibf8cec42015-02-08 08:28:084349 BrowserAccessibilityManager* manager =
jam5f358e52017-02-13 16:37:034350 static_cast<RenderFrameHostImpl*>(
4351 navigation_handle->GetRenderFrameHost())
4352 ->browser_accessibility_manager();
4353 if (manager) {
4354 if (navigation_handle->IsErrorPage()) {
4355 manager->NavigationFailed();
4356 } else {
4357 manager->NavigationSucceeded();
4358 }
4359 }
Tommy Steimel18360512017-11-01 00:38:194360
4361 if (navigation_handle->IsInMainFrame() &&
4362 !navigation_handle->IsSameDocument()) {
4363 was_ever_audible_ = false;
Steven Holte38041342018-06-22 22:00:574364 last_committed_source_id_ =
4365 ukm::ConvertToSourceId(navigation_handle->GetNavigationId(),
4366 ukm::SourceIdType::NAVIGATION_ID);
Tommy Steimel18360512017-11-01 00:38:194367 }
dmazzonibf8cec42015-02-08 08:28:084368 }
Xiyuan Xia2a66b112018-12-06 15:52:164369
4370 // If we didn't end up on about:blank after setting this in DidStartNavigation
4371 // then don't focus the location bar.
4372 if (should_focus_location_bar_by_default_ &&
4373 navigation_handle->GetURL() != url::kAboutBlankURL) {
4374 should_focus_location_bar_by_default_ = false;
4375 }
[email protected]400992b2012-06-14 00:03:544376}
4377
[email protected]b80624c2014-02-09 02:46:554378void WebContentsImpl::DidFailLoadWithError(
4379 RenderFrameHostImpl* render_frame_host,
[email protected]b80624c2014-02-09 02:46:554380 const GURL& url,
[email protected]b80624c2014-02-09 02:46:554381 int error_code,
Yutaka Hirano16d3c5d2017-07-31 06:17:304382 const base::string16& error_description) {
ericwilligersde386342016-10-19 02:35:094383 for (auto& observer : observers_) {
Yutaka Hirano16d3c5d2017-07-31 06:17:304384 observer.DidFailLoad(render_frame_host, url, error_code, error_description);
ericwilligersde386342016-10-19 02:35:094385 }
[email protected]b80624c2014-02-09 02:46:554386}
4387
[email protected]52913802013-12-10 05:52:184388void WebContentsImpl::NotifyChangedNavigationState(
4389 InvalidateTypes changed_flags) {
4390 NotifyNavigationStateChanged(changed_flags);
4391}
4392
toyoshim0df1d3a2016-09-09 09:52:484393void WebContentsImpl::DidStartNavigationToPendingEntry(const GURL& url,
4394 ReloadType reload_type) {
[email protected]3691e5cf2014-01-22 10:16:204395 // Notify observers about navigation.
ericwilligersde386342016-10-19 02:35:094396 for (auto& observer : observers_)
4397 observer.DidStartNavigationToPendingEntry(url, reload_type);
[email protected]3691e5cf2014-01-22 10:16:204398}
4399
lukasza1d025732016-09-28 21:36:084400bool WebContentsImpl::ShouldTransferNavigation(bool is_main_frame_navigation) {
creis29460272015-12-16 04:38:224401 if (!delegate_)
4402 return true;
lukasza1d025732016-09-28 21:36:084403 return delegate_->ShouldTransferNavigation(is_main_frame_navigation);
creis29460272015-12-16 04:38:224404}
4405
[email protected]aa62afd2014-04-22 19:22:464406bool WebContentsImpl::ShouldPreserveAbortedURLs() {
4407 if (!delegate_)
4408 return false;
4409 return delegate_->ShouldPreserveAbortedURLs(this);
4410}
4411
[email protected]0d0f4c492014-04-02 06:42:574412void WebContentsImpl::DidNavigateMainFramePreCommit(
[email protected]5cfbddc2014-06-23 23:52:234413 bool navigation_is_within_page) {
[email protected]0d0f4c492014-04-02 06:42:574414 // Ensure fullscreen mode is exited before committing the navigation to a
4415 // different page. The next page will not start out assuming it is in
4416 // fullscreen mode.
[email protected]5cfbddc2014-06-23 23:52:234417 if (navigation_is_within_page) {
[email protected]dfc39cb2014-04-09 22:58:194418 // No page change? Then, the renderer and browser can remain in fullscreen.
[email protected]0d0f4c492014-04-02 06:42:574419 return;
4420 }
alexmos5dd617d2016-06-07 04:21:154421 if (IsFullscreenForCurrentTab())
bokanece34a82016-01-28 19:49:464422 ExitFullscreen(false);
alexmos5dd617d2016-06-07 04:21:154423 DCHECK(!IsFullscreenForCurrentTab());
Joe Downingc1a57efe2018-05-09 18:23:544424
4425 // Clean up keyboard lock state when navigating.
4426 CancelKeyboardLock(keyboard_lock_widget_);
[email protected]0d0f4c492014-04-02 06:42:574427}
4428
[email protected]37567b432014-02-12 01:12:224429void WebContentsImpl::DidNavigateMainFramePostCommit(
sky8b00392d2015-01-10 00:30:284430 RenderFrameHostImpl* render_frame_host,
[email protected]37567b432014-02-12 01:12:224431 const LoadCommittedDetails& details,
4432 const FrameHostMsg_DidCommitProvisionalLoad_Params& params) {
4433 if (details.is_navigation_to_different_page()) {
4434 // Clear the status bubble. This is a workaround for a bug where WebKit
4435 // doesn't let us know that the cursor left an element during a
4436 // transition (this is also why the mouse cursor remains as a hand after
4437 // clicking on a link); see bugs 1184641 and 980803. We don't want to
4438 // clear the bubble when a user navigates to a named anchor in the same
4439 // page.
Lukasz Anforowiczfd707142018-02-07 19:46:134440 ClearTargetURL();
lanwei9d343ad22015-02-11 01:46:004441
4442 RenderWidgetHostViewBase* rwhvb =
4443 static_cast<RenderWidgetHostViewBase*>(GetRenderWidgetHostView());
4444 if (rwhvb)
4445 rwhvb->OnDidNavigateMainFrameToNewPage();
yusufod41c5f92015-03-06 00:14:284446
peter90afaba2015-06-01 12:05:294447 did_first_visually_non_empty_paint_ = false;
4448
yusufod41c5f92015-03-06 00:14:284449 // Reset theme color on navigation to new page.
Alan Cutter92d59252019-03-29 02:32:334450 theme_color_.reset();
[email protected]37567b432014-02-12 01:12:224451 }
4452
[email protected]e85165c642014-06-10 14:34:314453 if (delegate_)
[email protected]37567b432014-02-12 01:12:224454 delegate_->DidNavigateMainFramePostCommit(this);
[email protected]e85165c642014-06-10 14:34:314455 view_->SetOverscrollControllerEnabled(CanOverscrollContent());
Shivani Sharma985474f2019-05-23 21:36:474456
4457 if (!details.is_same_document && GetInnerWebContents().empty())
4458 had_inner_webcontents_ = false;
[email protected]37567b432014-02-12 01:12:224459}
4460
4461void WebContentsImpl::DidNavigateAnyFramePostCommit(
4462 RenderFrameHostImpl* render_frame_host,
4463 const LoadCommittedDetails& details,
4464 const FrameHostMsg_DidCommitProvisionalLoad_Params& params) {
[email protected]b4c84012014-04-28 19:51:104465 // Now that something has committed, we don't need to track whether the
4466 // initial page has been accessed.
4467 has_accessed_initial_document_ = false;
4468
[email protected]37567b432014-02-12 01:12:224469 // If we navigate off the page, close all JavaScript dialogs.
eugenebutee08663a2017-04-27 17:43:124470 if (!details.is_same_document)
creis89a0f782015-05-27 16:13:174471 CancelActiveAndPendingDialogs();
[email protected]37567b432014-02-12 01:12:224472
avid53461d2016-02-25 17:15:044473 // If this is a user-initiated navigation, start allowing JavaScript dialogs
4474 // again.
avi5d3b8692016-10-12 22:00:464475 if (params.gesture == NavigationGestureUser && dialog_manager_) {
avi6879fcf2017-01-21 05:27:534476 dialog_manager_->CancelDialogs(this, /*reset_state=*/true);
avi5d3b8692016-10-12 22:00:464477 }
[email protected]37567b432014-02-12 01:12:224478}
4479
4480void WebContentsImpl::SetMainFrameMimeType(const std::string& mime_type) {
4481 contents_mime_type_ = mime_type;
4482}
4483
[email protected]277857a2014-06-03 10:38:014484bool WebContentsImpl::CanOverscrollContent() const {
[email protected]e85165c642014-06-10 14:34:314485 // Disable overscroll when touch emulation is on. See crbug.com/369938.
[email protected]34ff1cfc2014-08-20 06:16:054486 if (force_disable_overscroll_content_)
[email protected]e85165c642014-06-10 14:34:314487 return false;
4488
[email protected]37567b432014-02-12 01:12:224489 if (delegate_)
4490 return delegate_->CanOverscrollContent();
4491
4492 return false;
4493}
4494
nicka0ac8382016-12-15 23:59:234495void WebContentsImpl::OnThemeColorChanged(RenderFrameHostImpl* source,
Alan Cutter92d59252019-03-29 02:32:334496 base::Optional<SkColor> theme_color) {
nicka0ac8382016-12-15 23:59:234497 if (source != GetMainFrame()) {
4498 // Only the main frame may control the theme.
4499 return;
4500 }
4501
peter90afaba2015-06-01 12:05:294502 // Update the theme color. This is to be published to observers after the
4503 // first visually non-empty paint.
yusufod41c5f92015-03-06 00:14:284504 theme_color_ = theme_color;
peter90afaba2015-06-01 12:05:294505
4506 if (did_first_visually_non_empty_paint_ &&
4507 last_sent_theme_color_ != theme_color_) {
ericwilligersde386342016-10-19 02:35:094508 for (auto& observer : observers_)
4509 observer.DidChangeThemeColor(theme_color_);
peter90afaba2015-06-01 12:05:294510 last_sent_theme_color_ = theme_color_;
4511 }
[email protected]e7104762014-06-20 19:17:254512}
4513
[email protected]b172aee2012-04-10 17:05:264514void WebContentsImpl::OnDidLoadResourceFromMemoryCache(
nicka0ac8382016-12-15 23:59:234515 RenderFrameHostImpl* source,
[email protected]724159a2010-12-30 01:11:184516 const GURL& url,
[email protected]70435962011-08-02 20:13:284517 const std::string& http_method,
[email protected]6d6cfb3a2012-05-23 22:53:184518 const std::string& mime_type,
Maks Orlovich158ea092018-12-17 22:16:124519 const base::Optional<url::Origin>& top_frame_origin,
[email protected]6c1e05212014-07-31 00:59:404520 ResourceType resource_type) {
ericwilligersde386342016-10-19 02:35:094521 for (auto& observer : observers_)
4522 observer.DidLoadResourceFromMemoryCache(url, mime_type, resource_type);
[email protected]b0f724c2013-09-05 04:21:134523
Steven Bingler674fb0822018-12-12 18:04:524524 if (url.is_valid() && url.SchemeIsHTTPOrHTTPS()) {
nicka0ac8382016-12-15 23:59:234525 StoragePartition* partition = source->GetProcess()->GetStoragePartition();
Shivani Sharma8ae506c2019-07-21 21:08:274526 const url::Origin& last_committed_origin = source->GetLastCommittedOrigin();
Steven Bingler674fb0822018-12-12 18:04:524527
John Abd-El-Malekf4fe9d82019-07-31 04:05:594528 partition->GetNetworkContext()->NotifyExternalCacheHit(
4529 url, http_method, top_frame_origin, last_committed_origin);
[email protected]8bfc8272013-09-09 20:10:534530 }
[email protected]724159a2010-12-30 01:11:184531}
4532
nicka0ac8382016-12-15 23:59:234533void WebContentsImpl::OnDidDisplayInsecureContent(RenderFrameHostImpl* source) {
4534 // Any frame can trigger display of insecure content, so we don't check
4535 // |source| here.
carloskd9d97942017-02-16 08:58:094536 DidDisplayInsecureContent();
4537}
4538
4539void WebContentsImpl::DidDisplayInsecureContent() {
estarkc2273502016-10-12 22:03:024540 controller_.ssl_manager()->DidDisplayMixedContent();
[email protected]724159a2010-12-30 01:11:184541}
4542
elawrenceb2ac2a232017-03-27 21:46:254543void WebContentsImpl::OnDidContainInsecureFormAction(
4544 RenderFrameHostImpl* source) {
4545 controller_.ssl_manager()->DidContainInsecureFormAction();
4546}
4547
nicka0ac8382016-12-15 23:59:234548void WebContentsImpl::OnDidRunInsecureContent(RenderFrameHostImpl* source,
4549 const GURL& security_origin,
estark910b4572015-12-09 20:55:414550 const GURL& target_url) {
nicka0ac8382016-12-15 23:59:234551 // TODO(nick, estark): Should we call FilterURL using |source|'s process on
4552 // these parameters? |target_url| seems unused, except for a log message. And
4553 // |security_origin| might be replaceable with the origin of the main frame.
carloskd9d97942017-02-16 08:58:094554 DidRunInsecureContent(security_origin, target_url);
4555}
4556
4557void WebContentsImpl::DidRunInsecureContent(const GURL& security_origin,
4558 const GURL& target_url) {
[email protected]9450c462013-11-23 01:22:584559 LOG(WARNING) << security_origin << " ran insecure content from "
4560 << target_url.possibly_invalid_spec();
[email protected]e6e30ac2014-01-13 21:24:394561 RecordAction(base::UserMetricsAction("SSL.RanInsecureContent"));
estark910b4572015-12-09 20:55:414562 if (base::EndsWith(security_origin.spec(), kDotGoogleDotCom,
brettwa7ff1b292015-07-16 17:49:294563 base::CompareCase::INSENSITIVE_ASCII))
[email protected]e6e30ac2014-01-13 21:24:394564 RecordAction(base::UserMetricsAction("SSL.RanInsecureContentGoogle"));
estarkc2273502016-10-12 22:03:024565 controller_.ssl_manager()->DidRunMixedContent(security_origin);
[email protected]724159a2010-12-30 01:11:184566}
4567
carloskd9d97942017-02-16 08:58:094568void WebContentsImpl::PassiveInsecureContentFound(const GURL& resource_url) {
Sergey Kuznetsov3ebc01b2018-02-01 04:57:364569 if (delegate_) {
4570 delegate_->PassiveInsecureContentFound(resource_url);
4571 }
carloskd9d97942017-02-16 08:58:094572}
4573
4574bool WebContentsImpl::ShouldAllowRunningInsecureContent(
4575 WebContents* web_contents,
4576 bool allowed_per_prefs,
4577 const url::Origin& origin,
4578 const GURL& resource_url) {
Sergey Kuznetsov3ebc01b2018-02-01 04:57:364579 if (delegate_) {
4580 return delegate_->ShouldAllowRunningInsecureContent(
4581 web_contents, allowed_per_prefs, origin, resource_url);
4582 }
4583
4584 return allowed_per_prefs;
carloskd9d97942017-02-16 08:58:094585}
4586
Lukasz Anforowicze1b954d92017-10-30 21:28:064587void WebContentsImpl::ViewSource(RenderFrameHostImpl* frame) {
4588 DCHECK_EQ(this, WebContents::FromRenderFrameHost(frame));
4589
4590 // Don't do anything if there is no |delegate_| that could accept and show the
4591 // new WebContents containing the view-source.
4592 if (!delegate_)
4593 return;
4594
4595 // Use the last committed entry, since the pending entry hasn't loaded yet and
4596 // won't be copied into the cloned tab.
4597 NavigationEntryImpl* last_committed_entry =
4598 static_cast<NavigationEntryImpl*>(frame->frame_tree_node()
4599 ->navigator()
4600 ->GetController()
4601 ->GetLastCommittedEntry());
4602 if (!last_committed_entry)
4603 return;
4604
4605 FrameNavigationEntry* frame_entry =
4606 last_committed_entry->GetFrameEntry(frame->frame_tree_node());
4607 if (!frame_entry)
4608 return;
4609
4610 // Any new WebContents opened while this WebContents is in fullscreen can be
4611 // used to confuse the user, so drop fullscreen.
Avi Drissmand18c51972018-08-15 18:32:094612 ForSecurityDropFullscreen();
Lukasz Anforowicze1b954d92017-10-30 21:28:064613
4614 // We intentionally don't share the SiteInstance with the original frame so
4615 // that view source has a consistent process model and always ends up in a new
4616 // process (https://crbug.com/699493).
4617 scoped_refptr<SiteInstanceImpl> site_instance_for_view_source = nullptr;
Lukasz Anforowicz435bcb582019-07-12 20:50:064618 // Referrer and initiator are not important, because view-source should not
4619 // hit the network, but should be served from the cache instead.
Lukasz Anforowicze1b954d92017-10-30 21:28:064620 Referrer referrer_for_view_source;
Lukasz Anforowicz435bcb582019-07-12 20:50:064621 base::Optional<url::Origin> initiator_for_view_source = base::nullopt;
Lukasz Anforowicze1b954d92017-10-30 21:28:064622 // Do not restore title, derive it from the url.
4623 base::string16 title_for_view_source;
4624 auto navigation_entry = std::make_unique<NavigationEntryImpl>(
4625 site_instance_for_view_source, frame_entry->url(),
Lukasz Anforowicz435bcb582019-07-12 20:50:064626 referrer_for_view_source, initiator_for_view_source,
4627 title_for_view_source, ui::PAGE_TRANSITION_LINK,
Marijn Kruisselbrink7a0d5e182018-05-24 22:55:094628 /* is_renderer_initiated = */ false,
4629 /* blob_url_loader_factory = */ nullptr);
Lukasz Anforowicze1b954d92017-10-30 21:28:064630 navigation_entry->SetVirtualURL(GURL(content::kViewSourceScheme +
4631 std::string(":") +
4632 frame_entry->url().spec()));
4633
4634 // Do not restore scroller position.
4635 // TODO(creis, lukasza, arthursonzogni): Do not reuse the original PageState,
4636 // but start from a new one and only copy the needed data.
4637 const PageState& new_page_state =
4638 frame_entry->page_state().RemoveScrollOffset();
4639
4640 scoped_refptr<FrameNavigationEntry> new_frame_entry =
4641 navigation_entry->root_node()->frame_entry;
4642 new_frame_entry->set_method(frame_entry->method());
4643 new_frame_entry->SetPageState(new_page_state);
4644
4645 // Create a new WebContents, which is used to display the source code.
erikchenbee5c9622018-04-27 19:30:254646 std::unique_ptr<WebContents> view_source_contents =
Erik Chenbb8e738e2018-04-28 14:10:434647 Create(CreateParams(GetBrowserContext()));
Lukasz Anforowicze1b954d92017-10-30 21:28:064648
4649 // Restore the previously created NavigationEntry.
4650 std::vector<std::unique_ptr<NavigationEntry>> navigation_entries;
4651 navigation_entries.push_back(std::move(navigation_entry));
4652 view_source_contents->GetController().Restore(0, RestoreType::CURRENT_SESSION,
4653 &navigation_entries);
4654
4655 // Add |view_source_contents| as a new tab.
4656 gfx::Rect initial_rect;
4657 constexpr bool kUserGesture = true;
4658 bool ignored_was_blocked;
erikchenbee5c9622018-04-27 19:30:254659 delegate_->AddNewContents(this, std::move(view_source_contents),
Lukasz Anforowicze1b954d92017-10-30 21:28:064660 WindowOpenDisposition::NEW_FOREGROUND_TAB,
4661 initial_rect, kUserGesture, &ignored_was_blocked);
4662 // Note that the |delegate_| could have deleted |view_source_contents| during
4663 // AddNewContents method call.
4664}
4665
Jay Civellid5904f02018-03-23 19:43:574666void WebContentsImpl::SubresourceResponseStarted(const GURL& url,
4667 net::CertStatus cert_status) {
4668 controller_.ssl_manager()->DidStartResourceResponse(url, cert_status);
4669 SetNotWaitingForResponse();
4670}
4671
Jay Civelli116683f2018-03-27 19:56:234672void WebContentsImpl::ResourceLoadComplete(
Jay Civelli52a63302018-04-30 22:39:554673 RenderFrameHost* render_frame_host,
Clark DuVall36164bd2018-08-09 22:49:084674 const GlobalRequestID& request_id,
Jay Civelli116683f2018-03-27 19:56:234675 mojom::ResourceLoadInfoPtr resource_load_info) {
John Abd-El-Malek9b8f7fa2017-12-04 23:52:254676 for (auto& observer : observers_) {
Clark DuVall36164bd2018-08-09 22:49:084677 observer.ResourceLoadComplete(render_frame_host, request_id,
4678 *resource_load_info);
John Abd-El-Malek9b8f7fa2017-12-04 23:52:254679 }
John Abd-El-Malekd4882642017-12-04 21:45:194680}
4681
Wei Li5bb659742018-02-14 03:07:584682void WebContentsImpl::PrintCrossProcessSubframe(
4683 const gfx::Rect& rect,
4684 int document_cookie,
4685 RenderFrameHost* subframe_host) {
Wei Li5c0b4d12018-03-22 15:05:224686 auto* outer_contents = GetOuterWebContents();
4687 if (outer_contents) {
4688 // When an extension or app page is printed, the content should be
4689 // composited with outer content, so the outer contents should handle the
4690 // print request.
4691 outer_contents->PrintCrossProcessSubframe(rect, document_cookie,
4692 subframe_host);
4693 return;
4694 }
4695
Wei Li5bb659742018-02-14 03:07:584696 // If there is no delegate such as in tests or during deletion, do nothing.
4697 if (!delegate_)
4698 return;
4699
4700 delegate_->PrintCrossProcessSubframe(this, rect, document_cookie,
4701 subframe_host);
4702}
4703
rob.buis300b0872017-03-10 20:43:584704#if defined(OS_ANDROID)
4705base::android::ScopedJavaLocalRef<jobject>
4706WebContentsImpl::GetJavaRenderFrameHostDelegate() {
4707 return GetJavaWebContents();
4708}
4709#endif
4710
estark910b4572015-12-09 20:55:414711void WebContentsImpl::OnDidDisplayContentWithCertificateErrors(
Zhuoyu Qian4353f7f2018-01-08 05:33:004712 RenderFrameHostImpl* source) {
estarkc2273502016-10-12 22:03:024713 controller_.ssl_manager()->DidDisplayContentWithCertErrors();
estark910b4572015-12-09 20:55:414714}
4715
4716void WebContentsImpl::OnDidRunContentWithCertificateErrors(
Zhuoyu Qian4353f7f2018-01-08 05:33:004717 RenderFrameHostImpl* source) {
nicka0ac8382016-12-15 23:59:234718 // TODO(nick, estark): Do we need to consider |source| here somehow?
estarkcd2e30c2016-08-12 06:51:154719 NavigationEntry* entry = controller_.GetVisibleEntry();
4720 if (!entry)
estarkaeda1422016-05-10 02:31:124721 return;
4722
estarkaeda1422016-05-10 02:31:124723 // TODO(estark): check that this does something reasonable for
4724 // about:blank and sandboxed origins. https://crbug.com/609527
estarkcd2e30c2016-08-12 06:51:154725 controller_.ssl_manager()->DidRunContentWithCertErrors(
4726 entry->GetURL().GetOrigin());
estark910b4572015-12-09 20:55:414727}
4728
nicka0ac8382016-12-15 23:59:234729void WebContentsImpl::OnDocumentLoadedInFrame(RenderFrameHostImpl* source) {
ericwilligersde386342016-10-19 02:35:094730 for (auto& observer : observers_)
nicka0ac8382016-12-15 23:59:234731 observer.DocumentLoadedInFrame(source);
[email protected]724159a2010-12-30 01:11:184732}
4733
nicka0ac8382016-12-15 23:59:234734void WebContentsImpl::OnDidFinishLoad(RenderFrameHostImpl* source,
4735 const GURL& url) {
[email protected]8b3af1e2014-01-24 13:29:124736 GURL validated_url(url);
nicka0ac8382016-12-15 23:59:234737 source->GetProcess()->FilterURL(false, &validated_url);
[email protected]028053d42014-03-05 22:20:374738
ericwilligersde386342016-10-19 02:35:094739 for (auto& observer : observers_)
nicka0ac8382016-12-15 23:59:234740 observer.DidFinishLoad(source, validated_url);
[email protected]1a55c5be2011-11-29 11:36:314741}
4742
Dave Tapuska549baab2019-03-19 15:52:404743void WebContentsImpl::OnGoToEntryAtOffset(RenderFrameHostImpl* source,
Arthur Sonzognia7d715a2018-09-20 16:11:134744 int offset,
4745 bool has_user_gesture) {
4746 // Non-user initiated navigations coming from the renderer should be ignored
4747 // if there is an ongoing browser-initiated navigation.
4748 // See https://crbug.com/879965.
4749 // TODO(arthursonzogni): See if this should check for ongoing navigations in
4750 // the frame(s) affected by the session history navigation, rather than just
4751 // the main frame.
4752 if (!has_user_gesture) {
4753 NavigationRequest* ongoing_navigation_request =
4754 frame_tree_.root()->navigation_request();
4755 if (ongoing_navigation_request &&
4756 ongoing_navigation_request->browser_initiated()) {
4757 return;
4758 }
4759 }
4760
nicka0ac8382016-12-15 23:59:234761 // All frames are allowed to navigate the global history.
Dave Tapuska8bfd84c2019-03-26 20:47:164762 if (!delegate_ || delegate_->OnGoToEntryOffset(offset)) {
4763 if (source->IsSandboxed(blink::WebSandboxFlags::kTopNavigation)) {
4764 // Keep track of whether this is a session history from a sandboxed iframe
4765 // with top level navigation disallowed.
4766 controller_.GoToOffsetInSandboxedFrame(offset,
4767 source->GetFrameTreeNodeId());
4768 } else {
4769 controller_.GoToOffset(offset);
4770 }
4771 }
[email protected]216813952011-05-19 22:21:264772}
4773
nicka0ac8382016-12-15 23:59:234774void WebContentsImpl::OnUpdateZoomLimits(RenderViewHostImpl* source,
4775 int minimum_percent,
[email protected]fce823222014-05-30 16:24:304776 int maximum_percent) {
[email protected]216813952011-05-19 22:21:264777 minimum_zoom_percent_ = minimum_percent;
4778 maximum_zoom_percent_ = maximum_percent;
[email protected]216813952011-05-19 22:21:264779}
4780
nicka0ac8382016-12-15 23:59:234781void WebContentsImpl::OnPageScaleFactorChanged(RenderViewHostImpl* source,
4782 float page_scale_factor) {
mcnee336ea2c2017-05-23 22:50:594783#if !defined(OS_ANDROID)
4784 // While page scale factor is used on mobile, this PageScaleFactorIsOne logic
4785 // is only needed on desktop.
mcnee432e47d2015-11-09 19:37:464786 bool is_one = page_scale_factor == 1.f;
4787 if (is_one != page_scale_factor_is_one_) {
4788 page_scale_factor_is_one_ = is_one;
4789
4790 HostZoomMapImpl* host_zoom_map =
4791 static_cast<HostZoomMapImpl*>(HostZoomMap::GetForWebContents(this));
4792
nicka0ac8382016-12-15 23:59:234793 if (host_zoom_map) {
mcnee432e47d2015-11-09 19:37:464794 host_zoom_map->SetPageScaleFactorIsOneForView(
nicka0ac8382016-12-15 23:59:234795 source->GetProcess()->GetID(), source->GetRoutingID(),
mcnee432e47d2015-11-09 19:37:464796 page_scale_factor_is_one_);
4797 }
4798 }
mcnee336ea2c2017-05-23 22:50:594799#endif // !defined(OS_ANDROID)
mcnee432e47d2015-11-09 19:37:464800
ericwilligersde386342016-10-19 02:35:094801 for (auto& observer : observers_)
4802 observer.OnPageScaleFactorChanged(page_scale_factor);
mcnee432e47d2015-11-09 19:37:464803}
4804
W. James MacLean62198672019-06-04 16:46:104805void WebContentsImpl::OnTextAutosizerPageInfoChanged(
4806 RenderViewHostImpl* source,
4807 const blink::WebTextAutosizerPageInfo& page_info) {
4808 // Keep a copy of |page_info| in case we create a new RenderView before
4809 // the next update.
4810 text_autosizer_page_info_ = page_info;
4811 frame_tree_.root()->render_manager()->SendPageMessage(
4812 new PageMsg_UpdateTextAutosizerPageInfoForRemoteMainFrames(
4813 MSG_ROUTING_NONE, page_info),
4814 source->GetSiteInstance());
4815}
4816
Kent Tamura8c9e0562018-11-06 07:02:194817void WebContentsImpl::EnumerateDirectory(
4818 RenderFrameHost* render_frame_host,
4819 std::unique_ptr<FileSelectListener> listener,
4820 const base::FilePath& directory_path) {
4821 if (delegate_)
4822 delegate_->EnumerateDirectory(this, std::move(listener), directory_path);
4823 else
4824 listener->FileSelectionCanceled();
[email protected]3a29a6e2011-08-24 18:26:214825}
4826
nicka0ac8382016-12-15 23:59:234827void WebContentsImpl::OnRegisterProtocolHandler(RenderFrameHostImpl* source,
4828 const std::string& protocol,
[email protected]b172aee2012-04-10 17:05:264829 const GURL& url,
[email protected]fcf75d42013-12-03 20:11:264830 const base::string16& title,
[email protected]3a3b75a2012-06-01 08:38:364831 bool user_gesture) {
nicka0ac8382016-12-15 23:59:234832 // TODO(nick): Should we consider |source| here or pass it to the delegate?
4833 // TODO(nick): Do we need to apply FilterURL to |url|?
[email protected]e5f2de02012-07-20 22:15:434834 if (!delegate_)
4835 return;
4836
Ben Wellsf0180032019-07-15 05:59:094837 ChildProcessSecurityPolicyImpl* policy =
4838 ChildProcessSecurityPolicyImpl::GetInstance();
4839 if (policy->IsPseudoScheme(protocol))
[email protected]b9535422012-02-09 01:47:594840 return;
[email protected]8f810632013-06-06 22:33:324841
[email protected]c125e532014-05-10 20:39:064842 delegate_->RegisterProtocolHandler(this, protocol, url, user_gesture);
[email protected]7d189022011-08-25 22:54:204843}
4844
nicka0ac8382016-12-15 23:59:234845void WebContentsImpl::OnUnregisterProtocolHandler(RenderFrameHostImpl* source,
4846 const std::string& protocol,
[email protected]f5273e52014-07-14 16:30:204847 const GURL& url,
4848 bool user_gesture) {
nicka0ac8382016-12-15 23:59:234849 // TODO(nick): Should we consider |source| here or pass it to the delegate?
4850 // TODO(nick): Do we need to apply FilterURL to |url|?
[email protected]f5273e52014-07-14 16:30:204851 if (!delegate_)
4852 return;
4853
Ben Wellsf0180032019-07-15 05:59:094854 ChildProcessSecurityPolicyImpl* policy =
4855 ChildProcessSecurityPolicyImpl::GetInstance();
4856 if (policy->IsPseudoScheme(protocol))
[email protected]f5273e52014-07-14 16:30:204857 return;
[email protected]f5273e52014-07-14 16:30:204858
4859 delegate_->UnregisterProtocolHandler(this, protocol, url, user_gesture);
4860}
4861
kouhei40f03cb2015-09-24 07:47:014862void WebContentsImpl::OnUpdatePageImportanceSignals(
nicka0ac8382016-12-15 23:59:234863 RenderFrameHostImpl* source,
kouhei40f03cb2015-09-24 07:47:014864 const PageImportanceSignals& signals) {
nicka0ac8382016-12-15 23:59:234865 // TODO(nick, kouhei): Fix this for oopifs; currently all frames' state gets
4866 // written to this one field.
kouhei40f03cb2015-09-24 07:47:014867 page_importance_signals_ = signals;
4868}
4869
nicka0ac8382016-12-15 23:59:234870void WebContentsImpl::OnDomOperationResponse(RenderFrameHostImpl* source,
4871 const std::string& json_string) {
4872 // TODO(nick, lukasza): The notification below should probably be updated to
4873 // include |source|.
avi60bd4902015-09-23 20:39:244874 std::string json = json_string;
4875 NotificationService::current()->Notify(NOTIFICATION_DOM_OPERATION_RESPONSE,
4876 Source<WebContents>(this),
4877 Details<std::string>(&json));
[email protected]cfa856d62014-02-22 07:58:404878}
4879
Marijn Kruisselbrink7f519672019-02-05 19:12:504880void WebContentsImpl::OnAppCacheAccessed(const GURL& manifest_url,
[email protected]b172aee2012-04-10 17:05:264881 bool blocked_by_policy) {
nicka0ac8382016-12-15 23:59:234882 // TODO(nick): Should we consider |source| here? Should we call FilterURL on
4883 // |manifest_url|?
4884
[email protected]7fc4bbb2011-09-08 21:23:104885 // Notify observers about navigation.
ericwilligersde386342016-10-19 02:35:094886 for (auto& observer : observers_)
4887 observer.AppCacheAccessed(manifest_url, blocked_by_policy);
[email protected]7fc4bbb2011-09-08 21:23:104888}
4889
Joel Hockey85b379d2017-12-11 10:42:134890void WebContentsImpl::OnColorChooserFactoryRequest(
Joel Hockey163835a2017-12-20 11:51:574891 blink::mojom::ColorChooserFactoryRequest request) {
Joel Hockey85b379d2017-12-11 10:42:134892 color_chooser_factory_bindings_.AddBinding(this, std::move(request));
4893}
4894
4895void WebContentsImpl::OpenColorChooser(
Joel Hockey163835a2017-12-20 11:51:574896 blink::mojom::ColorChooserRequest chooser_request,
4897 blink::mojom::ColorChooserClientPtr client,
[email protected]8ed16472014-04-11 19:02:484898 SkColor color,
Joel Hockey163835a2017-12-20 11:51:574899 std::vector<blink::mojom::ColorSuggestionPtr> suggestions) {
Joel Hockey85b379d2017-12-11 10:42:134900 content::ColorChooser* new_color_chooser =
Ivan Kotenkov2c0d2bb32017-11-01 15:41:284901 delegate_ ? delegate_->OpenColorChooser(this, color, suggestions)
4902 : nullptr;
[email protected]820957a2014-01-14 14:56:304903 if (!new_color_chooser)
[email protected]cb805452013-05-22 15:16:214904 return;
[email protected]8ed16472014-04-11 19:02:484905
Joel Hockey85b379d2017-12-11 10:42:134906 color_chooser_.reset();
4907 color_chooser_ = std::make_unique<ColorChooser>(
4908 new_color_chooser, std::move(chooser_request), std::move(client));
[email protected]da8543762012-03-20 08:52:204909}
4910
brettw4b461082016-11-19 18:55:164911#if BUILDFLAG(ENABLE_PLUGINS)
nicka0ac8382016-12-15 23:59:234912void WebContentsImpl::OnPepperInstanceCreated(RenderFrameHostImpl* source,
4913 int32_t pp_instance) {
ericwilligersde386342016-10-19 02:35:094914 for (auto& observer : observers_)
4915 observer.PepperInstanceCreated();
nicka0ac8382016-12-15 23:59:234916 pepper_playback_observer_->PepperInstanceCreated(source, pp_instance);
emaxxe70f5e12015-05-29 11:26:004917}
4918
nicka0ac8382016-12-15 23:59:234919void WebContentsImpl::OnPepperInstanceDeleted(RenderFrameHostImpl* source,
4920 int32_t pp_instance) {
ericwilligersde386342016-10-19 02:35:094921 for (auto& observer : observers_)
4922 observer.PepperInstanceDeleted();
nicka0ac8382016-12-15 23:59:234923 pepper_playback_observer_->PepperInstanceDeleted(source, pp_instance);
emaxxe70f5e12015-05-29 11:26:004924}
4925
nicka0ac8382016-12-15 23:59:234926void WebContentsImpl::OnPepperPluginHung(RenderFrameHostImpl* source,
4927 int plugin_child_id,
tommyclie6633ca72014-10-31 00:40:424928 const base::FilePath& path,
4929 bool is_hung) {
Steven Holte95922222018-09-14 20:06:234930 UMA_HISTOGRAM_COUNTS_1M("Pepper.PluginHung", 1);
tommyclie6633ca72014-10-31 00:40:424931
ericwilligersde386342016-10-19 02:35:094932 for (auto& observer : observers_)
4933 observer.PluginHungStatusChanged(plugin_child_id, path, is_hung);
tommyclie6633ca72014-10-31 00:40:424934}
4935
nicka0ac8382016-12-15 23:59:234936void WebContentsImpl::OnPepperStartsPlayback(RenderFrameHostImpl* source,
4937 int32_t pp_instance) {
4938 pepper_playback_observer_->PepperStartsPlayback(source, pp_instance);
zqzhang181047e62016-07-01 13:37:174939}
4940
nicka0ac8382016-12-15 23:59:234941void WebContentsImpl::OnPepperStopsPlayback(RenderFrameHostImpl* source,
4942 int32_t pp_instance) {
4943 pepper_playback_observer_->PepperStopsPlayback(source, pp_instance);
zqzhang181047e62016-07-01 13:37:174944}
4945
nicka0ac8382016-12-15 23:59:234946void WebContentsImpl::OnPluginCrashed(RenderFrameHostImpl* source,
4947 const base::FilePath& plugin_path,
tommyclie6633ca72014-10-31 00:40:424948 base::ProcessId plugin_pid) {
nicka0ac8382016-12-15 23:59:234949 // TODO(nick): Eliminate the |plugin_pid| parameter, which can't be trusted,
4950 // and is only used by BlinkTestController.
ericwilligersde386342016-10-19 02:35:094951 for (auto& observer : observers_)
4952 observer.PluginCrashed(plugin_path, plugin_pid);
tommyclie6633ca72014-10-31 00:40:424953}
4954
[email protected]d8415ad92012-08-23 14:40:504955void WebContentsImpl::OnRequestPpapiBrokerPermission(
nicka0ac8382016-12-15 23:59:234956 RenderViewHostImpl* source,
4957 int ppb_broker_route_id,
[email protected]d8415ad92012-08-23 14:40:504958 const GURL& url,
[email protected]2dec8ec2013-02-07 19:20:344959 const base::FilePath& plugin_path) {
Avi Drissman0a2a967d2019-04-01 19:33:524960 base::OnceCallback<void(bool)> permission_result_callback = base::BindOnce(
nicka0ac8382016-12-15 23:59:234961 &WebContentsImpl::SendPpapiBrokerPermissionResult, base::Unretained(this),
4962 source->GetProcess()->GetID(), ppb_broker_route_id);
[email protected]8c8fc292012-11-23 18:57:164963 if (!delegate_) {
Tommy Nyquist4b749d02018-03-20 21:46:294964 std::move(permission_result_callback).Run(false);
[email protected]8c8fc292012-11-23 18:57:164965 return;
[email protected]d8415ad92012-08-23 14:40:504966 }
4967
Avi Drissman0a2a967d2019-04-01 19:33:524968 delegate_->RequestPpapiBrokerPermission(
4969 this, url, plugin_path, std::move(permission_result_callback));
[email protected]d8415ad92012-08-23 14:40:504970}
4971
nicka0ac8382016-12-15 23:59:234972void WebContentsImpl::SendPpapiBrokerPermissionResult(int process_id,
4973 int ppb_broker_route_id,
4974 bool result) {
4975 RenderProcessHost* rph = RenderProcessHost::FromID(process_id);
4976 if (rph) {
4977 // TODO(nick): Convert this from ViewMsg_ to a Ppapi msg, since it
4978 // is not routed to a RenderView.
4979 rph->Send(
4980 new ViewMsg_PpapiBrokerPermissionResult(ppb_broker_route_id, result));
4981 }
[email protected]d8415ad92012-08-23 14:40:504982}
4983
fsamuel833ee7c2015-02-13 23:40:404984void WebContentsImpl::OnBrowserPluginMessage(RenderFrameHost* render_frame_host,
4985 const IPC::Message& message) {
Lei Zhang6410ec1a2019-01-31 21:38:584986 CHECK(!browser_plugin_embedder_);
fsamuel60b42282015-03-10 03:29:144987 CreateBrowserPluginEmbedderIfNecessary();
fsamuel833ee7c2015-02-13 23:40:404988 browser_plugin_embedder_->OnMessageReceived(message, render_frame_host);
[email protected]19be7a62012-10-01 23:03:374989}
brettw4b461082016-11-19 18:55:164990#endif // BUILDFLAG(ENABLE_PLUGINS)
[email protected]19be7a62012-10-01 23:03:374991
[email protected]795c28972012-12-06 06:13:394992void WebContentsImpl::OnUpdateFaviconURL(
lukasza178ff092017-06-07 23:39:504993 RenderFrameHostImpl* source,
[email protected]795c28972012-12-06 06:13:394994 const std::vector<FaviconURL>& candidates) {
lukasza178ff092017-06-07 23:39:504995 // Ignore favicons for non-main frame.
4996 if (source->GetParent()) {
4997 NOTREACHED();
4998 return;
4999 }
5000
pkotwiczf9bc20592015-04-21 18:57:065001 // We get updated favicon URLs after the page stops loading. If a cross-site
5002 // navigation occurs while a page is still loading, the initial page
5003 // may stop loading and send us updated favicon URLs after the navigation
5004 // for the new page has committed.
lukasza178ff092017-06-07 23:39:505005 if (!source->IsCurrent())
pkotwiczf9bc20592015-04-21 18:57:065006 return;
5007
ericwilligersde386342016-10-19 02:35:095008 for (auto& observer : observers_)
5009 observer.DidUpdateFaviconURL(candidates);
[email protected]795c28972012-12-06 06:13:395010}
5011
qinmin72e8bd02016-10-21 19:35:375012void WebContentsImpl::SetIsOverlayContent(bool is_overlay_content) {
5013 is_overlay_content_ = is_overlay_content;
5014}
5015
Albert J. Wongfb64e142018-10-16 01:10:455016void WebContentsImpl::DidFirstVisuallyNonEmptyPaint(
5017 RenderViewHostImpl* source) {
Francois Dorayae33f2f2018-10-30 13:40:045018 // Set |did_first_visually_non_empty_paint_| before notifying observers so
5019 // they can see that CompletedFirstVisuallyNonEmptyPaint() is true.
5020 did_first_visually_non_empty_paint_ = true;
5021
nicka0ac8382016-12-15 23:59:235022 // TODO(nick): When this is ported to FrameHostMsg_, we should only listen if
5023 // |source| is the main frame.
ericwilligersde386342016-10-19 02:35:095024 for (auto& observer : observers_)
5025 observer.DidFirstVisuallyNonEmptyPaint();
yusufod41c5f92015-03-06 00:14:285026
5027 if (theme_color_ != last_sent_theme_color_) {
5028 // Theme color should have updated by now if there was one.
ericwilligersde386342016-10-19 02:35:095029 for (auto& observer : observers_)
5030 observer.DidChangeThemeColor(theme_color_);
yusufod41c5f92015-03-06 00:14:285031 last_sent_theme_color_ = theme_color_;
5032 }
[email protected]9f268072013-11-07 00:02:155033}
[email protected]d9030b82013-07-19 08:26:065034
Albert J. Wongfb64e142018-10-16 01:10:455035void WebContentsImpl::DidCommitAndDrawCompositorFrame(
Yiming Zhoudd60b0b2018-07-20 18:51:355036 RenderViewHostImpl* source) {
5037 for (auto& observer : observers_)
5038 observer.DidCommitAndDrawCompositorFrame();
5039}
5040
Adithya Srinivasan46b8a792019-02-01 14:47:235041bool WebContentsImpl::IsPortal() const {
5042 return portal();
5043}
5044
[email protected]17e286e2013-03-01 23:29:395045void WebContentsImpl::NotifyBeforeFormRepostWarningShow() {
ericwilligersde386342016-10-19 02:35:095046 for (auto& observer : observers_)
5047 observer.BeforeFormRepostWarningShow();
[email protected]17e286e2013-03-01 23:29:395048}
5049
[email protected]ec6c05f2013-10-23 18:41:575050void WebContentsImpl::ActivateAndShowRepostFormWarningDialog() {
5051 Activate();
5052 if (delegate_)
5053 delegate_->ShowRepostFormWarningDialog(this);
5054}
5055
[email protected]b4c84012014-04-28 19:51:105056bool WebContentsImpl::HasAccessedInitialDocument() {
5057 return has_accessed_initial_document_;
5058}
5059
afakhry6f0c1ec22016-07-14 13:55:135060void WebContentsImpl::UpdateTitleForEntry(NavigationEntry* entry,
[email protected]fcf75d42013-12-03 20:11:265061 const base::string16& title) {
[email protected]fcf75d42013-12-03 20:11:265062 base::string16 final_title;
Avi Drissman93002212017-09-27 03:20:525063 base::TrimWhitespace(title, base::TRIM_ALL, &final_title);
[email protected]420ae012009-04-24 05:16:325064
[email protected]987fc3a2011-05-26 14:18:095065 // If a page is created via window.open and never navigated,
5066 // there will be no navigation entry. In this situation,
[email protected]73eb2602012-02-09 19:50:555067 // |page_title_when_no_navigation_entry_| will be used for page title.
[email protected]987fc3a2011-05-26 14:18:095068 if (entry) {
[email protected]36fc0392011-12-25 03:59:515069 if (final_title == entry->GetTitle())
afakhry6f0c1ec22016-07-14 13:55:135070 return; // Nothing changed, don't bother.
[email protected]420ae012009-04-24 05:16:325071
[email protected]36fc0392011-12-25 03:59:515072 entry->SetTitle(final_title);
Avi Drissman93002212017-09-27 03:20:525073
5074 // The title for display may differ from the title just set; grab it.
5075 final_title = entry->GetTitleForDisplay();
[email protected]987fc3a2011-05-26 14:18:095076 } else {
5077 if (page_title_when_no_navigation_entry_ == final_title)
afakhry6f0c1ec22016-07-14 13:55:135078 return; // Nothing changed, don't bother.
[email protected]987fc3a2011-05-26 14:18:095079
5080 page_title_when_no_navigation_entry_ = final_title;
5081 }
[email protected]420ae012009-04-24 05:16:325082
[email protected]420ae012009-04-24 05:16:325083 // Lastly, set the title for the view.
[email protected]790e2fd2011-09-21 20:28:235084 view_->SetPageTitle(final_title);
[email protected]420ae012009-04-24 05:16:325085
ericwilligersde386342016-10-19 02:35:095086 for (auto& observer : observers_)
Avi Drissman93002212017-09-27 03:20:525087 observer.TitleWasSet(entry);
[email protected]66798902013-10-01 18:40:165088
afakhry6f0c1ec22016-07-14 13:55:135089 // Broadcast notifications when the UI should be updated.
5090 if (entry == controller_.GetEntryAtOffset(0))
5091 NotifyNavigationStateChanged(INVALIDATE_TYPE_TITLE);
[email protected]420ae012009-04-24 05:16:325092}
5093
fdegansa696e5112015-04-17 01:57:595094void WebContentsImpl::SendChangeLoadProgress() {
[email protected]960b0372014-05-19 18:01:005095 loading_last_progress_update_ = base::TimeTicks::Now();
[email protected]960b0372014-05-19 18:01:005096 if (delegate_)
fdegansa696e5112015-04-17 01:57:595097 delegate_->LoadProgressChanged(this, frame_tree_.load_progress());
[email protected]960b0372014-05-19 18:01:005098}
5099
5100void WebContentsImpl::ResetLoadProgressState() {
fdegans1d16355162015-03-26 11:58:345101 frame_tree_.ResetLoadProgress();
[email protected]960b0372014-05-19 18:01:005102 loading_weak_factory_.InvalidateWeakPtrs();
5103 loading_last_progress_update_ = base::TimeTicks();
5104}
5105
clamy44e84ce2016-02-22 15:38:255106// Notifies the RenderWidgetHost instance about the fact that the page is
5107// loading, or done loading.
5108void WebContentsImpl::LoadingStateChanged(bool to_different_document,
5109 bool due_to_interstitial,
5110 LoadNotificationDetails* details) {
Nate Chapin22ea6592019-03-05 22:29:025111 if (is_being_destroyed_)
5112 return;
clamy44e84ce2016-02-22 15:38:255113 // Do not send notifications about loading changes in the FrameTree while the
5114 // interstitial page is pausing the throbber.
creisadca9862017-06-16 19:12:115115 if (ShowingInterstitialPage() && interstitial_page_->pause_throbber() &&
clamy44e84ce2016-02-22 15:38:255116 !due_to_interstitial) {
5117 return;
5118 }
5119
5120 bool is_loading = IsLoading();
5121
5122 if (!is_loading) {
5123 load_state_ = net::LoadStateWithParam(net::LOAD_STATE_IDLE,
5124 base::string16());
5125 load_state_host_.clear();
5126 upload_size_ = 0;
5127 upload_position_ = 0;
5128 }
5129
5130 GetRenderManager()->SetIsLoading(is_loading);
5131
5132 waiting_for_response_ = is_loading;
5133 is_load_to_different_document_ = to_different_document;
5134
5135 if (delegate_)
5136 delegate_->LoadingStateChanged(this, to_different_document);
5137 NotifyNavigationStateChanged(INVALIDATE_TYPE_LOAD);
5138
5139 std::string url = (details ? details->url.possibly_invalid_spec() : "NULL");
5140 if (is_loading) {
5141 TRACE_EVENT_ASYNC_BEGIN2("browser,navigation", "WebContentsImpl Loading",
5142 this, "URL", url, "Main FrameTreeNode id",
5143 GetFrameTree()->root()->frame_tree_node_id());
ericwilligersde386342016-10-19 02:35:095144 for (auto& observer : observers_)
5145 observer.DidStartLoading();
clamy44e84ce2016-02-22 15:38:255146 } else {
5147 TRACE_EVENT_ASYNC_END1("browser,navigation", "WebContentsImpl Loading",
5148 this, "URL", url);
ericwilligersde386342016-10-19 02:35:095149 for (auto& observer : observers_)
5150 observer.DidStopLoading();
clamy44e84ce2016-02-22 15:38:255151 }
5152
5153 // TODO(avi): Remove. http://crbug.com/170921
5154 int type = is_loading ? NOTIFICATION_LOAD_START : NOTIFICATION_LOAD_STOP;
5155 NotificationDetails det = NotificationService::NoDetails();
5156 if (details)
5157 det = Details<LoadNotificationDetails>(details);
5158 NotificationService::current()->Notify(
5159 type, Source<NavigationController>(&controller_), det);
5160}
5161
[email protected]02d7b6e2014-06-24 21:01:505162void WebContentsImpl::NotifyViewSwapped(RenderViewHost* old_host,
5163 RenderViewHost* new_host) {
[email protected]420ae012009-04-24 05:16:325164 // After sending out a swap notification, we need to send a disconnect
5165 // notification so that clients that pick up a pointer to |this| can NULL the
5166 // pointer. See Bug 1230284.
5167 notify_disconnection_ = true;
ericwilligersde386342016-10-19 02:35:095168 for (auto& observer : observers_)
5169 observer.RenderViewHostChanged(old_host, new_host);
[email protected]da7a7182013-09-06 08:11:115170
Jinsuk Kimdc5284c22018-07-11 02:22:065171 view_->RenderViewHostChanged(old_host, new_host);
5172
Lucas Furukawa Gadanif9ea4fc2018-08-03 23:57:135173 // If this is an inner WebContents that has swapped views, we need to reattach
5174 // it to its outer WebContents.
5175 if (node_.outer_web_contents())
5176 ReattachToOuterWebContentsFrame();
5177
[email protected]7a846df2012-09-20 19:17:395178 // Ensure that the associated embedder gets cleared after a RenderViewHost
5179 // gets swapped, so we don't reuse the same embedder next time a
5180 // RenderViewHost is attached to this WebContents.
5181 RemoveBrowserPluginEmbedder();
[email protected]420ae012009-04-24 05:16:325182}
5183
[email protected]02d7b6e2014-06-24 21:01:505184void WebContentsImpl::NotifyFrameSwapped(RenderFrameHost* old_host,
Jinsuk Kim895b2c52018-07-03 06:49:165185 RenderFrameHost* new_host,
5186 bool is_main_frame) {
Bo Liu168c8642017-08-28 18:26:025187#if defined(OS_ANDROID)
Bo Liu16dd7b8322018-05-02 21:19:505188 // Copy importance from |old_host| if |new_host| is a main frame.
5189 if (old_host && !new_host->GetParent()) {
Bo Liu7c6779e92017-08-16 02:02:285190 static_cast<RenderFrameHostImpl*>(new_host)
5191 ->GetRenderWidgetHost()
Bo Liu16dd7b8322018-05-02 21:19:505192 ->SetImportance(static_cast<RenderFrameHostImpl*>(old_host)
5193 ->GetRenderWidgetHost()
5194 ->importance());
Bo Liu7c6779e92017-08-16 02:02:285195 }
Bo Liu168c8642017-08-28 18:26:025196#endif
ericwilligersde386342016-10-19 02:35:095197 for (auto& observer : observers_)
5198 observer.RenderFrameHostChanged(old_host, new_host);
[email protected]02d7b6e2014-06-24 21:01:505199}
5200
[email protected]da7a7182013-09-06 08:11:115201// TODO(avi): Remove this entire function because this notification is already
5202// covered by two observer functions. http://crbug.com/170921
[email protected]b172aee2012-04-10 17:05:265203void WebContentsImpl::NotifyDisconnected() {
[email protected]420ae012009-04-24 05:16:325204 if (!notify_disconnection_)
5205 return;
5206
5207 notify_disconnection_ = false;
[email protected]8ff00d72012-10-23 19:12:215208 NotificationService::current()->Notify(
5209 NOTIFICATION_WEB_CONTENTS_DISCONNECTED,
5210 Source<WebContents>(this),
5211 NotificationService::NoDetails());
[email protected]420ae012009-04-24 05:16:325212}
5213
[email protected]cbb1ef592013-06-05 19:49:465214void WebContentsImpl::NotifyNavigationEntryCommitted(
5215 const LoadCommittedDetails& load_details) {
ericwilligersde386342016-10-19 02:35:095216 for (auto& observer : observers_)
5217 observer.NavigationEntryCommitted(load_details);
[email protected]cbb1ef592013-06-05 19:49:465218}
5219
Sam McNally5c087a32017-08-25 01:46:145220void WebContentsImpl::NotifyNavigationEntryChanged(
5221 const EntryChangedDetails& change_details) {
5222 for (auto& observer : observers_)
5223 observer.NavigationEntryChanged(change_details);
5224}
5225
5226void WebContentsImpl::NotifyNavigationListPruned(
5227 const PrunedDetails& pruned_details) {
5228 for (auto& observer : observers_)
5229 observer.NavigationListPruned(pruned_details);
5230}
5231
Christian Dullweber1af31e62018-02-22 11:49:485232void WebContentsImpl::NotifyNavigationEntriesDeleted() {
5233 for (auto& observer : observers_)
5234 observer.NavigationEntriesDeleted();
5235}
5236
rockotf62002a2016-09-15 00:08:595237void WebContentsImpl::OnAssociatedInterfaceRequest(
5238 RenderFrameHost* render_frame_host,
5239 const std::string& interface_name,
5240 mojo::ScopedInterfaceEndpointHandle handle) {
5241 auto it = binding_sets_.find(interface_name);
5242 if (it != binding_sets_.end())
5243 it->second->OnRequestForFrame(render_frame_host, std::move(handle));
5244}
5245
Ben Goodger9e24929e2017-07-21 03:13:405246void WebContentsImpl::OnInterfaceRequest(
5247 RenderFrameHost* render_frame_host,
5248 const std::string& interface_name,
5249 mojo::ScopedMessagePipeHandle* interface_pipe) {
Joel Hockey85b379d2017-12-11 10:42:135250 registry_.TryBindInterface(interface_name, interface_pipe);
Ben Goodger9e24929e2017-07-21 03:13:405251 for (auto& observer : observers_) {
5252 observer.OnInterfaceRequestFromFrame(render_frame_host, interface_name,
5253 interface_pipe);
5254 if (!interface_pipe->is_valid())
5255 break;
5256 }
5257}
5258
Varun Khaneja5da94302019-06-25 04:48:105259void WebContentsImpl::OnDidBlockNavigation(
5260 const GURL& blocked_url,
5261 const GURL& initiator_url,
5262 blink::NavigationBlockedReason reason) {
Nicolas Dossou-gbetebcab2472017-09-09 12:28:405263 if (delegate_)
Varun Khaneja5da94302019-06-25 04:48:105264 delegate_->OnDidBlockNavigation(this, blocked_url, initiator_url, reason);
Nicolas Dossou-gbetebcab2472017-09-09 12:28:405265}
5266
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:415267const GURL& WebContentsImpl::GetMainFrameLastCommittedURL() {
[email protected]c31a84802014-04-03 15:55:495268 return GetLastCommittedURL();
5269}
5270
[email protected]b849847b2013-12-10 21:57:585271void WebContentsImpl::RenderFrameCreated(RenderFrameHost* render_frame_host) {
ericwilligersde386342016-10-19 02:35:095272 for (auto& observer : observers_)
5273 observer.RenderFrameCreated(render_frame_host);
dmazzonif8a31042016-12-03 00:52:135274 UpdateAccessibilityModeOnFrame(render_frame_host);
clamy91b7abf2016-03-24 15:17:455275
Becca Hughesd11d6502018-07-31 17:01:285276 if (display_cutout_host_impl_)
5277 display_cutout_host_impl_->RenderFrameCreated(render_frame_host);
5278
clamy91b7abf2016-03-24 15:17:455279 if (!render_frame_host->IsRenderFrameLive() || render_frame_host->GetParent())
5280 return;
5281
5282 NavigationEntry* entry = controller_.GetPendingEntry();
5283 if (entry && entry->IsViewSourceMode()) {
5284 // Put the renderer in view source mode.
5285 render_frame_host->Send(
5286 new FrameMsg_EnableViewSourceMode(render_frame_host->GetRoutingID()));
5287 }
[email protected]b849847b2013-12-10 21:57:585288}
5289
5290void WebContentsImpl::RenderFrameDeleted(RenderFrameHost* render_frame_host) {
creis3638897452017-05-09 22:07:505291 is_notifying_observers_ = true;
ericwilligersde386342016-10-19 02:35:095292 for (auto& observer : observers_)
5293 observer.RenderFrameDeleted(render_frame_host);
creis3638897452017-05-09 22:07:505294 is_notifying_observers_ = false;
zqzhang7b6f4f02016-12-07 22:10:225295#if BUILDFLAG(ENABLE_PLUGINS)
5296 pepper_playback_observer_->RenderFrameDeleted(render_frame_host);
5297#endif
Becca Hughesfd5d8f892018-06-14 18:23:365298
Becca Hughesd11d6502018-07-31 17:01:285299 if (display_cutout_host_impl_)
5300 display_cutout_host_impl_->RenderFrameDeleted(render_frame_host);
5301
Becca Hughesfd5d8f892018-06-14 18:23:365302 // Remove any fullscreen state that the frame has stored.
5303 FullscreenStateChanged(render_frame_host, false /* is_fullscreen */);
[email protected]b849847b2013-12-10 21:57:585304}
5305
[email protected]a09d53ce2014-01-31 00:46:425306void WebContentsImpl::ShowContextMenu(RenderFrameHost* render_frame_host,
5307 const ContextMenuParams& params) {
avie9fd8722017-04-10 16:17:585308 // If a renderer fires off a second command to show a context menu before the
5309 // first context menu is closed, just ignore it. https://crbug.com/707534
ekaramadf6750aa2017-06-06 18:29:425310 if (showing_context_menu_)
avie9fd8722017-04-10 16:17:585311 return;
5312
[email protected]077e704b2014-05-23 19:24:105313 ContextMenuParams context_menu_params(params);
[email protected]a09d53ce2014-01-31 00:46:425314 // Allow WebContentsDelegates to handle the context menu operation first.
Joel Klinghed0ea22ea2019-02-20 22:04:225315 if (delegate_ &&
5316 delegate_->HandleContextMenu(render_frame_host, context_menu_params))
[email protected]a09d53ce2014-01-31 00:46:425317 return;
5318
[email protected]077e704b2014-05-23 19:24:105319 render_view_host_delegate_view_->ShowContextMenu(render_frame_host,
5320 context_menu_params);
[email protected]a09d53ce2014-01-31 00:46:425321}
5322
Avi Drissman4891b852018-09-25 18:12:585323namespace {
5324// Normalizes the line endings: \r\n -> \n, lone \r -> \n.
5325base::string16 NormalizeLineBreaks(const base::string16& source) {
5326 static const base::NoDestructor<base::string16> kReturnNewline(
5327 base::ASCIIToUTF16("\r\n"));
5328 static const base::NoDestructor<base::string16> kReturn(
5329 base::ASCIIToUTF16("\r"));
5330 static const base::NoDestructor<base::string16> kNewline(
5331 base::ASCIIToUTF16("\n"));
5332
5333 std::vector<base::StringPiece16> pieces;
5334
5335 for (const auto& rn_line : base::SplitStringPieceUsingSubstr(
5336 source, *kReturnNewline, base::KEEP_WHITESPACE,
5337 base::SPLIT_WANT_ALL)) {
5338 auto r_lines = base::SplitStringPieceUsingSubstr(
5339 rn_line, *kReturn, base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL);
5340 std::move(std::begin(r_lines), std::end(r_lines),
5341 std::back_inserter(pieces));
5342 }
5343
5344 return base::JoinString(pieces, *kNewline);
5345}
5346} // namespace
5347
avi777ff452017-02-09 19:04:485348void WebContentsImpl::RunJavaScriptDialog(RenderFrameHost* render_frame_host,
5349 const base::string16& message,
5350 const base::string16& default_prompt,
avi777ff452017-02-09 19:04:485351 JavaScriptDialogType dialog_type,
5352 IPC::Message* reply_msg) {
Avi Drissman2ccbb402018-03-20 21:11:365353 // Ensure that if showing a dialog is the first thing that a page does, that
5354 // the contents of the previous page aren't shown behind it. This is required
5355 // because showing a dialog freezes the renderer, so no frames will be coming
5356 // from it. https://crbug.com/823353
5357 auto* render_widget_host_impl =
5358 static_cast<RenderFrameHostImpl*>(render_frame_host)
5359 ->GetRenderWidgetHost();
5360 if (render_widget_host_impl)
5361 render_widget_host_impl->ForceFirstFrameAfterNavigationTimeout();
5362
avi0720b02e2017-06-13 03:22:135363 // Running a dialog causes an exit to webpage-initiated fullscreen.
5364 // http://crbug.com/728276
Avi Drissmand18c51972018-08-15 18:32:095365 ForSecurityDropFullscreen();
avi0720b02e2017-06-13 03:22:135366
Avi Drissmane04d3992017-10-05 15:11:365367 auto callback =
5368 base::BindOnce(&WebContentsImpl::OnDialogClosed, base::Unretained(this),
5369 render_frame_host->GetProcess()->GetID(),
5370 render_frame_host->GetRoutingID(), reply_msg);
Pavel Feldman3c1842b2017-08-02 05:00:165371
Pavel Feldman3c1842b2017-08-02 05:00:165372 std::vector<protocol::PageHandler*> page_handlers =
5373 protocol::PageHandler::EnabledForWebContents(this);
5374
Pavel Feldmanfab3cb62017-10-05 01:02:015375 if (delegate_)
5376 dialog_manager_ = delegate_->GetJavaScriptDialogManager(this);
5377
Charlie Reis96077362019-07-04 00:34:045378 // While a JS message dialog is showing, defer commits in this WebContents.
5379 javascript_dialog_navigation_deferrer_ =
5380 std::make_unique<JavaScriptDialogNavigationDeferrer>();
5381
Pavel Feldmanfab3cb62017-10-05 01:02:015382 // Suppress JavaScript dialogs when requested. Also suppress messages when
5383 // showing an interstitial as it's shown over the previous page and we don't
5384 // want the hidden page's dialogs to interfere with the interstitial.
5385 bool should_suppress = ShowingInterstitialPage() ||
5386 (delegate_ && delegate_->ShouldSuppressDialogs(this));
Pavel Feldman1c587bc52018-04-04 21:16:055387 bool has_non_devtools_handlers = delegate_ && dialog_manager_;
5388 bool has_handlers = page_handlers.size() || has_non_devtools_handlers;
Pavel Feldmanfab3cb62017-10-05 01:02:015389 bool suppress_this_message = should_suppress || !has_handlers;
5390
5391 if (suppress_this_message) {
Avi Drissmane04d3992017-10-05 15:11:365392 std::move(callback).Run(true, false, base::string16());
Pavel Feldman3c1842b2017-08-02 05:00:165393 return;
5394 }
5395
5396 scoped_refptr<CloseDialogCallbackWrapper> wrapper =
Avi Drissmane04d3992017-10-05 15:11:365397 new CloseDialogCallbackWrapper(std::move(callback));
Pavel Feldman3c1842b2017-08-02 05:00:165398
5399 is_showing_javascript_dialog_ = true;
5400
Avi Drissman4891b852018-09-25 18:12:585401 base::string16 normalized_message = NormalizeLineBreaks(message);
5402
Pavel Feldman3c1842b2017-08-02 05:00:165403 for (auto* handler : page_handlers) {
Avi Drissmane04d3992017-10-05 15:11:365404 handler->DidRunJavaScriptDialog(
Avi Drissman4891b852018-09-25 18:12:585405 render_frame_host->GetLastCommittedURL(), normalized_message,
5406 default_prompt, dialog_type, has_non_devtools_handlers,
Avi Drissmane04d3992017-10-05 15:11:365407 base::BindOnce(&CloseDialogCallbackWrapper::Run, wrapper, false));
Pavel Feldman3c1842b2017-08-02 05:00:165408 }
5409
5410 if (dialog_manager_) {
[email protected]87de04b02014-04-08 22:14:495411 dialog_manager_->RunJavaScriptDialog(
Avi Drissman4891b852018-09-25 18:12:585412 this, render_frame_host, dialog_type, normalized_message,
5413 default_prompt,
Avi Drissmane04d3992017-10-05 15:11:365414 base::BindOnce(&CloseDialogCallbackWrapper::Run, wrapper, false),
5415 &suppress_this_message);
[email protected]87de04b02014-04-08 22:14:495416 }
5417
5418 if (suppress_this_message) {
5419 // If we are suppressing messages, just reply as if the user immediately
5420 // pressed "Cancel", passing true to |dialog_was_suppressed|.
Avi Drissmane04d3992017-10-05 15:11:365421 wrapper->Run(true, false, base::string16());
[email protected]87de04b02014-04-08 22:14:495422 }
[email protected]87de04b02014-04-08 22:14:495423}
5424
5425void WebContentsImpl::RunBeforeUnloadConfirm(
[email protected]39f2f9252014-05-03 00:54:015426 RenderFrameHost* render_frame_host,
[email protected]87de04b02014-04-08 22:14:495427 bool is_reload,
5428 IPC::Message* reply_msg) {
Avi Drissman2ccbb402018-03-20 21:11:365429 // Ensure that if showing a dialog is the first thing that a page does, that
5430 // the contents of the previous page aren't shown behind it. This is required
5431 // because showing a dialog freezes the renderer, so no frames will be coming
5432 // from it. https://crbug.com/823353
5433 auto* render_widget_host_impl =
5434 static_cast<RenderFrameHostImpl*>(render_frame_host)
5435 ->GetRenderWidgetHost();
5436 if (render_widget_host_impl)
5437 render_widget_host_impl->ForceFirstFrameAfterNavigationTimeout();
5438
avi0720b02e2017-06-13 03:22:135439 // Running a dialog causes an exit to webpage-initiated fullscreen.
5440 // http://crbug.com/728276
Avi Drissmand18c51972018-08-15 18:32:095441 ForSecurityDropFullscreen();
avi0720b02e2017-06-13 03:22:135442
[email protected]39f2f9252014-05-03 00:54:015443 RenderFrameHostImpl* rfhi =
5444 static_cast<RenderFrameHostImpl*>(render_frame_host);
[email protected]87de04b02014-04-08 22:14:495445 if (delegate_)
5446 delegate_->WillRunBeforeUnloadConfirm();
5447
Avi Drissmane04d3992017-10-05 15:11:365448 auto callback =
5449 base::BindOnce(&WebContentsImpl::OnDialogClosed, base::Unretained(this),
5450 render_frame_host->GetProcess()->GetID(),
5451 render_frame_host->GetRoutingID(), reply_msg);
Pavel Feldman3c1842b2017-08-02 05:00:165452
Pavel Feldman3c1842b2017-08-02 05:00:165453 std::vector<protocol::PageHandler*> page_handlers =
5454 protocol::PageHandler::EnabledForWebContents(this);
5455
Pavel Feldmanfab3cb62017-10-05 01:02:015456 if (delegate_)
5457 dialog_manager_ = delegate_->GetJavaScriptDialogManager(this);
5458
Charlie Reis96077362019-07-04 00:34:045459 // While a JS beforeunload dialog is showing, defer commits in this
5460 // WebContents.
5461 javascript_dialog_navigation_deferrer_ =
5462 std::make_unique<JavaScriptDialogNavigationDeferrer>();
5463
Pavel Feldmanfab3cb62017-10-05 01:02:015464 bool should_suppress = ShowingInterstitialPage() || !rfhi->is_active() ||
5465 (delegate_ && delegate_->ShouldSuppressDialogs(this));
Pavel Feldman1c587bc52018-04-04 21:16:055466 bool has_non_devtools_handlers = delegate_ && dialog_manager_;
5467 bool has_handlers = page_handlers.size() || has_non_devtools_handlers;
Pavel Feldmanfab3cb62017-10-05 01:02:015468 if (should_suppress || !has_handlers) {
Avi Drissmane04d3992017-10-05 15:11:365469 std::move(callback).Run(false, true, base::string16());
[email protected]87de04b02014-04-08 22:14:495470 return;
5471 }
5472
5473 is_showing_before_unload_dialog_ = true;
Pavel Feldman3c1842b2017-08-02 05:00:165474
5475 scoped_refptr<CloseDialogCallbackWrapper> wrapper =
Avi Drissmane04d3992017-10-05 15:11:365476 new CloseDialogCallbackWrapper(std::move(callback));
Pavel Feldman3c1842b2017-08-02 05:00:165477
5478 GURL frame_url = rfhi->GetLastCommittedURL();
5479 for (auto* handler : page_handlers) {
Avi Drissmane04d3992017-10-05 15:11:365480 handler->DidRunBeforeUnloadConfirm(
Pavel Feldman1c587bc52018-04-04 21:16:055481 frame_url, has_non_devtools_handlers,
Avi Drissmane04d3992017-10-05 15:11:365482 base::BindOnce(&CloseDialogCallbackWrapper::Run, wrapper, false));
Pavel Feldman3c1842b2017-08-02 05:00:165483 }
5484
5485 if (dialog_manager_) {
Avi Drissmane04d3992017-10-05 15:11:365486 dialog_manager_->RunBeforeUnloadDialog(
Avi Drissmanff9ed752017-11-28 21:18:125487 this, render_frame_host, is_reload,
Avi Drissmane04d3992017-10-05 15:11:365488 base::BindOnce(&CloseDialogCallbackWrapper::Run, wrapper, false));
Pavel Feldman3c1842b2017-08-02 05:00:165489 }
[email protected]87de04b02014-04-08 22:14:495490}
5491
Kent Tamurac260c9a2018-09-14 09:10:565492void WebContentsImpl::RunFileChooser(
5493 RenderFrameHost* render_frame_host,
Kent Tamura512a27e2018-10-04 00:49:325494 std::unique_ptr<content::FileSelectListener> listener,
Kent Tamurac260c9a2018-09-14 09:10:565495 const blink::mojom::FileChooserParams& params) {
Avi Drissman3d41e7712018-08-27 21:18:085496 // Any explicit focusing of another window while this WebContents is in
5497 // fullscreen can be used to confuse the user, so drop fullscreen.
5498 ForSecurityDropFullscreen();
5499
Andrey Lushnikov81b992e2019-06-27 23:27:215500 RenderFrameHostImpl* rfhi =
5501 static_cast<RenderFrameHostImpl*>(render_frame_host);
5502 if (devtools_instrumentation::InterceptFileChooser(rfhi, &listener, params))
5503 return;
5504
naskoada75b22016-06-11 16:09:465505 if (delegate_)
Kent Tamura512a27e2018-10-04 00:49:325506 delegate_->RunFileChooser(render_frame_host, std::move(listener), params);
5507 else
5508 listener->FileSelectionCanceled();
naskoada75b22016-06-11 16:09:465509}
5510
[email protected]a86c0e962013-12-17 17:10:395511WebContents* WebContentsImpl::GetAsWebContents() {
5512 return this;
5513}
5514
mcnee336ea2c2017-05-23 22:50:595515#if !defined(OS_ANDROID)
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:415516double WebContentsImpl::GetPendingPageZoomLevel() {
wjmaclean64951902016-04-29 20:59:125517 NavigationEntry* pending_entry = GetController().GetPendingEntry();
5518 if (!pending_entry)
5519 return HostZoomMap::GetZoomLevel(this);
5520
5521 GURL url = pending_entry->GetURL();
5522 return HostZoomMap::GetForWebContents(this)->GetZoomLevelForHostAndScheme(
5523 url.scheme(), net::GetHostOrSpecFromURL(url));
5524}
mcnee336ea2c2017-05-23 22:50:595525#endif // !defined(OS_ANDROID)
wjmaclean64951902016-04-29 20:59:125526
qinmin72e8bd02016-10-21 19:35:375527bool WebContentsImpl::HideDownloadUI() const {
5528 return is_overlay_content_;
5529}
5530
mlamouri5cd9ae82017-02-18 11:05:095531bool WebContentsImpl::HasPersistentVideo() const {
5532 return has_persistent_video_;
5533}
5534
Ian Vollick5d5c37f2019-06-19 22:50:575535bool WebContentsImpl::IsSpatialNavigationDisabled() const {
5536 return is_spatial_navigation_disabled_;
5537}
5538
Alex Moshchuk27caae82017-09-11 23:11:185539RenderFrameHost* WebContentsImpl::GetPendingMainFrame() {
clamy610c63b32017-12-22 15:05:185540 return GetRenderManager()->speculative_frame_host();
Alex Moshchuk27caae82017-09-11 23:11:185541}
5542
mlamourie52c1b542017-02-23 14:57:505543bool WebContentsImpl::HasActiveEffectivelyFullscreenVideo() const {
5544 return media_web_contents_observer_->HasActiveEffectivelyFullscreenVideo();
5545}
5546
François Beaufortad6c5232018-02-26 11:00:445547bool WebContentsImpl::IsPictureInPictureAllowedForFullscreenVideo() const {
5548 return media_web_contents_observer_
5549 ->IsPictureInPictureAllowedForFullscreenVideo();
5550}
5551
ekaramada110f642016-12-21 19:47:285552bool WebContentsImpl::IsFocusedElementEditable() {
5553 RenderFrameHostImpl* frame = GetFocusedFrame();
5554 return frame && frame->has_focused_editable_element();
5555}
5556
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:415557bool WebContentsImpl::IsShowingContextMenu() {
ekaramadf6750aa2017-06-06 18:29:425558 return showing_context_menu_;
5559}
5560
5561void WebContentsImpl::SetShowingContextMenu(bool showing) {
5562 DCHECK_NE(showing_context_menu_, showing);
5563 showing_context_menu_ = showing;
5564
5565 if (auto* view = GetRenderWidgetHostView()) {
5566 // Notify the main frame's RWHV to run the platform-specific code, if any.
5567 static_cast<RenderWidgetHostViewBase*>(view)->SetShowingContextMenu(
5568 showing);
5569 }
5570}
5571
ekaramada110f642016-12-21 19:47:285572void WebContentsImpl::ClearFocusedElement() {
5573 if (auto* frame = GetFocusedFrame())
5574 frame->ClearFocusedElement();
5575}
5576
[email protected]7912e822014-04-16 02:37:035577bool WebContentsImpl::IsNeverVisible() {
5578 if (!delegate_)
5579 return false;
5580 return delegate_->IsNeverVisible(this);
5581}
5582
[email protected]5a3bdf52012-05-24 15:12:575583RenderViewHostDelegateView* WebContentsImpl::GetDelegateView() {
5584 return render_view_host_delegate_view_;
[email protected]420ae012009-04-24 05:16:325585}
5586
Leon Hanc819dc62019-01-28 04:30:195587blink::mojom::RendererPreferences WebContentsImpl::GetRendererPrefs(
[email protected]8ff00d72012-10-23 19:12:215588 BrowserContext* browser_context) const {
[email protected]840b1512009-07-21 16:53:465589 return renderer_preferences_;
[email protected]80d96fa2009-06-10 22:34:515590}
5591
[email protected]7a846df2012-09-20 19:17:395592void WebContentsImpl::RemoveBrowserPluginEmbedder() {
Lei Zhang6410ec1a2019-01-31 21:38:585593 browser_plugin_embedder_.reset();
[email protected]7a846df2012-09-20 19:17:395594}
5595
Lucas Furukawa Gadani2ec00c82018-12-14 15:53:165596RenderFrameHostImpl* WebContentsImpl::GetOuterWebContentsFrame() {
5597 if (GetOuterDelegateFrameTreeNodeId() ==
5598 FrameTreeNode::kFrameTreeNodeInvalidId) {
5599 return nullptr;
5600 }
5601
5602 FrameTreeNode* outer_node =
5603 FrameTreeNode::GloballyFindByID(GetOuterDelegateFrameTreeNodeId());
5604 // The outer node should be in the outer WebContents.
5605 DCHECK_EQ(outer_node->frame_tree(), GetOuterWebContents()->GetFrameTree());
5606 return outer_node->parent()->current_frame_host();
5607}
5608
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:415609WebContentsImpl* WebContentsImpl::GetOuterWebContents() {
lfg601233692017-03-06 22:45:445610 if (GuestMode::IsCrossProcessFrameGuest(this))
5611 return node_.outer_web_contents();
ekaramad1beabecc2016-09-23 20:18:175612
5613 if (browser_plugin_guest_)
5614 return browser_plugin_guest_->embedder_web_contents();
5615
Lucas Furukawa Gadani105de1e812018-05-31 19:38:395616 return node_.outer_web_contents();
avallee0206f782016-07-28 18:55:335617}
5618
Lucas Furukawa Gadani2ec00c82018-12-14 15:53:165619std::vector<WebContents*> WebContentsImpl::GetInnerWebContents() {
5620 std::vector<WebContents*> all_inner_contents;
5621 if (browser_plugin_embedder_) {
Sean Topping7df73952019-05-16 17:21:165622 BrowserPluginGuestManager* guest_manager =
5623 GetBrowserContext()->GetGuestManager();
5624 if (guest_manager) {
5625 guest_manager->ForEachGuest(
5626 this,
5627 base::BindRepeating(&GetInnerWebContentsHelper, &all_inner_contents));
5628 }
Lucas Furukawa Gadani2ec00c82018-12-14 15:53:165629 }
5630 const auto& inner_contents = node_.GetInnerWebContents();
5631 all_inner_contents.insert(all_inner_contents.end(), inner_contents.begin(),
5632 inner_contents.end());
5633 return all_inner_contents;
5634}
5635
avallee0206f782016-07-28 18:55:335636WebContentsImpl* WebContentsImpl::GetFocusedWebContents() {
lfg601233692017-03-06 22:45:445637 return GetOutermostWebContents()->node_.focused_web_contents();
avallee0206f782016-07-28 18:55:335638}
5639
avallee9993fca2016-11-17 06:16:505640bool WebContentsImpl::ContainsOrIsFocusedWebContents() {
5641 for (WebContentsImpl* focused_contents = GetFocusedWebContents();
5642 focused_contents;
5643 focused_contents = focused_contents->GetOuterWebContents()) {
5644 if (focused_contents == this)
5645 return true;
5646 }
5647
5648 return false;
5649}
5650
avallee0206f782016-07-28 18:55:335651WebContentsImpl* WebContentsImpl::GetOutermostWebContents() {
5652 WebContentsImpl* root = this;
5653 while (root->GetOuterWebContents())
5654 root = root->GetOuterWebContents();
5655 return root;
5656}
5657
avalleed68cdd12017-06-21 22:07:275658void WebContentsImpl::FocusOuterAttachmentFrameChain() {
5659 WebContentsImpl* outer_contents = GetOuterWebContents();
5660 if (!outer_contents)
5661 return;
5662
5663 FrameTreeNode* outer_node =
5664 FrameTreeNode::GloballyFindByID(GetOuterDelegateFrameTreeNodeId());
5665 outer_contents->frame_tree_.SetFocusedFrame(outer_node, nullptr);
5666
5667 // For a browser initiated focus change, let embedding renderer know of the
5668 // change. Otherwise, if the currently focused element is just across a
5669 // process boundary in focus order, it will not be possible to move across
5670 // that boundary. This is because the target element will already be focused
5671 // (that renderer was not notified) and drop the event.
5672 if (GetRenderManager()->GetProxyToOuterDelegate())
5673 GetRenderManager()->GetProxyToOuterDelegate()->SetFocusedFrame();
5674
5675 outer_contents->FocusOuterAttachmentFrameChain();
5676}
5677
Sean Toppingbbc4a2bd2019-01-30 02:16:145678void WebContentsImpl::InnerWebContentsCreated(WebContents* inner_web_contents) {
Shivani Sharma985474f2019-05-23 21:36:475679 had_inner_webcontents_ = true;
Sean Toppingbbc4a2bd2019-01-30 02:16:145680 for (auto& observer : observers_)
5681 observer.InnerWebContentsCreated(inner_web_contents);
5682}
5683
[email protected]b172aee2012-04-10 17:05:265684void WebContentsImpl::RenderViewCreated(RenderViewHost* render_view_host) {
[email protected]86f98a22013-03-20 14:35:005685 if (delegate_)
[email protected]e85165c642014-06-10 14:34:315686 view_->SetOverscrollControllerEnabled(CanOverscrollContent());
[email protected]a6b73c62013-02-11 23:05:085687
[email protected]8ff00d72012-10-23 19:12:215688 NotificationService::current()->Notify(
5689 NOTIFICATION_WEB_CONTENTS_RENDER_VIEW_HOST_CREATED,
5690 Source<WebContents>(this),
5691 Details<RenderViewHost>(render_view_host));
[email protected]420ae012009-04-24 05:16:325692
[email protected]60780f412013-02-25 16:34:105693 view_->RenderViewCreated(render_view_host);
[email protected]11f764bb2011-05-25 19:45:265694
ericwilligersde386342016-10-19 02:35:095695 for (auto& observer : observers_)
5696 observer.RenderViewCreated(render_view_host);
Joel Einbinder848001c2017-09-19 21:39:315697 if (delegate_)
5698 RenderFrameDevToolsAgentHost::WebContentsCreated(this);
[email protected]420ae012009-04-24 05:16:325699}
5700
[email protected]b172aee2012-04-10 17:05:265701void WebContentsImpl::RenderViewReady(RenderViewHost* rvh) {
[email protected]151a63d2011-12-20 22:32:525702 if (rvh != GetRenderViewHost()) {
[email protected]420ae012009-04-24 05:16:325703 // Don't notify the world, since this came from a renderer in the
5704 // background.
5705 return;
5706 }
5707
dmazzoni62111452017-04-28 23:46:555708 RenderWidgetHostViewBase* rwhv =
5709 static_cast<RenderWidgetHostViewBase*>(GetRenderWidgetHostView());
5710 if (rwhv)
5711 rwhv->SetMainFrameAXTreeID(GetMainFrame()->GetAXTreeID());
5712
[email protected]da7a7182013-09-06 08:11:115713 notify_disconnection_ = true;
[email protected]da7a7182013-09-06 08:11:115714
[email protected]be1f56ab2011-12-22 06:55:315715 bool was_crashed = IsCrashed();
[email protected]443b80e2010-12-14 00:42:235716 SetIsCrashed(base::TERMINATION_STATUS_STILL_RUNNING, 0);
[email protected]9d8a4642009-07-29 17:25:305717
5718 // Restore the focus to the tab (otherwise the focus will be on the top
5719 // window).
[email protected]484ae5912010-09-29 19:16:145720 if (was_crashed && !FocusLocationBarByDefault() &&
5721 (!delegate_ || delegate_->ShouldFocusPageAfterCrash())) {
[email protected]f3615f02013-02-26 06:09:065722 view_->Focus();
[email protected]484ae5912010-09-29 19:16:145723 }
[email protected]32ded2212011-11-10 18:51:435724
ericwilligersde386342016-10-19 02:35:095725 for (auto& observer : observers_)
5726 observer.RenderViewReady();
Jinsuk Kim895b2c52018-07-03 06:49:165727
5728 view_->RenderViewReady();
[email protected]420ae012009-04-24 05:16:325729}
5730
[email protected]ec6a7eb2013-04-22 17:34:225731void WebContentsImpl::RenderViewTerminated(RenderViewHost* rvh,
5732 base::TerminationStatus status,
5733 int error_code) {
[email protected]151a63d2011-12-20 22:32:525734 if (rvh != GetRenderViewHost()) {
[email protected]420ae012009-04-24 05:16:325735 // The pending page's RenderViewHost is gone.
5736 return;
5737 }
5738
[email protected]2fcdcc32014-03-05 02:14:075739 // Ensure fullscreen mode is exited in the |delegate_| since a crashed
5740 // renderer may not have made a clean exit.
alexmos5dd617d2016-06-07 04:21:155741 if (IsFullscreenForCurrentTab())
bokanece34a82016-01-28 19:49:465742 ExitFullscreenMode(false);
[email protected]2fcdcc32014-03-05 02:14:075743
Jennifer Apacibleb4b972a2018-06-07 04:41:025744 // Ensure any video in Picture-in-Picture is exited in the |delegate_| since
5745 // a crashed renderer may not have made a clean exit.
5746 if (HasPictureInPictureVideo())
5747 ExitPictureInPicture();
5748
[email protected]698191dc2014-02-25 01:06:135749 // Cancel any visible dialogs so they are not left dangling over the sad tab.
creis89a0f782015-05-27 16:13:175750 CancelActiveAndPendingDialogs();
[email protected]698191dc2014-02-25 01:06:135751
dalecurtis3d1e1302017-02-20 22:33:255752 audio_stream_monitor_.RenderProcessGone(rvh->GetProcess()->GetID());
5753
[email protected]960b0372014-05-19 18:01:005754 // Reset the loading progress. TODO(avi): What does it mean to have a
5755 // "renderer crash" when there is more than one renderer process serving a
5756 // webpage? Once this function is called at a more granular frame level, we
5757 // probably will need to more granularly reset the state here.
5758 ResetLoadProgressState();
clamy44e84ce2016-02-22 15:38:255759 NotifyDisconnected();
5760 SetIsCrashed(status, error_code);
[email protected]960b0372014-05-19 18:01:005761
ericwilligersde386342016-10-19 02:35:095762 for (auto& observer : observers_)
5763 observer.RenderProcessGone(GetCrashedStatus());
[email protected]420ae012009-04-24 05:16:325764}
5765
[email protected]b172aee2012-04-10 17:05:265766void WebContentsImpl::RenderViewDeleted(RenderViewHost* rvh) {
ericwilligersde386342016-10-19 02:35:095767 for (auto& observer : observers_)
5768 observer.RenderViewDeleted(rvh);
[email protected]2e4633c2009-07-09 16:58:065769}
5770
sky8b00392d2015-01-10 00:30:285771void WebContentsImpl::UpdateTargetURL(RenderViewHost* render_view_host,
5772 const GURL& url) {
5773 if (fullscreen_widget_routing_id_ != MSG_ROUTING_NONE) {
Lukasz Anforowiczfd707142018-02-07 19:46:135774 // If we're in flash fullscreen (i.e. Pepper plugin fullscreen) only update
5775 // the url if it's from the fullscreen renderer.
sky8b00392d2015-01-10 00:30:285776 RenderWidgetHostView* fs = GetFullscreenRenderWidgetHostView();
avif9ab5d942015-10-15 14:05:445777 if (fs && fs->GetRenderWidgetHost() != render_view_host->GetWidget())
sky8b00392d2015-01-10 00:30:285778 return;
5779 }
Lukasz Anforowiczfd707142018-02-07 19:46:135780
5781 // In case of racey updates from multiple RenderViewHosts, the last URL should
5782 // be shown - see also some discussion in https://crbug.com/807776.
5783 if (!url.is_valid() && render_view_host != view_that_set_last_target_url_)
5784 return;
5785 view_that_set_last_target_url_ = url.is_valid() ? render_view_host : nullptr;
5786
[email protected]6934a702011-12-20 00:04:515787 if (delegate_)
avi450ce00e2014-09-24 01:58:525788 delegate_->UpdateTargetURL(this, url);
[email protected]420ae012009-04-24 05:16:325789}
5790
Lukasz Anforowiczfd707142018-02-07 19:46:135791void WebContentsImpl::ClearTargetURL() {
5792 view_that_set_last_target_url_ = nullptr;
5793 if (delegate_)
5794 delegate_->UpdateTargetURL(this, GURL());
5795}
5796
[email protected]b172aee2012-04-10 17:05:265797void WebContentsImpl::Close(RenderViewHost* rvh) {
[email protected]60780f412013-02-25 16:34:105798#if defined(OS_MACOSX)
[email protected]07707302009-11-06 00:50:295799 // The UI may be in an event-tracking loop, such as between the
5800 // mouse-down and mouse-up in text selection or a button click.
5801 // Defer the close until after tracking is complete, so that we
5802 // don't free objects out from under the UI.
[email protected]07707302009-11-06 00:50:295803 // TODO(shess): This could get more fine-grained. For instance,
5804 // closing a tab in another window while selecting text in the
5805 // current window's Omnibox should be just fine.
Christopher Cameron9ae31e0d2019-03-01 20:01:195806 if (view_->CloseTabAfterEventTrackingIfNeeded())
[email protected]07707302009-11-06 00:50:295807 return;
[email protected]60780f412013-02-25 16:34:105808#endif
[email protected]07707302009-11-06 00:50:295809
[email protected]420ae012009-04-24 05:16:325810 // Ignore this if it comes from a RenderViewHost that we aren't showing.
[email protected]151a63d2011-12-20 22:32:525811 if (delegate_ && rvh == GetRenderViewHost())
[email protected]6934a702011-12-20 00:04:515812 delegate_->CloseContents(this);
[email protected]420ae012009-04-24 05:16:325813}
5814
Avi Drissman014dec72018-06-07 02:34:015815void WebContentsImpl::RequestSetBounds(const gfx::Rect& new_bounds) {
5816 if (delegate_)
5817 delegate_->SetContentsBounds(this, new_bounds);
[email protected]420ae012009-04-24 05:16:325818}
5819
fdegansa696e5112015-04-17 01:57:595820void WebContentsImpl::DidStartLoading(FrameTreeNode* frame_tree_node,
[email protected]e3b10d12014-03-28 16:06:095821 bool to_different_document) {
Evan Stadec3b26022017-07-14 19:50:245822 LoadingStateChanged(frame_tree_node->IsMainFrame() && to_different_document,
5823 false, nullptr);
dmazzonibf8cec42015-02-08 08:28:085824
Xiyuan Xia2a66b112018-12-06 15:52:165825 // Reset the focus state from DidStartNavigation to false if a new load starts
5826 // afterward, in case loading logic triggers a FocusLocationBarByDefault call.
5827 should_focus_location_bar_by_default_ = false;
5828
dmazzonibf8cec42015-02-08 08:28:085829 // Notify accessibility that the user is navigating away from the
5830 // current document.
Dominic Farolinof87ec2a2019-07-04 09:18:095831 // TODO(domfarolino, dmazzoni): Do this using WebContentsObserver. See
5832 // https://crbug.com/981271.
dmazzonibf8cec42015-02-08 08:28:085833 BrowserAccessibilityManager* manager =
fdegansa696e5112015-04-17 01:57:595834 frame_tree_node->current_frame_host()->browser_accessibility_manager();
dmazzonibf8cec42015-02-08 08:28:085835 if (manager)
5836 manager->UserIsNavigatingAway();
[email protected]420ae012009-04-24 05:16:325837}
5838
fdegansc73eec22015-03-19 14:47:545839void WebContentsImpl::DidStopLoading() {
dcheng59716272016-04-09 05:19:085840 std::unique_ptr<LoadNotificationDetails> details;
[email protected]420ae012009-04-24 05:16:325841
[email protected]9595fd82013-04-19 21:28:495842 // Use the last committed entry rather than the active one, in case a
5843 // pending entry has been created.
5844 NavigationEntry* entry = controller_.GetLastCommittedEntry();
[email protected]3691e5cf2014-01-22 10:16:205845 Navigator* navigator = frame_tree_.root()->navigator();
[email protected]9595fd82013-04-19 21:28:495846
[email protected]420ae012009-04-24 05:16:325847 // An entry may not exist for a stop when loading an initial blank page or
5848 // if an iframe injected by script into a blank page finishes loading.
5849 if (entry) {
[email protected]3691e5cf2014-01-22 10:16:205850 base::TimeDelta elapsed =
5851 base::TimeTicks::Now() - navigator->GetCurrentLoadStart();
[email protected]420ae012009-04-24 05:16:325852
5853 details.reset(new LoadNotificationDetails(
[email protected]36fc0392011-12-25 03:59:515854 entry->GetVirtualURL(),
[email protected]420ae012009-04-24 05:16:325855 elapsed,
5856 &controller_,
5857 controller_.GetCurrentEntryIndex()));
5858 }
5859
clamy44e84ce2016-02-22 15:38:255860 LoadingStateChanged(true, false, details.get());
[email protected]420ae012009-04-24 05:16:325861}
5862
fdegansa696e5112015-04-17 01:57:595863void WebContentsImpl::DidChangeLoadProgress() {
Nate Chapin22ea6592019-03-05 22:29:025864 if (is_being_destroyed_)
5865 return;
fdegansa696e5112015-04-17 01:57:595866 double load_progress = frame_tree_.load_progress();
5867
5868 // The delegate is notified immediately for the first and last updates. Also,
5869 // since the message loop may be pretty busy when a page is loaded, it might
5870 // not execute a posted task in a timely manner so the progress report is sent
5871 // immediately if enough time has passed.
5872 base::TimeDelta min_delay =
5873 base::TimeDelta::FromMilliseconds(kMinimumDelayBetweenLoadingUpdatesMS);
5874 bool delay_elapsed = loading_last_progress_update_.is_null() ||
5875 base::TimeTicks::Now() - loading_last_progress_update_ > min_delay;
5876
5877 if (load_progress == 0.0 || load_progress == 1.0 || delay_elapsed) {
5878 // If there is a pending task to send progress, it is now obsolete.
5879 loading_weak_factory_.InvalidateWeakPtrs();
5880
5881 // Notify the load progress change.
5882 SendChangeLoadProgress();
5883
5884 // Clean-up the states if needed.
5885 if (load_progress == 1.0)
5886 ResetLoadProgressState();
5887 return;
5888 }
5889
5890 if (loading_weak_factory_.HasWeakPtrs())
5891 return;
5892
skyostil95082a62015-06-05 19:53:075893 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
tzike2aca992017-09-05 08:50:545894 FROM_HERE,
5895 base::BindOnce(&WebContentsImpl::SendChangeLoadProgress,
5896 loading_weak_factory_.GetWeakPtr()),
fdegansa696e5112015-04-17 01:57:595897 min_delay);
5898}
5899
avid6d88b912017-01-13 00:16:005900std::vector<std::unique_ptr<NavigationThrottle>>
5901WebContentsImpl::CreateThrottlesForNavigation(
clamycbf524d42016-09-27 12:48:355902 NavigationHandle* navigation_handle) {
Charlie Reis96077362019-07-04 00:34:045903 auto throttles = GetContentClient()->browser()->CreateThrottlesForNavigation(
clamycbf524d42016-09-27 12:48:355904 navigation_handle);
Charlie Reis96077362019-07-04 00:34:045905
5906 // This is not a normal place to be adding a throttle. However, in the case
5907 // javascript dialogs, related logic is present in the web_contents/ layer,
5908 // and the purpose of the throttle is to ensure that navigation commits are
5909 // deferred for the entire WebContents. Most throttles are either added by
5910 // the embederrer outside of content/, or are per-frame and added by
5911 // NavigationThrottleRunner.
5912 std::unique_ptr<content::NavigationThrottle> dialog_throttle =
5913 JavaScriptDialogNavigationThrottle::MaybeCreateThrottleFor(
5914 navigation_handle);
5915 if (dialog_throttle)
5916 throttles.push_back(std::move(dialog_throttle));
5917
5918 return throttles;
clamycbf524d42016-09-27 12:48:355919}
5920
clamy1e5574e2016-09-29 16:48:445921std::unique_ptr<NavigationUIData> WebContentsImpl::GetNavigationUIData(
5922 NavigationHandle* navigation_handle) {
clamy1e5574e2016-09-29 16:48:445923 return GetContentClient()->browser()->GetNavigationUIData(navigation_handle);
5924}
5925
pdr15f5b5b2017-06-20 00:58:005926void WebContentsImpl::DidCancelLoading() {
5927 controller_.DiscardNonCommittedEntries();
5928
5929 // Update the URL display.
5930 NotifyNavigationStateChanged(INVALIDATE_TYPE_URL);
5931}
5932
[email protected]59167c22013-06-03 18:07:325933void WebContentsImpl::DidAccessInitialDocument() {
[email protected]b4c84012014-04-28 19:51:105934 has_accessed_initial_document_ = true;
5935
[email protected]d1371bf8e2014-04-19 03:26:245936 // We may have left a failed browser-initiated navigation in the address bar
5937 // to let the user edit it and try again. Clear it now that content might
5938 // show up underneath it.
5939 if (!IsLoading() && controller_.GetPendingEntry())
avi45a72532015-04-07 21:01:455940 controller_.DiscardPendingEntry(false);
[email protected]d1371bf8e2014-04-19 03:26:245941
[email protected]59167c22013-06-03 18:07:325942 // Update the URL display.
thestigc4cac8f2014-09-04 21:17:505943 NotifyNavigationStateChanged(INVALIDATE_TYPE_URL);
[email protected]59167c22013-06-03 18:07:325944}
5945
alexmosbe2f4c32015-03-10 02:30:235946void WebContentsImpl::DidChangeName(RenderFrameHost* render_frame_host,
5947 const std::string& name) {
ericwilligersde386342016-10-19 02:35:095948 for (auto& observer : observers_)
5949 observer.FrameNameChanged(render_frame_host, name);
alexmosbe2f4c32015-03-10 02:30:235950}
5951
John Delaneyedd8d6c2019-01-25 00:23:575952void WebContentsImpl::DidReceiveFirstUserActivation(
5953 RenderFrameHost* render_frame_host) {
5954 for (auto& observer : observers_)
5955 observer.FrameReceivedFirstUserActivation(render_frame_host);
5956}
5957
John Delaney0146a742019-01-25 19:25:285958void WebContentsImpl::DidChangeDisplayState(RenderFrameHost* render_frame_host,
5959 bool is_display_none) {
5960 for (auto& observer : observers_)
5961 observer.FrameDisplayStateChanged(render_frame_host, is_display_none);
5962}
5963
5964void WebContentsImpl::FrameSizeChanged(RenderFrameHost* render_frame_host,
5965 const gfx::Size& frame_size) {
5966 for (auto& observer : observers_)
5967 observer.FrameSizeChanged(render_frame_host, frame_size);
5968}
5969
[email protected]39f2f9252014-05-03 00:54:015970void WebContentsImpl::DocumentOnLoadCompleted(
[email protected]50279492014-05-05 23:24:295971 RenderFrameHost* render_frame_host) {
falken52a56e32016-12-08 05:02:405972 ShowInsecureLocalhostWarningIfNeeded();
5973
creis97867aa2017-02-17 18:39:455974 is_notifying_observers_ = true;
ericwilligersde386342016-10-19 02:35:095975 for (auto& observer : observers_)
5976 observer.DocumentOnLoadCompletedInMainFrame();
creis97867aa2017-02-17 18:39:455977 is_notifying_observers_ = false;
[email protected]6578fef2013-10-14 02:35:095978
5979 // TODO(avi): Remove. http://crbug.com/170921
[email protected]8ff00d72012-10-23 19:12:215980 NotificationService::current()->Notify(
5981 NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
5982 Source<WebContents>(this),
[email protected]50279492014-05-05 23:24:295983 NotificationService::NoDetails());
[email protected]25497492010-09-11 15:15:085984}
5985
creis8eb8ef22015-11-04 22:51:385986void WebContentsImpl::UpdateStateForFrame(RenderFrameHost* render_frame_host,
5987 const PageState& page_state) {
creis8eb8ef22015-11-04 22:51:385988 // The state update affects the last NavigationEntry associated with the given
5989 // |render_frame_host|. This may not be the last committed NavigationEntry (as
5990 // in the case of an UpdateState from a frame being swapped out). We track
5991 // which entry this is in the RenderFrameHost's nav_entry_id.
5992 RenderFrameHostImpl* rfhi =
5993 static_cast<RenderFrameHostImpl*>(render_frame_host);
5994 NavigationEntryImpl* entry =
5995 controller_.GetEntryWithUniqueID(rfhi->nav_entry_id());
5996 if (!entry)
5997 return;
5998
5999 FrameNavigationEntry* frame_entry =
6000 entry->GetFrameEntry(rfhi->frame_tree_node());
6001 if (!frame_entry)
6002 return;
6003
creisd06a9422015-11-11 03:08:456004 // The SiteInstance might not match if we do a cross-process navigation with
6005 // replacement (e.g., auto-subframe), in which case the swap out of the old
6006 // RenderFrameHost runs in the background after the old FrameNavigationEntry
6007 // has already been replaced and destroyed.
6008 if (frame_entry->site_instance() != rfhi->GetSiteInstance())
6009 return;
6010
creis8eb8ef22015-11-04 22:51:386011 if (page_state == frame_entry->page_state())
6012 return; // Nothing to update.
6013
creis0cade2e2017-02-28 06:37:476014 DCHECK(page_state.IsValid()) << "Shouldn't set an empty PageState.";
nasko34634672016-07-29 18:46:066015
6016 // The document_sequence_number and item_sequence_number recorded in the
6017 // FrameNavigationEntry should not differ from the one coming with the update,
6018 // since it must come from the same document. Do not update it if a difference
6019 // is detected, as this indicates that |frame_entry| is not the correct one.
6020 ExplodedPageState exploded_state;
6021 if (!DecodePageState(page_state.ToEncodedData(), &exploded_state))
6022 return;
6023
6024 if (exploded_state.top.document_sequence_number !=
6025 frame_entry->document_sequence_number() ||
6026 exploded_state.top.item_sequence_number !=
6027 frame_entry->item_sequence_number()) {
6028 return;
6029 }
6030
nasko04a322f25a2016-09-01 00:00:346031 frame_entry->SetPageState(page_state);
creis8eb8ef22015-11-04 22:51:386032 controller_.NotifyEntryChanged(entry);
6033}
6034
[email protected]ef3adfc2014-05-11 00:04:546035void WebContentsImpl::UpdateTitle(RenderFrameHost* render_frame_host,
[email protected]ef3adfc2014-05-11 00:04:546036 const base::string16& title,
6037 base::i18n::TextDirection title_direction) {
[email protected]ef3adfc2014-05-11 00:04:546038 // Try to find the navigation entry, which might not be the current one.
avi286019ce2015-07-29 01:26:586039 // For example, it might be from a recently swapped out RFH.
avi1edc8552016-10-13 21:37:166040 NavigationEntryImpl* entry = controller_.GetEntryWithUniqueID(
creisad8c31a2016-05-24 17:07:046041 static_cast<RenderFrameHostImpl*>(render_frame_host)->nav_entry_id());
[email protected]ef3adfc2014-05-11 00:04:546042
6043 // We can handle title updates when we don't have an entry in
6044 // UpdateTitleForEntry, but only if the update is from the current RVH.
6045 // TODO(avi): Change to make decisions based on the RenderFrameHost.
avi286019ce2015-07-29 01:26:586046 if (!entry && render_frame_host != GetMainFrame())
[email protected]ef3adfc2014-05-11 00:04:546047 return;
6048
6049 // TODO(evan): make use of title_direction.
6050 // http://code.google.com/p/chromium/issues/detail?id=27094
afakhry6f0c1ec22016-07-14 13:55:136051 UpdateTitleForEntry(entry, title);
[email protected]ef3adfc2014-05-11 00:04:546052}
6053
6054void WebContentsImpl::UpdateEncoding(RenderFrameHost* render_frame_host,
6055 const std::string& encoding) {
6056 SetEncoding(encoding);
6057}
6058
[email protected]39f2f9252014-05-03 00:54:016059void WebContentsImpl::DocumentAvailableInMainFrame(
6060 RenderViewHost* render_view_host) {
ericwilligersde386342016-10-19 02:35:096061 for (auto& observer : observers_)
6062 observer.DocumentAvailableInMainFrame();
[email protected]39f2f9252014-05-03 00:54:016063}
naskoc0fceff2015-04-30 15:53:526064
[email protected]e1c3a552012-05-04 20:51:326065void WebContentsImpl::RouteCloseEvent(RenderViewHost* rvh) {
6066 // Tell the active RenderViewHost to run unload handlers and close, as long
6067 // as the request came from a RenderViewHost in the same BrowsingInstance.
6068 // In most cases, we receive this from a swapped out RenderViewHost.
6069 // It is possible to receive it from one that has just been swapped in,
6070 // in which case we might as well deliver the message anyway.
6071 if (rvh->GetSiteInstance()->IsRelatedSiteInstance(GetSiteInstance()))
naskoc0fceff2015-04-30 15:53:526072 ClosePage();
[email protected]e1c3a552012-05-04 20:51:326073}
6074
alexmose7da5a12015-04-09 02:22:166075bool WebContentsImpl::ShouldRouteMessageEvent(
6076 RenderFrameHost* target_rfh,
6077 SiteInstance* source_site_instance) const {
6078 // Allow the message if this WebContents is dedicated to a browser plugin
6079 // guest.
[email protected]0eba810b2012-10-18 03:19:366080 // Note: This check means that an embedder could theoretically receive a
6081 // postMessage from anyone (not just its own guests). However, this is
6082 // probably not a risk for apps since other pages won't have references
6083 // to App windows.
alexmose7da5a12015-04-09 02:22:166084 return GetBrowserPluginGuest() || GetBrowserPluginEmbedder();
6085}
[email protected]f546640b2012-05-15 00:03:496086
alexmos58729042015-06-18 23:20:006087void WebContentsImpl::EnsureOpenerProxiesExist(RenderFrameHost* source_rfh) {
alexmose7da5a12015-04-09 02:22:166088 WebContentsImpl* source_web_contents = static_cast<WebContentsImpl*>(
6089 WebContents::FromRenderFrameHost(source_rfh));
[email protected]f546640b2012-05-15 00:03:496090
alexmose7da5a12015-04-09 02:22:166091 if (source_web_contents) {
lazyboy6ec48b2a2015-06-29 15:18:146092 // If this message is going to outer WebContents from inner WebContents,
6093 // then we should not create a RenderView. AttachToOuterWebContentsFrame()
6094 // already created a RenderFrameProxyHost for that purpose.
6095 if (GetBrowserPluginEmbedder() &&
ekaramad1beabecc2016-09-23 20:18:176096 GuestMode::IsCrossProcessFrameGuest(source_web_contents)) {
lazyboy6ec48b2a2015-06-29 15:18:146097 return;
6098 }
6099
lfg812874d2016-02-09 22:26:136100 if (this != source_web_contents && GetBrowserPluginGuest()) {
naskob3041b98a42016-03-12 04:43:066101 // We create a RenderFrameProxyHost for the embedder in the guest's render
6102 // process but we intentionally do not expose the embedder's opener chain
6103 // to it.
6104 source_web_contents->GetRenderManager()->CreateRenderFrameProxy(
6105 GetSiteInstance());
[email protected]f546640b2012-05-15 00:03:496106 } else {
alexmos58729042015-06-18 23:20:006107 RenderFrameHostImpl* source_rfhi =
6108 static_cast<RenderFrameHostImpl*>(source_rfh);
6109 source_rfhi->frame_tree_node()->render_manager()->CreateOpenerProxies(
alexmos90325cf2015-09-02 17:18:396110 GetSiteInstance(), nullptr);
[email protected]f546640b2012-05-15 00:03:496111 }
6112 }
[email protected]f546640b2012-05-15 00:03:496113}
6114
Avi Drissmand18c51972018-08-15 18:32:096115void WebContentsImpl::ForSecurityDropFullscreen() {
Avi Drissman734edf82019-06-20 22:10:106116 // There are two chains of WebContents to kick out of fullscreen.
6117 //
6118 // Chain 1, the inner/outer WebContents chain. If an inner WebContents has
6119 // done something that requires the browser to drop fullscreen, drop
6120 // fullscreen from it and any outer WebContents that may be in fullscreen.
6121 //
6122 // Chain 2, the opener WebContents chain. If a WebContents has done something
6123 // that requires the browser to drop fullscreen, drop fullscreen from any
6124 // WebContents that was involved in the chain of opening it.
6125 //
6126 // Note that these two chains don't interact, as only a top-level WebContents
6127 // can have an opener. This simplifies things.
6128
6129 WebContents* web_contents = this;
Avi Drissmand18c51972018-08-15 18:32:096130 while (web_contents) {
6131 if (web_contents->IsFullscreenForCurrentTab())
6132 web_contents->ExitFullscreen(true);
Avi Drissman734edf82019-06-20 22:10:106133
6134 if (web_contents->HasOriginalOpener())
6135 web_contents = FromRenderFrameHost(web_contents->GetOriginalOpener());
6136 else
6137 web_contents = web_contents->GetOuterWebContents();
Avi Drissmand18c51972018-08-15 18:32:096138 }
6139}
6140
avallee9993fca2016-11-17 06:16:506141void WebContentsImpl::SetAsFocusedWebContentsIfNecessary() {
6142 // Only change focus if we are not currently focused.
6143 WebContentsImpl* old_contents = GetFocusedWebContents();
6144 if (old_contents == this)
6145 return;
6146
paulmeyerfeafc2d2017-04-25 21:46:406147 GetOutermostWebContents()->node_.SetFocusedWebContents(this);
6148
Greg Thompsonfb957602017-09-04 10:25:526149 if (!GuestMode::IsCrossProcessFrameGuest(this) && browser_plugin_guest_)
paulmeyerfeafc2d2017-04-25 21:46:406150 return;
6151
avallee9993fca2016-11-17 06:16:506152 // Send a page level blur to the old contents so that it displays inactive UI
6153 // and focus this contents to activate it.
6154 if (old_contents)
6155 old_contents->GetMainFrame()->GetRenderWidgetHost()->SetPageFocus(false);
avalleec2ff1ef2017-01-21 00:12:306156
6157 // Make sure the outer web contents knows our frame is focused. Otherwise, the
6158 // outer renderer could have the element before or after the frame element
6159 // focused which would return early without actually advancing focus.
avalleed68cdd12017-06-21 22:07:276160 FocusOuterAttachmentFrameChain();
avalleec2ff1ef2017-01-21 00:12:306161
lfg1453e412017-04-11 00:48:506162 if (ShowingInterstitialPage()) {
creisadca9862017-06-16 19:12:116163 static_cast<RenderFrameHostImpl*>(interstitial_page_->GetMainFrame())
lfg1453e412017-04-11 00:48:506164 ->GetRenderWidgetHost()
6165 ->SetPageFocus(true);
6166 } else {
6167 GetMainFrame()->GetRenderWidgetHost()->SetPageFocus(true);
6168 }
avallee9993fca2016-11-17 06:16:506169}
6170
avallee0206f782016-07-28 18:55:336171void WebContentsImpl::SetFocusedFrame(FrameTreeNode* node,
6172 SiteInstance* source) {
avallee0206f782016-07-28 18:55:336173 frame_tree_.SetFocusedFrame(node, source);
avalleed68cdd12017-06-21 22:07:276174
Lucas Furukawa Gadaniabbfa342017-08-29 19:37:576175 if (auto* inner_contents = node_.GetInnerWebContentsInFrame(node)) {
6176 // |this| is an outer WebContents and |node| represents an inner
6177 // WebContents. Transfer the focus to the inner contents if |this| is
6178 // focused.
6179 if (GetFocusedWebContents() == this)
6180 inner_contents->SetAsFocusedWebContentsIfNecessary();
6181 } else if (node_.OuterContentsFrameTreeNode() &&
6182 node_.OuterContentsFrameTreeNode()
6183 ->current_frame_host()
6184 ->GetSiteInstance() == source) {
6185 // |this| is an inner WebContents, |node| is its main FrameTreeNode and
6186 // the outer WebContents FrameTreeNode is at |source|'s SiteInstance.
6187 // Transfer the focus to the inner WebContents if the outer WebContents is
6188 // focused. This branch is used when an inner WebContents is focused through
Lowell Mannersa9530382019-08-01 09:46:576189 // its RenderFrameProxyHost (via FrameFocused mojo call, used to
Lucas Furukawa Gadaniabbfa342017-08-29 19:37:576190 // implement the window.focus() API).
6191 if (GetFocusedWebContents() == GetOuterWebContents())
6192 SetAsFocusedWebContentsIfNecessary();
6193 } else if (!GetOuterWebContents()) {
6194 // This is an outermost WebContents.
6195 SetAsFocusedWebContentsIfNecessary();
6196 } else if (!GuestMode::IsCrossProcessFrameGuest(this) &&
6197 GetOuterWebContents()) {
6198 // TODO(lfg, paulmeyer): Allows BrowserPlugins to set themselves as the
6199 // focused WebContents. This works around a bug in FindRequestManager that
6200 // doesn't support properly traversing BrowserPlugins.
6201 SetAsFocusedWebContentsIfNecessary();
6202 }
avalleed68cdd12017-06-21 22:07:276203}
6204
Avi Drissman36f801f2018-02-01 20:06:046205void WebContentsImpl::DidCallFocus() {
6206 // Any explicit focusing of another window while this WebContents is in
6207 // fullscreen can be used to confuse the user, so drop fullscreen.
Avi Drissmand18c51972018-08-15 18:32:096208 ForSecurityDropFullscreen();
Avi Drissman36f801f2018-02-01 20:06:046209}
6210
avalleed68cdd12017-06-21 22:07:276211RenderFrameHost* WebContentsImpl::GetFocusedFrameIncludingInnerWebContents() {
6212 WebContentsImpl* contents = this;
6213 FrameTreeNode* focused_node = contents->frame_tree_.GetFocusedFrame();
6214
6215 // If there is no focused frame in the outer WebContents, we need to return
6216 // null.
6217 if (!focused_node)
6218 return nullptr;
6219
6220 // If the focused frame is embedding an inner WebContents, we must descend
6221 // into that contents. If the current WebContents does not have a focused
6222 // frame, return the main frame of this contents instead of the focused empty
6223 // frame embedding this contents.
6224 while (true) {
6225 contents = contents->node_.GetInnerWebContentsInFrame(focused_node);
6226 if (!contents)
6227 return focused_node->current_frame_host();
6228
6229 focused_node = contents->frame_tree_.GetFocusedFrame();
6230 if (!focused_node)
6231 return contents->GetMainFrame();
6232 }
avallee0206f782016-07-28 18:55:336233}
6234
Lucas Furukawa Gadaniabbfa342017-08-29 19:37:576235void WebContentsImpl::OnAdvanceFocus(RenderFrameHostImpl* source_rfh) {
6236 // When a RenderFrame needs to advance focus to a RenderFrameProxy (by hitting
6237 // TAB), the RenderFrameProxy sends an IPC to RenderFrameProxyHost. When this
6238 // RenderFrameProxyHost represents an inner WebContents, the outer WebContents
6239 // needs to focus the inner WebContents.
6240 if (GetOuterWebContents() &&
6241 GetOuterWebContents() == source_rfh->delegate()->GetAsWebContents() &&
6242 GetFocusedWebContents() == GetOuterWebContents()) {
6243 SetAsFocusedWebContentsIfNecessary();
6244 }
6245}
6246
ekaramada110f642016-12-21 19:47:286247void WebContentsImpl::OnFocusedElementChangedInFrame(
6248 RenderFrameHostImpl* frame,
6249 const gfx::Rect& bounds_in_root_view) {
6250 RenderWidgetHostViewBase* root_view =
6251 static_cast<RenderWidgetHostViewBase*>(GetRenderWidgetHostView());
6252 if (!root_view || !frame->GetView())
6253 return;
6254
6255 // Converting to screen coordinates.
6256 gfx::Point origin = bounds_in_root_view.origin();
6257 origin += root_view->GetViewBounds().OffsetFromOrigin();
6258 gfx::Rect bounds_in_screen(origin, bounds_in_root_view.size());
6259
6260 root_view->FocusedNodeChanged(frame->has_focused_editable_element(),
6261 bounds_in_screen);
6262
6263 FocusedNodeDetails details = {frame->has_focused_editable_element(),
6264 bounds_in_screen};
6265
6266 // TODO(ekaramad): We should replace this with an observer notification
6267 // (https://crbug.com/675975).
6268 NotificationService::current()->Notify(
6269 NOTIFICATION_FOCUS_CHANGED_IN_PAGE,
6270 Source<RenderViewHost>(GetRenderViewHost()),
6271 Details<FocusedNodeDetails>(&details));
Jeff Fisher709b4c52019-07-29 20:07:346272
6273 for (auto& observer : observers_)
6274 observer.OnFocusChangedInPage(&details);
ekaramada110f642016-12-21 19:47:286275}
6276
Lowell Manners1de5242e2019-04-25 10:18:466277bool WebContentsImpl::DidAddMessageToConsole(
6278 blink::mojom::ConsoleMessageLevel log_level,
6279 const base::string16& message,
6280 int32_t line_no,
6281 const base::string16& source_id) {
[email protected]a796f202012-05-30 14:14:256282 if (!delegate_)
6283 return false;
Lowell Manners1de5242e2019-04-25 10:18:466284 return delegate_->DidAddMessageToConsole(this, log_level, message, line_no,
avia90ae4e2016-11-11 20:49:336285 source_id);
[email protected]a796f202012-05-30 14:14:256286}
6287
Daniel Cheng90196c82018-04-25 21:49:146288void WebContentsImpl::DidReceiveInputEvent(
dominickn9c7ceaa32016-02-27 01:52:536289 RenderWidgetHostImpl* render_widget_host,
6290 const blink::WebInputEvent::Type type) {
Dominick Ng37a15052018-07-17 23:56:356291 if (!IsUserInteractionInputType(type))
Daniel Cheng90196c82018-04-25 21:49:146292 return;
6293
kenrb19221852016-04-29 17:21:406294 // Ignore unless the widget is currently in the frame tree.
6295 if (!HasMatchingWidgetHost(&frame_tree_, render_widget_host))
avic3aa8422015-11-09 20:57:226296 return;
6297
Daniel Cheng90196c82018-04-25 21:49:146298 if (type != blink::WebInputEvent::kGestureScrollBegin)
6299 last_interactive_input_event_time_ = ui::EventTimeForNow();
dominickn9c7ceaa32016-02-27 01:52:536300
Dominick Ng37a15052018-07-17 23:56:356301 for (auto& observer : observers_)
6302 observer.DidGetUserInteraction(type);
dominickn2dd142dd2015-10-29 05:30:506303}
6304
Avi Drissman738ea192018-08-29 20:24:166305bool WebContentsImpl::ShouldIgnoreInputEvents() {
6306 WebContentsImpl* web_contents = this;
6307 while (web_contents) {
6308 if (web_contents->ignore_input_events_)
6309 return true;
6310 web_contents = web_contents->GetOuterWebContents();
6311 }
6312
6313 return false;
6314}
6315
avallee9993fca2016-11-17 06:16:506316void WebContentsImpl::FocusOwningWebContents(
6317 RenderWidgetHostImpl* render_widget_host) {
6318 // The PDF plugin still runs as a BrowserPlugin and must go through the
6319 // input redirection mechanism. It must not become focused direcly.
6320 if (!GuestMode::IsCrossProcessFrameGuest(this) && browser_plugin_guest_)
6321 return;
6322
6323 RenderWidgetHostImpl* focused_widget =
6324 GetFocusedRenderWidgetHost(render_widget_host);
6325
6326 if (focused_widget != render_widget_host &&
avallee8e881592016-11-19 00:39:306327 (!focused_widget ||
6328 focused_widget->delegate() != render_widget_host->delegate())) {
avallee9993fca2016-11-17 06:16:506329 SetAsFocusedWebContentsIfNecessary();
6330 }
6331}
6332
[email protected]b172aee2012-04-10 17:05:266333void WebContentsImpl::OnIgnoredUIEvent() {
[email protected]31145092011-09-30 01:49:446334 // Notify observers.
ericwilligersde386342016-10-19 02:35:096335 for (auto& observer : observers_)
6336 observer.DidGetIgnoredUIEvent();
[email protected]fa1cf0b82010-01-15 21:49:446337}
6338
avic3aa8422015-11-09 20:57:226339void WebContentsImpl::RendererUnresponsive(
Lukasz Anforowicz52b93722018-06-20 16:11:396340 RenderWidgetHostImpl* render_widget_host,
6341 base::RepeatingClosure hang_monitor_restarter) {
avic031d392017-03-03 03:09:426342 if (ShouldIgnoreUnresponsiveRenderer())
[email protected]e5fc1632011-08-08 07:51:536343 return;
6344
Lukasz Anforowicz4f0593d2018-09-21 18:33:546345 // Do not report hangs (to task manager, to hang renderer dialog, etc.) for
6346 // invisible tabs (like extension background page, background tabs). See
6347 // https://crbug.com/881812 for rationale and for choosing the visibility
6348 // (rather than process priority) as the signal here.
6349 if (GetVisibility() != Visibility::VISIBLE)
6350 return;
6351
Avi Drissman8920def2018-01-31 19:53:366352 if (!render_widget_host->renderer_initialized())
[email protected]55452902011-06-01 21:57:476353 return;
6354
Lukasz Anforowicz4f0593d2018-09-21 18:33:546355 for (auto& observer : observers_)
6356 observer.OnRendererUnresponsive(render_widget_host->GetProcess());
6357
Dave Tapuskac6146c52017-12-20 22:48:156358 if (delegate_)
Lukasz Anforowicz52b93722018-06-20 16:11:396359 delegate_->RendererUnresponsive(this, render_widget_host,
6360 std::move(hang_monitor_restarter));
[email protected]420ae012009-04-24 05:16:326361}
6362
avic3aa8422015-11-09 20:57:226363void WebContentsImpl::RendererResponsive(
6364 RenderWidgetHostImpl* render_widget_host) {
[email protected]6934a702011-12-20 00:04:516365 if (delegate_)
Avi Drissman1572e8c3c2018-02-02 19:06:366366 delegate_->RendererResponsive(this, render_widget_host);
[email protected]420ae012009-04-24 05:16:326367}
6368
Alex Moshchuk7b4f0652019-05-30 18:54:416369void WebContentsImpl::SubframeCrashed(
6370 blink::mojom::FrameVisibility visibility) {
6371 // If a subframe crashed on a hidden tab, mark the tab for reload to avoid
6372 // showing a sad frame to the user if they ever switch back to that tab. Do
6373 // this for subframes that are either visible in viewport or visible but
6374 // scrolled out of view, but skip subframes that are not rendered (e.g., via
6375 // "display:none"), since in that case the user wouldn't see a sad frame
6376 // anyway.
6377 bool did_mark_for_reload = false;
6378 if (IsHidden() && visibility != blink::mojom::FrameVisibility::kNotRendered &&
6379 base::FeatureList::IsEnabled(
6380 features::kReloadHiddenTabsWithCrashedSubframes)) {
6381 controller_.SetNeedsReload(
6382 NavigationControllerImpl::NeedsReloadType::kCrashedSubframe);
6383 did_mark_for_reload = true;
6384 UMA_HISTOGRAM_ENUMERATION(
6385 "Stability.ChildFrameCrash.TabMarkedForReload.Visibility", visibility);
6386 }
6387
6388 UMA_HISTOGRAM_BOOLEAN("Stability.ChildFrameCrash.TabMarkedForReload",
6389 did_mark_for_reload);
6390}
6391
[email protected]b172aee2012-04-10 17:05:266392void WebContentsImpl::BeforeUnloadFiredFromRenderManager(
[email protected]feaded02012-09-25 19:12:556393 bool proceed, const base::TimeTicks& proceed_time,
[email protected]420ae012009-04-24 05:16:326394 bool* proceed_to_fire_unload) {
ericwilligersde386342016-10-19 02:35:096395 for (auto& observer : observers_)
Chris Hamilton3b3e315d2018-09-19 13:16:216396 observer.BeforeUnloadFired(proceed, proceed_time);
[email protected]6934a702011-12-20 00:04:516397 if (delegate_)
6398 delegate_->BeforeUnloadFired(this, proceed, proceed_to_fire_unload);
[email protected]e80af492013-06-24 21:52:096399 // Note: |this| might be deleted at this point.
[email protected]420ae012009-04-24 05:16:326400}
6401
[email protected]58d5cfe2013-07-10 02:40:526402void WebContentsImpl::RenderProcessGoneFromRenderManager(
[email protected]3a3d47472010-07-15 21:03:546403 RenderViewHost* render_view_host) {
[email protected]443b80e2010-12-14 00:42:236404 DCHECK(crashed_status_ != base::TERMINATION_STATUS_STILL_RUNNING);
[email protected]ec6a7eb2013-04-22 17:34:226405 RenderViewTerminated(render_view_host, crashed_status_, crashed_error_code_);
[email protected]3a3d47472010-07-15 21:03:546406}
6407
Jianzhou Fengd8720c72018-01-18 03:06:306408void WebContentsImpl::UpdateRenderViewSizeForRenderManager(bool is_main_frame) {
[email protected]8643e6d2012-01-18 20:26:106409 // TODO(brettw) this is a hack. See WebContentsView::SizeContents.
Jianzhou Fengd8720c72018-01-18 03:06:306410 gfx::Size size = GetSizeForNewRenderView(is_main_frame);
[email protected]0323ee42010-02-17 22:03:266411 // 0x0 isn't a valid window size (minimal window size is 1x1) but it may be
6412 // here during container initialization and normal window size will be set
6413 // later. In case of tab duplication this resizing to 0x0 prevents setting
6414 // normal size later so just ignore it.
6415 if (!size.IsEmpty())
6416 view_->SizeContents(size);
[email protected]420ae012009-04-24 05:16:326417}
6418
[email protected]af905902013-10-01 21:38:516419void WebContentsImpl::CancelModalDialogsForRenderManager() {
6420 // We need to cancel modal dialogs when doing a process swap, since the load
avi2460c762015-04-17 15:21:546421 // deferrer would prevent us from swapping out. We also clear the state
6422 // because this is a cross-process navigation, which means that it's a new
6423 // site that should not have to pay for the sins of its predecessor.
creis89a0f782015-05-27 16:13:176424 //
Lei Zhang6410ec1a2019-01-31 21:38:586425 // Note that we don't bother telling |browser_plugin_embedder_| because the
creis89a0f782015-05-27 16:13:176426 // cross-process navigation will either destroy the browser plugins or not
6427 // require their dialogs to close.
avi5d3b8692016-10-12 22:00:466428 if (dialog_manager_) {
avi6879fcf2017-01-21 05:27:536429 dialog_manager_->CancelDialogs(this, /*reset_state=*/true);
avi5d3b8692016-10-12 22:00:466430 }
[email protected]af905902013-10-01 21:38:516431}
6432
[email protected]02d7b6e2014-06-24 21:01:506433void WebContentsImpl::NotifySwappedFromRenderManager(RenderFrameHost* old_host,
6434 RenderFrameHost* new_host,
6435 bool is_main_frame) {
6436 if (is_main_frame) {
naskof5940b9f2015-03-02 23:04:056437 NotifyViewSwapped(old_host ? old_host->GetRenderViewHost() : nullptr,
[email protected]02d7b6e2014-06-24 21:01:506438 new_host->GetRenderViewHost());
[email protected]a6b73c62013-02-11 23:05:086439
[email protected]02d7b6e2014-06-24 21:01:506440 // Make sure the visible RVH reflects the new delegate's preferences.
6441 if (delegate_)
6442 view_->SetOverscrollControllerEnabled(CanOverscrollContent());
[email protected]d56a49e2013-02-23 00:47:396443
dmazzoni62111452017-04-28 23:46:556444 RenderWidgetHostViewBase* rwhv =
6445 static_cast<RenderWidgetHostViewBase*>(GetRenderWidgetHostView());
6446 if (rwhv)
6447 rwhv->SetMainFrameAXTreeID(GetMainFrame()->GetAXTreeID());
[email protected]02d7b6e2014-06-24 21:01:506448 }
6449
Jinsuk Kim895b2c52018-07-03 06:49:166450 NotifyFrameSwapped(old_host, new_host, is_main_frame);
[email protected]3a3d47472010-07-15 21:03:546451}
6452
naskof5940b9f2015-03-02 23:04:056453void WebContentsImpl::NotifyMainFrameSwappedFromRenderManager(
Jinsuk Kim895b2c52018-07-03 06:49:166454 RenderFrameHost* old_host,
6455 RenderFrameHost* new_host) {
6456 NotifyViewSwapped(old_host ? old_host->GetRenderViewHost() : nullptr,
6457 new_host->GetRenderViewHost());
naskof5940b9f2015-03-02 23:04:056458}
6459
[email protected]b172aee2012-04-10 17:05:266460NavigationControllerImpl& WebContentsImpl::GetControllerForRenderManager() {
[email protected]d1198fd2012-08-13 22:50:196461 return GetController();
[email protected]3a3d47472010-07-15 21:03:546462}
6463
dcheng59716272016-04-09 05:19:086464std::unique_ptr<WebUIImpl> WebContentsImpl::CreateWebUIForRenderFrameHost(
carlosk37f793792014-11-27 15:08:296465 const GURL& url) {
Dan Beam055fe8f2017-10-04 23:23:366466 return CreateWebUI(url);
[email protected]420ae012009-04-24 05:16:326467}
6468
[email protected]10f417c52011-12-28 21:04:236469NavigationEntry*
[email protected]b172aee2012-04-10 17:05:266470 WebContentsImpl::GetLastCommittedNavigationEntryForRenderManager() {
[email protected]420ae012009-04-24 05:16:326471 return controller_.GetLastCommittedEntry();
6472}
6473
creisadca9862017-06-16 19:12:116474InterstitialPageImpl* WebContentsImpl::GetInterstitialForRenderManager() {
6475 return interstitial_page_;
6476}
6477
lfgbede6392015-09-11 21:54:066478void WebContentsImpl::CreateRenderWidgetHostViewForRenderManager(
6479 RenderViewHost* render_view_host) {
lfgc740d4b22016-04-15 16:45:336480 RenderWidgetHostViewBase* rwh_view =
6481 view_->CreateViewForWidget(render_view_host->GetWidget(), false);
[email protected]1a98a932009-11-17 00:12:526482
[email protected]420ae012009-04-24 05:16:326483 // Now that the RenderView has been created, we need to tell it its size.
[email protected]245f7d52011-11-28 15:36:446484 if (rwh_view)
Jianzhou Fengd8720c72018-01-18 03:06:306485 rwh_view->SetSize(GetSizeForNewRenderView(true));
lfgbede6392015-09-11 21:54:066486}
6487
6488bool WebContentsImpl::CreateRenderViewForRenderManager(
6489 RenderViewHost* render_view_host,
6490 int opener_frame_routing_id,
6491 int proxy_routing_id,
Pavel Feldman25234722017-10-11 02:49:066492 const base::UnguessableToken& devtools_frame_token,
lfgbede6392015-09-11 21:54:066493 const FrameReplicationState& replicated_frame_state) {
6494 TRACE_EVENT0("browser,navigation",
6495 "WebContentsImpl::CreateRenderViewForRenderManager");
6496
piman00cf9402017-04-03 17:09:206497 if (proxy_routing_id == MSG_ROUTING_NONE)
lfg9431efd2016-12-14 14:45:266498 CreateRenderWidgetHostViewForRenderManager(render_view_host);
lfg9431efd2016-12-14 14:45:266499
piman00cf9402017-04-03 17:09:206500 if (!static_cast<RenderViewHostImpl*>(render_view_host)
6501 ->CreateRenderView(opener_frame_routing_id, proxy_routing_id,
Pavel Feldman25234722017-10-11 02:49:066502 devtools_frame_token, replicated_frame_state,
6503 created_with_opener_)) {
[email protected]a4127722011-04-27 23:13:526504 return false;
[email protected]14392a52012-05-02 20:28:446505 }
W. James MacLean62198672019-06-04 16:46:106506 // Set the TextAutosizer state from the main frame's renderer on the new view,
6507 // but only if it's not for the main frame. Main frame renderers should create
6508 // this state themselves from up-to-date values, so we shouldn't override it
6509 // with the cached values.
6510 if (!render_view_host->GetMainFrame()) {
6511 render_view_host->Send(
6512 new PageMsg_UpdateTextAutosizerPageInfoForRemoteMainFrames(
6513 render_view_host->GetRoutingID(), text_autosizer_page_info_));
6514 }
[email protected]a4127722011-04-27 23:13:526515
Lucas Furukawa Gadaniaed1fed2017-10-13 17:34:146516 if (proxy_routing_id == MSG_ROUTING_NONE && node_.outer_web_contents())
6517 ReattachToOuterWebContentsFrame();
6518
avi2b177592014-12-10 02:08:026519 SetHistoryOffsetAndLengthForView(render_view_host,
6520 controller_.GetLastCommittedEntryIndex(),
6521 controller_.GetEntryCount());
6522
[email protected]a220b5932013-09-21 03:47:446523#if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID)
[email protected]f8233cc2011-05-31 20:24:506524 // Force a ViewMsg_Resize to be sent, needed to make plugins show up on
6525 // linux. See crbug.com/83941.
avif9ab5d942015-10-15 14:05:446526 RenderWidgetHostView* rwh_view = render_view_host->GetWidget()->GetView();
[email protected]245f7d52011-11-28 15:36:446527 if (rwh_view) {
6528 if (RenderWidgetHost* render_widget_host = rwh_view->GetRenderWidgetHost())
Fady Samuel0b911822018-04-25 13:22:166529 render_widget_host->SynchronizeVisualProperties();
[email protected]245f7d52011-11-28 15:36:446530 }
[email protected]f8233cc2011-05-31 20:24:506531#endif
6532
[email protected]420ae012009-04-24 05:16:326533 return true;
6534}
6535
[email protected]82307f6b2014-08-07 03:30:126536bool WebContentsImpl::CreateRenderFrameForRenderManager(
6537 RenderFrameHost* render_frame_host,
arthursonzogni1efc7b962019-03-14 09:36:336538 int previous_routing_id,
alexmosa181efc2015-09-03 00:39:046539 int opener_routing_id,
japhete6adf142014-10-31 00:01:496540 int parent_routing_id,
alexmosa181efc2015-09-03 00:39:046541 int previous_sibling_routing_id) {
naskob8744d22014-08-28 17:07:436542 TRACE_EVENT0("browser,navigation",
6543 "WebContentsImpl::CreateRenderFrameForRenderManager");
[email protected]82307f6b2014-08-07 03:30:126544
6545 RenderFrameHostImpl* rfh =
6546 static_cast<RenderFrameHostImpl*>(render_frame_host);
arthursonzogni1efc7b962019-03-14 09:36:336547 if (!rfh->CreateRenderFrame(previous_routing_id, opener_routing_id,
alexmosa181efc2015-09-03 00:39:046548 parent_routing_id, previous_sibling_routing_id))
[email protected]82307f6b2014-08-07 03:30:126549 return false;
6550
6551 // TODO(nasko): When RenderWidgetHost is owned by RenderFrameHost, the passed
6552 // RenderFrameHost will have to be associated with the appropriate
6553 // RenderWidgetHostView or a new one should be created here.
6554
6555 return true;
6556}
6557
mfomitchev2b8b066a2016-01-28 19:23:156558#if defined(OS_ANDROID)
[email protected]fc2b46b2014-05-03 16:33:456559
[email protected]155c7f22013-12-09 17:07:186560base::android::ScopedJavaLocalRef<jobject>
6561WebContentsImpl::GetJavaWebContents() {
mostynb042582e2015-03-16 22:13:406562 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]0e813a52014-08-13 10:34:566563 return GetWebContentsAndroid()->GetJavaObject();
6564}
[email protected]155c7f22013-12-09 17:07:186565
[email protected]0e813a52014-08-13 10:34:566566WebContentsAndroid* WebContentsImpl::GetWebContentsAndroid() {
Kevin McNee576bb5d82019-05-29 16:47:516567 if (!web_contents_android_) {
6568 web_contents_android_ = std::make_unique<WebContentsAndroid>(this);
[email protected]155c7f22013-12-09 17:07:186569 }
Kevin McNee576bb5d82019-05-29 16:47:516570 return web_contents_android_.get();
6571}
6572
6573void WebContentsImpl::ClearWebContentsAndroid() {
6574 DCHECK_CURRENTLY_ON(BrowserThread::UI);
6575 web_contents_android_.reset();
[email protected]155c7f22013-12-09 17:07:186576}
6577
paulmeyerc0b762b2016-04-13 11:55:176578void WebContentsImpl::ActivateNearestFindResult(float x,
6579 float y) {
6580 GetOrCreateFindRequestManager()->ActivateNearestFindResult(x, y);
6581}
6582
6583void WebContentsImpl::RequestFindMatchRects(int current_version) {
6584 GetOrCreateFindRequestManager()->RequestFindMatchRects(current_version);
6585}
6586
[email protected]7d244f12013-08-22 21:41:516587bool WebContentsImpl::CreateRenderViewForInitialEmptyDocument() {
nasko4c0feb62015-06-05 18:37:066588 return CreateRenderViewForRenderManager(
6589 GetRenderViewHost(), MSG_ROUTING_NONE, MSG_ROUTING_NONE,
Pavel Feldman25234722017-10-11 02:49:066590 frame_tree_.root()->devtools_frame_token(),
lfgbede6392015-09-11 21:54:066591 frame_tree_.root()->current_replication_state());
[email protected]7d244f12013-08-22 21:41:516592}
[email protected]fc2b46b2014-05-03 16:33:456593
rockot400ea35b2016-10-15 19:15:326594service_manager::InterfaceProvider* WebContentsImpl::GetJavaInterfaces() {
sammcf5f1b0f2016-09-20 23:05:116595 if (!java_interfaces_) {
rockot400ea35b2016-10-15 19:15:326596 service_manager::mojom::InterfaceProviderPtr provider;
blundelle0a9f1582016-12-20 11:23:326597 BindInterfaceRegistryForWebContents(mojo::MakeRequest(&provider), this);
rockot400ea35b2016-10-15 19:15:326598 java_interfaces_.reset(new service_manager::InterfaceProvider);
sammcf5f1b0f2016-09-20 23:05:116599 java_interfaces_->Bind(std::move(provider));
6600 }
6601 return java_interfaces_.get();
6602}
6603
Francois Doray6c26f3342018-08-24 17:17:226604#endif
6605
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:416606bool WebContentsImpl::CompletedFirstVisuallyNonEmptyPaint() {
spqchane72c3cb2017-06-30 23:02:106607 return did_first_visually_non_empty_paint_;
6608}
6609
pkotwicz75ca8ffd2016-02-16 23:10:196610void WebContentsImpl::OnDidDownloadImage(
Avi Drissmana5a52dd2018-03-27 03:39:026611 ImageDownloadCallback callback,
pkotwicz75ca8ffd2016-02-16 23:10:196612 int id,
6613 const GURL& image_url,
leon.hancbc4bc3c2016-02-26 07:08:526614 int32_t http_status_code,
leon.hanc2228532016-08-16 05:59:186615 const std::vector<SkBitmap>& images,
6616 const std::vector<gfx::Size>& original_image_sizes) {
Avi Drissmana5a52dd2018-03-27 03:39:026617 std::move(callback).Run(id, http_status_code, image_url, images,
6618 original_image_sizes);
pkotwicz75ca8ffd2016-02-16 23:10:196619}
6620
[email protected]6fba26d2014-04-29 09:38:286621void WebContentsImpl::OnDialogClosed(int render_process_id,
6622 int render_frame_id,
[email protected]b172aee2012-04-10 17:05:266623 IPC::Message* reply_msg,
[email protected]87de04b02014-04-08 22:14:496624 bool dialog_was_suppressed,
[email protected]b172aee2012-04-10 17:05:266625 bool success,
[email protected]fcf75d42013-12-03 20:11:266626 const base::string16& user_input) {
[email protected]6fba26d2014-04-29 09:38:286627 RenderFrameHostImpl* rfh = RenderFrameHostImpl::FromID(render_process_id,
6628 render_frame_id);
[email protected]87de04b02014-04-08 22:14:496629 last_dialog_suppressed_ = dialog_was_suppressed;
6630
Charlie Reis96077362019-07-04 00:34:046631 javascript_dialog_navigation_deferrer_.reset();
6632
[email protected]beb440c2009-11-06 04:08:546633 if (is_showing_before_unload_dialog_ && !success) {
clamybc6f4152016-04-01 20:14:236634 // It is possible for the current RenderFrameHost to have changed in the
6635 // meantime. Do not reset the navigation state in that case.
6636 if (rfh && rfh == rfh->frame_tree_node()->current_frame_host()) {
clamy44e84ce2016-02-22 15:38:256637 rfh->frame_tree_node()->BeforeUnloadCanceled();
clamybc6f4152016-04-01 20:14:236638 controller_.DiscardNonCommittedEntries();
6639 }
[email protected]ec8e8b02013-06-19 04:57:106640
Charles Reis15d60b132017-11-03 17:43:476641 // Update the URL display either way, to avoid showing a stale URL.
6642 NotifyNavigationStateChanged(INVALIDATE_TYPE_URL);
6643
ericwilligersde386342016-10-19 02:35:096644 for (auto& observer : observers_)
6645 observer.BeforeUnloadDialogCancelled();
[email protected]beb440c2009-11-06 04:08:546646 }
[email protected]87de04b02014-04-08 22:14:496647
[email protected]6fba26d2014-04-29 09:38:286648 if (rfh) {
avi44acfaa82017-05-03 22:55:356649 rfh->JavaScriptDialogClosed(reply_msg, success, user_input);
Pavel Feldman3c1842b2017-08-02 05:00:166650
6651 std::vector<protocol::PageHandler*> page_handlers =
6652 protocol::PageHandler::EnabledForWebContents(this);
6653 for (auto* handler : page_handlers)
6654 handler->DidCloseJavaScriptDialog(success, user_input);
6655
[email protected]6fba26d2014-04-29 09:38:286656 } else {
6657 // Don't leak the sync IPC reply if the RFH or process is gone.
6658 delete reply_msg;
6659 }
avi1c51fb62017-02-27 20:43:036660
6661 is_showing_javascript_dialog_ = false;
6662 is_showing_before_unload_dialog_ = false;
[email protected]beb440c2009-11-06 04:08:546663}
6664
[email protected]b172aee2012-04-10 17:05:266665void WebContentsImpl::SetEncoding(const std::string& encoding) {
[email protected]ef3adfc2014-05-11 00:04:546666 if (encoding == last_reported_encoding_)
6667 return;
6668 last_reported_encoding_ = encoding;
6669
jinsukkim79df88a2017-01-04 06:26:156670 canonical_encoding_ = base::GetCanonicalEncodingNameByAliasName(encoding);
[email protected]aacd7cc2009-09-02 14:51:106671}
[email protected]f45d2a72010-03-08 23:28:356672
[email protected]1ac10dca2013-08-20 20:47:046673bool WebContentsImpl::IsHidden() {
Francois Doraye6161152018-03-27 22:05:376674 return !IsBeingCaptured() && visibility_ != Visibility::VISIBLE;
[email protected]1ac10dca2013-08-20 20:47:046675}
6676
rob97250742015-12-10 17:45:156677int WebContentsImpl::GetOuterDelegateFrameTreeNodeId() {
lfg601233692017-03-06 22:45:446678 return node_.outer_contents_frame_tree_node_id();
lazyboy6ec48b2a2015-06-29 15:18:146679}
6680
ekaramadaeb3c5c2016-11-09 03:22:356681RenderWidgetHostImpl* WebContentsImpl::GetFullscreenRenderWidgetHost() const {
6682 return RenderWidgetHostImpl::FromID(fullscreen_widget_process_id_,
6683 fullscreen_widget_routing_id_);
6684}
6685
[email protected]b0936d22013-11-28 06:47:366686RenderFrameHostManager* WebContentsImpl::GetRenderManager() const {
[email protected]fa944cb82013-11-15 17:51:216687 return frame_tree_.root()->render_manager();
6688}
6689
[email protected]f8497342013-02-05 22:15:026690BrowserPluginGuest* WebContentsImpl::GetBrowserPluginGuest() const {
[email protected]7a846df2012-09-20 19:17:396691 return browser_plugin_guest_.get();
6692}
6693
Albert J. Wongc55cc64d2018-10-12 02:50:046694void WebContentsImpl::SetBrowserPluginGuest(
6695 std::unique_ptr<BrowserPluginGuest> guest) {
6696 DCHECK(!browser_plugin_guest_);
6697 DCHECK(guest);
6698 browser_plugin_guest_ = std::move(guest);
[email protected]738f57a2013-06-29 21:06:546699}
6700
Aidan Wolter52bcc12a2018-11-14 03:27:296701base::UnguessableToken WebContentsImpl::GetAudioGroupId() {
6702 if (!base::FeatureList::IsEnabled(features::kAudioServiceAudioStreams))
6703 return base::UnguessableToken::Null();
6704
6705 return GetAudioStreamFactory()->group_id();
6706}
6707
Sofiya Semenova5e9d9122019-07-11 02:46:416708ukm::SourceId WebContentsImpl::GetLastCommittedSourceId() {
6709 return last_committed_source_id_;
6710}
6711
[email protected]f8497342013-02-05 22:15:026712BrowserPluginEmbedder* WebContentsImpl::GetBrowserPluginEmbedder() const {
[email protected]7a846df2012-09-20 19:17:396713 return browser_plugin_embedder_.get();
6714}
[email protected]8ff00d72012-10-23 19:12:216715
fsamuel60b42282015-03-10 03:29:146716void WebContentsImpl::CreateBrowserPluginEmbedderIfNecessary() {
6717 if (browser_plugin_embedder_)
6718 return;
6719 browser_plugin_embedder_.reset(BrowserPluginEmbedder::Create(this));
6720}
6721
Jianzhou Fengd8720c72018-01-18 03:06:306722gfx::Size WebContentsImpl::GetSizeForNewRenderView(bool is_main_frame) {
[email protected]dc0fd432013-08-27 15:29:216723 gfx::Size size;
Jianzhou Fengd8720c72018-01-18 03:06:306724 if (is_main_frame)
6725 size = device_emulation_size_;
6726 if (size.IsEmpty() && delegate_)
[email protected]dc0fd432013-08-27 15:29:216727 size = delegate_->GetSizeForNewRenderView(this);
6728 if (size.IsEmpty())
[email protected]fc2b46b2014-05-03 16:33:456729 size = GetContainerBounds().size();
[email protected]dc0fd432013-08-27 15:29:216730 return size;
6731}
6732
[email protected]ae7eeda2014-07-04 01:53:266733void WebContentsImpl::OnFrameRemoved(RenderFrameHost* render_frame_host) {
ericwilligersde386342016-10-19 02:35:096734 for (auto& observer : observers_)
6735 observer.FrameDeleted(render_frame_host);
[email protected]9b159a52013-10-03 17:24:556736}
6737
[email protected]222f5822014-02-05 23:40:496738void WebContentsImpl::OnPreferredSizeChanged(const gfx::Size& old_size) {
6739 if (!delegate_)
6740 return;
6741 const gfx::Size new_size = GetPreferredSize();
6742 if (new_size != old_size)
6743 delegate_->UpdatePreferredSize(this, new_size);
6744}
6745
Dan Beam055fe8f2017-10-04 23:23:366746std::unique_ptr<WebUIImpl> WebContentsImpl::CreateWebUI(const GURL& url) {
Jeremy Roman04f27c372017-10-27 15:20:556747 std::unique_ptr<WebUIImpl> web_ui = std::make_unique<WebUIImpl>(this);
Lei Zhangee3b63ad2018-06-06 23:25:316748 std::unique_ptr<WebUIController> controller(
avie865b1d2016-10-24 19:42:596749 WebUIControllerFactoryRegistry::GetInstance()
Lei Zhangee3b63ad2018-06-06 23:25:316750 ->CreateWebUIControllerForURL(web_ui.get(), url));
dbeama1b926a2015-08-31 23:17:516751 if (controller) {
Jeremy Roman04f27c372017-10-27 15:20:556752 web_ui->AddMessageHandler(std::make_unique<GenericHandler>());
Lei Zhangee3b63ad2018-06-06 23:25:316753 web_ui->SetController(std::move(controller));
dbeama1b926a2015-08-31 23:17:516754 return web_ui;
6755 }
6756
avie865b1d2016-10-24 19:42:596757 return nullptr;
dbeama1b926a2015-08-31 23:17:516758}
6759
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:416760FindRequestManager* WebContentsImpl::GetFindRequestManager() {
6761 for (auto* contents = this; contents;
paulmeyerfeafc2d2017-04-25 21:46:406762 contents = contents->GetOuterWebContents()) {
6763 if (contents->find_request_manager_)
6764 return contents->find_request_manager_.get();
6765 }
6766
6767 return nullptr;
6768}
6769
paulmeyerc0b762b2016-04-13 11:55:176770FindRequestManager* WebContentsImpl::GetOrCreateFindRequestManager() {
paulmeyerfeafc2d2017-04-25 21:46:406771 if (FindRequestManager* manager = GetFindRequestManager())
6772 return manager;
6773
Ehsan Karamad6beb2ea2018-11-25 18:15:136774 DCHECK(!browser_plugin_guest_ || GetOuterWebContents());
6775
paulmeyerfeafc2d2017-04-25 21:46:406776 // No existing FindRequestManager found, so one must be created.
6777 find_request_manager_.reset(new FindRequestManager(this));
6778
6779 // Concurrent find sessions must not overlap, so destroy any existing
6780 // FindRequestManagers in any inner WebContentses.
Lucas Furukawa Gadani2ec00c82018-12-14 15:53:166781 for (WebContents* contents : GetWebContentsAndAllInner()) {
6782 auto* web_contents_impl = static_cast<WebContentsImpl*>(contents);
6783 if (web_contents_impl == this)
paulmeyerfeafc2d2017-04-25 21:46:406784 continue;
Lucas Furukawa Gadani2ec00c82018-12-14 15:53:166785 if (web_contents_impl->find_request_manager_) {
6786 web_contents_impl->find_request_manager_->StopFinding(
6787 STOP_FIND_ACTION_CLEAR_SELECTION);
6788 web_contents_impl->find_request_manager_.release();
paulmeyerfeafc2d2017-04-25 21:46:406789 }
6790 }
paulmeyerc0b762b2016-04-13 11:55:176791
6792 return find_request_manager_.get();
6793}
6794
6795void WebContentsImpl::NotifyFindReply(int request_id,
6796 int number_of_matches,
6797 const gfx::Rect& selection_rect,
6798 int active_match_ordinal,
6799 bool final_update) {
Sebastien Marchandcf97ed52017-07-24 23:50:346800 if (delegate_ && !is_being_destroyed_ &&
Lukasz Anforowicze9ae3722017-09-29 17:37:386801 !GetMainFrame()->GetProcess()->FastShutdownStarted()) {
paulmeyerc0b762b2016-04-13 11:55:176802 delegate_->FindReply(this, request_id, number_of_matches, selection_rect,
6803 active_match_ordinal, final_update);
6804 }
6805}
6806
ortuno467e5792016-06-10 04:32:396807void WebContentsImpl::IncrementBluetoothConnectedDeviceCount() {
6808 // Trying to invalidate the tab state while being destroyed could result in a
6809 // use after free.
6810 if (IsBeingDestroyed()) {
6811 return;
6812 }
6813 // Notify for UI updates if the state changes.
6814 bluetooth_connected_device_count_++;
6815 if (bluetooth_connected_device_count_ == 1) {
6816 NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB);
6817 }
6818}
6819
6820void WebContentsImpl::DecrementBluetoothConnectedDeviceCount() {
6821 // Trying to invalidate the tab state while being destroyed could result in a
6822 // use after free.
6823 if (IsBeingDestroyed()) {
6824 return;
6825 }
6826 // Notify for UI updates if the state changes.
Lukasz Anforowicze9ae3722017-09-29 17:37:386827 DCHECK_NE(bluetooth_connected_device_count_, 0u);
ortuno467e5792016-06-10 04:32:396828 bluetooth_connected_device_count_--;
6829 if (bluetooth_connected_device_count_ == 0) {
6830 NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB);
6831 }
6832}
6833
Reilly Grant5e7c79b22019-04-09 17:26:206834void WebContentsImpl::IncrementSerialActiveFrameCount() {
6835 // Trying to invalidate the tab state while being destroyed could result in a
6836 // use after free.
6837 if (IsBeingDestroyed())
6838 return;
6839
6840 // Notify for UI updates if the state changes.
6841 serial_active_frame_count_++;
6842 if (serial_active_frame_count_ == 1)
6843 NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB);
6844}
6845
6846void WebContentsImpl::DecrementSerialActiveFrameCount() {
6847 // Trying to invalidate the tab state while being destroyed could result in a
6848 // use after free.
6849 if (IsBeingDestroyed())
6850 return;
6851
6852 // Notify for UI updates if the state changes.
6853 DCHECK_NE(0u, serial_active_frame_count_);
6854 serial_active_frame_count_--;
6855 if (serial_active_frame_count_ == 0)
6856 NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB);
6857}
6858
Marijn Kruisselbrink46fa4bd2019-07-11 18:15:476859void WebContentsImpl::AddNativeFileSystemDirectoryHandle(
6860 const base::FilePath& path) {
Marijn Kruisselbrinkf1714aa22019-07-02 03:45:336861 // Trying to invalidate the tab state while being destroyed could result in a
6862 // use after free.
6863 if (IsBeingDestroyed())
6864 return;
6865
6866 // Notify for UI updates if the state changes. Need both TYPE_TAB and TYPE_URL
6867 // to update both the tab-level usage indicator and the usage indicator in the
6868 // omnibox.
Marijn Kruisselbrink46fa4bd2019-07-11 18:15:476869 const bool was_empty = native_file_system_directory_handles_.empty();
6870 native_file_system_directory_handles_[path]++;
6871 if (was_empty) {
Marijn Kruisselbrinkf1714aa22019-07-02 03:45:336872 NotifyNavigationStateChanged(static_cast<content::InvalidateTypes>(
6873 INVALIDATE_TYPE_TAB | INVALIDATE_TYPE_URL));
6874 }
6875}
6876
Marijn Kruisselbrink46fa4bd2019-07-11 18:15:476877void WebContentsImpl::RemoveNativeFileSystemDirectoryHandle(
6878 const base::FilePath& path) {
Marijn Kruisselbrinkf1714aa22019-07-02 03:45:336879 // 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 state changes. Need both TYPE_TAB and TYPE_URL
6885 // to update both the tab-level usage indicator and the usage indicator in the
6886 // omnibox.
Marijn Kruisselbrink46fa4bd2019-07-11 18:15:476887 auto it = native_file_system_directory_handles_.find(path);
6888 DCHECK(it != native_file_system_directory_handles_.end());
6889 DCHECK_NE(0u, it->second);
6890 it->second--;
6891 if (it->second == 0)
6892 native_file_system_directory_handles_.erase(it);
6893 if (native_file_system_directory_handles_.empty()) {
Marijn Kruisselbrinkf1714aa22019-07-02 03:45:336894 NotifyNavigationStateChanged(static_cast<content::InvalidateTypes>(
6895 INVALIDATE_TYPE_TAB | INVALIDATE_TYPE_URL));
6896 }
6897}
6898
6899void WebContentsImpl::IncrementWritableNativeFileSystemHandleCount() {
6900 // Trying to invalidate the tab state while being destroyed could result in a
6901 // use after free.
6902 if (IsBeingDestroyed())
6903 return;
6904
6905 // Notify for UI updates if the state changes. Need both TYPE_TAB and TYPE_URL
6906 // to update both the tab-level usage indicator and the usage indicator in the
6907 // omnibox.
6908 native_file_system_writable_handle_count_++;
6909 if (native_file_system_writable_handle_count_ == 1) {
6910 NotifyNavigationStateChanged(static_cast<content::InvalidateTypes>(
6911 INVALIDATE_TYPE_TAB | INVALIDATE_TYPE_URL));
6912 }
6913}
6914
6915void WebContentsImpl::DecrementWritableNativeFileSystemHandleCount() {
6916 // Trying to invalidate the tab state while being destroyed could result in a
6917 // use after free.
6918 if (IsBeingDestroyed())
6919 return;
6920
6921 // Notify for UI updates if the state changes. Need both TYPE_TAB and TYPE_URL
6922 // to update both the tab-level usage indicator and the usage indicator in the
6923 // omnibox.
6924 DCHECK_NE(0u, native_file_system_writable_handle_count_);
6925 native_file_system_writable_handle_count_--;
6926 if (native_file_system_writable_handle_count_ == 0) {
6927 NotifyNavigationStateChanged(static_cast<content::InvalidateTypes>(
6928 INVALIDATE_TYPE_TAB | INVALIDATE_TYPE_URL));
6929 }
6930}
6931
zqzhang8ac49002017-03-16 21:51:356932void WebContentsImpl::SetHasPersistentVideo(bool has_persistent_video) {
6933 if (has_persistent_video_ == has_persistent_video)
mlamouri5cd9ae82017-02-18 11:05:096934 return;
6935
zqzhang8ac49002017-03-16 21:51:356936 has_persistent_video_ = has_persistent_video;
mlamouri5cd9ae82017-02-18 11:05:096937 NotifyPreferencesChanged();
zqzhang8ac49002017-03-16 21:51:356938 media_web_contents_observer()->RequestPersistentVideo(has_persistent_video);
mlamouri5cd9ae82017-02-18 11:05:096939}
6940
Ian Vollick5d5c37f2019-06-19 22:50:576941void WebContentsImpl::SetSpatialNavigationDisabled(bool disabled) {
6942 if (is_spatial_navigation_disabled_ == disabled)
6943 return;
6944
6945 is_spatial_navigation_disabled_ = disabled;
6946 NotifyPreferencesChanged();
6947}
6948
paulmeyerfeafc2d2017-04-25 21:46:406949void WebContentsImpl::BrowserPluginGuestWillDetach() {
6950 WebContentsImpl* outermost = GetOutermostWebContents();
6951 if (this != outermost && ContainsOrIsFocusedWebContents())
6952 outermost->SetAsFocusedWebContentsIfNecessary();
6953}
6954
Becca Hughes112832e2019-06-11 17:19:026955PictureInPictureResult WebContentsImpl::EnterPictureInPicture(
Mounir Lamouri11e9ef42018-05-22 03:10:166956 const viz::SurfaceId& surface_id,
6957 const gfx::Size& natural_size) {
François Beaufortf09e4462019-01-18 14:34:516958 return delegate_
6959 ? delegate_->EnterPictureInPicture(this, surface_id, natural_size)
Becca Hughes112832e2019-06-11 17:19:026960 : PictureInPictureResult::kNotSupported;
Mounir Lamouri11e9ef42018-05-22 03:10:166961}
6962
Mounir Lamouri6d759e12018-05-16 20:01:306963void WebContentsImpl::ExitPictureInPicture() {
6964 if (delegate_)
6965 delegate_->ExitPictureInPicture();
6966}
6967
paulmeyerc0b762b2016-04-13 11:55:176968#if defined(OS_ANDROID)
6969void WebContentsImpl::NotifyFindMatchRectsReply(
6970 int version,
6971 const std::vector<gfx::RectF>& rects,
6972 const gfx::RectF& active_rect) {
6973 if (delegate_)
6974 delegate_->FindMatchRectsReply(this, version, rects, active_rect);
6975}
6976#endif
6977
[email protected]34ff1cfc2014-08-20 06:16:056978void WebContentsImpl::SetForceDisableOverscrollContent(bool force_disable) {
6979 force_disable_overscroll_content_ = force_disable;
6980 if (view_)
6981 view_->SetOverscrollControllerEnabled(CanOverscrollContent());
6982}
6983
Jianzhou Fengd8720c72018-01-18 03:06:306984bool WebContentsImpl::SetDeviceEmulationSize(const gfx::Size& new_size) {
6985 device_emulation_size_ = new_size;
6986 RenderWidgetHostView* rwhv = GetMainFrame()->GetView();
6987
6988 const gfx::Size current_size = rwhv->GetViewBounds().size();
6989 if (view_size_before_emulation_.IsEmpty())
6990 view_size_before_emulation_ = current_size;
6991
6992 if (current_size != new_size)
6993 rwhv->SetSize(new_size);
6994
6995 return current_size != new_size;
6996}
6997
6998void WebContentsImpl::ClearDeviceEmulationSize() {
6999 RenderWidgetHostView* rwhv = GetMainFrame()->GetView();
7000 // WebContentsView could get resized during emulation, which also resizes
7001 // RWHV. If it happens, assume user would like to keep using the size after
7002 // emulation.
7003 // TODO(jzfeng): Prohibit resizing RWHV through any other means (at least when
7004 // WebContentsView size changes).
Dmitry Gozmanc63707182018-03-06 00:09:397005 if (!view_size_before_emulation_.IsEmpty() && rwhv &&
7006 rwhv->GetViewBounds().size() == device_emulation_size_) {
Jianzhou Fengd8720c72018-01-18 03:06:307007 rwhv->SetSize(view_size_before_emulation_);
Dmitry Gozmanc63707182018-03-06 00:09:397008 }
Jianzhou Fengd8720c72018-01-18 03:06:307009 device_emulation_size_ = gfx::Size();
7010 view_size_before_emulation_ = gfx::Size();
7011}
7012
Max Morin5bc74f52018-05-09 07:00:217013ForwardingAudioStreamFactory* WebContentsImpl::GetAudioStreamFactory() {
7014 if (!audio_stream_factory_) {
7015 audio_stream_factory_.emplace(
7016 this,
Max Morinddebb972018-09-20 10:04:147017 // BrowserMainLoop::GetInstance() may be null in unit tests.
7018 BrowserMainLoop::GetInstance()
7019 ? static_cast<media::UserInputMonitorBase*>(
7020 BrowserMainLoop::GetInstance()->user_input_monitor())
7021 : nullptr,
Ken Rockotfd03e682019-06-20 21:13:547022 GetSystemConnector()->Clone(), AudioStreamBrokerFactory::CreateImpl());
Max Morin5bc74f52018-05-09 07:00:217023 }
7024
7025 return &*audio_stream_factory_;
7026}
7027
dalecurtis88c240072015-12-09 02:11:187028void WebContentsImpl::MediaStartedPlaying(
billorr21c005d2016-11-17 03:57:047029 const WebContentsObserver::MediaPlayerInfo& media_info,
Mounir Lamouri0f5bdf52019-04-04 16:32:357030 const MediaPlayerId& id) {
billorr21c005d2016-11-17 03:57:047031 if (media_info.has_video)
7032 currently_playing_video_count_++;
7033
ericwilligersde386342016-10-19 02:35:097034 for (auto& observer : observers_)
billorr21c005d2016-11-17 03:57:047035 observer.MediaStartedPlaying(media_info, id);
dalecurtis88c240072015-12-09 02:11:187036}
7037
7038void WebContentsImpl::MediaStoppedPlaying(
billorr21c005d2016-11-17 03:57:047039 const WebContentsObserver::MediaPlayerInfo& media_info,
Mounir Lamouri0f5bdf52019-04-04 16:32:357040 const MediaPlayerId& id,
Becca Hughes220bfc102017-11-14 18:24:077041 WebContentsObserver::MediaStoppedReason reason) {
billorr21c005d2016-11-17 03:57:047042 if (media_info.has_video)
7043 currently_playing_video_count_--;
7044
ericwilligersde386342016-10-19 02:35:097045 for (auto& observer : observers_)
Becca Hughes220bfc102017-11-14 18:24:077046 observer.MediaStoppedPlaying(media_info, id, reason);
billorr21c005d2016-11-17 03:57:047047}
7048
Mounir Lamouri0f5bdf52019-04-04 16:32:357049void WebContentsImpl::MediaResized(const gfx::Size& size,
7050 const MediaPlayerId& id) {
peconn257951522017-06-09 18:24:597051 cached_video_sizes_[id] = size;
7052
7053 for (auto& observer : observers_)
7054 observer.MediaResized(size, id);
7055}
7056
Peter E Conneccb34c22017-09-08 09:37:587057void WebContentsImpl::MediaEffectivelyFullscreenChanged(bool is_fullscreen) {
7058 for (auto& observer : observers_)
7059 observer.MediaEffectivelyFullscreenChanged(is_fullscreen);
7060}
7061
Peter Conn31c726a2017-08-17 11:07:347062base::Optional<gfx::Size> WebContentsImpl::GetFullscreenVideoSize() {
Mounir Lamouri0f5bdf52019-04-04 16:32:357063 base::Optional<MediaPlayerId> id =
Peter Conn31c726a2017-08-17 11:07:347064 media_web_contents_observer_->GetFullscreenVideoMediaPlayerId();
Jan Wilken Dörrie531be7ca2019-06-07 10:05:577065 if (id && base::Contains(cached_video_sizes_, id.value()))
Peter Conn31c726a2017-08-17 11:07:347066 return base::Optional<gfx::Size>(cached_video_sizes_[id.value()]);
Lei Zhang6410ec1a2019-01-31 21:38:587067 return base::nullopt;
peconn257951522017-06-09 18:24:597068}
7069
billorr21c005d2016-11-17 03:57:047070int WebContentsImpl::GetCurrentlyPlayingVideoCount() {
7071 return currently_playing_video_count_;
dalecurtis88c240072015-12-09 02:11:187072}
7073
Raymond Toye413650c2018-08-31 00:12:477074void WebContentsImpl::AudioContextPlaybackStarted(RenderFrameHost* host,
7075 int context_id) {
7076 WebContentsObserver::AudioContextId audio_context_id(host, context_id);
7077 for (auto& observer : observers_)
7078 observer.AudioContextPlaybackStarted(audio_context_id);
7079}
7080
7081void WebContentsImpl::AudioContextPlaybackStopped(RenderFrameHost* host,
7082 int context_id) {
7083 WebContentsObserver::AudioContextId audio_context_id(host, context_id);
7084 for (auto& observer : observers_)
7085 observer.AudioContextPlaybackStopped(audio_context_id);
7086}
7087
Ehsan Karamad1c631762019-05-13 23:04:017088RenderFrameHostImpl* WebContentsImpl::GetMainFrameForInnerDelegate(
7089 FrameTreeNode* frame_tree_node) {
7090 if (auto* web_contents = node_.GetInnerWebContentsInFrame(frame_tree_node))
7091 return web_contents->GetMainFrame();
7092 return nullptr;
7093}
7094
Francois Doraye6161152018-03-27 22:05:377095void WebContentsImpl::UpdateWebContentsVisibility(Visibility visibility) {
Francois Doray3f2afbd2018-04-06 19:18:187096 // Occlusion is disabled when |features::kWebContentsOcclusion| is disabled
7097 // (for power and speed impact assessment) or when
7098 // |switches::kDisableBackgroundingOccludedWindowsForTesting| is specified on
7099 // the command line (to avoid flakiness in browser tests).
7100 const bool occlusion_is_disabled =
7101 !base::FeatureList::IsEnabled(features::kWebContentsOcclusion) ||
Francois Doraye6161152018-03-27 22:05:377102 base::CommandLine::ForCurrentProcess()->HasSwitch(
7103 switches::kDisableBackgroundingOccludedWindowsForTesting);
7104 if (occlusion_is_disabled && visibility == Visibility::OCCLUDED)
7105 visibility = Visibility::VISIBLE;
7106
tapted65ff2ea72016-03-01 23:39:007107 if (!did_first_set_visible_) {
Francois Doraye6161152018-03-27 22:05:377108 if (visibility == Visibility::VISIBLE) {
7109 // A WebContents created with CreateParams::initially_hidden = false
7110 // starts with GetVisibility() == Visibility::VISIBLE even though it is
7111 // not really visible. Call WasShown() when it becomes visible for real as
7112 // the page load mechanism and some WebContentsObserver rely on that.
tapted65ff2ea72016-03-01 23:39:007113 WasShown();
Francois Dorayfe4a1772018-02-17 04:17:097114 did_first_set_visible_ = true;
tapted65ff2ea72016-03-01 23:39:007115 }
Francois Doraye6161152018-03-27 22:05:377116
7117 // Trust the initial visibility of the WebContents and do not switch it to
7118 // HIDDEN or OCCLUDED before it becomes VISIBLE for real. Doing so would
7119 // result in destroying resources that would immediately be recreated (e.g.
7120 // UpdateWebContents(HIDDEN) can be called when a WebContents is added to a
7121 // hidden window that is about to be shown).
7122
tapted65ff2ea72016-03-01 23:39:007123 return;
7124 }
Francois Doraye6161152018-03-27 22:05:377125
7126 if (visibility == visibility_)
tapted65ff2ea72016-03-01 23:39:007127 return;
7128
Francois Doraye6161152018-03-27 22:05:377129 if (visibility == Visibility::VISIBLE)
tapted65ff2ea72016-03-01 23:39:007130 WasShown();
Francois Doraye6161152018-03-27 22:05:377131 else if (visibility == Visibility::OCCLUDED)
7132 WasOccluded();
tapted65ff2ea72016-03-01 23:39:007133 else
7134 WasHidden();
7135}
7136
aelias5252baa2016-04-10 01:18:027137void WebContentsImpl::UpdateOverridingUserAgent() {
mlamouri5cd9ae82017-02-18 11:05:097138 NotifyPreferencesChanged();
aelias5252baa2016-04-10 01:18:027139}
7140
avid53461d2016-02-25 17:15:047141void WebContentsImpl::SetJavaScriptDialogManagerForTesting(
7142 JavaScriptDialogManager* dialog_manager) {
7143 dialog_manager_ = dialog_manager;
7144}
7145
rockotf62002a2016-09-15 00:08:597146void WebContentsImpl::RemoveBindingSet(const std::string& interface_name) {
7147 auto it = binding_sets_.find(interface_name);
7148 if (it != binding_sets_.end())
7149 binding_sets_.erase(it);
7150}
7151
nzolghadrd87a308d2016-12-07 15:45:567152bool WebContentsImpl::AddDomainInfoToRapporSample(rappor::Sample* sample) {
nzolghadrbbffc6c2017-05-25 17:56:397153 // Here we associate this metric to the main frame URL regardless of what
7154 // caused it.
nzolghadrd87a308d2016-12-07 15:45:567155 sample->SetStringField("Domain", ::rappor::GetDomainAndRegistrySampleFromGURL(
7156 GetLastCommittedURL()));
nzolghadrd87a308d2016-12-07 15:45:567157 return true;
7158}
7159
ekaramada110f642016-12-21 19:47:287160void WebContentsImpl::FocusedNodeTouched(bool editable) {
7161#if defined(OS_WIN)
ekaramada110f642016-12-21 19:47:287162 RenderWidgetHostView* view = GetRenderWidgetHostView();
7163 if (!view)
7164 return;
Dave Tapuska2315d8f2018-03-06 17:52:557165 view->FocusedNodeTouched(editable);
ekaramada110f642016-12-21 19:47:287166#endif
7167}
7168
falken52a56e32016-12-08 05:02:407169void WebContentsImpl::ShowInsecureLocalhostWarningIfNeeded() {
7170 bool allow_localhost = base::CommandLine::ForCurrentProcess()->HasSwitch(
7171 switches::kAllowInsecureLocalhost);
7172 if (!allow_localhost)
7173 return;
7174
7175 content::NavigationEntry* entry = GetController().GetLastCommittedEntry();
Rob Wuf79b3ba2018-01-14 01:54:317176 if (!entry || !net::IsLocalhost(entry->GetURL()))
falken52a56e32016-12-08 05:02:407177 return;
7178
7179 content::SSLStatus ssl_status = entry->GetSSL();
7180 bool is_cert_error = net::IsCertStatusError(ssl_status.cert_status) &&
7181 !net::IsCertStatusMinorError(ssl_status.cert_status);
7182 if (!is_cert_error)
7183 return;
7184
7185 GetMainFrame()->AddMessageToConsole(
Abhijeet Kandalkara5928a0a2019-03-13 05:04:427186 blink::mojom::ConsoleMessageLevel::kWarning,
falken52a56e32016-12-08 05:02:407187 base::StringPrintf("This site does not have a valid SSL "
7188 "certificate! Without SSL, your site's and "
7189 "visitors' data is vulnerable to theft and "
7190 "tampering. Get a valid SSL certificate before"
7191 " releasing your website to the public."));
7192}
7193
ekaramadf6750aa2017-06-06 18:29:427194bool WebContentsImpl::IsShowingContextMenuOnPage() const {
7195 return showing_context_menu_;
7196}
7197
Min Qina904f3302018-02-13 23:33:347198download::DownloadUrlParameters::RequestHeadersType
7199WebContentsImpl::ParseDownloadHeaders(const std::string& headers) {
7200 download::DownloadUrlParameters::RequestHeadersType request_headers;
Megan Jablonski2f6a4c52017-07-10 23:01:257201 for (const base::StringPiece& key_value : base::SplitStringPiece(
7202 headers, "\r\n", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY)) {
7203 std::vector<std::string> pair = base::SplitString(
7204 key_value, ":", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
7205 if (2ul == pair.size())
7206 request_headers.push_back(make_pair(pair[0], pair[1]));
7207 }
7208 return request_headers;
7209}
7210
lukasza6f8ac622017-06-06 03:10:207211void WebContentsImpl::SetOpenerForNewContents(FrameTreeNode* opener,
7212 bool opener_suppressed) {
7213 if (opener) {
7214 FrameTreeNode* new_root = GetFrameTree()->root();
7215
7216 // For the "original opener", track the opener's main frame instead, because
7217 // if the opener is a subframe, the opener tracking could be easily bypassed
7218 // by spawning from a subframe and deleting the subframe.
7219 // https://crbug.com/705316
7220 new_root->SetOriginalOpener(opener->frame_tree()->root());
7221
7222 if (!opener_suppressed) {
7223 new_root->SetOpener(opener);
7224 created_with_opener_ = true;
7225 }
7226 }
7227}
7228
Mounir Lamouri0f5bdf52019-04-04 16:32:357229void WebContentsImpl::MediaMutedStatusChanged(const MediaPlayerId& id,
7230 bool muted) {
Becca Hughes9f6fd4b82017-06-15 10:01:407231 for (auto& observer : observers_)
7232 observer.MediaMutedStatusChanged(id, muted);
7233}
7234
EhsanK2075c7e2017-08-21 02:42:397235void WebContentsImpl::SetVisibilityForChildViews(bool visible) {
7236 GetMainFrame()->SetVisibilityForChildViews(visible);
7237}
7238
Alison Maherbe15c072019-07-08 18:12:397239void WebContentsImpl::OnNativeThemeUpdated(ui::NativeTheme* observed_theme) {
7240 DCHECK(native_theme_observer_.IsObserving(observed_theme));
7241
7242 bool in_dark_mode = observed_theme->SystemDarkModeEnabled();
7243 bool in_high_contrast = observed_theme->UsesHighContrastColors();
Alison Maher67fd4d9f2019-07-12 18:34:217244 ui::NativeTheme::PreferredColorScheme preferred_color_scheme =
7245 observed_theme->GetPreferredColorScheme();
Alison Maherbe15c072019-07-08 18:12:397246 bool preferences_changed = false;
7247
7248 if (in_dark_mode_ != in_dark_mode) {
7249 in_dark_mode_ = in_dark_mode;
7250 preferences_changed = true;
7251 }
7252 if (in_high_contrast_ != in_high_contrast) {
7253 in_high_contrast_ = in_high_contrast;
7254 preferences_changed = true;
7255 }
Alison Maher67fd4d9f2019-07-12 18:34:217256 if (preferred_color_scheme_ != preferred_color_scheme) {
7257 preferred_color_scheme_ = preferred_color_scheme;
7258 preferences_changed = true;
7259 }
Alison Maherbe15c072019-07-08 18:12:397260
7261 if (preferences_changed)
7262 NotifyPreferencesChanged();
Rune Lillesveen5d8c49982019-04-24 19:07:307263}
7264
Dave Tapuskae782bea2019-07-09 16:21:517265mojom::FrameInputHandler* WebContentsImpl::GetFocusedFrameInputHandler() {
7266 auto* focused_frame = GetFocusedFrame();
7267 if (!focused_frame)
7268 return nullptr;
7269 return focused_frame->GetFrameInputHandler();
7270}
7271
[email protected]8ff00d72012-10-23 19:12:217272} // namespace content