OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/webui/translate_internals/translate_internals_handle
r.h" | 5 #include "chrome/browser/ui/webui/translate_internals/translate_internals_handle
r.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 16 matching lines...) Expand all Loading... |
27 #include "content/public/browser/notification_source.h" | 27 #include "content/public/browser/notification_source.h" |
28 #include "content/public/browser/notification_types.h" | 28 #include "content/public/browser/notification_types.h" |
29 #include "content/public/browser/web_contents.h" | 29 #include "content/public/browser/web_contents.h" |
30 #include "content/public/browser/web_ui.h" | 30 #include "content/public/browser/web_ui.h" |
31 | 31 |
32 TranslateInternalsHandler::TranslateInternalsHandler() { | 32 TranslateInternalsHandler::TranslateInternalsHandler() { |
33 notification_registrar_.Add(this, | 33 notification_registrar_.Add(this, |
34 chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED, | 34 chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED, |
35 content::NotificationService::AllSources()); | 35 content::NotificationService::AllSources()); |
36 | 36 |
37 error_subscription_ = TranslateManager::RegisterTranslateErrorCallback( | 37 error_subscription_ = |
38 base::Bind(&TranslateInternalsHandler::OnTranslateError, | 38 translate::TranslateManager::RegisterTranslateErrorCallback( |
39 base::Unretained(this))); | 39 base::Bind(&TranslateInternalsHandler::OnTranslateError, |
| 40 base::Unretained(this))); |
40 | 41 |
41 TranslateLanguageList* language_list = | 42 translate::TranslateLanguageList* language_list = |
42 TranslateDownloadManager::GetInstance()->language_list(); | 43 translate::TranslateDownloadManager::GetInstance()->language_list(); |
43 if (!language_list) { | 44 if (!language_list) { |
44 NOTREACHED(); | 45 NOTREACHED(); |
45 return; | 46 return; |
46 } | 47 } |
47 | 48 |
48 event_subscription_ = language_list->RegisterEventCallback(base::Bind( | 49 event_subscription_ = language_list->RegisterEventCallback(base::Bind( |
49 &TranslateInternalsHandler::OnTranslateEvent, base::Unretained(this))); | 50 &TranslateInternalsHandler::OnTranslateEvent, base::Unretained(this))); |
50 } | 51 } |
51 | 52 |
52 TranslateInternalsHandler::~TranslateInternalsHandler() { | 53 TranslateInternalsHandler::~TranslateInternalsHandler() { |
53 // |event_subscription_| and |error_subscription_| are deleted automatically | 54 // |event_subscription_| and |error_subscription_| are deleted automatically |
54 // and un-register the callbacks automatically. | 55 // and un-register the callbacks automatically. |
55 } | 56 } |
56 | 57 |
57 void TranslateInternalsHandler::RegisterMessages() { | 58 void TranslateInternalsHandler::RegisterMessages() { |
58 web_ui()->RegisterMessageCallback("removePrefItem", base::Bind( | 59 web_ui()->RegisterMessageCallback("removePrefItem", base::Bind( |
59 &TranslateInternalsHandler::OnRemovePrefItem, base::Unretained(this))); | 60 &TranslateInternalsHandler::OnRemovePrefItem, base::Unretained(this))); |
60 web_ui()->RegisterMessageCallback("requestInfo", base::Bind( | 61 web_ui()->RegisterMessageCallback("requestInfo", base::Bind( |
61 &TranslateInternalsHandler::OnRequestInfo, base::Unretained(this))); | 62 &TranslateInternalsHandler::OnRequestInfo, base::Unretained(this))); |
62 } | 63 } |
63 | 64 |
64 void TranslateInternalsHandler::Observe( | 65 void TranslateInternalsHandler::Observe( |
65 int type, | 66 int type, |
66 const content::NotificationSource& source, | 67 const content::NotificationSource& source, |
67 const content::NotificationDetails& details) { | 68 const content::NotificationDetails& details) { |
68 DCHECK_EQ(chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED, type); | 69 DCHECK_EQ(chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED, type); |
69 const LanguageDetectionDetails* language_detection_details = | 70 const translate::LanguageDetectionDetails* language_detection_details = |
70 content::Details<const LanguageDetectionDetails>(details).ptr(); | 71 content::Details<const translate::LanguageDetectionDetails>(details) |
| 72 .ptr(); |
71 content::WebContents* web_contents = | 73 content::WebContents* web_contents = |
72 content::Source<content::WebContents>(source).ptr(); | 74 content::Source<content::WebContents>(source).ptr(); |
73 | 75 |
74 if (web_contents->GetBrowserContext()->IsOffTheRecord() || | 76 if (web_contents->GetBrowserContext()->IsOffTheRecord() || |
75 !TranslateService::IsTranslatableURL(language_detection_details->url)) { | 77 !TranslateService::IsTranslatableURL(language_detection_details->url)) { |
76 return; | 78 return; |
77 } | 79 } |
78 | 80 |
79 base::DictionaryValue dict; | 81 base::DictionaryValue dict; |
80 dict.Set( | 82 dict.Set( |
(...skipping 12 matching lines...) Expand all Loading... |
93 "html_root_language", | 95 "html_root_language", |
94 new base::StringValue(language_detection_details->html_root_language)); | 96 new base::StringValue(language_detection_details->html_root_language)); |
95 dict.Set("adopted_language", | 97 dict.Set("adopted_language", |
96 new base::StringValue(language_detection_details->adopted_language)); | 98 new base::StringValue(language_detection_details->adopted_language)); |
97 dict.Set("content", | 99 dict.Set("content", |
98 new base::StringValue(language_detection_details->contents)); | 100 new base::StringValue(language_detection_details->contents)); |
99 SendMessageToJs("languageDetectionInfoAdded", dict); | 101 SendMessageToJs("languageDetectionInfoAdded", dict); |
100 } | 102 } |
101 | 103 |
102 void TranslateInternalsHandler::OnTranslateError( | 104 void TranslateInternalsHandler::OnTranslateError( |
103 const TranslateErrorDetails& details) { | 105 const translate::TranslateErrorDetails& details) { |
104 base::DictionaryValue dict; | 106 base::DictionaryValue dict; |
105 dict.Set("time", | 107 dict.Set("time", |
106 new base::FundamentalValue(details.time.ToJsTime())); | 108 new base::FundamentalValue(details.time.ToJsTime())); |
107 dict.Set("url", | 109 dict.Set("url", |
108 new base::StringValue(details.url.spec())); | 110 new base::StringValue(details.url.spec())); |
109 dict.Set("error", | 111 dict.Set("error", |
110 new base::FundamentalValue(details.error)); | 112 new base::FundamentalValue(details.error)); |
111 SendMessageToJs("translateErrorDetailsAdded", dict); | 113 SendMessageToJs("translateErrorDetailsAdded", dict); |
112 } | 114 } |
113 | 115 |
114 void TranslateInternalsHandler::OnTranslateEvent( | 116 void TranslateInternalsHandler::OnTranslateEvent( |
115 const TranslateEventDetails& details) { | 117 const translate::TranslateEventDetails& details) { |
116 base::DictionaryValue dict; | 118 base::DictionaryValue dict; |
117 dict.Set("time", new base::FundamentalValue(details.time.ToJsTime())); | 119 dict.Set("time", new base::FundamentalValue(details.time.ToJsTime())); |
118 dict.Set("filename", new base::StringValue(details.filename)); | 120 dict.Set("filename", new base::StringValue(details.filename)); |
119 dict.Set("line", new base::FundamentalValue(details.line)); | 121 dict.Set("line", new base::FundamentalValue(details.line)); |
120 dict.Set("message", new base::StringValue(details.message)); | 122 dict.Set("message", new base::StringValue(details.message)); |
121 SendMessageToJs("translateEventDetailsAdded", dict); | 123 SendMessageToJs("translateEventDetailsAdded", dict); |
122 } | 124 } |
123 | 125 |
124 void TranslateInternalsHandler::OnRemovePrefItem(const base::ListValue* args) { | 126 void TranslateInternalsHandler::OnRemovePrefItem(const base::ListValue* args) { |
125 content::WebContents* web_contents = web_ui()->GetWebContents(); | 127 content::WebContents* web_contents = web_ui()->GetWebContents(); |
126 Profile* profile = | 128 Profile* profile = |
127 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | 129 Profile::FromBrowserContext(web_contents->GetBrowserContext()); |
128 PrefService* prefs = profile->GetOriginalProfile()->GetPrefs(); | 130 PrefService* prefs = profile->GetOriginalProfile()->GetPrefs(); |
129 scoped_ptr<TranslatePrefs> translate_prefs( | 131 scoped_ptr<translate::TranslatePrefs> translate_prefs( |
130 ChromeTranslateClient::CreateTranslatePrefs(prefs)); | 132 ChromeTranslateClient::CreateTranslatePrefs(prefs)); |
131 | 133 |
132 std::string pref_name; | 134 std::string pref_name; |
133 if (!args->GetString(0, &pref_name)) | 135 if (!args->GetString(0, &pref_name)) |
134 return; | 136 return; |
135 | 137 |
136 if (pref_name == "blocked_languages") { | 138 if (pref_name == "blocked_languages") { |
137 std::string language; | 139 std::string language; |
138 if (!args->GetString(1, &language)) | 140 if (!args->GetString(1, &language)) |
139 return; | 141 return; |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
178 content::WebContents* web_contents = web_ui()->GetWebContents(); | 180 content::WebContents* web_contents = web_ui()->GetWebContents(); |
179 Profile* profile = | 181 Profile* profile = |
180 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | 182 Profile::FromBrowserContext(web_contents->GetBrowserContext()); |
181 PrefService* prefs = profile->GetOriginalProfile()->GetPrefs(); | 183 PrefService* prefs = profile->GetOriginalProfile()->GetPrefs(); |
182 | 184 |
183 base::DictionaryValue dict; | 185 base::DictionaryValue dict; |
184 | 186 |
185 std::vector<std::string> keys; | 187 std::vector<std::string> keys; |
186 keys.push_back(prefs::kEnableTranslate); | 188 keys.push_back(prefs::kEnableTranslate); |
187 | 189 |
188 keys.push_back(TranslatePrefs::kPrefTranslateBlockedLanguages); | 190 keys.push_back(translate::TranslatePrefs::kPrefTranslateBlockedLanguages); |
189 keys.push_back(TranslatePrefs::kPrefTranslateLanguageBlacklist); | 191 keys.push_back(translate::TranslatePrefs::kPrefTranslateLanguageBlacklist); |
190 keys.push_back(TranslatePrefs::kPrefTranslateSiteBlacklist); | 192 keys.push_back(translate::TranslatePrefs::kPrefTranslateSiteBlacklist); |
191 keys.push_back(TranslatePrefs::kPrefTranslateWhitelists); | 193 keys.push_back(translate::TranslatePrefs::kPrefTranslateWhitelists); |
192 keys.push_back(TranslatePrefs::kPrefTranslateDeniedCount); | 194 keys.push_back(translate::TranslatePrefs::kPrefTranslateDeniedCount); |
193 keys.push_back(TranslatePrefs::kPrefTranslateAcceptedCount); | 195 keys.push_back(translate::TranslatePrefs::kPrefTranslateAcceptedCount); |
194 | 196 |
195 for (std::vector<std::string>::const_iterator it = keys.begin(); | 197 for (std::vector<std::string>::const_iterator it = keys.begin(); |
196 it != keys.end(); ++it) { | 198 it != keys.end(); ++it) { |
197 const std::string& key = *it; | 199 const std::string& key = *it; |
198 const PrefService::Preference* pref = prefs->FindPreference(key.c_str()); | 200 const PrefService::Preference* pref = prefs->FindPreference(key.c_str()); |
199 if (pref) | 201 if (pref) |
200 dict.Set(key, pref->GetValue()->DeepCopy()); | 202 dict.Set(key, pref->GetValue()->DeepCopy()); |
201 } | 203 } |
202 | 204 |
203 SendMessageToJs("prefsUpdated", dict); | 205 SendMessageToJs("prefsUpdated", dict); |
204 } | 206 } |
205 | 207 |
206 void TranslateInternalsHandler::SendSupportedLanguagesToJs() { | 208 void TranslateInternalsHandler::SendSupportedLanguagesToJs() { |
207 base::DictionaryValue dict; | 209 base::DictionaryValue dict; |
208 | 210 |
209 std::vector<std::string> languages; | 211 std::vector<std::string> languages; |
210 TranslateDownloadManager::GetSupportedLanguages(&languages); | 212 translate::TranslateDownloadManager::GetSupportedLanguages(&languages); |
211 base::Time last_updated = | 213 base::Time last_updated = |
212 TranslateDownloadManager::GetSupportedLanguagesLastUpdated(); | 214 translate::TranslateDownloadManager::GetSupportedLanguagesLastUpdated(); |
213 | 215 |
214 base::ListValue* languages_list = new base::ListValue(); | 216 base::ListValue* languages_list = new base::ListValue(); |
215 base::ListValue* alpha_languages_list = new base::ListValue(); | 217 base::ListValue* alpha_languages_list = new base::ListValue(); |
216 for (std::vector<std::string>::iterator it = languages.begin(); | 218 for (std::vector<std::string>::iterator it = languages.begin(); |
217 it != languages.end(); ++it) { | 219 it != languages.end(); ++it) { |
218 const std::string& lang = *it; | 220 const std::string& lang = *it; |
219 languages_list->Append(new base::StringValue(lang)); | 221 languages_list->Append(new base::StringValue(lang)); |
220 if (TranslateDownloadManager::IsAlphaLanguage(lang)) | 222 if (translate::TranslateDownloadManager::IsAlphaLanguage(lang)) |
221 alpha_languages_list->Append(new base::StringValue(lang)); | 223 alpha_languages_list->Append(new base::StringValue(lang)); |
222 } | 224 } |
223 | 225 |
224 dict.Set("languages", languages_list); | 226 dict.Set("languages", languages_list); |
225 dict.Set("alpha_languages", alpha_languages_list); | 227 dict.Set("alpha_languages", alpha_languages_list); |
226 dict.Set("last_updated", | 228 dict.Set("last_updated", |
227 new base::FundamentalValue(last_updated.ToJsTime())); | 229 new base::FundamentalValue(last_updated.ToJsTime())); |
228 SendMessageToJs("supportedLanguagesUpdated", dict); | 230 SendMessageToJs("supportedLanguagesUpdated", dict); |
229 } | 231 } |
OLD | NEW |