Revert of Add button to add new FSP services to Files app. (patchset #8 id:140001...
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / pdf_password_dialog_cocoa.mm
blob7cd83d820318ee2881c8560591e7040c21db0fb1
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 #import <Cocoa/Cocoa.h>
7 #include "base/mac/scoped_nsobject.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/strings/sys_string_conversions.h"
10 #include "base/strings/utf_string_conversions.h"
11 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_alert.h"
12 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_custom_sheet.h"
13 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_mac.h"
14 #import "chrome/browser/ui/cocoa/key_equivalent_constants.h"
15 #include "chrome/grit/generated_resources.h"
16 #include "components/pdf/browser/pdf_web_contents_helper_client.h"
17 #include "ui/base/l10n/l10n_util.h"
19 @class PDFPasswordDialogMac;
21 namespace {
23 class PDFPasswordDialogMacBridge : public ConstrainedWindowMacDelegate {
24  public:
25   explicit PDFPasswordDialogMacBridge(PDFPasswordDialogMac* dialog);
26   virtual ~PDFPasswordDialogMacBridge();
27   void OnConstrainedWindowClosed(ConstrainedWindowMac* window) override;
29  private:
30   PDFPasswordDialogMac* dialog_;  // weak
32   DISALLOW_COPY_AND_ASSIGN(PDFPasswordDialogMacBridge);
35 }  // namespace
37 @interface PDFPasswordDialogMac : NSObject {
38  @private
39   content::WebContents* webContents_;
40   base::string16 prompt_;
41   pdf::PasswordDialogClosedCallback callback_;
43   base::scoped_nsobject<NSSecureTextField> passwordField_;
45   base::scoped_nsobject<ConstrainedWindowAlert> alert_;
46   scoped_ptr<PDFPasswordDialogMacBridge> bridge_;
47   scoped_ptr<ConstrainedWindowMac> window_;
49 - (id)initWithWebContents:(content::WebContents*)webContents
50                    prompt:(base::string16)prompt
51                  callback:(pdf::PasswordDialogClosedCallback)callback;
52 - (void)onOKButton:(id)sender;
53 - (void)onCancelButton:(id)sender;
54 @end
56 namespace {
58 PDFPasswordDialogMacBridge::PDFPasswordDialogMacBridge(
59     PDFPasswordDialogMac* dialog) : dialog_(dialog) {
62 PDFPasswordDialogMacBridge::~PDFPasswordDialogMacBridge() {
65 void PDFPasswordDialogMacBridge::OnConstrainedWindowClosed(
66     ConstrainedWindowMac* window) {
67   [dialog_ release];
70 }  // namespace
72 @implementation PDFPasswordDialogMac
74 - (id)initWithWebContents:(content::WebContents*)webContents
75                    prompt:(base::string16)prompt
76                  callback:(pdf::PasswordDialogClosedCallback)callback {
77   if ((self = [super init])) {
78     webContents_ = webContents;
79     prompt_ = prompt;
80     callback_ = callback;
82     alert_.reset([[ConstrainedWindowAlert alloc] init]);
83     [alert_ setMessageText:
84         l10n_util::GetNSString(IDS_PDF_PASSWORD_DIALOG_TITLE)];
85     [alert_ setInformativeText:base::SysUTF16ToNSString(prompt)];
86     [alert_ addButtonWithTitle:l10n_util::GetNSString(IDS_OK)
87                  keyEquivalent:kKeyEquivalentReturn
88                         target:self
89                         action:@selector(onOKButton:)];
90     [alert_ addButtonWithTitle:l10n_util::GetNSString(IDS_CANCEL)
91                  keyEquivalent:kKeyEquivalentEscape
92                         target:self
93                         action:@selector(onCancelButton:)];
94     [[alert_ closeButton] setTarget:self];
95     [[alert_ closeButton] setAction:@selector(onCancelButton:)];
97     passwordField_.reset(
98         [[NSSecureTextField alloc] initWithFrame:NSMakeRect(0, 0, 300, 22)]);
99     [alert_ setAccessoryView:passwordField_];
101     [alert_ layout];
103     base::scoped_nsobject<CustomConstrainedWindowSheet> sheet(
104         [[CustomConstrainedWindowSheet alloc]
105             initWithCustomWindow:[alert_ window]]);
106     bridge_.reset(new PDFPasswordDialogMacBridge(self));
107     window_.reset(new ConstrainedWindowMac(bridge_.get(), webContents_, sheet));
108   }
109   return self;
112 - (void)dealloc {
113   if (!callback_.is_null()) {
114     // This dialog was torn down without either OK or cancel being clicked; be
115     // considerate and at least do the callback.
116     callback_.Run(false, base::string16());
117   }
118   [super dealloc];
121 - (void)onOKButton:(id)sender {
122   callback_.Run(true, base::SysNSStringToUTF16([passwordField_ stringValue]));
123   callback_.Reset();
124   window_->CloseWebContentsModalDialog();
127 - (void)onCancelButton:(id)sender {
128   callback_.Run(false, base::string16());
129   callback_.Reset();
130   window_->CloseWebContentsModalDialog();
133 @end
135 void ShowPDFPasswordDialog(content::WebContents* web_contents,
136                            const base::string16& prompt,
137                            const pdf::PasswordDialogClosedCallback& callback) {
138   [[PDFPasswordDialogMac alloc] initWithWebContents:web_contents
139                                              prompt:prompt
140                                            callback:callback];