Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 | |
| OLD | NEW |