blob: 6889a55534859cf2613e436e97c2a9de725bb023 [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"
Yutaka Hirano2109e582018-02-14 07:24:4628#include "content/public/browser/render_process_host.h"
[email protected]0b659b32012-03-26 21:29:3229#include "content/public/browser/render_view_host.h"
avif9ab5d942015-10-15 14:05:4430#include "content/public/browser/render_widget_host.h"
[email protected]0b659b32012-03-26 21:29:3231#include "content/public/browser/web_contents.h"
guoweis8efb6d892015-10-12 18:26:1732#include "content/public/common/content_switches.h"
guoweisc537ba22015-11-06 21:20:3133#include "content/public/common/webrtc_ip_handling_policy.h"
[email protected]de7d61ff2013-08-20 11:30:4134#include "content/shell/browser/shell_browser_main_parts.h"
35#include "content/shell/browser/shell_content_browser_client.h"
36#include "content/shell/browser/shell_devtools_frontend.h"
37#include "content/shell/browser/shell_javascript_dialog_manager.h"
Kent Tamuraf92649d882018-12-03 09:07:4738#include "content/shell/browser/web_test/blink_test_controller.h"
39#include "content/shell/browser/web_test/secondary_test_window_observer.h"
40#include "content/shell/browser/web_test/web_test_bluetooth_chooser_factory.h"
41#include "content/shell/browser/web_test/web_test_devtools_bindings.h"
42#include "content/shell/browser/web_test/web_test_javascript_dialog_manager.h"
[email protected]b7c504c2013-05-07 14:42:1243#include "content/shell/common/shell_messages.h"
44#include "content/shell/common/shell_switches.h"
Kent Tamurade503112018-12-04 08:57:4045#include "content/shell/common/web_test/web_test_switches.h"
Scott Violeta35f9a42018-03-22 22:00:4446#include "media/media_buildflags.h"
Leon Hanc819dc62019-01-28 04:30:1947#include "third_party/blink/public/mojom/renderer_preferences.mojom.h"
Blink Reformata30d4232018-04-07 15:31:0648#include "third_party/blink/public/web/web_presentation_receiver_flags.h"
[email protected]9fbd3f862011-09-20 23:31:3449
[email protected]9fbd3f862011-09-20 23:31:3450namespace content {
51
Wezcbf4a042018-06-13 16:29:1252// Null until/unless the default main message loop is running.
53base::NoDestructor<base::OnceClosure> g_quit_main_message_loop;
Wez9a58a152018-06-07 18:59:3354
pdrcab84ee2015-03-13 21:47:0455const int kDefaultTestWindowWidthDip = 800;
56const int kDefaultTestWindowHeightDip = 600;
[email protected]1e57cab2013-05-28 04:26:1157
[email protected]e99ca5112011-09-26 17:22:5458std::vector<Shell*> Shell::windows_;
[email protected]9e00e6352012-07-30 17:05:1859base::Callback<void(Shell*)> Shell::shell_created_callback_;
[email protected]9fbd3f862011-09-20 23:31:3460
[email protected]7fff43e2013-05-21 20:21:1061class Shell::DevToolsWebContentsObserver : public WebContentsObserver {
62 public:
63 DevToolsWebContentsObserver(Shell* shell, WebContents* web_contents)
64 : WebContentsObserver(web_contents),
65 shell_(shell) {
66 }
67
68 // WebContentsObserver
dchenge933b3e2014-10-21 11:44:0969 void WebContentsDestroyed() override {
[email protected]7fff43e2013-05-21 20:21:1070 shell_->OnDevToolsWebContentsDestroyed();
71 }
72
73 private:
74 Shell* shell_;
75
76 DISALLOW_COPY_AND_ASSIGN(DevToolsWebContentsObserver);
77};
78
Bo Liu300c6052018-06-12 04:46:0779Shell::Shell(std::unique_ptr<WebContents> web_contents,
80 bool should_set_delegate)
erikchenbee5c9622018-04-27 19:30:2581 : WebContentsObserver(web_contents.get()),
82 web_contents_(std::move(web_contents)),
Ivan Kotenkov2c0d2bb32017-11-01 15:41:2883 devtools_frontend_(nullptr),
[email protected]001841c92012-12-11 17:00:1384 is_fullscreen_(false),
Ivan Kotenkov2c0d2bb32017-11-01 15:41:2885 window_(nullptr),
sadrul8b11d262015-11-12 18:41:3386#if defined(OS_MACOSX)
[email protected]86b36e672012-12-21 00:09:5187 url_edit_view_(NULL),
sadrul8b11d262015-11-12 18:41:3388#endif
Pavel Feldmanc7cd063c2017-10-06 20:04:2889 headless_(false),
90 hide_toolbar_(false) {
Bo Liu300c6052018-06-12 04:46:0791 if (should_set_delegate)
92 web_contents_->SetDelegate(this);
arthursonzognifdd49912017-08-31 08:55:2693
Kent Tamuracd3ebc42018-05-16 06:44:2294 if (switches::IsRunWebTestsSwitchPresent()) {
[email protected]86b36e672012-12-21 00:09:5195 headless_ = true;
Francois Doraye6161152018-03-27 22:05:3796 // In a headless shell, disable occlusion tracking. Otherwise, WebContents
97 // would always behave as if they were occluded, i.e. would not render
98 // frames and would not receive input events.
99 base::CommandLine::ForCurrentProcess()->AppendSwitch(
100 switches::kDisableBackgroundingOccludedWindowsForTesting);
101 }
Pavel Feldmanc7cd063c2017-10-06 20:04:28102
103 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
104 switches::kContentShellHideToolbar))
105 hide_toolbar_ = true;
106
[email protected]9e00e6352012-07-30 17:05:18107 windows_.push_back(this);
108
109 if (!shell_created_callback_.is_null()) {
110 shell_created_callback_.Run(this);
111 shell_created_callback_.Reset();
112 }
[email protected]9fbd3f862011-09-20 23:31:34113}
114
115Shell::~Shell() {
116 PlatformCleanUp();
[email protected]e99ca5112011-09-26 17:22:54117
118 for (size_t i = 0; i < windows_.size(); ++i) {
119 if (windows_[i] == this) {
120 windows_.erase(windows_.begin() + i);
121 break;
122 }
123 }
[email protected]11a65b692012-03-30 11:29:16124
Sergey Ulanovf0875d12019-01-03 20:33:23125 // Always destroy WebContents before calling PlatformExit(). WebContents
126 // destruction sequence may depend on the resources destroyed in
127 // PlatformExit() (e.g. the display::Screen singleton).
128 web_contents_->SetDelegate(nullptr);
129 web_contents_.reset();
130
Wez9a58a152018-06-07 18:59:33131 if (windows_.empty()) {
[email protected]b53adf452014-02-07 12:55:08132 if (headless_)
133 PlatformExit();
Yutaka Hirano2109e582018-02-14 07:24:46134 for (auto it = RenderProcessHost::AllHostsIterator(); !it.IsAtEnd();
135 it.Advance()) {
136 it.GetCurrentValue()->DisableKeepAliveRefCount();
137 }
Wezcbf4a042018-06-13 16:29:12138 if (*g_quit_main_message_loop)
139 std::move(*g_quit_main_message_loop).Run();
[email protected]b53adf452014-02-07 12:55:08140 }
[email protected]9fbd3f862011-09-20 23:31:34141}
142
erikchenbee5c9622018-04-27 19:30:25143Shell* Shell::CreateShell(std::unique_ptr<WebContents> web_contents,
Bo Liu300c6052018-06-12 04:46:07144 const gfx::Size& initial_size,
145 bool should_set_delegate) {
erikchenbee5c9622018-04-27 19:30:25146 WebContents* raw_web_contents = web_contents.get();
Bo Liu300c6052018-06-12 04:46:07147 Shell* shell = new Shell(std::move(web_contents), should_set_delegate);
[email protected]c0d036d2013-05-07 12:50:50148 shell->PlatformCreateWindow(initial_size.width(), initial_size.height());
[email protected]3fd84032012-01-12 18:20:17149
[email protected]5cba3bf2012-01-14 02:40:35150 shell->PlatformSetContents();
[email protected]3fd84032012-01-12 18:20:17151
152 shell->PlatformResizeSubViews();
[email protected]1596efb2013-01-17 22:13:01153
creisb6561df2016-02-11 20:20:54154 // Note: Do not make RenderFrameHost or RenderViewHost specific state changes
155 // here, because they will be forgotten after a cross-process navigation. Use
156 // RenderFrameCreated or RenderViewCreated instead.
Kent Tamuracd3ebc42018-05-16 06:44:22157 if (switches::IsRunWebTestsSwitchPresent()) {
erikchenbee5c9622018-04-27 19:30:25158 raw_web_contents->GetMutableRendererPrefs()->use_custom_colors = false;
159 raw_web_contents->GetRenderViewHost()->SyncRendererPrefs();
[email protected]1596efb2013-01-17 22:13:01160 }
161
lukasza381b0492016-03-10 16:48:43162 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
guoweis4ee48592015-12-02 06:37:07163 if (command_line->HasSwitch(switches::kForceWebRtcIPHandlingPolicy)) {
erikchenbee5c9622018-04-27 19:30:25164 raw_web_contents->GetMutableRendererPrefs()->webrtc_ip_handling_policy =
guoweis4ee48592015-12-02 06:37:07165 command_line->GetSwitchValueASCII(
166 switches::kForceWebRtcIPHandlingPolicy);
guoweis8efb6d892015-10-12 18:26:17167 }
guoweis8efb6d892015-10-12 18:26:17168
[email protected]3fd84032012-01-12 18:20:17169 return shell;
170}
171
[email protected]11a65b692012-03-30 11:29:16172void Shell::CloseAllWindows() {
[email protected]b50452f2014-08-18 12:31:44173 DevToolsAgentHost::DetachAllClients();
[email protected]11a65b692012-03-30 11:29:16174 std::vector<Shell*> open_windows(windows_);
Wezcbf4a042018-06-13 16:29:12175 for (size_t i = 0; i < open_windows.size(); ++i)
176 open_windows[i]->Close();
177
178 // Pump the message loop to allow window teardown tasks to run.
179 base::RunLoop().RunUntilIdle();
180
Wez7d3eb012018-06-20 22:51:28181 // If there were no windows open then the message loop quit closure will
182 // not have been run.
183 if (*g_quit_main_message_loop)
184 std::move(*g_quit_main_message_loop).Run();
185
dmazzoni948fbc72015-05-17 07:57:33186 PlatformExit();
[email protected]11a65b692012-03-30 11:29:16187}
188
Wezcbf4a042018-06-13 16:29:12189void Shell::SetMainMessageLoopQuitClosure(base::OnceClosure quit_closure) {
190 *g_quit_main_message_loop = std::move(quit_closure);
191}
192
Wez7d3eb012018-06-20 22:51:28193void Shell::QuitMainMessageLoopForTesting() {
John Budorick7732d4d02018-07-30 19:02:54194 DCHECK(*g_quit_main_message_loop);
Wez7d3eb012018-06-20 22:51:28195 std::move(*g_quit_main_message_loop).Run();
196}
197
[email protected]9e00e6352012-07-30 17:05:18198void Shell::SetShellCreatedCallback(
199 base::Callback<void(Shell*)> shell_created_callback) {
200 DCHECK(shell_created_callback_.is_null());
Tommy Nyquist4b749d02018-03-20 21:46:29201 shell_created_callback_ = std::move(shell_created_callback);
[email protected]9e00e6352012-07-30 17:05:18202}
203
David Benjaminf62c6662019-03-21 20:25:04204Shell* Shell::FromWebContents(WebContents* web_contents) {
205 for (Shell* window : windows_) {
206 if (window->web_contents() && window->web_contents() == web_contents) {
207 return window;
[email protected]74830f02012-01-30 22:27:04208 }
209 }
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28210 return nullptr;
[email protected]74830f02012-01-30 22:27:04211}
212
[email protected]6153b272013-01-25 22:29:23213void Shell::Initialize() {
pdrcab84ee2015-03-13 21:47:04214 PlatformInitialize(GetShellDefaultSize());
[email protected]6153b272013-01-25 22:29:23215}
216
[email protected]a2904092013-10-15 04:53:59217gfx::Size Shell::AdjustWindowSize(const gfx::Size& initial_size) {
218 if (!initial_size.IsEmpty())
219 return initial_size;
pdrcab84ee2015-03-13 21:47:04220 return GetShellDefaultSize();
[email protected]a2904092013-10-15 04:53:59221}
222
[email protected]bdcf9152012-07-19 17:43:21223Shell* Shell::CreateNewWindow(BrowserContext* browser_context,
[email protected]e99ca5112011-09-26 17:22:54224 const GURL& url,
lukasza04130152016-10-21 20:26:32225 const scoped_refptr<SiteInstance>& site_instance,
[email protected]cdb806722013-01-10 14:18:23226 const gfx::Size& initial_size) {
[email protected]54944cde2012-12-09 09:24:59227 WebContents::CreateParams create_params(browser_context, site_instance);
mark a. foltzef394fce2017-10-21 09:11:02228 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
229 switches::kForcePresentationReceiverForTesting)) {
230 create_params.starting_sandbox_flags =
231 blink::kPresentationReceiverSandboxFlags;
232 }
[email protected]a2904092013-10-15 04:53:59233 create_params.initial_size = AdjustWindowSize(initial_size);
erikchenbee5c9622018-04-27 19:30:25234 std::unique_ptr<WebContents> web_contents =
Erik Chenbb8e738e2018-04-28 14:10:43235 WebContents::Create(create_params);
erikchenbee5c9622018-04-27 19:30:25236 Shell* shell =
Bo Liu300c6052018-06-12 04:46:07237 CreateShell(std::move(web_contents), create_params.initial_size,
238 true /* should_set_delegate */);
[email protected]e99ca5112011-09-26 17:22:54239 if (!url.is_empty())
240 shell->LoadURL(url);
[email protected]9fbd3f862011-09-20 23:31:34241 return shell;
242}
243
Daniel Murphy16ed0782018-05-26 00:15:13244Shell* Shell::CreateNewWindowWithSessionStorageNamespace(
245 BrowserContext* browser_context,
246 const GURL& url,
247 const scoped_refptr<SiteInstance>& site_instance,
248 const gfx::Size& initial_size,
249 scoped_refptr<SessionStorageNamespace> session_storage_namespace) {
250 WebContents::CreateParams create_params(browser_context, site_instance);
251 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
252 switches::kForcePresentationReceiverForTesting)) {
253 create_params.starting_sandbox_flags =
254 blink::kPresentationReceiverSandboxFlags;
255 }
256 create_params.initial_size = AdjustWindowSize(initial_size);
257 std::map<std::string, scoped_refptr<SessionStorageNamespace>>
258 session_storages;
259 session_storages[""] = session_storage_namespace;
260 std::unique_ptr<WebContents> web_contents =
261 WebContents::CreateWithSessionStorage(create_params, session_storages);
262 Shell* shell =
Bo Liu300c6052018-06-12 04:46:07263 CreateShell(std::move(web_contents), create_params.initial_size,
264 true /* should_set_delegate */);
Daniel Murphy16ed0782018-05-26 00:15:13265 if (!url.is_empty())
266 shell->LoadURL(url);
267 return shell;
268}
269
[email protected]9fbd3f862011-09-20 23:31:34270void Shell::LoadURL(const GURL& url) {
Alex Moshchuk7e26eca2018-03-03 01:34:29271 LoadURLForFrame(
272 url, std::string(),
273 ui::PageTransitionFromInt(ui::PAGE_TRANSITION_TYPED |
274 ui::PAGE_TRANSITION_FROM_ADDRESS_BAR));
[email protected]d2494ff2013-02-20 08:22:37275}
276
Alex Moshchuk7e26eca2018-03-03 01:34:29277void Shell::LoadURLForFrame(const GURL& url,
278 const std::string& frame_name,
279 ui::PageTransition transition_type) {
[email protected]d2494ff2013-02-20 08:22:37280 NavigationController::LoadURLParams params(url);
[email protected]d2494ff2013-02-20 08:22:37281 params.frame_name = frame_name;
Alex Moshchuk7e26eca2018-03-03 01:34:29282 params.transition_type = transition_type;
[email protected]d2494ff2013-02-20 08:22:37283 web_contents_->GetController().LoadURLWithParams(params);
[email protected]fc2b46b2014-05-03 16:33:45284 web_contents_->Focus();
[email protected]9fbd3f862011-09-20 23:31:34285}
286
[email protected]76bdecb2014-04-16 17:58:08287void Shell::LoadDataWithBaseURL(const GURL& url, const std::string& data,
288 const GURL& base_url) {
boliuec93ea92016-02-17 22:23:07289 bool load_as_string = false;
290 LoadDataWithBaseURLInternal(url, data, base_url, load_as_string);
291}
292
293#if defined(OS_ANDROID)
294void Shell::LoadDataAsStringWithBaseURL(const GURL& url,
295 const std::string& data,
296 const GURL& base_url) {
297 bool load_as_string = true;
298 LoadDataWithBaseURLInternal(url, data, base_url, load_as_string);
299}
300#endif
301
302void Shell::LoadDataWithBaseURLInternal(const GURL& url,
303 const std::string& data,
304 const GURL& base_url,
305 bool load_as_string) {
306#if !defined(OS_ANDROID)
307 DCHECK(!load_as_string); // Only supported on Android.
308#endif
309
310 NavigationController::LoadURLParams params(GURL::EmptyGURL());
311 const std::string data_url_header = "data:text/html;charset=utf-8,";
312 if (load_as_string) {
313 params.url = GURL(data_url_header);
314 std::string data_url_as_string = data_url_header + data;
315#if defined(OS_ANDROID)
316 params.data_url_as_string =
317 base::RefCountedString::TakeString(&data_url_as_string);
318#endif
319 } else {
320 params.url = GURL(data_url_header + data);
321 }
322
[email protected]76bdecb2014-04-16 17:58:08323 params.load_type = NavigationController::LOAD_TYPE_DATA;
324 params.base_url_for_data_url = base_url;
325 params.virtual_url_for_data_url = url;
326 params.override_user_agent = NavigationController::UA_OVERRIDE_FALSE;
327 web_contents_->GetController().LoadURLWithParams(params);
[email protected]fc2b46b2014-05-03 16:33:45328 web_contents_->Focus();
[email protected]76bdecb2014-04-16 17:58:08329}
330
[email protected]a2904092013-10-15 04:53:59331void Shell::AddNewContents(WebContents* source,
erikchenbee5c9622018-04-27 19:30:25332 std::unique_ptr<WebContents> new_contents,
[email protected]a2904092013-10-15 04:53:59333 WindowOpenDisposition disposition,
bokan107a47f2015-02-03 23:23:39334 const gfx::Rect& initial_rect,
[email protected]a2904092013-10-15 04:53:59335 bool user_gesture,
336 bool* was_blocked) {
erikchenbee5c9622018-04-27 19:30:25337 WebContents* raw_new_contents = new_contents.get();
Bo Liu300c6052018-06-12 04:46:07338 CreateShell(
339 std::move(new_contents), AdjustWindowSize(initial_rect.size()),
340 !delay_popup_contents_delegate_for_testing_ /* should_set_delegate */);
Kent Tamuracd3ebc42018-05-16 06:44:22341 if (switches::IsRunWebTestsSwitchPresent())
erikchenbee5c9622018-04-27 19:30:25342 SecondaryTestWindowObserver::CreateForWebContents(raw_new_contents);
[email protected]a2904092013-10-15 04:53:59343}
344
[email protected]9fbd3f862011-09-20 23:31:34345void Shell::GoBackOrForward(int offset) {
[email protected]0b659b32012-03-26 21:29:32346 web_contents_->GetController().GoToOffset(offset);
[email protected]fc2b46b2014-05-03 16:33:45347 web_contents_->Focus();
[email protected]9fbd3f862011-09-20 23:31:34348}
349
350void Shell::Reload() {
toyoshim6142d96f2016-12-19 09:07:25351 web_contents_->GetController().Reload(ReloadType::NORMAL, false);
[email protected]fc2b46b2014-05-03 16:33:45352 web_contents_->Focus();
[email protected]9fbd3f862011-09-20 23:31:34353}
354
toyoshime5aaf6a2016-05-18 08:07:48355void Shell::ReloadBypassingCache() {
toyoshim6142d96f2016-12-19 09:07:25356 web_contents_->GetController().Reload(ReloadType::BYPASSING_CACHE, false);
toyoshime5aaf6a2016-05-18 08:07:48357 web_contents_->Focus();
358}
359
[email protected]9fbd3f862011-09-20 23:31:34360void Shell::Stop() {
[email protected]0b659b32012-03-26 21:29:32361 web_contents_->Stop();
[email protected]fc2b46b2014-05-03 16:33:45362 web_contents_->Focus();
[email protected]9fbd3f862011-09-20 23:31:34363}
364
[email protected]e3b10d12014-03-28 16:06:09365void Shell::UpdateNavigationControls(bool to_different_document) {
[email protected]0b659b32012-03-26 21:29:32366 int current_index = web_contents_->GetController().GetCurrentEntryIndex();
367 int max_index = web_contents_->GetController().GetEntryCount() - 1;
[email protected]9fbd3f862011-09-20 23:31:34368
369 PlatformEnableUIControl(BACK_BUTTON, current_index > 0);
370 PlatformEnableUIControl(FORWARD_BUTTON, current_index < max_index);
[email protected]e3b10d12014-03-28 16:06:09371 PlatformEnableUIControl(STOP_BUTTON,
372 to_different_document && web_contents_->IsLoading());
[email protected]9fbd3f862011-09-20 23:31:34373}
374
[email protected]7c17b6992012-08-09 16:16:30375void Shell::ShowDevTools() {
mohsen6eb57fb2016-07-22 03:14:08376 if (!devtools_frontend_) {
377 devtools_frontend_ = ShellDevToolsFrontend::Show(web_contents());
378 devtools_observer_.reset(new DevToolsWebContentsObserver(
379 this, devtools_frontend_->frontend_shell()->web_contents()));
380 }
[email protected]3142e5d2014-02-07 00:54:46381
mohsen6eb57fb2016-07-22 03:14:08382 devtools_frontend_->Activate();
383 devtools_frontend_->Focus();
[email protected]7c17b6992012-08-09 16:16:30384}
385
[email protected]001841c92012-12-11 17:00:13386void Shell::CloseDevTools() {
[email protected]0773e0c2013-01-25 15:57:57387 if (!devtools_frontend_)
[email protected]001841c92012-12-11 17:00:13388 return;
[email protected]7fff43e2013-05-21 20:21:10389 devtools_observer_.reset();
[email protected]0773e0c2013-01-25 15:57:57390 devtools_frontend_->Close();
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28391 devtools_frontend_ = nullptr;
[email protected]001841c92012-12-11 17:00:13392}
393
[email protected]9fbd3f862011-09-20 23:31:34394gfx::NativeView Shell::GetContentView() {
[email protected]59383c782013-04-17 16:43:27395 if (!web_contents_)
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28396 return nullptr;
[email protected]fc2b46b2014-05-03 16:33:45397 return web_contents_->GetNativeView();
[email protected]9fbd3f862011-09-20 23:31:34398}
399
alexmos5a98a052016-01-06 00:15:02400WebContents* Shell::OpenURLFromTab(WebContents* source,
401 const OpenURLParams& params) {
lukasza04130152016-10-21 20:26:32402 WebContents* target = nullptr;
403 switch (params.disposition) {
404 case WindowOpenDisposition::CURRENT_TAB:
405 target = source;
406 break;
407
408 // Normally, the difference between NEW_POPUP and NEW_WINDOW is that a popup
409 // should have no toolbar, no status bar, no menu bar, no scrollbars and be
410 // not resizable. For simplicity and to enable new testing scenarios in
Kent Tamura21d1de62018-12-10 04:45:20411 // content shell and web tests, popups don't get special treatment below
lukasza04130152016-10-21 20:26:32412 // (i.e. they will have a toolbar and other things described here).
413 case WindowOpenDisposition::NEW_POPUP:
lukaszabe2f0da2017-04-25 00:43:00414 case WindowOpenDisposition::NEW_WINDOW:
Kent Tamura21d1de62018-12-10 04:45:20415 // content_shell doesn't really support tabs, but some web tests use
lukaszabe2f0da2017-04-25 00:43:00416 // middle click (which translates into kNavigationPolicyNewBackgroundTab),
417 // so we treat the cases below just like a NEW_WINDOW disposition.
418 case WindowOpenDisposition::NEW_BACKGROUND_TAB:
419 case WindowOpenDisposition::NEW_FOREGROUND_TAB: {
lukasza04130152016-10-21 20:26:32420 Shell* new_window =
421 Shell::CreateNewWindow(source->GetBrowserContext(),
422 GURL(), // Don't load anything just yet.
423 params.source_site_instance,
424 gfx::Size()); // Use default size.
425 target = new_window->web_contents();
Kent Tamuracd3ebc42018-05-16 06:44:22426 if (switches::IsRunWebTestsSwitchPresent())
lukasza04130152016-10-21 20:26:32427 SecondaryTestWindowObserver::CreateForWebContents(target);
428 break;
429 }
430
431 // No tabs in content_shell:
432 case WindowOpenDisposition::SINGLETON_TAB:
lukasza04130152016-10-21 20:26:32433 // No incognito mode in content_shell:
434 case WindowOpenDisposition::OFF_THE_RECORD:
Kent Tamura21d1de62018-12-10 04:45:20435 // TODO(lukasza): Investigate if some web tests might need support for
lukasza04130152016-10-21 20:26:32436 // SAVE_TO_DISK disposition. This would probably require that
437 // BlinkTestController always sets up and cleans up a temporary directory
438 // as the default downloads destinations for the duration of a test.
439 case WindowOpenDisposition::SAVE_TO_DISK:
440 // Ignoring requests with disposition == IGNORE_ACTION...
441 case WindowOpenDisposition::IGNORE_ACTION:
442 default:
443 return nullptr;
444 }
alexmos5a98a052016-01-06 00:15:02445
446 NavigationController::LoadURLParams load_url_params(params.url);
Nasko Oskov93e7c55c2018-12-19 01:59:29447 load_url_params.initiator_origin = params.initiator_origin;
alexmos5a98a052016-01-06 00:15:02448 load_url_params.source_site_instance = params.source_site_instance;
449 load_url_params.transition_type = params.transition;
450 load_url_params.frame_tree_node_id = params.frame_tree_node_id;
451 load_url_params.referrer = params.referrer;
452 load_url_params.redirect_chain = params.redirect_chain;
453 load_url_params.extra_headers = params.extra_headers;
454 load_url_params.is_renderer_initiated = params.is_renderer_initiated;
alexmos5a98a052016-01-06 00:15:02455 load_url_params.should_replace_current_entry =
456 params.should_replace_current_entry;
Marijn Kruisselbrink7a0d5e182018-05-24 22:55:09457 load_url_params.blob_url_loader_factory = params.blob_url_loader_factory;
Robert Ogden011a8082019-01-23 19:04:54458 load_url_params.reload_type = params.reload_type;
alexmos5a98a052016-01-06 00:15:02459
lukasza477a5a22016-06-16 18:28:43460 if (params.uses_post) {
461 load_url_params.load_type = NavigationController::LOAD_TYPE_HTTP_POST;
462 load_url_params.post_data = params.post_data;
alexmos5a98a052016-01-06 00:15:02463 }
464
lukasza04130152016-10-21 20:26:32465 target->GetController().LoadURLWithParams(load_url_params);
466 return target;
alexmos5a98a052016-01-06 00:15:02467}
468
[email protected]e3b10d12014-03-28 16:06:09469void Shell::LoadingStateChanged(WebContents* source,
470 bool to_different_document) {
471 UpdateNavigationControls(to_different_document);
[email protected]15aea412012-01-19 03:18:50472 PlatformSetIsLoading(source->IsLoading());
[email protected]e99ca5112011-09-26 17:22:54473}
474
Dave Tapuska3ed44192018-05-01 18:53:30475void Shell::EnterFullscreenModeForTab(
476 WebContents* web_contents,
477 const GURL& origin,
478 const blink::WebFullscreenOptions& options) {
mlamouri7a78d6fd2015-01-17 13:23:53479 ToggleFullscreenModeForTab(web_contents, true);
480}
481
482void Shell::ExitFullscreenModeForTab(WebContents* web_contents) {
483 ToggleFullscreenModeForTab(web_contents, false);
484}
485
[email protected]99c014c2012-11-27 12:03:42486void Shell::ToggleFullscreenModeForTab(WebContents* web_contents,
487 bool enter_fullscreen) {
488#if defined(OS_ANDROID)
489 PlatformToggleFullscreenModeForTab(web_contents, enter_fullscreen);
490#endif
Kent Tamuracd3ebc42018-05-16 06:44:22491 if (!switches::IsRunWebTestsSwitchPresent())
[email protected]99c014c2012-11-27 12:03:42492 return;
493 if (is_fullscreen_ != enter_fullscreen) {
494 is_fullscreen_ = enter_fullscreen;
Fady Samuel0b911822018-04-25 13:22:16495 web_contents->GetRenderViewHost()
496 ->GetWidget()
497 ->SynchronizeVisualProperties();
[email protected]99c014c2012-11-27 12:03:42498 }
499}
500
501bool Shell::IsFullscreenForTabOrPending(const WebContents* web_contents) const {
502#if defined(OS_ANDROID)
503 return PlatformIsFullscreenForTabOrPending(web_contents);
504#else
505 return is_fullscreen_;
506#endif
507}
508
mikhail.pozdnyakovc0e251b2015-04-15 06:51:12509blink::WebDisplayMode Shell::GetDisplayMode(
510 const WebContents* web_contents) const {
Lukasz Anforowicz52b93722018-06-20 16:11:39511 // TODO: should return blink::WebDisplayModeFullscreen wherever user puts
512 // a browser window into fullscreen (not only in case of renderer-initiated
513 // fullscreen mode): crbug.com/476874.
514 return IsFullscreenForTabOrPending(web_contents)
515 ? blink::kWebDisplayModeFullscreen
516 : blink::kWebDisplayModeBrowser;
mikhail.pozdnyakovc0e251b2015-04-15 06:51:12517}
518
[email protected]f78439002012-11-28 14:45:59519void Shell::RequestToLockMouse(WebContents* web_contents,
520 bool user_gesture,
521 bool last_unlocked_by_target) {
522 web_contents->GotResponseToLockMouseRequest(true);
523}
524
[email protected]9e00e6352012-07-30 17:05:18525void Shell::CloseContents(WebContents* source) {
526 Close();
527}
528
[email protected]067310262012-11-22 14:30:41529bool Shell::CanOverscrollContent() const {
530#if defined(USE_AURA)
531 return true;
532#else
533 return false;
534#endif
535}
536
[email protected]3bbacc5b2012-04-17 17:46:15537void Shell::DidNavigateMainFramePostCommit(WebContents* web_contents) {
[email protected]4e3c7522013-08-13 11:53:18538 PlatformSetAddressBarURL(web_contents->GetLastCommittedURL());
[email protected]e99ca5112011-09-26 17:22:54539}
540
mathiash72a5e462014-11-19 08:18:50541JavaScriptDialogManager* Shell::GetJavaScriptDialogManager(
542 WebContents* source) {
mkwst6efe7f2e62014-10-08 15:09:01543 if (!dialog_manager_) {
Kent Tamuracd3ebc42018-05-16 06:44:22544 dialog_manager_.reset(switches::IsRunWebTestsSwitchPresent()
Kent Tamuraf92649d882018-12-03 09:07:47545 ? new WebTestJavaScriptDialogManager
lukasza381b0492016-03-10 16:48:43546 : new ShellJavaScriptDialogManager);
mkwst6efe7f2e62014-10-08 15:09:01547 }
[email protected]71a88bb2013-02-01 22:05:15548 return dialog_manager_.get();
[email protected]f2210022012-03-29 00:36:08549}
550
dcheng6003e0b2016-04-09 18:42:34551std::unique_ptr<BluetoothChooser> Shell::RunBluetoothChooser(
jyasskin93649a52016-02-17 19:57:02552 RenderFrameHost* frame,
553 const BluetoothChooser::EventHandler& event_handler) {
Lukasz Anforowiczf227b4912017-12-22 23:08:59554 BlinkTestController* blink_test_controller = BlinkTestController::Get();
Kent Tamuracd3ebc42018-05-16 06:44:22555 if (blink_test_controller && switches::IsRunWebTestsSwitchPresent())
Lukasz Anforowiczf227b4912017-12-22 23:08:59556 return blink_test_controller->RunBluetoothChooser(frame, event_handler);
jyasskin8057c002015-09-10 19:09:53557 return nullptr;
558}
559
avia90ae4e2016-11-11 20:49:33560bool Shell::DidAddMessageToConsole(WebContents* source,
561 int32_t level,
562 const base::string16& message,
563 int32_t line_no,
564 const base::string16& source_id) {
Kent Tamuracd3ebc42018-05-16 06:44:22565 return switches::IsRunWebTestsSwitchPresent();
[email protected]efb5f572012-01-29 10:57:33566}
567
Lucas Furukawa Gadani06548fb2019-01-15 05:01:25568void Shell::PortalWebContentsCreated(WebContents* portal_web_contents) {
569 if (switches::IsRunWebTestsSwitchPresent())
570 SecondaryTestWindowObserver::CreateForWebContents(portal_web_contents);
571}
572
Lukasz Anforowicz52b93722018-06-20 16:11:39573void Shell::RendererUnresponsive(
574 WebContents* source,
575 RenderWidgetHost* render_widget_host,
576 base::RepeatingClosure hang_monitor_restarter) {
Lukasz Anforowiczf227b4912017-12-22 23:08:59577 BlinkTestController* blink_test_controller = BlinkTestController::Get();
Kent Tamuracd3ebc42018-05-16 06:44:22578 if (blink_test_controller && switches::IsRunWebTestsSwitchPresent())
Lukasz Anforowiczf227b4912017-12-22 23:08:59579 blink_test_controller->RendererUnresponsive();
[email protected]5bf68f22012-08-31 07:38:10580}
581
[email protected]233567d2013-02-27 20:22:02582void Shell::ActivateContents(WebContents* contents) {
avif9ab5d942015-10-15 14:05:44583 contents->GetRenderViewHost()->GetWidget()->Focus();
[email protected]233567d2013-02-27 20:22:02584}
585
Lucas Furukawa Gadanic5c0cd02018-10-04 20:32:33586std::unique_ptr<WebContents> Shell::SwapWebContents(
587 WebContents* old_contents,
588 std::unique_ptr<WebContents> new_contents,
589 bool did_start_load,
590 bool did_finish_load) {
591 DCHECK_EQ(old_contents, web_contents_.get());
592 new_contents->SetDelegate(this);
593 web_contents_->SetDelegate(nullptr);
594 std::swap(web_contents_, new_contents);
595 PlatformSetContents();
596 PlatformSetAddressBarURL(web_contents_->GetLastCommittedURL());
597 LoadingStateChanged(web_contents_.get(), true);
598 return new_contents;
599}
600
carloskd9d97942017-02-16 08:58:09601bool Shell::ShouldAllowRunningInsecureContent(
602 content::WebContents* web_contents,
603 bool allowed_per_prefs,
604 const url::Origin& origin,
605 const GURL& resource_url) {
606 bool allowed_by_test = false;
Lukasz Anforowiczf227b4912017-12-22 23:08:59607 BlinkTestController* blink_test_controller = BlinkTestController::Get();
Kent Tamuracd3ebc42018-05-16 06:44:22608 if (blink_test_controller && switches::IsRunWebTestsSwitchPresent()) {
carloskd9d97942017-02-16 08:58:09609 const base::DictionaryValue& test_flags =
Kent Tamura679c7c42018-12-05 03:21:10610 blink_test_controller->accumulated_web_test_runtime_flags_changes();
carloskd9d97942017-02-16 08:58:09611 test_flags.GetBoolean("running_insecure_content_allowed", &allowed_by_test);
612 }
613
614 return allowed_per_prefs || allowed_by_test;
615}
616
François Beaufortf09e4462019-01-18 14:34:51617gfx::Size Shell::EnterPictureInPicture(content::WebContents* web_contents,
618 const viz::SurfaceId& surface_id,
Mounir Lamouri11e9ef432018-05-22 03:10:16619 const gfx::Size& natural_size) {
François Beaufort480ee2d2018-10-24 14:05:17620 // During tests, returning a fake window size (same aspect ratio) to pretend
621 // the window was created and allow tests to run accordingly.
622 return switches::IsRunWebTestsSwitchPresent() ? natural_size
Mounir Lamouri11e9ef432018-05-22 03:10:16623 : gfx::Size(0, 0);
624}
625
Bo Liu300c6052018-06-12 04:46:07626bool Shell::ShouldResumeRequestsForCreatedWindow() {
627 return !delay_popup_contents_delegate_for_testing_;
628}
629
pdrcab84ee2015-03-13 21:47:04630gfx::Size Shell::GetShellDefaultSize() {
631 static gfx::Size default_shell_size;
632 if (!default_shell_size.IsEmpty())
633 return default_shell_size;
634 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
635 if (command_line->HasSwitch(switches::kContentShellHostWindowSize)) {
636 const std::string size_str = command_line->GetSwitchValueASCII(
637 switches::kContentShellHostWindowSize);
638 int width, height;
639 CHECK_EQ(2, sscanf(size_str.c_str(), "%dx%d", &width, &height));
640 default_shell_size = gfx::Size(width, height);
641 } else {
642 default_shell_size = gfx::Size(
643 kDefaultTestWindowWidthDip, kDefaultTestWindowHeightDip);
644 }
645 return default_shell_size;
646}
647
Avi Drissman93002212017-09-27 03:20:52648void Shell::TitleWasSet(NavigationEntry* entry) {
[email protected]1ef02d242013-10-07 16:18:53649 if (entry)
650 PlatformSetTitle(entry->GetTitle());
[email protected]aecc085b2012-06-01 18:15:53651}
652
[email protected]7fff43e2013-05-21 20:21:10653void Shell::OnDevToolsWebContentsDestroyed() {
654 devtools_observer_.reset();
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28655 devtools_frontend_ = nullptr;
[email protected]7fff43e2013-05-21 20:21:10656}
657
[email protected]9fbd3f862011-09-20 23:31:34658} // namespace content