Home | History | Annotate | Download | only in browser
      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 COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_LANGUAGE_LIST_H_
      6 #define COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_LANGUAGE_LIST_H_
      7 
      8 #include <set>
      9 #include <string>
     10 #include <vector>
     11 
     12 #include "base/callback_list.h"
     13 #include "base/memory/scoped_ptr.h"
     14 #include "base/time/time.h"
     15 
     16 class GURL;
     17 
     18 namespace translate {
     19 
     20 struct TranslateEventDetails;
     21 class TranslateURLFetcher;
     22 
     23 // The TranslateLanguageList class is responsible for maintaining the latest
     24 // supporting language list.
     25 class TranslateLanguageList {
     26  public:
     27   static const int kFetcherId = 1;
     28 
     29   TranslateLanguageList();
     30   virtual ~TranslateLanguageList();
     31 
     32   // Returns the last-updated time when the language list is fetched from the
     33   // Translate server. Returns null time if the list is yet to be fetched.
     34   base::Time last_updated() { return last_updated_; }
     35 
     36   // Fills |languages| with the list of languages that the translate server can
     37   // translate to and from. |languages| will include alpha languages.
     38   void GetSupportedLanguages(std::vector<std::string>* languages);
     39 
     40   // Returns the language code that can be used with the Translate method for a
     41   // specified |language|. (ex. GetLanguageCode("en-US") will return "en", and
     42   // GetLanguageCode("zh-CN") returns "zh-CN")
     43   std::string GetLanguageCode(const std::string& language);
     44 
     45   // Returns true if |language| is supported by the translation server. It also
     46   // returns true against alpha languages.
     47   bool IsSupportedLanguage(const std::string& language);
     48 
     49   // Returns true if |language| is supported by the translation server as a
     50   // alpha language.
     51   bool IsAlphaLanguage(const std::string& language);
     52 
     53   // Fetches the language list from the translate server if resource requests
     54   // are allowed, and otherwise keeps the request as pending until allowed.
     55   void RequestLanguageList();
     56 
     57   // Sets whether requests are allowed. If |allowed| is true, this resumes any
     58   // pending request.
     59   void SetResourceRequestsAllowed(bool allowed);
     60 
     61   typedef base::Callback<void(const TranslateEventDetails&)> EventCallback;
     62   typedef base::CallbackList<void(const TranslateEventDetails&)>
     63       EventCallbackList;
     64 
     65   // Registers a callback for translate events related to the language list,
     66   // such as updates and download errors.
     67   scoped_ptr<EventCallbackList::Subscription> RegisterEventCallback(
     68       const EventCallback& callback);
     69 
     70   // Disables the language list updater. This is used only for testing now.
     71   static void DisableUpdate();
     72 
     73   // static const values shared with our browser tests.
     74   static const char kLanguageListCallbackName[];
     75   static const char kTargetLanguagesKey[];
     76   static const char kAlphaLanguagesKey[];
     77 
     78  private:
     79   // Callback function called when TranslateURLFetcher::Request() is finished.
     80   void OnLanguageListFetchComplete(int id,
     81                                    bool success,
     82                                    const std::string& data);
     83 
     84   // Notifies the callback list of a translate event.
     85   void NotifyEvent(int line, const std::string& message);
     86 
     87   // Parses |language_list| containing the list of languages that the translate
     88   // server can translate to and from.
     89   void SetSupportedLanguages(const std::string& language_list);
     90 
     91   // Returns the url from which to load the list of languages.
     92   GURL TranslateLanguageUrl();
     93 
     94   // Callbacks called on translate events.
     95   EventCallbackList callback_list_;
     96 
     97   // Whether the language list can be requested.
     98   bool resource_requests_allowed_;
     99 
    100   // True if the list has to be fetched when resource requests are allowed.
    101   bool request_pending_;
    102 
    103   // All the languages supported by the translation server.
    104   std::set<std::string> all_supported_languages_;
    105 
    106   // Alpha languages supported by the translation server.
    107   std::set<std::string> alpha_languages_;
    108 
    109   // A LanguageListFetcher instance to fetch a server providing supported
    110   // language list including alpha languages.
    111   scoped_ptr<TranslateURLFetcher> language_list_fetcher_;
    112 
    113   // The last-updated time when the language list is sent.
    114   base::Time last_updated_;
    115 
    116   DISALLOW_COPY_AND_ASSIGN(TranslateLanguageList);
    117 };
    118 
    119 }  // namespace translate
    120 
    121 #endif  // COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_LANGUAGE_LIST_H_
    122