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 "components/translate/core/browser/translate_download_manager.h" 6 7 #include "base/command_line.h" 8 #include "base/logging.h" 9 #include "base/memory/singleton.h" 10 #include "base/prefs/pref_service.h" 11 #include "components/translate/core/common/translate_pref_names.h" 12 #include "components/translate/core/common/translate_switches.h" 13 14 namespace translate { 15 16 // static GetInstance()17TranslateDownloadManager* TranslateDownloadManager::GetInstance() { 18 return Singleton<TranslateDownloadManager>::get(); 19 } 20 TranslateDownloadManager()21TranslateDownloadManager::TranslateDownloadManager() 22 : language_list_(new TranslateLanguageList), 23 script_(new TranslateScript) {} 24 ~TranslateDownloadManager()25TranslateDownloadManager::~TranslateDownloadManager() {} 26 Shutdown()27void TranslateDownloadManager::Shutdown() { 28 language_list_.reset(); 29 script_.reset(); 30 request_context_ = NULL; 31 } 32 33 // static RequestLanguageList()34void TranslateDownloadManager::RequestLanguageList() { 35 TranslateLanguageList* language_list = GetInstance()->language_list(); 36 if (!language_list) { 37 NOTREACHED(); 38 return; 39 } 40 41 language_list->RequestLanguageList(); 42 } 43 44 // static RequestLanguageList(PrefService * prefs)45void TranslateDownloadManager::RequestLanguageList(PrefService* prefs) { 46 // We don't want to do this when translate is disabled. 47 DCHECK(prefs != NULL); 48 if (CommandLine::ForCurrentProcess()->HasSwitch( 49 translate::switches::kDisableTranslate) || 50 !prefs->GetBoolean(prefs::kEnableTranslate)) { 51 return; 52 } 53 54 RequestLanguageList(); 55 } 56 57 // static GetSupportedLanguages(std::vector<std::string> * languages)58void TranslateDownloadManager::GetSupportedLanguages( 59 std::vector<std::string>* languages) { 60 TranslateLanguageList* language_list = GetInstance()->language_list(); 61 if (!language_list) { 62 NOTREACHED(); 63 return; 64 } 65 66 language_list->GetSupportedLanguages(languages); 67 } 68 69 // static GetSupportedLanguagesLastUpdated()70base::Time TranslateDownloadManager::GetSupportedLanguagesLastUpdated() { 71 TranslateLanguageList* language_list = GetInstance()->language_list(); 72 if (!language_list) { 73 NOTREACHED(); 74 return base::Time(); 75 } 76 77 return language_list->last_updated(); 78 } 79 80 // static GetLanguageCode(const std::string & language)81std::string TranslateDownloadManager::GetLanguageCode( 82 const std::string& language) { 83 TranslateLanguageList* language_list = GetInstance()->language_list(); 84 if (!language_list) { 85 NOTREACHED(); 86 return language; 87 } 88 89 return language_list->GetLanguageCode(language); 90 } 91 92 // static IsSupportedLanguage(const std::string & language)93bool TranslateDownloadManager::IsSupportedLanguage( 94 const std::string& language) { 95 TranslateLanguageList* language_list = GetInstance()->language_list(); 96 if (!language_list) { 97 NOTREACHED(); 98 return false; 99 } 100 101 return language_list->IsSupportedLanguage(language); 102 } 103 104 // static IsAlphaLanguage(const std::string & language)105bool TranslateDownloadManager::IsAlphaLanguage(const std::string& language) { 106 TranslateLanguageList* language_list = GetInstance()->language_list(); 107 if (!language_list) { 108 NOTREACHED(); 109 return false; 110 } 111 112 return language_list->IsAlphaLanguage(language); 113 } 114 ClearTranslateScriptForTesting()115void TranslateDownloadManager::ClearTranslateScriptForTesting() { 116 if (script_.get() == NULL) { 117 NOTREACHED(); 118 return; 119 } 120 script_->Clear(); 121 } 122 ResetForTesting()123void TranslateDownloadManager::ResetForTesting() { 124 language_list_.reset(new TranslateLanguageList); 125 script_.reset(new TranslateScript); 126 } 127 SetTranslateScriptExpirationDelay(int delay_ms)128void TranslateDownloadManager::SetTranslateScriptExpirationDelay(int delay_ms) { 129 if (script_.get() == NULL) { 130 NOTREACHED(); 131 return; 132 } 133 script_->set_expiration_delay(delay_ms); 134 } 135 136 } // namespace translate 137