Upstream version 11.39.266.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / cocoa / pdf_password_dialog.mm
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.
4
5 #import <Cocoa/Cocoa.h>
6
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"
18
19 @class PDFPasswordDialogMac;
20
21 namespace {
22
23 class PDFPasswordDialogMacBridge : public ConstrainedWindowMacDelegate {
24  public:
25   explicit PDFPasswordDialogMacBridge(PDFPasswordDialogMac* dialog);
26   virtual ~PDFPasswordDialogMacBridge();
27   virtual void OnConstrainedWindowClosed(ConstrainedWindowMac* window) OVERRIDE;
28
29  private:
30   PDFPasswordDialogMac* dialog_;  // weak
31
32   DISALLOW_COPY_AND_ASSIGN(PDFPasswordDialogMacBridge);
33 };
34
35 }  // namespace
36
37 @interface PDFPasswordDialogMac : NSObject {
38  @private
39   content::WebContents* webContents_;
40   base::string16 prompt_;
41   pdf::PasswordDialogClosedCallback callback_;
42
43   base::scoped_nsobject<NSSecureTextField> passwordField_;
44
45   base::scoped_nsobject<ConstrainedWindowAlert> alert_;
46   scoped_ptr<PDFPasswordDialogMacBridge> bridge_;
47   scoped_ptr<ConstrainedWindowMac> window_;
48 }
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
55
56 namespace {
57
58 PDFPasswordDialogMacBridge::PDFPasswordDialogMacBridge(
59     PDFPasswordDialogMac* dialog) : dialog_(dialog) {
60 }
61
62 PDFPasswordDialogMacBridge::~PDFPasswordDialogMacBridge() {
63 }
64
65 void PDFPasswordDialogMacBridge::OnConstrainedWindowClosed(
66     ConstrainedWindowMac* window) {
67   [dialog_ release];
68 }
69
70 }  // namespace
71
72 @implementation PDFPasswordDialogMac
73
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;
81
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:)];
96
97     passwordField_.reset(
98         [[NSSecureTextField alloc] initWithFrame:NSMakeRect(0, 0, 300, 22)]);
99     [alert_ setAccessoryView:passwordField_];
100
101     [alert_ layout];
102
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;
110 }
111
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];
119 }
120
121 - (void)onOKButton:(id)sender {
122   callback_.Run(true, base::SysNSStringToUTF16([passwordField_ stringValue]));
123   callback_.Reset();
124   window_->CloseWebContentsModalDialog();
125 }
126
127 - (void)onCancelButton:(id)sender {
128   callback_.Run(false, base::string16());
129   callback_.Reset();
130   window_->CloseWebContentsModalDialog();
131 }
132
133 @end
134
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];
141 }