blob: 3731fda2e7d33333a9b926a21550528e1254af16 [file] [log] [blame]
[email protected]de7d61ff2013-08-20 11:30:411// Copyright 2013 The Chromium Authors. All rights reserved.
[email protected]9fbd3f862011-09-20 23:31:342// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]de7d61ff2013-08-20 11:30:415#include "content/shell/browser/shell.h"
[email protected]9fbd3f862011-09-20 23:31:346
avi66a07722015-12-25 23:38:127#include <stddef.h>
8
Lukasz Anforowicz52b93722018-06-20 16:11:399#include <map>
10#include <string>
11#include <utility>
12
[email protected]efb5f572012-01-29 10:57:3313#include "base/command_line.h"
skyostil95082a62015-06-05 19:53:0714#include "base/location.h"
avi66a07722015-12-25 23:38:1215#include "base/macros.h"
Wez9a58a152018-06-07 18:59:3316#include "base/no_destructor.h"
fdoray896bea12016-06-10 15:52:0117#include "base/run_loop.h"
skyostil95082a62015-06-05 19:53:0718#include "base/single_thread_task_runner.h"
[email protected]21aa99682013-06-11 07:17:0119#include "base/strings/string_number_conversions.h"
20#include "base/strings/string_util.h"
21#include "base/strings/stringprintf.h"
[email protected]74ebfb12013-06-07 20:48:0022#include "base/strings/utf_string_conversions.h"
gab30f26df2016-05-11 19:37:5523#include "base/threading/thread_task_runner_handle.h"
avi66a07722015-12-25 23:38:1224#include "build/build_config.h"
[email protected]b50452f2014-08-18 12:31:4425#include "content/public/browser/devtools_agent_host.h"
[email protected]0b659b32012-03-26 21:29:3226#include "content/public/browser/navigation_controller.h"
[email protected]b7c504c2013-05-07 14:42:1227#include "content/public/browser/navigation_entry.h"
Becca Hughes112832e2019-06-11 17:19:0228#include "content/public/browser/picture_in_picture_window_controller.h"
arthursonzognib93a4472020-04-10 07:38:0029#include "content/public/browser/presentation_receiver_flags.h"
Yutaka Hirano2109e582018-02-14 07:24:4630#include "content/public/browser/render_process_host.h"
[email protected]0b659b32012-03-26 21:29:3231#include "content/public/browser/render_view_host.h"
avif9ab5d942015-10-15 14:05:4432#include "content/public/browser/render_widget_host.h"
Xianzhu Wang6be66b012020-05-06 17:17:2533#include "content/public/browser/renderer_preferences_util.h"
[email protected]0b659b32012-03-26 21:29:3234#include "content/public/browser/web_contents.h"
guoweis8efb6d892015-10-12 18:26:1735#include "content/public/common/content_switches.h"
danakjde3e2a02020-05-12 16:51:2036#include "content/shell/app/resource.h"
[email protected]de7d61ff2013-08-20 11:30:4137#include "content/shell/browser/shell_content_browser_client.h"
38#include "content/shell/browser/shell_devtools_frontend.h"
39#include "content/shell/browser/shell_javascript_dialog_manager.h"
[email protected]b7c504c2013-05-07 14:42:1240#include "content/shell/common/shell_switches.h"
Scott Violeta35f9a42018-03-22 22:00:4441#include "media/media_buildflags.h"
Antonio Gomesb5bf548f2019-09-12 17:40:1542#include "third_party/blink/public/common/peerconnection/webrtc_ip_handling_policy.h"
Leon Hanc819dc62019-01-28 04:30:1943#include "third_party/blink/public/mojom/renderer_preferences.mojom.h"
[email protected]9fbd3f862011-09-20 23:31:3444
[email protected]9fbd3f862011-09-20 23:31:3445namespace content {
46
Wezcbf4a042018-06-13 16:29:1247// Null until/unless the default main message loop is running.
48base::NoDestructor<base::OnceClosure> g_quit_main_message_loop;
Wez9a58a152018-06-07 18:59:3349
pdrcab84ee2015-03-13 21:47:0450const int kDefaultTestWindowWidthDip = 800;
51const int kDefaultTestWindowHeightDip = 600;
[email protected]1e57cab2013-05-28 04:26:1152
[email protected]e99ca5112011-09-26 17:22:5453std::vector<Shell*> Shell::windows_;
danakja9fe91c2019-05-01 19:02:2954base::OnceCallback<void(Shell*)> Shell::shell_created_callback_;
[email protected]9fbd3f862011-09-20 23:31:3455
danakjde3e2a02020-05-12 16:51:2056ShellPlatformDelegate* g_platform;
57
[email protected]7fff43e2013-05-21 20:21:1058class Shell::DevToolsWebContentsObserver : public WebContentsObserver {
59 public:
60 DevToolsWebContentsObserver(Shell* shell, WebContents* web_contents)
61 : WebContentsObserver(web_contents),
62 shell_(shell) {
63 }
64
65 // WebContentsObserver
dchenge933b3e2014-10-21 11:44:0966 void WebContentsDestroyed() override {
[email protected]7fff43e2013-05-21 20:21:1067 shell_->OnDevToolsWebContentsDestroyed();
68 }
69
70 private:
71 Shell* shell_;
72
73 DISALLOW_COPY_AND_ASSIGN(DevToolsWebContentsObserver);
74};
75
Bo Liu300c6052018-06-12 04:46:0776Shell::Shell(std::unique_ptr<WebContents> web_contents,
77 bool should_set_delegate)
erikchenbee5c9622018-04-27 19:30:2578 : WebContentsObserver(web_contents.get()),
danakjde3e2a02020-05-12 16:51:2079 web_contents_(std::move(web_contents)) {
Bo Liu300c6052018-06-12 04:46:0780 if (should_set_delegate)
81 web_contents_->SetDelegate(this);
arthursonzognifdd49912017-08-31 08:55:2682
danakjd4b48df52020-07-02 18:16:4883 if (!switches::IsRunWebTestsSwitchPresent()) {
Xianzhu Wang6be66b012020-05-06 17:17:2584 UpdateFontRendererPreferencesFromSystemSettings(
85 web_contents_->GetMutableRendererPrefs());
Francois Doraye6161152018-03-27 22:05:3786 }
Pavel Feldmanc7cd063c2017-10-06 20:04:2887
[email protected]9e00e6352012-07-30 17:05:1888 windows_.push_back(this);
89
danakja9fe91c2019-05-01 19:02:2990 if (shell_created_callback_)
91 std::move(shell_created_callback_).Run(this);
[email protected]9fbd3f862011-09-20 23:31:3492}
93
94Shell::~Shell() {
danakjde3e2a02020-05-12 16:51:2095 g_platform->CleanUp(this);
[email protected]e99ca5112011-09-26 17:22:5496
97 for (size_t i = 0; i < windows_.size(); ++i) {
98 if (windows_[i] == this) {
99 windows_.erase(windows_.begin() + i);
100 break;
101 }
102 }
[email protected]11a65b692012-03-30 11:29:16103
danakjde3e2a02020-05-12 16:51:20104 // Always destroy WebContents before destroying ShellPlatformDelegate.
105 // WebContents destruction sequence may depend on the resources destroyed with
106 // ShellPlatformDelegate (e.g. the display::Screen singleton).
Sergey Ulanovf0875d12019-01-03 20:33:23107 web_contents_->SetDelegate(nullptr);
108 web_contents_.reset();
109
Wez9a58a152018-06-07 18:59:33110 if (windows_.empty()) {
Peter Kastinged599662020-05-18 18:40:42111 delete g_platform;
112 g_platform = nullptr;
danakjde3e2a02020-05-12 16:51:20113
Yutaka Hirano2109e582018-02-14 07:24:46114 for (auto it = RenderProcessHost::AllHostsIterator(); !it.IsAtEnd();
115 it.Advance()) {
116 it.GetCurrentValue()->DisableKeepAliveRefCount();
117 }
Wezcbf4a042018-06-13 16:29:12118 if (*g_quit_main_message_loop)
119 std::move(*g_quit_main_message_loop).Run();
[email protected]b53adf452014-02-07 12:55:08120 }
[email protected]9fbd3f862011-09-20 23:31:34121}
122
erikchenbee5c9622018-04-27 19:30:25123Shell* Shell::CreateShell(std::unique_ptr<WebContents> web_contents,
Bo Liu300c6052018-06-12 04:46:07124 const gfx::Size& initial_size,
125 bool should_set_delegate) {
erikchenbee5c9622018-04-27 19:30:25126 WebContents* raw_web_contents = web_contents.get();
Bo Liu300c6052018-06-12 04:46:07127 Shell* shell = new Shell(std::move(web_contents), should_set_delegate);
danakjde3e2a02020-05-12 16:51:20128 g_platform->CreatePlatformWindow(shell, initial_size);
[email protected]1596efb2013-01-17 22:13:01129
creisb6561df2016-02-11 20:20:54130 // Note: Do not make RenderFrameHost or RenderViewHost specific state changes
131 // here, because they will be forgotten after a cross-process navigation. Use
132 // RenderFrameCreated or RenderViewCreated instead.
Kent Tamuracd3ebc42018-05-16 06:44:22133 if (switches::IsRunWebTestsSwitchPresent()) {
erikchenbee5c9622018-04-27 19:30:25134 raw_web_contents->GetMutableRendererPrefs()->use_custom_colors = false;
Bruce Long1e3e1f542019-10-16 17:56:28135 raw_web_contents->SyncRendererPrefs();
[email protected]1596efb2013-01-17 22:13:01136 }
137
lukasza381b0492016-03-10 16:48:43138 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
guoweis4ee48592015-12-02 06:37:07139 if (command_line->HasSwitch(switches::kForceWebRtcIPHandlingPolicy)) {
erikchenbee5c9622018-04-27 19:30:25140 raw_web_contents->GetMutableRendererPrefs()->webrtc_ip_handling_policy =
guoweis4ee48592015-12-02 06:37:07141 command_line->GetSwitchValueASCII(
142 switches::kForceWebRtcIPHandlingPolicy);
guoweis8efb6d892015-10-12 18:26:17143 }
guoweis8efb6d892015-10-12 18:26:17144
danakj8de3b7492020-07-02 22:41:42145 g_platform->SetContents(shell);
146 g_platform->DidCreateOrAttachWebContents(shell, raw_web_contents);
danakj7da833932020-06-23 21:49:40147
[email protected]3fd84032012-01-12 18:20:17148 return shell;
149}
150
[email protected]11a65b692012-03-30 11:29:16151void Shell::CloseAllWindows() {
[email protected]b50452f2014-08-18 12:31:44152 DevToolsAgentHost::DetachAllClients();
Wezcbf4a042018-06-13 16:29:12153
Peter Kastinged599662020-05-18 18:40:42154 std::vector<Shell*> open_windows(windows_);
155 for (Shell* open_window : open_windows)
156 open_window->Close();
157 DCHECK(windows_.empty());
Wezcbf4a042018-06-13 16:29:12158
Peter Kastinged599662020-05-18 18:40:42159 // Pump the message loop to allow window teardown tasks to run.
160 base::RunLoop().RunUntilIdle();
[email protected]11a65b692012-03-30 11:29:16161}
162
Wezcbf4a042018-06-13 16:29:12163void Shell::SetMainMessageLoopQuitClosure(base::OnceClosure quit_closure) {
164 *g_quit_main_message_loop = std::move(quit_closure);
165}
166
Wez7d3eb012018-06-20 22:51:28167void Shell::QuitMainMessageLoopForTesting() {
Wezc93f15a92020-02-25 21:57:44168 if (*g_quit_main_message_loop)
169 std::move(*g_quit_main_message_loop).Run();
Wez7d3eb012018-06-20 22:51:28170}
171
[email protected]9e00e6352012-07-30 17:05:18172void Shell::SetShellCreatedCallback(
danakja9fe91c2019-05-01 19:02:29173 base::OnceCallback<void(Shell*)> shell_created_callback) {
174 DCHECK(!shell_created_callback_);
Tommy Nyquist4b749d02018-03-20 21:46:29175 shell_created_callback_ = std::move(shell_created_callback);
[email protected]9e00e6352012-07-30 17:05:18176}
177
danakjde3e2a02020-05-12 16:51:20178// static
179bool Shell::ShouldHideToolbar() {
180 return base::CommandLine::ForCurrentProcess()->HasSwitch(
181 switches::kContentShellHideToolbar);
182}
183
David Benjaminf62c6662019-03-21 20:25:04184Shell* Shell::FromWebContents(WebContents* web_contents) {
185 for (Shell* window : windows_) {
186 if (window->web_contents() && window->web_contents() == web_contents) {
187 return window;
[email protected]74830f02012-01-30 22:27:04188 }
189 }
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28190 return nullptr;
[email protected]74830f02012-01-30 22:27:04191}
192
danakjde3e2a02020-05-12 16:51:20193void Shell::Initialize(std::unique_ptr<ShellPlatformDelegate> platform) {
194 g_platform = platform.release();
195 g_platform->Initialize(GetShellDefaultSize());
[email protected]6153b272013-01-25 22:29:23196}
197
[email protected]a2904092013-10-15 04:53:59198gfx::Size Shell::AdjustWindowSize(const gfx::Size& initial_size) {
199 if (!initial_size.IsEmpty())
200 return initial_size;
pdrcab84ee2015-03-13 21:47:04201 return GetShellDefaultSize();
[email protected]a2904092013-10-15 04:53:59202}
203
[email protected]bdcf9152012-07-19 17:43:21204Shell* Shell::CreateNewWindow(BrowserContext* browser_context,
[email protected]e99ca5112011-09-26 17:22:54205 const GURL& url,
lukasza04130152016-10-21 20:26:32206 const scoped_refptr<SiteInstance>& site_instance,
[email protected]cdb806722013-01-10 14:18:23207 const gfx::Size& initial_size) {
[email protected]54944cde2012-12-09 09:24:59208 WebContents::CreateParams create_params(browser_context, site_instance);
mark a. foltzef394fce2017-10-21 09:11:02209 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
210 switches::kForcePresentationReceiverForTesting)) {
211 create_params.starting_sandbox_flags =
arthursonzognib93a4472020-04-10 07:38:00212 content::kPresentationReceiverSandboxFlags;
mark a. foltzef394fce2017-10-21 09:11:02213 }
erikchenbee5c9622018-04-27 19:30:25214 std::unique_ptr<WebContents> web_contents =
Erik Chenbb8e738e2018-04-28 14:10:43215 WebContents::Create(create_params);
erikchenbee5c9622018-04-27 19:30:25216 Shell* shell =
danakjfc5184932019-09-12 18:08:32217 CreateShell(std::move(web_contents), AdjustWindowSize(initial_size),
Bo Liu300c6052018-06-12 04:46:07218 true /* should_set_delegate */);
danakj7da833932020-06-23 21:49:40219
[email protected]e99ca5112011-09-26 17:22:54220 if (!url.is_empty())
221 shell->LoadURL(url);
[email protected]9fbd3f862011-09-20 23:31:34222 return shell;
223}
224
danakj24577b12020-05-13 22:38:18225void Shell::RenderViewReady() {
226 g_platform->RenderViewReady(this);
227}
228
[email protected]9fbd3f862011-09-20 23:31:34229void Shell::LoadURL(const GURL& url) {
Alex Moshchuk7e26eca2018-03-03 01:34:29230 LoadURLForFrame(
231 url, std::string(),
232 ui::PageTransitionFromInt(ui::PAGE_TRANSITION_TYPED |
233 ui::PAGE_TRANSITION_FROM_ADDRESS_BAR));
[email protected]d2494ff2013-02-20 08:22:37234}
235
Alex Moshchuk7e26eca2018-03-03 01:34:29236void Shell::LoadURLForFrame(const GURL& url,
237 const std::string& frame_name,
238 ui::PageTransition transition_type) {
[email protected]d2494ff2013-02-20 08:22:37239 NavigationController::LoadURLParams params(url);
[email protected]d2494ff2013-02-20 08:22:37240 params.frame_name = frame_name;
Alex Moshchuk7e26eca2018-03-03 01:34:29241 params.transition_type = transition_type;
[email protected]d2494ff2013-02-20 08:22:37242 web_contents_->GetController().LoadURLWithParams(params);
[email protected]9fbd3f862011-09-20 23:31:34243}
244
[email protected]76bdecb2014-04-16 17:58:08245void Shell::LoadDataWithBaseURL(const GURL& url, const std::string& data,
246 const GURL& base_url) {
boliuec93ea92016-02-17 22:23:07247 bool load_as_string = false;
248 LoadDataWithBaseURLInternal(url, data, base_url, load_as_string);
249}
250
251#if defined(OS_ANDROID)
252void Shell::LoadDataAsStringWithBaseURL(const GURL& url,
253 const std::string& data,
254 const GURL& base_url) {
255 bool load_as_string = true;
256 LoadDataWithBaseURLInternal(url, data, base_url, load_as_string);
257}
258#endif
259
260void Shell::LoadDataWithBaseURLInternal(const GURL& url,
261 const std::string& data,
262 const GURL& base_url,
263 bool load_as_string) {
264#if !defined(OS_ANDROID)
265 DCHECK(!load_as_string); // Only supported on Android.
266#endif
267
268 NavigationController::LoadURLParams params(GURL::EmptyGURL());
269 const std::string data_url_header = "data:text/html;charset=utf-8,";
270 if (load_as_string) {
271 params.url = GURL(data_url_header);
272 std::string data_url_as_string = data_url_header + data;
273#if defined(OS_ANDROID)
274 params.data_url_as_string =
275 base::RefCountedString::TakeString(&data_url_as_string);
276#endif
277 } else {
278 params.url = GURL(data_url_header + data);
279 }
280
[email protected]76bdecb2014-04-16 17:58:08281 params.load_type = NavigationController::LOAD_TYPE_DATA;
282 params.base_url_for_data_url = base_url;
283 params.virtual_url_for_data_url = url;
284 params.override_user_agent = NavigationController::UA_OVERRIDE_FALSE;
285 web_contents_->GetController().LoadURLWithParams(params);
[email protected]76bdecb2014-04-16 17:58:08286}
287
[email protected]a2904092013-10-15 04:53:59288void Shell::AddNewContents(WebContents* source,
erikchenbee5c9622018-04-27 19:30:25289 std::unique_ptr<WebContents> new_contents,
Joel Hockey891e88062020-04-30 05:38:44290 const GURL& target_url,
[email protected]a2904092013-10-15 04:53:59291 WindowOpenDisposition disposition,
bokan107a47f2015-02-03 23:23:39292 const gfx::Rect& initial_rect,
[email protected]a2904092013-10-15 04:53:59293 bool user_gesture,
294 bool* was_blocked) {
Bo Liu300c6052018-06-12 04:46:07295 CreateShell(
296 std::move(new_contents), AdjustWindowSize(initial_rect.size()),
297 !delay_popup_contents_delegate_for_testing_ /* should_set_delegate */);
[email protected]a2904092013-10-15 04:53:59298}
299
[email protected]9fbd3f862011-09-20 23:31:34300void Shell::GoBackOrForward(int offset) {
[email protected]0b659b32012-03-26 21:29:32301 web_contents_->GetController().GoToOffset(offset);
[email protected]9fbd3f862011-09-20 23:31:34302}
303
304void Shell::Reload() {
toyoshim6142d96f2016-12-19 09:07:25305 web_contents_->GetController().Reload(ReloadType::NORMAL, false);
[email protected]9fbd3f862011-09-20 23:31:34306}
307
toyoshime5aaf6a2016-05-18 08:07:48308void Shell::ReloadBypassingCache() {
toyoshim6142d96f2016-12-19 09:07:25309 web_contents_->GetController().Reload(ReloadType::BYPASSING_CACHE, false);
toyoshime5aaf6a2016-05-18 08:07:48310}
311
[email protected]9fbd3f862011-09-20 23:31:34312void Shell::Stop() {
[email protected]0b659b32012-03-26 21:29:32313 web_contents_->Stop();
[email protected]9fbd3f862011-09-20 23:31:34314}
315
[email protected]e3b10d12014-03-28 16:06:09316void Shell::UpdateNavigationControls(bool to_different_document) {
[email protected]0b659b32012-03-26 21:29:32317 int current_index = web_contents_->GetController().GetCurrentEntryIndex();
318 int max_index = web_contents_->GetController().GetEntryCount() - 1;
[email protected]9fbd3f862011-09-20 23:31:34319
danakjde3e2a02020-05-12 16:51:20320 g_platform->EnableUIControl(this, ShellPlatformDelegate::BACK_BUTTON,
321 current_index > 0);
322 g_platform->EnableUIControl(this, ShellPlatformDelegate::FORWARD_BUTTON,
323 current_index < max_index);
324 g_platform->EnableUIControl(
325 this, ShellPlatformDelegate::STOP_BUTTON,
[email protected]e3b10d12014-03-28 16:06:09326 to_different_document && web_contents_->IsLoading());
[email protected]9fbd3f862011-09-20 23:31:34327}
328
[email protected]7c17b6992012-08-09 16:16:30329void Shell::ShowDevTools() {
mohsen6eb57fb2016-07-22 03:14:08330 if (!devtools_frontend_) {
331 devtools_frontend_ = ShellDevToolsFrontend::Show(web_contents());
332 devtools_observer_.reset(new DevToolsWebContentsObserver(
333 this, devtools_frontend_->frontend_shell()->web_contents()));
334 }
[email protected]3142e5d2014-02-07 00:54:46335
mohsen6eb57fb2016-07-22 03:14:08336 devtools_frontend_->Activate();
[email protected]7c17b6992012-08-09 16:16:30337}
338
[email protected]001841c92012-12-11 17:00:13339void Shell::CloseDevTools() {
[email protected]0773e0c2013-01-25 15:57:57340 if (!devtools_frontend_)
[email protected]001841c92012-12-11 17:00:13341 return;
[email protected]7fff43e2013-05-21 20:21:10342 devtools_observer_.reset();
[email protected]0773e0c2013-01-25 15:57:57343 devtools_frontend_->Close();
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28344 devtools_frontend_ = nullptr;
[email protected]001841c92012-12-11 17:00:13345}
346
danakjde3e2a02020-05-12 16:51:20347void Shell::ResizeWebContentForTests(const gfx::Size& content_size) {
348 g_platform->ResizeWebContent(this, content_size);
349}
danakjde3e2a02020-05-12 16:51:20350
[email protected]9fbd3f862011-09-20 23:31:34351gfx::NativeView Shell::GetContentView() {
[email protected]59383c782013-04-17 16:43:27352 if (!web_contents_)
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28353 return nullptr;
[email protected]fc2b46b2014-05-03 16:33:45354 return web_contents_->GetNativeView();
[email protected]9fbd3f862011-09-20 23:31:34355}
356
danakjde3e2a02020-05-12 16:51:20357#if !defined(OS_ANDROID)
358gfx::NativeWindow Shell::window() {
359 return g_platform->GetNativeWindow(this);
360}
361#endif
362
Avi Drissman7c57be72020-07-29 20:09:46363#if defined(OS_MAC)
danakjde3e2a02020-05-12 16:51:20364void Shell::ActionPerformed(int control) {
365 switch (control) {
366 case IDC_NAV_BACK:
367 GoBackOrForward(-1);
368 break;
369 case IDC_NAV_FORWARD:
370 GoBackOrForward(1);
371 break;
372 case IDC_NAV_RELOAD:
373 Reload();
374 break;
375 case IDC_NAV_STOP:
376 Stop();
377 break;
378 }
379}
380
381void Shell::URLEntered(const std::string& url_string) {
382 if (!url_string.empty()) {
383 GURL url(url_string);
384 if (!url.has_scheme())
385 url = GURL("http://" + url_string);
386 LoadURL(url);
387 }
388}
389#endif
390
alexmos5a98a052016-01-06 00:15:02391WebContents* Shell::OpenURLFromTab(WebContents* source,
392 const OpenURLParams& params) {
lukasza04130152016-10-21 20:26:32393 WebContents* target = nullptr;
394 switch (params.disposition) {
395 case WindowOpenDisposition::CURRENT_TAB:
396 target = source;
397 break;
398
399 // Normally, the difference between NEW_POPUP and NEW_WINDOW is that a popup
400 // should have no toolbar, no status bar, no menu bar, no scrollbars and be
401 // not resizable. For simplicity and to enable new testing scenarios in
Kent Tamura21d1de62018-12-10 04:45:20402 // content shell and web tests, popups don't get special treatment below
lukasza04130152016-10-21 20:26:32403 // (i.e. they will have a toolbar and other things described here).
404 case WindowOpenDisposition::NEW_POPUP:
lukaszabe2f0da2017-04-25 00:43:00405 case WindowOpenDisposition::NEW_WINDOW:
Kent Tamura21d1de62018-12-10 04:45:20406 // content_shell doesn't really support tabs, but some web tests use
lukaszabe2f0da2017-04-25 00:43:00407 // middle click (which translates into kNavigationPolicyNewBackgroundTab),
408 // so we treat the cases below just like a NEW_WINDOW disposition.
409 case WindowOpenDisposition::NEW_BACKGROUND_TAB:
410 case WindowOpenDisposition::NEW_FOREGROUND_TAB: {
lukasza04130152016-10-21 20:26:32411 Shell* new_window =
412 Shell::CreateNewWindow(source->GetBrowserContext(),
413 GURL(), // Don't load anything just yet.
414 params.source_site_instance,
415 gfx::Size()); // Use default size.
416 target = new_window->web_contents();
lukasza04130152016-10-21 20:26:32417 break;
418 }
419
420 // No tabs in content_shell:
421 case WindowOpenDisposition::SINGLETON_TAB:
lukasza04130152016-10-21 20:26:32422 // No incognito mode in content_shell:
423 case WindowOpenDisposition::OFF_THE_RECORD:
Kent Tamura21d1de62018-12-10 04:45:20424 // TODO(lukasza): Investigate if some web tests might need support for
lukasza04130152016-10-21 20:26:32425 // SAVE_TO_DISK disposition. This would probably require that
Gyuyoung Kim26c7bc92020-04-29 00:53:00426 // WebTestControlHost always sets up and cleans up a temporary directory
lukasza04130152016-10-21 20:26:32427 // as the default downloads destinations for the duration of a test.
428 case WindowOpenDisposition::SAVE_TO_DISK:
429 // Ignoring requests with disposition == IGNORE_ACTION...
430 case WindowOpenDisposition::IGNORE_ACTION:
431 default:
432 return nullptr;
433 }
alexmos5a98a052016-01-06 00:15:02434
Lukasz Anforowicz82a5ca92019-10-24 18:45:37435 target->GetController().LoadURLWithParams(
436 NavigationController::LoadURLParams(params));
lukasza04130152016-10-21 20:26:32437 return target;
alexmos5a98a052016-01-06 00:15:02438}
439
[email protected]e3b10d12014-03-28 16:06:09440void Shell::LoadingStateChanged(WebContents* source,
441 bool to_different_document) {
442 UpdateNavigationControls(to_different_document);
danakjde3e2a02020-05-12 16:51:20443 g_platform->SetIsLoading(this, source->IsLoading());
[email protected]e99ca5112011-09-26 17:22:54444}
445
danakjde3e2a02020-05-12 16:51:20446#if defined(OS_ANDROID)
447void Shell::SetOverlayMode(bool use_overlay_mode) {
448 g_platform->SetOverlayMode(this, use_overlay_mode);
449}
450#endif
451
Dave Tapuskaa4189512019-10-15 20:27:34452void Shell::EnterFullscreenModeForTab(
Mike Wasserman4ca09792020-05-29 17:44:43453 RenderFrameHost* requesting_frame,
Dave Tapuskaa4189512019-10-15 20:27:34454 const blink::mojom::FullscreenOptions& options) {
Mike Wasserman4ca09792020-05-29 17:44:43455 ToggleFullscreenModeForTab(WebContents::FromRenderFrameHost(requesting_frame),
456 true);
mlamouri7a78d6fd2015-01-17 13:23:53457}
458
459void Shell::ExitFullscreenModeForTab(WebContents* web_contents) {
460 ToggleFullscreenModeForTab(web_contents, false);
461}
462
[email protected]99c014c2012-11-27 12:03:42463void Shell::ToggleFullscreenModeForTab(WebContents* web_contents,
464 bool enter_fullscreen) {
465#if defined(OS_ANDROID)
danakjde3e2a02020-05-12 16:51:20466 g_platform->ToggleFullscreenModeForTab(this, web_contents, enter_fullscreen);
[email protected]99c014c2012-11-27 12:03:42467#endif
[email protected]99c014c2012-11-27 12:03:42468 if (is_fullscreen_ != enter_fullscreen) {
469 is_fullscreen_ = enter_fullscreen;
Fady Samuel0b911822018-04-25 13:22:16470 web_contents->GetRenderViewHost()
471 ->GetWidget()
472 ->SynchronizeVisualProperties();
[email protected]99c014c2012-11-27 12:03:42473 }
474}
475
Lucas Furukawa Gadani4909f3c2019-06-18 22:36:52476bool Shell::IsFullscreenForTabOrPending(const WebContents* web_contents) {
[email protected]99c014c2012-11-27 12:03:42477#if defined(OS_ANDROID)
danakjde3e2a02020-05-12 16:51:20478 return g_platform->IsFullscreenForTabOrPending(this, web_contents);
[email protected]99c014c2012-11-27 12:03:42479#else
480 return is_fullscreen_;
481#endif
482}
483
Eric Willigers052f0432019-10-04 04:06:57484blink::mojom::DisplayMode Shell::GetDisplayMode(
485 const WebContents* web_contents) {
486 // TODO: should return blink::mojom::DisplayModeFullscreen wherever user puts
Lukasz Anforowicz52b93722018-06-20 16:11:39487 // a browser window into fullscreen (not only in case of renderer-initiated
488 // fullscreen mode): crbug.com/476874.
489 return IsFullscreenForTabOrPending(web_contents)
Eric Willigers052f0432019-10-04 04:06:57490 ? blink::mojom::DisplayMode::kFullscreen
491 : blink::mojom::DisplayMode::kBrowser;
mikhail.pozdnyakovc0e251b2015-04-15 06:51:12492}
493
[email protected]f78439002012-11-28 14:45:59494void Shell::RequestToLockMouse(WebContents* web_contents,
495 bool user_gesture,
496 bool last_unlocked_by_target) {
Dave Tapuskab4998782020-10-08 17:22:47497 // Give the platform a chance to handle the lock request, if it doesn't
498 // indicate it handled it, allow the request.
499 if (!g_platform->HandleRequestToLockMouse(this, web_contents, user_gesture,
500 last_unlocked_by_target)) {
501 web_contents->GotResponseToLockMouseRequest(
502 blink::mojom::PointerLockResult::kSuccess);
503 }
[email protected]f78439002012-11-28 14:45:59504}
505
danakjde3e2a02020-05-12 16:51:20506void Shell::Close() {
507 // Shell is "self-owned" and destroys itself. The ShellPlatformDelegate
508 // has the chance to co-opt this and do its own destruction.
509 if (!g_platform->DestroyShell(this))
510 delete this;
511}
512
[email protected]9e00e6352012-07-30 17:05:18513void Shell::CloseContents(WebContents* source) {
514 Close();
515}
516
Lucas Furukawa Gadani4909f3c2019-06-18 22:36:52517bool Shell::CanOverscrollContent() {
[email protected]067310262012-11-22 14:30:41518#if defined(USE_AURA)
519 return true;
520#else
521 return false;
522#endif
523}
524
Mugdha Lakhani5f8de7cc2020-03-10 20:43:36525void Shell::NavigationStateChanged(WebContents* source,
526 InvalidateTypes changed_flags) {
527 if (changed_flags & INVALIDATE_TYPE_URL)
danakjde3e2a02020-05-12 16:51:20528 g_platform->SetAddressBarURL(this, source->GetVisibleURL());
[email protected]e99ca5112011-09-26 17:22:54529}
530
mathiash72a5e462014-11-19 08:18:50531JavaScriptDialogManager* Shell::GetJavaScriptDialogManager(
532 WebContents* source) {
danakj8de3b7492020-07-02 22:41:42533 if (!dialog_manager_)
534 dialog_manager_ = g_platform->CreateJavaScriptDialogManager(this);
535 if (!dialog_manager_)
536 dialog_manager_ = std::make_unique<ShellJavaScriptDialogManager>();
[email protected]71a88bb2013-02-01 22:05:15537 return dialog_manager_.get();
[email protected]f2210022012-03-29 00:36:08538}
539
dcheng6003e0b2016-04-09 18:42:34540std::unique_ptr<BluetoothChooser> Shell::RunBluetoothChooser(
jyasskin93649a52016-02-17 19:57:02541 RenderFrameHost* frame,
542 const BluetoothChooser::EventHandler& event_handler) {
danakj8de3b7492020-07-02 22:41:42543 return g_platform->RunBluetoothChooser(this, frame, event_handler);
jyasskin8057c002015-09-10 19:09:53544}
545
danakj378b97f2020-06-11 02:40:52546class AlwaysAllowBluetoothScanning : public BluetoothScanningPrompt {
547 public:
548 explicit AlwaysAllowBluetoothScanning(const EventHandler& event_handler) {
549 event_handler.Run(content::BluetoothScanningPrompt::Event::kAllow);
550 }
551};
552
Jun Cai149002e2019-05-09 23:13:07553std::unique_ptr<BluetoothScanningPrompt> Shell::ShowBluetoothScanningPrompt(
554 RenderFrameHost* frame,
555 const BluetoothScanningPrompt::EventHandler& event_handler) {
danakj378b97f2020-06-11 02:40:52556 return std::make_unique<AlwaysAllowBluetoothScanning>(event_handler);
Jun Cai149002e2019-05-09 23:13:07557}
558
Avi Drissman7c57be72020-07-29 20:09:46559#if defined(OS_MAC)
Xianzhu Wang0f021a82020-07-03 01:29:47560void Shell::DidNavigateMainFramePostCommit(WebContents* contents) {
561 g_platform->DidNavigateMainFramePostCommit(this, contents);
562}
563
danakjde3e2a02020-05-12 16:51:20564bool Shell::HandleKeyboardEvent(WebContents* source,
565 const NativeWebKeyboardEvent& event) {
566 return g_platform->HandleKeyboardEvent(this, source, event);
567}
568#endif
569
avia90ae4e2016-11-11 20:49:33570bool Shell::DidAddMessageToConsole(WebContents* source,
Lowell Manners1de5242e2019-04-25 10:18:46571 blink::mojom::ConsoleMessageLevel log_level,
avia90ae4e2016-11-11 20:49:33572 const base::string16& message,
573 int32_t line_no,
574 const base::string16& source_id) {
Kent Tamuracd3ebc42018-05-16 06:44:22575 return switches::IsRunWebTestsSwitchPresent();
[email protected]efb5f572012-01-29 10:57:33576}
577
Lucas Furukawa Gadani06548fb2019-01-15 05:01:25578void Shell::PortalWebContentsCreated(WebContents* portal_web_contents) {
danakj8de3b7492020-07-02 22:41:42579 g_platform->DidCreateOrAttachWebContents(this, portal_web_contents);
Lucas Furukawa Gadani06548fb2019-01-15 05:01:25580}
581
Lukasz Anforowicz52b93722018-06-20 16:11:39582void Shell::RendererUnresponsive(
583 WebContents* source,
584 RenderWidgetHost* render_widget_host,
585 base::RepeatingClosure hang_monitor_restarter) {
danakj245441f2020-07-03 15:18:41586 LOG(WARNING) << "renderer unresponsive";
[email protected]5bf68f22012-08-31 07:38:10587}
588
[email protected]233567d2013-02-27 20:22:02589void Shell::ActivateContents(WebContents* contents) {
Avi Drissman7c57be72020-07-29 20:09:46590#if !defined(OS_MAC)
danakjd4b48df52020-07-02 18:16:48591 // TODO(danakj): Move this to ShellPlatformDelegate.
danakj674bf1c02020-05-01 18:37:51592 contents->Focus();
593#else
594 // Mac headless mode is quite different than other platforms. Normally
595 // focusing the WebContents would cause the OS to focus the window. Because
596 // headless mac doesn't actually have system windows, we can't go down the
597 // normal path and have to fake it out in the browser process.
danakjde3e2a02020-05-12 16:51:20598 g_platform->ActivateContents(this, contents);
danakj674bf1c02020-05-01 18:37:51599#endif
[email protected]233567d2013-02-27 20:22:02600}
601
Kevin McNee7422bc42020-02-20 18:49:55602std::unique_ptr<WebContents> Shell::ActivatePortalWebContents(
603 WebContents* predecessor_contents,
604 std::unique_ptr<WebContents> portal_contents) {
605 DCHECK_EQ(predecessor_contents, web_contents_.get());
606 portal_contents->SetDelegate(this);
Lucas Furukawa Gadanic5c0cd02018-10-04 20:32:33607 web_contents_->SetDelegate(nullptr);
Kevin McNee7422bc42020-02-20 18:49:55608 std::swap(web_contents_, portal_contents);
danakjde3e2a02020-05-12 16:51:20609 g_platform->SetContents(this);
610 g_platform->SetAddressBarURL(this, web_contents_->GetVisibleURL());
Lucas Furukawa Gadanic5c0cd02018-10-04 20:32:33611 LoadingStateChanged(web_contents_.get(), true);
Kevin McNee7422bc42020-02-20 18:49:55612 return portal_contents;
Lucas Furukawa Gadanic5c0cd02018-10-04 20:32:33613}
614
Adithya Srinivasanb7204c82020-08-17 14:26:33615namespace {
616class PendingCallback : public base::RefCounted<PendingCallback> {
617 public:
618 explicit PendingCallback(base::OnceCallback<void()> cb)
619 : callback_(std::move(cb)) {}
620
621 private:
622 friend class base::RefCounted<PendingCallback>;
623 ~PendingCallback() { std::move(callback_).Run(); }
624 base::OnceCallback<void()> callback_;
625};
626} // namespace
627
628void Shell::UpdateInspectedWebContentsIfNecessary(
629 content::WebContents* old_contents,
630 content::WebContents* new_contents,
631 base::OnceCallback<void()> callback) {
632 scoped_refptr<PendingCallback> pending_callback =
633 base::MakeRefCounted<PendingCallback>(std::move(callback));
634 for (auto* shell_devtools_bindings :
635 ShellDevToolsBindings::GetInstancesForWebContents(old_contents)) {
636 shell_devtools_bindings->UpdateInspectedWebContents(
637 new_contents,
638 base::BindOnce(base::DoNothing::Once<scoped_refptr<PendingCallback>>(),
639 pending_callback));
640 }
641}
642
Lukasz Anforowicz82a5ca92019-10-24 18:45:37643bool Shell::ShouldAllowRunningInsecureContent(WebContents* web_contents,
644 bool allowed_per_prefs,
645 const url::Origin& origin,
646 const GURL& resource_url) {
danakj8de3b7492020-07-02 22:41:42647 if (allowed_per_prefs)
648 return true;
carloskd9d97942017-02-16 08:58:09649
danakj8de3b7492020-07-02 22:41:42650 return g_platform->ShouldAllowRunningInsecureContent(this);
carloskd9d97942017-02-16 08:58:09651}
652
Becca Hughes112832e2019-06-11 17:19:02653PictureInPictureResult Shell::EnterPictureInPicture(
Lukasz Anforowicz82a5ca92019-10-24 18:45:37654 WebContents* web_contents,
Becca Hughes112832e2019-06-11 17:19:02655 const viz::SurfaceId& surface_id,
656 const gfx::Size& natural_size) {
657 // During tests, returning success to pretend the window was created and allow
658 // tests to run accordingly.
659 if (!switches::IsRunWebTestsSwitchPresent())
660 return PictureInPictureResult::kNotSupported;
Becca Hughes112832e2019-06-11 17:19:02661 return PictureInPictureResult::kSuccess;
Mounir Lamouri11e9ef432018-05-22 03:10:16662}
663
Bo Liu300c6052018-06-12 04:46:07664bool Shell::ShouldResumeRequestsForCreatedWindow() {
665 return !delay_popup_contents_delegate_for_testing_;
666}
667
danakjee2390a82020-06-10 16:53:37668void Shell::SetContentsBounds(WebContents* source, const gfx::Rect& bounds) {
669 DCHECK(source == web_contents()); // There's only one WebContents per Shell.
670
671 if (switches::IsRunWebTestsSwitchPresent()) {
672 // Note that chrome drops these requests on normal windows.
673 // TODO(danakj): The position is dropped here but we use the size. Web tests
674 // can't move the window in headless mode anyways, but maybe we should be
675 // letting them pretend?
676 g_platform->ResizeWebContent(this, bounds.size());
677 }
678}
679
pdrcab84ee2015-03-13 21:47:04680gfx::Size Shell::GetShellDefaultSize() {
danakj6c16fe92020-09-18 23:51:32681 static gfx::Size default_shell_size; // Only go through this method once.
682
pdrcab84ee2015-03-13 21:47:04683 if (!default_shell_size.IsEmpty())
684 return default_shell_size;
danakj6c16fe92020-09-18 23:51:32685
pdrcab84ee2015-03-13 21:47:04686 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
687 if (command_line->HasSwitch(switches::kContentShellHostWindowSize)) {
688 const std::string size_str = command_line->GetSwitchValueASCII(
689 switches::kContentShellHostWindowSize);
690 int width, height;
danakj6c16fe92020-09-18 23:51:32691 if (sscanf(size_str.c_str(), "%dx%d", &width, &height) == 2) {
692 default_shell_size = gfx::Size(width, height);
693 } else {
694 LOG(ERROR) << "Invalid size \"" << size_str << "\" given to --"
695 << switches::kContentShellHostWindowSize;
696 }
697 }
698
699 if (default_shell_size.IsEmpty()) {
pdrcab84ee2015-03-13 21:47:04700 default_shell_size = gfx::Size(
701 kDefaultTestWindowWidthDip, kDefaultTestWindowHeightDip);
702 }
danakj6c16fe92020-09-18 23:51:32703
pdrcab84ee2015-03-13 21:47:04704 return default_shell_size;
705}
706
danakjde3e2a02020-05-12 16:51:20707#if defined(OS_ANDROID)
708void Shell::LoadProgressChanged(double progress) {
709 g_platform->LoadProgressChanged(this, progress);
710}
711#endif
712
Avi Drissman93002212017-09-27 03:20:52713void Shell::TitleWasSet(NavigationEntry* entry) {
[email protected]1ef02d242013-10-07 16:18:53714 if (entry)
danakjde3e2a02020-05-12 16:51:20715 g_platform->SetTitle(this, entry->GetTitle());
[email protected]aecc085b2012-06-01 18:15:53716}
717
[email protected]7fff43e2013-05-21 20:21:10718void Shell::OnDevToolsWebContentsDestroyed() {
719 devtools_observer_.reset();
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28720 devtools_frontend_ = nullptr;
[email protected]7fff43e2013-05-21 20:21:10721}
722
[email protected]9fbd3f862011-09-20 23:31:34723} // namespace content