OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 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/instant/instant_unload_handler.h" | 5 #include "chrome/browser/instant/instant_unload_handler.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "chrome/browser/ui/browser_navigator.h" | 10 #include "chrome/browser/ui/browser_navigator.h" |
11 #include "content/public/browser/render_view_host.h" | 11 #include "content/public/browser/render_view_host.h" |
12 #include "content/public/browser/web_contents.h" | 12 #include "content/public/browser/web_contents.h" |
13 #include "content/public/browser/web_contents_delegate.h" | 13 #include "content/public/browser/web_contents_delegate.h" |
14 | 14 |
15 // WebContentsDelegate implementation. This owns the WebContents supplied to the | |
16 // constructor. | |
17 class InstantUnloadHandler::WebContentsDelegateImpl | 15 class InstantUnloadHandler::WebContentsDelegateImpl |
18 : public content::WebContentsDelegate { | 16 : public content::WebContentsDelegate { |
19 public: | 17 public: |
20 WebContentsDelegateImpl(InstantUnloadHandler* handler, | 18 WebContentsDelegateImpl(InstantUnloadHandler* handler, |
21 content::WebContents* contents, | 19 scoped_ptr<content::WebContents> contents, |
22 int index) | 20 int index) |
23 : handler_(handler), | 21 : handler_(handler), |
24 contents_(contents), | 22 contents_(contents.Pass()), |
25 index_(index) { | 23 index_(index) { |
26 contents->SetDelegate(this); | 24 contents_->SetDelegate(this); |
| 25 contents_->GetRenderViewHost()->FirePageBeforeUnload(false); |
27 } | 26 } |
28 | 27 |
29 // content::WebContentsDelegate overrides: | 28 // Overridden from content::WebContentsDelegate: |
| 29 virtual void CloseContents(content::WebContents* source) OVERRIDE { |
| 30 DCHECK_EQ(contents_, source); |
| 31 // Remove ourselves as the delegate, so that CloseContents() won't be |
| 32 // called twice, leading to double deletion (http://crbug.com/155848). |
| 33 contents_->SetDelegate(NULL); |
| 34 handler_->Destroy(this); |
| 35 } |
| 36 |
30 virtual void WillRunBeforeUnloadConfirm() OVERRIDE { | 37 virtual void WillRunBeforeUnloadConfirm() OVERRIDE { |
31 content::WebContents* contents = contents_.release(); | 38 contents_->SetDelegate(NULL); |
32 contents->SetDelegate(NULL); | 39 handler_->Activate(this, contents_.Pass(), index_); |
33 handler_->Activate(this, contents, index_); | |
34 } | 40 } |
35 | 41 |
36 virtual bool ShouldSuppressDialogs() OVERRIDE { | 42 virtual bool ShouldSuppressDialogs() OVERRIDE { |
37 return true; // Return true so dialogs are suppressed. | 43 return true; |
38 } | |
39 | |
40 virtual void CloseContents(content::WebContents* source) OVERRIDE { | |
41 contents_->SetDelegate(NULL); | |
42 handler_->Destroy(this); | |
43 } | 44 } |
44 | 45 |
45 private: | 46 private: |
46 InstantUnloadHandler* const handler_; | 47 InstantUnloadHandler* const handler_; |
47 scoped_ptr<content::WebContents> contents_; | 48 scoped_ptr<content::WebContents> contents_; |
48 | 49 |
49 // The index |contents_| was originally at. If we add the tab back we add it | 50 // The tab strip index |contents_| was originally at. If we add the tab back |
50 // at this index. | 51 // to the tabstrip, we add it at this index. |
51 const int index_; | 52 const int index_; |
52 | 53 |
53 DISALLOW_COPY_AND_ASSIGN(WebContentsDelegateImpl); | 54 DISALLOW_COPY_AND_ASSIGN(WebContentsDelegateImpl); |
54 }; | 55 }; |
55 | 56 |
56 InstantUnloadHandler::InstantUnloadHandler(Browser* browser) | 57 InstantUnloadHandler::InstantUnloadHandler(Browser* browser) |
57 : browser_(browser) { | 58 : browser_(browser) { |
58 } | 59 } |
59 | 60 |
60 InstantUnloadHandler::~InstantUnloadHandler() { | 61 InstantUnloadHandler::~InstantUnloadHandler() { |
61 } | 62 } |
62 | 63 |
63 void InstantUnloadHandler::RunUnloadListenersOrDestroy( | 64 void InstantUnloadHandler::RunUnloadListenersOrDestroy( |
64 content::WebContents* contents, | 65 scoped_ptr<content::WebContents> contents, |
65 int index) { | 66 int index) { |
66 DCHECK(!contents->GetDelegate()); | 67 DCHECK(!contents->GetDelegate()); |
67 | 68 |
68 if (!contents->NeedToFireBeforeUnload()) { | 69 if (!contents->NeedToFireBeforeUnload()) { |
69 // Tab doesn't have any beforeunload listeners and can be safely deleted. | 70 // Tab doesn't have any beforeunload listeners and can be safely deleted. |
70 // However, the tab object should not be deleted immediately because when we | 71 // However, the tab object should not be deleted immediately because when we |
71 // get here from BrowserInstantController::TabDeactivated, other tab | 72 // get here from BrowserInstantController::TabDeactivated, other tab |
72 // observers may still expect to interact with the tab before the event has | 73 // observers may still expect to interact with the tab before the event has |
73 // finished propagating. | 74 // finished propagating. |
74 MessageLoop::current()->DeleteSoon(FROM_HERE, contents); | 75 MessageLoop::current()->DeleteSoon(FROM_HERE, contents.release()); |
75 return; | 76 return; |
76 } | 77 } |
77 | 78 |
78 // Tab has before unload listener. Install a delegate and fire the before | 79 // Tab has beforeunload listeners. Install a delegate to run them. |
79 // unload listener. | 80 delegates_.push_back( |
80 delegates_.push_back(new WebContentsDelegateImpl(this, contents, index)); | 81 new WebContentsDelegateImpl(this, contents.Pass(), index)); |
81 contents->GetRenderViewHost()->FirePageBeforeUnload(false); | |
82 } | 82 } |
83 | 83 |
84 void InstantUnloadHandler::Activate(WebContentsDelegateImpl* delegate, | 84 void InstantUnloadHandler::Activate(WebContentsDelegateImpl* delegate, |
85 content::WebContents* contents, | 85 scoped_ptr<content::WebContents> contents, |
86 int index) { | 86 int index) { |
87 chrome::NavigateParams params(browser_, contents); | |
88 params.disposition = NEW_FOREGROUND_TAB; | |
89 params.tabstrip_index = index; | |
90 | |
91 // Remove (and delete) the delegate. | 87 // Remove (and delete) the delegate. |
92 Destroy(delegate); | 88 Destroy(delegate); |
93 | 89 |
94 // Add the tab back in. | 90 // Add the tab back in. |
| 91 chrome::NavigateParams params(browser_, contents.release()); |
| 92 params.disposition = NEW_FOREGROUND_TAB; |
| 93 params.tabstrip_index = index; |
95 chrome::Navigate(¶ms); | 94 chrome::Navigate(¶ms); |
96 } | 95 } |
97 | 96 |
98 void InstantUnloadHandler::Destroy(WebContentsDelegateImpl* delegate) { | 97 void InstantUnloadHandler::Destroy(WebContentsDelegateImpl* delegate) { |
99 ScopedVector<WebContentsDelegateImpl>::iterator i = | 98 ScopedVector<WebContentsDelegateImpl>::iterator i = |
100 std::find(delegates_.begin(), delegates_.end(), delegate); | 99 std::find(delegates_.begin(), delegates_.end(), delegate); |
101 DCHECK(i != delegates_.end()); | 100 DCHECK(i != delegates_.end()); |
102 | 101 |
103 // The delegate's method is a caller on the stack, so schedule the deletion | 102 // The delegate's method is a caller on the stack, so schedule the deletion |
104 // for later. | 103 // for later. |
105 delegates_.weak_erase(i); | 104 delegates_.weak_erase(i); |
106 MessageLoop::current()->DeleteSoon(FROM_HERE, delegate); | 105 MessageLoop::current()->DeleteSoon(FROM_HERE, delegate); |
107 } | 106 } |
OLD | NEW |