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