[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 | |
[email protected] | 11a65b69 | 2012-03-30 11:29:16 | [diff] [blame] | 9 | #include "base/auto_reset.h" |
[email protected] | efb5f57 | 2012-01-29 10:57:33 | [diff] [blame] | 10 | #include "base/command_line.h" |
skyostil | 95082a6 | 2015-06-05 19:53:07 | [diff] [blame] | 11 | #include "base/location.h" |
avi | 66a0772 | 2015-12-25 23:38:12 | [diff] [blame] | 12 | #include "base/macros.h" |
gab | f64a25e | 2017-05-12 19:42:56 | [diff] [blame] | 13 | #include "base/message_loop/message_loop.h" |
fdoray | 896bea1 | 2016-06-10 15:52:01 | [diff] [blame] | 14 | #include "base/run_loop.h" |
skyostil | 95082a6 | 2015-06-05 19:53:07 | [diff] [blame] | 15 | #include "base/single_thread_task_runner.h" |
[email protected] | 21aa9968 | 2013-06-11 07:17:01 | [diff] [blame] | 16 | #include "base/strings/string_number_conversions.h" |
| 17 | #include "base/strings/string_util.h" |
| 18 | #include "base/strings/stringprintf.h" |
[email protected] | 74ebfb1 | 2013-06-07 20:48:00 | [diff] [blame] | 19 | #include "base/strings/utf_string_conversions.h" |
gab | 30f26df | 2016-05-11 19:37:55 | [diff] [blame] | 20 | #include "base/threading/thread_task_runner_handle.h" |
avi | 66a0772 | 2015-12-25 23:38:12 | [diff] [blame] | 21 | #include "build/build_config.h" |
[email protected] | b50452f | 2014-08-18 12:31:44 | [diff] [blame] | 22 | #include "content/public/browser/devtools_agent_host.h" |
[email protected] | 0b659b3 | 2012-03-26 21:29:32 | [diff] [blame] | 23 | #include "content/public/browser/navigation_controller.h" |
[email protected] | b7c504c | 2013-05-07 14:42:12 | [diff] [blame] | 24 | #include "content/public/browser/navigation_entry.h" |
[email protected] | 0b659b3 | 2012-03-26 21:29:32 | [diff] [blame] | 25 | #include "content/public/browser/render_view_host.h" |
avi | f9ab5d94 | 2015-10-15 14:05:44 | [diff] [blame] | 26 | #include "content/public/browser/render_widget_host.h" |
[email protected] | 0b659b3 | 2012-03-26 21:29:32 | [diff] [blame] | 27 | #include "content/public/browser/web_contents.h" |
[email protected] | 7fff43e | 2013-05-21 20:21:10 | [diff] [blame] | 28 | #include "content/public/browser/web_contents_observer.h" |
guoweis | 8efb6d89 | 2015-10-12 18:26:17 | [diff] [blame] | 29 | #include "content/public/common/content_switches.h" |
[email protected] | 1596efb | 2013-01-17 22:13:01 | [diff] [blame] | 30 | #include "content/public/common/renderer_preferences.h" |
guoweis | c537ba2 | 2015-11-06 21:20:31 | [diff] [blame] | 31 | #include "content/public/common/webrtc_ip_handling_policy.h" |
jochen | 5ce1af9 | 2016-02-17 20:39:45 | [diff] [blame] | 32 | #include "content/shell/browser/layout_test/blink_test_controller.h" |
jyasskin | 8057c00 | 2015-09-10 19:09:53 | [diff] [blame] | 33 | #include "content/shell/browser/layout_test/layout_test_bluetooth_chooser_factory.h" |
chenwilliam | fed5bda | 2017-03-24 02:01:06 | [diff] [blame] | 34 | #include "content/shell/browser/layout_test/layout_test_devtools_bindings.h" |
mkwst | 6efe7f2e6 | 2014-10-08 15:09:01 | [diff] [blame] | 35 | #include "content/shell/browser/layout_test/layout_test_javascript_dialog_manager.h" |
lukasza | 8e550a0 | 2016-04-18 18:34:17 | [diff] [blame] | 36 | #include "content/shell/browser/layout_test/secondary_test_window_observer.h" |
[email protected] | de7d61ff | 2013-08-20 11:30:41 | [diff] [blame] | 37 | #include "content/shell/browser/shell_browser_main_parts.h" |
| 38 | #include "content/shell/browser/shell_content_browser_client.h" |
| 39 | #include "content/shell/browser/shell_devtools_frontend.h" |
| 40 | #include "content/shell/browser/shell_javascript_dialog_manager.h" |
carlosk | d9d9794 | 2017-02-16 08:58:09 | [diff] [blame] | 41 | #include "content/shell/common/layout_test/layout_test_switches.h" |
[email protected] | b7c504c | 2013-05-07 14:42:12 | [diff] [blame] | 42 | #include "content/shell/common/shell_messages.h" |
| 43 | #include "content/shell/common/shell_switches.h" |
Brett Wilson | 0748bf41 | 2016-11-22 17:55:46 | [diff] [blame] | 44 | #include "media/media_features.h" |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 45 | |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 46 | namespace content { |
| 47 | |
pdr | cab84ee | 2015-03-13 21:47:04 | [diff] [blame] | 48 | const int kDefaultTestWindowWidthDip = 800; |
| 49 | const int kDefaultTestWindowHeightDip = 600; |
[email protected] | 1e57cab | 2013-05-28 04:26:11 | [diff] [blame] | 50 | |
[email protected] | e99ca511 | 2011-09-26 17:22:54 | [diff] [blame] | 51 | std::vector<Shell*> Shell::windows_; |
[email protected] | 9e00e635 | 2012-07-30 17:05:18 | [diff] [blame] | 52 | base::Callback<void(Shell*)> Shell::shell_created_callback_; |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 53 | |
[email protected] | 11a65b69 | 2012-03-30 11:29:16 | [diff] [blame] | 54 | bool Shell::quit_message_loop_ = true; |
| 55 | |
[email protected] | 7fff43e | 2013-05-21 20:21:10 | [diff] [blame] | 56 | class Shell::DevToolsWebContentsObserver : public WebContentsObserver { |
| 57 | public: |
| 58 | DevToolsWebContentsObserver(Shell* shell, WebContents* web_contents) |
| 59 | : WebContentsObserver(web_contents), |
| 60 | shell_(shell) { |
| 61 | } |
| 62 | |
| 63 | // WebContentsObserver |
dcheng | e933b3e | 2014-10-21 11:44:09 | [diff] [blame] | 64 | void WebContentsDestroyed() override { |
[email protected] | 7fff43e | 2013-05-21 20:21:10 | [diff] [blame] | 65 | shell_->OnDevToolsWebContentsDestroyed(); |
| 66 | } |
| 67 | |
| 68 | private: |
| 69 | Shell* shell_; |
| 70 | |
| 71 | DISALLOW_COPY_AND_ASSIGN(DevToolsWebContentsObserver); |
| 72 | }; |
| 73 | |
[email protected] | 0b659b3 | 2012-03-26 21:29:32 | [diff] [blame] | 74 | Shell::Shell(WebContents* web_contents) |
[email protected] | 1ef02d24 | 2013-10-07 16:18:53 | [diff] [blame] | 75 | : WebContentsObserver(web_contents), |
arthursonzogni | fdd4991 | 2017-08-31 08:55:26 | [diff] [blame] | 76 | web_contents_(web_contents), |
[email protected] | 1ef02d24 | 2013-10-07 16:18:53 | [diff] [blame] | 77 | devtools_frontend_(NULL), |
[email protected] | 001841c9 | 2012-12-11 17:00:13 | [diff] [blame] | 78 | is_fullscreen_(false), |
[email protected] | 99c014c | 2012-11-27 12:03:42 | [diff] [blame] | 79 | window_(NULL), |
sadrul | 8b11d26 | 2015-11-12 18:41:33 | [diff] [blame] | 80 | #if defined(OS_MACOSX) |
[email protected] | 86b36e67 | 2012-12-21 00:09:51 | [diff] [blame] | 81 | url_edit_view_(NULL), |
sadrul | 8b11d26 | 2015-11-12 18:41:33 | [diff] [blame] | 82 | #endif |
[email protected] | 86b36e67 | 2012-12-21 00:09:51 | [diff] [blame] | 83 | headless_(false) { |
arthursonzogni | fdd4991 | 2017-08-31 08:55:26 | [diff] [blame] | 84 | web_contents_->SetDelegate(this); |
| 85 | |
lukasza | 381b049 | 2016-03-10 16:48:43 | [diff] [blame] | 86 | if (switches::IsRunLayoutTestSwitchPresent()) |
[email protected] | 86b36e67 | 2012-12-21 00:09:51 | [diff] [blame] | 87 | headless_ = true; |
[email protected] | 9e00e635 | 2012-07-30 17:05:18 | [diff] [blame] | 88 | windows_.push_back(this); |
| 89 | |
| 90 | if (!shell_created_callback_.is_null()) { |
| 91 | shell_created_callback_.Run(this); |
| 92 | shell_created_callback_.Reset(); |
| 93 | } |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | Shell::~Shell() { |
| 97 | PlatformCleanUp(); |
[email protected] | e99ca511 | 2011-09-26 17:22:54 | [diff] [blame] | 98 | |
| 99 | for (size_t i = 0; i < windows_.size(); ++i) { |
| 100 | if (windows_[i] == this) { |
| 101 | windows_.erase(windows_.begin() + i); |
| 102 | break; |
| 103 | } |
| 104 | } |
[email protected] | 11a65b69 | 2012-03-30 11:29:16 | [diff] [blame] | 105 | |
[email protected] | b53adf45 | 2014-02-07 12:55:08 | [diff] [blame] | 106 | if (windows_.empty() && quit_message_loop_) { |
| 107 | if (headless_) |
| 108 | PlatformExit(); |
skyostil | 95082a6 | 2015-06-05 19:53:07 | [diff] [blame] | 109 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
ki.stfu | 80077924 | 2015-10-12 22:46:26 | [diff] [blame] | 110 | FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); |
[email protected] | b53adf45 | 2014-02-07 12:55:08 | [diff] [blame] | 111 | } |
arthursonzogni | fdd4991 | 2017-08-31 08:55:26 | [diff] [blame] | 112 | |
| 113 | web_contents_->SetDelegate(nullptr); |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 114 | } |
| 115 | |
[email protected] | c0d036d | 2013-05-07 12:50:50 | [diff] [blame] | 116 | Shell* Shell::CreateShell(WebContents* web_contents, |
| 117 | const gfx::Size& initial_size) { |
[email protected] | 0b659b3 | 2012-03-26 21:29:32 | [diff] [blame] | 118 | Shell* shell = new Shell(web_contents); |
[email protected] | c0d036d | 2013-05-07 12:50:50 | [diff] [blame] | 119 | shell->PlatformCreateWindow(initial_size.width(), initial_size.height()); |
[email protected] | 3fd8403 | 2012-01-12 18:20:17 | [diff] [blame] | 120 | |
[email protected] | 5cba3bf | 2012-01-14 02:40:35 | [diff] [blame] | 121 | shell->PlatformSetContents(); |
[email protected] | 3fd8403 | 2012-01-12 18:20:17 | [diff] [blame] | 122 | |
| 123 | shell->PlatformResizeSubViews(); |
[email protected] | 1596efb | 2013-01-17 22:13:01 | [diff] [blame] | 124 | |
creis | b6561df | 2016-02-11 20:20:54 | [diff] [blame] | 125 | // Note: Do not make RenderFrameHost or RenderViewHost specific state changes |
| 126 | // here, because they will be forgotten after a cross-process navigation. Use |
| 127 | // RenderFrameCreated or RenderViewCreated instead. |
lukasza | 381b049 | 2016-03-10 16:48:43 | [diff] [blame] | 128 | if (switches::IsRunLayoutTestSwitchPresent()) { |
[email protected] | 1596efb | 2013-01-17 22:13:01 | [diff] [blame] | 129 | web_contents->GetMutableRendererPrefs()->use_custom_colors = false; |
| 130 | web_contents->GetRenderViewHost()->SyncRendererPrefs(); |
| 131 | } |
| 132 | |
Brett Wilson | 0748bf41 | 2016-11-22 17:55:46 | [diff] [blame] | 133 | #if BUILDFLAG(ENABLE_WEBRTC) |
lukasza | 381b049 | 2016-03-10 16:48:43 | [diff] [blame] | 134 | base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
guoweis | 4ee4859 | 2015-12-02 06:37:07 | [diff] [blame] | 135 | if (command_line->HasSwitch(switches::kForceWebRtcIPHandlingPolicy)) { |
guoweis | c537ba2 | 2015-11-06 21:20:31 | [diff] [blame] | 136 | web_contents->GetMutableRendererPrefs()->webrtc_ip_handling_policy = |
guoweis | 4ee4859 | 2015-12-02 06:37:07 | [diff] [blame] | 137 | command_line->GetSwitchValueASCII( |
| 138 | switches::kForceWebRtcIPHandlingPolicy); |
guoweis | 8efb6d89 | 2015-10-12 18:26:17 | [diff] [blame] | 139 | } |
| 140 | #endif |
| 141 | |
[email protected] | 3fd8403 | 2012-01-12 18:20:17 | [diff] [blame] | 142 | return shell; |
| 143 | } |
| 144 | |
[email protected] | 11a65b69 | 2012-03-30 11:29:16 | [diff] [blame] | 145 | void Shell::CloseAllWindows() { |
[email protected] | 997ec9f | 2012-11-21 04:44:14 | [diff] [blame] | 146 | base::AutoReset<bool> auto_reset(&quit_message_loop_, false); |
[email protected] | b50452f | 2014-08-18 12:31:44 | [diff] [blame] | 147 | DevToolsAgentHost::DetachAllClients(); |
[email protected] | 11a65b69 | 2012-03-30 11:29:16 | [diff] [blame] | 148 | std::vector<Shell*> open_windows(windows_); |
| 149 | for (size_t i = 0; i < open_windows.size(); ++i) |
| 150 | open_windows[i]->Close(); |
fdoray | 896bea1 | 2016-06-10 15:52:01 | [diff] [blame] | 151 | base::RunLoop().RunUntilIdle(); |
dmazzoni | 948fbc7 | 2015-05-17 07:57:33 | [diff] [blame] | 152 | PlatformExit(); |
[email protected] | 11a65b69 | 2012-03-30 11:29:16 | [diff] [blame] | 153 | } |
| 154 | |
[email protected] | 9e00e635 | 2012-07-30 17:05:18 | [diff] [blame] | 155 | void Shell::SetShellCreatedCallback( |
| 156 | base::Callback<void(Shell*)> shell_created_callback) { |
| 157 | DCHECK(shell_created_callback_.is_null()); |
| 158 | shell_created_callback_ = shell_created_callback; |
| 159 | } |
| 160 | |
[email protected] | 74830f0 | 2012-01-30 22:27:04 | [diff] [blame] | 161 | Shell* Shell::FromRenderViewHost(RenderViewHost* rvh) { |
| 162 | for (size_t i = 0; i < windows_.size(); ++i) { |
[email protected] | 0b659b3 | 2012-03-26 21:29:32 | [diff] [blame] | 163 | if (windows_[i]->web_contents() && |
| 164 | windows_[i]->web_contents()->GetRenderViewHost() == rvh) { |
[email protected] | 74830f0 | 2012-01-30 22:27:04 | [diff] [blame] | 165 | return windows_[i]; |
| 166 | } |
| 167 | } |
| 168 | return NULL; |
| 169 | } |
| 170 | |
[email protected] | 6153b27 | 2013-01-25 22:29:23 | [diff] [blame] | 171 | // static |
| 172 | void Shell::Initialize() { |
pdr | cab84ee | 2015-03-13 21:47:04 | [diff] [blame] | 173 | PlatformInitialize(GetShellDefaultSize()); |
[email protected] | 6153b27 | 2013-01-25 22:29:23 | [diff] [blame] | 174 | } |
| 175 | |
[email protected] | a290409 | 2013-10-15 04:53:59 | [diff] [blame] | 176 | gfx::Size Shell::AdjustWindowSize(const gfx::Size& initial_size) { |
| 177 | if (!initial_size.IsEmpty()) |
| 178 | return initial_size; |
pdr | cab84ee | 2015-03-13 21:47:04 | [diff] [blame] | 179 | return GetShellDefaultSize(); |
[email protected] | a290409 | 2013-10-15 04:53:59 | [diff] [blame] | 180 | } |
| 181 | |
[email protected] | bdcf915 | 2012-07-19 17:43:21 | [diff] [blame] | 182 | Shell* Shell::CreateNewWindow(BrowserContext* browser_context, |
[email protected] | e99ca511 | 2011-09-26 17:22:54 | [diff] [blame] | 183 | const GURL& url, |
lukasza | 0413015 | 2016-10-21 20:26:32 | [diff] [blame] | 184 | const scoped_refptr<SiteInstance>& site_instance, |
[email protected] | cdb80672 | 2013-01-10 14:18:23 | [diff] [blame] | 185 | const gfx::Size& initial_size) { |
[email protected] | 54944cde | 2012-12-09 09:24:59 | [diff] [blame] | 186 | WebContents::CreateParams create_params(browser_context, site_instance); |
[email protected] | a290409 | 2013-10-15 04:53:59 | [diff] [blame] | 187 | create_params.initial_size = AdjustWindowSize(initial_size); |
[email protected] | 54944cde | 2012-12-09 09:24:59 | [diff] [blame] | 188 | WebContents* web_contents = WebContents::Create(create_params); |
[email protected] | c0d036d | 2013-05-07 12:50:50 | [diff] [blame] | 189 | Shell* shell = CreateShell(web_contents, create_params.initial_size); |
[email protected] | e99ca511 | 2011-09-26 17:22:54 | [diff] [blame] | 190 | if (!url.is_empty()) |
| 191 | shell->LoadURL(url); |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 192 | return shell; |
| 193 | } |
| 194 | |
| 195 | void Shell::LoadURL(const GURL& url) { |
[email protected] | d2494ff | 2013-02-20 08:22:37 | [diff] [blame] | 196 | LoadURLForFrame(url, std::string()); |
| 197 | } |
| 198 | |
| 199 | void Shell::LoadURLForFrame(const GURL& url, const std::string& frame_name) { |
| 200 | NavigationController::LoadURLParams params(url); |
Sylvain Defresne | c6ccc77d | 2014-09-19 10:19:35 | [diff] [blame] | 201 | params.transition_type = ui::PageTransitionFromInt( |
| 202 | ui::PAGE_TRANSITION_TYPED | ui::PAGE_TRANSITION_FROM_ADDRESS_BAR); |
[email protected] | d2494ff | 2013-02-20 08:22:37 | [diff] [blame] | 203 | params.frame_name = frame_name; |
| 204 | web_contents_->GetController().LoadURLWithParams(params); |
[email protected] | fc2b46b | 2014-05-03 16:33:45 | [diff] [blame] | 205 | web_contents_->Focus(); |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 206 | } |
| 207 | |
[email protected] | 76bdecb | 2014-04-16 17:58:08 | [diff] [blame] | 208 | void Shell::LoadDataWithBaseURL(const GURL& url, const std::string& data, |
| 209 | const GURL& base_url) { |
boliu | ec93ea9 | 2016-02-17 22:23:07 | [diff] [blame] | 210 | bool load_as_string = false; |
| 211 | LoadDataWithBaseURLInternal(url, data, base_url, load_as_string); |
| 212 | } |
| 213 | |
| 214 | #if defined(OS_ANDROID) |
| 215 | void Shell::LoadDataAsStringWithBaseURL(const GURL& url, |
| 216 | const std::string& data, |
| 217 | const GURL& base_url) { |
| 218 | bool load_as_string = true; |
| 219 | LoadDataWithBaseURLInternal(url, data, base_url, load_as_string); |
| 220 | } |
| 221 | #endif |
| 222 | |
| 223 | void Shell::LoadDataWithBaseURLInternal(const GURL& url, |
| 224 | const std::string& data, |
| 225 | const GURL& base_url, |
| 226 | bool load_as_string) { |
| 227 | #if !defined(OS_ANDROID) |
| 228 | DCHECK(!load_as_string); // Only supported on Android. |
| 229 | #endif |
| 230 | |
| 231 | NavigationController::LoadURLParams params(GURL::EmptyGURL()); |
| 232 | const std::string data_url_header = "data:text/html;charset=utf-8,"; |
| 233 | if (load_as_string) { |
| 234 | params.url = GURL(data_url_header); |
| 235 | std::string data_url_as_string = data_url_header + data; |
| 236 | #if defined(OS_ANDROID) |
| 237 | params.data_url_as_string = |
| 238 | base::RefCountedString::TakeString(&data_url_as_string); |
| 239 | #endif |
| 240 | } else { |
| 241 | params.url = GURL(data_url_header + data); |
| 242 | } |
| 243 | |
[email protected] | 76bdecb | 2014-04-16 17:58:08 | [diff] [blame] | 244 | params.load_type = NavigationController::LOAD_TYPE_DATA; |
| 245 | params.base_url_for_data_url = base_url; |
| 246 | params.virtual_url_for_data_url = url; |
| 247 | params.override_user_agent = NavigationController::UA_OVERRIDE_FALSE; |
| 248 | web_contents_->GetController().LoadURLWithParams(params); |
[email protected] | fc2b46b | 2014-05-03 16:33:45 | [diff] [blame] | 249 | web_contents_->Focus(); |
[email protected] | 76bdecb | 2014-04-16 17:58:08 | [diff] [blame] | 250 | } |
| 251 | |
[email protected] | a290409 | 2013-10-15 04:53:59 | [diff] [blame] | 252 | void Shell::AddNewContents(WebContents* source, |
| 253 | WebContents* new_contents, |
| 254 | WindowOpenDisposition disposition, |
bokan | 107a47f | 2015-02-03 23:23:39 | [diff] [blame] | 255 | const gfx::Rect& initial_rect, |
[email protected] | a290409 | 2013-10-15 04:53:59 | [diff] [blame] | 256 | bool user_gesture, |
| 257 | bool* was_blocked) { |
bokan | 107a47f | 2015-02-03 23:23:39 | [diff] [blame] | 258 | CreateShell(new_contents, AdjustWindowSize(initial_rect.size())); |
lukasza | 381b049 | 2016-03-10 16:48:43 | [diff] [blame] | 259 | if (switches::IsRunLayoutTestSwitchPresent()) |
lukasza | 8e550a0 | 2016-04-18 18:34:17 | [diff] [blame] | 260 | SecondaryTestWindowObserver::CreateForWebContents(new_contents); |
[email protected] | a290409 | 2013-10-15 04:53:59 | [diff] [blame] | 261 | } |
| 262 | |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 263 | void Shell::GoBackOrForward(int offset) { |
[email protected] | 0b659b3 | 2012-03-26 21:29:32 | [diff] [blame] | 264 | web_contents_->GetController().GoToOffset(offset); |
[email protected] | fc2b46b | 2014-05-03 16:33:45 | [diff] [blame] | 265 | web_contents_->Focus(); |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | void Shell::Reload() { |
toyoshim | 6142d96f | 2016-12-19 09:07:25 | [diff] [blame] | 269 | web_contents_->GetController().Reload(ReloadType::NORMAL, false); |
[email protected] | fc2b46b | 2014-05-03 16:33:45 | [diff] [blame] | 270 | web_contents_->Focus(); |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 271 | } |
| 272 | |
toyoshim | e5aaf6a | 2016-05-18 08:07:48 | [diff] [blame] | 273 | void Shell::ReloadBypassingCache() { |
toyoshim | 6142d96f | 2016-12-19 09:07:25 | [diff] [blame] | 274 | web_contents_->GetController().Reload(ReloadType::BYPASSING_CACHE, false); |
toyoshim | e5aaf6a | 2016-05-18 08:07:48 | [diff] [blame] | 275 | web_contents_->Focus(); |
| 276 | } |
| 277 | |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 278 | void Shell::Stop() { |
[email protected] | 0b659b3 | 2012-03-26 21:29:32 | [diff] [blame] | 279 | web_contents_->Stop(); |
[email protected] | fc2b46b | 2014-05-03 16:33:45 | [diff] [blame] | 280 | web_contents_->Focus(); |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 281 | } |
| 282 | |
[email protected] | e3b10d1 | 2014-03-28 16:06:09 | [diff] [blame] | 283 | void Shell::UpdateNavigationControls(bool to_different_document) { |
[email protected] | 0b659b3 | 2012-03-26 21:29:32 | [diff] [blame] | 284 | int current_index = web_contents_->GetController().GetCurrentEntryIndex(); |
| 285 | int max_index = web_contents_->GetController().GetEntryCount() - 1; |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 286 | |
| 287 | PlatformEnableUIControl(BACK_BUTTON, current_index > 0); |
| 288 | PlatformEnableUIControl(FORWARD_BUTTON, current_index < max_index); |
[email protected] | e3b10d1 | 2014-03-28 16:06:09 | [diff] [blame] | 289 | PlatformEnableUIControl(STOP_BUTTON, |
| 290 | to_different_document && web_contents_->IsLoading()); |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 291 | } |
| 292 | |
[email protected] | 7c17b699 | 2012-08-09 16:16:30 | [diff] [blame] | 293 | void Shell::ShowDevTools() { |
mohsen | 6eb57fb | 2016-07-22 03:14:08 | [diff] [blame] | 294 | if (!devtools_frontend_) { |
| 295 | devtools_frontend_ = ShellDevToolsFrontend::Show(web_contents()); |
| 296 | devtools_observer_.reset(new DevToolsWebContentsObserver( |
| 297 | this, devtools_frontend_->frontend_shell()->web_contents())); |
| 298 | } |
[email protected] | 3142e5d | 2014-02-07 00:54:46 | [diff] [blame] | 299 | |
mohsen | 6eb57fb | 2016-07-22 03:14:08 | [diff] [blame] | 300 | devtools_frontend_->Activate(); |
| 301 | devtools_frontend_->Focus(); |
[email protected] | 7c17b699 | 2012-08-09 16:16:30 | [diff] [blame] | 302 | } |
| 303 | |
[email protected] | 001841c9 | 2012-12-11 17:00:13 | [diff] [blame] | 304 | void Shell::CloseDevTools() { |
[email protected] | 0773e0c | 2013-01-25 15:57:57 | [diff] [blame] | 305 | if (!devtools_frontend_) |
[email protected] | 001841c9 | 2012-12-11 17:00:13 | [diff] [blame] | 306 | return; |
[email protected] | 7fff43e | 2013-05-21 20:21:10 | [diff] [blame] | 307 | devtools_observer_.reset(); |
[email protected] | 0773e0c | 2013-01-25 15:57:57 | [diff] [blame] | 308 | devtools_frontend_->Close(); |
| 309 | devtools_frontend_ = NULL; |
[email protected] | 001841c9 | 2012-12-11 17:00:13 | [diff] [blame] | 310 | } |
| 311 | |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 312 | gfx::NativeView Shell::GetContentView() { |
[email protected] | 59383c78 | 2013-04-17 16:43:27 | [diff] [blame] | 313 | if (!web_contents_) |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 314 | return NULL; |
[email protected] | fc2b46b | 2014-05-03 16:33:45 | [diff] [blame] | 315 | return web_contents_->GetNativeView(); |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 316 | } |
| 317 | |
alexmos | 5a98a05 | 2016-01-06 00:15:02 | [diff] [blame] | 318 | WebContents* Shell::OpenURLFromTab(WebContents* source, |
| 319 | const OpenURLParams& params) { |
lukasza | 0413015 | 2016-10-21 20:26:32 | [diff] [blame] | 320 | WebContents* target = nullptr; |
| 321 | switch (params.disposition) { |
| 322 | case WindowOpenDisposition::CURRENT_TAB: |
| 323 | target = source; |
| 324 | break; |
| 325 | |
| 326 | // Normally, the difference between NEW_POPUP and NEW_WINDOW is that a popup |
| 327 | // should have no toolbar, no status bar, no menu bar, no scrollbars and be |
| 328 | // not resizable. For simplicity and to enable new testing scenarios in |
| 329 | // content shell and layout tests, popups don't get special treatment below |
| 330 | // (i.e. they will have a toolbar and other things described here). |
| 331 | case WindowOpenDisposition::NEW_POPUP: |
lukasza | be2f0da | 2017-04-25 00:43:00 | [diff] [blame] | 332 | case WindowOpenDisposition::NEW_WINDOW: |
| 333 | // content_shell doesn't really support tabs, but some layout tests use |
| 334 | // middle click (which translates into kNavigationPolicyNewBackgroundTab), |
| 335 | // so we treat the cases below just like a NEW_WINDOW disposition. |
| 336 | case WindowOpenDisposition::NEW_BACKGROUND_TAB: |
| 337 | case WindowOpenDisposition::NEW_FOREGROUND_TAB: { |
lukasza | 0413015 | 2016-10-21 20:26:32 | [diff] [blame] | 338 | Shell* new_window = |
| 339 | Shell::CreateNewWindow(source->GetBrowserContext(), |
| 340 | GURL(), // Don't load anything just yet. |
| 341 | params.source_site_instance, |
| 342 | gfx::Size()); // Use default size. |
| 343 | target = new_window->web_contents(); |
| 344 | if (switches::IsRunLayoutTestSwitchPresent()) |
| 345 | SecondaryTestWindowObserver::CreateForWebContents(target); |
| 346 | break; |
| 347 | } |
| 348 | |
| 349 | // No tabs in content_shell: |
| 350 | case WindowOpenDisposition::SINGLETON_TAB: |
lukasza | 0413015 | 2016-10-21 20:26:32 | [diff] [blame] | 351 | // No incognito mode in content_shell: |
| 352 | case WindowOpenDisposition::OFF_THE_RECORD: |
| 353 | // TODO(lukasza): Investigate if some layout tests might need support for |
| 354 | // SAVE_TO_DISK disposition. This would probably require that |
| 355 | // BlinkTestController always sets up and cleans up a temporary directory |
| 356 | // as the default downloads destinations for the duration of a test. |
| 357 | case WindowOpenDisposition::SAVE_TO_DISK: |
| 358 | // Ignoring requests with disposition == IGNORE_ACTION... |
| 359 | case WindowOpenDisposition::IGNORE_ACTION: |
| 360 | default: |
| 361 | return nullptr; |
| 362 | } |
alexmos | 5a98a05 | 2016-01-06 00:15:02 | [diff] [blame] | 363 | |
| 364 | NavigationController::LoadURLParams load_url_params(params.url); |
| 365 | load_url_params.source_site_instance = params.source_site_instance; |
| 366 | load_url_params.transition_type = params.transition; |
| 367 | load_url_params.frame_tree_node_id = params.frame_tree_node_id; |
| 368 | load_url_params.referrer = params.referrer; |
| 369 | load_url_params.redirect_chain = params.redirect_chain; |
| 370 | load_url_params.extra_headers = params.extra_headers; |
| 371 | load_url_params.is_renderer_initiated = params.is_renderer_initiated; |
alexmos | 5a98a05 | 2016-01-06 00:15:02 | [diff] [blame] | 372 | load_url_params.should_replace_current_entry = |
| 373 | params.should_replace_current_entry; |
| 374 | |
lukasza | 477a5a2 | 2016-06-16 18:28:43 | [diff] [blame] | 375 | if (params.uses_post) { |
| 376 | load_url_params.load_type = NavigationController::LOAD_TYPE_HTTP_POST; |
| 377 | load_url_params.post_data = params.post_data; |
alexmos | 5a98a05 | 2016-01-06 00:15:02 | [diff] [blame] | 378 | } |
| 379 | |
lukasza | 0413015 | 2016-10-21 20:26:32 | [diff] [blame] | 380 | target->GetController().LoadURLWithParams(load_url_params); |
| 381 | return target; |
alexmos | 5a98a05 | 2016-01-06 00:15:02 | [diff] [blame] | 382 | } |
| 383 | |
[email protected] | e3b10d1 | 2014-03-28 16:06:09 | [diff] [blame] | 384 | void Shell::LoadingStateChanged(WebContents* source, |
| 385 | bool to_different_document) { |
| 386 | UpdateNavigationControls(to_different_document); |
[email protected] | 15aea41 | 2012-01-19 03:18:50 | [diff] [blame] | 387 | PlatformSetIsLoading(source->IsLoading()); |
[email protected] | e99ca511 | 2011-09-26 17:22:54 | [diff] [blame] | 388 | } |
| 389 | |
mlamouri | 7a78d6fd | 2015-01-17 13:23:53 | [diff] [blame] | 390 | void Shell::EnterFullscreenModeForTab(WebContents* web_contents, |
| 391 | const GURL& origin) { |
| 392 | ToggleFullscreenModeForTab(web_contents, true); |
| 393 | } |
| 394 | |
| 395 | void Shell::ExitFullscreenModeForTab(WebContents* web_contents) { |
| 396 | ToggleFullscreenModeForTab(web_contents, false); |
| 397 | } |
| 398 | |
[email protected] | 99c014c | 2012-11-27 12:03:42 | [diff] [blame] | 399 | void Shell::ToggleFullscreenModeForTab(WebContents* web_contents, |
| 400 | bool enter_fullscreen) { |
| 401 | #if defined(OS_ANDROID) |
| 402 | PlatformToggleFullscreenModeForTab(web_contents, enter_fullscreen); |
| 403 | #endif |
lukasza | 381b049 | 2016-03-10 16:48:43 | [diff] [blame] | 404 | if (!switches::IsRunLayoutTestSwitchPresent()) |
[email protected] | 99c014c | 2012-11-27 12:03:42 | [diff] [blame] | 405 | return; |
| 406 | if (is_fullscreen_ != enter_fullscreen) { |
| 407 | is_fullscreen_ = enter_fullscreen; |
avi | f9ab5d94 | 2015-10-15 14:05:44 | [diff] [blame] | 408 | web_contents->GetRenderViewHost()->GetWidget()->WasResized(); |
[email protected] | 99c014c | 2012-11-27 12:03:42 | [diff] [blame] | 409 | } |
| 410 | } |
| 411 | |
| 412 | bool Shell::IsFullscreenForTabOrPending(const WebContents* web_contents) const { |
| 413 | #if defined(OS_ANDROID) |
| 414 | return PlatformIsFullscreenForTabOrPending(web_contents); |
| 415 | #else |
| 416 | return is_fullscreen_; |
| 417 | #endif |
| 418 | } |
| 419 | |
mikhail.pozdnyakov | c0e251b | 2015-04-15 06:51:12 | [diff] [blame] | 420 | blink::WebDisplayMode Shell::GetDisplayMode( |
| 421 | const WebContents* web_contents) const { |
| 422 | // TODO : should return blink::WebDisplayModeFullscreen wherever user puts |
| 423 | // a browser window into fullscreen (not only in case of renderer-initiated |
| 424 | // fullscreen mode): crbug.com/476874. |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 425 | return IsFullscreenForTabOrPending(web_contents) |
| 426 | ? blink::kWebDisplayModeFullscreen |
| 427 | : blink::kWebDisplayModeBrowser; |
mikhail.pozdnyakov | c0e251b | 2015-04-15 06:51:12 | [diff] [blame] | 428 | } |
| 429 | |
[email protected] | f7843900 | 2012-11-28 14:45:59 | [diff] [blame] | 430 | void Shell::RequestToLockMouse(WebContents* web_contents, |
| 431 | bool user_gesture, |
| 432 | bool last_unlocked_by_target) { |
| 433 | web_contents->GotResponseToLockMouseRequest(true); |
| 434 | } |
| 435 | |
[email protected] | 9e00e635 | 2012-07-30 17:05:18 | [diff] [blame] | 436 | void Shell::CloseContents(WebContents* source) { |
| 437 | Close(); |
| 438 | } |
| 439 | |
[email protected] | 06731026 | 2012-11-22 14:30:41 | [diff] [blame] | 440 | bool Shell::CanOverscrollContent() const { |
| 441 | #if defined(USE_AURA) |
| 442 | return true; |
| 443 | #else |
| 444 | return false; |
| 445 | #endif |
| 446 | } |
| 447 | |
[email protected] | 3bbacc5b | 2012-04-17 17:46:15 | [diff] [blame] | 448 | void Shell::DidNavigateMainFramePostCommit(WebContents* web_contents) { |
[email protected] | 4e3c752 | 2013-08-13 11:53:18 | [diff] [blame] | 449 | PlatformSetAddressBarURL(web_contents->GetLastCommittedURL()); |
[email protected] | e99ca511 | 2011-09-26 17:22:54 | [diff] [blame] | 450 | } |
| 451 | |
mathiash | 72a5e46 | 2014-11-19 08:18:50 | [diff] [blame] | 452 | JavaScriptDialogManager* Shell::GetJavaScriptDialogManager( |
| 453 | WebContents* source) { |
mkwst | 6efe7f2e6 | 2014-10-08 15:09:01 | [diff] [blame] | 454 | if (!dialog_manager_) { |
lukasza | 381b049 | 2016-03-10 16:48:43 | [diff] [blame] | 455 | dialog_manager_.reset(switches::IsRunLayoutTestSwitchPresent() |
| 456 | ? new LayoutTestJavaScriptDialogManager |
| 457 | : new ShellJavaScriptDialogManager); |
mkwst | 6efe7f2e6 | 2014-10-08 15:09:01 | [diff] [blame] | 458 | } |
[email protected] | 71a88bb | 2013-02-01 22:05:15 | [diff] [blame] | 459 | return dialog_manager_.get(); |
[email protected] | f221002 | 2012-03-29 00:36:08 | [diff] [blame] | 460 | } |
| 461 | |
dcheng | 6003e0b | 2016-04-09 18:42:34 | [diff] [blame] | 462 | std::unique_ptr<BluetoothChooser> Shell::RunBluetoothChooser( |
jyasskin | 93649a5 | 2016-02-17 19:57:02 | [diff] [blame] | 463 | RenderFrameHost* frame, |
| 464 | const BluetoothChooser::EventHandler& event_handler) { |
lukasza | 381b049 | 2016-03-10 16:48:43 | [diff] [blame] | 465 | if (switches::IsRunLayoutTestSwitchPresent()) { |
jyasskin | 93649a5 | 2016-02-17 19:57:02 | [diff] [blame] | 466 | return BlinkTestController::Get()->RunBluetoothChooser(frame, |
| 467 | event_handler); |
jyasskin | 8057c00 | 2015-09-10 19:09:53 | [diff] [blame] | 468 | } |
| 469 | return nullptr; |
| 470 | } |
| 471 | |
avi | a90ae4e | 2016-11-11 20:49:33 | [diff] [blame] | 472 | bool Shell::DidAddMessageToConsole(WebContents* source, |
| 473 | int32_t level, |
| 474 | const base::string16& message, |
| 475 | int32_t line_no, |
| 476 | const base::string16& source_id) { |
lukasza | 381b049 | 2016-03-10 16:48:43 | [diff] [blame] | 477 | return switches::IsRunLayoutTestSwitchPresent(); |
[email protected] | efb5f57 | 2012-01-29 10:57:33 | [diff] [blame] | 478 | } |
| 479 | |
dtapuska | e99ec6e5 | 2016-10-18 00:15:42 | [diff] [blame] | 480 | void Shell::RendererUnresponsive( |
| 481 | WebContents* source, |
| 482 | const WebContentsUnresponsiveState& unresponsive_state) { |
lukasza | 381b049 | 2016-03-10 16:48:43 | [diff] [blame] | 483 | if (switches::IsRunLayoutTestSwitchPresent()) |
| 484 | BlinkTestController::Get()->RendererUnresponsive(); |
[email protected] | 5bf68f2 | 2012-08-31 07:38:10 | [diff] [blame] | 485 | } |
| 486 | |
[email protected] | 233567d | 2013-02-27 20:22:02 | [diff] [blame] | 487 | void Shell::ActivateContents(WebContents* contents) { |
avi | f9ab5d94 | 2015-10-15 14:05:44 | [diff] [blame] | 488 | contents->GetRenderViewHost()->GetWidget()->Focus(); |
[email protected] | 233567d | 2013-02-27 20:22:02 | [diff] [blame] | 489 | } |
| 490 | |
carlosk | d9d9794 | 2017-02-16 08:58:09 | [diff] [blame] | 491 | bool Shell::ShouldAllowRunningInsecureContent( |
| 492 | content::WebContents* web_contents, |
| 493 | bool allowed_per_prefs, |
| 494 | const url::Origin& origin, |
| 495 | const GURL& resource_url) { |
| 496 | bool allowed_by_test = false; |
| 497 | if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 498 | switches::kRunLayoutTest)) { |
| 499 | const base::DictionaryValue& test_flags = |
| 500 | BlinkTestController::Get() |
| 501 | ->accumulated_layout_test_runtime_flags_changes(); |
| 502 | test_flags.GetBoolean("running_insecure_content_allowed", &allowed_by_test); |
| 503 | } |
| 504 | |
| 505 | return allowed_per_prefs || allowed_by_test; |
| 506 | } |
| 507 | |
pdr | cab84ee | 2015-03-13 21:47:04 | [diff] [blame] | 508 | gfx::Size Shell::GetShellDefaultSize() { |
| 509 | static gfx::Size default_shell_size; |
| 510 | if (!default_shell_size.IsEmpty()) |
| 511 | return default_shell_size; |
| 512 | base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
| 513 | if (command_line->HasSwitch(switches::kContentShellHostWindowSize)) { |
| 514 | const std::string size_str = command_line->GetSwitchValueASCII( |
| 515 | switches::kContentShellHostWindowSize); |
| 516 | int width, height; |
| 517 | CHECK_EQ(2, sscanf(size_str.c_str(), "%dx%d", &width, &height)); |
| 518 | default_shell_size = gfx::Size(width, height); |
| 519 | } else { |
| 520 | default_shell_size = gfx::Size( |
| 521 | kDefaultTestWindowWidthDip, kDefaultTestWindowHeightDip); |
| 522 | } |
| 523 | return default_shell_size; |
| 524 | } |
| 525 | |
Avi Drissman | 9300221 | 2017-09-27 03:20:52 | [diff] [blame^] | 526 | void Shell::TitleWasSet(NavigationEntry* entry) { |
[email protected] | 1ef02d24 | 2013-10-07 16:18:53 | [diff] [blame] | 527 | if (entry) |
| 528 | PlatformSetTitle(entry->GetTitle()); |
[email protected] | aecc085b | 2012-06-01 18:15:53 | [diff] [blame] | 529 | } |
| 530 | |
[email protected] | 7fff43e | 2013-05-21 20:21:10 | [diff] [blame] | 531 | void Shell::OnDevToolsWebContentsDestroyed() { |
| 532 | devtools_observer_.reset(); |
| 533 | devtools_frontend_ = NULL; |
| 534 | } |
| 535 | |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 536 | } // namespace content |