NaCl: Update revision in DEPS, r12770 -> r12773
[chromium-blink-merge.git] / chrome / browser / ui / webui / translate_internals / translate_internals_handler.cc
blob65323227be87b2148d06ec4c252413112fd87550
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_tab_helper.h"
16 #include "chrome/common/pref_names.h"
17 #include "components/translate/core/browser/translate_download_manager.h"
18 #include "components/translate/core/browser/translate_error_details.h"
19 #include "components/translate/core/browser/translate_event_details.h"
20 #include "components/translate/core/browser/translate_prefs.h"
21 #include "components/translate/core/common/language_detection_details.h"
22 #include "components/translate/core/common/translate_pref_names.h"
23 #include "content/public/browser/web_contents.h"
24 #include "content/public/browser/web_ui.h"
26 TranslateInternalsHandler::TranslateInternalsHandler() {
27 TranslateManager::GetInstance()->AddObserver(this);
28 TranslateLanguageList* language_list =
29 TranslateDownloadManager::GetInstance()->language_list();
30 if (!language_list) {
31 NOTREACHED();
32 return;
35 event_subscription_ = language_list->RegisterEventCallback(base::Bind(
36 &TranslateInternalsHandler::OnTranslateEvent, base::Unretained(this)));
39 TranslateInternalsHandler::~TranslateInternalsHandler() {
40 TranslateManager::GetInstance()->RemoveObserver(this);
42 // |event_subscription_| is deleted automatically and un-registers the
43 // callback automatically.
46 void TranslateInternalsHandler::RegisterMessages() {
47 web_ui()->RegisterMessageCallback("removePrefItem", base::Bind(
48 &TranslateInternalsHandler::OnRemovePrefItem, base::Unretained(this)));
49 web_ui()->RegisterMessageCallback("requestInfo", base::Bind(
50 &TranslateInternalsHandler::OnRequestInfo, base::Unretained(this)));
53 void TranslateInternalsHandler::OnLanguageDetection(
54 const LanguageDetectionDetails& details) {
55 if (!TranslateManager::IsTranslatableURL(details.url))
56 return;
58 base::DictionaryValue dict;
59 dict.Set("time",
60 new base::FundamentalValue(details.time.ToJsTime()));
61 dict.Set("url",
62 new base::StringValue(details.url.spec()));
63 dict.Set("content_language",
64 new base::StringValue(details.content_language));
65 dict.Set("cld_language",
66 new base::StringValue(details.cld_language));
67 dict.Set("is_cld_reliable",
68 new base::FundamentalValue(details.is_cld_reliable));
69 dict.Set("html_root_language",
70 new base::StringValue(details.html_root_language));
71 dict.Set("adopted_language",
72 new base::StringValue(details.adopted_language));
73 dict.Set("content", new base::StringValue(details.contents));
74 SendMessageToJs("languageDetectionInfoAdded", dict);
77 void TranslateInternalsHandler::OnTranslateError(
78 const TranslateErrorDetails& details) {
79 base::DictionaryValue dict;
80 dict.Set("time",
81 new base::FundamentalValue(details.time.ToJsTime()));
82 dict.Set("url",
83 new base::StringValue(details.url.spec()));
84 dict.Set("error",
85 new base::FundamentalValue(details.error));
86 SendMessageToJs("translateErrorDetailsAdded", dict);
89 void TranslateInternalsHandler::OnTranslateEvent(
90 const TranslateEventDetails& details) {
91 base::DictionaryValue dict;
92 dict.Set("time", new base::FundamentalValue(details.time.ToJsTime()));
93 dict.Set("filename", new base::StringValue(details.filename));
94 dict.Set("line", new base::FundamentalValue(details.line));
95 dict.Set("message", new base::StringValue(details.message));
96 SendMessageToJs("translateEventDetailsAdded", dict);
99 void TranslateInternalsHandler::OnRemovePrefItem(const base::ListValue* args) {
100 content::WebContents* web_contents = web_ui()->GetWebContents();
101 Profile* profile =
102 Profile::FromBrowserContext(web_contents->GetBrowserContext());
103 PrefService* prefs = profile->GetOriginalProfile()->GetPrefs();
104 scoped_ptr<TranslatePrefs> translate_prefs(
105 TranslateTabHelper::CreateTranslatePrefs(prefs));
107 std::string pref_name;
108 if (!args->GetString(0, &pref_name))
109 return;
111 if (pref_name == "blocked_languages") {
112 std::string language;
113 if (!args->GetString(1, &language))
114 return;
115 translate_prefs->UnblockLanguage(language);
116 } else if (pref_name == "language_blacklist") {
117 std::string language;
118 if (!args->GetString(1, &language))
119 return;
120 translate_prefs->RemoveLanguageFromLegacyBlacklist(language);
121 } else if (pref_name == "site_blacklist") {
122 std::string site;
123 if (!args->GetString(1, &site))
124 return;
125 translate_prefs->RemoveSiteFromBlacklist(site);
126 } else if (pref_name == "whitelists") {
127 std::string from, to;
128 if (!args->GetString(1, &from))
129 return;
130 if (!args->GetString(2, &to))
131 return;
132 translate_prefs->RemoveLanguagePairFromWhitelist(from, to);
133 } else {
134 return;
137 SendPrefsToJs();
140 void TranslateInternalsHandler::OnRequestInfo(const base::ListValue* /*args*/) {
141 SendPrefsToJs();
142 SendSupportedLanguagesToJs();
145 void TranslateInternalsHandler::SendMessageToJs(const std::string& message,
146 const base::Value& value) {
147 const char func[] = "cr.translateInternals.messageHandler";
148 base::StringValue message_data(message);
149 web_ui()->CallJavascriptFunction(func, message_data, value);
152 void TranslateInternalsHandler::SendPrefsToJs() {
153 content::WebContents* web_contents = web_ui()->GetWebContents();
154 Profile* profile =
155 Profile::FromBrowserContext(web_contents->GetBrowserContext());
156 PrefService* prefs = profile->GetOriginalProfile()->GetPrefs();
158 base::DictionaryValue dict;
160 std::vector<std::string> keys;
161 keys.push_back(prefs::kEnableTranslate);
163 keys.push_back(TranslatePrefs::kPrefTranslateBlockedLanguages);
164 keys.push_back(TranslatePrefs::kPrefTranslateLanguageBlacklist);
165 keys.push_back(TranslatePrefs::kPrefTranslateSiteBlacklist);
166 keys.push_back(TranslatePrefs::kPrefTranslateWhitelists);
167 keys.push_back(TranslatePrefs::kPrefTranslateDeniedCount);
168 keys.push_back(TranslatePrefs::kPrefTranslateAcceptedCount);
170 for (std::vector<std::string>::const_iterator it = keys.begin();
171 it != keys.end(); ++it) {
172 const std::string& key = *it;
173 const PrefService::Preference* pref = prefs->FindPreference(key.c_str());
174 if (pref)
175 dict.Set(key, pref->GetValue()->DeepCopy());
178 SendMessageToJs("prefsUpdated", dict);
181 void TranslateInternalsHandler::SendSupportedLanguagesToJs() {
182 base::DictionaryValue dict;
184 std::vector<std::string> languages;
185 TranslateDownloadManager::GetSupportedLanguages(&languages);
186 base::Time last_updated =
187 TranslateDownloadManager::GetSupportedLanguagesLastUpdated();
189 base::ListValue* languages_list = new base::ListValue();
190 base::ListValue* alpha_languages_list = new base::ListValue();
191 for (std::vector<std::string>::iterator it = languages.begin();
192 it != languages.end(); ++it) {
193 const std::string& lang = *it;
194 languages_list->Append(new base::StringValue(lang));
195 if (TranslateDownloadManager::IsAlphaLanguage(lang))
196 alpha_languages_list->Append(new base::StringValue(lang));
199 dict.Set("languages", languages_list);
200 dict.Set("alpha_languages", alpha_languages_list);
201 dict.Set("last_updated",
202 new base::FundamentalValue(last_updated.ToJsTime()));
203 SendMessageToJs("supportedLanguagesUpdated", dict);