| 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" |
| (...skipping 308 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 319 // 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, |
| 320 // 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 |
| 321 // notification and update the visual display accordingly. | 321 // notification and update the visual display accordingly. |
| 322 TabContents* tab = Source<TabContents>(source).ptr(); | 322 TabContents* tab = Source<TabContents>(source).ptr(); |
| 323 PageTranslatedDetails* page_translated_details = | 323 PageTranslatedDetails* page_translated_details = |
| 324 Details<PageTranslatedDetails>(details).ptr(); | 324 Details<PageTranslatedDetails>(details).ptr(); |
| 325 PageTranslated(tab, page_translated_details); | 325 PageTranslated(tab, page_translated_details); |
| 326 break; | 326 break; |
| 327 } | 327 } |
| 328 case chrome::NOTIFICATION_PROFILE_DESTROYED: { | 328 case chrome::NOTIFICATION_PROFILE_DESTROYED: { |
| 329 Profile* profile = Source<Profile>(source).ptr(); | 329 PrefService* pref_service = Source<Profile>(source).ptr()->GetPrefs(); |
| 330 notification_registrar_.Remove(this, | 330 notification_registrar_.Remove(this, |
| 331 chrome::NOTIFICATION_PROFILE_DESTROYED, | 331 chrome::NOTIFICATION_PROFILE_DESTROYED, |
| 332 source); | 332 source); |
| 333 size_t count = accept_languages_.erase(profile->GetPrefs()); | 333 size_t count = accept_languages_.erase(pref_service); |
| 334 // We should know about this profile since we are listening for | 334 // We should know about this profile since we are listening for |
| 335 // notifications on it. | 335 // notifications on it. |
| 336 DCHECK(count > 0); | 336 DCHECK(count == 1u); |
| 337 pref_change_registrar_.Remove(prefs::kAcceptLanguages, this); | 337 PrefChangeRegistrar* pref_change_registrar = |
| 338 pref_change_registrars_[pref_service]; |
| 339 count = pref_change_registrars_.erase(pref_service); |
| 340 DCHECK(count == 1u); |
| 341 delete pref_change_registrar; |
| 338 break; | 342 break; |
| 339 } | 343 } |
| 340 case chrome::NOTIFICATION_PREF_CHANGED: { | 344 case chrome::NOTIFICATION_PREF_CHANGED: { |
| 341 DCHECK(*Details<std::string>(details).ptr() == prefs::kAcceptLanguages); | 345 DCHECK(*Details<std::string>(details).ptr() == prefs::kAcceptLanguages); |
| 342 PrefService* prefs = Source<PrefService>(source).ptr(); | 346 PrefService* prefs = Source<PrefService>(source).ptr(); |
| 343 InitAcceptLanguages(prefs); | 347 InitAcceptLanguages(prefs); |
| 344 break; | 348 break; |
| 345 } | 349 } |
| 346 default: | 350 default: |
| 347 NOTREACHED(); | 351 NOTREACHED(); |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 436 NotificationService::AllSources()); | 440 NotificationService::AllSources()); |
| 437 } | 441 } |
| 438 | 442 |
| 439 void TranslateManager::InitiateTranslation(TabContents* tab, | 443 void TranslateManager::InitiateTranslation(TabContents* tab, |
| 440 const std::string& page_lang) { | 444 const std::string& page_lang) { |
| 441 Profile* profile = Profile::FromBrowserContext(tab->browser_context()); | 445 Profile* profile = Profile::FromBrowserContext(tab->browser_context()); |
| 442 PrefService* prefs = profile->GetOriginalProfile()->GetPrefs(); | 446 PrefService* prefs = profile->GetOriginalProfile()->GetPrefs(); |
| 443 if (!prefs->GetBoolean(prefs::kEnableTranslate)) | 447 if (!prefs->GetBoolean(prefs::kEnableTranslate)) |
| 444 return; | 448 return; |
| 445 | 449 |
| 446 pref_change_registrar_.Init(prefs); | |
| 447 | |
| 448 // Allow disabling of translate from the command line to assist with | 450 // Allow disabling of translate from the command line to assist with |
| 449 // automated browser testing. | 451 // automated browser testing. |
| 450 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kDisableTranslate)) | 452 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kDisableTranslate)) |
| 451 return; | 453 return; |
| 452 | 454 |
| 453 NavigationEntry* entry = tab->controller().GetActiveEntry(); | 455 NavigationEntry* entry = tab->controller().GetActiveEntry(); |
| 454 if (!entry) { | 456 if (!entry) { |
| 455 // This can happen for popups created with window.open(""). | 457 // This can happen for popups created with window.open(""). |
| 456 return; | 458 return; |
| 457 } | 459 } |
| (...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 655 PrefService* pref_service = profile->GetPrefs(); | 657 PrefService* pref_service = profile->GetPrefs(); |
| 656 PrefServiceLanguagesMap::const_iterator iter = | 658 PrefServiceLanguagesMap::const_iterator iter = |
| 657 accept_languages_.find(pref_service); | 659 accept_languages_.find(pref_service); |
| 658 if (iter == accept_languages_.end()) { | 660 if (iter == accept_languages_.end()) { |
| 659 InitAcceptLanguages(pref_service); | 661 InitAcceptLanguages(pref_service); |
| 660 // Listen for this profile going away, in which case we would need to clear | 662 // Listen for this profile going away, in which case we would need to clear |
| 661 // the accepted languages for the profile. | 663 // the accepted languages for the profile. |
| 662 notification_registrar_.Add(this, chrome::NOTIFICATION_PROFILE_DESTROYED, | 664 notification_registrar_.Add(this, chrome::NOTIFICATION_PROFILE_DESTROYED, |
| 663 Source<Profile>(profile)); | 665 Source<Profile>(profile)); |
| 664 // Also start listening for changes in the accept languages. | 666 // Also start listening for changes in the accept languages. |
| 665 pref_change_registrar_.Add(prefs::kAcceptLanguages, this); | 667 DCHECK(pref_change_registrars_.find(pref_service) == |
| 668 pref_change_registrars_.end()); |
| 669 PrefChangeRegistrar* pref_change_registrar = new PrefChangeRegistrar; |
| 670 pref_change_registrar->Init(pref_service); |
| 671 pref_change_registrar->Add(prefs::kAcceptLanguages, this); |
| 672 pref_change_registrars_[pref_service] = pref_change_registrar; |
| 666 | 673 |
| 667 iter = accept_languages_.find(pref_service); | 674 iter = accept_languages_.find(pref_service); |
| 668 } | 675 } |
| 669 | 676 |
| 670 return iter->second.count(language) != 0; | 677 return iter->second.count(language) != 0; |
| 671 } | 678 } |
| 672 | 679 |
| 673 void TranslateManager::InitAcceptLanguages(PrefService* prefs) { | 680 void TranslateManager::InitAcceptLanguages(PrefService* prefs) { |
| 674 // We have been asked for this profile, build the languages. | 681 // We have been asked for this profile, build the languages. |
| 675 std::string accept_langs_str = prefs->GetString(prefs::kAcceptLanguages); | 682 std::string accept_langs_str = prefs->GetString(prefs::kAcceptLanguages); |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 791 return NULL; | 798 return NULL; |
| 792 | 799 |
| 793 for (size_t i = 0; i < wrapper->infobar_count(); ++i) { | 800 for (size_t i = 0; i < wrapper->infobar_count(); ++i) { |
| 794 TranslateInfoBarDelegate* delegate = | 801 TranslateInfoBarDelegate* delegate = |
| 795 wrapper->GetInfoBarDelegateAt(i)->AsTranslateInfoBarDelegate(); | 802 wrapper->GetInfoBarDelegateAt(i)->AsTranslateInfoBarDelegate(); |
| 796 if (delegate) | 803 if (delegate) |
| 797 return delegate; | 804 return delegate; |
| 798 } | 805 } |
| 799 return NULL; | 806 return NULL; |
| 800 } | 807 } |
| OLD | NEW |