Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / components / translate / core / browser / translate_download_manager.h
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 #ifndef COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_DOWNLOAD_MANAGER_H_
6 #define COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_DOWNLOAD_MANAGER_H_
7
8 #include <string>
9
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"
15
16 template <typename T> struct DefaultSingletonTraits;
17
18 class PrefService;
19
20 // Manages the downloaded resources for Translate, such as the translate script
21 // and the language list.
22 class TranslateDownloadManager {
23  public:
24   // Returns the singleton instance.
25   static TranslateDownloadManager* GetInstance();
26
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;
32   }
33
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;
39   }
40
41   // The language list.
42   TranslateLanguageList* language_list() { return language_list_.get(); }
43
44   // The translate script.
45   TranslateScript* script() { return script_.get(); }
46
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);
51
52   // Fetches the language list from the translate server.
53   static void RequestLanguageList();
54
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);
58
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();
62
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);
67
68   // Returns true if |language| is supported by the translation server.
69   static bool IsSupportedLanguage(const std::string& language);
70
71   // Returns true if |language| is supported by the translation server as an
72   // alpha language.
73   static bool IsAlphaLanguage(const std::string& language);
74
75   // Must be called to shut Translate down. Cancels any pending fetches.
76   void Shutdown();
77
78   // Clears the translate script, so it will be fetched next time we translate.
79   void ClearTranslateScriptForTesting();
80
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);
85
86  private:
87   friend struct DefaultSingletonTraits<TranslateDownloadManager>;
88   TranslateDownloadManager();
89   virtual ~TranslateDownloadManager();
90
91   scoped_ptr<TranslateLanguageList> language_list_;
92
93   // An instance of TranslateScript which manages JavaScript source for
94   // Translate.
95   scoped_ptr<TranslateScript> script_;
96
97   std::string application_locale_;
98   scoped_refptr<net::URLRequestContextGetter> request_context_;
99 };
100
101 #endif  // COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_DOWNLOAD_MANAGER_H_