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;
23 class PDFPasswordDialogMacBridge : public ConstrainedWindowMacDelegate {
25 explicit PDFPasswordDialogMacBridge(PDFPasswordDialogMac* dialog);
26 virtual ~PDFPasswordDialogMacBridge();
27 void OnConstrainedWindowClosed(ConstrainedWindowMac* window) override;
30 PDFPasswordDialogMac* dialog_; // weak
32 DISALLOW_COPY_AND_ASSIGN(PDFPasswordDialogMacBridge);
37 @interface PDFPasswordDialogMac : NSObject {
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;
58 PDFPasswordDialogMacBridge::PDFPasswordDialogMacBridge(
59 PDFPasswordDialogMac* dialog) : dialog_(dialog) {
62 PDFPasswordDialogMacBridge::~PDFPasswordDialogMacBridge() {
65 void PDFPasswordDialogMacBridge::OnConstrainedWindowClosed(
66 ConstrainedWindowMac* window) {
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;
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
89 action:@selector(onOKButton:)];
90 [alert_ addButtonWithTitle:l10n_util::GetNSString(IDS_CANCEL)
91 keyEquivalent:kKeyEquivalentEscape
93 action:@selector(onCancelButton:)];
94 [[alert_ closeButton] setTarget:self];
95 [[alert_ closeButton] setAction:@selector(onCancelButton:)];
98 [[NSSecureTextField alloc] initWithFrame:NSMakeRect(0, 0, 300, 22)]);
99 [alert_ setAccessoryView:passwordField_];
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));
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());
121 - (void)onOKButton:(id)sender {
122 callback_.Run(true, base::SysNSStringToUTF16([passwordField_ stringValue]));
124 window_->CloseWebContentsModalDialog();
127 - (void)onCancelButton:(id)sender {
128 callback_.Run(false, base::string16());
130 window_->CloseWebContentsModalDialog();
135 void ShowPDFPasswordDialog(content::WebContents* web_contents,
136 const base::string16& prompt,
137 const pdf::PasswordDialogClosedCallback& callback) {
138 [[PDFPasswordDialogMac alloc] initWithWebContents:web_contents