Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / chrome / browser / ui / webui / translate_internals / translate_internals_handler.cc
blob1c73f901c3b069cdfe3670dbd044604d54bd8ad7
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 "chrome/browser/ui/webui/translate_internals/translate_internals_handler.h"
7 #include <map>
8 #include <vector>
10 #include "base/bind.h"
11 #include "base/bind_helpers.h"
12 #include "base/prefs/pref_service.h"
13 #include "base/values.h"
14 #include "chrome/browser/chrome_notification_types.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/translate/chrome_translate_client.h"
17 #include "chrome/browser/translate/translate_service.h"
18 #include "chrome/common/pref_names.h"
19 #include "components/translate/core/browser/translate_download_manager.h"
20 #include "components/translate/core/browser/translate_error_details.h"
21 #include "components/translate/core/browser/translate_event_details.h"
22 #include "components/translate/core/browser/translate_prefs.h"
23 #include "components/translate/core/common/language_detection_details.h"
24 #include "components/translate/core/common/translate_pref_names.h"
25 #include "content/public/browser/notification_details.h"
26 #include "content/public/browser/notification_service.h"
27 #include "content/public/browser/notification_source.h"
28 #include "content/public/browser/notification_types.h"
29 #include "content/public/browser/web_contents.h"
30 #include "content/public/browser/web_ui.h"
32 TranslateInternalsHandler::TranslateInternalsHandler() {
33 notification_registrar_.Add(this,
34 chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED,
35 content::NotificationService::AllSources());
37 error_subscription_ =
38 translate::TranslateManager::RegisterTranslateErrorCallback(
39 base::Bind(&TranslateInternalsHandler::OnTranslateError,
40 base::Unretained(this)));
42 translate::TranslateLanguageList* language_list =
43 translate::TranslateDownloadManager::GetInstance()->language_list();
44 if (!language_list) {
45 NOTREACHED();
46 return;
49 event_subscription_ = language_list->RegisterEventCallback(base::Bind(
50 &TranslateInternalsHandler::OnTranslateEvent, base::Unretained(this)));
53 TranslateInternalsHandler::~TranslateInternalsHandler() {
54 // |event_subscription_| and |error_subscription_| are deleted automatically
55 // and un-register the callbacks automatically.
58 void TranslateInternalsHandler::RegisterMessages() {
59 web_ui()->RegisterMessageCallback("removePrefItem", base::Bind(
60 &TranslateInternalsHandler::OnRemovePrefItem, base::Unretained(this)));
61 web_ui()->RegisterMessageCallback("requestInfo", base::Bind(
62 &TranslateInternalsHandler::OnRequestInfo, base::Unretained(this)));
65 void TranslateInternalsHandler::Observe(
66 int type,
67 const content::NotificationSource& source,
68 const content::NotificationDetails& details) {
69 DCHECK_EQ(chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED, type);
70 const translate::LanguageDetectionDetails* language_detection_details =
71 content::Details<const translate::LanguageDetectionDetails>(details)
72 .ptr();
73 content::WebContents* web_contents =
74 content::Source<content::WebContents>(source).ptr();
76 if (web_contents->GetBrowserContext()->IsOffTheRecord() ||
77 !TranslateService::IsTranslatableURL(language_detection_details->url)) {
78 return;
81 base::DictionaryValue dict;
82 dict.Set(
83 "time",
84 new base::FundamentalValue(language_detection_details->time.ToJsTime()));
85 dict.Set("url",
86 new base::StringValue(language_detection_details->url.spec()));
87 dict.Set("content_language",
88 new base::StringValue(language_detection_details->content_language));
89 dict.Set("cld_language",
90 new base::StringValue(language_detection_details->cld_language));
91 dict.Set(
92 "is_cld_reliable",
93 new base::FundamentalValue(language_detection_details->is_cld_reliable));
94 dict.Set(
95 "has_notranslate",
96 new base::FundamentalValue(language_detection_details->has_notranslate));
97 dict.Set(
98 "html_root_language",
99 new base::StringValue(language_detection_details->html_root_language));
100 dict.Set("adopted_language",
101 new base::StringValue(language_detection_details->adopted_language));
102 dict.Set("content",
103 new base::StringValue(language_detection_details->contents));
104 SendMessageToJs("languageDetectionInfoAdded", dict);
107 void TranslateInternalsHandler::OnTranslateError(
108 const translate::TranslateErrorDetails& details) {
109 base::DictionaryValue dict;
110 dict.Set("time",
111 new base::FundamentalValue(details.time.ToJsTime()));
112 dict.Set("url",
113 new base::StringValue(details.url.spec()));
114 dict.Set("error",
115 new base::FundamentalValue(details.error));
116 SendMessageToJs("translateErrorDetailsAdded", dict);
119 void TranslateInternalsHandler::OnTranslateEvent(
120 const translate::TranslateEventDetails& details) {
121 base::DictionaryValue dict;
122 dict.Set("time", new base::FundamentalValue(details.time.ToJsTime()));
123 dict.Set("filename", new base::StringValue(details.filename));
124 dict.Set("line", new base::FundamentalValue(details.line));
125 dict.Set("message", new base::StringValue(details.message));
126 SendMessageToJs("translateEventDetailsAdded", dict);
129 void TranslateInternalsHandler::OnRemovePrefItem(const base::ListValue* args) {
130 content::WebContents* web_contents = web_ui()->GetWebContents();
131 Profile* profile =
132 Profile::FromBrowserContext(web_contents->GetBrowserContext());
133 PrefService* prefs = profile->GetOriginalProfile()->GetPrefs();
134 scoped_ptr<translate::TranslatePrefs> translate_prefs(
135 ChromeTranslateClient::CreateTranslatePrefs(prefs));
137 std::string pref_name;
138 if (!args->GetString(0, &pref_name))
139 return;
141 if (pref_name == "blocked_languages") {
142 std::string language;
143 if (!args->GetString(1, &language))
144 return;
145 translate_prefs->UnblockLanguage(language);
146 } else if (pref_name == "language_blacklist") {
147 std::string language;
148 if (!args->GetString(1, &language))
149 return;
150 translate_prefs->RemoveLanguageFromLegacyBlacklist(language);
151 } else if (pref_name == "site_blacklist") {
152 std::string site;
153 if (!args->GetString(1, &site))
154 return;
155 translate_prefs->RemoveSiteFromBlacklist(site);
156 } else if (pref_name == "whitelists") {
157 std::string from, to;
158 if (!args->GetString(1, &from))
159 return;
160 if (!args->GetString(2, &to))
161 return;
162 translate_prefs->RemoveLanguagePairFromWhitelist(from, to);
163 } else if (pref_name == "too_often_denied") {
164 translate_prefs->ResetDenialState();
165 } else {
166 return;
169 SendPrefsToJs();
172 void TranslateInternalsHandler::OnRequestInfo(const base::ListValue* /*args*/) {
173 SendPrefsToJs();
174 SendSupportedLanguagesToJs();
177 void TranslateInternalsHandler::SendMessageToJs(const std::string& message,
178 const base::Value& value) {
179 const char func[] = "cr.translateInternals.messageHandler";
180 base::StringValue message_data(message);
181 web_ui()->CallJavascriptFunction(func, message_data, value);
184 void TranslateInternalsHandler::SendPrefsToJs() {
185 content::WebContents* web_contents = web_ui()->GetWebContents();
186 Profile* profile =
187 Profile::FromBrowserContext(web_contents->GetBrowserContext());
188 PrefService* prefs = profile->GetOriginalProfile()->GetPrefs();
190 base::DictionaryValue dict;
192 static const char* keys[] = {
193 prefs::kEnableTranslate,
194 translate::TranslatePrefs::kPrefTranslateBlockedLanguages,
195 translate::TranslatePrefs::kPrefTranslateSiteBlacklist,
196 translate::TranslatePrefs::kPrefTranslateWhitelists,
197 translate::TranslatePrefs::kPrefTranslateDeniedCount,
198 translate::TranslatePrefs::kPrefTranslateAcceptedCount,
199 translate::TranslatePrefs::kPrefTranslateLastDeniedTimeForLanguage,
200 translate::TranslatePrefs::kPrefTranslateTooOftenDeniedForLanguage,
202 for (const char* key : keys) {
203 const PrefService::Preference* pref = prefs->FindPreference(key);
204 if (pref)
205 dict.Set(key, pref->GetValue()->DeepCopy());
208 SendMessageToJs("prefsUpdated", dict);
211 void TranslateInternalsHandler::SendSupportedLanguagesToJs() {
212 base::DictionaryValue dict;
214 std::vector<std::string> languages;
215 translate::TranslateDownloadManager::GetSupportedLanguages(&languages);
216 base::Time last_updated =
217 translate::TranslateDownloadManager::GetSupportedLanguagesLastUpdated();
219 base::ListValue* languages_list = new base::ListValue();
220 base::ListValue* alpha_languages_list = new base::ListValue();
221 for (std::vector<std::string>::iterator it = languages.begin();
222 it != languages.end(); ++it) {
223 const std::string& lang = *it;
224 languages_list->Append(new base::StringValue(lang));
225 if (translate::TranslateDownloadManager::IsAlphaLanguage(lang))
226 alpha_languages_list->Append(new base::StringValue(lang));
229 dict.Set("languages", languages_list);
230 dict.Set("alpha_languages", alpha_languages_list);
231 dict.Set("last_updated",
232 new base::FundamentalValue(last_updated.ToJsTime()));
233 SendMessageToJs("supportedLanguagesUpdated", dict);