| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_TRANSLATE_TRANSLATE_MANAGER_H_ | 5 #ifndef CHROME_BROWSER_TRANSLATE_TRANSLATE_MANAGER_H_ |
| 6 #define CHROME_BROWSER_TRANSLATE_TRANSLATE_MANAGER_H_ | 6 #define CHROME_BROWSER_TRANSLATE_TRANSLATE_MANAGER_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <map> | 9 #include <map> |
| 10 #include <set> | 10 #include <set> |
| 11 #include <string> | 11 #include <string> |
| 12 #include <vector> | 12 #include <vector> |
| 13 | 13 |
| 14 #include "base/lazy_instance.h" | 14 #include "base/lazy_instance.h" |
| 15 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
| 16 #include "base/memory/weak_ptr.h" | 16 #include "base/memory/weak_ptr.h" |
| 17 #include "chrome/browser/prefs/pref_change_registrar.h" | 17 #include "chrome/browser/prefs/pref_change_registrar.h" |
| 18 #include "chrome/common/translate_errors.h" | 18 #include "chrome/common/translate_errors.h" |
| 19 #include "content/public/common/url_fetcher_delegate.h" | 19 #include "content/public/common/url_fetcher_delegate.h" |
| 20 #include "content/public/browser/notification_observer.h" | 20 #include "content/public/browser/notification_observer.h" |
| 21 #include "content/public/browser/notification_registrar.h" | 21 #include "content/public/browser/notification_registrar.h" |
| 22 | 22 |
| 23 template <typename T> struct DefaultSingletonTraits; | 23 template <typename T> struct DefaultSingletonTraits; |
| 24 class GURL; | 24 class GURL; |
| 25 struct PageTranslatedDetails; | 25 struct PageTranslatedDetails; |
| 26 class PrefService; | 26 class PrefService; |
| 27 class TabContents; | |
| 28 class TranslateInfoBarDelegate; | 27 class TranslateInfoBarDelegate; |
| 29 | 28 |
| 30 namespace content { | 29 namespace content { |
| 31 class WebContents; | 30 class WebContents; |
| 32 } | 31 } |
| 33 | 32 |
| 34 // The TranslateManager class is responsible for showing an info-bar when a page | 33 // The TranslateManager class is responsible for showing an info-bar when a page |
| 35 // in a language different than the user language is loaded. It triggers the | 34 // in a language different than the user language is loaded. It triggers the |
| 36 // page translation the user requests. | 35 // page translation the user requests. |
| 37 // It is a singleton. | 36 // It is a singleton. |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 81 // content::URLFetcherDelegate implementation: | 80 // content::URLFetcherDelegate implementation: |
| 82 virtual void OnURLFetchComplete(const content::URLFetcher* source) OVERRIDE; | 81 virtual void OnURLFetchComplete(const content::URLFetcher* source) OVERRIDE; |
| 83 | 82 |
| 84 // Used by unit-tests to override the default delay after which the translate | 83 // Used by unit-tests to override the default delay after which the translate |
| 85 // script is fetched again from the translation server. | 84 // script is fetched again from the translation server. |
| 86 void set_translate_script_expiration_delay(int delay_ms) { | 85 void set_translate_script_expiration_delay(int delay_ms) { |
| 87 translate_script_expiration_delay_ = delay_ms; | 86 translate_script_expiration_delay_ = delay_ms; |
| 88 } | 87 } |
| 89 | 88 |
| 90 // Convenience method to know if a tab is showing a translate infobar. | 89 // Convenience method to know if a tab is showing a translate infobar. |
| 91 static bool IsShowingTranslateInfobar(TabContents* tab); | 90 static bool IsShowingTranslateInfobar(content::WebContents* tab); |
| 92 | 91 |
| 93 // Returns true if the URL can be translated. | 92 // Returns true if the URL can be translated. |
| 94 static bool IsTranslatableURL(const GURL& url); | 93 static bool IsTranslatableURL(const GURL& url); |
| 95 | 94 |
| 96 // Fills |languages| with the list of languages that the translate server can | 95 // Fills |languages| with the list of languages that the translate server can |
| 97 // translate to and from. | 96 // translate to and from. |
| 98 static void GetSupportedLanguages(std::vector<std::string>* languages); | 97 static void GetSupportedLanguages(std::vector<std::string>* languages); |
| 99 | 98 |
| 100 // Returns the language code that can be used with the Translate method for a | 99 // Returns the language code that can be used with the Translate method for a |
| 101 // specified |chrome_locale|. | 100 // specified |chrome_locale|. |
| (...skipping 25 matching lines...) Expand all Loading... |
| 127 // Fills supported_languages_ with the list of languages that the translate | 126 // Fills supported_languages_ with the list of languages that the translate |
| 128 // server can translate to and from. | 127 // server can translate to and from. |
| 129 static void SetSupportedLanguages(const std::string& language_list); | 128 static void SetSupportedLanguages(const std::string& language_list); |
| 130 | 129 |
| 131 // Initializes the list of supported languages if it wasn't initialized before | 130 // Initializes the list of supported languages if it wasn't initialized before |
| 132 // in case we failed to get them from the server, or didn't get them just yet. | 131 // in case we failed to get them from the server, or didn't get them just yet. |
| 133 static void InitSupportedLanguages(); | 132 static void InitSupportedLanguages(); |
| 134 | 133 |
| 135 // Starts the translation process on |tab| containing the page in the | 134 // Starts the translation process on |tab| containing the page in the |
| 136 // |page_lang| language. | 135 // |page_lang| language. |
| 137 void InitiateTranslation(TabContents* tab, const std::string& page_lang); | 136 void InitiateTranslation(content::WebContents* tab, |
| 137 const std::string& page_lang); |
| 138 | 138 |
| 139 // If the tab identified by |process_id| and |render_id| has been closed, this | 139 // If the tab identified by |process_id| and |render_id| has been closed, this |
| 140 // does nothing, otherwise it calls InitiateTranslation. | 140 // does nothing, otherwise it calls InitiateTranslation. |
| 141 void InitiateTranslationPosted(int process_id, | 141 void InitiateTranslationPosted(int process_id, |
| 142 int render_id, | 142 int render_id, |
| 143 const std::string& page_lang); | 143 const std::string& page_lang); |
| 144 | 144 |
| 145 // Sends a translation request to the RenderView of |tab_contents|. | 145 // Sends a translation request to the RenderView of |tab_contents|. |
| 146 void DoTranslatePage(content::WebContents* web_contents, | 146 void DoTranslatePage(content::WebContents* web_contents, |
| 147 const std::string& translate_script, | 147 const std::string& translate_script, |
| 148 const std::string& source_lang, | 148 const std::string& source_lang, |
| 149 const std::string& target_lang); | 149 const std::string& target_lang); |
| 150 | 150 |
| 151 // Shows the after translate or error infobar depending on the details. | 151 // Shows the after translate or error infobar depending on the details. |
| 152 void PageTranslated(TabContents* tab, PageTranslatedDetails* details); | 152 void PageTranslated(content::WebContents* tab, |
| 153 PageTranslatedDetails* details); |
| 153 | 154 |
| 154 // Returns true if the passed language has been configured by the user as an | 155 // Returns true if the passed language has been configured by the user as an |
| 155 // accept language. | 156 // accept language. |
| 156 bool IsAcceptLanguage(TabContents* tab, const std::string& language); | 157 bool IsAcceptLanguage(content::WebContents* tab, const std::string& language); |
| 157 | 158 |
| 158 // Initializes the |accept_languages_| language table based on the associated | 159 // Initializes the |accept_languages_| language table based on the associated |
| 159 // preference in |prefs|. | 160 // preference in |prefs|. |
| 160 void InitAcceptLanguages(PrefService* prefs); | 161 void InitAcceptLanguages(PrefService* prefs); |
| 161 | 162 |
| 162 // Fetches the JS translate script (the script that is injected in the page | 163 // Fetches the JS translate script (the script that is injected in the page |
| 163 // to translate it). | 164 // to translate it). |
| 164 void RequestTranslateScript(); | 165 void RequestTranslateScript(); |
| 165 | 166 |
| 166 // Shows the specified translate |infobar| in the given |tab|. If a current | 167 // Shows the specified translate |infobar| in the given |tab|. If a current |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 213 // server. | 214 // server. |
| 214 std::vector<PendingRequest> pending_requests_; | 215 std::vector<PendingRequest> pending_requests_; |
| 215 | 216 |
| 216 // The languages supported by the translation server. | 217 // The languages supported by the translation server. |
| 217 static base::LazyInstance<std::set<std::string> > supported_languages_; | 218 static base::LazyInstance<std::set<std::string> > supported_languages_; |
| 218 | 219 |
| 219 DISALLOW_COPY_AND_ASSIGN(TranslateManager); | 220 DISALLOW_COPY_AND_ASSIGN(TranslateManager); |
| 220 }; | 221 }; |
| 221 | 222 |
| 222 #endif // CHROME_BROWSER_TRANSLATE_TRANSLATE_MANAGER_H_ | 223 #endif // CHROME_BROWSER_TRANSLATE_TRANSLATE_MANAGER_H_ |
| OLD | NEW |