Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(65)

Side by Side Diff: chrome/browser/translate/translate_manager.cc

Issue 3304015: Use PrefChangeRegistrar everywhere (Closed)
Patch Set: final version for commit Created 10 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 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 "app/resource_bundle.h" 7 #include "app/resource_bundle.h"
8 #include "base/command_line.h" 8 #include "base/command_line.h"
9 #include "base/compiler_specific.h" 9 #include "base/compiler_specific.h"
10 #include "base/histogram.h" 10 #include "base/histogram.h"
(...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after
242 break; 242 break;
243 } 243 }
244 case NotificationType::PROFILE_DESTROYED: { 244 case NotificationType::PROFILE_DESTROYED: {
245 Profile* profile = Source<Profile>(source).ptr(); 245 Profile* profile = Source<Profile>(source).ptr();
246 notification_registrar_.Remove(this, NotificationType::PROFILE_DESTROYED, 246 notification_registrar_.Remove(this, NotificationType::PROFILE_DESTROYED,
247 source); 247 source);
248 size_t count = accept_languages_.erase(profile->GetPrefs()); 248 size_t count = accept_languages_.erase(profile->GetPrefs());
249 // We should know about this profile since we are listening for 249 // We should know about this profile since we are listening for
250 // notifications on it. 250 // notifications on it.
251 DCHECK(count > 0); 251 DCHECK(count > 0);
252 profile->GetPrefs()->RemovePrefObserver(prefs::kAcceptLanguages, this); 252 pref_change_registrar_.Remove(prefs::kAcceptLanguages, this);
253 break; 253 break;
254 } 254 }
255 case NotificationType::PREF_CHANGED: { 255 case NotificationType::PREF_CHANGED: {
256 DCHECK(*Details<std::string>(details).ptr() == prefs::kAcceptLanguages); 256 DCHECK(*Details<std::string>(details).ptr() == prefs::kAcceptLanguages);
257 PrefService* prefs = Source<PrefService>(source).ptr(); 257 PrefService* prefs = Source<PrefService>(source).ptr();
258 InitAcceptLanguages(prefs); 258 InitAcceptLanguages(prefs);
259 break; 259 break;
260 } 260 }
261 default: 261 default:
262 NOTREACHED(); 262 NOTREACHED();
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
335 notification_registrar_.Add(this, NotificationType::PAGE_TRANSLATED, 335 notification_registrar_.Add(this, NotificationType::PAGE_TRANSLATED,
336 NotificationService::AllSources()); 336 NotificationService::AllSources());
337 } 337 }
338 338
339 void TranslateManager::InitiateTranslation(TabContents* tab, 339 void TranslateManager::InitiateTranslation(TabContents* tab,
340 const std::string& page_lang) { 340 const std::string& page_lang) {
341 PrefService* prefs = tab->profile()->GetPrefs(); 341 PrefService* prefs = tab->profile()->GetPrefs();
342 if (!prefs->GetBoolean(prefs::kEnableTranslate)) 342 if (!prefs->GetBoolean(prefs::kEnableTranslate))
343 return; 343 return;
344 344
345 pref_change_registrar_.Init(prefs);
346
345 // Allow disabling of translate from the command line to assist with 347 // Allow disabling of translate from the command line to assist with
346 // automated browser testing. 348 // automated browser testing.
347 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kDisableTranslate)) 349 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kDisableTranslate))
348 return; 350 return;
349 351
350 NavigationEntry* entry = tab->controller().GetActiveEntry(); 352 NavigationEntry* entry = tab->controller().GetActiveEntry();
351 if (!entry) { 353 if (!entry) {
352 // This can happen for popups created with window.open(""). 354 // This can happen for popups created with window.open("").
353 return; 355 return;
354 } 356 }
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after
527 PrefService* pref_service = tab->profile()->GetPrefs(); 529 PrefService* pref_service = tab->profile()->GetPrefs();
528 PrefServiceLanguagesMap::const_iterator iter = 530 PrefServiceLanguagesMap::const_iterator iter =
529 accept_languages_.find(pref_service); 531 accept_languages_.find(pref_service);
530 if (iter == accept_languages_.end()) { 532 if (iter == accept_languages_.end()) {
531 InitAcceptLanguages(pref_service); 533 InitAcceptLanguages(pref_service);
532 // Listen for this profile going away, in which case we would need to clear 534 // Listen for this profile going away, in which case we would need to clear
533 // the accepted languages for the profile. 535 // the accepted languages for the profile.
534 notification_registrar_.Add(this, NotificationType::PROFILE_DESTROYED, 536 notification_registrar_.Add(this, NotificationType::PROFILE_DESTROYED,
535 Source<Profile>(tab->profile())); 537 Source<Profile>(tab->profile()));
536 // Also start listening for changes in the accept languages. 538 // Also start listening for changes in the accept languages.
537 tab->profile()->GetPrefs()->AddPrefObserver(prefs::kAcceptLanguages, this); 539 pref_change_registrar_.Add(prefs::kAcceptLanguages, this);
538 540
539 iter = accept_languages_.find(pref_service); 541 iter = accept_languages_.find(pref_service);
540 } 542 }
541 543
542 return iter->second.count(language) != 0; 544 return iter->second.count(language) != 0;
543 } 545 }
544 546
545 void TranslateManager::InitAcceptLanguages(PrefService* prefs) { 547 void TranslateManager::InitAcceptLanguages(PrefService* prefs) {
546 // We have been asked for this profile, build the languages. 548 // We have been asked for this profile, build the languages.
547 std::string accept_langs_str = prefs->GetString(prefs::kAcceptLanguages); 549 std::string accept_langs_str = prefs->GetString(prefs::kAcceptLanguages);
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
610 TranslateInfoBarDelegate* TranslateManager::GetTranslateInfoBarDelegate( 612 TranslateInfoBarDelegate* TranslateManager::GetTranslateInfoBarDelegate(
611 TabContents* tab) { 613 TabContents* tab) {
612 for (int i = 0; i < tab->infobar_delegate_count(); ++i) { 614 for (int i = 0; i < tab->infobar_delegate_count(); ++i) {
613 TranslateInfoBarDelegate* delegate = 615 TranslateInfoBarDelegate* delegate =
614 tab->GetInfoBarDelegateAt(i)->AsTranslateInfoBarDelegate(); 616 tab->GetInfoBarDelegateAt(i)->AsTranslateInfoBarDelegate();
615 if (delegate) 617 if (delegate)
616 return delegate; 618 return delegate;
617 } 619 }
618 return NULL; 620 return NULL;
619 } 621 }
OLDNEW
« no previous file with comments | « chrome/browser/translate/translate_manager.h ('k') | chrome/browser/translate/translate_manager_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698