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
3 // found in the LICENSE file.
5 #include "ios/chrome/browser/infobars/confirm_infobar_controller.h"
7 #include "base/mac/foundation_util.h"
8 #include "base/strings/sys_string_conversions.h"
9 #include "components/infobars/core/confirm_infobar_delegate.h"
10 #include "ios/public/provider/chrome/browser/chrome_browser_provider.h"
11 #import "ios/public/provider/chrome/browser/ui/infobar_view_delegate.h"
12 #import "ios/public/provider/chrome/browser/ui/infobar_view_protocol.h"
13 #include "ui/gfx/image/image.h"
17 // UI Tags for the infobar buttons
18 enum ConfirmInfoBarUITags { OK = 1, CANCEL, CLOSE };
20 // Converts a UI button tag to the corresponding InfoBarButton.
21 ConfirmInfoBarDelegate::InfoBarButton UITagToButton(NSUInteger tag) {
23 case ConfirmInfoBarUITags::OK:
24 return ConfirmInfoBarDelegate::BUTTON_OK;
25 case ConfirmInfoBarUITags::CANCEL:
26 case ConfirmInfoBarUITags::CLOSE:
27 return ConfirmInfoBarDelegate::BUTTON_CANCEL;
30 return ConfirmInfoBarDelegate::BUTTON_CANCEL;
36 #pragma mark - ConfirmInfoBarController
38 @interface ConfirmInfoBarController ()
40 // Action for any of the user defined buttons.
41 - (void)infoBarButtonDidPress:(id)sender;
45 @implementation ConfirmInfoBarController {
49 #pragma mark InfoBarController
51 - (base::scoped_nsobject<UIView<InfoBarViewProtocol>>)
52 viewForDelegate:(infobars::InfoBarDelegate*)delegate
54 base::scoped_nsobject<UIView<InfoBarViewProtocol>> infoBarView;
55 ConfirmInfoBarDelegate* infoBarModel = delegate->AsConfirmInfoBarDelegate();
57 ios::GetChromeBrowserProvider()->CreateInfoBarView(frame, self.delegate));
59 NSString* modelMsg = nil;
60 if (infoBarModel->GetMessageText().length())
61 modelMsg = base::SysUTF16ToNSString(infoBarModel->GetMessageText());
62 gfx::Image modelIcon = infoBarModel->GetIcon();
63 int buttons = infoBarModel->GetButtons();
64 NSString* buttonOK = nil;
65 if (buttons & ConfirmInfoBarDelegate::BUTTON_OK) {
66 buttonOK = base::SysUTF16ToNSString(
67 infoBarModel->GetButtonLabel(ConfirmInfoBarDelegate::BUTTON_OK));
69 NSString* buttonCancel = nil;
70 if (buttons & ConfirmInfoBarDelegate::BUTTON_CANCEL) {
71 buttonCancel = base::SysUTF16ToNSString(
72 infoBarModel->GetButtonLabel(ConfirmInfoBarDelegate::BUTTON_CANCEL));
75 [infoBarView addCloseButtonWithTag:ConfirmInfoBarUITags::CLOSE
77 action:@selector(infoBarButtonDidPress:)];
79 // Optional left icon.
80 if (!modelIcon.IsEmpty())
81 [infoBarView addLeftIcon:modelIcon.ToUIImage()];
85 [infoBarView addLabel:modelMsg];
87 if (buttonOK && buttonCancel) {
88 [infoBarView addButton1:buttonOK
89 tag1:ConfirmInfoBarUITags::OK
91 tag2:ConfirmInfoBarUITags::CANCEL
93 action:@selector(infoBarButtonDidPress:)];
94 } else if (buttonOK) {
95 [infoBarView addButton:buttonOK
96 tag:ConfirmInfoBarUITags::OK
98 action:@selector(infoBarButtonDidPress:)];
100 // No buttons, only message.
101 DCHECK(modelMsg && !buttonCancel);
106 #pragma mark - Handling of User Events
108 - (void)infoBarButtonDidPress:(id)sender {
109 // This press might have occurred after the user has already pressed a button,
110 // in which case the view has been detached from the delegate and this press
111 // should be ignored.
112 if (!self.delegate) {
115 if ([sender isKindOfClass:[UIButton class]]) {
116 NSUInteger tag = static_cast<UIButton*>(sender).tag;
117 if (tag == ConfirmInfoBarUITags::CLOSE)
118 self.delegate->InfoBarDidCancel();
120 self.delegate->InfoBarButtonDidPress(UITagToButton(tag));