| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/extensions/shell_window.h" | 5 #include "chrome/browser/ui/extensions/shell_window.h" |
| 6 | 6 |
| 7 #include "base/utf_string_conversions.h" | 7 #include "base/utf_string_conversions.h" |
| 8 #include "chrome/browser/extensions/extension_process_manager.h" | 8 #include "chrome/browser/extensions/extension_process_manager.h" |
| 9 #include "chrome/browser/extensions/shell_window_registry.h" | 9 #include "chrome/browser/extensions/shell_window_registry.h" |
| 10 #include "chrome/browser/file_select_helper.h" | 10 #include "chrome/browser/file_select_helper.h" |
| 11 #include "chrome/browser/infobars/infobar_tab_helper.h" | 11 #include "chrome/browser/infobars/infobar_tab_helper.h" |
| 12 #include "chrome/browser/intents/web_intents_util.h" | 12 #include "chrome/browser/intents/web_intents_util.h" |
| 13 #include "chrome/browser/lifetime/application_lifetime.h" | 13 #include "chrome/browser/lifetime/application_lifetime.h" |
| 14 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
| 15 #include "chrome/browser/sessions/session_id.h" | 15 #include "chrome/browser/sessions/session_id.h" |
| 16 #include "chrome/browser/ui/browser.h" | 16 #include "chrome/browser/ui/browser.h" |
| 17 #include "chrome/browser/ui/browser_finder.h" | 17 #include "chrome/browser/ui/browser_finder.h" |
| 18 #include "chrome/browser/ui/browser_tabstrip.h" | 18 #include "chrome/browser/ui/browser_tabstrip.h" |
| 19 #include "chrome/browser/ui/browser_window.h" | 19 #include "chrome/browser/ui/browser_window.h" |
| 20 #include "chrome/browser/ui/extensions/native_shell_window.h" |
| 20 #include "chrome/browser/ui/intents/web_intent_picker_controller.h" | 21 #include "chrome/browser/ui/intents/web_intent_picker_controller.h" |
| 21 #include "chrome/browser/ui/tab_contents/tab_contents.h" | 22 #include "chrome/browser/ui/tab_contents/tab_contents.h" |
| 22 #include "chrome/browser/view_type_utils.h" | 23 #include "chrome/browser/view_type_utils.h" |
| 23 #include "chrome/common/chrome_notification_types.h" | 24 #include "chrome/common/chrome_notification_types.h" |
| 24 #include "chrome/common/extensions/extension.h" | 25 #include "chrome/common/extensions/extension.h" |
| 25 #include "chrome/common/extensions/extension_messages.h" | 26 #include "chrome/common/extensions/extension_messages.h" |
| 26 #include "content/public/browser/browser_thread.h" | 27 #include "content/public/browser/browser_thread.h" |
| 27 #include "content/public/browser/invalidate_type.h" | 28 #include "content/public/browser/invalidate_type.h" |
| 28 #include "content/public/browser/navigation_entry.h" | 29 #include "content/public/browser/navigation_entry.h" |
| 29 #include "content/public/browser/notification_details.h" | 30 #include "content/public/browser/notification_details.h" |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 63 ShellWindow::CreateParams::CreateParams() | 64 ShellWindow::CreateParams::CreateParams() |
| 64 : frame(ShellWindow::CreateParams::FRAME_CHROME), | 65 : frame(ShellWindow::CreateParams::FRAME_CHROME), |
| 65 bounds(10, 10, kDefaultWidth, kDefaultHeight) { | 66 bounds(10, 10, kDefaultWidth, kDefaultHeight) { |
| 66 } | 67 } |
| 67 | 68 |
| 68 ShellWindow* ShellWindow::Create(Profile* profile, | 69 ShellWindow* ShellWindow::Create(Profile* profile, |
| 69 const extensions::Extension* extension, | 70 const extensions::Extension* extension, |
| 70 const GURL& url, | 71 const GURL& url, |
| 71 const ShellWindow::CreateParams& params) { | 72 const ShellWindow::CreateParams& params) { |
| 72 // This object will delete itself when the window is closed. | 73 // This object will delete itself when the window is closed. |
| 73 ShellWindow* window = | 74 ShellWindow* window = new ShellWindow(profile, extension); |
| 74 ShellWindow::CreateImpl(profile, extension, url, params); | 75 window->Init(url, params); |
| 75 ShellWindowRegistry::Get(profile)->AddShellWindow(window); | 76 ShellWindowRegistry::Get(profile)->AddShellWindow(window); |
| 76 return window; | 77 return window; |
| 77 } | 78 } |
| 78 | 79 |
| 79 ShellWindow::ShellWindow(Profile* profile, | 80 ShellWindow::ShellWindow(Profile* profile, |
| 80 const extensions::Extension* extension, | 81 const extensions::Extension* extension) |
| 81 const GURL& url) | |
| 82 : profile_(profile), | 82 : profile_(profile), |
| 83 extension_(extension), | 83 extension_(extension), |
| 84 web_contents_(NULL), |
| 84 ALLOW_THIS_IN_INITIALIZER_LIST( | 85 ALLOW_THIS_IN_INITIALIZER_LIST( |
| 85 extension_function_dispatcher_(profile, this)) { | 86 extension_function_dispatcher_(profile, this)) { |
| 86 // TODO(jeremya) this should all be done in an Init() method, not in the | 87 } |
| 87 // constructor. During this code, WebContents will be calling | 88 |
| 88 // WebContentsDelegate methods, but at this point the vftables for the | 89 void ShellWindow::Init(const GURL& url, |
| 89 // subclass are not yet in place, since it's still halfway through its | 90 const ShellWindow::CreateParams& params) { |
| 90 // constructor. As a result, overridden virtual methods won't be called. | |
| 91 web_contents_ = WebContents::Create( | 91 web_contents_ = WebContents::Create( |
| 92 profile, SiteInstance::CreateForURL(profile, url), MSG_ROUTING_NONE, NULL, | 92 profile(), SiteInstance::CreateForURL(profile(), url), |
| 93 NULL); | 93 MSG_ROUTING_NONE, NULL, NULL); |
| 94 contents_.reset(new TabContents(web_contents_)); | 94 contents_.reset(new TabContents(web_contents_)); |
| 95 content::WebContentsObserver::Observe(web_contents_); | 95 content::WebContentsObserver::Observe(web_contents_); |
| 96 web_contents_->SetDelegate(this); | 96 web_contents_->SetDelegate(this); |
| 97 chrome::SetViewType(web_contents_, chrome::VIEW_TYPE_APP_SHELL); | 97 chrome::SetViewType(web_contents_, chrome::VIEW_TYPE_APP_SHELL); |
| 98 web_contents_->GetMutableRendererPrefs()-> | 98 web_contents_->GetMutableRendererPrefs()-> |
| 99 browser_handles_all_top_level_requests = true; | 99 browser_handles_all_top_level_requests = true; |
| 100 web_contents_->GetRenderViewHost()->SyncRendererPrefs(); | 100 web_contents_->GetRenderViewHost()->SyncRendererPrefs(); |
| 101 | 101 |
| 102 native_window_ = NativeShellWindow::Create(this, params); |
| 103 |
| 102 // Block the created RVH from loading anything until the background page | 104 // Block the created RVH from loading anything until the background page |
| 103 // has had a chance to do any initialization it wants. | 105 // has had a chance to do any initialization it wants. |
| 104 SuspendRenderViewHost(web_contents_->GetRenderViewHost()); | 106 SuspendRenderViewHost(web_contents_->GetRenderViewHost()); |
| 105 | 107 |
| 106 // TODO(jeremya): there's a bug where navigating a web contents to an | 108 // TODO(jeremya): there's a bug where navigating a web contents to an |
| 107 // extension URL causes it to create a new RVH and discard the old (perfectly | 109 // extension URL causes it to create a new RVH and discard the old (perfectly |
| 108 // usable) one. To work around this, we watch for a RVH_CHANGED message from | 110 // usable) one. To work around this, we watch for a RVH_CHANGED message from |
| 109 // the web contents (which will be sent during LoadURL) and suspend resource | 111 // the web contents (which will be sent during LoadURL) and suspend resource |
| 110 // requests on the new RVH to ensure that we block the new RVH from loading | 112 // requests on the new RVH to ensure that we block the new RVH from loading |
| 111 // anything. It should be okay to remove the NOTIFICATION_RVH_CHANGED | 113 // anything. It should be okay to remove the NOTIFICATION_RVH_CHANGED |
| (...skipping 23 matching lines...) Expand all Loading... |
| 135 browser::StartKeepAlive(); | 137 browser::StartKeepAlive(); |
| 136 } | 138 } |
| 137 | 139 |
| 138 ShellWindow::~ShellWindow() { | 140 ShellWindow::~ShellWindow() { |
| 139 // Unregister now to prevent getting NOTIFICATION_APP_TERMINATING if we're the | 141 // Unregister now to prevent getting NOTIFICATION_APP_TERMINATING if we're the |
| 140 // last window open. | 142 // last window open. |
| 141 registrar_.RemoveAll(); | 143 registrar_.RemoveAll(); |
| 142 | 144 |
| 143 // Remove shutdown prevention. | 145 // Remove shutdown prevention. |
| 144 browser::EndKeepAlive(); | 146 browser::EndKeepAlive(); |
| 145 } | |
| 146 | 147 |
| 147 bool ShellWindow::IsFullscreenOrPending() const { | 148 delete native_window_; |
| 148 return false; | |
| 149 } | 149 } |
| 150 | 150 |
| 151 void ShellWindow::RequestMediaAccessPermission( | 151 void ShellWindow::RequestMediaAccessPermission( |
| 152 content::WebContents* web_contents, | 152 content::WebContents* web_contents, |
| 153 const content::MediaStreamRequest* request, | 153 const content::MediaStreamRequest* request, |
| 154 const content::MediaResponseCallback& callback) { | 154 const content::MediaResponseCallback& callback) { |
| 155 content::MediaStreamDevices devices; | 155 content::MediaStreamDevices devices; |
| 156 | 156 |
| 157 content::MediaStreamDeviceMap::const_iterator iter = | 157 content::MediaStreamDeviceMap::const_iterator iter = |
| 158 request->devices.find(content::MEDIA_STREAM_DEVICE_TYPE_AUDIO_CAPTURE); | 158 request->devices.find(content::MEDIA_STREAM_DEVICE_TYPE_AUDIO_CAPTURE); |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 232 disposition == NEW_BACKGROUND_TAB ? disposition : NEW_FOREGROUND_TAB; | 232 disposition == NEW_BACKGROUND_TAB ? disposition : NEW_FOREGROUND_TAB; |
| 233 chrome::AddWebContents(browser, NULL, new_contents, disposition, initial_pos, | 233 chrome::AddWebContents(browser, NULL, new_contents, disposition, initial_pos, |
| 234 user_gesture); | 234 user_gesture); |
| 235 } | 235 } |
| 236 | 236 |
| 237 void ShellWindow::OnNativeClose() { | 237 void ShellWindow::OnNativeClose() { |
| 238 ShellWindowRegistry::Get(profile_)->RemoveShellWindow(this); | 238 ShellWindowRegistry::Get(profile_)->RemoveShellWindow(this); |
| 239 delete this; | 239 delete this; |
| 240 } | 240 } |
| 241 | 241 |
| 242 BaseWindow* ShellWindow::GetBaseWindow() { |
| 243 return native_window_; |
| 244 } |
| 245 |
| 242 string16 ShellWindow::GetTitle() const { | 246 string16 ShellWindow::GetTitle() const { |
| 243 // WebContents::GetTitle() will return the page's URL if there's no <title> | 247 // WebContents::GetTitle() will return the page's URL if there's no <title> |
| 244 // specified. However, we'd prefer to show the name of the extension in that | 248 // specified. However, we'd prefer to show the name of the extension in that |
| 245 // case, so we directly inspect the NavigationEntry's title. | 249 // case, so we directly inspect the NavigationEntry's title. |
| 246 if (!web_contents()->GetController().GetActiveEntry() || | 250 if (!web_contents()->GetController().GetActiveEntry() || |
| 247 web_contents()->GetController().GetActiveEntry()->GetTitle().empty()) | 251 web_contents()->GetController().GetActiveEntry()->GetTitle().empty()) |
| 248 return UTF8ToUTF16(extension()->name()); | 252 return UTF8ToUTF16(extension()->name()); |
| 249 return web_contents()->GetTitle(); | 253 return web_contents()->GetTitle(); |
| 250 } | 254 } |
| 251 | 255 |
| 252 bool ShellWindow::OnMessageReceived(const IPC::Message& message) { | 256 bool ShellWindow::OnMessageReceived(const IPC::Message& message) { |
| 253 bool handled = true; | 257 bool handled = true; |
| 254 IPC_BEGIN_MESSAGE_MAP(ShellWindow, message) | 258 IPC_BEGIN_MESSAGE_MAP(ShellWindow, message) |
| 255 IPC_MESSAGE_HANDLER(ExtensionHostMsg_Request, OnRequest) | 259 IPC_MESSAGE_HANDLER(ExtensionHostMsg_Request, OnRequest) |
| 256 IPC_MESSAGE_HANDLER(ExtensionHostMsg_UpdateDraggableRegions, | 260 IPC_MESSAGE_HANDLER(ExtensionHostMsg_UpdateDraggableRegions, |
| 257 UpdateDraggableRegions) | 261 UpdateDraggableRegions) |
| 258 IPC_MESSAGE_UNHANDLED(handled = false) | 262 IPC_MESSAGE_UNHANDLED(handled = false) |
| 259 IPC_END_MESSAGE_MAP() | 263 IPC_END_MESSAGE_MAP() |
| 260 return handled; | 264 return handled; |
| 261 } | 265 } |
| 262 | 266 |
| 267 void ShellWindow::UpdateDraggableRegions( |
| 268 const std::vector<extensions::DraggableRegion>& regions) { |
| 269 native_window_->UpdateDraggableRegions(regions); |
| 270 } |
| 271 |
| 263 void ShellWindow::CloseContents(WebContents* contents) { | 272 void ShellWindow::CloseContents(WebContents* contents) { |
| 264 Close(); | 273 DCHECK(contents == web_contents_); |
| 274 native_window_->Close(); |
| 265 } | 275 } |
| 266 | 276 |
| 267 bool ShellWindow::ShouldSuppressDialogs() { | 277 bool ShellWindow::ShouldSuppressDialogs() { |
| 268 return true; | 278 return true; |
| 269 } | 279 } |
| 270 | 280 |
| 271 void ShellWindow::WebIntentDispatch( | 281 void ShellWindow::WebIntentDispatch( |
| 272 content::WebContents* web_contents, | 282 content::WebContents* web_contents, |
| 273 content::WebIntentsDispatcher* intents_dispatcher) { | 283 content::WebIntentsDispatcher* intents_dispatcher) { |
| 274 if (!web_intents::IsWebIntentsEnabledForProfile(profile_)) | 284 if (!web_intents::IsWebIntentsEnabledForProfile(profile_)) |
| (...skipping 11 matching lines...) Expand all Loading... |
| 286 FileSelectHelper::RunFileChooser(tab, params); | 296 FileSelectHelper::RunFileChooser(tab, params); |
| 287 } | 297 } |
| 288 | 298 |
| 289 bool ShellWindow::IsPopupOrPanel(const WebContents* source) const { | 299 bool ShellWindow::IsPopupOrPanel(const WebContents* source) const { |
| 290 DCHECK(source == web_contents_); | 300 DCHECK(source == web_contents_); |
| 291 return true; | 301 return true; |
| 292 } | 302 } |
| 293 | 303 |
| 294 void ShellWindow::MoveContents(WebContents* source, const gfx::Rect& pos) { | 304 void ShellWindow::MoveContents(WebContents* source, const gfx::Rect& pos) { |
| 295 DCHECK(source == web_contents_); | 305 DCHECK(source == web_contents_); |
| 296 SetBounds(pos); | 306 native_window_->SetBounds(pos); |
| 297 } | 307 } |
| 298 | 308 |
| 299 void ShellWindow::NavigationStateChanged( | 309 void ShellWindow::NavigationStateChanged( |
| 300 const content::WebContents* source, unsigned changed_flags) { | 310 const content::WebContents* source, unsigned changed_flags) { |
| 301 DCHECK(source == web_contents_); | 311 DCHECK(source == web_contents_); |
| 302 if (changed_flags & content::INVALIDATE_TYPE_TITLE) | 312 if (changed_flags & content::INVALIDATE_TYPE_TITLE) |
| 303 UpdateWindowTitle(); | 313 native_window_->UpdateWindowTitle(); |
| 304 } | 314 } |
| 305 | 315 |
| 306 void ShellWindow::ToggleFullscreenModeForTab(content::WebContents* source, | 316 void ShellWindow::ToggleFullscreenModeForTab(content::WebContents* source, |
| 307 bool enter_fullscreen) { | 317 bool enter_fullscreen) { |
| 308 DCHECK(source == web_contents_); | 318 DCHECK(source == web_contents_); |
| 309 SetFullscreen(enter_fullscreen); | 319 native_window_->SetFullscreen(enter_fullscreen); |
| 310 } | 320 } |
| 311 | 321 |
| 312 bool ShellWindow::IsFullscreenForTabOrPending( | 322 bool ShellWindow::IsFullscreenForTabOrPending( |
| 313 const content::WebContents* source) const { | 323 const content::WebContents* source) const { |
| 314 DCHECK(source == web_contents_); | 324 DCHECK(source == web_contents_); |
| 315 return IsFullscreenOrPending(); | 325 return native_window_->IsFullscreenOrPending(); |
| 316 } | 326 } |
| 317 | 327 |
| 318 void ShellWindow::Observe(int type, | 328 void ShellWindow::Observe(int type, |
| 319 const content::NotificationSource& source, | 329 const content::NotificationSource& source, |
| 320 const content::NotificationDetails& details) { | 330 const content::NotificationDetails& details) { |
| 321 switch (type) { | 331 switch (type) { |
| 322 case content::NOTIFICATION_RENDER_VIEW_HOST_CHANGED: { | 332 case content::NOTIFICATION_RENDER_VIEW_HOST_CHANGED: { |
| 323 // TODO(jeremya): once http://crbug.com/123007 is fixed, we'll no longer | 333 // TODO(jeremya): once http://crbug.com/123007 is fixed, we'll no longer |
| 324 // need to suspend resource requests here (the call in the constructor | 334 // need to suspend resource requests here (the call in the constructor |
| 325 // should be enough). | 335 // should be enough). |
| 326 content::Details<std::pair<RenderViewHost*, RenderViewHost*> > | 336 content::Details<std::pair<RenderViewHost*, RenderViewHost*> > |
| 327 host_details(details); | 337 host_details(details); |
| 328 if (host_details->first) | 338 if (host_details->first) |
| 329 SuspendRenderViewHost(host_details->second); | 339 SuspendRenderViewHost(host_details->second); |
| 330 break; | 340 break; |
| 331 } | 341 } |
| 332 case chrome::NOTIFICATION_EXTENSION_UNLOADED: { | 342 case chrome::NOTIFICATION_EXTENSION_UNLOADED: { |
| 333 const extensions::Extension* unloaded_extension = | 343 const extensions::Extension* unloaded_extension = |
| 334 content::Details<extensions::UnloadedExtensionInfo>( | 344 content::Details<extensions::UnloadedExtensionInfo>( |
| 335 details)->extension; | 345 details)->extension; |
| 336 if (extension_ == unloaded_extension) | 346 if (extension_ == unloaded_extension) |
| 337 Close(); | 347 native_window_->Close(); |
| 338 break; | 348 break; |
| 339 } | 349 } |
| 340 case content::NOTIFICATION_APP_TERMINATING: | 350 case content::NOTIFICATION_APP_TERMINATING: |
| 341 Close(); | 351 native_window_->Close(); |
| 342 break; | 352 break; |
| 343 default: | 353 default: |
| 344 NOTREACHED() << "Received unexpected notification"; | 354 NOTREACHED() << "Received unexpected notification"; |
| 345 } | 355 } |
| 346 } | 356 } |
| 347 | 357 |
| 348 extensions::WindowController* | 358 extensions::WindowController* |
| 349 ShellWindow::GetExtensionWindowController() const { | 359 ShellWindow::GetExtensionWindowController() const { |
| 350 return NULL; | 360 return NULL; |
| 351 } | 361 } |
| 352 | 362 |
| 353 void ShellWindow::OnRequest(const ExtensionHostMsg_Request_Params& params) { | 363 void ShellWindow::OnRequest(const ExtensionHostMsg_Request_Params& params) { |
| 354 extension_function_dispatcher_.Dispatch(params, | 364 extension_function_dispatcher_.Dispatch(params, |
| 355 web_contents_->GetRenderViewHost()); | 365 web_contents_->GetRenderViewHost()); |
| 356 } | 366 } |
| 357 | 367 |
| 358 void ShellWindow::AddMessageToDevToolsConsole(ConsoleMessageLevel level, | 368 void ShellWindow::AddMessageToDevToolsConsole(ConsoleMessageLevel level, |
| 359 const std::string& message) { | 369 const std::string& message) { |
| 360 content::RenderViewHost* rvh = web_contents_->GetRenderViewHost(); | 370 content::RenderViewHost* rvh = web_contents_->GetRenderViewHost(); |
| 361 rvh->Send(new ExtensionMsg_AddMessageToConsole( | 371 rvh->Send(new ExtensionMsg_AddMessageToConsole( |
| 362 rvh->GetRoutingID(), level, message)); | 372 rvh->GetRoutingID(), level, message)); |
| 363 } | 373 } |
| OLD | NEW |