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