blob: 0e716382b346aa8fcff3301e92b067ba1585f6c6 [file] [log] [blame]
[email protected]de7d61ff2013-08-20 11:30:411// Copyright 2013 The Chromium Authors. All rights reserved.
[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
[email protected]a8522032013-06-24 22:51:469#import "base/mac/scoped_nsobject.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
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 Liaob641dca2019-12-11 16:31:3917 base::scoped_nsobject<NSAlert> _alert;
18 NSTextField* _textField; // WEAK; owned by alert_
[email protected]f2210022012-03-29 00:36:0819
20 // Copies of the fields in ShellJavaScriptDialog because they're private.
Robert Liaob641dca2019-12-11 16:31:3921 content::ShellJavaScriptDialogManager* _manager;
22 content::JavaScriptDialogManager::DialogClosedCallback _callback;
[email protected]f2210022012-03-29 00:36:0823}
24
[email protected]71a88bb2013-02-01 22:05:1525- (id)initHelperWithManager:(content::ShellJavaScriptDialogManager*)manager
26 andCallback:(content::JavaScriptDialogManager::DialogClosedCallback)callback;
[email protected]f2210022012-03-29 00:36:0827- (NSAlert*)alert;
28- (NSTextField*)textField;
Elly Fong-Jonesbd6d39d52019-01-23 22:10:2829- (void)alertDidEndWithResult:(NSModalResponse)returnCode
30 dialog:(content::ShellJavaScriptDialog*)dialog;
[email protected]f2210022012-03-29 00:36:0831- (void)cancel;
32
33@end
34
35@implementation ShellJavaScriptDialogHelper
36
[email protected]71a88bb2013-02-01 22:05:1537- (id)initHelperWithManager:(content::ShellJavaScriptDialogManager*)manager
38 andCallback:(content::JavaScriptDialogManager::DialogClosedCallback)callback {
[email protected]f2210022012-03-29 00:36:0839 if (self = [super init]) {
Robert Liaob641dca2019-12-11 16:31:3940 _manager = manager;
41 _callback = std::move(callback);
[email protected]f2210022012-03-29 00:36:0842 }
43
44 return self;
45}
46
47- (NSAlert*)alert {
Robert Liaob641dca2019-12-11 16:31:3948 _alert.reset([[NSAlert alloc] init]);
49 return _alert;
[email protected]f2210022012-03-29 00:36:0850}
51
52- (NSTextField*)textField {
Robert Liaob641dca2019-12-11 16:31:3953 _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]f2210022012-03-29 00:36:0858
Robert Liaob641dca2019-12-11 16:31:3959 return _textField;
[email protected]f2210022012-03-29 00:36:0860}
61
Elly Fong-Jonesbd6d39d52019-01-23 22:10:2862- (void)alertDidEndWithResult:(NSModalResponse)returnCode
63 dialog:(content::ShellJavaScriptDialog*)dialog {
64 if (returnCode == NSModalResponseStop)
[email protected]f2210022012-03-29 00:36:0865 return;
66
67 bool success = returnCode == NSAlertFirstButtonReturn;
Jan Wilken Dörrieaace0cfef2021-03-11 22:01:5868 std::u16string input;
Robert Liaob641dca2019-12-11 16:31:3969 if (_textField)
70 input = base::SysNSStringToUTF16([_textField stringValue]);
[email protected]f2210022012-03-29 00:36:0871
Robert Liaob641dca2019-12-11 16:31:3972 std::move(_callback).Run(success, input);
73 _manager->DialogClosed(dialog);
[email protected]f2210022012-03-29 00:36:0874}
75
76- (void)cancel {
Robert Liaob641dca2019-12-11 16:31:3977 [NSApp endSheet:[_alert window]];
78 _alert.reset();
Fergal Daly2b053c82019-12-19 02:05:2379 if (_callback)
Jan Wilken Dörrieaace0cfef2021-03-11 22:01:5880 std::move(_callback).Run(false, std::u16string());
[email protected]f2210022012-03-29 00:36:0881}
82
83@end
84
85namespace content {
86
87ShellJavaScriptDialog::ShellJavaScriptDialog(
[email protected]71a88bb2013-02-01 22:05:1588 ShellJavaScriptDialogManager* manager,
[email protected]fc4f4dd42012-07-30 20:52:4889 gfx::NativeWindow parent_window,
avi777ff452017-02-09 19:04:4890 JavaScriptDialogType dialog_type,
Jan Wilken Dörrieaace0cfef2021-03-11 22:01:5891 const std::u16string& message_text,
92 const std::u16string& default_prompt_text,
Fergal Daly2b053c82019-12-19 02:05:2393 JavaScriptDialogManager::DialogClosedCallback callback) {
avi777ff452017-02-09 19:04:4894 bool text_field = dialog_type == JAVASCRIPT_DIALOG_TYPE_PROMPT;
95 bool one_button = dialog_type == JAVASCRIPT_DIALOG_TYPE_ALERT;
[email protected]f2210022012-03-29 00:36:0896
Avi Drissmane04d3992017-10-05 15:11:3697 helper_ = [[ShellJavaScriptDialogHelper alloc]
98 initHelperWithManager:manager
99 andCallback:std::move(callback)];
[email protected]f2210022012-03-29 00:36:08100
101 // Show the modal dialog.
[email protected]96720fa2012-04-04 20:26:15102 NSAlert* alert = [helper_ alert];
[email protected]f2210022012-03-29 00:36:08103 NSTextField* field = nil;
104 if (text_field) {
105 field = [helper_ textField];
106 [field setStringValue:base::SysUTF16ToNSString(default_prompt_text)];
107 }
[email protected]96720fa2012-04-04 20:26:15108 [alert setDelegate:helper_];
109 [alert setInformativeText:base::SysUTF16ToNSString(message_text)];
110 [alert setMessageText:@"Javascript alert"];
111 [alert addButtonWithTitle:@"OK"];
[email protected]f2210022012-03-29 00:36:08112 if (!one_button) {
[email protected]96720fa2012-04-04 20:26:15113 NSButton* other = [alert addButtonWithTitle:@"Cancel"];
[email protected]f2210022012-03-29 00:36:08114 [other setKeyEquivalent:@"\e"];
115 }
116
Elly Fong-Jonesbd6d39d52019-01-23 22:10:28117 [alert beginSheetModalForWindow:nil // nil here makes it app-modal
118 completionHandler:^void(NSModalResponse returnCode) {
119 [helper_ alertDidEndWithResult:returnCode dialog:this];
120 }];
[email protected]f2210022012-03-29 00:36:08121}
122
123ShellJavaScriptDialog::~ShellJavaScriptDialog() {
124 [helper_ release];
125}
126
127void ShellJavaScriptDialog::Cancel() {
128 [helper_ cancel];
129}
130
131} // namespace content