| 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> |
| (...skipping 21 matching lines...) Expand all Loading... |
| 32 // It is a singleton. | 32 // It is a singleton. |
| 33 | 33 |
| 34 class TranslateManager : public NotificationObserver, | 34 class TranslateManager : public NotificationObserver, |
| 35 public URLFetcher::Delegate { | 35 public URLFetcher::Delegate { |
| 36 public: | 36 public: |
| 37 // Returns the singleton instance. | 37 // Returns the singleton instance. |
| 38 static TranslateManager* GetInstance(); | 38 static TranslateManager* GetInstance(); |
| 39 | 39 |
| 40 virtual ~TranslateManager(); | 40 virtual ~TranslateManager(); |
| 41 | 41 |
| 42 // Let the caller decide if and when we should fetch the language list from |
| 43 // the translate server. This is a NOOP if switches::kDisableTranslate is |
| 44 // set or if prefs::kEnableTranslate is set to false. |
| 45 // It will not retry more than kMaxRetryLanguageListFetch times. |
| 46 void FetchLanguageListFromTranslateServer(PrefService* prefs); |
| 47 |
| 42 // Translates the page contents from |source_lang| to |target_lang|. | 48 // Translates the page contents from |source_lang| to |target_lang|. |
| 43 // The actual translation might be performed asynchronously if the translate | 49 // The actual translation might be performed asynchronously if the translate |
| 44 // script is not yet available. | 50 // script is not yet available. |
| 45 void TranslatePage(TabContents* tab_contents, | 51 void TranslatePage(TabContents* tab_contents, |
| 46 const std::string& source_lang, | 52 const std::string& source_lang, |
| 47 const std::string& target_lang); | 53 const std::string& target_lang); |
| 48 | 54 |
| 49 // Reverts the contents of the page in |tab_contents| to its original | 55 // Reverts the contents of the page in |tab_contents| to its original |
| 50 // language. | 56 // language. |
| 51 void RevertTranslation(TabContents* tab_contents); | 57 void RevertTranslation(TabContents* tab_contents); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 // translate to and from. | 93 // translate to and from. |
| 88 static void GetSupportedLanguages(std::vector<std::string>* languages); | 94 static void GetSupportedLanguages(std::vector<std::string>* languages); |
| 89 | 95 |
| 90 // Returns the language code that can be used with the Translate method for a | 96 // Returns the language code that can be used with the Translate method for a |
| 91 // specified |chrome_locale|. | 97 // specified |chrome_locale|. |
| 92 static std::string GetLanguageCode(const std::string& chrome_locale); | 98 static std::string GetLanguageCode(const std::string& chrome_locale); |
| 93 | 99 |
| 94 // Returns true if |language| is supported by the translation server. | 100 // Returns true if |language| is supported by the translation server. |
| 95 static bool IsSupportedLanguage(const std::string& language); | 101 static bool IsSupportedLanguage(const std::string& language); |
| 96 | 102 |
| 103 // static const values shared with our browser tests. |
| 104 static const char* const kLanguageListCallbackName; |
| 105 static const char* const kTargetLanguagesKey; |
| 97 protected: | 106 protected: |
| 98 TranslateManager(); | 107 TranslateManager(); |
| 99 | 108 |
| 100 private: | 109 private: |
| 101 friend struct DefaultSingletonTraits<TranslateManager>; | 110 friend struct DefaultSingletonTraits<TranslateManager>; |
| 102 | 111 |
| 103 // Structure that describes a translate request. | 112 // Structure that describes a translate request. |
| 104 // Translation may be deferred while the translate script is being retrieved | 113 // Translation may be deferred while the translate script is being retrieved |
| 105 // from the translate server. | 114 // from the translate server. |
| 106 struct PendingRequest { | 115 struct PendingRequest { |
| 107 int render_process_id; | 116 int render_process_id; |
| 108 int render_view_id; | 117 int render_view_id; |
| 109 int page_id; | 118 int page_id; |
| 110 std::string source_lang; | 119 std::string source_lang; |
| 111 std::string target_lang; | 120 std::string target_lang; |
| 112 }; | 121 }; |
| 113 | 122 |
| 123 // Fills supported_languages_ with the list of languages that the translate |
| 124 // server can translate to and from. |
| 125 static void SetSupportedLanguages(const std::string& language_list); |
| 126 |
| 127 // Initializes the list of supported languages if it wasn't initialized before |
| 128 // in case we failed to get them from the server, or didn't get them just yet. |
| 129 static void InitSupportedLanguages(); |
| 130 |
| 114 // Starts the translation process on |tab| containing the page in the | 131 // Starts the translation process on |tab| containing the page in the |
| 115 // |page_lang| language. | 132 // |page_lang| language. |
| 116 void InitiateTranslation(TabContents* tab, const std::string& page_lang); | 133 void InitiateTranslation(TabContents* tab, const std::string& page_lang); |
| 117 | 134 |
| 118 // If the tab identified by |process_id| and |render_id| has been closed, this | 135 // If the tab identified by |process_id| and |render_id| has been closed, this |
| 119 // does nothing, otherwise it calls InitiateTranslation. | 136 // does nothing, otherwise it calls InitiateTranslation. |
| 120 void InitiateTranslationPosted(int process_id, | 137 void InitiateTranslationPosted(int process_id, |
| 121 int render_id, | 138 int render_id, |
| 122 const std::string& page_lang); | 139 const std::string& page_lang); |
| 123 | 140 |
| 124 // Sends a translation request to the RenderView of |tab_contents|. | 141 // Sends a translation request to the RenderView of |tab_contents|. |
| 125 void DoTranslatePage(TabContents* tab_contents, | 142 void DoTranslatePage(TabContents* tab_contents, |
| 126 const std::string& translate_script, | 143 const std::string& translate_script, |
| 127 const std::string& source_lang, | 144 const std::string& source_lang, |
| 128 const std::string& target_lang); | 145 const std::string& target_lang); |
| 129 | 146 |
| 130 // Shows the after translate or error infobar depending on the details. | 147 // Shows the after translate or error infobar depending on the details. |
| 131 void PageTranslated(TabContents* tab, PageTranslatedDetails* details); | 148 void PageTranslated(TabContents* tab, PageTranslatedDetails* details); |
| 132 | 149 |
| 133 // Returns true if the passed language has been configured by the user as an | 150 // Returns true if the passed language has been configured by the user as an |
| 134 // accept language. | 151 // accept language. |
| 135 bool IsAcceptLanguage(TabContents* tab, const std::string& language); | 152 bool IsAcceptLanguage(TabContents* tab, const std::string& language); |
| 136 | 153 |
| 137 // Initializes the |accept_languages_| language table based on the associated | 154 // Initializes the |accept_languages_| language table based on the associated |
| 138 // preference in |prefs|. | 155 // preference in |prefs|. |
| 139 void InitAcceptLanguages(PrefService* prefs); | 156 void InitAcceptLanguages(PrefService* prefs); |
| 140 | 157 |
| 141 // Fetches the JS translate script (the script that is injected in the page | 158 // Fetches the JS translate script (the script that is injected in the page |
| (...skipping 26 matching lines...) Expand all Loading... |
| 168 // The JS injected in the page to do the translation. | 185 // The JS injected in the page to do the translation. |
| 169 std::string translate_script_; | 186 std::string translate_script_; |
| 170 | 187 |
| 171 // Delay in milli-seconds after which the translate script is fetched again | 188 // Delay in milli-seconds after which the translate script is fetched again |
| 172 // from the translate server. | 189 // from the translate server. |
| 173 int translate_script_expiration_delay_; | 190 int translate_script_expiration_delay_; |
| 174 | 191 |
| 175 // Whether the translate JS is currently being retrieved. | 192 // Whether the translate JS is currently being retrieved. |
| 176 bool translate_script_request_pending_; | 193 bool translate_script_request_pending_; |
| 177 | 194 |
| 195 // Whether the list of languages is currently being retrieved. |
| 196 bool language_list_request_pending_; |
| 197 |
| 178 // The list of pending translate requests. Translate requests are queued when | 198 // The list of pending translate requests. Translate requests are queued when |
| 179 // the translate script is not ready and has to be fetched from the translate | 199 // the translate script is not ready and has to be fetched from the translate |
| 180 // server. | 200 // server. |
| 181 std::vector<PendingRequest> pending_requests_; | 201 std::vector<PendingRequest> pending_requests_; |
| 182 | 202 |
| 183 // The languages supported by the translation server. | 203 // The languages supported by the translation server. |
| 184 static base::LazyInstance<std::set<std::string> > supported_languages_; | 204 static base::LazyInstance<std::set<std::string> > supported_languages_; |
| 185 | 205 |
| 186 DISALLOW_COPY_AND_ASSIGN(TranslateManager); | 206 DISALLOW_COPY_AND_ASSIGN(TranslateManager); |
| 187 }; | 207 }; |
| 188 | 208 |
| 189 #endif // CHROME_BROWSER_TRANSLATE_TRANSLATE_MANAGER_H_ | 209 #endif // CHROME_BROWSER_TRANSLATE_TRANSLATE_MANAGER_H_ |
| OLD | NEW |