Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(141)

Side by Side Diff: chrome/browser/ui/apps/chrome_app_delegate.cc

Issue 875273003: Hidden windows should not keep Chrome alive. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Actually fix the ASAN issue. Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 // Resetting the ScopedKeepAlive may cause nested destruction of the
122 // ChromeAppDelegate which also resets the ScopedKeepAlive. To avoid this,
123 // move the ScopedKeepAlive out to here and let it fall out of scope.
124 if (chrome_app_delegate.get() && chrome_app_delegate->is_hidden_)
125 scoped_ptr<ScopedKeepAlive>(chrome_app_delegate->keep_alive_.Pass());
126 }
127
114 class ChromeAppDelegate::NewWindowContentsDelegate 128 class ChromeAppDelegate::NewWindowContentsDelegate
115 : public content::WebContentsDelegate { 129 : public content::WebContentsDelegate {
116 public: 130 public:
117 NewWindowContentsDelegate() {} 131 NewWindowContentsDelegate() {}
118 ~NewWindowContentsDelegate() override {} 132 ~NewWindowContentsDelegate() override {}
119 133
120 content::WebContents* OpenURLFromTab( 134 content::WebContents* OpenURLFromTab(
121 content::WebContents* source, 135 content::WebContents* source,
122 const content::OpenURLParams& params) override; 136 const content::OpenURLParams& params) override;
123 137
(...skipping 20 matching lines...) Expand all
144 // Object lifetime notes: The OpenURLFromTabBasedOnBrowserDefault is owned 158 // Object lifetime notes: The OpenURLFromTabBasedOnBrowserDefault is owned
145 // by check_if_default_browser_worker. StartCheckIsDefault() takes lifetime 159 // by check_if_default_browser_worker. StartCheckIsDefault() takes lifetime
146 // ownership of check_if_default_browser_worker and will clean up after 160 // ownership of check_if_default_browser_worker and will clean up after
147 // the asynchronous tasks. 161 // the asynchronous tasks.
148 check_if_default_browser_worker->StartCheckIsDefault(); 162 check_if_default_browser_worker->StartCheckIsDefault();
149 } 163 }
150 return NULL; 164 return NULL;
151 } 165 }
152 166
153 ChromeAppDelegate::ChromeAppDelegate(scoped_ptr<ScopedKeepAlive> keep_alive) 167 ChromeAppDelegate::ChromeAppDelegate(scoped_ptr<ScopedKeepAlive> keep_alive)
154 : keep_alive_(keep_alive.Pass()), 168 : has_been_shown_(false),
155 new_window_contents_delegate_(new NewWindowContentsDelegate()) { 169 is_hidden_(true),
170 keep_alive_(keep_alive.Pass()),
171 new_window_contents_delegate_(new NewWindowContentsDelegate()),
172 weak_factory_(this) {
156 registrar_.Add(this, 173 registrar_.Add(this,
157 chrome::NOTIFICATION_APP_TERMINATING, 174 chrome::NOTIFICATION_APP_TERMINATING,
158 content::NotificationService::AllSources()); 175 content::NotificationService::AllSources());
159 } 176 }
160 177
161 ChromeAppDelegate::~ChromeAppDelegate() { 178 ChromeAppDelegate::~ChromeAppDelegate() {
162 // Unregister now to prevent getting notified if |keep_alive_| is the last. 179 // Unregister now to prevent getting notified if |keep_alive_| is the last.
163 terminating_callback_.Reset(); 180 terminating_callback_.Reset();
164 } 181 }
165 182
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after
297 314
298 bool ChromeAppDelegate::IsWebContentsVisible( 315 bool ChromeAppDelegate::IsWebContentsVisible(
299 content::WebContents* web_contents) { 316 content::WebContents* web_contents) {
300 return platform_util::IsVisible(web_contents->GetNativeView()); 317 return platform_util::IsVisible(web_contents->GetNativeView());
301 } 318 }
302 319
303 void ChromeAppDelegate::SetTerminatingCallback(const base::Closure& callback) { 320 void ChromeAppDelegate::SetTerminatingCallback(const base::Closure& callback) {
304 terminating_callback_ = callback; 321 terminating_callback_ = callback;
305 } 322 }
306 323
324 void ChromeAppDelegate::OnHide() {
325 is_hidden_ = true;
326 if (has_been_shown_) {
327 keep_alive_.reset();
328 return;
329 }
330
331 // Hold on to the keep alive for some time to give the app a chance to show
332 // the window.
333 content::BrowserThread::PostDelayedTask(
334 content::BrowserThread::UI, FROM_HERE,
335 base::Bind(&ChromeAppDelegate::RelinquishKeepAliveAfterTimeout,
336 weak_factory_.GetWeakPtr()),
337 base::TimeDelta::FromSeconds(kAppWindowFirstShowTimeoutSeconds));
338 }
339
340 void ChromeAppDelegate::OnShow() {
341 has_been_shown_ = true;
342 is_hidden_ = false;
343 keep_alive_.reset(new ScopedKeepAlive);
344 }
345
307 void ChromeAppDelegate::Observe(int type, 346 void ChromeAppDelegate::Observe(int type,
308 const content::NotificationSource& source, 347 const content::NotificationSource& source,
309 const content::NotificationDetails& details) { 348 const content::NotificationDetails& details) {
310 switch (type) { 349 switch (type) {
311 case chrome::NOTIFICATION_APP_TERMINATING: 350 case chrome::NOTIFICATION_APP_TERMINATING:
312 if (!terminating_callback_.is_null()) 351 if (!terminating_callback_.is_null())
313 terminating_callback_.Run(); 352 terminating_callback_.Run();
314 break; 353 break;
315 default: 354 default:
316 NOTREACHED() << "Received unexpected notification"; 355 NOTREACHED() << "Received unexpected notification";
317 } 356 }
318 } 357 }
OLDNEW
« no previous file with comments | « chrome/browser/ui/apps/chrome_app_delegate.h ('k') | chrome/test/data/extensions/platform_apps/hidden/test.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698