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.
5 #ifndef COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_LANGUAGE_LIST_H_
6 #define COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_LANGUAGE_LIST_H_
12 #include "base/callback_list.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/time/time.h"
16 struct TranslateEventDetails;
17 class TranslateURLFetcher;
19 // The TranslateLanguageList class is responsible for maintaining the latest
20 // supporting language list.
21 class TranslateLanguageList {
23 static const int kFetcherId = 1;
25 TranslateLanguageList();
26 virtual ~TranslateLanguageList();
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_; }
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);
36 // Returns the language code that can be used with the Translate method for a
37 // specified |language|. (ex. GetLanguageCode("en-US") will return "en", and
38 // GetLanguageCode("zh-CN") returns "zh-CN")
39 std::string GetLanguageCode(const std::string& language);
41 // Returns true if |language| is supported by the translation server. It also
42 // returns true against alpha languages.
43 bool IsSupportedLanguage(const std::string& language);
45 // Returns true if |language| is supported by the translation server as a
47 bool IsAlphaLanguage(const std::string& language);
49 // Fetches the language list from the translate server if resource requests
50 // are allowed, and otherwise keeps the request as pending until allowed.
51 void RequestLanguageList();
53 // Sets whether requests are allowed. If |allowed| is true, this resumes any
55 void SetResourceRequestsAllowed(bool allowed);
57 typedef base::Callback<void(const TranslateEventDetails&)> EventCallback;
58 typedef base::CallbackList<void(const TranslateEventDetails&)>
61 // Registers a callback for translate events related to the language list,
62 // such as updates and download errors.
63 scoped_ptr<EventCallbackList::Subscription> RegisterEventCallback(
64 const EventCallback& callback);
66 // Disables the language list updater. This is used only for testing now.
67 static void DisableUpdate();
69 // static const values shared with our browser tests.
70 static const char kLanguageListCallbackName[];
71 static const char kTargetLanguagesKey[];
72 static const char kAlphaLanguagesKey[];
75 // Callback function called when TranslateURLFetcher::Request() is finished.
76 void OnLanguageListFetchComplete(int id,
78 const std::string& data);
80 // Notifies the callback list of a translate event.
81 void NotifyEvent(int line, const std::string& message);
83 // Parses |language_list| containing the list of languages that the translate
84 // server can translate to and from.
85 void SetSupportedLanguages(const std::string& language_list);
87 // Callbacks called on translate events.
88 EventCallbackList callback_list_;
90 // Whether the language list can be requested.
91 bool resource_requests_allowed_;
93 // True if the list has to be fetched when resource requests are allowed.
94 bool request_pending_;
96 // All the languages supported by the translation server.
97 std::set<std::string> all_supported_languages_;
99 // Alpha languages supported by the translation server.
100 std::set<std::string> alpha_languages_;
102 // A LanguageListFetcher instance to fetch a server providing supported
103 // language list including alpha languages.
104 scoped_ptr<TranslateURLFetcher> language_list_fetcher_;
106 // The last-updated time when the language list is sent.
107 base::Time last_updated_;
109 DISALLOW_COPY_AND_ASSIGN(TranslateLanguageList);
112 #endif // COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_LANGUAGE_LIST_H_