[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 | ||||
[email protected] | a852203 | 2013-06-24 22:51:46 | [diff] [blame] | 17 | base::scoped_nsobject<NSAlert> alert_; |
[email protected] | f221002 | 2012-03-29 00:36:08 | [diff] [blame] | 18 | NSTextField* textField_; // WEAK; owned by alert_ |
19 | |||||
20 | // Copies of the fields in ShellJavaScriptDialog because they're private. | ||||
[email protected] | 71a88bb | 2013-02-01 22:05:15 | [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]) { |
[email protected] | 71a88bb | 2013-02-01 22:05:15 | [diff] [blame] | 40 | manager_ = manager; |
Avi Drissman | e04d399 | 2017-10-05 15:11:36 | [diff] [blame] | 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 { | ||||
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_]; | ||||
avi | c4303ae | 2015-10-15 00:21:30 | [diff] [blame] | 56 | [[alert_ window] setInitialFirstResponder:textField_]; |
[email protected] | f221002 | 2012-03-29 00:36:08 | [diff] [blame] | 57 | [textField_ release]; |
58 | |||||
59 | return textField_; | ||||
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; | ||||
[email protected] | fcf75d4 | 2013-12-03 20:11:26 | [diff] [blame] | 68 | base::string16 input; |
[email protected] | f221002 | 2012-03-29 00:36:08 | [diff] [blame] | 69 | if (textField_) |
70 | input = base::SysNSStringToUTF16([textField_ stringValue]); | ||||
71 | |||||
Avi Drissman | e04d399 | 2017-10-05 15:11:36 | [diff] [blame] | 72 | std::move(callback_).Run(success, input); |
Elly Fong-Jones | bd6d39d5 | 2019-01-23 22:10:28 | [diff] [blame^] | 73 | manager_->DialogClosed(dialog); |
[email protected] | f221002 | 2012-03-29 00:36:08 | [diff] [blame] | 74 | } |
75 | |||||
76 | - (void)cancel { | ||||
77 | [NSApp endSheet:[alert_ window]]; | ||||
78 | alert_.reset(); | ||||
79 | } | ||||
80 | |||||
81 | @end | ||||
82 | |||||
83 | namespace content { | ||||
84 | |||||
85 | ShellJavaScriptDialog::ShellJavaScriptDialog( | ||||
[email protected] | 71a88bb | 2013-02-01 22:05:15 | [diff] [blame] | 86 | ShellJavaScriptDialogManager* manager, |
[email protected] | fc4f4dd4 | 2012-07-30 20:52:48 | [diff] [blame] | 87 | gfx::NativeWindow parent_window, |
avi | 777ff45 | 2017-02-09 19:04:48 | [diff] [blame] | 88 | JavaScriptDialogType dialog_type, |
[email protected] | fcf75d4 | 2013-12-03 20:11:26 | [diff] [blame] | 89 | const base::string16& message_text, |
90 | const base::string16& default_prompt_text, | ||||
Avi Drissman | e04d399 | 2017-10-05 15:11:36 | [diff] [blame] | 91 | JavaScriptDialogManager::DialogClosedCallback callback) |
92 | : callback_(std::move(callback)) { | ||||
avi | 777ff45 | 2017-02-09 19:04:48 | [diff] [blame] | 93 | bool text_field = dialog_type == JAVASCRIPT_DIALOG_TYPE_PROMPT; |
94 | bool one_button = dialog_type == JAVASCRIPT_DIALOG_TYPE_ALERT; | ||||
[email protected] | f221002 | 2012-03-29 00:36:08 | [diff] [blame] | 95 | |
Avi Drissman | e04d399 | 2017-10-05 15:11:36 | [diff] [blame] | 96 | helper_ = [[ShellJavaScriptDialogHelper alloc] |
97 | initHelperWithManager:manager | ||||
98 | andCallback:std::move(callback)]; | ||||
[email protected] | f221002 | 2012-03-29 00:36:08 | [diff] [blame] | 99 | |
100 | // Show the modal dialog. | ||||
[email protected] | 96720fa | 2012-04-04 20:26:15 | [diff] [blame] | 101 | NSAlert* alert = [helper_ alert]; |
[email protected] | f221002 | 2012-03-29 00:36:08 | [diff] [blame] | 102 | NSTextField* field = nil; |
103 | if (text_field) { | ||||
104 | field = [helper_ textField]; | ||||
105 | [field setStringValue:base::SysUTF16ToNSString(default_prompt_text)]; | ||||
106 | } | ||||
[email protected] | 96720fa | 2012-04-04 20:26:15 | [diff] [blame] | 107 | [alert setDelegate:helper_]; |
108 | [alert setInformativeText:base::SysUTF16ToNSString(message_text)]; | ||||
109 | [alert setMessageText:@"Javascript alert"]; | ||||
110 | [alert addButtonWithTitle:@"OK"]; | ||||
[email protected] | f221002 | 2012-03-29 00:36:08 | [diff] [blame] | 111 | if (!one_button) { |
[email protected] | 96720fa | 2012-04-04 20:26:15 | [diff] [blame] | 112 | NSButton* other = [alert addButtonWithTitle:@"Cancel"]; |
[email protected] | f221002 | 2012-03-29 00:36:08 | [diff] [blame] | 113 | [other setKeyEquivalent:@"\e"]; |
114 | } | ||||
115 | |||||
Elly Fong-Jones | bd6d39d5 | 2019-01-23 22:10:28 | [diff] [blame^] | 116 | [alert beginSheetModalForWindow:nil // nil here makes it app-modal |
117 | completionHandler:^void(NSModalResponse returnCode) { | ||||
118 | [helper_ alertDidEndWithResult:returnCode dialog:this]; | ||||
119 | }]; | ||||
[email protected] | f221002 | 2012-03-29 00:36:08 | [diff] [blame] | 120 | } |
121 | |||||
122 | ShellJavaScriptDialog::~ShellJavaScriptDialog() { | ||||
123 | [helper_ release]; | ||||
124 | } | ||||
125 | |||||
126 | void ShellJavaScriptDialog::Cancel() { | ||||
127 | [helper_ cancel]; | ||||
128 | } | ||||
129 | |||||
130 | } // namespace content |