1 // Copyright (c) 2012 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/cocoa/javascript_app_modal_dialog_cocoa.h"
7 #import <Cocoa/Cocoa.h>
9 #include "base/i18n/rtl.h"
10 #include "base/logging.h"
11 #import "base/mac/foundation_util.h"
12 #include "base/strings/sys_string_conversions.h"
13 #import "chrome/browser/chrome_browser_application_mac.h"
14 #include "chrome/browser/ui/app_modal_dialogs/javascript_app_modal_dialog.h"
15 #include "chrome/grit/generated_resources.h"
16 #include "ui/base/l10n/l10n_util_mac.h"
17 #include "ui/base/ui_base_types.h"
18 #include "ui/gfx/text_elider.h"
19 #include "ui/strings/grit/ui_strings.h"
23 const int kSlotsPerLine = 50;
24 const int kMessageTextMaxSlots = 2000;
26 // The presentation of the NSAlert is delayed, due to an AppKit bug. See
27 // JavaScriptAppModalDialogCocoa::ShowAppModalDialog for more details. If the
28 // NSAlert has not yet been presented, then actions that affect the NSAlert
29 // should be delayed as well. Due to the destructive nature of these actions,
30 // at most one action should be queued.
40 // Helper object that receives the notification that the dialog/sheet is
41 // going away. Is responsible for cleaning itself up.
42 @interface JavaScriptAppModalDialogHelper : NSObject<NSAlertDelegate> {
44 base::scoped_nsobject<NSAlert> alert_;
45 JavaScriptAppModalDialogCocoa* nativeDialog_; // Weak.
46 base::scoped_nsobject<NSTextField> textField_;
48 AlertAction queuedAction_;
51 // Creates an NSAlert if one does not already exist. Otherwise returns the
54 - (void)addTextFieldWithPrompt:(NSString*)prompt;
55 - (void)alertDidEnd:(NSAlert*)alert
56 returnCode:(int)returnCode
57 contextInfo:(void*)contextInfo;
59 // If the alert has been presented, immediately play the action. Otherwise
60 // queue the action for replay immediately after the alert is presented.
61 - (void)playOrQueueAction:(AlertAction)action;
62 - (void)queueAction:(AlertAction)action;
64 // Presents an AppKit blocking dialog.
67 // Selects the first button of the alert, which should accept it.
70 // Selects the second button of the alert, which should cancel it.
73 // Closes the window, and the alert along with it.
76 // Designated initializer.
77 - (instancetype)initWithNativeDialog:(JavaScriptAppModalDialogCocoa*)dialog;
81 @implementation JavaScriptAppModalDialogHelper
83 - (instancetype)init {
88 - (instancetype)initWithNativeDialog:(JavaScriptAppModalDialogCocoa*)dialog {
92 nativeDialog_ = dialog;
93 queuedAction_ = ACTION_NONE;
100 alert_.reset([[NSAlert alloc] init]);
104 - (void)addTextFieldWithPrompt:(NSString*)prompt {
107 [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 300, 22)]);
108 [[textField_ cell] setLineBreakMode:NSLineBreakByTruncatingTail];
109 [[self alert] setAccessoryView:textField_];
111 [textField_ setStringValue:prompt];
114 // |contextInfo| is the JavaScriptAppModalDialogCocoa that owns us.
115 - (void)alertDidEnd:(NSAlert*)alert
116 returnCode:(int)returnCode
117 contextInfo:(void*)contextInfo {
118 DCHECK(nativeDialog_);
119 base::string16 input;
121 input = base::SysNSStringToUTF16([textField_ stringValue]);
122 bool shouldSuppress = false;
123 if ([alert showsSuppressionButton])
124 shouldSuppress = [[alert suppressionButton] state] == NSOnState;
125 switch (returnCode) {
126 case NSAlertFirstButtonReturn: { // OK
127 nativeDialog_->dialog()->OnAccept(input, shouldSuppress);
130 case NSAlertSecondButtonReturn: { // Cancel
131 // If the user wants to stay on this page, stop quitting (if a quit is in
133 if (nativeDialog_->dialog()->is_before_unload_dialog())
134 chrome_browser_application_mac::CancelTerminate();
135 nativeDialog_->dialog()->OnCancel(shouldSuppress);
138 case NSRunStoppedResponse: { // Window was closed underneath us
139 // Need to call OnCancel() because there is some cleanup that needs
140 // to be done. It won't call back to the javascript since the
141 // JavaScriptAppModalDialog knows that the WebContents was destroyed.
142 nativeDialog_->dialog()->OnCancel(shouldSuppress);
151 - (void)playOrQueueAction:(AlertAction)action {
153 [self playAlertAction:action];
155 [self queueAction:action];
158 - (void)queueAction:(AlertAction)action {
159 DCHECK(!alertShown_);
160 DCHECK(queuedAction_ == ACTION_NONE);
162 queuedAction_ = action;
165 - (void)playAlertAction:(AlertAction)action {
183 NSAlert* alert = [self alert];
184 [alert beginSheetModalForWindow:nil // nil here makes it app-modal
186 didEndSelector:@selector(alertDidEnd:returnCode:contextInfo:)
189 if ([alert accessoryView])
190 [[alert window] makeFirstResponder:[alert accessoryView]];
192 [self playAlertAction:queuedAction_];
195 - (void)acceptAlert {
196 NSButton* first = [[[self alert] buttons] objectAtIndex:0];
197 [first performClick:nil];
200 - (void)cancelAlert {
201 NSAlert* alert = [self alert];
202 DCHECK([[alert buttons] count] >= 2);
203 NSButton* second = [[alert buttons] objectAtIndex:1];
204 [second performClick:nil];
207 - (void)closeWindow {
208 DCHECK([self alert]);
210 [NSApp endSheet:[[self alert] window]];
215 ////////////////////////////////////////////////////////////////////////////////
216 // JavaScriptAppModalDialogCocoa, public:
218 JavaScriptAppModalDialogCocoa::JavaScriptAppModalDialogCocoa(
219 JavaScriptAppModalDialog* dialog)
222 // Determine the names of the dialog buttons based on the flags. "Default"
223 // is the OK button. "Other" is the cancel button. We don't use the
224 // "Alternate" button in NSRunAlertPanel.
225 NSString* default_button = l10n_util::GetNSStringWithFixup(IDS_APP_OK);
226 NSString* other_button = l10n_util::GetNSStringWithFixup(IDS_APP_CANCEL);
227 bool text_field = false;
228 bool one_button = false;
229 switch (dialog_->javascript_message_type()) {
230 case content::JAVASCRIPT_MESSAGE_TYPE_ALERT:
233 case content::JAVASCRIPT_MESSAGE_TYPE_CONFIRM:
234 if (dialog_->is_before_unload_dialog()) {
235 if (dialog_->is_reload()) {
236 default_button = l10n_util::GetNSStringWithFixup(
237 IDS_BEFORERELOAD_MESSAGEBOX_OK_BUTTON_LABEL);
238 other_button = l10n_util::GetNSStringWithFixup(
239 IDS_BEFORERELOAD_MESSAGEBOX_CANCEL_BUTTON_LABEL);
241 default_button = l10n_util::GetNSStringWithFixup(
242 IDS_BEFOREUNLOAD_MESSAGEBOX_OK_BUTTON_LABEL);
243 other_button = l10n_util::GetNSStringWithFixup(
244 IDS_BEFOREUNLOAD_MESSAGEBOX_CANCEL_BUTTON_LABEL);
248 case content::JAVASCRIPT_MESSAGE_TYPE_PROMPT:
256 // Create a helper which will receive the sheet ended selector. It will
257 // delete itself when done.
259 [[JavaScriptAppModalDialogHelper alloc] initWithNativeDialog:this]);
261 // Show the modal dialog.
263 [helper_ addTextFieldWithPrompt:base::SysUTF16ToNSString(
264 dialog_->default_prompt_text())];
266 [GetAlert() setDelegate:helper_];
267 NSString* informative_text =
268 base::SysUTF16ToNSString(dialog_->message_text());
270 // Truncate long JS alerts - crbug.com/331219
271 NSCharacterSet* newline_char_set = [NSCharacterSet newlineCharacterSet];
272 for (size_t index = 0, slots_count = 0; index < informative_text.length;
274 unichar current_char = [informative_text characterAtIndex:index];
275 if ([newline_char_set characterIsMember:current_char])
276 slots_count += kSlotsPerLine;
279 if (slots_count > kMessageTextMaxSlots) {
280 base::string16 info_text = base::SysNSStringToUTF16(informative_text);
281 informative_text = base::SysUTF16ToNSString(
282 gfx::TruncateString(info_text, index, gfx::WORD_BREAK));
287 [GetAlert() setInformativeText:informative_text];
288 NSString* message_text =
289 base::SysUTF16ToNSString(dialog_->title());
290 [GetAlert() setMessageText:message_text];
291 [GetAlert() addButtonWithTitle:default_button];
293 NSButton* other = [GetAlert() addButtonWithTitle:other_button];
294 [other setKeyEquivalent:@"\e"];
296 if (dialog_->display_suppress_checkbox()) {
297 [GetAlert() setShowsSuppressionButton:YES];
298 NSString* suppression_title = l10n_util::GetNSStringWithFixup(
299 IDS_JAVASCRIPT_MESSAGEBOX_SUPPRESS_OPTION);
300 [[GetAlert() suppressionButton] setTitle:suppression_title];
305 // Mac OS X will always display NSAlert strings as LTR. A workaround is to
306 // manually set the text as attributed strings in the implementing
307 // NSTextFields. This is a basic correctness issue.
309 // In addition, for readability, the overall alignment is set based on the
310 // directionality of the first strongly-directional character.
312 // If the dialog fields are selectable then they will scramble when clicked.
313 // Therefore, selectability is disabled.
315 // See http://crbug.com/70806 for more details.
317 bool message_has_rtl =
318 base::i18n::StringContainsStrongRTLChars(dialog_->title());
319 bool informative_has_rtl =
320 base::i18n::StringContainsStrongRTLChars(dialog_->message_text());
322 NSTextField* message_text_field = nil;
323 NSTextField* informative_text_field = nil;
324 if (message_has_rtl || informative_has_rtl) {
325 // Force layout of the dialog. NSAlert leaves its dialog alone once laid
326 // out; if this is not done then all the modifications that are to come will
327 // be un-done when the dialog is finally displayed.
330 // Locate the NSTextFields that implement the text display. These are
331 // actually available as the ivars |_messageField| and |_informationField|
332 // of the NSAlert, but it is safer (and more forward-compatible) to search
333 // for them in the subviews.
334 for (NSView* view in [[[GetAlert() window] contentView] subviews]) {
335 NSTextField* text_field = base::mac::ObjCCast<NSTextField>(view);
336 if ([[text_field stringValue] isEqualTo:message_text])
337 message_text_field = text_field;
338 else if ([[text_field stringValue] isEqualTo:informative_text])
339 informative_text_field = text_field;
342 // This may fail in future OS releases, but it will still work for shipped
343 // versions of Chromium.
344 DCHECK(message_text_field);
345 DCHECK(informative_text_field);
348 if (message_has_rtl && message_text_field) {
349 base::scoped_nsobject<NSMutableParagraphStyle> alignment(
350 [[NSParagraphStyle defaultParagraphStyle] mutableCopy]);
351 [alignment setAlignment:NSRightTextAlignment];
353 NSDictionary* alignment_attributes =
354 @{ NSParagraphStyleAttributeName : alignment };
355 base::scoped_nsobject<NSAttributedString> attr_string(
356 [[NSAttributedString alloc] initWithString:message_text
357 attributes:alignment_attributes]);
359 [message_text_field setAttributedStringValue:attr_string];
360 [message_text_field setSelectable:NO];
363 if (informative_has_rtl && informative_text_field) {
364 base::i18n::TextDirection direction =
365 base::i18n::GetFirstStrongCharacterDirection(dialog_->message_text());
366 base::scoped_nsobject<NSMutableParagraphStyle> alignment(
367 [[NSParagraphStyle defaultParagraphStyle] mutableCopy]);
368 [alignment setAlignment:
369 (direction == base::i18n::RIGHT_TO_LEFT) ? NSRightTextAlignment
370 : NSLeftTextAlignment];
372 NSDictionary* alignment_attributes =
373 @{ NSParagraphStyleAttributeName : alignment };
374 base::scoped_nsobject<NSAttributedString> attr_string(
375 [[NSAttributedString alloc] initWithString:informative_text
376 attributes:alignment_attributes]);
378 [informative_text_field setAttributedStringValue:attr_string];
379 [informative_text_field setSelectable:NO];
383 JavaScriptAppModalDialogCocoa::~JavaScriptAppModalDialogCocoa() {
384 [NSObject cancelPreviousPerformRequestsWithTarget:helper_.get()];
387 ////////////////////////////////////////////////////////////////////////////////
388 // JavaScriptAppModalDialogCocoa, private:
390 NSAlert* JavaScriptAppModalDialogCocoa::GetAlert() const {
391 return [helper_ alert];
394 ////////////////////////////////////////////////////////////////////////////////
395 // JavaScriptAppModalDialogCocoa, NativeAppModalDialog implementation:
397 int JavaScriptAppModalDialogCocoa::GetAppModalDialogButtons() const {
398 // From the above, it is the case that if there is 1 button, it is always the
399 // OK button. The second button, if it exists, is always the Cancel button.
400 int num_buttons = [[GetAlert() buttons] count];
401 switch (num_buttons) {
403 return ui::DIALOG_BUTTON_OK;
405 return ui::DIALOG_BUTTON_OK | ui::DIALOG_BUTTON_CANCEL;
412 void JavaScriptAppModalDialogCocoa::ShowAppModalDialog() {
413 // Dispatch the method to show the alert back to the top of the CFRunLoop.
414 // This fixes an interaction bug with NSSavePanel. http://crbug.com/375785
415 // When this object is destroyed, outstanding performSelector: requests
416 // should be cancelled.
417 [helper_.get() performSelector:@selector(showAlert)
422 void JavaScriptAppModalDialogCocoa::ActivateAppModalDialog() {
425 void JavaScriptAppModalDialogCocoa::CloseAppModalDialog() {
426 [helper_ playOrQueueAction:ACTION_CLOSE];
429 void JavaScriptAppModalDialogCocoa::AcceptAppModalDialog() {
430 [helper_ playOrQueueAction:ACTION_ACCEPT];
433 void JavaScriptAppModalDialogCocoa::CancelAppModalDialog() {
434 [helper_ playOrQueueAction:ACTION_CANCEL];
437 ////////////////////////////////////////////////////////////////////////////////
438 // NativeAppModalDialog, public:
441 NativeAppModalDialog* NativeAppModalDialog::CreateNativeJavaScriptPrompt(
442 JavaScriptAppModalDialog* dialog,
443 gfx::NativeWindow parent_window) {
444 return new JavaScriptAppModalDialogCocoa(dialog);