[Metrics] Make MetricsStateManager take a callback param to check if UMA is enabled.
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / pdf_password_dialog.mm
bloba7f75a86d918a46e539ce8d4861742c745715faa
1 // Copyright 2013 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.
5 #include "chrome/browser/ui/pdf/pdf_tab_helper.h"
7 #import <Cocoa/Cocoa.h>
9 #include "base/mac/scoped_nsobject.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/strings/sys_string_conversions.h"
12 #include "base/strings/utf_string_conversions.h"
13 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_alert.h"
14 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_custom_sheet.h"
15 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_mac.h"
16 #import "chrome/browser/ui/cocoa/key_equivalent_constants.h"
17 #include "grit/generated_resources.h"
18 #include "ui/base/l10n/l10n_util.h"
20 @class PDFPasswordDialogMac;
22 namespace {
24 class PDFPasswordDialogMacBridge : public ConstrainedWindowMacDelegate {
25  public:
26   explicit PDFPasswordDialogMacBridge(PDFPasswordDialogMac* dialog);
27   virtual ~PDFPasswordDialogMacBridge();
28   virtual void OnConstrainedWindowClosed(ConstrainedWindowMac* window) OVERRIDE;
30  private:
31   PDFPasswordDialogMac* dialog_;  // weak
33   DISALLOW_COPY_AND_ASSIGN(PDFPasswordDialogMacBridge);
36 }  // namespace
38 @interface PDFPasswordDialogMac : NSObject {
39  @private
40   content::WebContents* webContents_;
41   base::string16 prompt_;
42   PasswordDialogClosedCallback callback_;
44   base::scoped_nsobject<NSSecureTextField> passwordField_;
46   base::scoped_nsobject<ConstrainedWindowAlert> alert_;
47   scoped_ptr<PDFPasswordDialogMacBridge> bridge_;
48   scoped_ptr<ConstrainedWindowMac> window_;
50 - (id)initWithWebContents:(content::WebContents*)webContents
51                    prompt:(base::string16)prompt
52                  callback:(PasswordDialogClosedCallback)callback;
53 - (void)onOKButton:(id)sender;
54 - (void)onCancelButton:(id)sender;
55 @end
57 namespace {
59 PDFPasswordDialogMacBridge::PDFPasswordDialogMacBridge(
60     PDFPasswordDialogMac* dialog) : dialog_(dialog) {
63 PDFPasswordDialogMacBridge::~PDFPasswordDialogMacBridge() {
66 void PDFPasswordDialogMacBridge::OnConstrainedWindowClosed(
67     ConstrainedWindowMac* window) {
68   [dialog_ release];
71 }  // namespace
73 @implementation PDFPasswordDialogMac
75 - (id)initWithWebContents:(content::WebContents*)webContents
76                    prompt:(base::string16)prompt
77                  callback:(PasswordDialogClosedCallback)callback {
78   if ((self = [super init])) {
79     webContents_ = webContents;
80     prompt_ = prompt;
81     callback_ = callback;
83     alert_.reset([[ConstrainedWindowAlert alloc] init]);
84     [alert_ setMessageText:
85         l10n_util::GetNSString(IDS_PDF_PASSWORD_DIALOG_TITLE)];
86     [alert_ setInformativeText:base::SysUTF16ToNSString(prompt)];
87     [alert_ addButtonWithTitle:l10n_util::GetNSString(IDS_OK)
88                  keyEquivalent:kKeyEquivalentReturn
89                         target:self
90                         action:@selector(onOKButton:)];
91     [alert_ addButtonWithTitle:l10n_util::GetNSString(IDS_CANCEL)
92                  keyEquivalent:kKeyEquivalentEscape
93                         target:self
94                         action:@selector(onCancelButton:)];
95     [[alert_ closeButton] setTarget:self];
96     [[alert_ closeButton] setAction:@selector(onCancelButton:)];
98     passwordField_.reset(
99         [[NSSecureTextField alloc] initWithFrame:NSMakeRect(0, 0, 300, 22)]);
100     [alert_ setAccessoryView:passwordField_];
102     [alert_ layout];
104     base::scoped_nsobject<CustomConstrainedWindowSheet> sheet(
105         [[CustomConstrainedWindowSheet alloc]
106             initWithCustomWindow:[alert_ window]]);
107     bridge_.reset(new PDFPasswordDialogMacBridge(self));
108     window_.reset(new ConstrainedWindowMac(bridge_.get(), webContents_, sheet));
109   }
110   return self;
113 - (void)dealloc {
114   if (!callback_.is_null()) {
115     // This dialog was torn down without either OK or cancel being clicked; be
116     // considerate and at least do the callback.
117     callback_.Run(false, base::string16());
118   }
119   [super dealloc];
122 - (void)onOKButton:(id)sender {
123   callback_.Run(true, base::SysNSStringToUTF16([passwordField_ stringValue]));
124   callback_.Reset();
125   window_->CloseWebContentsModalDialog();
128 - (void)onCancelButton:(id)sender {
129   callback_.Run(false, base::string16());
130   callback_.Reset();
131   window_->CloseWebContentsModalDialog();
134 @end
136 void ShowPDFPasswordDialog(content::WebContents* web_contents,
137                            const base::string16& prompt,
138                            const PasswordDialogClosedCallback& callback) {
139   [[PDFPasswordDialogMac alloc] initWithWebContents:web_contents
140                                              prompt:prompt
141                                            callback:callback];