diff options
Diffstat (limited to 'chrome/browser/translate/translate_manager.h')
-rw-r--r-- | chrome/browser/translate/translate_manager.h | 77 |
1 files changed, 72 insertions, 5 deletions
diff --git a/chrome/browser/translate/translate_manager.h b/chrome/browser/translate/translate_manager.h index cf0e452..5c7bbb2 100644 --- a/chrome/browser/translate/translate_manager.h +++ b/chrome/browser/translate/translate_manager.h @@ -8,9 +8,12 @@ #include <map> #include <set> #include <string> +#include <vector> +#include "base/lazy_instance.h" #include "base/singleton.h" #include "base/task.h" +#include "chrome/browser/net/url_fetcher.h" #include "chrome/browser/translate/translate_infobars_delegates.h" #include "chrome/common/notification_observer.h" #include "chrome/common/notification_registrar.h" @@ -25,7 +28,8 @@ class TabContents; // page translation the user requests. // It is a singleton. -class TranslateManager : public NotificationObserver { +class TranslateManager : public NotificationObserver, + public URLFetcher::Delegate { public: virtual ~TranslateManager(); @@ -34,6 +38,29 @@ class TranslateManager : public NotificationObserver { const NotificationSource& source, const NotificationDetails& details); + // URLFetcher::Delegate implementation: + virtual void OnURLFetchComplete(const URLFetcher* source, + const GURL& url, + const URLRequestStatus& status, + int response_code, + const ResponseCookies& cookies, + const std::string& data); + + // Translates the page contents from |source_lang| to |target_lang|. + // The actual translation might be performed asynchronously if the translate + // script is not yet available. + void TranslatePage(TabContents* tab_contents, + const std::string& source_lang, + const std::string& target_lang); + + // Reverts the contents of the page in |tab_contents| to its original + // language. + void RevertTranslation(TabContents* tab_contents); + + // Clears the translate script, so it will be fetched next time we translate. + // Currently used by unit-tests. + void ClearTranslateScript() { translate_script_.clear(); } + // Convenience method to know if a tab is showing a translate infobar. static bool IsShowingTranslateInfobar(TabContents* tab); @@ -41,9 +68,16 @@ class TranslateManager : public NotificationObserver { // (chrome:// and others). static bool IsTranslatableURL(const GURL& url); - // Used by unit-test to enable the TranslateManager for testing purpose. - static void set_test_enabled(bool enabled) { test_enabled_ = enabled; } - static bool test_enabled() { return test_enabled_; } + // Fills |languages| with the list of languages that the translate server can + // translate to and from. + static void GetSupportedLanguages(std::vector<std::string>* languages); + + // Returns the language code that can be used with the Translate method for a + // specified |chrome_locale|. + static std::string GetLanguageCode(const std::string& chrome_locale); + + // Returns true if |page_language| is supported by the translation server. + static bool IsSupportedLanguage(const std::string& page_language); protected: TranslateManager(); @@ -51,6 +85,17 @@ class TranslateManager : public NotificationObserver { private: friend struct DefaultSingletonTraits<TranslateManager>; + // Structure that describes a translate request. + // Translation may be deferred while the translate script is being retrieved + // from the translate server. + struct PendingRequest { + int render_process_id; + int render_view_id; + int page_id; + std::string source_lang; + std::string target_lang; + }; + // Starts the translation process on |tab| containing the page in the // |page_lang| language. void InitiateTranslation(TabContents* tab, const std::string& page_lang); @@ -61,6 +106,12 @@ class TranslateManager : public NotificationObserver { int render_id, const std::string& page_lang); + // Sends a translation request to the RenderView of |tab_contents|. + void DoTranslatePage(TabContents* tab_contents, + const std::string& translate_script, + const std::string& source_lang, + const std::string& target_lang); + // Returns true if the passed language has been configured by the user as an // accept language. bool IsAcceptLanguage(TabContents* tab, const std::string& language); @@ -69,6 +120,10 @@ class TranslateManager : public NotificationObserver { // preference in |prefs|. void InitAcceptLanguages(PrefService* prefs); + // Fetches the JS translate script (the script that is injected in the page + // to translate it). + void RequestTranslateScript(); + // Convenience method that adds a translate infobar to |tab|. static void AddTranslateInfoBar( TabContents* tab, @@ -92,7 +147,19 @@ class TranslateManager : public NotificationObserver { ScopedRunnableMethodFactory<TranslateManager> method_factory_; - static bool test_enabled_; + // The JS injected in the page to do the translation. + std::string translate_script_; + + // Whether the translate JS is currently being retrieved. + bool translate_script_request_pending_; + + // The list of pending translate requests. Translate requests are queued when + // the translate script is not ready and has to be fetched from the translate + // server. + std::vector<PendingRequest> pending_requests_; + + // The languages supported by the translation server. + static base::LazyInstance<std::set<std::string> > supported_languages_; DISALLOW_COPY_AND_ASSIGN(TranslateManager); }; |