blob: a921a30f5322452f6addc8316ebd9a18efb0b5bc [file] [log] [blame]
[email protected]5cba3bf2012-01-14 02:40:351// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]9fbd3f862011-09-20 23:31:342// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "content/shell/shell.h"
6
[email protected]11a65b692012-03-30 11:29:167#include "base/auto_reset.h"
[email protected]efb5f572012-01-29 10:57:338#include "base/command_line.h"
[email protected]9fbd3f862011-09-20 23:31:349#include "base/message_loop.h"
10#include "base/path_service.h"
[email protected]7c17b6992012-08-09 16:16:3011#include "base/string_number_conversions.h"
[email protected]9fbd3f862011-09-20 23:31:3412#include "base/string_util.h"
[email protected]b7c504c2013-05-07 14:42:1213#include "base/stringprintf.h"
[email protected]74ebfb12013-06-07 20:48:0014#include "base/strings/utf_string_conversions.h"
[email protected]e641ea692013-01-28 10:39:2315#include "content/public/browser/devtools_manager.h"
[email protected]0b659b32012-03-26 21:29:3216#include "content/public/browser/navigation_controller.h"
[email protected]b7c504c2013-05-07 14:42:1217#include "content/public/browser/navigation_entry.h"
[email protected]aecc085b2012-06-01 18:15:5318#include "content/public/browser/notification_details.h"
19#include "content/public/browser/notification_source.h"
20#include "content/public/browser/notification_types.h"
[email protected]0b659b32012-03-26 21:29:3221#include "content/public/browser/render_view_host.h"
22#include "content/public/browser/web_contents.h"
[email protected]7fff43e2013-05-21 20:21:1023#include "content/public/browser/web_contents_observer.h"
[email protected]ed245db2012-12-18 08:00:4524#include "content/public/browser/web_contents_view.h"
[email protected]1596efb2013-01-17 22:13:0125#include "content/public/common/renderer_preferences.h"
[email protected]b7c504c2013-05-07 14:42:1226#include "content/shell/common/shell_messages.h"
27#include "content/shell/common/shell_switches.h"
[email protected]1ca4fac2013-04-08 14:15:1128#include "content/shell/notify_done_forwarder.h"
[email protected]7c17b6992012-08-09 16:16:3029#include "content/shell/shell_browser_main_parts.h"
30#include "content/shell/shell_content_browser_client.h"
[email protected]0773e0c2013-01-25 15:57:5731#include "content/shell/shell_devtools_frontend.h"
[email protected]71a88bb2013-02-01 22:05:1532#include "content/shell/shell_javascript_dialog_manager.h"
[email protected]8dafad32012-12-10 14:11:4533#include "content/shell/webkit_test_controller.h"
[email protected]9fbd3f862011-09-20 23:31:3434
[email protected]9fbd3f862011-09-20 23:31:3435namespace content {
36
[email protected]1e57cab2013-05-28 04:26:1137const int Shell::kDefaultTestWindowWidthDip = 800;
38const int Shell::kDefaultTestWindowHeightDip = 600;
39
[email protected]e99ca5112011-09-26 17:22:5440std::vector<Shell*> Shell::windows_;
[email protected]9e00e6352012-07-30 17:05:1841base::Callback<void(Shell*)> Shell::shell_created_callback_;
[email protected]9fbd3f862011-09-20 23:31:3442
[email protected]11a65b692012-03-30 11:29:1643bool Shell::quit_message_loop_ = true;
44
[email protected]7fff43e2013-05-21 20:21:1045class Shell::DevToolsWebContentsObserver : public WebContentsObserver {
46 public:
47 DevToolsWebContentsObserver(Shell* shell, WebContents* web_contents)
48 : WebContentsObserver(web_contents),
49 shell_(shell) {
50 }
51
52 // WebContentsObserver
53 virtual void WebContentsDestroyed(WebContents* web_contents) OVERRIDE {
54 shell_->OnDevToolsWebContentsDestroyed();
55 }
56
57 private:
58 Shell* shell_;
59
60 DISALLOW_COPY_AND_ASSIGN(DevToolsWebContentsObserver);
61};
62
[email protected]0b659b32012-03-26 21:29:3263Shell::Shell(WebContents* web_contents)
[email protected]0773e0c2013-01-25 15:57:5764 : devtools_frontend_(NULL),
[email protected]001841c92012-12-11 17:00:1365 is_fullscreen_(false),
[email protected]99c014c2012-11-27 12:03:4266 window_(NULL),
[email protected]86b36e672012-12-21 00:09:5167 url_edit_view_(NULL),
[email protected]fa4a45832012-04-12 21:32:4868#if defined(OS_WIN) && !defined(USE_AURA)
[email protected]86b36e672012-12-21 00:09:5169 default_edit_wnd_proc_(0),
[email protected]9fbd3f862011-09-20 23:31:3470#endif
[email protected]86b36e672012-12-21 00:09:5171 headless_(false) {
72 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
73 if (command_line.HasSwitch(switches::kDumpRenderTree) &&
74 !command_line.HasSwitch(switches::kDisableHeadlessForLayoutTests)) {
75 headless_ = true;
76 }
[email protected]9e00e6352012-07-30 17:05:1877 registrar_.Add(this, NOTIFICATION_WEB_CONTENTS_TITLE_UPDATED,
78 Source<WebContents>(web_contents));
79 windows_.push_back(this);
80
81 if (!shell_created_callback_.is_null()) {
82 shell_created_callback_.Run(this);
83 shell_created_callback_.Reset();
84 }
[email protected]9fbd3f862011-09-20 23:31:3485}
86
87Shell::~Shell() {
88 PlatformCleanUp();
[email protected]e99ca5112011-09-26 17:22:5489
90 for (size_t i = 0; i < windows_.size(); ++i) {
91 if (windows_[i] == this) {
92 windows_.erase(windows_.begin() + i);
93 break;
94 }
95 }
[email protected]11a65b692012-03-30 11:29:1696
97 if (windows_.empty() && quit_message_loop_)
[email protected]dd32b1272013-05-04 14:17:1198 base::MessageLoop::current()->PostTask(FROM_HERE,
99 base::MessageLoop::QuitClosure());
[email protected]9fbd3f862011-09-20 23:31:34100}
101
[email protected]c0d036d2013-05-07 12:50:50102Shell* Shell::CreateShell(WebContents* web_contents,
103 const gfx::Size& initial_size) {
[email protected]0b659b32012-03-26 21:29:32104 Shell* shell = new Shell(web_contents);
[email protected]c0d036d2013-05-07 12:50:50105 shell->PlatformCreateWindow(initial_size.width(), initial_size.height());
[email protected]3fd84032012-01-12 18:20:17106
[email protected]0b659b32012-03-26 21:29:32107 shell->web_contents_.reset(web_contents);
108 web_contents->SetDelegate(shell);
[email protected]3fd84032012-01-12 18:20:17109
[email protected]5cba3bf2012-01-14 02:40:35110 shell->PlatformSetContents();
[email protected]3fd84032012-01-12 18:20:17111
112 shell->PlatformResizeSubViews();
[email protected]1596efb2013-01-17 22:13:01113
114 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree)) {
115 web_contents->GetMutableRendererPrefs()->use_custom_colors = false;
116 web_contents->GetRenderViewHost()->SyncRendererPrefs();
117 }
118
[email protected]3fd84032012-01-12 18:20:17119 return shell;
120}
121
[email protected]11a65b692012-03-30 11:29:16122void Shell::CloseAllWindows() {
[email protected]997ec9f2012-11-21 04:44:14123 base::AutoReset<bool> auto_reset(&quit_message_loop_, false);
[email protected]e641ea692013-01-28 10:39:23124 DevToolsManager::GetInstance()->CloseAllClientHosts();
[email protected]11a65b692012-03-30 11:29:16125 std::vector<Shell*> open_windows(windows_);
126 for (size_t i = 0; i < open_windows.size(); ++i)
127 open_windows[i]->Close();
[email protected]dd32b1272013-05-04 14:17:11128 base::MessageLoop::current()->RunUntilIdle();
[email protected]11a65b692012-03-30 11:29:16129}
130
[email protected]9e00e6352012-07-30 17:05:18131void Shell::SetShellCreatedCallback(
132 base::Callback<void(Shell*)> shell_created_callback) {
133 DCHECK(shell_created_callback_.is_null());
134 shell_created_callback_ = shell_created_callback;
135}
136
[email protected]74830f02012-01-30 22:27:04137Shell* Shell::FromRenderViewHost(RenderViewHost* rvh) {
138 for (size_t i = 0; i < windows_.size(); ++i) {
[email protected]0b659b32012-03-26 21:29:32139 if (windows_[i]->web_contents() &&
140 windows_[i]->web_contents()->GetRenderViewHost() == rvh) {
[email protected]74830f02012-01-30 22:27:04141 return windows_[i];
142 }
143 }
144 return NULL;
145}
146
[email protected]6153b272013-01-25 22:29:23147// static
148void Shell::Initialize() {
[email protected]1e57cab2013-05-28 04:26:11149 PlatformInitialize(
150 gfx::Size(kDefaultTestWindowWidthDip, kDefaultTestWindowHeightDip));
[email protected]6153b272013-01-25 22:29:23151}
152
[email protected]bdcf9152012-07-19 17:43:21153Shell* Shell::CreateNewWindow(BrowserContext* browser_context,
[email protected]e99ca5112011-09-26 17:22:54154 const GURL& url,
155 SiteInstance* site_instance,
156 int routing_id,
[email protected]cdb806722013-01-10 14:18:23157 const gfx::Size& initial_size) {
[email protected]54944cde2012-12-09 09:24:59158 WebContents::CreateParams create_params(browser_context, site_instance);
159 create_params.routing_id = routing_id;
[email protected]cdb806722013-01-10 14:18:23160 if (!initial_size.IsEmpty())
161 create_params.initial_size = initial_size;
162 else
[email protected]1e57cab2013-05-28 04:26:11163 create_params.initial_size =
164 gfx::Size(kDefaultTestWindowWidthDip, kDefaultTestWindowHeightDip);
[email protected]54944cde2012-12-09 09:24:59165 WebContents* web_contents = WebContents::Create(create_params);
[email protected]c0d036d2013-05-07 12:50:50166 Shell* shell = CreateShell(web_contents, create_params.initial_size);
[email protected]e99ca5112011-09-26 17:22:54167 if (!url.is_empty())
168 shell->LoadURL(url);
[email protected]9fbd3f862011-09-20 23:31:34169 return shell;
170}
171
172void Shell::LoadURL(const GURL& url) {
[email protected]d2494ff2013-02-20 08:22:37173 LoadURLForFrame(url, std::string());
174}
175
176void Shell::LoadURLForFrame(const GURL& url, const std::string& frame_name) {
177 NavigationController::LoadURLParams params(url);
178 params.transition_type = PageTransitionFromInt(
179 PAGE_TRANSITION_TYPED | PAGE_TRANSITION_FROM_ADDRESS_BAR);
180 params.frame_name = frame_name;
181 web_contents_->GetController().LoadURLWithParams(params);
[email protected]f3615f02013-02-26 06:09:06182 web_contents_->GetView()->Focus();
[email protected]9fbd3f862011-09-20 23:31:34183}
184
185void Shell::GoBackOrForward(int offset) {
[email protected]0b659b32012-03-26 21:29:32186 web_contents_->GetController().GoToOffset(offset);
[email protected]f3615f02013-02-26 06:09:06187 web_contents_->GetView()->Focus();
[email protected]9fbd3f862011-09-20 23:31:34188}
189
190void Shell::Reload() {
[email protected]0b659b32012-03-26 21:29:32191 web_contents_->GetController().Reload(false);
[email protected]f3615f02013-02-26 06:09:06192 web_contents_->GetView()->Focus();
[email protected]9fbd3f862011-09-20 23:31:34193}
194
195void Shell::Stop() {
[email protected]0b659b32012-03-26 21:29:32196 web_contents_->Stop();
[email protected]f3615f02013-02-26 06:09:06197 web_contents_->GetView()->Focus();
[email protected]9fbd3f862011-09-20 23:31:34198}
199
200void Shell::UpdateNavigationControls() {
[email protected]0b659b32012-03-26 21:29:32201 int current_index = web_contents_->GetController().GetCurrentEntryIndex();
202 int max_index = web_contents_->GetController().GetEntryCount() - 1;
[email protected]9fbd3f862011-09-20 23:31:34203
204 PlatformEnableUIControl(BACK_BUTTON, current_index > 0);
205 PlatformEnableUIControl(FORWARD_BUTTON, current_index < max_index);
[email protected]0b659b32012-03-26 21:29:32206 PlatformEnableUIControl(STOP_BUTTON, web_contents_->IsLoading());
[email protected]9fbd3f862011-09-20 23:31:34207}
208
[email protected]7c17b6992012-08-09 16:16:30209void Shell::ShowDevTools() {
[email protected]0773e0c2013-01-25 15:57:57210 if (devtools_frontend_) {
211 devtools_frontend_->Focus();
[email protected]001841c92012-12-11 17:00:13212 return;
213 }
[email protected]0773e0c2013-01-25 15:57:57214 devtools_frontend_ = ShellDevToolsFrontend::Show(web_contents());
[email protected]7fff43e2013-05-21 20:21:10215 devtools_observer_.reset(new DevToolsWebContentsObserver(
216 this, devtools_frontend_->frontend_shell()->web_contents()));
[email protected]7c17b6992012-08-09 16:16:30217}
218
[email protected]001841c92012-12-11 17:00:13219void Shell::CloseDevTools() {
[email protected]0773e0c2013-01-25 15:57:57220 if (!devtools_frontend_)
[email protected]001841c92012-12-11 17:00:13221 return;
[email protected]7fff43e2013-05-21 20:21:10222 devtools_observer_.reset();
[email protected]0773e0c2013-01-25 15:57:57223 devtools_frontend_->Close();
224 devtools_frontend_ = NULL;
[email protected]001841c92012-12-11 17:00:13225}
226
[email protected]9fbd3f862011-09-20 23:31:34227gfx::NativeView Shell::GetContentView() {
[email protected]59383c782013-04-17 16:43:27228 if (!web_contents_)
[email protected]9fbd3f862011-09-20 23:31:34229 return NULL;
[email protected]f3615f02013-02-26 06:09:06230 return web_contents_->GetView()->GetNativeView();
[email protected]9fbd3f862011-09-20 23:31:34231}
232
[email protected]9e00e6352012-07-30 17:05:18233WebContents* Shell::OpenURLFromTab(WebContents* source,
234 const OpenURLParams& params) {
235 // The only one we implement for now.
236 DCHECK(params.disposition == CURRENT_TAB);
237 source->GetController().LoadURL(
238 params.url, params.referrer, params.transition, std::string());
239 return source;
240}
241
[email protected]2a6bc3e2011-12-28 23:51:33242void Shell::LoadingStateChanged(WebContents* source) {
[email protected]e99ca5112011-09-26 17:22:54243 UpdateNavigationControls();
[email protected]15aea412012-01-19 03:18:50244 PlatformSetIsLoading(source->IsLoading());
[email protected]e99ca5112011-09-26 17:22:54245}
246
[email protected]99c014c2012-11-27 12:03:42247void Shell::ToggleFullscreenModeForTab(WebContents* web_contents,
248 bool enter_fullscreen) {
249#if defined(OS_ANDROID)
250 PlatformToggleFullscreenModeForTab(web_contents, enter_fullscreen);
251#endif
252 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree))
253 return;
254 if (is_fullscreen_ != enter_fullscreen) {
255 is_fullscreen_ = enter_fullscreen;
256 web_contents->GetRenderViewHost()->WasResized();
257 }
258}
259
260bool Shell::IsFullscreenForTabOrPending(const WebContents* web_contents) const {
261#if defined(OS_ANDROID)
262 return PlatformIsFullscreenForTabOrPending(web_contents);
263#else
264 return is_fullscreen_;
265#endif
266}
267
[email protected]f78439002012-11-28 14:45:59268void Shell::RequestToLockMouse(WebContents* web_contents,
269 bool user_gesture,
270 bool last_unlocked_by_target) {
271 web_contents->GotResponseToLockMouseRequest(true);
272}
273
[email protected]9e00e6352012-07-30 17:05:18274void Shell::CloseContents(WebContents* source) {
275 Close();
276}
277
[email protected]067310262012-11-22 14:30:41278bool Shell::CanOverscrollContent() const {
279#if defined(USE_AURA)
280 return true;
281#else
282 return false;
283#endif
284}
285
[email protected]3fd84032012-01-12 18:20:17286void Shell::WebContentsCreated(WebContents* source_contents,
287 int64 source_frame_id,
[email protected]50de3222013-03-20 15:36:13288 const string16& frame_name,
[email protected]3fd84032012-01-12 18:20:17289 const GURL& target_url,
290 WebContents* new_contents) {
[email protected]c0d036d2013-05-07 12:50:50291 CreateShell(new_contents, source_contents->GetView()->GetContainerSize());
[email protected]1ca4fac2013-04-08 14:15:11292 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree))
293 NotifyDoneForwarder::CreateForWebContents(new_contents);
[email protected]3fd84032012-01-12 18:20:17294}
295
[email protected]3bbacc5b2012-04-17 17:46:15296void Shell::DidNavigateMainFramePostCommit(WebContents* web_contents) {
297 PlatformSetAddressBarURL(web_contents->GetURL());
[email protected]e99ca5112011-09-26 17:22:54298}
299
[email protected]71a88bb2013-02-01 22:05:15300JavaScriptDialogManager* Shell::GetJavaScriptDialogManager() {
[email protected]59383c782013-04-17 16:43:27301 if (!dialog_manager_)
[email protected]71a88bb2013-02-01 22:05:15302 dialog_manager_.reset(new ShellJavaScriptDialogManager());
303 return dialog_manager_.get();
[email protected]f2210022012-03-29 00:36:08304}
305
[email protected]c272c5b2012-06-06 09:01:06306bool Shell::AddMessageToConsole(WebContents* source,
307 int32 level,
308 const string16& message,
309 int32 line_no,
310 const string16& source_id) {
[email protected]55a5cd12013-01-28 15:24:23311 return CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree);
[email protected]efb5f572012-01-29 10:57:33312}
313
[email protected]5bf68f22012-08-31 07:38:10314void Shell::RendererUnresponsive(WebContents* source) {
315 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree))
316 return;
317 WebKitTestController::Get()->RendererUnresponsive();
318}
319
[email protected]233567d2013-02-27 20:22:02320void Shell::ActivateContents(WebContents* contents) {
321 contents->GetRenderViewHost()->Focus();
322}
323
324void Shell::DeactivateContents(WebContents* contents) {
325 contents->GetRenderViewHost()->Blur();
326}
327
[email protected]aecc085b2012-06-01 18:15:53328void Shell::Observe(int type,
329 const NotificationSource& source,
330 const NotificationDetails& details) {
331 if (type == NOTIFICATION_WEB_CONTENTS_TITLE_UPDATED) {
332 std::pair<NavigationEntry*, bool>* title =
333 Details<std::pair<NavigationEntry*, bool> >(details).ptr();
334
335 if (title->first) {
336 string16 text = title->first->GetTitle();
337 PlatformSetTitle(text);
338 }
[email protected]d212efd2013-04-15 13:03:06339 } else {
340 NOTREACHED();
[email protected]aecc085b2012-06-01 18:15:53341 }
342}
343
[email protected]7fff43e2013-05-21 20:21:10344void Shell::OnDevToolsWebContentsDestroyed() {
345 devtools_observer_.reset();
346 devtools_frontend_ = NULL;
347}
348
[email protected]9fbd3f862011-09-20 23:31:34349} // namespace content