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

Side by Side Diff: chrome/browser/ui/unload_detached_handler.cc

Issue 11016023: Quickly close tabs/window with long-running unload handlers. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Add UnloadController class comment. Created 8 years, 1 month 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 | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/ui/unload_detached_handler.h"
6
7 #include "base/message_loop.h"
8 #include "chrome/browser/ui/tab_contents/tab_contents.h"
9 #include "chrome/browser/ui/tabs/tab_strip_model.h"
10 #include "content/public/browser/web_contents.h"
11 #include "content/public/browser/web_contents_delegate.h"
12
13 namespace chrome {
14
15 ////////////////////////////////////////////////////////////////////////////////
16 // WebContentsDelegate implementation. This owns the TabContents supplied to the
17 // constructor.
18 class UnloadDetachedHandler::WebContentsDelegateImpl
19 : public content::WebContentsDelegate {
20 public:
21 WebContentsDelegateImpl(UnloadDetachedHandler* handler,
22 TabContents* tab_contents)
23 : handler_(handler),
24 tab_contents_(tab_contents) {
25 tab_contents->web_contents()->SetDelegate(this);
26 }
27
28 virtual bool ShouldSuppressDialogs() OVERRIDE {
29 return true; // Return true so dialogs are suppressed.
30 }
31
32 virtual void CloseContents(content::WebContents* source) OVERRIDE {
33 handler_->Destroy(this);
34 }
35
36 private:
37 UnloadDetachedHandler* const handler_;
38 scoped_ptr<TabContents> tab_contents_;
39
40 DISALLOW_IMPLICIT_CONSTRUCTORS(WebContentsDelegateImpl);
41 };
42
43 ////////////////////////////////////////////////////////////////////////////////
44 // UnloadDetachedHandler implementation.
45
46 UnloadDetachedHandler::UnloadDetachedHandler(const TabsClosedCallback& callback)
47 : tabs_closed_callback_(callback) {
48 }
49
50 UnloadDetachedHandler::~UnloadDetachedHandler() {
51 }
52
53 bool UnloadDetachedHandler::DetachWebContents(
54 TabStripModel* tab_strip_model, content::WebContents* web_contents) {
55 if (tab_strip_model->count() > 1) {
56 // Only allow tab to be detached if it is not the last one.
57 // Otherwise, the tab_strip_model would notify the browser that it is empty.
58 // And, the browser would close without waiting for the unload handlers.
59 int index = tab_strip_model->GetIndexOfWebContents(web_contents);
60 if (index != TabStripModel::kNoTab &&
61 web_contents->NeedToFireBeforeUnload()) {
62 TabContents* tab_contents = tab_strip_model->DetachTabContentsAt(index);
63 delegates_.push_back(new WebContentsDelegateImpl(this, tab_contents));
Ben Goodger (Google) 2012/10/29 22:12:43 rather than creating a delegate per-tab, can you j
slamm 2012/10/31 18:03:59 Done. Thats for the suggestion -- much simpler.
64 web_contents->OnUnloadDetachedStarted();
65 return true;
66 }
67 }
68 return false;
69 }
70
71 bool UnloadDetachedHandler::HasTabs() {
72 return !delegates_.empty();
Ben Goodger (Google) 2012/10/29 22:12:43 if you don't have a list of delegates, you can at
slamm 2012/10/31 18:03:59 I made a web_contents -> tab_contents map. The tab
73 }
74
75 void UnloadDetachedHandler::Destroy(WebContentsDelegateImpl* delegate) {
76 ScopedVector<WebContentsDelegateImpl>::iterator i =
77 std::find(delegates_.begin(), delegates_.end(), delegate);
78 DCHECK(i != delegates_.end());
79
80 // The delegate's method is a caller on the stack, so schedule the deletion
81 // for later.
82 delegates_.weak_erase(i);
83 MessageLoop::current()->DeleteSoon(FROM_HERE, delegate);
84
85 tabs_closed_callback_.Run();
86 }
87
88 } // namespace chrome
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698