Give names to all utility processes.
[chromium-blink-merge.git] / ios / chrome / browser / translate / after_translate_infobar_controller.mm
blobc6c5c5d1455b7203f1a575577b1926c83264cea2
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/after_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 #include "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 AfterTranslateInfoBarController () {
20   __weak translate::TranslateInfoBarDelegate* _translateInfoBarDelegate;
23 // Action for any of the user defined buttons.
24 - (void)infoBarButtonDidPress:(id)sender;
26 @end
28 @implementation AfterTranslateInfoBarController
30 #pragma mark -
31 #pragma mark InfoBarControllerProtocol
33 - (void)layoutForDelegate:(infobars::InfoBarDelegate*)delegate
34                     frame:(CGRect)frame {
35   _translateInfoBarDelegate = delegate->AsTranslateInfoBarDelegate();
36   DCHECK(_translateInfoBarDelegate);
37   DCHECK(!infoBarView_);
38   infoBarView_.reset([ios::GetChromeBrowserProvider()->CreateInfoBarView()
39       initWithFrame:frame
40            delegate:delegate_]);
41   // Icon
42   gfx::Image icon = _translateInfoBarDelegate->GetIcon();
43   if (!icon.IsEmpty())
44     [infoBarView_ addLeftIcon:icon.ToUIImage()];
45   // Main text.
46   const bool autodeterminedSourceLanguage =
47       _translateInfoBarDelegate->original_language_index() ==
48       translate::TranslateInfoBarDelegate::kNoIndex;
49   bool swappedLanguageButtons;
50   std::vector<base::string16> strings;
51   translate::TranslateInfoBarDelegate::GetAfterTranslateStrings(
52       &strings, &swappedLanguageButtons, autodeterminedSourceLanguage);
53   DCHECK_EQ(autodeterminedSourceLanguage ? 2U : 3U, strings.size());
54   NSString* label1 = base::SysUTF16ToNSString(strings[0]);
55   NSString* label2 = base::SysUTF16ToNSString(strings[1]);
56   NSString* label3 = autodeterminedSourceLanguage
57                          ? @""
58                          : base::SysUTF16ToNSString(strings[2]);
59   base::string16 stdOriginal = _translateInfoBarDelegate->language_name_at(
60       _translateInfoBarDelegate->original_language_index());
61   NSString* original = base::SysUTF16ToNSString(stdOriginal);
62   NSString* target =
63       base::SysUTF16ToNSString(_translateInfoBarDelegate->language_name_at(
64           _translateInfoBarDelegate->target_language_index()));
65   base::scoped_nsobject<NSString> label(
66       [[NSString alloc] initWithFormat:@"%@ %@ %@%@ %@.", label1, original,
67                                        label2, label3, target]);
68   [infoBarView_ addLabel:label];
69   // Close button.
70   [infoBarView_ addCloseButtonWithTag:TranslateInfoBarIOSTag::CLOSE
71                                target:self
72                                action:@selector(infoBarButtonDidPress:)];
73   // Other buttons.
74   NSString* buttonRevert = l10n_util::GetNSString(IDS_TRANSLATE_INFOBAR_REVERT);
75   NSString* buttonOptions = base::SysUTF16ToNSString(
76       ios::GetChromeBrowserProvider()->GetStringProvider()->GetDoneString());
77   [infoBarView_ addButton1:buttonOptions
78                       tag1:TranslateInfoBarIOSTag::AFTER_DONE
79                    button2:buttonRevert
80                       tag2:TranslateInfoBarIOSTag::AFTER_REVERT
81                     target:self
82                     action:@selector(infoBarButtonDidPress:)];
83   // Always translate switch.
84   if (_translateInfoBarDelegate->ShouldShowAlwaysTranslateShortcut()) {
85     base::string16 alwaysTranslate = l10n_util::GetStringFUTF16(
86         IDS_TRANSLATE_INFOBAR_ALWAYS_TRANSLATE, stdOriginal);
87     const BOOL switchValue = _translateInfoBarDelegate->ShouldAlwaysTranslate();
88     [infoBarView_
89         addSwitchWithLabel:base::SysUTF16ToNSString(alwaysTranslate)
90                       isOn:switchValue
91                        tag:TranslateInfoBarIOSTag::ALWAYS_TRANSLATE_SWITCH
92                     target:self
93                     action:@selector(infoBarSwitchDidPress:)];
94   }
97 #pragma mark - Handling of User Events
99 - (void)infoBarButtonDidPress:(id)sender {
100   // This press might have occurred after the user has already pressed a button,
101   // in which case the view has been detached from the delegate and this press
102   // should be ignored.
103   if (!delegate_) {
104     return;
105   }
106   if ([sender isKindOfClass:[UIButton class]]) {
107     NSUInteger buttonId = static_cast<UIButton*>(sender).tag;
108     if (buttonId == TranslateInfoBarIOSTag::CLOSE) {
109       delegate_->InfoBarDidCancel();
110     } else {
111       DCHECK(buttonId == TranslateInfoBarIOSTag::AFTER_REVERT ||
112              buttonId == TranslateInfoBarIOSTag::AFTER_DONE);
113       delegate_->InfoBarButtonDidPress(buttonId);
114     }
115   }
118 - (void)infoBarSwitchDidPress:(id)sender {
119   DCHECK_EQ(TranslateInfoBarIOSTag::ALWAYS_TRANSLATE_SWITCH, [sender tag]);
120   DCHECK([sender respondsToSelector:@selector(isOn)]);
121   if (_translateInfoBarDelegate->ShouldAlwaysTranslate() != [sender isOn])
122     _translateInfoBarDelegate->ToggleAlwaysTranslate();
125 @end