summaryrefslogtreecommitdiffstats
path: root/chrome/browser/cocoa/repost_form_warning_mac.mm
blob: 31292c04a160e84b4e5b884c10a807ffd7bd4e65 (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// 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/cocoa/repost_form_warning_mac.h"

#include "app/l10n_util_mac.h"
#include "base/message_loop.h"
#include "chrome/browser/tab_contents/navigation_controller.h"
#include "chrome/common/notification_service.h"
#include "grit/generated_resources.h"

// The delegate of the NSAlert used to display the dialog. Forwards the alert's
// completion event to the C++ class |RepostFormWarningMac|.
@interface RepostDelegate : NSObject {
  RepostFormWarningMac* warning_;  // weak, owns us.
}
- (id)initWithWarning:(RepostFormWarningMac*)warning;
- (void)alertDidEnd:(NSAlert*)alert
         returnCode:(int)returnCode
        contextInfo:(void*)contextInfo;
@end

@implementation RepostDelegate
- (id)initWithWarning:(RepostFormWarningMac*)warning {
  if ((self = [super init])) {
    warning_ = warning;
  }
  return self;
}

- (void)alertDidEnd:(NSAlert*)alert
         returnCode:(int)returnCode
        contextInfo:(void*)contextInfo {
  if (returnCode == NSAlertFirstButtonReturn)
    warning_->Confirm();
  else
    warning_->Cancel();
}
@end

RepostFormWarningMac::RepostFormWarningMac(
    NSWindow* parent,
    TabContents* tab_contents)
    : ConstrainedWindowMacDelegateSystemSheet(
        [[[RepostDelegate alloc] initWithWarning:this] autorelease],
        @selector(alertDidEnd:returnCode:contextInfo:)),
      navigation_controller_(&tab_contents->controller()) {
  scoped_nsobject<NSAlert> alert([[NSAlert alloc] init]);
  [alert setMessageText:
      l10n_util::GetNSStringWithFixup(IDS_HTTP_POST_WARNING_TITLE)];
  [alert setInformativeText:
      l10n_util::GetNSStringWithFixup(IDS_HTTP_POST_WARNING)];
  [alert addButtonWithTitle:
      l10n_util::GetNSStringWithFixup(IDS_HTTP_POST_WARNING_RESEND)];
  [alert addButtonWithTitle:
      l10n_util::GetNSStringWithFixup(IDS_CANCEL)];

  set_sheet(alert);

  registrar_.Add(this, NotificationType::LOAD_START,
                 Source<NavigationController>(navigation_controller_));
  registrar_.Add(this, NotificationType::TAB_CLOSING,
                 Source<NavigationController>(navigation_controller_));
  registrar_.Add(this, NotificationType::RELOADING,
                 Source<NavigationController>(navigation_controller_));
  window_ = tab_contents->CreateConstrainedDialog(this);
}

RepostFormWarningMac::~RepostFormWarningMac() {
}

void RepostFormWarningMac::DeleteDelegate() {
  window_ = NULL;
  Dismiss();
  delete this;
}

void RepostFormWarningMac::Confirm() {
  if (navigation_controller_) {
    navigation_controller_->ContinuePendingReload();
  }
  Destroy();
}

void RepostFormWarningMac::Cancel() {
  if (navigation_controller_) {
    navigation_controller_->CancelPendingReload();
  }
  Destroy();
}

void RepostFormWarningMac::Dismiss() {
  if (sheet() && is_sheet_open()) {
    // This will call |Cancel()|.
    [NSApp endSheet:[(NSAlert*)sheet() window]
         returnCode:NSAlertSecondButtonReturn];
  }
}

void RepostFormWarningMac::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 ((type == NotificationType::LOAD_START ||
       type == NotificationType::TAB_CLOSING ||
       type == NotificationType::RELOADING)) {
    DCHECK_EQ(Source<NavigationController>(source).ptr(),
              navigation_controller_);
    Dismiss();
  }
}

void RepostFormWarningMac::Destroy() {
  navigation_controller_ = NULL;
  if (sheet()) {
    set_sheet(nil);
  }
  if (window_) {
    window_->CloseConstrainedWindow();
  }
}