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 - (void)layoutForDelegate:(infobars::InfoBarDelegate*)delegate
53 ConfirmInfoBarDelegate* infoBarModel = delegate->AsConfirmInfoBarDelegate();
54 DCHECK(!infoBarView_);
55 infoBarView_.reset([ios::GetChromeBrowserProvider()->CreateInfoBarView()
60 NSString* modelMsg = nil;
61 if (infoBarModel->GetMessageText().length())
62 modelMsg = base::SysUTF16ToNSString(infoBarModel->GetMessageText());
63 gfx::Image modelIcon = infoBarModel->GetIcon();
64 int buttons = infoBarModel->GetButtons();
65 NSString* buttonOK = nil;
66 if (buttons & ConfirmInfoBarDelegate::BUTTON_OK) {
67 buttonOK = base::SysUTF16ToNSString(
68 infoBarModel->GetButtonLabel(ConfirmInfoBarDelegate::BUTTON_OK));
70 NSString* buttonCancel = nil;
71 if (buttons & ConfirmInfoBarDelegate::BUTTON_CANCEL) {
72 buttonCancel = base::SysUTF16ToNSString(
73 infoBarModel->GetButtonLabel(ConfirmInfoBarDelegate::BUTTON_CANCEL));
76 [infoBarView_ addCloseButtonWithTag:ConfirmInfoBarUITags::CLOSE
78 action:@selector(infoBarButtonDidPress:)];
80 // Optional left icon.
81 if (!modelIcon.IsEmpty())
82 [infoBarView_ addLeftIcon:modelIcon.ToUIImage()];
86 [infoBarView_ addLabel:modelMsg];
88 if (buttonOK && buttonCancel) {
89 [infoBarView_ addButton1:buttonOK
90 tag1:ConfirmInfoBarUITags::OK
92 tag2:ConfirmInfoBarUITags::CANCEL
94 action:@selector(infoBarButtonDidPress:)];
95 } else if (buttonOK) {
96 [infoBarView_ addButton:buttonOK
97 tag:ConfirmInfoBarUITags::OK
99 action:@selector(infoBarButtonDidPress:)];
101 // No buttons, only message.
102 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.
115 if ([sender isKindOfClass:[UIButton class]]) {
116 NSUInteger tag = static_cast<UIButton*>(sender).tag;
117 if (tag == ConfirmInfoBarUITags::CLOSE)
118 delegate_->InfoBarDidCancel();
120 delegate_->InfoBarButtonDidPress(UITagToButton(tag));