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 #ifndef COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_DOWNLOAD_MANAGER_H_
6 #define COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_DOWNLOAD_MANAGER_H_
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "components/translate/core/browser/translate_language_list.h"
13 #include "components/translate/core/browser/translate_script.h"
14 #include "net/url_request/url_request_context_getter.h"
16 template <typename T> struct DefaultSingletonTraits;
20 // Manages the downloaded resources for Translate, such as the translate script
21 // and the language list.
22 class TranslateDownloadManager {
24 // Returns the singleton instance.
25 static TranslateDownloadManager* GetInstance();
27 // The request context used to download the resources.
28 // Should be set before this class can be used.
29 net::URLRequestContextGetter* request_context() { return request_context_; }
30 void set_request_context(net::URLRequestContextGetter* context) {
31 request_context_ = context;
34 // The application locale.
35 // Should be set before this class can be used.
36 const std::string& application_locale() { return application_locale_; }
37 void set_application_locale(const std::string& locale) {
38 application_locale_ = locale;
42 TranslateLanguageList* language_list() { return language_list_.get(); }
44 // The translate script.
45 TranslateScript* script() { return script_.get(); }
47 // Let the caller decide if and when we should fetch the language list from
48 // the translate server. This is a NOOP if switches::kDisableTranslate is set
49 // or if prefs::kEnableTranslate is set to false.
50 static void RequestLanguageList(PrefService* prefs);
52 // Fetches the language list from the translate server.
53 static void RequestLanguageList();
55 // Fills |languages| with the list of languages that the translate server can
56 // translate to and from.
57 static void GetSupportedLanguages(std::vector<std::string>* languages);
59 // Returns the last-updated time when Chrome received a language list from a
60 // Translate server. Returns null time if Chrome hasn't received any lists.
61 static base::Time GetSupportedLanguagesLastUpdated();
63 // Returns the language code that can be used with the Translate method for a
64 // specified |language|. (ex. GetLanguageCode("en-US") will return "en", and
65 // GetLanguageCode("zh-CN") returns "zh-CN")
66 static std::string GetLanguageCode(const std::string& language);
68 // Returns true if |language| is supported by the translation server.
69 static bool IsSupportedLanguage(const std::string& language);
71 // Returns true if |language| is supported by the translation server as an
73 static bool IsAlphaLanguage(const std::string& language);
75 // Must be called to shut Translate down. Cancels any pending fetches.
78 // Clears the translate script, so it will be fetched next time we translate.
79 void ClearTranslateScriptForTesting();
81 // Used by unit-tests to override some defaults:
82 // Delay after which the translate script is fetched again from the
83 // translation server.
84 void SetTranslateScriptExpirationDelay(int delay_ms);
87 friend struct DefaultSingletonTraits<TranslateDownloadManager>;
88 TranslateDownloadManager();
89 virtual ~TranslateDownloadManager();
91 scoped_ptr<TranslateLanguageList> language_list_;
93 // An instance of TranslateScript which manages JavaScript source for
95 scoped_ptr<TranslateScript> script_;
97 std::string application_locale_;
98 scoped_refptr<net::URLRequestContextGetter> request_context_;
101 #endif // COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_DOWNLOAD_MANAGER_H_