612d31d375db5b88a2c6f6405d34f97e37c2f0fd
[platform/framework/web/crosswalk.git] / src / chrome / browser / translate / translate_manager.h
1 // Copyright (c) 2012 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_MANAGER_H_
6 #define CHROME_BROWSER_TRANSLATE_TRANSLATE_MANAGER_H_
7
8 #include <map>
9 #include <string>
10 #include <vector>
11
12 #include "base/gtest_prod_util.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/observer_list.h"
16 #include "base/time/time.h"
17 #include "chrome/browser/ui/translate/translate_bubble_model.h"
18 #include "components/translate/core/common/translate_errors.h"
19 #include "content/public/browser/notification_observer.h"
20 #include "content/public/browser/notification_registrar.h"
21
22 template <typename T> struct DefaultSingletonTraits;
23 class GURL;
24 struct LanguageDetectionDetails;
25 struct PageTranslatedDetails;
26 class PrefService;
27 class Profile;
28 struct ShortcutConfiguration;
29 class TranslateAcceptLanguages;
30 struct TranslateErrorDetails;
31 struct TranslateEventDetails;
32 class TranslateInfoBarDelegate;
33 class TranslateLanguageList;
34 class TranslateScript;
35
36 namespace content {
37 class WebContents;
38 }
39
40 namespace net {
41 class URLFetcher;
42 }
43
44 // The TranslateManager class is responsible for showing an info-bar when a page
45 // in a language different than the user language is loaded.  It triggers the
46 // page translation the user requests.
47 // It is a singleton.
48
49 class TranslateManager : public content::NotificationObserver {
50  public:
51   // Returns the singleton instance.
52   static TranslateManager* GetInstance();
53
54   virtual ~TranslateManager();
55
56   // Returns true if the URL can be translated.
57   static bool IsTranslatableURL(const GURL& url);
58
59   // Fills |languages| with the list of languages that the translate server can
60   // translate to and from.
61   static void GetSupportedLanguages(std::vector<std::string>* languages);
62
63   // Returns the last-updated time when Chrome receives a language list from a
64   // Translate server. Returns null time if Chrome hasn't received any lists.
65   static base::Time GetSupportedLanguagesLastUpdated();
66
67   // Returns the language code that can be used with the Translate method for a
68   // specified |chrome_locale|.
69   static std::string GetLanguageCode(const std::string& chrome_locale);
70
71   // Returns true if |language| is supported by the translation server.
72   static bool IsSupportedLanguage(const std::string& language);
73
74   // Returns true if |language| is supported by the translation server as a
75   // alpha language.
76   static bool IsAlphaLanguage(const std::string& language);
77
78   // Returns true if |language| is an Accept language for the user profile.
79   static bool IsAcceptLanguage(Profile* profile, const std::string& language);
80
81   // Returns the language to translate to. The language returned is the
82   // first language found in the following list that is supported by the
83   // translation service:
84   //     the UI language
85   //     the accept-language list
86   // If no language is found then an empty string is returned.
87   static std::string GetTargetLanguage(PrefService* prefs);
88
89   // Returns the language to automatically translate to. |original_language| is
90   // the webpage's original language.
91   static std::string GetAutoTargetLanguage(const std::string& original_language,
92                                            PrefService* prefs);
93
94   // Returns true if the new translate bubble is enabled.
95   static bool IsTranslateBubbleEnabled();
96
97   // Let the caller decide if and when we should fetch the language list from
98   // the translate server. This is a NOOP if switches::kDisableTranslate is set
99   // or if prefs::kEnableTranslate is set to false.
100   void FetchLanguageListFromTranslateServer(PrefService* prefs);
101
102   // Allows caller to cleanup pending URLFetcher objects to make sure they
103   // get released in the appropriate thread... Mainly for tests.
104   void CleanupPendingUlrFetcher();
105
106   // Translates the page contents from |source_lang| to |target_lang|.
107   // The actual translation might be performed asynchronously if the translate
108   // script is not yet available.
109   void TranslatePage(content::WebContents* web_contents,
110                      const std::string& source_lang,
111                      const std::string& target_lang);
112
113   // Reverts the contents of the page in |web_contents| to its original
114   // language.
115   void RevertTranslation(content::WebContents* web_contents);
116
117   // Reports to the Google translate server that a page language was incorrectly
118   // detected.  This call is initiated by the user selecting the "report" menu
119   // under options in the translate infobar.
120   void ReportLanguageDetectionError(content::WebContents* web_contents);
121
122   // Clears the translate script, so it will be fetched next time we translate.
123   void ClearTranslateScript();
124
125   // content::NotificationObserver implementation:
126   virtual void Observe(int type,
127                        const content::NotificationSource& source,
128                        const content::NotificationDetails& details) OVERRIDE;
129
130   // Used by unit-tests to override some defaults:
131   // Delay after which the translate script is fetched again from the
132   // translation server.
133   void SetTranslateScriptExpirationDelay(int delay_ms);
134
135   // Number of attempts before waiting for a page to be fully reloaded.
136   void set_translate_max_reload_attemps(int attempts) {
137     max_reload_check_attempts_ = attempts;
138   }
139
140   // The observer class for TranslateManager.
141   class Observer {
142    public:
143     virtual void OnLanguageDetection(
144         const LanguageDetectionDetails& details) = 0;
145     virtual void OnTranslateError(
146         const TranslateErrorDetails& details) = 0;
147     virtual void OnTranslateEvent(
148         const TranslateEventDetails& details) = 0;
149   };
150
151   // Adds/removes observer.
152   void AddObserver(Observer* obs);
153   void RemoveObserver(Observer* obs);
154
155   // Notifies to the observers when translate event happens.
156   void NotifyTranslateEvent(const TranslateEventDetails& details);
157
158  protected:
159   TranslateManager();
160
161  private:
162   friend struct DefaultSingletonTraits<TranslateManager>;
163
164   // Structure that describes a translate request.
165   // Translation may be deferred while the translate script is being retrieved
166   // from the translate server.
167   struct PendingRequest {
168     int render_process_id;
169     int render_view_id;
170     int page_id;
171     std::string source_lang;
172     std::string target_lang;
173   };
174
175   // Starts the translation process on |tab| containing the page in the
176   // |page_lang| language.
177   void InitiateTranslation(content::WebContents* web_contents,
178                            const std::string& page_lang);
179
180   // If the tab identified by |process_id| and |render_id| has been closed, this
181   // does nothing, otherwise it calls InitiateTranslation.
182   void InitiateTranslationPosted(int process_id, int render_id,
183                                  const std::string& page_lang, int attempt);
184
185   // Sends a translation request to the RenderView of |web_contents|.
186   void DoTranslatePage(content::WebContents* web_contents,
187                        const std::string& translate_script,
188                        const std::string& source_lang,
189                        const std::string& target_lang);
190
191   // Shows the after translate or error infobar depending on the details.
192   void PageTranslated(content::WebContents* web_contents,
193                       PageTranslatedDetails* details);
194
195   void OnTranslateScriptFetchComplete(bool success, const std::string& data);
196
197   // Notifies to the observers when a language is detected.
198   void NotifyLanguageDetection(const LanguageDetectionDetails& details);
199
200   // Notifies to the observers when translate failed.
201   void NotifyTranslateError(const TranslateErrorDetails& details);
202
203   // Shows the translate bubble.
204   void ShowBubble(content::WebContents* web_contents,
205                   TranslateBubbleModel::ViewState view_state,
206                   TranslateErrors::Type error_type);
207
208   // Returns the different parameters used to decide whether extra shortcuts
209   // are needed.
210   static ShortcutConfiguration ShortcutConfig();
211
212   content::NotificationRegistrar notification_registrar_;
213
214   // Max number of attempts before checking if a page has been reloaded.
215   int max_reload_check_attempts_;
216
217   // The list of pending translate requests.  Translate requests are queued when
218   // the translate script is not ready and has to be fetched from the translate
219   // server.
220   std::vector<PendingRequest> pending_requests_;
221
222   // List of registered observers.
223   ObserverList<Observer> observer_list_;
224
225   // An instance of TranslateLanguageList which manages supported language list.
226   scoped_ptr<TranslateLanguageList> language_list_;
227
228   // An instance of TranslateScript which manages JavaScript source for
229   // Translate.
230   scoped_ptr<TranslateScript> script_;
231
232   // An instance of TranslateAcceptLanguages which manages Accept languages of
233   // each profiles.
234   scoped_ptr<TranslateAcceptLanguages> accept_languages_;
235
236   base::WeakPtrFactory<TranslateManager> weak_method_factory_;
237
238   DISALLOW_COPY_AND_ASSIGN(TranslateManager);
239 };
240
241 #endif  // CHROME_BROWSER_TRANSLATE_TRANSLATE_MANAGER_H_