Home | History | Annotate | Download | only in translate
      1 // Copyright 2013 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 CHROME_BROWSER_TRANSLATE_TRANSLATE_LANGUAGE_LIST_H_
      6 #define CHROME_BROWSER_TRANSLATE_TRANSLATE_LANGUAGE_LIST_H_
      7 
      8 #include <set>
      9 #include <string>
     10 #include <vector>
     11 
     12 #include "base/memory/scoped_ptr.h"
     13 #include "base/time/time.h"
     14 #include "chrome/browser/web_resource/resource_request_allowed_notifier.h"
     15 
     16 class TranslateURLFetcher;
     17 
     18 // The TranslateLanguageList class is responsible for maintaining the latest
     19 // supporting language list.
     20 // This class is defined to be owned only by TranslateManager.
     21 class TranslateLanguageList : public ResourceRequestAllowedNotifier::Observer {
     22  public:
     23   static const int kFetcherId = 1;
     24 
     25   TranslateLanguageList();
     26   virtual ~TranslateLanguageList();
     27 
     28   // Returns the last-updated time when the language list is fetched from the
     29   // Translate server. Returns null time if the list is yet to be fetched.
     30   base::Time last_updated() { return last_updated_; }
     31 
     32   // Fills |languages| with the list of languages that the translate server can
     33   // translate to and from. |languages| will include alpha languages.
     34   void GetSupportedLanguages(std::vector<std::string>* languages);
     35 
     36   // Returns the language code that can be used with the Translate method for a
     37   // specified |chrome_locale|.
     38   std::string GetLanguageCode(const std::string& chrome_locale);
     39 
     40   // Returns true if |language| is supported by the translation server. It also
     41   // returns true against alpha languages.
     42   bool IsSupportedLanguage(const std::string& language);
     43 
     44   // Returns true if |language| is supported by the translation server as a
     45   // alpha language.
     46   bool IsAlphaLanguage(const std::string& language);
     47 
     48   // Fetches the language list from the translate server. It will retry
     49   // automatically when a server return 5xx errors and retry count doesn't
     50   // reach to limits.
     51   void RequestLanguageList();
     52 
     53   // ResourceRequestAllowedNotifier::Observer implementation:
     54   virtual void OnResourceRequestsAllowed() OVERRIDE;
     55 
     56   // Disables the language list updater. This is used only for testing now.
     57   static void DisableUpdate();
     58 
     59   // static const values shared with our browser tests.
     60   static const char kLanguageListCallbackName[];
     61   static const char kTargetLanguagesKey[];
     62   static const char kAlphaLanguagesKey[];
     63 
     64  private:
     65   // Callback function called when TranslateURLFetcher::Request() is finished.
     66   void OnLanguageListFetchComplete(int id,
     67                                    bool success,
     68                                    const std::string& data);
     69 
     70   // All the languages supported by the translation server.
     71   std::set<std::string> all_supported_languages_;
     72 
     73   // Alpha languages supported by the translation server.
     74   std::set<std::string> alpha_languages_;
     75 
     76   // A LanguageListFetcher instance to fetch a server providing supported
     77   // language list including alpha languages.
     78   scoped_ptr<TranslateURLFetcher> language_list_fetcher_;
     79 
     80   // The last-updated time when the language list is sent.
     81   base::Time last_updated_;
     82 
     83   // Helper class to know if it's allowed to make network resource requests.
     84   ResourceRequestAllowedNotifier resource_request_allowed_notifier_;
     85 
     86   DISALLOW_COPY_AND_ASSIGN(TranslateLanguageList);
     87 };
     88 
     89 #endif  // CHROME_BROWSER_TRANSLATE_TRANSLATE_LANGUAGE_LIST_H_
     90