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 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 // Translation may be deferred while the translate script is being retrieved | 110 // Translation may be deferred while the translate script is being retrieved |
105 // from the translate server. | 111 // from the translate server. |
106 struct PendingRequest { | 112 struct PendingRequest { |
107 int render_process_id; | 113 int render_process_id; |
108 int render_view_id; | 114 int render_view_id; |
109 int page_id; | 115 int page_id; |
110 std::string source_lang; | 116 std::string source_lang; |
111 std::string target_lang; | 117 std::string target_lang; |
112 }; | 118 }; |
113 | 119 |
| 120 // Fills supported_languages_ with the list of languages that the translate |
| 121 // server can translate to and from. |
| 122 static void SetSupportedLanguages(const std::string& language_list); |
| 123 |
| 124 // Initializes the list of supported languages if it wasn't initialized before |
| 125 // in case we failed to get them from the server, or didn't get them just yet. |
| 126 static void InitSupportedLanguages(); |
| 127 |
114 // Starts the translation process on |tab| containing the page in the | 128 // Starts the translation process on |tab| containing the page in the |
115 // |page_lang| language. | 129 // |page_lang| language. |
116 void InitiateTranslation(TabContents* tab, const std::string& page_lang); | 130 void InitiateTranslation(TabContents* tab, const std::string& page_lang); |
117 | 131 |
118 // If the tab identified by |process_id| and |render_id| has been closed, this | 132 // If the tab identified by |process_id| and |render_id| has been closed, this |
119 // does nothing, otherwise it calls InitiateTranslation. | 133 // does nothing, otherwise it calls InitiateTranslation. |
120 void InitiateTranslationPosted(int process_id, | 134 void InitiateTranslationPosted(int process_id, |
121 int render_id, | 135 int render_id, |
122 const std::string& page_lang); | 136 const std::string& page_lang); |
123 | 137 |
124 // Sends a translation request to the RenderView of |tab_contents|. | 138 // Sends a translation request to the RenderView of |tab_contents|. |
125 void DoTranslatePage(TabContents* tab_contents, | 139 void DoTranslatePage(TabContents* tab_contents, |
126 const std::string& translate_script, | 140 const std::string& translate_script, |
127 const std::string& source_lang, | 141 const std::string& source_lang, |
128 const std::string& target_lang); | 142 const std::string& target_lang); |
129 | 143 |
130 // Shows the after translate or error infobar depending on the details. | 144 // Shows the after translate or error infobar depending on the details. |
131 void PageTranslated(TabContents* tab, PageTranslatedDetails* details); | 145 void PageTranslated(TabContents* tab, PageTranslatedDetails* details); |
132 | 146 |
133 // Returns true if the passed language has been configured by the user as an | 147 // Returns true if the passed language has been configured by the user as an |
134 // accept language. | 148 // accept language. |
135 bool IsAcceptLanguage(TabContents* tab, const std::string& language); | 149 bool IsAcceptLanguage(TabContents* tab, const std::string& language); |
136 | 150 |
137 // Initializes the |accept_languages_| language table based on the associated | 151 // Initializes the |accept_languages_| language table based on the associated |
138 // preference in |prefs|. | 152 // preference in |prefs|. |
139 void InitAcceptLanguages(PrefService* prefs); | 153 void InitAcceptLanguages(PrefService* prefs); |
140 | 154 |
141 // Fetches the JS translate script (the script that is injected in the page | 155 // 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. | 182 // The JS injected in the page to do the translation. |
169 std::string translate_script_; | 183 std::string translate_script_; |
170 | 184 |
171 // Delay in milli-seconds after which the translate script is fetched again | 185 // Delay in milli-seconds after which the translate script is fetched again |
172 // from the translate server. | 186 // from the translate server. |
173 int translate_script_expiration_delay_; | 187 int translate_script_expiration_delay_; |
174 | 188 |
175 // Whether the translate JS is currently being retrieved. | 189 // Whether the translate JS is currently being retrieved. |
176 bool translate_script_request_pending_; | 190 bool translate_script_request_pending_; |
177 | 191 |
| 192 // Whether the list of languages is currently being retrieved. |
| 193 bool language_list_request_pending_; |
| 194 |
178 // The list of pending translate requests. Translate requests are queued when | 195 // 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 | 196 // the translate script is not ready and has to be fetched from the translate |
180 // server. | 197 // server. |
181 std::vector<PendingRequest> pending_requests_; | 198 std::vector<PendingRequest> pending_requests_; |
182 | 199 |
183 // The languages supported by the translation server. | 200 // The languages supported by the translation server. |
184 static base::LazyInstance<std::set<std::string> > supported_languages_; | 201 static base::LazyInstance<std::set<std::string> > supported_languages_; |
185 | 202 |
186 DISALLOW_COPY_AND_ASSIGN(TranslateManager); | 203 DISALLOW_COPY_AND_ASSIGN(TranslateManager); |
187 }; | 204 }; |
188 | 205 |
189 #endif // CHROME_BROWSER_TRANSLATE_TRANSLATE_MANAGER_H_ | 206 #endif // CHROME_BROWSER_TRANSLATE_TRANSLATE_MANAGER_H_ |
OLD | NEW |