blob: 4ac947d22b42de7e2ffa165e5bfdbc98fd0e0605 [file] [log] [blame]
Avi Drissman4e1b7bc32022-09-15 14:03:501// Copyright 2013 The Chromium Authors
[email protected]f2210022012-03-29 00:36:082// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]de7d61ff2013-08-20 11:30:415#include "content/shell/browser/shell_javascript_dialog.h"
[email protected]f2210022012-03-29 00:36:086
7#import <Cocoa/Cocoa.h>
8
Avi Drissman49e8c2b32023-04-25 20:52:139#include "base/memory/raw_ptr.h"
[email protected]40d11e02013-03-28 17:43:1410#include "base/strings/sys_string_conversions.h"
[email protected]de7d61ff2013-08-20 11:30:4111#include "content/shell/browser/shell_javascript_dialog_manager.h"
[email protected]f2210022012-03-29 00:36:0812
[email protected]f2210022012-03-29 00:36:0813// 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 Drissman49e8c2b32023-04-25 20:52:1316 NSAlert* __strong _alert;
17 NSTextField* __weak _textField;
[email protected]f2210022012-03-29 00:36:0818
19 // Copies of the fields in ShellJavaScriptDialog because they're private.
Keishi Hattori7c3c7182022-06-24 22:18:1420 raw_ptr<content::ShellJavaScriptDialogManager> _manager;
Robert Liaob641dca2019-12-11 16:31:3921 content::JavaScriptDialogManager::DialogClosedCallback _callback;
[email protected]f2210022012-03-29 00:36:0822}
23
[email protected]71a88bb2013-02-01 22:05:1524- (id)initHelperWithManager:(content::ShellJavaScriptDialogManager*)manager
25 andCallback:(content::JavaScriptDialogManager::DialogClosedCallback)callback;
[email protected]f2210022012-03-29 00:36:0826- (NSAlert*)alert;
27- (NSTextField*)textField;
Elly Fong-Jonesbd6d39d52019-01-23 22:10:2828- (void)alertDidEndWithResult:(NSModalResponse)returnCode
29 dialog:(content::ShellJavaScriptDialog*)dialog;
[email protected]f2210022012-03-29 00:36:0830- (void)cancel;
31
32@end
33
34@implementation ShellJavaScriptDialogHelper
35
[email protected]71a88bb2013-02-01 22:05:1536- (id)initHelperWithManager:(content::ShellJavaScriptDialogManager*)manager
37 andCallback:(content::JavaScriptDialogManager::DialogClosedCallback)callback {
[email protected]f2210022012-03-29 00:36:0838 if (self = [super init]) {
Robert Liaob641dca2019-12-11 16:31:3939 _manager = manager;
40 _callback = std::move(callback);
[email protected]f2210022012-03-29 00:36:0841 }
42
43 return self;
44}
45
46- (NSAlert*)alert {
Avi Drissman49e8c2b32023-04-25 20:52:1347 _alert = [[NSAlert alloc] init];
Robert Liaob641dca2019-12-11 16:31:3948 return _alert;
[email protected]f2210022012-03-29 00:36:0849}
50
51- (NSTextField*)textField {
Avi Drissman49e8c2b32023-04-25 20:52:1352 NSTextField* textField =
53 [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 300, 22)];
54 textField.cell.lineBreakMode = NSLineBreakByTruncatingTail;
[email protected]f2210022012-03-29 00:36:0855
Avi Drissman49e8c2b32023-04-25 20:52:1356 _alert.accessoryView = textField;
57 _alert.window.initialFirstResponder = textField;
58
59 _textField = textField;
60 return textField;
[email protected]f2210022012-03-29 00:36:0861}
62
Elly Fong-Jonesbd6d39d52019-01-23 22:10:2863- (void)alertDidEndWithResult:(NSModalResponse)returnCode
64 dialog:(content::ShellJavaScriptDialog*)dialog {
Avi Drissman49e8c2b32023-04-25 20:52:1365 if (returnCode == NSModalResponseStop) {
[email protected]f2210022012-03-29 00:36:0866 return;
Avi Drissman49e8c2b32023-04-25 20:52:1367 }
[email protected]f2210022012-03-29 00:36:0868
69 bool success = returnCode == NSAlertFirstButtonReturn;
Jan Wilken Dörrieaace0cfef2021-03-11 22:01:5870 std::u16string input;
Avi Drissman49e8c2b32023-04-25 20:52:1371 if (_textField) {
72 input = base::SysNSStringToUTF16(_textField.stringValue);
73 }
[email protected]f2210022012-03-29 00:36:0874
Robert Liaob641dca2019-12-11 16:31:3975 std::move(_callback).Run(success, input);
76 _manager->DialogClosed(dialog);
[email protected]f2210022012-03-29 00:36:0877}
78
79- (void)cancel {
Avi Drissman49e8c2b32023-04-25 20:52:1380 [NSApp endSheet:_alert.window];
81 _alert = nil;
82 if (_callback) {
Jan Wilken Dörrieaace0cfef2021-03-11 22:01:5883 std::move(_callback).Run(false, std::u16string());
Avi Drissman49e8c2b32023-04-25 20:52:1384 }
[email protected]f2210022012-03-29 00:36:0885}
86
87@end
88
89namespace content {
90
91ShellJavaScriptDialog::ShellJavaScriptDialog(
[email protected]71a88bb2013-02-01 22:05:1592 ShellJavaScriptDialogManager* manager,
[email protected]fc4f4dd42012-07-30 20:52:4893 gfx::NativeWindow parent_window,
avi777ff452017-02-09 19:04:4894 JavaScriptDialogType dialog_type,
Jan Wilken Dörrieaace0cfef2021-03-11 22:01:5895 const std::u16string& message_text,
96 const std::u16string& default_prompt_text,
Fergal Daly2b053c82019-12-19 02:05:2397 JavaScriptDialogManager::DialogClosedCallback callback) {
avi777ff452017-02-09 19:04:4898 bool text_field = dialog_type == JAVASCRIPT_DIALOG_TYPE_PROMPT;
99 bool one_button = dialog_type == JAVASCRIPT_DIALOG_TYPE_ALERT;
[email protected]f2210022012-03-29 00:36:08100
Avi Drissmane04d3992017-10-05 15:11:36101 helper_ = [[ShellJavaScriptDialogHelper alloc]
102 initHelperWithManager:manager
103 andCallback:std::move(callback)];
[email protected]f2210022012-03-29 00:36:08104
105 // Show the modal dialog.
[email protected]96720fa2012-04-04 20:26:15106 NSAlert* alert = [helper_ alert];
[email protected]f2210022012-03-29 00:36:08107 NSTextField* field = nil;
108 if (text_field) {
109 field = [helper_ textField];
Avi Drissman49e8c2b32023-04-25 20:52:13110 field.stringValue = base::SysUTF16ToNSString(default_prompt_text);
[email protected]f2210022012-03-29 00:36:08111 }
Avi Drissman49e8c2b32023-04-25 20:52:13112 alert.delegate = helper_;
113 alert.informativeText = base::SysUTF16ToNSString(message_text);
114 alert.messageText = @"Javascript alert";
[email protected]96720fa2012-04-04 20:26:15115 [alert addButtonWithTitle:@"OK"];
[email protected]f2210022012-03-29 00:36:08116 if (!one_button) {
[email protected]96720fa2012-04-04 20:26:15117 NSButton* other = [alert addButtonWithTitle:@"Cancel"];
Avi Drissman49e8c2b32023-04-25 20:52:13118 other.keyEquivalent = @"\e";
[email protected]f2210022012-03-29 00:36:08119 }
120
Elly Fong-Jonesbd6d39d52019-01-23 22:10:28121 [alert beginSheetModalForWindow:nil // nil here makes it app-modal
122 completionHandler:^void(NSModalResponse returnCode) {
123 [helper_ alertDidEndWithResult:returnCode dialog:this];
124 }];
[email protected]f2210022012-03-29 00:36:08125}
126
Avi Drissman49e8c2b32023-04-25 20:52:13127ShellJavaScriptDialog::~ShellJavaScriptDialog() = default;
[email protected]f2210022012-03-29 00:36:08128
129void ShellJavaScriptDialog::Cancel() {
130 [helper_ cancel];
131}
132
133} // namespace content