| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/apps/chrome_app_delegate.h" | 5 #include "chrome/browser/ui/apps/chrome_app_delegate.h" |
| 6 | 6 |
| 7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
| 9 #include "chrome/browser/app_mode/app_mode_utils.h" | 9 #include "chrome/browser/app_mode/app_mode_utils.h" |
| 10 #include "chrome/browser/apps/scoped_keep_alive.h" | 10 #include "chrome/browser/apps/scoped_keep_alive.h" |
| 11 #include "chrome/browser/chrome_notification_types.h" | 11 #include "chrome/browser/chrome_notification_types.h" |
| 12 #include "chrome/browser/extensions/chrome_extension_web_contents_observer.h" | 12 #include "chrome/browser/extensions/chrome_extension_web_contents_observer.h" |
| 13 #include "chrome/browser/favicon/favicon_tab_helper.h" | 13 #include "chrome/browser/favicon/favicon_tab_helper.h" |
| 14 #include "chrome/browser/file_select_helper.h" | 14 #include "chrome/browser/file_select_helper.h" |
| 15 #include "chrome/browser/media/media_capture_devices_dispatcher.h" | 15 #include "chrome/browser/media/media_capture_devices_dispatcher.h" |
| 16 #include "chrome/browser/platform_util.h" | 16 #include "chrome/browser/platform_util.h" |
| 17 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
| 18 #include "chrome/browser/shell_integration.h" | 18 #include "chrome/browser/shell_integration.h" |
| 19 #include "chrome/browser/ui/browser.h" | 19 #include "chrome/browser/ui/browser.h" |
| 20 #include "chrome/browser/ui/browser_dialogs.h" | 20 #include "chrome/browser/ui/browser_dialogs.h" |
| 21 #include "chrome/browser/ui/browser_tabstrip.h" | 21 #include "chrome/browser/ui/browser_tabstrip.h" |
| 22 #include "chrome/browser/ui/browser_window.h" | 22 #include "chrome/browser/ui/browser_window.h" |
| 23 #include "chrome/browser/ui/scoped_tabbed_browser_displayer.h" | 23 #include "chrome/browser/ui/scoped_tabbed_browser_displayer.h" |
| 24 #include "chrome/browser/ui/web_contents_sizer.h" | 24 #include "chrome/browser/ui/web_contents_sizer.h" |
| 25 #include "chrome/common/extensions/chrome_extension_messages.h" | 25 #include "chrome/common/extensions/chrome_extension_messages.h" |
| 26 #include "components/ui/zoom/zoom_controller.h" | 26 #include "components/ui/zoom/zoom_controller.h" |
| 27 #include "content/public/browser/browser_context.h" | 27 #include "content/public/browser/browser_context.h" |
| 28 #include "content/public/browser/browser_thread.h" |
| 28 #include "content/public/browser/host_zoom_map.h" | 29 #include "content/public/browser/host_zoom_map.h" |
| 29 #include "content/public/browser/notification_service.h" | 30 #include "content/public/browser/notification_service.h" |
| 30 #include "content/public/browser/render_view_host.h" | 31 #include "content/public/browser/render_view_host.h" |
| 31 #include "content/public/browser/web_contents.h" | 32 #include "content/public/browser/web_contents.h" |
| 32 #include "content/public/browser/web_contents_delegate.h" | 33 #include "content/public/browser/web_contents_delegate.h" |
| 33 #include "extensions/common/constants.h" | 34 #include "extensions/common/constants.h" |
| 34 | 35 |
| 35 #if defined(USE_ASH) | 36 #if defined(USE_ASH) |
| 36 #include "ash/shelf/shelf_constants.h" | 37 #include "ash/shelf/shelf_constants.h" |
| 37 #endif | 38 #endif |
| 38 | 39 |
| 39 #if defined(ENABLE_PRINTING) | 40 #if defined(ENABLE_PRINTING) |
| 40 #if defined(ENABLE_PRINT_PREVIEW) | 41 #if defined(ENABLE_PRINT_PREVIEW) |
| 41 #include "chrome/browser/printing/print_preview_message_handler.h" | 42 #include "chrome/browser/printing/print_preview_message_handler.h" |
| 42 #include "chrome/browser/printing/print_view_manager.h" | 43 #include "chrome/browser/printing/print_view_manager.h" |
| 43 #else | 44 #else |
| 44 #include "chrome/browser/printing/print_view_manager_basic.h" | 45 #include "chrome/browser/printing/print_view_manager_basic.h" |
| 45 #endif // defined(ENABLE_PRINT_PREVIEW) | 46 #endif // defined(ENABLE_PRINT_PREVIEW) |
| 46 #endif // defined(ENABLE_PRINTING) | 47 #endif // defined(ENABLE_PRINTING) |
| 47 | 48 |
| 48 namespace { | 49 namespace { |
| 49 | 50 |
| 51 // Time to wait for an app window to show before allowing Chrome to quit. |
| 52 int kAppWindowFirstShowTimeoutSeconds = 10; |
| 53 |
| 50 bool disable_external_open_for_testing_ = false; | 54 bool disable_external_open_for_testing_ = false; |
| 51 | 55 |
| 52 // Opens a URL with Chromium (not external browser) with the right profile. | 56 // Opens a URL with Chromium (not external browser) with the right profile. |
| 53 content::WebContents* OpenURLFromTabInternal( | 57 content::WebContents* OpenURLFromTabInternal( |
| 54 content::BrowserContext* context, | 58 content::BrowserContext* context, |
| 55 const content::OpenURLParams& params) { | 59 const content::OpenURLParams& params) { |
| 56 // Force all links to open in a new tab, even if they were trying to open a | 60 // Force all links to open in a new tab, even if they were trying to open a |
| 57 // window. | 61 // window. |
| 58 chrome::NavigateParams new_tab_params( | 62 chrome::NavigateParams new_tab_params( |
| 59 static_cast<Browser*>(NULL), params.url, params.transition); | 63 static_cast<Browser*>(NULL), params.url, params.transition); |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 104 | 108 |
| 105 bool IsOwnedByWorker() override { return true; } | 109 bool IsOwnedByWorker() override { return true; } |
| 106 | 110 |
| 107 private: | 111 private: |
| 108 scoped_ptr<content::WebContents> source_; | 112 scoped_ptr<content::WebContents> source_; |
| 109 const content::OpenURLParams params_; | 113 const content::OpenURLParams params_; |
| 110 }; | 114 }; |
| 111 | 115 |
| 112 } // namespace | 116 } // namespace |
| 113 | 117 |
| 118 // static |
| 119 void ChromeAppDelegate::RelinquishKeepAliveAfterTimeout( |
| 120 const base::WeakPtr<ChromeAppDelegate>& chrome_app_delegate) { |
| 121 if (chrome_app_delegate && chrome_app_delegate->is_hidden_) |
| 122 chrome_app_delegate->keep_alive_.reset(); |
| 123 } |
| 124 |
| 114 class ChromeAppDelegate::NewWindowContentsDelegate | 125 class ChromeAppDelegate::NewWindowContentsDelegate |
| 115 : public content::WebContentsDelegate { | 126 : public content::WebContentsDelegate { |
| 116 public: | 127 public: |
| 117 NewWindowContentsDelegate() {} | 128 NewWindowContentsDelegate() {} |
| 118 ~NewWindowContentsDelegate() override {} | 129 ~NewWindowContentsDelegate() override {} |
| 119 | 130 |
| 120 content::WebContents* OpenURLFromTab( | 131 content::WebContents* OpenURLFromTab( |
| 121 content::WebContents* source, | 132 content::WebContents* source, |
| 122 const content::OpenURLParams& params) override; | 133 const content::OpenURLParams& params) override; |
| 123 | 134 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 144 // Object lifetime notes: The OpenURLFromTabBasedOnBrowserDefault is owned | 155 // Object lifetime notes: The OpenURLFromTabBasedOnBrowserDefault is owned |
| 145 // by check_if_default_browser_worker. StartCheckIsDefault() takes lifetime | 156 // by check_if_default_browser_worker. StartCheckIsDefault() takes lifetime |
| 146 // ownership of check_if_default_browser_worker and will clean up after | 157 // ownership of check_if_default_browser_worker and will clean up after |
| 147 // the asynchronous tasks. | 158 // the asynchronous tasks. |
| 148 check_if_default_browser_worker->StartCheckIsDefault(); | 159 check_if_default_browser_worker->StartCheckIsDefault(); |
| 149 } | 160 } |
| 150 return NULL; | 161 return NULL; |
| 151 } | 162 } |
| 152 | 163 |
| 153 ChromeAppDelegate::ChromeAppDelegate(scoped_ptr<ScopedKeepAlive> keep_alive) | 164 ChromeAppDelegate::ChromeAppDelegate(scoped_ptr<ScopedKeepAlive> keep_alive) |
| 154 : keep_alive_(keep_alive.Pass()), | 165 : has_been_shown_(false), |
| 155 new_window_contents_delegate_(new NewWindowContentsDelegate()) { | 166 is_hidden_(true), |
| 167 keep_alive_(keep_alive.Pass()), |
| 168 new_window_contents_delegate_(new NewWindowContentsDelegate()), |
| 169 weak_factory_(this) { |
| 156 registrar_.Add(this, | 170 registrar_.Add(this, |
| 157 chrome::NOTIFICATION_APP_TERMINATING, | 171 chrome::NOTIFICATION_APP_TERMINATING, |
| 158 content::NotificationService::AllSources()); | 172 content::NotificationService::AllSources()); |
| 159 } | 173 } |
| 160 | 174 |
| 161 ChromeAppDelegate::~ChromeAppDelegate() { | 175 ChromeAppDelegate::~ChromeAppDelegate() { |
| 162 // Unregister now to prevent getting notified if |keep_alive_| is the last. | 176 // Unregister now to prevent getting notified if |keep_alive_| is the last. |
| 163 terminating_callback_.Reset(); | 177 terminating_callback_.Reset(); |
| 164 } | 178 } |
| 165 | 179 |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 297 | 311 |
| 298 bool ChromeAppDelegate::IsWebContentsVisible( | 312 bool ChromeAppDelegate::IsWebContentsVisible( |
| 299 content::WebContents* web_contents) { | 313 content::WebContents* web_contents) { |
| 300 return platform_util::IsVisible(web_contents->GetNativeView()); | 314 return platform_util::IsVisible(web_contents->GetNativeView()); |
| 301 } | 315 } |
| 302 | 316 |
| 303 void ChromeAppDelegate::SetTerminatingCallback(const base::Closure& callback) { | 317 void ChromeAppDelegate::SetTerminatingCallback(const base::Closure& callback) { |
| 304 terminating_callback_ = callback; | 318 terminating_callback_ = callback; |
| 305 } | 319 } |
| 306 | 320 |
| 321 void ChromeAppDelegate::OnHide() { |
| 322 is_hidden_ = true; |
| 323 if (has_been_shown_) { |
| 324 keep_alive_.reset(); |
| 325 return; |
| 326 } |
| 327 |
| 328 // Hold on to the keep alive for some time to give the app a chance to show |
| 329 // the window. |
| 330 content::BrowserThread::PostDelayedTask( |
| 331 content::BrowserThread::UI, FROM_HERE, |
| 332 base::Bind(&ChromeAppDelegate::RelinquishKeepAliveAfterTimeout, |
| 333 weak_factory_.GetWeakPtr()), |
| 334 base::TimeDelta::FromSeconds(kAppWindowFirstShowTimeoutSeconds)); |
| 335 } |
| 336 |
| 337 void ChromeAppDelegate::OnShow() { |
| 338 has_been_shown_ = true; |
| 339 is_hidden_ = false; |
| 340 keep_alive_.reset(new ScopedKeepAlive); |
| 341 } |
| 342 |
| 307 void ChromeAppDelegate::Observe(int type, | 343 void ChromeAppDelegate::Observe(int type, |
| 308 const content::NotificationSource& source, | 344 const content::NotificationSource& source, |
| 309 const content::NotificationDetails& details) { | 345 const content::NotificationDetails& details) { |
| 310 switch (type) { | 346 switch (type) { |
| 311 case chrome::NOTIFICATION_APP_TERMINATING: | 347 case chrome::NOTIFICATION_APP_TERMINATING: |
| 312 if (!terminating_callback_.is_null()) | 348 if (!terminating_callback_.is_null()) |
| 313 terminating_callback_.Run(); | 349 terminating_callback_.Run(); |
| 314 break; | 350 break; |
| 315 default: | 351 default: |
| 316 NOTREACHED() << "Received unexpected notification"; | 352 NOTREACHED() << "Received unexpected notification"; |
| 317 } | 353 } |
| 318 } | 354 } |
| OLD | NEW |