Home | History | Annotate | Download | only in browser
      1 // Copyright (c) 2011 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/repost_form_warning_controller.h"
      6 
      7 #include "content/browser/tab_contents/tab_contents.h"
      8 #include "content/common/notification_source.h"
      9 
     10 RepostFormWarningController::RepostFormWarningController(
     11     TabContents* tab_contents)
     12     : tab_contents_(tab_contents),
     13       window_(NULL) {
     14   NavigationController* controller = &tab_contents->controller();
     15   registrar_.Add(this, NotificationType::LOAD_START,
     16                  Source<NavigationController>(controller));
     17   registrar_.Add(this, NotificationType::TAB_CLOSING,
     18                  Source<NavigationController>(controller));
     19   registrar_.Add(this, NotificationType::REPOST_WARNING_SHOWN,
     20                  Source<NavigationController>(controller));
     21 }
     22 
     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();
     29 }
     30 
     31 void RepostFormWarningController::Show(
     32     ConstrainedWindowDelegate* window_delegate) {
     33   window_ = tab_contents_->CreateConstrainedDialog(window_delegate);
     34 }
     35 
     36 void RepostFormWarningController::Cancel() {
     37   if (tab_contents_) {
     38     tab_contents_->controller().CancelPendingReload();
     39     CloseDialog();
     40   }
     41 }
     42 
     43 void RepostFormWarningController::Continue() {
     44   if (tab_contents_) {
     45     tab_contents_->controller().ContinuePendingReload();
     46     // If we reload the page, the dialog will be closed anyway.
     47   }
     48 }
     49 
     50 void RepostFormWarningController::Observe(NotificationType type,
     51                                 const NotificationSource& source,
     52                                 const NotificationDetails& details) {
     53   // Close the dialog if we load a page (because reloading might not apply to
     54   // the same page anymore) or if the tab is closed, because then we won't have
     55   // a navigation controller anymore.
     56   if (tab_contents_ &&
     57       (type == NotificationType::LOAD_START ||
     58        type == NotificationType::TAB_CLOSING ||
     59        type == NotificationType::REPOST_WARNING_SHOWN)) {
     60     DCHECK_EQ(Source<NavigationController>(source).ptr(),
     61               &tab_contents_->controller());
     62     Cancel();
     63   }
     64 }
     65 
     66 void RepostFormWarningController::CloseDialog() {
     67   // Make sure we won't do anything when |Cancel()| is called again.
     68   tab_contents_ = NULL;
     69   if (window_) {
     70     window_->CloseConstrainedWindow();
     71   }
     72 }
     73