| 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 "content/shell/shell_javascript_dialog.h" | 5 #include "content/shell/shell_javascript_dialog.h" |
| 6 | 6 |
| 7 #import <Cocoa/Cocoa.h> | 7 #import <Cocoa/Cocoa.h> |
| 8 | 8 |
| 9 #import "base/memory/scoped_nsobject.h" | 9 #import "base/memory/scoped_nsobject.h" |
| 10 #include "base/sys_string_conversions.h" | 10 #include "base/sys_string_conversions.h" |
| 11 #include "content/shell/shell_javascript_dialog_creator.h" | 11 #include "content/shell/shell_javascript_dialog_manager.h" |
| 12 | 12 |
| 13 // Helper object that receives the notification that the dialog/sheet is | 13 // Helper object that receives the notification that the dialog/sheet is |
| 14 // going away. Is responsible for cleaning itself up. | 14 // going away. Is responsible for cleaning itself up. |
| 15 @interface ShellJavaScriptDialogHelper : NSObject<NSAlertDelegate> { | 15 @interface ShellJavaScriptDialogHelper : NSObject<NSAlertDelegate> { |
| 16 @private | 16 @private |
| 17 scoped_nsobject<NSAlert> alert_; | 17 scoped_nsobject<NSAlert> alert_; |
| 18 NSTextField* textField_; // WEAK; owned by alert_ | 18 NSTextField* textField_; // WEAK; owned by alert_ |
| 19 | 19 |
| 20 // Copies of the fields in ShellJavaScriptDialog because they're private. | 20 // Copies of the fields in ShellJavaScriptDialog because they're private. |
| 21 content::ShellJavaScriptDialogCreator* creator_; | 21 content::ShellJavaScriptDialogManager* manager_; |
| 22 content::JavaScriptDialogCreator::DialogClosedCallback callback_; | 22 content::JavaScriptDialogManager::DialogClosedCallback callback_; |
| 23 } | 23 } |
| 24 | 24 |
| 25 - (id)initHelperWithCreator:(content::ShellJavaScriptDialogCreator*)creator | 25 - (id)initHelperWithManager:(content::ShellJavaScriptDialogManager*)manager |
| 26 andCallback:(content::JavaScriptDialogCreator::DialogClosedCallback)callback; | 26 andCallback:(content::JavaScriptDialogManager::DialogClosedCallback)callback; |
| 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 - (void)cancel; | 32 - (void)cancel; |
| 33 | 33 |
| 34 @end | 34 @end |
| 35 | 35 |
| 36 @implementation ShellJavaScriptDialogHelper | 36 @implementation ShellJavaScriptDialogHelper |
| 37 | 37 |
| 38 - (id)initHelperWithCreator:(content::ShellJavaScriptDialogCreator*)creator | 38 - (id)initHelperWithManager:(content::ShellJavaScriptDialogManager*)manager |
| 39 andCallback:(content::JavaScriptDialogCreator::DialogClosedCallback)callback { | 39 andCallback:(content::JavaScriptDialogManager::DialogClosedCallback)callback { |
| 40 if (self = [super init]) { | 40 if (self = [super init]) { |
| 41 creator_ = creator; | 41 manager_ = manager; |
| 42 callback_ = callback; | 42 callback_ = callback; |
| 43 } | 43 } |
| 44 | 44 |
| 45 return self; | 45 return self; |
| 46 } | 46 } |
| 47 | 47 |
| 48 - (NSAlert*)alert { | 48 - (NSAlert*)alert { |
| 49 alert_.reset([[NSAlert alloc] init]); | 49 alert_.reset([[NSAlert alloc] init]); |
| 50 return alert_; | 50 return alert_; |
| 51 } | 51 } |
| (...skipping 14 matching lines...) Expand all Loading... |
| 66 return; | 66 return; |
| 67 | 67 |
| 68 bool success = returnCode == NSAlertFirstButtonReturn; | 68 bool success = returnCode == NSAlertFirstButtonReturn; |
| 69 string16 input; | 69 string16 input; |
| 70 if (textField_) | 70 if (textField_) |
| 71 input = base::SysNSStringToUTF16([textField_ stringValue]); | 71 input = base::SysNSStringToUTF16([textField_ stringValue]); |
| 72 | 72 |
| 73 content::ShellJavaScriptDialog* native_dialog = | 73 content::ShellJavaScriptDialog* native_dialog = |
| 74 reinterpret_cast<content::ShellJavaScriptDialog*>(contextInfo); | 74 reinterpret_cast<content::ShellJavaScriptDialog*>(contextInfo); |
| 75 callback_.Run(success, input); | 75 callback_.Run(success, input); |
| 76 creator_->DialogClosed(native_dialog); | 76 manager_->DialogClosed(native_dialog); |
| 77 } | 77 } |
| 78 | 78 |
| 79 - (void)cancel { | 79 - (void)cancel { |
| 80 [NSApp endSheet:[alert_ window]]; | 80 [NSApp endSheet:[alert_ window]]; |
| 81 alert_.reset(); | 81 alert_.reset(); |
| 82 } | 82 } |
| 83 | 83 |
| 84 @end | 84 @end |
| 85 | 85 |
| 86 namespace content { | 86 namespace content { |
| 87 | 87 |
| 88 ShellJavaScriptDialog::ShellJavaScriptDialog( | 88 ShellJavaScriptDialog::ShellJavaScriptDialog( |
| 89 ShellJavaScriptDialogCreator* creator, | 89 ShellJavaScriptDialogManager* manager, |
| 90 gfx::NativeWindow parent_window, | 90 gfx::NativeWindow parent_window, |
| 91 JavaScriptMessageType message_type, | 91 JavaScriptMessageType message_type, |
| 92 const string16& message_text, | 92 const string16& message_text, |
| 93 const string16& default_prompt_text, | 93 const string16& default_prompt_text, |
| 94 const JavaScriptDialogCreator::DialogClosedCallback& callback) | 94 const JavaScriptDialogManager::DialogClosedCallback& callback) |
| 95 : creator_(creator), | 95 : manager_(manager), |
| 96 callback_(callback) { | 96 callback_(callback) { |
| 97 bool text_field = message_type == JAVASCRIPT_MESSAGE_TYPE_PROMPT; | 97 bool text_field = message_type == JAVASCRIPT_MESSAGE_TYPE_PROMPT; |
| 98 bool one_button = message_type == JAVASCRIPT_MESSAGE_TYPE_ALERT; | 98 bool one_button = message_type == JAVASCRIPT_MESSAGE_TYPE_ALERT; |
| 99 | 99 |
| 100 helper_ = | 100 helper_ = |
| 101 [[ShellJavaScriptDialogHelper alloc] initHelperWithCreator:creator | 101 [[ShellJavaScriptDialogHelper alloc] initHelperWithManager:manager |
| 102 andCallback:callback]; | 102 andCallback:callback]; |
| 103 | 103 |
| 104 // Show the modal dialog. | 104 // Show the modal dialog. |
| 105 NSAlert* alert = [helper_ alert]; | 105 NSAlert* alert = [helper_ alert]; |
| 106 NSTextField* field = nil; | 106 NSTextField* field = nil; |
| 107 if (text_field) { | 107 if (text_field) { |
| 108 field = [helper_ textField]; | 108 field = [helper_ textField]; |
| 109 [field setStringValue:base::SysUTF16ToNSString(default_prompt_text)]; | 109 [field setStringValue:base::SysUTF16ToNSString(default_prompt_text)]; |
| 110 } | 110 } |
| 111 [alert setDelegate:helper_]; | 111 [alert setDelegate:helper_]; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 129 | 129 |
| 130 ShellJavaScriptDialog::~ShellJavaScriptDialog() { | 130 ShellJavaScriptDialog::~ShellJavaScriptDialog() { |
| 131 [helper_ release]; | 131 [helper_ release]; |
| 132 } | 132 } |
| 133 | 133 |
| 134 void ShellJavaScriptDialog::Cancel() { | 134 void ShellJavaScriptDialog::Cancel() { |
| 135 [helper_ cancel]; | 135 [helper_ cancel]; |
| 136 } | 136 } |
| 137 | 137 |
| 138 } // namespace content | 138 } // namespace content |
| OLD | NEW |