blob: 85a1bd88ae5b319425e1de0719ca83dd885abe00 [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) {
Dana Friedc04ed872019-08-02 20:47:481455 // TODO: Share code with WasShown().
1456 SendPageMessage(new PageMsg_WasShown(MSG_ROUTING_NONE));
1457
1458 if (auto* view = GetRenderWidgetHostView()) {
1459 view->Show();
1460#if defined(OS_MACOSX)
1461 view->SetActive(true);
1462#endif
1463 }
1464
Ehsan Karamada03e1582018-12-05 23:07:561465 if (!ShowingInterstitialPage())
1466 SetVisibilityForChildViews(true);
Dana Friedc04ed872019-08-02 20:47:481467
1468 for (FrameTreeNode* node : frame_tree_.Nodes()) {
1469 RenderFrameProxyHost* parent = node->render_manager()->GetProxyToParent();
1470 if (!parent)
1471 continue;
1472
1473 parent->cross_process_frame_connector()->DelegateWasShown();
1474 }
Francois Doraye6161152018-03-27 22:05:371475 }
[email protected]54597982013-02-06 01:59:551476}
1477
1478void WebContentsImpl::DecrementCapturerCount() {
1479 --capturer_count_;
1480 DVLOG(1) << "There are now " << capturer_count_
1481 << " capturing(s) of WebContentsImpl@" << this;
1482 DCHECK_LE(0, capturer_count_);
1483
[email protected]5a652232013-02-12 06:15:251484 if (is_being_destroyed_)
1485 return;
1486
Francois Doray24fc62c2017-12-11 17:27:331487 if (!IsBeingCaptured()) {
[email protected]222f5822014-02-05 23:40:491488 const gfx::Size old_size = preferred_size_for_capture_;
1489 preferred_size_for_capture_ = gfx::Size();
1490 OnPreferredSizeChanged(old_size);
[email protected]222f5822014-02-05 23:40:491491
Francois Doraye6161152018-03-27 22:05:371492 if (visibility_ == Visibility::HIDDEN) {
Francois Doray18800972017-08-25 22:38:581493 DVLOG(1) << "Executing delayed WasHidden().";
1494 WasHidden();
Francois Doraye6161152018-03-27 22:05:371495 } else if (visibility_ == Visibility::OCCLUDED) {
Mitsuru Oshima04a80242018-03-03 03:06:151496 WasOccluded();
Francois Doraye6161152018-03-27 22:05:371497 }
[email protected]54597982013-02-06 01:59:551498 }
[email protected]be1f56ab2011-12-22 06:55:311499}
1500
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411501bool WebContentsImpl::IsBeingCaptured() {
Francois Doray24fc62c2017-12-11 17:27:331502 return capturer_count_ > 0;
[email protected]f2bd40812013-07-20 04:30:441503}
1504
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411505bool WebContentsImpl::IsAudioMuted() {
Max Morinb5506f72018-05-16 07:33:101506 if (base::FeatureList::IsEnabled(features::kAudioServiceAudioStreams)) {
1507 return audio_stream_factory_ && audio_stream_factory_->IsMuted();
1508 }
miu50f97892014-09-22 22:49:521509 return audio_muter_.get() && audio_muter_->is_muting();
1510}
1511
1512void WebContentsImpl::SetAudioMuted(bool mute) {
1513 DVLOG(1) << "SetAudioMuted(mute=" << mute << "), was " << IsAudioMuted()
1514 << " for WebContentsImpl@" << this;
1515
1516 if (mute == IsAudioMuted())
1517 return;
1518
Max Morinb5506f72018-05-16 07:33:101519 if (base::FeatureList::IsEnabled(features::kAudioServiceAudioStreams)) {
1520 GetAudioStreamFactory()->SetMuted(mute);
miu50f97892014-09-22 22:49:521521 } else {
Max Morinb5506f72018-05-16 07:33:101522 if (mute) {
1523 if (!audio_muter_)
1524 audio_muter_.reset(new WebContentsAudioMuter(this));
1525 audio_muter_->StartMuting();
1526 } else {
1527 DCHECK(audio_muter_);
1528 audio_muter_->StopMuting();
1529 }
miu50f97892014-09-22 22:49:521530 }
1531
ericwilligersde386342016-10-19 02:35:091532 for (auto& observer : observers_)
1533 observer.DidUpdateAudioMutingState(mute);
wjmaclean2f797c782016-01-07 14:52:031534
Jan Rucka442c83f2017-08-08 13:27:541535 // Notification for UI updates in response to the changed muting state.
1536 NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB);
1537}
1538
1539bool WebContentsImpl::IsCurrentlyAudible() {
Chris Hamiltondf0d72cd2018-05-29 16:23:531540 return is_currently_audible_;
miu50f97892014-09-22 22:49:521541}
1542
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411543bool WebContentsImpl::IsConnectedToBluetoothDevice() {
ortunodf4d7982016-04-08 02:33:351544 return bluetooth_connected_device_count_ > 0;
ortuno32e7db3c2016-03-29 16:14:201545}
1546
Lucas Furukawa Gadani4b4eed02019-06-04 23:12:041547bool WebContentsImpl::IsConnectedToSerialPort() {
Reilly Grant5e7c79b22019-04-09 17:26:201548 return serial_active_frame_count_ > 0;
1549}
1550
Marijn Kruisselbrinkf1714aa22019-07-02 03:45:331551bool WebContentsImpl::HasNativeFileSystemDirectoryHandles() {
Marijn Kruisselbrink46fa4bd2019-07-11 18:15:471552 return !native_file_system_directory_handles_.empty();
1553}
1554
1555std::vector<base::FilePath>
1556WebContentsImpl::GetNativeFileSystemDirectoryHandles() {
1557 std::vector<base::FilePath> result;
1558 result.reserve(native_file_system_directory_handles_.size());
1559 for (auto const& entry : native_file_system_directory_handles_)
1560 result.push_back(entry.first);
1561 return result;
Marijn Kruisselbrinkf1714aa22019-07-02 03:45:331562}
1563
1564bool WebContentsImpl::HasWritableNativeFileSystemHandles() {
1565 return native_file_system_writable_handle_count_ > 0;
1566}
1567
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411568bool WebContentsImpl::HasPictureInPictureVideo() {
sawtellea7333a82018-05-31 02:36:361569 return has_picture_in_picture_video_;
1570}
1571
1572void WebContentsImpl::SetHasPictureInPictureVideo(
1573 bool has_picture_in_picture_video) {
1574 // If status of |this| is already accurate, there is no need to update.
1575 if (has_picture_in_picture_video == has_picture_in_picture_video_)
1576 return;
1577 has_picture_in_picture_video_ = has_picture_in_picture_video;
1578 NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB);
François Beaufort058085d2018-08-30 15:29:141579 for (auto& observer : observers_)
1580 observer.MediaPictureInPictureChanged(has_picture_in_picture_video_);
sawtellea7333a82018-05-31 02:36:361581}
1582
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411583bool WebContentsImpl::IsCrashed() {
Dominic Mazzoni73a48322018-05-25 18:14:071584 switch (crashed_status_) {
1585 case base::TERMINATION_STATUS_PROCESS_CRASHED:
1586 case base::TERMINATION_STATUS_ABNORMAL_TERMINATION:
1587 case base::TERMINATION_STATUS_PROCESS_WAS_KILLED:
1588 case base::TERMINATION_STATUS_OOM:
1589 case base::TERMINATION_STATUS_LAUNCH_FAILED:
oshima620225722015-06-04 19:45:271590#if defined(OS_CHROMEOS)
Dominic Mazzoni73a48322018-05-25 18:14:071591 case base::TERMINATION_STATUS_PROCESS_WAS_KILLED_BY_OOM:
oshima620225722015-06-04 19:45:271592#endif
Dominic Mazzoni73a48322018-05-25 18:14:071593#if defined(OS_ANDROID)
1594 case base::TERMINATION_STATUS_OOM_PROTECTED:
1595#endif
1596 return true;
1597 case base::TERMINATION_STATUS_NORMAL_TERMINATION:
1598 case base::TERMINATION_STATUS_STILL_RUNNING:
1599 return false;
1600 case base::TERMINATION_STATUS_MAX_ENUM:
1601 NOTREACHED();
1602 return false;
1603 }
1604
1605 NOTREACHED();
1606 return false;
[email protected]3c9e1872010-11-18 16:17:491607}
1608
[email protected]b172aee2012-04-10 17:05:261609void WebContentsImpl::SetIsCrashed(base::TerminationStatus status,
1610 int error_code) {
[email protected]443b80e2010-12-14 00:42:231611 if (status == crashed_status_)
[email protected]d5f942ba2008-09-26 19:30:341612 return;
1613
[email protected]443b80e2010-12-14 00:42:231614 crashed_status_ = status;
1615 crashed_error_code_ = error_code;
[email protected]8ff00d72012-10-23 19:12:211616 NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB);
[email protected]d5f942ba2008-09-26 19:30:341617}
1618
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411619base::TerminationStatus WebContentsImpl::GetCrashedStatus() {
[email protected]be1f56ab2011-12-22 06:55:311620 return crashed_status_;
1621}
1622
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411623int WebContentsImpl::GetCrashedErrorCode() {
afakhry98241832016-03-11 19:27:471624 return crashed_error_code_;
1625}
1626
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411627bool WebContentsImpl::IsBeingDestroyed() {
[email protected]be1f56ab2011-12-22 06:55:311628 return is_being_destroyed_;
1629}
1630
[email protected]7f924832014-08-09 05:57:221631void WebContentsImpl::NotifyNavigationStateChanged(
1632 InvalidateTypes changed_flags) {
dalecurtis88c240072015-12-09 02:11:181633 // Notify the media observer of potential audibility changes.
1634 if (changed_flags & INVALIDATE_TYPE_TAB) {
mlamouri44e4ef42016-01-20 18:42:271635 media_web_contents_observer_->MaybeUpdateAudibleState();
dalecurtisbc6572e12014-09-12 19:22:301636 }
1637
[email protected]d5f942ba2008-09-26 19:30:341638 if (delegate_)
1639 delegate_->NavigationStateChanged(this, changed_flags);
wjmaclean1dabf3e32016-05-13 23:34:441640
1641 if (GetOuterWebContents())
1642 GetOuterWebContents()->NotifyNavigationStateChanged(changed_flags);
[email protected]d5f942ba2008-09-26 19:30:341643}
1644
W. James MacLean7c8581072019-06-18 18:07:531645void WebContentsImpl::NotifyVisibleViewportSizeChanged(
1646 const gfx::Size& visible_viewport_size) {
1647 SendPageMessage(new PageMsg_UpdatePageVisualProperties(
1648 MSG_ROUTING_NONE, visible_viewport_size));
1649}
1650
W. James MacLean6f34b042019-07-12 19:25:301651RenderFrameHostImpl* WebContentsImpl::GetFocusedFrameFromFocusedDelegate() {
1652 FrameTreeNode* focused_node =
1653 GetFocusedWebContents()->frame_tree_.GetFocusedFrame();
1654 return focused_node ? focused_node->current_frame_host() : nullptr;
1655}
1656
Chris Hamiltondf0d72cd2018-05-29 16:23:531657void WebContentsImpl::OnAudioStateChanged() {
1658 // This notification can come from any embedded contents or from this
1659 // WebContents' stream monitor. Aggregate these signals to get the actual
1660 // state.
1661 bool is_currently_audible =
1662 audio_stream_monitor_.IsCurrentlyAudible() ||
1663 (browser_plugin_embedder_ &&
1664 browser_plugin_embedder_->AreAnyGuestsCurrentlyAudible());
1665 if (is_currently_audible == is_currently_audible_)
1666 return;
1667
1668 // Update internal state.
1669 is_currently_audible_ = is_currently_audible;
1670 was_ever_audible_ = was_ever_audible_ || is_currently_audible_;
1671
1672 SendPageMessage(
1673 new PageMsg_AudioStateChanged(MSG_ROUTING_NONE, is_currently_audible_));
altimind8bd26c2016-11-04 11:44:541674
altiminec87fd1f2016-11-17 20:22:491675 // Notification for UI updates in response to the changed audio state.
altimind8bd26c2016-11-04 11:44:541676 NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB);
Jan Rucka442c83f2017-08-08 13:27:541677
Chris Hamiltondf0d72cd2018-05-29 16:23:531678 // Ensure that audio state changes propagate from innermost to outermost
1679 // WebContents.
1680 if (GetOuterWebContents())
1681 GetOuterWebContents()->OnAudioStateChanged();
Tommy Steimel18360512017-11-01 00:38:191682
Chris Hamiltoneb41198f2018-05-24 18:41:261683 for (auto& observer : observers_)
Chris Hamiltondf0d72cd2018-05-29 16:23:531684 observer.OnAudioStateChanged(is_currently_audible_);
altimind8bd26c2016-11-04 11:44:541685}
1686
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411687base::TimeTicks WebContentsImpl::GetLastActiveTime() {
[email protected]9a890452014-02-13 22:21:021688 return last_active_time_;
[email protected]3e324142012-06-25 18:26:331689}
1690
[email protected]9e2e4632012-07-27 16:38:411691void WebContentsImpl::WasShown() {
[email protected]96d185d2009-04-24 03:28:541692 controller_.SetActive(true);
[email protected]de3c5d82014-05-28 22:12:591693
danakjc1e71e4f2018-12-05 19:34:351694 // This shows the Page before showing the individual RenderWidgets, as
1695 // RenderWidgets will work to produce compositor frames and handle input
1696 // as soon as they are shown. But the Page and other classes do not expect to
1697 // be producing frames when the Page is hidden. So we make sure the Page is
1698 // shown first.
1699 SendPageMessage(new PageMsg_WasShown(MSG_ROUTING_NONE));
1700
EhsanK2075c7e2017-08-21 02:42:391701 if (auto* view = GetRenderWidgetHostView()) {
lfgf949e0e2017-05-10 22:18:111702 view->Show();
[email protected]789e9152009-08-04 21:59:431703#if defined(OS_MACOSX)
lfgf949e0e2017-05-10 22:18:111704 view->SetActive(true);
[email protected]789e9152009-08-04 21:59:431705#endif
1706 }
[email protected]96d185d2009-04-24 03:28:541707
EhsanK2075c7e2017-08-21 02:42:391708 if (!ShowingInterstitialPage())
1709 SetVisibilityForChildViews(true);
1710
gab0dccfef2015-05-20 18:43:391711 last_active_time_ = base::TimeTicks::Now();
Francois Doraye6161152018-03-27 22:05:371712 SetVisibility(Visibility::VISIBLE);
Lukasz Anforowicz873e6082018-05-17 00:12:331713
1714 for (FrameTreeNode* node : frame_tree_.Nodes()) {
1715 RenderFrameProxyHost* parent = node->render_manager()->GetProxyToParent();
1716 if (!parent)
1717 continue;
1718
Alex Moshchuk2b0c91d2018-10-10 18:41:161719 parent->cross_process_frame_connector()->DelegateWasShown();
Lukasz Anforowicz873e6082018-05-17 00:12:331720 }
[email protected]be1f56ab2011-12-22 06:55:311721}
1722
[email protected]b172aee2012-04-10 17:05:261723void WebContentsImpl::WasHidden() {
Jennifer Apacible72c31412018-06-19 16:52:241724 // If there are entities capturing screenshots or video (e.g. mirroring),
François Beaufort0e27cee02019-01-18 13:21:351725 // or in Picture-in-Picture mode, don't activate the "disable rendering"
1726 // optimization.
1727 if (!IsBeingCaptured() && !HasPictureInPictureVideo()) {
danakjc1e71e4f2018-12-05 19:34:351728 // This hides the individual RenderWidgets before hiding the Page, as
1729 // RenderWidgets will work to produce compositor frames and handle input
1730 // until they are hidden. But the Page and other classes do not expect to
1731 // be producing frames when the Page is hidden. So we stop the widgets,
1732 // then hide the Page.
1733 //
[email protected]151a63d2011-12-20 22:32:521734 // |GetRenderViewHost()| can be NULL if the user middle clicks a link to
[email protected]54597982013-02-06 01:59:551735 // open a tab in the background, then closes the tab before selecting it.
[email protected]b172aee2012-04-10 17:05:261736 // This is because closing the tab calls WebContentsImpl::Destroy(), which
[email protected]151a63d2011-12-20 22:32:521737 // removes the |GetRenderViewHost()|; then when we actually destroy the
[email protected]3e324142012-06-25 18:26:331738 // window, OnWindowPosChanged() notices and calls WasHidden() (which
[email protected]151a63d2011-12-20 22:32:521739 // calls us).
EhsanK2075c7e2017-08-21 02:42:391740 if (auto* view = GetRenderWidgetHostView())
lfgf949e0e2017-05-10 22:18:111741 view->Hide();
lfg717154072016-06-30 15:04:161742
EhsanK2075c7e2017-08-21 02:42:391743 if (!ShowingInterstitialPage())
Igor Eremeevef59187a2017-09-29 00:25:421744 SetVisibilityForChildViews(false);
[email protected]96d185d2009-04-24 03:28:541745 }
1746
François Beaufort0e27cee02019-01-18 13:21:351747 // Inform the renderer that the page was hidden if there are no entities
1748 // capturing screenshots or video (e.g. mirroring).
1749 if (!IsBeingCaptured())
1750 SendPageMessage(new PageMsg_WasHidden(MSG_ROUTING_NONE));
1751
Francois Doraye6161152018-03-27 22:05:371752 SetVisibility(Visibility::HIDDEN);
[email protected]375fa1b2012-05-22 22:05:371753}
1754
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411755bool WebContentsImpl::HasRecentInteractiveInputEvent() {
Daniel Cheng90196c82018-04-25 21:49:141756 static constexpr base::TimeDelta kMaxInterval =
1757 base::TimeDelta::FromSeconds(5);
1758 base::TimeDelta delta =
1759 ui::EventTimeForNow() - last_interactive_input_event_time_;
1760 // Note: the expectation is that the caller is typically expecting an input
1761 // event, e.g. validating that a WebUI message that requires a gesture is
Daniel Chenge81030b32019-07-17 20:20:231762 // actually attached to a gesture.
Daniel Cheng90196c82018-04-25 21:49:141763 return delta <= kMaxInterval;
1764}
1765
Avi Drissman738ea192018-08-29 20:24:161766void WebContentsImpl::SetIgnoreInputEvents(bool ignore_input_events) {
1767 ignore_input_events_ = ignore_input_events;
1768}
1769
Bo Liu168c8642017-08-28 18:26:021770#if defined(OS_ANDROID)
Bo Liu16dd7b8322018-05-02 21:19:501771void WebContentsImpl::SetMainFrameImportance(
1772 ChildProcessImportance importance) {
1773 GetMainFrame()->GetRenderWidgetHost()->SetImportance(importance);
Bo Liu7c6779e92017-08-16 02:02:281774 if (ShowingInterstitialPage()) {
Bo Liu16dd7b8322018-05-02 21:19:501775 static_cast<RenderFrameHostImpl*>(interstitial_page_->GetMainFrame())
1776 ->GetRenderWidgetHost()
1777 ->SetImportance(importance);
Bo Liu7c6779e92017-08-16 02:02:281778 }
Bo Liu7c6779e92017-08-16 02:02:281779}
Bo Liu168c8642017-08-28 18:26:021780#endif
Bo Liu7c6779e92017-08-16 02:02:281781
ccameron8807c38f2015-01-17 00:29:191782void WebContentsImpl::WasOccluded() {
François Beaufortfec686f2019-01-18 17:12:491783 // If there are entities capturing screenshots or video (e.g. mirroring),
1784 // or in Picture-in-Picture mode, don't activate the "disable rendering"
1785 // optimization.
1786 if (!IsBeingCaptured() && !HasPictureInPictureVideo()) {
Ehsan Karamada03e1582018-12-05 23:07:561787 if (auto* main_view = GetRenderWidgetHostView())
1788 main_view->WasOccluded();
1789 if (!ShowingInterstitialPage()) {
1790 // Occluding child view is treated as hiding the view
1791 // (https://crbug.com/903455).
1792 SetVisibilityForChildViews(false);
1793 }
Francois Doray18800972017-08-25 22:38:581794 }
François Beaufortfec686f2019-01-18 17:12:491795
1796 // Inform the renderer that the page was hidden if there are no entities
1797 // capturing screenshots or video (e.g. mirroring).
1798 if (!IsBeingCaptured())
1799 SendPageMessage(new PageMsg_WasHidden(MSG_ROUTING_NONE));
1800
Francois Doraye6161152018-03-27 22:05:371801 SetVisibility(Visibility::OCCLUDED);
ccameron8807c38f2015-01-17 00:29:191802}
1803
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411804Visibility WebContentsImpl::GetVisibility() {
Francois Doraye6161152018-03-27 22:05:371805 return visibility_;
Francois Dorayfe4a1772018-02-17 04:17:091806}
1807
Alex Moshchuk6fcaca752018-07-14 02:13:591808// TODO(alexmos): rename to NeedToFireBeforeUnloadOrUnload().
[email protected]b172aee2012-04-10 17:05:261809bool WebContentsImpl::NeedToFireBeforeUnload() {
[email protected]be1f56ab2011-12-22 06:55:311810 // TODO(creis): Should we fire even for interstitial pages?
Alex Moshchuk6fcaca752018-07-14 02:13:591811 if (ShowingInterstitialPage())
1812 return false;
1813
1814 if (!WillNotifyDisconnection())
1815 return false;
1816
1817 // Don't fire if the main frame's RenderViewHost indicates that beforeunload
1818 // and unload have already executed (e.g., after receiving a ClosePage ACK)
1819 // or should be ignored.
1820 if (GetRenderViewHost()->SuddenTerminationAllowed())
1821 return false;
1822
Aditya Keerthi34f37e62019-03-08 15:54:531823 // Check whether the main frame needs to run beforeunload or unload handlers.
1824 if (GetMainFrame()->GetSuddenTerminationDisablerState(
1825 blink::kBeforeUnloadHandler | blink::kUnloadHandler)) {
Alex Moshchuk6fcaca752018-07-14 02:13:591826 return true;
Aditya Keerthi34f37e62019-03-08 15:54:531827 }
Alex Moshchuk6fcaca752018-07-14 02:13:591828
1829 // Check whether any subframes need to run beforeunload handlers.
1830 //
1831 // TODO(alexmos): Also check whether subframes need to run unload handlers in
1832 // addition to beforeunload.
1833 return GetMainFrame()->ShouldDispatchBeforeUnload(
1834 true /* check_subframes_only */);
[email protected]be1f56ab2011-12-22 06:55:311835}
1836
Chris Hamilton3b3e315d2018-09-19 13:16:211837void WebContentsImpl::DispatchBeforeUnload(bool auto_cancel) {
1838 auto before_unload_type =
1839 auto_cancel ? RenderFrameHostImpl::BeforeUnloadType::DISCARD
1840 : RenderFrameHostImpl::BeforeUnloadType::TAB_CLOSE;
1841 GetMainFrame()->DispatchBeforeUnload(before_unload_type, false);
[email protected]1c3f80bd2014-04-08 23:02:061842}
1843
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:501844void WebContentsImpl::AttachInnerWebContents(
1845 std::unique_ptr<WebContents> inner_web_contents,
1846 RenderFrameHost* render_frame_host) {
1847 WebContentsImpl* inner_web_contents_impl =
1848 static_cast<WebContentsImpl*>(inner_web_contents.get());
1849 DCHECK(!inner_web_contents_impl->node_.outer_web_contents());
1850 auto* render_frame_host_impl =
1851 static_cast<RenderFrameHostImpl*>(render_frame_host);
1852 DCHECK_EQ(&frame_tree_,
1853 render_frame_host_impl->frame_tree_node()->frame_tree());
Lucas Furukawa Gadani105de1e812018-05-31 19:38:391854
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:501855 RenderFrameHostManager* inner_render_manager =
1856 inner_web_contents_impl->GetRenderManager();
1857 RenderFrameHostImpl* inner_main_frame =
1858 inner_render_manager->current_frame_host();
1859 RenderViewHostImpl* inner_render_view_host =
1860 inner_render_manager->current_host();
1861 auto* outer_render_manager =
1862 render_frame_host_impl->frame_tree_node()->render_manager();
lfg91a79e12016-04-01 23:52:191863
Lucas Furukawa Gadani105de1e812018-05-31 19:38:391864 // When attaching a WebContents as an inner WebContents, we need to replace
1865 // the Webcontents' view with a WebContentsViewChildFrame.
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:501866 inner_web_contents_impl->view_.reset(new WebContentsViewChildFrame(
1867 inner_web_contents_impl,
1868 GetContentClient()->browser()->GetWebContentsViewDelegate(
1869 inner_web_contents_impl),
1870 &inner_web_contents_impl->render_view_host_delegate_view_));
Lucas Furukawa Gadani105de1e812018-05-31 19:38:391871
lfg91a79e12016-04-01 23:52:191872 // When the WebContents being initialized has an opener, the browser side
1873 // Render{View,Frame}Host must be initialized and the RenderWidgetHostView
1874 // created. This is needed because the usual initialization happens during
1875 // the first navigation, but when attaching a new window we don't navigate
1876 // before attaching. If the browser side is already initialized, the calls
1877 // below will just early return.
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:501878 inner_render_manager->InitRenderView(inner_render_view_host, nullptr);
1879 inner_main_frame->Init();
1880 if (!inner_render_manager->GetRenderWidgetHostView()) {
1881 inner_web_contents_impl->CreateRenderWidgetHostViewForRenderManager(
1882 inner_render_view_host);
1883 }
lfg91a79e12016-04-01 23:52:191884
lazyboy6ec48b2a2015-06-29 15:18:141885 // Create a link to our outer WebContents.
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:501886 node_.AttachInnerWebContents(std::move(inner_web_contents),
1887 render_frame_host_impl);
lazyboy6ec48b2a2015-06-29 15:18:141888
lazyboy6ec48b2a2015-06-29 15:18:141889 // Create a proxy in top-level RenderFrameHostManager, pointing to the
1890 // SiteInstance of the outer WebContents. The proxy will be used to send
1891 // postMessage to the inner WebContents.
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:501892 auto* proxy = inner_render_manager->CreateOuterDelegateProxy(
1893 render_frame_host_impl->GetSiteInstance());
lazyboy6ec48b2a2015-06-29 15:18:141894
Lucas Furukawa Gadani99125822019-01-03 15:41:491895 // When attaching a GuestView as an inner WebContents, there should already be
1896 // a live RenderFrame, which has to be swapped. When attaching a portal, there
1897 // will not be a live RenderFrame before creating the proxy.
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:501898 if (render_frame_host_impl->IsRenderFrameLive()) {
1899 inner_render_manager->SwapOuterDelegateFrame(render_frame_host_impl, proxy);
Lucas Furukawa Gadani99125822019-01-03 15:41:491900
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:501901 inner_web_contents_impl->ReattachToOuterWebContentsFrame();
Lucas Furukawa Gadani99125822019-01-03 15:41:491902 }
ekaramadadd882292016-06-08 15:22:561903
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:501904 if (frame_tree_.GetFocusedFrame() ==
1905 render_frame_host_impl->frame_tree_node()) {
1906 inner_web_contents_impl->SetFocusedFrame(
1907 inner_web_contents_impl->frame_tree_.root(),
1908 render_frame_host_impl->GetSiteInstance());
avallee7529b2a2017-04-26 11:37:511909 }
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:501910 outer_render_manager->set_attach_complete();
lazyboy6ec48b2a2015-06-29 15:18:141911}
1912
Lucas Furukawa Gadani99125822019-01-03 15:41:491913std::unique_ptr<WebContents> WebContentsImpl::DetachFromOuterWebContents() {
1914 DCHECK(node_.outer_web_contents());
1915 if (RenderWidgetHostViewBase* view =
1916 static_cast<RenderWidgetHostViewBase*>(GetMainFrame()->GetView())) {
1917 view->Destroy();
1918 }
1919 GetRenderManager()->DeleteOuterDelegateProxy(
1920 node_.OuterContentsFrameTreeNode()
1921 ->current_frame_host()
1922 ->GetSiteInstance());
1923 view_.reset(CreateWebContentsView(
1924 this, GetContentClient()->browser()->GetWebContentsViewDelegate(this),
1925 &render_view_host_delegate_view_));
1926 view_->CreateView(GetPreferredSize(), nullptr);
1927 std::unique_ptr<WebContents> web_contents =
1928 node_.DisconnectFromOuterWebContents();
1929 DCHECK_EQ(web_contents.get(), this);
1930 node_.SetFocusedWebContents(this);
1931 CreateRenderWidgetHostViewForRenderManager(GetRenderViewHost());
1932 return web_contents;
1933}
1934
W. James MacLean2dc75ed42019-03-06 14:31:091935void WebContentsImpl::RecursivelyRegisterFrameSinkIds() {
1936 auto* view = static_cast<RenderWidgetHostViewBase*>(
1937 GetRenderManager()->GetRenderWidgetHostView());
1938 DCHECK(view);
1939 if (!view->IsRenderWidgetHostViewChildFrame())
1940 return;
1941 static_cast<RenderWidgetHostViewChildFrame*>(view)->RegisterFrameSinkId();
1942
1943 for (auto* inner_web_contents : node_.GetInnerWebContents()) {
1944 static_cast<WebContentsImpl*>(inner_web_contents)
1945 ->RecursivelyRegisterFrameSinkIds();
1946 }
1947}
1948
Lucas Furukawa Gadaniaed1fed2017-10-13 17:34:141949void WebContentsImpl::ReattachToOuterWebContentsFrame() {
1950 DCHECK(node_.outer_web_contents());
1951 auto* render_manager = GetRenderManager();
1952 auto* parent_frame =
1953 node_.OuterContentsFrameTreeNode()->current_frame_host()->GetParent();
1954 render_manager->SetRWHViewForInnerContents(
1955 render_manager->GetRenderWidgetHostView());
1956
W. James MacLean2dc75ed42019-03-06 14:31:091957 RecursivelyRegisterFrameSinkIds();
Lucas Furukawa Gadaniaed1fed2017-10-13 17:34:141958
1959 // Set up the the guest's AX tree to point back at the embedder's AX tree.
1960 GetMainFrame()->set_browser_plugin_embedder_ax_tree_id(
1961 parent_frame->GetAXTreeID());
1962 GetMainFrame()->UpdateAXTreeData();
1963}
1964
dalecurtis6c58ed02016-10-28 23:02:371965void WebContentsImpl::DidChangeVisibleSecurityState() {
Emily Starkc663b0242019-04-26 19:34:451966 if (delegate_)
dalecurtis6c58ed02016-10-28 23:02:371967 delegate_->VisibleSecurityStateChanged(this);
Emily Starkc663b0242019-04-26 19:34:451968 for (auto& observer : observers_)
1969 observer.DidChangeVisibleSecurityState();
dalecurtis6c58ed02016-10-28 23:02:371970}
1971
Ahmed Fakhryd70bf37b2018-04-04 17:07:241972void WebContentsImpl::NotifyPreferencesChanged() {
1973 std::set<RenderViewHost*> render_view_host_set;
1974 for (FrameTreeNode* node : frame_tree_.Nodes()) {
Giovanni Ortuño Urquidi17a5d7c2018-04-30 06:48:531975 render_view_host_set.insert(
1976 node->current_frame_host()->GetRenderViewHost());
Ahmed Fakhryd70bf37b2018-04-04 17:07:241977 }
Giovanni Ortuño Urquidi17a5d7c2018-04-30 06:48:531978
Ahmed Fakhryd70bf37b2018-04-04 17:07:241979 for (RenderViewHost* render_view_host : render_view_host_set)
1980 render_view_host->OnWebkitPreferencesChanged();
1981}
1982
Josh Karlindaba39322019-07-17 23:24:331983void WebContentsImpl::OnCookiesRead(const GURL& url,
1984 const GURL& first_party_url,
1985 const net::CookieList& cookie_list,
1986 bool blocked_by_policy) {
1987 for (auto& observer : observers_) {
1988 observer.OnCookiesRead(url, first_party_url, cookie_list,
1989 blocked_by_policy);
1990 }
1991}
1992
1993void WebContentsImpl::OnCookieChange(const GURL& url,
1994 const GURL& first_party_url,
1995 const net::CanonicalCookie& cookie,
1996 bool blocked_by_policy) {
1997 for (auto& observer : observers_) {
1998 observer.OnCookieChange(url, first_party_url, cookie, blocked_by_policy);
1999 }
2000}
2001
[email protected]b172aee2012-04-10 17:05:262002void WebContentsImpl::Stop() {
dcheng57e39e22016-01-21 00:25:382003 for (FrameTreeNode* node : frame_tree_.Nodes())
2004 node->StopLoading();
ericwilligersde386342016-10-19 02:35:092005 for (auto& observer : observers_)
2006 observer.NavigationStopped();
[email protected]0bfbf882011-12-22 18:19:272007}
2008
Francois Doray47f759d2018-06-11 18:13:512009void WebContentsImpl::SetPageFrozen(bool frozen) {
2010 // A visible page is never frozen.
2011 DCHECK_NE(Visibility::VISIBLE, GetVisibility());
2012
2013 SendPageMessage(new PageMsg_SetPageFrozen(MSG_ROUTING_NONE, frozen));
Fadi Meawada6573e02018-03-10 00:52:112014}
2015
erikchen6c7df7f7a2018-05-03 18:13:592016std::unique_ptr<WebContents> WebContentsImpl::Clone() {
[email protected]14392a52012-05-02 20:28:442017 // We use our current SiteInstance since the cloned entry will use it anyway.
alexmose201c7cd2015-06-10 17:14:212018 // We pass our own opener so that the cloned page can access it if it was set
[email protected]ed245db2012-12-18 08:00:452019 // before.
[email protected]54944cde2012-12-09 09:24:592020 CreateParams create_params(GetBrowserContext(), GetSiteInstance());
[email protected]fc2b46b2014-05-03 16:33:452021 create_params.initial_size = GetContainerBounds().size();
Ian Clelland5cbaaf82017-11-27 22:00:032022 FrameTreeNode* opener = frame_tree_.root()->opener();
2023 RenderFrameHostImpl* opener_rfh = nullptr;
2024 if (opener)
2025 opener_rfh = opener->current_frame_host();
erikchen6c7df7f7a2018-05-03 18:13:592026 std::unique_ptr<WebContentsImpl> tc =
erikchen50735fd2018-05-05 15:08:332027 CreateWithOpener(create_params, opener_rfh);
Lucas Furukawa Gadani5553a152019-01-08 18:55:572028 tc->GetController().CopyStateFrom(&controller_, true);
ericwilligersde386342016-10-19 02:35:092029 for (auto& observer : observers_)
erikchen6c7df7f7a2018-05-03 18:13:592030 observer.DidCloneToNewWebContents(this, tc.get());
[email protected]0bfbf882011-12-22 18:19:272031 return tc;
2032}
2033
[email protected]14392a52012-05-02 20:28:442034void WebContentsImpl::Observe(int type,
[email protected]8ff00d72012-10-23 19:12:212035 const NotificationSource& source,
2036 const NotificationDetails& details) {
[email protected]14392a52012-05-02 20:28:442037 switch (type) {
[email protected]8ff00d72012-10-23 19:12:212038 case NOTIFICATION_RENDER_WIDGET_HOST_DESTROYED: {
2039 RenderWidgetHost* host = Source<RenderWidgetHost>(source).ptr();
[email protected]0b431992014-06-24 00:08:032040 RenderWidgetHostView* view = host->GetView();
2041 if (view == GetFullscreenRenderWidgetHostView()) {
2042 // We cannot just call view_->RestoreFocus() here. On some platforms,
2043 // attempting to focus the currently-invisible WebContentsView will be
2044 // flat-out ignored. Therefore, this boolean is used to track whether
2045 // we will request focus after the fullscreen widget has been
2046 // destroyed.
2047 fullscreen_widget_had_focus_at_shutdown_ = (view && view->HasFocus());
2048 } else {
alexmosc2a8cec2016-05-23 22:19:532049 for (auto i = pending_widget_views_.begin();
[email protected]0b431992014-06-24 00:08:032050 i != pending_widget_views_.end(); ++i) {
2051 if (host->GetView() == i->second) {
2052 pending_widget_views_.erase(i);
2053 break;
2054 }
[email protected]bafe6cd2012-05-23 23:09:502055 }
2056 }
2057 break;
2058 }
[email protected]14392a52012-05-02 20:28:442059 default:
2060 NOTREACHED();
2061 }
2062}
2063
[email protected]ec6c05f2013-10-23 18:41:572064WebContents* WebContentsImpl::GetWebContents() {
2065 return this;
2066}
2067
[email protected]54944cde2012-12-09 09:24:592068void WebContentsImpl::Init(const WebContents::CreateParams& params) {
[email protected]fa944cb82013-11-15 17:51:212069 // This is set before initializing the render manager since
[email protected]b0936d22013-11-28 06:47:362070 // RenderFrameHostManager::Init calls back into us via its delegate to ask if
[email protected]fa944cb82013-11-15 17:51:212071 // it should be hidden.
Francois Doraye6161152018-03-27 22:05:372072 visibility_ =
2073 params.initially_hidden ? Visibility::HIDDEN : Visibility::VISIBLE;
[email protected]d6fa88f2013-10-18 16:00:432074
Sebastien Marchand28cf23182018-06-20 02:39:352075 if (!params.last_active_time.is_null())
2076 last_active_time_ = params.last_active_time;
2077
dcheng3ce04b62015-10-26 23:30:552078 // The routing ids must either all be set or all be unset.
avic2c29d782014-12-12 00:23:542079 DCHECK((params.routing_id == MSG_ROUTING_NONE &&
dcheng3ce04b62015-10-26 23:30:552080 params.main_frame_routing_id == MSG_ROUTING_NONE &&
2081 params.main_frame_widget_routing_id == MSG_ROUTING_NONE) ||
avic2c29d782014-12-12 00:23:542082 (params.routing_id != MSG_ROUTING_NONE &&
dcheng3ce04b62015-10-26 23:30:552083 params.main_frame_routing_id != MSG_ROUTING_NONE &&
2084 params.main_frame_widget_routing_id != MSG_ROUTING_NONE));
2085
2086 scoped_refptr<SiteInstance> site_instance = params.site_instance;
2087 if (!site_instance)
2088 site_instance = SiteInstance::Create(params.browser_context);
Lukasz Anforowicz3caa8372018-06-05 17:22:072089 if (params.desired_renderer_state == CreateParams::kNoRendererProcess) {
2090 static_cast<SiteInstanceImpl*>(site_instance.get())
2091 ->PreventAssociationWithSpareProcess();
2092 }
dcheng3ce04b62015-10-26 23:30:552093
2094 // A main RenderFrameHost always has a RenderWidgetHost, since it is always a
2095 // local root by definition.
2096 // TODO(avi): Once RenderViewHostImpl has-a RenderWidgetHostImpl, it will no
2097 // longer be necessary to eagerly grab a routing ID for the view.
2098 // https://crbug.com/545684
2099 int32_t view_routing_id = params.routing_id;
2100 int32_t main_frame_widget_routing_id = params.main_frame_widget_routing_id;
2101 if (main_frame_widget_routing_id == MSG_ROUTING_NONE) {
Albert J. Wongcb004632018-07-10 22:58:252102 view_routing_id = site_instance->GetProcess()->GetNextRoutingID();
2103 main_frame_widget_routing_id =
dcheng3ce04b62015-10-26 23:30:552104 site_instance->GetProcess()->GetNextRoutingID();
2105 }
2106
Albert J. Wongcb004632018-07-10 22:58:252107 DCHECK_NE(view_routing_id, main_frame_widget_routing_id);
2108
Dave Tapuska485aca92017-08-08 00:47:582109 GetRenderManager()->Init(
2110 site_instance.get(), view_routing_id, params.main_frame_routing_id,
2111 main_frame_widget_routing_id, params.renderer_initiated_creation);
lukasza464d8692016-02-22 19:26:322112
lukasza5140a412016-09-15 21:12:302113 // blink::FrameTree::setName always keeps |unique_name| empty in case of a
2114 // main frame - let's do the same thing here.
2115 std::string unique_name;
lukasza464d8692016-02-22 19:26:322116 frame_tree_.root()->SetFrameName(params.main_frame_name, unique_name);
[email protected]d1198fd2012-08-13 22:50:192117
[email protected]fc2b46b2014-05-03 16:33:452118 WebContentsViewDelegate* delegate =
2119 GetContentClient()->browser()->GetWebContentsViewDelegate(this);
2120
sky52a39e342016-12-17 17:23:222121 if (GuestMode::IsCrossProcessFrameGuest(this)) {
2122 view_.reset(new WebContentsViewChildFrame(
2123 this, delegate, &render_view_host_delegate_view_));
2124 } else {
2125 view_.reset(CreateWebContentsView(this, delegate,
2126 &render_view_host_delegate_view_));
Scott Violetc884c7fd2017-11-03 23:51:012127 if (browser_plugin_guest_) {
2128 view_ = std::make_unique<WebContentsViewGuest>(
2129 this, browser_plugin_guest_.get(), std::move(view_),
2130 &render_view_host_delegate_view_);
2131 }
[email protected]d1198fd2012-08-13 22:50:192132 }
[email protected]fc2b46b2014-05-03 16:33:452133 CHECK(render_view_host_delegate_view_);
[email protected]d1198fd2012-08-13 22:50:192134 CHECK(view_.get());
2135
[email protected]ed245db2012-12-18 08:00:452136 gfx::Size initial_size = params.initial_size;
[email protected]54944cde2012-12-09 09:24:592137 view_->CreateView(initial_size, params.context);
[email protected]d1198fd2012-08-13 22:50:192138
brettw4b461082016-11-19 18:55:162139#if BUILDFLAG(ENABLE_PLUGINS)
tommyclieb25b2a2014-11-03 19:45:092140 plugin_content_origin_whitelist_.reset(
2141 new PluginContentOriginWhitelist(this));
2142#endif
2143
[email protected]d1198fd2012-08-13 22:50:192144 registrar_.Add(this,
[email protected]8ff00d72012-10-23 19:12:212145 NOTIFICATION_RENDER_WIDGET_HOST_DESTROYED,
2146 NotificationService::AllBrowserContextsAndSources());
[email protected]f28ef9a32014-05-12 16:36:102147
leon.hane4db177a2017-02-07 14:19:162148 screen_orientation_provider_.reset(new ScreenOrientationProvider(this));
[email protected]01f83f92014-06-17 14:41:542149
mlamouriefdca9d2014-09-16 16:55:402150 manifest_manager_host_.reset(new ManifestManagerHost(this));
2151
mfomitchev2b8b066a2016-01-28 19:23:152152#if defined(OS_ANDROID)
Gyuyoung Kim1f672aa2019-04-25 02:10:252153 date_time_chooser_.reset(new DateTimeChooserAndroid(this));
[email protected]583418cc2013-01-17 14:01:102154#endif
fsamuel7310a4262014-12-05 05:06:442155
2156 // BrowserPluginGuest::Init needs to be called after this WebContents has
2157 // a RenderWidgetHostViewGuest. That is, |view_->CreateView| above.
2158 if (browser_plugin_guest_)
2159 browser_plugin_guest_->Init();
nickf9acfbe2014-12-23 19:12:372160
2161 for (size_t i = 0; i < g_created_callbacks.Get().size(); i++)
2162 g_created_callbacks.Get().at(i).Run(this);
naskob21fe4872015-02-24 14:15:562163
2164 // If the WebContents creation was renderer-initiated, it means that the
2165 // corresponding RenderView and main RenderFrame have already been created.
2166 // Ensure observers are notified about this.
2167 if (params.renderer_initiated_creation) {
avi3627ecac2015-10-16 17:40:432168 GetRenderViewHost()->GetWidget()->set_renderer_initialized(true);
Alex Moshchuk27caae82017-09-11 23:11:182169 GetRenderViewHost()->DispatchRenderViewCreated();
naskob21fe4872015-02-24 14:15:562170 GetRenderManager()->current_frame_host()->SetRenderFrameCreated(true);
2171 }
naskof5940b9f2015-03-02 23:04:052172
lof84501da082016-05-23 21:22:542173 // Create the renderer process in advance if requested.
Lukasz Anforowicz3caa8372018-06-05 17:22:072174 if (params.desired_renderer_state ==
2175 CreateParams::kInitializeAndWarmupRendererProcess) {
lof84501da082016-05-23 21:22:542176 if (!GetRenderManager()->current_frame_host()->IsRenderFrameLive()) {
2177 GetRenderManager()->InitRenderView(GetRenderViewHost(), nullptr);
2178 }
2179 }
2180
naskof5940b9f2015-03-02 23:04:052181 // Ensure that observers are notified of the creation of this WebContents's
2182 // main RenderFrameHost. It must be done here for main frames, since the
2183 // NotifySwappedFromRenderManager expects view_ to already be created and that
2184 // happens after RenderFrameHostManager::Init.
2185 NotifySwappedFromRenderManager(
2186 nullptr, GetRenderManager()->current_frame_host(), true);
[email protected]d1198fd2012-08-13 22:50:192187}
2188
[email protected]7fff43e2013-05-21 20:21:102189void WebContentsImpl::OnWebContentsDestroyed(WebContentsImpl* web_contents) {
2190 RemoveDestructionObserver(web_contents);
2191
[email protected]ceee8cd2013-03-08 04:59:512192 // Clear a pending contents that has been closed before being shown.
alexmosc2a8cec2016-05-23 22:19:532193 for (auto iter = pending_contents_.begin(); iter != pending_contents_.end();
[email protected]ceee8cd2013-03-08 04:59:512194 ++iter) {
erikchenbee5c9622018-04-27 19:30:252195 if (iter->second.get() != web_contents)
[email protected]ceee8cd2013-03-08 04:59:512196 continue;
erikchenbee5c9622018-04-27 19:30:252197
2198 // Someone else has deleted the WebContents. That should never happen!
2199 // TODO(erikchen): Fix semantics here. https://crbug.com/832879.
2200 iter->second.release();
[email protected]ceee8cd2013-03-08 04:59:512201 pending_contents_.erase(iter);
[email protected]ceee8cd2013-03-08 04:59:512202 return;
2203 }
2204 NOTREACHED();
[email protected]14392a52012-05-02 20:28:442205}
2206
[email protected]7fff43e2013-05-21 20:21:102207void WebContentsImpl::AddDestructionObserver(WebContentsImpl* web_contents) {
Jan Wilken Dörrie73c901e2019-06-12 09:02:322208 if (!base::Contains(destruction_observers_, web_contents)) {
[email protected]7fff43e2013-05-21 20:21:102209 destruction_observers_[web_contents] =
Jeremy Roman04f27c372017-10-27 15:20:552210 std::make_unique<DestructionObserver>(this, web_contents);
[email protected]7fff43e2013-05-21 20:21:102211 }
2212}
2213
2214void WebContentsImpl::RemoveDestructionObserver(WebContentsImpl* web_contents) {
avia8cf81982016-08-12 20:28:462215 destruction_observers_.erase(web_contents);
[email protected]7fff43e2013-05-21 20:21:102216}
2217
[email protected]b172aee2012-04-10 17:05:262218void WebContentsImpl::AddObserver(WebContentsObserver* observer) {
[email protected]be1f56ab2011-12-22 06:55:312219 observers_.AddObserver(observer);
2220}
2221
[email protected]b172aee2012-04-10 17:05:262222void WebContentsImpl::RemoveObserver(WebContentsObserver* observer) {
[email protected]be1f56ab2011-12-22 06:55:312223 observers_.RemoveObserver(observer);
2224}
2225
[email protected]948481d2014-06-11 18:32:222226std::set<RenderWidgetHostView*>
2227WebContentsImpl::GetRenderWidgetHostViewsInTree() {
2228 std::set<RenderWidgetHostView*> set;
2229 if (ShowingInterstitialPage()) {
lfgf949e0e2017-05-10 22:18:112230 if (RenderWidgetHostView* rwhv = GetRenderWidgetHostView())
2231 set.insert(rwhv);
[email protected]948481d2014-06-11 18:32:222232 } else {
dchengafb53e22016-02-04 08:11:082233 for (RenderFrameHost* rfh : GetAllFrames()) {
lfgf949e0e2017-05-10 22:18:112234 if (RenderWidgetHostView* rwhv = static_cast<RenderFrameHostImpl*>(rfh)
2235 ->frame_tree_node()
2236 ->render_manager()
2237 ->GetRenderWidgetHostView()) {
2238 set.insert(rwhv);
2239 }
dchengafb53e22016-02-04 08:11:082240 }
[email protected]948481d2014-06-11 18:32:222241 }
[email protected]de3c5d82014-05-28 22:12:592242 return set;
2243}
2244
[email protected]b172aee2012-04-10 17:05:262245void WebContentsImpl::Activate() {
[email protected]d5f942ba2008-09-26 19:30:342246 if (delegate_)
2247 delegate_->ActivateContents(this);
2248}
2249
avi3e4b8512015-11-11 01:55:492250void WebContentsImpl::LostCapture(RenderWidgetHostImpl* render_widget_host) {
2251 if (!RenderViewHostImpl::From(render_widget_host))
2252 return;
2253
[email protected]63954792011-07-11 04:17:482254 if (delegate_)
2255 delegate_->LostCapture();
2256}
2257
Steven Holte38041342018-06-22 22:00:572258ukm::SourceId WebContentsImpl::GetUkmSourceIdForLastCommittedSource() const {
2259 return last_committed_source_id_;
2260}
2261
Ahmed Fakhryfaa32d22018-10-05 15:56:162262void WebContentsImpl::SetTopControlsShownRatio(
2263 RenderWidgetHostImpl* render_widget_host,
2264 float ratio) {
2265 if (!delegate_)
2266 return;
2267
2268 RenderFrameHostImpl* rfh = GetMainFrame();
2269 if (!rfh || render_widget_host != rfh->GetRenderWidgetHost())
2270 return;
2271
2272 delegate_->SetTopControlsShownRatio(this, ratio);
Ahmed Fakhry58e6ef542018-09-04 18:05:382273}
2274
Ahmed Fakhry58e6ef542018-09-04 18:05:382275void WebContentsImpl::SetTopControlsGestureScrollInProgress(bool in_progress) {
2276 if (delegate_)
2277 delegate_->SetTopControlsGestureScrollInProgress(in_progress);
2278}
2279
lazyboy63f5b312015-11-23 20:19:522280void WebContentsImpl::RenderWidgetCreated(
2281 RenderWidgetHostImpl* render_widget_host) {
2282 created_widgets_.insert(render_widget_host);
2283}
2284
[email protected]b24b68a2012-09-24 21:57:262285void WebContentsImpl::RenderWidgetDeleted(
2286 RenderWidgetHostImpl* render_widget_host) {
lazyboy63f5b312015-11-23 20:19:522287 // Note that |is_being_destroyed_| can be true at this point as
2288 // ~WebContentsImpl() calls RFHM::ClearRFHsPendingShutdown(), which might lead
2289 // us here.
2290 created_widgets_.erase(render_widget_host);
[email protected]b24b68a2012-09-24 21:57:262291
lazyboy63f5b312015-11-23 20:19:522292 if (is_being_destroyed_)
2293 return;
[email protected]44470a22013-01-24 01:21:542294
2295 if (render_widget_host &&
alexmosc9e76ec2016-05-16 22:59:372296 render_widget_host->GetRoutingID() == fullscreen_widget_routing_id_ &&
2297 render_widget_host->GetProcess()->GetID() ==
2298 fullscreen_widget_process_id_) {
[email protected]4aebbca2013-09-17 22:26:492299 if (delegate_ && delegate_->EmbedsFullscreenWidget())
mlamouri7a78d6fd2015-01-17 13:23:532300 delegate_->ExitFullscreenModeForTab(this);
ericwilligersde386342016-10-19 02:35:092301 for (auto& observer : observers_)
2302 observer.DidDestroyFullscreenWidget();
alexmosc9e76ec2016-05-16 22:59:372303 fullscreen_widget_process_id_ = ChildProcessHost::kInvalidUniqueID;
[email protected]44470a22013-01-24 01:21:542304 fullscreen_widget_routing_id_ = MSG_ROUTING_NONE;
[email protected]0b431992014-06-24 00:08:032305 if (fullscreen_widget_had_focus_at_shutdown_)
2306 view_->RestoreFocus();
[email protected]44470a22013-01-24 01:21:542307 }
lfgbee1e0a2016-06-08 21:24:212308
lfg7d4caad2017-03-22 09:01:452309 if (render_widget_host == mouse_lock_widget_)
lfgff8dfc8d2016-10-21 17:42:322310 LostMouseLock(mouse_lock_widget_);
Joe Downing192998b22018-03-22 15:51:362311
Joe Downingc1a57efe2018-05-09 18:23:542312 CancelKeyboardLock(render_widget_host);
[email protected]b24b68a2012-09-24 21:57:262313}
2314
miu9e14e492014-10-25 02:39:042315void WebContentsImpl::RenderWidgetGotFocus(
2316 RenderWidgetHostImpl* render_widget_host) {
calamity7fe55ce2015-04-10 03:59:372317 // Notify the observers if an embedded fullscreen widget was focused.
2318 if (delegate_ && render_widget_host && delegate_->EmbedsFullscreenWidget() &&
2319 render_widget_host->GetView() == GetFullscreenRenderWidgetHostView()) {
Michael Thiessen896405db2017-07-20 17:52:322320 NotifyWebContentsFocused(render_widget_host);
calamity7fe55ce2015-04-10 03:59:372321 }
miu9e14e492014-10-25 02:39:042322}
2323
zijiehe3bee08e22017-05-17 04:14:462324void WebContentsImpl::RenderWidgetLostFocus(
2325 RenderWidgetHostImpl* render_widget_host) {
Michael Thiessen896405db2017-07-20 17:52:322326 // Notify the observers if an embedded fullscreen widget lost focus.
2327 if (delegate_ && render_widget_host && delegate_->EmbedsFullscreenWidget() &&
2328 render_widget_host->GetView() == GetFullscreenRenderWidgetHostView()) {
2329 NotifyWebContentsLostFocus(render_widget_host);
2330 }
zijiehe3bee08e22017-05-17 04:14:462331}
2332
estaded0a0c992015-01-21 14:12:062333void WebContentsImpl::RenderWidgetWasResized(
rouslan2f5993f2015-01-29 00:18:312334 RenderWidgetHostImpl* render_widget_host,
Christopher Cameron07f666b2018-02-13 23:41:192335 const ScreenInfo& screen_info,
rouslan2f5993f2015-01-29 00:18:312336 bool width_changed) {
creisc014b402015-04-23 16:41:452337 RenderFrameHostImpl* rfh = GetMainFrame();
estadece344ec2015-01-23 00:12:292338 if (!rfh || render_widget_host != rfh->GetRenderWidgetHost())
estaded0a0c992015-01-21 14:12:062339 return;
estaded0a0c992015-01-21 14:12:062340
lfg8d649cc2017-04-28 18:04:302341 SendPageMessage(new PageMsg_UpdateScreenInfo(MSG_ROUTING_NONE, screen_info));
2342
ericwilligersde386342016-10-19 02:35:092343 for (auto& observer : observers_)
2344 observer.MainFrameWasResized(width_changed);
estaded0a0c992015-01-21 14:12:062345}
2346
skyf65d9bb2017-03-24 02:26:392347KeyboardEventProcessingResult WebContentsImpl::PreHandleKeyboardEvent(
2348 const NativeWebKeyboardEvent& event) {
2349 return delegate_ ? delegate_->PreHandleKeyboardEvent(this, event)
2350 : KeyboardEventProcessingResult::NOT_HANDLED;
[email protected]63954792011-07-11 04:17:482351}
2352
Scott Violetd5caa872018-10-16 22:00:512353bool WebContentsImpl::HandleKeyboardEvent(const NativeWebKeyboardEvent& event) {
[email protected]0b094002014-08-20 18:28:102354 if (browser_plugin_embedder_ &&
2355 browser_plugin_embedder_->HandleKeyboardEvent(event)) {
Scott Violetd5caa872018-10-16 22:00:512356 return true;
[email protected]0b094002014-08-20 18:28:102357 }
Scott Violetd5caa872018-10-16 22:00:512358 return delegate_ && delegate_->HandleKeyboardEvent(this, event);
[email protected]63954792011-07-11 04:17:482359}
2360
[email protected]d92026ef2014-03-03 21:04:132361bool WebContentsImpl::HandleWheelEvent(
[email protected]180ef242013-11-07 06:50:462362 const blink::WebMouseWheelEvent& event) {
[email protected]bccc4472013-04-18 16:37:192363#if !defined(OS_MACOSX)
lanweia93644f2015-01-21 22:00:332364 // On platforms other than Mac, control+mousewheel may change zoom. On Mac,
2365 // this isn't done for two reasons:
[email protected]bccc4472013-04-18 16:37:192366 // -the OS already has a gesture to do this through pinch-zoom
2367 // -if a user starts an inertial scroll, let's go, and presses control
2368 // (i.e. control+tab) then the OS's buffered scroll events will come in
2369 // with control key set which isn't what the user wants
Blink Reformat1c4d759e2017-04-09 16:34:542370 if (delegate_ && event.wheel_ticks_y &&
Ehsan Karamad0f83cb112018-09-06 22:07:362371 event.event_action == blink::WebMouseWheelEvent::EventAction::kPageZoom) {
w.shackleton49bcd392016-01-06 17:38:222372 // Count only integer cumulative scrolls as zoom events; this handles
2373 // smooth scroll and regular scroll device behavior.
Blink Reformat1c4d759e2017-04-09 16:34:542374 zoom_scroll_remainder_ += event.wheel_ticks_y;
w.shackleton49bcd392016-01-06 17:38:222375 int whole_zoom_scroll_remainder_ = std::lround(zoom_scroll_remainder_);
2376 zoom_scroll_remainder_ -= whole_zoom_scroll_remainder_;
2377 if (whole_zoom_scroll_remainder_ != 0) {
2378 delegate_->ContentsZoomChange(whole_zoom_scroll_remainder_ > 0);
2379 }
[email protected]fb3f066e2013-02-12 19:12:522380 return true;
2381 }
[email protected]bccc4472013-04-18 16:37:192382#endif
[email protected]fb3f066e2013-02-12 19:12:522383 return false;
2384}
2385
[email protected]04bce562014-01-30 05:34:542386bool WebContentsImpl::PreHandleGestureEvent(
2387 const blink::WebGestureEvent& event) {
[email protected]28042d32014-02-03 14:10:032388 return delegate_ && delegate_->PreHandleGestureEvent(this, event);
[email protected]04bce562014-01-30 05:34:542389}
2390
kenrb2a565f82015-09-02 20:24:592391RenderWidgetHostInputEventRouter* WebContentsImpl::GetInputEventRouter() {
wjmacleane31234f2015-12-14 17:20:452392 if (!is_being_destroyed_ && GetOuterWebContents())
2393 return GetOuterWebContents()->GetInputEventRouter();
2394
2395 if (!rwh_input_event_router_.get() && !is_being_destroyed_)
kenrb2a565f82015-09-02 20:24:592396 rwh_input_event_router_.reset(new RenderWidgetHostInputEventRouter);
2397 return rwh_input_event_router_.get();
2398}
2399
alexmos3fcd0ca2015-10-23 18:18:332400void WebContentsImpl::ReplicatePageFocus(bool is_focused) {
2401 // Focus loss may occur while this WebContents is being destroyed. Don't
2402 // send the message in this case, as the main frame's RenderFrameHost and
2403 // other state has already been cleared.
2404 if (is_being_destroyed_)
2405 return;
2406
2407 frame_tree_.ReplicatePageFocus(is_focused);
2408}
2409
alexmosc4cbacb2015-11-21 01:29:222410RenderWidgetHostImpl* WebContentsImpl::GetFocusedRenderWidgetHost(
2411 RenderWidgetHostImpl* receiving_widget) {
alexmosc4cbacb2015-11-21 01:29:222412 // Events for widgets other than the main frame (e.g., popup menus) should be
2413 // forwarded directly to the widget they arrived on.
2414 if (receiving_widget != GetMainFrame()->GetRenderWidgetHost())
2415 return receiving_widget;
alexmosc4d183e2015-11-06 00:46:522416
lfg1453e412017-04-11 00:48:502417 WebContentsImpl* focused_contents = GetFocusedWebContents();
2418
2419 // If the focused WebContents is showing an interstitial, return the
2420 // interstitial's widget.
2421 if (focused_contents->ShowingInterstitialPage()) {
2422 return static_cast<RenderFrameHostImpl*>(
creisadca9862017-06-16 19:12:112423 focused_contents->interstitial_page_->GetMainFrame())
lfg1453e412017-04-11 00:48:502424 ->GetRenderWidgetHost();
2425 }
2426
paulmeyerfeafc2d2017-04-25 21:46:402427 // If the focused WebContents is a guest WebContents, then get the focused
2428 // frame in the embedder WebContents instead.
2429 FrameTreeNode* focused_frame = nullptr;
2430 if (focused_contents->browser_plugin_guest_ &&
2431 !GuestMode::IsCrossProcessFrameGuest(focused_contents)) {
2432 focused_frame = frame_tree_.GetFocusedFrame();
2433 } else {
2434 focused_frame = GetFocusedWebContents()->frame_tree_.GetFocusedFrame();
2435 }
lfg1453e412017-04-11 00:48:502436
alexmosc4d183e2015-11-06 00:46:522437 if (!focused_frame)
alexmosc4cbacb2015-11-21 01:29:222438 return receiving_widget;
alexmosc4d183e2015-11-06 00:46:522439
alexmos732ca3a2015-12-08 02:01:022440 // The view may be null if a subframe's renderer process has crashed while
2441 // the subframe has focus. Drop the event in that case. Do not give
2442 // it to the main frame, so that the user doesn't unexpectedly type into the
2443 // wrong frame if a focused subframe renderer crashes while they type.
2444 RenderWidgetHostView* view = focused_frame->current_frame_host()->GetView();
2445 if (!view)
2446 return nullptr;
2447
2448 return RenderWidgetHostImpl::From(view->GetRenderWidgetHost());
alexmosc4d183e2015-11-06 00:46:522449}
2450
avallee9993fca2016-11-17 06:16:502451RenderWidgetHostImpl* WebContentsImpl::GetRenderWidgetHostWithPageFocus() {
lfg1453e412017-04-11 00:48:502452 WebContentsImpl* focused_web_contents = GetFocusedWebContents();
2453
2454 if (focused_web_contents->ShowingInterstitialPage()) {
2455 return static_cast<RenderFrameHostImpl*>(
creisadca9862017-06-16 19:12:112456 focused_web_contents->interstitial_page_->GetMainFrame())
lfg1453e412017-04-11 00:48:502457 ->GetRenderWidgetHost();
2458 }
Alex Valléedc80ae2ed2017-09-06 21:55:442459 if (!GuestMode::IsCrossProcessFrameGuest(focused_web_contents) &&
2460 focused_web_contents->browser_plugin_guest_) {
2461 // If this is a guest, we need to be controlled by our embedder.
2462 return focused_web_contents->GetOuterWebContents()
2463 ->GetMainFrame()
2464 ->GetRenderWidgetHost();
2465 }
lfg1453e412017-04-11 00:48:502466
2467 return focused_web_contents->GetMainFrame()->GetRenderWidgetHost();
avallee9993fca2016-11-17 06:16:502468}
2469
Dave Tapuska3ed44192018-05-01 18:53:302470void WebContentsImpl::EnterFullscreenMode(
2471 const GURL& origin,
2472 const blink::WebFullscreenOptions& options) {
mlamouri7a78d6fd2015-01-17 13:23:532473 // This method is being called to enter renderer-initiated fullscreen mode.
2474 // Make sure any existing fullscreen widget is shut down first.
[email protected]4aebbca2013-09-17 22:26:492475 RenderWidgetHostView* const widget_view = GetFullscreenRenderWidgetHostView();
avi85a4cef2015-12-18 20:13:372476 if (widget_view) {
2477 RenderWidgetHostImpl::From(widget_view->GetRenderWidgetHost())
2478 ->ShutdownAndDestroyWidget(true);
2479 }
[email protected]4aebbca2013-09-17 22:26:492480
Joe Downing13dd76b2018-04-09 18:32:152481 if (delegate_) {
Dave Tapuska3ed44192018-05-01 18:53:302482 delegate_->EnterFullscreenModeForTab(this, origin, options);
mlamouri7a78d6fd2015-01-17 13:23:532483
Joe Downing13dd76b2018-04-09 18:32:152484 if (keyboard_lock_widget_)
2485 delegate_->RequestKeyboardLock(this, esc_key_locked_);
2486 }
2487
ericwilligersde386342016-10-19 02:35:092488 for (auto& observer : observers_)
2489 observer.DidToggleFullscreenModeForTab(IsFullscreenForCurrentTab(), false);
mlamouri7a78d6fd2015-01-17 13:23:532490}
2491
bokanece34a82016-01-28 19:49:462492void WebContentsImpl::ExitFullscreenMode(bool will_cause_resize) {
mlamouri7a78d6fd2015-01-17 13:23:532493 // This method is being called to leave renderer-initiated fullscreen mode.
2494 // Make sure any existing fullscreen widget is shut down first.
2495 RenderWidgetHostView* const widget_view = GetFullscreenRenderWidgetHostView();
avi85a4cef2015-12-18 20:13:372496 if (widget_view) {
2497 RenderWidgetHostImpl::From(widget_view->GetRenderWidgetHost())
2498 ->ShutdownAndDestroyWidget(true);
2499 }
mlamouri7a78d6fd2015-01-17 13:23:532500
Joe Downing13dd76b2018-04-09 18:32:152501 if (delegate_) {
mlamouri7a78d6fd2015-01-17 13:23:532502 delegate_->ExitFullscreenModeForTab(this);
[email protected]657c8e02014-03-19 19:18:372503
Joe Downing13dd76b2018-04-09 18:32:152504 if (keyboard_lock_widget_)
2505 delegate_->CancelKeyboardLockRequest(this);
2506 }
2507
bokanece34a82016-01-28 19:49:462508 // The fullscreen state is communicated to the renderer through a resize
2509 // message. If the change in fullscreen state doesn't cause a view resize
2510 // then we must ensure web contents exit the fullscreen state by explicitly
2511 // sending a resize message. This is required for the situation of the browser
2512 // moving the view into a "browser fullscreen" state and then the contents
2513 // entering "tab fullscreen". Exiting the contents "tab fullscreen" then won't
2514 // have the side effect of the view resizing, hence the explicit call here is
2515 // required.
2516 if (!will_cause_resize) {
2517 if (RenderWidgetHostView* rwhv = GetRenderWidgetHostView()) {
2518 if (RenderWidgetHost* render_widget_host = rwhv->GetRenderWidgetHost())
Fady Samuel0b911822018-04-25 13:22:162519 render_widget_host->SynchronizeVisualProperties();
bokanece34a82016-01-28 19:49:462520 }
scheib3dcb6c932015-02-23 10:55:582521 }
2522
Arthur Sonzogni929d29f2018-09-17 14:19:442523 current_fullscreen_frame_ = nullptr;
Becca Hughese9e27952018-06-25 17:08:272524
ericwilligersde386342016-10-19 02:35:092525 for (auto& observer : observers_) {
2526 observer.DidToggleFullscreenModeForTab(IsFullscreenForCurrentTab(),
2527 will_cause_resize);
2528 }
Becca Hughesd11d6502018-07-31 17:01:282529
2530 if (display_cutout_host_impl_)
2531 display_cutout_host_impl_->DidExitFullscreen();
[email protected]8a5e0ca2011-08-25 06:30:472532}
2533
Becca Hughesfd5d8f892018-06-14 18:23:362534void WebContentsImpl::FullscreenStateChanged(RenderFrameHost* rfh,
2535 bool is_fullscreen) {
Arthur Sonzogni929d29f2018-09-17 14:19:442536 RenderFrameHostImpl* frame = static_cast<RenderFrameHostImpl*>(rfh);
Becca Hughesfd5d8f892018-06-14 18:23:362537
2538 if (is_fullscreen) {
Jan Wilken Dörrie531be7ca2019-06-07 10:05:572539 if (!base::Contains(fullscreen_frames_, frame)) {
Arthur Sonzogni929d29f2018-09-17 14:19:442540 fullscreen_frames_.insert(frame);
2541 FullscreenFrameSetUpdated();
Becca Hughesfd5d8f892018-06-14 18:23:362542 }
Arthur Sonzogni929d29f2018-09-17 14:19:442543 return;
2544 }
Becca Hughesfd5d8f892018-06-14 18:23:362545
Arthur Sonzogni929d29f2018-09-17 14:19:442546 // If |frame| is no longer in fullscreen, remove it and any descendants.
2547 // See https://fullscreen.spec.whatwg.org.
Arthur Sonzogni969a794e2018-10-11 08:56:092548 size_t size_before_deletion = fullscreen_frames_.size();
2549 base::EraseIf(fullscreen_frames_, [&](RenderFrameHostImpl* current) {
2550 return (current == frame || current->IsDescendantOf(frame));
Arthur Sonzogni929d29f2018-09-17 14:19:442551 });
2552
Arthur Sonzogni969a794e2018-10-11 08:56:092553 if (size_before_deletion != fullscreen_frames_.size())
Arthur Sonzogni929d29f2018-09-17 14:19:442554 FullscreenFrameSetUpdated();
2555}
2556
2557void WebContentsImpl::FullscreenFrameSetUpdated() {
2558 if (fullscreen_frames_.empty()) {
2559 current_fullscreen_frame_ = nullptr;
2560 return;
Becca Hughesfd5d8f892018-06-14 18:23:362561 }
2562
Arthur Sonzogni929d29f2018-09-17 14:19:442563 // Find the current fullscreen frame and call the observers.
2564 // If frame A is fullscreen, then frame B goes into inner fullscreen, then B
2565 // exits fullscreen - that will result in A being fullscreen.
2566 RenderFrameHostImpl* new_fullscreen_frame = *std::max_element(
2567 fullscreen_frames_.begin(), fullscreen_frames_.end(), FrameCompareDepth);
Becca Hughesfd5d8f892018-06-14 18:23:362568
Arthur Sonzogni929d29f2018-09-17 14:19:442569 // If we have already notified observers about this frame then we should not
2570 // fire the observers again.
2571 if (new_fullscreen_frame == current_fullscreen_frame_)
2572 return;
2573 current_fullscreen_frame_ = new_fullscreen_frame;
Becca Hughesfd5d8f892018-06-14 18:23:362574
Arthur Sonzogni929d29f2018-09-17 14:19:442575 for (auto& observer : observers_)
2576 observer.DidAcquireFullscreen(new_fullscreen_frame);
Becca Hughesfd5d8f892018-06-14 18:23:362577
Arthur Sonzogni929d29f2018-09-17 14:19:442578 if (display_cutout_host_impl_)
2579 display_cutout_host_impl_->DidAcquireFullscreen(new_fullscreen_frame);
Becca Hughesfd5d8f892018-06-14 18:23:362580}
2581
Clark DuValla0e42992018-09-17 17:00:112582#if defined(OS_ANDROID)
2583void WebContentsImpl::UpdateUserGestureCarryoverInfo() {
2584 if (delegate_)
2585 delegate_->UpdateUserGestureCarryoverInfo(this);
2586}
2587#endif
2588
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:412589bool WebContentsImpl::IsFullscreenForCurrentTab() {
[email protected]199bba6e2012-04-04 16:19:382590 return delegate_ ? delegate_->IsFullscreenForTabOrPending(this) : false;
[email protected]5d5f7af2011-10-01 01:38:122591}
2592
Malay Keshav87c42c02019-01-15 08:37:472593bool WebContentsImpl::ShouldShowStaleContentOnEviction() {
2594 return GetDelegate() && GetDelegate()->ShouldShowStaleContentOnEviction(this);
2595}
2596
billorr21c005d2016-11-17 03:57:042597bool WebContentsImpl::IsFullscreen() {
2598 return IsFullscreenForCurrentTab();
2599}
2600
avibf58fa0c2015-11-11 01:45:522601blink::WebDisplayMode WebContentsImpl::GetDisplayMode(
2602 RenderWidgetHostImpl* render_widget_host) const {
2603 if (!RenderViewHostImpl::From(render_widget_host))
Blink Reformat1c4d759e2017-04-09 16:34:542604 return blink::kWebDisplayModeBrowser;
avibf58fa0c2015-11-11 01:45:522605
mikhail.pozdnyakovc0e251b2015-04-15 06:51:122606 return delegate_ ? delegate_->GetDisplayMode(this)
Blink Reformat1c4d759e2017-04-09 16:34:542607 : blink::kWebDisplayModeBrowser;
mikhail.pozdnyakovc0e251b2015-04-15 06:51:122608}
2609
avic3aa8422015-11-09 20:57:222610void WebContentsImpl::RequestToLockMouse(
2611 RenderWidgetHostImpl* render_widget_host,
2612 bool user_gesture,
lfgad8244352016-07-13 16:55:512613 bool last_unlocked_by_target,
2614 bool privileged) {
lfgf0cd46e2017-01-04 00:05:232615 for (WebContentsImpl* current = this; current;
2616 current = current->GetOuterWebContents()) {
2617 if (current->mouse_lock_widget_) {
2618 render_widget_host->GotResponseToLockMouseRequest(false);
2619 return;
2620 }
[email protected]e9621112011-10-17 05:38:372621 }
avic3aa8422015-11-09 20:57:222622
lfgad8244352016-07-13 16:55:512623 if (privileged) {
lfgf0cd46e2017-01-04 00:05:232624 DCHECK(!GetOuterWebContents());
lfgad8244352016-07-13 16:55:512625 mouse_lock_widget_ = render_widget_host;
2626 render_widget_host->GotResponseToLockMouseRequest(true);
2627 return;
2628 }
2629
lfgbee1e0a2016-06-08 21:24:212630 bool widget_in_frame_tree = false;
2631 for (FrameTreeNode* node : frame_tree_.Nodes()) {
2632 if (node->current_frame_host()->GetRenderWidgetHost() ==
2633 render_widget_host) {
2634 widget_in_frame_tree = true;
2635 break;
2636 }
2637 }
2638
2639 if (widget_in_frame_tree && delegate_) {
lfgf0cd46e2017-01-04 00:05:232640 for (WebContentsImpl* current = this; current;
2641 current = current->GetOuterWebContents()) {
2642 current->mouse_lock_widget_ = render_widget_host;
2643 }
2644
avic3aa8422015-11-09 20:57:222645 delegate_->RequestToLockMouse(this, user_gesture, last_unlocked_by_target);
lfgbee1e0a2016-06-08 21:24:212646 } else {
2647 render_widget_host->GotResponseToLockMouseRequest(false);
2648 }
[email protected]e9621112011-10-17 05:38:372649}
2650
avi3e4b8512015-11-11 01:55:492651void WebContentsImpl::LostMouseLock(RenderWidgetHostImpl* render_widget_host) {
lfgbee1e0a2016-06-08 21:24:212652 CHECK(mouse_lock_widget_);
lfgf0cd46e2017-01-04 00:05:232653
2654 if (mouse_lock_widget_->delegate()->GetAsWebContents() != this)
2655 return mouse_lock_widget_->delegate()->LostMouseLock(render_widget_host);
2656
lfgbee1e0a2016-06-08 21:24:212657 mouse_lock_widget_->SendMouseLockLost();
lfgf0cd46e2017-01-04 00:05:232658 for (WebContentsImpl* current = this; current;
2659 current = current->GetOuterWebContents()) {
2660 current->mouse_lock_widget_ = nullptr;
2661 }
avi3e4b8512015-11-11 01:55:492662
[email protected]e9621112011-10-17 05:38:372663 if (delegate_)
2664 delegate_->LostMouseLock();
2665}
2666
lfgad8244352016-07-13 16:55:512667bool WebContentsImpl::HasMouseLock(RenderWidgetHostImpl* render_widget_host) {
2668 // To verify if the mouse is locked, the mouse_lock_widget_ needs to be
2669 // assigned to the widget that requested the mouse lock, and the top-level
2670 // platform RenderWidgetHostView needs to hold the mouse lock from the OS.
Lucas Gadania0ea0172018-09-20 18:31:372671 auto* widget_host = GetTopLevelRenderWidgetHostView();
2672 return mouse_lock_widget_ == render_widget_host && widget_host &&
2673 widget_host->IsMouseLocked();
lfgad8244352016-07-13 16:55:512674}
2675
lfg84763c92017-02-16 18:55:152676RenderWidgetHostImpl* WebContentsImpl::GetMouseLockWidget() {
Lucas Gadania0ea0172018-09-20 18:31:372677 auto* widget_host = GetTopLevelRenderWidgetHostView();
2678 if ((widget_host && widget_host->IsMouseLocked()) ||
lfge2176fb2017-03-26 00:45:472679 (GetFullscreenRenderWidgetHostView() &&
Lucas Gadania0ea0172018-09-20 18:31:372680 GetFullscreenRenderWidgetHostView()->IsMouseLocked())) {
lfg84763c92017-02-16 18:55:152681 return mouse_lock_widget_;
Lucas Gadania0ea0172018-09-20 18:31:372682 }
lfg84763c92017-02-16 18:55:152683
2684 return nullptr;
2685}
2686
Joe Downing192998b22018-03-22 15:51:362687bool WebContentsImpl::RequestKeyboardLock(
Joe Downing13dd76b2018-04-09 18:32:152688 RenderWidgetHostImpl* render_widget_host,
2689 bool esc_key_locked) {
Joe Downing192998b22018-03-22 15:51:362690 DCHECK(render_widget_host);
Joe Downing192998b22018-03-22 15:51:362691 if (render_widget_host->delegate()->GetAsWebContents() != this) {
2692 NOTREACHED();
2693 return false;
2694 }
2695
2696 // KeyboardLock is only supported when called by the top-level browsing
2697 // context and is not supported in embedded content scenarios.
2698 if (GetOuterWebContents())
2699 return false;
2700
Joe Downing13dd76b2018-04-09 18:32:152701 esc_key_locked_ = esc_key_locked;
Joe Downing192998b22018-03-22 15:51:362702 keyboard_lock_widget_ = render_widget_host;
2703
Joe Downing13dd76b2018-04-09 18:32:152704 if (delegate_)
2705 delegate_->RequestKeyboardLock(this, esc_key_locked_);
Joe Downing192998b22018-03-22 15:51:362706 return true;
2707}
2708
2709void WebContentsImpl::CancelKeyboardLock(
2710 RenderWidgetHostImpl* render_widget_host) {
2711 if (!keyboard_lock_widget_ || render_widget_host != keyboard_lock_widget_)
2712 return;
2713
2714 RenderWidgetHostImpl* old_keyboard_lock_widget = keyboard_lock_widget_;
2715 keyboard_lock_widget_ = nullptr;
2716
Joe Downing13dd76b2018-04-09 18:32:152717 if (delegate_)
2718 delegate_->CancelKeyboardLockRequest(this);
2719
Joe Downing192998b22018-03-22 15:51:362720 old_keyboard_lock_widget->CancelKeyboardLock();
2721}
2722
2723RenderWidgetHostImpl* WebContentsImpl::GetKeyboardLockWidget() {
2724 return keyboard_lock_widget_;
2725}
2726
Dave Tapuskac3344362019-02-20 17:54:282727void WebContentsImpl::OnRenderFrameProxyVisibilityChanged(
2728 blink::mojom::FrameVisibility visibility) {
2729 switch (visibility) {
2730 case blink::mojom::FrameVisibility::kRenderedInViewport:
2731 WasShown();
2732 break;
2733 case blink::mojom::FrameVisibility::kNotRendered:
2734 WasHidden();
2735 break;
2736 case blink::mojom::FrameVisibility::kRenderedOutOfViewport:
2737 WasOccluded();
2738 break;
2739 }
ekaramada4f42692016-02-11 19:48:372740}
2741
[email protected]bafe6cd2012-05-23 23:09:502742void WebContentsImpl::CreateNewWindow(
csharrison95f01e922017-04-24 18:52:352743 RenderFrameHost* opener,
nick5ae4d2d2017-01-06 01:18:352744 int32_t render_view_route_id,
dcheng3ce04b62015-10-26 23:30:552745 int32_t main_frame_route_id,
2746 int32_t main_frame_widget_route_id,
rockot5c478a72016-09-28 23:14:182747 const mojom::CreateNewWindowParams& params,
Sergey Kuznetsov32257a22019-02-11 20:26:502748 bool has_user_gesture,
[email protected]97714c82012-06-06 10:15:132749 SessionStorageNamespace* session_storage_namespace) {
nick5ae4d2d2017-01-06 01:18:352750 // We should have zero valid routing ids, or three valid routing IDs.
2751 DCHECK_EQ((render_view_route_id == MSG_ROUTING_NONE),
2752 (main_frame_route_id == MSG_ROUTING_NONE));
2753 DCHECK_EQ((render_view_route_id == MSG_ROUTING_NONE),
2754 (main_frame_widget_route_id == MSG_ROUTING_NONE));
csharrison95f01e922017-04-24 18:52:352755 DCHECK(opener);
nick5ae4d2d2017-01-06 01:18:352756
csharrison95f01e922017-04-24 18:52:352757 int render_process_id = opener->GetProcess()->GetID();
2758 SiteInstance* source_site_instance = opener->GetSiteInstance();
2759
2760 // The route IDs passed into this function can be trusted not to already
2761 // be in use; they were allocated by the RenderWidgetHelper by the caller.
nick5ae4d2d2017-01-06 01:18:352762 DCHECK(!RenderFrameHostImpl::FromID(render_process_id, main_frame_route_id));
2763
[email protected]bafe6cd2012-05-23 23:09:502764 // We usually create the new window in the same BrowsingInstance (group of
2765 // script-related windows), by passing in the current SiteInstance. However,
[email protected]c4538072013-03-18 02:17:552766 // if the opener is being suppressed (in a non-guest), we create a new
2767 // SiteInstance in its own BrowsingInstance.
[email protected]a24efc22014-05-26 15:50:252768 bool is_guest = BrowserPluginGuest::IsGuest(this);
[email protected]c4538072013-03-18 02:17:552769
[email protected]04cbd3d2013-12-04 04:58:202770 // If the opener is to be suppressed, the new window can be in any process.
2771 // Since routing ids are process specific, we must not have one passed in
2772 // as argument here.
nick5ae4d2d2017-01-06 01:18:352773 DCHECK(!params.opener_suppressed || render_view_route_id == MSG_ROUTING_NONE);
[email protected]04cbd3d2013-12-04 04:58:202774
[email protected]bafe6cd2012-05-23 23:09:502775 scoped_refptr<SiteInstance> site_instance =
alexmos4cf2aa32015-07-15 23:40:432776 params.opener_suppressed && !is_guest
2777 ? SiteInstance::CreateForURL(GetBrowserContext(), params.target_url)
2778 : source_site_instance;
[email protected]bafe6cd2012-05-23 23:09:502779
[email protected]d1198fd2012-08-13 22:50:192780 // We must assign the SessionStorageNamespace before calling Init().
[email protected]4c3a23582012-08-18 08:54:342781 //
2782 // http://crbug.com/142685
[email protected]fdac6ade2013-07-20 01:06:302783 const std::string& partition_id =
2784 GetContentClient()->browser()->
2785 GetStoragePartitionIdForSite(GetBrowserContext(),
2786 site_instance->GetSiteURL());
[email protected]fc72bb12013-06-02 21:13:462787 StoragePartition* partition = BrowserContext::GetStoragePartition(
2788 GetBrowserContext(), site_instance.get());
[email protected]5f2aa722013-08-07 16:59:412789 DOMStorageContextWrapper* dom_storage_context =
2790 static_cast<DOMStorageContextWrapper*>(partition->GetDOMStorageContext());
[email protected]d1198fd2012-08-13 22:50:192791 SessionStorageNamespaceImpl* session_storage_namespace_impl =
2792 static_cast<SessionStorageNamespaceImpl*>(session_storage_namespace);
2793 CHECK(session_storage_namespace_impl->IsFromContext(dom_storage_context));
[email protected]dd6730412013-08-14 15:03:372794
2795 if (delegate_ &&
dcheng3ce04b62015-10-26 23:30:552796 !delegate_->ShouldCreateWebContents(
lukasza6f8ac622017-06-06 03:10:202797 this, opener, source_site_instance, render_view_route_id,
2798 main_frame_route_id, main_frame_widget_route_id,
2799 params.window_container_type, opener->GetLastCommittedURL(),
2800 params.frame_name, params.target_url, partition_id,
2801 session_storage_namespace)) {
jambaaab3a2016-11-05 00:46:182802 // Note: even though we're not creating a WebContents here, it could have
2803 // been created by the embedder so ensure that the RenderFrameHost is
2804 // properly initialized.
csharrisona2280cd2016-02-03 23:21:152805 // It's safe to only target the frame because the render process will not
2806 // have a chance to create more frames at this point.
jambaaab3a2016-11-05 00:46:182807 RenderFrameHostImpl* rfh =
2808 RenderFrameHostImpl::FromID(render_process_id, main_frame_route_id);
nick5ae4d2d2017-01-06 01:18:352809 if (rfh) {
2810 DCHECK(rfh->IsRenderFrameLive());
jambaaab3a2016-11-05 00:46:182811 rfh->Init();
nick5ae4d2d2017-01-06 01:18:352812 }
[email protected]dd6730412013-08-14 15:03:372813 return;
2814 }
2815
danakj22b4e8b2019-08-01 23:14:392816 bool renderer_started_hidden =
2817 params.disposition == WindowOpenDisposition::NEW_BACKGROUND_TAB;
2818
[email protected]dd6730412013-08-14 15:03:372819 // Create the new web contents. This will automatically create the new
2820 // WebContentsView. In the future, we may want to create the view separately.
[email protected]fc72bb12013-06-02 21:13:462821 CreateParams create_params(GetBrowserContext(), site_instance.get());
nick5ae4d2d2017-01-06 01:18:352822 create_params.routing_id = render_view_route_id;
[email protected]227692c52013-05-31 22:43:042823 create_params.main_frame_routing_id = main_frame_route_id;
dcheng3ce04b62015-10-26 23:30:552824 create_params.main_frame_widget_routing_id = main_frame_widget_route_id;
nasko48321ca32015-07-02 20:44:122825 create_params.main_frame_name = params.frame_name;
alexmos4cf2aa32015-07-15 23:40:432826 create_params.opener_render_process_id = render_process_id;
csharrison95f01e922017-04-24 18:52:352827 create_params.opener_render_frame_id = opener->GetRoutingID();
[email protected]50d326e2014-05-20 17:59:062828 create_params.opener_suppressed = params.opener_suppressed;
danakj22b4e8b2019-08-01 23:14:392829 create_params.initially_hidden = renderer_started_hidden;
lfg269b702f2015-06-08 19:28:192830 create_params.renderer_initiated_creation =
2831 main_frame_route_id != MSG_ROUTING_NONE;
[email protected]50d326e2014-05-20 17:59:062832
erikchenbee5c9622018-04-27 19:30:252833 std::unique_ptr<WebContents> new_contents;
[email protected]c4538072013-03-18 02:17:552834 if (!is_guest) {
2835 create_params.context = view_->GetNativeView();
[email protected]fc2b46b2014-05-03 16:33:452836 create_params.initial_size = GetContainerBounds().size();
Erik Chenbb8e738e2018-04-28 14:10:432837 new_contents = WebContents::Create(create_params);
[email protected]4858e432014-06-23 18:14:172838 } else {
erikchenbee5c9622018-04-27 19:30:252839 new_contents = base::WrapUnique(
2840 GetBrowserPluginGuest()->CreateNewGuestWindow(create_params));
[email protected]c4538072013-03-18 02:17:552841 }
danakje6c01622018-10-05 19:39:262842 auto owning_contents_impl =
2843 base::WrapUnique(static_cast<WebContentsImpl*>(new_contents.release()));
2844 auto* new_contents_impl = owning_contents_impl.get();
2845
2846 new_contents_impl->GetController().SetSessionStorageNamespace(
erikchenbee5c9622018-04-27 19:30:252847 partition_id, session_storage_namespace);
[email protected]d1198fd2012-08-13 22:50:192848
alexmos646fec02015-07-25 00:11:492849 // If the new frame has a name, make sure any SiteInstances that can find
2850 // this named frame have proxies for it. Must be called after
2851 // SetSessionStorageNamespace, since this calls CreateRenderView, which uses
2852 // GetSessionStorageNamespace.
2853 if (!params.frame_name.empty())
danakje6c01622018-10-05 19:39:262854 new_contents_impl->GetRenderManager()->CreateProxiesForNewNamedFrame();
alexmos646fec02015-07-25 00:11:492855
[email protected]c4538072013-03-18 02:17:552856 // Save the window for later if we're not suppressing the opener (since it
[email protected]d70bea92013-04-05 04:23:342857 // will be shown immediately).
2858 if (!params.opener_suppressed) {
2859 if (!is_guest) {
danakje6c01622018-10-05 19:39:262860 WebContentsView* new_view = new_contents_impl->view_.get();
[email protected]bafe6cd2012-05-23 23:09:502861
[email protected]d70bea92013-04-05 04:23:342862 // TODO(brettw): It seems bogus that we have to call this function on the
2863 // newly created object and give it one of its own member variables.
danakj22b4e8b2019-08-01 23:14:392864 RenderWidgetHostView* widget_view = new_view->CreateViewForWidget(
danakje6c01622018-10-05 19:39:262865 new_contents_impl->GetRenderViewHost()->GetWidget(), false);
danakj22b4e8b2019-08-01 23:14:392866 if (!renderer_started_hidden) {
2867 // RenderWidgets for frames always initialize as hidden. If the renderer
2868 // created this window as visible, then we show it here.
2869 widget_view->Show();
2870 }
[email protected]d70bea92013-04-05 04:23:342871 }
[email protected]bafe6cd2012-05-23 23:09:502872 // Save the created window associated with the route so we can show it
2873 // later.
Albert J. Wongcb004632018-07-10 22:58:252874 //
2875 // TODO(ajwong): This should be keyed off the RenderFrame routing id or the
2876 // FrameTreeNode id instead of the routing id of the Widget for the main
2877 // frame. https://crbug.com/545684
nick5ae4d2d2017-01-06 01:18:352878 DCHECK_NE(MSG_ROUTING_NONE, main_frame_widget_route_id);
danakje6c01622018-10-05 19:39:262879 GlobalRoutingID id(render_process_id, main_frame_widget_route_id);
2880 pending_contents_[id] = std::move(owning_contents_impl);
2881 AddDestructionObserver(new_contents_impl);
[email protected]bafe6cd2012-05-23 23:09:502882 }
2883
2884 if (delegate_) {
Bryan McQuaded2b93c02017-07-14 22:26:312885 delegate_->WebContentsCreated(this, render_process_id,
2886 opener->GetRoutingID(), params.frame_name,
danakje6c01622018-10-05 19:39:262887 params.target_url, new_contents_impl);
[email protected]bafe6cd2012-05-23 23:09:502888 }
2889
Charlie Harrisonf60dd1c2018-08-15 21:15:412890 for (auto& observer : observers_) {
danakje6c01622018-10-05 19:39:262891 observer.DidOpenRequestedURL(new_contents_impl, opener, params.target_url,
Leon Han963dc182018-11-06 05:41:482892 params.referrer.To<Referrer>(),
2893 params.disposition, ui::PAGE_TRANSITION_LINK,
Charlie Harrisonf60dd1c2018-08-15 21:15:412894 false, // started_from_context_menu
2895 true); // renderer_initiated
pnolandaae574e2017-03-06 21:04:212896 }
2897
Avi Drissmanba3b1b342017-08-29 18:16:132898 // Any new WebContents opened while this WebContents is in fullscreen can be
2899 // used to confuse the user, so drop fullscreen.
Avi Drissmand18c51972018-08-15 18:32:092900 ForSecurityDropFullscreen();
Avi Drissmanba3b1b342017-08-29 18:16:132901
[email protected]bafe6cd2012-05-23 23:09:502902 if (params.opener_suppressed) {
2903 // When the opener is suppressed, the original renderer cannot access the
2904 // new window. As a result, we need to show and navigate the window here.
[email protected]eda238a12012-09-07 23:44:002905 bool was_blocked = false;
erikchenbee5c9622018-04-27 19:30:252906
[email protected]eda238a12012-09-07 23:44:002907 if (delegate_) {
danakje6c01622018-10-05 19:39:262908 base::WeakPtr<WebContentsImpl> weak_new_contents =
2909 new_contents_impl->weak_factory_.GetWeakPtr();
nick0d558482017-02-13 22:27:222910
danakje6c01622018-10-05 19:39:262911 gfx::Rect initial_rect; // Report an empty initial rect.
2912 delegate_->AddNewContents(this, std::move(owning_contents_impl),
erikchenbee5c9622018-04-27 19:30:252913 params.disposition, initial_rect,
Sergey Kuznetsov32257a22019-02-11 20:26:502914 has_user_gesture, &was_blocked);
danakje6c01622018-10-05 19:39:262915 // The delegate may delete |new_contents_impl| during AddNewContents().
nick0d558482017-02-13 22:27:222916 if (!weak_new_contents)
danakje6c01622018-10-05 19:39:262917 return;
[email protected]eda238a12012-09-07 23:44:002918 }
erikchenbee5c9622018-04-27 19:30:252919
[email protected]eda238a12012-09-07 23:44:002920 if (!was_blocked) {
Nasko Oskov83a8cf92018-10-19 14:58:562921 std::unique_ptr<NavigationController::LoadURLParams> load_params =
2922 std::make_unique<NavigationController::LoadURLParams>(
2923 params.target_url);
Nasko Oskov93e7c55c2018-12-19 01:59:292924 load_params->initiator_origin = opener->GetLastCommittedOrigin();
Leon Han963dc182018-11-06 05:41:482925 load_params->referrer = params.referrer.To<Referrer>();
Nasko Oskov83a8cf92018-10-19 14:58:562926 load_params->transition_type = ui::PAGE_TRANSITION_LINK;
2927 load_params->is_renderer_initiated = true;
Sergey Kuznetsov32257a22019-02-11 20:26:502928 load_params->has_user_gesture = has_user_gesture;
mariakhomenkoa4971c12015-07-21 19:04:372929
2930 if (delegate_ && !is_guest &&
2931 !delegate_->ShouldResumeRequestsForCreatedWindow()) {
Nasko Oskov83a8cf92018-10-19 14:58:562932 // We are in asynchronous add new contents path, delay navigation.
2933 DCHECK(!new_contents_impl->delayed_open_url_params_);
2934 new_contents_impl->delayed_load_url_params_ = std::move(load_params);
mariakhomenkoa4971c12015-07-21 19:04:372935 } else {
Nasko Oskov83a8cf92018-10-19 14:58:562936 new_contents_impl->controller_.LoadURLWithParams(*load_params.get());
Nasko Oskov28ebd5b2018-12-07 22:29:332937 if (!is_guest)
2938 new_contents_impl->Focus();
mariakhomenkoa4971c12015-07-21 19:04:372939 }
[email protected]eda238a12012-09-07 23:44:002940 }
[email protected]bafe6cd2012-05-23 23:09:502941 }
2942}
2943
avib7348942015-12-25 20:57:102944void WebContentsImpl::CreateNewWidget(int32_t render_process_id,
Albert J. Wongf6e13ed2018-09-18 15:25:472945 int32_t widget_route_id,
danakj829cdd142018-09-14 21:13:272946 mojom::WidgetPtr widget) {
Albert J. Wongf6e13ed2018-09-18 15:25:472947 CreateNewWidget(render_process_id, widget_route_id, /*is_fullscreen=*/false,
danakj829cdd142018-09-14 21:13:272948 std::move(widget));
[email protected]bafe6cd2012-05-23 23:09:502949}
2950
avib7348942015-12-25 20:57:102951void WebContentsImpl::CreateNewFullscreenWidget(int32_t render_process_id,
Albert J. Wongf6e13ed2018-09-18 15:25:472952 int32_t widget_route_id,
Dave Tapuska1bdf1832017-07-07 18:07:192953 mojom::WidgetPtr widget) {
Albert J. Wongf6e13ed2018-09-18 15:25:472954 CreateNewWidget(render_process_id, widget_route_id, /*is_fullscreen=*/true,
danakj829cdd142018-09-14 21:13:272955 std::move(widget));
[email protected]bafe6cd2012-05-23 23:09:502956}
2957
avib7348942015-12-25 20:57:102958void WebContentsImpl::CreateNewWidget(int32_t render_process_id,
2959 int32_t route_id,
[email protected]bafe6cd2012-05-23 23:09:502960 bool is_fullscreen,
danakj829cdd142018-09-14 21:13:272961 mojom::WidgetPtr widget) {
kenrba26d15092016-02-10 22:00:252962 RenderProcessHost* process = RenderProcessHost::FromID(render_process_id);
kenrbb46f86a2015-12-07 07:53:232963 // A message to create a new widget can only come from an active process for
[email protected]a8504022013-12-04 20:23:512964 // this WebContentsImpl instance. If any other process sends the request,
2965 // it is invalid and the process must be terminated.
dcheng57e39e22016-01-21 00:25:382966 if (!HasMatchingProcess(&frame_tree_, render_process_id)) {
Lukasz Anforowicz6f746282018-01-04 23:24:512967 ReceivedBadMessage(process, bad_message::WCI_NEW_WIDGET_PROCESS_MISMATCH);
[email protected]a8504022013-12-04 20:23:512968 return;
2969 }
2970
Dave Tapuska485aca92017-08-08 00:47:582971 RenderWidgetHostImpl* widget_host = new RenderWidgetHostImpl(
2972 this, process, route_id, std::move(widget), IsHidden());
[email protected]b24b68a2012-09-24 21:57:262973
[email protected]cfd80b02014-05-01 17:46:482974 RenderWidgetHostViewBase* widget_view =
2975 static_cast<RenderWidgetHostViewBase*>(
danakj0b246942018-09-17 21:30:272976 view_->CreateViewForChildWidget(widget_host));
[email protected]83918ec2013-01-10 15:37:192977 if (!widget_view)
2978 return;
danakj0b246942018-09-17 21:30:272979 // Fullscreen child widgets are frames, other child widgets are popups, and
2980 // popups should not get activated.
2981 if (!is_fullscreen)
2982 widget_view->SetWidgetType(WidgetType::kPopup);
[email protected]bafe6cd2012-05-23 23:09:502983 // Save the created widget associated with the route so we can show it later.
Lukasz Anforowicz09060bdf72018-08-23 15:53:172984 pending_widget_views_[GlobalRoutingID(render_process_id, route_id)] =
alexmosc2a8cec2016-05-23 22:19:532985 widget_view;
[email protected]bafe6cd2012-05-23 23:09:502986}
2987
alexmosc2a8cec2016-05-23 22:19:532988void WebContentsImpl::ShowCreatedWindow(int process_id,
nick5ae4d2d2017-01-06 01:18:352989 int main_frame_widget_route_id,
[email protected]bafe6cd2012-05-23 23:09:502990 WindowOpenDisposition disposition,
bokan107a47f2015-02-03 23:23:392991 const gfx::Rect& initial_rect,
[email protected]bafe6cd2012-05-23 23:09:502992 bool user_gesture) {
danakje6c01622018-10-05 19:39:262993 // This method is the renderer requesting an existing top level window to
2994 // show a new top level window that the renderer created. Each top level
2995 // window is associated with a WebContents. In this case it was created
2996 // earlier but showing it was deferred until the renderer requested for it
2997 // to be shown. We find that previously created WebContents here.
2998 // TODO(danakj): Why do we defer this show step until the renderer asks for it
2999 // when it will always do so. What needs to happen in the renderer before we
3000 // reach here?
3001 std::unique_ptr<WebContentsImpl> owned_created =
nick5ae4d2d2017-01-06 01:18:353002 GetCreatedWindow(process_id, main_frame_widget_route_id);
danakje6c01622018-10-05 19:39:263003 WebContentsImpl* created = owned_created.get();
3004 // The browser may have rejected the request to make a new window, or the
3005 // renderer could be sending an invalid route id. Ignore the request then.
3006 if (!created)
3007 return;
mariakhomenko44bdc4732015-04-29 01:55:383008
danakje6c01622018-10-05 19:39:263009 // This uses the delegate for the WebContents where the window was created
3010 // from, to control how to show the newly created window.
3011 WebContentsDelegate* delegate = GetDelegate();
nick5ae4d2d2017-01-06 01:18:353012
danakje6c01622018-10-05 19:39:263013 // The delegate can be null in tests, so we must check for it :(.
3014 if (delegate) {
3015 // Mark the web contents as pending resume, then immediately do
3016 // the resume if the delegate wants it.
3017 created->is_resume_pending_ = true;
3018 if (delegate->ShouldResumeRequestsForCreatedWindow())
3019 created->ResumeLoadingCreatedWebContents();
3020
3021 base::WeakPtr<WebContentsImpl> weak_created =
3022 created->weak_factory_.GetWeakPtr();
3023 delegate->AddNewContents(this, std::move(owned_created), disposition,
3024 initial_rect, user_gesture, nullptr);
3025 // The delegate may delete |created| during AddNewContents().
3026 if (!weak_created)
3027 return;
[email protected]eda238a12012-09-07 23:44:003028 }
danakje6c01622018-10-05 19:39:263029
3030 RenderWidgetHostImpl* rwh = created->GetMainFrame()->GetRenderWidgetHost();
3031 DCHECK_EQ(main_frame_widget_route_id, rwh->GetRoutingID());
3032 rwh->Send(new WidgetMsg_SetBounds_ACK(rwh->GetRoutingID()));
[email protected]bafe6cd2012-05-23 23:09:503033}
3034
alexmosc2a8cec2016-05-23 22:19:533035void WebContentsImpl::ShowCreatedWidget(int process_id,
Albert J. Wongf6e13ed2018-09-18 15:25:473036 int widget_route_id,
bokan107a47f2015-02-03 23:23:393037 const gfx::Rect& initial_rect) {
Albert J. Wongf6e13ed2018-09-18 15:25:473038 ShowCreatedWidget(process_id, widget_route_id, false, initial_rect);
[email protected]bafe6cd2012-05-23 23:09:503039}
3040
alexmosc2a8cec2016-05-23 22:19:533041void WebContentsImpl::ShowCreatedFullscreenWidget(int process_id,
Albert J. Wongf6e13ed2018-09-18 15:25:473042 int widget_route_id) {
3043 ShowCreatedWidget(process_id, widget_route_id, true, gfx::Rect());
[email protected]bafe6cd2012-05-23 23:09:503044}
3045
alexmosc2a8cec2016-05-23 22:19:533046void WebContentsImpl::ShowCreatedWidget(int process_id,
3047 int route_id,
[email protected]bafe6cd2012-05-23 23:09:503048 bool is_fullscreen,
bokan107a47f2015-02-03 23:23:393049 const gfx::Rect& initial_rect) {
[email protected]cfd80b02014-05-01 17:46:483050 RenderWidgetHostViewBase* widget_host_view =
alexmosc2a8cec2016-05-23 22:19:533051 static_cast<RenderWidgetHostViewBase*>(
3052 GetCreatedWidget(process_id, route_id));
[email protected]bafe6cd2012-05-23 23:09:503053 if (!widget_host_view)
3054 return;
[email protected]cfd80b02014-05-01 17:46:483055
Ivan Kotenkov2c0d2bb32017-11-01 15:41:283056 RenderWidgetHostView* view = nullptr;
lfgc740d4b22016-04-15 16:45:333057 if (GetOuterWebContents()) {
3058 view = GetOuterWebContents()->GetRenderWidgetHostView();
[email protected]cfd80b02014-05-01 17:46:483059 } else {
3060 view = GetRenderWidgetHostView();
3061 }
3062
danakj0b246942018-09-17 21:30:273063 // Fullscreen child widgets are frames, other child widgets are popups.
[email protected]4aebbca2013-09-17 22:26:493064 if (is_fullscreen) {
[email protected]d7f80ba2013-10-12 07:42:313065 DCHECK_EQ(MSG_ROUTING_NONE, fullscreen_widget_routing_id_);
[email protected]0b431992014-06-24 00:08:033066 view_->StoreFocus();
alexmosc9e76ec2016-05-16 22:59:373067 fullscreen_widget_process_id_ =
3068 widget_host_view->GetRenderWidgetHost()->GetProcess()->GetID();
[email protected]d7f80ba2013-10-12 07:42:313069 fullscreen_widget_routing_id_ = route_id;
[email protected]4aebbca2013-09-17 22:26:493070 if (delegate_ && delegate_->EmbedsFullscreenWidget()) {
3071 widget_host_view->InitAsChild(GetRenderWidgetHostView()->GetNativeView());
Dave Tapuska3ed44192018-05-01 18:53:303072 delegate_->EnterFullscreenModeForTab(this, GURL(),
3073 blink::WebFullscreenOptions());
[email protected]4aebbca2013-09-17 22:26:493074 } else {
[email protected]cfd80b02014-05-01 17:46:483075 widget_host_view->InitAsFullscreen(view);
[email protected]4aebbca2013-09-17 22:26:493076 }
ericwilligersde386342016-10-19 02:35:093077 for (auto& observer : observers_)
3078 observer.DidShowFullscreenWidget();
[email protected]4aebbca2013-09-17 22:26:493079 if (!widget_host_view->HasFocus())
3080 widget_host_view->Focus();
3081 } else {
bokan107a47f2015-02-03 23:23:393082 widget_host_view->InitAsPopup(view, initial_rect);
[email protected]4aebbca2013-09-17 22:26:493083 }
[email protected]89054502012-06-03 10:29:243084
yiyixcb1fbc4f2018-03-16 19:54:083085 RenderWidgetHostImpl* render_widget_host_impl = widget_host_view->host();
[email protected]89054502012-06-03 10:29:243086 render_widget_host_impl->Init();
[email protected]d7f80ba2013-10-12 07:42:313087 // Only allow privileged mouse lock for fullscreen render widget, which is
3088 // used to implement Pepper Flash fullscreen.
3089 render_widget_host_impl->set_allow_privileged_mouse_lock(is_fullscreen);
[email protected]bafe6cd2012-05-23 23:09:503090}
3091
danakje6c01622018-10-05 19:39:263092std::unique_ptr<WebContentsImpl> WebContentsImpl::GetCreatedWindow(
nick5ae4d2d2017-01-06 01:18:353093 int process_id,
3094 int main_frame_widget_route_id) {
Lukasz Anforowicz09060bdf72018-08-23 15:53:173095 auto key = GlobalRoutingID(process_id, main_frame_widget_route_id);
nick5ae4d2d2017-01-06 01:18:353096 auto iter = pending_contents_.find(key);
[email protected]bafe6cd2012-05-23 23:09:503097
3098 // Certain systems can block the creation of new windows. If we didn't succeed
3099 // in creating one, just return NULL.
alexmosc2a8cec2016-05-23 22:19:533100 if (iter == pending_contents_.end())
3101 return nullptr;
[email protected]bafe6cd2012-05-23 23:09:503102
danakje6c01622018-10-05 19:39:263103 std::unique_ptr<WebContentsImpl> new_contents = std::move(iter->second);
nick5ae4d2d2017-01-06 01:18:353104 pending_contents_.erase(key);
danakje6c01622018-10-05 19:39:263105 RemoveDestructionObserver(new_contents.get());
[email protected]bafe6cd2012-05-23 23:09:503106
[email protected]d70bea92013-04-05 04:23:343107 // Don't initialize the guest WebContents immediately.
danakje6c01622018-10-05 19:39:263108 if (BrowserPluginGuest::IsGuest(new_contents.get()))
[email protected]d70bea92013-04-05 04:23:343109 return new_contents;
3110
Lukasz Anforowicz5510ed652018-06-06 16:16:193111 if (!new_contents->GetMainFrame()->GetProcess()->IsInitializedAndNotDead() ||
nick5ae4d2d2017-01-06 01:18:353112 !new_contents->GetMainFrame()->GetView()) {
alexmosc2a8cec2016-05-23 22:19:533113 return nullptr;
nick5ae4d2d2017-01-06 01:18:353114 }
[email protected]bafe6cd2012-05-23 23:09:503115
[email protected]bafe6cd2012-05-23 23:09:503116 return new_contents;
3117}
3118
alexmosc2a8cec2016-05-23 22:19:533119RenderWidgetHostView* WebContentsImpl::GetCreatedWidget(int process_id,
3120 int route_id) {
Lukasz Anforowicz09060bdf72018-08-23 15:53:173121 auto iter = pending_widget_views_.find(GlobalRoutingID(process_id, route_id));
[email protected]bafe6cd2012-05-23 23:09:503122 if (iter == pending_widget_views_.end()) {
3123 DCHECK(false);
alexmosc2a8cec2016-05-23 22:19:533124 return nullptr;
[email protected]bafe6cd2012-05-23 23:09:503125 }
3126
3127 RenderWidgetHostView* widget_host_view = iter->second;
Lukasz Anforowicz09060bdf72018-08-23 15:53:173128 pending_widget_views_.erase(GlobalRoutingID(process_id, route_id));
[email protected]bafe6cd2012-05-23 23:09:503129
3130 RenderWidgetHost* widget_host = widget_host_view->GetRenderWidgetHost();
Lukasz Anforowicz5510ed652018-06-06 16:16:193131 if (!widget_host->GetProcess()->IsInitializedAndNotDead()) {
[email protected]bafe6cd2012-05-23 23:09:503132 // The view has gone away or the renderer crashed. Nothing to do.
alexmosc2a8cec2016-05-23 22:19:533133 return nullptr;
[email protected]bafe6cd2012-05-23 23:09:503134 }
3135
3136 return widget_host_view;
3137}
3138
[email protected]f13b4202012-06-12 23:53:233139void WebContentsImpl::RequestMediaAccessPermission(
[email protected]33662e52013-01-07 21:31:093140 const MediaStreamRequest& request,
Mark Pilgrim57499082018-06-12 12:38:303141 MediaResponseCallback callback) {
[email protected]d19b84b2014-03-14 11:52:373142 if (delegate_) {
Mark Pilgrim57499082018-06-12 12:38:303143 delegate_->RequestMediaAccessPermission(this, request, std::move(callback));
[email protected]d19b84b2014-03-14 11:52:373144 } else {
Antonio Gomes0d42960a2019-06-05 12:35:513145 std::move(callback).Run(
3146 blink::MediaStreamDevices(),
3147 blink::mojom::MediaStreamRequestResult::FAILED_DUE_TO_SHUTDOWN,
3148 std::unique_ptr<MediaStreamUI>());
[email protected]d19b84b2014-03-14 11:52:373149 }
[email protected]f13b4202012-06-12 23:53:233150}
3151
Chandan Padhia4b8bcb72017-09-12 16:41:043152bool WebContentsImpl::CheckMediaAccessPermission(
Raymes Khouryad7c24a12018-03-05 23:22:583153 RenderFrameHost* render_frame_host,
Chandan Padhia4b8bcb72017-09-12 16:41:043154 const url::Origin& security_origin,
Antonio Gomesc8b734b2019-06-05 18:22:163155 blink::mojom::MediaStreamType type) {
3156 DCHECK(type == blink::mojom::MediaStreamType::DEVICE_AUDIO_CAPTURE ||
3157 type == blink::mojom::MediaStreamType::DEVICE_VIDEO_CAPTURE);
Chandan Padhia4b8bcb72017-09-12 16:41:043158 return delegate_ && delegate_->CheckMediaAccessPermission(
Raymes Khouryad7c24a12018-03-05 23:22:583159 render_frame_host, security_origin.GetURL(), type);
grunell657d4d82014-09-18 00:09:433160}
3161
Guido Urdaneta73fa6632019-01-14 18:46:263162std::string WebContentsImpl::GetDefaultMediaDeviceID(
Antonio Gomesc8b734b2019-06-05 18:22:163163 blink::mojom::MediaStreamType type) {
guidou8a440b842017-01-30 13:58:433164 if (!delegate_)
3165 return std::string();
3166 return delegate_->GetDefaultMediaDeviceID(this, type);
3167}
3168
[email protected]cc9200432013-07-23 23:02:403169SessionStorageNamespace* WebContentsImpl::GetSessionStorageNamespace(
3170 SiteInstance* instance) {
3171 return controller_.GetSessionStorageNamespace(instance);
3172}
3173
[email protected]6de7fc482014-06-06 10:46:443174SessionStorageNamespaceMap WebContentsImpl::GetSessionStorageNamespaceMap() {
3175 return controller_.GetSessionStorageNamespaceMap();
3176}
3177
[email protected]9b159a52013-10-03 17:24:553178FrameTree* WebContentsImpl::GetFrameTree() {
3179 return &frame_tree_;
3180}
3181
aelias5252baa2016-04-10 01:18:023182bool WebContentsImpl::IsOverridingUserAgent() {
3183 return GetController().GetVisibleEntry() &&
3184 GetController().GetVisibleEntry()->GetIsOverridingUserAgent();
3185}
3186
avi85ee8362016-10-08 02:09:083187bool WebContentsImpl::IsJavaScriptDialogShowing() const {
3188 return is_showing_javascript_dialog_;
3189}
3190
avic031d392017-03-03 03:09:423191bool WebContentsImpl::ShouldIgnoreUnresponsiveRenderer() {
3192 // Ignore unresponsive renderers if the debugger is attached to them since the
3193 // unresponsiveness might be a result of the renderer sitting on a breakpoint.
3194 //
David Bienvenu525f8a02019-02-15 02:02:423195#ifdef OS_WIN
3196 // Check if a windows debugger is attached to the renderer process.
3197 base::ProcessHandle process_handle =
3198 GetMainFrame()->GetProcess()->GetProcess().Handle();
3199 BOOL debugger_present = FALSE;
3200 if (CheckRemoteDebuggerPresent(process_handle, &debugger_present) &&
3201 debugger_present)
3202 return true;
3203#endif // OS_WIN
3204
avic031d392017-03-03 03:09:423205 // TODO(pfeldman): Fix this to only return true if the renderer is *actually*
3206 // sitting on a breakpoint. https://crbug.com/684202
3207 return DevToolsAgentHost::IsDebuggerAttached(this);
3208}
3209
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:413210ui::AXMode WebContentsImpl::GetAccessibilityMode() {
[email protected]95640212014-07-26 18:14:303211 return accessibility_mode_;
3212}
3213
Dominic Mazzonia7b0edb22017-08-09 16:32:513214void WebContentsImpl::AccessibilityEventReceived(
Dominic Mazzoniccbaa9b2018-06-06 07:44:233215 const AXEventNotificationDetails& details) {
ericwilligersde386342016-10-19 02:35:093216 for (auto& observer : observers_)
Dominic Mazzonia7b0edb22017-08-09 16:32:513217 observer.AccessibilityEventReceived(details);
[email protected]31a71eaf2014-03-13 01:47:363218}
3219
dmazzoni2400c462016-08-23 15:07:133220void WebContentsImpl::AccessibilityLocationChangesReceived(
3221 const std::vector<AXLocationChangeNotificationDetails>& details) {
ericwilligersde386342016-10-19 02:35:093222 for (auto& observer : observers_)
Dominic Mazzonia7b0edb22017-08-09 16:32:513223 observer.AccessibilityLocationChangesReceived(details);
dmazzoni2400c462016-08-23 15:07:133224}
3225
James Wallace-Leeeafc94cb92018-07-23 21:35:093226base::string16 WebContentsImpl::DumpAccessibilityTree(bool internal) {
3227 auto* ax_mgr = GetOrCreateRootBrowserAccessibilityManager();
3228 DCHECK(ax_mgr);
3229 return AccessibilityTreeFormatter::DumpAccessibilityTreeFromManager(ax_mgr,
3230 internal);
3231}
3232
dmazzonia656928e2014-09-15 20:28:543233RenderFrameHost* WebContentsImpl::GetGuestByInstanceID(
fsamuel833ee7c2015-02-13 23:40:403234 RenderFrameHost* render_frame_host,
dmazzonia656928e2014-09-15 20:28:543235 int browser_plugin_instance_id) {
3236 BrowserPluginGuestManager* guest_manager =
3237 GetBrowserContext()->GetGuestManager();
fsamuel9cbc00a2015-04-17 20:30:203238 if (!guest_manager)
3239 return nullptr;
3240
dmazzonia656928e2014-09-15 20:28:543241 WebContents* guest = guest_manager->GetGuestByInstanceID(
fsamuel2e9413d2015-02-25 01:25:443242 render_frame_host->GetProcess()->GetID(), browser_plugin_instance_id);
dmazzonia656928e2014-09-15 20:28:543243 if (!guest)
fsamuel9cbc00a2015-04-17 20:30:203244 return nullptr;
3245
dmazzonia656928e2014-09-15 20:28:543246 return guest->GetMainFrame();
3247}
3248
Ke He7319dbe2017-11-09 05:54:443249device::mojom::GeolocationContext* WebContentsImpl::GetGeolocationContext() {
3250 if (geolocation_context_)
3251 return geolocation_context_.get();
3252
3253 auto request = mojo::MakeRequest(&geolocation_context_);
Ken Rockotfd03e682019-06-20 21:13:543254 service_manager::Connector* connector = GetSystemConnector();
3255 if (connector)
3256 connector->BindInterface(device::mojom::kServiceName, std::move(request));
Conley Owens6894c4f2017-07-10 19:29:133257 return geolocation_context_.get();
blundellc57b93f2014-10-29 13:19:573258}
3259
ke.he98b761e2017-05-09 05:59:173260device::mojom::WakeLockContext* WebContentsImpl::GetWakeLockContext() {
blundelld8cd72b2017-03-28 07:18:383261 if (!wake_lock_context_host_)
3262 wake_lock_context_host_.reset(new WakeLockContextHost(this));
blundelle75a8f92017-03-27 08:11:173263 return wake_lock_context_host_->GetWakeLockContext();
alogvinovf50445a2015-10-30 13:00:123264}
3265
blundellf5316fc2017-05-15 11:49:033266#if defined(OS_ANDROID)
Han Leonf6a2e5f2017-05-23 13:40:473267void WebContentsImpl::GetNFC(device::mojom::NFCRequest request) {
blundellf5316fc2017-05-15 11:49:033268 if (!nfc_host_)
3269 nfc_host_.reset(new NFCHost(this));
3270 nfc_host_->GetNFC(std::move(request));
3271}
3272#endif
3273
Evan Stade5e750ad02017-08-07 21:59:273274void WebContentsImpl::SetNotWaitingForResponse() {
3275 if (waiting_for_response_ == false)
3276 return;
3277
3278 waiting_for_response_ = false;
3279 if (delegate_)
3280 delegate_->LoadingStateChanged(this, is_load_to_different_document_);
Chris Hamiltonf82f2a702018-02-15 19:37:293281 for (auto& observer : observers_)
3282 observer.DidReceiveResponse();
Evan Stade5e750ad02017-08-07 21:59:273283}
3284
lfgbb9c28a2016-03-01 03:19:493285void WebContentsImpl::SendScreenRects() {
kenrbc52e1b4e2016-03-20 01:03:373286 for (FrameTreeNode* node : frame_tree_.Nodes()) {
kenrb0e8dc202016-05-30 19:51:403287 if (node->current_frame_host()->is_local_root())
kenrbc52e1b4e2016-03-20 01:03:373288 node->current_frame_host()->GetRenderWidgetHost()->SendScreenRects();
3289 }
lfgbb9c28a2016-03-01 03:19:493290
danakj4df8a9d02019-02-05 20:36:593291 // Interstitials have their own |frame_tree_|.
3292 if (interstitial_page_) {
3293 FrameTree* interstitial_frame_tree = interstitial_page_->GetFrameTree();
3294 for (FrameTreeNode* node : interstitial_frame_tree->Nodes()) {
3295 if (node->current_frame_host()->is_local_root())
3296 node->current_frame_host()->GetRenderWidgetHost()->SendScreenRects();
3297 }
lfgdb5c4ed2016-03-04 23:09:073298 }
3299
avallee9f43b012016-11-26 07:02:103300 if (browser_plugin_embedder_ && !is_being_destroyed_)
[email protected]a7568e62013-06-14 07:50:443301 browser_plugin_embedder_->DidSendScreenRects();
[email protected]32deec62013-05-15 23:55:043302}
3303
ekaramadadd882292016-06-08 15:22:563304TextInputManager* WebContentsImpl::GetTextInputManager() {
3305 if (GetOuterWebContents())
3306 return GetOuterWebContents()->GetTextInputManager();
3307
Ehsan Karamadc179dc02018-12-15 03:09:293308 if (!text_input_manager_ && !browser_plugin_guest_) {
Darren Shenca53d5f2018-05-15 04:56:013309 text_input_manager_.reset(new TextInputManager(
3310 GetBrowserContext() &&
3311 !GetBrowserContext()->IsOffTheRecord()) /* should_do_learning */);
3312 }
ekaramadadd882292016-06-08 15:22:563313
3314 return text_input_manager_.get();
3315}
3316
paulmeyer7f6f1d4f2016-11-15 00:00:273317bool WebContentsImpl::OnUpdateDragCursor() {
Lei Zhang6410ec1a2019-01-31 21:38:583318 return browser_plugin_embedder_ &&
3319 browser_plugin_embedder_->OnUpdateDragCursor();
paulmeyer7f6f1d4f2016-11-15 00:00:273320}
3321
kenrb11f213a2017-03-24 18:12:063322bool WebContentsImpl::IsWidgetForMainFrame(
3323 RenderWidgetHostImpl* render_widget_host) {
3324 return render_widget_host == GetMainFrame()->GetRenderWidgetHost();
3325}
3326
[email protected]95640212014-07-26 18:14:303327BrowserAccessibilityManager*
3328 WebContentsImpl::GetRootBrowserAccessibilityManager() {
Dominic Mazzoniefb5f9e32017-12-21 01:08:223329 RenderFrameHostImpl* rfh = static_cast<RenderFrameHostImpl*>(
3330 ShowingInterstitialPage() ? GetInterstitialPage()->GetMainFrame()
3331 : GetMainFrame());
creisc014b402015-04-23 16:41:453332 return rfh ? rfh->browser_accessibility_manager() : nullptr;
[email protected]95640212014-07-26 18:14:303333}
3334
3335BrowserAccessibilityManager*
3336 WebContentsImpl::GetOrCreateRootBrowserAccessibilityManager() {
Dominic Mazzoniefb5f9e32017-12-21 01:08:223337 RenderFrameHostImpl* rfh = static_cast<RenderFrameHostImpl*>(
3338 ShowingInterstitialPage() ? GetInterstitialPage()->GetMainFrame()
3339 : GetMainFrame());
creisc014b402015-04-23 16:41:453340 return rfh ? rfh->GetOrCreateBrowserAccessibilityManager() : nullptr;
[email protected]95640212014-07-26 18:14:303341}
3342
Dave Tapuskab336b922017-07-06 19:24:053343void WebContentsImpl::ExecuteEditCommand(
3344 const std::string& command,
3345 const base::Optional<base::string16>& value) {
Dave Tapuskae782bea2019-07-09 16:21:513346 auto* input_handler = GetFocusedFrameInputHandler();
3347 if (!input_handler)
Dave Tapuskab336b922017-07-06 19:24:053348 return;
3349
Dave Tapuskae782bea2019-07-09 16:21:513350 input_handler->ExecuteEditCommand(command, value);
Dave Tapuskab336b922017-07-06 19:24:053351}
3352
mohsen7ab1ec16ec2015-07-02 18:26:233353void WebContentsImpl::MoveRangeSelectionExtent(const gfx::Point& extent) {
Dave Tapuskae782bea2019-07-09 16:21:513354 auto* input_handler = GetFocusedFrameInputHandler();
3355 if (!input_handler)
mohsen7ab1ec16ec2015-07-02 18:26:233356 return;
3357
Dave Tapuskae782bea2019-07-09 16:21:513358 input_handler->MoveRangeSelectionExtent(extent);
mohsen7ab1ec16ec2015-07-02 18:26:233359}
3360
3361void WebContentsImpl::SelectRange(const gfx::Point& base,
3362 const gfx::Point& extent) {
Dave Tapuskae782bea2019-07-09 16:21:513363 auto* input_handler = GetFocusedFrameInputHandler();
3364 if (!input_handler)
mohsen7ab1ec16ec2015-07-02 18:26:233365 return;
3366
Dave Tapuskae782bea2019-07-09 16:21:513367 input_handler->SelectRange(base, extent);
mohsen7ab1ec16ec2015-07-02 18:26:233368}
3369
Dave Tapuskab336b922017-07-06 19:24:053370void WebContentsImpl::MoveCaret(const gfx::Point& extent) {
Dave Tapuskae782bea2019-07-09 16:21:513371 auto* input_handler = GetFocusedFrameInputHandler();
3372 if (!input_handler)
Dave Tapuskab336b922017-07-06 19:24:053373 return;
3374
Dave Tapuskae782bea2019-07-09 16:21:513375 input_handler->MoveCaret(extent);
Dave Tapuskab336b922017-07-06 19:24:053376}
3377
Shimi Zhang37deeb22017-09-28 00:59:013378void WebContentsImpl::AdjustSelectionByCharacterOffset(
3379 int start_adjust,
3380 int end_adjust,
3381 bool show_selection_menu) {
Dave Tapuskae782bea2019-07-09 16:21:513382 auto* input_handler = GetFocusedFrameInputHandler();
3383 if (!input_handler)
aurimasab0319022015-07-10 21:57:383384 return;
3385
Shimi Zhang37deeb22017-09-28 00:59:013386 using blink::mojom::SelectionMenuBehavior;
Dave Tapuskae782bea2019-07-09 16:21:513387 input_handler->AdjustSelectionByCharacterOffset(
Shimi Zhang37deeb22017-09-28 00:59:013388 start_adjust, end_adjust,
3389 show_selection_menu ? SelectionMenuBehavior::kShow
3390 : SelectionMenuBehavior::kHide);
aurimasab0319022015-07-10 21:57:383391}
3392
[email protected]b172aee2012-04-10 17:05:263393void WebContentsImpl::UpdatePreferredSize(const gfx::Size& pref_size) {
[email protected]222f5822014-02-05 23:40:493394 const gfx::Size old_size = GetPreferredSize();
[email protected]bcd2815602012-01-14 18:17:233395 preferred_size_ = pref_size;
[email protected]222f5822014-02-05 23:40:493396 OnPreferredSizeChanged(old_size);
[email protected]0548c5352011-09-07 00:33:333397}
3398
avic3aa8422015-11-09 20:57:223399void WebContentsImpl::ResizeDueToAutoResize(
3400 RenderWidgetHostImpl* render_widget_host,
Fady Samuel97947112018-05-05 16:24:543401 const gfx::Size& new_size) {
avic3aa8422015-11-09 20:57:223402 if (render_widget_host != GetRenderViewHost()->GetWidget())
3403 return;
3404
kenrbe4f91082017-06-02 20:32:083405 auto_resize_size_ = new_size;
3406
3407 // Out-of-process iframe visible viewport sizes usually come from the
3408 // top-level RenderWidgetHostView, but when auto-resize is enabled on the
3409 // top frame then that size is used instead.
3410 for (FrameTreeNode* node : frame_tree_.Nodes()) {
3411 if (node->current_frame_host()->is_local_root()) {
3412 RenderWidgetHostImpl* host =
3413 node->current_frame_host()->GetRenderWidgetHost();
3414 if (host != render_widget_host)
Fady Samuel0b911822018-04-25 13:22:163415 host->SynchronizeVisualProperties();
kenrbe4f91082017-06-02 20:32:083416 }
3417 }
3418
[email protected]61e2b3cc2012-03-02 16:13:343419 if (delegate_)
3420 delegate_->ResizeDueToAutoResize(this, new_size);
3421}
3422
kenrbe4f91082017-06-02 20:32:083423gfx::Size WebContentsImpl::GetAutoResizeSize() {
3424 return auto_resize_size_;
3425}
3426
3427void WebContentsImpl::ResetAutoResizeSize() {
3428 auto_resize_size_ = gfx::Size();
3429}
3430
Albert J. Wongeb28e7e2018-10-12 00:57:433431InputEventShim* WebContentsImpl::GetInputEventShim() const {
3432 // The only thing that intercepts text input and mouse lock is the
3433 // BrowserPluginGuest. Delegate to the BrowserPluginGuest logic for
3434 // whether or not it needs to shim these events.
3435 if (browser_plugin_guest_) {
3436 return browser_plugin_guest_->GetInputEventShim();
3437 }
3438 return nullptr;
3439}
3440
[email protected]b172aee2012-04-10 17:05:263441WebContents* WebContentsImpl::OpenURL(const OpenURLParams& params) {
Bo Liu300c6052018-06-12 04:46:073442 if (!delegate_) {
3443 // Embedder can delay setting a delegate on new WebContents with
3444 // WebContentsDelegate::ShouldResumeRequestsForCreatedWindow. In the mean
3445 // time, navigations, including the initial one, that goes through OpenURL
3446 // should be delayed until embedder is ready to resume loading.
3447 delayed_open_url_params_ = std::make_unique<OpenURLParams>(params);
Nasko Oskov83a8cf92018-10-19 14:58:563448
3449 // If there was a navigation deferred when creating the window through
3450 // CreateNewWindow, drop it in favor of this navigation.
3451 delayed_load_url_params_.reset();
3452
Ivan Kotenkov2c0d2bb32017-11-01 15:41:283453 return nullptr;
Bo Liu300c6052018-06-12 04:46:073454 }
[email protected]00c37fc2011-08-02 00:22:503455
[email protected]e5d549d2011-12-28 01:29:203456 WebContents* new_contents = delegate_->OpenURLFromTab(this, params);
pnoland488944652017-02-22 18:58:543457
3458 RenderFrameHost* source_render_frame_host = RenderFrameHost::FromID(
3459 params.source_render_process_id, params.source_render_frame_id);
3460
3461 if (source_render_frame_host && params.source_site_instance) {
3462 CHECK_EQ(source_render_frame_host->GetSiteInstance(),
3463 params.source_site_instance.get());
3464 }
3465
3466 if (new_contents && source_render_frame_host && new_contents != this) {
3467 for (auto& observer : observers_) {
Lukasz Anforowicz48c9f872018-08-01 00:58:413468 observer.DidOpenRequestedURL(
3469 new_contents, source_render_frame_host, params.url, params.referrer,
3470 params.disposition, params.transition,
3471 params.started_from_context_menu, params.is_renderer_initiated);
pnoland488944652017-02-22 18:58:543472 }
3473 }
3474
[email protected]e5d549d2011-12-28 01:29:203475 return new_contents;
[email protected]d5f942ba2008-09-26 19:30:343476}
3477
[email protected]a86c0e962013-12-17 17:10:393478void WebContentsImpl::RenderFrameForInterstitialPageCreated(
3479 RenderFrameHost* render_frame_host) {
ericwilligersde386342016-10-19 02:35:093480 for (auto& observer : observers_)
3481 observer.RenderFrameForInterstitialPageCreated(render_frame_host);
[email protected]ba45bfd2012-05-22 21:51:443482}
3483
[email protected]20ca0382013-02-28 19:50:073484void WebContentsImpl::AttachInterstitialPage(
3485 InterstitialPageImpl* interstitial_page) {
creisadca9862017-06-16 19:12:113486 DCHECK(!interstitial_page_ && interstitial_page);
3487 interstitial_page_ = interstitial_page;
[email protected]90fb08ed2013-09-24 17:43:293488
3489 // Cancel any visible dialogs so that they don't interfere with the
3490 // interstitial.
creis89a0f782015-05-27 16:13:173491 CancelActiveAndPendingDialogs();
[email protected]90fb08ed2013-09-24 17:43:293492
ericwilligersde386342016-10-19 02:35:093493 for (auto& observer : observers_)
3494 observer.DidAttachInterstitialPage();
clamy44e84ce2016-02-22 15:38:253495
3496 // Stop the throbber if needed while the interstitial page is shown.
3497 if (frame_tree_.IsLoading())
3498 LoadingStateChanged(true, true, nullptr);
lfg1453e412017-04-11 00:48:503499
3500 // Connect to outer WebContents if necessary.
3501 if (node_.OuterContentsFrameTreeNode()) {
3502 if (GetRenderManager()->GetProxyToOuterDelegate()) {
3503 DCHECK(
3504 static_cast<RenderWidgetHostViewBase*>(interstitial_page->GetView())
3505 ->IsRenderWidgetHostViewChildFrame());
3506 RenderWidgetHostViewChildFrame* view =
3507 static_cast<RenderWidgetHostViewChildFrame*>(
3508 interstitial_page->GetView());
3509 GetRenderManager()->SetRWHViewForInnerContents(view);
3510 }
3511 }
Bo Liu7c6779e92017-08-16 02:02:283512
Bo Liu168c8642017-08-28 18:26:023513#if defined(OS_ANDROID)
Bo Liu7c6779e92017-08-16 02:02:283514 // Update importance of the interstitial.
3515 static_cast<RenderFrameHostImpl*>(interstitial_page_->GetMainFrame())
3516 ->GetRenderWidgetHost()
3517 ->SetImportance(GetMainFrame()->GetRenderWidgetHost()->importance());
Bo Liu168c8642017-08-28 18:26:023518#endif
Dominic Mazzoniefb5f9e32017-12-21 01:08:223519
3520 if (accessibility_mode_.has_mode(ui::AXMode::kNativeAPIs)) {
3521 // Make sure that the main page's accessibility tree is hidden and the
3522 // interstitial gets focus.
3523 RenderFrameHostImpl* rfh =
3524 static_cast<RenderFrameHostImpl*>(GetMainFrame());
3525 if (rfh) {
3526 BrowserAccessibilityManager* accessibility_manager =
3527 rfh->browser_accessibility_manager();
3528 if (accessibility_manager)
3529 accessibility_manager->set_hidden_by_interstitial_page(true);
3530 }
3531 rfh = static_cast<RenderFrameHostImpl*>(
3532 GetInterstitialPage()->GetMainFrame());
3533 if (rfh) {
3534 BrowserAccessibilityManager* accessibility_manager =
3535 rfh->GetOrCreateBrowserAccessibilityManager();
3536 if (accessibility_manager)
3537 accessibility_manager->OnWindowFocused();
3538 }
3539 }
clamy44e84ce2016-02-22 15:38:253540}
3541
3542void WebContentsImpl::DidProceedOnInterstitial() {
3543 // The interstitial page should no longer be pausing the throbber.
creisadca9862017-06-16 19:12:113544 DCHECK(!(ShowingInterstitialPage() && interstitial_page_->pause_throbber()));
clamy44e84ce2016-02-22 15:38:253545
3546 // Restart the throbber now that the interstitial page no longer pauses it.
3547 if (ShowingInterstitialPage() && frame_tree_.IsLoading())
3548 LoadingStateChanged(true, true, nullptr);
[email protected]20ca0382013-02-28 19:50:073549}
3550
Shivani Sharma985474f2019-05-23 21:36:473551bool WebContentsImpl::HadInnerWebContents() {
3552 return had_inner_webcontents_;
3553}
3554
Lucas Furukawa Gadani7f1241d2017-07-12 01:45:043555void WebContentsImpl::DetachInterstitialPage(bool has_focus) {
Lucas Furukawa Gadani69b609192017-07-10 17:08:343556 bool interstitial_pausing_throbber =
3557 ShowingInterstitialPage() && interstitial_page_->pause_throbber();
3558 if (ShowingInterstitialPage())
3559 interstitial_page_ = nullptr;
Lucas Furukawa Gadani7f1241d2017-07-12 01:45:043560
Dominic Mazzoniefb5f9e32017-12-21 01:08:223561 // Make sure that the main page's accessibility tree is no longer
3562 // suppressed.
3563 RenderFrameHostImpl* rfh = GetMainFrame();
3564 if (rfh) {
3565 BrowserAccessibilityManager* accessibility_manager =
3566 rfh->browser_accessibility_manager();
3567 if (accessibility_manager)
3568 accessibility_manager->set_hidden_by_interstitial_page(false);
3569 }
3570
Lucas Furukawa Gadani7f1241d2017-07-12 01:45:043571 // If the focus was on the interstitial, let's keep it to the page.
3572 // (Note that in unit-tests the RVH may not have a view).
3573 if (has_focus && GetRenderViewHost()->GetWidget()->GetView())
3574 GetRenderViewHost()->GetWidget()->GetView()->Focus();
3575
Lucas Furukawa Gadani69b609192017-07-10 17:08:343576 for (auto& observer : observers_)
3577 observer.DidDetachInterstitialPage();
3578
3579 // Disconnect from outer WebContents if necessary. This must happen after the
3580 // interstitial page is cleared above, since the call to
3581 // SetRWHViewForInnerContents below may loop over all the
3582 // RenderWidgetHostViews in the tree (otherwise, including the now-deleted
3583 // view for the interstitial).
lfg1453e412017-04-11 00:48:503584 if (node_.OuterContentsFrameTreeNode()) {
3585 if (GetRenderManager()->GetProxyToOuterDelegate()) {
3586 DCHECK(static_cast<RenderWidgetHostViewBase*>(
3587 GetRenderManager()->current_frame_host()->GetView())
3588 ->IsRenderWidgetHostViewChildFrame());
3589 RenderWidgetHostViewChildFrame* view =
3590 static_cast<RenderWidgetHostViewChildFrame*>(
3591 GetRenderManager()->current_frame_host()->GetView());
3592 GetRenderManager()->SetRWHViewForInnerContents(view);
3593 }
3594 }
3595
clamy44e84ce2016-02-22 15:38:253596 // Restart the throbber if needed now that the interstitial page is going
3597 // away.
3598 if (interstitial_pausing_throbber && frame_tree_.IsLoading())
3599 LoadingStateChanged(true, true, nullptr);
[email protected]20ca0382013-02-28 19:50:073600}
3601
avi2b177592014-12-10 02:08:023602void WebContentsImpl::SetHistoryOffsetAndLength(int history_offset,
3603 int history_length) {
alexmos136fd6e62016-08-15 20:58:413604 SendPageMessage(new PageMsg_SetHistoryOffsetAndLength(
3605 MSG_ROUTING_NONE, history_offset, history_length));
avi2b177592014-12-10 02:08:023606}
3607
3608void WebContentsImpl::SetHistoryOffsetAndLengthForView(
3609 RenderViewHost* render_view_host,
3610 int history_offset,
3611 int history_length) {
alexmos136fd6e62016-08-15 20:58:413612 render_view_host->Send(new PageMsg_SetHistoryOffsetAndLength(
avi2b177592014-12-10 02:08:023613 render_view_host->GetRoutingID(), history_offset, history_length));
[email protected]796931a92011-08-10 01:32:143614}
3615
toyoshim24a4c7ab2016-04-05 09:24:143616void WebContentsImpl::ReloadFocusedFrame(bool bypass_cache) {
[email protected]1f3fc1d2014-04-03 14:50:173617 RenderFrameHost* focused_frame = GetFocusedFrame();
3618 if (!focused_frame)
3619 return;
3620
[email protected]959be4c2014-04-08 15:01:333621 focused_frame->Send(new FrameMsg_Reload(
toyoshim24a4c7ab2016-04-05 09:24:143622 focused_frame->GetRoutingID(), bypass_cache));
[email protected]959be4c2014-04-08 15:01:333623}
3624
megjablon3f5941072016-02-04 23:27:523625void WebContentsImpl::ReloadLoFiImages() {
3626 SendToAllFrames(new FrameMsg_ReloadLoFiImages(MSG_ROUTING_NONE));
Robert Ogden34e97962018-09-14 23:50:273627 for (auto& observer : observers_)
3628 observer.DidReloadLoFiImages();
megjablon3f5941072016-02-04 23:27:523629}
3630
Ryan Sturmc65dbe42018-05-31 20:37:043631std::vector<blink::mojom::PauseSubresourceLoadingHandlePtr>
3632WebContentsImpl::PauseSubresourceLoading() {
3633 std::vector<blink::mojom::PauseSubresourceLoadingHandlePtr> handles;
3634 for (RenderFrameHost* rfh : GetAllFrames()) {
3635 if (!rfh->IsRenderFrameLive())
3636 continue;
3637 handles.push_back(rfh->PauseSubresourceLoading());
3638 }
3639 return handles;
3640}
3641
[email protected]959be4c2014-04-08 15:01:333642void WebContentsImpl::Undo() {
Dave Tapuskae782bea2019-07-09 16:21:513643 auto* input_handler = GetFocusedFrameInputHandler();
3644 if (!input_handler)
[email protected]959be4c2014-04-08 15:01:333645 return;
3646
Dave Tapuskae782bea2019-07-09 16:21:513647 input_handler->Undo();
[email protected]959be4c2014-04-08 15:01:333648 RecordAction(base::UserMetricsAction("Undo"));
3649}
3650
3651void WebContentsImpl::Redo() {
Dave Tapuskae782bea2019-07-09 16:21:513652 auto* input_handler = GetFocusedFrameInputHandler();
3653 if (!input_handler)
[email protected]959be4c2014-04-08 15:01:333654 return;
Dave Tapuskae782bea2019-07-09 16:21:513655
3656 input_handler->Redo();
[email protected]959be4c2014-04-08 15:01:333657 RecordAction(base::UserMetricsAction("Redo"));
3658}
3659
3660void WebContentsImpl::Cut() {
Dave Tapuskae782bea2019-07-09 16:21:513661 auto* input_handler = GetFocusedFrameInputHandler();
3662 if (!input_handler)
[email protected]959be4c2014-04-08 15:01:333663 return;
3664
Dave Tapuskae782bea2019-07-09 16:21:513665 input_handler->Cut();
[email protected]959be4c2014-04-08 15:01:333666 RecordAction(base::UserMetricsAction("Cut"));
3667}
3668
3669void WebContentsImpl::Copy() {
Dave Tapuskae782bea2019-07-09 16:21:513670 auto* input_handler = GetFocusedFrameInputHandler();
3671 if (!input_handler)
[email protected]959be4c2014-04-08 15:01:333672 return;
3673
Dave Tapuskae782bea2019-07-09 16:21:513674 input_handler->Copy();
[email protected]959be4c2014-04-08 15:01:333675 RecordAction(base::UserMetricsAction("Copy"));
3676}
3677
3678void WebContentsImpl::CopyToFindPboard() {
3679#if defined(OS_MACOSX)
Dave Tapuskae782bea2019-07-09 16:21:513680 auto* input_handler = GetFocusedFrameInputHandler();
3681 if (!input_handler)
[email protected]959be4c2014-04-08 15:01:333682 return;
3683
3684 // Windows/Linux don't have the concept of a find pasteboard.
Dave Tapuskae782bea2019-07-09 16:21:513685 input_handler->CopyToFindPboard();
[email protected]959be4c2014-04-08 15:01:333686 RecordAction(base::UserMetricsAction("CopyToFindPboard"));
3687#endif
3688}
3689
3690void WebContentsImpl::Paste() {
Dave Tapuskae782bea2019-07-09 16:21:513691 auto* input_handler = GetFocusedFrameInputHandler();
3692 if (!input_handler)
[email protected]959be4c2014-04-08 15:01:333693 return;
3694
Dave Tapuskae782bea2019-07-09 16:21:513695 input_handler->Paste();
Archana Simhaf67090c2019-05-06 23:43:233696 for (auto& observer : observers_)
3697 observer.OnPaste();
[email protected]959be4c2014-04-08 15:01:333698 RecordAction(base::UserMetricsAction("Paste"));
3699}
3700
3701void WebContentsImpl::PasteAndMatchStyle() {
Dave Tapuskae782bea2019-07-09 16:21:513702 auto* input_handler = GetFocusedFrameInputHandler();
3703 if (!input_handler)
[email protected]959be4c2014-04-08 15:01:333704 return;
3705
Dave Tapuskae782bea2019-07-09 16:21:513706 input_handler->PasteAndMatchStyle();
Archana Simhaf67090c2019-05-06 23:43:233707 for (auto& observer : observers_)
3708 observer.OnPaste();
[email protected]959be4c2014-04-08 15:01:333709 RecordAction(base::UserMetricsAction("PasteAndMatchStyle"));
3710}
3711
3712void WebContentsImpl::Delete() {
Dave Tapuskae782bea2019-07-09 16:21:513713 auto* input_handler = GetFocusedFrameInputHandler();
3714 if (!input_handler)
[email protected]959be4c2014-04-08 15:01:333715 return;
3716
Dave Tapuskae782bea2019-07-09 16:21:513717 input_handler->Delete();
[email protected]959be4c2014-04-08 15:01:333718 RecordAction(base::UserMetricsAction("DeleteSelection"));
3719}
3720
3721void WebContentsImpl::SelectAll() {
Dave Tapuskae782bea2019-07-09 16:21:513722 auto* input_handler = GetFocusedFrameInputHandler();
3723 if (!input_handler)
[email protected]959be4c2014-04-08 15:01:333724 return;
3725
Dave Tapuskae782bea2019-07-09 16:21:513726 input_handler->SelectAll();
[email protected]959be4c2014-04-08 15:01:333727 RecordAction(base::UserMetricsAction("SelectAll"));
3728}
3729
yabinh351e7ec2017-03-10 02:43:243730void WebContentsImpl::CollapseSelection() {
Dave Tapuskae782bea2019-07-09 16:21:513731 auto* input_handler = GetFocusedFrameInputHandler();
3732 if (!input_handler)
[email protected]959be4c2014-04-08 15:01:333733 return;
3734
Dave Tapuskae782bea2019-07-09 16:21:513735 input_handler->CollapseSelection();
[email protected]1f3fc1d2014-04-03 14:50:173736}
3737
3738void WebContentsImpl::Replace(const base::string16& word) {
Dave Tapuskae782bea2019-07-09 16:21:513739 auto* input_handler = GetFocusedFrameInputHandler();
3740 if (!input_handler)
[email protected]1f3fc1d2014-04-03 14:50:173741 return;
3742
Dave Tapuskae782bea2019-07-09 16:21:513743 input_handler->Replace(word);
[email protected]1f3fc1d2014-04-03 14:50:173744}
3745
3746void WebContentsImpl::ReplaceMisspelling(const base::string16& word) {
Dave Tapuskae782bea2019-07-09 16:21:513747 auto* input_handler = GetFocusedFrameInputHandler();
3748 if (!input_handler)
[email protected]1f3fc1d2014-04-03 14:50:173749 return;
3750
Dave Tapuskae782bea2019-07-09 16:21:513751 input_handler->ReplaceMisspelling(word);
[email protected]959be4c2014-04-08 15:01:333752}
3753
3754void WebContentsImpl::NotifyContextMenuClosed(
3755 const CustomContextMenuContext& context) {
3756 RenderFrameHost* focused_frame = GetFocusedFrame();
3757 if (!focused_frame)
3758 return;
3759
3760 focused_frame->Send(new FrameMsg_ContextMenuClosed(
3761 focused_frame->GetRoutingID(), context));
3762}
3763
3764void WebContentsImpl::ExecuteCustomContextMenuCommand(
3765 int action, const CustomContextMenuContext& context) {
3766 RenderFrameHost* focused_frame = GetFocusedFrame();
3767 if (!focused_frame)
3768 return;
3769
3770 focused_frame->Send(new FrameMsg_CustomContextMenuAction(
3771 focused_frame->GetRoutingID(), context, action));
[email protected]4fed3702014-04-01 09:08:003772}
3773
[email protected]fc2b46b2014-05-03 16:33:453774gfx::NativeView WebContentsImpl::GetNativeView() {
3775 return view_->GetNativeView();
3776}
3777
3778gfx::NativeView WebContentsImpl::GetContentNativeView() {
3779 return view_->GetContentNativeView();
3780}
3781
3782gfx::NativeWindow WebContentsImpl::GetTopLevelNativeWindow() {
3783 return view_->GetTopLevelNativeWindow();
3784}
3785
3786gfx::Rect WebContentsImpl::GetViewBounds() {
3787 return view_->GetViewBounds();
3788}
3789
3790gfx::Rect WebContentsImpl::GetContainerBounds() {
3791 gfx::Rect rv;
3792 view_->GetContainerBounds(&rv);
3793 return rv;
3794}
3795
3796DropData* WebContentsImpl::GetDropData() {
3797 return view_->GetDropData();
3798}
3799
3800void WebContentsImpl::Focus() {
miu9e14e492014-10-25 02:39:043801 view_->Focus();
[email protected]fc2b46b2014-05-03 16:33:453802}
3803
3804void WebContentsImpl::SetInitialFocus() {
miu9e14e492014-10-25 02:39:043805 view_->SetInitialFocus();
[email protected]fc2b46b2014-05-03 16:33:453806}
3807
3808void WebContentsImpl::StoreFocus() {
miu9e14e492014-10-25 02:39:043809 view_->StoreFocus();
[email protected]fc2b46b2014-05-03 16:33:453810}
3811
3812void WebContentsImpl::RestoreFocus() {
miu9e14e492014-10-25 02:39:043813 view_->RestoreFocus();
[email protected]fc2b46b2014-05-03 16:33:453814}
3815
[email protected]b172aee2012-04-10 17:05:263816void WebContentsImpl::FocusThroughTabTraversal(bool reverse) {
Maxim Podgorodskiya926c4ff2017-11-20 02:06:393817 view_->FocusThroughTabTraversal(reverse);
[email protected]96d185d2009-04-24 03:28:543818}
3819
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:413820bool WebContentsImpl::ShowingInterstitialPage() {
creisadca9862017-06-16 19:12:113821 return interstitial_page_ != nullptr;
[email protected]96d185d2009-04-24 03:28:543822}
3823
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:413824InterstitialPageImpl* WebContentsImpl::GetInterstitialPage() {
creisadca9862017-06-16 19:12:113825 return interstitial_page_;
[email protected]686493142011-07-15 21:47:223826}
3827
[email protected]b172aee2012-04-10 17:05:263828bool WebContentsImpl::IsSavable() {
[email protected]a53209b2012-01-20 16:48:163829 // WebKit creates Document object when MIME type is application/xhtml+xml,
3830 // so we also support this MIME type.
3831 return contents_mime_type_ == "text/html" ||
3832 contents_mime_type_ == "text/xml" ||
3833 contents_mime_type_ == "application/xhtml+xml" ||
3834 contents_mime_type_ == "text/plain" ||
3835 contents_mime_type_ == "text/css" ||
Kinuko Yasuda74702f92017-07-31 03:27:533836 blink::IsSupportedJavascriptMimeType(contents_mime_type_);
[email protected]a53209b2012-01-20 16:48:163837}
3838
[email protected]b172aee2012-04-10 17:05:263839void WebContentsImpl::OnSavePage() {
[email protected]c7dd2f62011-07-18 15:57:593840 // If we can not save the page, try to download it.
[email protected]a53209b2012-01-20 16:48:163841 if (!IsSavable()) {
Min Qinda0ed2062018-02-23 22:00:533842 download::RecordSavePackageEvent(
3843 download::SAVE_PACKAGE_DOWNLOAD_ON_NON_HTML);
nasko89ad7742015-03-05 22:14:193844 SaveFrame(GetLastCommittedURL(), Referrer());
[email protected]27678b2a2012-02-04 22:09:143845 return;
[email protected]c7dd2f62011-07-18 15:57:593846 }
3847
3848 Stop();
3849
3850 // Create the save package and possibly prompt the user for the name to save
3851 // the page as. The user prompt is an asynchronous operation that runs on
3852 // another thread.
3853 save_package_ = new SavePackage(this);
3854 save_package_->GetSaveInfo();
3855}
3856
3857// Used in automated testing to bypass prompting the user for file names.
3858// Instead, the names and paths are hard coded rather than running them through
3859// file name sanitation and extension / mime checking.
[email protected]2dec8ec2013-02-07 19:20:343860bool WebContentsImpl::SavePage(const base::FilePath& main_file,
3861 const base::FilePath& dir_path,
[email protected]8ff00d72012-10-23 19:12:213862 SavePageType save_type) {
[email protected]c7dd2f62011-07-18 15:57:593863 // Stop the page from navigating.
3864 Stop();
3865
3866 save_package_ = new SavePackage(this, save_type, main_file, dir_path);
[email protected]8ff00d72012-10-23 19:12:213867 return save_package_->Init(SavePackageDownloadCreatedCallback());
[email protected]c7dd2f62011-07-18 15:57:593868}
3869
[email protected]3c71576ce2013-07-23 02:00:013870void WebContentsImpl::SaveFrame(const GURL& url,
3871 const Referrer& referrer) {
Xing Liu10329bf2018-03-20 19:22:143872 SaveFrameWithHeaders(url, referrer, std::string(), base::string16());
kundaji6c7f9692015-03-09 18:00:373873}
3874
Xing Liu10329bf2018-03-20 19:22:143875void WebContentsImpl::SaveFrameWithHeaders(
3876 const GURL& url,
3877 const Referrer& referrer,
3878 const std::string& headers,
3879 const base::string16& suggested_filename) {
Lei Zhang13a42e42019-02-19 23:28:113880 // Check and see if the guest can handle this.
3881 if (delegate_) {
3882 WebContents* guest_web_contents = nullptr;
3883 if (browser_plugin_embedder_) {
3884 BrowserPluginGuest* guest = browser_plugin_embedder_->GetFullPageGuest();
3885 if (guest)
3886 guest_web_contents = guest->GetWebContents();
3887 } else if (browser_plugin_guest_) {
3888 guest_web_contents = this;
3889 }
3890
3891 if (guest_web_contents && delegate_->GuestSaveFrame(guest_web_contents))
3892 return;
3893 }
3894
nasko89ad7742015-03-05 22:14:193895 if (!GetLastCommittedURL().is_valid())
[email protected]3c71576ce2013-07-23 02:00:013896 return;
sammc92af61552014-11-19 23:27:403897 if (delegate_ && delegate_->SaveFrame(url, referrer))
3898 return;
3899
nasko89ad7742015-03-05 22:14:193900 // TODO(nasko): This check for main frame is incorrect and should be fixed
brettw760f7442016-05-23 21:19:223901 // by explicitly passing in which frame this method should target. This would
3902 // indicate whether it's the main frame, and also tell us the frame pointer
3903 // to use for routing.
nasko89ad7742015-03-05 22:14:193904 bool is_main_frame = (url == GetLastCommittedURL());
brettw760f7442016-05-23 21:19:223905 RenderFrameHost* frame_host = GetMainFrame();
[email protected]3c71576ce2013-07-23 02:00:013906
avib7348942015-12-25 20:57:103907 int64_t post_id = -1;
[email protected]3c71576ce2013-07-23 02:00:013908 if (is_main_frame) {
Lucas Furukawa Gadani5553a152019-01-08 18:55:573909 NavigationEntry* entry = controller_.GetLastCommittedEntry();
[email protected]3c71576ce2013-07-23 02:00:013910 if (entry)
3911 post_id = entry->GetPostID();
3912 }
Ramin Halavati24e21012017-07-10 12:56:063913 net::NetworkTrafficAnnotationTag traffic_annotation =
3914 net::DefineNetworkTrafficAnnotation("download_web_contents_frame", R"(
3915 semantics {
3916 sender: "Save Page Action"
3917 description:
3918 "Saves the given frame's URL to the local file system."
3919 trigger:
3920 "The user has triggered a save operation on the frame through a "
3921 "context menu or other mechanism."
3922 data: "None."
3923 destination: WEBSITE
3924 }
3925 policy {
Ramin Halavati3b979782017-07-21 11:40:263926 cookies_allowed: YES
Ramin Halavati24e21012017-07-10 12:56:063927 cookies_store: "user"
3928 setting:
3929 "This feature cannot be disabled by settings, but it's is only "
3930 "triggered by user request."
3931 policy_exception_justification: "Not implemented."
3932 })");
Min Qina904f3302018-02-13 23:33:343933 auto params = std::make_unique<download::DownloadUrlParameters>(
brettw760f7442016-05-23 21:19:223934 url, frame_host->GetProcess()->GetID(),
3935 frame_host->GetRenderViewHost()->GetRoutingID(),
Matt Menke6d93c3c2018-10-10 15:21:503936 frame_host->GetRoutingID(), traffic_annotation);
Min Qina904f3302018-02-13 23:33:343937 params->set_referrer(referrer.url);
3938 params->set_referrer_policy(
3939 Referrer::ReferrerPolicyForUrlRequest(referrer.policy));
[email protected]3c71576ce2013-07-23 02:00:013940 params->set_post_id(post_id);
[email protected]3c71576ce2013-07-23 02:00:013941 if (post_id >= 0)
3942 params->set_method("POST");
3943 params->set_prompt(true);
kundaji6c7f9692015-03-09 18:00:373944
3945 if (headers.empty()) {
3946 params->set_prefer_cache(true);
3947 } else {
Min Qina904f3302018-02-13 23:33:343948 for (download::DownloadUrlParameters::RequestHeadersNameValuePair
3949 key_value : ParseDownloadHeaders(headers)) {
Megan Jablonski2f6a4c52017-07-10 23:01:253950 params->add_request_header(key_value.first, key_value.second);
kundaji6c7f9692015-03-09 18:00:373951 }
3952 }
Xing Liu10329bf2018-03-20 19:22:143953 params->set_suggested_name(suggested_filename);
Min Qin0ca8e1ee2018-01-31 00:49:353954 params->set_download_source(download::DownloadSource::WEB_CONTENTS_API);
thestig859c78892017-05-15 21:17:063955 BrowserContext::GetDownloadManager(GetBrowserContext())
Ramin Halavati03efa732017-06-12 09:28:203956 ->DownloadUrl(std::move(params));
[email protected]3c71576ce2013-07-23 02:00:013957}
3958
[email protected]b172aee2012-04-10 17:05:263959void WebContentsImpl::GenerateMHTML(
dewittj6dc5747a2016-05-17 01:48:473960 const MHTMLGenerationParams& params,
Avi Drissman149b7832018-03-23 14:31:493961 base::OnceCallback<void(int64_t)> callback) {
3962 MHTMLGenerationManager::GetInstance()->SaveMHTML(this, params,
3963 std::move(callback));
[email protected]aa4f3972012-03-01 18:12:123964}
3965
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:413966const std::string& WebContentsImpl::GetContentsMimeType() {
[email protected]0bfbf882011-12-22 18:19:273967 return contents_mime_type_;
3968}
3969
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:413970bool WebContentsImpl::WillNotifyDisconnection() {
[email protected]0bfbf882011-12-22 18:19:273971 return notify_disconnection_;
3972}
3973
Leon Hanc819dc62019-01-28 04:30:193974blink::mojom::RendererPreferences* WebContentsImpl::GetMutableRendererPrefs() {
[email protected]0bfbf882011-12-22 18:19:273975 return &renderer_preferences_;
3976}
3977
[email protected]e35ccd52012-05-23 16:22:473978void WebContentsImpl::Close() {
3979 Close(GetRenderViewHost());
3980}
3981
Ella Ge80a52dce2017-11-15 18:01:523982void WebContentsImpl::DragSourceEndedAt(float client_x,
3983 float client_y,
3984 float screen_x,
3985 float screen_y,
Paul Meyer0c58c3712016-11-17 22:59:513986 blink::WebDragOperation operation,
3987 RenderWidgetHost* source_rwh) {
Lei Zhang6410ec1a2019-01-31 21:38:583988 if (browser_plugin_embedder_) {
paulmeyer8fc8ea92016-11-15 05:12:213989 browser_plugin_embedder_->DragSourceEndedAt(
3990 client_x, client_y, screen_x, screen_y, operation);
Lei Zhang6410ec1a2019-01-31 21:38:583991 }
Paul Meyer0c58c3712016-11-17 22:59:513992 if (source_rwh) {
Ella Ge80a52dce2017-11-15 18:01:523993 source_rwh->DragSourceEndedAt(gfx::PointF(client_x, client_y),
3994 gfx::PointF(screen_x, screen_y), operation);
Paul Meyer0c58c3712016-11-17 22:59:513995 }
[email protected]cf200a562013-05-03 16:24:293996}
3997
jam73f892642016-09-11 06:04:063998void WebContentsImpl::LoadStateChanged(
Charles Harrison911fa062018-03-06 21:01:463999 const std::string& host,
jam73f892642016-09-11 06:04:064000 const net::LoadStateWithParam& load_state,
4001 uint64_t upload_position,
4002 uint64_t upload_size) {
Charles Harrison911fa062018-03-06 21:01:464003 base::string16 host16 = url_formatter::IDNToUnicode(host);
Elly Fong-Jonescad9c3d12018-01-31 17:26:104004 // Drop no-op updates.
4005 if (load_state_.state == load_state.state &&
4006 load_state_.param == load_state.param &&
4007 upload_position_ == upload_position && upload_size_ == upload_size &&
Charles Harrison911fa062018-03-06 21:01:464008 load_state_host_ == host16) {
Elly Fong-Jonescad9c3d12018-01-31 17:26:104009 return;
4010 }
jam73f892642016-09-11 06:04:064011 load_state_ = load_state;
4012 upload_position_ = upload_position;
4013 upload_size_ = upload_size;
Charles Harrison911fa062018-03-06 21:01:464014 load_state_host_ = host16;
jam73f892642016-09-11 06:04:064015 if (load_state_.state == net::LOAD_STATE_READING_RESPONSE)
4016 SetNotWaitingForResponse();
4017 if (IsLoading()) {
4018 NotifyNavigationStateChanged(static_cast<InvalidateTypes>(
4019 INVALIDATE_TYPE_LOAD | INVALIDATE_TYPE_TAB));
4020 }
4021}
4022
Francois Doraye6161152018-03-27 22:05:374023void WebContentsImpl::SetVisibility(Visibility visibility) {
4024 const Visibility previous_visibility = visibility_;
4025 visibility_ = visibility;
4026
Francois Dorayfe4a1772018-02-17 04:17:094027 // Notify observers if the visibility changed or if WasShown() is being called
4028 // for the first time.
Francois Dorayfe4a1772018-02-17 04:17:094029 if (visibility != previous_visibility ||
4030 (visibility == Visibility::VISIBLE && !did_first_set_visible_)) {
4031 for (auto& observer : observers_)
4032 observer.OnVisibilityChanged(visibility);
4033 }
4034}
4035
Michael Thiessen896405db2017-07-20 17:52:324036void WebContentsImpl::NotifyWebContentsFocused(
4037 RenderWidgetHost* render_widget_host) {
ericwilligersde386342016-10-19 02:35:094038 for (auto& observer : observers_)
Michael Thiessen896405db2017-07-20 17:52:324039 observer.OnWebContentsFocused(render_widget_host);
calamity7fe55ce2015-04-10 03:59:374040}
4041
Michael Thiessen896405db2017-07-20 17:52:324042void WebContentsImpl::NotifyWebContentsLostFocus(
4043 RenderWidgetHost* render_widget_host) {
zijiehe3bee08e22017-05-17 04:14:464044 for (auto& observer : observers_)
Michael Thiessen896405db2017-07-20 17:52:324045 observer.OnWebContentsLostFocus(render_widget_host);
zijiehe3bee08e22017-05-17 04:14:464046}
4047
Paul Meyer0c58c3712016-11-17 22:59:514048void WebContentsImpl::SystemDragEnded(RenderWidgetHost* source_rwh) {
4049 if (source_rwh)
4050 source_rwh->DragSourceSystemDragEnded();
Lei Zhang6410ec1a2019-01-31 21:38:584051 if (browser_plugin_embedder_)
[email protected]cf200a562013-05-03 16:24:294052 browser_plugin_embedder_->SystemDragEnded();
[email protected]7813bd72011-02-05 02:19:344053}
4054
[email protected]b172aee2012-04-10 17:05:264055void WebContentsImpl::SetClosedByUserGesture(bool value) {
[email protected]0bfbf882011-12-22 18:19:274056 closed_by_user_gesture_ = value;
4057}
4058
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:414059bool WebContentsImpl::GetClosedByUserGesture() {
[email protected]0bfbf882011-12-22 18:19:274060 return closed_by_user_gesture_;
4061}
4062
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:414063int WebContentsImpl::GetMinimumZoomPercent() {
[email protected]0bfbf882011-12-22 18:19:274064 return minimum_zoom_percent_;
4065}
4066
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:414067int WebContentsImpl::GetMaximumZoomPercent() {
[email protected]0bfbf882011-12-22 18:19:274068 return maximum_zoom_percent_;
4069}
4070
mcnee432e47d2015-11-09 19:37:464071void WebContentsImpl::SetPageScale(float page_scale_factor) {
Lukasz Anforowiczf4357ca2017-09-07 01:43:324072 GetRenderViewHost()->Send(new ViewMsg_SetPageScale(
4073 GetRenderViewHost()->GetRoutingID(), page_scale_factor));
ccameronb7c1d6c2015-03-09 17:08:244074}
4075
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:414076gfx::Size WebContentsImpl::GetPreferredSize() {
Francois Doray24fc62c2017-12-11 17:27:334077 return IsBeingCaptured() ? preferred_size_for_capture_ : preferred_size_;
[email protected]bcd2815602012-01-14 18:17:234078}
4079
[email protected]b172aee2012-04-10 17:05:264080bool WebContentsImpl::GotResponseToLockMouseRequest(bool allowed) {
lfgf0cd46e2017-01-04 00:05:234081 if (!GuestMode::IsCrossProcessFrameGuest(GetWebContents()) &&
4082 GetBrowserPluginGuest())
[email protected]660f18e2014-05-15 20:53:054083 return GetBrowserPluginGuest()->LockMouse(allowed);
4084
lfgf0cd46e2017-01-04 00:05:234085 if (mouse_lock_widget_) {
4086 if (mouse_lock_widget_->delegate()->GetAsWebContents() != this) {
4087 return mouse_lock_widget_->delegate()
4088 ->GetAsWebContents()
4089 ->GotResponseToLockMouseRequest(allowed);
4090 }
lfgad8244352016-07-13 16:55:514091
lfgf0cd46e2017-01-04 00:05:234092 if (mouse_lock_widget_->GotResponseToLockMouseRequest(allowed))
4093 return true;
4094 }
4095
4096 for (WebContentsImpl* current = this; current;
4097 current = current->GetOuterWebContents()) {
4098 current->mouse_lock_widget_ = nullptr;
4099 }
4100
lfgbee1e0a2016-06-08 21:24:214101 return false;
[email protected]0bfbf882011-12-22 18:19:274102}
4103
Joe Downing13dd76b2018-04-09 18:32:154104bool WebContentsImpl::GotResponseToKeyboardLockRequest(bool allowed) {
4105 if (!keyboard_lock_widget_)
4106 return false;
4107
4108 if (keyboard_lock_widget_->delegate()->GetAsWebContents() != this) {
4109 NOTREACHED();
4110 return false;
4111 }
4112
4113 // KeyboardLock is only supported when called by the top-level browsing
4114 // context and is not supported in embedded content scenarios.
4115 if (GetOuterWebContents())
4116 return false;
4117
4118 keyboard_lock_widget_->GotResponseToKeyboardLockRequest(allowed);
4119 return true;
4120}
4121
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:414122bool WebContentsImpl::HasOpener() {
Ivan Kotenkov2c0d2bb32017-11-01 15:41:284123 return GetOpener() != nullptr;
[email protected]a0358d72012-03-09 14:06:504124}
4125
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:414126RenderFrameHostImpl* WebContentsImpl::GetOpener() {
alexmose201c7cd2015-06-10 17:14:214127 FrameTreeNode* opener_ftn = frame_tree_.root()->opener();
lukasza6f8ac622017-06-06 03:10:204128 return opener_ftn ? opener_ftn->current_frame_host() : nullptr;
jochen55ff3502014-12-18 20:52:574129}
4130
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:414131bool WebContentsImpl::HasOriginalOpener() {
Ivan Kotenkov2c0d2bb32017-11-01 15:41:284132 return GetOriginalOpener() != nullptr;
jochen6004a362017-02-04 00:11:404133}
4134
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:414135RenderFrameHostImpl* WebContentsImpl::GetOriginalOpener() {
jochen6004a362017-02-04 00:11:404136 FrameTreeNode* opener_ftn = frame_tree_.root()->original_opener();
lukasza6f8ac622017-06-06 03:10:204137 return opener_ftn ? opener_ftn->current_frame_host() : nullptr;
jochen6004a362017-02-04 00:11:404138}
4139
[email protected]cb805452013-05-22 15:16:214140void WebContentsImpl::DidChooseColorInColorChooser(SkColor color) {
Joel Hockey85b379d2017-12-11 10:42:134141 color_chooser_->DidChooseColorInColorChooser(color);
[email protected]da8543762012-03-20 08:52:204142}
4143
[email protected]cb805452013-05-22 15:16:214144void WebContentsImpl::DidEndColorChooser() {
Joel Hockey85b379d2017-12-11 10:42:134145 color_chooser_.reset();
[email protected]da8543762012-03-20 08:52:204146}
4147
halton.huoca2eabd2015-07-06 08:17:404148int WebContentsImpl::DownloadImage(
4149 const GURL& url,
4150 bool is_favicon,
4151 uint32_t max_bitmap_size,
4152 bool bypass_cache,
Avi Drissmana5a52dd2018-03-27 03:39:024153 WebContents::ImageDownloadCallback callback) {
amistry9f01b772015-07-29 01:54:554154 DCHECK_CURRENTLY_ON(BrowserThread::UI);
halton.huoca2eabd2015-07-06 08:17:404155 static int next_image_download_id = 0;
Gyuyoung Kime370bdb2019-06-28 00:31:524156 const blink::mojom::ImageDownloaderPtr& mojo_image_downloader =
halton.huoca2eabd2015-07-06 08:17:404157 GetMainFrame()->GetMojoImageDownloader();
amistry9f01b772015-07-29 01:54:554158 const int download_id = ++next_image_download_id;
4159 if (!mojo_image_downloader) {
4160 // If the renderer process is dead (i.e. crash, or memory pressure on
4161 // Android), the downloader service will be invalid. Pre-Mojo, this would
Avi Drissmana5a52dd2018-03-27 03:39:024162 // hang the callback indefinitely since the IPC would be dropped. Now,
amistry9f01b772015-07-29 01:54:554163 // respond with a 400 HTTP error code to indicate that something went wrong.
Sami Kyostila8e4d5a92019-08-02 12:45:054164 base::PostTask(
Eric Seckler8652dcd52018-09-20 10:42:284165 FROM_HERE, {BrowserThread::UI},
tzike2aca992017-09-05 08:50:544166 base::BindOnce(&WebContentsImpl::OnDidDownloadImage,
Avi Drissmana5a52dd2018-03-27 03:39:024167 weak_factory_.GetWeakPtr(), std::move(callback),
4168 download_id, url, 400, std::vector<SkBitmap>(),
4169 std::vector<gfx::Size>()));
amistry9f01b772015-07-29 01:54:554170 return download_id;
4171 }
4172
halton.huoca2eabd2015-07-06 08:17:404173 mojo_image_downloader->DownloadImage(
sammcf6f4fcf2016-06-21 05:39:394174 url, is_favicon, max_bitmap_size, bypass_cache,
tzike2aca992017-09-05 08:50:544175 base::BindOnce(&WebContentsImpl::OnDidDownloadImage,
Avi Drissmana5a52dd2018-03-27 03:39:024176 weak_factory_.GetWeakPtr(), std::move(callback),
4177 download_id, url));
amistry9f01b772015-07-29 01:54:554178 return download_id;
[email protected]795c28972012-12-06 06:13:394179}
4180
[email protected]36ec24f2014-01-09 00:32:084181void WebContentsImpl::Find(int request_id,
4182 const base::string16& search_text,
Rakina Zata Amni3f77dff2018-09-08 16:19:434183 blink::mojom::FindOptionsPtr options) {
thestig6057a6b22015-11-12 23:01:334184 // Cowardly refuse to search for no text.
4185 if (search_text.empty()) {
4186 NOTREACHED();
4187 return;
4188 }
4189
Rakina Zata Amni3f77dff2018-09-08 16:19:434190 GetOrCreateFindRequestManager()->Find(request_id, search_text,
4191 std::move(options));
[email protected]36ec24f2014-01-09 00:32:084192}
4193
4194void WebContentsImpl::StopFinding(StopFindAction action) {
paulmeyerfeafc2d2017-04-25 21:46:404195 if (FindRequestManager* manager = GetFindRequestManager())
4196 manager->StopFinding(action);
[email protected]36ec24f2014-01-09 00:32:084197}
4198
Tommy Steimel18360512017-11-01 00:38:194199bool WebContentsImpl::WasEverAudible() {
4200 return was_ever_audible_;
4201}
4202
Avi Drissmana5a52dd2018-03-27 03:39:024203void WebContentsImpl::GetManifest(GetManifestCallback callback) {
4204 manifest_manager_host_->GetManifest(std::move(callback));
mlamouriefdca9d2014-09-16 16:55:404205}
4206
bokanece34a82016-01-28 19:49:464207void WebContentsImpl::ExitFullscreen(bool will_cause_resize) {
mlamouri7a78d6fd2015-01-17 13:23:534208 // Clean up related state and initiate the fullscreen exit.
avi3627ecac2015-10-16 17:40:434209 GetRenderViewHost()->GetWidget()->RejectMouseLockOrUnlockIfNecessary();
bokanece34a82016-01-28 19:49:464210 ExitFullscreenMode(will_cause_resize);
mlamouri7a78d6fd2015-01-17 13:23:534211}
4212
mariakhomenko44bdc4732015-04-29 01:55:384213void WebContentsImpl::ResumeLoadingCreatedWebContents() {
Nasko Oskov83a8cf92018-10-19 14:58:564214 if (delayed_load_url_params_.get()) {
4215 DCHECK(!delayed_open_url_params_);
4216 controller_.LoadURLWithParams(*delayed_load_url_params_.get());
4217 delayed_load_url_params_.reset(nullptr);
4218 return;
4219 }
4220
mariakhomenkoa4971c12015-07-21 19:04:374221 if (delayed_open_url_params_.get()) {
4222 OpenURL(*delayed_open_url_params_.get());
4223 delayed_open_url_params_.reset(nullptr);
4224 return;
4225 }
4226
mariakhomenko44bdc4732015-04-29 01:55:384227 // Resume blocked requests for both the RenderViewHost and RenderFrameHost.
4228 // TODO(brettw): It seems bogus to reach into here and initialize the host.
dfalcantarae6b7b46752015-07-10 18:14:164229 if (is_resume_pending_) {
4230 is_resume_pending_ = false;
avi0f1bbc9712015-11-17 02:58:134231 GetRenderViewHost()->GetWidget()->Init();
dfalcantarae6b7b46752015-07-10 18:14:164232 GetMainFrame()->Init();
4233 }
mariakhomenko44bdc4732015-04-29 01:55:384234}
4235
[email protected]b172aee2012-04-10 17:05:264236bool WebContentsImpl::FocusLocationBarByDefault() {
Xiyuan Xia2a66b112018-12-06 15:52:164237 if (should_focus_location_bar_by_default_)
[email protected]0c9406632013-02-08 01:13:334238 return true;
Xiyuan Xia2a66b112018-12-06 15:52:164239
[email protected]0c9406632013-02-08 01:13:334240 return delegate_ && delegate_->ShouldFocusLocationBarByDefault(this);
[email protected]0bfbf882011-12-22 18:19:274241}
4242
Tommy C. Li8bb5bf42019-01-18 18:45:514243void WebContentsImpl::SetFocusToLocationBar() {
[email protected]0bfbf882011-12-22 18:19:274244 if (delegate_)
Tommy C. Li8bb5bf42019-01-18 18:45:514245 delegate_->SetFocusToLocationBar();
[email protected]c40d6232011-03-25 00:16:214246}
4247
clamy0e119882015-07-31 16:12:334248void WebContentsImpl::DidStartNavigation(NavigationHandle* navigation_handle) {
Charles Harrison53096ba2017-12-15 15:39:484249 TRACE_EVENT1("navigation", "WebContentsImpl::DidStartNavigation",
4250 "navigation_handle", navigation_handle);
ericwilligersde386342016-10-19 02:35:094251 for (auto& observer : observers_)
4252 observer.DidStartNavigation(navigation_handle);
Becca Hughesd11d6502018-07-31 17:01:284253
4254 if (display_cutout_host_impl_)
4255 display_cutout_host_impl_->DidStartNavigation(navigation_handle);
Xiyuan Xia2a66b112018-12-06 15:52:164256
4257 if (navigation_handle->IsInMainFrame()) {
4258 // When the browser is started with about:blank as the startup URL, focus
4259 // the location bar (which will also select its contents) so people can
4260 // simply begin typing to navigate elsewhere.
4261 //
4262 // We need to be careful not to trigger this for anything other than the
4263 // startup navigation. In particular, if we allow an attacker to open a
4264 // popup to about:blank, then navigate, focusing the Omnibox will cause the
4265 // end of the new URL to be scrolled into view instead of the start,
4266 // allowing the attacker to spoof other URLs. The conditions checked here
4267 // are all aimed at ensuring no such attacker-controlled navigation can
4268 // trigger this.
4269 should_focus_location_bar_by_default_ =
4270 controller_.IsInitialNavigation() &&
4271 !navigation_handle->IsRendererInitiated() &&
4272 navigation_handle->GetURL() == url::kAboutBlankURL;
4273 }
clamy0e119882015-07-31 16:12:334274}
4275
4276void WebContentsImpl::DidRedirectNavigation(
4277 NavigationHandle* navigation_handle) {
Charles Harrison53096ba2017-12-15 15:39:484278 TRACE_EVENT1("navigation", "WebContentsImpl::DidRedirectNavigation",
4279 "navigation_handle", navigation_handle);
ericwilligersde386342016-10-19 02:35:094280 for (auto& observer : observers_)
4281 observer.DidRedirectNavigation(navigation_handle);
jam5f358e52017-02-13 16:37:034282
4283 // Notify accessibility if this is a reload. This has to called on the
4284 // BrowserAccessibilityManager associated with the old RFHI.
4285 if (navigation_handle->GetReloadType() != ReloadType::NONE) {
4286 NavigationHandleImpl* nhi =
4287 static_cast<NavigationHandleImpl*>(navigation_handle);
4288 BrowserAccessibilityManager* manager =
4289 nhi->frame_tree_node()
4290 ->current_frame_host()
4291 ->browser_accessibility_manager();
4292 if (manager)
4293 manager->UserIsReloading();
4294 }
clamy0e119882015-07-31 16:12:334295}
4296
clamyefca29e2015-09-17 00:22:114297void WebContentsImpl::ReadyToCommitNavigation(
4298 NavigationHandle* navigation_handle) {
Charles Harrison53096ba2017-12-15 15:39:484299 TRACE_EVENT1("navigation", "WebContentsImpl::ReadyToCommitNavigation",
4300 "navigation_handle", navigation_handle);
ericwilligersde386342016-10-19 02:35:094301 for (auto& observer : observers_)
4302 observer.ReadyToCommitNavigation(navigation_handle);
John Abd-El-Malekd4882642017-12-04 21:45:194303
Kenneth Russell954ed9ce2018-04-12 23:07:014304 // If any domains are blocked from accessing 3D APIs because they may
4305 // have caused the GPU to reset recently, unblock them here if the user
4306 // initiated this navigation. This implies that the user was involved in
4307 // the decision to navigate, so there's no concern about
4308 // denial-of-service issues. Want to do this as early as
4309 // possible to avoid race conditions with pages attempting to access
4310 // WebGL early on.
4311 //
4312 // TODO(crbug.com/617904): currently navigations initiated by the browser
4313 // (reload button, reload menu option, pressing return in the Omnibox)
4314 // return false from HasUserGesture(). If or when that is addressed,
4315 // remove the check for IsRendererInitiated() below.
4316 //
4317 // TODO(crbug.com/832180): HasUserGesture comes from the renderer
4318 // process and isn't validated. Until it is, don't trust it.
4319 if (!navigation_handle->IsRendererInitiated()) {
4320 GpuDataManagerImpl::GetInstance()->UnblockDomainFrom3DAPIs(
4321 navigation_handle->GetURL());
4322 }
4323
John Abd-El-Malekfde08e6da2017-12-05 04:06:244324 if (navigation_handle->IsSameDocument())
4325 return;
4326
Livvie Lin139bf442018-08-10 00:18:544327 // SSLInfo is not needed on subframe navigations since the main-frame
4328 // certificate is the only one that can be inspected (using the info
4329 // bubble) without refreshing the page with DevTools open.
Carlos ILaed1e7b2019-03-12 19:27:214330 // We don't call DidStartResourceResponse on net errors, since that results on
4331 // existing cert exceptions being revoked, which leads to weird behavior with
4332 // committed interstitials or while offline. We only need the error check for
4333 // the main frame case because unlike this method, SubresourceResponseStarted
4334 // does not get called on network errors.
4335 if (navigation_handle->IsInMainFrame() &&
4336 navigation_handle->GetNetErrorCode() == net::OK) {
Livvie Lin139bf442018-08-10 00:18:544337 controller_.ssl_manager()->DidStartResourceResponse(
4338 navigation_handle->GetURL(),
Camille Lamy62b826012019-02-26 09:15:474339 navigation_handle->GetSSLInfo().has_value()
4340 ? net::IsCertStatusError(
4341 navigation_handle->GetSSLInfo()->cert_status)
4342 : false);
4343 }
John Abd-El-Malekfde08e6da2017-12-05 04:06:244344
4345 SetNotWaitingForResponse();
clamyefca29e2015-09-17 00:22:114346}
4347
clamy0e119882015-07-31 16:12:334348void WebContentsImpl::DidFinishNavigation(NavigationHandle* navigation_handle) {
Charles Harrison53096ba2017-12-15 15:39:484349 TRACE_EVENT1("navigation", "WebContentsImpl::DidFinishNavigation",
4350 "navigation_handle", navigation_handle);
Steven Holte38041342018-06-22 22:00:574351
ericwilligersde386342016-10-19 02:35:094352 for (auto& observer : observers_)
4353 observer.DidFinishNavigation(navigation_handle);
clamy0e119882015-07-31 16:12:334354
Becca Hughesd11d6502018-07-31 17:01:284355 if (display_cutout_host_impl_)
4356 display_cutout_host_impl_->DidFinishNavigation(navigation_handle);
4357
jam5f358e52017-02-13 16:37:034358 if (navigation_handle->HasCommitted()) {
Dominic Farolinof87ec2a2019-07-04 09:18:094359 // TODO(domfarolino, dmazzoni): Do this using WebContentsObserver. See
4360 // https://crbug.com/981271.
dmazzonibf8cec42015-02-08 08:28:084361 BrowserAccessibilityManager* manager =
jam5f358e52017-02-13 16:37:034362 static_cast<RenderFrameHostImpl*>(
4363 navigation_handle->GetRenderFrameHost())
4364 ->browser_accessibility_manager();
4365 if (manager) {
4366 if (navigation_handle->IsErrorPage()) {
4367 manager->NavigationFailed();
4368 } else {
4369 manager->NavigationSucceeded();
4370 }
4371 }
Tommy Steimel18360512017-11-01 00:38:194372
4373 if (navigation_handle->IsInMainFrame() &&
4374 !navigation_handle->IsSameDocument()) {
4375 was_ever_audible_ = false;
Steven Holte38041342018-06-22 22:00:574376 last_committed_source_id_ =
4377 ukm::ConvertToSourceId(navigation_handle->GetNavigationId(),
4378 ukm::SourceIdType::NAVIGATION_ID);
Tommy Steimel18360512017-11-01 00:38:194379 }
dmazzonibf8cec42015-02-08 08:28:084380 }
Xiyuan Xia2a66b112018-12-06 15:52:164381
4382 // If we didn't end up on about:blank after setting this in DidStartNavigation
4383 // then don't focus the location bar.
4384 if (should_focus_location_bar_by_default_ &&
4385 navigation_handle->GetURL() != url::kAboutBlankURL) {
4386 should_focus_location_bar_by_default_ = false;
4387 }
[email protected]400992b2012-06-14 00:03:544388}
4389
[email protected]b80624c2014-02-09 02:46:554390void WebContentsImpl::DidFailLoadWithError(
4391 RenderFrameHostImpl* render_frame_host,
[email protected]b80624c2014-02-09 02:46:554392 const GURL& url,
[email protected]b80624c2014-02-09 02:46:554393 int error_code,
Yutaka Hirano16d3c5d2017-07-31 06:17:304394 const base::string16& error_description) {
ericwilligersde386342016-10-19 02:35:094395 for (auto& observer : observers_) {
Yutaka Hirano16d3c5d2017-07-31 06:17:304396 observer.DidFailLoad(render_frame_host, url, error_code, error_description);
ericwilligersde386342016-10-19 02:35:094397 }
[email protected]b80624c2014-02-09 02:46:554398}
4399
[email protected]52913802013-12-10 05:52:184400void WebContentsImpl::NotifyChangedNavigationState(
4401 InvalidateTypes changed_flags) {
4402 NotifyNavigationStateChanged(changed_flags);
4403}
4404
toyoshim0df1d3a2016-09-09 09:52:484405void WebContentsImpl::DidStartNavigationToPendingEntry(const GURL& url,
4406 ReloadType reload_type) {
[email protected]3691e5cf2014-01-22 10:16:204407 // Notify observers about navigation.
ericwilligersde386342016-10-19 02:35:094408 for (auto& observer : observers_)
4409 observer.DidStartNavigationToPendingEntry(url, reload_type);
[email protected]3691e5cf2014-01-22 10:16:204410}
4411
lukasza1d025732016-09-28 21:36:084412bool WebContentsImpl::ShouldTransferNavigation(bool is_main_frame_navigation) {
creis29460272015-12-16 04:38:224413 if (!delegate_)
4414 return true;
lukasza1d025732016-09-28 21:36:084415 return delegate_->ShouldTransferNavigation(is_main_frame_navigation);
creis29460272015-12-16 04:38:224416}
4417
[email protected]aa62afd2014-04-22 19:22:464418bool WebContentsImpl::ShouldPreserveAbortedURLs() {
4419 if (!delegate_)
4420 return false;
4421 return delegate_->ShouldPreserveAbortedURLs(this);
4422}
4423
[email protected]0d0f4c492014-04-02 06:42:574424void WebContentsImpl::DidNavigateMainFramePreCommit(
[email protected]5cfbddc2014-06-23 23:52:234425 bool navigation_is_within_page) {
[email protected]0d0f4c492014-04-02 06:42:574426 // Ensure fullscreen mode is exited before committing the navigation to a
4427 // different page. The next page will not start out assuming it is in
4428 // fullscreen mode.
[email protected]5cfbddc2014-06-23 23:52:234429 if (navigation_is_within_page) {
[email protected]dfc39cb2014-04-09 22:58:194430 // No page change? Then, the renderer and browser can remain in fullscreen.
[email protected]0d0f4c492014-04-02 06:42:574431 return;
4432 }
alexmos5dd617d2016-06-07 04:21:154433 if (IsFullscreenForCurrentTab())
bokanece34a82016-01-28 19:49:464434 ExitFullscreen(false);
alexmos5dd617d2016-06-07 04:21:154435 DCHECK(!IsFullscreenForCurrentTab());
Joe Downingc1a57efe2018-05-09 18:23:544436
4437 // Clean up keyboard lock state when navigating.
4438 CancelKeyboardLock(keyboard_lock_widget_);
[email protected]0d0f4c492014-04-02 06:42:574439}
4440
[email protected]37567b432014-02-12 01:12:224441void WebContentsImpl::DidNavigateMainFramePostCommit(
sky8b00392d2015-01-10 00:30:284442 RenderFrameHostImpl* render_frame_host,
[email protected]37567b432014-02-12 01:12:224443 const LoadCommittedDetails& details,
4444 const FrameHostMsg_DidCommitProvisionalLoad_Params& params) {
4445 if (details.is_navigation_to_different_page()) {
4446 // Clear the status bubble. This is a workaround for a bug where WebKit
4447 // doesn't let us know that the cursor left an element during a
4448 // transition (this is also why the mouse cursor remains as a hand after
4449 // clicking on a link); see bugs 1184641 and 980803. We don't want to
4450 // clear the bubble when a user navigates to a named anchor in the same
4451 // page.
Lukasz Anforowiczfd707142018-02-07 19:46:134452 ClearTargetURL();
lanwei9d343ad22015-02-11 01:46:004453
4454 RenderWidgetHostViewBase* rwhvb =
4455 static_cast<RenderWidgetHostViewBase*>(GetRenderWidgetHostView());
4456 if (rwhvb)
4457 rwhvb->OnDidNavigateMainFrameToNewPage();
yusufod41c5f92015-03-06 00:14:284458
peter90afaba2015-06-01 12:05:294459 did_first_visually_non_empty_paint_ = false;
4460
yusufod41c5f92015-03-06 00:14:284461 // Reset theme color on navigation to new page.
Alan Cutter92d59252019-03-29 02:32:334462 theme_color_.reset();
[email protected]37567b432014-02-12 01:12:224463 }
4464
[email protected]e85165c642014-06-10 14:34:314465 if (delegate_)
[email protected]37567b432014-02-12 01:12:224466 delegate_->DidNavigateMainFramePostCommit(this);
[email protected]e85165c642014-06-10 14:34:314467 view_->SetOverscrollControllerEnabled(CanOverscrollContent());
Shivani Sharma985474f2019-05-23 21:36:474468
4469 if (!details.is_same_document && GetInnerWebContents().empty())
4470 had_inner_webcontents_ = false;
[email protected]37567b432014-02-12 01:12:224471}
4472
4473void WebContentsImpl::DidNavigateAnyFramePostCommit(
4474 RenderFrameHostImpl* render_frame_host,
4475 const LoadCommittedDetails& details,
4476 const FrameHostMsg_DidCommitProvisionalLoad_Params& params) {
[email protected]b4c84012014-04-28 19:51:104477 // Now that something has committed, we don't need to track whether the
4478 // initial page has been accessed.
4479 has_accessed_initial_document_ = false;
4480
[email protected]37567b432014-02-12 01:12:224481 // If we navigate off the page, close all JavaScript dialogs.
eugenebutee08663a2017-04-27 17:43:124482 if (!details.is_same_document)
creis89a0f782015-05-27 16:13:174483 CancelActiveAndPendingDialogs();
[email protected]37567b432014-02-12 01:12:224484
avid53461d2016-02-25 17:15:044485 // If this is a user-initiated navigation, start allowing JavaScript dialogs
4486 // again.
avi5d3b8692016-10-12 22:00:464487 if (params.gesture == NavigationGestureUser && dialog_manager_) {
avi6879fcf2017-01-21 05:27:534488 dialog_manager_->CancelDialogs(this, /*reset_state=*/true);
avi5d3b8692016-10-12 22:00:464489 }
[email protected]37567b432014-02-12 01:12:224490}
4491
4492void WebContentsImpl::SetMainFrameMimeType(const std::string& mime_type) {
4493 contents_mime_type_ = mime_type;
4494}
4495
[email protected]277857a2014-06-03 10:38:014496bool WebContentsImpl::CanOverscrollContent() const {
[email protected]e85165c642014-06-10 14:34:314497 // Disable overscroll when touch emulation is on. See crbug.com/369938.
[email protected]34ff1cfc2014-08-20 06:16:054498 if (force_disable_overscroll_content_)
[email protected]e85165c642014-06-10 14:34:314499 return false;
4500
[email protected]37567b432014-02-12 01:12:224501 if (delegate_)
4502 return delegate_->CanOverscrollContent();
4503
4504 return false;
4505}
4506
nicka0ac8382016-12-15 23:59:234507void WebContentsImpl::OnThemeColorChanged(RenderFrameHostImpl* source,
Alan Cutter92d59252019-03-29 02:32:334508 base::Optional<SkColor> theme_color) {
nicka0ac8382016-12-15 23:59:234509 if (source != GetMainFrame()) {
4510 // Only the main frame may control the theme.
4511 return;
4512 }
4513
peter90afaba2015-06-01 12:05:294514 // Update the theme color. This is to be published to observers after the
4515 // first visually non-empty paint.
yusufod41c5f92015-03-06 00:14:284516 theme_color_ = theme_color;
peter90afaba2015-06-01 12:05:294517
4518 if (did_first_visually_non_empty_paint_ &&
4519 last_sent_theme_color_ != theme_color_) {
ericwilligersde386342016-10-19 02:35:094520 for (auto& observer : observers_)
4521 observer.DidChangeThemeColor(theme_color_);
peter90afaba2015-06-01 12:05:294522 last_sent_theme_color_ = theme_color_;
4523 }
[email protected]e7104762014-06-20 19:17:254524}
4525
[email protected]b172aee2012-04-10 17:05:264526void WebContentsImpl::OnDidLoadResourceFromMemoryCache(
nicka0ac8382016-12-15 23:59:234527 RenderFrameHostImpl* source,
[email protected]724159a2010-12-30 01:11:184528 const GURL& url,
[email protected]70435962011-08-02 20:13:284529 const std::string& http_method,
[email protected]6d6cfb3a2012-05-23 22:53:184530 const std::string& mime_type,
Maks Orlovich158ea092018-12-17 22:16:124531 const base::Optional<url::Origin>& top_frame_origin,
[email protected]6c1e05212014-07-31 00:59:404532 ResourceType resource_type) {
ericwilligersde386342016-10-19 02:35:094533 for (auto& observer : observers_)
4534 observer.DidLoadResourceFromMemoryCache(url, mime_type, resource_type);
[email protected]b0f724c2013-09-05 04:21:134535
Steven Bingler674fb0822018-12-12 18:04:524536 if (url.is_valid() && url.SchemeIsHTTPOrHTTPS()) {
nicka0ac8382016-12-15 23:59:234537 StoragePartition* partition = source->GetProcess()->GetStoragePartition();
Shivani Sharma8ae506c2019-07-21 21:08:274538 const url::Origin& last_committed_origin = source->GetLastCommittedOrigin();
Steven Bingler674fb0822018-12-12 18:04:524539
John Abd-El-Malekf4fe9d82019-07-31 04:05:594540 partition->GetNetworkContext()->NotifyExternalCacheHit(
4541 url, http_method, top_frame_origin, last_committed_origin);
[email protected]8bfc8272013-09-09 20:10:534542 }
[email protected]724159a2010-12-30 01:11:184543}
4544
nicka0ac8382016-12-15 23:59:234545void WebContentsImpl::OnDidDisplayInsecureContent(RenderFrameHostImpl* source) {
4546 // Any frame can trigger display of insecure content, so we don't check
4547 // |source| here.
carloskd9d97942017-02-16 08:58:094548 DidDisplayInsecureContent();
4549}
4550
4551void WebContentsImpl::DidDisplayInsecureContent() {
estarkc2273502016-10-12 22:03:024552 controller_.ssl_manager()->DidDisplayMixedContent();
[email protected]724159a2010-12-30 01:11:184553}
4554
elawrenceb2ac2a232017-03-27 21:46:254555void WebContentsImpl::OnDidContainInsecureFormAction(
4556 RenderFrameHostImpl* source) {
4557 controller_.ssl_manager()->DidContainInsecureFormAction();
4558}
4559
nicka0ac8382016-12-15 23:59:234560void WebContentsImpl::OnDidRunInsecureContent(RenderFrameHostImpl* source,
4561 const GURL& security_origin,
estark910b4572015-12-09 20:55:414562 const GURL& target_url) {
nicka0ac8382016-12-15 23:59:234563 // TODO(nick, estark): Should we call FilterURL using |source|'s process on
4564 // these parameters? |target_url| seems unused, except for a log message. And
4565 // |security_origin| might be replaceable with the origin of the main frame.
carloskd9d97942017-02-16 08:58:094566 DidRunInsecureContent(security_origin, target_url);
4567}
4568
4569void WebContentsImpl::DidRunInsecureContent(const GURL& security_origin,
4570 const GURL& target_url) {
[email protected]9450c462013-11-23 01:22:584571 LOG(WARNING) << security_origin << " ran insecure content from "
4572 << target_url.possibly_invalid_spec();
[email protected]e6e30ac2014-01-13 21:24:394573 RecordAction(base::UserMetricsAction("SSL.RanInsecureContent"));
estark910b4572015-12-09 20:55:414574 if (base::EndsWith(security_origin.spec(), kDotGoogleDotCom,
brettwa7ff1b292015-07-16 17:49:294575 base::CompareCase::INSENSITIVE_ASCII))
[email protected]e6e30ac2014-01-13 21:24:394576 RecordAction(base::UserMetricsAction("SSL.RanInsecureContentGoogle"));
estarkc2273502016-10-12 22:03:024577 controller_.ssl_manager()->DidRunMixedContent(security_origin);
[email protected]724159a2010-12-30 01:11:184578}
4579
carloskd9d97942017-02-16 08:58:094580void WebContentsImpl::PassiveInsecureContentFound(const GURL& resource_url) {
Sergey Kuznetsov3ebc01b2018-02-01 04:57:364581 if (delegate_) {
4582 delegate_->PassiveInsecureContentFound(resource_url);
4583 }
carloskd9d97942017-02-16 08:58:094584}
4585
4586bool WebContentsImpl::ShouldAllowRunningInsecureContent(
4587 WebContents* web_contents,
4588 bool allowed_per_prefs,
4589 const url::Origin& origin,
4590 const GURL& resource_url) {
Sergey Kuznetsov3ebc01b2018-02-01 04:57:364591 if (delegate_) {
4592 return delegate_->ShouldAllowRunningInsecureContent(
4593 web_contents, allowed_per_prefs, origin, resource_url);
4594 }
4595
4596 return allowed_per_prefs;
carloskd9d97942017-02-16 08:58:094597}
4598
Lukasz Anforowicze1b954d92017-10-30 21:28:064599void WebContentsImpl::ViewSource(RenderFrameHostImpl* frame) {
4600 DCHECK_EQ(this, WebContents::FromRenderFrameHost(frame));
4601
4602 // Don't do anything if there is no |delegate_| that could accept and show the
4603 // new WebContents containing the view-source.
4604 if (!delegate_)
4605 return;
4606
4607 // Use the last committed entry, since the pending entry hasn't loaded yet and
4608 // won't be copied into the cloned tab.
4609 NavigationEntryImpl* last_committed_entry =
4610 static_cast<NavigationEntryImpl*>(frame->frame_tree_node()
4611 ->navigator()
4612 ->GetController()
4613 ->GetLastCommittedEntry());
4614 if (!last_committed_entry)
4615 return;
4616
4617 FrameNavigationEntry* frame_entry =
4618 last_committed_entry->GetFrameEntry(frame->frame_tree_node());
4619 if (!frame_entry)
4620 return;
4621
4622 // Any new WebContents opened while this WebContents is in fullscreen can be
4623 // used to confuse the user, so drop fullscreen.
Avi Drissmand18c51972018-08-15 18:32:094624 ForSecurityDropFullscreen();
Lukasz Anforowicze1b954d92017-10-30 21:28:064625
4626 // We intentionally don't share the SiteInstance with the original frame so
4627 // that view source has a consistent process model and always ends up in a new
4628 // process (https://crbug.com/699493).
4629 scoped_refptr<SiteInstanceImpl> site_instance_for_view_source = nullptr;
Lukasz Anforowicz435bcb582019-07-12 20:50:064630 // Referrer and initiator are not important, because view-source should not
4631 // hit the network, but should be served from the cache instead.
Lukasz Anforowicze1b954d92017-10-30 21:28:064632 Referrer referrer_for_view_source;
Lukasz Anforowicz435bcb582019-07-12 20:50:064633 base::Optional<url::Origin> initiator_for_view_source = base::nullopt;
Lukasz Anforowicze1b954d92017-10-30 21:28:064634 // Do not restore title, derive it from the url.
4635 base::string16 title_for_view_source;
4636 auto navigation_entry = std::make_unique<NavigationEntryImpl>(
4637 site_instance_for_view_source, frame_entry->url(),
Lukasz Anforowicz435bcb582019-07-12 20:50:064638 referrer_for_view_source, initiator_for_view_source,
4639 title_for_view_source, ui::PAGE_TRANSITION_LINK,
Marijn Kruisselbrink7a0d5e182018-05-24 22:55:094640 /* is_renderer_initiated = */ false,
4641 /* blob_url_loader_factory = */ nullptr);
Lukasz Anforowicze1b954d92017-10-30 21:28:064642 navigation_entry->SetVirtualURL(GURL(content::kViewSourceScheme +
4643 std::string(":") +
4644 frame_entry->url().spec()));
4645
4646 // Do not restore scroller position.
4647 // TODO(creis, lukasza, arthursonzogni): Do not reuse the original PageState,
4648 // but start from a new one and only copy the needed data.
4649 const PageState& new_page_state =
4650 frame_entry->page_state().RemoveScrollOffset();
4651
4652 scoped_refptr<FrameNavigationEntry> new_frame_entry =
4653 navigation_entry->root_node()->frame_entry;
4654 new_frame_entry->set_method(frame_entry->method());
4655 new_frame_entry->SetPageState(new_page_state);
4656
4657 // Create a new WebContents, which is used to display the source code.
erikchenbee5c9622018-04-27 19:30:254658 std::unique_ptr<WebContents> view_source_contents =
Erik Chenbb8e738e2018-04-28 14:10:434659 Create(CreateParams(GetBrowserContext()));
Lukasz Anforowicze1b954d92017-10-30 21:28:064660
4661 // Restore the previously created NavigationEntry.
4662 std::vector<std::unique_ptr<NavigationEntry>> navigation_entries;
4663 navigation_entries.push_back(std::move(navigation_entry));
4664 view_source_contents->GetController().Restore(0, RestoreType::CURRENT_SESSION,
4665 &navigation_entries);
4666
4667 // Add |view_source_contents| as a new tab.
4668 gfx::Rect initial_rect;
4669 constexpr bool kUserGesture = true;
4670 bool ignored_was_blocked;
erikchenbee5c9622018-04-27 19:30:254671 delegate_->AddNewContents(this, std::move(view_source_contents),
Lukasz Anforowicze1b954d92017-10-30 21:28:064672 WindowOpenDisposition::NEW_FOREGROUND_TAB,
4673 initial_rect, kUserGesture, &ignored_was_blocked);
4674 // Note that the |delegate_| could have deleted |view_source_contents| during
4675 // AddNewContents method call.
4676}
4677
Jay Civellid5904f02018-03-23 19:43:574678void WebContentsImpl::SubresourceResponseStarted(const GURL& url,
4679 net::CertStatus cert_status) {
4680 controller_.ssl_manager()->DidStartResourceResponse(url, cert_status);
4681 SetNotWaitingForResponse();
4682}
4683
Jay Civelli116683f2018-03-27 19:56:234684void WebContentsImpl::ResourceLoadComplete(
Jay Civelli52a63302018-04-30 22:39:554685 RenderFrameHost* render_frame_host,
Clark DuVall36164bd2018-08-09 22:49:084686 const GlobalRequestID& request_id,
Jay Civelli116683f2018-03-27 19:56:234687 mojom::ResourceLoadInfoPtr resource_load_info) {
John Abd-El-Malek9b8f7fa2017-12-04 23:52:254688 for (auto& observer : observers_) {
Clark DuVall36164bd2018-08-09 22:49:084689 observer.ResourceLoadComplete(render_frame_host, request_id,
4690 *resource_load_info);
John Abd-El-Malek9b8f7fa2017-12-04 23:52:254691 }
John Abd-El-Malekd4882642017-12-04 21:45:194692}
4693
Wei Li5bb659742018-02-14 03:07:584694void WebContentsImpl::PrintCrossProcessSubframe(
4695 const gfx::Rect& rect,
4696 int document_cookie,
4697 RenderFrameHost* subframe_host) {
Wei Li5c0b4d12018-03-22 15:05:224698 auto* outer_contents = GetOuterWebContents();
4699 if (outer_contents) {
4700 // When an extension or app page is printed, the content should be
4701 // composited with outer content, so the outer contents should handle the
4702 // print request.
4703 outer_contents->PrintCrossProcessSubframe(rect, document_cookie,
4704 subframe_host);
4705 return;
4706 }
4707
Wei Li5bb659742018-02-14 03:07:584708 // If there is no delegate such as in tests or during deletion, do nothing.
4709 if (!delegate_)
4710 return;
4711
4712 delegate_->PrintCrossProcessSubframe(this, rect, document_cookie,
4713 subframe_host);
4714}
4715
rob.buis300b0872017-03-10 20:43:584716#if defined(OS_ANDROID)
4717base::android::ScopedJavaLocalRef<jobject>
4718WebContentsImpl::GetJavaRenderFrameHostDelegate() {
4719 return GetJavaWebContents();
4720}
4721#endif
4722
estark910b4572015-12-09 20:55:414723void WebContentsImpl::OnDidDisplayContentWithCertificateErrors(
Zhuoyu Qian4353f7f2018-01-08 05:33:004724 RenderFrameHostImpl* source) {
estarkc2273502016-10-12 22:03:024725 controller_.ssl_manager()->DidDisplayContentWithCertErrors();
estark910b4572015-12-09 20:55:414726}
4727
4728void WebContentsImpl::OnDidRunContentWithCertificateErrors(
Zhuoyu Qian4353f7f2018-01-08 05:33:004729 RenderFrameHostImpl* source) {
nicka0ac8382016-12-15 23:59:234730 // TODO(nick, estark): Do we need to consider |source| here somehow?
estarkcd2e30c2016-08-12 06:51:154731 NavigationEntry* entry = controller_.GetVisibleEntry();
4732 if (!entry)
estarkaeda1422016-05-10 02:31:124733 return;
4734
estarkaeda1422016-05-10 02:31:124735 // TODO(estark): check that this does something reasonable for
4736 // about:blank and sandboxed origins. https://crbug.com/609527
estarkcd2e30c2016-08-12 06:51:154737 controller_.ssl_manager()->DidRunContentWithCertErrors(
4738 entry->GetURL().GetOrigin());
estark910b4572015-12-09 20:55:414739}
4740
nicka0ac8382016-12-15 23:59:234741void WebContentsImpl::OnDocumentLoadedInFrame(RenderFrameHostImpl* source) {
ericwilligersde386342016-10-19 02:35:094742 for (auto& observer : observers_)
nicka0ac8382016-12-15 23:59:234743 observer.DocumentLoadedInFrame(source);
[email protected]724159a2010-12-30 01:11:184744}
4745
nicka0ac8382016-12-15 23:59:234746void WebContentsImpl::OnDidFinishLoad(RenderFrameHostImpl* source,
4747 const GURL& url) {
[email protected]8b3af1e2014-01-24 13:29:124748 GURL validated_url(url);
nicka0ac8382016-12-15 23:59:234749 source->GetProcess()->FilterURL(false, &validated_url);
[email protected]028053d42014-03-05 22:20:374750
ericwilligersde386342016-10-19 02:35:094751 for (auto& observer : observers_)
nicka0ac8382016-12-15 23:59:234752 observer.DidFinishLoad(source, validated_url);
[email protected]1a55c5be2011-11-29 11:36:314753}
4754
Dave Tapuska549baab2019-03-19 15:52:404755void WebContentsImpl::OnGoToEntryAtOffset(RenderFrameHostImpl* source,
Arthur Sonzognia7d715a2018-09-20 16:11:134756 int offset,
4757 bool has_user_gesture) {
4758 // Non-user initiated navigations coming from the renderer should be ignored
4759 // if there is an ongoing browser-initiated navigation.
4760 // See https://crbug.com/879965.
4761 // TODO(arthursonzogni): See if this should check for ongoing navigations in
4762 // the frame(s) affected by the session history navigation, rather than just
4763 // the main frame.
4764 if (!has_user_gesture) {
4765 NavigationRequest* ongoing_navigation_request =
4766 frame_tree_.root()->navigation_request();
4767 if (ongoing_navigation_request &&
4768 ongoing_navigation_request->browser_initiated()) {
4769 return;
4770 }
4771 }
4772
nicka0ac8382016-12-15 23:59:234773 // All frames are allowed to navigate the global history.
Dave Tapuska8bfd84c2019-03-26 20:47:164774 if (!delegate_ || delegate_->OnGoToEntryOffset(offset)) {
4775 if (source->IsSandboxed(blink::WebSandboxFlags::kTopNavigation)) {
4776 // Keep track of whether this is a session history from a sandboxed iframe
4777 // with top level navigation disallowed.
4778 controller_.GoToOffsetInSandboxedFrame(offset,
4779 source->GetFrameTreeNodeId());
4780 } else {
4781 controller_.GoToOffset(offset);
4782 }
4783 }
[email protected]216813952011-05-19 22:21:264784}
4785
nicka0ac8382016-12-15 23:59:234786void WebContentsImpl::OnUpdateZoomLimits(RenderViewHostImpl* source,
4787 int minimum_percent,
[email protected]fce823222014-05-30 16:24:304788 int maximum_percent) {
[email protected]216813952011-05-19 22:21:264789 minimum_zoom_percent_ = minimum_percent;
4790 maximum_zoom_percent_ = maximum_percent;
[email protected]216813952011-05-19 22:21:264791}
4792
nicka0ac8382016-12-15 23:59:234793void WebContentsImpl::OnPageScaleFactorChanged(RenderViewHostImpl* source,
4794 float page_scale_factor) {
mcnee336ea2c2017-05-23 22:50:594795#if !defined(OS_ANDROID)
4796 // While page scale factor is used on mobile, this PageScaleFactorIsOne logic
4797 // is only needed on desktop.
mcnee432e47d2015-11-09 19:37:464798 bool is_one = page_scale_factor == 1.f;
4799 if (is_one != page_scale_factor_is_one_) {
4800 page_scale_factor_is_one_ = is_one;
4801
4802 HostZoomMapImpl* host_zoom_map =
4803 static_cast<HostZoomMapImpl*>(HostZoomMap::GetForWebContents(this));
4804
nicka0ac8382016-12-15 23:59:234805 if (host_zoom_map) {
mcnee432e47d2015-11-09 19:37:464806 host_zoom_map->SetPageScaleFactorIsOneForView(
nicka0ac8382016-12-15 23:59:234807 source->GetProcess()->GetID(), source->GetRoutingID(),
mcnee432e47d2015-11-09 19:37:464808 page_scale_factor_is_one_);
4809 }
4810 }
mcnee336ea2c2017-05-23 22:50:594811#endif // !defined(OS_ANDROID)
mcnee432e47d2015-11-09 19:37:464812
ericwilligersde386342016-10-19 02:35:094813 for (auto& observer : observers_)
4814 observer.OnPageScaleFactorChanged(page_scale_factor);
mcnee432e47d2015-11-09 19:37:464815}
4816
W. James MacLean62198672019-06-04 16:46:104817void WebContentsImpl::OnTextAutosizerPageInfoChanged(
4818 RenderViewHostImpl* source,
4819 const blink::WebTextAutosizerPageInfo& page_info) {
4820 // Keep a copy of |page_info| in case we create a new RenderView before
4821 // the next update.
4822 text_autosizer_page_info_ = page_info;
4823 frame_tree_.root()->render_manager()->SendPageMessage(
4824 new PageMsg_UpdateTextAutosizerPageInfoForRemoteMainFrames(
4825 MSG_ROUTING_NONE, page_info),
4826 source->GetSiteInstance());
4827}
4828
Kent Tamura8c9e0562018-11-06 07:02:194829void WebContentsImpl::EnumerateDirectory(
4830 RenderFrameHost* render_frame_host,
4831 std::unique_ptr<FileSelectListener> listener,
4832 const base::FilePath& directory_path) {
4833 if (delegate_)
4834 delegate_->EnumerateDirectory(this, std::move(listener), directory_path);
4835 else
4836 listener->FileSelectionCanceled();
[email protected]3a29a6e2011-08-24 18:26:214837}
4838
nicka0ac8382016-12-15 23:59:234839void WebContentsImpl::OnRegisterProtocolHandler(RenderFrameHostImpl* source,
4840 const std::string& protocol,
[email protected]b172aee2012-04-10 17:05:264841 const GURL& url,
[email protected]fcf75d42013-12-03 20:11:264842 const base::string16& title,
[email protected]3a3b75a2012-06-01 08:38:364843 bool user_gesture) {
nicka0ac8382016-12-15 23:59:234844 // TODO(nick): Should we consider |source| here or pass it to the delegate?
4845 // TODO(nick): Do we need to apply FilterURL to |url|?
[email protected]e5f2de02012-07-20 22:15:434846 if (!delegate_)
4847 return;
4848
Ben Wellsf0180032019-07-15 05:59:094849 ChildProcessSecurityPolicyImpl* policy =
4850 ChildProcessSecurityPolicyImpl::GetInstance();
4851 if (policy->IsPseudoScheme(protocol))
[email protected]b9535422012-02-09 01:47:594852 return;
[email protected]8f810632013-06-06 22:33:324853
[email protected]c125e532014-05-10 20:39:064854 delegate_->RegisterProtocolHandler(this, protocol, url, user_gesture);
[email protected]7d189022011-08-25 22:54:204855}
4856
nicka0ac8382016-12-15 23:59:234857void WebContentsImpl::OnUnregisterProtocolHandler(RenderFrameHostImpl* source,
4858 const std::string& protocol,
[email protected]f5273e52014-07-14 16:30:204859 const GURL& url,
4860 bool user_gesture) {
nicka0ac8382016-12-15 23:59:234861 // TODO(nick): Should we consider |source| here or pass it to the delegate?
4862 // TODO(nick): Do we need to apply FilterURL to |url|?
[email protected]f5273e52014-07-14 16:30:204863 if (!delegate_)
4864 return;
4865
Ben Wellsf0180032019-07-15 05:59:094866 ChildProcessSecurityPolicyImpl* policy =
4867 ChildProcessSecurityPolicyImpl::GetInstance();
4868 if (policy->IsPseudoScheme(protocol))
[email protected]f5273e52014-07-14 16:30:204869 return;
[email protected]f5273e52014-07-14 16:30:204870
4871 delegate_->UnregisterProtocolHandler(this, protocol, url, user_gesture);
4872}
4873
kouhei40f03cb2015-09-24 07:47:014874void WebContentsImpl::OnUpdatePageImportanceSignals(
nicka0ac8382016-12-15 23:59:234875 RenderFrameHostImpl* source,
kouhei40f03cb2015-09-24 07:47:014876 const PageImportanceSignals& signals) {
nicka0ac8382016-12-15 23:59:234877 // TODO(nick, kouhei): Fix this for oopifs; currently all frames' state gets
4878 // written to this one field.
kouhei40f03cb2015-09-24 07:47:014879 page_importance_signals_ = signals;
4880}
4881
nicka0ac8382016-12-15 23:59:234882void WebContentsImpl::OnDomOperationResponse(RenderFrameHostImpl* source,
4883 const std::string& json_string) {
4884 // TODO(nick, lukasza): The notification below should probably be updated to
4885 // include |source|.
avi60bd4902015-09-23 20:39:244886 std::string json = json_string;
4887 NotificationService::current()->Notify(NOTIFICATION_DOM_OPERATION_RESPONSE,
4888 Source<WebContents>(this),
4889 Details<std::string>(&json));
[email protected]cfa856d62014-02-22 07:58:404890}
4891
Marijn Kruisselbrink7f519672019-02-05 19:12:504892void WebContentsImpl::OnAppCacheAccessed(const GURL& manifest_url,
[email protected]b172aee2012-04-10 17:05:264893 bool blocked_by_policy) {
nicka0ac8382016-12-15 23:59:234894 // TODO(nick): Should we consider |source| here? Should we call FilterURL on
4895 // |manifest_url|?
4896
[email protected]7fc4bbb2011-09-08 21:23:104897 // Notify observers about navigation.
ericwilligersde386342016-10-19 02:35:094898 for (auto& observer : observers_)
4899 observer.AppCacheAccessed(manifest_url, blocked_by_policy);
[email protected]7fc4bbb2011-09-08 21:23:104900}
4901
Joel Hockey85b379d2017-12-11 10:42:134902void WebContentsImpl::OnColorChooserFactoryRequest(
Joel Hockey163835a2017-12-20 11:51:574903 blink::mojom::ColorChooserFactoryRequest request) {
Joel Hockey85b379d2017-12-11 10:42:134904 color_chooser_factory_bindings_.AddBinding(this, std::move(request));
4905}
4906
4907void WebContentsImpl::OpenColorChooser(
Joel Hockey163835a2017-12-20 11:51:574908 blink::mojom::ColorChooserRequest chooser_request,
4909 blink::mojom::ColorChooserClientPtr client,
[email protected]8ed16472014-04-11 19:02:484910 SkColor color,
Joel Hockey163835a2017-12-20 11:51:574911 std::vector<blink::mojom::ColorSuggestionPtr> suggestions) {
Joel Hockey85b379d2017-12-11 10:42:134912 content::ColorChooser* new_color_chooser =
Ivan Kotenkov2c0d2bb32017-11-01 15:41:284913 delegate_ ? delegate_->OpenColorChooser(this, color, suggestions)
4914 : nullptr;
[email protected]820957a2014-01-14 14:56:304915 if (!new_color_chooser)
[email protected]cb805452013-05-22 15:16:214916 return;
[email protected]8ed16472014-04-11 19:02:484917
Joel Hockey85b379d2017-12-11 10:42:134918 color_chooser_.reset();
4919 color_chooser_ = std::make_unique<ColorChooser>(
4920 new_color_chooser, std::move(chooser_request), std::move(client));
[email protected]da8543762012-03-20 08:52:204921}
4922
brettw4b461082016-11-19 18:55:164923#if BUILDFLAG(ENABLE_PLUGINS)
nicka0ac8382016-12-15 23:59:234924void WebContentsImpl::OnPepperInstanceCreated(RenderFrameHostImpl* source,
4925 int32_t pp_instance) {
ericwilligersde386342016-10-19 02:35:094926 for (auto& observer : observers_)
4927 observer.PepperInstanceCreated();
nicka0ac8382016-12-15 23:59:234928 pepper_playback_observer_->PepperInstanceCreated(source, pp_instance);
emaxxe70f5e12015-05-29 11:26:004929}
4930
nicka0ac8382016-12-15 23:59:234931void WebContentsImpl::OnPepperInstanceDeleted(RenderFrameHostImpl* source,
4932 int32_t pp_instance) {
ericwilligersde386342016-10-19 02:35:094933 for (auto& observer : observers_)
4934 observer.PepperInstanceDeleted();
nicka0ac8382016-12-15 23:59:234935 pepper_playback_observer_->PepperInstanceDeleted(source, pp_instance);
emaxxe70f5e12015-05-29 11:26:004936}
4937
nicka0ac8382016-12-15 23:59:234938void WebContentsImpl::OnPepperPluginHung(RenderFrameHostImpl* source,
4939 int plugin_child_id,
tommyclie6633ca72014-10-31 00:40:424940 const base::FilePath& path,
4941 bool is_hung) {
Steven Holte95922222018-09-14 20:06:234942 UMA_HISTOGRAM_COUNTS_1M("Pepper.PluginHung", 1);
tommyclie6633ca72014-10-31 00:40:424943
ericwilligersde386342016-10-19 02:35:094944 for (auto& observer : observers_)
4945 observer.PluginHungStatusChanged(plugin_child_id, path, is_hung);
tommyclie6633ca72014-10-31 00:40:424946}
4947
nicka0ac8382016-12-15 23:59:234948void WebContentsImpl::OnPepperStartsPlayback(RenderFrameHostImpl* source,
4949 int32_t pp_instance) {
4950 pepper_playback_observer_->PepperStartsPlayback(source, pp_instance);
zqzhang181047e62016-07-01 13:37:174951}
4952
nicka0ac8382016-12-15 23:59:234953void WebContentsImpl::OnPepperStopsPlayback(RenderFrameHostImpl* source,
4954 int32_t pp_instance) {
4955 pepper_playback_observer_->PepperStopsPlayback(source, pp_instance);
zqzhang181047e62016-07-01 13:37:174956}
4957
nicka0ac8382016-12-15 23:59:234958void WebContentsImpl::OnPluginCrashed(RenderFrameHostImpl* source,
4959 const base::FilePath& plugin_path,
tommyclie6633ca72014-10-31 00:40:424960 base::ProcessId plugin_pid) {
nicka0ac8382016-12-15 23:59:234961 // TODO(nick): Eliminate the |plugin_pid| parameter, which can't be trusted,
4962 // and is only used by BlinkTestController.
ericwilligersde386342016-10-19 02:35:094963 for (auto& observer : observers_)
4964 observer.PluginCrashed(plugin_path, plugin_pid);
tommyclie6633ca72014-10-31 00:40:424965}
4966
[email protected]d8415ad92012-08-23 14:40:504967void WebContentsImpl::OnRequestPpapiBrokerPermission(
nicka0ac8382016-12-15 23:59:234968 RenderViewHostImpl* source,
4969 int ppb_broker_route_id,
[email protected]d8415ad92012-08-23 14:40:504970 const GURL& url,
[email protected]2dec8ec2013-02-07 19:20:344971 const base::FilePath& plugin_path) {
Avi Drissman0a2a967d2019-04-01 19:33:524972 base::OnceCallback<void(bool)> permission_result_callback = base::BindOnce(
nicka0ac8382016-12-15 23:59:234973 &WebContentsImpl::SendPpapiBrokerPermissionResult, base::Unretained(this),
4974 source->GetProcess()->GetID(), ppb_broker_route_id);
[email protected]8c8fc292012-11-23 18:57:164975 if (!delegate_) {
Tommy Nyquist4b749d02018-03-20 21:46:294976 std::move(permission_result_callback).Run(false);
[email protected]8c8fc292012-11-23 18:57:164977 return;
[email protected]d8415ad92012-08-23 14:40:504978 }
4979
Avi Drissman0a2a967d2019-04-01 19:33:524980 delegate_->RequestPpapiBrokerPermission(
4981 this, url, plugin_path, std::move(permission_result_callback));
[email protected]d8415ad92012-08-23 14:40:504982}
4983
nicka0ac8382016-12-15 23:59:234984void WebContentsImpl::SendPpapiBrokerPermissionResult(int process_id,
4985 int ppb_broker_route_id,
4986 bool result) {
4987 RenderProcessHost* rph = RenderProcessHost::FromID(process_id);
4988 if (rph) {
4989 // TODO(nick): Convert this from ViewMsg_ to a Ppapi msg, since it
4990 // is not routed to a RenderView.
4991 rph->Send(
4992 new ViewMsg_PpapiBrokerPermissionResult(ppb_broker_route_id, result));
4993 }
[email protected]d8415ad92012-08-23 14:40:504994}
4995
fsamuel833ee7c2015-02-13 23:40:404996void WebContentsImpl::OnBrowserPluginMessage(RenderFrameHost* render_frame_host,
4997 const IPC::Message& message) {
Lei Zhang6410ec1a2019-01-31 21:38:584998 CHECK(!browser_plugin_embedder_);
fsamuel60b42282015-03-10 03:29:144999 CreateBrowserPluginEmbedderIfNecessary();
fsamuel833ee7c2015-02-13 23:40:405000 browser_plugin_embedder_->OnMessageReceived(message, render_frame_host);
[email protected]19be7a62012-10-01 23:03:375001}
brettw4b461082016-11-19 18:55:165002#endif // BUILDFLAG(ENABLE_PLUGINS)
[email protected]19be7a62012-10-01 23:03:375003
[email protected]795c28972012-12-06 06:13:395004void WebContentsImpl::OnUpdateFaviconURL(
lukasza178ff092017-06-07 23:39:505005 RenderFrameHostImpl* source,
[email protected]795c28972012-12-06 06:13:395006 const std::vector<FaviconURL>& candidates) {
lukasza178ff092017-06-07 23:39:505007 // Ignore favicons for non-main frame.
5008 if (source->GetParent()) {
5009 NOTREACHED();
5010 return;
5011 }
5012
pkotwiczf9bc20592015-04-21 18:57:065013 // We get updated favicon URLs after the page stops loading. If a cross-site
5014 // navigation occurs while a page is still loading, the initial page
5015 // may stop loading and send us updated favicon URLs after the navigation
5016 // for the new page has committed.
lukasza178ff092017-06-07 23:39:505017 if (!source->IsCurrent())
pkotwiczf9bc20592015-04-21 18:57:065018 return;
5019
ericwilligersde386342016-10-19 02:35:095020 for (auto& observer : observers_)
5021 observer.DidUpdateFaviconURL(candidates);
[email protected]795c28972012-12-06 06:13:395022}
5023
qinmin72e8bd02016-10-21 19:35:375024void WebContentsImpl::SetIsOverlayContent(bool is_overlay_content) {
5025 is_overlay_content_ = is_overlay_content;
5026}
5027
Albert J. Wongfb64e142018-10-16 01:10:455028void WebContentsImpl::DidFirstVisuallyNonEmptyPaint(
5029 RenderViewHostImpl* source) {
Francois Dorayae33f2f2018-10-30 13:40:045030 // Set |did_first_visually_non_empty_paint_| before notifying observers so
5031 // they can see that CompletedFirstVisuallyNonEmptyPaint() is true.
5032 did_first_visually_non_empty_paint_ = true;
5033
nicka0ac8382016-12-15 23:59:235034 // TODO(nick): When this is ported to FrameHostMsg_, we should only listen if
5035 // |source| is the main frame.
ericwilligersde386342016-10-19 02:35:095036 for (auto& observer : observers_)
5037 observer.DidFirstVisuallyNonEmptyPaint();
yusufod41c5f92015-03-06 00:14:285038
5039 if (theme_color_ != last_sent_theme_color_) {
5040 // Theme color should have updated by now if there was one.
ericwilligersde386342016-10-19 02:35:095041 for (auto& observer : observers_)
5042 observer.DidChangeThemeColor(theme_color_);
yusufod41c5f92015-03-06 00:14:285043 last_sent_theme_color_ = theme_color_;
5044 }
[email protected]9f268072013-11-07 00:02:155045}
[email protected]d9030b82013-07-19 08:26:065046
Albert J. Wongfb64e142018-10-16 01:10:455047void WebContentsImpl::DidCommitAndDrawCompositorFrame(
Yiming Zhoudd60b0b2018-07-20 18:51:355048 RenderViewHostImpl* source) {
5049 for (auto& observer : observers_)
5050 observer.DidCommitAndDrawCompositorFrame();
5051}
5052
Adithya Srinivasan46b8a792019-02-01 14:47:235053bool WebContentsImpl::IsPortal() const {
5054 return portal();
5055}
5056
[email protected]17e286e2013-03-01 23:29:395057void WebContentsImpl::NotifyBeforeFormRepostWarningShow() {
ericwilligersde386342016-10-19 02:35:095058 for (auto& observer : observers_)
5059 observer.BeforeFormRepostWarningShow();
[email protected]17e286e2013-03-01 23:29:395060}
5061
[email protected]ec6c05f2013-10-23 18:41:575062void WebContentsImpl::ActivateAndShowRepostFormWarningDialog() {
5063 Activate();
5064 if (delegate_)
5065 delegate_->ShowRepostFormWarningDialog(this);
5066}
5067
[email protected]b4c84012014-04-28 19:51:105068bool WebContentsImpl::HasAccessedInitialDocument() {
5069 return has_accessed_initial_document_;
5070}
5071
afakhry6f0c1ec22016-07-14 13:55:135072void WebContentsImpl::UpdateTitleForEntry(NavigationEntry* entry,
[email protected]fcf75d42013-12-03 20:11:265073 const base::string16& title) {
[email protected]fcf75d42013-12-03 20:11:265074 base::string16 final_title;
Avi Drissman93002212017-09-27 03:20:525075 base::TrimWhitespace(title, base::TRIM_ALL, &final_title);
[email protected]420ae012009-04-24 05:16:325076
[email protected]987fc3a2011-05-26 14:18:095077 // If a page is created via window.open and never navigated,
5078 // there will be no navigation entry. In this situation,
[email protected]73eb2602012-02-09 19:50:555079 // |page_title_when_no_navigation_entry_| will be used for page title.
[email protected]987fc3a2011-05-26 14:18:095080 if (entry) {
[email protected]36fc0392011-12-25 03:59:515081 if (final_title == entry->GetTitle())
afakhry6f0c1ec22016-07-14 13:55:135082 return; // Nothing changed, don't bother.
[email protected]420ae012009-04-24 05:16:325083
[email protected]36fc0392011-12-25 03:59:515084 entry->SetTitle(final_title);
Avi Drissman93002212017-09-27 03:20:525085
5086 // The title for display may differ from the title just set; grab it.
5087 final_title = entry->GetTitleForDisplay();
[email protected]987fc3a2011-05-26 14:18:095088 } else {
5089 if (page_title_when_no_navigation_entry_ == final_title)
afakhry6f0c1ec22016-07-14 13:55:135090 return; // Nothing changed, don't bother.
[email protected]987fc3a2011-05-26 14:18:095091
5092 page_title_when_no_navigation_entry_ = final_title;
5093 }
[email protected]420ae012009-04-24 05:16:325094
[email protected]420ae012009-04-24 05:16:325095 // Lastly, set the title for the view.
[email protected]790e2fd2011-09-21 20:28:235096 view_->SetPageTitle(final_title);
[email protected]420ae012009-04-24 05:16:325097
ericwilligersde386342016-10-19 02:35:095098 for (auto& observer : observers_)
Avi Drissman93002212017-09-27 03:20:525099 observer.TitleWasSet(entry);
[email protected]66798902013-10-01 18:40:165100
afakhry6f0c1ec22016-07-14 13:55:135101 // Broadcast notifications when the UI should be updated.
5102 if (entry == controller_.GetEntryAtOffset(0))
5103 NotifyNavigationStateChanged(INVALIDATE_TYPE_TITLE);
[email protected]420ae012009-04-24 05:16:325104}
5105
fdegansa696e5112015-04-17 01:57:595106void WebContentsImpl::SendChangeLoadProgress() {
[email protected]960b0372014-05-19 18:01:005107 loading_last_progress_update_ = base::TimeTicks::Now();
[email protected]960b0372014-05-19 18:01:005108 if (delegate_)
fdegansa696e5112015-04-17 01:57:595109 delegate_->LoadProgressChanged(this, frame_tree_.load_progress());
[email protected]960b0372014-05-19 18:01:005110}
5111
5112void WebContentsImpl::ResetLoadProgressState() {
fdegans1d16355162015-03-26 11:58:345113 frame_tree_.ResetLoadProgress();
[email protected]960b0372014-05-19 18:01:005114 loading_weak_factory_.InvalidateWeakPtrs();
5115 loading_last_progress_update_ = base::TimeTicks();
5116}
5117
clamy44e84ce2016-02-22 15:38:255118// Notifies the RenderWidgetHost instance about the fact that the page is
5119// loading, or done loading.
5120void WebContentsImpl::LoadingStateChanged(bool to_different_document,
5121 bool due_to_interstitial,
5122 LoadNotificationDetails* details) {
Nate Chapin22ea6592019-03-05 22:29:025123 if (is_being_destroyed_)
5124 return;
clamy44e84ce2016-02-22 15:38:255125 // Do not send notifications about loading changes in the FrameTree while the
5126 // interstitial page is pausing the throbber.
creisadca9862017-06-16 19:12:115127 if (ShowingInterstitialPage() && interstitial_page_->pause_throbber() &&
clamy44e84ce2016-02-22 15:38:255128 !due_to_interstitial) {
5129 return;
5130 }
5131
5132 bool is_loading = IsLoading();
5133
5134 if (!is_loading) {
5135 load_state_ = net::LoadStateWithParam(net::LOAD_STATE_IDLE,
5136 base::string16());
5137 load_state_host_.clear();
5138 upload_size_ = 0;
5139 upload_position_ = 0;
5140 }
5141
5142 GetRenderManager()->SetIsLoading(is_loading);
5143
5144 waiting_for_response_ = is_loading;
5145 is_load_to_different_document_ = to_different_document;
5146
5147 if (delegate_)
5148 delegate_->LoadingStateChanged(this, to_different_document);
5149 NotifyNavigationStateChanged(INVALIDATE_TYPE_LOAD);
5150
5151 std::string url = (details ? details->url.possibly_invalid_spec() : "NULL");
5152 if (is_loading) {
5153 TRACE_EVENT_ASYNC_BEGIN2("browser,navigation", "WebContentsImpl Loading",
5154 this, "URL", url, "Main FrameTreeNode id",
5155 GetFrameTree()->root()->frame_tree_node_id());
ericwilligersde386342016-10-19 02:35:095156 for (auto& observer : observers_)
5157 observer.DidStartLoading();
clamy44e84ce2016-02-22 15:38:255158 } else {
5159 TRACE_EVENT_ASYNC_END1("browser,navigation", "WebContentsImpl Loading",
5160 this, "URL", url);
ericwilligersde386342016-10-19 02:35:095161 for (auto& observer : observers_)
5162 observer.DidStopLoading();
clamy44e84ce2016-02-22 15:38:255163 }
5164
5165 // TODO(avi): Remove. http://crbug.com/170921
5166 int type = is_loading ? NOTIFICATION_LOAD_START : NOTIFICATION_LOAD_STOP;
5167 NotificationDetails det = NotificationService::NoDetails();
5168 if (details)
5169 det = Details<LoadNotificationDetails>(details);
5170 NotificationService::current()->Notify(
5171 type, Source<NavigationController>(&controller_), det);
5172}
5173
[email protected]02d7b6e2014-06-24 21:01:505174void WebContentsImpl::NotifyViewSwapped(RenderViewHost* old_host,
5175 RenderViewHost* new_host) {
[email protected]420ae012009-04-24 05:16:325176 // After sending out a swap notification, we need to send a disconnect
5177 // notification so that clients that pick up a pointer to |this| can NULL the
5178 // pointer. See Bug 1230284.
5179 notify_disconnection_ = true;
ericwilligersde386342016-10-19 02:35:095180 for (auto& observer : observers_)
5181 observer.RenderViewHostChanged(old_host, new_host);
[email protected]da7a7182013-09-06 08:11:115182
Jinsuk Kimdc5284c22018-07-11 02:22:065183 view_->RenderViewHostChanged(old_host, new_host);
5184
Lucas Furukawa Gadanif9ea4fc2018-08-03 23:57:135185 // If this is an inner WebContents that has swapped views, we need to reattach
5186 // it to its outer WebContents.
5187 if (node_.outer_web_contents())
5188 ReattachToOuterWebContentsFrame();
5189
[email protected]7a846df2012-09-20 19:17:395190 // Ensure that the associated embedder gets cleared after a RenderViewHost
5191 // gets swapped, so we don't reuse the same embedder next time a
5192 // RenderViewHost is attached to this WebContents.
5193 RemoveBrowserPluginEmbedder();
[email protected]420ae012009-04-24 05:16:325194}
5195
[email protected]02d7b6e2014-06-24 21:01:505196void WebContentsImpl::NotifyFrameSwapped(RenderFrameHost* old_host,
Jinsuk Kim895b2c52018-07-03 06:49:165197 RenderFrameHost* new_host,
5198 bool is_main_frame) {
Bo Liu168c8642017-08-28 18:26:025199#if defined(OS_ANDROID)
Bo Liu16dd7b8322018-05-02 21:19:505200 // Copy importance from |old_host| if |new_host| is a main frame.
5201 if (old_host && !new_host->GetParent()) {
Bo Liu7c6779e92017-08-16 02:02:285202 static_cast<RenderFrameHostImpl*>(new_host)
5203 ->GetRenderWidgetHost()
Bo Liu16dd7b8322018-05-02 21:19:505204 ->SetImportance(static_cast<RenderFrameHostImpl*>(old_host)
5205 ->GetRenderWidgetHost()
5206 ->importance());
Bo Liu7c6779e92017-08-16 02:02:285207 }
Bo Liu168c8642017-08-28 18:26:025208#endif
ericwilligersde386342016-10-19 02:35:095209 for (auto& observer : observers_)
5210 observer.RenderFrameHostChanged(old_host, new_host);
[email protected]02d7b6e2014-06-24 21:01:505211}
5212
[email protected]da7a7182013-09-06 08:11:115213// TODO(avi): Remove this entire function because this notification is already
5214// covered by two observer functions. http://crbug.com/170921
[email protected]b172aee2012-04-10 17:05:265215void WebContentsImpl::NotifyDisconnected() {
[email protected]420ae012009-04-24 05:16:325216 if (!notify_disconnection_)
5217 return;
5218
5219 notify_disconnection_ = false;
[email protected]8ff00d72012-10-23 19:12:215220 NotificationService::current()->Notify(
5221 NOTIFICATION_WEB_CONTENTS_DISCONNECTED,
5222 Source<WebContents>(this),
5223 NotificationService::NoDetails());
[email protected]420ae012009-04-24 05:16:325224}
5225
[email protected]cbb1ef592013-06-05 19:49:465226void WebContentsImpl::NotifyNavigationEntryCommitted(
5227 const LoadCommittedDetails& load_details) {
ericwilligersde386342016-10-19 02:35:095228 for (auto& observer : observers_)
5229 observer.NavigationEntryCommitted(load_details);
[email protected]cbb1ef592013-06-05 19:49:465230}
5231
Sam McNally5c087a32017-08-25 01:46:145232void WebContentsImpl::NotifyNavigationEntryChanged(
5233 const EntryChangedDetails& change_details) {
5234 for (auto& observer : observers_)
5235 observer.NavigationEntryChanged(change_details);
5236}
5237
5238void WebContentsImpl::NotifyNavigationListPruned(
5239 const PrunedDetails& pruned_details) {
5240 for (auto& observer : observers_)
5241 observer.NavigationListPruned(pruned_details);
5242}
5243
Christian Dullweber1af31e62018-02-22 11:49:485244void WebContentsImpl::NotifyNavigationEntriesDeleted() {
5245 for (auto& observer : observers_)
5246 observer.NavigationEntriesDeleted();
5247}
5248
rockotf62002a2016-09-15 00:08:595249void WebContentsImpl::OnAssociatedInterfaceRequest(
5250 RenderFrameHost* render_frame_host,
5251 const std::string& interface_name,
5252 mojo::ScopedInterfaceEndpointHandle handle) {
5253 auto it = binding_sets_.find(interface_name);
5254 if (it != binding_sets_.end())
5255 it->second->OnRequestForFrame(render_frame_host, std::move(handle));
5256}
5257
Ben Goodger9e24929e2017-07-21 03:13:405258void WebContentsImpl::OnInterfaceRequest(
5259 RenderFrameHost* render_frame_host,
5260 const std::string& interface_name,
5261 mojo::ScopedMessagePipeHandle* interface_pipe) {
Joel Hockey85b379d2017-12-11 10:42:135262 registry_.TryBindInterface(interface_name, interface_pipe);
Ben Goodger9e24929e2017-07-21 03:13:405263 for (auto& observer : observers_) {
5264 observer.OnInterfaceRequestFromFrame(render_frame_host, interface_name,
5265 interface_pipe);
5266 if (!interface_pipe->is_valid())
5267 break;
5268 }
5269}
5270
Varun Khaneja5da94302019-06-25 04:48:105271void WebContentsImpl::OnDidBlockNavigation(
5272 const GURL& blocked_url,
5273 const GURL& initiator_url,
5274 blink::NavigationBlockedReason reason) {
Nicolas Dossou-gbetebcab2472017-09-09 12:28:405275 if (delegate_)
Varun Khaneja5da94302019-06-25 04:48:105276 delegate_->OnDidBlockNavigation(this, blocked_url, initiator_url, reason);
Nicolas Dossou-gbetebcab2472017-09-09 12:28:405277}
5278
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:415279const GURL& WebContentsImpl::GetMainFrameLastCommittedURL() {
[email protected]c31a84802014-04-03 15:55:495280 return GetLastCommittedURL();
5281}
5282
[email protected]b849847b2013-12-10 21:57:585283void WebContentsImpl::RenderFrameCreated(RenderFrameHost* render_frame_host) {
ericwilligersde386342016-10-19 02:35:095284 for (auto& observer : observers_)
5285 observer.RenderFrameCreated(render_frame_host);
dmazzonif8a31042016-12-03 00:52:135286 UpdateAccessibilityModeOnFrame(render_frame_host);
clamy91b7abf2016-03-24 15:17:455287
Becca Hughesd11d6502018-07-31 17:01:285288 if (display_cutout_host_impl_)
5289 display_cutout_host_impl_->RenderFrameCreated(render_frame_host);
5290
clamy91b7abf2016-03-24 15:17:455291 if (!render_frame_host->IsRenderFrameLive() || render_frame_host->GetParent())
5292 return;
5293
5294 NavigationEntry* entry = controller_.GetPendingEntry();
5295 if (entry && entry->IsViewSourceMode()) {
5296 // Put the renderer in view source mode.
5297 render_frame_host->Send(
5298 new FrameMsg_EnableViewSourceMode(render_frame_host->GetRoutingID()));
5299 }
[email protected]b849847b2013-12-10 21:57:585300}
5301
5302void WebContentsImpl::RenderFrameDeleted(RenderFrameHost* render_frame_host) {
creis3638897452017-05-09 22:07:505303 is_notifying_observers_ = true;
ericwilligersde386342016-10-19 02:35:095304 for (auto& observer : observers_)
5305 observer.RenderFrameDeleted(render_frame_host);
creis3638897452017-05-09 22:07:505306 is_notifying_observers_ = false;
zqzhang7b6f4f02016-12-07 22:10:225307#if BUILDFLAG(ENABLE_PLUGINS)
5308 pepper_playback_observer_->RenderFrameDeleted(render_frame_host);
5309#endif
Becca Hughesfd5d8f892018-06-14 18:23:365310
Becca Hughesd11d6502018-07-31 17:01:285311 if (display_cutout_host_impl_)
5312 display_cutout_host_impl_->RenderFrameDeleted(render_frame_host);
5313
Becca Hughesfd5d8f892018-06-14 18:23:365314 // Remove any fullscreen state that the frame has stored.
5315 FullscreenStateChanged(render_frame_host, false /* is_fullscreen */);
[email protected]b849847b2013-12-10 21:57:585316}
5317
[email protected]a09d53ce2014-01-31 00:46:425318void WebContentsImpl::ShowContextMenu(RenderFrameHost* render_frame_host,
5319 const ContextMenuParams& params) {
avie9fd8722017-04-10 16:17:585320 // If a renderer fires off a second command to show a context menu before the
5321 // first context menu is closed, just ignore it. https://crbug.com/707534
ekaramadf6750aa2017-06-06 18:29:425322 if (showing_context_menu_)
avie9fd8722017-04-10 16:17:585323 return;
5324
[email protected]077e704b2014-05-23 19:24:105325 ContextMenuParams context_menu_params(params);
[email protected]a09d53ce2014-01-31 00:46:425326 // Allow WebContentsDelegates to handle the context menu operation first.
Joel Klinghed0ea22ea2019-02-20 22:04:225327 if (delegate_ &&
5328 delegate_->HandleContextMenu(render_frame_host, context_menu_params))
[email protected]a09d53ce2014-01-31 00:46:425329 return;
5330
[email protected]077e704b2014-05-23 19:24:105331 render_view_host_delegate_view_->ShowContextMenu(render_frame_host,
5332 context_menu_params);
[email protected]a09d53ce2014-01-31 00:46:425333}
5334
Avi Drissman4891b852018-09-25 18:12:585335namespace {
5336// Normalizes the line endings: \r\n -> \n, lone \r -> \n.
5337base::string16 NormalizeLineBreaks(const base::string16& source) {
5338 static const base::NoDestructor<base::string16> kReturnNewline(
5339 base::ASCIIToUTF16("\r\n"));
5340 static const base::NoDestructor<base::string16> kReturn(
5341 base::ASCIIToUTF16("\r"));
5342 static const base::NoDestructor<base::string16> kNewline(
5343 base::ASCIIToUTF16("\n"));
5344
5345 std::vector<base::StringPiece16> pieces;
5346
5347 for (const auto& rn_line : base::SplitStringPieceUsingSubstr(
5348 source, *kReturnNewline, base::KEEP_WHITESPACE,
5349 base::SPLIT_WANT_ALL)) {
5350 auto r_lines = base::SplitStringPieceUsingSubstr(
5351 rn_line, *kReturn, base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL);
5352 std::move(std::begin(r_lines), std::end(r_lines),
5353 std::back_inserter(pieces));
5354 }
5355
5356 return base::JoinString(pieces, *kNewline);
5357}
5358} // namespace
5359
avi777ff452017-02-09 19:04:485360void WebContentsImpl::RunJavaScriptDialog(RenderFrameHost* render_frame_host,
5361 const base::string16& message,
5362 const base::string16& default_prompt,
avi777ff452017-02-09 19:04:485363 JavaScriptDialogType dialog_type,
5364 IPC::Message* reply_msg) {
Avi Drissman2ccbb402018-03-20 21:11:365365 // Ensure that if showing a dialog is the first thing that a page does, that
5366 // the contents of the previous page aren't shown behind it. This is required
5367 // because showing a dialog freezes the renderer, so no frames will be coming
5368 // from it. https://crbug.com/823353
5369 auto* render_widget_host_impl =
5370 static_cast<RenderFrameHostImpl*>(render_frame_host)
5371 ->GetRenderWidgetHost();
5372 if (render_widget_host_impl)
5373 render_widget_host_impl->ForceFirstFrameAfterNavigationTimeout();
5374
avi0720b02e2017-06-13 03:22:135375 // Running a dialog causes an exit to webpage-initiated fullscreen.
5376 // http://crbug.com/728276
Avi Drissmand18c51972018-08-15 18:32:095377 ForSecurityDropFullscreen();
avi0720b02e2017-06-13 03:22:135378
Avi Drissmane04d3992017-10-05 15:11:365379 auto callback =
5380 base::BindOnce(&WebContentsImpl::OnDialogClosed, base::Unretained(this),
5381 render_frame_host->GetProcess()->GetID(),
5382 render_frame_host->GetRoutingID(), reply_msg);
Pavel Feldman3c1842b2017-08-02 05:00:165383
Pavel Feldman3c1842b2017-08-02 05:00:165384 std::vector<protocol::PageHandler*> page_handlers =
5385 protocol::PageHandler::EnabledForWebContents(this);
5386
Pavel Feldmanfab3cb62017-10-05 01:02:015387 if (delegate_)
5388 dialog_manager_ = delegate_->GetJavaScriptDialogManager(this);
5389
Charlie Reis96077362019-07-04 00:34:045390 // While a JS message dialog is showing, defer commits in this WebContents.
5391 javascript_dialog_navigation_deferrer_ =
5392 std::make_unique<JavaScriptDialogNavigationDeferrer>();
5393
Pavel Feldmanfab3cb62017-10-05 01:02:015394 // Suppress JavaScript dialogs when requested. Also suppress messages when
5395 // showing an interstitial as it's shown over the previous page and we don't
5396 // want the hidden page's dialogs to interfere with the interstitial.
5397 bool should_suppress = ShowingInterstitialPage() ||
5398 (delegate_ && delegate_->ShouldSuppressDialogs(this));
Pavel Feldman1c587bc52018-04-04 21:16:055399 bool has_non_devtools_handlers = delegate_ && dialog_manager_;
5400 bool has_handlers = page_handlers.size() || has_non_devtools_handlers;
Pavel Feldmanfab3cb62017-10-05 01:02:015401 bool suppress_this_message = should_suppress || !has_handlers;
5402
5403 if (suppress_this_message) {
Avi Drissmane04d3992017-10-05 15:11:365404 std::move(callback).Run(true, false, base::string16());
Pavel Feldman3c1842b2017-08-02 05:00:165405 return;
5406 }
5407
5408 scoped_refptr<CloseDialogCallbackWrapper> wrapper =
Avi Drissmane04d3992017-10-05 15:11:365409 new CloseDialogCallbackWrapper(std::move(callback));
Pavel Feldman3c1842b2017-08-02 05:00:165410
5411 is_showing_javascript_dialog_ = true;
5412
Avi Drissman4891b852018-09-25 18:12:585413 base::string16 normalized_message = NormalizeLineBreaks(message);
5414
Pavel Feldman3c1842b2017-08-02 05:00:165415 for (auto* handler : page_handlers) {
Avi Drissmane04d3992017-10-05 15:11:365416 handler->DidRunJavaScriptDialog(
Avi Drissman4891b852018-09-25 18:12:585417 render_frame_host->GetLastCommittedURL(), normalized_message,
5418 default_prompt, dialog_type, has_non_devtools_handlers,
Avi Drissmane04d3992017-10-05 15:11:365419 base::BindOnce(&CloseDialogCallbackWrapper::Run, wrapper, false));
Pavel Feldman3c1842b2017-08-02 05:00:165420 }
5421
5422 if (dialog_manager_) {
[email protected]87de04b02014-04-08 22:14:495423 dialog_manager_->RunJavaScriptDialog(
Avi Drissman4891b852018-09-25 18:12:585424 this, render_frame_host, dialog_type, normalized_message,
5425 default_prompt,
Avi Drissmane04d3992017-10-05 15:11:365426 base::BindOnce(&CloseDialogCallbackWrapper::Run, wrapper, false),
5427 &suppress_this_message);
[email protected]87de04b02014-04-08 22:14:495428 }
5429
5430 if (suppress_this_message) {
5431 // If we are suppressing messages, just reply as if the user immediately
5432 // pressed "Cancel", passing true to |dialog_was_suppressed|.
Avi Drissmane04d3992017-10-05 15:11:365433 wrapper->Run(true, false, base::string16());
[email protected]87de04b02014-04-08 22:14:495434 }
[email protected]87de04b02014-04-08 22:14:495435}
5436
5437void WebContentsImpl::RunBeforeUnloadConfirm(
[email protected]39f2f9252014-05-03 00:54:015438 RenderFrameHost* render_frame_host,
[email protected]87de04b02014-04-08 22:14:495439 bool is_reload,
5440 IPC::Message* reply_msg) {
Avi Drissman2ccbb402018-03-20 21:11:365441 // Ensure that if showing a dialog is the first thing that a page does, that
5442 // the contents of the previous page aren't shown behind it. This is required
5443 // because showing a dialog freezes the renderer, so no frames will be coming
5444 // from it. https://crbug.com/823353
5445 auto* render_widget_host_impl =
5446 static_cast<RenderFrameHostImpl*>(render_frame_host)
5447 ->GetRenderWidgetHost();
5448 if (render_widget_host_impl)
5449 render_widget_host_impl->ForceFirstFrameAfterNavigationTimeout();
5450
avi0720b02e2017-06-13 03:22:135451 // Running a dialog causes an exit to webpage-initiated fullscreen.
5452 // http://crbug.com/728276
Avi Drissmand18c51972018-08-15 18:32:095453 ForSecurityDropFullscreen();
avi0720b02e2017-06-13 03:22:135454
[email protected]39f2f9252014-05-03 00:54:015455 RenderFrameHostImpl* rfhi =
5456 static_cast<RenderFrameHostImpl*>(render_frame_host);
[email protected]87de04b02014-04-08 22:14:495457 if (delegate_)
5458 delegate_->WillRunBeforeUnloadConfirm();
5459
Avi Drissmane04d3992017-10-05 15:11:365460 auto callback =
5461 base::BindOnce(&WebContentsImpl::OnDialogClosed, base::Unretained(this),
5462 render_frame_host->GetProcess()->GetID(),
5463 render_frame_host->GetRoutingID(), reply_msg);
Pavel Feldman3c1842b2017-08-02 05:00:165464
Pavel Feldman3c1842b2017-08-02 05:00:165465 std::vector<protocol::PageHandler*> page_handlers =
5466 protocol::PageHandler::EnabledForWebContents(this);
5467
Pavel Feldmanfab3cb62017-10-05 01:02:015468 if (delegate_)
5469 dialog_manager_ = delegate_->GetJavaScriptDialogManager(this);
5470
Charlie Reis96077362019-07-04 00:34:045471 // While a JS beforeunload dialog is showing, defer commits in this
5472 // WebContents.
5473 javascript_dialog_navigation_deferrer_ =
5474 std::make_unique<JavaScriptDialogNavigationDeferrer>();
5475
Pavel Feldmanfab3cb62017-10-05 01:02:015476 bool should_suppress = ShowingInterstitialPage() || !rfhi->is_active() ||
5477 (delegate_ && delegate_->ShouldSuppressDialogs(this));
Pavel Feldman1c587bc52018-04-04 21:16:055478 bool has_non_devtools_handlers = delegate_ && dialog_manager_;
5479 bool has_handlers = page_handlers.size() || has_non_devtools_handlers;
Pavel Feldmanfab3cb62017-10-05 01:02:015480 if (should_suppress || !has_handlers) {
Avi Drissmane04d3992017-10-05 15:11:365481 std::move(callback).Run(false, true, base::string16());
[email protected]87de04b02014-04-08 22:14:495482 return;
5483 }
5484
5485 is_showing_before_unload_dialog_ = true;
Pavel Feldman3c1842b2017-08-02 05:00:165486
5487 scoped_refptr<CloseDialogCallbackWrapper> wrapper =
Avi Drissmane04d3992017-10-05 15:11:365488 new CloseDialogCallbackWrapper(std::move(callback));
Pavel Feldman3c1842b2017-08-02 05:00:165489
5490 GURL frame_url = rfhi->GetLastCommittedURL();
5491 for (auto* handler : page_handlers) {
Avi Drissmane04d3992017-10-05 15:11:365492 handler->DidRunBeforeUnloadConfirm(
Pavel Feldman1c587bc52018-04-04 21:16:055493 frame_url, has_non_devtools_handlers,
Avi Drissmane04d3992017-10-05 15:11:365494 base::BindOnce(&CloseDialogCallbackWrapper::Run, wrapper, false));
Pavel Feldman3c1842b2017-08-02 05:00:165495 }
5496
5497 if (dialog_manager_) {
Avi Drissmane04d3992017-10-05 15:11:365498 dialog_manager_->RunBeforeUnloadDialog(
Avi Drissmanff9ed752017-11-28 21:18:125499 this, render_frame_host, is_reload,
Avi Drissmane04d3992017-10-05 15:11:365500 base::BindOnce(&CloseDialogCallbackWrapper::Run, wrapper, false));
Pavel Feldman3c1842b2017-08-02 05:00:165501 }
[email protected]87de04b02014-04-08 22:14:495502}
5503
Kent Tamurac260c9a2018-09-14 09:10:565504void WebContentsImpl::RunFileChooser(
5505 RenderFrameHost* render_frame_host,
Kent Tamura512a27e2018-10-04 00:49:325506 std::unique_ptr<content::FileSelectListener> listener,
Kent Tamurac260c9a2018-09-14 09:10:565507 const blink::mojom::FileChooserParams& params) {
Avi Drissman3d41e7712018-08-27 21:18:085508 // Any explicit focusing of another window while this WebContents is in
5509 // fullscreen can be used to confuse the user, so drop fullscreen.
5510 ForSecurityDropFullscreen();
5511
Andrey Lushnikov81b992e2019-06-27 23:27:215512 RenderFrameHostImpl* rfhi =
5513 static_cast<RenderFrameHostImpl*>(render_frame_host);
5514 if (devtools_instrumentation::InterceptFileChooser(rfhi, &listener, params))
5515 return;
5516
naskoada75b22016-06-11 16:09:465517 if (delegate_)
Kent Tamura512a27e2018-10-04 00:49:325518 delegate_->RunFileChooser(render_frame_host, std::move(listener), params);
5519 else
5520 listener->FileSelectionCanceled();
naskoada75b22016-06-11 16:09:465521}
5522
[email protected]a86c0e962013-12-17 17:10:395523WebContents* WebContentsImpl::GetAsWebContents() {
5524 return this;
5525}
5526
mcnee336ea2c2017-05-23 22:50:595527#if !defined(OS_ANDROID)
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:415528double WebContentsImpl::GetPendingPageZoomLevel() {
wjmaclean64951902016-04-29 20:59:125529 NavigationEntry* pending_entry = GetController().GetPendingEntry();
5530 if (!pending_entry)
5531 return HostZoomMap::GetZoomLevel(this);
5532
5533 GURL url = pending_entry->GetURL();
5534 return HostZoomMap::GetForWebContents(this)->GetZoomLevelForHostAndScheme(
5535 url.scheme(), net::GetHostOrSpecFromURL(url));
5536}
mcnee336ea2c2017-05-23 22:50:595537#endif // !defined(OS_ANDROID)
wjmaclean64951902016-04-29 20:59:125538
qinmin72e8bd02016-10-21 19:35:375539bool WebContentsImpl::HideDownloadUI() const {
5540 return is_overlay_content_;
5541}
5542
mlamouri5cd9ae82017-02-18 11:05:095543bool WebContentsImpl::HasPersistentVideo() const {
5544 return has_persistent_video_;
5545}
5546
Ian Vollick5d5c37f2019-06-19 22:50:575547bool WebContentsImpl::IsSpatialNavigationDisabled() const {
5548 return is_spatial_navigation_disabled_;
5549}
5550
Alex Moshchuk27caae82017-09-11 23:11:185551RenderFrameHost* WebContentsImpl::GetPendingMainFrame() {
clamy610c63b32017-12-22 15:05:185552 return GetRenderManager()->speculative_frame_host();
Alex Moshchuk27caae82017-09-11 23:11:185553}
5554
mlamourie52c1b542017-02-23 14:57:505555bool WebContentsImpl::HasActiveEffectivelyFullscreenVideo() const {
5556 return media_web_contents_observer_->HasActiveEffectivelyFullscreenVideo();
5557}
5558
François Beaufortad6c5232018-02-26 11:00:445559bool WebContentsImpl::IsPictureInPictureAllowedForFullscreenVideo() const {
5560 return media_web_contents_observer_
5561 ->IsPictureInPictureAllowedForFullscreenVideo();
5562}
5563
ekaramada110f642016-12-21 19:47:285564bool WebContentsImpl::IsFocusedElementEditable() {
5565 RenderFrameHostImpl* frame = GetFocusedFrame();
5566 return frame && frame->has_focused_editable_element();
5567}
5568
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:415569bool WebContentsImpl::IsShowingContextMenu() {
ekaramadf6750aa2017-06-06 18:29:425570 return showing_context_menu_;
5571}
5572
5573void WebContentsImpl::SetShowingContextMenu(bool showing) {
5574 DCHECK_NE(showing_context_menu_, showing);
5575 showing_context_menu_ = showing;
5576
5577 if (auto* view = GetRenderWidgetHostView()) {
5578 // Notify the main frame's RWHV to run the platform-specific code, if any.
5579 static_cast<RenderWidgetHostViewBase*>(view)->SetShowingContextMenu(
5580 showing);
5581 }
5582}
5583
ekaramada110f642016-12-21 19:47:285584void WebContentsImpl::ClearFocusedElement() {
5585 if (auto* frame = GetFocusedFrame())
5586 frame->ClearFocusedElement();
5587}
5588
[email protected]7912e822014-04-16 02:37:035589bool WebContentsImpl::IsNeverVisible() {
5590 if (!delegate_)
5591 return false;
5592 return delegate_->IsNeverVisible(this);
5593}
5594
[email protected]5a3bdf52012-05-24 15:12:575595RenderViewHostDelegateView* WebContentsImpl::GetDelegateView() {
5596 return render_view_host_delegate_view_;
[email protected]420ae012009-04-24 05:16:325597}
5598
Leon Hanc819dc62019-01-28 04:30:195599blink::mojom::RendererPreferences WebContentsImpl::GetRendererPrefs(
[email protected]8ff00d72012-10-23 19:12:215600 BrowserContext* browser_context) const {
[email protected]840b1512009-07-21 16:53:465601 return renderer_preferences_;
[email protected]80d96fa2009-06-10 22:34:515602}
5603
[email protected]7a846df2012-09-20 19:17:395604void WebContentsImpl::RemoveBrowserPluginEmbedder() {
Lei Zhang6410ec1a2019-01-31 21:38:585605 browser_plugin_embedder_.reset();
[email protected]7a846df2012-09-20 19:17:395606}
5607
Lucas Furukawa Gadani2ec00c82018-12-14 15:53:165608RenderFrameHostImpl* WebContentsImpl::GetOuterWebContentsFrame() {
5609 if (GetOuterDelegateFrameTreeNodeId() ==
5610 FrameTreeNode::kFrameTreeNodeInvalidId) {
5611 return nullptr;
5612 }
5613
5614 FrameTreeNode* outer_node =
5615 FrameTreeNode::GloballyFindByID(GetOuterDelegateFrameTreeNodeId());
5616 // The outer node should be in the outer WebContents.
5617 DCHECK_EQ(outer_node->frame_tree(), GetOuterWebContents()->GetFrameTree());
5618 return outer_node->parent()->current_frame_host();
5619}
5620
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:415621WebContentsImpl* WebContentsImpl::GetOuterWebContents() {
lfg601233692017-03-06 22:45:445622 if (GuestMode::IsCrossProcessFrameGuest(this))
5623 return node_.outer_web_contents();
ekaramad1beabecc2016-09-23 20:18:175624
5625 if (browser_plugin_guest_)
5626 return browser_plugin_guest_->embedder_web_contents();
5627
Lucas Furukawa Gadani105de1e812018-05-31 19:38:395628 return node_.outer_web_contents();
avallee0206f782016-07-28 18:55:335629}
5630
Lucas Furukawa Gadani2ec00c82018-12-14 15:53:165631std::vector<WebContents*> WebContentsImpl::GetInnerWebContents() {
5632 std::vector<WebContents*> all_inner_contents;
5633 if (browser_plugin_embedder_) {
Sean Topping7df73952019-05-16 17:21:165634 BrowserPluginGuestManager* guest_manager =
5635 GetBrowserContext()->GetGuestManager();
5636 if (guest_manager) {
5637 guest_manager->ForEachGuest(
5638 this,
5639 base::BindRepeating(&GetInnerWebContentsHelper, &all_inner_contents));
5640 }
Lucas Furukawa Gadani2ec00c82018-12-14 15:53:165641 }
5642 const auto& inner_contents = node_.GetInnerWebContents();
5643 all_inner_contents.insert(all_inner_contents.end(), inner_contents.begin(),
5644 inner_contents.end());
5645 return all_inner_contents;
5646}
5647
avallee0206f782016-07-28 18:55:335648WebContentsImpl* WebContentsImpl::GetFocusedWebContents() {
lfg601233692017-03-06 22:45:445649 return GetOutermostWebContents()->node_.focused_web_contents();
avallee0206f782016-07-28 18:55:335650}
5651
avallee9993fca2016-11-17 06:16:505652bool WebContentsImpl::ContainsOrIsFocusedWebContents() {
5653 for (WebContentsImpl* focused_contents = GetFocusedWebContents();
5654 focused_contents;
5655 focused_contents = focused_contents->GetOuterWebContents()) {
5656 if (focused_contents == this)
5657 return true;
5658 }
5659
5660 return false;
5661}
5662
avallee0206f782016-07-28 18:55:335663WebContentsImpl* WebContentsImpl::GetOutermostWebContents() {
5664 WebContentsImpl* root = this;
5665 while (root->GetOuterWebContents())
5666 root = root->GetOuterWebContents();
5667 return root;
5668}
5669
avalleed68cdd12017-06-21 22:07:275670void WebContentsImpl::FocusOuterAttachmentFrameChain() {
5671 WebContentsImpl* outer_contents = GetOuterWebContents();
5672 if (!outer_contents)
5673 return;
5674
5675 FrameTreeNode* outer_node =
5676 FrameTreeNode::GloballyFindByID(GetOuterDelegateFrameTreeNodeId());
5677 outer_contents->frame_tree_.SetFocusedFrame(outer_node, nullptr);
5678
5679 // For a browser initiated focus change, let embedding renderer know of the
5680 // change. Otherwise, if the currently focused element is just across a
5681 // process boundary in focus order, it will not be possible to move across
5682 // that boundary. This is because the target element will already be focused
5683 // (that renderer was not notified) and drop the event.
5684 if (GetRenderManager()->GetProxyToOuterDelegate())
5685 GetRenderManager()->GetProxyToOuterDelegate()->SetFocusedFrame();
5686
5687 outer_contents->FocusOuterAttachmentFrameChain();
5688}
5689
Sean Toppingbbc4a2bd2019-01-30 02:16:145690void WebContentsImpl::InnerWebContentsCreated(WebContents* inner_web_contents) {
Shivani Sharma985474f2019-05-23 21:36:475691 had_inner_webcontents_ = true;
Sean Toppingbbc4a2bd2019-01-30 02:16:145692 for (auto& observer : observers_)
5693 observer.InnerWebContentsCreated(inner_web_contents);
5694}
5695
[email protected]b172aee2012-04-10 17:05:265696void WebContentsImpl::RenderViewCreated(RenderViewHost* render_view_host) {
[email protected]86f98a22013-03-20 14:35:005697 if (delegate_)
[email protected]e85165c642014-06-10 14:34:315698 view_->SetOverscrollControllerEnabled(CanOverscrollContent());
[email protected]a6b73c62013-02-11 23:05:085699
[email protected]8ff00d72012-10-23 19:12:215700 NotificationService::current()->Notify(
5701 NOTIFICATION_WEB_CONTENTS_RENDER_VIEW_HOST_CREATED,
5702 Source<WebContents>(this),
5703 Details<RenderViewHost>(render_view_host));
[email protected]420ae012009-04-24 05:16:325704
[email protected]60780f412013-02-25 16:34:105705 view_->RenderViewCreated(render_view_host);
[email protected]11f764bb2011-05-25 19:45:265706
ericwilligersde386342016-10-19 02:35:095707 for (auto& observer : observers_)
5708 observer.RenderViewCreated(render_view_host);
Joel Einbinder848001c2017-09-19 21:39:315709 if (delegate_)
5710 RenderFrameDevToolsAgentHost::WebContentsCreated(this);
[email protected]420ae012009-04-24 05:16:325711}
5712
[email protected]b172aee2012-04-10 17:05:265713void WebContentsImpl::RenderViewReady(RenderViewHost* rvh) {
[email protected]151a63d2011-12-20 22:32:525714 if (rvh != GetRenderViewHost()) {
[email protected]420ae012009-04-24 05:16:325715 // Don't notify the world, since this came from a renderer in the
5716 // background.
5717 return;
5718 }
5719
dmazzoni62111452017-04-28 23:46:555720 RenderWidgetHostViewBase* rwhv =
5721 static_cast<RenderWidgetHostViewBase*>(GetRenderWidgetHostView());
5722 if (rwhv)
5723 rwhv->SetMainFrameAXTreeID(GetMainFrame()->GetAXTreeID());
5724
[email protected]da7a7182013-09-06 08:11:115725 notify_disconnection_ = true;
[email protected]da7a7182013-09-06 08:11:115726
[email protected]be1f56ab2011-12-22 06:55:315727 bool was_crashed = IsCrashed();
[email protected]443b80e2010-12-14 00:42:235728 SetIsCrashed(base::TERMINATION_STATUS_STILL_RUNNING, 0);
[email protected]9d8a4642009-07-29 17:25:305729
5730 // Restore the focus to the tab (otherwise the focus will be on the top
5731 // window).
[email protected]484ae5912010-09-29 19:16:145732 if (was_crashed && !FocusLocationBarByDefault() &&
5733 (!delegate_ || delegate_->ShouldFocusPageAfterCrash())) {
[email protected]f3615f02013-02-26 06:09:065734 view_->Focus();
[email protected]484ae5912010-09-29 19:16:145735 }
[email protected]32ded2212011-11-10 18:51:435736
ericwilligersde386342016-10-19 02:35:095737 for (auto& observer : observers_)
5738 observer.RenderViewReady();
Jinsuk Kim895b2c52018-07-03 06:49:165739
5740 view_->RenderViewReady();
[email protected]420ae012009-04-24 05:16:325741}
5742
[email protected]ec6a7eb2013-04-22 17:34:225743void WebContentsImpl::RenderViewTerminated(RenderViewHost* rvh,
5744 base::TerminationStatus status,
5745 int error_code) {
[email protected]151a63d2011-12-20 22:32:525746 if (rvh != GetRenderViewHost()) {
[email protected]420ae012009-04-24 05:16:325747 // The pending page's RenderViewHost is gone.
5748 return;
5749 }
5750
[email protected]2fcdcc32014-03-05 02:14:075751 // Ensure fullscreen mode is exited in the |delegate_| since a crashed
5752 // renderer may not have made a clean exit.
alexmos5dd617d2016-06-07 04:21:155753 if (IsFullscreenForCurrentTab())
bokanece34a82016-01-28 19:49:465754 ExitFullscreenMode(false);
[email protected]2fcdcc32014-03-05 02:14:075755
Jennifer Apacibleb4b972a2018-06-07 04:41:025756 // Ensure any video in Picture-in-Picture is exited in the |delegate_| since
5757 // a crashed renderer may not have made a clean exit.
5758 if (HasPictureInPictureVideo())
5759 ExitPictureInPicture();
5760
[email protected]698191dc2014-02-25 01:06:135761 // Cancel any visible dialogs so they are not left dangling over the sad tab.
creis89a0f782015-05-27 16:13:175762 CancelActiveAndPendingDialogs();
[email protected]698191dc2014-02-25 01:06:135763
dalecurtis3d1e1302017-02-20 22:33:255764 audio_stream_monitor_.RenderProcessGone(rvh->GetProcess()->GetID());
5765
[email protected]960b0372014-05-19 18:01:005766 // Reset the loading progress. TODO(avi): What does it mean to have a
5767 // "renderer crash" when there is more than one renderer process serving a
5768 // webpage? Once this function is called at a more granular frame level, we
5769 // probably will need to more granularly reset the state here.
5770 ResetLoadProgressState();
clamy44e84ce2016-02-22 15:38:255771 NotifyDisconnected();
5772 SetIsCrashed(status, error_code);
[email protected]960b0372014-05-19 18:01:005773
ericwilligersde386342016-10-19 02:35:095774 for (auto& observer : observers_)
5775 observer.RenderProcessGone(GetCrashedStatus());
[email protected]420ae012009-04-24 05:16:325776}
5777
[email protected]b172aee2012-04-10 17:05:265778void WebContentsImpl::RenderViewDeleted(RenderViewHost* rvh) {
ericwilligersde386342016-10-19 02:35:095779 for (auto& observer : observers_)
5780 observer.RenderViewDeleted(rvh);
[email protected]2e4633c2009-07-09 16:58:065781}
5782
sky8b00392d2015-01-10 00:30:285783void WebContentsImpl::UpdateTargetURL(RenderViewHost* render_view_host,
5784 const GURL& url) {
5785 if (fullscreen_widget_routing_id_ != MSG_ROUTING_NONE) {
Lukasz Anforowiczfd707142018-02-07 19:46:135786 // If we're in flash fullscreen (i.e. Pepper plugin fullscreen) only update
5787 // the url if it's from the fullscreen renderer.
sky8b00392d2015-01-10 00:30:285788 RenderWidgetHostView* fs = GetFullscreenRenderWidgetHostView();
avif9ab5d942015-10-15 14:05:445789 if (fs && fs->GetRenderWidgetHost() != render_view_host->GetWidget())
sky8b00392d2015-01-10 00:30:285790 return;
5791 }
Lukasz Anforowiczfd707142018-02-07 19:46:135792
5793 // In case of racey updates from multiple RenderViewHosts, the last URL should
5794 // be shown - see also some discussion in https://crbug.com/807776.
5795 if (!url.is_valid() && render_view_host != view_that_set_last_target_url_)
5796 return;
5797 view_that_set_last_target_url_ = url.is_valid() ? render_view_host : nullptr;
5798
[email protected]6934a702011-12-20 00:04:515799 if (delegate_)
avi450ce00e2014-09-24 01:58:525800 delegate_->UpdateTargetURL(this, url);
[email protected]420ae012009-04-24 05:16:325801}
5802
Lukasz Anforowiczfd707142018-02-07 19:46:135803void WebContentsImpl::ClearTargetURL() {
5804 view_that_set_last_target_url_ = nullptr;
5805 if (delegate_)
5806 delegate_->UpdateTargetURL(this, GURL());
5807}
5808
[email protected]b172aee2012-04-10 17:05:265809void WebContentsImpl::Close(RenderViewHost* rvh) {
[email protected]60780f412013-02-25 16:34:105810#if defined(OS_MACOSX)
[email protected]07707302009-11-06 00:50:295811 // The UI may be in an event-tracking loop, such as between the
5812 // mouse-down and mouse-up in text selection or a button click.
5813 // Defer the close until after tracking is complete, so that we
5814 // don't free objects out from under the UI.
[email protected]07707302009-11-06 00:50:295815 // TODO(shess): This could get more fine-grained. For instance,
5816 // closing a tab in another window while selecting text in the
5817 // current window's Omnibox should be just fine.
Christopher Cameron9ae31e0d2019-03-01 20:01:195818 if (view_->CloseTabAfterEventTrackingIfNeeded())
[email protected]07707302009-11-06 00:50:295819 return;
[email protected]60780f412013-02-25 16:34:105820#endif
[email protected]07707302009-11-06 00:50:295821
[email protected]420ae012009-04-24 05:16:325822 // Ignore this if it comes from a RenderViewHost that we aren't showing.
[email protected]151a63d2011-12-20 22:32:525823 if (delegate_ && rvh == GetRenderViewHost())
[email protected]6934a702011-12-20 00:04:515824 delegate_->CloseContents(this);
[email protected]420ae012009-04-24 05:16:325825}
5826
Avi Drissman014dec72018-06-07 02:34:015827void WebContentsImpl::RequestSetBounds(const gfx::Rect& new_bounds) {
5828 if (delegate_)
5829 delegate_->SetContentsBounds(this, new_bounds);
[email protected]420ae012009-04-24 05:16:325830}
5831
fdegansa696e5112015-04-17 01:57:595832void WebContentsImpl::DidStartLoading(FrameTreeNode* frame_tree_node,
[email protected]e3b10d12014-03-28 16:06:095833 bool to_different_document) {
Evan Stadec3b26022017-07-14 19:50:245834 LoadingStateChanged(frame_tree_node->IsMainFrame() && to_different_document,
5835 false, nullptr);
dmazzonibf8cec42015-02-08 08:28:085836
Xiyuan Xia2a66b112018-12-06 15:52:165837 // Reset the focus state from DidStartNavigation to false if a new load starts
5838 // afterward, in case loading logic triggers a FocusLocationBarByDefault call.
5839 should_focus_location_bar_by_default_ = false;
5840
dmazzonibf8cec42015-02-08 08:28:085841 // Notify accessibility that the user is navigating away from the
5842 // current document.
Dominic Farolinof87ec2a2019-07-04 09:18:095843 // TODO(domfarolino, dmazzoni): Do this using WebContentsObserver. See
5844 // https://crbug.com/981271.
dmazzonibf8cec42015-02-08 08:28:085845 BrowserAccessibilityManager* manager =
fdegansa696e5112015-04-17 01:57:595846 frame_tree_node->current_frame_host()->browser_accessibility_manager();
dmazzonibf8cec42015-02-08 08:28:085847 if (manager)
5848 manager->UserIsNavigatingAway();
[email protected]420ae012009-04-24 05:16:325849}
5850
fdegansc73eec22015-03-19 14:47:545851void WebContentsImpl::DidStopLoading() {
dcheng59716272016-04-09 05:19:085852 std::unique_ptr<LoadNotificationDetails> details;
[email protected]420ae012009-04-24 05:16:325853
[email protected]9595fd82013-04-19 21:28:495854 // Use the last committed entry rather than the active one, in case a
5855 // pending entry has been created.
5856 NavigationEntry* entry = controller_.GetLastCommittedEntry();
[email protected]3691e5cf2014-01-22 10:16:205857 Navigator* navigator = frame_tree_.root()->navigator();
[email protected]9595fd82013-04-19 21:28:495858
[email protected]420ae012009-04-24 05:16:325859 // An entry may not exist for a stop when loading an initial blank page or
5860 // if an iframe injected by script into a blank page finishes loading.
5861 if (entry) {
[email protected]3691e5cf2014-01-22 10:16:205862 base::TimeDelta elapsed =
5863 base::TimeTicks::Now() - navigator->GetCurrentLoadStart();
[email protected]420ae012009-04-24 05:16:325864
5865 details.reset(new LoadNotificationDetails(
[email protected]36fc0392011-12-25 03:59:515866 entry->GetVirtualURL(),
[email protected]420ae012009-04-24 05:16:325867 elapsed,
5868 &controller_,
5869 controller_.GetCurrentEntryIndex()));
5870 }
5871
clamy44e84ce2016-02-22 15:38:255872 LoadingStateChanged(true, false, details.get());
[email protected]420ae012009-04-24 05:16:325873}
5874
fdegansa696e5112015-04-17 01:57:595875void WebContentsImpl::DidChangeLoadProgress() {
Nate Chapin22ea6592019-03-05 22:29:025876 if (is_being_destroyed_)
5877 return;
fdegansa696e5112015-04-17 01:57:595878 double load_progress = frame_tree_.load_progress();
5879
5880 // The delegate is notified immediately for the first and last updates. Also,
5881 // since the message loop may be pretty busy when a page is loaded, it might
5882 // not execute a posted task in a timely manner so the progress report is sent
5883 // immediately if enough time has passed.
5884 base::TimeDelta min_delay =
5885 base::TimeDelta::FromMilliseconds(kMinimumDelayBetweenLoadingUpdatesMS);
5886 bool delay_elapsed = loading_last_progress_update_.is_null() ||
5887 base::TimeTicks::Now() - loading_last_progress_update_ > min_delay;
5888
5889 if (load_progress == 0.0 || load_progress == 1.0 || delay_elapsed) {
5890 // If there is a pending task to send progress, it is now obsolete.
5891 loading_weak_factory_.InvalidateWeakPtrs();
5892
5893 // Notify the load progress change.
5894 SendChangeLoadProgress();
5895
5896 // Clean-up the states if needed.
5897 if (load_progress == 1.0)
5898 ResetLoadProgressState();
5899 return;
5900 }
5901
5902 if (loading_weak_factory_.HasWeakPtrs())
5903 return;
5904
skyostil95082a62015-06-05 19:53:075905 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
tzike2aca992017-09-05 08:50:545906 FROM_HERE,
5907 base::BindOnce(&WebContentsImpl::SendChangeLoadProgress,
5908 loading_weak_factory_.GetWeakPtr()),
fdegansa696e5112015-04-17 01:57:595909 min_delay);
5910}
5911
avid6d88b912017-01-13 00:16:005912std::vector<std::unique_ptr<NavigationThrottle>>
5913WebContentsImpl::CreateThrottlesForNavigation(
clamycbf524d42016-09-27 12:48:355914 NavigationHandle* navigation_handle) {
Charlie Reis96077362019-07-04 00:34:045915 auto throttles = GetContentClient()->browser()->CreateThrottlesForNavigation(
clamycbf524d42016-09-27 12:48:355916 navigation_handle);
Charlie Reis96077362019-07-04 00:34:045917
5918 // This is not a normal place to be adding a throttle. However, in the case
5919 // javascript dialogs, related logic is present in the web_contents/ layer,
5920 // and the purpose of the throttle is to ensure that navigation commits are
5921 // deferred for the entire WebContents. Most throttles are either added by
5922 // the embederrer outside of content/, or are per-frame and added by
5923 // NavigationThrottleRunner.
5924 std::unique_ptr<content::NavigationThrottle> dialog_throttle =
5925 JavaScriptDialogNavigationThrottle::MaybeCreateThrottleFor(
5926 navigation_handle);
5927 if (dialog_throttle)
5928 throttles.push_back(std::move(dialog_throttle));
5929
5930 return throttles;
clamycbf524d42016-09-27 12:48:355931}
5932
clamy1e5574e2016-09-29 16:48:445933std::unique_ptr<NavigationUIData> WebContentsImpl::GetNavigationUIData(
5934 NavigationHandle* navigation_handle) {
clamy1e5574e2016-09-29 16:48:445935 return GetContentClient()->browser()->GetNavigationUIData(navigation_handle);
5936}
5937
pdr15f5b5b2017-06-20 00:58:005938void WebContentsImpl::DidCancelLoading() {
5939 controller_.DiscardNonCommittedEntries();
5940
5941 // Update the URL display.
5942 NotifyNavigationStateChanged(INVALIDATE_TYPE_URL);
5943}
5944
[email protected]59167c22013-06-03 18:07:325945void WebContentsImpl::DidAccessInitialDocument() {
[email protected]b4c84012014-04-28 19:51:105946 has_accessed_initial_document_ = true;
5947
[email protected]d1371bf8e2014-04-19 03:26:245948 // We may have left a failed browser-initiated navigation in the address bar
5949 // to let the user edit it and try again. Clear it now that content might
5950 // show up underneath it.
5951 if (!IsLoading() && controller_.GetPendingEntry())
avi45a72532015-04-07 21:01:455952 controller_.DiscardPendingEntry(false);
[email protected]d1371bf8e2014-04-19 03:26:245953
[email protected]59167c22013-06-03 18:07:325954 // Update the URL display.
thestigc4cac8f2014-09-04 21:17:505955 NotifyNavigationStateChanged(INVALIDATE_TYPE_URL);
[email protected]59167c22013-06-03 18:07:325956}
5957
alexmosbe2f4c32015-03-10 02:30:235958void WebContentsImpl::DidChangeName(RenderFrameHost* render_frame_host,
5959 const std::string& name) {
ericwilligersde386342016-10-19 02:35:095960 for (auto& observer : observers_)
5961 observer.FrameNameChanged(render_frame_host, name);
alexmosbe2f4c32015-03-10 02:30:235962}
5963
John Delaneyedd8d6c2019-01-25 00:23:575964void WebContentsImpl::DidReceiveFirstUserActivation(
5965 RenderFrameHost* render_frame_host) {
5966 for (auto& observer : observers_)
5967 observer.FrameReceivedFirstUserActivation(render_frame_host);
5968}
5969
John Delaney0146a742019-01-25 19:25:285970void WebContentsImpl::DidChangeDisplayState(RenderFrameHost* render_frame_host,
5971 bool is_display_none) {
5972 for (auto& observer : observers_)
5973 observer.FrameDisplayStateChanged(render_frame_host, is_display_none);
5974}
5975
5976void WebContentsImpl::FrameSizeChanged(RenderFrameHost* render_frame_host,
5977 const gfx::Size& frame_size) {
5978 for (auto& observer : observers_)
5979 observer.FrameSizeChanged(render_frame_host, frame_size);
5980}
5981
[email protected]39f2f9252014-05-03 00:54:015982void WebContentsImpl::DocumentOnLoadCompleted(
[email protected]50279492014-05-05 23:24:295983 RenderFrameHost* render_frame_host) {
falken52a56e32016-12-08 05:02:405984 ShowInsecureLocalhostWarningIfNeeded();
5985
creis97867aa2017-02-17 18:39:455986 is_notifying_observers_ = true;
ericwilligersde386342016-10-19 02:35:095987 for (auto& observer : observers_)
5988 observer.DocumentOnLoadCompletedInMainFrame();
creis97867aa2017-02-17 18:39:455989 is_notifying_observers_ = false;
[email protected]6578fef2013-10-14 02:35:095990
5991 // TODO(avi): Remove. http://crbug.com/170921
[email protected]8ff00d72012-10-23 19:12:215992 NotificationService::current()->Notify(
5993 NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
5994 Source<WebContents>(this),
[email protected]50279492014-05-05 23:24:295995 NotificationService::NoDetails());
[email protected]25497492010-09-11 15:15:085996}
5997
creis8eb8ef22015-11-04 22:51:385998void WebContentsImpl::UpdateStateForFrame(RenderFrameHost* render_frame_host,
5999 const PageState& page_state) {
creis8eb8ef22015-11-04 22:51:386000 // The state update affects the last NavigationEntry associated with the given
6001 // |render_frame_host|. This may not be the last committed NavigationEntry (as
6002 // in the case of an UpdateState from a frame being swapped out). We track
6003 // which entry this is in the RenderFrameHost's nav_entry_id.
6004 RenderFrameHostImpl* rfhi =
6005 static_cast<RenderFrameHostImpl*>(render_frame_host);
6006 NavigationEntryImpl* entry =
6007 controller_.GetEntryWithUniqueID(rfhi->nav_entry_id());
6008 if (!entry)
6009 return;
6010
6011 FrameNavigationEntry* frame_entry =
6012 entry->GetFrameEntry(rfhi->frame_tree_node());
6013 if (!frame_entry)
6014 return;
6015
creisd06a9422015-11-11 03:08:456016 // The SiteInstance might not match if we do a cross-process navigation with
6017 // replacement (e.g., auto-subframe), in which case the swap out of the old
6018 // RenderFrameHost runs in the background after the old FrameNavigationEntry
6019 // has already been replaced and destroyed.
6020 if (frame_entry->site_instance() != rfhi->GetSiteInstance())
6021 return;
6022
creis8eb8ef22015-11-04 22:51:386023 if (page_state == frame_entry->page_state())
6024 return; // Nothing to update.
6025
creis0cade2e2017-02-28 06:37:476026 DCHECK(page_state.IsValid()) << "Shouldn't set an empty PageState.";
nasko34634672016-07-29 18:46:066027
6028 // The document_sequence_number and item_sequence_number recorded in the
6029 // FrameNavigationEntry should not differ from the one coming with the update,
6030 // since it must come from the same document. Do not update it if a difference
6031 // is detected, as this indicates that |frame_entry| is not the correct one.
6032 ExplodedPageState exploded_state;
6033 if (!DecodePageState(page_state.ToEncodedData(), &exploded_state))
6034 return;
6035
6036 if (exploded_state.top.document_sequence_number !=
6037 frame_entry->document_sequence_number() ||
6038 exploded_state.top.item_sequence_number !=
6039 frame_entry->item_sequence_number()) {
6040 return;
6041 }
6042
nasko04a322f25a2016-09-01 00:00:346043 frame_entry->SetPageState(page_state);
creis8eb8ef22015-11-04 22:51:386044 controller_.NotifyEntryChanged(entry);
6045}
6046
[email protected]ef3adfc2014-05-11 00:04:546047void WebContentsImpl::UpdateTitle(RenderFrameHost* render_frame_host,
[email protected]ef3adfc2014-05-11 00:04:546048 const base::string16& title,
6049 base::i18n::TextDirection title_direction) {
[email protected]ef3adfc2014-05-11 00:04:546050 // Try to find the navigation entry, which might not be the current one.
avi286019ce2015-07-29 01:26:586051 // For example, it might be from a recently swapped out RFH.
avi1edc8552016-10-13 21:37:166052 NavigationEntryImpl* entry = controller_.GetEntryWithUniqueID(
creisad8c31a2016-05-24 17:07:046053 static_cast<RenderFrameHostImpl*>(render_frame_host)->nav_entry_id());
[email protected]ef3adfc2014-05-11 00:04:546054
6055 // We can handle title updates when we don't have an entry in
6056 // UpdateTitleForEntry, but only if the update is from the current RVH.
6057 // TODO(avi): Change to make decisions based on the RenderFrameHost.
avi286019ce2015-07-29 01:26:586058 if (!entry && render_frame_host != GetMainFrame())
[email protected]ef3adfc2014-05-11 00:04:546059 return;
6060
6061 // TODO(evan): make use of title_direction.
6062 // http://code.google.com/p/chromium/issues/detail?id=27094
afakhry6f0c1ec22016-07-14 13:55:136063 UpdateTitleForEntry(entry, title);
[email protected]ef3adfc2014-05-11 00:04:546064}
6065
6066void WebContentsImpl::UpdateEncoding(RenderFrameHost* render_frame_host,
6067 const std::string& encoding) {
6068 SetEncoding(encoding);
6069}
6070
[email protected]39f2f9252014-05-03 00:54:016071void WebContentsImpl::DocumentAvailableInMainFrame(
6072 RenderViewHost* render_view_host) {
ericwilligersde386342016-10-19 02:35:096073 for (auto& observer : observers_)
6074 observer.DocumentAvailableInMainFrame();
[email protected]39f2f9252014-05-03 00:54:016075}
naskoc0fceff2015-04-30 15:53:526076
[email protected]e1c3a552012-05-04 20:51:326077void WebContentsImpl::RouteCloseEvent(RenderViewHost* rvh) {
6078 // Tell the active RenderViewHost to run unload handlers and close, as long
6079 // as the request came from a RenderViewHost in the same BrowsingInstance.
6080 // In most cases, we receive this from a swapped out RenderViewHost.
6081 // It is possible to receive it from one that has just been swapped in,
6082 // in which case we might as well deliver the message anyway.
6083 if (rvh->GetSiteInstance()->IsRelatedSiteInstance(GetSiteInstance()))
naskoc0fceff2015-04-30 15:53:526084 ClosePage();
[email protected]e1c3a552012-05-04 20:51:326085}
6086
alexmose7da5a12015-04-09 02:22:166087bool WebContentsImpl::ShouldRouteMessageEvent(
6088 RenderFrameHost* target_rfh,
6089 SiteInstance* source_site_instance) const {
6090 // Allow the message if this WebContents is dedicated to a browser plugin
6091 // guest.
[email protected]0eba810b2012-10-18 03:19:366092 // Note: This check means that an embedder could theoretically receive a
6093 // postMessage from anyone (not just its own guests). However, this is
6094 // probably not a risk for apps since other pages won't have references
6095 // to App windows.
alexmose7da5a12015-04-09 02:22:166096 return GetBrowserPluginGuest() || GetBrowserPluginEmbedder();
6097}
[email protected]f546640b2012-05-15 00:03:496098
alexmos58729042015-06-18 23:20:006099void WebContentsImpl::EnsureOpenerProxiesExist(RenderFrameHost* source_rfh) {
alexmose7da5a12015-04-09 02:22:166100 WebContentsImpl* source_web_contents = static_cast<WebContentsImpl*>(
6101 WebContents::FromRenderFrameHost(source_rfh));
[email protected]f546640b2012-05-15 00:03:496102
alexmose7da5a12015-04-09 02:22:166103 if (source_web_contents) {
lazyboy6ec48b2a2015-06-29 15:18:146104 // If this message is going to outer WebContents from inner WebContents,
6105 // then we should not create a RenderView. AttachToOuterWebContentsFrame()
6106 // already created a RenderFrameProxyHost for that purpose.
6107 if (GetBrowserPluginEmbedder() &&
ekaramad1beabecc2016-09-23 20:18:176108 GuestMode::IsCrossProcessFrameGuest(source_web_contents)) {
lazyboy6ec48b2a2015-06-29 15:18:146109 return;
6110 }
6111
lfg812874d2016-02-09 22:26:136112 if (this != source_web_contents && GetBrowserPluginGuest()) {
naskob3041b98a42016-03-12 04:43:066113 // We create a RenderFrameProxyHost for the embedder in the guest's render
6114 // process but we intentionally do not expose the embedder's opener chain
6115 // to it.
6116 source_web_contents->GetRenderManager()->CreateRenderFrameProxy(
6117 GetSiteInstance());
[email protected]f546640b2012-05-15 00:03:496118 } else {
alexmos58729042015-06-18 23:20:006119 RenderFrameHostImpl* source_rfhi =
6120 static_cast<RenderFrameHostImpl*>(source_rfh);
6121 source_rfhi->frame_tree_node()->render_manager()->CreateOpenerProxies(
alexmos90325cf2015-09-02 17:18:396122 GetSiteInstance(), nullptr);
[email protected]f546640b2012-05-15 00:03:496123 }
6124 }
[email protected]f546640b2012-05-15 00:03:496125}
6126
Avi Drissmand18c51972018-08-15 18:32:096127void WebContentsImpl::ForSecurityDropFullscreen() {
Avi Drissman734edf82019-06-20 22:10:106128 // There are two chains of WebContents to kick out of fullscreen.
6129 //
6130 // Chain 1, the inner/outer WebContents chain. If an inner WebContents has
6131 // done something that requires the browser to drop fullscreen, drop
6132 // fullscreen from it and any outer WebContents that may be in fullscreen.
6133 //
6134 // Chain 2, the opener WebContents chain. If a WebContents has done something
6135 // that requires the browser to drop fullscreen, drop fullscreen from any
6136 // WebContents that was involved in the chain of opening it.
6137 //
6138 // Note that these two chains don't interact, as only a top-level WebContents
6139 // can have an opener. This simplifies things.
6140
6141 WebContents* web_contents = this;
Avi Drissmand18c51972018-08-15 18:32:096142 while (web_contents) {
6143 if (web_contents->IsFullscreenForCurrentTab())
6144 web_contents->ExitFullscreen(true);
Avi Drissman734edf82019-06-20 22:10:106145
6146 if (web_contents->HasOriginalOpener())
6147 web_contents = FromRenderFrameHost(web_contents->GetOriginalOpener());
6148 else
6149 web_contents = web_contents->GetOuterWebContents();
Avi Drissmand18c51972018-08-15 18:32:096150 }
6151}
6152
avallee9993fca2016-11-17 06:16:506153void WebContentsImpl::SetAsFocusedWebContentsIfNecessary() {
6154 // Only change focus if we are not currently focused.
6155 WebContentsImpl* old_contents = GetFocusedWebContents();
6156 if (old_contents == this)
6157 return;
6158
paulmeyerfeafc2d2017-04-25 21:46:406159 GetOutermostWebContents()->node_.SetFocusedWebContents(this);
6160
Greg Thompsonfb957602017-09-04 10:25:526161 if (!GuestMode::IsCrossProcessFrameGuest(this) && browser_plugin_guest_)
paulmeyerfeafc2d2017-04-25 21:46:406162 return;
6163
avallee9993fca2016-11-17 06:16:506164 // Send a page level blur to the old contents so that it displays inactive UI
6165 // and focus this contents to activate it.
6166 if (old_contents)
6167 old_contents->GetMainFrame()->GetRenderWidgetHost()->SetPageFocus(false);
avalleec2ff1ef2017-01-21 00:12:306168
6169 // Make sure the outer web contents knows our frame is focused. Otherwise, the
6170 // outer renderer could have the element before or after the frame element
6171 // focused which would return early without actually advancing focus.
avalleed68cdd12017-06-21 22:07:276172 FocusOuterAttachmentFrameChain();
avalleec2ff1ef2017-01-21 00:12:306173
lfg1453e412017-04-11 00:48:506174 if (ShowingInterstitialPage()) {
creisadca9862017-06-16 19:12:116175 static_cast<RenderFrameHostImpl*>(interstitial_page_->GetMainFrame())
lfg1453e412017-04-11 00:48:506176 ->GetRenderWidgetHost()
6177 ->SetPageFocus(true);
6178 } else {
6179 GetMainFrame()->GetRenderWidgetHost()->SetPageFocus(true);
6180 }
avallee9993fca2016-11-17 06:16:506181}
6182
avallee0206f782016-07-28 18:55:336183void WebContentsImpl::SetFocusedFrame(FrameTreeNode* node,
6184 SiteInstance* source) {
avallee0206f782016-07-28 18:55:336185 frame_tree_.SetFocusedFrame(node, source);
avalleed68cdd12017-06-21 22:07:276186
Lucas Furukawa Gadaniabbfa342017-08-29 19:37:576187 if (auto* inner_contents = node_.GetInnerWebContentsInFrame(node)) {
6188 // |this| is an outer WebContents and |node| represents an inner
6189 // WebContents. Transfer the focus to the inner contents if |this| is
6190 // focused.
6191 if (GetFocusedWebContents() == this)
6192 inner_contents->SetAsFocusedWebContentsIfNecessary();
6193 } else if (node_.OuterContentsFrameTreeNode() &&
6194 node_.OuterContentsFrameTreeNode()
6195 ->current_frame_host()
6196 ->GetSiteInstance() == source) {
6197 // |this| is an inner WebContents, |node| is its main FrameTreeNode and
6198 // the outer WebContents FrameTreeNode is at |source|'s SiteInstance.
6199 // Transfer the focus to the inner WebContents if the outer WebContents is
6200 // focused. This branch is used when an inner WebContents is focused through
Lowell Mannersa9530382019-08-01 09:46:576201 // its RenderFrameProxyHost (via FrameFocused mojo call, used to
Lucas Furukawa Gadaniabbfa342017-08-29 19:37:576202 // implement the window.focus() API).
6203 if (GetFocusedWebContents() == GetOuterWebContents())
6204 SetAsFocusedWebContentsIfNecessary();
6205 } else if (!GetOuterWebContents()) {
6206 // This is an outermost WebContents.
6207 SetAsFocusedWebContentsIfNecessary();
6208 } else if (!GuestMode::IsCrossProcessFrameGuest(this) &&
6209 GetOuterWebContents()) {
6210 // TODO(lfg, paulmeyer): Allows BrowserPlugins to set themselves as the
6211 // focused WebContents. This works around a bug in FindRequestManager that
6212 // doesn't support properly traversing BrowserPlugins.
6213 SetAsFocusedWebContentsIfNecessary();
6214 }
avalleed68cdd12017-06-21 22:07:276215}
6216
Avi Drissman36f801f2018-02-01 20:06:046217void WebContentsImpl::DidCallFocus() {
6218 // Any explicit focusing of another window while this WebContents is in
6219 // fullscreen can be used to confuse the user, so drop fullscreen.
Avi Drissmand18c51972018-08-15 18:32:096220 ForSecurityDropFullscreen();
Avi Drissman36f801f2018-02-01 20:06:046221}
6222
avalleed68cdd12017-06-21 22:07:276223RenderFrameHost* WebContentsImpl::GetFocusedFrameIncludingInnerWebContents() {
6224 WebContentsImpl* contents = this;
6225 FrameTreeNode* focused_node = contents->frame_tree_.GetFocusedFrame();
6226
6227 // If there is no focused frame in the outer WebContents, we need to return
6228 // null.
6229 if (!focused_node)
6230 return nullptr;
6231
6232 // If the focused frame is embedding an inner WebContents, we must descend
6233 // into that contents. If the current WebContents does not have a focused
6234 // frame, return the main frame of this contents instead of the focused empty
6235 // frame embedding this contents.
6236 while (true) {
6237 contents = contents->node_.GetInnerWebContentsInFrame(focused_node);
6238 if (!contents)
6239 return focused_node->current_frame_host();
6240
6241 focused_node = contents->frame_tree_.GetFocusedFrame();
6242 if (!focused_node)
6243 return contents->GetMainFrame();
6244 }
avallee0206f782016-07-28 18:55:336245}
6246
Lucas Furukawa Gadaniabbfa342017-08-29 19:37:576247void WebContentsImpl::OnAdvanceFocus(RenderFrameHostImpl* source_rfh) {
6248 // When a RenderFrame needs to advance focus to a RenderFrameProxy (by hitting
6249 // TAB), the RenderFrameProxy sends an IPC to RenderFrameProxyHost. When this
6250 // RenderFrameProxyHost represents an inner WebContents, the outer WebContents
6251 // needs to focus the inner WebContents.
6252 if (GetOuterWebContents() &&
6253 GetOuterWebContents() == source_rfh->delegate()->GetAsWebContents() &&
6254 GetFocusedWebContents() == GetOuterWebContents()) {
6255 SetAsFocusedWebContentsIfNecessary();
6256 }
6257}
6258
ekaramada110f642016-12-21 19:47:286259void WebContentsImpl::OnFocusedElementChangedInFrame(
6260 RenderFrameHostImpl* frame,
6261 const gfx::Rect& bounds_in_root_view) {
6262 RenderWidgetHostViewBase* root_view =
6263 static_cast<RenderWidgetHostViewBase*>(GetRenderWidgetHostView());
6264 if (!root_view || !frame->GetView())
6265 return;
6266
6267 // Converting to screen coordinates.
6268 gfx::Point origin = bounds_in_root_view.origin();
6269 origin += root_view->GetViewBounds().OffsetFromOrigin();
6270 gfx::Rect bounds_in_screen(origin, bounds_in_root_view.size());
6271
6272 root_view->FocusedNodeChanged(frame->has_focused_editable_element(),
6273 bounds_in_screen);
6274
6275 FocusedNodeDetails details = {frame->has_focused_editable_element(),
6276 bounds_in_screen};
6277
6278 // TODO(ekaramad): We should replace this with an observer notification
6279 // (https://crbug.com/675975).
6280 NotificationService::current()->Notify(
6281 NOTIFICATION_FOCUS_CHANGED_IN_PAGE,
6282 Source<RenderViewHost>(GetRenderViewHost()),
6283 Details<FocusedNodeDetails>(&details));
Jeff Fisher709b4c52019-07-29 20:07:346284
6285 for (auto& observer : observers_)
6286 observer.OnFocusChangedInPage(&details);
ekaramada110f642016-12-21 19:47:286287}
6288
Lowell Manners1de5242e2019-04-25 10:18:466289bool WebContentsImpl::DidAddMessageToConsole(
6290 blink::mojom::ConsoleMessageLevel log_level,
6291 const base::string16& message,
6292 int32_t line_no,
6293 const base::string16& source_id) {
[email protected]a796f202012-05-30 14:14:256294 if (!delegate_)
6295 return false;
Lowell Manners1de5242e2019-04-25 10:18:466296 return delegate_->DidAddMessageToConsole(this, log_level, message, line_no,
avia90ae4e2016-11-11 20:49:336297 source_id);
[email protected]a796f202012-05-30 14:14:256298}
6299
Daniel Cheng90196c82018-04-25 21:49:146300void WebContentsImpl::DidReceiveInputEvent(
dominickn9c7ceaa32016-02-27 01:52:536301 RenderWidgetHostImpl* render_widget_host,
6302 const blink::WebInputEvent::Type type) {
Dominick Ng37a15052018-07-17 23:56:356303 if (!IsUserInteractionInputType(type))
Daniel Cheng90196c82018-04-25 21:49:146304 return;
6305
kenrb19221852016-04-29 17:21:406306 // Ignore unless the widget is currently in the frame tree.
6307 if (!HasMatchingWidgetHost(&frame_tree_, render_widget_host))
avic3aa8422015-11-09 20:57:226308 return;
6309
Daniel Cheng90196c82018-04-25 21:49:146310 if (type != blink::WebInputEvent::kGestureScrollBegin)
6311 last_interactive_input_event_time_ = ui::EventTimeForNow();
dominickn9c7ceaa32016-02-27 01:52:536312
Dominick Ng37a15052018-07-17 23:56:356313 for (auto& observer : observers_)
6314 observer.DidGetUserInteraction(type);
dominickn2dd142dd2015-10-29 05:30:506315}
6316
Avi Drissman738ea192018-08-29 20:24:166317bool WebContentsImpl::ShouldIgnoreInputEvents() {
6318 WebContentsImpl* web_contents = this;
6319 while (web_contents) {
6320 if (web_contents->ignore_input_events_)
6321 return true;
6322 web_contents = web_contents->GetOuterWebContents();
6323 }
6324
6325 return false;
6326}
6327
avallee9993fca2016-11-17 06:16:506328void WebContentsImpl::FocusOwningWebContents(
6329 RenderWidgetHostImpl* render_widget_host) {
6330 // The PDF plugin still runs as a BrowserPlugin and must go through the
6331 // input redirection mechanism. It must not become focused direcly.
6332 if (!GuestMode::IsCrossProcessFrameGuest(this) && browser_plugin_guest_)
6333 return;
6334
6335 RenderWidgetHostImpl* focused_widget =
6336 GetFocusedRenderWidgetHost(render_widget_host);
6337
6338 if (focused_widget != render_widget_host &&
avallee8e881592016-11-19 00:39:306339 (!focused_widget ||
6340 focused_widget->delegate() != render_widget_host->delegate())) {
avallee9993fca2016-11-17 06:16:506341 SetAsFocusedWebContentsIfNecessary();
6342 }
6343}
6344
[email protected]b172aee2012-04-10 17:05:266345void WebContentsImpl::OnIgnoredUIEvent() {
[email protected]31145092011-09-30 01:49:446346 // Notify observers.
ericwilligersde386342016-10-19 02:35:096347 for (auto& observer : observers_)
6348 observer.DidGetIgnoredUIEvent();
[email protected]fa1cf0b82010-01-15 21:49:446349}
6350
avic3aa8422015-11-09 20:57:226351void WebContentsImpl::RendererUnresponsive(
Lukasz Anforowicz52b93722018-06-20 16:11:396352 RenderWidgetHostImpl* render_widget_host,
6353 base::RepeatingClosure hang_monitor_restarter) {
avic031d392017-03-03 03:09:426354 if (ShouldIgnoreUnresponsiveRenderer())
[email protected]e5fc1632011-08-08 07:51:536355 return;
6356
Lukasz Anforowicz4f0593d2018-09-21 18:33:546357 // Do not report hangs (to task manager, to hang renderer dialog, etc.) for
6358 // invisible tabs (like extension background page, background tabs). See
6359 // https://crbug.com/881812 for rationale and for choosing the visibility
6360 // (rather than process priority) as the signal here.
6361 if (GetVisibility() != Visibility::VISIBLE)
6362 return;
6363
Avi Drissman8920def2018-01-31 19:53:366364 if (!render_widget_host->renderer_initialized())
[email protected]55452902011-06-01 21:57:476365 return;
6366
Lukasz Anforowicz4f0593d2018-09-21 18:33:546367 for (auto& observer : observers_)
6368 observer.OnRendererUnresponsive(render_widget_host->GetProcess());
6369
Dave Tapuskac6146c52017-12-20 22:48:156370 if (delegate_)
Lukasz Anforowicz52b93722018-06-20 16:11:396371 delegate_->RendererUnresponsive(this, render_widget_host,
6372 std::move(hang_monitor_restarter));
[email protected]420ae012009-04-24 05:16:326373}
6374
avic3aa8422015-11-09 20:57:226375void WebContentsImpl::RendererResponsive(
6376 RenderWidgetHostImpl* render_widget_host) {
[email protected]6934a702011-12-20 00:04:516377 if (delegate_)
Avi Drissman1572e8c3c2018-02-02 19:06:366378 delegate_->RendererResponsive(this, render_widget_host);
[email protected]420ae012009-04-24 05:16:326379}
6380
Alex Moshchuk7b4f0652019-05-30 18:54:416381void WebContentsImpl::SubframeCrashed(
6382 blink::mojom::FrameVisibility visibility) {
6383 // If a subframe crashed on a hidden tab, mark the tab for reload to avoid
6384 // showing a sad frame to the user if they ever switch back to that tab. Do
6385 // this for subframes that are either visible in viewport or visible but
6386 // scrolled out of view, but skip subframes that are not rendered (e.g., via
6387 // "display:none"), since in that case the user wouldn't see a sad frame
6388 // anyway.
6389 bool did_mark_for_reload = false;
6390 if (IsHidden() && visibility != blink::mojom::FrameVisibility::kNotRendered &&
6391 base::FeatureList::IsEnabled(
6392 features::kReloadHiddenTabsWithCrashedSubframes)) {
6393 controller_.SetNeedsReload(
6394 NavigationControllerImpl::NeedsReloadType::kCrashedSubframe);
6395 did_mark_for_reload = true;
6396 UMA_HISTOGRAM_ENUMERATION(
6397 "Stability.ChildFrameCrash.TabMarkedForReload.Visibility", visibility);
6398 }
6399
6400 UMA_HISTOGRAM_BOOLEAN("Stability.ChildFrameCrash.TabMarkedForReload",
6401 did_mark_for_reload);
6402}
6403
[email protected]b172aee2012-04-10 17:05:266404void WebContentsImpl::BeforeUnloadFiredFromRenderManager(
[email protected]feaded02012-09-25 19:12:556405 bool proceed, const base::TimeTicks& proceed_time,
[email protected]420ae012009-04-24 05:16:326406 bool* proceed_to_fire_unload) {
ericwilligersde386342016-10-19 02:35:096407 for (auto& observer : observers_)
Chris Hamilton3b3e315d2018-09-19 13:16:216408 observer.BeforeUnloadFired(proceed, proceed_time);
[email protected]6934a702011-12-20 00:04:516409 if (delegate_)
6410 delegate_->BeforeUnloadFired(this, proceed, proceed_to_fire_unload);
[email protected]e80af492013-06-24 21:52:096411 // Note: |this| might be deleted at this point.
[email protected]420ae012009-04-24 05:16:326412}
6413
[email protected]58d5cfe2013-07-10 02:40:526414void WebContentsImpl::RenderProcessGoneFromRenderManager(
[email protected]3a3d47472010-07-15 21:03:546415 RenderViewHost* render_view_host) {
[email protected]443b80e2010-12-14 00:42:236416 DCHECK(crashed_status_ != base::TERMINATION_STATUS_STILL_RUNNING);
[email protected]ec6a7eb2013-04-22 17:34:226417 RenderViewTerminated(render_view_host, crashed_status_, crashed_error_code_);
[email protected]3a3d47472010-07-15 21:03:546418}
6419
Jianzhou Fengd8720c72018-01-18 03:06:306420void WebContentsImpl::UpdateRenderViewSizeForRenderManager(bool is_main_frame) {
[email protected]8643e6d2012-01-18 20:26:106421 // TODO(brettw) this is a hack. See WebContentsView::SizeContents.
Jianzhou Fengd8720c72018-01-18 03:06:306422 gfx::Size size = GetSizeForNewRenderView(is_main_frame);
[email protected]0323ee42010-02-17 22:03:266423 // 0x0 isn't a valid window size (minimal window size is 1x1) but it may be
6424 // here during container initialization and normal window size will be set
6425 // later. In case of tab duplication this resizing to 0x0 prevents setting
6426 // normal size later so just ignore it.
6427 if (!size.IsEmpty())
6428 view_->SizeContents(size);
[email protected]420ae012009-04-24 05:16:326429}
6430
[email protected]af905902013-10-01 21:38:516431void WebContentsImpl::CancelModalDialogsForRenderManager() {
6432 // We need to cancel modal dialogs when doing a process swap, since the load
avi2460c762015-04-17 15:21:546433 // deferrer would prevent us from swapping out. We also clear the state
6434 // because this is a cross-process navigation, which means that it's a new
6435 // site that should not have to pay for the sins of its predecessor.
creis89a0f782015-05-27 16:13:176436 //
Lei Zhang6410ec1a2019-01-31 21:38:586437 // Note that we don't bother telling |browser_plugin_embedder_| because the
creis89a0f782015-05-27 16:13:176438 // cross-process navigation will either destroy the browser plugins or not
6439 // require their dialogs to close.
avi5d3b8692016-10-12 22:00:466440 if (dialog_manager_) {
avi6879fcf2017-01-21 05:27:536441 dialog_manager_->CancelDialogs(this, /*reset_state=*/true);
avi5d3b8692016-10-12 22:00:466442 }
[email protected]af905902013-10-01 21:38:516443}
6444
[email protected]02d7b6e2014-06-24 21:01:506445void WebContentsImpl::NotifySwappedFromRenderManager(RenderFrameHost* old_host,
6446 RenderFrameHost* new_host,
6447 bool is_main_frame) {
6448 if (is_main_frame) {
naskof5940b9f2015-03-02 23:04:056449 NotifyViewSwapped(old_host ? old_host->GetRenderViewHost() : nullptr,
[email protected]02d7b6e2014-06-24 21:01:506450 new_host->GetRenderViewHost());
[email protected]a6b73c62013-02-11 23:05:086451
[email protected]02d7b6e2014-06-24 21:01:506452 // Make sure the visible RVH reflects the new delegate's preferences.
6453 if (delegate_)
6454 view_->SetOverscrollControllerEnabled(CanOverscrollContent());
[email protected]d56a49e2013-02-23 00:47:396455
dmazzoni62111452017-04-28 23:46:556456 RenderWidgetHostViewBase* rwhv =
6457 static_cast<RenderWidgetHostViewBase*>(GetRenderWidgetHostView());
6458 if (rwhv)
6459 rwhv->SetMainFrameAXTreeID(GetMainFrame()->GetAXTreeID());
[email protected]02d7b6e2014-06-24 21:01:506460 }
6461
Jinsuk Kim895b2c52018-07-03 06:49:166462 NotifyFrameSwapped(old_host, new_host, is_main_frame);
[email protected]3a3d47472010-07-15 21:03:546463}
6464
naskof5940b9f2015-03-02 23:04:056465void WebContentsImpl::NotifyMainFrameSwappedFromRenderManager(
Jinsuk Kim895b2c52018-07-03 06:49:166466 RenderFrameHost* old_host,
6467 RenderFrameHost* new_host) {
6468 NotifyViewSwapped(old_host ? old_host->GetRenderViewHost() : nullptr,
6469 new_host->GetRenderViewHost());
naskof5940b9f2015-03-02 23:04:056470}
6471
[email protected]b172aee2012-04-10 17:05:266472NavigationControllerImpl& WebContentsImpl::GetControllerForRenderManager() {
[email protected]d1198fd2012-08-13 22:50:196473 return GetController();
[email protected]3a3d47472010-07-15 21:03:546474}
6475
dcheng59716272016-04-09 05:19:086476std::unique_ptr<WebUIImpl> WebContentsImpl::CreateWebUIForRenderFrameHost(
carlosk37f793792014-11-27 15:08:296477 const GURL& url) {
Dan Beam055fe8f2017-10-04 23:23:366478 return CreateWebUI(url);
[email protected]420ae012009-04-24 05:16:326479}
6480
[email protected]10f417c52011-12-28 21:04:236481NavigationEntry*
[email protected]b172aee2012-04-10 17:05:266482 WebContentsImpl::GetLastCommittedNavigationEntryForRenderManager() {
[email protected]420ae012009-04-24 05:16:326483 return controller_.GetLastCommittedEntry();
6484}
6485
creisadca9862017-06-16 19:12:116486InterstitialPageImpl* WebContentsImpl::GetInterstitialForRenderManager() {
6487 return interstitial_page_;
6488}
6489
lfgbede6392015-09-11 21:54:066490void WebContentsImpl::CreateRenderWidgetHostViewForRenderManager(
6491 RenderViewHost* render_view_host) {
lfgc740d4b22016-04-15 16:45:336492 RenderWidgetHostViewBase* rwh_view =
6493 view_->CreateViewForWidget(render_view_host->GetWidget(), false);
[email protected]1a98a932009-11-17 00:12:526494
[email protected]420ae012009-04-24 05:16:326495 // Now that the RenderView has been created, we need to tell it its size.
[email protected]245f7d52011-11-28 15:36:446496 if (rwh_view)
Jianzhou Fengd8720c72018-01-18 03:06:306497 rwh_view->SetSize(GetSizeForNewRenderView(true));
lfgbede6392015-09-11 21:54:066498}
6499
6500bool WebContentsImpl::CreateRenderViewForRenderManager(
6501 RenderViewHost* render_view_host,
6502 int opener_frame_routing_id,
6503 int proxy_routing_id,
Pavel Feldman25234722017-10-11 02:49:066504 const base::UnguessableToken& devtools_frame_token,
lfgbede6392015-09-11 21:54:066505 const FrameReplicationState& replicated_frame_state) {
6506 TRACE_EVENT0("browser,navigation",
6507 "WebContentsImpl::CreateRenderViewForRenderManager");
6508
piman00cf9402017-04-03 17:09:206509 if (proxy_routing_id == MSG_ROUTING_NONE)
lfg9431efd2016-12-14 14:45:266510 CreateRenderWidgetHostViewForRenderManager(render_view_host);
lfg9431efd2016-12-14 14:45:266511
piman00cf9402017-04-03 17:09:206512 if (!static_cast<RenderViewHostImpl*>(render_view_host)
6513 ->CreateRenderView(opener_frame_routing_id, proxy_routing_id,
Pavel Feldman25234722017-10-11 02:49:066514 devtools_frame_token, replicated_frame_state,
6515 created_with_opener_)) {
[email protected]a4127722011-04-27 23:13:526516 return false;
[email protected]14392a52012-05-02 20:28:446517 }
W. James MacLean62198672019-06-04 16:46:106518 // Set the TextAutosizer state from the main frame's renderer on the new view,
6519 // but only if it's not for the main frame. Main frame renderers should create
6520 // this state themselves from up-to-date values, so we shouldn't override it
6521 // with the cached values.
6522 if (!render_view_host->GetMainFrame()) {
6523 render_view_host->Send(
6524 new PageMsg_UpdateTextAutosizerPageInfoForRemoteMainFrames(
6525 render_view_host->GetRoutingID(), text_autosizer_page_info_));
6526 }
[email protected]a4127722011-04-27 23:13:526527
Lucas Furukawa Gadaniaed1fed2017-10-13 17:34:146528 if (proxy_routing_id == MSG_ROUTING_NONE && node_.outer_web_contents())
6529 ReattachToOuterWebContentsFrame();
6530
avi2b177592014-12-10 02:08:026531 SetHistoryOffsetAndLengthForView(render_view_host,
6532 controller_.GetLastCommittedEntryIndex(),
6533 controller_.GetEntryCount());
6534
[email protected]a220b5932013-09-21 03:47:446535#if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID)
[email protected]f8233cc2011-05-31 20:24:506536 // Force a ViewMsg_Resize to be sent, needed to make plugins show up on
6537 // linux. See crbug.com/83941.
avif9ab5d942015-10-15 14:05:446538 RenderWidgetHostView* rwh_view = render_view_host->GetWidget()->GetView();
[email protected]245f7d52011-11-28 15:36:446539 if (rwh_view) {
6540 if (RenderWidgetHost* render_widget_host = rwh_view->GetRenderWidgetHost())
Fady Samuel0b911822018-04-25 13:22:166541 render_widget_host->SynchronizeVisualProperties();
[email protected]245f7d52011-11-28 15:36:446542 }
[email protected]f8233cc2011-05-31 20:24:506543#endif
6544
[email protected]420ae012009-04-24 05:16:326545 return true;
6546}
6547
[email protected]82307f6b2014-08-07 03:30:126548bool WebContentsImpl::CreateRenderFrameForRenderManager(
6549 RenderFrameHost* render_frame_host,
arthursonzogni1efc7b962019-03-14 09:36:336550 int previous_routing_id,
alexmosa181efc2015-09-03 00:39:046551 int opener_routing_id,
japhete6adf142014-10-31 00:01:496552 int parent_routing_id,
alexmosa181efc2015-09-03 00:39:046553 int previous_sibling_routing_id) {
naskob8744d22014-08-28 17:07:436554 TRACE_EVENT0("browser,navigation",
6555 "WebContentsImpl::CreateRenderFrameForRenderManager");
[email protected]82307f6b2014-08-07 03:30:126556
6557 RenderFrameHostImpl* rfh =
6558 static_cast<RenderFrameHostImpl*>(render_frame_host);
arthursonzogni1efc7b962019-03-14 09:36:336559 if (!rfh->CreateRenderFrame(previous_routing_id, opener_routing_id,
alexmosa181efc2015-09-03 00:39:046560 parent_routing_id, previous_sibling_routing_id))
[email protected]82307f6b2014-08-07 03:30:126561 return false;
6562
6563 // TODO(nasko): When RenderWidgetHost is owned by RenderFrameHost, the passed
6564 // RenderFrameHost will have to be associated with the appropriate
6565 // RenderWidgetHostView or a new one should be created here.
6566
6567 return true;
6568}
6569
mfomitchev2b8b066a2016-01-28 19:23:156570#if defined(OS_ANDROID)
[email protected]fc2b46b2014-05-03 16:33:456571
[email protected]155c7f22013-12-09 17:07:186572base::android::ScopedJavaLocalRef<jobject>
6573WebContentsImpl::GetJavaWebContents() {
mostynb042582e2015-03-16 22:13:406574 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]0e813a52014-08-13 10:34:566575 return GetWebContentsAndroid()->GetJavaObject();
6576}
[email protected]155c7f22013-12-09 17:07:186577
[email protected]0e813a52014-08-13 10:34:566578WebContentsAndroid* WebContentsImpl::GetWebContentsAndroid() {
Kevin McNee576bb5d82019-05-29 16:47:516579 if (!web_contents_android_) {
6580 web_contents_android_ = std::make_unique<WebContentsAndroid>(this);
[email protected]155c7f22013-12-09 17:07:186581 }
Kevin McNee576bb5d82019-05-29 16:47:516582 return web_contents_android_.get();
6583}
6584
6585void WebContentsImpl::ClearWebContentsAndroid() {
6586 DCHECK_CURRENTLY_ON(BrowserThread::UI);
6587 web_contents_android_.reset();
[email protected]155c7f22013-12-09 17:07:186588}
6589
paulmeyerc0b762b2016-04-13 11:55:176590void WebContentsImpl::ActivateNearestFindResult(float x,
6591 float y) {
6592 GetOrCreateFindRequestManager()->ActivateNearestFindResult(x, y);
6593}
6594
6595void WebContentsImpl::RequestFindMatchRects(int current_version) {
6596 GetOrCreateFindRequestManager()->RequestFindMatchRects(current_version);
6597}
6598
[email protected]7d244f12013-08-22 21:41:516599bool WebContentsImpl::CreateRenderViewForInitialEmptyDocument() {
nasko4c0feb62015-06-05 18:37:066600 return CreateRenderViewForRenderManager(
6601 GetRenderViewHost(), MSG_ROUTING_NONE, MSG_ROUTING_NONE,
Pavel Feldman25234722017-10-11 02:49:066602 frame_tree_.root()->devtools_frame_token(),
lfgbede6392015-09-11 21:54:066603 frame_tree_.root()->current_replication_state());
[email protected]7d244f12013-08-22 21:41:516604}
[email protected]fc2b46b2014-05-03 16:33:456605
rockot400ea35b2016-10-15 19:15:326606service_manager::InterfaceProvider* WebContentsImpl::GetJavaInterfaces() {
sammcf5f1b0f2016-09-20 23:05:116607 if (!java_interfaces_) {
rockot400ea35b2016-10-15 19:15:326608 service_manager::mojom::InterfaceProviderPtr provider;
blundelle0a9f1582016-12-20 11:23:326609 BindInterfaceRegistryForWebContents(mojo::MakeRequest(&provider), this);
rockot400ea35b2016-10-15 19:15:326610 java_interfaces_.reset(new service_manager::InterfaceProvider);
sammcf5f1b0f2016-09-20 23:05:116611 java_interfaces_->Bind(std::move(provider));
6612 }
6613 return java_interfaces_.get();
6614}
6615
Francois Doray6c26f3342018-08-24 17:17:226616#endif
6617
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:416618bool WebContentsImpl::CompletedFirstVisuallyNonEmptyPaint() {
spqchane72c3cb2017-06-30 23:02:106619 return did_first_visually_non_empty_paint_;
6620}
6621
pkotwicz75ca8ffd2016-02-16 23:10:196622void WebContentsImpl::OnDidDownloadImage(
Avi Drissmana5a52dd2018-03-27 03:39:026623 ImageDownloadCallback callback,
pkotwicz75ca8ffd2016-02-16 23:10:196624 int id,
6625 const GURL& image_url,
leon.hancbc4bc3c2016-02-26 07:08:526626 int32_t http_status_code,
leon.hanc2228532016-08-16 05:59:186627 const std::vector<SkBitmap>& images,
6628 const std::vector<gfx::Size>& original_image_sizes) {
Avi Drissmana5a52dd2018-03-27 03:39:026629 std::move(callback).Run(id, http_status_code, image_url, images,
6630 original_image_sizes);
pkotwicz75ca8ffd2016-02-16 23:10:196631}
6632
[email protected]6fba26d2014-04-29 09:38:286633void WebContentsImpl::OnDialogClosed(int render_process_id,
6634 int render_frame_id,
[email protected]b172aee2012-04-10 17:05:266635 IPC::Message* reply_msg,
[email protected]87de04b02014-04-08 22:14:496636 bool dialog_was_suppressed,
[email protected]b172aee2012-04-10 17:05:266637 bool success,
[email protected]fcf75d42013-12-03 20:11:266638 const base::string16& user_input) {
[email protected]6fba26d2014-04-29 09:38:286639 RenderFrameHostImpl* rfh = RenderFrameHostImpl::FromID(render_process_id,
6640 render_frame_id);
[email protected]87de04b02014-04-08 22:14:496641 last_dialog_suppressed_ = dialog_was_suppressed;
6642
Charlie Reis96077362019-07-04 00:34:046643 javascript_dialog_navigation_deferrer_.reset();
6644
[email protected]beb440c2009-11-06 04:08:546645 if (is_showing_before_unload_dialog_ && !success) {
clamybc6f4152016-04-01 20:14:236646 // It is possible for the current RenderFrameHost to have changed in the
6647 // meantime. Do not reset the navigation state in that case.
6648 if (rfh && rfh == rfh->frame_tree_node()->current_frame_host()) {
clamy44e84ce2016-02-22 15:38:256649 rfh->frame_tree_node()->BeforeUnloadCanceled();
clamybc6f4152016-04-01 20:14:236650 controller_.DiscardNonCommittedEntries();
6651 }
[email protected]ec8e8b02013-06-19 04:57:106652
Charles Reis15d60b132017-11-03 17:43:476653 // Update the URL display either way, to avoid showing a stale URL.
6654 NotifyNavigationStateChanged(INVALIDATE_TYPE_URL);
6655
ericwilligersde386342016-10-19 02:35:096656 for (auto& observer : observers_)
6657 observer.BeforeUnloadDialogCancelled();
[email protected]beb440c2009-11-06 04:08:546658 }
[email protected]87de04b02014-04-08 22:14:496659
[email protected]6fba26d2014-04-29 09:38:286660 if (rfh) {
avi44acfaa82017-05-03 22:55:356661 rfh->JavaScriptDialogClosed(reply_msg, success, user_input);
Pavel Feldman3c1842b2017-08-02 05:00:166662
6663 std::vector<protocol::PageHandler*> page_handlers =
6664 protocol::PageHandler::EnabledForWebContents(this);
6665 for (auto* handler : page_handlers)
6666 handler->DidCloseJavaScriptDialog(success, user_input);
6667
[email protected]6fba26d2014-04-29 09:38:286668 } else {
6669 // Don't leak the sync IPC reply if the RFH or process is gone.
6670 delete reply_msg;
6671 }
avi1c51fb62017-02-27 20:43:036672
6673 is_showing_javascript_dialog_ = false;
6674 is_showing_before_unload_dialog_ = false;
[email protected]beb440c2009-11-06 04:08:546675}
6676
[email protected]b172aee2012-04-10 17:05:266677void WebContentsImpl::SetEncoding(const std::string& encoding) {
[email protected]ef3adfc2014-05-11 00:04:546678 if (encoding == last_reported_encoding_)
6679 return;
6680 last_reported_encoding_ = encoding;
6681
jinsukkim79df88a2017-01-04 06:26:156682 canonical_encoding_ = base::GetCanonicalEncodingNameByAliasName(encoding);
[email protected]aacd7cc2009-09-02 14:51:106683}
[email protected]f45d2a72010-03-08 23:28:356684
[email protected]1ac10dca2013-08-20 20:47:046685bool WebContentsImpl::IsHidden() {
Francois Doraye6161152018-03-27 22:05:376686 return !IsBeingCaptured() && visibility_ != Visibility::VISIBLE;
[email protected]1ac10dca2013-08-20 20:47:046687}
6688
rob97250742015-12-10 17:45:156689int WebContentsImpl::GetOuterDelegateFrameTreeNodeId() {
lfg601233692017-03-06 22:45:446690 return node_.outer_contents_frame_tree_node_id();
lazyboy6ec48b2a2015-06-29 15:18:146691}
6692
ekaramadaeb3c5c2016-11-09 03:22:356693RenderWidgetHostImpl* WebContentsImpl::GetFullscreenRenderWidgetHost() const {
6694 return RenderWidgetHostImpl::FromID(fullscreen_widget_process_id_,
6695 fullscreen_widget_routing_id_);
6696}
6697
[email protected]b0936d22013-11-28 06:47:366698RenderFrameHostManager* WebContentsImpl::GetRenderManager() const {
[email protected]fa944cb82013-11-15 17:51:216699 return frame_tree_.root()->render_manager();
6700}
6701
[email protected]f8497342013-02-05 22:15:026702BrowserPluginGuest* WebContentsImpl::GetBrowserPluginGuest() const {
[email protected]7a846df2012-09-20 19:17:396703 return browser_plugin_guest_.get();
6704}
6705
Albert J. Wongc55cc64d2018-10-12 02:50:046706void WebContentsImpl::SetBrowserPluginGuest(
6707 std::unique_ptr<BrowserPluginGuest> guest) {
6708 DCHECK(!browser_plugin_guest_);
6709 DCHECK(guest);
6710 browser_plugin_guest_ = std::move(guest);
[email protected]738f57a2013-06-29 21:06:546711}
6712
Aidan Wolter52bcc12a2018-11-14 03:27:296713base::UnguessableToken WebContentsImpl::GetAudioGroupId() {
6714 if (!base::FeatureList::IsEnabled(features::kAudioServiceAudioStreams))
6715 return base::UnguessableToken::Null();
6716
6717 return GetAudioStreamFactory()->group_id();
6718}
6719
Sofiya Semenova5e9d9122019-07-11 02:46:416720ukm::SourceId WebContentsImpl::GetLastCommittedSourceId() {
6721 return last_committed_source_id_;
6722}
6723
[email protected]f8497342013-02-05 22:15:026724BrowserPluginEmbedder* WebContentsImpl::GetBrowserPluginEmbedder() const {
[email protected]7a846df2012-09-20 19:17:396725 return browser_plugin_embedder_.get();
6726}
[email protected]8ff00d72012-10-23 19:12:216727
fsamuel60b42282015-03-10 03:29:146728void WebContentsImpl::CreateBrowserPluginEmbedderIfNecessary() {
6729 if (browser_plugin_embedder_)
6730 return;
6731 browser_plugin_embedder_.reset(BrowserPluginEmbedder::Create(this));
6732}
6733
Jianzhou Fengd8720c72018-01-18 03:06:306734gfx::Size WebContentsImpl::GetSizeForNewRenderView(bool is_main_frame) {
[email protected]dc0fd432013-08-27 15:29:216735 gfx::Size size;
Jianzhou Fengd8720c72018-01-18 03:06:306736 if (is_main_frame)
6737 size = device_emulation_size_;
6738 if (size.IsEmpty() && delegate_)
[email protected]dc0fd432013-08-27 15:29:216739 size = delegate_->GetSizeForNewRenderView(this);
6740 if (size.IsEmpty())
[email protected]fc2b46b2014-05-03 16:33:456741 size = GetContainerBounds().size();
[email protected]dc0fd432013-08-27 15:29:216742 return size;
6743}
6744
[email protected]ae7eeda2014-07-04 01:53:266745void WebContentsImpl::OnFrameRemoved(RenderFrameHost* render_frame_host) {
ericwilligersde386342016-10-19 02:35:096746 for (auto& observer : observers_)
6747 observer.FrameDeleted(render_frame_host);
[email protected]9b159a52013-10-03 17:24:556748}
6749
[email protected]222f5822014-02-05 23:40:496750void WebContentsImpl::OnPreferredSizeChanged(const gfx::Size& old_size) {
6751 if (!delegate_)
6752 return;
6753 const gfx::Size new_size = GetPreferredSize();
6754 if (new_size != old_size)
6755 delegate_->UpdatePreferredSize(this, new_size);
6756}
6757
Dan Beam055fe8f2017-10-04 23:23:366758std::unique_ptr<WebUIImpl> WebContentsImpl::CreateWebUI(const GURL& url) {
Jeremy Roman04f27c372017-10-27 15:20:556759 std::unique_ptr<WebUIImpl> web_ui = std::make_unique<WebUIImpl>(this);
Lei Zhangee3b63ad2018-06-06 23:25:316760 std::unique_ptr<WebUIController> controller(
avie865b1d2016-10-24 19:42:596761 WebUIControllerFactoryRegistry::GetInstance()
Lei Zhangee3b63ad2018-06-06 23:25:316762 ->CreateWebUIControllerForURL(web_ui.get(), url));
dbeama1b926a2015-08-31 23:17:516763 if (controller) {
Jeremy Roman04f27c372017-10-27 15:20:556764 web_ui->AddMessageHandler(std::make_unique<GenericHandler>());
Lei Zhangee3b63ad2018-06-06 23:25:316765 web_ui->SetController(std::move(controller));
dbeama1b926a2015-08-31 23:17:516766 return web_ui;
6767 }
6768
avie865b1d2016-10-24 19:42:596769 return nullptr;
dbeama1b926a2015-08-31 23:17:516770}
6771
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:416772FindRequestManager* WebContentsImpl::GetFindRequestManager() {
6773 for (auto* contents = this; contents;
paulmeyerfeafc2d2017-04-25 21:46:406774 contents = contents->GetOuterWebContents()) {
6775 if (contents->find_request_manager_)
6776 return contents->find_request_manager_.get();
6777 }
6778
6779 return nullptr;
6780}
6781
paulmeyerc0b762b2016-04-13 11:55:176782FindRequestManager* WebContentsImpl::GetOrCreateFindRequestManager() {
paulmeyerfeafc2d2017-04-25 21:46:406783 if (FindRequestManager* manager = GetFindRequestManager())
6784 return manager;
6785
Ehsan Karamad6beb2ea2018-11-25 18:15:136786 DCHECK(!browser_plugin_guest_ || GetOuterWebContents());
6787
paulmeyerfeafc2d2017-04-25 21:46:406788 // No existing FindRequestManager found, so one must be created.
6789 find_request_manager_.reset(new FindRequestManager(this));
6790
6791 // Concurrent find sessions must not overlap, so destroy any existing
6792 // FindRequestManagers in any inner WebContentses.
Lucas Furukawa Gadani2ec00c82018-12-14 15:53:166793 for (WebContents* contents : GetWebContentsAndAllInner()) {
6794 auto* web_contents_impl = static_cast<WebContentsImpl*>(contents);
6795 if (web_contents_impl == this)
paulmeyerfeafc2d2017-04-25 21:46:406796 continue;
Lucas Furukawa Gadani2ec00c82018-12-14 15:53:166797 if (web_contents_impl->find_request_manager_) {
6798 web_contents_impl->find_request_manager_->StopFinding(
6799 STOP_FIND_ACTION_CLEAR_SELECTION);
6800 web_contents_impl->find_request_manager_.release();
paulmeyerfeafc2d2017-04-25 21:46:406801 }
6802 }
paulmeyerc0b762b2016-04-13 11:55:176803
6804 return find_request_manager_.get();
6805}
6806
6807void WebContentsImpl::NotifyFindReply(int request_id,
6808 int number_of_matches,
6809 const gfx::Rect& selection_rect,
6810 int active_match_ordinal,
6811 bool final_update) {
Sebastien Marchandcf97ed52017-07-24 23:50:346812 if (delegate_ && !is_being_destroyed_ &&
Lukasz Anforowicze9ae3722017-09-29 17:37:386813 !GetMainFrame()->GetProcess()->FastShutdownStarted()) {
paulmeyerc0b762b2016-04-13 11:55:176814 delegate_->FindReply(this, request_id, number_of_matches, selection_rect,
6815 active_match_ordinal, final_update);
6816 }
6817}
6818
ortuno467e5792016-06-10 04:32:396819void WebContentsImpl::IncrementBluetoothConnectedDeviceCount() {
6820 // Trying to invalidate the tab state while being destroyed could result in a
6821 // use after free.
6822 if (IsBeingDestroyed()) {
6823 return;
6824 }
6825 // Notify for UI updates if the state changes.
6826 bluetooth_connected_device_count_++;
6827 if (bluetooth_connected_device_count_ == 1) {
6828 NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB);
6829 }
6830}
6831
6832void WebContentsImpl::DecrementBluetoothConnectedDeviceCount() {
6833 // Trying to invalidate the tab state while being destroyed could result in a
6834 // use after free.
6835 if (IsBeingDestroyed()) {
6836 return;
6837 }
6838 // Notify for UI updates if the state changes.
Lukasz Anforowicze9ae3722017-09-29 17:37:386839 DCHECK_NE(bluetooth_connected_device_count_, 0u);
ortuno467e5792016-06-10 04:32:396840 bluetooth_connected_device_count_--;
6841 if (bluetooth_connected_device_count_ == 0) {
6842 NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB);
6843 }
6844}
6845
Reilly Grant5e7c79b22019-04-09 17:26:206846void WebContentsImpl::IncrementSerialActiveFrameCount() {
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 serial_active_frame_count_++;
6854 if (serial_active_frame_count_ == 1)
6855 NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB);
6856}
6857
6858void WebContentsImpl::DecrementSerialActiveFrameCount() {
6859 // Trying to invalidate the tab state while being destroyed could result in a
6860 // use after free.
6861 if (IsBeingDestroyed())
6862 return;
6863
6864 // Notify for UI updates if the state changes.
6865 DCHECK_NE(0u, serial_active_frame_count_);
6866 serial_active_frame_count_--;
6867 if (serial_active_frame_count_ == 0)
6868 NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB);
6869}
6870
Marijn Kruisselbrink46fa4bd2019-07-11 18:15:476871void WebContentsImpl::AddNativeFileSystemDirectoryHandle(
6872 const base::FilePath& path) {
Marijn Kruisselbrinkf1714aa22019-07-02 03:45:336873 // Trying to invalidate the tab state while being destroyed could result in a
6874 // use after free.
6875 if (IsBeingDestroyed())
6876 return;
6877
6878 // Notify for UI updates if the state changes. Need both TYPE_TAB and TYPE_URL
6879 // to update both the tab-level usage indicator and the usage indicator in the
6880 // omnibox.
Marijn Kruisselbrink46fa4bd2019-07-11 18:15:476881 const bool was_empty = native_file_system_directory_handles_.empty();
6882 native_file_system_directory_handles_[path]++;
6883 if (was_empty) {
Marijn Kruisselbrinkf1714aa22019-07-02 03:45:336884 NotifyNavigationStateChanged(static_cast<content::InvalidateTypes>(
6885 INVALIDATE_TYPE_TAB | INVALIDATE_TYPE_URL));
6886 }
6887}
6888
Marijn Kruisselbrink46fa4bd2019-07-11 18:15:476889void WebContentsImpl::RemoveNativeFileSystemDirectoryHandle(
6890 const base::FilePath& path) {
Marijn Kruisselbrinkf1714aa22019-07-02 03:45:336891 // Trying to invalidate the tab state while being destroyed could result in a
6892 // use after free.
6893 if (IsBeingDestroyed())
6894 return;
6895
6896 // Notify for UI updates if the state changes. Need both TYPE_TAB and TYPE_URL
6897 // to update both the tab-level usage indicator and the usage indicator in the
6898 // omnibox.
Marijn Kruisselbrink46fa4bd2019-07-11 18:15:476899 auto it = native_file_system_directory_handles_.find(path);
6900 DCHECK(it != native_file_system_directory_handles_.end());
6901 DCHECK_NE(0u, it->second);
6902 it->second--;
6903 if (it->second == 0)
6904 native_file_system_directory_handles_.erase(it);
6905 if (native_file_system_directory_handles_.empty()) {
Marijn Kruisselbrinkf1714aa22019-07-02 03:45:336906 NotifyNavigationStateChanged(static_cast<content::InvalidateTypes>(
6907 INVALIDATE_TYPE_TAB | INVALIDATE_TYPE_URL));
6908 }
6909}
6910
6911void WebContentsImpl::IncrementWritableNativeFileSystemHandleCount() {
6912 // Trying to invalidate the tab state while being destroyed could result in a
6913 // use after free.
6914 if (IsBeingDestroyed())
6915 return;
6916
6917 // Notify for UI updates if the state changes. Need both TYPE_TAB and TYPE_URL
6918 // to update both the tab-level usage indicator and the usage indicator in the
6919 // omnibox.
6920 native_file_system_writable_handle_count_++;
6921 if (native_file_system_writable_handle_count_ == 1) {
6922 NotifyNavigationStateChanged(static_cast<content::InvalidateTypes>(
6923 INVALIDATE_TYPE_TAB | INVALIDATE_TYPE_URL));
6924 }
6925}
6926
6927void WebContentsImpl::DecrementWritableNativeFileSystemHandleCount() {
6928 // Trying to invalidate the tab state while being destroyed could result in a
6929 // use after free.
6930 if (IsBeingDestroyed())
6931 return;
6932
6933 // Notify for UI updates if the state changes. Need both TYPE_TAB and TYPE_URL
6934 // to update both the tab-level usage indicator and the usage indicator in the
6935 // omnibox.
6936 DCHECK_NE(0u, native_file_system_writable_handle_count_);
6937 native_file_system_writable_handle_count_--;
6938 if (native_file_system_writable_handle_count_ == 0) {
6939 NotifyNavigationStateChanged(static_cast<content::InvalidateTypes>(
6940 INVALIDATE_TYPE_TAB | INVALIDATE_TYPE_URL));
6941 }
6942}
6943
zqzhang8ac49002017-03-16 21:51:356944void WebContentsImpl::SetHasPersistentVideo(bool has_persistent_video) {
6945 if (has_persistent_video_ == has_persistent_video)
mlamouri5cd9ae82017-02-18 11:05:096946 return;
6947
zqzhang8ac49002017-03-16 21:51:356948 has_persistent_video_ = has_persistent_video;
mlamouri5cd9ae82017-02-18 11:05:096949 NotifyPreferencesChanged();
zqzhang8ac49002017-03-16 21:51:356950 media_web_contents_observer()->RequestPersistentVideo(has_persistent_video);
mlamouri5cd9ae82017-02-18 11:05:096951}
6952
Ian Vollick5d5c37f2019-06-19 22:50:576953void WebContentsImpl::SetSpatialNavigationDisabled(bool disabled) {
6954 if (is_spatial_navigation_disabled_ == disabled)
6955 return;
6956
6957 is_spatial_navigation_disabled_ = disabled;
6958 NotifyPreferencesChanged();
6959}
6960
paulmeyerfeafc2d2017-04-25 21:46:406961void WebContentsImpl::BrowserPluginGuestWillDetach() {
6962 WebContentsImpl* outermost = GetOutermostWebContents();
6963 if (this != outermost && ContainsOrIsFocusedWebContents())
6964 outermost->SetAsFocusedWebContentsIfNecessary();
6965}
6966
Becca Hughes112832e2019-06-11 17:19:026967PictureInPictureResult WebContentsImpl::EnterPictureInPicture(
Mounir Lamouri11e9ef42018-05-22 03:10:166968 const viz::SurfaceId& surface_id,
6969 const gfx::Size& natural_size) {
François Beaufortf09e4462019-01-18 14:34:516970 return delegate_
6971 ? delegate_->EnterPictureInPicture(this, surface_id, natural_size)
Becca Hughes112832e2019-06-11 17:19:026972 : PictureInPictureResult::kNotSupported;
Mounir Lamouri11e9ef42018-05-22 03:10:166973}
6974
Mounir Lamouri6d759e12018-05-16 20:01:306975void WebContentsImpl::ExitPictureInPicture() {
6976 if (delegate_)
6977 delegate_->ExitPictureInPicture();
6978}
6979
paulmeyerc0b762b2016-04-13 11:55:176980#if defined(OS_ANDROID)
6981void WebContentsImpl::NotifyFindMatchRectsReply(
6982 int version,
6983 const std::vector<gfx::RectF>& rects,
6984 const gfx::RectF& active_rect) {
6985 if (delegate_)
6986 delegate_->FindMatchRectsReply(this, version, rects, active_rect);
6987}
6988#endif
6989
[email protected]34ff1cfc2014-08-20 06:16:056990void WebContentsImpl::SetForceDisableOverscrollContent(bool force_disable) {
6991 force_disable_overscroll_content_ = force_disable;
6992 if (view_)
6993 view_->SetOverscrollControllerEnabled(CanOverscrollContent());
6994}
6995
Jianzhou Fengd8720c72018-01-18 03:06:306996bool WebContentsImpl::SetDeviceEmulationSize(const gfx::Size& new_size) {
6997 device_emulation_size_ = new_size;
6998 RenderWidgetHostView* rwhv = GetMainFrame()->GetView();
6999
7000 const gfx::Size current_size = rwhv->GetViewBounds().size();
7001 if (view_size_before_emulation_.IsEmpty())
7002 view_size_before_emulation_ = current_size;
7003
7004 if (current_size != new_size)
7005 rwhv->SetSize(new_size);
7006
7007 return current_size != new_size;
7008}
7009
7010void WebContentsImpl::ClearDeviceEmulationSize() {
7011 RenderWidgetHostView* rwhv = GetMainFrame()->GetView();
7012 // WebContentsView could get resized during emulation, which also resizes
7013 // RWHV. If it happens, assume user would like to keep using the size after
7014 // emulation.
7015 // TODO(jzfeng): Prohibit resizing RWHV through any other means (at least when
7016 // WebContentsView size changes).
Dmitry Gozmanc63707182018-03-06 00:09:397017 if (!view_size_before_emulation_.IsEmpty() && rwhv &&
7018 rwhv->GetViewBounds().size() == device_emulation_size_) {
Jianzhou Fengd8720c72018-01-18 03:06:307019 rwhv->SetSize(view_size_before_emulation_);
Dmitry Gozmanc63707182018-03-06 00:09:397020 }
Jianzhou Fengd8720c72018-01-18 03:06:307021 device_emulation_size_ = gfx::Size();
7022 view_size_before_emulation_ = gfx::Size();
7023}
7024
Max Morin5bc74f52018-05-09 07:00:217025ForwardingAudioStreamFactory* WebContentsImpl::GetAudioStreamFactory() {
7026 if (!audio_stream_factory_) {
7027 audio_stream_factory_.emplace(
7028 this,
Max Morinddebb972018-09-20 10:04:147029 // BrowserMainLoop::GetInstance() may be null in unit tests.
7030 BrowserMainLoop::GetInstance()
7031 ? static_cast<media::UserInputMonitorBase*>(
7032 BrowserMainLoop::GetInstance()->user_input_monitor())
7033 : nullptr,
Ken Rockotfd03e682019-06-20 21:13:547034 GetSystemConnector()->Clone(), AudioStreamBrokerFactory::CreateImpl());
Max Morin5bc74f52018-05-09 07:00:217035 }
7036
7037 return &*audio_stream_factory_;
7038}
7039
dalecurtis88c240072015-12-09 02:11:187040void WebContentsImpl::MediaStartedPlaying(
billorr21c005d2016-11-17 03:57:047041 const WebContentsObserver::MediaPlayerInfo& media_info,
Mounir Lamouri0f5bdf52019-04-04 16:32:357042 const MediaPlayerId& id) {
billorr21c005d2016-11-17 03:57:047043 if (media_info.has_video)
7044 currently_playing_video_count_++;
7045
ericwilligersde386342016-10-19 02:35:097046 for (auto& observer : observers_)
billorr21c005d2016-11-17 03:57:047047 observer.MediaStartedPlaying(media_info, id);
dalecurtis88c240072015-12-09 02:11:187048}
7049
7050void WebContentsImpl::MediaStoppedPlaying(
billorr21c005d2016-11-17 03:57:047051 const WebContentsObserver::MediaPlayerInfo& media_info,
Mounir Lamouri0f5bdf52019-04-04 16:32:357052 const MediaPlayerId& id,
Becca Hughes220bfc102017-11-14 18:24:077053 WebContentsObserver::MediaStoppedReason reason) {
billorr21c005d2016-11-17 03:57:047054 if (media_info.has_video)
7055 currently_playing_video_count_--;
7056
ericwilligersde386342016-10-19 02:35:097057 for (auto& observer : observers_)
Becca Hughes220bfc102017-11-14 18:24:077058 observer.MediaStoppedPlaying(media_info, id, reason);
billorr21c005d2016-11-17 03:57:047059}
7060
Mounir Lamouri0f5bdf52019-04-04 16:32:357061void WebContentsImpl::MediaResized(const gfx::Size& size,
7062 const MediaPlayerId& id) {
peconn257951522017-06-09 18:24:597063 cached_video_sizes_[id] = size;
7064
7065 for (auto& observer : observers_)
7066 observer.MediaResized(size, id);
7067}
7068
Peter E Conneccb34c22017-09-08 09:37:587069void WebContentsImpl::MediaEffectivelyFullscreenChanged(bool is_fullscreen) {
7070 for (auto& observer : observers_)
7071 observer.MediaEffectivelyFullscreenChanged(is_fullscreen);
7072}
7073
Peter Conn31c726a2017-08-17 11:07:347074base::Optional<gfx::Size> WebContentsImpl::GetFullscreenVideoSize() {
Mounir Lamouri0f5bdf52019-04-04 16:32:357075 base::Optional<MediaPlayerId> id =
Peter Conn31c726a2017-08-17 11:07:347076 media_web_contents_observer_->GetFullscreenVideoMediaPlayerId();
Jan Wilken Dörrie531be7ca2019-06-07 10:05:577077 if (id && base::Contains(cached_video_sizes_, id.value()))
Peter Conn31c726a2017-08-17 11:07:347078 return base::Optional<gfx::Size>(cached_video_sizes_[id.value()]);
Lei Zhang6410ec1a2019-01-31 21:38:587079 return base::nullopt;
peconn257951522017-06-09 18:24:597080}
7081
billorr21c005d2016-11-17 03:57:047082int WebContentsImpl::GetCurrentlyPlayingVideoCount() {
7083 return currently_playing_video_count_;
dalecurtis88c240072015-12-09 02:11:187084}
7085
Raymond Toye413650c2018-08-31 00:12:477086void WebContentsImpl::AudioContextPlaybackStarted(RenderFrameHost* host,
7087 int context_id) {
7088 WebContentsObserver::AudioContextId audio_context_id(host, context_id);
7089 for (auto& observer : observers_)
7090 observer.AudioContextPlaybackStarted(audio_context_id);
7091}
7092
7093void WebContentsImpl::AudioContextPlaybackStopped(RenderFrameHost* host,
7094 int context_id) {
7095 WebContentsObserver::AudioContextId audio_context_id(host, context_id);
7096 for (auto& observer : observers_)
7097 observer.AudioContextPlaybackStopped(audio_context_id);
7098}
7099
Ehsan Karamad1c631762019-05-13 23:04:017100RenderFrameHostImpl* WebContentsImpl::GetMainFrameForInnerDelegate(
7101 FrameTreeNode* frame_tree_node) {
7102 if (auto* web_contents = node_.GetInnerWebContentsInFrame(frame_tree_node))
7103 return web_contents->GetMainFrame();
7104 return nullptr;
7105}
7106
Francois Doraye6161152018-03-27 22:05:377107void WebContentsImpl::UpdateWebContentsVisibility(Visibility visibility) {
Francois Doray3f2afbd2018-04-06 19:18:187108 // Occlusion is disabled when |features::kWebContentsOcclusion| is disabled
7109 // (for power and speed impact assessment) or when
7110 // |switches::kDisableBackgroundingOccludedWindowsForTesting| is specified on
7111 // the command line (to avoid flakiness in browser tests).
7112 const bool occlusion_is_disabled =
7113 !base::FeatureList::IsEnabled(features::kWebContentsOcclusion) ||
Francois Doraye6161152018-03-27 22:05:377114 base::CommandLine::ForCurrentProcess()->HasSwitch(
7115 switches::kDisableBackgroundingOccludedWindowsForTesting);
7116 if (occlusion_is_disabled && visibility == Visibility::OCCLUDED)
7117 visibility = Visibility::VISIBLE;
7118
tapted65ff2ea72016-03-01 23:39:007119 if (!did_first_set_visible_) {
Francois Doraye6161152018-03-27 22:05:377120 if (visibility == Visibility::VISIBLE) {
7121 // A WebContents created with CreateParams::initially_hidden = false
7122 // starts with GetVisibility() == Visibility::VISIBLE even though it is
7123 // not really visible. Call WasShown() when it becomes visible for real as
7124 // the page load mechanism and some WebContentsObserver rely on that.
tapted65ff2ea72016-03-01 23:39:007125 WasShown();
Francois Dorayfe4a1772018-02-17 04:17:097126 did_first_set_visible_ = true;
tapted65ff2ea72016-03-01 23:39:007127 }
Francois Doraye6161152018-03-27 22:05:377128
7129 // Trust the initial visibility of the WebContents and do not switch it to
7130 // HIDDEN or OCCLUDED before it becomes VISIBLE for real. Doing so would
7131 // result in destroying resources that would immediately be recreated (e.g.
7132 // UpdateWebContents(HIDDEN) can be called when a WebContents is added to a
7133 // hidden window that is about to be shown).
7134
tapted65ff2ea72016-03-01 23:39:007135 return;
7136 }
Francois Doraye6161152018-03-27 22:05:377137
7138 if (visibility == visibility_)
tapted65ff2ea72016-03-01 23:39:007139 return;
7140
Francois Doraye6161152018-03-27 22:05:377141 if (visibility == Visibility::VISIBLE)
tapted65ff2ea72016-03-01 23:39:007142 WasShown();
Francois Doraye6161152018-03-27 22:05:377143 else if (visibility == Visibility::OCCLUDED)
7144 WasOccluded();
tapted65ff2ea72016-03-01 23:39:007145 else
7146 WasHidden();
7147}
7148
aelias5252baa2016-04-10 01:18:027149void WebContentsImpl::UpdateOverridingUserAgent() {
mlamouri5cd9ae82017-02-18 11:05:097150 NotifyPreferencesChanged();
aelias5252baa2016-04-10 01:18:027151}
7152
avid53461d2016-02-25 17:15:047153void WebContentsImpl::SetJavaScriptDialogManagerForTesting(
7154 JavaScriptDialogManager* dialog_manager) {
7155 dialog_manager_ = dialog_manager;
7156}
7157
rockotf62002a2016-09-15 00:08:597158void WebContentsImpl::RemoveBindingSet(const std::string& interface_name) {
7159 auto it = binding_sets_.find(interface_name);
7160 if (it != binding_sets_.end())
7161 binding_sets_.erase(it);
7162}
7163
nzolghadrd87a308d2016-12-07 15:45:567164bool WebContentsImpl::AddDomainInfoToRapporSample(rappor::Sample* sample) {
nzolghadrbbffc6c2017-05-25 17:56:397165 // Here we associate this metric to the main frame URL regardless of what
7166 // caused it.
nzolghadrd87a308d2016-12-07 15:45:567167 sample->SetStringField("Domain", ::rappor::GetDomainAndRegistrySampleFromGURL(
7168 GetLastCommittedURL()));
nzolghadrd87a308d2016-12-07 15:45:567169 return true;
7170}
7171
ekaramada110f642016-12-21 19:47:287172void WebContentsImpl::FocusedNodeTouched(bool editable) {
7173#if defined(OS_WIN)
ekaramada110f642016-12-21 19:47:287174 RenderWidgetHostView* view = GetRenderWidgetHostView();
7175 if (!view)
7176 return;
Dave Tapuska2315d8f2018-03-06 17:52:557177 view->FocusedNodeTouched(editable);
ekaramada110f642016-12-21 19:47:287178#endif
7179}
7180
falken52a56e32016-12-08 05:02:407181void WebContentsImpl::ShowInsecureLocalhostWarningIfNeeded() {
7182 bool allow_localhost = base::CommandLine::ForCurrentProcess()->HasSwitch(
7183 switches::kAllowInsecureLocalhost);
7184 if (!allow_localhost)
7185 return;
7186
7187 content::NavigationEntry* entry = GetController().GetLastCommittedEntry();
Rob Wuf79b3ba2018-01-14 01:54:317188 if (!entry || !net::IsLocalhost(entry->GetURL()))
falken52a56e32016-12-08 05:02:407189 return;
7190
7191 content::SSLStatus ssl_status = entry->GetSSL();
7192 bool is_cert_error = net::IsCertStatusError(ssl_status.cert_status) &&
7193 !net::IsCertStatusMinorError(ssl_status.cert_status);
7194 if (!is_cert_error)
7195 return;
7196
7197 GetMainFrame()->AddMessageToConsole(
Abhijeet Kandalkara5928a0a2019-03-13 05:04:427198 blink::mojom::ConsoleMessageLevel::kWarning,
falken52a56e32016-12-08 05:02:407199 base::StringPrintf("This site does not have a valid SSL "
7200 "certificate! Without SSL, your site's and "
7201 "visitors' data is vulnerable to theft and "
7202 "tampering. Get a valid SSL certificate before"
7203 " releasing your website to the public."));
7204}
7205
ekaramadf6750aa2017-06-06 18:29:427206bool WebContentsImpl::IsShowingContextMenuOnPage() const {
7207 return showing_context_menu_;
7208}
7209
Min Qina904f3302018-02-13 23:33:347210download::DownloadUrlParameters::RequestHeadersType
7211WebContentsImpl::ParseDownloadHeaders(const std::string& headers) {
7212 download::DownloadUrlParameters::RequestHeadersType request_headers;
Megan Jablonski2f6a4c52017-07-10 23:01:257213 for (const base::StringPiece& key_value : base::SplitStringPiece(
7214 headers, "\r\n", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY)) {
7215 std::vector<std::string> pair = base::SplitString(
7216 key_value, ":", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
7217 if (2ul == pair.size())
7218 request_headers.push_back(make_pair(pair[0], pair[1]));
7219 }
7220 return request_headers;
7221}
7222
lukasza6f8ac622017-06-06 03:10:207223void WebContentsImpl::SetOpenerForNewContents(FrameTreeNode* opener,
7224 bool opener_suppressed) {
7225 if (opener) {
7226 FrameTreeNode* new_root = GetFrameTree()->root();
7227
7228 // For the "original opener", track the opener's main frame instead, because
7229 // if the opener is a subframe, the opener tracking could be easily bypassed
7230 // by spawning from a subframe and deleting the subframe.
7231 // https://crbug.com/705316
7232 new_root->SetOriginalOpener(opener->frame_tree()->root());
7233
7234 if (!opener_suppressed) {
7235 new_root->SetOpener(opener);
7236 created_with_opener_ = true;
7237 }
7238 }
7239}
7240
Mounir Lamouri0f5bdf52019-04-04 16:32:357241void WebContentsImpl::MediaMutedStatusChanged(const MediaPlayerId& id,
7242 bool muted) {
Becca Hughes9f6fd4b82017-06-15 10:01:407243 for (auto& observer : observers_)
7244 observer.MediaMutedStatusChanged(id, muted);
7245}
7246
EhsanK2075c7e2017-08-21 02:42:397247void WebContentsImpl::SetVisibilityForChildViews(bool visible) {
7248 GetMainFrame()->SetVisibilityForChildViews(visible);
7249}
7250
Alison Maherbe15c072019-07-08 18:12:397251void WebContentsImpl::OnNativeThemeUpdated(ui::NativeTheme* observed_theme) {
7252 DCHECK(native_theme_observer_.IsObserving(observed_theme));
7253
7254 bool in_dark_mode = observed_theme->SystemDarkModeEnabled();
7255 bool in_high_contrast = observed_theme->UsesHighContrastColors();
Alison Maher67fd4d9f2019-07-12 18:34:217256 ui::NativeTheme::PreferredColorScheme preferred_color_scheme =
7257 observed_theme->GetPreferredColorScheme();
Alison Maherbe15c072019-07-08 18:12:397258 bool preferences_changed = false;
7259
7260 if (in_dark_mode_ != in_dark_mode) {
7261 in_dark_mode_ = in_dark_mode;
7262 preferences_changed = true;
7263 }
7264 if (in_high_contrast_ != in_high_contrast) {
7265 in_high_contrast_ = in_high_contrast;
7266 preferences_changed = true;
7267 }
Alison Maher67fd4d9f2019-07-12 18:34:217268 if (preferred_color_scheme_ != preferred_color_scheme) {
7269 preferred_color_scheme_ = preferred_color_scheme;
7270 preferences_changed = true;
7271 }
Alison Maherbe15c072019-07-08 18:12:397272
7273 if (preferences_changed)
7274 NotifyPreferencesChanged();
Rune Lillesveen5d8c49982019-04-24 19:07:307275}
7276
Dave Tapuskae782bea2019-07-09 16:21:517277mojom::FrameInputHandler* WebContentsImpl::GetFocusedFrameInputHandler() {
7278 auto* focused_frame = GetFocusedFrame();
7279 if (!focused_frame)
7280 return nullptr;
7281 return focused_frame->GetFrameInputHandler();
7282}
7283
[email protected]8ff00d72012-10-23 19:12:217284} // namespace content