OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #import "ios/clean/chrome/browser/ui/dialogs/http_auth_dialogs/http_auth_dialog_
mediator.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "components/strings/grit/components_strings.h" |
| 9 #include "ios/chrome/grit/ios_strings.h" |
| 10 #import "ios/clean/chrome/browser/ui/commands/http_auth_dialog_commands.h" |
| 11 #import "ios/clean/chrome/browser/ui/dialogs/dialog_button_configuration.h" |
| 12 #import "ios/clean/chrome/browser/ui/dialogs/dialog_consumer.h" |
| 13 #import "ios/clean/chrome/browser/ui/dialogs/dialog_mediator+subclassing.h" |
| 14 #import "ios/clean/chrome/browser/ui/dialogs/dialog_text_field_configuration.h" |
| 15 #import "ios/clean/chrome/browser/ui/dialogs/http_auth_dialogs/http_auth_dialog_
request.h" |
| 16 #import "ios/shared/chrome/browser/ui/commands/command_dispatcher.h" |
| 17 #include "ui/base/l10n/l10n_util.h" |
| 18 |
| 19 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 20 #error "This file requires ARC support." |
| 21 #endif |
| 22 |
| 23 namespace { |
| 24 // Dialog configuration tags. |
| 25 NSString* const kOKButtonID = @"OKButtonID"; |
| 26 NSString* const kCancelButtonID = @"CancelButtonID"; |
| 27 NSString* const kUsernameTextFieldID = @"UsernameTextFieldID"; |
| 28 NSString* const kPasswordTextFieldID = @"PasswordTextFieldID"; |
| 29 } |
| 30 |
| 31 @interface HTTPAuthDialogMediator () |
| 32 |
| 33 // The dispatcher for dismissal. |
| 34 @property(nonatomic, readonly, strong) id<HTTPAuthDialogDismissalCommands> |
| 35 dismissalDispatcher; |
| 36 // The request for this dialog. |
| 37 @property(nonatomic, readonly, strong) HTTPAuthDialogRequest* request; |
| 38 |
| 39 @end |
| 40 |
| 41 @implementation HTTPAuthDialogMediator |
| 42 @synthesize dismissalDispatcher = _dismissalDispatcher; |
| 43 @synthesize request = _request; |
| 44 |
| 45 - (instancetype)initWithRequest:(HTTPAuthDialogRequest*)request |
| 46 dispatcher: |
| 47 (id<HTTPAuthDialogDismissalCommands>)dispatcher { |
| 48 DCHECK(request); |
| 49 DCHECK(dispatcher); |
| 50 if ((self = [super init])) { |
| 51 _request = request; |
| 52 _dismissalDispatcher = dispatcher; |
| 53 } |
| 54 return self; |
| 55 } |
| 56 |
| 57 #pragma mark - DialogDismissalCommands |
| 58 |
| 59 - (void)dismissDialogWithButtonID:(id)buttonID |
| 60 textFieldValues: |
| 61 (NSDictionary<id, NSString*>*)textFieldValues { |
| 62 NSString* username = nil; |
| 63 NSString* password = nil; |
| 64 if (buttonID == kOKButtonID) { |
| 65 username = textFieldValues[kUsernameTextFieldID]; |
| 66 username = username ? username : @""; |
| 67 password = textFieldValues[kPasswordTextFieldID]; |
| 68 password = password ? password : @""; |
| 69 } else if (buttonID == kCancelButtonID) { |
| 70 // Use nil in callbacks for cancelled HTTP auth dialogs. |
| 71 } else { |
| 72 NOTREACHED() << "Received dialog dismissal for unknown button ID."; |
| 73 } |
| 74 [self.request completeAuthenticationWithUsername:username password:password]; |
| 75 [self.dismissalDispatcher dismissHTTPAuthDialog]; |
| 76 } |
| 77 |
| 78 @end |
| 79 |
| 80 @implementation HTTPAuthDialogMediator (Subclassing) |
| 81 |
| 82 - (NSString*)dialogTitle { |
| 83 return self.request.title; |
| 84 } |
| 85 |
| 86 - (NSString*)dialogMessage { |
| 87 return self.request.message; |
| 88 } |
| 89 |
| 90 - (NSArray<DialogButtonConfiguration*>*)buttonConfigs { |
| 91 return @[ |
| 92 [DialogButtonConfiguration itemWithText:l10n_util::GetNSString(IDS_OK) |
| 93 style:DialogButtonStyle::DEFAULT |
| 94 identifier:kOKButtonID], |
| 95 [DialogButtonConfiguration itemWithText:l10n_util::GetNSString(IDS_CANCEL) |
| 96 style:DialogButtonStyle::CANCEL |
| 97 identifier:kCancelButtonID], |
| 98 ]; |
| 99 } |
| 100 |
| 101 - (NSArray<DialogTextFieldConfiguration*>*)textFieldConfigs { |
| 102 NSString* defaultUsername = self.request.defaultUserNameText; |
| 103 NSString* usernamePlaceholder = |
| 104 l10n_util::GetNSString(IDS_IOS_HTTP_LOGIN_DIALOG_USERNAME_PLACEHOLDER); |
| 105 NSString* passwordPlaceholder = |
| 106 l10n_util::GetNSString(IDS_IOS_HTTP_LOGIN_DIALOG_PASSWORD_PLACEHOLDER); |
| 107 return @[ |
| 108 [DialogTextFieldConfiguration itemWithDefaultText:defaultUsername |
| 109 placeholderText:usernamePlaceholder |
| 110 secure:NO |
| 111 identifier:kUsernameTextFieldID], |
| 112 [DialogTextFieldConfiguration itemWithDefaultText:nil |
| 113 placeholderText:passwordPlaceholder |
| 114 secure:YES |
| 115 identifier:kPasswordTextFieldID], |
| 116 ]; |
| 117 } |
| 118 |
| 119 @end |
OLD | NEW |