Home | History | Annotate | Download | only in browser
      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 namespace translate {
     21 
     22 // Manages the downloaded resources for Translate, such as the translate script
     23 // and the language list.
     24 class TranslateDownloadManager {
     25  public:
     26   // Returns the singleton instance.
     27   static TranslateDownloadManager* GetInstance();
     28 
     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();
     33   }
     34   void set_request_context(net::URLRequestContextGetter* context) {
     35       request_context_ = context;
     36   }
     37 
     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;
     43   }
     44 
     45   // The language list.
     46   TranslateLanguageList* language_list() { return language_list_.get(); }
     47 
     48   // The translate script.
     49   TranslateScript* script() { return script_.get(); }
     50 
     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);
     55 
     56   // Fetches the language list from the translate server.
     57   static void RequestLanguageList();
     58 
     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);
     62 
     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();
     66 
     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);
     71 
     72   // Returns true if |language| is supported by the translation server.
     73   static bool IsSupportedLanguage(const std::string& language);
     74 
     75   // Returns true if |language| is supported by the translation server as an
     76   // alpha language.
     77   static bool IsAlphaLanguage(const std::string& language);
     78 
     79   // Must be called to shut Translate down. Cancels any pending fetches.
     80   void Shutdown();
     81 
     82   // Clears the translate script, so it will be fetched next time we translate.
     83   void ClearTranslateScriptForTesting();
     84 
     85   // Resets to its initial state as if newly created.
     86   void ResetForTesting();
     87 
     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);
     92 
     93  private:
     94   friend struct DefaultSingletonTraits<TranslateDownloadManager>;
     95   TranslateDownloadManager();
     96   virtual ~TranslateDownloadManager();
     97 
     98   scoped_ptr<TranslateLanguageList> language_list_;
     99 
    100   // An instance of TranslateScript which manages JavaScript source for
    101   // Translate.
    102   scoped_ptr<TranslateScript> script_;
    103 
    104   std::string application_locale_;
    105   scoped_refptr<net::URLRequestContextGetter> request_context_;
    106 };
    107 
    108 }  // namespace translate
    109 
    110 #endif  // COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_DOWNLOAD_MANAGER_H_
    111