blob: 89ee038af33c3c229193a330dc9898331194c240 [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"
Mario Sanchez Prada0bd8b8c2020-10-21 17:49:2343#include "third_party/blink/public/common/renderer_preferences/renderer_preferences.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);
danakj3dd7a6102020-12-30 19:58:39147 // If the RenderFrame was created during WebContents construction (as happens
148 // for windows opened from the renderer) then the Shell won't hear about the
149 // main frame being created as a WebContentsObservers. This gives the delegate
150 // a chance to act on the main frame accordingly.
151 if (raw_web_contents->GetMainFrame()->IsRenderFrameCreated())
152 g_platform->MainFrameCreated(shell);
danakj7da833932020-06-23 21:49:40153
[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 */);
danakj7da833932020-06-23 21:49:40225
[email protected]e99ca5112011-09-26 17:22:54226 if (!url.is_empty())
227 shell->LoadURL(url);
[email protected]9fbd3f862011-09-20 23:31:34228 return shell;
229}
230
danakj3dd7a6102020-12-30 19:58:39231void Shell::RenderFrameCreated(RenderFrameHost* frame_host) {
232 if (frame_host == web_contents_->GetMainFrame())
233 g_platform->MainFrameCreated(this);
danakj24577b12020-05-13 22:38:18234}
235
[email protected]9fbd3f862011-09-20 23:31:34236void Shell::LoadURL(const GURL& url) {
Alex Moshchuk7e26eca2018-03-03 01:34:29237 LoadURLForFrame(
238 url, std::string(),
239 ui::PageTransitionFromInt(ui::PAGE_TRANSITION_TYPED |
240 ui::PAGE_TRANSITION_FROM_ADDRESS_BAR));
[email protected]d2494ff2013-02-20 08:22:37241}
242
Alex Moshchuk7e26eca2018-03-03 01:34:29243void Shell::LoadURLForFrame(const GURL& url,
244 const std::string& frame_name,
245 ui::PageTransition transition_type) {
[email protected]d2494ff2013-02-20 08:22:37246 NavigationController::LoadURLParams params(url);
[email protected]d2494ff2013-02-20 08:22:37247 params.frame_name = frame_name;
Alex Moshchuk7e26eca2018-03-03 01:34:29248 params.transition_type = transition_type;
[email protected]d2494ff2013-02-20 08:22:37249 web_contents_->GetController().LoadURLWithParams(params);
[email protected]9fbd3f862011-09-20 23:31:34250}
251
[email protected]76bdecb2014-04-16 17:58:08252void Shell::LoadDataWithBaseURL(const GURL& url, const std::string& data,
253 const GURL& base_url) {
boliuec93ea92016-02-17 22:23:07254 bool load_as_string = false;
255 LoadDataWithBaseURLInternal(url, data, base_url, load_as_string);
256}
257
258#if defined(OS_ANDROID)
259void Shell::LoadDataAsStringWithBaseURL(const GURL& url,
260 const std::string& data,
261 const GURL& base_url) {
262 bool load_as_string = true;
263 LoadDataWithBaseURLInternal(url, data, base_url, load_as_string);
264}
265#endif
266
267void Shell::LoadDataWithBaseURLInternal(const GURL& url,
268 const std::string& data,
269 const GURL& base_url,
270 bool load_as_string) {
271#if !defined(OS_ANDROID)
272 DCHECK(!load_as_string); // Only supported on Android.
273#endif
274
275 NavigationController::LoadURLParams params(GURL::EmptyGURL());
276 const std::string data_url_header = "data:text/html;charset=utf-8,";
277 if (load_as_string) {
278 params.url = GURL(data_url_header);
279 std::string data_url_as_string = data_url_header + data;
280#if defined(OS_ANDROID)
281 params.data_url_as_string =
282 base::RefCountedString::TakeString(&data_url_as_string);
283#endif
284 } else {
285 params.url = GURL(data_url_header + data);
286 }
287
[email protected]76bdecb2014-04-16 17:58:08288 params.load_type = NavigationController::LOAD_TYPE_DATA;
289 params.base_url_for_data_url = base_url;
290 params.virtual_url_for_data_url = url;
291 params.override_user_agent = NavigationController::UA_OVERRIDE_FALSE;
292 web_contents_->GetController().LoadURLWithParams(params);
[email protected]76bdecb2014-04-16 17:58:08293}
294
[email protected]a2904092013-10-15 04:53:59295void Shell::AddNewContents(WebContents* source,
erikchenbee5c9622018-04-27 19:30:25296 std::unique_ptr<WebContents> new_contents,
Joel Hockey891e88062020-04-30 05:38:44297 const GURL& target_url,
[email protected]a2904092013-10-15 04:53:59298 WindowOpenDisposition disposition,
bokan107a47f2015-02-03 23:23:39299 const gfx::Rect& initial_rect,
[email protected]a2904092013-10-15 04:53:59300 bool user_gesture,
301 bool* was_blocked) {
Bo Liu300c6052018-06-12 04:46:07302 CreateShell(
303 std::move(new_contents), AdjustWindowSize(initial_rect.size()),
304 !delay_popup_contents_delegate_for_testing_ /* should_set_delegate */);
[email protected]a2904092013-10-15 04:53:59305}
306
[email protected]9fbd3f862011-09-20 23:31:34307void Shell::GoBackOrForward(int offset) {
[email protected]0b659b32012-03-26 21:29:32308 web_contents_->GetController().GoToOffset(offset);
[email protected]9fbd3f862011-09-20 23:31:34309}
310
311void Shell::Reload() {
toyoshim6142d96f2016-12-19 09:07:25312 web_contents_->GetController().Reload(ReloadType::NORMAL, false);
[email protected]9fbd3f862011-09-20 23:31:34313}
314
toyoshime5aaf6a2016-05-18 08:07:48315void Shell::ReloadBypassingCache() {
toyoshim6142d96f2016-12-19 09:07:25316 web_contents_->GetController().Reload(ReloadType::BYPASSING_CACHE, false);
toyoshime5aaf6a2016-05-18 08:07:48317}
318
[email protected]9fbd3f862011-09-20 23:31:34319void Shell::Stop() {
[email protected]0b659b32012-03-26 21:29:32320 web_contents_->Stop();
[email protected]9fbd3f862011-09-20 23:31:34321}
322
[email protected]e3b10d12014-03-28 16:06:09323void Shell::UpdateNavigationControls(bool to_different_document) {
[email protected]0b659b32012-03-26 21:29:32324 int current_index = web_contents_->GetController().GetCurrentEntryIndex();
325 int max_index = web_contents_->GetController().GetEntryCount() - 1;
[email protected]9fbd3f862011-09-20 23:31:34326
danakjde3e2a02020-05-12 16:51:20327 g_platform->EnableUIControl(this, ShellPlatformDelegate::BACK_BUTTON,
328 current_index > 0);
329 g_platform->EnableUIControl(this, ShellPlatformDelegate::FORWARD_BUTTON,
330 current_index < max_index);
331 g_platform->EnableUIControl(
332 this, ShellPlatformDelegate::STOP_BUTTON,
[email protected]e3b10d12014-03-28 16:06:09333 to_different_document && web_contents_->IsLoading());
[email protected]9fbd3f862011-09-20 23:31:34334}
335
[email protected]7c17b6992012-08-09 16:16:30336void Shell::ShowDevTools() {
mohsen6eb57fb2016-07-22 03:14:08337 if (!devtools_frontend_) {
338 devtools_frontend_ = ShellDevToolsFrontend::Show(web_contents());
339 devtools_observer_.reset(new DevToolsWebContentsObserver(
340 this, devtools_frontend_->frontend_shell()->web_contents()));
341 }
[email protected]3142e5d2014-02-07 00:54:46342
mohsen6eb57fb2016-07-22 03:14:08343 devtools_frontend_->Activate();
[email protected]7c17b6992012-08-09 16:16:30344}
345
[email protected]001841c92012-12-11 17:00:13346void Shell::CloseDevTools() {
[email protected]0773e0c2013-01-25 15:57:57347 if (!devtools_frontend_)
[email protected]001841c92012-12-11 17:00:13348 return;
[email protected]7fff43e2013-05-21 20:21:10349 devtools_observer_.reset();
[email protected]0773e0c2013-01-25 15:57:57350 devtools_frontend_->Close();
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28351 devtools_frontend_ = nullptr;
[email protected]001841c92012-12-11 17:00:13352}
353
danakjde3e2a02020-05-12 16:51:20354void Shell::ResizeWebContentForTests(const gfx::Size& content_size) {
355 g_platform->ResizeWebContent(this, content_size);
356}
danakjde3e2a02020-05-12 16:51:20357
[email protected]9fbd3f862011-09-20 23:31:34358gfx::NativeView Shell::GetContentView() {
[email protected]59383c782013-04-17 16:43:27359 if (!web_contents_)
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28360 return nullptr;
[email protected]fc2b46b2014-05-03 16:33:45361 return web_contents_->GetNativeView();
[email protected]9fbd3f862011-09-20 23:31:34362}
363
danakjde3e2a02020-05-12 16:51:20364#if !defined(OS_ANDROID)
365gfx::NativeWindow Shell::window() {
366 return g_platform->GetNativeWindow(this);
367}
368#endif
369
Avi Drissman7c57be72020-07-29 20:09:46370#if defined(OS_MAC)
danakjde3e2a02020-05-12 16:51:20371void Shell::ActionPerformed(int control) {
372 switch (control) {
373 case IDC_NAV_BACK:
374 GoBackOrForward(-1);
375 break;
376 case IDC_NAV_FORWARD:
377 GoBackOrForward(1);
378 break;
379 case IDC_NAV_RELOAD:
380 Reload();
381 break;
382 case IDC_NAV_STOP:
383 Stop();
384 break;
385 }
386}
387
388void Shell::URLEntered(const std::string& url_string) {
389 if (!url_string.empty()) {
390 GURL url(url_string);
391 if (!url.has_scheme())
392 url = GURL("http://" + url_string);
393 LoadURL(url);
394 }
395}
396#endif
397
alexmos5a98a052016-01-06 00:15:02398WebContents* Shell::OpenURLFromTab(WebContents* source,
399 const OpenURLParams& params) {
lukasza04130152016-10-21 20:26:32400 WebContents* target = nullptr;
401 switch (params.disposition) {
402 case WindowOpenDisposition::CURRENT_TAB:
403 target = source;
404 break;
405
406 // Normally, the difference between NEW_POPUP and NEW_WINDOW is that a popup
407 // should have no toolbar, no status bar, no menu bar, no scrollbars and be
408 // not resizable. For simplicity and to enable new testing scenarios in
Kent Tamura21d1de62018-12-10 04:45:20409 // content shell and web tests, popups don't get special treatment below
lukasza04130152016-10-21 20:26:32410 // (i.e. they will have a toolbar and other things described here).
411 case WindowOpenDisposition::NEW_POPUP:
lukaszabe2f0da2017-04-25 00:43:00412 case WindowOpenDisposition::NEW_WINDOW:
Kent Tamura21d1de62018-12-10 04:45:20413 // content_shell doesn't really support tabs, but some web tests use
lukaszabe2f0da2017-04-25 00:43:00414 // middle click (which translates into kNavigationPolicyNewBackgroundTab),
415 // so we treat the cases below just like a NEW_WINDOW disposition.
416 case WindowOpenDisposition::NEW_BACKGROUND_TAB:
417 case WindowOpenDisposition::NEW_FOREGROUND_TAB: {
lukasza04130152016-10-21 20:26:32418 Shell* new_window =
419 Shell::CreateNewWindow(source->GetBrowserContext(),
420 GURL(), // Don't load anything just yet.
421 params.source_site_instance,
422 gfx::Size()); // Use default size.
423 target = new_window->web_contents();
lukasza04130152016-10-21 20:26:32424 break;
425 }
426
427 // No tabs in content_shell:
428 case WindowOpenDisposition::SINGLETON_TAB:
lukasza04130152016-10-21 20:26:32429 // No incognito mode in content_shell:
430 case WindowOpenDisposition::OFF_THE_RECORD:
Kent Tamura21d1de62018-12-10 04:45:20431 // TODO(lukasza): Investigate if some web tests might need support for
lukasza04130152016-10-21 20:26:32432 // SAVE_TO_DISK disposition. This would probably require that
Gyuyoung Kim26c7bc92020-04-29 00:53:00433 // WebTestControlHost always sets up and cleans up a temporary directory
lukasza04130152016-10-21 20:26:32434 // as the default downloads destinations for the duration of a test.
435 case WindowOpenDisposition::SAVE_TO_DISK:
436 // Ignoring requests with disposition == IGNORE_ACTION...
437 case WindowOpenDisposition::IGNORE_ACTION:
438 default:
439 return nullptr;
440 }
alexmos5a98a052016-01-06 00:15:02441
Lukasz Anforowicz82a5ca92019-10-24 18:45:37442 target->GetController().LoadURLWithParams(
443 NavigationController::LoadURLParams(params));
lukasza04130152016-10-21 20:26:32444 return target;
alexmos5a98a052016-01-06 00:15:02445}
446
[email protected]e3b10d12014-03-28 16:06:09447void Shell::LoadingStateChanged(WebContents* source,
448 bool to_different_document) {
449 UpdateNavigationControls(to_different_document);
danakjde3e2a02020-05-12 16:51:20450 g_platform->SetIsLoading(this, source->IsLoading());
[email protected]e99ca5112011-09-26 17:22:54451}
452
danakjde3e2a02020-05-12 16:51:20453#if defined(OS_ANDROID)
454void Shell::SetOverlayMode(bool use_overlay_mode) {
455 g_platform->SetOverlayMode(this, use_overlay_mode);
456}
457#endif
458
Dave Tapuskaa4189512019-10-15 20:27:34459void Shell::EnterFullscreenModeForTab(
Mike Wasserman4ca09792020-05-29 17:44:43460 RenderFrameHost* requesting_frame,
Dave Tapuskaa4189512019-10-15 20:27:34461 const blink::mojom::FullscreenOptions& options) {
Mike Wasserman4ca09792020-05-29 17:44:43462 ToggleFullscreenModeForTab(WebContents::FromRenderFrameHost(requesting_frame),
463 true);
mlamouri7a78d6fd2015-01-17 13:23:53464}
465
466void Shell::ExitFullscreenModeForTab(WebContents* web_contents) {
467 ToggleFullscreenModeForTab(web_contents, false);
468}
469
[email protected]99c014c2012-11-27 12:03:42470void Shell::ToggleFullscreenModeForTab(WebContents* web_contents,
471 bool enter_fullscreen) {
472#if defined(OS_ANDROID)
danakjde3e2a02020-05-12 16:51:20473 g_platform->ToggleFullscreenModeForTab(this, web_contents, enter_fullscreen);
[email protected]99c014c2012-11-27 12:03:42474#endif
[email protected]99c014c2012-11-27 12:03:42475 if (is_fullscreen_ != enter_fullscreen) {
476 is_fullscreen_ = enter_fullscreen;
Fady Samuel0b911822018-04-25 13:22:16477 web_contents->GetRenderViewHost()
478 ->GetWidget()
479 ->SynchronizeVisualProperties();
[email protected]99c014c2012-11-27 12:03:42480 }
481}
482
Lucas Furukawa Gadani4909f3c2019-06-18 22:36:52483bool Shell::IsFullscreenForTabOrPending(const WebContents* web_contents) {
[email protected]99c014c2012-11-27 12:03:42484#if defined(OS_ANDROID)
danakjde3e2a02020-05-12 16:51:20485 return g_platform->IsFullscreenForTabOrPending(this, web_contents);
[email protected]99c014c2012-11-27 12:03:42486#else
487 return is_fullscreen_;
488#endif
489}
490
Eric Willigers052f0432019-10-04 04:06:57491blink::mojom::DisplayMode Shell::GetDisplayMode(
492 const WebContents* web_contents) {
493 // TODO: should return blink::mojom::DisplayModeFullscreen wherever user puts
Lukasz Anforowicz52b93722018-06-20 16:11:39494 // a browser window into fullscreen (not only in case of renderer-initiated
495 // fullscreen mode): crbug.com/476874.
496 return IsFullscreenForTabOrPending(web_contents)
Eric Willigers052f0432019-10-04 04:06:57497 ? blink::mojom::DisplayMode::kFullscreen
498 : blink::mojom::DisplayMode::kBrowser;
mikhail.pozdnyakovc0e251b2015-04-15 06:51:12499}
500
[email protected]f78439002012-11-28 14:45:59501void Shell::RequestToLockMouse(WebContents* web_contents,
502 bool user_gesture,
503 bool last_unlocked_by_target) {
Dave Tapuskab4998782020-10-08 17:22:47504 // Give the platform a chance to handle the lock request, if it doesn't
505 // indicate it handled it, allow the request.
506 if (!g_platform->HandleRequestToLockMouse(this, web_contents, user_gesture,
507 last_unlocked_by_target)) {
508 web_contents->GotResponseToLockMouseRequest(
509 blink::mojom::PointerLockResult::kSuccess);
510 }
[email protected]f78439002012-11-28 14:45:59511}
512
danakjde3e2a02020-05-12 16:51:20513void Shell::Close() {
514 // Shell is "self-owned" and destroys itself. The ShellPlatformDelegate
515 // has the chance to co-opt this and do its own destruction.
516 if (!g_platform->DestroyShell(this))
517 delete this;
518}
519
[email protected]9e00e6352012-07-30 17:05:18520void Shell::CloseContents(WebContents* source) {
521 Close();
522}
523
Lucas Furukawa Gadani4909f3c2019-06-18 22:36:52524bool Shell::CanOverscrollContent() {
[email protected]067310262012-11-22 14:30:41525#if defined(USE_AURA)
526 return true;
527#else
528 return false;
529#endif
530}
531
Mugdha Lakhani5f8de7cc2020-03-10 20:43:36532void Shell::NavigationStateChanged(WebContents* source,
533 InvalidateTypes changed_flags) {
534 if (changed_flags & INVALIDATE_TYPE_URL)
danakjde3e2a02020-05-12 16:51:20535 g_platform->SetAddressBarURL(this, source->GetVisibleURL());
[email protected]e99ca5112011-09-26 17:22:54536}
537
mathiash72a5e462014-11-19 08:18:50538JavaScriptDialogManager* Shell::GetJavaScriptDialogManager(
539 WebContents* source) {
danakj8de3b7492020-07-02 22:41:42540 if (!dialog_manager_)
541 dialog_manager_ = g_platform->CreateJavaScriptDialogManager(this);
542 if (!dialog_manager_)
543 dialog_manager_ = std::make_unique<ShellJavaScriptDialogManager>();
[email protected]71a88bb2013-02-01 22:05:15544 return dialog_manager_.get();
[email protected]f2210022012-03-29 00:36:08545}
546
Avi Drissman7c57be72020-07-29 20:09:46547#if defined(OS_MAC)
Xianzhu Wang0f021a82020-07-03 01:29:47548void Shell::DidNavigateMainFramePostCommit(WebContents* contents) {
549 g_platform->DidNavigateMainFramePostCommit(this, contents);
550}
551
danakjde3e2a02020-05-12 16:51:20552bool Shell::HandleKeyboardEvent(WebContents* source,
553 const NativeWebKeyboardEvent& event) {
554 return g_platform->HandleKeyboardEvent(this, source, event);
555}
556#endif
557
avia90ae4e2016-11-11 20:49:33558bool Shell::DidAddMessageToConsole(WebContents* source,
Lowell Manners1de5242e2019-04-25 10:18:46559 blink::mojom::ConsoleMessageLevel log_level,
avia90ae4e2016-11-11 20:49:33560 const base::string16& message,
561 int32_t line_no,
562 const base::string16& source_id) {
Kent Tamuracd3ebc42018-05-16 06:44:22563 return switches::IsRunWebTestsSwitchPresent();
[email protected]efb5f572012-01-29 10:57:33564}
565
Lucas Furukawa Gadani06548fb2019-01-15 05:01:25566void Shell::PortalWebContentsCreated(WebContents* portal_web_contents) {
danakj8de3b7492020-07-02 22:41:42567 g_platform->DidCreateOrAttachWebContents(this, portal_web_contents);
Lucas Furukawa Gadani06548fb2019-01-15 05:01:25568}
569
Lukasz Anforowicz52b93722018-06-20 16:11:39570void Shell::RendererUnresponsive(
571 WebContents* source,
572 RenderWidgetHost* render_widget_host,
573 base::RepeatingClosure hang_monitor_restarter) {
danakj245441f2020-07-03 15:18:41574 LOG(WARNING) << "renderer unresponsive";
[email protected]5bf68f22012-08-31 07:38:10575}
576
[email protected]233567d2013-02-27 20:22:02577void Shell::ActivateContents(WebContents* contents) {
Avi Drissman7c57be72020-07-29 20:09:46578#if !defined(OS_MAC)
danakjd4b48df52020-07-02 18:16:48579 // TODO(danakj): Move this to ShellPlatformDelegate.
danakj674bf1c02020-05-01 18:37:51580 contents->Focus();
581#else
582 // Mac headless mode is quite different than other platforms. Normally
583 // focusing the WebContents would cause the OS to focus the window. Because
584 // headless mac doesn't actually have system windows, we can't go down the
585 // normal path and have to fake it out in the browser process.
danakjde3e2a02020-05-12 16:51:20586 g_platform->ActivateContents(this, contents);
danakj674bf1c02020-05-01 18:37:51587#endif
[email protected]233567d2013-02-27 20:22:02588}
589
Kevin McNee7422bc42020-02-20 18:49:55590std::unique_ptr<WebContents> Shell::ActivatePortalWebContents(
591 WebContents* predecessor_contents,
592 std::unique_ptr<WebContents> portal_contents) {
593 DCHECK_EQ(predecessor_contents, web_contents_.get());
594 portal_contents->SetDelegate(this);
Lucas Furukawa Gadanic5c0cd02018-10-04 20:32:33595 web_contents_->SetDelegate(nullptr);
Kevin McNee7422bc42020-02-20 18:49:55596 std::swap(web_contents_, portal_contents);
danakjde3e2a02020-05-12 16:51:20597 g_platform->SetContents(this);
598 g_platform->SetAddressBarURL(this, web_contents_->GetVisibleURL());
Lucas Furukawa Gadanic5c0cd02018-10-04 20:32:33599 LoadingStateChanged(web_contents_.get(), true);
Kevin McNee7422bc42020-02-20 18:49:55600 return portal_contents;
Lucas Furukawa Gadanic5c0cd02018-10-04 20:32:33601}
602
Adithya Srinivasanb7204c82020-08-17 14:26:33603namespace {
604class PendingCallback : public base::RefCounted<PendingCallback> {
605 public:
606 explicit PendingCallback(base::OnceCallback<void()> cb)
607 : callback_(std::move(cb)) {}
608
609 private:
610 friend class base::RefCounted<PendingCallback>;
611 ~PendingCallback() { std::move(callback_).Run(); }
612 base::OnceCallback<void()> callback_;
613};
614} // namespace
615
616void Shell::UpdateInspectedWebContentsIfNecessary(
617 content::WebContents* old_contents,
618 content::WebContents* new_contents,
619 base::OnceCallback<void()> callback) {
620 scoped_refptr<PendingCallback> pending_callback =
621 base::MakeRefCounted<PendingCallback>(std::move(callback));
622 for (auto* shell_devtools_bindings :
623 ShellDevToolsBindings::GetInstancesForWebContents(old_contents)) {
624 shell_devtools_bindings->UpdateInspectedWebContents(
625 new_contents,
626 base::BindOnce(base::DoNothing::Once<scoped_refptr<PendingCallback>>(),
627 pending_callback));
628 }
629}
630
Lukasz Anforowicz82a5ca92019-10-24 18:45:37631bool Shell::ShouldAllowRunningInsecureContent(WebContents* web_contents,
632 bool allowed_per_prefs,
633 const url::Origin& origin,
634 const GURL& resource_url) {
danakj8de3b7492020-07-02 22:41:42635 if (allowed_per_prefs)
636 return true;
carloskd9d97942017-02-16 08:58:09637
danakj8de3b7492020-07-02 22:41:42638 return g_platform->ShouldAllowRunningInsecureContent(this);
carloskd9d97942017-02-16 08:58:09639}
640
Becca Hughes112832e2019-06-11 17:19:02641PictureInPictureResult Shell::EnterPictureInPicture(
Lukasz Anforowicz82a5ca92019-10-24 18:45:37642 WebContents* web_contents,
Becca Hughes112832e2019-06-11 17:19:02643 const viz::SurfaceId& surface_id,
644 const gfx::Size& natural_size) {
645 // During tests, returning success to pretend the window was created and allow
646 // tests to run accordingly.
647 if (!switches::IsRunWebTestsSwitchPresent())
648 return PictureInPictureResult::kNotSupported;
Becca Hughes112832e2019-06-11 17:19:02649 return PictureInPictureResult::kSuccess;
Mounir Lamouri11e9ef432018-05-22 03:10:16650}
651
Bo Liu300c6052018-06-12 04:46:07652bool Shell::ShouldResumeRequestsForCreatedWindow() {
653 return !delay_popup_contents_delegate_for_testing_;
654}
655
danakjee2390a82020-06-10 16:53:37656void Shell::SetContentsBounds(WebContents* source, const gfx::Rect& bounds) {
657 DCHECK(source == web_contents()); // There's only one WebContents per Shell.
658
659 if (switches::IsRunWebTestsSwitchPresent()) {
660 // Note that chrome drops these requests on normal windows.
661 // TODO(danakj): The position is dropped here but we use the size. Web tests
662 // can't move the window in headless mode anyways, but maybe we should be
663 // letting them pretend?
664 g_platform->ResizeWebContent(this, bounds.size());
665 }
666}
667
pdrcab84ee2015-03-13 21:47:04668gfx::Size Shell::GetShellDefaultSize() {
danakj6c16fe92020-09-18 23:51:32669 static gfx::Size default_shell_size; // Only go through this method once.
670
pdrcab84ee2015-03-13 21:47:04671 if (!default_shell_size.IsEmpty())
672 return default_shell_size;
danakj6c16fe92020-09-18 23:51:32673
pdrcab84ee2015-03-13 21:47:04674 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;
danakj6c16fe92020-09-18 23:51:32679 if (sscanf(size_str.c_str(), "%dx%d", &width, &height) == 2) {
680 default_shell_size = gfx::Size(width, height);
681 } else {
682 LOG(ERROR) << "Invalid size \"" << size_str << "\" given to --"
683 << switches::kContentShellHostWindowSize;
684 }
685 }
686
687 if (default_shell_size.IsEmpty()) {
pdrcab84ee2015-03-13 21:47:04688 default_shell_size = gfx::Size(
689 kDefaultTestWindowWidthDip, kDefaultTestWindowHeightDip);
690 }
danakj6c16fe92020-09-18 23:51:32691
pdrcab84ee2015-03-13 21:47:04692 return default_shell_size;
693}
694
danakjde3e2a02020-05-12 16:51:20695#if defined(OS_ANDROID)
696void Shell::LoadProgressChanged(double progress) {
697 g_platform->LoadProgressChanged(this, progress);
698}
699#endif
700
Avi Drissman93002212017-09-27 03:20:52701void Shell::TitleWasSet(NavigationEntry* entry) {
[email protected]1ef02d242013-10-07 16:18:53702 if (entry)
danakjde3e2a02020-05-12 16:51:20703 g_platform->SetTitle(this, entry->GetTitle());
[email protected]aecc085b2012-06-01 18:15:53704}
705
[email protected]7fff43e2013-05-21 20:21:10706void Shell::OnDevToolsWebContentsDestroyed() {
707 devtools_observer_.reset();
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28708 devtools_frontend_ = nullptr;
[email protected]7fff43e2013-05-21 20:21:10709}
710
[email protected]9fbd3f862011-09-20 23:31:34711} // namespace content