summaryrefslogtreecommitdiffstats
path: root/chrome/browser/repost_form_warning_controller.cc
blob: f5b9c37023f557f448bfc8451453cd38efcd758d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/repost_form_warning_controller.h"

#include "chrome/browser/tab_contents/tab_contents.h"
#include "chrome/common/notification_source.h"

RepostFormWarningController::RepostFormWarningController(
    TabContents* tab_contents)
    : tab_contents_(tab_contents),
      window_(NULL) {
  NavigationController* controller = &tab_contents->controller();
  registrar_.Add(this, NotificationType::LOAD_START,
                 Source<NavigationController>(controller));
  registrar_.Add(this, NotificationType::TAB_CLOSING,
                 Source<NavigationController>(controller));
  registrar_.Add(this, NotificationType::REPOST_WARNING_SHOWN,
                 Source<NavigationController>(controller));
}

RepostFormWarningController::~RepostFormWarningController() {
  // If we end up here, the constrained window has been closed, so make sure we
  // don't close it again.
  window_ = NULL;
  // Make sure everything is cleaned up.
  Cancel();
}

void RepostFormWarningController::Show(
    ConstrainedWindowDelegate* window_delegate) {
  window_ = tab_contents_->CreateConstrainedDialog(window_delegate);
}

void RepostFormWarningController::Cancel() {
  if (tab_contents_) {
    tab_contents_->controller().CancelPendingReload();
    CloseDialog();
  }
}

void RepostFormWarningController::Continue() {
  if (tab_contents_) {
    tab_contents_->controller().ContinuePendingReload();
    // If we reload the page, the dialog will be closed anyway.
  }
}

void RepostFormWarningController::Observe(NotificationType type,
                                const NotificationSource& source,
                                const NotificationDetails& details) {
  // Close the dialog if we load a page (because reloading might not apply to
  // the same page anymore) or if the tab is closed, because then we won't have
  // a navigation controller anymore.
  if (tab_contents_ &&
      (type == NotificationType::LOAD_START ||
       type == NotificationType::TAB_CLOSING ||
       type == NotificationType::REPOST_WARNING_SHOWN)) {
    DCHECK_EQ(Source<NavigationController>(source).ptr(),
              &tab_contents_->controller());
    Cancel();
  }
}

void RepostFormWarningController::CloseDialog() {
  // Make sure we won't do anything when |Cancel()| is called again.
  tab_contents_ = NULL;
  if (window_) {
    window_->CloseConstrainedWindow();
  }
}