| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/repost_form_warning_controller.h" | 5 #include "chrome/browser/repost_form_warning_controller.h" |
| 6 | 6 |
| 7 #include "chrome/browser/tab_contents/tab_contents.h" | 7 #include "chrome/browser/tab_contents/tab_contents.h" |
| 8 #include "chrome/common/notification_service.h" | 8 #include "chrome/common/notification_service.h" |
| 9 | 9 |
| 10 RepostFormWarningController::RepostFormWarningController( | 10 RepostFormWarningController::RepostFormWarningController( |
| 11 TabContents* tab_contents) | 11 TabContents* tab_contents) |
| 12 : tab_contents_(tab_contents), | 12 : tab_contents_(tab_contents), |
| 13 window_(NULL) { | 13 window_(NULL) { |
| 14 NavigationController* controller = &tab_contents->controller(); | 14 NavigationController* controller = &tab_contents->controller(); |
| 15 registrar_.Add(this, NotificationType::LOAD_START, | 15 registrar_.Add(this, NotificationType::LOAD_START, |
| 16 Source<NavigationController>(controller)); | 16 Source<NavigationController>(controller)); |
| 17 registrar_.Add(this, NotificationType::TAB_CLOSING, | 17 registrar_.Add(this, NotificationType::TAB_CLOSING, |
| 18 Source<NavigationController>(controller)); | 18 Source<NavigationController>(controller)); |
| 19 registrar_.Add(this, NotificationType::REPOST_WARNING_SHOWN, | 19 registrar_.Add(this, NotificationType::REPOST_WARNING_SHOWN, |
| 20 Source<NavigationController>(controller)); | 20 Source<NavigationController>(controller)); |
| 21 } | 21 } |
| 22 | 22 |
| 23 RepostFormWarningController::~RepostFormWarningController() { | 23 RepostFormWarningController::~RepostFormWarningController() { |
| 24 // If we end up here, the constrained window has been closed, so make sure we |
| 25 // don't close it again. |
| 26 window_ = NULL; |
| 27 // Make sure everything is cleaned up. |
| 28 Cancel(); |
| 24 } | 29 } |
| 25 | 30 |
| 26 void RepostFormWarningController::Show( | 31 void RepostFormWarningController::Show( |
| 27 ConstrainedWindowDelegate* window_delegate) { | 32 ConstrainedWindowDelegate* window_delegate) { |
| 28 window_ = tab_contents_->CreateConstrainedDialog(window_delegate); | 33 window_ = tab_contents_->CreateConstrainedDialog(window_delegate); |
| 29 } | 34 } |
| 30 | 35 |
| 31 void RepostFormWarningController::Cancel() { | 36 void RepostFormWarningController::Cancel() { |
| 32 if (tab_contents_) { | 37 if (tab_contents_) { |
| 33 tab_contents_->controller().CancelPendingReload(); | 38 tab_contents_->controller().CancelPendingReload(); |
| 34 CloseDialog(); | 39 CloseDialog(); |
| 35 } | 40 } |
| 36 } | 41 } |
| 37 | 42 |
| 38 void RepostFormWarningController::Continue() { | 43 void RepostFormWarningController::Continue() { |
| 39 if (tab_contents_) { | 44 if (tab_contents_) { |
| 40 tab_contents_->controller().ContinuePendingReload(); | 45 tab_contents_->controller().ContinuePendingReload(); |
| 41 // If we reload the page, the dialog will be closed anyway. | 46 // If we reload the page, the dialog will be closed anyway. |
| 42 } | 47 } |
| 43 } | 48 } |
| 44 | 49 |
| 45 void RepostFormWarningController::Observe(NotificationType type, | 50 void RepostFormWarningController::Observe(NotificationType type, |
| 46 const NotificationSource& source, | 51 const NotificationSource& source, |
| 47 const NotificationDetails& details) { | 52 const NotificationDetails& details) { |
| 48 // Close the dialog if we load a page (because reloading might not apply to | 53 // Close the dialog if we load a page (because reloading might not apply to |
| 49 // the same page anymore) or if the tab is closed, because then we won't have | 54 // the same page anymore) or if the tab is closed, because then we won't have |
| 50 // a navigation controller anymore. | 55 // a navigation controller anymore. |
| 51 if ((type == NotificationType::LOAD_START || | 56 if (tab_contents_ && |
| 57 (type == NotificationType::LOAD_START || |
| 52 type == NotificationType::TAB_CLOSING || | 58 type == NotificationType::TAB_CLOSING || |
| 53 type == NotificationType::REPOST_WARNING_SHOWN)) { | 59 type == NotificationType::REPOST_WARNING_SHOWN)) { |
| 54 DCHECK_EQ(Source<NavigationController>(source).ptr(), | 60 DCHECK_EQ(Source<NavigationController>(source).ptr(), |
| 55 &tab_contents_->controller()); | 61 &tab_contents_->controller()); |
| 56 Cancel(); | 62 Cancel(); |
| 57 } | 63 } |
| 58 } | 64 } |
| 59 | 65 |
| 60 void RepostFormWarningController::CloseDialog() { | 66 void RepostFormWarningController::CloseDialog() { |
| 67 // Make sure we won't do anything when |Cancel()| is called again. |
| 61 tab_contents_ = NULL; | 68 tab_contents_ = NULL; |
| 62 if (window_) { | 69 if (window_) { |
| 63 window_->CloseConstrainedWindow(); | 70 window_->CloseConstrainedWindow(); |
| 64 } | 71 } |
| 65 delete this; | |
| 66 } | 72 } |
| OLD | NEW |