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