blob: 4964305229b28031322fc6165cf433cacc14600f [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"
[email protected]1596efb2013-01-17 22:13:0133#include "content/public/common/renderer_preferences.h"
guoweisc537ba22015-11-06 21:20:3134#include "content/public/common/webrtc_ip_handling_policy.h"
jochen5ce1af92016-02-17 20:39:4535#include "content/shell/browser/layout_test/blink_test_controller.h"
jyasskin8057c002015-09-10 19:09:5336#include "content/shell/browser/layout_test/layout_test_bluetooth_chooser_factory.h"
chenwilliamfed5bda2017-03-24 02:01:0637#include "content/shell/browser/layout_test/layout_test_devtools_bindings.h"
mkwst6efe7f2e62014-10-08 15:09:0138#include "content/shell/browser/layout_test/layout_test_javascript_dialog_manager.h"
lukasza8e550a02016-04-18 18:34:1739#include "content/shell/browser/layout_test/secondary_test_window_observer.h"
[email protected]de7d61ff2013-08-20 11:30:4140#include "content/shell/browser/shell_browser_main_parts.h"
41#include "content/shell/browser/shell_content_browser_client.h"
42#include "content/shell/browser/shell_devtools_frontend.h"
43#include "content/shell/browser/shell_javascript_dialog_manager.h"
carloskd9d97942017-02-16 08:58:0944#include "content/shell/common/layout_test/layout_test_switches.h"
[email protected]b7c504c2013-05-07 14:42:1245#include "content/shell/common/shell_messages.h"
46#include "content/shell/common/shell_switches.h"
Scott Violeta35f9a42018-03-22 22:00:4447#include "media/media_buildflags.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
Wez9a58a152018-06-07 18:59:33125 if (windows_.empty()) {
[email protected]b53adf452014-02-07 12:55:08126 if (headless_)
127 PlatformExit();
Yutaka Hirano2109e582018-02-14 07:24:46128 for (auto it = RenderProcessHost::AllHostsIterator(); !it.IsAtEnd();
129 it.Advance()) {
130 it.GetCurrentValue()->DisableKeepAliveRefCount();
131 }
Wezcbf4a042018-06-13 16:29:12132 if (*g_quit_main_message_loop)
133 std::move(*g_quit_main_message_loop).Run();
[email protected]b53adf452014-02-07 12:55:08134 }
arthursonzognifdd49912017-08-31 08:55:26135
136 web_contents_->SetDelegate(nullptr);
[email protected]9fbd3f862011-09-20 23:31:34137}
138
erikchenbee5c9622018-04-27 19:30:25139Shell* Shell::CreateShell(std::unique_ptr<WebContents> web_contents,
Bo Liu300c6052018-06-12 04:46:07140 const gfx::Size& initial_size,
141 bool should_set_delegate) {
erikchenbee5c9622018-04-27 19:30:25142 WebContents* raw_web_contents = web_contents.get();
Bo Liu300c6052018-06-12 04:46:07143 Shell* shell = new Shell(std::move(web_contents), should_set_delegate);
[email protected]c0d036d2013-05-07 12:50:50144 shell->PlatformCreateWindow(initial_size.width(), initial_size.height());
[email protected]3fd84032012-01-12 18:20:17145
[email protected]5cba3bf2012-01-14 02:40:35146 shell->PlatformSetContents();
[email protected]3fd84032012-01-12 18:20:17147
148 shell->PlatformResizeSubViews();
[email protected]1596efb2013-01-17 22:13:01149
creisb6561df2016-02-11 20:20:54150 // Note: Do not make RenderFrameHost or RenderViewHost specific state changes
151 // here, because they will be forgotten after a cross-process navigation. Use
152 // RenderFrameCreated or RenderViewCreated instead.
Kent Tamuracd3ebc42018-05-16 06:44:22153 if (switches::IsRunWebTestsSwitchPresent()) {
erikchenbee5c9622018-04-27 19:30:25154 raw_web_contents->GetMutableRendererPrefs()->use_custom_colors = false;
155 raw_web_contents->GetRenderViewHost()->SyncRendererPrefs();
[email protected]1596efb2013-01-17 22:13:01156 }
157
lukasza381b0492016-03-10 16:48:43158 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
guoweis4ee48592015-12-02 06:37:07159 if (command_line->HasSwitch(switches::kForceWebRtcIPHandlingPolicy)) {
erikchenbee5c9622018-04-27 19:30:25160 raw_web_contents->GetMutableRendererPrefs()->webrtc_ip_handling_policy =
guoweis4ee48592015-12-02 06:37:07161 command_line->GetSwitchValueASCII(
162 switches::kForceWebRtcIPHandlingPolicy);
guoweis8efb6d892015-10-12 18:26:17163 }
guoweis8efb6d892015-10-12 18:26:17164
[email protected]3fd84032012-01-12 18:20:17165 return shell;
166}
167
[email protected]11a65b692012-03-30 11:29:16168void Shell::CloseAllWindows() {
[email protected]b50452f2014-08-18 12:31:44169 DevToolsAgentHost::DetachAllClients();
[email protected]11a65b692012-03-30 11:29:16170 std::vector<Shell*> open_windows(windows_);
Wezcbf4a042018-06-13 16:29:12171 for (size_t i = 0; i < open_windows.size(); ++i)
172 open_windows[i]->Close();
173
174 // Pump the message loop to allow window teardown tasks to run.
175 base::RunLoop().RunUntilIdle();
176
Wez7d3eb012018-06-20 22:51:28177 // If there were no windows open then the message loop quit closure will
178 // not have been run.
179 if (*g_quit_main_message_loop)
180 std::move(*g_quit_main_message_loop).Run();
181
dmazzoni948fbc72015-05-17 07:57:33182 PlatformExit();
[email protected]11a65b692012-03-30 11:29:16183}
184
Wezcbf4a042018-06-13 16:29:12185void Shell::SetMainMessageLoopQuitClosure(base::OnceClosure quit_closure) {
186 *g_quit_main_message_loop = std::move(quit_closure);
187}
188
Wez7d3eb012018-06-20 22:51:28189void Shell::QuitMainMessageLoopForTesting() {
190 std::move(*g_quit_main_message_loop).Run();
191}
192
[email protected]9e00e6352012-07-30 17:05:18193void Shell::SetShellCreatedCallback(
194 base::Callback<void(Shell*)> shell_created_callback) {
195 DCHECK(shell_created_callback_.is_null());
Tommy Nyquist4b749d02018-03-20 21:46:29196 shell_created_callback_ = std::move(shell_created_callback);
[email protected]9e00e6352012-07-30 17:05:18197}
198
[email protected]74830f02012-01-30 22:27:04199Shell* Shell::FromRenderViewHost(RenderViewHost* rvh) {
200 for (size_t i = 0; i < windows_.size(); ++i) {
[email protected]0b659b32012-03-26 21:29:32201 if (windows_[i]->web_contents() &&
202 windows_[i]->web_contents()->GetRenderViewHost() == rvh) {
[email protected]74830f02012-01-30 22:27:04203 return windows_[i];
204 }
205 }
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28206 return nullptr;
[email protected]74830f02012-01-30 22:27:04207}
208
[email protected]6153b272013-01-25 22:29:23209void Shell::Initialize() {
pdrcab84ee2015-03-13 21:47:04210 PlatformInitialize(GetShellDefaultSize());
[email protected]6153b272013-01-25 22:29:23211}
212
[email protected]a2904092013-10-15 04:53:59213gfx::Size Shell::AdjustWindowSize(const gfx::Size& initial_size) {
214 if (!initial_size.IsEmpty())
215 return initial_size;
pdrcab84ee2015-03-13 21:47:04216 return GetShellDefaultSize();
[email protected]a2904092013-10-15 04:53:59217}
218
[email protected]bdcf9152012-07-19 17:43:21219Shell* Shell::CreateNewWindow(BrowserContext* browser_context,
[email protected]e99ca5112011-09-26 17:22:54220 const GURL& url,
lukasza04130152016-10-21 20:26:32221 const scoped_refptr<SiteInstance>& site_instance,
[email protected]cdb806722013-01-10 14:18:23222 const gfx::Size& initial_size) {
[email protected]54944cde2012-12-09 09:24:59223 WebContents::CreateParams create_params(browser_context, site_instance);
mark a. foltzef394fce2017-10-21 09:11:02224 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
225 switches::kForcePresentationReceiverForTesting)) {
226 create_params.starting_sandbox_flags =
227 blink::kPresentationReceiverSandboxFlags;
228 }
[email protected]a2904092013-10-15 04:53:59229 create_params.initial_size = AdjustWindowSize(initial_size);
erikchenbee5c9622018-04-27 19:30:25230 std::unique_ptr<WebContents> web_contents =
Erik Chenbb8e738e2018-04-28 14:10:43231 WebContents::Create(create_params);
erikchenbee5c9622018-04-27 19:30:25232 Shell* shell =
Bo Liu300c6052018-06-12 04:46:07233 CreateShell(std::move(web_contents), create_params.initial_size,
234 true /* should_set_delegate */);
[email protected]e99ca5112011-09-26 17:22:54235 if (!url.is_empty())
236 shell->LoadURL(url);
[email protected]9fbd3f862011-09-20 23:31:34237 return shell;
238}
239
Daniel Murphy16ed0782018-05-26 00:15:13240Shell* Shell::CreateNewWindowWithSessionStorageNamespace(
241 BrowserContext* browser_context,
242 const GURL& url,
243 const scoped_refptr<SiteInstance>& site_instance,
244 const gfx::Size& initial_size,
245 scoped_refptr<SessionStorageNamespace> session_storage_namespace) {
246 WebContents::CreateParams create_params(browser_context, site_instance);
247 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
248 switches::kForcePresentationReceiverForTesting)) {
249 create_params.starting_sandbox_flags =
250 blink::kPresentationReceiverSandboxFlags;
251 }
252 create_params.initial_size = AdjustWindowSize(initial_size);
253 std::map<std::string, scoped_refptr<SessionStorageNamespace>>
254 session_storages;
255 session_storages[""] = session_storage_namespace;
256 std::unique_ptr<WebContents> web_contents =
257 WebContents::CreateWithSessionStorage(create_params, session_storages);
258 Shell* shell =
Bo Liu300c6052018-06-12 04:46:07259 CreateShell(std::move(web_contents), create_params.initial_size,
260 true /* should_set_delegate */);
Daniel Murphy16ed0782018-05-26 00:15:13261 if (!url.is_empty())
262 shell->LoadURL(url);
263 return shell;
264}
265
[email protected]9fbd3f862011-09-20 23:31:34266void Shell::LoadURL(const GURL& url) {
Alex Moshchuk7e26eca2018-03-03 01:34:29267 LoadURLForFrame(
268 url, std::string(),
269 ui::PageTransitionFromInt(ui::PAGE_TRANSITION_TYPED |
270 ui::PAGE_TRANSITION_FROM_ADDRESS_BAR));
[email protected]d2494ff2013-02-20 08:22:37271}
272
Alex Moshchuk7e26eca2018-03-03 01:34:29273void Shell::LoadURLForFrame(const GURL& url,
274 const std::string& frame_name,
275 ui::PageTransition transition_type) {
[email protected]d2494ff2013-02-20 08:22:37276 NavigationController::LoadURLParams params(url);
[email protected]d2494ff2013-02-20 08:22:37277 params.frame_name = frame_name;
Alex Moshchuk7e26eca2018-03-03 01:34:29278 params.transition_type = transition_type;
[email protected]d2494ff2013-02-20 08:22:37279 web_contents_->GetController().LoadURLWithParams(params);
[email protected]fc2b46b2014-05-03 16:33:45280 web_contents_->Focus();
[email protected]9fbd3f862011-09-20 23:31:34281}
282
[email protected]76bdecb2014-04-16 17:58:08283void Shell::LoadDataWithBaseURL(const GURL& url, const std::string& data,
284 const GURL& base_url) {
boliuec93ea92016-02-17 22:23:07285 bool load_as_string = false;
286 LoadDataWithBaseURLInternal(url, data, base_url, load_as_string);
287}
288
289#if defined(OS_ANDROID)
290void Shell::LoadDataAsStringWithBaseURL(const GURL& url,
291 const std::string& data,
292 const GURL& base_url) {
293 bool load_as_string = true;
294 LoadDataWithBaseURLInternal(url, data, base_url, load_as_string);
295}
296#endif
297
298void Shell::LoadDataWithBaseURLInternal(const GURL& url,
299 const std::string& data,
300 const GURL& base_url,
301 bool load_as_string) {
302#if !defined(OS_ANDROID)
303 DCHECK(!load_as_string); // Only supported on Android.
304#endif
305
306 NavigationController::LoadURLParams params(GURL::EmptyGURL());
307 const std::string data_url_header = "data:text/html;charset=utf-8,";
308 if (load_as_string) {
309 params.url = GURL(data_url_header);
310 std::string data_url_as_string = data_url_header + data;
311#if defined(OS_ANDROID)
312 params.data_url_as_string =
313 base::RefCountedString::TakeString(&data_url_as_string);
314#endif
315 } else {
316 params.url = GURL(data_url_header + data);
317 }
318
[email protected]76bdecb2014-04-16 17:58:08319 params.load_type = NavigationController::LOAD_TYPE_DATA;
320 params.base_url_for_data_url = base_url;
321 params.virtual_url_for_data_url = url;
322 params.override_user_agent = NavigationController::UA_OVERRIDE_FALSE;
323 web_contents_->GetController().LoadURLWithParams(params);
[email protected]fc2b46b2014-05-03 16:33:45324 web_contents_->Focus();
[email protected]76bdecb2014-04-16 17:58:08325}
326
[email protected]a2904092013-10-15 04:53:59327void Shell::AddNewContents(WebContents* source,
erikchenbee5c9622018-04-27 19:30:25328 std::unique_ptr<WebContents> new_contents,
[email protected]a2904092013-10-15 04:53:59329 WindowOpenDisposition disposition,
bokan107a47f2015-02-03 23:23:39330 const gfx::Rect& initial_rect,
[email protected]a2904092013-10-15 04:53:59331 bool user_gesture,
332 bool* was_blocked) {
erikchenbee5c9622018-04-27 19:30:25333 WebContents* raw_new_contents = new_contents.get();
Bo Liu300c6052018-06-12 04:46:07334 CreateShell(
335 std::move(new_contents), AdjustWindowSize(initial_rect.size()),
336 !delay_popup_contents_delegate_for_testing_ /* should_set_delegate */);
Kent Tamuracd3ebc42018-05-16 06:44:22337 if (switches::IsRunWebTestsSwitchPresent())
erikchenbee5c9622018-04-27 19:30:25338 SecondaryTestWindowObserver::CreateForWebContents(raw_new_contents);
[email protected]a2904092013-10-15 04:53:59339}
340
[email protected]9fbd3f862011-09-20 23:31:34341void Shell::GoBackOrForward(int offset) {
[email protected]0b659b32012-03-26 21:29:32342 web_contents_->GetController().GoToOffset(offset);
[email protected]fc2b46b2014-05-03 16:33:45343 web_contents_->Focus();
[email protected]9fbd3f862011-09-20 23:31:34344}
345
346void Shell::Reload() {
toyoshim6142d96f2016-12-19 09:07:25347 web_contents_->GetController().Reload(ReloadType::NORMAL, false);
[email protected]fc2b46b2014-05-03 16:33:45348 web_contents_->Focus();
[email protected]9fbd3f862011-09-20 23:31:34349}
350
toyoshime5aaf6a2016-05-18 08:07:48351void Shell::ReloadBypassingCache() {
toyoshim6142d96f2016-12-19 09:07:25352 web_contents_->GetController().Reload(ReloadType::BYPASSING_CACHE, false);
toyoshime5aaf6a2016-05-18 08:07:48353 web_contents_->Focus();
354}
355
[email protected]9fbd3f862011-09-20 23:31:34356void Shell::Stop() {
[email protected]0b659b32012-03-26 21:29:32357 web_contents_->Stop();
[email protected]fc2b46b2014-05-03 16:33:45358 web_contents_->Focus();
[email protected]9fbd3f862011-09-20 23:31:34359}
360
[email protected]e3b10d12014-03-28 16:06:09361void Shell::UpdateNavigationControls(bool to_different_document) {
[email protected]0b659b32012-03-26 21:29:32362 int current_index = web_contents_->GetController().GetCurrentEntryIndex();
363 int max_index = web_contents_->GetController().GetEntryCount() - 1;
[email protected]9fbd3f862011-09-20 23:31:34364
365 PlatformEnableUIControl(BACK_BUTTON, current_index > 0);
366 PlatformEnableUIControl(FORWARD_BUTTON, current_index < max_index);
[email protected]e3b10d12014-03-28 16:06:09367 PlatformEnableUIControl(STOP_BUTTON,
368 to_different_document && web_contents_->IsLoading());
[email protected]9fbd3f862011-09-20 23:31:34369}
370
[email protected]7c17b6992012-08-09 16:16:30371void Shell::ShowDevTools() {
mohsen6eb57fb2016-07-22 03:14:08372 if (!devtools_frontend_) {
373 devtools_frontend_ = ShellDevToolsFrontend::Show(web_contents());
374 devtools_observer_.reset(new DevToolsWebContentsObserver(
375 this, devtools_frontend_->frontend_shell()->web_contents()));
376 }
[email protected]3142e5d2014-02-07 00:54:46377
mohsen6eb57fb2016-07-22 03:14:08378 devtools_frontend_->Activate();
379 devtools_frontend_->Focus();
[email protected]7c17b6992012-08-09 16:16:30380}
381
[email protected]001841c92012-12-11 17:00:13382void Shell::CloseDevTools() {
[email protected]0773e0c2013-01-25 15:57:57383 if (!devtools_frontend_)
[email protected]001841c92012-12-11 17:00:13384 return;
[email protected]7fff43e2013-05-21 20:21:10385 devtools_observer_.reset();
[email protected]0773e0c2013-01-25 15:57:57386 devtools_frontend_->Close();
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28387 devtools_frontend_ = nullptr;
[email protected]001841c92012-12-11 17:00:13388}
389
[email protected]9fbd3f862011-09-20 23:31:34390gfx::NativeView Shell::GetContentView() {
[email protected]59383c782013-04-17 16:43:27391 if (!web_contents_)
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28392 return nullptr;
[email protected]fc2b46b2014-05-03 16:33:45393 return web_contents_->GetNativeView();
[email protected]9fbd3f862011-09-20 23:31:34394}
395
alexmos5a98a052016-01-06 00:15:02396WebContents* Shell::OpenURLFromTab(WebContents* source,
397 const OpenURLParams& params) {
lukasza04130152016-10-21 20:26:32398 WebContents* target = nullptr;
399 switch (params.disposition) {
400 case WindowOpenDisposition::CURRENT_TAB:
401 target = source;
402 break;
403
404 // Normally, the difference between NEW_POPUP and NEW_WINDOW is that a popup
405 // should have no toolbar, no status bar, no menu bar, no scrollbars and be
406 // not resizable. For simplicity and to enable new testing scenarios in
407 // content shell and layout tests, popups don't get special treatment below
408 // (i.e. they will have a toolbar and other things described here).
409 case WindowOpenDisposition::NEW_POPUP:
lukaszabe2f0da2017-04-25 00:43:00410 case WindowOpenDisposition::NEW_WINDOW:
411 // content_shell doesn't really support tabs, but some layout tests use
412 // middle click (which translates into kNavigationPolicyNewBackgroundTab),
413 // so we treat the cases below just like a NEW_WINDOW disposition.
414 case WindowOpenDisposition::NEW_BACKGROUND_TAB:
415 case WindowOpenDisposition::NEW_FOREGROUND_TAB: {
lukasza04130152016-10-21 20:26:32416 Shell* new_window =
417 Shell::CreateNewWindow(source->GetBrowserContext(),
418 GURL(), // Don't load anything just yet.
419 params.source_site_instance,
420 gfx::Size()); // Use default size.
421 target = new_window->web_contents();
Kent Tamuracd3ebc42018-05-16 06:44:22422 if (switches::IsRunWebTestsSwitchPresent())
lukasza04130152016-10-21 20:26:32423 SecondaryTestWindowObserver::CreateForWebContents(target);
424 break;
425 }
426
427 // No tabs in content_shell:
428 case WindowOpenDisposition::SINGLETON_TAB:
lukasza04130152016-10-21 20:26:32429 // No incognito mode in content_shell:
430 case WindowOpenDisposition::OFF_THE_RECORD:
431 // TODO(lukasza): Investigate if some layout tests might need support for
432 // SAVE_TO_DISK disposition. This would probably require that
433 // BlinkTestController always sets up and cleans up a temporary directory
434 // as the default downloads destinations for the duration of a test.
435 case WindowOpenDisposition::SAVE_TO_DISK:
436 // Ignoring requests with disposition == IGNORE_ACTION...
437 case WindowOpenDisposition::IGNORE_ACTION:
438 default:
439 return nullptr;
440 }
alexmos5a98a052016-01-06 00:15:02441
442 NavigationController::LoadURLParams load_url_params(params.url);
443 load_url_params.source_site_instance = params.source_site_instance;
444 load_url_params.transition_type = params.transition;
445 load_url_params.frame_tree_node_id = params.frame_tree_node_id;
446 load_url_params.referrer = params.referrer;
447 load_url_params.redirect_chain = params.redirect_chain;
448 load_url_params.extra_headers = params.extra_headers;
449 load_url_params.is_renderer_initiated = params.is_renderer_initiated;
alexmos5a98a052016-01-06 00:15:02450 load_url_params.should_replace_current_entry =
451 params.should_replace_current_entry;
Marijn Kruisselbrink7a0d5e182018-05-24 22:55:09452 load_url_params.blob_url_loader_factory = params.blob_url_loader_factory;
alexmos5a98a052016-01-06 00:15:02453
lukasza477a5a22016-06-16 18:28:43454 if (params.uses_post) {
455 load_url_params.load_type = NavigationController::LOAD_TYPE_HTTP_POST;
456 load_url_params.post_data = params.post_data;
alexmos5a98a052016-01-06 00:15:02457 }
458
lukasza04130152016-10-21 20:26:32459 target->GetController().LoadURLWithParams(load_url_params);
460 return target;
alexmos5a98a052016-01-06 00:15:02461}
462
[email protected]e3b10d12014-03-28 16:06:09463void Shell::LoadingStateChanged(WebContents* source,
464 bool to_different_document) {
465 UpdateNavigationControls(to_different_document);
[email protected]15aea412012-01-19 03:18:50466 PlatformSetIsLoading(source->IsLoading());
[email protected]e99ca5112011-09-26 17:22:54467}
468
Dave Tapuska3ed44192018-05-01 18:53:30469void Shell::EnterFullscreenModeForTab(
470 WebContents* web_contents,
471 const GURL& origin,
472 const blink::WebFullscreenOptions& options) {
mlamouri7a78d6fd2015-01-17 13:23:53473 ToggleFullscreenModeForTab(web_contents, true);
474}
475
476void Shell::ExitFullscreenModeForTab(WebContents* web_contents) {
477 ToggleFullscreenModeForTab(web_contents, false);
478}
479
[email protected]99c014c2012-11-27 12:03:42480void Shell::ToggleFullscreenModeForTab(WebContents* web_contents,
481 bool enter_fullscreen) {
482#if defined(OS_ANDROID)
483 PlatformToggleFullscreenModeForTab(web_contents, enter_fullscreen);
484#endif
Kent Tamuracd3ebc42018-05-16 06:44:22485 if (!switches::IsRunWebTestsSwitchPresent())
[email protected]99c014c2012-11-27 12:03:42486 return;
487 if (is_fullscreen_ != enter_fullscreen) {
488 is_fullscreen_ = enter_fullscreen;
Fady Samuel0b911822018-04-25 13:22:16489 web_contents->GetRenderViewHost()
490 ->GetWidget()
491 ->SynchronizeVisualProperties();
[email protected]99c014c2012-11-27 12:03:42492 }
493}
494
495bool Shell::IsFullscreenForTabOrPending(const WebContents* web_contents) const {
496#if defined(OS_ANDROID)
497 return PlatformIsFullscreenForTabOrPending(web_contents);
498#else
499 return is_fullscreen_;
500#endif
501}
502
mikhail.pozdnyakovc0e251b2015-04-15 06:51:12503blink::WebDisplayMode Shell::GetDisplayMode(
504 const WebContents* web_contents) const {
Lukasz Anforowicz52b93722018-06-20 16:11:39505 // TODO: should return blink::WebDisplayModeFullscreen wherever user puts
506 // a browser window into fullscreen (not only in case of renderer-initiated
507 // fullscreen mode): crbug.com/476874.
508 return IsFullscreenForTabOrPending(web_contents)
509 ? blink::kWebDisplayModeFullscreen
510 : blink::kWebDisplayModeBrowser;
mikhail.pozdnyakovc0e251b2015-04-15 06:51:12511}
512
[email protected]f78439002012-11-28 14:45:59513void Shell::RequestToLockMouse(WebContents* web_contents,
514 bool user_gesture,
515 bool last_unlocked_by_target) {
516 web_contents->GotResponseToLockMouseRequest(true);
517}
518
[email protected]9e00e6352012-07-30 17:05:18519void Shell::CloseContents(WebContents* source) {
520 Close();
521}
522
[email protected]067310262012-11-22 14:30:41523bool Shell::CanOverscrollContent() const {
524#if defined(USE_AURA)
525 return true;
526#else
527 return false;
528#endif
529}
530
[email protected]3bbacc5b2012-04-17 17:46:15531void Shell::DidNavigateMainFramePostCommit(WebContents* web_contents) {
[email protected]4e3c7522013-08-13 11:53:18532 PlatformSetAddressBarURL(web_contents->GetLastCommittedURL());
[email protected]e99ca5112011-09-26 17:22:54533}
534
mathiash72a5e462014-11-19 08:18:50535JavaScriptDialogManager* Shell::GetJavaScriptDialogManager(
536 WebContents* source) {
mkwst6efe7f2e62014-10-08 15:09:01537 if (!dialog_manager_) {
Kent Tamuracd3ebc42018-05-16 06:44:22538 dialog_manager_.reset(switches::IsRunWebTestsSwitchPresent()
lukasza381b0492016-03-10 16:48:43539 ? new LayoutTestJavaScriptDialogManager
540 : new ShellJavaScriptDialogManager);
mkwst6efe7f2e62014-10-08 15:09:01541 }
[email protected]71a88bb2013-02-01 22:05:15542 return dialog_manager_.get();
[email protected]f2210022012-03-29 00:36:08543}
544
dcheng6003e0b2016-04-09 18:42:34545std::unique_ptr<BluetoothChooser> Shell::RunBluetoothChooser(
jyasskin93649a52016-02-17 19:57:02546 RenderFrameHost* frame,
547 const BluetoothChooser::EventHandler& event_handler) {
Lukasz Anforowiczf227b4912017-12-22 23:08:59548 BlinkTestController* blink_test_controller = BlinkTestController::Get();
Kent Tamuracd3ebc42018-05-16 06:44:22549 if (blink_test_controller && switches::IsRunWebTestsSwitchPresent())
Lukasz Anforowiczf227b4912017-12-22 23:08:59550 return blink_test_controller->RunBluetoothChooser(frame, event_handler);
jyasskin8057c002015-09-10 19:09:53551 return nullptr;
552}
553
avia90ae4e2016-11-11 20:49:33554bool Shell::DidAddMessageToConsole(WebContents* source,
555 int32_t level,
556 const base::string16& message,
557 int32_t line_no,
558 const base::string16& source_id) {
Kent Tamuracd3ebc42018-05-16 06:44:22559 return switches::IsRunWebTestsSwitchPresent();
[email protected]efb5f572012-01-29 10:57:33560}
561
Lukasz Anforowicz52b93722018-06-20 16:11:39562void Shell::RendererUnresponsive(
563 WebContents* source,
564 RenderWidgetHost* render_widget_host,
565 base::RepeatingClosure hang_monitor_restarter) {
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())
Lukasz Anforowiczf227b4912017-12-22 23:08:59568 blink_test_controller->RendererUnresponsive();
[email protected]5bf68f22012-08-31 07:38:10569}
570
[email protected]233567d2013-02-27 20:22:02571void Shell::ActivateContents(WebContents* contents) {
avif9ab5d942015-10-15 14:05:44572 contents->GetRenderViewHost()->GetWidget()->Focus();
[email protected]233567d2013-02-27 20:22:02573}
574
carloskd9d97942017-02-16 08:58:09575bool Shell::ShouldAllowRunningInsecureContent(
576 content::WebContents* web_contents,
577 bool allowed_per_prefs,
578 const url::Origin& origin,
579 const GURL& resource_url) {
580 bool allowed_by_test = false;
Lukasz Anforowiczf227b4912017-12-22 23:08:59581 BlinkTestController* blink_test_controller = BlinkTestController::Get();
Kent Tamuracd3ebc42018-05-16 06:44:22582 if (blink_test_controller && switches::IsRunWebTestsSwitchPresent()) {
carloskd9d97942017-02-16 08:58:09583 const base::DictionaryValue& test_flags =
Lukasz Anforowiczf227b4912017-12-22 23:08:59584 blink_test_controller->accumulated_layout_test_runtime_flags_changes();
carloskd9d97942017-02-16 08:58:09585 test_flags.GetBoolean("running_insecure_content_allowed", &allowed_by_test);
586 }
587
588 return allowed_per_prefs || allowed_by_test;
589}
590
Mounir Lamouri11e9ef432018-05-22 03:10:16591gfx::Size Shell::EnterPictureInPicture(const viz::SurfaceId& surface_id,
592 const gfx::Size& natural_size) {
593 // During tests, returning a fake window size to pretent the window was
594 // created and allow tests to run accordingly.
595 return switches::IsRunWebTestsSwitchPresent() ? gfx::Size(42, 42)
596 : gfx::Size(0, 0);
597}
598
Bo Liu300c6052018-06-12 04:46:07599bool Shell::ShouldResumeRequestsForCreatedWindow() {
600 return !delay_popup_contents_delegate_for_testing_;
601}
602
pdrcab84ee2015-03-13 21:47:04603gfx::Size Shell::GetShellDefaultSize() {
604 static gfx::Size default_shell_size;
605 if (!default_shell_size.IsEmpty())
606 return default_shell_size;
607 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
608 if (command_line->HasSwitch(switches::kContentShellHostWindowSize)) {
609 const std::string size_str = command_line->GetSwitchValueASCII(
610 switches::kContentShellHostWindowSize);
611 int width, height;
612 CHECK_EQ(2, sscanf(size_str.c_str(), "%dx%d", &width, &height));
613 default_shell_size = gfx::Size(width, height);
614 } else {
615 default_shell_size = gfx::Size(
616 kDefaultTestWindowWidthDip, kDefaultTestWindowHeightDip);
617 }
618 return default_shell_size;
619}
620
Avi Drissman93002212017-09-27 03:20:52621void Shell::TitleWasSet(NavigationEntry* entry) {
[email protected]1ef02d242013-10-07 16:18:53622 if (entry)
623 PlatformSetTitle(entry->GetTitle());
[email protected]aecc085b2012-06-01 18:15:53624}
625
[email protected]7fff43e2013-05-21 20:21:10626void Shell::OnDevToolsWebContentsDestroyed() {
627 devtools_observer_.reset();
Ivan Kotenkov2c0d2bb32017-11-01 15:41:28628 devtools_frontend_ = nullptr;
[email protected]7fff43e2013-05-21 20:21:10629}
630
[email protected]9fbd3f862011-09-20 23:31:34631} // namespace content