[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 |
Robert Liao | b641dca | 2019-12-11 16:31:39 | [diff] [blame] | 17 | base::scoped_nsobject<NSAlert> _alert; |
| 18 | NSTextField* _textField; // WEAK; owned by alert_ |
[email protected] | f221002 | 2012-03-29 00:36:08 | [diff] [blame] | 19 | |
| 20 | // Copies of the fields in ShellJavaScriptDialog because they're private. |
Robert Liao | b641dca | 2019-12-11 16:31:39 | [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; |
Elly Fong-Jones | bd6d39d5 | 2019-01-23 22:10:28 | [diff] [blame] | 29 | - (void)alertDidEndWithResult:(NSModalResponse)returnCode |
| 30 | dialog:(content::ShellJavaScriptDialog*)dialog; |
[email protected] | f221002 | 2012-03-29 00:36:08 | [diff] [blame] | 31 | - (void)cancel; |
| 32 | |
| 33 | @end |
| 34 | |
| 35 | @implementation ShellJavaScriptDialogHelper |
| 36 | |
[email protected] | 71a88bb | 2013-02-01 22:05:15 | [diff] [blame] | 37 | - (id)initHelperWithManager:(content::ShellJavaScriptDialogManager*)manager |
| 38 | andCallback:(content::JavaScriptDialogManager::DialogClosedCallback)callback { |
[email protected] | f221002 | 2012-03-29 00:36:08 | [diff] [blame] | 39 | if (self = [super init]) { |
Robert Liao | b641dca | 2019-12-11 16:31:39 | [diff] [blame] | 40 | _manager = manager; |
| 41 | _callback = std::move(callback); |
[email protected] | f221002 | 2012-03-29 00:36:08 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | return self; |
| 45 | } |
| 46 | |
| 47 | - (NSAlert*)alert { |
Robert Liao | b641dca | 2019-12-11 16:31:39 | [diff] [blame] | 48 | _alert.reset([[NSAlert alloc] init]); |
| 49 | return _alert; |
[email protected] | f221002 | 2012-03-29 00:36:08 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | - (NSTextField*)textField { |
Robert Liao | b641dca | 2019-12-11 16:31:39 | [diff] [blame] | 53 | _textField = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 300, 22)]; |
| 54 | [[_textField cell] setLineBreakMode:NSLineBreakByTruncatingTail]; |
| 55 | [_alert setAccessoryView:_textField]; |
| 56 | [[_alert window] setInitialFirstResponder:_textField]; |
| 57 | [_textField release]; |
[email protected] | f221002 | 2012-03-29 00:36:08 | [diff] [blame] | 58 | |
Robert Liao | b641dca | 2019-12-11 16:31:39 | [diff] [blame] | 59 | return _textField; |
[email protected] | f221002 | 2012-03-29 00:36:08 | [diff] [blame] | 60 | } |
| 61 | |
Elly Fong-Jones | bd6d39d5 | 2019-01-23 22:10:28 | [diff] [blame] | 62 | - (void)alertDidEndWithResult:(NSModalResponse)returnCode |
| 63 | dialog:(content::ShellJavaScriptDialog*)dialog { |
| 64 | if (returnCode == NSModalResponseStop) |
[email protected] | f221002 | 2012-03-29 00:36:08 | [diff] [blame] | 65 | return; |
| 66 | |
| 67 | bool success = returnCode == NSAlertFirstButtonReturn; |
Jan Wilken Dörrie | aace0cfef | 2021-03-11 22:01:58 | [diff] [blame^] | 68 | std::u16string input; |
Robert Liao | b641dca | 2019-12-11 16:31:39 | [diff] [blame] | 69 | if (_textField) |
| 70 | input = base::SysNSStringToUTF16([_textField stringValue]); |
[email protected] | f221002 | 2012-03-29 00:36:08 | [diff] [blame] | 71 | |
Robert Liao | b641dca | 2019-12-11 16:31:39 | [diff] [blame] | 72 | std::move(_callback).Run(success, input); |
| 73 | _manager->DialogClosed(dialog); |
[email protected] | f221002 | 2012-03-29 00:36:08 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | - (void)cancel { |
Robert Liao | b641dca | 2019-12-11 16:31:39 | [diff] [blame] | 77 | [NSApp endSheet:[_alert window]]; |
| 78 | _alert.reset(); |
Fergal Daly | 2b053c8 | 2019-12-19 02:05:23 | [diff] [blame] | 79 | if (_callback) |
Jan Wilken Dörrie | aace0cfef | 2021-03-11 22:01:58 | [diff] [blame^] | 80 | std::move(_callback).Run(false, std::u16string()); |
[email protected] | f221002 | 2012-03-29 00:36:08 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | @end |
| 84 | |
| 85 | namespace content { |
| 86 | |
| 87 | ShellJavaScriptDialog::ShellJavaScriptDialog( |
[email protected] | 71a88bb | 2013-02-01 22:05:15 | [diff] [blame] | 88 | ShellJavaScriptDialogManager* manager, |
[email protected] | fc4f4dd4 | 2012-07-30 20:52:48 | [diff] [blame] | 89 | gfx::NativeWindow parent_window, |
avi | 777ff45 | 2017-02-09 19:04:48 | [diff] [blame] | 90 | JavaScriptDialogType dialog_type, |
Jan Wilken Dörrie | aace0cfef | 2021-03-11 22:01:58 | [diff] [blame^] | 91 | const std::u16string& message_text, |
| 92 | const std::u16string& default_prompt_text, |
Fergal Daly | 2b053c8 | 2019-12-19 02:05:23 | [diff] [blame] | 93 | JavaScriptDialogManager::DialogClosedCallback callback) { |
avi | 777ff45 | 2017-02-09 19:04:48 | [diff] [blame] | 94 | bool text_field = dialog_type == JAVASCRIPT_DIALOG_TYPE_PROMPT; |
| 95 | bool one_button = dialog_type == JAVASCRIPT_DIALOG_TYPE_ALERT; |
[email protected] | f221002 | 2012-03-29 00:36:08 | [diff] [blame] | 96 | |
Avi Drissman | e04d399 | 2017-10-05 15:11:36 | [diff] [blame] | 97 | helper_ = [[ShellJavaScriptDialogHelper alloc] |
| 98 | initHelperWithManager:manager |
| 99 | andCallback:std::move(callback)]; |
[email protected] | f221002 | 2012-03-29 00:36:08 | [diff] [blame] | 100 | |
| 101 | // Show the modal dialog. |
[email protected] | 96720fa | 2012-04-04 20:26:15 | [diff] [blame] | 102 | NSAlert* alert = [helper_ alert]; |
[email protected] | f221002 | 2012-03-29 00:36:08 | [diff] [blame] | 103 | NSTextField* field = nil; |
| 104 | if (text_field) { |
| 105 | field = [helper_ textField]; |
| 106 | [field setStringValue:base::SysUTF16ToNSString(default_prompt_text)]; |
| 107 | } |
[email protected] | 96720fa | 2012-04-04 20:26:15 | [diff] [blame] | 108 | [alert setDelegate:helper_]; |
| 109 | [alert setInformativeText:base::SysUTF16ToNSString(message_text)]; |
| 110 | [alert setMessageText:@"Javascript alert"]; |
| 111 | [alert addButtonWithTitle:@"OK"]; |
[email protected] | f221002 | 2012-03-29 00:36:08 | [diff] [blame] | 112 | if (!one_button) { |
[email protected] | 96720fa | 2012-04-04 20:26:15 | [diff] [blame] | 113 | NSButton* other = [alert addButtonWithTitle:@"Cancel"]; |
[email protected] | f221002 | 2012-03-29 00:36:08 | [diff] [blame] | 114 | [other setKeyEquivalent:@"\e"]; |
| 115 | } |
| 116 | |
Elly Fong-Jones | bd6d39d5 | 2019-01-23 22:10:28 | [diff] [blame] | 117 | [alert beginSheetModalForWindow:nil // nil here makes it app-modal |
| 118 | completionHandler:^void(NSModalResponse returnCode) { |
| 119 | [helper_ alertDidEndWithResult:returnCode dialog:this]; |
| 120 | }]; |
[email protected] | f221002 | 2012-03-29 00:36:08 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | ShellJavaScriptDialog::~ShellJavaScriptDialog() { |
| 124 | [helper_ release]; |
| 125 | } |
| 126 | |
| 127 | void ShellJavaScriptDialog::Cancel() { |
| 128 | [helper_ cancel]; |
| 129 | } |
| 130 | |
| 131 | } // namespace content |