| OLD | NEW |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ios/chrome/browser/infobars/confirm_infobar_controller.h" | 5 #import "ios/chrome/browser/infobars/confirm_infobar_controller.h" |
| 6 | 6 |
| 7 #include "base/mac/foundation_util.h" | 7 #include "base/mac/foundation_util.h" |
| 8 #include "base/strings/string_util.h" | 8 #include "base/strings/string_util.h" |
| 9 #include "base/strings/sys_string_conversions.h" | 9 #include "base/strings/sys_string_conversions.h" |
| 10 #include "components/infobars/core/confirm_infobar_delegate.h" | 10 #include "components/infobars/core/confirm_infobar_delegate.h" |
| 11 #include "ios/public/provider/chrome/browser/chrome_browser_provider.h" | 11 #include "ios/public/provider/chrome/browser/chrome_browser_provider.h" |
| 12 #import "ios/public/provider/chrome/browser/ui/infobar_view_delegate.h" | 12 #import "ios/public/provider/chrome/browser/ui/infobar_view_delegate.h" |
| 13 #import "ios/public/provider/chrome/browser/ui/infobar_view_protocol.h" | 13 #import "ios/public/provider/chrome/browser/ui/infobar_view_protocol.h" |
| 14 #include "ui/base/l10n/l10n_util.h" | 14 #include "ui/base/l10n/l10n_util.h" |
| 15 #include "ui/base/window_open_disposition.h" | 15 #include "ui/base/window_open_disposition.h" |
| 16 #include "ui/gfx/image/image.h" | 16 #include "ui/gfx/image/image.h" |
| 17 | 17 |
| 18 namespace { | 18 namespace { |
| 19 | 19 |
| 20 // UI Tags for the infobar elements. | 20 // UI Tags for the infobar elements. |
| 21 enum ConfirmInfoBarUITags { OK = 1, CANCEL, CLOSE, TITLE_LINK }; | 21 typedef NS_ENUM(NSInteger, ConfirmInfoBarUITags) { |
| 22 OK = 1, |
| 23 CANCEL, |
| 24 CLOSE, |
| 25 TITLE_LINK |
| 26 }; |
| 22 | 27 |
| 23 // Converts a UI button tag to the corresponding InfoBarButton. | 28 // Converts a UI button tag to the corresponding InfoBarButton. |
| 24 ConfirmInfoBarDelegate::InfoBarButton UITagToButton(NSUInteger tag) { | 29 ConfirmInfoBarDelegate::InfoBarButton UITagToButton(NSUInteger tag) { |
| 25 switch (tag) { | 30 switch (tag) { |
| 26 case ConfirmInfoBarUITags::OK: | 31 case ConfirmInfoBarUITags::OK: |
| 27 return ConfirmInfoBarDelegate::BUTTON_OK; | 32 return ConfirmInfoBarDelegate::BUTTON_OK; |
| 28 case ConfirmInfoBarUITags::CANCEL: | 33 case ConfirmInfoBarUITags::CANCEL: |
| 29 case ConfirmInfoBarUITags::CLOSE: | 34 case ConfirmInfoBarUITags::CLOSE: |
| 30 return ConfirmInfoBarDelegate::BUTTON_CANCEL; | 35 return ConfirmInfoBarDelegate::BUTTON_CANCEL; |
| 31 default: | 36 default: |
| 32 NOTREACHED(); | 37 NOTREACHED(); |
| 33 return ConfirmInfoBarDelegate::BUTTON_CANCEL; | 38 return ConfirmInfoBarDelegate::BUTTON_CANCEL; |
| 34 } | 39 } |
| 35 } | 40 } |
| 36 | 41 |
| 37 } // namespace | 42 } // namespace |
| 38 | 43 |
| 39 #pragma mark - ConfirmInfoBarController | 44 #pragma mark - ConfirmInfoBarController |
| 40 | 45 |
| 41 @interface ConfirmInfoBarController () | 46 @interface ConfirmInfoBarController () { |
| 42 | 47 ConfirmInfoBarDelegate* _confirmInfobarDelegate; // weak |
| 43 // Action for any of the user defined buttons. | 48 } |
| 44 - (void)infoBarButtonDidPress:(id)sender; | |
| 45 // Action for any of the user defined links. | |
| 46 - (void)infobarLinkDidPress:(NSNumber*)tag; | |
| 47 - (void)updateInfobarLabel:(UIView<InfoBarViewProtocol>*)view; | |
| 48 @end | 49 @end |
| 49 | 50 |
| 50 @implementation ConfirmInfoBarController { | 51 @implementation ConfirmInfoBarController |
| 51 ConfirmInfoBarDelegate* confirmInfobarDelegate_; // weak | |
| 52 } | |
| 53 | 52 |
| 54 #pragma mark - | 53 #pragma mark - |
| 55 #pragma mark InfoBarController | 54 #pragma mark InfoBarController |
| 56 | 55 |
| 57 - (base::scoped_nsobject<UIView<InfoBarViewProtocol>>) | 56 - (base::scoped_nsobject<UIView<InfoBarViewProtocol>>) |
| 58 viewForDelegate:(infobars::InfoBarDelegate*)delegate | 57 viewForDelegate:(infobars::InfoBarDelegate*)delegate |
| 59 frame:(CGRect)frame { | 58 frame:(CGRect)frame { |
| 60 base::scoped_nsobject<UIView<InfoBarViewProtocol>> infoBarView; | 59 base::scoped_nsobject<UIView<InfoBarViewProtocol>> infoBarView; |
| 61 confirmInfobarDelegate_ = delegate->AsConfirmInfoBarDelegate(); | 60 _confirmInfobarDelegate = delegate->AsConfirmInfoBarDelegate(); |
| 62 infoBarView.reset( | 61 infoBarView.reset( |
| 63 ios::GetChromeBrowserProvider()->CreateInfoBarView(frame, self.delegate)); | 62 ios::GetChromeBrowserProvider()->CreateInfoBarView(frame, self.delegate)); |
| 64 // Model data. | 63 // Model data. |
| 65 gfx::Image modelIcon = confirmInfobarDelegate_->GetIcon(); | 64 gfx::Image modelIcon = _confirmInfobarDelegate->GetIcon(); |
| 66 int buttons = confirmInfobarDelegate_->GetButtons(); | 65 int buttons = _confirmInfobarDelegate->GetButtons(); |
| 67 NSString* buttonOK = nil; | 66 NSString* buttonOK = nil; |
| 68 if (buttons & ConfirmInfoBarDelegate::BUTTON_OK) { | 67 if (buttons & ConfirmInfoBarDelegate::BUTTON_OK) { |
| 69 buttonOK = base::SysUTF16ToNSString(confirmInfobarDelegate_->GetButtonLabel( | 68 buttonOK = base::SysUTF16ToNSString(_confirmInfobarDelegate->GetButtonLabel( |
| 70 ConfirmInfoBarDelegate::BUTTON_OK)); | 69 ConfirmInfoBarDelegate::BUTTON_OK)); |
| 71 } | 70 } |
| 72 NSString* buttonCancel = nil; | 71 NSString* buttonCancel = nil; |
| 73 if (buttons & ConfirmInfoBarDelegate::BUTTON_CANCEL) { | 72 if (buttons & ConfirmInfoBarDelegate::BUTTON_CANCEL) { |
| 74 buttonCancel = | 73 buttonCancel = |
| 75 base::SysUTF16ToNSString(confirmInfobarDelegate_->GetButtonLabel( | 74 base::SysUTF16ToNSString(_confirmInfobarDelegate->GetButtonLabel( |
| 76 ConfirmInfoBarDelegate::BUTTON_CANCEL)); | 75 ConfirmInfoBarDelegate::BUTTON_CANCEL)); |
| 77 } | 76 } |
| 78 | 77 |
| 79 [infoBarView addCloseButtonWithTag:ConfirmInfoBarUITags::CLOSE | 78 [infoBarView addCloseButtonWithTag:ConfirmInfoBarUITags::CLOSE |
| 80 target:self | 79 target:self |
| 81 action:@selector(infoBarButtonDidPress:)]; | 80 action:@selector(infoBarButtonDidPress:)]; |
| 82 | 81 |
| 83 // Optional left icon. | 82 // Optional left icon. |
| 84 if (!modelIcon.IsEmpty()) | 83 if (!modelIcon.IsEmpty()) |
| 85 [infoBarView addLeftIcon:modelIcon.ToUIImage()]; | 84 [infoBarView addLeftIcon:modelIcon.ToUIImage()]; |
| 86 | 85 |
| 87 // Optional message. | 86 // Optional message. |
| 88 [self updateInfobarLabel:infoBarView]; | 87 [self updateInfobarLabel:infoBarView]; |
| 89 | 88 |
| 90 if (buttonOK && buttonCancel) { | 89 if (buttonOK && buttonCancel) { |
| 91 [infoBarView addButton1:buttonOK | 90 [infoBarView addButton1:buttonOK |
| 92 tag1:ConfirmInfoBarUITags::OK | 91 tag1:ConfirmInfoBarUITags::OK |
| 93 button2:buttonCancel | 92 button2:buttonCancel |
| 94 tag2:ConfirmInfoBarUITags::CANCEL | 93 tag2:ConfirmInfoBarUITags::CANCEL |
| 95 target:self | 94 target:self |
| 96 action:@selector(infoBarButtonDidPress:)]; | 95 action:@selector(infoBarButtonDidPress:)]; |
| 97 } else if (buttonOK) { | 96 } else if (buttonOK) { |
| 98 [infoBarView addButton:buttonOK | 97 [infoBarView addButton:buttonOK |
| 99 tag:ConfirmInfoBarUITags::OK | 98 tag:ConfirmInfoBarUITags::OK |
| 100 target:self | 99 target:self |
| 101 action:@selector(infoBarButtonDidPress:)]; | 100 action:@selector(infoBarButtonDidPress:)]; |
| 102 } else { | 101 } else { |
| 103 // No buttons, only message. | 102 // No buttons, only message. |
| 104 DCHECK(!confirmInfobarDelegate_->GetMessageText().empty() && !buttonCancel); | 103 DCHECK(!_confirmInfobarDelegate->GetMessageText().empty() && !buttonCancel); |
| 105 } | 104 } |
| 106 return infoBarView; | 105 return infoBarView; |
| 107 } | 106 } |
| 108 | 107 |
| 109 - (void)updateInfobarLabel:(UIView<InfoBarViewProtocol>*)view { | 108 - (void)updateInfobarLabel:(UIView<InfoBarViewProtocol>*)view { |
| 110 if (!confirmInfobarDelegate_->GetMessageText().length()) | 109 if (!_confirmInfobarDelegate->GetMessageText().length()) |
| 111 return; | 110 return; |
| 112 if (confirmInfobarDelegate_->GetLinkText().length()) { | 111 if (_confirmInfobarDelegate->GetLinkText().length()) { |
| 113 base::string16 msgLink = base::SysNSStringToUTF16( | 112 base::string16 msgLink = base::SysNSStringToUTF16([[view class] |
| 114 [[view class] stringAsLink:base::SysUTF16ToNSString( | 113 stringAsLink:base::SysUTF16ToNSString( |
| 115 confirmInfobarDelegate_->GetLinkText()) | 114 _confirmInfobarDelegate->GetLinkText()) |
| 116 tag:ConfirmInfoBarUITags::TITLE_LINK]); | 115 tag:ConfirmInfoBarUITags::TITLE_LINK]); |
| 117 base::string16 messageText = confirmInfobarDelegate_->GetMessageText(); | 116 base::string16 messageText = _confirmInfobarDelegate->GetMessageText(); |
| 118 base::ReplaceFirstSubstringAfterOffset( | 117 base::ReplaceFirstSubstringAfterOffset( |
| 119 &messageText, 0, confirmInfobarDelegate_->GetLinkText(), msgLink); | 118 &messageText, 0, _confirmInfobarDelegate->GetLinkText(), msgLink); |
| 120 | 119 |
| 121 [view addLabel:base::SysUTF16ToNSString(messageText) | 120 [view addLabel:base::SysUTF16ToNSString(messageText) |
| 122 target:self | 121 target:self |
| 123 action:@selector(infobarLinkDidPress:)]; | 122 action:@selector(infobarLinkDidPress:)]; |
| 124 } else { | 123 } else { |
| 125 NSString* label = | 124 NSString* label = |
| 126 base::SysUTF16ToNSString(confirmInfobarDelegate_->GetMessageText()); | 125 base::SysUTF16ToNSString(_confirmInfobarDelegate->GetMessageText()); |
| 127 [view addLabel:label]; | 126 [view addLabel:label]; |
| 128 } | 127 } |
| 129 } | 128 } |
| 130 | 129 |
| 131 #pragma mark - Handling of User Events | 130 #pragma mark - Handling of User Events |
| 132 | 131 |
| 133 - (void)infoBarButtonDidPress:(id)sender { | 132 - (void)infoBarButtonDidPress:(id)sender { |
| 134 // This press might have occurred after the user has already pressed a button, | 133 // This press might have occurred after the user has already pressed a button, |
| 135 // in which case the view has been detached from the delegate and this press | 134 // in which case the view has been detached from the delegate and this press |
| 136 // should be ignored. | 135 // should be ignored. |
| 137 if (!self.delegate) { | 136 if (!self.delegate) { |
| 138 return; | 137 return; |
| 139 } | 138 } |
| 140 if ([sender isKindOfClass:[UIButton class]]) { | 139 if ([sender isKindOfClass:[UIButton class]]) { |
| 141 NSUInteger tag = static_cast<UIButton*>(sender).tag; | 140 NSUInteger tag = static_cast<UIButton*>(sender).tag; |
| 142 if (tag == ConfirmInfoBarUITags::CLOSE) | 141 if (tag == ConfirmInfoBarUITags::CLOSE) |
| 143 self.delegate->InfoBarDidCancel(); | 142 self.delegate->InfoBarDidCancel(); |
| 144 else | 143 else |
| 145 self.delegate->InfoBarButtonDidPress(UITagToButton(tag)); | 144 self.delegate->InfoBarButtonDidPress(UITagToButton(tag)); |
| 146 } | 145 } |
| 147 } | 146 } |
| 148 | 147 |
| 149 // Title link was clicked. | 148 // Title link was clicked. |
| 150 - (void)infobarLinkDidPress:(NSNumber*)tag { | 149 - (void)infobarLinkDidPress:(NSNumber*)tag { |
| 151 DCHECK([tag isKindOfClass:[NSNumber class]]); | 150 DCHECK([tag isKindOfClass:[NSNumber class]]); |
| 152 if (!self.delegate) { | 151 if (!self.delegate) { |
| 153 return; | 152 return; |
| 154 } | 153 } |
| 155 if ([tag unsignedIntegerValue] == ConfirmInfoBarUITags::TITLE_LINK) { | 154 if ([tag unsignedIntegerValue] == ConfirmInfoBarUITags::TITLE_LINK) { |
| 156 confirmInfobarDelegate_->LinkClicked(NEW_FOREGROUND_TAB); | 155 _confirmInfobarDelegate->LinkClicked(NEW_FOREGROUND_TAB); |
| 157 } | 156 } |
| 158 } | 157 } |
| 159 | 158 |
| 160 @end | 159 @end |
| OLD | NEW |