1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
|
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import "components/javascript_dialogs/ios/javascript_dialog_view_coordinator.h"
#import "base/notreached.h"
#import "base/strings/sys_string_conversions.h"
#import "components/strings/grit/components_strings.h"
#import "ui/base/l10n/l10n_util.h"
@implementation JavascriptDialogViewCoordinator
- (instancetype)initWithBaseViewController:(UIViewController*)baseViewController
dialogView:
(javascript_dialogs::TabModalDialogViewIOS*)
dialogView
dialogType:(JavaScriptDialogType)dialogType
title:(NSString*)title
messageText:(NSString*)messageText
defaultPromptText:(NSString*)defaultPromptText {
_alertController =
[UIAlertController alertControllerWithTitle:title
message:messageText
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* okAction = [UIAlertAction
actionWithTitle:l10n_util::GetNSString(IDS_OK)
style:UIAlertActionStyleDefault
handler:^(UIAlertAction* _Nonnull action) {
UITextField* promptTextField =
self.alertController.textFields.firstObject;
std::u16string promptText =
promptTextField.text
? base::SysNSStringToUTF16(promptTextField.text)
: std::u16string();
dialogView->Accept(promptText);
}];
UIAlertAction* cancelAction =
[UIAlertAction actionWithTitle:l10n_util::GetNSString(IDS_CANCEL)
style:UIAlertActionStyleCancel
handler:^(UIAlertAction* _Nonnull action) {
dialogView->Cancel();
}];
switch (dialogType) {
case content::JAVASCRIPT_DIALOG_TYPE_ALERT: {
[_alertController addAction:okAction];
break;
}
case content::JAVASCRIPT_DIALOG_TYPE_CONFIRM: {
[_alertController addAction:cancelAction];
[_alertController addAction:okAction];
break;
}
case content::JAVASCRIPT_DIALOG_TYPE_PROMPT: {
[_alertController addTextFieldWithConfigurationHandler:^(
UITextField* _Nonnull textField) {
textField.placeholder = defaultPromptText;
}];
[_alertController addAction:cancelAction];
[_alertController addAction:okAction];
break;
}
default:
NOTREACHED();
}
[baseViewController presentViewController:_alertController
animated:YES
completion:nil];
return self;
}
- (std::u16string)promptText {
UITextField* promptTextField = self.alertController.textFields.firstObject;
return base::SysNSStringToUTF16(promptTextField.text);
}
@end
|