| 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" |
| (...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after 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::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 LanguageState& language_state = | 304 LanguageState& language_state = |
| 304 wrapper->translate_tab_helper()->language_state(); | 305 wrapper->translate_tab_helper()->language_state(); |
| 305 if (language_state.page_translatable() && | 306 if (language_state.page_translatable() && |
| 306 !language_state.translation_pending() && | 307 !language_state.translation_pending() && |
| 307 !language_state.translation_declined() && | 308 !language_state.translation_declined() && |
| 308 !language_state.IsPageTranslated()) { | 309 !language_state.IsPageTranslated()) { |
| 309 std::string language = *(Details<std::string>(details).ptr()); | 310 std::string language = *(Details<std::string>(details).ptr()); |
| 310 InitiateTranslation(tab, language); | 311 InitiateTranslation(tab, language); |
| 311 } | 312 } |
| 312 break; | 313 break; |
| 313 } | 314 } |
| 314 case NotificationType::PAGE_TRANSLATED: { | 315 case chrome::PAGE_TRANSLATED: { |
| 315 // Only add translate infobar if it doesn't exist; if it already exists, | 316 // Only add translate infobar if it doesn't exist; if it already exists, |
| 316 // just update the state, the actual infobar would have received the same | 317 // just update the state, the actual infobar would have received the same |
| 317 // notification and update the visual display accordingly. | 318 // notification and update the visual display accordingly. |
| 318 TabContents* tab = Source<TabContents>(source).ptr(); | 319 TabContents* tab = Source<TabContents>(source).ptr(); |
| 319 PageTranslatedDetails* page_translated_details = | 320 PageTranslatedDetails* page_translated_details = |
| 320 Details<PageTranslatedDetails>(details).ptr(); | 321 Details<PageTranslatedDetails>(details).ptr(); |
| 321 PageTranslated(tab, page_translated_details); | 322 PageTranslated(tab, page_translated_details); |
| 322 break; | 323 break; |
| 323 } | 324 } |
| 324 case NotificationType::PROFILE_DESTROYED: { | 325 case chrome::PROFILE_DESTROYED: { |
| 325 Profile* profile = Source<Profile>(source).ptr(); | 326 Profile* profile = Source<Profile>(source).ptr(); |
| 326 notification_registrar_.Remove(this, NotificationType::PROFILE_DESTROYED, | 327 notification_registrar_.Remove(this, chrome::PROFILE_DESTROYED, |
| 327 source); | 328 source); |
| 328 size_t count = accept_languages_.erase(profile->GetPrefs()); | 329 size_t count = accept_languages_.erase(profile->GetPrefs()); |
| 329 // We should know about this profile since we are listening for | 330 // We should know about this profile since we are listening for |
| 330 // notifications on it. | 331 // notifications on it. |
| 331 DCHECK(count > 0); | 332 DCHECK(count > 0); |
| 332 pref_change_registrar_.Remove(prefs::kAcceptLanguages, this); | 333 pref_change_registrar_.Remove(prefs::kAcceptLanguages, this); |
| 333 break; | 334 break; |
| 334 } | 335 } |
| 335 case NotificationType::PREF_CHANGED: { | 336 case chrome::PREF_CHANGED: { |
| 336 DCHECK(*Details<std::string>(details).ptr() == prefs::kAcceptLanguages); | 337 DCHECK(*Details<std::string>(details).ptr() == prefs::kAcceptLanguages); |
| 337 PrefService* prefs = Source<PrefService>(source).ptr(); | 338 PrefService* prefs = Source<PrefService>(source).ptr(); |
| 338 InitAcceptLanguages(prefs); | 339 InitAcceptLanguages(prefs); |
| 339 break; | 340 break; |
| 340 } | 341 } |
| 341 default: | 342 default: |
| 342 NOTREACHED(); | 343 NOTREACHED(); |
| 343 } | 344 } |
| 344 } | 345 } |
| 345 | 346 |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 417 // static | 418 // static |
| 418 bool TranslateManager::IsShowingTranslateInfobar(TabContents* tab) { | 419 bool TranslateManager::IsShowingTranslateInfobar(TabContents* tab) { |
| 419 return GetTranslateInfoBarDelegate(tab) != NULL; | 420 return GetTranslateInfoBarDelegate(tab) != NULL; |
| 420 } | 421 } |
| 421 | 422 |
| 422 TranslateManager::TranslateManager() | 423 TranslateManager::TranslateManager() |
| 423 : ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)), | 424 : ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)), |
| 424 translate_script_expiration_delay_(kTranslateScriptExpirationDelayMS) { | 425 translate_script_expiration_delay_(kTranslateScriptExpirationDelayMS) { |
| 425 notification_registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED, | 426 notification_registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED, |
| 426 NotificationService::AllSources()); | 427 NotificationService::AllSources()); |
| 427 notification_registrar_.Add(this, NotificationType::TAB_LANGUAGE_DETERMINED, | 428 notification_registrar_.Add(this, chrome::TAB_LANGUAGE_DETERMINED, |
| 428 NotificationService::AllSources()); | 429 NotificationService::AllSources()); |
| 429 notification_registrar_.Add(this, NotificationType::PAGE_TRANSLATED, | 430 notification_registrar_.Add(this, chrome::PAGE_TRANSLATED, |
| 430 NotificationService::AllSources()); | 431 NotificationService::AllSources()); |
| 431 } | 432 } |
| 432 | 433 |
| 433 void TranslateManager::InitiateTranslation(TabContents* tab, | 434 void TranslateManager::InitiateTranslation(TabContents* tab, |
| 434 const std::string& page_lang) { | 435 const std::string& page_lang) { |
| 435 PrefService* prefs = tab->profile()->GetOriginalProfile()->GetPrefs(); | 436 PrefService* prefs = tab->profile()->GetOriginalProfile()->GetPrefs(); |
| 436 if (!prefs->GetBoolean(prefs::kEnableTranslate)) | 437 if (!prefs->GetBoolean(prefs::kEnableTranslate)) |
| 437 return; | 438 return; |
| 438 | 439 |
| 439 pref_change_registrar_.Init(prefs); | 440 pref_change_registrar_.Init(prefs); |
| (...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 646 | 647 |
| 647 bool TranslateManager::IsAcceptLanguage(TabContents* tab, | 648 bool TranslateManager::IsAcceptLanguage(TabContents* tab, |
| 648 const std::string& language) { | 649 const std::string& language) { |
| 649 PrefService* pref_service = tab->profile()->GetOriginalProfile()->GetPrefs(); | 650 PrefService* pref_service = tab->profile()->GetOriginalProfile()->GetPrefs(); |
| 650 PrefServiceLanguagesMap::const_iterator iter = | 651 PrefServiceLanguagesMap::const_iterator iter = |
| 651 accept_languages_.find(pref_service); | 652 accept_languages_.find(pref_service); |
| 652 if (iter == accept_languages_.end()) { | 653 if (iter == accept_languages_.end()) { |
| 653 InitAcceptLanguages(pref_service); | 654 InitAcceptLanguages(pref_service); |
| 654 // Listen for this profile going away, in which case we would need to clear | 655 // Listen for this profile going away, in which case we would need to clear |
| 655 // the accepted languages for the profile. | 656 // the accepted languages for the profile. |
| 656 notification_registrar_.Add(this, NotificationType::PROFILE_DESTROYED, | 657 notification_registrar_.Add(this, chrome::PROFILE_DESTROYED, |
| 657 Source<Profile>(tab->profile())); | 658 Source<Profile>(tab->profile())); |
| 658 // Also start listening for changes in the accept languages. | 659 // Also start listening for changes in the accept languages. |
| 659 pref_change_registrar_.Add(prefs::kAcceptLanguages, this); | 660 pref_change_registrar_.Add(prefs::kAcceptLanguages, this); |
| 660 | 661 |
| 661 iter = accept_languages_.find(pref_service); | 662 iter = accept_languages_.find(pref_service); |
| 662 } | 663 } |
| 663 | 664 |
| 664 return iter->second.count(language) != 0; | 665 return iter->second.count(language) != 0; |
| 665 } | 666 } |
| 666 | 667 |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 761 TabContentsWrapper* wrapper = | 762 TabContentsWrapper* wrapper = |
| 762 TabContentsWrapper::GetCurrentWrapperForContents(tab); | 763 TabContentsWrapper::GetCurrentWrapperForContents(tab); |
| 763 for (size_t i = 0; i < wrapper->infobar_count(); ++i) { | 764 for (size_t i = 0; i < wrapper->infobar_count(); ++i) { |
| 764 TranslateInfoBarDelegate* delegate = | 765 TranslateInfoBarDelegate* delegate = |
| 765 wrapper->GetInfoBarDelegateAt(i)->AsTranslateInfoBarDelegate(); | 766 wrapper->GetInfoBarDelegateAt(i)->AsTranslateInfoBarDelegate(); |
| 766 if (delegate) | 767 if (delegate) |
| 767 return delegate; | 768 return delegate; |
| 768 } | 769 } |
| 769 return NULL; | 770 return NULL; |
| 770 } | 771 } |
| OLD | NEW |