[email protected] | de7d61ff | 2013-08-20 11:30:41 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors. All rights reserved. |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | de7d61ff | 2013-08-20 11:30:41 | [diff] [blame] | 5 | #include "content/shell/browser/shell.h" |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 6 | |
avi | 66a0772 | 2015-12-25 23:38:12 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | |
Lukasz Anforowicz | 52b9372 | 2018-06-20 16:11:39 | [diff] [blame] | 9 | #include <map> |
Peter Boström | dd7e40ec | 2021-04-05 20:40:10 | [diff] [blame] | 10 | #include <memory> |
Lukasz Anforowicz | 52b9372 | 2018-06-20 16:11:39 | [diff] [blame] | 11 | #include <string> |
| 12 | #include <utility> |
| 13 | |
[email protected] | efb5f57 | 2012-01-29 10:57:33 | [diff] [blame] | 14 | #include "base/command_line.h" |
skyostil | 95082a6 | 2015-06-05 19:53:07 | [diff] [blame] | 15 | #include "base/location.h" |
Wez | 9a58a15 | 2018-06-07 18:59:33 | [diff] [blame] | 16 | #include "base/no_destructor.h" |
fdoray | 896bea1 | 2016-06-10 15:52:01 | [diff] [blame] | 17 | #include "base/run_loop.h" |
[email protected] | 21aa9968 | 2013-06-11 07:17:01 | [diff] [blame] | 18 | #include "base/strings/string_number_conversions.h" |
| 19 | #include "base/strings/string_util.h" |
[email protected] | 74ebfb1 | 2013-06-07 20:48:00 | [diff] [blame] | 20 | #include "base/strings/utf_string_conversions.h" |
Patrick Monette | 643cdf6 | 2021-10-15 19:13:42 | [diff] [blame] | 21 | #include "base/task/single_thread_task_runner.h" |
gab | 30f26df | 2016-05-11 19:37:55 | [diff] [blame] | 22 | #include "base/threading/thread_task_runner_handle.h" |
avi | 66a0772 | 2015-12-25 23:38:12 | [diff] [blame] | 23 | #include "build/build_config.h" |
Javier Fernández García-Boente | 13150a04 | 2022-04-04 21:08:22 | [diff] [blame^] | 24 | #include "components/custom_handlers/protocol_handler.h" |
| 25 | #include "components/custom_handlers/protocol_handler_registry.h" |
| 26 | #include "components/custom_handlers/simple_protocol_handler_registry_factory.h" |
| 27 | #include "content/public/browser/browser_context.h" |
[email protected] | b50452f | 2014-08-18 12:31:44 | [diff] [blame] | 28 | #include "content/public/browser/devtools_agent_host.h" |
[email protected] | 0b659b3 | 2012-03-26 21:29:32 | [diff] [blame] | 29 | #include "content/public/browser/navigation_controller.h" |
[email protected] | b7c504c | 2013-05-07 14:42:12 | [diff] [blame] | 30 | #include "content/public/browser/navigation_entry.h" |
Becca Hughes | 112832e | 2019-06-11 17:19:02 | [diff] [blame] | 31 | #include "content/public/browser/picture_in_picture_window_controller.h" |
arthursonzogni | b93a447 | 2020-04-10 07:38:00 | [diff] [blame] | 32 | #include "content/public/browser/presentation_receiver_flags.h" |
Yutaka Hirano | 2109e58 | 2018-02-14 07:24:46 | [diff] [blame] | 33 | #include "content/public/browser/render_process_host.h" |
[email protected] | 0b659b3 | 2012-03-26 21:29:32 | [diff] [blame] | 34 | #include "content/public/browser/render_view_host.h" |
avi | f9ab5d94 | 2015-10-15 14:05:44 | [diff] [blame] | 35 | #include "content/public/browser/render_widget_host.h" |
Xianzhu Wang | 6be66b01 | 2020-05-06 17:17:25 | [diff] [blame] | 36 | #include "content/public/browser/renderer_preferences_util.h" |
[email protected] | 0b659b3 | 2012-03-26 21:29:32 | [diff] [blame] | 37 | #include "content/public/browser/web_contents.h" |
guoweis | 8efb6d89 | 2015-10-12 18:26:17 | [diff] [blame] | 38 | #include "content/public/common/content_switches.h" |
danakj | de3e2a0 | 2020-05-12 16:51:20 | [diff] [blame] | 39 | #include "content/shell/app/resource.h" |
[email protected] | de7d61ff | 2013-08-20 11:30:41 | [diff] [blame] | 40 | #include "content/shell/browser/shell_content_browser_client.h" |
| 41 | #include "content/shell/browser/shell_devtools_frontend.h" |
| 42 | #include "content/shell/browser/shell_javascript_dialog_manager.h" |
[email protected] | b7c504c | 2013-05-07 14:42:12 | [diff] [blame] | 43 | #include "content/shell/common/shell_switches.h" |
Scott Violet | a35f9a4 | 2018-03-22 22:00:44 | [diff] [blame] | 44 | #include "media/media_buildflags.h" |
Antonio Gomes | b5bf548f | 2019-09-12 17:40:15 | [diff] [blame] | 45 | #include "third_party/blink/public/common/peerconnection/webrtc_ip_handling_policy.h" |
Mario Sanchez Prada | 0bd8b8c | 2020-10-21 17:49:23 | [diff] [blame] | 46 | #include "third_party/blink/public/common/renderer_preferences/renderer_preferences.h" |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 47 | |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 48 | namespace content { |
| 49 | |
arthursonzogni | 75ede19 | 2021-07-06 14:45:46 | [diff] [blame] | 50 | namespace { |
Wez | cbf4a04 | 2018-06-13 16:29:12 | [diff] [blame] | 51 | // Null until/unless the default main message loop is running. |
| 52 | base::NoDestructor<base::OnceClosure> g_quit_main_message_loop; |
Wez | 9a58a15 | 2018-06-07 18:59:33 | [diff] [blame] | 53 | |
pdr | cab84ee | 2015-03-13 21:47:04 | [diff] [blame] | 54 | const int kDefaultTestWindowWidthDip = 800; |
| 55 | const int kDefaultTestWindowHeightDip = 600; |
[email protected] | 1e57cab | 2013-05-28 04:26:11 | [diff] [blame] | 56 | |
arthursonzogni | 75ede19 | 2021-07-06 14:45:46 | [diff] [blame] | 57 | // Owning pointer. We can not use unique_ptr as a global. That introduces a |
| 58 | // static constructor/destructor. |
| 59 | // Acquired in Shell::Init(), released in Shell::Shutdown(). |
| 60 | ShellPlatformDelegate* g_platform; |
| 61 | } // namespace |
| 62 | |
[email protected] | e99ca511 | 2011-09-26 17:22:54 | [diff] [blame] | 63 | std::vector<Shell*> Shell::windows_; |
danakj | a9fe91c | 2019-05-01 19:02:29 | [diff] [blame] | 64 | base::OnceCallback<void(Shell*)> Shell::shell_created_callback_; |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 65 | |
Bo Liu | 300c605 | 2018-06-12 04:46:07 | [diff] [blame] | 66 | Shell::Shell(std::unique_ptr<WebContents> web_contents, |
| 67 | bool should_set_delegate) |
erikchen | bee5c962 | 2018-04-27 19:30:25 | [diff] [blame] | 68 | : WebContentsObserver(web_contents.get()), |
danakj | de3e2a0 | 2020-05-12 16:51:20 | [diff] [blame] | 69 | web_contents_(std::move(web_contents)) { |
Bo Liu | 300c605 | 2018-06-12 04:46:07 | [diff] [blame] | 70 | if (should_set_delegate) |
| 71 | web_contents_->SetDelegate(this); |
arthursonzogni | fdd4991 | 2017-08-31 08:55:26 | [diff] [blame] | 72 | |
danakj | d4b48df5 | 2020-07-02 18:16:48 | [diff] [blame] | 73 | if (!switches::IsRunWebTestsSwitchPresent()) { |
Xianzhu Wang | 6be66b01 | 2020-05-06 17:17:25 | [diff] [blame] | 74 | UpdateFontRendererPreferencesFromSystemSettings( |
| 75 | web_contents_->GetMutableRendererPrefs()); |
Francois Doray | e616115 | 2018-03-27 22:05:37 | [diff] [blame] | 76 | } |
Pavel Feldman | c7cd063c | 2017-10-06 20:04:28 | [diff] [blame] | 77 | |
[email protected] | 9e00e635 | 2012-07-30 17:05:18 | [diff] [blame] | 78 | windows_.push_back(this); |
| 79 | |
danakj | a9fe91c | 2019-05-01 19:02:29 | [diff] [blame] | 80 | if (shell_created_callback_) |
| 81 | std::move(shell_created_callback_).Run(this); |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | Shell::~Shell() { |
danakj | de3e2a0 | 2020-05-12 16:51:20 | [diff] [blame] | 85 | g_platform->CleanUp(this); |
[email protected] | e99ca511 | 2011-09-26 17:22:54 | [diff] [blame] | 86 | |
| 87 | for (size_t i = 0; i < windows_.size(); ++i) { |
| 88 | if (windows_[i] == this) { |
| 89 | windows_.erase(windows_.begin() + i); |
| 90 | break; |
| 91 | } |
| 92 | } |
[email protected] | 11a65b69 | 2012-03-30 11:29:16 | [diff] [blame] | 93 | |
Sergey Ulanov | f0875d1 | 2019-01-03 20:33:23 | [diff] [blame] | 94 | web_contents_->SetDelegate(nullptr); |
| 95 | web_contents_.reset(); |
Arthur Sonzogni | 9e72df3f | 2021-07-02 14:32:17 | [diff] [blame] | 96 | |
arthursonzogni | 75ede19 | 2021-07-06 14:45:46 | [diff] [blame] | 97 | if (windows().empty()) |
| 98 | g_platform->DidCloseLastWindow(); |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 99 | } |
| 100 | |
erikchen | bee5c962 | 2018-04-27 19:30:25 | [diff] [blame] | 101 | Shell* Shell::CreateShell(std::unique_ptr<WebContents> web_contents, |
Bo Liu | 300c605 | 2018-06-12 04:46:07 | [diff] [blame] | 102 | const gfx::Size& initial_size, |
| 103 | bool should_set_delegate) { |
erikchen | bee5c962 | 2018-04-27 19:30:25 | [diff] [blame] | 104 | WebContents* raw_web_contents = web_contents.get(); |
Bo Liu | 300c605 | 2018-06-12 04:46:07 | [diff] [blame] | 105 | Shell* shell = new Shell(std::move(web_contents), should_set_delegate); |
danakj | de3e2a0 | 2020-05-12 16:51:20 | [diff] [blame] | 106 | g_platform->CreatePlatformWindow(shell, initial_size); |
[email protected] | 1596efb | 2013-01-17 22:13:01 | [diff] [blame] | 107 | |
creis | b6561df | 2016-02-11 20:20:54 | [diff] [blame] | 108 | // Note: Do not make RenderFrameHost or RenderViewHost specific state changes |
| 109 | // here, because they will be forgotten after a cross-process navigation. Use |
| 110 | // RenderFrameCreated or RenderViewCreated instead. |
Kent Tamura | cd3ebc4 | 2018-05-16 06:44:22 | [diff] [blame] | 111 | if (switches::IsRunWebTestsSwitchPresent()) { |
erikchen | bee5c962 | 2018-04-27 19:30:25 | [diff] [blame] | 112 | raw_web_contents->GetMutableRendererPrefs()->use_custom_colors = false; |
Bruce Long | 1e3e1f54 | 2019-10-16 17:56:28 | [diff] [blame] | 113 | raw_web_contents->SyncRendererPrefs(); |
[email protected] | 1596efb | 2013-01-17 22:13:01 | [diff] [blame] | 114 | } |
| 115 | |
lukasza | 381b049 | 2016-03-10 16:48:43 | [diff] [blame] | 116 | base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
guoweis | 4ee4859 | 2015-12-02 06:37:07 | [diff] [blame] | 117 | if (command_line->HasSwitch(switches::kForceWebRtcIPHandlingPolicy)) { |
erikchen | bee5c962 | 2018-04-27 19:30:25 | [diff] [blame] | 118 | raw_web_contents->GetMutableRendererPrefs()->webrtc_ip_handling_policy = |
guoweis | 4ee4859 | 2015-12-02 06:37:07 | [diff] [blame] | 119 | command_line->GetSwitchValueASCII( |
| 120 | switches::kForceWebRtcIPHandlingPolicy); |
guoweis | 8efb6d89 | 2015-10-12 18:26:17 | [diff] [blame] | 121 | } |
guoweis | 8efb6d89 | 2015-10-12 18:26:17 | [diff] [blame] | 122 | |
danakj | 8de3b749 | 2020-07-02 22:41:42 | [diff] [blame] | 123 | g_platform->SetContents(shell); |
| 124 | g_platform->DidCreateOrAttachWebContents(shell, raw_web_contents); |
danakj | 3dd7a610 | 2020-12-30 19:58:39 | [diff] [blame] | 125 | // If the RenderFrame was created during WebContents construction (as happens |
| 126 | // for windows opened from the renderer) then the Shell won't hear about the |
| 127 | // main frame being created as a WebContentsObservers. This gives the delegate |
| 128 | // a chance to act on the main frame accordingly. |
| 129 | if (raw_web_contents->GetMainFrame()->IsRenderFrameCreated()) |
| 130 | g_platform->MainFrameCreated(shell); |
danakj | 7da83393 | 2020-06-23 21:49:40 | [diff] [blame] | 131 | |
[email protected] | 3fd8403 | 2012-01-12 18:20:17 | [diff] [blame] | 132 | return shell; |
| 133 | } |
| 134 | |
arthursonzogni | 75ede19 | 2021-07-06 14:45:46 | [diff] [blame] | 135 | // static |
Wez | cbf4a04 | 2018-06-13 16:29:12 | [diff] [blame] | 136 | void Shell::SetMainMessageLoopQuitClosure(base::OnceClosure quit_closure) { |
| 137 | *g_quit_main_message_loop = std::move(quit_closure); |
| 138 | } |
| 139 | |
arthursonzogni | 75ede19 | 2021-07-06 14:45:46 | [diff] [blame] | 140 | // static |
Wez | 7d3eb01 | 2018-06-20 22:51:28 | [diff] [blame] | 141 | void Shell::QuitMainMessageLoopForTesting() { |
Wez | c93f15a9 | 2020-02-25 21:57:44 | [diff] [blame] | 142 | if (*g_quit_main_message_loop) |
| 143 | std::move(*g_quit_main_message_loop).Run(); |
Wez | 7d3eb01 | 2018-06-20 22:51:28 | [diff] [blame] | 144 | } |
| 145 | |
arthursonzogni | 75ede19 | 2021-07-06 14:45:46 | [diff] [blame] | 146 | // static |
[email protected] | 9e00e635 | 2012-07-30 17:05:18 | [diff] [blame] | 147 | void Shell::SetShellCreatedCallback( |
danakj | a9fe91c | 2019-05-01 19:02:29 | [diff] [blame] | 148 | base::OnceCallback<void(Shell*)> shell_created_callback) { |
| 149 | DCHECK(!shell_created_callback_); |
Tommy Nyquist | 4b749d0 | 2018-03-20 21:46:29 | [diff] [blame] | 150 | shell_created_callback_ = std::move(shell_created_callback); |
[email protected] | 9e00e635 | 2012-07-30 17:05:18 | [diff] [blame] | 151 | } |
| 152 | |
danakj | de3e2a0 | 2020-05-12 16:51:20 | [diff] [blame] | 153 | // static |
| 154 | bool Shell::ShouldHideToolbar() { |
| 155 | return base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 156 | switches::kContentShellHideToolbar); |
| 157 | } |
| 158 | |
arthursonzogni | 75ede19 | 2021-07-06 14:45:46 | [diff] [blame] | 159 | // static |
David Benjamin | f62c666 | 2019-03-21 20:25:04 | [diff] [blame] | 160 | Shell* Shell::FromWebContents(WebContents* web_contents) { |
| 161 | for (Shell* window : windows_) { |
| 162 | if (window->web_contents() && window->web_contents() == web_contents) { |
| 163 | return window; |
[email protected] | 74830f0 | 2012-01-30 22:27:04 | [diff] [blame] | 164 | } |
| 165 | } |
Ivan Kotenkov | 2c0d2bb3 | 2017-11-01 15:41:28 | [diff] [blame] | 166 | return nullptr; |
[email protected] | 74830f0 | 2012-01-30 22:27:04 | [diff] [blame] | 167 | } |
| 168 | |
arthursonzogni | 75ede19 | 2021-07-06 14:45:46 | [diff] [blame] | 169 | // static |
danakj | de3e2a0 | 2020-05-12 16:51:20 | [diff] [blame] | 170 | void Shell::Initialize(std::unique_ptr<ShellPlatformDelegate> platform) { |
arthursonzogni | 75ede19 | 2021-07-06 14:45:46 | [diff] [blame] | 171 | DCHECK(!g_platform); |
danakj | de3e2a0 | 2020-05-12 16:51:20 | [diff] [blame] | 172 | g_platform = platform.release(); |
| 173 | g_platform->Initialize(GetShellDefaultSize()); |
[email protected] | 6153b27 | 2013-01-25 22:29:23 | [diff] [blame] | 174 | } |
| 175 | |
arthursonzogni | 75ede19 | 2021-07-06 14:45:46 | [diff] [blame] | 176 | // static |
| 177 | void Shell::Shutdown() { |
| 178 | if (!g_platform) // Shutdown has already been called. |
| 179 | return; |
| 180 | |
| 181 | DevToolsAgentHost::DetachAllClients(); |
| 182 | |
| 183 | while (!Shell::windows().empty()) |
| 184 | Shell::windows().back()->Close(); |
| 185 | |
| 186 | delete g_platform; |
| 187 | g_platform = nullptr; |
| 188 | |
| 189 | for (auto it = RenderProcessHost::AllHostsIterator(); !it.IsAtEnd(); |
| 190 | it.Advance()) { |
W. James MacLean | 94cc8496 | 2021-09-07 21:51:40 | [diff] [blame] | 191 | it.GetCurrentValue()->DisableRefCounts(); |
arthursonzogni | 75ede19 | 2021-07-06 14:45:46 | [diff] [blame] | 192 | } |
| 193 | if (*g_quit_main_message_loop) |
| 194 | std::move(*g_quit_main_message_loop).Run(); |
| 195 | |
| 196 | // Pump the message loop to allow window teardown tasks to run. |
| 197 | base::RunLoop().RunUntilIdle(); |
| 198 | } |
| 199 | |
[email protected] | a290409 | 2013-10-15 04:53:59 | [diff] [blame] | 200 | gfx::Size Shell::AdjustWindowSize(const gfx::Size& initial_size) { |
| 201 | if (!initial_size.IsEmpty()) |
| 202 | return initial_size; |
pdr | cab84ee | 2015-03-13 21:47:04 | [diff] [blame] | 203 | return GetShellDefaultSize(); |
[email protected] | a290409 | 2013-10-15 04:53:59 | [diff] [blame] | 204 | } |
| 205 | |
arthursonzogni | 75ede19 | 2021-07-06 14:45:46 | [diff] [blame] | 206 | // static |
[email protected] | bdcf915 | 2012-07-19 17:43:21 | [diff] [blame] | 207 | Shell* Shell::CreateNewWindow(BrowserContext* browser_context, |
[email protected] | e99ca511 | 2011-09-26 17:22:54 | [diff] [blame] | 208 | const GURL& url, |
lukasza | 0413015 | 2016-10-21 20:26:32 | [diff] [blame] | 209 | const scoped_refptr<SiteInstance>& site_instance, |
[email protected] | cdb80672 | 2013-01-10 14:18:23 | [diff] [blame] | 210 | const gfx::Size& initial_size) { |
[email protected] | 54944cde | 2012-12-09 09:24:59 | [diff] [blame] | 211 | WebContents::CreateParams create_params(browser_context, site_instance); |
mark a. foltz | ef394fce | 2017-10-21 09:11:02 | [diff] [blame] | 212 | if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 213 | switches::kForcePresentationReceiverForTesting)) { |
| 214 | create_params.starting_sandbox_flags = |
arthursonzogni | b93a447 | 2020-04-10 07:38:00 | [diff] [blame] | 215 | content::kPresentationReceiverSandboxFlags; |
mark a. foltz | ef394fce | 2017-10-21 09:11:02 | [diff] [blame] | 216 | } |
erikchen | bee5c962 | 2018-04-27 19:30:25 | [diff] [blame] | 217 | std::unique_ptr<WebContents> web_contents = |
Erik Chen | bb8e738e | 2018-04-28 14:10:43 | [diff] [blame] | 218 | WebContents::Create(create_params); |
erikchen | bee5c962 | 2018-04-27 19:30:25 | [diff] [blame] | 219 | Shell* shell = |
danakj | fc518493 | 2019-09-12 18:08:32 | [diff] [blame] | 220 | CreateShell(std::move(web_contents), AdjustWindowSize(initial_size), |
Bo Liu | 300c605 | 2018-06-12 04:46:07 | [diff] [blame] | 221 | true /* should_set_delegate */); |
danakj | 7da83393 | 2020-06-23 21:49:40 | [diff] [blame] | 222 | |
[email protected] | e99ca511 | 2011-09-26 17:22:54 | [diff] [blame] | 223 | if (!url.is_empty()) |
| 224 | shell->LoadURL(url); |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 225 | return shell; |
| 226 | } |
| 227 | |
danakj | 3dd7a610 | 2020-12-30 19:58:39 | [diff] [blame] | 228 | void Shell::RenderFrameCreated(RenderFrameHost* frame_host) { |
| 229 | if (frame_host == web_contents_->GetMainFrame()) |
| 230 | g_platform->MainFrameCreated(this); |
danakj | 24577b1 | 2020-05-13 22:38:18 | [diff] [blame] | 231 | } |
| 232 | |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 233 | void Shell::LoadURL(const GURL& url) { |
Alex Moshchuk | 7e26eca | 2018-03-03 01:34:29 | [diff] [blame] | 234 | LoadURLForFrame( |
| 235 | url, std::string(), |
| 236 | ui::PageTransitionFromInt(ui::PAGE_TRANSITION_TYPED | |
| 237 | ui::PAGE_TRANSITION_FROM_ADDRESS_BAR)); |
[email protected] | d2494ff | 2013-02-20 08:22:37 | [diff] [blame] | 238 | } |
| 239 | |
Alex Moshchuk | 7e26eca | 2018-03-03 01:34:29 | [diff] [blame] | 240 | void Shell::LoadURLForFrame(const GURL& url, |
| 241 | const std::string& frame_name, |
| 242 | ui::PageTransition transition_type) { |
[email protected] | d2494ff | 2013-02-20 08:22:37 | [diff] [blame] | 243 | NavigationController::LoadURLParams params(url); |
[email protected] | d2494ff | 2013-02-20 08:22:37 | [diff] [blame] | 244 | params.frame_name = frame_name; |
Alex Moshchuk | 7e26eca | 2018-03-03 01:34:29 | [diff] [blame] | 245 | params.transition_type = transition_type; |
[email protected] | d2494ff | 2013-02-20 08:22:37 | [diff] [blame] | 246 | web_contents_->GetController().LoadURLWithParams(params); |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 247 | } |
| 248 | |
arthursonzogni | 75ede19 | 2021-07-06 14:45:46 | [diff] [blame] | 249 | void Shell::LoadDataWithBaseURL(const GURL& url, |
| 250 | const std::string& data, |
| 251 | const GURL& base_url) { |
boliu | ec93ea9 | 2016-02-17 22:23:07 | [diff] [blame] | 252 | bool load_as_string = false; |
| 253 | LoadDataWithBaseURLInternal(url, data, base_url, load_as_string); |
| 254 | } |
| 255 | |
Xiaohan Wang | bd08442 | 2022-01-15 18:47:51 | [diff] [blame] | 256 | #if BUILDFLAG(IS_ANDROID) |
boliu | ec93ea9 | 2016-02-17 22:23:07 | [diff] [blame] | 257 | void Shell::LoadDataAsStringWithBaseURL(const GURL& url, |
| 258 | const std::string& data, |
| 259 | const GURL& base_url) { |
| 260 | bool load_as_string = true; |
| 261 | LoadDataWithBaseURLInternal(url, data, base_url, load_as_string); |
| 262 | } |
| 263 | #endif |
| 264 | |
| 265 | void Shell::LoadDataWithBaseURLInternal(const GURL& url, |
| 266 | const std::string& data, |
| 267 | const GURL& base_url, |
| 268 | bool load_as_string) { |
Xiaohan Wang | bd08442 | 2022-01-15 18:47:51 | [diff] [blame] | 269 | #if !BUILDFLAG(IS_ANDROID) |
boliu | ec93ea9 | 2016-02-17 22:23:07 | [diff] [blame] | 270 | DCHECK(!load_as_string); // Only supported on Android. |
| 271 | #endif |
| 272 | |
| 273 | NavigationController::LoadURLParams params(GURL::EmptyGURL()); |
| 274 | const std::string data_url_header = "data:text/html;charset=utf-8,"; |
| 275 | if (load_as_string) { |
| 276 | params.url = GURL(data_url_header); |
| 277 | std::string data_url_as_string = data_url_header + data; |
Xiaohan Wang | bd08442 | 2022-01-15 18:47:51 | [diff] [blame] | 278 | #if BUILDFLAG(IS_ANDROID) |
boliu | ec93ea9 | 2016-02-17 22:23:07 | [diff] [blame] | 279 | params.data_url_as_string = |
| 280 | base::RefCountedString::TakeString(&data_url_as_string); |
| 281 | #endif |
| 282 | } else { |
| 283 | params.url = GURL(data_url_header + data); |
| 284 | } |
| 285 | |
[email protected] | 76bdecb | 2014-04-16 17:58:08 | [diff] [blame] | 286 | params.load_type = NavigationController::LOAD_TYPE_DATA; |
| 287 | params.base_url_for_data_url = base_url; |
| 288 | params.virtual_url_for_data_url = url; |
| 289 | params.override_user_agent = NavigationController::UA_OVERRIDE_FALSE; |
| 290 | web_contents_->GetController().LoadURLWithParams(params); |
[email protected] | 76bdecb | 2014-04-16 17:58:08 | [diff] [blame] | 291 | } |
| 292 | |
[email protected] | a290409 | 2013-10-15 04:53:59 | [diff] [blame] | 293 | void Shell::AddNewContents(WebContents* source, |
erikchen | bee5c962 | 2018-04-27 19:30:25 | [diff] [blame] | 294 | std::unique_ptr<WebContents> new_contents, |
Joel Hockey | 891e8806 | 2020-04-30 05:38:44 | [diff] [blame] | 295 | const GURL& target_url, |
[email protected] | a290409 | 2013-10-15 04:53:59 | [diff] [blame] | 296 | WindowOpenDisposition disposition, |
bokan | 107a47f | 2015-02-03 23:23:39 | [diff] [blame] | 297 | const gfx::Rect& initial_rect, |
[email protected] | a290409 | 2013-10-15 04:53:59 | [diff] [blame] | 298 | bool user_gesture, |
| 299 | bool* was_blocked) { |
Bo Liu | 300c605 | 2018-06-12 04:46:07 | [diff] [blame] | 300 | CreateShell( |
| 301 | std::move(new_contents), AdjustWindowSize(initial_rect.size()), |
| 302 | !delay_popup_contents_delegate_for_testing_ /* should_set_delegate */); |
[email protected] | a290409 | 2013-10-15 04:53:59 | [diff] [blame] | 303 | } |
| 304 | |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 305 | void Shell::GoBackOrForward(int offset) { |
[email protected] | 0b659b3 | 2012-03-26 21:29:32 | [diff] [blame] | 306 | web_contents_->GetController().GoToOffset(offset); |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | void Shell::Reload() { |
toyoshim | 6142d96f | 2016-12-19 09:07:25 | [diff] [blame] | 310 | web_contents_->GetController().Reload(ReloadType::NORMAL, false); |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 311 | } |
| 312 | |
toyoshim | e5aaf6a | 2016-05-18 08:07:48 | [diff] [blame] | 313 | void Shell::ReloadBypassingCache() { |
toyoshim | 6142d96f | 2016-12-19 09:07:25 | [diff] [blame] | 314 | web_contents_->GetController().Reload(ReloadType::BYPASSING_CACHE, false); |
toyoshim | e5aaf6a | 2016-05-18 08:07:48 | [diff] [blame] | 315 | } |
| 316 | |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 317 | void Shell::Stop() { |
[email protected] | 0b659b3 | 2012-03-26 21:29:32 | [diff] [blame] | 318 | web_contents_->Stop(); |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 319 | } |
| 320 | |
Nate Chapin | 9aabf5f | 2021-11-12 00:31:19 | [diff] [blame] | 321 | void Shell::UpdateNavigationControls(bool should_show_loading_ui) { |
[email protected] | 0b659b3 | 2012-03-26 21:29:32 | [diff] [blame] | 322 | int current_index = web_contents_->GetController().GetCurrentEntryIndex(); |
| 323 | int max_index = web_contents_->GetController().GetEntryCount() - 1; |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 324 | |
danakj | de3e2a0 | 2020-05-12 16:51:20 | [diff] [blame] | 325 | g_platform->EnableUIControl(this, ShellPlatformDelegate::BACK_BUTTON, |
| 326 | current_index > 0); |
| 327 | g_platform->EnableUIControl(this, ShellPlatformDelegate::FORWARD_BUTTON, |
| 328 | current_index < max_index); |
| 329 | g_platform->EnableUIControl( |
| 330 | this, ShellPlatformDelegate::STOP_BUTTON, |
Nate Chapin | 9aabf5f | 2021-11-12 00:31:19 | [diff] [blame] | 331 | should_show_loading_ui && web_contents_->IsLoading()); |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 332 | } |
| 333 | |
[email protected] | 7c17b699 | 2012-08-09 16:16:30 | [diff] [blame] | 334 | void Shell::ShowDevTools() { |
mohsen | 6eb57fb | 2016-07-22 03:14:08 | [diff] [blame] | 335 | if (!devtools_frontend_) { |
Dave Tapuska | 5198f0e | 2021-11-18 15:41:11 | [diff] [blame] | 336 | auto* devtools_frontend = ShellDevToolsFrontend::Show(web_contents()); |
| 337 | devtools_frontend_ = devtools_frontend->GetWeakPtr(); |
mohsen | 6eb57fb | 2016-07-22 03:14:08 | [diff] [blame] | 338 | } |
[email protected] | 3142e5d | 2014-02-07 00:54:46 | [diff] [blame] | 339 | |
mohsen | 6eb57fb | 2016-07-22 03:14:08 | [diff] [blame] | 340 | devtools_frontend_->Activate(); |
[email protected] | 7c17b699 | 2012-08-09 16:16:30 | [diff] [blame] | 341 | } |
| 342 | |
[email protected] | 001841c9 | 2012-12-11 17:00:13 | [diff] [blame] | 343 | void Shell::CloseDevTools() { |
[email protected] | 0773e0c | 2013-01-25 15:57:57 | [diff] [blame] | 344 | if (!devtools_frontend_) |
[email protected] | 001841c9 | 2012-12-11 17:00:13 | [diff] [blame] | 345 | return; |
[email protected] | 0773e0c | 2013-01-25 15:57:57 | [diff] [blame] | 346 | devtools_frontend_->Close(); |
Ivan Kotenkov | 2c0d2bb3 | 2017-11-01 15:41:28 | [diff] [blame] | 347 | devtools_frontend_ = nullptr; |
[email protected] | 001841c9 | 2012-12-11 17:00:13 | [diff] [blame] | 348 | } |
| 349 | |
danakj | de3e2a0 | 2020-05-12 16:51:20 | [diff] [blame] | 350 | void Shell::ResizeWebContentForTests(const gfx::Size& content_size) { |
| 351 | g_platform->ResizeWebContent(this, content_size); |
| 352 | } |
danakj | de3e2a0 | 2020-05-12 16:51:20 | [diff] [blame] | 353 | |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 354 | gfx::NativeView Shell::GetContentView() { |
[email protected] | 59383c78 | 2013-04-17 16:43:27 | [diff] [blame] | 355 | if (!web_contents_) |
Ivan Kotenkov | 2c0d2bb3 | 2017-11-01 15:41:28 | [diff] [blame] | 356 | return nullptr; |
[email protected] | fc2b46b | 2014-05-03 16:33:45 | [diff] [blame] | 357 | return web_contents_->GetNativeView(); |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 358 | } |
| 359 | |
Xiaohan Wang | bd08442 | 2022-01-15 18:47:51 | [diff] [blame] | 360 | #if !BUILDFLAG(IS_ANDROID) |
danakj | de3e2a0 | 2020-05-12 16:51:20 | [diff] [blame] | 361 | gfx::NativeWindow Shell::window() { |
| 362 | return g_platform->GetNativeWindow(this); |
| 363 | } |
| 364 | #endif |
| 365 | |
Xiaohan Wang | bd08442 | 2022-01-15 18:47:51 | [diff] [blame] | 366 | #if BUILDFLAG(IS_MAC) |
danakj | de3e2a0 | 2020-05-12 16:51:20 | [diff] [blame] | 367 | void Shell::ActionPerformed(int control) { |
| 368 | switch (control) { |
| 369 | case IDC_NAV_BACK: |
| 370 | GoBackOrForward(-1); |
| 371 | break; |
| 372 | case IDC_NAV_FORWARD: |
| 373 | GoBackOrForward(1); |
| 374 | break; |
| 375 | case IDC_NAV_RELOAD: |
| 376 | Reload(); |
| 377 | break; |
| 378 | case IDC_NAV_STOP: |
| 379 | Stop(); |
| 380 | break; |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | void Shell::URLEntered(const std::string& url_string) { |
| 385 | if (!url_string.empty()) { |
| 386 | GURL url(url_string); |
| 387 | if (!url.has_scheme()) |
| 388 | url = GURL("http://" + url_string); |
| 389 | LoadURL(url); |
| 390 | } |
| 391 | } |
| 392 | #endif |
| 393 | |
alexmos | 5a98a05 | 2016-01-06 00:15:02 | [diff] [blame] | 394 | WebContents* Shell::OpenURLFromTab(WebContents* source, |
| 395 | const OpenURLParams& params) { |
lukasza | 0413015 | 2016-10-21 20:26:32 | [diff] [blame] | 396 | WebContents* target = nullptr; |
| 397 | switch (params.disposition) { |
| 398 | case WindowOpenDisposition::CURRENT_TAB: |
| 399 | target = source; |
| 400 | break; |
| 401 | |
| 402 | // Normally, the difference between NEW_POPUP and NEW_WINDOW is that a popup |
| 403 | // should have no toolbar, no status bar, no menu bar, no scrollbars and be |
| 404 | // not resizable. For simplicity and to enable new testing scenarios in |
Kent Tamura | 21d1de6 | 2018-12-10 04:45:20 | [diff] [blame] | 405 | // content shell and web tests, popups don't get special treatment below |
lukasza | 0413015 | 2016-10-21 20:26:32 | [diff] [blame] | 406 | // (i.e. they will have a toolbar and other things described here). |
| 407 | case WindowOpenDisposition::NEW_POPUP: |
lukasza | be2f0da | 2017-04-25 00:43:00 | [diff] [blame] | 408 | case WindowOpenDisposition::NEW_WINDOW: |
Kent Tamura | 21d1de6 | 2018-12-10 04:45:20 | [diff] [blame] | 409 | // content_shell doesn't really support tabs, but some web tests use |
lukasza | be2f0da | 2017-04-25 00:43:00 | [diff] [blame] | 410 | // middle click (which translates into kNavigationPolicyNewBackgroundTab), |
| 411 | // so we treat the cases below just like a NEW_WINDOW disposition. |
| 412 | case WindowOpenDisposition::NEW_BACKGROUND_TAB: |
| 413 | case WindowOpenDisposition::NEW_FOREGROUND_TAB: { |
lukasza | 0413015 | 2016-10-21 20:26:32 | [diff] [blame] | 414 | Shell* new_window = |
| 415 | Shell::CreateNewWindow(source->GetBrowserContext(), |
| 416 | GURL(), // Don't load anything just yet. |
| 417 | params.source_site_instance, |
| 418 | gfx::Size()); // Use default size. |
| 419 | target = new_window->web_contents(); |
lukasza | 0413015 | 2016-10-21 20:26:32 | [diff] [blame] | 420 | break; |
| 421 | } |
| 422 | |
| 423 | // No tabs in content_shell: |
| 424 | case WindowOpenDisposition::SINGLETON_TAB: |
lukasza | 0413015 | 2016-10-21 20:26:32 | [diff] [blame] | 425 | // No incognito mode in content_shell: |
| 426 | case WindowOpenDisposition::OFF_THE_RECORD: |
Kent Tamura | 21d1de6 | 2018-12-10 04:45:20 | [diff] [blame] | 427 | // TODO(lukasza): Investigate if some web tests might need support for |
lukasza | 0413015 | 2016-10-21 20:26:32 | [diff] [blame] | 428 | // SAVE_TO_DISK disposition. This would probably require that |
Gyuyoung Kim | 26c7bc9 | 2020-04-29 00:53:00 | [diff] [blame] | 429 | // WebTestControlHost always sets up and cleans up a temporary directory |
lukasza | 0413015 | 2016-10-21 20:26:32 | [diff] [blame] | 430 | // as the default downloads destinations for the duration of a test. |
| 431 | case WindowOpenDisposition::SAVE_TO_DISK: |
| 432 | // Ignoring requests with disposition == IGNORE_ACTION... |
| 433 | case WindowOpenDisposition::IGNORE_ACTION: |
| 434 | default: |
| 435 | return nullptr; |
| 436 | } |
alexmos | 5a98a05 | 2016-01-06 00:15:02 | [diff] [blame] | 437 | |
Lukasz Anforowicz | 82a5ca9 | 2019-10-24 18:45:37 | [diff] [blame] | 438 | target->GetController().LoadURLWithParams( |
| 439 | NavigationController::LoadURLParams(params)); |
lukasza | 0413015 | 2016-10-21 20:26:32 | [diff] [blame] | 440 | return target; |
alexmos | 5a98a05 | 2016-01-06 00:15:02 | [diff] [blame] | 441 | } |
| 442 | |
[email protected] | e3b10d1 | 2014-03-28 16:06:09 | [diff] [blame] | 443 | void Shell::LoadingStateChanged(WebContents* source, |
Nate Chapin | 9aabf5f | 2021-11-12 00:31:19 | [diff] [blame] | 444 | bool should_show_loading_ui) { |
| 445 | UpdateNavigationControls(should_show_loading_ui); |
danakj | de3e2a0 | 2020-05-12 16:51:20 | [diff] [blame] | 446 | g_platform->SetIsLoading(this, source->IsLoading()); |
[email protected] | e99ca511 | 2011-09-26 17:22:54 | [diff] [blame] | 447 | } |
| 448 | |
Xiaohan Wang | bd08442 | 2022-01-15 18:47:51 | [diff] [blame] | 449 | #if BUILDFLAG(IS_ANDROID) |
danakj | de3e2a0 | 2020-05-12 16:51:20 | [diff] [blame] | 450 | void Shell::SetOverlayMode(bool use_overlay_mode) { |
| 451 | g_platform->SetOverlayMode(this, use_overlay_mode); |
| 452 | } |
| 453 | #endif |
| 454 | |
Dave Tapuska | a418951 | 2019-10-15 20:27:34 | [diff] [blame] | 455 | void Shell::EnterFullscreenModeForTab( |
Mike Wasserman | 4ca0979 | 2020-05-29 17:44:43 | [diff] [blame] | 456 | RenderFrameHost* requesting_frame, |
Dave Tapuska | a418951 | 2019-10-15 20:27:34 | [diff] [blame] | 457 | const blink::mojom::FullscreenOptions& options) { |
Mike Wasserman | 4ca0979 | 2020-05-29 17:44:43 | [diff] [blame] | 458 | ToggleFullscreenModeForTab(WebContents::FromRenderFrameHost(requesting_frame), |
| 459 | true); |
mlamouri | 7a78d6fd | 2015-01-17 13:23:53 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | void Shell::ExitFullscreenModeForTab(WebContents* web_contents) { |
| 463 | ToggleFullscreenModeForTab(web_contents, false); |
| 464 | } |
| 465 | |
[email protected] | 99c014c | 2012-11-27 12:03:42 | [diff] [blame] | 466 | void Shell::ToggleFullscreenModeForTab(WebContents* web_contents, |
| 467 | bool enter_fullscreen) { |
Xiaohan Wang | bd08442 | 2022-01-15 18:47:51 | [diff] [blame] | 468 | #if BUILDFLAG(IS_ANDROID) |
danakj | de3e2a0 | 2020-05-12 16:51:20 | [diff] [blame] | 469 | g_platform->ToggleFullscreenModeForTab(this, web_contents, enter_fullscreen); |
[email protected] | 99c014c | 2012-11-27 12:03:42 | [diff] [blame] | 470 | #endif |
[email protected] | 99c014c | 2012-11-27 12:03:42 | [diff] [blame] | 471 | if (is_fullscreen_ != enter_fullscreen) { |
| 472 | is_fullscreen_ = enter_fullscreen; |
Lucas Furukawa Gadani | e764942 | 2021-02-03 03:04:32 | [diff] [blame] | 473 | web_contents->GetMainFrame() |
| 474 | ->GetRenderViewHost() |
Fady Samuel | 0b91182 | 2018-04-25 13:22:16 | [diff] [blame] | 475 | ->GetWidget() |
| 476 | ->SynchronizeVisualProperties(); |
[email protected] | 99c014c | 2012-11-27 12:03:42 | [diff] [blame] | 477 | } |
| 478 | } |
| 479 | |
Lucas Furukawa Gadani | 4909f3c | 2019-06-18 22:36:52 | [diff] [blame] | 480 | bool Shell::IsFullscreenForTabOrPending(const WebContents* web_contents) { |
Xiaohan Wang | bd08442 | 2022-01-15 18:47:51 | [diff] [blame] | 481 | #if BUILDFLAG(IS_ANDROID) |
danakj | de3e2a0 | 2020-05-12 16:51:20 | [diff] [blame] | 482 | return g_platform->IsFullscreenForTabOrPending(this, web_contents); |
[email protected] | 99c014c | 2012-11-27 12:03:42 | [diff] [blame] | 483 | #else |
| 484 | return is_fullscreen_; |
| 485 | #endif |
| 486 | } |
| 487 | |
Eric Willigers | 052f043 | 2019-10-04 04:06:57 | [diff] [blame] | 488 | blink::mojom::DisplayMode Shell::GetDisplayMode( |
| 489 | const WebContents* web_contents) { |
| 490 | // TODO: should return blink::mojom::DisplayModeFullscreen wherever user puts |
Lukasz Anforowicz | 52b9372 | 2018-06-20 16:11:39 | [diff] [blame] | 491 | // a browser window into fullscreen (not only in case of renderer-initiated |
| 492 | // fullscreen mode): crbug.com/476874. |
| 493 | return IsFullscreenForTabOrPending(web_contents) |
Eric Willigers | 052f043 | 2019-10-04 04:06:57 | [diff] [blame] | 494 | ? blink::mojom::DisplayMode::kFullscreen |
| 495 | : blink::mojom::DisplayMode::kBrowser; |
mikhail.pozdnyakov | c0e251b | 2015-04-15 06:51:12 | [diff] [blame] | 496 | } |
| 497 | |
Javier Fernández García-Boente | 13150a04 | 2022-04-04 21:08:22 | [diff] [blame^] | 498 | #if !BUILDFLAG(IS_ANDROID) |
| 499 | void Shell::RegisterProtocolHandler(RenderFrameHost* requesting_frame, |
| 500 | const std::string& protocol, |
| 501 | const GURL& url, |
| 502 | bool user_gesture) { |
| 503 | content::BrowserContext* context = requesting_frame->GetBrowserContext(); |
| 504 | if (context->IsOffTheRecord()) |
| 505 | return; |
| 506 | |
| 507 | custom_handlers::ProtocolHandler handler = |
| 508 | custom_handlers::ProtocolHandler::CreateProtocolHandler( |
| 509 | protocol, url, GetProtocolHandlerSecurityLevel(requesting_frame)); |
| 510 | |
| 511 | // The parameters's normalization process defined in the spec has been already |
| 512 | // applied in the WebContentImpl class, so at this point it shouldn't be |
| 513 | // possible to create an invalid handler. |
| 514 | // https://html.spec.whatwg.org/multipage/system-state.html#normalize-protocol-handler-parameters |
| 515 | DCHECK(handler.IsValid()); |
| 516 | |
| 517 | custom_handlers::ProtocolHandlerRegistry* registry = custom_handlers:: |
| 518 | SimpleProtocolHandlerRegistryFactory::GetForBrowserContext(context, true); |
| 519 | DCHECK(registry); |
| 520 | if (registry->SilentlyHandleRegisterHandlerRequest(handler)) |
| 521 | return; |
| 522 | |
| 523 | // FencedFrames can not register to handle any protocols. |
| 524 | if (requesting_frame->IsNestedWithinFencedFrame()) { |
| 525 | registry->OnIgnoreRegisterProtocolHandler(handler); |
| 526 | return; |
| 527 | } |
| 528 | |
| 529 | // TODO(jfernandez): Are we interested at all on using the |
| 530 | // PermissionRequestManager in the ContentShell ? |
| 531 | registry->OnAcceptRegisterProtocolHandler(handler); |
| 532 | } |
| 533 | #endif |
| 534 | |
[email protected] | f7843900 | 2012-11-28 14:45:59 | [diff] [blame] | 535 | void Shell::RequestToLockMouse(WebContents* web_contents, |
| 536 | bool user_gesture, |
| 537 | bool last_unlocked_by_target) { |
Dave Tapuska | b499878 | 2020-10-08 17:22:47 | [diff] [blame] | 538 | // Give the platform a chance to handle the lock request, if it doesn't |
| 539 | // indicate it handled it, allow the request. |
| 540 | if (!g_platform->HandleRequestToLockMouse(this, web_contents, user_gesture, |
| 541 | last_unlocked_by_target)) { |
| 542 | web_contents->GotResponseToLockMouseRequest( |
| 543 | blink::mojom::PointerLockResult::kSuccess); |
| 544 | } |
[email protected] | f7843900 | 2012-11-28 14:45:59 | [diff] [blame] | 545 | } |
| 546 | |
danakj | de3e2a0 | 2020-05-12 16:51:20 | [diff] [blame] | 547 | void Shell::Close() { |
| 548 | // Shell is "self-owned" and destroys itself. The ShellPlatformDelegate |
| 549 | // has the chance to co-opt this and do its own destruction. |
| 550 | if (!g_platform->DestroyShell(this)) |
| 551 | delete this; |
| 552 | } |
| 553 | |
[email protected] | 9e00e635 | 2012-07-30 17:05:18 | [diff] [blame] | 554 | void Shell::CloseContents(WebContents* source) { |
| 555 | Close(); |
| 556 | } |
| 557 | |
Lucas Furukawa Gadani | 4909f3c | 2019-06-18 22:36:52 | [diff] [blame] | 558 | bool Shell::CanOverscrollContent() { |
[email protected] | 06731026 | 2012-11-22 14:30:41 | [diff] [blame] | 559 | #if defined(USE_AURA) |
| 560 | return true; |
| 561 | #else |
| 562 | return false; |
| 563 | #endif |
| 564 | } |
| 565 | |
Mugdha Lakhani | 5f8de7cc | 2020-03-10 20:43:36 | [diff] [blame] | 566 | void Shell::NavigationStateChanged(WebContents* source, |
| 567 | InvalidateTypes changed_flags) { |
| 568 | if (changed_flags & INVALIDATE_TYPE_URL) |
danakj | de3e2a0 | 2020-05-12 16:51:20 | [diff] [blame] | 569 | g_platform->SetAddressBarURL(this, source->GetVisibleURL()); |
[email protected] | e99ca511 | 2011-09-26 17:22:54 | [diff] [blame] | 570 | } |
| 571 | |
mathiash | 72a5e46 | 2014-11-19 08:18:50 | [diff] [blame] | 572 | JavaScriptDialogManager* Shell::GetJavaScriptDialogManager( |
| 573 | WebContents* source) { |
danakj | 8de3b749 | 2020-07-02 22:41:42 | [diff] [blame] | 574 | if (!dialog_manager_) |
| 575 | dialog_manager_ = g_platform->CreateJavaScriptDialogManager(this); |
| 576 | if (!dialog_manager_) |
| 577 | dialog_manager_ = std::make_unique<ShellJavaScriptDialogManager>(); |
[email protected] | 71a88bb | 2013-02-01 22:05:15 | [diff] [blame] | 578 | return dialog_manager_.get(); |
[email protected] | f221002 | 2012-03-29 00:36:08 | [diff] [blame] | 579 | } |
| 580 | |
Xiaohan Wang | bd08442 | 2022-01-15 18:47:51 | [diff] [blame] | 581 | #if BUILDFLAG(IS_MAC) |
Liviu Tinta | 4090405 | 2021-07-20 15:12:52 | [diff] [blame] | 582 | void Shell::DidNavigatePrimaryMainFramePostCommit(WebContents* contents) { |
| 583 | g_platform->DidNavigatePrimaryMainFramePostCommit(this, contents); |
Xianzhu Wang | 0f021a8 | 2020-07-03 01:29:47 | [diff] [blame] | 584 | } |
| 585 | |
danakj | de3e2a0 | 2020-05-12 16:51:20 | [diff] [blame] | 586 | bool Shell::HandleKeyboardEvent(WebContents* source, |
| 587 | const NativeWebKeyboardEvent& event) { |
| 588 | return g_platform->HandleKeyboardEvent(this, source, event); |
| 589 | } |
| 590 | #endif |
| 591 | |
avi | a90ae4e | 2016-11-11 20:49:33 | [diff] [blame] | 592 | bool Shell::DidAddMessageToConsole(WebContents* source, |
Lowell Manners | 1de5242e | 2019-04-25 10:18:46 | [diff] [blame] | 593 | blink::mojom::ConsoleMessageLevel log_level, |
Jan Wilken Dörrie | aace0cfef | 2021-03-11 22:01:58 | [diff] [blame] | 594 | const std::u16string& message, |
avi | a90ae4e | 2016-11-11 20:49:33 | [diff] [blame] | 595 | int32_t line_no, |
Jan Wilken Dörrie | aace0cfef | 2021-03-11 22:01:58 | [diff] [blame] | 596 | const std::u16string& source_id) { |
Kent Tamura | cd3ebc4 | 2018-05-16 06:44:22 | [diff] [blame] | 597 | return switches::IsRunWebTestsSwitchPresent(); |
[email protected] | efb5f57 | 2012-01-29 10:57:33 | [diff] [blame] | 598 | } |
| 599 | |
Lucas Furukawa Gadani | 06548fb | 2019-01-15 05:01:25 | [diff] [blame] | 600 | void Shell::PortalWebContentsCreated(WebContents* portal_web_contents) { |
danakj | 8de3b749 | 2020-07-02 22:41:42 | [diff] [blame] | 601 | g_platform->DidCreateOrAttachWebContents(this, portal_web_contents); |
Lucas Furukawa Gadani | 06548fb | 2019-01-15 05:01:25 | [diff] [blame] | 602 | } |
| 603 | |
Lukasz Anforowicz | 52b9372 | 2018-06-20 16:11:39 | [diff] [blame] | 604 | void Shell::RendererUnresponsive( |
| 605 | WebContents* source, |
| 606 | RenderWidgetHost* render_widget_host, |
| 607 | base::RepeatingClosure hang_monitor_restarter) { |
danakj | 245441f | 2020-07-03 15:18:41 | [diff] [blame] | 608 | LOG(WARNING) << "renderer unresponsive"; |
[email protected] | 5bf68f2 | 2012-08-31 07:38:10 | [diff] [blame] | 609 | } |
| 610 | |
[email protected] | 233567d | 2013-02-27 20:22:02 | [diff] [blame] | 611 | void Shell::ActivateContents(WebContents* contents) { |
Xiaohan Wang | bd08442 | 2022-01-15 18:47:51 | [diff] [blame] | 612 | #if !BUILDFLAG(IS_MAC) |
danakj | d4b48df5 | 2020-07-02 18:16:48 | [diff] [blame] | 613 | // TODO(danakj): Move this to ShellPlatformDelegate. |
danakj | 674bf1c0 | 2020-05-01 18:37:51 | [diff] [blame] | 614 | contents->Focus(); |
| 615 | #else |
| 616 | // Mac headless mode is quite different than other platforms. Normally |
| 617 | // focusing the WebContents would cause the OS to focus the window. Because |
| 618 | // headless mac doesn't actually have system windows, we can't go down the |
| 619 | // normal path and have to fake it out in the browser process. |
danakj | de3e2a0 | 2020-05-12 16:51:20 | [diff] [blame] | 620 | g_platform->ActivateContents(this, contents); |
danakj | 674bf1c0 | 2020-05-01 18:37:51 | [diff] [blame] | 621 | #endif |
[email protected] | 233567d | 2013-02-27 20:22:02 | [diff] [blame] | 622 | } |
| 623 | |
Dave Tapuska | dfff738 | 2021-04-23 19:46:41 | [diff] [blame] | 624 | bool Shell::IsBackForwardCacheSupported() { |
| 625 | return true; |
| 626 | } |
| 627 | |
Hiroki Nakagawa | 4ae11b88 | 2022-02-03 03:24:11 | [diff] [blame] | 628 | bool Shell::IsPrerender2Supported(WebContents& web_contents) { |
Lingqi Chi | 8159aec | 2021-06-15 01:46:03 | [diff] [blame] | 629 | return true; |
| 630 | } |
| 631 | |
Kevin McNee | 7422bc4 | 2020-02-20 18:49:55 | [diff] [blame] | 632 | std::unique_ptr<WebContents> Shell::ActivatePortalWebContents( |
| 633 | WebContents* predecessor_contents, |
| 634 | std::unique_ptr<WebContents> portal_contents) { |
| 635 | DCHECK_EQ(predecessor_contents, web_contents_.get()); |
| 636 | portal_contents->SetDelegate(this); |
Lucas Furukawa Gadani | c5c0cd0 | 2018-10-04 20:32:33 | [diff] [blame] | 637 | web_contents_->SetDelegate(nullptr); |
Kevin McNee | 7422bc4 | 2020-02-20 18:49:55 | [diff] [blame] | 638 | std::swap(web_contents_, portal_contents); |
danakj | de3e2a0 | 2020-05-12 16:51:20 | [diff] [blame] | 639 | g_platform->SetContents(this); |
| 640 | g_platform->SetAddressBarURL(this, web_contents_->GetVisibleURL()); |
Lucas Furukawa Gadani | c5c0cd0 | 2018-10-04 20:32:33 | [diff] [blame] | 641 | LoadingStateChanged(web_contents_.get(), true); |
Kevin McNee | 7422bc4 | 2020-02-20 18:49:55 | [diff] [blame] | 642 | return portal_contents; |
Lucas Furukawa Gadani | c5c0cd0 | 2018-10-04 20:32:33 | [diff] [blame] | 643 | } |
| 644 | |
Adithya Srinivasan | b7204c8 | 2020-08-17 14:26:33 | [diff] [blame] | 645 | namespace { |
| 646 | class PendingCallback : public base::RefCounted<PendingCallback> { |
| 647 | public: |
| 648 | explicit PendingCallback(base::OnceCallback<void()> cb) |
| 649 | : callback_(std::move(cb)) {} |
| 650 | |
| 651 | private: |
| 652 | friend class base::RefCounted<PendingCallback>; |
| 653 | ~PendingCallback() { std::move(callback_).Run(); } |
| 654 | base::OnceCallback<void()> callback_; |
| 655 | }; |
| 656 | } // namespace |
| 657 | |
| 658 | void Shell::UpdateInspectedWebContentsIfNecessary( |
| 659 | content::WebContents* old_contents, |
| 660 | content::WebContents* new_contents, |
| 661 | base::OnceCallback<void()> callback) { |
| 662 | scoped_refptr<PendingCallback> pending_callback = |
| 663 | base::MakeRefCounted<PendingCallback>(std::move(callback)); |
| 664 | for (auto* shell_devtools_bindings : |
| 665 | ShellDevToolsBindings::GetInstancesForWebContents(old_contents)) { |
| 666 | shell_devtools_bindings->UpdateInspectedWebContents( |
Daniel Cheng | b7b2f5b | 2021-09-25 21:37:19 | [diff] [blame] | 667 | new_contents, base::BindOnce([](scoped_refptr<PendingCallback>) {}, |
| 668 | pending_callback)); |
Adithya Srinivasan | b7204c8 | 2020-08-17 14:26:33 | [diff] [blame] | 669 | } |
| 670 | } |
| 671 | |
Lukasz Anforowicz | 82a5ca9 | 2019-10-24 18:45:37 | [diff] [blame] | 672 | bool Shell::ShouldAllowRunningInsecureContent(WebContents* web_contents, |
| 673 | bool allowed_per_prefs, |
| 674 | const url::Origin& origin, |
| 675 | const GURL& resource_url) { |
danakj | 8de3b749 | 2020-07-02 22:41:42 | [diff] [blame] | 676 | if (allowed_per_prefs) |
| 677 | return true; |
carlosk | d9d9794 | 2017-02-16 08:58:09 | [diff] [blame] | 678 | |
danakj | 8de3b749 | 2020-07-02 22:41:42 | [diff] [blame] | 679 | return g_platform->ShouldAllowRunningInsecureContent(this); |
carlosk | d9d9794 | 2017-02-16 08:58:09 | [diff] [blame] | 680 | } |
| 681 | |
François Beaufort | 1388f289 | 2022-01-29 08:22:47 | [diff] [blame] | 682 | PictureInPictureResult Shell::EnterPictureInPicture(WebContents* web_contents) { |
Becca Hughes | 112832e | 2019-06-11 17:19:02 | [diff] [blame] | 683 | // During tests, returning success to pretend the window was created and allow |
| 684 | // tests to run accordingly. |
| 685 | if (!switches::IsRunWebTestsSwitchPresent()) |
| 686 | return PictureInPictureResult::kNotSupported; |
Becca Hughes | 112832e | 2019-06-11 17:19:02 | [diff] [blame] | 687 | return PictureInPictureResult::kSuccess; |
Mounir Lamouri | 11e9ef43 | 2018-05-22 03:10:16 | [diff] [blame] | 688 | } |
| 689 | |
Bo Liu | 300c605 | 2018-06-12 04:46:07 | [diff] [blame] | 690 | bool Shell::ShouldResumeRequestsForCreatedWindow() { |
| 691 | return !delay_popup_contents_delegate_for_testing_; |
| 692 | } |
| 693 | |
danakj | ee2390a8 | 2020-06-10 16:53:37 | [diff] [blame] | 694 | void Shell::SetContentsBounds(WebContents* source, const gfx::Rect& bounds) { |
| 695 | DCHECK(source == web_contents()); // There's only one WebContents per Shell. |
| 696 | |
| 697 | if (switches::IsRunWebTestsSwitchPresent()) { |
| 698 | // Note that chrome drops these requests on normal windows. |
| 699 | // TODO(danakj): The position is dropped here but we use the size. Web tests |
| 700 | // can't move the window in headless mode anyways, but maybe we should be |
| 701 | // letting them pretend? |
| 702 | g_platform->ResizeWebContent(this, bounds.size()); |
| 703 | } |
| 704 | } |
| 705 | |
pdr | cab84ee | 2015-03-13 21:47:04 | [diff] [blame] | 706 | gfx::Size Shell::GetShellDefaultSize() { |
danakj | 6c16fe9 | 2020-09-18 23:51:32 | [diff] [blame] | 707 | static gfx::Size default_shell_size; // Only go through this method once. |
| 708 | |
pdr | cab84ee | 2015-03-13 21:47:04 | [diff] [blame] | 709 | if (!default_shell_size.IsEmpty()) |
| 710 | return default_shell_size; |
danakj | 6c16fe9 | 2020-09-18 23:51:32 | [diff] [blame] | 711 | |
pdr | cab84ee | 2015-03-13 21:47:04 | [diff] [blame] | 712 | base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
| 713 | if (command_line->HasSwitch(switches::kContentShellHostWindowSize)) { |
| 714 | const std::string size_str = command_line->GetSwitchValueASCII( |
arthursonzogni | 75ede19 | 2021-07-06 14:45:46 | [diff] [blame] | 715 | switches::kContentShellHostWindowSize); |
pdr | cab84ee | 2015-03-13 21:47:04 | [diff] [blame] | 716 | int width, height; |
danakj | 6c16fe9 | 2020-09-18 23:51:32 | [diff] [blame] | 717 | if (sscanf(size_str.c_str(), "%dx%d", &width, &height) == 2) { |
| 718 | default_shell_size = gfx::Size(width, height); |
| 719 | } else { |
| 720 | LOG(ERROR) << "Invalid size \"" << size_str << "\" given to --" |
| 721 | << switches::kContentShellHostWindowSize; |
| 722 | } |
| 723 | } |
| 724 | |
| 725 | if (default_shell_size.IsEmpty()) { |
arthursonzogni | 75ede19 | 2021-07-06 14:45:46 | [diff] [blame] | 726 | default_shell_size = |
| 727 | gfx::Size(kDefaultTestWindowWidthDip, kDefaultTestWindowHeightDip); |
pdr | cab84ee | 2015-03-13 21:47:04 | [diff] [blame] | 728 | } |
danakj | 6c16fe9 | 2020-09-18 23:51:32 | [diff] [blame] | 729 | |
pdr | cab84ee | 2015-03-13 21:47:04 | [diff] [blame] | 730 | return default_shell_size; |
| 731 | } |
| 732 | |
Xiaohan Wang | bd08442 | 2022-01-15 18:47:51 | [diff] [blame] | 733 | #if BUILDFLAG(IS_ANDROID) |
danakj | de3e2a0 | 2020-05-12 16:51:20 | [diff] [blame] | 734 | void Shell::LoadProgressChanged(double progress) { |
| 735 | g_platform->LoadProgressChanged(this, progress); |
| 736 | } |
| 737 | #endif |
| 738 | |
Avi Drissman | 9300221 | 2017-09-27 03:20:52 | [diff] [blame] | 739 | void Shell::TitleWasSet(NavigationEntry* entry) { |
[email protected] | 1ef02d24 | 2013-10-07 16:18:53 | [diff] [blame] | 740 | if (entry) |
danakj | de3e2a0 | 2020-05-12 16:51:20 | [diff] [blame] | 741 | g_platform->SetTitle(this, entry->GetTitle()); |
[email protected] | aecc085b | 2012-06-01 18:15:53 | [diff] [blame] | 742 | } |
| 743 | |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 744 | } // namespace content |