| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/cocoa/js_modal_dialog_cocoa.h" | 5 #include "chrome/browser/ui/cocoa/js_modal_dialog_cocoa.h" |
| 6 | 6 |
| 7 #import <Cocoa/Cocoa.h> | 7 #import <Cocoa/Cocoa.h> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #import "base/mac/cocoa_protocols.h" | 10 #import "base/mac/cocoa_protocols.h" |
| 11 #include "base/sys_string_conversions.h" | 11 #include "base/sys_string_conversions.h" |
| 12 #import "chrome/browser/chrome_browser_application_mac.h" | 12 #import "chrome/browser/chrome_browser_application_mac.h" |
| 13 #include "chrome/browser/ui/app_modal_dialogs/js_modal_dialog.h" | 13 #include "chrome/browser/ui/app_modal_dialogs/js_modal_dialog.h" |
| 14 #include "grit/generated_resources.h" | 14 #include "grit/generated_resources.h" |
| 15 #include "grit/ui_strings.h" | 15 #include "grit/ui_strings.h" |
| 16 #include "ui/base/l10n/l10n_util_mac.h" | 16 #include "ui/base/l10n/l10n_util_mac.h" |
| 17 #include "ui/base/ui_base_types.h" | 17 #include "ui/base/ui_base_types.h" |
| 18 | 18 |
| 19 // Helper object that receives the notification that the dialog/sheet is | 19 // Helper object that receives the notification that the dialog/sheet is |
| 20 // going away. Is responsible for cleaning itself up. | 20 // going away. Is responsible for cleaning itself up. |
| 21 @interface JavaScriptAppModalDialogHelper : NSObject<NSAlertDelegate> { | 21 @interface JavaScriptAppModalDialogHelper : NSObject<NSAlertDelegate> { |
| 22 @private | 22 @private |
| 23 NSAlert* alert_; | 23 scoped_nsobject<NSAlert> alert_; |
| 24 NSTextField* textField_; // WEAK; owned by alert_ | 24 NSTextField* textField_; // WEAK; owned by alert_ |
| 25 } | 25 } |
| 26 | 26 |
| 27 - (NSAlert*)alert; | 27 - (NSAlert*)alert; |
| 28 - (NSTextField*)textField; | 28 - (NSTextField*)textField; |
| 29 - (void)alertDidEnd:(NSAlert *)alert | 29 - (void)alertDidEnd:(NSAlert*)alert |
| 30 returnCode:(int)returnCode | 30 returnCode:(int)returnCode |
| 31 contextInfo:(void*)contextInfo; | 31 contextInfo:(void*)contextInfo; |
| 32 | 32 |
| 33 @end | 33 @end |
| 34 | 34 |
| 35 @implementation JavaScriptAppModalDialogHelper | 35 @implementation JavaScriptAppModalDialogHelper |
| 36 | 36 |
| 37 - (NSAlert*)alert { | 37 - (NSAlert*)alert { |
| 38 alert_ = [[NSAlert alloc] init]; | 38 alert_.reset([[NSAlert alloc] init]); |
| 39 return alert_; | 39 return alert_; |
| 40 } | 40 } |
| 41 | 41 |
| 42 - (NSTextField*)textField { | 42 - (NSTextField*)textField { |
| 43 textField_ = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 300, 22)]; | 43 textField_ = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 300, 22)]; |
| 44 [[textField_ cell] setLineBreakMode:NSLineBreakByTruncatingTail]; | 44 [[textField_ cell] setLineBreakMode:NSLineBreakByTruncatingTail]; |
| 45 [alert_ setAccessoryView:textField_]; | 45 [alert_ setAccessoryView:textField_]; |
| 46 [textField_ release]; | 46 [textField_ release]; |
| 47 | 47 |
| 48 return textField_; | 48 return textField_; |
| 49 } | 49 } |
| 50 | 50 |
| 51 - (void)dealloc { | |
| 52 [alert_ release]; | |
| 53 [super dealloc]; | |
| 54 } | |
| 55 | |
| 56 // |contextInfo| is the JSModalDialogCocoa that owns us. | 51 // |contextInfo| is the JSModalDialogCocoa that owns us. |
| 57 - (void)alertDidEnd:(NSAlert*)alert | 52 - (void)alertDidEnd:(NSAlert*)alert |
| 58 returnCode:(int)returnCode | 53 returnCode:(int)returnCode |
| 59 contextInfo:(void*)contextInfo { | 54 contextInfo:(void*)contextInfo { |
| 60 scoped_ptr<JSModalDialogCocoa> native_dialog( | 55 scoped_ptr<JSModalDialogCocoa> native_dialog( |
| 61 reinterpret_cast<JSModalDialogCocoa*>(contextInfo)); | 56 reinterpret_cast<JSModalDialogCocoa*>(contextInfo)); |
| 62 string16 input; | 57 string16 input; |
| 63 if (textField_) | 58 if (textField_) |
| 64 input = base::SysNSStringToUTF16([textField_ stringValue]); | 59 input = base::SysNSStringToUTF16([textField_ stringValue]); |
| 65 bool shouldSuppress = false; | 60 bool shouldSuppress = false; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 83 // to be done. It won't call back to the javascript since the | 78 // to be done. It won't call back to the javascript since the |
| 84 // JavaScriptAppModalDialog knows that the TabContents was destroyed. | 79 // JavaScriptAppModalDialog knows that the TabContents was destroyed. |
| 85 native_dialog->dialog()->OnCancel(shouldSuppress); | 80 native_dialog->dialog()->OnCancel(shouldSuppress); |
| 86 break; | 81 break; |
| 87 } | 82 } |
| 88 default: { | 83 default: { |
| 89 NOTREACHED(); | 84 NOTREACHED(); |
| 90 } | 85 } |
| 91 } | 86 } |
| 92 } | 87 } |
| 88 |
| 93 @end | 89 @end |
| 94 | 90 |
| 95 //////////////////////////////////////////////////////////////////////////////// | 91 //////////////////////////////////////////////////////////////////////////////// |
| 96 // JSModalDialogCocoa, public: | 92 // JSModalDialogCocoa, public: |
| 97 | 93 |
| 98 JSModalDialogCocoa::JSModalDialogCocoa(JavaScriptAppModalDialog* dialog) | 94 JSModalDialogCocoa::JSModalDialogCocoa(JavaScriptAppModalDialog* dialog) |
| 99 : dialog_(dialog), | 95 : dialog_(dialog), |
| 100 helper_(NULL) { | 96 helper_(NULL) { |
| 101 // Determine the names of the dialog buttons based on the flags. "Default" | 97 // Determine the names of the dialog buttons based on the flags. "Default" |
| 102 // is the OK button. "Other" is the cancel button. We don't use the | 98 // is the OK button. "Other" is the cancel button. We don't use the |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 217 | 213 |
| 218 //////////////////////////////////////////////////////////////////////////////// | 214 //////////////////////////////////////////////////////////////////////////////// |
| 219 // NativeAppModalDialog, public: | 215 // NativeAppModalDialog, public: |
| 220 | 216 |
| 221 // static | 217 // static |
| 222 NativeAppModalDialog* NativeAppModalDialog::CreateNativeJavaScriptPrompt( | 218 NativeAppModalDialog* NativeAppModalDialog::CreateNativeJavaScriptPrompt( |
| 223 JavaScriptAppModalDialog* dialog, | 219 JavaScriptAppModalDialog* dialog, |
| 224 gfx::NativeWindow parent_window) { | 220 gfx::NativeWindow parent_window) { |
| 225 return new JSModalDialogCocoa(dialog); | 221 return new JSModalDialogCocoa(dialog); |
| 226 } | 222 } |
| OLD | NEW |