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
;
22 // Manages the downloaded resources for Translate, such as the translate script
23 // and the language list.
24 class TranslateDownloadManager
{
26 // Returns the singleton instance.
27 static TranslateDownloadManager
* GetInstance();
29 // The request context used to download the resources.
30 // Should be set before this class can be used.
31 net::URLRequestContextGetter
* request_context() {
32 return request_context_
.get();
34 void set_request_context(net::URLRequestContextGetter
* context
) {
35 request_context_
= context
;
38 // The application locale.
39 // Should be set before this class can be used.
40 const std::string
& application_locale() { return application_locale_
; }
41 void set_application_locale(const std::string
& locale
) {
42 application_locale_
= locale
;
46 TranslateLanguageList
* language_list() { return language_list_
.get(); }
48 // The translate script.
49 TranslateScript
* script() { return script_
.get(); }
51 // Let the caller decide if and when we should fetch the language list from
52 // the translate server. This is a NOOP if switches::kDisableTranslate is set
53 // or if prefs::kEnableTranslate is set to false.
54 static void RequestLanguageList(PrefService
* prefs
);
56 // Fetches the language list from the translate server.
57 static void RequestLanguageList();
59 // Fills |languages| with the list of languages that the translate server can
60 // translate to and from.
61 static void GetSupportedLanguages(std::vector
<std::string
>* languages
);
63 // Returns the last-updated time when Chrome received a language list from a
64 // Translate server. Returns null time if Chrome hasn't received any lists.
65 static base::Time
GetSupportedLanguagesLastUpdated();
67 // Returns the language code that can be used with the Translate method for a
68 // specified |language|. (ex. GetLanguageCode("en-US") will return "en", and
69 // GetLanguageCode("zh-CN") returns "zh-CN")
70 static std::string
GetLanguageCode(const std::string
& language
);
72 // Returns true if |language| is supported by the translation server.
73 static bool IsSupportedLanguage(const std::string
& language
);
75 // Returns true if |language| is supported by the translation server as an
77 static bool IsAlphaLanguage(const std::string
& language
);
79 // Must be called to shut Translate down. Cancels any pending fetches.
82 // Clears the translate script, so it will be fetched next time we translate.
83 void ClearTranslateScriptForTesting();
85 // Resets to its initial state as if newly created.
86 void ResetForTesting();
88 // Used by unit-tests to override some defaults:
89 // Delay after which the translate script is fetched again from the
90 // translation server.
91 void SetTranslateScriptExpirationDelay(int delay_ms
);
94 friend struct DefaultSingletonTraits
<TranslateDownloadManager
>;
95 TranslateDownloadManager();
96 virtual ~TranslateDownloadManager();
98 scoped_ptr
<TranslateLanguageList
> language_list_
;
100 // An instance of TranslateScript which manages JavaScript source for
102 scoped_ptr
<TranslateScript
> script_
;
104 std::string application_locale_
;
105 scoped_refptr
<net::URLRequestContextGetter
> request_context_
;
108 } // namespace translate
110 #endif // COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_DOWNLOAD_MANAGER_H_