NaCl: Update revision in DEPS, r12770 -> r12773
[chromium-blink-merge.git] / chrome / browser / translate / options_menu_model.cc
blobba649a9ada7dd5c6c3a7ffe0cfb87e9e589a09a9
1 // Copyright (c) 2012 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 "chrome/browser/translate/options_menu_model.h"
7 #include "base/metrics/histogram.h"
8 #include "chrome/app/chrome_command_ids.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/translate/translate_infobar_delegate.h"
11 #include "chrome/common/url_constants.h"
12 #include "content/public/browser/navigation_controller.h"
13 #include "content/public/browser/navigation_entry.h"
14 #include "content/public/browser/web_contents.h"
15 #include "grit/generated_resources.h"
16 #include "grit/locale_settings.h"
17 #include "ui/base/l10n/l10n_util.h"
19 using content::NavigationEntry;
20 using content::OpenURLParams;
21 using content::Referrer;
22 using content::WebContents;
24 OptionsMenuModel::OptionsMenuModel(
25 TranslateInfoBarDelegate* translate_delegate)
26 : ui::SimpleMenuModel(this),
27 translate_infobar_delegate_(translate_delegate) {
28 // |translate_delegate| must already be owned.
29 DCHECK(translate_infobar_delegate_->web_contents());
31 base::string16 original_language = translate_delegate->language_name_at(
32 translate_delegate->original_language_index());
33 base::string16 target_language = translate_delegate->language_name_at(
34 translate_delegate->target_language_index());
36 bool autodetermined_source_language =
37 translate_delegate->original_language_index() ==
38 TranslateInfoBarDelegate::kNoIndex;
40 // Populate the menu.
41 // Incognito mode does not get any preferences related items.
42 if (!translate_delegate->web_contents()->GetBrowserContext()->
43 IsOffTheRecord()) {
44 if (!autodetermined_source_language) {
45 AddCheckItem(IDC_TRANSLATE_OPTIONS_ALWAYS,
46 l10n_util::GetStringFUTF16(IDS_TRANSLATE_INFOBAR_OPTIONS_ALWAYS,
47 original_language, target_language));
48 AddCheckItem(IDC_TRANSLATE_OPTIONS_NEVER_TRANSLATE_LANG,
49 l10n_util::GetStringFUTF16(
50 IDS_TRANSLATE_INFOBAR_OPTIONS_NEVER_TRANSLATE_LANG,
51 original_language));
53 AddCheckItem(IDC_TRANSLATE_OPTIONS_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(IDC_TRANSLATE_REPORT_BAD_LANGUAGE_DETECTION,
60 l10n_util::GetStringFUTF16(IDS_TRANSLATE_INFOBAR_OPTIONS_REPORT_ERROR,
61 original_language));
63 AddItemWithStringId(IDC_TRANSLATE_OPTIONS_ABOUT,
64 IDS_TRANSLATE_INFOBAR_OPTIONS_ABOUT);
67 OptionsMenuModel::~OptionsMenuModel() {
70 bool OptionsMenuModel::IsCommandIdChecked(int command_id) const {
71 switch (command_id) {
72 case IDC_TRANSLATE_OPTIONS_NEVER_TRANSLATE_LANG:
73 return !translate_infobar_delegate_->IsTranslatableLanguageByPrefs();
75 case IDC_TRANSLATE_OPTIONS_NEVER_TRANSLATE_SITE:
76 return translate_infobar_delegate_->IsSiteBlacklisted();
78 case IDC_TRANSLATE_OPTIONS_ALWAYS:
79 return translate_infobar_delegate_->ShouldAlwaysTranslate();
81 default:
82 NOTREACHED() << "Invalid command_id from menu";
83 break;
85 return false;
88 bool OptionsMenuModel::IsCommandIdEnabled(int command_id) const {
89 switch (command_id) {
90 case IDC_TRANSLATE_OPTIONS_NEVER_TRANSLATE_LANG :
91 case IDC_TRANSLATE_OPTIONS_NEVER_TRANSLATE_SITE :
92 return !translate_infobar_delegate_->ShouldAlwaysTranslate();
94 case IDC_TRANSLATE_OPTIONS_ALWAYS :
95 return (translate_infobar_delegate_->IsTranslatableLanguageByPrefs() &&
96 !translate_infobar_delegate_->IsSiteBlacklisted());
98 default:
99 break;
101 return true;
104 bool OptionsMenuModel::GetAcceleratorForCommandId(
105 int command_id, ui::Accelerator* accelerator) {
106 return false;
109 void OptionsMenuModel::ExecuteCommand(int command_id, int event_flags) {
110 switch (command_id) {
111 case IDC_TRANSLATE_OPTIONS_NEVER_TRANSLATE_LANG:
112 translate_infobar_delegate_->ToggleTranslatableLanguageByPrefs();
113 break;
115 case IDC_TRANSLATE_OPTIONS_NEVER_TRANSLATE_SITE:
116 translate_infobar_delegate_->ToggleSiteBlacklist();
117 break;
119 case IDC_TRANSLATE_OPTIONS_ALWAYS:
120 translate_infobar_delegate_->ToggleAlwaysTranslate();
121 break;
123 case IDC_TRANSLATE_REPORT_BAD_LANGUAGE_DETECTION:
124 translate_infobar_delegate_->ReportLanguageDetectionError();
125 break;
127 case IDC_TRANSLATE_OPTIONS_ABOUT: {
128 WebContents* web_contents = translate_infobar_delegate_->web_contents();
129 if (web_contents) {
130 OpenURLParams params(
131 GURL(chrome::kAboutGoogleTranslateURL), Referrer(),
132 NEW_FOREGROUND_TAB, content::PAGE_TRANSITION_LINK, false);
133 web_contents->OpenURL(params);
135 break;
138 default:
139 NOTREACHED() << "Invalid command id from menu.";
140 break;