blob: b504f3779cab8a96cd72c1524bcc9795645d6764 [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"
jinsukkim79df88a2017-01-04 06:26:1517#include "base/i18n/character_encoding.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"
[email protected]04cbd3d2013-12-04 04:58:2027#include "base/process/process.h"
skyostil95082a62015-06-05 19:53:0728#include "base/single_thread_task_runner.h"
Arthur Sonzogni929d29f2018-09-17 14:19:4429#include "base/stl_util.h"
[email protected]348fbaac2013-06-11 06:31:5130#include "base/strings/string16.h"
31#include "base/strings/string_number_conversions.h"
Avi Drissman4891b852018-09-25 18:12:5832#include "base/strings/string_split.h"
[email protected]348fbaac2013-06-11 06:31:5133#include "base/strings/string_util.h"
[email protected]74ebfb12013-06-07 20:48:0034#include "base/strings/utf_string_conversions.h"
Eric Seckler8652dcd52018-09-20 10:42:2835#include "base/task/post_task.h"
gab30f26df2016-05-11 19:37:5536#include "base/threading/thread_task_runner_handle.h"
[email protected]a43858f2013-06-28 15:18:3737#include "base/time/time.h"
ssid3e765612015-01-28 04:03:4238#include "base/trace_event/trace_event.h"
avib7348942015-12-25 20:57:1039#include "build/build_config.h"
Min Qinda0ed2062018-02-23 22:00:5340#include "components/download/public/common/download_stats.h"
nzolghadrd87a308d2016-12-07 15:45:5641#include "components/rappor/public/rappor_utils.h"
rsleevi24f64dc22015-08-07 21:39:2142#include "components/url_formatter/url_formatter.h"
James Wallace-Leeeafc94cb92018-07-23 21:35:0943#include "content/browser/accessibility/accessibility_tree_formatter.h"
44#include "content/browser/accessibility/accessibility_tree_formatter_blink.h"
jamescookda2505812015-03-20 18:01:1845#include "content/browser/bad_message.h"
Max Morinddebb972018-09-20 10:04:1446#include "content/browser/browser_main_loop.h"
[email protected]7a846df2012-09-20 19:17:3947#include "content/browser/browser_plugin/browser_plugin_embedder.h"
48#include "content/browser/browser_plugin/browser_plugin_guest.h"
[email protected]b9535422012-02-09 01:47:5949#include "content/browser/child_process_security_policy_impl.h"
Andrey Lushnikov81b992e2019-06-27 23:27:2150#include "content/browser/devtools/devtools_instrumentation.h"
Pavel Feldman3c1842b2017-08-02 05:00:1651#include "content/browser/devtools/protocol/page_handler.h"
dgozmanf00c4f72016-10-19 17:45:1552#include "content/browser/devtools/render_frame_devtools_agent_host.h"
Becca Hughes3b5a43482018-07-17 22:31:5553#include "content/browser/display_cutout/display_cutout_host_impl.h"
[email protected]5f2aa722013-08-07 16:59:4154#include "content/browser/dom_storage/dom_storage_context_wrapper.h"
[email protected]1ea3c792012-04-17 01:25:0455#include "content/browser/dom_storage/session_storage_namespace_impl.h"
[email protected]aa4f3972012-03-01 18:12:1256#include "content/browser/download/mhtml_generation_manager.h"
[email protected]a53209b2012-01-20 16:48:1657#include "content/browser/download/save_package.h"
paulmeyerc0b762b2016-04-13 11:55:1758#include "content/browser/find_request_manager.h"
[email protected]bffc8302014-01-23 20:52:1659#include "content/browser/frame_host/cross_process_frame_connector.h"
ananta4b7467a52016-09-23 01:42:3860#include "content/browser/frame_host/frame_tree_node.h"
[email protected]d4a8ca482013-10-30 21:06:4061#include "content/browser/frame_host/interstitial_page_impl.h"
62#include "content/browser/frame_host/navigation_entry_impl.h"
clamy0e119882015-07-31 16:12:3363#include "content/browser/frame_host/navigation_handle_impl.h"
Arthur Hemeryc3380172018-01-22 14:00:1764#include "content/browser/frame_host/navigation_request.h"
[email protected]6ea6bdf2013-12-06 13:35:0165#include "content/browser/frame_host/navigator_impl.h"
[email protected]52913802013-12-10 05:52:1866#include "content/browser/frame_host/render_frame_host_impl.h"
lfgdb5c4ed2016-03-04 23:09:0767#include "content/browser/frame_host/render_frame_proxy_host.h"
Kenneth Russell954ed9ce2018-04-12 23:07:0168#include "content/browser/gpu/gpu_data_manager_impl.h"
csharrison2e8c98e2016-08-01 23:56:2569#include "content/browser/loader/loader_io_thread_notifier.h"
[email protected]678c0362012-12-05 08:02:4470#include "content/browser/loader/resource_dispatcher_host_impl.h"
mlamouriefdca9d2014-09-16 16:55:4071#include "content/browser/manifest/manifest_manager_host.h"
Max Morin5bc74f52018-05-09 07:00:2172#include "content/browser/media/audio_stream_broker.h"
dalecurtisbc6572e12014-09-12 19:22:3073#include "content/browser/media/audio_stream_monitor.h"
miu50f97892014-09-22 22:49:5274#include "content/browser/media/capture/web_contents_audio_muter.h"
dalecurtis88c240072015-12-09 02:11:1875#include "content/browser/media/media_web_contents_observer.h"
zqzhang1adf3cb2016-11-01 11:26:4576#include "content/browser/media/session/media_session_impl.h"
tommyclieb25b2a2014-11-03 19:45:0977#include "content/browser/plugin_content_origin_whitelist.h"
[email protected]f3b1a082011-11-18 00:34:3078#include "content/browser/renderer_host/render_process_host_impl.h"
[email protected]21161032014-05-05 16:56:5079#include "content/browser/renderer_host/render_view_host_delegate_view.h"
[email protected]b3c41c0b2012-03-06 15:48:3280#include "content/browser/renderer_host/render_view_host_impl.h"
[email protected]bafe6cd2012-05-23 23:09:5081#include "content/browser/renderer_host/render_widget_host_impl.h"
kenrb2a565f82015-09-02 20:24:5982#include "content/browser/renderer_host/render_widget_host_input_event_router.h"
[email protected]cfd80b02014-05-01 17:46:4883#include "content/browser/renderer_host/render_widget_host_view_base.h"
Ken Buchanandaef006b2017-08-17 18:32:1584#include "content/browser/renderer_host/render_widget_host_view_child_frame.h"
ekaramadadd882292016-06-08 15:22:5685#include "content/browser/renderer_host/text_input_manager.h"
leon.han552e9de2017-02-09 14:37:3086#include "content/browser/screen_orientation/screen_orientation_provider.h"
[email protected]b6583592012-01-25 19:52:3387#include "content/browser/site_instance_impl.h"
lfgc740d4b22016-04-15 16:45:3388#include "content/browser/web_contents/web_contents_view_child_frame.h"
[email protected]b5a40842012-11-28 15:26:1189#include "content/browser/web_contents/web_contents_view_guest.h"
[email protected]eb2ef212013-01-29 04:27:5890#include "content/browser/webui/generic_handler.h"
[email protected]86a0a6e2013-01-28 06:33:0391#include "content/browser/webui/web_ui_controller_factory_registry.h"
[email protected]d2353452012-01-19 19:53:5692#include "content/browser/webui/web_ui_impl.h"
[email protected]8eb04562013-03-06 03:41:1493#include "content/common/browser_plugin/browser_plugin_constants.h"
[email protected]703dd662013-03-05 07:37:4294#include "content/common/browser_plugin/browser_plugin_messages.h"
Paul Meyer0c58c3712016-11-17 22:59:5195#include "content/common/drag_messages.h"
[email protected]f114fa42013-12-06 17:06:4496#include "content/common/frame_messages.h"
[email protected]1f3fc1d2014-04-03 14:50:1797#include "content/common/input_messages.h"
lfgdb5c4ed2016-03-04 23:09:0798#include "content/common/page_messages.h"
nasko34634672016-07-29 18:46:0699#include "content/common/page_state_serialization.h"
rockot5c478a72016-09-28 23:14:18100#include "content/common/render_message_filter.mojom.h"
[email protected]2c5569662011-03-22 20:45:02101#include "content/common/view_messages.h"
Albert J. Wong3c93c182018-09-27 17:29:43102#include "content/common/widget_messages.h"
[email protected]31a71eaf2014-03-13 01:47:36103#include "content/public/browser/ax_event_notification_details.h"
[email protected]ccb797302011-12-15 16:55:11104#include "content/public/browser/browser_context.h"
[email protected]139355f2014-05-11 14:21:28105#include "content/public/browser/browser_plugin_guest_manager.h"
Eric Seckler8652dcd52018-09-20 10:42:28106#include "content/public/browser/browser_task_traits.h"
Gabriel Charette790754c2018-03-16 21:32:59107#include "content/public/browser/browser_thread.h"
[email protected]87f3c082011-10-19 18:07:44108#include "content/public/browser/content_browser_client.h"
[email protected]e582fdd2011-12-20 16:48:17109#include "content/public/browser/download_manager.h"
Kent Tamura512a27e2018-10-04 00:49:32110#include "content/public/browser/file_select_listener.h"
ekaramada110f642016-12-21 19:47:28111#include "content/public/browser/focused_node_details.h"
ekaramad1beabecc2016-09-23 20:18:17112#include "content/public/browser/guest_mode.h"
[email protected]d9083482012-01-06 00:38:46113#include "content/public/browser/invalidate_type.h"
[email protected]71a88bb2013-02-01 22:05:15114#include "content/public/browser/javascript_dialog_manager.h"
skyf65d9bb2017-03-24 02:26:39115#include "content/public/browser/keyboard_event_processing_result.h"
[email protected]09d31d52012-03-11 22:30:27116#include "content/public/browser/load_notification_details.h"
[email protected]5b96836f2011-12-22 07:39:00117#include "content/public/browser/navigation_details.h"
[email protected]375fa1b2012-05-22 22:05:37118#include "content/public/browser/notification_details.h"
[email protected]be2510c02012-05-28 14:52:14119#include "content/public/browser/notification_service.h"
scottmg011d96e32016-06-29 19:15:08120#include "content/public/browser/notification_types.h"
[email protected]95640212014-07-26 18:14:30121#include "content/public/browser/render_widget_host_iterator.h"
Lukasz Anforowicze1b954d92017-10-30 21:28:06122#include "content/public/browser/restore_type.h"
estark5ea80e52015-06-19 18:43:12123#include "content/public/browser/security_style_explanations.h"
falken52a56e32016-12-08 05:02:40124#include "content/public/browser/ssl_status.h"
[email protected]4c3a23582012-08-18 08:54:34125#include "content/public/browser/storage_partition.h"
Ken Rockotfd03e682019-06-20 21:13:54126#include "content/public/browser/system_connector.h"
rockotf62002a2016-09-15 00:08:59127#include "content/public/browser/web_contents_binding_set.h"
[email protected]674bc592011-12-20 23:00:42128#include "content/public/browser/web_contents_delegate.h"
Lei Zhangee3b63ad2018-06-06 23:25:31129#include "content/public/browser/web_ui_controller.h"
[email protected]e091df82011-10-11 18:13:21130#include "content/public/common/bindings_policy.h"
alexmosc9e76ec2016-05-16 22:59:37131#include "content/public/common/child_process_host.h"
[email protected]54087fe2011-10-28 22:02:48132#include "content/public/common/content_constants.h"
Francois Doray3f2afbd2018-04-06 19:18:18133#include "content/public/common/content_features.h"
[email protected]64d69de42012-02-06 00:19:54134#include "content/public/common/content_switches.h"
Lukasz Anforowicze1b954d92017-10-30 21:28:06135#include "content/public/common/page_state.h"
[email protected]7940b8e2013-07-25 23:08:49136#include "content/public/common/page_zoom.h"
Leon Han963dc182018-11-06 05:41:48137#include "content/public/common/referrer_type_converters.h"
[email protected]04cbd3d2013-12-04 04:58:20138#include "content/public/common/result_codes.h"
[email protected]f16cc6782013-12-16 23:42:57139#include "content/public/common/url_utils.h"
[email protected]b3a97b52014-07-09 06:25:05140#include "content/public/common/web_preferences.h"
Max Morinddebb972018-09-20 10:04:14141#include "media/base/user_input_monitor.h"
wjmaclean64951902016-04-29 20:59:12142#include "net/base/url_util.h"
[email protected]8bfc8272013-09-09 20:10:53143#include "net/http/http_cache.h"
144#include "net/http/http_transaction_factory.h"
rhalavati0fbaeef32017-06-01 15:56:30145#include "net/traffic_annotation/network_traffic_annotation.h"
[email protected]8bfc8272013-09-09 20:10:53146#include "net/url_request/url_request_context.h"
[email protected]abe2c032011-03-31 18:49:34147#include "net/url_request/url_request_context_getter.h"
Scott Violet02e38b92018-03-27 23:42:14148#include "ppapi/buildflags/buildflags.h"
Ke He31d0bb02018-02-24 07:16:24149#include "services/device/public/mojom/constants.mojom.h"
Mounir Lamourifd9dcefa2017-07-06 10:26:55150#include "services/metrics/public/cpp/ukm_recorder.h"
John Abd-El-Malekb175a612018-12-03 20:11:58151#include "services/network/public/cpp/features.h"
Ke He7319dbe2017-11-09 05:54:44152#include "services/service_manager/public/cpp/connector.h"
rockot734fb662016-10-15 16:41:30153#include "services/service_manager/public/cpp/interface_provider.h"
Becca Hughes6daf5662018-06-27 16:50:54154#include "third_party/blink/public/common/associated_interfaces/associated_interface_provider.h"
Blink Reformata30d4232018-04-07 15:31:06155#include "third_party/blink/public/common/frame/sandbox_flags.h"
156#include "third_party/blink/public/common/mime_util/mime_util.h"
Takuto Ikutaaa3b796c2019-02-06 02:54:56157#include "third_party/blink/public/mojom/loader/pause_subresource_loading_handle.mojom.h"
Antonio Gomes0d42960a2019-06-05 12:35:51158#include "third_party/blink/public/mojom/mediastream/media_stream.mojom-shared.h"
Blink Reformata30d4232018-04-07 15:31:06159#include "third_party/blink/public/platform/web_security_style.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"
[email protected]155c7f22013-12-09 17:07:18175#include "content/browser/web_contents/web_contents_android.h"
Ke He31d0bb02018-02-24 07:16:24176#include "services/device/public/mojom/nfc.mojom.h"
mcnee336ea2c2017-05-23 22:50:59177#else // !OS_ANDROID
178#include "content/browser/host_zoom_map_impl.h"
179#include "content/browser/host_zoom_map_observer.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 {
[email protected]420ae012009-04-24 05:16:32187namespace {
188
[email protected]960b0372014-05-19 18:01:00189const int kMinimumDelayBetweenLoadingUpdatesMS = 100;
[email protected]82114f52012-03-20 22:53:41190const char kDotGoogleDotCom[] = ".google.com";
[email protected]ca406032011-07-19 21:53:05191
Avi Drissmana9114822018-03-23 18:25:55192base::LazyInstance<std::vector<
193 WebContentsImpl::FriendWrapper::CreatedCallback>>::DestructorAtExit
194 g_created_callbacks = LAZY_INSTANCE_INITIALIZER;
[email protected]cbb1ef592013-06-05 19:49:46195
[email protected]8bfc8272013-09-09 20:10:53196void NotifyCacheOnIO(
197 scoped_refptr<net::URLRequestContextGetter> request_context,
198 const GURL& url,
Maks Orlovich158ea092018-12-17 22:16:12199 const std::string& http_method,
200 const base::Optional<url::Origin>& top_frame_origin) {
gunsche668855c2015-03-05 02:37:58201 net::HttpCache* cache = request_context->GetURLRequestContext()->
202 http_transaction_factory()->GetCache();
203 if (cache)
Maks Orlovich158ea092018-12-17 22:16:12204 cache->OnExternalCacheHit(url, http_method, top_frame_origin);
[email protected]8bfc8272013-09-09 20:10:53205}
206
dcheng57e39e22016-01-21 00:25:38207bool HasMatchingProcess(FrameTree* tree, int render_process_id) {
208 for (FrameTreeNode* node : tree->Nodes()) {
209 if (node->current_frame_host()->GetProcess()->GetID() == render_process_id)
210 return true;
alexmos4cf2aa32015-07-15 23:40:43211 }
dcheng57e39e22016-01-21 00:25:38212 return false;
[email protected]95640212014-07-26 18:14:30213}
214
kenrb19221852016-04-29 17:21:40215bool HasMatchingWidgetHost(FrameTree* tree, RenderWidgetHost* host) {
216 // This method scans the frame tree rather than checking whether
217 // host->delegate() == this, which allows it to return false when the host
218 // for a frame that is pending or pending deletion.
219 if (!host)
220 return false;
221
222 for (FrameTreeNode* node : tree->Nodes()) {
223 if (node->current_frame_host()->GetRenderWidgetHost() == host)
224 return true;
225 }
226 return false;
227}
228
dmazzonif8a31042016-12-03 00:52:13229void UpdateAccessibilityModeOnFrame(RenderFrameHost* frame_host) {
230 static_cast<RenderFrameHostImpl*>(frame_host)->UpdateAccessibilityMode();
[email protected]95640212014-07-26 18:14:30231}
232
dtseng0dd3fa12015-07-22 19:00:52233void ResetAccessibility(RenderFrameHost* rfh) {
234 static_cast<RenderFrameHostImpl*>(rfh)->AccessibilityReset();
235}
236
paulmeyerfeafc2d2017-04-25 21:46:40237// Helper for GetInnerWebContents().
Lucas Furukawa Gadani2ec00c82018-12-14 15:53:16238bool GetInnerWebContentsHelper(std::vector<WebContents*>* all_guest_contents,
239 WebContents* guest_contents) {
Ehsan Karamad6beb2ea2018-11-25 18:15:13240 auto* web_contents_impl = static_cast<WebContentsImpl*>(guest_contents);
241 if (web_contents_impl->GetBrowserPluginGuest()->attached() &&
242 !GuestMode::IsCrossProcessFrameGuest(web_contents_impl)) {
243 all_guest_contents->push_back(web_contents_impl);
244 }
paulmeyerfeafc2d2017-04-25 21:46:40245 return false;
246}
247
Ian Clelland5cbaaf82017-11-27 22:00:03248RenderFrameHostImpl* FindOpenerRFH(const WebContents::CreateParams& params) {
249 RenderFrameHostImpl* opener_rfh = nullptr;
alexmose201c7cd2015-06-10 17:14:21250 if (params.opener_render_frame_id != MSG_ROUTING_NONE) {
Ian Clelland5cbaaf82017-11-27 22:00:03251 opener_rfh = RenderFrameHostImpl::FromID(params.opener_render_process_id,
252 params.opener_render_frame_id);
alexmose201c7cd2015-06-10 17:14:21253 }
Ian Clelland5cbaaf82017-11-27 22:00:03254 return opener_rfh;
lukasza6f8ac622017-06-06 03:10:20255}
256
Dominick Ng37a15052018-07-17 23:56:35257// Returns |true| if |type| is the kind of user input that should trigger the
258// user interaction observers.
259bool IsUserInteractionInputType(blink::WebInputEvent::Type type) {
260 // Ideally, this list would be based more off of
261 // https://whatwg.org/C/interaction.html#triggered-by-user-activation.
262 return type == blink::WebInputEvent::kMouseDown ||
263 type == blink::WebInputEvent::kGestureScrollBegin ||
264 type == blink::WebInputEvent::kTouchStart ||
265 type == blink::WebInputEvent::kRawKeyDown;
266}
267
268// Returns |true| if |type| is the kind of user input that should be used as
269// a user gesture signal for resource load dispatches.
270bool IsResourceLoadUserInteractionInputType(blink::WebInputEvent::Type type) {
271 return type == blink::WebInputEvent::kMouseDown ||
272 type == blink::WebInputEvent::kTouchStart ||
273 type == blink::WebInputEvent::kRawKeyDown;
274}
275
Pavel Feldman3c1842b2017-08-02 05:00:16276// Ensures that OnDialogClosed is only called once.
277class CloseDialogCallbackWrapper
278 : public base::RefCountedThreadSafe<CloseDialogCallbackWrapper> {
279 public:
Avi Drissmane04d3992017-10-05 15:11:36280 using CloseCallback =
281 base::OnceCallback<void(bool, bool, const base::string16&)>;
282
283 explicit CloseDialogCallbackWrapper(CloseCallback callback)
284 : callback_(std::move(callback)) {}
Pavel Feldman3c1842b2017-08-02 05:00:16285
286 void Run(bool dialog_was_suppressed,
287 bool success,
288 const base::string16& user_input) {
Avi Drissmane04d3992017-10-05 15:11:36289 if (callback_.is_null())
Pavel Feldman3c1842b2017-08-02 05:00:16290 return;
Avi Drissmane04d3992017-10-05 15:11:36291 std::move(callback_).Run(dialog_was_suppressed, success, user_input);
Pavel Feldman3c1842b2017-08-02 05:00:16292 }
293
294 private:
295 friend class base::RefCountedThreadSafe<CloseDialogCallbackWrapper>;
296 ~CloseDialogCallbackWrapper() {}
Avi Drissmane04d3992017-10-05 15:11:36297
298 CloseCallback callback_;
Pavel Feldman3c1842b2017-08-02 05:00:16299};
300
Arthur Sonzogni929d29f2018-09-17 14:19:44301bool FrameCompareDepth(RenderFrameHostImpl* a, RenderFrameHostImpl* b) {
302 return a->frame_tree_node()->depth() < b->frame_tree_node()->depth();
303}
304
lukasza6f8ac622017-06-06 03:10:20305} // namespace
306
Erik Chenbb8e738e2018-04-28 14:10:43307std::unique_ptr<WebContents> WebContents::Create(
308 const WebContents::CreateParams& params) {
erikchen50735fd2018-05-05 15:08:33309 return WebContentsImpl::CreateWithOpener(params, FindOpenerRFH(params));
[email protected]d1198fd2012-08-13 22:50:19310}
311
erikchenade1fef2018-05-02 22:07:57312std::unique_ptr<WebContents> WebContents::CreateWithSessionStorage(
[email protected]54944cde2012-12-09 09:24:59313 const WebContents::CreateParams& params,
[email protected]fdac6ade2013-07-20 01:06:30314 const SessionStorageNamespaceMap& session_storage_namespace_map) {
erikchenade1fef2018-05-02 22:07:57315 std::unique_ptr<WebContentsImpl> new_contents(
316 new WebContentsImpl(params.browser_context));
Ian Clelland5cbaaf82017-11-27 22:00:03317 RenderFrameHostImpl* opener_rfh = FindOpenerRFH(params);
318 FrameTreeNode* opener = nullptr;
319 if (opener_rfh)
320 opener = opener_rfh->frame_tree_node();
321 new_contents->SetOpenerForNewContents(opener, params.opener_suppressed);
[email protected]fdac6ade2013-07-20 01:06:30322
jdoerrie55ec69d2018-10-08 13:34:46323 for (auto it = session_storage_namespace_map.begin();
324 it != session_storage_namespace_map.end(); ++it) {
[email protected]fdac6ade2013-07-20 01:06:30325 new_contents->GetController()
326 .SetSessionStorageNamespace(it->first, it->second.get());
327 }
328
Sean Toppingbbc4a2bd2019-01-30 02:16:14329 WebContentsImpl* outer_web_contents = nullptr;
Sam McNally69a33772018-06-27 16:54:12330 if (params.guest_delegate) {
331 // This makes |new_contents| act as a guest.
332 // For more info, see comment above class BrowserPluginGuest.
Albert J. Wongc55cc64d2018-10-12 02:50:04333 BrowserPluginGuest::CreateInWebContents(new_contents.get(),
334 params.guest_delegate);
Sean Toppingbbc4a2bd2019-01-30 02:16:14335 outer_web_contents = static_cast<WebContentsImpl*>(
336 params.guest_delegate->GetOwnerWebContents());
Sam McNally69a33772018-06-27 16:54:12337 }
338
[email protected]54944cde2012-12-09 09:24:59339 new_contents->Init(params);
Sean Toppingbbc4a2bd2019-01-30 02:16:14340 if (outer_web_contents)
341 outer_web_contents->InnerWebContentsCreated(new_contents.get());
[email protected]d1198fd2012-08-13 22:50:19342 return new_contents;
[email protected]a81343d232011-12-27 07:39:20343}
[email protected]746d3052012-05-22 15:15:47344
benwells5e7260d2017-03-20 23:36:33345void WebContentsImpl::FriendWrapper::AddCreatedCallbackForTesting(
nickf9acfbe2014-12-23 19:12:37346 const CreatedCallback& callback) {
[email protected]cbb1ef592013-06-05 19:49:46347 g_created_callbacks.Get().push_back(callback);
348}
349
benwells5e7260d2017-03-20 23:36:33350void WebContentsImpl::FriendWrapper::RemoveCreatedCallbackForTesting(
nickf9acfbe2014-12-23 19:12:37351 const CreatedCallback& callback) {
[email protected]cbb1ef592013-06-05 19:49:46352 for (size_t i = 0; i < g_created_callbacks.Get().size(); ++i) {
Robert Liao1db5a352019-06-05 20:57:34353 if (g_created_callbacks.Get().at(i) == callback) {
[email protected]cbb1ef592013-06-05 19:49:46354 g_created_callbacks.Get().erase(g_created_callbacks.Get().begin() + i);
355 return;
356 }
357 }
358}
359
avidf38c952015-10-27 13:45:13360WebContents* WebContents::FromRenderViewHost(RenderViewHost* rvh) {
361 if (!rvh)
362 return nullptr;
[email protected]746d3052012-05-22 15:15:47363 return rvh->GetDelegate()->GetAsWebContents();
364}
365
[email protected]a86c0e962013-12-17 17:10:39366WebContents* WebContents::FromRenderFrameHost(RenderFrameHost* rfh) {
avidf38c952015-10-27 13:45:13367 if (!rfh)
368 return nullptr;
369 return static_cast<RenderFrameHostImpl*>(rfh)->delegate()->GetAsWebContents();
[email protected]a86c0e962013-12-17 17:10:39370}
371
ananta4b7467a52016-09-23 01:42:38372WebContents* WebContents::FromFrameTreeNodeId(int frame_tree_node_id) {
373 FrameTreeNode* frame_tree_node =
374 FrameTreeNode::GloballyFindByID(frame_tree_node_id);
anantab8cfa3b2016-10-14 22:43:19375 if (!frame_tree_node)
376 return nullptr;
jam8c4edd02017-05-06 18:50:33377 return WebContentsImpl::FromFrameTreeNode(frame_tree_node);
ananta4b7467a52016-09-23 01:42:38378}
379
leon.han552e9de2017-02-09 14:37:30380void WebContents::SetScreenOrientationDelegate(
381 ScreenOrientationDelegate* delegate) {
382 ScreenOrientationProvider::SetDelegate(delegate);
383}
384
[email protected]7fff43e2013-05-21 20:21:10385// WebContentsImpl::DestructionObserver ----------------------------------------
386
387class WebContentsImpl::DestructionObserver : public WebContentsObserver {
388 public:
389 DestructionObserver(WebContentsImpl* owner, WebContents* watched_contents)
390 : WebContentsObserver(watched_contents),
391 owner_(owner) {
392 }
393
394 // WebContentsObserver:
dchengc2282aa2014-10-21 12:07:58395 void WebContentsDestroyed() override {
[email protected]12a46832014-05-09 13:35:58396 owner_->OnWebContentsDestroyed(
397 static_cast<WebContentsImpl*>(web_contents()));
[email protected]7fff43e2013-05-21 20:21:10398 }
399
400 private:
401 WebContentsImpl* owner_;
402
403 DISALLOW_COPY_AND_ASSIGN(DestructionObserver);
404};
405
Joel Hockey85b379d2017-12-11 10:42:13406// WebContentsImpl::ColorChooser ----------------------------------------------
Joel Hockey163835a2017-12-20 11:51:57407class WebContentsImpl::ColorChooser : public blink::mojom::ColorChooser {
Joel Hockey85b379d2017-12-11 10:42:13408 public:
409 ColorChooser(content::ColorChooser* chooser,
Joel Hockey163835a2017-12-20 11:51:57410 blink::mojom::ColorChooserRequest request,
411 blink::mojom::ColorChooserClientPtr client)
Joel Hockey85b379d2017-12-11 10:42:13412 : chooser_(chooser),
413 binding_(this, std::move(request)),
414 client_(std::move(client)) {
415 binding_.set_connection_error_handler(
416 base::BindOnce([](content::ColorChooser* chooser) { chooser->End(); },
417 base::Unretained(chooser)));
418 }
[email protected]8ed16472014-04-11 19:02:48419
Joel Hockey85b379d2017-12-11 10:42:13420 ~ColorChooser() override { chooser_->End(); }
[email protected]8ed16472014-04-11 19:02:48421
Joel Hockey85b379d2017-12-11 10:42:13422 void SetSelectedColor(SkColor color) override {
423 chooser_->SetSelectedColor(color);
424 }
425
426 void DidChooseColorInColorChooser(SkColor color) {
427 client_->DidChooseColor(color);
428 }
429
430 private:
431 // Color chooser that was opened by this tab.
432 std::unique_ptr<content::ColorChooser> chooser_;
433
434 // mojo bindings.
Joel Hockey163835a2017-12-20 11:51:57435 mojo::Binding<blink::mojom::ColorChooser> binding_;
Joel Hockey85b379d2017-12-11 10:42:13436
437 // mojo renderer client.
Joel Hockey163835a2017-12-20 11:51:57438 blink::mojom::ColorChooserClientPtr client_;
Joel Hockey85b379d2017-12-11 10:42:13439};
nicka0ac8382016-12-15 23:59:23440
lazyboy6ec48b2a2015-06-29 15:18:14441// WebContentsImpl::WebContentsTreeNode ----------------------------------------
lfg601233692017-03-06 22:45:44442WebContentsImpl::WebContentsTreeNode::WebContentsTreeNode(
443 WebContentsImpl* current_web_contents)
444 : current_web_contents_(current_web_contents),
445 outer_web_contents_(nullptr),
lazyboy6ec48b2a2015-06-29 15:18:14446 outer_contents_frame_tree_node_id_(
avallee0206f782016-07-28 18:55:33447 FrameTreeNode::kFrameTreeNodeInvalidId),
lfg601233692017-03-06 22:45:44448 focused_web_contents_(current_web_contents) {}
lazyboy6ec48b2a2015-06-29 15:18:14449
Lucas Furukawa Gadani3d38ec152018-10-26 20:55:18450WebContentsImpl::WebContentsTreeNode::~WebContentsTreeNode() {}
lazyboy6ec48b2a2015-06-29 15:18:14451
Lucas Furukawa Gadani99125822019-01-03 15:41:49452std::unique_ptr<WebContents>
453WebContentsImpl::WebContentsTreeNode::DisconnectFromOuterWebContents() {
454 std::unique_ptr<WebContents> inner_contents =
455 outer_web_contents_->node_.DetachInnerWebContents(current_web_contents_);
456 OuterContentsFrameTreeNode()->RemoveObserver(this);
457 outer_contents_frame_tree_node_id_ = FrameTreeNode::kFrameTreeNodeInvalidId;
458 outer_web_contents_ = nullptr;
459 return inner_contents;
460}
461
paulmeyerfeafc2d2017-04-25 21:46:40462void WebContentsImpl::WebContentsTreeNode::AttachInnerWebContents(
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:50463 std::unique_ptr<WebContents> inner_web_contents,
464 RenderFrameHostImpl* render_frame_host) {
465 WebContentsImpl* inner_web_contents_impl =
466 static_cast<WebContentsImpl*>(inner_web_contents.get());
467 WebContentsTreeNode& inner_web_contents_node = inner_web_contents_impl->node_;
468
469 inner_web_contents_node.focused_web_contents_ = nullptr;
470 inner_web_contents_node.outer_web_contents_ = current_web_contents_;
471 inner_web_contents_node.outer_contents_frame_tree_node_id_ =
472 render_frame_host->frame_tree_node()->frame_tree_node_id();
473
Lucas Furukawa Gadani3d38ec152018-10-26 20:55:18474 inner_web_contents_.push_back(std::move(inner_web_contents));
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:50475
476 render_frame_host->frame_tree_node()->AddObserver(&inner_web_contents_node);
paulmeyerfeafc2d2017-04-25 21:46:40477}
478
Lucas Furukawa Gadani3d38ec152018-10-26 20:55:18479std::unique_ptr<WebContents>
480WebContentsImpl::WebContentsTreeNode::DetachInnerWebContents(
paulmeyerfeafc2d2017-04-25 21:46:40481 WebContentsImpl* inner_web_contents) {
Lucas Furukawa Gadani3d38ec152018-10-26 20:55:18482 std::unique_ptr<WebContents> detached_contents;
483 for (std::unique_ptr<WebContents>& web_contents : inner_web_contents_) {
484 if (web_contents.get() == inner_web_contents) {
485 detached_contents = std::move(web_contents);
486 std::swap(web_contents, inner_web_contents_.back());
487 inner_web_contents_.pop_back();
488 return detached_contents;
489 }
490 }
491
492 NOTREACHED();
493 return nullptr;
paulmeyerfeafc2d2017-04-25 21:46:40494}
495
lfga3e2bdc2017-03-07 20:44:01496FrameTreeNode*
497WebContentsImpl::WebContentsTreeNode::OuterContentsFrameTreeNode() const {
498 return FrameTreeNode::GloballyFindByID(outer_contents_frame_tree_node_id_);
lfg601233692017-03-06 22:45:44499}
500
lfga3e2bdc2017-03-07 20:44:01501void WebContentsImpl::WebContentsTreeNode::OnFrameTreeNodeDestroyed(
502 FrameTreeNode* node) {
503 DCHECK_EQ(outer_contents_frame_tree_node_id_, node->frame_tree_node_id())
504 << "WebContentsTreeNode should only receive notifications for the "
505 "FrameTreeNode in its outer WebContents that hosts it.";
Lucas Furukawa Gadani3d38ec152018-10-26 20:55:18506
507 // Deletes |this| too.
508 outer_web_contents_->node_.DetachInnerWebContents(current_web_contents_);
lazyboy6ec48b2a2015-06-29 15:18:14509}
510
avallee0206f782016-07-28 18:55:33511void WebContentsImpl::WebContentsTreeNode::SetFocusedWebContents(
512 WebContentsImpl* web_contents) {
513 DCHECK(!outer_web_contents())
514 << "Only the outermost WebContents tracks focus.";
515 focused_web_contents_ = web_contents;
516}
517
paulmeyerfeafc2d2017-04-25 21:46:40518WebContentsImpl*
519WebContentsImpl::WebContentsTreeNode::GetInnerWebContentsInFrame(
520 const FrameTreeNode* frame) {
521 auto ftn_id = frame->frame_tree_node_id();
Lucas Furukawa Gadani3d38ec152018-10-26 20:55:18522 for (auto& contents : inner_web_contents_) {
523 WebContentsImpl* impl = static_cast<WebContentsImpl*>(contents.get());
524 if (impl->node_.outer_contents_frame_tree_node_id() == ftn_id) {
525 return impl;
paulmeyerfeafc2d2017-04-25 21:46:40526 }
527 }
528 return nullptr;
529}
530
Lucas Furukawa Gadani3d38ec152018-10-26 20:55:18531std::vector<WebContentsImpl*>
532WebContentsImpl::WebContentsTreeNode::GetInnerWebContents() const {
533 std::vector<WebContentsImpl*> inner_web_contents;
534 for (auto& contents : inner_web_contents_)
535 inner_web_contents.push_back(static_cast<WebContentsImpl*>(contents.get()));
536
537 return inner_web_contents;
paulmeyerfeafc2d2017-04-25 21:46:40538}
539
[email protected]b172aee2012-04-10 17:05:26540// WebContentsImpl -------------------------------------------------------------
[email protected]420ae012009-04-24 05:16:32541
alexmose201c7cd2015-06-10 17:14:21542WebContentsImpl::WebContentsImpl(BrowserContext* browser_context)
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28543 : delegate_(nullptr),
[email protected]69e797f2013-04-30 01:10:22544 controller_(this, browser_context),
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28545 render_view_host_delegate_view_(nullptr),
alexmose201c7cd2015-06-10 17:14:21546 created_with_opener_(false),
Lucas Furukawa Gadani99125822019-01-03 15:41:49547 node_(this),
[email protected]6ea6bdf2013-12-06 13:35:01548 frame_tree_(new NavigatorImpl(&controller_, this),
blundellc57b93f2014-10-29 13:19:57549 this,
550 this,
551 this,
552 this),
[email protected]6dfed692014-05-22 04:18:03553 is_load_to_different_document_(false),
[email protected]443b80e2010-12-14 00:42:23554 crashed_status_(base::TERMINATION_STATUS_STILL_RUNNING),
555 crashed_error_code_(0),
[email protected]d5f942ba2008-09-26 19:30:34556 waiting_for_response_(false),
[email protected]fcf75d42013-12-03 20:11:26557 load_state_(net::LOAD_STATE_IDLE, base::string16()),
[email protected]094e5b22009-09-25 04:23:56558 upload_size_(0),
559 upload_position_(0),
dfalcantarae6b7b46752015-07-10 18:14:16560 is_resume_pending_(false),
creisadca9862017-06-16 19:12:11561 interstitial_page_(nullptr),
[email protected]b4c84012014-04-28 19:51:10562 has_accessed_initial_document_(false),
peter90afaba2015-06-01 12:05:29563 did_first_visually_non_empty_paint_(false),
[email protected]54597982013-02-06 01:59:55564 capturer_count_(0),
[email protected]fdd61c62009-04-22 19:22:57565 is_being_destroyed_(false),
creis97867aa2017-02-17 18:39:45566 is_notifying_observers_(false),
[email protected]fdd61c62009-04-22 19:22:57567 notify_disconnection_(false),
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28568 dialog_manager_(nullptr),
[email protected]7ab1e7d62009-10-14 23:32:01569 is_showing_before_unload_dialog_(false),
gab0dccfef2015-05-20 18:43:39570 last_active_time_(base::TimeTicks::Now()),
[email protected]ebf40a72010-07-22 01:46:38571 closed_by_user_gesture_(false),
[email protected]8ff00d72012-10-23 19:12:21572 minimum_zoom_percent_(static_cast<int>(kMinimumZoomFactor * 100)),
573 maximum_zoom_percent_(static_cast<int>(kMaximumZoomFactor * 100)),
w.shackleton49bcd392016-01-06 17:38:22574 zoom_scroll_remainder_(0),
alexmosc9e76ec2016-05-16 22:59:37575 fullscreen_widget_process_id_(ChildProcessHost::kInvalidUniqueID),
[email protected]5dcaf8e2013-12-28 01:31:42576 fullscreen_widget_routing_id_(MSG_ROUTING_NONE),
[email protected]0b431992014-06-24 00:08:03577 fullscreen_widget_had_focus_at_shutdown_(false),
[email protected]34ff1cfc2014-08-20 06:16:05578 force_disable_overscroll_content_(false),
[email protected]95640212014-07-26 18:14:30579 last_dialog_suppressed_(false),
waffles1c12bf402016-02-10 17:45:48580 accessibility_mode_(
Dominic Mazzoni21fb0282019-02-13 18:32:47581 GetContentClient()->browser()->GetAXModeForBrowserContext(
582 browser_context)),
avayvodcc85bbd2015-08-28 19:11:15583 audio_stream_monitor_(this),
Thomas Guilbert882c4cec2019-02-14 05:55:45584 media_web_contents_observer_(
585 std::make_unique<MediaWebContentsObserver>(this)),
Guido Urdanetaf4008002018-05-03 10:43:04586 media_device_group_id_salt_base_(
587 BrowserContext::CreateRandomMediaDeviceIDSalt()),
mcnee336ea2c2017-05-23 22:50:59588#if !defined(OS_ANDROID)
mcnee432e47d2015-11-09 19:37:46589 page_scale_factor_is_one_(true),
mcnee336ea2c2017-05-23 22:50:59590#endif // !defined(OS_ANDROID)
qinmin72e8bd02016-10-21 19:35:37591 is_overlay_content_(false),
ekaramadf6750aa2017-06-06 18:29:42592 showing_context_menu_(false),
W. James MacLean62198672019-06-04 16:46:10593 text_autosizer_page_info_({0, 0, 1.f}),
Shivani Sharma985474f2019-05-23 21:36:47594 had_inner_webcontents_(false),
pkotwicz75ca8ffd2016-02-16 23:10:19595 loading_weak_factory_(this),
596 weak_factory_(this) {
[email protected]9b159a52013-10-03 17:24:55597 frame_tree_.SetFrameRemoveListener(
598 base::Bind(&WebContentsImpl::OnFrameRemoved,
599 base::Unretained(this)));
brettw4b461082016-11-19 18:55:16600#if BUILDFLAG(ENABLE_PLUGINS)
zqzhang181047e62016-07-01 13:37:17601 pepper_playback_observer_.reset(new PepperPlaybackObserver(this));
602#endif
blundelle75a8f92017-03-27 08:11:17603
csharrisona2280cd2016-02-03 23:21:15604 loader_io_thread_notifier_.reset(new LoaderIOThreadNotifier(this));
mcnee336ea2c2017-05-23 22:50:59605#if !defined(OS_ANDROID)
scottmg276753cf2016-10-27 18:25:22606 host_zoom_map_observer_.reset(new HostZoomMapObserver(this));
mcnee336ea2c2017-05-23 22:50:59607#endif // !defined(OS_ANDROID)
Joel Hockey85b379d2017-12-11 10:42:13608
Becca Hughes86334622018-07-09 21:29:38609#if defined(OS_ANDROID)
Elly Fong-Jonescdd7ae82019-02-20 23:26:33610 display_cutout_host_impl_ = std::make_unique<DisplayCutoutHostImpl>(this);
Becca Hughes86334622018-07-09 21:29:38611#endif
Becca Hughes6fe54e432018-06-09 04:18:56612
Joel Hockey85b379d2017-12-11 10:42:13613 registry_.AddInterface(base::BindRepeating(
614 &WebContentsImpl::OnColorChooserFactoryRequest, base::Unretained(this)));
Rune Lillesveen5d8c49982019-04-24 19:07:30615
Findit72e2f8a2019-06-29 00:07:58616 dark_mode_observer_.Start();
[email protected]332af7732009-03-11 13:21:35617}
initial.commit09911bf2008-07-26 23:55:29618
[email protected]b172aee2012-04-10 17:05:26619WebContentsImpl::~WebContentsImpl() {
creisba91ba82017-05-12 22:56:19620 // Imperfect sanity check against double free, given some crashes unexpectedly
621 // observed in the wild.
622 CHECK(!is_being_destroyed_);
623
624 // We generally keep track of is_being_destroyed_ to let other features know
625 // to avoid certain actions during destruction.
[email protected]420ae012009-04-24 05:16:32626 is_being_destroyed_ = true;
627
creis97867aa2017-02-17 18:39:45628 // A WebContents should never be deleted while it is notifying observers,
creisba91ba82017-05-12 22:56:19629 // since this will lead to a use-after-free as it continues to notify later
creis97867aa2017-02-17 18:39:45630 // observers.
631 CHECK(!is_notifying_observers_);
632
kenrb2a565f82015-09-02 20:24:59633 rwh_input_event_router_.reset();
634
rockotf62002a2016-09-15 00:08:59635 for (auto& entry : binding_sets_)
636 entry.second->CloseAllBindings();
637
avallee0206f782016-07-28 18:55:33638 WebContentsImpl* outermost = GetOutermostWebContents();
avallee9993fca2016-11-17 06:16:50639 if (this != outermost && ContainsOrIsFocusedWebContents()) {
avallee0206f782016-07-28 18:55:33640 // If the current WebContents is in focus, unset it.
avallee9993fca2016-11-17 06:16:50641 outermost->SetAsFocusedWebContentsIfNecessary();
avallee0206f782016-07-28 18:55:33642 }
643
Nick Carterd73635b2018-03-13 18:31:41644 if (mouse_lock_widget_)
645 mouse_lock_widget_->RejectMouseLockOrUnlockIfNecessary();
646
dcheng72be33c2016-01-21 04:11:31647 for (FrameTreeNode* node : frame_tree_.Nodes()) {
648 // Delete all RFHs pending shutdown, which will lead the corresponding RVHs
649 // to be shutdown and be deleted as well.
dcheng57e39e22016-01-21 00:25:38650 node->render_manager()->ClearRFHsPendingShutdown();
dcheng57e39e22016-01-21 00:25:38651 node->render_manager()->ClearWebUIInstances();
dcheng72be33c2016-01-21 04:11:31652 }
carlosk35f35af2015-12-01 10:55:40653
dcheng57e39e22016-01-21 00:25:38654 for (RenderWidgetHostImpl* widget : created_widgets_)
655 widget->DetachDelegate();
[email protected]b24b68a2012-09-24 21:57:26656 created_widgets_.clear();
657
[email protected]3ab9cb82011-06-03 18:02:07658 // Clear out any JavaScript state.
avi5d3b8692016-10-12 22:00:46659 if (dialog_manager_) {
avi6879fcf2017-01-21 05:27:53660 dialog_manager_->CancelDialogs(this, /*reset_state=*/true);
avi5d3b8692016-10-12 22:00:46661 }
[email protected]3ab9cb82011-06-03 18:02:07662
Joel Hockey85b379d2017-12-11 10:42:13663 color_chooser_.reset();
Rakina Zata Amni16196122018-11-14 02:14:00664 find_request_manager_.reset();
[email protected]da8543762012-03-20 08:52:20665
[email protected]420ae012009-04-24 05:16:32666 NotifyDisconnected();
[email protected]420ae012009-04-24 05:16:32667
[email protected]ca13a442012-04-17 14:00:12668 // Notify any observer that have a reference on this WebContents.
[email protected]8ff00d72012-10-23 19:12:21669 NotificationService::current()->Notify(
670 NOTIFICATION_WEB_CONTENTS_DESTROYED,
671 Source<WebContents>(this),
672 NotificationService::NoDetails());
[email protected]420ae012009-04-24 05:16:32673
Arthur Sonzogni5a9a88c2018-10-18 13:16:00674 // Destroy all subframes now. This notifies observers.
675 GetMainFrame()->ResetChildren();
[email protected]df0c843f2014-05-23 20:37:23676 GetRenderManager()->ResetProxyHosts();
[email protected]c06c58c2014-03-12 20:31:59677
clamy0e119882015-07-31 16:12:33678 // Manually call the observer methods for the root frame tree node. It is
679 // necessary to manually delete all objects tracking navigations
680 // (NavigationHandle, NavigationRequest) for observers to be properly
681 // notified of these navigations stopping before the WebContents is
682 // destroyed.
[email protected]df0c843f2014-05-23 20:37:23683 RenderFrameHostManager* root = GetRenderManager();
naskob21fe4872015-02-24 14:15:56684
Arthur Sonzogni620cec62018-12-13 13:08:57685 GetController().back_forward_cache().Flush();
naskob21fe4872015-02-24 14:15:56686 root->current_frame_host()->SetRenderFrameCreated(false);
clamyfc3850d2018-02-23 14:35:28687 root->current_frame_host()->ResetNavigationRequests();
clamy0e119882015-07-31 16:12:33688
Arthur Hemeryc3380172018-01-22 14:00:17689 // Do not update state as the WebContents is being destroyed.
690 frame_tree_.root()->ResetNavigationRequest(true, true);
691 if (root->speculative_frame_host()) {
danakjf2342432019-05-15 21:46:09692 root->speculative_frame_host()->DeleteRenderFrame(
693 FrameDeleteIntention::kSpeculativeMainFrameForShutdown);
Arthur Hemeryc3380172018-01-22 14:00:17694 root->speculative_frame_host()->SetRenderFrameCreated(false);
clamyfc3850d2018-02-23 14:35:28695 root->speculative_frame_host()->ResetNavigationRequests();
clamy0e119882015-07-31 16:12:33696 }
[email protected]f273ee52013-10-18 16:05:27697
brettw4b461082016-11-19 18:55:16698#if BUILDFLAG(ENABLE_PLUGINS)
zqzhang181047e62016-07-01 13:37:17699 // Call this before WebContentsDestroyed() is broadcasted since
700 // AudioFocusManager will be destroyed after that.
701 pepper_playback_observer_.reset();
702#endif // defined(ENABLED_PLUGINS)
703
Chris Hamiltondf0d72cd2018-05-29 16:23:53704 // If audio is playing then notify external observers of the audio stream
705 // disappearing.
706 if (is_currently_audible_) {
707 is_currently_audible_ = false;
708 for (auto& observer : observers_)
709 observer.OnAudioStateChanged(false);
710
711 if (GetOuterWebContents())
712 GetOuterWebContents()->OnAudioStateChanged();
713 }
714
ericwilligersde386342016-10-19 02:35:09715 for (auto& observer : observers_)
716 observer.FrameDeleted(root->current_frame_host());
naskoa83483fb2015-02-27 16:57:10717
ericwilligersde386342016-10-19 02:35:09718 for (auto& observer : observers_)
719 observer.RenderViewDeleted(root->current_host());
[email protected]f273ee52013-10-18 16:05:27720
Kevin McNee576bb5d82019-05-29 16:47:51721#if defined(OS_ANDROID)
722 // For simplicity, destroy the Java WebContents before we notify of the
723 // destruction of the WebContents.
724 ClearWebContentsAndroid();
725#endif
726
ericwilligersde386342016-10-19 02:35:09727 for (auto& observer : observers_)
728 observer.WebContentsDestroyed();
[email protected]12a46832014-05-09 13:35:58729
Becca Hughesd11d6502018-07-31 17:01:28730 if (display_cutout_host_impl_)
731 display_cutout_host_impl_->WebContentsDestroyed();
732
ericwilligersde386342016-10-19 02:35:09733 for (auto& observer : observers_)
734 observer.ResetWebContents();
[email protected]232a5812011-03-04 22:42:08735
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28736 SetDelegate(nullptr);
[email protected]b5a1d11c2011-02-17 03:09:42737}
738
erikchen50735fd2018-05-05 15:08:33739std::unique_ptr<WebContentsImpl> WebContentsImpl::CreateWithOpener(
[email protected]54944cde2012-12-09 09:24:59740 const WebContents::CreateParams& params,
Ian Clelland5cbaaf82017-11-27 22:00:03741 RenderFrameHostImpl* opener_rfh) {
[email protected]e11f0e92013-06-12 15:12:03742 TRACE_EVENT0("browser", "WebContentsImpl::CreateWithOpener");
Ian Clelland5cbaaf82017-11-27 22:00:03743 FrameTreeNode* opener = nullptr;
744 if (opener_rfh)
745 opener = opener_rfh->frame_tree_node();
erikchen50735fd2018-05-05 15:08:33746 std::unique_ptr<WebContentsImpl> new_contents(
747 new WebContentsImpl(params.browser_context));
lukasza6f8ac622017-06-06 03:10:20748 new_contents->SetOpenerForNewContents(opener, params.opener_suppressed);
alexmose201c7cd2015-06-10 17:14:21749
alexmosaedfc6f2016-01-21 23:57:38750 // If the opener is sandboxed, a new popup must inherit the opener's sandbox
751 // flags, and these flags take effect immediately. An exception is if the
752 // opener's sandbox flags lack the PropagatesToAuxiliaryBrowsingContexts
753 // bit (which is controlled by the "allow-popups-to-escape-sandbox" token).
754 // See https://html.spec.whatwg.org/#attr-iframe-sandbox.
lukasza6f8ac622017-06-06 03:10:20755 FrameTreeNode* new_root = new_contents->GetFrameTree()->root();
alexmosaedfc6f2016-01-21 23:57:38756 if (opener) {
Ian Clelland5cbaaf82017-11-27 22:00:03757 blink::WebSandboxFlags opener_flags = opener_rfh->active_sandbox_flags();
alexmosaedfc6f2016-01-21 23:57:38758 const blink::WebSandboxFlags inherit_flag =
Blink Reformat1c4d759e2017-04-09 16:34:54759 blink::WebSandboxFlags::kPropagatesToAuxiliaryBrowsingContexts;
Ehsan Karamad39407082019-02-19 23:38:19760 bool sandbox_propagates_to_auxilary_context =
761 (opener_flags & inherit_flag) == inherit_flag;
762 if (sandbox_propagates_to_auxilary_context)
Ian Clellandcdc4f312017-10-13 22:24:12763 new_root->SetPendingFramePolicy({opener_flags, {}});
Ehsan Karamad39407082019-02-19 23:38:19764 if (opener_flags == blink::WebSandboxFlags::kNone ||
765 sandbox_propagates_to_auxilary_context) {
766 // TODO(ekaramad, iclelland): Do not propagate feature policies from non-
767 // sandboxed disowned openers (rel=noopener).
768 // If the current page is not sandboxed, or if the sandbox is to propagate
769 // to the popups then opener's feature policy will apply to the new popup
770 // as well.
771 new_root->SetOpenerFeaturePolicyState(
Ehsan Karamad34d10682019-02-26 20:55:04772 opener_rfh->feature_policy()->GetFeatureState());
alexmosaedfc6f2016-01-21 23:57:38773 }
774 }
775
mark a. foltzef394fce2017-10-21 09:11:02776 // Apply starting sandbox flags.
Luna Luc3fdacdf2017-11-08 04:48:53777 blink::FramePolicy frame_policy(new_root->pending_frame_policy());
mark a. foltzef394fce2017-10-21 09:11:02778 frame_policy.sandbox_flags |= params.starting_sandbox_flags;
779 new_root->SetPendingFramePolicy(frame_policy);
780 new_root->CommitPendingFramePolicy();
781
alexmose201c7cd2015-06-10 17:14:21782 // This may be true even when opener is null, such as when opening blocked
783 // popups.
alexmos090fae8e2015-05-28 17:09:28784 if (params.created_with_opener)
785 new_contents->created_with_opener_ = true;
786
Sean Toppingbbc4a2bd2019-01-30 02:16:14787 WebContentsImpl* outer_web_contents = nullptr;
[email protected]4858e432014-06-23 18:14:17788 if (params.guest_delegate) {
[email protected]83100cd2014-05-10 11:50:06789 // This makes |new_contents| act as a guest.
790 // For more info, see comment above class BrowserPluginGuest.
Albert J. Wongc55cc64d2018-10-12 02:50:04791 BrowserPluginGuest::CreateInWebContents(new_contents.get(),
792 params.guest_delegate);
Sean Toppingbbc4a2bd2019-01-30 02:16:14793 outer_web_contents = static_cast<WebContentsImpl*>(
794 params.guest_delegate->GetOwnerWebContents());
[email protected]83100cd2014-05-10 11:50:06795 }
mark a. foltzef394fce2017-10-21 09:11:02796
[email protected]54944cde2012-12-09 09:24:59797 new_contents->Init(params);
Sean Toppingbbc4a2bd2019-01-30 02:16:14798 if (outer_web_contents)
799 outer_web_contents->InnerWebContentsCreated(new_contents.get());
[email protected]d1198fd2012-08-13 22:50:19800 return new_contents;
801}
802
[email protected]95640212014-07-26 18:14:30803// static
804std::vector<WebContentsImpl*> WebContentsImpl::GetAllWebContents() {
805 std::vector<WebContentsImpl*> result;
dcheng59716272016-04-09 05:19:08806 std::unique_ptr<RenderWidgetHostIterator> widgets(
[email protected]95640212014-07-26 18:14:30807 RenderWidgetHostImpl::GetRenderWidgetHosts());
[email protected]95640212014-07-26 18:14:30808 while (RenderWidgetHost* rwh = widgets->GetNextHost()) {
[email protected]95640212014-07-26 18:14:30809 RenderViewHost* rvh = RenderViewHost::From(rwh);
810 if (!rvh)
811 continue;
812 WebContents* web_contents = WebContents::FromRenderViewHost(rvh);
813 if (!web_contents)
814 continue;
avidf38c952015-10-27 13:45:13815 if (web_contents->GetRenderViewHost() != rvh)
816 continue;
817 // Because a WebContents can only have one current RVH at a time, there will
818 // be no duplicate WebContents here.
819 result.push_back(static_cast<WebContentsImpl*>(web_contents));
[email protected]95640212014-07-26 18:14:30820 }
821 return result;
822}
823
clamya16aa8172015-05-26 13:07:25824// static
825WebContentsImpl* WebContentsImpl::FromFrameTreeNode(
paulmeyerfeafc2d2017-04-25 21:46:40826 const FrameTreeNode* frame_tree_node) {
clamya16aa8172015-05-26 13:07:25827 return static_cast<WebContentsImpl*>(
828 WebContents::FromRenderFrameHost(frame_tree_node->current_frame_host()));
829}
830
clamy0d32d6d2015-11-24 11:16:26831// static
832WebContents* WebContentsImpl::FromRenderFrameHostID(int render_process_host_id,
833 int render_frame_host_id) {
Min Qina1150112019-02-15 01:47:49834 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) ||
835 !BrowserThread::IsThreadInitialized(BrowserThread::UI));
clamy0d32d6d2015-11-24 11:16:26836 RenderFrameHost* render_frame_host =
837 RenderFrameHost::FromID(render_process_host_id, render_frame_host_id);
838 if (!render_frame_host)
839 return nullptr;
840
841 return WebContents::FromRenderFrameHost(render_frame_host);
842}
843
paulmeyerfeafc2d2017-04-25 21:46:40844// static
845WebContentsImpl* WebContentsImpl::FromOuterFrameTreeNode(
846 const FrameTreeNode* frame_tree_node) {
847 return WebContentsImpl::FromFrameTreeNode(frame_tree_node)
848 ->node_.GetInnerWebContentsInFrame(frame_tree_node);
849}
850
[email protected]b0936d22013-11-28 06:47:36851RenderFrameHostManager* WebContentsImpl::GetRenderManagerForTesting() {
[email protected]fa944cb82013-11-15 17:51:21852 return GetRenderManager();
[email protected]765187182012-01-11 23:59:28853}
854
nicka0ac8382016-12-15 23:59:23855bool WebContentsImpl::OnMessageReceived(RenderViewHostImpl* render_view_host,
[email protected]7bb761892012-07-20 09:32:47856 const IPC::Message& message) {
nicka0ac8382016-12-15 23:59:23857 for (auto& observer : observers_) {
Lukasz Anforowicz89f2c1b2017-08-30 21:36:46858 // TODO(nick, creis): https://crbug.com/758026: Replace all uses of this
859 // variant of OnMessageReceived with the version that takes a
860 // RenderFrameHost, and then delete it.
nicka0ac8382016-12-15 23:59:23861 if (observer.OnMessageReceived(message))
862 return true;
[email protected]64ffefa2014-05-10 12:06:33863 }
[email protected]403415a2011-01-10 18:57:53864
[email protected]724159a2010-12-30 01:11:18865 bool handled = true;
nicka0ac8382016-12-15 23:59:23866 IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(WebContentsImpl, message, render_view_host)
nicka0ac8382016-12-15 23:59:23867 IPC_MESSAGE_HANDLER(ViewHostMsg_UpdateZoomLimits, OnUpdateZoomLimits)
868 IPC_MESSAGE_HANDLER(ViewHostMsg_PageScaleFactorChanged,
869 OnPageScaleFactorChanged)
W. James MacLean62198672019-06-04 16:46:10870 IPC_MESSAGE_HANDLER(
871 ViewHostMsg_NotifyTextAutosizerPageInfoChangedInLocalMainFrame,
872 OnTextAutosizerPageInfoChanged)
nicka0ac8382016-12-15 23:59:23873#if BUILDFLAG(ENABLE_PLUGINS)
874 IPC_MESSAGE_HANDLER(ViewHostMsg_RequestPpapiBrokerPermission,
875 OnRequestPpapiBrokerPermission)
876#endif
nicka0ac8382016-12-15 23:59:23877 IPC_MESSAGE_UNHANDLED(handled = false)
878 IPC_END_MESSAGE_MAP()
879
880 return handled;
881}
882
883bool WebContentsImpl::OnMessageReceived(RenderFrameHostImpl* render_frame_host,
884 const IPC::Message& message) {
Lukasz Anforowicz02923102017-10-09 18:11:37885 {
886 WebUIImpl* web_ui = render_frame_host->web_ui();
887 if (web_ui && web_ui->OnMessageReceived(message, render_frame_host))
888 return true;
889 }
890
nicka0ac8382016-12-15 23:59:23891 for (auto& observer : observers_) {
892 if (observer.OnMessageReceived(message, render_frame_host))
893 return true;
894 }
895
896 bool handled = true;
897 IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(WebContentsImpl, message, render_frame_host)
[email protected]cfa856d62014-02-22 07:58:40898 IPC_MESSAGE_HANDLER(FrameHostMsg_DomOperationResponse,
899 OnDomOperationResponse)
[email protected]37b64c52014-07-11 21:14:05900 IPC_MESSAGE_HANDLER(FrameHostMsg_DidChangeThemeColor,
901 OnThemeColorChanged)
[email protected]8ed16472014-04-11 19:02:48902 IPC_MESSAGE_HANDLER(FrameHostMsg_DidFinishDocumentLoad,
903 OnDocumentLoadedInFrame)
904 IPC_MESSAGE_HANDLER(FrameHostMsg_DidFinishLoad, OnDidFinishLoad)
avi7830c852015-08-31 23:46:25905 IPC_MESSAGE_HANDLER(FrameHostMsg_DidLoadResourceFromMemoryCache,
[email protected]724159a2010-12-30 01:11:18906 OnDidLoadResourceFromMemoryCache)
avi3a3332622015-09-01 01:18:54907 IPC_MESSAGE_HANDLER(FrameHostMsg_DidDisplayInsecureContent,
[email protected]724159a2010-12-30 01:11:18908 OnDidDisplayInsecureContent)
elawrenceb2ac2a232017-03-27 21:46:25909 IPC_MESSAGE_HANDLER(FrameHostMsg_DidContainInsecureFormAction,
910 OnDidContainInsecureFormAction)
avi3a3332622015-09-01 01:18:54911 IPC_MESSAGE_HANDLER(FrameHostMsg_DidRunInsecureContent,
[email protected]724159a2010-12-30 01:11:18912 OnDidRunInsecureContent)
estark910b4572015-12-09 20:55:41913 IPC_MESSAGE_HANDLER(FrameHostMsg_DidDisplayContentWithCertificateErrors,
914 OnDidDisplayContentWithCertificateErrors)
915 IPC_MESSAGE_HANDLER(FrameHostMsg_DidRunContentWithCertificateErrors,
916 OnDidRunContentWithCertificateErrors)
Dave Tapuska549baab2019-03-19 15:52:40917 IPC_MESSAGE_HANDLER(FrameHostMsg_GoToEntryAtOffset, OnGoToEntryAtOffset)
avia23e16b2015-07-09 14:18:48918 IPC_MESSAGE_HANDLER(FrameHostMsg_RegisterProtocolHandler,
919 OnRegisterProtocolHandler)
920 IPC_MESSAGE_HANDLER(FrameHostMsg_UnregisterProtocolHandler,
921 OnUnregisterProtocolHandler)
kouhei40f03cb2015-09-24 07:47:01922 IPC_MESSAGE_HANDLER(FrameHostMsg_UpdatePageImportanceSignals,
923 OnUpdatePageImportanceSignals)
lukasza178ff092017-06-07 23:39:50924 IPC_MESSAGE_HANDLER(FrameHostMsg_UpdateFaviconURL, OnUpdateFaviconURL)
brettw4b461082016-11-19 18:55:16925#if BUILDFLAG(ENABLE_PLUGINS)
emaxxe70f5e12015-05-29 11:26:00926 IPC_MESSAGE_HANDLER(FrameHostMsg_PepperInstanceCreated,
927 OnPepperInstanceCreated)
928 IPC_MESSAGE_HANDLER(FrameHostMsg_PepperInstanceDeleted,
929 OnPepperInstanceDeleted)
tommyclie6633ca72014-10-31 00:40:42930 IPC_MESSAGE_HANDLER(FrameHostMsg_PepperPluginHung, OnPepperPluginHung)
zqzhang181047e62016-07-01 13:37:17931 IPC_MESSAGE_HANDLER(FrameHostMsg_PepperStartsPlayback,
932 OnPepperStartsPlayback)
933 IPC_MESSAGE_HANDLER(FrameHostMsg_PepperStopsPlayback,
934 OnPepperStopsPlayback)
tommyclie6633ca72014-10-31 00:40:42935 IPC_MESSAGE_HANDLER(FrameHostMsg_PluginCrashed, OnPluginCrashed)
[email protected]c4538072013-03-18 02:17:55936 IPC_MESSAGE_HANDLER_GENERIC(BrowserPluginHostMsg_Attach,
fsamuel833ee7c2015-02-13 23:40:40937 OnBrowserPluginMessage(render_frame_host,
938 message))
thestigc4cac8f2014-09-04 21:17:50939#endif
[email protected]724159a2010-12-30 01:11:18940 IPC_MESSAGE_UNHANDLED(handled = false)
[email protected]e44d1342014-05-16 21:29:33941 IPC_END_MESSAGE_MAP()
[email protected]724159a2010-12-30 01:11:18942
[email protected]724159a2010-12-30 01:11:18943 return handled;
944}
945
[email protected]d1198fd2012-08-13 22:50:19946NavigationControllerImpl& WebContentsImpl::GetController() {
[email protected]f5fa20e2011-12-21 22:35:56947 return controller_;
948}
949
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:41950BrowserContext* WebContentsImpl::GetBrowserContext() {
[email protected]a26023822011-12-29 00:23:55951 return controller_.GetBrowserContext();
[email protected]627e0512011-12-21 22:55:30952}
953
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:41954const GURL& WebContentsImpl::GetURL() {
Peter Boström101d7672018-12-15 00:43:59955 return GetVisibleURL();
[email protected]be1f56ab2011-12-22 06:55:31956}
957
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:41958const GURL& WebContentsImpl::GetVisibleURL() {
[email protected]c854a7e2013-05-21 16:42:24959 // We may not have a navigation entry yet.
[email protected]59167c22013-06-03 18:07:32960 NavigationEntry* entry = controller_.GetVisibleEntry();
[email protected]c854a7e2013-05-21 16:42:24961 return entry ? entry->GetVirtualURL() : GURL::EmptyGURL();
962}
963
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:41964const GURL& WebContentsImpl::GetLastCommittedURL() {
[email protected]c854a7e2013-05-21 16:42:24965 // We may not have a navigation entry yet.
966 NavigationEntry* entry = controller_.GetLastCommittedEntry();
967 return entry ? entry->GetVirtualURL() : GURL::EmptyGURL();
968}
969
[email protected]8ff00d72012-10-23 19:12:21970WebContentsDelegate* WebContentsImpl::GetDelegate() {
[email protected]be1f56ab2011-12-22 06:55:31971 return delegate_;
972}
973
[email protected]8ff00d72012-10-23 19:12:21974void WebContentsImpl::SetDelegate(WebContentsDelegate* delegate) {
[email protected]be1f56ab2011-12-22 06:55:31975 // TODO(cbentzel): remove this debugging code?
976 if (delegate == delegate_)
977 return;
978 if (delegate_)
979 delegate_->Detach(this);
980 delegate_ = delegate;
[email protected]a6b73c62013-02-11 23:05:08981 if (delegate_) {
[email protected]be1f56ab2011-12-22 06:55:31982 delegate_->Attach(this);
[email protected]a6b73c62013-02-11 23:05:08983 // Ensure the visible RVH reflects the new delegate's preferences.
[email protected]86f98a22013-03-20 14:35:00984 if (view_)
[email protected]e85165c642014-06-10 14:34:31985 view_->SetOverscrollControllerEnabled(CanOverscrollContent());
Joel Einbinder848001c2017-09-19 21:39:31986 if (GetRenderViewHost())
987 RenderFrameDevToolsAgentHost::WebContentsCreated(this);
[email protected]a6b73c62013-02-11 23:05:08988 }
[email protected]be1f56ab2011-12-22 06:55:31989}
990
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:41991RenderFrameHostImpl* WebContentsImpl::GetMainFrame() {
[email protected]94d0cc12013-12-18 00:07:41992 return frame_tree_.root()->current_frame_host();
[email protected]60eca4eb2013-12-06 00:02:16993}
994
nick53d5cbf2015-04-23 22:50:14995RenderFrameHostImpl* WebContentsImpl::GetFocusedFrame() {
996 FrameTreeNode* focused_node = frame_tree_.GetFocusedFrame();
997 if (!focused_node)
998 return nullptr;
999 return focused_node->current_frame_host();
[email protected]9c9343b2014-03-08 02:56:071000}
1001
rob3e2a0732016-01-06 21:22:091002RenderFrameHostImpl* WebContentsImpl::FindFrameByFrameTreeNodeId(
creisf71a2632017-05-04 19:03:501003 int frame_tree_node_id,
1004 int process_id) {
1005 FrameTreeNode* frame = frame_tree_.FindByID(frame_tree_node_id);
1006
1007 // Sanity check that this is in the caller's expected process. Otherwise a
1008 // recent cross-process navigation may have led to a privilege change that the
1009 // caller is not expecting.
1010 if (!frame ||
1011 frame->current_frame_host()->GetProcess()->GetID() != process_id)
1012 return nullptr;
1013
1014 return frame->current_frame_host();
1015}
1016
1017RenderFrameHostImpl* WebContentsImpl::UnsafeFindFrameByFrameTreeNodeId(
rob3e2a0732016-01-06 21:22:091018 int frame_tree_node_id) {
creisf71a2632017-05-04 19:03:501019 // Beware using this! The RenderFrameHost may have changed since the caller
1020 // obtained frame_tree_node_id.
rob3e2a0732016-01-06 21:22:091021 FrameTreeNode* frame = frame_tree_.FindByID(frame_tree_node_id);
1022 return frame ? frame->current_frame_host() : nullptr;
1023}
1024
[email protected]a86c0e962013-12-17 17:10:391025void WebContentsImpl::ForEachFrame(
Lei Zhangebcc6302018-01-12 19:46:451026 const base::RepeatingCallback<void(RenderFrameHost*)>& on_frame) {
dcheng57e39e22016-01-21 00:25:381027 for (FrameTreeNode* node : frame_tree_.Nodes()) {
1028 on_frame.Run(node->current_frame_host());
1029 }
[email protected]a86c0e962013-12-17 17:10:391030}
1031
dchengafb53e22016-02-04 08:11:081032std::vector<RenderFrameHost*> WebContentsImpl::GetAllFrames() {
1033 std::vector<RenderFrameHost*> frame_hosts;
1034 for (FrameTreeNode* node : frame_tree_.Nodes())
1035 frame_hosts.push_back(node->current_frame_host());
1036 return frame_hosts;
1037}
1038
lukaszacbdf52e2016-01-15 21:19:511039int WebContentsImpl::SendToAllFrames(IPC::Message* message) {
1040 int number_of_messages = 0;
dchengafb53e22016-02-04 08:11:081041 for (RenderFrameHost* rfh : GetAllFrames()) {
lukasza1a9ab2422016-03-02 18:47:251042 if (!rfh->IsRenderFrameLive())
1043 continue;
1044
dchengafb53e22016-02-04 08:11:081045 ++number_of_messages;
1046 IPC::Message* message_copy = new IPC::Message(*message);
1047 message_copy->set_routing_id(rfh->GetRoutingID());
1048 rfh->Send(message_copy);
1049 }
[email protected]a86c0e962013-12-17 17:10:391050 delete message;
lukaszacbdf52e2016-01-15 21:19:511051 return number_of_messages;
[email protected]a86c0e962013-12-17 17:10:391052}
1053
lfgdb5c4ed2016-03-04 23:09:071054void WebContentsImpl::SendPageMessage(IPC::Message* msg) {
alexmos136fd6e62016-08-15 20:58:411055 frame_tree_.root()->render_manager()->SendPageMessage(msg, nullptr);
lfgdb5c4ed2016-03-04 23:09:071056}
1057
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411058RenderViewHostImpl* WebContentsImpl::GetRenderViewHost() {
[email protected]fa944cb82013-11-15 17:51:211059 return GetRenderManager()->current_host();
[email protected]be1f56ab2011-12-22 06:55:311060}
1061
creis89a0f782015-05-27 16:13:171062void WebContentsImpl::CancelActiveAndPendingDialogs() {
avi5d3b8692016-10-12 22:00:461063 if (dialog_manager_) {
avi6879fcf2017-01-21 05:27:531064 dialog_manager_->CancelDialogs(this, /*reset_state=*/false);
avi5d3b8692016-10-12 22:00:461065 }
creis89a0f782015-05-27 16:13:171066 if (browser_plugin_embedder_)
1067 browser_plugin_embedder_->CancelGuestDialogs();
1068}
1069
naskoc0fceff2015-04-30 15:53:521070void WebContentsImpl::ClosePage() {
1071 GetRenderViewHost()->ClosePage();
1072}
1073
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411074RenderWidgetHostView* WebContentsImpl::GetRenderWidgetHostView() {
[email protected]fa944cb82013-11-15 17:51:211075 return GetRenderManager()->GetRenderWidgetHostView();
[email protected]be1f56ab2011-12-22 06:55:311076}
1077
lfg265a2672016-04-23 03:11:021078RenderWidgetHostView* WebContentsImpl::GetTopLevelRenderWidgetHostView() {
1079 if (GetOuterWebContents())
1080 return GetOuterWebContents()->GetTopLevelRenderWidgetHostView();
1081 return GetRenderManager()->GetRenderWidgetHostView();
1082}
1083
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411084RenderWidgetHostView* WebContentsImpl::GetFullscreenRenderWidgetHostView() {
vmpstr6d9996c82017-02-23 00:43:251085 if (auto* widget_host = GetFullscreenRenderWidgetHost())
ekaramadaeb3c5c2016-11-09 03:22:351086 return widget_host->GetView();
1087 return nullptr;
[email protected]4aebbca2013-09-17 22:26:491088}
1089
[email protected]8ff00d72012-10-23 19:12:211090WebContentsView* WebContentsImpl::GetView() const {
[email protected]be1f56ab2011-12-22 06:55:311091 return view_.get();
1092}
1093
leon.han552e9de2017-02-09 14:37:301094void WebContentsImpl::OnScreenOrientationChange() {
1095 DCHECK(screen_orientation_provider_);
Becca Hughese9e27952018-06-25 17:08:271096 screen_orientation_provider_->OnOrientationChange();
leon.hane4db177a2017-02-07 14:19:161097}
1098
Alan Cutter92d59252019-03-29 02:32:331099base::Optional<SkColor> WebContentsImpl::GetThemeColor() {
yusufod41c5f92015-03-06 00:14:281100 return theme_color_;
1101}
1102
Doug Turner63f3c7b2017-07-29 05:10:011103void WebContentsImpl::SetAccessibilityMode(ui::AXMode mode) {
[email protected]95640212014-07-26 18:14:301104 if (mode == accessibility_mode_)
1105 return;
1106
dmazzonif8a31042016-12-03 00:52:131107 // Don't allow accessibility to be enabled for WebContents that are never
1108 // visible, like background pages.
1109 if (IsNeverVisible())
1110 return;
1111
[email protected]95640212014-07-26 18:14:301112 accessibility_mode_ = mode;
dcheng57e39e22016-01-21 00:25:381113
1114 for (FrameTreeNode* node : frame_tree_.Nodes()) {
dmazzonif8a31042016-12-03 00:52:131115 UpdateAccessibilityModeOnFrame(node->current_frame_host());
clamy610c63b32017-12-22 15:05:181116 // Also update accessibility mode on the speculative RenderFrameHost for
1117 // this FrameTreeNode, if one exists.
Alex Moshchuk3d8a1f392017-10-24 19:01:261118 RenderFrameHost* speculative_frame_host =
1119 node->render_manager()->speculative_frame_host();
1120 if (speculative_frame_host)
1121 UpdateAccessibilityModeOnFrame(speculative_frame_host);
dcheng57e39e22016-01-21 00:25:381122 }
[email protected]95640212014-07-26 18:14:301123}
1124
Doug Turner63f3c7b2017-07-29 05:10:011125void WebContentsImpl::AddAccessibilityMode(ui::AXMode mode) {
1126 ui::AXMode new_mode(accessibility_mode_);
dougtcd3dad732017-03-14 03:26:231127 new_mode |= mode;
1128 SetAccessibilityMode(new_mode);
[email protected]95640212014-07-26 18:14:301129}
1130
Avi Drissman1e620f32018-03-16 13:57:291131// Helper class used by WebContentsImpl::RequestAXTreeSnapshot.
1132// Handles the callbacks from parallel snapshot requests to each frame,
1133// and feeds the results to an AXTreeCombiner, which converts them into a
1134// single combined accessibility tree.
1135class WebContentsImpl::AXTreeSnapshotCombiner
1136 : public base::RefCounted<AXTreeSnapshotCombiner> {
1137 public:
1138 explicit AXTreeSnapshotCombiner(AXTreeSnapshotCallback callback)
1139 : callback_(std::move(callback)) {}
1140
1141 AXTreeSnapshotCallback AddFrame(bool is_root) {
1142 // Adds a reference to |this|.
1143 return base::BindOnce(&AXTreeSnapshotCombiner::ReceiveSnapshot, this,
1144 is_root);
1145 }
1146
1147 void ReceiveSnapshot(bool is_root, const ui::AXTreeUpdate& snapshot) {
1148 combiner_.AddTree(snapshot, is_root);
1149 }
1150
1151 private:
1152 friend class base::RefCounted<AXTreeSnapshotCombiner>;
1153
1154 // This is called automatically after the last call to ReceiveSnapshot
1155 // when there are no more references to this object.
1156 ~AXTreeSnapshotCombiner() {
1157 combiner_.Combine();
1158 std::move(callback_).Run(combiner_.combined());
1159 }
1160
1161 ui::AXTreeCombiner combiner_;
1162 AXTreeSnapshotCallback callback_;
1163};
1164
1165void WebContentsImpl::RequestAXTreeSnapshot(AXTreeSnapshotCallback callback,
1166 ui::AXMode ax_mode) {
dmazzonid95ae842016-04-12 21:17:391167 // Send a request to each of the frames in parallel. Each one will return
1168 // an accessibility tree snapshot, and AXTreeSnapshotCombiner will combine
1169 // them into a single tree and call |callback| with that result, then
1170 // delete |combiner|.
Avi Drissman1e620f32018-03-16 13:57:291171 FrameTreeNode* root_node = frame_tree_.root();
tzik58faa742018-08-01 10:54:181172 auto combiner =
1173 base::MakeRefCounted<AXTreeSnapshotCombiner>(std::move(callback));
Avi Drissman1e620f32018-03-16 13:57:291174
tzik58faa742018-08-01 10:54:181175 RecursiveRequestAXTreeSnapshotOnFrame(root_node, combiner.get(), ax_mode);
Avi Drissman1e620f32018-03-16 13:57:291176}
1177
1178void WebContentsImpl::RecursiveRequestAXTreeSnapshotOnFrame(
1179 FrameTreeNode* root_node,
1180 AXTreeSnapshotCombiner* combiner,
1181 ui::AXMode ax_mode) {
dmazzonid95ae842016-04-12 21:17:391182 for (FrameTreeNode* frame_tree_node : frame_tree_.Nodes()) {
Avi Drissman1e620f32018-03-16 13:57:291183 WebContentsImpl* inner_contents =
1184 node_.GetInnerWebContentsInFrame(frame_tree_node);
1185 if (inner_contents) {
1186 inner_contents->RecursiveRequestAXTreeSnapshotOnFrame(root_node, combiner,
1187 ax_mode);
1188 } else {
1189 bool is_root = frame_tree_node == root_node;
1190 frame_tree_node->current_frame_host()->RequestAXTreeSnapshot(
1191 combiner->AddFrame(is_root), ax_mode);
1192 }
dmazzonid95ae842016-04-12 21:17:391193 }
dmazzoni83ba5c82015-04-14 07:11:511194}
1195
Becca Hughes3b5a43482018-07-17 22:31:551196void WebContentsImpl::NotifyViewportFitChanged(
1197 blink::mojom::ViewportFit value) {
1198 for (auto& observer : observers_)
1199 observer.ViewportFitChanged(value);
1200}
1201
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411202FindRequestManager* WebContentsImpl::GetFindRequestManagerForTesting() {
Ehsan Karamad6beb2ea2018-11-25 18:15:131203 return GetFindRequestManager();
1204}
1205
mcnee336ea2c2017-05-23 22:50:591206#if !defined(OS_ANDROID)
akabac6bd1212018-06-25 20:10:481207void WebContentsImpl::UpdateZoom() {
1208 RenderWidgetHostImpl* rwh = GetRenderViewHost()->GetWidget();
1209 if (rwh->GetView())
1210 rwh->SynchronizeVisualProperties();
wjmaclean64951902016-04-29 20:59:121211}
1212
wjmaclean64951902016-04-29 20:59:121213
1214void WebContentsImpl::UpdateZoomIfNecessary(const std::string& scheme,
akabac6bd1212018-06-25 20:10:481215 const std::string& host) {
wjmaclean64951902016-04-29 20:59:121216 NavigationEntry* entry = GetController().GetLastCommittedEntry();
1217 if (!entry)
1218 return;
1219
1220 GURL url = HostZoomMap::GetURLFromEntry(entry);
1221 if (host != net::GetHostOrSpecFromURL(url) ||
1222 (!scheme.empty() && !url.SchemeIs(scheme))) {
1223 return;
1224 }
1225
akabac6bd1212018-06-25 20:10:481226 UpdateZoom();
wjmaclean64951902016-04-29 20:59:121227}
mcnee336ea2c2017-05-23 22:50:591228#endif // !defined(OS_ANDROID)
wjmaclean64951902016-04-29 20:59:121229
rockotf62002a2016-09-15 00:08:591230base::Closure WebContentsImpl::AddBindingSet(
1231 const std::string& interface_name,
1232 WebContentsBindingSet* binding_set) {
1233 auto result =
1234 binding_sets_.insert(std::make_pair(interface_name, binding_set));
1235 DCHECK(result.second);
1236 return base::Bind(&WebContentsImpl::RemoveBindingSet,
1237 weak_factory_.GetWeakPtr(), interface_name);
1238}
1239
rockot8bbad222017-03-22 04:34:051240WebContentsBindingSet* WebContentsImpl::GetBindingSet(
1241 const std::string& interface_name) {
1242 auto it = binding_sets_.find(interface_name);
1243 if (it == binding_sets_.end())
1244 return nullptr;
1245 return it->second;
1246}
1247
paulmeyerfeafc2d2017-04-25 21:46:401248std::vector<WebContentsImpl*> WebContentsImpl::GetWebContentsAndAllInner() {
1249 std::vector<WebContentsImpl*> all_contents(1, this);
1250
1251 for (size_t i = 0; i != all_contents.size(); ++i) {
1252 for (auto* inner_contents : all_contents[i]->GetInnerWebContents()) {
Lucas Furukawa Gadani2ec00c82018-12-14 15:53:161253 all_contents.push_back(static_cast<WebContentsImpl*>(inner_contents));
paulmeyerfeafc2d2017-04-25 21:46:401254 }
1255 }
1256
1257 return all_contents;
1258}
1259
Sam McNally145fb172017-05-10 00:13:231260void WebContentsImpl::NotifyManifestUrlChanged(
1261 const base::Optional<GURL>& manifest_url) {
1262 for (auto& observer : observers_)
1263 observer.DidUpdateWebManifestURL(manifest_url);
1264}
1265
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411266WebUI* WebContentsImpl::GetWebUI() {
carlosk35f35af2015-12-01 10:55:401267 WebUI* commited_web_ui = GetCommittedWebUI();
1268 return commited_web_ui ? commited_web_ui
1269 : GetRenderManager()->GetNavigatingWebUI();
[email protected]be1f56ab2011-12-22 06:55:311270}
1271
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411272WebUI* WebContentsImpl::GetCommittedWebUI() {
carlosk35f35af2015-12-01 10:55:401273 return frame_tree_.root()->current_frame_host()->web_ui();
[email protected]d5f942ba2008-09-26 19:30:341274}
1275
Changwan Ryuc1134a82018-03-07 02:10:141276void WebContentsImpl::SetUserAgentOverride(const std::string& override,
1277 bool override_in_new_tabs) {
[email protected]bf70edce2012-06-20 22:32:221278 if (GetUserAgentOverride() == override)
1279 return;
1280
Changwan Ryuc1134a82018-03-07 02:10:141281 should_override_user_agent_in_new_tabs_ = override_in_new_tabs;
1282
[email protected]bf70edce2012-06-20 22:32:221283 renderer_preferences_.user_agent_override = override;
1284
1285 // Send the new override string to the renderer.
1286 RenderViewHost* host = GetRenderViewHost();
1287 if (host)
1288 host->SyncRendererPrefs();
1289
1290 // Reload the page if a load is currently in progress to avoid having
1291 // different parts of the page loaded using different user agents.
[email protected]6286a372013-10-09 04:03:271292 NavigationEntry* entry = controller_.GetVisibleEntry();
Ivan Kotenkov2c0d2bb32017-11-01 15:41:281293 if (IsLoading() && entry != nullptr && entry->GetIsOverridingUserAgent())
toyoshim6142d96f2016-12-19 09:07:251294 controller_.Reload(ReloadType::BYPASSING_CACHE, true);
[email protected]8d0f3312012-08-18 01:47:531295
ericwilligersde386342016-10-19 02:35:091296 for (auto& observer : observers_)
1297 observer.UserAgentOverrideSet(override);
[email protected]86ef6a392012-05-11 22:03:111298}
1299
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411300const std::string& WebContentsImpl::GetUserAgentOverride() {
[email protected]bf70edce2012-06-20 22:32:221301 return renderer_preferences_.user_agent_override;
[email protected]86ef6a392012-05-11 22:03:111302}
1303
Changwan Ryuc1134a82018-03-07 02:10:141304bool WebContentsImpl::ShouldOverrideUserAgentInNewTabs() {
1305 return should_override_user_agent_in_new_tabs_;
1306}
1307
dmazzonidd3d51a72016-12-14 18:41:011308void WebContentsImpl::EnableWebContentsOnlyAccessibilityMode() {
Dominic Mazzonid9fda172019-03-07 17:12:071309 // If accessibility is already enabled, we'll need to force a reset
1310 // in order to ensure new observers of accessibility events get the
1311 // full accessibility tree from scratch.
1312 bool need_reset = GetAccessibilityMode().has_mode(ui::AXMode::kWebContents);
1313
1314 ui::AXMode desired_mode =
1315 GetContentClient()->browser()->GetAXModeForBrowserContext(
1316 GetBrowserContext());
1317 desired_mode |= ui::kAXModeWebContentsOnly;
1318 AddAccessibilityMode(desired_mode);
1319
1320 if (need_reset) {
dchengafb53e22016-02-04 08:11:081321 for (RenderFrameHost* rfh : GetAllFrames())
1322 ResetAccessibility(rfh);
dchengafb53e22016-02-04 08:11:081323 }
[email protected]95640212014-07-26 18:14:301324}
1325
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411326bool WebContentsImpl::IsWebContentsOnlyAccessibilityModeForTesting() {
Doug Turner63f3c7b2017-07-29 05:10:011327 return accessibility_mode_ == ui::kAXModeWebContentsOnly;
[email protected]95640212014-07-26 18:14:301328}
1329
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411330bool WebContentsImpl::IsFullAccessibilityModeForTesting() {
Doug Turner63f3c7b2017-07-29 05:10:011331 return accessibility_mode_ == ui::kAXModeComplete;
[email protected]95640212014-07-26 18:14:301332}
1333
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411334const PageImportanceSignals& WebContentsImpl::GetPageImportanceSignals() {
kouhei40f03cb2015-09-24 07:47:011335 return page_importance_signals_;
1336}
1337
Guido Urdanetaf4008002018-05-03 10:43:041338const std::string& WebContentsImpl::GetMediaDeviceGroupIDSaltBase() const {
1339 return media_device_group_id_salt_base_;
1340}
1341
Becca Hughes6daf5662018-06-27 16:50:541342#if defined(OS_ANDROID)
1343
1344void WebContentsImpl::SetDisplayCutoutSafeArea(gfx::Insets insets) {
Becca Hughes86334622018-07-09 21:29:381345 if (display_cutout_host_impl_)
1346 display_cutout_host_impl_->SetDisplayCutoutSafeArea(insets);
Becca Hughes6daf5662018-06-27 16:50:541347}
1348
1349#endif
1350
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411351const base::string16& WebContentsImpl::GetTitle() {
[email protected]4c6092c5b2009-06-06 00:23:551352 // Transient entries take precedence. They are used for interstitial pages
1353 // that are shown on top of existing pages.
[email protected]10f417c52011-12-28 21:04:231354 NavigationEntry* entry = controller_.GetTransientEntry();
[email protected]45d0ef7f2011-01-05 13:46:231355 if (entry) {
jshin1fb76462016-04-05 22:13:031356 return entry->GetTitleForDisplay();
[email protected]45d0ef7f2011-01-05 13:46:231357 }
carlosk35f35af2015-12-01 10:55:401358
1359 WebUI* navigating_web_ui = GetRenderManager()->GetNavigatingWebUI();
1360 WebUI* our_web_ui = navigating_web_ui
1361 ? navigating_web_ui
1362 : GetRenderManager()->current_frame_host()->web_ui();
1363
[email protected]7ade2732011-02-10 00:13:581364 if (our_web_ui) {
[email protected]96d185d2009-04-24 03:28:541365 // Don't override the title in view source mode.
[email protected]59167c22013-06-03 18:07:321366 entry = controller_.GetVisibleEntry();
[email protected]96d185d2009-04-24 03:28:541367 if (!(entry && entry->IsViewSourceMode())) {
[email protected]e0112912011-02-02 22:54:351368 // Give the Web UI the chance to override our title.
[email protected]fcf75d42013-12-03 20:11:261369 const base::string16& title = our_web_ui->GetOverriddenTitle();
[email protected]96d185d2009-04-24 03:28:541370 if (!title.empty())
1371 return title;
1372 }
1373 }
1374
1375 // We use the title for the last committed entry rather than a pending
1376 // navigation entry. For example, when the user types in a URL, we want to
1377 // keep the old page's title until the new load has committed and we get a new
1378 // title.
[email protected]96d185d2009-04-24 03:28:541379 entry = controller_.GetLastCommittedEntry();
[email protected]59167c22013-06-03 18:07:321380
creis072b61f2015-09-28 20:52:241381 // We make an exception for initial navigations. We only want to use the title
1382 // from the visible entry if:
1383 // 1. The pending entry has been explicitly assigned a title to display.
1384 // 2. The user is doing a history navigation in a new tab (e.g., Ctrl+Back),
1385 // which case there is a pending entry index other than -1.
1386 //
1387 // Otherwise, we want to stick with the last committed entry's title during
1388 // new navigations, which have pending entries at index -1 with no title.
1389 if (controller_.IsInitialNavigation() &&
1390 ((controller_.GetVisibleEntry() &&
1391 !controller_.GetVisibleEntry()->GetTitle().empty()) ||
1392 controller_.GetPendingEntryIndex() != -1)) {
1393 entry = controller_.GetVisibleEntry();
[email protected]9eeafc012013-11-25 23:49:471394 }
[email protected]59167c22013-06-03 18:07:321395
[email protected]45d0ef7f2011-01-05 13:46:231396 if (entry) {
jshin1fb76462016-04-05 22:13:031397 return entry->GetTitleForDisplay();
[email protected]45d0ef7f2011-01-05 13:46:231398 }
[email protected]987fc3a2011-05-26 14:18:091399
1400 // |page_title_when_no_navigation_entry_| is finally used
1401 // if no title cannot be retrieved.
1402 return page_title_when_no_navigation_entry_;
[email protected]96d185d2009-04-24 03:28:541403}
1404
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411405SiteInstanceImpl* WebContentsImpl::GetSiteInstance() {
[email protected]fa944cb82013-11-15 17:51:211406 return GetRenderManager()->current_host()->GetSiteInstance();
[email protected]96d185d2009-04-24 03:28:541407}
1408
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411409bool WebContentsImpl::IsLoading() {
clamy44e84ce2016-02-22 15:38:251410 return frame_tree_.IsLoading() &&
creisadca9862017-06-16 19:12:111411 !(ShowingInterstitialPage() && interstitial_page_->pause_throbber());
[email protected]3c9e1872010-11-18 16:17:491412}
1413
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411414double WebContentsImpl::GetLoadProgress() {
Peter Boström18a40fa2018-10-31 19:03:501415 return frame_tree_.load_progress();
1416}
1417
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411418bool WebContentsImpl::IsLoadingToDifferentDocument() {
clamy44e84ce2016-02-22 15:38:251419 return IsLoading() && is_load_to_different_document_;
[email protected]6dfed692014-05-22 04:18:031420}
1421
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411422bool WebContentsImpl::IsWaitingForResponse() {
Peter Boströma9a27f6e2018-11-21 13:12:191423 NavigationRequest* ongoing_navigation_request =
1424 frame_tree_.root()->navigation_request();
1425
1426 // An ongoing navigation request means we're waiting for a response.
1427 return ongoing_navigation_request != nullptr;
[email protected]be1f56ab2011-12-22 06:55:311428}
1429
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411430const net::LoadStateWithParam& WebContentsImpl::GetLoadState() {
[email protected]be1f56ab2011-12-22 06:55:311431 return load_state_;
1432}
1433
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411434const base::string16& WebContentsImpl::GetLoadStateHost() {
[email protected]be1f56ab2011-12-22 06:55:311435 return load_state_host_;
1436}
1437
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411438uint64_t WebContentsImpl::GetUploadSize() {
[email protected]be1f56ab2011-12-22 06:55:311439 return upload_size_;
1440}
1441
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411442uint64_t WebContentsImpl::GetUploadPosition() {
[email protected]be1f56ab2011-12-22 06:55:311443 return upload_position_;
1444}
1445
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411446const std::string& WebContentsImpl::GetEncoding() {
[email protected]ef3adfc2014-05-11 00:04:541447 return canonical_encoding_;
[email protected]be1f56ab2011-12-22 06:55:311448}
1449
Charles Zhaod72f99d2018-09-17 14:18:271450bool WebContentsImpl::WasDiscarded() {
1451 return GetFrameTree()->root()->was_discarded();
1452}
1453
Shubhie Panickerddf2a4e2018-03-06 00:09:061454void WebContentsImpl::SetWasDiscarded(bool was_discarded) {
1455 GetFrameTree()->root()->set_was_discarded();
1456}
1457
[email protected]222f5822014-02-05 23:40:491458void WebContentsImpl::IncrementCapturerCount(const gfx::Size& capture_size) {
[email protected]5a652232013-02-12 06:15:251459 DCHECK(!is_being_destroyed_);
Francois Doraye6161152018-03-27 22:05:371460 const bool was_captured = IsBeingCaptured();
[email protected]54597982013-02-06 01:59:551461 ++capturer_count_;
1462 DVLOG(1) << "There are now " << capturer_count_
1463 << " capturing(s) of WebContentsImpl@" << this;
[email protected]222f5822014-02-05 23:40:491464
1465 // Note: This provides a hint to upstream code to size the views optimally
1466 // for quality (e.g., to avoid scaling).
1467 if (!capture_size.IsEmpty() && preferred_size_for_capture_.IsEmpty()) {
1468 preferred_size_for_capture_ = capture_size;
1469 OnPreferredSizeChanged(preferred_size_);
1470 }
ccameron8807c38f2015-01-17 00:29:191471
Francois Doraye6161152018-03-27 22:05:371472 if (GetVisibility() != Visibility::VISIBLE && !was_captured) {
1473 // Ensure that all views act as if they were visible before capture begins.
1474 // TODO(fdoray): Replace RenderWidgetHostView::WasUnOccluded() with a method
1475 // to explicitly notify the RenderWidgetHostView that capture began.
1476 // https://crbug.com/668690
Ehsan Karamada03e1582018-12-05 23:07:561477 if (auto* main_view = GetRenderWidgetHostView())
1478 main_view->WasUnOccluded();
1479 if (!ShowingInterstitialPage())
1480 SetVisibilityForChildViews(true);
Francois Doraye6161152018-03-27 22:05:371481 }
[email protected]54597982013-02-06 01:59:551482}
1483
1484void WebContentsImpl::DecrementCapturerCount() {
1485 --capturer_count_;
1486 DVLOG(1) << "There are now " << capturer_count_
1487 << " capturing(s) of WebContentsImpl@" << this;
1488 DCHECK_LE(0, capturer_count_);
1489
[email protected]5a652232013-02-12 06:15:251490 if (is_being_destroyed_)
1491 return;
1492
Francois Doray24fc62c2017-12-11 17:27:331493 if (!IsBeingCaptured()) {
[email protected]222f5822014-02-05 23:40:491494 const gfx::Size old_size = preferred_size_for_capture_;
1495 preferred_size_for_capture_ = gfx::Size();
1496 OnPreferredSizeChanged(old_size);
[email protected]222f5822014-02-05 23:40:491497
Francois Doraye6161152018-03-27 22:05:371498 if (visibility_ == Visibility::HIDDEN) {
Francois Doray18800972017-08-25 22:38:581499 DVLOG(1) << "Executing delayed WasHidden().";
1500 WasHidden();
Francois Doraye6161152018-03-27 22:05:371501 } else if (visibility_ == Visibility::OCCLUDED) {
Mitsuru Oshima04a80242018-03-03 03:06:151502 WasOccluded();
Francois Doraye6161152018-03-27 22:05:371503 }
[email protected]54597982013-02-06 01:59:551504 }
[email protected]be1f56ab2011-12-22 06:55:311505}
1506
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411507bool WebContentsImpl::IsBeingCaptured() {
Francois Doray24fc62c2017-12-11 17:27:331508 return capturer_count_ > 0;
[email protected]f2bd40812013-07-20 04:30:441509}
1510
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411511bool WebContentsImpl::IsAudioMuted() {
Max Morinb5506f72018-05-16 07:33:101512 if (base::FeatureList::IsEnabled(features::kAudioServiceAudioStreams)) {
1513 return audio_stream_factory_ && audio_stream_factory_->IsMuted();
1514 }
miu50f97892014-09-22 22:49:521515 return audio_muter_.get() && audio_muter_->is_muting();
1516}
1517
1518void WebContentsImpl::SetAudioMuted(bool mute) {
1519 DVLOG(1) << "SetAudioMuted(mute=" << mute << "), was " << IsAudioMuted()
1520 << " for WebContentsImpl@" << this;
1521
1522 if (mute == IsAudioMuted())
1523 return;
1524
Max Morinb5506f72018-05-16 07:33:101525 if (base::FeatureList::IsEnabled(features::kAudioServiceAudioStreams)) {
1526 GetAudioStreamFactory()->SetMuted(mute);
miu50f97892014-09-22 22:49:521527 } else {
Max Morinb5506f72018-05-16 07:33:101528 if (mute) {
1529 if (!audio_muter_)
1530 audio_muter_.reset(new WebContentsAudioMuter(this));
1531 audio_muter_->StartMuting();
1532 } else {
1533 DCHECK(audio_muter_);
1534 audio_muter_->StopMuting();
1535 }
miu50f97892014-09-22 22:49:521536 }
1537
ericwilligersde386342016-10-19 02:35:091538 for (auto& observer : observers_)
1539 observer.DidUpdateAudioMutingState(mute);
wjmaclean2f797c782016-01-07 14:52:031540
Jan Rucka442c83f2017-08-08 13:27:541541 // Notification for UI updates in response to the changed muting state.
1542 NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB);
1543}
1544
1545bool WebContentsImpl::IsCurrentlyAudible() {
Chris Hamiltondf0d72cd2018-05-29 16:23:531546 return is_currently_audible_;
miu50f97892014-09-22 22:49:521547}
1548
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411549bool WebContentsImpl::IsConnectedToBluetoothDevice() {
ortunodf4d7982016-04-08 02:33:351550 return bluetooth_connected_device_count_ > 0;
ortuno32e7db3c2016-03-29 16:14:201551}
1552
Lucas Furukawa Gadani4b4eed02019-06-04 23:12:041553bool WebContentsImpl::IsConnectedToSerialPort() {
Reilly Grant5e7c79b22019-04-09 17:26:201554 return serial_active_frame_count_ > 0;
1555}
1556
Marijn Kruisselbrinkf1714aa22019-07-02 03:45:331557bool WebContentsImpl::HasNativeFileSystemDirectoryHandles() {
1558 return native_file_system_directory_handle_count_ > 0;
1559}
1560
1561bool WebContentsImpl::HasWritableNativeFileSystemHandles() {
1562 return native_file_system_writable_handle_count_ > 0;
1563}
1564
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411565bool WebContentsImpl::HasPictureInPictureVideo() {
sawtellea7333a82018-05-31 02:36:361566 return has_picture_in_picture_video_;
1567}
1568
1569void WebContentsImpl::SetHasPictureInPictureVideo(
1570 bool has_picture_in_picture_video) {
1571 // If status of |this| is already accurate, there is no need to update.
1572 if (has_picture_in_picture_video == has_picture_in_picture_video_)
1573 return;
1574 has_picture_in_picture_video_ = has_picture_in_picture_video;
1575 NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB);
François Beaufort058085d2018-08-30 15:29:141576 for (auto& observer : observers_)
1577 observer.MediaPictureInPictureChanged(has_picture_in_picture_video_);
sawtellea7333a82018-05-31 02:36:361578}
1579
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411580bool WebContentsImpl::IsCrashed() {
Dominic Mazzoni73a48322018-05-25 18:14:071581 switch (crashed_status_) {
1582 case base::TERMINATION_STATUS_PROCESS_CRASHED:
1583 case base::TERMINATION_STATUS_ABNORMAL_TERMINATION:
1584 case base::TERMINATION_STATUS_PROCESS_WAS_KILLED:
1585 case base::TERMINATION_STATUS_OOM:
1586 case base::TERMINATION_STATUS_LAUNCH_FAILED:
oshima620225722015-06-04 19:45:271587#if defined(OS_CHROMEOS)
Dominic Mazzoni73a48322018-05-25 18:14:071588 case base::TERMINATION_STATUS_PROCESS_WAS_KILLED_BY_OOM:
oshima620225722015-06-04 19:45:271589#endif
Dominic Mazzoni73a48322018-05-25 18:14:071590#if defined(OS_ANDROID)
1591 case base::TERMINATION_STATUS_OOM_PROTECTED:
1592#endif
1593 return true;
1594 case base::TERMINATION_STATUS_NORMAL_TERMINATION:
1595 case base::TERMINATION_STATUS_STILL_RUNNING:
1596 return false;
1597 case base::TERMINATION_STATUS_MAX_ENUM:
1598 NOTREACHED();
1599 return false;
1600 }
1601
1602 NOTREACHED();
1603 return false;
[email protected]3c9e1872010-11-18 16:17:491604}
1605
[email protected]b172aee2012-04-10 17:05:261606void WebContentsImpl::SetIsCrashed(base::TerminationStatus status,
1607 int error_code) {
[email protected]443b80e2010-12-14 00:42:231608 if (status == crashed_status_)
[email protected]d5f942ba2008-09-26 19:30:341609 return;
1610
[email protected]443b80e2010-12-14 00:42:231611 crashed_status_ = status;
1612 crashed_error_code_ = error_code;
[email protected]8ff00d72012-10-23 19:12:211613 NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB);
[email protected]d5f942ba2008-09-26 19:30:341614}
1615
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411616base::TerminationStatus WebContentsImpl::GetCrashedStatus() {
[email protected]be1f56ab2011-12-22 06:55:311617 return crashed_status_;
1618}
1619
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411620int WebContentsImpl::GetCrashedErrorCode() {
afakhry98241832016-03-11 19:27:471621 return crashed_error_code_;
1622}
1623
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411624bool WebContentsImpl::IsBeingDestroyed() {
[email protected]be1f56ab2011-12-22 06:55:311625 return is_being_destroyed_;
1626}
1627
[email protected]7f924832014-08-09 05:57:221628void WebContentsImpl::NotifyNavigationStateChanged(
1629 InvalidateTypes changed_flags) {
dalecurtis88c240072015-12-09 02:11:181630 // Notify the media observer of potential audibility changes.
1631 if (changed_flags & INVALIDATE_TYPE_TAB) {
mlamouri44e4ef42016-01-20 18:42:271632 media_web_contents_observer_->MaybeUpdateAudibleState();
dalecurtisbc6572e12014-09-12 19:22:301633 }
1634
[email protected]d5f942ba2008-09-26 19:30:341635 if (delegate_)
1636 delegate_->NavigationStateChanged(this, changed_flags);
wjmaclean1dabf3e32016-05-13 23:34:441637
1638 if (GetOuterWebContents())
1639 GetOuterWebContents()->NotifyNavigationStateChanged(changed_flags);
[email protected]d5f942ba2008-09-26 19:30:341640}
1641
W. James MacLean7c8581072019-06-18 18:07:531642void WebContentsImpl::NotifyVisibleViewportSizeChanged(
1643 const gfx::Size& visible_viewport_size) {
1644 SendPageMessage(new PageMsg_UpdatePageVisualProperties(
1645 MSG_ROUTING_NONE, visible_viewport_size));
1646}
1647
Chris Hamiltondf0d72cd2018-05-29 16:23:531648void WebContentsImpl::OnAudioStateChanged() {
1649 // This notification can come from any embedded contents or from this
1650 // WebContents' stream monitor. Aggregate these signals to get the actual
1651 // state.
1652 bool is_currently_audible =
1653 audio_stream_monitor_.IsCurrentlyAudible() ||
1654 (browser_plugin_embedder_ &&
1655 browser_plugin_embedder_->AreAnyGuestsCurrentlyAudible());
1656 if (is_currently_audible == is_currently_audible_)
1657 return;
1658
1659 // Update internal state.
1660 is_currently_audible_ = is_currently_audible;
1661 was_ever_audible_ = was_ever_audible_ || is_currently_audible_;
1662
1663 SendPageMessage(
1664 new PageMsg_AudioStateChanged(MSG_ROUTING_NONE, is_currently_audible_));
altimind8bd26c2016-11-04 11:44:541665
altiminec87fd1f2016-11-17 20:22:491666 // Notification for UI updates in response to the changed audio state.
altimind8bd26c2016-11-04 11:44:541667 NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB);
Jan Rucka442c83f2017-08-08 13:27:541668
Chris Hamiltondf0d72cd2018-05-29 16:23:531669 // Ensure that audio state changes propagate from innermost to outermost
1670 // WebContents.
1671 if (GetOuterWebContents())
1672 GetOuterWebContents()->OnAudioStateChanged();
Tommy Steimel18360512017-11-01 00:38:191673
Chris Hamiltoneb41198f2018-05-24 18:41:261674 for (auto& observer : observers_)
Chris Hamiltondf0d72cd2018-05-29 16:23:531675 observer.OnAudioStateChanged(is_currently_audible_);
altimind8bd26c2016-11-04 11:44:541676}
1677
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411678base::TimeTicks WebContentsImpl::GetLastActiveTime() {
[email protected]9a890452014-02-13 22:21:021679 return last_active_time_;
[email protected]3e324142012-06-25 18:26:331680}
1681
[email protected]9e2e4632012-07-27 16:38:411682void WebContentsImpl::WasShown() {
[email protected]96d185d2009-04-24 03:28:541683 controller_.SetActive(true);
[email protected]de3c5d82014-05-28 22:12:591684
danakjc1e71e4f2018-12-05 19:34:351685 // This shows the Page before showing the individual RenderWidgets, as
1686 // RenderWidgets will work to produce compositor frames and handle input
1687 // as soon as they are shown. But the Page and other classes do not expect to
1688 // be producing frames when the Page is hidden. So we make sure the Page is
1689 // shown first.
1690 SendPageMessage(new PageMsg_WasShown(MSG_ROUTING_NONE));
1691
EhsanK2075c7e2017-08-21 02:42:391692 if (auto* view = GetRenderWidgetHostView()) {
lfgf949e0e2017-05-10 22:18:111693 view->Show();
[email protected]789e9152009-08-04 21:59:431694#if defined(OS_MACOSX)
lfgf949e0e2017-05-10 22:18:111695 view->SetActive(true);
[email protected]789e9152009-08-04 21:59:431696#endif
1697 }
[email protected]96d185d2009-04-24 03:28:541698
EhsanK2075c7e2017-08-21 02:42:391699 if (!ShowingInterstitialPage())
1700 SetVisibilityForChildViews(true);
1701
gab0dccfef2015-05-20 18:43:391702 last_active_time_ = base::TimeTicks::Now();
Francois Doraye6161152018-03-27 22:05:371703 SetVisibility(Visibility::VISIBLE);
Lukasz Anforowicz873e6082018-05-17 00:12:331704
1705 for (FrameTreeNode* node : frame_tree_.Nodes()) {
1706 RenderFrameProxyHost* parent = node->render_manager()->GetProxyToParent();
1707 if (!parent)
1708 continue;
1709
Alex Moshchuk2b0c91d2018-10-10 18:41:161710 parent->cross_process_frame_connector()->DelegateWasShown();
Lukasz Anforowicz873e6082018-05-17 00:12:331711 }
[email protected]be1f56ab2011-12-22 06:55:311712}
1713
[email protected]b172aee2012-04-10 17:05:261714void WebContentsImpl::WasHidden() {
Jennifer Apacible72c31412018-06-19 16:52:241715 // If there are entities capturing screenshots or video (e.g. mirroring),
François Beaufort0e27cee02019-01-18 13:21:351716 // or in Picture-in-Picture mode, don't activate the "disable rendering"
1717 // optimization.
1718 if (!IsBeingCaptured() && !HasPictureInPictureVideo()) {
danakjc1e71e4f2018-12-05 19:34:351719 // This hides the individual RenderWidgets before hiding the Page, as
1720 // RenderWidgets will work to produce compositor frames and handle input
1721 // until they are hidden. But the Page and other classes do not expect to
1722 // be producing frames when the Page is hidden. So we stop the widgets,
1723 // then hide the Page.
1724 //
[email protected]151a63d2011-12-20 22:32:521725 // |GetRenderViewHost()| can be NULL if the user middle clicks a link to
[email protected]54597982013-02-06 01:59:551726 // open a tab in the background, then closes the tab before selecting it.
[email protected]b172aee2012-04-10 17:05:261727 // This is because closing the tab calls WebContentsImpl::Destroy(), which
[email protected]151a63d2011-12-20 22:32:521728 // removes the |GetRenderViewHost()|; then when we actually destroy the
[email protected]3e324142012-06-25 18:26:331729 // window, OnWindowPosChanged() notices and calls WasHidden() (which
[email protected]151a63d2011-12-20 22:32:521730 // calls us).
EhsanK2075c7e2017-08-21 02:42:391731 if (auto* view = GetRenderWidgetHostView())
lfgf949e0e2017-05-10 22:18:111732 view->Hide();
lfg717154072016-06-30 15:04:161733
EhsanK2075c7e2017-08-21 02:42:391734 if (!ShowingInterstitialPage())
Igor Eremeevef59187a2017-09-29 00:25:421735 SetVisibilityForChildViews(false);
[email protected]96d185d2009-04-24 03:28:541736 }
1737
François Beaufort0e27cee02019-01-18 13:21:351738 // Inform the renderer that the page was hidden if there are no entities
1739 // capturing screenshots or video (e.g. mirroring).
1740 if (!IsBeingCaptured())
1741 SendPageMessage(new PageMsg_WasHidden(MSG_ROUTING_NONE));
1742
Francois Doraye6161152018-03-27 22:05:371743 SetVisibility(Visibility::HIDDEN);
[email protected]375fa1b2012-05-22 22:05:371744}
1745
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411746bool WebContentsImpl::HasRecentInteractiveInputEvent() {
Daniel Cheng90196c82018-04-25 21:49:141747 static constexpr base::TimeDelta kMaxInterval =
1748 base::TimeDelta::FromSeconds(5);
1749 base::TimeDelta delta =
1750 ui::EventTimeForNow() - last_interactive_input_event_time_;
1751 // Note: the expectation is that the caller is typically expecting an input
1752 // event, e.g. validating that a WebUI message that requires a gesture is
1753 // actually attached to a gesture. Logging to UMA here should hopefully give
1754 // sufficient data if 5 seconds is actually sufficient (or even too high a
1755 // threshhold).
1756 UMA_HISTOGRAM_TIMES("Tabs.TimeSinceLastInteraction", delta);
1757 return delta <= kMaxInterval;
1758}
1759
Avi Drissman738ea192018-08-29 20:24:161760void WebContentsImpl::SetIgnoreInputEvents(bool ignore_input_events) {
1761 ignore_input_events_ = ignore_input_events;
1762}
1763
Bo Liu168c8642017-08-28 18:26:021764#if defined(OS_ANDROID)
Bo Liu16dd7b8322018-05-02 21:19:501765void WebContentsImpl::SetMainFrameImportance(
1766 ChildProcessImportance importance) {
1767 GetMainFrame()->GetRenderWidgetHost()->SetImportance(importance);
Bo Liu7c6779e92017-08-16 02:02:281768 if (ShowingInterstitialPage()) {
Bo Liu16dd7b8322018-05-02 21:19:501769 static_cast<RenderFrameHostImpl*>(interstitial_page_->GetMainFrame())
1770 ->GetRenderWidgetHost()
1771 ->SetImportance(importance);
Bo Liu7c6779e92017-08-16 02:02:281772 }
Bo Liu7c6779e92017-08-16 02:02:281773}
Bo Liu168c8642017-08-28 18:26:021774#endif
Bo Liu7c6779e92017-08-16 02:02:281775
ccameron8807c38f2015-01-17 00:29:191776void WebContentsImpl::WasOccluded() {
François Beaufortfec686f2019-01-18 17:12:491777 // If there are entities capturing screenshots or video (e.g. mirroring),
1778 // or in Picture-in-Picture mode, don't activate the "disable rendering"
1779 // optimization.
1780 if (!IsBeingCaptured() && !HasPictureInPictureVideo()) {
Ehsan Karamada03e1582018-12-05 23:07:561781 if (auto* main_view = GetRenderWidgetHostView())
1782 main_view->WasOccluded();
1783 if (!ShowingInterstitialPage()) {
1784 // Occluding child view is treated as hiding the view
1785 // (https://crbug.com/903455).
1786 SetVisibilityForChildViews(false);
1787 }
Francois Doray18800972017-08-25 22:38:581788 }
François Beaufortfec686f2019-01-18 17:12:491789
1790 // Inform the renderer that the page was hidden if there are no entities
1791 // capturing screenshots or video (e.g. mirroring).
1792 if (!IsBeingCaptured())
1793 SendPageMessage(new PageMsg_WasHidden(MSG_ROUTING_NONE));
1794
Francois Doraye6161152018-03-27 22:05:371795 SetVisibility(Visibility::OCCLUDED);
ccameron8807c38f2015-01-17 00:29:191796}
1797
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:411798Visibility WebContentsImpl::GetVisibility() {
Francois Doraye6161152018-03-27 22:05:371799 return visibility_;
Francois Dorayfe4a1772018-02-17 04:17:091800}
1801
Alex Moshchuk6fcaca752018-07-14 02:13:591802// TODO(alexmos): rename to NeedToFireBeforeUnloadOrUnload().
[email protected]b172aee2012-04-10 17:05:261803bool WebContentsImpl::NeedToFireBeforeUnload() {
[email protected]be1f56ab2011-12-22 06:55:311804 // TODO(creis): Should we fire even for interstitial pages?
Alex Moshchuk6fcaca752018-07-14 02:13:591805 if (ShowingInterstitialPage())
1806 return false;
1807
1808 if (!WillNotifyDisconnection())
1809 return false;
1810
1811 // Don't fire if the main frame's RenderViewHost indicates that beforeunload
1812 // and unload have already executed (e.g., after receiving a ClosePage ACK)
1813 // or should be ignored.
1814 if (GetRenderViewHost()->SuddenTerminationAllowed())
1815 return false;
1816
Aditya Keerthi34f37e62019-03-08 15:54:531817 // Check whether the main frame needs to run beforeunload or unload handlers.
1818 if (GetMainFrame()->GetSuddenTerminationDisablerState(
1819 blink::kBeforeUnloadHandler | blink::kUnloadHandler)) {
Alex Moshchuk6fcaca752018-07-14 02:13:591820 return true;
Aditya Keerthi34f37e62019-03-08 15:54:531821 }
Alex Moshchuk6fcaca752018-07-14 02:13:591822
1823 // Check whether any subframes need to run beforeunload handlers.
1824 //
1825 // TODO(alexmos): Also check whether subframes need to run unload handlers in
1826 // addition to beforeunload.
1827 return GetMainFrame()->ShouldDispatchBeforeUnload(
1828 true /* check_subframes_only */);
[email protected]be1f56ab2011-12-22 06:55:311829}
1830
Chris Hamilton3b3e315d2018-09-19 13:16:211831void WebContentsImpl::DispatchBeforeUnload(bool auto_cancel) {
1832 auto before_unload_type =
1833 auto_cancel ? RenderFrameHostImpl::BeforeUnloadType::DISCARD
1834 : RenderFrameHostImpl::BeforeUnloadType::TAB_CLOSE;
1835 GetMainFrame()->DispatchBeforeUnload(before_unload_type, false);
[email protected]1c3f80bd2014-04-08 23:02:061836}
1837
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:501838void WebContentsImpl::AttachInnerWebContents(
1839 std::unique_ptr<WebContents> inner_web_contents,
1840 RenderFrameHost* render_frame_host) {
1841 WebContentsImpl* inner_web_contents_impl =
1842 static_cast<WebContentsImpl*>(inner_web_contents.get());
1843 DCHECK(!inner_web_contents_impl->node_.outer_web_contents());
1844 auto* render_frame_host_impl =
1845 static_cast<RenderFrameHostImpl*>(render_frame_host);
1846 DCHECK_EQ(&frame_tree_,
1847 render_frame_host_impl->frame_tree_node()->frame_tree());
Lucas Furukawa Gadani105de1e812018-05-31 19:38:391848
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:501849 RenderFrameHostManager* inner_render_manager =
1850 inner_web_contents_impl->GetRenderManager();
1851 RenderFrameHostImpl* inner_main_frame =
1852 inner_render_manager->current_frame_host();
1853 RenderViewHostImpl* inner_render_view_host =
1854 inner_render_manager->current_host();
1855 auto* outer_render_manager =
1856 render_frame_host_impl->frame_tree_node()->render_manager();
lfg91a79e12016-04-01 23:52:191857
Lucas Furukawa Gadani105de1e812018-05-31 19:38:391858 // When attaching a WebContents as an inner WebContents, we need to replace
1859 // the Webcontents' view with a WebContentsViewChildFrame.
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:501860 inner_web_contents_impl->view_.reset(new WebContentsViewChildFrame(
1861 inner_web_contents_impl,
1862 GetContentClient()->browser()->GetWebContentsViewDelegate(
1863 inner_web_contents_impl),
1864 &inner_web_contents_impl->render_view_host_delegate_view_));
Lucas Furukawa Gadani105de1e812018-05-31 19:38:391865
lfg91a79e12016-04-01 23:52:191866 // When the WebContents being initialized has an opener, the browser side
1867 // Render{View,Frame}Host must be initialized and the RenderWidgetHostView
1868 // created. This is needed because the usual initialization happens during
1869 // the first navigation, but when attaching a new window we don't navigate
1870 // before attaching. If the browser side is already initialized, the calls
1871 // below will just early return.
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:501872 inner_render_manager->InitRenderView(inner_render_view_host, nullptr);
1873 inner_main_frame->Init();
1874 if (!inner_render_manager->GetRenderWidgetHostView()) {
1875 inner_web_contents_impl->CreateRenderWidgetHostViewForRenderManager(
1876 inner_render_view_host);
1877 }
lfg91a79e12016-04-01 23:52:191878
lazyboy6ec48b2a2015-06-29 15:18:141879 // Create a link to our outer WebContents.
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:501880 node_.AttachInnerWebContents(std::move(inner_web_contents),
1881 render_frame_host_impl);
lazyboy6ec48b2a2015-06-29 15:18:141882
lazyboy6ec48b2a2015-06-29 15:18:141883 // Create a proxy in top-level RenderFrameHostManager, pointing to the
1884 // SiteInstance of the outer WebContents. The proxy will be used to send
1885 // postMessage to the inner WebContents.
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:501886 auto* proxy = inner_render_manager->CreateOuterDelegateProxy(
1887 render_frame_host_impl->GetSiteInstance());
lazyboy6ec48b2a2015-06-29 15:18:141888
Lucas Furukawa Gadani99125822019-01-03 15:41:491889 // When attaching a GuestView as an inner WebContents, there should already be
1890 // a live RenderFrame, which has to be swapped. When attaching a portal, there
1891 // will not be a live RenderFrame before creating the proxy.
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:501892 if (render_frame_host_impl->IsRenderFrameLive()) {
1893 inner_render_manager->SwapOuterDelegateFrame(render_frame_host_impl, proxy);
Lucas Furukawa Gadani99125822019-01-03 15:41:491894
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:501895 inner_web_contents_impl->ReattachToOuterWebContentsFrame();
Lucas Furukawa Gadani99125822019-01-03 15:41:491896 }
ekaramadadd882292016-06-08 15:22:561897
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:501898 if (frame_tree_.GetFocusedFrame() ==
1899 render_frame_host_impl->frame_tree_node()) {
1900 inner_web_contents_impl->SetFocusedFrame(
1901 inner_web_contents_impl->frame_tree_.root(),
1902 render_frame_host_impl->GetSiteInstance());
avallee7529b2a2017-04-26 11:37:511903 }
Lucas Furukawa Gadani6e5b4f32019-03-02 04:18:501904 outer_render_manager->set_attach_complete();
lazyboy6ec48b2a2015-06-29 15:18:141905}
1906
Lucas Furukawa Gadani99125822019-01-03 15:41:491907std::unique_ptr<WebContents> WebContentsImpl::DetachFromOuterWebContents() {
1908 DCHECK(node_.outer_web_contents());
1909 if (RenderWidgetHostViewBase* view =
1910 static_cast<RenderWidgetHostViewBase*>(GetMainFrame()->GetView())) {
1911 view->Destroy();
1912 }
1913 GetRenderManager()->DeleteOuterDelegateProxy(
1914 node_.OuterContentsFrameTreeNode()
1915 ->current_frame_host()
1916 ->GetSiteInstance());
1917 view_.reset(CreateWebContentsView(
1918 this, GetContentClient()->browser()->GetWebContentsViewDelegate(this),
1919 &render_view_host_delegate_view_));
1920 view_->CreateView(GetPreferredSize(), nullptr);
1921 std::unique_ptr<WebContents> web_contents =
1922 node_.DisconnectFromOuterWebContents();
1923 DCHECK_EQ(web_contents.get(), this);
1924 node_.SetFocusedWebContents(this);
1925 CreateRenderWidgetHostViewForRenderManager(GetRenderViewHost());
1926 return web_contents;
1927}
1928
W. James MacLean2dc75ed42019-03-06 14:31:091929void WebContentsImpl::RecursivelyRegisterFrameSinkIds() {
1930 auto* view = static_cast<RenderWidgetHostViewBase*>(
1931 GetRenderManager()->GetRenderWidgetHostView());
1932 DCHECK(view);
1933 if (!view->IsRenderWidgetHostViewChildFrame())
1934 return;
1935 static_cast<RenderWidgetHostViewChildFrame*>(view)->RegisterFrameSinkId();
1936
1937 for (auto* inner_web_contents : node_.GetInnerWebContents()) {
1938 static_cast<WebContentsImpl*>(inner_web_contents)
1939 ->RecursivelyRegisterFrameSinkIds();
1940 }
1941}
1942
Lucas Furukawa Gadaniaed1fed2017-10-13 17:34:141943void WebContentsImpl::ReattachToOuterWebContentsFrame() {
1944 DCHECK(node_.outer_web_contents());
1945 auto* render_manager = GetRenderManager();
1946 auto* parent_frame =
1947 node_.OuterContentsFrameTreeNode()->current_frame_host()->GetParent();
1948 render_manager->SetRWHViewForInnerContents(
1949 render_manager->GetRenderWidgetHostView());
1950
W. James MacLean2dc75ed42019-03-06 14:31:091951 RecursivelyRegisterFrameSinkIds();
Lucas Furukawa Gadaniaed1fed2017-10-13 17:34:141952
1953 // Set up the the guest's AX tree to point back at the embedder's AX tree.
1954 GetMainFrame()->set_browser_plugin_embedder_ax_tree_id(
1955 parent_frame->GetAXTreeID());
1956 GetMainFrame()->UpdateAXTreeData();
1957}
1958
dalecurtis6c58ed02016-10-28 23:02:371959void WebContentsImpl::DidChangeVisibleSecurityState() {
Emily Starkc663b0242019-04-26 19:34:451960 if (delegate_)
dalecurtis6c58ed02016-10-28 23:02:371961 delegate_->VisibleSecurityStateChanged(this);
Emily Starkc663b0242019-04-26 19:34:451962 for (auto& observer : observers_)
1963 observer.DidChangeVisibleSecurityState();
dalecurtis6c58ed02016-10-28 23:02:371964}
1965
Ahmed Fakhryd70bf37b2018-04-04 17:07:241966void WebContentsImpl::NotifyPreferencesChanged() {
1967 std::set<RenderViewHost*> render_view_host_set;
1968 for (FrameTreeNode* node : frame_tree_.Nodes()) {
Giovanni Ortuño Urquidi17a5d7c2018-04-30 06:48:531969 render_view_host_set.insert(
1970 node->current_frame_host()->GetRenderViewHost());
Ahmed Fakhryd70bf37b2018-04-04 17:07:241971 }
Giovanni Ortuño Urquidi17a5d7c2018-04-30 06:48:531972
Ahmed Fakhryd70bf37b2018-04-04 17:07:241973 for (RenderViewHost* render_view_host : render_view_host_set)
1974 render_view_host->OnWebkitPreferencesChanged();
1975}
1976
[email protected]b172aee2012-04-10 17:05:261977void WebContentsImpl::Stop() {
dcheng57e39e22016-01-21 00:25:381978 for (FrameTreeNode* node : frame_tree_.Nodes())
1979 node->StopLoading();
ericwilligersde386342016-10-19 02:35:091980 for (auto& observer : observers_)
1981 observer.NavigationStopped();
[email protected]0bfbf882011-12-22 18:19:271982}
1983
Francois Doray47f759d2018-06-11 18:13:511984void WebContentsImpl::SetPageFrozen(bool frozen) {
1985 // A visible page is never frozen.
1986 DCHECK_NE(Visibility::VISIBLE, GetVisibility());
1987
1988 SendPageMessage(new PageMsg_SetPageFrozen(MSG_ROUTING_NONE, frozen));
Fadi Meawada6573e02018-03-10 00:52:111989}
1990
erikchen6c7df7f7a2018-05-03 18:13:591991std::unique_ptr<WebContents> WebContentsImpl::Clone() {
[email protected]14392a52012-05-02 20:28:441992 // We use our current SiteInstance since the cloned entry will use it anyway.
alexmose201c7cd2015-06-10 17:14:211993 // We pass our own opener so that the cloned page can access it if it was set
[email protected]ed245db2012-12-18 08:00:451994 // before.
[email protected]54944cde2012-12-09 09:24:591995 CreateParams create_params(GetBrowserContext(), GetSiteInstance());
[email protected]fc2b46b2014-05-03 16:33:451996 create_params.initial_size = GetContainerBounds().size();
Ian Clelland5cbaaf82017-11-27 22:00:031997 FrameTreeNode* opener = frame_tree_.root()->opener();
1998 RenderFrameHostImpl* opener_rfh = nullptr;
1999 if (opener)
2000 opener_rfh = opener->current_frame_host();
erikchen6c7df7f7a2018-05-03 18:13:592001 std::unique_ptr<WebContentsImpl> tc =
erikchen50735fd2018-05-05 15:08:332002 CreateWithOpener(create_params, opener_rfh);
Lucas Furukawa Gadani5553a152019-01-08 18:55:572003 tc->GetController().CopyStateFrom(&controller_, true);
ericwilligersde386342016-10-19 02:35:092004 for (auto& observer : observers_)
erikchen6c7df7f7a2018-05-03 18:13:592005 observer.DidCloneToNewWebContents(this, tc.get());
[email protected]0bfbf882011-12-22 18:19:272006 return tc;
2007}
2008
[email protected]14392a52012-05-02 20:28:442009void WebContentsImpl::Observe(int type,
[email protected]8ff00d72012-10-23 19:12:212010 const NotificationSource& source,
2011 const NotificationDetails& details) {
[email protected]14392a52012-05-02 20:28:442012 switch (type) {
[email protected]8ff00d72012-10-23 19:12:212013 case NOTIFICATION_RENDER_WIDGET_HOST_DESTROYED: {
2014 RenderWidgetHost* host = Source<RenderWidgetHost>(source).ptr();
[email protected]0b431992014-06-24 00:08:032015 RenderWidgetHostView* view = host->GetView();
2016 if (view == GetFullscreenRenderWidgetHostView()) {
2017 // We cannot just call view_->RestoreFocus() here. On some platforms,
2018 // attempting to focus the currently-invisible WebContentsView will be
2019 // flat-out ignored. Therefore, this boolean is used to track whether
2020 // we will request focus after the fullscreen widget has been
2021 // destroyed.
2022 fullscreen_widget_had_focus_at_shutdown_ = (view && view->HasFocus());
2023 } else {
alexmosc2a8cec2016-05-23 22:19:532024 for (auto i = pending_widget_views_.begin();
[email protected]0b431992014-06-24 00:08:032025 i != pending_widget_views_.end(); ++i) {
2026 if (host->GetView() == i->second) {
2027 pending_widget_views_.erase(i);
2028 break;
2029 }
[email protected]bafe6cd2012-05-23 23:09:502030 }
2031 }
2032 break;
2033 }
[email protected]14392a52012-05-02 20:28:442034 default:
2035 NOTREACHED();
2036 }
2037}
2038
[email protected]ec6c05f2013-10-23 18:41:572039WebContents* WebContentsImpl::GetWebContents() {
2040 return this;
2041}
2042
[email protected]54944cde2012-12-09 09:24:592043void WebContentsImpl::Init(const WebContents::CreateParams& params) {
[email protected]fa944cb82013-11-15 17:51:212044 // This is set before initializing the render manager since
[email protected]b0936d22013-11-28 06:47:362045 // RenderFrameHostManager::Init calls back into us via its delegate to ask if
[email protected]fa944cb82013-11-15 17:51:212046 // it should be hidden.
Francois Doraye6161152018-03-27 22:05:372047 visibility_ =
2048 params.initially_hidden ? Visibility::HIDDEN : Visibility::VISIBLE;
[email protected]d6fa88f2013-10-18 16:00:432049
Sebastien Marchand28cf23182018-06-20 02:39:352050 if (!params.last_active_time.is_null())
2051 last_active_time_ = params.last_active_time;
2052
dcheng3ce04b62015-10-26 23:30:552053 // The routing ids must either all be set or all be unset.
avic2c29d782014-12-12 00:23:542054 DCHECK((params.routing_id == MSG_ROUTING_NONE &&
dcheng3ce04b62015-10-26 23:30:552055 params.main_frame_routing_id == MSG_ROUTING_NONE &&
2056 params.main_frame_widget_routing_id == MSG_ROUTING_NONE) ||
avic2c29d782014-12-12 00:23:542057 (params.routing_id != MSG_ROUTING_NONE &&
dcheng3ce04b62015-10-26 23:30:552058 params.main_frame_routing_id != MSG_ROUTING_NONE &&
2059 params.main_frame_widget_routing_id != MSG_ROUTING_NONE));
2060
2061 scoped_refptr<SiteInstance> site_instance = params.site_instance;
2062 if (!site_instance)
2063 site_instance = SiteInstance::Create(params.browser_context);
Lukasz Anforowicz3caa8372018-06-05 17:22:072064 if (params.desired_renderer_state == CreateParams::kNoRendererProcess) {
2065 static_cast<SiteInstanceImpl*>(site_instance.get())
2066 ->PreventAssociationWithSpareProcess();
2067 }
dcheng3ce04b62015-10-26 23:30:552068
2069 // A main RenderFrameHost always has a RenderWidgetHost, since it is always a
2070 // local root by definition.
2071 // TODO(avi): Once RenderViewHostImpl has-a RenderWidgetHostImpl, it will no
2072 // longer be necessary to eagerly grab a routing ID for the view.
2073 // https://crbug.com/545684
2074 int32_t view_routing_id = params.routing_id;
2075 int32_t main_frame_widget_routing_id = params.main_frame_widget_routing_id;
2076 if (main_frame_widget_routing_id == MSG_ROUTING_NONE) {
Albert J. Wongcb004632018-07-10 22:58:252077 view_routing_id = site_instance->GetProcess()->GetNextRoutingID();
2078 main_frame_widget_routing_id =
dcheng3ce04b62015-10-26 23:30:552079 site_instance->GetProcess()->GetNextRoutingID();
2080 }
2081
Albert J. Wongcb004632018-07-10 22:58:252082 DCHECK_NE(view_routing_id, main_frame_widget_routing_id);
2083
Dave Tapuska485aca92017-08-08 00:47:582084 GetRenderManager()->Init(
2085 site_instance.get(), view_routing_id, params.main_frame_routing_id,
2086 main_frame_widget_routing_id, params.renderer_initiated_creation);
lukasza464d8692016-02-22 19:26:322087
lukasza5140a412016-09-15 21:12:302088 // blink::FrameTree::setName always keeps |unique_name| empty in case of a
2089 // main frame - let's do the same thing here.
2090 std::string unique_name;
lukasza464d8692016-02-22 19:26:322091 frame_tree_.root()->SetFrameName(params.main_frame_name, unique_name);
[email protected]d1198fd2012-08-13 22:50:192092
[email protected]fc2b46b2014-05-03 16:33:452093 WebContentsViewDelegate* delegate =
2094 GetContentClient()->browser()->GetWebContentsViewDelegate(this);
2095
sky52a39e342016-12-17 17:23:222096 if (GuestMode::IsCrossProcessFrameGuest(this)) {
2097 view_.reset(new WebContentsViewChildFrame(
2098 this, delegate, &render_view_host_delegate_view_));
2099 } else {
2100 view_.reset(CreateWebContentsView(this, delegate,
2101 &render_view_host_delegate_view_));
Scott Violetc884c7fd2017-11-03 23:51:012102 if (browser_plugin_guest_) {
2103 view_ = std::make_unique<WebContentsViewGuest>(
2104 this, browser_plugin_guest_.get(), std::move(view_),
2105 &render_view_host_delegate_view_);
2106 }
[email protected]d1198fd2012-08-13 22:50:192107 }
[email protected]fc2b46b2014-05-03 16:33:452108 CHECK(render_view_host_delegate_view_);
[email protected]d1198fd2012-08-13 22:50:192109 CHECK(view_.get());
2110
[email protected]ed245db2012-12-18 08:00:452111 gfx::Size initial_size = params.initial_size;
[email protected]54944cde2012-12-09 09:24:592112 view_->CreateView(initial_size, params.context);
[email protected]d1198fd2012-08-13 22:50:192113
brettw4b461082016-11-19 18:55:162114#if BUILDFLAG(ENABLE_PLUGINS)
tommyclieb25b2a2014-11-03 19:45:092115 plugin_content_origin_whitelist_.reset(
2116 new PluginContentOriginWhitelist(this));
2117#endif
2118
[email protected]d1198fd2012-08-13 22:50:192119 registrar_.Add(this,
[email protected]8ff00d72012-10-23 19:12:212120 NOTIFICATION_RENDER_WIDGET_HOST_DESTROYED,
2121 NotificationService::AllBrowserContextsAndSources());
[email protected]f28ef9a32014-05-12 16:36:102122
leon.hane4db177a2017-02-07 14:19:162123 screen_orientation_provider_.reset(new ScreenOrientationProvider(this));
[email protected]01f83f92014-06-17 14:41:542124
mlamouriefdca9d2014-09-16 16:55:402125 manifest_manager_host_.reset(new ManifestManagerHost(this));
2126
mfomitchev2b8b066a2016-01-28 19:23:152127#if defined(OS_ANDROID)
Gyuyoung Kim1f672aa2019-04-25 02:10:252128 date_time_chooser_.reset(new DateTimeChooserAndroid(this));
[email protected]583418cc2013-01-17 14:01:102129#endif
fsamuel7310a4262014-12-05 05:06:442130
2131 // BrowserPluginGuest::Init needs to be called after this WebContents has
2132 // a RenderWidgetHostViewGuest. That is, |view_->CreateView| above.
2133 if (browser_plugin_guest_)
2134 browser_plugin_guest_->Init();
nickf9acfbe2014-12-23 19:12:372135
2136 for (size_t i = 0; i < g_created_callbacks.Get().size(); i++)
2137 g_created_callbacks.Get().at(i).Run(this);
naskob21fe4872015-02-24 14:15:562138
2139 // If the WebContents creation was renderer-initiated, it means that the
2140 // corresponding RenderView and main RenderFrame have already been created.
2141 // Ensure observers are notified about this.
2142 if (params.renderer_initiated_creation) {
avi3627ecac2015-10-16 17:40:432143 GetRenderViewHost()->GetWidget()->set_renderer_initialized(true);
Alex Moshchuk27caae82017-09-11 23:11:182144 GetRenderViewHost()->DispatchRenderViewCreated();
naskob21fe4872015-02-24 14:15:562145 GetRenderManager()->current_frame_host()->SetRenderFrameCreated(true);
2146 }
naskof5940b9f2015-03-02 23:04:052147
lof84501da082016-05-23 21:22:542148 // Create the renderer process in advance if requested.
Lukasz Anforowicz3caa8372018-06-05 17:22:072149 if (params.desired_renderer_state ==
2150 CreateParams::kInitializeAndWarmupRendererProcess) {
lof84501da082016-05-23 21:22:542151 if (!GetRenderManager()->current_frame_host()->IsRenderFrameLive()) {
2152 GetRenderManager()->InitRenderView(GetRenderViewHost(), nullptr);
2153 }
2154 }
2155
naskof5940b9f2015-03-02 23:04:052156 // Ensure that observers are notified of the creation of this WebContents's
2157 // main RenderFrameHost. It must be done here for main frames, since the
2158 // NotifySwappedFromRenderManager expects view_ to already be created and that
2159 // happens after RenderFrameHostManager::Init.
2160 NotifySwappedFromRenderManager(
2161 nullptr, GetRenderManager()->current_frame_host(), true);
[email protected]d1198fd2012-08-13 22:50:192162}
2163
[email protected]7fff43e2013-05-21 20:21:102164void WebContentsImpl::OnWebContentsDestroyed(WebContentsImpl* web_contents) {
2165 RemoveDestructionObserver(web_contents);
2166
[email protected]ceee8cd2013-03-08 04:59:512167 // Clear a pending contents that has been closed before being shown.
alexmosc2a8cec2016-05-23 22:19:532168 for (auto iter = pending_contents_.begin(); iter != pending_contents_.end();
[email protected]ceee8cd2013-03-08 04:59:512169 ++iter) {
erikchenbee5c9622018-04-27 19:30:252170 if (iter->second.get() != web_contents)
[email protected]ceee8cd2013-03-08 04:59:512171 continue;
erikchenbee5c9622018-04-27 19:30:252172
2173 // Someone else has deleted the WebContents. That should never happen!
2174 // TODO(erikchen): Fix semantics here. https://crbug.com/832879.
2175 iter->second.release();
[email protected]ceee8cd2013-03-08 04:59:512176 pending_contents_.erase(iter);
[email protected]ceee8cd2013-03-08 04:59:512177 return;
2178 }
2179 NOTREACHED();
[email protected]14392a52012-05-02 20:28:442180}
2181
[email protected]7fff43e2013-05-21 20:21:102182void WebContentsImpl::AddDestructionObserver(WebContentsImpl* web_contents) {
Jan Wilken Dörrie73c901e2019-06-12 09:02:322183 if (!base::Contains(destruction_observers_, web_contents)) {
[email protected]7fff43e2013-05-21 20:21:102184 destruction_observers_[web_contents] =
Jeremy Roman04f27c372017-10-27 15:20:552185 std::make_unique<DestructionObserver>(this, web_contents);
[email protected]7fff43e2013-05-21 20:21:102186 }
2187}
2188
2189void WebContentsImpl::RemoveDestructionObserver(WebContentsImpl* web_contents) {
avia8cf81982016-08-12 20:28:462190 destruction_observers_.erase(web_contents);
[email protected]7fff43e2013-05-21 20:21:102191}
2192
[email protected]b172aee2012-04-10 17:05:262193void WebContentsImpl::AddObserver(WebContentsObserver* observer) {
[email protected]be1f56ab2011-12-22 06:55:312194 observers_.AddObserver(observer);
2195}
2196
[email protected]b172aee2012-04-10 17:05:262197void WebContentsImpl::RemoveObserver(WebContentsObserver* observer) {
[email protected]be1f56ab2011-12-22 06:55:312198 observers_.RemoveObserver(observer);
2199}
2200
[email protected]948481d2014-06-11 18:32:222201std::set<RenderWidgetHostView*>
2202WebContentsImpl::GetRenderWidgetHostViewsInTree() {
2203 std::set<RenderWidgetHostView*> set;
2204 if (ShowingInterstitialPage()) {
lfgf949e0e2017-05-10 22:18:112205 if (RenderWidgetHostView* rwhv = GetRenderWidgetHostView())
2206 set.insert(rwhv);
[email protected]948481d2014-06-11 18:32:222207 } else {
dchengafb53e22016-02-04 08:11:082208 for (RenderFrameHost* rfh : GetAllFrames()) {
lfgf949e0e2017-05-10 22:18:112209 if (RenderWidgetHostView* rwhv = static_cast<RenderFrameHostImpl*>(rfh)
2210 ->frame_tree_node()
2211 ->render_manager()
2212 ->GetRenderWidgetHostView()) {
2213 set.insert(rwhv);
2214 }
dchengafb53e22016-02-04 08:11:082215 }
[email protected]948481d2014-06-11 18:32:222216 }
[email protected]de3c5d82014-05-28 22:12:592217 return set;
2218}
2219
[email protected]b172aee2012-04-10 17:05:262220void WebContentsImpl::Activate() {
[email protected]d5f942ba2008-09-26 19:30:342221 if (delegate_)
2222 delegate_->ActivateContents(this);
2223}
2224
avi3e4b8512015-11-11 01:55:492225void WebContentsImpl::LostCapture(RenderWidgetHostImpl* render_widget_host) {
2226 if (!RenderViewHostImpl::From(render_widget_host))
2227 return;
2228
[email protected]63954792011-07-11 04:17:482229 if (delegate_)
2230 delegate_->LostCapture();
2231}
2232
Steven Holte38041342018-06-22 22:00:572233ukm::SourceId WebContentsImpl::GetUkmSourceIdForLastCommittedSource() const {
2234 return last_committed_source_id_;
2235}
2236
Ahmed Fakhryfaa32d22018-10-05 15:56:162237void WebContentsImpl::SetTopControlsShownRatio(
2238 RenderWidgetHostImpl* render_widget_host,
2239 float ratio) {
2240 if (!delegate_)
2241 return;
2242
2243 RenderFrameHostImpl* rfh = GetMainFrame();
2244 if (!rfh || render_widget_host != rfh->GetRenderWidgetHost())
2245 return;
2246
2247 delegate_->SetTopControlsShownRatio(this, ratio);
Ahmed Fakhry58e6ef542018-09-04 18:05:382248}
2249
Ahmed Fakhry58e6ef542018-09-04 18:05:382250void WebContentsImpl::SetTopControlsGestureScrollInProgress(bool in_progress) {
2251 if (delegate_)
2252 delegate_->SetTopControlsGestureScrollInProgress(in_progress);
2253}
2254
lazyboy63f5b312015-11-23 20:19:522255void WebContentsImpl::RenderWidgetCreated(
2256 RenderWidgetHostImpl* render_widget_host) {
2257 created_widgets_.insert(render_widget_host);
2258}
2259
[email protected]b24b68a2012-09-24 21:57:262260void WebContentsImpl::RenderWidgetDeleted(
2261 RenderWidgetHostImpl* render_widget_host) {
lazyboy63f5b312015-11-23 20:19:522262 // Note that |is_being_destroyed_| can be true at this point as
2263 // ~WebContentsImpl() calls RFHM::ClearRFHsPendingShutdown(), which might lead
2264 // us here.
2265 created_widgets_.erase(render_widget_host);
[email protected]b24b68a2012-09-24 21:57:262266
lazyboy63f5b312015-11-23 20:19:522267 if (is_being_destroyed_)
2268 return;
[email protected]44470a22013-01-24 01:21:542269
2270 if (render_widget_host &&
alexmosc9e76ec2016-05-16 22:59:372271 render_widget_host->GetRoutingID() == fullscreen_widget_routing_id_ &&
2272 render_widget_host->GetProcess()->GetID() ==
2273 fullscreen_widget_process_id_) {
[email protected]4aebbca2013-09-17 22:26:492274 if (delegate_ && delegate_->EmbedsFullscreenWidget())
mlamouri7a78d6fd2015-01-17 13:23:532275 delegate_->ExitFullscreenModeForTab(this);
ericwilligersde386342016-10-19 02:35:092276 for (auto& observer : observers_)
2277 observer.DidDestroyFullscreenWidget();
alexmosc9e76ec2016-05-16 22:59:372278 fullscreen_widget_process_id_ = ChildProcessHost::kInvalidUniqueID;
[email protected]44470a22013-01-24 01:21:542279 fullscreen_widget_routing_id_ = MSG_ROUTING_NONE;
[email protected]0b431992014-06-24 00:08:032280 if (fullscreen_widget_had_focus_at_shutdown_)
2281 view_->RestoreFocus();
[email protected]44470a22013-01-24 01:21:542282 }
lfgbee1e0a2016-06-08 21:24:212283
lfg7d4caad2017-03-22 09:01:452284 if (render_widget_host == mouse_lock_widget_)
lfgff8dfc8d2016-10-21 17:42:322285 LostMouseLock(mouse_lock_widget_);
Joe Downing192998b22018-03-22 15:51:362286
Joe Downingc1a57efe2018-05-09 18:23:542287 CancelKeyboardLock(render_widget_host);
[email protected]b24b68a2012-09-24 21:57:262288}
2289
miu9e14e492014-10-25 02:39:042290void WebContentsImpl::RenderWidgetGotFocus(
2291 RenderWidgetHostImpl* render_widget_host) {
calamity7fe55ce2015-04-10 03:59:372292 // Notify the observers if an embedded fullscreen widget was focused.
2293 if (delegate_ && render_widget_host && delegate_->EmbedsFullscreenWidget() &&
2294 render_widget_host->GetView() == GetFullscreenRenderWidgetHostView()) {
Michael Thiessen896405db2017-07-20 17:52:322295 NotifyWebContentsFocused(render_widget_host);
calamity7fe55ce2015-04-10 03:59:372296 }
miu9e14e492014-10-25 02:39:042297}
2298
zijiehe3bee08e22017-05-17 04:14:462299void WebContentsImpl::RenderWidgetLostFocus(
2300 RenderWidgetHostImpl* render_widget_host) {
Michael Thiessen896405db2017-07-20 17:52:322301 // Notify the observers if an embedded fullscreen widget lost focus.
2302 if (delegate_ && render_widget_host && delegate_->EmbedsFullscreenWidget() &&
2303 render_widget_host->GetView() == GetFullscreenRenderWidgetHostView()) {
2304 NotifyWebContentsLostFocus(render_widget_host);
2305 }
zijiehe3bee08e22017-05-17 04:14:462306}
2307
estaded0a0c992015-01-21 14:12:062308void WebContentsImpl::RenderWidgetWasResized(
rouslan2f5993f2015-01-29 00:18:312309 RenderWidgetHostImpl* render_widget_host,
Christopher Cameron07f666b2018-02-13 23:41:192310 const ScreenInfo& screen_info,
rouslan2f5993f2015-01-29 00:18:312311 bool width_changed) {
creisc014b402015-04-23 16:41:452312 RenderFrameHostImpl* rfh = GetMainFrame();
estadece344ec2015-01-23 00:12:292313 if (!rfh || render_widget_host != rfh->GetRenderWidgetHost())
estaded0a0c992015-01-21 14:12:062314 return;
estaded0a0c992015-01-21 14:12:062315
lfg8d649cc2017-04-28 18:04:302316 SendPageMessage(new PageMsg_UpdateScreenInfo(MSG_ROUTING_NONE, screen_info));
2317
ericwilligersde386342016-10-19 02:35:092318 for (auto& observer : observers_)
2319 observer.MainFrameWasResized(width_changed);
estaded0a0c992015-01-21 14:12:062320}
2321
skyf65d9bb2017-03-24 02:26:392322KeyboardEventProcessingResult WebContentsImpl::PreHandleKeyboardEvent(
2323 const NativeWebKeyboardEvent& event) {
2324 return delegate_ ? delegate_->PreHandleKeyboardEvent(this, event)
2325 : KeyboardEventProcessingResult::NOT_HANDLED;
[email protected]63954792011-07-11 04:17:482326}
2327
Scott Violetd5caa872018-10-16 22:00:512328bool WebContentsImpl::HandleKeyboardEvent(const NativeWebKeyboardEvent& event) {
[email protected]0b094002014-08-20 18:28:102329 if (browser_plugin_embedder_ &&
2330 browser_plugin_embedder_->HandleKeyboardEvent(event)) {
Scott Violetd5caa872018-10-16 22:00:512331 return true;
[email protected]0b094002014-08-20 18:28:102332 }
Scott Violetd5caa872018-10-16 22:00:512333 return delegate_ && delegate_->HandleKeyboardEvent(this, event);
[email protected]63954792011-07-11 04:17:482334}
2335
[email protected]d92026ef2014-03-03 21:04:132336bool WebContentsImpl::HandleWheelEvent(
[email protected]180ef242013-11-07 06:50:462337 const blink::WebMouseWheelEvent& event) {
[email protected]bccc4472013-04-18 16:37:192338#if !defined(OS_MACOSX)
lanweia93644f2015-01-21 22:00:332339 // On platforms other than Mac, control+mousewheel may change zoom. On Mac,
2340 // this isn't done for two reasons:
[email protected]bccc4472013-04-18 16:37:192341 // -the OS already has a gesture to do this through pinch-zoom
2342 // -if a user starts an inertial scroll, let's go, and presses control
2343 // (i.e. control+tab) then the OS's buffered scroll events will come in
2344 // with control key set which isn't what the user wants
Blink Reformat1c4d759e2017-04-09 16:34:542345 if (delegate_ && event.wheel_ticks_y &&
Ehsan Karamad0f83cb112018-09-06 22:07:362346 event.event_action == blink::WebMouseWheelEvent::EventAction::kPageZoom) {
w.shackleton49bcd392016-01-06 17:38:222347 // Count only integer cumulative scrolls as zoom events; this handles
2348 // smooth scroll and regular scroll device behavior.
Blink Reformat1c4d759e2017-04-09 16:34:542349 zoom_scroll_remainder_ += event.wheel_ticks_y;
w.shackleton49bcd392016-01-06 17:38:222350 int whole_zoom_scroll_remainder_ = std::lround(zoom_scroll_remainder_);
2351 zoom_scroll_remainder_ -= whole_zoom_scroll_remainder_;
2352 if (whole_zoom_scroll_remainder_ != 0) {
2353 delegate_->ContentsZoomChange(whole_zoom_scroll_remainder_ > 0);
2354 }
[email protected]fb3f066e2013-02-12 19:12:522355 return true;
2356 }
[email protected]bccc4472013-04-18 16:37:192357#endif
[email protected]fb3f066e2013-02-12 19:12:522358 return false;
2359}
2360
[email protected]04bce562014-01-30 05:34:542361bool WebContentsImpl::PreHandleGestureEvent(
2362 const blink::WebGestureEvent& event) {
[email protected]28042d32014-02-03 14:10:032363 return delegate_ && delegate_->PreHandleGestureEvent(this, event);
[email protected]04bce562014-01-30 05:34:542364}
2365
kenrb2a565f82015-09-02 20:24:592366RenderWidgetHostInputEventRouter* WebContentsImpl::GetInputEventRouter() {
wjmacleane31234f2015-12-14 17:20:452367 if (!is_being_destroyed_ && GetOuterWebContents())
2368 return GetOuterWebContents()->GetInputEventRouter();
2369
2370 if (!rwh_input_event_router_.get() && !is_being_destroyed_)
kenrb2a565f82015-09-02 20:24:592371 rwh_input_event_router_.reset(new RenderWidgetHostInputEventRouter);
2372 return rwh_input_event_router_.get();
2373}
2374
alexmos3fcd0ca2015-10-23 18:18:332375void WebContentsImpl::ReplicatePageFocus(bool is_focused) {
2376 // Focus loss may occur while this WebContents is being destroyed. Don't
2377 // send the message in this case, as the main frame's RenderFrameHost and
2378 // other state has already been cleared.
2379 if (is_being_destroyed_)
2380 return;
2381
2382 frame_tree_.ReplicatePageFocus(is_focused);
2383}
2384
alexmosc4cbacb2015-11-21 01:29:222385RenderWidgetHostImpl* WebContentsImpl::GetFocusedRenderWidgetHost(
2386 RenderWidgetHostImpl* receiving_widget) {
alexmosc4cbacb2015-11-21 01:29:222387 // Events for widgets other than the main frame (e.g., popup menus) should be
2388 // forwarded directly to the widget they arrived on.
2389 if (receiving_widget != GetMainFrame()->GetRenderWidgetHost())
2390 return receiving_widget;
alexmosc4d183e2015-11-06 00:46:522391
lfg1453e412017-04-11 00:48:502392 WebContentsImpl* focused_contents = GetFocusedWebContents();
2393
2394 // If the focused WebContents is showing an interstitial, return the
2395 // interstitial's widget.
2396 if (focused_contents->ShowingInterstitialPage()) {
2397 return static_cast<RenderFrameHostImpl*>(
creisadca9862017-06-16 19:12:112398 focused_contents->interstitial_page_->GetMainFrame())
lfg1453e412017-04-11 00:48:502399 ->GetRenderWidgetHost();
2400 }
2401
paulmeyerfeafc2d2017-04-25 21:46:402402 // If the focused WebContents is a guest WebContents, then get the focused
2403 // frame in the embedder WebContents instead.
2404 FrameTreeNode* focused_frame = nullptr;
2405 if (focused_contents->browser_plugin_guest_ &&
2406 !GuestMode::IsCrossProcessFrameGuest(focused_contents)) {
2407 focused_frame = frame_tree_.GetFocusedFrame();
2408 } else {
2409 focused_frame = GetFocusedWebContents()->frame_tree_.GetFocusedFrame();
2410 }
lfg1453e412017-04-11 00:48:502411
alexmosc4d183e2015-11-06 00:46:522412 if (!focused_frame)
alexmosc4cbacb2015-11-21 01:29:222413 return receiving_widget;
alexmosc4d183e2015-11-06 00:46:522414
alexmos732ca3a2015-12-08 02:01:022415 // The view may be null if a subframe's renderer process has crashed while
2416 // the subframe has focus. Drop the event in that case. Do not give
2417 // it to the main frame, so that the user doesn't unexpectedly type into the
2418 // wrong frame if a focused subframe renderer crashes while they type.
2419 RenderWidgetHostView* view = focused_frame->current_frame_host()->GetView();
2420 if (!view)
2421 return nullptr;
2422
2423 return RenderWidgetHostImpl::From(view->GetRenderWidgetHost());
alexmosc4d183e2015-11-06 00:46:522424}
2425
avallee9993fca2016-11-17 06:16:502426RenderWidgetHostImpl* WebContentsImpl::GetRenderWidgetHostWithPageFocus() {
lfg1453e412017-04-11 00:48:502427 WebContentsImpl* focused_web_contents = GetFocusedWebContents();
2428
2429 if (focused_web_contents->ShowingInterstitialPage()) {
2430 return static_cast<RenderFrameHostImpl*>(
creisadca9862017-06-16 19:12:112431 focused_web_contents->interstitial_page_->GetMainFrame())
lfg1453e412017-04-11 00:48:502432 ->GetRenderWidgetHost();
2433 }
Alex Valléedc80ae2ed2017-09-06 21:55:442434 if (!GuestMode::IsCrossProcessFrameGuest(focused_web_contents) &&
2435 focused_web_contents->browser_plugin_guest_) {
2436 // If this is a guest, we need to be controlled by our embedder.
2437 return focused_web_contents->GetOuterWebContents()
2438 ->GetMainFrame()
2439 ->GetRenderWidgetHost();
2440 }
lfg1453e412017-04-11 00:48:502441
2442 return focused_web_contents->GetMainFrame()->GetRenderWidgetHost();
avallee9993fca2016-11-17 06:16:502443}
2444
Dave Tapuska3ed44192018-05-01 18:53:302445void WebContentsImpl::EnterFullscreenMode(
2446 const GURL& origin,
2447 const blink::WebFullscreenOptions& options) {
mlamouri7a78d6fd2015-01-17 13:23:532448 // This method is being called to enter renderer-initiated fullscreen mode.
2449 // Make sure any existing fullscreen widget is shut down first.
[email protected]4aebbca2013-09-17 22:26:492450 RenderWidgetHostView* const widget_view = GetFullscreenRenderWidgetHostView();
avi85a4cef2015-12-18 20:13:372451 if (widget_view) {
2452 RenderWidgetHostImpl::From(widget_view->GetRenderWidgetHost())
2453 ->ShutdownAndDestroyWidget(true);
2454 }
[email protected]4aebbca2013-09-17 22:26:492455
Joe Downing13dd76b2018-04-09 18:32:152456 if (delegate_) {
Dave Tapuska3ed44192018-05-01 18:53:302457 delegate_->EnterFullscreenModeForTab(this, origin, options);
mlamouri7a78d6fd2015-01-17 13:23:532458
Joe Downing13dd76b2018-04-09 18:32:152459 if (keyboard_lock_widget_)
2460 delegate_->RequestKeyboardLock(this, esc_key_locked_);
2461 }
2462
ericwilligersde386342016-10-19 02:35:092463 for (auto& observer : observers_)
2464 observer.DidToggleFullscreenModeForTab(IsFullscreenForCurrentTab(), false);
mlamouri7a78d6fd2015-01-17 13:23:532465}
2466
bokanece34a82016-01-28 19:49:462467void WebContentsImpl::ExitFullscreenMode(bool will_cause_resize) {
mlamouri7a78d6fd2015-01-17 13:23:532468 // This method is being called to leave renderer-initiated fullscreen mode.
2469 // Make sure any existing fullscreen widget is shut down first.
2470 RenderWidgetHostView* const widget_view = GetFullscreenRenderWidgetHostView();
avi85a4cef2015-12-18 20:13:372471 if (widget_view) {
2472 RenderWidgetHostImpl::From(widget_view->GetRenderWidgetHost())
2473 ->ShutdownAndDestroyWidget(true);
2474 }
mlamouri7a78d6fd2015-01-17 13:23:532475
Joe Downing13dd76b2018-04-09 18:32:152476 if (delegate_) {
mlamouri7a78d6fd2015-01-17 13:23:532477 delegate_->ExitFullscreenModeForTab(this);
[email protected]657c8e02014-03-19 19:18:372478
Joe Downing13dd76b2018-04-09 18:32:152479 if (keyboard_lock_widget_)
2480 delegate_->CancelKeyboardLockRequest(this);
2481 }
2482
bokanece34a82016-01-28 19:49:462483 // The fullscreen state is communicated to the renderer through a resize
2484 // message. If the change in fullscreen state doesn't cause a view resize
2485 // then we must ensure web contents exit the fullscreen state by explicitly
2486 // sending a resize message. This is required for the situation of the browser
2487 // moving the view into a "browser fullscreen" state and then the contents
2488 // entering "tab fullscreen". Exiting the contents "tab fullscreen" then won't
2489 // have the side effect of the view resizing, hence the explicit call here is
2490 // required.
2491 if (!will_cause_resize) {
2492 if (RenderWidgetHostView* rwhv = GetRenderWidgetHostView()) {
2493 if (RenderWidgetHost* render_widget_host = rwhv->GetRenderWidgetHost())
Fady Samuel0b911822018-04-25 13:22:162494 render_widget_host->SynchronizeVisualProperties();
bokanece34a82016-01-28 19:49:462495 }
scheib3dcb6c932015-02-23 10:55:582496 }
2497
Arthur Sonzogni929d29f2018-09-17 14:19:442498 current_fullscreen_frame_ = nullptr;
Becca Hughese9e27952018-06-25 17:08:272499
ericwilligersde386342016-10-19 02:35:092500 for (auto& observer : observers_) {
2501 observer.DidToggleFullscreenModeForTab(IsFullscreenForCurrentTab(),
2502 will_cause_resize);
2503 }
Becca Hughesd11d6502018-07-31 17:01:282504
2505 if (display_cutout_host_impl_)
2506 display_cutout_host_impl_->DidExitFullscreen();
[email protected]8a5e0ca2011-08-25 06:30:472507}
2508
Becca Hughesfd5d8f892018-06-14 18:23:362509void WebContentsImpl::FullscreenStateChanged(RenderFrameHost* rfh,
2510 bool is_fullscreen) {
Arthur Sonzogni929d29f2018-09-17 14:19:442511 RenderFrameHostImpl* frame = static_cast<RenderFrameHostImpl*>(rfh);
Becca Hughesfd5d8f892018-06-14 18:23:362512
2513 if (is_fullscreen) {
Jan Wilken Dörrie531be7ca2019-06-07 10:05:572514 if (!base::Contains(fullscreen_frames_, frame)) {
Arthur Sonzogni929d29f2018-09-17 14:19:442515 fullscreen_frames_.insert(frame);
2516 FullscreenFrameSetUpdated();
Becca Hughesfd5d8f892018-06-14 18:23:362517 }
Arthur Sonzogni929d29f2018-09-17 14:19:442518 return;
2519 }
Becca Hughesfd5d8f892018-06-14 18:23:362520
Arthur Sonzogni929d29f2018-09-17 14:19:442521 // If |frame| is no longer in fullscreen, remove it and any descendants.
2522 // See https://fullscreen.spec.whatwg.org.
Arthur Sonzogni969a794e2018-10-11 08:56:092523 size_t size_before_deletion = fullscreen_frames_.size();
2524 base::EraseIf(fullscreen_frames_, [&](RenderFrameHostImpl* current) {
2525 return (current == frame || current->IsDescendantOf(frame));
Arthur Sonzogni929d29f2018-09-17 14:19:442526 });
2527
Arthur Sonzogni969a794e2018-10-11 08:56:092528 if (size_before_deletion != fullscreen_frames_.size())
Arthur Sonzogni929d29f2018-09-17 14:19:442529 FullscreenFrameSetUpdated();
2530}
2531
2532void WebContentsImpl::FullscreenFrameSetUpdated() {
2533 if (fullscreen_frames_.empty()) {
2534 current_fullscreen_frame_ = nullptr;
2535 return;
Becca Hughesfd5d8f892018-06-14 18:23:362536 }
2537
Arthur Sonzogni929d29f2018-09-17 14:19:442538 // Find the current fullscreen frame and call the observers.
2539 // If frame A is fullscreen, then frame B goes into inner fullscreen, then B
2540 // exits fullscreen - that will result in A being fullscreen.
2541 RenderFrameHostImpl* new_fullscreen_frame = *std::max_element(
2542 fullscreen_frames_.begin(), fullscreen_frames_.end(), FrameCompareDepth);
Becca Hughesfd5d8f892018-06-14 18:23:362543
Arthur Sonzogni929d29f2018-09-17 14:19:442544 // If we have already notified observers about this frame then we should not
2545 // fire the observers again.
2546 if (new_fullscreen_frame == current_fullscreen_frame_)
2547 return;
2548 current_fullscreen_frame_ = new_fullscreen_frame;
Becca Hughesfd5d8f892018-06-14 18:23:362549
Arthur Sonzogni929d29f2018-09-17 14:19:442550 for (auto& observer : observers_)
2551 observer.DidAcquireFullscreen(new_fullscreen_frame);
Becca Hughesfd5d8f892018-06-14 18:23:362552
Arthur Sonzogni929d29f2018-09-17 14:19:442553 if (display_cutout_host_impl_)
2554 display_cutout_host_impl_->DidAcquireFullscreen(new_fullscreen_frame);
Becca Hughesfd5d8f892018-06-14 18:23:362555}
2556
Clark DuValla0e42992018-09-17 17:00:112557#if defined(OS_ANDROID)
2558void WebContentsImpl::UpdateUserGestureCarryoverInfo() {
2559 if (delegate_)
2560 delegate_->UpdateUserGestureCarryoverInfo(this);
2561}
2562#endif
2563
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:412564bool WebContentsImpl::IsFullscreenForCurrentTab() {
[email protected]199bba6e2012-04-04 16:19:382565 return delegate_ ? delegate_->IsFullscreenForTabOrPending(this) : false;
[email protected]5d5f7af2011-10-01 01:38:122566}
2567
Malay Keshav87c42c02019-01-15 08:37:472568bool WebContentsImpl::ShouldShowStaleContentOnEviction() {
2569 return GetDelegate() && GetDelegate()->ShouldShowStaleContentOnEviction(this);
2570}
2571
billorr21c005d2016-11-17 03:57:042572bool WebContentsImpl::IsFullscreen() {
2573 return IsFullscreenForCurrentTab();
2574}
2575
avibf58fa0c2015-11-11 01:45:522576blink::WebDisplayMode WebContentsImpl::GetDisplayMode(
2577 RenderWidgetHostImpl* render_widget_host) const {
2578 if (!RenderViewHostImpl::From(render_widget_host))
Blink Reformat1c4d759e2017-04-09 16:34:542579 return blink::kWebDisplayModeBrowser;
avibf58fa0c2015-11-11 01:45:522580
mikhail.pozdnyakovc0e251b2015-04-15 06:51:122581 return delegate_ ? delegate_->GetDisplayMode(this)
Blink Reformat1c4d759e2017-04-09 16:34:542582 : blink::kWebDisplayModeBrowser;
mikhail.pozdnyakovc0e251b2015-04-15 06:51:122583}
2584
avic3aa8422015-11-09 20:57:222585void WebContentsImpl::RequestToLockMouse(
2586 RenderWidgetHostImpl* render_widget_host,
2587 bool user_gesture,
lfgad8244352016-07-13 16:55:512588 bool last_unlocked_by_target,
2589 bool privileged) {
lfgf0cd46e2017-01-04 00:05:232590 for (WebContentsImpl* current = this; current;
2591 current = current->GetOuterWebContents()) {
2592 if (current->mouse_lock_widget_) {
2593 render_widget_host->GotResponseToLockMouseRequest(false);
2594 return;
2595 }
[email protected]e9621112011-10-17 05:38:372596 }
avic3aa8422015-11-09 20:57:222597
lfgad8244352016-07-13 16:55:512598 if (privileged) {
lfgf0cd46e2017-01-04 00:05:232599 DCHECK(!GetOuterWebContents());
lfgad8244352016-07-13 16:55:512600 mouse_lock_widget_ = render_widget_host;
2601 render_widget_host->GotResponseToLockMouseRequest(true);
2602 return;
2603 }
2604
lfgbee1e0a2016-06-08 21:24:212605 bool widget_in_frame_tree = false;
2606 for (FrameTreeNode* node : frame_tree_.Nodes()) {
2607 if (node->current_frame_host()->GetRenderWidgetHost() ==
2608 render_widget_host) {
2609 widget_in_frame_tree = true;
2610 break;
2611 }
2612 }
2613
2614 if (widget_in_frame_tree && delegate_) {
lfgf0cd46e2017-01-04 00:05:232615 for (WebContentsImpl* current = this; current;
2616 current = current->GetOuterWebContents()) {
2617 current->mouse_lock_widget_ = render_widget_host;
2618 }
2619
avic3aa8422015-11-09 20:57:222620 delegate_->RequestToLockMouse(this, user_gesture, last_unlocked_by_target);
lfgbee1e0a2016-06-08 21:24:212621 } else {
2622 render_widget_host->GotResponseToLockMouseRequest(false);
2623 }
[email protected]e9621112011-10-17 05:38:372624}
2625
avi3e4b8512015-11-11 01:55:492626void WebContentsImpl::LostMouseLock(RenderWidgetHostImpl* render_widget_host) {
lfgbee1e0a2016-06-08 21:24:212627 CHECK(mouse_lock_widget_);
lfgf0cd46e2017-01-04 00:05:232628
2629 if (mouse_lock_widget_->delegate()->GetAsWebContents() != this)
2630 return mouse_lock_widget_->delegate()->LostMouseLock(render_widget_host);
2631
lfgbee1e0a2016-06-08 21:24:212632 mouse_lock_widget_->SendMouseLockLost();
lfgf0cd46e2017-01-04 00:05:232633 for (WebContentsImpl* current = this; current;
2634 current = current->GetOuterWebContents()) {
2635 current->mouse_lock_widget_ = nullptr;
2636 }
avi3e4b8512015-11-11 01:55:492637
[email protected]e9621112011-10-17 05:38:372638 if (delegate_)
2639 delegate_->LostMouseLock();
2640}
2641
lfgad8244352016-07-13 16:55:512642bool WebContentsImpl::HasMouseLock(RenderWidgetHostImpl* render_widget_host) {
2643 // To verify if the mouse is locked, the mouse_lock_widget_ needs to be
2644 // assigned to the widget that requested the mouse lock, and the top-level
2645 // platform RenderWidgetHostView needs to hold the mouse lock from the OS.
Lucas Gadania0ea0172018-09-20 18:31:372646 auto* widget_host = GetTopLevelRenderWidgetHostView();
2647 return mouse_lock_widget_ == render_widget_host && widget_host &&
2648 widget_host->IsMouseLocked();
lfgad8244352016-07-13 16:55:512649}
2650
lfg84763c92017-02-16 18:55:152651RenderWidgetHostImpl* WebContentsImpl::GetMouseLockWidget() {
Lucas Gadania0ea0172018-09-20 18:31:372652 auto* widget_host = GetTopLevelRenderWidgetHostView();
2653 if ((widget_host && widget_host->IsMouseLocked()) ||
lfge2176fb2017-03-26 00:45:472654 (GetFullscreenRenderWidgetHostView() &&
Lucas Gadania0ea0172018-09-20 18:31:372655 GetFullscreenRenderWidgetHostView()->IsMouseLocked())) {
lfg84763c92017-02-16 18:55:152656 return mouse_lock_widget_;
Lucas Gadania0ea0172018-09-20 18:31:372657 }
lfg84763c92017-02-16 18:55:152658
2659 return nullptr;
2660}
2661
Joe Downing192998b22018-03-22 15:51:362662bool WebContentsImpl::RequestKeyboardLock(
Joe Downing13dd76b2018-04-09 18:32:152663 RenderWidgetHostImpl* render_widget_host,
2664 bool esc_key_locked) {
Joe Downing192998b22018-03-22 15:51:362665 DCHECK(render_widget_host);
Joe Downing192998b22018-03-22 15:51:362666 if (render_widget_host->delegate()->GetAsWebContents() != this) {
2667 NOTREACHED();
2668 return false;
2669 }
2670
2671 // KeyboardLock is only supported when called by the top-level browsing
2672 // context and is not supported in embedded content scenarios.
2673 if (GetOuterWebContents())
2674 return false;
2675
Joe Downing13dd76b2018-04-09 18:32:152676 esc_key_locked_ = esc_key_locked;
Joe Downing192998b22018-03-22 15:51:362677 keyboard_lock_widget_ = render_widget_host;
2678
Joe Downing13dd76b2018-04-09 18:32:152679 if (delegate_)
2680 delegate_->RequestKeyboardLock(this, esc_key_locked_);
Joe Downing192998b22018-03-22 15:51:362681 return true;
2682}
2683
2684void WebContentsImpl::CancelKeyboardLock(
2685 RenderWidgetHostImpl* render_widget_host) {
2686 if (!keyboard_lock_widget_ || render_widget_host != keyboard_lock_widget_)
2687 return;
2688
2689 RenderWidgetHostImpl* old_keyboard_lock_widget = keyboard_lock_widget_;
2690 keyboard_lock_widget_ = nullptr;
2691
Joe Downing13dd76b2018-04-09 18:32:152692 if (delegate_)
2693 delegate_->CancelKeyboardLockRequest(this);
2694
Joe Downing192998b22018-03-22 15:51:362695 old_keyboard_lock_widget->CancelKeyboardLock();
2696}
2697
2698RenderWidgetHostImpl* WebContentsImpl::GetKeyboardLockWidget() {
2699 return keyboard_lock_widget_;
2700}
2701
Dave Tapuskac3344362019-02-20 17:54:282702void WebContentsImpl::OnRenderFrameProxyVisibilityChanged(
2703 blink::mojom::FrameVisibility visibility) {
2704 switch (visibility) {
2705 case blink::mojom::FrameVisibility::kRenderedInViewport:
2706 WasShown();
2707 break;
2708 case blink::mojom::FrameVisibility::kNotRendered:
2709 WasHidden();
2710 break;
2711 case blink::mojom::FrameVisibility::kRenderedOutOfViewport:
2712 WasOccluded();
2713 break;
2714 }
ekaramada4f42692016-02-11 19:48:372715}
2716
[email protected]bafe6cd2012-05-23 23:09:502717void WebContentsImpl::CreateNewWindow(
csharrison95f01e922017-04-24 18:52:352718 RenderFrameHost* opener,
nick5ae4d2d2017-01-06 01:18:352719 int32_t render_view_route_id,
dcheng3ce04b62015-10-26 23:30:552720 int32_t main_frame_route_id,
2721 int32_t main_frame_widget_route_id,
rockot5c478a72016-09-28 23:14:182722 const mojom::CreateNewWindowParams& params,
Sergey Kuznetsov32257a22019-02-11 20:26:502723 bool has_user_gesture,
[email protected]97714c82012-06-06 10:15:132724 SessionStorageNamespace* session_storage_namespace) {
nick5ae4d2d2017-01-06 01:18:352725 // We should have zero valid routing ids, or three valid routing IDs.
2726 DCHECK_EQ((render_view_route_id == MSG_ROUTING_NONE),
2727 (main_frame_route_id == MSG_ROUTING_NONE));
2728 DCHECK_EQ((render_view_route_id == MSG_ROUTING_NONE),
2729 (main_frame_widget_route_id == MSG_ROUTING_NONE));
csharrison95f01e922017-04-24 18:52:352730 DCHECK(opener);
nick5ae4d2d2017-01-06 01:18:352731
csharrison95f01e922017-04-24 18:52:352732 int render_process_id = opener->GetProcess()->GetID();
2733 SiteInstance* source_site_instance = opener->GetSiteInstance();
2734
2735 // The route IDs passed into this function can be trusted not to already
2736 // be in use; they were allocated by the RenderWidgetHelper by the caller.
nick5ae4d2d2017-01-06 01:18:352737 DCHECK(!RenderFrameHostImpl::FromID(render_process_id, main_frame_route_id));
2738
[email protected]bafe6cd2012-05-23 23:09:502739 // We usually create the new window in the same BrowsingInstance (group of
2740 // script-related windows), by passing in the current SiteInstance. However,
[email protected]c4538072013-03-18 02:17:552741 // if the opener is being suppressed (in a non-guest), we create a new
2742 // SiteInstance in its own BrowsingInstance.
[email protected]a24efc22014-05-26 15:50:252743 bool is_guest = BrowserPluginGuest::IsGuest(this);
[email protected]c4538072013-03-18 02:17:552744
[email protected]04cbd3d2013-12-04 04:58:202745 // If the opener is to be suppressed, the new window can be in any process.
2746 // Since routing ids are process specific, we must not have one passed in
2747 // as argument here.
nick5ae4d2d2017-01-06 01:18:352748 DCHECK(!params.opener_suppressed || render_view_route_id == MSG_ROUTING_NONE);
[email protected]04cbd3d2013-12-04 04:58:202749
[email protected]bafe6cd2012-05-23 23:09:502750 scoped_refptr<SiteInstance> site_instance =
alexmos4cf2aa32015-07-15 23:40:432751 params.opener_suppressed && !is_guest
2752 ? SiteInstance::CreateForURL(GetBrowserContext(), params.target_url)
2753 : source_site_instance;
[email protected]bafe6cd2012-05-23 23:09:502754
[email protected]d1198fd2012-08-13 22:50:192755 // We must assign the SessionStorageNamespace before calling Init().
[email protected]4c3a23582012-08-18 08:54:342756 //
2757 // http://crbug.com/142685
[email protected]fdac6ade2013-07-20 01:06:302758 const std::string& partition_id =
2759 GetContentClient()->browser()->
2760 GetStoragePartitionIdForSite(GetBrowserContext(),
2761 site_instance->GetSiteURL());
[email protected]fc72bb12013-06-02 21:13:462762 StoragePartition* partition = BrowserContext::GetStoragePartition(
2763 GetBrowserContext(), site_instance.get());
[email protected]5f2aa722013-08-07 16:59:412764 DOMStorageContextWrapper* dom_storage_context =
2765 static_cast<DOMStorageContextWrapper*>(partition->GetDOMStorageContext());
[email protected]d1198fd2012-08-13 22:50:192766 SessionStorageNamespaceImpl* session_storage_namespace_impl =
2767 static_cast<SessionStorageNamespaceImpl*>(session_storage_namespace);
2768 CHECK(session_storage_namespace_impl->IsFromContext(dom_storage_context));
[email protected]dd6730412013-08-14 15:03:372769
2770 if (delegate_ &&
dcheng3ce04b62015-10-26 23:30:552771 !delegate_->ShouldCreateWebContents(
lukasza6f8ac622017-06-06 03:10:202772 this, opener, source_site_instance, render_view_route_id,
2773 main_frame_route_id, main_frame_widget_route_id,
2774 params.window_container_type, opener->GetLastCommittedURL(),
2775 params.frame_name, params.target_url, partition_id,
2776 session_storage_namespace)) {
jambaaab3a2016-11-05 00:46:182777 // Note: even though we're not creating a WebContents here, it could have
2778 // been created by the embedder so ensure that the RenderFrameHost is
2779 // properly initialized.
csharrisona2280cd2016-02-03 23:21:152780 // It's safe to only target the frame because the render process will not
2781 // have a chance to create more frames at this point.
jambaaab3a2016-11-05 00:46:182782 RenderFrameHostImpl* rfh =
2783 RenderFrameHostImpl::FromID(render_process_id, main_frame_route_id);
nick5ae4d2d2017-01-06 01:18:352784 if (rfh) {
2785 DCHECK(rfh->IsRenderFrameLive());
jambaaab3a2016-11-05 00:46:182786 rfh->Init();
nick5ae4d2d2017-01-06 01:18:352787 }
[email protected]dd6730412013-08-14 15:03:372788 return;
2789 }
2790
2791 // Create the new web contents. This will automatically create the new
2792 // WebContentsView. In the future, we may want to create the view separately.
[email protected]fc72bb12013-06-02 21:13:462793 CreateParams create_params(GetBrowserContext(), site_instance.get());
nick5ae4d2d2017-01-06 01:18:352794 create_params.routing_id = render_view_route_id;
[email protected]227692c52013-05-31 22:43:042795 create_params.main_frame_routing_id = main_frame_route_id;
dcheng3ce04b62015-10-26 23:30:552796 create_params.main_frame_widget_routing_id = main_frame_widget_route_id;
nasko48321ca32015-07-02 20:44:122797 create_params.main_frame_name = params.frame_name;
alexmos4cf2aa32015-07-15 23:40:432798 create_params.opener_render_process_id = render_process_id;
csharrison95f01e922017-04-24 18:52:352799 create_params.opener_render_frame_id = opener->GetRoutingID();
[email protected]50d326e2014-05-20 17:59:062800 create_params.opener_suppressed = params.opener_suppressed;
nick3b04f322016-08-31 19:29:192801 if (params.disposition == WindowOpenDisposition::NEW_BACKGROUND_TAB)
[email protected]50d326e2014-05-20 17:59:062802 create_params.initially_hidden = true;
lfg269b702f2015-06-08 19:28:192803 create_params.renderer_initiated_creation =
2804 main_frame_route_id != MSG_ROUTING_NONE;
[email protected]50d326e2014-05-20 17:59:062805
erikchenbee5c9622018-04-27 19:30:252806 std::unique_ptr<WebContents> new_contents;
[email protected]c4538072013-03-18 02:17:552807 if (!is_guest) {
2808 create_params.context = view_->GetNativeView();
[email protected]fc2b46b2014-05-03 16:33:452809 create_params.initial_size = GetContainerBounds().size();
Erik Chenbb8e738e2018-04-28 14:10:432810 new_contents = WebContents::Create(create_params);
[email protected]4858e432014-06-23 18:14:172811 } else {
erikchenbee5c9622018-04-27 19:30:252812 new_contents = base::WrapUnique(
2813 GetBrowserPluginGuest()->CreateNewGuestWindow(create_params));
[email protected]c4538072013-03-18 02:17:552814 }
danakje6c01622018-10-05 19:39:262815 auto owning_contents_impl =
2816 base::WrapUnique(static_cast<WebContentsImpl*>(new_contents.release()));
2817 auto* new_contents_impl = owning_contents_impl.get();
2818
2819 new_contents_impl->GetController().SetSessionStorageNamespace(
erikchenbee5c9622018-04-27 19:30:252820 partition_id, session_storage_namespace);
[email protected]d1198fd2012-08-13 22:50:192821
alexmos646fec02015-07-25 00:11:492822 // If the new frame has a name, make sure any SiteInstances that can find
2823 // this named frame have proxies for it. Must be called after
2824 // SetSessionStorageNamespace, since this calls CreateRenderView, which uses
2825 // GetSessionStorageNamespace.
2826 if (!params.frame_name.empty())
danakje6c01622018-10-05 19:39:262827 new_contents_impl->GetRenderManager()->CreateProxiesForNewNamedFrame();
alexmos646fec02015-07-25 00:11:492828
[email protected]c4538072013-03-18 02:17:552829 // Save the window for later if we're not suppressing the opener (since it
[email protected]d70bea92013-04-05 04:23:342830 // will be shown immediately).
2831 if (!params.opener_suppressed) {
2832 if (!is_guest) {
danakje6c01622018-10-05 19:39:262833 WebContentsView* new_view = new_contents_impl->view_.get();
[email protected]bafe6cd2012-05-23 23:09:502834
[email protected]d70bea92013-04-05 04:23:342835 // TODO(brettw): It seems bogus that we have to call this function on the
2836 // newly created object and give it one of its own member variables.
avi3627ecac2015-10-16 17:40:432837 new_view->CreateViewForWidget(
danakje6c01622018-10-05 19:39:262838 new_contents_impl->GetRenderViewHost()->GetWidget(), false);
[email protected]d70bea92013-04-05 04:23:342839 }
[email protected]bafe6cd2012-05-23 23:09:502840 // Save the created window associated with the route so we can show it
2841 // later.
Albert J. Wongcb004632018-07-10 22:58:252842 //
2843 // TODO(ajwong): This should be keyed off the RenderFrame routing id or the
2844 // FrameTreeNode id instead of the routing id of the Widget for the main
2845 // frame. https://crbug.com/545684
nick5ae4d2d2017-01-06 01:18:352846 DCHECK_NE(MSG_ROUTING_NONE, main_frame_widget_route_id);
danakje6c01622018-10-05 19:39:262847 GlobalRoutingID id(render_process_id, main_frame_widget_route_id);
2848 pending_contents_[id] = std::move(owning_contents_impl);
2849 AddDestructionObserver(new_contents_impl);
[email protected]bafe6cd2012-05-23 23:09:502850 }
2851
2852 if (delegate_) {
Bryan McQuaded2b93c02017-07-14 22:26:312853 delegate_->WebContentsCreated(this, render_process_id,
2854 opener->GetRoutingID(), params.frame_name,
danakje6c01622018-10-05 19:39:262855 params.target_url, new_contents_impl);
[email protected]bafe6cd2012-05-23 23:09:502856 }
2857
Charlie Harrisonf60dd1c2018-08-15 21:15:412858 for (auto& observer : observers_) {
danakje6c01622018-10-05 19:39:262859 observer.DidOpenRequestedURL(new_contents_impl, opener, params.target_url,
Leon Han963dc182018-11-06 05:41:482860 params.referrer.To<Referrer>(),
2861 params.disposition, ui::PAGE_TRANSITION_LINK,
Charlie Harrisonf60dd1c2018-08-15 21:15:412862 false, // started_from_context_menu
2863 true); // renderer_initiated
pnolandaae574e2017-03-06 21:04:212864 }
2865
Avi Drissmanba3b1b342017-08-29 18:16:132866 // Any new WebContents opened while this WebContents is in fullscreen can be
2867 // used to confuse the user, so drop fullscreen.
Avi Drissmand18c51972018-08-15 18:32:092868 ForSecurityDropFullscreen();
Avi Drissmanba3b1b342017-08-29 18:16:132869
[email protected]bafe6cd2012-05-23 23:09:502870 if (params.opener_suppressed) {
2871 // When the opener is suppressed, the original renderer cannot access the
2872 // new window. As a result, we need to show and navigate the window here.
[email protected]eda238a12012-09-07 23:44:002873 bool was_blocked = false;
erikchenbee5c9622018-04-27 19:30:252874
[email protected]eda238a12012-09-07 23:44:002875 if (delegate_) {
danakje6c01622018-10-05 19:39:262876 base::WeakPtr<WebContentsImpl> weak_new_contents =
2877 new_contents_impl->weak_factory_.GetWeakPtr();
nick0d558482017-02-13 22:27:222878
danakje6c01622018-10-05 19:39:262879 gfx::Rect initial_rect; // Report an empty initial rect.
2880 delegate_->AddNewContents(this, std::move(owning_contents_impl),
erikchenbee5c9622018-04-27 19:30:252881 params.disposition, initial_rect,
Sergey Kuznetsov32257a22019-02-11 20:26:502882 has_user_gesture, &was_blocked);
danakje6c01622018-10-05 19:39:262883 // The delegate may delete |new_contents_impl| during AddNewContents().
nick0d558482017-02-13 22:27:222884 if (!weak_new_contents)
danakje6c01622018-10-05 19:39:262885 return;
[email protected]eda238a12012-09-07 23:44:002886 }
erikchenbee5c9622018-04-27 19:30:252887
[email protected]eda238a12012-09-07 23:44:002888 if (!was_blocked) {
Nasko Oskov83a8cf92018-10-19 14:58:562889 std::unique_ptr<NavigationController::LoadURLParams> load_params =
2890 std::make_unique<NavigationController::LoadURLParams>(
2891 params.target_url);
Nasko Oskov93e7c55c2018-12-19 01:59:292892 load_params->initiator_origin = opener->GetLastCommittedOrigin();
Leon Han963dc182018-11-06 05:41:482893 load_params->referrer = params.referrer.To<Referrer>();
Nasko Oskov83a8cf92018-10-19 14:58:562894 load_params->transition_type = ui::PAGE_TRANSITION_LINK;
2895 load_params->is_renderer_initiated = true;
Sergey Kuznetsov32257a22019-02-11 20:26:502896 load_params->has_user_gesture = has_user_gesture;
mariakhomenkoa4971c12015-07-21 19:04:372897
2898 if (delegate_ && !is_guest &&
2899 !delegate_->ShouldResumeRequestsForCreatedWindow()) {
Nasko Oskov83a8cf92018-10-19 14:58:562900 // We are in asynchronous add new contents path, delay navigation.
2901 DCHECK(!new_contents_impl->delayed_open_url_params_);
2902 new_contents_impl->delayed_load_url_params_ = std::move(load_params);
mariakhomenkoa4971c12015-07-21 19:04:372903 } else {
Nasko Oskov83a8cf92018-10-19 14:58:562904 new_contents_impl->controller_.LoadURLWithParams(*load_params.get());
Nasko Oskov28ebd5b2018-12-07 22:29:332905 if (!is_guest)
2906 new_contents_impl->Focus();
mariakhomenkoa4971c12015-07-21 19:04:372907 }
[email protected]eda238a12012-09-07 23:44:002908 }
[email protected]bafe6cd2012-05-23 23:09:502909 }
2910}
2911
avib7348942015-12-25 20:57:102912void WebContentsImpl::CreateNewWidget(int32_t render_process_id,
Albert J. Wongf6e13ed2018-09-18 15:25:472913 int32_t widget_route_id,
danakj829cdd142018-09-14 21:13:272914 mojom::WidgetPtr widget) {
Albert J. Wongf6e13ed2018-09-18 15:25:472915 CreateNewWidget(render_process_id, widget_route_id, /*is_fullscreen=*/false,
danakj829cdd142018-09-14 21:13:272916 std::move(widget));
[email protected]bafe6cd2012-05-23 23:09:502917}
2918
avib7348942015-12-25 20:57:102919void WebContentsImpl::CreateNewFullscreenWidget(int32_t render_process_id,
Albert J. Wongf6e13ed2018-09-18 15:25:472920 int32_t widget_route_id,
Dave Tapuska1bdf1832017-07-07 18:07:192921 mojom::WidgetPtr widget) {
Albert J. Wongf6e13ed2018-09-18 15:25:472922 CreateNewWidget(render_process_id, widget_route_id, /*is_fullscreen=*/true,
danakj829cdd142018-09-14 21:13:272923 std::move(widget));
[email protected]bafe6cd2012-05-23 23:09:502924}
2925
avib7348942015-12-25 20:57:102926void WebContentsImpl::CreateNewWidget(int32_t render_process_id,
2927 int32_t route_id,
[email protected]bafe6cd2012-05-23 23:09:502928 bool is_fullscreen,
danakj829cdd142018-09-14 21:13:272929 mojom::WidgetPtr widget) {
kenrba26d15092016-02-10 22:00:252930 RenderProcessHost* process = RenderProcessHost::FromID(render_process_id);
kenrbb46f86a2015-12-07 07:53:232931 // A message to create a new widget can only come from an active process for
[email protected]a8504022013-12-04 20:23:512932 // this WebContentsImpl instance. If any other process sends the request,
2933 // it is invalid and the process must be terminated.
dcheng57e39e22016-01-21 00:25:382934 if (!HasMatchingProcess(&frame_tree_, render_process_id)) {
Lukasz Anforowicz6f746282018-01-04 23:24:512935 ReceivedBadMessage(process, bad_message::WCI_NEW_WIDGET_PROCESS_MISMATCH);
[email protected]a8504022013-12-04 20:23:512936 return;
2937 }
2938
Dave Tapuska485aca92017-08-08 00:47:582939 RenderWidgetHostImpl* widget_host = new RenderWidgetHostImpl(
2940 this, process, route_id, std::move(widget), IsHidden());
[email protected]b24b68a2012-09-24 21:57:262941
[email protected]cfd80b02014-05-01 17:46:482942 RenderWidgetHostViewBase* widget_view =
2943 static_cast<RenderWidgetHostViewBase*>(
danakj0b246942018-09-17 21:30:272944 view_->CreateViewForChildWidget(widget_host));
[email protected]83918ec2013-01-10 15:37:192945 if (!widget_view)
2946 return;
danakj0b246942018-09-17 21:30:272947 // Fullscreen child widgets are frames, other child widgets are popups, and
2948 // popups should not get activated.
2949 if (!is_fullscreen)
2950 widget_view->SetWidgetType(WidgetType::kPopup);
[email protected]bafe6cd2012-05-23 23:09:502951 // Save the created widget associated with the route so we can show it later.
Lukasz Anforowicz09060bdf72018-08-23 15:53:172952 pending_widget_views_[GlobalRoutingID(render_process_id, route_id)] =
alexmosc2a8cec2016-05-23 22:19:532953 widget_view;
[email protected]bafe6cd2012-05-23 23:09:502954}
2955
alexmosc2a8cec2016-05-23 22:19:532956void WebContentsImpl::ShowCreatedWindow(int process_id,
nick5ae4d2d2017-01-06 01:18:352957 int main_frame_widget_route_id,
[email protected]bafe6cd2012-05-23 23:09:502958 WindowOpenDisposition disposition,
bokan107a47f2015-02-03 23:23:392959 const gfx::Rect& initial_rect,
[email protected]bafe6cd2012-05-23 23:09:502960 bool user_gesture) {
danakje6c01622018-10-05 19:39:262961 // This method is the renderer requesting an existing top level window to
2962 // show a new top level window that the renderer created. Each top level
2963 // window is associated with a WebContents. In this case it was created
2964 // earlier but showing it was deferred until the renderer requested for it
2965 // to be shown. We find that previously created WebContents here.
2966 // TODO(danakj): Why do we defer this show step until the renderer asks for it
2967 // when it will always do so. What needs to happen in the renderer before we
2968 // reach here?
2969 std::unique_ptr<WebContentsImpl> owned_created =
nick5ae4d2d2017-01-06 01:18:352970 GetCreatedWindow(process_id, main_frame_widget_route_id);
danakje6c01622018-10-05 19:39:262971 WebContentsImpl* created = owned_created.get();
2972 // The browser may have rejected the request to make a new window, or the
2973 // renderer could be sending an invalid route id. Ignore the request then.
2974 if (!created)
2975 return;
mariakhomenko44bdc4732015-04-29 01:55:382976
danakje6c01622018-10-05 19:39:262977 // This uses the delegate for the WebContents where the window was created
2978 // from, to control how to show the newly created window.
2979 WebContentsDelegate* delegate = GetDelegate();
nick5ae4d2d2017-01-06 01:18:352980
danakje6c01622018-10-05 19:39:262981 // The delegate can be null in tests, so we must check for it :(.
2982 if (delegate) {
2983 // Mark the web contents as pending resume, then immediately do
2984 // the resume if the delegate wants it.
2985 created->is_resume_pending_ = true;
2986 if (delegate->ShouldResumeRequestsForCreatedWindow())
2987 created->ResumeLoadingCreatedWebContents();
2988
2989 base::WeakPtr<WebContentsImpl> weak_created =
2990 created->weak_factory_.GetWeakPtr();
2991 delegate->AddNewContents(this, std::move(owned_created), disposition,
2992 initial_rect, user_gesture, nullptr);
2993 // The delegate may delete |created| during AddNewContents().
2994 if (!weak_created)
2995 return;
[email protected]eda238a12012-09-07 23:44:002996 }
danakje6c01622018-10-05 19:39:262997
2998 RenderWidgetHostImpl* rwh = created->GetMainFrame()->GetRenderWidgetHost();
2999 DCHECK_EQ(main_frame_widget_route_id, rwh->GetRoutingID());
3000 rwh->Send(new WidgetMsg_SetBounds_ACK(rwh->GetRoutingID()));
[email protected]bafe6cd2012-05-23 23:09:503001}
3002
alexmosc2a8cec2016-05-23 22:19:533003void WebContentsImpl::ShowCreatedWidget(int process_id,
Albert J. Wongf6e13ed2018-09-18 15:25:473004 int widget_route_id,
bokan107a47f2015-02-03 23:23:393005 const gfx::Rect& initial_rect) {
Albert J. Wongf6e13ed2018-09-18 15:25:473006 ShowCreatedWidget(process_id, widget_route_id, false, initial_rect);
[email protected]bafe6cd2012-05-23 23:09:503007}
3008
alexmosc2a8cec2016-05-23 22:19:533009void WebContentsImpl::ShowCreatedFullscreenWidget(int process_id,
Albert J. Wongf6e13ed2018-09-18 15:25:473010 int widget_route_id) {
3011 ShowCreatedWidget(process_id, widget_route_id, true, gfx::Rect());
[email protected]bafe6cd2012-05-23 23:09:503012}
3013
alexmosc2a8cec2016-05-23 22:19:533014void WebContentsImpl::ShowCreatedWidget(int process_id,
3015 int route_id,
[email protected]bafe6cd2012-05-23 23:09:503016 bool is_fullscreen,
bokan107a47f2015-02-03 23:23:393017 const gfx::Rect& initial_rect) {
[email protected]cfd80b02014-05-01 17:46:483018 RenderWidgetHostViewBase* widget_host_view =
alexmosc2a8cec2016-05-23 22:19:533019 static_cast<RenderWidgetHostViewBase*>(
3020 GetCreatedWidget(process_id, route_id));
[email protected]bafe6cd2012-05-23 23:09:503021 if (!widget_host_view)
3022 return;
[email protected]cfd80b02014-05-01 17:46:483023
Ivan Kotenkov2c0d2bb32017-11-01 15:41:283024 RenderWidgetHostView* view = nullptr;
lfgc740d4b22016-04-15 16:45:333025 if (GetOuterWebContents()) {
3026 view = GetOuterWebContents()->GetRenderWidgetHostView();
[email protected]cfd80b02014-05-01 17:46:483027 } else {
3028 view = GetRenderWidgetHostView();
3029 }
3030
danakj0b246942018-09-17 21:30:273031 // Fullscreen child widgets are frames, other child widgets are popups.
[email protected]4aebbca2013-09-17 22:26:493032 if (is_fullscreen) {
[email protected]d7f80ba2013-10-12 07:42:313033 DCHECK_EQ(MSG_ROUTING_NONE, fullscreen_widget_routing_id_);
[email protected]0b431992014-06-24 00:08:033034 view_->StoreFocus();
alexmosc9e76ec2016-05-16 22:59:373035 fullscreen_widget_process_id_ =
3036 widget_host_view->GetRenderWidgetHost()->GetProcess()->GetID();
[email protected]d7f80ba2013-10-12 07:42:313037 fullscreen_widget_routing_id_ = route_id;
[email protected]4aebbca2013-09-17 22:26:493038 if (delegate_ && delegate_->EmbedsFullscreenWidget()) {
3039 widget_host_view->InitAsChild(GetRenderWidgetHostView()->GetNativeView());
Dave Tapuska3ed44192018-05-01 18:53:303040 delegate_->EnterFullscreenModeForTab(this, GURL(),
3041 blink::WebFullscreenOptions());
[email protected]4aebbca2013-09-17 22:26:493042 } else {
[email protected]cfd80b02014-05-01 17:46:483043 widget_host_view->InitAsFullscreen(view);
[email protected]4aebbca2013-09-17 22:26:493044 }
ericwilligersde386342016-10-19 02:35:093045 for (auto& observer : observers_)
3046 observer.DidShowFullscreenWidget();
[email protected]4aebbca2013-09-17 22:26:493047 if (!widget_host_view->HasFocus())
3048 widget_host_view->Focus();
3049 } else {
bokan107a47f2015-02-03 23:23:393050 widget_host_view->InitAsPopup(view, initial_rect);
[email protected]4aebbca2013-09-17 22:26:493051 }
[email protected]89054502012-06-03 10:29:243052
yiyixcb1fbc4f2018-03-16 19:54:083053 RenderWidgetHostImpl* render_widget_host_impl = widget_host_view->host();
[email protected]89054502012-06-03 10:29:243054 render_widget_host_impl->Init();
[email protected]d7f80ba2013-10-12 07:42:313055 // Only allow privileged mouse lock for fullscreen render widget, which is
3056 // used to implement Pepper Flash fullscreen.
3057 render_widget_host_impl->set_allow_privileged_mouse_lock(is_fullscreen);
[email protected]bafe6cd2012-05-23 23:09:503058}
3059
danakje6c01622018-10-05 19:39:263060std::unique_ptr<WebContentsImpl> WebContentsImpl::GetCreatedWindow(
nick5ae4d2d2017-01-06 01:18:353061 int process_id,
3062 int main_frame_widget_route_id) {
Lukasz Anforowicz09060bdf72018-08-23 15:53:173063 auto key = GlobalRoutingID(process_id, main_frame_widget_route_id);
nick5ae4d2d2017-01-06 01:18:353064 auto iter = pending_contents_.find(key);
[email protected]bafe6cd2012-05-23 23:09:503065
3066 // Certain systems can block the creation of new windows. If we didn't succeed
3067 // in creating one, just return NULL.
alexmosc2a8cec2016-05-23 22:19:533068 if (iter == pending_contents_.end())
3069 return nullptr;
[email protected]bafe6cd2012-05-23 23:09:503070
danakje6c01622018-10-05 19:39:263071 std::unique_ptr<WebContentsImpl> new_contents = std::move(iter->second);
nick5ae4d2d2017-01-06 01:18:353072 pending_contents_.erase(key);
danakje6c01622018-10-05 19:39:263073 RemoveDestructionObserver(new_contents.get());
[email protected]bafe6cd2012-05-23 23:09:503074
[email protected]d70bea92013-04-05 04:23:343075 // Don't initialize the guest WebContents immediately.
danakje6c01622018-10-05 19:39:263076 if (BrowserPluginGuest::IsGuest(new_contents.get()))
[email protected]d70bea92013-04-05 04:23:343077 return new_contents;
3078
Lukasz Anforowicz5510ed652018-06-06 16:16:193079 if (!new_contents->GetMainFrame()->GetProcess()->IsInitializedAndNotDead() ||
nick5ae4d2d2017-01-06 01:18:353080 !new_contents->GetMainFrame()->GetView()) {
alexmosc2a8cec2016-05-23 22:19:533081 return nullptr;
nick5ae4d2d2017-01-06 01:18:353082 }
[email protected]bafe6cd2012-05-23 23:09:503083
[email protected]bafe6cd2012-05-23 23:09:503084 return new_contents;
3085}
3086
alexmosc2a8cec2016-05-23 22:19:533087RenderWidgetHostView* WebContentsImpl::GetCreatedWidget(int process_id,
3088 int route_id) {
Lukasz Anforowicz09060bdf72018-08-23 15:53:173089 auto iter = pending_widget_views_.find(GlobalRoutingID(process_id, route_id));
[email protected]bafe6cd2012-05-23 23:09:503090 if (iter == pending_widget_views_.end()) {
3091 DCHECK(false);
alexmosc2a8cec2016-05-23 22:19:533092 return nullptr;
[email protected]bafe6cd2012-05-23 23:09:503093 }
3094
3095 RenderWidgetHostView* widget_host_view = iter->second;
Lukasz Anforowicz09060bdf72018-08-23 15:53:173096 pending_widget_views_.erase(GlobalRoutingID(process_id, route_id));
[email protected]bafe6cd2012-05-23 23:09:503097
3098 RenderWidgetHost* widget_host = widget_host_view->GetRenderWidgetHost();
Lukasz Anforowicz5510ed652018-06-06 16:16:193099 if (!widget_host->GetProcess()->IsInitializedAndNotDead()) {
[email protected]bafe6cd2012-05-23 23:09:503100 // The view has gone away or the renderer crashed. Nothing to do.
alexmosc2a8cec2016-05-23 22:19:533101 return nullptr;
[email protected]bafe6cd2012-05-23 23:09:503102 }
3103
3104 return widget_host_view;
3105}
3106
[email protected]f13b4202012-06-12 23:53:233107void WebContentsImpl::RequestMediaAccessPermission(
[email protected]33662e52013-01-07 21:31:093108 const MediaStreamRequest& request,
Mark Pilgrim57499082018-06-12 12:38:303109 MediaResponseCallback callback) {
[email protected]d19b84b2014-03-14 11:52:373110 if (delegate_) {
Mark Pilgrim57499082018-06-12 12:38:303111 delegate_->RequestMediaAccessPermission(this, request, std::move(callback));
[email protected]d19b84b2014-03-14 11:52:373112 } else {
Antonio Gomes0d42960a2019-06-05 12:35:513113 std::move(callback).Run(
3114 blink::MediaStreamDevices(),
3115 blink::mojom::MediaStreamRequestResult::FAILED_DUE_TO_SHUTDOWN,
3116 std::unique_ptr<MediaStreamUI>());
[email protected]d19b84b2014-03-14 11:52:373117 }
[email protected]f13b4202012-06-12 23:53:233118}
3119
Chandan Padhia4b8bcb72017-09-12 16:41:043120bool WebContentsImpl::CheckMediaAccessPermission(
Raymes Khouryad7c24a12018-03-05 23:22:583121 RenderFrameHost* render_frame_host,
Chandan Padhia4b8bcb72017-09-12 16:41:043122 const url::Origin& security_origin,
Antonio Gomesc8b734b2019-06-05 18:22:163123 blink::mojom::MediaStreamType type) {
3124 DCHECK(type == blink::mojom::MediaStreamType::DEVICE_AUDIO_CAPTURE ||
3125 type == blink::mojom::MediaStreamType::DEVICE_VIDEO_CAPTURE);
Chandan Padhia4b8bcb72017-09-12 16:41:043126 return delegate_ && delegate_->CheckMediaAccessPermission(
Raymes Khouryad7c24a12018-03-05 23:22:583127 render_frame_host, security_origin.GetURL(), type);
grunell657d4d82014-09-18 00:09:433128}
3129
Guido Urdaneta73fa6632019-01-14 18:46:263130std::string WebContentsImpl::GetDefaultMediaDeviceID(
Antonio Gomesc8b734b2019-06-05 18:22:163131 blink::mojom::MediaStreamType type) {
guidou8a440b842017-01-30 13:58:433132 if (!delegate_)
3133 return std::string();
3134 return delegate_->GetDefaultMediaDeviceID(this, type);
3135}
3136
[email protected]cc9200432013-07-23 23:02:403137SessionStorageNamespace* WebContentsImpl::GetSessionStorageNamespace(
3138 SiteInstance* instance) {
3139 return controller_.GetSessionStorageNamespace(instance);
3140}
3141
[email protected]6de7fc482014-06-06 10:46:443142SessionStorageNamespaceMap WebContentsImpl::GetSessionStorageNamespaceMap() {
3143 return controller_.GetSessionStorageNamespaceMap();
3144}
3145
[email protected]9b159a52013-10-03 17:24:553146FrameTree* WebContentsImpl::GetFrameTree() {
3147 return &frame_tree_;
3148}
3149
aelias5252baa2016-04-10 01:18:023150bool WebContentsImpl::IsOverridingUserAgent() {
3151 return GetController().GetVisibleEntry() &&
3152 GetController().GetVisibleEntry()->GetIsOverridingUserAgent();
3153}
3154
avi85ee8362016-10-08 02:09:083155bool WebContentsImpl::IsJavaScriptDialogShowing() const {
3156 return is_showing_javascript_dialog_;
3157}
3158
avic031d392017-03-03 03:09:423159bool WebContentsImpl::ShouldIgnoreUnresponsiveRenderer() {
3160 // Ignore unresponsive renderers if the debugger is attached to them since the
3161 // unresponsiveness might be a result of the renderer sitting on a breakpoint.
3162 //
David Bienvenu525f8a02019-02-15 02:02:423163#ifdef OS_WIN
3164 // Check if a windows debugger is attached to the renderer process.
3165 base::ProcessHandle process_handle =
3166 GetMainFrame()->GetProcess()->GetProcess().Handle();
3167 BOOL debugger_present = FALSE;
3168 if (CheckRemoteDebuggerPresent(process_handle, &debugger_present) &&
3169 debugger_present)
3170 return true;
3171#endif // OS_WIN
3172
avic031d392017-03-03 03:09:423173 // TODO(pfeldman): Fix this to only return true if the renderer is *actually*
3174 // sitting on a breakpoint. https://crbug.com/684202
3175 return DevToolsAgentHost::IsDebuggerAttached(this);
3176}
3177
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:413178ui::AXMode WebContentsImpl::GetAccessibilityMode() {
[email protected]95640212014-07-26 18:14:303179 return accessibility_mode_;
3180}
3181
Dominic Mazzonia7b0edb22017-08-09 16:32:513182void WebContentsImpl::AccessibilityEventReceived(
Dominic Mazzoniccbaa9b2018-06-06 07:44:233183 const AXEventNotificationDetails& details) {
ericwilligersde386342016-10-19 02:35:093184 for (auto& observer : observers_)
Dominic Mazzonia7b0edb22017-08-09 16:32:513185 observer.AccessibilityEventReceived(details);
[email protected]31a71eaf2014-03-13 01:47:363186}
3187
dmazzoni2400c462016-08-23 15:07:133188void WebContentsImpl::AccessibilityLocationChangesReceived(
3189 const std::vector<AXLocationChangeNotificationDetails>& details) {
ericwilligersde386342016-10-19 02:35:093190 for (auto& observer : observers_)
Dominic Mazzonia7b0edb22017-08-09 16:32:513191 observer.AccessibilityLocationChangesReceived(details);
dmazzoni2400c462016-08-23 15:07:133192}
3193
James Wallace-Leeeafc94cb92018-07-23 21:35:093194base::string16 WebContentsImpl::DumpAccessibilityTree(bool internal) {
3195 auto* ax_mgr = GetOrCreateRootBrowserAccessibilityManager();
3196 DCHECK(ax_mgr);
3197 return AccessibilityTreeFormatter::DumpAccessibilityTreeFromManager(ax_mgr,
3198 internal);
3199}
3200
dmazzonia656928e2014-09-15 20:28:543201RenderFrameHost* WebContentsImpl::GetGuestByInstanceID(
fsamuel833ee7c2015-02-13 23:40:403202 RenderFrameHost* render_frame_host,
dmazzonia656928e2014-09-15 20:28:543203 int browser_plugin_instance_id) {
3204 BrowserPluginGuestManager* guest_manager =
3205 GetBrowserContext()->GetGuestManager();
fsamuel9cbc00a2015-04-17 20:30:203206 if (!guest_manager)
3207 return nullptr;
3208
dmazzonia656928e2014-09-15 20:28:543209 WebContents* guest = guest_manager->GetGuestByInstanceID(
fsamuel2e9413d2015-02-25 01:25:443210 render_frame_host->GetProcess()->GetID(), browser_plugin_instance_id);
dmazzonia656928e2014-09-15 20:28:543211 if (!guest)
fsamuel9cbc00a2015-04-17 20:30:203212 return nullptr;
3213
dmazzonia656928e2014-09-15 20:28:543214 return guest->GetMainFrame();
3215}
3216
Ke He7319dbe2017-11-09 05:54:443217device::mojom::GeolocationContext* WebContentsImpl::GetGeolocationContext() {
3218 if (geolocation_context_)
3219 return geolocation_context_.get();
3220
3221 auto request = mojo::MakeRequest(&geolocation_context_);
Ken Rockotfd03e682019-06-20 21:13:543222 service_manager::Connector* connector = GetSystemConnector();
3223 if (connector)
3224 connector->BindInterface(device::mojom::kServiceName, std::move(request));
Conley Owens6894c4f2017-07-10 19:29:133225 return geolocation_context_.get();
blundellc57b93f2014-10-29 13:19:573226}
3227
ke.he98b761e2017-05-09 05:59:173228device::mojom::WakeLockContext* WebContentsImpl::GetWakeLockContext() {
blundelld8cd72b2017-03-28 07:18:383229 if (!wake_lock_context_host_)
3230 wake_lock_context_host_.reset(new WakeLockContextHost(this));
blundelle75a8f92017-03-27 08:11:173231 return wake_lock_context_host_->GetWakeLockContext();
alogvinovf50445a2015-10-30 13:00:123232}
3233
blundellf5316fc2017-05-15 11:49:033234#if defined(OS_ANDROID)
Han Leonf6a2e5f2017-05-23 13:40:473235void WebContentsImpl::GetNFC(device::mojom::NFCRequest request) {
blundellf5316fc2017-05-15 11:49:033236 if (!nfc_host_)
3237 nfc_host_.reset(new NFCHost(this));
3238 nfc_host_->GetNFC(std::move(request));
3239}
3240#endif
3241
Evan Stade5e750ad02017-08-07 21:59:273242void WebContentsImpl::SetNotWaitingForResponse() {
3243 if (waiting_for_response_ == false)
3244 return;
3245
3246 waiting_for_response_ = false;
3247 if (delegate_)
3248 delegate_->LoadingStateChanged(this, is_load_to_different_document_);
Chris Hamiltonf82f2a702018-02-15 19:37:293249 for (auto& observer : observers_)
3250 observer.DidReceiveResponse();
Evan Stade5e750ad02017-08-07 21:59:273251}
3252
lfgbb9c28a2016-03-01 03:19:493253void WebContentsImpl::SendScreenRects() {
kenrbc52e1b4e2016-03-20 01:03:373254 for (FrameTreeNode* node : frame_tree_.Nodes()) {
kenrb0e8dc202016-05-30 19:51:403255 if (node->current_frame_host()->is_local_root())
kenrbc52e1b4e2016-03-20 01:03:373256 node->current_frame_host()->GetRenderWidgetHost()->SendScreenRects();
3257 }
lfgbb9c28a2016-03-01 03:19:493258
danakj4df8a9d02019-02-05 20:36:593259 // Interstitials have their own |frame_tree_|.
3260 if (interstitial_page_) {
3261 FrameTree* interstitial_frame_tree = interstitial_page_->GetFrameTree();
3262 for (FrameTreeNode* node : interstitial_frame_tree->Nodes()) {
3263 if (node->current_frame_host()->is_local_root())
3264 node->current_frame_host()->GetRenderWidgetHost()->SendScreenRects();
3265 }
lfgdb5c4ed2016-03-04 23:09:073266 }
3267
avallee9f43b012016-11-26 07:02:103268 if (browser_plugin_embedder_ && !is_being_destroyed_)
[email protected]a7568e62013-06-14 07:50:443269 browser_plugin_embedder_->DidSendScreenRects();
[email protected]32deec62013-05-15 23:55:043270}
3271
ekaramadadd882292016-06-08 15:22:563272TextInputManager* WebContentsImpl::GetTextInputManager() {
3273 if (GetOuterWebContents())
3274 return GetOuterWebContents()->GetTextInputManager();
3275
Ehsan Karamadc179dc02018-12-15 03:09:293276 if (!text_input_manager_ && !browser_plugin_guest_) {
Darren Shenca53d5f2018-05-15 04:56:013277 text_input_manager_.reset(new TextInputManager(
3278 GetBrowserContext() &&
3279 !GetBrowserContext()->IsOffTheRecord()) /* should_do_learning */);
3280 }
ekaramadadd882292016-06-08 15:22:563281
3282 return text_input_manager_.get();
3283}
3284
paulmeyer7f6f1d4f2016-11-15 00:00:273285bool WebContentsImpl::OnUpdateDragCursor() {
Lei Zhang6410ec1a2019-01-31 21:38:583286 return browser_plugin_embedder_ &&
3287 browser_plugin_embedder_->OnUpdateDragCursor();
paulmeyer7f6f1d4f2016-11-15 00:00:273288}
3289
kenrb11f213a2017-03-24 18:12:063290bool WebContentsImpl::IsWidgetForMainFrame(
3291 RenderWidgetHostImpl* render_widget_host) {
3292 return render_widget_host == GetMainFrame()->GetRenderWidgetHost();
3293}
3294
[email protected]95640212014-07-26 18:14:303295BrowserAccessibilityManager*
3296 WebContentsImpl::GetRootBrowserAccessibilityManager() {
Dominic Mazzoniefb5f9e32017-12-21 01:08:223297 RenderFrameHostImpl* rfh = static_cast<RenderFrameHostImpl*>(
3298 ShowingInterstitialPage() ? GetInterstitialPage()->GetMainFrame()
3299 : GetMainFrame());
creisc014b402015-04-23 16:41:453300 return rfh ? rfh->browser_accessibility_manager() : nullptr;
[email protected]95640212014-07-26 18:14:303301}
3302
3303BrowserAccessibilityManager*
3304 WebContentsImpl::GetOrCreateRootBrowserAccessibilityManager() {
Dominic Mazzoniefb5f9e32017-12-21 01:08:223305 RenderFrameHostImpl* rfh = static_cast<RenderFrameHostImpl*>(
3306 ShowingInterstitialPage() ? GetInterstitialPage()->GetMainFrame()
3307 : GetMainFrame());
creisc014b402015-04-23 16:41:453308 return rfh ? rfh->GetOrCreateBrowserAccessibilityManager() : nullptr;
[email protected]95640212014-07-26 18:14:303309}
3310
Dave Tapuskab336b922017-07-06 19:24:053311void WebContentsImpl::ExecuteEditCommand(
3312 const std::string& command,
3313 const base::Optional<base::string16>& value) {
3314 RenderFrameHostImpl* focused_frame = GetFocusedFrame();
3315 if (!focused_frame)
3316 return;
3317
3318 focused_frame->GetFrameInputHandler()->ExecuteEditCommand(command, value);
3319}
3320
mohsen7ab1ec16ec2015-07-02 18:26:233321void WebContentsImpl::MoveRangeSelectionExtent(const gfx::Point& extent) {
Dave Tapuskab336b922017-07-06 19:24:053322 RenderFrameHostImpl* focused_frame = GetFocusedFrame();
mohsen7ab1ec16ec2015-07-02 18:26:233323 if (!focused_frame)
3324 return;
3325
dtapuska9d46ef7d2017-05-26 19:06:063326 focused_frame->GetFrameInputHandler()->MoveRangeSelectionExtent(extent);
mohsen7ab1ec16ec2015-07-02 18:26:233327}
3328
3329void WebContentsImpl::SelectRange(const gfx::Point& base,
3330 const gfx::Point& extent) {
Dave Tapuskab336b922017-07-06 19:24:053331 RenderFrameHostImpl* focused_frame = GetFocusedFrame();
mohsen7ab1ec16ec2015-07-02 18:26:233332 if (!focused_frame)
3333 return;
3334
dtapuska9d46ef7d2017-05-26 19:06:063335 focused_frame->GetFrameInputHandler()->SelectRange(base, extent);
mohsen7ab1ec16ec2015-07-02 18:26:233336}
3337
Dave Tapuskab336b922017-07-06 19:24:053338void WebContentsImpl::MoveCaret(const gfx::Point& extent) {
3339 RenderFrameHostImpl* focused_frame = GetFocusedFrame();
3340 if (!focused_frame)
3341 return;
3342
3343 focused_frame->GetFrameInputHandler()->MoveCaret(extent);
3344}
3345
Shimi Zhang37deeb22017-09-28 00:59:013346void WebContentsImpl::AdjustSelectionByCharacterOffset(
3347 int start_adjust,
3348 int end_adjust,
3349 bool show_selection_menu) {
Dave Tapuskab336b922017-07-06 19:24:053350 RenderFrameHostImpl* focused_frame = GetFocusedFrame();
aurimasab0319022015-07-10 21:57:383351 if (!focused_frame)
3352 return;
3353
Shimi Zhang37deeb22017-09-28 00:59:013354 using blink::mojom::SelectionMenuBehavior;
dtapuska9d46ef7d2017-05-26 19:06:063355 focused_frame->GetFrameInputHandler()->AdjustSelectionByCharacterOffset(
Shimi Zhang37deeb22017-09-28 00:59:013356 start_adjust, end_adjust,
3357 show_selection_menu ? SelectionMenuBehavior::kShow
3358 : SelectionMenuBehavior::kHide);
aurimasab0319022015-07-10 21:57:383359}
3360
[email protected]b172aee2012-04-10 17:05:263361void WebContentsImpl::UpdatePreferredSize(const gfx::Size& pref_size) {
[email protected]222f5822014-02-05 23:40:493362 const gfx::Size old_size = GetPreferredSize();
[email protected]bcd2815602012-01-14 18:17:233363 preferred_size_ = pref_size;
[email protected]222f5822014-02-05 23:40:493364 OnPreferredSizeChanged(old_size);
[email protected]0548c5352011-09-07 00:33:333365}
3366
avic3aa8422015-11-09 20:57:223367void WebContentsImpl::ResizeDueToAutoResize(
3368 RenderWidgetHostImpl* render_widget_host,
Fady Samuel97947112018-05-05 16:24:543369 const gfx::Size& new_size) {
avic3aa8422015-11-09 20:57:223370 if (render_widget_host != GetRenderViewHost()->GetWidget())
3371 return;
3372
kenrbe4f91082017-06-02 20:32:083373 auto_resize_size_ = new_size;
3374
3375 // Out-of-process iframe visible viewport sizes usually come from the
3376 // top-level RenderWidgetHostView, but when auto-resize is enabled on the
3377 // top frame then that size is used instead.
3378 for (FrameTreeNode* node : frame_tree_.Nodes()) {
3379 if (node->current_frame_host()->is_local_root()) {
3380 RenderWidgetHostImpl* host =
3381 node->current_frame_host()->GetRenderWidgetHost();
3382 if (host != render_widget_host)
Fady Samuel0b911822018-04-25 13:22:163383 host->SynchronizeVisualProperties();
kenrbe4f91082017-06-02 20:32:083384 }
3385 }
3386
[email protected]61e2b3cc2012-03-02 16:13:343387 if (delegate_)
3388 delegate_->ResizeDueToAutoResize(this, new_size);
3389}
3390
kenrbe4f91082017-06-02 20:32:083391gfx::Size WebContentsImpl::GetAutoResizeSize() {
3392 return auto_resize_size_;
3393}
3394
3395void WebContentsImpl::ResetAutoResizeSize() {
3396 auto_resize_size_ = gfx::Size();
3397}
3398
Albert J. Wongeb28e7e2018-10-12 00:57:433399InputEventShim* WebContentsImpl::GetInputEventShim() const {
3400 // The only thing that intercepts text input and mouse lock is the
3401 // BrowserPluginGuest. Delegate to the BrowserPluginGuest logic for
3402 // whether or not it needs to shim these events.
3403 if (browser_plugin_guest_) {
3404 return browser_plugin_guest_->GetInputEventShim();
3405 }
3406 return nullptr;
3407}
3408
[email protected]b172aee2012-04-10 17:05:263409WebContents* WebContentsImpl::OpenURL(const OpenURLParams& params) {
Bo Liu300c6052018-06-12 04:46:073410 if (!delegate_) {
3411 // Embedder can delay setting a delegate on new WebContents with
3412 // WebContentsDelegate::ShouldResumeRequestsForCreatedWindow. In the mean
3413 // time, navigations, including the initial one, that goes through OpenURL
3414 // should be delayed until embedder is ready to resume loading.
3415 delayed_open_url_params_ = std::make_unique<OpenURLParams>(params);
Nasko Oskov83a8cf92018-10-19 14:58:563416
3417 // If there was a navigation deferred when creating the window through
3418 // CreateNewWindow, drop it in favor of this navigation.
3419 delayed_load_url_params_.reset();
3420
Ivan Kotenkov2c0d2bb32017-11-01 15:41:283421 return nullptr;
Bo Liu300c6052018-06-12 04:46:073422 }
[email protected]00c37fc2011-08-02 00:22:503423
[email protected]e5d549d2011-12-28 01:29:203424 WebContents* new_contents = delegate_->OpenURLFromTab(this, params);
pnoland488944652017-02-22 18:58:543425
3426 RenderFrameHost* source_render_frame_host = RenderFrameHost::FromID(
3427 params.source_render_process_id, params.source_render_frame_id);
3428
3429 if (source_render_frame_host && params.source_site_instance) {
3430 CHECK_EQ(source_render_frame_host->GetSiteInstance(),
3431 params.source_site_instance.get());
3432 }
3433
3434 if (new_contents && source_render_frame_host && new_contents != this) {
3435 for (auto& observer : observers_) {
Lukasz Anforowicz48c9f872018-08-01 00:58:413436 observer.DidOpenRequestedURL(
3437 new_contents, source_render_frame_host, params.url, params.referrer,
3438 params.disposition, params.transition,
3439 params.started_from_context_menu, params.is_renderer_initiated);
pnoland488944652017-02-22 18:58:543440 }
3441 }
3442
[email protected]e5d549d2011-12-28 01:29:203443 return new_contents;
[email protected]d5f942ba2008-09-26 19:30:343444}
3445
[email protected]a86c0e962013-12-17 17:10:393446void WebContentsImpl::RenderFrameForInterstitialPageCreated(
3447 RenderFrameHost* render_frame_host) {
ericwilligersde386342016-10-19 02:35:093448 for (auto& observer : observers_)
3449 observer.RenderFrameForInterstitialPageCreated(render_frame_host);
[email protected]ba45bfd2012-05-22 21:51:443450}
3451
[email protected]20ca0382013-02-28 19:50:073452void WebContentsImpl::AttachInterstitialPage(
3453 InterstitialPageImpl* interstitial_page) {
creisadca9862017-06-16 19:12:113454 DCHECK(!interstitial_page_ && interstitial_page);
3455 interstitial_page_ = interstitial_page;
[email protected]90fb08ed2013-09-24 17:43:293456
3457 // Cancel any visible dialogs so that they don't interfere with the
3458 // interstitial.
creis89a0f782015-05-27 16:13:173459 CancelActiveAndPendingDialogs();
[email protected]90fb08ed2013-09-24 17:43:293460
ericwilligersde386342016-10-19 02:35:093461 for (auto& observer : observers_)
3462 observer.DidAttachInterstitialPage();
clamy44e84ce2016-02-22 15:38:253463
3464 // Stop the throbber if needed while the interstitial page is shown.
3465 if (frame_tree_.IsLoading())
3466 LoadingStateChanged(true, true, nullptr);
lfg1453e412017-04-11 00:48:503467
3468 // Connect to outer WebContents if necessary.
3469 if (node_.OuterContentsFrameTreeNode()) {
3470 if (GetRenderManager()->GetProxyToOuterDelegate()) {
3471 DCHECK(
3472 static_cast<RenderWidgetHostViewBase*>(interstitial_page->GetView())
3473 ->IsRenderWidgetHostViewChildFrame());
3474 RenderWidgetHostViewChildFrame* view =
3475 static_cast<RenderWidgetHostViewChildFrame*>(
3476 interstitial_page->GetView());
3477 GetRenderManager()->SetRWHViewForInnerContents(view);
3478 }
3479 }
Bo Liu7c6779e92017-08-16 02:02:283480
Bo Liu168c8642017-08-28 18:26:023481#if defined(OS_ANDROID)
Bo Liu7c6779e92017-08-16 02:02:283482 // Update importance of the interstitial.
3483 static_cast<RenderFrameHostImpl*>(interstitial_page_->GetMainFrame())
3484 ->GetRenderWidgetHost()
3485 ->SetImportance(GetMainFrame()->GetRenderWidgetHost()->importance());
Bo Liu168c8642017-08-28 18:26:023486#endif
Dominic Mazzoniefb5f9e32017-12-21 01:08:223487
3488 if (accessibility_mode_.has_mode(ui::AXMode::kNativeAPIs)) {
3489 // Make sure that the main page's accessibility tree is hidden and the
3490 // interstitial gets focus.
3491 RenderFrameHostImpl* rfh =
3492 static_cast<RenderFrameHostImpl*>(GetMainFrame());
3493 if (rfh) {
3494 BrowserAccessibilityManager* accessibility_manager =
3495 rfh->browser_accessibility_manager();
3496 if (accessibility_manager)
3497 accessibility_manager->set_hidden_by_interstitial_page(true);
3498 }
3499 rfh = static_cast<RenderFrameHostImpl*>(
3500 GetInterstitialPage()->GetMainFrame());
3501 if (rfh) {
3502 BrowserAccessibilityManager* accessibility_manager =
3503 rfh->GetOrCreateBrowserAccessibilityManager();
3504 if (accessibility_manager)
3505 accessibility_manager->OnWindowFocused();
3506 }
3507 }
clamy44e84ce2016-02-22 15:38:253508}
3509
3510void WebContentsImpl::DidProceedOnInterstitial() {
3511 // The interstitial page should no longer be pausing the throbber.
creisadca9862017-06-16 19:12:113512 DCHECK(!(ShowingInterstitialPage() && interstitial_page_->pause_throbber()));
clamy44e84ce2016-02-22 15:38:253513
3514 // Restart the throbber now that the interstitial page no longer pauses it.
3515 if (ShowingInterstitialPage() && frame_tree_.IsLoading())
3516 LoadingStateChanged(true, true, nullptr);
[email protected]20ca0382013-02-28 19:50:073517}
3518
Shivani Sharma985474f2019-05-23 21:36:473519bool WebContentsImpl::HadInnerWebContents() {
3520 return had_inner_webcontents_;
3521}
3522
Lucas Furukawa Gadani7f1241d2017-07-12 01:45:043523void WebContentsImpl::DetachInterstitialPage(bool has_focus) {
Lucas Furukawa Gadani69b609192017-07-10 17:08:343524 bool interstitial_pausing_throbber =
3525 ShowingInterstitialPage() && interstitial_page_->pause_throbber();
3526 if (ShowingInterstitialPage())
3527 interstitial_page_ = nullptr;
Lucas Furukawa Gadani7f1241d2017-07-12 01:45:043528
Dominic Mazzoniefb5f9e32017-12-21 01:08:223529 // Make sure that the main page's accessibility tree is no longer
3530 // suppressed.
3531 RenderFrameHostImpl* rfh = GetMainFrame();
3532 if (rfh) {
3533 BrowserAccessibilityManager* accessibility_manager =
3534 rfh->browser_accessibility_manager();
3535 if (accessibility_manager)
3536 accessibility_manager->set_hidden_by_interstitial_page(false);
3537 }
3538
Lucas Furukawa Gadani7f1241d2017-07-12 01:45:043539 // If the focus was on the interstitial, let's keep it to the page.
3540 // (Note that in unit-tests the RVH may not have a view).
3541 if (has_focus && GetRenderViewHost()->GetWidget()->GetView())
3542 GetRenderViewHost()->GetWidget()->GetView()->Focus();
3543
Lucas Furukawa Gadani69b609192017-07-10 17:08:343544 for (auto& observer : observers_)
3545 observer.DidDetachInterstitialPage();
3546
3547 // Disconnect from outer WebContents if necessary. This must happen after the
3548 // interstitial page is cleared above, since the call to
3549 // SetRWHViewForInnerContents below may loop over all the
3550 // RenderWidgetHostViews in the tree (otherwise, including the now-deleted
3551 // view for the interstitial).
lfg1453e412017-04-11 00:48:503552 if (node_.OuterContentsFrameTreeNode()) {
3553 if (GetRenderManager()->GetProxyToOuterDelegate()) {
3554 DCHECK(static_cast<RenderWidgetHostViewBase*>(
3555 GetRenderManager()->current_frame_host()->GetView())
3556 ->IsRenderWidgetHostViewChildFrame());
3557 RenderWidgetHostViewChildFrame* view =
3558 static_cast<RenderWidgetHostViewChildFrame*>(
3559 GetRenderManager()->current_frame_host()->GetView());
3560 GetRenderManager()->SetRWHViewForInnerContents(view);
3561 }
3562 }
3563
clamy44e84ce2016-02-22 15:38:253564 // Restart the throbber if needed now that the interstitial page is going
3565 // away.
3566 if (interstitial_pausing_throbber && frame_tree_.IsLoading())
3567 LoadingStateChanged(true, true, nullptr);
[email protected]20ca0382013-02-28 19:50:073568}
3569
avi2b177592014-12-10 02:08:023570void WebContentsImpl::SetHistoryOffsetAndLength(int history_offset,
3571 int history_length) {
alexmos136fd6e62016-08-15 20:58:413572 SendPageMessage(new PageMsg_SetHistoryOffsetAndLength(
3573 MSG_ROUTING_NONE, history_offset, history_length));
avi2b177592014-12-10 02:08:023574}
3575
3576void WebContentsImpl::SetHistoryOffsetAndLengthForView(
3577 RenderViewHost* render_view_host,
3578 int history_offset,
3579 int history_length) {
alexmos136fd6e62016-08-15 20:58:413580 render_view_host->Send(new PageMsg_SetHistoryOffsetAndLength(
avi2b177592014-12-10 02:08:023581 render_view_host->GetRoutingID(), history_offset, history_length));
[email protected]796931a92011-08-10 01:32:143582}
3583
toyoshim24a4c7ab2016-04-05 09:24:143584void WebContentsImpl::ReloadFocusedFrame(bool bypass_cache) {
[email protected]1f3fc1d2014-04-03 14:50:173585 RenderFrameHost* focused_frame = GetFocusedFrame();
3586 if (!focused_frame)
3587 return;
3588
[email protected]959be4c2014-04-08 15:01:333589 focused_frame->Send(new FrameMsg_Reload(
toyoshim24a4c7ab2016-04-05 09:24:143590 focused_frame->GetRoutingID(), bypass_cache));
[email protected]959be4c2014-04-08 15:01:333591}
3592
megjablon3f5941072016-02-04 23:27:523593void WebContentsImpl::ReloadLoFiImages() {
3594 SendToAllFrames(new FrameMsg_ReloadLoFiImages(MSG_ROUTING_NONE));
Robert Ogden34e97962018-09-14 23:50:273595 for (auto& observer : observers_)
3596 observer.DidReloadLoFiImages();
megjablon3f5941072016-02-04 23:27:523597}
3598
Ryan Sturmc65dbe42018-05-31 20:37:043599std::vector<blink::mojom::PauseSubresourceLoadingHandlePtr>
3600WebContentsImpl::PauseSubresourceLoading() {
3601 std::vector<blink::mojom::PauseSubresourceLoadingHandlePtr> handles;
3602 for (RenderFrameHost* rfh : GetAllFrames()) {
3603 if (!rfh->IsRenderFrameLive())
3604 continue;
3605 handles.push_back(rfh->PauseSubresourceLoading());
3606 }
3607 return handles;
3608}
3609
[email protected]959be4c2014-04-08 15:01:333610void WebContentsImpl::Undo() {
Dave Tapuskab336b922017-07-06 19:24:053611 RenderFrameHostImpl* focused_frame = GetFocusedFrame();
[email protected]959be4c2014-04-08 15:01:333612 if (!focused_frame)
3613 return;
3614
dtapuska9d46ef7d2017-05-26 19:06:063615 focused_frame->GetFrameInputHandler()->Undo();
[email protected]959be4c2014-04-08 15:01:333616 RecordAction(base::UserMetricsAction("Undo"));
3617}
3618
3619void WebContentsImpl::Redo() {
Dave Tapuskab336b922017-07-06 19:24:053620 RenderFrameHostImpl* focused_frame = GetFocusedFrame();
[email protected]959be4c2014-04-08 15:01:333621 if (!focused_frame)
3622 return;
dtapuska9d46ef7d2017-05-26 19:06:063623 focused_frame->GetFrameInputHandler()->Redo();
[email protected]959be4c2014-04-08 15:01:333624 RecordAction(base::UserMetricsAction("Redo"));
3625}
3626
3627void WebContentsImpl::Cut() {
Dave Tapuskab336b922017-07-06 19:24:053628 RenderFrameHostImpl* focused_frame = GetFocusedFrame();
[email protected]959be4c2014-04-08 15:01:333629 if (!focused_frame)
3630 return;
3631
dtapuska9d46ef7d2017-05-26 19:06:063632 focused_frame->GetFrameInputHandler()->Cut();
[email protected]959be4c2014-04-08 15:01:333633 RecordAction(base::UserMetricsAction("Cut"));
3634}
3635
3636void WebContentsImpl::Copy() {
Dave Tapuskab336b922017-07-06 19:24:053637 RenderFrameHostImpl* focused_frame = GetFocusedFrame();
[email protected]959be4c2014-04-08 15:01:333638 if (!focused_frame)
3639 return;
3640
dtapuska9d46ef7d2017-05-26 19:06:063641 focused_frame->GetFrameInputHandler()->Copy();
[email protected]959be4c2014-04-08 15:01:333642 RecordAction(base::UserMetricsAction("Copy"));
3643}
3644
3645void WebContentsImpl::CopyToFindPboard() {
3646#if defined(OS_MACOSX)
Dave Tapuskab336b922017-07-06 19:24:053647 RenderFrameHostImpl* focused_frame = GetFocusedFrame();
[email protected]959be4c2014-04-08 15:01:333648 if (!focused_frame)
3649 return;
3650
3651 // Windows/Linux don't have the concept of a find pasteboard.
dtapuska9d46ef7d2017-05-26 19:06:063652 focused_frame->GetFrameInputHandler()->CopyToFindPboard();
[email protected]959be4c2014-04-08 15:01:333653 RecordAction(base::UserMetricsAction("CopyToFindPboard"));
3654#endif
3655}
3656
3657void WebContentsImpl::Paste() {
Dave Tapuskab336b922017-07-06 19:24:053658 RenderFrameHostImpl* focused_frame = GetFocusedFrame();
[email protected]959be4c2014-04-08 15:01:333659 if (!focused_frame)
3660 return;
3661
dtapuska9d46ef7d2017-05-26 19:06:063662 focused_frame->GetFrameInputHandler()->Paste();
Archana Simhaf67090c2019-05-06 23:43:233663 for (auto& observer : observers_)
3664 observer.OnPaste();
[email protected]959be4c2014-04-08 15:01:333665 RecordAction(base::UserMetricsAction("Paste"));
3666}
3667
3668void WebContentsImpl::PasteAndMatchStyle() {
Dave Tapuskab336b922017-07-06 19:24:053669 RenderFrameHostImpl* focused_frame = GetFocusedFrame();
[email protected]959be4c2014-04-08 15:01:333670 if (!focused_frame)
3671 return;
3672
dtapuska9d46ef7d2017-05-26 19:06:063673 focused_frame->GetFrameInputHandler()->PasteAndMatchStyle();
Archana Simhaf67090c2019-05-06 23:43:233674 for (auto& observer : observers_)
3675 observer.OnPaste();
[email protected]959be4c2014-04-08 15:01:333676 RecordAction(base::UserMetricsAction("PasteAndMatchStyle"));
3677}
3678
3679void WebContentsImpl::Delete() {
Dave Tapuskab336b922017-07-06 19:24:053680 RenderFrameHostImpl* focused_frame = GetFocusedFrame();
[email protected]959be4c2014-04-08 15:01:333681 if (!focused_frame)
3682 return;
3683
dtapuska9d46ef7d2017-05-26 19:06:063684 focused_frame->GetFrameInputHandler()->Delete();
[email protected]959be4c2014-04-08 15:01:333685 RecordAction(base::UserMetricsAction("DeleteSelection"));
3686}
3687
3688void WebContentsImpl::SelectAll() {
Dave Tapuskab336b922017-07-06 19:24:053689 RenderFrameHostImpl* focused_frame = GetFocusedFrame();
[email protected]959be4c2014-04-08 15:01:333690 if (!focused_frame)
3691 return;
3692
dtapuska9d46ef7d2017-05-26 19:06:063693 focused_frame->GetFrameInputHandler()->SelectAll();
[email protected]959be4c2014-04-08 15:01:333694 RecordAction(base::UserMetricsAction("SelectAll"));
3695}
3696
yabinh351e7ec2017-03-10 02:43:243697void WebContentsImpl::CollapseSelection() {
Dave Tapuskab336b922017-07-06 19:24:053698 RenderFrameHostImpl* focused_frame = GetFocusedFrame();
[email protected]959be4c2014-04-08 15:01:333699 if (!focused_frame)
3700 return;
3701
dtapuska9d46ef7d2017-05-26 19:06:063702 focused_frame->GetFrameInputHandler()->CollapseSelection();
[email protected]1f3fc1d2014-04-03 14:50:173703}
3704
3705void WebContentsImpl::Replace(const base::string16& word) {
Dave Tapuskab336b922017-07-06 19:24:053706 RenderFrameHostImpl* focused_frame = GetFocusedFrame();
[email protected]1f3fc1d2014-04-03 14:50:173707 if (!focused_frame)
3708 return;
3709
dtapuska9d46ef7d2017-05-26 19:06:063710 focused_frame->GetFrameInputHandler()->Replace(word);
[email protected]1f3fc1d2014-04-03 14:50:173711}
3712
3713void WebContentsImpl::ReplaceMisspelling(const base::string16& word) {
Dave Tapuskab336b922017-07-06 19:24:053714 RenderFrameHostImpl* focused_frame = GetFocusedFrame();
[email protected]1f3fc1d2014-04-03 14:50:173715 if (!focused_frame)
3716 return;
3717
dtapuska9d46ef7d2017-05-26 19:06:063718 focused_frame->GetFrameInputHandler()->ReplaceMisspelling(word);
[email protected]959be4c2014-04-08 15:01:333719}
3720
3721void WebContentsImpl::NotifyContextMenuClosed(
3722 const CustomContextMenuContext& context) {
3723 RenderFrameHost* focused_frame = GetFocusedFrame();
3724 if (!focused_frame)
3725 return;
3726
3727 focused_frame->Send(new FrameMsg_ContextMenuClosed(
3728 focused_frame->GetRoutingID(), context));
3729}
3730
3731void WebContentsImpl::ExecuteCustomContextMenuCommand(
3732 int action, const CustomContextMenuContext& context) {
3733 RenderFrameHost* focused_frame = GetFocusedFrame();
3734 if (!focused_frame)
3735 return;
3736
3737 focused_frame->Send(new FrameMsg_CustomContextMenuAction(
3738 focused_frame->GetRoutingID(), context, action));
[email protected]4fed3702014-04-01 09:08:003739}
3740
[email protected]fc2b46b2014-05-03 16:33:453741gfx::NativeView WebContentsImpl::GetNativeView() {
3742 return view_->GetNativeView();
3743}
3744
3745gfx::NativeView WebContentsImpl::GetContentNativeView() {
3746 return view_->GetContentNativeView();
3747}
3748
3749gfx::NativeWindow WebContentsImpl::GetTopLevelNativeWindow() {
3750 return view_->GetTopLevelNativeWindow();
3751}
3752
3753gfx::Rect WebContentsImpl::GetViewBounds() {
3754 return view_->GetViewBounds();
3755}
3756
3757gfx::Rect WebContentsImpl::GetContainerBounds() {
3758 gfx::Rect rv;
3759 view_->GetContainerBounds(&rv);
3760 return rv;
3761}
3762
3763DropData* WebContentsImpl::GetDropData() {
3764 return view_->GetDropData();
3765}
3766
3767void WebContentsImpl::Focus() {
miu9e14e492014-10-25 02:39:043768 view_->Focus();
[email protected]fc2b46b2014-05-03 16:33:453769}
3770
3771void WebContentsImpl::SetInitialFocus() {
miu9e14e492014-10-25 02:39:043772 view_->SetInitialFocus();
[email protected]fc2b46b2014-05-03 16:33:453773}
3774
3775void WebContentsImpl::StoreFocus() {
miu9e14e492014-10-25 02:39:043776 view_->StoreFocus();
[email protected]fc2b46b2014-05-03 16:33:453777}
3778
3779void WebContentsImpl::RestoreFocus() {
miu9e14e492014-10-25 02:39:043780 view_->RestoreFocus();
[email protected]fc2b46b2014-05-03 16:33:453781}
3782
[email protected]b172aee2012-04-10 17:05:263783void WebContentsImpl::FocusThroughTabTraversal(bool reverse) {
Maxim Podgorodskiya926c4ff2017-11-20 02:06:393784 view_->FocusThroughTabTraversal(reverse);
[email protected]96d185d2009-04-24 03:28:543785}
3786
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:413787bool WebContentsImpl::ShowingInterstitialPage() {
creisadca9862017-06-16 19:12:113788 return interstitial_page_ != nullptr;
[email protected]96d185d2009-04-24 03:28:543789}
3790
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:413791InterstitialPageImpl* WebContentsImpl::GetInterstitialPage() {
creisadca9862017-06-16 19:12:113792 return interstitial_page_;
[email protected]686493142011-07-15 21:47:223793}
3794
[email protected]b172aee2012-04-10 17:05:263795bool WebContentsImpl::IsSavable() {
[email protected]a53209b2012-01-20 16:48:163796 // WebKit creates Document object when MIME type is application/xhtml+xml,
3797 // so we also support this MIME type.
3798 return contents_mime_type_ == "text/html" ||
3799 contents_mime_type_ == "text/xml" ||
3800 contents_mime_type_ == "application/xhtml+xml" ||
3801 contents_mime_type_ == "text/plain" ||
3802 contents_mime_type_ == "text/css" ||
Kinuko Yasuda74702f92017-07-31 03:27:533803 blink::IsSupportedJavascriptMimeType(contents_mime_type_);
[email protected]a53209b2012-01-20 16:48:163804}
3805
[email protected]b172aee2012-04-10 17:05:263806void WebContentsImpl::OnSavePage() {
[email protected]c7dd2f62011-07-18 15:57:593807 // If we can not save the page, try to download it.
[email protected]a53209b2012-01-20 16:48:163808 if (!IsSavable()) {
Min Qinda0ed2062018-02-23 22:00:533809 download::RecordSavePackageEvent(
3810 download::SAVE_PACKAGE_DOWNLOAD_ON_NON_HTML);
nasko89ad7742015-03-05 22:14:193811 SaveFrame(GetLastCommittedURL(), Referrer());
[email protected]27678b2a2012-02-04 22:09:143812 return;
[email protected]c7dd2f62011-07-18 15:57:593813 }
3814
3815 Stop();
3816
3817 // Create the save package and possibly prompt the user for the name to save
3818 // the page as. The user prompt is an asynchronous operation that runs on
3819 // another thread.
3820 save_package_ = new SavePackage(this);
3821 save_package_->GetSaveInfo();
3822}
3823
3824// Used in automated testing to bypass prompting the user for file names.
3825// Instead, the names and paths are hard coded rather than running them through
3826// file name sanitation and extension / mime checking.
[email protected]2dec8ec2013-02-07 19:20:343827bool WebContentsImpl::SavePage(const base::FilePath& main_file,
3828 const base::FilePath& dir_path,
[email protected]8ff00d72012-10-23 19:12:213829 SavePageType save_type) {
[email protected]c7dd2f62011-07-18 15:57:593830 // Stop the page from navigating.
3831 Stop();
3832
3833 save_package_ = new SavePackage(this, save_type, main_file, dir_path);
[email protected]8ff00d72012-10-23 19:12:213834 return save_package_->Init(SavePackageDownloadCreatedCallback());
[email protected]c7dd2f62011-07-18 15:57:593835}
3836
[email protected]3c71576ce2013-07-23 02:00:013837void WebContentsImpl::SaveFrame(const GURL& url,
3838 const Referrer& referrer) {
Xing Liu10329bf2018-03-20 19:22:143839 SaveFrameWithHeaders(url, referrer, std::string(), base::string16());
kundaji6c7f9692015-03-09 18:00:373840}
3841
Xing Liu10329bf2018-03-20 19:22:143842void WebContentsImpl::SaveFrameWithHeaders(
3843 const GURL& url,
3844 const Referrer& referrer,
3845 const std::string& headers,
3846 const base::string16& suggested_filename) {
Lei Zhang13a42e42019-02-19 23:28:113847 // Check and see if the guest can handle this.
3848 if (delegate_) {
3849 WebContents* guest_web_contents = nullptr;
3850 if (browser_plugin_embedder_) {
3851 BrowserPluginGuest* guest = browser_plugin_embedder_->GetFullPageGuest();
3852 if (guest)
3853 guest_web_contents = guest->GetWebContents();
3854 } else if (browser_plugin_guest_) {
3855 guest_web_contents = this;
3856 }
3857
3858 if (guest_web_contents && delegate_->GuestSaveFrame(guest_web_contents))
3859 return;
3860 }
3861
nasko89ad7742015-03-05 22:14:193862 if (!GetLastCommittedURL().is_valid())
[email protected]3c71576ce2013-07-23 02:00:013863 return;
sammc92af61552014-11-19 23:27:403864 if (delegate_ && delegate_->SaveFrame(url, referrer))
3865 return;
3866
nasko89ad7742015-03-05 22:14:193867 // TODO(nasko): This check for main frame is incorrect and should be fixed
brettw760f7442016-05-23 21:19:223868 // by explicitly passing in which frame this method should target. This would
3869 // indicate whether it's the main frame, and also tell us the frame pointer
3870 // to use for routing.
nasko89ad7742015-03-05 22:14:193871 bool is_main_frame = (url == GetLastCommittedURL());
brettw760f7442016-05-23 21:19:223872 RenderFrameHost* frame_host = GetMainFrame();
[email protected]3c71576ce2013-07-23 02:00:013873
avib7348942015-12-25 20:57:103874 int64_t post_id = -1;
[email protected]3c71576ce2013-07-23 02:00:013875 if (is_main_frame) {
Lucas Furukawa Gadani5553a152019-01-08 18:55:573876 NavigationEntry* entry = controller_.GetLastCommittedEntry();
[email protected]3c71576ce2013-07-23 02:00:013877 if (entry)
3878 post_id = entry->GetPostID();
3879 }
Ramin Halavati24e21012017-07-10 12:56:063880 net::NetworkTrafficAnnotationTag traffic_annotation =
3881 net::DefineNetworkTrafficAnnotation("download_web_contents_frame", R"(
3882 semantics {
3883 sender: "Save Page Action"
3884 description:
3885 "Saves the given frame's URL to the local file system."
3886 trigger:
3887 "The user has triggered a save operation on the frame through a "
3888 "context menu or other mechanism."
3889 data: "None."
3890 destination: WEBSITE
3891 }
3892 policy {
Ramin Halavati3b979782017-07-21 11:40:263893 cookies_allowed: YES
Ramin Halavati24e21012017-07-10 12:56:063894 cookies_store: "user"
3895 setting:
3896 "This feature cannot be disabled by settings, but it's is only "
3897 "triggered by user request."
3898 policy_exception_justification: "Not implemented."
3899 })");
Min Qina904f3302018-02-13 23:33:343900 auto params = std::make_unique<download::DownloadUrlParameters>(
brettw760f7442016-05-23 21:19:223901 url, frame_host->GetProcess()->GetID(),
3902 frame_host->GetRenderViewHost()->GetRoutingID(),
Matt Menke6d93c3c2018-10-10 15:21:503903 frame_host->GetRoutingID(), traffic_annotation);
Min Qina904f3302018-02-13 23:33:343904 params->set_referrer(referrer.url);
3905 params->set_referrer_policy(
3906 Referrer::ReferrerPolicyForUrlRequest(referrer.policy));
[email protected]3c71576ce2013-07-23 02:00:013907 params->set_post_id(post_id);
[email protected]3c71576ce2013-07-23 02:00:013908 if (post_id >= 0)
3909 params->set_method("POST");
3910 params->set_prompt(true);
kundaji6c7f9692015-03-09 18:00:373911
3912 if (headers.empty()) {
3913 params->set_prefer_cache(true);
3914 } else {
Min Qina904f3302018-02-13 23:33:343915 for (download::DownloadUrlParameters::RequestHeadersNameValuePair
3916 key_value : ParseDownloadHeaders(headers)) {
Megan Jablonski2f6a4c52017-07-10 23:01:253917 params->add_request_header(key_value.first, key_value.second);
kundaji6c7f9692015-03-09 18:00:373918 }
3919 }
Xing Liu10329bf2018-03-20 19:22:143920 params->set_suggested_name(suggested_filename);
Min Qin0ca8e1ee2018-01-31 00:49:353921 params->set_download_source(download::DownloadSource::WEB_CONTENTS_API);
thestig859c78892017-05-15 21:17:063922 BrowserContext::GetDownloadManager(GetBrowserContext())
Ramin Halavati03efa732017-06-12 09:28:203923 ->DownloadUrl(std::move(params));
[email protected]3c71576ce2013-07-23 02:00:013924}
3925
[email protected]b172aee2012-04-10 17:05:263926void WebContentsImpl::GenerateMHTML(
dewittj6dc5747a2016-05-17 01:48:473927 const MHTMLGenerationParams& params,
Avi Drissman149b7832018-03-23 14:31:493928 base::OnceCallback<void(int64_t)> callback) {
3929 MHTMLGenerationManager::GetInstance()->SaveMHTML(this, params,
3930 std::move(callback));
[email protected]aa4f3972012-03-01 18:12:123931}
3932
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:413933const std::string& WebContentsImpl::GetContentsMimeType() {
[email protected]0bfbf882011-12-22 18:19:273934 return contents_mime_type_;
3935}
3936
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:413937bool WebContentsImpl::WillNotifyDisconnection() {
[email protected]0bfbf882011-12-22 18:19:273938 return notify_disconnection_;
3939}
3940
Leon Hanc819dc62019-01-28 04:30:193941blink::mojom::RendererPreferences* WebContentsImpl::GetMutableRendererPrefs() {
[email protected]0bfbf882011-12-22 18:19:273942 return &renderer_preferences_;
3943}
3944
[email protected]e35ccd52012-05-23 16:22:473945void WebContentsImpl::Close() {
3946 Close(GetRenderViewHost());
3947}
3948
Ella Ge80a52dce2017-11-15 18:01:523949void WebContentsImpl::DragSourceEndedAt(float client_x,
3950 float client_y,
3951 float screen_x,
3952 float screen_y,
Paul Meyer0c58c3712016-11-17 22:59:513953 blink::WebDragOperation operation,
3954 RenderWidgetHost* source_rwh) {
Lei Zhang6410ec1a2019-01-31 21:38:583955 if (browser_plugin_embedder_) {
paulmeyer8fc8ea92016-11-15 05:12:213956 browser_plugin_embedder_->DragSourceEndedAt(
3957 client_x, client_y, screen_x, screen_y, operation);
Lei Zhang6410ec1a2019-01-31 21:38:583958 }
Paul Meyer0c58c3712016-11-17 22:59:513959 if (source_rwh) {
Ella Ge80a52dce2017-11-15 18:01:523960 source_rwh->DragSourceEndedAt(gfx::PointF(client_x, client_y),
3961 gfx::PointF(screen_x, screen_y), operation);
Paul Meyer0c58c3712016-11-17 22:59:513962 }
[email protected]cf200a562013-05-03 16:24:293963}
3964
jam73f892642016-09-11 06:04:063965void WebContentsImpl::LoadStateChanged(
Charles Harrison911fa062018-03-06 21:01:463966 const std::string& host,
jam73f892642016-09-11 06:04:063967 const net::LoadStateWithParam& load_state,
3968 uint64_t upload_position,
3969 uint64_t upload_size) {
Charles Harrison911fa062018-03-06 21:01:463970 base::string16 host16 = url_formatter::IDNToUnicode(host);
Elly Fong-Jonescad9c3d12018-01-31 17:26:103971 // Drop no-op updates.
3972 if (load_state_.state == load_state.state &&
3973 load_state_.param == load_state.param &&
3974 upload_position_ == upload_position && upload_size_ == upload_size &&
Charles Harrison911fa062018-03-06 21:01:463975 load_state_host_ == host16) {
Elly Fong-Jonescad9c3d12018-01-31 17:26:103976 return;
3977 }
jam73f892642016-09-11 06:04:063978 load_state_ = load_state;
3979 upload_position_ = upload_position;
3980 upload_size_ = upload_size;
Charles Harrison911fa062018-03-06 21:01:463981 load_state_host_ = host16;
jam73f892642016-09-11 06:04:063982 if (load_state_.state == net::LOAD_STATE_READING_RESPONSE)
3983 SetNotWaitingForResponse();
3984 if (IsLoading()) {
3985 NotifyNavigationStateChanged(static_cast<InvalidateTypes>(
3986 INVALIDATE_TYPE_LOAD | INVALIDATE_TYPE_TAB));
3987 }
3988}
3989
Francois Doraye6161152018-03-27 22:05:373990void WebContentsImpl::SetVisibility(Visibility visibility) {
3991 const Visibility previous_visibility = visibility_;
3992 visibility_ = visibility;
3993
Francois Dorayfe4a1772018-02-17 04:17:093994 // Notify observers if the visibility changed or if WasShown() is being called
3995 // for the first time.
Francois Dorayfe4a1772018-02-17 04:17:093996 if (visibility != previous_visibility ||
3997 (visibility == Visibility::VISIBLE && !did_first_set_visible_)) {
3998 for (auto& observer : observers_)
3999 observer.OnVisibilityChanged(visibility);
4000 }
4001}
4002
Michael Thiessen896405db2017-07-20 17:52:324003void WebContentsImpl::NotifyWebContentsFocused(
4004 RenderWidgetHost* render_widget_host) {
ericwilligersde386342016-10-19 02:35:094005 for (auto& observer : observers_)
Michael Thiessen896405db2017-07-20 17:52:324006 observer.OnWebContentsFocused(render_widget_host);
calamity7fe55ce2015-04-10 03:59:374007}
4008
Michael Thiessen896405db2017-07-20 17:52:324009void WebContentsImpl::NotifyWebContentsLostFocus(
4010 RenderWidgetHost* render_widget_host) {
zijiehe3bee08e22017-05-17 04:14:464011 for (auto& observer : observers_)
Michael Thiessen896405db2017-07-20 17:52:324012 observer.OnWebContentsLostFocus(render_widget_host);
zijiehe3bee08e22017-05-17 04:14:464013}
4014
Paul Meyer0c58c3712016-11-17 22:59:514015void WebContentsImpl::SystemDragEnded(RenderWidgetHost* source_rwh) {
4016 if (source_rwh)
4017 source_rwh->DragSourceSystemDragEnded();
Lei Zhang6410ec1a2019-01-31 21:38:584018 if (browser_plugin_embedder_)
[email protected]cf200a562013-05-03 16:24:294019 browser_plugin_embedder_->SystemDragEnded();
[email protected]7813bd72011-02-05 02:19:344020}
4021
Daniel Cheng90196c82018-04-25 21:49:144022void WebContentsImpl::NavigatedByUser() {
Dominick Ng37a15052018-07-17 23:56:354023 SendUserGestureForResourceDispatchHost();
[email protected]e35ccd52012-05-23 16:22:474024}
4025
[email protected]b172aee2012-04-10 17:05:264026void WebContentsImpl::SetClosedByUserGesture(bool value) {
[email protected]0bfbf882011-12-22 18:19:274027 closed_by_user_gesture_ = value;
4028}
4029
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:414030bool WebContentsImpl::GetClosedByUserGesture() {
[email protected]0bfbf882011-12-22 18:19:274031 return closed_by_user_gesture_;
4032}
4033
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:414034int WebContentsImpl::GetMinimumZoomPercent() {
[email protected]0bfbf882011-12-22 18:19:274035 return minimum_zoom_percent_;
4036}
4037
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:414038int WebContentsImpl::GetMaximumZoomPercent() {
[email protected]0bfbf882011-12-22 18:19:274039 return maximum_zoom_percent_;
4040}
4041
mcnee432e47d2015-11-09 19:37:464042void WebContentsImpl::SetPageScale(float page_scale_factor) {
Lukasz Anforowiczf4357ca2017-09-07 01:43:324043 GetRenderViewHost()->Send(new ViewMsg_SetPageScale(
4044 GetRenderViewHost()->GetRoutingID(), page_scale_factor));
ccameronb7c1d6c2015-03-09 17:08:244045}
4046
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:414047gfx::Size WebContentsImpl::GetPreferredSize() {
Francois Doray24fc62c2017-12-11 17:27:334048 return IsBeingCaptured() ? preferred_size_for_capture_ : preferred_size_;
[email protected]bcd2815602012-01-14 18:17:234049}
4050
[email protected]b172aee2012-04-10 17:05:264051bool WebContentsImpl::GotResponseToLockMouseRequest(bool allowed) {
lfgf0cd46e2017-01-04 00:05:234052 if (!GuestMode::IsCrossProcessFrameGuest(GetWebContents()) &&
4053 GetBrowserPluginGuest())
[email protected]660f18e2014-05-15 20:53:054054 return GetBrowserPluginGuest()->LockMouse(allowed);
4055
lfgf0cd46e2017-01-04 00:05:234056 if (mouse_lock_widget_) {
4057 if (mouse_lock_widget_->delegate()->GetAsWebContents() != this) {
4058 return mouse_lock_widget_->delegate()
4059 ->GetAsWebContents()
4060 ->GotResponseToLockMouseRequest(allowed);
4061 }
lfgad8244352016-07-13 16:55:514062
lfgf0cd46e2017-01-04 00:05:234063 if (mouse_lock_widget_->GotResponseToLockMouseRequest(allowed))
4064 return true;
4065 }
4066
4067 for (WebContentsImpl* current = this; current;
4068 current = current->GetOuterWebContents()) {
4069 current->mouse_lock_widget_ = nullptr;
4070 }
4071
lfgbee1e0a2016-06-08 21:24:214072 return false;
[email protected]0bfbf882011-12-22 18:19:274073}
4074
Joe Downing13dd76b2018-04-09 18:32:154075bool WebContentsImpl::GotResponseToKeyboardLockRequest(bool allowed) {
4076 if (!keyboard_lock_widget_)
4077 return false;
4078
4079 if (keyboard_lock_widget_->delegate()->GetAsWebContents() != this) {
4080 NOTREACHED();
4081 return false;
4082 }
4083
4084 // KeyboardLock is only supported when called by the top-level browsing
4085 // context and is not supported in embedded content scenarios.
4086 if (GetOuterWebContents())
4087 return false;
4088
4089 keyboard_lock_widget_->GotResponseToKeyboardLockRequest(allowed);
4090 return true;
4091}
4092
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:414093bool WebContentsImpl::HasOpener() {
Ivan Kotenkov2c0d2bb32017-11-01 15:41:284094 return GetOpener() != nullptr;
[email protected]a0358d72012-03-09 14:06:504095}
4096
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:414097RenderFrameHostImpl* WebContentsImpl::GetOpener() {
alexmose201c7cd2015-06-10 17:14:214098 FrameTreeNode* opener_ftn = frame_tree_.root()->opener();
lukasza6f8ac622017-06-06 03:10:204099 return opener_ftn ? opener_ftn->current_frame_host() : nullptr;
jochen55ff3502014-12-18 20:52:574100}
4101
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:414102bool WebContentsImpl::HasOriginalOpener() {
Ivan Kotenkov2c0d2bb32017-11-01 15:41:284103 return GetOriginalOpener() != nullptr;
jochen6004a362017-02-04 00:11:404104}
4105
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:414106RenderFrameHostImpl* WebContentsImpl::GetOriginalOpener() {
jochen6004a362017-02-04 00:11:404107 FrameTreeNode* opener_ftn = frame_tree_.root()->original_opener();
lukasza6f8ac622017-06-06 03:10:204108 return opener_ftn ? opener_ftn->current_frame_host() : nullptr;
jochen6004a362017-02-04 00:11:404109}
4110
[email protected]cb805452013-05-22 15:16:214111void WebContentsImpl::DidChooseColorInColorChooser(SkColor color) {
Joel Hockey85b379d2017-12-11 10:42:134112 color_chooser_->DidChooseColorInColorChooser(color);
[email protected]da8543762012-03-20 08:52:204113}
4114
[email protected]cb805452013-05-22 15:16:214115void WebContentsImpl::DidEndColorChooser() {
Joel Hockey85b379d2017-12-11 10:42:134116 color_chooser_.reset();
[email protected]da8543762012-03-20 08:52:204117}
4118
halton.huoca2eabd2015-07-06 08:17:404119int WebContentsImpl::DownloadImage(
4120 const GURL& url,
4121 bool is_favicon,
4122 uint32_t max_bitmap_size,
4123 bool bypass_cache,
Avi Drissmana5a52dd2018-03-27 03:39:024124 WebContents::ImageDownloadCallback callback) {
amistry9f01b772015-07-29 01:54:554125 DCHECK_CURRENTLY_ON(BrowserThread::UI);
halton.huoca2eabd2015-07-06 08:17:404126 static int next_image_download_id = 0;
Gyuyoung Kime370bdb2019-06-28 00:31:524127 const blink::mojom::ImageDownloaderPtr& mojo_image_downloader =
halton.huoca2eabd2015-07-06 08:17:404128 GetMainFrame()->GetMojoImageDownloader();
amistry9f01b772015-07-29 01:54:554129 const int download_id = ++next_image_download_id;
4130 if (!mojo_image_downloader) {
4131 // If the renderer process is dead (i.e. crash, or memory pressure on
4132 // Android), the downloader service will be invalid. Pre-Mojo, this would
Avi Drissmana5a52dd2018-03-27 03:39:024133 // hang the callback indefinitely since the IPC would be dropped. Now,
amistry9f01b772015-07-29 01:54:554134 // respond with a 400 HTTP error code to indicate that something went wrong.
Eric Seckler8652dcd52018-09-20 10:42:284135 base::PostTaskWithTraits(
4136 FROM_HERE, {BrowserThread::UI},
tzike2aca992017-09-05 08:50:544137 base::BindOnce(&WebContentsImpl::OnDidDownloadImage,
Avi Drissmana5a52dd2018-03-27 03:39:024138 weak_factory_.GetWeakPtr(), std::move(callback),
4139 download_id, url, 400, std::vector<SkBitmap>(),
4140 std::vector<gfx::Size>()));
amistry9f01b772015-07-29 01:54:554141 return download_id;
4142 }
4143
halton.huoca2eabd2015-07-06 08:17:404144 mojo_image_downloader->DownloadImage(
sammcf6f4fcf2016-06-21 05:39:394145 url, is_favicon, max_bitmap_size, bypass_cache,
tzike2aca992017-09-05 08:50:544146 base::BindOnce(&WebContentsImpl::OnDidDownloadImage,
Avi Drissmana5a52dd2018-03-27 03:39:024147 weak_factory_.GetWeakPtr(), std::move(callback),
4148 download_id, url));
amistry9f01b772015-07-29 01:54:554149 return download_id;
[email protected]795c28972012-12-06 06:13:394150}
4151
[email protected]36ec24f2014-01-09 00:32:084152void WebContentsImpl::Find(int request_id,
4153 const base::string16& search_text,
Rakina Zata Amni3f77dff2018-09-08 16:19:434154 blink::mojom::FindOptionsPtr options) {
thestig6057a6b22015-11-12 23:01:334155 // Cowardly refuse to search for no text.
4156 if (search_text.empty()) {
4157 NOTREACHED();
4158 return;
4159 }
4160
Rakina Zata Amni3f77dff2018-09-08 16:19:434161 GetOrCreateFindRequestManager()->Find(request_id, search_text,
4162 std::move(options));
[email protected]36ec24f2014-01-09 00:32:084163}
4164
4165void WebContentsImpl::StopFinding(StopFindAction action) {
paulmeyerfeafc2d2017-04-25 21:46:404166 if (FindRequestManager* manager = GetFindRequestManager())
4167 manager->StopFinding(action);
[email protected]36ec24f2014-01-09 00:32:084168}
4169
Tommy Steimel18360512017-11-01 00:38:194170bool WebContentsImpl::WasEverAudible() {
4171 return was_ever_audible_;
4172}
4173
Avi Drissmana5a52dd2018-03-27 03:39:024174void WebContentsImpl::GetManifest(GetManifestCallback callback) {
4175 manifest_manager_host_->GetManifest(std::move(callback));
mlamouriefdca9d2014-09-16 16:55:404176}
4177
bokanece34a82016-01-28 19:49:464178void WebContentsImpl::ExitFullscreen(bool will_cause_resize) {
mlamouri7a78d6fd2015-01-17 13:23:534179 // Clean up related state and initiate the fullscreen exit.
avi3627ecac2015-10-16 17:40:434180 GetRenderViewHost()->GetWidget()->RejectMouseLockOrUnlockIfNecessary();
bokanece34a82016-01-28 19:49:464181 ExitFullscreenMode(will_cause_resize);
mlamouri7a78d6fd2015-01-17 13:23:534182}
4183
mariakhomenko44bdc4732015-04-29 01:55:384184void WebContentsImpl::ResumeLoadingCreatedWebContents() {
Nasko Oskov83a8cf92018-10-19 14:58:564185 if (delayed_load_url_params_.get()) {
4186 DCHECK(!delayed_open_url_params_);
4187 controller_.LoadURLWithParams(*delayed_load_url_params_.get());
4188 delayed_load_url_params_.reset(nullptr);
4189 return;
4190 }
4191
mariakhomenkoa4971c12015-07-21 19:04:374192 if (delayed_open_url_params_.get()) {
4193 OpenURL(*delayed_open_url_params_.get());
4194 delayed_open_url_params_.reset(nullptr);
4195 return;
4196 }
4197
mariakhomenko44bdc4732015-04-29 01:55:384198 // Resume blocked requests for both the RenderViewHost and RenderFrameHost.
4199 // TODO(brettw): It seems bogus to reach into here and initialize the host.
dfalcantarae6b7b46752015-07-10 18:14:164200 if (is_resume_pending_) {
4201 is_resume_pending_ = false;
avi0f1bbc9712015-11-17 02:58:134202 GetRenderViewHost()->GetWidget()->Init();
dfalcantarae6b7b46752015-07-10 18:14:164203 GetMainFrame()->Init();
4204 }
mariakhomenko44bdc4732015-04-29 01:55:384205}
4206
[email protected]b172aee2012-04-10 17:05:264207bool WebContentsImpl::FocusLocationBarByDefault() {
Xiyuan Xia2a66b112018-12-06 15:52:164208 if (should_focus_location_bar_by_default_)
[email protected]0c9406632013-02-08 01:13:334209 return true;
Xiyuan Xia2a66b112018-12-06 15:52:164210
[email protected]0c9406632013-02-08 01:13:334211 return delegate_ && delegate_->ShouldFocusLocationBarByDefault(this);
[email protected]0bfbf882011-12-22 18:19:274212}
4213
Tommy C. Li8bb5bf42019-01-18 18:45:514214void WebContentsImpl::SetFocusToLocationBar() {
[email protected]0bfbf882011-12-22 18:19:274215 if (delegate_)
Tommy C. Li8bb5bf42019-01-18 18:45:514216 delegate_->SetFocusToLocationBar();
[email protected]c40d6232011-03-25 00:16:214217}
4218
clamy0e119882015-07-31 16:12:334219void WebContentsImpl::DidStartNavigation(NavigationHandle* navigation_handle) {
Charles Harrison53096ba2017-12-15 15:39:484220 TRACE_EVENT1("navigation", "WebContentsImpl::DidStartNavigation",
4221 "navigation_handle", navigation_handle);
ericwilligersde386342016-10-19 02:35:094222 for (auto& observer : observers_)
4223 observer.DidStartNavigation(navigation_handle);
Becca Hughesd11d6502018-07-31 17:01:284224
4225 if (display_cutout_host_impl_)
4226 display_cutout_host_impl_->DidStartNavigation(navigation_handle);
Xiyuan Xia2a66b112018-12-06 15:52:164227
4228 if (navigation_handle->IsInMainFrame()) {
4229 // When the browser is started with about:blank as the startup URL, focus
4230 // the location bar (which will also select its contents) so people can
4231 // simply begin typing to navigate elsewhere.
4232 //
4233 // We need to be careful not to trigger this for anything other than the
4234 // startup navigation. In particular, if we allow an attacker to open a
4235 // popup to about:blank, then navigate, focusing the Omnibox will cause the
4236 // end of the new URL to be scrolled into view instead of the start,
4237 // allowing the attacker to spoof other URLs. The conditions checked here
4238 // are all aimed at ensuring no such attacker-controlled navigation can
4239 // trigger this.
4240 should_focus_location_bar_by_default_ =
4241 controller_.IsInitialNavigation() &&
4242 !navigation_handle->IsRendererInitiated() &&
4243 navigation_handle->GetURL() == url::kAboutBlankURL;
4244 }
clamy0e119882015-07-31 16:12:334245}
4246
4247void WebContentsImpl::DidRedirectNavigation(
4248 NavigationHandle* navigation_handle) {
Charles Harrison53096ba2017-12-15 15:39:484249 TRACE_EVENT1("navigation", "WebContentsImpl::DidRedirectNavigation",
4250 "navigation_handle", navigation_handle);
ericwilligersde386342016-10-19 02:35:094251 for (auto& observer : observers_)
4252 observer.DidRedirectNavigation(navigation_handle);
jam5f358e52017-02-13 16:37:034253
4254 // Notify accessibility if this is a reload. This has to called on the
4255 // BrowserAccessibilityManager associated with the old RFHI.
4256 if (navigation_handle->GetReloadType() != ReloadType::NONE) {
4257 NavigationHandleImpl* nhi =
4258 static_cast<NavigationHandleImpl*>(navigation_handle);
4259 BrowserAccessibilityManager* manager =
4260 nhi->frame_tree_node()
4261 ->current_frame_host()
4262 ->browser_accessibility_manager();
4263 if (manager)
4264 manager->UserIsReloading();
4265 }
clamy0e119882015-07-31 16:12:334266}
4267
clamyefca29e2015-09-17 00:22:114268void WebContentsImpl::ReadyToCommitNavigation(
4269 NavigationHandle* navigation_handle) {
Charles Harrison53096ba2017-12-15 15:39:484270 TRACE_EVENT1("navigation", "WebContentsImpl::ReadyToCommitNavigation",
4271 "navigation_handle", navigation_handle);
ericwilligersde386342016-10-19 02:35:094272 for (auto& observer : observers_)
4273 observer.ReadyToCommitNavigation(navigation_handle);
John Abd-El-Malekd4882642017-12-04 21:45:194274
Kenneth Russell954ed9ce2018-04-12 23:07:014275 // If any domains are blocked from accessing 3D APIs because they may
4276 // have caused the GPU to reset recently, unblock them here if the user
4277 // initiated this navigation. This implies that the user was involved in
4278 // the decision to navigate, so there's no concern about
4279 // denial-of-service issues. Want to do this as early as
4280 // possible to avoid race conditions with pages attempting to access
4281 // WebGL early on.
4282 //
4283 // TODO(crbug.com/617904): currently navigations initiated by the browser
4284 // (reload button, reload menu option, pressing return in the Omnibox)
4285 // return false from HasUserGesture(). If or when that is addressed,
4286 // remove the check for IsRendererInitiated() below.
4287 //
4288 // TODO(crbug.com/832180): HasUserGesture comes from the renderer
4289 // process and isn't validated. Until it is, don't trust it.
4290 if (!navigation_handle->IsRendererInitiated()) {
4291 GpuDataManagerImpl::GetInstance()->UnblockDomainFrom3DAPIs(
4292 navigation_handle->GetURL());
4293 }
4294
John Abd-El-Malekfde08e6da2017-12-05 04:06:244295 if (navigation_handle->IsSameDocument())
4296 return;
4297
Livvie Lin139bf442018-08-10 00:18:544298 // SSLInfo is not needed on subframe navigations since the main-frame
4299 // certificate is the only one that can be inspected (using the info
4300 // bubble) without refreshing the page with DevTools open.
Carlos ILaed1e7b2019-03-12 19:27:214301 // We don't call DidStartResourceResponse on net errors, since that results on
4302 // existing cert exceptions being revoked, which leads to weird behavior with
4303 // committed interstitials or while offline. We only need the error check for
4304 // the main frame case because unlike this method, SubresourceResponseStarted
4305 // does not get called on network errors.
4306 if (navigation_handle->IsInMainFrame() &&
4307 navigation_handle->GetNetErrorCode() == net::OK) {
Livvie Lin139bf442018-08-10 00:18:544308 controller_.ssl_manager()->DidStartResourceResponse(
4309 navigation_handle->GetURL(),
Camille Lamy62b826012019-02-26 09:15:474310 navigation_handle->GetSSLInfo().has_value()
4311 ? net::IsCertStatusError(
4312 navigation_handle->GetSSLInfo()->cert_status)
4313 : false);
4314 }
John Abd-El-Malekfde08e6da2017-12-05 04:06:244315
4316 SetNotWaitingForResponse();
clamyefca29e2015-09-17 00:22:114317}
4318
clamy0e119882015-07-31 16:12:334319void WebContentsImpl::DidFinishNavigation(NavigationHandle* navigation_handle) {
Charles Harrison53096ba2017-12-15 15:39:484320 TRACE_EVENT1("navigation", "WebContentsImpl::DidFinishNavigation",
4321 "navigation_handle", navigation_handle);
Steven Holte38041342018-06-22 22:00:574322
ericwilligersde386342016-10-19 02:35:094323 for (auto& observer : observers_)
4324 observer.DidFinishNavigation(navigation_handle);
clamy0e119882015-07-31 16:12:334325
Becca Hughesd11d6502018-07-31 17:01:284326 if (display_cutout_host_impl_)
4327 display_cutout_host_impl_->DidFinishNavigation(navigation_handle);
4328
jam5f358e52017-02-13 16:37:034329 if (navigation_handle->HasCommitted()) {
dmazzonibf8cec42015-02-08 08:28:084330 BrowserAccessibilityManager* manager =
jam5f358e52017-02-13 16:37:034331 static_cast<RenderFrameHostImpl*>(
4332 navigation_handle->GetRenderFrameHost())
4333 ->browser_accessibility_manager();
4334 if (manager) {
4335 if (navigation_handle->IsErrorPage()) {
4336 manager->NavigationFailed();
4337 } else {
4338 manager->NavigationSucceeded();
4339 }
4340 }
Tommy Steimel18360512017-11-01 00:38:194341
4342 if (navigation_handle->IsInMainFrame() &&
4343 !navigation_handle->IsSameDocument()) {
4344 was_ever_audible_ = false;
Steven Holte38041342018-06-22 22:00:574345 last_committed_source_id_ =
4346 ukm::ConvertToSourceId(navigation_handle->GetNavigationId(),
4347 ukm::SourceIdType::NAVIGATION_ID);
Tommy Steimel18360512017-11-01 00:38:194348 }
dmazzonibf8cec42015-02-08 08:28:084349 }
Xiyuan Xia2a66b112018-12-06 15:52:164350
4351 // If we didn't end up on about:blank after setting this in DidStartNavigation
4352 // then don't focus the location bar.
4353 if (should_focus_location_bar_by_default_ &&
4354 navigation_handle->GetURL() != url::kAboutBlankURL) {
4355 should_focus_location_bar_by_default_ = false;
4356 }
[email protected]400992b2012-06-14 00:03:544357}
4358
[email protected]b80624c2014-02-09 02:46:554359void WebContentsImpl::DidFailLoadWithError(
4360 RenderFrameHostImpl* render_frame_host,
[email protected]b80624c2014-02-09 02:46:554361 const GURL& url,
[email protected]b80624c2014-02-09 02:46:554362 int error_code,
Yutaka Hirano16d3c5d2017-07-31 06:17:304363 const base::string16& error_description) {
ericwilligersde386342016-10-19 02:35:094364 for (auto& observer : observers_) {
Yutaka Hirano16d3c5d2017-07-31 06:17:304365 observer.DidFailLoad(render_frame_host, url, error_code, error_description);
ericwilligersde386342016-10-19 02:35:094366 }
[email protected]b80624c2014-02-09 02:46:554367}
4368
[email protected]52913802013-12-10 05:52:184369void WebContentsImpl::NotifyChangedNavigationState(
4370 InvalidateTypes changed_flags) {
4371 NotifyNavigationStateChanged(changed_flags);
4372}
4373
toyoshim0df1d3a2016-09-09 09:52:484374void WebContentsImpl::DidStartNavigationToPendingEntry(const GURL& url,
4375 ReloadType reload_type) {
[email protected]3691e5cf2014-01-22 10:16:204376 // Notify observers about navigation.
ericwilligersde386342016-10-19 02:35:094377 for (auto& observer : observers_)
4378 observer.DidStartNavigationToPendingEntry(url, reload_type);
[email protected]3691e5cf2014-01-22 10:16:204379}
4380
lukasza1d025732016-09-28 21:36:084381bool WebContentsImpl::ShouldTransferNavigation(bool is_main_frame_navigation) {
creis29460272015-12-16 04:38:224382 if (!delegate_)
4383 return true;
lukasza1d025732016-09-28 21:36:084384 return delegate_->ShouldTransferNavigation(is_main_frame_navigation);
creis29460272015-12-16 04:38:224385}
4386
[email protected]aa62afd2014-04-22 19:22:464387bool WebContentsImpl::ShouldPreserveAbortedURLs() {
4388 if (!delegate_)
4389 return false;
4390 return delegate_->ShouldPreserveAbortedURLs(this);
4391}
4392
[email protected]0d0f4c492014-04-02 06:42:574393void WebContentsImpl::DidNavigateMainFramePreCommit(
[email protected]5cfbddc2014-06-23 23:52:234394 bool navigation_is_within_page) {
[email protected]0d0f4c492014-04-02 06:42:574395 // Ensure fullscreen mode is exited before committing the navigation to a
4396 // different page. The next page will not start out assuming it is in
4397 // fullscreen mode.
[email protected]5cfbddc2014-06-23 23:52:234398 if (navigation_is_within_page) {
[email protected]dfc39cb2014-04-09 22:58:194399 // No page change? Then, the renderer and browser can remain in fullscreen.
[email protected]0d0f4c492014-04-02 06:42:574400 return;
4401 }
alexmos5dd617d2016-06-07 04:21:154402 if (IsFullscreenForCurrentTab())
bokanece34a82016-01-28 19:49:464403 ExitFullscreen(false);
alexmos5dd617d2016-06-07 04:21:154404 DCHECK(!IsFullscreenForCurrentTab());
Joe Downingc1a57efe2018-05-09 18:23:544405
4406 // Clean up keyboard lock state when navigating.
4407 CancelKeyboardLock(keyboard_lock_widget_);
[email protected]0d0f4c492014-04-02 06:42:574408}
4409
[email protected]37567b432014-02-12 01:12:224410void WebContentsImpl::DidNavigateMainFramePostCommit(
sky8b00392d2015-01-10 00:30:284411 RenderFrameHostImpl* render_frame_host,
[email protected]37567b432014-02-12 01:12:224412 const LoadCommittedDetails& details,
4413 const FrameHostMsg_DidCommitProvisionalLoad_Params& params) {
4414 if (details.is_navigation_to_different_page()) {
4415 // Clear the status bubble. This is a workaround for a bug where WebKit
4416 // doesn't let us know that the cursor left an element during a
4417 // transition (this is also why the mouse cursor remains as a hand after
4418 // clicking on a link); see bugs 1184641 and 980803. We don't want to
4419 // clear the bubble when a user navigates to a named anchor in the same
4420 // page.
Lukasz Anforowiczfd707142018-02-07 19:46:134421 ClearTargetURL();
lanwei9d343ad22015-02-11 01:46:004422
4423 RenderWidgetHostViewBase* rwhvb =
4424 static_cast<RenderWidgetHostViewBase*>(GetRenderWidgetHostView());
4425 if (rwhvb)
4426 rwhvb->OnDidNavigateMainFrameToNewPage();
yusufod41c5f92015-03-06 00:14:284427
peter90afaba2015-06-01 12:05:294428 did_first_visually_non_empty_paint_ = false;
4429
yusufod41c5f92015-03-06 00:14:284430 // Reset theme color on navigation to new page.
Alan Cutter92d59252019-03-29 02:32:334431 theme_color_.reset();
[email protected]37567b432014-02-12 01:12:224432 }
4433
[email protected]e85165c642014-06-10 14:34:314434 if (delegate_)
[email protected]37567b432014-02-12 01:12:224435 delegate_->DidNavigateMainFramePostCommit(this);
[email protected]e85165c642014-06-10 14:34:314436 view_->SetOverscrollControllerEnabled(CanOverscrollContent());
Shivani Sharma985474f2019-05-23 21:36:474437
4438 if (!details.is_same_document && GetInnerWebContents().empty())
4439 had_inner_webcontents_ = false;
[email protected]37567b432014-02-12 01:12:224440}
4441
4442void WebContentsImpl::DidNavigateAnyFramePostCommit(
4443 RenderFrameHostImpl* render_frame_host,
4444 const LoadCommittedDetails& details,
4445 const FrameHostMsg_DidCommitProvisionalLoad_Params& params) {
[email protected]b4c84012014-04-28 19:51:104446 // Now that something has committed, we don't need to track whether the
4447 // initial page has been accessed.
4448 has_accessed_initial_document_ = false;
4449
[email protected]37567b432014-02-12 01:12:224450 // If we navigate off the page, close all JavaScript dialogs.
eugenebutee08663a2017-04-27 17:43:124451 if (!details.is_same_document)
creis89a0f782015-05-27 16:13:174452 CancelActiveAndPendingDialogs();
[email protected]37567b432014-02-12 01:12:224453
avid53461d2016-02-25 17:15:044454 // If this is a user-initiated navigation, start allowing JavaScript dialogs
4455 // again.
avi5d3b8692016-10-12 22:00:464456 if (params.gesture == NavigationGestureUser && dialog_manager_) {
avi6879fcf2017-01-21 05:27:534457 dialog_manager_->CancelDialogs(this, /*reset_state=*/true);
avi5d3b8692016-10-12 22:00:464458 }
[email protected]37567b432014-02-12 01:12:224459}
4460
4461void WebContentsImpl::SetMainFrameMimeType(const std::string& mime_type) {
4462 contents_mime_type_ = mime_type;
4463}
4464
[email protected]277857a2014-06-03 10:38:014465bool WebContentsImpl::CanOverscrollContent() const {
[email protected]e85165c642014-06-10 14:34:314466 // Disable overscroll when touch emulation is on. See crbug.com/369938.
[email protected]34ff1cfc2014-08-20 06:16:054467 if (force_disable_overscroll_content_)
[email protected]e85165c642014-06-10 14:34:314468 return false;
4469
[email protected]37567b432014-02-12 01:12:224470 if (delegate_)
4471 return delegate_->CanOverscrollContent();
4472
4473 return false;
4474}
4475
nicka0ac8382016-12-15 23:59:234476void WebContentsImpl::OnThemeColorChanged(RenderFrameHostImpl* source,
Alan Cutter92d59252019-03-29 02:32:334477 base::Optional<SkColor> theme_color) {
nicka0ac8382016-12-15 23:59:234478 if (source != GetMainFrame()) {
4479 // Only the main frame may control the theme.
4480 return;
4481 }
4482
peter90afaba2015-06-01 12:05:294483 // Update the theme color. This is to be published to observers after the
4484 // first visually non-empty paint.
yusufod41c5f92015-03-06 00:14:284485 theme_color_ = theme_color;
peter90afaba2015-06-01 12:05:294486
4487 if (did_first_visually_non_empty_paint_ &&
4488 last_sent_theme_color_ != theme_color_) {
ericwilligersde386342016-10-19 02:35:094489 for (auto& observer : observers_)
4490 observer.DidChangeThemeColor(theme_color_);
peter90afaba2015-06-01 12:05:294491 last_sent_theme_color_ = theme_color_;
4492 }
[email protected]e7104762014-06-20 19:17:254493}
4494
[email protected]b172aee2012-04-10 17:05:264495void WebContentsImpl::OnDidLoadResourceFromMemoryCache(
nicka0ac8382016-12-15 23:59:234496 RenderFrameHostImpl* source,
[email protected]724159a2010-12-30 01:11:184497 const GURL& url,
[email protected]70435962011-08-02 20:13:284498 const std::string& http_method,
[email protected]6d6cfb3a2012-05-23 22:53:184499 const std::string& mime_type,
Maks Orlovich158ea092018-12-17 22:16:124500 const base::Optional<url::Origin>& top_frame_origin,
[email protected]6c1e05212014-07-31 00:59:404501 ResourceType resource_type) {
ericwilligersde386342016-10-19 02:35:094502 for (auto& observer : observers_)
4503 observer.DidLoadResourceFromMemoryCache(url, mime_type, resource_type);
[email protected]b0f724c2013-09-05 04:21:134504
Steven Bingler674fb0822018-12-12 18:04:524505 if (url.is_valid() && url.SchemeIsHTTPOrHTTPS()) {
nicka0ac8382016-12-15 23:59:234506 StoragePartition* partition = source->GetProcess()->GetStoragePartition();
Steven Bingler674fb0822018-12-12 18:04:524507
4508 // We require different paths here because there is no NetworkContext
4509 // for media cache.
4510 if (base::FeatureList::IsEnabled(network::features::kNetworkService)) {
Maks Orlovich158ea092018-12-17 22:16:124511 partition->GetNetworkContext()->NotifyExternalCacheHit(url, http_method,
4512 top_frame_origin);
Steven Bingler674fb0822018-12-12 18:04:524513 } else {
4514 scoped_refptr<net::URLRequestContextGetter> request_context(
Daniel Chengaee5c0312019-04-18 23:49:054515 resource_type == ResourceType::kMedia
Steven Bingler674fb0822018-12-12 18:04:524516 ? partition->GetMediaURLRequestContext()
4517 : partition->GetURLRequestContext());
4518 base::PostTaskWithTraits(
4519 FROM_HERE, {BrowserThread::IO},
Maks Orlovich158ea092018-12-17 22:16:124520 base::BindOnce(&NotifyCacheOnIO, request_context, url, http_method,
4521 top_frame_origin));
Steven Bingler674fb0822018-12-12 18:04:524522 }
[email protected]8bfc8272013-09-09 20:10:534523 }
[email protected]724159a2010-12-30 01:11:184524}
4525
nicka0ac8382016-12-15 23:59:234526void WebContentsImpl::OnDidDisplayInsecureContent(RenderFrameHostImpl* source) {
4527 // Any frame can trigger display of insecure content, so we don't check
4528 // |source| here.
carloskd9d97942017-02-16 08:58:094529 DidDisplayInsecureContent();
4530}
4531
4532void WebContentsImpl::DidDisplayInsecureContent() {
estarkc2273502016-10-12 22:03:024533 controller_.ssl_manager()->DidDisplayMixedContent();
[email protected]724159a2010-12-30 01:11:184534}
4535
elawrenceb2ac2a232017-03-27 21:46:254536void WebContentsImpl::OnDidContainInsecureFormAction(
4537 RenderFrameHostImpl* source) {
4538 controller_.ssl_manager()->DidContainInsecureFormAction();
4539}
4540
nicka0ac8382016-12-15 23:59:234541void WebContentsImpl::OnDidRunInsecureContent(RenderFrameHostImpl* source,
4542 const GURL& security_origin,
estark910b4572015-12-09 20:55:414543 const GURL& target_url) {
nicka0ac8382016-12-15 23:59:234544 // TODO(nick, estark): Should we call FilterURL using |source|'s process on
4545 // these parameters? |target_url| seems unused, except for a log message. And
4546 // |security_origin| might be replaceable with the origin of the main frame.
carloskd9d97942017-02-16 08:58:094547 DidRunInsecureContent(security_origin, target_url);
4548}
4549
4550void WebContentsImpl::DidRunInsecureContent(const GURL& security_origin,
4551 const GURL& target_url) {
[email protected]9450c462013-11-23 01:22:584552 LOG(WARNING) << security_origin << " ran insecure content from "
4553 << target_url.possibly_invalid_spec();
[email protected]e6e30ac2014-01-13 21:24:394554 RecordAction(base::UserMetricsAction("SSL.RanInsecureContent"));
estark910b4572015-12-09 20:55:414555 if (base::EndsWith(security_origin.spec(), kDotGoogleDotCom,
brettwa7ff1b292015-07-16 17:49:294556 base::CompareCase::INSENSITIVE_ASCII))
[email protected]e6e30ac2014-01-13 21:24:394557 RecordAction(base::UserMetricsAction("SSL.RanInsecureContentGoogle"));
estarkc2273502016-10-12 22:03:024558 controller_.ssl_manager()->DidRunMixedContent(security_origin);
[email protected]724159a2010-12-30 01:11:184559}
4560
carloskd9d97942017-02-16 08:58:094561void WebContentsImpl::PassiveInsecureContentFound(const GURL& resource_url) {
Sergey Kuznetsov3ebc01b2018-02-01 04:57:364562 if (delegate_) {
4563 delegate_->PassiveInsecureContentFound(resource_url);
4564 }
carloskd9d97942017-02-16 08:58:094565}
4566
4567bool WebContentsImpl::ShouldAllowRunningInsecureContent(
4568 WebContents* web_contents,
4569 bool allowed_per_prefs,
4570 const url::Origin& origin,
4571 const GURL& resource_url) {
Sergey Kuznetsov3ebc01b2018-02-01 04:57:364572 if (delegate_) {
4573 return delegate_->ShouldAllowRunningInsecureContent(
4574 web_contents, allowed_per_prefs, origin, resource_url);
4575 }
4576
4577 return allowed_per_prefs;
carloskd9d97942017-02-16 08:58:094578}
4579
Lukasz Anforowicze1b954d92017-10-30 21:28:064580void WebContentsImpl::ViewSource(RenderFrameHostImpl* frame) {
4581 DCHECK_EQ(this, WebContents::FromRenderFrameHost(frame));
4582
4583 // Don't do anything if there is no |delegate_| that could accept and show the
4584 // new WebContents containing the view-source.
4585 if (!delegate_)
4586 return;
4587
4588 // Use the last committed entry, since the pending entry hasn't loaded yet and
4589 // won't be copied into the cloned tab.
4590 NavigationEntryImpl* last_committed_entry =
4591 static_cast<NavigationEntryImpl*>(frame->frame_tree_node()
4592 ->navigator()
4593 ->GetController()
4594 ->GetLastCommittedEntry());
4595 if (!last_committed_entry)
4596 return;
4597
4598 FrameNavigationEntry* frame_entry =
4599 last_committed_entry->GetFrameEntry(frame->frame_tree_node());
4600 if (!frame_entry)
4601 return;
4602
4603 // Any new WebContents opened while this WebContents is in fullscreen can be
4604 // used to confuse the user, so drop fullscreen.
Avi Drissmand18c51972018-08-15 18:32:094605 ForSecurityDropFullscreen();
Lukasz Anforowicze1b954d92017-10-30 21:28:064606
4607 // We intentionally don't share the SiteInstance with the original frame so
4608 // that view source has a consistent process model and always ends up in a new
4609 // process (https://crbug.com/699493).
4610 scoped_refptr<SiteInstanceImpl> site_instance_for_view_source = nullptr;
4611 // Referrer is not important, because view-source should not hit the network,
4612 // but should be served from the cache instead.
4613 Referrer referrer_for_view_source;
4614 // Do not restore title, derive it from the url.
4615 base::string16 title_for_view_source;
4616 auto navigation_entry = std::make_unique<NavigationEntryImpl>(
4617 site_instance_for_view_source, frame_entry->url(),
4618 referrer_for_view_source, title_for_view_source, ui::PAGE_TRANSITION_LINK,
Marijn Kruisselbrink7a0d5e182018-05-24 22:55:094619 /* is_renderer_initiated = */ false,
4620 /* blob_url_loader_factory = */ nullptr);
Lukasz Anforowicze1b954d92017-10-30 21:28:064621 navigation_entry->SetVirtualURL(GURL(content::kViewSourceScheme +
4622 std::string(":") +
4623 frame_entry->url().spec()));
4624
4625 // Do not restore scroller position.
4626 // TODO(creis, lukasza, arthursonzogni): Do not reuse the original PageState,
4627 // but start from a new one and only copy the needed data.
4628 const PageState& new_page_state =
4629 frame_entry->page_state().RemoveScrollOffset();
4630
4631 scoped_refptr<FrameNavigationEntry> new_frame_entry =
4632 navigation_entry->root_node()->frame_entry;
4633 new_frame_entry->set_method(frame_entry->method());
4634 new_frame_entry->SetPageState(new_page_state);
4635
4636 // Create a new WebContents, which is used to display the source code.
erikchenbee5c9622018-04-27 19:30:254637 std::unique_ptr<WebContents> view_source_contents =
Erik Chenbb8e738e2018-04-28 14:10:434638 Create(CreateParams(GetBrowserContext()));
Lukasz Anforowicze1b954d92017-10-30 21:28:064639
4640 // Restore the previously created NavigationEntry.
4641 std::vector<std::unique_ptr<NavigationEntry>> navigation_entries;
4642 navigation_entries.push_back(std::move(navigation_entry));
4643 view_source_contents->GetController().Restore(0, RestoreType::CURRENT_SESSION,
4644 &navigation_entries);
4645
4646 // Add |view_source_contents| as a new tab.
4647 gfx::Rect initial_rect;
4648 constexpr bool kUserGesture = true;
4649 bool ignored_was_blocked;
erikchenbee5c9622018-04-27 19:30:254650 delegate_->AddNewContents(this, std::move(view_source_contents),
Lukasz Anforowicze1b954d92017-10-30 21:28:064651 WindowOpenDisposition::NEW_FOREGROUND_TAB,
4652 initial_rect, kUserGesture, &ignored_was_blocked);
4653 // Note that the |delegate_| could have deleted |view_source_contents| during
4654 // AddNewContents method call.
4655}
4656
Jay Civellid5904f02018-03-23 19:43:574657void WebContentsImpl::SubresourceResponseStarted(const GURL& url,
4658 net::CertStatus cert_status) {
4659 controller_.ssl_manager()->DidStartResourceResponse(url, cert_status);
4660 SetNotWaitingForResponse();
4661}
4662
Jay Civelli116683f2018-03-27 19:56:234663void WebContentsImpl::ResourceLoadComplete(
Jay Civelli52a63302018-04-30 22:39:554664 RenderFrameHost* render_frame_host,
Clark DuVall36164bd2018-08-09 22:49:084665 const GlobalRequestID& request_id,
Jay Civelli116683f2018-03-27 19:56:234666 mojom::ResourceLoadInfoPtr resource_load_info) {
John Abd-El-Malek9b8f7fa2017-12-04 23:52:254667 for (auto& observer : observers_) {
Clark DuVall36164bd2018-08-09 22:49:084668 observer.ResourceLoadComplete(render_frame_host, request_id,
4669 *resource_load_info);
John Abd-El-Malek9b8f7fa2017-12-04 23:52:254670 }
John Abd-El-Malekd4882642017-12-04 21:45:194671}
4672
Wei Li5bb659742018-02-14 03:07:584673void WebContentsImpl::PrintCrossProcessSubframe(
4674 const gfx::Rect& rect,
4675 int document_cookie,
4676 RenderFrameHost* subframe_host) {
Wei Li5c0b4d12018-03-22 15:05:224677 auto* outer_contents = GetOuterWebContents();
4678 if (outer_contents) {
4679 // When an extension or app page is printed, the content should be
4680 // composited with outer content, so the outer contents should handle the
4681 // print request.
4682 outer_contents->PrintCrossProcessSubframe(rect, document_cookie,
4683 subframe_host);
4684 return;
4685 }
4686
Wei Li5bb659742018-02-14 03:07:584687 // If there is no delegate such as in tests or during deletion, do nothing.
4688 if (!delegate_)
4689 return;
4690
4691 delegate_->PrintCrossProcessSubframe(this, rect, document_cookie,
4692 subframe_host);
4693}
4694
rob.buis300b0872017-03-10 20:43:584695#if defined(OS_ANDROID)
4696base::android::ScopedJavaLocalRef<jobject>
4697WebContentsImpl::GetJavaRenderFrameHostDelegate() {
4698 return GetJavaWebContents();
4699}
4700#endif
4701
estark910b4572015-12-09 20:55:414702void WebContentsImpl::OnDidDisplayContentWithCertificateErrors(
Zhuoyu Qian4353f7f2018-01-08 05:33:004703 RenderFrameHostImpl* source) {
estarkc2273502016-10-12 22:03:024704 controller_.ssl_manager()->DidDisplayContentWithCertErrors();
estark910b4572015-12-09 20:55:414705}
4706
4707void WebContentsImpl::OnDidRunContentWithCertificateErrors(
Zhuoyu Qian4353f7f2018-01-08 05:33:004708 RenderFrameHostImpl* source) {
nicka0ac8382016-12-15 23:59:234709 // TODO(nick, estark): Do we need to consider |source| here somehow?
estarkcd2e30c2016-08-12 06:51:154710 NavigationEntry* entry = controller_.GetVisibleEntry();
4711 if (!entry)
estarkaeda1422016-05-10 02:31:124712 return;
4713
estarkaeda1422016-05-10 02:31:124714 // TODO(estark): check that this does something reasonable for
4715 // about:blank and sandboxed origins. https://crbug.com/609527
estarkcd2e30c2016-08-12 06:51:154716 controller_.ssl_manager()->DidRunContentWithCertErrors(
4717 entry->GetURL().GetOrigin());
estark910b4572015-12-09 20:55:414718}
4719
nicka0ac8382016-12-15 23:59:234720void WebContentsImpl::OnDocumentLoadedInFrame(RenderFrameHostImpl* source) {
ericwilligersde386342016-10-19 02:35:094721 for (auto& observer : observers_)
nicka0ac8382016-12-15 23:59:234722 observer.DocumentLoadedInFrame(source);
[email protected]724159a2010-12-30 01:11:184723}
4724
nicka0ac8382016-12-15 23:59:234725void WebContentsImpl::OnDidFinishLoad(RenderFrameHostImpl* source,
4726 const GURL& url) {
[email protected]8b3af1e2014-01-24 13:29:124727 GURL validated_url(url);
nicka0ac8382016-12-15 23:59:234728 source->GetProcess()->FilterURL(false, &validated_url);
[email protected]028053d42014-03-05 22:20:374729
ericwilligersde386342016-10-19 02:35:094730 for (auto& observer : observers_)
nicka0ac8382016-12-15 23:59:234731 observer.DidFinishLoad(source, validated_url);
[email protected]1a55c5be2011-11-29 11:36:314732}
4733
Dave Tapuska549baab2019-03-19 15:52:404734void WebContentsImpl::OnGoToEntryAtOffset(RenderFrameHostImpl* source,
Arthur Sonzognia7d715a2018-09-20 16:11:134735 int offset,
4736 bool has_user_gesture) {
4737 // Non-user initiated navigations coming from the renderer should be ignored
4738 // if there is an ongoing browser-initiated navigation.
4739 // See https://crbug.com/879965.
4740 // TODO(arthursonzogni): See if this should check for ongoing navigations in
4741 // the frame(s) affected by the session history navigation, rather than just
4742 // the main frame.
4743 if (!has_user_gesture) {
4744 NavigationRequest* ongoing_navigation_request =
4745 frame_tree_.root()->navigation_request();
4746 if (ongoing_navigation_request &&
4747 ongoing_navigation_request->browser_initiated()) {
4748 return;
4749 }
4750 }
4751
nicka0ac8382016-12-15 23:59:234752 // All frames are allowed to navigate the global history.
Dave Tapuska8bfd84c2019-03-26 20:47:164753 if (!delegate_ || delegate_->OnGoToEntryOffset(offset)) {
4754 if (source->IsSandboxed(blink::WebSandboxFlags::kTopNavigation)) {
4755 // Keep track of whether this is a session history from a sandboxed iframe
4756 // with top level navigation disallowed.
4757 controller_.GoToOffsetInSandboxedFrame(offset,
4758 source->GetFrameTreeNodeId());
4759 } else {
4760 controller_.GoToOffset(offset);
4761 }
4762 }
[email protected]216813952011-05-19 22:21:264763}
4764
nicka0ac8382016-12-15 23:59:234765void WebContentsImpl::OnUpdateZoomLimits(RenderViewHostImpl* source,
4766 int minimum_percent,
[email protected]fce823222014-05-30 16:24:304767 int maximum_percent) {
[email protected]216813952011-05-19 22:21:264768 minimum_zoom_percent_ = minimum_percent;
4769 maximum_zoom_percent_ = maximum_percent;
[email protected]216813952011-05-19 22:21:264770}
4771
nicka0ac8382016-12-15 23:59:234772void WebContentsImpl::OnPageScaleFactorChanged(RenderViewHostImpl* source,
4773 float page_scale_factor) {
mcnee336ea2c2017-05-23 22:50:594774#if !defined(OS_ANDROID)
4775 // While page scale factor is used on mobile, this PageScaleFactorIsOne logic
4776 // is only needed on desktop.
mcnee432e47d2015-11-09 19:37:464777 bool is_one = page_scale_factor == 1.f;
4778 if (is_one != page_scale_factor_is_one_) {
4779 page_scale_factor_is_one_ = is_one;
4780
4781 HostZoomMapImpl* host_zoom_map =
4782 static_cast<HostZoomMapImpl*>(HostZoomMap::GetForWebContents(this));
4783
nicka0ac8382016-12-15 23:59:234784 if (host_zoom_map) {
mcnee432e47d2015-11-09 19:37:464785 host_zoom_map->SetPageScaleFactorIsOneForView(
nicka0ac8382016-12-15 23:59:234786 source->GetProcess()->GetID(), source->GetRoutingID(),
mcnee432e47d2015-11-09 19:37:464787 page_scale_factor_is_one_);
4788 }
4789 }
mcnee336ea2c2017-05-23 22:50:594790#endif // !defined(OS_ANDROID)
mcnee432e47d2015-11-09 19:37:464791
ericwilligersde386342016-10-19 02:35:094792 for (auto& observer : observers_)
4793 observer.OnPageScaleFactorChanged(page_scale_factor);
mcnee432e47d2015-11-09 19:37:464794}
4795
W. James MacLean62198672019-06-04 16:46:104796void WebContentsImpl::OnTextAutosizerPageInfoChanged(
4797 RenderViewHostImpl* source,
4798 const blink::WebTextAutosizerPageInfo& page_info) {
4799 // Keep a copy of |page_info| in case we create a new RenderView before
4800 // the next update.
4801 text_autosizer_page_info_ = page_info;
4802 frame_tree_.root()->render_manager()->SendPageMessage(
4803 new PageMsg_UpdateTextAutosizerPageInfoForRemoteMainFrames(
4804 MSG_ROUTING_NONE, page_info),
4805 source->GetSiteInstance());
4806}
4807
Kent Tamura8c9e0562018-11-06 07:02:194808void WebContentsImpl::EnumerateDirectory(
4809 RenderFrameHost* render_frame_host,
4810 std::unique_ptr<FileSelectListener> listener,
4811 const base::FilePath& directory_path) {
4812 if (delegate_)
4813 delegate_->EnumerateDirectory(this, std::move(listener), directory_path);
4814 else
4815 listener->FileSelectionCanceled();
[email protected]3a29a6e2011-08-24 18:26:214816}
4817
nicka0ac8382016-12-15 23:59:234818void WebContentsImpl::OnRegisterProtocolHandler(RenderFrameHostImpl* source,
4819 const std::string& protocol,
[email protected]b172aee2012-04-10 17:05:264820 const GURL& url,
[email protected]fcf75d42013-12-03 20:11:264821 const base::string16& title,
[email protected]3a3b75a2012-06-01 08:38:364822 bool user_gesture) {
nicka0ac8382016-12-15 23:59:234823 // TODO(nick): Should we consider |source| here or pass it to the delegate?
4824 // TODO(nick): Do we need to apply FilterURL to |url|?
[email protected]e5f2de02012-07-20 22:15:434825 if (!delegate_)
4826 return;
4827
[email protected]b9535422012-02-09 01:47:594828 ChildProcessSecurityPolicyImpl* policy =
4829 ChildProcessSecurityPolicyImpl::GetInstance();
[email protected]8f810632013-06-06 22:33:324830 if (policy->IsPseudoScheme(protocol))
[email protected]b9535422012-02-09 01:47:594831 return;
[email protected]8f810632013-06-06 22:33:324832
[email protected]c125e532014-05-10 20:39:064833 delegate_->RegisterProtocolHandler(this, protocol, url, user_gesture);
[email protected]7d189022011-08-25 22:54:204834}
4835
nicka0ac8382016-12-15 23:59:234836void WebContentsImpl::OnUnregisterProtocolHandler(RenderFrameHostImpl* source,
4837 const std::string& protocol,
[email protected]f5273e52014-07-14 16:30:204838 const GURL& url,
4839 bool user_gesture) {
nicka0ac8382016-12-15 23:59:234840 // TODO(nick): Should we consider |source| here or pass it to the delegate?
4841 // TODO(nick): Do we need to apply FilterURL to |url|?
[email protected]f5273e52014-07-14 16:30:204842 if (!delegate_)
4843 return;
4844
4845 ChildProcessSecurityPolicyImpl* policy =
4846 ChildProcessSecurityPolicyImpl::GetInstance();
4847 if (policy->IsPseudoScheme(protocol))
4848 return;
4849
4850 delegate_->UnregisterProtocolHandler(this, protocol, url, user_gesture);
4851}
4852
kouhei40f03cb2015-09-24 07:47:014853void WebContentsImpl::OnUpdatePageImportanceSignals(
nicka0ac8382016-12-15 23:59:234854 RenderFrameHostImpl* source,
kouhei40f03cb2015-09-24 07:47:014855 const PageImportanceSignals& signals) {
nicka0ac8382016-12-15 23:59:234856 // TODO(nick, kouhei): Fix this for oopifs; currently all frames' state gets
4857 // written to this one field.
kouhei40f03cb2015-09-24 07:47:014858 page_importance_signals_ = signals;
4859}
4860
nicka0ac8382016-12-15 23:59:234861void WebContentsImpl::OnDomOperationResponse(RenderFrameHostImpl* source,
4862 const std::string& json_string) {
4863 // TODO(nick, lukasza): The notification below should probably be updated to
4864 // include |source|.
avi60bd4902015-09-23 20:39:244865 std::string json = json_string;
4866 NotificationService::current()->Notify(NOTIFICATION_DOM_OPERATION_RESPONSE,
4867 Source<WebContents>(this),
4868 Details<std::string>(&json));
[email protected]cfa856d62014-02-22 07:58:404869}
4870
Marijn Kruisselbrink7f519672019-02-05 19:12:504871void WebContentsImpl::OnAppCacheAccessed(const GURL& manifest_url,
[email protected]b172aee2012-04-10 17:05:264872 bool blocked_by_policy) {
nicka0ac8382016-12-15 23:59:234873 // TODO(nick): Should we consider |source| here? Should we call FilterURL on
4874 // |manifest_url|?
4875
[email protected]7fc4bbb2011-09-08 21:23:104876 // Notify observers about navigation.
ericwilligersde386342016-10-19 02:35:094877 for (auto& observer : observers_)
4878 observer.AppCacheAccessed(manifest_url, blocked_by_policy);
[email protected]7fc4bbb2011-09-08 21:23:104879}
4880
Joel Hockey85b379d2017-12-11 10:42:134881void WebContentsImpl::OnColorChooserFactoryRequest(
Joel Hockey163835a2017-12-20 11:51:574882 blink::mojom::ColorChooserFactoryRequest request) {
Joel Hockey85b379d2017-12-11 10:42:134883 color_chooser_factory_bindings_.AddBinding(this, std::move(request));
4884}
4885
4886void WebContentsImpl::OpenColorChooser(
Joel Hockey163835a2017-12-20 11:51:574887 blink::mojom::ColorChooserRequest chooser_request,
4888 blink::mojom::ColorChooserClientPtr client,
[email protected]8ed16472014-04-11 19:02:484889 SkColor color,
Joel Hockey163835a2017-12-20 11:51:574890 std::vector<blink::mojom::ColorSuggestionPtr> suggestions) {
Joel Hockey85b379d2017-12-11 10:42:134891 content::ColorChooser* new_color_chooser =
Ivan Kotenkov2c0d2bb32017-11-01 15:41:284892 delegate_ ? delegate_->OpenColorChooser(this, color, suggestions)
4893 : nullptr;
[email protected]820957a2014-01-14 14:56:304894 if (!new_color_chooser)
[email protected]cb805452013-05-22 15:16:214895 return;
[email protected]8ed16472014-04-11 19:02:484896
Joel Hockey85b379d2017-12-11 10:42:134897 color_chooser_.reset();
4898 color_chooser_ = std::make_unique<ColorChooser>(
4899 new_color_chooser, std::move(chooser_request), std::move(client));
[email protected]da8543762012-03-20 08:52:204900}
4901
brettw4b461082016-11-19 18:55:164902#if BUILDFLAG(ENABLE_PLUGINS)
nicka0ac8382016-12-15 23:59:234903void WebContentsImpl::OnPepperInstanceCreated(RenderFrameHostImpl* source,
4904 int32_t pp_instance) {
ericwilligersde386342016-10-19 02:35:094905 for (auto& observer : observers_)
4906 observer.PepperInstanceCreated();
nicka0ac8382016-12-15 23:59:234907 pepper_playback_observer_->PepperInstanceCreated(source, pp_instance);
emaxxe70f5e12015-05-29 11:26:004908}
4909
nicka0ac8382016-12-15 23:59:234910void WebContentsImpl::OnPepperInstanceDeleted(RenderFrameHostImpl* source,
4911 int32_t pp_instance) {
ericwilligersde386342016-10-19 02:35:094912 for (auto& observer : observers_)
4913 observer.PepperInstanceDeleted();
nicka0ac8382016-12-15 23:59:234914 pepper_playback_observer_->PepperInstanceDeleted(source, pp_instance);
emaxxe70f5e12015-05-29 11:26:004915}
4916
nicka0ac8382016-12-15 23:59:234917void WebContentsImpl::OnPepperPluginHung(RenderFrameHostImpl* source,
4918 int plugin_child_id,
tommyclie6633ca72014-10-31 00:40:424919 const base::FilePath& path,
4920 bool is_hung) {
Steven Holte95922222018-09-14 20:06:234921 UMA_HISTOGRAM_COUNTS_1M("Pepper.PluginHung", 1);
tommyclie6633ca72014-10-31 00:40:424922
ericwilligersde386342016-10-19 02:35:094923 for (auto& observer : observers_)
4924 observer.PluginHungStatusChanged(plugin_child_id, path, is_hung);
tommyclie6633ca72014-10-31 00:40:424925}
4926
nicka0ac8382016-12-15 23:59:234927void WebContentsImpl::OnPepperStartsPlayback(RenderFrameHostImpl* source,
4928 int32_t pp_instance) {
4929 pepper_playback_observer_->PepperStartsPlayback(source, pp_instance);
zqzhang181047e62016-07-01 13:37:174930}
4931
nicka0ac8382016-12-15 23:59:234932void WebContentsImpl::OnPepperStopsPlayback(RenderFrameHostImpl* source,
4933 int32_t pp_instance) {
4934 pepper_playback_observer_->PepperStopsPlayback(source, pp_instance);
zqzhang181047e62016-07-01 13:37:174935}
4936
nicka0ac8382016-12-15 23:59:234937void WebContentsImpl::OnPluginCrashed(RenderFrameHostImpl* source,
4938 const base::FilePath& plugin_path,
tommyclie6633ca72014-10-31 00:40:424939 base::ProcessId plugin_pid) {
nicka0ac8382016-12-15 23:59:234940 // TODO(nick): Eliminate the |plugin_pid| parameter, which can't be trusted,
4941 // and is only used by BlinkTestController.
ericwilligersde386342016-10-19 02:35:094942 for (auto& observer : observers_)
4943 observer.PluginCrashed(plugin_path, plugin_pid);
tommyclie6633ca72014-10-31 00:40:424944}
4945
[email protected]d8415ad92012-08-23 14:40:504946void WebContentsImpl::OnRequestPpapiBrokerPermission(
nicka0ac8382016-12-15 23:59:234947 RenderViewHostImpl* source,
4948 int ppb_broker_route_id,
[email protected]d8415ad92012-08-23 14:40:504949 const GURL& url,
[email protected]2dec8ec2013-02-07 19:20:344950 const base::FilePath& plugin_path) {
Avi Drissman0a2a967d2019-04-01 19:33:524951 base::OnceCallback<void(bool)> permission_result_callback = base::BindOnce(
nicka0ac8382016-12-15 23:59:234952 &WebContentsImpl::SendPpapiBrokerPermissionResult, base::Unretained(this),
4953 source->GetProcess()->GetID(), ppb_broker_route_id);
[email protected]8c8fc292012-11-23 18:57:164954 if (!delegate_) {
Tommy Nyquist4b749d02018-03-20 21:46:294955 std::move(permission_result_callback).Run(false);
[email protected]8c8fc292012-11-23 18:57:164956 return;
[email protected]d8415ad92012-08-23 14:40:504957 }
4958
Avi Drissman0a2a967d2019-04-01 19:33:524959 delegate_->RequestPpapiBrokerPermission(
4960 this, url, plugin_path, std::move(permission_result_callback));
[email protected]d8415ad92012-08-23 14:40:504961}
4962
nicka0ac8382016-12-15 23:59:234963void WebContentsImpl::SendPpapiBrokerPermissionResult(int process_id,
4964 int ppb_broker_route_id,
4965 bool result) {
4966 RenderProcessHost* rph = RenderProcessHost::FromID(process_id);
4967 if (rph) {
4968 // TODO(nick): Convert this from ViewMsg_ to a Ppapi msg, since it
4969 // is not routed to a RenderView.
4970 rph->Send(
4971 new ViewMsg_PpapiBrokerPermissionResult(ppb_broker_route_id, result));
4972 }
[email protected]d8415ad92012-08-23 14:40:504973}
4974
fsamuel833ee7c2015-02-13 23:40:404975void WebContentsImpl::OnBrowserPluginMessage(RenderFrameHost* render_frame_host,
4976 const IPC::Message& message) {
Lei Zhang6410ec1a2019-01-31 21:38:584977 CHECK(!browser_plugin_embedder_);
fsamuel60b42282015-03-10 03:29:144978 CreateBrowserPluginEmbedderIfNecessary();
fsamuel833ee7c2015-02-13 23:40:404979 browser_plugin_embedder_->OnMessageReceived(message, render_frame_host);
[email protected]19be7a62012-10-01 23:03:374980}
brettw4b461082016-11-19 18:55:164981#endif // BUILDFLAG(ENABLE_PLUGINS)
[email protected]19be7a62012-10-01 23:03:374982
[email protected]795c28972012-12-06 06:13:394983void WebContentsImpl::OnUpdateFaviconURL(
lukasza178ff092017-06-07 23:39:504984 RenderFrameHostImpl* source,
[email protected]795c28972012-12-06 06:13:394985 const std::vector<FaviconURL>& candidates) {
lukasza178ff092017-06-07 23:39:504986 // Ignore favicons for non-main frame.
4987 if (source->GetParent()) {
4988 NOTREACHED();
4989 return;
4990 }
4991
pkotwiczf9bc20592015-04-21 18:57:064992 // We get updated favicon URLs after the page stops loading. If a cross-site
4993 // navigation occurs while a page is still loading, the initial page
4994 // may stop loading and send us updated favicon URLs after the navigation
4995 // for the new page has committed.
lukasza178ff092017-06-07 23:39:504996 if (!source->IsCurrent())
pkotwiczf9bc20592015-04-21 18:57:064997 return;
4998
ericwilligersde386342016-10-19 02:35:094999 for (auto& observer : observers_)
5000 observer.DidUpdateFaviconURL(candidates);
[email protected]795c28972012-12-06 06:13:395001}
5002
qinmin72e8bd02016-10-21 19:35:375003void WebContentsImpl::SetIsOverlayContent(bool is_overlay_content) {
5004 is_overlay_content_ = is_overlay_content;
5005}
5006
Albert J. Wongfb64e142018-10-16 01:10:455007void WebContentsImpl::DidFirstVisuallyNonEmptyPaint(
5008 RenderViewHostImpl* source) {
Francois Dorayae33f2f2018-10-30 13:40:045009 // Set |did_first_visually_non_empty_paint_| before notifying observers so
5010 // they can see that CompletedFirstVisuallyNonEmptyPaint() is true.
5011 did_first_visually_non_empty_paint_ = true;
5012
nicka0ac8382016-12-15 23:59:235013 // TODO(nick): When this is ported to FrameHostMsg_, we should only listen if
5014 // |source| is the main frame.
ericwilligersde386342016-10-19 02:35:095015 for (auto& observer : observers_)
5016 observer.DidFirstVisuallyNonEmptyPaint();
yusufod41c5f92015-03-06 00:14:285017
5018 if (theme_color_ != last_sent_theme_color_) {
5019 // Theme color should have updated by now if there was one.
ericwilligersde386342016-10-19 02:35:095020 for (auto& observer : observers_)
5021 observer.DidChangeThemeColor(theme_color_);
yusufod41c5f92015-03-06 00:14:285022 last_sent_theme_color_ = theme_color_;
5023 }
[email protected]9f268072013-11-07 00:02:155024}
[email protected]d9030b82013-07-19 08:26:065025
Albert J. Wongfb64e142018-10-16 01:10:455026void WebContentsImpl::DidCommitAndDrawCompositorFrame(
Yiming Zhoudd60b0b2018-07-20 18:51:355027 RenderViewHostImpl* source) {
5028 for (auto& observer : observers_)
5029 observer.DidCommitAndDrawCompositorFrame();
5030}
5031
Adithya Srinivasan46b8a792019-02-01 14:47:235032bool WebContentsImpl::IsPortal() const {
5033 return portal();
5034}
5035
[email protected]17e286e2013-03-01 23:29:395036void WebContentsImpl::NotifyBeforeFormRepostWarningShow() {
ericwilligersde386342016-10-19 02:35:095037 for (auto& observer : observers_)
5038 observer.BeforeFormRepostWarningShow();
[email protected]17e286e2013-03-01 23:29:395039}
5040
[email protected]ec6c05f2013-10-23 18:41:575041void WebContentsImpl::ActivateAndShowRepostFormWarningDialog() {
5042 Activate();
5043 if (delegate_)
5044 delegate_->ShowRepostFormWarningDialog(this);
5045}
5046
[email protected]b4c84012014-04-28 19:51:105047bool WebContentsImpl::HasAccessedInitialDocument() {
5048 return has_accessed_initial_document_;
5049}
5050
afakhry6f0c1ec22016-07-14 13:55:135051void WebContentsImpl::UpdateTitleForEntry(NavigationEntry* entry,
[email protected]fcf75d42013-12-03 20:11:265052 const base::string16& title) {
[email protected]fcf75d42013-12-03 20:11:265053 base::string16 final_title;
Avi Drissman93002212017-09-27 03:20:525054 base::TrimWhitespace(title, base::TRIM_ALL, &final_title);
[email protected]420ae012009-04-24 05:16:325055
[email protected]987fc3a2011-05-26 14:18:095056 // If a page is created via window.open and never navigated,
5057 // there will be no navigation entry. In this situation,
[email protected]73eb2602012-02-09 19:50:555058 // |page_title_when_no_navigation_entry_| will be used for page title.
[email protected]987fc3a2011-05-26 14:18:095059 if (entry) {
[email protected]36fc0392011-12-25 03:59:515060 if (final_title == entry->GetTitle())
afakhry6f0c1ec22016-07-14 13:55:135061 return; // Nothing changed, don't bother.
[email protected]420ae012009-04-24 05:16:325062
[email protected]36fc0392011-12-25 03:59:515063 entry->SetTitle(final_title);
Avi Drissman93002212017-09-27 03:20:525064
5065 // The title for display may differ from the title just set; grab it.
5066 final_title = entry->GetTitleForDisplay();
[email protected]987fc3a2011-05-26 14:18:095067 } else {
5068 if (page_title_when_no_navigation_entry_ == final_title)
afakhry6f0c1ec22016-07-14 13:55:135069 return; // Nothing changed, don't bother.
[email protected]987fc3a2011-05-26 14:18:095070
5071 page_title_when_no_navigation_entry_ = final_title;
5072 }
[email protected]420ae012009-04-24 05:16:325073
[email protected]420ae012009-04-24 05:16:325074 // Lastly, set the title for the view.
[email protected]790e2fd2011-09-21 20:28:235075 view_->SetPageTitle(final_title);
[email protected]420ae012009-04-24 05:16:325076
ericwilligersde386342016-10-19 02:35:095077 for (auto& observer : observers_)
Avi Drissman93002212017-09-27 03:20:525078 observer.TitleWasSet(entry);
[email protected]66798902013-10-01 18:40:165079
afakhry6f0c1ec22016-07-14 13:55:135080 // Broadcast notifications when the UI should be updated.
5081 if (entry == controller_.GetEntryAtOffset(0))
5082 NotifyNavigationStateChanged(INVALIDATE_TYPE_TITLE);
[email protected]420ae012009-04-24 05:16:325083}
5084
fdegansa696e5112015-04-17 01:57:595085void WebContentsImpl::SendChangeLoadProgress() {
[email protected]960b0372014-05-19 18:01:005086 loading_last_progress_update_ = base::TimeTicks::Now();
[email protected]960b0372014-05-19 18:01:005087 if (delegate_)
fdegansa696e5112015-04-17 01:57:595088 delegate_->LoadProgressChanged(this, frame_tree_.load_progress());
[email protected]960b0372014-05-19 18:01:005089}
5090
5091void WebContentsImpl::ResetLoadProgressState() {
fdegans1d16355162015-03-26 11:58:345092 frame_tree_.ResetLoadProgress();
[email protected]960b0372014-05-19 18:01:005093 loading_weak_factory_.InvalidateWeakPtrs();
5094 loading_last_progress_update_ = base::TimeTicks();
5095}
5096
clamy44e84ce2016-02-22 15:38:255097// Notifies the RenderWidgetHost instance about the fact that the page is
5098// loading, or done loading.
5099void WebContentsImpl::LoadingStateChanged(bool to_different_document,
5100 bool due_to_interstitial,
5101 LoadNotificationDetails* details) {
Nate Chapin22ea6592019-03-05 22:29:025102 if (is_being_destroyed_)
5103 return;
clamy44e84ce2016-02-22 15:38:255104 // Do not send notifications about loading changes in the FrameTree while the
5105 // interstitial page is pausing the throbber.
creisadca9862017-06-16 19:12:115106 if (ShowingInterstitialPage() && interstitial_page_->pause_throbber() &&
clamy44e84ce2016-02-22 15:38:255107 !due_to_interstitial) {
5108 return;
5109 }
5110
5111 bool is_loading = IsLoading();
5112
5113 if (!is_loading) {
5114 load_state_ = net::LoadStateWithParam(net::LOAD_STATE_IDLE,
5115 base::string16());
5116 load_state_host_.clear();
5117 upload_size_ = 0;
5118 upload_position_ = 0;
5119 }
5120
5121 GetRenderManager()->SetIsLoading(is_loading);
5122
5123 waiting_for_response_ = is_loading;
5124 is_load_to_different_document_ = to_different_document;
5125
5126 if (delegate_)
5127 delegate_->LoadingStateChanged(this, to_different_document);
5128 NotifyNavigationStateChanged(INVALIDATE_TYPE_LOAD);
5129
5130 std::string url = (details ? details->url.possibly_invalid_spec() : "NULL");
5131 if (is_loading) {
5132 TRACE_EVENT_ASYNC_BEGIN2("browser,navigation", "WebContentsImpl Loading",
5133 this, "URL", url, "Main FrameTreeNode id",
5134 GetFrameTree()->root()->frame_tree_node_id());
ericwilligersde386342016-10-19 02:35:095135 for (auto& observer : observers_)
5136 observer.DidStartLoading();
clamy44e84ce2016-02-22 15:38:255137 } else {
5138 TRACE_EVENT_ASYNC_END1("browser,navigation", "WebContentsImpl Loading",
5139 this, "URL", url);
ericwilligersde386342016-10-19 02:35:095140 for (auto& observer : observers_)
5141 observer.DidStopLoading();
clamy44e84ce2016-02-22 15:38:255142 }
5143
5144 // TODO(avi): Remove. http://crbug.com/170921
5145 int type = is_loading ? NOTIFICATION_LOAD_START : NOTIFICATION_LOAD_STOP;
5146 NotificationDetails det = NotificationService::NoDetails();
5147 if (details)
5148 det = Details<LoadNotificationDetails>(details);
5149 NotificationService::current()->Notify(
5150 type, Source<NavigationController>(&controller_), det);
5151}
5152
[email protected]02d7b6e2014-06-24 21:01:505153void WebContentsImpl::NotifyViewSwapped(RenderViewHost* old_host,
5154 RenderViewHost* new_host) {
[email protected]420ae012009-04-24 05:16:325155 // After sending out a swap notification, we need to send a disconnect
5156 // notification so that clients that pick up a pointer to |this| can NULL the
5157 // pointer. See Bug 1230284.
5158 notify_disconnection_ = true;
ericwilligersde386342016-10-19 02:35:095159 for (auto& observer : observers_)
5160 observer.RenderViewHostChanged(old_host, new_host);
[email protected]da7a7182013-09-06 08:11:115161
Jinsuk Kimdc5284c22018-07-11 02:22:065162 view_->RenderViewHostChanged(old_host, new_host);
5163
Lucas Furukawa Gadanif9ea4fc2018-08-03 23:57:135164 // If this is an inner WebContents that has swapped views, we need to reattach
5165 // it to its outer WebContents.
5166 if (node_.outer_web_contents())
5167 ReattachToOuterWebContentsFrame();
5168
[email protected]7a846df2012-09-20 19:17:395169 // Ensure that the associated embedder gets cleared after a RenderViewHost
5170 // gets swapped, so we don't reuse the same embedder next time a
5171 // RenderViewHost is attached to this WebContents.
5172 RemoveBrowserPluginEmbedder();
[email protected]420ae012009-04-24 05:16:325173}
5174
[email protected]02d7b6e2014-06-24 21:01:505175void WebContentsImpl::NotifyFrameSwapped(RenderFrameHost* old_host,
Jinsuk Kim895b2c52018-07-03 06:49:165176 RenderFrameHost* new_host,
5177 bool is_main_frame) {
Bo Liu168c8642017-08-28 18:26:025178#if defined(OS_ANDROID)
Bo Liu16dd7b8322018-05-02 21:19:505179 // Copy importance from |old_host| if |new_host| is a main frame.
5180 if (old_host && !new_host->GetParent()) {
Bo Liu7c6779e92017-08-16 02:02:285181 static_cast<RenderFrameHostImpl*>(new_host)
5182 ->GetRenderWidgetHost()
Bo Liu16dd7b8322018-05-02 21:19:505183 ->SetImportance(static_cast<RenderFrameHostImpl*>(old_host)
5184 ->GetRenderWidgetHost()
5185 ->importance());
Bo Liu7c6779e92017-08-16 02:02:285186 }
Bo Liu168c8642017-08-28 18:26:025187#endif
ericwilligersde386342016-10-19 02:35:095188 for (auto& observer : observers_)
5189 observer.RenderFrameHostChanged(old_host, new_host);
[email protected]02d7b6e2014-06-24 21:01:505190}
5191
[email protected]da7a7182013-09-06 08:11:115192// TODO(avi): Remove this entire function because this notification is already
5193// covered by two observer functions. http://crbug.com/170921
[email protected]b172aee2012-04-10 17:05:265194void WebContentsImpl::NotifyDisconnected() {
[email protected]420ae012009-04-24 05:16:325195 if (!notify_disconnection_)
5196 return;
5197
5198 notify_disconnection_ = false;
[email protected]8ff00d72012-10-23 19:12:215199 NotificationService::current()->Notify(
5200 NOTIFICATION_WEB_CONTENTS_DISCONNECTED,
5201 Source<WebContents>(this),
5202 NotificationService::NoDetails());
[email protected]420ae012009-04-24 05:16:325203}
5204
[email protected]cbb1ef592013-06-05 19:49:465205void WebContentsImpl::NotifyNavigationEntryCommitted(
5206 const LoadCommittedDetails& load_details) {
ericwilligersde386342016-10-19 02:35:095207 for (auto& observer : observers_)
5208 observer.NavigationEntryCommitted(load_details);
[email protected]cbb1ef592013-06-05 19:49:465209}
5210
Sam McNally5c087a32017-08-25 01:46:145211void WebContentsImpl::NotifyNavigationEntryChanged(
5212 const EntryChangedDetails& change_details) {
5213 for (auto& observer : observers_)
5214 observer.NavigationEntryChanged(change_details);
5215}
5216
5217void WebContentsImpl::NotifyNavigationListPruned(
5218 const PrunedDetails& pruned_details) {
5219 for (auto& observer : observers_)
5220 observer.NavigationListPruned(pruned_details);
5221}
5222
Christian Dullweber1af31e62018-02-22 11:49:485223void WebContentsImpl::NotifyNavigationEntriesDeleted() {
5224 for (auto& observer : observers_)
5225 observer.NavigationEntriesDeleted();
5226}
5227
rockotf62002a2016-09-15 00:08:595228void WebContentsImpl::OnAssociatedInterfaceRequest(
5229 RenderFrameHost* render_frame_host,
5230 const std::string& interface_name,
5231 mojo::ScopedInterfaceEndpointHandle handle) {
5232 auto it = binding_sets_.find(interface_name);
5233 if (it != binding_sets_.end())
5234 it->second->OnRequestForFrame(render_frame_host, std::move(handle));
5235}
5236
Ben Goodger9e24929e2017-07-21 03:13:405237void WebContentsImpl::OnInterfaceRequest(
5238 RenderFrameHost* render_frame_host,
5239 const std::string& interface_name,
5240 mojo::ScopedMessagePipeHandle* interface_pipe) {
Joel Hockey85b379d2017-12-11 10:42:135241 registry_.TryBindInterface(interface_name, interface_pipe);
Ben Goodger9e24929e2017-07-21 03:13:405242 for (auto& observer : observers_) {
5243 observer.OnInterfaceRequestFromFrame(render_frame_host, interface_name,
5244 interface_pipe);
5245 if (!interface_pipe->is_valid())
5246 break;
5247 }
5248}
5249
Varun Khaneja5da94302019-06-25 04:48:105250void WebContentsImpl::OnDidBlockNavigation(
5251 const GURL& blocked_url,
5252 const GURL& initiator_url,
5253 blink::NavigationBlockedReason reason) {
Nicolas Dossou-gbetebcab2472017-09-09 12:28:405254 if (delegate_)
Varun Khaneja5da94302019-06-25 04:48:105255 delegate_->OnDidBlockNavigation(this, blocked_url, initiator_url, reason);
Nicolas Dossou-gbetebcab2472017-09-09 12:28:405256}
5257
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:415258const GURL& WebContentsImpl::GetMainFrameLastCommittedURL() {
[email protected]c31a84802014-04-03 15:55:495259 return GetLastCommittedURL();
5260}
5261
[email protected]b849847b2013-12-10 21:57:585262void WebContentsImpl::RenderFrameCreated(RenderFrameHost* render_frame_host) {
ericwilligersde386342016-10-19 02:35:095263 for (auto& observer : observers_)
5264 observer.RenderFrameCreated(render_frame_host);
dmazzonif8a31042016-12-03 00:52:135265 UpdateAccessibilityModeOnFrame(render_frame_host);
clamy91b7abf2016-03-24 15:17:455266
Becca Hughesd11d6502018-07-31 17:01:285267 if (display_cutout_host_impl_)
5268 display_cutout_host_impl_->RenderFrameCreated(render_frame_host);
5269
clamy91b7abf2016-03-24 15:17:455270 if (!render_frame_host->IsRenderFrameLive() || render_frame_host->GetParent())
5271 return;
5272
5273 NavigationEntry* entry = controller_.GetPendingEntry();
5274 if (entry && entry->IsViewSourceMode()) {
5275 // Put the renderer in view source mode.
5276 render_frame_host->Send(
5277 new FrameMsg_EnableViewSourceMode(render_frame_host->GetRoutingID()));
5278 }
[email protected]b849847b2013-12-10 21:57:585279}
5280
5281void WebContentsImpl::RenderFrameDeleted(RenderFrameHost* render_frame_host) {
creis3638897452017-05-09 22:07:505282 is_notifying_observers_ = true;
ericwilligersde386342016-10-19 02:35:095283 for (auto& observer : observers_)
5284 observer.RenderFrameDeleted(render_frame_host);
creis3638897452017-05-09 22:07:505285 is_notifying_observers_ = false;
zqzhang7b6f4f02016-12-07 22:10:225286#if BUILDFLAG(ENABLE_PLUGINS)
5287 pepper_playback_observer_->RenderFrameDeleted(render_frame_host);
5288#endif
Becca Hughesfd5d8f892018-06-14 18:23:365289
Becca Hughesd11d6502018-07-31 17:01:285290 if (display_cutout_host_impl_)
5291 display_cutout_host_impl_->RenderFrameDeleted(render_frame_host);
5292
Becca Hughesfd5d8f892018-06-14 18:23:365293 // Remove any fullscreen state that the frame has stored.
5294 FullscreenStateChanged(render_frame_host, false /* is_fullscreen */);
[email protected]b849847b2013-12-10 21:57:585295}
5296
[email protected]a09d53ce2014-01-31 00:46:425297void WebContentsImpl::ShowContextMenu(RenderFrameHost* render_frame_host,
5298 const ContextMenuParams& params) {
avie9fd8722017-04-10 16:17:585299 // If a renderer fires off a second command to show a context menu before the
5300 // first context menu is closed, just ignore it. https://crbug.com/707534
ekaramadf6750aa2017-06-06 18:29:425301 if (showing_context_menu_)
avie9fd8722017-04-10 16:17:585302 return;
5303
[email protected]077e704b2014-05-23 19:24:105304 ContextMenuParams context_menu_params(params);
[email protected]a09d53ce2014-01-31 00:46:425305 // Allow WebContentsDelegates to handle the context menu operation first.
Joel Klinghed0ea22ea2019-02-20 22:04:225306 if (delegate_ &&
5307 delegate_->HandleContextMenu(render_frame_host, context_menu_params))
[email protected]a09d53ce2014-01-31 00:46:425308 return;
5309
[email protected]077e704b2014-05-23 19:24:105310 render_view_host_delegate_view_->ShowContextMenu(render_frame_host,
5311 context_menu_params);
[email protected]a09d53ce2014-01-31 00:46:425312}
5313
Avi Drissman4891b852018-09-25 18:12:585314namespace {
5315// Normalizes the line endings: \r\n -> \n, lone \r -> \n.
5316base::string16 NormalizeLineBreaks(const base::string16& source) {
5317 static const base::NoDestructor<base::string16> kReturnNewline(
5318 base::ASCIIToUTF16("\r\n"));
5319 static const base::NoDestructor<base::string16> kReturn(
5320 base::ASCIIToUTF16("\r"));
5321 static const base::NoDestructor<base::string16> kNewline(
5322 base::ASCIIToUTF16("\n"));
5323
5324 std::vector<base::StringPiece16> pieces;
5325
5326 for (const auto& rn_line : base::SplitStringPieceUsingSubstr(
5327 source, *kReturnNewline, base::KEEP_WHITESPACE,
5328 base::SPLIT_WANT_ALL)) {
5329 auto r_lines = base::SplitStringPieceUsingSubstr(
5330 rn_line, *kReturn, base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL);
5331 std::move(std::begin(r_lines), std::end(r_lines),
5332 std::back_inserter(pieces));
5333 }
5334
5335 return base::JoinString(pieces, *kNewline);
5336}
5337} // namespace
5338
avi777ff452017-02-09 19:04:485339void WebContentsImpl::RunJavaScriptDialog(RenderFrameHost* render_frame_host,
5340 const base::string16& message,
5341 const base::string16& default_prompt,
avi777ff452017-02-09 19:04:485342 JavaScriptDialogType dialog_type,
5343 IPC::Message* reply_msg) {
Avi Drissman2ccbb402018-03-20 21:11:365344 // Ensure that if showing a dialog is the first thing that a page does, that
5345 // the contents of the previous page aren't shown behind it. This is required
5346 // because showing a dialog freezes the renderer, so no frames will be coming
5347 // from it. https://crbug.com/823353
5348 auto* render_widget_host_impl =
5349 static_cast<RenderFrameHostImpl*>(render_frame_host)
5350 ->GetRenderWidgetHost();
5351 if (render_widget_host_impl)
5352 render_widget_host_impl->ForceFirstFrameAfterNavigationTimeout();
5353
avi0720b02e2017-06-13 03:22:135354 // Running a dialog causes an exit to webpage-initiated fullscreen.
5355 // http://crbug.com/728276
Avi Drissmand18c51972018-08-15 18:32:095356 ForSecurityDropFullscreen();
avi0720b02e2017-06-13 03:22:135357
Avi Drissmane04d3992017-10-05 15:11:365358 auto callback =
5359 base::BindOnce(&WebContentsImpl::OnDialogClosed, base::Unretained(this),
5360 render_frame_host->GetProcess()->GetID(),
5361 render_frame_host->GetRoutingID(), reply_msg);
Pavel Feldman3c1842b2017-08-02 05:00:165362
Pavel Feldman3c1842b2017-08-02 05:00:165363 std::vector<protocol::PageHandler*> page_handlers =
5364 protocol::PageHandler::EnabledForWebContents(this);
5365
Pavel Feldmanfab3cb62017-10-05 01:02:015366 if (delegate_)
5367 dialog_manager_ = delegate_->GetJavaScriptDialogManager(this);
5368
5369 // Suppress JavaScript dialogs when requested. Also suppress messages when
5370 // showing an interstitial as it's shown over the previous page and we don't
5371 // want the hidden page's dialogs to interfere with the interstitial.
5372 bool should_suppress = ShowingInterstitialPage() ||
5373 (delegate_ && delegate_->ShouldSuppressDialogs(this));
Pavel Feldman1c587bc52018-04-04 21:16:055374 bool has_non_devtools_handlers = delegate_ && dialog_manager_;
5375 bool has_handlers = page_handlers.size() || has_non_devtools_handlers;
Pavel Feldmanfab3cb62017-10-05 01:02:015376 bool suppress_this_message = should_suppress || !has_handlers;
5377
5378 if (suppress_this_message) {
Avi Drissmane04d3992017-10-05 15:11:365379 std::move(callback).Run(true, false, base::string16());
Pavel Feldman3c1842b2017-08-02 05:00:165380 return;
5381 }
5382
5383 scoped_refptr<CloseDialogCallbackWrapper> wrapper =
Avi Drissmane04d3992017-10-05 15:11:365384 new CloseDialogCallbackWrapper(std::move(callback));
Pavel Feldman3c1842b2017-08-02 05:00:165385
5386 is_showing_javascript_dialog_ = true;
5387
Avi Drissman4891b852018-09-25 18:12:585388 base::string16 normalized_message = NormalizeLineBreaks(message);
5389
Pavel Feldman3c1842b2017-08-02 05:00:165390 for (auto* handler : page_handlers) {
Avi Drissmane04d3992017-10-05 15:11:365391 handler->DidRunJavaScriptDialog(
Avi Drissman4891b852018-09-25 18:12:585392 render_frame_host->GetLastCommittedURL(), normalized_message,
5393 default_prompt, dialog_type, has_non_devtools_handlers,
Avi Drissmane04d3992017-10-05 15:11:365394 base::BindOnce(&CloseDialogCallbackWrapper::Run, wrapper, false));
Pavel Feldman3c1842b2017-08-02 05:00:165395 }
5396
5397 if (dialog_manager_) {
[email protected]87de04b02014-04-08 22:14:495398 dialog_manager_->RunJavaScriptDialog(
Avi Drissman4891b852018-09-25 18:12:585399 this, render_frame_host, dialog_type, normalized_message,
5400 default_prompt,
Avi Drissmane04d3992017-10-05 15:11:365401 base::BindOnce(&CloseDialogCallbackWrapper::Run, wrapper, false),
5402 &suppress_this_message);
[email protected]87de04b02014-04-08 22:14:495403 }
5404
5405 if (suppress_this_message) {
5406 // If we are suppressing messages, just reply as if the user immediately
5407 // pressed "Cancel", passing true to |dialog_was_suppressed|.
Avi Drissmane04d3992017-10-05 15:11:365408 wrapper->Run(true, false, base::string16());
[email protected]87de04b02014-04-08 22:14:495409 }
[email protected]87de04b02014-04-08 22:14:495410}
5411
5412void WebContentsImpl::RunBeforeUnloadConfirm(
[email protected]39f2f9252014-05-03 00:54:015413 RenderFrameHost* render_frame_host,
[email protected]87de04b02014-04-08 22:14:495414 bool is_reload,
5415 IPC::Message* reply_msg) {
Avi Drissman2ccbb402018-03-20 21:11:365416 // Ensure that if showing a dialog is the first thing that a page does, that
5417 // the contents of the previous page aren't shown behind it. This is required
5418 // because showing a dialog freezes the renderer, so no frames will be coming
5419 // from it. https://crbug.com/823353
5420 auto* render_widget_host_impl =
5421 static_cast<RenderFrameHostImpl*>(render_frame_host)
5422 ->GetRenderWidgetHost();
5423 if (render_widget_host_impl)
5424 render_widget_host_impl->ForceFirstFrameAfterNavigationTimeout();
5425
avi0720b02e2017-06-13 03:22:135426 // Running a dialog causes an exit to webpage-initiated fullscreen.
5427 // http://crbug.com/728276
Avi Drissmand18c51972018-08-15 18:32:095428 ForSecurityDropFullscreen();
avi0720b02e2017-06-13 03:22:135429
[email protected]39f2f9252014-05-03 00:54:015430 RenderFrameHostImpl* rfhi =
5431 static_cast<RenderFrameHostImpl*>(render_frame_host);
[email protected]87de04b02014-04-08 22:14:495432 if (delegate_)
5433 delegate_->WillRunBeforeUnloadConfirm();
5434
Avi Drissmane04d3992017-10-05 15:11:365435 auto callback =
5436 base::BindOnce(&WebContentsImpl::OnDialogClosed, base::Unretained(this),
5437 render_frame_host->GetProcess()->GetID(),
5438 render_frame_host->GetRoutingID(), reply_msg);
Pavel Feldman3c1842b2017-08-02 05:00:165439
Pavel Feldman3c1842b2017-08-02 05:00:165440 std::vector<protocol::PageHandler*> page_handlers =
5441 protocol::PageHandler::EnabledForWebContents(this);
5442
Pavel Feldmanfab3cb62017-10-05 01:02:015443 if (delegate_)
5444 dialog_manager_ = delegate_->GetJavaScriptDialogManager(this);
5445
5446 bool should_suppress = ShowingInterstitialPage() || !rfhi->is_active() ||
5447 (delegate_ && delegate_->ShouldSuppressDialogs(this));
Pavel Feldman1c587bc52018-04-04 21:16:055448 bool has_non_devtools_handlers = delegate_ && dialog_manager_;
5449 bool has_handlers = page_handlers.size() || has_non_devtools_handlers;
Pavel Feldmanfab3cb62017-10-05 01:02:015450 if (should_suppress || !has_handlers) {
Avi Drissmane04d3992017-10-05 15:11:365451 std::move(callback).Run(false, true, base::string16());
[email protected]87de04b02014-04-08 22:14:495452 return;
5453 }
5454
5455 is_showing_before_unload_dialog_ = true;
Pavel Feldman3c1842b2017-08-02 05:00:165456
5457 scoped_refptr<CloseDialogCallbackWrapper> wrapper =
Avi Drissmane04d3992017-10-05 15:11:365458 new CloseDialogCallbackWrapper(std::move(callback));
Pavel Feldman3c1842b2017-08-02 05:00:165459
5460 GURL frame_url = rfhi->GetLastCommittedURL();
5461 for (auto* handler : page_handlers) {
Avi Drissmane04d3992017-10-05 15:11:365462 handler->DidRunBeforeUnloadConfirm(
Pavel Feldman1c587bc52018-04-04 21:16:055463 frame_url, has_non_devtools_handlers,
Avi Drissmane04d3992017-10-05 15:11:365464 base::BindOnce(&CloseDialogCallbackWrapper::Run, wrapper, false));
Pavel Feldman3c1842b2017-08-02 05:00:165465 }
5466
5467 if (dialog_manager_) {
Avi Drissmane04d3992017-10-05 15:11:365468 dialog_manager_->RunBeforeUnloadDialog(
Avi Drissmanff9ed752017-11-28 21:18:125469 this, render_frame_host, is_reload,
Avi Drissmane04d3992017-10-05 15:11:365470 base::BindOnce(&CloseDialogCallbackWrapper::Run, wrapper, false));
Pavel Feldman3c1842b2017-08-02 05:00:165471 }
[email protected]87de04b02014-04-08 22:14:495472}
5473
Kent Tamurac260c9a2018-09-14 09:10:565474void WebContentsImpl::RunFileChooser(
5475 RenderFrameHost* render_frame_host,
Kent Tamura512a27e2018-10-04 00:49:325476 std::unique_ptr<content::FileSelectListener> listener,
Kent Tamurac260c9a2018-09-14 09:10:565477 const blink::mojom::FileChooserParams& params) {
Avi Drissman3d41e7712018-08-27 21:18:085478 // Any explicit focusing of another window while this WebContents is in
5479 // fullscreen can be used to confuse the user, so drop fullscreen.
5480 ForSecurityDropFullscreen();
5481
Andrey Lushnikov81b992e2019-06-27 23:27:215482 RenderFrameHostImpl* rfhi =
5483 static_cast<RenderFrameHostImpl*>(render_frame_host);
5484 if (devtools_instrumentation::InterceptFileChooser(rfhi, &listener, params))
5485 return;
5486
naskoada75b22016-06-11 16:09:465487 if (delegate_)
Kent Tamura512a27e2018-10-04 00:49:325488 delegate_->RunFileChooser(render_frame_host, std::move(listener), params);
5489 else
5490 listener->FileSelectionCanceled();
naskoada75b22016-06-11 16:09:465491}
5492
[email protected]a86c0e962013-12-17 17:10:395493WebContents* WebContentsImpl::GetAsWebContents() {
5494 return this;
5495}
5496
mcnee336ea2c2017-05-23 22:50:595497#if !defined(OS_ANDROID)
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:415498double WebContentsImpl::GetPendingPageZoomLevel() {
wjmaclean64951902016-04-29 20:59:125499 NavigationEntry* pending_entry = GetController().GetPendingEntry();
5500 if (!pending_entry)
5501 return HostZoomMap::GetZoomLevel(this);
5502
5503 GURL url = pending_entry->GetURL();
5504 return HostZoomMap::GetForWebContents(this)->GetZoomLevelForHostAndScheme(
5505 url.scheme(), net::GetHostOrSpecFromURL(url));
5506}
mcnee336ea2c2017-05-23 22:50:595507#endif // !defined(OS_ANDROID)
wjmaclean64951902016-04-29 20:59:125508
qinmin72e8bd02016-10-21 19:35:375509bool WebContentsImpl::HideDownloadUI() const {
5510 return is_overlay_content_;
5511}
5512
mlamouri5cd9ae82017-02-18 11:05:095513bool WebContentsImpl::HasPersistentVideo() const {
5514 return has_persistent_video_;
5515}
5516
Ian Vollick5d5c37f2019-06-19 22:50:575517bool WebContentsImpl::IsSpatialNavigationDisabled() const {
5518 return is_spatial_navigation_disabled_;
5519}
5520
Alex Moshchuk27caae82017-09-11 23:11:185521RenderFrameHost* WebContentsImpl::GetPendingMainFrame() {
clamy610c63b32017-12-22 15:05:185522 return GetRenderManager()->speculative_frame_host();
Alex Moshchuk27caae82017-09-11 23:11:185523}
5524
mlamourie52c1b542017-02-23 14:57:505525bool WebContentsImpl::HasActiveEffectivelyFullscreenVideo() const {
5526 return media_web_contents_observer_->HasActiveEffectivelyFullscreenVideo();
5527}
5528
François Beaufortad6c5232018-02-26 11:00:445529bool WebContentsImpl::IsPictureInPictureAllowedForFullscreenVideo() const {
5530 return media_web_contents_observer_
5531 ->IsPictureInPictureAllowedForFullscreenVideo();
5532}
5533
ekaramada110f642016-12-21 19:47:285534bool WebContentsImpl::IsFocusedElementEditable() {
5535 RenderFrameHostImpl* frame = GetFocusedFrame();
5536 return frame && frame->has_focused_editable_element();
5537}
5538
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:415539bool WebContentsImpl::IsShowingContextMenu() {
ekaramadf6750aa2017-06-06 18:29:425540 return showing_context_menu_;
5541}
5542
5543void WebContentsImpl::SetShowingContextMenu(bool showing) {
5544 DCHECK_NE(showing_context_menu_, showing);
5545 showing_context_menu_ = showing;
5546
5547 if (auto* view = GetRenderWidgetHostView()) {
5548 // Notify the main frame's RWHV to run the platform-specific code, if any.
5549 static_cast<RenderWidgetHostViewBase*>(view)->SetShowingContextMenu(
5550 showing);
5551 }
5552}
5553
ekaramada110f642016-12-21 19:47:285554void WebContentsImpl::ClearFocusedElement() {
5555 if (auto* frame = GetFocusedFrame())
5556 frame->ClearFocusedElement();
5557}
5558
[email protected]7912e822014-04-16 02:37:035559bool WebContentsImpl::IsNeverVisible() {
5560 if (!delegate_)
5561 return false;
5562 return delegate_->IsNeverVisible(this);
5563}
5564
[email protected]5a3bdf52012-05-24 15:12:575565RenderViewHostDelegateView* WebContentsImpl::GetDelegateView() {
5566 return render_view_host_delegate_view_;
[email protected]420ae012009-04-24 05:16:325567}
5568
Leon Hanc819dc62019-01-28 04:30:195569blink::mojom::RendererPreferences WebContentsImpl::GetRendererPrefs(
[email protected]8ff00d72012-10-23 19:12:215570 BrowserContext* browser_context) const {
[email protected]840b1512009-07-21 16:53:465571 return renderer_preferences_;
[email protected]80d96fa2009-06-10 22:34:515572}
5573
[email protected]7a846df2012-09-20 19:17:395574void WebContentsImpl::RemoveBrowserPluginEmbedder() {
Lei Zhang6410ec1a2019-01-31 21:38:585575 browser_plugin_embedder_.reset();
[email protected]7a846df2012-09-20 19:17:395576}
5577
Lucas Furukawa Gadani2ec00c82018-12-14 15:53:165578RenderFrameHostImpl* WebContentsImpl::GetOuterWebContentsFrame() {
5579 if (GetOuterDelegateFrameTreeNodeId() ==
5580 FrameTreeNode::kFrameTreeNodeInvalidId) {
5581 return nullptr;
5582 }
5583
5584 FrameTreeNode* outer_node =
5585 FrameTreeNode::GloballyFindByID(GetOuterDelegateFrameTreeNodeId());
5586 // The outer node should be in the outer WebContents.
5587 DCHECK_EQ(outer_node->frame_tree(), GetOuterWebContents()->GetFrameTree());
5588 return outer_node->parent()->current_frame_host();
5589}
5590
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:415591WebContentsImpl* WebContentsImpl::GetOuterWebContents() {
lfg601233692017-03-06 22:45:445592 if (GuestMode::IsCrossProcessFrameGuest(this))
5593 return node_.outer_web_contents();
ekaramad1beabecc2016-09-23 20:18:175594
5595 if (browser_plugin_guest_)
5596 return browser_plugin_guest_->embedder_web_contents();
5597
Lucas Furukawa Gadani105de1e812018-05-31 19:38:395598 return node_.outer_web_contents();
avallee0206f782016-07-28 18:55:335599}
5600
Lucas Furukawa Gadani2ec00c82018-12-14 15:53:165601std::vector<WebContents*> WebContentsImpl::GetInnerWebContents() {
5602 std::vector<WebContents*> all_inner_contents;
5603 if (browser_plugin_embedder_) {
Sean Topping7df73952019-05-16 17:21:165604 BrowserPluginGuestManager* guest_manager =
5605 GetBrowserContext()->GetGuestManager();
5606 if (guest_manager) {
5607 guest_manager->ForEachGuest(
5608 this,
5609 base::BindRepeating(&GetInnerWebContentsHelper, &all_inner_contents));
5610 }
Lucas Furukawa Gadani2ec00c82018-12-14 15:53:165611 }
5612 const auto& inner_contents = node_.GetInnerWebContents();
5613 all_inner_contents.insert(all_inner_contents.end(), inner_contents.begin(),
5614 inner_contents.end());
5615 return all_inner_contents;
5616}
5617
avallee0206f782016-07-28 18:55:335618WebContentsImpl* WebContentsImpl::GetFocusedWebContents() {
lfg601233692017-03-06 22:45:445619 return GetOutermostWebContents()->node_.focused_web_contents();
avallee0206f782016-07-28 18:55:335620}
5621
avallee9993fca2016-11-17 06:16:505622bool WebContentsImpl::ContainsOrIsFocusedWebContents() {
5623 for (WebContentsImpl* focused_contents = GetFocusedWebContents();
5624 focused_contents;
5625 focused_contents = focused_contents->GetOuterWebContents()) {
5626 if (focused_contents == this)
5627 return true;
5628 }
5629
5630 return false;
5631}
5632
avallee0206f782016-07-28 18:55:335633WebContentsImpl* WebContentsImpl::GetOutermostWebContents() {
5634 WebContentsImpl* root = this;
5635 while (root->GetOuterWebContents())
5636 root = root->GetOuterWebContents();
5637 return root;
5638}
5639
avalleed68cdd12017-06-21 22:07:275640void WebContentsImpl::FocusOuterAttachmentFrameChain() {
5641 WebContentsImpl* outer_contents = GetOuterWebContents();
5642 if (!outer_contents)
5643 return;
5644
5645 FrameTreeNode* outer_node =
5646 FrameTreeNode::GloballyFindByID(GetOuterDelegateFrameTreeNodeId());
5647 outer_contents->frame_tree_.SetFocusedFrame(outer_node, nullptr);
5648
5649 // For a browser initiated focus change, let embedding renderer know of the
5650 // change. Otherwise, if the currently focused element is just across a
5651 // process boundary in focus order, it will not be possible to move across
5652 // that boundary. This is because the target element will already be focused
5653 // (that renderer was not notified) and drop the event.
5654 if (GetRenderManager()->GetProxyToOuterDelegate())
5655 GetRenderManager()->GetProxyToOuterDelegate()->SetFocusedFrame();
5656
5657 outer_contents->FocusOuterAttachmentFrameChain();
5658}
5659
Sean Toppingbbc4a2bd2019-01-30 02:16:145660void WebContentsImpl::InnerWebContentsCreated(WebContents* inner_web_contents) {
Shivani Sharma985474f2019-05-23 21:36:475661 had_inner_webcontents_ = true;
Sean Toppingbbc4a2bd2019-01-30 02:16:145662 for (auto& observer : observers_)
5663 observer.InnerWebContentsCreated(inner_web_contents);
5664}
5665
[email protected]b172aee2012-04-10 17:05:265666void WebContentsImpl::RenderViewCreated(RenderViewHost* render_view_host) {
[email protected]86f98a22013-03-20 14:35:005667 if (delegate_)
[email protected]e85165c642014-06-10 14:34:315668 view_->SetOverscrollControllerEnabled(CanOverscrollContent());
[email protected]a6b73c62013-02-11 23:05:085669
[email protected]8ff00d72012-10-23 19:12:215670 NotificationService::current()->Notify(
5671 NOTIFICATION_WEB_CONTENTS_RENDER_VIEW_HOST_CREATED,
5672 Source<WebContents>(this),
5673 Details<RenderViewHost>(render_view_host));
[email protected]420ae012009-04-24 05:16:325674
[email protected]60780f412013-02-25 16:34:105675 view_->RenderViewCreated(render_view_host);
[email protected]11f764bb2011-05-25 19:45:265676
ericwilligersde386342016-10-19 02:35:095677 for (auto& observer : observers_)
5678 observer.RenderViewCreated(render_view_host);
Joel Einbinder848001c2017-09-19 21:39:315679 if (delegate_)
5680 RenderFrameDevToolsAgentHost::WebContentsCreated(this);
[email protected]420ae012009-04-24 05:16:325681}
5682
[email protected]b172aee2012-04-10 17:05:265683void WebContentsImpl::RenderViewReady(RenderViewHost* rvh) {
[email protected]151a63d2011-12-20 22:32:525684 if (rvh != GetRenderViewHost()) {
[email protected]420ae012009-04-24 05:16:325685 // Don't notify the world, since this came from a renderer in the
5686 // background.
5687 return;
5688 }
5689
dmazzoni62111452017-04-28 23:46:555690 RenderWidgetHostViewBase* rwhv =
5691 static_cast<RenderWidgetHostViewBase*>(GetRenderWidgetHostView());
5692 if (rwhv)
5693 rwhv->SetMainFrameAXTreeID(GetMainFrame()->GetAXTreeID());
5694
[email protected]da7a7182013-09-06 08:11:115695 notify_disconnection_ = true;
[email protected]da7a7182013-09-06 08:11:115696
[email protected]be1f56ab2011-12-22 06:55:315697 bool was_crashed = IsCrashed();
[email protected]443b80e2010-12-14 00:42:235698 SetIsCrashed(base::TERMINATION_STATUS_STILL_RUNNING, 0);
[email protected]9d8a4642009-07-29 17:25:305699
5700 // Restore the focus to the tab (otherwise the focus will be on the top
5701 // window).
[email protected]484ae5912010-09-29 19:16:145702 if (was_crashed && !FocusLocationBarByDefault() &&
5703 (!delegate_ || delegate_->ShouldFocusPageAfterCrash())) {
[email protected]f3615f02013-02-26 06:09:065704 view_->Focus();
[email protected]484ae5912010-09-29 19:16:145705 }
[email protected]32ded2212011-11-10 18:51:435706
ericwilligersde386342016-10-19 02:35:095707 for (auto& observer : observers_)
5708 observer.RenderViewReady();
Jinsuk Kim895b2c52018-07-03 06:49:165709
5710 view_->RenderViewReady();
[email protected]420ae012009-04-24 05:16:325711}
5712
[email protected]ec6a7eb2013-04-22 17:34:225713void WebContentsImpl::RenderViewTerminated(RenderViewHost* rvh,
5714 base::TerminationStatus status,
5715 int error_code) {
[email protected]151a63d2011-12-20 22:32:525716 if (rvh != GetRenderViewHost()) {
[email protected]420ae012009-04-24 05:16:325717 // The pending page's RenderViewHost is gone.
5718 return;
5719 }
5720
[email protected]2fcdcc32014-03-05 02:14:075721 // Ensure fullscreen mode is exited in the |delegate_| since a crashed
5722 // renderer may not have made a clean exit.
alexmos5dd617d2016-06-07 04:21:155723 if (IsFullscreenForCurrentTab())
bokanece34a82016-01-28 19:49:465724 ExitFullscreenMode(false);
[email protected]2fcdcc32014-03-05 02:14:075725
Jennifer Apacibleb4b972a2018-06-07 04:41:025726 // Ensure any video in Picture-in-Picture is exited in the |delegate_| since
5727 // a crashed renderer may not have made a clean exit.
5728 if (HasPictureInPictureVideo())
5729 ExitPictureInPicture();
5730
[email protected]698191dc2014-02-25 01:06:135731 // Cancel any visible dialogs so they are not left dangling over the sad tab.
creis89a0f782015-05-27 16:13:175732 CancelActiveAndPendingDialogs();
[email protected]698191dc2014-02-25 01:06:135733
dalecurtis3d1e1302017-02-20 22:33:255734 audio_stream_monitor_.RenderProcessGone(rvh->GetProcess()->GetID());
5735
[email protected]960b0372014-05-19 18:01:005736 // Reset the loading progress. TODO(avi): What does it mean to have a
5737 // "renderer crash" when there is more than one renderer process serving a
5738 // webpage? Once this function is called at a more granular frame level, we
5739 // probably will need to more granularly reset the state here.
5740 ResetLoadProgressState();
clamy44e84ce2016-02-22 15:38:255741 NotifyDisconnected();
5742 SetIsCrashed(status, error_code);
[email protected]960b0372014-05-19 18:01:005743
ericwilligersde386342016-10-19 02:35:095744 for (auto& observer : observers_)
5745 observer.RenderProcessGone(GetCrashedStatus());
[email protected]420ae012009-04-24 05:16:325746}
5747
[email protected]b172aee2012-04-10 17:05:265748void WebContentsImpl::RenderViewDeleted(RenderViewHost* rvh) {
ericwilligersde386342016-10-19 02:35:095749 for (auto& observer : observers_)
5750 observer.RenderViewDeleted(rvh);
[email protected]2e4633c2009-07-09 16:58:065751}
5752
sky8b00392d2015-01-10 00:30:285753void WebContentsImpl::UpdateTargetURL(RenderViewHost* render_view_host,
5754 const GURL& url) {
5755 if (fullscreen_widget_routing_id_ != MSG_ROUTING_NONE) {
Lukasz Anforowiczfd707142018-02-07 19:46:135756 // If we're in flash fullscreen (i.e. Pepper plugin fullscreen) only update
5757 // the url if it's from the fullscreen renderer.
sky8b00392d2015-01-10 00:30:285758 RenderWidgetHostView* fs = GetFullscreenRenderWidgetHostView();
avif9ab5d942015-10-15 14:05:445759 if (fs && fs->GetRenderWidgetHost() != render_view_host->GetWidget())
sky8b00392d2015-01-10 00:30:285760 return;
5761 }
Lukasz Anforowiczfd707142018-02-07 19:46:135762
5763 // In case of racey updates from multiple RenderViewHosts, the last URL should
5764 // be shown - see also some discussion in https://crbug.com/807776.
5765 if (!url.is_valid() && render_view_host != view_that_set_last_target_url_)
5766 return;
5767 view_that_set_last_target_url_ = url.is_valid() ? render_view_host : nullptr;
5768
[email protected]6934a702011-12-20 00:04:515769 if (delegate_)
avi450ce00e2014-09-24 01:58:525770 delegate_->UpdateTargetURL(this, url);
[email protected]420ae012009-04-24 05:16:325771}
5772
Lukasz Anforowiczfd707142018-02-07 19:46:135773void WebContentsImpl::ClearTargetURL() {
5774 view_that_set_last_target_url_ = nullptr;
5775 if (delegate_)
5776 delegate_->UpdateTargetURL(this, GURL());
5777}
5778
[email protected]b172aee2012-04-10 17:05:265779void WebContentsImpl::Close(RenderViewHost* rvh) {
[email protected]60780f412013-02-25 16:34:105780#if defined(OS_MACOSX)
[email protected]07707302009-11-06 00:50:295781 // The UI may be in an event-tracking loop, such as between the
5782 // mouse-down and mouse-up in text selection or a button click.
5783 // Defer the close until after tracking is complete, so that we
5784 // don't free objects out from under the UI.
[email protected]07707302009-11-06 00:50:295785 // TODO(shess): This could get more fine-grained. For instance,
5786 // closing a tab in another window while selecting text in the
5787 // current window's Omnibox should be just fine.
Christopher Cameron9ae31e0d2019-03-01 20:01:195788 if (view_->CloseTabAfterEventTrackingIfNeeded())
[email protected]07707302009-11-06 00:50:295789 return;
[email protected]60780f412013-02-25 16:34:105790#endif
[email protected]07707302009-11-06 00:50:295791
[email protected]420ae012009-04-24 05:16:325792 // Ignore this if it comes from a RenderViewHost that we aren't showing.
[email protected]151a63d2011-12-20 22:32:525793 if (delegate_ && rvh == GetRenderViewHost())
[email protected]6934a702011-12-20 00:04:515794 delegate_->CloseContents(this);
[email protected]420ae012009-04-24 05:16:325795}
5796
Avi Drissman014dec72018-06-07 02:34:015797void WebContentsImpl::RequestSetBounds(const gfx::Rect& new_bounds) {
5798 if (delegate_)
5799 delegate_->SetContentsBounds(this, new_bounds);
[email protected]420ae012009-04-24 05:16:325800}
5801
fdegansa696e5112015-04-17 01:57:595802void WebContentsImpl::DidStartLoading(FrameTreeNode* frame_tree_node,
[email protected]e3b10d12014-03-28 16:06:095803 bool to_different_document) {
Evan Stadec3b26022017-07-14 19:50:245804 LoadingStateChanged(frame_tree_node->IsMainFrame() && to_different_document,
5805 false, nullptr);
dmazzonibf8cec42015-02-08 08:28:085806
Xiyuan Xia2a66b112018-12-06 15:52:165807 // Reset the focus state from DidStartNavigation to false if a new load starts
5808 // afterward, in case loading logic triggers a FocusLocationBarByDefault call.
5809 should_focus_location_bar_by_default_ = false;
5810
dmazzonibf8cec42015-02-08 08:28:085811 // Notify accessibility that the user is navigating away from the
5812 // current document.
5813 //
5814 // TODO(dmazzoni): do this using a WebContentsObserver.
dmazzonibf8cec42015-02-08 08:28:085815 BrowserAccessibilityManager* manager =
fdegansa696e5112015-04-17 01:57:595816 frame_tree_node->current_frame_host()->browser_accessibility_manager();
dmazzonibf8cec42015-02-08 08:28:085817 if (manager)
5818 manager->UserIsNavigatingAway();
[email protected]420ae012009-04-24 05:16:325819}
5820
fdegansc73eec22015-03-19 14:47:545821void WebContentsImpl::DidStopLoading() {
dcheng59716272016-04-09 05:19:085822 std::unique_ptr<LoadNotificationDetails> details;
[email protected]420ae012009-04-24 05:16:325823
[email protected]9595fd82013-04-19 21:28:495824 // Use the last committed entry rather than the active one, in case a
5825 // pending entry has been created.
5826 NavigationEntry* entry = controller_.GetLastCommittedEntry();
[email protected]3691e5cf2014-01-22 10:16:205827 Navigator* navigator = frame_tree_.root()->navigator();
[email protected]9595fd82013-04-19 21:28:495828
[email protected]420ae012009-04-24 05:16:325829 // An entry may not exist for a stop when loading an initial blank page or
5830 // if an iframe injected by script into a blank page finishes loading.
5831 if (entry) {
[email protected]3691e5cf2014-01-22 10:16:205832 base::TimeDelta elapsed =
5833 base::TimeTicks::Now() - navigator->GetCurrentLoadStart();
[email protected]420ae012009-04-24 05:16:325834
5835 details.reset(new LoadNotificationDetails(
[email protected]36fc0392011-12-25 03:59:515836 entry->GetVirtualURL(),
[email protected]420ae012009-04-24 05:16:325837 elapsed,
5838 &controller_,
5839 controller_.GetCurrentEntryIndex()));
5840 }
5841
clamy44e84ce2016-02-22 15:38:255842 LoadingStateChanged(true, false, details.get());
[email protected]420ae012009-04-24 05:16:325843}
5844
fdegansa696e5112015-04-17 01:57:595845void WebContentsImpl::DidChangeLoadProgress() {
Nate Chapin22ea6592019-03-05 22:29:025846 if (is_being_destroyed_)
5847 return;
fdegansa696e5112015-04-17 01:57:595848 double load_progress = frame_tree_.load_progress();
5849
5850 // The delegate is notified immediately for the first and last updates. Also,
5851 // since the message loop may be pretty busy when a page is loaded, it might
5852 // not execute a posted task in a timely manner so the progress report is sent
5853 // immediately if enough time has passed.
5854 base::TimeDelta min_delay =
5855 base::TimeDelta::FromMilliseconds(kMinimumDelayBetweenLoadingUpdatesMS);
5856 bool delay_elapsed = loading_last_progress_update_.is_null() ||
5857 base::TimeTicks::Now() - loading_last_progress_update_ > min_delay;
5858
5859 if (load_progress == 0.0 || load_progress == 1.0 || delay_elapsed) {
5860 // If there is a pending task to send progress, it is now obsolete.
5861 loading_weak_factory_.InvalidateWeakPtrs();
5862
5863 // Notify the load progress change.
5864 SendChangeLoadProgress();
5865
5866 // Clean-up the states if needed.
5867 if (load_progress == 1.0)
5868 ResetLoadProgressState();
5869 return;
5870 }
5871
5872 if (loading_weak_factory_.HasWeakPtrs())
5873 return;
5874
skyostil95082a62015-06-05 19:53:075875 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
tzike2aca992017-09-05 08:50:545876 FROM_HERE,
5877 base::BindOnce(&WebContentsImpl::SendChangeLoadProgress,
5878 loading_weak_factory_.GetWeakPtr()),
fdegansa696e5112015-04-17 01:57:595879 min_delay);
5880}
5881
avid6d88b912017-01-13 00:16:005882std::vector<std::unique_ptr<NavigationThrottle>>
5883WebContentsImpl::CreateThrottlesForNavigation(
clamycbf524d42016-09-27 12:48:355884 NavigationHandle* navigation_handle) {
5885 return GetContentClient()->browser()->CreateThrottlesForNavigation(
5886 navigation_handle);
5887}
5888
clamy1e5574e2016-09-29 16:48:445889std::unique_ptr<NavigationUIData> WebContentsImpl::GetNavigationUIData(
5890 NavigationHandle* navigation_handle) {
clamy1e5574e2016-09-29 16:48:445891 return GetContentClient()->browser()->GetNavigationUIData(navigation_handle);
5892}
5893
pdr15f5b5b2017-06-20 00:58:005894void WebContentsImpl::DidCancelLoading() {
5895 controller_.DiscardNonCommittedEntries();
5896
5897 // Update the URL display.
5898 NotifyNavigationStateChanged(INVALIDATE_TYPE_URL);
5899}
5900
[email protected]59167c22013-06-03 18:07:325901void WebContentsImpl::DidAccessInitialDocument() {
[email protected]b4c84012014-04-28 19:51:105902 has_accessed_initial_document_ = true;
5903
[email protected]d1371bf8e2014-04-19 03:26:245904 // We may have left a failed browser-initiated navigation in the address bar
5905 // to let the user edit it and try again. Clear it now that content might
5906 // show up underneath it.
5907 if (!IsLoading() && controller_.GetPendingEntry())
avi45a72532015-04-07 21:01:455908 controller_.DiscardPendingEntry(false);
[email protected]d1371bf8e2014-04-19 03:26:245909
[email protected]59167c22013-06-03 18:07:325910 // Update the URL display.
thestigc4cac8f2014-09-04 21:17:505911 NotifyNavigationStateChanged(INVALIDATE_TYPE_URL);
[email protected]59167c22013-06-03 18:07:325912}
5913
alexmosbe2f4c32015-03-10 02:30:235914void WebContentsImpl::DidChangeName(RenderFrameHost* render_frame_host,
5915 const std::string& name) {
ericwilligersde386342016-10-19 02:35:095916 for (auto& observer : observers_)
5917 observer.FrameNameChanged(render_frame_host, name);
alexmosbe2f4c32015-03-10 02:30:235918}
5919
John Delaneyedd8d6c2019-01-25 00:23:575920void WebContentsImpl::DidReceiveFirstUserActivation(
5921 RenderFrameHost* render_frame_host) {
5922 for (auto& observer : observers_)
5923 observer.FrameReceivedFirstUserActivation(render_frame_host);
5924}
5925
John Delaney0146a742019-01-25 19:25:285926void WebContentsImpl::DidChangeDisplayState(RenderFrameHost* render_frame_host,
5927 bool is_display_none) {
5928 for (auto& observer : observers_)
5929 observer.FrameDisplayStateChanged(render_frame_host, is_display_none);
5930}
5931
5932void WebContentsImpl::FrameSizeChanged(RenderFrameHost* render_frame_host,
5933 const gfx::Size& frame_size) {
5934 for (auto& observer : observers_)
5935 observer.FrameSizeChanged(render_frame_host, frame_size);
5936}
5937
[email protected]39f2f9252014-05-03 00:54:015938void WebContentsImpl::DocumentOnLoadCompleted(
[email protected]50279492014-05-05 23:24:295939 RenderFrameHost* render_frame_host) {
falken52a56e32016-12-08 05:02:405940 ShowInsecureLocalhostWarningIfNeeded();
5941
creis97867aa2017-02-17 18:39:455942 is_notifying_observers_ = true;
ericwilligersde386342016-10-19 02:35:095943 for (auto& observer : observers_)
5944 observer.DocumentOnLoadCompletedInMainFrame();
creis97867aa2017-02-17 18:39:455945 is_notifying_observers_ = false;
[email protected]6578fef2013-10-14 02:35:095946
5947 // TODO(avi): Remove. http://crbug.com/170921
[email protected]8ff00d72012-10-23 19:12:215948 NotificationService::current()->Notify(
5949 NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
5950 Source<WebContents>(this),
[email protected]50279492014-05-05 23:24:295951 NotificationService::NoDetails());
[email protected]25497492010-09-11 15:15:085952}
5953
creis8eb8ef22015-11-04 22:51:385954void WebContentsImpl::UpdateStateForFrame(RenderFrameHost* render_frame_host,
5955 const PageState& page_state) {
creis8eb8ef22015-11-04 22:51:385956 // The state update affects the last NavigationEntry associated with the given
5957 // |render_frame_host|. This may not be the last committed NavigationEntry (as
5958 // in the case of an UpdateState from a frame being swapped out). We track
5959 // which entry this is in the RenderFrameHost's nav_entry_id.
5960 RenderFrameHostImpl* rfhi =
5961 static_cast<RenderFrameHostImpl*>(render_frame_host);
5962 NavigationEntryImpl* entry =
5963 controller_.GetEntryWithUniqueID(rfhi->nav_entry_id());
5964 if (!entry)
5965 return;
5966
5967 FrameNavigationEntry* frame_entry =
5968 entry->GetFrameEntry(rfhi->frame_tree_node());
5969 if (!frame_entry)
5970 return;
5971
creisd06a9422015-11-11 03:08:455972 // The SiteInstance might not match if we do a cross-process navigation with
5973 // replacement (e.g., auto-subframe), in which case the swap out of the old
5974 // RenderFrameHost runs in the background after the old FrameNavigationEntry
5975 // has already been replaced and destroyed.
5976 if (frame_entry->site_instance() != rfhi->GetSiteInstance())
5977 return;
5978
creis8eb8ef22015-11-04 22:51:385979 if (page_state == frame_entry->page_state())
5980 return; // Nothing to update.
5981
creis0cade2e2017-02-28 06:37:475982 DCHECK(page_state.IsValid()) << "Shouldn't set an empty PageState.";
nasko34634672016-07-29 18:46:065983
5984 // The document_sequence_number and item_sequence_number recorded in the
5985 // FrameNavigationEntry should not differ from the one coming with the update,
5986 // since it must come from the same document. Do not update it if a difference
5987 // is detected, as this indicates that |frame_entry| is not the correct one.
5988 ExplodedPageState exploded_state;
5989 if (!DecodePageState(page_state.ToEncodedData(), &exploded_state))
5990 return;
5991
5992 if (exploded_state.top.document_sequence_number !=
5993 frame_entry->document_sequence_number() ||
5994 exploded_state.top.item_sequence_number !=
5995 frame_entry->item_sequence_number()) {
5996 return;
5997 }
5998
nasko04a322f25a2016-09-01 00:00:345999 frame_entry->SetPageState(page_state);
creis8eb8ef22015-11-04 22:51:386000 controller_.NotifyEntryChanged(entry);
6001}
6002
[email protected]ef3adfc2014-05-11 00:04:546003void WebContentsImpl::UpdateTitle(RenderFrameHost* render_frame_host,
[email protected]ef3adfc2014-05-11 00:04:546004 const base::string16& title,
6005 base::i18n::TextDirection title_direction) {
[email protected]ef3adfc2014-05-11 00:04:546006 // Try to find the navigation entry, which might not be the current one.
avi286019ce2015-07-29 01:26:586007 // For example, it might be from a recently swapped out RFH.
avi1edc8552016-10-13 21:37:166008 NavigationEntryImpl* entry = controller_.GetEntryWithUniqueID(
creisad8c31a2016-05-24 17:07:046009 static_cast<RenderFrameHostImpl*>(render_frame_host)->nav_entry_id());
[email protected]ef3adfc2014-05-11 00:04:546010
6011 // We can handle title updates when we don't have an entry in
6012 // UpdateTitleForEntry, but only if the update is from the current RVH.
6013 // TODO(avi): Change to make decisions based on the RenderFrameHost.
avi286019ce2015-07-29 01:26:586014 if (!entry && render_frame_host != GetMainFrame())
[email protected]ef3adfc2014-05-11 00:04:546015 return;
6016
6017 // TODO(evan): make use of title_direction.
6018 // http://code.google.com/p/chromium/issues/detail?id=27094
afakhry6f0c1ec22016-07-14 13:55:136019 UpdateTitleForEntry(entry, title);
[email protected]ef3adfc2014-05-11 00:04:546020}
6021
6022void WebContentsImpl::UpdateEncoding(RenderFrameHost* render_frame_host,
6023 const std::string& encoding) {
6024 SetEncoding(encoding);
6025}
6026
[email protected]39f2f9252014-05-03 00:54:016027void WebContentsImpl::DocumentAvailableInMainFrame(
6028 RenderViewHost* render_view_host) {
ericwilligersde386342016-10-19 02:35:096029 for (auto& observer : observers_)
6030 observer.DocumentAvailableInMainFrame();
[email protected]39f2f9252014-05-03 00:54:016031}
naskoc0fceff2015-04-30 15:53:526032
[email protected]e1c3a552012-05-04 20:51:326033void WebContentsImpl::RouteCloseEvent(RenderViewHost* rvh) {
6034 // Tell the active RenderViewHost to run unload handlers and close, as long
6035 // as the request came from a RenderViewHost in the same BrowsingInstance.
6036 // In most cases, we receive this from a swapped out RenderViewHost.
6037 // It is possible to receive it from one that has just been swapped in,
6038 // in which case we might as well deliver the message anyway.
6039 if (rvh->GetSiteInstance()->IsRelatedSiteInstance(GetSiteInstance()))
naskoc0fceff2015-04-30 15:53:526040 ClosePage();
[email protected]e1c3a552012-05-04 20:51:326041}
6042
alexmose7da5a12015-04-09 02:22:166043bool WebContentsImpl::ShouldRouteMessageEvent(
6044 RenderFrameHost* target_rfh,
6045 SiteInstance* source_site_instance) const {
6046 // Allow the message if this WebContents is dedicated to a browser plugin
6047 // guest.
[email protected]0eba810b2012-10-18 03:19:366048 // Note: This check means that an embedder could theoretically receive a
6049 // postMessage from anyone (not just its own guests). However, this is
6050 // probably not a risk for apps since other pages won't have references
6051 // to App windows.
alexmose7da5a12015-04-09 02:22:166052 return GetBrowserPluginGuest() || GetBrowserPluginEmbedder();
6053}
[email protected]f546640b2012-05-15 00:03:496054
alexmos58729042015-06-18 23:20:006055void WebContentsImpl::EnsureOpenerProxiesExist(RenderFrameHost* source_rfh) {
alexmose7da5a12015-04-09 02:22:166056 WebContentsImpl* source_web_contents = static_cast<WebContentsImpl*>(
6057 WebContents::FromRenderFrameHost(source_rfh));
[email protected]f546640b2012-05-15 00:03:496058
alexmose7da5a12015-04-09 02:22:166059 if (source_web_contents) {
lazyboy6ec48b2a2015-06-29 15:18:146060 // If this message is going to outer WebContents from inner WebContents,
6061 // then we should not create a RenderView. AttachToOuterWebContentsFrame()
6062 // already created a RenderFrameProxyHost for that purpose.
6063 if (GetBrowserPluginEmbedder() &&
ekaramad1beabecc2016-09-23 20:18:176064 GuestMode::IsCrossProcessFrameGuest(source_web_contents)) {
lazyboy6ec48b2a2015-06-29 15:18:146065 return;
6066 }
6067
lfg812874d2016-02-09 22:26:136068 if (this != source_web_contents && GetBrowserPluginGuest()) {
naskob3041b98a42016-03-12 04:43:066069 // We create a RenderFrameProxyHost for the embedder in the guest's render
6070 // process but we intentionally do not expose the embedder's opener chain
6071 // to it.
6072 source_web_contents->GetRenderManager()->CreateRenderFrameProxy(
6073 GetSiteInstance());
[email protected]f546640b2012-05-15 00:03:496074 } else {
alexmos58729042015-06-18 23:20:006075 RenderFrameHostImpl* source_rfhi =
6076 static_cast<RenderFrameHostImpl*>(source_rfh);
6077 source_rfhi->frame_tree_node()->render_manager()->CreateOpenerProxies(
alexmos90325cf2015-09-02 17:18:396078 GetSiteInstance(), nullptr);
[email protected]f546640b2012-05-15 00:03:496079 }
6080 }
[email protected]f546640b2012-05-15 00:03:496081}
6082
Avi Drissmand18c51972018-08-15 18:32:096083void WebContentsImpl::ForSecurityDropFullscreen() {
Avi Drissman734edf82019-06-20 22:10:106084 // There are two chains of WebContents to kick out of fullscreen.
6085 //
6086 // Chain 1, the inner/outer WebContents chain. If an inner WebContents has
6087 // done something that requires the browser to drop fullscreen, drop
6088 // fullscreen from it and any outer WebContents that may be in fullscreen.
6089 //
6090 // Chain 2, the opener WebContents chain. If a WebContents has done something
6091 // that requires the browser to drop fullscreen, drop fullscreen from any
6092 // WebContents that was involved in the chain of opening it.
6093 //
6094 // Note that these two chains don't interact, as only a top-level WebContents
6095 // can have an opener. This simplifies things.
6096
6097 WebContents* web_contents = this;
Avi Drissmand18c51972018-08-15 18:32:096098 while (web_contents) {
6099 if (web_contents->IsFullscreenForCurrentTab())
6100 web_contents->ExitFullscreen(true);
Avi Drissman734edf82019-06-20 22:10:106101
6102 if (web_contents->HasOriginalOpener())
6103 web_contents = FromRenderFrameHost(web_contents->GetOriginalOpener());
6104 else
6105 web_contents = web_contents->GetOuterWebContents();
Avi Drissmand18c51972018-08-15 18:32:096106 }
6107}
6108
avallee9993fca2016-11-17 06:16:506109void WebContentsImpl::SetAsFocusedWebContentsIfNecessary() {
6110 // Only change focus if we are not currently focused.
6111 WebContentsImpl* old_contents = GetFocusedWebContents();
6112 if (old_contents == this)
6113 return;
6114
paulmeyerfeafc2d2017-04-25 21:46:406115 GetOutermostWebContents()->node_.SetFocusedWebContents(this);
6116
Greg Thompsonfb957602017-09-04 10:25:526117 if (!GuestMode::IsCrossProcessFrameGuest(this) && browser_plugin_guest_)
paulmeyerfeafc2d2017-04-25 21:46:406118 return;
6119
avallee9993fca2016-11-17 06:16:506120 // Send a page level blur to the old contents so that it displays inactive UI
6121 // and focus this contents to activate it.
6122 if (old_contents)
6123 old_contents->GetMainFrame()->GetRenderWidgetHost()->SetPageFocus(false);
avalleec2ff1ef2017-01-21 00:12:306124
6125 // Make sure the outer web contents knows our frame is focused. Otherwise, the
6126 // outer renderer could have the element before or after the frame element
6127 // focused which would return early without actually advancing focus.
avalleed68cdd12017-06-21 22:07:276128 FocusOuterAttachmentFrameChain();
avalleec2ff1ef2017-01-21 00:12:306129
lfg1453e412017-04-11 00:48:506130 if (ShowingInterstitialPage()) {
creisadca9862017-06-16 19:12:116131 static_cast<RenderFrameHostImpl*>(interstitial_page_->GetMainFrame())
lfg1453e412017-04-11 00:48:506132 ->GetRenderWidgetHost()
6133 ->SetPageFocus(true);
6134 } else {
6135 GetMainFrame()->GetRenderWidgetHost()->SetPageFocus(true);
6136 }
avallee9993fca2016-11-17 06:16:506137}
6138
avallee0206f782016-07-28 18:55:336139void WebContentsImpl::SetFocusedFrame(FrameTreeNode* node,
6140 SiteInstance* source) {
avallee0206f782016-07-28 18:55:336141 frame_tree_.SetFocusedFrame(node, source);
avalleed68cdd12017-06-21 22:07:276142
Lucas Furukawa Gadaniabbfa342017-08-29 19:37:576143 if (auto* inner_contents = node_.GetInnerWebContentsInFrame(node)) {
6144 // |this| is an outer WebContents and |node| represents an inner
6145 // WebContents. Transfer the focus to the inner contents if |this| is
6146 // focused.
6147 if (GetFocusedWebContents() == this)
6148 inner_contents->SetAsFocusedWebContentsIfNecessary();
6149 } else if (node_.OuterContentsFrameTreeNode() &&
6150 node_.OuterContentsFrameTreeNode()
6151 ->current_frame_host()
6152 ->GetSiteInstance() == source) {
6153 // |this| is an inner WebContents, |node| is its main FrameTreeNode and
6154 // the outer WebContents FrameTreeNode is at |source|'s SiteInstance.
6155 // Transfer the focus to the inner WebContents if the outer WebContents is
6156 // focused. This branch is used when an inner WebContents is focused through
6157 // its RenderFrameProxyHost (via FrameHostMsg_FrameFocused IPC, used to
6158 // implement the window.focus() API).
6159 if (GetFocusedWebContents() == GetOuterWebContents())
6160 SetAsFocusedWebContentsIfNecessary();
6161 } else if (!GetOuterWebContents()) {
6162 // This is an outermost WebContents.
6163 SetAsFocusedWebContentsIfNecessary();
6164 } else if (!GuestMode::IsCrossProcessFrameGuest(this) &&
6165 GetOuterWebContents()) {
6166 // TODO(lfg, paulmeyer): Allows BrowserPlugins to set themselves as the
6167 // focused WebContents. This works around a bug in FindRequestManager that
6168 // doesn't support properly traversing BrowserPlugins.
6169 SetAsFocusedWebContentsIfNecessary();
6170 }
avalleed68cdd12017-06-21 22:07:276171}
6172
Avi Drissman36f801f2018-02-01 20:06:046173void WebContentsImpl::DidCallFocus() {
6174 // Any explicit focusing of another window while this WebContents is in
6175 // fullscreen can be used to confuse the user, so drop fullscreen.
Avi Drissmand18c51972018-08-15 18:32:096176 ForSecurityDropFullscreen();
Avi Drissman36f801f2018-02-01 20:06:046177}
6178
avalleed68cdd12017-06-21 22:07:276179RenderFrameHost* WebContentsImpl::GetFocusedFrameIncludingInnerWebContents() {
6180 WebContentsImpl* contents = this;
6181 FrameTreeNode* focused_node = contents->frame_tree_.GetFocusedFrame();
6182
6183 // If there is no focused frame in the outer WebContents, we need to return
6184 // null.
6185 if (!focused_node)
6186 return nullptr;
6187
6188 // If the focused frame is embedding an inner WebContents, we must descend
6189 // into that contents. If the current WebContents does not have a focused
6190 // frame, return the main frame of this contents instead of the focused empty
6191 // frame embedding this contents.
6192 while (true) {
6193 contents = contents->node_.GetInnerWebContentsInFrame(focused_node);
6194 if (!contents)
6195 return focused_node->current_frame_host();
6196
6197 focused_node = contents->frame_tree_.GetFocusedFrame();
6198 if (!focused_node)
6199 return contents->GetMainFrame();
6200 }
avallee0206f782016-07-28 18:55:336201}
6202
Lucas Furukawa Gadaniabbfa342017-08-29 19:37:576203void WebContentsImpl::OnAdvanceFocus(RenderFrameHostImpl* source_rfh) {
6204 // When a RenderFrame needs to advance focus to a RenderFrameProxy (by hitting
6205 // TAB), the RenderFrameProxy sends an IPC to RenderFrameProxyHost. When this
6206 // RenderFrameProxyHost represents an inner WebContents, the outer WebContents
6207 // needs to focus the inner WebContents.
6208 if (GetOuterWebContents() &&
6209 GetOuterWebContents() == source_rfh->delegate()->GetAsWebContents() &&
6210 GetFocusedWebContents() == GetOuterWebContents()) {
6211 SetAsFocusedWebContentsIfNecessary();
6212 }
6213}
6214
ekaramada110f642016-12-21 19:47:286215void WebContentsImpl::OnFocusedElementChangedInFrame(
6216 RenderFrameHostImpl* frame,
6217 const gfx::Rect& bounds_in_root_view) {
6218 RenderWidgetHostViewBase* root_view =
6219 static_cast<RenderWidgetHostViewBase*>(GetRenderWidgetHostView());
6220 if (!root_view || !frame->GetView())
6221 return;
6222
6223 // Converting to screen coordinates.
6224 gfx::Point origin = bounds_in_root_view.origin();
6225 origin += root_view->GetViewBounds().OffsetFromOrigin();
6226 gfx::Rect bounds_in_screen(origin, bounds_in_root_view.size());
6227
6228 root_view->FocusedNodeChanged(frame->has_focused_editable_element(),
6229 bounds_in_screen);
6230
6231 FocusedNodeDetails details = {frame->has_focused_editable_element(),
6232 bounds_in_screen};
6233
6234 // TODO(ekaramad): We should replace this with an observer notification
6235 // (https://crbug.com/675975).
6236 NotificationService::current()->Notify(
6237 NOTIFICATION_FOCUS_CHANGED_IN_PAGE,
6238 Source<RenderViewHost>(GetRenderViewHost()),
6239 Details<FocusedNodeDetails>(&details));
6240}
6241
Lowell Manners1de5242e2019-04-25 10:18:466242bool WebContentsImpl::DidAddMessageToConsole(
6243 blink::mojom::ConsoleMessageLevel log_level,
6244 const base::string16& message,
6245 int32_t line_no,
6246 const base::string16& source_id) {
[email protected]a796f202012-05-30 14:14:256247 if (!delegate_)
6248 return false;
Lowell Manners1de5242e2019-04-25 10:18:466249 return delegate_->DidAddMessageToConsole(this, log_level, message, line_no,
avia90ae4e2016-11-11 20:49:336250 source_id);
[email protected]a796f202012-05-30 14:14:256251}
6252
Daniel Cheng90196c82018-04-25 21:49:146253void WebContentsImpl::DidReceiveInputEvent(
dominickn9c7ceaa32016-02-27 01:52:536254 RenderWidgetHostImpl* render_widget_host,
6255 const blink::WebInputEvent::Type type) {
Dominick Ng37a15052018-07-17 23:56:356256 if (!IsUserInteractionInputType(type))
Daniel Cheng90196c82018-04-25 21:49:146257 return;
6258
kenrb19221852016-04-29 17:21:406259 // Ignore unless the widget is currently in the frame tree.
6260 if (!HasMatchingWidgetHost(&frame_tree_, render_widget_host))
avic3aa8422015-11-09 20:57:226261 return;
6262
Daniel Cheng90196c82018-04-25 21:49:146263 if (type != blink::WebInputEvent::kGestureScrollBegin)
6264 last_interactive_input_event_time_ = ui::EventTimeForNow();
dominickn9c7ceaa32016-02-27 01:52:536265
Dominick Ng37a15052018-07-17 23:56:356266 for (auto& observer : observers_)
6267 observer.DidGetUserInteraction(type);
6268
6269 if (IsResourceLoadUserInteractionInputType(type))
6270 SendUserGestureForResourceDispatchHost();
dominickn2dd142dd2015-10-29 05:30:506271}
6272
Avi Drissman738ea192018-08-29 20:24:166273bool WebContentsImpl::ShouldIgnoreInputEvents() {
6274 WebContentsImpl* web_contents = this;
6275 while (web_contents) {
6276 if (web_contents->ignore_input_events_)
6277 return true;
6278 web_contents = web_contents->GetOuterWebContents();
6279 }
6280
6281 return false;
6282}
6283
avallee9993fca2016-11-17 06:16:506284void WebContentsImpl::FocusOwningWebContents(
6285 RenderWidgetHostImpl* render_widget_host) {
6286 // The PDF plugin still runs as a BrowserPlugin and must go through the
6287 // input redirection mechanism. It must not become focused direcly.
6288 if (!GuestMode::IsCrossProcessFrameGuest(this) && browser_plugin_guest_)
6289 return;
6290
6291 RenderWidgetHostImpl* focused_widget =
6292 GetFocusedRenderWidgetHost(render_widget_host);
6293
6294 if (focused_widget != render_widget_host &&
avallee8e881592016-11-19 00:39:306295 (!focused_widget ||
6296 focused_widget->delegate() != render_widget_host->delegate())) {
avallee9993fca2016-11-17 06:16:506297 SetAsFocusedWebContentsIfNecessary();
6298 }
6299}
6300
[email protected]b172aee2012-04-10 17:05:266301void WebContentsImpl::OnIgnoredUIEvent() {
[email protected]31145092011-09-30 01:49:446302 // Notify observers.
ericwilligersde386342016-10-19 02:35:096303 for (auto& observer : observers_)
6304 observer.DidGetIgnoredUIEvent();
[email protected]fa1cf0b82010-01-15 21:49:446305}
6306
avic3aa8422015-11-09 20:57:226307void WebContentsImpl::RendererUnresponsive(
Lukasz Anforowicz52b93722018-06-20 16:11:396308 RenderWidgetHostImpl* render_widget_host,
6309 base::RepeatingClosure hang_monitor_restarter) {
avic031d392017-03-03 03:09:426310 if (ShouldIgnoreUnresponsiveRenderer())
[email protected]e5fc1632011-08-08 07:51:536311 return;
6312
Lukasz Anforowicz4f0593d2018-09-21 18:33:546313 // Do not report hangs (to task manager, to hang renderer dialog, etc.) for
6314 // invisible tabs (like extension background page, background tabs). See
6315 // https://crbug.com/881812 for rationale and for choosing the visibility
6316 // (rather than process priority) as the signal here.
6317 if (GetVisibility() != Visibility::VISIBLE)
6318 return;
6319
Avi Drissman8920def2018-01-31 19:53:366320 if (!render_widget_host->renderer_initialized())
[email protected]55452902011-06-01 21:57:476321 return;
6322
Lukasz Anforowicz4f0593d2018-09-21 18:33:546323 for (auto& observer : observers_)
6324 observer.OnRendererUnresponsive(render_widget_host->GetProcess());
6325
Dave Tapuskac6146c52017-12-20 22:48:156326 if (delegate_)
Lukasz Anforowicz52b93722018-06-20 16:11:396327 delegate_->RendererUnresponsive(this, render_widget_host,
6328 std::move(hang_monitor_restarter));
[email protected]420ae012009-04-24 05:16:326329}
6330
avic3aa8422015-11-09 20:57:226331void WebContentsImpl::RendererResponsive(
6332 RenderWidgetHostImpl* render_widget_host) {
[email protected]6934a702011-12-20 00:04:516333 if (delegate_)
Avi Drissman1572e8c3c2018-02-02 19:06:366334 delegate_->RendererResponsive(this, render_widget_host);
[email protected]420ae012009-04-24 05:16:326335}
6336
Alex Moshchuk7b4f0652019-05-30 18:54:416337void WebContentsImpl::SubframeCrashed(
6338 blink::mojom::FrameVisibility visibility) {
6339 // If a subframe crashed on a hidden tab, mark the tab for reload to avoid
6340 // showing a sad frame to the user if they ever switch back to that tab. Do
6341 // this for subframes that are either visible in viewport or visible but
6342 // scrolled out of view, but skip subframes that are not rendered (e.g., via
6343 // "display:none"), since in that case the user wouldn't see a sad frame
6344 // anyway.
6345 bool did_mark_for_reload = false;
6346 if (IsHidden() && visibility != blink::mojom::FrameVisibility::kNotRendered &&
6347 base::FeatureList::IsEnabled(
6348 features::kReloadHiddenTabsWithCrashedSubframes)) {
6349 controller_.SetNeedsReload(
6350 NavigationControllerImpl::NeedsReloadType::kCrashedSubframe);
6351 did_mark_for_reload = true;
6352 UMA_HISTOGRAM_ENUMERATION(
6353 "Stability.ChildFrameCrash.TabMarkedForReload.Visibility", visibility);
6354 }
6355
6356 UMA_HISTOGRAM_BOOLEAN("Stability.ChildFrameCrash.TabMarkedForReload",
6357 did_mark_for_reload);
6358}
6359
[email protected]b172aee2012-04-10 17:05:266360void WebContentsImpl::BeforeUnloadFiredFromRenderManager(
[email protected]feaded02012-09-25 19:12:556361 bool proceed, const base::TimeTicks& proceed_time,
[email protected]420ae012009-04-24 05:16:326362 bool* proceed_to_fire_unload) {
ericwilligersde386342016-10-19 02:35:096363 for (auto& observer : observers_)
Chris Hamilton3b3e315d2018-09-19 13:16:216364 observer.BeforeUnloadFired(proceed, proceed_time);
[email protected]6934a702011-12-20 00:04:516365 if (delegate_)
6366 delegate_->BeforeUnloadFired(this, proceed, proceed_to_fire_unload);
[email protected]e80af492013-06-24 21:52:096367 // Note: |this| might be deleted at this point.
[email protected]420ae012009-04-24 05:16:326368}
6369
[email protected]58d5cfe2013-07-10 02:40:526370void WebContentsImpl::RenderProcessGoneFromRenderManager(
[email protected]3a3d47472010-07-15 21:03:546371 RenderViewHost* render_view_host) {
[email protected]443b80e2010-12-14 00:42:236372 DCHECK(crashed_status_ != base::TERMINATION_STATUS_STILL_RUNNING);
[email protected]ec6a7eb2013-04-22 17:34:226373 RenderViewTerminated(render_view_host, crashed_status_, crashed_error_code_);
[email protected]3a3d47472010-07-15 21:03:546374}
6375
Jianzhou Fengd8720c72018-01-18 03:06:306376void WebContentsImpl::UpdateRenderViewSizeForRenderManager(bool is_main_frame) {
[email protected]8643e6d2012-01-18 20:26:106377 // TODO(brettw) this is a hack. See WebContentsView::SizeContents.
Jianzhou Fengd8720c72018-01-18 03:06:306378 gfx::Size size = GetSizeForNewRenderView(is_main_frame);
[email protected]0323ee42010-02-17 22:03:266379 // 0x0 isn't a valid window size (minimal window size is 1x1) but it may be
6380 // here during container initialization and normal window size will be set
6381 // later. In case of tab duplication this resizing to 0x0 prevents setting
6382 // normal size later so just ignore it.
6383 if (!size.IsEmpty())
6384 view_->SizeContents(size);
[email protected]420ae012009-04-24 05:16:326385}
6386
[email protected]af905902013-10-01 21:38:516387void WebContentsImpl::CancelModalDialogsForRenderManager() {
6388 // We need to cancel modal dialogs when doing a process swap, since the load
avi2460c762015-04-17 15:21:546389 // deferrer would prevent us from swapping out. We also clear the state
6390 // because this is a cross-process navigation, which means that it's a new
6391 // site that should not have to pay for the sins of its predecessor.
creis89a0f782015-05-27 16:13:176392 //
Lei Zhang6410ec1a2019-01-31 21:38:586393 // Note that we don't bother telling |browser_plugin_embedder_| because the
creis89a0f782015-05-27 16:13:176394 // cross-process navigation will either destroy the browser plugins or not
6395 // require their dialogs to close.
avi5d3b8692016-10-12 22:00:466396 if (dialog_manager_) {
avi6879fcf2017-01-21 05:27:536397 dialog_manager_->CancelDialogs(this, /*reset_state=*/true);
avi5d3b8692016-10-12 22:00:466398 }
[email protected]af905902013-10-01 21:38:516399}
6400
[email protected]02d7b6e2014-06-24 21:01:506401void WebContentsImpl::NotifySwappedFromRenderManager(RenderFrameHost* old_host,
6402 RenderFrameHost* new_host,
6403 bool is_main_frame) {
6404 if (is_main_frame) {
naskof5940b9f2015-03-02 23:04:056405 NotifyViewSwapped(old_host ? old_host->GetRenderViewHost() : nullptr,
[email protected]02d7b6e2014-06-24 21:01:506406 new_host->GetRenderViewHost());
[email protected]a6b73c62013-02-11 23:05:086407
[email protected]02d7b6e2014-06-24 21:01:506408 // Make sure the visible RVH reflects the new delegate's preferences.
6409 if (delegate_)
6410 view_->SetOverscrollControllerEnabled(CanOverscrollContent());
[email protected]d56a49e2013-02-23 00:47:396411
dmazzoni62111452017-04-28 23:46:556412 RenderWidgetHostViewBase* rwhv =
6413 static_cast<RenderWidgetHostViewBase*>(GetRenderWidgetHostView());
6414 if (rwhv)
6415 rwhv->SetMainFrameAXTreeID(GetMainFrame()->GetAXTreeID());
[email protected]02d7b6e2014-06-24 21:01:506416 }
6417
Jinsuk Kim895b2c52018-07-03 06:49:166418 NotifyFrameSwapped(old_host, new_host, is_main_frame);
[email protected]3a3d47472010-07-15 21:03:546419}
6420
naskof5940b9f2015-03-02 23:04:056421void WebContentsImpl::NotifyMainFrameSwappedFromRenderManager(
Jinsuk Kim895b2c52018-07-03 06:49:166422 RenderFrameHost* old_host,
6423 RenderFrameHost* new_host) {
6424 NotifyViewSwapped(old_host ? old_host->GetRenderViewHost() : nullptr,
6425 new_host->GetRenderViewHost());
naskof5940b9f2015-03-02 23:04:056426}
6427
[email protected]b172aee2012-04-10 17:05:266428NavigationControllerImpl& WebContentsImpl::GetControllerForRenderManager() {
[email protected]d1198fd2012-08-13 22:50:196429 return GetController();
[email protected]3a3d47472010-07-15 21:03:546430}
6431
dcheng59716272016-04-09 05:19:086432std::unique_ptr<WebUIImpl> WebContentsImpl::CreateWebUIForRenderFrameHost(
carlosk37f793792014-11-27 15:08:296433 const GURL& url) {
Dan Beam055fe8f2017-10-04 23:23:366434 return CreateWebUI(url);
[email protected]420ae012009-04-24 05:16:326435}
6436
[email protected]10f417c52011-12-28 21:04:236437NavigationEntry*
[email protected]b172aee2012-04-10 17:05:266438 WebContentsImpl::GetLastCommittedNavigationEntryForRenderManager() {
[email protected]420ae012009-04-24 05:16:326439 return controller_.GetLastCommittedEntry();
6440}
6441
creisadca9862017-06-16 19:12:116442InterstitialPageImpl* WebContentsImpl::GetInterstitialForRenderManager() {
6443 return interstitial_page_;
6444}
6445
lfgbede6392015-09-11 21:54:066446void WebContentsImpl::CreateRenderWidgetHostViewForRenderManager(
6447 RenderViewHost* render_view_host) {
lfgc740d4b22016-04-15 16:45:336448 RenderWidgetHostViewBase* rwh_view =
6449 view_->CreateViewForWidget(render_view_host->GetWidget(), false);
[email protected]1a98a932009-11-17 00:12:526450
[email protected]420ae012009-04-24 05:16:326451 // Now that the RenderView has been created, we need to tell it its size.
[email protected]245f7d52011-11-28 15:36:446452 if (rwh_view)
Jianzhou Fengd8720c72018-01-18 03:06:306453 rwh_view->SetSize(GetSizeForNewRenderView(true));
lfgbede6392015-09-11 21:54:066454}
6455
6456bool WebContentsImpl::CreateRenderViewForRenderManager(
6457 RenderViewHost* render_view_host,
6458 int opener_frame_routing_id,
6459 int proxy_routing_id,
Pavel Feldman25234722017-10-11 02:49:066460 const base::UnguessableToken& devtools_frame_token,
lfgbede6392015-09-11 21:54:066461 const FrameReplicationState& replicated_frame_state) {
6462 TRACE_EVENT0("browser,navigation",
6463 "WebContentsImpl::CreateRenderViewForRenderManager");
6464
piman00cf9402017-04-03 17:09:206465 if (proxy_routing_id == MSG_ROUTING_NONE)
lfg9431efd2016-12-14 14:45:266466 CreateRenderWidgetHostViewForRenderManager(render_view_host);
lfg9431efd2016-12-14 14:45:266467
piman00cf9402017-04-03 17:09:206468 if (!static_cast<RenderViewHostImpl*>(render_view_host)
6469 ->CreateRenderView(opener_frame_routing_id, proxy_routing_id,
Pavel Feldman25234722017-10-11 02:49:066470 devtools_frame_token, replicated_frame_state,
6471 created_with_opener_)) {
[email protected]a4127722011-04-27 23:13:526472 return false;
[email protected]14392a52012-05-02 20:28:446473 }
W. James MacLean62198672019-06-04 16:46:106474 // Set the TextAutosizer state from the main frame's renderer on the new view,
6475 // but only if it's not for the main frame. Main frame renderers should create
6476 // this state themselves from up-to-date values, so we shouldn't override it
6477 // with the cached values.
6478 if (!render_view_host->GetMainFrame()) {
6479 render_view_host->Send(
6480 new PageMsg_UpdateTextAutosizerPageInfoForRemoteMainFrames(
6481 render_view_host->GetRoutingID(), text_autosizer_page_info_));
6482 }
[email protected]a4127722011-04-27 23:13:526483
Lucas Furukawa Gadaniaed1fed2017-10-13 17:34:146484 if (proxy_routing_id == MSG_ROUTING_NONE && node_.outer_web_contents())
6485 ReattachToOuterWebContentsFrame();
6486
avi2b177592014-12-10 02:08:026487 SetHistoryOffsetAndLengthForView(render_view_host,
6488 controller_.GetLastCommittedEntryIndex(),
6489 controller_.GetEntryCount());
6490
[email protected]a220b5932013-09-21 03:47:446491#if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID)
[email protected]f8233cc2011-05-31 20:24:506492 // Force a ViewMsg_Resize to be sent, needed to make plugins show up on
6493 // linux. See crbug.com/83941.
avif9ab5d942015-10-15 14:05:446494 RenderWidgetHostView* rwh_view = render_view_host->GetWidget()->GetView();
[email protected]245f7d52011-11-28 15:36:446495 if (rwh_view) {
6496 if (RenderWidgetHost* render_widget_host = rwh_view->GetRenderWidgetHost())
Fady Samuel0b911822018-04-25 13:22:166497 render_widget_host->SynchronizeVisualProperties();
[email protected]245f7d52011-11-28 15:36:446498 }
[email protected]f8233cc2011-05-31 20:24:506499#endif
6500
[email protected]420ae012009-04-24 05:16:326501 return true;
6502}
6503
[email protected]82307f6b2014-08-07 03:30:126504bool WebContentsImpl::CreateRenderFrameForRenderManager(
6505 RenderFrameHost* render_frame_host,
arthursonzogni1efc7b962019-03-14 09:36:336506 int previous_routing_id,
alexmosa181efc2015-09-03 00:39:046507 int opener_routing_id,
japhete6adf142014-10-31 00:01:496508 int parent_routing_id,
alexmosa181efc2015-09-03 00:39:046509 int previous_sibling_routing_id) {
naskob8744d22014-08-28 17:07:436510 TRACE_EVENT0("browser,navigation",
6511 "WebContentsImpl::CreateRenderFrameForRenderManager");
[email protected]82307f6b2014-08-07 03:30:126512
6513 RenderFrameHostImpl* rfh =
6514 static_cast<RenderFrameHostImpl*>(render_frame_host);
arthursonzogni1efc7b962019-03-14 09:36:336515 if (!rfh->CreateRenderFrame(previous_routing_id, opener_routing_id,
alexmosa181efc2015-09-03 00:39:046516 parent_routing_id, previous_sibling_routing_id))
[email protected]82307f6b2014-08-07 03:30:126517 return false;
6518
6519 // TODO(nasko): When RenderWidgetHost is owned by RenderFrameHost, the passed
6520 // RenderFrameHost will have to be associated with the appropriate
6521 // RenderWidgetHostView or a new one should be created here.
6522
6523 return true;
6524}
6525
mfomitchev2b8b066a2016-01-28 19:23:156526#if defined(OS_ANDROID)
[email protected]fc2b46b2014-05-03 16:33:456527
[email protected]155c7f22013-12-09 17:07:186528base::android::ScopedJavaLocalRef<jobject>
6529WebContentsImpl::GetJavaWebContents() {
mostynb042582e2015-03-16 22:13:406530 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]0e813a52014-08-13 10:34:566531 return GetWebContentsAndroid()->GetJavaObject();
6532}
[email protected]155c7f22013-12-09 17:07:186533
[email protected]0e813a52014-08-13 10:34:566534WebContentsAndroid* WebContentsImpl::GetWebContentsAndroid() {
Kevin McNee576bb5d82019-05-29 16:47:516535 if (!web_contents_android_) {
6536 web_contents_android_ = std::make_unique<WebContentsAndroid>(this);
[email protected]155c7f22013-12-09 17:07:186537 }
Kevin McNee576bb5d82019-05-29 16:47:516538 return web_contents_android_.get();
6539}
6540
6541void WebContentsImpl::ClearWebContentsAndroid() {
6542 DCHECK_CURRENTLY_ON(BrowserThread::UI);
6543 web_contents_android_.reset();
[email protected]155c7f22013-12-09 17:07:186544}
6545
paulmeyerc0b762b2016-04-13 11:55:176546void WebContentsImpl::ActivateNearestFindResult(float x,
6547 float y) {
6548 GetOrCreateFindRequestManager()->ActivateNearestFindResult(x, y);
6549}
6550
6551void WebContentsImpl::RequestFindMatchRects(int current_version) {
6552 GetOrCreateFindRequestManager()->RequestFindMatchRects(current_version);
6553}
6554
[email protected]7d244f12013-08-22 21:41:516555bool WebContentsImpl::CreateRenderViewForInitialEmptyDocument() {
nasko4c0feb62015-06-05 18:37:066556 return CreateRenderViewForRenderManager(
6557 GetRenderViewHost(), MSG_ROUTING_NONE, MSG_ROUTING_NONE,
Pavel Feldman25234722017-10-11 02:49:066558 frame_tree_.root()->devtools_frame_token(),
lfgbede6392015-09-11 21:54:066559 frame_tree_.root()->current_replication_state());
[email protected]7d244f12013-08-22 21:41:516560}
[email protected]fc2b46b2014-05-03 16:33:456561
rockot400ea35b2016-10-15 19:15:326562service_manager::InterfaceProvider* WebContentsImpl::GetJavaInterfaces() {
sammcf5f1b0f2016-09-20 23:05:116563 if (!java_interfaces_) {
rockot400ea35b2016-10-15 19:15:326564 service_manager::mojom::InterfaceProviderPtr provider;
blundelle0a9f1582016-12-20 11:23:326565 BindInterfaceRegistryForWebContents(mojo::MakeRequest(&provider), this);
rockot400ea35b2016-10-15 19:15:326566 java_interfaces_.reset(new service_manager::InterfaceProvider);
sammcf5f1b0f2016-09-20 23:05:116567 java_interfaces_->Bind(std::move(provider));
6568 }
6569 return java_interfaces_.get();
6570}
6571
Francois Doray6c26f3342018-08-24 17:17:226572#endif
6573
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:416574bool WebContentsImpl::CompletedFirstVisuallyNonEmptyPaint() {
spqchane72c3cb2017-06-30 23:02:106575 return did_first_visually_non_empty_paint_;
6576}
6577
pkotwicz75ca8ffd2016-02-16 23:10:196578void WebContentsImpl::OnDidDownloadImage(
Avi Drissmana5a52dd2018-03-27 03:39:026579 ImageDownloadCallback callback,
pkotwicz75ca8ffd2016-02-16 23:10:196580 int id,
6581 const GURL& image_url,
leon.hancbc4bc3c2016-02-26 07:08:526582 int32_t http_status_code,
leon.hanc2228532016-08-16 05:59:186583 const std::vector<SkBitmap>& images,
6584 const std::vector<gfx::Size>& original_image_sizes) {
Avi Drissmana5a52dd2018-03-27 03:39:026585 std::move(callback).Run(id, http_status_code, image_url, images,
6586 original_image_sizes);
pkotwicz75ca8ffd2016-02-16 23:10:196587}
6588
[email protected]6fba26d2014-04-29 09:38:286589void WebContentsImpl::OnDialogClosed(int render_process_id,
6590 int render_frame_id,
[email protected]b172aee2012-04-10 17:05:266591 IPC::Message* reply_msg,
[email protected]87de04b02014-04-08 22:14:496592 bool dialog_was_suppressed,
[email protected]b172aee2012-04-10 17:05:266593 bool success,
[email protected]fcf75d42013-12-03 20:11:266594 const base::string16& user_input) {
[email protected]6fba26d2014-04-29 09:38:286595 RenderFrameHostImpl* rfh = RenderFrameHostImpl::FromID(render_process_id,
6596 render_frame_id);
[email protected]87de04b02014-04-08 22:14:496597 last_dialog_suppressed_ = dialog_was_suppressed;
6598
[email protected]beb440c2009-11-06 04:08:546599 if (is_showing_before_unload_dialog_ && !success) {
clamybc6f4152016-04-01 20:14:236600 // It is possible for the current RenderFrameHost to have changed in the
6601 // meantime. Do not reset the navigation state in that case.
6602 if (rfh && rfh == rfh->frame_tree_node()->current_frame_host()) {
clamy44e84ce2016-02-22 15:38:256603 rfh->frame_tree_node()->BeforeUnloadCanceled();
clamybc6f4152016-04-01 20:14:236604 controller_.DiscardNonCommittedEntries();
6605 }
[email protected]ec8e8b02013-06-19 04:57:106606
Charles Reis15d60b132017-11-03 17:43:476607 // Update the URL display either way, to avoid showing a stale URL.
6608 NotifyNavigationStateChanged(INVALIDATE_TYPE_URL);
6609
ericwilligersde386342016-10-19 02:35:096610 for (auto& observer : observers_)
6611 observer.BeforeUnloadDialogCancelled();
[email protected]beb440c2009-11-06 04:08:546612 }
[email protected]87de04b02014-04-08 22:14:496613
[email protected]6fba26d2014-04-29 09:38:286614 if (rfh) {
avi44acfaa82017-05-03 22:55:356615 rfh->JavaScriptDialogClosed(reply_msg, success, user_input);
Pavel Feldman3c1842b2017-08-02 05:00:166616
6617 std::vector<protocol::PageHandler*> page_handlers =
6618 protocol::PageHandler::EnabledForWebContents(this);
6619 for (auto* handler : page_handlers)
6620 handler->DidCloseJavaScriptDialog(success, user_input);
6621
[email protected]6fba26d2014-04-29 09:38:286622 } else {
6623 // Don't leak the sync IPC reply if the RFH or process is gone.
6624 delete reply_msg;
6625 }
avi1c51fb62017-02-27 20:43:036626
6627 is_showing_javascript_dialog_ = false;
6628 is_showing_before_unload_dialog_ = false;
[email protected]beb440c2009-11-06 04:08:546629}
6630
[email protected]b172aee2012-04-10 17:05:266631void WebContentsImpl::SetEncoding(const std::string& encoding) {
[email protected]ef3adfc2014-05-11 00:04:546632 if (encoding == last_reported_encoding_)
6633 return;
6634 last_reported_encoding_ = encoding;
6635
jinsukkim79df88a2017-01-04 06:26:156636 canonical_encoding_ = base::GetCanonicalEncodingNameByAliasName(encoding);
[email protected]aacd7cc2009-09-02 14:51:106637}
[email protected]f45d2a72010-03-08 23:28:356638
[email protected]1ac10dca2013-08-20 20:47:046639bool WebContentsImpl::IsHidden() {
Francois Doraye6161152018-03-27 22:05:376640 return !IsBeingCaptured() && visibility_ != Visibility::VISIBLE;
[email protected]1ac10dca2013-08-20 20:47:046641}
6642
rob97250742015-12-10 17:45:156643int WebContentsImpl::GetOuterDelegateFrameTreeNodeId() {
lfg601233692017-03-06 22:45:446644 return node_.outer_contents_frame_tree_node_id();
lazyboy6ec48b2a2015-06-29 15:18:146645}
6646
ekaramadaeb3c5c2016-11-09 03:22:356647RenderWidgetHostImpl* WebContentsImpl::GetFullscreenRenderWidgetHost() const {
6648 return RenderWidgetHostImpl::FromID(fullscreen_widget_process_id_,
6649 fullscreen_widget_routing_id_);
6650}
6651
[email protected]b0936d22013-11-28 06:47:366652RenderFrameHostManager* WebContentsImpl::GetRenderManager() const {
[email protected]fa944cb82013-11-15 17:51:216653 return frame_tree_.root()->render_manager();
6654}
6655
[email protected]f8497342013-02-05 22:15:026656BrowserPluginGuest* WebContentsImpl::GetBrowserPluginGuest() const {
[email protected]7a846df2012-09-20 19:17:396657 return browser_plugin_guest_.get();
6658}
6659
Albert J. Wongc55cc64d2018-10-12 02:50:046660void WebContentsImpl::SetBrowserPluginGuest(
6661 std::unique_ptr<BrowserPluginGuest> guest) {
6662 DCHECK(!browser_plugin_guest_);
6663 DCHECK(guest);
6664 browser_plugin_guest_ = std::move(guest);
[email protected]738f57a2013-06-29 21:06:546665}
6666
Aidan Wolter52bcc12a2018-11-14 03:27:296667base::UnguessableToken WebContentsImpl::GetAudioGroupId() {
6668 if (!base::FeatureList::IsEnabled(features::kAudioServiceAudioStreams))
6669 return base::UnguessableToken::Null();
6670
6671 return GetAudioStreamFactory()->group_id();
6672}
6673
[email protected]f8497342013-02-05 22:15:026674BrowserPluginEmbedder* WebContentsImpl::GetBrowserPluginEmbedder() const {
[email protected]7a846df2012-09-20 19:17:396675 return browser_plugin_embedder_.get();
6676}
[email protected]8ff00d72012-10-23 19:12:216677
fsamuel60b42282015-03-10 03:29:146678void WebContentsImpl::CreateBrowserPluginEmbedderIfNecessary() {
6679 if (browser_plugin_embedder_)
6680 return;
6681 browser_plugin_embedder_.reset(BrowserPluginEmbedder::Create(this));
6682}
6683
Jianzhou Fengd8720c72018-01-18 03:06:306684gfx::Size WebContentsImpl::GetSizeForNewRenderView(bool is_main_frame) {
[email protected]dc0fd432013-08-27 15:29:216685 gfx::Size size;
Jianzhou Fengd8720c72018-01-18 03:06:306686 if (is_main_frame)
6687 size = device_emulation_size_;
6688 if (size.IsEmpty() && delegate_)
[email protected]dc0fd432013-08-27 15:29:216689 size = delegate_->GetSizeForNewRenderView(this);
6690 if (size.IsEmpty())
[email protected]fc2b46b2014-05-03 16:33:456691 size = GetContainerBounds().size();
[email protected]dc0fd432013-08-27 15:29:216692 return size;
6693}
6694
[email protected]ae7eeda2014-07-04 01:53:266695void WebContentsImpl::OnFrameRemoved(RenderFrameHost* render_frame_host) {
ericwilligersde386342016-10-19 02:35:096696 for (auto& observer : observers_)
6697 observer.FrameDeleted(render_frame_host);
[email protected]9b159a52013-10-03 17:24:556698}
6699
[email protected]222f5822014-02-05 23:40:496700void WebContentsImpl::OnPreferredSizeChanged(const gfx::Size& old_size) {
6701 if (!delegate_)
6702 return;
6703 const gfx::Size new_size = GetPreferredSize();
6704 if (new_size != old_size)
6705 delegate_->UpdatePreferredSize(this, new_size);
6706}
6707
Dominick Ng37a15052018-07-17 23:56:356708void WebContentsImpl::SendUserGestureForResourceDispatchHost() {
6709 // This is null in unittests. =(
Daniel Cheng90196c82018-04-25 21:49:146710 ResourceDispatcherHostImpl* rdh = ResourceDispatcherHostImpl::Get();
Dominick Ng37a15052018-07-17 23:56:356711
6712 if (rdh)
Daniel Cheng90196c82018-04-25 21:49:146713 rdh->OnUserGesture();
6714}
6715
Dan Beam055fe8f2017-10-04 23:23:366716std::unique_ptr<WebUIImpl> WebContentsImpl::CreateWebUI(const GURL& url) {
Jeremy Roman04f27c372017-10-27 15:20:556717 std::unique_ptr<WebUIImpl> web_ui = std::make_unique<WebUIImpl>(this);
Lei Zhangee3b63ad2018-06-06 23:25:316718 std::unique_ptr<WebUIController> controller(
avie865b1d2016-10-24 19:42:596719 WebUIControllerFactoryRegistry::GetInstance()
Lei Zhangee3b63ad2018-06-06 23:25:316720 ->CreateWebUIControllerForURL(web_ui.get(), url));
dbeama1b926a2015-08-31 23:17:516721 if (controller) {
Jeremy Roman04f27c372017-10-27 15:20:556722 web_ui->AddMessageHandler(std::make_unique<GenericHandler>());
Lei Zhangee3b63ad2018-06-06 23:25:316723 web_ui->SetController(std::move(controller));
dbeama1b926a2015-08-31 23:17:516724 return web_ui;
6725 }
6726
avie865b1d2016-10-24 19:42:596727 return nullptr;
dbeama1b926a2015-08-31 23:17:516728}
6729
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:416730FindRequestManager* WebContentsImpl::GetFindRequestManager() {
6731 for (auto* contents = this; contents;
paulmeyerfeafc2d2017-04-25 21:46:406732 contents = contents->GetOuterWebContents()) {
6733 if (contents->find_request_manager_)
6734 return contents->find_request_manager_.get();
6735 }
6736
6737 return nullptr;
6738}
6739
paulmeyerc0b762b2016-04-13 11:55:176740FindRequestManager* WebContentsImpl::GetOrCreateFindRequestManager() {
paulmeyerfeafc2d2017-04-25 21:46:406741 if (FindRequestManager* manager = GetFindRequestManager())
6742 return manager;
6743
Ehsan Karamad6beb2ea2018-11-25 18:15:136744 DCHECK(!browser_plugin_guest_ || GetOuterWebContents());
6745
paulmeyerfeafc2d2017-04-25 21:46:406746 // No existing FindRequestManager found, so one must be created.
6747 find_request_manager_.reset(new FindRequestManager(this));
6748
6749 // Concurrent find sessions must not overlap, so destroy any existing
6750 // FindRequestManagers in any inner WebContentses.
Lucas Furukawa Gadani2ec00c82018-12-14 15:53:166751 for (WebContents* contents : GetWebContentsAndAllInner()) {
6752 auto* web_contents_impl = static_cast<WebContentsImpl*>(contents);
6753 if (web_contents_impl == this)
paulmeyerfeafc2d2017-04-25 21:46:406754 continue;
Lucas Furukawa Gadani2ec00c82018-12-14 15:53:166755 if (web_contents_impl->find_request_manager_) {
6756 web_contents_impl->find_request_manager_->StopFinding(
6757 STOP_FIND_ACTION_CLEAR_SELECTION);
6758 web_contents_impl->find_request_manager_.release();
paulmeyerfeafc2d2017-04-25 21:46:406759 }
6760 }
paulmeyerc0b762b2016-04-13 11:55:176761
6762 return find_request_manager_.get();
6763}
6764
6765void WebContentsImpl::NotifyFindReply(int request_id,
6766 int number_of_matches,
6767 const gfx::Rect& selection_rect,
6768 int active_match_ordinal,
6769 bool final_update) {
Sebastien Marchandcf97ed52017-07-24 23:50:346770 if (delegate_ && !is_being_destroyed_ &&
Lukasz Anforowicze9ae3722017-09-29 17:37:386771 !GetMainFrame()->GetProcess()->FastShutdownStarted()) {
paulmeyerc0b762b2016-04-13 11:55:176772 delegate_->FindReply(this, request_id, number_of_matches, selection_rect,
6773 active_match_ordinal, final_update);
6774 }
6775}
6776
ortuno467e5792016-06-10 04:32:396777void WebContentsImpl::IncrementBluetoothConnectedDeviceCount() {
6778 // Trying to invalidate the tab state while being destroyed could result in a
6779 // use after free.
6780 if (IsBeingDestroyed()) {
6781 return;
6782 }
6783 // Notify for UI updates if the state changes.
6784 bluetooth_connected_device_count_++;
6785 if (bluetooth_connected_device_count_ == 1) {
6786 NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB);
6787 }
6788}
6789
6790void WebContentsImpl::DecrementBluetoothConnectedDeviceCount() {
6791 // Trying to invalidate the tab state while being destroyed could result in a
6792 // use after free.
6793 if (IsBeingDestroyed()) {
6794 return;
6795 }
6796 // Notify for UI updates if the state changes.
Lukasz Anforowicze9ae3722017-09-29 17:37:386797 DCHECK_NE(bluetooth_connected_device_count_, 0u);
ortuno467e5792016-06-10 04:32:396798 bluetooth_connected_device_count_--;
6799 if (bluetooth_connected_device_count_ == 0) {
6800 NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB);
6801 }
6802}
6803
Reilly Grant5e7c79b22019-04-09 17:26:206804void WebContentsImpl::IncrementSerialActiveFrameCount() {
6805 // Trying to invalidate the tab state while being destroyed could result in a
6806 // use after free.
6807 if (IsBeingDestroyed())
6808 return;
6809
6810 // Notify for UI updates if the state changes.
6811 serial_active_frame_count_++;
6812 if (serial_active_frame_count_ == 1)
6813 NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB);
6814}
6815
6816void WebContentsImpl::DecrementSerialActiveFrameCount() {
6817 // Trying to invalidate the tab state while being destroyed could result in a
6818 // use after free.
6819 if (IsBeingDestroyed())
6820 return;
6821
6822 // Notify for UI updates if the state changes.
6823 DCHECK_NE(0u, serial_active_frame_count_);
6824 serial_active_frame_count_--;
6825 if (serial_active_frame_count_ == 0)
6826 NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB);
6827}
6828
Marijn Kruisselbrinkf1714aa22019-07-02 03:45:336829void WebContentsImpl::IncrementNativeFileSystemDirectoryHandleCount() {
6830 // Trying to invalidate the tab state while being destroyed could result in a
6831 // use after free.
6832 if (IsBeingDestroyed())
6833 return;
6834
6835 // Notify for UI updates if the state changes. Need both TYPE_TAB and TYPE_URL
6836 // to update both the tab-level usage indicator and the usage indicator in the
6837 // omnibox.
6838 native_file_system_directory_handle_count_++;
6839 if (native_file_system_directory_handle_count_ == 1) {
6840 NotifyNavigationStateChanged(static_cast<content::InvalidateTypes>(
6841 INVALIDATE_TYPE_TAB | INVALIDATE_TYPE_URL));
6842 }
6843}
6844
6845void WebContentsImpl::DecrementNativeFileSystemDirectoryHandleCount() {
6846 // Trying to invalidate the tab state while being destroyed could result in a
6847 // use after free.
6848 if (IsBeingDestroyed())
6849 return;
6850
6851 // Notify for UI updates if the state changes. Need both TYPE_TAB and TYPE_URL
6852 // to update both the tab-level usage indicator and the usage indicator in the
6853 // omnibox.
6854 DCHECK_NE(0u, native_file_system_directory_handle_count_);
6855 native_file_system_directory_handle_count_--;
6856 if (native_file_system_directory_handle_count_ == 0) {
6857 NotifyNavigationStateChanged(static_cast<content::InvalidateTypes>(
6858 INVALIDATE_TYPE_TAB | INVALIDATE_TYPE_URL));
6859 }
6860}
6861
6862void WebContentsImpl::IncrementWritableNativeFileSystemHandleCount() {
6863 // Trying to invalidate the tab state while being destroyed could result in a
6864 // use after free.
6865 if (IsBeingDestroyed())
6866 return;
6867
6868 // Notify for UI updates if the state changes. Need both TYPE_TAB and TYPE_URL
6869 // to update both the tab-level usage indicator and the usage indicator in the
6870 // omnibox.
6871 native_file_system_writable_handle_count_++;
6872 if (native_file_system_writable_handle_count_ == 1) {
6873 NotifyNavigationStateChanged(static_cast<content::InvalidateTypes>(
6874 INVALIDATE_TYPE_TAB | INVALIDATE_TYPE_URL));
6875 }
6876}
6877
6878void WebContentsImpl::DecrementWritableNativeFileSystemHandleCount() {
6879 // Trying to invalidate the tab state while being destroyed could result in a
6880 // use after free.
6881 if (IsBeingDestroyed())
6882 return;
6883
6884 // Notify for UI updates if the state changes. Need both TYPE_TAB and TYPE_URL
6885 // to update both the tab-level usage indicator and the usage indicator in the
6886 // omnibox.
6887 DCHECK_NE(0u, native_file_system_writable_handle_count_);
6888 native_file_system_writable_handle_count_--;
6889 if (native_file_system_writable_handle_count_ == 0) {
6890 NotifyNavigationStateChanged(static_cast<content::InvalidateTypes>(
6891 INVALIDATE_TYPE_TAB | INVALIDATE_TYPE_URL));
6892 }
6893}
6894
zqzhang8ac49002017-03-16 21:51:356895void WebContentsImpl::SetHasPersistentVideo(bool has_persistent_video) {
6896 if (has_persistent_video_ == has_persistent_video)
mlamouri5cd9ae82017-02-18 11:05:096897 return;
6898
zqzhang8ac49002017-03-16 21:51:356899 has_persistent_video_ = has_persistent_video;
mlamouri5cd9ae82017-02-18 11:05:096900 NotifyPreferencesChanged();
zqzhang8ac49002017-03-16 21:51:356901 media_web_contents_observer()->RequestPersistentVideo(has_persistent_video);
mlamouri5cd9ae82017-02-18 11:05:096902}
6903
Ian Vollick5d5c37f2019-06-19 22:50:576904void WebContentsImpl::SetSpatialNavigationDisabled(bool disabled) {
6905 if (is_spatial_navigation_disabled_ == disabled)
6906 return;
6907
6908 is_spatial_navigation_disabled_ = disabled;
6909 NotifyPreferencesChanged();
6910}
6911
paulmeyerfeafc2d2017-04-25 21:46:406912void WebContentsImpl::BrowserPluginGuestWillDetach() {
6913 WebContentsImpl* outermost = GetOutermostWebContents();
6914 if (this != outermost && ContainsOrIsFocusedWebContents())
6915 outermost->SetAsFocusedWebContentsIfNecessary();
6916}
6917
Becca Hughes112832e2019-06-11 17:19:026918PictureInPictureResult WebContentsImpl::EnterPictureInPicture(
Mounir Lamouri11e9ef42018-05-22 03:10:166919 const viz::SurfaceId& surface_id,
6920 const gfx::Size& natural_size) {
François Beaufortf09e4462019-01-18 14:34:516921 return delegate_
6922 ? delegate_->EnterPictureInPicture(this, surface_id, natural_size)
Becca Hughes112832e2019-06-11 17:19:026923 : PictureInPictureResult::kNotSupported;
Mounir Lamouri11e9ef42018-05-22 03:10:166924}
6925
Mounir Lamouri6d759e12018-05-16 20:01:306926void WebContentsImpl::ExitPictureInPicture() {
6927 if (delegate_)
6928 delegate_->ExitPictureInPicture();
6929}
6930
paulmeyerc0b762b2016-04-13 11:55:176931#if defined(OS_ANDROID)
6932void WebContentsImpl::NotifyFindMatchRectsReply(
6933 int version,
6934 const std::vector<gfx::RectF>& rects,
6935 const gfx::RectF& active_rect) {
6936 if (delegate_)
6937 delegate_->FindMatchRectsReply(this, version, rects, active_rect);
6938}
6939#endif
6940
[email protected]34ff1cfc2014-08-20 06:16:056941void WebContentsImpl::SetForceDisableOverscrollContent(bool force_disable) {
6942 force_disable_overscroll_content_ = force_disable;
6943 if (view_)
6944 view_->SetOverscrollControllerEnabled(CanOverscrollContent());
6945}
6946
Jianzhou Fengd8720c72018-01-18 03:06:306947bool WebContentsImpl::SetDeviceEmulationSize(const gfx::Size& new_size) {
6948 device_emulation_size_ = new_size;
6949 RenderWidgetHostView* rwhv = GetMainFrame()->GetView();
6950
6951 const gfx::Size current_size = rwhv->GetViewBounds().size();
6952 if (view_size_before_emulation_.IsEmpty())
6953 view_size_before_emulation_ = current_size;
6954
6955 if (current_size != new_size)
6956 rwhv->SetSize(new_size);
6957
6958 return current_size != new_size;
6959}
6960
6961void WebContentsImpl::ClearDeviceEmulationSize() {
6962 RenderWidgetHostView* rwhv = GetMainFrame()->GetView();
6963 // WebContentsView could get resized during emulation, which also resizes
6964 // RWHV. If it happens, assume user would like to keep using the size after
6965 // emulation.
6966 // TODO(jzfeng): Prohibit resizing RWHV through any other means (at least when
6967 // WebContentsView size changes).
Dmitry Gozmanc63707182018-03-06 00:09:396968 if (!view_size_before_emulation_.IsEmpty() && rwhv &&
6969 rwhv->GetViewBounds().size() == device_emulation_size_) {
Jianzhou Fengd8720c72018-01-18 03:06:306970 rwhv->SetSize(view_size_before_emulation_);
Dmitry Gozmanc63707182018-03-06 00:09:396971 }
Jianzhou Fengd8720c72018-01-18 03:06:306972 device_emulation_size_ = gfx::Size();
6973 view_size_before_emulation_ = gfx::Size();
6974}
6975
Max Morin5bc74f52018-05-09 07:00:216976ForwardingAudioStreamFactory* WebContentsImpl::GetAudioStreamFactory() {
6977 if (!audio_stream_factory_) {
6978 audio_stream_factory_.emplace(
6979 this,
Max Morinddebb972018-09-20 10:04:146980 // BrowserMainLoop::GetInstance() may be null in unit tests.
6981 BrowserMainLoop::GetInstance()
6982 ? static_cast<media::UserInputMonitorBase*>(
6983 BrowserMainLoop::GetInstance()->user_input_monitor())
6984 : nullptr,
Ken Rockotfd03e682019-06-20 21:13:546985 GetSystemConnector()->Clone(), AudioStreamBrokerFactory::CreateImpl());
Max Morin5bc74f52018-05-09 07:00:216986 }
6987
6988 return &*audio_stream_factory_;
6989}
6990
dalecurtis88c240072015-12-09 02:11:186991void WebContentsImpl::MediaStartedPlaying(
billorr21c005d2016-11-17 03:57:046992 const WebContentsObserver::MediaPlayerInfo& media_info,
Mounir Lamouri0f5bdf52019-04-04 16:32:356993 const MediaPlayerId& id) {
billorr21c005d2016-11-17 03:57:046994 if (media_info.has_video)
6995 currently_playing_video_count_++;
6996
ericwilligersde386342016-10-19 02:35:096997 for (auto& observer : observers_)
billorr21c005d2016-11-17 03:57:046998 observer.MediaStartedPlaying(media_info, id);
dalecurtis88c240072015-12-09 02:11:186999}
7000
7001void WebContentsImpl::MediaStoppedPlaying(
billorr21c005d2016-11-17 03:57:047002 const WebContentsObserver::MediaPlayerInfo& media_info,
Mounir Lamouri0f5bdf52019-04-04 16:32:357003 const MediaPlayerId& id,
Becca Hughes220bfc102017-11-14 18:24:077004 WebContentsObserver::MediaStoppedReason reason) {
billorr21c005d2016-11-17 03:57:047005 if (media_info.has_video)
7006 currently_playing_video_count_--;
7007
ericwilligersde386342016-10-19 02:35:097008 for (auto& observer : observers_)
Becca Hughes220bfc102017-11-14 18:24:077009 observer.MediaStoppedPlaying(media_info, id, reason);
billorr21c005d2016-11-17 03:57:047010}
7011
Mounir Lamouri0f5bdf52019-04-04 16:32:357012void WebContentsImpl::MediaResized(const gfx::Size& size,
7013 const MediaPlayerId& id) {
peconn257951522017-06-09 18:24:597014 cached_video_sizes_[id] = size;
7015
7016 for (auto& observer : observers_)
7017 observer.MediaResized(size, id);
7018}
7019
Peter E Conneccb34c22017-09-08 09:37:587020void WebContentsImpl::MediaEffectivelyFullscreenChanged(bool is_fullscreen) {
7021 for (auto& observer : observers_)
7022 observer.MediaEffectivelyFullscreenChanged(is_fullscreen);
7023}
7024
Peter Conn31c726a2017-08-17 11:07:347025base::Optional<gfx::Size> WebContentsImpl::GetFullscreenVideoSize() {
Mounir Lamouri0f5bdf52019-04-04 16:32:357026 base::Optional<MediaPlayerId> id =
Peter Conn31c726a2017-08-17 11:07:347027 media_web_contents_observer_->GetFullscreenVideoMediaPlayerId();
Jan Wilken Dörrie531be7ca2019-06-07 10:05:577028 if (id && base::Contains(cached_video_sizes_, id.value()))
Peter Conn31c726a2017-08-17 11:07:347029 return base::Optional<gfx::Size>(cached_video_sizes_[id.value()]);
Lei Zhang6410ec1a2019-01-31 21:38:587030 return base::nullopt;
peconn257951522017-06-09 18:24:597031}
7032
billorr21c005d2016-11-17 03:57:047033int WebContentsImpl::GetCurrentlyPlayingVideoCount() {
7034 return currently_playing_video_count_;
dalecurtis88c240072015-12-09 02:11:187035}
7036
Raymond Toye413650c2018-08-31 00:12:477037void WebContentsImpl::AudioContextPlaybackStarted(RenderFrameHost* host,
7038 int context_id) {
7039 WebContentsObserver::AudioContextId audio_context_id(host, context_id);
7040 for (auto& observer : observers_)
7041 observer.AudioContextPlaybackStarted(audio_context_id);
7042}
7043
7044void WebContentsImpl::AudioContextPlaybackStopped(RenderFrameHost* host,
7045 int context_id) {
7046 WebContentsObserver::AudioContextId audio_context_id(host, context_id);
7047 for (auto& observer : observers_)
7048 observer.AudioContextPlaybackStopped(audio_context_id);
7049}
7050
Ehsan Karamad1c631762019-05-13 23:04:017051RenderFrameHostImpl* WebContentsImpl::GetMainFrameForInnerDelegate(
7052 FrameTreeNode* frame_tree_node) {
7053 if (auto* web_contents = node_.GetInnerWebContentsInFrame(frame_tree_node))
7054 return web_contents->GetMainFrame();
7055 return nullptr;
7056}
7057
Francois Doraye6161152018-03-27 22:05:377058void WebContentsImpl::UpdateWebContentsVisibility(Visibility visibility) {
Francois Doray3f2afbd2018-04-06 19:18:187059 // Occlusion is disabled when |features::kWebContentsOcclusion| is disabled
7060 // (for power and speed impact assessment) or when
7061 // |switches::kDisableBackgroundingOccludedWindowsForTesting| is specified on
7062 // the command line (to avoid flakiness in browser tests).
7063 const bool occlusion_is_disabled =
7064 !base::FeatureList::IsEnabled(features::kWebContentsOcclusion) ||
Francois Doraye6161152018-03-27 22:05:377065 base::CommandLine::ForCurrentProcess()->HasSwitch(
7066 switches::kDisableBackgroundingOccludedWindowsForTesting);
7067 if (occlusion_is_disabled && visibility == Visibility::OCCLUDED)
7068 visibility = Visibility::VISIBLE;
7069
tapted65ff2ea72016-03-01 23:39:007070 if (!did_first_set_visible_) {
Francois Doraye6161152018-03-27 22:05:377071 if (visibility == Visibility::VISIBLE) {
7072 // A WebContents created with CreateParams::initially_hidden = false
7073 // starts with GetVisibility() == Visibility::VISIBLE even though it is
7074 // not really visible. Call WasShown() when it becomes visible for real as
7075 // the page load mechanism and some WebContentsObserver rely on that.
tapted65ff2ea72016-03-01 23:39:007076 WasShown();
Francois Dorayfe4a1772018-02-17 04:17:097077 did_first_set_visible_ = true;
tapted65ff2ea72016-03-01 23:39:007078 }
Francois Doraye6161152018-03-27 22:05:377079
7080 // Trust the initial visibility of the WebContents and do not switch it to
7081 // HIDDEN or OCCLUDED before it becomes VISIBLE for real. Doing so would
7082 // result in destroying resources that would immediately be recreated (e.g.
7083 // UpdateWebContents(HIDDEN) can be called when a WebContents is added to a
7084 // hidden window that is about to be shown).
7085
tapted65ff2ea72016-03-01 23:39:007086 return;
7087 }
Francois Doraye6161152018-03-27 22:05:377088
7089 if (visibility == visibility_)
tapted65ff2ea72016-03-01 23:39:007090 return;
7091
Francois Doraye6161152018-03-27 22:05:377092 if (visibility == Visibility::VISIBLE)
tapted65ff2ea72016-03-01 23:39:007093 WasShown();
Francois Doraye6161152018-03-27 22:05:377094 else if (visibility == Visibility::OCCLUDED)
7095 WasOccluded();
tapted65ff2ea72016-03-01 23:39:007096 else
7097 WasHidden();
7098}
7099
aelias5252baa2016-04-10 01:18:027100void WebContentsImpl::UpdateOverridingUserAgent() {
mlamouri5cd9ae82017-02-18 11:05:097101 NotifyPreferencesChanged();
aelias5252baa2016-04-10 01:18:027102}
7103
avid53461d2016-02-25 17:15:047104void WebContentsImpl::SetJavaScriptDialogManagerForTesting(
7105 JavaScriptDialogManager* dialog_manager) {
7106 dialog_manager_ = dialog_manager;
7107}
7108
rockotf62002a2016-09-15 00:08:597109void WebContentsImpl::RemoveBindingSet(const std::string& interface_name) {
7110 auto it = binding_sets_.find(interface_name);
7111 if (it != binding_sets_.end())
7112 binding_sets_.erase(it);
7113}
7114
nzolghadrd87a308d2016-12-07 15:45:567115bool WebContentsImpl::AddDomainInfoToRapporSample(rappor::Sample* sample) {
nzolghadrbbffc6c2017-05-25 17:56:397116 // Here we associate this metric to the main frame URL regardless of what
7117 // caused it.
nzolghadrd87a308d2016-12-07 15:45:567118 sample->SetStringField("Domain", ::rappor::GetDomainAndRegistrySampleFromGURL(
7119 GetLastCommittedURL()));
nzolghadrd87a308d2016-12-07 15:45:567120 return true;
7121}
7122
ekaramada110f642016-12-21 19:47:287123void WebContentsImpl::FocusedNodeTouched(bool editable) {
7124#if defined(OS_WIN)
ekaramada110f642016-12-21 19:47:287125 RenderWidgetHostView* view = GetRenderWidgetHostView();
7126 if (!view)
7127 return;
Dave Tapuska2315d8f2018-03-06 17:52:557128 view->FocusedNodeTouched(editable);
ekaramada110f642016-12-21 19:47:287129#endif
7130}
7131
falken52a56e32016-12-08 05:02:407132void WebContentsImpl::ShowInsecureLocalhostWarningIfNeeded() {
7133 bool allow_localhost = base::CommandLine::ForCurrentProcess()->HasSwitch(
7134 switches::kAllowInsecureLocalhost);
7135 if (!allow_localhost)
7136 return;
7137
7138 content::NavigationEntry* entry = GetController().GetLastCommittedEntry();
Rob Wuf79b3ba2018-01-14 01:54:317139 if (!entry || !net::IsLocalhost(entry->GetURL()))
falken52a56e32016-12-08 05:02:407140 return;
7141
7142 content::SSLStatus ssl_status = entry->GetSSL();
7143 bool is_cert_error = net::IsCertStatusError(ssl_status.cert_status) &&
7144 !net::IsCertStatusMinorError(ssl_status.cert_status);
7145 if (!is_cert_error)
7146 return;
7147
7148 GetMainFrame()->AddMessageToConsole(
Abhijeet Kandalkara5928a0a2019-03-13 05:04:427149 blink::mojom::ConsoleMessageLevel::kWarning,
falken52a56e32016-12-08 05:02:407150 base::StringPrintf("This site does not have a valid SSL "
7151 "certificate! Without SSL, your site's and "
7152 "visitors' data is vulnerable to theft and "
7153 "tampering. Get a valid SSL certificate before"
7154 " releasing your website to the public."));
7155}
7156
ekaramadf6750aa2017-06-06 18:29:427157bool WebContentsImpl::IsShowingContextMenuOnPage() const {
7158 return showing_context_menu_;
7159}
7160
Min Qina904f3302018-02-13 23:33:347161download::DownloadUrlParameters::RequestHeadersType
7162WebContentsImpl::ParseDownloadHeaders(const std::string& headers) {
7163 download::DownloadUrlParameters::RequestHeadersType request_headers;
Megan Jablonski2f6a4c52017-07-10 23:01:257164 for (const base::StringPiece& key_value : base::SplitStringPiece(
7165 headers, "\r\n", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY)) {
7166 std::vector<std::string> pair = base::SplitString(
7167 key_value, ":", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
7168 if (2ul == pair.size())
7169 request_headers.push_back(make_pair(pair[0], pair[1]));
7170 }
7171 return request_headers;
7172}
7173
lukasza6f8ac622017-06-06 03:10:207174void WebContentsImpl::SetOpenerForNewContents(FrameTreeNode* opener,
7175 bool opener_suppressed) {
7176 if (opener) {
7177 FrameTreeNode* new_root = GetFrameTree()->root();
7178
7179 // For the "original opener", track the opener's main frame instead, because
7180 // if the opener is a subframe, the opener tracking could be easily bypassed
7181 // by spawning from a subframe and deleting the subframe.
7182 // https://crbug.com/705316
7183 new_root->SetOriginalOpener(opener->frame_tree()->root());
7184
7185 if (!opener_suppressed) {
7186 new_root->SetOpener(opener);
7187 created_with_opener_ = true;
7188 }
7189 }
7190}
7191
Mounir Lamouri0f5bdf52019-04-04 16:32:357192void WebContentsImpl::MediaMutedStatusChanged(const MediaPlayerId& id,
7193 bool muted) {
Becca Hughes9f6fd4b82017-06-15 10:01:407194 for (auto& observer : observers_)
7195 observer.MediaMutedStatusChanged(id, muted);
7196}
7197
EhsanK2075c7e2017-08-21 02:42:397198void WebContentsImpl::SetVisibilityForChildViews(bool visible) {
7199 GetMainFrame()->SetVisibilityForChildViews(visible);
7200}
7201
Findit72e2f8a2019-06-29 00:07:587202void WebContentsImpl::OnDarkModeChanged(bool dark_mode) {
7203 NotifyPreferencesChanged();
Rune Lillesveen5d8c49982019-04-24 19:07:307204}
7205
[email protected]8ff00d72012-10-23 19:12:217206} // namespace content