[email protected] | 5cba3bf | 2012-01-14 02:40:35 | [diff] [blame] | 1 | // Copyright (c) 2012 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 | |
| 5 | #include "content/shell/shell.h" |
| 6 | |
[email protected] | 11a65b69 | 2012-03-30 11:29:16 | [diff] [blame] | 7 | #include "base/auto_reset.h" |
[email protected] | efb5f57 | 2012-01-29 10:57:33 | [diff] [blame] | 8 | #include "base/command_line.h" |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 9 | #include "base/message_loop.h" |
| 10 | #include "base/path_service.h" |
[email protected] | 2cd0d33 | 2012-08-30 13:16:54 | [diff] [blame] | 11 | #include "base/stringprintf.h" |
[email protected] | 7c17b699 | 2012-08-09 16:16:30 | [diff] [blame] | 12 | #include "base/string_number_conversions.h" |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 13 | #include "base/string_util.h" |
[email protected] | c272c5b | 2012-06-06 09:01:06 | [diff] [blame] | 14 | #include "base/utf_string_conversions.h" |
[email protected] | 7c17b699 | 2012-08-09 16:16:30 | [diff] [blame] | 15 | #include "content/public/browser/devtools_http_handler.h" |
[email protected] | aecc085b | 2012-06-01 18:15:53 | [diff] [blame] | 16 | #include "content/public/browser/navigation_entry.h" |
[email protected] | 0b659b3 | 2012-03-26 21:29:32 | [diff] [blame] | 17 | #include "content/public/browser/navigation_controller.h" |
[email protected] | aecc085b | 2012-06-01 18:15:53 | [diff] [blame] | 18 | #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] | 0b659b3 | 2012-03-26 21:29:32 | [diff] [blame] | 21 | #include "content/public/browser/render_view_host.h" |
| 22 | #include "content/public/browser/web_contents.h" |
[email protected] | 7c17b699 | 2012-08-09 16:16:30 | [diff] [blame] | 23 | #include "content/shell/shell_browser_main_parts.h" |
| 24 | #include "content/shell/shell_content_browser_client.h" |
| 25 | #include "content/shell/shell_devtools_delegate.h" |
[email protected] | f221002 | 2012-03-29 00:36:08 | [diff] [blame] | 26 | #include "content/shell/shell_javascript_dialog_creator.h" |
[email protected] | efb5f57 | 2012-01-29 10:57:33 | [diff] [blame] | 27 | #include "content/shell/shell_messages.h" |
| 28 | #include "content/shell/shell_switches.h" |
[email protected] | 2cd0d33 | 2012-08-30 13:16:54 | [diff] [blame] | 29 | #include "content/shell/webkit_test_runner_host.h" |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 30 | #include "ui/gfx/size.h" |
| 31 | |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 32 | // Content area size for newly created windows. |
| 33 | static const int kTestWindowWidth = 800; |
| 34 | static const int kTestWindowHeight = 600; |
| 35 | |
| 36 | namespace content { |
| 37 | |
[email protected] | e99ca511 | 2011-09-26 17:22:54 | [diff] [blame] | 38 | std::vector<Shell*> Shell::windows_; |
[email protected] | 9e00e635 | 2012-07-30 17:05:18 | [diff] [blame] | 39 | base::Callback<void(Shell*)> Shell::shell_created_callback_; |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 40 | |
[email protected] | 11a65b69 | 2012-03-30 11:29:16 | [diff] [blame] | 41 | bool Shell::quit_message_loop_ = true; |
| 42 | |
[email protected] | 0b659b3 | 2012-03-26 21:29:32 | [diff] [blame] | 43 | Shell::Shell(WebContents* web_contents) |
[email protected] | 99c014c | 2012-11-27 12:03:42 | [diff] [blame] | 44 | : is_fullscreen_(false), |
| 45 | window_(NULL), |
[email protected] | 46d04fa | 2011-10-06 18:32:43 | [diff] [blame] | 46 | url_edit_view_(NULL) |
[email protected] | fa4a4583 | 2012-04-12 21:32:48 | [diff] [blame] | 47 | #if defined(OS_WIN) && !defined(USE_AURA) |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 48 | , default_edit_wnd_proc_(0) |
| 49 | #endif |
| 50 | { |
[email protected] | 9e00e635 | 2012-07-30 17:05:18 | [diff] [blame] | 51 | registrar_.Add(this, NOTIFICATION_WEB_CONTENTS_TITLE_UPDATED, |
| 52 | Source<WebContents>(web_contents)); |
| 53 | windows_.push_back(this); |
| 54 | |
| 55 | if (!shell_created_callback_.is_null()) { |
| 56 | shell_created_callback_.Run(this); |
| 57 | shell_created_callback_.Reset(); |
| 58 | } |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | Shell::~Shell() { |
| 62 | PlatformCleanUp(); |
[email protected] | e99ca511 | 2011-09-26 17:22:54 | [diff] [blame] | 63 | |
| 64 | for (size_t i = 0; i < windows_.size(); ++i) { |
| 65 | if (windows_[i] == this) { |
| 66 | windows_.erase(windows_.begin() + i); |
| 67 | break; |
| 68 | } |
| 69 | } |
[email protected] | 11a65b69 | 2012-03-30 11:29:16 | [diff] [blame] | 70 | |
| 71 | if (windows_.empty() && quit_message_loop_) |
| 72 | MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 73 | } |
| 74 | |
[email protected] | 0b659b3 | 2012-03-26 21:29:32 | [diff] [blame] | 75 | Shell* Shell::CreateShell(WebContents* web_contents) { |
| 76 | Shell* shell = new Shell(web_contents); |
[email protected] | 3fd8403 | 2012-01-12 18:20:17 | [diff] [blame] | 77 | shell->PlatformCreateWindow(kTestWindowWidth, kTestWindowHeight); |
| 78 | |
[email protected] | 0b659b3 | 2012-03-26 21:29:32 | [diff] [blame] | 79 | shell->web_contents_.reset(web_contents); |
| 80 | web_contents->SetDelegate(shell); |
[email protected] | 3fd8403 | 2012-01-12 18:20:17 | [diff] [blame] | 81 | |
[email protected] | 5cba3bf | 2012-01-14 02:40:35 | [diff] [blame] | 82 | shell->PlatformSetContents(); |
[email protected] | 3fd8403 | 2012-01-12 18:20:17 | [diff] [blame] | 83 | |
| 84 | shell->PlatformResizeSubViews(); |
| 85 | return shell; |
| 86 | } |
| 87 | |
[email protected] | 11a65b69 | 2012-03-30 11:29:16 | [diff] [blame] | 88 | void Shell::CloseAllWindows() { |
[email protected] | 997ec9f | 2012-11-21 04:44:14 | [diff] [blame] | 89 | base::AutoReset<bool> auto_reset(&quit_message_loop_, false); |
[email protected] | 11a65b69 | 2012-03-30 11:29:16 | [diff] [blame] | 90 | std::vector<Shell*> open_windows(windows_); |
| 91 | for (size_t i = 0; i < open_windows.size(); ++i) |
| 92 | open_windows[i]->Close(); |
[email protected] | f319ace | 2012-11-10 19:07:29 | [diff] [blame] | 93 | MessageLoop::current()->RunUntilIdle(); |
[email protected] | 11a65b69 | 2012-03-30 11:29:16 | [diff] [blame] | 94 | } |
| 95 | |
[email protected] | 9e00e635 | 2012-07-30 17:05:18 | [diff] [blame] | 96 | void Shell::SetShellCreatedCallback( |
| 97 | base::Callback<void(Shell*)> shell_created_callback) { |
| 98 | DCHECK(shell_created_callback_.is_null()); |
| 99 | shell_created_callback_ = shell_created_callback; |
| 100 | } |
| 101 | |
[email protected] | 74830f0 | 2012-01-30 22:27:04 | [diff] [blame] | 102 | Shell* Shell::FromRenderViewHost(RenderViewHost* rvh) { |
| 103 | for (size_t i = 0; i < windows_.size(); ++i) { |
[email protected] | 0b659b3 | 2012-03-26 21:29:32 | [diff] [blame] | 104 | if (windows_[i]->web_contents() && |
| 105 | windows_[i]->web_contents()->GetRenderViewHost() == rvh) { |
[email protected] | 74830f0 | 2012-01-30 22:27:04 | [diff] [blame] | 106 | return windows_[i]; |
| 107 | } |
| 108 | } |
| 109 | return NULL; |
| 110 | } |
| 111 | |
[email protected] | bdcf915 | 2012-07-19 17:43:21 | [diff] [blame] | 112 | Shell* Shell::CreateNewWindow(BrowserContext* browser_context, |
[email protected] | e99ca511 | 2011-09-26 17:22:54 | [diff] [blame] | 113 | const GURL& url, |
| 114 | SiteInstance* site_instance, |
| 115 | int routing_id, |
[email protected] | 0b659b3 | 2012-03-26 21:29:32 | [diff] [blame] | 116 | WebContents* base_web_contents) { |
| 117 | WebContents* web_contents = WebContents::Create( |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 118 | browser_context, |
[email protected] | e99ca511 | 2011-09-26 17:22:54 | [diff] [blame] | 119 | site_instance, |
| 120 | routing_id, |
[email protected] | d1198fd | 2012-08-13 22:50:19 | [diff] [blame] | 121 | base_web_contents); |
[email protected] | 0b659b3 | 2012-03-26 21:29:32 | [diff] [blame] | 122 | Shell* shell = CreateShell(web_contents); |
[email protected] | e99ca511 | 2011-09-26 17:22:54 | [diff] [blame] | 123 | if (!url.is_empty()) |
| 124 | shell->LoadURL(url); |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 125 | return shell; |
| 126 | } |
| 127 | |
| 128 | void Shell::LoadURL(const GURL& url) { |
[email protected] | 0b659b3 | 2012-03-26 21:29:32 | [diff] [blame] | 129 | web_contents_->GetController().LoadURL( |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 130 | url, |
[email protected] | bdcf915 | 2012-07-19 17:43:21 | [diff] [blame] | 131 | Referrer(), |
[email protected] | 8d57530e | 2012-08-20 19:43:58 | [diff] [blame] | 132 | static_cast<PageTransition>( |
| 133 | PAGE_TRANSITION_TYPED | PAGE_TRANSITION_FROM_ADDRESS_BAR), |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 134 | std::string()); |
[email protected] | 0b659b3 | 2012-03-26 21:29:32 | [diff] [blame] | 135 | web_contents_->Focus(); |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | void Shell::GoBackOrForward(int offset) { |
[email protected] | 0b659b3 | 2012-03-26 21:29:32 | [diff] [blame] | 139 | web_contents_->GetController().GoToOffset(offset); |
| 140 | web_contents_->Focus(); |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | void Shell::Reload() { |
[email protected] | 0b659b3 | 2012-03-26 21:29:32 | [diff] [blame] | 144 | web_contents_->GetController().Reload(false); |
| 145 | web_contents_->Focus(); |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | void Shell::Stop() { |
[email protected] | 0b659b3 | 2012-03-26 21:29:32 | [diff] [blame] | 149 | web_contents_->Stop(); |
| 150 | web_contents_->Focus(); |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | void Shell::UpdateNavigationControls() { |
[email protected] | 0b659b3 | 2012-03-26 21:29:32 | [diff] [blame] | 154 | int current_index = web_contents_->GetController().GetCurrentEntryIndex(); |
| 155 | int max_index = web_contents_->GetController().GetEntryCount() - 1; |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 156 | |
| 157 | PlatformEnableUIControl(BACK_BUTTON, current_index > 0); |
| 158 | PlatformEnableUIControl(FORWARD_BUTTON, current_index < max_index); |
[email protected] | 0b659b3 | 2012-03-26 21:29:32 | [diff] [blame] | 159 | PlatformEnableUIControl(STOP_BUTTON, web_contents_->IsLoading()); |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 160 | } |
| 161 | |
[email protected] | 7c17b699 | 2012-08-09 16:16:30 | [diff] [blame] | 162 | void Shell::ShowDevTools() { |
| 163 | ShellContentBrowserClient* browser_client = |
| 164 | static_cast<ShellContentBrowserClient*>( |
| 165 | GetContentClient()->browser()); |
| 166 | ShellDevToolsDelegate* delegate = |
| 167 | browser_client->shell_browser_main_parts()->devtools_delegate(); |
| 168 | GURL url = delegate->devtools_http_handler()->GetFrontendURL( |
| 169 | web_contents()->GetRenderViewHost()); |
| 170 | CreateNewWindow( |
| 171 | web_contents()->GetBrowserContext(), |
| 172 | url, NULL, MSG_ROUTING_NONE, NULL); |
| 173 | } |
| 174 | |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 175 | gfx::NativeView Shell::GetContentView() { |
[email protected] | 0b659b3 | 2012-03-26 21:29:32 | [diff] [blame] | 176 | if (!web_contents_.get()) |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 177 | return NULL; |
[email protected] | 0b659b3 | 2012-03-26 21:29:32 | [diff] [blame] | 178 | return web_contents_->GetNativeView(); |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 179 | } |
| 180 | |
[email protected] | 9e00e635 | 2012-07-30 17:05:18 | [diff] [blame] | 181 | WebContents* Shell::OpenURLFromTab(WebContents* source, |
| 182 | const OpenURLParams& params) { |
| 183 | // The only one we implement for now. |
| 184 | DCHECK(params.disposition == CURRENT_TAB); |
| 185 | source->GetController().LoadURL( |
| 186 | params.url, params.referrer, params.transition, std::string()); |
| 187 | return source; |
| 188 | } |
| 189 | |
[email protected] | 2a6bc3e | 2011-12-28 23:51:33 | [diff] [blame] | 190 | void Shell::LoadingStateChanged(WebContents* source) { |
[email protected] | e99ca511 | 2011-09-26 17:22:54 | [diff] [blame] | 191 | UpdateNavigationControls(); |
[email protected] | 15aea41 | 2012-01-19 03:18:50 | [diff] [blame] | 192 | PlatformSetIsLoading(source->IsLoading()); |
[email protected] | e99ca511 | 2011-09-26 17:22:54 | [diff] [blame] | 193 | } |
| 194 | |
[email protected] | 99c014c | 2012-11-27 12:03:42 | [diff] [blame] | 195 | void Shell::ToggleFullscreenModeForTab(WebContents* web_contents, |
| 196 | bool enter_fullscreen) { |
| 197 | #if defined(OS_ANDROID) |
| 198 | PlatformToggleFullscreenModeForTab(web_contents, enter_fullscreen); |
| 199 | #endif |
| 200 | if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree)) |
| 201 | return; |
| 202 | if (is_fullscreen_ != enter_fullscreen) { |
| 203 | is_fullscreen_ = enter_fullscreen; |
| 204 | web_contents->GetRenderViewHost()->WasResized(); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | bool Shell::IsFullscreenForTabOrPending(const WebContents* web_contents) const { |
| 209 | #if defined(OS_ANDROID) |
| 210 | return PlatformIsFullscreenForTabOrPending(web_contents); |
| 211 | #else |
| 212 | return is_fullscreen_; |
| 213 | #endif |
| 214 | } |
| 215 | |
[email protected] | f7843900 | 2012-11-28 14:45:59 | [diff] [blame^] | 216 | void Shell::RequestToLockMouse(WebContents* web_contents, |
| 217 | bool user_gesture, |
| 218 | bool last_unlocked_by_target) { |
| 219 | web_contents->GotResponseToLockMouseRequest(true); |
| 220 | } |
| 221 | |
[email protected] | 9e00e635 | 2012-07-30 17:05:18 | [diff] [blame] | 222 | void Shell::CloseContents(WebContents* source) { |
| 223 | Close(); |
| 224 | } |
| 225 | |
[email protected] | 06731026 | 2012-11-22 14:30:41 | [diff] [blame] | 226 | bool Shell::CanOverscrollContent() const { |
| 227 | #if defined(USE_AURA) |
| 228 | return true; |
| 229 | #else |
| 230 | return false; |
| 231 | #endif |
| 232 | } |
| 233 | |
[email protected] | 3fd8403 | 2012-01-12 18:20:17 | [diff] [blame] | 234 | void Shell::WebContentsCreated(WebContents* source_contents, |
| 235 | int64 source_frame_id, |
| 236 | const GURL& target_url, |
| 237 | WebContents* new_contents) { |
[email protected] | 0b659b3 | 2012-03-26 21:29:32 | [diff] [blame] | 238 | CreateShell(new_contents); |
[email protected] | 3fd8403 | 2012-01-12 18:20:17 | [diff] [blame] | 239 | } |
| 240 | |
[email protected] | 3bbacc5b | 2012-04-17 17:46:15 | [diff] [blame] | 241 | void Shell::DidNavigateMainFramePostCommit(WebContents* web_contents) { |
| 242 | PlatformSetAddressBarURL(web_contents->GetURL()); |
[email protected] | e99ca511 | 2011-09-26 17:22:54 | [diff] [blame] | 243 | } |
| 244 | |
[email protected] | f221002 | 2012-03-29 00:36:08 | [diff] [blame] | 245 | JavaScriptDialogCreator* Shell::GetJavaScriptDialogCreator() { |
[email protected] | f221002 | 2012-03-29 00:36:08 | [diff] [blame] | 246 | if (!dialog_creator_.get()) |
| 247 | dialog_creator_.reset(new ShellJavaScriptDialogCreator()); |
| 248 | return dialog_creator_.get(); |
| 249 | } |
| 250 | |
[email protected] | c272c5b | 2012-06-06 09:01:06 | [diff] [blame] | 251 | bool Shell::AddMessageToConsole(WebContents* source, |
| 252 | int32 level, |
| 253 | const string16& message, |
| 254 | int32 line_no, |
| 255 | const string16& source_id) { |
[email protected] | efb5f57 | 2012-01-29 10:57:33 | [diff] [blame] | 256 | if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree)) |
[email protected] | c272c5b | 2012-06-06 09:01:06 | [diff] [blame] | 257 | return false; |
| 258 | |
[email protected] | 2cd0d33 | 2012-08-30 13:16:54 | [diff] [blame] | 259 | std::string buffer("CONSOLE MESSAGE: "); |
[email protected] | c272c5b | 2012-06-06 09:01:06 | [diff] [blame] | 260 | if (line_no) |
[email protected] | 2cd0d33 | 2012-08-30 13:16:54 | [diff] [blame] | 261 | buffer += base::StringPrintf("line %d: ", line_no); |
| 262 | buffer += UTF16ToUTF8(message); |
[email protected] | 1183106 | 2012-09-26 15:20:04 | [diff] [blame] | 263 | WebKitTestController::Get()->printer()->AddMessage(buffer); |
[email protected] | c272c5b | 2012-06-06 09:01:06 | [diff] [blame] | 264 | return true; |
[email protected] | efb5f57 | 2012-01-29 10:57:33 | [diff] [blame] | 265 | } |
| 266 | |
[email protected] | 5bf68f2 | 2012-08-31 07:38:10 | [diff] [blame] | 267 | void Shell::RendererUnresponsive(WebContents* source) { |
| 268 | if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree)) |
| 269 | return; |
| 270 | WebKitTestController::Get()->RendererUnresponsive(); |
| 271 | } |
| 272 | |
[email protected] | aecc085b | 2012-06-01 18:15:53 | [diff] [blame] | 273 | void Shell::Observe(int type, |
| 274 | const NotificationSource& source, |
| 275 | const NotificationDetails& details) { |
| 276 | if (type == NOTIFICATION_WEB_CONTENTS_TITLE_UPDATED) { |
| 277 | std::pair<NavigationEntry*, bool>* title = |
| 278 | Details<std::pair<NavigationEntry*, bool> >(details).ptr(); |
| 279 | |
| 280 | if (title->first) { |
| 281 | string16 text = title->first->GetTitle(); |
| 282 | PlatformSetTitle(text); |
| 283 | } |
| 284 | } |
| 285 | } |
| 286 | |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 287 | } // namespace content |