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/never_translate_infobar_controller.h"
7 #include "base/strings/sys_string_conversions.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "components/translate/core/browser/translate_infobar_delegate.h"
10 #include "grit/components_strings.h"
11 #include "ios/chrome/browser/translate/translate_infobar_tags.h"
12 #include "ios/public/provider/chrome/browser/chrome_browser_provider.h"
13 #import "ios/public/provider/chrome/browser/string_provider.h"
14 #import "ios/public/provider/chrome/browser/ui/infobar_view_delegate.h"
15 #import "ios/public/provider/chrome/browser/ui/infobar_view_protocol.h"
16 #include "ui/base/l10n/l10n_util.h"
17 #include "ui/gfx/image/image.h"
19 @interface NeverTranslateInfoBarController ()
21 // Action for any of the user defined buttons.
22 - (void)infoBarButtonDidPress:(id)sender;
26 @implementation NeverTranslateInfoBarController
29 #pragma mark InfoBarControllerProtocol
31 - (base::scoped_nsobject<UIView<InfoBarViewProtocol>>)
32 viewForDelegate:(infobars::InfoBarDelegate*)delegate
34 base::scoped_nsobject<UIView<InfoBarViewProtocol>> infoBarView;
35 translate::TranslateInfoBarDelegate* translateInfoBarDelegate =
36 delegate->AsTranslateInfoBarDelegate();
37 ios::ChromeBrowserProvider* provider = ios::GetChromeBrowserProvider();
39 ios::GetChromeBrowserProvider()->CreateInfoBarView(frame, self.delegate));
41 gfx::Image icon = translateInfoBarDelegate->GetIcon();
43 [infoBarView addLeftIcon:icon.ToUIImage()];
45 base::string16 originalLanguage = translateInfoBarDelegate->language_name_at(
46 translateInfoBarDelegate->original_language_index());
47 [infoBarView addLabel:l10n_util::GetNSStringF(
48 IDS_TRANSLATE_INFOBAR_NEVER_MESSAGE_IOS,
49 provider->GetStringProvider()->GetProductName(),
52 [infoBarView addCloseButtonWithTag:TranslateInfoBarIOSTag::CLOSE
54 action:@selector(infoBarButtonDidPress:)];
56 NSString* buttonLanguage = l10n_util::GetNSStringF(
57 IDS_TRANSLATE_INFOBAR_NEVER_TRANSLATE, originalLanguage);
58 NSString* buttonSite = l10n_util::GetNSString(
59 IDS_TRANSLATE_INFOBAR_OPTIONS_NEVER_TRANSLATE_SITE);
60 [infoBarView addButton1:buttonLanguage
61 tag1:TranslateInfoBarIOSTag::DENY_LANGUAGE
63 tag2:TranslateInfoBarIOSTag::DENY_WEBSITE
65 action:@selector(infoBarButtonDidPress:)];
69 #pragma mark - Handling of User Events
71 - (void)infoBarButtonDidPress:(id)sender {
72 // This press might have occurred after the user has already pressed a button,
73 // in which case the view has been detached from the delegate and this press
78 if ([sender isKindOfClass:[UIButton class]]) {
79 NSUInteger buttonId = static_cast<UIButton*>(sender).tag;
80 if (buttonId == TranslateInfoBarIOSTag::CLOSE) {
81 self.delegate->InfoBarDidCancel();
83 DCHECK(buttonId == TranslateInfoBarIOSTag::DENY_LANGUAGE ||
84 buttonId == TranslateInfoBarIOSTag::DENY_WEBSITE);
85 self.delegate->InfoBarButtonDidPress(buttonId);