1 // Copyright 2014 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "chrome/browser/translate/translate_accept_languages_factory.h" 6 7 #include "base/prefs/pref_service.h" 8 #include "chrome/browser/profiles/incognito_helpers.h" 9 #include "chrome/browser/profiles/profile.h" 10 #include "chrome/common/pref_names.h" 11 #include "components/keyed_service/content/browser_context_dependency_manager.h" 12 #include "components/keyed_service/core/keyed_service.h" 13 #include "components/translate/core/browser/translate_accept_languages.h" 14 15 namespace { 16 17 // TranslateAcceptLanguagesService is a thin container for 18 // TranslateAcceptLanguages to enable associating it with a BrowserContext. 19 class TranslateAcceptLanguagesService : public KeyedService { 20 public: 21 explicit TranslateAcceptLanguagesService(PrefService* prefs); 22 virtual ~TranslateAcceptLanguagesService(); 23 24 // Returns the associated TranslateAcceptLanguages. accept_languages()25 translate::TranslateAcceptLanguages& accept_languages() { 26 return accept_languages_; 27 } 28 29 private: 30 translate::TranslateAcceptLanguages accept_languages_; 31 DISALLOW_COPY_AND_ASSIGN(TranslateAcceptLanguagesService); 32 }; 33 TranslateAcceptLanguagesService(PrefService * prefs)34TranslateAcceptLanguagesService::TranslateAcceptLanguagesService( 35 PrefService* prefs) 36 : accept_languages_(prefs, prefs::kAcceptLanguages) {} 37 ~TranslateAcceptLanguagesService()38TranslateAcceptLanguagesService::~TranslateAcceptLanguagesService() {} 39 40 } // namespace 41 42 // static 43 TranslateAcceptLanguagesFactory* GetInstance()44TranslateAcceptLanguagesFactory::GetInstance() { 45 return Singleton<TranslateAcceptLanguagesFactory>::get(); 46 } 47 48 // static 49 translate::TranslateAcceptLanguages* GetForBrowserContext(content::BrowserContext * context)50TranslateAcceptLanguagesFactory::GetForBrowserContext( 51 content::BrowserContext* context) { 52 TranslateAcceptLanguagesService* service = 53 static_cast<TranslateAcceptLanguagesService*>( 54 GetInstance()->GetServiceForBrowserContext(context, true)); 55 return &service->accept_languages(); 56 } 57 TranslateAcceptLanguagesFactory()58TranslateAcceptLanguagesFactory::TranslateAcceptLanguagesFactory() 59 : BrowserContextKeyedServiceFactory( 60 "TranslateAcceptLanguagesService", 61 BrowserContextDependencyManager::GetInstance()) {} 62 ~TranslateAcceptLanguagesFactory()63TranslateAcceptLanguagesFactory::~TranslateAcceptLanguagesFactory() {} 64 BuildServiceInstanceFor(content::BrowserContext * browser_context) const65KeyedService* TranslateAcceptLanguagesFactory::BuildServiceInstanceFor( 66 content::BrowserContext* browser_context) const { 67 Profile* profile = Profile::FromBrowserContext(browser_context); 68 return new TranslateAcceptLanguagesService(profile->GetPrefs()); 69 } 70 71 content::BrowserContext* GetBrowserContextToUse(content::BrowserContext * context) const72TranslateAcceptLanguagesFactory::GetBrowserContextToUse( 73 content::BrowserContext* context) const { 74 return chrome::GetBrowserContextRedirectedInIncognito(context); 75 } 76