Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / components / translate / core / browser / translate_language_list.h
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 TranslateURLFetcher;
18
19 // The TranslateLanguageList class is responsible for maintaining the latest
20 // supporting language list.
21 class TranslateLanguageList {
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 |language|. (ex. GetLanguageCode("en-US") will return "en", and
38   // GetLanguageCode("zh-CN") returns "zh-CN")
39   std::string GetLanguageCode(const std::string& language);
40
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);
44
45   // Returns true if |language| is supported by the translation server as a
46   // alpha language.
47   bool IsAlphaLanguage(const std::string& language);
48
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();
52
53   // Sets whether requests are allowed. If |allowed| is true, this resumes any
54   // pending request.
55   void SetResourceRequestsAllowed(bool allowed);
56
57   typedef base::Callback<void(const TranslateEventDetails&)> EventCallback;
58   typedef base::CallbackList<void(const TranslateEventDetails&)>
59       EventCallbackList;
60
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);
65
66   // Disables the language list updater. This is used only for testing now.
67   static void DisableUpdate();
68
69   // static const values shared with our browser tests.
70   static const char kLanguageListCallbackName[];
71   static const char kTargetLanguagesKey[];
72   static const char kAlphaLanguagesKey[];
73
74  private:
75   // Callback function called when TranslateURLFetcher::Request() is finished.
76   void OnLanguageListFetchComplete(int id,
77                                    bool success,
78                                    const std::string& data);
79
80   // Notifies the callback list of a translate event.
81   void NotifyEvent(int line, const std::string& message);
82
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);
86
87   // Callbacks called on translate events.
88   EventCallbackList callback_list_;
89
90   // Whether the language list can be requested.
91   bool resource_requests_allowed_;
92
93   // True if the list has to be fetched when resource requests are allowed.
94   bool request_pending_;
95
96   // All the languages supported by the translation server.
97   std::set<std::string> all_supported_languages_;
98
99   // Alpha languages supported by the translation server.
100   std::set<std::string> alpha_languages_;
101
102   // A LanguageListFetcher instance to fetch a server providing supported
103   // language list including alpha languages.
104   scoped_ptr<TranslateURLFetcher> language_list_fetcher_;
105
106   // The last-updated time when the language list is sent.
107   base::Time last_updated_;
108
109   DISALLOW_COPY_AND_ASSIGN(TranslateLanguageList);
110 };
111
112 #endif  // COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_LANGUAGE_LIST_H_