OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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/js_modal_dialog.h" |
| 6 |
| 7 #import <Cocoa/Cocoa.h> |
| 8 |
| 9 #include "app/l10n_util_mac.h" |
| 10 #include "app/message_box_flags.h" |
| 11 #import "base/cocoa_protocols_mac.h" |
| 12 #include "base/sys_string_conversions.h" |
| 13 #include "grit/app_strings.h" |
| 14 #include "grit/generated_resources.h" |
| 15 |
| 16 // Helper object that receives the notification that the dialog/sheet is |
| 17 // going away. Is responsible for cleaning itself up. |
| 18 @interface JavaScriptAppModalDialogHelper : NSObject<NSAlertDelegate> { |
| 19 @private |
| 20 NSAlert* alert_; |
| 21 NSTextField* textField_; // WEAK; owned by alert_ |
| 22 } |
| 23 |
| 24 - (NSAlert*)alert; |
| 25 - (NSTextField*)textField; |
| 26 - (void)alertDidEnd:(NSAlert *)alert |
| 27 returnCode:(int)returnCode |
| 28 contextInfo:(void*)contextInfo; |
| 29 |
| 30 @end |
| 31 |
| 32 @implementation JavaScriptAppModalDialogHelper |
| 33 |
| 34 - (NSAlert*)alert { |
| 35 alert_ = [[NSAlert alloc] init]; |
| 36 return alert_; |
| 37 } |
| 38 |
| 39 - (NSTextField*)textField { |
| 40 textField_ = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 300, 22)]; |
| 41 [alert_ setAccessoryView:textField_]; |
| 42 [textField_ release]; |
| 43 |
| 44 return textField_; |
| 45 } |
| 46 |
| 47 - (void)dealloc { |
| 48 [alert_ release]; |
| 49 [super dealloc]; |
| 50 } |
| 51 |
| 52 // |contextInfo| is the bridge back to the C++ JavaScriptAppModalDialog. When |
| 53 // complete, autorelease to clean ourselves up. |
| 54 - (void)alertDidEnd:(NSAlert*)alert |
| 55 returnCode:(int)returnCode |
| 56 contextInfo:(void*)contextInfo { |
| 57 JavaScriptAppModalDialog* bridge = |
| 58 reinterpret_cast<JavaScriptAppModalDialog*>(contextInfo); |
| 59 std::wstring input; |
| 60 if (textField_) |
| 61 input = base::SysNSStringToWide([textField_ stringValue]); |
| 62 switch (returnCode) { |
| 63 case NSAlertFirstButtonReturn: { // OK |
| 64 bool shouldSuppress = false; |
| 65 if ([alert showsSuppressionButton]) |
| 66 shouldSuppress = [[alert suppressionButton] state] == NSOnState; |
| 67 bridge->OnAccept(input, shouldSuppress); |
| 68 break; |
| 69 } |
| 70 case NSAlertSecondButtonReturn: { // Cancel |
| 71 bridge->OnCancel(); |
| 72 break; |
| 73 } |
| 74 case NSRunStoppedResponse: { // Window was closed underneath us |
| 75 // Need to call OnCancel() because there is some cleanup that needs |
| 76 // to be done. It won't call back to the javascript since the |
| 77 // JavaScriptAppModalDialog knows that the TabContents was destroyed. |
| 78 bridge->OnCancel(); |
| 79 break; |
| 80 } |
| 81 default: { |
| 82 NOTREACHED(); |
| 83 } |
| 84 } |
| 85 [self autorelease]; |
| 86 delete bridge; // Done with the dialog, it needs be destroyed. |
| 87 } |
| 88 @end |
| 89 |
| 90 void JavaScriptAppModalDialog::CreateAndShowDialog() { |
| 91 // Determine the names of the dialog buttons based on the flags. "Default" |
| 92 // is the OK button. "Other" is the cancel button. We don't use the |
| 93 // "Alternate" button in NSRunAlertPanel. |
| 94 NSString* default_button = l10n_util::GetNSStringWithFixup(IDS_APP_OK); |
| 95 NSString* other_button = l10n_util::GetNSStringWithFixup(IDS_APP_CANCEL); |
| 96 bool text_field = false; |
| 97 bool one_button = false; |
| 98 switch (dialog_flags_) { |
| 99 case MessageBoxFlags::kIsJavascriptAlert: |
| 100 one_button = true; |
| 101 break; |
| 102 case MessageBoxFlags::kIsJavascriptConfirm: |
| 103 if (is_before_unload_dialog_) { |
| 104 default_button = l10n_util::GetNSStringWithFixup( |
| 105 IDS_BEFOREUNLOAD_MESSAGEBOX_OK_BUTTON_LABEL); |
| 106 other_button = l10n_util::GetNSStringWithFixup( |
| 107 IDS_BEFOREUNLOAD_MESSAGEBOX_CANCEL_BUTTON_LABEL); |
| 108 } |
| 109 break; |
| 110 case MessageBoxFlags::kIsJavascriptPrompt: |
| 111 text_field = true; |
| 112 break; |
| 113 |
| 114 default: |
| 115 NOTREACHED(); |
| 116 } |
| 117 |
| 118 // Create a helper which will receive the sheet ended selector. It will |
| 119 // delete itself when done. It doesn't need anything passed to its init |
| 120 // as it will get a contextInfo parameter. |
| 121 JavaScriptAppModalDialogHelper* helper = |
| 122 [[JavaScriptAppModalDialogHelper alloc] init]; |
| 123 |
| 124 // Show the modal dialog. |
| 125 NSAlert* alert = [helper alert]; |
| 126 dialog_ = alert; |
| 127 NSTextField* field = nil; |
| 128 if (text_field) { |
| 129 field = [helper textField]; |
| 130 [field setStringValue:base::SysWideToNSString(default_prompt_text_)]; |
| 131 } |
| 132 [alert setDelegate:helper]; |
| 133 [alert setInformativeText:base::SysWideToNSString(message_text_)]; |
| 134 [alert setMessageText:base::SysWideToNSString(title_)]; |
| 135 [alert addButtonWithTitle:default_button]; |
| 136 if (!one_button) |
| 137 [alert addButtonWithTitle:other_button]; |
| 138 if (display_suppress_checkbox_) { |
| 139 [alert setShowsSuppressionButton:YES]; |
| 140 NSString* suppression_title = l10n_util::GetNSStringWithFixup( |
| 141 IDS_JAVASCRIPT_MESSAGEBOX_SUPPRESS_OPTION); |
| 142 [[alert suppressionButton] setTitle:suppression_title]; |
| 143 } |
| 144 |
| 145 [alert beginSheetModalForWindow:nil // nil here makes it app-modal |
| 146 modalDelegate:helper |
| 147 didEndSelector:@selector(alertDidEnd:returnCode:contextInfo:) |
| 148 contextInfo:this]; |
| 149 |
| 150 if (field) |
| 151 [[alert window] makeFirstResponder:field]; |
| 152 } |
| 153 |
| 154 int JavaScriptAppModalDialog::GetDialogButtons() { |
| 155 NOTIMPLEMENTED(); |
| 156 return 0; |
| 157 } |
| 158 |
| 159 void JavaScriptAppModalDialog::AcceptWindow() { |
| 160 NOTIMPLEMENTED(); |
| 161 } |
| 162 |
| 163 void JavaScriptAppModalDialog::CancelWindow() { |
| 164 NOTIMPLEMENTED(); |
| 165 } |
| 166 |
| 167 NativeDialog JavaScriptAppModalDialog::CreateNativeDialog() { |
| 168 NOTIMPLEMENTED(); |
| 169 return nil; |
| 170 } |
OLD | NEW |