blob: d77829d8ff8451a77b96daa7978739f9b7a73ebc [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
[email protected]a8522032013-06-24 22:51:4617 base::scoped_nsobject<NSAlert> alert_;
[email protected]f2210022012-03-29 00:36:0818 NSTextField* textField_; // WEAK; owned by alert_
19
20 // Copies of the fields in ShellJavaScriptDialog because they're private.
[email protected]71a88bb2013-02-01 22:05:1521 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]) {
[email protected]71a88bb2013-02-01 22:05:1540 manager_ = manager;
Avi Drissmane04d3992017-10-05 15:11:3641 callback_ = std::move(callback);
[email protected]f2210022012-03-29 00:36:0842 }
43
44 return self;
45}
46
47- (NSAlert*)alert {
48 alert_.reset([[NSAlert alloc] init]);
49 return alert_;
50}
51
52- (NSTextField*)textField {
53 textField_ = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 300, 22)];
54 [[textField_ cell] setLineBreakMode:NSLineBreakByTruncatingTail];
55 [alert_ setAccessoryView:textField_];
avic4303ae2015-10-15 00:21:3056 [[alert_ window] setInitialFirstResponder:textField_];
[email protected]f2210022012-03-29 00:36:0857 [textField_ release];
58
59 return textField_;
60}
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;
[email protected]fcf75d42013-12-03 20:11:2668 base::string16 input;
[email protected]f2210022012-03-29 00:36:0869 if (textField_)
70 input = base::SysNSStringToUTF16([textField_ stringValue]);
71
Avi Drissmane04d3992017-10-05 15:11:3672 std::move(callback_).Run(success, input);
Elly Fong-Jonesbd6d39d52019-01-23 22:10:2873 manager_->DialogClosed(dialog);
[email protected]f2210022012-03-29 00:36:0874}
75
76- (void)cancel {
77 [NSApp endSheet:[alert_ window]];
78 alert_.reset();
79}
80
81@end
82
83namespace content {
84
85ShellJavaScriptDialog::ShellJavaScriptDialog(
[email protected]71a88bb2013-02-01 22:05:1586 ShellJavaScriptDialogManager* manager,
[email protected]fc4f4dd42012-07-30 20:52:4887 gfx::NativeWindow parent_window,
avi777ff452017-02-09 19:04:4888 JavaScriptDialogType dialog_type,
[email protected]fcf75d42013-12-03 20:11:2689 const base::string16& message_text,
90 const base::string16& default_prompt_text,
Avi Drissmane04d3992017-10-05 15:11:3691 JavaScriptDialogManager::DialogClosedCallback callback)
92 : callback_(std::move(callback)) {
avi777ff452017-02-09 19:04:4893 bool text_field = dialog_type == JAVASCRIPT_DIALOG_TYPE_PROMPT;
94 bool one_button = dialog_type == JAVASCRIPT_DIALOG_TYPE_ALERT;
[email protected]f2210022012-03-29 00:36:0895
Avi Drissmane04d3992017-10-05 15:11:3696 helper_ = [[ShellJavaScriptDialogHelper alloc]
97 initHelperWithManager:manager
98 andCallback:std::move(callback)];
[email protected]f2210022012-03-29 00:36:0899
100 // Show the modal dialog.
[email protected]96720fa2012-04-04 20:26:15101 NSAlert* alert = [helper_ alert];
[email protected]f2210022012-03-29 00:36:08102 NSTextField* field = nil;
103 if (text_field) {
104 field = [helper_ textField];
105 [field setStringValue:base::SysUTF16ToNSString(default_prompt_text)];
106 }
[email protected]96720fa2012-04-04 20:26:15107 [alert setDelegate:helper_];
108 [alert setInformativeText:base::SysUTF16ToNSString(message_text)];
109 [alert setMessageText:@"Javascript alert"];
110 [alert addButtonWithTitle:@"OK"];
[email protected]f2210022012-03-29 00:36:08111 if (!one_button) {
[email protected]96720fa2012-04-04 20:26:15112 NSButton* other = [alert addButtonWithTitle:@"Cancel"];
[email protected]f2210022012-03-29 00:36:08113 [other setKeyEquivalent:@"\e"];
114 }
115
Elly Fong-Jonesbd6d39d52019-01-23 22:10:28116 [alert beginSheetModalForWindow:nil // nil here makes it app-modal
117 completionHandler:^void(NSModalResponse returnCode) {
118 [helper_ alertDidEndWithResult:returnCode dialog:this];
119 }];
[email protected]f2210022012-03-29 00:36:08120}
121
122ShellJavaScriptDialog::~ShellJavaScriptDialog() {
123 [helper_ release];
124}
125
126void ShellJavaScriptDialog::Cancel() {
127 [helper_ cancel];
128}
129
130} // namespace content