Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / ui / webui / translate_internals / translate_internals_handler.cc
blob8d7b1959b09b5449536eb6978065ddb3809405eb
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/profiles/profile.h"
15 #include "chrome/browser/translate/translate_prefs.h"
16 #include "chrome/common/pref_names.h"
17 #include "components/translate/core/browser/translate_error_details.h"
18 #include "components/translate/core/browser/translate_event_details.h"
19 #include "components/translate/core/common/language_detection_details.h"
20 #include "content/public/browser/web_contents.h"
21 #include "content/public/browser/web_ui.h"
23 TranslateInternalsHandler::TranslateInternalsHandler() {
24 TranslateManager::GetInstance()->AddObserver(this);
27 TranslateInternalsHandler::~TranslateInternalsHandler() {
28 TranslateManager::GetInstance()->RemoveObserver(this);
31 void TranslateInternalsHandler::RegisterMessages() {
32 web_ui()->RegisterMessageCallback("removePrefItem", base::Bind(
33 &TranslateInternalsHandler::OnRemovePrefItem, base::Unretained(this)));
34 web_ui()->RegisterMessageCallback("requestInfo", base::Bind(
35 &TranslateInternalsHandler::OnRequestInfo, base::Unretained(this)));
38 void TranslateInternalsHandler::OnLanguageDetection(
39 const LanguageDetectionDetails& details) {
40 if (!TranslateManager::IsTranslatableURL(details.url))
41 return;
43 base::DictionaryValue dict;
44 dict.Set("time",
45 new base::FundamentalValue(details.time.ToJsTime()));
46 dict.Set("url",
47 new base::StringValue(details.url.spec()));
48 dict.Set("content_language",
49 new base::StringValue(details.content_language));
50 dict.Set("cld_language",
51 new base::StringValue(details.cld_language));
52 dict.Set("is_cld_reliable",
53 new base::FundamentalValue(details.is_cld_reliable));
54 dict.Set("html_root_language",
55 new base::StringValue(details.html_root_language));
56 dict.Set("adopted_language",
57 new base::StringValue(details.adopted_language));
58 dict.Set("content", new base::StringValue(details.contents));
59 SendMessageToJs("languageDetectionInfoAdded", dict);
62 void TranslateInternalsHandler::OnTranslateError(
63 const TranslateErrorDetails& details) {
64 base::DictionaryValue dict;
65 dict.Set("time",
66 new base::FundamentalValue(details.time.ToJsTime()));
67 dict.Set("url",
68 new base::StringValue(details.url.spec()));
69 dict.Set("error",
70 new base::FundamentalValue(details.error));
71 SendMessageToJs("translateErrorDetailsAdded", dict);
74 void TranslateInternalsHandler::OnTranslateEvent(
75 const TranslateEventDetails& details) {
76 base::DictionaryValue dict;
77 dict.Set("time", new base::FundamentalValue(details.time.ToJsTime()));
78 dict.Set("filename", new base::StringValue(details.filename));
79 dict.Set("line", new base::FundamentalValue(details.line));
80 dict.Set("message", new base::StringValue(details.message));
81 SendMessageToJs("translateEventDetailsAdded", dict);
84 void TranslateInternalsHandler::OnRemovePrefItem(const base::ListValue* args) {
85 content::WebContents* web_contents = web_ui()->GetWebContents();
86 Profile* profile =
87 Profile::FromBrowserContext(web_contents->GetBrowserContext());
88 PrefService* prefs = profile->GetOriginalProfile()->GetPrefs();
89 TranslatePrefs translate_prefs(prefs);
91 std::string pref_name;
92 if (!args->GetString(0, &pref_name))
93 return;
95 if (pref_name == "blocked_languages") {
96 std::string language;
97 if (!args->GetString(1, &language))
98 return;
99 translate_prefs.UnblockLanguage(language);
100 } else if (pref_name == "language_blacklist") {
101 std::string language;
102 if (!args->GetString(1, &language))
103 return;
104 translate_prefs.RemoveLanguageFromLegacyBlacklist(language);
105 } else if (pref_name == "site_blacklist") {
106 std::string site;
107 if (!args->GetString(1, &site))
108 return;
109 translate_prefs.RemoveSiteFromBlacklist(site);
110 } else if (pref_name == "whitelists") {
111 std::string from, to;
112 if (!args->GetString(1, &from))
113 return;
114 if (!args->GetString(2, &to))
115 return;
116 translate_prefs.RemoveLanguagePairFromWhitelist(from, to);
117 } else {
118 return;
121 SendPrefsToJs();
124 void TranslateInternalsHandler::OnRequestInfo(const base::ListValue* /*args*/) {
125 SendPrefsToJs();
126 SendSupportedLanguagesToJs();
129 void TranslateInternalsHandler::SendMessageToJs(const std::string& message,
130 const base::Value& value) {
131 const char func[] = "cr.translateInternals.messageHandler";
132 base::StringValue message_data(message);
133 web_ui()->CallJavascriptFunction(func, message_data, value);
136 void TranslateInternalsHandler::SendPrefsToJs() {
137 content::WebContents* web_contents = web_ui()->GetWebContents();
138 Profile* profile =
139 Profile::FromBrowserContext(web_contents->GetBrowserContext());
140 PrefService* prefs = profile->GetOriginalProfile()->GetPrefs();
142 base::DictionaryValue dict;
144 std::vector<std::string> keys;
145 keys.push_back(prefs::kEnableTranslate);
147 keys.push_back(TranslatePrefs::kPrefTranslateBlockedLanguages);
148 keys.push_back(TranslatePrefs::kPrefTranslateLanguageBlacklist);
149 keys.push_back(TranslatePrefs::kPrefTranslateSiteBlacklist);
150 keys.push_back(TranslatePrefs::kPrefTranslateWhitelists);
151 keys.push_back(TranslatePrefs::kPrefTranslateDeniedCount);
152 keys.push_back(TranslatePrefs::kPrefTranslateAcceptedCount);
154 for (std::vector<std::string>::const_iterator it = keys.begin();
155 it != keys.end(); ++it) {
156 const std::string& key = *it;
157 const PrefService::Preference* pref = prefs->FindPreference(key.c_str());
158 if (pref)
159 dict.Set(key, pref->GetValue()->DeepCopy());
162 SendMessageToJs("prefsUpdated", dict);
165 void TranslateInternalsHandler::SendSupportedLanguagesToJs() {
166 base::DictionaryValue dict;
168 std::vector<std::string> languages;
169 TranslateManager::GetSupportedLanguages(&languages);
170 base::Time last_updated =
171 TranslateManager::GetSupportedLanguagesLastUpdated();
173 base::ListValue* languages_list = new base::ListValue();
174 base::ListValue* alpha_languages_list = new base::ListValue();
175 for (std::vector<std::string>::iterator it = languages.begin();
176 it != languages.end(); ++it) {
177 const std::string& lang = *it;
178 languages_list->Append(new base::StringValue(lang));
179 if (TranslateManager::IsAlphaLanguage(lang))
180 alpha_languages_list->Append(new base::StringValue(lang));
183 dict.Set("languages", languages_list);
184 dict.Set("alpha_languages", alpha_languages_list);
185 dict.Set("last_updated",
186 new base::FundamentalValue(last_updated.ToJsTime()));
187 SendMessageToJs("supportedLanguagesUpdated", dict);