[email protected] | de7d61ff | 2013-08-20 11:30:41 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors. All rights reserved. |
[email protected] | f221002 | 2012-03-29 00:36:08 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | de7d61ff | 2013-08-20 11:30:41 | [diff] [blame] | 5 | #include "content/shell/browser/shell_javascript_dialog.h" |
[email protected] | f221002 | 2012-03-29 00:36:08 | [diff] [blame] | 6 | |
| 7 | #import <Cocoa/Cocoa.h> |
| 8 | |
[email protected] | a852203 | 2013-06-24 22:51:46 | [diff] [blame] | 9 | #import "base/mac/scoped_nsobject.h" |
[email protected] | 40d11e0 | 2013-03-28 17:43:14 | [diff] [blame] | 10 | #include "base/strings/sys_string_conversions.h" |
[email protected] | de7d61ff | 2013-08-20 11:30:41 | [diff] [blame] | 11 | #include "content/shell/browser/shell_javascript_dialog_manager.h" |
[email protected] | f221002 | 2012-03-29 00:36:08 | [diff] [blame] | 12 | |
| 13 | // Helper object that receives the notification that the dialog/sheet is |
| 14 | // going away. Is responsible for cleaning itself up. |
| 15 | @interface ShellJavaScriptDialogHelper : NSObject<NSAlertDelegate> { |
| 16 | @private |
[email protected] | a852203 | 2013-06-24 22:51:46 | [diff] [blame] | 17 | base::scoped_nsobject<NSAlert> alert_; |
[email protected] | f221002 | 2012-03-29 00:36:08 | [diff] [blame] | 18 | NSTextField* textField_; // WEAK; owned by alert_ |
| 19 | |
| 20 | // Copies of the fields in ShellJavaScriptDialog because they're private. |
[email protected] | 71a88bb | 2013-02-01 22:05:15 | [diff] [blame] | 21 | content::ShellJavaScriptDialogManager* manager_; |
| 22 | content::JavaScriptDialogManager::DialogClosedCallback callback_; |
[email protected] | f221002 | 2012-03-29 00:36:08 | [diff] [blame] | 23 | } |
| 24 | |
[email protected] | 71a88bb | 2013-02-01 22:05:15 | [diff] [blame] | 25 | - (id)initHelperWithManager:(content::ShellJavaScriptDialogManager*)manager |
| 26 | andCallback:(content::JavaScriptDialogManager::DialogClosedCallback)callback; |
[email protected] | f221002 | 2012-03-29 00:36:08 | [diff] [blame] | 27 | - (NSAlert*)alert; |
| 28 | - (NSTextField*)textField; |
| 29 | - (void)alertDidEnd:(NSAlert*)alert |
| 30 | returnCode:(int)returnCode |
| 31 | contextInfo:(void*)contextInfo; |
| 32 | - (void)cancel; |
| 33 | |
| 34 | @end |
| 35 | |
| 36 | @implementation ShellJavaScriptDialogHelper |
| 37 | |
[email protected] | 71a88bb | 2013-02-01 22:05:15 | [diff] [blame] | 38 | - (id)initHelperWithManager:(content::ShellJavaScriptDialogManager*)manager |
| 39 | andCallback:(content::JavaScriptDialogManager::DialogClosedCallback)callback { |
[email protected] | f221002 | 2012-03-29 00:36:08 | [diff] [blame] | 40 | if (self = [super init]) { |
[email protected] | 71a88bb | 2013-02-01 22:05:15 | [diff] [blame] | 41 | manager_ = manager; |
[email protected] | f221002 | 2012-03-29 00:36:08 | [diff] [blame] | 42 | callback_ = callback; |
| 43 | } |
| 44 | |
| 45 | return self; |
| 46 | } |
| 47 | |
| 48 | - (NSAlert*)alert { |
| 49 | alert_.reset([[NSAlert alloc] init]); |
| 50 | return alert_; |
| 51 | } |
| 52 | |
| 53 | - (NSTextField*)textField { |
| 54 | textField_ = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 300, 22)]; |
| 55 | [[textField_ cell] setLineBreakMode:NSLineBreakByTruncatingTail]; |
| 56 | [alert_ setAccessoryView:textField_]; |
| 57 | [textField_ release]; |
| 58 | |
| 59 | return textField_; |
| 60 | } |
| 61 | |
| 62 | - (void)alertDidEnd:(NSAlert*)alert |
| 63 | returnCode:(int)returnCode |
| 64 | contextInfo:(void*)contextInfo { |
| 65 | if (returnCode == NSRunStoppedResponse) |
| 66 | return; |
| 67 | |
| 68 | bool success = returnCode == NSAlertFirstButtonReturn; |
[email protected] | fcf75d4 | 2013-12-03 20:11:26 | [diff] [blame^] | 69 | base::string16 input; |
[email protected] | f221002 | 2012-03-29 00:36:08 | [diff] [blame] | 70 | if (textField_) |
| 71 | input = base::SysNSStringToUTF16([textField_ stringValue]); |
| 72 | |
| 73 | content::ShellJavaScriptDialog* native_dialog = |
| 74 | reinterpret_cast<content::ShellJavaScriptDialog*>(contextInfo); |
| 75 | callback_.Run(success, input); |
[email protected] | 71a88bb | 2013-02-01 22:05:15 | [diff] [blame] | 76 | manager_->DialogClosed(native_dialog); |
[email protected] | f221002 | 2012-03-29 00:36:08 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | - (void)cancel { |
| 80 | [NSApp endSheet:[alert_ window]]; |
| 81 | alert_.reset(); |
| 82 | } |
| 83 | |
| 84 | @end |
| 85 | |
| 86 | namespace content { |
| 87 | |
| 88 | ShellJavaScriptDialog::ShellJavaScriptDialog( |
[email protected] | 71a88bb | 2013-02-01 22:05:15 | [diff] [blame] | 89 | ShellJavaScriptDialogManager* manager, |
[email protected] | fc4f4dd4 | 2012-07-30 20:52:48 | [diff] [blame] | 90 | gfx::NativeWindow parent_window, |
[email protected] | be2510c0 | 2012-05-28 14:52:14 | [diff] [blame] | 91 | JavaScriptMessageType message_type, |
[email protected] | fcf75d4 | 2013-12-03 20:11:26 | [diff] [blame^] | 92 | const base::string16& message_text, |
| 93 | const base::string16& default_prompt_text, |
[email protected] | 71a88bb | 2013-02-01 22:05:15 | [diff] [blame] | 94 | const JavaScriptDialogManager::DialogClosedCallback& callback) |
| 95 | : manager_(manager), |
[email protected] | f221002 | 2012-03-29 00:36:08 | [diff] [blame] | 96 | callback_(callback) { |
[email protected] | be2510c0 | 2012-05-28 14:52:14 | [diff] [blame] | 97 | bool text_field = message_type == JAVASCRIPT_MESSAGE_TYPE_PROMPT; |
| 98 | bool one_button = message_type == JAVASCRIPT_MESSAGE_TYPE_ALERT; |
[email protected] | f221002 | 2012-03-29 00:36:08 | [diff] [blame] | 99 | |
| 100 | helper_ = |
[email protected] | 71a88bb | 2013-02-01 22:05:15 | [diff] [blame] | 101 | [[ShellJavaScriptDialogHelper alloc] initHelperWithManager:manager |
[email protected] | f221002 | 2012-03-29 00:36:08 | [diff] [blame] | 102 | andCallback:callback]; |
| 103 | |
| 104 | // Show the modal dialog. |
[email protected] | 96720fa | 2012-04-04 20:26:15 | [diff] [blame] | 105 | NSAlert* alert = [helper_ alert]; |
[email protected] | f221002 | 2012-03-29 00:36:08 | [diff] [blame] | 106 | NSTextField* field = nil; |
| 107 | if (text_field) { |
| 108 | field = [helper_ textField]; |
| 109 | [field setStringValue:base::SysUTF16ToNSString(default_prompt_text)]; |
| 110 | } |
[email protected] | 96720fa | 2012-04-04 20:26:15 | [diff] [blame] | 111 | [alert setDelegate:helper_]; |
| 112 | [alert setInformativeText:base::SysUTF16ToNSString(message_text)]; |
| 113 | [alert setMessageText:@"Javascript alert"]; |
| 114 | [alert addButtonWithTitle:@"OK"]; |
[email protected] | f221002 | 2012-03-29 00:36:08 | [diff] [blame] | 115 | if (!one_button) { |
[email protected] | 96720fa | 2012-04-04 20:26:15 | [diff] [blame] | 116 | NSButton* other = [alert addButtonWithTitle:@"Cancel"]; |
[email protected] | f221002 | 2012-03-29 00:36:08 | [diff] [blame] | 117 | [other setKeyEquivalent:@"\e"]; |
| 118 | } |
| 119 | |
[email protected] | 96720fa | 2012-04-04 20:26:15 | [diff] [blame] | 120 | [alert |
[email protected] | f221002 | 2012-03-29 00:36:08 | [diff] [blame] | 121 | beginSheetModalForWindow:nil // nil here makes it app-modal |
| 122 | modalDelegate:helper_ |
| 123 | didEndSelector:@selector(alertDidEnd:returnCode:contextInfo:) |
| 124 | contextInfo:this]; |
| 125 | |
[email protected] | 96720fa | 2012-04-04 20:26:15 | [diff] [blame] | 126 | if ([alert accessoryView]) |
| 127 | [[alert window] makeFirstResponder:[alert accessoryView]]; |
[email protected] | f221002 | 2012-03-29 00:36:08 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | ShellJavaScriptDialog::~ShellJavaScriptDialog() { |
| 131 | [helper_ release]; |
| 132 | } |
| 133 | |
| 134 | void ShellJavaScriptDialog::Cancel() { |
| 135 | [helper_ cancel]; |
| 136 | } |
| 137 | |
| 138 | } // namespace content |