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;
24 class PDFPasswordDialogMacBridge : public ConstrainedWindowMacDelegate {
26 explicit PDFPasswordDialogMacBridge(PDFPasswordDialogMac* dialog);
27 virtual ~PDFPasswordDialogMacBridge();
28 virtual void OnConstrainedWindowClosed(ConstrainedWindowMac* window) OVERRIDE;
31 PDFPasswordDialogMac* dialog_; // weak
33 DISALLOW_COPY_AND_ASSIGN(PDFPasswordDialogMacBridge);
38 @interface PDFPasswordDialogMac : NSObject {
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;
59 PDFPasswordDialogMacBridge::PDFPasswordDialogMacBridge(
60 PDFPasswordDialogMac* dialog) : dialog_(dialog) {
63 PDFPasswordDialogMacBridge::~PDFPasswordDialogMacBridge() {
66 void PDFPasswordDialogMacBridge::OnConstrainedWindowClosed(
67 ConstrainedWindowMac* window) {
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;
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
90 action:@selector(onOKButton:)];
91 [alert_ addButtonWithTitle:l10n_util::GetNSString(IDS_CANCEL)
92 keyEquivalent:kKeyEquivalentEscape
94 action:@selector(onCancelButton:)];
95 [[alert_ closeButton] setTarget:self];
96 [[alert_ closeButton] setAction:@selector(onCancelButton:)];
99 [[NSSecureTextField alloc] initWithFrame:NSMakeRect(0, 0, 300, 22)]);
100 [alert_ setAccessoryView:passwordField_];
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));
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());
122 - (void)onOKButton:(id)sender {
123 callback_.Run(true, base::SysNSStringToUTF16([passwordField_ stringValue]));
125 window_->CloseWebContentsModalDialog();
128 - (void)onCancelButton:(id)sender {
129 callback_.Run(false, base::string16());
131 window_->CloseWebContentsModalDialog();
136 void ShowPDFPasswordDialog(content::WebContents* web_contents,
137 const base::string16& prompt,
138 const PasswordDialogClosedCallback& callback) {
139 [[PDFPasswordDialogMac alloc] initWithWebContents:web_contents