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 #ifndef CHROME_BROWSER_INSTANT_INSTANT_UNLOAD_HANDLER_H_ | 5 #ifndef CHROME_BROWSER_INSTANT_INSTANT_UNLOAD_HANDLER_H_ |
6 #define CHROME_BROWSER_INSTANT_INSTANT_UNLOAD_HANDLER_H_ | 6 #define CHROME_BROWSER_INSTANT_INSTANT_UNLOAD_HANDLER_H_ |
7 | 7 |
8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
| 9 #include "base/memory/scoped_ptr.h" |
9 #include "base/memory/scoped_vector.h" | 10 #include "base/memory/scoped_vector.h" |
10 | 11 |
11 class Browser; | 12 class Browser; |
12 | 13 |
13 namespace content { | 14 namespace content { |
14 class WebContents; | 15 class WebContents; |
15 } | 16 } |
16 | 17 |
17 // InstantUnloadHandler ensures that the beforeunload and unload handlers are | 18 // InstantUnloadHandler ensures that it runs the BeforeUnload/Unload Handlers |
18 // run when using Instant. When the user commits the Instant preview the | 19 // (BUH) of a page if the page is replaced by an Instant overlay. |
19 // existing WebContents is passed to RunUnloadListenersOrDestroy(). If the tab | 20 // |
20 // has no beforeunload or unload listeners, the tab is deleted; otherwise the | 21 // Why is this needed? Say the user is looking at a page P. They then try to |
21 // beforeunload and unload listeners are executed. If the beforeunload listener | 22 // navigate to another page Q. Consider what happens with and without Instant: |
22 // shows a dialog the tab is added back to the tabstrip at its original location | 23 // |
23 // next to the Instant page. | 24 // Without Instant: Before the navigation is committed, P's BUH are run. If P's |
| 25 // BUH return a string (instead of the default null), the user is prompted to |
| 26 // "Stay or Leave?". If the user clicks "Stay", the navigation is cancelled, |
| 27 // and the user remains on P. |
| 28 // |
| 29 // With Instant: The navigation to Q has already happened, since Q is being |
| 30 // shown as a preview (overlay). When the user "commits" the overlay, it's too |
| 31 // late to cancel Q based on P's BUH. So, Instant just replaces P with Q and |
| 32 // passes P to InstantUnloadHandler::RunUnloadListenersOrDestroy(). This class |
| 33 // runs P's BUH in the background. If the "Stay or Leave?" dialog needs to be |
| 34 // shown, it adds P back onto the tabstrip, next to Q. Otherwise, P is deleted. |
24 class InstantUnloadHandler { | 35 class InstantUnloadHandler { |
25 public: | 36 public: |
26 explicit InstantUnloadHandler(Browser* browser); | 37 explicit InstantUnloadHandler(Browser* browser); |
27 ~InstantUnloadHandler(); | 38 ~InstantUnloadHandler(); |
28 | 39 |
29 // See class description for details on what this does. | 40 // See class description for details on what this does. |
30 void RunUnloadListenersOrDestroy(content::WebContents* contents, int index); | 41 void RunUnloadListenersOrDestroy(scoped_ptr<content::WebContents> contents, |
| 42 int index); |
31 | 43 |
32 private: | 44 private: |
33 class WebContentsDelegateImpl; | 45 class WebContentsDelegateImpl; |
34 | 46 |
35 // Invoked if the tab is to be shown, at |index| on the tab strip. This | 47 // Invoked if the tab is to be shown, at |index| on the tab strip. This |
36 // happens if the before unload listener returns a string. Takes ownership of | 48 // happens if the beforeunload listener returns a string. |
37 // |delegate| and |contents|. | |
38 void Activate(WebContentsDelegateImpl* delegate, | 49 void Activate(WebContentsDelegateImpl* delegate, |
39 content::WebContents* contents, | 50 scoped_ptr<content::WebContents> contents, |
40 int index); | 51 int index); |
41 | 52 |
42 // Destroys the old tab. This is invoked if script tries to close the page. | 53 // Destroys the old tab. This is invoked if script tries to close the page. |
43 void Destroy(WebContentsDelegateImpl* delegate); | 54 void Destroy(WebContentsDelegateImpl* delegate); |
44 | 55 |
45 // TODO(sky): Browser really needs to wait to close until there are no more | 56 // TODO(sky): Browser really needs to wait to close until there are no more |
46 // tabs managed by InstantUnloadHandler. | 57 // tabs managed by InstantUnloadHandler. |
47 Browser* const browser_; | 58 Browser* const browser_; |
48 | 59 |
49 ScopedVector<WebContentsDelegateImpl> delegates_; | 60 ScopedVector<WebContentsDelegateImpl> delegates_; |
50 | 61 |
51 DISALLOW_COPY_AND_ASSIGN(InstantUnloadHandler); | 62 DISALLOW_COPY_AND_ASSIGN(InstantUnloadHandler); |
52 }; | 63 }; |
53 | 64 |
54 #endif // CHROME_BROWSER_INSTANT_INSTANT_UNLOAD_HANDLER_H_ | 65 #endif // CHROME_BROWSER_INSTANT_INSTANT_UNLOAD_HANDLER_H_ |
OLD | NEW |