This source file includes following definitions.
- Initialize
- Shutdown
- InitializeForTesting
- ShutdownForTesting
- OnResourceRequestsAllowed
- IsTranslateBubbleEnabled
- SetUseInfobar
- GetTargetLanguage
#include "chrome/browser/translate/translate_service.h"
#include "base/command_line.h"
#include "base/logging.h"
#include "base/prefs/pref_service.h"
#include "base/strings/string_split.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/translate/translate_manager.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/pref_names.h"
#include "components/translate/core/browser/translate_download_manager.h"
namespace {
TranslateService* g_translate_service = NULL;
}
TranslateService::TranslateService() : use_infobar_(false) {
resource_request_allowed_notifier_.Init(this);
}
TranslateService::~TranslateService() {}
void TranslateService::Initialize() {
if (g_translate_service)
return;
g_translate_service = new TranslateService;
g_translate_service->OnResourceRequestsAllowed();
TranslateDownloadManager* download_manager =
TranslateDownloadManager::GetInstance();
download_manager->set_request_context(
g_browser_process->system_request_context());
download_manager->set_application_locale(
g_browser_process->GetApplicationLocale());
}
void TranslateService::Shutdown(bool cleanup_pending_fetcher) {
TranslateDownloadManager* download_manager =
TranslateDownloadManager::GetInstance();
if (cleanup_pending_fetcher) {
download_manager->Shutdown();
} else {
download_manager->set_request_context(NULL);
}
}
void TranslateService::InitializeForTesting() {
if (!g_translate_service) {
TranslateService::Initialize();
} else {
TranslateDownloadManager::GetInstance()->ResetForTesting();
g_translate_service->OnResourceRequestsAllowed();
}
}
void TranslateService::ShutdownForTesting() {
TranslateDownloadManager::GetInstance()->Shutdown();
}
void TranslateService::OnResourceRequestsAllowed() {
TranslateLanguageList* language_list =
TranslateDownloadManager::GetInstance()->language_list();
if (!language_list) {
NOTREACHED();
return;
}
language_list->SetResourceRequestsAllowed(
resource_request_allowed_notifier_.ResourceRequestsAllowed());
}
bool TranslateService::IsTranslateBubbleEnabled() {
#if defined(USE_AURA)
Initialize();
return !g_translate_service->use_infobar_;
#elif defined(OS_MACOSX)
return CommandLine::ForCurrentProcess()->HasSwitch(
switches::kEnableTranslateNewUX);
#else
return false;
#endif
}
void TranslateService::SetUseInfobar(bool value) {
Initialize();
g_translate_service->use_infobar_ = value;
}
std::string TranslateService::GetTargetLanguage(PrefService* prefs) {
std::vector<std::string> accept_languages_list;
base::SplitString(prefs->GetString(prefs::kAcceptLanguages), ',',
&accept_languages_list);
return TranslateManager::GetTargetLanguage(accept_languages_list);
}