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;
22 @implementation TranslateMessageInfoBarController
24 - (base::scoped_nsobject<UIView<InfoBarViewProtocol>>)
25 viewForDelegate:(infobars::InfoBarDelegate*)delegate
27 base::scoped_nsobject<UIView<InfoBarViewProtocol>> infoBarView;
28 translate::TranslateInfoBarDelegate* translateInfoBarDelegate =
29 delegate->AsTranslateInfoBarDelegate();
31 ios::GetChromeBrowserProvider()->CreateInfoBarView(frame, self.delegate));
33 gfx::Image icon = translateInfoBarDelegate->GetIcon();
35 [infoBarView addLeftIcon:icon.ToUIImage()];
37 [infoBarView addLabel:base::SysUTF16ToNSString(
38 translateInfoBarDelegate->GetMessageInfoBarText())];
40 [infoBarView addCloseButtonWithTag:TranslateInfoBarIOSTag::CLOSE
42 action:@selector(infoBarButtonDidPress:)];
44 base::string16 buttonText(
45 translateInfoBarDelegate->GetMessageInfoBarButtonText());
46 if (!buttonText.empty()) {
47 [infoBarView addButton:base::SysUTF16ToNSString(buttonText)
48 tag:TranslateInfoBarIOSTag::MESSAGE
50 action:@selector(infoBarButtonDidPress:)];
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
64 if ([sender isKindOfClass:[UIButton class]]) {
65 NSUInteger buttonId = static_cast<UIButton*>(sender).tag;
66 if (buttonId == TranslateInfoBarIOSTag::CLOSE) {
67 self.delegate->InfoBarDidCancel();
69 DCHECK(buttonId == TranslateInfoBarIOSTag::MESSAGE);
70 self.delegate->InfoBarButtonDidPress(buttonId);