Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / ios / chrome / browser / infobars / confirm_infobar_controller.mm
blob74e54a6316a6c84225e6f0ec6a80134e30ba83e9
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 - (void)layoutForDelegate:(infobars::InfoBarDelegate*)delegate
52                     frame:(CGRect)frame {
53   ConfirmInfoBarDelegate* infoBarModel = delegate->AsConfirmInfoBarDelegate();
54   DCHECK(!infoBarView_);
55   infoBarView_.reset([ios::GetChromeBrowserProvider()->CreateInfoBarView()
56       initWithFrame:frame
57            delegate:delegate_]);
59   // Model data.
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));
69   }
70   NSString* buttonCancel = nil;
71   if (buttons & ConfirmInfoBarDelegate::BUTTON_CANCEL) {
72     buttonCancel = base::SysUTF16ToNSString(
73         infoBarModel->GetButtonLabel(ConfirmInfoBarDelegate::BUTTON_CANCEL));
74   }
76   [infoBarView_ addCloseButtonWithTag:ConfirmInfoBarUITags::CLOSE
77                                target:self
78                                action:@selector(infoBarButtonDidPress:)];
80   // Optional left icon.
81   if (!modelIcon.IsEmpty())
82     [infoBarView_ addLeftIcon:modelIcon.ToUIImage()];
84   // Optional message.
85   if (modelMsg)
86     [infoBarView_ addLabel:modelMsg];
88   if (buttonOK && buttonCancel) {
89     [infoBarView_ addButton1:buttonOK
90                         tag1:ConfirmInfoBarUITags::OK
91                      button2:buttonCancel
92                         tag2:ConfirmInfoBarUITags::CANCEL
93                       target:self
94                       action:@selector(infoBarButtonDidPress:)];
95   } else if (buttonOK) {
96     [infoBarView_ addButton:buttonOK
97                         tag:ConfirmInfoBarUITags::OK
98                      target:self
99                      action:@selector(infoBarButtonDidPress:)];
100   } else {
101     // No buttons, only message.
102     DCHECK(modelMsg && !buttonCancel);
103   }
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 (!delegate_) {
113     return;
114   }
115   if ([sender isKindOfClass:[UIButton class]]) {
116     NSUInteger tag = static_cast<UIButton*>(sender).tag;
117     if (tag == ConfirmInfoBarUITags::CLOSE)
118       delegate_->InfoBarDidCancel();
119     else
120       delegate_->InfoBarButtonDidPress(UITagToButton(tag));
121   }
124 @end