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