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.
5 #include "ios/chrome/browser/translate/translate_accept_languages_factory.h"
7 #include "base/prefs/pref_service.h"
8 #include "components/keyed_service/core/keyed_service.h"
9 #include "components/keyed_service/ios/browser_state_dependency_manager.h"
10 #include "components/translate/core/browser/translate_accept_languages.h"
11 #include "ios/chrome/browser/browser_state/browser_state_otr_helper.h"
12 #include "ios/chrome/browser/pref_names.h"
13 #include "ios/public/provider/chrome/browser/browser_state/chrome_browser_state.h"
17 // TranslateAcceptLanguagesService is a thin container for
18 // TranslateAcceptLanguages to enable associating it with a BrowserState.
19 class TranslateAcceptLanguagesService
: public KeyedService
{
21 explicit TranslateAcceptLanguagesService(PrefService
* prefs
);
22 ~TranslateAcceptLanguagesService() override
;
24 // Returns the associated TranslateAcceptLanguages.
25 translate::TranslateAcceptLanguages
& accept_languages() {
26 return accept_languages_
;
30 translate::TranslateAcceptLanguages accept_languages_
;
31 DISALLOW_COPY_AND_ASSIGN(TranslateAcceptLanguagesService
);
34 TranslateAcceptLanguagesService::TranslateAcceptLanguagesService(
36 : accept_languages_(prefs
, ios::prefs::kAcceptLanguages
) {
39 TranslateAcceptLanguagesService::~TranslateAcceptLanguagesService() {
45 TranslateAcceptLanguagesFactory
*
46 TranslateAcceptLanguagesFactory::GetInstance() {
47 return Singleton
<TranslateAcceptLanguagesFactory
>::get();
51 translate::TranslateAcceptLanguages
*
52 TranslateAcceptLanguagesFactory::GetForBrowserState(web::BrowserState
* state
) {
53 TranslateAcceptLanguagesService
* service
=
54 static_cast<TranslateAcceptLanguagesService
*>(
55 GetInstance()->GetServiceForBrowserState(state
, true));
56 return &service
->accept_languages();
59 TranslateAcceptLanguagesFactory::TranslateAcceptLanguagesFactory()
60 : BrowserStateKeyedServiceFactory(
61 "TranslateAcceptLanguagesService",
62 BrowserStateDependencyManager::GetInstance()) {
65 TranslateAcceptLanguagesFactory::~TranslateAcceptLanguagesFactory() {
68 KeyedService
* TranslateAcceptLanguagesFactory::BuildServiceInstanceFor(
69 web::BrowserState
* browser_state
) const {
70 ios::ChromeBrowserState
* chrome_browser_state
=
71 ios::ChromeBrowserState::FromBrowserState(browser_state
);
72 return new TranslateAcceptLanguagesService(chrome_browser_state
->GetPrefs());
75 web::BrowserState
* TranslateAcceptLanguagesFactory::GetBrowserStateToUse(
76 web::BrowserState
* state
) const {
77 return GetBrowserStateRedirectedInIncognito(state
);