blob: e236a5a55d136e66a1d7bc551bd633441a7e8032 [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
[email protected]efb5f572012-01-29 10:57:339#include "base/command_line.h"
skyostil95082a62015-06-05 19:53:0710#include "base/location.h"
avi66a07722015-12-25 23:38:1211#include "base/macros.h"
Wez9a58a152018-06-07 18:59:3312#include "base/no_destructor.h"
fdoray896bea12016-06-10 15:52:0113#include "base/run_loop.h"
skyostil95082a62015-06-05 19:53:0714#include "base/single_thread_task_runner.h"
[email protected]21aa99682013-06-11 07:17:0115#include "base/strings/string_number_conversions.h"
16#include "base/strings/string_util.h"
17#include "base/strings/stringprintf.h"
[email protected]74ebfb12013-06-07 20:48:0018#include "base/strings/utf_string_conversions.h"
gab30f26df2016-05-11 19:37:5519#include "base/threading/thread_task_runner_handle.h"
avi66a07722015-12-25 23:38:1220#include "build/build_config.h"
[email protected]b50452f2014-08-18 12:31:4421#include "content/public/browser/devtools_agent_host.h"
[email protected]0b659b32012-03-26 21:29:3222#include "content/public/browser/navigation_controller.h"
[email protected]b7c504c2013-05-07 14:42:1223#include "content/public/browser/navigation_entry.h"
Yutaka Hirano2109e582018-02-14 07:24:4624#include "content/public/browser/render_process_host.h"
[email protected]0b659b32012-03-26 21:29:3225#include "content/public/browser/render_view_host.h"
avif9ab5d942015-10-15 14:05:4426#include "content/public/browser/render_widget_host.h"
[email protected]0b659b32012-03-26 21:29:3227#include "content/public/browser/web_contents.h"
guoweis8efb6d892015-10-12 18:26:1728#include "content/public/common/content_switches.h"
[email protected]1596efb2013-01-17 22:13:0129#include "content/public/common/renderer_preferences.h"
guoweisc537ba22015-11-06 21:20:3130#include "content/public/common/webrtc_ip_handling_policy.h"
jochen5ce1af92016-02-17 20:39:4531#include "content/shell/browser/layout_test/blink_test_controller.h"
jyasskin8057c002015-09-10 19:09:5332#include "content/shell/browser/layout_test/layout_test_bluetooth_chooser_factory.h"
chenwilliamfed5bda2017-03-24 02:01:0633#include "content/shell/browser/layout_test/layout_test_devtools_bindings.h"
mkwst6efe7f2e62014-10-08 15:09:0134#include "content/shell/browser/layout_test/layout_test_javascript_dialog_manager.h"
lukasza8e550a02016-04-18 18:34:1735#include "content/shell/browser/layout_test/secondary_test_window_observer.h"
[email protected]de7d61ff2013-08-20 11:30:4136#include "content/shell/browser/shell_browser_main_parts.h"
37#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"
carloskd9d97942017-02-16 08:58:0940#include "content/shell/common/layout_test/layout_test_switches.h"
[email protected]b7c504c2013-05-07 14:42:1241#include "content/shell/common/shell_messages.h"
42#include "content/shell/common/shell_switches.h"
Scott Violeta35f9a42018-03-22 22:00:4443#include "media/media_buildflags.h"
Blink Reformata30d4232018-04-07 15:31:0644#include "third_party/blink/public/web/web_presentation_receiver_flags.h"
[email protected]9fbd3f862011-09-20 23:31:3445
[email protected]9fbd3f862011-09-20 23:31:3446namespace content {
47
Wez9a58a152018-06-07 18:59:3348// Closure to run if set, and the last Shell instance is deleted.
49base::NoDestructor<base::OnceClosure> g_quit_closure;
50
pdrcab84ee2015-03-13 21:47:0451const int kDefaultTestWindowWidthDip = 800;
52const int kDefaultTestWindowHeightDip = 600;
[email protected]1e57cab2013-05-28 04:26:1153
[email protected]e99ca5112011-09-26 17:22:5454std::vector<Shell*> Shell::windows_;
[email protected]9e00e6352012-07-30 17:05:1855base::Callback<void(Shell*)> Shell::shell_created_callback_;
[email protected]9fbd3f862011-09-20 23:31:3456
[email protected]7fff43e2013-05-21 20:21:1057class Shell::DevToolsWebContentsObserver : public WebContentsObserver {
58 public:
59 DevToolsWebContentsObserver(Shell* shell, WebContents* web_contents)
60 : WebContentsObserver(web_contents),
61 shell_(shell) {
62 }
63
64 // WebContentsObserver
dchenge933b3e2014-10-21 11:44:0965 void WebContentsDestroyed() override {
[email protected]7fff43e2013-05-21 20:21:1066 shell_->OnDevToolsWebContentsDestroyed();
67 }
68
69 private:
70 Shell* shell_;
71
72 DISALLOW_COPY_AND_ASSIGN(DevToolsWebContentsObserver);
73};
74
Bo Liu300c6052018-06-12 04:46:0775Shell::Shell(std::unique_ptr<WebContents> web_contents,
76 bool should_set_delegate)
erikchenbee5c9622018-04-27 19:30:2577 : WebContentsObserver(web_contents.get()),
78 web_contents_(std::move(web_contents)),
Ivan Kotenkov2c0d2bb32017-11-01 15:41:2879 devtools_frontend_(nullptr),
[email protected]001841c92012-12-11 17:00:1380 is_fullscreen_(false),
Ivan Kotenkov2c0d2bb32017-11-01 15:41:2881 window_(nullptr),
sadrul8b11d262015-11-12 18:41:3382#if defined(OS_MACOSX)
[email protected]86b36e672012-12-21 00:09:5183 url_edit_view_(NULL),
sadrul8b11d262015-11-12 18:41:3384#endif
Pavel Feldmanc7cd063c2017-10-06 20:04:2885 headless_(false),
86 hide_toolbar_(false) {
Bo Liu300c6052018-06-12 04:46:0787 if (should_set_delegate)
88 web_contents_->SetDelegate(this);
arthursonzognifdd49912017-08-31 08:55:2689
Kent Tamuracd3ebc42018-05-16 06:44:2290 if (switches::IsRunWebTestsSwitchPresent()) {
[email protected]86b36e672012-12-21 00:09:5191 headless_ = true;
Francois Doraye6161152018-03-27 22:05:3792 // In a headless shell, disable occlusion tracking. Otherwise, WebContents
93 // would always behave as if they were occluded, i.e. would not render
94 // frames and would not receive input events.
95 base::CommandLine::ForCurrentProcess()->AppendSwitch(
96 switches::kDisableBackgroundingOccludedWindowsForTesting);
97 }
Pavel Feldmanc7cd063c2017-10-06 20:04:2898
99 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
100 switches::kContentShellHideToolbar))
101 hide_toolbar_ = true;
102
[email protected]9e00e6352012-07-30 17:05:18103 windows_.push_back(this);
104
105 if (!shell_created_callback_.is_null()) {
106 shell_created_callback_.Run(this);
107 shell_created_callback_.Reset();
108 }
[email protected]9fbd3f862011-09-20 23:31:34109}
110
111Shell::~Shell() {
112 PlatformCleanUp();
[email protected]e99ca5112011-09-26 17:22:54113
114 for (size_t i = 0; i < windows_.size(); ++i) {
115 if (windows_[i] == this) {
116 windows_.erase(windows_.begin() + i);
117 break;
118 }
119 }
[email protected]11a65b692012-03-30 11:29:16120
Wez9a58a152018-06-07 18:59:33121 if (windows_.empty()) {
[email protected]b53adf452014-02-07 12:55:08122 if (headless_)
123 PlatformExit();
Yutaka Hirano2109e582018-02-14 07:24:46124 for (auto it = RenderProcessHost::AllHostsIterator(); !it.IsAtEnd();
125 it.Advance()) {
126 it.GetCurrentValue()->DisableKeepAliveRefCount();
127 }
Wez9a58a152018-06-07 18:59:33128 if (*g_quit_closure)
129 std::move(*g_quit_closure).Run();
[email protected]b53adf452014-02-07 12:55:08130 }
arthursonzognifdd49912017-08-31 08:55:26131
132 web_contents_->SetDelegate(nullptr);
[email protected]9fbd3f862011-09-20 23:31:34133}
134
erikchenbee5c9622018-04-27 19:30:25135Shell* Shell::CreateShell(std::unique_ptr<WebContents> web_contents,
Bo Liu300c6052018-06-12 04:46:07136 const gfx::Size& initial_size,
137 bool should_set_delegate) {
erikchenbee5c9622018-04-27 19:30:25138 WebContents* raw_web_contents = web_contents.get();
Bo Liu300c6052018-06-12 04:46:07139 Shell* shell = new Shell(std::move(web_contents), should_set_delegate);
[email protected]c0d036d2013-05-07 12:50:50140 shell->PlatformCreateWindow(initial_size.width(), initial_size.height());
[email protected]3fd84032012-01-12 18:20:17141
[email protected]5cba3bf2012-01-14 02:40:35142 shell->PlatformSetContents();
[email protected]3fd84032012-01-12 18:20:17143
144 shell->PlatformResizeSubViews();
[email protected]1596efb2013-01-17 22:13:01145
creisb6561df2016-02-11 20:20:54146 // Note: Do not make RenderFrameHost or RenderViewHost specific state changes
147 // here, because they will be forgotten after a cross-process navigation. Use
148 // RenderFrameCreated or RenderViewCreated instead.
Kent Tamuracd3ebc42018-05-16 06:44:22149 if (switches::IsRunWebTestsSwitchPresent()) {
erikchenbee5c9622018-04-27 19:30:25150 raw_web_contents->GetMutableRendererPrefs()->use_custom_colors = false;
151 raw_web_contents->GetRenderViewHost()->SyncRendererPrefs();
[email protected]1596efb2013-01-17 22:13:01152 }
153
lukasza381b0492016-03-10 16:48:43154 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
guoweis4ee48592015-12-02 06:37:07155 if (command_line->HasSwitch(switches::kForceWebRtcIPHandlingPolicy)) {
erikchenbee5c9622018-04-27 19:30:25156 raw_web_contents->GetMutableRendererPrefs()->webrtc_ip_handling_policy =
guoweis4ee48592015-12-02 06:37:07157 command_line->GetSwitchValueASCII(
158 switches::kForceWebRtcIPHandlingPolicy);
guoweis8efb6d892015-10-12 18:26:17159 }
guoweis8efb6d892015-10-12 18:26:17160
[email protected]3fd84032012-01-12 18:20:17161 return shell;
162}
163
[email protected]11a65b692012-03-30 11:29:16164void Shell::CloseAllWindows() {
[email protected]b50452f2014-08-18 12:31:44165 DevToolsAgentHost::DetachAllClients();
[email protected]11a65b692012-03-30 11:29:16166 std::vector<Shell*> open_windows(windows_);
Wez9a58a152018-06-07 18:59:33167 if (!open_windows.empty()) {
168 base::RunLoop run_loop;
169 *g_quit_closure = run_loop.QuitWhenIdleClosure();
170 for (size_t i = 0; i < open_windows.size(); ++i)
171 open_windows[i]->Close();
172 // Run the message loop until all Shell windows are closed.
173 run_loop.Run();
174 }
dmazzoni948fbc72015-05-17 07:57:33175 PlatformExit();
[email protected]11a65b692012-03-30 11:29:16176}
177
[email protected]9e00e6352012-07-30 17:05:18178void Shell::SetShellCreatedCallback(
179 base::Callback<void(Shell*)> shell_created_callback) {
180 DCHECK(shell_created_callback_.is_null());
Tommy Nyquist4b749d02018-03-20 21:46:29181 shell_created_callback_ = std::move(shell_created_callback);
[email protected]9e00e6352012-07-30 17:05:18182}
183
[email protected]74830f02012-01-30 22:27:04184Shell* Shell::FromRenderViewHost(RenderViewHost* rvh) {
185 for (size_t i = 0; i < windows_.size(); ++i) {
[email protected]0b659b32012-03-26 21:29:32186 if (windows_[i]->web_contents() &&
187 windows_[i]->web_contents()->GetRenderViewHost() == rvh) {
[email protected]74830f02012-01-30 22:27:04188 return windows_[i];
189 }
190 }
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28191 return nullptr;
[email protected]74830f02012-01-30 22:27:04192}
193
[email protected]6153b272013-01-25 22:29:23194// static
195void Shell::Initialize() {
pdrcab84ee2015-03-13 21:47:04196 PlatformInitialize(GetShellDefaultSize());
[email protected]6153b272013-01-25 22:29:23197}
198
[email protected]a2904092013-10-15 04:53:59199gfx::Size Shell::AdjustWindowSize(const gfx::Size& initial_size) {
200 if (!initial_size.IsEmpty())
201 return initial_size;
pdrcab84ee2015-03-13 21:47:04202 return GetShellDefaultSize();
[email protected]a2904092013-10-15 04:53:59203}
204
[email protected]bdcf9152012-07-19 17:43:21205Shell* Shell::CreateNewWindow(BrowserContext* browser_context,
[email protected]e99ca5112011-09-26 17:22:54206 const GURL& url,
lukasza04130152016-10-21 20:26:32207 const scoped_refptr<SiteInstance>& site_instance,
[email protected]cdb806722013-01-10 14:18:23208 const gfx::Size& initial_size) {
[email protected]54944cde2012-12-09 09:24:59209 WebContents::CreateParams create_params(browser_context, site_instance);
mark a. foltzef394fce2017-10-21 09:11:02210 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
211 switches::kForcePresentationReceiverForTesting)) {
212 create_params.starting_sandbox_flags =
213 blink::kPresentationReceiverSandboxFlags;
214 }
[email protected]a2904092013-10-15 04:53:59215 create_params.initial_size = AdjustWindowSize(initial_size);
erikchenbee5c9622018-04-27 19:30:25216 std::unique_ptr<WebContents> web_contents =
Erik Chenbb8e738e2018-04-28 14:10:43217 WebContents::Create(create_params);
erikchenbee5c9622018-04-27 19:30:25218 Shell* shell =
Bo Liu300c6052018-06-12 04:46:07219 CreateShell(std::move(web_contents), create_params.initial_size,
220 true /* should_set_delegate */);
[email protected]e99ca5112011-09-26 17:22:54221 if (!url.is_empty())
222 shell->LoadURL(url);
[email protected]9fbd3f862011-09-20 23:31:34223 return shell;
224}
225
Daniel Murphy16ed0782018-05-26 00:15:13226Shell* Shell::CreateNewWindowWithSessionStorageNamespace(
227 BrowserContext* browser_context,
228 const GURL& url,
229 const scoped_refptr<SiteInstance>& site_instance,
230 const gfx::Size& initial_size,
231 scoped_refptr<SessionStorageNamespace> session_storage_namespace) {
232 WebContents::CreateParams create_params(browser_context, site_instance);
233 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
234 switches::kForcePresentationReceiverForTesting)) {
235 create_params.starting_sandbox_flags =
236 blink::kPresentationReceiverSandboxFlags;
237 }
238 create_params.initial_size = AdjustWindowSize(initial_size);
239 std::map<std::string, scoped_refptr<SessionStorageNamespace>>
240 session_storages;
241 session_storages[""] = session_storage_namespace;
242 std::unique_ptr<WebContents> web_contents =
243 WebContents::CreateWithSessionStorage(create_params, session_storages);
244 Shell* shell =
Bo Liu300c6052018-06-12 04:46:07245 CreateShell(std::move(web_contents), create_params.initial_size,
246 true /* should_set_delegate */);
Daniel Murphy16ed0782018-05-26 00:15:13247 if (!url.is_empty())
248 shell->LoadURL(url);
249 return shell;
250}
251
[email protected]9fbd3f862011-09-20 23:31:34252void Shell::LoadURL(const GURL& url) {
Alex Moshchuk7e26eca2018-03-03 01:34:29253 LoadURLForFrame(
254 url, std::string(),
255 ui::PageTransitionFromInt(ui::PAGE_TRANSITION_TYPED |
256 ui::PAGE_TRANSITION_FROM_ADDRESS_BAR));
[email protected]d2494ff2013-02-20 08:22:37257}
258
Alex Moshchuk7e26eca2018-03-03 01:34:29259void Shell::LoadURLForFrame(const GURL& url,
260 const std::string& frame_name,
261 ui::PageTransition transition_type) {
[email protected]d2494ff2013-02-20 08:22:37262 NavigationController::LoadURLParams params(url);
[email protected]d2494ff2013-02-20 08:22:37263 params.frame_name = frame_name;
Alex Moshchuk7e26eca2018-03-03 01:34:29264 params.transition_type = transition_type;
[email protected]d2494ff2013-02-20 08:22:37265 web_contents_->GetController().LoadURLWithParams(params);
[email protected]fc2b46b2014-05-03 16:33:45266 web_contents_->Focus();
[email protected]9fbd3f862011-09-20 23:31:34267}
268
[email protected]76bdecb2014-04-16 17:58:08269void Shell::LoadDataWithBaseURL(const GURL& url, const std::string& data,
270 const GURL& base_url) {
boliuec93ea92016-02-17 22:23:07271 bool load_as_string = false;
272 LoadDataWithBaseURLInternal(url, data, base_url, load_as_string);
273}
274
275#if defined(OS_ANDROID)
276void Shell::LoadDataAsStringWithBaseURL(const GURL& url,
277 const std::string& data,
278 const GURL& base_url) {
279 bool load_as_string = true;
280 LoadDataWithBaseURLInternal(url, data, base_url, load_as_string);
281}
282#endif
283
284void Shell::LoadDataWithBaseURLInternal(const GURL& url,
285 const std::string& data,
286 const GURL& base_url,
287 bool load_as_string) {
288#if !defined(OS_ANDROID)
289 DCHECK(!load_as_string); // Only supported on Android.
290#endif
291
292 NavigationController::LoadURLParams params(GURL::EmptyGURL());
293 const std::string data_url_header = "data:text/html;charset=utf-8,";
294 if (load_as_string) {
295 params.url = GURL(data_url_header);
296 std::string data_url_as_string = data_url_header + data;
297#if defined(OS_ANDROID)
298 params.data_url_as_string =
299 base::RefCountedString::TakeString(&data_url_as_string);
300#endif
301 } else {
302 params.url = GURL(data_url_header + data);
303 }
304
[email protected]76bdecb2014-04-16 17:58:08305 params.load_type = NavigationController::LOAD_TYPE_DATA;
306 params.base_url_for_data_url = base_url;
307 params.virtual_url_for_data_url = url;
308 params.override_user_agent = NavigationController::UA_OVERRIDE_FALSE;
309 web_contents_->GetController().LoadURLWithParams(params);
[email protected]fc2b46b2014-05-03 16:33:45310 web_contents_->Focus();
[email protected]76bdecb2014-04-16 17:58:08311}
312
[email protected]a2904092013-10-15 04:53:59313void Shell::AddNewContents(WebContents* source,
erikchenbee5c9622018-04-27 19:30:25314 std::unique_ptr<WebContents> new_contents,
[email protected]a2904092013-10-15 04:53:59315 WindowOpenDisposition disposition,
bokan107a47f2015-02-03 23:23:39316 const gfx::Rect& initial_rect,
[email protected]a2904092013-10-15 04:53:59317 bool user_gesture,
318 bool* was_blocked) {
erikchenbee5c9622018-04-27 19:30:25319 WebContents* raw_new_contents = new_contents.get();
Bo Liu300c6052018-06-12 04:46:07320 CreateShell(
321 std::move(new_contents), AdjustWindowSize(initial_rect.size()),
322 !delay_popup_contents_delegate_for_testing_ /* should_set_delegate */);
Kent Tamuracd3ebc42018-05-16 06:44:22323 if (switches::IsRunWebTestsSwitchPresent())
erikchenbee5c9622018-04-27 19:30:25324 SecondaryTestWindowObserver::CreateForWebContents(raw_new_contents);
[email protected]a2904092013-10-15 04:53:59325}
326
[email protected]9fbd3f862011-09-20 23:31:34327void Shell::GoBackOrForward(int offset) {
[email protected]0b659b32012-03-26 21:29:32328 web_contents_->GetController().GoToOffset(offset);
[email protected]fc2b46b2014-05-03 16:33:45329 web_contents_->Focus();
[email protected]9fbd3f862011-09-20 23:31:34330}
331
332void Shell::Reload() {
toyoshim6142d96f2016-12-19 09:07:25333 web_contents_->GetController().Reload(ReloadType::NORMAL, false);
[email protected]fc2b46b2014-05-03 16:33:45334 web_contents_->Focus();
[email protected]9fbd3f862011-09-20 23:31:34335}
336
toyoshime5aaf6a2016-05-18 08:07:48337void Shell::ReloadBypassingCache() {
toyoshim6142d96f2016-12-19 09:07:25338 web_contents_->GetController().Reload(ReloadType::BYPASSING_CACHE, false);
toyoshime5aaf6a2016-05-18 08:07:48339 web_contents_->Focus();
340}
341
[email protected]9fbd3f862011-09-20 23:31:34342void Shell::Stop() {
[email protected]0b659b32012-03-26 21:29:32343 web_contents_->Stop();
[email protected]fc2b46b2014-05-03 16:33:45344 web_contents_->Focus();
[email protected]9fbd3f862011-09-20 23:31:34345}
346
[email protected]e3b10d12014-03-28 16:06:09347void Shell::UpdateNavigationControls(bool to_different_document) {
[email protected]0b659b32012-03-26 21:29:32348 int current_index = web_contents_->GetController().GetCurrentEntryIndex();
349 int max_index = web_contents_->GetController().GetEntryCount() - 1;
[email protected]9fbd3f862011-09-20 23:31:34350
351 PlatformEnableUIControl(BACK_BUTTON, current_index > 0);
352 PlatformEnableUIControl(FORWARD_BUTTON, current_index < max_index);
[email protected]e3b10d12014-03-28 16:06:09353 PlatformEnableUIControl(STOP_BUTTON,
354 to_different_document && web_contents_->IsLoading());
[email protected]9fbd3f862011-09-20 23:31:34355}
356
[email protected]7c17b6992012-08-09 16:16:30357void Shell::ShowDevTools() {
mohsen6eb57fb2016-07-22 03:14:08358 if (!devtools_frontend_) {
359 devtools_frontend_ = ShellDevToolsFrontend::Show(web_contents());
360 devtools_observer_.reset(new DevToolsWebContentsObserver(
361 this, devtools_frontend_->frontend_shell()->web_contents()));
362 }
[email protected]3142e5d2014-02-07 00:54:46363
mohsen6eb57fb2016-07-22 03:14:08364 devtools_frontend_->Activate();
365 devtools_frontend_->Focus();
[email protected]7c17b6992012-08-09 16:16:30366}
367
[email protected]001841c92012-12-11 17:00:13368void Shell::CloseDevTools() {
[email protected]0773e0c2013-01-25 15:57:57369 if (!devtools_frontend_)
[email protected]001841c92012-12-11 17:00:13370 return;
[email protected]7fff43e2013-05-21 20:21:10371 devtools_observer_.reset();
[email protected]0773e0c2013-01-25 15:57:57372 devtools_frontend_->Close();
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28373 devtools_frontend_ = nullptr;
[email protected]001841c92012-12-11 17:00:13374}
375
[email protected]9fbd3f862011-09-20 23:31:34376gfx::NativeView Shell::GetContentView() {
[email protected]59383c782013-04-17 16:43:27377 if (!web_contents_)
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28378 return nullptr;
[email protected]fc2b46b2014-05-03 16:33:45379 return web_contents_->GetNativeView();
[email protected]9fbd3f862011-09-20 23:31:34380}
381
alexmos5a98a052016-01-06 00:15:02382WebContents* Shell::OpenURLFromTab(WebContents* source,
383 const OpenURLParams& params) {
lukasza04130152016-10-21 20:26:32384 WebContents* target = nullptr;
385 switch (params.disposition) {
386 case WindowOpenDisposition::CURRENT_TAB:
387 target = source;
388 break;
389
390 // Normally, the difference between NEW_POPUP and NEW_WINDOW is that a popup
391 // should have no toolbar, no status bar, no menu bar, no scrollbars and be
392 // not resizable. For simplicity and to enable new testing scenarios in
393 // content shell and layout tests, popups don't get special treatment below
394 // (i.e. they will have a toolbar and other things described here).
395 case WindowOpenDisposition::NEW_POPUP:
lukaszabe2f0da2017-04-25 00:43:00396 case WindowOpenDisposition::NEW_WINDOW:
397 // content_shell doesn't really support tabs, but some layout tests use
398 // middle click (which translates into kNavigationPolicyNewBackgroundTab),
399 // so we treat the cases below just like a NEW_WINDOW disposition.
400 case WindowOpenDisposition::NEW_BACKGROUND_TAB:
401 case WindowOpenDisposition::NEW_FOREGROUND_TAB: {
lukasza04130152016-10-21 20:26:32402 Shell* new_window =
403 Shell::CreateNewWindow(source->GetBrowserContext(),
404 GURL(), // Don't load anything just yet.
405 params.source_site_instance,
406 gfx::Size()); // Use default size.
407 target = new_window->web_contents();
Kent Tamuracd3ebc42018-05-16 06:44:22408 if (switches::IsRunWebTestsSwitchPresent())
lukasza04130152016-10-21 20:26:32409 SecondaryTestWindowObserver::CreateForWebContents(target);
410 break;
411 }
412
413 // No tabs in content_shell:
414 case WindowOpenDisposition::SINGLETON_TAB:
lukasza04130152016-10-21 20:26:32415 // No incognito mode in content_shell:
416 case WindowOpenDisposition::OFF_THE_RECORD:
417 // TODO(lukasza): Investigate if some layout tests might need support for
418 // SAVE_TO_DISK disposition. This would probably require that
419 // BlinkTestController always sets up and cleans up a temporary directory
420 // as the default downloads destinations for the duration of a test.
421 case WindowOpenDisposition::SAVE_TO_DISK:
422 // Ignoring requests with disposition == IGNORE_ACTION...
423 case WindowOpenDisposition::IGNORE_ACTION:
424 default:
425 return nullptr;
426 }
alexmos5a98a052016-01-06 00:15:02427
428 NavigationController::LoadURLParams load_url_params(params.url);
429 load_url_params.source_site_instance = params.source_site_instance;
430 load_url_params.transition_type = params.transition;
431 load_url_params.frame_tree_node_id = params.frame_tree_node_id;
432 load_url_params.referrer = params.referrer;
433 load_url_params.redirect_chain = params.redirect_chain;
434 load_url_params.extra_headers = params.extra_headers;
435 load_url_params.is_renderer_initiated = params.is_renderer_initiated;
alexmos5a98a052016-01-06 00:15:02436 load_url_params.should_replace_current_entry =
437 params.should_replace_current_entry;
Marijn Kruisselbrink7a0d5e182018-05-24 22:55:09438 load_url_params.blob_url_loader_factory = params.blob_url_loader_factory;
alexmos5a98a052016-01-06 00:15:02439
lukasza477a5a22016-06-16 18:28:43440 if (params.uses_post) {
441 load_url_params.load_type = NavigationController::LOAD_TYPE_HTTP_POST;
442 load_url_params.post_data = params.post_data;
alexmos5a98a052016-01-06 00:15:02443 }
444
lukasza04130152016-10-21 20:26:32445 target->GetController().LoadURLWithParams(load_url_params);
446 return target;
alexmos5a98a052016-01-06 00:15:02447}
448
[email protected]e3b10d12014-03-28 16:06:09449void Shell::LoadingStateChanged(WebContents* source,
450 bool to_different_document) {
451 UpdateNavigationControls(to_different_document);
[email protected]15aea412012-01-19 03:18:50452 PlatformSetIsLoading(source->IsLoading());
[email protected]e99ca5112011-09-26 17:22:54453}
454
Dave Tapuska3ed44192018-05-01 18:53:30455void Shell::EnterFullscreenModeForTab(
456 WebContents* web_contents,
457 const GURL& origin,
458 const blink::WebFullscreenOptions& options) {
mlamouri7a78d6fd2015-01-17 13:23:53459 ToggleFullscreenModeForTab(web_contents, true);
460}
461
462void Shell::ExitFullscreenModeForTab(WebContents* web_contents) {
463 ToggleFullscreenModeForTab(web_contents, false);
464}
465
[email protected]99c014c2012-11-27 12:03:42466void Shell::ToggleFullscreenModeForTab(WebContents* web_contents,
467 bool enter_fullscreen) {
468#if defined(OS_ANDROID)
469 PlatformToggleFullscreenModeForTab(web_contents, enter_fullscreen);
470#endif
Kent Tamuracd3ebc42018-05-16 06:44:22471 if (!switches::IsRunWebTestsSwitchPresent())
[email protected]99c014c2012-11-27 12:03:42472 return;
473 if (is_fullscreen_ != enter_fullscreen) {
474 is_fullscreen_ = enter_fullscreen;
Fady Samuel0b911822018-04-25 13:22:16475 web_contents->GetRenderViewHost()
476 ->GetWidget()
477 ->SynchronizeVisualProperties();
[email protected]99c014c2012-11-27 12:03:42478 }
479}
480
481bool Shell::IsFullscreenForTabOrPending(const WebContents* web_contents) const {
482#if defined(OS_ANDROID)
483 return PlatformIsFullscreenForTabOrPending(web_contents);
484#else
485 return is_fullscreen_;
486#endif
487}
488
mikhail.pozdnyakovc0e251b2015-04-15 06:51:12489blink::WebDisplayMode Shell::GetDisplayMode(
490 const WebContents* web_contents) const {
491 // TODO : should return blink::WebDisplayModeFullscreen wherever user puts
492 // a browser window into fullscreen (not only in case of renderer-initiated
493 // fullscreen mode): crbug.com/476874.
Blink Reformat1c4d759e2017-04-09 16:34:54494 return IsFullscreenForTabOrPending(web_contents)
495 ? blink::kWebDisplayModeFullscreen
496 : blink::kWebDisplayModeBrowser;
mikhail.pozdnyakovc0e251b2015-04-15 06:51:12497}
498
[email protected]f78439002012-11-28 14:45:59499void Shell::RequestToLockMouse(WebContents* web_contents,
500 bool user_gesture,
501 bool last_unlocked_by_target) {
502 web_contents->GotResponseToLockMouseRequest(true);
503}
504
[email protected]9e00e6352012-07-30 17:05:18505void Shell::CloseContents(WebContents* source) {
506 Close();
507}
508
[email protected]067310262012-11-22 14:30:41509bool Shell::CanOverscrollContent() const {
510#if defined(USE_AURA)
511 return true;
512#else
513 return false;
514#endif
515}
516
[email protected]3bbacc5b2012-04-17 17:46:15517void Shell::DidNavigateMainFramePostCommit(WebContents* web_contents) {
[email protected]4e3c7522013-08-13 11:53:18518 PlatformSetAddressBarURL(web_contents->GetLastCommittedURL());
[email protected]e99ca5112011-09-26 17:22:54519}
520
mathiash72a5e462014-11-19 08:18:50521JavaScriptDialogManager* Shell::GetJavaScriptDialogManager(
522 WebContents* source) {
mkwst6efe7f2e62014-10-08 15:09:01523 if (!dialog_manager_) {
Kent Tamuracd3ebc42018-05-16 06:44:22524 dialog_manager_.reset(switches::IsRunWebTestsSwitchPresent()
lukasza381b0492016-03-10 16:48:43525 ? new LayoutTestJavaScriptDialogManager
526 : new ShellJavaScriptDialogManager);
mkwst6efe7f2e62014-10-08 15:09:01527 }
[email protected]71a88bb2013-02-01 22:05:15528 return dialog_manager_.get();
[email protected]f2210022012-03-29 00:36:08529}
530
dcheng6003e0b2016-04-09 18:42:34531std::unique_ptr<BluetoothChooser> Shell::RunBluetoothChooser(
jyasskin93649a52016-02-17 19:57:02532 RenderFrameHost* frame,
533 const BluetoothChooser::EventHandler& event_handler) {
Lukasz Anforowiczf227b4912017-12-22 23:08:59534 BlinkTestController* blink_test_controller = BlinkTestController::Get();
Kent Tamuracd3ebc42018-05-16 06:44:22535 if (blink_test_controller && switches::IsRunWebTestsSwitchPresent())
Lukasz Anforowiczf227b4912017-12-22 23:08:59536 return blink_test_controller->RunBluetoothChooser(frame, event_handler);
jyasskin8057c002015-09-10 19:09:53537 return nullptr;
538}
539
avia90ae4e2016-11-11 20:49:33540bool Shell::DidAddMessageToConsole(WebContents* source,
541 int32_t level,
542 const base::string16& message,
543 int32_t line_no,
544 const base::string16& source_id) {
Kent Tamuracd3ebc42018-05-16 06:44:22545 return switches::IsRunWebTestsSwitchPresent();
[email protected]efb5f572012-01-29 10:57:33546}
547
Avi Drissman8920def2018-01-31 19:53:36548void Shell::RendererUnresponsive(WebContents* source,
Avi Drissman1572e8c3c2018-02-02 19:06:36549 RenderWidgetHost* render_widget_host) {
Lukasz Anforowiczf227b4912017-12-22 23:08:59550 BlinkTestController* blink_test_controller = BlinkTestController::Get();
Kent Tamuracd3ebc42018-05-16 06:44:22551 if (blink_test_controller && switches::IsRunWebTestsSwitchPresent())
Lukasz Anforowiczf227b4912017-12-22 23:08:59552 blink_test_controller->RendererUnresponsive();
[email protected]5bf68f22012-08-31 07:38:10553}
554
[email protected]233567d2013-02-27 20:22:02555void Shell::ActivateContents(WebContents* contents) {
avif9ab5d942015-10-15 14:05:44556 contents->GetRenderViewHost()->GetWidget()->Focus();
[email protected]233567d2013-02-27 20:22:02557}
558
carloskd9d97942017-02-16 08:58:09559bool Shell::ShouldAllowRunningInsecureContent(
560 content::WebContents* web_contents,
561 bool allowed_per_prefs,
562 const url::Origin& origin,
563 const GURL& resource_url) {
564 bool allowed_by_test = false;
Lukasz Anforowiczf227b4912017-12-22 23:08:59565 BlinkTestController* blink_test_controller = BlinkTestController::Get();
Kent Tamuracd3ebc42018-05-16 06:44:22566 if (blink_test_controller && switches::IsRunWebTestsSwitchPresent()) {
carloskd9d97942017-02-16 08:58:09567 const base::DictionaryValue& test_flags =
Lukasz Anforowiczf227b4912017-12-22 23:08:59568 blink_test_controller->accumulated_layout_test_runtime_flags_changes();
carloskd9d97942017-02-16 08:58:09569 test_flags.GetBoolean("running_insecure_content_allowed", &allowed_by_test);
570 }
571
572 return allowed_per_prefs || allowed_by_test;
573}
574
Mounir Lamouri11e9ef432018-05-22 03:10:16575gfx::Size Shell::EnterPictureInPicture(const viz::SurfaceId& surface_id,
576 const gfx::Size& natural_size) {
577 // During tests, returning a fake window size to pretent the window was
578 // created and allow tests to run accordingly.
579 return switches::IsRunWebTestsSwitchPresent() ? gfx::Size(42, 42)
580 : gfx::Size(0, 0);
581}
582
Bo Liu300c6052018-06-12 04:46:07583bool Shell::ShouldResumeRequestsForCreatedWindow() {
584 return !delay_popup_contents_delegate_for_testing_;
585}
586
pdrcab84ee2015-03-13 21:47:04587gfx::Size Shell::GetShellDefaultSize() {
588 static gfx::Size default_shell_size;
589 if (!default_shell_size.IsEmpty())
590 return default_shell_size;
591 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
592 if (command_line->HasSwitch(switches::kContentShellHostWindowSize)) {
593 const std::string size_str = command_line->GetSwitchValueASCII(
594 switches::kContentShellHostWindowSize);
595 int width, height;
596 CHECK_EQ(2, sscanf(size_str.c_str(), "%dx%d", &width, &height));
597 default_shell_size = gfx::Size(width, height);
598 } else {
599 default_shell_size = gfx::Size(
600 kDefaultTestWindowWidthDip, kDefaultTestWindowHeightDip);
601 }
602 return default_shell_size;
603}
604
Avi Drissman93002212017-09-27 03:20:52605void Shell::TitleWasSet(NavigationEntry* entry) {
[email protected]1ef02d242013-10-07 16:18:53606 if (entry)
607 PlatformSetTitle(entry->GetTitle());
[email protected]aecc085b2012-06-01 18:15:53608}
609
[email protected]7fff43e2013-05-21 20:21:10610void Shell::OnDevToolsWebContentsDestroyed() {
611 devtools_observer_.reset();
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28612 devtools_frontend_ = nullptr;
[email protected]7fff43e2013-05-21 20:21:10613}
614
[email protected]9fbd3f862011-09-20 23:31:34615} // namespace content