OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 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/chrome/browser/passwords/update_password_infobar_controller.h" | |
6 | |
7 #include "base/strings/string_util.h" | |
8 #include "base/strings/sys_string_conversions.h" | |
9 #include "ios/chrome/browser/infobars/confirm_infobar_controller+protected.h" | |
10 #import "ios/chrome/browser/infobars/infobar_picker_controller.h" | |
11 #import "ios/chrome/browser/infobars/infobar_picker_coordinator.h" | |
12 #include "ios/chrome/browser/passwords/ios_chrome_update_password_infobar_delega te.h" | |
13 #import "ios/public/provider/chrome/browser/ui/infobar_view_protocol.h" | |
14 | |
15 namespace { | |
16 // Tag for the account link in the info bar message. Set to 10 to avoid conflict | |
17 // with tags from superclasses. | |
vabr (Chromium)
2016/06/30 13:31:48
Just wondering: is this based on the fact that eve
Jackie Quinn
2016/06/30 13:52:50
ConfirmInfoBarController uses 1-4 in its enum for
vabr (Chromium)
2016/06/30 15:11:27
Thanks for explaining. Optionally, you could squee
| |
18 NSUInteger kAccountTag = 10; | |
19 } | |
20 | |
21 @interface UpdatePasswordInfoBarController ()<InfoBarPickerControllerDelegate> { | |
22 IOSChromeUpdatePasswordInfoBarDelegate* _delegate; // weak | |
vabr (Chromium)
2016/06/30 13:31:48
https://docs.google.com/document/d/1AXwleU_zbOLB_o
Jackie Quinn
2016/06/30 13:52:50
Ah good call! Done.
Jackie Quinn
2016/06/30 14:29:59
... nevermind. IOSChromeUpdatePasswordInfoBarDeleg
vabr (Chromium)
2016/06/30 15:11:27
Sorry! :)
| |
23 // Coordinator for presenting and dismissing the picker view for choosing | |
24 // the account for which to update password. | |
25 base::scoped_nsobject<InfoBarPickerCoordinator> _pickerCoordinator; | |
26 // Array of accounts that the password might be updated for. | |
27 base::scoped_nsobject<NSArray<NSString*>> _accounts; | |
28 } | |
29 // Action for when the "Done" button is pressed on the displayed picker view. | |
30 - (void)pickerDonePressed; | |
31 // Action for when the "Cancel" button is pressed on the displayed picker view. | |
32 - (void)pickerCancelPressed; | |
33 @end | |
34 | |
35 @implementation UpdatePasswordInfoBarController | |
36 | |
37 - (base::scoped_nsobject<UIView<InfoBarViewProtocol>>) | |
38 viewForDelegate:(IOSChromeUpdatePasswordInfoBarDelegate*)delegate | |
39 frame:(CGRect)frame { | |
40 _delegate = delegate; | |
41 return [super viewForDelegate:delegate frame:frame]; | |
42 } | |
43 | |
44 - (void)updateInfobarLabel:(UIView<InfoBarViewProtocol>*)view { | |
45 [super updateInfobarLabel:view]; | |
46 | |
47 // Get the message text with current links marked. | |
48 base::string16 messageText = base::SysNSStringToUTF16(view.markedLabel); | |
49 // If there are multiple possible credentials, turn the account string into a | |
50 // link. | |
51 if (_delegate->ShowMultipleAccounts()) { | |
52 base::string16 usernameLink = base::SysNSStringToUTF16([[view class] | |
53 stringAsLink:base::SysUTF16ToNSString(_delegate->selected_account()) | |
54 tag:kAccountTag]); | |
55 base::ReplaceFirstSubstringAfterOffset( | |
56 &messageText, 0, _delegate->selected_account(), usernameLink); | |
57 } | |
58 | |
59 [view addLabel:base::SysUTF16ToNSString(messageText) | |
60 target:self | |
61 action:@selector(infobarLinkDidPress:)]; | |
62 } | |
63 | |
64 - (void)infobarLinkDidPress:(NSNumber*)tag { | |
65 [super infobarLinkDidPress:tag]; | |
66 if ([tag unsignedIntegerValue] != kAccountTag) | |
67 return; | |
68 | |
69 _accounts.reset([_delegate->GetAccounts() copy]); | |
70 if (!_pickerCoordinator) { | |
71 UIViewController* baseViewController = | |
72 [[UIApplication sharedApplication] keyWindow].rootViewController; | |
73 _pickerCoordinator.reset([[InfoBarPickerCoordinator alloc] | |
74 initWithBaseViewController:baseViewController]); | |
75 InfoBarPickerController* pickerController = | |
76 [_pickerCoordinator infoBarPickerController]; | |
77 pickerController.delegate = self; | |
78 [pickerController setDoneTarget:self action:@selector(pickerDonePressed)]; | |
79 [pickerController setCancelTarget:self | |
80 action:@selector(pickerCancelPressed)]; | |
81 } | |
82 [_pickerCoordinator start]; | |
83 } | |
84 | |
85 #pragma mark InfoBarPickerControllerDelegate | |
86 | |
87 - (NSString*)infoBarPickerController:(InfoBarPickerController*)controller | |
88 textForRow:(NSInteger)row { | |
89 return _accounts[row]; | |
90 } | |
91 | |
92 - (NSInteger)infoBarPickerControllerNumberOfRows: | |
93 (InfoBarPickerController*)controller { | |
94 return [_accounts count]; | |
95 } | |
96 | |
97 - (NSInteger)infoBarPickerControllerInitialRow: | |
98 (InfoBarPickerController*)controller { | |
99 return [_accounts | |
100 indexOfObject:base::SysUTF16ToNSString(_delegate->selected_account())]; | |
101 } | |
102 | |
103 #pragma mark Private methods | |
104 | |
105 - (void)pickerDonePressed { | |
106 NSInteger selectedRow = | |
107 [[_pickerCoordinator infoBarPickerController].pickerView | |
108 selectedRowInComponent:0]; | |
109 _delegate->set_selected_account( | |
110 base::SysNSStringToUTF16(_accounts[selectedRow])); | |
111 [self updateInfobarLabel:self.view]; | |
112 [_pickerCoordinator stop]; | |
113 } | |
114 | |
115 - (void)pickerCancelPressed { | |
116 [_pickerCoordinator stop]; | |
117 } | |
118 | |
119 @end | |
OLD | NEW |