Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / components / translate / core / browser / options_menu_model.cc
blob4bb07c2d98f7c4ffc7dabab0f0c776f50d34c809
1 // Copyright 2014 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 "components/translate/core/browser/options_menu_model.h"
7 #include "base/metrics/histogram.h"
8 #include "components/translate/core/browser/translate_driver.h"
9 #include "components/translate/core/browser/translate_infobar_delegate.h"
10 #include "grit/components_strings.h"
11 #include "ui/base/l10n/l10n_util.h"
13 namespace translate {
15 namespace {
17 const char kAboutGoogleTranslateURL[] =
18 #if defined(OS_CHROMEOS)
19 "https://support.google.com/chromebook/?p=ib_translation_bar";
20 #else
21 "https://support.google.com/chrome/?p=ib_translation_bar";
22 #endif
24 } // namespace
26 OptionsMenuModel::OptionsMenuModel(TranslateInfoBarDelegate* translate_delegate)
27 : ui::SimpleMenuModel(this),
28 translate_infobar_delegate_(translate_delegate) {
29 // |translate_delegate| must already be owned.
30 DCHECK(translate_infobar_delegate_->GetTranslateDriver());
32 base::string16 original_language = translate_delegate->language_name_at(
33 translate_delegate->original_language_index());
34 base::string16 target_language = translate_delegate->language_name_at(
35 translate_delegate->target_language_index());
37 bool autodetermined_source_language =
38 translate_delegate->original_language_index() ==
39 TranslateInfoBarDelegate::kNoIndex;
41 // Populate the menu.
42 // Incognito mode does not get any preferences related items.
43 if (!translate_delegate->is_off_the_record()) {
44 if (!autodetermined_source_language) {
45 AddCheckItem(ALWAYS_TRANSLATE,
46 l10n_util::GetStringFUTF16(IDS_TRANSLATE_INFOBAR_OPTIONS_ALWAYS,
47 original_language, target_language));
48 AddCheckItem(NEVER_TRANSLATE_LANGUAGE,
49 l10n_util::GetStringFUTF16(
50 IDS_TRANSLATE_INFOBAR_OPTIONS_NEVER_TRANSLATE_LANG,
51 original_language));
53 AddCheckItem(NEVER_TRANSLATE_SITE,
54 l10n_util::GetStringUTF16(
55 IDS_TRANSLATE_INFOBAR_OPTIONS_NEVER_TRANSLATE_SITE));
56 AddSeparator(ui::NORMAL_SEPARATOR);
58 if (!autodetermined_source_language) {
59 AddItem(REPORT_BAD_DETECTION,
60 l10n_util::GetStringFUTF16(IDS_TRANSLATE_INFOBAR_OPTIONS_REPORT_ERROR,
61 original_language));
63 AddItemWithStringId(ABOUT_TRANSLATE, IDS_TRANSLATE_INFOBAR_OPTIONS_ABOUT);
66 OptionsMenuModel::~OptionsMenuModel() {
69 bool OptionsMenuModel::IsCommandIdChecked(int command_id) const {
70 switch (command_id) {
71 case NEVER_TRANSLATE_LANGUAGE:
72 return !translate_infobar_delegate_->IsTranslatableLanguageByPrefs();
74 case NEVER_TRANSLATE_SITE:
75 return translate_infobar_delegate_->IsSiteBlacklisted();
77 case ALWAYS_TRANSLATE:
78 return translate_infobar_delegate_->ShouldAlwaysTranslate();
80 default:
81 NOTREACHED() << "Invalid command_id from menu";
82 break;
84 return false;
87 bool OptionsMenuModel::IsCommandIdEnabled(int command_id) const {
88 switch (command_id) {
89 case NEVER_TRANSLATE_LANGUAGE:
90 case NEVER_TRANSLATE_SITE:
91 return !translate_infobar_delegate_->ShouldAlwaysTranslate();
93 case ALWAYS_TRANSLATE:
94 return (translate_infobar_delegate_->IsTranslatableLanguageByPrefs() &&
95 !translate_infobar_delegate_->IsSiteBlacklisted());
97 default:
98 break;
100 return true;
103 bool OptionsMenuModel::GetAcceleratorForCommandId(
104 int command_id,
105 ui::Accelerator* accelerator) {
106 return false;
109 void OptionsMenuModel::ExecuteCommand(int command_id, int event_flags) {
110 switch (command_id) {
111 case NEVER_TRANSLATE_LANGUAGE:
112 translate_infobar_delegate_->ToggleTranslatableLanguageByPrefs();
113 break;
115 case NEVER_TRANSLATE_SITE:
116 translate_infobar_delegate_->ToggleSiteBlacklist();
117 break;
119 case ALWAYS_TRANSLATE:
120 translate_infobar_delegate_->ToggleAlwaysTranslate();
121 break;
123 case REPORT_BAD_DETECTION:
124 translate_infobar_delegate_->ReportLanguageDetectionError();
125 break;
127 case ABOUT_TRANSLATE: {
128 TranslateDriver* translate_driver =
129 translate_infobar_delegate_->GetTranslateDriver();
130 if (translate_driver)
131 translate_driver->OpenUrlInNewTab(GURL(kAboutGoogleTranslateURL));
132 break;
135 default:
136 NOTREACHED() << "Invalid command id from menu.";
137 break;
141 } // namespace translate