Fixes for Android GN build input/outputs
[chromium-blink-merge.git] / ios / chrome / browser / infobars / confirm_infobar_controller.mm
blob172787636d9a6ef9008bcd19ec8544d0cf334873
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"
15 namespace {
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) {
22   switch (tag) {
23     case ConfirmInfoBarUITags::OK:
24       return ConfirmInfoBarDelegate::BUTTON_OK;
25     case ConfirmInfoBarUITags::CANCEL:
26     case ConfirmInfoBarUITags::CLOSE:
27       return ConfirmInfoBarDelegate::BUTTON_CANCEL;
28     default:
29       NOTREACHED();
30       return ConfirmInfoBarDelegate::BUTTON_CANCEL;
31   }
34 }  // namespace
36 #pragma mark - ConfirmInfoBarController
38 @interface ConfirmInfoBarController ()
40 // Action for any of the user defined buttons.
41 - (void)infoBarButtonDidPress:(id)sender;
43 @end
45 @implementation ConfirmInfoBarController {
48 #pragma mark -
49 #pragma mark InfoBarController
51 - (base::scoped_nsobject<UIView<InfoBarViewProtocol>>)
52     viewForDelegate:(infobars::InfoBarDelegate*)delegate
53               frame:(CGRect)frame {
54   base::scoped_nsobject<UIView<InfoBarViewProtocol>> infoBarView;
55   ConfirmInfoBarDelegate* infoBarModel = delegate->AsConfirmInfoBarDelegate();
56   infoBarView.reset(
57       ios::GetChromeBrowserProvider()->CreateInfoBarView(frame, self.delegate));
58   // Model data.
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));
68   }
69   NSString* buttonCancel = nil;
70   if (buttons & ConfirmInfoBarDelegate::BUTTON_CANCEL) {
71     buttonCancel = base::SysUTF16ToNSString(
72         infoBarModel->GetButtonLabel(ConfirmInfoBarDelegate::BUTTON_CANCEL));
73   }
75   [infoBarView addCloseButtonWithTag:ConfirmInfoBarUITags::CLOSE
76                               target:self
77                               action:@selector(infoBarButtonDidPress:)];
79   // Optional left icon.
80   if (!modelIcon.IsEmpty())
81     [infoBarView addLeftIcon:modelIcon.ToUIImage()];
83   // Optional message.
84   if (modelMsg)
85     [infoBarView addLabel:modelMsg];
87   if (buttonOK && buttonCancel) {
88     [infoBarView addButton1:buttonOK
89                        tag1:ConfirmInfoBarUITags::OK
90                     button2:buttonCancel
91                        tag2:ConfirmInfoBarUITags::CANCEL
92                      target:self
93                      action:@selector(infoBarButtonDidPress:)];
94   } else if (buttonOK) {
95     [infoBarView addButton:buttonOK
96                        tag:ConfirmInfoBarUITags::OK
97                     target:self
98                     action:@selector(infoBarButtonDidPress:)];
99   } else {
100     // No buttons, only message.
101     DCHECK(modelMsg && !buttonCancel);
102   }
103   return infoBarView;
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) {
113     return;
114   }
115   if ([sender isKindOfClass:[UIButton class]]) {
116     NSUInteger tag = static_cast<UIButton*>(sender).tag;
117     if (tag == ConfirmInfoBarUITags::CLOSE)
118       self.delegate->InfoBarDidCancel();
119     else
120       self.delegate->InfoBarButtonDidPress(UITagToButton(tag));
121   }
124 @end