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