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