Chromium Code Reviews| Index: ios/chrome/browser/passwords/update_password_infobar_controller.mm |
| diff --git a/ios/chrome/browser/passwords/update_password_infobar_controller.mm b/ios/chrome/browser/passwords/update_password_infobar_controller.mm |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..f1df6ac6abb39cf2d0b9829c0b14bfd2e8b9da21 |
| --- /dev/null |
| +++ b/ios/chrome/browser/passwords/update_password_infobar_controller.mm |
| @@ -0,0 +1,119 @@ |
| +// Copyright 2016 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/chrome/browser/passwords/update_password_infobar_controller.h" |
| + |
| +#include "base/strings/string_util.h" |
| +#include "base/strings/sys_string_conversions.h" |
| +#include "ios/chrome/browser/infobars/confirm_infobar_controller+protected.h" |
| +#import "ios/chrome/browser/infobars/infobar_picker_controller.h" |
| +#import "ios/chrome/browser/infobars/infobar_picker_coordinator.h" |
|
marq (ping after 24h)
2016/06/30 16:17:38
Should the coordinator be in this CL? I haven't se
Jackie Quinn
2016/07/01 12:01:59
Yes, I haven't sent you the CL because I was waiti
|
| +#include "ios/chrome/browser/passwords/ios_chrome_update_password_infobar_delegate.h" |
| +#import "ios/public/provider/chrome/browser/ui/infobar_view_protocol.h" |
| + |
| +namespace { |
| +// Tag for the account link in the info bar message. Set to 10 to avoid conflict |
| +// with tags from superclass ConfirmInfoBarController, which uses tags 1-4. |
| +NSUInteger kAccountTag = 10; |
|
marq (ping after 24h)
2016/06/30 16:17:38
This is fragile; ideally the superclass would expo
Jackie Quinn
2016/07/01 12:01:59
I agree... I was considering moving the enum to a
|
| +} |
| + |
| +@interface UpdatePasswordInfoBarController ()<InfoBarPickerControllerDelegate> { |
| + __unsafe_unretained IOSChromeUpdatePasswordInfoBarDelegate* _delegate; |
| + // Coordinator for presenting and dismissing the picker view for choosing |
| + // the account for which to update password. |
| + base::scoped_nsobject<InfoBarPickerCoordinator> _pickerCoordinator; |
| + // Array of accounts that the password might be updated for. |
| + base::scoped_nsobject<NSArray<NSString*>> _accounts; |
| +} |
| +// Action for when the "Done" button is pressed on the displayed picker view. |
| +- (void)pickerDonePressed; |
| +// Action for when the "Cancel" button is pressed on the displayed picker view. |
| +- (void)pickerCancelPressed; |
| +@end |
| + |
| +@implementation UpdatePasswordInfoBarController |
| + |
| +- (base::scoped_nsobject<UIView<InfoBarViewProtocol>>) |
| +viewForDelegate:(IOSChromeUpdatePasswordInfoBarDelegate*)delegate |
| + frame:(CGRect)frame { |
| + _delegate = delegate; |
|
marq (ping after 24h)
2016/06/30 16:17:38
The superclass stores the delegate already, just u
Jackie Quinn
2016/07/01 12:01:59
I agree, but I need access to the delegate as IOSC
|
| + return [super viewForDelegate:delegate frame:frame]; |
| +} |
| + |
| +- (void)updateInfobarLabel:(UIView<InfoBarViewProtocol>*)view { |
| + [super updateInfobarLabel:view]; |
| + |
| + // Get the message text with current links marked. |
| + base::string16 messageText = base::SysNSStringToUTF16(view.markedLabel); |
| + // If there are multiple possible credentials, turn the account string into a |
| + // link. |
| + if (_delegate->ShowMultipleAccounts()) { |
| + base::string16 usernameLink = base::SysNSStringToUTF16([[view class] |
| + stringAsLink:base::SysUTF16ToNSString(_delegate->selected_account()) |
| + tag:kAccountTag]); |
| + base::ReplaceFirstSubstringAfterOffset( |
| + &messageText, 0, _delegate->selected_account(), usernameLink); |
| + } |
| + |
| + [view addLabel:base::SysUTF16ToNSString(messageText) |
| + target:self |
| + action:@selector(infobarLinkDidPress:)]; |
| +} |
| + |
| +- (void)infobarLinkDidPress:(NSNumber*)tag { |
| + [super infobarLinkDidPress:tag]; |
| + if ([tag unsignedIntegerValue] != kAccountTag) |
| + return; |
| + |
| + _accounts.reset([_delegate->GetAccounts() copy]); |
| + if (!_pickerCoordinator) { |
| + UIViewController* baseViewController = |
| + [[UIApplication sharedApplication] keyWindow].rootViewController; |
| + _pickerCoordinator.reset([[InfoBarPickerCoordinator alloc] |
| + initWithBaseViewController:baseViewController]); |
| + InfoBarPickerController* pickerController = |
| + [_pickerCoordinator infoBarPickerController]; |
| + pickerController.delegate = self; |
| + [pickerController setDoneTarget:self action:@selector(pickerDonePressed)]; |
| + [pickerController setCancelTarget:self |
| + action:@selector(pickerCancelPressed)]; |
| + } |
| + [_pickerCoordinator start]; |
| +} |
| + |
| +#pragma mark InfoBarPickerControllerDelegate |
|
marq (ping after 24h)
2016/06/30 16:17:38
Why not just hand the infobar picker view controll
Jackie Quinn
2016/07/01 12:01:59
I was basing this off usage in BeforeTranslateInfo
|
| + |
| +- (NSString*)infoBarPickerController:(InfoBarPickerController*)controller |
| + textForRow:(NSInteger)row { |
| + return _accounts[row]; |
| +} |
| + |
| +- (NSInteger)infoBarPickerControllerNumberOfRows: |
| + (InfoBarPickerController*)controller { |
| + return [_accounts count]; |
| +} |
| + |
| +- (NSInteger)infoBarPickerControllerInitialRow: |
| + (InfoBarPickerController*)controller { |
| + return [_accounts |
| + indexOfObject:base::SysUTF16ToNSString(_delegate->selected_account())]; |
| +} |
| + |
| +#pragma mark Private methods |
| + |
| +- (void)pickerDonePressed { |
|
marq (ping after 24h)
2016/06/30 16:17:38
This class shouldn't be handling UI events in the
Jackie Quinn
2016/07/01 12:01:59
Acknowledged.
|
| + NSInteger selectedRow = |
| + [[_pickerCoordinator infoBarPickerController].pickerView |
| + selectedRowInComponent:0]; |
| + _delegate->set_selected_account( |
| + base::SysNSStringToUTF16(_accounts[selectedRow])); |
| + [self updateInfobarLabel:self.view]; |
| + [_pickerCoordinator stop]; |
| +} |
| + |
| +- (void)pickerCancelPressed { |
| + [_pickerCoordinator stop]; |
| +} |
| + |
| +@end |