2012-04-16 23:15:27 +02:00
|
|
|
// Copyright (c) 2012 The Chromium Embedded Framework Authors.
|
|
|
|
// Portions copyright (c) 2012 The Chromium Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2015-11-17 19:20:13 +01:00
|
|
|
#include "libcef/browser/native/javascript_dialog_runner_mac.h"
|
2012-04-16 23:15:27 +02:00
|
|
|
|
|
|
|
#import <Cocoa/Cocoa.h>
|
|
|
|
|
2015-11-17 19:20:13 +01:00
|
|
|
#include "base/bind.h"
|
2013-04-16 00:16:01 +02:00
|
|
|
#include "base/strings/sys_string_conversions.h"
|
2013-06-22 04:06:32 +02:00
|
|
|
#include "base/strings/utf_string_conversions.h"
|
2012-04-16 23:15:27 +02:00
|
|
|
|
|
|
|
// Helper object that receives the notification that the dialog/sheet is
|
|
|
|
// going away. Is responsible for cleaning itself up.
|
2019-02-01 20:40:18 +01:00
|
|
|
@interface CefJavaScriptDialogHelper : NSObject <NSAlertDelegate> {
|
2012-04-16 23:15:27 +02:00
|
|
|
@private
|
2013-07-19 00:37:02 +02:00
|
|
|
base::scoped_nsobject<NSAlert> alert_;
|
2012-04-16 23:15:27 +02:00
|
|
|
NSTextField* textField_; // WEAK; owned by alert_
|
|
|
|
|
|
|
|
// Copies of the fields in CefJavaScriptDialog because they're private.
|
2015-11-17 19:20:13 +01:00
|
|
|
CefJavaScriptDialogRunner::DialogClosedCallback callback_;
|
2012-04-16 23:15:27 +02:00
|
|
|
}
|
|
|
|
|
2015-11-17 19:20:13 +01:00
|
|
|
- (id)initHelperWithCallback:
|
2017-05-17 11:29:28 +02:00
|
|
|
(CefJavaScriptDialogRunner::DialogClosedCallback)callback;
|
2012-04-16 23:15:27 +02:00
|
|
|
- (NSAlert*)alert;
|
|
|
|
- (NSTextField*)textField;
|
2019-07-25 18:28:41 +02:00
|
|
|
- (void)alertDidEnd:(NSAlert*)alert
|
|
|
|
returnCode:(int)returnCode
|
|
|
|
contextInfo:(void*)contextInfo;
|
2012-04-16 23:15:27 +02:00
|
|
|
- (void)cancel;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation CefJavaScriptDialogHelper
|
|
|
|
|
2015-11-17 19:20:13 +01:00
|
|
|
- (id)initHelperWithCallback:
|
2017-05-17 11:29:28 +02:00
|
|
|
(CefJavaScriptDialogRunner::DialogClosedCallback)callback {
|
2015-11-17 19:20:13 +01:00
|
|
|
if (self = [super init])
|
2020-03-04 01:29:39 +01:00
|
|
|
callback_ = std::move(callback);
|
2012-04-16 23:15:27 +02:00
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSAlert*)alert {
|
|
|
|
alert_.reset([[NSAlert alloc] init]);
|
|
|
|
return alert_;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSTextField*)textField {
|
|
|
|
textField_ = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 300, 22)];
|
|
|
|
[[textField_ cell] setLineBreakMode:NSLineBreakByTruncatingTail];
|
|
|
|
[alert_ setAccessoryView:textField_];
|
2015-11-10 21:18:16 +01:00
|
|
|
[[alert_ window] setInitialFirstResponder:textField_];
|
2012-04-16 23:15:27 +02:00
|
|
|
[textField_ release];
|
|
|
|
|
|
|
|
return textField_;
|
|
|
|
}
|
|
|
|
|
2019-07-25 18:28:41 +02:00
|
|
|
- (void)alertDidEnd:(NSAlert*)alert
|
|
|
|
returnCode:(int)returnCode
|
|
|
|
contextInfo:(void*)contextInfo {
|
2019-02-01 20:40:18 +01:00
|
|
|
if (returnCode == NSModalResponseStop)
|
2012-04-16 23:15:27 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
bool success = returnCode == NSAlertFirstButtonReturn;
|
2013-12-17 23:04:35 +01:00
|
|
|
base::string16 input;
|
2012-04-16 23:15:27 +02:00
|
|
|
if (textField_)
|
|
|
|
input = base::SysNSStringToUTF16([textField_ stringValue]);
|
|
|
|
|
2020-03-04 01:29:39 +01:00
|
|
|
std::move(callback_).Run(success, input);
|
2012-04-16 23:15:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)cancel {
|
|
|
|
[NSApp endSheet:[alert_ window]];
|
|
|
|
alert_.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2015-11-17 19:20:13 +01:00
|
|
|
CefJavaScriptDialogRunnerMac::CefJavaScriptDialogRunnerMac()
|
2017-05-17 11:29:28 +02:00
|
|
|
: weak_ptr_factory_(this) {}
|
2015-11-17 19:20:13 +01:00
|
|
|
|
|
|
|
CefJavaScriptDialogRunnerMac::~CefJavaScriptDialogRunnerMac() {
|
|
|
|
Cancel();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefJavaScriptDialogRunnerMac::Run(
|
2020-09-22 21:54:02 +02:00
|
|
|
AlloyBrowserHostImpl* browser,
|
2017-03-03 23:37:23 +01:00
|
|
|
content::JavaScriptDialogType message_type,
|
2013-12-17 23:04:35 +01:00
|
|
|
const base::string16& display_url,
|
|
|
|
const base::string16& message_text,
|
|
|
|
const base::string16& default_prompt_text,
|
2020-03-04 01:29:39 +01:00
|
|
|
DialogClosedCallback callback) {
|
2015-11-17 19:20:13 +01:00
|
|
|
DCHECK(!helper_.get());
|
2020-03-04 01:29:39 +01:00
|
|
|
callback_ = std::move(callback);
|
2015-11-17 19:20:13 +01:00
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
bool text_field = message_type == content::JAVASCRIPT_DIALOG_TYPE_PROMPT;
|
|
|
|
bool one_button = message_type == content::JAVASCRIPT_DIALOG_TYPE_ALERT;
|
2012-04-16 23:15:27 +02:00
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
helper_.reset([[CefJavaScriptDialogHelper alloc]
|
2020-03-04 01:29:39 +01:00
|
|
|
initHelperWithCallback:base::BindOnce(
|
2017-05-17 11:29:28 +02:00
|
|
|
&CefJavaScriptDialogRunnerMac::DialogClosed,
|
|
|
|
weak_ptr_factory_.GetWeakPtr())]);
|
2012-04-16 23:15:27 +02:00
|
|
|
|
|
|
|
// Show the modal dialog.
|
|
|
|
NSAlert* alert = [helper_ alert];
|
|
|
|
NSTextField* field = nil;
|
|
|
|
if (text_field) {
|
|
|
|
field = [helper_ textField];
|
|
|
|
[field setStringValue:base::SysUTF16ToNSString(default_prompt_text)];
|
|
|
|
}
|
|
|
|
[alert setDelegate:helper_];
|
|
|
|
[alert setInformativeText:base::SysUTF16ToNSString(message_text)];
|
|
|
|
|
2013-12-17 23:04:35 +01:00
|
|
|
base::string16 label;
|
2012-05-31 17:19:33 +02:00
|
|
|
switch (message_type) {
|
2017-03-03 23:37:23 +01:00
|
|
|
case content::JAVASCRIPT_DIALOG_TYPE_ALERT:
|
2014-01-02 23:41:11 +01:00
|
|
|
label = base::ASCIIToUTF16("JavaScript Alert");
|
2012-04-16 23:15:27 +02:00
|
|
|
break;
|
2017-03-03 23:37:23 +01:00
|
|
|
case content::JAVASCRIPT_DIALOG_TYPE_PROMPT:
|
2014-01-02 23:41:11 +01:00
|
|
|
label = base::ASCIIToUTF16("JavaScript Prompt");
|
2012-04-16 23:15:27 +02:00
|
|
|
break;
|
2017-03-03 23:37:23 +01:00
|
|
|
case content::JAVASCRIPT_DIALOG_TYPE_CONFIRM:
|
2014-01-02 23:41:11 +01:00
|
|
|
label = base::ASCIIToUTF16("JavaScript Confirm");
|
2012-04-16 23:15:27 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!display_url.empty())
|
2014-01-02 23:41:11 +01:00
|
|
|
label += base::ASCIIToUTF16(" - ") + display_url;
|
2012-04-16 23:15:27 +02:00
|
|
|
|
|
|
|
[alert setMessageText:base::SysUTF16ToNSString(label)];
|
|
|
|
|
|
|
|
[alert addButtonWithTitle:@"OK"];
|
|
|
|
if (!one_button) {
|
|
|
|
NSButton* other = [alert addButtonWithTitle:@"Cancel"];
|
|
|
|
[other setKeyEquivalent:@"\e"];
|
|
|
|
}
|
|
|
|
|
2015-12-08 18:59:24 +01:00
|
|
|
// Calling beginSheetModalForWindow:nil is wrong API usage. For now work
|
|
|
|
// around the "callee requires a non-null argument" error that occurs when
|
|
|
|
// building with the 10.11 SDK. See http://crbug.com/383820 for related
|
|
|
|
// discussion.
|
2019-07-25 18:28:41 +02:00
|
|
|
// We can't use the newer beginSheetModalForWindow:completionHandler: variant
|
|
|
|
// because it fails silently when passed a nil argument (see issue #2726).
|
2015-12-08 18:59:24 +01:00
|
|
|
id nilArg = nil;
|
2019-07-25 18:28:41 +02:00
|
|
|
#pragma clang diagnostic push
|
|
|
|
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
|
2017-05-17 11:29:28 +02:00
|
|
|
[alert beginSheetModalForWindow:nilArg // nil here makes it app-modal
|
2019-07-25 18:28:41 +02:00
|
|
|
modalDelegate:helper_
|
|
|
|
didEndSelector:@selector(alertDidEnd:returnCode:contextInfo:)
|
|
|
|
contextInfo:this];
|
|
|
|
#pragma clang diagnostic pop
|
2012-04-16 23:15:27 +02:00
|
|
|
|
|
|
|
if ([alert accessoryView])
|
|
|
|
[[alert window] makeFirstResponder:[alert accessoryView]];
|
|
|
|
}
|
|
|
|
|
2015-11-17 19:20:13 +01:00
|
|
|
void CefJavaScriptDialogRunnerMac::Cancel() {
|
|
|
|
if (helper_.get()) {
|
|
|
|
[helper_ cancel];
|
|
|
|
helper_.reset(nil);
|
|
|
|
}
|
2012-04-16 23:15:27 +02:00
|
|
|
}
|
|
|
|
|
2015-11-17 19:20:13 +01:00
|
|
|
void CefJavaScriptDialogRunnerMac::DialogClosed(
|
|
|
|
bool success,
|
|
|
|
const base::string16& user_input) {
|
|
|
|
helper_.reset(nil);
|
2020-03-04 01:29:39 +01:00
|
|
|
std::move(callback_).Run(success, user_input);
|
2012-04-16 23:15:27 +02:00
|
|
|
}
|