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.
5 #ifndef CHROME_BROWSER_TRANSLATE_TRANSLATE_MANAGER_H_
6 #define CHROME_BROWSER_TRANSLATE_TRANSLATE_MANAGER_H_
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 "chrome/common/translate/translate_errors.h"
19 #include "content/public/browser/notification_observer.h"
20 #include "content/public/browser/notification_registrar.h"
22 template <typename T> struct DefaultSingletonTraits;
24 struct LanguageDetectionDetails;
25 struct PageTranslatedDetails;
28 struct ShortcutConfiguration;
29 class TranslateAcceptLanguages;
30 struct TranslateErrorDetails;
31 struct TranslateEventDetails;
32 class TranslateInfoBarDelegate;
33 class TranslateLanguageList;
34 class TranslateScript;
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.
49 class TranslateManager : public content::NotificationObserver {
51 // Returns the singleton instance.
52 static TranslateManager* GetInstance();
54 virtual ~TranslateManager();
56 // Returns true if the URL can be translated.
57 static bool IsTranslatableURL(const GURL& url);
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);
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();
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);
71 // Returns true if |language| is supported by the translation server.
72 static bool IsSupportedLanguage(const std::string& language);
74 // Returns true if |language| is supported by the translation server as a
76 static bool IsAlphaLanguage(const std::string& language);
78 // Returns true if |language| is an Accept language for the user profile.
79 static bool IsAcceptLanguage(Profile* profile, const std::string& language);
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:
85 // the accept-language list
86 // If no language is found then an empty string is returned.
87 static std::string GetTargetLanguage(PrefService* prefs);
89 // Let the caller decide if and when we should fetch the language list from
90 // the translate server. This is a NOOP if switches::kDisableTranslate is set
91 // or if prefs::kEnableTranslate is set to false.
92 void FetchLanguageListFromTranslateServer(PrefService* prefs);
94 // Allows caller to cleanup pending URLFetcher objects to make sure they
95 // get released in the appropriate thread... Mainly for tests.
96 void CleanupPendingUlrFetcher();
98 // Translates the page contents from |source_lang| to |target_lang|.
99 // The actual translation might be performed asynchronously if the translate
100 // script is not yet available.
101 void TranslatePage(content::WebContents* web_contents,
102 const std::string& source_lang,
103 const std::string& target_lang);
105 // Reverts the contents of the page in |web_contents| to its original
107 void RevertTranslation(content::WebContents* web_contents);
109 // Reports to the Google translate server that a page language was incorrectly
110 // detected. This call is initiated by the user selecting the "report" menu
111 // under options in the translate infobar.
112 void ReportLanguageDetectionError(content::WebContents* web_contents);
114 // Clears the translate script, so it will be fetched next time we translate.
115 void ClearTranslateScript();
117 // content::NotificationObserver implementation:
118 virtual void Observe(int type,
119 const content::NotificationSource& source,
120 const content::NotificationDetails& details) OVERRIDE;
122 // Used by unit-tests to override some defaults:
123 // Delay after which the translate script is fetched again from the
124 // translation server.
125 void SetTranslateScriptExpirationDelay(int delay_ms);
127 // Number of attempts before waiting for a page to be fully reloaded.
128 void set_translate_max_reload_attemps(int attempts) {
129 max_reload_check_attempts_ = attempts;
132 // The observer class for TranslateManager.
135 virtual void OnLanguageDetection(
136 const LanguageDetectionDetails& details) = 0;
137 virtual void OnTranslateError(
138 const TranslateErrorDetails& details) = 0;
139 virtual void OnTranslateEvent(
140 const TranslateEventDetails& details) = 0;
143 // Adds/removes observer.
144 void AddObserver(Observer* obs);
145 void RemoveObserver(Observer* obs);
147 // Notifies to the observers when translate event happens.
148 void NotifyTranslateEvent(const TranslateEventDetails& details);
154 friend struct DefaultSingletonTraits<TranslateManager>;
156 // Structure that describes a translate request.
157 // Translation may be deferred while the translate script is being retrieved
158 // from the translate server.
159 struct PendingRequest {
160 int render_process_id;
163 std::string source_lang;
164 std::string target_lang;
167 // Starts the translation process on |tab| containing the page in the
168 // |page_lang| language.
169 void InitiateTranslation(content::WebContents* web_contents,
170 const std::string& page_lang);
172 // If the tab identified by |process_id| and |render_id| has been closed, this
173 // does nothing, otherwise it calls InitiateTranslation.
174 void InitiateTranslationPosted(int process_id, int render_id,
175 const std::string& page_lang, int attempt);
177 // Sends a translation request to the RenderView of |web_contents|.
178 void DoTranslatePage(content::WebContents* web_contents,
179 const std::string& translate_script,
180 const std::string& source_lang,
181 const std::string& target_lang);
183 // Shows the after translate or error infobar depending on the details.
184 void PageTranslated(content::WebContents* web_contents,
185 PageTranslatedDetails* details);
187 void OnTranslateScriptFetchComplete(bool success, const std::string& data);
189 // Notifies to the observers when a language is detected.
190 void NotifyLanguageDetection(const LanguageDetectionDetails& details);
192 // Notifies to the observers when translate failed.
193 void NotifyTranslateError(const TranslateErrorDetails& details);
195 // Shows the translate bubble.
196 void ShowBubble(content::WebContents* web_contents,
197 TranslateBubbleModel::ViewState view_state);
199 // Returns the different parameters used to decide whether extra shortcuts
201 static ShortcutConfiguration ShortcutConfig();
203 content::NotificationRegistrar notification_registrar_;
205 // Max number of attempts before checking if a page has been reloaded.
206 int max_reload_check_attempts_;
208 // The list of pending translate requests. Translate requests are queued when
209 // the translate script is not ready and has to be fetched from the translate
211 std::vector<PendingRequest> pending_requests_;
213 // List of registered observers.
214 ObserverList<Observer> observer_list_;
216 // An instance of TranslateLanguageList which manages supported language list.
217 scoped_ptr<TranslateLanguageList> language_list_;
219 // An instance of TranslateScript which manages JavaScript source for
221 scoped_ptr<TranslateScript> script_;
223 // An instance of TranslateAcceptLanguages which manages Accept languages of
225 scoped_ptr<TranslateAcceptLanguages> accept_languages_;
227 base::WeakPtrFactory<TranslateManager> weak_method_factory_;
229 DISALLOW_COPY_AND_ASSIGN(TranslateManager);
232 #endif // CHROME_BROWSER_TRANSLATE_TRANSLATE_MANAGER_H_