blob: c31b236532827364fd394d6e6758dd36a8884a31 [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"
Kent Tamuraf92649d882018-12-03 09:07:4740#include "content/shell/browser/web_test/secondary_test_window_observer.h"
41#include "content/shell/browser/web_test/web_test_bluetooth_chooser_factory.h"
Gyuyoung Kim26c7bc92020-04-29 00:53:0042#include "content/shell/browser/web_test/web_test_control_host.h"
Kent Tamuraf92649d882018-12-03 09:07:4743#include "content/shell/browser/web_test/web_test_javascript_dialog_manager.h"
[email protected]b7c504c2013-05-07 14:42:1244#include "content/shell/common/shell_switches.h"
Kent Tamurade503112018-12-04 08:57:4045#include "content/shell/common/web_test/web_test_switches.h"
Scott Violeta35f9a42018-03-22 22:00:4446#include "media/media_buildflags.h"
Antonio Gomesb5bf548f2019-09-12 17:40:1547#include "third_party/blink/public/common/peerconnection/webrtc_ip_handling_policy.h"
Leon Hanc819dc62019-01-28 04:30:1948#include "third_party/blink/public/mojom/renderer_preferences.mojom.h"
[email protected]9fbd3f862011-09-20 23:31:3449
[email protected]9fbd3f862011-09-20 23:31:3450namespace content {
51
Wezcbf4a042018-06-13 16:29:1252// Null until/unless the default main message loop is running.
53base::NoDestructor<base::OnceClosure> g_quit_main_message_loop;
Wez9a58a152018-06-07 18:59:3354
pdrcab84ee2015-03-13 21:47:0455const int kDefaultTestWindowWidthDip = 800;
56const int kDefaultTestWindowHeightDip = 600;
[email protected]1e57cab2013-05-28 04:26:1157
[email protected]e99ca5112011-09-26 17:22:5458std::vector<Shell*> Shell::windows_;
danakja9fe91c2019-05-01 19:02:2959base::OnceCallback<void(Shell*)> Shell::shell_created_callback_;
[email protected]9fbd3f862011-09-20 23:31:3460
danakjde3e2a02020-05-12 16:51:2061ShellPlatformDelegate* g_platform;
62
[email protected]7fff43e2013-05-21 20:21:1063class Shell::DevToolsWebContentsObserver : public WebContentsObserver {
64 public:
65 DevToolsWebContentsObserver(Shell* shell, WebContents* web_contents)
66 : WebContentsObserver(web_contents),
67 shell_(shell) {
68 }
69
70 // WebContentsObserver
dchenge933b3e2014-10-21 11:44:0971 void WebContentsDestroyed() override {
[email protected]7fff43e2013-05-21 20:21:1072 shell_->OnDevToolsWebContentsDestroyed();
73 }
74
75 private:
76 Shell* shell_;
77
78 DISALLOW_COPY_AND_ASSIGN(DevToolsWebContentsObserver);
79};
80
Bo Liu300c6052018-06-12 04:46:0781Shell::Shell(std::unique_ptr<WebContents> web_contents,
82 bool should_set_delegate)
erikchenbee5c9622018-04-27 19:30:2583 : WebContentsObserver(web_contents.get()),
danakjde3e2a02020-05-12 16:51:2084 web_contents_(std::move(web_contents)) {
Bo Liu300c6052018-06-12 04:46:0785 if (should_set_delegate)
86 web_contents_->SetDelegate(this);
arthursonzognifdd49912017-08-31 08:55:2687
Kent Tamuracd3ebc42018-05-16 06:44:2288 if (switches::IsRunWebTestsSwitchPresent()) {
jonross26185702019-04-08 18:54:1089 headless_ = !base::CommandLine::ForCurrentProcess()->HasSwitch(
90 switches::kDisableHeadlessMode);
Xianzhu Wang6be66b012020-05-06 17:17:2591 } else {
92 UpdateFontRendererPreferencesFromSystemSettings(
93 web_contents_->GetMutableRendererPrefs());
Francois Doraye6161152018-03-27 22:05:3794 }
Pavel Feldmanc7cd063c2017-10-06 20:04:2895
[email protected]9e00e6352012-07-30 17:05:1896 windows_.push_back(this);
97
danakja9fe91c2019-05-01 19:02:2998 if (shell_created_callback_)
99 std::move(shell_created_callback_).Run(this);
[email protected]9fbd3f862011-09-20 23:31:34100}
101
102Shell::~Shell() {
danakjde3e2a02020-05-12 16:51:20103 g_platform->CleanUp(this);
[email protected]e99ca5112011-09-26 17:22:54104
105 for (size_t i = 0; i < windows_.size(); ++i) {
106 if (windows_[i] == this) {
107 windows_.erase(windows_.begin() + i);
108 break;
109 }
110 }
[email protected]11a65b692012-03-30 11:29:16111
danakjde3e2a02020-05-12 16:51:20112 // Always destroy WebContents before destroying ShellPlatformDelegate.
113 // WebContents destruction sequence may depend on the resources destroyed with
114 // ShellPlatformDelegate (e.g. the display::Screen singleton).
Sergey Ulanovf0875d12019-01-03 20:33:23115 web_contents_->SetDelegate(nullptr);
116 web_contents_.reset();
117
Wez9a58a152018-06-07 18:59:33118 if (windows_.empty()) {
Peter Kastinged599662020-05-18 18:40:42119 delete g_platform;
120 g_platform = nullptr;
danakjde3e2a02020-05-12 16:51:20121
Yutaka Hirano2109e582018-02-14 07:24:46122 for (auto it = RenderProcessHost::AllHostsIterator(); !it.IsAtEnd();
123 it.Advance()) {
124 it.GetCurrentValue()->DisableKeepAliveRefCount();
125 }
Wezcbf4a042018-06-13 16:29:12126 if (*g_quit_main_message_loop)
127 std::move(*g_quit_main_message_loop).Run();
[email protected]b53adf452014-02-07 12:55:08128 }
[email protected]9fbd3f862011-09-20 23:31:34129}
130
erikchenbee5c9622018-04-27 19:30:25131Shell* Shell::CreateShell(std::unique_ptr<WebContents> web_contents,
Bo Liu300c6052018-06-12 04:46:07132 const gfx::Size& initial_size,
133 bool should_set_delegate) {
erikchenbee5c9622018-04-27 19:30:25134 WebContents* raw_web_contents = web_contents.get();
Bo Liu300c6052018-06-12 04:46:07135 Shell* shell = new Shell(std::move(web_contents), should_set_delegate);
danakjde3e2a02020-05-12 16:51:20136 g_platform->CreatePlatformWindow(shell, initial_size);
137 g_platform->SetContents(shell);
[email protected]1596efb2013-01-17 22:13:01138
creisb6561df2016-02-11 20:20:54139 // Note: Do not make RenderFrameHost or RenderViewHost specific state changes
140 // here, because they will be forgotten after a cross-process navigation. Use
141 // RenderFrameCreated or RenderViewCreated instead.
Kent Tamuracd3ebc42018-05-16 06:44:22142 if (switches::IsRunWebTestsSwitchPresent()) {
erikchenbee5c9622018-04-27 19:30:25143 raw_web_contents->GetMutableRendererPrefs()->use_custom_colors = false;
Bruce Long1e3e1f542019-10-16 17:56:28144 raw_web_contents->SyncRendererPrefs();
[email protected]1596efb2013-01-17 22:13:01145 }
146
lukasza381b0492016-03-10 16:48:43147 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
guoweis4ee48592015-12-02 06:37:07148 if (command_line->HasSwitch(switches::kForceWebRtcIPHandlingPolicy)) {
erikchenbee5c9622018-04-27 19:30:25149 raw_web_contents->GetMutableRendererPrefs()->webrtc_ip_handling_policy =
guoweis4ee48592015-12-02 06:37:07150 command_line->GetSwitchValueASCII(
151 switches::kForceWebRtcIPHandlingPolicy);
guoweis8efb6d892015-10-12 18:26:17152 }
guoweis8efb6d892015-10-12 18:26:17153
[email protected]3fd84032012-01-12 18:20:17154 return shell;
155}
156
[email protected]11a65b692012-03-30 11:29:16157void Shell::CloseAllWindows() {
[email protected]b50452f2014-08-18 12:31:44158 DevToolsAgentHost::DetachAllClients();
Wezcbf4a042018-06-13 16:29:12159
Peter Kastinged599662020-05-18 18:40:42160 std::vector<Shell*> open_windows(windows_);
161 for (Shell* open_window : open_windows)
162 open_window->Close();
163 DCHECK(windows_.empty());
Wezcbf4a042018-06-13 16:29:12164
Peter Kastinged599662020-05-18 18:40:42165 // Pump the message loop to allow window teardown tasks to run.
166 base::RunLoop().RunUntilIdle();
[email protected]11a65b692012-03-30 11:29:16167}
168
Wezcbf4a042018-06-13 16:29:12169void Shell::SetMainMessageLoopQuitClosure(base::OnceClosure quit_closure) {
170 *g_quit_main_message_loop = std::move(quit_closure);
171}
172
Wez7d3eb012018-06-20 22:51:28173void Shell::QuitMainMessageLoopForTesting() {
Wezc93f15a92020-02-25 21:57:44174 if (*g_quit_main_message_loop)
175 std::move(*g_quit_main_message_loop).Run();
Wez7d3eb012018-06-20 22:51:28176}
177
[email protected]9e00e6352012-07-30 17:05:18178void Shell::SetShellCreatedCallback(
danakja9fe91c2019-05-01 19:02:29179 base::OnceCallback<void(Shell*)> shell_created_callback) {
180 DCHECK(!shell_created_callback_);
Tommy Nyquist4b749d02018-03-20 21:46:29181 shell_created_callback_ = std::move(shell_created_callback);
[email protected]9e00e6352012-07-30 17:05:18182}
183
danakjde3e2a02020-05-12 16:51:20184// static
185bool Shell::ShouldHideToolbar() {
186 return base::CommandLine::ForCurrentProcess()->HasSwitch(
187 switches::kContentShellHideToolbar);
188}
189
David Benjaminf62c6662019-03-21 20:25:04190Shell* Shell::FromWebContents(WebContents* web_contents) {
191 for (Shell* window : windows_) {
192 if (window->web_contents() && window->web_contents() == web_contents) {
193 return window;
[email protected]74830f02012-01-30 22:27:04194 }
195 }
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28196 return nullptr;
[email protected]74830f02012-01-30 22:27:04197}
198
danakjde3e2a02020-05-12 16:51:20199void Shell::Initialize(std::unique_ptr<ShellPlatformDelegate> platform) {
200 g_platform = platform.release();
201 g_platform->Initialize(GetShellDefaultSize());
[email protected]6153b272013-01-25 22:29:23202}
203
[email protected]a2904092013-10-15 04:53:59204gfx::Size Shell::AdjustWindowSize(const gfx::Size& initial_size) {
205 if (!initial_size.IsEmpty())
206 return initial_size;
pdrcab84ee2015-03-13 21:47:04207 return GetShellDefaultSize();
[email protected]a2904092013-10-15 04:53:59208}
209
[email protected]bdcf9152012-07-19 17:43:21210Shell* Shell::CreateNewWindow(BrowserContext* browser_context,
[email protected]e99ca5112011-09-26 17:22:54211 const GURL& url,
lukasza04130152016-10-21 20:26:32212 const scoped_refptr<SiteInstance>& site_instance,
[email protected]cdb806722013-01-10 14:18:23213 const gfx::Size& initial_size) {
[email protected]54944cde2012-12-09 09:24:59214 WebContents::CreateParams create_params(browser_context, site_instance);
mark a. foltzef394fce2017-10-21 09:11:02215 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
216 switches::kForcePresentationReceiverForTesting)) {
217 create_params.starting_sandbox_flags =
arthursonzognib93a4472020-04-10 07:38:00218 content::kPresentationReceiverSandboxFlags;
mark a. foltzef394fce2017-10-21 09:11:02219 }
erikchenbee5c9622018-04-27 19:30:25220 std::unique_ptr<WebContents> web_contents =
Erik Chenbb8e738e2018-04-28 14:10:43221 WebContents::Create(create_params);
erikchenbee5c9622018-04-27 19:30:25222 Shell* shell =
danakjfc5184932019-09-12 18:08:32223 CreateShell(std::move(web_contents), AdjustWindowSize(initial_size),
Bo Liu300c6052018-06-12 04:46:07224 true /* should_set_delegate */);
[email protected]e99ca5112011-09-26 17:22:54225 if (!url.is_empty())
226 shell->LoadURL(url);
[email protected]9fbd3f862011-09-20 23:31:34227 return shell;
228}
229
danakj24577b12020-05-13 22:38:18230void Shell::RenderViewReady() {
231 g_platform->RenderViewReady(this);
232}
233
[email protected]9fbd3f862011-09-20 23:31:34234void Shell::LoadURL(const GURL& url) {
Alex Moshchuk7e26eca2018-03-03 01:34:29235 LoadURLForFrame(
236 url, std::string(),
237 ui::PageTransitionFromInt(ui::PAGE_TRANSITION_TYPED |
238 ui::PAGE_TRANSITION_FROM_ADDRESS_BAR));
[email protected]d2494ff2013-02-20 08:22:37239}
240
Alex Moshchuk7e26eca2018-03-03 01:34:29241void Shell::LoadURLForFrame(const GURL& url,
242 const std::string& frame_name,
243 ui::PageTransition transition_type) {
[email protected]d2494ff2013-02-20 08:22:37244 NavigationController::LoadURLParams params(url);
[email protected]d2494ff2013-02-20 08:22:37245 params.frame_name = frame_name;
Alex Moshchuk7e26eca2018-03-03 01:34:29246 params.transition_type = transition_type;
[email protected]d2494ff2013-02-20 08:22:37247 web_contents_->GetController().LoadURLWithParams(params);
[email protected]9fbd3f862011-09-20 23:31:34248}
249
[email protected]76bdecb2014-04-16 17:58:08250void Shell::LoadDataWithBaseURL(const GURL& url, const std::string& data,
251 const GURL& base_url) {
boliuec93ea92016-02-17 22:23:07252 bool load_as_string = false;
253 LoadDataWithBaseURLInternal(url, data, base_url, load_as_string);
254}
255
256#if defined(OS_ANDROID)
257void Shell::LoadDataAsStringWithBaseURL(const GURL& url,
258 const std::string& data,
259 const GURL& base_url) {
260 bool load_as_string = true;
261 LoadDataWithBaseURLInternal(url, data, base_url, load_as_string);
262}
263#endif
264
265void Shell::LoadDataWithBaseURLInternal(const GURL& url,
266 const std::string& data,
267 const GURL& base_url,
268 bool load_as_string) {
269#if !defined(OS_ANDROID)
270 DCHECK(!load_as_string); // Only supported on Android.
271#endif
272
273 NavigationController::LoadURLParams params(GURL::EmptyGURL());
274 const std::string data_url_header = "data:text/html;charset=utf-8,";
275 if (load_as_string) {
276 params.url = GURL(data_url_header);
277 std::string data_url_as_string = data_url_header + data;
278#if defined(OS_ANDROID)
279 params.data_url_as_string =
280 base::RefCountedString::TakeString(&data_url_as_string);
281#endif
282 } else {
283 params.url = GURL(data_url_header + data);
284 }
285
[email protected]76bdecb2014-04-16 17:58:08286 params.load_type = NavigationController::LOAD_TYPE_DATA;
287 params.base_url_for_data_url = base_url;
288 params.virtual_url_for_data_url = url;
289 params.override_user_agent = NavigationController::UA_OVERRIDE_FALSE;
290 web_contents_->GetController().LoadURLWithParams(params);
[email protected]76bdecb2014-04-16 17:58:08291}
292
[email protected]a2904092013-10-15 04:53:59293void Shell::AddNewContents(WebContents* source,
erikchenbee5c9622018-04-27 19:30:25294 std::unique_ptr<WebContents> new_contents,
Joel Hockey891e88062020-04-30 05:38:44295 const GURL& target_url,
[email protected]a2904092013-10-15 04:53:59296 WindowOpenDisposition disposition,
bokan107a47f2015-02-03 23:23:39297 const gfx::Rect& initial_rect,
[email protected]a2904092013-10-15 04:53:59298 bool user_gesture,
299 bool* was_blocked) {
erikchenbee5c9622018-04-27 19:30:25300 WebContents* raw_new_contents = new_contents.get();
Bo Liu300c6052018-06-12 04:46:07301 CreateShell(
302 std::move(new_contents), AdjustWindowSize(initial_rect.size()),
303 !delay_popup_contents_delegate_for_testing_ /* should_set_delegate */);
Kent Tamuracd3ebc42018-05-16 06:44:22304 if (switches::IsRunWebTestsSwitchPresent())
erikchenbee5c9622018-04-27 19:30:25305 SecondaryTestWindowObserver::CreateForWebContents(raw_new_contents);
[email protected]a2904092013-10-15 04:53:59306}
307
[email protected]9fbd3f862011-09-20 23:31:34308void Shell::GoBackOrForward(int offset) {
[email protected]0b659b32012-03-26 21:29:32309 web_contents_->GetController().GoToOffset(offset);
[email protected]9fbd3f862011-09-20 23:31:34310}
311
312void Shell::Reload() {
toyoshim6142d96f2016-12-19 09:07:25313 web_contents_->GetController().Reload(ReloadType::NORMAL, false);
[email protected]9fbd3f862011-09-20 23:31:34314}
315
toyoshime5aaf6a2016-05-18 08:07:48316void Shell::ReloadBypassingCache() {
toyoshim6142d96f2016-12-19 09:07:25317 web_contents_->GetController().Reload(ReloadType::BYPASSING_CACHE, false);
toyoshime5aaf6a2016-05-18 08:07:48318}
319
[email protected]9fbd3f862011-09-20 23:31:34320void Shell::Stop() {
[email protected]0b659b32012-03-26 21:29:32321 web_contents_->Stop();
[email protected]9fbd3f862011-09-20 23:31:34322}
323
[email protected]e3b10d12014-03-28 16:06:09324void Shell::UpdateNavigationControls(bool to_different_document) {
[email protected]0b659b32012-03-26 21:29:32325 int current_index = web_contents_->GetController().GetCurrentEntryIndex();
326 int max_index = web_contents_->GetController().GetEntryCount() - 1;
[email protected]9fbd3f862011-09-20 23:31:34327
danakjde3e2a02020-05-12 16:51:20328 g_platform->EnableUIControl(this, ShellPlatformDelegate::BACK_BUTTON,
329 current_index > 0);
330 g_platform->EnableUIControl(this, ShellPlatformDelegate::FORWARD_BUTTON,
331 current_index < max_index);
332 g_platform->EnableUIControl(
333 this, ShellPlatformDelegate::STOP_BUTTON,
[email protected]e3b10d12014-03-28 16:06:09334 to_different_document && web_contents_->IsLoading());
[email protected]9fbd3f862011-09-20 23:31:34335}
336
[email protected]7c17b6992012-08-09 16:16:30337void Shell::ShowDevTools() {
mohsen6eb57fb2016-07-22 03:14:08338 if (!devtools_frontend_) {
339 devtools_frontend_ = ShellDevToolsFrontend::Show(web_contents());
340 devtools_observer_.reset(new DevToolsWebContentsObserver(
341 this, devtools_frontend_->frontend_shell()->web_contents()));
342 }
[email protected]3142e5d2014-02-07 00:54:46343
mohsen6eb57fb2016-07-22 03:14:08344 devtools_frontend_->Activate();
[email protected]7c17b6992012-08-09 16:16:30345}
346
[email protected]001841c92012-12-11 17:00:13347void Shell::CloseDevTools() {
[email protected]0773e0c2013-01-25 15:57:57348 if (!devtools_frontend_)
[email protected]001841c92012-12-11 17:00:13349 return;
[email protected]7fff43e2013-05-21 20:21:10350 devtools_observer_.reset();
[email protected]0773e0c2013-01-25 15:57:57351 devtools_frontend_->Close();
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28352 devtools_frontend_ = nullptr;
[email protected]001841c92012-12-11 17:00:13353}
354
danakjde3e2a02020-05-12 16:51:20355void Shell::ResizeWebContentForTests(const gfx::Size& content_size) {
356 g_platform->ResizeWebContent(this, content_size);
357}
danakjde3e2a02020-05-12 16:51:20358
[email protected]9fbd3f862011-09-20 23:31:34359gfx::NativeView Shell::GetContentView() {
[email protected]59383c782013-04-17 16:43:27360 if (!web_contents_)
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28361 return nullptr;
[email protected]fc2b46b2014-05-03 16:33:45362 return web_contents_->GetNativeView();
[email protected]9fbd3f862011-09-20 23:31:34363}
364
danakjde3e2a02020-05-12 16:51:20365#if !defined(OS_ANDROID)
366gfx::NativeWindow Shell::window() {
367 return g_platform->GetNativeWindow(this);
368}
369#endif
370
371#if defined(OS_MACOSX)
372void Shell::ActionPerformed(int control) {
373 switch (control) {
374 case IDC_NAV_BACK:
375 GoBackOrForward(-1);
376 break;
377 case IDC_NAV_FORWARD:
378 GoBackOrForward(1);
379 break;
380 case IDC_NAV_RELOAD:
381 Reload();
382 break;
383 case IDC_NAV_STOP:
384 Stop();
385 break;
386 }
387}
388
389void Shell::URLEntered(const std::string& url_string) {
390 if (!url_string.empty()) {
391 GURL url(url_string);
392 if (!url.has_scheme())
393 url = GURL("http://" + url_string);
394 LoadURL(url);
395 }
396}
397#endif
398
alexmos5a98a052016-01-06 00:15:02399WebContents* Shell::OpenURLFromTab(WebContents* source,
400 const OpenURLParams& params) {
lukasza04130152016-10-21 20:26:32401 WebContents* target = nullptr;
402 switch (params.disposition) {
403 case WindowOpenDisposition::CURRENT_TAB:
404 target = source;
405 break;
406
407 // Normally, the difference between NEW_POPUP and NEW_WINDOW is that a popup
408 // should have no toolbar, no status bar, no menu bar, no scrollbars and be
409 // not resizable. For simplicity and to enable new testing scenarios in
Kent Tamura21d1de62018-12-10 04:45:20410 // content shell and web tests, popups don't get special treatment below
lukasza04130152016-10-21 20:26:32411 // (i.e. they will have a toolbar and other things described here).
412 case WindowOpenDisposition::NEW_POPUP:
lukaszabe2f0da2017-04-25 00:43:00413 case WindowOpenDisposition::NEW_WINDOW:
Kent Tamura21d1de62018-12-10 04:45:20414 // content_shell doesn't really support tabs, but some web tests use
lukaszabe2f0da2017-04-25 00:43:00415 // middle click (which translates into kNavigationPolicyNewBackgroundTab),
416 // so we treat the cases below just like a NEW_WINDOW disposition.
417 case WindowOpenDisposition::NEW_BACKGROUND_TAB:
418 case WindowOpenDisposition::NEW_FOREGROUND_TAB: {
lukasza04130152016-10-21 20:26:32419 Shell* new_window =
420 Shell::CreateNewWindow(source->GetBrowserContext(),
421 GURL(), // Don't load anything just yet.
422 params.source_site_instance,
423 gfx::Size()); // Use default size.
424 target = new_window->web_contents();
Kent Tamuracd3ebc42018-05-16 06:44:22425 if (switches::IsRunWebTestsSwitchPresent())
lukasza04130152016-10-21 20:26:32426 SecondaryTestWindowObserver::CreateForWebContents(target);
427 break;
428 }
429
430 // No tabs in content_shell:
431 case WindowOpenDisposition::SINGLETON_TAB:
lukasza04130152016-10-21 20:26:32432 // No incognito mode in content_shell:
433 case WindowOpenDisposition::OFF_THE_RECORD:
Kent Tamura21d1de62018-12-10 04:45:20434 // TODO(lukasza): Investigate if some web tests might need support for
lukasza04130152016-10-21 20:26:32435 // SAVE_TO_DISK disposition. This would probably require that
Gyuyoung Kim26c7bc92020-04-29 00:53:00436 // WebTestControlHost always sets up and cleans up a temporary directory
lukasza04130152016-10-21 20:26:32437 // as the default downloads destinations for the duration of a test.
438 case WindowOpenDisposition::SAVE_TO_DISK:
439 // Ignoring requests with disposition == IGNORE_ACTION...
440 case WindowOpenDisposition::IGNORE_ACTION:
441 default:
442 return nullptr;
443 }
alexmos5a98a052016-01-06 00:15:02444
Lukasz Anforowicz82a5ca92019-10-24 18:45:37445 target->GetController().LoadURLWithParams(
446 NavigationController::LoadURLParams(params));
lukasza04130152016-10-21 20:26:32447 return target;
alexmos5a98a052016-01-06 00:15:02448}
449
[email protected]e3b10d12014-03-28 16:06:09450void Shell::LoadingStateChanged(WebContents* source,
451 bool to_different_document) {
452 UpdateNavigationControls(to_different_document);
danakjde3e2a02020-05-12 16:51:20453 g_platform->SetIsLoading(this, source->IsLoading());
[email protected]e99ca5112011-09-26 17:22:54454}
455
danakjde3e2a02020-05-12 16:51:20456#if defined(OS_ANDROID)
457void Shell::SetOverlayMode(bool use_overlay_mode) {
458 g_platform->SetOverlayMode(this, use_overlay_mode);
459}
460#endif
461
Dave Tapuskaa4189512019-10-15 20:27:34462void Shell::EnterFullscreenModeForTab(
Mike Wasserman4ca09792020-05-29 17:44:43463 RenderFrameHost* requesting_frame,
Dave Tapuskaa4189512019-10-15 20:27:34464 const blink::mojom::FullscreenOptions& options) {
Mike Wasserman4ca09792020-05-29 17:44:43465 ToggleFullscreenModeForTab(WebContents::FromRenderFrameHost(requesting_frame),
466 true);
mlamouri7a78d6fd2015-01-17 13:23:53467}
468
469void Shell::ExitFullscreenModeForTab(WebContents* web_contents) {
470 ToggleFullscreenModeForTab(web_contents, false);
471}
472
[email protected]99c014c2012-11-27 12:03:42473void Shell::ToggleFullscreenModeForTab(WebContents* web_contents,
474 bool enter_fullscreen) {
475#if defined(OS_ANDROID)
danakjde3e2a02020-05-12 16:51:20476 g_platform->ToggleFullscreenModeForTab(this, web_contents, enter_fullscreen);
[email protected]99c014c2012-11-27 12:03:42477#endif
[email protected]99c014c2012-11-27 12:03:42478 if (is_fullscreen_ != enter_fullscreen) {
479 is_fullscreen_ = enter_fullscreen;
Fady Samuel0b911822018-04-25 13:22:16480 web_contents->GetRenderViewHost()
481 ->GetWidget()
482 ->SynchronizeVisualProperties();
[email protected]99c014c2012-11-27 12:03:42483 }
484}
485
Lucas Furukawa Gadani4909f3c2019-06-18 22:36:52486bool Shell::IsFullscreenForTabOrPending(const WebContents* web_contents) {
[email protected]99c014c2012-11-27 12:03:42487#if defined(OS_ANDROID)
danakjde3e2a02020-05-12 16:51:20488 return g_platform->IsFullscreenForTabOrPending(this, web_contents);
[email protected]99c014c2012-11-27 12:03:42489#else
490 return is_fullscreen_;
491#endif
492}
493
Eric Willigers052f0432019-10-04 04:06:57494blink::mojom::DisplayMode Shell::GetDisplayMode(
495 const WebContents* web_contents) {
496 // TODO: should return blink::mojom::DisplayModeFullscreen wherever user puts
Lukasz Anforowicz52b93722018-06-20 16:11:39497 // a browser window into fullscreen (not only in case of renderer-initiated
498 // fullscreen mode): crbug.com/476874.
499 return IsFullscreenForTabOrPending(web_contents)
Eric Willigers052f0432019-10-04 04:06:57500 ? blink::mojom::DisplayMode::kFullscreen
501 : blink::mojom::DisplayMode::kBrowser;
mikhail.pozdnyakovc0e251b2015-04-15 06:51:12502}
503
[email protected]f78439002012-11-28 14:45:59504void Shell::RequestToLockMouse(WebContents* web_contents,
505 bool user_gesture,
506 bool last_unlocked_by_target) {
James Hollyerd5c9de462020-03-10 19:02:45507 web_contents->GotResponseToLockMouseRequest(
508 blink::mojom::PointerLockResult::kSuccess);
[email protected]f78439002012-11-28 14:45:59509}
510
danakjde3e2a02020-05-12 16:51:20511void Shell::Close() {
512 // Shell is "self-owned" and destroys itself. The ShellPlatformDelegate
513 // has the chance to co-opt this and do its own destruction.
514 if (!g_platform->DestroyShell(this))
515 delete this;
516}
517
[email protected]9e00e6352012-07-30 17:05:18518void Shell::CloseContents(WebContents* source) {
519 Close();
520}
521
Lucas Furukawa Gadani4909f3c2019-06-18 22:36:52522bool Shell::CanOverscrollContent() {
[email protected]067310262012-11-22 14:30:41523#if defined(USE_AURA)
524 return true;
525#else
526 return false;
527#endif
528}
529
Mugdha Lakhani5f8de7cc2020-03-10 20:43:36530void Shell::NavigationStateChanged(WebContents* source,
531 InvalidateTypes changed_flags) {
532 if (changed_flags & INVALIDATE_TYPE_URL)
danakjde3e2a02020-05-12 16:51:20533 g_platform->SetAddressBarURL(this, source->GetVisibleURL());
[email protected]e99ca5112011-09-26 17:22:54534}
535
mathiash72a5e462014-11-19 08:18:50536JavaScriptDialogManager* Shell::GetJavaScriptDialogManager(
537 WebContents* source) {
mkwst6efe7f2e62014-10-08 15:09:01538 if (!dialog_manager_) {
danakjaf29c6072020-03-31 16:25:01539 if (switches::IsRunWebTestsSwitchPresent())
540 dialog_manager_ = std::make_unique<WebTestJavaScriptDialogManager>();
541 else
542 dialog_manager_ = std::make_unique<ShellJavaScriptDialogManager>();
mkwst6efe7f2e62014-10-08 15:09:01543 }
[email protected]71a88bb2013-02-01 22:05:15544 return dialog_manager_.get();
[email protected]f2210022012-03-29 00:36:08545}
546
dcheng6003e0b2016-04-09 18:42:34547std::unique_ptr<BluetoothChooser> Shell::RunBluetoothChooser(
jyasskin93649a52016-02-17 19:57:02548 RenderFrameHost* frame,
549 const BluetoothChooser::EventHandler& event_handler) {
Gyuyoung Kim26c7bc92020-04-29 00:53:00550 WebTestControlHost* web_test_control_host = WebTestControlHost::Get();
danakj9dc21fb2020-06-17 00:00:23551 if (web_test_control_host)
Gyuyoung Kim26c7bc92020-04-29 00:53:00552 return web_test_control_host->RunBluetoothChooser(frame, event_handler);
jyasskin8057c002015-09-10 19:09:53553 return nullptr;
554}
555
danakj378b97f2020-06-11 02:40:52556class AlwaysAllowBluetoothScanning : public BluetoothScanningPrompt {
557 public:
558 explicit AlwaysAllowBluetoothScanning(const EventHandler& event_handler) {
559 event_handler.Run(content::BluetoothScanningPrompt::Event::kAllow);
560 }
561};
562
Jun Cai149002e2019-05-09 23:13:07563std::unique_ptr<BluetoothScanningPrompt> Shell::ShowBluetoothScanningPrompt(
564 RenderFrameHost* frame,
565 const BluetoothScanningPrompt::EventHandler& event_handler) {
danakj378b97f2020-06-11 02:40:52566 return std::make_unique<AlwaysAllowBluetoothScanning>(event_handler);
Jun Cai149002e2019-05-09 23:13:07567}
568
danakjde3e2a02020-05-12 16:51:20569#if defined(OS_MACOSX)
570bool Shell::HandleKeyboardEvent(WebContents* source,
571 const NativeWebKeyboardEvent& event) {
572 return g_platform->HandleKeyboardEvent(this, source, event);
573}
574#endif
575
avia90ae4e2016-11-11 20:49:33576bool Shell::DidAddMessageToConsole(WebContents* source,
Lowell Manners1de5242e2019-04-25 10:18:46577 blink::mojom::ConsoleMessageLevel log_level,
avia90ae4e2016-11-11 20:49:33578 const base::string16& message,
579 int32_t line_no,
580 const base::string16& source_id) {
Kent Tamuracd3ebc42018-05-16 06:44:22581 return switches::IsRunWebTestsSwitchPresent();
[email protected]efb5f572012-01-29 10:57:33582}
583
Lucas Furukawa Gadani06548fb2019-01-15 05:01:25584void Shell::PortalWebContentsCreated(WebContents* portal_web_contents) {
585 if (switches::IsRunWebTestsSwitchPresent())
586 SecondaryTestWindowObserver::CreateForWebContents(portal_web_contents);
587}
588
Lukasz Anforowicz52b93722018-06-20 16:11:39589void Shell::RendererUnresponsive(
590 WebContents* source,
591 RenderWidgetHost* render_widget_host,
592 base::RepeatingClosure hang_monitor_restarter) {
Gyuyoung Kim26c7bc92020-04-29 00:53:00593 WebTestControlHost* web_test_control_host = WebTestControlHost::Get();
danakj9dc21fb2020-06-17 00:00:23594 if (web_test_control_host)
Gyuyoung Kim26c7bc92020-04-29 00:53:00595 web_test_control_host->RendererUnresponsive();
[email protected]5bf68f22012-08-31 07:38:10596}
597
[email protected]233567d2013-02-27 20:22:02598void Shell::ActivateContents(WebContents* contents) {
danakj674bf1c02020-05-01 18:37:51599#if !defined(OS_MACOSX)
600 contents->Focus();
601#else
602 // Mac headless mode is quite different than other platforms. Normally
603 // focusing the WebContents would cause the OS to focus the window. Because
604 // headless mac doesn't actually have system windows, we can't go down the
605 // normal path and have to fake it out in the browser process.
danakjde3e2a02020-05-12 16:51:20606 g_platform->ActivateContents(this, contents);
danakj674bf1c02020-05-01 18:37:51607#endif
[email protected]233567d2013-02-27 20:22:02608}
609
Kevin McNee7422bc42020-02-20 18:49:55610std::unique_ptr<WebContents> Shell::ActivatePortalWebContents(
611 WebContents* predecessor_contents,
612 std::unique_ptr<WebContents> portal_contents) {
613 DCHECK_EQ(predecessor_contents, web_contents_.get());
614 portal_contents->SetDelegate(this);
Lucas Furukawa Gadanic5c0cd02018-10-04 20:32:33615 web_contents_->SetDelegate(nullptr);
Adithya Srinivasan5d50d1e2019-10-09 00:27:48616 for (auto* shell_devtools_bindings :
Kevin McNee7422bc42020-02-20 18:49:55617 ShellDevToolsBindings::GetInstancesForWebContents(
618 predecessor_contents)) {
619 shell_devtools_bindings->UpdateInspectedWebContents(portal_contents.get());
Adithya Srinivasan5d50d1e2019-10-09 00:27:48620 }
Kevin McNee7422bc42020-02-20 18:49:55621 std::swap(web_contents_, portal_contents);
danakjde3e2a02020-05-12 16:51:20622 g_platform->SetContents(this);
623 g_platform->SetAddressBarURL(this, web_contents_->GetVisibleURL());
Lucas Furukawa Gadanic5c0cd02018-10-04 20:32:33624 LoadingStateChanged(web_contents_.get(), true);
Kevin McNee7422bc42020-02-20 18:49:55625 return portal_contents;
Lucas Furukawa Gadanic5c0cd02018-10-04 20:32:33626}
627
Lukasz Anforowicz82a5ca92019-10-24 18:45:37628bool Shell::ShouldAllowRunningInsecureContent(WebContents* web_contents,
629 bool allowed_per_prefs,
630 const url::Origin& origin,
631 const GURL& resource_url) {
carloskd9d97942017-02-16 08:58:09632 bool allowed_by_test = false;
Gyuyoung Kim26c7bc92020-04-29 00:53:00633 WebTestControlHost* web_test_control_host = WebTestControlHost::Get();
danakj9dc21fb2020-06-17 00:00:23634 if (web_test_control_host) {
carloskd9d97942017-02-16 08:58:09635 const base::DictionaryValue& test_flags =
Gyuyoung Kim26c7bc92020-04-29 00:53:00636 web_test_control_host->accumulated_web_test_runtime_flags_changes();
carloskd9d97942017-02-16 08:58:09637 test_flags.GetBoolean("running_insecure_content_allowed", &allowed_by_test);
638 }
639
640 return allowed_per_prefs || allowed_by_test;
641}
642
Becca Hughes112832e2019-06-11 17:19:02643PictureInPictureResult Shell::EnterPictureInPicture(
Lukasz Anforowicz82a5ca92019-10-24 18:45:37644 WebContents* web_contents,
Becca Hughes112832e2019-06-11 17:19:02645 const viz::SurfaceId& surface_id,
646 const gfx::Size& natural_size) {
647 // During tests, returning success to pretend the window was created and allow
648 // tests to run accordingly.
649 if (!switches::IsRunWebTestsSwitchPresent())
650 return PictureInPictureResult::kNotSupported;
Becca Hughes112832e2019-06-11 17:19:02651 return PictureInPictureResult::kSuccess;
Mounir Lamouri11e9ef432018-05-22 03:10:16652}
653
Bo Liu300c6052018-06-12 04:46:07654bool Shell::ShouldResumeRequestsForCreatedWindow() {
655 return !delay_popup_contents_delegate_for_testing_;
656}
657
danakjee2390a82020-06-10 16:53:37658void Shell::SetContentsBounds(WebContents* source, const gfx::Rect& bounds) {
659 DCHECK(source == web_contents()); // There's only one WebContents per Shell.
660
661 if (switches::IsRunWebTestsSwitchPresent()) {
662 // Note that chrome drops these requests on normal windows.
663 // TODO(danakj): The position is dropped here but we use the size. Web tests
664 // can't move the window in headless mode anyways, but maybe we should be
665 // letting them pretend?
666 g_platform->ResizeWebContent(this, bounds.size());
667 }
668}
669
pdrcab84ee2015-03-13 21:47:04670gfx::Size Shell::GetShellDefaultSize() {
671 static gfx::Size default_shell_size;
672 if (!default_shell_size.IsEmpty())
673 return default_shell_size;
674 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
675 if (command_line->HasSwitch(switches::kContentShellHostWindowSize)) {
676 const std::string size_str = command_line->GetSwitchValueASCII(
677 switches::kContentShellHostWindowSize);
678 int width, height;
679 CHECK_EQ(2, sscanf(size_str.c_str(), "%dx%d", &width, &height));
680 default_shell_size = gfx::Size(width, height);
681 } else {
682 default_shell_size = gfx::Size(
683 kDefaultTestWindowWidthDip, kDefaultTestWindowHeightDip);
684 }
685 return default_shell_size;
686}
687
danakjde3e2a02020-05-12 16:51:20688#if defined(OS_ANDROID)
689void Shell::LoadProgressChanged(double progress) {
690 g_platform->LoadProgressChanged(this, progress);
691}
692#endif
693
Avi Drissman93002212017-09-27 03:20:52694void Shell::TitleWasSet(NavigationEntry* entry) {
[email protected]1ef02d242013-10-07 16:18:53695 if (entry)
danakjde3e2a02020-05-12 16:51:20696 g_platform->SetTitle(this, entry->GetTitle());
[email protected]aecc085b2012-06-01 18:15:53697}
698
[email protected]7fff43e2013-05-21 20:21:10699void Shell::OnDevToolsWebContentsDestroyed() {
700 devtools_observer_.reset();
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28701 devtools_frontend_ = nullptr;
[email protected]7fff43e2013-05-21 20:21:10702}
703
[email protected]9fbd3f862011-09-20 23:31:34704} // namespace content