blob: 8abb0ee9c44d0950ddaec5c3dd43777519a6f5fe [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
Wezcbf4a042018-06-13 16:29:1248// Null until/unless the default main message loop is running.
49base::NoDestructor<base::OnceClosure> g_quit_main_message_loop;
Wez9a58a152018-06-07 18:59:3350
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 }
Wezcbf4a042018-06-13 16:29:12128 if (*g_quit_main_message_loop)
129 std::move(*g_quit_main_message_loop).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_);
Wezcbf4a042018-06-13 16:29:12167 for (size_t i = 0; i < open_windows.size(); ++i)
168 open_windows[i]->Close();
169
170 // Pump the message loop to allow window teardown tasks to run.
171 base::RunLoop().RunUntilIdle();
172
dmazzoni948fbc72015-05-17 07:57:33173 PlatformExit();
[email protected]11a65b692012-03-30 11:29:16174}
175
Wezcbf4a042018-06-13 16:29:12176void Shell::SetMainMessageLoopQuitClosure(base::OnceClosure quit_closure) {
177 *g_quit_main_message_loop = std::move(quit_closure);
178}
179
[email protected]9e00e6352012-07-30 17:05:18180void Shell::SetShellCreatedCallback(
181 base::Callback<void(Shell*)> shell_created_callback) {
182 DCHECK(shell_created_callback_.is_null());
Tommy Nyquist4b749d02018-03-20 21:46:29183 shell_created_callback_ = std::move(shell_created_callback);
[email protected]9e00e6352012-07-30 17:05:18184}
185
[email protected]74830f02012-01-30 22:27:04186Shell* Shell::FromRenderViewHost(RenderViewHost* rvh) {
187 for (size_t i = 0; i < windows_.size(); ++i) {
[email protected]0b659b32012-03-26 21:29:32188 if (windows_[i]->web_contents() &&
189 windows_[i]->web_contents()->GetRenderViewHost() == rvh) {
[email protected]74830f02012-01-30 22:27:04190 return windows_[i];
191 }
192 }
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28193 return nullptr;
[email protected]74830f02012-01-30 22:27:04194}
195
[email protected]6153b272013-01-25 22:29:23196void Shell::Initialize() {
pdrcab84ee2015-03-13 21:47:04197 PlatformInitialize(GetShellDefaultSize());
[email protected]6153b272013-01-25 22:29:23198}
199
[email protected]a2904092013-10-15 04:53:59200gfx::Size Shell::AdjustWindowSize(const gfx::Size& initial_size) {
201 if (!initial_size.IsEmpty())
202 return initial_size;
pdrcab84ee2015-03-13 21:47:04203 return GetShellDefaultSize();
[email protected]a2904092013-10-15 04:53:59204}
205
[email protected]bdcf9152012-07-19 17:43:21206Shell* Shell::CreateNewWindow(BrowserContext* browser_context,
[email protected]e99ca5112011-09-26 17:22:54207 const GURL& url,
lukasza04130152016-10-21 20:26:32208 const scoped_refptr<SiteInstance>& site_instance,
[email protected]cdb806722013-01-10 14:18:23209 const gfx::Size& initial_size) {
[email protected]54944cde2012-12-09 09:24:59210 WebContents::CreateParams create_params(browser_context, site_instance);
mark a. foltzef394fce2017-10-21 09:11:02211 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
212 switches::kForcePresentationReceiverForTesting)) {
213 create_params.starting_sandbox_flags =
214 blink::kPresentationReceiverSandboxFlags;
215 }
[email protected]a2904092013-10-15 04:53:59216 create_params.initial_size = AdjustWindowSize(initial_size);
erikchenbee5c9622018-04-27 19:30:25217 std::unique_ptr<WebContents> web_contents =
Erik Chenbb8e738e2018-04-28 14:10:43218 WebContents::Create(create_params);
erikchenbee5c9622018-04-27 19:30:25219 Shell* shell =
Bo Liu300c6052018-06-12 04:46:07220 CreateShell(std::move(web_contents), create_params.initial_size,
221 true /* should_set_delegate */);
[email protected]e99ca5112011-09-26 17:22:54222 if (!url.is_empty())
223 shell->LoadURL(url);
[email protected]9fbd3f862011-09-20 23:31:34224 return shell;
225}
226
Daniel Murphy16ed0782018-05-26 00:15:13227Shell* Shell::CreateNewWindowWithSessionStorageNamespace(
228 BrowserContext* browser_context,
229 const GURL& url,
230 const scoped_refptr<SiteInstance>& site_instance,
231 const gfx::Size& initial_size,
232 scoped_refptr<SessionStorageNamespace> session_storage_namespace) {
233 WebContents::CreateParams create_params(browser_context, site_instance);
234 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
235 switches::kForcePresentationReceiverForTesting)) {
236 create_params.starting_sandbox_flags =
237 blink::kPresentationReceiverSandboxFlags;
238 }
239 create_params.initial_size = AdjustWindowSize(initial_size);
240 std::map<std::string, scoped_refptr<SessionStorageNamespace>>
241 session_storages;
242 session_storages[""] = session_storage_namespace;
243 std::unique_ptr<WebContents> web_contents =
244 WebContents::CreateWithSessionStorage(create_params, session_storages);
245 Shell* shell =
Bo Liu300c6052018-06-12 04:46:07246 CreateShell(std::move(web_contents), create_params.initial_size,
247 true /* should_set_delegate */);
Daniel Murphy16ed0782018-05-26 00:15:13248 if (!url.is_empty())
249 shell->LoadURL(url);
250 return shell;
251}
252
[email protected]9fbd3f862011-09-20 23:31:34253void Shell::LoadURL(const GURL& url) {
Alex Moshchuk7e26eca2018-03-03 01:34:29254 LoadURLForFrame(
255 url, std::string(),
256 ui::PageTransitionFromInt(ui::PAGE_TRANSITION_TYPED |
257 ui::PAGE_TRANSITION_FROM_ADDRESS_BAR));
[email protected]d2494ff2013-02-20 08:22:37258}
259
Alex Moshchuk7e26eca2018-03-03 01:34:29260void Shell::LoadURLForFrame(const GURL& url,
261 const std::string& frame_name,
262 ui::PageTransition transition_type) {
[email protected]d2494ff2013-02-20 08:22:37263 NavigationController::LoadURLParams params(url);
[email protected]d2494ff2013-02-20 08:22:37264 params.frame_name = frame_name;
Alex Moshchuk7e26eca2018-03-03 01:34:29265 params.transition_type = transition_type;
[email protected]d2494ff2013-02-20 08:22:37266 web_contents_->GetController().LoadURLWithParams(params);
[email protected]fc2b46b2014-05-03 16:33:45267 web_contents_->Focus();
[email protected]9fbd3f862011-09-20 23:31:34268}
269
[email protected]76bdecb2014-04-16 17:58:08270void Shell::LoadDataWithBaseURL(const GURL& url, const std::string& data,
271 const GURL& base_url) {
boliuec93ea92016-02-17 22:23:07272 bool load_as_string = false;
273 LoadDataWithBaseURLInternal(url, data, base_url, load_as_string);
274}
275
276#if defined(OS_ANDROID)
277void Shell::LoadDataAsStringWithBaseURL(const GURL& url,
278 const std::string& data,
279 const GURL& base_url) {
280 bool load_as_string = true;
281 LoadDataWithBaseURLInternal(url, data, base_url, load_as_string);
282}
283#endif
284
285void Shell::LoadDataWithBaseURLInternal(const GURL& url,
286 const std::string& data,
287 const GURL& base_url,
288 bool load_as_string) {
289#if !defined(OS_ANDROID)
290 DCHECK(!load_as_string); // Only supported on Android.
291#endif
292
293 NavigationController::LoadURLParams params(GURL::EmptyGURL());
294 const std::string data_url_header = "data:text/html;charset=utf-8,";
295 if (load_as_string) {
296 params.url = GURL(data_url_header);
297 std::string data_url_as_string = data_url_header + data;
298#if defined(OS_ANDROID)
299 params.data_url_as_string =
300 base::RefCountedString::TakeString(&data_url_as_string);
301#endif
302 } else {
303 params.url = GURL(data_url_header + data);
304 }
305
[email protected]76bdecb2014-04-16 17:58:08306 params.load_type = NavigationController::LOAD_TYPE_DATA;
307 params.base_url_for_data_url = base_url;
308 params.virtual_url_for_data_url = url;
309 params.override_user_agent = NavigationController::UA_OVERRIDE_FALSE;
310 web_contents_->GetController().LoadURLWithParams(params);
[email protected]fc2b46b2014-05-03 16:33:45311 web_contents_->Focus();
[email protected]76bdecb2014-04-16 17:58:08312}
313
[email protected]a2904092013-10-15 04:53:59314void Shell::AddNewContents(WebContents* source,
erikchenbee5c9622018-04-27 19:30:25315 std::unique_ptr<WebContents> new_contents,
[email protected]a2904092013-10-15 04:53:59316 WindowOpenDisposition disposition,
bokan107a47f2015-02-03 23:23:39317 const gfx::Rect& initial_rect,
[email protected]a2904092013-10-15 04:53:59318 bool user_gesture,
319 bool* was_blocked) {
erikchenbee5c9622018-04-27 19:30:25320 WebContents* raw_new_contents = new_contents.get();
Bo Liu300c6052018-06-12 04:46:07321 CreateShell(
322 std::move(new_contents), AdjustWindowSize(initial_rect.size()),
323 !delay_popup_contents_delegate_for_testing_ /* should_set_delegate */);
Kent Tamuracd3ebc42018-05-16 06:44:22324 if (switches::IsRunWebTestsSwitchPresent())
erikchenbee5c9622018-04-27 19:30:25325 SecondaryTestWindowObserver::CreateForWebContents(raw_new_contents);
[email protected]a2904092013-10-15 04:53:59326}
327
[email protected]9fbd3f862011-09-20 23:31:34328void Shell::GoBackOrForward(int offset) {
[email protected]0b659b32012-03-26 21:29:32329 web_contents_->GetController().GoToOffset(offset);
[email protected]fc2b46b2014-05-03 16:33:45330 web_contents_->Focus();
[email protected]9fbd3f862011-09-20 23:31:34331}
332
333void Shell::Reload() {
toyoshim6142d96f2016-12-19 09:07:25334 web_contents_->GetController().Reload(ReloadType::NORMAL, false);
[email protected]fc2b46b2014-05-03 16:33:45335 web_contents_->Focus();
[email protected]9fbd3f862011-09-20 23:31:34336}
337
toyoshime5aaf6a2016-05-18 08:07:48338void Shell::ReloadBypassingCache() {
toyoshim6142d96f2016-12-19 09:07:25339 web_contents_->GetController().Reload(ReloadType::BYPASSING_CACHE, false);
toyoshime5aaf6a2016-05-18 08:07:48340 web_contents_->Focus();
341}
342
[email protected]9fbd3f862011-09-20 23:31:34343void Shell::Stop() {
[email protected]0b659b32012-03-26 21:29:32344 web_contents_->Stop();
[email protected]fc2b46b2014-05-03 16:33:45345 web_contents_->Focus();
[email protected]9fbd3f862011-09-20 23:31:34346}
347
[email protected]e3b10d12014-03-28 16:06:09348void Shell::UpdateNavigationControls(bool to_different_document) {
[email protected]0b659b32012-03-26 21:29:32349 int current_index = web_contents_->GetController().GetCurrentEntryIndex();
350 int max_index = web_contents_->GetController().GetEntryCount() - 1;
[email protected]9fbd3f862011-09-20 23:31:34351
352 PlatformEnableUIControl(BACK_BUTTON, current_index > 0);
353 PlatformEnableUIControl(FORWARD_BUTTON, current_index < max_index);
[email protected]e3b10d12014-03-28 16:06:09354 PlatformEnableUIControl(STOP_BUTTON,
355 to_different_document && web_contents_->IsLoading());
[email protected]9fbd3f862011-09-20 23:31:34356}
357
[email protected]7c17b6992012-08-09 16:16:30358void Shell::ShowDevTools() {
mohsen6eb57fb2016-07-22 03:14:08359 if (!devtools_frontend_) {
360 devtools_frontend_ = ShellDevToolsFrontend::Show(web_contents());
361 devtools_observer_.reset(new DevToolsWebContentsObserver(
362 this, devtools_frontend_->frontend_shell()->web_contents()));
363 }
[email protected]3142e5d2014-02-07 00:54:46364
mohsen6eb57fb2016-07-22 03:14:08365 devtools_frontend_->Activate();
366 devtools_frontend_->Focus();
[email protected]7c17b6992012-08-09 16:16:30367}
368
[email protected]001841c92012-12-11 17:00:13369void Shell::CloseDevTools() {
[email protected]0773e0c2013-01-25 15:57:57370 if (!devtools_frontend_)
[email protected]001841c92012-12-11 17:00:13371 return;
[email protected]7fff43e2013-05-21 20:21:10372 devtools_observer_.reset();
[email protected]0773e0c2013-01-25 15:57:57373 devtools_frontend_->Close();
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28374 devtools_frontend_ = nullptr;
[email protected]001841c92012-12-11 17:00:13375}
376
[email protected]9fbd3f862011-09-20 23:31:34377gfx::NativeView Shell::GetContentView() {
[email protected]59383c782013-04-17 16:43:27378 if (!web_contents_)
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28379 return nullptr;
[email protected]fc2b46b2014-05-03 16:33:45380 return web_contents_->GetNativeView();
[email protected]9fbd3f862011-09-20 23:31:34381}
382
alexmos5a98a052016-01-06 00:15:02383WebContents* Shell::OpenURLFromTab(WebContents* source,
384 const OpenURLParams& params) {
lukasza04130152016-10-21 20:26:32385 WebContents* target = nullptr;
386 switch (params.disposition) {
387 case WindowOpenDisposition::CURRENT_TAB:
388 target = source;
389 break;
390
391 // Normally, the difference between NEW_POPUP and NEW_WINDOW is that a popup
392 // should have no toolbar, no status bar, no menu bar, no scrollbars and be
393 // not resizable. For simplicity and to enable new testing scenarios in
394 // content shell and layout tests, popups don't get special treatment below
395 // (i.e. they will have a toolbar and other things described here).
396 case WindowOpenDisposition::NEW_POPUP:
lukaszabe2f0da2017-04-25 00:43:00397 case WindowOpenDisposition::NEW_WINDOW:
398 // content_shell doesn't really support tabs, but some layout tests use
399 // middle click (which translates into kNavigationPolicyNewBackgroundTab),
400 // so we treat the cases below just like a NEW_WINDOW disposition.
401 case WindowOpenDisposition::NEW_BACKGROUND_TAB:
402 case WindowOpenDisposition::NEW_FOREGROUND_TAB: {
lukasza04130152016-10-21 20:26:32403 Shell* new_window =
404 Shell::CreateNewWindow(source->GetBrowserContext(),
405 GURL(), // Don't load anything just yet.
406 params.source_site_instance,
407 gfx::Size()); // Use default size.
408 target = new_window->web_contents();
Kent Tamuracd3ebc42018-05-16 06:44:22409 if (switches::IsRunWebTestsSwitchPresent())
lukasza04130152016-10-21 20:26:32410 SecondaryTestWindowObserver::CreateForWebContents(target);
411 break;
412 }
413
414 // No tabs in content_shell:
415 case WindowOpenDisposition::SINGLETON_TAB:
lukasza04130152016-10-21 20:26:32416 // No incognito mode in content_shell:
417 case WindowOpenDisposition::OFF_THE_RECORD:
418 // TODO(lukasza): Investigate if some layout tests might need support for
419 // SAVE_TO_DISK disposition. This would probably require that
420 // BlinkTestController always sets up and cleans up a temporary directory
421 // as the default downloads destinations for the duration of a test.
422 case WindowOpenDisposition::SAVE_TO_DISK:
423 // Ignoring requests with disposition == IGNORE_ACTION...
424 case WindowOpenDisposition::IGNORE_ACTION:
425 default:
426 return nullptr;
427 }
alexmos5a98a052016-01-06 00:15:02428
429 NavigationController::LoadURLParams load_url_params(params.url);
430 load_url_params.source_site_instance = params.source_site_instance;
431 load_url_params.transition_type = params.transition;
432 load_url_params.frame_tree_node_id = params.frame_tree_node_id;
433 load_url_params.referrer = params.referrer;
434 load_url_params.redirect_chain = params.redirect_chain;
435 load_url_params.extra_headers = params.extra_headers;
436 load_url_params.is_renderer_initiated = params.is_renderer_initiated;
alexmos5a98a052016-01-06 00:15:02437 load_url_params.should_replace_current_entry =
438 params.should_replace_current_entry;
Marijn Kruisselbrink7a0d5e182018-05-24 22:55:09439 load_url_params.blob_url_loader_factory = params.blob_url_loader_factory;
alexmos5a98a052016-01-06 00:15:02440
lukasza477a5a22016-06-16 18:28:43441 if (params.uses_post) {
442 load_url_params.load_type = NavigationController::LOAD_TYPE_HTTP_POST;
443 load_url_params.post_data = params.post_data;
alexmos5a98a052016-01-06 00:15:02444 }
445
lukasza04130152016-10-21 20:26:32446 target->GetController().LoadURLWithParams(load_url_params);
447 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);
[email protected]15aea412012-01-19 03:18:50453 PlatformSetIsLoading(source->IsLoading());
[email protected]e99ca5112011-09-26 17:22:54454}
455
Dave Tapuska3ed44192018-05-01 18:53:30456void Shell::EnterFullscreenModeForTab(
457 WebContents* web_contents,
458 const GURL& origin,
459 const blink::WebFullscreenOptions& options) {
mlamouri7a78d6fd2015-01-17 13:23:53460 ToggleFullscreenModeForTab(web_contents, true);
461}
462
463void Shell::ExitFullscreenModeForTab(WebContents* web_contents) {
464 ToggleFullscreenModeForTab(web_contents, false);
465}
466
[email protected]99c014c2012-11-27 12:03:42467void Shell::ToggleFullscreenModeForTab(WebContents* web_contents,
468 bool enter_fullscreen) {
469#if defined(OS_ANDROID)
470 PlatformToggleFullscreenModeForTab(web_contents, enter_fullscreen);
471#endif
Kent Tamuracd3ebc42018-05-16 06:44:22472 if (!switches::IsRunWebTestsSwitchPresent())
[email protected]99c014c2012-11-27 12:03:42473 return;
474 if (is_fullscreen_ != enter_fullscreen) {
475 is_fullscreen_ = enter_fullscreen;
Fady Samuel0b911822018-04-25 13:22:16476 web_contents->GetRenderViewHost()
477 ->GetWidget()
478 ->SynchronizeVisualProperties();
[email protected]99c014c2012-11-27 12:03:42479 }
480}
481
482bool Shell::IsFullscreenForTabOrPending(const WebContents* web_contents) const {
483#if defined(OS_ANDROID)
484 return PlatformIsFullscreenForTabOrPending(web_contents);
485#else
486 return is_fullscreen_;
487#endif
488}
489
mikhail.pozdnyakovc0e251b2015-04-15 06:51:12490blink::WebDisplayMode Shell::GetDisplayMode(
491 const WebContents* web_contents) const {
492 // TODO : should return blink::WebDisplayModeFullscreen wherever user puts
493 // a browser window into fullscreen (not only in case of renderer-initiated
494 // fullscreen mode): crbug.com/476874.
Blink Reformat1c4d759e2017-04-09 16:34:54495 return IsFullscreenForTabOrPending(web_contents)
496 ? blink::kWebDisplayModeFullscreen
497 : blink::kWebDisplayModeBrowser;
mikhail.pozdnyakovc0e251b2015-04-15 06:51:12498}
499
[email protected]f78439002012-11-28 14:45:59500void Shell::RequestToLockMouse(WebContents* web_contents,
501 bool user_gesture,
502 bool last_unlocked_by_target) {
503 web_contents->GotResponseToLockMouseRequest(true);
504}
505
[email protected]9e00e6352012-07-30 17:05:18506void Shell::CloseContents(WebContents* source) {
507 Close();
508}
509
[email protected]067310262012-11-22 14:30:41510bool Shell::CanOverscrollContent() const {
511#if defined(USE_AURA)
512 return true;
513#else
514 return false;
515#endif
516}
517
[email protected]3bbacc5b2012-04-17 17:46:15518void Shell::DidNavigateMainFramePostCommit(WebContents* web_contents) {
[email protected]4e3c7522013-08-13 11:53:18519 PlatformSetAddressBarURL(web_contents->GetLastCommittedURL());
[email protected]e99ca5112011-09-26 17:22:54520}
521
mathiash72a5e462014-11-19 08:18:50522JavaScriptDialogManager* Shell::GetJavaScriptDialogManager(
523 WebContents* source) {
mkwst6efe7f2e62014-10-08 15:09:01524 if (!dialog_manager_) {
Kent Tamuracd3ebc42018-05-16 06:44:22525 dialog_manager_.reset(switches::IsRunWebTestsSwitchPresent()
lukasza381b0492016-03-10 16:48:43526 ? new LayoutTestJavaScriptDialogManager
527 : new ShellJavaScriptDialogManager);
mkwst6efe7f2e62014-10-08 15:09:01528 }
[email protected]71a88bb2013-02-01 22:05:15529 return dialog_manager_.get();
[email protected]f2210022012-03-29 00:36:08530}
531
dcheng6003e0b2016-04-09 18:42:34532std::unique_ptr<BluetoothChooser> Shell::RunBluetoothChooser(
jyasskin93649a52016-02-17 19:57:02533 RenderFrameHost* frame,
534 const BluetoothChooser::EventHandler& event_handler) {
Lukasz Anforowiczf227b4912017-12-22 23:08:59535 BlinkTestController* blink_test_controller = BlinkTestController::Get();
Kent Tamuracd3ebc42018-05-16 06:44:22536 if (blink_test_controller && switches::IsRunWebTestsSwitchPresent())
Lukasz Anforowiczf227b4912017-12-22 23:08:59537 return blink_test_controller->RunBluetoothChooser(frame, event_handler);
jyasskin8057c002015-09-10 19:09:53538 return nullptr;
539}
540
avia90ae4e2016-11-11 20:49:33541bool Shell::DidAddMessageToConsole(WebContents* source,
542 int32_t level,
543 const base::string16& message,
544 int32_t line_no,
545 const base::string16& source_id) {
Kent Tamuracd3ebc42018-05-16 06:44:22546 return switches::IsRunWebTestsSwitchPresent();
[email protected]efb5f572012-01-29 10:57:33547}
548
Avi Drissman8920def2018-01-31 19:53:36549void Shell::RendererUnresponsive(WebContents* source,
Avi Drissman1572e8c3c2018-02-02 19:06:36550 RenderWidgetHost* render_widget_host) {
Lukasz Anforowiczf227b4912017-12-22 23:08:59551 BlinkTestController* blink_test_controller = BlinkTestController::Get();
Kent Tamuracd3ebc42018-05-16 06:44:22552 if (blink_test_controller && switches::IsRunWebTestsSwitchPresent())
Lukasz Anforowiczf227b4912017-12-22 23:08:59553 blink_test_controller->RendererUnresponsive();
[email protected]5bf68f22012-08-31 07:38:10554}
555
[email protected]233567d2013-02-27 20:22:02556void Shell::ActivateContents(WebContents* contents) {
avif9ab5d942015-10-15 14:05:44557 contents->GetRenderViewHost()->GetWidget()->Focus();
[email protected]233567d2013-02-27 20:22:02558}
559
carloskd9d97942017-02-16 08:58:09560bool Shell::ShouldAllowRunningInsecureContent(
561 content::WebContents* web_contents,
562 bool allowed_per_prefs,
563 const url::Origin& origin,
564 const GURL& resource_url) {
565 bool allowed_by_test = false;
Lukasz Anforowiczf227b4912017-12-22 23:08:59566 BlinkTestController* blink_test_controller = BlinkTestController::Get();
Kent Tamuracd3ebc42018-05-16 06:44:22567 if (blink_test_controller && switches::IsRunWebTestsSwitchPresent()) {
carloskd9d97942017-02-16 08:58:09568 const base::DictionaryValue& test_flags =
Lukasz Anforowiczf227b4912017-12-22 23:08:59569 blink_test_controller->accumulated_layout_test_runtime_flags_changes();
carloskd9d97942017-02-16 08:58:09570 test_flags.GetBoolean("running_insecure_content_allowed", &allowed_by_test);
571 }
572
573 return allowed_per_prefs || allowed_by_test;
574}
575
Mounir Lamouri11e9ef432018-05-22 03:10:16576gfx::Size Shell::EnterPictureInPicture(const viz::SurfaceId& surface_id,
577 const gfx::Size& natural_size) {
578 // During tests, returning a fake window size to pretent the window was
579 // created and allow tests to run accordingly.
580 return switches::IsRunWebTestsSwitchPresent() ? gfx::Size(42, 42)
581 : gfx::Size(0, 0);
582}
583
Bo Liu300c6052018-06-12 04:46:07584bool Shell::ShouldResumeRequestsForCreatedWindow() {
585 return !delay_popup_contents_delegate_for_testing_;
586}
587
pdrcab84ee2015-03-13 21:47:04588gfx::Size Shell::GetShellDefaultSize() {
589 static gfx::Size default_shell_size;
590 if (!default_shell_size.IsEmpty())
591 return default_shell_size;
592 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
593 if (command_line->HasSwitch(switches::kContentShellHostWindowSize)) {
594 const std::string size_str = command_line->GetSwitchValueASCII(
595 switches::kContentShellHostWindowSize);
596 int width, height;
597 CHECK_EQ(2, sscanf(size_str.c_str(), "%dx%d", &width, &height));
598 default_shell_size = gfx::Size(width, height);
599 } else {
600 default_shell_size = gfx::Size(
601 kDefaultTestWindowWidthDip, kDefaultTestWindowHeightDip);
602 }
603 return default_shell_size;
604}
605
Avi Drissman93002212017-09-27 03:20:52606void Shell::TitleWasSet(NavigationEntry* entry) {
[email protected]1ef02d242013-10-07 16:18:53607 if (entry)
608 PlatformSetTitle(entry->GetTitle());
[email protected]aecc085b2012-06-01 18:15:53609}
610
[email protected]7fff43e2013-05-21 20:21:10611void Shell::OnDevToolsWebContentsDestroyed() {
612 devtools_observer_.reset();
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28613 devtools_frontend_ = nullptr;
[email protected]7fff43e2013-05-21 20:21:10614}
615
[email protected]9fbd3f862011-09-20 23:31:34616} // namespace content