Chromium Code Reviews| Index: ios/clean/chrome/browser/ui/dialogs/http_auth_dialogs/http_auth_dialog_mediator.mm |
| diff --git a/ios/clean/chrome/browser/ui/dialogs/http_auth_dialogs/http_auth_dialog_mediator.mm b/ios/clean/chrome/browser/ui/dialogs/http_auth_dialogs/http_auth_dialog_mediator.mm |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..f5ce496e6e03dc60ecfeead412780d9968de4398 |
| --- /dev/null |
| +++ b/ios/clean/chrome/browser/ui/dialogs/http_auth_dialogs/http_auth_dialog_mediator.mm |
| @@ -0,0 +1,124 @@ |
| +// Copyright 2017 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. |
| + |
| +#import "ios/clean/chrome/browser/ui/dialogs/http_auth_dialogs/http_auth_dialog_mediator.h" |
| + |
| +#include "base/logging.h" |
| +#include "components/strings/grit/components_strings.h" |
| +#include "ios/chrome/grit/ios_strings.h" |
| +#import "ios/clean/chrome/browser/ui/commands/dialog_commands.h" |
| +#import "ios/clean/chrome/browser/ui/commands/http_auth_dialog_commands.h" |
| +#import "ios/clean/chrome/browser/ui/dialogs/dialog_button_item.h" |
| +#import "ios/clean/chrome/browser/ui/dialogs/dialog_consumer.h" |
| +#import "ios/clean/chrome/browser/ui/dialogs/dialog_mediator+internal.h" |
| +#import "ios/clean/chrome/browser/ui/dialogs/dialog_text_field_item.h" |
| +#import "ios/clean/chrome/browser/ui/dialogs/http_auth_dialogs/http_auth_dialog_state.h" |
| +#import "ios/shared/chrome/browser/ui/commands/command_dispatcher.h" |
| +#include "ui/base/l10n/l10n_util.h" |
| + |
| +#if !defined(__has_feature) || !__has_feature(objc_arc) |
| +#error "This file requires ARC support." |
| +#endif |
| + |
| +namespace { |
| +// Button tags. |
| +NSString* const kOKButtonTag = @"OK"; |
| +NSString* const kCancelButtonTag = @"Cancel"; |
| +// An enum representing the indices of the username and password fields in the |
| +// DialogTextFieldItem array passed to the consumer. |
| +typedef NS_ENUM(NSInteger, TextFieldIndex) { |
| + USER_NAME_IDX = 0, |
| + PASSWORD_IDX, |
| + TEXT_FIELD_COUNT, |
| +}; |
| +} |
| + |
| +@interface HTTPAuthDialogMediator ()<DialogDismissalCommands> |
| + |
| +// The dispatcher to use for HTTPAuthDialogDismissalCommands. |
| +@property(nonatomic, readonly) id<HTTPAuthDialogDismissalCommands> |
| + dismissalDispatcher; |
| +// The state passed on initialization. |
| +@property(nonatomic, readonly, strong) HTTPAuthDialogState* state; |
| + |
| +@end |
| + |
| +@implementation HTTPAuthDialogMediator |
| + |
| +@synthesize state = _state; |
| + |
| +- (instancetype)initWithDispatcher:(CommandDispatcher*)dispatcher |
| + state:(HTTPAuthDialogState*)state { |
| + DCHECK(state); |
| + if ((self = [super initWithDispatcher:dispatcher])) { |
| + _state = state; |
| + } |
| + return self; |
| +} |
| + |
| +#pragma mark - Accessors |
| + |
| +- (id<HTTPAuthDialogDismissalCommands>)dismissalDispatcher { |
| + return static_cast<id<HTTPAuthDialogDismissalCommands>>(self.dispatcher); |
| +} |
| + |
| +@end |
| + |
| +@implementation HTTPAuthDialogMediator (Internal) |
| + |
| +- (NSString*)dialogTitle { |
| + return self.state.title; |
| +} |
| + |
| +- (NSString*)dialogMessage { |
| + return self.state.message; |
| +} |
| + |
| +- (NSArray<DialogButtonItem*>*)buttonItems { |
| + NSMutableArray<DialogButtonItem*>* items = [[NSMutableArray alloc] init]; |
|
marq (ping after 24h)
2017/06/14 11:19:15
I'd prefer just a static array return here:
re
kkhorimoto
2017/06/23 06:25:41
Done.
|
| + [items addObject:[DialogButtonItem itemWithText:l10n_util::GetNSString(IDS_OK) |
| + style:DialogButtonStyle::DEFAULT |
| + tag:kOKButtonTag]]; |
| + NSString* cancelText = l10n_util::GetNSString(IDS_CANCEL); |
| + [items addObject:[DialogButtonItem itemWithText:cancelText |
| + style:DialogButtonStyle::CANCEL |
| + tag:kCancelButtonTag]]; |
| + return items; |
| +} |
| + |
| +- (NSArray<DialogTextFieldItem*>*)textFieldItems { |
| + NSMutableArray<DialogTextFieldItem*>* items = [[NSMutableArray alloc] init]; |
|
marq (ping after 24h)
2017/06/14 11:19:15
Static array return here too.
kkhorimoto
2017/06/23 06:25:41
Done.
|
| + NSString* defaultUsername = self.state.defaultUserNameText; |
| + NSString* usernamePlaceholder = |
| + l10n_util::GetNSString(IDS_IOS_HTTP_LOGIN_DIALOG_USERNAME_PLACEHOLDER); |
| + [items addObject:[DialogTextFieldItem itemWithDefaultText:defaultUsername |
| + placeholderText:usernamePlaceholder |
| + secure:NO]]; |
| + NSString* passwordPlaceholder = |
| + l10n_util::GetNSString(IDS_IOS_HTTP_LOGIN_DIALOG_PASSWORD_PLACEHOLDER); |
| + [items addObject:[DialogTextFieldItem itemWithDefaultText:nil |
| + placeholderText:passwordPlaceholder |
| + secure:YES]]; |
| + return items; |
| +} |
| + |
| +#pragma mark - DialogDismissalCommands |
| + |
| +- (void)dismissDialogWithButtonTag:(id)buttonTag |
| + userInputStrings:(NSArray<NSString*>*)inputStrings { |
| + DCHECK_EQ(inputStrings.count, TEXT_FIELD_COUNT); |
| + if (buttonTag == kOKButtonTag) { |
| + NSString* username = inputStrings[USER_NAME_IDX]; |
|
marq (ping after 24h)
2017/06/14 11:19:15
Hmm.
Should the text field items be mutable objec
kkhorimoto
2017/06/23 06:25:41
As we discussed in the weekly meeting, I just impl
|
| + NSString* password = inputStrings[PASSWORD_IDX]; |
| + [self.dismissalDispatcher dismissHTTPAuthDialogWithUsername:username |
| + password:password]; |
| + } else if (buttonTag == kCancelButtonTag) { |
| + [self.dismissalDispatcher dismissHTTPAuthDialogWithUsername:nil |
| + password:nil]; |
| + } else { |
| + NOTREACHED() << "Received dialog dismissal for unknown button tag."; |
| + } |
| +} |
| + |
| +@end |