blob: 565b430d05c7188edf08df6bb334d9f865f11e65 [file] [log] [blame]
[email protected]91854cd2012-01-10 19:43:571// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]93ddb3c2012-04-11 21:44:295#include "content/browser/web_contents/web_contents_impl.h"
[email protected]39526562011-02-05 03:41:516
avib7348942015-12-25 20:57:107#include <stddef.h>
8
w.shackleton49bcd392016-01-06 17:38:229#include <cmath>
[email protected]2bb171882012-03-07 02:09:4610#include <utility>
rkaplowe56a52032017-02-07 00:14:5411#include <vector>
[email protected]b75b8292010-10-01 07:28:2512
Sebastien Marchandf8cbfab2019-01-25 16:02:3013#include "base/bind.h"
[email protected]36fb2c7c2011-04-04 15:49:0814#include "base/command_line.h"
creisfafed882016-07-29 00:03:0915#include "base/debug/dump_without_crashing.h"
zqzhang181047e62016-07-01 13:37:1716#include "base/feature_list.h"
Marijn Kruisselbrink46fa4bd2019-07-11 18:15:4717#include "base/files/file_path.h"
[email protected]cbb1ef592013-06-05 19:49:4618#include "base/lazy_instance.h"
skyostil95082a62015-06-05 19:53:0719#include "base/location.h"
[email protected]6d636e62013-07-31 03:15:5620#include "base/logging.h"
avib7348942015-12-25 20:57:1021#include "base/macros.h"
fdorayba121422016-12-23 19:51:4822#include "base/memory/ptr_util.h"
dmazzonid95ae842016-04-12 21:17:3923#include "base/memory/ref_counted.h"
asvitkine8d51e9d2016-09-02 23:55:4324#include "base/metrics/histogram_macros.h"
bratell0a7406f2017-03-28 07:46:3725#include "base/metrics/user_metrics.h"
Avi Drissman4891b852018-09-25 18:12:5826#include "base/no_destructor.h"
Carlos Caballero1215f882019-10-29 15:58:4327#include "base/optional.h"
[email protected]04cbd3d2013-12-04 04:58:2028#include "base/process/process.h"
skyostil95082a62015-06-05 19:53:0729#include "base/single_thread_task_runner.h"
Arthur Sonzogni929d29f2018-09-17 14:19:4430#include "base/stl_util.h"
[email protected]348fbaac2013-06-11 06:31:5131#include "base/strings/string16.h"
32#include "base/strings/string_number_conversions.h"
Avi Drissman4891b852018-09-25 18:12:5833#include "base/strings/string_split.h"
[email protected]348fbaac2013-06-11 06:31:5134#include "base/strings/string_util.h"
[email protected]74ebfb12013-06-07 20:48:0035#include "base/strings/utf_string_conversions.h"
Eric Seckler8652dcd52018-09-20 10:42:2836#include "base/task/post_task.h"
gab30f26df2016-05-11 19:37:5537#include "base/threading/thread_task_runner_handle.h"
[email protected]a43858f2013-06-28 15:18:3738#include "base/time/time.h"
ssid3e765612015-01-28 04:03:4239#include "base/trace_event/trace_event.h"
avib7348942015-12-25 20:57:1040#include "build/build_config.h"
Min Qinda0ed2062018-02-23 22:00:5341#include "components/download/public/common/download_stats.h"
nzolghadrd87a308d2016-12-07 15:45:5642#include "components/rappor/public/rappor_utils.h"
rsleevi24f64dc22015-08-07 21:39:2143#include "components/url_formatter/url_formatter.h"
Abigail Kleinaa898742019-11-01 02:31:2544#include "content/browser/accessibility/accessibility_event_recorder.h"
James Wallace-Leeeafc94cb92018-07-23 21:35:0945#include "content/browser/accessibility/accessibility_tree_formatter_blink.h"
jamescookda2505812015-03-20 18:01:1846#include "content/browser/bad_message.h"
Max Morinddebb972018-09-20 10:04:1447#include "content/browser/browser_main_loop.h"
[email protected]7a846df2012-09-20 19:17:3948#include "content/browser/browser_plugin/browser_plugin_embedder.h"
49#include "content/browser/browser_plugin/browser_plugin_guest.h"
[email protected]b9535422012-02-09 01:47:5950#include "content/browser/child_process_security_policy_impl.h"
Pavel Feldman3c1842b2017-08-02 05:00:1651#include "content/browser/devtools/protocol/page_handler.h"
dgozmanf00c4f72016-10-19 17:45:1552#include "content/browser/devtools/render_frame_devtools_agent_host.h"
Becca Hughes3b5a43482018-07-17 22:31:5553#include "content/browser/display_cutout/display_cutout_host_impl.h"
[email protected]5f2aa722013-08-07 16:59:4154#include "content/browser/dom_storage/dom_storage_context_wrapper.h"
[email protected]1ea3c792012-04-17 01:25:0455#include "content/browser/dom_storage/session_storage_namespace_impl.h"
[email protected]aa4f3972012-03-01 18:12:1256#include "content/browser/download/mhtml_generation_manager.h"
[email protected]a53209b2012-01-20 16:48:1657#include "content/browser/download/save_package.h"
paulmeyerc0b762b2016-04-13 11:55:1758#include "content/browser/find_request_manager.h"
[email protected]bffc8302014-01-23 20:52:1659#include "content/browser/frame_host/cross_process_frame_connector.h"
ananta4b7467a52016-09-23 01:42:3860#include "content/browser/frame_host/frame_tree_node.h"
[email protected]d4a8ca482013-10-30 21:06:4061#include "content/browser/frame_host/interstitial_page_impl.h"
62#include "content/browser/frame_host/navigation_entry_impl.h"
Arthur Hemeryc3380172018-01-22 14:00:1763#include "content/browser/frame_host/navigation_request.h"
[email protected]6ea6bdf2013-12-06 13:35:0164#include "content/browser/frame_host/navigator_impl.h"
[email protected]52913802013-12-10 05:52:1865#include "content/browser/frame_host/render_frame_host_impl.h"
lfgdb5c4ed2016-03-04 23:09:0766#include "content/browser/frame_host/render_frame_proxy_host.h"
Kenneth Russell954ed9ce2018-04-12 23:07:0167#include "content/browser/gpu/gpu_data_manager_impl.h"
mlamouriefdca9d2014-09-16 16:55:4068#include "content/browser/manifest/manifest_manager_host.h"
Max Morin5bc74f52018-05-09 07:00:2169#include "content/browser/media/audio_stream_broker.h"
dalecurtisbc6572e12014-09-12 19:22:3070#include "content/browser/media/audio_stream_monitor.h"
miu50f97892014-09-22 22:49:5271#include "content/browser/media/capture/web_contents_audio_muter.h"
dalecurtis88c240072015-12-09 02:11:1872#include "content/browser/media/media_web_contents_observer.h"
zqzhang1adf3cb2016-11-01 11:26:4573#include "content/browser/media/session/media_session_impl.h"
tommyclieb25b2a2014-11-03 19:45:0974#include "content/browser/plugin_content_origin_whitelist.h"
David Black9cca3592019-11-06 23:02:2275#include "content/browser/renderer_host/frame_token_message_queue.h"
[email protected]f3b1a082011-11-18 00:34:3076#include "content/browser/renderer_host/render_process_host_impl.h"
[email protected]21161032014-05-05 16:56:5077#include "content/browser/renderer_host/render_view_host_delegate_view.h"
[email protected]b3c41c0b2012-03-06 15:48:3278#include "content/browser/renderer_host/render_view_host_impl.h"
[email protected]bafe6cd2012-05-23 23:09:5079#include "content/browser/renderer_host/render_widget_host_impl.h"
kenrb2a565f82015-09-02 20:24:5980#include "content/browser/renderer_host/render_widget_host_input_event_router.h"
[email protected]cfd80b02014-05-01 17:46:4881#include "content/browser/renderer_host/render_widget_host_view_base.h"
Ken Buchanandaef006b2017-08-17 18:32:1582#include "content/browser/renderer_host/render_widget_host_view_child_frame.h"
ekaramadadd882292016-06-08 15:22:5683#include "content/browser/renderer_host/text_input_manager.h"
leon.han552e9de2017-02-09 14:37:3084#include "content/browser/screen_orientation/screen_orientation_provider.h"
[email protected]b6583592012-01-25 19:52:3385#include "content/browser/site_instance_impl.h"
Charlie Reis96077362019-07-04 00:34:0486#include "content/browser/web_contents/javascript_dialog_navigation_deferrer.h"
lfgc740d4b22016-04-15 16:45:3387#include "content/browser/web_contents/web_contents_view_child_frame.h"
[email protected]86a0a6e2013-01-28 06:33:0388#include "content/browser/webui/web_ui_controller_factory_registry.h"
[email protected]d2353452012-01-19 19:53:5689#include "content/browser/webui/web_ui_impl.h"
[email protected]8eb04562013-03-06 03:41:1490#include "content/common/browser_plugin/browser_plugin_constants.h"
Paul Meyer0c58c3712016-11-17 22:59:5191#include "content/common/drag_messages.h"
[email protected]f114fa42013-12-06 17:06:4492#include "content/common/frame_messages.h"
[email protected]1f3fc1d2014-04-03 14:50:1793#include "content/common/input_messages.h"
lfgdb5c4ed2016-03-04 23:09:0794#include "content/common/page_messages.h"
nasko34634672016-07-29 18:46:0695#include "content/common/page_state_serialization.h"
rockot5c478a72016-09-28 23:14:1896#include "content/common/render_message_filter.mojom.h"
[email protected]2c5569662011-03-22 20:45:0297#include "content/common/view_messages.h"
Albert J. Wong3c93c182018-09-27 17:29:4398#include "content/common/widget_messages.h"
Abigail Kleinabb428332019-09-13 18:26:2199#include "content/public/browser/accessibility_tree_formatter.h"
[email protected]31a71eaf2014-03-13 01:47:36100#include "content/public/browser/ax_event_notification_details.h"
[email protected]ccb797302011-12-15 16:55:11101#include "content/public/browser/browser_context.h"
[email protected]139355f2014-05-11 14:21:28102#include "content/public/browser/browser_plugin_guest_manager.h"
Eric Seckler8652dcd52018-09-20 10:42:28103#include "content/public/browser/browser_task_traits.h"
Gabriel Charette790754c2018-03-16 21:32:59104#include "content/public/browser/browser_thread.h"
[email protected]87f3c082011-10-19 18:07:44105#include "content/public/browser/content_browser_client.h"
[email protected]e582fdd2011-12-20 16:48:17106#include "content/public/browser/download_manager.h"
Kent Tamura512a27e2018-10-04 00:49:32107#include "content/public/browser/file_select_listener.h"
ekaramada110f642016-12-21 19:47:28108#include "content/public/browser/focused_node_details.h"
ekaramad1beabecc2016-09-23 20:18:17109#include "content/public/browser/guest_mode.h"
[email protected]d9083482012-01-06 00:38:46110#include "content/public/browser/invalidate_type.h"
[email protected]71a88bb2013-02-01 22:05:15111#include "content/public/browser/javascript_dialog_manager.h"
skyf65d9bb2017-03-24 02:26:39112#include "content/public/browser/keyboard_event_processing_result.h"
[email protected]09d31d52012-03-11 22:30:27113#include "content/public/browser/load_notification_details.h"
[email protected]5b96836f2011-12-22 07:39:00114#include "content/public/browser/navigation_details.h"
[email protected]375fa1b2012-05-22 22:05:37115#include "content/public/browser/notification_details.h"
[email protected]be2510c02012-05-28 14:52:14116#include "content/public/browser/notification_service.h"
scottmg011d96e32016-06-29 19:15:08117#include "content/public/browser/notification_types.h"
[email protected]95640212014-07-26 18:14:30118#include "content/public/browser/render_widget_host_iterator.h"
Lukasz Anforowicze1b954d92017-10-30 21:28:06119#include "content/public/browser/restore_type.h"
estark5ea80e52015-06-19 18:43:12120#include "content/public/browser/security_style_explanations.h"
falken52a56e32016-12-08 05:02:40121#include "content/public/browser/ssl_status.h"
[email protected]4c3a23582012-08-18 08:54:34122#include "content/public/browser/storage_partition.h"
Ken Rockotfd03e682019-06-20 21:13:54123#include "content/public/browser/system_connector.h"
[email protected]674bc592011-12-20 23:00:42124#include "content/public/browser/web_contents_delegate.h"
Julie Jeongeun Kim7d0986172019-12-04 03:17:25125#include "content/public/browser/web_contents_receiver_set.h"
Lei Zhangee3b63ad2018-06-06 23:25:31126#include "content/public/browser/web_ui_controller.h"
[email protected]e091df82011-10-11 18:13:21127#include "content/public/common/bindings_policy.h"
alexmosc9e76ec2016-05-16 22:59:37128#include "content/public/common/child_process_host.h"
Hans Wennborg5ffd1392019-10-16 11:00:02129#include "content/public/common/content_client.h"
[email protected]54087fe2011-10-28 22:02:48130#include "content/public/common/content_constants.h"
Francois Doray3f2afbd2018-04-06 19:18:18131#include "content/public/common/content_features.h"
[email protected]64d69de42012-02-06 00:19:54132#include "content/public/common/content_switches.h"
Lukasz Anforowicze1b954d92017-10-30 21:28:06133#include "content/public/common/page_state.h"
Leon Han963dc182018-11-06 05:41:48134#include "content/public/common/referrer_type_converters.h"
[email protected]04cbd3d2013-12-04 04:58:20135#include "content/public/common/result_codes.h"
[email protected]f16cc6782013-12-16 23:42:57136#include "content/public/common/url_utils.h"
danakjffc8baf2019-10-10 23:49:42137#include "content/public/common/use_zoom_for_dsf_policy.h"
[email protected]b3a97b52014-07-09 06:25:05138#include "content/public/common/web_preferences.h"
Max Morinddebb972018-09-20 10:04:14139#include "media/base/user_input_monitor.h"
wjmaclean64951902016-04-29 20:59:12140#include "net/base/url_util.h"
[email protected]8bfc8272013-09-09 20:10:53141#include "net/http/http_cache.h"
142#include "net/http/http_transaction_factory.h"
rhalavati0fbaeef32017-06-01 15:56:30143#include "net/traffic_annotation/network_traffic_annotation.h"
[email protected]8bfc8272013-09-09 20:10:53144#include "net/url_request/url_request_context.h"
[email protected]abe2c032011-03-31 18:49:34145#include "net/url_request/url_request_context_getter.h"
Scott Violet02e38b92018-03-27 23:42:14146#include "ppapi/buildflags/buildflags.h"
Ke He31d0bb02018-02-24 07:16:24147#include "services/device/public/mojom/constants.mojom.h"
Mounir Lamourifd9dcefa2017-07-06 10:26:55148#include "services/metrics/public/cpp/ukm_recorder.h"
John Abd-El-Malekb175a612018-12-03 20:11:58149#include "services/network/public/cpp/features.h"
Ke He7319dbe2017-11-09 05:54:44150#include "services/service_manager/public/cpp/connector.h"
rockot734fb662016-10-15 16:41:30151#include "services/service_manager/public/cpp/interface_provider.h"
Becca Hughes6daf5662018-06-27 16:50:54152#include "third_party/blink/public/common/associated_interfaces/associated_interface_provider.h"
Blink Reformata30d4232018-04-07 15:31:06153#include "third_party/blink/public/common/frame/sandbox_flags.h"
154#include "third_party/blink/public/common/mime_util/mime_util.h"
danakj938b37a62019-09-24 18:35:54155#include "third_party/blink/public/common/page/page_zoom.h"
Sergio Villar Senin9d9d4f52019-10-15 10:57:57156#include "third_party/blink/public/common/security/security_style.h"
Dave Tapuskaa4189512019-10-15 20:27:34157#include "third_party/blink/public/mojom/frame/fullscreen.mojom.h"
Takuto Ikutaaa3b796c2019-02-06 02:54:56158#include "third_party/blink/public/mojom/loader/pause_subresource_loading_handle.mojom.h"
Antonio Gomes0d42960a2019-06-05 12:35:51159#include "third_party/blink/public/mojom/mediastream/media_stream.mojom-shared.h"
halton.huoca2eabd2015-07-06 08:17:40160#include "third_party/skia/include/core/SkBitmap.h"
dmazzonid95ae842016-04-12 21:17:39161#include "ui/accessibility/ax_tree_combiner.h"
[email protected]a08412b62012-05-29 21:28:56162#include "ui/base/layout.h"
Daniel Cheng90196c82018-04-25 21:49:14163#include "ui/events/base_event_utils.h"
chongz4a9751942016-08-17 17:49:39164#include "ui/events/blink/web_input_event_traits.h"
[email protected]66bd5512012-08-01 02:02:52165#include "ui/gl/gl_switches.h"
[email protected]3c733bde2010-12-21 19:56:31166
ekaramada110f642016-12-21 19:47:28167#if defined(OS_WIN)
168#include "content/browser/renderer_host/dip_util.h"
169#include "ui/gfx/geometry/dip_util.h"
170#endif
171
[email protected]583418cc2013-01-17 14:01:10172#if defined(OS_ANDROID)
mfomitchev2b8b066a2016-01-28 19:23:15173#include "content/browser/android/date_time_chooser_android.h"
sammcf5f1b0f2016-09-20 23:05:11174#include "content/browser/android/java_interfaces_impl.h"
Leon Hanccb985f2019-08-26 02:43:24175#include "content/browser/android/nfc_host.h"
[email protected]155c7f22013-12-09 17:07:18176#include "content/browser/web_contents/web_contents_android.h"
Ke He31d0bb02018-02-24 07:16:24177#include "services/device/public/mojom/nfc.mojom.h"
mcnee336ea2c2017-05-23 22:50:59178#else // !OS_ANDROID
179#include "content/browser/host_zoom_map_impl.h"
mfomitchev2b8b066a2016-01-28 19:23:15180#endif // OS_ANDROID
[email protected]583418cc2013-01-17 14:01:10181
brettw4b461082016-11-19 18:55:16182#if BUILDFLAG(ENABLE_PLUGINS)
zqzhang181047e62016-07-01 13:37:17183#include "content/browser/media/session/pepper_playback_observer.h"
Lei Zhangebcc6302018-01-12 19:46:45184#endif
zqzhang181047e62016-07-01 13:37:17185
[email protected]8ff00d72012-10-23 19:12:21186namespace content {
Abigail Kleinaa898742019-11-01 02:31:25187
188using AccessibilityEventCallback =
189 base::RepeatingCallback<void(const std::string&)>;
190
[email protected]420ae012009-04-24 05:16:32191namespace {
192
[email protected]960b0372014-05-19 18:01:00193const int kMinimumDelayBetweenLoadingUpdatesMS = 100;
[email protected]82114f52012-03-20 22:53:41194const char kDotGoogleDotCom[] = ".google.com";
[email protected]ca406032011-07-19 21:53:05195
Avi Drissmana9114822018-03-23 18:25:55196base::LazyInstance<std::vector<
197 WebContentsImpl::FriendWrapper::CreatedCallback>>::DestructorAtExit
198 g_created_callbacks = LAZY_INSTANCE_INITIALIZER;
[email protected]cbb1ef592013-06-05 19:49:46199
dcheng57e39e22016-01-21 00:25:38200bool HasMatchingProcess(FrameTree* tree, int render_process_id) {
201 for (FrameTreeNode* node : tree->Nodes()) {
202 if (node->current_frame_host()->GetProcess()->GetID() == render_process_id)
203 return true;
alexmos4cf2aa32015-07-15 23:40:43204 }
dcheng57e39e22016-01-21 00:25:38205 return false;
[email protected]95640212014-07-26 18:14:30206}
207
kenrb19221852016-04-29 17:21:40208bool HasMatchingWidgetHost(FrameTree* tree, RenderWidgetHost* host) {
209 // This method scans the frame tree rather than checking whether
210 // host->delegate() == this, which allows it to return false when the host
211 // for a frame that is pending or pending deletion.
212 if (!host)
213 return false;
214
215 for (FrameTreeNode* node : tree->Nodes()) {
216 if (node->current_frame_host()->GetRenderWidgetHost() == host)
217 return true;
218 }
219 return false;
220}
221
dmazzonif8a31042016-12-03 00:52:13222void UpdateAccessibilityModeOnFrame(RenderFrameHost* frame_host) {
223 static_cast<RenderFrameHostImpl*>(frame_host)->UpdateAccessibilityMode();
[email protected]95640212014-07-26 18:14:30224}
225
dtseng0dd3fa12015-07-22 19:00:52226void ResetAccessibility(RenderFrameHost* rfh) {
227 static_cast<RenderFrameHostImpl*>(rfh)->AccessibilityReset();
228}
229
Ian Clelland5cbaaf82017-11-27 22:00:03230RenderFrameHostImpl* FindOpenerRFH(const WebContents::CreateParams& params) {
231 RenderFrameHostImpl* opener_rfh = nullptr;
alexmose201c7cd2015-06-10 17:14:21232 if (params.opener_render_frame_id != MSG_ROUTING_NONE) {
Ian Clelland5cbaaf82017-11-27 22:00:03233 opener_rfh = RenderFrameHostImpl::FromID(params.opener_render_process_id,
234 params.opener_render_frame_id);
alexmose201c7cd2015-06-10 17:14:21235 }
Ian Clelland5cbaaf82017-11-27 22:00:03236 return opener_rfh;
lukasza6f8ac622017-06-06 03:10:20237}
238
Dominick Ng37a15052018-07-17 23:56:35239// Returns |true| if |type| is the kind of user input that should trigger the
240// user interaction observers.
241bool IsUserInteractionInputType(blink::WebInputEvent::Type type) {
242 // Ideally, this list would be based more off of
243 // https://whatwg.org/C/interaction.html#triggered-by-user-activation.
244 return type == blink::WebInputEvent::kMouseDown ||
245 type == blink::WebInputEvent::kGestureScrollBegin ||
246 type == blink::WebInputEvent::kTouchStart ||
247 type == blink::WebInputEvent::kRawKeyDown;
248}
249
Pavel Feldman3c1842b2017-08-02 05:00:16250// Ensures that OnDialogClosed is only called once.
251class CloseDialogCallbackWrapper
252 : public base::RefCountedThreadSafe<CloseDialogCallbackWrapper> {
253 public:
Avi Drissmane04d3992017-10-05 15:11:36254 using CloseCallback =
255 base::OnceCallback<void(bool, bool, const base::string16&)>;
256
257 explicit CloseDialogCallbackWrapper(CloseCallback callback)
258 : callback_(std::move(callback)) {}
Pavel Feldman3c1842b2017-08-02 05:00:16259
260 void Run(bool dialog_was_suppressed,
261 bool success,
262 const base::string16& user_input) {
Avi Drissmane04d3992017-10-05 15:11:36263 if (callback_.is_null())
Pavel Feldman3c1842b2017-08-02 05:00:16264 return;
Avi Drissmane04d3992017-10-05 15:11:36265 std::move(callback_).Run(dialog_was_suppressed, success, user_input);
Pavel Feldman3c1842b2017-08-02 05:00:16266 }
267
268 private:
269 friend class base::RefCountedThreadSafe<CloseDialogCallbackWrapper>;
270 ~CloseDialogCallbackWrapper() {}
Avi Drissmane04d3992017-10-05 15:11:36271
272 CloseCallback callback_;
Pavel Feldman3c1842b2017-08-02 05:00:16273};
274
Arthur Sonzogni929d29f2018-09-17 14:19:44275bool FrameCompareDepth(RenderFrameHostImpl* a, RenderFrameHostImpl* b) {
276 return a->frame_tree_node()->depth() < b->frame_tree_node()->depth();
277}
278
Raymes Khoury58373df2019-08-06 06:29:20279bool AreValidRegisterProtocolHandlerArguments(const std::string& protocol,
280 const GURL& url,
281 const url::Origin& origin) {
282 ChildProcessSecurityPolicyImpl* policy =
283 ChildProcessSecurityPolicyImpl::GetInstance();
284 if (policy->IsPseudoScheme(protocol))
285 return false;
286
287 url::Origin url_origin = url::Origin::Create(url);
288 if (url_origin.opaque())
289 return false;
290
291 if (!url_origin.IsSameOriginWith(origin))
292 return false;
293
294 return true;
295}
296
yilkal796e89e2019-10-02 23:58:28297void RecordMaxFrameCountUMA(size_t max_frame_count) {
298 UMA_HISTOGRAM_COUNTS_10000("Navigation.MainFrame.MaxFrameCount",
299 max_frame_count);
300}
301
lukasza6f8ac622017-06-06 03:10:20302} // namespace
303
Erik Chenbb8e738e2018-04-28 14:10:43304std::unique_ptr<WebContents> WebContents::Create(
305 const WebContents::CreateParams& params) {
Albert J. Wong65fe64d2019-09-20 02:48:14306 return WebContentsImpl::Create(params);
307}
308
309std::unique_ptr<WebContentsImpl> WebContentsImpl::Create(
310 const CreateParams& params) {
311 return CreateWithOpener(params, FindOpenerRFH(params));
[email protected]d1198fd2012-08-13 22:50:19312}
313
erikchenade1fef2018-05-02 22:07:57314std::unique_ptr<WebContents> WebContents::CreateWithSessionStorage(
[email protected]54944cde2012-12-09 09:24:59315 const WebContents::CreateParams& params,
[email protected]fdac6ade2013-07-20 01:06:30316 const SessionStorageNamespaceMap& session_storage_namespace_map) {
erikchenade1fef2018-05-02 22:07:57317 std::unique_ptr<WebContentsImpl> new_contents(
318 new WebContentsImpl(params.browser_context));
Ian Clelland5cbaaf82017-11-27 22:00:03319 RenderFrameHostImpl* opener_rfh = FindOpenerRFH(params);
320 FrameTreeNode* opener = nullptr;
321 if (opener_rfh)
322 opener = opener_rfh->frame_tree_node();
323 new_contents->SetOpenerForNewContents(opener, params.opener_suppressed);
[email protected]fdac6ade2013-07-20 01:06:30324
jdoerrie55ec69d2018-10-08 13:34:46325 for (auto it = session_storage_namespace_map.begin();
326 it != session_storage_namespace_map.end(); ++it) {
[email protected]fdac6ade2013-07-20 01:06:30327 new_contents->GetController()
328 .SetSessionStorageNamespace(it->first, it->second.get());
329 }
330
Sean Toppingbbc4a2bd2019-01-30 02:16:14331 WebContentsImpl* outer_web_contents = nullptr;
Sam McNally69a33772018-06-27 16:54:12332 if (params.guest_delegate) {
333 // This makes |new_contents| act as a guest.
334 // For more info, see comment above class BrowserPluginGuest.
Albert J. Wongc55cc64d2018-10-12 02:50:04335 BrowserPluginGuest::CreateInWebContents(new_contents.get(),
336 params.guest_delegate);
Sean Toppingbbc4a2bd2019-01-30 02:16:14337 outer_web_contents = static_cast<WebContentsImpl*>(
338 params.guest_delegate->GetOwnerWebContents());
Sam McNally69a33772018-06-27 16:54:12339 }
340
[email protected]54944cde2012-12-09 09:24:59341 new_contents->Init(params);
Sean Toppingbbc4a2bd2019-01-30 02:16:14342 if (outer_web_contents)
343 outer_web_contents->InnerWebContentsCreated(new_contents.get());
[email protected]d1198fd2012-08-13 22:50:19344 return new_contents;
[email protected]a81343d232011-12-27 07:39:20345}
[email protected]746d3052012-05-22 15:15:47346
benwells5e7260d2017-03-20 23:36:33347void WebContentsImpl::FriendWrapper::AddCreatedCallbackForTesting(
nickf9acfbe2014-12-23 19:12:37348 const CreatedCallback& callback) {
[email protected]cbb1ef592013-06-05 19:49:46349 g_created_callbacks.Get().push_back(callback);
350}
351
benwells5e7260d2017-03-20 23:36:33352void WebContentsImpl::FriendWrapper::RemoveCreatedCallbackForTesting(
nickf9acfbe2014-12-23 19:12:37353 const CreatedCallback& callback) {
[email protected]cbb1ef592013-06-05 19:49:46354 for (size_t i = 0; i < g_created_callbacks.Get().size(); ++i) {
Robert Liao1db5a352019-06-05 20:57:34355 if (g_created_callbacks.Get().at(i) == callback) {
[email protected]cbb1ef592013-06-05 19:49:46356 g_created_callbacks.Get().erase(g_created_callbacks.Get().begin() + i);
357 return;
358 }
359 }
360}
361
avidf38c952015-10-27 13:45:13362WebContents* WebContents::FromRenderViewHost(RenderViewHost* rvh) {
363 if (!rvh)
364 return nullptr;
[email protected]746d3052012-05-22 15:15:47365 return rvh->GetDelegate()->GetAsWebContents();
366}
367
[email protected]a86c0e962013-12-17 17:10:39368WebContents* WebContents::FromRenderFrameHost(RenderFrameHost* rfh) {
avidf38c952015-10-27 13:45:13369 if (!rfh)
370 return nullptr;
371 return static_cast<RenderFrameHostImpl*>(rfh)->delegate()->GetAsWebContents();
[email protected]a86c0e962013-12-17 17:10:39372}
373
ananta4b7467a52016-09-23 01:42:38374WebContents* WebContents::FromFrameTreeNodeId(int frame_tree_node_id) {
375 FrameTreeNode* frame_tree_node =
376 FrameTreeNode::GloballyFindByID(frame_tree_node_id);
anantab8cfa3b2016-10-14 22:43:19377 if (!frame_tree_node)
378 return nullptr;
jam8c4edd02017-05-06 18:50:33379 return WebContentsImpl::FromFrameTreeNode(frame_tree_node);
ananta4b7467a52016-09-23 01:42:38380}
381
leon.han552e9de2017-02-09 14:37:30382void WebContents::SetScreenOrientationDelegate(
383 ScreenOrientationDelegate* delegate) {
384 ScreenOrientationProvider::SetDelegate(delegate);
385}
386
[email protected]7fff43e2013-05-21 20:21:10387// WebContentsImpl::DestructionObserver ----------------------------------------
388
389class WebContentsImpl::DestructionObserver : public WebContentsObserver {
390 public:
391 DestructionObserver(WebContentsImpl* owner, WebContents* watched_contents)
392 : WebContentsObserver(watched_contents),
393 owner_(owner) {
394 }
395
396 // WebContentsObserver:
dchengc2282aa2014-10-21 12:07:58397 void WebContentsDestroyed() override {
[email protected]12a46832014-05-09 13:35:58398 owner_->OnWebContentsDestroyed(
399 static_cast<WebContentsImpl*>(web_contents()));
[email protected]7fff43e2013-05-21 20:21:10400 }
401
402 private:
403 WebContentsImpl* owner_;
404
405 DISALLOW_COPY_AND_ASSIGN(DestructionObserver);
406};
407
Joel Hockey85b379d2017-12-11 10:42:13408// WebContentsImpl::ColorChooser ----------------------------------------------
Joel Hockey163835a2017-12-20 11:51:57409class WebContentsImpl::ColorChooser : public blink::mojom::ColorChooser {
Joel Hockey85b379d2017-12-11 10:42:13410 public:
411 ColorChooser(content::ColorChooser* chooser,
Julie Jeongeun Kim8e2879e2019-08-06 23:55:20412 mojo::PendingReceiver<blink::mojom::ColorChooser> receiver,
413 mojo::PendingRemote<blink::mojom::ColorChooserClient> client)
Joel Hockey85b379d2017-12-11 10:42:13414 : chooser_(chooser),
Julie Jeongeun Kim8e2879e2019-08-06 23:55:20415 receiver_(this, std::move(receiver)),
Joel Hockey85b379d2017-12-11 10:42:13416 client_(std::move(client)) {
Julie Jeongeun Kim8e2879e2019-08-06 23:55:20417 receiver_.set_disconnect_handler(
Joel Hockey85b379d2017-12-11 10:42:13418 base::BindOnce([](content::ColorChooser* chooser) { chooser->End(); },
419 base::Unretained(chooser)));
420 }
[email protected]8ed16472014-04-11 19:02:48421
Joel Hockey85b379d2017-12-11 10:42:13422 ~ColorChooser() override { chooser_->End(); }
[email protected]8ed16472014-04-11 19:02:48423
Joel Hockey85b379d2017-12-11 10:42:13424 void SetSelectedColor(SkColor color) override {
425 chooser_->SetSelectedColor(color);
426 }
427
428 void DidChooseColorInColorChooser(SkColor color) {
429 client_->DidChooseColor(color);
430 }
431
432 private:
433 // Color chooser that was opened by this tab.
434 std::unique_ptr<content::ColorChooser> chooser_;
435
Julie Jeongeun Kim8e2879e2019-08-06 23:55:20436 // mojo receiver.
437 mojo::Receiver<blink::mojom::ColorChooser> receiver_;
Joel Hockey85b379d2017-12-11 10:42:13438
439 // mojo renderer client.
Julie Jeongeun Kim8e2879e2019-08-06 23:55:20440 mojo::Remote<blink::mojom::ColorChooserClient> client_;
Joel Hockey85b379d2017-12-11 10:42:13441};
nicka0ac8382016-12-15 23:59:23442
lazyboy6ec48b2a2015-06-29 15:18:14443// WebContentsImpl::WebContentsTreeNode ----------------------------------------
lfg601233692017-03-06 22:45:44444WebContentsImpl::WebContentsTreeNode::WebContentsTreeNode(
445 WebContentsImpl* current_web_contents)
446 : current_web_contents_(current_web_contents),
447 outer_web_contents_(nullptr),
lazyboy6ec48b2a2015-06-29 15:18:14448 outer_contents_frame_tree_node_id_(
avallee0206f782016-07-28 18:55:33449 FrameTreeNode::kFrameTreeNodeInvalidId),
lfg601233692017-03-06 22:45:44450 focused_web_contents_(current_web_contents) {}
lazyboy6ec48b2a2015-06-29 15:18:14451
Lucas Furukawa Gadani3d38ec152018-10-26 20:55:18452WebContentsImpl::WebContentsTreeNode::~WebContentsTreeNode() {}
lazyboy6ec48b2a2015-06-29 15:18:14453
Lucas Furukawa Gadani99125822019-01-03 15:41:49454std::unique_ptr<WebContents>
455WebContentsImpl::WebContentsTreeNode::DisconnectFromOuterWebContents() {
456 std::unique_ptr<WebContents> inner_contents =
457 outer_web_contents_->node_.DetachInnerWebContents(current_web_contents_);
458 OuterContentsFrameTreeNode()->RemoveObserver(this);
459 outer_contents_frame_tree_node_id_ = FrameTreeNode::kFrameTreeNodeInvalidId;
460 outer_web_contents_ = nullptr;
461 return inner_contents;
462}
463
paulmeyerfeafc2d2017-04-25 21:46:40464void WebContentsImpl::WebContentsTreeNode::AttachInnerWebContents(
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:50465 std::unique_ptr<WebContents> inner_web_contents,
466 RenderFrameHostImpl* render_frame_host) {
467 WebContentsImpl* inner_web_contents_impl =
468 static_cast<WebContentsImpl*>(inner_web_contents.get());
469 WebContentsTreeNode& inner_web_contents_node = inner_web_contents_impl->node_;
470
471 inner_web_contents_node.focused_web_contents_ = nullptr;
472 inner_web_contents_node.outer_web_contents_ = current_web_contents_;
473 inner_web_contents_node.outer_contents_frame_tree_node_id_ =
474 render_frame_host->frame_tree_node()->frame_tree_node_id();
475
Lucas Furukawa Gadani3d38ec152018-10-26 20:55:18476 inner_web_contents_.push_back(std::move(inner_web_contents));
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:50477
478 render_frame_host->frame_tree_node()->AddObserver(&inner_web_contents_node);
paulmeyerfeafc2d2017-04-25 21:46:40479}
480
Lucas Furukawa Gadani3d38ec152018-10-26 20:55:18481std::unique_ptr<WebContents>
482WebContentsImpl::WebContentsTreeNode::DetachInnerWebContents(
paulmeyerfeafc2d2017-04-25 21:46:40483 WebContentsImpl* inner_web_contents) {
Lucas Furukawa Gadani3d38ec152018-10-26 20:55:18484 std::unique_ptr<WebContents> detached_contents;
485 for (std::unique_ptr<WebContents>& web_contents : inner_web_contents_) {
486 if (web_contents.get() == inner_web_contents) {
487 detached_contents = std::move(web_contents);
488 std::swap(web_contents, inner_web_contents_.back());
489 inner_web_contents_.pop_back();
490 return detached_contents;
491 }
492 }
493
494 NOTREACHED();
495 return nullptr;
paulmeyerfeafc2d2017-04-25 21:46:40496}
497
lfga3e2bdc2017-03-07 20:44:01498FrameTreeNode*
499WebContentsImpl::WebContentsTreeNode::OuterContentsFrameTreeNode() const {
500 return FrameTreeNode::GloballyFindByID(outer_contents_frame_tree_node_id_);
lfg601233692017-03-06 22:45:44501}
502
lfga3e2bdc2017-03-07 20:44:01503void WebContentsImpl::WebContentsTreeNode::OnFrameTreeNodeDestroyed(
504 FrameTreeNode* node) {
505 DCHECK_EQ(outer_contents_frame_tree_node_id_, node->frame_tree_node_id())
506 << "WebContentsTreeNode should only receive notifications for the "
507 "FrameTreeNode in its outer WebContents that hosts it.";
Lucas Furukawa Gadani3d38ec152018-10-26 20:55:18508
509 // Deletes |this| too.
510 outer_web_contents_->node_.DetachInnerWebContents(current_web_contents_);
lazyboy6ec48b2a2015-06-29 15:18:14511}
512
avallee0206f782016-07-28 18:55:33513void WebContentsImpl::WebContentsTreeNode::SetFocusedWebContents(
514 WebContentsImpl* web_contents) {
515 DCHECK(!outer_web_contents())
516 << "Only the outermost WebContents tracks focus.";
517 focused_web_contents_ = web_contents;
518}
519
paulmeyerfeafc2d2017-04-25 21:46:40520WebContentsImpl*
521WebContentsImpl::WebContentsTreeNode::GetInnerWebContentsInFrame(
522 const FrameTreeNode* frame) {
523 auto ftn_id = frame->frame_tree_node_id();
Lucas Furukawa Gadani3d38ec152018-10-26 20:55:18524 for (auto& contents : inner_web_contents_) {
525 WebContentsImpl* impl = static_cast<WebContentsImpl*>(contents.get());
526 if (impl->node_.outer_contents_frame_tree_node_id() == ftn_id) {
527 return impl;
paulmeyerfeafc2d2017-04-25 21:46:40528 }
529 }
530 return nullptr;
531}
532
Lucas Furukawa Gadani3d38ec152018-10-26 20:55:18533std::vector<WebContentsImpl*>
534WebContentsImpl::WebContentsTreeNode::GetInnerWebContents() const {
535 std::vector<WebContentsImpl*> inner_web_contents;
536 for (auto& contents : inner_web_contents_)
537 inner_web_contents.push_back(static_cast<WebContentsImpl*>(contents.get()));
538
539 return inner_web_contents;
paulmeyerfeafc2d2017-04-25 21:46:40540}
541
[email protected]b172aee2012-04-10 17:05:26542// WebContentsImpl -------------------------------------------------------------
[email protected]420ae012009-04-24 05:16:32543
alexmose201c7cd2015-06-10 17:14:21544WebContentsImpl::WebContentsImpl(BrowserContext* browser_context)
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28545 : delegate_(nullptr),
[email protected]69e797f2013-04-30 01:10:22546 controller_(this, browser_context),
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28547 render_view_host_delegate_view_(nullptr),
alexmose201c7cd2015-06-10 17:14:21548 created_with_opener_(false),
Lucas Furukawa Gadani99125822019-01-03 15:41:49549 node_(this),
[email protected]6ea6bdf2013-12-06 13:35:01550 frame_tree_(new NavigatorImpl(&controller_, this),
blundellc57b93f2014-10-29 13:19:57551 this,
552 this,
553 this,
554 this),
[email protected]6dfed692014-05-22 04:18:03555 is_load_to_different_document_(false),
[email protected]443b80e2010-12-14 00:42:23556 crashed_status_(base::TERMINATION_STATUS_STILL_RUNNING),
557 crashed_error_code_(0),
[email protected]d5f942ba2008-09-26 19:30:34558 waiting_for_response_(false),
[email protected]fcf75d42013-12-03 20:11:26559 load_state_(net::LOAD_STATE_IDLE, base::string16()),
[email protected]094e5b22009-09-25 04:23:56560 upload_size_(0),
561 upload_position_(0),
dfalcantarae6b7b46752015-07-10 18:14:16562 is_resume_pending_(false),
creisadca9862017-06-16 19:12:11563 interstitial_page_(nullptr),
[email protected]b4c84012014-04-28 19:51:10564 has_accessed_initial_document_(false),
Collin Bakerc5259962019-11-14 17:51:58565 visible_capturer_count_(0),
566 hidden_capturer_count_(0),
[email protected]fdd61c62009-04-22 19:22:57567 is_being_destroyed_(false),
creis97867aa2017-02-17 18:39:45568 is_notifying_observers_(false),
[email protected]fdd61c62009-04-22 19:22:57569 notify_disconnection_(false),
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28570 dialog_manager_(nullptr),
[email protected]7ab1e7d62009-10-14 23:32:01571 is_showing_before_unload_dialog_(false),
gab0dccfef2015-05-20 18:43:39572 last_active_time_(base::TimeTicks::Now()),
[email protected]ebf40a72010-07-22 01:46:38573 closed_by_user_gesture_(false),
danakj938b37a62019-09-24 18:35:54574 minimum_zoom_percent_(
575 static_cast<int>(blink::kMinimumPageZoomFactor * 100)),
576 maximum_zoom_percent_(
577 static_cast<int>(blink::kMaximumPageZoomFactor * 100)),
w.shackleton49bcd392016-01-06 17:38:22578 zoom_scroll_remainder_(0),
alexmosc9e76ec2016-05-16 22:59:37579 fullscreen_widget_process_id_(ChildProcessHost::kInvalidUniqueID),
[email protected]5dcaf8e2013-12-28 01:31:42580 fullscreen_widget_routing_id_(MSG_ROUTING_NONE),
[email protected]0b431992014-06-24 00:08:03581 fullscreen_widget_had_focus_at_shutdown_(false),
[email protected]34ff1cfc2014-08-20 06:16:05582 force_disable_overscroll_content_(false),
[email protected]95640212014-07-26 18:14:30583 last_dialog_suppressed_(false),
waffles1c12bf402016-02-10 17:45:48584 accessibility_mode_(
Dominic Mazzoni21fb0282019-02-13 18:32:47585 GetContentClient()->browser()->GetAXModeForBrowserContext(
586 browser_context)),
avayvodcc85bbd2015-08-28 19:11:15587 audio_stream_monitor_(this),
Thomas Guilbert882c4cec2019-02-14 05:55:45588 media_web_contents_observer_(
589 std::make_unique<MediaWebContentsObserver>(this)),
mcnee336ea2c2017-05-23 22:50:59590#if !defined(OS_ANDROID)
mcnee432e47d2015-11-09 19:37:46591 page_scale_factor_is_one_(true),
mcnee336ea2c2017-05-23 22:50:59592#endif // !defined(OS_ANDROID)
qinmin72e8bd02016-10-21 19:35:37593 is_overlay_content_(false),
ekaramadf6750aa2017-06-06 18:29:42594 showing_context_menu_(false),
W. James MacLean62198672019-06-04 16:46:10595 text_autosizer_page_info_({0, 0, 1.f}),
Jeremy Roman3bca4bf2019-07-11 03:41:25596 had_inner_webcontents_(false) {
danakjf4b9e942019-11-29 15:43:04597 frame_tree_.SetFrameRemoveListener(base::BindRepeating(
598 &WebContentsImpl::OnFrameRemoved, base::Unretained(this)));
brettw4b461082016-11-19 18:55:16599#if BUILDFLAG(ENABLE_PLUGINS)
zqzhang181047e62016-07-01 13:37:17600 pepper_playback_observer_.reset(new PepperPlaybackObserver(this));
601#endif
blundelle75a8f92017-03-27 08:11:17602
Becca Hughes86334622018-07-09 21:29:38603#if defined(OS_ANDROID)
Elly Fong-Jonescdd7ae82019-02-20 23:26:33604 display_cutout_host_impl_ = std::make_unique<DisplayCutoutHostImpl>(this);
Becca Hughes86334622018-07-09 21:29:38605#endif
[email protected]332af7732009-03-11 13:21:35606}
initial.commit09911bf2008-07-26 23:55:29607
[email protected]b172aee2012-04-10 17:05:26608WebContentsImpl::~WebContentsImpl() {
creisba91ba82017-05-12 22:56:19609 // Imperfect sanity check against double free, given some crashes unexpectedly
610 // observed in the wild.
611 CHECK(!is_being_destroyed_);
612
613 // We generally keep track of is_being_destroyed_ to let other features know
614 // to avoid certain actions during destruction.
[email protected]420ae012009-04-24 05:16:32615 is_being_destroyed_ = true;
616
creis97867aa2017-02-17 18:39:45617 // A WebContents should never be deleted while it is notifying observers,
creisba91ba82017-05-12 22:56:19618 // since this will lead to a use-after-free as it continues to notify later
creis97867aa2017-02-17 18:39:45619 // observers.
620 CHECK(!is_notifying_observers_);
621
kenrb2a565f82015-09-02 20:24:59622 rwh_input_event_router_.reset();
623
Julie Jeongeun Kim7d0986172019-12-04 03:17:25624 for (auto& entry : receiver_sets_)
625 entry.second->CloseAllReceivers();
rockotf62002a2016-09-15 00:08:59626
avallee0206f782016-07-28 18:55:33627 WebContentsImpl* outermost = GetOutermostWebContents();
avallee9993fca2016-11-17 06:16:50628 if (this != outermost && ContainsOrIsFocusedWebContents()) {
avallee0206f782016-07-28 18:55:33629 // If the current WebContents is in focus, unset it.
avallee9993fca2016-11-17 06:16:50630 outermost->SetAsFocusedWebContentsIfNecessary();
avallee0206f782016-07-28 18:55:33631 }
632
Nick Carterd73635b2018-03-13 18:31:41633 if (mouse_lock_widget_)
634 mouse_lock_widget_->RejectMouseLockOrUnlockIfNecessary();
635
dcheng72be33c2016-01-21 04:11:31636 for (FrameTreeNode* node : frame_tree_.Nodes()) {
637 // Delete all RFHs pending shutdown, which will lead the corresponding RVHs
638 // to be shutdown and be deleted as well.
dcheng57e39e22016-01-21 00:25:38639 node->render_manager()->ClearRFHsPendingShutdown();
dcheng57e39e22016-01-21 00:25:38640 node->render_manager()->ClearWebUIInstances();
dcheng72be33c2016-01-21 04:11:31641 }
carlosk35f35af2015-12-01 10:55:40642
dcheng57e39e22016-01-21 00:25:38643 for (RenderWidgetHostImpl* widget : created_widgets_)
644 widget->DetachDelegate();
[email protected]b24b68a2012-09-24 21:57:26645 created_widgets_.clear();
646
[email protected]3ab9cb82011-06-03 18:02:07647 // Clear out any JavaScript state.
avi5d3b8692016-10-12 22:00:46648 if (dialog_manager_) {
avi6879fcf2017-01-21 05:27:53649 dialog_manager_->CancelDialogs(this, /*reset_state=*/true);
avi5d3b8692016-10-12 22:00:46650 }
[email protected]3ab9cb82011-06-03 18:02:07651
Joel Hockey85b379d2017-12-11 10:42:13652 color_chooser_.reset();
Rakina Zata Amni16196122018-11-14 02:14:00653 find_request_manager_.reset();
[email protected]da8543762012-03-20 08:52:20654
[email protected]420ae012009-04-24 05:16:32655 NotifyDisconnected();
[email protected]420ae012009-04-24 05:16:32656
[email protected]ca13a442012-04-17 14:00:12657 // Notify any observer that have a reference on this WebContents.
[email protected]8ff00d72012-10-23 19:12:21658 NotificationService::current()->Notify(
659 NOTIFICATION_WEB_CONTENTS_DESTROYED,
660 Source<WebContents>(this),
661 NotificationService::NoDetails());
[email protected]420ae012009-04-24 05:16:32662
Arthur Sonzogni5a9a88c2018-10-18 13:16:00663 // Destroy all subframes now. This notifies observers.
664 GetMainFrame()->ResetChildren();
[email protected]df0c843f2014-05-23 20:37:23665 GetRenderManager()->ResetProxyHosts();
[email protected]c06c58c2014-03-12 20:31:59666
clamy0e119882015-07-31 16:12:33667 // Manually call the observer methods for the root frame tree node. It is
668 // necessary to manually delete all objects tracking navigations
669 // (NavigationHandle, NavigationRequest) for observers to be properly
670 // notified of these navigations stopping before the WebContents is
671 // destroyed.
[email protected]df0c843f2014-05-23 20:37:23672 RenderFrameHostManager* root = GetRenderManager();
naskob21fe4872015-02-24 14:15:56673
Lowell Manners20250112019-10-18 15:06:15674 GetController().GetBackForwardCache().Shutdown();
675
naskob21fe4872015-02-24 14:15:56676 root->current_frame_host()->SetRenderFrameCreated(false);
clamyfc3850d2018-02-23 14:35:28677 root->current_frame_host()->ResetNavigationRequests();
clamy0e119882015-07-31 16:12:33678
Arthur Hemeryc3380172018-01-22 14:00:17679 // Do not update state as the WebContents is being destroyed.
Arthur Hemery241b9392019-10-24 11:08:41680 frame_tree_.root()->ResetNavigationRequest(true);
Arthur Hemeryc3380172018-01-22 14:00:17681 if (root->speculative_frame_host()) {
danakjf2342432019-05-15 21:46:09682 root->speculative_frame_host()->DeleteRenderFrame(
683 FrameDeleteIntention::kSpeculativeMainFrameForShutdown);
Arthur Hemeryc3380172018-01-22 14:00:17684 root->speculative_frame_host()->SetRenderFrameCreated(false);
clamyfc3850d2018-02-23 14:35:28685 root->speculative_frame_host()->ResetNavigationRequests();
clamy0e119882015-07-31 16:12:33686 }
[email protected]f273ee52013-10-18 16:05:27687
brettw4b461082016-11-19 18:55:16688#if BUILDFLAG(ENABLE_PLUGINS)
zqzhang181047e62016-07-01 13:37:17689 // Call this before WebContentsDestroyed() is broadcasted since
690 // AudioFocusManager will be destroyed after that.
691 pepper_playback_observer_.reset();
692#endif // defined(ENABLED_PLUGINS)
693
Chris Hamiltondf0d72cd2018-05-29 16:23:53694 // If audio is playing then notify external observers of the audio stream
695 // disappearing.
696 if (is_currently_audible_) {
697 is_currently_audible_ = false;
698 for (auto& observer : observers_)
699 observer.OnAudioStateChanged(false);
700
701 if (GetOuterWebContents())
702 GetOuterWebContents()->OnAudioStateChanged();
703 }
704
ericwilligersde386342016-10-19 02:35:09705 for (auto& observer : observers_)
706 observer.FrameDeleted(root->current_frame_host());
naskoa83483fb2015-02-27 16:57:10707
ericwilligersde386342016-10-19 02:35:09708 for (auto& observer : observers_)
709 observer.RenderViewDeleted(root->current_host());
[email protected]f273ee52013-10-18 16:05:27710
Kevin McNee576bb5d82019-05-29 16:47:51711#if defined(OS_ANDROID)
712 // For simplicity, destroy the Java WebContents before we notify of the
713 // destruction of the WebContents.
714 ClearWebContentsAndroid();
715#endif
716
ericwilligersde386342016-10-19 02:35:09717 for (auto& observer : observers_)
718 observer.WebContentsDestroyed();
[email protected]12a46832014-05-09 13:35:58719
Becca Hughesd11d6502018-07-31 17:01:28720 if (display_cutout_host_impl_)
721 display_cutout_host_impl_->WebContentsDestroyed();
722
ericwilligersde386342016-10-19 02:35:09723 for (auto& observer : observers_)
724 observer.ResetWebContents();
[email protected]232a5812011-03-04 22:42:08725
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28726 SetDelegate(nullptr);
[email protected]b5a1d11c2011-02-17 03:09:42727}
728
erikchen50735fd2018-05-05 15:08:33729std::unique_ptr<WebContentsImpl> WebContentsImpl::CreateWithOpener(
[email protected]54944cde2012-12-09 09:24:59730 const WebContents::CreateParams& params,
Ian Clelland5cbaaf82017-11-27 22:00:03731 RenderFrameHostImpl* opener_rfh) {
[email protected]e11f0e92013-06-12 15:12:03732 TRACE_EVENT0("browser", "WebContentsImpl::CreateWithOpener");
Ian Clelland5cbaaf82017-11-27 22:00:03733 FrameTreeNode* opener = nullptr;
734 if (opener_rfh)
735 opener = opener_rfh->frame_tree_node();
erikchen50735fd2018-05-05 15:08:33736 std::unique_ptr<WebContentsImpl> new_contents(
737 new WebContentsImpl(params.browser_context));
lukasza6f8ac622017-06-06 03:10:20738 new_contents->SetOpenerForNewContents(opener, params.opener_suppressed);
alexmose201c7cd2015-06-10 17:14:21739
alexmosaedfc6f2016-01-21 23:57:38740 // If the opener is sandboxed, a new popup must inherit the opener's sandbox
741 // flags, and these flags take effect immediately. An exception is if the
742 // opener's sandbox flags lack the PropagatesToAuxiliaryBrowsingContexts
743 // bit (which is controlled by the "allow-popups-to-escape-sandbox" token).
744 // See https://html.spec.whatwg.org/#attr-iframe-sandbox.
lukasza6f8ac622017-06-06 03:10:20745 FrameTreeNode* new_root = new_contents->GetFrameTree()->root();
alexmosaedfc6f2016-01-21 23:57:38746 if (opener) {
Ian Clelland5cbaaf82017-11-27 22:00:03747 blink::WebSandboxFlags opener_flags = opener_rfh->active_sandbox_flags();
alexmosaedfc6f2016-01-21 23:57:38748 const blink::WebSandboxFlags inherit_flag =
Blink Reformat1c4d759e2017-04-09 16:34:54749 blink::WebSandboxFlags::kPropagatesToAuxiliaryBrowsingContexts;
Ehsan Karamad39407082019-02-19 23:38:19750 bool sandbox_propagates_to_auxilary_context =
751 (opener_flags & inherit_flag) == inherit_flag;
752 if (sandbox_propagates_to_auxilary_context)
Ian Clellandcdc4f312017-10-13 22:24:12753 new_root->SetPendingFramePolicy({opener_flags, {}});
Ehsan Karamad39407082019-02-19 23:38:19754 if (opener_flags == blink::WebSandboxFlags::kNone ||
755 sandbox_propagates_to_auxilary_context) {
756 // TODO(ekaramad, iclelland): Do not propagate feature policies from non-
757 // sandboxed disowned openers (rel=noopener).
758 // If the current page is not sandboxed, or if the sandbox is to propagate
759 // to the popups then opener's feature policy will apply to the new popup
760 // as well.
761 new_root->SetOpenerFeaturePolicyState(
Ehsan Karamad34d10682019-02-26 20:55:04762 opener_rfh->feature_policy()->GetFeatureState());
alexmosaedfc6f2016-01-21 23:57:38763 }
764 }
765
mark a. foltzef394fce2017-10-21 09:11:02766 // Apply starting sandbox flags.
Luna Luc3fdacdf2017-11-08 04:48:53767 blink::FramePolicy frame_policy(new_root->pending_frame_policy());
mark a. foltzef394fce2017-10-21 09:11:02768 frame_policy.sandbox_flags |= params.starting_sandbox_flags;
769 new_root->SetPendingFramePolicy(frame_policy);
Charlie Hu5ffc0152019-12-06 15:59:53770 new_root->CommitFramePolicy(frame_policy);
mark a. foltzef394fce2017-10-21 09:11:02771
alexmose201c7cd2015-06-10 17:14:21772 // This may be true even when opener is null, such as when opening blocked
773 // popups.
alexmos090fae8e2015-05-28 17:09:28774 if (params.created_with_opener)
775 new_contents->created_with_opener_ = true;
776
Sean Toppingbbc4a2bd2019-01-30 02:16:14777 WebContentsImpl* outer_web_contents = nullptr;
[email protected]4858e432014-06-23 18:14:17778 if (params.guest_delegate) {
[email protected]83100cd2014-05-10 11:50:06779 // This makes |new_contents| act as a guest.
780 // For more info, see comment above class BrowserPluginGuest.
Albert J. Wongc55cc64d2018-10-12 02:50:04781 BrowserPluginGuest::CreateInWebContents(new_contents.get(),
782 params.guest_delegate);
Sean Toppingbbc4a2bd2019-01-30 02:16:14783 outer_web_contents = static_cast<WebContentsImpl*>(
784 params.guest_delegate->GetOwnerWebContents());
[email protected]83100cd2014-05-10 11:50:06785 }
mark a. foltzef394fce2017-10-21 09:11:02786
[email protected]54944cde2012-12-09 09:24:59787 new_contents->Init(params);
Sean Toppingbbc4a2bd2019-01-30 02:16:14788 if (outer_web_contents)
789 outer_web_contents->InnerWebContentsCreated(new_contents.get());
[email protected]d1198fd2012-08-13 22:50:19790 return new_contents;
791}
792
[email protected]95640212014-07-26 18:14:30793// static
794std::vector<WebContentsImpl*> WebContentsImpl::GetAllWebContents() {
795 std::vector<WebContentsImpl*> result;
dcheng59716272016-04-09 05:19:08796 std::unique_ptr<RenderWidgetHostIterator> widgets(
[email protected]95640212014-07-26 18:14:30797 RenderWidgetHostImpl::GetRenderWidgetHosts());
[email protected]95640212014-07-26 18:14:30798 while (RenderWidgetHost* rwh = widgets->GetNextHost()) {
[email protected]95640212014-07-26 18:14:30799 RenderViewHost* rvh = RenderViewHost::From(rwh);
800 if (!rvh)
801 continue;
802 WebContents* web_contents = WebContents::FromRenderViewHost(rvh);
803 if (!web_contents)
804 continue;
avidf38c952015-10-27 13:45:13805 if (web_contents->GetRenderViewHost() != rvh)
806 continue;
807 // Because a WebContents can only have one current RVH at a time, there will
808 // be no duplicate WebContents here.
809 result.push_back(static_cast<WebContentsImpl*>(web_contents));
[email protected]95640212014-07-26 18:14:30810 }
811 return result;
812}
813
clamya16aa8172015-05-26 13:07:25814// static
815WebContentsImpl* WebContentsImpl::FromFrameTreeNode(
paulmeyerfeafc2d2017-04-25 21:46:40816 const FrameTreeNode* frame_tree_node) {
clamya16aa8172015-05-26 13:07:25817 return static_cast<WebContentsImpl*>(
818 WebContents::FromRenderFrameHost(frame_tree_node->current_frame_host()));
819}
820
clamy0d32d6d2015-11-24 11:16:26821// static
822WebContents* WebContentsImpl::FromRenderFrameHostID(int render_process_host_id,
823 int render_frame_host_id) {
Min Qina1150112019-02-15 01:47:49824 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) ||
825 !BrowserThread::IsThreadInitialized(BrowserThread::UI));
clamy0d32d6d2015-11-24 11:16:26826 RenderFrameHost* render_frame_host =
827 RenderFrameHost::FromID(render_process_host_id, render_frame_host_id);
828 if (!render_frame_host)
829 return nullptr;
830
831 return WebContents::FromRenderFrameHost(render_frame_host);
832}
833
paulmeyerfeafc2d2017-04-25 21:46:40834// static
835WebContentsImpl* WebContentsImpl::FromOuterFrameTreeNode(
836 const FrameTreeNode* frame_tree_node) {
837 return WebContentsImpl::FromFrameTreeNode(frame_tree_node)
838 ->node_.GetInnerWebContentsInFrame(frame_tree_node);
839}
840
[email protected]b0936d22013-11-28 06:47:36841RenderFrameHostManager* WebContentsImpl::GetRenderManagerForTesting() {
[email protected]fa944cb82013-11-15 17:51:21842 return GetRenderManager();
[email protected]765187182012-01-11 23:59:28843}
844
nicka0ac8382016-12-15 23:59:23845bool WebContentsImpl::OnMessageReceived(RenderViewHostImpl* render_view_host,
[email protected]7bb761892012-07-20 09:32:47846 const IPC::Message& message) {
nicka0ac8382016-12-15 23:59:23847 for (auto& observer : observers_) {
Lukasz Anforowicz89f2c1b2017-08-30 21:36:46848 // TODO(nick, creis): https://crbug.com/758026: Replace all uses of this
849 // variant of OnMessageReceived with the version that takes a
850 // RenderFrameHost, and then delete it.
nicka0ac8382016-12-15 23:59:23851 if (observer.OnMessageReceived(message))
852 return true;
[email protected]64ffefa2014-05-10 12:06:33853 }
[email protected]403415a2011-01-10 18:57:53854
[email protected]724159a2010-12-30 01:11:18855 bool handled = true;
nicka0ac8382016-12-15 23:59:23856 IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(WebContentsImpl, message, render_view_host)
nicka0ac8382016-12-15 23:59:23857 IPC_MESSAGE_HANDLER(ViewHostMsg_PageScaleFactorChanged,
858 OnPageScaleFactorChanged)
W. James MacLean62198672019-06-04 16:46:10859 IPC_MESSAGE_HANDLER(
860 ViewHostMsg_NotifyTextAutosizerPageInfoChangedInLocalMainFrame,
861 OnTextAutosizerPageInfoChanged)
nicka0ac8382016-12-15 23:59:23862#if BUILDFLAG(ENABLE_PLUGINS)
863 IPC_MESSAGE_HANDLER(ViewHostMsg_RequestPpapiBrokerPermission,
864 OnRequestPpapiBrokerPermission)
865#endif
nicka0ac8382016-12-15 23:59:23866 IPC_MESSAGE_UNHANDLED(handled = false)
867 IPC_END_MESSAGE_MAP()
868
869 return handled;
870}
871
872bool WebContentsImpl::OnMessageReceived(RenderFrameHostImpl* render_frame_host,
873 const IPC::Message& message) {
Lukasz Anforowicz02923102017-10-09 18:11:37874 {
875 WebUIImpl* web_ui = render_frame_host->web_ui();
876 if (web_ui && web_ui->OnMessageReceived(message, render_frame_host))
877 return true;
878 }
879
nicka0ac8382016-12-15 23:59:23880 for (auto& observer : observers_) {
881 if (observer.OnMessageReceived(message, render_frame_host))
882 return true;
883 }
884
885 bool handled = true;
886 IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(WebContentsImpl, message, render_frame_host)
[email protected]cfa856d62014-02-22 07:58:40887 IPC_MESSAGE_HANDLER(FrameHostMsg_DomOperationResponse,
888 OnDomOperationResponse)
[email protected]8ed16472014-04-11 19:02:48889 IPC_MESSAGE_HANDLER(FrameHostMsg_DidFinishLoad, OnDidFinishLoad)
avi7830c852015-08-31 23:46:25890 IPC_MESSAGE_HANDLER(FrameHostMsg_DidLoadResourceFromMemoryCache,
[email protected]724159a2010-12-30 01:11:18891 OnDidLoadResourceFromMemoryCache)
avi3a3332622015-09-01 01:18:54892 IPC_MESSAGE_HANDLER(FrameHostMsg_DidRunInsecureContent,
[email protected]724159a2010-12-30 01:11:18893 OnDidRunInsecureContent)
estark910b4572015-12-09 20:55:41894 IPC_MESSAGE_HANDLER(FrameHostMsg_DidDisplayContentWithCertificateErrors,
895 OnDidDisplayContentWithCertificateErrors)
896 IPC_MESSAGE_HANDLER(FrameHostMsg_DidRunContentWithCertificateErrors,
897 OnDidRunContentWithCertificateErrors)
Dave Tapuska549baab2019-03-19 15:52:40898 IPC_MESSAGE_HANDLER(FrameHostMsg_GoToEntryAtOffset, OnGoToEntryAtOffset)
kouhei40f03cb2015-09-24 07:47:01899 IPC_MESSAGE_HANDLER(FrameHostMsg_UpdatePageImportanceSignals,
900 OnUpdatePageImportanceSignals)
lukasza178ff092017-06-07 23:39:50901 IPC_MESSAGE_HANDLER(FrameHostMsg_UpdateFaviconURL, OnUpdateFaviconURL)
brettw4b461082016-11-19 18:55:16902#if BUILDFLAG(ENABLE_PLUGINS)
emaxxe70f5e12015-05-29 11:26:00903 IPC_MESSAGE_HANDLER(FrameHostMsg_PepperInstanceCreated,
904 OnPepperInstanceCreated)
905 IPC_MESSAGE_HANDLER(FrameHostMsg_PepperInstanceDeleted,
906 OnPepperInstanceDeleted)
tommyclie6633ca72014-10-31 00:40:42907 IPC_MESSAGE_HANDLER(FrameHostMsg_PepperPluginHung, OnPepperPluginHung)
zqzhang181047e62016-07-01 13:37:17908 IPC_MESSAGE_HANDLER(FrameHostMsg_PepperStartsPlayback,
909 OnPepperStartsPlayback)
910 IPC_MESSAGE_HANDLER(FrameHostMsg_PepperStopsPlayback,
911 OnPepperStopsPlayback)
tommyclie6633ca72014-10-31 00:40:42912 IPC_MESSAGE_HANDLER(FrameHostMsg_PluginCrashed, OnPluginCrashed)
thestigc4cac8f2014-09-04 21:17:50913#endif
[email protected]724159a2010-12-30 01:11:18914 IPC_MESSAGE_UNHANDLED(handled = false)
[email protected]e44d1342014-05-16 21:29:33915 IPC_END_MESSAGE_MAP()
[email protected]724159a2010-12-30 01:11:18916
[email protected]724159a2010-12-30 01:11:18917 return handled;
918}
919
[email protected]d1198fd2012-08-13 22:50:19920NavigationControllerImpl& WebContentsImpl::GetController() {
[email protected]f5fa20e2011-12-21 22:35:56921 return controller_;
922}
923
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:41924BrowserContext* WebContentsImpl::GetBrowserContext() {
[email protected]a26023822011-12-29 00:23:55925 return controller_.GetBrowserContext();
[email protected]627e0512011-12-21 22:55:30926}
927
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:41928const GURL& WebContentsImpl::GetURL() {
Peter Boström101d7672018-12-15 00:43:59929 return GetVisibleURL();
[email protected]be1f56ab2011-12-22 06:55:31930}
931
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:41932const GURL& WebContentsImpl::GetVisibleURL() {
[email protected]c854a7e2013-05-21 16:42:24933 // We may not have a navigation entry yet.
[email protected]59167c22013-06-03 18:07:32934 NavigationEntry* entry = controller_.GetVisibleEntry();
[email protected]c854a7e2013-05-21 16:42:24935 return entry ? entry->GetVirtualURL() : GURL::EmptyGURL();
936}
937
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:41938const GURL& WebContentsImpl::GetLastCommittedURL() {
[email protected]c854a7e2013-05-21 16:42:24939 // We may not have a navigation entry yet.
940 NavigationEntry* entry = controller_.GetLastCommittedEntry();
941 return entry ? entry->GetVirtualURL() : GURL::EmptyGURL();
942}
943
[email protected]8ff00d72012-10-23 19:12:21944WebContentsDelegate* WebContentsImpl::GetDelegate() {
[email protected]be1f56ab2011-12-22 06:55:31945 return delegate_;
946}
947
[email protected]8ff00d72012-10-23 19:12:21948void WebContentsImpl::SetDelegate(WebContentsDelegate* delegate) {
[email protected]be1f56ab2011-12-22 06:55:31949 // TODO(cbentzel): remove this debugging code?
950 if (delegate == delegate_)
951 return;
952 if (delegate_)
953 delegate_->Detach(this);
954 delegate_ = delegate;
[email protected]a6b73c62013-02-11 23:05:08955 if (delegate_) {
[email protected]be1f56ab2011-12-22 06:55:31956 delegate_->Attach(this);
[email protected]a6b73c62013-02-11 23:05:08957 // Ensure the visible RVH reflects the new delegate's preferences.
[email protected]86f98a22013-03-20 14:35:00958 if (view_)
[email protected]e85165c642014-06-10 14:34:31959 view_->SetOverscrollControllerEnabled(CanOverscrollContent());
Joel Einbinder848001c2017-09-19 21:39:31960 if (GetRenderViewHost())
961 RenderFrameDevToolsAgentHost::WebContentsCreated(this);
[email protected]a6b73c62013-02-11 23:05:08962 }
[email protected]be1f56ab2011-12-22 06:55:31963}
964
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:41965RenderFrameHostImpl* WebContentsImpl::GetMainFrame() {
[email protected]94d0cc12013-12-18 00:07:41966 return frame_tree_.root()->current_frame_host();
[email protected]60eca4eb2013-12-06 00:02:16967}
968
nick53d5cbf2015-04-23 22:50:14969RenderFrameHostImpl* WebContentsImpl::GetFocusedFrame() {
970 FrameTreeNode* focused_node = frame_tree_.GetFocusedFrame();
971 if (!focused_node)
972 return nullptr;
973 return focused_node->current_frame_host();
[email protected]9c9343b2014-03-08 02:56:07974}
975
rob3e2a0732016-01-06 21:22:09976RenderFrameHostImpl* WebContentsImpl::FindFrameByFrameTreeNodeId(
creisf71a2632017-05-04 19:03:50977 int frame_tree_node_id,
978 int process_id) {
979 FrameTreeNode* frame = frame_tree_.FindByID(frame_tree_node_id);
980
981 // Sanity check that this is in the caller's expected process. Otherwise a
982 // recent cross-process navigation may have led to a privilege change that the
983 // caller is not expecting.
984 if (!frame ||
985 frame->current_frame_host()->GetProcess()->GetID() != process_id)
986 return nullptr;
987
988 return frame->current_frame_host();
989}
990
991RenderFrameHostImpl* WebContentsImpl::UnsafeFindFrameByFrameTreeNodeId(
rob3e2a0732016-01-06 21:22:09992 int frame_tree_node_id) {
creisf71a2632017-05-04 19:03:50993 // Beware using this! The RenderFrameHost may have changed since the caller
994 // obtained frame_tree_node_id.
rob3e2a0732016-01-06 21:22:09995 FrameTreeNode* frame = frame_tree_.FindByID(frame_tree_node_id);
996 return frame ? frame->current_frame_host() : nullptr;
997}
998
[email protected]a86c0e962013-12-17 17:10:39999void WebContentsImpl::ForEachFrame(
Lei Zhangebcc6302018-01-12 19:46:451000 const base::RepeatingCallback<void(RenderFrameHost*)>& on_frame) {
dcheng57e39e22016-01-21 00:25:381001 for (FrameTreeNode* node : frame_tree_.Nodes()) {
1002 on_frame.Run(node->current_frame_host());
1003 }
[email protected]a86c0e962013-12-17 17:10:391004}
1005
dchengafb53e22016-02-04 08:11:081006std::vector<RenderFrameHost*> WebContentsImpl::GetAllFrames() {
1007 std::vector<RenderFrameHost*> frame_hosts;
1008 for (FrameTreeNode* node : frame_tree_.Nodes())
1009 frame_hosts.push_back(node->current_frame_host());
1010 return frame_hosts;
1011}
1012
lukaszacbdf52e2016-01-15 21:19:511013int WebContentsImpl::SendToAllFrames(IPC::Message* message) {
1014 int number_of_messages = 0;
dchengafb53e22016-02-04 08:11:081015 for (RenderFrameHost* rfh : GetAllFrames()) {
lukasza1a9ab2422016-03-02 18:47:251016 if (!rfh->IsRenderFrameLive())
1017 continue;
1018
dchengafb53e22016-02-04 08:11:081019 ++number_of_messages;
1020 IPC::Message* message_copy = new IPC::Message(*message);
1021 message_copy->set_routing_id(rfh->GetRoutingID());
1022 rfh->Send(message_copy);
1023 }
[email protected]a86c0e962013-12-17 17:10:391024 delete message;
lukaszacbdf52e2016-01-15 21:19:511025 return number_of_messages;
[email protected]a86c0e962013-12-17 17:10:391026}
1027
lfgdb5c4ed2016-03-04 23:09:071028void WebContentsImpl::SendPageMessage(IPC::Message* msg) {
alexmos136fd6e62016-08-15 20:58:411029 frame_tree_.root()->render_manager()->SendPageMessage(msg, nullptr);
lfgdb5c4ed2016-03-04 23:09:071030}
1031
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411032RenderViewHostImpl* WebContentsImpl::GetRenderViewHost() {
[email protected]fa944cb82013-11-15 17:51:211033 return GetRenderManager()->current_host();
[email protected]be1f56ab2011-12-22 06:55:311034}
1035
creis89a0f782015-05-27 16:13:171036void WebContentsImpl::CancelActiveAndPendingDialogs() {
avi5d3b8692016-10-12 22:00:461037 if (dialog_manager_) {
avi6879fcf2017-01-21 05:27:531038 dialog_manager_->CancelDialogs(this, /*reset_state=*/false);
avi5d3b8692016-10-12 22:00:461039 }
creis89a0f782015-05-27 16:13:171040 if (browser_plugin_embedder_)
1041 browser_plugin_embedder_->CancelGuestDialogs();
1042}
1043
naskoc0fceff2015-04-30 15:53:521044void WebContentsImpl::ClosePage() {
1045 GetRenderViewHost()->ClosePage();
1046}
1047
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411048RenderWidgetHostView* WebContentsImpl::GetRenderWidgetHostView() {
[email protected]fa944cb82013-11-15 17:51:211049 return GetRenderManager()->GetRenderWidgetHostView();
[email protected]be1f56ab2011-12-22 06:55:311050}
1051
lfg265a2672016-04-23 03:11:021052RenderWidgetHostView* WebContentsImpl::GetTopLevelRenderWidgetHostView() {
1053 if (GetOuterWebContents())
1054 return GetOuterWebContents()->GetTopLevelRenderWidgetHostView();
1055 return GetRenderManager()->GetRenderWidgetHostView();
1056}
1057
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411058RenderWidgetHostView* WebContentsImpl::GetFullscreenRenderWidgetHostView() {
vmpstr6d9996c82017-02-23 00:43:251059 if (auto* widget_host = GetFullscreenRenderWidgetHost())
ekaramadaeb3c5c2016-11-09 03:22:351060 return widget_host->GetView();
1061 return nullptr;
[email protected]4aebbca2013-09-17 22:26:491062}
1063
[email protected]8ff00d72012-10-23 19:12:211064WebContentsView* WebContentsImpl::GetView() const {
[email protected]be1f56ab2011-12-22 06:55:311065 return view_.get();
1066}
1067
leon.han552e9de2017-02-09 14:37:301068void WebContentsImpl::OnScreenOrientationChange() {
1069 DCHECK(screen_orientation_provider_);
Becca Hughese9e27952018-06-25 17:08:271070 screen_orientation_provider_->OnOrientationChange();
leon.hane4db177a2017-02-07 14:19:161071}
1072
Alan Cutter92d59252019-03-29 02:32:331073base::Optional<SkColor> WebContentsImpl::GetThemeColor() {
Carlos Caballero1215f882019-10-29 15:58:431074 return GetRenderViewHost()->theme_color();
yusufod41c5f92015-03-06 00:14:281075}
1076
Doug Turner63f3c7b2017-07-29 05:10:011077void WebContentsImpl::SetAccessibilityMode(ui::AXMode mode) {
[email protected]95640212014-07-26 18:14:301078 if (mode == accessibility_mode_)
1079 return;
1080
dmazzonif8a31042016-12-03 00:52:131081 // Don't allow accessibility to be enabled for WebContents that are never
1082 // visible, like background pages.
1083 if (IsNeverVisible())
1084 return;
1085
[email protected]95640212014-07-26 18:14:301086 accessibility_mode_ = mode;
dcheng57e39e22016-01-21 00:25:381087
1088 for (FrameTreeNode* node : frame_tree_.Nodes()) {
dmazzonif8a31042016-12-03 00:52:131089 UpdateAccessibilityModeOnFrame(node->current_frame_host());
clamy610c63b32017-12-22 15:05:181090 // Also update accessibility mode on the speculative RenderFrameHost for
1091 // this FrameTreeNode, if one exists.
Alex Moshchuk3d8a1f392017-10-24 19:01:261092 RenderFrameHost* speculative_frame_host =
1093 node->render_manager()->speculative_frame_host();
1094 if (speculative_frame_host)
1095 UpdateAccessibilityModeOnFrame(speculative_frame_host);
dcheng57e39e22016-01-21 00:25:381096 }
[email protected]95640212014-07-26 18:14:301097}
1098
Doug Turner63f3c7b2017-07-29 05:10:011099void WebContentsImpl::AddAccessibilityMode(ui::AXMode mode) {
1100 ui::AXMode new_mode(accessibility_mode_);
dougtcd3dad732017-03-14 03:26:231101 new_mode |= mode;
1102 SetAccessibilityMode(new_mode);
[email protected]95640212014-07-26 18:14:301103}
1104
Avi Drissman1e620f32018-03-16 13:57:291105// Helper class used by WebContentsImpl::RequestAXTreeSnapshot.
1106// Handles the callbacks from parallel snapshot requests to each frame,
1107// and feeds the results to an AXTreeCombiner, which converts them into a
1108// single combined accessibility tree.
1109class WebContentsImpl::AXTreeSnapshotCombiner
1110 : public base::RefCounted<AXTreeSnapshotCombiner> {
1111 public:
1112 explicit AXTreeSnapshotCombiner(AXTreeSnapshotCallback callback)
1113 : callback_(std::move(callback)) {}
1114
1115 AXTreeSnapshotCallback AddFrame(bool is_root) {
1116 // Adds a reference to |this|.
1117 return base::BindOnce(&AXTreeSnapshotCombiner::ReceiveSnapshot, this,
1118 is_root);
1119 }
1120
1121 void ReceiveSnapshot(bool is_root, const ui::AXTreeUpdate& snapshot) {
1122 combiner_.AddTree(snapshot, is_root);
1123 }
1124
1125 private:
1126 friend class base::RefCounted<AXTreeSnapshotCombiner>;
1127
1128 // This is called automatically after the last call to ReceiveSnapshot
1129 // when there are no more references to this object.
1130 ~AXTreeSnapshotCombiner() {
1131 combiner_.Combine();
1132 std::move(callback_).Run(combiner_.combined());
1133 }
1134
1135 ui::AXTreeCombiner combiner_;
1136 AXTreeSnapshotCallback callback_;
1137};
1138
1139void WebContentsImpl::RequestAXTreeSnapshot(AXTreeSnapshotCallback callback,
1140 ui::AXMode ax_mode) {
dmazzonid95ae842016-04-12 21:17:391141 // Send a request to each of the frames in parallel. Each one will return
1142 // an accessibility tree snapshot, and AXTreeSnapshotCombiner will combine
1143 // them into a single tree and call |callback| with that result, then
1144 // delete |combiner|.
Avi Drissman1e620f32018-03-16 13:57:291145 FrameTreeNode* root_node = frame_tree_.root();
tzik58faa742018-08-01 10:54:181146 auto combiner =
1147 base::MakeRefCounted<AXTreeSnapshotCombiner>(std::move(callback));
Avi Drissman1e620f32018-03-16 13:57:291148
tzik58faa742018-08-01 10:54:181149 RecursiveRequestAXTreeSnapshotOnFrame(root_node, combiner.get(), ax_mode);
Avi Drissman1e620f32018-03-16 13:57:291150}
1151
1152void WebContentsImpl::RecursiveRequestAXTreeSnapshotOnFrame(
1153 FrameTreeNode* root_node,
1154 AXTreeSnapshotCombiner* combiner,
1155 ui::AXMode ax_mode) {
dmazzonid95ae842016-04-12 21:17:391156 for (FrameTreeNode* frame_tree_node : frame_tree_.Nodes()) {
Avi Drissman1e620f32018-03-16 13:57:291157 WebContentsImpl* inner_contents =
1158 node_.GetInnerWebContentsInFrame(frame_tree_node);
1159 if (inner_contents) {
1160 inner_contents->RecursiveRequestAXTreeSnapshotOnFrame(root_node, combiner,
1161 ax_mode);
1162 } else {
1163 bool is_root = frame_tree_node == root_node;
1164 frame_tree_node->current_frame_host()->RequestAXTreeSnapshot(
1165 combiner->AddFrame(is_root), ax_mode);
1166 }
dmazzonid95ae842016-04-12 21:17:391167 }
dmazzoni83ba5c82015-04-14 07:11:511168}
1169
Becca Hughes3b5a43482018-07-17 22:31:551170void WebContentsImpl::NotifyViewportFitChanged(
1171 blink::mojom::ViewportFit value) {
1172 for (auto& observer : observers_)
1173 observer.ViewportFitChanged(value);
1174}
1175
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411176FindRequestManager* WebContentsImpl::GetFindRequestManagerForTesting() {
Ehsan Karamad6beb2ea2018-11-25 18:15:131177 return GetFindRequestManager();
1178}
1179
mcnee336ea2c2017-05-23 22:50:591180#if !defined(OS_ANDROID)
akabac6bd1212018-06-25 20:10:481181void WebContentsImpl::UpdateZoom() {
1182 RenderWidgetHostImpl* rwh = GetRenderViewHost()->GetWidget();
1183 if (rwh->GetView())
1184 rwh->SynchronizeVisualProperties();
wjmaclean64951902016-04-29 20:59:121185}
1186
wjmaclean64951902016-04-29 20:59:121187
1188void WebContentsImpl::UpdateZoomIfNecessary(const std::string& scheme,
akabac6bd1212018-06-25 20:10:481189 const std::string& host) {
wjmaclean64951902016-04-29 20:59:121190 NavigationEntry* entry = GetController().GetLastCommittedEntry();
1191 if (!entry)
1192 return;
1193
1194 GURL url = HostZoomMap::GetURLFromEntry(entry);
1195 if (host != net::GetHostOrSpecFromURL(url) ||
1196 (!scheme.empty() && !url.SchemeIs(scheme))) {
1197 return;
1198 }
1199
akabac6bd1212018-06-25 20:10:481200 UpdateZoom();
wjmaclean64951902016-04-29 20:59:121201}
mcnee336ea2c2017-05-23 22:50:591202#endif // !defined(OS_ANDROID)
wjmaclean64951902016-04-29 20:59:121203
Julie Jeongeun Kim7d0986172019-12-04 03:17:251204base::OnceClosure WebContentsImpl::AddReceiverSet(
rockotf62002a2016-09-15 00:08:591205 const std::string& interface_name,
Julie Jeongeun Kim7d0986172019-12-04 03:17:251206 WebContentsReceiverSet* receiver_set) {
rockotf62002a2016-09-15 00:08:591207 auto result =
Julie Jeongeun Kim7d0986172019-12-04 03:17:251208 receiver_sets_.insert(std::make_pair(interface_name, receiver_set));
rockotf62002a2016-09-15 00:08:591209 DCHECK(result.second);
Julie Jeongeun Kim7d0986172019-12-04 03:17:251210 return base::BindOnce(&WebContentsImpl::RemoveReceiverSet,
danakjf4b9e942019-11-29 15:43:041211 weak_factory_.GetWeakPtr(), interface_name);
rockotf62002a2016-09-15 00:08:591212}
1213
Julie Jeongeun Kim7d0986172019-12-04 03:17:251214WebContentsReceiverSet* WebContentsImpl::GetReceiverSet(
rockot8bbad222017-03-22 04:34:051215 const std::string& interface_name) {
Julie Jeongeun Kim7d0986172019-12-04 03:17:251216 auto it = receiver_sets_.find(interface_name);
1217 if (it == receiver_sets_.end())
rockot8bbad222017-03-22 04:34:051218 return nullptr;
1219 return it->second;
1220}
1221
paulmeyerfeafc2d2017-04-25 21:46:401222std::vector<WebContentsImpl*> WebContentsImpl::GetWebContentsAndAllInner() {
1223 std::vector<WebContentsImpl*> all_contents(1, this);
1224
1225 for (size_t i = 0; i != all_contents.size(); ++i) {
1226 for (auto* inner_contents : all_contents[i]->GetInnerWebContents()) {
Lucas Furukawa Gadani2ec00c82018-12-14 15:53:161227 all_contents.push_back(static_cast<WebContentsImpl*>(inner_contents));
paulmeyerfeafc2d2017-04-25 21:46:401228 }
1229 }
1230
1231 return all_contents;
1232}
1233
Sam McNally145fb172017-05-10 00:13:231234void WebContentsImpl::NotifyManifestUrlChanged(
1235 const base::Optional<GURL>& manifest_url) {
1236 for (auto& observer : observers_)
1237 observer.DidUpdateWebManifestURL(manifest_url);
1238}
1239
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411240WebUI* WebContentsImpl::GetWebUI() {
Nasko Oskov2f7d2112019-10-29 03:26:571241 WebUI* committed_web_ui = GetCommittedWebUI();
1242 if (committed_web_ui)
1243 return committed_web_ui;
1244
1245 if (GetRenderManager()->speculative_frame_host())
1246 return GetRenderManager()->speculative_frame_host()->web_ui();
1247
1248 return nullptr;
[email protected]be1f56ab2011-12-22 06:55:311249}
1250
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411251WebUI* WebContentsImpl::GetCommittedWebUI() {
carlosk35f35af2015-12-01 10:55:401252 return frame_tree_.root()->current_frame_host()->web_ui();
[email protected]d5f942ba2008-09-26 19:30:341253}
1254
Changwan Ryuc1134a82018-03-07 02:10:141255void WebContentsImpl::SetUserAgentOverride(const std::string& override,
1256 bool override_in_new_tabs) {
[email protected]bf70edce2012-06-20 22:32:221257 if (GetUserAgentOverride() == override)
1258 return;
1259
Changwan Ryuc1134a82018-03-07 02:10:141260 should_override_user_agent_in_new_tabs_ = override_in_new_tabs;
1261
[email protected]bf70edce2012-06-20 22:32:221262 renderer_preferences_.user_agent_override = override;
1263
Bruce Long1e3e1f542019-10-16 17:56:281264 // Send the new override string to all renderers in the current page.
1265 SyncRendererPrefs();
[email protected]bf70edce2012-06-20 22:32:221266
1267 // Reload the page if a load is currently in progress to avoid having
1268 // different parts of the page loaded using different user agents.
[email protected]6286a372013-10-09 04:03:271269 NavigationEntry* entry = controller_.GetVisibleEntry();
Ivan Kotenkov2c0d2bb32017-11-01 15:41:281270 if (IsLoading() && entry != nullptr && entry->GetIsOverridingUserAgent())
toyoshim6142d96f2016-12-19 09:07:251271 controller_.Reload(ReloadType::BYPASSING_CACHE, true);
[email protected]8d0f3312012-08-18 01:47:531272
ericwilligersde386342016-10-19 02:35:091273 for (auto& observer : observers_)
1274 observer.UserAgentOverrideSet(override);
[email protected]86ef6a392012-05-11 22:03:111275}
1276
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411277const std::string& WebContentsImpl::GetUserAgentOverride() {
[email protected]bf70edce2012-06-20 22:32:221278 return renderer_preferences_.user_agent_override;
[email protected]86ef6a392012-05-11 22:03:111279}
1280
Changwan Ryuc1134a82018-03-07 02:10:141281bool WebContentsImpl::ShouldOverrideUserAgentInNewTabs() {
1282 return should_override_user_agent_in_new_tabs_;
1283}
1284
dmazzonidd3d51a72016-12-14 18:41:011285void WebContentsImpl::EnableWebContentsOnlyAccessibilityMode() {
Dominic Mazzonid9fda172019-03-07 17:12:071286 // If accessibility is already enabled, we'll need to force a reset
1287 // in order to ensure new observers of accessibility events get the
1288 // full accessibility tree from scratch.
1289 bool need_reset = GetAccessibilityMode().has_mode(ui::AXMode::kWebContents);
1290
1291 ui::AXMode desired_mode =
1292 GetContentClient()->browser()->GetAXModeForBrowserContext(
1293 GetBrowserContext());
1294 desired_mode |= ui::kAXModeWebContentsOnly;
1295 AddAccessibilityMode(desired_mode);
1296
1297 if (need_reset) {
dchengafb53e22016-02-04 08:11:081298 for (RenderFrameHost* rfh : GetAllFrames())
1299 ResetAccessibility(rfh);
dchengafb53e22016-02-04 08:11:081300 }
[email protected]95640212014-07-26 18:14:301301}
1302
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411303bool WebContentsImpl::IsWebContentsOnlyAccessibilityModeForTesting() {
Doug Turner63f3c7b2017-07-29 05:10:011304 return accessibility_mode_ == ui::kAXModeWebContentsOnly;
[email protected]95640212014-07-26 18:14:301305}
1306
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411307bool WebContentsImpl::IsFullAccessibilityModeForTesting() {
Doug Turner63f3c7b2017-07-29 05:10:011308 return accessibility_mode_ == ui::kAXModeComplete;
[email protected]95640212014-07-26 18:14:301309}
1310
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411311const PageImportanceSignals& WebContentsImpl::GetPageImportanceSignals() {
kouhei40f03cb2015-09-24 07:47:011312 return page_importance_signals_;
1313}
1314
Becca Hughes6daf5662018-06-27 16:50:541315#if defined(OS_ANDROID)
1316
1317void WebContentsImpl::SetDisplayCutoutSafeArea(gfx::Insets insets) {
Becca Hughes86334622018-07-09 21:29:381318 if (display_cutout_host_impl_)
1319 display_cutout_host_impl_->SetDisplayCutoutSafeArea(insets);
Becca Hughes6daf5662018-06-27 16:50:541320}
1321
1322#endif
1323
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411324const base::string16& WebContentsImpl::GetTitle() {
[email protected]4c6092c5b2009-06-06 00:23:551325 // Transient entries take precedence. They are used for interstitial pages
1326 // that are shown on top of existing pages.
[email protected]10f417c52011-12-28 21:04:231327 NavigationEntry* entry = controller_.GetTransientEntry();
[email protected]45d0ef7f2011-01-05 13:46:231328 if (entry) {
jshin1fb76462016-04-05 22:13:031329 return entry->GetTitleForDisplay();
[email protected]45d0ef7f2011-01-05 13:46:231330 }
carlosk35f35af2015-12-01 10:55:401331
Nasko Oskov2f7d2112019-10-29 03:26:571332 WebUI* our_web_ui =
1333 GetRenderManager()->speculative_frame_host()
1334 ? GetRenderManager()->speculative_frame_host()->web_ui()
1335 : GetRenderManager()->current_frame_host()->web_ui();
[email protected]7ade2732011-02-10 00:13:581336 if (our_web_ui) {
[email protected]96d185d2009-04-24 03:28:541337 // Don't override the title in view source mode.
[email protected]59167c22013-06-03 18:07:321338 entry = controller_.GetVisibleEntry();
[email protected]96d185d2009-04-24 03:28:541339 if (!(entry && entry->IsViewSourceMode())) {
[email protected]e0112912011-02-02 22:54:351340 // Give the Web UI the chance to override our title.
[email protected]fcf75d42013-12-03 20:11:261341 const base::string16& title = our_web_ui->GetOverriddenTitle();
[email protected]96d185d2009-04-24 03:28:541342 if (!title.empty())
1343 return title;
1344 }
1345 }
1346
1347 // We use the title for the last committed entry rather than a pending
1348 // navigation entry. For example, when the user types in a URL, we want to
1349 // keep the old page's title until the new load has committed and we get a new
1350 // title.
[email protected]96d185d2009-04-24 03:28:541351 entry = controller_.GetLastCommittedEntry();
[email protected]59167c22013-06-03 18:07:321352
creis072b61f2015-09-28 20:52:241353 // We make an exception for initial navigations. We only want to use the title
1354 // from the visible entry if:
1355 // 1. The pending entry has been explicitly assigned a title to display.
1356 // 2. The user is doing a history navigation in a new tab (e.g., Ctrl+Back),
1357 // which case there is a pending entry index other than -1.
1358 //
1359 // Otherwise, we want to stick with the last committed entry's title during
1360 // new navigations, which have pending entries at index -1 with no title.
1361 if (controller_.IsInitialNavigation() &&
1362 ((controller_.GetVisibleEntry() &&
1363 !controller_.GetVisibleEntry()->GetTitle().empty()) ||
1364 controller_.GetPendingEntryIndex() != -1)) {
1365 entry = controller_.GetVisibleEntry();
[email protected]9eeafc012013-11-25 23:49:471366 }
[email protected]59167c22013-06-03 18:07:321367
[email protected]45d0ef7f2011-01-05 13:46:231368 if (entry) {
jshin1fb76462016-04-05 22:13:031369 return entry->GetTitleForDisplay();
[email protected]45d0ef7f2011-01-05 13:46:231370 }
[email protected]987fc3a2011-05-26 14:18:091371
1372 // |page_title_when_no_navigation_entry_| is finally used
1373 // if no title cannot be retrieved.
1374 return page_title_when_no_navigation_entry_;
[email protected]96d185d2009-04-24 03:28:541375}
1376
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411377SiteInstanceImpl* WebContentsImpl::GetSiteInstance() {
[email protected]fa944cb82013-11-15 17:51:211378 return GetRenderManager()->current_host()->GetSiteInstance();
[email protected]96d185d2009-04-24 03:28:541379}
1380
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411381bool WebContentsImpl::IsLoading() {
clamy44e84ce2016-02-22 15:38:251382 return frame_tree_.IsLoading() &&
creisadca9862017-06-16 19:12:111383 !(ShowingInterstitialPage() && interstitial_page_->pause_throbber());
[email protected]3c9e1872010-11-18 16:17:491384}
1385
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411386double WebContentsImpl::GetLoadProgress() {
Peter Boström18a40fa2018-10-31 19:03:501387 return frame_tree_.load_progress();
1388}
1389
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411390bool WebContentsImpl::IsLoadingToDifferentDocument() {
clamy44e84ce2016-02-22 15:38:251391 return IsLoading() && is_load_to_different_document_;
[email protected]6dfed692014-05-22 04:18:031392}
1393
Alexander Timindd0fa0c52019-11-06 13:03:371394bool WebContentsImpl::IsDocumentOnLoadCompletedInMainFrame() {
1395 return GetRenderViewHost()->IsDocumentOnLoadCompletedInMainFrame();
1396}
1397
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411398bool WebContentsImpl::IsWaitingForResponse() {
Peter Boströma9a27f6e2018-11-21 13:12:191399 NavigationRequest* ongoing_navigation_request =
1400 frame_tree_.root()->navigation_request();
1401
1402 // An ongoing navigation request means we're waiting for a response.
1403 return ongoing_navigation_request != nullptr;
[email protected]be1f56ab2011-12-22 06:55:311404}
1405
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411406const net::LoadStateWithParam& WebContentsImpl::GetLoadState() {
[email protected]be1f56ab2011-12-22 06:55:311407 return load_state_;
1408}
1409
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411410const base::string16& WebContentsImpl::GetLoadStateHost() {
[email protected]be1f56ab2011-12-22 06:55:311411 return load_state_host_;
1412}
1413
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411414uint64_t WebContentsImpl::GetUploadSize() {
[email protected]be1f56ab2011-12-22 06:55:311415 return upload_size_;
1416}
1417
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411418uint64_t WebContentsImpl::GetUploadPosition() {
[email protected]be1f56ab2011-12-22 06:55:311419 return upload_position_;
1420}
1421
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411422const std::string& WebContentsImpl::GetEncoding() {
Alexander Timine5f20e22019-10-09 23:22:351423 return GetMainFrame()->GetEncoding();
[email protected]be1f56ab2011-12-22 06:55:311424}
1425
Charles Zhaod72f99d2018-09-17 14:18:271426bool WebContentsImpl::WasDiscarded() {
1427 return GetFrameTree()->root()->was_discarded();
1428}
1429
Shubhie Panickerddf2a4e2018-03-06 00:09:061430void WebContentsImpl::SetWasDiscarded(bool was_discarded) {
1431 GetFrameTree()->root()->set_was_discarded();
1432}
1433
Collin Bakerc5259962019-11-14 17:51:581434void WebContentsImpl::IncrementCapturerCount(const gfx::Size& capture_size,
1435 bool stay_hidden) {
[email protected]5a652232013-02-12 06:15:251436 DCHECK(!is_being_destroyed_);
Collin Bakerc5259962019-11-14 17:51:581437 if (stay_hidden)
1438 ++hidden_capturer_count_;
1439 else
1440 ++visible_capturer_count_;
[email protected]222f5822014-02-05 23:40:491441
1442 // Note: This provides a hint to upstream code to size the views optimally
1443 // for quality (e.g., to avoid scaling).
1444 if (!capture_size.IsEmpty() && preferred_size_for_capture_.IsEmpty()) {
1445 preferred_size_for_capture_ = capture_size;
1446 OnPreferredSizeChanged(preferred_size_);
1447 }
ccameron8807c38f2015-01-17 00:29:191448
Collin Bakerc5259962019-11-14 17:51:581449 UpdateVisibilityAndNotifyPageAndView(GetVisibility());
[email protected]54597982013-02-06 01:59:551450}
1451
Collin Bakerc5259962019-11-14 17:51:581452void WebContentsImpl::DecrementCapturerCount(bool stay_hidden) {
1453 if (stay_hidden)
1454 --hidden_capturer_count_;
1455 else
1456 --visible_capturer_count_;
1457 DCHECK_GE(hidden_capturer_count_, 0);
1458 DCHECK_GE(visible_capturer_count_, 0);
[email protected]54597982013-02-06 01:59:551459
[email protected]5a652232013-02-12 06:15:251460 if (is_being_destroyed_)
1461 return;
1462
Francois Doray24fc62c2017-12-11 17:27:331463 if (!IsBeingCaptured()) {
[email protected]222f5822014-02-05 23:40:491464 const gfx::Size old_size = preferred_size_for_capture_;
1465 preferred_size_for_capture_ = gfx::Size();
1466 OnPreferredSizeChanged(old_size);
[email protected]54597982013-02-06 01:59:551467 }
Collin Bakerc5259962019-11-14 17:51:581468
1469 UpdateVisibilityAndNotifyPageAndView(GetVisibility());
[email protected]be1f56ab2011-12-22 06:55:311470}
1471
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411472bool WebContentsImpl::IsBeingCaptured() {
Collin Bakerc5259962019-11-14 17:51:581473 return visible_capturer_count_ + hidden_capturer_count_ > 0;
[email protected]f2bd40812013-07-20 04:30:441474}
1475
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411476bool WebContentsImpl::IsAudioMuted() {
Guido Urdanetaf8372942019-11-15 09:47:211477 return audio_stream_factory_ && audio_stream_factory_->IsMuted();
miu50f97892014-09-22 22:49:521478}
1479
1480void WebContentsImpl::SetAudioMuted(bool mute) {
1481 DVLOG(1) << "SetAudioMuted(mute=" << mute << "), was " << IsAudioMuted()
1482 << " for WebContentsImpl@" << this;
1483
1484 if (mute == IsAudioMuted())
1485 return;
1486
Guido Urdanetaf8372942019-11-15 09:47:211487 GetAudioStreamFactory()->SetMuted(mute);
miu50f97892014-09-22 22:49:521488
ericwilligersde386342016-10-19 02:35:091489 for (auto& observer : observers_)
1490 observer.DidUpdateAudioMutingState(mute);
wjmaclean2f797c782016-01-07 14:52:031491
Jan Rucka442c83f2017-08-08 13:27:541492 // Notification for UI updates in response to the changed muting state.
Collin Bakeraf540cf2019-09-20 20:54:161493 NotifyNavigationStateChanged(INVALIDATE_TYPE_AUDIO);
Jan Rucka442c83f2017-08-08 13:27:541494}
1495
1496bool WebContentsImpl::IsCurrentlyAudible() {
Chris Hamiltondf0d72cd2018-05-29 16:23:531497 return is_currently_audible_;
miu50f97892014-09-22 22:49:521498}
1499
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411500bool WebContentsImpl::IsConnectedToBluetoothDevice() {
ortunodf4d7982016-04-08 02:33:351501 return bluetooth_connected_device_count_ > 0;
ortuno32e7db3c2016-03-29 16:14:201502}
1503
Lucas Furukawa Gadani4b4eed02019-06-04 23:12:041504bool WebContentsImpl::IsConnectedToSerialPort() {
Reilly Grant5e7c79b22019-04-09 17:26:201505 return serial_active_frame_count_ > 0;
1506}
1507
Matt Reynoldse8c6c1f2019-11-02 09:53:531508bool WebContentsImpl::IsConnectedToHidDevice() {
1509 return hid_active_frame_count_ > 0;
1510}
1511
Marijn Kruisselbrink29051042019-08-06 22:56:551512bool WebContentsImpl::HasNativeFileSystemHandles() {
1513 return native_file_system_handle_count_ > 0;
1514}
Marijn Kruisselbrinkf1714aa22019-07-02 03:45:331515bool WebContentsImpl::HasNativeFileSystemDirectoryHandles() {
Marijn Kruisselbrink46fa4bd2019-07-11 18:15:471516 return !native_file_system_directory_handles_.empty();
1517}
1518
1519std::vector<base::FilePath>
1520WebContentsImpl::GetNativeFileSystemDirectoryHandles() {
1521 std::vector<base::FilePath> result;
1522 result.reserve(native_file_system_directory_handles_.size());
1523 for (auto const& entry : native_file_system_directory_handles_)
1524 result.push_back(entry.first);
1525 return result;
Marijn Kruisselbrinkf1714aa22019-07-02 03:45:331526}
1527
1528bool WebContentsImpl::HasWritableNativeFileSystemHandles() {
1529 return native_file_system_writable_handle_count_ > 0;
1530}
1531
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411532bool WebContentsImpl::HasPictureInPictureVideo() {
sawtellea7333a82018-05-31 02:36:361533 return has_picture_in_picture_video_;
1534}
1535
1536void WebContentsImpl::SetHasPictureInPictureVideo(
1537 bool has_picture_in_picture_video) {
1538 // If status of |this| is already accurate, there is no need to update.
1539 if (has_picture_in_picture_video == has_picture_in_picture_video_)
1540 return;
1541 has_picture_in_picture_video_ = has_picture_in_picture_video;
1542 NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB);
François Beaufort058085d2018-08-30 15:29:141543 for (auto& observer : observers_)
1544 observer.MediaPictureInPictureChanged(has_picture_in_picture_video_);
sawtellea7333a82018-05-31 02:36:361545}
1546
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411547bool WebContentsImpl::IsCrashed() {
Dominic Mazzoni73a48322018-05-25 18:14:071548 switch (crashed_status_) {
1549 case base::TERMINATION_STATUS_PROCESS_CRASHED:
1550 case base::TERMINATION_STATUS_ABNORMAL_TERMINATION:
1551 case base::TERMINATION_STATUS_PROCESS_WAS_KILLED:
1552 case base::TERMINATION_STATUS_OOM:
1553 case base::TERMINATION_STATUS_LAUNCH_FAILED:
oshima620225722015-06-04 19:45:271554#if defined(OS_CHROMEOS)
Dominic Mazzoni73a48322018-05-25 18:14:071555 case base::TERMINATION_STATUS_PROCESS_WAS_KILLED_BY_OOM:
oshima620225722015-06-04 19:45:271556#endif
Dominic Mazzoni73a48322018-05-25 18:14:071557#if defined(OS_ANDROID)
1558 case base::TERMINATION_STATUS_OOM_PROTECTED:
1559#endif
Will Harris07925d12019-10-31 03:03:051560#if defined(OS_WIN)
1561 case base::TERMINATION_STATUS_INTEGRITY_FAILURE:
1562#endif
Dominic Mazzoni73a48322018-05-25 18:14:071563 return true;
1564 case base::TERMINATION_STATUS_NORMAL_TERMINATION:
1565 case base::TERMINATION_STATUS_STILL_RUNNING:
1566 return false;
1567 case base::TERMINATION_STATUS_MAX_ENUM:
1568 NOTREACHED();
1569 return false;
1570 }
1571
1572 NOTREACHED();
1573 return false;
[email protected]3c9e1872010-11-18 16:17:491574}
1575
[email protected]b172aee2012-04-10 17:05:261576void WebContentsImpl::SetIsCrashed(base::TerminationStatus status,
1577 int error_code) {
[email protected]443b80e2010-12-14 00:42:231578 if (status == crashed_status_)
[email protected]d5f942ba2008-09-26 19:30:341579 return;
1580
[email protected]443b80e2010-12-14 00:42:231581 crashed_status_ = status;
1582 crashed_error_code_ = error_code;
[email protected]8ff00d72012-10-23 19:12:211583 NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB);
[email protected]d5f942ba2008-09-26 19:30:341584}
1585
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411586base::TerminationStatus WebContentsImpl::GetCrashedStatus() {
[email protected]be1f56ab2011-12-22 06:55:311587 return crashed_status_;
1588}
1589
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411590int WebContentsImpl::GetCrashedErrorCode() {
afakhry98241832016-03-11 19:27:471591 return crashed_error_code_;
1592}
1593
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411594bool WebContentsImpl::IsBeingDestroyed() {
[email protected]be1f56ab2011-12-22 06:55:311595 return is_being_destroyed_;
1596}
1597
[email protected]7f924832014-08-09 05:57:221598void WebContentsImpl::NotifyNavigationStateChanged(
1599 InvalidateTypes changed_flags) {
dalecurtis88c240072015-12-09 02:11:181600 // Notify the media observer of potential audibility changes.
Collin Bakeraf540cf2019-09-20 20:54:161601 if (changed_flags & INVALIDATE_TYPE_AUDIO) {
mlamouri44e4ef42016-01-20 18:42:271602 media_web_contents_observer_->MaybeUpdateAudibleState();
dalecurtisbc6572e12014-09-12 19:22:301603 }
1604
[email protected]d5f942ba2008-09-26 19:30:341605 if (delegate_)
1606 delegate_->NavigationStateChanged(this, changed_flags);
wjmaclean1dabf3e32016-05-13 23:34:441607
1608 if (GetOuterWebContents())
1609 GetOuterWebContents()->NotifyNavigationStateChanged(changed_flags);
[email protected]d5f942ba2008-09-26 19:30:341610}
1611
W. James MacLean6f34b042019-07-12 19:25:301612RenderFrameHostImpl* WebContentsImpl::GetFocusedFrameFromFocusedDelegate() {
1613 FrameTreeNode* focused_node =
1614 GetFocusedWebContents()->frame_tree_.GetFocusedFrame();
1615 return focused_node ? focused_node->current_frame_host() : nullptr;
1616}
1617
David Black9cca3592019-11-06 23:02:221618void WebContentsImpl::OnVerticalScrollDirectionChanged(
1619 viz::VerticalScrollDirection scroll_direction) {
1620 for (auto& observer : observers_)
1621 observer.DidChangeVerticalScrollDirection(scroll_direction);
1622}
1623
Chris Hamiltondf0d72cd2018-05-29 16:23:531624void WebContentsImpl::OnAudioStateChanged() {
1625 // This notification can come from any embedded contents or from this
1626 // WebContents' stream monitor. Aggregate these signals to get the actual
1627 // state.
1628 bool is_currently_audible =
1629 audio_stream_monitor_.IsCurrentlyAudible() ||
1630 (browser_plugin_embedder_ &&
1631 browser_plugin_embedder_->AreAnyGuestsCurrentlyAudible());
1632 if (is_currently_audible == is_currently_audible_)
1633 return;
1634
1635 // Update internal state.
1636 is_currently_audible_ = is_currently_audible;
1637 was_ever_audible_ = was_ever_audible_ || is_currently_audible_;
1638
1639 SendPageMessage(
1640 new PageMsg_AudioStateChanged(MSG_ROUTING_NONE, is_currently_audible_));
altimind8bd26c2016-11-04 11:44:541641
altiminec87fd1f2016-11-17 20:22:491642 // Notification for UI updates in response to the changed audio state.
Collin Bakeraf540cf2019-09-20 20:54:161643 NotifyNavigationStateChanged(INVALIDATE_TYPE_AUDIO);
Jan Rucka442c83f2017-08-08 13:27:541644
Chris Hamiltondf0d72cd2018-05-29 16:23:531645 // Ensure that audio state changes propagate from innermost to outermost
1646 // WebContents.
1647 if (GetOuterWebContents())
1648 GetOuterWebContents()->OnAudioStateChanged();
Tommy Steimel18360512017-11-01 00:38:191649
Chris Hamiltoneb41198f2018-05-24 18:41:261650 for (auto& observer : observers_)
Chris Hamiltondf0d72cd2018-05-29 16:23:531651 observer.OnAudioStateChanged(is_currently_audible_);
altimind8bd26c2016-11-04 11:44:541652}
1653
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411654base::TimeTicks WebContentsImpl::GetLastActiveTime() {
[email protected]9a890452014-02-13 22:21:021655 return last_active_time_;
[email protected]3e324142012-06-25 18:26:331656}
1657
[email protected]9e2e4632012-07-27 16:38:411658void WebContentsImpl::WasShown() {
Collin Baker989e0882019-11-01 01:27:171659 UpdateVisibilityAndNotifyPageAndView(Visibility::VISIBLE);
[email protected]be1f56ab2011-12-22 06:55:311660}
1661
[email protected]b172aee2012-04-10 17:05:261662void WebContentsImpl::WasHidden() {
Collin Baker989e0882019-11-01 01:27:171663 UpdateVisibilityAndNotifyPageAndView(Visibility::HIDDEN);
[email protected]375fa1b2012-05-22 22:05:371664}
1665
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411666bool WebContentsImpl::HasRecentInteractiveInputEvent() {
Daniel Cheng90196c82018-04-25 21:49:141667 static constexpr base::TimeDelta kMaxInterval =
1668 base::TimeDelta::FromSeconds(5);
1669 base::TimeDelta delta =
1670 ui::EventTimeForNow() - last_interactive_input_event_time_;
1671 // Note: the expectation is that the caller is typically expecting an input
1672 // event, e.g. validating that a WebUI message that requires a gesture is
Daniel Chenge81030b32019-07-17 20:20:231673 // actually attached to a gesture.
Daniel Cheng90196c82018-04-25 21:49:141674 return delta <= kMaxInterval;
1675}
1676
Avi Drissman738ea192018-08-29 20:24:161677void WebContentsImpl::SetIgnoreInputEvents(bool ignore_input_events) {
1678 ignore_input_events_ = ignore_input_events;
1679}
1680
Bo Liu168c8642017-08-28 18:26:021681#if defined(OS_ANDROID)
Bo Liu16dd7b8322018-05-02 21:19:501682void WebContentsImpl::SetMainFrameImportance(
1683 ChildProcessImportance importance) {
1684 GetMainFrame()->GetRenderWidgetHost()->SetImportance(importance);
Bo Liu7c6779e92017-08-16 02:02:281685 if (ShowingInterstitialPage()) {
Bo Liu16dd7b8322018-05-02 21:19:501686 static_cast<RenderFrameHostImpl*>(interstitial_page_->GetMainFrame())
1687 ->GetRenderWidgetHost()
1688 ->SetImportance(importance);
Bo Liu7c6779e92017-08-16 02:02:281689 }
Bo Liu7c6779e92017-08-16 02:02:281690}
Bo Liu168c8642017-08-28 18:26:021691#endif
Bo Liu7c6779e92017-08-16 02:02:281692
ccameron8807c38f2015-01-17 00:29:191693void WebContentsImpl::WasOccluded() {
Collin Baker989e0882019-11-01 01:27:171694 UpdateVisibilityAndNotifyPageAndView(Visibility::OCCLUDED);
ccameron8807c38f2015-01-17 00:29:191695}
1696
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411697Visibility WebContentsImpl::GetVisibility() {
Francois Doraye6161152018-03-27 22:05:371698 return visibility_;
Francois Dorayfe4a1772018-02-17 04:17:091699}
1700
Alex Moshchuk5bcd7c12019-11-12 22:55:311701bool WebContentsImpl::NeedToFireBeforeUnloadOrUnload() {
[email protected]be1f56ab2011-12-22 06:55:311702 // TODO(creis): Should we fire even for interstitial pages?
Alex Moshchuk6fcaca752018-07-14 02:13:591703 if (ShowingInterstitialPage())
1704 return false;
1705
1706 if (!WillNotifyDisconnection())
1707 return false;
1708
1709 // Don't fire if the main frame's RenderViewHost indicates that beforeunload
1710 // and unload have already executed (e.g., after receiving a ClosePage ACK)
1711 // or should be ignored.
1712 if (GetRenderViewHost()->SuddenTerminationAllowed())
1713 return false;
1714
Alex Moshchukc78dbc722019-10-31 20:00:501715 // Check whether any frame in the frame tree needs to run beforeunload or
1716 // unload handlers.
1717 for (FrameTreeNode* node : frame_tree_.Nodes()) {
1718 RenderFrameHostImpl* rfh = node->current_frame_host();
1719
1720 // No need to run beforeunload/unload if the RenderFrame isn't live.
1721 if (!rfh->IsRenderFrameLive())
1722 continue;
1723
Dave Tapuska97d22ab2019-12-03 17:56:361724 if (rfh->GetSuddenTerminationDisablerState(
1725 blink::mojom::SuddenTerminationDisablerType::
1726 kBeforeUnloadHandler) ||
1727 rfh->GetSuddenTerminationDisablerState(
1728 blink::mojom::SuddenTerminationDisablerType::kUnloadHandler)) {
Alex Moshchukc78dbc722019-10-31 20:00:501729 return true;
1730 }
Aditya Keerthi34f37e62019-03-08 15:54:531731 }
Alex Moshchuk6fcaca752018-07-14 02:13:591732
Alex Moshchukc78dbc722019-10-31 20:00:501733 return false;
[email protected]be1f56ab2011-12-22 06:55:311734}
1735
Chris Hamilton3b3e315d2018-09-19 13:16:211736void WebContentsImpl::DispatchBeforeUnload(bool auto_cancel) {
1737 auto before_unload_type =
1738 auto_cancel ? RenderFrameHostImpl::BeforeUnloadType::DISCARD
1739 : RenderFrameHostImpl::BeforeUnloadType::TAB_CLOSE;
1740 GetMainFrame()->DispatchBeforeUnload(before_unload_type, false);
[email protected]1c3f80bd2014-04-08 23:02:061741}
1742
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:501743void WebContentsImpl::AttachInnerWebContents(
1744 std::unique_ptr<WebContents> inner_web_contents,
W. James MacLean62883982019-11-12 20:39:001745 RenderFrameHost* render_frame_host,
1746 bool is_full_page) {
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:501747 WebContentsImpl* inner_web_contents_impl =
1748 static_cast<WebContentsImpl*>(inner_web_contents.get());
1749 DCHECK(!inner_web_contents_impl->node_.outer_web_contents());
1750 auto* render_frame_host_impl =
1751 static_cast<RenderFrameHostImpl*>(render_frame_host);
1752 DCHECK_EQ(&frame_tree_,
1753 render_frame_host_impl->frame_tree_node()->frame_tree());
Lucas Furukawa Gadani105de1e812018-05-31 19:38:391754
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:501755 RenderFrameHostManager* inner_render_manager =
1756 inner_web_contents_impl->GetRenderManager();
1757 RenderFrameHostImpl* inner_main_frame =
1758 inner_render_manager->current_frame_host();
1759 RenderViewHostImpl* inner_render_view_host =
1760 inner_render_manager->current_host();
1761 auto* outer_render_manager =
1762 render_frame_host_impl->frame_tree_node()->render_manager();
lfg91a79e12016-04-01 23:52:191763
Lucas Furukawa Gadani105de1e812018-05-31 19:38:391764 // When attaching a WebContents as an inner WebContents, we need to replace
1765 // the Webcontents' view with a WebContentsViewChildFrame.
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:501766 inner_web_contents_impl->view_.reset(new WebContentsViewChildFrame(
1767 inner_web_contents_impl,
1768 GetContentClient()->browser()->GetWebContentsViewDelegate(
1769 inner_web_contents_impl),
1770 &inner_web_contents_impl->render_view_host_delegate_view_));
Lucas Furukawa Gadani105de1e812018-05-31 19:38:391771
lfg91a79e12016-04-01 23:52:191772 // When the WebContents being initialized has an opener, the browser side
1773 // Render{View,Frame}Host must be initialized and the RenderWidgetHostView
1774 // created. This is needed because the usual initialization happens during
1775 // the first navigation, but when attaching a new window we don't navigate
1776 // before attaching. If the browser side is already initialized, the calls
1777 // below will just early return.
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:501778 inner_render_manager->InitRenderView(inner_render_view_host, nullptr);
1779 inner_main_frame->Init();
1780 if (!inner_render_manager->GetRenderWidgetHostView()) {
1781 inner_web_contents_impl->CreateRenderWidgetHostViewForRenderManager(
1782 inner_render_view_host);
1783 }
lfg91a79e12016-04-01 23:52:191784
lazyboy6ec48b2a2015-06-29 15:18:141785 // Create a link to our outer WebContents.
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:501786 node_.AttachInnerWebContents(std::move(inner_web_contents),
1787 render_frame_host_impl);
lazyboy6ec48b2a2015-06-29 15:18:141788
lazyboy6ec48b2a2015-06-29 15:18:141789 // Create a proxy in top-level RenderFrameHostManager, pointing to the
1790 // SiteInstance of the outer WebContents. The proxy will be used to send
1791 // postMessage to the inner WebContents.
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:501792 auto* proxy = inner_render_manager->CreateOuterDelegateProxy(
1793 render_frame_host_impl->GetSiteInstance());
lazyboy6ec48b2a2015-06-29 15:18:141794
Lucas Furukawa Gadani99125822019-01-03 15:41:491795 // When attaching a GuestView as an inner WebContents, there should already be
1796 // a live RenderFrame, which has to be swapped. When attaching a portal, there
1797 // will not be a live RenderFrame before creating the proxy.
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:501798 if (render_frame_host_impl->IsRenderFrameLive()) {
1799 inner_render_manager->SwapOuterDelegateFrame(render_frame_host_impl, proxy);
Lucas Furukawa Gadani99125822019-01-03 15:41:491800
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:501801 inner_web_contents_impl->ReattachToOuterWebContentsFrame();
Lucas Furukawa Gadani99125822019-01-03 15:41:491802 }
ekaramadadd882292016-06-08 15:22:561803
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:501804 if (frame_tree_.GetFocusedFrame() ==
1805 render_frame_host_impl->frame_tree_node()) {
1806 inner_web_contents_impl->SetFocusedFrame(
1807 inner_web_contents_impl->frame_tree_.root(),
1808 render_frame_host_impl->GetSiteInstance());
avallee7529b2a2017-04-26 11:37:511809 }
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:501810 outer_render_manager->set_attach_complete();
W. James MacLean62883982019-11-12 20:39:001811
1812 // If the inner WebContents is full frame, give it focus.
1813 if (is_full_page) {
1814 // There should only ever be one inner WebContents when |is_full_page| is
1815 // true, and it is the one we just attached.
1816 DCHECK_EQ(1u, node_.GetInnerWebContents().size());
1817 inner_web_contents_impl->SetAsFocusedWebContentsIfNecessary();
1818 }
lazyboy6ec48b2a2015-06-29 15:18:141819}
1820
Lucas Furukawa Gadani99125822019-01-03 15:41:491821std::unique_ptr<WebContents> WebContentsImpl::DetachFromOuterWebContents() {
1822 DCHECK(node_.outer_web_contents());
1823 if (RenderWidgetHostViewBase* view =
1824 static_cast<RenderWidgetHostViewBase*>(GetMainFrame()->GetView())) {
1825 view->Destroy();
1826 }
1827 GetRenderManager()->DeleteOuterDelegateProxy(
1828 node_.OuterContentsFrameTreeNode()
1829 ->current_frame_host()
1830 ->GetSiteInstance());
1831 view_.reset(CreateWebContentsView(
1832 this, GetContentClient()->browser()->GetWebContentsViewDelegate(this),
1833 &render_view_host_delegate_view_));
danakjfc5184932019-09-12 18:08:321834 view_->CreateView(nullptr);
Lucas Furukawa Gadani99125822019-01-03 15:41:491835 std::unique_ptr<WebContents> web_contents =
1836 node_.DisconnectFromOuterWebContents();
1837 DCHECK_EQ(web_contents.get(), this);
1838 node_.SetFocusedWebContents(this);
1839 CreateRenderWidgetHostViewForRenderManager(GetRenderViewHost());
1840 return web_contents;
1841}
1842
W. James MacLean2dc75ed42019-03-06 14:31:091843void WebContentsImpl::RecursivelyRegisterFrameSinkIds() {
1844 auto* view = static_cast<RenderWidgetHostViewBase*>(
1845 GetRenderManager()->GetRenderWidgetHostView());
1846 DCHECK(view);
1847 if (!view->IsRenderWidgetHostViewChildFrame())
1848 return;
1849 static_cast<RenderWidgetHostViewChildFrame*>(view)->RegisterFrameSinkId();
1850
1851 for (auto* inner_web_contents : node_.GetInnerWebContents()) {
1852 static_cast<WebContentsImpl*>(inner_web_contents)
1853 ->RecursivelyRegisterFrameSinkIds();
1854 }
1855}
1856
Lucas Furukawa Gadaniaed1fed2017-10-13 17:34:141857void WebContentsImpl::ReattachToOuterWebContentsFrame() {
1858 DCHECK(node_.outer_web_contents());
1859 auto* render_manager = GetRenderManager();
1860 auto* parent_frame =
1861 node_.OuterContentsFrameTreeNode()->current_frame_host()->GetParent();
1862 render_manager->SetRWHViewForInnerContents(
1863 render_manager->GetRenderWidgetHostView());
1864
W. James MacLean2dc75ed42019-03-06 14:31:091865 RecursivelyRegisterFrameSinkIds();
Lucas Furukawa Gadaniaed1fed2017-10-13 17:34:141866
1867 // Set up the the guest's AX tree to point back at the embedder's AX tree.
1868 GetMainFrame()->set_browser_plugin_embedder_ax_tree_id(
1869 parent_frame->GetAXTreeID());
1870 GetMainFrame()->UpdateAXTreeData();
1871}
1872
dalecurtis6c58ed02016-10-28 23:02:371873void WebContentsImpl::DidChangeVisibleSecurityState() {
Emily Starkc663b0242019-04-26 19:34:451874 if (delegate_)
dalecurtis6c58ed02016-10-28 23:02:371875 delegate_->VisibleSecurityStateChanged(this);
Emily Starkc663b0242019-04-26 19:34:451876 for (auto& observer : observers_)
1877 observer.DidChangeVisibleSecurityState();
dalecurtis6c58ed02016-10-28 23:02:371878}
1879
Ahmed Fakhryd70bf37b2018-04-04 17:07:241880void WebContentsImpl::NotifyPreferencesChanged() {
1881 std::set<RenderViewHost*> render_view_host_set;
1882 for (FrameTreeNode* node : frame_tree_.Nodes()) {
Giovanni Ortuño Urquidi17a5d7c2018-04-30 06:48:531883 render_view_host_set.insert(
1884 node->current_frame_host()->GetRenderViewHost());
Ahmed Fakhryd70bf37b2018-04-04 17:07:241885 }
Giovanni Ortuño Urquidi17a5d7c2018-04-30 06:48:531886
Ahmed Fakhryd70bf37b2018-04-04 17:07:241887 for (RenderViewHost* render_view_host : render_view_host_set)
1888 render_view_host->OnWebkitPreferencesChanged();
1889}
1890
Bruce Long1e3e1f542019-10-16 17:56:281891void WebContentsImpl::SyncRendererPrefs() {
1892 blink::mojom::RendererPreferences renderer_preferences =
1893 GetRendererPrefs(GetBrowserContext());
1894 RenderViewHostImpl::GetPlatformSpecificPrefs(&renderer_preferences);
1895 SendPageMessage(
1896 new PageMsg_SetRendererPrefs(MSG_ROUTING_NONE, renderer_preferences));
1897}
1898
Josh Karlindaba39322019-07-17 23:24:331899void WebContentsImpl::OnCookiesRead(const GURL& url,
1900 const GURL& first_party_url,
1901 const net::CookieList& cookie_list,
1902 bool blocked_by_policy) {
1903 for (auto& observer : observers_) {
1904 observer.OnCookiesRead(url, first_party_url, cookie_list,
1905 blocked_by_policy);
1906 }
1907}
1908
1909void WebContentsImpl::OnCookieChange(const GURL& url,
1910 const GURL& first_party_url,
1911 const net::CanonicalCookie& cookie,
1912 bool blocked_by_policy) {
1913 for (auto& observer : observers_) {
1914 observer.OnCookieChange(url, first_party_url, cookie, blocked_by_policy);
1915 }
1916}
1917
[email protected]b172aee2012-04-10 17:05:261918void WebContentsImpl::Stop() {
dcheng57e39e22016-01-21 00:25:381919 for (FrameTreeNode* node : frame_tree_.Nodes())
1920 node->StopLoading();
ericwilligersde386342016-10-19 02:35:091921 for (auto& observer : observers_)
1922 observer.NavigationStopped();
[email protected]0bfbf882011-12-22 18:19:271923}
1924
Francois Doray47f759d2018-06-11 18:13:511925void WebContentsImpl::SetPageFrozen(bool frozen) {
1926 // A visible page is never frozen.
1927 DCHECK_NE(Visibility::VISIBLE, GetVisibility());
1928
1929 SendPageMessage(new PageMsg_SetPageFrozen(MSG_ROUTING_NONE, frozen));
Fadi Meawada6573e02018-03-10 00:52:111930}
1931
erikchen6c7df7f7a2018-05-03 18:13:591932std::unique_ptr<WebContents> WebContentsImpl::Clone() {
[email protected]14392a52012-05-02 20:28:441933 // We use our current SiteInstance since the cloned entry will use it anyway.
alexmose201c7cd2015-06-10 17:14:211934 // We pass our own opener so that the cloned page can access it if it was set
[email protected]ed245db2012-12-18 08:00:451935 // before.
[email protected]54944cde2012-12-09 09:24:591936 CreateParams create_params(GetBrowserContext(), GetSiteInstance());
Ian Clelland5cbaaf82017-11-27 22:00:031937 FrameTreeNode* opener = frame_tree_.root()->opener();
1938 RenderFrameHostImpl* opener_rfh = nullptr;
1939 if (opener)
1940 opener_rfh = opener->current_frame_host();
erikchen6c7df7f7a2018-05-03 18:13:591941 std::unique_ptr<WebContentsImpl> tc =
erikchen50735fd2018-05-05 15:08:331942 CreateWithOpener(create_params, opener_rfh);
Lucas Furukawa Gadani5553a152019-01-08 18:55:571943 tc->GetController().CopyStateFrom(&controller_, true);
ericwilligersde386342016-10-19 02:35:091944 for (auto& observer : observers_)
erikchen6c7df7f7a2018-05-03 18:13:591945 observer.DidCloneToNewWebContents(this, tc.get());
[email protected]0bfbf882011-12-22 18:19:271946 return tc;
1947}
1948
[email protected]14392a52012-05-02 20:28:441949void WebContentsImpl::Observe(int type,
[email protected]8ff00d72012-10-23 19:12:211950 const NotificationSource& source,
1951 const NotificationDetails& details) {
[email protected]14392a52012-05-02 20:28:441952 switch (type) {
[email protected]8ff00d72012-10-23 19:12:211953 case NOTIFICATION_RENDER_WIDGET_HOST_DESTROYED: {
1954 RenderWidgetHost* host = Source<RenderWidgetHost>(source).ptr();
[email protected]0b431992014-06-24 00:08:031955 RenderWidgetHostView* view = host->GetView();
1956 if (view == GetFullscreenRenderWidgetHostView()) {
1957 // We cannot just call view_->RestoreFocus() here. On some platforms,
1958 // attempting to focus the currently-invisible WebContentsView will be
1959 // flat-out ignored. Therefore, this boolean is used to track whether
1960 // we will request focus after the fullscreen widget has been
1961 // destroyed.
1962 fullscreen_widget_had_focus_at_shutdown_ = (view && view->HasFocus());
1963 } else {
alexmosc2a8cec2016-05-23 22:19:531964 for (auto i = pending_widget_views_.begin();
[email protected]0b431992014-06-24 00:08:031965 i != pending_widget_views_.end(); ++i) {
1966 if (host->GetView() == i->second) {
1967 pending_widget_views_.erase(i);
1968 break;
1969 }
[email protected]bafe6cd2012-05-23 23:09:501970 }
1971 }
1972 break;
1973 }
[email protected]14392a52012-05-02 20:28:441974 default:
1975 NOTREACHED();
1976 }
1977}
1978
[email protected]ec6c05f2013-10-23 18:41:571979WebContents* WebContentsImpl::GetWebContents() {
1980 return this;
1981}
1982
[email protected]54944cde2012-12-09 09:24:591983void WebContentsImpl::Init(const WebContents::CreateParams& params) {
[email protected]fa944cb82013-11-15 17:51:211984 // This is set before initializing the render manager since
[email protected]b0936d22013-11-28 06:47:361985 // RenderFrameHostManager::Init calls back into us via its delegate to ask if
[email protected]fa944cb82013-11-15 17:51:211986 // it should be hidden.
Francois Doraye6161152018-03-27 22:05:371987 visibility_ =
1988 params.initially_hidden ? Visibility::HIDDEN : Visibility::VISIBLE;
[email protected]d6fa88f2013-10-18 16:00:431989
Sebastien Marchand28cf23182018-06-20 02:39:351990 if (!params.last_active_time.is_null())
1991 last_active_time_ = params.last_active_time;
1992
dcheng3ce04b62015-10-26 23:30:551993 // The routing ids must either all be set or all be unset.
avic2c29d782014-12-12 00:23:541994 DCHECK((params.routing_id == MSG_ROUTING_NONE &&
dcheng3ce04b62015-10-26 23:30:551995 params.main_frame_routing_id == MSG_ROUTING_NONE &&
1996 params.main_frame_widget_routing_id == MSG_ROUTING_NONE) ||
avic2c29d782014-12-12 00:23:541997 (params.routing_id != MSG_ROUTING_NONE &&
dcheng3ce04b62015-10-26 23:30:551998 params.main_frame_routing_id != MSG_ROUTING_NONE &&
1999 params.main_frame_widget_routing_id != MSG_ROUTING_NONE));
2000
2001 scoped_refptr<SiteInstance> site_instance = params.site_instance;
2002 if (!site_instance)
2003 site_instance = SiteInstance::Create(params.browser_context);
Lukasz Anforowicz3caa8372018-06-05 17:22:072004 if (params.desired_renderer_state == CreateParams::kNoRendererProcess) {
2005 static_cast<SiteInstanceImpl*>(site_instance.get())
2006 ->PreventAssociationWithSpareProcess();
2007 }
dcheng3ce04b62015-10-26 23:30:552008
2009 // A main RenderFrameHost always has a RenderWidgetHost, since it is always a
2010 // local root by definition.
2011 // TODO(avi): Once RenderViewHostImpl has-a RenderWidgetHostImpl, it will no
2012 // longer be necessary to eagerly grab a routing ID for the view.
2013 // https://crbug.com/545684
2014 int32_t view_routing_id = params.routing_id;
2015 int32_t main_frame_widget_routing_id = params.main_frame_widget_routing_id;
2016 if (main_frame_widget_routing_id == MSG_ROUTING_NONE) {
Albert J. Wongcb004632018-07-10 22:58:252017 view_routing_id = site_instance->GetProcess()->GetNextRoutingID();
2018 main_frame_widget_routing_id =
dcheng3ce04b62015-10-26 23:30:552019 site_instance->GetProcess()->GetNextRoutingID();
2020 }
2021
Albert J. Wongcb004632018-07-10 22:58:252022 DCHECK_NE(view_routing_id, main_frame_widget_routing_id);
2023
Dave Tapuska485aca92017-08-08 00:47:582024 GetRenderManager()->Init(
2025 site_instance.get(), view_routing_id, params.main_frame_routing_id,
2026 main_frame_widget_routing_id, params.renderer_initiated_creation);
lukasza464d8692016-02-22 19:26:322027
lukasza5140a412016-09-15 21:12:302028 // blink::FrameTree::setName always keeps |unique_name| empty in case of a
2029 // main frame - let's do the same thing here.
2030 std::string unique_name;
lukasza464d8692016-02-22 19:26:322031 frame_tree_.root()->SetFrameName(params.main_frame_name, unique_name);
[email protected]d1198fd2012-08-13 22:50:192032
[email protected]fc2b46b2014-05-03 16:33:452033 WebContentsViewDelegate* delegate =
2034 GetContentClient()->browser()->GetWebContentsViewDelegate(this);
2035
sky52a39e342016-12-17 17:23:222036 if (GuestMode::IsCrossProcessFrameGuest(this)) {
2037 view_.reset(new WebContentsViewChildFrame(
2038 this, delegate, &render_view_host_delegate_view_));
2039 } else {
2040 view_.reset(CreateWebContentsView(this, delegate,
2041 &render_view_host_delegate_view_));
[email protected]d1198fd2012-08-13 22:50:192042 }
[email protected]fc2b46b2014-05-03 16:33:452043 CHECK(render_view_host_delegate_view_);
[email protected]d1198fd2012-08-13 22:50:192044 CHECK(view_.get());
2045
danakjfc5184932019-09-12 18:08:322046 view_->CreateView(params.context);
[email protected]d1198fd2012-08-13 22:50:192047
brettw4b461082016-11-19 18:55:162048#if BUILDFLAG(ENABLE_PLUGINS)
tommyclieb25b2a2014-11-03 19:45:092049 plugin_content_origin_whitelist_.reset(
2050 new PluginContentOriginWhitelist(this));
2051#endif
2052
[email protected]d1198fd2012-08-13 22:50:192053 registrar_.Add(this,
[email protected]8ff00d72012-10-23 19:12:212054 NOTIFICATION_RENDER_WIDGET_HOST_DESTROYED,
2055 NotificationService::AllBrowserContextsAndSources());
[email protected]f28ef9a32014-05-12 16:36:102056
leon.hane4db177a2017-02-07 14:19:162057 screen_orientation_provider_.reset(new ScreenOrientationProvider(this));
[email protected]01f83f92014-06-17 14:41:542058
mlamouriefdca9d2014-09-16 16:55:402059 manifest_manager_host_.reset(new ManifestManagerHost(this));
2060
mfomitchev2b8b066a2016-01-28 19:23:152061#if defined(OS_ANDROID)
Oksana Zhuravlova0b634332019-10-28 23:04:222062 DateTimeChooserAndroid::CreateForWebContents(this);
[email protected]583418cc2013-01-17 14:01:102063#endif
fsamuel7310a4262014-12-05 05:06:442064
2065 // BrowserPluginGuest::Init needs to be called after this WebContents has
2066 // a RenderWidgetHostViewGuest. That is, |view_->CreateView| above.
2067 if (browser_plugin_guest_)
2068 browser_plugin_guest_->Init();
nickf9acfbe2014-12-23 19:12:372069
2070 for (size_t i = 0; i < g_created_callbacks.Get().size(); i++)
2071 g_created_callbacks.Get().at(i).Run(this);
naskob21fe4872015-02-24 14:15:562072
2073 // If the WebContents creation was renderer-initiated, it means that the
2074 // corresponding RenderView and main RenderFrame have already been created.
2075 // Ensure observers are notified about this.
2076 if (params.renderer_initiated_creation) {
avi3627ecac2015-10-16 17:40:432077 GetRenderViewHost()->GetWidget()->set_renderer_initialized(true);
Alex Moshchuk27caae82017-09-11 23:11:182078 GetRenderViewHost()->DispatchRenderViewCreated();
naskob21fe4872015-02-24 14:15:562079 GetRenderManager()->current_frame_host()->SetRenderFrameCreated(true);
2080 }
naskof5940b9f2015-03-02 23:04:052081
lof84501da082016-05-23 21:22:542082 // Create the renderer process in advance if requested.
Lukasz Anforowicz3caa8372018-06-05 17:22:072083 if (params.desired_renderer_state ==
2084 CreateParams::kInitializeAndWarmupRendererProcess) {
lof84501da082016-05-23 21:22:542085 if (!GetRenderManager()->current_frame_host()->IsRenderFrameLive()) {
2086 GetRenderManager()->InitRenderView(GetRenderViewHost(), nullptr);
2087 }
2088 }
2089
naskof5940b9f2015-03-02 23:04:052090 // Ensure that observers are notified of the creation of this WebContents's
2091 // main RenderFrameHost. It must be done here for main frames, since the
2092 // NotifySwappedFromRenderManager expects view_ to already be created and that
2093 // happens after RenderFrameHostManager::Init.
2094 NotifySwappedFromRenderManager(
2095 nullptr, GetRenderManager()->current_frame_host(), true);
Albert J. Wonge76bf0b82019-09-27 07:47:062096
2097 // For WebContents that are never shown, do critical initialization here which
2098 // would normally only happen when the WebContents is shown.
2099 if (params.is_never_visible) {
2100 // This has just been created so there can only be one frame. Thus it is
2101 // safe to initialize the root.
2102 GetMainFrame()->Init();
2103 }
[email protected]d1198fd2012-08-13 22:50:192104}
2105
[email protected]7fff43e2013-05-21 20:21:102106void WebContentsImpl::OnWebContentsDestroyed(WebContentsImpl* web_contents) {
2107 RemoveDestructionObserver(web_contents);
2108
[email protected]ceee8cd2013-03-08 04:59:512109 // Clear a pending contents that has been closed before being shown.
alexmosc2a8cec2016-05-23 22:19:532110 for (auto iter = pending_contents_.begin(); iter != pending_contents_.end();
[email protected]ceee8cd2013-03-08 04:59:512111 ++iter) {
erikchenbee5c9622018-04-27 19:30:252112 if (iter->second.get() != web_contents)
[email protected]ceee8cd2013-03-08 04:59:512113 continue;
erikchenbee5c9622018-04-27 19:30:252114
2115 // Someone else has deleted the WebContents. That should never happen!
2116 // TODO(erikchen): Fix semantics here. https://crbug.com/832879.
2117 iter->second.release();
[email protected]ceee8cd2013-03-08 04:59:512118 pending_contents_.erase(iter);
[email protected]ceee8cd2013-03-08 04:59:512119 return;
2120 }
2121 NOTREACHED();
[email protected]14392a52012-05-02 20:28:442122}
2123
[email protected]7fff43e2013-05-21 20:21:102124void WebContentsImpl::AddDestructionObserver(WebContentsImpl* web_contents) {
Jan Wilken Dörrie73c901e2019-06-12 09:02:322125 if (!base::Contains(destruction_observers_, web_contents)) {
[email protected]7fff43e2013-05-21 20:21:102126 destruction_observers_[web_contents] =
Jeremy Roman04f27c372017-10-27 15:20:552127 std::make_unique<DestructionObserver>(this, web_contents);
[email protected]7fff43e2013-05-21 20:21:102128 }
2129}
2130
2131void WebContentsImpl::RemoveDestructionObserver(WebContentsImpl* web_contents) {
avia8cf81982016-08-12 20:28:462132 destruction_observers_.erase(web_contents);
[email protected]7fff43e2013-05-21 20:21:102133}
2134
[email protected]b172aee2012-04-10 17:05:262135void WebContentsImpl::AddObserver(WebContentsObserver* observer) {
[email protected]be1f56ab2011-12-22 06:55:312136 observers_.AddObserver(observer);
2137}
2138
[email protected]b172aee2012-04-10 17:05:262139void WebContentsImpl::RemoveObserver(WebContentsObserver* observer) {
[email protected]be1f56ab2011-12-22 06:55:312140 observers_.RemoveObserver(observer);
2141}
2142
[email protected]948481d2014-06-11 18:32:222143std::set<RenderWidgetHostView*>
2144WebContentsImpl::GetRenderWidgetHostViewsInTree() {
2145 std::set<RenderWidgetHostView*> set;
2146 if (ShowingInterstitialPage()) {
lfgf949e0e2017-05-10 22:18:112147 if (RenderWidgetHostView* rwhv = GetRenderWidgetHostView())
2148 set.insert(rwhv);
[email protected]948481d2014-06-11 18:32:222149 } else {
dchengafb53e22016-02-04 08:11:082150 for (RenderFrameHost* rfh : GetAllFrames()) {
lfgf949e0e2017-05-10 22:18:112151 if (RenderWidgetHostView* rwhv = static_cast<RenderFrameHostImpl*>(rfh)
2152 ->frame_tree_node()
2153 ->render_manager()
2154 ->GetRenderWidgetHostView()) {
2155 set.insert(rwhv);
2156 }
dchengafb53e22016-02-04 08:11:082157 }
[email protected]948481d2014-06-11 18:32:222158 }
[email protected]de3c5d82014-05-28 22:12:592159 return set;
2160}
2161
[email protected]b172aee2012-04-10 17:05:262162void WebContentsImpl::Activate() {
[email protected]d5f942ba2008-09-26 19:30:342163 if (delegate_)
2164 delegate_->ActivateContents(this);
2165}
2166
avi3e4b8512015-11-11 01:55:492167void WebContentsImpl::LostCapture(RenderWidgetHostImpl* render_widget_host) {
2168 if (!RenderViewHostImpl::From(render_widget_host))
2169 return;
2170
[email protected]63954792011-07-11 04:17:482171 if (delegate_)
2172 delegate_->LostCapture();
2173}
2174
Steven Holte38041342018-06-22 22:00:572175ukm::SourceId WebContentsImpl::GetUkmSourceIdForLastCommittedSource() const {
2176 return last_committed_source_id_;
2177}
2178
Yue Ru Sun979c4b162019-11-06 16:11:042179ukm::SourceId
2180WebContentsImpl::GetUkmSourceIdForLastCommittedSourceIncludingSameDocument()
2181 const {
2182 return last_committed_source_id_including_same_document_;
2183}
2184
Ahmed Fakhryfaa32d22018-10-05 15:56:162185void WebContentsImpl::SetTopControlsShownRatio(
2186 RenderWidgetHostImpl* render_widget_host,
2187 float ratio) {
2188 if (!delegate_)
2189 return;
2190
2191 RenderFrameHostImpl* rfh = GetMainFrame();
2192 if (!rfh || render_widget_host != rfh->GetRenderWidgetHost())
2193 return;
2194
2195 delegate_->SetTopControlsShownRatio(this, ratio);
Ahmed Fakhry58e6ef542018-09-04 18:05:382196}
2197
Ahmed Fakhry58e6ef542018-09-04 18:05:382198void WebContentsImpl::SetTopControlsGestureScrollInProgress(bool in_progress) {
2199 if (delegate_)
2200 delegate_->SetTopControlsGestureScrollInProgress(in_progress);
2201}
2202
lazyboy63f5b312015-11-23 20:19:522203void WebContentsImpl::RenderWidgetCreated(
2204 RenderWidgetHostImpl* render_widget_host) {
2205 created_widgets_.insert(render_widget_host);
2206}
2207
[email protected]b24b68a2012-09-24 21:57:262208void WebContentsImpl::RenderWidgetDeleted(
2209 RenderWidgetHostImpl* render_widget_host) {
lazyboy63f5b312015-11-23 20:19:522210 // Note that |is_being_destroyed_| can be true at this point as
2211 // ~WebContentsImpl() calls RFHM::ClearRFHsPendingShutdown(), which might lead
2212 // us here.
2213 created_widgets_.erase(render_widget_host);
[email protected]b24b68a2012-09-24 21:57:262214
lazyboy63f5b312015-11-23 20:19:522215 if (is_being_destroyed_)
2216 return;
[email protected]44470a22013-01-24 01:21:542217
2218 if (render_widget_host &&
alexmosc9e76ec2016-05-16 22:59:372219 render_widget_host->GetRoutingID() == fullscreen_widget_routing_id_ &&
2220 render_widget_host->GetProcess()->GetID() ==
2221 fullscreen_widget_process_id_) {
[email protected]4aebbca2013-09-17 22:26:492222 if (delegate_ && delegate_->EmbedsFullscreenWidget())
mlamouri7a78d6fd2015-01-17 13:23:532223 delegate_->ExitFullscreenModeForTab(this);
ericwilligersde386342016-10-19 02:35:092224 for (auto& observer : observers_)
2225 observer.DidDestroyFullscreenWidget();
alexmosc9e76ec2016-05-16 22:59:372226 fullscreen_widget_process_id_ = ChildProcessHost::kInvalidUniqueID;
[email protected]44470a22013-01-24 01:21:542227 fullscreen_widget_routing_id_ = MSG_ROUTING_NONE;
[email protected]0b431992014-06-24 00:08:032228 if (fullscreen_widget_had_focus_at_shutdown_)
2229 view_->RestoreFocus();
[email protected]44470a22013-01-24 01:21:542230 }
lfgbee1e0a2016-06-08 21:24:212231
lfg7d4caad2017-03-22 09:01:452232 if (render_widget_host == mouse_lock_widget_)
lfgff8dfc8d2016-10-21 17:42:322233 LostMouseLock(mouse_lock_widget_);
Joe Downing192998b22018-03-22 15:51:362234
Joe Downingc1a57efe2018-05-09 18:23:542235 CancelKeyboardLock(render_widget_host);
[email protected]b24b68a2012-09-24 21:57:262236}
2237
miu9e14e492014-10-25 02:39:042238void WebContentsImpl::RenderWidgetGotFocus(
2239 RenderWidgetHostImpl* render_widget_host) {
calamity7fe55ce2015-04-10 03:59:372240 // Notify the observers if an embedded fullscreen widget was focused.
2241 if (delegate_ && render_widget_host && delegate_->EmbedsFullscreenWidget() &&
2242 render_widget_host->GetView() == GetFullscreenRenderWidgetHostView()) {
Michael Thiessen896405db2017-07-20 17:52:322243 NotifyWebContentsFocused(render_widget_host);
calamity7fe55ce2015-04-10 03:59:372244 }
miu9e14e492014-10-25 02:39:042245}
2246
zijiehe3bee08e22017-05-17 04:14:462247void WebContentsImpl::RenderWidgetLostFocus(
2248 RenderWidgetHostImpl* render_widget_host) {
Michael Thiessen896405db2017-07-20 17:52:322249 // Notify the observers if an embedded fullscreen widget lost focus.
2250 if (delegate_ && render_widget_host && delegate_->EmbedsFullscreenWidget() &&
2251 render_widget_host->GetView() == GetFullscreenRenderWidgetHostView()) {
2252 NotifyWebContentsLostFocus(render_widget_host);
2253 }
zijiehe3bee08e22017-05-17 04:14:462254}
2255
estaded0a0c992015-01-21 14:12:062256void WebContentsImpl::RenderWidgetWasResized(
rouslan2f5993f2015-01-29 00:18:312257 RenderWidgetHostImpl* render_widget_host,
2258 bool width_changed) {
creisc014b402015-04-23 16:41:452259 RenderFrameHostImpl* rfh = GetMainFrame();
estadece344ec2015-01-23 00:12:292260 if (!rfh || render_widget_host != rfh->GetRenderWidgetHost())
estaded0a0c992015-01-21 14:12:062261 return;
estaded0a0c992015-01-21 14:12:062262
ericwilligersde386342016-10-19 02:35:092263 for (auto& observer : observers_)
2264 observer.MainFrameWasResized(width_changed);
estaded0a0c992015-01-21 14:12:062265}
2266
skyf65d9bb2017-03-24 02:26:392267KeyboardEventProcessingResult WebContentsImpl::PreHandleKeyboardEvent(
2268 const NativeWebKeyboardEvent& event) {
W. James MacLeane1187272019-11-12 03:42:482269 auto* outermost_contents = GetOutermostWebContents();
2270 // TODO(wjmaclean): Generalize this to forward all key events to the outermost
2271 // delegate's handler.
2272 if (outermost_contents != this && IsFullscreenForCurrentTab() &&
2273 event.windows_key_code == ui::VKEY_ESCAPE) {
2274 // When an inner WebContents has focus and is fullscreen, redirect <esc>
2275 // key events to the outermost WebContents so it can be handled by that
2276 // WebContents' delegate.
2277 if (outermost_contents->PreHandleKeyboardEvent(event) ==
2278 KeyboardEventProcessingResult::HANDLED) {
2279 return KeyboardEventProcessingResult::HANDLED;
2280 }
2281 }
skyf65d9bb2017-03-24 02:26:392282 return delegate_ ? delegate_->PreHandleKeyboardEvent(this, event)
2283 : KeyboardEventProcessingResult::NOT_HANDLED;
[email protected]63954792011-07-11 04:17:482284}
2285
Dave Tapuska344399e2019-09-09 15:18:262286bool WebContentsImpl::HandleMouseEvent(const blink::WebMouseEvent& event) {
2287 // Handle mouse button back/forward in the browser process after the render
2288 // process is done with the event. This ensures all renderer-initiated history
2289 // navigations can be treated consistently.
2290 if (event.GetType() == blink::WebInputEvent::Type::kMouseUp) {
2291 WebContentsImpl* outermost = GetOutermostWebContents();
2292 if (event.button == blink::WebPointerProperties::Button::kBack &&
2293 outermost->controller_.CanGoBack()) {
2294 outermost->controller_.GoBack();
2295 return true;
2296 } else if (event.button == blink::WebPointerProperties::Button::kForward &&
2297 outermost->controller_.CanGoForward()) {
2298 outermost->controller_.GoForward();
2299 return true;
2300 }
2301 }
2302 return false;
2303}
2304
Scott Violetd5caa872018-10-16 22:00:512305bool WebContentsImpl::HandleKeyboardEvent(const NativeWebKeyboardEvent& event) {
[email protected]0b094002014-08-20 18:28:102306 if (browser_plugin_embedder_ &&
2307 browser_plugin_embedder_->HandleKeyboardEvent(event)) {
Scott Violetd5caa872018-10-16 22:00:512308 return true;
[email protected]0b094002014-08-20 18:28:102309 }
Scott Violetd5caa872018-10-16 22:00:512310 return delegate_ && delegate_->HandleKeyboardEvent(this, event);
[email protected]63954792011-07-11 04:17:482311}
2312
[email protected]d92026ef2014-03-03 21:04:132313bool WebContentsImpl::HandleWheelEvent(
[email protected]180ef242013-11-07 06:50:462314 const blink::WebMouseWheelEvent& event) {
[email protected]bccc4472013-04-18 16:37:192315#if !defined(OS_MACOSX)
lanweia93644f2015-01-21 22:00:332316 // On platforms other than Mac, control+mousewheel may change zoom. On Mac,
2317 // this isn't done for two reasons:
[email protected]bccc4472013-04-18 16:37:192318 // -the OS already has a gesture to do this through pinch-zoom
2319 // -if a user starts an inertial scroll, let's go, and presses control
2320 // (i.e. control+tab) then the OS's buffered scroll events will come in
2321 // with control key set which isn't what the user wants
Blink Reformat1c4d759e2017-04-09 16:34:542322 if (delegate_ && event.wheel_ticks_y &&
Ehsan Karamad0f83cb112018-09-06 22:07:362323 event.event_action == blink::WebMouseWheelEvent::EventAction::kPageZoom) {
w.shackleton49bcd392016-01-06 17:38:222324 // Count only integer cumulative scrolls as zoom events; this handles
2325 // smooth scroll and regular scroll device behavior.
Blink Reformat1c4d759e2017-04-09 16:34:542326 zoom_scroll_remainder_ += event.wheel_ticks_y;
w.shackleton49bcd392016-01-06 17:38:222327 int whole_zoom_scroll_remainder_ = std::lround(zoom_scroll_remainder_);
2328 zoom_scroll_remainder_ -= whole_zoom_scroll_remainder_;
2329 if (whole_zoom_scroll_remainder_ != 0) {
2330 delegate_->ContentsZoomChange(whole_zoom_scroll_remainder_ > 0);
2331 }
[email protected]fb3f066e2013-02-12 19:12:522332 return true;
2333 }
[email protected]bccc4472013-04-18 16:37:192334#endif
[email protected]fb3f066e2013-02-12 19:12:522335 return false;
2336}
2337
[email protected]04bce562014-01-30 05:34:542338bool WebContentsImpl::PreHandleGestureEvent(
2339 const blink::WebGestureEvent& event) {
[email protected]28042d32014-02-03 14:10:032340 return delegate_ && delegate_->PreHandleGestureEvent(this, event);
[email protected]04bce562014-01-30 05:34:542341}
2342
kenrb2a565f82015-09-02 20:24:592343RenderWidgetHostInputEventRouter* WebContentsImpl::GetInputEventRouter() {
wjmacleane31234f2015-12-14 17:20:452344 if (!is_being_destroyed_ && GetOuterWebContents())
2345 return GetOuterWebContents()->GetInputEventRouter();
2346
2347 if (!rwh_input_event_router_.get() && !is_being_destroyed_)
kenrb2a565f82015-09-02 20:24:592348 rwh_input_event_router_.reset(new RenderWidgetHostInputEventRouter);
2349 return rwh_input_event_router_.get();
2350}
2351
alexmos3fcd0ca2015-10-23 18:18:332352void WebContentsImpl::ReplicatePageFocus(bool is_focused) {
2353 // Focus loss may occur while this WebContents is being destroyed. Don't
2354 // send the message in this case, as the main frame's RenderFrameHost and
2355 // other state has already been cleared.
2356 if (is_being_destroyed_)
2357 return;
2358
2359 frame_tree_.ReplicatePageFocus(is_focused);
2360}
2361
alexmosc4cbacb2015-11-21 01:29:222362RenderWidgetHostImpl* WebContentsImpl::GetFocusedRenderWidgetHost(
2363 RenderWidgetHostImpl* receiving_widget) {
alexmosc4cbacb2015-11-21 01:29:222364 // Events for widgets other than the main frame (e.g., popup menus) should be
2365 // forwarded directly to the widget they arrived on.
2366 if (receiving_widget != GetMainFrame()->GetRenderWidgetHost())
2367 return receiving_widget;
alexmosc4d183e2015-11-06 00:46:522368
lfg1453e412017-04-11 00:48:502369 WebContentsImpl* focused_contents = GetFocusedWebContents();
2370
2371 // If the focused WebContents is showing an interstitial, return the
2372 // interstitial's widget.
2373 if (focused_contents->ShowingInterstitialPage()) {
2374 return static_cast<RenderFrameHostImpl*>(
creisadca9862017-06-16 19:12:112375 focused_contents->interstitial_page_->GetMainFrame())
lfg1453e412017-04-11 00:48:502376 ->GetRenderWidgetHost();
2377 }
2378
paulmeyerfeafc2d2017-04-25 21:46:402379 // If the focused WebContents is a guest WebContents, then get the focused
2380 // frame in the embedder WebContents instead.
W. James MacLean13d834d2019-12-04 16:06:452381 FrameTreeNode* focused_frame =
2382 GetFocusedWebContents()->frame_tree_.GetFocusedFrame();
lfg1453e412017-04-11 00:48:502383
alexmosc4d183e2015-11-06 00:46:522384 if (!focused_frame)
alexmosc4cbacb2015-11-21 01:29:222385 return receiving_widget;
alexmosc4d183e2015-11-06 00:46:522386
alexmos732ca3a2015-12-08 02:01:022387 // The view may be null if a subframe's renderer process has crashed while
2388 // the subframe has focus. Drop the event in that case. Do not give
2389 // it to the main frame, so that the user doesn't unexpectedly type into the
2390 // wrong frame if a focused subframe renderer crashes while they type.
2391 RenderWidgetHostView* view = focused_frame->current_frame_host()->GetView();
2392 if (!view)
2393 return nullptr;
2394
2395 return RenderWidgetHostImpl::From(view->GetRenderWidgetHost());
alexmosc4d183e2015-11-06 00:46:522396}
2397
avallee9993fca2016-11-17 06:16:502398RenderWidgetHostImpl* WebContentsImpl::GetRenderWidgetHostWithPageFocus() {
lfg1453e412017-04-11 00:48:502399 WebContentsImpl* focused_web_contents = GetFocusedWebContents();
2400
2401 if (focused_web_contents->ShowingInterstitialPage()) {
2402 return static_cast<RenderFrameHostImpl*>(
creisadca9862017-06-16 19:12:112403 focused_web_contents->interstitial_page_->GetMainFrame())
lfg1453e412017-04-11 00:48:502404 ->GetRenderWidgetHost();
2405 }
lfg1453e412017-04-11 00:48:502406
2407 return focused_web_contents->GetMainFrame()->GetRenderWidgetHost();
avallee9993fca2016-11-17 06:16:502408}
2409
Dave Tapuska3ed44192018-05-01 18:53:302410void WebContentsImpl::EnterFullscreenMode(
2411 const GURL& origin,
Dave Tapuskaa4189512019-10-15 20:27:342412 const blink::mojom::FullscreenOptions& options) {
mlamouri7a78d6fd2015-01-17 13:23:532413 // This method is being called to enter renderer-initiated fullscreen mode.
2414 // Make sure any existing fullscreen widget is shut down first.
[email protected]4aebbca2013-09-17 22:26:492415 RenderWidgetHostView* const widget_view = GetFullscreenRenderWidgetHostView();
avi85a4cef2015-12-18 20:13:372416 if (widget_view) {
2417 RenderWidgetHostImpl::From(widget_view->GetRenderWidgetHost())
2418 ->ShutdownAndDestroyWidget(true);
2419 }
[email protected]4aebbca2013-09-17 22:26:492420
Joe Downing13dd76b2018-04-09 18:32:152421 if (delegate_) {
Dave Tapuska3ed44192018-05-01 18:53:302422 delegate_->EnterFullscreenModeForTab(this, origin, options);
mlamouri7a78d6fd2015-01-17 13:23:532423
Joe Downing13dd76b2018-04-09 18:32:152424 if (keyboard_lock_widget_)
2425 delegate_->RequestKeyboardLock(this, esc_key_locked_);
2426 }
2427
ericwilligersde386342016-10-19 02:35:092428 for (auto& observer : observers_)
2429 observer.DidToggleFullscreenModeForTab(IsFullscreenForCurrentTab(), false);
mlamouri7a78d6fd2015-01-17 13:23:532430}
2431
bokanece34a82016-01-28 19:49:462432void WebContentsImpl::ExitFullscreenMode(bool will_cause_resize) {
mlamouri7a78d6fd2015-01-17 13:23:532433 // This method is being called to leave renderer-initiated fullscreen mode.
2434 // Make sure any existing fullscreen widget is shut down first.
2435 RenderWidgetHostView* const widget_view = GetFullscreenRenderWidgetHostView();
avi85a4cef2015-12-18 20:13:372436 if (widget_view) {
2437 RenderWidgetHostImpl::From(widget_view->GetRenderWidgetHost())
2438 ->ShutdownAndDestroyWidget(true);
2439 }
mlamouri7a78d6fd2015-01-17 13:23:532440
Joe Downing13dd76b2018-04-09 18:32:152441 if (delegate_) {
mlamouri7a78d6fd2015-01-17 13:23:532442 delegate_->ExitFullscreenModeForTab(this);
[email protected]657c8e02014-03-19 19:18:372443
Joe Downing13dd76b2018-04-09 18:32:152444 if (keyboard_lock_widget_)
2445 delegate_->CancelKeyboardLockRequest(this);
2446 }
2447
bokanece34a82016-01-28 19:49:462448 // The fullscreen state is communicated to the renderer through a resize
2449 // message. If the change in fullscreen state doesn't cause a view resize
2450 // then we must ensure web contents exit the fullscreen state by explicitly
2451 // sending a resize message. This is required for the situation of the browser
2452 // moving the view into a "browser fullscreen" state and then the contents
2453 // entering "tab fullscreen". Exiting the contents "tab fullscreen" then won't
2454 // have the side effect of the view resizing, hence the explicit call here is
2455 // required.
2456 if (!will_cause_resize) {
2457 if (RenderWidgetHostView* rwhv = GetRenderWidgetHostView()) {
2458 if (RenderWidgetHost* render_widget_host = rwhv->GetRenderWidgetHost())
Fady Samuel0b911822018-04-25 13:22:162459 render_widget_host->SynchronizeVisualProperties();
bokanece34a82016-01-28 19:49:462460 }
scheib3dcb6c932015-02-23 10:55:582461 }
2462
Arthur Sonzogni929d29f2018-09-17 14:19:442463 current_fullscreen_frame_ = nullptr;
Becca Hughese9e27952018-06-25 17:08:272464
ericwilligersde386342016-10-19 02:35:092465 for (auto& observer : observers_) {
2466 observer.DidToggleFullscreenModeForTab(IsFullscreenForCurrentTab(),
2467 will_cause_resize);
2468 }
Becca Hughesd11d6502018-07-31 17:01:282469
2470 if (display_cutout_host_impl_)
2471 display_cutout_host_impl_->DidExitFullscreen();
[email protected]8a5e0ca2011-08-25 06:30:472472}
2473
Becca Hughesfd5d8f892018-06-14 18:23:362474void WebContentsImpl::FullscreenStateChanged(RenderFrameHost* rfh,
2475 bool is_fullscreen) {
Arthur Sonzogni929d29f2018-09-17 14:19:442476 RenderFrameHostImpl* frame = static_cast<RenderFrameHostImpl*>(rfh);
Becca Hughesfd5d8f892018-06-14 18:23:362477
2478 if (is_fullscreen) {
Jan Wilken Dörrie531be7ca2019-06-07 10:05:572479 if (!base::Contains(fullscreen_frames_, frame)) {
Arthur Sonzogni929d29f2018-09-17 14:19:442480 fullscreen_frames_.insert(frame);
2481 FullscreenFrameSetUpdated();
Becca Hughesfd5d8f892018-06-14 18:23:362482 }
Arthur Sonzogni929d29f2018-09-17 14:19:442483 return;
2484 }
Becca Hughesfd5d8f892018-06-14 18:23:362485
Arthur Sonzogni929d29f2018-09-17 14:19:442486 // If |frame| is no longer in fullscreen, remove it and any descendants.
2487 // See https://fullscreen.spec.whatwg.org.
Arthur Sonzogni969a794e2018-10-11 08:56:092488 size_t size_before_deletion = fullscreen_frames_.size();
2489 base::EraseIf(fullscreen_frames_, [&](RenderFrameHostImpl* current) {
2490 return (current == frame || current->IsDescendantOf(frame));
Arthur Sonzogni929d29f2018-09-17 14:19:442491 });
2492
Arthur Sonzogni969a794e2018-10-11 08:56:092493 if (size_before_deletion != fullscreen_frames_.size())
Arthur Sonzogni929d29f2018-09-17 14:19:442494 FullscreenFrameSetUpdated();
2495}
2496
2497void WebContentsImpl::FullscreenFrameSetUpdated() {
2498 if (fullscreen_frames_.empty()) {
2499 current_fullscreen_frame_ = nullptr;
2500 return;
Becca Hughesfd5d8f892018-06-14 18:23:362501 }
2502
Arthur Sonzogni929d29f2018-09-17 14:19:442503 // Find the current fullscreen frame and call the observers.
2504 // If frame A is fullscreen, then frame B goes into inner fullscreen, then B
2505 // exits fullscreen - that will result in A being fullscreen.
2506 RenderFrameHostImpl* new_fullscreen_frame = *std::max_element(
2507 fullscreen_frames_.begin(), fullscreen_frames_.end(), FrameCompareDepth);
Becca Hughesfd5d8f892018-06-14 18:23:362508
Arthur Sonzogni929d29f2018-09-17 14:19:442509 // If we have already notified observers about this frame then we should not
2510 // fire the observers again.
2511 if (new_fullscreen_frame == current_fullscreen_frame_)
2512 return;
2513 current_fullscreen_frame_ = new_fullscreen_frame;
Becca Hughesfd5d8f892018-06-14 18:23:362514
Arthur Sonzogni929d29f2018-09-17 14:19:442515 for (auto& observer : observers_)
2516 observer.DidAcquireFullscreen(new_fullscreen_frame);
Becca Hughesfd5d8f892018-06-14 18:23:362517
Arthur Sonzogni929d29f2018-09-17 14:19:442518 if (display_cutout_host_impl_)
2519 display_cutout_host_impl_->DidAcquireFullscreen(new_fullscreen_frame);
Becca Hughesfd5d8f892018-06-14 18:23:362520}
2521
Collin Baker989e0882019-11-01 01:27:172522void WebContentsImpl::UpdateVisibilityAndNotifyPageAndView(
2523 Visibility new_visibility) {
Collin Baker989e0882019-11-01 01:27:172524 // Only hide the page if there are no entities capturing screenshots
Collin Bakerc5259962019-11-14 17:51:582525 // or video (e.g. mirroring). If there are, apply the correct state of
2526 // kHidden or kHiddenButPainting.
2527 PageVisibilityState page_visibility;
2528 if (new_visibility == Visibility::VISIBLE || visible_capturer_count_ > 0)
2529 page_visibility = PageVisibilityState::kVisible;
2530 else if (hidden_capturer_count_ > 0)
2531 page_visibility = PageVisibilityState::kHiddenButPainting;
2532 else
2533 page_visibility = PageVisibilityState::kHidden;
Collin Baker989e0882019-11-01 01:27:172534 // If there are entities in Picture-in-Picture mode, don't activate
2535 // the "disable rendering" optimization.
Collin Bakerc5259962019-11-14 17:51:582536 const bool view_is_visible =
2537 page_visibility != PageVisibilityState::kHidden ||
2538 HasPictureInPictureVideo();
Collin Baker989e0882019-11-01 01:27:172539
Collin Bakerc5259962019-11-14 17:51:582540 if (page_visibility != PageVisibilityState::kHidden) {
Dana Friedcf3bc8e2019-11-13 20:00:242541 // We cannot show a page or capture video unless there is a valid renderer
2542 // associated with this web contents. The navigation controller for this
2543 // page must be set to active (allowing navigation to complete, a renderer
2544 // and its associated views to be created, etc.) if any of these conditions
2545 // holds.
2546 //
2547 // Previously, it was possible for browser-side code to try to capture video
2548 // from a restored tab (for a variety of reasons, including the browser
2549 // creating preview thumbnails) and the tab would never actually load. By
Collin Bakerc5259962019-11-14 17:51:582550 // keying this behavior off of |page_visibility| instead of just
Dana Friedcf3bc8e2019-11-13 20:00:242551 // |new_visibility| we avoid this case. See crbug.com/1020782 for more
2552 // context.
2553 controller_.SetActive(true);
2554
2555 // This shows the Page before showing the individual RenderWidgets, as
2556 // RenderWidgets will work to produce compositor frames and handle input
2557 // as soon as they are shown. But the Page and other classes do not expect
2558 // to be producing frames when the Page is hidden. So we make sure the Page
2559 // is shown first.
Collin Bakerc5259962019-11-14 17:51:582560 SendPageMessage(
2561 new PageMsg_VisibilityChanged(MSG_ROUTING_NONE, page_visibility));
Collin Baker98457b52019-11-06 21:34:292562 }
Collin Baker989e0882019-11-01 01:27:172563
2564 // |GetRenderWidgetHostView()| can be null if the user middle clicks a link to
2565 // open a tab in the background, then closes the tab before selecting it.
2566 // This is because closing the tab calls WebContentsImpl::Destroy(), which
2567 // removes the |GetRenderViewHost()|; then when we actually destroy the
2568 // window, OnWindowPosChanged() notices and calls WasHidden() (which
2569 // calls us).
2570 if (auto* view = GetRenderWidgetHostView()) {
2571 if (view_is_visible) {
2572 view->Show();
Collin Baker989e0882019-11-01 01:27:172573 } else if (new_visibility == Visibility::HIDDEN) {
2574 view->Hide();
2575 } else {
2576 view->WasOccluded();
2577 }
2578 }
2579
2580 if (!ShowingInterstitialPage())
2581 SetVisibilityForChildViews(view_is_visible);
2582
2583 // Make sure to call SetVisibilityAndNotifyObservers(VISIBLE) before notifying
2584 // the CrossProcessFrameConnector.
2585 if (new_visibility == Visibility::VISIBLE) {
2586 last_active_time_ = base::TimeTicks::Now();
2587 SetVisibilityAndNotifyObservers(new_visibility);
2588 }
2589
Collin Bakerc5259962019-11-14 17:51:582590 if (page_visibility == PageVisibilityState::kHidden) {
Collin Baker989e0882019-11-01 01:27:172591 // Similar to when showing the page, we only hide the page after
2592 // hiding the individual RenderWidgets.
Collin Baker98457b52019-11-06 21:34:292593 SendPageMessage(new PageMsg_VisibilityChanged(
2594 MSG_ROUTING_NONE, PageVisibilityState::kHidden));
Collin Baker989e0882019-11-01 01:27:172595 } else {
2596 for (FrameTreeNode* node : frame_tree_.Nodes()) {
2597 RenderFrameProxyHost* parent = node->render_manager()->GetProxyToParent();
2598 if (!parent)
2599 continue;
2600
2601 parent->cross_process_frame_connector()->DelegateWasShown();
2602 }
2603 }
2604
2605 if (new_visibility != Visibility::VISIBLE)
2606 SetVisibilityAndNotifyObservers(new_visibility);
2607}
2608
Clark DuValla0e42992018-09-17 17:00:112609#if defined(OS_ANDROID)
2610void WebContentsImpl::UpdateUserGestureCarryoverInfo() {
2611 if (delegate_)
2612 delegate_->UpdateUserGestureCarryoverInfo(this);
2613}
2614#endif
2615
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:412616bool WebContentsImpl::IsFullscreenForCurrentTab() {
[email protected]199bba6e2012-04-04 16:19:382617 return delegate_ ? delegate_->IsFullscreenForTabOrPending(this) : false;
[email protected]5d5f7af2011-10-01 01:38:122618}
2619
Malay Keshav87c42c02019-01-15 08:37:472620bool WebContentsImpl::ShouldShowStaleContentOnEviction() {
2621 return GetDelegate() && GetDelegate()->ShouldShowStaleContentOnEviction(this);
2622}
2623
billorr21c005d2016-11-17 03:57:042624bool WebContentsImpl::IsFullscreen() {
2625 return IsFullscreenForCurrentTab();
2626}
2627
Eric Willigers052f0432019-10-04 04:06:572628blink::mojom::DisplayMode WebContentsImpl::GetDisplayMode(
avibf58fa0c2015-11-11 01:45:522629 RenderWidgetHostImpl* render_widget_host) const {
2630 if (!RenderViewHostImpl::From(render_widget_host))
Eric Willigers052f0432019-10-04 04:06:572631 return blink::mojom::DisplayMode::kBrowser;
avibf58fa0c2015-11-11 01:45:522632
mikhail.pozdnyakovc0e251b2015-04-15 06:51:122633 return delegate_ ? delegate_->GetDisplayMode(this)
Eric Willigers052f0432019-10-04 04:06:572634 : blink::mojom::DisplayMode::kBrowser;
mikhail.pozdnyakovc0e251b2015-04-15 06:51:122635}
2636
avic3aa8422015-11-09 20:57:222637void WebContentsImpl::RequestToLockMouse(
2638 RenderWidgetHostImpl* render_widget_host,
2639 bool user_gesture,
lfgad8244352016-07-13 16:55:512640 bool last_unlocked_by_target,
2641 bool privileged) {
lfgf0cd46e2017-01-04 00:05:232642 for (WebContentsImpl* current = this; current;
2643 current = current->GetOuterWebContents()) {
2644 if (current->mouse_lock_widget_) {
2645 render_widget_host->GotResponseToLockMouseRequest(false);
2646 return;
2647 }
[email protected]e9621112011-10-17 05:38:372648 }
avic3aa8422015-11-09 20:57:222649
lfgad8244352016-07-13 16:55:512650 if (privileged) {
lfgf0cd46e2017-01-04 00:05:232651 DCHECK(!GetOuterWebContents());
lfgad8244352016-07-13 16:55:512652 mouse_lock_widget_ = render_widget_host;
2653 render_widget_host->GotResponseToLockMouseRequest(true);
2654 return;
2655 }
2656
lfgbee1e0a2016-06-08 21:24:212657 bool widget_in_frame_tree = false;
2658 for (FrameTreeNode* node : frame_tree_.Nodes()) {
2659 if (node->current_frame_host()->GetRenderWidgetHost() ==
2660 render_widget_host) {
2661 widget_in_frame_tree = true;
2662 break;
2663 }
2664 }
2665
2666 if (widget_in_frame_tree && delegate_) {
lfgf0cd46e2017-01-04 00:05:232667 for (WebContentsImpl* current = this; current;
2668 current = current->GetOuterWebContents()) {
2669 current->mouse_lock_widget_ = render_widget_host;
2670 }
2671
avic3aa8422015-11-09 20:57:222672 delegate_->RequestToLockMouse(this, user_gesture, last_unlocked_by_target);
lfgbee1e0a2016-06-08 21:24:212673 } else {
2674 render_widget_host->GotResponseToLockMouseRequest(false);
2675 }
[email protected]e9621112011-10-17 05:38:372676}
2677
avi3e4b8512015-11-11 01:55:492678void WebContentsImpl::LostMouseLock(RenderWidgetHostImpl* render_widget_host) {
lfgbee1e0a2016-06-08 21:24:212679 CHECK(mouse_lock_widget_);
lfgf0cd46e2017-01-04 00:05:232680
2681 if (mouse_lock_widget_->delegate()->GetAsWebContents() != this)
2682 return mouse_lock_widget_->delegate()->LostMouseLock(render_widget_host);
2683
lfgbee1e0a2016-06-08 21:24:212684 mouse_lock_widget_->SendMouseLockLost();
lfgf0cd46e2017-01-04 00:05:232685 for (WebContentsImpl* current = this; current;
2686 current = current->GetOuterWebContents()) {
2687 current->mouse_lock_widget_ = nullptr;
2688 }
avi3e4b8512015-11-11 01:55:492689
[email protected]e9621112011-10-17 05:38:372690 if (delegate_)
2691 delegate_->LostMouseLock();
2692}
2693
lfgad8244352016-07-13 16:55:512694bool WebContentsImpl::HasMouseLock(RenderWidgetHostImpl* render_widget_host) {
2695 // To verify if the mouse is locked, the mouse_lock_widget_ needs to be
2696 // assigned to the widget that requested the mouse lock, and the top-level
2697 // platform RenderWidgetHostView needs to hold the mouse lock from the OS.
Lucas Gadania0ea0172018-09-20 18:31:372698 auto* widget_host = GetTopLevelRenderWidgetHostView();
2699 return mouse_lock_widget_ == render_widget_host && widget_host &&
2700 widget_host->IsMouseLocked();
lfgad8244352016-07-13 16:55:512701}
2702
lfg84763c92017-02-16 18:55:152703RenderWidgetHostImpl* WebContentsImpl::GetMouseLockWidget() {
Lucas Gadania0ea0172018-09-20 18:31:372704 auto* widget_host = GetTopLevelRenderWidgetHostView();
2705 if ((widget_host && widget_host->IsMouseLocked()) ||
lfge2176fb2017-03-26 00:45:472706 (GetFullscreenRenderWidgetHostView() &&
Lucas Gadania0ea0172018-09-20 18:31:372707 GetFullscreenRenderWidgetHostView()->IsMouseLocked())) {
lfg84763c92017-02-16 18:55:152708 return mouse_lock_widget_;
Lucas Gadania0ea0172018-09-20 18:31:372709 }
lfg84763c92017-02-16 18:55:152710
2711 return nullptr;
2712}
2713
Joe Downing192998b22018-03-22 15:51:362714bool WebContentsImpl::RequestKeyboardLock(
Joe Downing13dd76b2018-04-09 18:32:152715 RenderWidgetHostImpl* render_widget_host,
2716 bool esc_key_locked) {
Joe Downing192998b22018-03-22 15:51:362717 DCHECK(render_widget_host);
Joe Downing192998b22018-03-22 15:51:362718 if (render_widget_host->delegate()->GetAsWebContents() != this) {
2719 NOTREACHED();
2720 return false;
2721 }
2722
2723 // KeyboardLock is only supported when called by the top-level browsing
2724 // context and is not supported in embedded content scenarios.
2725 if (GetOuterWebContents())
2726 return false;
2727
Joe Downing13dd76b2018-04-09 18:32:152728 esc_key_locked_ = esc_key_locked;
Joe Downing192998b22018-03-22 15:51:362729 keyboard_lock_widget_ = render_widget_host;
2730
Joe Downing13dd76b2018-04-09 18:32:152731 if (delegate_)
2732 delegate_->RequestKeyboardLock(this, esc_key_locked_);
Joe Downing192998b22018-03-22 15:51:362733 return true;
2734}
2735
2736void WebContentsImpl::CancelKeyboardLock(
2737 RenderWidgetHostImpl* render_widget_host) {
2738 if (!keyboard_lock_widget_ || render_widget_host != keyboard_lock_widget_)
2739 return;
2740
2741 RenderWidgetHostImpl* old_keyboard_lock_widget = keyboard_lock_widget_;
2742 keyboard_lock_widget_ = nullptr;
2743
Joe Downing13dd76b2018-04-09 18:32:152744 if (delegate_)
2745 delegate_->CancelKeyboardLockRequest(this);
2746
Joe Downing192998b22018-03-22 15:51:362747 old_keyboard_lock_widget->CancelKeyboardLock();
2748}
2749
2750RenderWidgetHostImpl* WebContentsImpl::GetKeyboardLockWidget() {
2751 return keyboard_lock_widget_;
2752}
2753
Dave Tapuskac3344362019-02-20 17:54:282754void WebContentsImpl::OnRenderFrameProxyVisibilityChanged(
2755 blink::mojom::FrameVisibility visibility) {
2756 switch (visibility) {
2757 case blink::mojom::FrameVisibility::kRenderedInViewport:
2758 WasShown();
2759 break;
2760 case blink::mojom::FrameVisibility::kNotRendered:
2761 WasHidden();
2762 break;
2763 case blink::mojom::FrameVisibility::kRenderedOutOfViewport:
2764 WasOccluded();
2765 break;
2766 }
ekaramada4f42692016-02-11 19:48:372767}
2768
Albert J. Wong65fe64d2019-09-20 02:48:142769RenderFrameHostDelegate* WebContentsImpl::CreateNewWindow(
csharrison95f01e922017-04-24 18:52:352770 RenderFrameHost* opener,
rockot5c478a72016-09-28 23:14:182771 const mojom::CreateNewWindowParams& params,
Albert J. Wong65fe64d2019-09-20 02:48:142772 bool is_new_browsing_instance,
Sergey Kuznetsov32257a22019-02-11 20:26:502773 bool has_user_gesture,
[email protected]97714c82012-06-06 10:15:132774 SessionStorageNamespace* session_storage_namespace) {
csharrison95f01e922017-04-24 18:52:352775 DCHECK(opener);
nick5ae4d2d2017-01-06 01:18:352776
csharrison95f01e922017-04-24 18:52:352777 int render_process_id = opener->GetProcess()->GetID();
csharrison95f01e922017-04-24 18:52:352778
Albert J. Wong65fe64d2019-09-20 02:48:142779 SiteInstance* source_site_instance = opener->GetSiteInstance();
nick5ae4d2d2017-01-06 01:18:352780
[email protected]bafe6cd2012-05-23 23:09:502781 // We usually create the new window in the same BrowsingInstance (group of
2782 // script-related windows), by passing in the current SiteInstance. However,
[email protected]c4538072013-03-18 02:17:552783 // if the opener is being suppressed (in a non-guest), we create a new
2784 // SiteInstance in its own BrowsingInstance.
[email protected]a24efc22014-05-26 15:50:252785 bool is_guest = BrowserPluginGuest::IsGuest(this);
[email protected]c4538072013-03-18 02:17:552786
[email protected]bafe6cd2012-05-23 23:09:502787 scoped_refptr<SiteInstance> site_instance =
alexmos4cf2aa32015-07-15 23:40:432788 params.opener_suppressed && !is_guest
2789 ? SiteInstance::CreateForURL(GetBrowserContext(), params.target_url)
2790 : source_site_instance;
[email protected]bafe6cd2012-05-23 23:09:502791
[email protected]d1198fd2012-08-13 22:50:192792 // We must assign the SessionStorageNamespace before calling Init().
[email protected]4c3a23582012-08-18 08:54:342793 //
2794 // http://crbug.com/142685
[email protected]fdac6ade2013-07-20 01:06:302795 const std::string& partition_id =
2796 GetContentClient()->browser()->
2797 GetStoragePartitionIdForSite(GetBrowserContext(),
2798 site_instance->GetSiteURL());
[email protected]fc72bb12013-06-02 21:13:462799 StoragePartition* partition = BrowserContext::GetStoragePartition(
2800 GetBrowserContext(), site_instance.get());
[email protected]5f2aa722013-08-07 16:59:412801 DOMStorageContextWrapper* dom_storage_context =
2802 static_cast<DOMStorageContextWrapper*>(partition->GetDOMStorageContext());
[email protected]d1198fd2012-08-13 22:50:192803 SessionStorageNamespaceImpl* session_storage_namespace_impl =
2804 static_cast<SessionStorageNamespaceImpl*>(session_storage_namespace);
2805 CHECK(session_storage_namespace_impl->IsFromContext(dom_storage_context));
[email protected]dd6730412013-08-14 15:03:372806
Albert J. Wong1ceccef92019-10-08 08:25:202807 if (delegate_ && delegate_->IsWebContentsCreationOverridden(
2808 source_site_instance, params.window_container_type,
2809 opener->GetLastCommittedURL(), params.frame_name,
2810 params.target_url)) {
2811 return static_cast<WebContentsImpl*>(delegate_->CreateCustomWebContents(
2812 opener, source_site_instance, is_new_browsing_instance,
2813 opener->GetLastCommittedURL(), params.frame_name, params.target_url,
2814 partition_id, session_storage_namespace));
[email protected]dd6730412013-08-14 15:03:372815 }
2816
danakj22b4e8b2019-08-01 23:14:392817 bool renderer_started_hidden =
2818 params.disposition == WindowOpenDisposition::NEW_BACKGROUND_TAB;
2819
[email protected]dd6730412013-08-14 15:03:372820 // Create the new web contents. This will automatically create the new
2821 // WebContentsView. In the future, we may want to create the view separately.
[email protected]fc72bb12013-06-02 21:13:462822 CreateParams create_params(GetBrowserContext(), site_instance.get());
nasko48321ca32015-07-02 20:44:122823 create_params.main_frame_name = params.frame_name;
alexmos4cf2aa32015-07-15 23:40:432824 create_params.opener_render_process_id = render_process_id;
csharrison95f01e922017-04-24 18:52:352825 create_params.opener_render_frame_id = opener->GetRoutingID();
[email protected]50d326e2014-05-20 17:59:062826 create_params.opener_suppressed = params.opener_suppressed;
danakj22b4e8b2019-08-01 23:14:392827 create_params.initially_hidden = renderer_started_hidden;
[email protected]50d326e2014-05-20 17:59:062828
Albert J. Wong65fe64d2019-09-20 02:48:142829 // Even though all codepaths leading here are in response to a renderer
2830 // tryng to open a new window, if the new window ends up in a different
2831 // browsing instance, then the RenderViewHost, RenderWidgetHost,
2832 // RenderFrameHost constellation is effectively browser initiated
2833 // the opener's process will not given the routing IDs for the new
2834 // objects.
2835 create_params.renderer_initiated_creation = !is_new_browsing_instance;
Albert J. Wong1ceccef92019-10-08 08:25:202836
2837 // If |is_new_browsing_instance| is true, defer routing_id allocation
2838 // to the WebContentsImpl::Create() call. This is required because with
2839 // a new browsing instance, WebContentsImpl::Create() may elect a different
2840 // SiteInstance from |site_instance| (which happens if |site_instance| is
2841 // nullptr for example).
2842 //
2843 // TODO(ajwong): This routing id allocation should be pushed down further
2844 // into WebContentsImpl::Create().
2845 if (!is_new_browsing_instance) {
2846 create_params.routing_id = opener->GetProcess()->GetNextRoutingID();
2847 create_params.main_frame_routing_id =
2848 opener->GetProcess()->GetNextRoutingID();
2849 create_params.main_frame_widget_routing_id =
2850 opener->GetProcess()->GetNextRoutingID();
2851 }
Albert J. Wong65fe64d2019-09-20 02:48:142852
2853 std::unique_ptr<WebContentsImpl> new_contents;
[email protected]c4538072013-03-18 02:17:552854 if (!is_guest) {
2855 create_params.context = view_->GetNativeView();
Albert J. Wong65fe64d2019-09-20 02:48:142856 new_contents = WebContentsImpl::Create(create_params);
2857 } else {
2858 new_contents = base::WrapUnique(static_cast<WebContentsImpl*>(
2859 GetBrowserPluginGuest()->CreateNewGuestWindow(create_params)));
[email protected]c4538072013-03-18 02:17:552860 }
Albert J. Wong65fe64d2019-09-20 02:48:142861 auto* new_contents_impl = new_contents.get();
danakje6c01622018-10-05 19:39:262862
2863 new_contents_impl->GetController().SetSessionStorageNamespace(
erikchenbee5c9622018-04-27 19:30:252864 partition_id, session_storage_namespace);
[email protected]d1198fd2012-08-13 22:50:192865
alexmos646fec02015-07-25 00:11:492866 // If the new frame has a name, make sure any SiteInstances that can find
2867 // this named frame have proxies for it. Must be called after
2868 // SetSessionStorageNamespace, since this calls CreateRenderView, which uses
2869 // GetSessionStorageNamespace.
2870 if (!params.frame_name.empty())
danakje6c01622018-10-05 19:39:262871 new_contents_impl->GetRenderManager()->CreateProxiesForNewNamedFrame();
alexmos646fec02015-07-25 00:11:492872
[email protected]c4538072013-03-18 02:17:552873 // Save the window for later if we're not suppressing the opener (since it
[email protected]d70bea92013-04-05 04:23:342874 // will be shown immediately).
2875 if (!params.opener_suppressed) {
2876 if (!is_guest) {
danakje6c01622018-10-05 19:39:262877 WebContentsView* new_view = new_contents_impl->view_.get();
[email protected]bafe6cd2012-05-23 23:09:502878
[email protected]d70bea92013-04-05 04:23:342879 // TODO(brettw): It seems bogus that we have to call this function on the
2880 // newly created object and give it one of its own member variables.
danakj22b4e8b2019-08-01 23:14:392881 RenderWidgetHostView* widget_view = new_view->CreateViewForWidget(
W. James MacLean13d834d2019-12-04 16:06:452882 new_contents_impl->GetRenderViewHost()->GetWidget());
danakj22b4e8b2019-08-01 23:14:392883 if (!renderer_started_hidden) {
2884 // RenderWidgets for frames always initialize as hidden. If the renderer
2885 // created this window as visible, then we show it here.
2886 widget_view->Show();
2887 }
[email protected]d70bea92013-04-05 04:23:342888 }
[email protected]bafe6cd2012-05-23 23:09:502889 // Save the created window associated with the route so we can show it
2890 // later.
Albert J. Wongcb004632018-07-10 22:58:252891 //
2892 // TODO(ajwong): This should be keyed off the RenderFrame routing id or the
2893 // FrameTreeNode id instead of the routing id of the Widget for the main
2894 // frame. https://crbug.com/545684
Albert J. Wong1ceccef92019-10-08 08:25:202895 DCHECK_NE(MSG_ROUTING_NONE, create_params.main_frame_routing_id);
2896 GlobalRoutingID id(render_process_id,
2897 create_params.main_frame_widget_routing_id);
Albert J. Wong65fe64d2019-09-20 02:48:142898 pending_contents_[id] = std::move(new_contents);
danakje6c01622018-10-05 19:39:262899 AddDestructionObserver(new_contents_impl);
[email protected]bafe6cd2012-05-23 23:09:502900 }
2901
2902 if (delegate_) {
Bryan McQuaded2b93c02017-07-14 22:26:312903 delegate_->WebContentsCreated(this, render_process_id,
2904 opener->GetRoutingID(), params.frame_name,
danakje6c01622018-10-05 19:39:262905 params.target_url, new_contents_impl);
[email protected]bafe6cd2012-05-23 23:09:502906 }
2907
Charlie Harrisonf60dd1c2018-08-15 21:15:412908 for (auto& observer : observers_) {
danakje6c01622018-10-05 19:39:262909 observer.DidOpenRequestedURL(new_contents_impl, opener, params.target_url,
Leon Han963dc182018-11-06 05:41:482910 params.referrer.To<Referrer>(),
2911 params.disposition, ui::PAGE_TRANSITION_LINK,
Charlie Harrisonf60dd1c2018-08-15 21:15:412912 false, // started_from_context_menu
2913 true); // renderer_initiated
pnolandaae574e2017-03-06 21:04:212914 }
2915
Avi Drissmanba3b1b342017-08-29 18:16:132916 // Any new WebContents opened while this WebContents is in fullscreen can be
2917 // used to confuse the user, so drop fullscreen.
Avi Drissmand18c51972018-08-15 18:32:092918 ForSecurityDropFullscreen();
Avi Drissmanba3b1b342017-08-29 18:16:132919
[email protected]bafe6cd2012-05-23 23:09:502920 if (params.opener_suppressed) {
2921 // When the opener is suppressed, the original renderer cannot access the
2922 // new window. As a result, we need to show and navigate the window here.
[email protected]eda238a12012-09-07 23:44:002923 bool was_blocked = false;
erikchenbee5c9622018-04-27 19:30:252924
[email protected]eda238a12012-09-07 23:44:002925 if (delegate_) {
danakje6c01622018-10-05 19:39:262926 base::WeakPtr<WebContentsImpl> weak_new_contents =
2927 new_contents_impl->weak_factory_.GetWeakPtr();
nick0d558482017-02-13 22:27:222928
danakje6c01622018-10-05 19:39:262929 gfx::Rect initial_rect; // Report an empty initial rect.
Albert J. Wong65fe64d2019-09-20 02:48:142930 delegate_->AddNewContents(this, std::move(new_contents),
erikchenbee5c9622018-04-27 19:30:252931 params.disposition, initial_rect,
Sergey Kuznetsov32257a22019-02-11 20:26:502932 has_user_gesture, &was_blocked);
danakje6c01622018-10-05 19:39:262933 // The delegate may delete |new_contents_impl| during AddNewContents().
nick0d558482017-02-13 22:27:222934 if (!weak_new_contents)
Albert J. Wong65fe64d2019-09-20 02:48:142935 return nullptr;
[email protected]eda238a12012-09-07 23:44:002936 }
erikchenbee5c9622018-04-27 19:30:252937
[email protected]eda238a12012-09-07 23:44:002938 if (!was_blocked) {
Nasko Oskov83a8cf92018-10-19 14:58:562939 std::unique_ptr<NavigationController::LoadURLParams> load_params =
2940 std::make_unique<NavigationController::LoadURLParams>(
2941 params.target_url);
Nasko Oskov93e7c55c2018-12-19 01:59:292942 load_params->initiator_origin = opener->GetLastCommittedOrigin();
Aaron Colwell9dab1652019-12-09 18:29:492943 load_params->source_site_instance = source_site_instance;
Leon Han963dc182018-11-06 05:41:482944 load_params->referrer = params.referrer.To<Referrer>();
Nasko Oskov83a8cf92018-10-19 14:58:562945 load_params->transition_type = ui::PAGE_TRANSITION_LINK;
2946 load_params->is_renderer_initiated = true;
Sergey Kuznetsov32257a22019-02-11 20:26:502947 load_params->has_user_gesture = has_user_gesture;
mariakhomenkoa4971c12015-07-21 19:04:372948
2949 if (delegate_ && !is_guest &&
2950 !delegate_->ShouldResumeRequestsForCreatedWindow()) {
Nasko Oskov83a8cf92018-10-19 14:58:562951 // We are in asynchronous add new contents path, delay navigation.
2952 DCHECK(!new_contents_impl->delayed_open_url_params_);
2953 new_contents_impl->delayed_load_url_params_ = std::move(load_params);
mariakhomenkoa4971c12015-07-21 19:04:372954 } else {
Nasko Oskov83a8cf92018-10-19 14:58:562955 new_contents_impl->controller_.LoadURLWithParams(*load_params.get());
Nasko Oskov28ebd5b2018-12-07 22:29:332956 if (!is_guest)
2957 new_contents_impl->Focus();
mariakhomenkoa4971c12015-07-21 19:04:372958 }
[email protected]eda238a12012-09-07 23:44:002959 }
[email protected]bafe6cd2012-05-23 23:09:502960 }
Albert J. Wong65fe64d2019-09-20 02:48:142961 return new_contents_impl;
[email protected]bafe6cd2012-05-23 23:09:502962}
2963
Erik Chenc71b51fe2019-09-19 21:34:272964void WebContentsImpl::CreateNewWidget(int32_t render_process_id,
2965 int32_t widget_route_id,
2966 mojo::PendingRemote<mojom::Widget> widget,
2967 RenderViewHostImpl* render_view_host) {
Albert J. Wongf6e13ed2018-09-18 15:25:472968 CreateNewWidget(render_process_id, widget_route_id, /*is_fullscreen=*/false,
Erik Chenc71b51fe2019-09-19 21:34:272969 std::move(widget), render_view_host);
[email protected]bafe6cd2012-05-23 23:09:502970}
2971
Julie Jeongeun Kimcaacf9102019-09-12 02:13:052972void WebContentsImpl::CreateNewFullscreenWidget(
2973 int32_t render_process_id,
2974 int32_t widget_route_id,
Erik Chenc71b51fe2019-09-19 21:34:272975 mojo::PendingRemote<mojom::Widget> widget,
2976 RenderViewHostImpl* render_view_host) {
Albert J. Wongf6e13ed2018-09-18 15:25:472977 CreateNewWidget(render_process_id, widget_route_id, /*is_fullscreen=*/true,
Erik Chenc71b51fe2019-09-19 21:34:272978 std::move(widget), render_view_host);
[email protected]bafe6cd2012-05-23 23:09:502979}
2980
Erik Chenc71b51fe2019-09-19 21:34:272981void WebContentsImpl::CreateNewWidget(int32_t render_process_id,
2982 int32_t route_id,
2983 bool is_fullscreen,
2984 mojo::PendingRemote<mojom::Widget> widget,
2985 RenderViewHostImpl* render_view_host) {
kenrba26d15092016-02-10 22:00:252986 RenderProcessHost* process = RenderProcessHost::FromID(render_process_id);
kenrbb46f86a2015-12-07 07:53:232987 // A message to create a new widget can only come from an active process for
[email protected]a8504022013-12-04 20:23:512988 // this WebContentsImpl instance. If any other process sends the request,
2989 // it is invalid and the process must be terminated.
dcheng57e39e22016-01-21 00:25:382990 if (!HasMatchingProcess(&frame_tree_, render_process_id)) {
Lukasz Anforowicz6f746282018-01-04 23:24:512991 ReceivedBadMessage(process, bad_message::WCI_NEW_WIDGET_PROCESS_MISMATCH);
[email protected]a8504022013-12-04 20:23:512992 return;
2993 }
2994
Dave Tapuska485aca92017-08-08 00:47:582995 RenderWidgetHostImpl* widget_host = new RenderWidgetHostImpl(
David Black9cca3592019-11-06 23:02:222996 this, process, route_id, std::move(widget), IsHidden(),
2997 std::make_unique<FrameTokenMessageQueue>());
[email protected]cfd80b02014-05-01 17:46:482998 RenderWidgetHostViewBase* widget_view =
2999 static_cast<RenderWidgetHostViewBase*>(
danakj0b246942018-09-17 21:30:273000 view_->CreateViewForChildWidget(widget_host));
[email protected]83918ec2013-01-10 15:37:193001 if (!widget_view)
3002 return;
danakj0b246942018-09-17 21:30:273003 // Fullscreen child widgets are frames, other child widgets are popups, and
3004 // popups should not get activated.
3005 if (!is_fullscreen)
3006 widget_view->SetWidgetType(WidgetType::kPopup);
[email protected]bafe6cd2012-05-23 23:09:503007 // Save the created widget associated with the route so we can show it later.
Lukasz Anforowicz09060bdf72018-08-23 15:53:173008 pending_widget_views_[GlobalRoutingID(render_process_id, route_id)] =
alexmosc2a8cec2016-05-23 22:19:533009 widget_view;
[email protected]bafe6cd2012-05-23 23:09:503010}
3011
alexmosc2a8cec2016-05-23 22:19:533012void WebContentsImpl::ShowCreatedWindow(int process_id,
nick5ae4d2d2017-01-06 01:18:353013 int main_frame_widget_route_id,
[email protected]bafe6cd2012-05-23 23:09:503014 WindowOpenDisposition disposition,
bokan107a47f2015-02-03 23:23:393015 const gfx::Rect& initial_rect,
[email protected]bafe6cd2012-05-23 23:09:503016 bool user_gesture) {
danakje6c01622018-10-05 19:39:263017 // This method is the renderer requesting an existing top level window to
3018 // show a new top level window that the renderer created. Each top level
3019 // window is associated with a WebContents. In this case it was created
3020 // earlier but showing it was deferred until the renderer requested for it
3021 // to be shown. We find that previously created WebContents here.
3022 // TODO(danakj): Why do we defer this show step until the renderer asks for it
3023 // when it will always do so. What needs to happen in the renderer before we
3024 // reach here?
3025 std::unique_ptr<WebContentsImpl> owned_created =
nick5ae4d2d2017-01-06 01:18:353026 GetCreatedWindow(process_id, main_frame_widget_route_id);
danakje6c01622018-10-05 19:39:263027 WebContentsImpl* created = owned_created.get();
3028 // The browser may have rejected the request to make a new window, or the
3029 // renderer could be sending an invalid route id. Ignore the request then.
3030 if (!created)
3031 return;
mariakhomenko44bdc4732015-04-29 01:55:383032
danakje6c01622018-10-05 19:39:263033 // This uses the delegate for the WebContents where the window was created
3034 // from, to control how to show the newly created window.
3035 WebContentsDelegate* delegate = GetDelegate();
nick5ae4d2d2017-01-06 01:18:353036
danakje6c01622018-10-05 19:39:263037 // The delegate can be null in tests, so we must check for it :(.
3038 if (delegate) {
3039 // Mark the web contents as pending resume, then immediately do
3040 // the resume if the delegate wants it.
3041 created->is_resume_pending_ = true;
3042 if (delegate->ShouldResumeRequestsForCreatedWindow())
3043 created->ResumeLoadingCreatedWebContents();
3044
3045 base::WeakPtr<WebContentsImpl> weak_created =
3046 created->weak_factory_.GetWeakPtr();
3047 delegate->AddNewContents(this, std::move(owned_created), disposition,
3048 initial_rect, user_gesture, nullptr);
3049 // The delegate may delete |created| during AddNewContents().
3050 if (!weak_created)
3051 return;
[email protected]eda238a12012-09-07 23:44:003052 }
danakje6c01622018-10-05 19:39:263053
3054 RenderWidgetHostImpl* rwh = created->GetMainFrame()->GetRenderWidgetHost();
3055 DCHECK_EQ(main_frame_widget_route_id, rwh->GetRoutingID());
3056 rwh->Send(new WidgetMsg_SetBounds_ACK(rwh->GetRoutingID()));
[email protected]bafe6cd2012-05-23 23:09:503057}
3058
alexmosc2a8cec2016-05-23 22:19:533059void WebContentsImpl::ShowCreatedWidget(int process_id,
Albert J. Wongf6e13ed2018-09-18 15:25:473060 int widget_route_id,
bokan107a47f2015-02-03 23:23:393061 const gfx::Rect& initial_rect) {
Albert J. Wongf6e13ed2018-09-18 15:25:473062 ShowCreatedWidget(process_id, widget_route_id, false, initial_rect);
[email protected]bafe6cd2012-05-23 23:09:503063}
3064
alexmosc2a8cec2016-05-23 22:19:533065void WebContentsImpl::ShowCreatedFullscreenWidget(int process_id,
Albert J. Wongf6e13ed2018-09-18 15:25:473066 int widget_route_id) {
3067 ShowCreatedWidget(process_id, widget_route_id, true, gfx::Rect());
[email protected]bafe6cd2012-05-23 23:09:503068}
3069
alexmosc2a8cec2016-05-23 22:19:533070void WebContentsImpl::ShowCreatedWidget(int process_id,
3071 int route_id,
[email protected]bafe6cd2012-05-23 23:09:503072 bool is_fullscreen,
bokan107a47f2015-02-03 23:23:393073 const gfx::Rect& initial_rect) {
[email protected]cfd80b02014-05-01 17:46:483074 RenderWidgetHostViewBase* widget_host_view =
alexmosc2a8cec2016-05-23 22:19:533075 static_cast<RenderWidgetHostViewBase*>(
3076 GetCreatedWidget(process_id, route_id));
[email protected]bafe6cd2012-05-23 23:09:503077 if (!widget_host_view)
3078 return;
[email protected]cfd80b02014-05-01 17:46:483079
W. James MacLeanec4125c2019-09-27 18:56:003080 // GetOutermostWebContents() returns |this| if there are no outer WebContents.
W. James MacLean3c5f50462019-10-28 16:24:213081 auto* outer_web_contents = GetOuterWebContents();
3082 auto* outermost_web_contents = GetOutermostWebContents();
W. James MacLeanec4125c2019-09-27 18:56:003083 RenderWidgetHostView* view =
W. James MacLean3c5f50462019-10-28 16:24:213084 outermost_web_contents->GetRenderWidgetHostView();
3085 // It's not entirely obvious why we need the transform only in the case where
3086 // the outer webcontents is not the same as the outermost webcontents. It may
3087 // be due to the fact that oopifs that are children of the mainframe get
3088 // correct values for their screenrects, but deeper cross-process frames do
3089 // not. Hopefully this can be resolved with https://crbug.com/928825.
3090 // Handling these cases separately is needed for http://crbug.com/1015298.
3091 bool needs_transform = this != outermost_web_contents &&
3092 outermost_web_contents != outer_web_contents;
W. James MacLeanec4125c2019-09-27 18:56:003093
3094 gfx::Rect transformed_rect(initial_rect);
3095 RenderWidgetHostView* this_view = GetRenderWidgetHostView();
W. James MacLean3c5f50462019-10-28 16:24:213096 if (needs_transform) {
W. James MacLeanec4125c2019-09-27 18:56:003097 // We need to transform the coordinates of initial_rect.
3098 gfx::Point origin =
3099 this_view->TransformPointToRootCoordSpace(initial_rect.origin());
3100 gfx::Point bottom_right =
3101 this_view->TransformPointToRootCoordSpace(initial_rect.bottom_right());
3102 transformed_rect =
3103 gfx::Rect(origin.x(), origin.y(), bottom_right.x() - origin.x(),
3104 bottom_right.y() - origin.y());
[email protected]cfd80b02014-05-01 17:46:483105 }
3106
danakj0b246942018-09-17 21:30:273107 // Fullscreen child widgets are frames, other child widgets are popups.
[email protected]4aebbca2013-09-17 22:26:493108 if (is_fullscreen) {
[email protected]d7f80ba2013-10-12 07:42:313109 DCHECK_EQ(MSG_ROUTING_NONE, fullscreen_widget_routing_id_);
[email protected]0b431992014-06-24 00:08:033110 view_->StoreFocus();
alexmosc9e76ec2016-05-16 22:59:373111 fullscreen_widget_process_id_ =
3112 widget_host_view->GetRenderWidgetHost()->GetProcess()->GetID();
[email protected]d7f80ba2013-10-12 07:42:313113 fullscreen_widget_routing_id_ = route_id;
[email protected]4aebbca2013-09-17 22:26:493114 if (delegate_ && delegate_->EmbedsFullscreenWidget()) {
3115 widget_host_view->InitAsChild(GetRenderWidgetHostView()->GetNativeView());
Dave Tapuska3ed44192018-05-01 18:53:303116 delegate_->EnterFullscreenModeForTab(this, GURL(),
Dave Tapuskaa4189512019-10-15 20:27:343117 blink::mojom::FullscreenOptions());
[email protected]4aebbca2013-09-17 22:26:493118 } else {
[email protected]cfd80b02014-05-01 17:46:483119 widget_host_view->InitAsFullscreen(view);
[email protected]4aebbca2013-09-17 22:26:493120 }
ericwilligersde386342016-10-19 02:35:093121 for (auto& observer : observers_)
3122 observer.DidShowFullscreenWidget();
[email protected]4aebbca2013-09-17 22:26:493123 if (!widget_host_view->HasFocus())
3124 widget_host_view->Focus();
3125 } else {
W. James MacLeanec4125c2019-09-27 18:56:003126 widget_host_view->InitAsPopup(view, transformed_rect);
[email protected]4aebbca2013-09-17 22:26:493127 }
[email protected]89054502012-06-03 10:29:243128
yiyixcb1fbc4f2018-03-16 19:54:083129 RenderWidgetHostImpl* render_widget_host_impl = widget_host_view->host();
[email protected]89054502012-06-03 10:29:243130 render_widget_host_impl->Init();
[email protected]d7f80ba2013-10-12 07:42:313131 // Only allow privileged mouse lock for fullscreen render widget, which is
3132 // used to implement Pepper Flash fullscreen.
3133 render_widget_host_impl->set_allow_privileged_mouse_lock(is_fullscreen);
[email protected]bafe6cd2012-05-23 23:09:503134}
3135
danakje6c01622018-10-05 19:39:263136std::unique_ptr<WebContentsImpl> WebContentsImpl::GetCreatedWindow(
nick5ae4d2d2017-01-06 01:18:353137 int process_id,
3138 int main_frame_widget_route_id) {
Lukasz Anforowicz09060bdf72018-08-23 15:53:173139 auto key = GlobalRoutingID(process_id, main_frame_widget_route_id);
nick5ae4d2d2017-01-06 01:18:353140 auto iter = pending_contents_.find(key);
[email protected]bafe6cd2012-05-23 23:09:503141
3142 // Certain systems can block the creation of new windows. If we didn't succeed
3143 // in creating one, just return NULL.
alexmosc2a8cec2016-05-23 22:19:533144 if (iter == pending_contents_.end())
3145 return nullptr;
[email protected]bafe6cd2012-05-23 23:09:503146
danakje6c01622018-10-05 19:39:263147 std::unique_ptr<WebContentsImpl> new_contents = std::move(iter->second);
nick5ae4d2d2017-01-06 01:18:353148 pending_contents_.erase(key);
danakje6c01622018-10-05 19:39:263149 RemoveDestructionObserver(new_contents.get());
[email protected]bafe6cd2012-05-23 23:09:503150
[email protected]d70bea92013-04-05 04:23:343151 // Don't initialize the guest WebContents immediately.
danakje6c01622018-10-05 19:39:263152 if (BrowserPluginGuest::IsGuest(new_contents.get()))
[email protected]d70bea92013-04-05 04:23:343153 return new_contents;
3154
Lukasz Anforowicz5510ed652018-06-06 16:16:193155 if (!new_contents->GetMainFrame()->GetProcess()->IsInitializedAndNotDead() ||
nick5ae4d2d2017-01-06 01:18:353156 !new_contents->GetMainFrame()->GetView()) {
alexmosc2a8cec2016-05-23 22:19:533157 return nullptr;
nick5ae4d2d2017-01-06 01:18:353158 }
[email protected]bafe6cd2012-05-23 23:09:503159
[email protected]bafe6cd2012-05-23 23:09:503160 return new_contents;
3161}
3162
alexmosc2a8cec2016-05-23 22:19:533163RenderWidgetHostView* WebContentsImpl::GetCreatedWidget(int process_id,
3164 int route_id) {
Lukasz Anforowicz09060bdf72018-08-23 15:53:173165 auto iter = pending_widget_views_.find(GlobalRoutingID(process_id, route_id));
[email protected]bafe6cd2012-05-23 23:09:503166 if (iter == pending_widget_views_.end()) {
3167 DCHECK(false);
alexmosc2a8cec2016-05-23 22:19:533168 return nullptr;
[email protected]bafe6cd2012-05-23 23:09:503169 }
3170
3171 RenderWidgetHostView* widget_host_view = iter->second;
Lukasz Anforowicz09060bdf72018-08-23 15:53:173172 pending_widget_views_.erase(GlobalRoutingID(process_id, route_id));
[email protected]bafe6cd2012-05-23 23:09:503173
3174 RenderWidgetHost* widget_host = widget_host_view->GetRenderWidgetHost();
Lukasz Anforowicz5510ed652018-06-06 16:16:193175 if (!widget_host->GetProcess()->IsInitializedAndNotDead()) {
[email protected]bafe6cd2012-05-23 23:09:503176 // The view has gone away or the renderer crashed. Nothing to do.
alexmosc2a8cec2016-05-23 22:19:533177 return nullptr;
[email protected]bafe6cd2012-05-23 23:09:503178 }
3179
3180 return widget_host_view;
3181}
3182
[email protected]f13b4202012-06-12 23:53:233183void WebContentsImpl::RequestMediaAccessPermission(
[email protected]33662e52013-01-07 21:31:093184 const MediaStreamRequest& request,
Mark Pilgrim57499082018-06-12 12:38:303185 MediaResponseCallback callback) {
[email protected]d19b84b2014-03-14 11:52:373186 if (delegate_) {
Mark Pilgrim57499082018-06-12 12:38:303187 delegate_->RequestMediaAccessPermission(this, request, std::move(callback));
[email protected]d19b84b2014-03-14 11:52:373188 } else {
Antonio Gomes0d42960a2019-06-05 12:35:513189 std::move(callback).Run(
3190 blink::MediaStreamDevices(),
3191 blink::mojom::MediaStreamRequestResult::FAILED_DUE_TO_SHUTDOWN,
3192 std::unique_ptr<MediaStreamUI>());
[email protected]d19b84b2014-03-14 11:52:373193 }
[email protected]f13b4202012-06-12 23:53:233194}
3195
Chandan Padhia4b8bcb72017-09-12 16:41:043196bool WebContentsImpl::CheckMediaAccessPermission(
Raymes Khouryad7c24a12018-03-05 23:22:583197 RenderFrameHost* render_frame_host,
Chandan Padhia4b8bcb72017-09-12 16:41:043198 const url::Origin& security_origin,
Antonio Gomesc8b734b2019-06-05 18:22:163199 blink::mojom::MediaStreamType type) {
3200 DCHECK(type == blink::mojom::MediaStreamType::DEVICE_AUDIO_CAPTURE ||
3201 type == blink::mojom::MediaStreamType::DEVICE_VIDEO_CAPTURE);
Chandan Padhia4b8bcb72017-09-12 16:41:043202 return delegate_ && delegate_->CheckMediaAccessPermission(
Raymes Khouryad7c24a12018-03-05 23:22:583203 render_frame_host, security_origin.GetURL(), type);
grunell657d4d82014-09-18 00:09:433204}
3205
Guido Urdaneta73fa6632019-01-14 18:46:263206std::string WebContentsImpl::GetDefaultMediaDeviceID(
Antonio Gomesc8b734b2019-06-05 18:22:163207 blink::mojom::MediaStreamType type) {
guidou8a440b842017-01-30 13:58:433208 if (!delegate_)
3209 return std::string();
3210 return delegate_->GetDefaultMediaDeviceID(this, type);
3211}
3212
[email protected]cc9200432013-07-23 23:02:403213SessionStorageNamespace* WebContentsImpl::GetSessionStorageNamespace(
3214 SiteInstance* instance) {
3215 return controller_.GetSessionStorageNamespace(instance);
3216}
3217
[email protected]6de7fc482014-06-06 10:46:443218SessionStorageNamespaceMap WebContentsImpl::GetSessionStorageNamespaceMap() {
3219 return controller_.GetSessionStorageNamespaceMap();
3220}
3221
[email protected]9b159a52013-10-03 17:24:553222FrameTree* WebContentsImpl::GetFrameTree() {
3223 return &frame_tree_;
3224}
3225
aelias5252baa2016-04-10 01:18:023226bool WebContentsImpl::IsOverridingUserAgent() {
3227 return GetController().GetVisibleEntry() &&
3228 GetController().GetVisibleEntry()->GetIsOverridingUserAgent();
3229}
3230
avi85ee8362016-10-08 02:09:083231bool WebContentsImpl::IsJavaScriptDialogShowing() const {
3232 return is_showing_javascript_dialog_;
3233}
3234
avic031d392017-03-03 03:09:423235bool WebContentsImpl::ShouldIgnoreUnresponsiveRenderer() {
3236 // Ignore unresponsive renderers if the debugger is attached to them since the
3237 // unresponsiveness might be a result of the renderer sitting on a breakpoint.
3238 //
David Bienvenu525f8a02019-02-15 02:02:423239#ifdef OS_WIN
3240 // Check if a windows debugger is attached to the renderer process.
3241 base::ProcessHandle process_handle =
3242 GetMainFrame()->GetProcess()->GetProcess().Handle();
3243 BOOL debugger_present = FALSE;
3244 if (CheckRemoteDebuggerPresent(process_handle, &debugger_present) &&
3245 debugger_present)
3246 return true;
3247#endif // OS_WIN
3248
avic031d392017-03-03 03:09:423249 // TODO(pfeldman): Fix this to only return true if the renderer is *actually*
3250 // sitting on a breakpoint. https://crbug.com/684202
3251 return DevToolsAgentHost::IsDebuggerAttached(this);
3252}
3253
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:413254ui::AXMode WebContentsImpl::GetAccessibilityMode() {
[email protected]95640212014-07-26 18:14:303255 return accessibility_mode_;
3256}
3257
Dominic Mazzonia7b0edb22017-08-09 16:32:513258void WebContentsImpl::AccessibilityEventReceived(
Dominic Mazzoniccbaa9b2018-06-06 07:44:233259 const AXEventNotificationDetails& details) {
ericwilligersde386342016-10-19 02:35:093260 for (auto& observer : observers_)
Dominic Mazzonia7b0edb22017-08-09 16:32:513261 observer.AccessibilityEventReceived(details);
[email protected]31a71eaf2014-03-13 01:47:363262}
3263
dmazzoni2400c462016-08-23 15:07:133264void WebContentsImpl::AccessibilityLocationChangesReceived(
3265 const std::vector<AXLocationChangeNotificationDetails>& details) {
ericwilligersde386342016-10-19 02:35:093266 for (auto& observer : observers_)
Dominic Mazzonia7b0edb22017-08-09 16:32:513267 observer.AccessibilityLocationChangesReceived(details);
dmazzoni2400c462016-08-23 15:07:133268}
3269
Abigail Kleinabb428332019-09-13 18:26:213270base::string16 WebContentsImpl::DumpAccessibilityTree(
3271 bool internal,
3272 std::vector<AccessibilityTreeFormatter::PropertyFilter> property_filters) {
James Wallace-Leeeafc94cb92018-07-23 21:35:093273 auto* ax_mgr = GetOrCreateRootBrowserAccessibilityManager();
3274 DCHECK(ax_mgr);
Lei Zhangc98caa3822019-11-07 19:17:273275 return AccessibilityTreeFormatterBase::DumpAccessibilityTreeFromManager(
Abigail Kleinabb428332019-09-13 18:26:213276 ax_mgr, internal, property_filters);
James Wallace-Leeeafc94cb92018-07-23 21:35:093277}
3278
Abigail Kleinaa898742019-11-01 02:31:253279void WebContentsImpl::RecordAccessibilityEvents(
3280 AccessibilityEventCallback callback,
3281 bool start) {
3282 if (start) {
3283 SetAccessibilityMode(ui::AXMode::kWebContents);
3284 auto* ax_mgr = GetOrCreateRootBrowserAccessibilityManager();
3285 DCHECK(ax_mgr);
3286 base::ProcessId pid = base::Process::Current().Pid();
3287 event_recorder_ = content::AccessibilityEventRecorder::Create(
3288 ax_mgr, pid, base::StringPiece{});
3289 event_recorder_->ListenToEvents(callback);
3290 } else {
3291 DCHECK(event_recorder_);
3292 event_recorder_->FlushAsyncEvents();
3293 event_recorder_ = nullptr;
3294 }
3295}
3296
dmazzonia656928e2014-09-15 20:28:543297RenderFrameHost* WebContentsImpl::GetGuestByInstanceID(
fsamuel833ee7c2015-02-13 23:40:403298 RenderFrameHost* render_frame_host,
dmazzonia656928e2014-09-15 20:28:543299 int browser_plugin_instance_id) {
3300 BrowserPluginGuestManager* guest_manager =
3301 GetBrowserContext()->GetGuestManager();
fsamuel9cbc00a2015-04-17 20:30:203302 if (!guest_manager)
3303 return nullptr;
3304
dmazzonia656928e2014-09-15 20:28:543305 WebContents* guest = guest_manager->GetGuestByInstanceID(
fsamuel2e9413d2015-02-25 01:25:443306 render_frame_host->GetProcess()->GetID(), browser_plugin_instance_id);
dmazzonia656928e2014-09-15 20:28:543307 if (!guest)
fsamuel9cbc00a2015-04-17 20:30:203308 return nullptr;
3309
dmazzonia656928e2014-09-15 20:28:543310 return guest->GetMainFrame();
3311}
3312
Ke He7319dbe2017-11-09 05:54:443313device::mojom::GeolocationContext* WebContentsImpl::GetGeolocationContext() {
3314 if (geolocation_context_)
3315 return geolocation_context_.get();
3316
Ken Rockotfd03e682019-06-20 21:13:543317 service_manager::Connector* connector = GetSystemConnector();
Miyoung Shin0f4480d2019-09-12 01:15:263318 auto receiver = geolocation_context_.BindNewPipeAndPassReceiver();
Ken Rockotfd03e682019-06-20 21:13:543319 if (connector)
Miyoung Shin0f4480d2019-09-12 01:15:263320 connector->Connect(device::mojom::kServiceName, std::move(receiver));
Conley Owens6894c4f2017-07-10 19:29:133321 return geolocation_context_.get();
blundellc57b93f2014-10-29 13:19:573322}
3323
ke.he98b761e2017-05-09 05:59:173324device::mojom::WakeLockContext* WebContentsImpl::GetWakeLockContext() {
blundelld8cd72b2017-03-28 07:18:383325 if (!wake_lock_context_host_)
3326 wake_lock_context_host_.reset(new WakeLockContextHost(this));
blundelle75a8f92017-03-27 08:11:173327 return wake_lock_context_host_->GetWakeLockContext();
alogvinovf50445a2015-10-30 13:00:123328}
3329
blundellf5316fc2017-05-15 11:49:033330#if defined(OS_ANDROID)
Miyoung Shin59f39ff2019-09-17 07:23:203331void WebContentsImpl::GetNFC(
3332 mojo::PendingReceiver<device::mojom::NFC> receiver) {
Leon Hanccb985f2019-08-26 02:43:243333 NFCHost nfc_host(this);
Miyoung Shin59f39ff2019-09-17 07:23:203334 nfc_host.GetNFC(std::move(receiver));
blundellf5316fc2017-05-15 11:49:033335}
3336#endif
3337
Evan Stade5e750ad02017-08-07 21:59:273338void WebContentsImpl::SetNotWaitingForResponse() {
3339 if (waiting_for_response_ == false)
3340 return;
3341
3342 waiting_for_response_ = false;
3343 if (delegate_)
3344 delegate_->LoadingStateChanged(this, is_load_to_different_document_);
Chris Hamiltonf82f2a702018-02-15 19:37:293345 for (auto& observer : observers_)
3346 observer.DidReceiveResponse();
Evan Stade5e750ad02017-08-07 21:59:273347}
3348
lfgbb9c28a2016-03-01 03:19:493349void WebContentsImpl::SendScreenRects() {
kenrbc52e1b4e2016-03-20 01:03:373350 for (FrameTreeNode* node : frame_tree_.Nodes()) {
kenrb0e8dc202016-05-30 19:51:403351 if (node->current_frame_host()->is_local_root())
kenrbc52e1b4e2016-03-20 01:03:373352 node->current_frame_host()->GetRenderWidgetHost()->SendScreenRects();
3353 }
lfgbb9c28a2016-03-01 03:19:493354
danakj4df8a9d02019-02-05 20:36:593355 // Interstitials have their own |frame_tree_|.
3356 if (interstitial_page_) {
3357 FrameTree* interstitial_frame_tree = interstitial_page_->GetFrameTree();
3358 for (FrameTreeNode* node : interstitial_frame_tree->Nodes()) {
3359 if (node->current_frame_host()->is_local_root())
3360 node->current_frame_host()->GetRenderWidgetHost()->SendScreenRects();
3361 }
lfgdb5c4ed2016-03-04 23:09:073362 }
[email protected]32deec62013-05-15 23:55:043363}
3364
ekaramadadd882292016-06-08 15:22:563365TextInputManager* WebContentsImpl::GetTextInputManager() {
3366 if (GetOuterWebContents())
3367 return GetOuterWebContents()->GetTextInputManager();
3368
Ehsan Karamadc179dc02018-12-15 03:09:293369 if (!text_input_manager_ && !browser_plugin_guest_) {
Darren Shenca53d5f2018-05-15 04:56:013370 text_input_manager_.reset(new TextInputManager(
3371 GetBrowserContext() &&
3372 !GetBrowserContext()->IsOffTheRecord()) /* should_do_learning */);
3373 }
ekaramadadd882292016-06-08 15:22:563374
3375 return text_input_manager_.get();
3376}
3377
paulmeyer7f6f1d4f2016-11-15 00:00:273378bool WebContentsImpl::OnUpdateDragCursor() {
Lei Zhang6410ec1a2019-01-31 21:38:583379 return browser_plugin_embedder_ &&
3380 browser_plugin_embedder_->OnUpdateDragCursor();
paulmeyer7f6f1d4f2016-11-15 00:00:273381}
3382
kenrb11f213a2017-03-24 18:12:063383bool WebContentsImpl::IsWidgetForMainFrame(
3384 RenderWidgetHostImpl* render_widget_host) {
3385 return render_widget_host == GetMainFrame()->GetRenderWidgetHost();
3386}
3387
[email protected]95640212014-07-26 18:14:303388BrowserAccessibilityManager*
3389 WebContentsImpl::GetRootBrowserAccessibilityManager() {
Dominic Mazzoniefb5f9e32017-12-21 01:08:223390 RenderFrameHostImpl* rfh = static_cast<RenderFrameHostImpl*>(
3391 ShowingInterstitialPage() ? GetInterstitialPage()->GetMainFrame()
3392 : GetMainFrame());
creisc014b402015-04-23 16:41:453393 return rfh ? rfh->browser_accessibility_manager() : nullptr;
[email protected]95640212014-07-26 18:14:303394}
3395
3396BrowserAccessibilityManager*
3397 WebContentsImpl::GetOrCreateRootBrowserAccessibilityManager() {
Dominic Mazzoniefb5f9e32017-12-21 01:08:223398 RenderFrameHostImpl* rfh = static_cast<RenderFrameHostImpl*>(
3399 ShowingInterstitialPage() ? GetInterstitialPage()->GetMainFrame()
3400 : GetMainFrame());
creisc014b402015-04-23 16:41:453401 return rfh ? rfh->GetOrCreateBrowserAccessibilityManager() : nullptr;
[email protected]95640212014-07-26 18:14:303402}
3403
Dave Tapuskab336b922017-07-06 19:24:053404void WebContentsImpl::ExecuteEditCommand(
3405 const std::string& command,
3406 const base::Optional<base::string16>& value) {
Dave Tapuskae782bea2019-07-09 16:21:513407 auto* input_handler = GetFocusedFrameInputHandler();
3408 if (!input_handler)
Dave Tapuskab336b922017-07-06 19:24:053409 return;
3410
Dave Tapuskae782bea2019-07-09 16:21:513411 input_handler->ExecuteEditCommand(command, value);
Dave Tapuskab336b922017-07-06 19:24:053412}
3413
mohsen7ab1ec16ec2015-07-02 18:26:233414void WebContentsImpl::MoveRangeSelectionExtent(const gfx::Point& extent) {
Dave Tapuskae782bea2019-07-09 16:21:513415 auto* input_handler = GetFocusedFrameInputHandler();
3416 if (!input_handler)
mohsen7ab1ec16ec2015-07-02 18:26:233417 return;
3418
Dave Tapuskae782bea2019-07-09 16:21:513419 input_handler->MoveRangeSelectionExtent(extent);
mohsen7ab1ec16ec2015-07-02 18:26:233420}
3421
3422void WebContentsImpl::SelectRange(const gfx::Point& base,
3423 const gfx::Point& extent) {
Dave Tapuskae782bea2019-07-09 16:21:513424 auto* input_handler = GetFocusedFrameInputHandler();
3425 if (!input_handler)
mohsen7ab1ec16ec2015-07-02 18:26:233426 return;
3427
Dave Tapuskae782bea2019-07-09 16:21:513428 input_handler->SelectRange(base, extent);
mohsen7ab1ec16ec2015-07-02 18:26:233429}
3430
Dave Tapuskab336b922017-07-06 19:24:053431void WebContentsImpl::MoveCaret(const gfx::Point& extent) {
Dave Tapuskae782bea2019-07-09 16:21:513432 auto* input_handler = GetFocusedFrameInputHandler();
3433 if (!input_handler)
Dave Tapuskab336b922017-07-06 19:24:053434 return;
3435
Dave Tapuskae782bea2019-07-09 16:21:513436 input_handler->MoveCaret(extent);
Dave Tapuskab336b922017-07-06 19:24:053437}
3438
Shimi Zhang37deeb22017-09-28 00:59:013439void WebContentsImpl::AdjustSelectionByCharacterOffset(
3440 int start_adjust,
3441 int end_adjust,
3442 bool show_selection_menu) {
Dave Tapuskae782bea2019-07-09 16:21:513443 auto* input_handler = GetFocusedFrameInputHandler();
3444 if (!input_handler)
aurimasab0319022015-07-10 21:57:383445 return;
3446
Shimi Zhang37deeb22017-09-28 00:59:013447 using blink::mojom::SelectionMenuBehavior;
Dave Tapuskae782bea2019-07-09 16:21:513448 input_handler->AdjustSelectionByCharacterOffset(
Shimi Zhang37deeb22017-09-28 00:59:013449 start_adjust, end_adjust,
3450 show_selection_menu ? SelectionMenuBehavior::kShow
3451 : SelectionMenuBehavior::kHide);
aurimasab0319022015-07-10 21:57:383452}
3453
[email protected]b172aee2012-04-10 17:05:263454void WebContentsImpl::UpdatePreferredSize(const gfx::Size& pref_size) {
[email protected]222f5822014-02-05 23:40:493455 const gfx::Size old_size = GetPreferredSize();
[email protected]bcd2815602012-01-14 18:17:233456 preferred_size_ = pref_size;
[email protected]222f5822014-02-05 23:40:493457 OnPreferredSizeChanged(old_size);
[email protected]0548c5352011-09-07 00:33:333458}
3459
avic3aa8422015-11-09 20:57:223460void WebContentsImpl::ResizeDueToAutoResize(
3461 RenderWidgetHostImpl* render_widget_host,
Fady Samuel97947112018-05-05 16:24:543462 const gfx::Size& new_size) {
avic3aa8422015-11-09 20:57:223463 if (render_widget_host != GetRenderViewHost()->GetWidget())
3464 return;
3465
kenrbe4f91082017-06-02 20:32:083466 auto_resize_size_ = new_size;
3467
3468 // Out-of-process iframe visible viewport sizes usually come from the
3469 // top-level RenderWidgetHostView, but when auto-resize is enabled on the
3470 // top frame then that size is used instead.
3471 for (FrameTreeNode* node : frame_tree_.Nodes()) {
3472 if (node->current_frame_host()->is_local_root()) {
3473 RenderWidgetHostImpl* host =
3474 node->current_frame_host()->GetRenderWidgetHost();
3475 if (host != render_widget_host)
Fady Samuel0b911822018-04-25 13:22:163476 host->SynchronizeVisualProperties();
kenrbe4f91082017-06-02 20:32:083477 }
3478 }
3479
[email protected]61e2b3cc2012-03-02 16:13:343480 if (delegate_)
3481 delegate_->ResizeDueToAutoResize(this, new_size);
3482}
3483
kenrbe4f91082017-06-02 20:32:083484gfx::Size WebContentsImpl::GetAutoResizeSize() {
3485 return auto_resize_size_;
3486}
3487
3488void WebContentsImpl::ResetAutoResizeSize() {
3489 auto_resize_size_ = gfx::Size();
3490}
3491
Albert J. Wongeb28e7e2018-10-12 00:57:433492InputEventShim* WebContentsImpl::GetInputEventShim() const {
3493 // The only thing that intercepts text input and mouse lock is the
3494 // BrowserPluginGuest. Delegate to the BrowserPluginGuest logic for
3495 // whether or not it needs to shim these events.
3496 if (browser_plugin_guest_) {
3497 return browser_plugin_guest_->GetInputEventShim();
3498 }
3499 return nullptr;
3500}
3501
[email protected]b172aee2012-04-10 17:05:263502WebContents* WebContentsImpl::OpenURL(const OpenURLParams& params) {
Aaron Colwell9dab1652019-12-09 18:29:493503#if DCHECK_IS_ON()
3504 DCHECK(params.Valid());
3505#endif
3506
Bo Liu300c6052018-06-12 04:46:073507 if (!delegate_) {
3508 // Embedder can delay setting a delegate on new WebContents with
3509 // WebContentsDelegate::ShouldResumeRequestsForCreatedWindow. In the mean
3510 // time, navigations, including the initial one, that goes through OpenURL
3511 // should be delayed until embedder is ready to resume loading.
3512 delayed_open_url_params_ = std::make_unique<OpenURLParams>(params);
Nasko Oskov83a8cf92018-10-19 14:58:563513
3514 // If there was a navigation deferred when creating the window through
3515 // CreateNewWindow, drop it in favor of this navigation.
3516 delayed_load_url_params_.reset();
3517
Ivan Kotenkov2c0d2bb32017-11-01 15:41:283518 return nullptr;
Bo Liu300c6052018-06-12 04:46:073519 }
[email protected]00c37fc2011-08-02 00:22:503520
[email protected]e5d549d2011-12-28 01:29:203521 WebContents* new_contents = delegate_->OpenURLFromTab(this, params);
pnoland488944652017-02-22 18:58:543522
3523 RenderFrameHost* source_render_frame_host = RenderFrameHost::FromID(
3524 params.source_render_process_id, params.source_render_frame_id);
3525
3526 if (source_render_frame_host && params.source_site_instance) {
3527 CHECK_EQ(source_render_frame_host->GetSiteInstance(),
3528 params.source_site_instance.get());
3529 }
3530
3531 if (new_contents && source_render_frame_host && new_contents != this) {
3532 for (auto& observer : observers_) {
Lukasz Anforowicz48c9f872018-08-01 00:58:413533 observer.DidOpenRequestedURL(
3534 new_contents, source_render_frame_host, params.url, params.referrer,
3535 params.disposition, params.transition,
3536 params.started_from_context_menu, params.is_renderer_initiated);
pnoland488944652017-02-22 18:58:543537 }
3538 }
3539
[email protected]e5d549d2011-12-28 01:29:203540 return new_contents;
[email protected]d5f942ba2008-09-26 19:30:343541}
3542
[email protected]a86c0e962013-12-17 17:10:393543void WebContentsImpl::RenderFrameForInterstitialPageCreated(
3544 RenderFrameHost* render_frame_host) {
ericwilligersde386342016-10-19 02:35:093545 for (auto& observer : observers_)
3546 observer.RenderFrameForInterstitialPageCreated(render_frame_host);
[email protected]ba45bfd2012-05-22 21:51:443547}
3548
[email protected]20ca0382013-02-28 19:50:073549void WebContentsImpl::AttachInterstitialPage(
3550 InterstitialPageImpl* interstitial_page) {
creisadca9862017-06-16 19:12:113551 DCHECK(!interstitial_page_ && interstitial_page);
3552 interstitial_page_ = interstitial_page;
[email protected]90fb08ed2013-09-24 17:43:293553
3554 // Cancel any visible dialogs so that they don't interfere with the
3555 // interstitial.
creis89a0f782015-05-27 16:13:173556 CancelActiveAndPendingDialogs();
[email protected]90fb08ed2013-09-24 17:43:293557
ericwilligersde386342016-10-19 02:35:093558 for (auto& observer : observers_)
3559 observer.DidAttachInterstitialPage();
clamy44e84ce2016-02-22 15:38:253560
3561 // Stop the throbber if needed while the interstitial page is shown.
3562 if (frame_tree_.IsLoading())
3563 LoadingStateChanged(true, true, nullptr);
lfg1453e412017-04-11 00:48:503564
3565 // Connect to outer WebContents if necessary.
3566 if (node_.OuterContentsFrameTreeNode()) {
3567 if (GetRenderManager()->GetProxyToOuterDelegate()) {
3568 DCHECK(
3569 static_cast<RenderWidgetHostViewBase*>(interstitial_page->GetView())
3570 ->IsRenderWidgetHostViewChildFrame());
3571 RenderWidgetHostViewChildFrame* view =
3572 static_cast<RenderWidgetHostViewChildFrame*>(
3573 interstitial_page->GetView());
3574 GetRenderManager()->SetRWHViewForInnerContents(view);
3575 }
3576 }
Bo Liu7c6779e92017-08-16 02:02:283577
Bo Liu168c8642017-08-28 18:26:023578#if defined(OS_ANDROID)
Bo Liu7c6779e92017-08-16 02:02:283579 // Update importance of the interstitial.
3580 static_cast<RenderFrameHostImpl*>(interstitial_page_->GetMainFrame())
3581 ->GetRenderWidgetHost()
3582 ->SetImportance(GetMainFrame()->GetRenderWidgetHost()->importance());
Bo Liu168c8642017-08-28 18:26:023583#endif
Dominic Mazzoniefb5f9e32017-12-21 01:08:223584
3585 if (accessibility_mode_.has_mode(ui::AXMode::kNativeAPIs)) {
3586 // Make sure that the main page's accessibility tree is hidden and the
3587 // interstitial gets focus.
3588 RenderFrameHostImpl* rfh =
3589 static_cast<RenderFrameHostImpl*>(GetMainFrame());
3590 if (rfh) {
3591 BrowserAccessibilityManager* accessibility_manager =
3592 rfh->browser_accessibility_manager();
3593 if (accessibility_manager)
3594 accessibility_manager->set_hidden_by_interstitial_page(true);
3595 }
3596 rfh = static_cast<RenderFrameHostImpl*>(
3597 GetInterstitialPage()->GetMainFrame());
3598 if (rfh) {
3599 BrowserAccessibilityManager* accessibility_manager =
3600 rfh->GetOrCreateBrowserAccessibilityManager();
3601 if (accessibility_manager)
3602 accessibility_manager->OnWindowFocused();
3603 }
3604 }
clamy44e84ce2016-02-22 15:38:253605}
3606
3607void WebContentsImpl::DidProceedOnInterstitial() {
3608 // The interstitial page should no longer be pausing the throbber.
creisadca9862017-06-16 19:12:113609 DCHECK(!(ShowingInterstitialPage() && interstitial_page_->pause_throbber()));
clamy44e84ce2016-02-22 15:38:253610
3611 // Restart the throbber now that the interstitial page no longer pauses it.
3612 if (ShowingInterstitialPage() && frame_tree_.IsLoading())
3613 LoadingStateChanged(true, true, nullptr);
[email protected]20ca0382013-02-28 19:50:073614}
3615
Shivani Sharma985474f2019-05-23 21:36:473616bool WebContentsImpl::HadInnerWebContents() {
3617 return had_inner_webcontents_;
3618}
3619
Lucas Furukawa Gadani7f1241d2017-07-12 01:45:043620void WebContentsImpl::DetachInterstitialPage(bool has_focus) {
Lucas Furukawa Gadani69b609192017-07-10 17:08:343621 bool interstitial_pausing_throbber =
3622 ShowingInterstitialPage() && interstitial_page_->pause_throbber();
3623 if (ShowingInterstitialPage())
3624 interstitial_page_ = nullptr;
Lucas Furukawa Gadani7f1241d2017-07-12 01:45:043625
Dominic Mazzoniefb5f9e32017-12-21 01:08:223626 // Make sure that the main page's accessibility tree is no longer
3627 // suppressed.
3628 RenderFrameHostImpl* rfh = GetMainFrame();
3629 if (rfh) {
3630 BrowserAccessibilityManager* accessibility_manager =
3631 rfh->browser_accessibility_manager();
3632 if (accessibility_manager)
3633 accessibility_manager->set_hidden_by_interstitial_page(false);
3634 }
3635
Lucas Furukawa Gadani7f1241d2017-07-12 01:45:043636 // If the focus was on the interstitial, let's keep it to the page.
3637 // (Note that in unit-tests the RVH may not have a view).
3638 if (has_focus && GetRenderViewHost()->GetWidget()->GetView())
3639 GetRenderViewHost()->GetWidget()->GetView()->Focus();
3640
Lucas Furukawa Gadani69b609192017-07-10 17:08:343641 for (auto& observer : observers_)
3642 observer.DidDetachInterstitialPage();
3643
3644 // Disconnect from outer WebContents if necessary. This must happen after the
3645 // interstitial page is cleared above, since the call to
3646 // SetRWHViewForInnerContents below may loop over all the
3647 // RenderWidgetHostViews in the tree (otherwise, including the now-deleted
3648 // view for the interstitial).
lfg1453e412017-04-11 00:48:503649 if (node_.OuterContentsFrameTreeNode()) {
3650 if (GetRenderManager()->GetProxyToOuterDelegate()) {
3651 DCHECK(static_cast<RenderWidgetHostViewBase*>(
3652 GetRenderManager()->current_frame_host()->GetView())
3653 ->IsRenderWidgetHostViewChildFrame());
3654 RenderWidgetHostViewChildFrame* view =
3655 static_cast<RenderWidgetHostViewChildFrame*>(
3656 GetRenderManager()->current_frame_host()->GetView());
3657 GetRenderManager()->SetRWHViewForInnerContents(view);
3658 }
3659 }
3660
clamy44e84ce2016-02-22 15:38:253661 // Restart the throbber if needed now that the interstitial page is going
3662 // away.
3663 if (interstitial_pausing_throbber && frame_tree_.IsLoading())
3664 LoadingStateChanged(true, true, nullptr);
[email protected]20ca0382013-02-28 19:50:073665}
3666
avi2b177592014-12-10 02:08:023667void WebContentsImpl::SetHistoryOffsetAndLength(int history_offset,
3668 int history_length) {
alexmos136fd6e62016-08-15 20:58:413669 SendPageMessage(new PageMsg_SetHistoryOffsetAndLength(
3670 MSG_ROUTING_NONE, history_offset, history_length));
avi2b177592014-12-10 02:08:023671}
3672
3673void WebContentsImpl::SetHistoryOffsetAndLengthForView(
3674 RenderViewHost* render_view_host,
3675 int history_offset,
3676 int history_length) {
alexmos136fd6e62016-08-15 20:58:413677 render_view_host->Send(new PageMsg_SetHistoryOffsetAndLength(
avi2b177592014-12-10 02:08:023678 render_view_host->GetRoutingID(), history_offset, history_length));
[email protected]796931a92011-08-10 01:32:143679}
3680
arthursonzogni818c2642019-09-27 12:18:103681void WebContentsImpl::ReloadFocusedFrame() {
[email protected]1f3fc1d2014-04-03 14:50:173682 RenderFrameHost* focused_frame = GetFocusedFrame();
3683 if (!focused_frame)
3684 return;
3685
yilkal921048bd2019-10-09 23:51:043686 focused_frame->Reload();
[email protected]959be4c2014-04-08 15:01:333687}
3688
Miyoung Shin298d5ad2019-09-05 01:52:373689std::vector<mojo::Remote<blink::mojom::PauseSubresourceLoadingHandle>>
Ryan Sturmc65dbe42018-05-31 20:37:043690WebContentsImpl::PauseSubresourceLoading() {
Miyoung Shin298d5ad2019-09-05 01:52:373691 std::vector<mojo::Remote<blink::mojom::PauseSubresourceLoadingHandle>>
3692 handles;
Ryan Sturmc65dbe42018-05-31 20:37:043693 for (RenderFrameHost* rfh : GetAllFrames()) {
3694 if (!rfh->IsRenderFrameLive())
3695 continue;
3696 handles.push_back(rfh->PauseSubresourceLoading());
3697 }
3698 return handles;
3699}
3700
[email protected]959be4c2014-04-08 15:01:333701void WebContentsImpl::Undo() {
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->Undo();
[email protected]959be4c2014-04-08 15:01:333707 RecordAction(base::UserMetricsAction("Undo"));
3708}
3709
3710void WebContentsImpl::Redo() {
Dave Tapuskae782bea2019-07-09 16:21:513711 auto* input_handler = GetFocusedFrameInputHandler();
3712 if (!input_handler)
[email protected]959be4c2014-04-08 15:01:333713 return;
Dave Tapuskae782bea2019-07-09 16:21:513714
3715 input_handler->Redo();
[email protected]959be4c2014-04-08 15:01:333716 RecordAction(base::UserMetricsAction("Redo"));
3717}
3718
3719void WebContentsImpl::Cut() {
Dave Tapuskae782bea2019-07-09 16:21:513720 auto* input_handler = GetFocusedFrameInputHandler();
3721 if (!input_handler)
[email protected]959be4c2014-04-08 15:01:333722 return;
3723
Dave Tapuskae782bea2019-07-09 16:21:513724 input_handler->Cut();
[email protected]959be4c2014-04-08 15:01:333725 RecordAction(base::UserMetricsAction("Cut"));
3726}
3727
3728void WebContentsImpl::Copy() {
Dave Tapuskae782bea2019-07-09 16:21:513729 auto* input_handler = GetFocusedFrameInputHandler();
3730 if (!input_handler)
[email protected]959be4c2014-04-08 15:01:333731 return;
3732
Dave Tapuskae782bea2019-07-09 16:21:513733 input_handler->Copy();
[email protected]959be4c2014-04-08 15:01:333734 RecordAction(base::UserMetricsAction("Copy"));
3735}
3736
3737void WebContentsImpl::CopyToFindPboard() {
3738#if defined(OS_MACOSX)
Dave Tapuskae782bea2019-07-09 16:21:513739 auto* input_handler = GetFocusedFrameInputHandler();
3740 if (!input_handler)
[email protected]959be4c2014-04-08 15:01:333741 return;
3742
3743 // Windows/Linux don't have the concept of a find pasteboard.
Dave Tapuskae782bea2019-07-09 16:21:513744 input_handler->CopyToFindPboard();
[email protected]959be4c2014-04-08 15:01:333745 RecordAction(base::UserMetricsAction("CopyToFindPboard"));
3746#endif
3747}
3748
3749void WebContentsImpl::Paste() {
Dave Tapuskae782bea2019-07-09 16:21:513750 auto* input_handler = GetFocusedFrameInputHandler();
3751 if (!input_handler)
[email protected]959be4c2014-04-08 15:01:333752 return;
3753
Dave Tapuskae782bea2019-07-09 16:21:513754 input_handler->Paste();
Archana Simhaf67090c2019-05-06 23:43:233755 for (auto& observer : observers_)
3756 observer.OnPaste();
[email protected]959be4c2014-04-08 15:01:333757 RecordAction(base::UserMetricsAction("Paste"));
3758}
3759
3760void WebContentsImpl::PasteAndMatchStyle() {
Dave Tapuskae782bea2019-07-09 16:21:513761 auto* input_handler = GetFocusedFrameInputHandler();
3762 if (!input_handler)
[email protected]959be4c2014-04-08 15:01:333763 return;
3764
Dave Tapuskae782bea2019-07-09 16:21:513765 input_handler->PasteAndMatchStyle();
Archana Simhaf67090c2019-05-06 23:43:233766 for (auto& observer : observers_)
3767 observer.OnPaste();
[email protected]959be4c2014-04-08 15:01:333768 RecordAction(base::UserMetricsAction("PasteAndMatchStyle"));
3769}
3770
3771void WebContentsImpl::Delete() {
Dave Tapuskae782bea2019-07-09 16:21:513772 auto* input_handler = GetFocusedFrameInputHandler();
3773 if (!input_handler)
[email protected]959be4c2014-04-08 15:01:333774 return;
3775
Dave Tapuskae782bea2019-07-09 16:21:513776 input_handler->Delete();
[email protected]959be4c2014-04-08 15:01:333777 RecordAction(base::UserMetricsAction("DeleteSelection"));
3778}
3779
3780void WebContentsImpl::SelectAll() {
Dave Tapuskae782bea2019-07-09 16:21:513781 auto* input_handler = GetFocusedFrameInputHandler();
3782 if (!input_handler)
[email protected]959be4c2014-04-08 15:01:333783 return;
3784
Dave Tapuskae782bea2019-07-09 16:21:513785 input_handler->SelectAll();
[email protected]959be4c2014-04-08 15:01:333786 RecordAction(base::UserMetricsAction("SelectAll"));
3787}
3788
yabinh351e7ec2017-03-10 02:43:243789void WebContentsImpl::CollapseSelection() {
Dave Tapuskae782bea2019-07-09 16:21:513790 auto* input_handler = GetFocusedFrameInputHandler();
3791 if (!input_handler)
[email protected]959be4c2014-04-08 15:01:333792 return;
3793
Dave Tapuskae782bea2019-07-09 16:21:513794 input_handler->CollapseSelection();
[email protected]1f3fc1d2014-04-03 14:50:173795}
3796
3797void WebContentsImpl::Replace(const base::string16& word) {
Dave Tapuskae782bea2019-07-09 16:21:513798 auto* input_handler = GetFocusedFrameInputHandler();
3799 if (!input_handler)
[email protected]1f3fc1d2014-04-03 14:50:173800 return;
3801
Dave Tapuskae782bea2019-07-09 16:21:513802 input_handler->Replace(word);
[email protected]1f3fc1d2014-04-03 14:50:173803}
3804
3805void WebContentsImpl::ReplaceMisspelling(const base::string16& word) {
Dave Tapuskae782bea2019-07-09 16:21:513806 auto* input_handler = GetFocusedFrameInputHandler();
3807 if (!input_handler)
[email protected]1f3fc1d2014-04-03 14:50:173808 return;
3809
Dave Tapuskae782bea2019-07-09 16:21:513810 input_handler->ReplaceMisspelling(word);
[email protected]959be4c2014-04-08 15:01:333811}
3812
3813void WebContentsImpl::NotifyContextMenuClosed(
3814 const CustomContextMenuContext& context) {
3815 RenderFrameHost* focused_frame = GetFocusedFrame();
3816 if (!focused_frame)
3817 return;
3818
3819 focused_frame->Send(new FrameMsg_ContextMenuClosed(
3820 focused_frame->GetRoutingID(), context));
3821}
3822
3823void WebContentsImpl::ExecuteCustomContextMenuCommand(
3824 int action, const CustomContextMenuContext& context) {
3825 RenderFrameHost* focused_frame = GetFocusedFrame();
3826 if (!focused_frame)
3827 return;
3828
3829 focused_frame->Send(new FrameMsg_CustomContextMenuAction(
3830 focused_frame->GetRoutingID(), context, action));
[email protected]4fed3702014-04-01 09:08:003831}
3832
[email protected]fc2b46b2014-05-03 16:33:453833gfx::NativeView WebContentsImpl::GetNativeView() {
3834 return view_->GetNativeView();
3835}
3836
3837gfx::NativeView WebContentsImpl::GetContentNativeView() {
3838 return view_->GetContentNativeView();
3839}
3840
3841gfx::NativeWindow WebContentsImpl::GetTopLevelNativeWindow() {
3842 return view_->GetTopLevelNativeWindow();
3843}
3844
3845gfx::Rect WebContentsImpl::GetViewBounds() {
3846 return view_->GetViewBounds();
3847}
3848
3849gfx::Rect WebContentsImpl::GetContainerBounds() {
3850 gfx::Rect rv;
3851 view_->GetContainerBounds(&rv);
3852 return rv;
3853}
3854
3855DropData* WebContentsImpl::GetDropData() {
3856 return view_->GetDropData();
3857}
3858
3859void WebContentsImpl::Focus() {
miu9e14e492014-10-25 02:39:043860 view_->Focus();
[email protected]fc2b46b2014-05-03 16:33:453861}
3862
3863void WebContentsImpl::SetInitialFocus() {
miu9e14e492014-10-25 02:39:043864 view_->SetInitialFocus();
[email protected]fc2b46b2014-05-03 16:33:453865}
3866
3867void WebContentsImpl::StoreFocus() {
miu9e14e492014-10-25 02:39:043868 view_->StoreFocus();
[email protected]fc2b46b2014-05-03 16:33:453869}
3870
3871void WebContentsImpl::RestoreFocus() {
miu9e14e492014-10-25 02:39:043872 view_->RestoreFocus();
[email protected]fc2b46b2014-05-03 16:33:453873}
3874
[email protected]b172aee2012-04-10 17:05:263875void WebContentsImpl::FocusThroughTabTraversal(bool reverse) {
Maxim Podgorodskiya926c4ff2017-11-20 02:06:393876 view_->FocusThroughTabTraversal(reverse);
[email protected]96d185d2009-04-24 03:28:543877}
3878
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:413879bool WebContentsImpl::ShowingInterstitialPage() {
creisadca9862017-06-16 19:12:113880 return interstitial_page_ != nullptr;
[email protected]96d185d2009-04-24 03:28:543881}
3882
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:413883InterstitialPageImpl* WebContentsImpl::GetInterstitialPage() {
creisadca9862017-06-16 19:12:113884 return interstitial_page_;
[email protected]686493142011-07-15 21:47:223885}
3886
[email protected]b172aee2012-04-10 17:05:263887bool WebContentsImpl::IsSavable() {
[email protected]a53209b2012-01-20 16:48:163888 // WebKit creates Document object when MIME type is application/xhtml+xml,
3889 // so we also support this MIME type.
3890 return contents_mime_type_ == "text/html" ||
3891 contents_mime_type_ == "text/xml" ||
3892 contents_mime_type_ == "application/xhtml+xml" ||
3893 contents_mime_type_ == "text/plain" ||
3894 contents_mime_type_ == "text/css" ||
Kinuko Yasuda74702f92017-07-31 03:27:533895 blink::IsSupportedJavascriptMimeType(contents_mime_type_);
[email protected]a53209b2012-01-20 16:48:163896}
3897
[email protected]b172aee2012-04-10 17:05:263898void WebContentsImpl::OnSavePage() {
[email protected]c7dd2f62011-07-18 15:57:593899 // If we can not save the page, try to download it.
[email protected]a53209b2012-01-20 16:48:163900 if (!IsSavable()) {
Min Qinda0ed2062018-02-23 22:00:533901 download::RecordSavePackageEvent(
3902 download::SAVE_PACKAGE_DOWNLOAD_ON_NON_HTML);
nasko89ad7742015-03-05 22:14:193903 SaveFrame(GetLastCommittedURL(), Referrer());
[email protected]27678b2a2012-02-04 22:09:143904 return;
[email protected]c7dd2f62011-07-18 15:57:593905 }
3906
3907 Stop();
3908
3909 // Create the save package and possibly prompt the user for the name to save
3910 // the page as. The user prompt is an asynchronous operation that runs on
3911 // another thread.
3912 save_package_ = new SavePackage(this);
3913 save_package_->GetSaveInfo();
3914}
3915
3916// Used in automated testing to bypass prompting the user for file names.
3917// Instead, the names and paths are hard coded rather than running them through
3918// file name sanitation and extension / mime checking.
[email protected]2dec8ec2013-02-07 19:20:343919bool WebContentsImpl::SavePage(const base::FilePath& main_file,
3920 const base::FilePath& dir_path,
[email protected]8ff00d72012-10-23 19:12:213921 SavePageType save_type) {
[email protected]c7dd2f62011-07-18 15:57:593922 // Stop the page from navigating.
3923 Stop();
3924
3925 save_package_ = new SavePackage(this, save_type, main_file, dir_path);
[email protected]8ff00d72012-10-23 19:12:213926 return save_package_->Init(SavePackageDownloadCreatedCallback());
[email protected]c7dd2f62011-07-18 15:57:593927}
3928
[email protected]3c71576ce2013-07-23 02:00:013929void WebContentsImpl::SaveFrame(const GURL& url,
3930 const Referrer& referrer) {
Xing Liu10329bf2018-03-20 19:22:143931 SaveFrameWithHeaders(url, referrer, std::string(), base::string16());
kundaji6c7f9692015-03-09 18:00:373932}
3933
Xing Liu10329bf2018-03-20 19:22:143934void WebContentsImpl::SaveFrameWithHeaders(
3935 const GURL& url,
3936 const Referrer& referrer,
3937 const std::string& headers,
3938 const base::string16& suggested_filename) {
Lei Zhang13a42e42019-02-19 23:28:113939 // Check and see if the guest can handle this.
3940 if (delegate_) {
3941 WebContents* guest_web_contents = nullptr;
3942 if (browser_plugin_embedder_) {
3943 BrowserPluginGuest* guest = browser_plugin_embedder_->GetFullPageGuest();
3944 if (guest)
3945 guest_web_contents = guest->GetWebContents();
3946 } else if (browser_plugin_guest_) {
3947 guest_web_contents = this;
3948 }
3949
3950 if (guest_web_contents && delegate_->GuestSaveFrame(guest_web_contents))
3951 return;
3952 }
3953
nasko89ad7742015-03-05 22:14:193954 if (!GetLastCommittedURL().is_valid())
[email protected]3c71576ce2013-07-23 02:00:013955 return;
sammc92af61552014-11-19 23:27:403956 if (delegate_ && delegate_->SaveFrame(url, referrer))
3957 return;
3958
nasko89ad7742015-03-05 22:14:193959 // TODO(nasko): This check for main frame is incorrect and should be fixed
brettw760f7442016-05-23 21:19:223960 // by explicitly passing in which frame this method should target. This would
3961 // indicate whether it's the main frame, and also tell us the frame pointer
3962 // to use for routing.
nasko89ad7742015-03-05 22:14:193963 bool is_main_frame = (url == GetLastCommittedURL());
brettw760f7442016-05-23 21:19:223964 RenderFrameHost* frame_host = GetMainFrame();
[email protected]3c71576ce2013-07-23 02:00:013965
avib7348942015-12-25 20:57:103966 int64_t post_id = -1;
[email protected]3c71576ce2013-07-23 02:00:013967 if (is_main_frame) {
Lucas Furukawa Gadani5553a152019-01-08 18:55:573968 NavigationEntry* entry = controller_.GetLastCommittedEntry();
[email protected]3c71576ce2013-07-23 02:00:013969 if (entry)
3970 post_id = entry->GetPostID();
3971 }
Ramin Halavati24e21012017-07-10 12:56:063972 net::NetworkTrafficAnnotationTag traffic_annotation =
3973 net::DefineNetworkTrafficAnnotation("download_web_contents_frame", R"(
3974 semantics {
3975 sender: "Save Page Action"
3976 description:
3977 "Saves the given frame's URL to the local file system."
3978 trigger:
3979 "The user has triggered a save operation on the frame through a "
3980 "context menu or other mechanism."
3981 data: "None."
3982 destination: WEBSITE
3983 }
3984 policy {
Ramin Halavati3b979782017-07-21 11:40:263985 cookies_allowed: YES
Ramin Halavati24e21012017-07-10 12:56:063986 cookies_store: "user"
3987 setting:
3988 "This feature cannot be disabled by settings, but it's is only "
3989 "triggered by user request."
3990 policy_exception_justification: "Not implemented."
3991 })");
Min Qina904f3302018-02-13 23:33:343992 auto params = std::make_unique<download::DownloadUrlParameters>(
brettw760f7442016-05-23 21:19:223993 url, frame_host->GetProcess()->GetID(),
3994 frame_host->GetRenderViewHost()->GetRoutingID(),
Yao Xiao65d20b82019-12-05 22:04:203995 frame_host->GetRoutingID(), traffic_annotation,
3996 frame_host->GetNetworkIsolationKey());
Min Qina904f3302018-02-13 23:33:343997 params->set_referrer(referrer.url);
3998 params->set_referrer_policy(
3999 Referrer::ReferrerPolicyForUrlRequest(referrer.policy));
[email protected]3c71576ce2013-07-23 02:00:014000 params->set_post_id(post_id);
[email protected]3c71576ce2013-07-23 02:00:014001 if (post_id >= 0)
4002 params->set_method("POST");
4003 params->set_prompt(true);
kundaji6c7f9692015-03-09 18:00:374004
4005 if (headers.empty()) {
4006 params->set_prefer_cache(true);
4007 } else {
Min Qina904f3302018-02-13 23:33:344008 for (download::DownloadUrlParameters::RequestHeadersNameValuePair
4009 key_value : ParseDownloadHeaders(headers)) {
Megan Jablonski2f6a4c52017-07-10 23:01:254010 params->add_request_header(key_value.first, key_value.second);
kundaji6c7f9692015-03-09 18:00:374011 }
4012 }
Xing Liu10329bf2018-03-20 19:22:144013 params->set_suggested_name(suggested_filename);
Min Qin0ca8e1ee2018-01-31 00:49:354014 params->set_download_source(download::DownloadSource::WEB_CONTENTS_API);
thestig859c78892017-05-15 21:17:064015 BrowserContext::GetDownloadManager(GetBrowserContext())
Ramin Halavati03efa732017-06-12 09:28:204016 ->DownloadUrl(std::move(params));
[email protected]3c71576ce2013-07-23 02:00:014017}
4018
[email protected]b172aee2012-04-10 17:05:264019void WebContentsImpl::GenerateMHTML(
dewittj6dc5747a2016-05-17 01:48:474020 const MHTMLGenerationParams& params,
Avi Drissman149b7832018-03-23 14:31:494021 base::OnceCallback<void(int64_t)> callback) {
Angel Alvarez902496682019-08-27 22:58:424022 base::OnceCallback<void(const MHTMLGenerationResult&)> wrapper_callback =
4023 base::BindOnce(
4024 [](base::OnceCallback<void(int64_t)> size_callback,
4025 const MHTMLGenerationResult& result) {
4026 std::move(size_callback).Run(result.file_size);
4027 },
4028 std::move(callback));
4029 MHTMLGenerationManager::GetInstance()->SaveMHTML(this, params,
4030 std::move(wrapper_callback));
4031}
4032
4033void WebContentsImpl::GenerateMHTMLWithResult(
4034 const MHTMLGenerationParams& params,
4035 MHTMLGenerationResult::GenerateMHTMLCallback callback) {
Avi Drissman149b7832018-03-23 14:31:494036 MHTMLGenerationManager::GetInstance()->SaveMHTML(this, params,
4037 std::move(callback));
[email protected]aa4f3972012-03-01 18:12:124038}
4039
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:414040const std::string& WebContentsImpl::GetContentsMimeType() {
[email protected]0bfbf882011-12-22 18:19:274041 return contents_mime_type_;
4042}
4043
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:414044bool WebContentsImpl::WillNotifyDisconnection() {
[email protected]0bfbf882011-12-22 18:19:274045 return notify_disconnection_;
4046}
4047
Leon Hanc819dc62019-01-28 04:30:194048blink::mojom::RendererPreferences* WebContentsImpl::GetMutableRendererPrefs() {
[email protected]0bfbf882011-12-22 18:19:274049 return &renderer_preferences_;
4050}
4051
[email protected]e35ccd52012-05-23 16:22:474052void WebContentsImpl::Close() {
4053 Close(GetRenderViewHost());
4054}
4055
Ella Ge80a52dce2017-11-15 18:01:524056void WebContentsImpl::DragSourceEndedAt(float client_x,
4057 float client_y,
4058 float screen_x,
4059 float screen_y,
Paul Meyer0c58c3712016-11-17 22:59:514060 blink::WebDragOperation operation,
4061 RenderWidgetHost* source_rwh) {
Lei Zhang6410ec1a2019-01-31 21:38:584062 if (browser_plugin_embedder_) {
W. James MacLean9437b5bf2019-12-06 17:33:444063 browser_plugin_embedder_->DragSourceEndedAt(client_x, client_y, screen_x,
4064 screen_y, operation);
Lei Zhang6410ec1a2019-01-31 21:38:584065 }
Paul Meyer0c58c3712016-11-17 22:59:514066 if (source_rwh) {
Ella Ge80a52dce2017-11-15 18:01:524067 source_rwh->DragSourceEndedAt(gfx::PointF(client_x, client_y),
4068 gfx::PointF(screen_x, screen_y), operation);
Paul Meyer0c58c3712016-11-17 22:59:514069 }
[email protected]cf200a562013-05-03 16:24:294070}
4071
jam73f892642016-09-11 06:04:064072void WebContentsImpl::LoadStateChanged(
Charles Harrison911fa062018-03-06 21:01:464073 const std::string& host,
jam73f892642016-09-11 06:04:064074 const net::LoadStateWithParam& load_state,
4075 uint64_t upload_position,
4076 uint64_t upload_size) {
Charles Harrison911fa062018-03-06 21:01:464077 base::string16 host16 = url_formatter::IDNToUnicode(host);
Elly Fong-Jonescad9c3d12018-01-31 17:26:104078 // Drop no-op updates.
4079 if (load_state_.state == load_state.state &&
4080 load_state_.param == load_state.param &&
4081 upload_position_ == upload_position && upload_size_ == upload_size &&
Charles Harrison911fa062018-03-06 21:01:464082 load_state_host_ == host16) {
Elly Fong-Jonescad9c3d12018-01-31 17:26:104083 return;
4084 }
jam73f892642016-09-11 06:04:064085 load_state_ = load_state;
4086 upload_position_ = upload_position;
4087 upload_size_ = upload_size;
Charles Harrison911fa062018-03-06 21:01:464088 load_state_host_ = host16;
jam73f892642016-09-11 06:04:064089 if (load_state_.state == net::LOAD_STATE_READING_RESPONSE)
4090 SetNotWaitingForResponse();
4091 if (IsLoading()) {
4092 NotifyNavigationStateChanged(static_cast<InvalidateTypes>(
4093 INVALIDATE_TYPE_LOAD | INVALIDATE_TYPE_TAB));
4094 }
4095}
4096
Collin Baker989e0882019-11-01 01:27:174097void WebContentsImpl::SetVisibilityAndNotifyObservers(Visibility visibility) {
Francois Doraye6161152018-03-27 22:05:374098 const Visibility previous_visibility = visibility_;
4099 visibility_ = visibility;
4100
Francois Dorayfe4a1772018-02-17 04:17:094101 // Notify observers if the visibility changed or if WasShown() is being called
4102 // for the first time.
Francois Dorayfe4a1772018-02-17 04:17:094103 if (visibility != previous_visibility ||
4104 (visibility == Visibility::VISIBLE && !did_first_set_visible_)) {
4105 for (auto& observer : observers_)
4106 observer.OnVisibilityChanged(visibility);
4107 }
4108}
4109
Michael Thiessen896405db2017-07-20 17:52:324110void WebContentsImpl::NotifyWebContentsFocused(
4111 RenderWidgetHost* render_widget_host) {
ericwilligersde386342016-10-19 02:35:094112 for (auto& observer : observers_)
Michael Thiessen896405db2017-07-20 17:52:324113 observer.OnWebContentsFocused(render_widget_host);
calamity7fe55ce2015-04-10 03:59:374114}
4115
Michael Thiessen896405db2017-07-20 17:52:324116void WebContentsImpl::NotifyWebContentsLostFocus(
4117 RenderWidgetHost* render_widget_host) {
zijiehe3bee08e22017-05-17 04:14:464118 for (auto& observer : observers_)
Michael Thiessen896405db2017-07-20 17:52:324119 observer.OnWebContentsLostFocus(render_widget_host);
zijiehe3bee08e22017-05-17 04:14:464120}
4121
Paul Meyer0c58c3712016-11-17 22:59:514122void WebContentsImpl::SystemDragEnded(RenderWidgetHost* source_rwh) {
4123 if (source_rwh)
4124 source_rwh->DragSourceSystemDragEnded();
Lei Zhang6410ec1a2019-01-31 21:38:584125 if (browser_plugin_embedder_)
[email protected]cf200a562013-05-03 16:24:294126 browser_plugin_embedder_->SystemDragEnded();
[email protected]7813bd72011-02-05 02:19:344127}
4128
[email protected]b172aee2012-04-10 17:05:264129void WebContentsImpl::SetClosedByUserGesture(bool value) {
[email protected]0bfbf882011-12-22 18:19:274130 closed_by_user_gesture_ = value;
4131}
4132
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:414133bool WebContentsImpl::GetClosedByUserGesture() {
[email protected]0bfbf882011-12-22 18:19:274134 return closed_by_user_gesture_;
4135}
4136
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:414137int WebContentsImpl::GetMinimumZoomPercent() {
[email protected]0bfbf882011-12-22 18:19:274138 return minimum_zoom_percent_;
4139}
4140
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:414141int WebContentsImpl::GetMaximumZoomPercent() {
[email protected]0bfbf882011-12-22 18:19:274142 return maximum_zoom_percent_;
4143}
4144
mcnee432e47d2015-11-09 19:37:464145void WebContentsImpl::SetPageScale(float page_scale_factor) {
Lukasz Anforowiczf4357ca2017-09-07 01:43:324146 GetRenderViewHost()->Send(new ViewMsg_SetPageScale(
4147 GetRenderViewHost()->GetRoutingID(), page_scale_factor));
ccameronb7c1d6c2015-03-09 17:08:244148}
4149
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:414150gfx::Size WebContentsImpl::GetPreferredSize() {
Francois Doray24fc62c2017-12-11 17:27:334151 return IsBeingCaptured() ? preferred_size_for_capture_ : preferred_size_;
[email protected]bcd2815602012-01-14 18:17:234152}
4153
[email protected]b172aee2012-04-10 17:05:264154bool WebContentsImpl::GotResponseToLockMouseRequest(bool allowed) {
lfgf0cd46e2017-01-04 00:05:234155 if (!GuestMode::IsCrossProcessFrameGuest(GetWebContents()) &&
4156 GetBrowserPluginGuest())
[email protected]660f18e2014-05-15 20:53:054157 return GetBrowserPluginGuest()->LockMouse(allowed);
4158
lfgf0cd46e2017-01-04 00:05:234159 if (mouse_lock_widget_) {
4160 if (mouse_lock_widget_->delegate()->GetAsWebContents() != this) {
4161 return mouse_lock_widget_->delegate()
4162 ->GetAsWebContents()
4163 ->GotResponseToLockMouseRequest(allowed);
4164 }
lfgad8244352016-07-13 16:55:514165
lfgf0cd46e2017-01-04 00:05:234166 if (mouse_lock_widget_->GotResponseToLockMouseRequest(allowed))
4167 return true;
4168 }
4169
4170 for (WebContentsImpl* current = this; current;
4171 current = current->GetOuterWebContents()) {
4172 current->mouse_lock_widget_ = nullptr;
4173 }
4174
lfgbee1e0a2016-06-08 21:24:214175 return false;
[email protected]0bfbf882011-12-22 18:19:274176}
4177
Joe Downing13dd76b2018-04-09 18:32:154178bool WebContentsImpl::GotResponseToKeyboardLockRequest(bool allowed) {
4179 if (!keyboard_lock_widget_)
4180 return false;
4181
4182 if (keyboard_lock_widget_->delegate()->GetAsWebContents() != this) {
4183 NOTREACHED();
4184 return false;
4185 }
4186
4187 // KeyboardLock is only supported when called by the top-level browsing
4188 // context and is not supported in embedded content scenarios.
4189 if (GetOuterWebContents())
4190 return false;
4191
4192 keyboard_lock_widget_->GotResponseToKeyboardLockRequest(allowed);
4193 return true;
4194}
4195
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:414196bool WebContentsImpl::HasOpener() {
Ivan Kotenkov2c0d2bb32017-11-01 15:41:284197 return GetOpener() != nullptr;
[email protected]a0358d72012-03-09 14:06:504198}
4199
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:414200RenderFrameHostImpl* WebContentsImpl::GetOpener() {
alexmose201c7cd2015-06-10 17:14:214201 FrameTreeNode* opener_ftn = frame_tree_.root()->opener();
lukasza6f8ac622017-06-06 03:10:204202 return opener_ftn ? opener_ftn->current_frame_host() : nullptr;
jochen55ff3502014-12-18 20:52:574203}
4204
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:414205bool WebContentsImpl::HasOriginalOpener() {
Ivan Kotenkov2c0d2bb32017-11-01 15:41:284206 return GetOriginalOpener() != nullptr;
jochen6004a362017-02-04 00:11:404207}
4208
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:414209RenderFrameHostImpl* WebContentsImpl::GetOriginalOpener() {
jochen6004a362017-02-04 00:11:404210 FrameTreeNode* opener_ftn = frame_tree_.root()->original_opener();
lukasza6f8ac622017-06-06 03:10:204211 return opener_ftn ? opener_ftn->current_frame_host() : nullptr;
jochen6004a362017-02-04 00:11:404212}
4213
[email protected]cb805452013-05-22 15:16:214214void WebContentsImpl::DidChooseColorInColorChooser(SkColor color) {
Joel Hockey85b379d2017-12-11 10:42:134215 color_chooser_->DidChooseColorInColorChooser(color);
[email protected]da8543762012-03-20 08:52:204216}
4217
[email protected]cb805452013-05-22 15:16:214218void WebContentsImpl::DidEndColorChooser() {
Joel Hockey85b379d2017-12-11 10:42:134219 color_chooser_.reset();
[email protected]da8543762012-03-20 08:52:204220}
4221
halton.huoca2eabd2015-07-06 08:17:404222int WebContentsImpl::DownloadImage(
4223 const GURL& url,
4224 bool is_favicon,
Fredrik Söderquistb2b39eb92019-11-18 16:16:504225 uint32_t preferred_size,
halton.huoca2eabd2015-07-06 08:17:404226 uint32_t max_bitmap_size,
4227 bool bypass_cache,
Avi Drissmana5a52dd2018-03-27 03:39:024228 WebContents::ImageDownloadCallback callback) {
amistry9f01b772015-07-29 01:54:554229 DCHECK_CURRENTLY_ON(BrowserThread::UI);
halton.huoca2eabd2015-07-06 08:17:404230 static int next_image_download_id = 0;
Julie Jeongeun Kimb1473572019-08-15 05:59:134231 const mojo::Remote<blink::mojom::ImageDownloader>& mojo_image_downloader =
halton.huoca2eabd2015-07-06 08:17:404232 GetMainFrame()->GetMojoImageDownloader();
amistry9f01b772015-07-29 01:54:554233 const int download_id = ++next_image_download_id;
4234 if (!mojo_image_downloader) {
4235 // If the renderer process is dead (i.e. crash, or memory pressure on
4236 // Android), the downloader service will be invalid. Pre-Mojo, this would
Avi Drissmana5a52dd2018-03-27 03:39:024237 // hang the callback indefinitely since the IPC would be dropped. Now,
amistry9f01b772015-07-29 01:54:554238 // respond with a 400 HTTP error code to indicate that something went wrong.
Sami Kyostila8e4d5a92019-08-02 12:45:054239 base::PostTask(
Eric Seckler8652dcd52018-09-20 10:42:284240 FROM_HERE, {BrowserThread::UI},
tzike2aca992017-09-05 08:50:544241 base::BindOnce(&WebContentsImpl::OnDidDownloadImage,
Avi Drissmana5a52dd2018-03-27 03:39:024242 weak_factory_.GetWeakPtr(), std::move(callback),
4243 download_id, url, 400, std::vector<SkBitmap>(),
4244 std::vector<gfx::Size>()));
amistry9f01b772015-07-29 01:54:554245 return download_id;
4246 }
4247
halton.huoca2eabd2015-07-06 08:17:404248 mojo_image_downloader->DownloadImage(
Fredrik Söderquistb2b39eb92019-11-18 16:16:504249 url, is_favicon, preferred_size, max_bitmap_size, bypass_cache,
tzike2aca992017-09-05 08:50:544250 base::BindOnce(&WebContentsImpl::OnDidDownloadImage,
Avi Drissmana5a52dd2018-03-27 03:39:024251 weak_factory_.GetWeakPtr(), std::move(callback),
4252 download_id, url));
amistry9f01b772015-07-29 01:54:554253 return download_id;
[email protected]795c28972012-12-06 06:13:394254}
4255
[email protected]36ec24f2014-01-09 00:32:084256void WebContentsImpl::Find(int request_id,
4257 const base::string16& search_text,
Rakina Zata Amni3f77dff2018-09-08 16:19:434258 blink::mojom::FindOptionsPtr options) {
thestig6057a6b22015-11-12 23:01:334259 // Cowardly refuse to search for no text.
4260 if (search_text.empty()) {
4261 NOTREACHED();
4262 return;
4263 }
4264
Rakina Zata Amni3f77dff2018-09-08 16:19:434265 GetOrCreateFindRequestManager()->Find(request_id, search_text,
4266 std::move(options));
[email protected]36ec24f2014-01-09 00:32:084267}
4268
4269void WebContentsImpl::StopFinding(StopFindAction action) {
paulmeyerfeafc2d2017-04-25 21:46:404270 if (FindRequestManager* manager = GetFindRequestManager())
4271 manager->StopFinding(action);
[email protected]36ec24f2014-01-09 00:32:084272}
4273
Tommy Steimel18360512017-11-01 00:38:194274bool WebContentsImpl::WasEverAudible() {
4275 return was_ever_audible_;
4276}
4277
Avi Drissmana5a52dd2018-03-27 03:39:024278void WebContentsImpl::GetManifest(GetManifestCallback callback) {
4279 manifest_manager_host_->GetManifest(std::move(callback));
mlamouriefdca9d2014-09-16 16:55:404280}
4281
bokanece34a82016-01-28 19:49:464282void WebContentsImpl::ExitFullscreen(bool will_cause_resize) {
mlamouri7a78d6fd2015-01-17 13:23:534283 // Clean up related state and initiate the fullscreen exit.
avi3627ecac2015-10-16 17:40:434284 GetRenderViewHost()->GetWidget()->RejectMouseLockOrUnlockIfNecessary();
bokanece34a82016-01-28 19:49:464285 ExitFullscreenMode(will_cause_resize);
mlamouri7a78d6fd2015-01-17 13:23:534286}
4287
Avi Drissmanced52b62019-08-14 21:25:464288void WebContentsImpl::ForSecurityDropFullscreen() {
4289 // There are two chains of WebContents to kick out of fullscreen.
4290 //
4291 // Chain 1, the inner/outer WebContents chain. If an inner WebContents has
4292 // done something that requires the browser to drop fullscreen, drop
4293 // fullscreen from it and any outer WebContents that may be in fullscreen.
4294 //
4295 // Chain 2, the opener WebContents chain. If a WebContents has done something
4296 // that requires the browser to drop fullscreen, drop fullscreen from any
4297 // WebContents that was involved in the chain of opening it.
4298 //
4299 // Note that these two chains don't interact, as only a top-level WebContents
4300 // can have an opener. This simplifies things.
4301
4302 WebContents* web_contents = this;
4303 while (web_contents) {
4304 if (web_contents->IsFullscreenForCurrentTab())
4305 web_contents->ExitFullscreen(true);
4306
4307 if (web_contents->HasOriginalOpener())
4308 web_contents = FromRenderFrameHost(web_contents->GetOriginalOpener());
4309 else
4310 web_contents = web_contents->GetOuterWebContents();
4311 }
4312}
4313
mariakhomenko44bdc4732015-04-29 01:55:384314void WebContentsImpl::ResumeLoadingCreatedWebContents() {
Nasko Oskov83a8cf92018-10-19 14:58:564315 if (delayed_load_url_params_.get()) {
4316 DCHECK(!delayed_open_url_params_);
4317 controller_.LoadURLWithParams(*delayed_load_url_params_.get());
4318 delayed_load_url_params_.reset(nullptr);
4319 return;
4320 }
4321
mariakhomenkoa4971c12015-07-21 19:04:374322 if (delayed_open_url_params_.get()) {
4323 OpenURL(*delayed_open_url_params_.get());
4324 delayed_open_url_params_.reset(nullptr);
4325 return;
4326 }
4327
mariakhomenko44bdc4732015-04-29 01:55:384328 // Resume blocked requests for both the RenderViewHost and RenderFrameHost.
4329 // TODO(brettw): It seems bogus to reach into here and initialize the host.
dfalcantarae6b7b46752015-07-10 18:14:164330 if (is_resume_pending_) {
4331 is_resume_pending_ = false;
avi0f1bbc9712015-11-17 02:58:134332 GetRenderViewHost()->GetWidget()->Init();
dfalcantarae6b7b46752015-07-10 18:14:164333 GetMainFrame()->Init();
4334 }
mariakhomenko44bdc4732015-04-29 01:55:384335}
4336
[email protected]b172aee2012-04-10 17:05:264337bool WebContentsImpl::FocusLocationBarByDefault() {
Xiyuan Xia2a66b112018-12-06 15:52:164338 if (should_focus_location_bar_by_default_)
[email protected]0c9406632013-02-08 01:13:334339 return true;
Xiyuan Xia2a66b112018-12-06 15:52:164340
[email protected]0c9406632013-02-08 01:13:334341 return delegate_ && delegate_->ShouldFocusLocationBarByDefault(this);
[email protected]0bfbf882011-12-22 18:19:274342}
4343
Tommy C. Li8bb5bf42019-01-18 18:45:514344void WebContentsImpl::SetFocusToLocationBar() {
[email protected]0bfbf882011-12-22 18:19:274345 if (delegate_)
Tommy C. Li8bb5bf42019-01-18 18:45:514346 delegate_->SetFocusToLocationBar();
[email protected]c40d6232011-03-25 00:16:214347}
4348
clamy0e119882015-07-31 16:12:334349void WebContentsImpl::DidStartNavigation(NavigationHandle* navigation_handle) {
Charles Harrison53096ba2017-12-15 15:39:484350 TRACE_EVENT1("navigation", "WebContentsImpl::DidStartNavigation",
4351 "navigation_handle", navigation_handle);
ericwilligersde386342016-10-19 02:35:094352 for (auto& observer : observers_)
4353 observer.DidStartNavigation(navigation_handle);
Becca Hughesd11d6502018-07-31 17:01:284354
4355 if (display_cutout_host_impl_)
4356 display_cutout_host_impl_->DidStartNavigation(navigation_handle);
Xiyuan Xia2a66b112018-12-06 15:52:164357
4358 if (navigation_handle->IsInMainFrame()) {
4359 // When the browser is started with about:blank as the startup URL, focus
4360 // the location bar (which will also select its contents) so people can
4361 // simply begin typing to navigate elsewhere.
4362 //
4363 // We need to be careful not to trigger this for anything other than the
4364 // startup navigation. In particular, if we allow an attacker to open a
4365 // popup to about:blank, then navigate, focusing the Omnibox will cause the
4366 // end of the new URL to be scrolled into view instead of the start,
4367 // allowing the attacker to spoof other URLs. The conditions checked here
4368 // are all aimed at ensuring no such attacker-controlled navigation can
4369 // trigger this.
4370 should_focus_location_bar_by_default_ =
4371 controller_.IsInitialNavigation() &&
4372 !navigation_handle->IsRendererInitiated() &&
4373 navigation_handle->GetURL() == url::kAboutBlankURL;
4374 }
clamy0e119882015-07-31 16:12:334375}
4376
4377void WebContentsImpl::DidRedirectNavigation(
4378 NavigationHandle* navigation_handle) {
Charles Harrison53096ba2017-12-15 15:39:484379 TRACE_EVENT1("navigation", "WebContentsImpl::DidRedirectNavigation",
4380 "navigation_handle", navigation_handle);
ericwilligersde386342016-10-19 02:35:094381 for (auto& observer : observers_)
4382 observer.DidRedirectNavigation(navigation_handle);
jam5f358e52017-02-13 16:37:034383
4384 // Notify accessibility if this is a reload. This has to called on the
4385 // BrowserAccessibilityManager associated with the old RFHI.
4386 if (navigation_handle->GetReloadType() != ReloadType::NONE) {
Mohamed Abdelhalim95794962019-09-20 11:16:494387 NavigationRequest* request = NavigationRequest::From(navigation_handle);
jam5f358e52017-02-13 16:37:034388 BrowserAccessibilityManager* manager =
Mohamed Abdelhalim95794962019-09-20 11:16:494389 request->frame_tree_node()
jam5f358e52017-02-13 16:37:034390 ->current_frame_host()
4391 ->browser_accessibility_manager();
4392 if (manager)
4393 manager->UserIsReloading();
4394 }
clamy0e119882015-07-31 16:12:334395}
4396
clamyefca29e2015-09-17 00:22:114397void WebContentsImpl::ReadyToCommitNavigation(
4398 NavigationHandle* navigation_handle) {
Charles Harrison53096ba2017-12-15 15:39:484399 TRACE_EVENT1("navigation", "WebContentsImpl::ReadyToCommitNavigation",
4400 "navigation_handle", navigation_handle);
ericwilligersde386342016-10-19 02:35:094401 for (auto& observer : observers_)
4402 observer.ReadyToCommitNavigation(navigation_handle);
John Abd-El-Malekd4882642017-12-04 21:45:194403
Kenneth Russell954ed9ce2018-04-12 23:07:014404 // If any domains are blocked from accessing 3D APIs because they may
4405 // have caused the GPU to reset recently, unblock them here if the user
4406 // initiated this navigation. This implies that the user was involved in
4407 // the decision to navigate, so there's no concern about
4408 // denial-of-service issues. Want to do this as early as
4409 // possible to avoid race conditions with pages attempting to access
4410 // WebGL early on.
4411 //
4412 // TODO(crbug.com/617904): currently navigations initiated by the browser
4413 // (reload button, reload menu option, pressing return in the Omnibox)
4414 // return false from HasUserGesture(). If or when that is addressed,
4415 // remove the check for IsRendererInitiated() below.
4416 //
4417 // TODO(crbug.com/832180): HasUserGesture comes from the renderer
4418 // process and isn't validated. Until it is, don't trust it.
4419 if (!navigation_handle->IsRendererInitiated()) {
4420 GpuDataManagerImpl::GetInstance()->UnblockDomainFrom3DAPIs(
4421 navigation_handle->GetURL());
4422 }
4423
John Abd-El-Malekfde08e6da2017-12-05 04:06:244424 if (navigation_handle->IsSameDocument())
4425 return;
4426
Livvie Lin139bf442018-08-10 00:18:544427 // SSLInfo is not needed on subframe navigations since the main-frame
4428 // certificate is the only one that can be inspected (using the info
4429 // bubble) without refreshing the page with DevTools open.
Carlos ILaed1e7b2019-03-12 19:27:214430 // We don't call DidStartResourceResponse on net errors, since that results on
4431 // existing cert exceptions being revoked, which leads to weird behavior with
4432 // committed interstitials or while offline. We only need the error check for
4433 // the main frame case because unlike this method, SubresourceResponseStarted
4434 // does not get called on network errors.
4435 if (navigation_handle->IsInMainFrame() &&
4436 navigation_handle->GetNetErrorCode() == net::OK) {
Livvie Lin139bf442018-08-10 00:18:544437 controller_.ssl_manager()->DidStartResourceResponse(
Lukasz Anforowiczee527cc2019-11-19 02:06:594438 url::Origin::Create(navigation_handle->GetURL()),
Camille Lamy62b826012019-02-26 09:15:474439 navigation_handle->GetSSLInfo().has_value()
4440 ? net::IsCertStatusError(
4441 navigation_handle->GetSSLInfo()->cert_status)
4442 : false);
4443 }
John Abd-El-Malekfde08e6da2017-12-05 04:06:244444
4445 SetNotWaitingForResponse();
clamyefca29e2015-09-17 00:22:114446}
4447
clamy0e119882015-07-31 16:12:334448void WebContentsImpl::DidFinishNavigation(NavigationHandle* navigation_handle) {
Charles Harrison53096ba2017-12-15 15:39:484449 TRACE_EVENT1("navigation", "WebContentsImpl::DidFinishNavigation",
4450 "navigation_handle", navigation_handle);
Steven Holte38041342018-06-22 22:00:574451
ericwilligersde386342016-10-19 02:35:094452 for (auto& observer : observers_)
4453 observer.DidFinishNavigation(navigation_handle);
clamy0e119882015-07-31 16:12:334454
Becca Hughesd11d6502018-07-31 17:01:284455 if (display_cutout_host_impl_)
4456 display_cutout_host_impl_->DidFinishNavigation(navigation_handle);
4457
jam5f358e52017-02-13 16:37:034458 if (navigation_handle->HasCommitted()) {
Yue Ru Sun979c4b162019-11-06 16:11:044459 if (navigation_handle->IsInMainFrame()) {
4460 last_committed_source_id_including_same_document_ =
Steven Holte38041342018-06-22 22:00:574461 ukm::ConvertToSourceId(navigation_handle->GetNavigationId(),
4462 ukm::SourceIdType::NAVIGATION_ID);
Yue Ru Sun979c4b162019-11-06 16:11:044463
4464 if (!navigation_handle->IsSameDocument()) {
4465 was_ever_audible_ = false;
4466 last_committed_source_id_ =
4467 last_committed_source_id_including_same_document_;
4468 }
Tommy Steimel18360512017-11-01 00:38:194469 }
dmazzonibf8cec42015-02-08 08:28:084470 }
Xiyuan Xia2a66b112018-12-06 15:52:164471
4472 // If we didn't end up on about:blank after setting this in DidStartNavigation
4473 // then don't focus the location bar.
4474 if (should_focus_location_bar_by_default_ &&
4475 navigation_handle->GetURL() != url::kAboutBlankURL) {
4476 should_focus_location_bar_by_default_ = false;
4477 }
yilkal796e89e2019-10-02 23:58:284478
yilkal172da772019-11-06 22:49:074479 if (navigation_handle->IsInMainFrame() && first_navigation_completed_)
4480 RecordMaxFrameCountUMA(max_loaded_frame_count_);
yilkal796e89e2019-10-02 23:58:284481
yilkal172da772019-11-06 22:49:074482 // If navigation has successfully finished in the main frame, set
4483 // |first_navigation_completed_| to true so that we will record
4484 // |max_loaded_frame_count_| above when future main frame navigations finish.
4485 if (navigation_handle->IsInMainFrame() && !navigation_handle->IsErrorPage()) {
4486 first_navigation_completed_ = true;
4487
4488 // Navigation has completed in main frame. Reset |max_loaded_frame_count_|.
4489 // |max_loaded_frame_count_| is not necessarily 1 if the navigation was
4490 // served from BackForwardCache.
4491 max_loaded_frame_count_ =
4492 GetMainFrame()->frame_tree_node()->GetFrameTreeSize();
yilkal796e89e2019-10-02 23:58:284493 }
[email protected]400992b2012-06-14 00:03:544494}
4495
[email protected]b80624c2014-02-09 02:46:554496void WebContentsImpl::DidFailLoadWithError(
4497 RenderFrameHostImpl* render_frame_host,
[email protected]b80624c2014-02-09 02:46:554498 const GURL& url,
[email protected]b80624c2014-02-09 02:46:554499 int error_code,
Yutaka Hirano16d3c5d2017-07-31 06:17:304500 const base::string16& error_description) {
ericwilligersde386342016-10-19 02:35:094501 for (auto& observer : observers_) {
Yutaka Hirano16d3c5d2017-07-31 06:17:304502 observer.DidFailLoad(render_frame_host, url, error_code, error_description);
ericwilligersde386342016-10-19 02:35:094503 }
[email protected]b80624c2014-02-09 02:46:554504}
4505
[email protected]52913802013-12-10 05:52:184506void WebContentsImpl::NotifyChangedNavigationState(
4507 InvalidateTypes changed_flags) {
4508 NotifyNavigationStateChanged(changed_flags);
4509}
4510
toyoshim0df1d3a2016-09-09 09:52:484511void WebContentsImpl::DidStartNavigationToPendingEntry(const GURL& url,
4512 ReloadType reload_type) {
[email protected]3691e5cf2014-01-22 10:16:204513 // Notify observers about navigation.
ericwilligersde386342016-10-19 02:35:094514 for (auto& observer : observers_)
4515 observer.DidStartNavigationToPendingEntry(url, reload_type);
[email protected]3691e5cf2014-01-22 10:16:204516}
4517
lukasza1d025732016-09-28 21:36:084518bool WebContentsImpl::ShouldTransferNavigation(bool is_main_frame_navigation) {
creis29460272015-12-16 04:38:224519 if (!delegate_)
4520 return true;
lukasza1d025732016-09-28 21:36:084521 return delegate_->ShouldTransferNavigation(is_main_frame_navigation);
creis29460272015-12-16 04:38:224522}
4523
[email protected]aa62afd2014-04-22 19:22:464524bool WebContentsImpl::ShouldPreserveAbortedURLs() {
4525 if (!delegate_)
4526 return false;
4527 return delegate_->ShouldPreserveAbortedURLs(this);
4528}
4529
[email protected]0d0f4c492014-04-02 06:42:574530void WebContentsImpl::DidNavigateMainFramePreCommit(
[email protected]5cfbddc2014-06-23 23:52:234531 bool navigation_is_within_page) {
[email protected]0d0f4c492014-04-02 06:42:574532 // Ensure fullscreen mode is exited before committing the navigation to a
4533 // different page. The next page will not start out assuming it is in
4534 // fullscreen mode.
[email protected]5cfbddc2014-06-23 23:52:234535 if (navigation_is_within_page) {
[email protected]dfc39cb2014-04-09 22:58:194536 // No page change? Then, the renderer and browser can remain in fullscreen.
[email protected]0d0f4c492014-04-02 06:42:574537 return;
4538 }
alexmos5dd617d2016-06-07 04:21:154539 if (IsFullscreenForCurrentTab())
bokanece34a82016-01-28 19:49:464540 ExitFullscreen(false);
alexmos5dd617d2016-06-07 04:21:154541 DCHECK(!IsFullscreenForCurrentTab());
Joe Downingc1a57efe2018-05-09 18:23:544542
4543 // Clean up keyboard lock state when navigating.
4544 CancelKeyboardLock(keyboard_lock_widget_);
[email protected]0d0f4c492014-04-02 06:42:574545}
4546
[email protected]37567b432014-02-12 01:12:224547void WebContentsImpl::DidNavigateMainFramePostCommit(
sky8b00392d2015-01-10 00:30:284548 RenderFrameHostImpl* render_frame_host,
[email protected]37567b432014-02-12 01:12:224549 const LoadCommittedDetails& details,
4550 const FrameHostMsg_DidCommitProvisionalLoad_Params& params) {
4551 if (details.is_navigation_to_different_page()) {
4552 // Clear the status bubble. This is a workaround for a bug where WebKit
4553 // doesn't let us know that the cursor left an element during a
4554 // transition (this is also why the mouse cursor remains as a hand after
4555 // clicking on a link); see bugs 1184641 and 980803. We don't want to
4556 // clear the bubble when a user navigates to a named anchor in the same
4557 // page.
Lukasz Anforowiczfd707142018-02-07 19:46:134558 ClearTargetURL();
lanwei9d343ad22015-02-11 01:46:004559
4560 RenderWidgetHostViewBase* rwhvb =
4561 static_cast<RenderWidgetHostViewBase*>(GetRenderWidgetHostView());
4562 if (rwhvb)
4563 rwhvb->OnDidNavigateMainFrameToNewPage();
[email protected]37567b432014-02-12 01:12:224564 }
4565
[email protected]e85165c642014-06-10 14:34:314566 if (delegate_)
[email protected]37567b432014-02-12 01:12:224567 delegate_->DidNavigateMainFramePostCommit(this);
[email protected]e85165c642014-06-10 14:34:314568 view_->SetOverscrollControllerEnabled(CanOverscrollContent());
Shivani Sharma985474f2019-05-23 21:36:474569
4570 if (!details.is_same_document && GetInnerWebContents().empty())
4571 had_inner_webcontents_ = false;
Carlos Caballero91fffb22019-10-29 15:24:304572
4573 if (details.is_navigation_to_different_page() &&
4574 GetRenderViewHost()->did_first_visually_non_empty_paint()) {
4575 // This event will not fire again if the page is restored from the
4576 // BackForwardCache. So fire it ourselves if needed.
4577 DidFirstVisuallyNonEmptyPaint(GetRenderViewHost());
4578 }
Carlos Caballero1215f882019-10-29 15:58:434579
4580 if (GetRenderViewHost()->theme_color() != last_sent_theme_color_) {
4581 // This event will not fire again if the page is restored from the
4582 // BackForwardCache. So fire it ourselves if needed.
4583 OnThemeColorChanged(GetRenderViewHost());
4584 }
[email protected]37567b432014-02-12 01:12:224585}
4586
4587void WebContentsImpl::DidNavigateAnyFramePostCommit(
4588 RenderFrameHostImpl* render_frame_host,
4589 const LoadCommittedDetails& details,
4590 const FrameHostMsg_DidCommitProvisionalLoad_Params& params) {
[email protected]b4c84012014-04-28 19:51:104591 // Now that something has committed, we don't need to track whether the
4592 // initial page has been accessed.
4593 has_accessed_initial_document_ = false;
4594
[email protected]37567b432014-02-12 01:12:224595 // If we navigate off the page, close all JavaScript dialogs.
eugenebutee08663a2017-04-27 17:43:124596 if (!details.is_same_document)
creis89a0f782015-05-27 16:13:174597 CancelActiveAndPendingDialogs();
[email protected]37567b432014-02-12 01:12:224598
avid53461d2016-02-25 17:15:044599 // If this is a user-initiated navigation, start allowing JavaScript dialogs
4600 // again.
avi5d3b8692016-10-12 22:00:464601 if (params.gesture == NavigationGestureUser && dialog_manager_) {
avi6879fcf2017-01-21 05:27:534602 dialog_manager_->CancelDialogs(this, /*reset_state=*/true);
avi5d3b8692016-10-12 22:00:464603 }
[email protected]37567b432014-02-12 01:12:224604}
4605
4606void WebContentsImpl::SetMainFrameMimeType(const std::string& mime_type) {
4607 contents_mime_type_ = mime_type;
4608}
4609
[email protected]277857a2014-06-03 10:38:014610bool WebContentsImpl::CanOverscrollContent() const {
[email protected]e85165c642014-06-10 14:34:314611 // Disable overscroll when touch emulation is on. See crbug.com/369938.
[email protected]34ff1cfc2014-08-20 06:16:054612 if (force_disable_overscroll_content_)
[email protected]e85165c642014-06-10 14:34:314613 return false;
4614
[email protected]37567b432014-02-12 01:12:224615 if (delegate_)
4616 return delegate_->CanOverscrollContent();
4617
4618 return false;
4619}
4620
Carlos Caballero1215f882019-10-29 15:58:434621void WebContentsImpl::OnThemeColorChanged(RenderViewHostImpl* source) {
4622 if (source->did_first_visually_non_empty_paint() &&
4623 last_sent_theme_color_ != source->theme_color()) {
ericwilligersde386342016-10-19 02:35:094624 for (auto& observer : observers_)
Carlos Caballero1215f882019-10-29 15:58:434625 observer.DidChangeThemeColor(source->theme_color());
4626 last_sent_theme_color_ = source->theme_color();
peter90afaba2015-06-01 12:05:294627 }
[email protected]e7104762014-06-20 19:17:254628}
4629
[email protected]b172aee2012-04-10 17:05:264630void WebContentsImpl::OnDidLoadResourceFromMemoryCache(
nicka0ac8382016-12-15 23:59:234631 RenderFrameHostImpl* source,
[email protected]724159a2010-12-30 01:11:184632 const GURL& url,
[email protected]70435962011-08-02 20:13:284633 const std::string& http_method,
[email protected]6d6cfb3a2012-05-23 22:53:184634 const std::string& mime_type,
[email protected]6c1e05212014-07-31 00:59:404635 ResourceType resource_type) {
ericwilligersde386342016-10-19 02:35:094636 for (auto& observer : observers_)
4637 observer.DidLoadResourceFromMemoryCache(url, mime_type, resource_type);
[email protected]b0f724c2013-09-05 04:21:134638
Steven Bingler674fb0822018-12-12 18:04:524639 if (url.is_valid() && url.SchemeIsHTTPOrHTTPS()) {
nicka0ac8382016-12-15 23:59:234640 StoragePartition* partition = source->GetProcess()->GetStoragePartition();
John Abd-El-Malekf4fe9d82019-07-31 04:05:594641 partition->GetNetworkContext()->NotifyExternalCacheHit(
Matt Menke2a33dee2019-12-02 17:01:544642 url, http_method, source->GetNetworkIsolationKey());
[email protected]8bfc8272013-09-09 20:10:534643 }
[email protected]724159a2010-12-30 01:11:184644}
4645
carloskd9d97942017-02-16 08:58:094646void WebContentsImpl::DidDisplayInsecureContent() {
estarkc2273502016-10-12 22:03:024647 controller_.ssl_manager()->DidDisplayMixedContent();
[email protected]724159a2010-12-30 01:11:184648}
4649
Dave Tapuskaa657d872019-10-21 20:23:214650void WebContentsImpl::DidContainInsecureFormAction() {
elawrenceb2ac2a232017-03-27 21:46:254651 controller_.ssl_manager()->DidContainInsecureFormAction();
4652}
4653
nicka0ac8382016-12-15 23:59:234654void WebContentsImpl::OnDidRunInsecureContent(RenderFrameHostImpl* source,
4655 const GURL& security_origin,
estark910b4572015-12-09 20:55:414656 const GURL& target_url) {
nicka0ac8382016-12-15 23:59:234657 // TODO(nick, estark): Should we call FilterURL using |source|'s process on
4658 // these parameters? |target_url| seems unused, except for a log message. And
4659 // |security_origin| might be replaceable with the origin of the main frame.
carloskd9d97942017-02-16 08:58:094660 DidRunInsecureContent(security_origin, target_url);
4661}
4662
4663void WebContentsImpl::DidRunInsecureContent(const GURL& security_origin,
4664 const GURL& target_url) {
[email protected]9450c462013-11-23 01:22:584665 LOG(WARNING) << security_origin << " ran insecure content from "
4666 << target_url.possibly_invalid_spec();
[email protected]e6e30ac2014-01-13 21:24:394667 RecordAction(base::UserMetricsAction("SSL.RanInsecureContent"));
estark910b4572015-12-09 20:55:414668 if (base::EndsWith(security_origin.spec(), kDotGoogleDotCom,
brettwa7ff1b292015-07-16 17:49:294669 base::CompareCase::INSENSITIVE_ASCII))
[email protected]e6e30ac2014-01-13 21:24:394670 RecordAction(base::UserMetricsAction("SSL.RanInsecureContentGoogle"));
estarkc2273502016-10-12 22:03:024671 controller_.ssl_manager()->DidRunMixedContent(security_origin);
[email protected]724159a2010-12-30 01:11:184672}
4673
carloskd9d97942017-02-16 08:58:094674void WebContentsImpl::PassiveInsecureContentFound(const GURL& resource_url) {
Sergey Kuznetsov3ebc01b2018-02-01 04:57:364675 if (delegate_) {
4676 delegate_->PassiveInsecureContentFound(resource_url);
4677 }
carloskd9d97942017-02-16 08:58:094678}
4679
4680bool WebContentsImpl::ShouldAllowRunningInsecureContent(
4681 WebContents* web_contents,
4682 bool allowed_per_prefs,
4683 const url::Origin& origin,
4684 const GURL& resource_url) {
Sergey Kuznetsov3ebc01b2018-02-01 04:57:364685 if (delegate_) {
4686 return delegate_->ShouldAllowRunningInsecureContent(
4687 web_contents, allowed_per_prefs, origin, resource_url);
4688 }
4689
4690 return allowed_per_prefs;
carloskd9d97942017-02-16 08:58:094691}
4692
Lukasz Anforowicze1b954d92017-10-30 21:28:064693void WebContentsImpl::ViewSource(RenderFrameHostImpl* frame) {
4694 DCHECK_EQ(this, WebContents::FromRenderFrameHost(frame));
4695
4696 // Don't do anything if there is no |delegate_| that could accept and show the
4697 // new WebContents containing the view-source.
4698 if (!delegate_)
4699 return;
4700
4701 // Use the last committed entry, since the pending entry hasn't loaded yet and
4702 // won't be copied into the cloned tab.
4703 NavigationEntryImpl* last_committed_entry =
4704 static_cast<NavigationEntryImpl*>(frame->frame_tree_node()
4705 ->navigator()
4706 ->GetController()
4707 ->GetLastCommittedEntry());
4708 if (!last_committed_entry)
4709 return;
4710
4711 FrameNavigationEntry* frame_entry =
4712 last_committed_entry->GetFrameEntry(frame->frame_tree_node());
4713 if (!frame_entry)
4714 return;
4715
4716 // Any new WebContents opened while this WebContents is in fullscreen can be
4717 // used to confuse the user, so drop fullscreen.
Avi Drissmand18c51972018-08-15 18:32:094718 ForSecurityDropFullscreen();
Lukasz Anforowicze1b954d92017-10-30 21:28:064719
4720 // We intentionally don't share the SiteInstance with the original frame so
4721 // that view source has a consistent process model and always ends up in a new
4722 // process (https://crbug.com/699493).
4723 scoped_refptr<SiteInstanceImpl> site_instance_for_view_source = nullptr;
Lukasz Anforowicz435bcb582019-07-12 20:50:064724 // Referrer and initiator are not important, because view-source should not
4725 // hit the network, but should be served from the cache instead.
Lukasz Anforowicze1b954d92017-10-30 21:28:064726 Referrer referrer_for_view_source;
Lukasz Anforowicz435bcb582019-07-12 20:50:064727 base::Optional<url::Origin> initiator_for_view_source = base::nullopt;
Lukasz Anforowicze1b954d92017-10-30 21:28:064728 // Do not restore title, derive it from the url.
4729 base::string16 title_for_view_source;
4730 auto navigation_entry = std::make_unique<NavigationEntryImpl>(
4731 site_instance_for_view_source, frame_entry->url(),
Lukasz Anforowicz435bcb582019-07-12 20:50:064732 referrer_for_view_source, initiator_for_view_source,
4733 title_for_view_source, ui::PAGE_TRANSITION_LINK,
Marijn Kruisselbrink7a0d5e182018-05-24 22:55:094734 /* is_renderer_initiated = */ false,
4735 /* blob_url_loader_factory = */ nullptr);
Lukasz Anforowicze1b954d92017-10-30 21:28:064736 navigation_entry->SetVirtualURL(GURL(content::kViewSourceScheme +
4737 std::string(":") +
4738 frame_entry->url().spec()));
4739
Matt Menke2a33dee2019-12-02 17:01:544740 navigation_entry->set_network_isolation_key(frame->GetNetworkIsolationKey());
Yao Xiaof68f57d2019-10-03 04:20:054741
Lukasz Anforowicze1b954d92017-10-30 21:28:064742 // Do not restore scroller position.
4743 // TODO(creis, lukasza, arthursonzogni): Do not reuse the original PageState,
4744 // but start from a new one and only copy the needed data.
4745 const PageState& new_page_state =
4746 frame_entry->page_state().RemoveScrollOffset();
4747
4748 scoped_refptr<FrameNavigationEntry> new_frame_entry =
4749 navigation_entry->root_node()->frame_entry;
4750 new_frame_entry->set_method(frame_entry->method());
4751 new_frame_entry->SetPageState(new_page_state);
4752
4753 // Create a new WebContents, which is used to display the source code.
erikchenbee5c9622018-04-27 19:30:254754 std::unique_ptr<WebContents> view_source_contents =
Erik Chenbb8e738e2018-04-28 14:10:434755 Create(CreateParams(GetBrowserContext()));
Lukasz Anforowicze1b954d92017-10-30 21:28:064756
4757 // Restore the previously created NavigationEntry.
4758 std::vector<std::unique_ptr<NavigationEntry>> navigation_entries;
4759 navigation_entries.push_back(std::move(navigation_entry));
4760 view_source_contents->GetController().Restore(0, RestoreType::CURRENT_SESSION,
4761 &navigation_entries);
4762
4763 // Add |view_source_contents| as a new tab.
4764 gfx::Rect initial_rect;
4765 constexpr bool kUserGesture = true;
4766 bool ignored_was_blocked;
erikchenbee5c9622018-04-27 19:30:254767 delegate_->AddNewContents(this, std::move(view_source_contents),
Lukasz Anforowicze1b954d92017-10-30 21:28:064768 WindowOpenDisposition::NEW_FOREGROUND_TAB,
4769 initial_rect, kUserGesture, &ignored_was_blocked);
4770 // Note that the |delegate_| could have deleted |view_source_contents| during
4771 // AddNewContents method call.
4772}
4773
Lukasz Anforowiczee527cc2019-11-19 02:06:594774void WebContentsImpl::SubresourceResponseStarted(
4775 const url::Origin& origin_of_final_response_url,
4776 net::CertStatus cert_status) {
4777 controller_.ssl_manager()->DidStartResourceResponse(
4778 origin_of_final_response_url, cert_status);
Jay Civellid5904f02018-03-23 19:43:574779 SetNotWaitingForResponse();
4780}
4781
Jay Civelli116683f2018-03-27 19:56:234782void WebContentsImpl::ResourceLoadComplete(
Jay Civelli52a63302018-04-30 22:39:554783 RenderFrameHost* render_frame_host,
Clark DuVall36164bd2018-08-09 22:49:084784 const GlobalRequestID& request_id,
Jay Civelli116683f2018-03-27 19:56:234785 mojom::ResourceLoadInfoPtr resource_load_info) {
John Abd-El-Malek9b8f7fa2017-12-04 23:52:254786 for (auto& observer : observers_) {
Clark DuVall36164bd2018-08-09 22:49:084787 observer.ResourceLoadComplete(render_frame_host, request_id,
4788 *resource_load_info);
John Abd-El-Malek9b8f7fa2017-12-04 23:52:254789 }
John Abd-El-Malekd4882642017-12-04 21:45:194790}
4791
Wei Li5bb659742018-02-14 03:07:584792void WebContentsImpl::PrintCrossProcessSubframe(
4793 const gfx::Rect& rect,
4794 int document_cookie,
4795 RenderFrameHost* subframe_host) {
Wei Li5c0b4d12018-03-22 15:05:224796 auto* outer_contents = GetOuterWebContents();
4797 if (outer_contents) {
4798 // When an extension or app page is printed, the content should be
4799 // composited with outer content, so the outer contents should handle the
4800 // print request.
4801 outer_contents->PrintCrossProcessSubframe(rect, document_cookie,
4802 subframe_host);
4803 return;
4804 }
4805
Wei Li5bb659742018-02-14 03:07:584806 // If there is no delegate such as in tests or during deletion, do nothing.
4807 if (!delegate_)
4808 return;
4809
4810 delegate_->PrintCrossProcessSubframe(this, rect, document_cookie,
4811 subframe_host);
4812}
4813
rob.buis300b0872017-03-10 20:43:584814#if defined(OS_ANDROID)
4815base::android::ScopedJavaLocalRef<jobject>
4816WebContentsImpl::GetJavaRenderFrameHostDelegate() {
4817 return GetJavaWebContents();
4818}
4819#endif
4820
estark910b4572015-12-09 20:55:414821void WebContentsImpl::OnDidDisplayContentWithCertificateErrors(
Zhuoyu Qian4353f7f2018-01-08 05:33:004822 RenderFrameHostImpl* source) {
estarkc2273502016-10-12 22:03:024823 controller_.ssl_manager()->DidDisplayContentWithCertErrors();
estark910b4572015-12-09 20:55:414824}
4825
4826void WebContentsImpl::OnDidRunContentWithCertificateErrors(
Zhuoyu Qian4353f7f2018-01-08 05:33:004827 RenderFrameHostImpl* source) {
nicka0ac8382016-12-15 23:59:234828 // TODO(nick, estark): Do we need to consider |source| here somehow?
estarkcd2e30c2016-08-12 06:51:154829 NavigationEntry* entry = controller_.GetVisibleEntry();
4830 if (!entry)
estarkaeda1422016-05-10 02:31:124831 return;
4832
estarkaeda1422016-05-10 02:31:124833 // TODO(estark): check that this does something reasonable for
4834 // about:blank and sandboxed origins. https://crbug.com/609527
estarkcd2e30c2016-08-12 06:51:154835 controller_.ssl_manager()->DidRunContentWithCertErrors(
4836 entry->GetURL().GetOrigin());
estark910b4572015-12-09 20:55:414837}
4838
Kouhei Ueno97e0990d2019-09-25 02:57:274839void WebContentsImpl::DOMContentLoaded(RenderFrameHost* render_frame_host) {
ericwilligersde386342016-10-19 02:35:094840 for (auto& observer : observers_)
Kouhei Uenof6e96242019-09-27 01:54:354841 observer.DOMContentLoaded(render_frame_host);
[email protected]724159a2010-12-30 01:11:184842}
4843
nicka0ac8382016-12-15 23:59:234844void WebContentsImpl::OnDidFinishLoad(RenderFrameHostImpl* source,
4845 const GURL& url) {
[email protected]8b3af1e2014-01-24 13:29:124846 GURL validated_url(url);
nicka0ac8382016-12-15 23:59:234847 source->GetProcess()->FilterURL(false, &validated_url);
[email protected]028053d42014-03-05 22:20:374848
ericwilligersde386342016-10-19 02:35:094849 for (auto& observer : observers_)
nicka0ac8382016-12-15 23:59:234850 observer.DidFinishLoad(source, validated_url);
yilkal172da772019-11-06 22:49:074851
4852 size_t tree_size = frame_tree_.root()->GetFrameTreeSize();
4853 if (max_loaded_frame_count_ < tree_size)
4854 max_loaded_frame_count_ = tree_size;
4855
4856 if (!source->GetParent())
4857 UMA_HISTOGRAM_COUNTS_1000("Navigation.MainFrame.FrameCount", tree_size);
[email protected]1a55c5be2011-11-29 11:36:314858}
4859
Dave Tapuska549baab2019-03-19 15:52:404860void WebContentsImpl::OnGoToEntryAtOffset(RenderFrameHostImpl* source,
Arthur Sonzognia7d715a2018-09-20 16:11:134861 int offset,
Dave Tapuska043b94a2019-09-10 14:14:354862 bool has_user_gesture) {
Arthur Sonzognia7d715a2018-09-20 16:11:134863 // Non-user initiated navigations coming from the renderer should be ignored
4864 // if there is an ongoing browser-initiated navigation.
4865 // See https://crbug.com/879965.
4866 // TODO(arthursonzogni): See if this should check for ongoing navigations in
4867 // the frame(s) affected by the session history navigation, rather than just
4868 // the main frame.
4869 if (!has_user_gesture) {
4870 NavigationRequest* ongoing_navigation_request =
4871 frame_tree_.root()->navigation_request();
4872 if (ongoing_navigation_request &&
4873 ongoing_navigation_request->browser_initiated()) {
4874 return;
4875 }
4876 }
4877
nicka0ac8382016-12-15 23:59:234878 // All frames are allowed to navigate the global history.
Dave Tapuska8bfd84c2019-03-26 20:47:164879 if (!delegate_ || delegate_->OnGoToEntryOffset(offset)) {
Dave Tapuska043b94a2019-09-10 14:14:354880 if (source->IsSandboxed(blink::WebSandboxFlags::kTopNavigation)) {
Dave Tapuska8bfd84c2019-03-26 20:47:164881 // Keep track of whether this is a session history from a sandboxed iframe
4882 // with top level navigation disallowed.
4883 controller_.GoToOffsetInSandboxedFrame(offset,
4884 source->GetFrameTreeNodeId());
4885 } else {
4886 controller_.GoToOffset(offset);
4887 }
4888 }
[email protected]216813952011-05-19 22:21:264889}
4890
nicka0ac8382016-12-15 23:59:234891void WebContentsImpl::OnPageScaleFactorChanged(RenderViewHostImpl* source,
4892 float page_scale_factor) {
mcnee336ea2c2017-05-23 22:50:594893#if !defined(OS_ANDROID)
4894 // While page scale factor is used on mobile, this PageScaleFactorIsOne logic
4895 // is only needed on desktop.
mcnee432e47d2015-11-09 19:37:464896 bool is_one = page_scale_factor == 1.f;
4897 if (is_one != page_scale_factor_is_one_) {
4898 page_scale_factor_is_one_ = is_one;
4899
4900 HostZoomMapImpl* host_zoom_map =
4901 static_cast<HostZoomMapImpl*>(HostZoomMap::GetForWebContents(this));
4902
nicka0ac8382016-12-15 23:59:234903 if (host_zoom_map) {
mcnee432e47d2015-11-09 19:37:464904 host_zoom_map->SetPageScaleFactorIsOneForView(
nicka0ac8382016-12-15 23:59:234905 source->GetProcess()->GetID(), source->GetRoutingID(),
mcnee432e47d2015-11-09 19:37:464906 page_scale_factor_is_one_);
4907 }
4908 }
mcnee336ea2c2017-05-23 22:50:594909#endif // !defined(OS_ANDROID)
mcnee432e47d2015-11-09 19:37:464910
ericwilligersde386342016-10-19 02:35:094911 for (auto& observer : observers_)
4912 observer.OnPageScaleFactorChanged(page_scale_factor);
mcnee432e47d2015-11-09 19:37:464913}
4914
W. James MacLean62198672019-06-04 16:46:104915void WebContentsImpl::OnTextAutosizerPageInfoChanged(
4916 RenderViewHostImpl* source,
4917 const blink::WebTextAutosizerPageInfo& page_info) {
4918 // Keep a copy of |page_info| in case we create a new RenderView before
4919 // the next update.
4920 text_autosizer_page_info_ = page_info;
4921 frame_tree_.root()->render_manager()->SendPageMessage(
4922 new PageMsg_UpdateTextAutosizerPageInfoForRemoteMainFrames(
4923 MSG_ROUTING_NONE, page_info),
4924 source->GetSiteInstance());
4925}
4926
Kent Tamura8c9e0562018-11-06 07:02:194927void WebContentsImpl::EnumerateDirectory(
4928 RenderFrameHost* render_frame_host,
4929 std::unique_ptr<FileSelectListener> listener,
4930 const base::FilePath& directory_path) {
4931 if (delegate_)
4932 delegate_->EnumerateDirectory(this, std::move(listener), directory_path);
4933 else
4934 listener->FileSelectionCanceled();
[email protected]3a29a6e2011-08-24 18:26:214935}
4936
Dave Tapuskaf9fc3122019-10-18 19:42:554937void WebContentsImpl::RegisterProtocolHandler(RenderFrameHostImpl* source,
4938 const std::string& protocol,
4939 const GURL& url,
4940 const base::string16& title,
4941 bool user_gesture) {
nicka0ac8382016-12-15 23:59:234942 // TODO(nick): Should we consider |source| here or pass it to the delegate?
4943 // TODO(nick): Do we need to apply FilterURL to |url|?
[email protected]e5f2de02012-07-20 22:15:434944 if (!delegate_)
4945 return;
4946
Raymes Khoury58373df2019-08-06 06:29:204947 if (!AreValidRegisterProtocolHandlerArguments(
4948 protocol, url, source->GetLastCommittedOrigin())) {
4949 ReceivedBadMessage(source->GetProcess(),
4950 bad_message::REGISTER_PROTOCOL_HANDLER_INVALID_URL);
[email protected]b9535422012-02-09 01:47:594951 return;
Raymes Khoury58373df2019-08-06 06:29:204952 }
[email protected]8f810632013-06-06 22:33:324953
[email protected]c125e532014-05-10 20:39:064954 delegate_->RegisterProtocolHandler(this, protocol, url, user_gesture);
[email protected]7d189022011-08-25 22:54:204955}
4956
Dave Tapuskaf9fc3122019-10-18 19:42:554957void WebContentsImpl::UnregisterProtocolHandler(RenderFrameHostImpl* source,
4958 const std::string& protocol,
4959 const GURL& url,
4960 bool user_gesture) {
nicka0ac8382016-12-15 23:59:234961 // TODO(nick): Should we consider |source| here or pass it to the delegate?
4962 // TODO(nick): Do we need to apply FilterURL to |url|?
[email protected]f5273e52014-07-14 16:30:204963 if (!delegate_)
4964 return;
4965
Raymes Khoury58373df2019-08-06 06:29:204966 if (!AreValidRegisterProtocolHandlerArguments(
4967 protocol, url, source->GetLastCommittedOrigin())) {
4968 ReceivedBadMessage(source->GetProcess(),
4969 bad_message::REGISTER_PROTOCOL_HANDLER_INVALID_URL);
[email protected]f5273e52014-07-14 16:30:204970 return;
Raymes Khoury58373df2019-08-06 06:29:204971 }
[email protected]f5273e52014-07-14 16:30:204972
4973 delegate_->UnregisterProtocolHandler(this, protocol, url, user_gesture);
4974}
4975
kouhei40f03cb2015-09-24 07:47:014976void WebContentsImpl::OnUpdatePageImportanceSignals(
nicka0ac8382016-12-15 23:59:234977 RenderFrameHostImpl* source,
kouhei40f03cb2015-09-24 07:47:014978 const PageImportanceSignals& signals) {
nicka0ac8382016-12-15 23:59:234979 // TODO(nick, kouhei): Fix this for oopifs; currently all frames' state gets
4980 // written to this one field.
kouhei40f03cb2015-09-24 07:47:014981 page_importance_signals_ = signals;
4982}
4983
nicka0ac8382016-12-15 23:59:234984void WebContentsImpl::OnDomOperationResponse(RenderFrameHostImpl* source,
4985 const std::string& json_string) {
4986 // TODO(nick, lukasza): The notification below should probably be updated to
4987 // include |source|.
avi60bd4902015-09-23 20:39:244988 std::string json = json_string;
4989 NotificationService::current()->Notify(NOTIFICATION_DOM_OPERATION_RESPONSE,
4990 Source<WebContents>(this),
4991 Details<std::string>(&json));
[email protected]cfa856d62014-02-22 07:58:404992}
4993
Marijn Kruisselbrink7f519672019-02-05 19:12:504994void WebContentsImpl::OnAppCacheAccessed(const GURL& manifest_url,
[email protected]b172aee2012-04-10 17:05:264995 bool blocked_by_policy) {
nicka0ac8382016-12-15 23:59:234996 // TODO(nick): Should we consider |source| here? Should we call FilterURL on
4997 // |manifest_url|?
4998
[email protected]7fc4bbb2011-09-08 21:23:104999 // Notify observers about navigation.
ericwilligersde386342016-10-19 02:35:095000 for (auto& observer : observers_)
5001 observer.AppCacheAccessed(manifest_url, blocked_by_policy);
[email protected]7fc4bbb2011-09-08 21:23:105002}
5003
Miyoung Shincb6475a2019-11-09 10:49:455004void WebContentsImpl::OnColorChooserFactoryReceiver(
Julie Jeongeun Kim8e2879e2019-08-06 23:55:205005 mojo::PendingReceiver<blink::mojom::ColorChooserFactory> receiver) {
5006 color_chooser_factory_receivers_.Add(this, std::move(receiver));
Joel Hockey85b379d2017-12-11 10:42:135007}
5008
5009void WebContentsImpl::OpenColorChooser(
Julie Jeongeun Kim8e2879e2019-08-06 23:55:205010 mojo::PendingReceiver<blink::mojom::ColorChooser> chooser_receiver,
5011 mojo::PendingRemote<blink::mojom::ColorChooserClient> client,
[email protected]8ed16472014-04-11 19:02:485012 SkColor color,
Joel Hockey163835a2017-12-20 11:51:575013 std::vector<blink::mojom::ColorSuggestionPtr> suggestions) {
Joel Hockey85b379d2017-12-11 10:42:135014 content::ColorChooser* new_color_chooser =
Ivan Kotenkov2c0d2bb32017-11-01 15:41:285015 delegate_ ? delegate_->OpenColorChooser(this, color, suggestions)
5016 : nullptr;
[email protected]820957a2014-01-14 14:56:305017 if (!new_color_chooser)
[email protected]cb805452013-05-22 15:16:215018 return;
[email protected]8ed16472014-04-11 19:02:485019
Joel Hockey85b379d2017-12-11 10:42:135020 color_chooser_.reset();
5021 color_chooser_ = std::make_unique<ColorChooser>(
Julie Jeongeun Kim8e2879e2019-08-06 23:55:205022 new_color_chooser, std::move(chooser_receiver), std::move(client));
[email protected]da8543762012-03-20 08:52:205023}
5024
brettw4b461082016-11-19 18:55:165025#if BUILDFLAG(ENABLE_PLUGINS)
nicka0ac8382016-12-15 23:59:235026void WebContentsImpl::OnPepperInstanceCreated(RenderFrameHostImpl* source,
5027 int32_t pp_instance) {
ericwilligersde386342016-10-19 02:35:095028 for (auto& observer : observers_)
5029 observer.PepperInstanceCreated();
nicka0ac8382016-12-15 23:59:235030 pepper_playback_observer_->PepperInstanceCreated(source, pp_instance);
emaxxe70f5e12015-05-29 11:26:005031}
5032
nicka0ac8382016-12-15 23:59:235033void WebContentsImpl::OnPepperInstanceDeleted(RenderFrameHostImpl* source,
5034 int32_t pp_instance) {
ericwilligersde386342016-10-19 02:35:095035 for (auto& observer : observers_)
5036 observer.PepperInstanceDeleted();
nicka0ac8382016-12-15 23:59:235037 pepper_playback_observer_->PepperInstanceDeleted(source, pp_instance);
emaxxe70f5e12015-05-29 11:26:005038}
5039
nicka0ac8382016-12-15 23:59:235040void WebContentsImpl::OnPepperPluginHung(RenderFrameHostImpl* source,
5041 int plugin_child_id,
tommyclie6633ca72014-10-31 00:40:425042 const base::FilePath& path,
5043 bool is_hung) {
Steven Holte95922222018-09-14 20:06:235044 UMA_HISTOGRAM_COUNTS_1M("Pepper.PluginHung", 1);
tommyclie6633ca72014-10-31 00:40:425045
ericwilligersde386342016-10-19 02:35:095046 for (auto& observer : observers_)
5047 observer.PluginHungStatusChanged(plugin_child_id, path, is_hung);
tommyclie6633ca72014-10-31 00:40:425048}
5049
nicka0ac8382016-12-15 23:59:235050void WebContentsImpl::OnPepperStartsPlayback(RenderFrameHostImpl* source,
5051 int32_t pp_instance) {
5052 pepper_playback_observer_->PepperStartsPlayback(source, pp_instance);
zqzhang181047e62016-07-01 13:37:175053}
5054
nicka0ac8382016-12-15 23:59:235055void WebContentsImpl::OnPepperStopsPlayback(RenderFrameHostImpl* source,
5056 int32_t pp_instance) {
5057 pepper_playback_observer_->PepperStopsPlayback(source, pp_instance);
zqzhang181047e62016-07-01 13:37:175058}
5059
nicka0ac8382016-12-15 23:59:235060void WebContentsImpl::OnPluginCrashed(RenderFrameHostImpl* source,
5061 const base::FilePath& plugin_path,
tommyclie6633ca72014-10-31 00:40:425062 base::ProcessId plugin_pid) {
nicka0ac8382016-12-15 23:59:235063 // TODO(nick): Eliminate the |plugin_pid| parameter, which can't be trusted,
5064 // and is only used by BlinkTestController.
ericwilligersde386342016-10-19 02:35:095065 for (auto& observer : observers_)
5066 observer.PluginCrashed(plugin_path, plugin_pid);
tommyclie6633ca72014-10-31 00:40:425067}
5068
[email protected]d8415ad92012-08-23 14:40:505069void WebContentsImpl::OnRequestPpapiBrokerPermission(
nicka0ac8382016-12-15 23:59:235070 RenderViewHostImpl* source,
5071 int ppb_broker_route_id,
[email protected]d8415ad92012-08-23 14:40:505072 const GURL& url,
[email protected]2dec8ec2013-02-07 19:20:345073 const base::FilePath& plugin_path) {
Avi Drissman0a2a967d2019-04-01 19:33:525074 base::OnceCallback<void(bool)> permission_result_callback = base::BindOnce(
nicka0ac8382016-12-15 23:59:235075 &WebContentsImpl::SendPpapiBrokerPermissionResult, base::Unretained(this),
5076 source->GetProcess()->GetID(), ppb_broker_route_id);
[email protected]8c8fc292012-11-23 18:57:165077 if (!delegate_) {
Tommy Nyquist4b749d02018-03-20 21:46:295078 std::move(permission_result_callback).Run(false);
[email protected]8c8fc292012-11-23 18:57:165079 return;
[email protected]d8415ad92012-08-23 14:40:505080 }
5081
Avi Drissman0a2a967d2019-04-01 19:33:525082 delegate_->RequestPpapiBrokerPermission(
5083 this, url, plugin_path, std::move(permission_result_callback));
[email protected]d8415ad92012-08-23 14:40:505084}
5085
nicka0ac8382016-12-15 23:59:235086void WebContentsImpl::SendPpapiBrokerPermissionResult(int process_id,
5087 int ppb_broker_route_id,
5088 bool result) {
5089 RenderProcessHost* rph = RenderProcessHost::FromID(process_id);
5090 if (rph) {
5091 // TODO(nick): Convert this from ViewMsg_ to a Ppapi msg, since it
5092 // is not routed to a RenderView.
5093 rph->Send(
5094 new ViewMsg_PpapiBrokerPermissionResult(ppb_broker_route_id, result));
5095 }
[email protected]d8415ad92012-08-23 14:40:505096}
brettw4b461082016-11-19 18:55:165097#endif // BUILDFLAG(ENABLE_PLUGINS)
[email protected]19be7a62012-10-01 23:03:375098
[email protected]795c28972012-12-06 06:13:395099void WebContentsImpl::OnUpdateFaviconURL(
lukasza178ff092017-06-07 23:39:505100 RenderFrameHostImpl* source,
[email protected]795c28972012-12-06 06:13:395101 const std::vector<FaviconURL>& candidates) {
lukasza178ff092017-06-07 23:39:505102 // Ignore favicons for non-main frame.
5103 if (source->GetParent()) {
5104 NOTREACHED();
5105 return;
5106 }
5107
pkotwiczf9bc20592015-04-21 18:57:065108 // We get updated favicon URLs after the page stops loading. If a cross-site
5109 // navigation occurs while a page is still loading, the initial page
5110 // may stop loading and send us updated favicon URLs after the navigation
5111 // for the new page has committed.
lukasza178ff092017-06-07 23:39:505112 if (!source->IsCurrent())
pkotwiczf9bc20592015-04-21 18:57:065113 return;
5114
ericwilligersde386342016-10-19 02:35:095115 for (auto& observer : observers_)
5116 observer.DidUpdateFaviconURL(candidates);
[email protected]795c28972012-12-06 06:13:395117}
5118
qinmin72e8bd02016-10-21 19:35:375119void WebContentsImpl::SetIsOverlayContent(bool is_overlay_content) {
5120 is_overlay_content_ = is_overlay_content;
5121}
5122
Albert J. Wongfb64e142018-10-16 01:10:455123void WebContentsImpl::DidFirstVisuallyNonEmptyPaint(
5124 RenderViewHostImpl* source) {
nicka0ac8382016-12-15 23:59:235125 // TODO(nick): When this is ported to FrameHostMsg_, we should only listen if
5126 // |source| is the main frame.
ericwilligersde386342016-10-19 02:35:095127 for (auto& observer : observers_)
5128 observer.DidFirstVisuallyNonEmptyPaint();
yusufod41c5f92015-03-06 00:14:285129
Carlos Caballero1215f882019-10-29 15:58:435130 if (source->theme_color() != last_sent_theme_color_) {
yusufod41c5f92015-03-06 00:14:285131 // Theme color should have updated by now if there was one.
ericwilligersde386342016-10-19 02:35:095132 for (auto& observer : observers_)
Carlos Caballero1215f882019-10-29 15:58:435133 observer.DidChangeThemeColor(source->theme_color());
5134 last_sent_theme_color_ = source->theme_color();
yusufod41c5f92015-03-06 00:14:285135 }
[email protected]9f268072013-11-07 00:02:155136}
[email protected]d9030b82013-07-19 08:26:065137
Adithya Srinivasan46b8a792019-02-01 14:47:235138bool WebContentsImpl::IsPortal() const {
5139 return portal();
5140}
5141
[email protected]17e286e2013-03-01 23:29:395142void WebContentsImpl::NotifyBeforeFormRepostWarningShow() {
ericwilligersde386342016-10-19 02:35:095143 for (auto& observer : observers_)
5144 observer.BeforeFormRepostWarningShow();
[email protected]17e286e2013-03-01 23:29:395145}
5146
[email protected]ec6c05f2013-10-23 18:41:575147void WebContentsImpl::ActivateAndShowRepostFormWarningDialog() {
5148 Activate();
5149 if (delegate_)
5150 delegate_->ShowRepostFormWarningDialog(this);
5151}
5152
[email protected]b4c84012014-04-28 19:51:105153bool WebContentsImpl::HasAccessedInitialDocument() {
5154 return has_accessed_initial_document_;
5155}
5156
afakhry6f0c1ec22016-07-14 13:55:135157void WebContentsImpl::UpdateTitleForEntry(NavigationEntry* entry,
[email protected]fcf75d42013-12-03 20:11:265158 const base::string16& title) {
[email protected]fcf75d42013-12-03 20:11:265159 base::string16 final_title;
Avi Drissman93002212017-09-27 03:20:525160 base::TrimWhitespace(title, base::TRIM_ALL, &final_title);
[email protected]420ae012009-04-24 05:16:325161
[email protected]987fc3a2011-05-26 14:18:095162 // If a page is created via window.open and never navigated,
5163 // there will be no navigation entry. In this situation,
[email protected]73eb2602012-02-09 19:50:555164 // |page_title_when_no_navigation_entry_| will be used for page title.
[email protected]987fc3a2011-05-26 14:18:095165 if (entry) {
[email protected]36fc0392011-12-25 03:59:515166 if (final_title == entry->GetTitle())
afakhry6f0c1ec22016-07-14 13:55:135167 return; // Nothing changed, don't bother.
[email protected]420ae012009-04-24 05:16:325168
[email protected]36fc0392011-12-25 03:59:515169 entry->SetTitle(final_title);
Avi Drissman93002212017-09-27 03:20:525170
5171 // The title for display may differ from the title just set; grab it.
5172 final_title = entry->GetTitleForDisplay();
[email protected]987fc3a2011-05-26 14:18:095173 } else {
5174 if (page_title_when_no_navigation_entry_ == final_title)
afakhry6f0c1ec22016-07-14 13:55:135175 return; // Nothing changed, don't bother.
[email protected]987fc3a2011-05-26 14:18:095176
5177 page_title_when_no_navigation_entry_ = final_title;
5178 }
[email protected]420ae012009-04-24 05:16:325179
[email protected]420ae012009-04-24 05:16:325180 // Lastly, set the title for the view.
[email protected]790e2fd2011-09-21 20:28:235181 view_->SetPageTitle(final_title);
[email protected]420ae012009-04-24 05:16:325182
ericwilligersde386342016-10-19 02:35:095183 for (auto& observer : observers_)
Avi Drissman93002212017-09-27 03:20:525184 observer.TitleWasSet(entry);
[email protected]66798902013-10-01 18:40:165185
afakhry6f0c1ec22016-07-14 13:55:135186 // Broadcast notifications when the UI should be updated.
5187 if (entry == controller_.GetEntryAtOffset(0))
5188 NotifyNavigationStateChanged(INVALIDATE_TYPE_TITLE);
[email protected]420ae012009-04-24 05:16:325189}
5190
fdegansa696e5112015-04-17 01:57:595191void WebContentsImpl::SendChangeLoadProgress() {
[email protected]960b0372014-05-19 18:01:005192 loading_last_progress_update_ = base::TimeTicks::Now();
Evan Stade9f328112019-10-23 20:49:255193 for (auto& observer : observers_)
5194 observer.LoadProgressChanged(frame_tree_.load_progress());
[email protected]960b0372014-05-19 18:01:005195}
5196
5197void WebContentsImpl::ResetLoadProgressState() {
fdegans1d16355162015-03-26 11:58:345198 frame_tree_.ResetLoadProgress();
[email protected]960b0372014-05-19 18:01:005199 loading_weak_factory_.InvalidateWeakPtrs();
5200 loading_last_progress_update_ = base::TimeTicks();
5201}
5202
clamy44e84ce2016-02-22 15:38:255203// Notifies the RenderWidgetHost instance about the fact that the page is
5204// loading, or done loading.
5205void WebContentsImpl::LoadingStateChanged(bool to_different_document,
5206 bool due_to_interstitial,
5207 LoadNotificationDetails* details) {
Nate Chapin22ea6592019-03-05 22:29:025208 if (is_being_destroyed_)
5209 return;
clamy44e84ce2016-02-22 15:38:255210 // Do not send notifications about loading changes in the FrameTree while the
5211 // interstitial page is pausing the throbber.
creisadca9862017-06-16 19:12:115212 if (ShowingInterstitialPage() && interstitial_page_->pause_throbber() &&
clamy44e84ce2016-02-22 15:38:255213 !due_to_interstitial) {
5214 return;
5215 }
5216
5217 bool is_loading = IsLoading();
5218
5219 if (!is_loading) {
5220 load_state_ = net::LoadStateWithParam(net::LOAD_STATE_IDLE,
5221 base::string16());
5222 load_state_host_.clear();
5223 upload_size_ = 0;
5224 upload_position_ = 0;
5225 }
5226
5227 GetRenderManager()->SetIsLoading(is_loading);
5228
5229 waiting_for_response_ = is_loading;
5230 is_load_to_different_document_ = to_different_document;
5231
5232 if (delegate_)
5233 delegate_->LoadingStateChanged(this, to_different_document);
5234 NotifyNavigationStateChanged(INVALIDATE_TYPE_LOAD);
5235
5236 std::string url = (details ? details->url.possibly_invalid_spec() : "NULL");
5237 if (is_loading) {
5238 TRACE_EVENT_ASYNC_BEGIN2("browser,navigation", "WebContentsImpl Loading",
5239 this, "URL", url, "Main FrameTreeNode id",
5240 GetFrameTree()->root()->frame_tree_node_id());
ericwilligersde386342016-10-19 02:35:095241 for (auto& observer : observers_)
5242 observer.DidStartLoading();
clamy44e84ce2016-02-22 15:38:255243 } else {
5244 TRACE_EVENT_ASYNC_END1("browser,navigation", "WebContentsImpl Loading",
5245 this, "URL", url);
ericwilligersde386342016-10-19 02:35:095246 for (auto& observer : observers_)
5247 observer.DidStopLoading();
clamy44e84ce2016-02-22 15:38:255248 }
5249
5250 // TODO(avi): Remove. http://crbug.com/170921
5251 int type = is_loading ? NOTIFICATION_LOAD_START : NOTIFICATION_LOAD_STOP;
5252 NotificationDetails det = NotificationService::NoDetails();
5253 if (details)
5254 det = Details<LoadNotificationDetails>(details);
5255 NotificationService::current()->Notify(
5256 type, Source<NavigationController>(&controller_), det);
5257}
5258
[email protected]02d7b6e2014-06-24 21:01:505259void WebContentsImpl::NotifyViewSwapped(RenderViewHost* old_host,
5260 RenderViewHost* new_host) {
[email protected]420ae012009-04-24 05:16:325261 // After sending out a swap notification, we need to send a disconnect
5262 // notification so that clients that pick up a pointer to |this| can NULL the
5263 // pointer. See Bug 1230284.
5264 notify_disconnection_ = true;
ericwilligersde386342016-10-19 02:35:095265 for (auto& observer : observers_)
5266 observer.RenderViewHostChanged(old_host, new_host);
[email protected]da7a7182013-09-06 08:11:115267
Jinsuk Kimdc5284c22018-07-11 02:22:065268 view_->RenderViewHostChanged(old_host, new_host);
5269
Lucas Furukawa Gadanif9ea4fc2018-08-03 23:57:135270 // If this is an inner WebContents that has swapped views, we need to reattach
5271 // it to its outer WebContents.
5272 if (node_.outer_web_contents())
5273 ReattachToOuterWebContentsFrame();
5274
[email protected]7a846df2012-09-20 19:17:395275 // Ensure that the associated embedder gets cleared after a RenderViewHost
5276 // gets swapped, so we don't reuse the same embedder next time a
5277 // RenderViewHost is attached to this WebContents.
5278 RemoveBrowserPluginEmbedder();
[email protected]420ae012009-04-24 05:16:325279}
5280
[email protected]02d7b6e2014-06-24 21:01:505281void WebContentsImpl::NotifyFrameSwapped(RenderFrameHost* old_host,
Jinsuk Kim895b2c52018-07-03 06:49:165282 RenderFrameHost* new_host,
5283 bool is_main_frame) {
Bo Liu168c8642017-08-28 18:26:025284#if defined(OS_ANDROID)
Bo Liu16dd7b8322018-05-02 21:19:505285 // Copy importance from |old_host| if |new_host| is a main frame.
5286 if (old_host && !new_host->GetParent()) {
Bo Liu7c6779e92017-08-16 02:02:285287 static_cast<RenderFrameHostImpl*>(new_host)
5288 ->GetRenderWidgetHost()
Bo Liu16dd7b8322018-05-02 21:19:505289 ->SetImportance(static_cast<RenderFrameHostImpl*>(old_host)
5290 ->GetRenderWidgetHost()
5291 ->importance());
Bo Liu7c6779e92017-08-16 02:02:285292 }
Bo Liu168c8642017-08-28 18:26:025293#endif
ericwilligersde386342016-10-19 02:35:095294 for (auto& observer : observers_)
5295 observer.RenderFrameHostChanged(old_host, new_host);
[email protected]02d7b6e2014-06-24 21:01:505296}
5297
[email protected]da7a7182013-09-06 08:11:115298// TODO(avi): Remove this entire function because this notification is already
5299// covered by two observer functions. http://crbug.com/170921
[email protected]b172aee2012-04-10 17:05:265300void WebContentsImpl::NotifyDisconnected() {
[email protected]420ae012009-04-24 05:16:325301 if (!notify_disconnection_)
5302 return;
5303
5304 notify_disconnection_ = false;
[email protected]8ff00d72012-10-23 19:12:215305 NotificationService::current()->Notify(
5306 NOTIFICATION_WEB_CONTENTS_DISCONNECTED,
5307 Source<WebContents>(this),
5308 NotificationService::NoDetails());
[email protected]420ae012009-04-24 05:16:325309}
5310
[email protected]cbb1ef592013-06-05 19:49:465311void WebContentsImpl::NotifyNavigationEntryCommitted(
5312 const LoadCommittedDetails& load_details) {
ericwilligersde386342016-10-19 02:35:095313 for (auto& observer : observers_)
5314 observer.NavigationEntryCommitted(load_details);
[email protected]cbb1ef592013-06-05 19:49:465315}
5316
Sam McNally5c087a32017-08-25 01:46:145317void WebContentsImpl::NotifyNavigationEntryChanged(
5318 const EntryChangedDetails& change_details) {
5319 for (auto& observer : observers_)
5320 observer.NavigationEntryChanged(change_details);
5321}
5322
5323void WebContentsImpl::NotifyNavigationListPruned(
5324 const PrunedDetails& pruned_details) {
5325 for (auto& observer : observers_)
5326 observer.NavigationListPruned(pruned_details);
5327}
5328
Christian Dullweber1af31e62018-02-22 11:49:485329void WebContentsImpl::NotifyNavigationEntriesDeleted() {
5330 for (auto& observer : observers_)
5331 observer.NavigationEntriesDeleted();
5332}
5333
rockotf62002a2016-09-15 00:08:595334void WebContentsImpl::OnAssociatedInterfaceRequest(
5335 RenderFrameHost* render_frame_host,
5336 const std::string& interface_name,
5337 mojo::ScopedInterfaceEndpointHandle handle) {
Julie Jeongeun Kim7d0986172019-12-04 03:17:255338 auto it = receiver_sets_.find(interface_name);
5339 if (it != receiver_sets_.end())
5340 it->second->OnReceiverForFrame(render_frame_host, std::move(handle));
rockotf62002a2016-09-15 00:08:595341}
5342
Ben Goodger9e24929e2017-07-21 03:13:405343void WebContentsImpl::OnInterfaceRequest(
5344 RenderFrameHost* render_frame_host,
5345 const std::string& interface_name,
5346 mojo::ScopedMessagePipeHandle* interface_pipe) {
5347 for (auto& observer : observers_) {
5348 observer.OnInterfaceRequestFromFrame(render_frame_host, interface_name,
5349 interface_pipe);
5350 if (!interface_pipe->is_valid())
5351 break;
5352 }
5353}
5354
Varun Khaneja5da94302019-06-25 04:48:105355void WebContentsImpl::OnDidBlockNavigation(
5356 const GURL& blocked_url,
5357 const GURL& initiator_url,
5358 blink::NavigationBlockedReason reason) {
Nicolas Dossou-gbetebcab2472017-09-09 12:28:405359 if (delegate_)
Varun Khaneja5da94302019-06-25 04:48:105360 delegate_->OnDidBlockNavigation(this, blocked_url, initiator_url, reason);
Nicolas Dossou-gbetebcab2472017-09-09 12:28:405361}
5362
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:415363const GURL& WebContentsImpl::GetMainFrameLastCommittedURL() {
[email protected]c31a84802014-04-03 15:55:495364 return GetLastCommittedURL();
5365}
5366
[email protected]b849847b2013-12-10 21:57:585367void WebContentsImpl::RenderFrameCreated(RenderFrameHost* render_frame_host) {
ericwilligersde386342016-10-19 02:35:095368 for (auto& observer : observers_)
5369 observer.RenderFrameCreated(render_frame_host);
dmazzonif8a31042016-12-03 00:52:135370 UpdateAccessibilityModeOnFrame(render_frame_host);
clamy91b7abf2016-03-24 15:17:455371
Becca Hughesd11d6502018-07-31 17:01:285372 if (display_cutout_host_impl_)
5373 display_cutout_host_impl_->RenderFrameCreated(render_frame_host);
5374
clamy91b7abf2016-03-24 15:17:455375 if (!render_frame_host->IsRenderFrameLive() || render_frame_host->GetParent())
5376 return;
5377
5378 NavigationEntry* entry = controller_.GetPendingEntry();
5379 if (entry && entry->IsViewSourceMode()) {
5380 // Put the renderer in view source mode.
Dave Tapuska3c05e1c2019-11-27 18:34:185381 static_cast<RenderFrameHostImpl*>(render_frame_host)
5382 ->GetAssociatedLocalFrame()
5383 ->EnableViewSourceMode();
clamy91b7abf2016-03-24 15:17:455384 }
[email protected]b849847b2013-12-10 21:57:585385}
5386
5387void WebContentsImpl::RenderFrameDeleted(RenderFrameHost* render_frame_host) {
yilkal172da772019-11-06 22:49:075388 if (IsBeingDestroyed() && !render_frame_host->GetParent() &&
5389 first_navigation_completed_ &&
Rakina Zata Amni74627df2019-10-15 02:03:175390 !static_cast<RenderFrameHostImpl*>(render_frame_host)
5391 ->is_in_back_forward_cache()) {
yilkal796e89e2019-10-02 23:58:285392 // Main frame has been deleted because WebContents is being destroyed.
Rakina Zata Amni74627df2019-10-15 02:03:175393 // Note that we aren't recording this here when the main frame is in the
5394 // back-forward cache because that means we've actually already navigated
5395 // away from it (and we got to this point because the WebContents is
yilkal172da772019-11-06 22:49:075396 // deleted), which means |max_loaded_frame_count_| is already overwritten.
5397 // The |max_loaded_frame_count_| value will instead be recorded from within
5398 // |WebContentsImpl::DidFinishNavigation()|.
5399 RecordMaxFrameCountUMA(max_loaded_frame_count_);
yilkal796e89e2019-10-02 23:58:285400 }
5401
creis3638897452017-05-09 22:07:505402 is_notifying_observers_ = true;
ericwilligersde386342016-10-19 02:35:095403 for (auto& observer : observers_)
5404 observer.RenderFrameDeleted(render_frame_host);
creis3638897452017-05-09 22:07:505405 is_notifying_observers_ = false;
zqzhang7b6f4f02016-12-07 22:10:225406#if BUILDFLAG(ENABLE_PLUGINS)
5407 pepper_playback_observer_->RenderFrameDeleted(render_frame_host);
5408#endif
Becca Hughesfd5d8f892018-06-14 18:23:365409
Becca Hughesd11d6502018-07-31 17:01:285410 if (display_cutout_host_impl_)
5411 display_cutout_host_impl_->RenderFrameDeleted(render_frame_host);
5412
Becca Hughesfd5d8f892018-06-14 18:23:365413 // Remove any fullscreen state that the frame has stored.
5414 FullscreenStateChanged(render_frame_host, false /* is_fullscreen */);
[email protected]b849847b2013-12-10 21:57:585415}
5416
[email protected]a09d53ce2014-01-31 00:46:425417void WebContentsImpl::ShowContextMenu(RenderFrameHost* render_frame_host,
5418 const ContextMenuParams& params) {
avie9fd8722017-04-10 16:17:585419 // If a renderer fires off a second command to show a context menu before the
5420 // first context menu is closed, just ignore it. https://crbug.com/707534
ekaramadf6750aa2017-06-06 18:29:425421 if (showing_context_menu_)
avie9fd8722017-04-10 16:17:585422 return;
5423
[email protected]077e704b2014-05-23 19:24:105424 ContextMenuParams context_menu_params(params);
[email protected]a09d53ce2014-01-31 00:46:425425 // Allow WebContentsDelegates to handle the context menu operation first.
Joel Klinghed0ea22ea2019-02-20 22:04:225426 if (delegate_ &&
5427 delegate_->HandleContextMenu(render_frame_host, context_menu_params))
[email protected]a09d53ce2014-01-31 00:46:425428 return;
5429
[email protected]077e704b2014-05-23 19:24:105430 render_view_host_delegate_view_->ShowContextMenu(render_frame_host,
5431 context_menu_params);
[email protected]a09d53ce2014-01-31 00:46:425432}
5433
Avi Drissman4891b852018-09-25 18:12:585434namespace {
5435// Normalizes the line endings: \r\n -> \n, lone \r -> \n.
5436base::string16 NormalizeLineBreaks(const base::string16& source) {
5437 static const base::NoDestructor<base::string16> kReturnNewline(
5438 base::ASCIIToUTF16("\r\n"));
5439 static const base::NoDestructor<base::string16> kReturn(
5440 base::ASCIIToUTF16("\r"));
5441 static const base::NoDestructor<base::string16> kNewline(
5442 base::ASCIIToUTF16("\n"));
5443
5444 std::vector<base::StringPiece16> pieces;
5445
5446 for (const auto& rn_line : base::SplitStringPieceUsingSubstr(
5447 source, *kReturnNewline, base::KEEP_WHITESPACE,
5448 base::SPLIT_WANT_ALL)) {
5449 auto r_lines = base::SplitStringPieceUsingSubstr(
5450 rn_line, *kReturn, base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL);
5451 std::move(std::begin(r_lines), std::end(r_lines),
5452 std::back_inserter(pieces));
5453 }
5454
5455 return base::JoinString(pieces, *kNewline);
5456}
5457} // namespace
5458
avi777ff452017-02-09 19:04:485459void WebContentsImpl::RunJavaScriptDialog(RenderFrameHost* render_frame_host,
5460 const base::string16& message,
5461 const base::string16& default_prompt,
avi777ff452017-02-09 19:04:485462 JavaScriptDialogType dialog_type,
5463 IPC::Message* reply_msg) {
Avi Drissman2ccbb402018-03-20 21:11:365464 // Ensure that if showing a dialog is the first thing that a page does, that
5465 // the contents of the previous page aren't shown behind it. This is required
5466 // because showing a dialog freezes the renderer, so no frames will be coming
5467 // from it. https://crbug.com/823353
5468 auto* render_widget_host_impl =
5469 static_cast<RenderFrameHostImpl*>(render_frame_host)
5470 ->GetRenderWidgetHost();
5471 if (render_widget_host_impl)
5472 render_widget_host_impl->ForceFirstFrameAfterNavigationTimeout();
5473
avi0720b02e2017-06-13 03:22:135474 // Running a dialog causes an exit to webpage-initiated fullscreen.
5475 // http://crbug.com/728276
Avi Drissmand18c51972018-08-15 18:32:095476 ForSecurityDropFullscreen();
avi0720b02e2017-06-13 03:22:135477
Avi Drissmane04d3992017-10-05 15:11:365478 auto callback =
5479 base::BindOnce(&WebContentsImpl::OnDialogClosed, base::Unretained(this),
5480 render_frame_host->GetProcess()->GetID(),
5481 render_frame_host->GetRoutingID(), reply_msg);
Pavel Feldman3c1842b2017-08-02 05:00:165482
Pavel Feldman3c1842b2017-08-02 05:00:165483 std::vector<protocol::PageHandler*> page_handlers =
5484 protocol::PageHandler::EnabledForWebContents(this);
5485
Pavel Feldmanfab3cb62017-10-05 01:02:015486 if (delegate_)
5487 dialog_manager_ = delegate_->GetJavaScriptDialogManager(this);
5488
Charlie Reis96077362019-07-04 00:34:045489 // While a JS message dialog is showing, defer commits in this WebContents.
5490 javascript_dialog_navigation_deferrer_ =
5491 std::make_unique<JavaScriptDialogNavigationDeferrer>();
5492
Pavel Feldmanfab3cb62017-10-05 01:02:015493 // Suppress JavaScript dialogs when requested. Also suppress messages when
5494 // showing an interstitial as it's shown over the previous page and we don't
5495 // want the hidden page's dialogs to interfere with the interstitial.
5496 bool should_suppress = ShowingInterstitialPage() ||
5497 (delegate_ && delegate_->ShouldSuppressDialogs(this));
Pavel Feldman1c587bc52018-04-04 21:16:055498 bool has_non_devtools_handlers = delegate_ && dialog_manager_;
5499 bool has_handlers = page_handlers.size() || has_non_devtools_handlers;
Pavel Feldmanfab3cb62017-10-05 01:02:015500 bool suppress_this_message = should_suppress || !has_handlers;
5501
5502 if (suppress_this_message) {
Avi Drissmane04d3992017-10-05 15:11:365503 std::move(callback).Run(true, false, base::string16());
Pavel Feldman3c1842b2017-08-02 05:00:165504 return;
5505 }
5506
5507 scoped_refptr<CloseDialogCallbackWrapper> wrapper =
Avi Drissmane04d3992017-10-05 15:11:365508 new CloseDialogCallbackWrapper(std::move(callback));
Pavel Feldman3c1842b2017-08-02 05:00:165509
5510 is_showing_javascript_dialog_ = true;
5511
Avi Drissman4891b852018-09-25 18:12:585512 base::string16 normalized_message = NormalizeLineBreaks(message);
5513
Pavel Feldman3c1842b2017-08-02 05:00:165514 for (auto* handler : page_handlers) {
Avi Drissmane04d3992017-10-05 15:11:365515 handler->DidRunJavaScriptDialog(
Avi Drissman4891b852018-09-25 18:12:585516 render_frame_host->GetLastCommittedURL(), normalized_message,
5517 default_prompt, dialog_type, has_non_devtools_handlers,
Avi Drissmane04d3992017-10-05 15:11:365518 base::BindOnce(&CloseDialogCallbackWrapper::Run, wrapper, false));
Pavel Feldman3c1842b2017-08-02 05:00:165519 }
5520
5521 if (dialog_manager_) {
[email protected]87de04b02014-04-08 22:14:495522 dialog_manager_->RunJavaScriptDialog(
Avi Drissman4891b852018-09-25 18:12:585523 this, render_frame_host, dialog_type, normalized_message,
5524 default_prompt,
Avi Drissmane04d3992017-10-05 15:11:365525 base::BindOnce(&CloseDialogCallbackWrapper::Run, wrapper, false),
5526 &suppress_this_message);
[email protected]87de04b02014-04-08 22:14:495527 }
5528
5529 if (suppress_this_message) {
5530 // If we are suppressing messages, just reply as if the user immediately
5531 // pressed "Cancel", passing true to |dialog_was_suppressed|.
Avi Drissmane04d3992017-10-05 15:11:365532 wrapper->Run(true, false, base::string16());
[email protected]87de04b02014-04-08 22:14:495533 }
[email protected]87de04b02014-04-08 22:14:495534}
5535
5536void WebContentsImpl::RunBeforeUnloadConfirm(
[email protected]39f2f9252014-05-03 00:54:015537 RenderFrameHost* render_frame_host,
[email protected]87de04b02014-04-08 22:14:495538 bool is_reload,
5539 IPC::Message* reply_msg) {
Avi Drissman2ccbb402018-03-20 21:11:365540 // Ensure that if showing a dialog is the first thing that a page does, that
5541 // the contents of the previous page aren't shown behind it. This is required
5542 // because showing a dialog freezes the renderer, so no frames will be coming
5543 // from it. https://crbug.com/823353
5544 auto* render_widget_host_impl =
5545 static_cast<RenderFrameHostImpl*>(render_frame_host)
5546 ->GetRenderWidgetHost();
5547 if (render_widget_host_impl)
5548 render_widget_host_impl->ForceFirstFrameAfterNavigationTimeout();
5549
avi0720b02e2017-06-13 03:22:135550 // Running a dialog causes an exit to webpage-initiated fullscreen.
5551 // http://crbug.com/728276
Avi Drissmand18c51972018-08-15 18:32:095552 ForSecurityDropFullscreen();
avi0720b02e2017-06-13 03:22:135553
[email protected]39f2f9252014-05-03 00:54:015554 RenderFrameHostImpl* rfhi =
5555 static_cast<RenderFrameHostImpl*>(render_frame_host);
[email protected]87de04b02014-04-08 22:14:495556 if (delegate_)
5557 delegate_->WillRunBeforeUnloadConfirm();
5558
Avi Drissmane04d3992017-10-05 15:11:365559 auto callback =
5560 base::BindOnce(&WebContentsImpl::OnDialogClosed, base::Unretained(this),
5561 render_frame_host->GetProcess()->GetID(),
5562 render_frame_host->GetRoutingID(), reply_msg);
Pavel Feldman3c1842b2017-08-02 05:00:165563
Pavel Feldman3c1842b2017-08-02 05:00:165564 std::vector<protocol::PageHandler*> page_handlers =
5565 protocol::PageHandler::EnabledForWebContents(this);
5566
Pavel Feldmanfab3cb62017-10-05 01:02:015567 if (delegate_)
5568 dialog_manager_ = delegate_->GetJavaScriptDialogManager(this);
5569
Charlie Reis96077362019-07-04 00:34:045570 // While a JS beforeunload dialog is showing, defer commits in this
5571 // WebContents.
5572 javascript_dialog_navigation_deferrer_ =
5573 std::make_unique<JavaScriptDialogNavigationDeferrer>();
5574
Pavel Feldmanfab3cb62017-10-05 01:02:015575 bool should_suppress = ShowingInterstitialPage() || !rfhi->is_active() ||
5576 (delegate_ && delegate_->ShouldSuppressDialogs(this));
Pavel Feldman1c587bc52018-04-04 21:16:055577 bool has_non_devtools_handlers = delegate_ && dialog_manager_;
5578 bool has_handlers = page_handlers.size() || has_non_devtools_handlers;
Pavel Feldmanfab3cb62017-10-05 01:02:015579 if (should_suppress || !has_handlers) {
Avi Drissmane04d3992017-10-05 15:11:365580 std::move(callback).Run(false, true, base::string16());
[email protected]87de04b02014-04-08 22:14:495581 return;
5582 }
5583
5584 is_showing_before_unload_dialog_ = true;
Pavel Feldman3c1842b2017-08-02 05:00:165585
5586 scoped_refptr<CloseDialogCallbackWrapper> wrapper =
Avi Drissmane04d3992017-10-05 15:11:365587 new CloseDialogCallbackWrapper(std::move(callback));
Pavel Feldman3c1842b2017-08-02 05:00:165588
5589 GURL frame_url = rfhi->GetLastCommittedURL();
5590 for (auto* handler : page_handlers) {
Avi Drissmane04d3992017-10-05 15:11:365591 handler->DidRunBeforeUnloadConfirm(
Pavel Feldman1c587bc52018-04-04 21:16:055592 frame_url, has_non_devtools_handlers,
Avi Drissmane04d3992017-10-05 15:11:365593 base::BindOnce(&CloseDialogCallbackWrapper::Run, wrapper, false));
Pavel Feldman3c1842b2017-08-02 05:00:165594 }
5595
5596 if (dialog_manager_) {
Avi Drissmane04d3992017-10-05 15:11:365597 dialog_manager_->RunBeforeUnloadDialog(
Avi Drissmanff9ed752017-11-28 21:18:125598 this, render_frame_host, is_reload,
Avi Drissmane04d3992017-10-05 15:11:365599 base::BindOnce(&CloseDialogCallbackWrapper::Run, wrapper, false));
Pavel Feldman3c1842b2017-08-02 05:00:165600 }
[email protected]87de04b02014-04-08 22:14:495601}
5602
Kent Tamurac260c9a2018-09-14 09:10:565603void WebContentsImpl::RunFileChooser(
5604 RenderFrameHost* render_frame_host,
Kent Tamura512a27e2018-10-04 00:49:325605 std::unique_ptr<content::FileSelectListener> listener,
Kent Tamurac260c9a2018-09-14 09:10:565606 const blink::mojom::FileChooserParams& params) {
Avi Drissman3d41e7712018-08-27 21:18:085607 // Any explicit focusing of another window while this WebContents is in
5608 // fullscreen can be used to confuse the user, so drop fullscreen.
5609 ForSecurityDropFullscreen();
5610
naskoada75b22016-06-11 16:09:465611 if (delegate_)
Kent Tamura512a27e2018-10-04 00:49:325612 delegate_->RunFileChooser(render_frame_host, std::move(listener), params);
5613 else
5614 listener->FileSelectionCanceled();
naskoada75b22016-06-11 16:09:465615}
5616
[email protected]a86c0e962013-12-17 17:10:395617WebContents* WebContentsImpl::GetAsWebContents() {
5618 return this;
5619}
5620
mcnee336ea2c2017-05-23 22:50:595621#if !defined(OS_ANDROID)
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:415622double WebContentsImpl::GetPendingPageZoomLevel() {
wjmaclean64951902016-04-29 20:59:125623 NavigationEntry* pending_entry = GetController().GetPendingEntry();
5624 if (!pending_entry)
5625 return HostZoomMap::GetZoomLevel(this);
5626
5627 GURL url = pending_entry->GetURL();
5628 return HostZoomMap::GetForWebContents(this)->GetZoomLevelForHostAndScheme(
5629 url.scheme(), net::GetHostOrSpecFromURL(url));
5630}
mcnee336ea2c2017-05-23 22:50:595631#endif // !defined(OS_ANDROID)
wjmaclean64951902016-04-29 20:59:125632
qinmin72e8bd02016-10-21 19:35:375633bool WebContentsImpl::HideDownloadUI() const {
5634 return is_overlay_content_;
5635}
5636
mlamouri5cd9ae82017-02-18 11:05:095637bool WebContentsImpl::HasPersistentVideo() const {
5638 return has_persistent_video_;
5639}
5640
Ian Vollick5d5c37f2019-06-19 22:50:575641bool WebContentsImpl::IsSpatialNavigationDisabled() const {
5642 return is_spatial_navigation_disabled_;
5643}
5644
Albert J. Wong65fe64d2019-09-20 02:48:145645RenderFrameHostImpl* WebContentsImpl::GetPendingMainFrame() {
clamy610c63b32017-12-22 15:05:185646 return GetRenderManager()->speculative_frame_host();
Alex Moshchuk27caae82017-09-11 23:11:185647}
5648
mlamourie52c1b542017-02-23 14:57:505649bool WebContentsImpl::HasActiveEffectivelyFullscreenVideo() const {
5650 return media_web_contents_observer_->HasActiveEffectivelyFullscreenVideo();
5651}
5652
François Beaufortad6c5232018-02-26 11:00:445653bool WebContentsImpl::IsPictureInPictureAllowedForFullscreenVideo() const {
5654 return media_web_contents_observer_
5655 ->IsPictureInPictureAllowedForFullscreenVideo();
5656}
5657
ekaramada110f642016-12-21 19:47:285658bool WebContentsImpl::IsFocusedElementEditable() {
5659 RenderFrameHostImpl* frame = GetFocusedFrame();
5660 return frame && frame->has_focused_editable_element();
5661}
5662
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:415663bool WebContentsImpl::IsShowingContextMenu() {
ekaramadf6750aa2017-06-06 18:29:425664 return showing_context_menu_;
5665}
5666
5667void WebContentsImpl::SetShowingContextMenu(bool showing) {
5668 DCHECK_NE(showing_context_menu_, showing);
5669 showing_context_menu_ = showing;
5670
5671 if (auto* view = GetRenderWidgetHostView()) {
5672 // Notify the main frame's RWHV to run the platform-specific code, if any.
5673 static_cast<RenderWidgetHostViewBase*>(view)->SetShowingContextMenu(
5674 showing);
5675 }
5676}
5677
ekaramada110f642016-12-21 19:47:285678void WebContentsImpl::ClearFocusedElement() {
5679 if (auto* frame = GetFocusedFrame())
5680 frame->ClearFocusedElement();
5681}
5682
[email protected]7912e822014-04-16 02:37:035683bool WebContentsImpl::IsNeverVisible() {
5684 if (!delegate_)
5685 return false;
5686 return delegate_->IsNeverVisible(this);
5687}
5688
[email protected]5a3bdf52012-05-24 15:12:575689RenderViewHostDelegateView* WebContentsImpl::GetDelegateView() {
5690 return render_view_host_delegate_view_;
[email protected]420ae012009-04-24 05:16:325691}
5692
Leon Hanc819dc62019-01-28 04:30:195693blink::mojom::RendererPreferences WebContentsImpl::GetRendererPrefs(
[email protected]8ff00d72012-10-23 19:12:215694 BrowserContext* browser_context) const {
[email protected]840b1512009-07-21 16:53:465695 return renderer_preferences_;
[email protected]80d96fa2009-06-10 22:34:515696}
5697
Lucas Furukawa Gadani2ec00c82018-12-14 15:53:165698RenderFrameHostImpl* WebContentsImpl::GetOuterWebContentsFrame() {
5699 if (GetOuterDelegateFrameTreeNodeId() ==
5700 FrameTreeNode::kFrameTreeNodeInvalidId) {
5701 return nullptr;
5702 }
5703
5704 FrameTreeNode* outer_node =
5705 FrameTreeNode::GloballyFindByID(GetOuterDelegateFrameTreeNodeId());
5706 // The outer node should be in the outer WebContents.
5707 DCHECK_EQ(outer_node->frame_tree(), GetOuterWebContents()->GetFrameTree());
5708 return outer_node->parent()->current_frame_host();
5709}
5710
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:415711WebContentsImpl* WebContentsImpl::GetOuterWebContents() {
Lucas Furukawa Gadani105de1e812018-05-31 19:38:395712 return node_.outer_web_contents();
avallee0206f782016-07-28 18:55:335713}
5714
Lucas Furukawa Gadani2ec00c82018-12-14 15:53:165715std::vector<WebContents*> WebContentsImpl::GetInnerWebContents() {
5716 std::vector<WebContents*> all_inner_contents;
Lucas Furukawa Gadani2ec00c82018-12-14 15:53:165717 const auto& inner_contents = node_.GetInnerWebContents();
5718 all_inner_contents.insert(all_inner_contents.end(), inner_contents.begin(),
5719 inner_contents.end());
5720 return all_inner_contents;
5721}
5722
avallee0206f782016-07-28 18:55:335723WebContentsImpl* WebContentsImpl::GetFocusedWebContents() {
lfg601233692017-03-06 22:45:445724 return GetOutermostWebContents()->node_.focused_web_contents();
avallee0206f782016-07-28 18:55:335725}
5726
avallee9993fca2016-11-17 06:16:505727bool WebContentsImpl::ContainsOrIsFocusedWebContents() {
5728 for (WebContentsImpl* focused_contents = GetFocusedWebContents();
5729 focused_contents;
5730 focused_contents = focused_contents->GetOuterWebContents()) {
5731 if (focused_contents == this)
5732 return true;
5733 }
5734
5735 return false;
5736}
5737
W. James MacLean9437b5bf2019-12-06 17:33:445738void WebContentsImpl::RemoveBrowserPluginEmbedder() {
5739 browser_plugin_embedder_.reset();
5740}
5741
avallee0206f782016-07-28 18:55:335742WebContentsImpl* WebContentsImpl::GetOutermostWebContents() {
5743 WebContentsImpl* root = this;
5744 while (root->GetOuterWebContents())
5745 root = root->GetOuterWebContents();
5746 return root;
5747}
5748
avalleed68cdd12017-06-21 22:07:275749void WebContentsImpl::FocusOuterAttachmentFrameChain() {
5750 WebContentsImpl* outer_contents = GetOuterWebContents();
5751 if (!outer_contents)
5752 return;
5753
5754 FrameTreeNode* outer_node =
5755 FrameTreeNode::GloballyFindByID(GetOuterDelegateFrameTreeNodeId());
5756 outer_contents->frame_tree_.SetFocusedFrame(outer_node, nullptr);
5757
5758 // For a browser initiated focus change, let embedding renderer know of the
5759 // change. Otherwise, if the currently focused element is just across a
5760 // process boundary in focus order, it will not be possible to move across
5761 // that boundary. This is because the target element will already be focused
5762 // (that renderer was not notified) and drop the event.
5763 if (GetRenderManager()->GetProxyToOuterDelegate())
5764 GetRenderManager()->GetProxyToOuterDelegate()->SetFocusedFrame();
5765
5766 outer_contents->FocusOuterAttachmentFrameChain();
5767}
5768
Sean Toppingbbc4a2bd2019-01-30 02:16:145769void WebContentsImpl::InnerWebContentsCreated(WebContents* inner_web_contents) {
Shivani Sharma985474f2019-05-23 21:36:475770 had_inner_webcontents_ = true;
Sean Toppingbbc4a2bd2019-01-30 02:16:145771 for (auto& observer : observers_)
5772 observer.InnerWebContentsCreated(inner_web_contents);
5773}
5774
[email protected]b172aee2012-04-10 17:05:265775void WebContentsImpl::RenderViewCreated(RenderViewHost* render_view_host) {
[email protected]86f98a22013-03-20 14:35:005776 if (delegate_)
[email protected]e85165c642014-06-10 14:34:315777 view_->SetOverscrollControllerEnabled(CanOverscrollContent());
[email protected]a6b73c62013-02-11 23:05:085778
[email protected]8ff00d72012-10-23 19:12:215779 NotificationService::current()->Notify(
5780 NOTIFICATION_WEB_CONTENTS_RENDER_VIEW_HOST_CREATED,
5781 Source<WebContents>(this),
5782 Details<RenderViewHost>(render_view_host));
[email protected]420ae012009-04-24 05:16:325783
[email protected]60780f412013-02-25 16:34:105784 view_->RenderViewCreated(render_view_host);
[email protected]11f764bb2011-05-25 19:45:265785
ericwilligersde386342016-10-19 02:35:095786 for (auto& observer : observers_)
5787 observer.RenderViewCreated(render_view_host);
Joel Einbinder848001c2017-09-19 21:39:315788 if (delegate_)
5789 RenderFrameDevToolsAgentHost::WebContentsCreated(this);
[email protected]420ae012009-04-24 05:16:325790}
5791
[email protected]b172aee2012-04-10 17:05:265792void WebContentsImpl::RenderViewReady(RenderViewHost* rvh) {
[email protected]151a63d2011-12-20 22:32:525793 if (rvh != GetRenderViewHost()) {
[email protected]420ae012009-04-24 05:16:325794 // Don't notify the world, since this came from a renderer in the
5795 // background.
5796 return;
5797 }
5798
dmazzoni62111452017-04-28 23:46:555799 RenderWidgetHostViewBase* rwhv =
5800 static_cast<RenderWidgetHostViewBase*>(GetRenderWidgetHostView());
5801 if (rwhv)
5802 rwhv->SetMainFrameAXTreeID(GetMainFrame()->GetAXTreeID());
5803
[email protected]da7a7182013-09-06 08:11:115804 notify_disconnection_ = true;
[email protected]da7a7182013-09-06 08:11:115805
[email protected]be1f56ab2011-12-22 06:55:315806 bool was_crashed = IsCrashed();
[email protected]443b80e2010-12-14 00:42:235807 SetIsCrashed(base::TERMINATION_STATUS_STILL_RUNNING, 0);
[email protected]9d8a4642009-07-29 17:25:305808
5809 // Restore the focus to the tab (otherwise the focus will be on the top
5810 // window).
[email protected]484ae5912010-09-29 19:16:145811 if (was_crashed && !FocusLocationBarByDefault() &&
5812 (!delegate_ || delegate_->ShouldFocusPageAfterCrash())) {
[email protected]f3615f02013-02-26 06:09:065813 view_->Focus();
[email protected]484ae5912010-09-29 19:16:145814 }
[email protected]32ded2212011-11-10 18:51:435815
ericwilligersde386342016-10-19 02:35:095816 for (auto& observer : observers_)
5817 observer.RenderViewReady();
Jinsuk Kim895b2c52018-07-03 06:49:165818
5819 view_->RenderViewReady();
[email protected]420ae012009-04-24 05:16:325820}
5821
[email protected]ec6a7eb2013-04-22 17:34:225822void WebContentsImpl::RenderViewTerminated(RenderViewHost* rvh,
5823 base::TerminationStatus status,
5824 int error_code) {
[email protected]151a63d2011-12-20 22:32:525825 if (rvh != GetRenderViewHost()) {
[email protected]420ae012009-04-24 05:16:325826 // The pending page's RenderViewHost is gone.
5827 return;
5828 }
5829
[email protected]2fcdcc32014-03-05 02:14:075830 // Ensure fullscreen mode is exited in the |delegate_| since a crashed
5831 // renderer may not have made a clean exit.
alexmos5dd617d2016-06-07 04:21:155832 if (IsFullscreenForCurrentTab())
bokanece34a82016-01-28 19:49:465833 ExitFullscreenMode(false);
[email protected]2fcdcc32014-03-05 02:14:075834
Jennifer Apacibleb4b972a2018-06-07 04:41:025835 // Ensure any video in Picture-in-Picture is exited in the |delegate_| since
5836 // a crashed renderer may not have made a clean exit.
5837 if (HasPictureInPictureVideo())
5838 ExitPictureInPicture();
5839
[email protected]698191dc2014-02-25 01:06:135840 // Cancel any visible dialogs so they are not left dangling over the sad tab.
creis89a0f782015-05-27 16:13:175841 CancelActiveAndPendingDialogs();
[email protected]698191dc2014-02-25 01:06:135842
dalecurtis3d1e1302017-02-20 22:33:255843 audio_stream_monitor_.RenderProcessGone(rvh->GetProcess()->GetID());
5844
[email protected]960b0372014-05-19 18:01:005845 // Reset the loading progress. TODO(avi): What does it mean to have a
5846 // "renderer crash" when there is more than one renderer process serving a
5847 // webpage? Once this function is called at a more granular frame level, we
5848 // probably will need to more granularly reset the state here.
5849 ResetLoadProgressState();
clamy44e84ce2016-02-22 15:38:255850 NotifyDisconnected();
5851 SetIsCrashed(status, error_code);
[email protected]960b0372014-05-19 18:01:005852
ericwilligersde386342016-10-19 02:35:095853 for (auto& observer : observers_)
5854 observer.RenderProcessGone(GetCrashedStatus());
[email protected]420ae012009-04-24 05:16:325855}
5856
[email protected]b172aee2012-04-10 17:05:265857void WebContentsImpl::RenderViewDeleted(RenderViewHost* rvh) {
ericwilligersde386342016-10-19 02:35:095858 for (auto& observer : observers_)
5859 observer.RenderViewDeleted(rvh);
[email protected]2e4633c2009-07-09 16:58:065860}
5861
sky8b00392d2015-01-10 00:30:285862void WebContentsImpl::UpdateTargetURL(RenderViewHost* render_view_host,
5863 const GURL& url) {
5864 if (fullscreen_widget_routing_id_ != MSG_ROUTING_NONE) {
Lukasz Anforowiczfd707142018-02-07 19:46:135865 // If we're in flash fullscreen (i.e. Pepper plugin fullscreen) only update
5866 // the url if it's from the fullscreen renderer.
sky8b00392d2015-01-10 00:30:285867 RenderWidgetHostView* fs = GetFullscreenRenderWidgetHostView();
avif9ab5d942015-10-15 14:05:445868 if (fs && fs->GetRenderWidgetHost() != render_view_host->GetWidget())
sky8b00392d2015-01-10 00:30:285869 return;
5870 }
Lukasz Anforowiczfd707142018-02-07 19:46:135871
5872 // In case of racey updates from multiple RenderViewHosts, the last URL should
5873 // be shown - see also some discussion in https://crbug.com/807776.
5874 if (!url.is_valid() && render_view_host != view_that_set_last_target_url_)
5875 return;
5876 view_that_set_last_target_url_ = url.is_valid() ? render_view_host : nullptr;
5877
[email protected]6934a702011-12-20 00:04:515878 if (delegate_)
avi450ce00e2014-09-24 01:58:525879 delegate_->UpdateTargetURL(this, url);
[email protected]420ae012009-04-24 05:16:325880}
5881
Lukasz Anforowiczfd707142018-02-07 19:46:135882void WebContentsImpl::ClearTargetURL() {
5883 view_that_set_last_target_url_ = nullptr;
5884 if (delegate_)
5885 delegate_->UpdateTargetURL(this, GURL());
5886}
5887
[email protected]b172aee2012-04-10 17:05:265888void WebContentsImpl::Close(RenderViewHost* rvh) {
[email protected]60780f412013-02-25 16:34:105889#if defined(OS_MACOSX)
[email protected]07707302009-11-06 00:50:295890 // The UI may be in an event-tracking loop, such as between the
5891 // mouse-down and mouse-up in text selection or a button click.
5892 // Defer the close until after tracking is complete, so that we
5893 // don't free objects out from under the UI.
[email protected]07707302009-11-06 00:50:295894 // TODO(shess): This could get more fine-grained. For instance,
5895 // closing a tab in another window while selecting text in the
5896 // current window's Omnibox should be just fine.
Christopher Cameron9ae31e0d2019-03-01 20:01:195897 if (view_->CloseTabAfterEventTrackingIfNeeded())
[email protected]07707302009-11-06 00:50:295898 return;
[email protected]60780f412013-02-25 16:34:105899#endif
[email protected]07707302009-11-06 00:50:295900
[email protected]420ae012009-04-24 05:16:325901 // Ignore this if it comes from a RenderViewHost that we aren't showing.
[email protected]151a63d2011-12-20 22:32:525902 if (delegate_ && rvh == GetRenderViewHost())
[email protected]6934a702011-12-20 00:04:515903 delegate_->CloseContents(this);
[email protected]420ae012009-04-24 05:16:325904}
5905
Avi Drissman014dec72018-06-07 02:34:015906void WebContentsImpl::RequestSetBounds(const gfx::Rect& new_bounds) {
5907 if (delegate_)
5908 delegate_->SetContentsBounds(this, new_bounds);
[email protected]420ae012009-04-24 05:16:325909}
5910
fdegansa696e5112015-04-17 01:57:595911void WebContentsImpl::DidStartLoading(FrameTreeNode* frame_tree_node,
[email protected]e3b10d12014-03-28 16:06:095912 bool to_different_document) {
Evan Stadec3b26022017-07-14 19:50:245913 LoadingStateChanged(frame_tree_node->IsMainFrame() && to_different_document,
5914 false, nullptr);
dmazzonibf8cec42015-02-08 08:28:085915
Xiyuan Xia2a66b112018-12-06 15:52:165916 // Reset the focus state from DidStartNavigation to false if a new load starts
5917 // afterward, in case loading logic triggers a FocusLocationBarByDefault call.
5918 should_focus_location_bar_by_default_ = false;
[email protected]420ae012009-04-24 05:16:325919}
5920
fdegansc73eec22015-03-19 14:47:545921void WebContentsImpl::DidStopLoading() {
dcheng59716272016-04-09 05:19:085922 std::unique_ptr<LoadNotificationDetails> details;
[email protected]420ae012009-04-24 05:16:325923
[email protected]9595fd82013-04-19 21:28:495924 // Use the last committed entry rather than the active one, in case a
5925 // pending entry has been created.
5926 NavigationEntry* entry = controller_.GetLastCommittedEntry();
[email protected]3691e5cf2014-01-22 10:16:205927 Navigator* navigator = frame_tree_.root()->navigator();
[email protected]9595fd82013-04-19 21:28:495928
[email protected]420ae012009-04-24 05:16:325929 // An entry may not exist for a stop when loading an initial blank page or
5930 // if an iframe injected by script into a blank page finishes loading.
5931 if (entry) {
[email protected]3691e5cf2014-01-22 10:16:205932 base::TimeDelta elapsed =
5933 base::TimeTicks::Now() - navigator->GetCurrentLoadStart();
[email protected]420ae012009-04-24 05:16:325934
5935 details.reset(new LoadNotificationDetails(
[email protected]36fc0392011-12-25 03:59:515936 entry->GetVirtualURL(),
[email protected]420ae012009-04-24 05:16:325937 elapsed,
5938 &controller_,
5939 controller_.GetCurrentEntryIndex()));
5940 }
5941
clamy44e84ce2016-02-22 15:38:255942 LoadingStateChanged(true, false, details.get());
[email protected]420ae012009-04-24 05:16:325943}
5944
fdegansa696e5112015-04-17 01:57:595945void WebContentsImpl::DidChangeLoadProgress() {
Nate Chapin22ea6592019-03-05 22:29:025946 if (is_being_destroyed_)
5947 return;
fdegansa696e5112015-04-17 01:57:595948 double load_progress = frame_tree_.load_progress();
5949
5950 // The delegate is notified immediately for the first and last updates. Also,
5951 // since the message loop may be pretty busy when a page is loaded, it might
5952 // not execute a posted task in a timely manner so the progress report is sent
5953 // immediately if enough time has passed.
5954 base::TimeDelta min_delay =
5955 base::TimeDelta::FromMilliseconds(kMinimumDelayBetweenLoadingUpdatesMS);
5956 bool delay_elapsed = loading_last_progress_update_.is_null() ||
5957 base::TimeTicks::Now() - loading_last_progress_update_ > min_delay;
5958
5959 if (load_progress == 0.0 || load_progress == 1.0 || delay_elapsed) {
5960 // If there is a pending task to send progress, it is now obsolete.
5961 loading_weak_factory_.InvalidateWeakPtrs();
5962
5963 // Notify the load progress change.
5964 SendChangeLoadProgress();
5965
5966 // Clean-up the states if needed.
5967 if (load_progress == 1.0)
5968 ResetLoadProgressState();
5969 return;
5970 }
5971
5972 if (loading_weak_factory_.HasWeakPtrs())
5973 return;
5974
skyostil95082a62015-06-05 19:53:075975 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
tzike2aca992017-09-05 08:50:545976 FROM_HERE,
5977 base::BindOnce(&WebContentsImpl::SendChangeLoadProgress,
5978 loading_weak_factory_.GetWeakPtr()),
fdegansa696e5112015-04-17 01:57:595979 min_delay);
5980}
5981
avid6d88b912017-01-13 00:16:005982std::vector<std::unique_ptr<NavigationThrottle>>
5983WebContentsImpl::CreateThrottlesForNavigation(
clamycbf524d42016-09-27 12:48:355984 NavigationHandle* navigation_handle) {
Charlie Reis96077362019-07-04 00:34:045985 auto throttles = GetContentClient()->browser()->CreateThrottlesForNavigation(
clamycbf524d42016-09-27 12:48:355986 navigation_handle);
Charlie Reis96077362019-07-04 00:34:045987
5988 // This is not a normal place to be adding a throttle. However, in the case
5989 // javascript dialogs, related logic is present in the web_contents/ layer,
5990 // and the purpose of the throttle is to ensure that navigation commits are
5991 // deferred for the entire WebContents. Most throttles are either added by
5992 // the embederrer outside of content/, or are per-frame and added by
5993 // NavigationThrottleRunner.
5994 std::unique_ptr<content::NavigationThrottle> dialog_throttle =
5995 JavaScriptDialogNavigationThrottle::MaybeCreateThrottleFor(
5996 navigation_handle);
5997 if (dialog_throttle)
5998 throttles.push_back(std::move(dialog_throttle));
5999
6000 return throttles;
clamycbf524d42016-09-27 12:48:356001}
6002
clamy1e5574e2016-09-29 16:48:446003std::unique_ptr<NavigationUIData> WebContentsImpl::GetNavigationUIData(
6004 NavigationHandle* navigation_handle) {
clamy1e5574e2016-09-29 16:48:446005 return GetContentClient()->browser()->GetNavigationUIData(navigation_handle);
6006}
6007
pdr15f5b5b2017-06-20 00:58:006008void WebContentsImpl::DidCancelLoading() {
6009 controller_.DiscardNonCommittedEntries();
6010
6011 // Update the URL display.
6012 NotifyNavigationStateChanged(INVALIDATE_TYPE_URL);
6013}
6014
[email protected]59167c22013-06-03 18:07:326015void WebContentsImpl::DidAccessInitialDocument() {
[email protected]b4c84012014-04-28 19:51:106016 has_accessed_initial_document_ = true;
6017
[email protected]d1371bf8e2014-04-19 03:26:246018 // We may have left a failed browser-initiated navigation in the address bar
6019 // to let the user edit it and try again. Clear it now that content might
6020 // show up underneath it.
6021 if (!IsLoading() && controller_.GetPendingEntry())
avi45a72532015-04-07 21:01:456022 controller_.DiscardPendingEntry(false);
[email protected]d1371bf8e2014-04-19 03:26:246023
[email protected]59167c22013-06-03 18:07:326024 // Update the URL display.
thestigc4cac8f2014-09-04 21:17:506025 NotifyNavigationStateChanged(INVALIDATE_TYPE_URL);
[email protected]59167c22013-06-03 18:07:326026}
6027
alexmosbe2f4c32015-03-10 02:30:236028void WebContentsImpl::DidChangeName(RenderFrameHost* render_frame_host,
6029 const std::string& name) {
ericwilligersde386342016-10-19 02:35:096030 for (auto& observer : observers_)
6031 observer.FrameNameChanged(render_frame_host, name);
alexmosbe2f4c32015-03-10 02:30:236032}
6033
John Delaneyedd8d6c2019-01-25 00:23:576034void WebContentsImpl::DidReceiveFirstUserActivation(
6035 RenderFrameHost* render_frame_host) {
6036 for (auto& observer : observers_)
6037 observer.FrameReceivedFirstUserActivation(render_frame_host);
6038}
6039
John Delaney0146a742019-01-25 19:25:286040void WebContentsImpl::DidChangeDisplayState(RenderFrameHost* render_frame_host,
6041 bool is_display_none) {
6042 for (auto& observer : observers_)
6043 observer.FrameDisplayStateChanged(render_frame_host, is_display_none);
6044}
6045
6046void WebContentsImpl::FrameSizeChanged(RenderFrameHost* render_frame_host,
6047 const gfx::Size& frame_size) {
6048 for (auto& observer : observers_)
6049 observer.FrameSizeChanged(render_frame_host, frame_size);
6050}
6051
[email protected]39f2f9252014-05-03 00:54:016052void WebContentsImpl::DocumentOnLoadCompleted(
[email protected]50279492014-05-05 23:24:296053 RenderFrameHost* render_frame_host) {
falken52a56e32016-12-08 05:02:406054 ShowInsecureLocalhostWarningIfNeeded();
6055
Alexander Timindd0fa0c52019-11-06 13:03:376056 GetRenderViewHost()->DocumentOnLoadCompletedInMainFrame();
6057
creis97867aa2017-02-17 18:39:456058 is_notifying_observers_ = true;
ericwilligersde386342016-10-19 02:35:096059 for (auto& observer : observers_)
6060 observer.DocumentOnLoadCompletedInMainFrame();
creis97867aa2017-02-17 18:39:456061 is_notifying_observers_ = false;
[email protected]6578fef2013-10-14 02:35:096062
6063 // TODO(avi): Remove. http://crbug.com/170921
[email protected]8ff00d72012-10-23 19:12:216064 NotificationService::current()->Notify(
6065 NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
6066 Source<WebContents>(this),
[email protected]50279492014-05-05 23:24:296067 NotificationService::NoDetails());
[email protected]25497492010-09-11 15:15:086068}
6069
creis8eb8ef22015-11-04 22:51:386070void WebContentsImpl::UpdateStateForFrame(RenderFrameHost* render_frame_host,
6071 const PageState& page_state) {
creis8eb8ef22015-11-04 22:51:386072 // The state update affects the last NavigationEntry associated with the given
6073 // |render_frame_host|. This may not be the last committed NavigationEntry (as
6074 // in the case of an UpdateState from a frame being swapped out). We track
6075 // which entry this is in the RenderFrameHost's nav_entry_id.
6076 RenderFrameHostImpl* rfhi =
6077 static_cast<RenderFrameHostImpl*>(render_frame_host);
6078 NavigationEntryImpl* entry =
6079 controller_.GetEntryWithUniqueID(rfhi->nav_entry_id());
6080 if (!entry)
6081 return;
6082
6083 FrameNavigationEntry* frame_entry =
6084 entry->GetFrameEntry(rfhi->frame_tree_node());
6085 if (!frame_entry)
6086 return;
6087
creisd06a9422015-11-11 03:08:456088 // The SiteInstance might not match if we do a cross-process navigation with
6089 // replacement (e.g., auto-subframe), in which case the swap out of the old
6090 // RenderFrameHost runs in the background after the old FrameNavigationEntry
6091 // has already been replaced and destroyed.
6092 if (frame_entry->site_instance() != rfhi->GetSiteInstance())
6093 return;
6094
creis8eb8ef22015-11-04 22:51:386095 if (page_state == frame_entry->page_state())
6096 return; // Nothing to update.
6097
creis0cade2e2017-02-28 06:37:476098 DCHECK(page_state.IsValid()) << "Shouldn't set an empty PageState.";
nasko34634672016-07-29 18:46:066099
6100 // The document_sequence_number and item_sequence_number recorded in the
6101 // FrameNavigationEntry should not differ from the one coming with the update,
6102 // since it must come from the same document. Do not update it if a difference
6103 // is detected, as this indicates that |frame_entry| is not the correct one.
6104 ExplodedPageState exploded_state;
6105 if (!DecodePageState(page_state.ToEncodedData(), &exploded_state))
6106 return;
6107
6108 if (exploded_state.top.document_sequence_number !=
6109 frame_entry->document_sequence_number() ||
6110 exploded_state.top.item_sequence_number !=
6111 frame_entry->item_sequence_number()) {
6112 return;
6113 }
6114
nasko04a322f25a2016-09-01 00:00:346115 frame_entry->SetPageState(page_state);
creis8eb8ef22015-11-04 22:51:386116 controller_.NotifyEntryChanged(entry);
6117}
6118
[email protected]ef3adfc2014-05-11 00:04:546119void WebContentsImpl::UpdateTitle(RenderFrameHost* render_frame_host,
[email protected]ef3adfc2014-05-11 00:04:546120 const base::string16& title,
6121 base::i18n::TextDirection title_direction) {
[email protected]ef3adfc2014-05-11 00:04:546122 // Try to find the navigation entry, which might not be the current one.
avi286019ce2015-07-29 01:26:586123 // For example, it might be from a recently swapped out RFH.
avi1edc8552016-10-13 21:37:166124 NavigationEntryImpl* entry = controller_.GetEntryWithUniqueID(
creisad8c31a2016-05-24 17:07:046125 static_cast<RenderFrameHostImpl*>(render_frame_host)->nav_entry_id());
[email protected]ef3adfc2014-05-11 00:04:546126
6127 // We can handle title updates when we don't have an entry in
6128 // UpdateTitleForEntry, but only if the update is from the current RVH.
6129 // TODO(avi): Change to make decisions based on the RenderFrameHost.
avi286019ce2015-07-29 01:26:586130 if (!entry && render_frame_host != GetMainFrame())
[email protected]ef3adfc2014-05-11 00:04:546131 return;
6132
6133 // TODO(evan): make use of title_direction.
6134 // http://code.google.com/p/chromium/issues/detail?id=27094
afakhry6f0c1ec22016-07-14 13:55:136135 UpdateTitleForEntry(entry, title);
[email protected]ef3adfc2014-05-11 00:04:546136}
6137
[email protected]39f2f9252014-05-03 00:54:016138void WebContentsImpl::DocumentAvailableInMainFrame(
6139 RenderViewHost* render_view_host) {
ericwilligersde386342016-10-19 02:35:096140 for (auto& observer : observers_)
6141 observer.DocumentAvailableInMainFrame();
[email protected]39f2f9252014-05-03 00:54:016142}
naskoc0fceff2015-04-30 15:53:526143
[email protected]e1c3a552012-05-04 20:51:326144void WebContentsImpl::RouteCloseEvent(RenderViewHost* rvh) {
6145 // Tell the active RenderViewHost to run unload handlers and close, as long
6146 // as the request came from a RenderViewHost in the same BrowsingInstance.
6147 // In most cases, we receive this from a swapped out RenderViewHost.
6148 // It is possible to receive it from one that has just been swapped in,
6149 // in which case we might as well deliver the message anyway.
6150 if (rvh->GetSiteInstance()->IsRelatedSiteInstance(GetSiteInstance()))
naskoc0fceff2015-04-30 15:53:526151 ClosePage();
[email protected]e1c3a552012-05-04 20:51:326152}
6153
alexmose7da5a12015-04-09 02:22:166154bool WebContentsImpl::ShouldRouteMessageEvent(
6155 RenderFrameHost* target_rfh,
6156 SiteInstance* source_site_instance) const {
6157 // Allow the message if this WebContents is dedicated to a browser plugin
6158 // guest.
[email protected]0eba810b2012-10-18 03:19:366159 // Note: This check means that an embedder could theoretically receive a
6160 // postMessage from anyone (not just its own guests). However, this is
6161 // probably not a risk for apps since other pages won't have references
6162 // to App windows.
alexmose7da5a12015-04-09 02:22:166163 return GetBrowserPluginGuest() || GetBrowserPluginEmbedder();
6164}
[email protected]f546640b2012-05-15 00:03:496165
alexmos58729042015-06-18 23:20:006166void WebContentsImpl::EnsureOpenerProxiesExist(RenderFrameHost* source_rfh) {
alexmose7da5a12015-04-09 02:22:166167 WebContentsImpl* source_web_contents = static_cast<WebContentsImpl*>(
6168 WebContents::FromRenderFrameHost(source_rfh));
[email protected]f546640b2012-05-15 00:03:496169
alexmose7da5a12015-04-09 02:22:166170 if (source_web_contents) {
lazyboy6ec48b2a2015-06-29 15:18:146171 // If this message is going to outer WebContents from inner WebContents,
6172 // then we should not create a RenderView. AttachToOuterWebContentsFrame()
6173 // already created a RenderFrameProxyHost for that purpose.
6174 if (GetBrowserPluginEmbedder() &&
W. James MacLean9437b5bf2019-12-06 17:33:446175 GuestMode::IsCrossProcessFrameGuest(source_web_contents))
lazyboy6ec48b2a2015-06-29 15:18:146176 return;
lazyboy6ec48b2a2015-06-29 15:18:146177
lfg812874d2016-02-09 22:26:136178 if (this != source_web_contents && GetBrowserPluginGuest()) {
naskob3041b98a42016-03-12 04:43:066179 // We create a RenderFrameProxyHost for the embedder in the guest's render
6180 // process but we intentionally do not expose the embedder's opener chain
6181 // to it.
6182 source_web_contents->GetRenderManager()->CreateRenderFrameProxy(
6183 GetSiteInstance());
[email protected]f546640b2012-05-15 00:03:496184 } else {
alexmos58729042015-06-18 23:20:006185 RenderFrameHostImpl* source_rfhi =
6186 static_cast<RenderFrameHostImpl*>(source_rfh);
6187 source_rfhi->frame_tree_node()->render_manager()->CreateOpenerProxies(
alexmos90325cf2015-09-02 17:18:396188 GetSiteInstance(), nullptr);
[email protected]f546640b2012-05-15 00:03:496189 }
6190 }
[email protected]f546640b2012-05-15 00:03:496191}
6192
avallee9993fca2016-11-17 06:16:506193void WebContentsImpl::SetAsFocusedWebContentsIfNecessary() {
6194 // Only change focus if we are not currently focused.
6195 WebContentsImpl* old_contents = GetFocusedWebContents();
6196 if (old_contents == this)
6197 return;
6198
paulmeyerfeafc2d2017-04-25 21:46:406199 GetOutermostWebContents()->node_.SetFocusedWebContents(this);
6200
avallee9993fca2016-11-17 06:16:506201 // Send a page level blur to the old contents so that it displays inactive UI
6202 // and focus this contents to activate it.
6203 if (old_contents)
6204 old_contents->GetMainFrame()->GetRenderWidgetHost()->SetPageFocus(false);
avalleec2ff1ef2017-01-21 00:12:306205
6206 // Make sure the outer web contents knows our frame is focused. Otherwise, the
6207 // outer renderer could have the element before or after the frame element
6208 // focused which would return early without actually advancing focus.
avalleed68cdd12017-06-21 22:07:276209 FocusOuterAttachmentFrameChain();
avalleec2ff1ef2017-01-21 00:12:306210
lfg1453e412017-04-11 00:48:506211 if (ShowingInterstitialPage()) {
creisadca9862017-06-16 19:12:116212 static_cast<RenderFrameHostImpl*>(interstitial_page_->GetMainFrame())
lfg1453e412017-04-11 00:48:506213 ->GetRenderWidgetHost()
6214 ->SetPageFocus(true);
6215 } else {
6216 GetMainFrame()->GetRenderWidgetHost()->SetPageFocus(true);
6217 }
avallee9993fca2016-11-17 06:16:506218}
6219
avallee0206f782016-07-28 18:55:336220void WebContentsImpl::SetFocusedFrame(FrameTreeNode* node,
6221 SiteInstance* source) {
avallee0206f782016-07-28 18:55:336222 frame_tree_.SetFocusedFrame(node, source);
avalleed68cdd12017-06-21 22:07:276223
Lucas Furukawa Gadaniabbfa342017-08-29 19:37:576224 if (auto* inner_contents = node_.GetInnerWebContentsInFrame(node)) {
6225 // |this| is an outer WebContents and |node| represents an inner
6226 // WebContents. Transfer the focus to the inner contents if |this| is
6227 // focused.
6228 if (GetFocusedWebContents() == this)
6229 inner_contents->SetAsFocusedWebContentsIfNecessary();
6230 } else if (node_.OuterContentsFrameTreeNode() &&
6231 node_.OuterContentsFrameTreeNode()
6232 ->current_frame_host()
6233 ->GetSiteInstance() == source) {
6234 // |this| is an inner WebContents, |node| is its main FrameTreeNode and
6235 // the outer WebContents FrameTreeNode is at |source|'s SiteInstance.
6236 // Transfer the focus to the inner WebContents if the outer WebContents is
6237 // focused. This branch is used when an inner WebContents is focused through
Lowell Mannersa9530382019-08-01 09:46:576238 // its RenderFrameProxyHost (via FrameFocused mojo call, used to
Lucas Furukawa Gadaniabbfa342017-08-29 19:37:576239 // implement the window.focus() API).
6240 if (GetFocusedWebContents() == GetOuterWebContents())
6241 SetAsFocusedWebContentsIfNecessary();
6242 } else if (!GetOuterWebContents()) {
6243 // This is an outermost WebContents.
6244 SetAsFocusedWebContentsIfNecessary();
6245 } else if (!GuestMode::IsCrossProcessFrameGuest(this) &&
6246 GetOuterWebContents()) {
6247 // TODO(lfg, paulmeyer): Allows BrowserPlugins to set themselves as the
6248 // focused WebContents. This works around a bug in FindRequestManager that
6249 // doesn't support properly traversing BrowserPlugins.
6250 SetAsFocusedWebContentsIfNecessary();
6251 }
avalleed68cdd12017-06-21 22:07:276252}
6253
Avi Drissman36f801f2018-02-01 20:06:046254void WebContentsImpl::DidCallFocus() {
6255 // Any explicit focusing of another window while this WebContents is in
6256 // fullscreen can be used to confuse the user, so drop fullscreen.
Avi Drissmand18c51972018-08-15 18:32:096257 ForSecurityDropFullscreen();
Avi Drissman36f801f2018-02-01 20:06:046258}
6259
avalleed68cdd12017-06-21 22:07:276260RenderFrameHost* WebContentsImpl::GetFocusedFrameIncludingInnerWebContents() {
6261 WebContentsImpl* contents = this;
6262 FrameTreeNode* focused_node = contents->frame_tree_.GetFocusedFrame();
6263
6264 // If there is no focused frame in the outer WebContents, we need to return
6265 // null.
6266 if (!focused_node)
6267 return nullptr;
6268
6269 // If the focused frame is embedding an inner WebContents, we must descend
6270 // into that contents. If the current WebContents does not have a focused
6271 // frame, return the main frame of this contents instead of the focused empty
6272 // frame embedding this contents.
6273 while (true) {
6274 contents = contents->node_.GetInnerWebContentsInFrame(focused_node);
6275 if (!contents)
6276 return focused_node->current_frame_host();
6277
6278 focused_node = contents->frame_tree_.GetFocusedFrame();
6279 if (!focused_node)
6280 return contents->GetMainFrame();
6281 }
avallee0206f782016-07-28 18:55:336282}
6283
Lucas Furukawa Gadaniabbfa342017-08-29 19:37:576284void WebContentsImpl::OnAdvanceFocus(RenderFrameHostImpl* source_rfh) {
6285 // When a RenderFrame needs to advance focus to a RenderFrameProxy (by hitting
6286 // TAB), the RenderFrameProxy sends an IPC to RenderFrameProxyHost. When this
6287 // RenderFrameProxyHost represents an inner WebContents, the outer WebContents
6288 // needs to focus the inner WebContents.
6289 if (GetOuterWebContents() &&
6290 GetOuterWebContents() == source_rfh->delegate()->GetAsWebContents() &&
6291 GetFocusedWebContents() == GetOuterWebContents()) {
6292 SetAsFocusedWebContentsIfNecessary();
6293 }
6294}
6295
ekaramada110f642016-12-21 19:47:286296void WebContentsImpl::OnFocusedElementChangedInFrame(
6297 RenderFrameHostImpl* frame,
6298 const gfx::Rect& bounds_in_root_view) {
6299 RenderWidgetHostViewBase* root_view =
6300 static_cast<RenderWidgetHostViewBase*>(GetRenderWidgetHostView());
6301 if (!root_view || !frame->GetView())
6302 return;
6303
6304 // Converting to screen coordinates.
6305 gfx::Point origin = bounds_in_root_view.origin();
6306 origin += root_view->GetViewBounds().OffsetFromOrigin();
6307 gfx::Rect bounds_in_screen(origin, bounds_in_root_view.size());
6308
6309 root_view->FocusedNodeChanged(frame->has_focused_editable_element(),
6310 bounds_in_screen);
6311
6312 FocusedNodeDetails details = {frame->has_focused_editable_element(),
6313 bounds_in_screen};
6314
6315 // TODO(ekaramad): We should replace this with an observer notification
6316 // (https://crbug.com/675975).
6317 NotificationService::current()->Notify(
6318 NOTIFICATION_FOCUS_CHANGED_IN_PAGE,
6319 Source<RenderViewHost>(GetRenderViewHost()),
6320 Details<FocusedNodeDetails>(&details));
Jeff Fisher709b4c52019-07-29 20:07:346321
6322 for (auto& observer : observers_)
6323 observer.OnFocusChangedInPage(&details);
ekaramada110f642016-12-21 19:47:286324}
6325
Lowell Manners1de5242e2019-04-25 10:18:466326bool WebContentsImpl::DidAddMessageToConsole(
6327 blink::mojom::ConsoleMessageLevel log_level,
6328 const base::string16& message,
6329 int32_t line_no,
6330 const base::string16& source_id) {
[email protected]a796f202012-05-30 14:14:256331 if (!delegate_)
6332 return false;
Lowell Manners1de5242e2019-04-25 10:18:466333 return delegate_->DidAddMessageToConsole(this, log_level, message, line_no,
avia90ae4e2016-11-11 20:49:336334 source_id);
[email protected]a796f202012-05-30 14:14:256335}
6336
Daniel Cheng90196c82018-04-25 21:49:146337void WebContentsImpl::DidReceiveInputEvent(
dominickn9c7ceaa32016-02-27 01:52:536338 RenderWidgetHostImpl* render_widget_host,
6339 const blink::WebInputEvent::Type type) {
Dominick Ng37a15052018-07-17 23:56:356340 if (!IsUserInteractionInputType(type))
Daniel Cheng90196c82018-04-25 21:49:146341 return;
6342
kenrb19221852016-04-29 17:21:406343 // Ignore unless the widget is currently in the frame tree.
6344 if (!HasMatchingWidgetHost(&frame_tree_, render_widget_host))
avic3aa8422015-11-09 20:57:226345 return;
6346
Daniel Cheng90196c82018-04-25 21:49:146347 if (type != blink::WebInputEvent::kGestureScrollBegin)
6348 last_interactive_input_event_time_ = ui::EventTimeForNow();
dominickn9c7ceaa32016-02-27 01:52:536349
Dominick Ng37a15052018-07-17 23:56:356350 for (auto& observer : observers_)
6351 observer.DidGetUserInteraction(type);
dominickn2dd142dd2015-10-29 05:30:506352}
6353
Avi Drissman738ea192018-08-29 20:24:166354bool WebContentsImpl::ShouldIgnoreInputEvents() {
6355 WebContentsImpl* web_contents = this;
6356 while (web_contents) {
6357 if (web_contents->ignore_input_events_)
6358 return true;
6359 web_contents = web_contents->GetOuterWebContents();
6360 }
6361
6362 return false;
6363}
6364
avallee9993fca2016-11-17 06:16:506365void WebContentsImpl::FocusOwningWebContents(
6366 RenderWidgetHostImpl* render_widget_host) {
Alex Moshchukc4f0a722019-11-19 22:41:286367 RenderWidgetHostImpl* main_frame_widget_host =
6368 GetMainFrame()->GetRenderWidgetHost();
avallee9993fca2016-11-17 06:16:506369 RenderWidgetHostImpl* focused_widget =
Alex Moshchukc4f0a722019-11-19 22:41:286370 GetFocusedRenderWidgetHost(main_frame_widget_host);
avallee9993fca2016-11-17 06:16:506371
6372 if (focused_widget != render_widget_host &&
avallee8e881592016-11-19 00:39:306373 (!focused_widget ||
6374 focused_widget->delegate() != render_widget_host->delegate())) {
avallee9993fca2016-11-17 06:16:506375 SetAsFocusedWebContentsIfNecessary();
6376 }
6377}
6378
[email protected]b172aee2012-04-10 17:05:266379void WebContentsImpl::OnIgnoredUIEvent() {
[email protected]31145092011-09-30 01:49:446380 // Notify observers.
ericwilligersde386342016-10-19 02:35:096381 for (auto& observer : observers_)
6382 observer.DidGetIgnoredUIEvent();
[email protected]fa1cf0b82010-01-15 21:49:446383}
6384
avic3aa8422015-11-09 20:57:226385void WebContentsImpl::RendererUnresponsive(
Lukasz Anforowicz52b93722018-06-20 16:11:396386 RenderWidgetHostImpl* render_widget_host,
6387 base::RepeatingClosure hang_monitor_restarter) {
avic031d392017-03-03 03:09:426388 if (ShouldIgnoreUnresponsiveRenderer())
[email protected]e5fc1632011-08-08 07:51:536389 return;
6390
Lukasz Anforowicz4f0593d2018-09-21 18:33:546391 // Do not report hangs (to task manager, to hang renderer dialog, etc.) for
6392 // invisible tabs (like extension background page, background tabs). See
6393 // https://crbug.com/881812 for rationale and for choosing the visibility
6394 // (rather than process priority) as the signal here.
6395 if (GetVisibility() != Visibility::VISIBLE)
6396 return;
6397
Avi Drissman8920def2018-01-31 19:53:366398 if (!render_widget_host->renderer_initialized())
[email protected]55452902011-06-01 21:57:476399 return;
6400
Lukasz Anforowicz4f0593d2018-09-21 18:33:546401 for (auto& observer : observers_)
6402 observer.OnRendererUnresponsive(render_widget_host->GetProcess());
6403
Dave Tapuskac6146c52017-12-20 22:48:156404 if (delegate_)
Lukasz Anforowicz52b93722018-06-20 16:11:396405 delegate_->RendererUnresponsive(this, render_widget_host,
6406 std::move(hang_monitor_restarter));
[email protected]420ae012009-04-24 05:16:326407}
6408
avic3aa8422015-11-09 20:57:226409void WebContentsImpl::RendererResponsive(
6410 RenderWidgetHostImpl* render_widget_host) {
[email protected]6934a702011-12-20 00:04:516411 if (delegate_)
Avi Drissman1572e8c3c2018-02-02 19:06:366412 delegate_->RendererResponsive(this, render_widget_host);
[email protected]420ae012009-04-24 05:16:326413}
6414
Alex Moshchuk7b4f0652019-05-30 18:54:416415void WebContentsImpl::SubframeCrashed(
6416 blink::mojom::FrameVisibility visibility) {
6417 // If a subframe crashed on a hidden tab, mark the tab for reload to avoid
6418 // showing a sad frame to the user if they ever switch back to that tab. Do
6419 // this for subframes that are either visible in viewport or visible but
6420 // scrolled out of view, but skip subframes that are not rendered (e.g., via
6421 // "display:none"), since in that case the user wouldn't see a sad frame
6422 // anyway.
6423 bool did_mark_for_reload = false;
6424 if (IsHidden() && visibility != blink::mojom::FrameVisibility::kNotRendered &&
6425 base::FeatureList::IsEnabled(
6426 features::kReloadHiddenTabsWithCrashedSubframes)) {
6427 controller_.SetNeedsReload(
6428 NavigationControllerImpl::NeedsReloadType::kCrashedSubframe);
6429 did_mark_for_reload = true;
6430 UMA_HISTOGRAM_ENUMERATION(
6431 "Stability.ChildFrameCrash.TabMarkedForReload.Visibility", visibility);
6432 }
6433
6434 UMA_HISTOGRAM_BOOLEAN("Stability.ChildFrameCrash.TabMarkedForReload",
6435 did_mark_for_reload);
6436}
6437
[email protected]b172aee2012-04-10 17:05:266438void WebContentsImpl::BeforeUnloadFiredFromRenderManager(
[email protected]feaded02012-09-25 19:12:556439 bool proceed, const base::TimeTicks& proceed_time,
[email protected]420ae012009-04-24 05:16:326440 bool* proceed_to_fire_unload) {
ericwilligersde386342016-10-19 02:35:096441 for (auto& observer : observers_)
Chris Hamilton3b3e315d2018-09-19 13:16:216442 observer.BeforeUnloadFired(proceed, proceed_time);
[email protected]6934a702011-12-20 00:04:516443 if (delegate_)
6444 delegate_->BeforeUnloadFired(this, proceed, proceed_to_fire_unload);
[email protected]e80af492013-06-24 21:52:096445 // Note: |this| might be deleted at this point.
[email protected]420ae012009-04-24 05:16:326446}
6447
[email protected]58d5cfe2013-07-10 02:40:526448void WebContentsImpl::RenderProcessGoneFromRenderManager(
[email protected]3a3d47472010-07-15 21:03:546449 RenderViewHost* render_view_host) {
[email protected]443b80e2010-12-14 00:42:236450 DCHECK(crashed_status_ != base::TERMINATION_STATUS_STILL_RUNNING);
[email protected]ec6a7eb2013-04-22 17:34:226451 RenderViewTerminated(render_view_host, crashed_status_, crashed_error_code_);
[email protected]3a3d47472010-07-15 21:03:546452}
6453
Jianzhou Fengd8720c72018-01-18 03:06:306454void WebContentsImpl::UpdateRenderViewSizeForRenderManager(bool is_main_frame) {
[email protected]8643e6d2012-01-18 20:26:106455 // TODO(brettw) this is a hack. See WebContentsView::SizeContents.
Jianzhou Fengd8720c72018-01-18 03:06:306456 gfx::Size size = GetSizeForNewRenderView(is_main_frame);
[email protected]0323ee42010-02-17 22:03:266457 // 0x0 isn't a valid window size (minimal window size is 1x1) but it may be
6458 // here during container initialization and normal window size will be set
6459 // later. In case of tab duplication this resizing to 0x0 prevents setting
6460 // normal size later so just ignore it.
6461 if (!size.IsEmpty())
6462 view_->SizeContents(size);
[email protected]420ae012009-04-24 05:16:326463}
6464
[email protected]af905902013-10-01 21:38:516465void WebContentsImpl::CancelModalDialogsForRenderManager() {
6466 // We need to cancel modal dialogs when doing a process swap, since the load
avi2460c762015-04-17 15:21:546467 // deferrer would prevent us from swapping out. We also clear the state
6468 // because this is a cross-process navigation, which means that it's a new
6469 // site that should not have to pay for the sins of its predecessor.
creis89a0f782015-05-27 16:13:176470 //
Lei Zhang6410ec1a2019-01-31 21:38:586471 // Note that we don't bother telling |browser_plugin_embedder_| because the
creis89a0f782015-05-27 16:13:176472 // cross-process navigation will either destroy the browser plugins or not
6473 // require their dialogs to close.
avi5d3b8692016-10-12 22:00:466474 if (dialog_manager_) {
avi6879fcf2017-01-21 05:27:536475 dialog_manager_->CancelDialogs(this, /*reset_state=*/true);
avi5d3b8692016-10-12 22:00:466476 }
[email protected]af905902013-10-01 21:38:516477}
6478
[email protected]02d7b6e2014-06-24 21:01:506479void WebContentsImpl::NotifySwappedFromRenderManager(RenderFrameHost* old_host,
6480 RenderFrameHost* new_host,
6481 bool is_main_frame) {
6482 if (is_main_frame) {
arthursonzognif97bca512019-10-10 11:32:586483 NotifyViewSwapped(old_host ? old_host->GetRenderViewHost() : nullptr,
6484 new_host->GetRenderViewHost());
[email protected]a6b73c62013-02-11 23:05:086485
[email protected]02d7b6e2014-06-24 21:01:506486 // Make sure the visible RVH reflects the new delegate's preferences.
6487 if (delegate_)
6488 view_->SetOverscrollControllerEnabled(CanOverscrollContent());
[email protected]d56a49e2013-02-23 00:47:396489
dmazzoni62111452017-04-28 23:46:556490 RenderWidgetHostViewBase* rwhv =
6491 static_cast<RenderWidgetHostViewBase*>(GetRenderWidgetHostView());
6492 if (rwhv)
6493 rwhv->SetMainFrameAXTreeID(GetMainFrame()->GetAXTreeID());
[email protected]02d7b6e2014-06-24 21:01:506494 }
6495
Jinsuk Kim895b2c52018-07-03 06:49:166496 NotifyFrameSwapped(old_host, new_host, is_main_frame);
[email protected]3a3d47472010-07-15 21:03:546497}
6498
naskof5940b9f2015-03-02 23:04:056499void WebContentsImpl::NotifyMainFrameSwappedFromRenderManager(
Jinsuk Kim895b2c52018-07-03 06:49:166500 RenderFrameHost* old_host,
6501 RenderFrameHost* new_host) {
6502 NotifyViewSwapped(old_host ? old_host->GetRenderViewHost() : nullptr,
6503 new_host->GetRenderViewHost());
naskof5940b9f2015-03-02 23:04:056504}
6505
[email protected]b172aee2012-04-10 17:05:266506NavigationControllerImpl& WebContentsImpl::GetControllerForRenderManager() {
[email protected]d1198fd2012-08-13 22:50:196507 return GetController();
[email protected]3a3d47472010-07-15 21:03:546508}
6509
dcheng59716272016-04-09 05:19:086510std::unique_ptr<WebUIImpl> WebContentsImpl::CreateWebUIForRenderFrameHost(
carlosk37f793792014-11-27 15:08:296511 const GURL& url) {
Dan Beam055fe8f2017-10-04 23:23:366512 return CreateWebUI(url);
[email protected]420ae012009-04-24 05:16:326513}
6514
creisadca9862017-06-16 19:12:116515InterstitialPageImpl* WebContentsImpl::GetInterstitialForRenderManager() {
6516 return interstitial_page_;
6517}
6518
lfgbede6392015-09-11 21:54:066519void WebContentsImpl::CreateRenderWidgetHostViewForRenderManager(
6520 RenderViewHost* render_view_host) {
lfgc740d4b22016-04-15 16:45:336521 RenderWidgetHostViewBase* rwh_view =
W. James MacLean13d834d2019-12-04 16:06:456522 view_->CreateViewForWidget(render_view_host->GetWidget());
danakjf616f2252019-09-18 21:44:096523 rwh_view->SetSize(GetSizeForNewRenderView(true));
lfgbede6392015-09-11 21:54:066524}
6525
6526bool WebContentsImpl::CreateRenderViewForRenderManager(
6527 RenderViewHost* render_view_host,
6528 int opener_frame_routing_id,
6529 int proxy_routing_id,
Pavel Feldman25234722017-10-11 02:49:066530 const base::UnguessableToken& devtools_frame_token,
lfgbede6392015-09-11 21:54:066531 const FrameReplicationState& replicated_frame_state) {
6532 TRACE_EVENT0("browser,navigation",
6533 "WebContentsImpl::CreateRenderViewForRenderManager");
6534
piman00cf9402017-04-03 17:09:206535 if (proxy_routing_id == MSG_ROUTING_NONE)
lfg9431efd2016-12-14 14:45:266536 CreateRenderWidgetHostViewForRenderManager(render_view_host);
lfg9431efd2016-12-14 14:45:266537
piman00cf9402017-04-03 17:09:206538 if (!static_cast<RenderViewHostImpl*>(render_view_host)
6539 ->CreateRenderView(opener_frame_routing_id, proxy_routing_id,
Pavel Feldman25234722017-10-11 02:49:066540 devtools_frame_token, replicated_frame_state,
6541 created_with_opener_)) {
[email protected]a4127722011-04-27 23:13:526542 return false;
[email protected]14392a52012-05-02 20:28:446543 }
W. James MacLean62198672019-06-04 16:46:106544 // Set the TextAutosizer state from the main frame's renderer on the new view,
6545 // but only if it's not for the main frame. Main frame renderers should create
6546 // this state themselves from up-to-date values, so we shouldn't override it
6547 // with the cached values.
6548 if (!render_view_host->GetMainFrame()) {
6549 render_view_host->Send(
6550 new PageMsg_UpdateTextAutosizerPageInfoForRemoteMainFrames(
6551 render_view_host->GetRoutingID(), text_autosizer_page_info_));
6552 }
[email protected]a4127722011-04-27 23:13:526553
Lucas Furukawa Gadaniaed1fed2017-10-13 17:34:146554 if (proxy_routing_id == MSG_ROUTING_NONE && node_.outer_web_contents())
6555 ReattachToOuterWebContentsFrame();
6556
avi2b177592014-12-10 02:08:026557 SetHistoryOffsetAndLengthForView(render_view_host,
6558 controller_.GetLastCommittedEntryIndex(),
6559 controller_.GetEntryCount());
6560
[email protected]a220b5932013-09-21 03:47:446561#if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID)
[email protected]f8233cc2011-05-31 20:24:506562 // Force a ViewMsg_Resize to be sent, needed to make plugins show up on
6563 // linux. See crbug.com/83941.
avif9ab5d942015-10-15 14:05:446564 RenderWidgetHostView* rwh_view = render_view_host->GetWidget()->GetView();
[email protected]245f7d52011-11-28 15:36:446565 if (rwh_view) {
6566 if (RenderWidgetHost* render_widget_host = rwh_view->GetRenderWidgetHost())
Fady Samuel0b911822018-04-25 13:22:166567 render_widget_host->SynchronizeVisualProperties();
[email protected]245f7d52011-11-28 15:36:446568 }
[email protected]f8233cc2011-05-31 20:24:506569#endif
6570
[email protected]420ae012009-04-24 05:16:326571 return true;
6572}
6573
[email protected]82307f6b2014-08-07 03:30:126574bool WebContentsImpl::CreateRenderFrameForRenderManager(
6575 RenderFrameHost* render_frame_host,
arthursonzogni1efc7b962019-03-14 09:36:336576 int previous_routing_id,
alexmosa181efc2015-09-03 00:39:046577 int opener_routing_id,
japhete6adf142014-10-31 00:01:496578 int parent_routing_id,
alexmosa181efc2015-09-03 00:39:046579 int previous_sibling_routing_id) {
naskob8744d22014-08-28 17:07:436580 TRACE_EVENT0("browser,navigation",
6581 "WebContentsImpl::CreateRenderFrameForRenderManager");
[email protected]82307f6b2014-08-07 03:30:126582
6583 RenderFrameHostImpl* rfh =
6584 static_cast<RenderFrameHostImpl*>(render_frame_host);
arthursonzogni1efc7b962019-03-14 09:36:336585 if (!rfh->CreateRenderFrame(previous_routing_id, opener_routing_id,
alexmosa181efc2015-09-03 00:39:046586 parent_routing_id, previous_sibling_routing_id))
[email protected]82307f6b2014-08-07 03:30:126587 return false;
6588
6589 // TODO(nasko): When RenderWidgetHost is owned by RenderFrameHost, the passed
6590 // RenderFrameHost will have to be associated with the appropriate
6591 // RenderWidgetHostView or a new one should be created here.
6592
6593 return true;
6594}
6595
mfomitchev2b8b066a2016-01-28 19:23:156596#if defined(OS_ANDROID)
[email protected]fc2b46b2014-05-03 16:33:456597
[email protected]155c7f22013-12-09 17:07:186598base::android::ScopedJavaLocalRef<jobject>
6599WebContentsImpl::GetJavaWebContents() {
mostynb042582e2015-03-16 22:13:406600 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]0e813a52014-08-13 10:34:566601 return GetWebContentsAndroid()->GetJavaObject();
6602}
[email protected]155c7f22013-12-09 17:07:186603
[email protected]0e813a52014-08-13 10:34:566604WebContentsAndroid* WebContentsImpl::GetWebContentsAndroid() {
Kevin McNee576bb5d82019-05-29 16:47:516605 if (!web_contents_android_) {
6606 web_contents_android_ = std::make_unique<WebContentsAndroid>(this);
[email protected]155c7f22013-12-09 17:07:186607 }
Kevin McNee576bb5d82019-05-29 16:47:516608 return web_contents_android_.get();
6609}
6610
6611void WebContentsImpl::ClearWebContentsAndroid() {
6612 DCHECK_CURRENTLY_ON(BrowserThread::UI);
6613 web_contents_android_.reset();
[email protected]155c7f22013-12-09 17:07:186614}
6615
paulmeyerc0b762b2016-04-13 11:55:176616void WebContentsImpl::ActivateNearestFindResult(float x,
6617 float y) {
6618 GetOrCreateFindRequestManager()->ActivateNearestFindResult(x, y);
6619}
6620
6621void WebContentsImpl::RequestFindMatchRects(int current_version) {
6622 GetOrCreateFindRequestManager()->RequestFindMatchRects(current_version);
6623}
6624
[email protected]7d244f12013-08-22 21:41:516625bool WebContentsImpl::CreateRenderViewForInitialEmptyDocument() {
nasko4c0feb62015-06-05 18:37:066626 return CreateRenderViewForRenderManager(
6627 GetRenderViewHost(), MSG_ROUTING_NONE, MSG_ROUTING_NONE,
Pavel Feldman25234722017-10-11 02:49:066628 frame_tree_.root()->devtools_frame_token(),
lfgbede6392015-09-11 21:54:066629 frame_tree_.root()->current_replication_state());
[email protected]7d244f12013-08-22 21:41:516630}
[email protected]fc2b46b2014-05-03 16:33:456631
rockot400ea35b2016-10-15 19:15:326632service_manager::InterfaceProvider* WebContentsImpl::GetJavaInterfaces() {
sammcf5f1b0f2016-09-20 23:05:116633 if (!java_interfaces_) {
Gyuyoung Kim6c9ce9022019-11-26 05:40:086634 mojo::PendingRemote<service_manager::mojom::InterfaceProvider> provider;
6635 BindInterfaceRegistryForWebContents(
6636 provider.InitWithNewPipeAndPassReceiver(), this);
rockot400ea35b2016-10-15 19:15:326637 java_interfaces_.reset(new service_manager::InterfaceProvider);
sammcf5f1b0f2016-09-20 23:05:116638 java_interfaces_->Bind(std::move(provider));
6639 }
6640 return java_interfaces_.get();
6641}
6642
Francois Doray6c26f3342018-08-24 17:17:226643#endif
6644
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:416645bool WebContentsImpl::CompletedFirstVisuallyNonEmptyPaint() {
Carlos Caballero91fffb22019-10-29 15:24:306646 return GetRenderViewHost()->did_first_visually_non_empty_paint();
spqchane72c3cb2017-06-30 23:02:106647}
6648
pkotwicz75ca8ffd2016-02-16 23:10:196649void WebContentsImpl::OnDidDownloadImage(
Avi Drissmana5a52dd2018-03-27 03:39:026650 ImageDownloadCallback callback,
pkotwicz75ca8ffd2016-02-16 23:10:196651 int id,
6652 const GURL& image_url,
leon.hancbc4bc3c2016-02-26 07:08:526653 int32_t http_status_code,
leon.hanc2228532016-08-16 05:59:186654 const std::vector<SkBitmap>& images,
6655 const std::vector<gfx::Size>& original_image_sizes) {
Avi Drissmana5a52dd2018-03-27 03:39:026656 std::move(callback).Run(id, http_status_code, image_url, images,
6657 original_image_sizes);
pkotwicz75ca8ffd2016-02-16 23:10:196658}
6659
[email protected]6fba26d2014-04-29 09:38:286660void WebContentsImpl::OnDialogClosed(int render_process_id,
6661 int render_frame_id,
[email protected]b172aee2012-04-10 17:05:266662 IPC::Message* reply_msg,
[email protected]87de04b02014-04-08 22:14:496663 bool dialog_was_suppressed,
[email protected]b172aee2012-04-10 17:05:266664 bool success,
[email protected]fcf75d42013-12-03 20:11:266665 const base::string16& user_input) {
[email protected]6fba26d2014-04-29 09:38:286666 RenderFrameHostImpl* rfh = RenderFrameHostImpl::FromID(render_process_id,
6667 render_frame_id);
[email protected]87de04b02014-04-08 22:14:496668 last_dialog_suppressed_ = dialog_was_suppressed;
6669
Charlie Reis96077362019-07-04 00:34:046670 javascript_dialog_navigation_deferrer_.reset();
6671
[email protected]beb440c2009-11-06 04:08:546672 if (is_showing_before_unload_dialog_ && !success) {
clamybc6f4152016-04-01 20:14:236673 // It is possible for the current RenderFrameHost to have changed in the
6674 // meantime. Do not reset the navigation state in that case.
6675 if (rfh && rfh == rfh->frame_tree_node()->current_frame_host()) {
clamy44e84ce2016-02-22 15:38:256676 rfh->frame_tree_node()->BeforeUnloadCanceled();
clamybc6f4152016-04-01 20:14:236677 controller_.DiscardNonCommittedEntries();
6678 }
[email protected]ec8e8b02013-06-19 04:57:106679
Charles Reis15d60b132017-11-03 17:43:476680 // Update the URL display either way, to avoid showing a stale URL.
6681 NotifyNavigationStateChanged(INVALIDATE_TYPE_URL);
6682
ericwilligersde386342016-10-19 02:35:096683 for (auto& observer : observers_)
6684 observer.BeforeUnloadDialogCancelled();
[email protected]beb440c2009-11-06 04:08:546685 }
[email protected]87de04b02014-04-08 22:14:496686
[email protected]6fba26d2014-04-29 09:38:286687 if (rfh) {
avi44acfaa82017-05-03 22:55:356688 rfh->JavaScriptDialogClosed(reply_msg, success, user_input);
Pavel Feldman3c1842b2017-08-02 05:00:166689
6690 std::vector<protocol::PageHandler*> page_handlers =
6691 protocol::PageHandler::EnabledForWebContents(this);
6692 for (auto* handler : page_handlers)
6693 handler->DidCloseJavaScriptDialog(success, user_input);
6694
[email protected]6fba26d2014-04-29 09:38:286695 } else {
6696 // Don't leak the sync IPC reply if the RFH or process is gone.
6697 delete reply_msg;
6698 }
avi1c51fb62017-02-27 20:43:036699
6700 is_showing_javascript_dialog_ = false;
6701 is_showing_before_unload_dialog_ = false;
[email protected]beb440c2009-11-06 04:08:546702}
6703
[email protected]1ac10dca2013-08-20 20:47:046704bool WebContentsImpl::IsHidden() {
Francois Doraye6161152018-03-27 22:05:376705 return !IsBeingCaptured() && visibility_ != Visibility::VISIBLE;
[email protected]1ac10dca2013-08-20 20:47:046706}
6707
rob97250742015-12-10 17:45:156708int WebContentsImpl::GetOuterDelegateFrameTreeNodeId() {
lfg601233692017-03-06 22:45:446709 return node_.outer_contents_frame_tree_node_id();
lazyboy6ec48b2a2015-06-29 15:18:146710}
6711
ekaramadaeb3c5c2016-11-09 03:22:356712RenderWidgetHostImpl* WebContentsImpl::GetFullscreenRenderWidgetHost() const {
6713 return RenderWidgetHostImpl::FromID(fullscreen_widget_process_id_,
6714 fullscreen_widget_routing_id_);
6715}
6716
[email protected]b0936d22013-11-28 06:47:366717RenderFrameHostManager* WebContentsImpl::GetRenderManager() const {
[email protected]fa944cb82013-11-15 17:51:216718 return frame_tree_.root()->render_manager();
6719}
6720
[email protected]f8497342013-02-05 22:15:026721BrowserPluginGuest* WebContentsImpl::GetBrowserPluginGuest() const {
[email protected]7a846df2012-09-20 19:17:396722 return browser_plugin_guest_.get();
6723}
6724
Albert J. Wongc55cc64d2018-10-12 02:50:046725void WebContentsImpl::SetBrowserPluginGuest(
6726 std::unique_ptr<BrowserPluginGuest> guest) {
6727 DCHECK(!browser_plugin_guest_);
6728 DCHECK(guest);
6729 browser_plugin_guest_ = std::move(guest);
[email protected]738f57a2013-06-29 21:06:546730}
6731
Aidan Wolter52bcc12a2018-11-14 03:27:296732base::UnguessableToken WebContentsImpl::GetAudioGroupId() {
Aidan Wolter52bcc12a2018-11-14 03:27:296733 return GetAudioStreamFactory()->group_id();
6734}
6735
Sofiya Semenova5e9d9122019-07-11 02:46:416736ukm::SourceId WebContentsImpl::GetLastCommittedSourceId() {
6737 return last_committed_source_id_;
6738}
6739
[email protected]f8497342013-02-05 22:15:026740BrowserPluginEmbedder* WebContentsImpl::GetBrowserPluginEmbedder() const {
[email protected]7a846df2012-09-20 19:17:396741 return browser_plugin_embedder_.get();
6742}
[email protected]8ff00d72012-10-23 19:12:216743
fsamuel60b42282015-03-10 03:29:146744void WebContentsImpl::CreateBrowserPluginEmbedderIfNecessary() {
6745 if (browser_plugin_embedder_)
6746 return;
6747 browser_plugin_embedder_.reset(BrowserPluginEmbedder::Create(this));
6748}
6749
Jianzhou Fengd8720c72018-01-18 03:06:306750gfx::Size WebContentsImpl::GetSizeForNewRenderView(bool is_main_frame) {
[email protected]dc0fd432013-08-27 15:29:216751 gfx::Size size;
Jianzhou Fengd8720c72018-01-18 03:06:306752 if (is_main_frame)
6753 size = device_emulation_size_;
6754 if (size.IsEmpty() && delegate_)
[email protected]dc0fd432013-08-27 15:29:216755 size = delegate_->GetSizeForNewRenderView(this);
6756 if (size.IsEmpty())
[email protected]fc2b46b2014-05-03 16:33:456757 size = GetContainerBounds().size();
[email protected]dc0fd432013-08-27 15:29:216758 return size;
6759}
6760
[email protected]ae7eeda2014-07-04 01:53:266761void WebContentsImpl::OnFrameRemoved(RenderFrameHost* render_frame_host) {
ericwilligersde386342016-10-19 02:35:096762 for (auto& observer : observers_)
6763 observer.FrameDeleted(render_frame_host);
[email protected]9b159a52013-10-03 17:24:556764}
6765
[email protected]222f5822014-02-05 23:40:496766void WebContentsImpl::OnPreferredSizeChanged(const gfx::Size& old_size) {
6767 if (!delegate_)
6768 return;
6769 const gfx::Size new_size = GetPreferredSize();
6770 if (new_size != old_size)
6771 delegate_->UpdatePreferredSize(this, new_size);
6772}
6773
Dan Beam055fe8f2017-10-04 23:23:366774std::unique_ptr<WebUIImpl> WebContentsImpl::CreateWebUI(const GURL& url) {
Jeremy Roman04f27c372017-10-27 15:20:556775 std::unique_ptr<WebUIImpl> web_ui = std::make_unique<WebUIImpl>(this);
Lei Zhangee3b63ad2018-06-06 23:25:316776 std::unique_ptr<WebUIController> controller(
avie865b1d2016-10-24 19:42:596777 WebUIControllerFactoryRegistry::GetInstance()
Lei Zhangee3b63ad2018-06-06 23:25:316778 ->CreateWebUIControllerForURL(web_ui.get(), url));
dbeama1b926a2015-08-31 23:17:516779 if (controller) {
Lei Zhangee3b63ad2018-06-06 23:25:316780 web_ui->SetController(std::move(controller));
dbeama1b926a2015-08-31 23:17:516781 return web_ui;
6782 }
6783
avie865b1d2016-10-24 19:42:596784 return nullptr;
dbeama1b926a2015-08-31 23:17:516785}
6786
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:416787FindRequestManager* WebContentsImpl::GetFindRequestManager() {
6788 for (auto* contents = this; contents;
paulmeyerfeafc2d2017-04-25 21:46:406789 contents = contents->GetOuterWebContents()) {
6790 if (contents->find_request_manager_)
6791 return contents->find_request_manager_.get();
6792 }
6793
6794 return nullptr;
6795}
6796
paulmeyerc0b762b2016-04-13 11:55:176797FindRequestManager* WebContentsImpl::GetOrCreateFindRequestManager() {
paulmeyerfeafc2d2017-04-25 21:46:406798 if (FindRequestManager* manager = GetFindRequestManager())
6799 return manager;
6800
Ehsan Karamad6beb2ea2018-11-25 18:15:136801 DCHECK(!browser_plugin_guest_ || GetOuterWebContents());
6802
paulmeyerfeafc2d2017-04-25 21:46:406803 // No existing FindRequestManager found, so one must be created.
6804 find_request_manager_.reset(new FindRequestManager(this));
6805
6806 // Concurrent find sessions must not overlap, so destroy any existing
6807 // FindRequestManagers in any inner WebContentses.
Lucas Furukawa Gadani2ec00c82018-12-14 15:53:166808 for (WebContents* contents : GetWebContentsAndAllInner()) {
6809 auto* web_contents_impl = static_cast<WebContentsImpl*>(contents);
6810 if (web_contents_impl == this)
paulmeyerfeafc2d2017-04-25 21:46:406811 continue;
Lucas Furukawa Gadani2ec00c82018-12-14 15:53:166812 if (web_contents_impl->find_request_manager_) {
6813 web_contents_impl->find_request_manager_->StopFinding(
6814 STOP_FIND_ACTION_CLEAR_SELECTION);
6815 web_contents_impl->find_request_manager_.release();
paulmeyerfeafc2d2017-04-25 21:46:406816 }
6817 }
paulmeyerc0b762b2016-04-13 11:55:176818
6819 return find_request_manager_.get();
6820}
6821
6822void WebContentsImpl::NotifyFindReply(int request_id,
6823 int number_of_matches,
6824 const gfx::Rect& selection_rect,
6825 int active_match_ordinal,
6826 bool final_update) {
Sebastien Marchandcf97ed52017-07-24 23:50:346827 if (delegate_ && !is_being_destroyed_ &&
Lukasz Anforowicze9ae3722017-09-29 17:37:386828 !GetMainFrame()->GetProcess()->FastShutdownStarted()) {
paulmeyerc0b762b2016-04-13 11:55:176829 delegate_->FindReply(this, request_id, number_of_matches, selection_rect,
6830 active_match_ordinal, final_update);
6831 }
6832}
6833
ortuno467e5792016-06-10 04:32:396834void WebContentsImpl::IncrementBluetoothConnectedDeviceCount() {
6835 // Trying to invalidate the tab state while being destroyed could result in a
6836 // use after free.
6837 if (IsBeingDestroyed()) {
6838 return;
6839 }
6840 // Notify for UI updates if the state changes.
6841 bluetooth_connected_device_count_++;
6842 if (bluetooth_connected_device_count_ == 1) {
6843 NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB);
6844 }
6845}
6846
6847void WebContentsImpl::DecrementBluetoothConnectedDeviceCount() {
6848 // Trying to invalidate the tab state while being destroyed could result in a
6849 // use after free.
6850 if (IsBeingDestroyed()) {
6851 return;
6852 }
6853 // Notify for UI updates if the state changes.
Lukasz Anforowicze9ae3722017-09-29 17:37:386854 DCHECK_NE(bluetooth_connected_device_count_, 0u);
ortuno467e5792016-06-10 04:32:396855 bluetooth_connected_device_count_--;
6856 if (bluetooth_connected_device_count_ == 0) {
6857 NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB);
6858 }
6859}
6860
Reilly Grant5e7c79b22019-04-09 17:26:206861void WebContentsImpl::IncrementSerialActiveFrameCount() {
6862 // Trying to invalidate the tab state while being destroyed could result in a
6863 // use after free.
6864 if (IsBeingDestroyed())
6865 return;
6866
6867 // Notify for UI updates if the state changes.
6868 serial_active_frame_count_++;
6869 if (serial_active_frame_count_ == 1)
6870 NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB);
6871}
6872
6873void WebContentsImpl::DecrementSerialActiveFrameCount() {
6874 // Trying to invalidate the tab state while being destroyed could result in a
6875 // use after free.
6876 if (IsBeingDestroyed())
6877 return;
6878
6879 // Notify for UI updates if the state changes.
6880 DCHECK_NE(0u, serial_active_frame_count_);
6881 serial_active_frame_count_--;
6882 if (serial_active_frame_count_ == 0)
6883 NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB);
6884}
6885
Matt Reynoldse8c6c1f2019-11-02 09:53:536886void WebContentsImpl::IncrementHidActiveFrameCount() {
6887 // Trying to invalidate the tab state while being destroyed could result in a
6888 // use after free.
6889 if (IsBeingDestroyed())
6890 return;
6891
6892 // Notify for UI updates if the active frame count transitions from zero to
6893 // non-zero.
6894 hid_active_frame_count_++;
6895 if (hid_active_frame_count_ == 1)
6896 NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB);
6897}
6898
6899void WebContentsImpl::DecrementHidActiveFrameCount() {
6900 // Trying to invalidate the tab state while being destroyed could result in a
6901 // use after free.
6902 if (IsBeingDestroyed())
6903 return;
6904
6905 // Notify for UI updates if the active frame count transitions from non-zero
6906 // to zero.
6907 DCHECK_NE(0u, hid_active_frame_count_);
6908 hid_active_frame_count_--;
6909 if (hid_active_frame_count_ == 0)
6910 NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB);
6911}
6912
Marijn Kruisselbrink29051042019-08-06 22:56:556913void WebContentsImpl::IncrementNativeFileSystemHandleCount() {
6914 // Trying to invalidate the tab state while being destroyed could result in a
6915 // use after free.
6916 if (IsBeingDestroyed())
6917 return;
6918
6919 // Notify for UI updates if the state changes. Need both TYPE_TAB and TYPE_URL
6920 // to update both the tab-level usage indicator and the usage indicator in the
6921 // omnibox.
6922 native_file_system_handle_count_++;
6923 if (native_file_system_handle_count_ == 1) {
6924 NotifyNavigationStateChanged(static_cast<content::InvalidateTypes>(
6925 INVALIDATE_TYPE_TAB | INVALIDATE_TYPE_URL));
6926 }
6927}
6928
6929void WebContentsImpl::DecrementNativeFileSystemHandleCount() {
6930 // Trying to invalidate the tab state while being destroyed could result in a
6931 // use after free.
6932 if (IsBeingDestroyed())
6933 return;
6934
6935 // Notify for UI updates if the state changes. Need both TYPE_TAB and TYPE_URL
6936 // to update both the tab-level usage indicator and the usage indicator in the
6937 // omnibox.
6938 DCHECK_NE(0u, native_file_system_handle_count_);
6939 native_file_system_handle_count_--;
6940 if (native_file_system_handle_count_ == 0) {
6941 NotifyNavigationStateChanged(static_cast<content::InvalidateTypes>(
6942 INVALIDATE_TYPE_TAB | INVALIDATE_TYPE_URL));
6943 }
6944}
6945
Marijn Kruisselbrink46fa4bd2019-07-11 18:15:476946void WebContentsImpl::AddNativeFileSystemDirectoryHandle(
6947 const base::FilePath& path) {
Marijn Kruisselbrinkf1714aa22019-07-02 03:45:336948 // Trying to invalidate the tab state while being destroyed could result in a
6949 // use after free.
6950 if (IsBeingDestroyed())
6951 return;
6952
6953 // Notify for UI updates if the state changes. Need both TYPE_TAB and TYPE_URL
6954 // to update both the tab-level usage indicator and the usage indicator in the
6955 // omnibox.
Marijn Kruisselbrink46fa4bd2019-07-11 18:15:476956 const bool was_empty = native_file_system_directory_handles_.empty();
6957 native_file_system_directory_handles_[path]++;
6958 if (was_empty) {
Marijn Kruisselbrinkf1714aa22019-07-02 03:45:336959 NotifyNavigationStateChanged(static_cast<content::InvalidateTypes>(
6960 INVALIDATE_TYPE_TAB | INVALIDATE_TYPE_URL));
6961 }
6962}
6963
Marijn Kruisselbrink46fa4bd2019-07-11 18:15:476964void WebContentsImpl::RemoveNativeFileSystemDirectoryHandle(
6965 const base::FilePath& path) {
Marijn Kruisselbrinkf1714aa22019-07-02 03:45:336966 // Trying to invalidate the tab state while being destroyed could result in a
6967 // use after free.
6968 if (IsBeingDestroyed())
6969 return;
6970
6971 // Notify for UI updates if the state changes. Need both TYPE_TAB and TYPE_URL
6972 // to update both the tab-level usage indicator and the usage indicator in the
6973 // omnibox.
Marijn Kruisselbrink46fa4bd2019-07-11 18:15:476974 auto it = native_file_system_directory_handles_.find(path);
6975 DCHECK(it != native_file_system_directory_handles_.end());
6976 DCHECK_NE(0u, it->second);
6977 it->second--;
6978 if (it->second == 0)
6979 native_file_system_directory_handles_.erase(it);
6980 if (native_file_system_directory_handles_.empty()) {
Marijn Kruisselbrinkf1714aa22019-07-02 03:45:336981 NotifyNavigationStateChanged(static_cast<content::InvalidateTypes>(
6982 INVALIDATE_TYPE_TAB | INVALIDATE_TYPE_URL));
6983 }
6984}
6985
6986void WebContentsImpl::IncrementWritableNativeFileSystemHandleCount() {
6987 // Trying to invalidate the tab state while being destroyed could result in a
6988 // use after free.
6989 if (IsBeingDestroyed())
6990 return;
6991
6992 // Notify for UI updates if the state changes. Need both TYPE_TAB and TYPE_URL
6993 // to update both the tab-level usage indicator and the usage indicator in the
6994 // omnibox.
6995 native_file_system_writable_handle_count_++;
6996 if (native_file_system_writable_handle_count_ == 1) {
6997 NotifyNavigationStateChanged(static_cast<content::InvalidateTypes>(
6998 INVALIDATE_TYPE_TAB | INVALIDATE_TYPE_URL));
6999 }
7000}
7001
7002void WebContentsImpl::DecrementWritableNativeFileSystemHandleCount() {
7003 // Trying to invalidate the tab state while being destroyed could result in a
7004 // use after free.
7005 if (IsBeingDestroyed())
7006 return;
7007
7008 // Notify for UI updates if the state changes. Need both TYPE_TAB and TYPE_URL
7009 // to update both the tab-level usage indicator and the usage indicator in the
7010 // omnibox.
7011 DCHECK_NE(0u, native_file_system_writable_handle_count_);
7012 native_file_system_writable_handle_count_--;
7013 if (native_file_system_writable_handle_count_ == 0) {
7014 NotifyNavigationStateChanged(static_cast<content::InvalidateTypes>(
7015 INVALIDATE_TYPE_TAB | INVALIDATE_TYPE_URL));
7016 }
7017}
7018
zqzhang8ac49002017-03-16 21:51:357019void WebContentsImpl::SetHasPersistentVideo(bool has_persistent_video) {
7020 if (has_persistent_video_ == has_persistent_video)
mlamouri5cd9ae82017-02-18 11:05:097021 return;
7022
zqzhang8ac49002017-03-16 21:51:357023 has_persistent_video_ = has_persistent_video;
mlamouri5cd9ae82017-02-18 11:05:097024 NotifyPreferencesChanged();
zqzhang8ac49002017-03-16 21:51:357025 media_web_contents_observer()->RequestPersistentVideo(has_persistent_video);
mlamouri5cd9ae82017-02-18 11:05:097026}
7027
Ian Vollick5d5c37f2019-06-19 22:50:577028void WebContentsImpl::SetSpatialNavigationDisabled(bool disabled) {
7029 if (is_spatial_navigation_disabled_ == disabled)
7030 return;
7031
7032 is_spatial_navigation_disabled_ = disabled;
7033 NotifyPreferencesChanged();
7034}
7035
paulmeyerfeafc2d2017-04-25 21:46:407036void WebContentsImpl::BrowserPluginGuestWillDetach() {
7037 WebContentsImpl* outermost = GetOutermostWebContents();
7038 if (this != outermost && ContainsOrIsFocusedWebContents())
7039 outermost->SetAsFocusedWebContentsIfNecessary();
7040}
7041
Becca Hughes112832e2019-06-11 17:19:027042PictureInPictureResult WebContentsImpl::EnterPictureInPicture(
Mounir Lamouri11e9ef42018-05-22 03:10:167043 const viz::SurfaceId& surface_id,
7044 const gfx::Size& natural_size) {
François Beaufortf09e4462019-01-18 14:34:517045 return delegate_
7046 ? delegate_->EnterPictureInPicture(this, surface_id, natural_size)
Becca Hughes112832e2019-06-11 17:19:027047 : PictureInPictureResult::kNotSupported;
Mounir Lamouri11e9ef42018-05-22 03:10:167048}
7049
Mounir Lamouri6d759e12018-05-16 20:01:307050void WebContentsImpl::ExitPictureInPicture() {
7051 if (delegate_)
7052 delegate_->ExitPictureInPicture();
7053}
7054
paulmeyerc0b762b2016-04-13 11:55:177055#if defined(OS_ANDROID)
7056void WebContentsImpl::NotifyFindMatchRectsReply(
7057 int version,
7058 const std::vector<gfx::RectF>& rects,
7059 const gfx::RectF& active_rect) {
7060 if (delegate_)
7061 delegate_->FindMatchRectsReply(this, version, rects, active_rect);
7062}
7063#endif
7064
[email protected]34ff1cfc2014-08-20 06:16:057065void WebContentsImpl::SetForceDisableOverscrollContent(bool force_disable) {
7066 force_disable_overscroll_content_ = force_disable;
7067 if (view_)
7068 view_->SetOverscrollControllerEnabled(CanOverscrollContent());
7069}
7070
Jianzhou Fengd8720c72018-01-18 03:06:307071bool WebContentsImpl::SetDeviceEmulationSize(const gfx::Size& new_size) {
7072 device_emulation_size_ = new_size;
7073 RenderWidgetHostView* rwhv = GetMainFrame()->GetView();
7074
7075 const gfx::Size current_size = rwhv->GetViewBounds().size();
7076 if (view_size_before_emulation_.IsEmpty())
7077 view_size_before_emulation_ = current_size;
7078
7079 if (current_size != new_size)
7080 rwhv->SetSize(new_size);
7081
7082 return current_size != new_size;
7083}
7084
7085void WebContentsImpl::ClearDeviceEmulationSize() {
7086 RenderWidgetHostView* rwhv = GetMainFrame()->GetView();
7087 // WebContentsView could get resized during emulation, which also resizes
7088 // RWHV. If it happens, assume user would like to keep using the size after
7089 // emulation.
7090 // TODO(jzfeng): Prohibit resizing RWHV through any other means (at least when
7091 // WebContentsView size changes).
Dmitry Gozmanc63707182018-03-06 00:09:397092 if (!view_size_before_emulation_.IsEmpty() && rwhv &&
7093 rwhv->GetViewBounds().size() == device_emulation_size_) {
Jianzhou Fengd8720c72018-01-18 03:06:307094 rwhv->SetSize(view_size_before_emulation_);
Dmitry Gozmanc63707182018-03-06 00:09:397095 }
Jianzhou Fengd8720c72018-01-18 03:06:307096 device_emulation_size_ = gfx::Size();
7097 view_size_before_emulation_ = gfx::Size();
7098}
7099
Max Morin5bc74f52018-05-09 07:00:217100ForwardingAudioStreamFactory* WebContentsImpl::GetAudioStreamFactory() {
7101 if (!audio_stream_factory_) {
7102 audio_stream_factory_.emplace(
7103 this,
Max Morinddebb972018-09-20 10:04:147104 // BrowserMainLoop::GetInstance() may be null in unit tests.
7105 BrowserMainLoop::GetInstance()
7106 ? static_cast<media::UserInputMonitorBase*>(
7107 BrowserMainLoop::GetInstance()->user_input_monitor())
7108 : nullptr,
Ken Rockot72964402019-12-06 10:40:517109 AudioStreamBrokerFactory::CreateImpl());
Max Morin5bc74f52018-05-09 07:00:217110 }
7111
7112 return &*audio_stream_factory_;
7113}
7114
dalecurtis88c240072015-12-09 02:11:187115void WebContentsImpl::MediaStartedPlaying(
billorr21c005d2016-11-17 03:57:047116 const WebContentsObserver::MediaPlayerInfo& media_info,
Mounir Lamouri0f5bdf52019-04-04 16:32:357117 const MediaPlayerId& id) {
billorr21c005d2016-11-17 03:57:047118 if (media_info.has_video)
7119 currently_playing_video_count_++;
7120
ericwilligersde386342016-10-19 02:35:097121 for (auto& observer : observers_)
billorr21c005d2016-11-17 03:57:047122 observer.MediaStartedPlaying(media_info, id);
dalecurtis88c240072015-12-09 02:11:187123}
7124
7125void WebContentsImpl::MediaStoppedPlaying(
billorr21c005d2016-11-17 03:57:047126 const WebContentsObserver::MediaPlayerInfo& media_info,
Mounir Lamouri0f5bdf52019-04-04 16:32:357127 const MediaPlayerId& id,
Becca Hughes220bfc102017-11-14 18:24:077128 WebContentsObserver::MediaStoppedReason reason) {
billorr21c005d2016-11-17 03:57:047129 if (media_info.has_video)
7130 currently_playing_video_count_--;
7131
ericwilligersde386342016-10-19 02:35:097132 for (auto& observer : observers_)
Becca Hughes220bfc102017-11-14 18:24:077133 observer.MediaStoppedPlaying(media_info, id, reason);
billorr21c005d2016-11-17 03:57:047134}
7135
Mounir Lamouri0f5bdf52019-04-04 16:32:357136void WebContentsImpl::MediaResized(const gfx::Size& size,
7137 const MediaPlayerId& id) {
peconn257951522017-06-09 18:24:597138 cached_video_sizes_[id] = size;
7139
7140 for (auto& observer : observers_)
7141 observer.MediaResized(size, id);
7142}
7143
Peter E Conneccb34c22017-09-08 09:37:587144void WebContentsImpl::MediaEffectivelyFullscreenChanged(bool is_fullscreen) {
7145 for (auto& observer : observers_)
7146 observer.MediaEffectivelyFullscreenChanged(is_fullscreen);
7147}
7148
Peter Conn31c726a2017-08-17 11:07:347149base::Optional<gfx::Size> WebContentsImpl::GetFullscreenVideoSize() {
Mounir Lamouri0f5bdf52019-04-04 16:32:357150 base::Optional<MediaPlayerId> id =
Peter Conn31c726a2017-08-17 11:07:347151 media_web_contents_observer_->GetFullscreenVideoMediaPlayerId();
Jan Wilken Dörrie531be7ca2019-06-07 10:05:577152 if (id && base::Contains(cached_video_sizes_, id.value()))
Peter Conn31c726a2017-08-17 11:07:347153 return base::Optional<gfx::Size>(cached_video_sizes_[id.value()]);
Lei Zhang6410ec1a2019-01-31 21:38:587154 return base::nullopt;
peconn257951522017-06-09 18:24:597155}
7156
billorr21c005d2016-11-17 03:57:047157int WebContentsImpl::GetCurrentlyPlayingVideoCount() {
7158 return currently_playing_video_count_;
dalecurtis88c240072015-12-09 02:11:187159}
7160
Raymond Toye413650c2018-08-31 00:12:477161void WebContentsImpl::AudioContextPlaybackStarted(RenderFrameHost* host,
7162 int context_id) {
7163 WebContentsObserver::AudioContextId audio_context_id(host, context_id);
7164 for (auto& observer : observers_)
7165 observer.AudioContextPlaybackStarted(audio_context_id);
7166}
7167
7168void WebContentsImpl::AudioContextPlaybackStopped(RenderFrameHost* host,
7169 int context_id) {
7170 WebContentsObserver::AudioContextId audio_context_id(host, context_id);
7171 for (auto& observer : observers_)
7172 observer.AudioContextPlaybackStopped(audio_context_id);
7173}
7174
evliufc233aa2019-10-31 19:53:037175void WebContentsImpl::MediaWatchTimeChanged(
7176 const content::MediaPlayerWatchTime& watch_time) {
7177 for (auto& observer : observers_)
7178 observer.MediaWatchTimeChanged(watch_time);
7179}
7180
evliu7f736f02019-11-07 17:43:507181media::MediaMetricsProvider::RecordAggregateWatchTimeCallback
7182WebContentsImpl::GetRecordAggregateWatchTimeCallback() {
7183 return base::BindRepeating(
7184 [](base::WeakPtr<RenderFrameHostDelegate> delegate,
7185 GURL last_committed_url, base::TimeDelta total_watch_time,
evliua283c9e2019-11-08 22:57:227186 base::TimeDelta time_stamp, bool has_video, bool has_audio) {
7187 content::MediaPlayerWatchTime watch_time(
7188 last_committed_url, last_committed_url.GetOrigin(),
7189 total_watch_time, time_stamp, has_video, has_audio);
evliu7f736f02019-11-07 17:43:507190
7191 // Save the watch time if the delegate is still alive.
7192 if (delegate)
7193 delegate->MediaWatchTimeChanged(watch_time);
7194 },
7195 weak_factory_.GetWeakPtr(), GetMainFrameLastCommittedURL());
7196}
7197
Ehsan Karamad1c631762019-05-13 23:04:017198RenderFrameHostImpl* WebContentsImpl::GetMainFrameForInnerDelegate(
7199 FrameTreeNode* frame_tree_node) {
7200 if (auto* web_contents = node_.GetInnerWebContentsInFrame(frame_tree_node))
7201 return web_contents->GetMainFrame();
7202 return nullptr;
7203}
7204
John Budorickee1c9512019-10-17 15:32:197205bool WebContentsImpl::IsFrameLowPriority(
7206 const RenderFrameHost* render_frame_host) {
7207 if (!delegate_)
7208 return false;
7209 return delegate_->IsFrameLowPriority(this, render_frame_host);
7210}
7211
Francois Doraye6161152018-03-27 22:05:377212void WebContentsImpl::UpdateWebContentsVisibility(Visibility visibility) {
Francois Doray3f2afbd2018-04-06 19:18:187213 // Occlusion is disabled when |features::kWebContentsOcclusion| is disabled
7214 // (for power and speed impact assessment) or when
7215 // |switches::kDisableBackgroundingOccludedWindowsForTesting| is specified on
7216 // the command line (to avoid flakiness in browser tests).
7217 const bool occlusion_is_disabled =
7218 !base::FeatureList::IsEnabled(features::kWebContentsOcclusion) ||
Francois Doraye6161152018-03-27 22:05:377219 base::CommandLine::ForCurrentProcess()->HasSwitch(
7220 switches::kDisableBackgroundingOccludedWindowsForTesting);
7221 if (occlusion_is_disabled && visibility == Visibility::OCCLUDED)
7222 visibility = Visibility::VISIBLE;
7223
tapted65ff2ea72016-03-01 23:39:007224 if (!did_first_set_visible_) {
Francois Doraye6161152018-03-27 22:05:377225 if (visibility == Visibility::VISIBLE) {
7226 // A WebContents created with CreateParams::initially_hidden = false
7227 // starts with GetVisibility() == Visibility::VISIBLE even though it is
7228 // not really visible. Call WasShown() when it becomes visible for real as
7229 // the page load mechanism and some WebContentsObserver rely on that.
tapted65ff2ea72016-03-01 23:39:007230 WasShown();
Francois Dorayfe4a1772018-02-17 04:17:097231 did_first_set_visible_ = true;
tapted65ff2ea72016-03-01 23:39:007232 }
Francois Doraye6161152018-03-27 22:05:377233
7234 // Trust the initial visibility of the WebContents and do not switch it to
7235 // HIDDEN or OCCLUDED before it becomes VISIBLE for real. Doing so would
7236 // result in destroying resources that would immediately be recreated (e.g.
7237 // UpdateWebContents(HIDDEN) can be called when a WebContents is added to a
7238 // hidden window that is about to be shown).
7239
tapted65ff2ea72016-03-01 23:39:007240 return;
7241 }
Francois Doraye6161152018-03-27 22:05:377242
7243 if (visibility == visibility_)
tapted65ff2ea72016-03-01 23:39:007244 return;
7245
Collin Baker989e0882019-11-01 01:27:177246 UpdateVisibilityAndNotifyPageAndView(visibility);
tapted65ff2ea72016-03-01 23:39:007247}
7248
aelias5252baa2016-04-10 01:18:027249void WebContentsImpl::UpdateOverridingUserAgent() {
mlamouri5cd9ae82017-02-18 11:05:097250 NotifyPreferencesChanged();
aelias5252baa2016-04-10 01:18:027251}
7252
avid53461d2016-02-25 17:15:047253void WebContentsImpl::SetJavaScriptDialogManagerForTesting(
7254 JavaScriptDialogManager* dialog_manager) {
7255 dialog_manager_ = dialog_manager;
7256}
7257
Julie Jeongeun Kim7d0986172019-12-04 03:17:257258void WebContentsImpl::RemoveReceiverSet(const std::string& interface_name) {
7259 auto it = receiver_sets_.find(interface_name);
7260 if (it != receiver_sets_.end())
7261 receiver_sets_.erase(it);
rockotf62002a2016-09-15 00:08:597262}
7263
nzolghadrd87a308d2016-12-07 15:45:567264bool WebContentsImpl::AddDomainInfoToRapporSample(rappor::Sample* sample) {
nzolghadrbbffc6c2017-05-25 17:56:397265 // Here we associate this metric to the main frame URL regardless of what
7266 // caused it.
nzolghadrd87a308d2016-12-07 15:45:567267 sample->SetStringField("Domain", ::rappor::GetDomainAndRegistrySampleFromGURL(
7268 GetLastCommittedURL()));
nzolghadrd87a308d2016-12-07 15:45:567269 return true;
7270}
7271
falken52a56e32016-12-08 05:02:407272void WebContentsImpl::ShowInsecureLocalhostWarningIfNeeded() {
7273 bool allow_localhost = base::CommandLine::ForCurrentProcess()->HasSwitch(
7274 switches::kAllowInsecureLocalhost);
7275 if (!allow_localhost)
7276 return;
7277
7278 content::NavigationEntry* entry = GetController().GetLastCommittedEntry();
Rob Wuf79b3ba2018-01-14 01:54:317279 if (!entry || !net::IsLocalhost(entry->GetURL()))
falken52a56e32016-12-08 05:02:407280 return;
7281
7282 content::SSLStatus ssl_status = entry->GetSSL();
Matt Mueller936511442019-09-03 18:15:127283 if (!net::IsCertStatusError(ssl_status.cert_status))
falken52a56e32016-12-08 05:02:407284 return;
7285
7286 GetMainFrame()->AddMessageToConsole(
Abhijeet Kandalkara5928a0a2019-03-13 05:04:427287 blink::mojom::ConsoleMessageLevel::kWarning,
falken52a56e32016-12-08 05:02:407288 base::StringPrintf("This site does not have a valid SSL "
7289 "certificate! Without SSL, your site's and "
7290 "visitors' data is vulnerable to theft and "
7291 "tampering. Get a valid SSL certificate before"
7292 " releasing your website to the public."));
7293}
7294
ekaramadf6750aa2017-06-06 18:29:427295bool WebContentsImpl::IsShowingContextMenuOnPage() const {
7296 return showing_context_menu_;
7297}
7298
Min Qina904f3302018-02-13 23:33:347299download::DownloadUrlParameters::RequestHeadersType
7300WebContentsImpl::ParseDownloadHeaders(const std::string& headers) {
7301 download::DownloadUrlParameters::RequestHeadersType request_headers;
Megan Jablonski2f6a4c52017-07-10 23:01:257302 for (const base::StringPiece& key_value : base::SplitStringPiece(
7303 headers, "\r\n", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY)) {
7304 std::vector<std::string> pair = base::SplitString(
7305 key_value, ":", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
7306 if (2ul == pair.size())
7307 request_headers.push_back(make_pair(pair[0], pair[1]));
7308 }
7309 return request_headers;
7310}
7311
lukasza6f8ac622017-06-06 03:10:207312void WebContentsImpl::SetOpenerForNewContents(FrameTreeNode* opener,
7313 bool opener_suppressed) {
7314 if (opener) {
7315 FrameTreeNode* new_root = GetFrameTree()->root();
7316
7317 // For the "original opener", track the opener's main frame instead, because
7318 // if the opener is a subframe, the opener tracking could be easily bypassed
7319 // by spawning from a subframe and deleting the subframe.
7320 // https://crbug.com/705316
7321 new_root->SetOriginalOpener(opener->frame_tree()->root());
7322
7323 if (!opener_suppressed) {
7324 new_root->SetOpener(opener);
7325 created_with_opener_ = true;
7326 }
7327 }
7328}
7329
Mounir Lamouri0f5bdf52019-04-04 16:32:357330void WebContentsImpl::MediaMutedStatusChanged(const MediaPlayerId& id,
7331 bool muted) {
Becca Hughes9f6fd4b82017-06-15 10:01:407332 for (auto& observer : observers_)
7333 observer.MediaMutedStatusChanged(id, muted);
7334}
7335
EhsanK2075c7e2017-08-21 02:42:397336void WebContentsImpl::SetVisibilityForChildViews(bool visible) {
7337 GetMainFrame()->SetVisibilityForChildViews(visible);
7338}
7339
Dave Tapuskae782bea2019-07-09 16:21:517340mojom::FrameInputHandler* WebContentsImpl::GetFocusedFrameInputHandler() {
7341 auto* focused_frame = GetFocusedFrame();
7342 if (!focused_frame)
7343 return nullptr;
7344 return focused_frame->GetFrameInputHandler();
7345}
7346
[email protected]8ff00d72012-10-23 19:12:217347} // namespace content