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