Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ios / chrome / browser / translate / translate_message_infobar_controller.mm
blobc0b2c4abedcebefb9e056563011d48bf16747f00
1 // Copyright 2013 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/translate/translate_message_infobar_controller.h"
7 #include "base/strings/sys_string_conversions.h"
8 #include "components/translate/core/browser/translate_infobar_delegate.h"
9 #include "ios/chrome/browser/translate/translate_infobar_tags.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 @interface TranslateMessageInfoBarController ()
17 // Action for any of the user defined buttons.
18 - (void)infoBarButtonDidPress:(id)sender;
20 @end
22 @implementation TranslateMessageInfoBarController
24 - (base::scoped_nsobject<UIView<InfoBarViewProtocol>>)
25     viewForDelegate:(infobars::InfoBarDelegate*)delegate
26               frame:(CGRect)frame {
27   base::scoped_nsobject<UIView<InfoBarViewProtocol>> infoBarView;
28   translate::TranslateInfoBarDelegate* translateInfoBarDelegate =
29       delegate->AsTranslateInfoBarDelegate();
30   infoBarView.reset(
31       ios::GetChromeBrowserProvider()->CreateInfoBarView(frame, self.delegate));
32   // Icon
33   gfx::Image icon = translateInfoBarDelegate->GetIcon();
34   if (!icon.IsEmpty())
35     [infoBarView addLeftIcon:icon.ToUIImage()];
36   // Text.
37   [infoBarView addLabel:base::SysUTF16ToNSString(
38                             translateInfoBarDelegate->GetMessageInfoBarText())];
39   // Close button.
40   [infoBarView addCloseButtonWithTag:TranslateInfoBarIOSTag::CLOSE
41                               target:self
42                               action:@selector(infoBarButtonDidPress:)];
43   // Other button.
44   base::string16 buttonText(
45       translateInfoBarDelegate->GetMessageInfoBarButtonText());
46   if (!buttonText.empty()) {
47     [infoBarView addButton:base::SysUTF16ToNSString(buttonText)
48                        tag:TranslateInfoBarIOSTag::MESSAGE
49                     target:self
50                     action:@selector(infoBarButtonDidPress:)];
51   }
52   return infoBarView;
55 #pragma mark - Handling of User Events
57 - (void)infoBarButtonDidPress:(id)sender {
58   // This press might have occurred after the user has already pressed a button,
59   // in which case the view has been detached from the delegate and this press
60   // should be ignored.
61   if (!self.delegate) {
62     return;
63   }
64   if ([sender isKindOfClass:[UIButton class]]) {
65     NSUInteger buttonId = static_cast<UIButton*>(sender).tag;
66     if (buttonId == TranslateInfoBarIOSTag::CLOSE) {
67       self.delegate->InfoBarDidCancel();
68     } else {
69       DCHECK(buttonId == TranslateInfoBarIOSTag::MESSAGE);
70       self.delegate->InfoBarButtonDidPress(buttonId);
71     }
72   }
75 @end