OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 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 #include "chrome/browser/ui/javascript_dialogs/javascript_dialog_cocoa.h" | |
6 | |
7 #import "base/mac/scoped_nsobject.h" | |
8 #include "base/memory/ptr_util.h" | |
9 #include "base/strings/sys_string_conversions.h" | |
10 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_alert.h" | |
11 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_custom_sh eet.h" | |
12 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_mac.h" | |
13 #import "chrome/browser/ui/cocoa/key_equivalent_constants.h" | |
14 #include "ui/base/l10n/l10n_util_mac.h" | |
15 #include "ui/strings/grit/ui_strings.h" | |
16 | |
17 @class JavaScriptDialogCocoaBridge; | |
18 | |
19 class JavaScriptDialogCocoa::JavaScriptDialogCocoaImpl | |
20 : public ConstrainedWindowMacDelegate { | |
21 public: | |
22 JavaScriptDialogCocoaImpl( | |
23 JavaScriptDialogCocoa* parent, | |
24 content::WebContents* parent_web_contents, | |
25 content::WebContents* alerting_web_contents, | |
26 const base::string16& title, | |
27 content::JavaScriptMessageType message_type, | |
28 const base::string16& message_text, | |
29 const base::string16& default_prompt_text, | |
30 const content::JavaScriptDialogManager::DialogClosedCallback& | |
31 dialog_callback); | |
32 virtual ~JavaScriptDialogCocoaImpl() = default; | |
33 | |
34 // Callbacks from the bridge when buttons are clicked. | |
35 void Accept(); | |
36 void Cancel(); | |
37 | |
38 // ConstrainedWindowMacDelegate: | |
39 void OnConstrainedWindowClosed(ConstrainedWindowMac* window) override; | |
40 | |
41 private: | |
42 friend class JavaScriptDialogCocoa; | |
43 | |
44 content::JavaScriptDialogManager::DialogClosedCallback dialog_callback_; | |
45 | |
46 std::unique_ptr<ConstrainedWindowMac> window_; | |
47 base::scoped_nsobject<ConstrainedWindowAlert> alert_; | |
48 base::scoped_nsobject<JavaScriptDialogCocoaBridge> bridge_; | |
49 | |
50 base::scoped_nsobject<NSTextField> textField_; | |
51 | |
52 JavaScriptDialogCocoa* parent_; | |
53 }; | |
54 | |
55 @interface JavaScriptDialogCocoaBridge : NSObject { | |
56 JavaScriptDialogCocoa::JavaScriptDialogCocoaImpl* dialogImpl_; // weak | |
57 } | |
58 @end | |
59 | |
60 @implementation JavaScriptDialogCocoaBridge | |
61 | |
62 - (instancetype)initWithDialogImpl: | |
63 (JavaScriptDialogCocoa::JavaScriptDialogCocoaImpl*)dialogImpl { | |
64 if ((self = [super init])) { | |
65 dialogImpl_ = dialogImpl; | |
66 } | |
67 return self; | |
68 } | |
69 | |
70 - (void)onAcceptButton:(id)sender { | |
71 dialogImpl_->Accept(); | |
72 } | |
73 | |
74 - (void)onCancelButton:(id)sender { | |
75 dialogImpl_->Cancel(); | |
76 } | |
77 | |
78 @end | |
79 | |
80 JavaScriptDialogCocoa::JavaScriptDialogCocoaImpl::JavaScriptDialogCocoaImpl( | |
81 JavaScriptDialogCocoa* parent, | |
82 content::WebContents* parent_web_contents, | |
83 content::WebContents* alerting_web_contents, | |
84 const base::string16& title, | |
85 content::JavaScriptMessageType message_type, | |
86 const base::string16& message_text, | |
87 const base::string16& default_prompt_text, | |
88 const content::JavaScriptDialogManager::DialogClosedCallback& | |
89 dialog_callback) | |
90 : dialog_callback_(dialog_callback), parent_(parent) { | |
91 bridge_.reset([[JavaScriptDialogCocoaBridge alloc] initWithDialogImpl:this]); | |
92 | |
93 alert_.reset([[ConstrainedWindowAlert alloc] init]); | |
94 [alert_ setMessageText:base::SysUTF16ToNSString(title)]; | |
95 [alert_ setInformativeText:base::SysUTF16ToNSString(message_text)]; | |
96 [alert_ addButtonWithTitle:l10n_util::GetNSStringWithFixup(IDS_APP_OK) | |
97 keyEquivalent:kKeyEquivalentReturn | |
98 target:bridge_ | |
99 action:@selector(onAcceptButton:)]; | |
100 if (message_type != content::JAVASCRIPT_MESSAGE_TYPE_ALERT) { | |
101 [alert_ addButtonWithTitle:l10n_util::GetNSStringWithFixup(IDS_APP_CANCEL) | |
102 keyEquivalent:kKeyEquivalentEscape | |
103 target:bridge_ | |
104 action:@selector(onCancelButton:)]; | |
105 } | |
106 if (message_type == content::JAVASCRIPT_MESSAGE_TYPE_PROMPT) { | |
107 textField_.reset( | |
108 [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 460, 22)]); | |
Robert Sesek
2016/10/27 15:17:24
Where do these values come from?
Avi (use Gerrit)
2016/10/27 18:41:22
The height, 22, is the standard height of an NSTex
| |
109 [[textField_ cell] setLineBreakMode:NSLineBreakByTruncatingTail]; | |
110 [textField_ setStringValue:base::SysUTF16ToNSString(default_prompt_text)]; | |
111 [alert_ setAccessoryView:textField_]; | |
112 } | |
113 [[alert_ closeButton] removeFromSuperview]; | |
114 [alert_ layout]; | |
115 | |
116 base::scoped_nsobject<CustomConstrainedWindowSheet> sheet( | |
117 [[CustomConstrainedWindowSheet alloc] | |
118 initWithCustomWindow:[alert_ window]]); | |
119 window_ = CreateAndShowWebModalDialogMac(this, parent_web_contents, sheet); | |
120 } | |
121 | |
122 void JavaScriptDialogCocoa::JavaScriptDialogCocoaImpl:: | |
123 OnConstrainedWindowClosed(ConstrainedWindowMac* window) { | |
124 if (dialog_callback_) | |
125 dialog_callback_.Run(false, base::string16()); | |
126 delete parent_; | |
127 // parent_ owns this object, so return immediately. | |
128 } | |
129 | |
130 void JavaScriptDialogCocoa::JavaScriptDialogCocoaImpl::Cancel() { | |
131 if (dialog_callback_) { | |
132 dialog_callback_.Run(false, base::string16()); | |
133 dialog_callback_.Reset(); | |
134 } | |
135 window_->CloseWebContentsModalDialog(); | |
136 } | |
137 | |
138 void JavaScriptDialogCocoa::JavaScriptDialogCocoaImpl::Accept() { | |
139 if (dialog_callback_) { | |
140 base::string16 input; | |
141 if (textField_) | |
142 input = base::SysNSStringToUTF16([textField_ stringValue]); | |
143 dialog_callback_.Run(true, input); | |
144 dialog_callback_.Reset(); | |
145 } | |
146 window_->CloseWebContentsModalDialog(); | |
147 } | |
148 | |
149 JavaScriptDialogCocoa::~JavaScriptDialogCocoa() = default; | |
150 | |
151 // static | |
152 base::WeakPtr<JavaScriptDialogCocoa> JavaScriptDialogCocoa::Create( | |
153 content::WebContents* parent_web_contents, | |
154 content::WebContents* alerting_web_contents, | |
155 const base::string16& title, | |
156 content::JavaScriptMessageType message_type, | |
157 const base::string16& message_text, | |
158 const base::string16& default_prompt_text, | |
159 const content::JavaScriptDialogManager::DialogClosedCallback& | |
160 dialog_callback) { | |
161 return (new JavaScriptDialogCocoa(parent_web_contents, alerting_web_contents, | |
162 title, message_type, message_text, | |
163 default_prompt_text, dialog_callback)) | |
164 ->weak_factory_.GetWeakPtr(); | |
165 } | |
166 | |
167 void JavaScriptDialogCocoa::CloseDialogWithoutCallback() { | |
168 impl_->dialog_callback_.Reset(); | |
169 impl_->window_->CloseWebContentsModalDialog(); | |
170 } | |
171 | |
172 JavaScriptDialogCocoa::JavaScriptDialogCocoa( | |
173 content::WebContents* parent_web_contents, | |
174 content::WebContents* alerting_web_contents, | |
175 const base::string16& title, | |
176 content::JavaScriptMessageType message_type, | |
177 const base::string16& message_text, | |
178 const base::string16& default_prompt_text, | |
179 const content::JavaScriptDialogManager::DialogClosedCallback& | |
180 dialog_callback) | |
181 : impl_(base::MakeUnique<JavaScriptDialogCocoaImpl>(this, | |
182 parent_web_contents, | |
183 alerting_web_contents, | |
184 title, | |
185 message_type, | |
186 message_text, | |
187 default_prompt_text, | |
188 dialog_callback)), | |
189 weak_factory_(this) {} | |
OLD | NEW |