OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/translate/translate_manager.h" | 5 #include "chrome/browser/translate/translate_manager.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
9 #include "base/json/json_reader.h" | 9 #include "base/json/json_reader.h" |
10 #include "base/memory/singleton.h" | 10 #include "base/memory/singleton.h" |
11 #include "base/metrics/histogram.h" | 11 #include "base/metrics/histogram.h" |
12 #include "base/string_split.h" | 12 #include "base/string_split.h" |
13 #include "base/string_util.h" | 13 #include "base/string_util.h" |
14 #include "base/values.h" | 14 #include "base/values.h" |
15 #include "chrome/browser/autofill/autofill_manager.h" | 15 #include "chrome/browser/autofill/autofill_manager.h" |
16 #include "chrome/browser/browser_process.h" | 16 #include "chrome/browser/browser_process.h" |
17 #include "chrome/browser/prefs/pref_service.h" | 17 #include "chrome/browser/prefs/pref_service.h" |
18 #include "chrome/browser/profiles/profile.h" | 18 #include "chrome/browser/profiles/profile.h" |
19 #include "chrome/browser/tab_contents/language_state.h" | 19 #include "chrome/browser/tab_contents/language_state.h" |
20 #include "chrome/browser/tab_contents/tab_util.h" | 20 #include "chrome/browser/tab_contents/tab_util.h" |
21 #include "chrome/browser/tabs/tab_strip_model.h" | 21 #include "chrome/browser/tabs/tab_strip_model.h" |
22 #include "chrome/browser/translate/page_translated_details.h" | 22 #include "chrome/browser/translate/page_translated_details.h" |
23 #include "chrome/browser/translate/translate_infobar_delegate.h" | 23 #include "chrome/browser/translate/translate_infobar_delegate.h" |
24 #include "chrome/browser/translate/translate_tab_helper.h" | 24 #include "chrome/browser/translate/translate_tab_helper.h" |
25 #include "chrome/browser/translate/translate_prefs.h" | 25 #include "chrome/browser/translate/translate_prefs.h" |
26 #include "chrome/browser/ui/browser.h" | 26 #include "chrome/browser/ui/browser.h" |
27 #include "chrome/browser/ui/browser_list.h" | 27 #include "chrome/browser/ui/browser_list.h" |
28 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | 28 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
| 29 #include "chrome/common/chrome_notification_types.h" |
29 #include "chrome/common/chrome_switches.h" | 30 #include "chrome/common/chrome_switches.h" |
30 #include "chrome/common/pref_names.h" | 31 #include "chrome/common/pref_names.h" |
31 #include "chrome/common/render_messages.h" | 32 #include "chrome/common/render_messages.h" |
32 #include "chrome/common/translate_errors.h" | 33 #include "chrome/common/translate_errors.h" |
33 #include "chrome/common/url_constants.h" | 34 #include "chrome/common/url_constants.h" |
34 #include "content/browser/renderer_host/render_process_host.h" | 35 #include "content/browser/renderer_host/render_process_host.h" |
35 #include "content/browser/renderer_host/render_view_host.h" | 36 #include "content/browser/renderer_host/render_view_host.h" |
36 #include "content/browser/tab_contents/navigation_details.h" | 37 #include "content/browser/tab_contents/navigation_details.h" |
37 #include "content/browser/tab_contents/navigation_entry.h" | 38 #include "content/browser/tab_contents/navigation_entry.h" |
38 #include "content/browser/tab_contents/tab_contents.h" | 39 #include "content/browser/tab_contents/tab_contents.h" |
| 40 #include "content/common/content_notification_types.h" |
39 #include "content/common/notification_details.h" | 41 #include "content/common/notification_details.h" |
40 #include "content/common/notification_service.h" | 42 #include "content/common/notification_service.h" |
41 #include "content/common/notification_source.h" | 43 #include "content/common/notification_source.h" |
42 #include "content/common/notification_type.h" | |
43 #include "grit/browser_resources.h" | 44 #include "grit/browser_resources.h" |
44 #include "net/base/escape.h" | 45 #include "net/base/escape.h" |
45 #include "net/url_request/url_request_status.h" | 46 #include "net/url_request/url_request_status.h" |
46 #include "ui/base/resource/resource_bundle.h" | 47 #include "ui/base/resource/resource_bundle.h" |
47 | 48 |
48 namespace { | 49 namespace { |
49 | 50 |
50 // The list of languages the Google translation server supports. | 51 // The list of languages the Google translation server supports. |
51 // For information, here is the list of languages that Chrome can be run in | 52 // For information, here is the list of languages that Chrome can be run in |
52 // but that the translation server does not support: | 53 // but that the translation server does not support: |
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
240 return chrome_locale; | 241 return chrome_locale; |
241 return chrome_locale.substr(0, hypen_index); | 242 return chrome_locale.substr(0, hypen_index); |
242 } | 243 } |
243 | 244 |
244 // static | 245 // static |
245 bool TranslateManager::IsSupportedLanguage(const std::string& page_language) { | 246 bool TranslateManager::IsSupportedLanguage(const std::string& page_language) { |
246 InitSupportedLanguages(); | 247 InitSupportedLanguages(); |
247 return supported_languages_.Pointer()->count(page_language) != 0; | 248 return supported_languages_.Pointer()->count(page_language) != 0; |
248 } | 249 } |
249 | 250 |
250 void TranslateManager::Observe(NotificationType type, | 251 void TranslateManager::Observe(int type, |
251 const NotificationSource& source, | 252 const NotificationSource& source, |
252 const NotificationDetails& details) { | 253 const NotificationDetails& details) { |
253 switch (type.value) { | 254 switch (type) { |
254 case NotificationType::NAV_ENTRY_COMMITTED: { | 255 case content::NOTIFICATION_NAV_ENTRY_COMMITTED: { |
255 NavigationController* controller = | 256 NavigationController* controller = |
256 Source<NavigationController>(source).ptr(); | 257 Source<NavigationController>(source).ptr(); |
257 content::LoadCommittedDetails* load_details = | 258 content::LoadCommittedDetails* load_details = |
258 Details<content::LoadCommittedDetails>(details).ptr(); | 259 Details<content::LoadCommittedDetails>(details).ptr(); |
259 NavigationEntry* entry = controller->GetActiveEntry(); | 260 NavigationEntry* entry = controller->GetActiveEntry(); |
260 if (!entry) { | 261 if (!entry) { |
261 NOTREACHED(); | 262 NOTREACHED(); |
262 return; | 263 return; |
263 } | 264 } |
264 | 265 |
(...skipping 22 matching lines...) Expand all Loading... |
287 // current infobars. Since InitTranslation might add an infobar, it must | 288 // current infobars. Since InitTranslation might add an infobar, it must |
288 // be done after that. | 289 // be done after that. |
289 MessageLoop::current()->PostTask(FROM_HERE, | 290 MessageLoop::current()->PostTask(FROM_HERE, |
290 method_factory_.NewRunnableMethod( | 291 method_factory_.NewRunnableMethod( |
291 &TranslateManager::InitiateTranslationPosted, | 292 &TranslateManager::InitiateTranslationPosted, |
292 controller->tab_contents()->render_view_host()->process()->id(), | 293 controller->tab_contents()->render_view_host()->process()->id(), |
293 controller->tab_contents()->render_view_host()->routing_id(), | 294 controller->tab_contents()->render_view_host()->routing_id(), |
294 helper->language_state().original_language())); | 295 helper->language_state().original_language())); |
295 break; | 296 break; |
296 } | 297 } |
297 case NotificationType::TAB_LANGUAGE_DETERMINED: { | 298 case chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED: { |
298 TabContents* tab = Source<TabContents>(source).ptr(); | 299 TabContents* tab = Source<TabContents>(source).ptr(); |
299 // We may get this notifications multiple times. Make sure to translate | 300 // We may get this notifications multiple times. Make sure to translate |
300 // only once. | 301 // only once. |
301 TabContentsWrapper* wrapper = | 302 TabContentsWrapper* wrapper = |
302 TabContentsWrapper::GetCurrentWrapperForContents(tab); | 303 TabContentsWrapper::GetCurrentWrapperForContents(tab); |
303 if (!wrapper || !wrapper->translate_tab_helper()) | 304 if (!wrapper || !wrapper->translate_tab_helper()) |
304 return; | 305 return; |
305 | 306 |
306 LanguageState& language_state = | 307 LanguageState& language_state = |
307 wrapper->translate_tab_helper()->language_state(); | 308 wrapper->translate_tab_helper()->language_state(); |
308 if (language_state.page_translatable() && | 309 if (language_state.page_translatable() && |
309 !language_state.translation_pending() && | 310 !language_state.translation_pending() && |
310 !language_state.translation_declined() && | 311 !language_state.translation_declined() && |
311 !language_state.IsPageTranslated()) { | 312 !language_state.IsPageTranslated()) { |
312 std::string language = *(Details<std::string>(details).ptr()); | 313 std::string language = *(Details<std::string>(details).ptr()); |
313 InitiateTranslation(tab, language); | 314 InitiateTranslation(tab, language); |
314 } | 315 } |
315 break; | 316 break; |
316 } | 317 } |
317 case NotificationType::PAGE_TRANSLATED: { | 318 case chrome::NOTIFICATION_PAGE_TRANSLATED: { |
318 // Only add translate infobar if it doesn't exist; if it already exists, | 319 // Only add translate infobar if it doesn't exist; if it already exists, |
319 // just update the state, the actual infobar would have received the same | 320 // just update the state, the actual infobar would have received the same |
320 // notification and update the visual display accordingly. | 321 // notification and update the visual display accordingly. |
321 TabContents* tab = Source<TabContents>(source).ptr(); | 322 TabContents* tab = Source<TabContents>(source).ptr(); |
322 PageTranslatedDetails* page_translated_details = | 323 PageTranslatedDetails* page_translated_details = |
323 Details<PageTranslatedDetails>(details).ptr(); | 324 Details<PageTranslatedDetails>(details).ptr(); |
324 PageTranslated(tab, page_translated_details); | 325 PageTranslated(tab, page_translated_details); |
325 break; | 326 break; |
326 } | 327 } |
327 case NotificationType::PROFILE_DESTROYED: { | 328 case chrome::NOTIFICATION_PROFILE_DESTROYED: { |
328 Profile* profile = Source<Profile>(source).ptr(); | 329 Profile* profile = Source<Profile>(source).ptr(); |
329 notification_registrar_.Remove(this, NotificationType::PROFILE_DESTROYED, | 330 notification_registrar_.Remove(this, |
| 331 chrome::NOTIFICATION_PROFILE_DESTROYED, |
330 source); | 332 source); |
331 size_t count = accept_languages_.erase(profile->GetPrefs()); | 333 size_t count = accept_languages_.erase(profile->GetPrefs()); |
332 // We should know about this profile since we are listening for | 334 // We should know about this profile since we are listening for |
333 // notifications on it. | 335 // notifications on it. |
334 DCHECK(count > 0); | 336 DCHECK(count > 0); |
335 pref_change_registrar_.Remove(prefs::kAcceptLanguages, this); | 337 pref_change_registrar_.Remove(prefs::kAcceptLanguages, this); |
336 break; | 338 break; |
337 } | 339 } |
338 case NotificationType::PREF_CHANGED: { | 340 case chrome::NOTIFICATION_PREF_CHANGED: { |
339 DCHECK(*Details<std::string>(details).ptr() == prefs::kAcceptLanguages); | 341 DCHECK(*Details<std::string>(details).ptr() == prefs::kAcceptLanguages); |
340 PrefService* prefs = Source<PrefService>(source).ptr(); | 342 PrefService* prefs = Source<PrefService>(source).ptr(); |
341 InitAcceptLanguages(prefs); | 343 InitAcceptLanguages(prefs); |
342 break; | 344 break; |
343 } | 345 } |
344 default: | 346 default: |
345 NOTREACHED(); | 347 NOTREACHED(); |
346 } | 348 } |
347 } | 349 } |
348 | 350 |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
418 } | 420 } |
419 | 421 |
420 // static | 422 // static |
421 bool TranslateManager::IsShowingTranslateInfobar(TabContents* tab) { | 423 bool TranslateManager::IsShowingTranslateInfobar(TabContents* tab) { |
422 return GetTranslateInfoBarDelegate(tab) != NULL; | 424 return GetTranslateInfoBarDelegate(tab) != NULL; |
423 } | 425 } |
424 | 426 |
425 TranslateManager::TranslateManager() | 427 TranslateManager::TranslateManager() |
426 : ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)), | 428 : ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)), |
427 translate_script_expiration_delay_(kTranslateScriptExpirationDelayMS) { | 429 translate_script_expiration_delay_(kTranslateScriptExpirationDelayMS) { |
428 notification_registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED, | 430 notification_registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, |
429 NotificationService::AllSources()); | 431 NotificationService::AllSources()); |
430 notification_registrar_.Add(this, NotificationType::TAB_LANGUAGE_DETERMINED, | 432 notification_registrar_.Add(this, |
| 433 chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED, |
431 NotificationService::AllSources()); | 434 NotificationService::AllSources()); |
432 notification_registrar_.Add(this, NotificationType::PAGE_TRANSLATED, | 435 notification_registrar_.Add(this, chrome::NOTIFICATION_PAGE_TRANSLATED, |
433 NotificationService::AllSources()); | 436 NotificationService::AllSources()); |
434 } | 437 } |
435 | 438 |
436 void TranslateManager::InitiateTranslation(TabContents* tab, | 439 void TranslateManager::InitiateTranslation(TabContents* tab, |
437 const std::string& page_lang) { | 440 const std::string& page_lang) { |
438 PrefService* prefs = tab->profile()->GetOriginalProfile()->GetPrefs(); | 441 PrefService* prefs = tab->profile()->GetOriginalProfile()->GetPrefs(); |
439 if (!prefs->GetBoolean(prefs::kEnableTranslate)) | 442 if (!prefs->GetBoolean(prefs::kEnableTranslate)) |
440 return; | 443 return; |
441 | 444 |
442 pref_change_registrar_.Init(prefs); | 445 pref_change_registrar_.Init(prefs); |
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
653 | 656 |
654 bool TranslateManager::IsAcceptLanguage(TabContents* tab, | 657 bool TranslateManager::IsAcceptLanguage(TabContents* tab, |
655 const std::string& language) { | 658 const std::string& language) { |
656 PrefService* pref_service = tab->profile()->GetOriginalProfile()->GetPrefs(); | 659 PrefService* pref_service = tab->profile()->GetOriginalProfile()->GetPrefs(); |
657 PrefServiceLanguagesMap::const_iterator iter = | 660 PrefServiceLanguagesMap::const_iterator iter = |
658 accept_languages_.find(pref_service); | 661 accept_languages_.find(pref_service); |
659 if (iter == accept_languages_.end()) { | 662 if (iter == accept_languages_.end()) { |
660 InitAcceptLanguages(pref_service); | 663 InitAcceptLanguages(pref_service); |
661 // Listen for this profile going away, in which case we would need to clear | 664 // Listen for this profile going away, in which case we would need to clear |
662 // the accepted languages for the profile. | 665 // the accepted languages for the profile. |
663 notification_registrar_.Add(this, NotificationType::PROFILE_DESTROYED, | 666 notification_registrar_.Add(this, chrome::NOTIFICATION_PROFILE_DESTROYED, |
664 Source<Profile>(tab->profile())); | 667 Source<Profile>(tab->profile())); |
665 // Also start listening for changes in the accept languages. | 668 // Also start listening for changes in the accept languages. |
666 pref_change_registrar_.Add(prefs::kAcceptLanguages, this); | 669 pref_change_registrar_.Add(prefs::kAcceptLanguages, this); |
667 | 670 |
668 iter = accept_languages_.find(pref_service); | 671 iter = accept_languages_.find(pref_service); |
669 } | 672 } |
670 | 673 |
671 return iter->second.count(language) != 0; | 674 return iter->second.count(language) != 0; |
672 } | 675 } |
673 | 676 |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
773 return NULL; | 776 return NULL; |
774 | 777 |
775 for (size_t i = 0; i < wrapper->infobar_count(); ++i) { | 778 for (size_t i = 0; i < wrapper->infobar_count(); ++i) { |
776 TranslateInfoBarDelegate* delegate = | 779 TranslateInfoBarDelegate* delegate = |
777 wrapper->GetInfoBarDelegateAt(i)->AsTranslateInfoBarDelegate(); | 780 wrapper->GetInfoBarDelegateAt(i)->AsTranslateInfoBarDelegate(); |
778 if (delegate) | 781 if (delegate) |
779 return delegate; | 782 return delegate; |
780 } | 783 } |
781 return NULL; | 784 return NULL; |
782 } | 785 } |
OLD | NEW |